blob: c19842960f28dff15639572da533c19352e33d0c [file] [log] [blame]
Florian Fainelli967dd822016-06-09 18:23:53 -07001/*
2 * B53 common definitions
3 *
4 * Copyright (C) 2011-2013 Jonas Gorski <jogo@openwrt.org>
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 __B53_PRIV_H
20#define __B53_PRIV_H
21
22#include <linux/kernel.h>
23#include <linux/mutex.h>
24#include <linux/phy.h>
25#include <net/dsa.h>
26
Florian Fainelli1da6df82016-06-09 18:23:55 -070027#include "b53_regs.h"
28
Florian Fainelli967dd822016-06-09 18:23:53 -070029struct b53_device;
Florian Fainelliff39c2d2016-06-09 18:23:56 -070030struct net_device;
Florian Fainelli967dd822016-06-09 18:23:53 -070031
32struct b53_io_ops {
33 int (*read8)(struct b53_device *dev, u8 page, u8 reg, u8 *value);
34 int (*read16)(struct b53_device *dev, u8 page, u8 reg, u16 *value);
35 int (*read32)(struct b53_device *dev, u8 page, u8 reg, u32 *value);
36 int (*read48)(struct b53_device *dev, u8 page, u8 reg, u64 *value);
37 int (*read64)(struct b53_device *dev, u8 page, u8 reg, u64 *value);
38 int (*write8)(struct b53_device *dev, u8 page, u8 reg, u8 value);
39 int (*write16)(struct b53_device *dev, u8 page, u8 reg, u16 value);
40 int (*write32)(struct b53_device *dev, u8 page, u8 reg, u32 value);
41 int (*write48)(struct b53_device *dev, u8 page, u8 reg, u64 value);
42 int (*write64)(struct b53_device *dev, u8 page, u8 reg, u64 value);
43 int (*phy_read16)(struct b53_device *dev, int addr, int reg, u16 *value);
44 int (*phy_write16)(struct b53_device *dev, int addr, int reg, u16 value);
45};
46
47enum {
48 BCM5325_DEVICE_ID = 0x25,
49 BCM5365_DEVICE_ID = 0x65,
50 BCM5395_DEVICE_ID = 0x95,
51 BCM5397_DEVICE_ID = 0x97,
52 BCM5398_DEVICE_ID = 0x98,
53 BCM53115_DEVICE_ID = 0x53115,
54 BCM53125_DEVICE_ID = 0x53125,
55 BCM53128_DEVICE_ID = 0x53128,
56 BCM63XX_DEVICE_ID = 0x6300,
57 BCM53010_DEVICE_ID = 0x53010,
58 BCM53011_DEVICE_ID = 0x53011,
59 BCM53012_DEVICE_ID = 0x53012,
60 BCM53018_DEVICE_ID = 0x53018,
61 BCM53019_DEVICE_ID = 0x53019,
62};
63
64#define B53_N_PORTS 9
65#define B53_N_PORTS_25 6
66
67struct b53_port {
Florian Fainelliff39c2d2016-06-09 18:23:56 -070068 u16 vlan_ctl_mask;
69 struct net_device *bridge_dev;
Florian Fainelli967dd822016-06-09 18:23:53 -070070};
71
72struct b53_device {
73 struct dsa_switch *ds;
74 struct b53_platform_data *pdata;
75 const char *name;
76
77 struct mutex reg_mutex;
78 struct mutex stats_mutex;
79 const struct b53_io_ops *ops;
80
81 /* chip specific data */
82 u32 chip_id;
83 u8 core_rev;
84 u8 vta_regs[3];
85 u8 duplex_reg;
86 u8 jumbo_pm_reg;
87 u8 jumbo_size_reg;
88 int reset_gpio;
Florian Fainelli1da6df82016-06-09 18:23:55 -070089 u8 num_arl_entries;
Florian Fainelli967dd822016-06-09 18:23:53 -070090
91 /* used ports mask */
92 u16 enabled_ports;
93 unsigned int cpu_port;
94
95 /* connect specific data */
96 u8 current_page;
97 struct device *dev;
98
99 /* Master MDIO bus we got probed from */
100 struct mii_bus *bus;
101
102 /* Slave MDIO bus we created */
103 struct mii_bus *slave_bus;
104 void *priv;
105
106 /* run time configuration */
107 unsigned enable_jumbo:1;
108 unsigned allow_vid_4095:1;
109 unsigned int num_vlans;
110 unsigned int num_ports;
111 struct b53_port *ports;
112};
113
114#define b53_for_each_port(dev, i) \
115 for (i = 0; i < B53_N_PORTS; i++) \
116 if (dev->enabled_ports & BIT(i))
117
118
119static inline int is5325(struct b53_device *dev)
120{
121 return dev->chip_id == BCM5325_DEVICE_ID;
122}
123
124static inline int is5365(struct b53_device *dev)
125{
126#ifdef CONFIG_BCM47XX
127 return dev->chip_id == BCM5365_DEVICE_ID;
128#else
129 return 0;
130#endif
131}
132
133static inline int is5397_98(struct b53_device *dev)
134{
135 return dev->chip_id == BCM5397_DEVICE_ID ||
136 dev->chip_id == BCM5398_DEVICE_ID;
137}
138
139static inline int is539x(struct b53_device *dev)
140{
141 return dev->chip_id == BCM5395_DEVICE_ID ||
142 dev->chip_id == BCM5397_DEVICE_ID ||
143 dev->chip_id == BCM5398_DEVICE_ID;
144}
145
146static inline int is531x5(struct b53_device *dev)
147{
148 return dev->chip_id == BCM53115_DEVICE_ID ||
149 dev->chip_id == BCM53125_DEVICE_ID ||
150 dev->chip_id == BCM53128_DEVICE_ID;
151}
152
153static inline int is63xx(struct b53_device *dev)
154{
155#ifdef CONFIG_BCM63XX
156 return dev->chip_id == BCM63XX_DEVICE_ID;
157#else
158 return 0;
159#endif
160}
161
162static inline int is5301x(struct b53_device *dev)
163{
164 return dev->chip_id == BCM53010_DEVICE_ID ||
165 dev->chip_id == BCM53011_DEVICE_ID ||
166 dev->chip_id == BCM53012_DEVICE_ID ||
167 dev->chip_id == BCM53018_DEVICE_ID ||
168 dev->chip_id == BCM53019_DEVICE_ID;
169}
170
171#define B53_CPU_PORT_25 5
172#define B53_CPU_PORT 8
173
174static inline int is_cpu_port(struct b53_device *dev, int port)
175{
176 return dev->cpu_port;
177}
178
179struct b53_device *b53_switch_alloc(struct device *base, struct b53_io_ops *ops,
180 void *priv);
181
182int b53_switch_detect(struct b53_device *dev);
183
184int b53_switch_register(struct b53_device *dev);
185
186static inline void b53_switch_remove(struct b53_device *dev)
187{
188 dsa_unregister_switch(dev->ds);
189}
190
191static inline int b53_read8(struct b53_device *dev, u8 page, u8 reg, u8 *val)
192{
193 int ret;
194
195 mutex_lock(&dev->reg_mutex);
196 ret = dev->ops->read8(dev, page, reg, val);
197 mutex_unlock(&dev->reg_mutex);
198
199 return ret;
200}
201
202static inline int b53_read16(struct b53_device *dev, u8 page, u8 reg, u16 *val)
203{
204 int ret;
205
206 mutex_lock(&dev->reg_mutex);
207 ret = dev->ops->read16(dev, page, reg, val);
208 mutex_unlock(&dev->reg_mutex);
209
210 return ret;
211}
212
213static inline int b53_read32(struct b53_device *dev, u8 page, u8 reg, u32 *val)
214{
215 int ret;
216
217 mutex_lock(&dev->reg_mutex);
218 ret = dev->ops->read32(dev, page, reg, val);
219 mutex_unlock(&dev->reg_mutex);
220
221 return ret;
222}
223
224static inline int b53_read48(struct b53_device *dev, u8 page, u8 reg, u64 *val)
225{
226 int ret;
227
228 mutex_lock(&dev->reg_mutex);
229 ret = dev->ops->read48(dev, page, reg, val);
230 mutex_unlock(&dev->reg_mutex);
231
232 return ret;
233}
234
235static inline int b53_read64(struct b53_device *dev, u8 page, u8 reg, u64 *val)
236{
237 int ret;
238
239 mutex_lock(&dev->reg_mutex);
240 ret = dev->ops->read64(dev, page, reg, val);
241 mutex_unlock(&dev->reg_mutex);
242
243 return ret;
244}
245
246static inline int b53_write8(struct b53_device *dev, u8 page, u8 reg, u8 value)
247{
248 int ret;
249
250 mutex_lock(&dev->reg_mutex);
251 ret = dev->ops->write8(dev, page, reg, value);
252 mutex_unlock(&dev->reg_mutex);
253
254 return ret;
255}
256
257static inline int b53_write16(struct b53_device *dev, u8 page, u8 reg,
258 u16 value)
259{
260 int ret;
261
262 mutex_lock(&dev->reg_mutex);
263 ret = dev->ops->write16(dev, page, reg, value);
264 mutex_unlock(&dev->reg_mutex);
265
266 return ret;
267}
268
269static inline int b53_write32(struct b53_device *dev, u8 page, u8 reg,
270 u32 value)
271{
272 int ret;
273
274 mutex_lock(&dev->reg_mutex);
275 ret = dev->ops->write32(dev, page, reg, value);
276 mutex_unlock(&dev->reg_mutex);
277
278 return ret;
279}
280
281static inline int b53_write48(struct b53_device *dev, u8 page, u8 reg,
282 u64 value)
283{
284 int ret;
285
286 mutex_lock(&dev->reg_mutex);
287 ret = dev->ops->write48(dev, page, reg, value);
288 mutex_unlock(&dev->reg_mutex);
289
290 return ret;
291}
292
293static inline int b53_write64(struct b53_device *dev, u8 page, u8 reg,
294 u64 value)
295{
296 int ret;
297
298 mutex_lock(&dev->reg_mutex);
299 ret = dev->ops->write64(dev, page, reg, value);
300 mutex_unlock(&dev->reg_mutex);
301
302 return ret;
303}
304
Florian Fainelli1da6df82016-06-09 18:23:55 -0700305struct b53_arl_entry {
306 u8 port;
307 u8 mac[ETH_ALEN];
308 u16 vid;
309 u8 is_valid:1;
310 u8 is_age:1;
311 u8 is_static:1;
312};
313
314static inline void b53_mac_from_u64(u64 src, u8 *dst)
315{
316 unsigned int i;
317
318 for (i = 0; i < ETH_ALEN; i++)
319 dst[ETH_ALEN - 1 - i] = (src >> (8 * i)) & 0xff;
320}
321
322static inline u64 b53_mac_to_u64(const u8 *src)
323{
324 unsigned int i;
325 u64 dst = 0;
326
327 for (i = 0; i < ETH_ALEN; i++)
328 dst |= (u64)src[ETH_ALEN - 1 - i] << (8 * i);
329
330 return dst;
331}
332
333static inline void b53_arl_to_entry(struct b53_arl_entry *ent,
334 u64 mac_vid, u32 fwd_entry)
335{
336 memset(ent, 0, sizeof(*ent));
337 ent->port = fwd_entry & ARLTBL_DATA_PORT_ID_MASK;
338 ent->is_valid = !!(fwd_entry & ARLTBL_VALID);
339 ent->is_age = !!(fwd_entry & ARLTBL_AGE);
340 ent->is_static = !!(fwd_entry & ARLTBL_STATIC);
341 b53_mac_from_u64(mac_vid, ent->mac);
342 ent->vid = mac_vid >> ARLTBL_VID_S;
343}
344
345static inline void b53_arl_from_entry(u64 *mac_vid, u32 *fwd_entry,
346 const struct b53_arl_entry *ent)
347{
348 *mac_vid = b53_mac_to_u64(ent->mac);
349 *mac_vid |= (u64)(ent->vid & ARLTBL_VID_MASK) << ARLTBL_VID_S;
350 *fwd_entry = ent->port & ARLTBL_DATA_PORT_ID_MASK;
351 if (ent->is_valid)
352 *fwd_entry |= ARLTBL_VALID;
353 if (ent->is_static)
354 *fwd_entry |= ARLTBL_STATIC;
355 if (ent->is_age)
356 *fwd_entry |= ARLTBL_AGE;
357}
358
Florian Fainelli967dd822016-06-09 18:23:53 -0700359#ifdef CONFIG_BCM47XX
360
361#include <linux/version.h>
362#include <linux/bcm47xx_nvram.h>
363#include <bcm47xx_board.h>
364static inline int b53_switch_get_reset_gpio(struct b53_device *dev)
365{
366 enum bcm47xx_board board = bcm47xx_board_get();
367
368 switch (board) {
369 case BCM47XX_BOARD_LINKSYS_WRT300NV11:
370 case BCM47XX_BOARD_LINKSYS_WRT310NV1:
371 return 8;
372 default:
373 return bcm47xx_nvram_gpio_pin("robo_reset");
374 }
375}
376#else
377static inline int b53_switch_get_reset_gpio(struct b53_device *dev)
378{
379 return -ENOENT;
380}
381#endif
382#endif