rtl8366_smi.c 34 KB

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