rtl8306.c 24 KB

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