rtl8367b.c 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649
  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/of_platform.h>
  18. #include <linux/delay.h>
  19. #include <linux/skbuff.h>
  20. #include <linux/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. #ifdef CONFIG_OF
  646. static int rtl8367b_extif_init_of(struct rtl8366_smi *smi,
  647. const char *name)
  648. {
  649. struct rtl8367_extif_config *cfg;
  650. const __be32 *prop;
  651. int size;
  652. int err;
  653. unsigned cpu_port;
  654. unsigned id = UINT_MAX;
  655. prop = of_get_property(smi->parent->of_node, name, &size);
  656. if (!prop || (size != (10 * sizeof(*prop)))) {
  657. dev_err(smi->parent, "%s property is not defined or invalid\n", name);
  658. err = -EINVAL;
  659. goto err_init;
  660. }
  661. cpu_port = be32_to_cpup(prop++);
  662. switch (cpu_port) {
  663. case RTL8367B_CPU_PORT_NUM:
  664. case RTL8367B_CPU_PORT_NUM + 1:
  665. case RTL8367B_CPU_PORT_NUM + 2:
  666. if (smi->rtl8367b_chip == RTL8367B_CHIP_RTL8367R_VB) { /* for the RTL8367R-VB chip, cpu_port 5 corresponds to extif1 */
  667. if (cpu_port == RTL8367B_CPU_PORT_NUM)
  668. id = 1;
  669. else {
  670. dev_err(smi->parent, "wrong cpu_port %u in %s property\n", cpu_port, name);
  671. err = -EINVAL;
  672. goto err_init;
  673. }
  674. } 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 */
  675. if (cpu_port != RTL8367B_CPU_PORT_NUM) {
  676. id = cpu_port - RTL8367B_CPU_PORT_NUM - 1;
  677. } else {
  678. dev_err(smi->parent, "wrong cpu_port %u in %s property\n", cpu_port, name);
  679. err = -EINVAL;
  680. goto err_init;
  681. }
  682. } else {
  683. id = cpu_port - RTL8367B_CPU_PORT_NUM;
  684. }
  685. if (smi->cpu_port == UINT_MAX) {
  686. dev_info(smi->parent, "cpu_port:%u, assigned to extif%u\n", cpu_port, id);
  687. smi->cpu_port = cpu_port;
  688. }
  689. break;
  690. default:
  691. dev_err(smi->parent, "wrong cpu_port %u in %s property\n", cpu_port, name);
  692. err = -EINVAL;
  693. goto err_init;
  694. }
  695. cfg = kzalloc(sizeof(struct rtl8367_extif_config), GFP_KERNEL);
  696. if (!cfg)
  697. return -ENOMEM;
  698. cfg->txdelay = be32_to_cpup(prop++);
  699. cfg->rxdelay = be32_to_cpup(prop++);
  700. cfg->mode = be32_to_cpup(prop++);
  701. cfg->ability.force_mode = be32_to_cpup(prop++);
  702. cfg->ability.txpause = be32_to_cpup(prop++);
  703. cfg->ability.rxpause = be32_to_cpup(prop++);
  704. cfg->ability.link = be32_to_cpup(prop++);
  705. cfg->ability.duplex = be32_to_cpup(prop++);
  706. cfg->ability.speed = be32_to_cpup(prop++);
  707. err = rtl8367b_extif_init(smi, id, cfg);
  708. kfree(cfg);
  709. err_init:
  710. if (id != 0) rtl8367b_extif_init(smi, 0, NULL);
  711. if (id != 1) rtl8367b_extif_init(smi, 1, NULL);
  712. if (id != 2) rtl8367b_extif_init(smi, 2, NULL);
  713. return err;
  714. }
  715. #else
  716. static int rtl8367b_extif_init_of(struct rtl8366_smi *smi,
  717. const char *name)
  718. {
  719. return -EINVAL;
  720. }
  721. #endif
  722. static int rtl8367b_setup(struct rtl8366_smi *smi)
  723. {
  724. struct rtl8367_platform_data *pdata;
  725. int err;
  726. int i;
  727. pdata = smi->parent->platform_data;
  728. err = rtl8367b_init_regs(smi);
  729. if (err)
  730. return err;
  731. /* initialize external interfaces */
  732. if (smi->parent->of_node) {
  733. err = rtl8367b_extif_init_of(smi, "realtek,extif");
  734. if (err)
  735. return err;
  736. } else {
  737. err = rtl8367b_extif_init(smi, 0, pdata->extif0_cfg);
  738. if (err)
  739. return err;
  740. err = rtl8367b_extif_init(smi, 1, pdata->extif1_cfg);
  741. if (err)
  742. return err;
  743. }
  744. /* set maximum packet length to 1536 bytes */
  745. REG_RMW(smi, RTL8367B_SWC0_REG, RTL8367B_SWC0_MAX_LENGTH_MASK,
  746. RTL8367B_SWC0_MAX_LENGTH_1536);
  747. /*
  748. * discard VLAN tagged packets if the port is not a member of
  749. * the VLAN with which the packets is associated.
  750. */
  751. REG_WR(smi, RTL8367B_VLAN_INGRESS_REG, RTL8367B_PORTS_ALL);
  752. /*
  753. * Setup egress tag mode for each port.
  754. */
  755. for (i = 0; i < RTL8367B_NUM_PORTS; i++)
  756. REG_RMW(smi,
  757. RTL8367B_PORT_MISC_CFG_REG(i),
  758. RTL8367B_PORT_MISC_CFG_EGRESS_MODE_MASK <<
  759. RTL8367B_PORT_MISC_CFG_EGRESS_MODE_SHIFT,
  760. RTL8367B_PORT_MISC_CFG_EGRESS_MODE_ORIGINAL <<
  761. RTL8367B_PORT_MISC_CFG_EGRESS_MODE_SHIFT);
  762. return 0;
  763. }
  764. static int rtl8367b_get_mib_counter(struct rtl8366_smi *smi, int counter,
  765. int port, unsigned long long *val)
  766. {
  767. struct rtl8366_mib_counter *mib;
  768. int offset;
  769. int i;
  770. int err;
  771. u32 addr, data;
  772. u64 mibvalue;
  773. if (port > RTL8367B_NUM_PORTS ||
  774. counter >= RTL8367B_NUM_MIB_COUNTERS)
  775. return -EINVAL;
  776. mib = &rtl8367b_mib_counters[counter];
  777. addr = RTL8367B_MIB_COUNTER_PORT_OFFSET * port + mib->offset;
  778. /*
  779. * Writing access counter address first
  780. * then ASIC will prepare 64bits counter wait for being retrived
  781. */
  782. REG_WR(smi, RTL8367B_MIB_ADDRESS_REG, addr >> 2);
  783. /* read MIB control register */
  784. REG_RD(smi, RTL8367B_MIB_CTRL0_REG(0), &data);
  785. if (data & RTL8367B_MIB_CTRL0_BUSY_MASK)
  786. return -EBUSY;
  787. if (data & RTL8367B_MIB_CTRL0_RESET_MASK)
  788. return -EIO;
  789. if (mib->length == 4)
  790. offset = 3;
  791. else
  792. offset = (mib->offset + 1) % 4;
  793. mibvalue = 0;
  794. for (i = 0; i < mib->length; i++) {
  795. REG_RD(smi, RTL8367B_MIB_COUNTER_REG(offset - i), &data);
  796. mibvalue = (mibvalue << 16) | (data & 0xFFFF);
  797. }
  798. *val = mibvalue;
  799. return 0;
  800. }
  801. static int rtl8367b_get_vlan_4k(struct rtl8366_smi *smi, u32 vid,
  802. struct rtl8366_vlan_4k *vlan4k)
  803. {
  804. u32 data[RTL8367B_TA_VLAN_NUM_WORDS];
  805. int err;
  806. int i;
  807. memset(vlan4k, '\0', sizeof(struct rtl8366_vlan_4k));
  808. if (vid >= RTL8367B_NUM_VIDS)
  809. return -EINVAL;
  810. /* write VID */
  811. REG_WR(smi, RTL8367B_TA_ADDR_REG, vid);
  812. /* write table access control word */
  813. REG_WR(smi, RTL8367B_TA_CTRL_REG, RTL8367B_TA_CTRL_CVLAN_READ);
  814. for (i = 0; i < ARRAY_SIZE(data); i++)
  815. REG_RD(smi, RTL8367B_TA_RDDATA_REG(i), &data[i]);
  816. vlan4k->vid = vid;
  817. vlan4k->member = (data[0] >> RTL8367B_TA_VLAN0_MEMBER_SHIFT) &
  818. RTL8367B_TA_VLAN0_MEMBER_MASK;
  819. vlan4k->untag = (data[0] >> RTL8367B_TA_VLAN0_UNTAG_SHIFT) &
  820. RTL8367B_TA_VLAN0_UNTAG_MASK;
  821. if (smi->rtl8367b_chip >= RTL8367B_CHIP_RTL8367S_VB) /* Family D */
  822. vlan4k->fid = (data[1] >> RTL8367D_TA_VLAN1_FID_SHIFT) &
  823. RTL8367D_TA_VLAN1_FID_MASK;
  824. else
  825. vlan4k->fid = (data[1] >> RTL8367B_TA_VLAN1_FID_SHIFT) &
  826. RTL8367B_TA_VLAN1_FID_MASK;
  827. return 0;
  828. }
  829. static int rtl8367b_set_vlan_4k(struct rtl8366_smi *smi,
  830. const struct rtl8366_vlan_4k *vlan4k)
  831. {
  832. u32 data[RTL8367B_TA_VLAN_NUM_WORDS];
  833. int err;
  834. int i;
  835. if (vlan4k->vid >= RTL8367B_NUM_VIDS ||
  836. vlan4k->member > RTL8367B_TA_VLAN0_MEMBER_MASK ||
  837. vlan4k->untag > RTL8367B_UNTAG_MASK ||
  838. vlan4k->fid > ((smi->rtl8367b_chip >= RTL8367B_CHIP_RTL8367S_VB) ? RTL8367D_FIDMAX : RTL8367B_FIDMAX))
  839. return -EINVAL;
  840. memset(data, 0, sizeof(data));
  841. data[0] = (vlan4k->member & RTL8367B_TA_VLAN0_MEMBER_MASK) <<
  842. RTL8367B_TA_VLAN0_MEMBER_SHIFT;
  843. data[0] |= (vlan4k->untag & RTL8367B_TA_VLAN0_UNTAG_MASK) <<
  844. RTL8367B_TA_VLAN0_UNTAG_SHIFT;
  845. if (smi->rtl8367b_chip >= RTL8367B_CHIP_RTL8367S_VB) /* Family D */
  846. data[1] = ((vlan4k->fid & RTL8367D_TA_VLAN1_FID_MASK) <<
  847. RTL8367D_TA_VLAN1_FID_SHIFT) | 12; /* ivl_svl - BIT(3), svlan_chek_ivl_svl - BIT(2) */
  848. else
  849. data[1] = (vlan4k->fid & RTL8367B_TA_VLAN1_FID_MASK) <<
  850. RTL8367B_TA_VLAN1_FID_SHIFT;
  851. for (i = 0; i < ARRAY_SIZE(data); i++)
  852. REG_WR(smi, RTL8367B_TA_WRDATA_REG(i), data[i]);
  853. /* write VID */
  854. if (smi->rtl8367b_chip >= RTL8367B_CHIP_RTL8367S_VB) /* Family D */
  855. REG_WR(smi, RTL8367B_TA_ADDR_REG,
  856. vlan4k->vid & RTL8367D_TA_VLAN_VID_MASK);
  857. else
  858. REG_WR(smi, RTL8367B_TA_ADDR_REG,
  859. vlan4k->vid & RTL8367B_TA_VLAN_VID_MASK);
  860. /* write table access control word */
  861. REG_WR(smi, RTL8367B_TA_CTRL_REG, RTL8367B_TA_CTRL_CVLAN_WRITE);
  862. return 0;
  863. }
  864. static int rtl8367b_get_vlan_mc(struct rtl8366_smi *smi, u32 index,
  865. struct rtl8366_vlan_mc *vlanmc)
  866. {
  867. u32 data[RTL8367B_VLAN_MC_NUM_WORDS];
  868. int err;
  869. int i;
  870. memset(vlanmc, '\0', sizeof(struct rtl8366_vlan_mc));
  871. if (index >= RTL8367B_NUM_VLANS)
  872. return -EINVAL;
  873. if (smi->emu_vlanmc) { /* use vlan mc emulation */
  874. vlanmc->vid = smi->emu_vlanmc[index].vid;
  875. vlanmc->member = smi->emu_vlanmc[index].member;
  876. vlanmc->fid = smi->emu_vlanmc[index].fid;
  877. vlanmc->untag = smi->emu_vlanmc[index].untag;
  878. return 0;
  879. }
  880. for (i = 0; i < ARRAY_SIZE(data); i++)
  881. REG_RD(smi, RTL8367B_VLAN_MC_BASE(index) + i, &data[i]);
  882. vlanmc->member = (data[0] >> RTL8367B_VLAN_MC0_MEMBER_SHIFT) &
  883. RTL8367B_VLAN_MC0_MEMBER_MASK;
  884. vlanmc->fid = (data[1] >> RTL8367B_VLAN_MC1_FID_SHIFT) &
  885. RTL8367B_VLAN_MC1_FID_MASK;
  886. vlanmc->vid = (data[3] >> RTL8367B_VLAN_MC3_EVID_SHIFT) &
  887. RTL8367B_VLAN_MC3_EVID_MASK;
  888. return 0;
  889. }
  890. static int rtl8367b_set_vlan_mc(struct rtl8366_smi *smi, u32 index,
  891. const struct rtl8366_vlan_mc *vlanmc)
  892. {
  893. u32 data[RTL8367B_VLAN_MC_NUM_WORDS];
  894. int err;
  895. int i;
  896. if (index >= RTL8367B_NUM_VLANS ||
  897. vlanmc->vid >= RTL8367B_NUM_VIDS ||
  898. vlanmc->priority > RTL8367B_PRIORITYMAX ||
  899. vlanmc->member > RTL8367B_VLAN_MC0_MEMBER_MASK ||
  900. vlanmc->untag > RTL8367B_UNTAG_MASK ||
  901. vlanmc->fid > ((smi->rtl8367b_chip >= RTL8367B_CHIP_RTL8367S_VB) ? RTL8367D_FIDMAX : RTL8367B_FIDMAX))
  902. return -EINVAL;
  903. if (smi->emu_vlanmc) { /* use vlanmc emulation */
  904. smi->emu_vlanmc[index].vid = vlanmc->vid;
  905. smi->emu_vlanmc[index].member = vlanmc->member;
  906. smi->emu_vlanmc[index].fid = vlanmc->fid;
  907. smi->emu_vlanmc[index].untag = vlanmc->untag;
  908. return 0;
  909. }
  910. data[0] = (vlanmc->member & RTL8367B_VLAN_MC0_MEMBER_MASK) <<
  911. RTL8367B_VLAN_MC0_MEMBER_SHIFT;
  912. data[1] = (vlanmc->fid & RTL8367B_VLAN_MC1_FID_MASK) <<
  913. RTL8367B_VLAN_MC1_FID_SHIFT;
  914. data[2] = 0;
  915. data[3] = (vlanmc->vid & RTL8367B_VLAN_MC3_EVID_MASK) <<
  916. RTL8367B_VLAN_MC3_EVID_SHIFT;
  917. for (i = 0; i < ARRAY_SIZE(data); i++)
  918. REG_WR(smi, RTL8367B_VLAN_MC_BASE(index) + i, data[i]);
  919. return 0;
  920. }
  921. static int rtl8367b_get_mc_index(struct rtl8366_smi *smi, int port, int *val)
  922. {
  923. u32 data;
  924. int err;
  925. if (port >= RTL8367B_NUM_PORTS)
  926. return -EINVAL;
  927. if (smi->rtl8367b_chip >= RTL8367B_CHIP_RTL8367S_VB) { /* Family D */
  928. int i;
  929. struct rtl8366_vlan_mc vlanmc;
  930. err = rtl8366_smi_read_reg(smi, RTL8367D_VLAN_PVID_CTRL_REG(port), &data);
  931. if (err) {
  932. dev_err(smi->parent, "read pvid register 0x%04x fail", RTL8367D_VLAN_PVID_CTRL_REG(port));
  933. return err;
  934. }
  935. data &= RTL8367D_VLAN_PVID_CTRL_MASK;
  936. for (i = 0; i < smi->num_vlan_mc; i++) {
  937. err = rtl8367b_get_vlan_mc(smi, i, &vlanmc);
  938. if (err) {
  939. dev_err(smi->parent, "get vlan mc index %d fail", i);
  940. return err;
  941. }
  942. if (data == vlanmc.vid) break;
  943. }
  944. if (i < smi->num_vlan_mc) {
  945. *val = i;
  946. } else {
  947. dev_err(smi->parent, "vlan mc index for pvid %d not found", data);
  948. return -EINVAL;
  949. }
  950. } else {
  951. REG_RD(smi, RTL8367B_VLAN_PVID_CTRL_REG(port), &data);
  952. *val = (data >> RTL8367B_VLAN_PVID_CTRL_SHIFT(port)) &
  953. RTL8367B_VLAN_PVID_CTRL_MASK;
  954. }
  955. return 0;
  956. }
  957. static int rtl8367b_set_mc_index(struct rtl8366_smi *smi, int port, int index)
  958. {
  959. if (port >= RTL8367B_NUM_PORTS || index >= RTL8367B_NUM_VLANS)
  960. return -EINVAL;
  961. if (smi->rtl8367b_chip >= RTL8367B_CHIP_RTL8367S_VB) { /* Family D */
  962. int pvid, err;
  963. struct rtl8366_vlan_mc vlanmc;
  964. err = rtl8367b_get_vlan_mc(smi, index, &vlanmc);
  965. if (err) {
  966. dev_err(smi->parent, "get vlan mc index %d fail", index);
  967. return err;
  968. }
  969. pvid = vlanmc.vid & RTL8367D_VLAN_PVID_CTRL_MASK;
  970. err = rtl8366_smi_write_reg(smi, RTL8367D_VLAN_PVID_CTRL_REG(port), pvid);
  971. if (err) {
  972. dev_err(smi->parent, "set port %d pvid %d fail", port, pvid);
  973. return err;
  974. }
  975. return 0;
  976. } else
  977. return rtl8366_smi_rmwr(smi, RTL8367B_VLAN_PVID_CTRL_REG(port),
  978. RTL8367B_VLAN_PVID_CTRL_MASK <<
  979. RTL8367B_VLAN_PVID_CTRL_SHIFT(port),
  980. (index & RTL8367B_VLAN_PVID_CTRL_MASK) <<
  981. RTL8367B_VLAN_PVID_CTRL_SHIFT(port));
  982. }
  983. static int rtl8367b_enable_vlan(struct rtl8366_smi *smi, int enable)
  984. {
  985. return rtl8366_smi_rmwr(smi, RTL8367B_VLAN_CTRL_REG,
  986. RTL8367B_VLAN_CTRL_ENABLE,
  987. (enable) ? RTL8367B_VLAN_CTRL_ENABLE : 0);
  988. }
  989. static int rtl8367b_enable_vlan4k(struct rtl8366_smi *smi, int enable)
  990. {
  991. return 0;
  992. }
  993. static int rtl8367b_is_vlan_valid(struct rtl8366_smi *smi, unsigned vlan)
  994. {
  995. unsigned max = RTL8367B_NUM_VLANS;
  996. if (smi->vlan4k_enabled)
  997. max = RTL8367B_NUM_VIDS - 1;
  998. if (vlan == 0 || vlan >= max)
  999. return 0;
  1000. return 1;
  1001. }
  1002. static int rtl8367b_enable_port(struct rtl8366_smi *smi, int port, int enable)
  1003. {
  1004. int err;
  1005. REG_WR(smi, RTL8367B_PORT_ISOLATION_REG(port),
  1006. (enable) ? RTL8367B_PORTS_ALL : 0);
  1007. return 0;
  1008. }
  1009. static int rtl8367b_sw_reset_mibs(struct switch_dev *dev,
  1010. const struct switch_attr *attr,
  1011. struct switch_val *val)
  1012. {
  1013. struct rtl8366_smi *smi = sw_to_rtl8366_smi(dev);
  1014. return rtl8366_smi_rmwr(smi, RTL8367B_MIB_CTRL0_REG(0), 0,
  1015. RTL8367B_MIB_CTRL0_GLOBAL_RESET_MASK);
  1016. }
  1017. static int rtl8367b_sw_get_port_link(struct switch_dev *dev,
  1018. int port,
  1019. struct switch_port_link *link)
  1020. {
  1021. struct rtl8366_smi *smi = sw_to_rtl8366_smi(dev);
  1022. u32 data = 0;
  1023. u32 speed;
  1024. if (port >= RTL8367B_NUM_PORTS)
  1025. return -EINVAL;
  1026. if (smi->rtl8367b_chip >= RTL8367B_CHIP_RTL8367S_VB) /* Family D */
  1027. rtl8366_smi_read_reg(smi, RTL8367D_PORT_STATUS_REG(port), &data);
  1028. else
  1029. rtl8366_smi_read_reg(smi, RTL8367B_PORT_STATUS_REG(port), &data);
  1030. link->link = !!(data & RTL8367B_PORT_STATUS_LINK);
  1031. if (!link->link)
  1032. return 0;
  1033. link->duplex = !!(data & RTL8367B_PORT_STATUS_DUPLEX);
  1034. link->rx_flow = !!(data & RTL8367B_PORT_STATUS_RXPAUSE);
  1035. link->tx_flow = !!(data & RTL8367B_PORT_STATUS_TXPAUSE);
  1036. link->aneg = !!(data & RTL8367B_PORT_STATUS_NWAY);
  1037. if (smi->rtl8367b_chip >= RTL8367B_CHIP_RTL8367S_VB) /* Family D */
  1038. speed = (data & RTL8367B_PORT_STATUS_SPEED_MASK) | ((data & RTL8367D_PORT_STATUS_SPEED1_MASK) >> RTL8367D_PORT_STATUS_SPEED1_SHIFT);
  1039. else
  1040. speed = (data & RTL8367B_PORT_STATUS_SPEED_MASK);
  1041. switch (speed) {
  1042. case RTL8367B_PORT_STATUS_SPEED_10:
  1043. link->speed = SWITCH_PORT_SPEED_10;
  1044. break;
  1045. case RTL8367B_PORT_STATUS_SPEED_100:
  1046. link->speed = SWITCH_PORT_SPEED_100;
  1047. break;
  1048. case RTL8367B_PORT_STATUS_SPEED_1000:
  1049. link->speed = SWITCH_PORT_SPEED_1000;
  1050. break;
  1051. default:
  1052. link->speed = SWITCH_PORT_SPEED_UNKNOWN;
  1053. break;
  1054. }
  1055. return 0;
  1056. }
  1057. static int rtl8367b_sw_get_max_length(struct switch_dev *dev,
  1058. const struct switch_attr *attr,
  1059. struct switch_val *val)
  1060. {
  1061. struct rtl8366_smi *smi = sw_to_rtl8366_smi(dev);
  1062. u32 data;
  1063. rtl8366_smi_read_reg(smi, RTL8367B_SWC0_REG, &data);
  1064. val->value.i = (data & RTL8367B_SWC0_MAX_LENGTH_MASK) >>
  1065. RTL8367B_SWC0_MAX_LENGTH_SHIFT;
  1066. return 0;
  1067. }
  1068. static int rtl8367b_sw_set_max_length(struct switch_dev *dev,
  1069. const struct switch_attr *attr,
  1070. struct switch_val *val)
  1071. {
  1072. struct rtl8366_smi *smi = sw_to_rtl8366_smi(dev);
  1073. u32 max_len;
  1074. switch (val->value.i) {
  1075. case 0:
  1076. max_len = RTL8367B_SWC0_MAX_LENGTH_1522;
  1077. break;
  1078. case 1:
  1079. max_len = RTL8367B_SWC0_MAX_LENGTH_1536;
  1080. break;
  1081. case 2:
  1082. max_len = RTL8367B_SWC0_MAX_LENGTH_1552;
  1083. break;
  1084. case 3:
  1085. max_len = RTL8367B_SWC0_MAX_LENGTH_16000;
  1086. break;
  1087. default:
  1088. return -EINVAL;
  1089. }
  1090. return rtl8366_smi_rmwr(smi, RTL8367B_SWC0_REG,
  1091. RTL8367B_SWC0_MAX_LENGTH_MASK, max_len);
  1092. }
  1093. static int rtl8367b_sw_reset_port_mibs(struct switch_dev *dev,
  1094. const struct switch_attr *attr,
  1095. struct switch_val *val)
  1096. {
  1097. struct rtl8366_smi *smi = sw_to_rtl8366_smi(dev);
  1098. int port;
  1099. port = val->port_vlan;
  1100. if (port >= RTL8367B_NUM_PORTS)
  1101. return -EINVAL;
  1102. return rtl8366_smi_rmwr(smi, RTL8367B_MIB_CTRL0_REG(port / 8), 0,
  1103. RTL8367B_MIB_CTRL0_PORT_RESET_MASK(port % 8));
  1104. }
  1105. static int rtl8367b_sw_get_port_stats(struct switch_dev *dev, int port,
  1106. struct switch_port_stats *stats)
  1107. {
  1108. return (rtl8366_sw_get_port_stats(dev, port, stats,
  1109. RTL8367B_MIB_TXB_ID, RTL8367B_MIB_RXB_ID));
  1110. }
  1111. static struct switch_attr rtl8367b_globals[] = {
  1112. {
  1113. .type = SWITCH_TYPE_INT,
  1114. .name = "enable_vlan",
  1115. .description = "Enable VLAN mode",
  1116. .set = rtl8366_sw_set_vlan_enable,
  1117. .get = rtl8366_sw_get_vlan_enable,
  1118. .max = 1,
  1119. .ofs = 1
  1120. }, {
  1121. .type = SWITCH_TYPE_INT,
  1122. .name = "enable_vlan4k",
  1123. .description = "Enable VLAN 4K mode",
  1124. .set = rtl8366_sw_set_vlan_enable,
  1125. .get = rtl8366_sw_get_vlan_enable,
  1126. .max = 1,
  1127. .ofs = 2
  1128. }, {
  1129. .type = SWITCH_TYPE_NOVAL,
  1130. .name = "reset_mibs",
  1131. .description = "Reset all MIB counters",
  1132. .set = rtl8367b_sw_reset_mibs,
  1133. }, {
  1134. .type = SWITCH_TYPE_INT,
  1135. .name = "max_length",
  1136. .description = "Get/Set the maximum length of valid packets"
  1137. "(0:1522, 1:1536, 2:1552, 3:16000)",
  1138. .set = rtl8367b_sw_set_max_length,
  1139. .get = rtl8367b_sw_get_max_length,
  1140. .max = 3,
  1141. }
  1142. };
  1143. static struct switch_attr rtl8367b_port[] = {
  1144. {
  1145. .type = SWITCH_TYPE_NOVAL,
  1146. .name = "reset_mib",
  1147. .description = "Reset single port MIB counters",
  1148. .set = rtl8367b_sw_reset_port_mibs,
  1149. }, {
  1150. .type = SWITCH_TYPE_STRING,
  1151. .name = "mib",
  1152. .description = "Get MIB counters for port",
  1153. .max = 33,
  1154. .set = NULL,
  1155. .get = rtl8366_sw_get_port_mib,
  1156. },
  1157. };
  1158. static struct switch_attr rtl8367b_vlan[] = {
  1159. {
  1160. .type = SWITCH_TYPE_STRING,
  1161. .name = "info",
  1162. .description = "Get vlan information",
  1163. .max = 1,
  1164. .set = NULL,
  1165. .get = rtl8366_sw_get_vlan_info,
  1166. },
  1167. };
  1168. static const struct switch_dev_ops rtl8367b_sw_ops = {
  1169. .attr_global = {
  1170. .attr = rtl8367b_globals,
  1171. .n_attr = ARRAY_SIZE(rtl8367b_globals),
  1172. },
  1173. .attr_port = {
  1174. .attr = rtl8367b_port,
  1175. .n_attr = ARRAY_SIZE(rtl8367b_port),
  1176. },
  1177. .attr_vlan = {
  1178. .attr = rtl8367b_vlan,
  1179. .n_attr = ARRAY_SIZE(rtl8367b_vlan),
  1180. },
  1181. .get_vlan_ports = rtl8366_sw_get_vlan_ports,
  1182. .set_vlan_ports = rtl8366_sw_set_vlan_ports,
  1183. .get_port_pvid = rtl8366_sw_get_port_pvid,
  1184. .set_port_pvid = rtl8366_sw_set_port_pvid,
  1185. .reset_switch = rtl8366_sw_reset_switch,
  1186. .get_port_link = rtl8367b_sw_get_port_link,
  1187. .get_port_stats = rtl8367b_sw_get_port_stats,
  1188. };
  1189. static int rtl8367b_switch_init(struct rtl8366_smi *smi)
  1190. {
  1191. struct switch_dev *dev = &smi->sw_dev;
  1192. int err;
  1193. dev->name = "RTL8367B";
  1194. dev->cpu_port = smi->cpu_port;
  1195. dev->ports = RTL8367B_NUM_PORTS;
  1196. dev->vlans = RTL8367B_NUM_VIDS;
  1197. dev->ops = &rtl8367b_sw_ops;
  1198. dev->alias = dev_name(smi->parent);
  1199. err = register_switch(dev, NULL);
  1200. if (err)
  1201. dev_err(smi->parent, "switch registration failed\n");
  1202. return err;
  1203. }
  1204. static void rtl8367b_switch_cleanup(struct rtl8366_smi *smi)
  1205. {
  1206. unregister_switch(&smi->sw_dev);
  1207. }
  1208. static int rtl8367b_mii_read(struct mii_bus *bus, int addr, int reg)
  1209. {
  1210. struct rtl8366_smi *smi = bus->priv;
  1211. u32 val = 0;
  1212. int err;
  1213. err = rtl8367b_read_phy_reg(smi, addr, reg, &val);
  1214. if (err)
  1215. return 0xffff;
  1216. return val;
  1217. }
  1218. static int rtl8367b_mii_write(struct mii_bus *bus, int addr, int reg, u16 val)
  1219. {
  1220. struct rtl8366_smi *smi = bus->priv;
  1221. u32 t;
  1222. int err;
  1223. err = rtl8367b_write_phy_reg(smi, addr, reg, val);
  1224. if (err)
  1225. return err;
  1226. /* flush write */
  1227. (void) rtl8367b_read_phy_reg(smi, addr, reg, &t);
  1228. return err;
  1229. }
  1230. static int rtl8367b_detect(struct rtl8366_smi *smi)
  1231. {
  1232. const char *chip_name = NULL;
  1233. u32 chip_num;
  1234. u32 chip_ver;
  1235. int ret;
  1236. smi->emu_vlanmc = NULL;
  1237. smi->rtl8367b_chip = RTL8367B_CHIP_UNKNOWN;
  1238. rtl8366_smi_write_reg(smi, RTL8367B_RTL_MAGIC_ID_REG,
  1239. RTL8367B_RTL_MAGIC_ID_VAL);
  1240. ret = rtl8366_smi_read_reg(smi, RTL8367B_CHIP_NUMBER_REG, &chip_num);
  1241. if (ret) {
  1242. dev_err(smi->parent, "unable to read %s register\n",
  1243. "chip number");
  1244. return ret;
  1245. }
  1246. ret = rtl8366_smi_read_reg(smi, RTL8367B_CHIP_VER_REG, &chip_ver);
  1247. if (ret) {
  1248. dev_err(smi->parent, "unable to read %s register\n",
  1249. "chip version");
  1250. return ret;
  1251. }
  1252. switch (chip_ver) {
  1253. case 0x0010:
  1254. if (chip_num == 0x6642) {
  1255. chip_name = "8367S-VB";
  1256. smi->rtl8367b_chip = RTL8367B_CHIP_RTL8367S_VB;
  1257. }
  1258. break;
  1259. case 0x0020:
  1260. if (chip_num == 0x6367) {
  1261. chip_name = "8367RB-VB";
  1262. smi->rtl8367b_chip = RTL8367B_CHIP_RTL8367RB_VB;
  1263. }
  1264. break;
  1265. case 0x00A0:
  1266. if (chip_num == 0x6367) {
  1267. chip_name = "8367S";
  1268. smi->rtl8367b_chip = RTL8367B_CHIP_RTL8367S;
  1269. }
  1270. break;
  1271. case 0x1000:
  1272. chip_name = "8367RB";
  1273. smi->rtl8367b_chip = RTL8367B_CHIP_RTL8367RB;
  1274. break;
  1275. case 0x1010:
  1276. chip_name = "8367R-VB";
  1277. smi->rtl8367b_chip = RTL8367B_CHIP_RTL8367R_VB;
  1278. }
  1279. if (!chip_name) {
  1280. dev_err(smi->parent,
  1281. "unknown chip (num:%04x ver:%04x)\n",
  1282. chip_num, chip_ver);
  1283. return -ENODEV;
  1284. }
  1285. dev_info(smi->parent, "RTL%s chip found (num:%04x ver:%04x)\n", chip_name, chip_num, chip_ver);
  1286. return 0;
  1287. }
  1288. static struct rtl8366_smi_ops rtl8367b_smi_ops = {
  1289. .detect = rtl8367b_detect,
  1290. .reset_chip = rtl8367b_reset_chip,
  1291. .setup = rtl8367b_setup,
  1292. .mii_read = rtl8367b_mii_read,
  1293. .mii_write = rtl8367b_mii_write,
  1294. .get_vlan_mc = rtl8367b_get_vlan_mc,
  1295. .set_vlan_mc = rtl8367b_set_vlan_mc,
  1296. .get_vlan_4k = rtl8367b_get_vlan_4k,
  1297. .set_vlan_4k = rtl8367b_set_vlan_4k,
  1298. .get_mc_index = rtl8367b_get_mc_index,
  1299. .set_mc_index = rtl8367b_set_mc_index,
  1300. .get_mib_counter = rtl8367b_get_mib_counter,
  1301. .is_vlan_valid = rtl8367b_is_vlan_valid,
  1302. .enable_vlan = rtl8367b_enable_vlan,
  1303. .enable_vlan4k = rtl8367b_enable_vlan4k,
  1304. .enable_port = rtl8367b_enable_port,
  1305. };
  1306. static int rtl8367b_probe(struct platform_device *pdev)
  1307. {
  1308. struct rtl8366_smi *smi;
  1309. int err;
  1310. smi = rtl8366_smi_probe(pdev);
  1311. if (IS_ERR(smi))
  1312. return PTR_ERR(smi);
  1313. smi->clk_delay = 1500;
  1314. smi->cmd_read = 0xb9;
  1315. smi->cmd_write = 0xb8;
  1316. smi->ops = &rtl8367b_smi_ops;
  1317. smi->num_ports = RTL8367B_NUM_PORTS;
  1318. smi->cpu_port = UINT_MAX; /* not defined yet */
  1319. smi->num_vlan_mc = RTL8367B_NUM_VLANS;
  1320. smi->mib_counters = rtl8367b_mib_counters;
  1321. smi->num_mib_counters = ARRAY_SIZE(rtl8367b_mib_counters);
  1322. err = rtl8366_smi_init(smi);
  1323. if (err)
  1324. goto err_free_smi;
  1325. platform_set_drvdata(pdev, smi);
  1326. err = rtl8367b_switch_init(smi);
  1327. if (err)
  1328. goto err_clear_drvdata;
  1329. return 0;
  1330. err_clear_drvdata:
  1331. platform_set_drvdata(pdev, NULL);
  1332. rtl8366_smi_cleanup(smi);
  1333. err_free_smi:
  1334. if (smi->emu_vlanmc)
  1335. kfree(smi->emu_vlanmc);
  1336. kfree(smi);
  1337. return err;
  1338. }
  1339. static void rtl8367b_remove(struct platform_device *pdev)
  1340. {
  1341. struct rtl8366_smi *smi = platform_get_drvdata(pdev);
  1342. if (smi) {
  1343. rtl8367b_switch_cleanup(smi);
  1344. platform_set_drvdata(pdev, NULL);
  1345. rtl8366_smi_cleanup(smi);
  1346. kfree(smi);
  1347. }
  1348. }
  1349. static void rtl8367b_shutdown(struct platform_device *pdev)
  1350. {
  1351. struct rtl8366_smi *smi = platform_get_drvdata(pdev);
  1352. if (smi)
  1353. rtl8367b_reset_chip(smi);
  1354. }
  1355. #ifdef CONFIG_OF
  1356. static const struct of_device_id rtl8367b_match[] = {
  1357. { .compatible = "realtek,rtl8367b" },
  1358. {},
  1359. };
  1360. MODULE_DEVICE_TABLE(of, rtl8367b_match);
  1361. #endif
  1362. static struct platform_driver rtl8367b_driver = {
  1363. .driver = {
  1364. .name = RTL8367B_DRIVER_NAME,
  1365. #ifdef CONFIG_OF
  1366. .of_match_table = of_match_ptr(rtl8367b_match),
  1367. #endif
  1368. },
  1369. .probe = rtl8367b_probe,
  1370. .remove_new = rtl8367b_remove,
  1371. .shutdown = rtl8367b_shutdown,
  1372. };
  1373. module_platform_driver(rtl8367b_driver);
  1374. MODULE_DESCRIPTION("Realtek RTL8367B ethernet switch driver");
  1375. MODULE_AUTHOR("Gabor Juhos <[email protected]>");
  1376. MODULE_LICENSE("GPL v2");
  1377. MODULE_ALIAS("platform:" RTL8367B_DRIVER_NAME);