rtl8366rb.c 38 KB

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