rtl8367b.c 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623
  1. /*
  2. * Platform driver for Realtek RTL8367B family chips, i.e. RTL8367RB and RTL8367R-VB
  3. * extended with support for RTL8367C family chips, i.e. RTL8367RB-VB and RTL8367S
  4. * extended with support for RTL8367D family chips, i.e. RTL8367S-VB
  5. *
  6. * Copyright (C) 2012 Gabor Juhos <[email protected]>
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of the GNU General Public License version 2 as published
  10. * by the Free Software Foundation.
  11. */
  12. #include <linux/kernel.h>
  13. #include <linux/module.h>
  14. #include <linux/init.h>
  15. #include <linux/device.h>
  16. #include <linux/of.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/delay.h>
  19. #include <linux/skbuff.h>
  20. #include "rtl8367.h"
  21. #include "rtl8366_smi.h"
  22. #define RTL8367B_RESET_DELAY 1000 /* msecs*/
  23. #define RTL8367B_PHY_ADDR_MAX 8
  24. #define RTL8367B_PHY_REG_MAX 31
  25. #define RTL8367B_VID_MASK 0x3fff
  26. #define RTL8367B_FID_MASK 0xf
  27. #define RTL8367B_UNTAG_MASK 0xff
  28. #define RTL8367B_MEMBER_MASK 0xff
  29. #define RTL8367B_PORT_MISC_CFG_REG(_p) (0x000e + 0x20 * (_p))
  30. #define RTL8367B_PORT_MISC_CFG_EGRESS_MODE_SHIFT 4
  31. #define RTL8367B_PORT_MISC_CFG_EGRESS_MODE_MASK 0x3
  32. #define RTL8367B_PORT_MISC_CFG_EGRESS_MODE_ORIGINAL 0
  33. #define RTL8367B_PORT_MISC_CFG_EGRESS_MODE_KEEP 1
  34. #define RTL8367B_PORT_MISC_CFG_EGRESS_MODE_PRI 2
  35. #define RTL8367B_PORT_MISC_CFG_EGRESS_MODE_REAL 3
  36. #define RTL8367B_BYPASS_LINE_RATE_REG 0x03f7
  37. #define RTL8367B_TA_CTRL_REG 0x0500 /*GOOD*/
  38. #define RTL8367B_TA_CTRL_SPA_SHIFT 8
  39. #define RTL8367B_TA_CTRL_SPA_MASK 0x7
  40. #define RTL8367B_TA_CTRL_METHOD BIT(4)/*GOOD*/
  41. #define RTL8367B_TA_CTRL_CMD_SHIFT 3
  42. #define RTL8367B_TA_CTRL_CMD_READ 0
  43. #define RTL8367B_TA_CTRL_CMD_WRITE 1
  44. #define RTL8367B_TA_CTRL_TABLE_SHIFT 0 /*GOOD*/
  45. #define RTL8367B_TA_CTRL_TABLE_ACLRULE 1
  46. #define RTL8367B_TA_CTRL_TABLE_ACLACT 2
  47. #define RTL8367B_TA_CTRL_TABLE_CVLAN 3
  48. #define RTL8367B_TA_CTRL_TABLE_L2 4
  49. #define RTL8367B_TA_CTRL_CVLAN_READ \
  50. ((RTL8367B_TA_CTRL_CMD_READ << RTL8367B_TA_CTRL_CMD_SHIFT) | \
  51. RTL8367B_TA_CTRL_TABLE_CVLAN)
  52. #define RTL8367B_TA_CTRL_CVLAN_WRITE \
  53. ((RTL8367B_TA_CTRL_CMD_WRITE << RTL8367B_TA_CTRL_CMD_SHIFT) | \
  54. RTL8367B_TA_CTRL_TABLE_CVLAN)
  55. #define RTL8367B_TA_ADDR_REG 0x0501/*GOOD*/
  56. #define RTL8367B_TA_ADDR_MASK 0x3fff/*GOOD*/
  57. #define RTL8367B_TA_LUT_REG 0x0502/*GOOD*/
  58. #define RTL8367B_TA_WRDATA_REG(_x) (0x0510 + (_x))/*GOOD*/
  59. #define RTL8367B_TA_VLAN_NUM_WORDS 2
  60. #define RTL8367B_TA_VLAN_VID_MASK RTL8367B_VID_MASK
  61. #define RTL8367B_TA_VLAN0_MEMBER_SHIFT 0
  62. #define RTL8367B_TA_VLAN0_MEMBER_MASK RTL8367B_MEMBER_MASK
  63. #define RTL8367B_TA_VLAN0_UNTAG_SHIFT 8
  64. #define RTL8367B_TA_VLAN0_UNTAG_MASK RTL8367B_MEMBER_MASK
  65. #define RTL8367B_TA_VLAN1_FID_SHIFT 0
  66. #define RTL8367B_TA_VLAN1_FID_MASK RTL8367B_FID_MASK
  67. #define RTL8367B_TA_RDDATA_REG(_x) (0x0520 + (_x))/*GOOD*/
  68. #define RTL8367B_VLAN_PVID_CTRL_REG(_p) (0x0700 + (_p) / 2) /*GOOD*/
  69. #define RTL8367B_VLAN_PVID_CTRL_MASK 0x1f /*GOOD*/
  70. #define RTL8367B_VLAN_PVID_CTRL_SHIFT(_p) (8 * ((_p) % 2)) /*GOOD*/
  71. #define RTL8367B_VLAN_MC_BASE(_x) (0x0728 + (_x) * 4) /*GOOD*/
  72. #define RTL8367B_VLAN_MC_NUM_WORDS 4 /*GOOD*/
  73. #define RTL8367B_VLAN_MC0_MEMBER_SHIFT 0/*GOOD*/
  74. #define RTL8367B_VLAN_MC0_MEMBER_MASK RTL8367B_MEMBER_MASK/*GOOD*/
  75. #define RTL8367B_VLAN_MC1_FID_SHIFT 0/*GOOD*/
  76. #define RTL8367B_VLAN_MC1_FID_MASK RTL8367B_FID_MASK/*GOOD*/
  77. #define RTL8367B_VLAN_MC3_EVID_SHIFT 0/*GOOD*/
  78. #define RTL8367B_VLAN_MC3_EVID_MASK RTL8367B_VID_MASK/*GOOD*/
  79. #define RTL8367B_VLAN_CTRL_REG 0x07a8 /*GOOD*/
  80. #define RTL8367B_VLAN_CTRL_ENABLE BIT(0)
  81. #define RTL8367B_VLAN_INGRESS_REG 0x07a9 /*GOOD*/
  82. #define RTL8367B_PORT_ISOLATION_REG(_p) (0x08a2 + (_p)) /*GOOD*/
  83. #define RTL8367B_MIB_COUNTER_REG(_x) (0x1000 + (_x)) /*GOOD*/
  84. #define RTL8367B_MIB_COUNTER_PORT_OFFSET 0x007c /*GOOD*/
  85. #define RTL8367B_MIB_ADDRESS_REG 0x1004 /*GOOD*/
  86. #define RTL8367B_MIB_CTRL0_REG(_x) (0x1005 + (_x)) /*GOOD*/
  87. #define RTL8367B_MIB_CTRL0_GLOBAL_RESET_MASK BIT(11) /*GOOD*/
  88. #define RTL8367B_MIB_CTRL0_QM_RESET_MASK BIT(10) /*GOOD*/
  89. #define RTL8367B_MIB_CTRL0_PORT_RESET_MASK(_p) BIT(2 + (_p)) /*GOOD*/
  90. #define RTL8367B_MIB_CTRL0_RESET_MASK BIT(1) /*GOOD*/
  91. #define RTL8367B_MIB_CTRL0_BUSY_MASK BIT(0) /*GOOD*/
  92. #define RTL8367B_SWC0_REG 0x1200/*GOOD*/
  93. #define RTL8367B_SWC0_MAX_LENGTH_SHIFT 13/*GOOD*/
  94. #define RTL8367B_SWC0_MAX_LENGTH(_x) ((_x) << 13) /*GOOD*/
  95. #define RTL8367B_SWC0_MAX_LENGTH_MASK RTL8367B_SWC0_MAX_LENGTH(0x3)
  96. #define RTL8367B_SWC0_MAX_LENGTH_1522 RTL8367B_SWC0_MAX_LENGTH(0)
  97. #define RTL8367B_SWC0_MAX_LENGTH_1536 RTL8367B_SWC0_MAX_LENGTH(1)
  98. #define RTL8367B_SWC0_MAX_LENGTH_1552 RTL8367B_SWC0_MAX_LENGTH(2)
  99. #define RTL8367B_SWC0_MAX_LENGTH_16000 RTL8367B_SWC0_MAX_LENGTH(3)
  100. #define RTL8367B_CHIP_NUMBER_REG 0x1300/*GOOD*/
  101. #define RTL8367B_CHIP_VER_REG 0x1301/*GOOD*/
  102. #define RTL8367B_CHIP_VER_RLVID_SHIFT 12/*GOOD*/
  103. #define RTL8367B_CHIP_VER_RLVID_MASK 0xf/*GOOD*/
  104. #define RTL8367B_CHIP_VER_MCID_SHIFT 8/*GOOD*/
  105. #define RTL8367B_CHIP_VER_MCID_MASK 0xf/*GOOD*/
  106. #define RTL8367B_CHIP_VER_BOID_SHIFT 4/*GOOD*/
  107. #define RTL8367B_CHIP_VER_BOID_MASK 0xf/*GOOD*/
  108. #define RTL8367B_CHIP_VER_AFE_SHIFT 0/*GOOD*/
  109. #define RTL8367B_CHIP_VER_AFE_MASK 0x1/*GOOD*/
  110. #define RTL8367B_CHIP_MODE_REG 0x1302
  111. #define RTL8367B_CHIP_MODE_MASK 0x7
  112. #define RTL8367B_CHIP_DEBUG0_REG 0x1303
  113. #define RTL8367B_DEBUG0_SEL33(_x) BIT(8 + (_x))
  114. #define RTL8367B_DEBUG0_DRI_OTHER BIT(7)
  115. #define RTL8367B_DEBUG0_DRI_RG(_x) BIT(5 + (_x))
  116. #define RTL8367B_DEBUG0_DRI(_x) BIT(3 + (_x))
  117. #define RTL8367B_DEBUG0_SLR_OTHER BIT(2)
  118. #define RTL8367B_DEBUG0_SLR(_x) BIT(_x)
  119. #define RTL8367B_CHIP_DEBUG1_REG 0x1304
  120. #define RTL8367B_DEBUG1_DN_MASK(_x) \
  121. GENMASK(6 + (_x)*8, 4 + (_x)*8)
  122. #define RTL8367B_DEBUG1_DN_SHIFT(_x) (4 + (_x) * 8)
  123. #define RTL8367B_DEBUG1_DP_MASK(_x) \
  124. GENMASK(2 + (_x) * 8, (_x) * 8)
  125. #define RTL8367B_DEBUG1_DP_SHIFT(_x) ((_x) * 8)
  126. #define RTL8367B_CHIP_DEBUG2_REG 0x13e2
  127. #define RTL8367B_DEBUG2_RG2_DN_MASK GENMASK(8, 6)
  128. #define RTL8367B_DEBUG2_RG2_DN_SHIFT 6
  129. #define RTL8367B_DEBUG2_RG2_DP_MASK GENMASK(5, 3)
  130. #define RTL8367B_DEBUG2_RG2_DP_SHIFT 3
  131. #define RTL8367B_DEBUG2_DRI_EXT2_RG BIT(2)
  132. #define RTL8367B_DEBUG2_DRI_EXT2 BIT(1)
  133. #define RTL8367B_DEBUG2_SLR_EXT2 BIT(0)
  134. #define RTL8367B_DIS_REG 0x1305
  135. #define RTL8367B_DIS_SKIP_MII_RXER(_x) BIT(12 + (_x))
  136. #define RTL8367B_DIS_RGMII_SHIFT(_x) (4 * (_x))
  137. #define RTL8367B_DIS_RGMII_MASK 0x7
  138. #define RTL8367B_DIS2_REG 0x13c3
  139. #define RTL8367B_DIS2_SKIP_MII_RXER_SHIFT 4
  140. #define RTL8367B_DIS2_SKIP_MII_RXER 0x10
  141. #define RTL8367B_DIS2_RGMII_SHIFT 0
  142. #define RTL8367B_DIS2_RGMII_MASK 0xf
  143. #define RTL8367B_EXT_RGMXF_REG(_x) \
  144. ((_x) == 2 ? 0x13c5 : 0x1306 + (_x))
  145. #define RTL8367B_EXT_RGMXF_DUMMY0_SHIFT 5
  146. #define RTL8367B_EXT_RGMXF_DUMMY0_MASK 0x7ff
  147. #define RTL8367B_EXT_RGMXF_TXDELAY_SHIFT 3
  148. #define RTL8367B_EXT_RGMXF_TXDELAY_MASK 1
  149. #define RTL8367B_EXT_RGMXF_RXDELAY_MASK 0x7
  150. #define RTL8367B_DI_FORCE_REG(_x) \
  151. ((_x) == 2 ? 0x13c4 : 0x1310 + (_x))
  152. #define RTL8367B_DI_FORCE_MODE BIT(12)
  153. #define RTL8367B_DI_FORCE_NWAY BIT(7)
  154. #define RTL8367B_DI_FORCE_TXPAUSE BIT(6)
  155. #define RTL8367B_DI_FORCE_RXPAUSE BIT(5)
  156. #define RTL8367B_DI_FORCE_LINK BIT(4)
  157. #define RTL8367B_DI_FORCE_DUPLEX BIT(2)
  158. #define RTL8367B_DI_FORCE_SPEED_MASK 3
  159. #define RTL8367B_DI_FORCE_SPEED_10 0
  160. #define RTL8367B_DI_FORCE_SPEED_100 1
  161. #define RTL8367B_DI_FORCE_SPEED_1000 2
  162. #define RTL8367B_MAC_FORCE_REG(_x) (0x1312 + (_x))
  163. #define RTL8367B_CHIP_RESET_REG 0x1322 /*GOOD*/
  164. #define RTL8367B_CHIP_RESET_SW BIT(1) /*GOOD*/
  165. #define RTL8367B_CHIP_RESET_HW BIT(0) /*GOOD*/
  166. #define RTL8367B_PORT_STATUS_REG(_p) (0x1352 + (_p)) /*GOOD*/
  167. #define RTL8367B_PORT_STATUS_EN_1000_SPI BIT(11) /*GOOD*/
  168. #define RTL8367B_PORT_STATUS_EN_100_SPI BIT(10)/*GOOD*/
  169. #define RTL8367B_PORT_STATUS_NWAY_FAULT BIT(9)/*GOOD*/
  170. #define RTL8367B_PORT_STATUS_LINK_MASTER BIT(8)/*GOOD*/
  171. #define RTL8367B_PORT_STATUS_NWAY BIT(7)/*GOOD*/
  172. #define RTL8367B_PORT_STATUS_TXPAUSE BIT(6)/*GOOD*/
  173. #define RTL8367B_PORT_STATUS_RXPAUSE BIT(5)/*GOOD*/
  174. #define RTL8367B_PORT_STATUS_LINK BIT(4)/*GOOD*/
  175. #define RTL8367B_PORT_STATUS_DUPLEX BIT(2)/*GOOD*/
  176. #define RTL8367B_PORT_STATUS_SPEED_MASK 0x0003/*GOOD*/
  177. #define RTL8367B_PORT_STATUS_SPEED_10 0/*GOOD*/
  178. #define RTL8367B_PORT_STATUS_SPEED_100 1/*GOOD*/
  179. #define RTL8367B_PORT_STATUS_SPEED_1000 2/*GOOD*/
  180. #define RTL8367B_RTL_MAGIC_ID_REG 0x13c2
  181. #define RTL8367B_RTL_MAGIC_ID_VAL 0x0249
  182. #define RTL8367B_IA_CTRL_REG 0x1f00
  183. #define RTL8367B_IA_CTRL_RW(_x) ((_x) << 1)
  184. #define RTL8367B_IA_CTRL_RW_READ RTL8367B_IA_CTRL_RW(0)
  185. #define RTL8367B_IA_CTRL_RW_WRITE RTL8367B_IA_CTRL_RW(1)
  186. #define RTL8367B_IA_CTRL_CMD_MASK BIT(0)
  187. #define RTL8367B_IA_STATUS_REG 0x1f01
  188. #define RTL8367B_IA_STATUS_PHY_BUSY BIT(2)
  189. #define RTL8367B_IA_STATUS_SDS_BUSY BIT(1)
  190. #define RTL8367B_IA_STATUS_MDX_BUSY BIT(0)
  191. #define RTL8367B_IA_ADDRESS_REG 0x1f02
  192. #define RTL8367B_IA_WRITE_DATA_REG 0x1f03
  193. #define RTL8367B_IA_READ_DATA_REG 0x1f04
  194. #define RTL8367B_INTERNAL_PHY_REG(_a, _r) (0x2000 + 32 * (_a) + (_r))
  195. #define RTL8367B_NUM_MIB_COUNTERS 58
  196. #define RTL8367B_CPU_PORT_NUM 5
  197. #define RTL8367B_NUM_PORTS 8
  198. #define RTL8367B_NUM_VLANS 32
  199. #define RTL8367B_NUM_VIDS 4096
  200. #define RTL8367B_PRIORITYMAX 7
  201. #define RTL8367B_FIDMAX 7
  202. #define RTL8367B_PORT_0 BIT(0)
  203. #define RTL8367B_PORT_1 BIT(1)
  204. #define RTL8367B_PORT_2 BIT(2)
  205. #define RTL8367B_PORT_3 BIT(3)
  206. #define RTL8367B_PORT_4 BIT(4)
  207. #define RTL8367B_PORT_E0 BIT(5) /* External port 0 */
  208. #define RTL8367B_PORT_E1 BIT(6) /* External port 1 */
  209. #define RTL8367B_PORT_E2 BIT(7) /* External port 2 */
  210. #define RTL8367B_PORTS_ALL \
  211. (RTL8367B_PORT_0 | RTL8367B_PORT_1 | RTL8367B_PORT_2 | \
  212. RTL8367B_PORT_3 | RTL8367B_PORT_4 | RTL8367B_PORT_E0 | \
  213. RTL8367B_PORT_E1 | RTL8367B_PORT_E2)
  214. #define RTL8367B_PORTS_ALL_BUT_CPU \
  215. (RTL8367B_PORT_0 | RTL8367B_PORT_1 | RTL8367B_PORT_2 | \
  216. RTL8367B_PORT_3 | RTL8367B_PORT_4 | RTL8367B_PORT_E1 | \
  217. RTL8367B_PORT_E2)
  218. struct rtl8367b_initval {
  219. u16 reg;
  220. u16 val;
  221. };
  222. #define RTL8367B_MIB_RXB_ID 0 /* IfInOctets */
  223. #define RTL8367B_MIB_TXB_ID 28 /* IfOutOctets */
  224. #define RTL8367D_PORT_STATUS_REG(_p) (0x12d0 + (_p))
  225. #define RTL8367D_PORT_STATUS_SPEED1_MASK 0x3000
  226. #define RTL8367D_PORT_STATUS_SPEED1_SHIFT 10 /*12-2*/
  227. #define RTL8367D_REG_MAC0_FORCE_SELECT 0x12c0
  228. #define RTL8367D_REG_MAC0_FORCE_SELECT_EN 0x12c8
  229. #define RTL8367D_VLAN_PVID_CTRL_REG(_p) (0x0700 + (_p))
  230. #define RTL8367D_VLAN_PVID_CTRL_MASK 0xfff
  231. #define RTL8367D_VLAN_PVID_CTRL_SHIFT(_p) 0
  232. #define RTL8367D_FIDMAX 3
  233. #define RTL8367D_FID_MASK 3
  234. #define RTL8367D_TA_VLAN1_FID_SHIFT 0
  235. #define RTL8367D_TA_VLAN1_FID_MASK RTL8367D_FID_MASK
  236. #define RTL8367D_VID_MASK 0xfff
  237. #define RTL8367D_TA_VLAN_VID_MASK RTL8367D_VID_MASK
  238. #define RTL8367D_REG_EXT_TXC_DLY 0x13f9
  239. #define RTL8367D_EXT1_RGMII_TX_DLY_MASK 0x38
  240. #define RTL8367D_REG_TOP_CON0 0x1d70
  241. #define RTL8367D_MAC7_SEL_EXT1_MASK 0x2000
  242. #define RTL8367D_MAC4_SEL_EXT1_MASK 0x1000
  243. #define RTL8367D_REG_SDS1_MISC0 0x1d78
  244. #define RTL8367D_SDS1_MODE_MASK 0x1f
  245. #define RTL8367D_PORT_SDS_MODE_DISABLE 0x1f
  246. static struct rtl8366_mib_counter
  247. rtl8367b_mib_counters[RTL8367B_NUM_MIB_COUNTERS] = {
  248. {0, 0, 4, "ifInOctets" },
  249. {0, 4, 2, "dot3StatsFCSErrors" },
  250. {0, 6, 2, "dot3StatsSymbolErrors" },
  251. {0, 8, 2, "dot3InPauseFrames" },
  252. {0, 10, 2, "dot3ControlInUnknownOpcodes" },
  253. {0, 12, 2, "etherStatsFragments" },
  254. {0, 14, 2, "etherStatsJabbers" },
  255. {0, 16, 2, "ifInUcastPkts" },
  256. {0, 18, 2, "etherStatsDropEvents" },
  257. {0, 20, 2, "ifInMulticastPkts" },
  258. {0, 22, 2, "ifInBroadcastPkts" },
  259. {0, 24, 2, "inMldChecksumError" },
  260. {0, 26, 2, "inIgmpChecksumError" },
  261. {0, 28, 2, "inMldSpecificQuery" },
  262. {0, 30, 2, "inMldGeneralQuery" },
  263. {0, 32, 2, "inIgmpSpecificQuery" },
  264. {0, 34, 2, "inIgmpGeneralQuery" },
  265. {0, 36, 2, "inMldLeaves" },
  266. {0, 38, 2, "inIgmpLeaves" },
  267. {0, 40, 4, "etherStatsOctets" },
  268. {0, 44, 2, "etherStatsUnderSizePkts" },
  269. {0, 46, 2, "etherOversizeStats" },
  270. {0, 48, 2, "etherStatsPkts64Octets" },
  271. {0, 50, 2, "etherStatsPkts65to127Octets" },
  272. {0, 52, 2, "etherStatsPkts128to255Octets" },
  273. {0, 54, 2, "etherStatsPkts256to511Octets" },
  274. {0, 56, 2, "etherStatsPkts512to1023Octets" },
  275. {0, 58, 2, "etherStatsPkts1024to1518Octets" },
  276. {0, 60, 4, "ifOutOctets" },
  277. {0, 64, 2, "dot3StatsSingleCollisionFrames" },
  278. {0, 66, 2, "dot3StatMultipleCollisionFrames" },
  279. {0, 68, 2, "dot3sDeferredTransmissions" },
  280. {0, 70, 2, "dot3StatsLateCollisions" },
  281. {0, 72, 2, "etherStatsCollisions" },
  282. {0, 74, 2, "dot3StatsExcessiveCollisions" },
  283. {0, 76, 2, "dot3OutPauseFrames" },
  284. {0, 78, 2, "ifOutDiscards" },
  285. {0, 80, 2, "dot1dTpPortInDiscards" },
  286. {0, 82, 2, "ifOutUcastPkts" },
  287. {0, 84, 2, "ifOutMulticastPkts" },
  288. {0, 86, 2, "ifOutBroadcastPkts" },
  289. {0, 88, 2, "outOampduPkts" },
  290. {0, 90, 2, "inOampduPkts" },
  291. {0, 92, 2, "inIgmpJoinsSuccess" },
  292. {0, 94, 2, "inIgmpJoinsFail" },
  293. {0, 96, 2, "inMldJoinsSuccess" },
  294. {0, 98, 2, "inMldJoinsFail" },
  295. {0, 100, 2, "inReportSuppressionDrop" },
  296. {0, 102, 2, "inLeaveSuppressionDrop" },
  297. {0, 104, 2, "outIgmpReports" },
  298. {0, 106, 2, "outIgmpLeaves" },
  299. {0, 108, 2, "outIgmpGeneralQuery" },
  300. {0, 110, 2, "outIgmpSpecificQuery" },
  301. {0, 112, 2, "outMldReports" },
  302. {0, 114, 2, "outMldLeaves" },
  303. {0, 116, 2, "outMldGeneralQuery" },
  304. {0, 118, 2, "outMldSpecificQuery" },
  305. {0, 120, 2, "inKnownMulticastPkts" },
  306. };
  307. #define REG_RD(_smi, _reg, _val) \
  308. do { \
  309. err = rtl8366_smi_read_reg(_smi, _reg, _val); \
  310. if (err) \
  311. return err; \
  312. } while (0)
  313. #define REG_WR(_smi, _reg, _val) \
  314. do { \
  315. err = rtl8366_smi_write_reg(_smi, _reg, _val); \
  316. if (err) \
  317. return err; \
  318. } while (0)
  319. #define REG_RMW(_smi, _reg, _mask, _val) \
  320. do { \
  321. err = rtl8366_smi_rmwr(_smi, _reg, _mask, _val); \
  322. if (err) \
  323. return err; \
  324. } while (0)
  325. static const struct rtl8367b_initval rtl8367b_initvals[] = {
  326. {0x1B03, 0x0876}, {0x1200, 0x7FC4}, {0x1305, 0xC000}, {0x121E, 0x03CA},
  327. {0x1233, 0x0352}, {0x1234, 0x0064}, {0x1237, 0x0096}, {0x1238, 0x0078},
  328. {0x1239, 0x0084}, {0x123A, 0x0030}, {0x205F, 0x0002}, {0x2059, 0x1A00},
  329. {0x205F, 0x0000}, {0x207F, 0x0002}, {0x2077, 0x0000}, {0x2078, 0x0000},
  330. {0x2079, 0x0000}, {0x207A, 0x0000}, {0x207B, 0x0000}, {0x207F, 0x0000},
  331. {0x205F, 0x0002}, {0x2053, 0x0000}, {0x2054, 0x0000}, {0x2055, 0x0000},
  332. {0x2056, 0x0000}, {0x2057, 0x0000}, {0x205F, 0x0000}, {0x133F, 0x0030},
  333. {0x133E, 0x000E}, {0x221F, 0x0005}, {0x2205, 0x8B86}, {0x2206, 0x800E},
  334. {0x221F, 0x0000}, {0x133F, 0x0010}, {0x12A3, 0x2200}, {0x6107, 0xE58B},
  335. {0x6103, 0xA970}, {0x0018, 0x0F00}, {0x0038, 0x0F00}, {0x0058, 0x0F00},
  336. {0x0078, 0x0F00}, {0x0098, 0x0F00}, {0x133F, 0x0030}, {0x133E, 0x000E},
  337. {0x221F, 0x0005}, {0x2205, 0x8B6E}, {0x2206, 0x0000}, {0x220F, 0x0100},
  338. {0x2205, 0xFFF6}, {0x2206, 0x0080}, {0x2205, 0x8000}, {0x2206, 0x0280},
  339. {0x2206, 0x2BF7}, {0x2206, 0x00E0}, {0x2206, 0xFFF7}, {0x2206, 0xA080},
  340. {0x2206, 0x02AE}, {0x2206, 0xF602}, {0x2206, 0x0153}, {0x2206, 0x0201},
  341. {0x2206, 0x6602}, {0x2206, 0x8044}, {0x2206, 0x0201}, {0x2206, 0x7CE0},
  342. {0x2206, 0x8B8C}, {0x2206, 0xE18B}, {0x2206, 0x8D1E}, {0x2206, 0x01E1},
  343. {0x2206, 0x8B8E}, {0x2206, 0x1E01}, {0x2206, 0xA000}, {0x2206, 0xE4AE},
  344. {0x2206, 0xD8EE}, {0x2206, 0x85C0}, {0x2206, 0x00EE}, {0x2206, 0x85C1},
  345. {0x2206, 0x00EE}, {0x2206, 0x8AFC}, {0x2206, 0x07EE}, {0x2206, 0x8AFD},
  346. {0x2206, 0x73EE}, {0x2206, 0xFFF6}, {0x2206, 0x00EE}, {0x2206, 0xFFF7},
  347. {0x2206, 0xFC04}, {0x2206, 0xF8E0}, {0x2206, 0x8B8E}, {0x2206, 0xAD20},
  348. {0x2206, 0x0302}, {0x2206, 0x8050}, {0x2206, 0xFC04}, {0x2206, 0xF8F9},
  349. {0x2206, 0xE08B}, {0x2206, 0x85AD}, {0x2206, 0x2548}, {0x2206, 0xE08A},
  350. {0x2206, 0xE4E1}, {0x2206, 0x8AE5}, {0x2206, 0x7C00}, {0x2206, 0x009E},
  351. {0x2206, 0x35EE}, {0x2206, 0x8AE4}, {0x2206, 0x00EE}, {0x2206, 0x8AE5},
  352. {0x2206, 0x00E0}, {0x2206, 0x8AFC}, {0x2206, 0xE18A}, {0x2206, 0xFDE2},
  353. {0x2206, 0x85C0}, {0x2206, 0xE385}, {0x2206, 0xC102}, {0x2206, 0x2DAC},
  354. {0x2206, 0xAD20}, {0x2206, 0x12EE}, {0x2206, 0x8AE4}, {0x2206, 0x03EE},
  355. {0x2206, 0x8AE5}, {0x2206, 0xB7EE}, {0x2206, 0x85C0}, {0x2206, 0x00EE},
  356. {0x2206, 0x85C1}, {0x2206, 0x00AE}, {0x2206, 0x1115}, {0x2206, 0xE685},
  357. {0x2206, 0xC0E7}, {0x2206, 0x85C1}, {0x2206, 0xAE08}, {0x2206, 0xEE85},
  358. {0x2206, 0xC000}, {0x2206, 0xEE85}, {0x2206, 0xC100}, {0x2206, 0xFDFC},
  359. {0x2206, 0x0400}, {0x2205, 0xE142}, {0x2206, 0x0701}, {0x2205, 0xE140},
  360. {0x2206, 0x0405}, {0x220F, 0x0000}, {0x221F, 0x0000}, {0x133E, 0x000E},
  361. {0x133F, 0x0010}, {0x13EB, 0x11BB}, {0x207F, 0x0002}, {0x2073, 0x1D22},
  362. {0x207F, 0x0000}, {0x133F, 0x0030}, {0x133E, 0x000E}, {0x2200, 0x1340},
  363. {0x133E, 0x000E}, {0x133F, 0x0010},
  364. };
  365. static const struct rtl8367b_initval rtl8367c_initvals[] = {
  366. {0x13c2, 0x0000}, {0x0018, 0x0f00}, {0x0038, 0x0f00}, {0x0058, 0x0f00},
  367. {0x0078, 0x0f00}, {0x0098, 0x0f00}, {0x1d15, 0x0a69}, {0x2000, 0x1340},
  368. {0x2020, 0x1340}, {0x2040, 0x1340}, {0x2060, 0x1340}, {0x2080, 0x1340},
  369. {0x13eb, 0x15bb}, {0x1303, 0x06d6}, {0x1304, 0x0700}, {0x13E2, 0x003F},
  370. {0x13F9, 0x0090}, {0x121e, 0x03CA}, {0x1233, 0x0352}, {0x1237, 0x00a0},
  371. {0x123a, 0x0030}, {0x1239, 0x0084}, {0x0301, 0x1000}, {0x1349, 0x001F},
  372. {0x18e0, 0x4004}, {0x122b, 0x641c}, {0x1305, 0xc000}, {0x1200, 0x7fcb},
  373. {0x0884, 0x0003}, {0x06eb, 0x0001}, {0x00cf, 0xffff}, {0x00d0, 0x0007},
  374. {0x00ce, 0x48b0}, {0x00ce, 0x48b0}, {0x0398, 0xffff}, {0x0399, 0x0007},
  375. {0x0300, 0x0001}, {0x03fa, 0x0007}, {0x08c8, 0x00c0}, {0x0a30, 0x020e},
  376. {0x0800, 0x0000}, {0x0802, 0x0000}, {0x09da, 0x0017}, {0x1d32, 0x0002},
  377. };
  378. static int rtl8367b_write_initvals(struct rtl8366_smi *smi,
  379. const struct rtl8367b_initval *initvals,
  380. int count)
  381. {
  382. int err;
  383. int i;
  384. for (i = 0; i < count; i++)
  385. REG_WR(smi, initvals[i].reg, initvals[i].val);
  386. return 0;
  387. }
  388. static int rtl8367b_read_phy_reg(struct rtl8366_smi *smi,
  389. u32 phy_addr, u32 phy_reg, u32 *val)
  390. {
  391. int timeout;
  392. u32 data;
  393. int err;
  394. if (phy_addr > RTL8367B_PHY_ADDR_MAX)
  395. return -EINVAL;
  396. if (phy_reg > RTL8367B_PHY_REG_MAX)
  397. return -EINVAL;
  398. REG_RD(smi, RTL8367B_IA_STATUS_REG, &data);
  399. if (data & RTL8367B_IA_STATUS_PHY_BUSY)
  400. return -ETIMEDOUT;
  401. /* prepare address */
  402. REG_WR(smi, RTL8367B_IA_ADDRESS_REG,
  403. RTL8367B_INTERNAL_PHY_REG(phy_addr, phy_reg));
  404. /* send read command */
  405. REG_WR(smi, RTL8367B_IA_CTRL_REG,
  406. RTL8367B_IA_CTRL_CMD_MASK | RTL8367B_IA_CTRL_RW_READ);
  407. timeout = 5;
  408. do {
  409. REG_RD(smi, RTL8367B_IA_STATUS_REG, &data);
  410. if ((data & RTL8367B_IA_STATUS_PHY_BUSY) == 0)
  411. break;
  412. if (timeout--) {
  413. dev_err(smi->parent, "phy read timed out\n");
  414. return -ETIMEDOUT;
  415. }
  416. udelay(1);
  417. } while (1);
  418. /* read data */
  419. REG_RD(smi, RTL8367B_IA_READ_DATA_REG, val);
  420. dev_dbg(smi->parent, "phy_read: addr:%02x, reg:%02x, val:%04x\n",
  421. phy_addr, phy_reg, *val);
  422. return 0;
  423. }
  424. static int rtl8367b_write_phy_reg(struct rtl8366_smi *smi,
  425. u32 phy_addr, u32 phy_reg, u32 val)
  426. {
  427. int timeout;
  428. u32 data;
  429. int err;
  430. dev_dbg(smi->parent, "phy_write: addr:%02x, reg:%02x, val:%04x\n",
  431. phy_addr, phy_reg, val);
  432. if (phy_addr > RTL8367B_PHY_ADDR_MAX)
  433. return -EINVAL;
  434. if (phy_reg > RTL8367B_PHY_REG_MAX)
  435. return -EINVAL;
  436. REG_RD(smi, RTL8367B_IA_STATUS_REG, &data);
  437. if (data & RTL8367B_IA_STATUS_PHY_BUSY)
  438. return -ETIMEDOUT;
  439. /* preapre data */
  440. REG_WR(smi, RTL8367B_IA_WRITE_DATA_REG, val);
  441. /* prepare address */
  442. REG_WR(smi, RTL8367B_IA_ADDRESS_REG,
  443. RTL8367B_INTERNAL_PHY_REG(phy_addr, phy_reg));
  444. /* send write command */
  445. REG_WR(smi, RTL8367B_IA_CTRL_REG,
  446. RTL8367B_IA_CTRL_CMD_MASK | RTL8367B_IA_CTRL_RW_WRITE);
  447. timeout = 5;
  448. do {
  449. REG_RD(smi, RTL8367B_IA_STATUS_REG, &data);
  450. if ((data & RTL8367B_IA_STATUS_PHY_BUSY) == 0)
  451. break;
  452. if (timeout--) {
  453. dev_err(smi->parent, "phy write timed out\n");
  454. return -ETIMEDOUT;
  455. }
  456. udelay(1);
  457. } while (1);
  458. return 0;
  459. }
  460. static int rtl8367b_init_regs(struct rtl8366_smi *smi)
  461. {
  462. const struct rtl8367b_initval *initvals;
  463. int count;
  464. switch (smi->rtl8367b_chip) {
  465. case RTL8367B_CHIP_RTL8367RB:
  466. case RTL8367B_CHIP_RTL8367R_VB:
  467. initvals = rtl8367b_initvals;
  468. count = ARRAY_SIZE(rtl8367b_initvals);
  469. break;
  470. case RTL8367B_CHIP_RTL8367RB_VB:
  471. case RTL8367B_CHIP_RTL8367S:
  472. case RTL8367B_CHIP_RTL8367S_VB:
  473. initvals = rtl8367c_initvals;
  474. count = ARRAY_SIZE(rtl8367c_initvals);
  475. if ((smi->rtl8367b_chip == RTL8367B_CHIP_RTL8367S_VB) && (smi->emu_vlanmc == NULL)) {
  476. smi->emu_vlanmc = kzalloc(sizeof(struct rtl8366_vlan_mc) * smi->num_vlan_mc, GFP_KERNEL);
  477. dev_info(smi->parent, "alloc vlan mc emulator");
  478. }
  479. break;
  480. default:
  481. return -ENODEV;
  482. }
  483. return rtl8367b_write_initvals(smi, initvals, count);
  484. }
  485. static int rtl8367b_reset_chip(struct rtl8366_smi *smi)
  486. {
  487. int timeout = 10;
  488. int err;
  489. u32 data;
  490. REG_WR(smi, RTL8367B_CHIP_RESET_REG, RTL8367B_CHIP_RESET_HW);
  491. msleep(RTL8367B_RESET_DELAY);
  492. do {
  493. REG_RD(smi, RTL8367B_CHIP_RESET_REG, &data);
  494. if (!(data & RTL8367B_CHIP_RESET_HW))
  495. break;
  496. msleep(1);
  497. } while (--timeout);
  498. if (!timeout) {
  499. dev_err(smi->parent, "chip reset timed out\n");
  500. return -ETIMEDOUT;
  501. }
  502. return 0;
  503. }
  504. static int rtl8367b_extif_set_mode(struct rtl8366_smi *smi, int id,
  505. enum rtl8367_extif_mode mode)
  506. {
  507. int err;
  508. u32 data;
  509. /* set port mode */
  510. switch (mode) {
  511. case RTL8367_EXTIF_MODE_RGMII:
  512. REG_RMW(smi, RTL8367B_CHIP_DEBUG0_REG,
  513. RTL8367B_DEBUG0_SEL33(id),
  514. RTL8367B_DEBUG0_SEL33(id));
  515. if (id <= 1) {
  516. REG_RMW(smi, RTL8367B_CHIP_DEBUG0_REG,
  517. RTL8367B_DEBUG0_DRI(id) |
  518. RTL8367B_DEBUG0_DRI_RG(id) |
  519. RTL8367B_DEBUG0_SLR(id),
  520. RTL8367B_DEBUG0_DRI_RG(id) |
  521. RTL8367B_DEBUG0_SLR(id));
  522. REG_RMW(smi, RTL8367B_CHIP_DEBUG1_REG,
  523. RTL8367B_DEBUG1_DN_MASK(id) |
  524. RTL8367B_DEBUG1_DP_MASK(id),
  525. (7 << RTL8367B_DEBUG1_DN_SHIFT(id)) |
  526. (7 << RTL8367B_DEBUG1_DP_SHIFT(id)));
  527. if ((smi->rtl8367b_chip == RTL8367B_CHIP_RTL8367S_VB) && (id == 1)) {
  528. REG_RMW(smi, RTL8367D_REG_EXT_TXC_DLY, RTL8367D_EXT1_RGMII_TX_DLY_MASK, 0);
  529. /* Configure RGMII/MII mux to port 7 if UTP_PORT4 is not RGMII mode */
  530. REG_RD(smi, RTL8367D_REG_TOP_CON0, &data);
  531. data &= RTL8367D_MAC4_SEL_EXT1_MASK;
  532. if (data == 0)
  533. REG_RMW(smi, RTL8367D_REG_TOP_CON0, RTL8367D_MAC7_SEL_EXT1_MASK, RTL8367D_MAC7_SEL_EXT1_MASK);
  534. REG_RMW(smi, RTL8367D_REG_SDS1_MISC0, RTL8367D_SDS1_MODE_MASK, RTL8367D_PORT_SDS_MODE_DISABLE);
  535. }
  536. } else {
  537. REG_RMW(smi, RTL8367B_CHIP_DEBUG2_REG,
  538. RTL8367B_DEBUG2_DRI_EXT2 |
  539. RTL8367B_DEBUG2_DRI_EXT2_RG |
  540. RTL8367B_DEBUG2_SLR_EXT2 |
  541. RTL8367B_DEBUG2_RG2_DN_MASK |
  542. RTL8367B_DEBUG2_RG2_DP_MASK,
  543. RTL8367B_DEBUG2_DRI_EXT2_RG |
  544. RTL8367B_DEBUG2_SLR_EXT2 |
  545. (7 << RTL8367B_DEBUG2_RG2_DN_SHIFT) |
  546. (7 << RTL8367B_DEBUG2_RG2_DP_SHIFT));
  547. }
  548. break;
  549. case RTL8367_EXTIF_MODE_TMII_MAC:
  550. case RTL8367_EXTIF_MODE_TMII_PHY:
  551. REG_RMW(smi, RTL8367B_BYPASS_LINE_RATE_REG, BIT(id), BIT(id));
  552. break;
  553. case RTL8367_EXTIF_MODE_GMII:
  554. REG_RMW(smi, RTL8367B_CHIP_DEBUG0_REG,
  555. RTL8367B_DEBUG0_SEL33(id),
  556. RTL8367B_DEBUG0_SEL33(id));
  557. REG_RMW(smi, RTL8367B_EXT_RGMXF_REG(id), BIT(6), BIT(6));
  558. break;
  559. case RTL8367_EXTIF_MODE_MII_MAC:
  560. case RTL8367_EXTIF_MODE_MII_PHY:
  561. case RTL8367_EXTIF_MODE_DISABLED:
  562. REG_RMW(smi, RTL8367B_BYPASS_LINE_RATE_REG, BIT(id), 0);
  563. REG_RMW(smi, RTL8367B_EXT_RGMXF_REG(id), BIT(6), 0);
  564. break;
  565. default:
  566. dev_err(smi->parent,
  567. "invalid mode for external interface %d\n", id);
  568. return -EINVAL;
  569. }
  570. if (id <= 1)
  571. REG_RMW(smi, RTL8367B_DIS_REG,
  572. RTL8367B_DIS_RGMII_MASK << RTL8367B_DIS_RGMII_SHIFT(id),
  573. mode << RTL8367B_DIS_RGMII_SHIFT(id));
  574. else
  575. REG_RMW(smi, RTL8367B_DIS2_REG,
  576. RTL8367B_DIS2_RGMII_MASK << RTL8367B_DIS2_RGMII_SHIFT,
  577. mode << RTL8367B_DIS2_RGMII_SHIFT);
  578. return 0;
  579. }
  580. static int rtl8367b_extif_set_force(struct rtl8366_smi *smi, int id,
  581. struct rtl8367_port_ability *pa)
  582. {
  583. u32 mask;
  584. u32 val;
  585. int err;
  586. val = pa->speed & RTL8367B_DI_FORCE_SPEED_MASK;
  587. val |= pa->nway ? RTL8367B_DI_FORCE_NWAY : 0;
  588. val |= pa->txpause ? RTL8367B_DI_FORCE_TXPAUSE : 0;
  589. val |= pa->rxpause ? RTL8367B_DI_FORCE_RXPAUSE : 0;
  590. val |= pa->link ? RTL8367B_DI_FORCE_LINK : 0;
  591. val |= pa->duplex ? RTL8367B_DI_FORCE_DUPLEX : 0;
  592. if (smi->rtl8367b_chip >= RTL8367B_CHIP_RTL8367S_VB) { /* Family D */
  593. val |= (pa->speed << RTL8367D_PORT_STATUS_SPEED1_SHIFT) & RTL8367D_PORT_STATUS_SPEED1_MASK;
  594. if (smi->cpu_port != UINT_MAX) {
  595. REG_WR(smi, RTL8367D_REG_MAC0_FORCE_SELECT + smi->cpu_port, val);
  596. REG_WR(smi, RTL8367D_REG_MAC0_FORCE_SELECT_EN + smi->cpu_port, pa->force_mode ? 0xffff : 0x0000);
  597. }
  598. } else {
  599. val |= pa->force_mode ? RTL8367B_DI_FORCE_MODE : 0;
  600. mask = (RTL8367B_DI_FORCE_MODE |
  601. RTL8367B_DI_FORCE_NWAY |
  602. RTL8367B_DI_FORCE_TXPAUSE |
  603. RTL8367B_DI_FORCE_RXPAUSE |
  604. RTL8367B_DI_FORCE_LINK |
  605. RTL8367B_DI_FORCE_DUPLEX |
  606. RTL8367B_DI_FORCE_SPEED_MASK);
  607. REG_RMW(smi, RTL8367B_DI_FORCE_REG(id), mask, val);
  608. }
  609. return 0;
  610. }
  611. static int rtl8367b_extif_set_rgmii_delay(struct rtl8366_smi *smi, int id,
  612. unsigned txdelay, unsigned rxdelay)
  613. {
  614. u32 mask;
  615. u32 val;
  616. int err;
  617. mask = (RTL8367B_EXT_RGMXF_RXDELAY_MASK |
  618. (RTL8367B_EXT_RGMXF_TXDELAY_MASK <<
  619. RTL8367B_EXT_RGMXF_TXDELAY_SHIFT));
  620. val = rxdelay;
  621. val |= txdelay << RTL8367B_EXT_RGMXF_TXDELAY_SHIFT;
  622. REG_RMW(smi, RTL8367B_EXT_RGMXF_REG(id), mask, val);
  623. return 0;
  624. }
  625. static int rtl8367b_extif_init(struct rtl8366_smi *smi, int id,
  626. struct rtl8367_extif_config *cfg)
  627. {
  628. enum rtl8367_extif_mode mode;
  629. int err;
  630. mode = (cfg) ? cfg->mode : RTL8367_EXTIF_MODE_DISABLED;
  631. err = rtl8367b_extif_set_mode(smi, id, mode);
  632. if (err)
  633. return err;
  634. if (mode != RTL8367_EXTIF_MODE_DISABLED) {
  635. err = rtl8367b_extif_set_force(smi, id, &cfg->ability);
  636. if (err)
  637. return err;
  638. err = rtl8367b_extif_set_rgmii_delay(smi, id, cfg->txdelay,
  639. cfg->rxdelay);
  640. if (err)
  641. return err;
  642. }
  643. return 0;
  644. }
  645. static int rtl8367b_extif_init_of(struct rtl8366_smi *smi,
  646. const char *name)
  647. {
  648. struct rtl8367_extif_config *cfg;
  649. const __be32 *prop;
  650. int size;
  651. int err;
  652. unsigned cpu_port;
  653. unsigned id = UINT_MAX;
  654. prop = of_get_property(smi->parent->of_node, name, &size);
  655. if (!prop || (size != (10 * sizeof(*prop)))) {
  656. dev_err(smi->parent, "%s property is not defined or invalid\n", name);
  657. err = -EINVAL;
  658. goto err_init;
  659. }
  660. cpu_port = be32_to_cpup(prop++);
  661. switch (cpu_port) {
  662. case RTL8367B_CPU_PORT_NUM:
  663. case RTL8367B_CPU_PORT_NUM + 1:
  664. case RTL8367B_CPU_PORT_NUM + 2:
  665. if (smi->rtl8367b_chip == RTL8367B_CHIP_RTL8367R_VB) { /* for the RTL8367R-VB chip, cpu_port 5 corresponds to extif1 */
  666. if (cpu_port == RTL8367B_CPU_PORT_NUM)
  667. id = 1;
  668. else {
  669. dev_err(smi->parent, "wrong cpu_port %u in %s property\n", cpu_port, name);
  670. err = -EINVAL;
  671. goto err_init;
  672. }
  673. } else if (smi->rtl8367b_chip == RTL8367B_CHIP_RTL8367S_VB) { /* for the RTL8367S-VB chip, cpu_port 7 corresponds to extif1, cpu_port 6 corresponds to extif0 */
  674. if (cpu_port != RTL8367B_CPU_PORT_NUM) {
  675. id = cpu_port - RTL8367B_CPU_PORT_NUM - 1;
  676. } else {
  677. dev_err(smi->parent, "wrong cpu_port %u in %s property\n", cpu_port, name);
  678. err = -EINVAL;
  679. goto err_init;
  680. }
  681. } else {
  682. id = cpu_port - RTL8367B_CPU_PORT_NUM;
  683. }
  684. if (smi->cpu_port == UINT_MAX) {
  685. dev_info(smi->parent, "cpu_port:%u, assigned to extif%u\n", cpu_port, id);
  686. smi->cpu_port = cpu_port;
  687. }
  688. break;
  689. default:
  690. dev_err(smi->parent, "wrong cpu_port %u in %s property\n", cpu_port, name);
  691. err = -EINVAL;
  692. goto err_init;
  693. }
  694. cfg = kzalloc(sizeof(struct rtl8367_extif_config), GFP_KERNEL);
  695. if (!cfg)
  696. return -ENOMEM;
  697. cfg->txdelay = be32_to_cpup(prop++);
  698. cfg->rxdelay = be32_to_cpup(prop++);
  699. cfg->mode = be32_to_cpup(prop++);
  700. cfg->ability.force_mode = be32_to_cpup(prop++);
  701. cfg->ability.txpause = be32_to_cpup(prop++);
  702. cfg->ability.rxpause = be32_to_cpup(prop++);
  703. cfg->ability.link = be32_to_cpup(prop++);
  704. cfg->ability.duplex = be32_to_cpup(prop++);
  705. cfg->ability.speed = be32_to_cpup(prop++);
  706. err = rtl8367b_extif_init(smi, id, cfg);
  707. kfree(cfg);
  708. err_init:
  709. if (id != 0) rtl8367b_extif_init(smi, 0, NULL);
  710. if (id != 1) rtl8367b_extif_init(smi, 1, NULL);
  711. if (id != 2) rtl8367b_extif_init(smi, 2, NULL);
  712. return err;
  713. }
  714. static int rtl8367b_setup(struct rtl8366_smi *smi)
  715. {
  716. int err;
  717. int i;
  718. err = rtl8367b_init_regs(smi);
  719. if (err)
  720. return err;
  721. /* initialize external interfaces */
  722. err = rtl8367b_extif_init_of(smi, "realtek,extif");
  723. if (err)
  724. return err;
  725. /* set maximum packet length to 1536 bytes */
  726. REG_RMW(smi, RTL8367B_SWC0_REG, RTL8367B_SWC0_MAX_LENGTH_MASK,
  727. RTL8367B_SWC0_MAX_LENGTH_1536);
  728. /*
  729. * discard VLAN tagged packets if the port is not a member of
  730. * the VLAN with which the packets is associated.
  731. */
  732. REG_WR(smi, RTL8367B_VLAN_INGRESS_REG, RTL8367B_PORTS_ALL);
  733. /*
  734. * Setup egress tag mode for each port.
  735. */
  736. for (i = 0; i < RTL8367B_NUM_PORTS; i++)
  737. REG_RMW(smi,
  738. RTL8367B_PORT_MISC_CFG_REG(i),
  739. RTL8367B_PORT_MISC_CFG_EGRESS_MODE_MASK <<
  740. RTL8367B_PORT_MISC_CFG_EGRESS_MODE_SHIFT,
  741. RTL8367B_PORT_MISC_CFG_EGRESS_MODE_ORIGINAL <<
  742. RTL8367B_PORT_MISC_CFG_EGRESS_MODE_SHIFT);
  743. return 0;
  744. }
  745. static int rtl8367b_get_mib_counter(struct rtl8366_smi *smi, int counter,
  746. int port, unsigned long long *val)
  747. {
  748. struct rtl8366_mib_counter *mib;
  749. int offset;
  750. int i;
  751. int err;
  752. u32 addr, data;
  753. u64 mibvalue;
  754. if (port > RTL8367B_NUM_PORTS ||
  755. counter >= RTL8367B_NUM_MIB_COUNTERS)
  756. return -EINVAL;
  757. mib = &rtl8367b_mib_counters[counter];
  758. addr = RTL8367B_MIB_COUNTER_PORT_OFFSET * port + mib->offset;
  759. /*
  760. * Writing access counter address first
  761. * then ASIC will prepare 64bits counter wait for being retrived
  762. */
  763. REG_WR(smi, RTL8367B_MIB_ADDRESS_REG, addr >> 2);
  764. /* read MIB control register */
  765. REG_RD(smi, RTL8367B_MIB_CTRL0_REG(0), &data);
  766. if (data & RTL8367B_MIB_CTRL0_BUSY_MASK)
  767. return -EBUSY;
  768. if (data & RTL8367B_MIB_CTRL0_RESET_MASK)
  769. return -EIO;
  770. if (mib->length == 4)
  771. offset = 3;
  772. else
  773. offset = (mib->offset + 1) % 4;
  774. mibvalue = 0;
  775. for (i = 0; i < mib->length; i++) {
  776. REG_RD(smi, RTL8367B_MIB_COUNTER_REG(offset - i), &data);
  777. mibvalue = (mibvalue << 16) | (data & 0xFFFF);
  778. }
  779. *val = mibvalue;
  780. return 0;
  781. }
  782. static int rtl8367b_get_vlan_4k(struct rtl8366_smi *smi, u32 vid,
  783. struct rtl8366_vlan_4k *vlan4k)
  784. {
  785. u32 data[RTL8367B_TA_VLAN_NUM_WORDS];
  786. int err;
  787. int i;
  788. memset(vlan4k, '\0', sizeof(struct rtl8366_vlan_4k));
  789. if (vid >= RTL8367B_NUM_VIDS)
  790. return -EINVAL;
  791. /* write VID */
  792. REG_WR(smi, RTL8367B_TA_ADDR_REG, vid);
  793. /* write table access control word */
  794. REG_WR(smi, RTL8367B_TA_CTRL_REG, RTL8367B_TA_CTRL_CVLAN_READ);
  795. for (i = 0; i < ARRAY_SIZE(data); i++)
  796. REG_RD(smi, RTL8367B_TA_RDDATA_REG(i), &data[i]);
  797. vlan4k->vid = vid;
  798. vlan4k->member = (data[0] >> RTL8367B_TA_VLAN0_MEMBER_SHIFT) &
  799. RTL8367B_TA_VLAN0_MEMBER_MASK;
  800. vlan4k->untag = (data[0] >> RTL8367B_TA_VLAN0_UNTAG_SHIFT) &
  801. RTL8367B_TA_VLAN0_UNTAG_MASK;
  802. if (smi->rtl8367b_chip >= RTL8367B_CHIP_RTL8367S_VB) /* Family D */
  803. vlan4k->fid = (data[1] >> RTL8367D_TA_VLAN1_FID_SHIFT) &
  804. RTL8367D_TA_VLAN1_FID_MASK;
  805. else
  806. vlan4k->fid = (data[1] >> RTL8367B_TA_VLAN1_FID_SHIFT) &
  807. RTL8367B_TA_VLAN1_FID_MASK;
  808. return 0;
  809. }
  810. static int rtl8367b_set_vlan_4k(struct rtl8366_smi *smi,
  811. const struct rtl8366_vlan_4k *vlan4k)
  812. {
  813. u32 data[RTL8367B_TA_VLAN_NUM_WORDS];
  814. int err;
  815. int i;
  816. if (vlan4k->vid >= RTL8367B_NUM_VIDS ||
  817. vlan4k->member > RTL8367B_TA_VLAN0_MEMBER_MASK ||
  818. vlan4k->untag > RTL8367B_UNTAG_MASK ||
  819. vlan4k->fid > ((smi->rtl8367b_chip >= RTL8367B_CHIP_RTL8367S_VB) ? RTL8367D_FIDMAX : RTL8367B_FIDMAX))
  820. return -EINVAL;
  821. memset(data, 0, sizeof(data));
  822. data[0] = (vlan4k->member & RTL8367B_TA_VLAN0_MEMBER_MASK) <<
  823. RTL8367B_TA_VLAN0_MEMBER_SHIFT;
  824. data[0] |= (vlan4k->untag & RTL8367B_TA_VLAN0_UNTAG_MASK) <<
  825. RTL8367B_TA_VLAN0_UNTAG_SHIFT;
  826. if (smi->rtl8367b_chip >= RTL8367B_CHIP_RTL8367S_VB) /* Family D */
  827. data[1] = ((vlan4k->fid & RTL8367D_TA_VLAN1_FID_MASK) <<
  828. RTL8367D_TA_VLAN1_FID_SHIFT) | 12; /* ivl_svl - BIT(3), svlan_chek_ivl_svl - BIT(2) */
  829. else
  830. data[1] = (vlan4k->fid & RTL8367B_TA_VLAN1_FID_MASK) <<
  831. RTL8367B_TA_VLAN1_FID_SHIFT;
  832. for (i = 0; i < ARRAY_SIZE(data); i++)
  833. REG_WR(smi, RTL8367B_TA_WRDATA_REG(i), data[i]);
  834. /* write VID */
  835. if (smi->rtl8367b_chip >= RTL8367B_CHIP_RTL8367S_VB) /* Family D */
  836. REG_WR(smi, RTL8367B_TA_ADDR_REG,
  837. vlan4k->vid & RTL8367D_TA_VLAN_VID_MASK);
  838. else
  839. REG_WR(smi, RTL8367B_TA_ADDR_REG,
  840. vlan4k->vid & RTL8367B_TA_VLAN_VID_MASK);
  841. /* write table access control word */
  842. REG_WR(smi, RTL8367B_TA_CTRL_REG, RTL8367B_TA_CTRL_CVLAN_WRITE);
  843. return 0;
  844. }
  845. static int rtl8367b_get_vlan_mc(struct rtl8366_smi *smi, u32 index,
  846. struct rtl8366_vlan_mc *vlanmc)
  847. {
  848. u32 data[RTL8367B_VLAN_MC_NUM_WORDS];
  849. int err;
  850. int i;
  851. memset(vlanmc, '\0', sizeof(struct rtl8366_vlan_mc));
  852. if (index >= RTL8367B_NUM_VLANS)
  853. return -EINVAL;
  854. if (smi->emu_vlanmc) { /* use vlan mc emulation */
  855. vlanmc->vid = smi->emu_vlanmc[index].vid;
  856. vlanmc->member = smi->emu_vlanmc[index].member;
  857. vlanmc->fid = smi->emu_vlanmc[index].fid;
  858. vlanmc->untag = smi->emu_vlanmc[index].untag;
  859. return 0;
  860. }
  861. for (i = 0; i < ARRAY_SIZE(data); i++)
  862. REG_RD(smi, RTL8367B_VLAN_MC_BASE(index) + i, &data[i]);
  863. vlanmc->member = (data[0] >> RTL8367B_VLAN_MC0_MEMBER_SHIFT) &
  864. RTL8367B_VLAN_MC0_MEMBER_MASK;
  865. vlanmc->fid = (data[1] >> RTL8367B_VLAN_MC1_FID_SHIFT) &
  866. RTL8367B_VLAN_MC1_FID_MASK;
  867. vlanmc->vid = (data[3] >> RTL8367B_VLAN_MC3_EVID_SHIFT) &
  868. RTL8367B_VLAN_MC3_EVID_MASK;
  869. return 0;
  870. }
  871. static int rtl8367b_set_vlan_mc(struct rtl8366_smi *smi, u32 index,
  872. const struct rtl8366_vlan_mc *vlanmc)
  873. {
  874. u32 data[RTL8367B_VLAN_MC_NUM_WORDS];
  875. int err;
  876. int i;
  877. if (index >= RTL8367B_NUM_VLANS ||
  878. vlanmc->vid >= RTL8367B_NUM_VIDS ||
  879. vlanmc->priority > RTL8367B_PRIORITYMAX ||
  880. vlanmc->member > RTL8367B_VLAN_MC0_MEMBER_MASK ||
  881. vlanmc->untag > RTL8367B_UNTAG_MASK ||
  882. vlanmc->fid > ((smi->rtl8367b_chip >= RTL8367B_CHIP_RTL8367S_VB) ? RTL8367D_FIDMAX : RTL8367B_FIDMAX))
  883. return -EINVAL;
  884. if (smi->emu_vlanmc) { /* use vlanmc emulation */
  885. smi->emu_vlanmc[index].vid = vlanmc->vid;
  886. smi->emu_vlanmc[index].member = vlanmc->member;
  887. smi->emu_vlanmc[index].fid = vlanmc->fid;
  888. smi->emu_vlanmc[index].untag = vlanmc->untag;
  889. return 0;
  890. }
  891. data[0] = (vlanmc->member & RTL8367B_VLAN_MC0_MEMBER_MASK) <<
  892. RTL8367B_VLAN_MC0_MEMBER_SHIFT;
  893. data[1] = (vlanmc->fid & RTL8367B_VLAN_MC1_FID_MASK) <<
  894. RTL8367B_VLAN_MC1_FID_SHIFT;
  895. data[2] = 0;
  896. data[3] = (vlanmc->vid & RTL8367B_VLAN_MC3_EVID_MASK) <<
  897. RTL8367B_VLAN_MC3_EVID_SHIFT;
  898. for (i = 0; i < ARRAY_SIZE(data); i++)
  899. REG_WR(smi, RTL8367B_VLAN_MC_BASE(index) + i, data[i]);
  900. return 0;
  901. }
  902. static int rtl8367b_get_mc_index(struct rtl8366_smi *smi, int port, int *val)
  903. {
  904. u32 data;
  905. int err;
  906. if (port >= RTL8367B_NUM_PORTS)
  907. return -EINVAL;
  908. if (smi->rtl8367b_chip >= RTL8367B_CHIP_RTL8367S_VB) { /* Family D */
  909. int i;
  910. struct rtl8366_vlan_mc vlanmc;
  911. err = rtl8366_smi_read_reg(smi, RTL8367D_VLAN_PVID_CTRL_REG(port), &data);
  912. if (err) {
  913. dev_err(smi->parent, "read pvid register 0x%04x fail", RTL8367D_VLAN_PVID_CTRL_REG(port));
  914. return err;
  915. }
  916. data &= RTL8367D_VLAN_PVID_CTRL_MASK;
  917. for (i = 0; i < smi->num_vlan_mc; i++) {
  918. err = rtl8367b_get_vlan_mc(smi, i, &vlanmc);
  919. if (err) {
  920. dev_err(smi->parent, "get vlan mc index %d fail", i);
  921. return err;
  922. }
  923. if (data == vlanmc.vid) break;
  924. }
  925. if (i < smi->num_vlan_mc) {
  926. *val = i;
  927. } else {
  928. dev_err(smi->parent, "vlan mc index for pvid %d not found", data);
  929. return -EINVAL;
  930. }
  931. } else {
  932. REG_RD(smi, RTL8367B_VLAN_PVID_CTRL_REG(port), &data);
  933. *val = (data >> RTL8367B_VLAN_PVID_CTRL_SHIFT(port)) &
  934. RTL8367B_VLAN_PVID_CTRL_MASK;
  935. }
  936. return 0;
  937. }
  938. static int rtl8367b_set_mc_index(struct rtl8366_smi *smi, int port, int index)
  939. {
  940. if (port >= RTL8367B_NUM_PORTS || index >= RTL8367B_NUM_VLANS)
  941. return -EINVAL;
  942. if (smi->rtl8367b_chip >= RTL8367B_CHIP_RTL8367S_VB) { /* Family D */
  943. int pvid, err;
  944. struct rtl8366_vlan_mc vlanmc;
  945. err = rtl8367b_get_vlan_mc(smi, index, &vlanmc);
  946. if (err) {
  947. dev_err(smi->parent, "get vlan mc index %d fail", index);
  948. return err;
  949. }
  950. pvid = vlanmc.vid & RTL8367D_VLAN_PVID_CTRL_MASK;
  951. err = rtl8366_smi_write_reg(smi, RTL8367D_VLAN_PVID_CTRL_REG(port), pvid);
  952. if (err) {
  953. dev_err(smi->parent, "set port %d pvid %d fail", port, pvid);
  954. return err;
  955. }
  956. return 0;
  957. } else
  958. return rtl8366_smi_rmwr(smi, RTL8367B_VLAN_PVID_CTRL_REG(port),
  959. RTL8367B_VLAN_PVID_CTRL_MASK <<
  960. RTL8367B_VLAN_PVID_CTRL_SHIFT(port),
  961. (index & RTL8367B_VLAN_PVID_CTRL_MASK) <<
  962. RTL8367B_VLAN_PVID_CTRL_SHIFT(port));
  963. }
  964. static int rtl8367b_enable_vlan(struct rtl8366_smi *smi, int enable)
  965. {
  966. return rtl8366_smi_rmwr(smi, RTL8367B_VLAN_CTRL_REG,
  967. RTL8367B_VLAN_CTRL_ENABLE,
  968. (enable) ? RTL8367B_VLAN_CTRL_ENABLE : 0);
  969. }
  970. static int rtl8367b_enable_vlan4k(struct rtl8366_smi *smi, int enable)
  971. {
  972. return 0;
  973. }
  974. static int rtl8367b_is_vlan_valid(struct rtl8366_smi *smi, unsigned vlan)
  975. {
  976. unsigned max = RTL8367B_NUM_VLANS;
  977. if (smi->vlan4k_enabled)
  978. max = RTL8367B_NUM_VIDS - 1;
  979. if (vlan == 0 || vlan >= max)
  980. return 0;
  981. return 1;
  982. }
  983. static int rtl8367b_enable_port(struct rtl8366_smi *smi, int port, int enable)
  984. {
  985. int err;
  986. REG_WR(smi, RTL8367B_PORT_ISOLATION_REG(port),
  987. (enable) ? RTL8367B_PORTS_ALL : 0);
  988. return 0;
  989. }
  990. static int rtl8367b_sw_reset_mibs(struct switch_dev *dev,
  991. const struct switch_attr *attr,
  992. struct switch_val *val)
  993. {
  994. struct rtl8366_smi *smi = sw_to_rtl8366_smi(dev);
  995. return rtl8366_smi_rmwr(smi, RTL8367B_MIB_CTRL0_REG(0), 0,
  996. RTL8367B_MIB_CTRL0_GLOBAL_RESET_MASK);
  997. }
  998. static int rtl8367b_sw_get_port_link(struct switch_dev *dev,
  999. int port,
  1000. struct switch_port_link *link)
  1001. {
  1002. struct rtl8366_smi *smi = sw_to_rtl8366_smi(dev);
  1003. u32 data = 0;
  1004. u32 speed;
  1005. if (port >= RTL8367B_NUM_PORTS)
  1006. return -EINVAL;
  1007. if (smi->rtl8367b_chip >= RTL8367B_CHIP_RTL8367S_VB) /* Family D */
  1008. rtl8366_smi_read_reg(smi, RTL8367D_PORT_STATUS_REG(port), &data);
  1009. else
  1010. rtl8366_smi_read_reg(smi, RTL8367B_PORT_STATUS_REG(port), &data);
  1011. link->link = !!(data & RTL8367B_PORT_STATUS_LINK);
  1012. if (!link->link)
  1013. return 0;
  1014. link->duplex = !!(data & RTL8367B_PORT_STATUS_DUPLEX);
  1015. link->rx_flow = !!(data & RTL8367B_PORT_STATUS_RXPAUSE);
  1016. link->tx_flow = !!(data & RTL8367B_PORT_STATUS_TXPAUSE);
  1017. link->aneg = !!(data & RTL8367B_PORT_STATUS_NWAY);
  1018. if (smi->rtl8367b_chip >= RTL8367B_CHIP_RTL8367S_VB) /* Family D */
  1019. speed = (data & RTL8367B_PORT_STATUS_SPEED_MASK) | ((data & RTL8367D_PORT_STATUS_SPEED1_MASK) >> RTL8367D_PORT_STATUS_SPEED1_SHIFT);
  1020. else
  1021. speed = (data & RTL8367B_PORT_STATUS_SPEED_MASK);
  1022. switch (speed) {
  1023. case RTL8367B_PORT_STATUS_SPEED_10:
  1024. link->speed = SWITCH_PORT_SPEED_10;
  1025. break;
  1026. case RTL8367B_PORT_STATUS_SPEED_100:
  1027. link->speed = SWITCH_PORT_SPEED_100;
  1028. break;
  1029. case RTL8367B_PORT_STATUS_SPEED_1000:
  1030. link->speed = SWITCH_PORT_SPEED_1000;
  1031. break;
  1032. default:
  1033. link->speed = SWITCH_PORT_SPEED_UNKNOWN;
  1034. break;
  1035. }
  1036. return 0;
  1037. }
  1038. static int rtl8367b_sw_get_max_length(struct switch_dev *dev,
  1039. const struct switch_attr *attr,
  1040. struct switch_val *val)
  1041. {
  1042. struct rtl8366_smi *smi = sw_to_rtl8366_smi(dev);
  1043. u32 data;
  1044. rtl8366_smi_read_reg(smi, RTL8367B_SWC0_REG, &data);
  1045. val->value.i = (data & RTL8367B_SWC0_MAX_LENGTH_MASK) >>
  1046. RTL8367B_SWC0_MAX_LENGTH_SHIFT;
  1047. return 0;
  1048. }
  1049. static int rtl8367b_sw_set_max_length(struct switch_dev *dev,
  1050. const struct switch_attr *attr,
  1051. struct switch_val *val)
  1052. {
  1053. struct rtl8366_smi *smi = sw_to_rtl8366_smi(dev);
  1054. u32 max_len;
  1055. switch (val->value.i) {
  1056. case 0:
  1057. max_len = RTL8367B_SWC0_MAX_LENGTH_1522;
  1058. break;
  1059. case 1:
  1060. max_len = RTL8367B_SWC0_MAX_LENGTH_1536;
  1061. break;
  1062. case 2:
  1063. max_len = RTL8367B_SWC0_MAX_LENGTH_1552;
  1064. break;
  1065. case 3:
  1066. max_len = RTL8367B_SWC0_MAX_LENGTH_16000;
  1067. break;
  1068. default:
  1069. return -EINVAL;
  1070. }
  1071. return rtl8366_smi_rmwr(smi, RTL8367B_SWC0_REG,
  1072. RTL8367B_SWC0_MAX_LENGTH_MASK, max_len);
  1073. }
  1074. static int rtl8367b_sw_reset_port_mibs(struct switch_dev *dev,
  1075. const struct switch_attr *attr,
  1076. struct switch_val *val)
  1077. {
  1078. struct rtl8366_smi *smi = sw_to_rtl8366_smi(dev);
  1079. int port;
  1080. port = val->port_vlan;
  1081. if (port >= RTL8367B_NUM_PORTS)
  1082. return -EINVAL;
  1083. return rtl8366_smi_rmwr(smi, RTL8367B_MIB_CTRL0_REG(port / 8), 0,
  1084. RTL8367B_MIB_CTRL0_PORT_RESET_MASK(port % 8));
  1085. }
  1086. static int rtl8367b_sw_get_port_stats(struct switch_dev *dev, int port,
  1087. struct switch_port_stats *stats)
  1088. {
  1089. return (rtl8366_sw_get_port_stats(dev, port, stats,
  1090. RTL8367B_MIB_TXB_ID, RTL8367B_MIB_RXB_ID));
  1091. }
  1092. static struct switch_attr rtl8367b_globals[] = {
  1093. {
  1094. .type = SWITCH_TYPE_INT,
  1095. .name = "enable_vlan",
  1096. .description = "Enable VLAN mode",
  1097. .set = rtl8366_sw_set_vlan_enable,
  1098. .get = rtl8366_sw_get_vlan_enable,
  1099. .max = 1,
  1100. .ofs = 1
  1101. }, {
  1102. .type = SWITCH_TYPE_INT,
  1103. .name = "enable_vlan4k",
  1104. .description = "Enable VLAN 4K mode",
  1105. .set = rtl8366_sw_set_vlan_enable,
  1106. .get = rtl8366_sw_get_vlan_enable,
  1107. .max = 1,
  1108. .ofs = 2
  1109. }, {
  1110. .type = SWITCH_TYPE_NOVAL,
  1111. .name = "reset_mibs",
  1112. .description = "Reset all MIB counters",
  1113. .set = rtl8367b_sw_reset_mibs,
  1114. }, {
  1115. .type = SWITCH_TYPE_INT,
  1116. .name = "max_length",
  1117. .description = "Get/Set the maximum length of valid packets"
  1118. "(0:1522, 1:1536, 2:1552, 3:16000)",
  1119. .set = rtl8367b_sw_set_max_length,
  1120. .get = rtl8367b_sw_get_max_length,
  1121. .max = 3,
  1122. }
  1123. };
  1124. static struct switch_attr rtl8367b_port[] = {
  1125. {
  1126. .type = SWITCH_TYPE_NOVAL,
  1127. .name = "reset_mib",
  1128. .description = "Reset single port MIB counters",
  1129. .set = rtl8367b_sw_reset_port_mibs,
  1130. }, {
  1131. .type = SWITCH_TYPE_STRING,
  1132. .name = "mib",
  1133. .description = "Get MIB counters for port",
  1134. .max = 33,
  1135. .set = NULL,
  1136. .get = rtl8366_sw_get_port_mib,
  1137. },
  1138. };
  1139. static struct switch_attr rtl8367b_vlan[] = {
  1140. {
  1141. .type = SWITCH_TYPE_STRING,
  1142. .name = "info",
  1143. .description = "Get vlan information",
  1144. .max = 1,
  1145. .set = NULL,
  1146. .get = rtl8366_sw_get_vlan_info,
  1147. },
  1148. };
  1149. static const struct switch_dev_ops rtl8367b_sw_ops = {
  1150. .attr_global = {
  1151. .attr = rtl8367b_globals,
  1152. .n_attr = ARRAY_SIZE(rtl8367b_globals),
  1153. },
  1154. .attr_port = {
  1155. .attr = rtl8367b_port,
  1156. .n_attr = ARRAY_SIZE(rtl8367b_port),
  1157. },
  1158. .attr_vlan = {
  1159. .attr = rtl8367b_vlan,
  1160. .n_attr = ARRAY_SIZE(rtl8367b_vlan),
  1161. },
  1162. .get_vlan_ports = rtl8366_sw_get_vlan_ports,
  1163. .set_vlan_ports = rtl8366_sw_set_vlan_ports,
  1164. .get_port_pvid = rtl8366_sw_get_port_pvid,
  1165. .set_port_pvid = rtl8366_sw_set_port_pvid,
  1166. .reset_switch = rtl8366_sw_reset_switch,
  1167. .get_port_link = rtl8367b_sw_get_port_link,
  1168. .get_port_stats = rtl8367b_sw_get_port_stats,
  1169. };
  1170. static int rtl8367b_switch_init(struct rtl8366_smi *smi)
  1171. {
  1172. struct switch_dev *dev = &smi->sw_dev;
  1173. int err;
  1174. dev->name = "RTL8367B";
  1175. dev->cpu_port = smi->cpu_port;
  1176. dev->ports = RTL8367B_NUM_PORTS;
  1177. dev->vlans = RTL8367B_NUM_VIDS;
  1178. dev->ops = &rtl8367b_sw_ops;
  1179. dev->alias = dev_name(smi->parent);
  1180. err = register_switch(dev, NULL);
  1181. if (err)
  1182. dev_err(smi->parent, "switch registration failed\n");
  1183. return err;
  1184. }
  1185. static void rtl8367b_switch_cleanup(struct rtl8366_smi *smi)
  1186. {
  1187. unregister_switch(&smi->sw_dev);
  1188. }
  1189. static int rtl8367b_mii_read(struct mii_bus *bus, int addr, int reg)
  1190. {
  1191. struct rtl8366_smi *smi = bus->priv;
  1192. u32 val = 0;
  1193. int err;
  1194. err = rtl8367b_read_phy_reg(smi, addr, reg, &val);
  1195. if (err)
  1196. return 0xffff;
  1197. return val;
  1198. }
  1199. static int rtl8367b_mii_write(struct mii_bus *bus, int addr, int reg, u16 val)
  1200. {
  1201. struct rtl8366_smi *smi = bus->priv;
  1202. u32 t;
  1203. int err;
  1204. err = rtl8367b_write_phy_reg(smi, addr, reg, val);
  1205. if (err)
  1206. return err;
  1207. /* flush write */
  1208. (void) rtl8367b_read_phy_reg(smi, addr, reg, &t);
  1209. return err;
  1210. }
  1211. static int rtl8367b_detect(struct rtl8366_smi *smi)
  1212. {
  1213. const char *chip_name = NULL;
  1214. u32 chip_num;
  1215. u32 chip_ver;
  1216. int ret;
  1217. smi->emu_vlanmc = NULL;
  1218. smi->rtl8367b_chip = RTL8367B_CHIP_UNKNOWN;
  1219. rtl8366_smi_write_reg(smi, RTL8367B_RTL_MAGIC_ID_REG,
  1220. RTL8367B_RTL_MAGIC_ID_VAL);
  1221. ret = rtl8366_smi_read_reg(smi, RTL8367B_CHIP_NUMBER_REG, &chip_num);
  1222. if (ret) {
  1223. dev_err(smi->parent, "unable to read %s register\n",
  1224. "chip number");
  1225. return ret;
  1226. }
  1227. ret = rtl8366_smi_read_reg(smi, RTL8367B_CHIP_VER_REG, &chip_ver);
  1228. if (ret) {
  1229. dev_err(smi->parent, "unable to read %s register\n",
  1230. "chip version");
  1231. return ret;
  1232. }
  1233. switch (chip_ver) {
  1234. case 0x0010:
  1235. if (chip_num == 0x6642) {
  1236. chip_name = "8367S-VB";
  1237. smi->rtl8367b_chip = RTL8367B_CHIP_RTL8367S_VB;
  1238. }
  1239. break;
  1240. case 0x0020:
  1241. if (chip_num == 0x6367) {
  1242. chip_name = "8367RB-VB";
  1243. smi->rtl8367b_chip = RTL8367B_CHIP_RTL8367RB_VB;
  1244. }
  1245. break;
  1246. case 0x00A0:
  1247. if (chip_num == 0x6367) {
  1248. chip_name = "8367S";
  1249. smi->rtl8367b_chip = RTL8367B_CHIP_RTL8367S;
  1250. }
  1251. break;
  1252. case 0x1000:
  1253. chip_name = "8367RB";
  1254. smi->rtl8367b_chip = RTL8367B_CHIP_RTL8367RB;
  1255. break;
  1256. case 0x1010:
  1257. chip_name = "8367R-VB";
  1258. smi->rtl8367b_chip = RTL8367B_CHIP_RTL8367R_VB;
  1259. }
  1260. if (!chip_name) {
  1261. dev_err(smi->parent,
  1262. "unknown chip (num:%04x ver:%04x)\n",
  1263. chip_num, chip_ver);
  1264. return -ENODEV;
  1265. }
  1266. dev_info(smi->parent, "RTL%s chip found (num:%04x ver:%04x)\n", chip_name, chip_num, chip_ver);
  1267. return 0;
  1268. }
  1269. static struct rtl8366_smi_ops rtl8367b_smi_ops = {
  1270. .detect = rtl8367b_detect,
  1271. .reset_chip = rtl8367b_reset_chip,
  1272. .setup = rtl8367b_setup,
  1273. .mii_read = rtl8367b_mii_read,
  1274. .mii_write = rtl8367b_mii_write,
  1275. .get_vlan_mc = rtl8367b_get_vlan_mc,
  1276. .set_vlan_mc = rtl8367b_set_vlan_mc,
  1277. .get_vlan_4k = rtl8367b_get_vlan_4k,
  1278. .set_vlan_4k = rtl8367b_set_vlan_4k,
  1279. .get_mc_index = rtl8367b_get_mc_index,
  1280. .set_mc_index = rtl8367b_set_mc_index,
  1281. .get_mib_counter = rtl8367b_get_mib_counter,
  1282. .is_vlan_valid = rtl8367b_is_vlan_valid,
  1283. .enable_vlan = rtl8367b_enable_vlan,
  1284. .enable_vlan4k = rtl8367b_enable_vlan4k,
  1285. .enable_port = rtl8367b_enable_port,
  1286. };
  1287. static int rtl8367b_probe(struct platform_device *pdev)
  1288. {
  1289. struct rtl8366_smi *smi;
  1290. int err;
  1291. smi = rtl8366_smi_probe(pdev);
  1292. if (IS_ERR(smi))
  1293. return PTR_ERR(smi);
  1294. smi->clk_delay = 1500;
  1295. smi->cmd_read = 0xb9;
  1296. smi->cmd_write = 0xb8;
  1297. smi->ops = &rtl8367b_smi_ops;
  1298. smi->num_ports = RTL8367B_NUM_PORTS;
  1299. smi->cpu_port = UINT_MAX; /* not defined yet */
  1300. smi->num_vlan_mc = RTL8367B_NUM_VLANS;
  1301. smi->mib_counters = rtl8367b_mib_counters;
  1302. smi->num_mib_counters = ARRAY_SIZE(rtl8367b_mib_counters);
  1303. err = rtl8366_smi_init(smi);
  1304. if (err)
  1305. goto err_free_smi;
  1306. platform_set_drvdata(pdev, smi);
  1307. err = rtl8367b_switch_init(smi);
  1308. if (err)
  1309. goto err_clear_drvdata;
  1310. return 0;
  1311. err_clear_drvdata:
  1312. platform_set_drvdata(pdev, NULL);
  1313. rtl8366_smi_cleanup(smi);
  1314. err_free_smi:
  1315. kfree(smi->emu_vlanmc);
  1316. kfree(smi);
  1317. return err;
  1318. }
  1319. static void rtl8367b_remove(struct platform_device *pdev)
  1320. {
  1321. struct rtl8366_smi *smi = platform_get_drvdata(pdev);
  1322. if (smi) {
  1323. rtl8367b_switch_cleanup(smi);
  1324. platform_set_drvdata(pdev, NULL);
  1325. rtl8366_smi_cleanup(smi);
  1326. kfree(smi);
  1327. }
  1328. }
  1329. static void rtl8367b_shutdown(struct platform_device *pdev)
  1330. {
  1331. struct rtl8366_smi *smi = platform_get_drvdata(pdev);
  1332. if (smi)
  1333. rtl8367b_reset_chip(smi);
  1334. }
  1335. static const struct of_device_id rtl8367b_match[] = {
  1336. { .compatible = "realtek,rtl8367b" },
  1337. {},
  1338. };
  1339. MODULE_DEVICE_TABLE(of, rtl8367b_match);
  1340. static struct platform_driver rtl8367b_driver = {
  1341. .driver = {
  1342. .name = RTL8367B_DRIVER_NAME,
  1343. .of_match_table = rtl8367b_match,
  1344. },
  1345. .probe = rtl8367b_probe,
  1346. .remove_new = rtl8367b_remove,
  1347. .shutdown = rtl8367b_shutdown,
  1348. };
  1349. module_platform_driver(rtl8367b_driver);
  1350. MODULE_DESCRIPTION("Realtek RTL8367B ethernet switch driver");
  1351. MODULE_AUTHOR("Gabor Juhos <[email protected]>");
  1352. MODULE_LICENSE("GPL v2");
  1353. MODULE_ALIAS("platform:" RTL8367B_DRIVER_NAME);