Linux 4.19.133
[linux/fpc-iii.git] / drivers / net / dsa / microchip / ksz_priv.h
blob2a98dbd51456fa0cd9011e924122b49aa1dc965e
1 /*
2 * Microchip KSZ series switch common definitions
4 * Copyright (C) 2017
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 #ifndef __KSZ_PRIV_H
20 #define __KSZ_PRIV_H
22 #include <linux/kernel.h>
23 #include <linux/mutex.h>
24 #include <linux/phy.h>
25 #include <linux/etherdevice.h>
26 #include <net/dsa.h>
28 #include "ksz_9477_reg.h"
30 struct ksz_io_ops;
32 struct vlan_table {
33 u32 table[3];
36 struct ksz_device {
37 struct dsa_switch *ds;
38 struct ksz_platform_data *pdata;
39 const char *name;
41 struct mutex reg_mutex; /* register access */
42 struct mutex stats_mutex; /* status access */
43 struct mutex alu_mutex; /* ALU access */
44 struct mutex vlan_mutex; /* vlan access */
45 const struct ksz_io_ops *ops;
47 struct device *dev;
49 void *priv;
51 /* chip specific data */
52 u32 chip_id;
53 int num_vlans;
54 int num_alus;
55 int num_statics;
56 int cpu_port; /* port connected to CPU */
57 int cpu_ports; /* port bitmap can be cpu port */
58 int port_cnt;
60 struct vlan_table *vlan_cache;
62 u64 mib_value[TOTAL_SWITCH_COUNTER_NUM];
65 struct ksz_io_ops {
66 int (*read8)(struct ksz_device *dev, u32 reg, u8 *value);
67 int (*read16)(struct ksz_device *dev, u32 reg, u16 *value);
68 int (*read24)(struct ksz_device *dev, u32 reg, u32 *value);
69 int (*read32)(struct ksz_device *dev, u32 reg, u32 *value);
70 int (*write8)(struct ksz_device *dev, u32 reg, u8 value);
71 int (*write16)(struct ksz_device *dev, u32 reg, u16 value);
72 int (*write24)(struct ksz_device *dev, u32 reg, u32 value);
73 int (*write32)(struct ksz_device *dev, u32 reg, u32 value);
74 int (*phy_read16)(struct ksz_device *dev, int addr, int reg,
75 u16 *value);
76 int (*phy_write16)(struct ksz_device *dev, int addr, int reg,
77 u16 value);
80 struct ksz_device *ksz_switch_alloc(struct device *base,
81 const struct ksz_io_ops *ops, void *priv);
82 int ksz_switch_detect(struct ksz_device *dev);
83 int ksz_switch_register(struct ksz_device *dev);
84 void ksz_switch_remove(struct ksz_device *dev);
86 static inline int ksz_read8(struct ksz_device *dev, u32 reg, u8 *val)
88 int ret;
90 mutex_lock(&dev->reg_mutex);
91 ret = dev->ops->read8(dev, reg, val);
92 mutex_unlock(&dev->reg_mutex);
94 return ret;
97 static inline int ksz_read16(struct ksz_device *dev, u32 reg, u16 *val)
99 int ret;
101 mutex_lock(&dev->reg_mutex);
102 ret = dev->ops->read16(dev, reg, val);
103 mutex_unlock(&dev->reg_mutex);
105 return ret;
108 static inline int ksz_read24(struct ksz_device *dev, u32 reg, u32 *val)
110 int ret;
112 mutex_lock(&dev->reg_mutex);
113 ret = dev->ops->read24(dev, reg, val);
114 mutex_unlock(&dev->reg_mutex);
116 return ret;
119 static inline int ksz_read32(struct ksz_device *dev, u32 reg, u32 *val)
121 int ret;
123 mutex_lock(&dev->reg_mutex);
124 ret = dev->ops->read32(dev, reg, val);
125 mutex_unlock(&dev->reg_mutex);
127 return ret;
130 static inline int ksz_write8(struct ksz_device *dev, u32 reg, u8 value)
132 int ret;
134 mutex_lock(&dev->reg_mutex);
135 ret = dev->ops->write8(dev, reg, value);
136 mutex_unlock(&dev->reg_mutex);
138 return ret;
141 static inline int ksz_write16(struct ksz_device *dev, u32 reg, u16 value)
143 int ret;
145 mutex_lock(&dev->reg_mutex);
146 ret = dev->ops->write16(dev, reg, value);
147 mutex_unlock(&dev->reg_mutex);
149 return ret;
152 static inline int ksz_write24(struct ksz_device *dev, u32 reg, u32 value)
154 int ret;
156 mutex_lock(&dev->reg_mutex);
157 ret = dev->ops->write24(dev, reg, value);
158 mutex_unlock(&dev->reg_mutex);
160 return ret;
163 static inline int ksz_write32(struct ksz_device *dev, u32 reg, u32 value)
165 int ret;
167 mutex_lock(&dev->reg_mutex);
168 ret = dev->ops->write32(dev, reg, value);
169 mutex_unlock(&dev->reg_mutex);
171 return ret;
174 static inline void ksz_pread8(struct ksz_device *dev, int port, int offset,
175 u8 *data)
177 ksz_read8(dev, PORT_CTRL_ADDR(port, offset), data);
180 static inline void ksz_pread16(struct ksz_device *dev, int port, int offset,
181 u16 *data)
183 ksz_read16(dev, PORT_CTRL_ADDR(port, offset), data);
186 static inline void ksz_pread32(struct ksz_device *dev, int port, int offset,
187 u32 *data)
189 ksz_read32(dev, PORT_CTRL_ADDR(port, offset), data);
192 static inline void ksz_pwrite8(struct ksz_device *dev, int port, int offset,
193 u8 data)
195 ksz_write8(dev, PORT_CTRL_ADDR(port, offset), data);
198 static inline void ksz_pwrite16(struct ksz_device *dev, int port, int offset,
199 u16 data)
201 ksz_write16(dev, PORT_CTRL_ADDR(port, offset), data);
204 static inline void ksz_pwrite32(struct ksz_device *dev, int port, int offset,
205 u32 data)
207 ksz_write32(dev, PORT_CTRL_ADDR(port, offset), data);
210 #endif