1
0

rtl8366_smi.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632
  1. /*
  2. * Realtek RTL8366 SMI interface driver
  3. *
  4. * Copyright (C) 2009-2010 Gabor Juhos <juhosg@openwrt.org>
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License version 2 as published
  8. * by the Free Software Foundation.
  9. */
  10. #include <linux/kernel.h>
  11. #include <linux/module.h>
  12. #include <linux/device.h>
  13. #include <linux/delay.h>
  14. #include <linux/gpio.h>
  15. #include <linux/spinlock.h>
  16. #include <linux/skbuff.h>
  17. #include <linux/of.h>
  18. #include <linux/of_platform.h>
  19. #include <linux/of_gpio.h>
  20. #include <linux/rtl8366.h>
  21. #include <linux/version.h>
  22. #include <linux/of_mdio.h>
  23. #ifdef CONFIG_RTL8366_SMI_DEBUG_FS
  24. #include <linux/debugfs.h>
  25. #endif
  26. #include "rtl8366_smi.h"
  27. #define RTL8366_SMI_ACK_RETRY_COUNT 5
  28. #define RTL8366_SMI_HW_STOP_DELAY 25 /* msecs */
  29. #define RTL8366_SMI_HW_START_DELAY 100 /* msecs */
  30. static inline void rtl8366_smi_clk_delay(struct rtl8366_smi *smi)
  31. {
  32. ndelay(smi->clk_delay);
  33. }
  34. static void rtl8366_smi_start(struct rtl8366_smi *smi)
  35. {
  36. unsigned int sda = smi->gpio_sda;
  37. unsigned int sck = smi->gpio_sck;
  38. /*
  39. * Set GPIO pins to output mode, with initial state:
  40. * SCK = 0, SDA = 1
  41. */
  42. gpio_direction_output(sck, 0);
  43. gpio_direction_output(sda, 1);
  44. rtl8366_smi_clk_delay(smi);
  45. /* CLK 1: 0 -> 1, 1 -> 0 */
  46. gpio_set_value(sck, 1);
  47. rtl8366_smi_clk_delay(smi);
  48. gpio_set_value(sck, 0);
  49. rtl8366_smi_clk_delay(smi);
  50. /* CLK 2: */
  51. gpio_set_value(sck, 1);
  52. rtl8366_smi_clk_delay(smi);
  53. gpio_set_value(sda, 0);
  54. rtl8366_smi_clk_delay(smi);
  55. gpio_set_value(sck, 0);
  56. rtl8366_smi_clk_delay(smi);
  57. gpio_set_value(sda, 1);
  58. }
  59. static void rtl8366_smi_stop(struct rtl8366_smi *smi)
  60. {
  61. unsigned int sda = smi->gpio_sda;
  62. unsigned int sck = smi->gpio_sck;
  63. rtl8366_smi_clk_delay(smi);
  64. gpio_set_value(sda, 0);
  65. gpio_set_value(sck, 1);
  66. rtl8366_smi_clk_delay(smi);
  67. gpio_set_value(sda, 1);
  68. rtl8366_smi_clk_delay(smi);
  69. gpio_set_value(sck, 1);
  70. rtl8366_smi_clk_delay(smi);
  71. gpio_set_value(sck, 0);
  72. rtl8366_smi_clk_delay(smi);
  73. gpio_set_value(sck, 1);
  74. /* add a click */
  75. rtl8366_smi_clk_delay(smi);
  76. gpio_set_value(sck, 0);
  77. rtl8366_smi_clk_delay(smi);
  78. gpio_set_value(sck, 1);
  79. /* set GPIO pins to input mode */
  80. gpio_direction_input(sda);
  81. gpio_direction_input(sck);
  82. }
  83. static void rtl8366_smi_write_bits(struct rtl8366_smi *smi, u32 data, u32 len)
  84. {
  85. unsigned int sda = smi->gpio_sda;
  86. unsigned int sck = smi->gpio_sck;
  87. for (; len > 0; len--) {
  88. rtl8366_smi_clk_delay(smi);
  89. /* prepare data */
  90. gpio_set_value(sda, !!(data & ( 1 << (len - 1))));
  91. rtl8366_smi_clk_delay(smi);
  92. /* clocking */
  93. gpio_set_value(sck, 1);
  94. rtl8366_smi_clk_delay(smi);
  95. gpio_set_value(sck, 0);
  96. }
  97. }
  98. static void rtl8366_smi_read_bits(struct rtl8366_smi *smi, u32 len, u32 *data)
  99. {
  100. unsigned int sda = smi->gpio_sda;
  101. unsigned int sck = smi->gpio_sck;
  102. gpio_direction_input(sda);
  103. for (*data = 0; len > 0; len--) {
  104. u32 u;
  105. rtl8366_smi_clk_delay(smi);
  106. /* clocking */
  107. gpio_set_value(sck, 1);
  108. rtl8366_smi_clk_delay(smi);
  109. u = !!gpio_get_value(sda);
  110. gpio_set_value(sck, 0);
  111. *data |= (u << (len - 1));
  112. }
  113. gpio_direction_output(sda, 0);
  114. }
  115. static int rtl8366_smi_wait_for_ack(struct rtl8366_smi *smi)
  116. {
  117. int retry_cnt;
  118. retry_cnt = 0;
  119. do {
  120. u32 ack;
  121. rtl8366_smi_read_bits(smi, 1, &ack);
  122. if (ack == 0)
  123. break;
  124. if (++retry_cnt > RTL8366_SMI_ACK_RETRY_COUNT) {
  125. dev_err(smi->parent, "ACK timeout\n");
  126. return -ETIMEDOUT;
  127. }
  128. } while (1);
  129. return 0;
  130. }
  131. static int rtl8366_smi_write_byte(struct rtl8366_smi *smi, u8 data)
  132. {
  133. rtl8366_smi_write_bits(smi, data, 8);
  134. return rtl8366_smi_wait_for_ack(smi);
  135. }
  136. static int rtl8366_smi_write_byte_noack(struct rtl8366_smi *smi, u8 data)
  137. {
  138. rtl8366_smi_write_bits(smi, data, 8);
  139. return 0;
  140. }
  141. static int rtl8366_smi_read_byte0(struct rtl8366_smi *smi, u8 *data)
  142. {
  143. u32 t;
  144. /* read data */
  145. rtl8366_smi_read_bits(smi, 8, &t);
  146. *data = (t & 0xff);
  147. /* send an ACK */
  148. rtl8366_smi_write_bits(smi, 0x00, 1);
  149. return 0;
  150. }
  151. static int rtl8366_smi_read_byte1(struct rtl8366_smi *smi, u8 *data)
  152. {
  153. u32 t;
  154. /* read data */
  155. rtl8366_smi_read_bits(smi, 8, &t);
  156. *data = (t & 0xff);
  157. /* send an ACK */
  158. rtl8366_smi_write_bits(smi, 0x01, 1);
  159. return 0;
  160. }
  161. static int __rtl8366_smi_read_reg(struct rtl8366_smi *smi, u32 addr, u32 *data)
  162. {
  163. unsigned long flags;
  164. u8 lo = 0;
  165. u8 hi = 0;
  166. int ret;
  167. spin_lock_irqsave(&smi->lock, flags);
  168. rtl8366_smi_start(smi);
  169. /* send READ command */
  170. ret = rtl8366_smi_write_byte(smi, smi->cmd_read);
  171. if (ret)
  172. goto out;
  173. /* set ADDR[7:0] */
  174. ret = rtl8366_smi_write_byte(smi, addr & 0xff);
  175. if (ret)
  176. goto out;
  177. /* set ADDR[15:8] */
  178. ret = rtl8366_smi_write_byte(smi, addr >> 8);
  179. if (ret)
  180. goto out;
  181. /* read DATA[7:0] */
  182. rtl8366_smi_read_byte0(smi, &lo);
  183. /* read DATA[15:8] */
  184. rtl8366_smi_read_byte1(smi, &hi);
  185. *data = ((u32) lo) | (((u32) hi) << 8);
  186. ret = 0;
  187. out:
  188. rtl8366_smi_stop(smi);
  189. spin_unlock_irqrestore(&smi->lock, flags);
  190. return ret;
  191. }
  192. /* Read/write via mdiobus */
  193. #define MDC_MDIO_CTRL0_REG 31
  194. #define MDC_MDIO_START_REG 29
  195. #define MDC_MDIO_CTRL1_REG 21
  196. #define MDC_MDIO_ADDRESS_REG 23
  197. #define MDC_MDIO_DATA_WRITE_REG 24
  198. #define MDC_MDIO_DATA_READ_REG 25
  199. #define MDC_MDIO_START_OP 0xFFFF
  200. #define MDC_MDIO_ADDR_OP 0x000E
  201. #define MDC_MDIO_READ_OP 0x0001
  202. #define MDC_MDIO_WRITE_OP 0x0003
  203. #define MDC_REALTEK_PHY_ADDR 0x0
  204. int __rtl8366_mdio_read_reg(struct rtl8366_smi *smi, u32 addr, u32 *data)
  205. {
  206. u32 phy_id = MDC_REALTEK_PHY_ADDR;
  207. struct mii_bus *mbus = smi->ext_mbus;
  208. BUG_ON(in_interrupt());
  209. mutex_lock(&mbus->mdio_lock);
  210. /* Write Start command to register 29 */
  211. mbus->write(mbus, phy_id, MDC_MDIO_START_REG, MDC_MDIO_START_OP);
  212. /* Write address control code to register 31 */
  213. mbus->write(mbus, phy_id, MDC_MDIO_CTRL0_REG, MDC_MDIO_ADDR_OP);
  214. /* Write Start command to register 29 */
  215. mbus->write(mbus, phy_id, MDC_MDIO_START_REG, MDC_MDIO_START_OP);
  216. /* Write address to register 23 */
  217. mbus->write(mbus, phy_id, MDC_MDIO_ADDRESS_REG, addr);
  218. /* Write Start command to register 29 */
  219. mbus->write(mbus, phy_id, MDC_MDIO_START_REG, MDC_MDIO_START_OP);
  220. /* Write read control code to register 21 */
  221. mbus->write(mbus, phy_id, MDC_MDIO_CTRL1_REG, MDC_MDIO_READ_OP);
  222. /* Write Start command to register 29 */
  223. mbus->write(smi->ext_mbus, phy_id, MDC_MDIO_START_REG, MDC_MDIO_START_OP);
  224. /* Read data from register 25 */
  225. *data = mbus->read(mbus, phy_id, MDC_MDIO_DATA_READ_REG);
  226. mutex_unlock(&mbus->mdio_lock);
  227. return 0;
  228. }
  229. static int __rtl8366_mdio_write_reg(struct rtl8366_smi *smi, u32 addr, u32 data)
  230. {
  231. u32 phy_id = MDC_REALTEK_PHY_ADDR;
  232. struct mii_bus *mbus = smi->ext_mbus;
  233. BUG_ON(in_interrupt());
  234. mutex_lock(&mbus->mdio_lock);
  235. /* Write Start command to register 29 */
  236. mbus->write(mbus, phy_id, MDC_MDIO_START_REG, MDC_MDIO_START_OP);
  237. /* Write address control code to register 31 */
  238. mbus->write(mbus, phy_id, MDC_MDIO_CTRL0_REG, MDC_MDIO_ADDR_OP);
  239. /* Write Start command to register 29 */
  240. mbus->write(mbus, phy_id, MDC_MDIO_START_REG, MDC_MDIO_START_OP);
  241. /* Write address to register 23 */
  242. mbus->write(mbus, phy_id, MDC_MDIO_ADDRESS_REG, addr);
  243. /* Write Start command to register 29 */
  244. mbus->write(mbus, phy_id, MDC_MDIO_START_REG, MDC_MDIO_START_OP);
  245. /* Write data to register 24 */
  246. mbus->write(mbus, phy_id, MDC_MDIO_DATA_WRITE_REG, data);
  247. /* Write Start command to register 29 */
  248. mbus->write(mbus, phy_id, MDC_MDIO_START_REG, MDC_MDIO_START_OP);
  249. /* Write data control code to register 21 */
  250. mbus->write(mbus, phy_id, MDC_MDIO_CTRL1_REG, MDC_MDIO_WRITE_OP);
  251. mutex_unlock(&mbus->mdio_lock);
  252. return 0;
  253. }
  254. int rtl8366_smi_read_reg(struct rtl8366_smi *smi, u32 addr, u32 *data)
  255. {
  256. if (smi->ext_mbus)
  257. return __rtl8366_mdio_read_reg(smi, addr, data);
  258. else
  259. return __rtl8366_smi_read_reg(smi, addr, data);
  260. }
  261. EXPORT_SYMBOL_GPL(rtl8366_smi_read_reg);
  262. static int __rtl8366_smi_write_reg(struct rtl8366_smi *smi,
  263. u32 addr, u32 data, bool ack)
  264. {
  265. unsigned long flags;
  266. int ret;
  267. spin_lock_irqsave(&smi->lock, flags);
  268. rtl8366_smi_start(smi);
  269. /* send WRITE command */
  270. ret = rtl8366_smi_write_byte(smi, smi->cmd_write);
  271. if (ret)
  272. goto out;
  273. /* set ADDR[7:0] */
  274. ret = rtl8366_smi_write_byte(smi, addr & 0xff);
  275. if (ret)
  276. goto out;
  277. /* set ADDR[15:8] */
  278. ret = rtl8366_smi_write_byte(smi, addr >> 8);
  279. if (ret)
  280. goto out;
  281. /* write DATA[7:0] */
  282. ret = rtl8366_smi_write_byte(smi, data & 0xff);
  283. if (ret)
  284. goto out;
  285. /* write DATA[15:8] */
  286. if (ack)
  287. ret = rtl8366_smi_write_byte(smi, data >> 8);
  288. else
  289. ret = rtl8366_smi_write_byte_noack(smi, data >> 8);
  290. if (ret)
  291. goto out;
  292. ret = 0;
  293. out:
  294. rtl8366_smi_stop(smi);
  295. spin_unlock_irqrestore(&smi->lock, flags);
  296. return ret;
  297. }
  298. int rtl8366_smi_write_reg(struct rtl8366_smi *smi, u32 addr, u32 data)
  299. {
  300. if (smi->ext_mbus)
  301. return __rtl8366_mdio_write_reg(smi, addr, data);
  302. else
  303. return __rtl8366_smi_write_reg(smi, addr, data, true);
  304. }
  305. EXPORT_SYMBOL_GPL(rtl8366_smi_write_reg);
  306. int rtl8366_smi_write_reg_noack(struct rtl8366_smi *smi, u32 addr, u32 data)
  307. {
  308. return __rtl8366_smi_write_reg(smi, addr, data, false);
  309. }
  310. EXPORT_SYMBOL_GPL(rtl8366_smi_write_reg_noack);
  311. int rtl8366_smi_rmwr(struct rtl8366_smi *smi, u32 addr, u32 mask, u32 data)
  312. {
  313. u32 t;
  314. int err;
  315. err = rtl8366_smi_read_reg(smi, addr, &t);
  316. if (err)
  317. return err;
  318. err = rtl8366_smi_write_reg(smi, addr, (t & ~mask) | data);
  319. return err;
  320. }
  321. EXPORT_SYMBOL_GPL(rtl8366_smi_rmwr);
  322. static int rtl8366_reset(struct rtl8366_smi *smi)
  323. {
  324. if (smi->hw_reset) {
  325. smi->hw_reset(smi, true);
  326. msleep(RTL8366_SMI_HW_STOP_DELAY);
  327. smi->hw_reset(smi, false);
  328. msleep(RTL8366_SMI_HW_START_DELAY);
  329. return 0;
  330. }
  331. return smi->ops->reset_chip(smi);
  332. }
  333. static int rtl8366_mc_is_used(struct rtl8366_smi *smi, int mc_index, int *used)
  334. {
  335. int err;
  336. int i;
  337. *used = 0;
  338. for (i = 0; i < smi->num_ports; i++) {
  339. int index = 0;
  340. err = smi->ops->get_mc_index(smi, i, &index);
  341. if (err)
  342. return err;
  343. if (mc_index == index) {
  344. *used = 1;
  345. break;
  346. }
  347. }
  348. return 0;
  349. }
  350. static int rtl8366_set_vlan(struct rtl8366_smi *smi, int vid, u32 member,
  351. u32 untag, u32 fid)
  352. {
  353. struct rtl8366_vlan_4k vlan4k;
  354. int err;
  355. int i;
  356. /* Update the 4K table */
  357. err = smi->ops->get_vlan_4k(smi, vid, &vlan4k);
  358. if (err)
  359. return err;
  360. vlan4k.member = member;
  361. vlan4k.untag = untag;
  362. vlan4k.fid = fid;
  363. err = smi->ops->set_vlan_4k(smi, &vlan4k);
  364. if (err)
  365. return err;
  366. /* Try to find an existing MC entry for this VID */
  367. for (i = 0; i < smi->num_vlan_mc; i++) {
  368. struct rtl8366_vlan_mc vlanmc;
  369. err = smi->ops->get_vlan_mc(smi, i, &vlanmc);
  370. if (err)
  371. return err;
  372. if (vid == vlanmc.vid) {
  373. /* update the MC entry */
  374. vlanmc.member = member;
  375. vlanmc.untag = untag;
  376. vlanmc.fid = fid;
  377. err = smi->ops->set_vlan_mc(smi, i, &vlanmc);
  378. break;
  379. }
  380. }
  381. return err;
  382. }
  383. static int rtl8366_get_pvid(struct rtl8366_smi *smi, int port, int *val)
  384. {
  385. struct rtl8366_vlan_mc vlanmc;
  386. int err;
  387. int index;
  388. err = smi->ops->get_mc_index(smi, port, &index);
  389. if (err)
  390. return err;
  391. err = smi->ops->get_vlan_mc(smi, index, &vlanmc);
  392. if (err)
  393. return err;
  394. *val = vlanmc.vid;
  395. return 0;
  396. }
  397. static int rtl8366_set_pvid(struct rtl8366_smi *smi, unsigned port,
  398. unsigned vid)
  399. {
  400. struct rtl8366_vlan_mc vlanmc;
  401. struct rtl8366_vlan_4k vlan4k;
  402. int err;
  403. int i;
  404. /* Try to find an existing MC entry for this VID */
  405. for (i = 0; i < smi->num_vlan_mc; i++) {
  406. err = smi->ops->get_vlan_mc(smi, i, &vlanmc);
  407. if (err)
  408. return err;
  409. if (vid == vlanmc.vid) {
  410. err = smi->ops->set_vlan_mc(smi, i, &vlanmc);
  411. if (err)
  412. return err;
  413. err = smi->ops->set_mc_index(smi, port, i);
  414. return err;
  415. }
  416. }
  417. /* We have no MC entry for this VID, try to find an empty one */
  418. for (i = 0; i < smi->num_vlan_mc; i++) {
  419. err = smi->ops->get_vlan_mc(smi, i, &vlanmc);
  420. if (err)
  421. return err;
  422. if (vlanmc.vid == 0 && vlanmc.member == 0) {
  423. /* Update the entry from the 4K table */
  424. err = smi->ops->get_vlan_4k(smi, vid, &vlan4k);
  425. if (err)
  426. return err;
  427. vlanmc.vid = vid;
  428. vlanmc.member = vlan4k.member;
  429. vlanmc.untag = vlan4k.untag;
  430. vlanmc.fid = vlan4k.fid;
  431. err = smi->ops->set_vlan_mc(smi, i, &vlanmc);
  432. if (err)
  433. return err;
  434. err = smi->ops->set_mc_index(smi, port, i);
  435. return err;
  436. }
  437. }
  438. /* MC table is full, try to find an unused entry and replace it */
  439. for (i = 0; i < smi->num_vlan_mc; i++) {
  440. int used;
  441. err = rtl8366_mc_is_used(smi, i, &used);
  442. if (err)
  443. return err;
  444. if (!used) {
  445. /* Update the entry from the 4K table */
  446. err = smi->ops->get_vlan_4k(smi, vid, &vlan4k);
  447. if (err)
  448. return err;
  449. vlanmc.vid = vid;
  450. vlanmc.member = vlan4k.member;
  451. vlanmc.untag = vlan4k.untag;
  452. vlanmc.fid = vlan4k.fid;
  453. err = smi->ops->set_vlan_mc(smi, i, &vlanmc);
  454. if (err)
  455. return err;
  456. err = smi->ops->set_mc_index(smi, port, i);
  457. return err;
  458. }
  459. }
  460. dev_err(smi->parent,
  461. "all VLAN member configurations are in use\n");
  462. return -ENOSPC;
  463. }
  464. int rtl8366_enable_vlan(struct rtl8366_smi *smi, int enable)
  465. {
  466. int err;
  467. err = smi->ops->enable_vlan(smi, enable);
  468. if (err)
  469. return err;
  470. smi->vlan_enabled = enable;
  471. if (!enable) {
  472. smi->vlan4k_enabled = 0;
  473. err = smi->ops->enable_vlan4k(smi, enable);
  474. }
  475. return err;
  476. }
  477. EXPORT_SYMBOL_GPL(rtl8366_enable_vlan);
  478. static int rtl8366_enable_vlan4k(struct rtl8366_smi *smi, int enable)
  479. {
  480. int err;
  481. if (enable) {
  482. err = smi->ops->enable_vlan(smi, enable);
  483. if (err)
  484. return err;
  485. smi->vlan_enabled = enable;
  486. }
  487. err = smi->ops->enable_vlan4k(smi, enable);
  488. if (err)
  489. return err;
  490. smi->vlan4k_enabled = enable;
  491. return 0;
  492. }
  493. int rtl8366_enable_all_ports(struct rtl8366_smi *smi, int enable)
  494. {
  495. int port;
  496. int err;
  497. for (port = 0; port < smi->num_ports; port++) {
  498. err = smi->ops->enable_port(smi, port, enable);
  499. if (err)
  500. return err;
  501. }
  502. return 0;
  503. }
  504. EXPORT_SYMBOL_GPL(rtl8366_enable_all_ports);
  505. int rtl8366_reset_vlan(struct rtl8366_smi *smi)
  506. {
  507. struct rtl8366_vlan_mc vlanmc;
  508. int err;
  509. int i;
  510. rtl8366_enable_vlan(smi, 0);
  511. rtl8366_enable_vlan4k(smi, 0);
  512. /* clear VLAN member configurations */
  513. vlanmc.vid = 0;
  514. vlanmc.priority = 0;
  515. vlanmc.member = 0;
  516. vlanmc.untag = 0;
  517. vlanmc.fid = 0;
  518. for (i = 0; i < smi->num_vlan_mc; i++) {
  519. err = smi->ops->set_vlan_mc(smi, i, &vlanmc);
  520. if (err)
  521. return err;
  522. }
  523. return 0;
  524. }
  525. EXPORT_SYMBOL_GPL(rtl8366_reset_vlan);
  526. static int rtl8366_init_vlan(struct rtl8366_smi *smi)
  527. {
  528. int port;
  529. int err;
  530. err = rtl8366_reset_vlan(smi);
  531. if (err)
  532. return err;
  533. for (port = 0; port < smi->num_ports; port++) {
  534. u32 mask;
  535. if (port == smi->cpu_port)
  536. mask = (1 << smi->num_ports) - 1;
  537. else
  538. mask = (1 << port) | (1 << smi->cpu_port);
  539. err = rtl8366_set_vlan(smi, (port + 1), mask, mask, 0);
  540. if (err)
  541. return err;
  542. err = rtl8366_set_pvid(smi, port, (port + 1));
  543. if (err)
  544. return err;
  545. }
  546. return rtl8366_enable_vlan(smi, 1);
  547. }
  548. #ifdef CONFIG_RTL8366_SMI_DEBUG_FS
  549. int rtl8366_debugfs_open(struct inode *inode, struct file *file)
  550. {
  551. file->private_data = inode->i_private;
  552. return 0;
  553. }
  554. EXPORT_SYMBOL_GPL(rtl8366_debugfs_open);
  555. static ssize_t rtl8366_read_debugfs_vlan_mc(struct file *file,
  556. char __user *user_buf,
  557. size_t count, loff_t *ppos)
  558. {
  559. struct rtl8366_smi *smi = (struct rtl8366_smi *)file->private_data;
  560. int i, len = 0;
  561. char *buf = smi->buf;
  562. len += snprintf(buf + len, sizeof(smi->buf) - len,
  563. "%2s %6s %4s %6s %6s %3s\n",
  564. "id", "vid","prio", "member", "untag", "fid");
  565. for (i = 0; i < smi->num_vlan_mc; ++i) {
  566. struct rtl8366_vlan_mc vlanmc;
  567. smi->ops->get_vlan_mc(smi, i, &vlanmc);
  568. len += snprintf(buf + len, sizeof(smi->buf) - len,
  569. "%2d %6d %4d 0x%04x 0x%04x %3d\n",
  570. i, vlanmc.vid, vlanmc.priority,
  571. vlanmc.member, vlanmc.untag, vlanmc.fid);
  572. }
  573. return simple_read_from_buffer(user_buf, count, ppos, buf, len);
  574. }
  575. #define RTL8366_VLAN4K_PAGE_SIZE 64
  576. #define RTL8366_VLAN4K_NUM_PAGES (4096 / RTL8366_VLAN4K_PAGE_SIZE)
  577. static ssize_t rtl8366_read_debugfs_vlan_4k(struct file *file,
  578. char __user *user_buf,
  579. size_t count, loff_t *ppos)
  580. {
  581. struct rtl8366_smi *smi = (struct rtl8366_smi *)file->private_data;
  582. int i, len = 0;
  583. int offset;
  584. char *buf = smi->buf;
  585. if (smi->dbg_vlan_4k_page >= RTL8366_VLAN4K_NUM_PAGES) {
  586. len += snprintf(buf + len, sizeof(smi->buf) - len,
  587. "invalid page: %u\n", smi->dbg_vlan_4k_page);
  588. return simple_read_from_buffer(user_buf, count, ppos, buf, len);
  589. }
  590. len += snprintf(buf + len, sizeof(smi->buf) - len,
  591. "%4s %6s %6s %3s\n",
  592. "vid", "member", "untag", "fid");
  593. offset = RTL8366_VLAN4K_PAGE_SIZE * smi->dbg_vlan_4k_page;
  594. for (i = 0; i < RTL8366_VLAN4K_PAGE_SIZE; i++) {
  595. struct rtl8366_vlan_4k vlan4k;
  596. smi->ops->get_vlan_4k(smi, offset + i, &vlan4k);
  597. len += snprintf(buf + len, sizeof(smi->buf) - len,
  598. "%4d 0x%04x 0x%04x %3d\n",
  599. vlan4k.vid, vlan4k.member,
  600. vlan4k.untag, vlan4k.fid);
  601. }
  602. return simple_read_from_buffer(user_buf, count, ppos, buf, len);
  603. }
  604. static ssize_t rtl8366_read_debugfs_pvid(struct file *file,
  605. char __user *user_buf,
  606. size_t count, loff_t *ppos)
  607. {
  608. struct rtl8366_smi *smi = (struct rtl8366_smi *)file->private_data;
  609. char *buf = smi->buf;
  610. int len = 0;
  611. int i;
  612. len += snprintf(buf + len, sizeof(smi->buf) - len, "%4s %4s\n",
  613. "port", "pvid");
  614. for (i = 0; i < smi->num_ports; i++) {
  615. int pvid;
  616. int err;
  617. err = rtl8366_get_pvid(smi, i, &pvid);
  618. if (err)
  619. len += snprintf(buf + len, sizeof(smi->buf) - len,
  620. "%4d error\n", i);
  621. else
  622. len += snprintf(buf + len, sizeof(smi->buf) - len,
  623. "%4d %4d\n", i, pvid);
  624. }
  625. return simple_read_from_buffer(user_buf, count, ppos, buf, len);
  626. }
  627. static ssize_t rtl8366_read_debugfs_reg(struct file *file,
  628. char __user *user_buf,
  629. size_t count, loff_t *ppos)
  630. {
  631. struct rtl8366_smi *smi = (struct rtl8366_smi *)file->private_data;
  632. u32 t, reg = smi->dbg_reg;
  633. int err, len = 0;
  634. char *buf = smi->buf;
  635. memset(buf, '\0', sizeof(smi->buf));
  636. err = rtl8366_smi_read_reg(smi, reg, &t);
  637. if (err) {
  638. len += snprintf(buf, sizeof(smi->buf),
  639. "Read failed (reg: 0x%04x)\n", reg);
  640. return simple_read_from_buffer(user_buf, count, ppos, buf, len);
  641. }
  642. len += snprintf(buf, sizeof(smi->buf), "reg = 0x%04x, val = 0x%04x\n",
  643. reg, t);
  644. return simple_read_from_buffer(user_buf, count, ppos, buf, len);
  645. }
  646. static ssize_t rtl8366_write_debugfs_reg(struct file *file,
  647. const char __user *user_buf,
  648. size_t count, loff_t *ppos)
  649. {
  650. struct rtl8366_smi *smi = (struct rtl8366_smi *)file->private_data;
  651. unsigned long data;
  652. u32 reg = smi->dbg_reg;
  653. int err;
  654. size_t len;
  655. char *buf = smi->buf;
  656. len = min(count, sizeof(smi->buf) - 1);
  657. if (copy_from_user(buf, user_buf, len)) {
  658. dev_err(smi->parent, "copy from user failed\n");
  659. return -EFAULT;
  660. }
  661. buf[len] = '\0';
  662. if (len > 0 && buf[len - 1] == '\n')
  663. buf[len - 1] = '\0';
  664. if (kstrtoul(buf, 16, &data)) {
  665. dev_err(smi->parent, "Invalid reg value %s\n", buf);
  666. } else {
  667. err = rtl8366_smi_write_reg(smi, reg, data);
  668. if (err) {
  669. dev_err(smi->parent,
  670. "writing reg 0x%04x val 0x%04lx failed\n",
  671. reg, data);
  672. }
  673. }
  674. return count;
  675. }
  676. static ssize_t rtl8366_read_debugfs_mibs(struct file *file,
  677. char __user *user_buf,
  678. size_t count, loff_t *ppos)
  679. {
  680. struct rtl8366_smi *smi = file->private_data;
  681. int i, j, len = 0;
  682. char *buf = smi->buf;
  683. len += snprintf(buf + len, sizeof(smi->buf) - len, "%-36s",
  684. "Counter");
  685. for (i = 0; i < smi->num_ports; i++) {
  686. char port_buf[10];
  687. snprintf(port_buf, sizeof(port_buf), "Port %d", i);
  688. len += snprintf(buf + len, sizeof(smi->buf) - len, " %12s",
  689. port_buf);
  690. }
  691. len += snprintf(buf + len, sizeof(smi->buf) - len, "\n");
  692. for (i = 0; i < smi->num_mib_counters; i++) {
  693. len += snprintf(buf + len, sizeof(smi->buf) - len, "%-36s ",
  694. smi->mib_counters[i].name);
  695. for (j = 0; j < smi->num_ports; j++) {
  696. unsigned long long counter = 0;
  697. if (!smi->ops->get_mib_counter(smi, i, j, &counter))
  698. len += snprintf(buf + len,
  699. sizeof(smi->buf) - len,
  700. "%12llu ", counter);
  701. else
  702. len += snprintf(buf + len,
  703. sizeof(smi->buf) - len,
  704. "%12s ", "error");
  705. }
  706. len += snprintf(buf + len, sizeof(smi->buf) - len, "\n");
  707. }
  708. return simple_read_from_buffer(user_buf, count, ppos, buf, len);
  709. }
  710. static const struct file_operations fops_rtl8366_regs = {
  711. .read = rtl8366_read_debugfs_reg,
  712. .write = rtl8366_write_debugfs_reg,
  713. .open = rtl8366_debugfs_open,
  714. .owner = THIS_MODULE
  715. };
  716. static const struct file_operations fops_rtl8366_vlan_mc = {
  717. .read = rtl8366_read_debugfs_vlan_mc,
  718. .open = rtl8366_debugfs_open,
  719. .owner = THIS_MODULE
  720. };
  721. static const struct file_operations fops_rtl8366_vlan_4k = {
  722. .read = rtl8366_read_debugfs_vlan_4k,
  723. .open = rtl8366_debugfs_open,
  724. .owner = THIS_MODULE
  725. };
  726. static const struct file_operations fops_rtl8366_pvid = {
  727. .read = rtl8366_read_debugfs_pvid,
  728. .open = rtl8366_debugfs_open,
  729. .owner = THIS_MODULE
  730. };
  731. static const struct file_operations fops_rtl8366_mibs = {
  732. .read = rtl8366_read_debugfs_mibs,
  733. .open = rtl8366_debugfs_open,
  734. .owner = THIS_MODULE
  735. };
  736. static void rtl8366_debugfs_init(struct rtl8366_smi *smi)
  737. {
  738. struct dentry *node;
  739. struct dentry *root;
  740. if (!smi->debugfs_root)
  741. smi->debugfs_root = debugfs_create_dir(dev_name(smi->parent),
  742. NULL);
  743. if (!smi->debugfs_root) {
  744. dev_err(smi->parent, "Unable to create debugfs dir\n");
  745. return;
  746. }
  747. root = smi->debugfs_root;
  748. node = debugfs_create_x16("reg", S_IRUGO | S_IWUSR, root,
  749. &smi->dbg_reg);
  750. if (!node) {
  751. dev_err(smi->parent, "Creating debugfs file '%s' failed\n",
  752. "reg");
  753. return;
  754. }
  755. node = debugfs_create_file("val", S_IRUGO | S_IWUSR, root, smi,
  756. &fops_rtl8366_regs);
  757. if (!node) {
  758. dev_err(smi->parent, "Creating debugfs file '%s' failed\n",
  759. "val");
  760. return;
  761. }
  762. node = debugfs_create_file("vlan_mc", S_IRUSR, root, smi,
  763. &fops_rtl8366_vlan_mc);
  764. if (!node) {
  765. dev_err(smi->parent, "Creating debugfs file '%s' failed\n",
  766. "vlan_mc");
  767. return;
  768. }
  769. node = debugfs_create_u8("vlan_4k_page", S_IRUGO | S_IWUSR, root,
  770. &smi->dbg_vlan_4k_page);
  771. if (!node) {
  772. dev_err(smi->parent, "Creating debugfs file '%s' failed\n",
  773. "vlan_4k_page");
  774. return;
  775. }
  776. node = debugfs_create_file("vlan_4k", S_IRUSR, root, smi,
  777. &fops_rtl8366_vlan_4k);
  778. if (!node) {
  779. dev_err(smi->parent, "Creating debugfs file '%s' failed\n",
  780. "vlan_4k");
  781. return;
  782. }
  783. node = debugfs_create_file("pvid", S_IRUSR, root, smi,
  784. &fops_rtl8366_pvid);
  785. if (!node) {
  786. dev_err(smi->parent, "Creating debugfs file '%s' failed\n",
  787. "pvid");
  788. return;
  789. }
  790. node = debugfs_create_file("mibs", S_IRUSR, smi->debugfs_root, smi,
  791. &fops_rtl8366_mibs);
  792. if (!node)
  793. dev_err(smi->parent, "Creating debugfs file '%s' failed\n",
  794. "mibs");
  795. }
  796. static void rtl8366_debugfs_remove(struct rtl8366_smi *smi)
  797. {
  798. if (smi->debugfs_root) {
  799. debugfs_remove_recursive(smi->debugfs_root);
  800. smi->debugfs_root = NULL;
  801. }
  802. }
  803. #else
  804. static inline void rtl8366_debugfs_init(struct rtl8366_smi *smi) {}
  805. static inline void rtl8366_debugfs_remove(struct rtl8366_smi *smi) {}
  806. #endif /* CONFIG_RTL8366_SMI_DEBUG_FS */
  807. static int rtl8366_smi_mii_init(struct rtl8366_smi *smi)
  808. {
  809. int ret;
  810. #ifdef CONFIG_OF
  811. struct device_node *np = NULL;
  812. np = of_get_child_by_name(smi->parent->of_node, "mdio-bus");
  813. #endif
  814. smi->mii_bus = mdiobus_alloc();
  815. if (smi->mii_bus == NULL) {
  816. ret = -ENOMEM;
  817. goto err;
  818. }
  819. smi->mii_bus->priv = (void *) smi;
  820. smi->mii_bus->name = dev_name(smi->parent);
  821. smi->mii_bus->read = smi->ops->mii_read;
  822. smi->mii_bus->write = smi->ops->mii_write;
  823. snprintf(smi->mii_bus->id, MII_BUS_ID_SIZE, "%s",
  824. dev_name(smi->parent));
  825. smi->mii_bus->parent = smi->parent;
  826. smi->mii_bus->phy_mask = ~(0x1f);
  827. #if LINUX_VERSION_CODE < KERNEL_VERSION(4,5,0)
  828. {
  829. int i;
  830. smi->mii_bus->irq = smi->mii_irq;
  831. for (i = 0; i < PHY_MAX_ADDR; i++)
  832. smi->mii_irq[i] = PHY_POLL;
  833. }
  834. #endif
  835. #ifdef CONFIG_OF
  836. if (np)
  837. ret = of_mdiobus_register(smi->mii_bus, np);
  838. else
  839. #endif
  840. ret = mdiobus_register(smi->mii_bus);
  841. if (ret)
  842. goto err_free;
  843. return 0;
  844. err_free:
  845. mdiobus_free(smi->mii_bus);
  846. err:
  847. return ret;
  848. }
  849. static void rtl8366_smi_mii_cleanup(struct rtl8366_smi *smi)
  850. {
  851. mdiobus_unregister(smi->mii_bus);
  852. mdiobus_free(smi->mii_bus);
  853. }
  854. int rtl8366_sw_reset_switch(struct switch_dev *dev)
  855. {
  856. struct rtl8366_smi *smi = sw_to_rtl8366_smi(dev);
  857. int err;
  858. err = rtl8366_reset(smi);
  859. if (err)
  860. return err;
  861. err = smi->ops->setup(smi);
  862. if (err)
  863. return err;
  864. err = rtl8366_reset_vlan(smi);
  865. if (err)
  866. return err;
  867. err = rtl8366_enable_vlan(smi, 1);
  868. if (err)
  869. return err;
  870. return rtl8366_enable_all_ports(smi, 1);
  871. }
  872. EXPORT_SYMBOL_GPL(rtl8366_sw_reset_switch);
  873. int rtl8366_sw_get_port_pvid(struct switch_dev *dev, int port, int *val)
  874. {
  875. struct rtl8366_smi *smi = sw_to_rtl8366_smi(dev);
  876. return rtl8366_get_pvid(smi, port, val);
  877. }
  878. EXPORT_SYMBOL_GPL(rtl8366_sw_get_port_pvid);
  879. int rtl8366_sw_set_port_pvid(struct switch_dev *dev, int port, int val)
  880. {
  881. struct rtl8366_smi *smi = sw_to_rtl8366_smi(dev);
  882. return rtl8366_set_pvid(smi, port, val);
  883. }
  884. EXPORT_SYMBOL_GPL(rtl8366_sw_set_port_pvid);
  885. int rtl8366_sw_get_port_mib(struct switch_dev *dev,
  886. const struct switch_attr *attr,
  887. struct switch_val *val)
  888. {
  889. struct rtl8366_smi *smi = sw_to_rtl8366_smi(dev);
  890. int i, len = 0;
  891. unsigned long long counter = 0;
  892. char *buf = smi->buf;
  893. if (val->port_vlan >= smi->num_ports)
  894. return -EINVAL;
  895. len += snprintf(buf + len, sizeof(smi->buf) - len,
  896. "Port %d MIB counters\n",
  897. val->port_vlan);
  898. for (i = 0; i < smi->num_mib_counters; ++i) {
  899. len += snprintf(buf + len, sizeof(smi->buf) - len,
  900. "%-36s: ", smi->mib_counters[i].name);
  901. if (!smi->ops->get_mib_counter(smi, i, val->port_vlan,
  902. &counter))
  903. len += snprintf(buf + len, sizeof(smi->buf) - len,
  904. "%llu\n", counter);
  905. else
  906. len += snprintf(buf + len, sizeof(smi->buf) - len,
  907. "%s\n", "error");
  908. }
  909. val->value.s = buf;
  910. val->len = len;
  911. return 0;
  912. }
  913. EXPORT_SYMBOL_GPL(rtl8366_sw_get_port_mib);
  914. int rtl8366_sw_get_port_stats(struct switch_dev *dev, int port,
  915. struct switch_port_stats *stats,
  916. int txb_id, int rxb_id)
  917. {
  918. struct rtl8366_smi *smi = sw_to_rtl8366_smi(dev);
  919. unsigned long long counter = 0;
  920. int ret;
  921. if (port >= smi->num_ports)
  922. return -EINVAL;
  923. ret = smi->ops->get_mib_counter(smi, txb_id, port, &counter);
  924. if (ret)
  925. return ret;
  926. stats->tx_bytes = counter;
  927. ret = smi->ops->get_mib_counter(smi, rxb_id, port, &counter);
  928. if (ret)
  929. return ret;
  930. stats->rx_bytes = counter;
  931. return 0;
  932. }
  933. EXPORT_SYMBOL_GPL(rtl8366_sw_get_port_stats);
  934. int rtl8366_sw_get_vlan_info(struct switch_dev *dev,
  935. const struct switch_attr *attr,
  936. struct switch_val *val)
  937. {
  938. int i;
  939. u32 len = 0;
  940. struct rtl8366_vlan_4k vlan4k;
  941. struct rtl8366_smi *smi = sw_to_rtl8366_smi(dev);
  942. char *buf = smi->buf;
  943. int err;
  944. if (!smi->ops->is_vlan_valid(smi, val->port_vlan))
  945. return -EINVAL;
  946. memset(buf, '\0', sizeof(smi->buf));
  947. err = smi->ops->get_vlan_4k(smi, val->port_vlan, &vlan4k);
  948. if (err)
  949. return err;
  950. len += snprintf(buf + len, sizeof(smi->buf) - len,
  951. "VLAN %d: Ports: '", vlan4k.vid);
  952. for (i = 0; i < smi->num_ports; i++) {
  953. if (!(vlan4k.member & (1 << i)))
  954. continue;
  955. len += snprintf(buf + len, sizeof(smi->buf) - len, "%d%s", i,
  956. (vlan4k.untag & (1 << i)) ? "" : "t");
  957. }
  958. len += snprintf(buf + len, sizeof(smi->buf) - len,
  959. "', members=%04x, untag=%04x, fid=%u",
  960. vlan4k.member, vlan4k.untag, vlan4k.fid);
  961. val->value.s = buf;
  962. val->len = len;
  963. return 0;
  964. }
  965. EXPORT_SYMBOL_GPL(rtl8366_sw_get_vlan_info);
  966. int rtl8366_sw_get_vlan_ports(struct switch_dev *dev, struct switch_val *val)
  967. {
  968. struct rtl8366_smi *smi = sw_to_rtl8366_smi(dev);
  969. struct switch_port *port;
  970. struct rtl8366_vlan_4k vlan4k;
  971. int i;
  972. if (!smi->ops->is_vlan_valid(smi, val->port_vlan))
  973. return -EINVAL;
  974. smi->ops->get_vlan_4k(smi, val->port_vlan, &vlan4k);
  975. port = &val->value.ports[0];
  976. val->len = 0;
  977. for (i = 0; i < smi->num_ports; i++) {
  978. if (!(vlan4k.member & BIT(i)))
  979. continue;
  980. port->id = i;
  981. port->flags = (vlan4k.untag & BIT(i)) ?
  982. 0 : BIT(SWITCH_PORT_FLAG_TAGGED);
  983. val->len++;
  984. port++;
  985. }
  986. return 0;
  987. }
  988. EXPORT_SYMBOL_GPL(rtl8366_sw_get_vlan_ports);
  989. int rtl8366_sw_set_vlan_ports(struct switch_dev *dev, struct switch_val *val)
  990. {
  991. struct rtl8366_smi *smi = sw_to_rtl8366_smi(dev);
  992. struct switch_port *port;
  993. u32 member = 0;
  994. u32 untag = 0;
  995. int err;
  996. int i;
  997. if (!smi->ops->is_vlan_valid(smi, val->port_vlan))
  998. return -EINVAL;
  999. port = &val->value.ports[0];
  1000. for (i = 0; i < val->len; i++, port++) {
  1001. int pvid = 0;
  1002. member |= BIT(port->id);
  1003. if (!(port->flags & BIT(SWITCH_PORT_FLAG_TAGGED)))
  1004. untag |= BIT(port->id);
  1005. /*
  1006. * To ensure that we have a valid MC entry for this VLAN,
  1007. * initialize the port VLAN ID here.
  1008. */
  1009. err = rtl8366_get_pvid(smi, port->id, &pvid);
  1010. if (err < 0)
  1011. return err;
  1012. if (pvid == 0) {
  1013. err = rtl8366_set_pvid(smi, port->id, val->port_vlan);
  1014. if (err < 0)
  1015. return err;
  1016. }
  1017. }
  1018. return rtl8366_set_vlan(smi, val->port_vlan, member, untag, 0);
  1019. }
  1020. EXPORT_SYMBOL_GPL(rtl8366_sw_set_vlan_ports);
  1021. int rtl8366_sw_get_vlan_fid(struct switch_dev *dev,
  1022. const struct switch_attr *attr,
  1023. struct switch_val *val)
  1024. {
  1025. struct rtl8366_vlan_4k vlan4k;
  1026. struct rtl8366_smi *smi = sw_to_rtl8366_smi(dev);
  1027. int err;
  1028. if (!smi->ops->is_vlan_valid(smi, val->port_vlan))
  1029. return -EINVAL;
  1030. err = smi->ops->get_vlan_4k(smi, val->port_vlan, &vlan4k);
  1031. if (err)
  1032. return err;
  1033. val->value.i = vlan4k.fid;
  1034. return 0;
  1035. }
  1036. EXPORT_SYMBOL_GPL(rtl8366_sw_get_vlan_fid);
  1037. int rtl8366_sw_set_vlan_fid(struct switch_dev *dev,
  1038. const struct switch_attr *attr,
  1039. struct switch_val *val)
  1040. {
  1041. struct rtl8366_vlan_4k vlan4k;
  1042. struct rtl8366_smi *smi = sw_to_rtl8366_smi(dev);
  1043. int err;
  1044. if (!smi->ops->is_vlan_valid(smi, val->port_vlan))
  1045. return -EINVAL;
  1046. if (val->value.i < 0 || val->value.i > attr->max)
  1047. return -EINVAL;
  1048. err = smi->ops->get_vlan_4k(smi, val->port_vlan, &vlan4k);
  1049. if (err)
  1050. return err;
  1051. return rtl8366_set_vlan(smi, val->port_vlan,
  1052. vlan4k.member,
  1053. vlan4k.untag,
  1054. val->value.i);
  1055. }
  1056. EXPORT_SYMBOL_GPL(rtl8366_sw_set_vlan_fid);
  1057. int rtl8366_sw_get_vlan_enable(struct switch_dev *dev,
  1058. const struct switch_attr *attr,
  1059. struct switch_val *val)
  1060. {
  1061. struct rtl8366_smi *smi = sw_to_rtl8366_smi(dev);
  1062. if (attr->ofs > 2)
  1063. return -EINVAL;
  1064. if (attr->ofs == 1)
  1065. val->value.i = smi->vlan_enabled;
  1066. else
  1067. val->value.i = smi->vlan4k_enabled;
  1068. return 0;
  1069. }
  1070. EXPORT_SYMBOL_GPL(rtl8366_sw_get_vlan_enable);
  1071. int rtl8366_sw_set_vlan_enable(struct switch_dev *dev,
  1072. const struct switch_attr *attr,
  1073. struct switch_val *val)
  1074. {
  1075. struct rtl8366_smi *smi = sw_to_rtl8366_smi(dev);
  1076. int err;
  1077. if (attr->ofs > 2)
  1078. return -EINVAL;
  1079. if (attr->ofs == 1)
  1080. err = rtl8366_enable_vlan(smi, val->value.i);
  1081. else
  1082. err = rtl8366_enable_vlan4k(smi, val->value.i);
  1083. return err;
  1084. }
  1085. EXPORT_SYMBOL_GPL(rtl8366_sw_set_vlan_enable);
  1086. struct rtl8366_smi *rtl8366_smi_alloc(struct device *parent)
  1087. {
  1088. struct rtl8366_smi *smi;
  1089. BUG_ON(!parent);
  1090. smi = kzalloc(sizeof(*smi), GFP_KERNEL);
  1091. if (!smi) {
  1092. dev_err(parent, "no memory for private data\n");
  1093. return NULL;
  1094. }
  1095. smi->parent = parent;
  1096. return smi;
  1097. }
  1098. EXPORT_SYMBOL_GPL(rtl8366_smi_alloc);
  1099. static int __rtl8366_smi_init(struct rtl8366_smi *smi, const char *name)
  1100. {
  1101. int err;
  1102. if (!smi->ext_mbus) {
  1103. err = gpio_request(smi->gpio_sda, name);
  1104. if (err) {
  1105. printk(KERN_ERR "rtl8366_smi: gpio_request failed for %u, err=%d\n",
  1106. smi->gpio_sda, err);
  1107. goto err_out;
  1108. }
  1109. err = gpio_request(smi->gpio_sck, name);
  1110. if (err) {
  1111. printk(KERN_ERR "rtl8366_smi: gpio_request failed for %u, err=%d\n",
  1112. smi->gpio_sck, err);
  1113. goto err_free_sda;
  1114. }
  1115. }
  1116. spin_lock_init(&smi->lock);
  1117. /* start the switch */
  1118. if (smi->hw_reset) {
  1119. smi->hw_reset(smi, false);
  1120. msleep(RTL8366_SMI_HW_START_DELAY);
  1121. }
  1122. return 0;
  1123. err_free_sda:
  1124. gpio_free(smi->gpio_sda);
  1125. err_out:
  1126. return err;
  1127. }
  1128. static void __rtl8366_smi_cleanup(struct rtl8366_smi *smi)
  1129. {
  1130. if (smi->hw_reset)
  1131. smi->hw_reset(smi, true);
  1132. if (!smi->ext_mbus) {
  1133. gpio_free(smi->gpio_sck);
  1134. gpio_free(smi->gpio_sda);
  1135. }
  1136. }
  1137. enum rtl8366_type rtl8366_smi_detect(struct rtl8366_platform_data *pdata)
  1138. {
  1139. static struct rtl8366_smi smi;
  1140. enum rtl8366_type type = RTL8366_TYPE_UNKNOWN;
  1141. u32 reg = 0;
  1142. memset(&smi, 0, sizeof(smi));
  1143. smi.gpio_sda = pdata->gpio_sda;
  1144. smi.gpio_sck = pdata->gpio_sck;
  1145. smi.clk_delay = 10;
  1146. smi.cmd_read = 0xa9;
  1147. smi.cmd_write = 0xa8;
  1148. if (__rtl8366_smi_init(&smi, "rtl8366"))
  1149. goto out;
  1150. if (rtl8366_smi_read_reg(&smi, 0x5c, &reg))
  1151. goto cleanup;
  1152. switch(reg) {
  1153. case 0x6027:
  1154. printk("Found an RTL8366S switch\n");
  1155. type = RTL8366_TYPE_S;
  1156. break;
  1157. case 0x5937:
  1158. printk("Found an RTL8366RB switch\n");
  1159. type = RTL8366_TYPE_RB;
  1160. break;
  1161. default:
  1162. printk("Found an Unknown RTL8366 switch (id=0x%04x)\n", reg);
  1163. break;
  1164. }
  1165. cleanup:
  1166. __rtl8366_smi_cleanup(&smi);
  1167. out:
  1168. return type;
  1169. }
  1170. int rtl8366_smi_init(struct rtl8366_smi *smi)
  1171. {
  1172. int err;
  1173. if (!smi->ops)
  1174. return -EINVAL;
  1175. err = __rtl8366_smi_init(smi, dev_name(smi->parent));
  1176. if (err)
  1177. goto err_out;
  1178. if (!smi->ext_mbus)
  1179. dev_info(smi->parent, "using GPIO pins %u (SDA) and %u (SCK)\n",
  1180. smi->gpio_sda, smi->gpio_sck);
  1181. else
  1182. dev_info(smi->parent, "using MDIO bus '%s'\n", smi->ext_mbus->name);
  1183. err = smi->ops->detect(smi);
  1184. if (err) {
  1185. dev_err(smi->parent, "chip detection failed, err=%d\n", err);
  1186. goto err_free_sck;
  1187. }
  1188. err = rtl8366_reset(smi);
  1189. if (err)
  1190. goto err_free_sck;
  1191. err = smi->ops->setup(smi);
  1192. if (err) {
  1193. dev_err(smi->parent, "chip setup failed, err=%d\n", err);
  1194. goto err_free_sck;
  1195. }
  1196. err = rtl8366_init_vlan(smi);
  1197. if (err) {
  1198. dev_err(smi->parent, "VLAN initialization failed, err=%d\n",
  1199. err);
  1200. goto err_free_sck;
  1201. }
  1202. err = rtl8366_enable_all_ports(smi, 1);
  1203. if (err)
  1204. goto err_free_sck;
  1205. err = rtl8366_smi_mii_init(smi);
  1206. if (err)
  1207. goto err_free_sck;
  1208. rtl8366_debugfs_init(smi);
  1209. return 0;
  1210. err_free_sck:
  1211. __rtl8366_smi_cleanup(smi);
  1212. err_out:
  1213. return err;
  1214. }
  1215. EXPORT_SYMBOL_GPL(rtl8366_smi_init);
  1216. void rtl8366_smi_cleanup(struct rtl8366_smi *smi)
  1217. {
  1218. rtl8366_debugfs_remove(smi);
  1219. rtl8366_smi_mii_cleanup(smi);
  1220. __rtl8366_smi_cleanup(smi);
  1221. }
  1222. EXPORT_SYMBOL_GPL(rtl8366_smi_cleanup);
  1223. #ifdef CONFIG_OF
  1224. static void rtl8366_smi_reset(struct rtl8366_smi *smi, bool active)
  1225. {
  1226. if (active)
  1227. reset_control_assert(smi->reset);
  1228. else
  1229. reset_control_deassert(smi->reset);
  1230. }
  1231. int rtl8366_smi_probe_of(struct platform_device *pdev, struct rtl8366_smi *smi)
  1232. {
  1233. int sck = of_get_named_gpio(pdev->dev.of_node, "gpio-sck", 0);
  1234. int sda = of_get_named_gpio(pdev->dev.of_node, "gpio-sda", 0);
  1235. struct device_node *np = pdev->dev.of_node;
  1236. struct device_node *mdio_node;
  1237. mdio_node = of_parse_phandle(np, "mii-bus", 0);
  1238. if (!mdio_node) {
  1239. dev_err(&pdev->dev, "cannot find mdio node phandle");
  1240. goto try_gpio;
  1241. }
  1242. smi->ext_mbus = of_mdio_find_bus(mdio_node);
  1243. if (!smi->ext_mbus) {
  1244. dev_info(&pdev->dev,
  1245. "cannot find mdio bus from bus handle (yet)");
  1246. goto try_gpio;
  1247. }
  1248. return 0;
  1249. try_gpio:
  1250. if (!gpio_is_valid(sck) || !gpio_is_valid(sda)) {
  1251. if (!mdio_node) {
  1252. dev_err(&pdev->dev, "gpios missing in devictree\n");
  1253. return -EINVAL;
  1254. } else {
  1255. return -EPROBE_DEFER;
  1256. }
  1257. }
  1258. smi->gpio_sda = sda;
  1259. smi->gpio_sck = sck;
  1260. smi->reset = devm_reset_control_get(&pdev->dev, "switch");
  1261. if (!IS_ERR(smi->reset))
  1262. smi->hw_reset = rtl8366_smi_reset;
  1263. return 0;
  1264. }
  1265. #else
  1266. static inline int rtl8366_smi_probe_of(struct platform_device *pdev, struct rtl8366_smi *smi)
  1267. {
  1268. return -ENODEV;
  1269. }
  1270. #endif
  1271. int rtl8366_smi_probe_plat(struct platform_device *pdev, struct rtl8366_smi *smi)
  1272. {
  1273. struct rtl8366_platform_data *pdata = pdev->dev.platform_data;
  1274. if (!pdev->dev.platform_data) {
  1275. dev_err(&pdev->dev, "no platform data specified\n");
  1276. return -EINVAL;
  1277. }
  1278. smi->gpio_sda = pdata->gpio_sda;
  1279. smi->gpio_sck = pdata->gpio_sck;
  1280. smi->hw_reset = pdata->hw_reset;
  1281. return 0;
  1282. }
  1283. struct rtl8366_smi *rtl8366_smi_probe(struct platform_device *pdev)
  1284. {
  1285. struct rtl8366_smi *smi;
  1286. int err;
  1287. smi = rtl8366_smi_alloc(&pdev->dev);
  1288. if (!smi)
  1289. return NULL;
  1290. if (pdev->dev.of_node)
  1291. err = rtl8366_smi_probe_of(pdev, smi);
  1292. else
  1293. err = rtl8366_smi_probe_plat(pdev, smi);
  1294. if (err)
  1295. goto free_smi;
  1296. return smi;
  1297. free_smi:
  1298. kfree(smi);
  1299. return ERR_PTR(err);
  1300. }
  1301. EXPORT_SYMBOL_GPL(rtl8366_smi_probe);
  1302. MODULE_DESCRIPTION("Realtek RTL8366 SMI interface driver");
  1303. MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org>");
  1304. MODULE_LICENSE("GPL v2");