rtl8366rb.c 56 KB

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