rtl8306.c 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060
  1. /*
  2. * rtl8306.c: RTL8306S switch driver
  3. *
  4. * Copyright (C) 2009 Felix Fietkau <nbd@openwrt.org>
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * version 2 as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. */
  15. #include <linux/if.h>
  16. #include <linux/module.h>
  17. #include <linux/init.h>
  18. #include <linux/list.h>
  19. #include <linux/if_ether.h>
  20. #include <linux/skbuff.h>
  21. #include <linux/netdevice.h>
  22. #include <linux/netlink.h>
  23. #include <net/genetlink.h>
  24. #include <linux/switch.h>
  25. #include <linux/delay.h>
  26. #include <linux/phy.h>
  27. //#define DEBUG 1
  28. /* Global (PHY0) */
  29. #define RTL8306_REG_PAGE 16
  30. #define RTL8306_REG_PAGE_LO (1 << 15)
  31. #define RTL8306_REG_PAGE_HI (1 << 1) /* inverted */
  32. #define RTL8306_NUM_VLANS 16
  33. #define RTL8306_NUM_PORTS 6
  34. #define RTL8306_PORT_CPU 5
  35. #define RTL8306_NUM_PAGES 4
  36. #define RTL8306_NUM_REGS 32
  37. #define RTL_NAME_S "RTL8306S"
  38. #define RTL_NAME_SD "RTL8306SD"
  39. #define RTL_NAME_SDM "RTL8306SDM"
  40. #define RTL_NAME_UNKNOWN "RTL8306(unknown)"
  41. #define RTL8306_MAGIC 0x8306
  42. static LIST_HEAD(phydevs);
  43. struct rtl_priv {
  44. struct list_head list;
  45. struct switch_dev dev;
  46. int page;
  47. int type;
  48. int do_cpu;
  49. struct mii_bus *bus;
  50. char hwname[sizeof(RTL_NAME_UNKNOWN)];
  51. bool fixup;
  52. };
  53. struct rtl_phyregs {
  54. int nway;
  55. int speed;
  56. int duplex;
  57. };
  58. #define to_rtl(_dev) container_of(_dev, struct rtl_priv, dev)
  59. enum {
  60. RTL_TYPE_S,
  61. RTL_TYPE_SD,
  62. RTL_TYPE_SDM,
  63. };
  64. struct rtl_reg {
  65. int page;
  66. int phy;
  67. int reg;
  68. int bits;
  69. int shift;
  70. int inverted;
  71. };
  72. #define RTL_VLAN_REGOFS(name) \
  73. (RTL_REG_VLAN1_##name - RTL_REG_VLAN0_##name)
  74. #define RTL_PORT_REGOFS(name) \
  75. (RTL_REG_PORT1_##name - RTL_REG_PORT0_##name)
  76. #define RTL_PORT_REG(id, reg) \
  77. (RTL_REG_PORT0_##reg + (id * RTL_PORT_REGOFS(reg)))
  78. #define RTL_VLAN_REG(id, reg) \
  79. (RTL_REG_VLAN0_##reg + (id * RTL_VLAN_REGOFS(reg)))
  80. #define RTL_GLOBAL_REGATTR(reg) \
  81. .id = RTL_REG_##reg, \
  82. .type = SWITCH_TYPE_INT, \
  83. .ofs = 0, \
  84. .set = rtl_attr_set_int, \
  85. .get = rtl_attr_get_int
  86. #define RTL_PORT_REGATTR(reg) \
  87. .id = RTL_REG_PORT0_##reg, \
  88. .type = SWITCH_TYPE_INT, \
  89. .ofs = RTL_PORT_REGOFS(reg), \
  90. .set = rtl_attr_set_port_int, \
  91. .get = rtl_attr_get_port_int
  92. #define RTL_VLAN_REGATTR(reg) \
  93. .id = RTL_REG_VLAN0_##reg, \
  94. .type = SWITCH_TYPE_INT, \
  95. .ofs = RTL_VLAN_REGOFS(reg), \
  96. .set = rtl_attr_set_vlan_int, \
  97. .get = rtl_attr_get_vlan_int
  98. enum rtl_regidx {
  99. RTL_REG_CHIPID,
  100. RTL_REG_CHIPVER,
  101. RTL_REG_CHIPTYPE,
  102. RTL_REG_CPUPORT,
  103. RTL_REG_EN_CPUPORT,
  104. RTL_REG_EN_TAG_OUT,
  105. RTL_REG_EN_TAG_CLR,
  106. RTL_REG_EN_TAG_IN,
  107. RTL_REG_TRAP_CPU,
  108. RTL_REG_CPU_LINKUP,
  109. RTL_REG_TRUNK_PORTSEL,
  110. RTL_REG_EN_TRUNK,
  111. RTL_REG_RESET,
  112. RTL_REG_VLAN_ENABLE,
  113. RTL_REG_VLAN_FILTER,
  114. RTL_REG_VLAN_TAG_ONLY,
  115. RTL_REG_VLAN_TAG_AWARE,
  116. #define RTL_VLAN_ENUM(id) \
  117. RTL_REG_VLAN##id##_VID, \
  118. RTL_REG_VLAN##id##_PORTMASK
  119. RTL_VLAN_ENUM(0),
  120. RTL_VLAN_ENUM(1),
  121. RTL_VLAN_ENUM(2),
  122. RTL_VLAN_ENUM(3),
  123. RTL_VLAN_ENUM(4),
  124. RTL_VLAN_ENUM(5),
  125. RTL_VLAN_ENUM(6),
  126. RTL_VLAN_ENUM(7),
  127. RTL_VLAN_ENUM(8),
  128. RTL_VLAN_ENUM(9),
  129. RTL_VLAN_ENUM(10),
  130. RTL_VLAN_ENUM(11),
  131. RTL_VLAN_ENUM(12),
  132. RTL_VLAN_ENUM(13),
  133. RTL_VLAN_ENUM(14),
  134. RTL_VLAN_ENUM(15),
  135. #define RTL_PORT_ENUM(id) \
  136. RTL_REG_PORT##id##_PVID, \
  137. RTL_REG_PORT##id##_NULL_VID_REPLACE, \
  138. RTL_REG_PORT##id##_NON_PVID_DISCARD, \
  139. RTL_REG_PORT##id##_VID_INSERT, \
  140. RTL_REG_PORT##id##_TAG_INSERT, \
  141. RTL_REG_PORT##id##_LINK, \
  142. RTL_REG_PORT##id##_SPEED, \
  143. RTL_REG_PORT##id##_NWAY, \
  144. RTL_REG_PORT##id##_NRESTART, \
  145. RTL_REG_PORT##id##_DUPLEX, \
  146. RTL_REG_PORT##id##_RXEN, \
  147. RTL_REG_PORT##id##_TXEN
  148. RTL_PORT_ENUM(0),
  149. RTL_PORT_ENUM(1),
  150. RTL_PORT_ENUM(2),
  151. RTL_PORT_ENUM(3),
  152. RTL_PORT_ENUM(4),
  153. RTL_PORT_ENUM(5),
  154. };
  155. static const struct rtl_reg rtl_regs[] = {
  156. [RTL_REG_CHIPID] = { 0, 4, 30, 16, 0, 0 },
  157. [RTL_REG_CHIPVER] = { 0, 4, 31, 8, 0, 0 },
  158. [RTL_REG_CHIPTYPE] = { 0, 4, 31, 2, 8, 0 },
  159. /* CPU port number */
  160. [RTL_REG_CPUPORT] = { 2, 4, 21, 3, 0, 0 },
  161. /* Enable CPU port function */
  162. [RTL_REG_EN_CPUPORT] = { 3, 2, 21, 1, 15, 1 },
  163. /* Enable CPU port tag insertion */
  164. [RTL_REG_EN_TAG_OUT] = { 3, 2, 21, 1, 12, 0 },
  165. /* Enable CPU port tag removal */
  166. [RTL_REG_EN_TAG_CLR] = { 3, 2, 21, 1, 11, 0 },
  167. /* Enable CPU port tag checking */
  168. [RTL_REG_EN_TAG_IN] = { 0, 4, 21, 1, 7, 0 },
  169. [RTL_REG_EN_TRUNK] = { 0, 0, 19, 1, 11, 1 },
  170. [RTL_REG_TRUNK_PORTSEL] = { 0, 0, 16, 1, 6, 1 },
  171. [RTL_REG_RESET] = { 0, 0, 16, 1, 12, 0 },
  172. [RTL_REG_TRAP_CPU] = { 3, 2, 22, 1, 6, 0 },
  173. [RTL_REG_CPU_LINKUP] = { 0, 6, 22, 1, 15, 0 },
  174. [RTL_REG_VLAN_TAG_ONLY] = { 0, 0, 16, 1, 8, 1 },
  175. [RTL_REG_VLAN_FILTER] = { 0, 0, 16, 1, 9, 1 },
  176. [RTL_REG_VLAN_TAG_AWARE] = { 0, 0, 16, 1, 10, 1 },
  177. [RTL_REG_VLAN_ENABLE] = { 0, 0, 18, 1, 8, 1 },
  178. #define RTL_VLAN_REGS(id, phy, page, regofs) \
  179. [RTL_REG_VLAN##id##_VID] = { page, phy, 25 + regofs, 12, 0, 0 }, \
  180. [RTL_REG_VLAN##id##_PORTMASK] = { page, phy, 24 + regofs, 6, 0, 0 }
  181. RTL_VLAN_REGS( 0, 0, 0, 0),
  182. RTL_VLAN_REGS( 1, 1, 0, 0),
  183. RTL_VLAN_REGS( 2, 2, 0, 0),
  184. RTL_VLAN_REGS( 3, 3, 0, 0),
  185. RTL_VLAN_REGS( 4, 4, 0, 0),
  186. RTL_VLAN_REGS( 5, 0, 1, 2),
  187. RTL_VLAN_REGS( 6, 1, 1, 2),
  188. RTL_VLAN_REGS( 7, 2, 1, 2),
  189. RTL_VLAN_REGS( 8, 3, 1, 2),
  190. RTL_VLAN_REGS( 9, 4, 1, 2),
  191. RTL_VLAN_REGS(10, 0, 1, 4),
  192. RTL_VLAN_REGS(11, 1, 1, 4),
  193. RTL_VLAN_REGS(12, 2, 1, 4),
  194. RTL_VLAN_REGS(13, 3, 1, 4),
  195. RTL_VLAN_REGS(14, 4, 1, 4),
  196. RTL_VLAN_REGS(15, 0, 1, 6),
  197. #define REG_PORT_SETTING(port, phy) \
  198. [RTL_REG_PORT##port##_SPEED] = { 0, phy, 0, 1, 13, 0 }, \
  199. [RTL_REG_PORT##port##_NWAY] = { 0, phy, 0, 1, 12, 0 }, \
  200. [RTL_REG_PORT##port##_NRESTART] = { 0, phy, 0, 1, 9, 0 }, \
  201. [RTL_REG_PORT##port##_DUPLEX] = { 0, phy, 0, 1, 8, 0 }, \
  202. [RTL_REG_PORT##port##_TXEN] = { 0, phy, 24, 1, 11, 0 }, \
  203. [RTL_REG_PORT##port##_RXEN] = { 0, phy, 24, 1, 10, 0 }, \
  204. [RTL_REG_PORT##port##_LINK] = { 0, phy, 1, 1, 2, 0 }, \
  205. [RTL_REG_PORT##port##_NULL_VID_REPLACE] = { 0, phy, 22, 1, 12, 0 }, \
  206. [RTL_REG_PORT##port##_NON_PVID_DISCARD] = { 0, phy, 22, 1, 11, 0 }, \
  207. [RTL_REG_PORT##port##_VID_INSERT] = { 0, phy, 22, 2, 9, 0 }, \
  208. [RTL_REG_PORT##port##_TAG_INSERT] = { 0, phy, 22, 2, 0, 0 }
  209. REG_PORT_SETTING(0, 0),
  210. REG_PORT_SETTING(1, 1),
  211. REG_PORT_SETTING(2, 2),
  212. REG_PORT_SETTING(3, 3),
  213. REG_PORT_SETTING(4, 4),
  214. REG_PORT_SETTING(5, 6),
  215. #define REG_PORT_PVID(phy, page, regofs) \
  216. { page, phy, 24 + regofs, 4, 12, 0 }
  217. [RTL_REG_PORT0_PVID] = REG_PORT_PVID(0, 0, 0),
  218. [RTL_REG_PORT1_PVID] = REG_PORT_PVID(1, 0, 0),
  219. [RTL_REG_PORT2_PVID] = REG_PORT_PVID(2, 0, 0),
  220. [RTL_REG_PORT3_PVID] = REG_PORT_PVID(3, 0, 0),
  221. [RTL_REG_PORT4_PVID] = REG_PORT_PVID(4, 0, 0),
  222. [RTL_REG_PORT5_PVID] = REG_PORT_PVID(0, 1, 2),
  223. };
  224. static inline void
  225. rtl_set_page(struct rtl_priv *priv, unsigned int page)
  226. {
  227. struct mii_bus *bus = priv->bus;
  228. u16 pgsel;
  229. if (priv->fixup)
  230. return;
  231. if (priv->page == page)
  232. return;
  233. BUG_ON(page > RTL8306_NUM_PAGES);
  234. pgsel = bus->read(bus, 0, RTL8306_REG_PAGE);
  235. pgsel &= ~(RTL8306_REG_PAGE_LO | RTL8306_REG_PAGE_HI);
  236. if (page & (1 << 0))
  237. pgsel |= RTL8306_REG_PAGE_LO;
  238. if (!(page & (1 << 1))) /* bit is inverted */
  239. pgsel |= RTL8306_REG_PAGE_HI;
  240. bus->write(bus, 0, RTL8306_REG_PAGE, pgsel);
  241. }
  242. static inline int
  243. rtl_w16(struct switch_dev *dev, unsigned int page, unsigned int phy, unsigned int reg, u16 val)
  244. {
  245. struct rtl_priv *priv = to_rtl(dev);
  246. struct mii_bus *bus = priv->bus;
  247. rtl_set_page(priv, page);
  248. bus->write(bus, phy, reg, val);
  249. bus->read(bus, phy, reg); /* flush */
  250. return 0;
  251. }
  252. static inline int
  253. rtl_r16(struct switch_dev *dev, unsigned int page, unsigned int phy, unsigned int reg)
  254. {
  255. struct rtl_priv *priv = to_rtl(dev);
  256. struct mii_bus *bus = priv->bus;
  257. rtl_set_page(priv, page);
  258. return bus->read(bus, phy, reg);
  259. }
  260. static inline u16
  261. rtl_rmw(struct switch_dev *dev, unsigned int page, unsigned int phy, unsigned int reg, u16 mask, u16 val)
  262. {
  263. struct rtl_priv *priv = to_rtl(dev);
  264. struct mii_bus *bus = priv->bus;
  265. u16 r;
  266. rtl_set_page(priv, page);
  267. r = bus->read(bus, phy, reg);
  268. r &= ~mask;
  269. r |= val;
  270. bus->write(bus, phy, reg, r);
  271. return bus->read(bus, phy, reg); /* flush */
  272. }
  273. static inline int
  274. rtl_get(struct switch_dev *dev, enum rtl_regidx s)
  275. {
  276. const struct rtl_reg *r = &rtl_regs[s];
  277. u16 val;
  278. BUG_ON(s >= ARRAY_SIZE(rtl_regs));
  279. if (r->bits == 0) /* unimplemented */
  280. return 0;
  281. val = rtl_r16(dev, r->page, r->phy, r->reg);
  282. if (r->shift > 0)
  283. val >>= r->shift;
  284. if (r->inverted)
  285. val = ~val;
  286. val &= (1 << r->bits) - 1;
  287. return val;
  288. }
  289. static int
  290. rtl_set(struct switch_dev *dev, enum rtl_regidx s, unsigned int val)
  291. {
  292. const struct rtl_reg *r = &rtl_regs[s];
  293. u16 mask = 0xffff;
  294. BUG_ON(s >= ARRAY_SIZE(rtl_regs));
  295. if (r->bits == 0) /* unimplemented */
  296. return 0;
  297. if (r->shift > 0)
  298. val <<= r->shift;
  299. if (r->inverted)
  300. val = ~val;
  301. if (r->bits != 16) {
  302. mask = (1 << r->bits) - 1;
  303. mask <<= r->shift;
  304. }
  305. val &= mask;
  306. return rtl_rmw(dev, r->page, r->phy, r->reg, mask, val);
  307. }
  308. static void
  309. rtl_phy_save(struct switch_dev *dev, int port, struct rtl_phyregs *regs)
  310. {
  311. regs->nway = rtl_get(dev, RTL_PORT_REG(port, NWAY));
  312. regs->speed = rtl_get(dev, RTL_PORT_REG(port, SPEED));
  313. regs->duplex = rtl_get(dev, RTL_PORT_REG(port, DUPLEX));
  314. }
  315. static void
  316. rtl_phy_restore(struct switch_dev *dev, int port, struct rtl_phyregs *regs)
  317. {
  318. rtl_set(dev, RTL_PORT_REG(port, NWAY), regs->nway);
  319. rtl_set(dev, RTL_PORT_REG(port, SPEED), regs->speed);
  320. rtl_set(dev, RTL_PORT_REG(port, DUPLEX), regs->duplex);
  321. }
  322. static void
  323. rtl_port_set_enable(struct switch_dev *dev, int port, int enabled)
  324. {
  325. rtl_set(dev, RTL_PORT_REG(port, RXEN), enabled);
  326. rtl_set(dev, RTL_PORT_REG(port, TXEN), enabled);
  327. if ((port >= 5) || !enabled)
  328. return;
  329. /* restart autonegotiation if enabled */
  330. rtl_set(dev, RTL_PORT_REG(port, NRESTART), 1);
  331. }
  332. static int
  333. rtl_hw_apply(struct switch_dev *dev)
  334. {
  335. int i;
  336. int trunk_en, trunk_psel;
  337. struct rtl_phyregs port5;
  338. rtl_phy_save(dev, 5, &port5);
  339. /* disable rx/tx from PHYs */
  340. for (i = 0; i < RTL8306_NUM_PORTS - 1; i++) {
  341. rtl_port_set_enable(dev, i, 0);
  342. }
  343. /* save trunking status */
  344. trunk_en = rtl_get(dev, RTL_REG_EN_TRUNK);
  345. trunk_psel = rtl_get(dev, RTL_REG_TRUNK_PORTSEL);
  346. /* trunk port 3 and 4
  347. * XXX: Big WTF, but RealTek seems to do it */
  348. rtl_set(dev, RTL_REG_EN_TRUNK, 1);
  349. rtl_set(dev, RTL_REG_TRUNK_PORTSEL, 1);
  350. /* execute the software reset */
  351. rtl_set(dev, RTL_REG_RESET, 1);
  352. /* wait for the reset to complete,
  353. * but don't wait for too long */
  354. for (i = 0; i < 10; i++) {
  355. if (rtl_get(dev, RTL_REG_RESET) == 0)
  356. break;
  357. msleep(1);
  358. }
  359. /* enable rx/tx from PHYs */
  360. for (i = 0; i < RTL8306_NUM_PORTS - 1; i++) {
  361. rtl_port_set_enable(dev, i, 1);
  362. }
  363. /* restore trunking settings */
  364. rtl_set(dev, RTL_REG_EN_TRUNK, trunk_en);
  365. rtl_set(dev, RTL_REG_TRUNK_PORTSEL, trunk_psel);
  366. rtl_phy_restore(dev, 5, &port5);
  367. rtl_set(dev, RTL_REG_CPU_LINKUP, 1);
  368. return 0;
  369. }
  370. static void
  371. rtl_hw_init(struct switch_dev *dev)
  372. {
  373. struct rtl_priv *priv = to_rtl(dev);
  374. int cpu_mask = 1 << dev->cpu_port;
  375. int i;
  376. rtl_set(dev, RTL_REG_VLAN_ENABLE, 0);
  377. rtl_set(dev, RTL_REG_VLAN_FILTER, 0);
  378. rtl_set(dev, RTL_REG_EN_TRUNK, 0);
  379. rtl_set(dev, RTL_REG_TRUNK_PORTSEL, 0);
  380. /* initialize cpu port settings */
  381. if (priv->do_cpu) {
  382. rtl_set(dev, RTL_REG_CPUPORT, dev->cpu_port);
  383. rtl_set(dev, RTL_REG_EN_CPUPORT, 1);
  384. } else {
  385. rtl_set(dev, RTL_REG_CPUPORT, 7);
  386. rtl_set(dev, RTL_REG_EN_CPUPORT, 0);
  387. }
  388. rtl_set(dev, RTL_REG_EN_TAG_OUT, 0);
  389. rtl_set(dev, RTL_REG_EN_TAG_IN, 0);
  390. rtl_set(dev, RTL_REG_EN_TAG_CLR, 0);
  391. /* reset all vlans */
  392. for (i = 0; i < RTL8306_NUM_VLANS; i++) {
  393. rtl_set(dev, RTL_VLAN_REG(i, VID), i);
  394. rtl_set(dev, RTL_VLAN_REG(i, PORTMASK), 0);
  395. }
  396. /* default to port isolation */
  397. for (i = 0; i < RTL8306_NUM_PORTS; i++) {
  398. unsigned long mask;
  399. if ((1 << i) == cpu_mask)
  400. mask = ((1 << RTL8306_NUM_PORTS) - 1) & ~cpu_mask; /* all bits set */
  401. else
  402. mask = cpu_mask | (1 << i);
  403. rtl_set(dev, RTL_VLAN_REG(i, PORTMASK), mask);
  404. rtl_set(dev, RTL_PORT_REG(i, PVID), i);
  405. rtl_set(dev, RTL_PORT_REG(i, NULL_VID_REPLACE), 1);
  406. rtl_set(dev, RTL_PORT_REG(i, VID_INSERT), 1);
  407. rtl_set(dev, RTL_PORT_REG(i, TAG_INSERT), 3);
  408. }
  409. rtl_hw_apply(dev);
  410. }
  411. #ifdef DEBUG
  412. static int
  413. rtl_set_use_cpuport(struct switch_dev *dev, const struct switch_attr *attr, struct switch_val *val)
  414. {
  415. struct rtl_priv *priv = to_rtl(dev);
  416. priv->do_cpu = val->value.i;
  417. rtl_hw_init(dev);
  418. return 0;
  419. }
  420. static int
  421. rtl_get_use_cpuport(struct switch_dev *dev, const struct switch_attr *attr, struct switch_val *val)
  422. {
  423. struct rtl_priv *priv = to_rtl(dev);
  424. val->value.i = priv->do_cpu;
  425. return 0;
  426. }
  427. static int
  428. rtl_set_cpuport(struct switch_dev *dev, const struct switch_attr *attr, struct switch_val *val)
  429. {
  430. dev->cpu_port = val->value.i;
  431. rtl_hw_init(dev);
  432. return 0;
  433. }
  434. static int
  435. rtl_get_cpuport(struct switch_dev *dev, const struct switch_attr *attr, struct switch_val *val)
  436. {
  437. val->value.i = dev->cpu_port;
  438. return 0;
  439. }
  440. #endif
  441. static int
  442. rtl_reset(struct switch_dev *dev)
  443. {
  444. rtl_hw_init(dev);
  445. return 0;
  446. }
  447. static int
  448. rtl_attr_set_int(struct switch_dev *dev, const struct switch_attr *attr, struct switch_val *val)
  449. {
  450. int idx = attr->id + (val->port_vlan * attr->ofs);
  451. struct rtl_phyregs port;
  452. if (attr->id >= ARRAY_SIZE(rtl_regs))
  453. return -EINVAL;
  454. if ((attr->max > 0) && (val->value.i > attr->max))
  455. return -EINVAL;
  456. /* access to phy register 22 on port 4/5
  457. * needs phy status save/restore */
  458. if ((val->port_vlan > 3) &&
  459. (rtl_regs[idx].reg == 22) &&
  460. (rtl_regs[idx].page == 0)) {
  461. rtl_phy_save(dev, val->port_vlan, &port);
  462. rtl_set(dev, idx, val->value.i);
  463. rtl_phy_restore(dev, val->port_vlan, &port);
  464. } else {
  465. rtl_set(dev, idx, val->value.i);
  466. }
  467. return 0;
  468. }
  469. static int
  470. rtl_attr_get_int(struct switch_dev *dev, const struct switch_attr *attr, struct switch_val *val)
  471. {
  472. int idx = attr->id + (val->port_vlan * attr->ofs);
  473. if (idx >= ARRAY_SIZE(rtl_regs))
  474. return -EINVAL;
  475. val->value.i = rtl_get(dev, idx);
  476. return 0;
  477. }
  478. static int
  479. rtl_attr_set_port_int(struct switch_dev *dev, const struct switch_attr *attr, struct switch_val *val)
  480. {
  481. if (val->port_vlan >= RTL8306_NUM_PORTS)
  482. return -EINVAL;
  483. return rtl_attr_set_int(dev, attr, val);
  484. }
  485. static int
  486. rtl_attr_get_port_int(struct switch_dev *dev, const struct switch_attr *attr, struct switch_val *val)
  487. {
  488. if (val->port_vlan >= RTL8306_NUM_PORTS)
  489. return -EINVAL;
  490. return rtl_attr_get_int(dev, attr, val);
  491. }
  492. static int
  493. rtl_get_port_link(struct switch_dev *dev, int port, struct switch_port_link *link)
  494. {
  495. if (port >= RTL8306_NUM_PORTS)
  496. return -EINVAL;
  497. link->link = rtl_get(dev, RTL_PORT_REG(port, LINK));
  498. if (!link->link)
  499. return 0;
  500. link->duplex = rtl_get(dev, RTL_PORT_REG(port, DUPLEX));
  501. link->aneg = rtl_get(dev, RTL_PORT_REG(port, NWAY));
  502. if (rtl_get(dev, RTL_PORT_REG(port, SPEED)))
  503. link->speed = SWITCH_PORT_SPEED_100;
  504. else
  505. link->speed = SWITCH_PORT_SPEED_10;
  506. return 0;
  507. }
  508. static int
  509. rtl_attr_set_vlan_int(struct switch_dev *dev, const struct switch_attr *attr, struct switch_val *val)
  510. {
  511. if (val->port_vlan >= dev->vlans)
  512. return -EINVAL;
  513. return rtl_attr_set_int(dev, attr, val);
  514. }
  515. static int
  516. rtl_attr_get_vlan_int(struct switch_dev *dev, const struct switch_attr *attr, struct switch_val *val)
  517. {
  518. if (val->port_vlan >= dev->vlans)
  519. return -EINVAL;
  520. return rtl_attr_get_int(dev, attr, val);
  521. }
  522. static int
  523. rtl_get_ports(struct switch_dev *dev, struct switch_val *val)
  524. {
  525. unsigned int i, mask;
  526. mask = rtl_get(dev, RTL_VLAN_REG(val->port_vlan, PORTMASK));
  527. for (i = 0; i < RTL8306_NUM_PORTS; i++) {
  528. struct switch_port *port;
  529. if (!(mask & (1 << i)))
  530. continue;
  531. port = &val->value.ports[val->len];
  532. port->id = i;
  533. if (rtl_get(dev, RTL_PORT_REG(i, TAG_INSERT)) == 2 || i == dev->cpu_port)
  534. port->flags = (1 << SWITCH_PORT_FLAG_TAGGED);
  535. val->len++;
  536. }
  537. return 0;
  538. }
  539. static int
  540. rtl_set_vlan(struct switch_dev *dev, const struct switch_attr *attr, struct switch_val *val)
  541. {
  542. struct rtl_priv *priv = to_rtl(dev);
  543. struct rtl_phyregs port;
  544. int en = val->value.i;
  545. int i;
  546. rtl_set(dev, RTL_REG_EN_TAG_OUT, en && priv->do_cpu);
  547. rtl_set(dev, RTL_REG_EN_TAG_IN, en && priv->do_cpu);
  548. rtl_set(dev, RTL_REG_EN_TAG_CLR, en && priv->do_cpu);
  549. rtl_set(dev, RTL_REG_VLAN_TAG_AWARE, en);
  550. if (en)
  551. rtl_set(dev, RTL_REG_VLAN_FILTER, en);
  552. for (i = 0; i < RTL8306_NUM_PORTS; i++) {
  553. if (i > 3)
  554. rtl_phy_save(dev, val->port_vlan, &port);
  555. rtl_set(dev, RTL_PORT_REG(i, NULL_VID_REPLACE), 1);
  556. rtl_set(dev, RTL_PORT_REG(i, VID_INSERT), (en ? (i == dev->cpu_port ? 0 : 1) : 1));
  557. rtl_set(dev, RTL_PORT_REG(i, TAG_INSERT), (en ? (i == dev->cpu_port ? 2 : 1) : 3));
  558. if (i > 3)
  559. rtl_phy_restore(dev, val->port_vlan, &port);
  560. }
  561. rtl_set(dev, RTL_REG_VLAN_ENABLE, en);
  562. return 0;
  563. }
  564. static int
  565. rtl_get_vlan(struct switch_dev *dev, const struct switch_attr *attr, struct switch_val *val)
  566. {
  567. val->value.i = rtl_get(dev, RTL_REG_VLAN_ENABLE);
  568. return 0;
  569. }
  570. static int
  571. rtl_set_ports(struct switch_dev *dev, struct switch_val *val)
  572. {
  573. unsigned int mask = 0;
  574. unsigned int oldmask;
  575. int i;
  576. for(i = 0; i < val->len; i++)
  577. {
  578. struct switch_port *port = &val->value.ports[i];
  579. bool tagged = false;
  580. mask |= (1 << port->id);
  581. if (port->id == dev->cpu_port)
  582. continue;
  583. if ((i == dev->cpu_port) ||
  584. (port->flags & (1 << SWITCH_PORT_FLAG_TAGGED)))
  585. tagged = true;
  586. /* fix up PVIDs for added ports */
  587. if (!tagged)
  588. rtl_set(dev, RTL_PORT_REG(port->id, PVID), val->port_vlan);
  589. rtl_set(dev, RTL_PORT_REG(port->id, NON_PVID_DISCARD), (tagged ? 0 : 1));
  590. rtl_set(dev, RTL_PORT_REG(port->id, VID_INSERT), (tagged ? 0 : 1));
  591. rtl_set(dev, RTL_PORT_REG(port->id, TAG_INSERT), (tagged ? 2 : 1));
  592. }
  593. oldmask = rtl_get(dev, RTL_VLAN_REG(val->port_vlan, PORTMASK));
  594. rtl_set(dev, RTL_VLAN_REG(val->port_vlan, PORTMASK), mask);
  595. /* fix up PVIDs for removed ports, default to last vlan */
  596. oldmask &= ~mask;
  597. for (i = 0; i < RTL8306_NUM_PORTS; i++) {
  598. if (!(oldmask & (1 << i)))
  599. continue;
  600. if (i == dev->cpu_port)
  601. continue;
  602. if (rtl_get(dev, RTL_PORT_REG(i, PVID)) == val->port_vlan)
  603. rtl_set(dev, RTL_PORT_REG(i, PVID), dev->vlans - 1);
  604. }
  605. return 0;
  606. }
  607. static struct switch_attr rtl_globals[] = {
  608. {
  609. .type = SWITCH_TYPE_INT,
  610. .name = "enable_vlan",
  611. .description = "Enable VLAN mode",
  612. .max = 1,
  613. .set = rtl_set_vlan,
  614. .get = rtl_get_vlan,
  615. },
  616. {
  617. RTL_GLOBAL_REGATTR(EN_TRUNK),
  618. .name = "trunk",
  619. .description = "Enable port trunking",
  620. .max = 1,
  621. },
  622. {
  623. RTL_GLOBAL_REGATTR(TRUNK_PORTSEL),
  624. .name = "trunk_sel",
  625. .description = "Select ports for trunking (0: 0,1 - 1: 3,4)",
  626. .max = 1,
  627. },
  628. #ifdef DEBUG
  629. {
  630. RTL_GLOBAL_REGATTR(VLAN_FILTER),
  631. .name = "vlan_filter",
  632. .description = "Filter incoming packets for allowed VLANS",
  633. .max = 1,
  634. },
  635. {
  636. .type = SWITCH_TYPE_INT,
  637. .name = "cpuport",
  638. .description = "CPU Port",
  639. .set = rtl_set_cpuport,
  640. .get = rtl_get_cpuport,
  641. .max = RTL8306_NUM_PORTS,
  642. },
  643. {
  644. .type = SWITCH_TYPE_INT,
  645. .name = "use_cpuport",
  646. .description = "CPU Port handling flag",
  647. .set = rtl_set_use_cpuport,
  648. .get = rtl_get_use_cpuport,
  649. .max = RTL8306_NUM_PORTS,
  650. },
  651. {
  652. RTL_GLOBAL_REGATTR(TRAP_CPU),
  653. .name = "trap_cpu",
  654. .description = "VLAN trap to CPU",
  655. .max = 1,
  656. },
  657. {
  658. RTL_GLOBAL_REGATTR(VLAN_TAG_AWARE),
  659. .name = "vlan_tag_aware",
  660. .description = "Enable VLAN tag awareness",
  661. .max = 1,
  662. },
  663. {
  664. RTL_GLOBAL_REGATTR(VLAN_TAG_ONLY),
  665. .name = "tag_only",
  666. .description = "Only accept tagged packets",
  667. .max = 1,
  668. },
  669. #endif
  670. };
  671. static struct switch_attr rtl_port[] = {
  672. {
  673. RTL_PORT_REGATTR(PVID),
  674. .name = "pvid",
  675. .description = "Port VLAN ID",
  676. .max = RTL8306_NUM_VLANS - 1,
  677. },
  678. #ifdef DEBUG
  679. {
  680. RTL_PORT_REGATTR(NULL_VID_REPLACE),
  681. .name = "null_vid",
  682. .description = "NULL VID gets replaced by port default vid",
  683. .max = 1,
  684. },
  685. {
  686. RTL_PORT_REGATTR(NON_PVID_DISCARD),
  687. .name = "non_pvid_discard",
  688. .description = "discard packets with VID != PVID",
  689. .max = 1,
  690. },
  691. {
  692. RTL_PORT_REGATTR(VID_INSERT),
  693. .name = "vid_insert_remove",
  694. .description = "how should the switch insert and remove vids ?",
  695. .max = 3,
  696. },
  697. {
  698. RTL_PORT_REGATTR(TAG_INSERT),
  699. .name = "tag_insert",
  700. .description = "tag insertion handling",
  701. .max = 3,
  702. },
  703. #endif
  704. };
  705. static struct switch_attr rtl_vlan[] = {
  706. {
  707. RTL_VLAN_REGATTR(VID),
  708. .name = "vid",
  709. .description = "VLAN ID (1-4095)",
  710. .max = 4095,
  711. },
  712. };
  713. static const struct switch_dev_ops rtl8306_ops = {
  714. .attr_global = {
  715. .attr = rtl_globals,
  716. .n_attr = ARRAY_SIZE(rtl_globals),
  717. },
  718. .attr_port = {
  719. .attr = rtl_port,
  720. .n_attr = ARRAY_SIZE(rtl_port),
  721. },
  722. .attr_vlan = {
  723. .attr = rtl_vlan,
  724. .n_attr = ARRAY_SIZE(rtl_vlan),
  725. },
  726. .get_vlan_ports = rtl_get_ports,
  727. .set_vlan_ports = rtl_set_ports,
  728. .apply_config = rtl_hw_apply,
  729. .reset_switch = rtl_reset,
  730. .get_port_link = rtl_get_port_link,
  731. };
  732. static int
  733. rtl8306_config_init(struct phy_device *pdev)
  734. {
  735. struct net_device *netdev = pdev->attached_dev;
  736. struct rtl_priv *priv = pdev->priv;
  737. struct switch_dev *dev = &priv->dev;
  738. struct switch_val val;
  739. unsigned int chipid, chipver, chiptype;
  740. int err;
  741. /* Only init the switch for the primary PHY */
  742. if (pdev->addr != 0)
  743. return 0;
  744. val.value.i = 1;
  745. priv->dev.cpu_port = RTL8306_PORT_CPU;
  746. priv->dev.ports = RTL8306_NUM_PORTS;
  747. priv->dev.vlans = RTL8306_NUM_VLANS;
  748. priv->dev.ops = &rtl8306_ops;
  749. priv->do_cpu = 0;
  750. priv->page = -1;
  751. priv->bus = pdev->bus;
  752. chipid = rtl_get(dev, RTL_REG_CHIPID);
  753. chipver = rtl_get(dev, RTL_REG_CHIPVER);
  754. chiptype = rtl_get(dev, RTL_REG_CHIPTYPE);
  755. switch(chiptype) {
  756. case 0:
  757. case 2:
  758. strncpy(priv->hwname, RTL_NAME_S, sizeof(priv->hwname));
  759. priv->type = RTL_TYPE_S;
  760. break;
  761. case 1:
  762. strncpy(priv->hwname, RTL_NAME_SD, sizeof(priv->hwname));
  763. priv->type = RTL_TYPE_SD;
  764. break;
  765. case 3:
  766. strncpy(priv->hwname, RTL_NAME_SDM, sizeof(priv->hwname));
  767. priv->type = RTL_TYPE_SDM;
  768. break;
  769. default:
  770. strncpy(priv->hwname, RTL_NAME_UNKNOWN, sizeof(priv->hwname));
  771. break;
  772. }
  773. dev->name = priv->hwname;
  774. rtl_hw_init(dev);
  775. printk(KERN_INFO "Registering %s switch with Chip ID: 0x%04x, version: 0x%04x\n", priv->hwname, chipid, chipver);
  776. err = register_switch(dev, netdev);
  777. if (err < 0) {
  778. kfree(priv);
  779. return err;
  780. }
  781. return 0;
  782. }
  783. static int
  784. rtl8306_fixup(struct phy_device *pdev)
  785. {
  786. struct rtl_priv priv;
  787. u16 chipid;
  788. /* Attach to primary LAN port and WAN port */
  789. if (pdev->addr != 0 && pdev->addr != 4)
  790. return 0;
  791. memset(&priv, 0, sizeof(priv));
  792. priv.fixup = true;
  793. priv.page = -1;
  794. priv.bus = pdev->bus;
  795. chipid = rtl_get(&priv.dev, RTL_REG_CHIPID);
  796. if (chipid == 0x5988)
  797. pdev->phy_id = RTL8306_MAGIC;
  798. return 0;
  799. }
  800. static int
  801. rtl8306_probe(struct phy_device *pdev)
  802. {
  803. struct rtl_priv *priv;
  804. list_for_each_entry(priv, &phydevs, list) {
  805. /*
  806. * share one rtl_priv instance between virtual phy
  807. * devices on the same bus
  808. */
  809. if (priv->bus == pdev->bus)
  810. goto found;
  811. }
  812. priv = kzalloc(sizeof(struct rtl_priv), GFP_KERNEL);
  813. if (!priv)
  814. return -ENOMEM;
  815. priv->bus = pdev->bus;
  816. found:
  817. pdev->priv = priv;
  818. return 0;
  819. }
  820. static void
  821. rtl8306_remove(struct phy_device *pdev)
  822. {
  823. struct rtl_priv *priv = pdev->priv;
  824. unregister_switch(&priv->dev);
  825. kfree(priv);
  826. }
  827. static int
  828. rtl8306_config_aneg(struct phy_device *pdev)
  829. {
  830. struct rtl_priv *priv = pdev->priv;
  831. /* Only for WAN */
  832. if (pdev->addr == 0)
  833. return 0;
  834. /* Restart autonegotiation */
  835. rtl_set(&priv->dev, RTL_PORT_REG(4, NWAY), 1);
  836. rtl_set(&priv->dev, RTL_PORT_REG(4, NRESTART), 1);
  837. return 0;
  838. }
  839. static int
  840. rtl8306_read_status(struct phy_device *pdev)
  841. {
  842. struct rtl_priv *priv = pdev->priv;
  843. struct switch_dev *dev = &priv->dev;
  844. if (pdev->addr == 4) {
  845. /* WAN */
  846. pdev->speed = rtl_get(dev, RTL_PORT_REG(4, SPEED)) ? SPEED_100 : SPEED_10;
  847. pdev->duplex = rtl_get(dev, RTL_PORT_REG(4, DUPLEX)) ? DUPLEX_FULL : DUPLEX_HALF;
  848. pdev->link = !!rtl_get(dev, RTL_PORT_REG(4, LINK));
  849. } else {
  850. /* LAN */
  851. pdev->speed = SPEED_100;
  852. pdev->duplex = DUPLEX_FULL;
  853. pdev->link = 1;
  854. }
  855. /*
  856. * Bypass generic PHY status read,
  857. * it doesn't work with this switch
  858. */
  859. if (pdev->link) {
  860. pdev->state = PHY_RUNNING;
  861. netif_carrier_on(pdev->attached_dev);
  862. pdev->adjust_link(pdev->attached_dev);
  863. } else {
  864. pdev->state = PHY_NOLINK;
  865. netif_carrier_off(pdev->attached_dev);
  866. pdev->adjust_link(pdev->attached_dev);
  867. }
  868. return 0;
  869. }
  870. static struct phy_driver rtl8306_driver = {
  871. .name = "Realtek RTL8306S",
  872. .flags = PHY_HAS_MAGICANEG,
  873. .phy_id = RTL8306_MAGIC,
  874. .phy_id_mask = 0xffffffff,
  875. .features = PHY_BASIC_FEATURES,
  876. .probe = &rtl8306_probe,
  877. .remove = &rtl8306_remove,
  878. .config_init = &rtl8306_config_init,
  879. .config_aneg = &rtl8306_config_aneg,
  880. .read_status = &rtl8306_read_status,
  881. .driver = { .owner = THIS_MODULE,},
  882. };
  883. static int __init
  884. rtl_init(void)
  885. {
  886. phy_register_fixup_for_id(PHY_ANY_ID, rtl8306_fixup);
  887. return phy_driver_register(&rtl8306_driver);
  888. }
  889. static void __exit
  890. rtl_exit(void)
  891. {
  892. phy_driver_unregister(&rtl8306_driver);
  893. }
  894. module_init(rtl_init);
  895. module_exit(rtl_exit);
  896. MODULE_LICENSE("GPL");