1
0

rtl8366rb.c 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532
  1. /*
  2. * Platform driver for the Realtek RTL8366RB ethernet switch
  3. *
  4. * Copyright (C) 2009-2010 Gabor Juhos <juhosg@openwrt.org>
  5. * Copyright (C) 2010 Antti Seppälä <a.seppala@gmail.com>
  6. * Copyright (C) 2010 Roman Yeryomin <roman@advem.lv>
  7. * Copyright (C) 2011 Colin Leitner <colin.leitner@googlemail.com>
  8. *
  9. * This program is free software; you can redistribute it and/or modify it
  10. * under the terms of the GNU General Public License version 2 as published
  11. * by the Free Software Foundation.
  12. */
  13. #include <linux/kernel.h>
  14. #include <linux/module.h>
  15. #include <linux/init.h>
  16. #include <linux/device.h>
  17. #include <linux/of.h>
  18. #include <linux/of_platform.h>
  19. #include <linux/delay.h>
  20. #include <linux/skbuff.h>
  21. #include <linux/rtl8366.h>
  22. #include "rtl8366_smi.h"
  23. #define RTL8366RB_DRIVER_DESC "Realtek RTL8366RB ethernet switch driver"
  24. #define RTL8366RB_DRIVER_VER "0.2.4"
  25. #define RTL8366RB_PHY_NO_MAX 4
  26. #define RTL8366RB_PHY_PAGE_MAX 7
  27. #define RTL8366RB_PHY_ADDR_MAX 31
  28. /* Switch Global Configuration register */
  29. #define RTL8366RB_SGCR 0x0000
  30. #define RTL8366RB_SGCR_EN_BC_STORM_CTRL BIT(0)
  31. #define RTL8366RB_SGCR_MAX_LENGTH(_x) (_x << 4)
  32. #define RTL8366RB_SGCR_MAX_LENGTH_MASK RTL8366RB_SGCR_MAX_LENGTH(0x3)
  33. #define RTL8366RB_SGCR_MAX_LENGTH_1522 RTL8366RB_SGCR_MAX_LENGTH(0x0)
  34. #define RTL8366RB_SGCR_MAX_LENGTH_1536 RTL8366RB_SGCR_MAX_LENGTH(0x1)
  35. #define RTL8366RB_SGCR_MAX_LENGTH_1552 RTL8366RB_SGCR_MAX_LENGTH(0x2)
  36. #define RTL8366RB_SGCR_MAX_LENGTH_9216 RTL8366RB_SGCR_MAX_LENGTH(0x3)
  37. #define RTL8366RB_SGCR_EN_VLAN BIT(13)
  38. #define RTL8366RB_SGCR_EN_VLAN_4KTB BIT(14)
  39. /* Port Enable Control register */
  40. #define RTL8366RB_PECR 0x0001
  41. /* Port Mirror Control Register */
  42. #define RTL8366RB_PMCR 0x0007
  43. #define RTL8366RB_PMCR_SOURCE_PORT(_x) (_x)
  44. #define RTL8366RB_PMCR_SOURCE_PORT_MASK 0x000f
  45. #define RTL8366RB_PMCR_MONITOR_PORT(_x) ((_x) << 4)
  46. #define RTL8366RB_PMCR_MONITOR_PORT_MASK 0x00f0
  47. #define RTL8366RB_PMCR_MIRROR_RX BIT(8)
  48. #define RTL8366RB_PMCR_MIRROR_TX BIT(9)
  49. #define RTL8366RB_PMCR_MIRROR_SPC BIT(10)
  50. #define RTL8366RB_PMCR_MIRROR_ISO BIT(11)
  51. /* Switch Security Control registers */
  52. #define RTL8366RB_SSCR0 0x0002
  53. #define RTL8366RB_SSCR1 0x0003
  54. #define RTL8366RB_SSCR2 0x0004
  55. #define RTL8366RB_SSCR2_DROP_UNKNOWN_DA BIT(0)
  56. #define RTL8366RB_RESET_CTRL_REG 0x0100
  57. #define RTL8366RB_CHIP_CTRL_RESET_HW 1
  58. #define RTL8366RB_CHIP_CTRL_RESET_SW (1 << 1)
  59. #define RTL8366RB_CHIP_VERSION_CTRL_REG 0x050A
  60. #define RTL8366RB_CHIP_VERSION_MASK 0xf
  61. #define RTL8366RB_CHIP_ID_REG 0x0509
  62. #define RTL8366RB_CHIP_ID_8366 0x5937
  63. /* PHY registers control */
  64. #define RTL8366RB_PHY_ACCESS_CTRL_REG 0x8000
  65. #define RTL8366RB_PHY_ACCESS_DATA_REG 0x8002
  66. #define RTL8366RB_PHY_CTRL_READ 1
  67. #define RTL8366RB_PHY_CTRL_WRITE 0
  68. #define RTL8366RB_PHY_REG_MASK 0x1f
  69. #define RTL8366RB_PHY_PAGE_OFFSET 5
  70. #define RTL8366RB_PHY_PAGE_MASK (0xf << 5)
  71. #define RTL8366RB_PHY_NO_OFFSET 9
  72. #define RTL8366RB_PHY_NO_MASK (0x1f << 9)
  73. #define RTL8366RB_VLAN_INGRESS_CTRL2_REG 0x037f
  74. /* LED control registers */
  75. #define RTL8366RB_LED_BLINKRATE_REG 0x0430
  76. #define RTL8366RB_LED_BLINKRATE_BIT 0
  77. #define RTL8366RB_LED_BLINKRATE_MASK 0x0007
  78. #define RTL8366RB_LED_CTRL_REG 0x0431
  79. #define RTL8366RB_LED_0_1_CTRL_REG 0x0432
  80. #define RTL8366RB_LED_2_3_CTRL_REG 0x0433
  81. #define RTL8366RB_MIB_COUNT 33
  82. #define RTL8366RB_GLOBAL_MIB_COUNT 1
  83. #define RTL8366RB_MIB_COUNTER_PORT_OFFSET 0x0050
  84. #define RTL8366RB_MIB_COUNTER_BASE 0x1000
  85. #define RTL8366RB_MIB_CTRL_REG 0x13F0
  86. #define RTL8366RB_MIB_CTRL_USER_MASK 0x0FFC
  87. #define RTL8366RB_MIB_CTRL_BUSY_MASK BIT(0)
  88. #define RTL8366RB_MIB_CTRL_RESET_MASK BIT(1)
  89. #define RTL8366RB_MIB_CTRL_PORT_RESET(_p) BIT(2 + (_p))
  90. #define RTL8366RB_MIB_CTRL_GLOBAL_RESET BIT(11)
  91. #define RTL8366RB_PORT_VLAN_CTRL_BASE 0x0063
  92. #define RTL8366RB_PORT_VLAN_CTRL_REG(_p) \
  93. (RTL8366RB_PORT_VLAN_CTRL_BASE + (_p) / 4)
  94. #define RTL8366RB_PORT_VLAN_CTRL_MASK 0xf
  95. #define RTL8366RB_PORT_VLAN_CTRL_SHIFT(_p) (4 * ((_p) % 4))
  96. #define RTL8366RB_VLAN_TABLE_READ_BASE 0x018C
  97. #define RTL8366RB_VLAN_TABLE_WRITE_BASE 0x0185
  98. #define RTL8366RB_TABLE_ACCESS_CTRL_REG 0x0180
  99. #define RTL8366RB_TABLE_VLAN_READ_CTRL 0x0E01
  100. #define RTL8366RB_TABLE_VLAN_WRITE_CTRL 0x0F01
  101. #define RTL8366RB_VLAN_MC_BASE(_x) (0x0020 + (_x) * 3)
  102. #define RTL8366RB_PORT_LINK_STATUS_BASE 0x0014
  103. #define RTL8366RB_PORT_STATUS_SPEED_MASK 0x0003
  104. #define RTL8366RB_PORT_STATUS_DUPLEX_MASK 0x0004
  105. #define RTL8366RB_PORT_STATUS_LINK_MASK 0x0010
  106. #define RTL8366RB_PORT_STATUS_TXPAUSE_MASK 0x0020
  107. #define RTL8366RB_PORT_STATUS_RXPAUSE_MASK 0x0040
  108. #define RTL8366RB_PORT_STATUS_AN_MASK 0x0080
  109. #define RTL8366RB_PORT_NUM_CPU 5
  110. #define RTL8366RB_NUM_PORTS 6
  111. #define RTL8366RB_NUM_VLANS 16
  112. #define RTL8366RB_NUM_LEDGROUPS 4
  113. #define RTL8366RB_NUM_VIDS 4096
  114. #define RTL8366RB_PRIORITYMAX 7
  115. #define RTL8366RB_FIDMAX 7
  116. #define RTL8366RB_PORT_1 (1 << 0) /* In userspace port 0 */
  117. #define RTL8366RB_PORT_2 (1 << 1) /* In userspace port 1 */
  118. #define RTL8366RB_PORT_3 (1 << 2) /* In userspace port 2 */
  119. #define RTL8366RB_PORT_4 (1 << 3) /* In userspace port 3 */
  120. #define RTL8366RB_PORT_5 (1 << 4) /* In userspace port 4 */
  121. #define RTL8366RB_PORT_CPU (1 << 5) /* CPU port */
  122. #define RTL8366RB_PORT_ALL (RTL8366RB_PORT_1 | \
  123. RTL8366RB_PORT_2 | \
  124. RTL8366RB_PORT_3 | \
  125. RTL8366RB_PORT_4 | \
  126. RTL8366RB_PORT_5 | \
  127. RTL8366RB_PORT_CPU)
  128. #define RTL8366RB_PORT_ALL_BUT_CPU (RTL8366RB_PORT_1 | \
  129. RTL8366RB_PORT_2 | \
  130. RTL8366RB_PORT_3 | \
  131. RTL8366RB_PORT_4 | \
  132. RTL8366RB_PORT_5)
  133. #define RTL8366RB_PORT_ALL_EXTERNAL (RTL8366RB_PORT_1 | \
  134. RTL8366RB_PORT_2 | \
  135. RTL8366RB_PORT_3 | \
  136. RTL8366RB_PORT_4)
  137. #define RTL8366RB_PORT_ALL_INTERNAL RTL8366RB_PORT_CPU
  138. #define RTL8366RB_VLAN_VID_MASK 0xfff
  139. #define RTL8366RB_VLAN_PRIORITY_SHIFT 12
  140. #define RTL8366RB_VLAN_PRIORITY_MASK 0x7
  141. #define RTL8366RB_VLAN_UNTAG_SHIFT 8
  142. #define RTL8366RB_VLAN_UNTAG_MASK 0xff
  143. #define RTL8366RB_VLAN_MEMBER_MASK 0xff
  144. #define RTL8366RB_VLAN_FID_MASK 0x7
  145. /* Port ingress bandwidth control */
  146. #define RTL8366RB_IB_BASE 0x0200
  147. #define RTL8366RB_IB_REG(pnum) (RTL8366RB_IB_BASE + pnum)
  148. #define RTL8366RB_IB_BDTH_MASK 0x3fff
  149. #define RTL8366RB_IB_PREIFG_OFFSET 14
  150. #define RTL8366RB_IB_PREIFG_MASK (1 << RTL8366RB_IB_PREIFG_OFFSET)
  151. /* Port egress bandwidth control */
  152. #define RTL8366RB_EB_BASE 0x02d1
  153. #define RTL8366RB_EB_REG(pnum) (RTL8366RB_EB_BASE + pnum)
  154. #define RTL8366RB_EB_BDTH_MASK 0x3fff
  155. #define RTL8366RB_EB_PREIFG_REG 0x02f8
  156. #define RTL8366RB_EB_PREIFG_OFFSET 9
  157. #define RTL8366RB_EB_PREIFG_MASK (1 << RTL8366RB_EB_PREIFG_OFFSET)
  158. #define RTL8366RB_BDTH_SW_MAX 1048512
  159. #define RTL8366RB_BDTH_UNIT 64
  160. #define RTL8366RB_BDTH_REG_DEFAULT 16383
  161. /* QOS */
  162. #define RTL8366RB_QOS_BIT 15
  163. #define RTL8366RB_QOS_MASK (1 << RTL8366RB_QOS_BIT)
  164. /* Include/Exclude Preamble and IFG (20 bytes). 0:Exclude, 1:Include. */
  165. #define RTL8366RB_QOS_DEFAULT_PREIFG 1
  166. #define RTL8366RB_MIB_RXB_ID 0 /* IfInOctets */
  167. #define RTL8366RB_MIB_TXB_ID 20 /* IfOutOctets */
  168. static struct rtl8366_mib_counter rtl8366rb_mib_counters[] = {
  169. { 0, 0, 4, "IfInOctets" },
  170. { 0, 4, 4, "EtherStatsOctets" },
  171. { 0, 8, 2, "EtherStatsUnderSizePkts" },
  172. { 0, 10, 2, "EtherFragments" },
  173. { 0, 12, 2, "EtherStatsPkts64Octets" },
  174. { 0, 14, 2, "EtherStatsPkts65to127Octets" },
  175. { 0, 16, 2, "EtherStatsPkts128to255Octets" },
  176. { 0, 18, 2, "EtherStatsPkts256to511Octets" },
  177. { 0, 20, 2, "EtherStatsPkts512to1023Octets" },
  178. { 0, 22, 2, "EtherStatsPkts1024to1518Octets" },
  179. { 0, 24, 2, "EtherOversizeStats" },
  180. { 0, 26, 2, "EtherStatsJabbers" },
  181. { 0, 28, 2, "IfInUcastPkts" },
  182. { 0, 30, 2, "EtherStatsMulticastPkts" },
  183. { 0, 32, 2, "EtherStatsBroadcastPkts" },
  184. { 0, 34, 2, "EtherStatsDropEvents" },
  185. { 0, 36, 2, "Dot3StatsFCSErrors" },
  186. { 0, 38, 2, "Dot3StatsSymbolErrors" },
  187. { 0, 40, 2, "Dot3InPauseFrames" },
  188. { 0, 42, 2, "Dot3ControlInUnknownOpcodes" },
  189. { 0, 44, 4, "IfOutOctets" },
  190. { 0, 48, 2, "Dot3StatsSingleCollisionFrames" },
  191. { 0, 50, 2, "Dot3StatMultipleCollisionFrames" },
  192. { 0, 52, 2, "Dot3sDeferredTransmissions" },
  193. { 0, 54, 2, "Dot3StatsLateCollisions" },
  194. { 0, 56, 2, "EtherStatsCollisions" },
  195. { 0, 58, 2, "Dot3StatsExcessiveCollisions" },
  196. { 0, 60, 2, "Dot3OutPauseFrames" },
  197. { 0, 62, 2, "Dot1dBasePortDelayExceededDiscards" },
  198. { 0, 64, 2, "Dot1dTpPortInDiscards" },
  199. { 0, 66, 2, "IfOutUcastPkts" },
  200. { 0, 68, 2, "IfOutMulticastPkts" },
  201. { 0, 70, 2, "IfOutBroadcastPkts" },
  202. };
  203. #define REG_WR(_smi, _reg, _val) \
  204. do { \
  205. err = rtl8366_smi_write_reg(_smi, _reg, _val); \
  206. if (err) \
  207. return err; \
  208. } while (0)
  209. #define REG_RMW(_smi, _reg, _mask, _val) \
  210. do { \
  211. err = rtl8366_smi_rmwr(_smi, _reg, _mask, _val); \
  212. if (err) \
  213. return err; \
  214. } while (0)
  215. static int rtl8366rb_reset_chip(struct rtl8366_smi *smi)
  216. {
  217. int timeout = 10;
  218. u32 data;
  219. rtl8366_smi_write_reg_noack(smi, RTL8366RB_RESET_CTRL_REG,
  220. RTL8366RB_CHIP_CTRL_RESET_HW);
  221. do {
  222. msleep(1);
  223. if (rtl8366_smi_read_reg(smi, RTL8366RB_RESET_CTRL_REG, &data))
  224. return -EIO;
  225. if (!(data & RTL8366RB_CHIP_CTRL_RESET_HW))
  226. break;
  227. } while (--timeout);
  228. if (!timeout) {
  229. printk("Timeout waiting for the switch to reset\n");
  230. return -EIO;
  231. }
  232. return 0;
  233. }
  234. static int rtl8366rb_setup(struct rtl8366_smi *smi)
  235. {
  236. int err;
  237. #ifdef CONFIG_OF
  238. unsigned i;
  239. struct device_node *np;
  240. unsigned num_initvals;
  241. const __be32 *paddr;
  242. np = smi->parent->of_node;
  243. paddr = of_get_property(np, "realtek,initvals", &num_initvals);
  244. if (paddr) {
  245. dev_info(smi->parent, "applying initvals from DTS\n");
  246. if (num_initvals < (2 * sizeof(*paddr)))
  247. return -EINVAL;
  248. num_initvals /= sizeof(*paddr);
  249. for (i = 0; i < num_initvals - 1; i += 2) {
  250. u32 reg = be32_to_cpup(paddr + i);
  251. u32 val = be32_to_cpup(paddr + i + 1);
  252. REG_WR(smi, reg, val);
  253. }
  254. }
  255. #endif
  256. /* set maximum packet length to 1536 bytes */
  257. REG_RMW(smi, RTL8366RB_SGCR, RTL8366RB_SGCR_MAX_LENGTH_MASK,
  258. RTL8366RB_SGCR_MAX_LENGTH_1536);
  259. /* enable learning for all ports */
  260. REG_WR(smi, RTL8366RB_SSCR0, 0);
  261. /* enable auto ageing for all ports */
  262. REG_WR(smi, RTL8366RB_SSCR1, 0);
  263. /*
  264. * discard VLAN tagged packets if the port is not a member of
  265. * the VLAN with which the packets is associated.
  266. */
  267. REG_WR(smi, RTL8366RB_VLAN_INGRESS_CTRL2_REG, RTL8366RB_PORT_ALL);
  268. /* don't drop packets whose DA has not been learned */
  269. REG_RMW(smi, RTL8366RB_SSCR2, RTL8366RB_SSCR2_DROP_UNKNOWN_DA, 0);
  270. return 0;
  271. }
  272. static int rtl8366rb_read_phy_reg(struct rtl8366_smi *smi,
  273. u32 phy_no, u32 page, u32 addr, u32 *data)
  274. {
  275. u32 reg;
  276. int ret;
  277. if (phy_no > RTL8366RB_PHY_NO_MAX)
  278. return -EINVAL;
  279. if (page > RTL8366RB_PHY_PAGE_MAX)
  280. return -EINVAL;
  281. if (addr > RTL8366RB_PHY_ADDR_MAX)
  282. return -EINVAL;
  283. ret = rtl8366_smi_write_reg(smi, RTL8366RB_PHY_ACCESS_CTRL_REG,
  284. RTL8366RB_PHY_CTRL_READ);
  285. if (ret)
  286. return ret;
  287. reg = 0x8000 | (1 << (phy_no + RTL8366RB_PHY_NO_OFFSET)) |
  288. ((page << RTL8366RB_PHY_PAGE_OFFSET) & RTL8366RB_PHY_PAGE_MASK) |
  289. (addr & RTL8366RB_PHY_REG_MASK);
  290. ret = rtl8366_smi_write_reg(smi, reg, 0);
  291. if (ret)
  292. return ret;
  293. ret = rtl8366_smi_read_reg(smi, RTL8366RB_PHY_ACCESS_DATA_REG, data);
  294. if (ret)
  295. return ret;
  296. return 0;
  297. }
  298. static int rtl8366rb_write_phy_reg(struct rtl8366_smi *smi,
  299. u32 phy_no, u32 page, u32 addr, u32 data)
  300. {
  301. u32 reg;
  302. int ret;
  303. if (phy_no > RTL8366RB_PHY_NO_MAX)
  304. return -EINVAL;
  305. if (page > RTL8366RB_PHY_PAGE_MAX)
  306. return -EINVAL;
  307. if (addr > RTL8366RB_PHY_ADDR_MAX)
  308. return -EINVAL;
  309. ret = rtl8366_smi_write_reg(smi, RTL8366RB_PHY_ACCESS_CTRL_REG,
  310. RTL8366RB_PHY_CTRL_WRITE);
  311. if (ret)
  312. return ret;
  313. reg = 0x8000 | (1 << (phy_no + RTL8366RB_PHY_NO_OFFSET)) |
  314. ((page << RTL8366RB_PHY_PAGE_OFFSET) & RTL8366RB_PHY_PAGE_MASK) |
  315. (addr & RTL8366RB_PHY_REG_MASK);
  316. ret = rtl8366_smi_write_reg(smi, reg, data);
  317. if (ret)
  318. return ret;
  319. return 0;
  320. }
  321. static int rtl8366rb_get_mib_counter(struct rtl8366_smi *smi, int counter,
  322. int port, unsigned long long *val)
  323. {
  324. int i;
  325. int err;
  326. u32 addr, data;
  327. u64 mibvalue;
  328. if (port > RTL8366RB_NUM_PORTS || counter >= RTL8366RB_MIB_COUNT)
  329. return -EINVAL;
  330. addr = RTL8366RB_MIB_COUNTER_BASE +
  331. RTL8366RB_MIB_COUNTER_PORT_OFFSET * (port) +
  332. rtl8366rb_mib_counters[counter].offset;
  333. /*
  334. * Writing access counter address first
  335. * then ASIC will prepare 64bits counter wait for being retrived
  336. */
  337. data = 0; /* writing data will be discard by ASIC */
  338. err = rtl8366_smi_write_reg(smi, addr, data);
  339. if (err)
  340. return err;
  341. /* read MIB control register */
  342. err = rtl8366_smi_read_reg(smi, RTL8366RB_MIB_CTRL_REG, &data);
  343. if (err)
  344. return err;
  345. if (data & RTL8366RB_MIB_CTRL_BUSY_MASK)
  346. return -EBUSY;
  347. if (data & RTL8366RB_MIB_CTRL_RESET_MASK)
  348. return -EIO;
  349. mibvalue = 0;
  350. for (i = rtl8366rb_mib_counters[counter].length; i > 0; i--) {
  351. err = rtl8366_smi_read_reg(smi, addr + (i - 1), &data);
  352. if (err)
  353. return err;
  354. mibvalue = (mibvalue << 16) | (data & 0xFFFF);
  355. }
  356. *val = mibvalue;
  357. return 0;
  358. }
  359. static int rtl8366rb_get_vlan_4k(struct rtl8366_smi *smi, u32 vid,
  360. struct rtl8366_vlan_4k *vlan4k)
  361. {
  362. u32 data[3];
  363. int err;
  364. int i;
  365. memset(vlan4k, '\0', sizeof(struct rtl8366_vlan_4k));
  366. if (vid >= RTL8366RB_NUM_VIDS)
  367. return -EINVAL;
  368. /* write VID */
  369. err = rtl8366_smi_write_reg(smi, RTL8366RB_VLAN_TABLE_WRITE_BASE,
  370. vid & RTL8366RB_VLAN_VID_MASK);
  371. if (err)
  372. return err;
  373. /* write table access control word */
  374. err = rtl8366_smi_write_reg(smi, RTL8366RB_TABLE_ACCESS_CTRL_REG,
  375. RTL8366RB_TABLE_VLAN_READ_CTRL);
  376. if (err)
  377. return err;
  378. for (i = 0; i < 3; i++) {
  379. err = rtl8366_smi_read_reg(smi,
  380. RTL8366RB_VLAN_TABLE_READ_BASE + i,
  381. &data[i]);
  382. if (err)
  383. return err;
  384. }
  385. vlan4k->vid = vid;
  386. vlan4k->untag = (data[1] >> RTL8366RB_VLAN_UNTAG_SHIFT) &
  387. RTL8366RB_VLAN_UNTAG_MASK;
  388. vlan4k->member = data[1] & RTL8366RB_VLAN_MEMBER_MASK;
  389. vlan4k->fid = data[2] & RTL8366RB_VLAN_FID_MASK;
  390. return 0;
  391. }
  392. static int rtl8366rb_set_vlan_4k(struct rtl8366_smi *smi,
  393. const struct rtl8366_vlan_4k *vlan4k)
  394. {
  395. u32 data[3];
  396. int err;
  397. int i;
  398. if (vlan4k->vid >= RTL8366RB_NUM_VIDS ||
  399. vlan4k->member > RTL8366RB_VLAN_MEMBER_MASK ||
  400. vlan4k->untag > RTL8366RB_VLAN_UNTAG_MASK ||
  401. vlan4k->fid > RTL8366RB_FIDMAX)
  402. return -EINVAL;
  403. data[0] = vlan4k->vid & RTL8366RB_VLAN_VID_MASK;
  404. data[1] = (vlan4k->member & RTL8366RB_VLAN_MEMBER_MASK) |
  405. ((vlan4k->untag & RTL8366RB_VLAN_UNTAG_MASK) <<
  406. RTL8366RB_VLAN_UNTAG_SHIFT);
  407. data[2] = vlan4k->fid & RTL8366RB_VLAN_FID_MASK;
  408. for (i = 0; i < 3; i++) {
  409. err = rtl8366_smi_write_reg(smi,
  410. RTL8366RB_VLAN_TABLE_WRITE_BASE + i,
  411. data[i]);
  412. if (err)
  413. return err;
  414. }
  415. /* write table access control word */
  416. err = rtl8366_smi_write_reg(smi, RTL8366RB_TABLE_ACCESS_CTRL_REG,
  417. RTL8366RB_TABLE_VLAN_WRITE_CTRL);
  418. return err;
  419. }
  420. static int rtl8366rb_get_vlan_mc(struct rtl8366_smi *smi, u32 index,
  421. struct rtl8366_vlan_mc *vlanmc)
  422. {
  423. u32 data[3];
  424. int err;
  425. int i;
  426. memset(vlanmc, '\0', sizeof(struct rtl8366_vlan_mc));
  427. if (index >= RTL8366RB_NUM_VLANS)
  428. return -EINVAL;
  429. for (i = 0; i < 3; i++) {
  430. err = rtl8366_smi_read_reg(smi,
  431. RTL8366RB_VLAN_MC_BASE(index) + i,
  432. &data[i]);
  433. if (err)
  434. return err;
  435. }
  436. vlanmc->vid = data[0] & RTL8366RB_VLAN_VID_MASK;
  437. vlanmc->priority = (data[0] >> RTL8366RB_VLAN_PRIORITY_SHIFT) &
  438. RTL8366RB_VLAN_PRIORITY_MASK;
  439. vlanmc->untag = (data[1] >> RTL8366RB_VLAN_UNTAG_SHIFT) &
  440. RTL8366RB_VLAN_UNTAG_MASK;
  441. vlanmc->member = data[1] & RTL8366RB_VLAN_MEMBER_MASK;
  442. vlanmc->fid = data[2] & RTL8366RB_VLAN_FID_MASK;
  443. return 0;
  444. }
  445. static int rtl8366rb_set_vlan_mc(struct rtl8366_smi *smi, u32 index,
  446. const struct rtl8366_vlan_mc *vlanmc)
  447. {
  448. u32 data[3];
  449. int err;
  450. int i;
  451. if (index >= RTL8366RB_NUM_VLANS ||
  452. vlanmc->vid >= RTL8366RB_NUM_VIDS ||
  453. vlanmc->priority > RTL8366RB_PRIORITYMAX ||
  454. vlanmc->member > RTL8366RB_VLAN_MEMBER_MASK ||
  455. vlanmc->untag > RTL8366RB_VLAN_UNTAG_MASK ||
  456. vlanmc->fid > RTL8366RB_FIDMAX)
  457. return -EINVAL;
  458. data[0] = (vlanmc->vid & RTL8366RB_VLAN_VID_MASK) |
  459. ((vlanmc->priority & RTL8366RB_VLAN_PRIORITY_MASK) <<
  460. RTL8366RB_VLAN_PRIORITY_SHIFT);
  461. data[1] = (vlanmc->member & RTL8366RB_VLAN_MEMBER_MASK) |
  462. ((vlanmc->untag & RTL8366RB_VLAN_UNTAG_MASK) <<
  463. RTL8366RB_VLAN_UNTAG_SHIFT);
  464. data[2] = vlanmc->fid & RTL8366RB_VLAN_FID_MASK;
  465. for (i = 0; i < 3; i++) {
  466. err = rtl8366_smi_write_reg(smi,
  467. RTL8366RB_VLAN_MC_BASE(index) + i,
  468. data[i]);
  469. if (err)
  470. return err;
  471. }
  472. return 0;
  473. }
  474. static int rtl8366rb_get_mc_index(struct rtl8366_smi *smi, int port, int *val)
  475. {
  476. u32 data;
  477. int err;
  478. if (port >= RTL8366RB_NUM_PORTS)
  479. return -EINVAL;
  480. err = rtl8366_smi_read_reg(smi, RTL8366RB_PORT_VLAN_CTRL_REG(port),
  481. &data);
  482. if (err)
  483. return err;
  484. *val = (data >> RTL8366RB_PORT_VLAN_CTRL_SHIFT(port)) &
  485. RTL8366RB_PORT_VLAN_CTRL_MASK;
  486. return 0;
  487. }
  488. static int rtl8366rb_set_mc_index(struct rtl8366_smi *smi, int port, int index)
  489. {
  490. if (port >= RTL8366RB_NUM_PORTS || index >= RTL8366RB_NUM_VLANS)
  491. return -EINVAL;
  492. return rtl8366_smi_rmwr(smi, RTL8366RB_PORT_VLAN_CTRL_REG(port),
  493. RTL8366RB_PORT_VLAN_CTRL_MASK <<
  494. RTL8366RB_PORT_VLAN_CTRL_SHIFT(port),
  495. (index & RTL8366RB_PORT_VLAN_CTRL_MASK) <<
  496. RTL8366RB_PORT_VLAN_CTRL_SHIFT(port));
  497. }
  498. static int rtl8366rb_is_vlan_valid(struct rtl8366_smi *smi, unsigned vlan)
  499. {
  500. unsigned max = RTL8366RB_NUM_VLANS;
  501. if (smi->vlan4k_enabled)
  502. max = RTL8366RB_NUM_VIDS - 1;
  503. if (vlan == 0 || vlan >= max)
  504. return 0;
  505. return 1;
  506. }
  507. static int rtl8366rb_enable_vlan(struct rtl8366_smi *smi, int enable)
  508. {
  509. return rtl8366_smi_rmwr(smi, RTL8366RB_SGCR, RTL8366RB_SGCR_EN_VLAN,
  510. (enable) ? RTL8366RB_SGCR_EN_VLAN : 0);
  511. }
  512. static int rtl8366rb_enable_vlan4k(struct rtl8366_smi *smi, int enable)
  513. {
  514. return rtl8366_smi_rmwr(smi, RTL8366RB_SGCR,
  515. RTL8366RB_SGCR_EN_VLAN_4KTB,
  516. (enable) ? RTL8366RB_SGCR_EN_VLAN_4KTB : 0);
  517. }
  518. static int rtl8366rb_enable_port(struct rtl8366_smi *smi, int port, int enable)
  519. {
  520. return rtl8366_smi_rmwr(smi, RTL8366RB_PECR, (1 << port),
  521. (enable) ? 0 : (1 << port));
  522. }
  523. static int rtl8366rb_sw_reset_mibs(struct switch_dev *dev,
  524. const struct switch_attr *attr,
  525. struct switch_val *val)
  526. {
  527. struct rtl8366_smi *smi = sw_to_rtl8366_smi(dev);
  528. return rtl8366_smi_rmwr(smi, RTL8366RB_MIB_CTRL_REG, 0,
  529. RTL8366RB_MIB_CTRL_GLOBAL_RESET);
  530. }
  531. static int rtl8366rb_sw_get_blinkrate(struct switch_dev *dev,
  532. const struct switch_attr *attr,
  533. struct switch_val *val)
  534. {
  535. struct rtl8366_smi *smi = sw_to_rtl8366_smi(dev);
  536. u32 data;
  537. rtl8366_smi_read_reg(smi, RTL8366RB_LED_BLINKRATE_REG, &data);
  538. val->value.i = (data & (RTL8366RB_LED_BLINKRATE_MASK));
  539. return 0;
  540. }
  541. static int rtl8366rb_sw_set_blinkrate(struct switch_dev *dev,
  542. const struct switch_attr *attr,
  543. struct switch_val *val)
  544. {
  545. struct rtl8366_smi *smi = sw_to_rtl8366_smi(dev);
  546. if (val->value.i >= 6)
  547. return -EINVAL;
  548. return rtl8366_smi_rmwr(smi, RTL8366RB_LED_BLINKRATE_REG,
  549. RTL8366RB_LED_BLINKRATE_MASK,
  550. val->value.i);
  551. }
  552. static int rtl8366rb_sw_get_learning_enable(struct switch_dev *dev,
  553. const struct switch_attr *attr,
  554. struct switch_val *val)
  555. {
  556. struct rtl8366_smi *smi = sw_to_rtl8366_smi(dev);
  557. u32 data;
  558. rtl8366_smi_read_reg(smi, RTL8366RB_SSCR0, &data);
  559. val->value.i = !data;
  560. return 0;
  561. }
  562. static int rtl8366rb_sw_set_learning_enable(struct switch_dev *dev,
  563. const struct switch_attr *attr,
  564. struct switch_val *val)
  565. {
  566. struct rtl8366_smi *smi = sw_to_rtl8366_smi(dev);
  567. u32 portmask = 0;
  568. int err = 0;
  569. if (!val->value.i)
  570. portmask = RTL8366RB_PORT_ALL;
  571. /* set learning for all ports */
  572. REG_WR(smi, RTL8366RB_SSCR0, portmask);
  573. /* set auto ageing for all ports */
  574. REG_WR(smi, RTL8366RB_SSCR1, portmask);
  575. return 0;
  576. }
  577. static int rtl8366rb_sw_get_port_link(struct switch_dev *dev,
  578. int port,
  579. struct switch_port_link *link)
  580. {
  581. struct rtl8366_smi *smi = sw_to_rtl8366_smi(dev);
  582. u32 data = 0;
  583. u32 speed;
  584. if (port >= RTL8366RB_NUM_PORTS)
  585. return -EINVAL;
  586. rtl8366_smi_read_reg(smi, RTL8366RB_PORT_LINK_STATUS_BASE + (port / 2),
  587. &data);
  588. if (port % 2)
  589. data = data >> 8;
  590. link->link = !!(data & RTL8366RB_PORT_STATUS_LINK_MASK);
  591. if (!link->link)
  592. return 0;
  593. link->duplex = !!(data & RTL8366RB_PORT_STATUS_DUPLEX_MASK);
  594. link->rx_flow = !!(data & RTL8366RB_PORT_STATUS_RXPAUSE_MASK);
  595. link->tx_flow = !!(data & RTL8366RB_PORT_STATUS_TXPAUSE_MASK);
  596. link->aneg = !!(data & RTL8366RB_PORT_STATUS_AN_MASK);
  597. speed = (data & RTL8366RB_PORT_STATUS_SPEED_MASK);
  598. switch (speed) {
  599. case 0:
  600. link->speed = SWITCH_PORT_SPEED_10;
  601. break;
  602. case 1:
  603. link->speed = SWITCH_PORT_SPEED_100;
  604. break;
  605. case 2:
  606. link->speed = SWITCH_PORT_SPEED_1000;
  607. break;
  608. default:
  609. link->speed = SWITCH_PORT_SPEED_UNKNOWN;
  610. break;
  611. }
  612. return 0;
  613. }
  614. static int rtl8366rb_sw_set_port_led(struct switch_dev *dev,
  615. const struct switch_attr *attr,
  616. struct switch_val *val)
  617. {
  618. struct rtl8366_smi *smi = sw_to_rtl8366_smi(dev);
  619. u32 data;
  620. u32 mask;
  621. u32 reg;
  622. if (val->port_vlan >= RTL8366RB_NUM_PORTS)
  623. return -EINVAL;
  624. if (val->port_vlan == RTL8366RB_PORT_NUM_CPU) {
  625. reg = RTL8366RB_LED_BLINKRATE_REG;
  626. mask = 0xF << 4;
  627. data = val->value.i << 4;
  628. } else {
  629. reg = RTL8366RB_LED_CTRL_REG;
  630. mask = 0xF << (val->port_vlan * 4),
  631. data = val->value.i << (val->port_vlan * 4);
  632. }
  633. return rtl8366_smi_rmwr(smi, reg, mask, data);
  634. }
  635. static int rtl8366rb_sw_get_port_led(struct switch_dev *dev,
  636. const struct switch_attr *attr,
  637. struct switch_val *val)
  638. {
  639. struct rtl8366_smi *smi = sw_to_rtl8366_smi(dev);
  640. u32 data = 0;
  641. if (val->port_vlan >= RTL8366RB_NUM_LEDGROUPS)
  642. return -EINVAL;
  643. rtl8366_smi_read_reg(smi, RTL8366RB_LED_CTRL_REG, &data);
  644. val->value.i = (data >> (val->port_vlan * 4)) & 0x000F;
  645. return 0;
  646. }
  647. static int rtl8366rb_sw_set_port_disable(struct switch_dev *dev,
  648. const struct switch_attr *attr,
  649. struct switch_val *val)
  650. {
  651. struct rtl8366_smi *smi = sw_to_rtl8366_smi(dev);
  652. u32 mask, data;
  653. if (val->port_vlan >= RTL8366RB_NUM_PORTS)
  654. return -EINVAL;
  655. mask = 1 << val->port_vlan ;
  656. if (val->value.i)
  657. data = mask;
  658. else
  659. data = 0;
  660. return rtl8366_smi_rmwr(smi, RTL8366RB_PECR, mask, data);
  661. }
  662. static int rtl8366rb_sw_get_port_disable(struct switch_dev *dev,
  663. const struct switch_attr *attr,
  664. struct switch_val *val)
  665. {
  666. struct rtl8366_smi *smi = sw_to_rtl8366_smi(dev);
  667. u32 data;
  668. if (val->port_vlan >= RTL8366RB_NUM_PORTS)
  669. return -EINVAL;
  670. rtl8366_smi_read_reg(smi, RTL8366RB_PECR, &data);
  671. if (data & (1 << val->port_vlan))
  672. val->value.i = 1;
  673. else
  674. val->value.i = 0;
  675. return 0;
  676. }
  677. static int rtl8366rb_sw_set_port_rate_in(struct switch_dev *dev,
  678. const struct switch_attr *attr,
  679. struct switch_val *val)
  680. {
  681. struct rtl8366_smi *smi = sw_to_rtl8366_smi(dev);
  682. if (val->port_vlan >= RTL8366RB_NUM_PORTS)
  683. return -EINVAL;
  684. if (val->value.i > 0 && val->value.i < RTL8366RB_BDTH_SW_MAX)
  685. val->value.i = (val->value.i - 1) / RTL8366RB_BDTH_UNIT;
  686. else
  687. val->value.i = RTL8366RB_BDTH_REG_DEFAULT;
  688. return rtl8366_smi_rmwr(smi, RTL8366RB_IB_REG(val->port_vlan),
  689. RTL8366RB_IB_BDTH_MASK | RTL8366RB_IB_PREIFG_MASK,
  690. val->value.i |
  691. (RTL8366RB_QOS_DEFAULT_PREIFG << RTL8366RB_IB_PREIFG_OFFSET));
  692. }
  693. static int rtl8366rb_sw_get_port_rate_in(struct switch_dev *dev,
  694. const struct switch_attr *attr,
  695. struct switch_val *val)
  696. {
  697. struct rtl8366_smi *smi = sw_to_rtl8366_smi(dev);
  698. u32 data;
  699. if (val->port_vlan >= RTL8366RB_NUM_PORTS)
  700. return -EINVAL;
  701. rtl8366_smi_read_reg(smi, RTL8366RB_IB_REG(val->port_vlan), &data);
  702. data &= RTL8366RB_IB_BDTH_MASK;
  703. if (data < RTL8366RB_IB_BDTH_MASK)
  704. data += 1;
  705. val->value.i = (int)data * RTL8366RB_BDTH_UNIT;
  706. return 0;
  707. }
  708. static int rtl8366rb_sw_set_port_rate_out(struct switch_dev *dev,
  709. const struct switch_attr *attr,
  710. struct switch_val *val)
  711. {
  712. struct rtl8366_smi *smi = sw_to_rtl8366_smi(dev);
  713. if (val->port_vlan >= RTL8366RB_NUM_PORTS)
  714. return -EINVAL;
  715. rtl8366_smi_rmwr(smi, RTL8366RB_EB_PREIFG_REG,
  716. RTL8366RB_EB_PREIFG_MASK,
  717. (RTL8366RB_QOS_DEFAULT_PREIFG << RTL8366RB_EB_PREIFG_OFFSET));
  718. if (val->value.i > 0 && val->value.i < RTL8366RB_BDTH_SW_MAX)
  719. val->value.i = (val->value.i - 1) / RTL8366RB_BDTH_UNIT;
  720. else
  721. val->value.i = RTL8366RB_BDTH_REG_DEFAULT;
  722. return rtl8366_smi_rmwr(smi, RTL8366RB_EB_REG(val->port_vlan),
  723. RTL8366RB_EB_BDTH_MASK, val->value.i );
  724. }
  725. static int rtl8366rb_sw_get_port_rate_out(struct switch_dev *dev,
  726. const struct switch_attr *attr,
  727. struct switch_val *val)
  728. {
  729. struct rtl8366_smi *smi = sw_to_rtl8366_smi(dev);
  730. u32 data;
  731. if (val->port_vlan >= RTL8366RB_NUM_PORTS)
  732. return -EINVAL;
  733. rtl8366_smi_read_reg(smi, RTL8366RB_EB_REG(val->port_vlan), &data);
  734. data &= RTL8366RB_EB_BDTH_MASK;
  735. if (data < RTL8366RB_EB_BDTH_MASK)
  736. data += 1;
  737. val->value.i = (int)data * RTL8366RB_BDTH_UNIT;
  738. return 0;
  739. }
  740. static int rtl8366rb_sw_set_qos_enable(struct switch_dev *dev,
  741. const struct switch_attr *attr,
  742. struct switch_val *val)
  743. {
  744. struct rtl8366_smi *smi = sw_to_rtl8366_smi(dev);
  745. u32 data;
  746. if (val->value.i)
  747. data = RTL8366RB_QOS_MASK;
  748. else
  749. data = 0;
  750. return rtl8366_smi_rmwr(smi, RTL8366RB_SGCR, RTL8366RB_QOS_MASK, data);
  751. }
  752. static int rtl8366rb_sw_get_qos_enable(struct switch_dev *dev,
  753. const struct switch_attr *attr,
  754. struct switch_val *val)
  755. {
  756. struct rtl8366_smi *smi = sw_to_rtl8366_smi(dev);
  757. u32 data;
  758. rtl8366_smi_read_reg(smi, RTL8366RB_SGCR, &data);
  759. if (data & RTL8366RB_QOS_MASK)
  760. val->value.i = 1;
  761. else
  762. val->value.i = 0;
  763. return 0;
  764. }
  765. static int rtl8366rb_sw_set_mirror_rx_enable(struct switch_dev *dev,
  766. const struct switch_attr *attr,
  767. struct switch_val *val)
  768. {
  769. struct rtl8366_smi *smi = sw_to_rtl8366_smi(dev);
  770. u32 data;
  771. if (val->value.i)
  772. data = RTL8366RB_PMCR_MIRROR_RX;
  773. else
  774. data = 0;
  775. return rtl8366_smi_rmwr(smi, RTL8366RB_PMCR, RTL8366RB_PMCR_MIRROR_RX, data);
  776. }
  777. static int rtl8366rb_sw_get_mirror_rx_enable(struct switch_dev *dev,
  778. const struct switch_attr *attr,
  779. struct switch_val *val)
  780. {
  781. struct rtl8366_smi *smi = sw_to_rtl8366_smi(dev);
  782. u32 data;
  783. rtl8366_smi_read_reg(smi, RTL8366RB_PMCR, &data);
  784. if (data & RTL8366RB_PMCR_MIRROR_RX)
  785. val->value.i = 1;
  786. else
  787. val->value.i = 0;
  788. return 0;
  789. }
  790. static int rtl8366rb_sw_set_mirror_tx_enable(struct switch_dev *dev,
  791. const struct switch_attr *attr,
  792. struct switch_val *val)
  793. {
  794. struct rtl8366_smi *smi = sw_to_rtl8366_smi(dev);
  795. u32 data;
  796. if (val->value.i)
  797. data = RTL8366RB_PMCR_MIRROR_TX;
  798. else
  799. data = 0;
  800. return rtl8366_smi_rmwr(smi, RTL8366RB_PMCR, RTL8366RB_PMCR_MIRROR_TX, data);
  801. }
  802. static int rtl8366rb_sw_get_mirror_tx_enable(struct switch_dev *dev,
  803. const struct switch_attr *attr,
  804. struct switch_val *val)
  805. {
  806. struct rtl8366_smi *smi = sw_to_rtl8366_smi(dev);
  807. u32 data;
  808. rtl8366_smi_read_reg(smi, RTL8366RB_PMCR, &data);
  809. if (data & RTL8366RB_PMCR_MIRROR_TX)
  810. val->value.i = 1;
  811. else
  812. val->value.i = 0;
  813. return 0;
  814. }
  815. static int rtl8366rb_sw_set_monitor_isolation_enable(struct switch_dev *dev,
  816. const struct switch_attr *attr,
  817. struct switch_val *val)
  818. {
  819. struct rtl8366_smi *smi = sw_to_rtl8366_smi(dev);
  820. u32 data;
  821. if (val->value.i)
  822. data = RTL8366RB_PMCR_MIRROR_ISO;
  823. else
  824. data = 0;
  825. return rtl8366_smi_rmwr(smi, RTL8366RB_PMCR, RTL8366RB_PMCR_MIRROR_ISO, data);
  826. }
  827. static int rtl8366rb_sw_get_monitor_isolation_enable(struct switch_dev *dev,
  828. const struct switch_attr *attr,
  829. struct switch_val *val)
  830. {
  831. struct rtl8366_smi *smi = sw_to_rtl8366_smi(dev);
  832. u32 data;
  833. rtl8366_smi_read_reg(smi, RTL8366RB_PMCR, &data);
  834. if (data & RTL8366RB_PMCR_MIRROR_ISO)
  835. val->value.i = 1;
  836. else
  837. val->value.i = 0;
  838. return 0;
  839. }
  840. static int rtl8366rb_sw_set_mirror_pause_frames_enable(struct switch_dev *dev,
  841. const struct switch_attr *attr,
  842. struct switch_val *val)
  843. {
  844. struct rtl8366_smi *smi = sw_to_rtl8366_smi(dev);
  845. u32 data;
  846. if (val->value.i)
  847. data = RTL8366RB_PMCR_MIRROR_SPC;
  848. else
  849. data = 0;
  850. return rtl8366_smi_rmwr(smi, RTL8366RB_PMCR, RTL8366RB_PMCR_MIRROR_SPC, data);
  851. }
  852. static int rtl8366rb_sw_get_mirror_pause_frames_enable(struct switch_dev *dev,
  853. const struct switch_attr *attr,
  854. struct switch_val *val)
  855. {
  856. struct rtl8366_smi *smi = sw_to_rtl8366_smi(dev);
  857. u32 data;
  858. rtl8366_smi_read_reg(smi, RTL8366RB_PMCR, &data);
  859. if (data & RTL8366RB_PMCR_MIRROR_SPC)
  860. val->value.i = 1;
  861. else
  862. val->value.i = 0;
  863. return 0;
  864. }
  865. static int rtl8366rb_sw_set_mirror_monitor_port(struct switch_dev *dev,
  866. const struct switch_attr *attr,
  867. struct switch_val *val)
  868. {
  869. struct rtl8366_smi *smi = sw_to_rtl8366_smi(dev);
  870. u32 data;
  871. data = RTL8366RB_PMCR_MONITOR_PORT(val->value.i);
  872. return rtl8366_smi_rmwr(smi, RTL8366RB_PMCR, RTL8366RB_PMCR_MONITOR_PORT_MASK, data);
  873. }
  874. static int rtl8366rb_sw_get_mirror_monitor_port(struct switch_dev *dev,
  875. const struct switch_attr *attr,
  876. struct switch_val *val)
  877. {
  878. struct rtl8366_smi *smi = sw_to_rtl8366_smi(dev);
  879. u32 data;
  880. rtl8366_smi_read_reg(smi, RTL8366RB_PMCR, &data);
  881. val->value.i = (data & RTL8366RB_PMCR_MONITOR_PORT_MASK) >> 4;
  882. return 0;
  883. }
  884. static int rtl8366rb_sw_set_mirror_source_port(struct switch_dev *dev,
  885. const struct switch_attr *attr,
  886. struct switch_val *val)
  887. {
  888. struct rtl8366_smi *smi = sw_to_rtl8366_smi(dev);
  889. u32 data;
  890. data = RTL8366RB_PMCR_SOURCE_PORT(val->value.i);
  891. return rtl8366_smi_rmwr(smi, RTL8366RB_PMCR, RTL8366RB_PMCR_SOURCE_PORT_MASK, data);
  892. }
  893. static int rtl8366rb_sw_get_mirror_source_port(struct switch_dev *dev,
  894. const struct switch_attr *attr,
  895. struct switch_val *val)
  896. {
  897. struct rtl8366_smi *smi = sw_to_rtl8366_smi(dev);
  898. u32 data;
  899. rtl8366_smi_read_reg(smi, RTL8366RB_PMCR, &data);
  900. val->value.i = data & RTL8366RB_PMCR_SOURCE_PORT_MASK;
  901. return 0;
  902. }
  903. static int rtl8366rb_sw_reset_port_mibs(struct switch_dev *dev,
  904. const struct switch_attr *attr,
  905. struct switch_val *val)
  906. {
  907. struct rtl8366_smi *smi = sw_to_rtl8366_smi(dev);
  908. if (val->port_vlan >= RTL8366RB_NUM_PORTS)
  909. return -EINVAL;
  910. return rtl8366_smi_rmwr(smi, RTL8366RB_MIB_CTRL_REG, 0,
  911. RTL8366RB_MIB_CTRL_PORT_RESET(val->port_vlan));
  912. }
  913. static int rtl8366rb_sw_get_port_stats(struct switch_dev *dev, int port,
  914. struct switch_port_stats *stats)
  915. {
  916. return (rtl8366_sw_get_port_stats(dev, port, stats,
  917. RTL8366RB_MIB_TXB_ID, RTL8366RB_MIB_RXB_ID));
  918. }
  919. static struct switch_attr rtl8366rb_globals[] = {
  920. {
  921. .type = SWITCH_TYPE_INT,
  922. .name = "enable_learning",
  923. .description = "Enable learning, enable aging",
  924. .set = rtl8366rb_sw_set_learning_enable,
  925. .get = rtl8366rb_sw_get_learning_enable,
  926. .max = 1
  927. }, {
  928. .type = SWITCH_TYPE_INT,
  929. .name = "enable_vlan",
  930. .description = "Enable VLAN mode",
  931. .set = rtl8366_sw_set_vlan_enable,
  932. .get = rtl8366_sw_get_vlan_enable,
  933. .max = 1,
  934. .ofs = 1
  935. }, {
  936. .type = SWITCH_TYPE_INT,
  937. .name = "enable_vlan4k",
  938. .description = "Enable VLAN 4K mode",
  939. .set = rtl8366_sw_set_vlan_enable,
  940. .get = rtl8366_sw_get_vlan_enable,
  941. .max = 1,
  942. .ofs = 2
  943. }, {
  944. .type = SWITCH_TYPE_NOVAL,
  945. .name = "reset_mibs",
  946. .description = "Reset all MIB counters",
  947. .set = rtl8366rb_sw_reset_mibs,
  948. }, {
  949. .type = SWITCH_TYPE_INT,
  950. .name = "blinkrate",
  951. .description = "Get/Set LED blinking rate (0 = 43ms, 1 = 84ms,"
  952. " 2 = 120ms, 3 = 170ms, 4 = 340ms, 5 = 670ms)",
  953. .set = rtl8366rb_sw_set_blinkrate,
  954. .get = rtl8366rb_sw_get_blinkrate,
  955. .max = 5
  956. }, {
  957. .type = SWITCH_TYPE_INT,
  958. .name = "enable_qos",
  959. .description = "Enable QOS",
  960. .set = rtl8366rb_sw_set_qos_enable,
  961. .get = rtl8366rb_sw_get_qos_enable,
  962. .max = 1
  963. }, {
  964. .type = SWITCH_TYPE_INT,
  965. .name = "enable_mirror_rx",
  966. .description = "Enable mirroring of RX packets",
  967. .set = rtl8366rb_sw_set_mirror_rx_enable,
  968. .get = rtl8366rb_sw_get_mirror_rx_enable,
  969. .max = 1
  970. }, {
  971. .type = SWITCH_TYPE_INT,
  972. .name = "enable_mirror_tx",
  973. .description = "Enable mirroring of TX packets",
  974. .set = rtl8366rb_sw_set_mirror_tx_enable,
  975. .get = rtl8366rb_sw_get_mirror_tx_enable,
  976. .max = 1
  977. }, {
  978. .type = SWITCH_TYPE_INT,
  979. .name = "enable_monitor_isolation",
  980. .description = "Enable isolation of monitor port (TX packets will be dropped)",
  981. .set = rtl8366rb_sw_set_monitor_isolation_enable,
  982. .get = rtl8366rb_sw_get_monitor_isolation_enable,
  983. .max = 1
  984. }, {
  985. .type = SWITCH_TYPE_INT,
  986. .name = "enable_mirror_pause_frames",
  987. .description = "Enable mirroring of RX pause frames",
  988. .set = rtl8366rb_sw_set_mirror_pause_frames_enable,
  989. .get = rtl8366rb_sw_get_mirror_pause_frames_enable,
  990. .max = 1
  991. }, {
  992. .type = SWITCH_TYPE_INT,
  993. .name = "mirror_monitor_port",
  994. .description = "Mirror monitor port",
  995. .set = rtl8366rb_sw_set_mirror_monitor_port,
  996. .get = rtl8366rb_sw_get_mirror_monitor_port,
  997. .max = 5
  998. }, {
  999. .type = SWITCH_TYPE_INT,
  1000. .name = "mirror_source_port",
  1001. .description = "Mirror source port",
  1002. .set = rtl8366rb_sw_set_mirror_source_port,
  1003. .get = rtl8366rb_sw_get_mirror_source_port,
  1004. .max = 5
  1005. },
  1006. };
  1007. static struct switch_attr rtl8366rb_port[] = {
  1008. {
  1009. .type = SWITCH_TYPE_NOVAL,
  1010. .name = "reset_mib",
  1011. .description = "Reset single port MIB counters",
  1012. .set = rtl8366rb_sw_reset_port_mibs,
  1013. }, {
  1014. .type = SWITCH_TYPE_STRING,
  1015. .name = "mib",
  1016. .description = "Get MIB counters for port",
  1017. .max = 33,
  1018. .set = NULL,
  1019. .get = rtl8366_sw_get_port_mib,
  1020. }, {
  1021. .type = SWITCH_TYPE_INT,
  1022. .name = "led",
  1023. .description = "Get/Set port group (0 - 3) led mode (0 - 15)",
  1024. .max = 15,
  1025. .set = rtl8366rb_sw_set_port_led,
  1026. .get = rtl8366rb_sw_get_port_led,
  1027. }, {
  1028. .type = SWITCH_TYPE_INT,
  1029. .name = "disable",
  1030. .description = "Get/Set port state (enabled or disabled)",
  1031. .max = 1,
  1032. .set = rtl8366rb_sw_set_port_disable,
  1033. .get = rtl8366rb_sw_get_port_disable,
  1034. }, {
  1035. .type = SWITCH_TYPE_INT,
  1036. .name = "rate_in",
  1037. .description = "Get/Set port ingress (incoming) bandwidth limit in kbps",
  1038. .max = RTL8366RB_BDTH_SW_MAX,
  1039. .set = rtl8366rb_sw_set_port_rate_in,
  1040. .get = rtl8366rb_sw_get_port_rate_in,
  1041. }, {
  1042. .type = SWITCH_TYPE_INT,
  1043. .name = "rate_out",
  1044. .description = "Get/Set port egress (outgoing) bandwidth limit in kbps",
  1045. .max = RTL8366RB_BDTH_SW_MAX,
  1046. .set = rtl8366rb_sw_set_port_rate_out,
  1047. .get = rtl8366rb_sw_get_port_rate_out,
  1048. },
  1049. };
  1050. static struct switch_attr rtl8366rb_vlan[] = {
  1051. {
  1052. .type = SWITCH_TYPE_STRING,
  1053. .name = "info",
  1054. .description = "Get vlan information",
  1055. .max = 1,
  1056. .set = NULL,
  1057. .get = rtl8366_sw_get_vlan_info,
  1058. }, {
  1059. .type = SWITCH_TYPE_INT,
  1060. .name = "fid",
  1061. .description = "Get/Set vlan FID",
  1062. .max = RTL8366RB_FIDMAX,
  1063. .set = rtl8366_sw_set_vlan_fid,
  1064. .get = rtl8366_sw_get_vlan_fid,
  1065. },
  1066. };
  1067. static const struct switch_dev_ops rtl8366_ops = {
  1068. .attr_global = {
  1069. .attr = rtl8366rb_globals,
  1070. .n_attr = ARRAY_SIZE(rtl8366rb_globals),
  1071. },
  1072. .attr_port = {
  1073. .attr = rtl8366rb_port,
  1074. .n_attr = ARRAY_SIZE(rtl8366rb_port),
  1075. },
  1076. .attr_vlan = {
  1077. .attr = rtl8366rb_vlan,
  1078. .n_attr = ARRAY_SIZE(rtl8366rb_vlan),
  1079. },
  1080. .get_vlan_ports = rtl8366_sw_get_vlan_ports,
  1081. .set_vlan_ports = rtl8366_sw_set_vlan_ports,
  1082. .get_port_pvid = rtl8366_sw_get_port_pvid,
  1083. .set_port_pvid = rtl8366_sw_set_port_pvid,
  1084. .reset_switch = rtl8366_sw_reset_switch,
  1085. .get_port_link = rtl8366rb_sw_get_port_link,
  1086. .get_port_stats = rtl8366rb_sw_get_port_stats,
  1087. };
  1088. static int rtl8366rb_switch_init(struct rtl8366_smi *smi)
  1089. {
  1090. struct switch_dev *dev = &smi->sw_dev;
  1091. int err;
  1092. dev->name = "RTL8366RB";
  1093. dev->cpu_port = RTL8366RB_PORT_NUM_CPU;
  1094. dev->ports = RTL8366RB_NUM_PORTS;
  1095. dev->vlans = RTL8366RB_NUM_VIDS;
  1096. dev->ops = &rtl8366_ops;
  1097. dev->alias = dev_name(smi->parent);
  1098. err = register_switch(dev, NULL);
  1099. if (err)
  1100. dev_err(smi->parent, "switch registration failed\n");
  1101. return err;
  1102. }
  1103. static void rtl8366rb_switch_cleanup(struct rtl8366_smi *smi)
  1104. {
  1105. unregister_switch(&smi->sw_dev);
  1106. }
  1107. static int rtl8366rb_mii_read(struct mii_bus *bus, int addr, int reg)
  1108. {
  1109. struct rtl8366_smi *smi = bus->priv;
  1110. u32 val = 0;
  1111. int err;
  1112. err = rtl8366rb_read_phy_reg(smi, addr, 0, reg, &val);
  1113. if (err)
  1114. return 0xffff;
  1115. return val;
  1116. }
  1117. static int rtl8366rb_mii_write(struct mii_bus *bus, int addr, int reg, u16 val)
  1118. {
  1119. struct rtl8366_smi *smi = bus->priv;
  1120. u32 t;
  1121. int err;
  1122. err = rtl8366rb_write_phy_reg(smi, addr, 0, reg, val);
  1123. /* flush write */
  1124. (void) rtl8366rb_read_phy_reg(smi, addr, 0, reg, &t);
  1125. return err;
  1126. }
  1127. static int rtl8366rb_detect(struct rtl8366_smi *smi)
  1128. {
  1129. u32 chip_id = 0;
  1130. u32 chip_ver = 0;
  1131. int ret;
  1132. ret = rtl8366_smi_read_reg(smi, RTL8366RB_CHIP_ID_REG, &chip_id);
  1133. if (ret) {
  1134. dev_err(smi->parent, "unable to read chip id\n");
  1135. return ret;
  1136. }
  1137. switch (chip_id) {
  1138. case RTL8366RB_CHIP_ID_8366:
  1139. break;
  1140. default:
  1141. dev_err(smi->parent, "unknown chip id (%04x)\n", chip_id);
  1142. return -ENODEV;
  1143. }
  1144. ret = rtl8366_smi_read_reg(smi, RTL8366RB_CHIP_VERSION_CTRL_REG,
  1145. &chip_ver);
  1146. if (ret) {
  1147. dev_err(smi->parent, "unable to read chip version\n");
  1148. return ret;
  1149. }
  1150. dev_info(smi->parent, "RTL%04x ver. %u chip found\n",
  1151. chip_id, chip_ver & RTL8366RB_CHIP_VERSION_MASK);
  1152. return 0;
  1153. }
  1154. static struct rtl8366_smi_ops rtl8366rb_smi_ops = {
  1155. .detect = rtl8366rb_detect,
  1156. .reset_chip = rtl8366rb_reset_chip,
  1157. .setup = rtl8366rb_setup,
  1158. .mii_read = rtl8366rb_mii_read,
  1159. .mii_write = rtl8366rb_mii_write,
  1160. .get_vlan_mc = rtl8366rb_get_vlan_mc,
  1161. .set_vlan_mc = rtl8366rb_set_vlan_mc,
  1162. .get_vlan_4k = rtl8366rb_get_vlan_4k,
  1163. .set_vlan_4k = rtl8366rb_set_vlan_4k,
  1164. .get_mc_index = rtl8366rb_get_mc_index,
  1165. .set_mc_index = rtl8366rb_set_mc_index,
  1166. .get_mib_counter = rtl8366rb_get_mib_counter,
  1167. .is_vlan_valid = rtl8366rb_is_vlan_valid,
  1168. .enable_vlan = rtl8366rb_enable_vlan,
  1169. .enable_vlan4k = rtl8366rb_enable_vlan4k,
  1170. .enable_port = rtl8366rb_enable_port,
  1171. };
  1172. static int rtl8366rb_probe(struct platform_device *pdev)
  1173. {
  1174. static int rtl8366_smi_version_printed;
  1175. struct rtl8366_smi *smi;
  1176. int err;
  1177. if (!rtl8366_smi_version_printed++)
  1178. printk(KERN_NOTICE RTL8366RB_DRIVER_DESC
  1179. " version " RTL8366RB_DRIVER_VER"\n");
  1180. smi = rtl8366_smi_probe(pdev);
  1181. if (IS_ERR(smi))
  1182. return PTR_ERR(smi);
  1183. smi->clk_delay = 10;
  1184. smi->cmd_read = 0xa9;
  1185. smi->cmd_write = 0xa8;
  1186. smi->ops = &rtl8366rb_smi_ops;
  1187. smi->cpu_port = RTL8366RB_PORT_NUM_CPU;
  1188. smi->num_ports = RTL8366RB_NUM_PORTS;
  1189. smi->num_vlan_mc = RTL8366RB_NUM_VLANS;
  1190. smi->mib_counters = rtl8366rb_mib_counters;
  1191. smi->num_mib_counters = ARRAY_SIZE(rtl8366rb_mib_counters);
  1192. err = rtl8366_smi_init(smi);
  1193. if (err)
  1194. goto err_free_smi;
  1195. platform_set_drvdata(pdev, smi);
  1196. err = rtl8366rb_switch_init(smi);
  1197. if (err)
  1198. goto err_clear_drvdata;
  1199. return 0;
  1200. err_clear_drvdata:
  1201. platform_set_drvdata(pdev, NULL);
  1202. rtl8366_smi_cleanup(smi);
  1203. err_free_smi:
  1204. kfree(smi);
  1205. return err;
  1206. }
  1207. static int rtl8366rb_remove(struct platform_device *pdev)
  1208. {
  1209. struct rtl8366_smi *smi = platform_get_drvdata(pdev);
  1210. if (smi) {
  1211. rtl8366rb_switch_cleanup(smi);
  1212. platform_set_drvdata(pdev, NULL);
  1213. rtl8366_smi_cleanup(smi);
  1214. kfree(smi);
  1215. }
  1216. return 0;
  1217. }
  1218. #ifdef CONFIG_OF
  1219. static const struct of_device_id rtl8366rb_match[] = {
  1220. { .compatible = "realtek,rtl8366rb" },
  1221. {},
  1222. };
  1223. MODULE_DEVICE_TABLE(of, rtl8366rb_match);
  1224. #endif
  1225. static struct platform_driver rtl8366rb_driver = {
  1226. .driver = {
  1227. .name = RTL8366RB_DRIVER_NAME,
  1228. .owner = THIS_MODULE,
  1229. .of_match_table = of_match_ptr(rtl8366rb_match),
  1230. },
  1231. .probe = rtl8366rb_probe,
  1232. .remove = rtl8366rb_remove,
  1233. };
  1234. static int __init rtl8366rb_module_init(void)
  1235. {
  1236. return platform_driver_register(&rtl8366rb_driver);
  1237. }
  1238. module_init(rtl8366rb_module_init);
  1239. static void __exit rtl8366rb_module_exit(void)
  1240. {
  1241. platform_driver_unregister(&rtl8366rb_driver);
  1242. }
  1243. module_exit(rtl8366rb_module_exit);
  1244. MODULE_DESCRIPTION(RTL8366RB_DRIVER_DESC);
  1245. MODULE_VERSION(RTL8366RB_DRIVER_VER);
  1246. MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org>");
  1247. MODULE_AUTHOR("Antti Seppälä <a.seppala@gmail.com>");
  1248. MODULE_AUTHOR("Roman Yeryomin <roman@advem.lv>");
  1249. MODULE_AUTHOR("Colin Leitner <colin.leitner@googlemail.com>");
  1250. MODULE_LICENSE("GPL v2");
  1251. MODULE_ALIAS("platform:" RTL8366RB_DRIVER_NAME);