rtl8366rb.c 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458
  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-core.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. ret = irq ? irq : -EINVAL;
  444. goto out_put_node;
  445. }
  446. /* This clears the IRQ status register */
  447. ret = regmap_read(smi->map, RTL8366RB_INTERRUPT_STATUS_REG,
  448. &val);
  449. if (ret) {
  450. dev_err(smi->dev, "can't read interrupt status\n");
  451. goto out_put_node;
  452. }
  453. /* Fetch IRQ edge information from the descriptor */
  454. irq_trig = irqd_get_trigger_type(irq_get_irq_data(irq));
  455. switch (irq_trig) {
  456. case IRQF_TRIGGER_RISING:
  457. case IRQF_TRIGGER_HIGH:
  458. dev_info(smi->dev, "active high/rising IRQ\n");
  459. val = 0;
  460. break;
  461. case IRQF_TRIGGER_FALLING:
  462. case IRQF_TRIGGER_LOW:
  463. dev_info(smi->dev, "active low/falling IRQ\n");
  464. val = RTL8366RB_INTERRUPT_POLARITY;
  465. break;
  466. }
  467. ret = regmap_update_bits(smi->map, RTL8366RB_INTERRUPT_CONTROL_REG,
  468. RTL8366RB_INTERRUPT_POLARITY,
  469. val);
  470. if (ret) {
  471. dev_err(smi->dev, "could not configure IRQ polarity\n");
  472. goto out_put_node;
  473. }
  474. ret = devm_request_threaded_irq(smi->dev, irq, NULL,
  475. rtl8366rb_irq, IRQF_ONESHOT,
  476. "RTL8366RB", smi);
  477. if (ret) {
  478. dev_err(smi->dev, "unable to request irq: %d\n", ret);
  479. goto out_put_node;
  480. }
  481. smi->irqdomain = irq_domain_add_linear(intc,
  482. RTL8366RB_NUM_INTERRUPT,
  483. &rtl8366rb_irqdomain_ops,
  484. smi);
  485. if (!smi->irqdomain) {
  486. dev_err(smi->dev, "failed to create IRQ domain\n");
  487. ret = -EINVAL;
  488. goto out_put_node;
  489. }
  490. for (i = 0; i < smi->num_ports; i++)
  491. irq_set_parent(irq_create_mapping(smi->irqdomain, i), irq);
  492. out_put_node:
  493. of_node_put(intc);
  494. return ret;
  495. }
  496. static int rtl8366rb_set_addr(struct realtek_smi *smi)
  497. {
  498. u8 addr[ETH_ALEN];
  499. u16 val;
  500. int ret;
  501. eth_random_addr(addr);
  502. dev_info(smi->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(smi->map, RTL8366RB_SMAR0, val);
  506. if (ret)
  507. return ret;
  508. val = addr[2] << 8 | addr[3];
  509. ret = regmap_write(smi->map, RTL8366RB_SMAR1, val);
  510. if (ret)
  511. return ret;
  512. val = addr[4] << 8 | addr[5];
  513. ret = regmap_write(smi->map, RTL8366RB_SMAR2, val);
  514. if (ret)
  515. return ret;
  516. return 0;
  517. }
  518. /* Found in a vendor driver */
  519. /* For the "version 0" early silicon, appear in most source releases */
  520. static const u16 rtl8366rb_init_jam_ver_0[] = {
  521. 0x000B, 0x0001, 0x03A6, 0x0100, 0x03A7, 0x0001, 0x02D1, 0x3FFF,
  522. 0x02D2, 0x3FFF, 0x02D3, 0x3FFF, 0x02D4, 0x3FFF, 0x02D5, 0x3FFF,
  523. 0x02D6, 0x3FFF, 0x02D7, 0x3FFF, 0x02D8, 0x3FFF, 0x022B, 0x0688,
  524. 0x022C, 0x0FAC, 0x03D0, 0x4688, 0x03D1, 0x01F5, 0x0000, 0x0830,
  525. 0x02F9, 0x0200, 0x02F7, 0x7FFF, 0x02F8, 0x03FF, 0x0080, 0x03E8,
  526. 0x0081, 0x00CE, 0x0082, 0x00DA, 0x0083, 0x0230, 0xBE0F, 0x2000,
  527. 0x0231, 0x422A, 0x0232, 0x422A, 0x0233, 0x422A, 0x0234, 0x422A,
  528. 0x0235, 0x422A, 0x0236, 0x422A, 0x0237, 0x422A, 0x0238, 0x422A,
  529. 0x0239, 0x422A, 0x023A, 0x422A, 0x023B, 0x422A, 0x023C, 0x422A,
  530. 0x023D, 0x422A, 0x023E, 0x422A, 0x023F, 0x422A, 0x0240, 0x422A,
  531. 0x0241, 0x422A, 0x0242, 0x422A, 0x0243, 0x422A, 0x0244, 0x422A,
  532. 0x0245, 0x422A, 0x0246, 0x422A, 0x0247, 0x422A, 0x0248, 0x422A,
  533. 0x0249, 0x0146, 0x024A, 0x0146, 0x024B, 0x0146, 0xBE03, 0xC961,
  534. 0x024D, 0x0146, 0x024E, 0x0146, 0x024F, 0x0146, 0x0250, 0x0146,
  535. 0xBE64, 0x0226, 0x0252, 0x0146, 0x0253, 0x0146, 0x024C, 0x0146,
  536. 0x0251, 0x0146, 0x0254, 0x0146, 0xBE62, 0x3FD0, 0x0084, 0x0320,
  537. 0x0255, 0x0146, 0x0256, 0x0146, 0x0257, 0x0146, 0x0258, 0x0146,
  538. 0x0259, 0x0146, 0x025A, 0x0146, 0x025B, 0x0146, 0x025C, 0x0146,
  539. 0x025D, 0x0146, 0x025E, 0x0146, 0x025F, 0x0146, 0x0260, 0x0146,
  540. 0x0261, 0xA23F, 0x0262, 0x0294, 0x0263, 0xA23F, 0x0264, 0x0294,
  541. 0x0265, 0xA23F, 0x0266, 0x0294, 0x0267, 0xA23F, 0x0268, 0x0294,
  542. 0x0269, 0xA23F, 0x026A, 0x0294, 0x026B, 0xA23F, 0x026C, 0x0294,
  543. 0x026D, 0xA23F, 0x026E, 0x0294, 0x026F, 0xA23F, 0x0270, 0x0294,
  544. 0x02F5, 0x0048, 0xBE09, 0x0E00, 0xBE1E, 0x0FA0, 0xBE14, 0x8448,
  545. 0xBE15, 0x1007, 0xBE4A, 0xA284, 0xC454, 0x3F0B, 0xC474, 0x3F0B,
  546. 0xBE48, 0x3672, 0xBE4B, 0x17A7, 0xBE4C, 0x0B15, 0xBE52, 0x0EDD,
  547. 0xBE49, 0x8C00, 0xBE5B, 0x785C, 0xBE5C, 0x785C, 0xBE5D, 0x785C,
  548. 0xBE61, 0x368A, 0xBE63, 0x9B84, 0xC456, 0xCC13, 0xC476, 0xCC13,
  549. 0xBE65, 0x307D, 0xBE6D, 0x0005, 0xBE6E, 0xE120, 0xBE2E, 0x7BAF,
  550. };
  551. /* This v1 init sequence is from Belkin F5D8235 U-Boot release */
  552. static const u16 rtl8366rb_init_jam_ver_1[] = {
  553. 0x0000, 0x0830, 0x0001, 0x8000, 0x0400, 0x8130, 0xBE78, 0x3C3C,
  554. 0x0431, 0x5432, 0xBE37, 0x0CE4, 0x02FA, 0xFFDF, 0x02FB, 0xFFE0,
  555. 0xC44C, 0x1585, 0xC44C, 0x1185, 0xC44C, 0x1585, 0xC46C, 0x1585,
  556. 0xC46C, 0x1185, 0xC46C, 0x1585, 0xC451, 0x2135, 0xC471, 0x2135,
  557. 0xBE10, 0x8140, 0xBE15, 0x0007, 0xBE6E, 0xE120, 0xBE69, 0xD20F,
  558. 0xBE6B, 0x0320, 0xBE24, 0xB000, 0xBE23, 0xFF51, 0xBE22, 0xDF20,
  559. 0xBE21, 0x0140, 0xBE20, 0x00BB, 0xBE24, 0xB800, 0xBE24, 0x0000,
  560. 0xBE24, 0x7000, 0xBE23, 0xFF51, 0xBE22, 0xDF60, 0xBE21, 0x0140,
  561. 0xBE20, 0x0077, 0xBE24, 0x7800, 0xBE24, 0x0000, 0xBE2E, 0x7B7A,
  562. 0xBE36, 0x0CE4, 0x02F5, 0x0048, 0xBE77, 0x2940, 0x000A, 0x83E0,
  563. 0xBE79, 0x3C3C, 0xBE00, 0x1340,
  564. };
  565. /* This v2 init sequence is from Belkin F5D8235 U-Boot release */
  566. static const u16 rtl8366rb_init_jam_ver_2[] = {
  567. 0x0450, 0x0000, 0x0400, 0x8130, 0x000A, 0x83ED, 0x0431, 0x5432,
  568. 0xC44F, 0x6250, 0xC46F, 0x6250, 0xC456, 0x0C14, 0xC476, 0x0C14,
  569. 0xC44C, 0x1C85, 0xC44C, 0x1885, 0xC44C, 0x1C85, 0xC46C, 0x1C85,
  570. 0xC46C, 0x1885, 0xC46C, 0x1C85, 0xC44C, 0x0885, 0xC44C, 0x0881,
  571. 0xC44C, 0x0885, 0xC46C, 0x0885, 0xC46C, 0x0881, 0xC46C, 0x0885,
  572. 0xBE2E, 0x7BA7, 0xBE36, 0x1000, 0xBE37, 0x1000, 0x8000, 0x0001,
  573. 0xBE69, 0xD50F, 0x8000, 0x0000, 0xBE69, 0xD50F, 0xBE6E, 0x0320,
  574. 0xBE77, 0x2940, 0xBE78, 0x3C3C, 0xBE79, 0x3C3C, 0xBE6E, 0xE120,
  575. 0x8000, 0x0001, 0xBE15, 0x1007, 0x8000, 0x0000, 0xBE15, 0x1007,
  576. 0xBE14, 0x0448, 0xBE1E, 0x00A0, 0xBE10, 0x8160, 0xBE10, 0x8140,
  577. 0xBE00, 0x1340, 0x0F51, 0x0010,
  578. };
  579. /* Appears in a DDWRT code dump */
  580. static const u16 rtl8366rb_init_jam_ver_3[] = {
  581. 0x0000, 0x0830, 0x0400, 0x8130, 0x000A, 0x83ED, 0x0431, 0x5432,
  582. 0x0F51, 0x0017, 0x02F5, 0x0048, 0x02FA, 0xFFDF, 0x02FB, 0xFFE0,
  583. 0xC456, 0x0C14, 0xC476, 0x0C14, 0xC454, 0x3F8B, 0xC474, 0x3F8B,
  584. 0xC450, 0x2071, 0xC470, 0x2071, 0xC451, 0x226B, 0xC471, 0x226B,
  585. 0xC452, 0xA293, 0xC472, 0xA293, 0xC44C, 0x1585, 0xC44C, 0x1185,
  586. 0xC44C, 0x1585, 0xC46C, 0x1585, 0xC46C, 0x1185, 0xC46C, 0x1585,
  587. 0xC44C, 0x0185, 0xC44C, 0x0181, 0xC44C, 0x0185, 0xC46C, 0x0185,
  588. 0xC46C, 0x0181, 0xC46C, 0x0185, 0xBE24, 0xB000, 0xBE23, 0xFF51,
  589. 0xBE22, 0xDF20, 0xBE21, 0x0140, 0xBE20, 0x00BB, 0xBE24, 0xB800,
  590. 0xBE24, 0x0000, 0xBE24, 0x7000, 0xBE23, 0xFF51, 0xBE22, 0xDF60,
  591. 0xBE21, 0x0140, 0xBE20, 0x0077, 0xBE24, 0x7800, 0xBE24, 0x0000,
  592. 0xBE2E, 0x7BA7, 0xBE36, 0x1000, 0xBE37, 0x1000, 0x8000, 0x0001,
  593. 0xBE69, 0xD50F, 0x8000, 0x0000, 0xBE69, 0xD50F, 0xBE6B, 0x0320,
  594. 0xBE77, 0x2800, 0xBE78, 0x3C3C, 0xBE79, 0x3C3C, 0xBE6E, 0xE120,
  595. 0x8000, 0x0001, 0xBE10, 0x8140, 0x8000, 0x0000, 0xBE10, 0x8140,
  596. 0xBE15, 0x1007, 0xBE14, 0x0448, 0xBE1E, 0x00A0, 0xBE10, 0x8160,
  597. 0xBE10, 0x8140, 0xBE00, 0x1340, 0x0450, 0x0000, 0x0401, 0x0000,
  598. };
  599. /* Belkin F5D8235 v1, "belkin,f5d8235-v1" */
  600. static const u16 rtl8366rb_init_jam_f5d8235[] = {
  601. 0x0242, 0x02BF, 0x0245, 0x02BF, 0x0248, 0x02BF, 0x024B, 0x02BF,
  602. 0x024E, 0x02BF, 0x0251, 0x02BF, 0x0254, 0x0A3F, 0x0256, 0x0A3F,
  603. 0x0258, 0x0A3F, 0x025A, 0x0A3F, 0x025C, 0x0A3F, 0x025E, 0x0A3F,
  604. 0x0263, 0x007C, 0x0100, 0x0004, 0xBE5B, 0x3500, 0x800E, 0x200F,
  605. 0xBE1D, 0x0F00, 0x8001, 0x5011, 0x800A, 0xA2F4, 0x800B, 0x17A3,
  606. 0xBE4B, 0x17A3, 0xBE41, 0x5011, 0xBE17, 0x2100, 0x8000, 0x8304,
  607. 0xBE40, 0x8304, 0xBE4A, 0xA2F4, 0x800C, 0xA8D5, 0x8014, 0x5500,
  608. 0x8015, 0x0004, 0xBE4C, 0xA8D5, 0xBE59, 0x0008, 0xBE09, 0x0E00,
  609. 0xBE36, 0x1036, 0xBE37, 0x1036, 0x800D, 0x00FF, 0xBE4D, 0x00FF,
  610. };
  611. /* DGN3500, "netgear,dgn3500", "netgear,dgn3500b" */
  612. static const u16 rtl8366rb_init_jam_dgn3500[] = {
  613. 0x0000, 0x0830, 0x0400, 0x8130, 0x000A, 0x83ED, 0x0F51, 0x0017,
  614. 0x02F5, 0x0048, 0x02FA, 0xFFDF, 0x02FB, 0xFFE0, 0x0450, 0x0000,
  615. 0x0401, 0x0000, 0x0431, 0x0960,
  616. };
  617. /* This jam table activates "green ethernet", which means low power mode
  618. * and is claimed to detect the cable length and not use more power than
  619. * necessary, and the ports should enter power saving mode 10 seconds after
  620. * a cable is disconnected. Seems to always be the same.
  621. */
  622. static const u16 rtl8366rb_green_jam[][2] = {
  623. {0xBE78, 0x323C}, {0xBE77, 0x5000}, {0xBE2E, 0x7BA7},
  624. {0xBE59, 0x3459}, {0xBE5A, 0x745A}, {0xBE5B, 0x785C},
  625. {0xBE5C, 0x785C}, {0xBE6E, 0xE120}, {0xBE79, 0x323C},
  626. };
  627. static int rtl8366rb_setup(struct dsa_switch *ds)
  628. {
  629. struct realtek_smi *smi = ds->priv;
  630. const u16 *jam_table;
  631. u32 chip_ver = 0;
  632. u32 chip_id = 0;
  633. int jam_size;
  634. u32 val;
  635. int ret;
  636. int i;
  637. ret = regmap_read(smi->map, RTL8366RB_CHIP_ID_REG, &chip_id);
  638. if (ret) {
  639. dev_err(smi->dev, "unable to read chip id\n");
  640. return ret;
  641. }
  642. switch (chip_id) {
  643. case RTL8366RB_CHIP_ID_8366:
  644. break;
  645. default:
  646. dev_err(smi->dev, "unknown chip id (%04x)\n", chip_id);
  647. return -ENODEV;
  648. }
  649. ret = regmap_read(smi->map, RTL8366RB_CHIP_VERSION_CTRL_REG,
  650. &chip_ver);
  651. if (ret) {
  652. dev_err(smi->dev, "unable to read chip version\n");
  653. return ret;
  654. }
  655. dev_info(smi->dev, "RTL%04x ver %u chip found\n",
  656. chip_id, chip_ver & RTL8366RB_CHIP_VERSION_MASK);
  657. /* Do the init dance using the right jam table */
  658. switch (chip_ver) {
  659. case 0:
  660. jam_table = rtl8366rb_init_jam_ver_0;
  661. jam_size = ARRAY_SIZE(rtl8366rb_init_jam_ver_0);
  662. break;
  663. case 1:
  664. jam_table = rtl8366rb_init_jam_ver_1;
  665. jam_size = ARRAY_SIZE(rtl8366rb_init_jam_ver_1);
  666. break;
  667. case 2:
  668. jam_table = rtl8366rb_init_jam_ver_2;
  669. jam_size = ARRAY_SIZE(rtl8366rb_init_jam_ver_2);
  670. break;
  671. default:
  672. jam_table = rtl8366rb_init_jam_ver_3;
  673. jam_size = ARRAY_SIZE(rtl8366rb_init_jam_ver_3);
  674. break;
  675. }
  676. /* Special jam tables for special routers
  677. * TODO: are these necessary? Maintainers, please test
  678. * without them, using just the off-the-shelf tables.
  679. */
  680. if (of_machine_is_compatible("belkin,f5d8235-v1")) {
  681. jam_table = rtl8366rb_init_jam_f5d8235;
  682. jam_size = ARRAY_SIZE(rtl8366rb_init_jam_f5d8235);
  683. }
  684. if (of_machine_is_compatible("netgear,dgn3500") ||
  685. of_machine_is_compatible("netgear,dgn3500b")) {
  686. jam_table = rtl8366rb_init_jam_dgn3500;
  687. jam_size = ARRAY_SIZE(rtl8366rb_init_jam_dgn3500);
  688. }
  689. i = 0;
  690. while (i < jam_size) {
  691. if ((jam_table[i] & 0xBE00) == 0xBE00) {
  692. ret = regmap_read(smi->map,
  693. RTL8366RB_PHY_ACCESS_BUSY_REG,
  694. &val);
  695. if (ret)
  696. return ret;
  697. if (!(val & RTL8366RB_PHY_INT_BUSY)) {
  698. ret = regmap_write(smi->map,
  699. RTL8366RB_PHY_ACCESS_CTRL_REG,
  700. RTL8366RB_PHY_CTRL_WRITE);
  701. if (ret)
  702. return ret;
  703. }
  704. }
  705. dev_dbg(smi->dev, "jam %04x into register %04x\n",
  706. jam_table[i + 1],
  707. jam_table[i]);
  708. ret = regmap_write(smi->map,
  709. jam_table[i],
  710. jam_table[i + 1]);
  711. if (ret)
  712. return ret;
  713. i += 2;
  714. }
  715. /* Set up the "green ethernet" feature */
  716. i = 0;
  717. while (i < ARRAY_SIZE(rtl8366rb_green_jam)) {
  718. ret = regmap_read(smi->map, RTL8366RB_PHY_ACCESS_BUSY_REG,
  719. &val);
  720. if (ret)
  721. return ret;
  722. if (!(val & RTL8366RB_PHY_INT_BUSY)) {
  723. ret = regmap_write(smi->map,
  724. RTL8366RB_PHY_ACCESS_CTRL_REG,
  725. RTL8366RB_PHY_CTRL_WRITE);
  726. if (ret)
  727. return ret;
  728. ret = regmap_write(smi->map,
  729. rtl8366rb_green_jam[i][0],
  730. rtl8366rb_green_jam[i][1]);
  731. if (ret)
  732. return ret;
  733. i++;
  734. }
  735. }
  736. ret = regmap_write(smi->map,
  737. RTL8366RB_GREEN_FEATURE_REG,
  738. (chip_ver == 1) ? 0x0007 : 0x0003);
  739. if (ret)
  740. return ret;
  741. /* Vendor driver sets 0x240 in registers 0xc and 0xd (undocumented) */
  742. ret = regmap_write(smi->map, 0x0c, 0x240);
  743. if (ret)
  744. return ret;
  745. ret = regmap_write(smi->map, 0x0d, 0x240);
  746. if (ret)
  747. return ret;
  748. /* Set some random MAC address */
  749. ret = rtl8366rb_set_addr(smi);
  750. if (ret)
  751. return ret;
  752. /* Enable CPU port and enable inserting CPU tag
  753. *
  754. * Disabling RTL8368RB_CPU_INSTAG here will change the behaviour
  755. * of the switch totally and it will start talking Realtek RRCP
  756. * internally. It is probably possible to experiment with this,
  757. * but then the kernel needs to understand and handle RRCP first.
  758. */
  759. ret = regmap_update_bits(smi->map, RTL8368RB_CPU_CTRL_REG,
  760. 0xFFFF,
  761. RTL8368RB_CPU_INSTAG | BIT(smi->cpu_port));
  762. if (ret)
  763. return ret;
  764. /* Make sure we default-enable the fixed CPU port */
  765. ret = regmap_update_bits(smi->map, RTL8366RB_PECR,
  766. BIT(smi->cpu_port),
  767. 0);
  768. if (ret)
  769. return ret;
  770. /* Set maximum packet length to 1536 bytes */
  771. ret = regmap_update_bits(smi->map, RTL8366RB_SGCR,
  772. RTL8366RB_SGCR_MAX_LENGTH_MASK,
  773. RTL8366RB_SGCR_MAX_LENGTH_1536);
  774. if (ret)
  775. return ret;
  776. /* Enable learning for all ports */
  777. ret = regmap_write(smi->map, RTL8366RB_SSCR0, 0);
  778. if (ret)
  779. return ret;
  780. /* Enable auto ageing for all ports */
  781. ret = regmap_write(smi->map, RTL8366RB_SSCR1, 0);
  782. if (ret)
  783. return ret;
  784. /* Port 4 setup: this enables Port 4, usually the WAN port,
  785. * common PHY IO mode is apparently mode 0, and this is not what
  786. * the port is initialized to. There is no explanation of the
  787. * IO modes in the Realtek source code, if your WAN port is
  788. * connected to something exotic such as fiber, then this might
  789. * be worth experimenting with.
  790. */
  791. ret = regmap_update_bits(smi->map, RTL8366RB_PMC0,
  792. RTL8366RB_PMC0_P4_IOMODE_MASK,
  793. 0 << RTL8366RB_PMC0_P4_IOMODE_SHIFT);
  794. if (ret)
  795. return ret;
  796. /* Discard VLAN tagged packets if the port is not a member of
  797. * the VLAN with which the packets is associated.
  798. */
  799. ret = regmap_write(smi->map, RTL8366RB_VLAN_INGRESS_CTRL2_REG,
  800. RTL8366RB_PORT_ALL);
  801. if (ret)
  802. return ret;
  803. /* Don't drop packets whose DA has not been learned */
  804. ret = regmap_update_bits(smi->map, RTL8366RB_SSCR2,
  805. RTL8366RB_SSCR2_DROP_UNKNOWN_DA, 0);
  806. if (ret)
  807. return ret;
  808. /* Set blinking, TODO: make this configurable */
  809. ret = regmap_update_bits(smi->map, RTL8366RB_LED_BLINKRATE_REG,
  810. RTL8366RB_LED_BLINKRATE_MASK,
  811. RTL8366RB_LED_BLINKRATE_56MS);
  812. if (ret)
  813. return ret;
  814. /* Set up LED activity:
  815. * Each port has 4 LEDs, we configure all ports to the same
  816. * behaviour (no individual config) but we can set up each
  817. * LED separately.
  818. */
  819. if (smi->leds_disabled) {
  820. /* Turn everything off */
  821. regmap_update_bits(smi->map,
  822. RTL8366RB_LED_0_1_CTRL_REG,
  823. 0x0FFF, 0);
  824. regmap_update_bits(smi->map,
  825. RTL8366RB_LED_2_3_CTRL_REG,
  826. 0x0FFF, 0);
  827. regmap_update_bits(smi->map,
  828. RTL8366RB_INTERRUPT_CONTROL_REG,
  829. RTL8366RB_P4_RGMII_LED,
  830. 0);
  831. val = RTL8366RB_LED_OFF;
  832. } else {
  833. /* TODO: make this configurable per LED */
  834. val = RTL8366RB_LED_FORCE;
  835. }
  836. for (i = 0; i < 4; i++) {
  837. ret = regmap_update_bits(smi->map,
  838. RTL8366RB_LED_CTRL_REG,
  839. 0xf << (i * 4),
  840. val << (i * 4));
  841. if (ret)
  842. return ret;
  843. }
  844. ret = rtl8366_init_vlan(smi);
  845. if (ret)
  846. return ret;
  847. ret = rtl8366rb_setup_cascaded_irq(smi);
  848. if (ret)
  849. dev_info(smi->dev, "no interrupt support\n");
  850. ret = realtek_smi_setup_mdio(smi);
  851. if (ret) {
  852. dev_info(smi->dev, "could not set up MDIO bus\n");
  853. return -ENODEV;
  854. }
  855. return 0;
  856. }
  857. static enum dsa_tag_protocol rtl8366_get_tag_protocol(struct dsa_switch *ds,
  858. int port)
  859. {
  860. /* For now, the RTL switches are handled without any custom tags.
  861. *
  862. * It is possible to turn on "custom tags" by removing the
  863. * RTL8368RB_CPU_INSTAG flag when enabling the port but what it
  864. * does is unfamiliar to DSA: ethernet frames of type 8899, the Realtek
  865. * Remote Control Protocol (RRCP) start to appear on the CPU port of
  866. * the device. So this is not the ordinary few extra bytes in the
  867. * frame. Instead it appears that the switch starts to talk Realtek
  868. * RRCP internally which means a pretty complex RRCP implementation
  869. * decoding and responding the RRCP protocol is needed to exploit this.
  870. *
  871. * The OpenRRCP project (dormant since 2009) have reverse-egineered
  872. * parts of the protocol.
  873. */
  874. return DSA_TAG_PROTO_NONE;
  875. }
  876. static void rtl8366rb_adjust_link(struct dsa_switch *ds, int port,
  877. struct phy_device *phydev)
  878. {
  879. struct realtek_smi *smi = ds->priv;
  880. int ret;
  881. if (port != smi->cpu_port)
  882. return;
  883. dev_info(smi->dev, "adjust link on CPU port (%d)\n", port);
  884. /* Force the fixed CPU port into 1Gbit mode, no autonegotiation */
  885. ret = regmap_update_bits(smi->map, RTL8366RB_MAC_FORCE_CTRL_REG,
  886. BIT(port), BIT(port));
  887. if (ret)
  888. return;
  889. ret = regmap_update_bits(smi->map, RTL8366RB_PAACR2,
  890. 0xFF00U,
  891. RTL8366RB_PAACR_CPU_PORT << 8);
  892. if (ret)
  893. return;
  894. /* Enable the CPU port */
  895. ret = regmap_update_bits(smi->map, RTL8366RB_PECR, BIT(port),
  896. 0);
  897. if (ret)
  898. return;
  899. }
  900. static void rb8366rb_set_port_led(struct realtek_smi *smi,
  901. int port, bool enable)
  902. {
  903. u16 val = enable ? 0x3f : 0;
  904. int ret;
  905. if (smi->leds_disabled)
  906. return;
  907. switch (port) {
  908. case 0:
  909. ret = regmap_update_bits(smi->map,
  910. RTL8366RB_LED_0_1_CTRL_REG,
  911. 0x3F, val);
  912. break;
  913. case 1:
  914. ret = regmap_update_bits(smi->map,
  915. RTL8366RB_LED_0_1_CTRL_REG,
  916. 0x3F << RTL8366RB_LED_1_OFFSET,
  917. val << RTL8366RB_LED_1_OFFSET);
  918. break;
  919. case 2:
  920. ret = regmap_update_bits(smi->map,
  921. RTL8366RB_LED_2_3_CTRL_REG,
  922. 0x3F, val);
  923. break;
  924. case 3:
  925. ret = regmap_update_bits(smi->map,
  926. RTL8366RB_LED_2_3_CTRL_REG,
  927. 0x3F << RTL8366RB_LED_3_OFFSET,
  928. val << RTL8366RB_LED_3_OFFSET);
  929. break;
  930. case 4:
  931. ret = regmap_update_bits(smi->map,
  932. RTL8366RB_INTERRUPT_CONTROL_REG,
  933. RTL8366RB_P4_RGMII_LED,
  934. enable ? RTL8366RB_P4_RGMII_LED : 0);
  935. break;
  936. default:
  937. dev_err(smi->dev, "no LED for port %d\n", port);
  938. return;
  939. }
  940. if (ret)
  941. dev_err(smi->dev, "error updating LED on port %d\n", port);
  942. }
  943. static int
  944. rtl8366rb_port_enable(struct dsa_switch *ds, int port,
  945. struct phy_device *phy)
  946. {
  947. struct realtek_smi *smi = ds->priv;
  948. int ret;
  949. dev_dbg(smi->dev, "enable port %d\n", port);
  950. ret = regmap_update_bits(smi->map, RTL8366RB_PECR, BIT(port),
  951. 0);
  952. if (ret)
  953. return ret;
  954. rb8366rb_set_port_led(smi, port, true);
  955. return 0;
  956. }
  957. static void
  958. rtl8366rb_port_disable(struct dsa_switch *ds, int port)
  959. {
  960. struct realtek_smi *smi = ds->priv;
  961. int ret;
  962. dev_dbg(smi->dev, "disable port %d\n", port);
  963. ret = regmap_update_bits(smi->map, RTL8366RB_PECR, BIT(port),
  964. BIT(port));
  965. if (ret)
  966. return;
  967. rb8366rb_set_port_led(smi, port, false);
  968. }
  969. static int rtl8366rb_get_vlan_4k(struct realtek_smi *smi, u32 vid,
  970. struct rtl8366_vlan_4k *vlan4k)
  971. {
  972. u32 data[3];
  973. int ret;
  974. int i;
  975. memset(vlan4k, '\0', sizeof(struct rtl8366_vlan_4k));
  976. if (vid >= RTL8366RB_NUM_VIDS)
  977. return -EINVAL;
  978. /* write VID */
  979. ret = regmap_write(smi->map, RTL8366RB_VLAN_TABLE_WRITE_BASE,
  980. vid & RTL8366RB_VLAN_VID_MASK);
  981. if (ret)
  982. return ret;
  983. /* write table access control word */
  984. ret = regmap_write(smi->map, RTL8366RB_TABLE_ACCESS_CTRL_REG,
  985. RTL8366RB_TABLE_VLAN_READ_CTRL);
  986. if (ret)
  987. return ret;
  988. for (i = 0; i < 3; i++) {
  989. ret = regmap_read(smi->map,
  990. RTL8366RB_VLAN_TABLE_READ_BASE + i,
  991. &data[i]);
  992. if (ret)
  993. return ret;
  994. }
  995. vlan4k->vid = vid;
  996. vlan4k->untag = (data[1] >> RTL8366RB_VLAN_UNTAG_SHIFT) &
  997. RTL8366RB_VLAN_UNTAG_MASK;
  998. vlan4k->member = data[1] & RTL8366RB_VLAN_MEMBER_MASK;
  999. vlan4k->fid = data[2] & RTL8366RB_VLAN_FID_MASK;
  1000. return 0;
  1001. }
  1002. static int rtl8366rb_set_vlan_4k(struct realtek_smi *smi,
  1003. const struct rtl8366_vlan_4k *vlan4k)
  1004. {
  1005. u32 data[3];
  1006. int ret;
  1007. int i;
  1008. if (vlan4k->vid >= RTL8366RB_NUM_VIDS ||
  1009. vlan4k->member > RTL8366RB_VLAN_MEMBER_MASK ||
  1010. vlan4k->untag > RTL8366RB_VLAN_UNTAG_MASK ||
  1011. vlan4k->fid > RTL8366RB_FIDMAX)
  1012. return -EINVAL;
  1013. data[0] = vlan4k->vid & RTL8366RB_VLAN_VID_MASK;
  1014. data[1] = (vlan4k->member & RTL8366RB_VLAN_MEMBER_MASK) |
  1015. ((vlan4k->untag & RTL8366RB_VLAN_UNTAG_MASK) <<
  1016. RTL8366RB_VLAN_UNTAG_SHIFT);
  1017. data[2] = vlan4k->fid & RTL8366RB_VLAN_FID_MASK;
  1018. for (i = 0; i < 3; i++) {
  1019. ret = regmap_write(smi->map,
  1020. RTL8366RB_VLAN_TABLE_WRITE_BASE + i,
  1021. data[i]);
  1022. if (ret)
  1023. return ret;
  1024. }
  1025. /* write table access control word */
  1026. ret = regmap_write(smi->map, RTL8366RB_TABLE_ACCESS_CTRL_REG,
  1027. RTL8366RB_TABLE_VLAN_WRITE_CTRL);
  1028. return ret;
  1029. }
  1030. static int rtl8366rb_get_vlan_mc(struct realtek_smi *smi, u32 index,
  1031. struct rtl8366_vlan_mc *vlanmc)
  1032. {
  1033. u32 data[3];
  1034. int ret;
  1035. int i;
  1036. memset(vlanmc, '\0', sizeof(struct rtl8366_vlan_mc));
  1037. if (index >= RTL8366RB_NUM_VLANS)
  1038. return -EINVAL;
  1039. for (i = 0; i < 3; i++) {
  1040. ret = regmap_read(smi->map,
  1041. RTL8366RB_VLAN_MC_BASE(index) + i,
  1042. &data[i]);
  1043. if (ret)
  1044. return ret;
  1045. }
  1046. vlanmc->vid = data[0] & RTL8366RB_VLAN_VID_MASK;
  1047. vlanmc->priority = (data[0] >> RTL8366RB_VLAN_PRIORITY_SHIFT) &
  1048. RTL8366RB_VLAN_PRIORITY_MASK;
  1049. vlanmc->untag = (data[1] >> RTL8366RB_VLAN_UNTAG_SHIFT) &
  1050. RTL8366RB_VLAN_UNTAG_MASK;
  1051. vlanmc->member = data[1] & RTL8366RB_VLAN_MEMBER_MASK;
  1052. vlanmc->fid = data[2] & RTL8366RB_VLAN_FID_MASK;
  1053. return 0;
  1054. }
  1055. static int rtl8366rb_set_vlan_mc(struct realtek_smi *smi, u32 index,
  1056. const struct rtl8366_vlan_mc *vlanmc)
  1057. {
  1058. u32 data[3];
  1059. int ret;
  1060. int i;
  1061. if (index >= RTL8366RB_NUM_VLANS ||
  1062. vlanmc->vid >= RTL8366RB_NUM_VIDS ||
  1063. vlanmc->priority > RTL8366RB_PRIORITYMAX ||
  1064. vlanmc->member > RTL8366RB_VLAN_MEMBER_MASK ||
  1065. vlanmc->untag > RTL8366RB_VLAN_UNTAG_MASK ||
  1066. vlanmc->fid > RTL8366RB_FIDMAX)
  1067. return -EINVAL;
  1068. data[0] = (vlanmc->vid & RTL8366RB_VLAN_VID_MASK) |
  1069. ((vlanmc->priority & RTL8366RB_VLAN_PRIORITY_MASK) <<
  1070. RTL8366RB_VLAN_PRIORITY_SHIFT);
  1071. data[1] = (vlanmc->member & RTL8366RB_VLAN_MEMBER_MASK) |
  1072. ((vlanmc->untag & RTL8366RB_VLAN_UNTAG_MASK) <<
  1073. RTL8366RB_VLAN_UNTAG_SHIFT);
  1074. data[2] = vlanmc->fid & RTL8366RB_VLAN_FID_MASK;
  1075. for (i = 0; i < 3; i++) {
  1076. ret = regmap_write(smi->map,
  1077. RTL8366RB_VLAN_MC_BASE(index) + i,
  1078. data[i]);
  1079. if (ret)
  1080. return ret;
  1081. }
  1082. return 0;
  1083. }
  1084. static int rtl8366rb_get_mc_index(struct realtek_smi *smi, int port, int *val)
  1085. {
  1086. u32 data;
  1087. int ret;
  1088. if (port >= smi->num_ports)
  1089. return -EINVAL;
  1090. ret = regmap_read(smi->map, RTL8366RB_PORT_VLAN_CTRL_REG(port),
  1091. &data);
  1092. if (ret)
  1093. return ret;
  1094. *val = (data >> RTL8366RB_PORT_VLAN_CTRL_SHIFT(port)) &
  1095. RTL8366RB_PORT_VLAN_CTRL_MASK;
  1096. return 0;
  1097. }
  1098. static int rtl8366rb_set_mc_index(struct realtek_smi *smi, int port, int index)
  1099. {
  1100. if (port >= smi->num_ports || index >= RTL8366RB_NUM_VLANS)
  1101. return -EINVAL;
  1102. return regmap_update_bits(smi->map, RTL8366RB_PORT_VLAN_CTRL_REG(port),
  1103. RTL8366RB_PORT_VLAN_CTRL_MASK <<
  1104. RTL8366RB_PORT_VLAN_CTRL_SHIFT(port),
  1105. (index & RTL8366RB_PORT_VLAN_CTRL_MASK) <<
  1106. RTL8366RB_PORT_VLAN_CTRL_SHIFT(port));
  1107. }
  1108. static bool rtl8366rb_is_vlan_valid(struct realtek_smi *smi, unsigned int vlan)
  1109. {
  1110. unsigned int max = RTL8366RB_NUM_VLANS;
  1111. if (smi->vlan4k_enabled)
  1112. max = RTL8366RB_NUM_VIDS - 1;
  1113. if (vlan == 0 || vlan > max)
  1114. return false;
  1115. return true;
  1116. }
  1117. static int rtl8366rb_enable_vlan(struct realtek_smi *smi, bool enable)
  1118. {
  1119. dev_dbg(smi->dev, "%s VLAN\n", enable ? "enable" : "disable");
  1120. return regmap_update_bits(smi->map,
  1121. RTL8366RB_SGCR, RTL8366RB_SGCR_EN_VLAN,
  1122. enable ? RTL8366RB_SGCR_EN_VLAN : 0);
  1123. }
  1124. static int rtl8366rb_enable_vlan4k(struct realtek_smi *smi, bool enable)
  1125. {
  1126. dev_dbg(smi->dev, "%s VLAN 4k\n", enable ? "enable" : "disable");
  1127. return regmap_update_bits(smi->map, RTL8366RB_SGCR,
  1128. RTL8366RB_SGCR_EN_VLAN_4KTB,
  1129. enable ? RTL8366RB_SGCR_EN_VLAN_4KTB : 0);
  1130. }
  1131. static int rtl8366rb_phy_read(struct realtek_smi *smi, int phy, int regnum)
  1132. {
  1133. u32 val;
  1134. u32 reg;
  1135. int ret;
  1136. if (phy > RTL8366RB_PHY_NO_MAX)
  1137. return -EINVAL;
  1138. ret = regmap_write(smi->map, RTL8366RB_PHY_ACCESS_CTRL_REG,
  1139. RTL8366RB_PHY_CTRL_READ);
  1140. if (ret)
  1141. return ret;
  1142. reg = 0x8000 | (1 << (phy + RTL8366RB_PHY_NO_OFFSET)) | regnum;
  1143. ret = regmap_write(smi->map, reg, 0);
  1144. if (ret) {
  1145. dev_err(smi->dev,
  1146. "failed to write PHY%d reg %04x @ %04x, ret %d\n",
  1147. phy, regnum, reg, ret);
  1148. return ret;
  1149. }
  1150. ret = regmap_read(smi->map, RTL8366RB_PHY_ACCESS_DATA_REG, &val);
  1151. if (ret)
  1152. return ret;
  1153. dev_dbg(smi->dev, "read PHY%d register 0x%04x @ %08x, val <- %04x\n",
  1154. phy, regnum, reg, val);
  1155. return val;
  1156. }
  1157. static int rtl8366rb_phy_write(struct realtek_smi *smi, int phy, int regnum,
  1158. u16 val)
  1159. {
  1160. u32 reg;
  1161. int ret;
  1162. if (phy > RTL8366RB_PHY_NO_MAX)
  1163. return -EINVAL;
  1164. ret = regmap_write(smi->map, RTL8366RB_PHY_ACCESS_CTRL_REG,
  1165. RTL8366RB_PHY_CTRL_WRITE);
  1166. if (ret)
  1167. return ret;
  1168. reg = 0x8000 | (1 << (phy + RTL8366RB_PHY_NO_OFFSET)) | regnum;
  1169. dev_dbg(smi->dev, "write PHY%d register 0x%04x @ %04x, val -> %04x\n",
  1170. phy, regnum, reg, val);
  1171. ret = regmap_write(smi->map, reg, val);
  1172. if (ret)
  1173. return ret;
  1174. return 0;
  1175. }
  1176. static int rtl8366rb_reset_chip(struct realtek_smi *smi)
  1177. {
  1178. int timeout = 10;
  1179. u32 val;
  1180. int ret;
  1181. realtek_smi_write_reg_noack(smi, RTL8366RB_RESET_CTRL_REG,
  1182. RTL8366RB_CHIP_CTRL_RESET_HW);
  1183. do {
  1184. usleep_range(20000, 25000);
  1185. ret = regmap_read(smi->map, RTL8366RB_RESET_CTRL_REG, &val);
  1186. if (ret)
  1187. return ret;
  1188. if (!(val & RTL8366RB_CHIP_CTRL_RESET_HW))
  1189. break;
  1190. } while (--timeout);
  1191. if (!timeout) {
  1192. dev_err(smi->dev, "timeout waiting for the switch to reset\n");
  1193. return -EIO;
  1194. }
  1195. return 0;
  1196. }
  1197. static int rtl8366rb_detect(struct realtek_smi *smi)
  1198. {
  1199. struct device *dev = smi->dev;
  1200. int ret;
  1201. u32 val;
  1202. /* Detect device */
  1203. ret = regmap_read(smi->map, 0x5c, &val);
  1204. if (ret) {
  1205. dev_err(dev, "can't get chip ID (%d)\n", ret);
  1206. return ret;
  1207. }
  1208. switch (val) {
  1209. case 0x6027:
  1210. dev_info(dev, "found an RTL8366S switch\n");
  1211. dev_err(dev, "this switch is not yet supported, submit patches!\n");
  1212. return -ENODEV;
  1213. case 0x5937:
  1214. dev_info(dev, "found an RTL8366RB switch\n");
  1215. smi->cpu_port = RTL8366RB_PORT_NUM_CPU;
  1216. smi->num_ports = RTL8366RB_NUM_PORTS;
  1217. smi->num_vlan_mc = RTL8366RB_NUM_VLANS;
  1218. smi->mib_counters = rtl8366rb_mib_counters;
  1219. smi->num_mib_counters = ARRAY_SIZE(rtl8366rb_mib_counters);
  1220. break;
  1221. default:
  1222. dev_info(dev, "found an Unknown Realtek switch (id=0x%04x)\n",
  1223. val);
  1224. break;
  1225. }
  1226. ret = rtl8366rb_reset_chip(smi);
  1227. if (ret)
  1228. return ret;
  1229. return 0;
  1230. }
  1231. static const struct dsa_switch_ops rtl8366rb_switch_ops = {
  1232. .get_tag_protocol = rtl8366_get_tag_protocol,
  1233. .setup = rtl8366rb_setup,
  1234. .adjust_link = rtl8366rb_adjust_link,
  1235. .get_strings = rtl8366_get_strings,
  1236. .get_ethtool_stats = rtl8366_get_ethtool_stats,
  1237. .get_sset_count = rtl8366_get_sset_count,
  1238. .port_vlan_filtering = rtl8366_vlan_filtering,
  1239. .port_vlan_prepare = rtl8366_vlan_prepare,
  1240. .port_vlan_add = rtl8366_vlan_add,
  1241. .port_vlan_del = rtl8366_vlan_del,
  1242. .port_enable = rtl8366rb_port_enable,
  1243. .port_disable = rtl8366rb_port_disable,
  1244. };
  1245. static const struct realtek_smi_ops rtl8366rb_smi_ops = {
  1246. .detect = rtl8366rb_detect,
  1247. .get_vlan_mc = rtl8366rb_get_vlan_mc,
  1248. .set_vlan_mc = rtl8366rb_set_vlan_mc,
  1249. .get_vlan_4k = rtl8366rb_get_vlan_4k,
  1250. .set_vlan_4k = rtl8366rb_set_vlan_4k,
  1251. .get_mc_index = rtl8366rb_get_mc_index,
  1252. .set_mc_index = rtl8366rb_set_mc_index,
  1253. .get_mib_counter = rtl8366rb_get_mib_counter,
  1254. .is_vlan_valid = rtl8366rb_is_vlan_valid,
  1255. .enable_vlan = rtl8366rb_enable_vlan,
  1256. .enable_vlan4k = rtl8366rb_enable_vlan4k,
  1257. .phy_read = rtl8366rb_phy_read,
  1258. .phy_write = rtl8366rb_phy_write,
  1259. };
  1260. const struct realtek_smi_variant rtl8366rb_variant = {
  1261. .ds_ops = &rtl8366rb_switch_ops,
  1262. .ops = &rtl8366rb_smi_ops,
  1263. .clk_delay = 10,
  1264. .cmd_read = 0xa9,
  1265. .cmd_write = 0xa8,
  1266. };
  1267. EXPORT_SYMBOL_GPL(rtl8366rb_variant);