Linux-libre 4.14.12-gnu
[librecmc/linux-libre.git] / drivers / net / dsa / microchip / ksz_priv.h
1 /*
2  * Microchip KSZ series switch common definitions
3  *
4  * Copyright (C) 2017
5  *
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.
9  *
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.
17  */
18
19 #ifndef __KSZ_PRIV_H
20 #define __KSZ_PRIV_H
21
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>
27
28 #include "ksz_9477_reg.h"
29
30 struct ksz_io_ops;
31
32 struct vlan_table {
33         u32 table[3];
34 };
35
36 struct ksz_device {
37         struct dsa_switch *ds;
38         struct ksz_platform_data *pdata;
39         const char *name;
40
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;
46
47         struct device *dev;
48
49         void *priv;
50
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;
59
60         struct vlan_table *vlan_cache;
61
62         u64 mib_value[TOTAL_SWITCH_COUNTER_NUM];
63 };
64
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);
78 };
79
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);
85
86 static inline int ksz_read8(struct ksz_device *dev, u32 reg, u8 *val)
87 {
88         int ret;
89
90         mutex_lock(&dev->reg_mutex);
91         ret = dev->ops->read8(dev, reg, val);
92         mutex_unlock(&dev->reg_mutex);
93
94         return ret;
95 }
96
97 static inline int ksz_read16(struct ksz_device *dev, u32 reg, u16 *val)
98 {
99         int ret;
100
101         mutex_lock(&dev->reg_mutex);
102         ret = dev->ops->read16(dev, reg, val);
103         mutex_unlock(&dev->reg_mutex);
104
105         return ret;
106 }
107
108 static inline int ksz_read24(struct ksz_device *dev, u32 reg, u32 *val)
109 {
110         int ret;
111
112         mutex_lock(&dev->reg_mutex);
113         ret = dev->ops->read24(dev, reg, val);
114         mutex_unlock(&dev->reg_mutex);
115
116         return ret;
117 }
118
119 static inline int ksz_read32(struct ksz_device *dev, u32 reg, u32 *val)
120 {
121         int ret;
122
123         mutex_lock(&dev->reg_mutex);
124         ret = dev->ops->read32(dev, reg, val);
125         mutex_unlock(&dev->reg_mutex);
126
127         return ret;
128 }
129
130 static inline int ksz_write8(struct ksz_device *dev, u32 reg, u8 value)
131 {
132         int ret;
133
134         mutex_lock(&dev->reg_mutex);
135         ret = dev->ops->write8(dev, reg, value);
136         mutex_unlock(&dev->reg_mutex);
137
138         return ret;
139 }
140
141 static inline int ksz_write16(struct ksz_device *dev, u32 reg, u16 value)
142 {
143         int ret;
144
145         mutex_lock(&dev->reg_mutex);
146         ret = dev->ops->write16(dev, reg, value);
147         mutex_unlock(&dev->reg_mutex);
148
149         return ret;
150 }
151
152 static inline int ksz_write24(struct ksz_device *dev, u32 reg, u32 value)
153 {
154         int ret;
155
156         mutex_lock(&dev->reg_mutex);
157         ret = dev->ops->write24(dev, reg, value);
158         mutex_unlock(&dev->reg_mutex);
159
160         return ret;
161 }
162
163 static inline int ksz_write32(struct ksz_device *dev, u32 reg, u32 value)
164 {
165         int ret;
166
167         mutex_lock(&dev->reg_mutex);
168         ret = dev->ops->write32(dev, reg, value);
169         mutex_unlock(&dev->reg_mutex);
170
171         return ret;
172 }
173
174 static inline void ksz_pread8(struct ksz_device *dev, int port, int offset,
175                               u8 *data)
176 {
177         ksz_read8(dev, PORT_CTRL_ADDR(port, offset), data);
178 }
179
180 static inline void ksz_pread16(struct ksz_device *dev, int port, int offset,
181                                u16 *data)
182 {
183         ksz_read16(dev, PORT_CTRL_ADDR(port, offset), data);
184 }
185
186 static inline void ksz_pread32(struct ksz_device *dev, int port, int offset,
187                                u32 *data)
188 {
189         ksz_read32(dev, PORT_CTRL_ADDR(port, offset), data);
190 }
191
192 static inline void ksz_pwrite8(struct ksz_device *dev, int port, int offset,
193                                u8 data)
194 {
195         ksz_write8(dev, PORT_CTRL_ADDR(port, offset), data);
196 }
197
198 static inline void ksz_pwrite16(struct ksz_device *dev, int port, int offset,
199                                 u16 data)
200 {
201         ksz_write16(dev, PORT_CTRL_ADDR(port, offset), data);
202 }
203
204 static inline void ksz_pwrite32(struct ksz_device *dev, int port, int offset,
205                                 u32 data)
206 {
207         ksz_write32(dev, PORT_CTRL_ADDR(port, offset), data);
208 }
209
210 #endif