rtl8366rb.c 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Realtek SMI subdriver for the Realtek RTL8366RB ethernet switch
  3. *
  4. * This is a sparsely documented chip, the only viable documentation seems
  5. * to be a patched up code drop from the vendor that appear in various
  6. * GPL source trees.
  7. *
  8. * Copyright (C) 2017 Linus Walleij <linus.walleij@linaro.org>
  9. * Copyright (C) 2009-2010 Gabor Juhos <juhosg@openwrt.org>
  10. * Copyright (C) 2010 Antti Seppälä <a.seppala@gmail.com>
  11. * Copyright (C) 2010 Roman Yeryomin <roman@advem.lv>
  12. * Copyright (C) 2011 Colin Leitner <colin.leitner@googlemail.com>
  13. */
  14. #include <linux/bitops.h>
  15. #include <linux/etherdevice.h>
  16. #include <linux/if_bridge.h>
  17. #include <linux/if_vlan.h>
  18. #include <linux/interrupt.h>
  19. #include <linux/irqdomain.h>
  20. #include <linux/irqchip/chained_irq.h>
  21. #include <linux/of_irq.h>
  22. #include <linux/regmap.h>
  23. #include "realtek.h"
  24. #include "realtek-smi.h"
  25. #include "realtek-mdio.h"
  26. #include "rtl83xx.h"
  27. #include "rtl8366rb.h"
  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(a) ((a) << 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_16000 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. /* Switch per-port learning disablement register */
  42. #define RTL8366RB_PORT_LEARNDIS_CTRL 0x0002
  43. /* Security control, actually aging register */
  44. #define RTL8366RB_SECURITY_CTRL 0x0003
  45. #define RTL8366RB_SSCR2 0x0004
  46. #define RTL8366RB_SSCR2_DROP_UNKNOWN_DA BIT(0)
  47. /* Port Mode Control registers */
  48. #define RTL8366RB_PMC0 0x0005
  49. #define RTL8366RB_PMC0_SPI BIT(0)
  50. #define RTL8366RB_PMC0_EN_AUTOLOAD BIT(1)
  51. #define RTL8366RB_PMC0_PROBE BIT(2)
  52. #define RTL8366RB_PMC0_DIS_BISR BIT(3)
  53. #define RTL8366RB_PMC0_ADCTEST BIT(4)
  54. #define RTL8366RB_PMC0_SRAM_DIAG BIT(5)
  55. #define RTL8366RB_PMC0_EN_SCAN BIT(6)
  56. #define RTL8366RB_PMC0_P4_IOMODE_SHIFT 7
  57. #define RTL8366RB_PMC0_P4_IOMODE_MASK GENMASK(9, 7)
  58. #define RTL8366RB_PMC0_P5_IOMODE_SHIFT 10
  59. #define RTL8366RB_PMC0_P5_IOMODE_MASK GENMASK(12, 10)
  60. #define RTL8366RB_PMC0_SDSMODE_SHIFT 13
  61. #define RTL8366RB_PMC0_SDSMODE_MASK GENMASK(15, 13)
  62. #define RTL8366RB_PMC1 0x0006
  63. /* Port Mirror Control Register */
  64. #define RTL8366RB_PMCR 0x0007
  65. #define RTL8366RB_PMCR_SOURCE_PORT(a) (a)
  66. #define RTL8366RB_PMCR_SOURCE_PORT_MASK 0x000f
  67. #define RTL8366RB_PMCR_MONITOR_PORT(a) ((a) << 4)
  68. #define RTL8366RB_PMCR_MONITOR_PORT_MASK 0x00f0
  69. #define RTL8366RB_PMCR_MIRROR_RX BIT(8)
  70. #define RTL8366RB_PMCR_MIRROR_TX BIT(9)
  71. #define RTL8366RB_PMCR_MIRROR_SPC BIT(10)
  72. #define RTL8366RB_PMCR_MIRROR_ISO BIT(11)
  73. /* bits 0..7 = port 0, bits 8..15 = port 1 */
  74. #define RTL8366RB_PAACR0 0x0010
  75. /* bits 0..7 = port 2, bits 8..15 = port 3 */
  76. #define RTL8366RB_PAACR1 0x0011
  77. /* bits 0..7 = port 4, bits 8..15 = port 5 */
  78. #define RTL8366RB_PAACR2 0x0012
  79. #define RTL8366RB_PAACR_SPEED_10M 0
  80. #define RTL8366RB_PAACR_SPEED_100M 1
  81. #define RTL8366RB_PAACR_SPEED_1000M 2
  82. #define RTL8366RB_PAACR_FULL_DUPLEX BIT(2)
  83. #define RTL8366RB_PAACR_LINK_UP BIT(4)
  84. #define RTL8366RB_PAACR_TX_PAUSE BIT(5)
  85. #define RTL8366RB_PAACR_RX_PAUSE BIT(6)
  86. #define RTL8366RB_PAACR_AN BIT(7)
  87. /* bits 0..7 = port 0, bits 8..15 = port 1 */
  88. #define RTL8366RB_PSTAT0 0x0014
  89. /* bits 0..7 = port 2, bits 8..15 = port 3 */
  90. #define RTL8366RB_PSTAT1 0x0015
  91. /* bits 0..7 = port 4, bits 8..15 = port 5 */
  92. #define RTL8366RB_PSTAT2 0x0016
  93. #define RTL8366RB_POWER_SAVING_REG 0x0021
  94. /* Spanning tree status (STP) control, two bits per port per FID */
  95. #define RTL8366RB_STP_STATE_BASE 0x0050 /* 0x0050..0x0057 */
  96. #define RTL8366RB_STP_STATE_DISABLED 0x0
  97. #define RTL8366RB_STP_STATE_BLOCKING 0x1
  98. #define RTL8366RB_STP_STATE_LEARNING 0x2
  99. #define RTL8366RB_STP_STATE_FORWARDING 0x3
  100. #define RTL8366RB_STP_MASK GENMASK(1, 0)
  101. #define RTL8366RB_STP_STATE(port, state) \
  102. ((state) << ((port) * 2))
  103. #define RTL8366RB_STP_STATE_MASK(port) \
  104. RTL8366RB_STP_STATE((port), RTL8366RB_STP_MASK)
  105. /* CPU port control reg */
  106. #define RTL8366RB_CPU_CTRL_REG 0x0061
  107. #define RTL8366RB_CPU_PORTS_MSK 0x00FF
  108. /* Disables inserting custom tag length/type 0x8899 */
  109. #define RTL8366RB_CPU_NO_TAG BIT(15)
  110. #define RTL8366RB_CPU_TAG_SIZE 4
  111. #define RTL8366RB_SMAR0 0x0070 /* bits 0..15 */
  112. #define RTL8366RB_SMAR1 0x0071 /* bits 16..31 */
  113. #define RTL8366RB_SMAR2 0x0072 /* bits 32..47 */
  114. #define RTL8366RB_RESET_CTRL_REG 0x0100
  115. #define RTL8366RB_CHIP_CTRL_RESET_HW BIT(0)
  116. #define RTL8366RB_CHIP_CTRL_RESET_SW BIT(1)
  117. #define RTL8366RB_CHIP_ID_REG 0x0509
  118. #define RTL8366RB_CHIP_ID_8366 0x5937
  119. #define RTL8366RB_CHIP_VERSION_CTRL_REG 0x050A
  120. #define RTL8366RB_CHIP_VERSION_MASK 0xf
  121. /* PHY registers control */
  122. #define RTL8366RB_PHY_ACCESS_CTRL_REG 0x8000
  123. #define RTL8366RB_PHY_CTRL_READ BIT(0)
  124. #define RTL8366RB_PHY_CTRL_WRITE 0
  125. #define RTL8366RB_PHY_ACCESS_BUSY_REG 0x8001
  126. #define RTL8366RB_PHY_INT_BUSY BIT(0)
  127. #define RTL8366RB_PHY_EXT_BUSY BIT(4)
  128. #define RTL8366RB_PHY_ACCESS_DATA_REG 0x8002
  129. #define RTL8366RB_PHY_EXT_CTRL_REG 0x8010
  130. #define RTL8366RB_PHY_EXT_WRDATA_REG 0x8011
  131. #define RTL8366RB_PHY_EXT_RDDATA_REG 0x8012
  132. #define RTL8366RB_PHY_REG_MASK 0x1f
  133. #define RTL8366RB_PHY_PAGE_OFFSET 5
  134. #define RTL8366RB_PHY_PAGE_MASK (0xf << 5)
  135. #define RTL8366RB_PHY_NO_OFFSET 9
  136. #define RTL8366RB_PHY_NO_MASK (0x1f << 9)
  137. /* VLAN Ingress Control Register 1, one bit per port.
  138. * bit 0 .. 5 will make the switch drop ingress frames without
  139. * VID such as untagged or priority-tagged frames for respective
  140. * port.
  141. * bit 6 .. 11 will make the switch drop ingress frames carrying
  142. * a C-tag with VID != 0 for respective port.
  143. */
  144. #define RTL8366RB_VLAN_INGRESS_CTRL1_REG 0x037E
  145. #define RTL8366RB_VLAN_INGRESS_CTRL1_DROP(port) (BIT((port)) | BIT((port) + 6))
  146. /* VLAN Ingress Control Register 2, one bit per port.
  147. * bit0 .. bit5 will make the switch drop all ingress frames with
  148. * a VLAN classification that does not include the port is in its
  149. * member set.
  150. */
  151. #define RTL8366RB_VLAN_INGRESS_CTRL2_REG 0x037f
  152. #define RTL8366RB_MIB_COUNT 33
  153. #define RTL8366RB_GLOBAL_MIB_COUNT 1
  154. #define RTL8366RB_MIB_COUNTER_PORT_OFFSET 0x0050
  155. #define RTL8366RB_MIB_COUNTER_BASE 0x1000
  156. #define RTL8366RB_MIB_CTRL_REG 0x13F0
  157. #define RTL8366RB_MIB_CTRL_USER_MASK 0x0FFC
  158. #define RTL8366RB_MIB_CTRL_BUSY_MASK BIT(0)
  159. #define RTL8366RB_MIB_CTRL_RESET_MASK BIT(1)
  160. #define RTL8366RB_MIB_CTRL_PORT_RESET(_p) BIT(2 + (_p))
  161. #define RTL8366RB_MIB_CTRL_GLOBAL_RESET BIT(11)
  162. #define RTL8366RB_PORT_VLAN_CTRL_BASE 0x0063
  163. #define RTL8366RB_PORT_VLAN_CTRL_REG(_p) \
  164. (RTL8366RB_PORT_VLAN_CTRL_BASE + (_p) / 4)
  165. #define RTL8366RB_PORT_VLAN_CTRL_MASK 0xf
  166. #define RTL8366RB_PORT_VLAN_CTRL_SHIFT(_p) (4 * ((_p) % 4))
  167. #define RTL8366RB_VLAN_TABLE_READ_BASE 0x018C
  168. #define RTL8366RB_VLAN_TABLE_WRITE_BASE 0x0185
  169. #define RTL8366RB_TABLE_ACCESS_CTRL_REG 0x0180
  170. #define RTL8366RB_TABLE_VLAN_READ_CTRL 0x0E01
  171. #define RTL8366RB_TABLE_VLAN_WRITE_CTRL 0x0F01
  172. #define RTL8366RB_VLAN_MC_BASE(_x) (0x0020 + (_x) * 3)
  173. #define RTL8366RB_PORT_LINK_STATUS_BASE 0x0014
  174. #define RTL8366RB_PORT_STATUS_SPEED_MASK 0x0003
  175. #define RTL8366RB_PORT_STATUS_DUPLEX_MASK 0x0004
  176. #define RTL8366RB_PORT_STATUS_LINK_MASK 0x0010
  177. #define RTL8366RB_PORT_STATUS_TXPAUSE_MASK 0x0020
  178. #define RTL8366RB_PORT_STATUS_RXPAUSE_MASK 0x0040
  179. #define RTL8366RB_PORT_STATUS_AN_MASK 0x0080
  180. #define RTL8366RB_NUM_VLANS 16
  181. #define RTL8366RB_NUM_VIDS 4096
  182. #define RTL8366RB_PRIORITYMAX 7
  183. #define RTL8366RB_NUM_FIDS 8
  184. #define RTL8366RB_FIDMAX 7
  185. #define RTL8366RB_PORT_1 BIT(0) /* In userspace port 0 */
  186. #define RTL8366RB_PORT_2 BIT(1) /* In userspace port 1 */
  187. #define RTL8366RB_PORT_3 BIT(2) /* In userspace port 2 */
  188. #define RTL8366RB_PORT_4 BIT(3) /* In userspace port 3 */
  189. #define RTL8366RB_PORT_5 BIT(4) /* In userspace port 4 */
  190. #define RTL8366RB_PORT_CPU BIT(5) /* CPU port */
  191. #define RTL8366RB_PORT_ALL (RTL8366RB_PORT_1 | \
  192. RTL8366RB_PORT_2 | \
  193. RTL8366RB_PORT_3 | \
  194. RTL8366RB_PORT_4 | \
  195. RTL8366RB_PORT_5 | \
  196. RTL8366RB_PORT_CPU)
  197. #define RTL8366RB_PORT_ALL_BUT_CPU (RTL8366RB_PORT_1 | \
  198. RTL8366RB_PORT_2 | \
  199. RTL8366RB_PORT_3 | \
  200. RTL8366RB_PORT_4 | \
  201. RTL8366RB_PORT_5)
  202. #define RTL8366RB_PORT_ALL_EXTERNAL (RTL8366RB_PORT_1 | \
  203. RTL8366RB_PORT_2 | \
  204. RTL8366RB_PORT_3 | \
  205. RTL8366RB_PORT_4)
  206. #define RTL8366RB_PORT_ALL_INTERNAL RTL8366RB_PORT_CPU
  207. /* First configuration word per member config, VID and prio */
  208. #define RTL8366RB_VLAN_VID_MASK 0xfff
  209. #define RTL8366RB_VLAN_PRIORITY_SHIFT 12
  210. #define RTL8366RB_VLAN_PRIORITY_MASK 0x7
  211. /* Second configuration word per member config, member and untagged */
  212. #define RTL8366RB_VLAN_UNTAG_SHIFT 8
  213. #define RTL8366RB_VLAN_UNTAG_MASK 0xff
  214. #define RTL8366RB_VLAN_MEMBER_MASK 0xff
  215. /* Third config word per member config, STAG currently unused */
  216. #define RTL8366RB_VLAN_STAG_MBR_MASK 0xff
  217. #define RTL8366RB_VLAN_STAG_MBR_SHIFT 8
  218. #define RTL8366RB_VLAN_STAG_IDX_MASK 0x7
  219. #define RTL8366RB_VLAN_STAG_IDX_SHIFT 5
  220. #define RTL8366RB_VLAN_FID_MASK 0x7
  221. /* Port ingress bandwidth control */
  222. #define RTL8366RB_IB_BASE 0x0200
  223. #define RTL8366RB_IB_REG(pnum) (RTL8366RB_IB_BASE + (pnum))
  224. #define RTL8366RB_IB_BDTH_MASK 0x3fff
  225. #define RTL8366RB_IB_PREIFG BIT(14)
  226. /* Port egress bandwidth control */
  227. #define RTL8366RB_EB_BASE 0x02d1
  228. #define RTL8366RB_EB_REG(pnum) (RTL8366RB_EB_BASE + (pnum))
  229. #define RTL8366RB_EB_BDTH_MASK 0x3fff
  230. #define RTL8366RB_EB_PREIFG_REG 0x02f8
  231. #define RTL8366RB_EB_PREIFG BIT(9)
  232. #define RTL8366RB_BDTH_SW_MAX 1048512 /* 1048576? */
  233. #define RTL8366RB_BDTH_UNIT 64
  234. #define RTL8366RB_BDTH_REG_DEFAULT 16383
  235. /* QOS */
  236. #define RTL8366RB_QOS BIT(15)
  237. /* Include/Exclude Preamble and IFG (20 bytes). 0:Exclude, 1:Include. */
  238. #define RTL8366RB_QOS_DEFAULT_PREIFG 1
  239. /* Interrupt handling */
  240. #define RTL8366RB_INTERRUPT_CONTROL_REG 0x0440
  241. #define RTL8366RB_INTERRUPT_POLARITY BIT(0)
  242. #define RTL8366RB_P4_RGMII_LED BIT(2)
  243. #define RTL8366RB_INTERRUPT_MASK_REG 0x0441
  244. #define RTL8366RB_INTERRUPT_LINK_CHGALL GENMASK(11, 0)
  245. #define RTL8366RB_INTERRUPT_ACLEXCEED BIT(8)
  246. #define RTL8366RB_INTERRUPT_STORMEXCEED BIT(9)
  247. #define RTL8366RB_INTERRUPT_P4_FIBER BIT(12)
  248. #define RTL8366RB_INTERRUPT_P4_UTP BIT(13)
  249. #define RTL8366RB_INTERRUPT_VALID (RTL8366RB_INTERRUPT_LINK_CHGALL | \
  250. RTL8366RB_INTERRUPT_ACLEXCEED | \
  251. RTL8366RB_INTERRUPT_STORMEXCEED | \
  252. RTL8366RB_INTERRUPT_P4_FIBER | \
  253. RTL8366RB_INTERRUPT_P4_UTP)
  254. #define RTL8366RB_INTERRUPT_STATUS_REG 0x0442
  255. #define RTL8366RB_NUM_INTERRUPT 14 /* 0..13 */
  256. /* Port isolation registers */
  257. #define RTL8366RB_PORT_ISO_BASE 0x0F08
  258. #define RTL8366RB_PORT_ISO(pnum) (RTL8366RB_PORT_ISO_BASE + (pnum))
  259. #define RTL8366RB_PORT_ISO_EN BIT(0)
  260. #define RTL8366RB_PORT_ISO_PORTS_MASK GENMASK(7, 1)
  261. #define RTL8366RB_PORT_ISO_PORTS(pmask) ((pmask) << 1)
  262. /* bits 0..5 enable force when cleared */
  263. #define RTL8366RB_MAC_FORCE_CTRL_REG 0x0F11
  264. #define RTL8366RB_OAM_PARSER_REG 0x0F14
  265. #define RTL8366RB_OAM_MULTIPLEXER_REG 0x0F15
  266. #define RTL8366RB_GREEN_FEATURE_REG 0x0F51
  267. #define RTL8366RB_GREEN_FEATURE_MSK 0x0007
  268. #define RTL8366RB_GREEN_FEATURE_TX BIT(0)
  269. #define RTL8366RB_GREEN_FEATURE_RX BIT(2)
  270. static struct rtl8366_mib_counter rtl8366rb_mib_counters[] = {
  271. { 0, 0, 4, "IfInOctets" },
  272. { 0, 4, 4, "EtherStatsOctets" },
  273. { 0, 8, 2, "EtherStatsUnderSizePkts" },
  274. { 0, 10, 2, "EtherFragments" },
  275. { 0, 12, 2, "EtherStatsPkts64Octets" },
  276. { 0, 14, 2, "EtherStatsPkts65to127Octets" },
  277. { 0, 16, 2, "EtherStatsPkts128to255Octets" },
  278. { 0, 18, 2, "EtherStatsPkts256to511Octets" },
  279. { 0, 20, 2, "EtherStatsPkts512to1023Octets" },
  280. { 0, 22, 2, "EtherStatsPkts1024to1518Octets" },
  281. { 0, 24, 2, "EtherOversizeStats" },
  282. { 0, 26, 2, "EtherStatsJabbers" },
  283. { 0, 28, 2, "IfInUcastPkts" },
  284. { 0, 30, 2, "EtherStatsMulticastPkts" },
  285. { 0, 32, 2, "EtherStatsBroadcastPkts" },
  286. { 0, 34, 2, "EtherStatsDropEvents" },
  287. { 0, 36, 2, "Dot3StatsFCSErrors" },
  288. { 0, 38, 2, "Dot3StatsSymbolErrors" },
  289. { 0, 40, 2, "Dot3InPauseFrames" },
  290. { 0, 42, 2, "Dot3ControlInUnknownOpcodes" },
  291. { 0, 44, 4, "IfOutOctets" },
  292. { 0, 48, 2, "Dot3StatsSingleCollisionFrames" },
  293. { 0, 50, 2, "Dot3StatMultipleCollisionFrames" },
  294. { 0, 52, 2, "Dot3sDeferredTransmissions" },
  295. { 0, 54, 2, "Dot3StatsLateCollisions" },
  296. { 0, 56, 2, "EtherStatsCollisions" },
  297. { 0, 58, 2, "Dot3StatsExcessiveCollisions" },
  298. { 0, 60, 2, "Dot3OutPauseFrames" },
  299. { 0, 62, 2, "Dot1dBasePortDelayExceededDiscards" },
  300. { 0, 64, 2, "Dot1dTpPortInDiscards" },
  301. { 0, 66, 2, "IfOutUcastPkts" },
  302. { 0, 68, 2, "IfOutMulticastPkts" },
  303. { 0, 70, 2, "IfOutBroadcastPkts" },
  304. };
  305. static int rtl8366rb_get_mib_counter(struct realtek_priv *priv,
  306. int port,
  307. struct rtl8366_mib_counter *mib,
  308. u64 *mibvalue)
  309. {
  310. u32 addr, val;
  311. int ret;
  312. int i;
  313. addr = RTL8366RB_MIB_COUNTER_BASE +
  314. RTL8366RB_MIB_COUNTER_PORT_OFFSET * (port) +
  315. mib->offset;
  316. /* Writing access counter address first
  317. * then ASIC will prepare 64bits counter wait for being retrived
  318. */
  319. ret = regmap_write(priv->map, addr, 0); /* Write whatever */
  320. if (ret)
  321. return ret;
  322. /* Read MIB control register */
  323. ret = regmap_read(priv->map, RTL8366RB_MIB_CTRL_REG, &val);
  324. if (ret)
  325. return -EIO;
  326. if (val & RTL8366RB_MIB_CTRL_BUSY_MASK)
  327. return -EBUSY;
  328. if (val & RTL8366RB_MIB_CTRL_RESET_MASK)
  329. return -EIO;
  330. /* Read each individual MIB 16 bits at the time */
  331. *mibvalue = 0;
  332. for (i = mib->length; i > 0; i--) {
  333. ret = regmap_read(priv->map, addr + (i - 1), &val);
  334. if (ret)
  335. return ret;
  336. *mibvalue = (*mibvalue << 16) | (val & 0xFFFF);
  337. }
  338. return 0;
  339. }
  340. static u32 rtl8366rb_get_irqmask(struct irq_data *d)
  341. {
  342. int line = irqd_to_hwirq(d);
  343. u32 val;
  344. /* For line interrupts we combine link down in bits
  345. * 6..11 with link up in bits 0..5 into one interrupt.
  346. */
  347. if (line < 12)
  348. val = BIT(line) | BIT(line + 6);
  349. else
  350. val = BIT(line);
  351. return val;
  352. }
  353. static void rtl8366rb_mask_irq(struct irq_data *d)
  354. {
  355. struct realtek_priv *priv = irq_data_get_irq_chip_data(d);
  356. int ret;
  357. ret = regmap_update_bits(priv->map, RTL8366RB_INTERRUPT_MASK_REG,
  358. rtl8366rb_get_irqmask(d), 0);
  359. if (ret)
  360. dev_err(priv->dev, "could not mask IRQ\n");
  361. }
  362. static void rtl8366rb_unmask_irq(struct irq_data *d)
  363. {
  364. struct realtek_priv *priv = irq_data_get_irq_chip_data(d);
  365. int ret;
  366. ret = regmap_update_bits(priv->map, RTL8366RB_INTERRUPT_MASK_REG,
  367. rtl8366rb_get_irqmask(d),
  368. rtl8366rb_get_irqmask(d));
  369. if (ret)
  370. dev_err(priv->dev, "could not unmask IRQ\n");
  371. }
  372. static irqreturn_t rtl8366rb_irq(int irq, void *data)
  373. {
  374. struct realtek_priv *priv = data;
  375. u32 stat;
  376. int ret;
  377. /* This clears the IRQ status register */
  378. ret = regmap_read(priv->map, RTL8366RB_INTERRUPT_STATUS_REG,
  379. &stat);
  380. if (ret) {
  381. dev_err(priv->dev, "can't read interrupt status\n");
  382. return IRQ_NONE;
  383. }
  384. stat &= RTL8366RB_INTERRUPT_VALID;
  385. if (!stat)
  386. return IRQ_NONE;
  387. while (stat) {
  388. int line = __ffs(stat);
  389. int child_irq;
  390. stat &= ~BIT(line);
  391. /* For line interrupts we combine link down in bits
  392. * 6..11 with link up in bits 0..5 into one interrupt.
  393. */
  394. if (line < 12 && line > 5)
  395. line -= 5;
  396. child_irq = irq_find_mapping(priv->irqdomain, line);
  397. handle_nested_irq(child_irq);
  398. }
  399. return IRQ_HANDLED;
  400. }
  401. static struct irq_chip rtl8366rb_irq_chip = {
  402. .name = "RTL8366RB",
  403. .irq_mask = rtl8366rb_mask_irq,
  404. .irq_unmask = rtl8366rb_unmask_irq,
  405. };
  406. static int rtl8366rb_irq_map(struct irq_domain *domain, unsigned int irq,
  407. irq_hw_number_t hwirq)
  408. {
  409. irq_set_chip_data(irq, domain->host_data);
  410. irq_set_chip_and_handler(irq, &rtl8366rb_irq_chip, handle_simple_irq);
  411. irq_set_nested_thread(irq, 1);
  412. irq_set_noprobe(irq);
  413. return 0;
  414. }
  415. static void rtl8366rb_irq_unmap(struct irq_domain *d, unsigned int irq)
  416. {
  417. irq_set_nested_thread(irq, 0);
  418. irq_set_chip_and_handler(irq, NULL, NULL);
  419. irq_set_chip_data(irq, NULL);
  420. }
  421. static const struct irq_domain_ops rtl8366rb_irqdomain_ops = {
  422. .map = rtl8366rb_irq_map,
  423. .unmap = rtl8366rb_irq_unmap,
  424. .xlate = irq_domain_xlate_onecell,
  425. };
  426. static int rtl8366rb_setup_cascaded_irq(struct realtek_priv *priv)
  427. {
  428. struct device_node *intc;
  429. unsigned long irq_trig;
  430. int irq;
  431. int ret;
  432. u32 val;
  433. int i;
  434. intc = of_get_child_by_name(priv->dev->of_node, "interrupt-controller");
  435. if (!intc) {
  436. dev_err(priv->dev, "missing child interrupt-controller node\n");
  437. return -EINVAL;
  438. }
  439. /* RB8366RB IRQs cascade off this one */
  440. irq = of_irq_get(intc, 0);
  441. if (irq <= 0) {
  442. dev_err(priv->dev, "failed to get parent IRQ\n");
  443. ret = irq ? irq : -EINVAL;
  444. goto out_put_node;
  445. }
  446. /* This clears the IRQ status register */
  447. ret = regmap_read(priv->map, RTL8366RB_INTERRUPT_STATUS_REG,
  448. &val);
  449. if (ret) {
  450. dev_err(priv->dev, "can't read interrupt status\n");
  451. goto out_put_node;
  452. }
  453. /* Fetch IRQ edge information from the descriptor */
  454. irq_trig = irq_get_trigger_type(irq);
  455. switch (irq_trig) {
  456. case IRQF_TRIGGER_RISING:
  457. case IRQF_TRIGGER_HIGH:
  458. dev_info(priv->dev, "active high/rising IRQ\n");
  459. val = 0;
  460. break;
  461. case IRQF_TRIGGER_FALLING:
  462. case IRQF_TRIGGER_LOW:
  463. dev_info(priv->dev, "active low/falling IRQ\n");
  464. val = RTL8366RB_INTERRUPT_POLARITY;
  465. break;
  466. }
  467. ret = regmap_update_bits(priv->map, RTL8366RB_INTERRUPT_CONTROL_REG,
  468. RTL8366RB_INTERRUPT_POLARITY,
  469. val);
  470. if (ret) {
  471. dev_err(priv->dev, "could not configure IRQ polarity\n");
  472. goto out_put_node;
  473. }
  474. ret = devm_request_threaded_irq(priv->dev, irq, NULL,
  475. rtl8366rb_irq, IRQF_ONESHOT,
  476. "RTL8366RB", priv);
  477. if (ret) {
  478. dev_err(priv->dev, "unable to request irq: %d\n", ret);
  479. goto out_put_node;
  480. }
  481. priv->irqdomain = irq_domain_add_linear(intc,
  482. RTL8366RB_NUM_INTERRUPT,
  483. &rtl8366rb_irqdomain_ops,
  484. priv);
  485. if (!priv->irqdomain) {
  486. dev_err(priv->dev, "failed to create IRQ domain\n");
  487. ret = -EINVAL;
  488. goto out_put_node;
  489. }
  490. for (i = 0; i < priv->num_ports; i++)
  491. irq_set_parent(irq_create_mapping(priv->irqdomain, i), irq);
  492. out_put_node:
  493. of_node_put(intc);
  494. return ret;
  495. }
  496. static int rtl8366rb_set_addr(struct realtek_priv *priv)
  497. {
  498. u8 addr[ETH_ALEN];
  499. u16 val;
  500. int ret;
  501. eth_random_addr(addr);
  502. dev_info(priv->dev, "set MAC: %02X:%02X:%02X:%02X:%02X:%02X\n",
  503. addr[0], addr[1], addr[2], addr[3], addr[4], addr[5]);
  504. val = addr[0] << 8 | addr[1];
  505. ret = regmap_write(priv->map, RTL8366RB_SMAR0, val);
  506. if (ret)
  507. return ret;
  508. val = addr[2] << 8 | addr[3];
  509. ret = regmap_write(priv->map, RTL8366RB_SMAR1, val);
  510. if (ret)
  511. return ret;
  512. val = addr[4] << 8 | addr[5];
  513. ret = regmap_write(priv->map, RTL8366RB_SMAR2, val);
  514. if (ret)
  515. return ret;
  516. return 0;
  517. }
  518. /* Found in a vendor driver */
  519. /* Struct for handling the jam tables' entries */
  520. struct rtl8366rb_jam_tbl_entry {
  521. u16 reg;
  522. u16 val;
  523. };
  524. /* For the "version 0" early silicon, appear in most source releases */
  525. static const struct rtl8366rb_jam_tbl_entry rtl8366rb_init_jam_ver_0[] = {
  526. {0x000B, 0x0001}, {0x03A6, 0x0100}, {0x03A7, 0x0001}, {0x02D1, 0x3FFF},
  527. {0x02D2, 0x3FFF}, {0x02D3, 0x3FFF}, {0x02D4, 0x3FFF}, {0x02D5, 0x3FFF},
  528. {0x02D6, 0x3FFF}, {0x02D7, 0x3FFF}, {0x02D8, 0x3FFF}, {0x022B, 0x0688},
  529. {0x022C, 0x0FAC}, {0x03D0, 0x4688}, {0x03D1, 0x01F5}, {0x0000, 0x0830},
  530. {0x02F9, 0x0200}, {0x02F7, 0x7FFF}, {0x02F8, 0x03FF}, {0x0080, 0x03E8},
  531. {0x0081, 0x00CE}, {0x0082, 0x00DA}, {0x0083, 0x0230}, {0xBE0F, 0x2000},
  532. {0x0231, 0x422A}, {0x0232, 0x422A}, {0x0233, 0x422A}, {0x0234, 0x422A},
  533. {0x0235, 0x422A}, {0x0236, 0x422A}, {0x0237, 0x422A}, {0x0238, 0x422A},
  534. {0x0239, 0x422A}, {0x023A, 0x422A}, {0x023B, 0x422A}, {0x023C, 0x422A},
  535. {0x023D, 0x422A}, {0x023E, 0x422A}, {0x023F, 0x422A}, {0x0240, 0x422A},
  536. {0x0241, 0x422A}, {0x0242, 0x422A}, {0x0243, 0x422A}, {0x0244, 0x422A},
  537. {0x0245, 0x422A}, {0x0246, 0x422A}, {0x0247, 0x422A}, {0x0248, 0x422A},
  538. {0x0249, 0x0146}, {0x024A, 0x0146}, {0x024B, 0x0146}, {0xBE03, 0xC961},
  539. {0x024D, 0x0146}, {0x024E, 0x0146}, {0x024F, 0x0146}, {0x0250, 0x0146},
  540. {0xBE64, 0x0226}, {0x0252, 0x0146}, {0x0253, 0x0146}, {0x024C, 0x0146},
  541. {0x0251, 0x0146}, {0x0254, 0x0146}, {0xBE62, 0x3FD0}, {0x0084, 0x0320},
  542. {0x0255, 0x0146}, {0x0256, 0x0146}, {0x0257, 0x0146}, {0x0258, 0x0146},
  543. {0x0259, 0x0146}, {0x025A, 0x0146}, {0x025B, 0x0146}, {0x025C, 0x0146},
  544. {0x025D, 0x0146}, {0x025E, 0x0146}, {0x025F, 0x0146}, {0x0260, 0x0146},
  545. {0x0261, 0xA23F}, {0x0262, 0x0294}, {0x0263, 0xA23F}, {0x0264, 0x0294},
  546. {0x0265, 0xA23F}, {0x0266, 0x0294}, {0x0267, 0xA23F}, {0x0268, 0x0294},
  547. {0x0269, 0xA23F}, {0x026A, 0x0294}, {0x026B, 0xA23F}, {0x026C, 0x0294},
  548. {0x026D, 0xA23F}, {0x026E, 0x0294}, {0x026F, 0xA23F}, {0x0270, 0x0294},
  549. {0x02F5, 0x0048}, {0xBE09, 0x0E00}, {0xBE1E, 0x0FA0}, {0xBE14, 0x8448},
  550. {0xBE15, 0x1007}, {0xBE4A, 0xA284}, {0xC454, 0x3F0B}, {0xC474, 0x3F0B},
  551. {0xBE48, 0x3672}, {0xBE4B, 0x17A7}, {0xBE4C, 0x0B15}, {0xBE52, 0x0EDD},
  552. {0xBE49, 0x8C00}, {0xBE5B, 0x785C}, {0xBE5C, 0x785C}, {0xBE5D, 0x785C},
  553. {0xBE61, 0x368A}, {0xBE63, 0x9B84}, {0xC456, 0xCC13}, {0xC476, 0xCC13},
  554. {0xBE65, 0x307D}, {0xBE6D, 0x0005}, {0xBE6E, 0xE120}, {0xBE2E, 0x7BAF},
  555. };
  556. /* This v1 init sequence is from Belkin F5D8235 U-Boot release */
  557. static const struct rtl8366rb_jam_tbl_entry rtl8366rb_init_jam_ver_1[] = {
  558. {0x0000, 0x0830}, {0x0001, 0x8000}, {0x0400, 0x8130}, {0xBE78, 0x3C3C},
  559. {0x0431, 0x5432}, {0xBE37, 0x0CE4}, {0x02FA, 0xFFDF}, {0x02FB, 0xFFE0},
  560. {0xC44C, 0x1585}, {0xC44C, 0x1185}, {0xC44C, 0x1585}, {0xC46C, 0x1585},
  561. {0xC46C, 0x1185}, {0xC46C, 0x1585}, {0xC451, 0x2135}, {0xC471, 0x2135},
  562. {0xBE10, 0x8140}, {0xBE15, 0x0007}, {0xBE6E, 0xE120}, {0xBE69, 0xD20F},
  563. {0xBE6B, 0x0320}, {0xBE24, 0xB000}, {0xBE23, 0xFF51}, {0xBE22, 0xDF20},
  564. {0xBE21, 0x0140}, {0xBE20, 0x00BB}, {0xBE24, 0xB800}, {0xBE24, 0x0000},
  565. {0xBE24, 0x7000}, {0xBE23, 0xFF51}, {0xBE22, 0xDF60}, {0xBE21, 0x0140},
  566. {0xBE20, 0x0077}, {0xBE24, 0x7800}, {0xBE24, 0x0000}, {0xBE2E, 0x7B7A},
  567. {0xBE36, 0x0CE4}, {0x02F5, 0x0048}, {0xBE77, 0x2940}, {0x000A, 0x83E0},
  568. {0xBE79, 0x3C3C}, {0xBE00, 0x1340},
  569. };
  570. /* This v2 init sequence is from Belkin F5D8235 U-Boot release */
  571. static const struct rtl8366rb_jam_tbl_entry rtl8366rb_init_jam_ver_2[] = {
  572. {0x0450, 0x0000}, {0x0400, 0x8130}, {0x000A, 0x83ED}, {0x0431, 0x5432},
  573. {0xC44F, 0x6250}, {0xC46F, 0x6250}, {0xC456, 0x0C14}, {0xC476, 0x0C14},
  574. {0xC44C, 0x1C85}, {0xC44C, 0x1885}, {0xC44C, 0x1C85}, {0xC46C, 0x1C85},
  575. {0xC46C, 0x1885}, {0xC46C, 0x1C85}, {0xC44C, 0x0885}, {0xC44C, 0x0881},
  576. {0xC44C, 0x0885}, {0xC46C, 0x0885}, {0xC46C, 0x0881}, {0xC46C, 0x0885},
  577. {0xBE2E, 0x7BA7}, {0xBE36, 0x1000}, {0xBE37, 0x1000}, {0x8000, 0x0001},
  578. {0xBE69, 0xD50F}, {0x8000, 0x0000}, {0xBE69, 0xD50F}, {0xBE6E, 0x0320},
  579. {0xBE77, 0x2940}, {0xBE78, 0x3C3C}, {0xBE79, 0x3C3C}, {0xBE6E, 0xE120},
  580. {0x8000, 0x0001}, {0xBE15, 0x1007}, {0x8000, 0x0000}, {0xBE15, 0x1007},
  581. {0xBE14, 0x0448}, {0xBE1E, 0x00A0}, {0xBE10, 0x8160}, {0xBE10, 0x8140},
  582. {0xBE00, 0x1340}, {0x0F51, 0x0010},
  583. };
  584. /* Appears in a DDWRT code dump */
  585. static const struct rtl8366rb_jam_tbl_entry rtl8366rb_init_jam_ver_3[] = {
  586. {0x0000, 0x0830}, {0x0400, 0x8130}, {0x000A, 0x83ED}, {0x0431, 0x5432},
  587. {0x0F51, 0x0017}, {0x02F5, 0x0048}, {0x02FA, 0xFFDF}, {0x02FB, 0xFFE0},
  588. {0xC456, 0x0C14}, {0xC476, 0x0C14}, {0xC454, 0x3F8B}, {0xC474, 0x3F8B},
  589. {0xC450, 0x2071}, {0xC470, 0x2071}, {0xC451, 0x226B}, {0xC471, 0x226B},
  590. {0xC452, 0xA293}, {0xC472, 0xA293}, {0xC44C, 0x1585}, {0xC44C, 0x1185},
  591. {0xC44C, 0x1585}, {0xC46C, 0x1585}, {0xC46C, 0x1185}, {0xC46C, 0x1585},
  592. {0xC44C, 0x0185}, {0xC44C, 0x0181}, {0xC44C, 0x0185}, {0xC46C, 0x0185},
  593. {0xC46C, 0x0181}, {0xC46C, 0x0185}, {0xBE24, 0xB000}, {0xBE23, 0xFF51},
  594. {0xBE22, 0xDF20}, {0xBE21, 0x0140}, {0xBE20, 0x00BB}, {0xBE24, 0xB800},
  595. {0xBE24, 0x0000}, {0xBE24, 0x7000}, {0xBE23, 0xFF51}, {0xBE22, 0xDF60},
  596. {0xBE21, 0x0140}, {0xBE20, 0x0077}, {0xBE24, 0x7800}, {0xBE24, 0x0000},
  597. {0xBE2E, 0x7BA7}, {0xBE36, 0x1000}, {0xBE37, 0x1000}, {0x8000, 0x0001},
  598. {0xBE69, 0xD50F}, {0x8000, 0x0000}, {0xBE69, 0xD50F}, {0xBE6B, 0x0320},
  599. {0xBE77, 0x2800}, {0xBE78, 0x3C3C}, {0xBE79, 0x3C3C}, {0xBE6E, 0xE120},
  600. {0x8000, 0x0001}, {0xBE10, 0x8140}, {0x8000, 0x0000}, {0xBE10, 0x8140},
  601. {0xBE15, 0x1007}, {0xBE14, 0x0448}, {0xBE1E, 0x00A0}, {0xBE10, 0x8160},
  602. {0xBE10, 0x8140}, {0xBE00, 0x1340}, {0x0450, 0x0000}, {0x0401, 0x0000},
  603. };
  604. /* Belkin F5D8235 v1, "belkin,f5d8235-v1" */
  605. static const struct rtl8366rb_jam_tbl_entry rtl8366rb_init_jam_f5d8235[] = {
  606. {0x0242, 0x02BF}, {0x0245, 0x02BF}, {0x0248, 0x02BF}, {0x024B, 0x02BF},
  607. {0x024E, 0x02BF}, {0x0251, 0x02BF}, {0x0254, 0x0A3F}, {0x0256, 0x0A3F},
  608. {0x0258, 0x0A3F}, {0x025A, 0x0A3F}, {0x025C, 0x0A3F}, {0x025E, 0x0A3F},
  609. {0x0263, 0x007C}, {0x0100, 0x0004}, {0xBE5B, 0x3500}, {0x800E, 0x200F},
  610. {0xBE1D, 0x0F00}, {0x8001, 0x5011}, {0x800A, 0xA2F4}, {0x800B, 0x17A3},
  611. {0xBE4B, 0x17A3}, {0xBE41, 0x5011}, {0xBE17, 0x2100}, {0x8000, 0x8304},
  612. {0xBE40, 0x8304}, {0xBE4A, 0xA2F4}, {0x800C, 0xA8D5}, {0x8014, 0x5500},
  613. {0x8015, 0x0004}, {0xBE4C, 0xA8D5}, {0xBE59, 0x0008}, {0xBE09, 0x0E00},
  614. {0xBE36, 0x1036}, {0xBE37, 0x1036}, {0x800D, 0x00FF}, {0xBE4D, 0x00FF},
  615. };
  616. /* DGN3500, "netgear,dgn3500", "netgear,dgn3500b" */
  617. static const struct rtl8366rb_jam_tbl_entry rtl8366rb_init_jam_dgn3500[] = {
  618. {0x0000, 0x0830}, {0x0400, 0x8130}, {0x000A, 0x83ED}, {0x0F51, 0x0017},
  619. {0x02F5, 0x0048}, {0x02FA, 0xFFDF}, {0x02FB, 0xFFE0}, {0x0450, 0x0000},
  620. {0x0401, 0x0000}, {0x0431, 0x0960},
  621. };
  622. /* This jam table activates "green ethernet", which means low power mode
  623. * and is claimed to detect the cable length and not use more power than
  624. * necessary, and the ports should enter power saving mode 10 seconds after
  625. * a cable is disconnected. Seems to always be the same.
  626. */
  627. static const struct rtl8366rb_jam_tbl_entry rtl8366rb_green_jam[] = {
  628. {0xBE78, 0x323C}, {0xBE77, 0x5000}, {0xBE2E, 0x7BA7},
  629. {0xBE59, 0x3459}, {0xBE5A, 0x745A}, {0xBE5B, 0x785C},
  630. {0xBE5C, 0x785C}, {0xBE6E, 0xE120}, {0xBE79, 0x323C},
  631. };
  632. /* Function that jams the tables in the proper registers */
  633. static int rtl8366rb_jam_table(const struct rtl8366rb_jam_tbl_entry *jam_table,
  634. int jam_size, struct realtek_priv *priv,
  635. bool write_dbg)
  636. {
  637. u32 val;
  638. int ret;
  639. int i;
  640. for (i = 0; i < jam_size; i++) {
  641. if ((jam_table[i].reg & 0xBE00) == 0xBE00) {
  642. ret = regmap_read(priv->map,
  643. RTL8366RB_PHY_ACCESS_BUSY_REG,
  644. &val);
  645. if (ret)
  646. return ret;
  647. if (!(val & RTL8366RB_PHY_INT_BUSY)) {
  648. ret = regmap_write(priv->map,
  649. RTL8366RB_PHY_ACCESS_CTRL_REG,
  650. RTL8366RB_PHY_CTRL_WRITE);
  651. if (ret)
  652. return ret;
  653. }
  654. }
  655. if (write_dbg)
  656. dev_dbg(priv->dev, "jam %04x into register %04x\n",
  657. jam_table[i].val,
  658. jam_table[i].reg);
  659. ret = regmap_write(priv->map,
  660. jam_table[i].reg,
  661. jam_table[i].val);
  662. if (ret)
  663. return ret;
  664. }
  665. return 0;
  666. }
  667. /* This code is used also with LEDs disabled */
  668. int rb8366rb_set_ledgroup_mode(struct realtek_priv *priv,
  669. u8 led_group,
  670. enum rtl8366_ledgroup_mode mode)
  671. {
  672. int ret;
  673. u32 val;
  674. val = mode << RTL8366RB_LED_CTRL_OFFSET(led_group);
  675. ret = regmap_update_bits(priv->map,
  676. RTL8366RB_LED_CTRL_REG,
  677. RTL8366RB_LED_CTRL_MASK(led_group),
  678. val);
  679. if (ret)
  680. return ret;
  681. return 0;
  682. }
  683. /* This code is used also with LEDs disabled */
  684. static int rtl8366rb_setup_all_leds_off(struct realtek_priv *priv)
  685. {
  686. int ret = 0;
  687. int i;
  688. regmap_update_bits(priv->map,
  689. RTL8366RB_INTERRUPT_CONTROL_REG,
  690. RTL8366RB_P4_RGMII_LED,
  691. 0);
  692. for (i = 0; i < RTL8366RB_NUM_LEDGROUPS; i++) {
  693. ret = rb8366rb_set_ledgroup_mode(priv, i,
  694. RTL8366RB_LEDGROUP_OFF);
  695. if (ret)
  696. return ret;
  697. }
  698. return ret;
  699. }
  700. static int rtl8366rb_setup(struct dsa_switch *ds)
  701. {
  702. struct realtek_priv *priv = ds->priv;
  703. const struct rtl8366rb_jam_tbl_entry *jam_table;
  704. struct rtl8366rb *rb;
  705. u32 chip_ver = 0;
  706. u32 chip_id = 0;
  707. int jam_size;
  708. int ret;
  709. int i;
  710. rb = priv->chip_data;
  711. ret = regmap_read(priv->map, RTL8366RB_CHIP_ID_REG, &chip_id);
  712. if (ret) {
  713. dev_err(priv->dev, "unable to read chip id\n");
  714. return ret;
  715. }
  716. switch (chip_id) {
  717. case RTL8366RB_CHIP_ID_8366:
  718. break;
  719. default:
  720. dev_err(priv->dev, "unknown chip id (%04x)\n", chip_id);
  721. return -ENODEV;
  722. }
  723. ret = regmap_read(priv->map, RTL8366RB_CHIP_VERSION_CTRL_REG,
  724. &chip_ver);
  725. if (ret) {
  726. dev_err(priv->dev, "unable to read chip version\n");
  727. return ret;
  728. }
  729. dev_info(priv->dev, "RTL%04x ver %u chip found\n",
  730. chip_id, chip_ver & RTL8366RB_CHIP_VERSION_MASK);
  731. /* Do the init dance using the right jam table */
  732. switch (chip_ver) {
  733. case 0:
  734. jam_table = rtl8366rb_init_jam_ver_0;
  735. jam_size = ARRAY_SIZE(rtl8366rb_init_jam_ver_0);
  736. break;
  737. case 1:
  738. jam_table = rtl8366rb_init_jam_ver_1;
  739. jam_size = ARRAY_SIZE(rtl8366rb_init_jam_ver_1);
  740. break;
  741. case 2:
  742. jam_table = rtl8366rb_init_jam_ver_2;
  743. jam_size = ARRAY_SIZE(rtl8366rb_init_jam_ver_2);
  744. break;
  745. default:
  746. jam_table = rtl8366rb_init_jam_ver_3;
  747. jam_size = ARRAY_SIZE(rtl8366rb_init_jam_ver_3);
  748. break;
  749. }
  750. /* Special jam tables for special routers
  751. * TODO: are these necessary? Maintainers, please test
  752. * without them, using just the off-the-shelf tables.
  753. */
  754. if (of_machine_is_compatible("belkin,f5d8235-v1")) {
  755. jam_table = rtl8366rb_init_jam_f5d8235;
  756. jam_size = ARRAY_SIZE(rtl8366rb_init_jam_f5d8235);
  757. }
  758. if (of_machine_is_compatible("netgear,dgn3500") ||
  759. of_machine_is_compatible("netgear,dgn3500b")) {
  760. jam_table = rtl8366rb_init_jam_dgn3500;
  761. jam_size = ARRAY_SIZE(rtl8366rb_init_jam_dgn3500);
  762. }
  763. ret = rtl8366rb_jam_table(jam_table, jam_size, priv, true);
  764. if (ret)
  765. return ret;
  766. /* Isolate all user ports so they can only send packets to itself and the CPU port */
  767. for (i = 0; i < RTL8366RB_PORT_NUM_CPU; i++) {
  768. ret = regmap_write(priv->map, RTL8366RB_PORT_ISO(i),
  769. RTL8366RB_PORT_ISO_PORTS(BIT(RTL8366RB_PORT_NUM_CPU)) |
  770. RTL8366RB_PORT_ISO_EN);
  771. if (ret)
  772. return ret;
  773. }
  774. /* CPU port can send packets to all ports */
  775. ret = regmap_write(priv->map, RTL8366RB_PORT_ISO(RTL8366RB_PORT_NUM_CPU),
  776. RTL8366RB_PORT_ISO_PORTS(dsa_user_ports(ds)) |
  777. RTL8366RB_PORT_ISO_EN);
  778. if (ret)
  779. return ret;
  780. /* Set up the "green ethernet" feature */
  781. ret = rtl8366rb_jam_table(rtl8366rb_green_jam,
  782. ARRAY_SIZE(rtl8366rb_green_jam), priv, false);
  783. if (ret)
  784. return ret;
  785. ret = regmap_write(priv->map,
  786. RTL8366RB_GREEN_FEATURE_REG,
  787. (chip_ver == 1) ? 0x0007 : 0x0003);
  788. if (ret)
  789. return ret;
  790. /* Vendor driver sets 0x240 in registers 0xc and 0xd (undocumented) */
  791. ret = regmap_write(priv->map, 0x0c, 0x240);
  792. if (ret)
  793. return ret;
  794. ret = regmap_write(priv->map, 0x0d, 0x240);
  795. if (ret)
  796. return ret;
  797. /* Set some random MAC address */
  798. ret = rtl8366rb_set_addr(priv);
  799. if (ret)
  800. return ret;
  801. /* Enable CPU port with custom DSA tag 8899.
  802. *
  803. * If you set RTL8366RB_CPU_NO_TAG (bit 15) in this register
  804. * the custom tag is turned off.
  805. */
  806. ret = regmap_update_bits(priv->map, RTL8366RB_CPU_CTRL_REG,
  807. 0xFFFF,
  808. BIT(priv->cpu_port));
  809. if (ret)
  810. return ret;
  811. /* Make sure we default-enable the fixed CPU port */
  812. ret = regmap_update_bits(priv->map, RTL8366RB_PECR,
  813. BIT(priv->cpu_port),
  814. 0);
  815. if (ret)
  816. return ret;
  817. /* Set default maximum packet length to 1536 bytes */
  818. ret = regmap_update_bits(priv->map, RTL8366RB_SGCR,
  819. RTL8366RB_SGCR_MAX_LENGTH_MASK,
  820. RTL8366RB_SGCR_MAX_LENGTH_1536);
  821. if (ret)
  822. return ret;
  823. for (i = 0; i < RTL8366RB_NUM_PORTS; i++) {
  824. if (i == priv->cpu_port)
  825. /* CPU port need to also accept the tag */
  826. rb->max_mtu[i] = ETH_DATA_LEN + RTL8366RB_CPU_TAG_SIZE;
  827. else
  828. rb->max_mtu[i] = ETH_DATA_LEN;
  829. }
  830. /* Disable learning for all ports */
  831. ret = regmap_write(priv->map, RTL8366RB_PORT_LEARNDIS_CTRL,
  832. RTL8366RB_PORT_ALL);
  833. if (ret)
  834. return ret;
  835. /* Enable auto ageing for all ports */
  836. ret = regmap_write(priv->map, RTL8366RB_SECURITY_CTRL, 0);
  837. if (ret)
  838. return ret;
  839. /* Port 4 setup: this enables Port 4, usually the WAN port,
  840. * common PHY IO mode is apparently mode 0, and this is not what
  841. * the port is initialized to. There is no explanation of the
  842. * IO modes in the Realtek source code, if your WAN port is
  843. * connected to something exotic such as fiber, then this might
  844. * be worth experimenting with.
  845. */
  846. ret = regmap_update_bits(priv->map, RTL8366RB_PMC0,
  847. RTL8366RB_PMC0_P4_IOMODE_MASK,
  848. 0 << RTL8366RB_PMC0_P4_IOMODE_SHIFT);
  849. if (ret)
  850. return ret;
  851. /* Accept all packets by default, we enable filtering on-demand */
  852. ret = regmap_write(priv->map, RTL8366RB_VLAN_INGRESS_CTRL1_REG,
  853. 0);
  854. if (ret)
  855. return ret;
  856. ret = regmap_write(priv->map, RTL8366RB_VLAN_INGRESS_CTRL2_REG,
  857. 0);
  858. if (ret)
  859. return ret;
  860. /* Don't drop packets whose DA has not been learned */
  861. ret = regmap_update_bits(priv->map, RTL8366RB_SSCR2,
  862. RTL8366RB_SSCR2_DROP_UNKNOWN_DA, 0);
  863. if (ret)
  864. return ret;
  865. /* Set blinking, used by all LED groups using HW triggers.
  866. * TODO: make this configurable
  867. */
  868. ret = regmap_update_bits(priv->map, RTL8366RB_LED_BLINKRATE_REG,
  869. RTL8366RB_LED_BLINKRATE_MASK,
  870. RTL8366RB_LED_BLINKRATE_56MS);
  871. if (ret)
  872. return ret;
  873. /* Set up LED activity:
  874. * Each port has 4 LEDs on fixed groups. Each group shares the same
  875. * hardware trigger across all ports. LEDs can only be indiviually
  876. * controlled setting the LED group to fixed mode and using the driver
  877. * to toggle them LEDs on/off.
  878. */
  879. if (priv->leds_disabled) {
  880. ret = rtl8366rb_setup_all_leds_off(priv);
  881. if (ret)
  882. return ret;
  883. } else {
  884. ret = rtl8366rb_setup_leds(priv);
  885. if (ret)
  886. return ret;
  887. }
  888. ret = rtl8366_reset_vlan(priv);
  889. if (ret)
  890. return ret;
  891. ret = rtl8366rb_setup_cascaded_irq(priv);
  892. if (ret)
  893. dev_info(priv->dev, "no interrupt support\n");
  894. ret = rtl83xx_setup_user_mdio(ds);
  895. if (ret) {
  896. dev_err(priv->dev, "could not set up MDIO bus\n");
  897. return -ENODEV;
  898. }
  899. return 0;
  900. }
  901. static enum dsa_tag_protocol rtl8366_get_tag_protocol(struct dsa_switch *ds,
  902. int port,
  903. enum dsa_tag_protocol mp)
  904. {
  905. /* This switch uses the 4 byte protocol A Realtek DSA tag */
  906. return DSA_TAG_PROTO_RTL4_A;
  907. }
  908. static void rtl8366rb_phylink_get_caps(struct dsa_switch *ds, int port,
  909. struct phylink_config *config)
  910. {
  911. unsigned long *interfaces = config->supported_interfaces;
  912. struct realtek_priv *priv = ds->priv;
  913. if (port == priv->cpu_port) {
  914. __set_bit(PHY_INTERFACE_MODE_MII, interfaces);
  915. __set_bit(PHY_INTERFACE_MODE_GMII, interfaces);
  916. /* REVMII only supports 100M FD */
  917. __set_bit(PHY_INTERFACE_MODE_REVMII, interfaces);
  918. /* RGMII only supports 1G FD */
  919. phy_interface_set_rgmii(interfaces);
  920. config->mac_capabilities = MAC_1000 | MAC_100 |
  921. MAC_SYM_PAUSE;
  922. } else {
  923. /* RSGMII port, but we don't have that, and we don't
  924. * specify in DT, so phylib uses the default of GMII
  925. */
  926. __set_bit(PHY_INTERFACE_MODE_GMII, interfaces);
  927. config->mac_capabilities = MAC_1000 | MAC_100 | MAC_10 |
  928. MAC_SYM_PAUSE | MAC_ASYM_PAUSE;
  929. }
  930. }
  931. static void
  932. rtl8366rb_mac_config(struct phylink_config *config, unsigned int mode,
  933. const struct phylink_link_state *state)
  934. {
  935. }
  936. static void
  937. rtl8366rb_mac_link_up(struct phylink_config *config, struct phy_device *phydev,
  938. unsigned int mode, phy_interface_t interface,
  939. int speed, int duplex, bool tx_pause, bool rx_pause)
  940. {
  941. struct dsa_port *dp = dsa_phylink_to_port(config);
  942. struct realtek_priv *priv = dp->ds->priv;
  943. int port = dp->index;
  944. unsigned int val;
  945. int ret;
  946. /* Allow forcing the mode on the fixed CPU port, no autonegotiation.
  947. * We assume autonegotiation works on the PHY-facing ports.
  948. */
  949. if (port != priv->cpu_port)
  950. return;
  951. dev_dbg(priv->dev, "MAC link up on CPU port (%d)\n", port);
  952. ret = regmap_update_bits(priv->map, RTL8366RB_MAC_FORCE_CTRL_REG,
  953. BIT(port), BIT(port));
  954. if (ret) {
  955. dev_err(priv->dev, "failed to force CPU port\n");
  956. return;
  957. }
  958. /* Conjure port config */
  959. switch (speed) {
  960. case SPEED_10:
  961. val = RTL8366RB_PAACR_SPEED_10M;
  962. break;
  963. case SPEED_100:
  964. val = RTL8366RB_PAACR_SPEED_100M;
  965. break;
  966. case SPEED_1000:
  967. val = RTL8366RB_PAACR_SPEED_1000M;
  968. break;
  969. default:
  970. val = RTL8366RB_PAACR_SPEED_1000M;
  971. break;
  972. }
  973. if (duplex == DUPLEX_FULL)
  974. val |= RTL8366RB_PAACR_FULL_DUPLEX;
  975. if (tx_pause)
  976. val |= RTL8366RB_PAACR_TX_PAUSE;
  977. if (rx_pause)
  978. val |= RTL8366RB_PAACR_RX_PAUSE;
  979. val |= RTL8366RB_PAACR_LINK_UP;
  980. ret = regmap_update_bits(priv->map, RTL8366RB_PAACR2,
  981. 0xFF00U,
  982. val << 8);
  983. if (ret) {
  984. dev_err(priv->dev, "failed to set PAACR on CPU port\n");
  985. return;
  986. }
  987. dev_dbg(priv->dev, "set PAACR to %04x\n", val);
  988. /* Enable the CPU port */
  989. ret = regmap_update_bits(priv->map, RTL8366RB_PECR, BIT(port),
  990. 0);
  991. if (ret) {
  992. dev_err(priv->dev, "failed to enable the CPU port\n");
  993. return;
  994. }
  995. }
  996. static void
  997. rtl8366rb_mac_link_down(struct phylink_config *config, unsigned int mode,
  998. phy_interface_t interface)
  999. {
  1000. struct dsa_port *dp = dsa_phylink_to_port(config);
  1001. struct realtek_priv *priv = dp->ds->priv;
  1002. int port = dp->index;
  1003. int ret;
  1004. if (port != priv->cpu_port)
  1005. return;
  1006. dev_dbg(priv->dev, "MAC link down on CPU port (%d)\n", port);
  1007. /* Disable the CPU port */
  1008. ret = regmap_update_bits(priv->map, RTL8366RB_PECR, BIT(port),
  1009. BIT(port));
  1010. if (ret) {
  1011. dev_err(priv->dev, "failed to disable the CPU port\n");
  1012. return;
  1013. }
  1014. }
  1015. static int
  1016. rtl8366rb_port_enable(struct dsa_switch *ds, int port,
  1017. struct phy_device *phy)
  1018. {
  1019. struct realtek_priv *priv = ds->priv;
  1020. int ret;
  1021. dev_dbg(priv->dev, "enable port %d\n", port);
  1022. ret = regmap_update_bits(priv->map, RTL8366RB_PECR, BIT(port),
  1023. 0);
  1024. if (ret)
  1025. return ret;
  1026. return 0;
  1027. }
  1028. static void
  1029. rtl8366rb_port_disable(struct dsa_switch *ds, int port)
  1030. {
  1031. struct realtek_priv *priv = ds->priv;
  1032. int ret;
  1033. dev_dbg(priv->dev, "disable port %d\n", port);
  1034. ret = regmap_update_bits(priv->map, RTL8366RB_PECR, BIT(port),
  1035. BIT(port));
  1036. if (ret)
  1037. return;
  1038. }
  1039. static int
  1040. rtl8366rb_port_bridge_join(struct dsa_switch *ds, int port,
  1041. struct dsa_bridge bridge,
  1042. bool *tx_fwd_offload,
  1043. struct netlink_ext_ack *extack)
  1044. {
  1045. struct realtek_priv *priv = ds->priv;
  1046. unsigned int port_bitmap = 0;
  1047. int ret, i;
  1048. /* Loop over all other ports than the current one */
  1049. for (i = 0; i < RTL8366RB_PORT_NUM_CPU; i++) {
  1050. /* Current port handled last */
  1051. if (i == port)
  1052. continue;
  1053. /* Not on this bridge */
  1054. if (!dsa_port_offloads_bridge(dsa_to_port(ds, i), &bridge))
  1055. continue;
  1056. /* Join this port to each other port on the bridge */
  1057. ret = regmap_update_bits(priv->map, RTL8366RB_PORT_ISO(i),
  1058. RTL8366RB_PORT_ISO_PORTS(BIT(port)),
  1059. RTL8366RB_PORT_ISO_PORTS(BIT(port)));
  1060. if (ret)
  1061. dev_err(priv->dev, "failed to join port %d\n", port);
  1062. port_bitmap |= BIT(i);
  1063. }
  1064. /* Set the bits for the ports we can access */
  1065. return regmap_update_bits(priv->map, RTL8366RB_PORT_ISO(port),
  1066. RTL8366RB_PORT_ISO_PORTS(port_bitmap),
  1067. RTL8366RB_PORT_ISO_PORTS(port_bitmap));
  1068. }
  1069. static void
  1070. rtl8366rb_port_bridge_leave(struct dsa_switch *ds, int port,
  1071. struct dsa_bridge bridge)
  1072. {
  1073. struct realtek_priv *priv = ds->priv;
  1074. unsigned int port_bitmap = 0;
  1075. int ret, i;
  1076. /* Loop over all other ports than this one */
  1077. for (i = 0; i < RTL8366RB_PORT_NUM_CPU; i++) {
  1078. /* Current port handled last */
  1079. if (i == port)
  1080. continue;
  1081. /* Not on this bridge */
  1082. if (!dsa_port_offloads_bridge(dsa_to_port(ds, i), &bridge))
  1083. continue;
  1084. /* Remove this port from any other port on the bridge */
  1085. ret = regmap_update_bits(priv->map, RTL8366RB_PORT_ISO(i),
  1086. RTL8366RB_PORT_ISO_PORTS(BIT(port)), 0);
  1087. if (ret)
  1088. dev_err(priv->dev, "failed to leave port %d\n", port);
  1089. port_bitmap |= BIT(i);
  1090. }
  1091. /* Clear the bits for the ports we can not access, leave ourselves */
  1092. regmap_update_bits(priv->map, RTL8366RB_PORT_ISO(port),
  1093. RTL8366RB_PORT_ISO_PORTS(port_bitmap), 0);
  1094. }
  1095. /**
  1096. * rtl8366rb_drop_untagged() - make the switch drop untagged and C-tagged frames
  1097. * @priv: SMI state container
  1098. * @port: the port to drop untagged and C-tagged frames on
  1099. * @drop: whether to drop or pass untagged and C-tagged frames
  1100. *
  1101. * Return: zero for success, a negative number on error.
  1102. */
  1103. static int rtl8366rb_drop_untagged(struct realtek_priv *priv, int port, bool drop)
  1104. {
  1105. return regmap_update_bits(priv->map, RTL8366RB_VLAN_INGRESS_CTRL1_REG,
  1106. RTL8366RB_VLAN_INGRESS_CTRL1_DROP(port),
  1107. drop ? RTL8366RB_VLAN_INGRESS_CTRL1_DROP(port) : 0);
  1108. }
  1109. static int rtl8366rb_vlan_filtering(struct dsa_switch *ds, int port,
  1110. bool vlan_filtering,
  1111. struct netlink_ext_ack *extack)
  1112. {
  1113. struct realtek_priv *priv = ds->priv;
  1114. struct rtl8366rb *rb;
  1115. int ret;
  1116. rb = priv->chip_data;
  1117. dev_dbg(priv->dev, "port %d: %s VLAN filtering\n", port,
  1118. vlan_filtering ? "enable" : "disable");
  1119. /* If the port is not in the member set, the frame will be dropped */
  1120. ret = regmap_update_bits(priv->map, RTL8366RB_VLAN_INGRESS_CTRL2_REG,
  1121. BIT(port), vlan_filtering ? BIT(port) : 0);
  1122. if (ret)
  1123. return ret;
  1124. /* If VLAN filtering is enabled and PVID is also enabled, we must
  1125. * not drop any untagged or C-tagged frames. If we turn off VLAN
  1126. * filtering on a port, we need to accept any frames.
  1127. */
  1128. if (vlan_filtering)
  1129. ret = rtl8366rb_drop_untagged(priv, port, !rb->pvid_enabled[port]);
  1130. else
  1131. ret = rtl8366rb_drop_untagged(priv, port, false);
  1132. return ret;
  1133. }
  1134. static int
  1135. rtl8366rb_port_pre_bridge_flags(struct dsa_switch *ds, int port,
  1136. struct switchdev_brport_flags flags,
  1137. struct netlink_ext_ack *extack)
  1138. {
  1139. /* We support enabling/disabling learning */
  1140. if (flags.mask & ~(BR_LEARNING))
  1141. return -EINVAL;
  1142. return 0;
  1143. }
  1144. static int
  1145. rtl8366rb_port_bridge_flags(struct dsa_switch *ds, int port,
  1146. struct switchdev_brport_flags flags,
  1147. struct netlink_ext_ack *extack)
  1148. {
  1149. struct realtek_priv *priv = ds->priv;
  1150. int ret;
  1151. if (flags.mask & BR_LEARNING) {
  1152. ret = regmap_update_bits(priv->map, RTL8366RB_PORT_LEARNDIS_CTRL,
  1153. BIT(port),
  1154. (flags.val & BR_LEARNING) ? 0 : BIT(port));
  1155. if (ret)
  1156. return ret;
  1157. }
  1158. return 0;
  1159. }
  1160. static void
  1161. rtl8366rb_port_stp_state_set(struct dsa_switch *ds, int port, u8 state)
  1162. {
  1163. struct realtek_priv *priv = ds->priv;
  1164. u32 val;
  1165. int i;
  1166. switch (state) {
  1167. case BR_STATE_DISABLED:
  1168. val = RTL8366RB_STP_STATE_DISABLED;
  1169. break;
  1170. case BR_STATE_BLOCKING:
  1171. case BR_STATE_LISTENING:
  1172. val = RTL8366RB_STP_STATE_BLOCKING;
  1173. break;
  1174. case BR_STATE_LEARNING:
  1175. val = RTL8366RB_STP_STATE_LEARNING;
  1176. break;
  1177. case BR_STATE_FORWARDING:
  1178. val = RTL8366RB_STP_STATE_FORWARDING;
  1179. break;
  1180. default:
  1181. dev_err(priv->dev, "unknown bridge state requested\n");
  1182. return;
  1183. }
  1184. /* Set the same status for the port on all the FIDs */
  1185. for (i = 0; i < RTL8366RB_NUM_FIDS; i++) {
  1186. regmap_update_bits(priv->map, RTL8366RB_STP_STATE_BASE + i,
  1187. RTL8366RB_STP_STATE_MASK(port),
  1188. RTL8366RB_STP_STATE(port, val));
  1189. }
  1190. }
  1191. static void
  1192. rtl8366rb_port_fast_age(struct dsa_switch *ds, int port)
  1193. {
  1194. struct realtek_priv *priv = ds->priv;
  1195. /* This will age out any learned L2 entries */
  1196. regmap_update_bits(priv->map, RTL8366RB_SECURITY_CTRL,
  1197. BIT(port), BIT(port));
  1198. /* Restore the normal state of things */
  1199. regmap_update_bits(priv->map, RTL8366RB_SECURITY_CTRL,
  1200. BIT(port), 0);
  1201. }
  1202. static int rtl8366rb_change_mtu(struct dsa_switch *ds, int port, int new_mtu)
  1203. {
  1204. struct realtek_priv *priv = ds->priv;
  1205. struct rtl8366rb *rb;
  1206. unsigned int max_mtu;
  1207. u32 len;
  1208. int i;
  1209. /* Cache the per-port MTU setting */
  1210. rb = priv->chip_data;
  1211. rb->max_mtu[port] = new_mtu;
  1212. /* Roof out the MTU for the entire switch to the greatest
  1213. * common denominator: the biggest set for any one port will
  1214. * be the biggest MTU for the switch.
  1215. */
  1216. max_mtu = ETH_DATA_LEN;
  1217. for (i = 0; i < RTL8366RB_NUM_PORTS; i++) {
  1218. if (rb->max_mtu[i] > max_mtu)
  1219. max_mtu = rb->max_mtu[i];
  1220. }
  1221. /* Translate to layer 2 size.
  1222. * Add ethernet and (possible) VLAN headers, and checksum to the size.
  1223. * For ETH_DATA_LEN (1500 bytes) this will add up to 1522 bytes.
  1224. */
  1225. max_mtu += VLAN_ETH_HLEN;
  1226. max_mtu += ETH_FCS_LEN;
  1227. if (max_mtu <= 1522)
  1228. len = RTL8366RB_SGCR_MAX_LENGTH_1522;
  1229. else if (max_mtu > 1522 && max_mtu <= 1536)
  1230. /* This will be the most common default if using VLAN and
  1231. * CPU tagging on a port as both VLAN and CPU tag will
  1232. * result in 1518 + 4 + 4 = 1526 bytes.
  1233. */
  1234. len = RTL8366RB_SGCR_MAX_LENGTH_1536;
  1235. else if (max_mtu > 1536 && max_mtu <= 1552)
  1236. len = RTL8366RB_SGCR_MAX_LENGTH_1552;
  1237. else
  1238. len = RTL8366RB_SGCR_MAX_LENGTH_16000;
  1239. return regmap_update_bits(priv->map, RTL8366RB_SGCR,
  1240. RTL8366RB_SGCR_MAX_LENGTH_MASK,
  1241. len);
  1242. }
  1243. static int rtl8366rb_max_mtu(struct dsa_switch *ds, int port)
  1244. {
  1245. /* The max MTU is 16000 bytes, so we subtract the ethernet
  1246. * headers with VLAN and checksum and arrive at
  1247. * 16000 - 18 - 4 = 15978. This does not include the CPU tag
  1248. * since that is added to the requested MTU by the DSA framework.
  1249. */
  1250. return 16000 - VLAN_ETH_HLEN - ETH_FCS_LEN;
  1251. }
  1252. static int rtl8366rb_get_vlan_4k(struct realtek_priv *priv, u32 vid,
  1253. struct rtl8366_vlan_4k *vlan4k)
  1254. {
  1255. u32 data[3];
  1256. int ret;
  1257. int i;
  1258. memset(vlan4k, '\0', sizeof(struct rtl8366_vlan_4k));
  1259. if (vid >= RTL8366RB_NUM_VIDS)
  1260. return -EINVAL;
  1261. /* write VID */
  1262. ret = regmap_write(priv->map, RTL8366RB_VLAN_TABLE_WRITE_BASE,
  1263. vid & RTL8366RB_VLAN_VID_MASK);
  1264. if (ret)
  1265. return ret;
  1266. /* write table access control word */
  1267. ret = regmap_write(priv->map, RTL8366RB_TABLE_ACCESS_CTRL_REG,
  1268. RTL8366RB_TABLE_VLAN_READ_CTRL);
  1269. if (ret)
  1270. return ret;
  1271. for (i = 0; i < 3; i++) {
  1272. ret = regmap_read(priv->map,
  1273. RTL8366RB_VLAN_TABLE_READ_BASE + i,
  1274. &data[i]);
  1275. if (ret)
  1276. return ret;
  1277. }
  1278. vlan4k->vid = vid;
  1279. vlan4k->untag = (data[1] >> RTL8366RB_VLAN_UNTAG_SHIFT) &
  1280. RTL8366RB_VLAN_UNTAG_MASK;
  1281. vlan4k->member = data[1] & RTL8366RB_VLAN_MEMBER_MASK;
  1282. vlan4k->fid = data[2] & RTL8366RB_VLAN_FID_MASK;
  1283. return 0;
  1284. }
  1285. static int rtl8366rb_set_vlan_4k(struct realtek_priv *priv,
  1286. const struct rtl8366_vlan_4k *vlan4k)
  1287. {
  1288. u32 data[3];
  1289. int ret;
  1290. int i;
  1291. if (vlan4k->vid >= RTL8366RB_NUM_VIDS ||
  1292. vlan4k->member > RTL8366RB_VLAN_MEMBER_MASK ||
  1293. vlan4k->untag > RTL8366RB_VLAN_UNTAG_MASK ||
  1294. vlan4k->fid > RTL8366RB_FIDMAX)
  1295. return -EINVAL;
  1296. data[0] = vlan4k->vid & RTL8366RB_VLAN_VID_MASK;
  1297. data[1] = (vlan4k->member & RTL8366RB_VLAN_MEMBER_MASK) |
  1298. ((vlan4k->untag & RTL8366RB_VLAN_UNTAG_MASK) <<
  1299. RTL8366RB_VLAN_UNTAG_SHIFT);
  1300. data[2] = vlan4k->fid & RTL8366RB_VLAN_FID_MASK;
  1301. for (i = 0; i < 3; i++) {
  1302. ret = regmap_write(priv->map,
  1303. RTL8366RB_VLAN_TABLE_WRITE_BASE + i,
  1304. data[i]);
  1305. if (ret)
  1306. return ret;
  1307. }
  1308. /* write table access control word */
  1309. ret = regmap_write(priv->map, RTL8366RB_TABLE_ACCESS_CTRL_REG,
  1310. RTL8366RB_TABLE_VLAN_WRITE_CTRL);
  1311. return ret;
  1312. }
  1313. static int rtl8366rb_get_vlan_mc(struct realtek_priv *priv, u32 index,
  1314. struct rtl8366_vlan_mc *vlanmc)
  1315. {
  1316. u32 data[3];
  1317. int ret;
  1318. int i;
  1319. memset(vlanmc, '\0', sizeof(struct rtl8366_vlan_mc));
  1320. if (index >= RTL8366RB_NUM_VLANS)
  1321. return -EINVAL;
  1322. for (i = 0; i < 3; i++) {
  1323. ret = regmap_read(priv->map,
  1324. RTL8366RB_VLAN_MC_BASE(index) + i,
  1325. &data[i]);
  1326. if (ret)
  1327. return ret;
  1328. }
  1329. vlanmc->vid = data[0] & RTL8366RB_VLAN_VID_MASK;
  1330. vlanmc->priority = (data[0] >> RTL8366RB_VLAN_PRIORITY_SHIFT) &
  1331. RTL8366RB_VLAN_PRIORITY_MASK;
  1332. vlanmc->untag = (data[1] >> RTL8366RB_VLAN_UNTAG_SHIFT) &
  1333. RTL8366RB_VLAN_UNTAG_MASK;
  1334. vlanmc->member = data[1] & RTL8366RB_VLAN_MEMBER_MASK;
  1335. vlanmc->fid = data[2] & RTL8366RB_VLAN_FID_MASK;
  1336. return 0;
  1337. }
  1338. static int rtl8366rb_set_vlan_mc(struct realtek_priv *priv, u32 index,
  1339. const struct rtl8366_vlan_mc *vlanmc)
  1340. {
  1341. u32 data[3];
  1342. int ret;
  1343. int i;
  1344. if (index >= RTL8366RB_NUM_VLANS ||
  1345. vlanmc->vid >= RTL8366RB_NUM_VIDS ||
  1346. vlanmc->priority > RTL8366RB_PRIORITYMAX ||
  1347. vlanmc->member > RTL8366RB_VLAN_MEMBER_MASK ||
  1348. vlanmc->untag > RTL8366RB_VLAN_UNTAG_MASK ||
  1349. vlanmc->fid > RTL8366RB_FIDMAX)
  1350. return -EINVAL;
  1351. data[0] = (vlanmc->vid & RTL8366RB_VLAN_VID_MASK) |
  1352. ((vlanmc->priority & RTL8366RB_VLAN_PRIORITY_MASK) <<
  1353. RTL8366RB_VLAN_PRIORITY_SHIFT);
  1354. data[1] = (vlanmc->member & RTL8366RB_VLAN_MEMBER_MASK) |
  1355. ((vlanmc->untag & RTL8366RB_VLAN_UNTAG_MASK) <<
  1356. RTL8366RB_VLAN_UNTAG_SHIFT);
  1357. data[2] = vlanmc->fid & RTL8366RB_VLAN_FID_MASK;
  1358. for (i = 0; i < 3; i++) {
  1359. ret = regmap_write(priv->map,
  1360. RTL8366RB_VLAN_MC_BASE(index) + i,
  1361. data[i]);
  1362. if (ret)
  1363. return ret;
  1364. }
  1365. return 0;
  1366. }
  1367. static int rtl8366rb_get_mc_index(struct realtek_priv *priv, int port, int *val)
  1368. {
  1369. u32 data;
  1370. int ret;
  1371. if (port >= priv->num_ports)
  1372. return -EINVAL;
  1373. ret = regmap_read(priv->map, RTL8366RB_PORT_VLAN_CTRL_REG(port),
  1374. &data);
  1375. if (ret)
  1376. return ret;
  1377. *val = (data >> RTL8366RB_PORT_VLAN_CTRL_SHIFT(port)) &
  1378. RTL8366RB_PORT_VLAN_CTRL_MASK;
  1379. return 0;
  1380. }
  1381. static int rtl8366rb_set_mc_index(struct realtek_priv *priv, int port, int index)
  1382. {
  1383. struct dsa_switch *ds = &priv->ds;
  1384. struct rtl8366rb *rb;
  1385. bool pvid_enabled;
  1386. int ret;
  1387. rb = priv->chip_data;
  1388. pvid_enabled = !!index;
  1389. if (port >= priv->num_ports || index >= RTL8366RB_NUM_VLANS)
  1390. return -EINVAL;
  1391. ret = regmap_update_bits(priv->map, RTL8366RB_PORT_VLAN_CTRL_REG(port),
  1392. RTL8366RB_PORT_VLAN_CTRL_MASK <<
  1393. RTL8366RB_PORT_VLAN_CTRL_SHIFT(port),
  1394. (index & RTL8366RB_PORT_VLAN_CTRL_MASK) <<
  1395. RTL8366RB_PORT_VLAN_CTRL_SHIFT(port));
  1396. if (ret)
  1397. return ret;
  1398. rb->pvid_enabled[port] = pvid_enabled;
  1399. /* If VLAN filtering is enabled and PVID is also enabled, we must
  1400. * not drop any untagged or C-tagged frames. Make sure to update the
  1401. * filtering setting.
  1402. */
  1403. if (dsa_port_is_vlan_filtering(dsa_to_port(ds, port)))
  1404. ret = rtl8366rb_drop_untagged(priv, port, !pvid_enabled);
  1405. return ret;
  1406. }
  1407. static bool rtl8366rb_is_vlan_valid(struct realtek_priv *priv, unsigned int vlan)
  1408. {
  1409. unsigned int max = RTL8366RB_NUM_VLANS - 1;
  1410. if (priv->vlan4k_enabled)
  1411. max = RTL8366RB_NUM_VIDS - 1;
  1412. if (vlan > max)
  1413. return false;
  1414. return true;
  1415. }
  1416. static int rtl8366rb_enable_vlan(struct realtek_priv *priv, bool enable)
  1417. {
  1418. dev_dbg(priv->dev, "%s VLAN\n", enable ? "enable" : "disable");
  1419. return regmap_update_bits(priv->map,
  1420. RTL8366RB_SGCR, RTL8366RB_SGCR_EN_VLAN,
  1421. enable ? RTL8366RB_SGCR_EN_VLAN : 0);
  1422. }
  1423. static int rtl8366rb_enable_vlan4k(struct realtek_priv *priv, bool enable)
  1424. {
  1425. dev_dbg(priv->dev, "%s VLAN 4k\n", enable ? "enable" : "disable");
  1426. return regmap_update_bits(priv->map, RTL8366RB_SGCR,
  1427. RTL8366RB_SGCR_EN_VLAN_4KTB,
  1428. enable ? RTL8366RB_SGCR_EN_VLAN_4KTB : 0);
  1429. }
  1430. static int rtl8366rb_phy_read(struct realtek_priv *priv, int phy, int regnum)
  1431. {
  1432. u32 val;
  1433. u32 reg;
  1434. int ret;
  1435. if (phy > RTL8366RB_PHY_NO_MAX)
  1436. return -EINVAL;
  1437. rtl83xx_lock(priv);
  1438. ret = regmap_write(priv->map_nolock, RTL8366RB_PHY_ACCESS_CTRL_REG,
  1439. RTL8366RB_PHY_CTRL_READ);
  1440. if (ret)
  1441. goto out;
  1442. reg = 0x8000 | (1 << (phy + RTL8366RB_PHY_NO_OFFSET)) | regnum;
  1443. ret = regmap_write(priv->map_nolock, reg, 0);
  1444. if (ret) {
  1445. dev_err(priv->dev,
  1446. "failed to write PHY%d reg %04x @ %04x, ret %d\n",
  1447. phy, regnum, reg, ret);
  1448. goto out;
  1449. }
  1450. ret = regmap_read(priv->map_nolock, RTL8366RB_PHY_ACCESS_DATA_REG,
  1451. &val);
  1452. if (ret)
  1453. goto out;
  1454. ret = val;
  1455. dev_dbg(priv->dev, "read PHY%d register 0x%04x @ %08x, val <- %04x\n",
  1456. phy, regnum, reg, val);
  1457. out:
  1458. rtl83xx_unlock(priv);
  1459. return ret;
  1460. }
  1461. static int rtl8366rb_phy_write(struct realtek_priv *priv, int phy, int regnum,
  1462. u16 val)
  1463. {
  1464. u32 reg;
  1465. int ret;
  1466. if (phy > RTL8366RB_PHY_NO_MAX)
  1467. return -EINVAL;
  1468. rtl83xx_lock(priv);
  1469. ret = regmap_write(priv->map_nolock, RTL8366RB_PHY_ACCESS_CTRL_REG,
  1470. RTL8366RB_PHY_CTRL_WRITE);
  1471. if (ret)
  1472. goto out;
  1473. reg = 0x8000 | (1 << (phy + RTL8366RB_PHY_NO_OFFSET)) | regnum;
  1474. dev_dbg(priv->dev, "write PHY%d register 0x%04x @ %04x, val -> %04x\n",
  1475. phy, regnum, reg, val);
  1476. ret = regmap_write(priv->map_nolock, reg, val);
  1477. if (ret)
  1478. goto out;
  1479. out:
  1480. rtl83xx_unlock(priv);
  1481. return ret;
  1482. }
  1483. static int rtl8366rb_reset_chip(struct realtek_priv *priv)
  1484. {
  1485. int timeout = 10;
  1486. u32 val;
  1487. int ret;
  1488. priv->write_reg_noack(priv, RTL8366RB_RESET_CTRL_REG,
  1489. RTL8366RB_CHIP_CTRL_RESET_HW);
  1490. do {
  1491. usleep_range(20000, 25000);
  1492. ret = regmap_read(priv->map, RTL8366RB_RESET_CTRL_REG, &val);
  1493. if (ret)
  1494. return ret;
  1495. if (!(val & RTL8366RB_CHIP_CTRL_RESET_HW))
  1496. break;
  1497. } while (--timeout);
  1498. if (!timeout) {
  1499. dev_err(priv->dev, "timeout waiting for the switch to reset\n");
  1500. return -EIO;
  1501. }
  1502. return 0;
  1503. }
  1504. static int rtl8366rb_detect(struct realtek_priv *priv)
  1505. {
  1506. struct device *dev = priv->dev;
  1507. int ret;
  1508. u32 val;
  1509. /* Detect device */
  1510. ret = regmap_read(priv->map, 0x5c, &val);
  1511. if (ret) {
  1512. dev_err(dev, "can't get chip ID (%d)\n", ret);
  1513. return ret;
  1514. }
  1515. switch (val) {
  1516. case 0x6027:
  1517. dev_info(dev, "found an RTL8366S switch\n");
  1518. dev_err(dev, "this switch is not yet supported, submit patches!\n");
  1519. return -ENODEV;
  1520. case 0x5937:
  1521. dev_info(dev, "found an RTL8366RB switch\n");
  1522. priv->cpu_port = RTL8366RB_PORT_NUM_CPU;
  1523. priv->num_ports = RTL8366RB_NUM_PORTS;
  1524. priv->num_vlan_mc = RTL8366RB_NUM_VLANS;
  1525. priv->mib_counters = rtl8366rb_mib_counters;
  1526. priv->num_mib_counters = ARRAY_SIZE(rtl8366rb_mib_counters);
  1527. break;
  1528. default:
  1529. dev_info(dev, "found an Unknown Realtek switch (id=0x%04x)\n",
  1530. val);
  1531. break;
  1532. }
  1533. ret = rtl8366rb_reset_chip(priv);
  1534. if (ret)
  1535. return ret;
  1536. return 0;
  1537. }
  1538. static const struct phylink_mac_ops rtl8366rb_phylink_mac_ops = {
  1539. .mac_config = rtl8366rb_mac_config,
  1540. .mac_link_down = rtl8366rb_mac_link_down,
  1541. .mac_link_up = rtl8366rb_mac_link_up,
  1542. };
  1543. static const struct dsa_switch_ops rtl8366rb_switch_ops = {
  1544. .get_tag_protocol = rtl8366_get_tag_protocol,
  1545. .setup = rtl8366rb_setup,
  1546. .phylink_get_caps = rtl8366rb_phylink_get_caps,
  1547. .get_strings = rtl8366_get_strings,
  1548. .get_ethtool_stats = rtl8366_get_ethtool_stats,
  1549. .get_sset_count = rtl8366_get_sset_count,
  1550. .port_bridge_join = rtl8366rb_port_bridge_join,
  1551. .port_bridge_leave = rtl8366rb_port_bridge_leave,
  1552. .port_vlan_filtering = rtl8366rb_vlan_filtering,
  1553. .port_vlan_add = rtl8366_vlan_add,
  1554. .port_vlan_del = rtl8366_vlan_del,
  1555. .port_enable = rtl8366rb_port_enable,
  1556. .port_disable = rtl8366rb_port_disable,
  1557. .port_pre_bridge_flags = rtl8366rb_port_pre_bridge_flags,
  1558. .port_bridge_flags = rtl8366rb_port_bridge_flags,
  1559. .port_stp_state_set = rtl8366rb_port_stp_state_set,
  1560. .port_fast_age = rtl8366rb_port_fast_age,
  1561. .port_change_mtu = rtl8366rb_change_mtu,
  1562. .port_max_mtu = rtl8366rb_max_mtu,
  1563. };
  1564. static const struct realtek_ops rtl8366rb_ops = {
  1565. .detect = rtl8366rb_detect,
  1566. .get_vlan_mc = rtl8366rb_get_vlan_mc,
  1567. .set_vlan_mc = rtl8366rb_set_vlan_mc,
  1568. .get_vlan_4k = rtl8366rb_get_vlan_4k,
  1569. .set_vlan_4k = rtl8366rb_set_vlan_4k,
  1570. .get_mc_index = rtl8366rb_get_mc_index,
  1571. .set_mc_index = rtl8366rb_set_mc_index,
  1572. .get_mib_counter = rtl8366rb_get_mib_counter,
  1573. .is_vlan_valid = rtl8366rb_is_vlan_valid,
  1574. .enable_vlan = rtl8366rb_enable_vlan,
  1575. .enable_vlan4k = rtl8366rb_enable_vlan4k,
  1576. .phy_read = rtl8366rb_phy_read,
  1577. .phy_write = rtl8366rb_phy_write,
  1578. };
  1579. const struct realtek_variant rtl8366rb_variant = {
  1580. .ds_ops = &rtl8366rb_switch_ops,
  1581. .ops = &rtl8366rb_ops,
  1582. .phylink_mac_ops = &rtl8366rb_phylink_mac_ops,
  1583. .clk_delay = 10,
  1584. .cmd_read = 0xa9,
  1585. .cmd_write = 0xa8,
  1586. .chip_data_sz = sizeof(struct rtl8366rb),
  1587. };
  1588. static const struct of_device_id rtl8366rb_of_match[] = {
  1589. { .compatible = "realtek,rtl8366rb", .data = &rtl8366rb_variant, },
  1590. { /* sentinel */ },
  1591. };
  1592. MODULE_DEVICE_TABLE(of, rtl8366rb_of_match);
  1593. static struct platform_driver rtl8366rb_smi_driver = {
  1594. .driver = {
  1595. .name = "rtl8366rb-smi",
  1596. .of_match_table = rtl8366rb_of_match,
  1597. },
  1598. .probe = realtek_smi_probe,
  1599. .remove_new = realtek_smi_remove,
  1600. .shutdown = realtek_smi_shutdown,
  1601. };
  1602. static struct mdio_driver rtl8366rb_mdio_driver = {
  1603. .mdiodrv.driver = {
  1604. .name = "rtl8366rb-mdio",
  1605. .of_match_table = rtl8366rb_of_match,
  1606. },
  1607. .probe = realtek_mdio_probe,
  1608. .remove = realtek_mdio_remove,
  1609. .shutdown = realtek_mdio_shutdown,
  1610. };
  1611. static int rtl8366rb_init(void)
  1612. {
  1613. int ret;
  1614. ret = realtek_mdio_driver_register(&rtl8366rb_mdio_driver);
  1615. if (ret)
  1616. return ret;
  1617. ret = realtek_smi_driver_register(&rtl8366rb_smi_driver);
  1618. if (ret) {
  1619. realtek_mdio_driver_unregister(&rtl8366rb_mdio_driver);
  1620. return ret;
  1621. }
  1622. return 0;
  1623. }
  1624. module_init(rtl8366rb_init);
  1625. static void __exit rtl8366rb_exit(void)
  1626. {
  1627. realtek_smi_driver_unregister(&rtl8366rb_smi_driver);
  1628. realtek_mdio_driver_unregister(&rtl8366rb_mdio_driver);
  1629. }
  1630. module_exit(rtl8366rb_exit);
  1631. MODULE_AUTHOR("Linus Walleij <linus.walleij@linaro.org>");
  1632. MODULE_DESCRIPTION("Driver for RTL8366RB ethernet switch");
  1633. MODULE_LICENSE("GPL");
  1634. MODULE_IMPORT_NS(REALTEK_DSA);