b53_common.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526
  1. /*
  2. * B53 switch driver main logic
  3. *
  4. * Copyright (C) 2011-2013 Jonas Gorski <jogo@openwrt.org>
  5. *
  6. * Permission to use, copy, modify, and/or distribute this software for any
  7. * purpose with or without fee is hereby granted, provided that the above
  8. * copyright notice and this permission notice appear in all copies.
  9. *
  10. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  11. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  12. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  13. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  14. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  15. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  16. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  17. */
  18. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  19. #include <linux/delay.h>
  20. #include <linux/export.h>
  21. #include <linux/gpio.h>
  22. #include <linux/kernel.h>
  23. #include <linux/module.h>
  24. #include <linux/switch.h>
  25. #include <linux/platform_data/b53.h>
  26. #include "b53_regs.h"
  27. #include "b53_priv.h"
  28. /* buffer size needed for displaying all MIBs with max'd values */
  29. #define B53_BUF_SIZE 1188
  30. struct b53_mib_desc {
  31. u8 size;
  32. u8 offset;
  33. const char *name;
  34. };
  35. /* BCM5365 MIB counters */
  36. static const struct b53_mib_desc b53_mibs_65[] = {
  37. { 8, 0x00, "TxOctets" },
  38. { 4, 0x08, "TxDropPkts" },
  39. { 4, 0x10, "TxBroadcastPkts" },
  40. { 4, 0x14, "TxMulticastPkts" },
  41. { 4, 0x18, "TxUnicastPkts" },
  42. { 4, 0x1c, "TxCollisions" },
  43. { 4, 0x20, "TxSingleCollision" },
  44. { 4, 0x24, "TxMultipleCollision" },
  45. { 4, 0x28, "TxDeferredTransmit" },
  46. { 4, 0x2c, "TxLateCollision" },
  47. { 4, 0x30, "TxExcessiveCollision" },
  48. { 4, 0x38, "TxPausePkts" },
  49. { 8, 0x44, "RxOctets" },
  50. { 4, 0x4c, "RxUndersizePkts" },
  51. { 4, 0x50, "RxPausePkts" },
  52. { 4, 0x54, "Pkts64Octets" },
  53. { 4, 0x58, "Pkts65to127Octets" },
  54. { 4, 0x5c, "Pkts128to255Octets" },
  55. { 4, 0x60, "Pkts256to511Octets" },
  56. { 4, 0x64, "Pkts512to1023Octets" },
  57. { 4, 0x68, "Pkts1024to1522Octets" },
  58. { 4, 0x6c, "RxOversizePkts" },
  59. { 4, 0x70, "RxJabbers" },
  60. { 4, 0x74, "RxAlignmentErrors" },
  61. { 4, 0x78, "RxFCSErrors" },
  62. { 8, 0x7c, "RxGoodOctets" },
  63. { 4, 0x84, "RxDropPkts" },
  64. { 4, 0x88, "RxUnicastPkts" },
  65. { 4, 0x8c, "RxMulticastPkts" },
  66. { 4, 0x90, "RxBroadcastPkts" },
  67. { 4, 0x94, "RxSAChanges" },
  68. { 4, 0x98, "RxFragments" },
  69. { },
  70. };
  71. /* BCM63xx MIB counters */
  72. static const struct b53_mib_desc b53_mibs_63xx[] = {
  73. { 8, 0x00, "TxOctets" },
  74. { 4, 0x08, "TxDropPkts" },
  75. { 4, 0x0c, "TxQoSPkts" },
  76. { 4, 0x10, "TxBroadcastPkts" },
  77. { 4, 0x14, "TxMulticastPkts" },
  78. { 4, 0x18, "TxUnicastPkts" },
  79. { 4, 0x1c, "TxCollisions" },
  80. { 4, 0x20, "TxSingleCollision" },
  81. { 4, 0x24, "TxMultipleCollision" },
  82. { 4, 0x28, "TxDeferredTransmit" },
  83. { 4, 0x2c, "TxLateCollision" },
  84. { 4, 0x30, "TxExcessiveCollision" },
  85. { 4, 0x38, "TxPausePkts" },
  86. { 8, 0x3c, "TxQoSOctets" },
  87. { 8, 0x44, "RxOctets" },
  88. { 4, 0x4c, "RxUndersizePkts" },
  89. { 4, 0x50, "RxPausePkts" },
  90. { 4, 0x54, "Pkts64Octets" },
  91. { 4, 0x58, "Pkts65to127Octets" },
  92. { 4, 0x5c, "Pkts128to255Octets" },
  93. { 4, 0x60, "Pkts256to511Octets" },
  94. { 4, 0x64, "Pkts512to1023Octets" },
  95. { 4, 0x68, "Pkts1024to1522Octets" },
  96. { 4, 0x6c, "RxOversizePkts" },
  97. { 4, 0x70, "RxJabbers" },
  98. { 4, 0x74, "RxAlignmentErrors" },
  99. { 4, 0x78, "RxFCSErrors" },
  100. { 8, 0x7c, "RxGoodOctets" },
  101. { 4, 0x84, "RxDropPkts" },
  102. { 4, 0x88, "RxUnicastPkts" },
  103. { 4, 0x8c, "RxMulticastPkts" },
  104. { 4, 0x90, "RxBroadcastPkts" },
  105. { 4, 0x94, "RxSAChanges" },
  106. { 4, 0x98, "RxFragments" },
  107. { 4, 0xa0, "RxSymbolErrors" },
  108. { 4, 0xa4, "RxQoSPkts" },
  109. { 8, 0xa8, "RxQoSOctets" },
  110. { 4, 0xb0, "Pkts1523to2047Octets" },
  111. { 4, 0xb4, "Pkts2048to4095Octets" },
  112. { 4, 0xb8, "Pkts4096to8191Octets" },
  113. { 4, 0xbc, "Pkts8192to9728Octets" },
  114. { 4, 0xc0, "RxDiscarded" },
  115. { }
  116. };
  117. /* MIB counters */
  118. static const struct b53_mib_desc b53_mibs[] = {
  119. { 8, 0x00, "TxOctets" },
  120. { 4, 0x08, "TxDropPkts" },
  121. { 4, 0x10, "TxBroadcastPkts" },
  122. { 4, 0x14, "TxMulticastPkts" },
  123. { 4, 0x18, "TxUnicastPkts" },
  124. { 4, 0x1c, "TxCollisions" },
  125. { 4, 0x20, "TxSingleCollision" },
  126. { 4, 0x24, "TxMultipleCollision" },
  127. { 4, 0x28, "TxDeferredTransmit" },
  128. { 4, 0x2c, "TxLateCollision" },
  129. { 4, 0x30, "TxExcessiveCollision" },
  130. { 4, 0x38, "TxPausePkts" },
  131. { 8, 0x50, "RxOctets" },
  132. { 4, 0x58, "RxUndersizePkts" },
  133. { 4, 0x5c, "RxPausePkts" },
  134. { 4, 0x60, "Pkts64Octets" },
  135. { 4, 0x64, "Pkts65to127Octets" },
  136. { 4, 0x68, "Pkts128to255Octets" },
  137. { 4, 0x6c, "Pkts256to511Octets" },
  138. { 4, 0x70, "Pkts512to1023Octets" },
  139. { 4, 0x74, "Pkts1024to1522Octets" },
  140. { 4, 0x78, "RxOversizePkts" },
  141. { 4, 0x7c, "RxJabbers" },
  142. { 4, 0x80, "RxAlignmentErrors" },
  143. { 4, 0x84, "RxFCSErrors" },
  144. { 8, 0x88, "RxGoodOctets" },
  145. { 4, 0x90, "RxDropPkts" },
  146. { 4, 0x94, "RxUnicastPkts" },
  147. { 4, 0x98, "RxMulticastPkts" },
  148. { 4, 0x9c, "RxBroadcastPkts" },
  149. { 4, 0xa0, "RxSAChanges" },
  150. { 4, 0xa4, "RxFragments" },
  151. { 4, 0xa8, "RxJumboPkts" },
  152. { 4, 0xac, "RxSymbolErrors" },
  153. { 4, 0xc0, "RxDiscarded" },
  154. { }
  155. };
  156. static int b53_do_vlan_op(struct b53_device *dev, u8 op)
  157. {
  158. unsigned int i;
  159. b53_write8(dev, B53_ARLIO_PAGE, dev->vta_regs[0], VTA_START_CMD | op);
  160. for (i = 0; i < 10; i++) {
  161. u8 vta;
  162. b53_read8(dev, B53_ARLIO_PAGE, dev->vta_regs[0], &vta);
  163. if (!(vta & VTA_START_CMD))
  164. return 0;
  165. usleep_range(100, 200);
  166. }
  167. return -EIO;
  168. }
  169. static void b53_set_vlan_entry(struct b53_device *dev, u16 vid, u16 members,
  170. u16 untag)
  171. {
  172. if (is5325(dev)) {
  173. u32 entry = 0;
  174. if (members) {
  175. entry = ((untag & VA_UNTAG_MASK_25) << VA_UNTAG_S_25) |
  176. members;
  177. if (dev->core_rev >= 3)
  178. entry |= VA_VALID_25_R4 | vid << VA_VID_HIGH_S;
  179. else
  180. entry |= VA_VALID_25;
  181. }
  182. b53_write32(dev, B53_VLAN_PAGE, B53_VLAN_WRITE_25, entry);
  183. b53_write16(dev, B53_VLAN_PAGE, B53_VLAN_TABLE_ACCESS_25, vid |
  184. VTA_RW_STATE_WR | VTA_RW_OP_EN);
  185. } else if (is5365(dev)) {
  186. u16 entry = 0;
  187. if (members)
  188. entry = ((untag & VA_UNTAG_MASK_65) << VA_UNTAG_S_65) |
  189. members | VA_VALID_65;
  190. b53_write16(dev, B53_VLAN_PAGE, B53_VLAN_WRITE_65, entry);
  191. b53_write16(dev, B53_VLAN_PAGE, B53_VLAN_TABLE_ACCESS_65, vid |
  192. VTA_RW_STATE_WR | VTA_RW_OP_EN);
  193. } else {
  194. b53_write16(dev, B53_ARLIO_PAGE, dev->vta_regs[1], vid);
  195. b53_write32(dev, B53_ARLIO_PAGE, dev->vta_regs[2],
  196. (untag << VTE_UNTAG_S) | members);
  197. b53_do_vlan_op(dev, VTA_CMD_WRITE);
  198. }
  199. }
  200. void b53_set_forwarding(struct b53_device *dev, int enable)
  201. {
  202. u8 mgmt;
  203. b53_read8(dev, B53_CTRL_PAGE, B53_SWITCH_MODE, &mgmt);
  204. if (enable)
  205. mgmt |= SM_SW_FWD_EN;
  206. else
  207. mgmt &= ~SM_SW_FWD_EN;
  208. b53_write8(dev, B53_CTRL_PAGE, B53_SWITCH_MODE, mgmt);
  209. }
  210. static void b53_enable_vlan(struct b53_device *dev, int enable)
  211. {
  212. u8 mgmt, vc0, vc1, vc4 = 0, vc5;
  213. b53_read8(dev, B53_CTRL_PAGE, B53_SWITCH_MODE, &mgmt);
  214. b53_read8(dev, B53_VLAN_PAGE, B53_VLAN_CTRL0, &vc0);
  215. b53_read8(dev, B53_VLAN_PAGE, B53_VLAN_CTRL1, &vc1);
  216. if (is5325(dev) || is5365(dev)) {
  217. b53_read8(dev, B53_VLAN_PAGE, B53_VLAN_CTRL4_25, &vc4);
  218. b53_read8(dev, B53_VLAN_PAGE, B53_VLAN_CTRL5_25, &vc5);
  219. } else if (is63xx(dev)) {
  220. b53_read8(dev, B53_VLAN_PAGE, B53_VLAN_CTRL4_63XX, &vc4);
  221. b53_read8(dev, B53_VLAN_PAGE, B53_VLAN_CTRL5_63XX, &vc5);
  222. } else {
  223. b53_read8(dev, B53_VLAN_PAGE, B53_VLAN_CTRL4, &vc4);
  224. b53_read8(dev, B53_VLAN_PAGE, B53_VLAN_CTRL5, &vc5);
  225. }
  226. mgmt &= ~SM_SW_FWD_MODE;
  227. if (enable) {
  228. vc0 |= VC0_VLAN_EN | VC0_VID_CHK_EN | VC0_VID_HASH_VID;
  229. vc1 |= VC1_RX_MCST_UNTAG_EN | VC1_RX_MCST_FWD_EN;
  230. vc4 &= ~VC4_ING_VID_CHECK_MASK;
  231. vc4 |= VC4_ING_VID_VIO_DROP << VC4_ING_VID_CHECK_S;
  232. vc5 |= VC5_DROP_VTABLE_MISS;
  233. if (is5325(dev))
  234. vc0 &= ~VC0_RESERVED_1;
  235. if (is5325(dev) || is5365(dev))
  236. vc1 |= VC1_RX_MCST_TAG_EN;
  237. if (!is5325(dev) && !is5365(dev)) {
  238. if (dev->allow_vid_4095)
  239. vc5 |= VC5_VID_FFF_EN;
  240. else
  241. vc5 &= ~VC5_VID_FFF_EN;
  242. }
  243. } else {
  244. vc0 &= ~(VC0_VLAN_EN | VC0_VID_CHK_EN | VC0_VID_HASH_VID);
  245. vc1 &= ~(VC1_RX_MCST_UNTAG_EN | VC1_RX_MCST_FWD_EN);
  246. vc4 &= ~VC4_ING_VID_CHECK_MASK;
  247. vc5 &= ~VC5_DROP_VTABLE_MISS;
  248. if (is5325(dev) || is5365(dev))
  249. vc4 |= VC4_ING_VID_VIO_FWD << VC4_ING_VID_CHECK_S;
  250. else
  251. vc4 |= VC4_ING_VID_VIO_TO_IMP << VC4_ING_VID_CHECK_S;
  252. if (is5325(dev) || is5365(dev))
  253. vc1 &= ~VC1_RX_MCST_TAG_EN;
  254. if (!is5325(dev) && !is5365(dev))
  255. vc5 &= ~VC5_VID_FFF_EN;
  256. }
  257. b53_write8(dev, B53_VLAN_PAGE, B53_VLAN_CTRL0, vc0);
  258. b53_write8(dev, B53_VLAN_PAGE, B53_VLAN_CTRL1, vc1);
  259. if (is5325(dev) || is5365(dev)) {
  260. /* enable the high 8 bit vid check on 5325 */
  261. if (is5325(dev) && enable)
  262. b53_write8(dev, B53_VLAN_PAGE, B53_VLAN_CTRL3,
  263. VC3_HIGH_8BIT_EN);
  264. else
  265. b53_write8(dev, B53_VLAN_PAGE, B53_VLAN_CTRL3, 0);
  266. b53_write8(dev, B53_VLAN_PAGE, B53_VLAN_CTRL4_25, vc4);
  267. b53_write8(dev, B53_VLAN_PAGE, B53_VLAN_CTRL5_25, vc5);
  268. } else if (is63xx(dev)) {
  269. b53_write16(dev, B53_VLAN_PAGE, B53_VLAN_CTRL3_63XX, 0);
  270. b53_write8(dev, B53_VLAN_PAGE, B53_VLAN_CTRL4_63XX, vc4);
  271. b53_write8(dev, B53_VLAN_PAGE, B53_VLAN_CTRL5_63XX, vc5);
  272. } else {
  273. b53_write16(dev, B53_VLAN_PAGE, B53_VLAN_CTRL3, 0);
  274. b53_write8(dev, B53_VLAN_PAGE, B53_VLAN_CTRL4, vc4);
  275. b53_write8(dev, B53_VLAN_PAGE, B53_VLAN_CTRL5, vc5);
  276. }
  277. b53_write8(dev, B53_CTRL_PAGE, B53_SWITCH_MODE, mgmt);
  278. }
  279. static int b53_set_jumbo(struct b53_device *dev, int enable, int allow_10_100)
  280. {
  281. u32 port_mask = 0;
  282. u16 max_size = JMS_MIN_SIZE;
  283. if (is5325(dev) || is5365(dev))
  284. return -EINVAL;
  285. if (enable) {
  286. port_mask = dev->enabled_ports;
  287. max_size = JMS_MAX_SIZE;
  288. if (allow_10_100)
  289. port_mask |= JPM_10_100_JUMBO_EN;
  290. }
  291. b53_write32(dev, B53_JUMBO_PAGE, dev->jumbo_pm_reg, port_mask);
  292. return b53_write16(dev, B53_JUMBO_PAGE, dev->jumbo_size_reg, max_size);
  293. }
  294. static int b53_flush_arl(struct b53_device *dev)
  295. {
  296. unsigned int i;
  297. b53_write8(dev, B53_CTRL_PAGE, B53_FAST_AGE_CTRL,
  298. FAST_AGE_DONE | FAST_AGE_DYNAMIC | FAST_AGE_STATIC);
  299. for (i = 0; i < 10; i++) {
  300. u8 fast_age_ctrl;
  301. b53_read8(dev, B53_CTRL_PAGE, B53_FAST_AGE_CTRL,
  302. &fast_age_ctrl);
  303. if (!(fast_age_ctrl & FAST_AGE_DONE))
  304. return 0;
  305. mdelay(1);
  306. }
  307. pr_warn("time out while flushing ARL\n");
  308. return -EINVAL;
  309. }
  310. static void b53_enable_ports(struct b53_device *dev)
  311. {
  312. unsigned i;
  313. b53_for_each_port(dev, i) {
  314. u8 port_ctrl;
  315. u16 pvlan_mask;
  316. /*
  317. * prevent leaking packets between wan and lan in unmanaged
  318. * mode through port vlans.
  319. */
  320. if (dev->enable_vlan || is_cpu_port(dev, i))
  321. pvlan_mask = 0x1ff;
  322. else if (is531x5(dev) || is5301x(dev))
  323. /* BCM53115 may use a different port as cpu port */
  324. pvlan_mask = BIT(dev->sw_dev.cpu_port);
  325. else
  326. pvlan_mask = BIT(B53_CPU_PORT);
  327. /* BCM5325 CPU port is at 8 */
  328. if ((is5325(dev) || is5365(dev)) && i == B53_CPU_PORT_25)
  329. i = B53_CPU_PORT;
  330. if (dev->chip_id == BCM5398_DEVICE_ID && (i == 6 || i == 7))
  331. /* disable unused ports 6 & 7 */
  332. port_ctrl = PORT_CTRL_RX_DISABLE | PORT_CTRL_TX_DISABLE;
  333. else if (i == B53_CPU_PORT)
  334. port_ctrl = PORT_CTRL_RX_BCST_EN |
  335. PORT_CTRL_RX_MCST_EN |
  336. PORT_CTRL_RX_UCST_EN;
  337. else
  338. port_ctrl = 0;
  339. b53_write16(dev, B53_PVLAN_PAGE, B53_PVLAN_PORT_MASK(i),
  340. pvlan_mask);
  341. /* port state is handled by bcm63xx_enet driver */
  342. if (!is63xx(dev) && !(is5301x(dev) && i == 6))
  343. b53_write8(dev, B53_CTRL_PAGE, B53_PORT_CTRL(i),
  344. port_ctrl);
  345. }
  346. }
  347. static void b53_enable_mib(struct b53_device *dev)
  348. {
  349. u8 gc;
  350. b53_read8(dev, B53_MGMT_PAGE, B53_GLOBAL_CONFIG, &gc);
  351. gc &= ~(GC_RESET_MIB | GC_MIB_AC_EN);
  352. b53_write8(dev, B53_MGMT_PAGE, B53_GLOBAL_CONFIG, gc);
  353. }
  354. static int b53_apply(struct b53_device *dev)
  355. {
  356. int i;
  357. /* clear all vlan entries */
  358. if (is5325(dev) || is5365(dev)) {
  359. for (i = 1; i < dev->sw_dev.vlans; i++)
  360. b53_set_vlan_entry(dev, i, 0, 0);
  361. } else {
  362. b53_do_vlan_op(dev, VTA_CMD_CLEAR);
  363. }
  364. b53_enable_vlan(dev, dev->enable_vlan);
  365. /* fill VLAN table */
  366. if (dev->enable_vlan) {
  367. for (i = 0; i < dev->sw_dev.vlans; i++) {
  368. struct b53_vlan *vlan = &dev->vlans[i];
  369. if (!vlan->members)
  370. continue;
  371. b53_set_vlan_entry(dev, i, vlan->members, vlan->untag);
  372. }
  373. b53_for_each_port(dev, i)
  374. b53_write16(dev, B53_VLAN_PAGE,
  375. B53_VLAN_PORT_DEF_TAG(i),
  376. dev->ports[i].pvid);
  377. } else {
  378. b53_for_each_port(dev, i)
  379. b53_write16(dev, B53_VLAN_PAGE,
  380. B53_VLAN_PORT_DEF_TAG(i), 1);
  381. }
  382. b53_enable_ports(dev);
  383. if (!is5325(dev) && !is5365(dev))
  384. b53_set_jumbo(dev, dev->enable_jumbo, 1);
  385. return 0;
  386. }
  387. static void b53_switch_reset_gpio(struct b53_device *dev)
  388. {
  389. int gpio = dev->reset_gpio;
  390. if (gpio < 0)
  391. return;
  392. /*
  393. * Reset sequence: RESET low(50ms)->high(20ms)
  394. */
  395. gpio_set_value(gpio, 0);
  396. mdelay(50);
  397. gpio_set_value(gpio, 1);
  398. mdelay(20);
  399. dev->current_page = 0xff;
  400. }
  401. static int b53_switch_reset(struct b53_device *dev)
  402. {
  403. u8 cpu_port = dev->sw_dev.cpu_port;
  404. u8 mgmt;
  405. b53_switch_reset_gpio(dev);
  406. if (is539x(dev)) {
  407. b53_write8(dev, B53_CTRL_PAGE, B53_SOFTRESET, 0x83);
  408. b53_write8(dev, B53_CTRL_PAGE, B53_SOFTRESET, 0x00);
  409. }
  410. b53_read8(dev, B53_CTRL_PAGE, B53_SWITCH_MODE, &mgmt);
  411. if (!(mgmt & SM_SW_FWD_EN)) {
  412. mgmt &= ~SM_SW_FWD_MODE;
  413. mgmt |= SM_SW_FWD_EN;
  414. b53_write8(dev, B53_CTRL_PAGE, B53_SWITCH_MODE, mgmt);
  415. b53_read8(dev, B53_CTRL_PAGE, B53_SWITCH_MODE, &mgmt);
  416. if (!(mgmt & SM_SW_FWD_EN)) {
  417. pr_err("Failed to enable switch!\n");
  418. return -EINVAL;
  419. }
  420. }
  421. /* enable all ports */
  422. b53_enable_ports(dev);
  423. /* configure MII port if necessary */
  424. if (is5325(dev)) {
  425. u8 mii_port_override;
  426. b53_read8(dev, B53_CTRL_PAGE, B53_PORT_OVERRIDE_CTRL,
  427. &mii_port_override);
  428. /* reverse mii needs to be enabled */
  429. if (!(mii_port_override & PORT_OVERRIDE_RV_MII_25)) {
  430. b53_write8(dev, B53_CTRL_PAGE, B53_PORT_OVERRIDE_CTRL,
  431. mii_port_override | PORT_OVERRIDE_RV_MII_25);
  432. b53_read8(dev, B53_CTRL_PAGE, B53_PORT_OVERRIDE_CTRL,
  433. &mii_port_override);
  434. if (!(mii_port_override & PORT_OVERRIDE_RV_MII_25)) {
  435. pr_err("Failed to enable reverse MII mode\n");
  436. return -EINVAL;
  437. }
  438. }
  439. } else if (is531x5(dev) && cpu_port == B53_CPU_PORT) {
  440. u8 mii_port_override;
  441. b53_read8(dev, B53_CTRL_PAGE, B53_PORT_OVERRIDE_CTRL,
  442. &mii_port_override);
  443. b53_write8(dev, B53_CTRL_PAGE, B53_PORT_OVERRIDE_CTRL,
  444. mii_port_override | PORT_OVERRIDE_EN |
  445. PORT_OVERRIDE_LINK);
  446. /* BCM47189 has another interface connected to the port 5 */
  447. if (dev->enabled_ports & BIT(5)) {
  448. u8 po_reg = B53_GMII_PORT_OVERRIDE_CTRL(5);
  449. u8 gmii_po;
  450. b53_read8(dev, B53_CTRL_PAGE, po_reg, &gmii_po);
  451. gmii_po |= GMII_PO_LINK |
  452. GMII_PO_RX_FLOW |
  453. GMII_PO_TX_FLOW |
  454. GMII_PO_EN;
  455. b53_write8(dev, B53_CTRL_PAGE, po_reg, gmii_po);
  456. }
  457. } else if (is5301x(dev)) {
  458. if (cpu_port == 8) {
  459. u8 mii_port_override;
  460. b53_read8(dev, B53_CTRL_PAGE, B53_PORT_OVERRIDE_CTRL,
  461. &mii_port_override);
  462. mii_port_override |= PORT_OVERRIDE_LINK |
  463. PORT_OVERRIDE_RX_FLOW |
  464. PORT_OVERRIDE_TX_FLOW |
  465. PORT_OVERRIDE_SPEED_2000M |
  466. PORT_OVERRIDE_EN;
  467. b53_write8(dev, B53_CTRL_PAGE, B53_PORT_OVERRIDE_CTRL,
  468. mii_port_override);
  469. /* TODO: Ports 5 & 7 require some extra handling */
  470. } else {
  471. u8 po_reg = B53_GMII_PORT_OVERRIDE_CTRL(cpu_port);
  472. u8 gmii_po;
  473. b53_read8(dev, B53_CTRL_PAGE, po_reg, &gmii_po);
  474. gmii_po |= GMII_PO_LINK |
  475. GMII_PO_RX_FLOW |
  476. GMII_PO_TX_FLOW |
  477. GMII_PO_EN |
  478. GMII_PO_SPEED_2000M;
  479. b53_write8(dev, B53_CTRL_PAGE, po_reg, gmii_po);
  480. }
  481. }
  482. b53_enable_mib(dev);
  483. return b53_flush_arl(dev);
  484. }
  485. /*
  486. * Swconfig glue functions
  487. */
  488. static int b53_global_get_vlan_enable(struct switch_dev *dev,
  489. const struct switch_attr *attr,
  490. struct switch_val *val)
  491. {
  492. struct b53_device *priv = sw_to_b53(dev);
  493. val->value.i = priv->enable_vlan;
  494. return 0;
  495. }
  496. static int b53_global_set_vlan_enable(struct switch_dev *dev,
  497. const struct switch_attr *attr,
  498. struct switch_val *val)
  499. {
  500. struct b53_device *priv = sw_to_b53(dev);
  501. priv->enable_vlan = val->value.i;
  502. return 0;
  503. }
  504. static int b53_global_get_jumbo_enable(struct switch_dev *dev,
  505. const struct switch_attr *attr,
  506. struct switch_val *val)
  507. {
  508. struct b53_device *priv = sw_to_b53(dev);
  509. val->value.i = priv->enable_jumbo;
  510. return 0;
  511. }
  512. static int b53_global_set_jumbo_enable(struct switch_dev *dev,
  513. const struct switch_attr *attr,
  514. struct switch_val *val)
  515. {
  516. struct b53_device *priv = sw_to_b53(dev);
  517. priv->enable_jumbo = val->value.i;
  518. return 0;
  519. }
  520. static int b53_global_get_4095_enable(struct switch_dev *dev,
  521. const struct switch_attr *attr,
  522. struct switch_val *val)
  523. {
  524. struct b53_device *priv = sw_to_b53(dev);
  525. val->value.i = priv->allow_vid_4095;
  526. return 0;
  527. }
  528. static int b53_global_set_4095_enable(struct switch_dev *dev,
  529. const struct switch_attr *attr,
  530. struct switch_val *val)
  531. {
  532. struct b53_device *priv = sw_to_b53(dev);
  533. priv->allow_vid_4095 = val->value.i;
  534. return 0;
  535. }
  536. static int b53_global_get_ports(struct switch_dev *dev,
  537. const struct switch_attr *attr,
  538. struct switch_val *val)
  539. {
  540. struct b53_device *priv = sw_to_b53(dev);
  541. val->len = snprintf(priv->buf, B53_BUF_SIZE, "0x%04x",
  542. priv->enabled_ports);
  543. val->value.s = priv->buf;
  544. return 0;
  545. }
  546. static int b53_port_get_pvid(struct switch_dev *dev, int port, int *val)
  547. {
  548. struct b53_device *priv = sw_to_b53(dev);
  549. *val = priv->ports[port].pvid;
  550. return 0;
  551. }
  552. static int b53_port_set_pvid(struct switch_dev *dev, int port, int val)
  553. {
  554. struct b53_device *priv = sw_to_b53(dev);
  555. if (val > 15 && is5325(priv))
  556. return -EINVAL;
  557. if (val == 4095 && !priv->allow_vid_4095)
  558. return -EINVAL;
  559. priv->ports[port].pvid = val;
  560. return 0;
  561. }
  562. static int b53_vlan_get_ports(struct switch_dev *dev, struct switch_val *val)
  563. {
  564. struct b53_device *priv = sw_to_b53(dev);
  565. struct switch_port *port = &val->value.ports[0];
  566. struct b53_vlan *vlan = &priv->vlans[val->port_vlan];
  567. int i;
  568. val->len = 0;
  569. if (!vlan->members)
  570. return 0;
  571. for (i = 0; i < dev->ports; i++) {
  572. if (!(vlan->members & BIT(i)))
  573. continue;
  574. if (!(vlan->untag & BIT(i)))
  575. port->flags = BIT(SWITCH_PORT_FLAG_TAGGED);
  576. else
  577. port->flags = 0;
  578. port->id = i;
  579. val->len++;
  580. port++;
  581. }
  582. return 0;
  583. }
  584. static int b53_vlan_set_ports(struct switch_dev *dev, struct switch_val *val)
  585. {
  586. struct b53_device *priv = sw_to_b53(dev);
  587. struct switch_port *port;
  588. struct b53_vlan *vlan = &priv->vlans[val->port_vlan];
  589. int i;
  590. /* only BCM5325 and BCM5365 supports VID 0 */
  591. if (val->port_vlan == 0 && !is5325(priv) && !is5365(priv))
  592. return -EINVAL;
  593. /* VLAN 4095 needs special handling */
  594. if (val->port_vlan == 4095 && !priv->allow_vid_4095)
  595. return -EINVAL;
  596. port = &val->value.ports[0];
  597. vlan->members = 0;
  598. vlan->untag = 0;
  599. for (i = 0; i < val->len; i++, port++) {
  600. vlan->members |= BIT(port->id);
  601. if (!(port->flags & BIT(SWITCH_PORT_FLAG_TAGGED))) {
  602. vlan->untag |= BIT(port->id);
  603. priv->ports[port->id].pvid = val->port_vlan;
  604. };
  605. }
  606. /* ignore disabled ports */
  607. vlan->members &= priv->enabled_ports;
  608. vlan->untag &= priv->enabled_ports;
  609. return 0;
  610. }
  611. static int b53_port_get_link(struct switch_dev *dev, int port,
  612. struct switch_port_link *link)
  613. {
  614. struct b53_device *priv = sw_to_b53(dev);
  615. if (is_cpu_port(priv, port)) {
  616. link->link = 1;
  617. link->duplex = 1;
  618. link->speed = is5325(priv) || is5365(priv) ?
  619. SWITCH_PORT_SPEED_100 : SWITCH_PORT_SPEED_1000;
  620. link->aneg = 0;
  621. } else if (priv->enabled_ports & BIT(port)) {
  622. u32 speed;
  623. u16 lnk, duplex;
  624. b53_read16(priv, B53_STAT_PAGE, B53_LINK_STAT, &lnk);
  625. b53_read16(priv, B53_STAT_PAGE, priv->duplex_reg, &duplex);
  626. lnk = (lnk >> port) & 1;
  627. duplex = (duplex >> port) & 1;
  628. if (is5325(priv) || is5365(priv)) {
  629. u16 tmp;
  630. b53_read16(priv, B53_STAT_PAGE, B53_SPEED_STAT, &tmp);
  631. speed = SPEED_PORT_FE(tmp, port);
  632. } else {
  633. b53_read32(priv, B53_STAT_PAGE, B53_SPEED_STAT, &speed);
  634. speed = SPEED_PORT_GE(speed, port);
  635. }
  636. link->link = lnk;
  637. if (lnk) {
  638. link->duplex = duplex;
  639. switch (speed) {
  640. case SPEED_STAT_10M:
  641. link->speed = SWITCH_PORT_SPEED_10;
  642. break;
  643. case SPEED_STAT_100M:
  644. link->speed = SWITCH_PORT_SPEED_100;
  645. break;
  646. case SPEED_STAT_1000M:
  647. link->speed = SWITCH_PORT_SPEED_1000;
  648. break;
  649. }
  650. }
  651. link->aneg = 1;
  652. } else {
  653. link->link = 0;
  654. }
  655. return 0;
  656. }
  657. static int b53_port_set_link(struct switch_dev *sw_dev, int port,
  658. struct switch_port_link *link)
  659. {
  660. struct b53_device *dev = sw_to_b53(sw_dev);
  661. /*
  662. * TODO: BCM63XX requires special handling as it can have external phys
  663. * and ports might be GE or only FE
  664. */
  665. if (is63xx(dev))
  666. return -ENOTSUPP;
  667. if (port == sw_dev->cpu_port)
  668. return -EINVAL;
  669. if (!(BIT(port) & dev->enabled_ports))
  670. return -EINVAL;
  671. if (link->speed == SWITCH_PORT_SPEED_1000 &&
  672. (is5325(dev) || is5365(dev)))
  673. return -EINVAL;
  674. if (link->speed == SWITCH_PORT_SPEED_1000 && !link->duplex)
  675. return -EINVAL;
  676. return switch_generic_set_link(sw_dev, port, link);
  677. }
  678. static int b53_phy_read16(struct switch_dev *dev, int addr, u8 reg, u16 *value)
  679. {
  680. struct b53_device *priv = sw_to_b53(dev);
  681. if (priv->ops->phy_read16)
  682. return priv->ops->phy_read16(priv, addr, reg, value);
  683. return b53_read16(priv, B53_PORT_MII_PAGE(addr), reg, value);
  684. }
  685. static int b53_phy_write16(struct switch_dev *dev, int addr, u8 reg, u16 value)
  686. {
  687. struct b53_device *priv = sw_to_b53(dev);
  688. if (priv->ops->phy_write16)
  689. return priv->ops->phy_write16(priv, addr, reg, value);
  690. return b53_write16(priv, B53_PORT_MII_PAGE(addr), reg, value);
  691. }
  692. static int b53_global_reset_switch(struct switch_dev *dev)
  693. {
  694. struct b53_device *priv = sw_to_b53(dev);
  695. /* reset vlans */
  696. priv->enable_vlan = 0;
  697. priv->enable_jumbo = 0;
  698. priv->allow_vid_4095 = 0;
  699. memset(priv->vlans, 0, sizeof(*priv->vlans) * dev->vlans);
  700. memset(priv->ports, 0, sizeof(*priv->ports) * dev->ports);
  701. return b53_switch_reset(priv);
  702. }
  703. static int b53_global_apply_config(struct switch_dev *dev)
  704. {
  705. struct b53_device *priv = sw_to_b53(dev);
  706. /* disable switching */
  707. b53_set_forwarding(priv, 0);
  708. b53_apply(priv);
  709. /* enable switching */
  710. b53_set_forwarding(priv, 1);
  711. return 0;
  712. }
  713. static int b53_global_reset_mib(struct switch_dev *dev,
  714. const struct switch_attr *attr,
  715. struct switch_val *val)
  716. {
  717. struct b53_device *priv = sw_to_b53(dev);
  718. u8 gc;
  719. b53_read8(priv, B53_MGMT_PAGE, B53_GLOBAL_CONFIG, &gc);
  720. b53_write8(priv, B53_MGMT_PAGE, B53_GLOBAL_CONFIG, gc | GC_RESET_MIB);
  721. mdelay(1);
  722. b53_write8(priv, B53_MGMT_PAGE, B53_GLOBAL_CONFIG, gc & ~GC_RESET_MIB);
  723. mdelay(1);
  724. return 0;
  725. }
  726. static int b53_port_get_mib(struct switch_dev *sw_dev,
  727. const struct switch_attr *attr,
  728. struct switch_val *val)
  729. {
  730. struct b53_device *dev = sw_to_b53(sw_dev);
  731. const struct b53_mib_desc *mibs;
  732. int port = val->port_vlan;
  733. int len = 0;
  734. if (!(BIT(port) & dev->enabled_ports))
  735. return -1;
  736. if (is5365(dev)) {
  737. if (port == 5)
  738. port = 8;
  739. mibs = b53_mibs_65;
  740. } else if (is63xx(dev)) {
  741. mibs = b53_mibs_63xx;
  742. } else {
  743. mibs = b53_mibs;
  744. }
  745. dev->buf[0] = 0;
  746. for (; mibs->size > 0; mibs++) {
  747. u64 val;
  748. if (mibs->size == 8) {
  749. b53_read64(dev, B53_MIB_PAGE(port), mibs->offset, &val);
  750. } else {
  751. u32 val32;
  752. b53_read32(dev, B53_MIB_PAGE(port), mibs->offset,
  753. &val32);
  754. val = val32;
  755. }
  756. len += snprintf(dev->buf + len, B53_BUF_SIZE - len,
  757. "%-20s: %llu\n", mibs->name, val);
  758. }
  759. val->len = len;
  760. val->value.s = dev->buf;
  761. return 0;
  762. }
  763. static struct switch_attr b53_global_ops_25[] = {
  764. {
  765. .type = SWITCH_TYPE_INT,
  766. .name = "enable_vlan",
  767. .description = "Enable VLAN mode",
  768. .set = b53_global_set_vlan_enable,
  769. .get = b53_global_get_vlan_enable,
  770. .max = 1,
  771. },
  772. {
  773. .type = SWITCH_TYPE_STRING,
  774. .name = "ports",
  775. .description = "Available ports (as bitmask)",
  776. .get = b53_global_get_ports,
  777. },
  778. };
  779. static struct switch_attr b53_global_ops_65[] = {
  780. {
  781. .type = SWITCH_TYPE_INT,
  782. .name = "enable_vlan",
  783. .description = "Enable VLAN mode",
  784. .set = b53_global_set_vlan_enable,
  785. .get = b53_global_get_vlan_enable,
  786. .max = 1,
  787. },
  788. {
  789. .type = SWITCH_TYPE_STRING,
  790. .name = "ports",
  791. .description = "Available ports (as bitmask)",
  792. .get = b53_global_get_ports,
  793. },
  794. {
  795. .type = SWITCH_TYPE_INT,
  796. .name = "reset_mib",
  797. .description = "Reset MIB counters",
  798. .set = b53_global_reset_mib,
  799. },
  800. };
  801. static struct switch_attr b53_global_ops[] = {
  802. {
  803. .type = SWITCH_TYPE_INT,
  804. .name = "enable_vlan",
  805. .description = "Enable VLAN mode",
  806. .set = b53_global_set_vlan_enable,
  807. .get = b53_global_get_vlan_enable,
  808. .max = 1,
  809. },
  810. {
  811. .type = SWITCH_TYPE_STRING,
  812. .name = "ports",
  813. .description = "Available Ports (as bitmask)",
  814. .get = b53_global_get_ports,
  815. },
  816. {
  817. .type = SWITCH_TYPE_INT,
  818. .name = "reset_mib",
  819. .description = "Reset MIB counters",
  820. .set = b53_global_reset_mib,
  821. },
  822. {
  823. .type = SWITCH_TYPE_INT,
  824. .name = "enable_jumbo",
  825. .description = "Enable Jumbo Frames",
  826. .set = b53_global_set_jumbo_enable,
  827. .get = b53_global_get_jumbo_enable,
  828. .max = 1,
  829. },
  830. {
  831. .type = SWITCH_TYPE_INT,
  832. .name = "allow_vid_4095",
  833. .description = "Allow VID 4095",
  834. .set = b53_global_set_4095_enable,
  835. .get = b53_global_get_4095_enable,
  836. .max = 1,
  837. },
  838. };
  839. static struct switch_attr b53_port_ops[] = {
  840. {
  841. .type = SWITCH_TYPE_STRING,
  842. .name = "mib",
  843. .description = "Get port's MIB counters",
  844. .get = b53_port_get_mib,
  845. },
  846. };
  847. static struct switch_attr b53_no_ops[] = {
  848. };
  849. static const struct switch_dev_ops b53_switch_ops_25 = {
  850. .attr_global = {
  851. .attr = b53_global_ops_25,
  852. .n_attr = ARRAY_SIZE(b53_global_ops_25),
  853. },
  854. .attr_port = {
  855. .attr = b53_no_ops,
  856. .n_attr = ARRAY_SIZE(b53_no_ops),
  857. },
  858. .attr_vlan = {
  859. .attr = b53_no_ops,
  860. .n_attr = ARRAY_SIZE(b53_no_ops),
  861. },
  862. .get_vlan_ports = b53_vlan_get_ports,
  863. .set_vlan_ports = b53_vlan_set_ports,
  864. .get_port_pvid = b53_port_get_pvid,
  865. .set_port_pvid = b53_port_set_pvid,
  866. .apply_config = b53_global_apply_config,
  867. .reset_switch = b53_global_reset_switch,
  868. .get_port_link = b53_port_get_link,
  869. .set_port_link = b53_port_set_link,
  870. .phy_read16 = b53_phy_read16,
  871. .phy_write16 = b53_phy_write16,
  872. };
  873. static const struct switch_dev_ops b53_switch_ops_65 = {
  874. .attr_global = {
  875. .attr = b53_global_ops_65,
  876. .n_attr = ARRAY_SIZE(b53_global_ops_65),
  877. },
  878. .attr_port = {
  879. .attr = b53_port_ops,
  880. .n_attr = ARRAY_SIZE(b53_port_ops),
  881. },
  882. .attr_vlan = {
  883. .attr = b53_no_ops,
  884. .n_attr = ARRAY_SIZE(b53_no_ops),
  885. },
  886. .get_vlan_ports = b53_vlan_get_ports,
  887. .set_vlan_ports = b53_vlan_set_ports,
  888. .get_port_pvid = b53_port_get_pvid,
  889. .set_port_pvid = b53_port_set_pvid,
  890. .apply_config = b53_global_apply_config,
  891. .reset_switch = b53_global_reset_switch,
  892. .get_port_link = b53_port_get_link,
  893. .set_port_link = b53_port_set_link,
  894. .phy_read16 = b53_phy_read16,
  895. .phy_write16 = b53_phy_write16,
  896. };
  897. static const struct switch_dev_ops b53_switch_ops = {
  898. .attr_global = {
  899. .attr = b53_global_ops,
  900. .n_attr = ARRAY_SIZE(b53_global_ops),
  901. },
  902. .attr_port = {
  903. .attr = b53_port_ops,
  904. .n_attr = ARRAY_SIZE(b53_port_ops),
  905. },
  906. .attr_vlan = {
  907. .attr = b53_no_ops,
  908. .n_attr = ARRAY_SIZE(b53_no_ops),
  909. },
  910. .get_vlan_ports = b53_vlan_get_ports,
  911. .set_vlan_ports = b53_vlan_set_ports,
  912. .get_port_pvid = b53_port_get_pvid,
  913. .set_port_pvid = b53_port_set_pvid,
  914. .apply_config = b53_global_apply_config,
  915. .reset_switch = b53_global_reset_switch,
  916. .get_port_link = b53_port_get_link,
  917. .set_port_link = b53_port_set_link,
  918. .phy_read16 = b53_phy_read16,
  919. .phy_write16 = b53_phy_write16,
  920. };
  921. struct b53_chip_data {
  922. u32 chip_id;
  923. const char *dev_name;
  924. const char *alias;
  925. u16 vlans;
  926. u16 enabled_ports;
  927. u8 cpu_port;
  928. u8 vta_regs[3];
  929. u8 duplex_reg;
  930. u8 jumbo_pm_reg;
  931. u8 jumbo_size_reg;
  932. const struct switch_dev_ops *sw_ops;
  933. };
  934. #define B53_VTA_REGS \
  935. { B53_VT_ACCESS, B53_VT_INDEX, B53_VT_ENTRY }
  936. #define B53_VTA_REGS_9798 \
  937. { B53_VT_ACCESS_9798, B53_VT_INDEX_9798, B53_VT_ENTRY_9798 }
  938. #define B53_VTA_REGS_63XX \
  939. { B53_VT_ACCESS_63XX, B53_VT_INDEX_63XX, B53_VT_ENTRY_63XX }
  940. static const struct b53_chip_data b53_switch_chips[] = {
  941. {
  942. .chip_id = BCM5325_DEVICE_ID,
  943. .dev_name = "BCM5325",
  944. .alias = "bcm5325",
  945. .vlans = 16,
  946. .enabled_ports = 0x1f,
  947. .cpu_port = B53_CPU_PORT_25,
  948. .duplex_reg = B53_DUPLEX_STAT_FE,
  949. .sw_ops = &b53_switch_ops_25,
  950. },
  951. {
  952. .chip_id = BCM5365_DEVICE_ID,
  953. .dev_name = "BCM5365",
  954. .alias = "bcm5365",
  955. .vlans = 256,
  956. .enabled_ports = 0x1f,
  957. .cpu_port = B53_CPU_PORT_25,
  958. .duplex_reg = B53_DUPLEX_STAT_FE,
  959. .sw_ops = &b53_switch_ops_65,
  960. },
  961. {
  962. .chip_id = BCM5395_DEVICE_ID,
  963. .dev_name = "BCM5395",
  964. .alias = "bcm5395",
  965. .vlans = 4096,
  966. .enabled_ports = 0x1f,
  967. .cpu_port = B53_CPU_PORT,
  968. .vta_regs = B53_VTA_REGS,
  969. .duplex_reg = B53_DUPLEX_STAT_GE,
  970. .jumbo_pm_reg = B53_JUMBO_PORT_MASK,
  971. .jumbo_size_reg = B53_JUMBO_MAX_SIZE,
  972. .sw_ops = &b53_switch_ops,
  973. },
  974. {
  975. .chip_id = BCM5397_DEVICE_ID,
  976. .dev_name = "BCM5397",
  977. .alias = "bcm5397",
  978. .vlans = 4096,
  979. .enabled_ports = 0x1f,
  980. .cpu_port = B53_CPU_PORT,
  981. .vta_regs = B53_VTA_REGS_9798,
  982. .duplex_reg = B53_DUPLEX_STAT_GE,
  983. .jumbo_pm_reg = B53_JUMBO_PORT_MASK,
  984. .jumbo_size_reg = B53_JUMBO_MAX_SIZE,
  985. .sw_ops = &b53_switch_ops,
  986. },
  987. {
  988. .chip_id = BCM5398_DEVICE_ID,
  989. .dev_name = "BCM5398",
  990. .alias = "bcm5398",
  991. .vlans = 4096,
  992. .enabled_ports = 0x7f,
  993. .cpu_port = B53_CPU_PORT,
  994. .vta_regs = B53_VTA_REGS_9798,
  995. .duplex_reg = B53_DUPLEX_STAT_GE,
  996. .jumbo_pm_reg = B53_JUMBO_PORT_MASK,
  997. .jumbo_size_reg = B53_JUMBO_MAX_SIZE,
  998. .sw_ops = &b53_switch_ops,
  999. },
  1000. {
  1001. .chip_id = BCM53115_DEVICE_ID,
  1002. .dev_name = "BCM53115",
  1003. .alias = "bcm53115",
  1004. .vlans = 4096,
  1005. .enabled_ports = 0x1f,
  1006. .vta_regs = B53_VTA_REGS,
  1007. .cpu_port = B53_CPU_PORT,
  1008. .duplex_reg = B53_DUPLEX_STAT_GE,
  1009. .jumbo_pm_reg = B53_JUMBO_PORT_MASK,
  1010. .jumbo_size_reg = B53_JUMBO_MAX_SIZE,
  1011. .sw_ops = &b53_switch_ops,
  1012. },
  1013. {
  1014. .chip_id = BCM53125_DEVICE_ID,
  1015. .dev_name = "BCM53125",
  1016. .alias = "bcm53125",
  1017. .vlans = 4096,
  1018. .enabled_ports = 0x1f,
  1019. .cpu_port = B53_CPU_PORT,
  1020. .vta_regs = B53_VTA_REGS,
  1021. .duplex_reg = B53_DUPLEX_STAT_GE,
  1022. .jumbo_pm_reg = B53_JUMBO_PORT_MASK,
  1023. .jumbo_size_reg = B53_JUMBO_MAX_SIZE,
  1024. .sw_ops = &b53_switch_ops,
  1025. },
  1026. {
  1027. .chip_id = BCM53128_DEVICE_ID,
  1028. .dev_name = "BCM53128",
  1029. .alias = "bcm53128",
  1030. .vlans = 4096,
  1031. .enabled_ports = 0x1ff,
  1032. .cpu_port = B53_CPU_PORT,
  1033. .vta_regs = B53_VTA_REGS,
  1034. .duplex_reg = B53_DUPLEX_STAT_GE,
  1035. .jumbo_pm_reg = B53_JUMBO_PORT_MASK,
  1036. .jumbo_size_reg = B53_JUMBO_MAX_SIZE,
  1037. .sw_ops = &b53_switch_ops,
  1038. },
  1039. {
  1040. .chip_id = BCM63XX_DEVICE_ID,
  1041. .dev_name = "BCM63xx",
  1042. .alias = "bcm63xx",
  1043. .vlans = 4096,
  1044. .enabled_ports = 0, /* pdata must provide them */
  1045. .cpu_port = B53_CPU_PORT,
  1046. .vta_regs = B53_VTA_REGS_63XX,
  1047. .duplex_reg = B53_DUPLEX_STAT_63XX,
  1048. .jumbo_pm_reg = B53_JUMBO_PORT_MASK_63XX,
  1049. .jumbo_size_reg = B53_JUMBO_MAX_SIZE_63XX,
  1050. .sw_ops = &b53_switch_ops,
  1051. },
  1052. {
  1053. .chip_id = BCM53010_DEVICE_ID,
  1054. .dev_name = "BCM53010",
  1055. .alias = "bcm53011",
  1056. .vlans = 4096,
  1057. .enabled_ports = 0x1f,
  1058. .cpu_port = B53_CPU_PORT_25, /* TODO: auto detect */
  1059. .vta_regs = B53_VTA_REGS,
  1060. .duplex_reg = B53_DUPLEX_STAT_GE,
  1061. .jumbo_pm_reg = B53_JUMBO_PORT_MASK,
  1062. .jumbo_size_reg = B53_JUMBO_MAX_SIZE,
  1063. .sw_ops = &b53_switch_ops,
  1064. },
  1065. {
  1066. .chip_id = BCM53011_DEVICE_ID,
  1067. .dev_name = "BCM53011",
  1068. .alias = "bcm53011",
  1069. .vlans = 4096,
  1070. .enabled_ports = 0x1bf,
  1071. .cpu_port = B53_CPU_PORT_25, /* TODO: auto detect */
  1072. .vta_regs = B53_VTA_REGS,
  1073. .duplex_reg = B53_DUPLEX_STAT_GE,
  1074. .jumbo_pm_reg = B53_JUMBO_PORT_MASK,
  1075. .jumbo_size_reg = B53_JUMBO_MAX_SIZE,
  1076. .sw_ops = &b53_switch_ops,
  1077. },
  1078. {
  1079. .chip_id = BCM53012_DEVICE_ID,
  1080. .dev_name = "BCM53012",
  1081. .alias = "bcm53011",
  1082. .vlans = 4096,
  1083. .enabled_ports = 0x1bf,
  1084. .cpu_port = B53_CPU_PORT_25, /* TODO: auto detect */
  1085. .vta_regs = B53_VTA_REGS,
  1086. .duplex_reg = B53_DUPLEX_STAT_GE,
  1087. .jumbo_pm_reg = B53_JUMBO_PORT_MASK,
  1088. .jumbo_size_reg = B53_JUMBO_MAX_SIZE,
  1089. .sw_ops = &b53_switch_ops,
  1090. },
  1091. {
  1092. .chip_id = BCM53018_DEVICE_ID,
  1093. .dev_name = "BCM53018",
  1094. .alias = "bcm53018",
  1095. .vlans = 4096,
  1096. .enabled_ports = 0x1f,
  1097. .cpu_port = B53_CPU_PORT_25, /* TODO: auto detect */
  1098. .vta_regs = B53_VTA_REGS,
  1099. .duplex_reg = B53_DUPLEX_STAT_GE,
  1100. .jumbo_pm_reg = B53_JUMBO_PORT_MASK,
  1101. .jumbo_size_reg = B53_JUMBO_MAX_SIZE,
  1102. .sw_ops = &b53_switch_ops,
  1103. },
  1104. {
  1105. .chip_id = BCM53019_DEVICE_ID,
  1106. .dev_name = "BCM53019",
  1107. .alias = "bcm53019",
  1108. .vlans = 4096,
  1109. .enabled_ports = 0x1f,
  1110. .cpu_port = B53_CPU_PORT_25, /* TODO: auto detect */
  1111. .vta_regs = B53_VTA_REGS,
  1112. .duplex_reg = B53_DUPLEX_STAT_GE,
  1113. .jumbo_pm_reg = B53_JUMBO_PORT_MASK,
  1114. .jumbo_size_reg = B53_JUMBO_MAX_SIZE,
  1115. .sw_ops = &b53_switch_ops,
  1116. },
  1117. };
  1118. static int b53_switch_init(struct b53_device *dev)
  1119. {
  1120. struct switch_dev *sw_dev = &dev->sw_dev;
  1121. unsigned i;
  1122. int ret;
  1123. for (i = 0; i < ARRAY_SIZE(b53_switch_chips); i++) {
  1124. const struct b53_chip_data *chip = &b53_switch_chips[i];
  1125. if (chip->chip_id == dev->chip_id) {
  1126. sw_dev->name = chip->dev_name;
  1127. if (!sw_dev->alias)
  1128. sw_dev->alias = chip->alias;
  1129. if (!dev->enabled_ports)
  1130. dev->enabled_ports = chip->enabled_ports;
  1131. dev->duplex_reg = chip->duplex_reg;
  1132. dev->vta_regs[0] = chip->vta_regs[0];
  1133. dev->vta_regs[1] = chip->vta_regs[1];
  1134. dev->vta_regs[2] = chip->vta_regs[2];
  1135. dev->jumbo_pm_reg = chip->jumbo_pm_reg;
  1136. sw_dev->ops = chip->sw_ops;
  1137. sw_dev->cpu_port = chip->cpu_port;
  1138. sw_dev->vlans = chip->vlans;
  1139. break;
  1140. }
  1141. }
  1142. if (!sw_dev->name)
  1143. return -EINVAL;
  1144. /* check which BCM5325x version we have */
  1145. if (is5325(dev)) {
  1146. u8 vc4;
  1147. b53_read8(dev, B53_VLAN_PAGE, B53_VLAN_CTRL4_25, &vc4);
  1148. /* check reserved bits */
  1149. switch (vc4 & 3) {
  1150. case 1:
  1151. /* BCM5325E */
  1152. break;
  1153. case 3:
  1154. /* BCM5325F - do not use port 4 */
  1155. dev->enabled_ports &= ~BIT(4);
  1156. break;
  1157. default:
  1158. /* On the BCM47XX SoCs this is the supported internal switch.*/
  1159. #ifndef CONFIG_BCM47XX
  1160. /* BCM5325M */
  1161. return -EINVAL;
  1162. #else
  1163. break;
  1164. #endif
  1165. }
  1166. } else if (dev->chip_id == BCM53115_DEVICE_ID) {
  1167. u64 strap_value;
  1168. b53_read48(dev, B53_STAT_PAGE, B53_STRAP_VALUE, &strap_value);
  1169. /* use second IMP port if GMII is enabled */
  1170. if (strap_value & SV_GMII_CTRL_115)
  1171. sw_dev->cpu_port = 5;
  1172. }
  1173. dev->enabled_ports |= BIT(sw_dev->cpu_port);
  1174. sw_dev->ports = fls(dev->enabled_ports);
  1175. dev->ports = devm_kzalloc(dev->dev,
  1176. sizeof(struct b53_port) * sw_dev->ports,
  1177. GFP_KERNEL);
  1178. if (!dev->ports)
  1179. return -ENOMEM;
  1180. dev->vlans = devm_kzalloc(dev->dev,
  1181. sizeof(struct b53_vlan) * sw_dev->vlans,
  1182. GFP_KERNEL);
  1183. if (!dev->vlans)
  1184. return -ENOMEM;
  1185. dev->buf = devm_kzalloc(dev->dev, B53_BUF_SIZE, GFP_KERNEL);
  1186. if (!dev->buf)
  1187. return -ENOMEM;
  1188. dev->reset_gpio = b53_switch_get_reset_gpio(dev);
  1189. if (dev->reset_gpio >= 0) {
  1190. ret = devm_gpio_request_one(dev->dev, dev->reset_gpio,
  1191. GPIOF_OUT_INIT_HIGH, "robo_reset");
  1192. if (ret)
  1193. return ret;
  1194. }
  1195. return b53_switch_reset(dev);
  1196. }
  1197. struct b53_device *b53_switch_alloc(struct device *base, struct b53_io_ops *ops,
  1198. void *priv)
  1199. {
  1200. struct b53_device *dev;
  1201. dev = devm_kzalloc(base, sizeof(*dev), GFP_KERNEL);
  1202. if (!dev)
  1203. return NULL;
  1204. dev->dev = base;
  1205. dev->ops = ops;
  1206. dev->priv = priv;
  1207. mutex_init(&dev->reg_mutex);
  1208. return dev;
  1209. }
  1210. EXPORT_SYMBOL(b53_switch_alloc);
  1211. int b53_switch_detect(struct b53_device *dev)
  1212. {
  1213. u32 id32;
  1214. u16 tmp;
  1215. u8 id8;
  1216. int ret;
  1217. ret = b53_read8(dev, B53_MGMT_PAGE, B53_DEVICE_ID, &id8);
  1218. if (ret)
  1219. return ret;
  1220. switch (id8) {
  1221. case 0:
  1222. /*
  1223. * BCM5325 and BCM5365 do not have this register so reads
  1224. * return 0. But the read operation did succeed, so assume
  1225. * this is one of them.
  1226. *
  1227. * Next check if we can write to the 5325's VTA register; for
  1228. * 5365 it is read only.
  1229. */
  1230. b53_write16(dev, B53_VLAN_PAGE, B53_VLAN_TABLE_ACCESS_25, 0xf);
  1231. b53_read16(dev, B53_VLAN_PAGE, B53_VLAN_TABLE_ACCESS_25, &tmp);
  1232. if (tmp == 0xf)
  1233. dev->chip_id = BCM5325_DEVICE_ID;
  1234. else
  1235. dev->chip_id = BCM5365_DEVICE_ID;
  1236. break;
  1237. case BCM5395_DEVICE_ID:
  1238. case BCM5397_DEVICE_ID:
  1239. case BCM5398_DEVICE_ID:
  1240. dev->chip_id = id8;
  1241. break;
  1242. default:
  1243. ret = b53_read32(dev, B53_MGMT_PAGE, B53_DEVICE_ID, &id32);
  1244. if (ret)
  1245. return ret;
  1246. switch (id32) {
  1247. case BCM53115_DEVICE_ID:
  1248. case BCM53125_DEVICE_ID:
  1249. case BCM53128_DEVICE_ID:
  1250. case BCM53010_DEVICE_ID:
  1251. case BCM53011_DEVICE_ID:
  1252. case BCM53012_DEVICE_ID:
  1253. case BCM53018_DEVICE_ID:
  1254. case BCM53019_DEVICE_ID:
  1255. dev->chip_id = id32;
  1256. break;
  1257. default:
  1258. pr_err("unsupported switch detected (BCM53%02x/BCM%x)\n",
  1259. id8, id32);
  1260. return -ENODEV;
  1261. }
  1262. }
  1263. if (dev->chip_id == BCM5325_DEVICE_ID)
  1264. return b53_read8(dev, B53_STAT_PAGE, B53_REV_ID_25,
  1265. &dev->core_rev);
  1266. else
  1267. return b53_read8(dev, B53_MGMT_PAGE, B53_REV_ID,
  1268. &dev->core_rev);
  1269. }
  1270. EXPORT_SYMBOL(b53_switch_detect);
  1271. int b53_switch_register(struct b53_device *dev)
  1272. {
  1273. int ret;
  1274. if (dev->pdata) {
  1275. dev->chip_id = dev->pdata->chip_id;
  1276. dev->enabled_ports = dev->pdata->enabled_ports;
  1277. dev->sw_dev.alias = dev->pdata->alias;
  1278. }
  1279. if (!dev->chip_id && b53_switch_detect(dev))
  1280. return -EINVAL;
  1281. ret = b53_switch_init(dev);
  1282. if (ret)
  1283. return ret;
  1284. pr_info("found switch: %s, rev %i\n", dev->sw_dev.name, dev->core_rev);
  1285. return register_switch(&dev->sw_dev, NULL);
  1286. }
  1287. EXPORT_SYMBOL(b53_switch_register);
  1288. MODULE_AUTHOR("Jonas Gorski <jogo@openwrt.org>");
  1289. MODULE_DESCRIPTION("B53 switch library");
  1290. MODULE_LICENSE("Dual BSD/GPL");