rtl8366rb.c 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454
  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/interrupt.h>
  17. #include <linux/irqdomain.h>
  18. #include <linux/irqchip/chained_irq.h>
  19. #include <linux/of_irq.h>
  20. #include <linux/regmap.h>
  21. #include "realtek-smi.h"
  22. #define RTL8366RB_PORT_NUM_CPU 5
  23. #define RTL8366RB_NUM_PORTS 6
  24. #define RTL8366RB_PHY_NO_MAX 4
  25. #define RTL8366RB_PHY_ADDR_MAX 31
  26. /* Switch Global Configuration register */
  27. #define RTL8366RB_SGCR 0x0000
  28. #define RTL8366RB_SGCR_EN_BC_STORM_CTRL BIT(0)
  29. #define RTL8366RB_SGCR_MAX_LENGTH(a) ((a) << 4)
  30. #define RTL8366RB_SGCR_MAX_LENGTH_MASK RTL8366RB_SGCR_MAX_LENGTH(0x3)
  31. #define RTL8366RB_SGCR_MAX_LENGTH_1522 RTL8366RB_SGCR_MAX_LENGTH(0x0)
  32. #define RTL8366RB_SGCR_MAX_LENGTH_1536 RTL8366RB_SGCR_MAX_LENGTH(0x1)
  33. #define RTL8366RB_SGCR_MAX_LENGTH_1552 RTL8366RB_SGCR_MAX_LENGTH(0x2)
  34. #define RTL8366RB_SGCR_MAX_LENGTH_9216 RTL8366RB_SGCR_MAX_LENGTH(0x3)
  35. #define RTL8366RB_SGCR_EN_VLAN BIT(13)
  36. #define RTL8366RB_SGCR_EN_VLAN_4KTB BIT(14)
  37. /* Port Enable Control register */
  38. #define RTL8366RB_PECR 0x0001
  39. /* Switch Security Control registers */
  40. #define RTL8366RB_SSCR0 0x0002
  41. #define RTL8366RB_SSCR1 0x0003
  42. #define RTL8366RB_SSCR2 0x0004
  43. #define RTL8366RB_SSCR2_DROP_UNKNOWN_DA BIT(0)
  44. /* Port Mode Control registers */
  45. #define RTL8366RB_PMC0 0x0005
  46. #define RTL8366RB_PMC0_SPI BIT(0)
  47. #define RTL8366RB_PMC0_EN_AUTOLOAD BIT(1)
  48. #define RTL8366RB_PMC0_PROBE BIT(2)
  49. #define RTL8366RB_PMC0_DIS_BISR BIT(3)
  50. #define RTL8366RB_PMC0_ADCTEST BIT(4)
  51. #define RTL8366RB_PMC0_SRAM_DIAG BIT(5)
  52. #define RTL8366RB_PMC0_EN_SCAN BIT(6)
  53. #define RTL8366RB_PMC0_P4_IOMODE_SHIFT 7
  54. #define RTL8366RB_PMC0_P4_IOMODE_MASK GENMASK(9, 7)
  55. #define RTL8366RB_PMC0_P5_IOMODE_SHIFT 10
  56. #define RTL8366RB_PMC0_P5_IOMODE_MASK GENMASK(12, 10)
  57. #define RTL8366RB_PMC0_SDSMODE_SHIFT 13
  58. #define RTL8366RB_PMC0_SDSMODE_MASK GENMASK(15, 13)
  59. #define RTL8366RB_PMC1 0x0006
  60. /* Port Mirror Control Register */
  61. #define RTL8366RB_PMCR 0x0007
  62. #define RTL8366RB_PMCR_SOURCE_PORT(a) (a)
  63. #define RTL8366RB_PMCR_SOURCE_PORT_MASK 0x000f
  64. #define RTL8366RB_PMCR_MONITOR_PORT(a) ((a) << 4)
  65. #define RTL8366RB_PMCR_MONITOR_PORT_MASK 0x00f0
  66. #define RTL8366RB_PMCR_MIRROR_RX BIT(8)
  67. #define RTL8366RB_PMCR_MIRROR_TX BIT(9)
  68. #define RTL8366RB_PMCR_MIRROR_SPC BIT(10)
  69. #define RTL8366RB_PMCR_MIRROR_ISO BIT(11)
  70. /* bits 0..7 = port 0, bits 8..15 = port 1 */
  71. #define RTL8366RB_PAACR0 0x0010
  72. /* bits 0..7 = port 2, bits 8..15 = port 3 */
  73. #define RTL8366RB_PAACR1 0x0011
  74. /* bits 0..7 = port 4, bits 8..15 = port 5 */
  75. #define RTL8366RB_PAACR2 0x0012
  76. #define RTL8366RB_PAACR_SPEED_10M 0
  77. #define RTL8366RB_PAACR_SPEED_100M 1
  78. #define RTL8366RB_PAACR_SPEED_1000M 2
  79. #define RTL8366RB_PAACR_FULL_DUPLEX BIT(2)
  80. #define RTL8366RB_PAACR_LINK_UP BIT(4)
  81. #define RTL8366RB_PAACR_TX_PAUSE BIT(5)
  82. #define RTL8366RB_PAACR_RX_PAUSE BIT(6)
  83. #define RTL8366RB_PAACR_AN BIT(7)
  84. #define RTL8366RB_PAACR_CPU_PORT (RTL8366RB_PAACR_SPEED_1000M | \
  85. RTL8366RB_PAACR_FULL_DUPLEX | \
  86. RTL8366RB_PAACR_LINK_UP | \
  87. RTL8366RB_PAACR_TX_PAUSE | \
  88. RTL8366RB_PAACR_RX_PAUSE)
  89. /* bits 0..7 = port 0, bits 8..15 = port 1 */
  90. #define RTL8366RB_PSTAT0 0x0014
  91. /* bits 0..7 = port 2, bits 8..15 = port 3 */
  92. #define RTL8366RB_PSTAT1 0x0015
  93. /* bits 0..7 = port 4, bits 8..15 = port 5 */
  94. #define RTL8366RB_PSTAT2 0x0016
  95. #define RTL8366RB_POWER_SAVING_REG 0x0021
  96. /* CPU port control reg */
  97. #define RTL8368RB_CPU_CTRL_REG 0x0061
  98. #define RTL8368RB_CPU_PORTS_MSK 0x00FF
  99. /* Enables inserting custom tag length/type 0x8899 */
  100. #define RTL8368RB_CPU_INSTAG BIT(15)
  101. #define RTL8366RB_SMAR0 0x0070 /* bits 0..15 */
  102. #define RTL8366RB_SMAR1 0x0071 /* bits 16..31 */
  103. #define RTL8366RB_SMAR2 0x0072 /* bits 32..47 */
  104. #define RTL8366RB_RESET_CTRL_REG 0x0100
  105. #define RTL8366RB_CHIP_CTRL_RESET_HW BIT(0)
  106. #define RTL8366RB_CHIP_CTRL_RESET_SW BIT(1)
  107. #define RTL8366RB_CHIP_ID_REG 0x0509
  108. #define RTL8366RB_CHIP_ID_8366 0x5937
  109. #define RTL8366RB_CHIP_VERSION_CTRL_REG 0x050A
  110. #define RTL8366RB_CHIP_VERSION_MASK 0xf
  111. /* PHY registers control */
  112. #define RTL8366RB_PHY_ACCESS_CTRL_REG 0x8000
  113. #define RTL8366RB_PHY_CTRL_READ BIT(0)
  114. #define RTL8366RB_PHY_CTRL_WRITE 0
  115. #define RTL8366RB_PHY_ACCESS_BUSY_REG 0x8001
  116. #define RTL8366RB_PHY_INT_BUSY BIT(0)
  117. #define RTL8366RB_PHY_EXT_BUSY BIT(4)
  118. #define RTL8366RB_PHY_ACCESS_DATA_REG 0x8002
  119. #define RTL8366RB_PHY_EXT_CTRL_REG 0x8010
  120. #define RTL8366RB_PHY_EXT_WRDATA_REG 0x8011
  121. #define RTL8366RB_PHY_EXT_RDDATA_REG 0x8012
  122. #define RTL8366RB_PHY_REG_MASK 0x1f
  123. #define RTL8366RB_PHY_PAGE_OFFSET 5
  124. #define RTL8366RB_PHY_PAGE_MASK (0xf << 5)
  125. #define RTL8366RB_PHY_NO_OFFSET 9
  126. #define RTL8366RB_PHY_NO_MASK (0x1f << 9)
  127. #define RTL8366RB_VLAN_INGRESS_CTRL2_REG 0x037f
  128. /* LED control registers */
  129. #define RTL8366RB_LED_BLINKRATE_REG 0x0430
  130. #define RTL8366RB_LED_BLINKRATE_MASK 0x0007
  131. #define RTL8366RB_LED_BLINKRATE_28MS 0x0000
  132. #define RTL8366RB_LED_BLINKRATE_56MS 0x0001
  133. #define RTL8366RB_LED_BLINKRATE_84MS 0x0002
  134. #define RTL8366RB_LED_BLINKRATE_111MS 0x0003
  135. #define RTL8366RB_LED_BLINKRATE_222MS 0x0004
  136. #define RTL8366RB_LED_BLINKRATE_446MS 0x0005
  137. #define RTL8366RB_LED_CTRL_REG 0x0431
  138. #define RTL8366RB_LED_OFF 0x0
  139. #define RTL8366RB_LED_DUP_COL 0x1
  140. #define RTL8366RB_LED_LINK_ACT 0x2
  141. #define RTL8366RB_LED_SPD1000 0x3
  142. #define RTL8366RB_LED_SPD100 0x4
  143. #define RTL8366RB_LED_SPD10 0x5
  144. #define RTL8366RB_LED_SPD1000_ACT 0x6
  145. #define RTL8366RB_LED_SPD100_ACT 0x7
  146. #define RTL8366RB_LED_SPD10_ACT 0x8
  147. #define RTL8366RB_LED_SPD100_10_ACT 0x9
  148. #define RTL8366RB_LED_FIBER 0xa
  149. #define RTL8366RB_LED_AN_FAULT 0xb
  150. #define RTL8366RB_LED_LINK_RX 0xc
  151. #define RTL8366RB_LED_LINK_TX 0xd
  152. #define RTL8366RB_LED_MASTER 0xe
  153. #define RTL8366RB_LED_FORCE 0xf
  154. #define RTL8366RB_LED_0_1_CTRL_REG 0x0432
  155. #define RTL8366RB_LED_1_OFFSET 6
  156. #define RTL8366RB_LED_2_3_CTRL_REG 0x0433
  157. #define RTL8366RB_LED_3_OFFSET 6
  158. #define RTL8366RB_MIB_COUNT 33
  159. #define RTL8366RB_GLOBAL_MIB_COUNT 1
  160. #define RTL8366RB_MIB_COUNTER_PORT_OFFSET 0x0050
  161. #define RTL8366RB_MIB_COUNTER_BASE 0x1000
  162. #define RTL8366RB_MIB_CTRL_REG 0x13F0
  163. #define RTL8366RB_MIB_CTRL_USER_MASK 0x0FFC
  164. #define RTL8366RB_MIB_CTRL_BUSY_MASK BIT(0)
  165. #define RTL8366RB_MIB_CTRL_RESET_MASK BIT(1)
  166. #define RTL8366RB_MIB_CTRL_PORT_RESET(_p) BIT(2 + (_p))
  167. #define RTL8366RB_MIB_CTRL_GLOBAL_RESET BIT(11)
  168. #define RTL8366RB_PORT_VLAN_CTRL_BASE 0x0063
  169. #define RTL8366RB_PORT_VLAN_CTRL_REG(_p) \
  170. (RTL8366RB_PORT_VLAN_CTRL_BASE + (_p) / 4)
  171. #define RTL8366RB_PORT_VLAN_CTRL_MASK 0xf
  172. #define RTL8366RB_PORT_VLAN_CTRL_SHIFT(_p) (4 * ((_p) % 4))
  173. #define RTL8366RB_VLAN_TABLE_READ_BASE 0x018C
  174. #define RTL8366RB_VLAN_TABLE_WRITE_BASE 0x0185
  175. #define RTL8366RB_TABLE_ACCESS_CTRL_REG 0x0180
  176. #define RTL8366RB_TABLE_VLAN_READ_CTRL 0x0E01
  177. #define RTL8366RB_TABLE_VLAN_WRITE_CTRL 0x0F01
  178. #define RTL8366RB_VLAN_MC_BASE(_x) (0x0020 + (_x) * 3)
  179. #define RTL8366RB_PORT_LINK_STATUS_BASE 0x0014
  180. #define RTL8366RB_PORT_STATUS_SPEED_MASK 0x0003
  181. #define RTL8366RB_PORT_STATUS_DUPLEX_MASK 0x0004
  182. #define RTL8366RB_PORT_STATUS_LINK_MASK 0x0010
  183. #define RTL8366RB_PORT_STATUS_TXPAUSE_MASK 0x0020
  184. #define RTL8366RB_PORT_STATUS_RXPAUSE_MASK 0x0040
  185. #define RTL8366RB_PORT_STATUS_AN_MASK 0x0080
  186. #define RTL8366RB_NUM_VLANS 16
  187. #define RTL8366RB_NUM_LEDGROUPS 4
  188. #define RTL8366RB_NUM_VIDS 4096
  189. #define RTL8366RB_PRIORITYMAX 7
  190. #define RTL8366RB_FIDMAX 7
  191. #define RTL8366RB_PORT_1 BIT(0) /* In userspace port 0 */
  192. #define RTL8366RB_PORT_2 BIT(1) /* In userspace port 1 */
  193. #define RTL8366RB_PORT_3 BIT(2) /* In userspace port 2 */
  194. #define RTL8366RB_PORT_4 BIT(3) /* In userspace port 3 */
  195. #define RTL8366RB_PORT_5 BIT(4) /* In userspace port 4 */
  196. #define RTL8366RB_PORT_CPU BIT(5) /* CPU port */
  197. #define RTL8366RB_PORT_ALL (RTL8366RB_PORT_1 | \
  198. RTL8366RB_PORT_2 | \
  199. RTL8366RB_PORT_3 | \
  200. RTL8366RB_PORT_4 | \
  201. RTL8366RB_PORT_5 | \
  202. RTL8366RB_PORT_CPU)
  203. #define RTL8366RB_PORT_ALL_BUT_CPU (RTL8366RB_PORT_1 | \
  204. RTL8366RB_PORT_2 | \
  205. RTL8366RB_PORT_3 | \
  206. RTL8366RB_PORT_4 | \
  207. RTL8366RB_PORT_5)
  208. #define RTL8366RB_PORT_ALL_EXTERNAL (RTL8366RB_PORT_1 | \
  209. RTL8366RB_PORT_2 | \
  210. RTL8366RB_PORT_3 | \
  211. RTL8366RB_PORT_4)
  212. #define RTL8366RB_PORT_ALL_INTERNAL RTL8366RB_PORT_CPU
  213. /* First configuration word per member config, VID and prio */
  214. #define RTL8366RB_VLAN_VID_MASK 0xfff
  215. #define RTL8366RB_VLAN_PRIORITY_SHIFT 12
  216. #define RTL8366RB_VLAN_PRIORITY_MASK 0x7
  217. /* Second configuration word per member config, member and untagged */
  218. #define RTL8366RB_VLAN_UNTAG_SHIFT 8
  219. #define RTL8366RB_VLAN_UNTAG_MASK 0xff
  220. #define RTL8366RB_VLAN_MEMBER_MASK 0xff
  221. /* Third config word per member config, STAG currently unused */
  222. #define RTL8366RB_VLAN_STAG_MBR_MASK 0xff
  223. #define RTL8366RB_VLAN_STAG_MBR_SHIFT 8
  224. #define RTL8366RB_VLAN_STAG_IDX_MASK 0x7
  225. #define RTL8366RB_VLAN_STAG_IDX_SHIFT 5
  226. #define RTL8366RB_VLAN_FID_MASK 0x7
  227. /* Port ingress bandwidth control */
  228. #define RTL8366RB_IB_BASE 0x0200
  229. #define RTL8366RB_IB_REG(pnum) (RTL8366RB_IB_BASE + (pnum))
  230. #define RTL8366RB_IB_BDTH_MASK 0x3fff
  231. #define RTL8366RB_IB_PREIFG BIT(14)
  232. /* Port egress bandwidth control */
  233. #define RTL8366RB_EB_BASE 0x02d1
  234. #define RTL8366RB_EB_REG(pnum) (RTL8366RB_EB_BASE + (pnum))
  235. #define RTL8366RB_EB_BDTH_MASK 0x3fff
  236. #define RTL8366RB_EB_PREIFG_REG 0x02f8
  237. #define RTL8366RB_EB_PREIFG BIT(9)
  238. #define RTL8366RB_BDTH_SW_MAX 1048512 /* 1048576? */
  239. #define RTL8366RB_BDTH_UNIT 64
  240. #define RTL8366RB_BDTH_REG_DEFAULT 16383
  241. /* QOS */
  242. #define RTL8366RB_QOS BIT(15)
  243. /* Include/Exclude Preamble and IFG (20 bytes). 0:Exclude, 1:Include. */
  244. #define RTL8366RB_QOS_DEFAULT_PREIFG 1
  245. /* Interrupt handling */
  246. #define RTL8366RB_INTERRUPT_CONTROL_REG 0x0440
  247. #define RTL8366RB_INTERRUPT_POLARITY BIT(0)
  248. #define RTL8366RB_P4_RGMII_LED BIT(2)
  249. #define RTL8366RB_INTERRUPT_MASK_REG 0x0441
  250. #define RTL8366RB_INTERRUPT_LINK_CHGALL GENMASK(11, 0)
  251. #define RTL8366RB_INTERRUPT_ACLEXCEED BIT(8)
  252. #define RTL8366RB_INTERRUPT_STORMEXCEED BIT(9)
  253. #define RTL8366RB_INTERRUPT_P4_FIBER BIT(12)
  254. #define RTL8366RB_INTERRUPT_P4_UTP BIT(13)
  255. #define RTL8366RB_INTERRUPT_VALID (RTL8366RB_INTERRUPT_LINK_CHGALL | \
  256. RTL8366RB_INTERRUPT_ACLEXCEED | \
  257. RTL8366RB_INTERRUPT_STORMEXCEED | \
  258. RTL8366RB_INTERRUPT_P4_FIBER | \
  259. RTL8366RB_INTERRUPT_P4_UTP)
  260. #define RTL8366RB_INTERRUPT_STATUS_REG 0x0442
  261. #define RTL8366RB_NUM_INTERRUPT 14 /* 0..13 */
  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_smi *smi,
  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(smi->map, addr, 0); /* Write whatever */
  320. if (ret)
  321. return ret;
  322. /* Read MIB control register */
  323. ret = regmap_read(smi->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(smi->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_smi *smi = irq_data_get_irq_chip_data(d);
  356. int ret;
  357. ret = regmap_update_bits(smi->map, RTL8366RB_INTERRUPT_MASK_REG,
  358. rtl8366rb_get_irqmask(d), 0);
  359. if (ret)
  360. dev_err(smi->dev, "could not mask IRQ\n");
  361. }
  362. static void rtl8366rb_unmask_irq(struct irq_data *d)
  363. {
  364. struct realtek_smi *smi = irq_data_get_irq_chip_data(d);
  365. int ret;
  366. ret = regmap_update_bits(smi->map, RTL8366RB_INTERRUPT_MASK_REG,
  367. rtl8366rb_get_irqmask(d),
  368. rtl8366rb_get_irqmask(d));
  369. if (ret)
  370. dev_err(smi->dev, "could not unmask IRQ\n");
  371. }
  372. static irqreturn_t rtl8366rb_irq(int irq, void *data)
  373. {
  374. struct realtek_smi *smi = data;
  375. u32 stat;
  376. int ret;
  377. /* This clears the IRQ status register */
  378. ret = regmap_read(smi->map, RTL8366RB_INTERRUPT_STATUS_REG,
  379. &stat);
  380. if (ret) {
  381. dev_err(smi->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(smi->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_smi *smi)
  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(smi->dev->of_node, "interrupt-controller");
  435. if (!intc) {
  436. dev_err(smi->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(smi->dev, "failed to get parent IRQ\n");
  443. return irq ? irq : -EINVAL;
  444. }
  445. /* This clears the IRQ status register */
  446. ret = regmap_read(smi->map, RTL8366RB_INTERRUPT_STATUS_REG,
  447. &val);
  448. if (ret) {
  449. dev_err(smi->dev, "can't read interrupt status\n");
  450. return ret;
  451. }
  452. /* Fetch IRQ edge information from the descriptor */
  453. irq_trig = irqd_get_trigger_type(irq_get_irq_data(irq));
  454. switch (irq_trig) {
  455. case IRQF_TRIGGER_RISING:
  456. case IRQF_TRIGGER_HIGH:
  457. dev_info(smi->dev, "active high/rising IRQ\n");
  458. val = 0;
  459. break;
  460. case IRQF_TRIGGER_FALLING:
  461. case IRQF_TRIGGER_LOW:
  462. dev_info(smi->dev, "active low/falling IRQ\n");
  463. val = RTL8366RB_INTERRUPT_POLARITY;
  464. break;
  465. }
  466. ret = regmap_update_bits(smi->map, RTL8366RB_INTERRUPT_CONTROL_REG,
  467. RTL8366RB_INTERRUPT_POLARITY,
  468. val);
  469. if (ret) {
  470. dev_err(smi->dev, "could not configure IRQ polarity\n");
  471. return ret;
  472. }
  473. ret = devm_request_threaded_irq(smi->dev, irq, NULL,
  474. rtl8366rb_irq, IRQF_ONESHOT,
  475. "RTL8366RB", smi);
  476. if (ret) {
  477. dev_err(smi->dev, "unable to request irq: %d\n", ret);
  478. return ret;
  479. }
  480. smi->irqdomain = irq_domain_add_linear(intc,
  481. RTL8366RB_NUM_INTERRUPT,
  482. &rtl8366rb_irqdomain_ops,
  483. smi);
  484. if (!smi->irqdomain) {
  485. dev_err(smi->dev, "failed to create IRQ domain\n");
  486. return -EINVAL;
  487. }
  488. for (i = 0; i < smi->num_ports; i++)
  489. irq_set_parent(irq_create_mapping(smi->irqdomain, i), irq);
  490. return 0;
  491. }
  492. static int rtl8366rb_set_addr(struct realtek_smi *smi)
  493. {
  494. u8 addr[ETH_ALEN];
  495. u16 val;
  496. int ret;
  497. eth_random_addr(addr);
  498. dev_info(smi->dev, "set MAC: %02X:%02X:%02X:%02X:%02X:%02X\n",
  499. addr[0], addr[1], addr[2], addr[3], addr[4], addr[5]);
  500. val = addr[0] << 8 | addr[1];
  501. ret = regmap_write(smi->map, RTL8366RB_SMAR0, val);
  502. if (ret)
  503. return ret;
  504. val = addr[2] << 8 | addr[3];
  505. ret = regmap_write(smi->map, RTL8366RB_SMAR1, val);
  506. if (ret)
  507. return ret;
  508. val = addr[4] << 8 | addr[5];
  509. ret = regmap_write(smi->map, RTL8366RB_SMAR2, val);
  510. if (ret)
  511. return ret;
  512. return 0;
  513. }
  514. /* Found in a vendor driver */
  515. /* For the "version 0" early silicon, appear in most source releases */
  516. static const u16 rtl8366rb_init_jam_ver_0[] = {
  517. 0x000B, 0x0001, 0x03A6, 0x0100, 0x03A7, 0x0001, 0x02D1, 0x3FFF,
  518. 0x02D2, 0x3FFF, 0x02D3, 0x3FFF, 0x02D4, 0x3FFF, 0x02D5, 0x3FFF,
  519. 0x02D6, 0x3FFF, 0x02D7, 0x3FFF, 0x02D8, 0x3FFF, 0x022B, 0x0688,
  520. 0x022C, 0x0FAC, 0x03D0, 0x4688, 0x03D1, 0x01F5, 0x0000, 0x0830,
  521. 0x02F9, 0x0200, 0x02F7, 0x7FFF, 0x02F8, 0x03FF, 0x0080, 0x03E8,
  522. 0x0081, 0x00CE, 0x0082, 0x00DA, 0x0083, 0x0230, 0xBE0F, 0x2000,
  523. 0x0231, 0x422A, 0x0232, 0x422A, 0x0233, 0x422A, 0x0234, 0x422A,
  524. 0x0235, 0x422A, 0x0236, 0x422A, 0x0237, 0x422A, 0x0238, 0x422A,
  525. 0x0239, 0x422A, 0x023A, 0x422A, 0x023B, 0x422A, 0x023C, 0x422A,
  526. 0x023D, 0x422A, 0x023E, 0x422A, 0x023F, 0x422A, 0x0240, 0x422A,
  527. 0x0241, 0x422A, 0x0242, 0x422A, 0x0243, 0x422A, 0x0244, 0x422A,
  528. 0x0245, 0x422A, 0x0246, 0x422A, 0x0247, 0x422A, 0x0248, 0x422A,
  529. 0x0249, 0x0146, 0x024A, 0x0146, 0x024B, 0x0146, 0xBE03, 0xC961,
  530. 0x024D, 0x0146, 0x024E, 0x0146, 0x024F, 0x0146, 0x0250, 0x0146,
  531. 0xBE64, 0x0226, 0x0252, 0x0146, 0x0253, 0x0146, 0x024C, 0x0146,
  532. 0x0251, 0x0146, 0x0254, 0x0146, 0xBE62, 0x3FD0, 0x0084, 0x0320,
  533. 0x0255, 0x0146, 0x0256, 0x0146, 0x0257, 0x0146, 0x0258, 0x0146,
  534. 0x0259, 0x0146, 0x025A, 0x0146, 0x025B, 0x0146, 0x025C, 0x0146,
  535. 0x025D, 0x0146, 0x025E, 0x0146, 0x025F, 0x0146, 0x0260, 0x0146,
  536. 0x0261, 0xA23F, 0x0262, 0x0294, 0x0263, 0xA23F, 0x0264, 0x0294,
  537. 0x0265, 0xA23F, 0x0266, 0x0294, 0x0267, 0xA23F, 0x0268, 0x0294,
  538. 0x0269, 0xA23F, 0x026A, 0x0294, 0x026B, 0xA23F, 0x026C, 0x0294,
  539. 0x026D, 0xA23F, 0x026E, 0x0294, 0x026F, 0xA23F, 0x0270, 0x0294,
  540. 0x02F5, 0x0048, 0xBE09, 0x0E00, 0xBE1E, 0x0FA0, 0xBE14, 0x8448,
  541. 0xBE15, 0x1007, 0xBE4A, 0xA284, 0xC454, 0x3F0B, 0xC474, 0x3F0B,
  542. 0xBE48, 0x3672, 0xBE4B, 0x17A7, 0xBE4C, 0x0B15, 0xBE52, 0x0EDD,
  543. 0xBE49, 0x8C00, 0xBE5B, 0x785C, 0xBE5C, 0x785C, 0xBE5D, 0x785C,
  544. 0xBE61, 0x368A, 0xBE63, 0x9B84, 0xC456, 0xCC13, 0xC476, 0xCC13,
  545. 0xBE65, 0x307D, 0xBE6D, 0x0005, 0xBE6E, 0xE120, 0xBE2E, 0x7BAF,
  546. };
  547. /* This v1 init sequence is from Belkin F5D8235 U-Boot release */
  548. static const u16 rtl8366rb_init_jam_ver_1[] = {
  549. 0x0000, 0x0830, 0x0001, 0x8000, 0x0400, 0x8130, 0xBE78, 0x3C3C,
  550. 0x0431, 0x5432, 0xBE37, 0x0CE4, 0x02FA, 0xFFDF, 0x02FB, 0xFFE0,
  551. 0xC44C, 0x1585, 0xC44C, 0x1185, 0xC44C, 0x1585, 0xC46C, 0x1585,
  552. 0xC46C, 0x1185, 0xC46C, 0x1585, 0xC451, 0x2135, 0xC471, 0x2135,
  553. 0xBE10, 0x8140, 0xBE15, 0x0007, 0xBE6E, 0xE120, 0xBE69, 0xD20F,
  554. 0xBE6B, 0x0320, 0xBE24, 0xB000, 0xBE23, 0xFF51, 0xBE22, 0xDF20,
  555. 0xBE21, 0x0140, 0xBE20, 0x00BB, 0xBE24, 0xB800, 0xBE24, 0x0000,
  556. 0xBE24, 0x7000, 0xBE23, 0xFF51, 0xBE22, 0xDF60, 0xBE21, 0x0140,
  557. 0xBE20, 0x0077, 0xBE24, 0x7800, 0xBE24, 0x0000, 0xBE2E, 0x7B7A,
  558. 0xBE36, 0x0CE4, 0x02F5, 0x0048, 0xBE77, 0x2940, 0x000A, 0x83E0,
  559. 0xBE79, 0x3C3C, 0xBE00, 0x1340,
  560. };
  561. /* This v2 init sequence is from Belkin F5D8235 U-Boot release */
  562. static const u16 rtl8366rb_init_jam_ver_2[] = {
  563. 0x0450, 0x0000, 0x0400, 0x8130, 0x000A, 0x83ED, 0x0431, 0x5432,
  564. 0xC44F, 0x6250, 0xC46F, 0x6250, 0xC456, 0x0C14, 0xC476, 0x0C14,
  565. 0xC44C, 0x1C85, 0xC44C, 0x1885, 0xC44C, 0x1C85, 0xC46C, 0x1C85,
  566. 0xC46C, 0x1885, 0xC46C, 0x1C85, 0xC44C, 0x0885, 0xC44C, 0x0881,
  567. 0xC44C, 0x0885, 0xC46C, 0x0885, 0xC46C, 0x0881, 0xC46C, 0x0885,
  568. 0xBE2E, 0x7BA7, 0xBE36, 0x1000, 0xBE37, 0x1000, 0x8000, 0x0001,
  569. 0xBE69, 0xD50F, 0x8000, 0x0000, 0xBE69, 0xD50F, 0xBE6E, 0x0320,
  570. 0xBE77, 0x2940, 0xBE78, 0x3C3C, 0xBE79, 0x3C3C, 0xBE6E, 0xE120,
  571. 0x8000, 0x0001, 0xBE15, 0x1007, 0x8000, 0x0000, 0xBE15, 0x1007,
  572. 0xBE14, 0x0448, 0xBE1E, 0x00A0, 0xBE10, 0x8160, 0xBE10, 0x8140,
  573. 0xBE00, 0x1340, 0x0F51, 0x0010,
  574. };
  575. /* Appears in a DDWRT code dump */
  576. static const u16 rtl8366rb_init_jam_ver_3[] = {
  577. 0x0000, 0x0830, 0x0400, 0x8130, 0x000A, 0x83ED, 0x0431, 0x5432,
  578. 0x0F51, 0x0017, 0x02F5, 0x0048, 0x02FA, 0xFFDF, 0x02FB, 0xFFE0,
  579. 0xC456, 0x0C14, 0xC476, 0x0C14, 0xC454, 0x3F8B, 0xC474, 0x3F8B,
  580. 0xC450, 0x2071, 0xC470, 0x2071, 0xC451, 0x226B, 0xC471, 0x226B,
  581. 0xC452, 0xA293, 0xC472, 0xA293, 0xC44C, 0x1585, 0xC44C, 0x1185,
  582. 0xC44C, 0x1585, 0xC46C, 0x1585, 0xC46C, 0x1185, 0xC46C, 0x1585,
  583. 0xC44C, 0x0185, 0xC44C, 0x0181, 0xC44C, 0x0185, 0xC46C, 0x0185,
  584. 0xC46C, 0x0181, 0xC46C, 0x0185, 0xBE24, 0xB000, 0xBE23, 0xFF51,
  585. 0xBE22, 0xDF20, 0xBE21, 0x0140, 0xBE20, 0x00BB, 0xBE24, 0xB800,
  586. 0xBE24, 0x0000, 0xBE24, 0x7000, 0xBE23, 0xFF51, 0xBE22, 0xDF60,
  587. 0xBE21, 0x0140, 0xBE20, 0x0077, 0xBE24, 0x7800, 0xBE24, 0x0000,
  588. 0xBE2E, 0x7BA7, 0xBE36, 0x1000, 0xBE37, 0x1000, 0x8000, 0x0001,
  589. 0xBE69, 0xD50F, 0x8000, 0x0000, 0xBE69, 0xD50F, 0xBE6B, 0x0320,
  590. 0xBE77, 0x2800, 0xBE78, 0x3C3C, 0xBE79, 0x3C3C, 0xBE6E, 0xE120,
  591. 0x8000, 0x0001, 0xBE10, 0x8140, 0x8000, 0x0000, 0xBE10, 0x8140,
  592. 0xBE15, 0x1007, 0xBE14, 0x0448, 0xBE1E, 0x00A0, 0xBE10, 0x8160,
  593. 0xBE10, 0x8140, 0xBE00, 0x1340, 0x0450, 0x0000, 0x0401, 0x0000,
  594. };
  595. /* Belkin F5D8235 v1, "belkin,f5d8235-v1" */
  596. static const u16 rtl8366rb_init_jam_f5d8235[] = {
  597. 0x0242, 0x02BF, 0x0245, 0x02BF, 0x0248, 0x02BF, 0x024B, 0x02BF,
  598. 0x024E, 0x02BF, 0x0251, 0x02BF, 0x0254, 0x0A3F, 0x0256, 0x0A3F,
  599. 0x0258, 0x0A3F, 0x025A, 0x0A3F, 0x025C, 0x0A3F, 0x025E, 0x0A3F,
  600. 0x0263, 0x007C, 0x0100, 0x0004, 0xBE5B, 0x3500, 0x800E, 0x200F,
  601. 0xBE1D, 0x0F00, 0x8001, 0x5011, 0x800A, 0xA2F4, 0x800B, 0x17A3,
  602. 0xBE4B, 0x17A3, 0xBE41, 0x5011, 0xBE17, 0x2100, 0x8000, 0x8304,
  603. 0xBE40, 0x8304, 0xBE4A, 0xA2F4, 0x800C, 0xA8D5, 0x8014, 0x5500,
  604. 0x8015, 0x0004, 0xBE4C, 0xA8D5, 0xBE59, 0x0008, 0xBE09, 0x0E00,
  605. 0xBE36, 0x1036, 0xBE37, 0x1036, 0x800D, 0x00FF, 0xBE4D, 0x00FF,
  606. };
  607. /* DGN3500, "netgear,dgn3500", "netgear,dgn3500b" */
  608. static const u16 rtl8366rb_init_jam_dgn3500[] = {
  609. 0x0000, 0x0830, 0x0400, 0x8130, 0x000A, 0x83ED, 0x0F51, 0x0017,
  610. 0x02F5, 0x0048, 0x02FA, 0xFFDF, 0x02FB, 0xFFE0, 0x0450, 0x0000,
  611. 0x0401, 0x0000, 0x0431, 0x0960,
  612. };
  613. /* This jam table activates "green ethernet", which means low power mode
  614. * and is claimed to detect the cable length and not use more power than
  615. * necessary, and the ports should enter power saving mode 10 seconds after
  616. * a cable is disconnected. Seems to always be the same.
  617. */
  618. static const u16 rtl8366rb_green_jam[][2] = {
  619. {0xBE78, 0x323C}, {0xBE77, 0x5000}, {0xBE2E, 0x7BA7},
  620. {0xBE59, 0x3459}, {0xBE5A, 0x745A}, {0xBE5B, 0x785C},
  621. {0xBE5C, 0x785C}, {0xBE6E, 0xE120}, {0xBE79, 0x323C},
  622. };
  623. static int rtl8366rb_setup(struct dsa_switch *ds)
  624. {
  625. struct realtek_smi *smi = ds->priv;
  626. const u16 *jam_table;
  627. u32 chip_ver = 0;
  628. u32 chip_id = 0;
  629. int jam_size;
  630. u32 val;
  631. int ret;
  632. int i;
  633. ret = regmap_read(smi->map, RTL8366RB_CHIP_ID_REG, &chip_id);
  634. if (ret) {
  635. dev_err(smi->dev, "unable to read chip id\n");
  636. return ret;
  637. }
  638. switch (chip_id) {
  639. case RTL8366RB_CHIP_ID_8366:
  640. break;
  641. default:
  642. dev_err(smi->dev, "unknown chip id (%04x)\n", chip_id);
  643. return -ENODEV;
  644. }
  645. ret = regmap_read(smi->map, RTL8366RB_CHIP_VERSION_CTRL_REG,
  646. &chip_ver);
  647. if (ret) {
  648. dev_err(smi->dev, "unable to read chip version\n");
  649. return ret;
  650. }
  651. dev_info(smi->dev, "RTL%04x ver %u chip found\n",
  652. chip_id, chip_ver & RTL8366RB_CHIP_VERSION_MASK);
  653. /* Do the init dance using the right jam table */
  654. switch (chip_ver) {
  655. case 0:
  656. jam_table = rtl8366rb_init_jam_ver_0;
  657. jam_size = ARRAY_SIZE(rtl8366rb_init_jam_ver_0);
  658. break;
  659. case 1:
  660. jam_table = rtl8366rb_init_jam_ver_1;
  661. jam_size = ARRAY_SIZE(rtl8366rb_init_jam_ver_1);
  662. break;
  663. case 2:
  664. jam_table = rtl8366rb_init_jam_ver_2;
  665. jam_size = ARRAY_SIZE(rtl8366rb_init_jam_ver_2);
  666. break;
  667. default:
  668. jam_table = rtl8366rb_init_jam_ver_3;
  669. jam_size = ARRAY_SIZE(rtl8366rb_init_jam_ver_3);
  670. break;
  671. }
  672. /* Special jam tables for special routers
  673. * TODO: are these necessary? Maintainers, please test
  674. * without them, using just the off-the-shelf tables.
  675. */
  676. if (of_machine_is_compatible("belkin,f5d8235-v1")) {
  677. jam_table = rtl8366rb_init_jam_f5d8235;
  678. jam_size = ARRAY_SIZE(rtl8366rb_init_jam_f5d8235);
  679. }
  680. if (of_machine_is_compatible("netgear,dgn3500") ||
  681. of_machine_is_compatible("netgear,dgn3500b")) {
  682. jam_table = rtl8366rb_init_jam_dgn3500;
  683. jam_size = ARRAY_SIZE(rtl8366rb_init_jam_dgn3500);
  684. }
  685. i = 0;
  686. while (i < jam_size) {
  687. if ((jam_table[i] & 0xBE00) == 0xBE00) {
  688. ret = regmap_read(smi->map,
  689. RTL8366RB_PHY_ACCESS_BUSY_REG,
  690. &val);
  691. if (ret)
  692. return ret;
  693. if (!(val & RTL8366RB_PHY_INT_BUSY)) {
  694. ret = regmap_write(smi->map,
  695. RTL8366RB_PHY_ACCESS_CTRL_REG,
  696. RTL8366RB_PHY_CTRL_WRITE);
  697. if (ret)
  698. return ret;
  699. }
  700. }
  701. dev_dbg(smi->dev, "jam %04x into register %04x\n",
  702. jam_table[i + 1],
  703. jam_table[i]);
  704. ret = regmap_write(smi->map,
  705. jam_table[i],
  706. jam_table[i + 1]);
  707. if (ret)
  708. return ret;
  709. i += 2;
  710. }
  711. /* Set up the "green ethernet" feature */
  712. i = 0;
  713. while (i < ARRAY_SIZE(rtl8366rb_green_jam)) {
  714. ret = regmap_read(smi->map, RTL8366RB_PHY_ACCESS_BUSY_REG,
  715. &val);
  716. if (ret)
  717. return ret;
  718. if (!(val & RTL8366RB_PHY_INT_BUSY)) {
  719. ret = regmap_write(smi->map,
  720. RTL8366RB_PHY_ACCESS_CTRL_REG,
  721. RTL8366RB_PHY_CTRL_WRITE);
  722. if (ret)
  723. return ret;
  724. ret = regmap_write(smi->map,
  725. rtl8366rb_green_jam[i][0],
  726. rtl8366rb_green_jam[i][1]);
  727. if (ret)
  728. return ret;
  729. i++;
  730. }
  731. }
  732. ret = regmap_write(smi->map,
  733. RTL8366RB_GREEN_FEATURE_REG,
  734. (chip_ver == 1) ? 0x0007 : 0x0003);
  735. if (ret)
  736. return ret;
  737. /* Vendor driver sets 0x240 in registers 0xc and 0xd (undocumented) */
  738. ret = regmap_write(smi->map, 0x0c, 0x240);
  739. if (ret)
  740. return ret;
  741. ret = regmap_write(smi->map, 0x0d, 0x240);
  742. if (ret)
  743. return ret;
  744. /* Set some random MAC address */
  745. ret = rtl8366rb_set_addr(smi);
  746. if (ret)
  747. return ret;
  748. /* Enable CPU port and enable inserting CPU tag
  749. *
  750. * Disabling RTL8368RB_CPU_INSTAG here will change the behaviour
  751. * of the switch totally and it will start talking Realtek RRCP
  752. * internally. It is probably possible to experiment with this,
  753. * but then the kernel needs to understand and handle RRCP first.
  754. */
  755. ret = regmap_update_bits(smi->map, RTL8368RB_CPU_CTRL_REG,
  756. 0xFFFF,
  757. RTL8368RB_CPU_INSTAG | BIT(smi->cpu_port));
  758. if (ret)
  759. return ret;
  760. /* Make sure we default-enable the fixed CPU port */
  761. ret = regmap_update_bits(smi->map, RTL8366RB_PECR,
  762. BIT(smi->cpu_port),
  763. 0);
  764. if (ret)
  765. return ret;
  766. /* Set maximum packet length to 1536 bytes */
  767. ret = regmap_update_bits(smi->map, RTL8366RB_SGCR,
  768. RTL8366RB_SGCR_MAX_LENGTH_MASK,
  769. RTL8366RB_SGCR_MAX_LENGTH_1536);
  770. if (ret)
  771. return ret;
  772. /* Enable learning for all ports */
  773. ret = regmap_write(smi->map, RTL8366RB_SSCR0, 0);
  774. if (ret)
  775. return ret;
  776. /* Enable auto ageing for all ports */
  777. ret = regmap_write(smi->map, RTL8366RB_SSCR1, 0);
  778. if (ret)
  779. return ret;
  780. /* Port 4 setup: this enables Port 4, usually the WAN port,
  781. * common PHY IO mode is apparently mode 0, and this is not what
  782. * the port is initialized to. There is no explanation of the
  783. * IO modes in the Realtek source code, if your WAN port is
  784. * connected to something exotic such as fiber, then this might
  785. * be worth experimenting with.
  786. */
  787. ret = regmap_update_bits(smi->map, RTL8366RB_PMC0,
  788. RTL8366RB_PMC0_P4_IOMODE_MASK,
  789. 0 << RTL8366RB_PMC0_P4_IOMODE_SHIFT);
  790. if (ret)
  791. return ret;
  792. /* Discard VLAN tagged packets if the port is not a member of
  793. * the VLAN with which the packets is associated.
  794. */
  795. ret = regmap_write(smi->map, RTL8366RB_VLAN_INGRESS_CTRL2_REG,
  796. RTL8366RB_PORT_ALL);
  797. if (ret)
  798. return ret;
  799. /* Don't drop packets whose DA has not been learned */
  800. ret = regmap_update_bits(smi->map, RTL8366RB_SSCR2,
  801. RTL8366RB_SSCR2_DROP_UNKNOWN_DA, 0);
  802. if (ret)
  803. return ret;
  804. /* Set blinking, TODO: make this configurable */
  805. ret = regmap_update_bits(smi->map, RTL8366RB_LED_BLINKRATE_REG,
  806. RTL8366RB_LED_BLINKRATE_MASK,
  807. RTL8366RB_LED_BLINKRATE_56MS);
  808. if (ret)
  809. return ret;
  810. /* Set up LED activity:
  811. * Each port has 4 LEDs, we configure all ports to the same
  812. * behaviour (no individual config) but we can set up each
  813. * LED separately.
  814. */
  815. if (smi->leds_disabled) {
  816. /* Turn everything off */
  817. regmap_update_bits(smi->map,
  818. RTL8366RB_LED_0_1_CTRL_REG,
  819. 0x0FFF, 0);
  820. regmap_update_bits(smi->map,
  821. RTL8366RB_LED_2_3_CTRL_REG,
  822. 0x0FFF, 0);
  823. regmap_update_bits(smi->map,
  824. RTL8366RB_INTERRUPT_CONTROL_REG,
  825. RTL8366RB_P4_RGMII_LED,
  826. 0);
  827. val = RTL8366RB_LED_OFF;
  828. } else {
  829. /* TODO: make this configurable per LED */
  830. val = RTL8366RB_LED_FORCE;
  831. }
  832. for (i = 0; i < 4; i++) {
  833. ret = regmap_update_bits(smi->map,
  834. RTL8366RB_LED_CTRL_REG,
  835. 0xf << (i * 4),
  836. val << (i * 4));
  837. if (ret)
  838. return ret;
  839. }
  840. ret = rtl8366_init_vlan(smi);
  841. if (ret)
  842. return ret;
  843. ret = rtl8366rb_setup_cascaded_irq(smi);
  844. if (ret)
  845. dev_info(smi->dev, "no interrupt support\n");
  846. ret = realtek_smi_setup_mdio(smi);
  847. if (ret) {
  848. dev_info(smi->dev, "could not set up MDIO bus\n");
  849. return -ENODEV;
  850. }
  851. return 0;
  852. }
  853. static enum dsa_tag_protocol rtl8366_get_tag_protocol(struct dsa_switch *ds,
  854. int port)
  855. {
  856. /* For now, the RTL switches are handled without any custom tags.
  857. *
  858. * It is possible to turn on "custom tags" by removing the
  859. * RTL8368RB_CPU_INSTAG flag when enabling the port but what it
  860. * does is unfamiliar to DSA: ethernet frames of type 8899, the Realtek
  861. * Remote Control Protocol (RRCP) start to appear on the CPU port of
  862. * the device. So this is not the ordinary few extra bytes in the
  863. * frame. Instead it appears that the switch starts to talk Realtek
  864. * RRCP internally which means a pretty complex RRCP implementation
  865. * decoding and responding the RRCP protocol is needed to exploit this.
  866. *
  867. * The OpenRRCP project (dormant since 2009) have reverse-egineered
  868. * parts of the protocol.
  869. */
  870. return DSA_TAG_PROTO_NONE;
  871. }
  872. static void rtl8366rb_adjust_link(struct dsa_switch *ds, int port,
  873. struct phy_device *phydev)
  874. {
  875. struct realtek_smi *smi = ds->priv;
  876. int ret;
  877. if (port != smi->cpu_port)
  878. return;
  879. dev_info(smi->dev, "adjust link on CPU port (%d)\n", port);
  880. /* Force the fixed CPU port into 1Gbit mode, no autonegotiation */
  881. ret = regmap_update_bits(smi->map, RTL8366RB_MAC_FORCE_CTRL_REG,
  882. BIT(port), BIT(port));
  883. if (ret)
  884. return;
  885. ret = regmap_update_bits(smi->map, RTL8366RB_PAACR2,
  886. 0xFF00U,
  887. RTL8366RB_PAACR_CPU_PORT << 8);
  888. if (ret)
  889. return;
  890. /* Enable the CPU port */
  891. ret = regmap_update_bits(smi->map, RTL8366RB_PECR, BIT(port),
  892. 0);
  893. if (ret)
  894. return;
  895. }
  896. static void rb8366rb_set_port_led(struct realtek_smi *smi,
  897. int port, bool enable)
  898. {
  899. u16 val = enable ? 0x3f : 0;
  900. int ret;
  901. if (smi->leds_disabled)
  902. return;
  903. switch (port) {
  904. case 0:
  905. ret = regmap_update_bits(smi->map,
  906. RTL8366RB_LED_0_1_CTRL_REG,
  907. 0x3F, val);
  908. break;
  909. case 1:
  910. ret = regmap_update_bits(smi->map,
  911. RTL8366RB_LED_0_1_CTRL_REG,
  912. 0x3F << RTL8366RB_LED_1_OFFSET,
  913. val << RTL8366RB_LED_1_OFFSET);
  914. break;
  915. case 2:
  916. ret = regmap_update_bits(smi->map,
  917. RTL8366RB_LED_2_3_CTRL_REG,
  918. 0x3F, val);
  919. break;
  920. case 3:
  921. ret = regmap_update_bits(smi->map,
  922. RTL8366RB_LED_2_3_CTRL_REG,
  923. 0x3F << RTL8366RB_LED_3_OFFSET,
  924. val << RTL8366RB_LED_3_OFFSET);
  925. break;
  926. case 4:
  927. ret = regmap_update_bits(smi->map,
  928. RTL8366RB_INTERRUPT_CONTROL_REG,
  929. RTL8366RB_P4_RGMII_LED,
  930. enable ? RTL8366RB_P4_RGMII_LED : 0);
  931. break;
  932. default:
  933. dev_err(smi->dev, "no LED for port %d\n", port);
  934. return;
  935. }
  936. if (ret)
  937. dev_err(smi->dev, "error updating LED on port %d\n", port);
  938. }
  939. static int
  940. rtl8366rb_port_enable(struct dsa_switch *ds, int port,
  941. struct phy_device *phy)
  942. {
  943. struct realtek_smi *smi = ds->priv;
  944. int ret;
  945. dev_dbg(smi->dev, "enable port %d\n", port);
  946. ret = regmap_update_bits(smi->map, RTL8366RB_PECR, BIT(port),
  947. 0);
  948. if (ret)
  949. return ret;
  950. rb8366rb_set_port_led(smi, port, true);
  951. return 0;
  952. }
  953. static void
  954. rtl8366rb_port_disable(struct dsa_switch *ds, int port,
  955. struct phy_device *phy)
  956. {
  957. struct realtek_smi *smi = ds->priv;
  958. int ret;
  959. dev_dbg(smi->dev, "disable port %d\n", port);
  960. ret = regmap_update_bits(smi->map, RTL8366RB_PECR, BIT(port),
  961. BIT(port));
  962. if (ret)
  963. return;
  964. rb8366rb_set_port_led(smi, port, false);
  965. }
  966. static int rtl8366rb_get_vlan_4k(struct realtek_smi *smi, u32 vid,
  967. struct rtl8366_vlan_4k *vlan4k)
  968. {
  969. u32 data[3];
  970. int ret;
  971. int i;
  972. memset(vlan4k, '\0', sizeof(struct rtl8366_vlan_4k));
  973. if (vid >= RTL8366RB_NUM_VIDS)
  974. return -EINVAL;
  975. /* write VID */
  976. ret = regmap_write(smi->map, RTL8366RB_VLAN_TABLE_WRITE_BASE,
  977. vid & RTL8366RB_VLAN_VID_MASK);
  978. if (ret)
  979. return ret;
  980. /* write table access control word */
  981. ret = regmap_write(smi->map, RTL8366RB_TABLE_ACCESS_CTRL_REG,
  982. RTL8366RB_TABLE_VLAN_READ_CTRL);
  983. if (ret)
  984. return ret;
  985. for (i = 0; i < 3; i++) {
  986. ret = regmap_read(smi->map,
  987. RTL8366RB_VLAN_TABLE_READ_BASE + i,
  988. &data[i]);
  989. if (ret)
  990. return ret;
  991. }
  992. vlan4k->vid = vid;
  993. vlan4k->untag = (data[1] >> RTL8366RB_VLAN_UNTAG_SHIFT) &
  994. RTL8366RB_VLAN_UNTAG_MASK;
  995. vlan4k->member = data[1] & RTL8366RB_VLAN_MEMBER_MASK;
  996. vlan4k->fid = data[2] & RTL8366RB_VLAN_FID_MASK;
  997. return 0;
  998. }
  999. static int rtl8366rb_set_vlan_4k(struct realtek_smi *smi,
  1000. const struct rtl8366_vlan_4k *vlan4k)
  1001. {
  1002. u32 data[3];
  1003. int ret;
  1004. int i;
  1005. if (vlan4k->vid >= RTL8366RB_NUM_VIDS ||
  1006. vlan4k->member > RTL8366RB_VLAN_MEMBER_MASK ||
  1007. vlan4k->untag > RTL8366RB_VLAN_UNTAG_MASK ||
  1008. vlan4k->fid > RTL8366RB_FIDMAX)
  1009. return -EINVAL;
  1010. data[0] = vlan4k->vid & RTL8366RB_VLAN_VID_MASK;
  1011. data[1] = (vlan4k->member & RTL8366RB_VLAN_MEMBER_MASK) |
  1012. ((vlan4k->untag & RTL8366RB_VLAN_UNTAG_MASK) <<
  1013. RTL8366RB_VLAN_UNTAG_SHIFT);
  1014. data[2] = vlan4k->fid & RTL8366RB_VLAN_FID_MASK;
  1015. for (i = 0; i < 3; i++) {
  1016. ret = regmap_write(smi->map,
  1017. RTL8366RB_VLAN_TABLE_WRITE_BASE + i,
  1018. data[i]);
  1019. if (ret)
  1020. return ret;
  1021. }
  1022. /* write table access control word */
  1023. ret = regmap_write(smi->map, RTL8366RB_TABLE_ACCESS_CTRL_REG,
  1024. RTL8366RB_TABLE_VLAN_WRITE_CTRL);
  1025. return ret;
  1026. }
  1027. static int rtl8366rb_get_vlan_mc(struct realtek_smi *smi, u32 index,
  1028. struct rtl8366_vlan_mc *vlanmc)
  1029. {
  1030. u32 data[3];
  1031. int ret;
  1032. int i;
  1033. memset(vlanmc, '\0', sizeof(struct rtl8366_vlan_mc));
  1034. if (index >= RTL8366RB_NUM_VLANS)
  1035. return -EINVAL;
  1036. for (i = 0; i < 3; i++) {
  1037. ret = regmap_read(smi->map,
  1038. RTL8366RB_VLAN_MC_BASE(index) + i,
  1039. &data[i]);
  1040. if (ret)
  1041. return ret;
  1042. }
  1043. vlanmc->vid = data[0] & RTL8366RB_VLAN_VID_MASK;
  1044. vlanmc->priority = (data[0] >> RTL8366RB_VLAN_PRIORITY_SHIFT) &
  1045. RTL8366RB_VLAN_PRIORITY_MASK;
  1046. vlanmc->untag = (data[1] >> RTL8366RB_VLAN_UNTAG_SHIFT) &
  1047. RTL8366RB_VLAN_UNTAG_MASK;
  1048. vlanmc->member = data[1] & RTL8366RB_VLAN_MEMBER_MASK;
  1049. vlanmc->fid = data[2] & RTL8366RB_VLAN_FID_MASK;
  1050. return 0;
  1051. }
  1052. static int rtl8366rb_set_vlan_mc(struct realtek_smi *smi, u32 index,
  1053. const struct rtl8366_vlan_mc *vlanmc)
  1054. {
  1055. u32 data[3];
  1056. int ret;
  1057. int i;
  1058. if (index >= RTL8366RB_NUM_VLANS ||
  1059. vlanmc->vid >= RTL8366RB_NUM_VIDS ||
  1060. vlanmc->priority > RTL8366RB_PRIORITYMAX ||
  1061. vlanmc->member > RTL8366RB_VLAN_MEMBER_MASK ||
  1062. vlanmc->untag > RTL8366RB_VLAN_UNTAG_MASK ||
  1063. vlanmc->fid > RTL8366RB_FIDMAX)
  1064. return -EINVAL;
  1065. data[0] = (vlanmc->vid & RTL8366RB_VLAN_VID_MASK) |
  1066. ((vlanmc->priority & RTL8366RB_VLAN_PRIORITY_MASK) <<
  1067. RTL8366RB_VLAN_PRIORITY_SHIFT);
  1068. data[1] = (vlanmc->member & RTL8366RB_VLAN_MEMBER_MASK) |
  1069. ((vlanmc->untag & RTL8366RB_VLAN_UNTAG_MASK) <<
  1070. RTL8366RB_VLAN_UNTAG_SHIFT);
  1071. data[2] = vlanmc->fid & RTL8366RB_VLAN_FID_MASK;
  1072. for (i = 0; i < 3; i++) {
  1073. ret = regmap_write(smi->map,
  1074. RTL8366RB_VLAN_MC_BASE(index) + i,
  1075. data[i]);
  1076. if (ret)
  1077. return ret;
  1078. }
  1079. return 0;
  1080. }
  1081. static int rtl8366rb_get_mc_index(struct realtek_smi *smi, int port, int *val)
  1082. {
  1083. u32 data;
  1084. int ret;
  1085. if (port >= smi->num_ports)
  1086. return -EINVAL;
  1087. ret = regmap_read(smi->map, RTL8366RB_PORT_VLAN_CTRL_REG(port),
  1088. &data);
  1089. if (ret)
  1090. return ret;
  1091. *val = (data >> RTL8366RB_PORT_VLAN_CTRL_SHIFT(port)) &
  1092. RTL8366RB_PORT_VLAN_CTRL_MASK;
  1093. return 0;
  1094. }
  1095. static int rtl8366rb_set_mc_index(struct realtek_smi *smi, int port, int index)
  1096. {
  1097. if (port >= smi->num_ports || index >= RTL8366RB_NUM_VLANS)
  1098. return -EINVAL;
  1099. return regmap_update_bits(smi->map, RTL8366RB_PORT_VLAN_CTRL_REG(port),
  1100. RTL8366RB_PORT_VLAN_CTRL_MASK <<
  1101. RTL8366RB_PORT_VLAN_CTRL_SHIFT(port),
  1102. (index & RTL8366RB_PORT_VLAN_CTRL_MASK) <<
  1103. RTL8366RB_PORT_VLAN_CTRL_SHIFT(port));
  1104. }
  1105. static bool rtl8366rb_is_vlan_valid(struct realtek_smi *smi, unsigned int vlan)
  1106. {
  1107. unsigned int max = RTL8366RB_NUM_VLANS;
  1108. if (smi->vlan4k_enabled)
  1109. max = RTL8366RB_NUM_VIDS - 1;
  1110. if (vlan == 0 || vlan >= max)
  1111. return false;
  1112. return true;
  1113. }
  1114. static int rtl8366rb_enable_vlan(struct realtek_smi *smi, bool enable)
  1115. {
  1116. dev_dbg(smi->dev, "%s VLAN\n", enable ? "enable" : "disable");
  1117. return regmap_update_bits(smi->map,
  1118. RTL8366RB_SGCR, RTL8366RB_SGCR_EN_VLAN,
  1119. enable ? RTL8366RB_SGCR_EN_VLAN : 0);
  1120. }
  1121. static int rtl8366rb_enable_vlan4k(struct realtek_smi *smi, bool enable)
  1122. {
  1123. dev_dbg(smi->dev, "%s VLAN 4k\n", enable ? "enable" : "disable");
  1124. return regmap_update_bits(smi->map, RTL8366RB_SGCR,
  1125. RTL8366RB_SGCR_EN_VLAN_4KTB,
  1126. enable ? RTL8366RB_SGCR_EN_VLAN_4KTB : 0);
  1127. }
  1128. static int rtl8366rb_phy_read(struct realtek_smi *smi, int phy, int regnum)
  1129. {
  1130. u32 val;
  1131. u32 reg;
  1132. int ret;
  1133. if (phy > RTL8366RB_PHY_NO_MAX)
  1134. return -EINVAL;
  1135. ret = regmap_write(smi->map, RTL8366RB_PHY_ACCESS_CTRL_REG,
  1136. RTL8366RB_PHY_CTRL_READ);
  1137. if (ret)
  1138. return ret;
  1139. reg = 0x8000 | (1 << (phy + RTL8366RB_PHY_NO_OFFSET)) | regnum;
  1140. ret = regmap_write(smi->map, reg, 0);
  1141. if (ret) {
  1142. dev_err(smi->dev,
  1143. "failed to write PHY%d reg %04x @ %04x, ret %d\n",
  1144. phy, regnum, reg, ret);
  1145. return ret;
  1146. }
  1147. ret = regmap_read(smi->map, RTL8366RB_PHY_ACCESS_DATA_REG, &val);
  1148. if (ret)
  1149. return ret;
  1150. dev_dbg(smi->dev, "read PHY%d register 0x%04x @ %08x, val <- %04x\n",
  1151. phy, regnum, reg, val);
  1152. return val;
  1153. }
  1154. static int rtl8366rb_phy_write(struct realtek_smi *smi, int phy, int regnum,
  1155. u16 val)
  1156. {
  1157. u32 reg;
  1158. int ret;
  1159. if (phy > RTL8366RB_PHY_NO_MAX)
  1160. return -EINVAL;
  1161. ret = regmap_write(smi->map, RTL8366RB_PHY_ACCESS_CTRL_REG,
  1162. RTL8366RB_PHY_CTRL_WRITE);
  1163. if (ret)
  1164. return ret;
  1165. reg = 0x8000 | (1 << (phy + RTL8366RB_PHY_NO_OFFSET)) | regnum;
  1166. dev_dbg(smi->dev, "write PHY%d register 0x%04x @ %04x, val -> %04x\n",
  1167. phy, regnum, reg, val);
  1168. ret = regmap_write(smi->map, reg, val);
  1169. if (ret)
  1170. return ret;
  1171. return 0;
  1172. }
  1173. static int rtl8366rb_reset_chip(struct realtek_smi *smi)
  1174. {
  1175. int timeout = 10;
  1176. u32 val;
  1177. int ret;
  1178. realtek_smi_write_reg_noack(smi, RTL8366RB_RESET_CTRL_REG,
  1179. RTL8366RB_CHIP_CTRL_RESET_HW);
  1180. do {
  1181. usleep_range(20000, 25000);
  1182. ret = regmap_read(smi->map, RTL8366RB_RESET_CTRL_REG, &val);
  1183. if (ret)
  1184. return ret;
  1185. if (!(val & RTL8366RB_CHIP_CTRL_RESET_HW))
  1186. break;
  1187. } while (--timeout);
  1188. if (!timeout) {
  1189. dev_err(smi->dev, "timeout waiting for the switch to reset\n");
  1190. return -EIO;
  1191. }
  1192. return 0;
  1193. }
  1194. static int rtl8366rb_detect(struct realtek_smi *smi)
  1195. {
  1196. struct device *dev = smi->dev;
  1197. int ret;
  1198. u32 val;
  1199. /* Detect device */
  1200. ret = regmap_read(smi->map, 0x5c, &val);
  1201. if (ret) {
  1202. dev_err(dev, "can't get chip ID (%d)\n", ret);
  1203. return ret;
  1204. }
  1205. switch (val) {
  1206. case 0x6027:
  1207. dev_info(dev, "found an RTL8366S switch\n");
  1208. dev_err(dev, "this switch is not yet supported, submit patches!\n");
  1209. return -ENODEV;
  1210. case 0x5937:
  1211. dev_info(dev, "found an RTL8366RB switch\n");
  1212. smi->cpu_port = RTL8366RB_PORT_NUM_CPU;
  1213. smi->num_ports = RTL8366RB_NUM_PORTS;
  1214. smi->num_vlan_mc = RTL8366RB_NUM_VLANS;
  1215. smi->mib_counters = rtl8366rb_mib_counters;
  1216. smi->num_mib_counters = ARRAY_SIZE(rtl8366rb_mib_counters);
  1217. break;
  1218. default:
  1219. dev_info(dev, "found an Unknown Realtek switch (id=0x%04x)\n",
  1220. val);
  1221. break;
  1222. }
  1223. ret = rtl8366rb_reset_chip(smi);
  1224. if (ret)
  1225. return ret;
  1226. return 0;
  1227. }
  1228. static const struct dsa_switch_ops rtl8366rb_switch_ops = {
  1229. .get_tag_protocol = rtl8366_get_tag_protocol,
  1230. .setup = rtl8366rb_setup,
  1231. .adjust_link = rtl8366rb_adjust_link,
  1232. .get_strings = rtl8366_get_strings,
  1233. .get_ethtool_stats = rtl8366_get_ethtool_stats,
  1234. .get_sset_count = rtl8366_get_sset_count,
  1235. .port_vlan_filtering = rtl8366_vlan_filtering,
  1236. .port_vlan_prepare = rtl8366_vlan_prepare,
  1237. .port_vlan_add = rtl8366_vlan_add,
  1238. .port_vlan_del = rtl8366_vlan_del,
  1239. .port_enable = rtl8366rb_port_enable,
  1240. .port_disable = rtl8366rb_port_disable,
  1241. };
  1242. static const struct realtek_smi_ops rtl8366rb_smi_ops = {
  1243. .detect = rtl8366rb_detect,
  1244. .get_vlan_mc = rtl8366rb_get_vlan_mc,
  1245. .set_vlan_mc = rtl8366rb_set_vlan_mc,
  1246. .get_vlan_4k = rtl8366rb_get_vlan_4k,
  1247. .set_vlan_4k = rtl8366rb_set_vlan_4k,
  1248. .get_mc_index = rtl8366rb_get_mc_index,
  1249. .set_mc_index = rtl8366rb_set_mc_index,
  1250. .get_mib_counter = rtl8366rb_get_mib_counter,
  1251. .is_vlan_valid = rtl8366rb_is_vlan_valid,
  1252. .enable_vlan = rtl8366rb_enable_vlan,
  1253. .enable_vlan4k = rtl8366rb_enable_vlan4k,
  1254. .phy_read = rtl8366rb_phy_read,
  1255. .phy_write = rtl8366rb_phy_write,
  1256. };
  1257. const struct realtek_smi_variant rtl8366rb_variant = {
  1258. .ds_ops = &rtl8366rb_switch_ops,
  1259. .ops = &rtl8366rb_smi_ops,
  1260. .clk_delay = 10,
  1261. .cmd_read = 0xa9,
  1262. .cmd_write = 0xa8,
  1263. };
  1264. EXPORT_SYMBOL_GPL(rtl8366rb_variant);