2
0

rtl8366_smi.c 31 KB

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