1
0

rtl8366s.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320
  1. /*
  2. * Platform driver for the Realtek RTL8366S ethernet switch
  3. *
  4. * Copyright (C) 2009-2010 Gabor Juhos <juhosg@openwrt.org>
  5. * Copyright (C) 2010 Antti Seppälä <a.seppala@gmail.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License version 2 as published
  9. * by the Free Software Foundation.
  10. */
  11. #include <linux/kernel.h>
  12. #include <linux/module.h>
  13. #include <linux/init.h>
  14. #include <linux/device.h>
  15. #include <linux/of.h>
  16. #include <linux/of_platform.h>
  17. #include <linux/delay.h>
  18. #include <linux/skbuff.h>
  19. #include <linux/rtl8366.h>
  20. #include "rtl8366_smi.h"
  21. #define RTL8366S_DRIVER_DESC "Realtek RTL8366S ethernet switch driver"
  22. #define RTL8366S_DRIVER_VER "0.2.2"
  23. #define RTL8366S_PHY_NO_MAX 4
  24. #define RTL8366S_PHY_PAGE_MAX 7
  25. #define RTL8366S_PHY_ADDR_MAX 31
  26. /* Switch Global Configuration register */
  27. #define RTL8366S_SGCR 0x0000
  28. #define RTL8366S_SGCR_EN_BC_STORM_CTRL BIT(0)
  29. #define RTL8366S_SGCR_MAX_LENGTH(_x) (_x << 4)
  30. #define RTL8366S_SGCR_MAX_LENGTH_MASK RTL8366S_SGCR_MAX_LENGTH(0x3)
  31. #define RTL8366S_SGCR_MAX_LENGTH_1522 RTL8366S_SGCR_MAX_LENGTH(0x0)
  32. #define RTL8366S_SGCR_MAX_LENGTH_1536 RTL8366S_SGCR_MAX_LENGTH(0x1)
  33. #define RTL8366S_SGCR_MAX_LENGTH_1552 RTL8366S_SGCR_MAX_LENGTH(0x2)
  34. #define RTL8366S_SGCR_MAX_LENGTH_16000 RTL8366S_SGCR_MAX_LENGTH(0x3)
  35. #define RTL8366S_SGCR_EN_VLAN BIT(13)
  36. /* Port Enable Control register */
  37. #define RTL8366S_PECR 0x0001
  38. /* Green Ethernet Feature (based on GPL_BELKIN_F5D8235-4_v1000 v1.01.24) */
  39. #define RTL8366S_GREEN_ETHERNET_CTRL_REG 0x000a
  40. #define RTL8366S_GREEN_ETHERNET_CTRL_MASK 0x0018
  41. #define RTL8366S_GREEN_ETHERNET_TX_BIT (1 << 3)
  42. #define RTL8366S_GREEN_ETHERNET_RX_BIT (1 << 4)
  43. /* Switch Security Control registers */
  44. #define RTL8366S_SSCR0 0x0002
  45. #define RTL8366S_SSCR1 0x0003
  46. #define RTL8366S_SSCR2 0x0004
  47. #define RTL8366S_SSCR2_DROP_UNKNOWN_DA BIT(0)
  48. #define RTL8366S_RESET_CTRL_REG 0x0100
  49. #define RTL8366S_CHIP_CTRL_RESET_HW 1
  50. #define RTL8366S_CHIP_CTRL_RESET_SW (1 << 1)
  51. #define RTL8366S_CHIP_VERSION_CTRL_REG 0x0104
  52. #define RTL8366S_CHIP_VERSION_MASK 0xf
  53. #define RTL8366S_CHIP_ID_REG 0x0105
  54. #define RTL8366S_CHIP_ID_8366 0x8366
  55. /* PHY registers control */
  56. #define RTL8366S_PHY_ACCESS_CTRL_REG 0x8028
  57. #define RTL8366S_PHY_ACCESS_DATA_REG 0x8029
  58. #define RTL8366S_PHY_CTRL_READ 1
  59. #define RTL8366S_PHY_CTRL_WRITE 0
  60. #define RTL8366S_PHY_REG_MASK 0x1f
  61. #define RTL8366S_PHY_PAGE_OFFSET 5
  62. #define RTL8366S_PHY_PAGE_MASK (0x7 << 5)
  63. #define RTL8366S_PHY_NO_OFFSET 9
  64. #define RTL8366S_PHY_NO_MASK (0x1f << 9)
  65. /* Green Ethernet Feature for PHY ports */
  66. #define RTL8366S_PHY_POWER_SAVING_CTRL_REG 12
  67. #define RTL8366S_PHY_POWER_SAVING_MASK 0x1000
  68. /* LED control registers */
  69. #define RTL8366S_LED_BLINKRATE_REG 0x0420
  70. #define RTL8366S_LED_BLINKRATE_BIT 0
  71. #define RTL8366S_LED_BLINKRATE_MASK 0x0007
  72. #define RTL8366S_LED_CTRL_REG 0x0421
  73. #define RTL8366S_LED_0_1_CTRL_REG 0x0422
  74. #define RTL8366S_LED_2_3_CTRL_REG 0x0423
  75. #define RTL8366S_MIB_COUNT 33
  76. #define RTL8366S_GLOBAL_MIB_COUNT 1
  77. #define RTL8366S_MIB_COUNTER_PORT_OFFSET 0x0040
  78. #define RTL8366S_MIB_COUNTER_BASE 0x1000
  79. #define RTL8366S_MIB_COUNTER_PORT_OFFSET2 0x0008
  80. #define RTL8366S_MIB_COUNTER_BASE2 0x1180
  81. #define RTL8366S_MIB_CTRL_REG 0x11F0
  82. #define RTL8366S_MIB_CTRL_USER_MASK 0x01FF
  83. #define RTL8366S_MIB_CTRL_BUSY_MASK 0x0001
  84. #define RTL8366S_MIB_CTRL_RESET_MASK 0x0002
  85. #define RTL8366S_MIB_CTRL_GLOBAL_RESET_MASK 0x0004
  86. #define RTL8366S_MIB_CTRL_PORT_RESET_BIT 0x0003
  87. #define RTL8366S_MIB_CTRL_PORT_RESET_MASK 0x01FC
  88. #define RTL8366S_PORT_VLAN_CTRL_BASE 0x0058
  89. #define RTL8366S_PORT_VLAN_CTRL_REG(_p) \
  90. (RTL8366S_PORT_VLAN_CTRL_BASE + (_p) / 4)
  91. #define RTL8366S_PORT_VLAN_CTRL_MASK 0xf
  92. #define RTL8366S_PORT_VLAN_CTRL_SHIFT(_p) (4 * ((_p) % 4))
  93. #define RTL8366S_VLAN_TABLE_READ_BASE 0x018B
  94. #define RTL8366S_VLAN_TABLE_WRITE_BASE 0x0185
  95. #define RTL8366S_VLAN_TB_CTRL_REG 0x010F
  96. #define RTL8366S_TABLE_ACCESS_CTRL_REG 0x0180
  97. #define RTL8366S_TABLE_VLAN_READ_CTRL 0x0E01
  98. #define RTL8366S_TABLE_VLAN_WRITE_CTRL 0x0F01
  99. #define RTL8366S_VLAN_MC_BASE(_x) (0x0016 + (_x) * 2)
  100. #define RTL8366S_VLAN_MEMBERINGRESS_REG 0x0379
  101. #define RTL8366S_PORT_LINK_STATUS_BASE 0x0060
  102. #define RTL8366S_PORT_STATUS_SPEED_MASK 0x0003
  103. #define RTL8366S_PORT_STATUS_DUPLEX_MASK 0x0004
  104. #define RTL8366S_PORT_STATUS_LINK_MASK 0x0010
  105. #define RTL8366S_PORT_STATUS_TXPAUSE_MASK 0x0020
  106. #define RTL8366S_PORT_STATUS_RXPAUSE_MASK 0x0040
  107. #define RTL8366S_PORT_STATUS_AN_MASK 0x0080
  108. #define RTL8366S_PORT_NUM_CPU 5
  109. #define RTL8366S_NUM_PORTS 6
  110. #define RTL8366S_NUM_VLANS 16
  111. #define RTL8366S_NUM_LEDGROUPS 4
  112. #define RTL8366S_NUM_VIDS 4096
  113. #define RTL8366S_PRIORITYMAX 7
  114. #define RTL8366S_FIDMAX 7
  115. #define RTL8366S_PORT_1 (1 << 0) /* In userspace port 0 */
  116. #define RTL8366S_PORT_2 (1 << 1) /* In userspace port 1 */
  117. #define RTL8366S_PORT_3 (1 << 2) /* In userspace port 2 */
  118. #define RTL8366S_PORT_4 (1 << 3) /* In userspace port 3 */
  119. #define RTL8366S_PORT_UNKNOWN (1 << 4) /* No known connection */
  120. #define RTL8366S_PORT_CPU (1 << 5) /* CPU port */
  121. #define RTL8366S_PORT_ALL (RTL8366S_PORT_1 | \
  122. RTL8366S_PORT_2 | \
  123. RTL8366S_PORT_3 | \
  124. RTL8366S_PORT_4 | \
  125. RTL8366S_PORT_UNKNOWN | \
  126. RTL8366S_PORT_CPU)
  127. #define RTL8366S_PORT_ALL_BUT_CPU (RTL8366S_PORT_1 | \
  128. RTL8366S_PORT_2 | \
  129. RTL8366S_PORT_3 | \
  130. RTL8366S_PORT_4 | \
  131. RTL8366S_PORT_UNKNOWN)
  132. #define RTL8366S_PORT_ALL_EXTERNAL (RTL8366S_PORT_1 | \
  133. RTL8366S_PORT_2 | \
  134. RTL8366S_PORT_3 | \
  135. RTL8366S_PORT_4)
  136. #define RTL8366S_PORT_ALL_INTERNAL (RTL8366S_PORT_UNKNOWN | \
  137. RTL8366S_PORT_CPU)
  138. #define RTL8366S_VLAN_VID_MASK 0xfff
  139. #define RTL8366S_VLAN_PRIORITY_SHIFT 12
  140. #define RTL8366S_VLAN_PRIORITY_MASK 0x7
  141. #define RTL8366S_VLAN_MEMBER_MASK 0x3f
  142. #define RTL8366S_VLAN_UNTAG_SHIFT 6
  143. #define RTL8366S_VLAN_UNTAG_MASK 0x3f
  144. #define RTL8366S_VLAN_FID_SHIFT 12
  145. #define RTL8366S_VLAN_FID_MASK 0x7
  146. #define RTL8366S_MIB_RXB_ID 0 /* IfInOctets */
  147. #define RTL8366S_MIB_TXB_ID 20 /* IfOutOctets */
  148. static struct rtl8366_mib_counter rtl8366s_mib_counters[] = {
  149. { 0, 0, 4, "IfInOctets" },
  150. { 0, 4, 4, "EtherStatsOctets" },
  151. { 0, 8, 2, "EtherStatsUnderSizePkts" },
  152. { 0, 10, 2, "EtherFragments" },
  153. { 0, 12, 2, "EtherStatsPkts64Octets" },
  154. { 0, 14, 2, "EtherStatsPkts65to127Octets" },
  155. { 0, 16, 2, "EtherStatsPkts128to255Octets" },
  156. { 0, 18, 2, "EtherStatsPkts256to511Octets" },
  157. { 0, 20, 2, "EtherStatsPkts512to1023Octets" },
  158. { 0, 22, 2, "EtherStatsPkts1024to1518Octets" },
  159. { 0, 24, 2, "EtherOversizeStats" },
  160. { 0, 26, 2, "EtherStatsJabbers" },
  161. { 0, 28, 2, "IfInUcastPkts" },
  162. { 0, 30, 2, "EtherStatsMulticastPkts" },
  163. { 0, 32, 2, "EtherStatsBroadcastPkts" },
  164. { 0, 34, 2, "EtherStatsDropEvents" },
  165. { 0, 36, 2, "Dot3StatsFCSErrors" },
  166. { 0, 38, 2, "Dot3StatsSymbolErrors" },
  167. { 0, 40, 2, "Dot3InPauseFrames" },
  168. { 0, 42, 2, "Dot3ControlInUnknownOpcodes" },
  169. { 0, 44, 4, "IfOutOctets" },
  170. { 0, 48, 2, "Dot3StatsSingleCollisionFrames" },
  171. { 0, 50, 2, "Dot3StatMultipleCollisionFrames" },
  172. { 0, 52, 2, "Dot3sDeferredTransmissions" },
  173. { 0, 54, 2, "Dot3StatsLateCollisions" },
  174. { 0, 56, 2, "EtherStatsCollisions" },
  175. { 0, 58, 2, "Dot3StatsExcessiveCollisions" },
  176. { 0, 60, 2, "Dot3OutPauseFrames" },
  177. { 0, 62, 2, "Dot1dBasePortDelayExceededDiscards" },
  178. /*
  179. * The following counters are accessible at a different
  180. * base address.
  181. */
  182. { 1, 0, 2, "Dot1dTpPortInDiscards" },
  183. { 1, 2, 2, "IfOutUcastPkts" },
  184. { 1, 4, 2, "IfOutMulticastPkts" },
  185. { 1, 6, 2, "IfOutBroadcastPkts" },
  186. };
  187. #define REG_WR(_smi, _reg, _val) \
  188. do { \
  189. err = rtl8366_smi_write_reg(_smi, _reg, _val); \
  190. if (err) \
  191. return err; \
  192. } while (0)
  193. #define REG_RMW(_smi, _reg, _mask, _val) \
  194. do { \
  195. err = rtl8366_smi_rmwr(_smi, _reg, _mask, _val); \
  196. if (err) \
  197. return err; \
  198. } while (0)
  199. static int rtl8366s_reset_chip(struct rtl8366_smi *smi)
  200. {
  201. int timeout = 10;
  202. u32 data;
  203. rtl8366_smi_write_reg_noack(smi, RTL8366S_RESET_CTRL_REG,
  204. RTL8366S_CHIP_CTRL_RESET_HW);
  205. do {
  206. msleep(1);
  207. if (rtl8366_smi_read_reg(smi, RTL8366S_RESET_CTRL_REG, &data))
  208. return -EIO;
  209. if (!(data & RTL8366S_CHIP_CTRL_RESET_HW))
  210. break;
  211. } while (--timeout);
  212. if (!timeout) {
  213. printk("Timeout waiting for the switch to reset\n");
  214. return -EIO;
  215. }
  216. return 0;
  217. }
  218. static int rtl8366s_read_phy_reg(struct rtl8366_smi *smi,
  219. u32 phy_no, u32 page, u32 addr, u32 *data)
  220. {
  221. u32 reg;
  222. int ret;
  223. if (phy_no > RTL8366S_PHY_NO_MAX)
  224. return -EINVAL;
  225. if (page > RTL8366S_PHY_PAGE_MAX)
  226. return -EINVAL;
  227. if (addr > RTL8366S_PHY_ADDR_MAX)
  228. return -EINVAL;
  229. ret = rtl8366_smi_write_reg(smi, RTL8366S_PHY_ACCESS_CTRL_REG,
  230. RTL8366S_PHY_CTRL_READ);
  231. if (ret)
  232. return ret;
  233. reg = 0x8000 | (1 << (phy_no + RTL8366S_PHY_NO_OFFSET)) |
  234. ((page << RTL8366S_PHY_PAGE_OFFSET) & RTL8366S_PHY_PAGE_MASK) |
  235. (addr & RTL8366S_PHY_REG_MASK);
  236. ret = rtl8366_smi_write_reg(smi, reg, 0);
  237. if (ret)
  238. return ret;
  239. ret = rtl8366_smi_read_reg(smi, RTL8366S_PHY_ACCESS_DATA_REG, data);
  240. if (ret)
  241. return ret;
  242. return 0;
  243. }
  244. static int rtl8366s_write_phy_reg(struct rtl8366_smi *smi,
  245. u32 phy_no, u32 page, u32 addr, u32 data)
  246. {
  247. u32 reg;
  248. int ret;
  249. if (phy_no > RTL8366S_PHY_NO_MAX)
  250. return -EINVAL;
  251. if (page > RTL8366S_PHY_PAGE_MAX)
  252. return -EINVAL;
  253. if (addr > RTL8366S_PHY_ADDR_MAX)
  254. return -EINVAL;
  255. ret = rtl8366_smi_write_reg(smi, RTL8366S_PHY_ACCESS_CTRL_REG,
  256. RTL8366S_PHY_CTRL_WRITE);
  257. if (ret)
  258. return ret;
  259. reg = 0x8000 | (1 << (phy_no + RTL8366S_PHY_NO_OFFSET)) |
  260. ((page << RTL8366S_PHY_PAGE_OFFSET) & RTL8366S_PHY_PAGE_MASK) |
  261. (addr & RTL8366S_PHY_REG_MASK);
  262. ret = rtl8366_smi_write_reg(smi, reg, data);
  263. if (ret)
  264. return ret;
  265. return 0;
  266. }
  267. static int rtl8366s_set_green_port(struct rtl8366_smi *smi, int port, int enable)
  268. {
  269. int err;
  270. u32 phyData;
  271. if (port >= RTL8366S_NUM_PORTS)
  272. return -EINVAL;
  273. err = rtl8366s_read_phy_reg(smi, port, 0, RTL8366S_PHY_POWER_SAVING_CTRL_REG, &phyData);
  274. if (err)
  275. return err;
  276. if (enable)
  277. phyData |= RTL8366S_PHY_POWER_SAVING_MASK;
  278. else
  279. phyData &= ~RTL8366S_PHY_POWER_SAVING_MASK;
  280. err = rtl8366s_write_phy_reg(smi, port, 0, RTL8366S_PHY_POWER_SAVING_CTRL_REG, phyData);
  281. if (err)
  282. return err;
  283. return 0;
  284. }
  285. static int rtl8366s_set_green(struct rtl8366_smi *smi, int enable)
  286. {
  287. int err;
  288. unsigned i;
  289. u32 data = 0;
  290. if (!enable) {
  291. for (i = 0; i <= RTL8366S_PHY_NO_MAX; i++) {
  292. rtl8366s_set_green_port(smi, i, 0);
  293. }
  294. }
  295. if (enable)
  296. data = (RTL8366S_GREEN_ETHERNET_TX_BIT | RTL8366S_GREEN_ETHERNET_RX_BIT);
  297. REG_RMW(smi, RTL8366S_GREEN_ETHERNET_CTRL_REG, RTL8366S_GREEN_ETHERNET_CTRL_MASK, data);
  298. return 0;
  299. }
  300. static int rtl8366s_setup(struct rtl8366_smi *smi)
  301. {
  302. struct rtl8366_platform_data *pdata;
  303. int err;
  304. unsigned i;
  305. #ifdef CONFIG_OF
  306. struct device_node *np;
  307. unsigned num_initvals;
  308. const __be32 *paddr;
  309. #endif
  310. pdata = smi->parent->platform_data;
  311. if (pdata && pdata->num_initvals && pdata->initvals) {
  312. dev_info(smi->parent, "applying initvals\n");
  313. for (i = 0; i < pdata->num_initvals; i++)
  314. REG_WR(smi, pdata->initvals[i].reg,
  315. pdata->initvals[i].val);
  316. }
  317. #ifdef CONFIG_OF
  318. np = smi->parent->of_node;
  319. paddr = of_get_property(np, "realtek,initvals", &num_initvals);
  320. if (paddr) {
  321. dev_info(smi->parent, "applying initvals from DTS\n");
  322. if (num_initvals < (2 * sizeof(*paddr)))
  323. return -EINVAL;
  324. num_initvals /= sizeof(*paddr);
  325. for (i = 0; i < num_initvals - 1; i += 2) {
  326. u32 reg = be32_to_cpup(paddr + i);
  327. u32 val = be32_to_cpup(paddr + i + 1);
  328. REG_WR(smi, reg, val);
  329. }
  330. }
  331. if (of_property_read_bool(np, "realtek,green-ethernet-features")) {
  332. dev_info(smi->parent, "activating Green Ethernet features\n");
  333. err = rtl8366s_set_green(smi, 1);
  334. if (err)
  335. return err;
  336. for (i = 0; i <= RTL8366S_PHY_NO_MAX; i++) {
  337. err = rtl8366s_set_green_port(smi, i, 1);
  338. if (err)
  339. return err;
  340. }
  341. }
  342. #endif
  343. /* set maximum packet length to 1536 bytes */
  344. REG_RMW(smi, RTL8366S_SGCR, RTL8366S_SGCR_MAX_LENGTH_MASK,
  345. RTL8366S_SGCR_MAX_LENGTH_1536);
  346. /* enable learning for all ports */
  347. REG_WR(smi, RTL8366S_SSCR0, 0);
  348. /* enable auto ageing for all ports */
  349. REG_WR(smi, RTL8366S_SSCR1, 0);
  350. /*
  351. * discard VLAN tagged packets if the port is not a member of
  352. * the VLAN with which the packets is associated.
  353. */
  354. REG_WR(smi, RTL8366S_VLAN_MEMBERINGRESS_REG, RTL8366S_PORT_ALL);
  355. /* don't drop packets whose DA has not been learned */
  356. REG_RMW(smi, RTL8366S_SSCR2, RTL8366S_SSCR2_DROP_UNKNOWN_DA, 0);
  357. return 0;
  358. }
  359. static int rtl8366_get_mib_counter(struct rtl8366_smi *smi, int counter,
  360. int port, unsigned long long *val)
  361. {
  362. int i;
  363. int err;
  364. u32 addr, data;
  365. u64 mibvalue;
  366. if (port > RTL8366S_NUM_PORTS || counter >= RTL8366S_MIB_COUNT)
  367. return -EINVAL;
  368. switch (rtl8366s_mib_counters[counter].base) {
  369. case 0:
  370. addr = RTL8366S_MIB_COUNTER_BASE +
  371. RTL8366S_MIB_COUNTER_PORT_OFFSET * port;
  372. break;
  373. case 1:
  374. addr = RTL8366S_MIB_COUNTER_BASE2 +
  375. RTL8366S_MIB_COUNTER_PORT_OFFSET2 * port;
  376. break;
  377. default:
  378. return -EINVAL;
  379. }
  380. addr += rtl8366s_mib_counters[counter].offset;
  381. /*
  382. * Writing access counter address first
  383. * then ASIC will prepare 64bits counter wait for being retrived
  384. */
  385. data = 0; /* writing data will be discard by ASIC */
  386. err = rtl8366_smi_write_reg(smi, addr, data);
  387. if (err)
  388. return err;
  389. /* read MIB control register */
  390. err = rtl8366_smi_read_reg(smi, RTL8366S_MIB_CTRL_REG, &data);
  391. if (err)
  392. return err;
  393. if (data & RTL8366S_MIB_CTRL_BUSY_MASK)
  394. return -EBUSY;
  395. if (data & RTL8366S_MIB_CTRL_RESET_MASK)
  396. return -EIO;
  397. mibvalue = 0;
  398. for (i = rtl8366s_mib_counters[counter].length; i > 0; i--) {
  399. err = rtl8366_smi_read_reg(smi, addr + (i - 1), &data);
  400. if (err)
  401. return err;
  402. mibvalue = (mibvalue << 16) | (data & 0xFFFF);
  403. }
  404. *val = mibvalue;
  405. return 0;
  406. }
  407. static int rtl8366s_get_vlan_4k(struct rtl8366_smi *smi, u32 vid,
  408. struct rtl8366_vlan_4k *vlan4k)
  409. {
  410. u32 data[2];
  411. int err;
  412. int i;
  413. memset(vlan4k, '\0', sizeof(struct rtl8366_vlan_4k));
  414. if (vid >= RTL8366S_NUM_VIDS)
  415. return -EINVAL;
  416. /* write VID */
  417. err = rtl8366_smi_write_reg(smi, RTL8366S_VLAN_TABLE_WRITE_BASE,
  418. vid & RTL8366S_VLAN_VID_MASK);
  419. if (err)
  420. return err;
  421. /* write table access control word */
  422. err = rtl8366_smi_write_reg(smi, RTL8366S_TABLE_ACCESS_CTRL_REG,
  423. RTL8366S_TABLE_VLAN_READ_CTRL);
  424. if (err)
  425. return err;
  426. for (i = 0; i < 2; i++) {
  427. err = rtl8366_smi_read_reg(smi,
  428. RTL8366S_VLAN_TABLE_READ_BASE + i,
  429. &data[i]);
  430. if (err)
  431. return err;
  432. }
  433. vlan4k->vid = vid;
  434. vlan4k->untag = (data[1] >> RTL8366S_VLAN_UNTAG_SHIFT) &
  435. RTL8366S_VLAN_UNTAG_MASK;
  436. vlan4k->member = data[1] & RTL8366S_VLAN_MEMBER_MASK;
  437. vlan4k->fid = (data[1] >> RTL8366S_VLAN_FID_SHIFT) &
  438. RTL8366S_VLAN_FID_MASK;
  439. return 0;
  440. }
  441. static int rtl8366s_set_vlan_4k(struct rtl8366_smi *smi,
  442. const struct rtl8366_vlan_4k *vlan4k)
  443. {
  444. u32 data[2];
  445. int err;
  446. int i;
  447. if (vlan4k->vid >= RTL8366S_NUM_VIDS ||
  448. vlan4k->member > RTL8366S_VLAN_MEMBER_MASK ||
  449. vlan4k->untag > RTL8366S_VLAN_UNTAG_MASK ||
  450. vlan4k->fid > RTL8366S_FIDMAX)
  451. return -EINVAL;
  452. data[0] = vlan4k->vid & RTL8366S_VLAN_VID_MASK;
  453. data[1] = (vlan4k->member & RTL8366S_VLAN_MEMBER_MASK) |
  454. ((vlan4k->untag & RTL8366S_VLAN_UNTAG_MASK) <<
  455. RTL8366S_VLAN_UNTAG_SHIFT) |
  456. ((vlan4k->fid & RTL8366S_VLAN_FID_MASK) <<
  457. RTL8366S_VLAN_FID_SHIFT);
  458. for (i = 0; i < 2; i++) {
  459. err = rtl8366_smi_write_reg(smi,
  460. RTL8366S_VLAN_TABLE_WRITE_BASE + i,
  461. data[i]);
  462. if (err)
  463. return err;
  464. }
  465. /* write table access control word */
  466. err = rtl8366_smi_write_reg(smi, RTL8366S_TABLE_ACCESS_CTRL_REG,
  467. RTL8366S_TABLE_VLAN_WRITE_CTRL);
  468. return err;
  469. }
  470. static int rtl8366s_get_vlan_mc(struct rtl8366_smi *smi, u32 index,
  471. struct rtl8366_vlan_mc *vlanmc)
  472. {
  473. u32 data[2];
  474. int err;
  475. int i;
  476. memset(vlanmc, '\0', sizeof(struct rtl8366_vlan_mc));
  477. if (index >= RTL8366S_NUM_VLANS)
  478. return -EINVAL;
  479. for (i = 0; i < 2; i++) {
  480. err = rtl8366_smi_read_reg(smi,
  481. RTL8366S_VLAN_MC_BASE(index) + i,
  482. &data[i]);
  483. if (err)
  484. return err;
  485. }
  486. vlanmc->vid = data[0] & RTL8366S_VLAN_VID_MASK;
  487. vlanmc->priority = (data[0] >> RTL8366S_VLAN_PRIORITY_SHIFT) &
  488. RTL8366S_VLAN_PRIORITY_MASK;
  489. vlanmc->untag = (data[1] >> RTL8366S_VLAN_UNTAG_SHIFT) &
  490. RTL8366S_VLAN_UNTAG_MASK;
  491. vlanmc->member = data[1] & RTL8366S_VLAN_MEMBER_MASK;
  492. vlanmc->fid = (data[1] >> RTL8366S_VLAN_FID_SHIFT) &
  493. RTL8366S_VLAN_FID_MASK;
  494. return 0;
  495. }
  496. static int rtl8366s_set_vlan_mc(struct rtl8366_smi *smi, u32 index,
  497. const struct rtl8366_vlan_mc *vlanmc)
  498. {
  499. u32 data[2];
  500. int err;
  501. int i;
  502. if (index >= RTL8366S_NUM_VLANS ||
  503. vlanmc->vid >= RTL8366S_NUM_VIDS ||
  504. vlanmc->priority > RTL8366S_PRIORITYMAX ||
  505. vlanmc->member > RTL8366S_VLAN_MEMBER_MASK ||
  506. vlanmc->untag > RTL8366S_VLAN_UNTAG_MASK ||
  507. vlanmc->fid > RTL8366S_FIDMAX)
  508. return -EINVAL;
  509. data[0] = (vlanmc->vid & RTL8366S_VLAN_VID_MASK) |
  510. ((vlanmc->priority & RTL8366S_VLAN_PRIORITY_MASK) <<
  511. RTL8366S_VLAN_PRIORITY_SHIFT);
  512. data[1] = (vlanmc->member & RTL8366S_VLAN_MEMBER_MASK) |
  513. ((vlanmc->untag & RTL8366S_VLAN_UNTAG_MASK) <<
  514. RTL8366S_VLAN_UNTAG_SHIFT) |
  515. ((vlanmc->fid & RTL8366S_VLAN_FID_MASK) <<
  516. RTL8366S_VLAN_FID_SHIFT);
  517. for (i = 0; i < 2; i++) {
  518. err = rtl8366_smi_write_reg(smi,
  519. RTL8366S_VLAN_MC_BASE(index) + i,
  520. data[i]);
  521. if (err)
  522. return err;
  523. }
  524. return 0;
  525. }
  526. static int rtl8366s_get_mc_index(struct rtl8366_smi *smi, int port, int *val)
  527. {
  528. u32 data;
  529. int err;
  530. if (port >= RTL8366S_NUM_PORTS)
  531. return -EINVAL;
  532. err = rtl8366_smi_read_reg(smi, RTL8366S_PORT_VLAN_CTRL_REG(port),
  533. &data);
  534. if (err)
  535. return err;
  536. *val = (data >> RTL8366S_PORT_VLAN_CTRL_SHIFT(port)) &
  537. RTL8366S_PORT_VLAN_CTRL_MASK;
  538. return 0;
  539. }
  540. static int rtl8366s_set_mc_index(struct rtl8366_smi *smi, int port, int index)
  541. {
  542. if (port >= RTL8366S_NUM_PORTS || index >= RTL8366S_NUM_VLANS)
  543. return -EINVAL;
  544. return rtl8366_smi_rmwr(smi, RTL8366S_PORT_VLAN_CTRL_REG(port),
  545. RTL8366S_PORT_VLAN_CTRL_MASK <<
  546. RTL8366S_PORT_VLAN_CTRL_SHIFT(port),
  547. (index & RTL8366S_PORT_VLAN_CTRL_MASK) <<
  548. RTL8366S_PORT_VLAN_CTRL_SHIFT(port));
  549. }
  550. static int rtl8366s_enable_vlan(struct rtl8366_smi *smi, int enable)
  551. {
  552. return rtl8366_smi_rmwr(smi, RTL8366S_SGCR, RTL8366S_SGCR_EN_VLAN,
  553. (enable) ? RTL8366S_SGCR_EN_VLAN : 0);
  554. }
  555. static int rtl8366s_enable_vlan4k(struct rtl8366_smi *smi, int enable)
  556. {
  557. return rtl8366_smi_rmwr(smi, RTL8366S_VLAN_TB_CTRL_REG,
  558. 1, (enable) ? 1 : 0);
  559. }
  560. static int rtl8366s_is_vlan_valid(struct rtl8366_smi *smi, unsigned vlan)
  561. {
  562. unsigned max = RTL8366S_NUM_VLANS;
  563. if (smi->vlan4k_enabled)
  564. max = RTL8366S_NUM_VIDS - 1;
  565. if (vlan == 0 || vlan >= max)
  566. return 0;
  567. return 1;
  568. }
  569. static int rtl8366s_enable_port(struct rtl8366_smi *smi, int port, int enable)
  570. {
  571. return rtl8366_smi_rmwr(smi, RTL8366S_PECR, (1 << port),
  572. (enable) ? 0 : (1 << port));
  573. }
  574. static int rtl8366s_sw_reset_mibs(struct switch_dev *dev,
  575. const struct switch_attr *attr,
  576. struct switch_val *val)
  577. {
  578. struct rtl8366_smi *smi = sw_to_rtl8366_smi(dev);
  579. return rtl8366_smi_rmwr(smi, RTL8366S_MIB_CTRL_REG, 0, (1 << 2));
  580. }
  581. static int rtl8366s_sw_get_blinkrate(struct switch_dev *dev,
  582. const struct switch_attr *attr,
  583. struct switch_val *val)
  584. {
  585. struct rtl8366_smi *smi = sw_to_rtl8366_smi(dev);
  586. u32 data;
  587. rtl8366_smi_read_reg(smi, RTL8366S_LED_BLINKRATE_REG, &data);
  588. val->value.i = (data & (RTL8366S_LED_BLINKRATE_MASK));
  589. return 0;
  590. }
  591. static int rtl8366s_sw_set_blinkrate(struct switch_dev *dev,
  592. const struct switch_attr *attr,
  593. struct switch_val *val)
  594. {
  595. struct rtl8366_smi *smi = sw_to_rtl8366_smi(dev);
  596. if (val->value.i >= 6)
  597. return -EINVAL;
  598. return rtl8366_smi_rmwr(smi, RTL8366S_LED_BLINKRATE_REG,
  599. RTL8366S_LED_BLINKRATE_MASK,
  600. val->value.i);
  601. }
  602. static int rtl8366s_sw_get_max_length(struct switch_dev *dev,
  603. const struct switch_attr *attr,
  604. struct switch_val *val)
  605. {
  606. struct rtl8366_smi *smi = sw_to_rtl8366_smi(dev);
  607. u32 data;
  608. rtl8366_smi_read_reg(smi, RTL8366S_SGCR, &data);
  609. val->value.i = ((data & (RTL8366S_SGCR_MAX_LENGTH_MASK)) >> 4);
  610. return 0;
  611. }
  612. static int rtl8366s_sw_set_max_length(struct switch_dev *dev,
  613. const struct switch_attr *attr,
  614. struct switch_val *val)
  615. {
  616. struct rtl8366_smi *smi = sw_to_rtl8366_smi(dev);
  617. char length_code;
  618. switch (val->value.i) {
  619. case 0:
  620. length_code = RTL8366S_SGCR_MAX_LENGTH_1522;
  621. break;
  622. case 1:
  623. length_code = RTL8366S_SGCR_MAX_LENGTH_1536;
  624. break;
  625. case 2:
  626. length_code = RTL8366S_SGCR_MAX_LENGTH_1552;
  627. break;
  628. case 3:
  629. length_code = RTL8366S_SGCR_MAX_LENGTH_16000;
  630. break;
  631. default:
  632. return -EINVAL;
  633. }
  634. return rtl8366_smi_rmwr(smi, RTL8366S_SGCR,
  635. RTL8366S_SGCR_MAX_LENGTH_MASK,
  636. length_code);
  637. }
  638. static int rtl8366s_sw_get_learning_enable(struct switch_dev *dev,
  639. const struct switch_attr *attr,
  640. struct switch_val *val)
  641. {
  642. struct rtl8366_smi *smi = sw_to_rtl8366_smi(dev);
  643. u32 data;
  644. rtl8366_smi_read_reg(smi,RTL8366S_SSCR0, &data);
  645. val->value.i = !data;
  646. return 0;
  647. }
  648. static int rtl8366s_sw_set_learning_enable(struct switch_dev *dev,
  649. const struct switch_attr *attr,
  650. struct switch_val *val)
  651. {
  652. struct rtl8366_smi *smi = sw_to_rtl8366_smi(dev);
  653. u32 portmask = 0;
  654. int err = 0;
  655. if (!val->value.i)
  656. portmask = RTL8366S_PORT_ALL;
  657. /* set learning for all ports */
  658. REG_WR(smi, RTL8366S_SSCR0, portmask);
  659. /* set auto ageing for all ports */
  660. REG_WR(smi, RTL8366S_SSCR1, portmask);
  661. return 0;
  662. }
  663. static int rtl8366s_sw_get_green(struct switch_dev *dev,
  664. const struct switch_attr *attr,
  665. struct switch_val *val)
  666. {
  667. struct rtl8366_smi *smi = sw_to_rtl8366_smi(dev);
  668. u32 data;
  669. int err;
  670. err = rtl8366_smi_read_reg(smi, RTL8366S_GREEN_ETHERNET_CTRL_REG, &data);
  671. if (err)
  672. return err;
  673. val->value.i = ((data & (RTL8366S_GREEN_ETHERNET_TX_BIT | RTL8366S_GREEN_ETHERNET_RX_BIT)) != 0) ? 1 : 0;
  674. return 0;
  675. }
  676. static int rtl8366s_sw_set_green(struct switch_dev *dev,
  677. const struct switch_attr *attr,
  678. struct switch_val *val)
  679. {
  680. struct rtl8366_smi *smi = sw_to_rtl8366_smi(dev);
  681. return rtl8366s_set_green(smi, val->value.i);
  682. }
  683. static int rtl8366s_sw_get_port_link(struct switch_dev *dev,
  684. int port,
  685. struct switch_port_link *link)
  686. {
  687. struct rtl8366_smi *smi = sw_to_rtl8366_smi(dev);
  688. u32 data = 0;
  689. u32 speed;
  690. if (port >= RTL8366S_NUM_PORTS)
  691. return -EINVAL;
  692. rtl8366_smi_read_reg(smi, RTL8366S_PORT_LINK_STATUS_BASE + (port / 2),
  693. &data);
  694. if (port % 2)
  695. data = data >> 8;
  696. link->link = !!(data & RTL8366S_PORT_STATUS_LINK_MASK);
  697. if (!link->link)
  698. return 0;
  699. link->duplex = !!(data & RTL8366S_PORT_STATUS_DUPLEX_MASK);
  700. link->rx_flow = !!(data & RTL8366S_PORT_STATUS_RXPAUSE_MASK);
  701. link->tx_flow = !!(data & RTL8366S_PORT_STATUS_TXPAUSE_MASK);
  702. link->aneg = !!(data & RTL8366S_PORT_STATUS_AN_MASK);
  703. speed = (data & RTL8366S_PORT_STATUS_SPEED_MASK);
  704. switch (speed) {
  705. case 0:
  706. link->speed = SWITCH_PORT_SPEED_10;
  707. break;
  708. case 1:
  709. link->speed = SWITCH_PORT_SPEED_100;
  710. break;
  711. case 2:
  712. link->speed = SWITCH_PORT_SPEED_1000;
  713. break;
  714. default:
  715. link->speed = SWITCH_PORT_SPEED_UNKNOWN;
  716. break;
  717. }
  718. return 0;
  719. }
  720. static int rtl8366s_sw_set_port_led(struct switch_dev *dev,
  721. const struct switch_attr *attr,
  722. struct switch_val *val)
  723. {
  724. struct rtl8366_smi *smi = sw_to_rtl8366_smi(dev);
  725. u32 data;
  726. u32 mask;
  727. u32 reg;
  728. if (val->port_vlan >= RTL8366S_NUM_PORTS ||
  729. (1 << val->port_vlan) == RTL8366S_PORT_UNKNOWN)
  730. return -EINVAL;
  731. if (val->port_vlan == RTL8366S_PORT_NUM_CPU) {
  732. reg = RTL8366S_LED_BLINKRATE_REG;
  733. mask = 0xF << 4;
  734. data = val->value.i << 4;
  735. } else {
  736. reg = RTL8366S_LED_CTRL_REG;
  737. mask = 0xF << (val->port_vlan * 4),
  738. data = val->value.i << (val->port_vlan * 4);
  739. }
  740. return rtl8366_smi_rmwr(smi, reg, mask, data);
  741. }
  742. static int rtl8366s_sw_get_port_led(struct switch_dev *dev,
  743. const struct switch_attr *attr,
  744. struct switch_val *val)
  745. {
  746. struct rtl8366_smi *smi = sw_to_rtl8366_smi(dev);
  747. u32 data = 0;
  748. if (val->port_vlan >= RTL8366S_NUM_LEDGROUPS)
  749. return -EINVAL;
  750. rtl8366_smi_read_reg(smi, RTL8366S_LED_CTRL_REG, &data);
  751. val->value.i = (data >> (val->port_vlan * 4)) & 0x000F;
  752. return 0;
  753. }
  754. static int rtl8366s_sw_get_green_port(struct switch_dev *dev,
  755. const struct switch_attr *attr,
  756. struct switch_val *val)
  757. {
  758. struct rtl8366_smi *smi = sw_to_rtl8366_smi(dev);
  759. int err;
  760. u32 phyData;
  761. if (val->port_vlan >= RTL8366S_NUM_PORTS)
  762. return -EINVAL;
  763. err = rtl8366s_read_phy_reg(smi, val->port_vlan, 0, RTL8366S_PHY_POWER_SAVING_CTRL_REG, &phyData);
  764. if (err)
  765. return err;
  766. val->value.i = ((phyData & RTL8366S_PHY_POWER_SAVING_MASK) != 0) ? 1 : 0;
  767. return 0;
  768. }
  769. static int rtl8366s_sw_set_green_port(struct switch_dev *dev,
  770. const struct switch_attr *attr,
  771. struct switch_val *val)
  772. {
  773. struct rtl8366_smi *smi = sw_to_rtl8366_smi(dev);
  774. return rtl8366s_set_green_port(smi, val->port_vlan, val->value.i);
  775. }
  776. static int rtl8366s_sw_reset_port_mibs(struct switch_dev *dev,
  777. const struct switch_attr *attr,
  778. struct switch_val *val)
  779. {
  780. struct rtl8366_smi *smi = sw_to_rtl8366_smi(dev);
  781. if (val->port_vlan >= RTL8366S_NUM_PORTS)
  782. return -EINVAL;
  783. return rtl8366_smi_rmwr(smi, RTL8366S_MIB_CTRL_REG,
  784. 0, (1 << (val->port_vlan + 3)));
  785. }
  786. static int rtl8366s_sw_get_port_stats(struct switch_dev *dev, int port,
  787. struct switch_port_stats *stats)
  788. {
  789. return (rtl8366_sw_get_port_stats(dev, port, stats,
  790. RTL8366S_MIB_TXB_ID, RTL8366S_MIB_RXB_ID));
  791. }
  792. static struct switch_attr rtl8366s_globals[] = {
  793. {
  794. .type = SWITCH_TYPE_INT,
  795. .name = "enable_learning",
  796. .description = "Enable learning, enable aging",
  797. .set = rtl8366s_sw_set_learning_enable,
  798. .get = rtl8366s_sw_get_learning_enable,
  799. .max = 1,
  800. }, {
  801. .type = SWITCH_TYPE_INT,
  802. .name = "enable_vlan",
  803. .description = "Enable VLAN mode",
  804. .set = rtl8366_sw_set_vlan_enable,
  805. .get = rtl8366_sw_get_vlan_enable,
  806. .max = 1,
  807. .ofs = 1
  808. }, {
  809. .type = SWITCH_TYPE_INT,
  810. .name = "enable_vlan4k",
  811. .description = "Enable VLAN 4K mode",
  812. .set = rtl8366_sw_set_vlan_enable,
  813. .get = rtl8366_sw_get_vlan_enable,
  814. .max = 1,
  815. .ofs = 2
  816. }, {
  817. .type = SWITCH_TYPE_NOVAL,
  818. .name = "reset_mibs",
  819. .description = "Reset all MIB counters",
  820. .set = rtl8366s_sw_reset_mibs,
  821. }, {
  822. .type = SWITCH_TYPE_INT,
  823. .name = "blinkrate",
  824. .description = "Get/Set LED blinking rate (0 = 43ms, 1 = 84ms,"
  825. " 2 = 120ms, 3 = 170ms, 4 = 340ms, 5 = 670ms)",
  826. .set = rtl8366s_sw_set_blinkrate,
  827. .get = rtl8366s_sw_get_blinkrate,
  828. .max = 5
  829. }, {
  830. .type = SWITCH_TYPE_INT,
  831. .name = "max_length",
  832. .description = "Get/Set the maximum length of valid packets"
  833. " (0 = 1522, 1 = 1536, 2 = 1552, 3 = 16000 (9216?))",
  834. .set = rtl8366s_sw_set_max_length,
  835. .get = rtl8366s_sw_get_max_length,
  836. .max = 3,
  837. }, {
  838. .type = SWITCH_TYPE_INT,
  839. .name = "green_mode",
  840. .description = "Get/Set the router green feature",
  841. .set = rtl8366s_sw_set_green,
  842. .get = rtl8366s_sw_get_green,
  843. .max = 1,
  844. },
  845. };
  846. static struct switch_attr rtl8366s_port[] = {
  847. {
  848. .type = SWITCH_TYPE_NOVAL,
  849. .name = "reset_mib",
  850. .description = "Reset single port MIB counters",
  851. .set = rtl8366s_sw_reset_port_mibs,
  852. }, {
  853. .type = SWITCH_TYPE_STRING,
  854. .name = "mib",
  855. .description = "Get MIB counters for port",
  856. .max = 33,
  857. .set = NULL,
  858. .get = rtl8366_sw_get_port_mib,
  859. }, {
  860. .type = SWITCH_TYPE_INT,
  861. .name = "led",
  862. .description = "Get/Set port group (0 - 3) led mode (0 - 15)",
  863. .max = 15,
  864. .set = rtl8366s_sw_set_port_led,
  865. .get = rtl8366s_sw_get_port_led,
  866. }, {
  867. .type = SWITCH_TYPE_INT,
  868. .name = "green_port",
  869. .description = "Get/Set port green feature (0 - 1)",
  870. .max = 1,
  871. .set = rtl8366s_sw_set_green_port,
  872. .get = rtl8366s_sw_get_green_port,
  873. },
  874. };
  875. static struct switch_attr rtl8366s_vlan[] = {
  876. {
  877. .type = SWITCH_TYPE_STRING,
  878. .name = "info",
  879. .description = "Get vlan information",
  880. .max = 1,
  881. .set = NULL,
  882. .get = rtl8366_sw_get_vlan_info,
  883. }, {
  884. .type = SWITCH_TYPE_INT,
  885. .name = "fid",
  886. .description = "Get/Set vlan FID",
  887. .max = RTL8366S_FIDMAX,
  888. .set = rtl8366_sw_set_vlan_fid,
  889. .get = rtl8366_sw_get_vlan_fid,
  890. },
  891. };
  892. static const struct switch_dev_ops rtl8366_ops = {
  893. .attr_global = {
  894. .attr = rtl8366s_globals,
  895. .n_attr = ARRAY_SIZE(rtl8366s_globals),
  896. },
  897. .attr_port = {
  898. .attr = rtl8366s_port,
  899. .n_attr = ARRAY_SIZE(rtl8366s_port),
  900. },
  901. .attr_vlan = {
  902. .attr = rtl8366s_vlan,
  903. .n_attr = ARRAY_SIZE(rtl8366s_vlan),
  904. },
  905. .get_vlan_ports = rtl8366_sw_get_vlan_ports,
  906. .set_vlan_ports = rtl8366_sw_set_vlan_ports,
  907. .get_port_pvid = rtl8366_sw_get_port_pvid,
  908. .set_port_pvid = rtl8366_sw_set_port_pvid,
  909. .reset_switch = rtl8366_sw_reset_switch,
  910. .get_port_link = rtl8366s_sw_get_port_link,
  911. .get_port_stats = rtl8366s_sw_get_port_stats,
  912. };
  913. static int rtl8366s_switch_init(struct rtl8366_smi *smi)
  914. {
  915. struct switch_dev *dev = &smi->sw_dev;
  916. int err;
  917. dev->name = "RTL8366S";
  918. dev->cpu_port = RTL8366S_PORT_NUM_CPU;
  919. dev->ports = RTL8366S_NUM_PORTS;
  920. dev->vlans = RTL8366S_NUM_VIDS;
  921. dev->ops = &rtl8366_ops;
  922. dev->alias = dev_name(smi->parent);
  923. err = register_switch(dev, NULL);
  924. if (err)
  925. dev_err(smi->parent, "switch registration failed\n");
  926. return err;
  927. }
  928. static void rtl8366s_switch_cleanup(struct rtl8366_smi *smi)
  929. {
  930. unregister_switch(&smi->sw_dev);
  931. }
  932. static int rtl8366s_mii_read(struct mii_bus *bus, int addr, int reg)
  933. {
  934. struct rtl8366_smi *smi = bus->priv;
  935. u32 val = 0;
  936. int err;
  937. err = rtl8366s_read_phy_reg(smi, addr, 0, reg, &val);
  938. if (err)
  939. return 0xffff;
  940. return val;
  941. }
  942. static int rtl8366s_mii_write(struct mii_bus *bus, int addr, int reg, u16 val)
  943. {
  944. struct rtl8366_smi *smi = bus->priv;
  945. u32 t;
  946. int err;
  947. err = rtl8366s_write_phy_reg(smi, addr, 0, reg, val);
  948. /* flush write */
  949. (void) rtl8366s_read_phy_reg(smi, addr, 0, reg, &t);
  950. return err;
  951. }
  952. static int rtl8366s_detect(struct rtl8366_smi *smi)
  953. {
  954. u32 chip_id = 0;
  955. u32 chip_ver = 0;
  956. int ret;
  957. ret = rtl8366_smi_read_reg(smi, RTL8366S_CHIP_ID_REG, &chip_id);
  958. if (ret) {
  959. dev_err(smi->parent, "unable to read chip id\n");
  960. return ret;
  961. }
  962. switch (chip_id) {
  963. case RTL8366S_CHIP_ID_8366:
  964. break;
  965. default:
  966. dev_err(smi->parent, "unknown chip id (%04x)\n", chip_id);
  967. return -ENODEV;
  968. }
  969. ret = rtl8366_smi_read_reg(smi, RTL8366S_CHIP_VERSION_CTRL_REG,
  970. &chip_ver);
  971. if (ret) {
  972. dev_err(smi->parent, "unable to read chip version\n");
  973. return ret;
  974. }
  975. dev_info(smi->parent, "RTL%04x ver. %u chip found\n",
  976. chip_id, chip_ver & RTL8366S_CHIP_VERSION_MASK);
  977. return 0;
  978. }
  979. static struct rtl8366_smi_ops rtl8366s_smi_ops = {
  980. .detect = rtl8366s_detect,
  981. .reset_chip = rtl8366s_reset_chip,
  982. .setup = rtl8366s_setup,
  983. .mii_read = rtl8366s_mii_read,
  984. .mii_write = rtl8366s_mii_write,
  985. .get_vlan_mc = rtl8366s_get_vlan_mc,
  986. .set_vlan_mc = rtl8366s_set_vlan_mc,
  987. .get_vlan_4k = rtl8366s_get_vlan_4k,
  988. .set_vlan_4k = rtl8366s_set_vlan_4k,
  989. .get_mc_index = rtl8366s_get_mc_index,
  990. .set_mc_index = rtl8366s_set_mc_index,
  991. .get_mib_counter = rtl8366_get_mib_counter,
  992. .is_vlan_valid = rtl8366s_is_vlan_valid,
  993. .enable_vlan = rtl8366s_enable_vlan,
  994. .enable_vlan4k = rtl8366s_enable_vlan4k,
  995. .enable_port = rtl8366s_enable_port,
  996. };
  997. static int rtl8366s_probe(struct platform_device *pdev)
  998. {
  999. static int rtl8366_smi_version_printed;
  1000. struct rtl8366_smi *smi;
  1001. int err;
  1002. if (!rtl8366_smi_version_printed++)
  1003. printk(KERN_NOTICE RTL8366S_DRIVER_DESC
  1004. " version " RTL8366S_DRIVER_VER"\n");
  1005. smi = rtl8366_smi_probe(pdev);
  1006. if (IS_ERR(smi))
  1007. return PTR_ERR(smi);
  1008. smi->clk_delay = 10;
  1009. smi->cmd_read = 0xa9;
  1010. smi->cmd_write = 0xa8;
  1011. smi->ops = &rtl8366s_smi_ops;
  1012. smi->cpu_port = RTL8366S_PORT_NUM_CPU;
  1013. smi->num_ports = RTL8366S_NUM_PORTS;
  1014. smi->num_vlan_mc = RTL8366S_NUM_VLANS;
  1015. smi->mib_counters = rtl8366s_mib_counters;
  1016. smi->num_mib_counters = ARRAY_SIZE(rtl8366s_mib_counters);
  1017. err = rtl8366_smi_init(smi);
  1018. if (err)
  1019. goto err_free_smi;
  1020. platform_set_drvdata(pdev, smi);
  1021. err = rtl8366s_switch_init(smi);
  1022. if (err)
  1023. goto err_clear_drvdata;
  1024. return 0;
  1025. err_clear_drvdata:
  1026. platform_set_drvdata(pdev, NULL);
  1027. rtl8366_smi_cleanup(smi);
  1028. err_free_smi:
  1029. kfree(smi);
  1030. return err;
  1031. }
  1032. static int rtl8366s_remove(struct platform_device *pdev)
  1033. {
  1034. struct rtl8366_smi *smi = platform_get_drvdata(pdev);
  1035. if (smi) {
  1036. rtl8366s_switch_cleanup(smi);
  1037. platform_set_drvdata(pdev, NULL);
  1038. rtl8366_smi_cleanup(smi);
  1039. kfree(smi);
  1040. }
  1041. return 0;
  1042. }
  1043. #ifdef CONFIG_OF
  1044. static const struct of_device_id rtl8366s_match[] = {
  1045. { .compatible = "realtek,rtl8366s" },
  1046. {},
  1047. };
  1048. MODULE_DEVICE_TABLE(of, rtl8366s_match);
  1049. #endif
  1050. static struct platform_driver rtl8366s_driver = {
  1051. .driver = {
  1052. .name = RTL8366S_DRIVER_NAME,
  1053. .owner = THIS_MODULE,
  1054. #ifdef CONFIG_OF
  1055. .of_match_table = of_match_ptr(rtl8366s_match),
  1056. #endif
  1057. },
  1058. .probe = rtl8366s_probe,
  1059. .remove = rtl8366s_remove,
  1060. };
  1061. static int __init rtl8366s_module_init(void)
  1062. {
  1063. return platform_driver_register(&rtl8366s_driver);
  1064. }
  1065. module_init(rtl8366s_module_init);
  1066. static void __exit rtl8366s_module_exit(void)
  1067. {
  1068. platform_driver_unregister(&rtl8366s_driver);
  1069. }
  1070. module_exit(rtl8366s_module_exit);
  1071. MODULE_DESCRIPTION(RTL8366S_DRIVER_DESC);
  1072. MODULE_VERSION(RTL8366S_DRIVER_VER);
  1073. MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org>");
  1074. MODULE_AUTHOR("Antti Seppälä <a.seppala@gmail.com>");
  1075. MODULE_LICENSE("GPL v2");
  1076. MODULE_ALIAS("platform:" RTL8366S_DRIVER_NAME);