rtl8306.c 25 KB

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