rtl838x_sw.c 68 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. #include <linux/etherdevice.h>
  3. #include <linux/if_bridge.h>
  4. #include <linux/iopoll.h>
  5. #include <linux/mdio.h>
  6. #include <linux/mfd/syscon.h>
  7. #include <linux/module.h>
  8. #include <linux/netdevice.h>
  9. #include <linux/of_mdio.h>
  10. #include <linux/of_net.h>
  11. #include <linux/of_platform.h>
  12. #include <linux/phylink.h>
  13. #include <linux/phy_fixed.h>
  14. #include <net/dsa.h>
  15. #include <net/switchdev.h>
  16. #include <asm/mach-rtl838x/mach-rtl838x.h>
  17. #include "rtl838x.h"
  18. #define RTL8380_VERSION_A 'A'
  19. #define RTL8390_VERSION_A 'A'
  20. #define RTL8380_VERSION_B 'B'
  21. DEFINE_MUTEX(smi_lock);
  22. #define MIB_DESC(_size, _offset, _name) {.size = _size, .offset = _offset, .name = _name}
  23. struct rtl838x_mib_desc {
  24. unsigned int size;
  25. unsigned int offset;
  26. const char *name;
  27. };
  28. static inline void rtl838x_mask_port_reg(u64 clear, u64 set, int reg)
  29. {
  30. sw_w32_mask((u32)clear, (u32)set, reg);
  31. }
  32. static inline void rtl838x_set_port_reg(u64 set, int reg)
  33. {
  34. sw_w32(set, reg);
  35. }
  36. static inline u64 rtl838x_get_port_reg(int reg)
  37. {
  38. return ((u64) sw_r32(reg));
  39. }
  40. static inline void rtl839x_mask_port_reg_be(u64 clear, u64 set, int reg)
  41. {
  42. sw_w32_mask((u32)(clear >> 32), (u32)(set >> 32), reg);
  43. sw_w32_mask((u32)(clear & 0xffffffff), (u32)(set & 0xffffffff), reg + 4);
  44. }
  45. static inline void rtl839x_mask_port_reg_le(u64 clear, u64 set, int reg)
  46. {
  47. sw_w32_mask((u32)clear, (u32)set, reg);
  48. sw_w32_mask((u32)(clear >> 32), (u32)(set >> 32), reg + 4);
  49. }
  50. static inline void rtl839x_set_port_reg_be(u64 set, int reg)
  51. {
  52. sw_w32(set >> 32, reg);
  53. sw_w32(set & 0xffffffff, reg + 4);
  54. }
  55. static inline void rtl839x_set_port_reg_le(u64 set, int reg)
  56. {
  57. sw_w32(set, reg);
  58. sw_w32(set >> 32, reg + 4);
  59. }
  60. static inline u64 rtl839x_get_port_reg_be(int reg)
  61. {
  62. u64 v = sw_r32(reg);
  63. v <<= 32;
  64. v |= sw_r32(reg + 4);
  65. return v;
  66. }
  67. static inline u64 rtl839x_get_port_reg_le(int reg)
  68. {
  69. u64 v = sw_r32(reg + 4);
  70. v <<= 32;
  71. v |= sw_r32(reg);
  72. return v;
  73. }
  74. static inline int rtl838x_stat_port_std_mib(int p)
  75. {
  76. return RTL838X_STAT_PORT_STD_MIB + (p << 8);
  77. }
  78. static inline int rtl839x_stat_port_std_mib(int p)
  79. {
  80. return RTL839X_STAT_PORT_STD_MIB + (p << 8);
  81. }
  82. static inline int rtl838x_port_iso_ctrl(int p)
  83. {
  84. return RTL838X_PORT_ISO_CTRL(p);
  85. }
  86. static inline int rtl839x_port_iso_ctrl(int p)
  87. {
  88. return RTL839X_PORT_ISO_CTRL(p);
  89. }
  90. static inline void rtl838x_exec_tbl0_cmd(u32 cmd)
  91. {
  92. sw_w32(cmd, RTL838X_TBL_ACCESS_CTRL_0);
  93. do { } while (sw_r32(RTL838X_TBL_ACCESS_CTRL_0) & (1 << 15));
  94. }
  95. static inline void rtl839x_exec_tbl0_cmd(u32 cmd)
  96. {
  97. sw_w32(cmd, RTL839X_TBL_ACCESS_CTRL_0);
  98. do { } while (sw_r32(RTL839X_TBL_ACCESS_CTRL_0) & (1 << 16));
  99. }
  100. static inline void rtl838x_exec_tbl1_cmd(u32 cmd)
  101. {
  102. sw_w32(cmd, RTL838X_TBL_ACCESS_CTRL_1);
  103. do { } while (sw_r32(RTL838X_TBL_ACCESS_CTRL_1) & (1 << 15));
  104. }
  105. static inline void rtl839x_exec_tbl1_cmd(u32 cmd)
  106. {
  107. sw_w32(cmd, RTL839X_TBL_ACCESS_CTRL_1);
  108. do { } while (sw_r32(RTL839X_TBL_ACCESS_CTRL_1) & (1 << 16));
  109. }
  110. static inline int rtl838x_tbl_access_data_0(int i)
  111. {
  112. return RTL838X_TBL_ACCESS_DATA_0 + (i << 2);
  113. }
  114. static inline int rtl839x_tbl_access_data_0(int i)
  115. {
  116. return RTL839X_TBL_ACCESS_DATA_0 + (i << 2);
  117. }
  118. static inline int rtl838x_vlan_profile(int profile)
  119. {
  120. return RTL838X_VLAN_PROFILE + (profile << 2);
  121. }
  122. static inline int rtl839x_vlan_profile(int profile)
  123. {
  124. return RTL839X_VLAN_PROFILE + (profile << 3);
  125. }
  126. static inline int rtl838x_vlan_port_egr_filter(int port)
  127. {
  128. return RTL838X_VLAN_PORT_EGR_FLTR;
  129. }
  130. static inline int rtl839x_vlan_port_egr_filter(int port)
  131. {
  132. return RTL839X_VLAN_PORT_EGR_FLTR + ((port >> 5) << 2);
  133. }
  134. static inline int rtl838x_vlan_port_igr_filter(int port)
  135. {
  136. return RTL838X_VLAN_PORT_IGR_FLTR + ((port >> 4) << 2);
  137. }
  138. static inline int rtl839x_vlan_port_igr_filter(int port)
  139. {
  140. return RTL839X_VLAN_PORT_IGR_FLTR + ((port >> 4) << 2);
  141. }
  142. static inline int rtl838x_vlan_port_pb(int port)
  143. {
  144. return RTL838X_VLAN_PORT_PB_VLAN + (port << 2);
  145. }
  146. static inline int rtl839x_vlan_port_pb(int port)
  147. {
  148. return RTL839X_VLAN_PORT_PB_VLAN + (port << 2);
  149. }
  150. static void rtl839x_vlan_tables_read(u32 vlan, struct rtl838x_vlan_info *info)
  151. {
  152. u32 cmd;
  153. u64 v;
  154. u32 u, w;
  155. cmd = 1 << 16 /* Execute cmd */
  156. | 0 << 15 /* Read */
  157. | 0 << 12 /* Table type 0b000 */
  158. | (vlan & 0xfff);
  159. rtl839x_exec_tbl0_cmd(cmd);
  160. v = sw_r32(rtl838x_tbl_access_data_0(0));
  161. v <<= 32;
  162. u = sw_r32(rtl838x_tbl_access_data_0(1));
  163. v |= u;
  164. info->tagged_ports = v >> 11;
  165. w = sw_r32(rtl838x_tbl_access_data_0(2));
  166. info->profile_id = w >> 30 | ((u & 1) << 2);
  167. info->hash_mc = !!(u & 2);
  168. info->hash_uc = !!(u & 4);
  169. info->fid = (u >> 3) & 0xff;
  170. cmd = 1 << 16 /* Execute cmd */
  171. | 0 << 15 /* Read */
  172. | 0 << 12 /* Table type 0b000 */
  173. | (vlan & 0xfff);
  174. rtl839x_exec_tbl1_cmd(cmd);
  175. v = sw_r32(RTL838X_TBL_ACCESS_DATA_1(0));
  176. v <<= 32;
  177. v |= sw_r32(RTL838X_TBL_ACCESS_DATA_1(1));
  178. info->untagged_ports = v >> 11;
  179. }
  180. static void rtl838x_vlan_tables_read(u32 vlan, struct rtl838x_vlan_info *info)
  181. {
  182. u32 cmd, v;
  183. cmd = 1 << 15 /* Execute cmd */
  184. | 1 << 14 /* Read */
  185. | 0 << 12 /* Table type 0b00 */
  186. | (vlan & 0xfff);
  187. rtl838x_exec_tbl0_cmd(cmd);
  188. info->tagged_ports = sw_r32(rtl838x_tbl_access_data_0(0));
  189. v = sw_r32(rtl838x_tbl_access_data_0(1));
  190. info->profile_id = v & 0x7;
  191. info->hash_mc = !!(v & 0x8);
  192. info->hash_uc = !!(v & 0x10);
  193. info->fid = (v >> 5) & 0x3f;
  194. cmd = 1 << 15 /* Execute cmd */
  195. | 1 << 14 /* Read */
  196. | 0 << 12 /* Table type 0b00 */
  197. | (vlan & 0xfff);
  198. rtl838x_exec_tbl1_cmd(cmd);
  199. info->untagged_ports = sw_r32(RTL838X_TBL_ACCESS_DATA_1(0));
  200. }
  201. static void rtl839x_vlan_set_tagged(u32 vlan, const struct rtl838x_vlan_info *info)
  202. {
  203. u32 cmd = 1 << 16 /* Execute cmd */
  204. | 1 << 15 /* Write */
  205. | 0 << 12 /* Table type 0b00 */
  206. | (vlan & 0xfff);
  207. u32 w;
  208. u64 v = info->tagged_ports << 11;
  209. v |= info->profile_id >> 2;
  210. v |= info->hash_mc ? 2 : 0;
  211. v |= info->hash_uc ? 4 : 0;
  212. v |= ((u32)info->fid) << 3;
  213. rtl839x_set_port_reg_be(v, rtl838x_tbl_access_data_0(0));
  214. w = info->profile_id;
  215. sw_w32(w << 30, rtl838x_tbl_access_data_0(2));
  216. rtl839x_exec_tbl0_cmd(cmd);
  217. }
  218. static void rtl838x_vlan_set_tagged(u32 vlan, const struct rtl838x_vlan_info *info)
  219. {
  220. u32 cmd = 1 << 15 /* Execute cmd */
  221. | 0 << 14 /* Write */
  222. | 0 << 12 /* Table type 0b00 */
  223. | (vlan & 0xfff);
  224. u32 v;
  225. sw_w32(info->tagged_ports, rtl838x_tbl_access_data_0(0));
  226. v = info->profile_id;
  227. v |= info->hash_mc ? 0x8 : 0;
  228. v |= info->hash_uc ? 0x10 : 0;
  229. v |= ((u32)info->fid) << 5;
  230. sw_w32(v, rtl838x_tbl_access_data_0(1));
  231. rtl838x_exec_tbl0_cmd(cmd);
  232. }
  233. static void rtl839x_vlan_set_untagged(u32 vlan, u64 portmask)
  234. {
  235. u32 cmd = 1 << 16 /* Execute cmd */
  236. | 1 << 15 /* Write */
  237. | 0 << 12 /* Table type 0b00 */
  238. | (vlan & 0xfff);
  239. rtl839x_set_port_reg_be(portmask << 11, RTL838X_TBL_ACCESS_DATA_1(0));
  240. rtl839x_exec_tbl1_cmd(cmd);
  241. }
  242. static void rtl838x_vlan_set_untagged(u32 vlan, u64 portmask)
  243. {
  244. u32 cmd = 1 << 15 /* Execute cmd */
  245. | 0 << 14 /* Write */
  246. | 0 << 12 /* Table type 0b00 */
  247. | (vlan & 0xfff);
  248. sw_w32(portmask & 0x1fffffff, RTL838X_TBL_ACCESS_DATA_1(0));
  249. rtl838x_exec_tbl1_cmd(cmd);
  250. }
  251. static inline int rtl838x_mac_force_mode_ctrl(int p)
  252. {
  253. return RTL838X_MAC_FORCE_MODE_CTRL + (p << 2);
  254. }
  255. static inline int rtl839x_mac_force_mode_ctrl(int p)
  256. {
  257. return RTL839X_MAC_FORCE_MODE_CTRL + (p << 2);
  258. }
  259. static inline int rtl838x_mac_port_ctrl(int p)
  260. {
  261. return RTL838X_MAC_PORT_CTRL(p);
  262. }
  263. static inline int rtl839x_mac_port_ctrl(int p)
  264. {
  265. return RTL839X_MAC_PORT_CTRL(p);
  266. }
  267. static inline int rtl838x_l2_port_new_salrn(int p)
  268. {
  269. return RTL838X_L2_PORT_NEW_SALRN(p);
  270. }
  271. static inline int rtl839x_l2_port_new_salrn(int p)
  272. {
  273. return RTL839X_L2_PORT_NEW_SALRN(p);
  274. }
  275. static inline int rtl838x_l2_port_new_sa_fwd(int p)
  276. {
  277. return RTL838X_L2_PORT_NEW_SA_FWD(p);
  278. }
  279. static inline int rtl839x_l2_port_new_sa_fwd(int p)
  280. {
  281. return RTL839X_L2_PORT_NEW_SA_FWD(p);
  282. }
  283. static inline int rtl838x_mac_link_spd_sts(int p)
  284. {
  285. return RTL838X_MAC_LINK_SPD_STS(p);
  286. }
  287. static inline int rtl839x_mac_link_spd_sts(int p)
  288. {
  289. return RTL839X_MAC_LINK_SPD_STS(p);
  290. }
  291. static inline int rtl838x_mir_ctrl(int group)
  292. {
  293. return RTL838X_MIR_CTRL(group);
  294. }
  295. static inline int rtl839x_mir_ctrl(int group)
  296. {
  297. return RTL839X_MIR_CTRL(group);
  298. }
  299. static inline int rtl838x_mir_dpm(int group)
  300. {
  301. return RTL838X_MIR_DPM_CTRL(group);
  302. }
  303. static inline int rtl839x_mir_dpm(int group)
  304. {
  305. return RTL839X_MIR_DPM_CTRL(group);
  306. }
  307. static inline int rtl838x_mir_spm(int group)
  308. {
  309. return RTL838X_MIR_SPM_CTRL(group);
  310. }
  311. static inline int rtl839x_mir_spm(int group)
  312. {
  313. return RTL839X_MIR_SPM_CTRL(group);
  314. }
  315. static u64 rtl838x_read_l2_entry_using_hash(u32 hash, u32 position, struct rtl838x_l2_entry *e)
  316. {
  317. u64 entry;
  318. u32 r[3];
  319. /* Search in SRAM, with hash and at position in hash bucket (0-3) */
  320. u32 idx = (0 << 14) | (hash << 2) | position;
  321. u32 cmd = 1 << 16 /* Execute cmd */
  322. | 1 << 15 /* Read */
  323. | 0 << 13 /* Table type 0b00 */
  324. | (idx & 0x1fff);
  325. sw_w32(cmd, RTL838X_TBL_ACCESS_L2_CTRL);
  326. do { } while (sw_r32(RTL838X_TBL_ACCESS_L2_CTRL) & (1 << 16));
  327. r[0] = sw_r32(RTL838X_TBL_ACCESS_L2_DATA(0));
  328. r[1] = sw_r32(RTL838X_TBL_ACCESS_L2_DATA(1));
  329. r[2] = sw_r32(RTL838X_TBL_ACCESS_L2_DATA(2));
  330. e->mac[0] = (r[1] >> 20);
  331. e->mac[1] = (r[1] >> 12);
  332. e->mac[2] = (r[1] >> 4);
  333. e->mac[3] = (r[1] & 0xf) << 4 | (r[2] >> 28);
  334. e->mac[4] = (r[2] >> 20);
  335. e->mac[5] = (r[2] >> 12);
  336. e->is_static = !!((r[0] >> 19) & 1);
  337. e->vid = r[0] & 0xfff;
  338. e->rvid = r[2] & 0xfff;
  339. e->port = (r[0] >> 12) & 0x1f;
  340. e->valid = true;
  341. if (!(r[0] >> 17)) /* Check for invalid entry */
  342. e->valid = false;
  343. if (e->valid)
  344. pr_info("Found in Hash: R1 %x R2 %x R3 %x\n", r[0], r[1], r[2]);
  345. entry = (((u64) r[1]) << 32) | (r[2] & 0xfffff000) | (r[0] & 0xfff);
  346. return entry;
  347. }
  348. static u64 rtl839x_read_l2_entry_using_hash(u32 hash, u32 position, struct rtl838x_l2_entry *e)
  349. {
  350. u64 entry;
  351. u32 r[3];
  352. /* Search in SRAM, with hash and at position in hash bucket (0-3) */
  353. u32 idx = (0 << 14) | (hash << 2) | position;
  354. u32 cmd = 1 << 17 /* Execute cmd */
  355. | 0 << 16 /* Read */
  356. | 0 << 14 /* Table type 0b00 */
  357. | (idx & 0x3fff);
  358. sw_w32(cmd, RTL839X_TBL_ACCESS_L2_CTRL);
  359. do { } while (sw_r32(RTL839X_TBL_ACCESS_L2_CTRL) & (1 << 17));
  360. r[0] = sw_r32(RTL839X_TBL_ACCESS_L2_DATA(0));
  361. r[1] = sw_r32(RTL839X_TBL_ACCESS_L2_DATA(1));
  362. r[2] = sw_r32(RTL839X_TBL_ACCESS_L2_DATA(2));
  363. /* Table contains different entry types, we need to identify the right one:
  364. * Check for MC entries, first
  365. */
  366. e->is_ip_mc = !!(r[2] & (1 << 31));
  367. e->is_ipv6_mc = !!(r[2] & (1 << 30));
  368. e->type = L2_INVALID;
  369. if (!e->is_ip_mc) {
  370. e->mac[0] = (r[0] >> 12);
  371. e->mac[1] = (r[0] >> 4);
  372. e->mac[2] = ((r[1] >> 28) | (r[0] << 4));
  373. e->mac[3] = (r[1] >> 20);
  374. e->mac[4] = (r[1] >> 12);
  375. e->mac[5] = (r[1] >> 4);
  376. /* Is it a unicast entry? check multicast bit */
  377. if (!(e->mac[0] & 1)) {
  378. e->is_static = !!((r[2] >> 18) & 1);
  379. e->vid = (r[2] >> 4) & 0xfff;
  380. e->rvid = (r[0] >> 20) & 0xfff;
  381. e->port = (r[2] >> 24) & 0x3f;
  382. e->block_da = !!(r[2] & (1 << 19));
  383. e->block_sa = !!(r[2] & (1 << 20));
  384. e->suspended = !!(r[2] & (1 << 17));
  385. e->next_hop = !!(r[2] & (1 << 16));
  386. if (e->next_hop)
  387. pr_info("Found next hop entry, need to read data\n");
  388. e->age = (r[2] >> 21) & 3;
  389. e->valid = true;
  390. if (!(r[2] & 0xc0fd0000)) /* Check for valid entry */
  391. e->valid = false;
  392. else
  393. e->type = L2_UNICAST;
  394. } else {
  395. e->valid = true;
  396. e->type = L2_MULTICAST;
  397. e->mc_portmask_index = (r[2] >> 6) & 0xfff;
  398. }
  399. }
  400. if (e->is_ip_mc) {
  401. e->valid = true;
  402. e->type = IP4_MULTICAST;
  403. }
  404. if (e->is_ipv6_mc) {
  405. e->valid = true;
  406. e->type = IP6_MULTICAST;
  407. }
  408. entry = (((u64) r[0]) << 44) | ((u64)(r[1] & 0xfffffff0) << 12) | ((r[2] >> 4) & 0xfff);
  409. return entry;
  410. }
  411. static u64 rtl838x_read_cam(int idx, struct rtl838x_l2_entry *e)
  412. {
  413. u64 entry;
  414. u32 r[3];
  415. u32 cmd = 1 << 16 /* Execute cmd */
  416. | 1 << 15 /* Read */
  417. | 1 << 13 /* Table type 0b01 */
  418. | (idx & 0x3f);
  419. sw_w32(cmd, RTL838X_TBL_ACCESS_L2_CTRL);
  420. do { } while (sw_r32(RTL838X_TBL_ACCESS_L2_CTRL) & (1 << 16));
  421. r[0] = sw_r32(RTL838X_TBL_ACCESS_L2_DATA(0));
  422. r[1] = sw_r32(RTL838X_TBL_ACCESS_L2_DATA(1));
  423. r[2] = sw_r32(RTL838X_TBL_ACCESS_L2_DATA(2));
  424. e->mac[0] = (r[1] >> 20);
  425. e->mac[1] = (r[1] >> 12);
  426. e->mac[2] = (r[1] >> 4);
  427. e->mac[3] = (r[1] & 0xf) << 4 | (r[2] >> 28);
  428. e->mac[4] = (r[2] >> 20);
  429. e->mac[5] = (r[2] >> 12);
  430. e->is_static = !!((r[0] >> 19) & 1);
  431. e->vid = r[0] & 0xfff;
  432. e->rvid = r[2] & 0xfff;
  433. e->port = (r[0] >> 12) & 0x1f;
  434. e->valid = true;
  435. if (!(r[0] >> 17)) /* Check for invalid entry */
  436. e->valid = false;
  437. if (e->valid)
  438. pr_info("Found in CAM: R1 %x R2 %x R3 %x\n", r[0], r[1], r[2]);
  439. entry = (((u64) r[1]) << 32) | (r[2] & 0xfffff000) | (r[0] & 0xfff);
  440. return entry;
  441. }
  442. static u64 rtl839x_read_cam(int idx, struct rtl838x_l2_entry *e)
  443. {
  444. u64 entry;
  445. u32 r[3];
  446. u32 cmd = 1 << 17 /* Execute cmd */
  447. | 0 << 16 /* Read */
  448. | 1 << 14 /* Table type 0b01 */
  449. | (idx & 0x3f);
  450. sw_w32(cmd, RTL839X_TBL_ACCESS_L2_CTRL);
  451. do { } while (sw_r32(RTL839X_TBL_ACCESS_L2_CTRL) & (1 << 17));
  452. r[0] = sw_r32(RTL839X_TBL_ACCESS_L2_DATA(0));
  453. r[1] = sw_r32(RTL839X_TBL_ACCESS_L2_DATA(1));
  454. r[2] = sw_r32(RTL839X_TBL_ACCESS_L2_DATA(2));
  455. e->mac[0] = (r[0] >> 12);
  456. e->mac[1] = (r[0] >> 4);
  457. e->mac[2] = ((r[1] >> 28) | (r[0] << 4));
  458. e->mac[3] = (r[1] >> 20);
  459. e->mac[4] = (r[1] >> 12);
  460. e->mac[5] = (r[1] >> 4);
  461. e->is_static = !!((r[2] >> 18) & 1);
  462. e->vid = (r[2] >> 4) & 0xfff;
  463. e->rvid = (r[0] >> 20) & 0xfff;
  464. e->port = (r[2] >> 24) & 0x3f;
  465. e->valid = true;
  466. if (!(r[2] & 0x10fd0000)) /* Check for invalid entry */
  467. e->valid = false;
  468. if (e->valid)
  469. pr_info("Found in CAM: R1 %x R2 %x R3 %x\n", r[0], r[1], r[2]);
  470. entry = (((u64) r[0]) << 12) | ((r[1] & 0xfffffff0) << 12) | ((r[2] >> 4) & 0xfff);
  471. return entry;
  472. }
  473. static const struct rtl838x_reg rtl838x_reg = {
  474. .mask_port_reg_be = rtl838x_mask_port_reg,
  475. .set_port_reg_be = rtl838x_set_port_reg,
  476. .get_port_reg_be = rtl838x_get_port_reg,
  477. .mask_port_reg_le = rtl838x_mask_port_reg,
  478. .set_port_reg_le = rtl838x_set_port_reg,
  479. .get_port_reg_le = rtl838x_get_port_reg,
  480. .stat_port_rst = RTL838X_STAT_PORT_RST,
  481. .stat_rst = RTL838X_STAT_RST,
  482. .stat_port_std_mib = rtl838x_stat_port_std_mib,
  483. .port_iso_ctrl = rtl838x_port_iso_ctrl,
  484. .l2_ctrl_0 = RTL838X_L2_CTRL_0,
  485. .l2_ctrl_1 = RTL838X_L2_CTRL_1,
  486. .l2_port_aging_out = RTL838X_L2_PORT_AGING_OUT,
  487. .smi_poll_ctrl = RTL838X_SMI_POLL_CTRL,
  488. .l2_tbl_flush_ctrl = RTL838X_L2_TBL_FLUSH_CTRL,
  489. .exec_tbl0_cmd = rtl838x_exec_tbl0_cmd,
  490. .exec_tbl1_cmd = rtl838x_exec_tbl1_cmd,
  491. .tbl_access_data_0 = rtl838x_tbl_access_data_0,
  492. .isr_glb_src = RTL838X_ISR_GLB_SRC,
  493. .isr_port_link_sts_chg = RTL838X_ISR_PORT_LINK_STS_CHG,
  494. .imr_port_link_sts_chg = RTL838X_IMR_PORT_LINK_STS_CHG,
  495. .imr_glb = RTL838X_IMR_GLB,
  496. .vlan_tables_read = rtl838x_vlan_tables_read,
  497. .vlan_set_tagged = rtl838x_vlan_set_tagged,
  498. .vlan_set_untagged = rtl838x_vlan_set_untagged,
  499. .mac_force_mode_ctrl = rtl838x_mac_force_mode_ctrl,
  500. .mac_port_ctrl = rtl838x_mac_port_ctrl,
  501. .l2_port_new_salrn = rtl838x_l2_port_new_salrn,
  502. .l2_port_new_sa_fwd = rtl838x_l2_port_new_sa_fwd,
  503. .mir_ctrl = rtl838x_mir_ctrl,
  504. .mir_dpm = rtl838x_mir_dpm,
  505. .mir_spm = rtl838x_mir_spm,
  506. .mac_link_sts = RTL838X_MAC_LINK_STS,
  507. .mac_link_dup_sts = RTL838X_MAC_LINK_DUP_STS,
  508. .mac_link_spd_sts = rtl838x_mac_link_spd_sts,
  509. .mac_rx_pause_sts = RTL838X_MAC_RX_PAUSE_STS,
  510. .mac_tx_pause_sts = RTL838X_MAC_TX_PAUSE_STS,
  511. .read_l2_entry_using_hash = rtl838x_read_l2_entry_using_hash,
  512. .read_cam = rtl838x_read_cam,
  513. .vlan_profile = rtl838x_vlan_profile,
  514. .vlan_port_egr_filter = rtl838x_vlan_port_egr_filter,
  515. .vlan_port_igr_filter = rtl838x_vlan_port_igr_filter,
  516. .vlan_port_pb = rtl838x_vlan_port_pb,
  517. };
  518. static const struct rtl838x_reg rtl839x_reg = {
  519. .mask_port_reg_be = rtl839x_mask_port_reg_be,
  520. .set_port_reg_be = rtl839x_set_port_reg_be,
  521. .get_port_reg_be = rtl839x_get_port_reg_be,
  522. .mask_port_reg_le = rtl839x_mask_port_reg_le,
  523. .set_port_reg_le = rtl839x_set_port_reg_le,
  524. .get_port_reg_le = rtl839x_get_port_reg_le,
  525. .stat_port_rst = RTL839X_STAT_PORT_RST,
  526. .stat_rst = RTL839X_STAT_RST,
  527. .stat_port_std_mib = rtl839x_stat_port_std_mib,
  528. .port_iso_ctrl = rtl839x_port_iso_ctrl,
  529. .l2_ctrl_0 = RTL839X_L2_CTRL_0,
  530. .l2_ctrl_1 = RTL839X_L2_CTRL_1,
  531. .l2_port_aging_out = RTL839X_L2_PORT_AGING_OUT,
  532. .smi_poll_ctrl = RTL839X_SMI_PORT_POLLING_CTRL,
  533. .l2_tbl_flush_ctrl = RTL839X_L2_TBL_FLUSH_CTRL,
  534. .exec_tbl0_cmd = rtl839x_exec_tbl0_cmd,
  535. .exec_tbl1_cmd = rtl839x_exec_tbl1_cmd,
  536. .tbl_access_data_0 = rtl839x_tbl_access_data_0,
  537. .isr_glb_src = RTL839X_ISR_GLB_SRC,
  538. .isr_port_link_sts_chg = RTL839X_ISR_PORT_LINK_STS_CHG,
  539. .imr_port_link_sts_chg = RTL839X_IMR_PORT_LINK_STS_CHG,
  540. .imr_glb = RTL839X_IMR_GLB,
  541. .vlan_tables_read = rtl839x_vlan_tables_read,
  542. .vlan_set_tagged = rtl839x_vlan_set_tagged,
  543. .vlan_set_untagged = rtl839x_vlan_set_untagged,
  544. .mac_force_mode_ctrl = rtl839x_mac_force_mode_ctrl,
  545. .mac_port_ctrl = rtl839x_mac_port_ctrl,
  546. .l2_port_new_salrn = rtl839x_l2_port_new_salrn,
  547. .l2_port_new_sa_fwd = rtl839x_l2_port_new_sa_fwd,
  548. .mir_ctrl = rtl839x_mir_ctrl,
  549. .mir_dpm = rtl839x_mir_dpm,
  550. .mir_spm = rtl839x_mir_spm,
  551. .mac_link_sts = RTL839X_MAC_LINK_STS,
  552. .mac_link_dup_sts = RTL839X_MAC_LINK_DUP_STS,
  553. .mac_link_spd_sts = rtl839x_mac_link_spd_sts,
  554. .mac_rx_pause_sts = RTL839X_MAC_RX_PAUSE_STS,
  555. .mac_tx_pause_sts = RTL839X_MAC_TX_PAUSE_STS,
  556. .read_l2_entry_using_hash = rtl839x_read_l2_entry_using_hash,
  557. .read_cam = rtl839x_read_cam,
  558. .vlan_profile = rtl839x_vlan_profile,
  559. .vlan_port_egr_filter = rtl839x_vlan_port_egr_filter,
  560. .vlan_port_igr_filter = rtl839x_vlan_port_igr_filter,
  561. .vlan_port_pb = rtl839x_vlan_port_pb,
  562. };
  563. static const struct rtl838x_mib_desc rtl838x_mib[] = {
  564. MIB_DESC(2, 0xf8, "ifInOctets"),
  565. MIB_DESC(2, 0xf0, "ifOutOctets"),
  566. MIB_DESC(1, 0xec, "dot1dTpPortInDiscards"),
  567. MIB_DESC(1, 0xe8, "ifInUcastPkts"),
  568. MIB_DESC(1, 0xe4, "ifInMulticastPkts"),
  569. MIB_DESC(1, 0xe0, "ifInBroadcastPkts"),
  570. MIB_DESC(1, 0xdc, "ifOutUcastPkts"),
  571. MIB_DESC(1, 0xd8, "ifOutMulticastPkts"),
  572. MIB_DESC(1, 0xd4, "ifOutBroadcastPkts"),
  573. MIB_DESC(1, 0xd0, "ifOutDiscards"),
  574. MIB_DESC(1, 0xcc, ".3SingleCollisionFrames"),
  575. MIB_DESC(1, 0xc8, ".3MultipleCollisionFrames"),
  576. MIB_DESC(1, 0xc4, ".3DeferredTransmissions"),
  577. MIB_DESC(1, 0xc0, ".3LateCollisions"),
  578. MIB_DESC(1, 0xbc, ".3ExcessiveCollisions"),
  579. MIB_DESC(1, 0xb8, ".3SymbolErrors"),
  580. MIB_DESC(1, 0xb4, ".3ControlInUnknownOpcodes"),
  581. MIB_DESC(1, 0xb0, ".3InPauseFrames"),
  582. MIB_DESC(1, 0xac, ".3OutPauseFrames"),
  583. MIB_DESC(1, 0xa8, "DropEvents"),
  584. MIB_DESC(1, 0xa4, "tx_BroadcastPkts"),
  585. MIB_DESC(1, 0xa0, "tx_MulticastPkts"),
  586. MIB_DESC(1, 0x9c, "CRCAlignErrors"),
  587. MIB_DESC(1, 0x98, "tx_UndersizePkts"),
  588. MIB_DESC(1, 0x94, "rx_UndersizePkts"),
  589. MIB_DESC(1, 0x90, "rx_UndersizedropPkts"),
  590. MIB_DESC(1, 0x8c, "tx_OversizePkts"),
  591. MIB_DESC(1, 0x88, "rx_OversizePkts"),
  592. MIB_DESC(1, 0x84, "Fragments"),
  593. MIB_DESC(1, 0x80, "Jabbers"),
  594. MIB_DESC(1, 0x7c, "Collisions"),
  595. MIB_DESC(1, 0x78, "tx_Pkts64Octets"),
  596. MIB_DESC(1, 0x74, "rx_Pkts64Octets"),
  597. MIB_DESC(1, 0x70, "tx_Pkts65to127Octets"),
  598. MIB_DESC(1, 0x6c, "rx_Pkts65to127Octets"),
  599. MIB_DESC(1, 0x68, "tx_Pkts128to255Octets"),
  600. MIB_DESC(1, 0x64, "rx_Pkts128to255Octets"),
  601. MIB_DESC(1, 0x60, "tx_Pkts256to511Octets"),
  602. MIB_DESC(1, 0x5c, "rx_Pkts256to511Octets"),
  603. MIB_DESC(1, 0x58, "tx_Pkts512to1023Octets"),
  604. MIB_DESC(1, 0x54, "rx_Pkts512to1023Octets"),
  605. MIB_DESC(1, 0x50, "tx_Pkts1024to1518Octets"),
  606. MIB_DESC(1, 0x4c, "rx_StatsPkts1024to1518Octets"),
  607. MIB_DESC(1, 0x48, "tx_Pkts1519toMaxOctets"),
  608. MIB_DESC(1, 0x44, "rx_Pkts1519toMaxOctets"),
  609. MIB_DESC(1, 0x40, "rxMacDiscards")
  610. };
  611. static irqreturn_t rtl838x_switch_irq(int irq, void *dev_id)
  612. {
  613. struct dsa_switch *ds = dev_id;
  614. u32 status = sw_r32(RTL838X_ISR_GLB_SRC);
  615. u32 ports = sw_r32(RTL838X_ISR_PORT_LINK_STS_CHG);
  616. u32 link;
  617. int i;
  618. /* Clear status */
  619. sw_w32(ports, RTL838X_ISR_PORT_LINK_STS_CHG);
  620. pr_info("RTL8380 Link change: status: %x, ports %x\n", status, ports);
  621. for (i = 0; i < 28; i++) {
  622. if (ports & (1 << i)) {
  623. link = sw_r32(RTL838X_MAC_LINK_STS);
  624. if (link & (1 << i))
  625. dsa_port_phylink_mac_change(ds, i, true);
  626. else
  627. dsa_port_phylink_mac_change(ds, i, false);
  628. }
  629. }
  630. return IRQ_HANDLED;
  631. }
  632. static irqreturn_t rtl839x_switch_irq(int irq, void *dev_id)
  633. {
  634. struct dsa_switch *ds = dev_id;
  635. u32 status = sw_r32(RTL839X_ISR_GLB_SRC);
  636. u64 ports = rtl839x_get_port_reg_le(RTL839X_ISR_PORT_LINK_STS_CHG);
  637. u64 link;
  638. int i;
  639. /* Clear status */
  640. rtl839x_set_port_reg_le(ports, RTL839X_ISR_PORT_LINK_STS_CHG);
  641. pr_info("RTL8390 Link change: status: %x, ports %llx\n", status, ports);
  642. for (i = 0; i < 52; i++) {
  643. if (ports & (1ULL << i)) {
  644. link = rtl839x_get_port_reg_le(RTL839X_MAC_LINK_STS);
  645. if (link & (1ULL << i))
  646. dsa_port_phylink_mac_change(ds, i, true);
  647. else
  648. dsa_port_phylink_mac_change(ds, i, false);
  649. }
  650. }
  651. return IRQ_HANDLED;
  652. }
  653. struct fdb_update_work {
  654. struct work_struct work;
  655. struct net_device *ndev;
  656. u64 macs[];
  657. };
  658. void rtl838x_fdb_sync(struct work_struct *work)
  659. {
  660. const struct fdb_update_work *uw =
  661. container_of(work, struct fdb_update_work, work);
  662. struct switchdev_notifier_fdb_info info;
  663. u8 addr[ETH_ALEN];
  664. int i = 0;
  665. int action;
  666. while (uw->macs[i]) {
  667. action = (uw->macs[i] & (1ULL << 63)) ? SWITCHDEV_FDB_ADD_TO_BRIDGE
  668. : SWITCHDEV_FDB_DEL_TO_BRIDGE;
  669. u64_to_ether_addr(uw->macs[i] & 0xffffffffffffULL, addr);
  670. info.addr = &addr[0];
  671. info.vid = 0;
  672. info.offloaded = 1;
  673. pr_debug("FDB entry %d: %llx, action %d\n", i, uw->macs[0], action);
  674. call_switchdev_notifiers(action, uw->ndev, &info.info, NULL);
  675. i++;
  676. }
  677. kfree(work);
  678. }
  679. int rtl8380_sds_power(int mac, int val)
  680. {
  681. u32 mode = (val == 1) ? 0x4 : 0x9;
  682. u32 offset = (mac == 24) ? 5 : 0;
  683. if ((mac != 24) && (mac != 26)) {
  684. pr_err("%s: not a fibre port: %d\n", __func__, mac);
  685. return -1;
  686. }
  687. sw_w32_mask(0x1f << offset, mode << offset, RTL838X_SDS_MODE_SEL);
  688. rtl8380_sds_rst(mac);
  689. return 0;
  690. }
  691. int rtl8390_sds_power(int mac, int val)
  692. {
  693. u32 offset = (mac == 48) ? 0x0 : 0x100;
  694. u32 mode = val ? 0 : 1;
  695. pr_info("In %s: mac %d, set %d\n", __func__, mac, val);
  696. if ((mac != 48) && (mac != 49)) {
  697. pr_err("%s: not an SFP port: %d\n", __func__, mac);
  698. return -1;
  699. }
  700. // Set bit 1003. 1000 starts at 7c
  701. sw_w32_mask(1 << 11, mode << 11, RTL839X_SDS12_13_PWR0 + offset);
  702. return 0;
  703. }
  704. static int rtl838x_smi_wait_op(int timeout)
  705. {
  706. do {
  707. timeout--;
  708. udelay(10);
  709. } while ((sw_r32(RTL838X_SMI_ACCESS_PHY_CTRL_1) & 0x1) && (timeout >= 0));
  710. if (timeout <= 0)
  711. return -1;
  712. return 0;
  713. }
  714. /*
  715. * Write to a register in a page of the PHY
  716. */
  717. int rtl838x_write_phy(u32 port, u32 page, u32 reg, u32 val)
  718. {
  719. u32 v;
  720. u32 park_page;
  721. val &= 0xffff;
  722. if (port > 31 || page > 4095 || reg > 31)
  723. return -ENOTSUPP;
  724. mutex_lock(&smi_lock);
  725. if (rtl838x_smi_wait_op(10000))
  726. goto timeout;
  727. sw_w32(1 << port, RTL838X_SMI_ACCESS_PHY_CTRL_0);
  728. mdelay(10);
  729. sw_w32_mask(0xffff0000, val << 16, RTL838X_SMI_ACCESS_PHY_CTRL_2);
  730. park_page = sw_r32(RTL838X_SMI_ACCESS_PHY_CTRL_1) & ((0x1f << 15) | 0x2);
  731. v = reg << 20 | page << 3 | 0x4;
  732. sw_w32(v | park_page, RTL838X_SMI_ACCESS_PHY_CTRL_1);
  733. sw_w32_mask(0, 1, RTL838X_SMI_ACCESS_PHY_CTRL_1);
  734. if (rtl838x_smi_wait_op(10000))
  735. goto timeout;
  736. mutex_unlock(&smi_lock);
  737. return 0;
  738. timeout:
  739. mutex_unlock(&smi_lock);
  740. return -ETIMEDOUT;
  741. }
  742. int rtl839x_write_phy(u32 port, u32 page, u32 reg, u32 val)
  743. {
  744. u32 v;
  745. int err = 0;
  746. val &= 0xffff;
  747. if (port > 63 || page > 4095 || reg > 31)
  748. return -ENOTSUPP;
  749. mutex_lock(&smi_lock);
  750. /* Clear both port registers */
  751. sw_w32(0, RTL839X_PHYREG_PORT_CTRL(0));
  752. sw_w32(0, RTL839X_PHYREG_PORT_CTRL(0) + 4);
  753. sw_w32_mask(0, 1 << port, RTL839X_PHYREG_PORT_CTRL(port));
  754. sw_w32_mask(0xffff0000, val << 16, RTL839X_PHYREG_DATA_CTRL);
  755. v = reg << 5 | page << 10 | ((page == 0x1fff) ? 0x1f : 0) << 23;
  756. sw_w32(v, RTL839X_PHYREG_ACCESS_CTRL);
  757. sw_w32(0x1ff, RTL839X_PHYREG_CTRL);
  758. v |= 1 << 3 | 1; /* Write operation and execute */
  759. sw_w32(v, RTL839X_PHYREG_ACCESS_CTRL);
  760. do {
  761. } while (sw_r32(RTL839X_PHYREG_ACCESS_CTRL) & 0x1);
  762. if (sw_r32(RTL839X_PHYREG_ACCESS_CTRL) & 0x2)
  763. err = -EIO;
  764. mutex_unlock(&smi_lock);
  765. return err;
  766. }
  767. /*
  768. * Reads a register in a page from the PHY
  769. */
  770. int rtl838x_read_phy(u32 port, u32 page, u32 reg, u32 *val)
  771. {
  772. u32 v;
  773. u32 park_page;
  774. if (port > 31) {
  775. *val = 0xffff;
  776. return 0;
  777. }
  778. if (page > 4095 || reg > 31)
  779. return -ENOTSUPP;
  780. mutex_lock(&smi_lock);
  781. if (rtl838x_smi_wait_op(10000))
  782. goto timeout;
  783. sw_w32_mask(0xffff0000, port << 16, RTL838X_SMI_ACCESS_PHY_CTRL_2);
  784. park_page = sw_r32(RTL838X_SMI_ACCESS_PHY_CTRL_1) & ((0x1f << 15) | 0x2);
  785. v = reg << 20 | page << 3;
  786. sw_w32(v | park_page, RTL838X_SMI_ACCESS_PHY_CTRL_1);
  787. sw_w32_mask(0, 1, RTL838X_SMI_ACCESS_PHY_CTRL_1);
  788. if (rtl838x_smi_wait_op(10000))
  789. goto timeout;
  790. *val = sw_r32(RTL838X_SMI_ACCESS_PHY_CTRL_2) & 0xffff;
  791. mutex_unlock(&smi_lock);
  792. return 0;
  793. timeout:
  794. mutex_unlock(&smi_lock);
  795. return -ETIMEDOUT;
  796. }
  797. int rtl839x_read_phy(u32 port, u32 page, u32 reg, u32 *val)
  798. {
  799. u32 v;
  800. if (port > 63 || page > 4095 || reg > 31)
  801. return -ENOTSUPP;
  802. mutex_lock(&smi_lock);
  803. sw_w32_mask(0xffff0000, port << 16, RTL839X_PHYREG_DATA_CTRL);
  804. v = reg << 5 | page << 10 | ((page == 0x1fff) ? 0x1f : 0) << 23;
  805. sw_w32(v, RTL839X_PHYREG_ACCESS_CTRL);
  806. sw_w32(0x1ff, RTL839X_PHYREG_CTRL);
  807. v |= 1;
  808. sw_w32(v, RTL839X_PHYREG_ACCESS_CTRL);
  809. do {
  810. } while (sw_r32(RTL839X_PHYREG_ACCESS_CTRL) & 0x1);
  811. *val = sw_r32(RTL839X_PHYREG_DATA_CTRL) & 0xffff;
  812. mutex_unlock(&smi_lock);
  813. return 0;
  814. }
  815. static int read_phy(u32 port, u32 page, u32 reg, u32 *val)
  816. {
  817. if (soc_info.family == RTL8390_FAMILY_ID)
  818. return rtl839x_read_phy(port, page, reg, val);
  819. else
  820. return rtl838x_read_phy(port, page, reg, val);
  821. }
  822. static int write_phy(u32 port, u32 page, u32 reg, u32 val)
  823. {
  824. if (soc_info.family == RTL8390_FAMILY_ID)
  825. return rtl839x_write_phy(port, page, reg, val);
  826. else
  827. return rtl838x_write_phy(port, page, reg, val);
  828. }
  829. /*
  830. * Write to an mmd register of the PHY
  831. */
  832. int rtl838x_write_mmd_phy(u32 port, u32 addr, u32 reg, u32 val)
  833. {
  834. u32 v;
  835. pr_debug("MMD write: port %d, dev %d, reg %d, val %x\n", port, addr, reg, val);
  836. val &= 0xffff;
  837. mutex_lock(&smi_lock);
  838. if (rtl838x_smi_wait_op(10000))
  839. goto timeout;
  840. sw_w32(1 << port, RTL838X_SMI_ACCESS_PHY_CTRL_0);
  841. mdelay(10);
  842. sw_w32_mask(0xffff0000, val << 16, RTL838X_SMI_ACCESS_PHY_CTRL_2);
  843. sw_w32_mask(0x1f << 16, addr << 16, RTL838X_SMI_ACCESS_PHY_CTRL_3);
  844. sw_w32_mask(0xffff, reg, RTL838X_SMI_ACCESS_PHY_CTRL_3);
  845. /* mmd-access | write | cmd-start */
  846. v = 1 << 1 | 1 << 2 | 1;
  847. sw_w32(v, RTL838X_SMI_ACCESS_PHY_CTRL_1);
  848. if (rtl838x_smi_wait_op(10000))
  849. goto timeout;
  850. mutex_unlock(&smi_lock);
  851. return 0;
  852. timeout:
  853. mutex_unlock(&smi_lock);
  854. return -ETIMEDOUT;
  855. }
  856. /*
  857. * Read an mmd register of the PHY
  858. */
  859. int rtl838x_read_mmd_phy(u32 port, u32 addr, u32 reg, u32 *val)
  860. {
  861. u32 v;
  862. mutex_lock(&smi_lock);
  863. if (rtl838x_smi_wait_op(10000))
  864. goto timeout;
  865. sw_w32(1 << port, RTL838X_SMI_ACCESS_PHY_CTRL_0);
  866. mdelay(10);
  867. sw_w32_mask(0xffff0000, port << 16, RTL838X_SMI_ACCESS_PHY_CTRL_2);
  868. v = addr << 16 | reg;
  869. sw_w32(v, RTL838X_SMI_ACCESS_PHY_CTRL_3);
  870. /* mmd-access | read | cmd-start */
  871. v = 1 << 1 | 0 << 2 | 1;
  872. sw_w32(v, RTL838X_SMI_ACCESS_PHY_CTRL_1);
  873. if (rtl838x_smi_wait_op(10000))
  874. goto timeout;
  875. *val = sw_r32(RTL838X_SMI_ACCESS_PHY_CTRL_2) & 0xffff;
  876. mutex_unlock(&smi_lock);
  877. return 0;
  878. timeout:
  879. mutex_unlock(&smi_lock);
  880. return -ETIMEDOUT;
  881. }
  882. static void rtl8380_get_version(struct rtl838x_switch_priv *priv)
  883. {
  884. u32 rw_save, info_save;
  885. u32 info;
  886. if (priv->id)
  887. pr_debug("SoC ID: %4x: %s\n", priv->id, soc_info.name);
  888. else
  889. pr_err("Unknown chip id (%04x)\n", priv->id);
  890. rw_save = sw_r32(RTL838X_INT_RW_CTRL);
  891. sw_w32(rw_save | 0x3, RTL838X_INT_RW_CTRL);
  892. info_save = sw_r32(RTL838X_CHIP_INFO);
  893. sw_w32(info_save | 0xA0000000, RTL838X_CHIP_INFO);
  894. info = sw_r32(RTL838X_CHIP_INFO);
  895. sw_w32(info_save, RTL838X_CHIP_INFO);
  896. sw_w32(rw_save, RTL838X_INT_RW_CTRL);
  897. if ((info & 0xFFFF) == 0x6275) {
  898. if (((info >> 16) & 0x1F) == 0x1)
  899. priv->version = RTL8380_VERSION_A;
  900. else if (((info >> 16) & 0x1F) == 0x2)
  901. priv->version = RTL8380_VERSION_B;
  902. else
  903. priv->version = RTL8380_VERSION_B;
  904. } else {
  905. priv->version = '-';
  906. }
  907. }
  908. static void rtl8390_get_version(struct rtl838x_switch_priv *priv)
  909. {
  910. u32 info;
  911. sw_w32_mask(0xf << 28, 0xa << 28, RTL839X_CHIP_INFO);
  912. info = sw_r32(RTL839X_CHIP_INFO);
  913. pr_info("Chip-Info: %x\n", info);
  914. priv->version = RTL8390_VERSION_A;
  915. }
  916. int dsa_phy_read(struct dsa_switch *ds, int phy_addr, int phy_reg)
  917. {
  918. u32 val;
  919. u32 offset = 0;
  920. struct rtl838x_switch_priv *priv = ds->priv;
  921. if (phy_addr >= 24 && phy_addr <= 27
  922. && priv->ports[24].phy == PHY_RTL838X_SDS) {
  923. if (phy_addr == 26)
  924. offset = 0x100;
  925. val = sw_r32(MAPLE_SDS4_FIB_REG0r + offset + (phy_reg << 2)) & 0xffff;
  926. return val;
  927. }
  928. read_phy(phy_addr, 0, phy_reg, &val);
  929. return val;
  930. }
  931. int dsa_phy_write(struct dsa_switch *ds, int phy_addr, int phy_reg, u16 val)
  932. {
  933. u32 offset = 0;
  934. struct rtl838x_switch_priv *priv = ds->priv;
  935. if (phy_addr >= 24 && phy_addr <= 27
  936. && priv->ports[24].phy == PHY_RTL838X_SDS) {
  937. if (phy_addr == 26)
  938. offset = 0x100;
  939. sw_w32(val, MAPLE_SDS4_FIB_REG0r + offset + (phy_reg << 2));
  940. return 0;
  941. }
  942. return write_phy(phy_addr, 0, phy_reg, val);
  943. }
  944. static int rtl838x_mdio_read(struct mii_bus *bus, int addr, int regnum)
  945. {
  946. int ret;
  947. struct rtl838x_switch_priv *priv = bus->priv;
  948. ret = dsa_phy_read(priv->ds, addr, regnum);
  949. return ret;
  950. }
  951. static int rtl838x_mdio_write(struct mii_bus *bus, int addr, int regnum,
  952. u16 val)
  953. {
  954. struct rtl838x_switch_priv *priv = bus->priv;
  955. return dsa_phy_write(priv->ds, addr, regnum, val);
  956. }
  957. static void rtl838x_enable_phy_polling(struct rtl838x_switch_priv *priv)
  958. {
  959. int i;
  960. u64 v = 0;
  961. msleep(1000);
  962. /* Enable all ports with a PHY, including the SFP-ports */
  963. for (i = 0; i < priv->cpu_port; i++) {
  964. if (priv->ports[i].phy)
  965. v |= 1 << i;
  966. }
  967. pr_info("%s: %16llx\n", __func__, v);
  968. priv->r->set_port_reg_le(v, priv->r->smi_poll_ctrl);
  969. /* PHY update complete */
  970. if (priv->family_id == RTL8390_FAMILY_ID)
  971. sw_w32_mask(0, 1 << 7, RTL839X_SMI_GLB_CTRL);
  972. else
  973. sw_w32_mask(0, 0x8000, RTL838X_SMI_GLB_CTRL);
  974. }
  975. void rtl839x_print_matrix(void)
  976. {
  977. volatile u64 *ptr = RTL838X_SW_BASE + RTL839X_PORT_ISO_CTRL(0);
  978. int i;
  979. for (i = 0; i < 52; i += 4)
  980. pr_info("> %16llx %16llx %16llx %16llx\n",
  981. ptr[i + 0], ptr[i + 1], ptr[i + 2], ptr[i + 3]);
  982. pr_info("CPU_PORT> %16llx\n", ptr[52]);
  983. }
  984. void rtl838x_print_matrix(void)
  985. {
  986. unsigned volatile int *ptr = RTL838X_SW_BASE + RTL838X_PORT_ISO_CTRL(0);
  987. int i;
  988. if (soc_info.family == RTL8390_FAMILY_ID)
  989. return rtl839x_print_matrix();
  990. for (i = 0; i < 28; i += 8)
  991. pr_info("> %8x %8x %8x %8x %8x %8x %8x %8x\n",
  992. ptr[i + 0], ptr[i + 1], ptr[i + 2], ptr[i + 3], ptr[i + 4], ptr[i + 5],
  993. ptr[i + 6], ptr[i + 7]);
  994. pr_info("CPU_PORT> %8x\n", ptr[28]);
  995. }
  996. static void rtl838x_init_stats(struct rtl838x_switch_priv *priv)
  997. {
  998. mutex_lock(&priv->reg_mutex);
  999. /* Enable statistics module: all counters plus debug.
  1000. * On RTL839x all counters are enabled by default
  1001. */
  1002. if (priv->family_id == RTL8380_FAMILY_ID)
  1003. sw_w32_mask(0, 3, RTL838X_STAT_CTRL);
  1004. /* Reset statistics counters */
  1005. sw_w32_mask(0, 1, priv->r->stat_rst);
  1006. mutex_unlock(&priv->reg_mutex);
  1007. }
  1008. static int rtl838x_setup(struct dsa_switch *ds)
  1009. {
  1010. int i;
  1011. struct rtl838x_switch_priv *priv = ds->priv;
  1012. u64 port_bitmap = 1ULL << priv->cpu_port;
  1013. pr_info("%s called\n", __func__);
  1014. /* Disable MAC polling the PHY so that we can start configuration */
  1015. priv->r->set_port_reg_le(0ULL, priv->r->smi_poll_ctrl);
  1016. for (i = 0; i < ds->num_ports; i++)
  1017. priv->ports[i].enable = false;
  1018. priv->ports[priv->cpu_port].enable = true;
  1019. /* Isolate ports from each other: traffic only CPU <-> port */
  1020. /* Setting bit j in register RTL838X_PORT_ISO_CTRL(i) allows
  1021. * traffic from source port i to destination port j
  1022. */
  1023. for (i = 0; i < priv->cpu_port; i++) {
  1024. if (priv->ports[i].phy) {
  1025. priv->r->set_port_reg_be(1ULL << priv->cpu_port | 1ULL << i,
  1026. priv->r->port_iso_ctrl(i));
  1027. port_bitmap |= 1ULL << i;
  1028. }
  1029. }
  1030. priv->r->set_port_reg_be(port_bitmap, priv->r->port_iso_ctrl(priv->cpu_port));
  1031. rtl838x_print_matrix();
  1032. rtl838x_init_stats(priv);
  1033. /* Enable MAC Polling PHY again */
  1034. rtl838x_enable_phy_polling(priv);
  1035. pr_info("Please wait until PHY is settled\n");
  1036. msleep(1000);
  1037. return 0;
  1038. }
  1039. static void rtl838x_get_strings(struct dsa_switch *ds,
  1040. int port, u32 stringset, u8 *data)
  1041. {
  1042. int i;
  1043. if (stringset != ETH_SS_STATS)
  1044. return;
  1045. for (i = 0; i < ARRAY_SIZE(rtl838x_mib); i++)
  1046. strncpy(data + i * ETH_GSTRING_LEN, rtl838x_mib[i].name,
  1047. ETH_GSTRING_LEN);
  1048. }
  1049. static void rtl838x_get_ethtool_stats(struct dsa_switch *ds, int port,
  1050. uint64_t *data)
  1051. {
  1052. struct rtl838x_switch_priv *priv = ds->priv;
  1053. const struct rtl838x_mib_desc *mib;
  1054. int i;
  1055. u64 high;
  1056. for (i = 0; i < ARRAY_SIZE(rtl838x_mib); i++) {
  1057. mib = &rtl838x_mib[i];
  1058. data[i] = sw_r32(priv->r->stat_port_std_mib(port) + 252 - mib->offset);
  1059. if (mib->size == 2) {
  1060. high = sw_r32(priv->r->stat_port_std_mib(port) + 252 - mib->offset - 4);
  1061. data[i] |= high << 32;
  1062. }
  1063. }
  1064. }
  1065. static int rtl838x_get_sset_count(struct dsa_switch *ds, int port, int sset)
  1066. {
  1067. if (sset != ETH_SS_STATS)
  1068. return 0;
  1069. return ARRAY_SIZE(rtl838x_mib);
  1070. }
  1071. static enum dsa_tag_protocol
  1072. rtl838x_get_tag_protocol(struct dsa_switch *ds, int port)
  1073. {
  1074. /* The switch does not tag the frames, instead internally the header
  1075. * structure for each packet is tagged accordingly.
  1076. */
  1077. return DSA_TAG_PROTO_TRAILER;
  1078. }
  1079. static int rtl838x_get_l2aging(struct rtl838x_switch_priv *priv)
  1080. {
  1081. int t = sw_r32(priv->r->l2_ctrl_1);
  1082. t &= priv->family_id == RTL8380_FAMILY_ID ? 0x7fffff : 0x1FFFFF;
  1083. if (priv->family_id == RTL8380_FAMILY_ID)
  1084. t = t * 128 / 625; /* Aging time in seconds. 0: L2 aging disabled */
  1085. else
  1086. t = (t * 3) / 5;
  1087. pr_info("L2 AGING time: %d sec\n", t);
  1088. pr_info("Dynamic aging for ports: %x\n", sw_r32(priv->r->l2_port_aging_out));
  1089. return t;
  1090. }
  1091. /*
  1092. * Set Switch L2 Aging time, t is time in milliseconds
  1093. * t = 0: aging is disabled
  1094. */
  1095. static int rtl838x_set_l2aging(struct dsa_switch *ds, u32 t)
  1096. {
  1097. struct rtl838x_switch_priv *priv = ds->priv;
  1098. int t_max = priv->family_id == RTL8380_FAMILY_ID ? 0x7fffff : 0x1FFFFF;
  1099. /* Convert time in mseconds to internal value */
  1100. if (t > 0x10000000) { /* Set to maximum */
  1101. t = t_max;
  1102. } else {
  1103. if (priv->family_id == RTL8380_FAMILY_ID)
  1104. t = ((t * 625) / 1000 + 127) / 128;
  1105. else
  1106. t = (t * 5 + 2) / 3;
  1107. }
  1108. sw_w32(t, priv->r->l2_ctrl_1);
  1109. return 0;
  1110. }
  1111. static void rtl838x_fast_age(struct dsa_switch *ds, int port)
  1112. {
  1113. struct rtl838x_switch_priv *priv = ds->priv;
  1114. int s = priv->family_id == RTL8390_FAMILY_ID ? 2 : 0;
  1115. pr_info("FAST AGE port %d\n", port);
  1116. mutex_lock(&priv->reg_mutex);
  1117. /* RTL838X_L2_TBL_FLUSH_CTRL register bits, 839x has 1 bit larger
  1118. * port fields:
  1119. * 0-4: Replacing port
  1120. * 5-9: Flushed/replaced port
  1121. * 10-21: FVID
  1122. * 22: Entry types: 1: dynamic, 0: also static
  1123. * 23: Match flush port
  1124. * 24: Match FVID
  1125. * 25: Flush (0) or replace (1) L2 entries
  1126. * 26: Status of action (1: Start, 0: Done)
  1127. */
  1128. sw_w32(1 << (26 + s) | 1 << (23 + s) | port << (5 + (s / 2)), priv->r->l2_tbl_flush_ctrl);
  1129. do { } while (sw_r32(priv->r->l2_tbl_flush_ctrl) & (1 << (26 + s)));
  1130. mutex_unlock(&priv->reg_mutex);
  1131. }
  1132. /*
  1133. * Applies the same hash algorithm as the one used currently by the ASIC
  1134. */
  1135. static u32 rtl838x_hash(struct rtl838x_switch_priv *priv, u64 seed)
  1136. {
  1137. u32 h1, h2, h3, h;
  1138. if (sw_r32(priv->r->l2_ctrl_0) & 1) {
  1139. h1 = (seed >> 11) & 0x7ff;
  1140. h1 = ((h1 & 0x1f) << 6) | ((h1 >> 5) & 0x3f);
  1141. h2 = (seed >> 33) & 0x7ff;
  1142. h2 = ((h2 & 0x3f) << 5) | ((h2 >> 6) & 0x1f);
  1143. h3 = (seed >> 44) & 0x7ff;
  1144. h3 = ((h3 & 0x7f) << 4) | ((h3 >> 7) & 0xf);
  1145. h = h1 ^ h2 ^ h3 ^ ((seed >> 55) & 0x1ff);
  1146. h ^= ((seed >> 22) & 0x7ff) ^ (seed & 0x7ff);
  1147. } else {
  1148. h = ((seed >> 55) & 0x1ff) ^ ((seed >> 44) & 0x7ff)
  1149. ^ ((seed >> 33) & 0x7ff) ^ ((seed >> 22) & 0x7ff)
  1150. ^ ((seed >> 11) & 0x7ff) ^ (seed & 0x7ff);
  1151. }
  1152. return h;
  1153. }
  1154. static u32 rtl839x_hash(struct rtl838x_switch_priv *priv, u64 seed)
  1155. {
  1156. u32 h1, h2, h;
  1157. if (sw_r32(priv->r->l2_ctrl_0) & 1) {
  1158. h1 = (u32) (((seed >> 60) & 0x3f) ^ ((seed >> 54) & 0x3f)
  1159. ^ ((seed >> 36) & 0x3f) ^ ((seed >> 30) & 0x3f)
  1160. ^ ((seed >> 12) & 0x3f) ^ ((seed >> 6) & 0x3f));
  1161. h2 = (u32) (((seed >> 48) & 0x3f) ^ ((seed >> 42) & 0x3f)
  1162. ^ ((seed >> 24) & 0x3f) ^ ((seed >> 18) & 0x3f)
  1163. ^ (seed & 0x3f));
  1164. h = (h1 << 6) | h2;
  1165. } else {
  1166. h = (seed >> 60)
  1167. ^ ((((seed >> 48) & 0x3f) << 6) | ((seed >> 54) & 0x3f))
  1168. ^ ((seed >> 36) & 0xfff) ^ ((seed >> 24) & 0xfff)
  1169. ^ ((seed >> 12) & 0xfff) ^ (seed & 0xfff);
  1170. }
  1171. return h;
  1172. }
  1173. static u64 rtl838x_hash_key(struct rtl838x_switch_priv *priv, u64 mac, u32 vid)
  1174. {
  1175. if (priv->family_id == RTL8380_FAMILY_ID)
  1176. return rtl838x_hash(priv, mac << 12 | vid);
  1177. else
  1178. return rtl839x_hash(priv, mac << 12 | vid);
  1179. }
  1180. static void rtl838x_write_cam(int idx, u32 *r)
  1181. {
  1182. u32 cmd = 1 << 16 /* Execute cmd */
  1183. | 1 << 15 /* Read */
  1184. | 1 << 13 /* Table type 0b01 */
  1185. | (idx & 0x3f);
  1186. sw_w32(r[0], RTL838X_TBL_ACCESS_L2_DATA(0));
  1187. sw_w32(r[1], RTL838X_TBL_ACCESS_L2_DATA(1));
  1188. sw_w32(r[2], RTL838X_TBL_ACCESS_L2_DATA(2));
  1189. sw_w32(cmd, RTL838X_TBL_ACCESS_L2_CTRL);
  1190. do { } while (sw_r32(RTL838X_TBL_ACCESS_L2_CTRL) & (1 << 16));
  1191. }
  1192. static void rtl838x_write_hash(int idx, u32 *r)
  1193. {
  1194. u32 cmd = 1 << 16 /* Execute cmd */
  1195. | 0 << 15 /* Write */
  1196. | 0 << 13 /* Table type 0b00 */
  1197. | (idx & 0x1fff);
  1198. sw_w32(0, RTL838X_TBL_ACCESS_L2_DATA(0));
  1199. sw_w32(0, RTL838X_TBL_ACCESS_L2_DATA(1));
  1200. sw_w32(0, RTL838X_TBL_ACCESS_L2_DATA(2));
  1201. sw_w32(cmd, RTL838X_TBL_ACCESS_L2_CTRL);
  1202. do { } while (sw_r32(RTL838X_TBL_ACCESS_L2_CTRL) & (1 << 16));
  1203. }
  1204. static void dump_fdb(struct rtl838x_switch_priv *priv)
  1205. {
  1206. struct rtl838x_l2_entry e;
  1207. int i;
  1208. mutex_lock(&priv->reg_mutex);
  1209. for (i = 0; i < priv->fib_entries; i++) {
  1210. priv->r->read_l2_entry_using_hash(i >> 2, i & 0x3, &e);
  1211. if (!e.valid) /* Check for invalid entry */
  1212. continue;
  1213. pr_info("-> port %02d: mac %pM, vid: %d, rvid: %d, MC: %d, %d\n",
  1214. e.port, &e.mac[0], e.vid, e.rvid, e.is_ip_mc, e.is_ipv6_mc);
  1215. }
  1216. mutex_unlock(&priv->reg_mutex);
  1217. }
  1218. static void rtl838x_port_get_stp_state(struct rtl838x_switch_priv *priv, int port)
  1219. {
  1220. u32 cmd, msti = 0;
  1221. u32 port_state[4];
  1222. int index, bit, i;
  1223. int pos = port;
  1224. int n = priv->family_id == RTL8380_FAMILY_ID ? 2 : 4;
  1225. /* CPU PORT can only be configured on RTL838x */
  1226. if (port >= priv->cpu_port || port > 51)
  1227. return;
  1228. mutex_lock(&priv->reg_mutex);
  1229. /* For the RTL839x, the bits are left-aligned in the 128 bit field */
  1230. if (priv->family_id == RTL8390_FAMILY_ID)
  1231. pos += 12;
  1232. index = n - (pos >> 4) - 1;
  1233. bit = (pos << 1) % 32;
  1234. if (priv->family_id == RTL8380_FAMILY_ID) {
  1235. cmd = 1 << 15 /* Execute cmd */
  1236. | 1 << 14 /* Read */
  1237. | 2 << 12 /* Table type 0b10 */
  1238. | (msti & 0xfff);
  1239. } else {
  1240. cmd = 1 << 16 /* Execute cmd */
  1241. | 0 << 15 /* Read */
  1242. | 5 << 12 /* Table type 0b101 */
  1243. | (msti & 0xfff);
  1244. }
  1245. priv->r->exec_tbl0_cmd(cmd);
  1246. for (i = 0; i < n; i++)
  1247. port_state[i] = sw_r32(priv->r->tbl_access_data_0(i));
  1248. mutex_unlock(&priv->reg_mutex);
  1249. }
  1250. static int rtl838x_port_fdb_dump(struct dsa_switch *ds, int port,
  1251. dsa_fdb_dump_cb_t *cb, void *data)
  1252. {
  1253. struct rtl838x_l2_entry e;
  1254. struct rtl838x_switch_priv *priv = ds->priv;
  1255. int i;
  1256. u32 fid;
  1257. u32 pkey;
  1258. u64 mac;
  1259. mutex_lock(&priv->reg_mutex);
  1260. for (i = 0; i < priv->fib_entries; i++) {
  1261. priv->r->read_l2_entry_using_hash(i >> 2, i & 0x3, &e);
  1262. if (!e.valid)
  1263. continue;
  1264. if (e.port == port) {
  1265. fid = (i & 0x3ff) | (e.rvid & ~0x3ff);
  1266. mac = ether_addr_to_u64(&e.mac[0]);
  1267. pkey = rtl838x_hash(priv, mac << 12 | fid);
  1268. fid = (pkey & 0x3ff) | (fid & ~0x3ff);
  1269. pr_info("-> mac %016llx, fid: %d\n", mac, fid);
  1270. cb(e.mac, e.vid, e.is_static, data);
  1271. }
  1272. }
  1273. for (i = 0; i < 64; i++) {
  1274. priv->r->read_cam(i, &e);
  1275. if (!e.valid)
  1276. continue;
  1277. if (e.port == port)
  1278. cb(e.mac, e.vid, e.is_static, data);
  1279. }
  1280. mutex_unlock(&priv->reg_mutex);
  1281. return 0;
  1282. }
  1283. static int rtl838x_port_fdb_del(struct dsa_switch *ds, int port,
  1284. const unsigned char *addr, u16 vid)
  1285. {
  1286. struct rtl838x_switch_priv *priv = ds->priv;
  1287. u64 mac = ether_addr_to_u64(addr);
  1288. u32 key = rtl838x_hash_key(priv, mac, vid);
  1289. struct rtl838x_l2_entry e;
  1290. u32 r[3];
  1291. u64 entry;
  1292. int idx = -1, err = 0, i;
  1293. pr_info("In %s, mac %llx, vid: %d, key: %x\n", __func__, mac, vid, key);
  1294. mutex_lock(&priv->reg_mutex);
  1295. for (i = 0; i < 4; i++) {
  1296. entry = priv->r->read_l2_entry_using_hash(key, i, &e);
  1297. if (!e.valid)
  1298. continue;
  1299. if ((entry & 0x0fffffffffffffffULL) == ((mac << 12) | vid)) {
  1300. idx = (key << 2) | i;
  1301. break;
  1302. }
  1303. }
  1304. if (idx >= 0) {
  1305. r[0] = r[1] = r[2] = 0;
  1306. rtl838x_write_hash(idx, r);
  1307. goto out;
  1308. }
  1309. /* Check CAM for spillover from hash buckets */
  1310. for (i = 0; i < 64; i++) {
  1311. entry = priv->r->read_cam(i, &e);
  1312. if ((entry & 0x0fffffffffffffffULL) == ((mac << 12) | vid)) {
  1313. idx = i;
  1314. break;
  1315. }
  1316. }
  1317. if (idx >= 0) {
  1318. r[0] = r[1] = r[2] = 0;
  1319. rtl838x_write_cam(idx, r);
  1320. goto out;
  1321. }
  1322. err = -ENOENT;
  1323. out:
  1324. mutex_unlock(&priv->reg_mutex);
  1325. return err;
  1326. }
  1327. static int rtl838x_port_fdb_add(struct dsa_switch *ds, int port,
  1328. const unsigned char *addr, u16 vid)
  1329. {
  1330. struct rtl838x_switch_priv *priv = ds->priv;
  1331. u64 mac = ether_addr_to_u64(addr);
  1332. u32 key = rtl838x_hash_key(priv, mac, vid);
  1333. struct rtl838x_l2_entry e;
  1334. u32 r[3];
  1335. u64 entry;
  1336. int idx = -1, err = 0, i;
  1337. mutex_lock(&priv->reg_mutex);
  1338. for (i = 0; i < 4; i++) {
  1339. entry = priv->r->read_l2_entry_using_hash(key, i, &e);
  1340. if (!e.valid) {
  1341. idx = (key << 2) | i;
  1342. break;
  1343. }
  1344. if ((entry & 0x0fffffffffffffffULL) == ((mac << 12) | vid)) {
  1345. idx = (key << 2) | i;
  1346. break;
  1347. }
  1348. }
  1349. if (idx >= 0) {
  1350. r[0] = 3 << 17 | port << 12; // Aging and port
  1351. r[0] |= vid;
  1352. r[1] = mac >> 16;
  1353. r[2] = (mac & 0xffff) << 12; /* rvid = 0 */
  1354. rtl838x_write_hash(idx, r);
  1355. goto out;
  1356. }
  1357. /* Hash buckets full, try CAM */
  1358. for (i = 0; i < 64; i++) {
  1359. entry = rtl838x_read_cam(i, &e);
  1360. if (!e.valid) {
  1361. if (idx < 0) /* First empty entry? */
  1362. idx = i;
  1363. break;
  1364. } else if ((entry & 0x0fffffffffffffffULL) == ((mac << 12) | vid)) {
  1365. pr_debug("Found entry in CAM\n");
  1366. idx = i;
  1367. break;
  1368. }
  1369. }
  1370. if (idx >= 0) {
  1371. r[0] = 3 << 17 | port << 12; // Aging
  1372. r[0] |= vid;
  1373. r[1] = mac >> 16;
  1374. r[2] = (mac & 0xffff) << 12; /* rvid = 0 */
  1375. rtl838x_write_cam(idx, r);
  1376. goto out;
  1377. }
  1378. err = -ENOTSUPP;
  1379. out:
  1380. mutex_unlock(&priv->reg_mutex);
  1381. return err;
  1382. }
  1383. static void rtl838x_port_stp_state_set(struct dsa_switch *ds, int port,
  1384. u8 state)
  1385. {
  1386. u32 cmd, msti = 0;
  1387. u32 port_state[4];
  1388. int index, bit, i;
  1389. int pos = port;
  1390. struct rtl838x_switch_priv *priv = ds->priv;
  1391. int n = priv->family_id == RTL8380_FAMILY_ID ? 2 : 4;
  1392. pr_info("%s: port %d state %2x\n", __func__, port, state);
  1393. /* CPU PORT can only be configured on RTL838x */
  1394. if (port >= priv->cpu_port || port > 51)
  1395. return;
  1396. mutex_lock(&priv->reg_mutex);
  1397. /* For the RTL839x, the bits are left-aligned in the 128 bit field */
  1398. if (priv->family_id == RTL8390_FAMILY_ID)
  1399. pos += 12;
  1400. index = n - (pos >> 4) - 1;
  1401. bit = (pos << 1) % 32;
  1402. if (priv->family_id == RTL8380_FAMILY_ID) {
  1403. cmd = 1 << 15 /* Execute cmd */
  1404. | 1 << 14 /* Read */
  1405. | 2 << 12 /* Table type 0b10 */
  1406. | (msti & 0xfff);
  1407. } else {
  1408. cmd = 1 << 16 /* Execute cmd */
  1409. | 0 << 15 /* Read */
  1410. | 5 << 12 /* Table type 0b101 */
  1411. | (msti & 0xfff);
  1412. }
  1413. priv->r->exec_tbl0_cmd(cmd);
  1414. for (i = 0; i < n; i++)
  1415. port_state[i] = sw_r32(priv->r->tbl_access_data_0(i));
  1416. pr_debug("Current state, port %d: %d\n", port, (port_state[index] >> bit) & 3);
  1417. port_state[index] &= ~(3 << bit);
  1418. switch (state) {
  1419. case BR_STATE_DISABLED: /* 0 */
  1420. port_state[index] |= (0 << bit);
  1421. break;
  1422. case BR_STATE_BLOCKING: /* 4 */
  1423. case BR_STATE_LISTENING: /* 1 */
  1424. port_state[index] |= (1 << bit);
  1425. break;
  1426. case BR_STATE_LEARNING: /* 2 */
  1427. port_state[index] |= (2 << bit);
  1428. break;
  1429. case BR_STATE_FORWARDING: /* 3*/
  1430. port_state[index] |= (3 << bit);
  1431. default:
  1432. break;
  1433. }
  1434. if (priv->family_id == RTL8380_FAMILY_ID) {
  1435. cmd = 1 << 15 /* Execute cmd */
  1436. | 0 << 14 /* Write */
  1437. | 2 << 12 /* Table type 0b10 */
  1438. | (msti & 0xfff);
  1439. } else {
  1440. cmd = 1 << 16 /* Execute cmd */
  1441. | 1 << 15 /* Write */
  1442. | 5 << 12 /* Table type 0b101 */
  1443. | (msti & 0xfff);
  1444. }
  1445. for (i = 0; i < n; i++)
  1446. sw_w32(port_state[i], priv->r->tbl_access_data_0(i));
  1447. priv->r->exec_tbl0_cmd(cmd);
  1448. mutex_unlock(&priv->reg_mutex);
  1449. }
  1450. static int rtl838x_port_mirror_add(struct dsa_switch *ds, int port,
  1451. struct dsa_mall_mirror_tc_entry *mirror,
  1452. bool ingress)
  1453. {
  1454. /* We support 4 mirror groups, one destination port per group */
  1455. int group;
  1456. struct rtl838x_switch_priv *priv = ds->priv;
  1457. pr_info("In %s\n", __func__);
  1458. for (group = 0; group < 4; group++) {
  1459. if (priv->mirror_group_ports[group] == mirror->to_local_port)
  1460. break;
  1461. }
  1462. if (group >= 4) {
  1463. for (group = 0; group < 4; group++) {
  1464. if (priv->mirror_group_ports[group] < 0)
  1465. break;
  1466. }
  1467. }
  1468. if (group >= 4)
  1469. return -ENOSPC;
  1470. pr_debug("Using group %d\n", group);
  1471. mutex_lock(&priv->reg_mutex);
  1472. if (priv->family_id == RTL8380_FAMILY_ID) {
  1473. /* Enable mirroring to port across VLANs (bit 11) */
  1474. sw_w32(1 << 11 | (mirror->to_local_port << 4) | 1, RTL838X_MIR_CTRL(group));
  1475. } else {
  1476. /* Enable mirroring to destination port */
  1477. sw_w32((mirror->to_local_port << 4) | 1, RTL839X_MIR_CTRL(group));
  1478. }
  1479. if (ingress && (priv->r->get_port_reg_be(priv->r->mir_spm(group)) & (1ULL << port))) {
  1480. mutex_unlock(&priv->reg_mutex);
  1481. return -EEXIST;
  1482. }
  1483. if ((!ingress) && (priv->r->get_port_reg_be(priv->r->mir_dpm(group)) & (1ULL << port))) {
  1484. mutex_unlock(&priv->reg_mutex);
  1485. return -EEXIST;
  1486. }
  1487. if (ingress)
  1488. priv->r->mask_port_reg_be(0, 1ULL << port, priv->r->mir_spm(group));
  1489. else
  1490. priv->r->mask_port_reg_be(0, 1ULL << port, priv->r->mir_dpm(group));
  1491. priv->mirror_group_ports[group] = mirror->to_local_port;
  1492. mutex_unlock(&priv->reg_mutex);
  1493. return 0;
  1494. }
  1495. static void rtl838x_port_mirror_del(struct dsa_switch *ds, int port,
  1496. struct dsa_mall_mirror_tc_entry *mirror)
  1497. {
  1498. int group = 0;
  1499. struct rtl838x_switch_priv *priv = ds->priv;
  1500. pr_info("In %s\n", __func__);
  1501. for (group = 0; group < 4; group++) {
  1502. if (priv->mirror_group_ports[group] == mirror->to_local_port)
  1503. break;
  1504. }
  1505. if (group >= 4)
  1506. return;
  1507. mutex_lock(&priv->reg_mutex);
  1508. if (mirror->ingress) {
  1509. /* Ingress, clear source port matrix */
  1510. priv->r->mask_port_reg_be(1ULL << port, 0, priv->r->mir_spm(group));
  1511. } else {
  1512. /* Egress, clear destination port matrix */
  1513. priv->r->mask_port_reg_be(1ULL << port, 0, priv->r->mir_dpm(group));
  1514. }
  1515. if (!(sw_r32(priv->r->mir_spm(group)) || sw_r32(priv->r->mir_dpm(group)))) {
  1516. priv->mirror_group_ports[group] = -1;
  1517. sw_w32(0, priv->r->mir_ctrl(group));
  1518. }
  1519. mutex_unlock(&priv->reg_mutex);
  1520. }
  1521. void rtl838x_vlan_profile_dump(int index)
  1522. {
  1523. u32 profile;
  1524. if (index < 0 || index > 7)
  1525. return;
  1526. profile = sw_r32(rtl838x_vlan_profile(index));
  1527. pr_info("VLAN %d: L2 learning: %d, L2 Unknown MultiCast Field %x, \
  1528. IPv4 Unknown MultiCast Field %x, IPv6 Unknown MultiCast Field: %x",
  1529. index, profile & 1, (profile >> 1) & 0x1ff, (profile >> 10) & 0x1ff,
  1530. (profile >> 19) & 0x1ff);
  1531. }
  1532. void rtl839x_vlan_profile_dump(int index)
  1533. {
  1534. u32 profile, profile1;
  1535. if (index < 0 || index > 7)
  1536. return;
  1537. profile1 = sw_r32(rtl839x_vlan_profile(index) + 4);
  1538. profile = sw_r32(rtl839x_vlan_profile(index));
  1539. pr_info("VLAN %d: L2 learning: %d, L2 Unknown MultiCast Field %x, \
  1540. IPv4 Unknown MultiCast Field %x, IPv6 Unknown MultiCast Field: %x",
  1541. index, profile & 1, (profile >> 1) & 0xfff, (profile >> 13) & 0xfff,
  1542. (profile1) & 0xfff);
  1543. }
  1544. static int rtl838x_vlan_filtering(struct dsa_switch *ds, int port,
  1545. bool vlan_filtering)
  1546. {
  1547. struct rtl838x_switch_priv *priv = ds->priv;
  1548. pr_info("%s: port %d\n", __func__, port);
  1549. mutex_lock(&priv->reg_mutex);
  1550. if (vlan_filtering) {
  1551. /* Enable ingress and egress filtering */
  1552. if (port != priv->cpu_port)
  1553. sw_w32_mask(0b10 << ((port % 16) << 1), 0b01 << ((port % 16) << 1),
  1554. priv->r->vlan_port_igr_filter(port));
  1555. sw_w32_mask(0, 1 << (port % 32), priv->r->vlan_port_egr_filter(port));
  1556. } else {
  1557. /* Disable ingress and egress filtering */
  1558. if (port != priv->cpu_port)
  1559. sw_w32_mask(0b11 << ((port % 16) << 1), 0,
  1560. priv->r->vlan_port_igr_filter(port));
  1561. sw_w32_mask(1 << (port % 32), 0, priv->r->vlan_port_egr_filter(port));
  1562. }
  1563. /* Do we need to do something to the CPU-Port, too? */
  1564. mutex_unlock(&priv->reg_mutex);
  1565. return 0;
  1566. }
  1567. static int rtl838x_vlan_prepare(struct dsa_switch *ds, int port,
  1568. const struct switchdev_obj_port_vlan *vlan)
  1569. {
  1570. struct rtl838x_vlan_info info;
  1571. struct rtl838x_switch_priv *priv = ds->priv;
  1572. pr_info("%s: port %d\n", __func__, port);
  1573. mutex_lock(&priv->reg_mutex);
  1574. if (priv->family_id == RTL8380_FAMILY_ID)
  1575. rtl839x_vlan_profile_dump(0);
  1576. else
  1577. rtl839x_vlan_profile_dump(0);
  1578. priv->r->vlan_tables_read(0, &info);
  1579. pr_info("Tagged ports %llx, untag %llx, prof %x, MC# %d, UC# %d, FID %x\n",
  1580. info.tagged_ports, info.untagged_ports, info.profile_id,
  1581. info.hash_mc, info.hash_uc, info.fid);
  1582. mutex_unlock(&priv->reg_mutex);
  1583. return 0;
  1584. }
  1585. static void rtl838x_vlan_add(struct dsa_switch *ds, int port,
  1586. const struct switchdev_obj_port_vlan *vlan)
  1587. {
  1588. struct rtl838x_vlan_info info;
  1589. struct rtl838x_switch_priv *priv = ds->priv;
  1590. int v;
  1591. u64 portmask;
  1592. pr_info("%s port %d, vid_end %d, vid_end %d, flags %x\n", __func__,
  1593. port, vlan->vid_begin, vlan->vid_end, vlan->flags);
  1594. if (vlan->vid_begin > 4095 || vlan->vid_end > 4095) {
  1595. dev_err(priv->dev, "VLAN out of range: %d - %d",
  1596. vlan->vid_begin, vlan->vid_end);
  1597. return;
  1598. }
  1599. mutex_lock(&priv->reg_mutex);
  1600. if (vlan->flags & BRIDGE_VLAN_INFO_PVID) {
  1601. for (v = vlan->vid_begin; v <= vlan->vid_end; v++) {
  1602. /* Set both inner and outer PVID of the port */
  1603. sw_w32((v << 16) | v, priv->r->vlan_port_pb(port));
  1604. }
  1605. }
  1606. if (vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED) {
  1607. for (v = vlan->vid_begin; v <= vlan->vid_end; v++) {
  1608. /* Get untagged port memberships of this vlan */
  1609. priv->r->vlan_tables_read(v, &info);
  1610. portmask = info.untagged_ports | (1 << port);
  1611. pr_debug("Untagged ports, VLAN %d: %llx\n", v, portmask);
  1612. priv->r->vlan_set_untagged(v, portmask);
  1613. }
  1614. } else {
  1615. for (v = vlan->vid_begin; v <= vlan->vid_end; v++) {
  1616. /* Get tagged port memberships of this vlan */
  1617. priv->r->vlan_tables_read(v, &info);
  1618. info.tagged_ports |= (1 << port);
  1619. pr_debug("Tagged ports, VLAN %d: %llx\n", v, info.tagged_ports);
  1620. priv->r->vlan_set_tagged(v, &info);
  1621. }
  1622. }
  1623. mutex_unlock(&priv->reg_mutex);
  1624. }
  1625. static int rtl838x_vlan_del(struct dsa_switch *ds, int port,
  1626. const struct switchdev_obj_port_vlan *vlan)
  1627. {
  1628. struct rtl838x_vlan_info info;
  1629. struct rtl838x_switch_priv *priv = ds->priv;
  1630. int v;
  1631. u64 portmask;
  1632. pr_info("%s: port %d, vid_end %d, vid_end %d, flags %x\n", __func__,
  1633. port, vlan->vid_begin, vlan->vid_end, vlan->flags);
  1634. if (vlan->vid_begin > 4095 || vlan->vid_end > 4095) {
  1635. dev_err(priv->dev, "VLAN out of range: %d - %d",
  1636. vlan->vid_begin, vlan->vid_end);
  1637. return -ENOTSUPP;
  1638. }
  1639. mutex_lock(&priv->reg_mutex);
  1640. for (v = vlan->vid_begin; v <= vlan->vid_end; v++) {
  1641. /* Reset both inner and out PVID of the port */
  1642. sw_w32(0, priv->r->vlan_port_pb(port));
  1643. if (vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED) {
  1644. /* Get untagged port memberships of this vlan */
  1645. priv->r->vlan_tables_read(v, &info);
  1646. portmask = info.untagged_ports & (~(1ULL << port));
  1647. pr_info("Untagged ports, VLAN %d: %llx\n", v, portmask);
  1648. priv->r->vlan_set_untagged(v, portmask);
  1649. }
  1650. /* Get tagged port memberships of this vlan */
  1651. priv->r->vlan_tables_read(v, &info);
  1652. info.tagged_ports &= (~(1ULL << port));
  1653. pr_info("Tagged ports, VLAN %d: %llx\n", v, info.tagged_ports);
  1654. priv->r->vlan_set_tagged(v, &info);
  1655. }
  1656. mutex_unlock(&priv->reg_mutex);
  1657. return 0;
  1658. }
  1659. static void rtl838x_port_bridge_leave(struct dsa_switch *ds, int port,
  1660. struct net_device *bridge)
  1661. {
  1662. struct rtl838x_switch_priv *priv = ds->priv;
  1663. u64 port_bitmap = 1ULL << priv->cpu_port;
  1664. int i;
  1665. pr_info("%s %x: %d", __func__, (u32)priv, port);
  1666. mutex_lock(&priv->reg_mutex);
  1667. for (i = 0; i < ds->num_ports; i++) {
  1668. /* Remove this port from the port matrix of the other ports
  1669. * in the same bridge. If the port is disabled, port matrix
  1670. * is kept and not being setup until the port becomes enabled.
  1671. * And the other port's port matrix cannot be broken when the
  1672. * other port is still a VLAN-aware port.
  1673. */
  1674. if (dsa_is_user_port(ds, i) && i != port) {
  1675. if (dsa_to_port(ds, i)->bridge_dev != bridge)
  1676. continue;
  1677. if (priv->ports[i].enable)
  1678. priv->r->mask_port_reg_be(1ULL << port, 0,
  1679. priv->r->port_iso_ctrl(i));
  1680. priv->ports[i].pm |= 1ULL << port;
  1681. port_bitmap &= ~(1ULL << i);
  1682. }
  1683. }
  1684. /* Add all other ports to this port matrix. */
  1685. if (priv->ports[port].enable)
  1686. priv->r->mask_port_reg_be(0, port_bitmap, priv->r->port_iso_ctrl(port));
  1687. priv->ports[port].pm &= ~port_bitmap;
  1688. mutex_unlock(&priv->reg_mutex);
  1689. }
  1690. static int rtl838x_port_bridge_join(struct dsa_switch *ds, int port,
  1691. struct net_device *bridge)
  1692. {
  1693. struct rtl838x_switch_priv *priv = ds->priv;
  1694. u64 port_bitmap = 1ULL << priv->cpu_port;
  1695. int i;
  1696. pr_info("%s %x: %d %llx", __func__, (u32)priv, port, port_bitmap);
  1697. mutex_lock(&priv->reg_mutex);
  1698. for (i = 0; i < ds->num_ports; i++) {
  1699. /* Add this port to the port matrix of the other ports in the
  1700. * same bridge. If the port is disabled, port matrix is kept
  1701. * and not being setup until the port becomes enabled.
  1702. */
  1703. if (dsa_is_user_port(ds, i) && i != port) {
  1704. if (dsa_to_port(ds, i)->bridge_dev != bridge)
  1705. continue;
  1706. if (priv->ports[i].enable)
  1707. priv->r->mask_port_reg_be(0, 1ULL << port,
  1708. priv->r->port_iso_ctrl(i));
  1709. priv->ports[i].pm |= 1ULL << port;
  1710. port_bitmap |= 1ULL << i;
  1711. }
  1712. }
  1713. /* Add all other ports to this port matrix. */
  1714. if (priv->ports[port].enable) {
  1715. priv->r->mask_port_reg_be(0, 1ULL << port,
  1716. priv->r->port_iso_ctrl(priv->cpu_port));
  1717. priv->r->mask_port_reg_be(0, port_bitmap,
  1718. priv->r->port_iso_ctrl(port));
  1719. }
  1720. priv->ports[port].pm |= port_bitmap;
  1721. mutex_unlock(&priv->reg_mutex);
  1722. return 0;
  1723. }
  1724. static int rtl838x_port_enable(struct dsa_switch *ds, int port,
  1725. struct phy_device *phydev)
  1726. {
  1727. struct rtl838x_switch_priv *priv = ds->priv;
  1728. pr_info("%s: %x %d", __func__, (u32) priv, port);
  1729. priv->ports[port].enable = true;
  1730. if (dsa_is_cpu_port(ds, port))
  1731. return 0;
  1732. /* add port to switch mask of CPU_PORT */
  1733. priv->r->mask_port_reg_be(0, 1ULL << port, priv->r->port_iso_ctrl(priv->cpu_port));
  1734. /* add all other ports in the same bridge to switch mask of port */
  1735. priv->r->mask_port_reg_be(0, priv->ports[port].pm, priv->r->port_iso_ctrl(port));
  1736. return 0;
  1737. }
  1738. static void rtl838x_port_disable(struct dsa_switch *ds, int port)
  1739. {
  1740. struct rtl838x_switch_priv *priv = ds->priv;
  1741. pr_info("%s %x: %d", __func__, (u32)priv, port);
  1742. /* you can only disable user ports */
  1743. if (!dsa_is_user_port(ds, port))
  1744. return;
  1745. /* remove port from switch mask of CPU_PORT */
  1746. priv->r->mask_port_reg_be(1ULL << port, 0, priv->r->port_iso_ctrl(priv->cpu_port));
  1747. /* remove all other ports in the same bridge from switch mask of port */
  1748. priv->r->mask_port_reg_be(priv->ports[port].pm, 0LL, priv->r->port_iso_ctrl(port));
  1749. priv->ports[port].enable = false;
  1750. }
  1751. static int rtl838x_get_mac_eee(struct dsa_switch *ds, int port,
  1752. struct ethtool_eee *e)
  1753. {
  1754. struct rtl838x_switch_priv *priv = ds->priv;
  1755. pr_info("%s: port %d", __func__, port);
  1756. e->supported = SUPPORTED_100baseT_Full | SUPPORTED_1000baseT_Full;
  1757. if (sw_r32(priv->r->mac_force_mode_ctrl(port)) & (1 << 9))
  1758. e->advertised |= ADVERTISED_100baseT_Full;
  1759. if (sw_r32(priv->r->mac_force_mode_ctrl(port)) & (1 << 10))
  1760. e->advertised |= ADVERTISED_1000baseT_Full;
  1761. e->eee_enabled = priv->ports[port].eee_enabled;
  1762. pr_info("enabled: %d, active %x\n", e->eee_enabled, e->advertised);
  1763. if (sw_r32(RTL838X_MAC_EEE_ABLTY) & (1 << port)) {
  1764. e->lp_advertised = ADVERTISED_100baseT_Full;
  1765. e->lp_advertised |= ADVERTISED_1000baseT_Full;
  1766. }
  1767. e->eee_active = !!(e->advertised & e->lp_advertised);
  1768. pr_info("active: %d, lp %x\n", e->eee_active, e->lp_advertised);
  1769. return 0;
  1770. }
  1771. static int rtl838x_set_mac_eee(struct dsa_switch *ds, int port,
  1772. struct ethtool_eee *e)
  1773. {
  1774. struct rtl838x_switch_priv *priv = ds->priv;
  1775. pr_info("%s: port %d", __func__, port);
  1776. if (e->eee_enabled) {
  1777. pr_info("Globally enabling EEE\n");
  1778. sw_w32_mask(0x4, 0, RTL838X_SMI_GLB_CTRL);
  1779. }
  1780. if (e->eee_enabled) {
  1781. pr_info("Enabling EEE for MAC %d\n", port);
  1782. sw_w32_mask(0, 3 << 9, priv->r->mac_force_mode_ctrl(port));
  1783. sw_w32_mask(0, 1 << port, RTL838X_EEE_PORT_TX_EN);
  1784. sw_w32_mask(0, 1 << port, RTL838X_EEE_PORT_RX_EN);
  1785. priv->ports[port].eee_enabled = true;
  1786. e->eee_enabled = true;
  1787. } else {
  1788. pr_info("Disabling EEE for MAC %d\n", port);
  1789. sw_w32_mask(3 << 9, 0, priv->r->mac_force_mode_ctrl(port));
  1790. sw_w32_mask(1 << port, 0, RTL838X_EEE_PORT_TX_EN);
  1791. sw_w32_mask(1 << port, 0, RTL838X_EEE_PORT_RX_EN);
  1792. priv->ports[port].eee_enabled = false;
  1793. e->eee_enabled = false;
  1794. }
  1795. return 0;
  1796. }
  1797. static void rtl838x_phylink_mac_config(struct dsa_switch *ds, int port,
  1798. unsigned int mode,
  1799. const struct phylink_link_state *state)
  1800. {
  1801. struct rtl838x_switch_priv *priv = ds->priv;
  1802. u32 reg;
  1803. int speed_bit = priv->family_id == RTL8380_FAMILY_ID ? 4 : 3;
  1804. pr_info("%s port %d, mode %x\n", __func__, port, mode);
  1805. if (port == priv->cpu_port) {
  1806. /* Set Speed, duplex, flow control
  1807. * FORCE_EN | LINK_EN | NWAY_EN | DUP_SEL
  1808. * | SPD_SEL = 0b10 | FORCE_FC_EN | PHY_MASTER_SLV_MANUAL_EN
  1809. * | MEDIA_SEL
  1810. */
  1811. if (priv->family_id == RTL8380_FAMILY_ID) {
  1812. sw_w32(0x6192F, priv->r->mac_force_mode_ctrl(priv->cpu_port));
  1813. /* allow CRC errors on CPU-port */
  1814. sw_w32_mask(0, 0x8, RTL838X_MAC_PORT_CTRL(priv->cpu_port));
  1815. } else {
  1816. sw_w32_mask(0, 3, priv->r->mac_force_mode_ctrl(priv->cpu_port));
  1817. }
  1818. return;
  1819. }
  1820. reg = sw_r32(priv->r->mac_force_mode_ctrl(port));
  1821. /* Auto-Negotiation does not work for MAC in RTL8390 */
  1822. if (priv->family_id == RTL8380_FAMILY_ID) {
  1823. if (mode == MLO_AN_PHY) {
  1824. pr_info("PHY autonegotiates\n");
  1825. reg |= 1 << 2;
  1826. sw_w32(reg, priv->r->mac_force_mode_ctrl(port));
  1827. return;
  1828. }
  1829. }
  1830. if (mode != MLO_AN_FIXED)
  1831. pr_info("Fixed state.\n");
  1832. if (priv->family_id == RTL8380_FAMILY_ID) {
  1833. /* Clear id_mode_dis bit, and the existing port mode, let
  1834. * RGMII_MODE_EN bet set by mac_link_{up,down}
  1835. */
  1836. reg &= ~(RX_PAUSE_EN | TX_PAUSE_EN);
  1837. if (state->pause & MLO_PAUSE_TXRX_MASK) {
  1838. if (state->pause & MLO_PAUSE_TX)
  1839. reg |= TX_PAUSE_EN;
  1840. reg |= RX_PAUSE_EN;
  1841. }
  1842. }
  1843. reg &= ~(3 << speed_bit);
  1844. switch (state->speed) {
  1845. case SPEED_1000:
  1846. reg |= 2 << speed_bit;
  1847. break;
  1848. case SPEED_100:
  1849. reg |= 1 << speed_bit;
  1850. break;
  1851. }
  1852. if (priv->family_id == RTL8380_FAMILY_ID) {
  1853. reg &= ~(DUPLEX_FULL | FORCE_LINK_EN);
  1854. if (state->link)
  1855. reg |= FORCE_LINK_EN;
  1856. if (state->duplex == DUPLEX_FULL)
  1857. reg |= DUPLX_MODE;
  1858. }
  1859. // Disable AN
  1860. if (priv->family_id == RTL8380_FAMILY_ID)
  1861. reg &= ~(1 << 2);
  1862. sw_w32(reg, priv->r->mac_force_mode_ctrl(port));
  1863. }
  1864. static void rtl838x_phylink_mac_link_down(struct dsa_switch *ds, int port,
  1865. unsigned int mode,
  1866. phy_interface_t interface)
  1867. {
  1868. struct rtl838x_switch_priv *priv = ds->priv;
  1869. /* Stop TX/RX to port */
  1870. sw_w32_mask(0x3, 0, priv->r->mac_port_ctrl(port));
  1871. }
  1872. static void rtl838x_phylink_mac_link_up(struct dsa_switch *ds, int port,
  1873. unsigned int mode,
  1874. phy_interface_t interface,
  1875. struct phy_device *phydev)
  1876. {
  1877. struct rtl838x_switch_priv *priv = ds->priv;
  1878. /* Restart TX/RX to port */
  1879. sw_w32_mask(0, 0x3, priv->r->mac_port_ctrl(port));
  1880. }
  1881. static void rtl838x_phylink_validate(struct dsa_switch *ds, int port,
  1882. unsigned long *supported,
  1883. struct phylink_link_state *state)
  1884. {
  1885. struct rtl838x_switch_priv *priv = ds->priv;
  1886. __ETHTOOL_DECLARE_LINK_MODE_MASK(mask) = { 0, };
  1887. pr_info("In %s port %d", __func__, port);
  1888. if (!phy_interface_mode_is_rgmii(state->interface) &&
  1889. state->interface != PHY_INTERFACE_MODE_1000BASEX &&
  1890. state->interface != PHY_INTERFACE_MODE_MII &&
  1891. state->interface != PHY_INTERFACE_MODE_REVMII &&
  1892. state->interface != PHY_INTERFACE_MODE_GMII &&
  1893. state->interface != PHY_INTERFACE_MODE_QSGMII &&
  1894. state->interface != PHY_INTERFACE_MODE_INTERNAL &&
  1895. state->interface != PHY_INTERFACE_MODE_SGMII) {
  1896. bitmap_zero(supported, __ETHTOOL_LINK_MODE_MASK_NBITS);
  1897. dev_err(ds->dev,
  1898. "Unsupported interface: %d for port %d\n",
  1899. state->interface, port);
  1900. return;
  1901. }
  1902. /* Allow all the expected bits */
  1903. phylink_set(mask, Autoneg);
  1904. phylink_set_port_modes(mask);
  1905. phylink_set(mask, Pause);
  1906. phylink_set(mask, Asym_Pause);
  1907. /* With the exclusion of MII and Reverse MII, we support Gigabit,
  1908. * including Half duplex
  1909. */
  1910. if (state->interface != PHY_INTERFACE_MODE_MII &&
  1911. state->interface != PHY_INTERFACE_MODE_REVMII) {
  1912. phylink_set(mask, 1000baseT_Full);
  1913. phylink_set(mask, 1000baseT_Half);
  1914. }
  1915. /* On both the 8380 and 8382, ports 24-27 are SFP ports */
  1916. if (port >= 24 && port <= 27 && priv->family_id == RTL8380_FAMILY_ID)
  1917. phylink_set(mask, 1000baseX_Full);
  1918. phylink_set(mask, 10baseT_Half);
  1919. phylink_set(mask, 10baseT_Full);
  1920. phylink_set(mask, 100baseT_Half);
  1921. phylink_set(mask, 100baseT_Full);
  1922. bitmap_and(supported, supported, mask,
  1923. __ETHTOOL_LINK_MODE_MASK_NBITS);
  1924. bitmap_and(state->advertising, state->advertising, mask,
  1925. __ETHTOOL_LINK_MODE_MASK_NBITS);
  1926. }
  1927. static int rtl838x_phylink_mac_link_state(struct dsa_switch *ds, int port,
  1928. struct phylink_link_state *state)
  1929. {
  1930. struct rtl838x_switch_priv *priv = ds->priv;
  1931. u64 speed;
  1932. if (port < 0 || port > priv->cpu_port)
  1933. return -EINVAL;
  1934. state->link = 0;
  1935. if (priv->r->get_port_reg_le(priv->r->mac_link_sts) & (1ULL << port))
  1936. state->link = 1;
  1937. state->duplex = 0;
  1938. if (priv->r->get_port_reg_le(priv->r->mac_link_dup_sts) & (1ULL << port))
  1939. state->duplex = 1;
  1940. speed = priv->r->get_port_reg_le(priv->r->mac_link_spd_sts(port));
  1941. speed >>= (port % 16) << 1;
  1942. switch (speed & 0x3) {
  1943. case 0:
  1944. state->speed = SPEED_10;
  1945. break;
  1946. case 1:
  1947. state->speed = SPEED_100;
  1948. break;
  1949. case 2:
  1950. state->speed = SPEED_1000;
  1951. break;
  1952. case 3:
  1953. if (port == 24 || port == 26) /* Internal serdes */
  1954. state->speed = SPEED_2500;
  1955. else
  1956. state->speed = SPEED_100; /* Is in fact 500Mbit */
  1957. }
  1958. state->pause &= (MLO_PAUSE_RX | MLO_PAUSE_TX);
  1959. if (priv->r->get_port_reg_le(priv->r->mac_rx_pause_sts) & (1ULL << port))
  1960. state->pause |= MLO_PAUSE_RX;
  1961. if (priv->r->get_port_reg_le(priv->r->mac_tx_pause_sts) & (1ULL << port))
  1962. state->pause |= MLO_PAUSE_TX;
  1963. return 1;
  1964. }
  1965. static int rtl838x_mdio_probe(struct rtl838x_switch_priv *priv)
  1966. {
  1967. struct device *dev = priv->dev;
  1968. struct device_node *dn, *mii_np = dev->of_node;
  1969. struct mii_bus *bus;
  1970. int ret;
  1971. u32 pn;
  1972. pr_info("In %s\n", __func__);
  1973. mii_np = of_find_compatible_node(NULL, NULL, "realtek,rtl838x-mdio");
  1974. if (mii_np) {
  1975. pr_info("Found compatible MDIO node!\n");
  1976. } else {
  1977. dev_err(priv->dev, "no %s child node found", "mdio-bus");
  1978. return -ENODEV;
  1979. }
  1980. priv->mii_bus = of_mdio_find_bus(mii_np);
  1981. if (!priv->mii_bus) {
  1982. pr_info("Deferring probe of mdio bus\n");
  1983. return -EPROBE_DEFER;
  1984. }
  1985. if (!of_device_is_available(mii_np))
  1986. ret = -ENODEV;
  1987. bus = devm_mdiobus_alloc(priv->ds->dev);
  1988. if (!bus)
  1989. return -ENOMEM;
  1990. bus->name = "rtl838x slave mii";
  1991. bus->read = &rtl838x_mdio_read;
  1992. bus->write = &rtl838x_mdio_write;
  1993. snprintf(bus->id, MII_BUS_ID_SIZE, "%s-%d", bus->name, dev->id);
  1994. bus->parent = dev;
  1995. priv->ds->slave_mii_bus = bus;
  1996. priv->ds->slave_mii_bus->priv = priv;
  1997. ret = mdiobus_register(priv->ds->slave_mii_bus);
  1998. if (ret && mii_np) {
  1999. of_node_put(dn);
  2000. return ret;
  2001. }
  2002. dn = mii_np;
  2003. for_each_node_by_name(dn, "ethernet-phy") {
  2004. if (of_property_read_u32(dn, "reg", &pn))
  2005. continue;
  2006. // Check for the integrated SerDes of the RTL8380M first
  2007. if (of_property_read_bool(dn, "phy-is-integrated")
  2008. && priv->id == 0x8380 && pn >= 24) {
  2009. pr_info("----> FÓUND A SERDES\n");
  2010. priv->ports[pn].phy = PHY_RTL838X_SDS;
  2011. continue;
  2012. }
  2013. if (of_property_read_bool(dn, "phy-is-integrated")
  2014. && !of_property_read_bool(dn, "sfp")) {
  2015. priv->ports[pn].phy = PHY_RTL8218B_INT;
  2016. continue;
  2017. }
  2018. if (!of_property_read_bool(dn, "phy-is-integrated")
  2019. && of_property_read_bool(dn, "sfp")) {
  2020. priv->ports[pn].phy = PHY_RTL8214FC;
  2021. continue;
  2022. }
  2023. if (!of_property_read_bool(dn, "phy-is-integrated")
  2024. && !of_property_read_bool(dn, "sfp")) {
  2025. priv->ports[pn].phy = PHY_RTL8218B_EXT;
  2026. continue;
  2027. }
  2028. }
  2029. /* Disable MAC polling the PHY so that we can start configuration */
  2030. priv->r->set_port_reg_le(0ULL, priv->r->smi_poll_ctrl);
  2031. /* Enable PHY control via SoC */
  2032. if (priv->family_id == RTL8380_FAMILY_ID) {
  2033. /* Enable PHY control via SoC */
  2034. sw_w32_mask(0, 1 << 15, RTL838X_SMI_GLB_CTRL);
  2035. } else {
  2036. /* Disable PHY polling via SoC */
  2037. sw_w32_mask(1 << 7, 0, RTL839X_SMI_GLB_CTRL);
  2038. }
  2039. /* Power on fibre ports and reset them if necessary */
  2040. if (priv->ports[24].phy == PHY_RTL838X_SDS) {
  2041. pr_info("Powering on fibre ports & reset\n");
  2042. rtl8380_sds_power(24, 1);
  2043. rtl8380_sds_power(26, 1);
  2044. }
  2045. pr_info("%s done\n", __func__);
  2046. return 0;
  2047. }
  2048. static const struct dsa_switch_ops rtl838x_switch_ops = {
  2049. .get_tag_protocol = rtl838x_get_tag_protocol,
  2050. .setup = rtl838x_setup,
  2051. .port_vlan_filtering = rtl838x_vlan_filtering,
  2052. .port_vlan_prepare = rtl838x_vlan_prepare,
  2053. .port_vlan_add = rtl838x_vlan_add,
  2054. .port_vlan_del = rtl838x_vlan_del,
  2055. .port_bridge_join = rtl838x_port_bridge_join,
  2056. .port_bridge_leave = rtl838x_port_bridge_leave,
  2057. .port_stp_state_set = rtl838x_port_stp_state_set,
  2058. .set_ageing_time = rtl838x_set_l2aging,
  2059. .port_fast_age = rtl838x_fast_age,
  2060. .port_fdb_add = rtl838x_port_fdb_add,
  2061. .port_fdb_del = rtl838x_port_fdb_del,
  2062. .port_fdb_dump = rtl838x_port_fdb_dump,
  2063. .port_enable = rtl838x_port_enable,
  2064. .port_disable = rtl838x_port_disable,
  2065. .port_mirror_add = rtl838x_port_mirror_add,
  2066. .port_mirror_del = rtl838x_port_mirror_del,
  2067. .phy_read = dsa_phy_read,
  2068. .phy_write = dsa_phy_write,
  2069. .get_strings = rtl838x_get_strings,
  2070. .get_ethtool_stats = rtl838x_get_ethtool_stats,
  2071. .get_sset_count = rtl838x_get_sset_count,
  2072. .phylink_validate = rtl838x_phylink_validate,
  2073. .phylink_mac_link_state = rtl838x_phylink_mac_link_state,
  2074. .phylink_mac_config = rtl838x_phylink_mac_config,
  2075. .phylink_mac_link_down = rtl838x_phylink_mac_link_down,
  2076. .phylink_mac_link_up = rtl838x_phylink_mac_link_up,
  2077. .set_mac_eee = rtl838x_set_mac_eee,
  2078. .get_mac_eee = rtl838x_get_mac_eee,
  2079. };
  2080. static int __init rtl838x_sw_probe(struct platform_device *pdev)
  2081. {
  2082. int err = 0, i;
  2083. struct rtl838x_switch_priv *priv;
  2084. struct device *dev = &pdev->dev;
  2085. u64 irq_mask;
  2086. pr_info("Probing RTL838X switch device\n");
  2087. if (!pdev->dev.of_node) {
  2088. dev_err(dev, "No DT found\n");
  2089. return -EINVAL;
  2090. }
  2091. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  2092. if (!priv)
  2093. return -ENOMEM;
  2094. priv->ds = dsa_switch_alloc(dev, DSA_MAX_PORTS);
  2095. if (!priv->ds)
  2096. return -ENOMEM;
  2097. priv->ds->dev = dev;
  2098. priv->ds->priv = priv;
  2099. priv->ds->ops = &rtl838x_switch_ops;
  2100. priv->dev = dev;
  2101. priv->family_id = soc_info.family;
  2102. priv->id = soc_info.id;
  2103. if (soc_info.family == RTL8380_FAMILY_ID) {
  2104. priv->cpu_port = RTL838X_CPU_PORT;
  2105. priv->port_mask = 0x1f;
  2106. priv->r = &rtl838x_reg;
  2107. priv->ds->num_ports = 30;
  2108. priv->fib_entries = 8192;
  2109. rtl8380_get_version(priv);
  2110. } else {
  2111. priv->cpu_port = RTL839X_CPU_PORT;
  2112. priv->port_mask = 0x3f;
  2113. priv->r = &rtl839x_reg;
  2114. priv->ds->num_ports = 53;
  2115. priv->fib_entries = 16384;
  2116. rtl8390_get_version(priv);
  2117. }
  2118. pr_info("Chip version %c\n", priv->version);
  2119. err = rtl838x_mdio_probe(priv);
  2120. if (err) {
  2121. /* Probing fails the 1st time because of missing ethernet driver
  2122. * initialization. Use this to disable traffic in case the bootloader left if on
  2123. */
  2124. return err;
  2125. }
  2126. err = dsa_register_switch(priv->ds);
  2127. if (err) {
  2128. dev_err(dev, "Error registering switch: %d\n", err);
  2129. return err;
  2130. }
  2131. /* Enable link and media change interrupts. Are the SERDES masks needed? */
  2132. sw_w32_mask(0, 3, priv->r->isr_glb_src);
  2133. /* ... for all ports */
  2134. irq_mask = soc_info.family == RTL8380_FAMILY_ID ? 0x0FFFFFFF : 0xFFFFFFFFFFFFFULL;
  2135. priv->r->set_port_reg_le(irq_mask, priv->r->isr_port_link_sts_chg);
  2136. priv->r->set_port_reg_le(irq_mask, priv->r->imr_port_link_sts_chg);
  2137. priv->link_state_irq = 20;
  2138. if (priv->family_id == RTL8380_FAMILY_ID) {
  2139. err = request_irq(priv->link_state_irq, rtl838x_switch_irq,
  2140. IRQF_SHARED, "rtl838x-link-state", priv->ds);
  2141. } else {
  2142. err = request_irq(priv->link_state_irq, rtl839x_switch_irq,
  2143. IRQF_SHARED, "rtl838x-link-state", priv->ds);
  2144. }
  2145. if (err) {
  2146. dev_err(dev, "Error setting up switch interrupt.\n");
  2147. /* Need to free allocated switch here */
  2148. }
  2149. /* Enable interrupts for switch */
  2150. sw_w32(0x1, priv->r->imr_glb);
  2151. rtl838x_get_l2aging(priv);
  2152. /* Clear all destination ports for mirror groups */
  2153. for (i = 0; i < 4; i++)
  2154. priv->mirror_group_ports[i] = -1;
  2155. return err;
  2156. }
  2157. static int rtl838x_sw_remove(struct platform_device *pdev)
  2158. {
  2159. pr_info("Removing platform driver for rtl838x-sw\n");
  2160. return 0;
  2161. }
  2162. static const struct of_device_id rtl838x_switch_of_ids[] = {
  2163. { .compatible = "realtek,rtl838x-switch"},
  2164. { /* sentinel */ }
  2165. };
  2166. MODULE_DEVICE_TABLE(of, rtl838x_switch_of_ids);
  2167. static struct platform_driver rtl838x_switch_driver = {
  2168. .probe = rtl838x_sw_probe,
  2169. .remove = rtl838x_sw_remove,
  2170. .driver = {
  2171. .name = "rtl838x-switch",
  2172. .pm = NULL,
  2173. .of_match_table = rtl838x_switch_of_ids,
  2174. },
  2175. };
  2176. module_platform_driver(rtl838x_switch_driver);
  2177. MODULE_AUTHOR("B. Koblitz");
  2178. MODULE_DESCRIPTION("RTL838X SoC Switch Driver");
  2179. MODULE_LICENSE("GPL");