1 From 42cb399df978a33539b95d668b3f973d927cb902 Mon Sep 17 00:00:00 2001
2 From: Daniel Schwierzeck <daniel.schwierzeck@gmail.com>
3 Date: Mon, 17 Dec 2012 23:37:57 +0100
4 Subject: net: switchlib: add driver for REALTEK RTL8306
6 Signed-off-by: Oliver Muth <dr.o.muth@gmx.de>
7 Signed-off-by: Daniel Schwierzeck <daniel.schwierzeck@gmail.com>
9 diff --git a/drivers/net/switch/Makefile b/drivers/net/switch/Makefile
10 index 7400897..08c6972 100644
11 --- a/drivers/net/switch/Makefile
12 +++ b/drivers/net/switch/Makefile
13 @@ -13,6 +13,7 @@ COBJS-$(CONFIG_SWITCH_MULTI) += switch.o
14 COBJS-$(CONFIG_SWITCH_PSB697X) += psb697x.o
15 COBJS-$(CONFIG_SWITCH_ADM6996I) += adm6996i.o
16 COBJS-$(CONFIG_SWITCH_AR8216) += ar8216.o
17 +COBJS-$(CONFIG_SWITCH_RTL8306) += rtl8306.o
20 SRCS := $(COBJS:.o=.c)
21 diff --git a/drivers/net/switch/rtl8306.c b/drivers/net/switch/rtl8306.c
23 index 0000000..7a6a917
25 +++ b/drivers/net/switch/rtl8306.c
28 + * Based on OpenWRT linux driver
30 + * Copyright (C) 2011-2012 Daniel Schwierzeck, daniel.schwierzeck@gmail.com
31 + * Copyright (C) 2009 Felix Fietkau <nbd@openwrt.org>
33 + * SPDX-License-Identifier: GPL-2.0+
41 +#define RTL8306_REG_PAGE 16
42 +#define RTL8306_REG_PAGE_LO (1 << 15)
43 +#define RTL8306_REG_PAGE_HI (1 << 1) /* inverted */
44 +#define RTL8306_CHIPID 0x5988
46 +#define RTL8306_NUM_VLANS 16
47 +#define RTL8306_NUM_PORTS 6
48 +#define RTL8306_PORT_CPU 5
49 +#define RTL8306_NUM_PAGES 4
50 +#define RTL8306_NUM_REGS 32
78 + RTL_REG_TRUNK_PORTSEL,
84 + RTL_REG_VLAN_ENABLE,
85 + RTL_REG_VLAN_FILTER,
86 + RTL_REG_VLAN_TAG_ONLY,
87 + RTL_REG_VLAN_TAG_AWARE,
88 +#define RTL_VLAN_ENUM(id) \
89 + RTL_REG_VLAN##id##_VID, \
90 + RTL_REG_VLAN##id##_PORTMASK
107 +#define RTL_PORT_ENUM(id) \
108 + RTL_REG_PORT##id##_PVID, \
109 + RTL_REG_PORT##id##_NULL_VID_REPLACE, \
110 + RTL_REG_PORT##id##_NON_PVID_DISCARD, \
111 + RTL_REG_PORT##id##_VID_INSERT, \
112 + RTL_REG_PORT##id##_TAG_INSERT, \
113 + RTL_REG_PORT##id##_LINK, \
114 + RTL_REG_PORT##id##_SPEED, \
115 + RTL_REG_PORT##id##_NWAY, \
116 + RTL_REG_PORT##id##_NRESTART, \
117 + RTL_REG_PORT##id##_DUPLEX, \
118 + RTL_REG_PORT##id##_RXEN, \
119 + RTL_REG_PORT##id##_TXEN, \
120 + RTL_REG_PORT##id##_LRNEN
129 +static const struct rtl_reg rtl_regs[] = {
130 + [RTL_REG_CHIPID] = { 0, 4, 30, 16, 0, 0 },
131 + [RTL_REG_CHIPVER] = { 0, 4, 31, 8, 0, 0 },
132 + [RTL_REG_CHIPTYPE] = { 0, 4, 31, 2, 8, 0 },
134 + /* CPU port number */
135 + [RTL_REG_CPUPORT] = { 2, 4, 21, 3, 0, 0 },
136 + /* Enable CPU port function */
137 + [RTL_REG_EN_CPUPORT] = { 3, 2, 21, 1, 15, 1 },
138 + /* Enable CPU port tag insertion */
139 + [RTL_REG_EN_TAG_OUT] = { 3, 2, 21, 1, 12, 0 },
140 + /* Enable CPU port tag removal */
141 + [RTL_REG_EN_TAG_CLR] = { 3, 2, 21, 1, 11, 0 },
142 + /* Enable CPU port tag checking */
143 + [RTL_REG_EN_TAG_IN] = { 0, 4, 21, 1, 7, 0 },
144 + [RTL_REG_EN_TRUNK] = { 0, 0, 19, 1, 11, 1 },
145 + [RTL_REG_TRUNK_PORTSEL] = { 0, 0, 16, 1, 6, 1 },
146 + [RTL_REG_RESET] = { 0, 0, 16, 1, 12, 0 },
147 + [RTL_REG_PHY_RESET] = { 0, 0, 0, 1, 15, 0 },
148 + [RTL_REG_CPU_LINKUP] = { 0, 6, 22, 1, 15, 0 },
149 + [RTL_REG_TRAP_CPU] = { 3, 2, 22, 1, 6, 0 },
151 + [RTL_REG_VLAN_TAG_ONLY] = { 0, 0, 16, 1, 8, 1 },
152 + [RTL_REG_VLAN_FILTER] = { 0, 0, 16, 1, 9, 1 },
153 + [RTL_REG_VLAN_TAG_AWARE] = { 0, 0, 16, 1, 10, 1 },
154 + [RTL_REG_VLAN_ENABLE] = { 0, 0, 18, 1, 8, 1 },
156 +#define RTL_VLAN_REGS(id, phy, page, regofs) \
157 + [RTL_REG_VLAN##id##_VID] = { page, phy, 25 + regofs, 12, 0, 0 }, \
158 + [RTL_REG_VLAN##id##_PORTMASK] = { page, phy, 24 + regofs, 6, 0, 0 }
159 + RTL_VLAN_REGS( 0, 0, 0, 0),
160 + RTL_VLAN_REGS( 1, 1, 0, 0),
161 + RTL_VLAN_REGS( 2, 2, 0, 0),
162 + RTL_VLAN_REGS( 3, 3, 0, 0),
163 + RTL_VLAN_REGS( 4, 4, 0, 0),
164 + RTL_VLAN_REGS( 5, 0, 1, 2),
165 + RTL_VLAN_REGS( 6, 1, 1, 2),
166 + RTL_VLAN_REGS( 7, 2, 1, 2),
167 + RTL_VLAN_REGS( 8, 3, 1, 2),
168 + RTL_VLAN_REGS( 9, 4, 1, 2),
169 + RTL_VLAN_REGS(10, 0, 1, 4),
170 + RTL_VLAN_REGS(11, 1, 1, 4),
171 + RTL_VLAN_REGS(12, 2, 1, 4),
172 + RTL_VLAN_REGS(13, 3, 1, 4),
173 + RTL_VLAN_REGS(14, 4, 1, 4),
174 + RTL_VLAN_REGS(15, 0, 1, 6),
176 +#define REG_PORT_SETTING(port, phy) \
177 + [RTL_REG_PORT##port##_SPEED] = { 0, phy, 0, 1, 13, 0 }, \
178 + [RTL_REG_PORT##port##_NWAY] = { 0, phy, 0, 1, 12, 0 }, \
179 + [RTL_REG_PORT##port##_NRESTART] = { 0, phy, 0, 1, 9, 0 }, \
180 + [RTL_REG_PORT##port##_DUPLEX] = { 0, phy, 0, 1, 8, 0 }, \
181 + [RTL_REG_PORT##port##_TXEN] = { 0, phy, 24, 1, 11, 0 }, \
182 + [RTL_REG_PORT##port##_RXEN] = { 0, phy, 24, 1, 10, 0 }, \
183 + [RTL_REG_PORT##port##_LRNEN] = { 0, phy, 24, 1, 9, 0 }, \
184 + [RTL_REG_PORT##port##_LINK] = { 0, phy, 1, 1, 2, 0 }, \
185 + [RTL_REG_PORT##port##_NULL_VID_REPLACE] = { 0, phy, 22, 1, 12, 0 }, \
186 + [RTL_REG_PORT##port##_NON_PVID_DISCARD] = { 0, phy, 22, 1, 11, 0 }, \
187 + [RTL_REG_PORT##port##_VID_INSERT] = { 0, phy, 22, 2, 9, 0 }, \
188 + [RTL_REG_PORT##port##_TAG_INSERT] = { 0, phy, 22, 2, 0, 0 }
190 + REG_PORT_SETTING(0, 0),
191 + REG_PORT_SETTING(1, 1),
192 + REG_PORT_SETTING(2, 2),
193 + REG_PORT_SETTING(3, 3),
194 + REG_PORT_SETTING(4, 4),
195 + REG_PORT_SETTING(5, 6),
197 +#define REG_PORT_PVID(phy, page, regofs) \
198 + { page, phy, 24 + regofs, 4, 12, 0 }
199 + [RTL_REG_PORT0_PVID] = REG_PORT_PVID(0, 0, 0),
200 + [RTL_REG_PORT1_PVID] = REG_PORT_PVID(1, 0, 0),
201 + [RTL_REG_PORT2_PVID] = REG_PORT_PVID(2, 0, 0),
202 + [RTL_REG_PORT3_PVID] = REG_PORT_PVID(3, 0, 0),
203 + [RTL_REG_PORT4_PVID] = REG_PORT_PVID(4, 0, 0),
204 + [RTL_REG_PORT5_PVID] = REG_PORT_PVID(0, 1, 2),
207 +static void rtl_set_page(struct mii_dev *bus, unsigned int page)
211 + BUG_ON(page > RTL8306_NUM_PAGES);
213 + pgsel = bus->read(bus, 0, MDIO_DEVAD_NONE, RTL8306_REG_PAGE);
214 + pgsel &= ~(RTL8306_REG_PAGE_LO | RTL8306_REG_PAGE_HI);
216 + if (page & (1 << 0))
217 + pgsel |= RTL8306_REG_PAGE_LO;
219 + if (!(page & (1 << 1))) /* bit is inverted */
220 + pgsel |= RTL8306_REG_PAGE_HI;
222 + bus->write(bus, 0, MDIO_DEVAD_NONE, RTL8306_REG_PAGE, pgsel);
226 +static __maybe_unused int rtl_w16(struct mii_dev *bus, unsigned int page, unsigned int phy,
227 + unsigned int reg, u16 val)
229 + rtl_set_page(bus, page);
231 + bus->write(bus, phy, MDIO_DEVAD_NONE, reg, val);
232 + bus->read(bus, phy, MDIO_DEVAD_NONE, reg); /* flush */
237 +static int rtl_r16(struct mii_dev *bus, unsigned int page, unsigned int phy,
240 + rtl_set_page(bus, page);
242 + return bus->read(bus, phy, MDIO_DEVAD_NONE, reg);
245 +static u16 rtl_rmw(struct mii_dev *bus, unsigned int page, unsigned int phy,
246 + unsigned int reg, u16 mask, u16 val)
250 + rtl_set_page(bus, page);
252 + r = bus->read(bus, phy, MDIO_DEVAD_NONE, reg);
255 + bus->write(bus, phy, MDIO_DEVAD_NONE, reg, r);
257 + return bus->read(bus, phy, MDIO_DEVAD_NONE, reg); /* flush */
260 +static int rtl_get(struct mii_dev *bus, enum rtl_regidx s)
262 + const struct rtl_reg *r = &rtl_regs[s];
265 + BUG_ON(s >= ARRAY_SIZE(rtl_regs));
267 + if (r->bits == 0) /* unimplemented */
270 + val = rtl_r16(bus, r->page, r->phy, r->reg);
278 + val &= (1 << r->bits) - 1;
283 +static __maybe_unused int rtl_set(struct mii_dev *bus, enum rtl_regidx s, unsigned int val)
285 + const struct rtl_reg *r = &rtl_regs[s];
288 + BUG_ON(s >= ARRAY_SIZE(rtl_regs));
290 + if (r->bits == 0) /* unimplemented */
299 + if (r->bits != 16) {
300 + mask = (1 << r->bits) - 1;
306 + return rtl_rmw(bus, r->page, r->phy, r->reg, mask, val);
309 +static int rtl8306_probe(struct switch_device *dev)
311 + struct mii_dev *bus = dev->bus;
312 + unsigned int chipid, chipver, chiptype;
314 + chipid = rtl_get(bus, RTL_REG_CHIPID);
315 + chipver = rtl_get(bus, RTL_REG_CHIPVER);
316 + chiptype = rtl_get(bus, RTL_REG_CHIPTYPE);
318 + debug("%s: chipid %x, chipver %x, chiptype %x\n",
319 + __func__, chipid, chipver, chiptype);
321 + if (chipid == RTL8306_CHIPID)
327 +static void rtl8306_setup(struct switch_device *dev)
329 + struct mii_dev *bus = dev->bus;
331 + /* initialize cpu port settings */
332 + rtl_set(bus, RTL_REG_CPUPORT, dev->cpu_port);
333 + rtl_set(bus, RTL_REG_EN_CPUPORT, 1);
335 + /* enable phy 5 link status */
336 + rtl_set(bus, RTL_REG_CPU_LINKUP, 1);
337 +// rtl_set(bus, RTL_REG_PORT5_TXEN, 1);
338 +// rtl_set(bus, RTL_REG_PORT5_RXEN, 1);
339 +// rtl_set(bus, RTL_REG_PORT5_LRNEN, 1);
341 + debug("%s: CPU link up: %i\n",
342 + __func__, rtl_get(bus, RTL_REG_PORT5_LINK));
347 +static struct switch_driver rtl8306_drv = {
351 +void switch_rtl8306_init(void)
353 + /* For archs with manual relocation */
354 + rtl8306_drv.probe = rtl8306_probe;
355 + rtl8306_drv.setup = rtl8306_setup;
357 + switch_driver_register(&rtl8306_drv);
359 diff --git a/drivers/net/switch/switch.c b/drivers/net/switch/switch.c
360 index 3e34a7f..2e1c668 100644
361 --- a/drivers/net/switch/switch.c
362 +++ b/drivers/net/switch/switch.c
363 @@ -26,6 +26,9 @@ void switch_init(void)
364 #if defined(CONFIG_SWITCH_AR8216)
365 switch_ar8216_init();
367 +#if defined(CONFIG_SWITCH_RTL8306)
368 + switch_rtl8306_init();
373 diff --git a/include/switch.h b/include/switch.h
374 index ae7b123..927b1d2 100644
375 --- a/include/switch.h
376 +++ b/include/switch.h
377 @@ -100,6 +100,7 @@ static inline void switch_setup(struct switch_device *dev)
378 extern void switch_psb697x_init(void);
379 extern void switch_adm6996i_init(void);
380 extern void switch_ar8216_init(void);
381 +extern void switch_rtl8306_init(void);
383 #endif /* __SWITCH_H */