chip.c 92 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898
  1. /*
  2. * Marvell 88e6xxx Ethernet switch single-chip support
  3. *
  4. * Copyright (c) 2008 Marvell Semiconductor
  5. *
  6. * Copyright (c) 2015 CMC Electronics, Inc.
  7. * Added support for VLAN Table Unit operations
  8. *
  9. * Copyright (c) 2016 Andrew Lunn <andrew@lunn.ch>
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version.
  15. */
  16. #include <linux/delay.h>
  17. #include <linux/etherdevice.h>
  18. #include <linux/ethtool.h>
  19. #include <linux/if_bridge.h>
  20. #include <linux/jiffies.h>
  21. #include <linux/list.h>
  22. #include <linux/mdio.h>
  23. #include <linux/module.h>
  24. #include <linux/of_device.h>
  25. #include <linux/of_mdio.h>
  26. #include <linux/netdevice.h>
  27. #include <linux/gpio/consumer.h>
  28. #include <linux/phy.h>
  29. #include <net/dsa.h>
  30. #include <net/switchdev.h>
  31. #include "mv88e6xxx.h"
  32. #include "global1.h"
  33. #include "global2.h"
  34. static void assert_reg_lock(struct mv88e6xxx_chip *chip)
  35. {
  36. if (unlikely(!mutex_is_locked(&chip->reg_lock))) {
  37. dev_err(chip->dev, "Switch registers lock not held!\n");
  38. dump_stack();
  39. }
  40. }
  41. /* The switch ADDR[4:1] configuration pins define the chip SMI device address
  42. * (ADDR[0] is always zero, thus only even SMI addresses can be strapped).
  43. *
  44. * When ADDR is all zero, the chip uses Single-chip Addressing Mode, assuming it
  45. * is the only device connected to the SMI master. In this mode it responds to
  46. * all 32 possible SMI addresses, and thus maps directly the internal devices.
  47. *
  48. * When ADDR is non-zero, the chip uses Multi-chip Addressing Mode, allowing
  49. * multiple devices to share the SMI interface. In this mode it responds to only
  50. * 2 registers, used to indirectly access the internal SMI devices.
  51. */
  52. static int mv88e6xxx_smi_read(struct mv88e6xxx_chip *chip,
  53. int addr, int reg, u16 *val)
  54. {
  55. if (!chip->smi_ops)
  56. return -EOPNOTSUPP;
  57. return chip->smi_ops->read(chip, addr, reg, val);
  58. }
  59. static int mv88e6xxx_smi_write(struct mv88e6xxx_chip *chip,
  60. int addr, int reg, u16 val)
  61. {
  62. if (!chip->smi_ops)
  63. return -EOPNOTSUPP;
  64. return chip->smi_ops->write(chip, addr, reg, val);
  65. }
  66. static int mv88e6xxx_smi_single_chip_read(struct mv88e6xxx_chip *chip,
  67. int addr, int reg, u16 *val)
  68. {
  69. int ret;
  70. ret = mdiobus_read_nested(chip->bus, addr, reg);
  71. if (ret < 0)
  72. return ret;
  73. *val = ret & 0xffff;
  74. return 0;
  75. }
  76. static int mv88e6xxx_smi_single_chip_write(struct mv88e6xxx_chip *chip,
  77. int addr, int reg, u16 val)
  78. {
  79. int ret;
  80. ret = mdiobus_write_nested(chip->bus, addr, reg, val);
  81. if (ret < 0)
  82. return ret;
  83. return 0;
  84. }
  85. static const struct mv88e6xxx_bus_ops mv88e6xxx_smi_single_chip_ops = {
  86. .read = mv88e6xxx_smi_single_chip_read,
  87. .write = mv88e6xxx_smi_single_chip_write,
  88. };
  89. static int mv88e6xxx_smi_multi_chip_wait(struct mv88e6xxx_chip *chip)
  90. {
  91. int ret;
  92. int i;
  93. for (i = 0; i < 16; i++) {
  94. ret = mdiobus_read_nested(chip->bus, chip->sw_addr, SMI_CMD);
  95. if (ret < 0)
  96. return ret;
  97. if ((ret & SMI_CMD_BUSY) == 0)
  98. return 0;
  99. }
  100. return -ETIMEDOUT;
  101. }
  102. static int mv88e6xxx_smi_multi_chip_read(struct mv88e6xxx_chip *chip,
  103. int addr, int reg, u16 *val)
  104. {
  105. int ret;
  106. /* Wait for the bus to become free. */
  107. ret = mv88e6xxx_smi_multi_chip_wait(chip);
  108. if (ret < 0)
  109. return ret;
  110. /* Transmit the read command. */
  111. ret = mdiobus_write_nested(chip->bus, chip->sw_addr, SMI_CMD,
  112. SMI_CMD_OP_22_READ | (addr << 5) | reg);
  113. if (ret < 0)
  114. return ret;
  115. /* Wait for the read command to complete. */
  116. ret = mv88e6xxx_smi_multi_chip_wait(chip);
  117. if (ret < 0)
  118. return ret;
  119. /* Read the data. */
  120. ret = mdiobus_read_nested(chip->bus, chip->sw_addr, SMI_DATA);
  121. if (ret < 0)
  122. return ret;
  123. *val = ret & 0xffff;
  124. return 0;
  125. }
  126. static int mv88e6xxx_smi_multi_chip_write(struct mv88e6xxx_chip *chip,
  127. int addr, int reg, u16 val)
  128. {
  129. int ret;
  130. /* Wait for the bus to become free. */
  131. ret = mv88e6xxx_smi_multi_chip_wait(chip);
  132. if (ret < 0)
  133. return ret;
  134. /* Transmit the data to write. */
  135. ret = mdiobus_write_nested(chip->bus, chip->sw_addr, SMI_DATA, val);
  136. if (ret < 0)
  137. return ret;
  138. /* Transmit the write command. */
  139. ret = mdiobus_write_nested(chip->bus, chip->sw_addr, SMI_CMD,
  140. SMI_CMD_OP_22_WRITE | (addr << 5) | reg);
  141. if (ret < 0)
  142. return ret;
  143. /* Wait for the write command to complete. */
  144. ret = mv88e6xxx_smi_multi_chip_wait(chip);
  145. if (ret < 0)
  146. return ret;
  147. return 0;
  148. }
  149. static const struct mv88e6xxx_bus_ops mv88e6xxx_smi_multi_chip_ops = {
  150. .read = mv88e6xxx_smi_multi_chip_read,
  151. .write = mv88e6xxx_smi_multi_chip_write,
  152. };
  153. int mv88e6xxx_read(struct mv88e6xxx_chip *chip, int addr, int reg, u16 *val)
  154. {
  155. int err;
  156. assert_reg_lock(chip);
  157. err = mv88e6xxx_smi_read(chip, addr, reg, val);
  158. if (err)
  159. return err;
  160. dev_dbg(chip->dev, "<- addr: 0x%.2x reg: 0x%.2x val: 0x%.4x\n",
  161. addr, reg, *val);
  162. return 0;
  163. }
  164. int mv88e6xxx_write(struct mv88e6xxx_chip *chip, int addr, int reg, u16 val)
  165. {
  166. int err;
  167. assert_reg_lock(chip);
  168. err = mv88e6xxx_smi_write(chip, addr, reg, val);
  169. if (err)
  170. return err;
  171. dev_dbg(chip->dev, "-> addr: 0x%.2x reg: 0x%.2x val: 0x%.4x\n",
  172. addr, reg, val);
  173. return 0;
  174. }
  175. static int mv88e6xxx_port_read(struct mv88e6xxx_chip *chip, int port, int reg,
  176. u16 *val)
  177. {
  178. int addr = chip->info->port_base_addr + port;
  179. return mv88e6xxx_read(chip, addr, reg, val);
  180. }
  181. static int mv88e6xxx_port_write(struct mv88e6xxx_chip *chip, int port, int reg,
  182. u16 val)
  183. {
  184. int addr = chip->info->port_base_addr + port;
  185. return mv88e6xxx_write(chip, addr, reg, val);
  186. }
  187. static int mv88e6xxx_phy_read(struct mv88e6xxx_chip *chip, int phy,
  188. int reg, u16 *val)
  189. {
  190. int addr = phy; /* PHY devices addresses start at 0x0 */
  191. if (!chip->info->ops->phy_read)
  192. return -EOPNOTSUPP;
  193. return chip->info->ops->phy_read(chip, addr, reg, val);
  194. }
  195. static int mv88e6xxx_phy_write(struct mv88e6xxx_chip *chip, int phy,
  196. int reg, u16 val)
  197. {
  198. int addr = phy; /* PHY devices addresses start at 0x0 */
  199. if (!chip->info->ops->phy_write)
  200. return -EOPNOTSUPP;
  201. return chip->info->ops->phy_write(chip, addr, reg, val);
  202. }
  203. static int mv88e6xxx_phy_page_get(struct mv88e6xxx_chip *chip, int phy, u8 page)
  204. {
  205. if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_PHY_PAGE))
  206. return -EOPNOTSUPP;
  207. return mv88e6xxx_phy_write(chip, phy, PHY_PAGE, page);
  208. }
  209. static void mv88e6xxx_phy_page_put(struct mv88e6xxx_chip *chip, int phy)
  210. {
  211. int err;
  212. /* Restore PHY page Copper 0x0 for access via the registered MDIO bus */
  213. err = mv88e6xxx_phy_write(chip, phy, PHY_PAGE, PHY_PAGE_COPPER);
  214. if (unlikely(err)) {
  215. dev_err(chip->dev, "failed to restore PHY %d page Copper (%d)\n",
  216. phy, err);
  217. }
  218. }
  219. static int mv88e6xxx_phy_page_read(struct mv88e6xxx_chip *chip, int phy,
  220. u8 page, int reg, u16 *val)
  221. {
  222. int err;
  223. /* There is no paging for registers 22 */
  224. if (reg == PHY_PAGE)
  225. return -EINVAL;
  226. err = mv88e6xxx_phy_page_get(chip, phy, page);
  227. if (!err) {
  228. err = mv88e6xxx_phy_read(chip, phy, reg, val);
  229. mv88e6xxx_phy_page_put(chip, phy);
  230. }
  231. return err;
  232. }
  233. static int mv88e6xxx_phy_page_write(struct mv88e6xxx_chip *chip, int phy,
  234. u8 page, int reg, u16 val)
  235. {
  236. int err;
  237. /* There is no paging for registers 22 */
  238. if (reg == PHY_PAGE)
  239. return -EINVAL;
  240. err = mv88e6xxx_phy_page_get(chip, phy, page);
  241. if (!err) {
  242. err = mv88e6xxx_phy_write(chip, phy, PHY_PAGE, page);
  243. mv88e6xxx_phy_page_put(chip, phy);
  244. }
  245. return err;
  246. }
  247. static int mv88e6xxx_serdes_read(struct mv88e6xxx_chip *chip, int reg, u16 *val)
  248. {
  249. return mv88e6xxx_phy_page_read(chip, ADDR_SERDES, SERDES_PAGE_FIBER,
  250. reg, val);
  251. }
  252. static int mv88e6xxx_serdes_write(struct mv88e6xxx_chip *chip, int reg, u16 val)
  253. {
  254. return mv88e6xxx_phy_page_write(chip, ADDR_SERDES, SERDES_PAGE_FIBER,
  255. reg, val);
  256. }
  257. int mv88e6xxx_wait(struct mv88e6xxx_chip *chip, int addr, int reg, u16 mask)
  258. {
  259. int i;
  260. for (i = 0; i < 16; i++) {
  261. u16 val;
  262. int err;
  263. err = mv88e6xxx_read(chip, addr, reg, &val);
  264. if (err)
  265. return err;
  266. if (!(val & mask))
  267. return 0;
  268. usleep_range(1000, 2000);
  269. }
  270. dev_err(chip->dev, "Timeout while waiting for switch\n");
  271. return -ETIMEDOUT;
  272. }
  273. /* Indirect write to single pointer-data register with an Update bit */
  274. int mv88e6xxx_update(struct mv88e6xxx_chip *chip, int addr, int reg, u16 update)
  275. {
  276. u16 val;
  277. int err;
  278. /* Wait until the previous operation is completed */
  279. err = mv88e6xxx_wait(chip, addr, reg, BIT(15));
  280. if (err)
  281. return err;
  282. /* Set the Update bit to trigger a write operation */
  283. val = BIT(15) | update;
  284. return mv88e6xxx_write(chip, addr, reg, val);
  285. }
  286. static int mv88e6xxx_ppu_disable(struct mv88e6xxx_chip *chip)
  287. {
  288. u16 val;
  289. int i, err;
  290. err = mv88e6xxx_g1_read(chip, GLOBAL_CONTROL, &val);
  291. if (err)
  292. return err;
  293. err = mv88e6xxx_g1_write(chip, GLOBAL_CONTROL,
  294. val & ~GLOBAL_CONTROL_PPU_ENABLE);
  295. if (err)
  296. return err;
  297. for (i = 0; i < 16; i++) {
  298. err = mv88e6xxx_g1_read(chip, GLOBAL_STATUS, &val);
  299. if (err)
  300. return err;
  301. usleep_range(1000, 2000);
  302. if ((val & GLOBAL_STATUS_PPU_MASK) != GLOBAL_STATUS_PPU_POLLING)
  303. return 0;
  304. }
  305. return -ETIMEDOUT;
  306. }
  307. static int mv88e6xxx_ppu_enable(struct mv88e6xxx_chip *chip)
  308. {
  309. u16 val;
  310. int i, err;
  311. err = mv88e6xxx_g1_read(chip, GLOBAL_CONTROL, &val);
  312. if (err)
  313. return err;
  314. err = mv88e6xxx_g1_write(chip, GLOBAL_CONTROL,
  315. val | GLOBAL_CONTROL_PPU_ENABLE);
  316. if (err)
  317. return err;
  318. for (i = 0; i < 16; i++) {
  319. err = mv88e6xxx_g1_read(chip, GLOBAL_STATUS, &val);
  320. if (err)
  321. return err;
  322. usleep_range(1000, 2000);
  323. if ((val & GLOBAL_STATUS_PPU_MASK) == GLOBAL_STATUS_PPU_POLLING)
  324. return 0;
  325. }
  326. return -ETIMEDOUT;
  327. }
  328. static void mv88e6xxx_ppu_reenable_work(struct work_struct *ugly)
  329. {
  330. struct mv88e6xxx_chip *chip;
  331. chip = container_of(ugly, struct mv88e6xxx_chip, ppu_work);
  332. mutex_lock(&chip->reg_lock);
  333. if (mutex_trylock(&chip->ppu_mutex)) {
  334. if (mv88e6xxx_ppu_enable(chip) == 0)
  335. chip->ppu_disabled = 0;
  336. mutex_unlock(&chip->ppu_mutex);
  337. }
  338. mutex_unlock(&chip->reg_lock);
  339. }
  340. static void mv88e6xxx_ppu_reenable_timer(unsigned long _ps)
  341. {
  342. struct mv88e6xxx_chip *chip = (void *)_ps;
  343. schedule_work(&chip->ppu_work);
  344. }
  345. static int mv88e6xxx_ppu_access_get(struct mv88e6xxx_chip *chip)
  346. {
  347. int ret;
  348. mutex_lock(&chip->ppu_mutex);
  349. /* If the PHY polling unit is enabled, disable it so that
  350. * we can access the PHY registers. If it was already
  351. * disabled, cancel the timer that is going to re-enable
  352. * it.
  353. */
  354. if (!chip->ppu_disabled) {
  355. ret = mv88e6xxx_ppu_disable(chip);
  356. if (ret < 0) {
  357. mutex_unlock(&chip->ppu_mutex);
  358. return ret;
  359. }
  360. chip->ppu_disabled = 1;
  361. } else {
  362. del_timer(&chip->ppu_timer);
  363. ret = 0;
  364. }
  365. return ret;
  366. }
  367. static void mv88e6xxx_ppu_access_put(struct mv88e6xxx_chip *chip)
  368. {
  369. /* Schedule a timer to re-enable the PHY polling unit. */
  370. mod_timer(&chip->ppu_timer, jiffies + msecs_to_jiffies(10));
  371. mutex_unlock(&chip->ppu_mutex);
  372. }
  373. static void mv88e6xxx_ppu_state_init(struct mv88e6xxx_chip *chip)
  374. {
  375. mutex_init(&chip->ppu_mutex);
  376. INIT_WORK(&chip->ppu_work, mv88e6xxx_ppu_reenable_work);
  377. init_timer(&chip->ppu_timer);
  378. chip->ppu_timer.data = (unsigned long)chip;
  379. chip->ppu_timer.function = mv88e6xxx_ppu_reenable_timer;
  380. }
  381. static void mv88e6xxx_ppu_state_destroy(struct mv88e6xxx_chip *chip)
  382. {
  383. del_timer_sync(&chip->ppu_timer);
  384. }
  385. static int mv88e6xxx_phy_ppu_read(struct mv88e6xxx_chip *chip, int addr,
  386. int reg, u16 *val)
  387. {
  388. int err;
  389. err = mv88e6xxx_ppu_access_get(chip);
  390. if (!err) {
  391. err = mv88e6xxx_read(chip, addr, reg, val);
  392. mv88e6xxx_ppu_access_put(chip);
  393. }
  394. return err;
  395. }
  396. static int mv88e6xxx_phy_ppu_write(struct mv88e6xxx_chip *chip, int addr,
  397. int reg, u16 val)
  398. {
  399. int err;
  400. err = mv88e6xxx_ppu_access_get(chip);
  401. if (!err) {
  402. err = mv88e6xxx_write(chip, addr, reg, val);
  403. mv88e6xxx_ppu_access_put(chip);
  404. }
  405. return err;
  406. }
  407. static bool mv88e6xxx_6065_family(struct mv88e6xxx_chip *chip)
  408. {
  409. return chip->info->family == MV88E6XXX_FAMILY_6065;
  410. }
  411. static bool mv88e6xxx_6095_family(struct mv88e6xxx_chip *chip)
  412. {
  413. return chip->info->family == MV88E6XXX_FAMILY_6095;
  414. }
  415. static bool mv88e6xxx_6097_family(struct mv88e6xxx_chip *chip)
  416. {
  417. return chip->info->family == MV88E6XXX_FAMILY_6097;
  418. }
  419. static bool mv88e6xxx_6165_family(struct mv88e6xxx_chip *chip)
  420. {
  421. return chip->info->family == MV88E6XXX_FAMILY_6165;
  422. }
  423. static bool mv88e6xxx_6185_family(struct mv88e6xxx_chip *chip)
  424. {
  425. return chip->info->family == MV88E6XXX_FAMILY_6185;
  426. }
  427. static bool mv88e6xxx_6320_family(struct mv88e6xxx_chip *chip)
  428. {
  429. return chip->info->family == MV88E6XXX_FAMILY_6320;
  430. }
  431. static bool mv88e6xxx_6351_family(struct mv88e6xxx_chip *chip)
  432. {
  433. return chip->info->family == MV88E6XXX_FAMILY_6351;
  434. }
  435. static bool mv88e6xxx_6352_family(struct mv88e6xxx_chip *chip)
  436. {
  437. return chip->info->family == MV88E6XXX_FAMILY_6352;
  438. }
  439. /* We expect the switch to perform auto negotiation if there is a real
  440. * phy. However, in the case of a fixed link phy, we force the port
  441. * settings from the fixed link settings.
  442. */
  443. static void mv88e6xxx_adjust_link(struct dsa_switch *ds, int port,
  444. struct phy_device *phydev)
  445. {
  446. struct mv88e6xxx_chip *chip = ds->priv;
  447. u16 reg;
  448. int err;
  449. if (!phy_is_pseudo_fixed_link(phydev))
  450. return;
  451. mutex_lock(&chip->reg_lock);
  452. err = mv88e6xxx_port_read(chip, port, PORT_PCS_CTRL, &reg);
  453. if (err)
  454. goto out;
  455. reg &= ~(PORT_PCS_CTRL_LINK_UP |
  456. PORT_PCS_CTRL_FORCE_LINK |
  457. PORT_PCS_CTRL_DUPLEX_FULL |
  458. PORT_PCS_CTRL_FORCE_DUPLEX |
  459. PORT_PCS_CTRL_UNFORCED);
  460. reg |= PORT_PCS_CTRL_FORCE_LINK;
  461. if (phydev->link)
  462. reg |= PORT_PCS_CTRL_LINK_UP;
  463. if (mv88e6xxx_6065_family(chip) && phydev->speed > SPEED_100)
  464. goto out;
  465. switch (phydev->speed) {
  466. case SPEED_1000:
  467. reg |= PORT_PCS_CTRL_1000;
  468. break;
  469. case SPEED_100:
  470. reg |= PORT_PCS_CTRL_100;
  471. break;
  472. case SPEED_10:
  473. reg |= PORT_PCS_CTRL_10;
  474. break;
  475. default:
  476. pr_info("Unknown speed");
  477. goto out;
  478. }
  479. reg |= PORT_PCS_CTRL_FORCE_DUPLEX;
  480. if (phydev->duplex == DUPLEX_FULL)
  481. reg |= PORT_PCS_CTRL_DUPLEX_FULL;
  482. if ((mv88e6xxx_6352_family(chip) || mv88e6xxx_6351_family(chip)) &&
  483. (port >= mv88e6xxx_num_ports(chip) - 2)) {
  484. if (phydev->interface == PHY_INTERFACE_MODE_RGMII_RXID)
  485. reg |= PORT_PCS_CTRL_RGMII_DELAY_RXCLK;
  486. if (phydev->interface == PHY_INTERFACE_MODE_RGMII_TXID)
  487. reg |= PORT_PCS_CTRL_RGMII_DELAY_TXCLK;
  488. if (phydev->interface == PHY_INTERFACE_MODE_RGMII_ID)
  489. reg |= (PORT_PCS_CTRL_RGMII_DELAY_RXCLK |
  490. PORT_PCS_CTRL_RGMII_DELAY_TXCLK);
  491. }
  492. mv88e6xxx_port_write(chip, port, PORT_PCS_CTRL, reg);
  493. out:
  494. mutex_unlock(&chip->reg_lock);
  495. }
  496. static int _mv88e6xxx_stats_wait(struct mv88e6xxx_chip *chip)
  497. {
  498. u16 val;
  499. int i, err;
  500. for (i = 0; i < 10; i++) {
  501. err = mv88e6xxx_g1_read(chip, GLOBAL_STATS_OP, &val);
  502. if ((val & GLOBAL_STATS_OP_BUSY) == 0)
  503. return 0;
  504. }
  505. return -ETIMEDOUT;
  506. }
  507. static int _mv88e6xxx_stats_snapshot(struct mv88e6xxx_chip *chip, int port)
  508. {
  509. int err;
  510. if (mv88e6xxx_6320_family(chip) || mv88e6xxx_6352_family(chip))
  511. port = (port + 1) << 5;
  512. /* Snapshot the hardware statistics counters for this port. */
  513. err = mv88e6xxx_g1_write(chip, GLOBAL_STATS_OP,
  514. GLOBAL_STATS_OP_CAPTURE_PORT |
  515. GLOBAL_STATS_OP_HIST_RX_TX | port);
  516. if (err)
  517. return err;
  518. /* Wait for the snapshotting to complete. */
  519. return _mv88e6xxx_stats_wait(chip);
  520. }
  521. static void _mv88e6xxx_stats_read(struct mv88e6xxx_chip *chip,
  522. int stat, u32 *val)
  523. {
  524. u32 value;
  525. u16 reg;
  526. int err;
  527. *val = 0;
  528. err = mv88e6xxx_g1_write(chip, GLOBAL_STATS_OP,
  529. GLOBAL_STATS_OP_READ_CAPTURED |
  530. GLOBAL_STATS_OP_HIST_RX_TX | stat);
  531. if (err)
  532. return;
  533. err = _mv88e6xxx_stats_wait(chip);
  534. if (err)
  535. return;
  536. err = mv88e6xxx_g1_read(chip, GLOBAL_STATS_COUNTER_32, &reg);
  537. if (err)
  538. return;
  539. value = reg << 16;
  540. err = mv88e6xxx_g1_read(chip, GLOBAL_STATS_COUNTER_01, &reg);
  541. if (err)
  542. return;
  543. *val = value | reg;
  544. }
  545. static struct mv88e6xxx_hw_stat mv88e6xxx_hw_stats[] = {
  546. { "in_good_octets", 8, 0x00, BANK0, },
  547. { "in_bad_octets", 4, 0x02, BANK0, },
  548. { "in_unicast", 4, 0x04, BANK0, },
  549. { "in_broadcasts", 4, 0x06, BANK0, },
  550. { "in_multicasts", 4, 0x07, BANK0, },
  551. { "in_pause", 4, 0x16, BANK0, },
  552. { "in_undersize", 4, 0x18, BANK0, },
  553. { "in_fragments", 4, 0x19, BANK0, },
  554. { "in_oversize", 4, 0x1a, BANK0, },
  555. { "in_jabber", 4, 0x1b, BANK0, },
  556. { "in_rx_error", 4, 0x1c, BANK0, },
  557. { "in_fcs_error", 4, 0x1d, BANK0, },
  558. { "out_octets", 8, 0x0e, BANK0, },
  559. { "out_unicast", 4, 0x10, BANK0, },
  560. { "out_broadcasts", 4, 0x13, BANK0, },
  561. { "out_multicasts", 4, 0x12, BANK0, },
  562. { "out_pause", 4, 0x15, BANK0, },
  563. { "excessive", 4, 0x11, BANK0, },
  564. { "collisions", 4, 0x1e, BANK0, },
  565. { "deferred", 4, 0x05, BANK0, },
  566. { "single", 4, 0x14, BANK0, },
  567. { "multiple", 4, 0x17, BANK0, },
  568. { "out_fcs_error", 4, 0x03, BANK0, },
  569. { "late", 4, 0x1f, BANK0, },
  570. { "hist_64bytes", 4, 0x08, BANK0, },
  571. { "hist_65_127bytes", 4, 0x09, BANK0, },
  572. { "hist_128_255bytes", 4, 0x0a, BANK0, },
  573. { "hist_256_511bytes", 4, 0x0b, BANK0, },
  574. { "hist_512_1023bytes", 4, 0x0c, BANK0, },
  575. { "hist_1024_max_bytes", 4, 0x0d, BANK0, },
  576. { "sw_in_discards", 4, 0x10, PORT, },
  577. { "sw_in_filtered", 2, 0x12, PORT, },
  578. { "sw_out_filtered", 2, 0x13, PORT, },
  579. { "in_discards", 4, 0x00 | GLOBAL_STATS_OP_BANK_1, BANK1, },
  580. { "in_filtered", 4, 0x01 | GLOBAL_STATS_OP_BANK_1, BANK1, },
  581. { "in_accepted", 4, 0x02 | GLOBAL_STATS_OP_BANK_1, BANK1, },
  582. { "in_bad_accepted", 4, 0x03 | GLOBAL_STATS_OP_BANK_1, BANK1, },
  583. { "in_good_avb_class_a", 4, 0x04 | GLOBAL_STATS_OP_BANK_1, BANK1, },
  584. { "in_good_avb_class_b", 4, 0x05 | GLOBAL_STATS_OP_BANK_1, BANK1, },
  585. { "in_bad_avb_class_a", 4, 0x06 | GLOBAL_STATS_OP_BANK_1, BANK1, },
  586. { "in_bad_avb_class_b", 4, 0x07 | GLOBAL_STATS_OP_BANK_1, BANK1, },
  587. { "tcam_counter_0", 4, 0x08 | GLOBAL_STATS_OP_BANK_1, BANK1, },
  588. { "tcam_counter_1", 4, 0x09 | GLOBAL_STATS_OP_BANK_1, BANK1, },
  589. { "tcam_counter_2", 4, 0x0a | GLOBAL_STATS_OP_BANK_1, BANK1, },
  590. { "tcam_counter_3", 4, 0x0b | GLOBAL_STATS_OP_BANK_1, BANK1, },
  591. { "in_da_unknown", 4, 0x0e | GLOBAL_STATS_OP_BANK_1, BANK1, },
  592. { "in_management", 4, 0x0f | GLOBAL_STATS_OP_BANK_1, BANK1, },
  593. { "out_queue_0", 4, 0x10 | GLOBAL_STATS_OP_BANK_1, BANK1, },
  594. { "out_queue_1", 4, 0x11 | GLOBAL_STATS_OP_BANK_1, BANK1, },
  595. { "out_queue_2", 4, 0x12 | GLOBAL_STATS_OP_BANK_1, BANK1, },
  596. { "out_queue_3", 4, 0x13 | GLOBAL_STATS_OP_BANK_1, BANK1, },
  597. { "out_queue_4", 4, 0x14 | GLOBAL_STATS_OP_BANK_1, BANK1, },
  598. { "out_queue_5", 4, 0x15 | GLOBAL_STATS_OP_BANK_1, BANK1, },
  599. { "out_queue_6", 4, 0x16 | GLOBAL_STATS_OP_BANK_1, BANK1, },
  600. { "out_queue_7", 4, 0x17 | GLOBAL_STATS_OP_BANK_1, BANK1, },
  601. { "out_cut_through", 4, 0x18 | GLOBAL_STATS_OP_BANK_1, BANK1, },
  602. { "out_octets_a", 4, 0x1a | GLOBAL_STATS_OP_BANK_1, BANK1, },
  603. { "out_octets_b", 4, 0x1b | GLOBAL_STATS_OP_BANK_1, BANK1, },
  604. { "out_management", 4, 0x1f | GLOBAL_STATS_OP_BANK_1, BANK1, },
  605. };
  606. static bool mv88e6xxx_has_stat(struct mv88e6xxx_chip *chip,
  607. struct mv88e6xxx_hw_stat *stat)
  608. {
  609. switch (stat->type) {
  610. case BANK0:
  611. return true;
  612. case BANK1:
  613. return mv88e6xxx_6320_family(chip);
  614. case PORT:
  615. return mv88e6xxx_6095_family(chip) ||
  616. mv88e6xxx_6185_family(chip) ||
  617. mv88e6xxx_6097_family(chip) ||
  618. mv88e6xxx_6165_family(chip) ||
  619. mv88e6xxx_6351_family(chip) ||
  620. mv88e6xxx_6352_family(chip);
  621. }
  622. return false;
  623. }
  624. static uint64_t _mv88e6xxx_get_ethtool_stat(struct mv88e6xxx_chip *chip,
  625. struct mv88e6xxx_hw_stat *s,
  626. int port)
  627. {
  628. u32 low;
  629. u32 high = 0;
  630. int err;
  631. u16 reg;
  632. u64 value;
  633. switch (s->type) {
  634. case PORT:
  635. err = mv88e6xxx_port_read(chip, port, s->reg, &reg);
  636. if (err)
  637. return UINT64_MAX;
  638. low = reg;
  639. if (s->sizeof_stat == 4) {
  640. err = mv88e6xxx_port_read(chip, port, s->reg + 1, &reg);
  641. if (err)
  642. return UINT64_MAX;
  643. high = reg;
  644. }
  645. break;
  646. case BANK0:
  647. case BANK1:
  648. _mv88e6xxx_stats_read(chip, s->reg, &low);
  649. if (s->sizeof_stat == 8)
  650. _mv88e6xxx_stats_read(chip, s->reg + 1, &high);
  651. }
  652. value = (((u64)high) << 16) | low;
  653. return value;
  654. }
  655. static void mv88e6xxx_get_strings(struct dsa_switch *ds, int port,
  656. uint8_t *data)
  657. {
  658. struct mv88e6xxx_chip *chip = ds->priv;
  659. struct mv88e6xxx_hw_stat *stat;
  660. int i, j;
  661. for (i = 0, j = 0; i < ARRAY_SIZE(mv88e6xxx_hw_stats); i++) {
  662. stat = &mv88e6xxx_hw_stats[i];
  663. if (mv88e6xxx_has_stat(chip, stat)) {
  664. memcpy(data + j * ETH_GSTRING_LEN, stat->string,
  665. ETH_GSTRING_LEN);
  666. j++;
  667. }
  668. }
  669. }
  670. static int mv88e6xxx_get_sset_count(struct dsa_switch *ds)
  671. {
  672. struct mv88e6xxx_chip *chip = ds->priv;
  673. struct mv88e6xxx_hw_stat *stat;
  674. int i, j;
  675. for (i = 0, j = 0; i < ARRAY_SIZE(mv88e6xxx_hw_stats); i++) {
  676. stat = &mv88e6xxx_hw_stats[i];
  677. if (mv88e6xxx_has_stat(chip, stat))
  678. j++;
  679. }
  680. return j;
  681. }
  682. static void mv88e6xxx_get_ethtool_stats(struct dsa_switch *ds, int port,
  683. uint64_t *data)
  684. {
  685. struct mv88e6xxx_chip *chip = ds->priv;
  686. struct mv88e6xxx_hw_stat *stat;
  687. int ret;
  688. int i, j;
  689. mutex_lock(&chip->reg_lock);
  690. ret = _mv88e6xxx_stats_snapshot(chip, port);
  691. if (ret < 0) {
  692. mutex_unlock(&chip->reg_lock);
  693. return;
  694. }
  695. for (i = 0, j = 0; i < ARRAY_SIZE(mv88e6xxx_hw_stats); i++) {
  696. stat = &mv88e6xxx_hw_stats[i];
  697. if (mv88e6xxx_has_stat(chip, stat)) {
  698. data[j] = _mv88e6xxx_get_ethtool_stat(chip, stat, port);
  699. j++;
  700. }
  701. }
  702. mutex_unlock(&chip->reg_lock);
  703. }
  704. static int mv88e6xxx_get_regs_len(struct dsa_switch *ds, int port)
  705. {
  706. return 32 * sizeof(u16);
  707. }
  708. static void mv88e6xxx_get_regs(struct dsa_switch *ds, int port,
  709. struct ethtool_regs *regs, void *_p)
  710. {
  711. struct mv88e6xxx_chip *chip = ds->priv;
  712. int err;
  713. u16 reg;
  714. u16 *p = _p;
  715. int i;
  716. regs->version = 0;
  717. memset(p, 0xff, 32 * sizeof(u16));
  718. mutex_lock(&chip->reg_lock);
  719. for (i = 0; i < 32; i++) {
  720. err = mv88e6xxx_port_read(chip, port, i, &reg);
  721. if (!err)
  722. p[i] = reg;
  723. }
  724. mutex_unlock(&chip->reg_lock);
  725. }
  726. static int _mv88e6xxx_atu_wait(struct mv88e6xxx_chip *chip)
  727. {
  728. return mv88e6xxx_g1_wait(chip, GLOBAL_ATU_OP, GLOBAL_ATU_OP_BUSY);
  729. }
  730. static int mv88e6xxx_get_eee(struct dsa_switch *ds, int port,
  731. struct ethtool_eee *e)
  732. {
  733. struct mv88e6xxx_chip *chip = ds->priv;
  734. u16 reg;
  735. int err;
  736. if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_EEE))
  737. return -EOPNOTSUPP;
  738. mutex_lock(&chip->reg_lock);
  739. err = mv88e6xxx_phy_read(chip, port, 16, &reg);
  740. if (err)
  741. goto out;
  742. e->eee_enabled = !!(reg & 0x0200);
  743. e->tx_lpi_enabled = !!(reg & 0x0100);
  744. err = mv88e6xxx_port_read(chip, port, PORT_STATUS, &reg);
  745. if (err)
  746. goto out;
  747. e->eee_active = !!(reg & PORT_STATUS_EEE);
  748. out:
  749. mutex_unlock(&chip->reg_lock);
  750. return err;
  751. }
  752. static int mv88e6xxx_set_eee(struct dsa_switch *ds, int port,
  753. struct phy_device *phydev, struct ethtool_eee *e)
  754. {
  755. struct mv88e6xxx_chip *chip = ds->priv;
  756. u16 reg;
  757. int err;
  758. if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_EEE))
  759. return -EOPNOTSUPP;
  760. mutex_lock(&chip->reg_lock);
  761. err = mv88e6xxx_phy_read(chip, port, 16, &reg);
  762. if (err)
  763. goto out;
  764. reg &= ~0x0300;
  765. if (e->eee_enabled)
  766. reg |= 0x0200;
  767. if (e->tx_lpi_enabled)
  768. reg |= 0x0100;
  769. err = mv88e6xxx_phy_write(chip, port, 16, reg);
  770. out:
  771. mutex_unlock(&chip->reg_lock);
  772. return err;
  773. }
  774. static int _mv88e6xxx_atu_cmd(struct mv88e6xxx_chip *chip, u16 fid, u16 cmd)
  775. {
  776. u16 val;
  777. int err;
  778. if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_G1_ATU_FID)) {
  779. err = mv88e6xxx_g1_write(chip, GLOBAL_ATU_FID, fid);
  780. if (err)
  781. return err;
  782. } else if (mv88e6xxx_num_databases(chip) == 256) {
  783. /* ATU DBNum[7:4] are located in ATU Control 15:12 */
  784. err = mv88e6xxx_g1_read(chip, GLOBAL_ATU_CONTROL, &val);
  785. if (err)
  786. return err;
  787. err = mv88e6xxx_g1_write(chip, GLOBAL_ATU_CONTROL,
  788. (val & 0xfff) | ((fid << 8) & 0xf000));
  789. if (err)
  790. return err;
  791. /* ATU DBNum[3:0] are located in ATU Operation 3:0 */
  792. cmd |= fid & 0xf;
  793. }
  794. err = mv88e6xxx_g1_write(chip, GLOBAL_ATU_OP, cmd);
  795. if (err)
  796. return err;
  797. return _mv88e6xxx_atu_wait(chip);
  798. }
  799. static int _mv88e6xxx_atu_data_write(struct mv88e6xxx_chip *chip,
  800. struct mv88e6xxx_atu_entry *entry)
  801. {
  802. u16 data = entry->state & GLOBAL_ATU_DATA_STATE_MASK;
  803. if (entry->state != GLOBAL_ATU_DATA_STATE_UNUSED) {
  804. unsigned int mask, shift;
  805. if (entry->trunk) {
  806. data |= GLOBAL_ATU_DATA_TRUNK;
  807. mask = GLOBAL_ATU_DATA_TRUNK_ID_MASK;
  808. shift = GLOBAL_ATU_DATA_TRUNK_ID_SHIFT;
  809. } else {
  810. mask = GLOBAL_ATU_DATA_PORT_VECTOR_MASK;
  811. shift = GLOBAL_ATU_DATA_PORT_VECTOR_SHIFT;
  812. }
  813. data |= (entry->portv_trunkid << shift) & mask;
  814. }
  815. return mv88e6xxx_g1_write(chip, GLOBAL_ATU_DATA, data);
  816. }
  817. static int _mv88e6xxx_atu_flush_move(struct mv88e6xxx_chip *chip,
  818. struct mv88e6xxx_atu_entry *entry,
  819. bool static_too)
  820. {
  821. int op;
  822. int err;
  823. err = _mv88e6xxx_atu_wait(chip);
  824. if (err)
  825. return err;
  826. err = _mv88e6xxx_atu_data_write(chip, entry);
  827. if (err)
  828. return err;
  829. if (entry->fid) {
  830. op = static_too ? GLOBAL_ATU_OP_FLUSH_MOVE_ALL_DB :
  831. GLOBAL_ATU_OP_FLUSH_MOVE_NON_STATIC_DB;
  832. } else {
  833. op = static_too ? GLOBAL_ATU_OP_FLUSH_MOVE_ALL :
  834. GLOBAL_ATU_OP_FLUSH_MOVE_NON_STATIC;
  835. }
  836. return _mv88e6xxx_atu_cmd(chip, entry->fid, op);
  837. }
  838. static int _mv88e6xxx_atu_flush(struct mv88e6xxx_chip *chip,
  839. u16 fid, bool static_too)
  840. {
  841. struct mv88e6xxx_atu_entry entry = {
  842. .fid = fid,
  843. .state = 0, /* EntryState bits must be 0 */
  844. };
  845. return _mv88e6xxx_atu_flush_move(chip, &entry, static_too);
  846. }
  847. static int _mv88e6xxx_atu_move(struct mv88e6xxx_chip *chip, u16 fid,
  848. int from_port, int to_port, bool static_too)
  849. {
  850. struct mv88e6xxx_atu_entry entry = {
  851. .trunk = false,
  852. .fid = fid,
  853. };
  854. /* EntryState bits must be 0xF */
  855. entry.state = GLOBAL_ATU_DATA_STATE_MASK;
  856. /* ToPort and FromPort are respectively in PortVec bits 7:4 and 3:0 */
  857. entry.portv_trunkid = (to_port & 0x0f) << 4;
  858. entry.portv_trunkid |= from_port & 0x0f;
  859. return _mv88e6xxx_atu_flush_move(chip, &entry, static_too);
  860. }
  861. static int _mv88e6xxx_atu_remove(struct mv88e6xxx_chip *chip, u16 fid,
  862. int port, bool static_too)
  863. {
  864. /* Destination port 0xF means remove the entries */
  865. return _mv88e6xxx_atu_move(chip, fid, port, 0x0f, static_too);
  866. }
  867. static const char * const mv88e6xxx_port_state_names[] = {
  868. [PORT_CONTROL_STATE_DISABLED] = "Disabled",
  869. [PORT_CONTROL_STATE_BLOCKING] = "Blocking/Listening",
  870. [PORT_CONTROL_STATE_LEARNING] = "Learning",
  871. [PORT_CONTROL_STATE_FORWARDING] = "Forwarding",
  872. };
  873. static int _mv88e6xxx_port_state(struct mv88e6xxx_chip *chip, int port,
  874. u8 state)
  875. {
  876. struct dsa_switch *ds = chip->ds;
  877. u16 reg;
  878. int err;
  879. u8 oldstate;
  880. err = mv88e6xxx_port_read(chip, port, PORT_CONTROL, &reg);
  881. if (err)
  882. return err;
  883. oldstate = reg & PORT_CONTROL_STATE_MASK;
  884. reg &= ~PORT_CONTROL_STATE_MASK;
  885. reg |= state;
  886. err = mv88e6xxx_port_write(chip, port, PORT_CONTROL, reg);
  887. if (err)
  888. return err;
  889. netdev_dbg(ds->ports[port].netdev, "PortState %s (was %s)\n",
  890. mv88e6xxx_port_state_names[state],
  891. mv88e6xxx_port_state_names[oldstate]);
  892. return 0;
  893. }
  894. static int _mv88e6xxx_port_based_vlan_map(struct mv88e6xxx_chip *chip, int port)
  895. {
  896. struct net_device *bridge = chip->ports[port].bridge_dev;
  897. const u16 mask = (1 << mv88e6xxx_num_ports(chip)) - 1;
  898. struct dsa_switch *ds = chip->ds;
  899. u16 output_ports = 0;
  900. u16 reg;
  901. int err;
  902. int i;
  903. /* allow CPU port or DSA link(s) to send frames to every port */
  904. if (dsa_is_cpu_port(ds, port) || dsa_is_dsa_port(ds, port)) {
  905. output_ports = mask;
  906. } else {
  907. for (i = 0; i < mv88e6xxx_num_ports(chip); ++i) {
  908. /* allow sending frames to every group member */
  909. if (bridge && chip->ports[i].bridge_dev == bridge)
  910. output_ports |= BIT(i);
  911. /* allow sending frames to CPU port and DSA link(s) */
  912. if (dsa_is_cpu_port(ds, i) || dsa_is_dsa_port(ds, i))
  913. output_ports |= BIT(i);
  914. }
  915. }
  916. /* prevent frames from going back out of the port they came in on */
  917. output_ports &= ~BIT(port);
  918. err = mv88e6xxx_port_read(chip, port, PORT_BASE_VLAN, &reg);
  919. if (err)
  920. return err;
  921. reg &= ~mask;
  922. reg |= output_ports & mask;
  923. return mv88e6xxx_port_write(chip, port, PORT_BASE_VLAN, reg);
  924. }
  925. static void mv88e6xxx_port_stp_state_set(struct dsa_switch *ds, int port,
  926. u8 state)
  927. {
  928. struct mv88e6xxx_chip *chip = ds->priv;
  929. int stp_state;
  930. int err;
  931. switch (state) {
  932. case BR_STATE_DISABLED:
  933. stp_state = PORT_CONTROL_STATE_DISABLED;
  934. break;
  935. case BR_STATE_BLOCKING:
  936. case BR_STATE_LISTENING:
  937. stp_state = PORT_CONTROL_STATE_BLOCKING;
  938. break;
  939. case BR_STATE_LEARNING:
  940. stp_state = PORT_CONTROL_STATE_LEARNING;
  941. break;
  942. case BR_STATE_FORWARDING:
  943. default:
  944. stp_state = PORT_CONTROL_STATE_FORWARDING;
  945. break;
  946. }
  947. mutex_lock(&chip->reg_lock);
  948. err = _mv88e6xxx_port_state(chip, port, stp_state);
  949. mutex_unlock(&chip->reg_lock);
  950. if (err)
  951. netdev_err(ds->ports[port].netdev,
  952. "failed to update state to %s\n",
  953. mv88e6xxx_port_state_names[stp_state]);
  954. }
  955. static void mv88e6xxx_port_fast_age(struct dsa_switch *ds, int port)
  956. {
  957. struct mv88e6xxx_chip *chip = ds->priv;
  958. int err;
  959. mutex_lock(&chip->reg_lock);
  960. err = _mv88e6xxx_atu_remove(chip, 0, port, false);
  961. mutex_unlock(&chip->reg_lock);
  962. if (err)
  963. netdev_err(ds->ports[port].netdev, "failed to flush ATU\n");
  964. }
  965. static int _mv88e6xxx_port_pvid(struct mv88e6xxx_chip *chip, int port,
  966. u16 *new, u16 *old)
  967. {
  968. struct dsa_switch *ds = chip->ds;
  969. u16 pvid, reg;
  970. int err;
  971. err = mv88e6xxx_port_read(chip, port, PORT_DEFAULT_VLAN, &reg);
  972. if (err)
  973. return err;
  974. pvid = reg & PORT_DEFAULT_VLAN_MASK;
  975. if (new) {
  976. reg &= ~PORT_DEFAULT_VLAN_MASK;
  977. reg |= *new & PORT_DEFAULT_VLAN_MASK;
  978. err = mv88e6xxx_port_write(chip, port, PORT_DEFAULT_VLAN, reg);
  979. if (err)
  980. return err;
  981. netdev_dbg(ds->ports[port].netdev,
  982. "DefaultVID %d (was %d)\n", *new, pvid);
  983. }
  984. if (old)
  985. *old = pvid;
  986. return 0;
  987. }
  988. static int _mv88e6xxx_port_pvid_get(struct mv88e6xxx_chip *chip,
  989. int port, u16 *pvid)
  990. {
  991. return _mv88e6xxx_port_pvid(chip, port, NULL, pvid);
  992. }
  993. static int _mv88e6xxx_port_pvid_set(struct mv88e6xxx_chip *chip,
  994. int port, u16 pvid)
  995. {
  996. return _mv88e6xxx_port_pvid(chip, port, &pvid, NULL);
  997. }
  998. static int _mv88e6xxx_vtu_wait(struct mv88e6xxx_chip *chip)
  999. {
  1000. return mv88e6xxx_g1_wait(chip, GLOBAL_VTU_OP, GLOBAL_VTU_OP_BUSY);
  1001. }
  1002. static int _mv88e6xxx_vtu_cmd(struct mv88e6xxx_chip *chip, u16 op)
  1003. {
  1004. int err;
  1005. err = mv88e6xxx_g1_write(chip, GLOBAL_VTU_OP, op);
  1006. if (err)
  1007. return err;
  1008. return _mv88e6xxx_vtu_wait(chip);
  1009. }
  1010. static int _mv88e6xxx_vtu_stu_flush(struct mv88e6xxx_chip *chip)
  1011. {
  1012. int ret;
  1013. ret = _mv88e6xxx_vtu_wait(chip);
  1014. if (ret < 0)
  1015. return ret;
  1016. return _mv88e6xxx_vtu_cmd(chip, GLOBAL_VTU_OP_FLUSH_ALL);
  1017. }
  1018. static int _mv88e6xxx_vtu_stu_data_read(struct mv88e6xxx_chip *chip,
  1019. struct mv88e6xxx_vtu_entry *entry,
  1020. unsigned int nibble_offset)
  1021. {
  1022. u16 regs[3];
  1023. int i, err;
  1024. for (i = 0; i < 3; ++i) {
  1025. u16 *reg = &regs[i];
  1026. err = mv88e6xxx_g1_read(chip, GLOBAL_VTU_DATA_0_3 + i, reg);
  1027. if (err)
  1028. return err;
  1029. }
  1030. for (i = 0; i < mv88e6xxx_num_ports(chip); ++i) {
  1031. unsigned int shift = (i % 4) * 4 + nibble_offset;
  1032. u16 reg = regs[i / 4];
  1033. entry->data[i] = (reg >> shift) & GLOBAL_VTU_STU_DATA_MASK;
  1034. }
  1035. return 0;
  1036. }
  1037. static int mv88e6xxx_vtu_data_read(struct mv88e6xxx_chip *chip,
  1038. struct mv88e6xxx_vtu_entry *entry)
  1039. {
  1040. return _mv88e6xxx_vtu_stu_data_read(chip, entry, 0);
  1041. }
  1042. static int mv88e6xxx_stu_data_read(struct mv88e6xxx_chip *chip,
  1043. struct mv88e6xxx_vtu_entry *entry)
  1044. {
  1045. return _mv88e6xxx_vtu_stu_data_read(chip, entry, 2);
  1046. }
  1047. static int _mv88e6xxx_vtu_stu_data_write(struct mv88e6xxx_chip *chip,
  1048. struct mv88e6xxx_vtu_entry *entry,
  1049. unsigned int nibble_offset)
  1050. {
  1051. u16 regs[3] = { 0 };
  1052. int i, err;
  1053. for (i = 0; i < mv88e6xxx_num_ports(chip); ++i) {
  1054. unsigned int shift = (i % 4) * 4 + nibble_offset;
  1055. u8 data = entry->data[i];
  1056. regs[i / 4] |= (data & GLOBAL_VTU_STU_DATA_MASK) << shift;
  1057. }
  1058. for (i = 0; i < 3; ++i) {
  1059. u16 reg = regs[i];
  1060. err = mv88e6xxx_g1_write(chip, GLOBAL_VTU_DATA_0_3 + i, reg);
  1061. if (err)
  1062. return err;
  1063. }
  1064. return 0;
  1065. }
  1066. static int mv88e6xxx_vtu_data_write(struct mv88e6xxx_chip *chip,
  1067. struct mv88e6xxx_vtu_entry *entry)
  1068. {
  1069. return _mv88e6xxx_vtu_stu_data_write(chip, entry, 0);
  1070. }
  1071. static int mv88e6xxx_stu_data_write(struct mv88e6xxx_chip *chip,
  1072. struct mv88e6xxx_vtu_entry *entry)
  1073. {
  1074. return _mv88e6xxx_vtu_stu_data_write(chip, entry, 2);
  1075. }
  1076. static int _mv88e6xxx_vtu_vid_write(struct mv88e6xxx_chip *chip, u16 vid)
  1077. {
  1078. return mv88e6xxx_g1_write(chip, GLOBAL_VTU_VID,
  1079. vid & GLOBAL_VTU_VID_MASK);
  1080. }
  1081. static int _mv88e6xxx_vtu_getnext(struct mv88e6xxx_chip *chip,
  1082. struct mv88e6xxx_vtu_entry *entry)
  1083. {
  1084. struct mv88e6xxx_vtu_entry next = { 0 };
  1085. u16 val;
  1086. int err;
  1087. err = _mv88e6xxx_vtu_wait(chip);
  1088. if (err)
  1089. return err;
  1090. err = _mv88e6xxx_vtu_cmd(chip, GLOBAL_VTU_OP_VTU_GET_NEXT);
  1091. if (err)
  1092. return err;
  1093. err = mv88e6xxx_g1_read(chip, GLOBAL_VTU_VID, &val);
  1094. if (err)
  1095. return err;
  1096. next.vid = val & GLOBAL_VTU_VID_MASK;
  1097. next.valid = !!(val & GLOBAL_VTU_VID_VALID);
  1098. if (next.valid) {
  1099. err = mv88e6xxx_vtu_data_read(chip, &next);
  1100. if (err)
  1101. return err;
  1102. if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_G1_VTU_FID)) {
  1103. err = mv88e6xxx_g1_read(chip, GLOBAL_VTU_FID, &val);
  1104. if (err)
  1105. return err;
  1106. next.fid = val & GLOBAL_VTU_FID_MASK;
  1107. } else if (mv88e6xxx_num_databases(chip) == 256) {
  1108. /* VTU DBNum[7:4] are located in VTU Operation 11:8, and
  1109. * VTU DBNum[3:0] are located in VTU Operation 3:0
  1110. */
  1111. err = mv88e6xxx_g1_read(chip, GLOBAL_VTU_OP, &val);
  1112. if (err)
  1113. return err;
  1114. next.fid = (val & 0xf00) >> 4;
  1115. next.fid |= val & 0xf;
  1116. }
  1117. if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_STU)) {
  1118. err = mv88e6xxx_g1_read(chip, GLOBAL_VTU_SID, &val);
  1119. if (err)
  1120. return err;
  1121. next.sid = val & GLOBAL_VTU_SID_MASK;
  1122. }
  1123. }
  1124. *entry = next;
  1125. return 0;
  1126. }
  1127. static int mv88e6xxx_port_vlan_dump(struct dsa_switch *ds, int port,
  1128. struct switchdev_obj_port_vlan *vlan,
  1129. int (*cb)(struct switchdev_obj *obj))
  1130. {
  1131. struct mv88e6xxx_chip *chip = ds->priv;
  1132. struct mv88e6xxx_vtu_entry next;
  1133. u16 pvid;
  1134. int err;
  1135. if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_VTU))
  1136. return -EOPNOTSUPP;
  1137. mutex_lock(&chip->reg_lock);
  1138. err = _mv88e6xxx_port_pvid_get(chip, port, &pvid);
  1139. if (err)
  1140. goto unlock;
  1141. err = _mv88e6xxx_vtu_vid_write(chip, GLOBAL_VTU_VID_MASK);
  1142. if (err)
  1143. goto unlock;
  1144. do {
  1145. err = _mv88e6xxx_vtu_getnext(chip, &next);
  1146. if (err)
  1147. break;
  1148. if (!next.valid)
  1149. break;
  1150. if (next.data[port] == GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER)
  1151. continue;
  1152. /* reinit and dump this VLAN obj */
  1153. vlan->vid_begin = next.vid;
  1154. vlan->vid_end = next.vid;
  1155. vlan->flags = 0;
  1156. if (next.data[port] == GLOBAL_VTU_DATA_MEMBER_TAG_UNTAGGED)
  1157. vlan->flags |= BRIDGE_VLAN_INFO_UNTAGGED;
  1158. if (next.vid == pvid)
  1159. vlan->flags |= BRIDGE_VLAN_INFO_PVID;
  1160. err = cb(&vlan->obj);
  1161. if (err)
  1162. break;
  1163. } while (next.vid < GLOBAL_VTU_VID_MASK);
  1164. unlock:
  1165. mutex_unlock(&chip->reg_lock);
  1166. return err;
  1167. }
  1168. static int _mv88e6xxx_vtu_loadpurge(struct mv88e6xxx_chip *chip,
  1169. struct mv88e6xxx_vtu_entry *entry)
  1170. {
  1171. u16 op = GLOBAL_VTU_OP_VTU_LOAD_PURGE;
  1172. u16 reg = 0;
  1173. int err;
  1174. err = _mv88e6xxx_vtu_wait(chip);
  1175. if (err)
  1176. return err;
  1177. if (!entry->valid)
  1178. goto loadpurge;
  1179. /* Write port member tags */
  1180. err = mv88e6xxx_vtu_data_write(chip, entry);
  1181. if (err)
  1182. return err;
  1183. if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_STU)) {
  1184. reg = entry->sid & GLOBAL_VTU_SID_MASK;
  1185. err = mv88e6xxx_g1_write(chip, GLOBAL_VTU_SID, reg);
  1186. if (err)
  1187. return err;
  1188. }
  1189. if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_G1_VTU_FID)) {
  1190. reg = entry->fid & GLOBAL_VTU_FID_MASK;
  1191. err = mv88e6xxx_g1_write(chip, GLOBAL_VTU_FID, reg);
  1192. if (err)
  1193. return err;
  1194. } else if (mv88e6xxx_num_databases(chip) == 256) {
  1195. /* VTU DBNum[7:4] are located in VTU Operation 11:8, and
  1196. * VTU DBNum[3:0] are located in VTU Operation 3:0
  1197. */
  1198. op |= (entry->fid & 0xf0) << 8;
  1199. op |= entry->fid & 0xf;
  1200. }
  1201. reg = GLOBAL_VTU_VID_VALID;
  1202. loadpurge:
  1203. reg |= entry->vid & GLOBAL_VTU_VID_MASK;
  1204. err = mv88e6xxx_g1_write(chip, GLOBAL_VTU_VID, reg);
  1205. if (err)
  1206. return err;
  1207. return _mv88e6xxx_vtu_cmd(chip, op);
  1208. }
  1209. static int _mv88e6xxx_stu_getnext(struct mv88e6xxx_chip *chip, u8 sid,
  1210. struct mv88e6xxx_vtu_entry *entry)
  1211. {
  1212. struct mv88e6xxx_vtu_entry next = { 0 };
  1213. u16 val;
  1214. int err;
  1215. err = _mv88e6xxx_vtu_wait(chip);
  1216. if (err)
  1217. return err;
  1218. err = mv88e6xxx_g1_write(chip, GLOBAL_VTU_SID,
  1219. sid & GLOBAL_VTU_SID_MASK);
  1220. if (err)
  1221. return err;
  1222. err = _mv88e6xxx_vtu_cmd(chip, GLOBAL_VTU_OP_STU_GET_NEXT);
  1223. if (err)
  1224. return err;
  1225. err = mv88e6xxx_g1_read(chip, GLOBAL_VTU_SID, &val);
  1226. if (err)
  1227. return err;
  1228. next.sid = val & GLOBAL_VTU_SID_MASK;
  1229. err = mv88e6xxx_g1_read(chip, GLOBAL_VTU_VID, &val);
  1230. if (err)
  1231. return err;
  1232. next.valid = !!(val & GLOBAL_VTU_VID_VALID);
  1233. if (next.valid) {
  1234. err = mv88e6xxx_stu_data_read(chip, &next);
  1235. if (err)
  1236. return err;
  1237. }
  1238. *entry = next;
  1239. return 0;
  1240. }
  1241. static int _mv88e6xxx_stu_loadpurge(struct mv88e6xxx_chip *chip,
  1242. struct mv88e6xxx_vtu_entry *entry)
  1243. {
  1244. u16 reg = 0;
  1245. int err;
  1246. err = _mv88e6xxx_vtu_wait(chip);
  1247. if (err)
  1248. return err;
  1249. if (!entry->valid)
  1250. goto loadpurge;
  1251. /* Write port states */
  1252. err = mv88e6xxx_stu_data_write(chip, entry);
  1253. if (err)
  1254. return err;
  1255. reg = GLOBAL_VTU_VID_VALID;
  1256. loadpurge:
  1257. err = mv88e6xxx_g1_write(chip, GLOBAL_VTU_VID, reg);
  1258. if (err)
  1259. return err;
  1260. reg = entry->sid & GLOBAL_VTU_SID_MASK;
  1261. err = mv88e6xxx_g1_write(chip, GLOBAL_VTU_SID, reg);
  1262. if (err)
  1263. return err;
  1264. return _mv88e6xxx_vtu_cmd(chip, GLOBAL_VTU_OP_STU_LOAD_PURGE);
  1265. }
  1266. static int _mv88e6xxx_port_fid(struct mv88e6xxx_chip *chip, int port,
  1267. u16 *new, u16 *old)
  1268. {
  1269. struct dsa_switch *ds = chip->ds;
  1270. u16 upper_mask;
  1271. u16 fid;
  1272. u16 reg;
  1273. int err;
  1274. if (mv88e6xxx_num_databases(chip) == 4096)
  1275. upper_mask = 0xff;
  1276. else if (mv88e6xxx_num_databases(chip) == 256)
  1277. upper_mask = 0xf;
  1278. else
  1279. return -EOPNOTSUPP;
  1280. /* Port's default FID bits 3:0 are located in reg 0x06, offset 12 */
  1281. err = mv88e6xxx_port_read(chip, port, PORT_BASE_VLAN, &reg);
  1282. if (err)
  1283. return err;
  1284. fid = (reg & PORT_BASE_VLAN_FID_3_0_MASK) >> 12;
  1285. if (new) {
  1286. reg &= ~PORT_BASE_VLAN_FID_3_0_MASK;
  1287. reg |= (*new << 12) & PORT_BASE_VLAN_FID_3_0_MASK;
  1288. err = mv88e6xxx_port_write(chip, port, PORT_BASE_VLAN, reg);
  1289. if (err)
  1290. return err;
  1291. }
  1292. /* Port's default FID bits 11:4 are located in reg 0x05, offset 0 */
  1293. err = mv88e6xxx_port_read(chip, port, PORT_CONTROL_1, &reg);
  1294. if (err)
  1295. return err;
  1296. fid |= (reg & upper_mask) << 4;
  1297. if (new) {
  1298. reg &= ~upper_mask;
  1299. reg |= (*new >> 4) & upper_mask;
  1300. err = mv88e6xxx_port_write(chip, port, PORT_CONTROL_1, reg);
  1301. if (err)
  1302. return err;
  1303. netdev_dbg(ds->ports[port].netdev,
  1304. "FID %d (was %d)\n", *new, fid);
  1305. }
  1306. if (old)
  1307. *old = fid;
  1308. return 0;
  1309. }
  1310. static int _mv88e6xxx_port_fid_get(struct mv88e6xxx_chip *chip,
  1311. int port, u16 *fid)
  1312. {
  1313. return _mv88e6xxx_port_fid(chip, port, NULL, fid);
  1314. }
  1315. static int _mv88e6xxx_port_fid_set(struct mv88e6xxx_chip *chip,
  1316. int port, u16 fid)
  1317. {
  1318. return _mv88e6xxx_port_fid(chip, port, &fid, NULL);
  1319. }
  1320. static int _mv88e6xxx_fid_new(struct mv88e6xxx_chip *chip, u16 *fid)
  1321. {
  1322. DECLARE_BITMAP(fid_bitmap, MV88E6XXX_N_FID);
  1323. struct mv88e6xxx_vtu_entry vlan;
  1324. int i, err;
  1325. bitmap_zero(fid_bitmap, MV88E6XXX_N_FID);
  1326. /* Set every FID bit used by the (un)bridged ports */
  1327. for (i = 0; i < mv88e6xxx_num_ports(chip); ++i) {
  1328. err = _mv88e6xxx_port_fid_get(chip, i, fid);
  1329. if (err)
  1330. return err;
  1331. set_bit(*fid, fid_bitmap);
  1332. }
  1333. /* Set every FID bit used by the VLAN entries */
  1334. err = _mv88e6xxx_vtu_vid_write(chip, GLOBAL_VTU_VID_MASK);
  1335. if (err)
  1336. return err;
  1337. do {
  1338. err = _mv88e6xxx_vtu_getnext(chip, &vlan);
  1339. if (err)
  1340. return err;
  1341. if (!vlan.valid)
  1342. break;
  1343. set_bit(vlan.fid, fid_bitmap);
  1344. } while (vlan.vid < GLOBAL_VTU_VID_MASK);
  1345. /* The reset value 0x000 is used to indicate that multiple address
  1346. * databases are not needed. Return the next positive available.
  1347. */
  1348. *fid = find_next_zero_bit(fid_bitmap, MV88E6XXX_N_FID, 1);
  1349. if (unlikely(*fid >= mv88e6xxx_num_databases(chip)))
  1350. return -ENOSPC;
  1351. /* Clear the database */
  1352. return _mv88e6xxx_atu_flush(chip, *fid, true);
  1353. }
  1354. static int _mv88e6xxx_vtu_new(struct mv88e6xxx_chip *chip, u16 vid,
  1355. struct mv88e6xxx_vtu_entry *entry)
  1356. {
  1357. struct dsa_switch *ds = chip->ds;
  1358. struct mv88e6xxx_vtu_entry vlan = {
  1359. .valid = true,
  1360. .vid = vid,
  1361. };
  1362. int i, err;
  1363. err = _mv88e6xxx_fid_new(chip, &vlan.fid);
  1364. if (err)
  1365. return err;
  1366. /* exclude all ports except the CPU and DSA ports */
  1367. for (i = 0; i < mv88e6xxx_num_ports(chip); ++i)
  1368. vlan.data[i] = dsa_is_cpu_port(ds, i) || dsa_is_dsa_port(ds, i)
  1369. ? GLOBAL_VTU_DATA_MEMBER_TAG_UNMODIFIED
  1370. : GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER;
  1371. if (mv88e6xxx_6097_family(chip) || mv88e6xxx_6165_family(chip) ||
  1372. mv88e6xxx_6351_family(chip) || mv88e6xxx_6352_family(chip)) {
  1373. struct mv88e6xxx_vtu_entry vstp;
  1374. /* Adding a VTU entry requires a valid STU entry. As VSTP is not
  1375. * implemented, only one STU entry is needed to cover all VTU
  1376. * entries. Thus, validate the SID 0.
  1377. */
  1378. vlan.sid = 0;
  1379. err = _mv88e6xxx_stu_getnext(chip, GLOBAL_VTU_SID_MASK, &vstp);
  1380. if (err)
  1381. return err;
  1382. if (vstp.sid != vlan.sid || !vstp.valid) {
  1383. memset(&vstp, 0, sizeof(vstp));
  1384. vstp.valid = true;
  1385. vstp.sid = vlan.sid;
  1386. err = _mv88e6xxx_stu_loadpurge(chip, &vstp);
  1387. if (err)
  1388. return err;
  1389. }
  1390. }
  1391. *entry = vlan;
  1392. return 0;
  1393. }
  1394. static int _mv88e6xxx_vtu_get(struct mv88e6xxx_chip *chip, u16 vid,
  1395. struct mv88e6xxx_vtu_entry *entry, bool creat)
  1396. {
  1397. int err;
  1398. if (!vid)
  1399. return -EINVAL;
  1400. err = _mv88e6xxx_vtu_vid_write(chip, vid - 1);
  1401. if (err)
  1402. return err;
  1403. err = _mv88e6xxx_vtu_getnext(chip, entry);
  1404. if (err)
  1405. return err;
  1406. if (entry->vid != vid || !entry->valid) {
  1407. if (!creat)
  1408. return -EOPNOTSUPP;
  1409. /* -ENOENT would've been more appropriate, but switchdev expects
  1410. * -EOPNOTSUPP to inform bridge about an eventual software VLAN.
  1411. */
  1412. err = _mv88e6xxx_vtu_new(chip, vid, entry);
  1413. }
  1414. return err;
  1415. }
  1416. static int mv88e6xxx_port_check_hw_vlan(struct dsa_switch *ds, int port,
  1417. u16 vid_begin, u16 vid_end)
  1418. {
  1419. struct mv88e6xxx_chip *chip = ds->priv;
  1420. struct mv88e6xxx_vtu_entry vlan;
  1421. int i, err;
  1422. if (!vid_begin)
  1423. return -EOPNOTSUPP;
  1424. mutex_lock(&chip->reg_lock);
  1425. err = _mv88e6xxx_vtu_vid_write(chip, vid_begin - 1);
  1426. if (err)
  1427. goto unlock;
  1428. do {
  1429. err = _mv88e6xxx_vtu_getnext(chip, &vlan);
  1430. if (err)
  1431. goto unlock;
  1432. if (!vlan.valid)
  1433. break;
  1434. if (vlan.vid > vid_end)
  1435. break;
  1436. for (i = 0; i < mv88e6xxx_num_ports(chip); ++i) {
  1437. if (dsa_is_dsa_port(ds, i) || dsa_is_cpu_port(ds, i))
  1438. continue;
  1439. if (vlan.data[i] ==
  1440. GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER)
  1441. continue;
  1442. if (chip->ports[i].bridge_dev ==
  1443. chip->ports[port].bridge_dev)
  1444. break; /* same bridge, check next VLAN */
  1445. netdev_warn(ds->ports[port].netdev,
  1446. "hardware VLAN %d already used by %s\n",
  1447. vlan.vid,
  1448. netdev_name(chip->ports[i].bridge_dev));
  1449. err = -EOPNOTSUPP;
  1450. goto unlock;
  1451. }
  1452. } while (vlan.vid < vid_end);
  1453. unlock:
  1454. mutex_unlock(&chip->reg_lock);
  1455. return err;
  1456. }
  1457. static const char * const mv88e6xxx_port_8021q_mode_names[] = {
  1458. [PORT_CONTROL_2_8021Q_DISABLED] = "Disabled",
  1459. [PORT_CONTROL_2_8021Q_FALLBACK] = "Fallback",
  1460. [PORT_CONTROL_2_8021Q_CHECK] = "Check",
  1461. [PORT_CONTROL_2_8021Q_SECURE] = "Secure",
  1462. };
  1463. static int mv88e6xxx_port_vlan_filtering(struct dsa_switch *ds, int port,
  1464. bool vlan_filtering)
  1465. {
  1466. struct mv88e6xxx_chip *chip = ds->priv;
  1467. u16 old, new = vlan_filtering ? PORT_CONTROL_2_8021Q_SECURE :
  1468. PORT_CONTROL_2_8021Q_DISABLED;
  1469. u16 reg;
  1470. int err;
  1471. if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_VTU))
  1472. return -EOPNOTSUPP;
  1473. mutex_lock(&chip->reg_lock);
  1474. err = mv88e6xxx_port_read(chip, port, PORT_CONTROL_2, &reg);
  1475. if (err)
  1476. goto unlock;
  1477. old = reg & PORT_CONTROL_2_8021Q_MASK;
  1478. if (new != old) {
  1479. reg &= ~PORT_CONTROL_2_8021Q_MASK;
  1480. reg |= new & PORT_CONTROL_2_8021Q_MASK;
  1481. err = mv88e6xxx_port_write(chip, port, PORT_CONTROL_2, reg);
  1482. if (err)
  1483. goto unlock;
  1484. netdev_dbg(ds->ports[port].netdev, "802.1Q Mode %s (was %s)\n",
  1485. mv88e6xxx_port_8021q_mode_names[new],
  1486. mv88e6xxx_port_8021q_mode_names[old]);
  1487. }
  1488. err = 0;
  1489. unlock:
  1490. mutex_unlock(&chip->reg_lock);
  1491. return err;
  1492. }
  1493. static int
  1494. mv88e6xxx_port_vlan_prepare(struct dsa_switch *ds, int port,
  1495. const struct switchdev_obj_port_vlan *vlan,
  1496. struct switchdev_trans *trans)
  1497. {
  1498. struct mv88e6xxx_chip *chip = ds->priv;
  1499. int err;
  1500. if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_VTU))
  1501. return -EOPNOTSUPP;
  1502. /* If the requested port doesn't belong to the same bridge as the VLAN
  1503. * members, do not support it (yet) and fallback to software VLAN.
  1504. */
  1505. err = mv88e6xxx_port_check_hw_vlan(ds, port, vlan->vid_begin,
  1506. vlan->vid_end);
  1507. if (err)
  1508. return err;
  1509. /* We don't need any dynamic resource from the kernel (yet),
  1510. * so skip the prepare phase.
  1511. */
  1512. return 0;
  1513. }
  1514. static int _mv88e6xxx_port_vlan_add(struct mv88e6xxx_chip *chip, int port,
  1515. u16 vid, bool untagged)
  1516. {
  1517. struct mv88e6xxx_vtu_entry vlan;
  1518. int err;
  1519. err = _mv88e6xxx_vtu_get(chip, vid, &vlan, true);
  1520. if (err)
  1521. return err;
  1522. vlan.data[port] = untagged ?
  1523. GLOBAL_VTU_DATA_MEMBER_TAG_UNTAGGED :
  1524. GLOBAL_VTU_DATA_MEMBER_TAG_TAGGED;
  1525. return _mv88e6xxx_vtu_loadpurge(chip, &vlan);
  1526. }
  1527. static void mv88e6xxx_port_vlan_add(struct dsa_switch *ds, int port,
  1528. const struct switchdev_obj_port_vlan *vlan,
  1529. struct switchdev_trans *trans)
  1530. {
  1531. struct mv88e6xxx_chip *chip = ds->priv;
  1532. bool untagged = vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED;
  1533. bool pvid = vlan->flags & BRIDGE_VLAN_INFO_PVID;
  1534. u16 vid;
  1535. if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_VTU))
  1536. return;
  1537. mutex_lock(&chip->reg_lock);
  1538. for (vid = vlan->vid_begin; vid <= vlan->vid_end; ++vid)
  1539. if (_mv88e6xxx_port_vlan_add(chip, port, vid, untagged))
  1540. netdev_err(ds->ports[port].netdev,
  1541. "failed to add VLAN %d%c\n",
  1542. vid, untagged ? 'u' : 't');
  1543. if (pvid && _mv88e6xxx_port_pvid_set(chip, port, vlan->vid_end))
  1544. netdev_err(ds->ports[port].netdev, "failed to set PVID %d\n",
  1545. vlan->vid_end);
  1546. mutex_unlock(&chip->reg_lock);
  1547. }
  1548. static int _mv88e6xxx_port_vlan_del(struct mv88e6xxx_chip *chip,
  1549. int port, u16 vid)
  1550. {
  1551. struct dsa_switch *ds = chip->ds;
  1552. struct mv88e6xxx_vtu_entry vlan;
  1553. int i, err;
  1554. err = _mv88e6xxx_vtu_get(chip, vid, &vlan, false);
  1555. if (err)
  1556. return err;
  1557. /* Tell switchdev if this VLAN is handled in software */
  1558. if (vlan.data[port] == GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER)
  1559. return -EOPNOTSUPP;
  1560. vlan.data[port] = GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER;
  1561. /* keep the VLAN unless all ports are excluded */
  1562. vlan.valid = false;
  1563. for (i = 0; i < mv88e6xxx_num_ports(chip); ++i) {
  1564. if (dsa_is_cpu_port(ds, i) || dsa_is_dsa_port(ds, i))
  1565. continue;
  1566. if (vlan.data[i] != GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER) {
  1567. vlan.valid = true;
  1568. break;
  1569. }
  1570. }
  1571. err = _mv88e6xxx_vtu_loadpurge(chip, &vlan);
  1572. if (err)
  1573. return err;
  1574. return _mv88e6xxx_atu_remove(chip, vlan.fid, port, false);
  1575. }
  1576. static int mv88e6xxx_port_vlan_del(struct dsa_switch *ds, int port,
  1577. const struct switchdev_obj_port_vlan *vlan)
  1578. {
  1579. struct mv88e6xxx_chip *chip = ds->priv;
  1580. u16 pvid, vid;
  1581. int err = 0;
  1582. if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_VTU))
  1583. return -EOPNOTSUPP;
  1584. mutex_lock(&chip->reg_lock);
  1585. err = _mv88e6xxx_port_pvid_get(chip, port, &pvid);
  1586. if (err)
  1587. goto unlock;
  1588. for (vid = vlan->vid_begin; vid <= vlan->vid_end; ++vid) {
  1589. err = _mv88e6xxx_port_vlan_del(chip, port, vid);
  1590. if (err)
  1591. goto unlock;
  1592. if (vid == pvid) {
  1593. err = _mv88e6xxx_port_pvid_set(chip, port, 0);
  1594. if (err)
  1595. goto unlock;
  1596. }
  1597. }
  1598. unlock:
  1599. mutex_unlock(&chip->reg_lock);
  1600. return err;
  1601. }
  1602. static int _mv88e6xxx_atu_mac_write(struct mv88e6xxx_chip *chip,
  1603. const unsigned char *addr)
  1604. {
  1605. int i, err;
  1606. for (i = 0; i < 3; i++) {
  1607. err = mv88e6xxx_g1_write(chip, GLOBAL_ATU_MAC_01 + i,
  1608. (addr[i * 2] << 8) | addr[i * 2 + 1]);
  1609. if (err)
  1610. return err;
  1611. }
  1612. return 0;
  1613. }
  1614. static int _mv88e6xxx_atu_mac_read(struct mv88e6xxx_chip *chip,
  1615. unsigned char *addr)
  1616. {
  1617. u16 val;
  1618. int i, err;
  1619. for (i = 0; i < 3; i++) {
  1620. err = mv88e6xxx_g1_read(chip, GLOBAL_ATU_MAC_01 + i, &val);
  1621. if (err)
  1622. return err;
  1623. addr[i * 2] = val >> 8;
  1624. addr[i * 2 + 1] = val & 0xff;
  1625. }
  1626. return 0;
  1627. }
  1628. static int _mv88e6xxx_atu_load(struct mv88e6xxx_chip *chip,
  1629. struct mv88e6xxx_atu_entry *entry)
  1630. {
  1631. int ret;
  1632. ret = _mv88e6xxx_atu_wait(chip);
  1633. if (ret < 0)
  1634. return ret;
  1635. ret = _mv88e6xxx_atu_mac_write(chip, entry->mac);
  1636. if (ret < 0)
  1637. return ret;
  1638. ret = _mv88e6xxx_atu_data_write(chip, entry);
  1639. if (ret < 0)
  1640. return ret;
  1641. return _mv88e6xxx_atu_cmd(chip, entry->fid, GLOBAL_ATU_OP_LOAD_DB);
  1642. }
  1643. static int _mv88e6xxx_atu_getnext(struct mv88e6xxx_chip *chip, u16 fid,
  1644. struct mv88e6xxx_atu_entry *entry);
  1645. static int mv88e6xxx_atu_get(struct mv88e6xxx_chip *chip, int fid,
  1646. const u8 *addr, struct mv88e6xxx_atu_entry *entry)
  1647. {
  1648. struct mv88e6xxx_atu_entry next;
  1649. int err;
  1650. eth_broadcast_addr(next.mac);
  1651. err = _mv88e6xxx_atu_mac_write(chip, next.mac);
  1652. if (err)
  1653. return err;
  1654. do {
  1655. err = _mv88e6xxx_atu_getnext(chip, fid, &next);
  1656. if (err)
  1657. return err;
  1658. if (next.state == GLOBAL_ATU_DATA_STATE_UNUSED)
  1659. break;
  1660. if (ether_addr_equal(next.mac, addr)) {
  1661. *entry = next;
  1662. return 0;
  1663. }
  1664. } while (!is_broadcast_ether_addr(next.mac));
  1665. memset(entry, 0, sizeof(*entry));
  1666. entry->fid = fid;
  1667. ether_addr_copy(entry->mac, addr);
  1668. return 0;
  1669. }
  1670. static int mv88e6xxx_port_db_load_purge(struct mv88e6xxx_chip *chip, int port,
  1671. const unsigned char *addr, u16 vid,
  1672. u8 state)
  1673. {
  1674. struct mv88e6xxx_vtu_entry vlan;
  1675. struct mv88e6xxx_atu_entry entry;
  1676. int err;
  1677. /* Null VLAN ID corresponds to the port private database */
  1678. if (vid == 0)
  1679. err = _mv88e6xxx_port_fid_get(chip, port, &vlan.fid);
  1680. else
  1681. err = _mv88e6xxx_vtu_get(chip, vid, &vlan, false);
  1682. if (err)
  1683. return err;
  1684. err = mv88e6xxx_atu_get(chip, vlan.fid, addr, &entry);
  1685. if (err)
  1686. return err;
  1687. /* Purge the ATU entry only if no port is using it anymore */
  1688. if (state == GLOBAL_ATU_DATA_STATE_UNUSED) {
  1689. entry.portv_trunkid &= ~BIT(port);
  1690. if (!entry.portv_trunkid)
  1691. entry.state = GLOBAL_ATU_DATA_STATE_UNUSED;
  1692. } else {
  1693. entry.portv_trunkid |= BIT(port);
  1694. entry.state = state;
  1695. }
  1696. return _mv88e6xxx_atu_load(chip, &entry);
  1697. }
  1698. static int mv88e6xxx_port_fdb_prepare(struct dsa_switch *ds, int port,
  1699. const struct switchdev_obj_port_fdb *fdb,
  1700. struct switchdev_trans *trans)
  1701. {
  1702. /* We don't need any dynamic resource from the kernel (yet),
  1703. * so skip the prepare phase.
  1704. */
  1705. return 0;
  1706. }
  1707. static void mv88e6xxx_port_fdb_add(struct dsa_switch *ds, int port,
  1708. const struct switchdev_obj_port_fdb *fdb,
  1709. struct switchdev_trans *trans)
  1710. {
  1711. struct mv88e6xxx_chip *chip = ds->priv;
  1712. mutex_lock(&chip->reg_lock);
  1713. if (mv88e6xxx_port_db_load_purge(chip, port, fdb->addr, fdb->vid,
  1714. GLOBAL_ATU_DATA_STATE_UC_STATIC))
  1715. netdev_err(ds->ports[port].netdev, "failed to load unicast MAC address\n");
  1716. mutex_unlock(&chip->reg_lock);
  1717. }
  1718. static int mv88e6xxx_port_fdb_del(struct dsa_switch *ds, int port,
  1719. const struct switchdev_obj_port_fdb *fdb)
  1720. {
  1721. struct mv88e6xxx_chip *chip = ds->priv;
  1722. int err;
  1723. mutex_lock(&chip->reg_lock);
  1724. err = mv88e6xxx_port_db_load_purge(chip, port, fdb->addr, fdb->vid,
  1725. GLOBAL_ATU_DATA_STATE_UNUSED);
  1726. mutex_unlock(&chip->reg_lock);
  1727. return err;
  1728. }
  1729. static int _mv88e6xxx_atu_getnext(struct mv88e6xxx_chip *chip, u16 fid,
  1730. struct mv88e6xxx_atu_entry *entry)
  1731. {
  1732. struct mv88e6xxx_atu_entry next = { 0 };
  1733. u16 val;
  1734. int err;
  1735. next.fid = fid;
  1736. err = _mv88e6xxx_atu_wait(chip);
  1737. if (err)
  1738. return err;
  1739. err = _mv88e6xxx_atu_cmd(chip, fid, GLOBAL_ATU_OP_GET_NEXT_DB);
  1740. if (err)
  1741. return err;
  1742. err = _mv88e6xxx_atu_mac_read(chip, next.mac);
  1743. if (err)
  1744. return err;
  1745. err = mv88e6xxx_g1_read(chip, GLOBAL_ATU_DATA, &val);
  1746. if (err)
  1747. return err;
  1748. next.state = val & GLOBAL_ATU_DATA_STATE_MASK;
  1749. if (next.state != GLOBAL_ATU_DATA_STATE_UNUSED) {
  1750. unsigned int mask, shift;
  1751. if (val & GLOBAL_ATU_DATA_TRUNK) {
  1752. next.trunk = true;
  1753. mask = GLOBAL_ATU_DATA_TRUNK_ID_MASK;
  1754. shift = GLOBAL_ATU_DATA_TRUNK_ID_SHIFT;
  1755. } else {
  1756. next.trunk = false;
  1757. mask = GLOBAL_ATU_DATA_PORT_VECTOR_MASK;
  1758. shift = GLOBAL_ATU_DATA_PORT_VECTOR_SHIFT;
  1759. }
  1760. next.portv_trunkid = (val & mask) >> shift;
  1761. }
  1762. *entry = next;
  1763. return 0;
  1764. }
  1765. static int mv88e6xxx_port_db_dump_fid(struct mv88e6xxx_chip *chip,
  1766. u16 fid, u16 vid, int port,
  1767. struct switchdev_obj *obj,
  1768. int (*cb)(struct switchdev_obj *obj))
  1769. {
  1770. struct mv88e6xxx_atu_entry addr = {
  1771. .mac = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff },
  1772. };
  1773. int err;
  1774. err = _mv88e6xxx_atu_mac_write(chip, addr.mac);
  1775. if (err)
  1776. return err;
  1777. do {
  1778. err = _mv88e6xxx_atu_getnext(chip, fid, &addr);
  1779. if (err)
  1780. return err;
  1781. if (addr.state == GLOBAL_ATU_DATA_STATE_UNUSED)
  1782. break;
  1783. if (addr.trunk || (addr.portv_trunkid & BIT(port)) == 0)
  1784. continue;
  1785. if (obj->id == SWITCHDEV_OBJ_ID_PORT_FDB) {
  1786. struct switchdev_obj_port_fdb *fdb;
  1787. if (!is_unicast_ether_addr(addr.mac))
  1788. continue;
  1789. fdb = SWITCHDEV_OBJ_PORT_FDB(obj);
  1790. fdb->vid = vid;
  1791. ether_addr_copy(fdb->addr, addr.mac);
  1792. if (addr.state == GLOBAL_ATU_DATA_STATE_UC_STATIC)
  1793. fdb->ndm_state = NUD_NOARP;
  1794. else
  1795. fdb->ndm_state = NUD_REACHABLE;
  1796. } else if (obj->id == SWITCHDEV_OBJ_ID_PORT_MDB) {
  1797. struct switchdev_obj_port_mdb *mdb;
  1798. if (!is_multicast_ether_addr(addr.mac))
  1799. continue;
  1800. mdb = SWITCHDEV_OBJ_PORT_MDB(obj);
  1801. mdb->vid = vid;
  1802. ether_addr_copy(mdb->addr, addr.mac);
  1803. } else {
  1804. return -EOPNOTSUPP;
  1805. }
  1806. err = cb(obj);
  1807. if (err)
  1808. return err;
  1809. } while (!is_broadcast_ether_addr(addr.mac));
  1810. return err;
  1811. }
  1812. static int mv88e6xxx_port_db_dump(struct mv88e6xxx_chip *chip, int port,
  1813. struct switchdev_obj *obj,
  1814. int (*cb)(struct switchdev_obj *obj))
  1815. {
  1816. struct mv88e6xxx_vtu_entry vlan = {
  1817. .vid = GLOBAL_VTU_VID_MASK, /* all ones */
  1818. };
  1819. u16 fid;
  1820. int err;
  1821. /* Dump port's default Filtering Information Database (VLAN ID 0) */
  1822. err = _mv88e6xxx_port_fid_get(chip, port, &fid);
  1823. if (err)
  1824. return err;
  1825. err = mv88e6xxx_port_db_dump_fid(chip, fid, 0, port, obj, cb);
  1826. if (err)
  1827. return err;
  1828. /* Dump VLANs' Filtering Information Databases */
  1829. err = _mv88e6xxx_vtu_vid_write(chip, vlan.vid);
  1830. if (err)
  1831. return err;
  1832. do {
  1833. err = _mv88e6xxx_vtu_getnext(chip, &vlan);
  1834. if (err)
  1835. return err;
  1836. if (!vlan.valid)
  1837. break;
  1838. err = mv88e6xxx_port_db_dump_fid(chip, vlan.fid, vlan.vid, port,
  1839. obj, cb);
  1840. if (err)
  1841. return err;
  1842. } while (vlan.vid < GLOBAL_VTU_VID_MASK);
  1843. return err;
  1844. }
  1845. static int mv88e6xxx_port_fdb_dump(struct dsa_switch *ds, int port,
  1846. struct switchdev_obj_port_fdb *fdb,
  1847. int (*cb)(struct switchdev_obj *obj))
  1848. {
  1849. struct mv88e6xxx_chip *chip = ds->priv;
  1850. int err;
  1851. mutex_lock(&chip->reg_lock);
  1852. err = mv88e6xxx_port_db_dump(chip, port, &fdb->obj, cb);
  1853. mutex_unlock(&chip->reg_lock);
  1854. return err;
  1855. }
  1856. static int mv88e6xxx_port_bridge_join(struct dsa_switch *ds, int port,
  1857. struct net_device *bridge)
  1858. {
  1859. struct mv88e6xxx_chip *chip = ds->priv;
  1860. int i, err = 0;
  1861. mutex_lock(&chip->reg_lock);
  1862. /* Assign the bridge and remap each port's VLANTable */
  1863. chip->ports[port].bridge_dev = bridge;
  1864. for (i = 0; i < mv88e6xxx_num_ports(chip); ++i) {
  1865. if (chip->ports[i].bridge_dev == bridge) {
  1866. err = _mv88e6xxx_port_based_vlan_map(chip, i);
  1867. if (err)
  1868. break;
  1869. }
  1870. }
  1871. mutex_unlock(&chip->reg_lock);
  1872. return err;
  1873. }
  1874. static void mv88e6xxx_port_bridge_leave(struct dsa_switch *ds, int port)
  1875. {
  1876. struct mv88e6xxx_chip *chip = ds->priv;
  1877. struct net_device *bridge = chip->ports[port].bridge_dev;
  1878. int i;
  1879. mutex_lock(&chip->reg_lock);
  1880. /* Unassign the bridge and remap each port's VLANTable */
  1881. chip->ports[port].bridge_dev = NULL;
  1882. for (i = 0; i < mv88e6xxx_num_ports(chip); ++i)
  1883. if (i == port || chip->ports[i].bridge_dev == bridge)
  1884. if (_mv88e6xxx_port_based_vlan_map(chip, i))
  1885. netdev_warn(ds->ports[i].netdev,
  1886. "failed to remap\n");
  1887. mutex_unlock(&chip->reg_lock);
  1888. }
  1889. static int mv88e6xxx_switch_reset(struct mv88e6xxx_chip *chip)
  1890. {
  1891. bool ppu_active = mv88e6xxx_has(chip, MV88E6XXX_FLAG_PPU_ACTIVE);
  1892. u16 is_reset = (ppu_active ? 0x8800 : 0xc800);
  1893. struct gpio_desc *gpiod = chip->reset;
  1894. unsigned long timeout;
  1895. u16 reg;
  1896. int err;
  1897. int i;
  1898. /* Set all ports to the disabled state. */
  1899. for (i = 0; i < mv88e6xxx_num_ports(chip); i++) {
  1900. err = mv88e6xxx_port_read(chip, i, PORT_CONTROL, &reg);
  1901. if (err)
  1902. return err;
  1903. err = mv88e6xxx_port_write(chip, i, PORT_CONTROL,
  1904. reg & 0xfffc);
  1905. if (err)
  1906. return err;
  1907. }
  1908. /* Wait for transmit queues to drain. */
  1909. usleep_range(2000, 4000);
  1910. /* If there is a gpio connected to the reset pin, toggle it */
  1911. if (gpiod) {
  1912. gpiod_set_value_cansleep(gpiod, 1);
  1913. usleep_range(10000, 20000);
  1914. gpiod_set_value_cansleep(gpiod, 0);
  1915. usleep_range(10000, 20000);
  1916. }
  1917. /* Reset the switch. Keep the PPU active if requested. The PPU
  1918. * needs to be active to support indirect phy register access
  1919. * through global registers 0x18 and 0x19.
  1920. */
  1921. if (ppu_active)
  1922. err = mv88e6xxx_g1_write(chip, 0x04, 0xc000);
  1923. else
  1924. err = mv88e6xxx_g1_write(chip, 0x04, 0xc400);
  1925. if (err)
  1926. return err;
  1927. /* Wait up to one second for reset to complete. */
  1928. timeout = jiffies + 1 * HZ;
  1929. while (time_before(jiffies, timeout)) {
  1930. err = mv88e6xxx_g1_read(chip, 0x00, &reg);
  1931. if (err)
  1932. return err;
  1933. if ((reg & is_reset) == is_reset)
  1934. break;
  1935. usleep_range(1000, 2000);
  1936. }
  1937. if (time_after(jiffies, timeout))
  1938. err = -ETIMEDOUT;
  1939. else
  1940. err = 0;
  1941. return err;
  1942. }
  1943. static int mv88e6xxx_serdes_power_on(struct mv88e6xxx_chip *chip)
  1944. {
  1945. u16 val;
  1946. int err;
  1947. /* Clear Power Down bit */
  1948. err = mv88e6xxx_serdes_read(chip, MII_BMCR, &val);
  1949. if (err)
  1950. return err;
  1951. if (val & BMCR_PDOWN) {
  1952. val &= ~BMCR_PDOWN;
  1953. err = mv88e6xxx_serdes_write(chip, MII_BMCR, val);
  1954. }
  1955. return err;
  1956. }
  1957. static int mv88e6xxx_setup_port(struct mv88e6xxx_chip *chip, int port)
  1958. {
  1959. struct dsa_switch *ds = chip->ds;
  1960. int err;
  1961. u16 reg;
  1962. if (mv88e6xxx_6352_family(chip) || mv88e6xxx_6351_family(chip) ||
  1963. mv88e6xxx_6165_family(chip) || mv88e6xxx_6097_family(chip) ||
  1964. mv88e6xxx_6185_family(chip) || mv88e6xxx_6095_family(chip) ||
  1965. mv88e6xxx_6065_family(chip) || mv88e6xxx_6320_family(chip)) {
  1966. /* MAC Forcing register: don't force link, speed,
  1967. * duplex or flow control state to any particular
  1968. * values on physical ports, but force the CPU port
  1969. * and all DSA ports to their maximum bandwidth and
  1970. * full duplex.
  1971. */
  1972. err = mv88e6xxx_port_read(chip, port, PORT_PCS_CTRL, &reg);
  1973. if (dsa_is_cpu_port(ds, port) || dsa_is_dsa_port(ds, port)) {
  1974. reg &= ~PORT_PCS_CTRL_UNFORCED;
  1975. reg |= PORT_PCS_CTRL_FORCE_LINK |
  1976. PORT_PCS_CTRL_LINK_UP |
  1977. PORT_PCS_CTRL_DUPLEX_FULL |
  1978. PORT_PCS_CTRL_FORCE_DUPLEX;
  1979. if (mv88e6xxx_6065_family(chip))
  1980. reg |= PORT_PCS_CTRL_100;
  1981. else
  1982. reg |= PORT_PCS_CTRL_1000;
  1983. } else {
  1984. reg |= PORT_PCS_CTRL_UNFORCED;
  1985. }
  1986. err = mv88e6xxx_port_write(chip, port, PORT_PCS_CTRL, reg);
  1987. if (err)
  1988. return err;
  1989. }
  1990. /* Port Control: disable Drop-on-Unlock, disable Drop-on-Lock,
  1991. * disable Header mode, enable IGMP/MLD snooping, disable VLAN
  1992. * tunneling, determine priority by looking at 802.1p and IP
  1993. * priority fields (IP prio has precedence), and set STP state
  1994. * to Forwarding.
  1995. *
  1996. * If this is the CPU link, use DSA or EDSA tagging depending
  1997. * on which tagging mode was configured.
  1998. *
  1999. * If this is a link to another switch, use DSA tagging mode.
  2000. *
  2001. * If this is the upstream port for this switch, enable
  2002. * forwarding of unknown unicasts and multicasts.
  2003. */
  2004. reg = 0;
  2005. if (mv88e6xxx_6352_family(chip) || mv88e6xxx_6351_family(chip) ||
  2006. mv88e6xxx_6165_family(chip) || mv88e6xxx_6097_family(chip) ||
  2007. mv88e6xxx_6095_family(chip) || mv88e6xxx_6065_family(chip) ||
  2008. mv88e6xxx_6185_family(chip) || mv88e6xxx_6320_family(chip))
  2009. reg = PORT_CONTROL_IGMP_MLD_SNOOP |
  2010. PORT_CONTROL_USE_TAG | PORT_CONTROL_USE_IP |
  2011. PORT_CONTROL_STATE_FORWARDING;
  2012. if (dsa_is_cpu_port(ds, port)) {
  2013. if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_EDSA))
  2014. reg |= PORT_CONTROL_FRAME_ETHER_TYPE_DSA |
  2015. PORT_CONTROL_FORWARD_UNKNOWN_MC;
  2016. else
  2017. reg |= PORT_CONTROL_DSA_TAG;
  2018. reg |= PORT_CONTROL_EGRESS_ADD_TAG |
  2019. PORT_CONTROL_FORWARD_UNKNOWN;
  2020. }
  2021. if (dsa_is_dsa_port(ds, port)) {
  2022. if (mv88e6xxx_6095_family(chip) ||
  2023. mv88e6xxx_6185_family(chip))
  2024. reg |= PORT_CONTROL_DSA_TAG;
  2025. if (mv88e6xxx_6352_family(chip) ||
  2026. mv88e6xxx_6351_family(chip) ||
  2027. mv88e6xxx_6165_family(chip) ||
  2028. mv88e6xxx_6097_family(chip) ||
  2029. mv88e6xxx_6320_family(chip)) {
  2030. reg |= PORT_CONTROL_FRAME_MODE_DSA;
  2031. }
  2032. if (port == dsa_upstream_port(ds))
  2033. reg |= PORT_CONTROL_FORWARD_UNKNOWN |
  2034. PORT_CONTROL_FORWARD_UNKNOWN_MC;
  2035. }
  2036. if (reg) {
  2037. err = mv88e6xxx_port_write(chip, port, PORT_CONTROL, reg);
  2038. if (err)
  2039. return err;
  2040. }
  2041. /* If this port is connected to a SerDes, make sure the SerDes is not
  2042. * powered down.
  2043. */
  2044. if (mv88e6xxx_has(chip, MV88E6XXX_FLAGS_SERDES)) {
  2045. err = mv88e6xxx_port_read(chip, port, PORT_STATUS, &reg);
  2046. if (err)
  2047. return err;
  2048. reg &= PORT_STATUS_CMODE_MASK;
  2049. if ((reg == PORT_STATUS_CMODE_100BASE_X) ||
  2050. (reg == PORT_STATUS_CMODE_1000BASE_X) ||
  2051. (reg == PORT_STATUS_CMODE_SGMII)) {
  2052. err = mv88e6xxx_serdes_power_on(chip);
  2053. if (err < 0)
  2054. return err;
  2055. }
  2056. }
  2057. /* Port Control 2: don't force a good FCS, set the maximum frame size to
  2058. * 10240 bytes, disable 802.1q tags checking, don't discard tagged or
  2059. * untagged frames on this port, do a destination address lookup on all
  2060. * received packets as usual, disable ARP mirroring and don't send a
  2061. * copy of all transmitted/received frames on this port to the CPU.
  2062. */
  2063. reg = 0;
  2064. if (mv88e6xxx_6352_family(chip) || mv88e6xxx_6351_family(chip) ||
  2065. mv88e6xxx_6165_family(chip) || mv88e6xxx_6097_family(chip) ||
  2066. mv88e6xxx_6095_family(chip) || mv88e6xxx_6320_family(chip) ||
  2067. mv88e6xxx_6185_family(chip))
  2068. reg = PORT_CONTROL_2_MAP_DA;
  2069. if (mv88e6xxx_6352_family(chip) || mv88e6xxx_6351_family(chip) ||
  2070. mv88e6xxx_6165_family(chip) || mv88e6xxx_6320_family(chip))
  2071. reg |= PORT_CONTROL_2_JUMBO_10240;
  2072. if (mv88e6xxx_6095_family(chip) || mv88e6xxx_6185_family(chip)) {
  2073. /* Set the upstream port this port should use */
  2074. reg |= dsa_upstream_port(ds);
  2075. /* enable forwarding of unknown multicast addresses to
  2076. * the upstream port
  2077. */
  2078. if (port == dsa_upstream_port(ds))
  2079. reg |= PORT_CONTROL_2_FORWARD_UNKNOWN;
  2080. }
  2081. reg |= PORT_CONTROL_2_8021Q_DISABLED;
  2082. if (reg) {
  2083. err = mv88e6xxx_port_write(chip, port, PORT_CONTROL_2, reg);
  2084. if (err)
  2085. return err;
  2086. }
  2087. /* Port Association Vector: when learning source addresses
  2088. * of packets, add the address to the address database using
  2089. * a port bitmap that has only the bit for this port set and
  2090. * the other bits clear.
  2091. */
  2092. reg = 1 << port;
  2093. /* Disable learning for CPU port */
  2094. if (dsa_is_cpu_port(ds, port))
  2095. reg = 0;
  2096. err = mv88e6xxx_port_write(chip, port, PORT_ASSOC_VECTOR, reg);
  2097. if (err)
  2098. return err;
  2099. /* Egress rate control 2: disable egress rate control. */
  2100. err = mv88e6xxx_port_write(chip, port, PORT_RATE_CONTROL_2, 0x0000);
  2101. if (err)
  2102. return err;
  2103. if (mv88e6xxx_6352_family(chip) || mv88e6xxx_6351_family(chip) ||
  2104. mv88e6xxx_6165_family(chip) || mv88e6xxx_6097_family(chip) ||
  2105. mv88e6xxx_6320_family(chip)) {
  2106. /* Do not limit the period of time that this port can
  2107. * be paused for by the remote end or the period of
  2108. * time that this port can pause the remote end.
  2109. */
  2110. err = mv88e6xxx_port_write(chip, port, PORT_PAUSE_CTRL, 0x0000);
  2111. if (err)
  2112. return err;
  2113. /* Port ATU control: disable limiting the number of
  2114. * address database entries that this port is allowed
  2115. * to use.
  2116. */
  2117. err = mv88e6xxx_port_write(chip, port, PORT_ATU_CONTROL,
  2118. 0x0000);
  2119. /* Priority Override: disable DA, SA and VTU priority
  2120. * override.
  2121. */
  2122. err = mv88e6xxx_port_write(chip, port, PORT_PRI_OVERRIDE,
  2123. 0x0000);
  2124. if (err)
  2125. return err;
  2126. /* Port Ethertype: use the Ethertype DSA Ethertype
  2127. * value.
  2128. */
  2129. if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_EDSA)) {
  2130. err = mv88e6xxx_port_write(chip, port, PORT_ETH_TYPE,
  2131. ETH_P_EDSA);
  2132. if (err)
  2133. return err;
  2134. }
  2135. /* Tag Remap: use an identity 802.1p prio -> switch
  2136. * prio mapping.
  2137. */
  2138. err = mv88e6xxx_port_write(chip, port, PORT_TAG_REGMAP_0123,
  2139. 0x3210);
  2140. if (err)
  2141. return err;
  2142. /* Tag Remap 2: use an identity 802.1p prio -> switch
  2143. * prio mapping.
  2144. */
  2145. err = mv88e6xxx_port_write(chip, port, PORT_TAG_REGMAP_4567,
  2146. 0x7654);
  2147. if (err)
  2148. return err;
  2149. }
  2150. /* Rate Control: disable ingress rate limiting. */
  2151. if (mv88e6xxx_6352_family(chip) || mv88e6xxx_6351_family(chip) ||
  2152. mv88e6xxx_6165_family(chip) || mv88e6xxx_6097_family(chip) ||
  2153. mv88e6xxx_6320_family(chip)) {
  2154. err = mv88e6xxx_port_write(chip, port, PORT_RATE_CONTROL,
  2155. 0x0001);
  2156. if (err)
  2157. return err;
  2158. } else if (mv88e6xxx_6185_family(chip) || mv88e6xxx_6095_family(chip)) {
  2159. err = mv88e6xxx_port_write(chip, port, PORT_RATE_CONTROL,
  2160. 0x0000);
  2161. if (err)
  2162. return err;
  2163. }
  2164. /* Port Control 1: disable trunking, disable sending
  2165. * learning messages to this port.
  2166. */
  2167. err = mv88e6xxx_port_write(chip, port, PORT_CONTROL_1, 0x0000);
  2168. if (err)
  2169. return err;
  2170. /* Port based VLAN map: give each port the same default address
  2171. * database, and allow bidirectional communication between the
  2172. * CPU and DSA port(s), and the other ports.
  2173. */
  2174. err = _mv88e6xxx_port_fid_set(chip, port, 0);
  2175. if (err)
  2176. return err;
  2177. err = _mv88e6xxx_port_based_vlan_map(chip, port);
  2178. if (err)
  2179. return err;
  2180. /* Default VLAN ID and priority: don't set a default VLAN
  2181. * ID, and set the default packet priority to zero.
  2182. */
  2183. return mv88e6xxx_port_write(chip, port, PORT_DEFAULT_VLAN, 0x0000);
  2184. }
  2185. int mv88e6xxx_g1_set_switch_mac(struct mv88e6xxx_chip *chip, u8 *addr)
  2186. {
  2187. int err;
  2188. err = mv88e6xxx_g1_write(chip, GLOBAL_MAC_01, (addr[0] << 8) | addr[1]);
  2189. if (err)
  2190. return err;
  2191. err = mv88e6xxx_g1_write(chip, GLOBAL_MAC_23, (addr[2] << 8) | addr[3]);
  2192. if (err)
  2193. return err;
  2194. err = mv88e6xxx_g1_write(chip, GLOBAL_MAC_45, (addr[4] << 8) | addr[5]);
  2195. if (err)
  2196. return err;
  2197. return 0;
  2198. }
  2199. static int mv88e6xxx_g1_set_age_time(struct mv88e6xxx_chip *chip,
  2200. unsigned int msecs)
  2201. {
  2202. const unsigned int coeff = chip->info->age_time_coeff;
  2203. const unsigned int min = 0x01 * coeff;
  2204. const unsigned int max = 0xff * coeff;
  2205. u8 age_time;
  2206. u16 val;
  2207. int err;
  2208. if (msecs < min || msecs > max)
  2209. return -ERANGE;
  2210. /* Round to nearest multiple of coeff */
  2211. age_time = (msecs + coeff / 2) / coeff;
  2212. err = mv88e6xxx_g1_read(chip, GLOBAL_ATU_CONTROL, &val);
  2213. if (err)
  2214. return err;
  2215. /* AgeTime is 11:4 bits */
  2216. val &= ~0xff0;
  2217. val |= age_time << 4;
  2218. return mv88e6xxx_g1_write(chip, GLOBAL_ATU_CONTROL, val);
  2219. }
  2220. static int mv88e6xxx_set_ageing_time(struct dsa_switch *ds,
  2221. unsigned int ageing_time)
  2222. {
  2223. struct mv88e6xxx_chip *chip = ds->priv;
  2224. int err;
  2225. mutex_lock(&chip->reg_lock);
  2226. err = mv88e6xxx_g1_set_age_time(chip, ageing_time);
  2227. mutex_unlock(&chip->reg_lock);
  2228. return err;
  2229. }
  2230. static int mv88e6xxx_g1_setup(struct mv88e6xxx_chip *chip)
  2231. {
  2232. struct dsa_switch *ds = chip->ds;
  2233. u32 upstream_port = dsa_upstream_port(ds);
  2234. u16 reg;
  2235. int err;
  2236. /* Enable the PHY Polling Unit if present, don't discard any packets,
  2237. * and mask all interrupt sources.
  2238. */
  2239. reg = 0;
  2240. if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_PPU) ||
  2241. mv88e6xxx_has(chip, MV88E6XXX_FLAG_PPU_ACTIVE))
  2242. reg |= GLOBAL_CONTROL_PPU_ENABLE;
  2243. err = mv88e6xxx_g1_write(chip, GLOBAL_CONTROL, reg);
  2244. if (err)
  2245. return err;
  2246. /* Configure the upstream port, and configure it as the port to which
  2247. * ingress and egress and ARP monitor frames are to be sent.
  2248. */
  2249. reg = upstream_port << GLOBAL_MONITOR_CONTROL_INGRESS_SHIFT |
  2250. upstream_port << GLOBAL_MONITOR_CONTROL_EGRESS_SHIFT |
  2251. upstream_port << GLOBAL_MONITOR_CONTROL_ARP_SHIFT;
  2252. err = mv88e6xxx_g1_write(chip, GLOBAL_MONITOR_CONTROL, reg);
  2253. if (err)
  2254. return err;
  2255. /* Disable remote management, and set the switch's DSA device number. */
  2256. err = mv88e6xxx_g1_write(chip, GLOBAL_CONTROL_2,
  2257. GLOBAL_CONTROL_2_MULTIPLE_CASCADE |
  2258. (ds->index & 0x1f));
  2259. if (err)
  2260. return err;
  2261. /* Clear all the VTU and STU entries */
  2262. err = _mv88e6xxx_vtu_stu_flush(chip);
  2263. if (err < 0)
  2264. return err;
  2265. /* Set the default address aging time to 5 minutes, and
  2266. * enable address learn messages to be sent to all message
  2267. * ports.
  2268. */
  2269. err = mv88e6xxx_g1_write(chip, GLOBAL_ATU_CONTROL,
  2270. GLOBAL_ATU_CONTROL_LEARN2ALL);
  2271. if (err)
  2272. return err;
  2273. err = mv88e6xxx_g1_set_age_time(chip, 300000);
  2274. if (err)
  2275. return err;
  2276. /* Clear all ATU entries */
  2277. err = _mv88e6xxx_atu_flush(chip, 0, true);
  2278. if (err)
  2279. return err;
  2280. /* Configure the IP ToS mapping registers. */
  2281. err = mv88e6xxx_g1_write(chip, GLOBAL_IP_PRI_0, 0x0000);
  2282. if (err)
  2283. return err;
  2284. err = mv88e6xxx_g1_write(chip, GLOBAL_IP_PRI_1, 0x0000);
  2285. if (err)
  2286. return err;
  2287. err = mv88e6xxx_g1_write(chip, GLOBAL_IP_PRI_2, 0x5555);
  2288. if (err)
  2289. return err;
  2290. err = mv88e6xxx_g1_write(chip, GLOBAL_IP_PRI_3, 0x5555);
  2291. if (err)
  2292. return err;
  2293. err = mv88e6xxx_g1_write(chip, GLOBAL_IP_PRI_4, 0xaaaa);
  2294. if (err)
  2295. return err;
  2296. err = mv88e6xxx_g1_write(chip, GLOBAL_IP_PRI_5, 0xaaaa);
  2297. if (err)
  2298. return err;
  2299. err = mv88e6xxx_g1_write(chip, GLOBAL_IP_PRI_6, 0xffff);
  2300. if (err)
  2301. return err;
  2302. err = mv88e6xxx_g1_write(chip, GLOBAL_IP_PRI_7, 0xffff);
  2303. if (err)
  2304. return err;
  2305. /* Configure the IEEE 802.1p priority mapping register. */
  2306. err = mv88e6xxx_g1_write(chip, GLOBAL_IEEE_PRI, 0xfa41);
  2307. if (err)
  2308. return err;
  2309. /* Clear the statistics counters for all ports */
  2310. err = mv88e6xxx_g1_write(chip, GLOBAL_STATS_OP,
  2311. GLOBAL_STATS_OP_FLUSH_ALL);
  2312. if (err)
  2313. return err;
  2314. /* Wait for the flush to complete. */
  2315. err = _mv88e6xxx_stats_wait(chip);
  2316. if (err)
  2317. return err;
  2318. return 0;
  2319. }
  2320. static int mv88e6xxx_setup(struct dsa_switch *ds)
  2321. {
  2322. struct mv88e6xxx_chip *chip = ds->priv;
  2323. int err;
  2324. int i;
  2325. chip->ds = ds;
  2326. ds->slave_mii_bus = chip->mdio_bus;
  2327. mutex_lock(&chip->reg_lock);
  2328. err = mv88e6xxx_switch_reset(chip);
  2329. if (err)
  2330. goto unlock;
  2331. /* Setup Switch Port Registers */
  2332. for (i = 0; i < mv88e6xxx_num_ports(chip); i++) {
  2333. err = mv88e6xxx_setup_port(chip, i);
  2334. if (err)
  2335. goto unlock;
  2336. }
  2337. /* Setup Switch Global 1 Registers */
  2338. err = mv88e6xxx_g1_setup(chip);
  2339. if (err)
  2340. goto unlock;
  2341. /* Setup Switch Global 2 Registers */
  2342. if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_GLOBAL2)) {
  2343. err = mv88e6xxx_g2_setup(chip);
  2344. if (err)
  2345. goto unlock;
  2346. }
  2347. unlock:
  2348. mutex_unlock(&chip->reg_lock);
  2349. return err;
  2350. }
  2351. static int mv88e6xxx_set_addr(struct dsa_switch *ds, u8 *addr)
  2352. {
  2353. struct mv88e6xxx_chip *chip = ds->priv;
  2354. int err;
  2355. if (!chip->info->ops->set_switch_mac)
  2356. return -EOPNOTSUPP;
  2357. mutex_lock(&chip->reg_lock);
  2358. err = chip->info->ops->set_switch_mac(chip, addr);
  2359. mutex_unlock(&chip->reg_lock);
  2360. return err;
  2361. }
  2362. static int mv88e6xxx_mdio_read(struct mii_bus *bus, int phy, int reg)
  2363. {
  2364. struct mv88e6xxx_chip *chip = bus->priv;
  2365. u16 val;
  2366. int err;
  2367. if (phy >= mv88e6xxx_num_ports(chip))
  2368. return 0xffff;
  2369. mutex_lock(&chip->reg_lock);
  2370. err = mv88e6xxx_phy_read(chip, phy, reg, &val);
  2371. mutex_unlock(&chip->reg_lock);
  2372. return err ? err : val;
  2373. }
  2374. static int mv88e6xxx_mdio_write(struct mii_bus *bus, int phy, int reg, u16 val)
  2375. {
  2376. struct mv88e6xxx_chip *chip = bus->priv;
  2377. int err;
  2378. if (phy >= mv88e6xxx_num_ports(chip))
  2379. return 0xffff;
  2380. mutex_lock(&chip->reg_lock);
  2381. err = mv88e6xxx_phy_write(chip, phy, reg, val);
  2382. mutex_unlock(&chip->reg_lock);
  2383. return err;
  2384. }
  2385. static int mv88e6xxx_mdio_register(struct mv88e6xxx_chip *chip,
  2386. struct device_node *np)
  2387. {
  2388. static int index;
  2389. struct mii_bus *bus;
  2390. int err;
  2391. if (np)
  2392. chip->mdio_np = of_get_child_by_name(np, "mdio");
  2393. bus = devm_mdiobus_alloc(chip->dev);
  2394. if (!bus)
  2395. return -ENOMEM;
  2396. bus->priv = (void *)chip;
  2397. if (np) {
  2398. bus->name = np->full_name;
  2399. snprintf(bus->id, MII_BUS_ID_SIZE, "%s", np->full_name);
  2400. } else {
  2401. bus->name = "mv88e6xxx SMI";
  2402. snprintf(bus->id, MII_BUS_ID_SIZE, "mv88e6xxx-%d", index++);
  2403. }
  2404. bus->read = mv88e6xxx_mdio_read;
  2405. bus->write = mv88e6xxx_mdio_write;
  2406. bus->parent = chip->dev;
  2407. if (chip->mdio_np)
  2408. err = of_mdiobus_register(bus, chip->mdio_np);
  2409. else
  2410. err = mdiobus_register(bus);
  2411. if (err) {
  2412. dev_err(chip->dev, "Cannot register MDIO bus (%d)\n", err);
  2413. goto out;
  2414. }
  2415. chip->mdio_bus = bus;
  2416. return 0;
  2417. out:
  2418. if (chip->mdio_np)
  2419. of_node_put(chip->mdio_np);
  2420. return err;
  2421. }
  2422. static void mv88e6xxx_mdio_unregister(struct mv88e6xxx_chip *chip)
  2423. {
  2424. struct mii_bus *bus = chip->mdio_bus;
  2425. mdiobus_unregister(bus);
  2426. if (chip->mdio_np)
  2427. of_node_put(chip->mdio_np);
  2428. }
  2429. #ifdef CONFIG_NET_DSA_HWMON
  2430. static int mv88e61xx_get_temp(struct dsa_switch *ds, int *temp)
  2431. {
  2432. struct mv88e6xxx_chip *chip = ds->priv;
  2433. u16 val;
  2434. int ret;
  2435. *temp = 0;
  2436. mutex_lock(&chip->reg_lock);
  2437. ret = mv88e6xxx_phy_write(chip, 0x0, 0x16, 0x6);
  2438. if (ret < 0)
  2439. goto error;
  2440. /* Enable temperature sensor */
  2441. ret = mv88e6xxx_phy_read(chip, 0x0, 0x1a, &val);
  2442. if (ret < 0)
  2443. goto error;
  2444. ret = mv88e6xxx_phy_write(chip, 0x0, 0x1a, val | (1 << 5));
  2445. if (ret < 0)
  2446. goto error;
  2447. /* Wait for temperature to stabilize */
  2448. usleep_range(10000, 12000);
  2449. ret = mv88e6xxx_phy_read(chip, 0x0, 0x1a, &val);
  2450. if (ret < 0)
  2451. goto error;
  2452. /* Disable temperature sensor */
  2453. ret = mv88e6xxx_phy_write(chip, 0x0, 0x1a, val & ~(1 << 5));
  2454. if (ret < 0)
  2455. goto error;
  2456. *temp = ((val & 0x1f) - 5) * 5;
  2457. error:
  2458. mv88e6xxx_phy_write(chip, 0x0, 0x16, 0x0);
  2459. mutex_unlock(&chip->reg_lock);
  2460. return ret;
  2461. }
  2462. static int mv88e63xx_get_temp(struct dsa_switch *ds, int *temp)
  2463. {
  2464. struct mv88e6xxx_chip *chip = ds->priv;
  2465. int phy = mv88e6xxx_6320_family(chip) ? 3 : 0;
  2466. u16 val;
  2467. int ret;
  2468. *temp = 0;
  2469. mutex_lock(&chip->reg_lock);
  2470. ret = mv88e6xxx_phy_page_read(chip, phy, 6, 27, &val);
  2471. mutex_unlock(&chip->reg_lock);
  2472. if (ret < 0)
  2473. return ret;
  2474. *temp = (val & 0xff) - 25;
  2475. return 0;
  2476. }
  2477. static int mv88e6xxx_get_temp(struct dsa_switch *ds, int *temp)
  2478. {
  2479. struct mv88e6xxx_chip *chip = ds->priv;
  2480. if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_TEMP))
  2481. return -EOPNOTSUPP;
  2482. if (mv88e6xxx_6320_family(chip) || mv88e6xxx_6352_family(chip))
  2483. return mv88e63xx_get_temp(ds, temp);
  2484. return mv88e61xx_get_temp(ds, temp);
  2485. }
  2486. static int mv88e6xxx_get_temp_limit(struct dsa_switch *ds, int *temp)
  2487. {
  2488. struct mv88e6xxx_chip *chip = ds->priv;
  2489. int phy = mv88e6xxx_6320_family(chip) ? 3 : 0;
  2490. u16 val;
  2491. int ret;
  2492. if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_TEMP_LIMIT))
  2493. return -EOPNOTSUPP;
  2494. *temp = 0;
  2495. mutex_lock(&chip->reg_lock);
  2496. ret = mv88e6xxx_phy_page_read(chip, phy, 6, 26, &val);
  2497. mutex_unlock(&chip->reg_lock);
  2498. if (ret < 0)
  2499. return ret;
  2500. *temp = (((val >> 8) & 0x1f) * 5) - 25;
  2501. return 0;
  2502. }
  2503. static int mv88e6xxx_set_temp_limit(struct dsa_switch *ds, int temp)
  2504. {
  2505. struct mv88e6xxx_chip *chip = ds->priv;
  2506. int phy = mv88e6xxx_6320_family(chip) ? 3 : 0;
  2507. u16 val;
  2508. int err;
  2509. if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_TEMP_LIMIT))
  2510. return -EOPNOTSUPP;
  2511. mutex_lock(&chip->reg_lock);
  2512. err = mv88e6xxx_phy_page_read(chip, phy, 6, 26, &val);
  2513. if (err)
  2514. goto unlock;
  2515. temp = clamp_val(DIV_ROUND_CLOSEST(temp, 5) + 5, 0, 0x1f);
  2516. err = mv88e6xxx_phy_page_write(chip, phy, 6, 26,
  2517. (val & 0xe0ff) | (temp << 8));
  2518. unlock:
  2519. mutex_unlock(&chip->reg_lock);
  2520. return err;
  2521. }
  2522. static int mv88e6xxx_get_temp_alarm(struct dsa_switch *ds, bool *alarm)
  2523. {
  2524. struct mv88e6xxx_chip *chip = ds->priv;
  2525. int phy = mv88e6xxx_6320_family(chip) ? 3 : 0;
  2526. u16 val;
  2527. int ret;
  2528. if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_TEMP_LIMIT))
  2529. return -EOPNOTSUPP;
  2530. *alarm = false;
  2531. mutex_lock(&chip->reg_lock);
  2532. ret = mv88e6xxx_phy_page_read(chip, phy, 6, 26, &val);
  2533. mutex_unlock(&chip->reg_lock);
  2534. if (ret < 0)
  2535. return ret;
  2536. *alarm = !!(val & 0x40);
  2537. return 0;
  2538. }
  2539. #endif /* CONFIG_NET_DSA_HWMON */
  2540. static int mv88e6xxx_get_eeprom_len(struct dsa_switch *ds)
  2541. {
  2542. struct mv88e6xxx_chip *chip = ds->priv;
  2543. return chip->eeprom_len;
  2544. }
  2545. static int mv88e6xxx_get_eeprom(struct dsa_switch *ds,
  2546. struct ethtool_eeprom *eeprom, u8 *data)
  2547. {
  2548. struct mv88e6xxx_chip *chip = ds->priv;
  2549. int err;
  2550. if (!chip->info->ops->get_eeprom)
  2551. return -EOPNOTSUPP;
  2552. mutex_lock(&chip->reg_lock);
  2553. err = chip->info->ops->get_eeprom(chip, eeprom, data);
  2554. mutex_unlock(&chip->reg_lock);
  2555. if (err)
  2556. return err;
  2557. eeprom->magic = 0xc3ec4951;
  2558. return 0;
  2559. }
  2560. static int mv88e6xxx_set_eeprom(struct dsa_switch *ds,
  2561. struct ethtool_eeprom *eeprom, u8 *data)
  2562. {
  2563. struct mv88e6xxx_chip *chip = ds->priv;
  2564. int err;
  2565. if (!chip->info->ops->set_eeprom)
  2566. return -EOPNOTSUPP;
  2567. if (eeprom->magic != 0xc3ec4951)
  2568. return -EINVAL;
  2569. mutex_lock(&chip->reg_lock);
  2570. err = chip->info->ops->set_eeprom(chip, eeprom, data);
  2571. mutex_unlock(&chip->reg_lock);
  2572. return err;
  2573. }
  2574. static const struct mv88e6xxx_ops mv88e6085_ops = {
  2575. .set_switch_mac = mv88e6xxx_g1_set_switch_mac,
  2576. .phy_read = mv88e6xxx_phy_ppu_read,
  2577. .phy_write = mv88e6xxx_phy_ppu_write,
  2578. };
  2579. static const struct mv88e6xxx_ops mv88e6095_ops = {
  2580. .set_switch_mac = mv88e6xxx_g1_set_switch_mac,
  2581. .phy_read = mv88e6xxx_phy_ppu_read,
  2582. .phy_write = mv88e6xxx_phy_ppu_write,
  2583. };
  2584. static const struct mv88e6xxx_ops mv88e6123_ops = {
  2585. .set_switch_mac = mv88e6xxx_g2_set_switch_mac,
  2586. .phy_read = mv88e6xxx_read,
  2587. .phy_write = mv88e6xxx_write,
  2588. };
  2589. static const struct mv88e6xxx_ops mv88e6131_ops = {
  2590. .set_switch_mac = mv88e6xxx_g1_set_switch_mac,
  2591. .phy_read = mv88e6xxx_phy_ppu_read,
  2592. .phy_write = mv88e6xxx_phy_ppu_write,
  2593. };
  2594. static const struct mv88e6xxx_ops mv88e6161_ops = {
  2595. .set_switch_mac = mv88e6xxx_g2_set_switch_mac,
  2596. .phy_read = mv88e6xxx_read,
  2597. .phy_write = mv88e6xxx_write,
  2598. };
  2599. static const struct mv88e6xxx_ops mv88e6165_ops = {
  2600. .set_switch_mac = mv88e6xxx_g2_set_switch_mac,
  2601. .phy_read = mv88e6xxx_read,
  2602. .phy_write = mv88e6xxx_write,
  2603. };
  2604. static const struct mv88e6xxx_ops mv88e6171_ops = {
  2605. .set_switch_mac = mv88e6xxx_g2_set_switch_mac,
  2606. .phy_read = mv88e6xxx_g2_smi_phy_read,
  2607. .phy_write = mv88e6xxx_g2_smi_phy_write,
  2608. };
  2609. static const struct mv88e6xxx_ops mv88e6172_ops = {
  2610. .get_eeprom = mv88e6xxx_g2_get_eeprom16,
  2611. .set_eeprom = mv88e6xxx_g2_set_eeprom16,
  2612. .set_switch_mac = mv88e6xxx_g2_set_switch_mac,
  2613. .phy_read = mv88e6xxx_g2_smi_phy_read,
  2614. .phy_write = mv88e6xxx_g2_smi_phy_write,
  2615. };
  2616. static const struct mv88e6xxx_ops mv88e6175_ops = {
  2617. .set_switch_mac = mv88e6xxx_g2_set_switch_mac,
  2618. .phy_read = mv88e6xxx_g2_smi_phy_read,
  2619. .phy_write = mv88e6xxx_g2_smi_phy_write,
  2620. };
  2621. static const struct mv88e6xxx_ops mv88e6176_ops = {
  2622. .get_eeprom = mv88e6xxx_g2_get_eeprom16,
  2623. .set_eeprom = mv88e6xxx_g2_set_eeprom16,
  2624. .set_switch_mac = mv88e6xxx_g2_set_switch_mac,
  2625. .phy_read = mv88e6xxx_g2_smi_phy_read,
  2626. .phy_write = mv88e6xxx_g2_smi_phy_write,
  2627. };
  2628. static const struct mv88e6xxx_ops mv88e6185_ops = {
  2629. .set_switch_mac = mv88e6xxx_g1_set_switch_mac,
  2630. .phy_read = mv88e6xxx_phy_ppu_read,
  2631. .phy_write = mv88e6xxx_phy_ppu_write,
  2632. };
  2633. static const struct mv88e6xxx_ops mv88e6240_ops = {
  2634. .get_eeprom = mv88e6xxx_g2_get_eeprom16,
  2635. .set_eeprom = mv88e6xxx_g2_set_eeprom16,
  2636. .set_switch_mac = mv88e6xxx_g2_set_switch_mac,
  2637. .phy_read = mv88e6xxx_g2_smi_phy_read,
  2638. .phy_write = mv88e6xxx_g2_smi_phy_write,
  2639. };
  2640. static const struct mv88e6xxx_ops mv88e6320_ops = {
  2641. .get_eeprom = mv88e6xxx_g2_get_eeprom16,
  2642. .set_eeprom = mv88e6xxx_g2_set_eeprom16,
  2643. .set_switch_mac = mv88e6xxx_g2_set_switch_mac,
  2644. .phy_read = mv88e6xxx_g2_smi_phy_read,
  2645. .phy_write = mv88e6xxx_g2_smi_phy_write,
  2646. };
  2647. static const struct mv88e6xxx_ops mv88e6321_ops = {
  2648. .get_eeprom = mv88e6xxx_g2_get_eeprom16,
  2649. .set_eeprom = mv88e6xxx_g2_set_eeprom16,
  2650. .set_switch_mac = mv88e6xxx_g2_set_switch_mac,
  2651. .phy_read = mv88e6xxx_g2_smi_phy_read,
  2652. .phy_write = mv88e6xxx_g2_smi_phy_write,
  2653. };
  2654. static const struct mv88e6xxx_ops mv88e6350_ops = {
  2655. .set_switch_mac = mv88e6xxx_g2_set_switch_mac,
  2656. .phy_read = mv88e6xxx_g2_smi_phy_read,
  2657. .phy_write = mv88e6xxx_g2_smi_phy_write,
  2658. };
  2659. static const struct mv88e6xxx_ops mv88e6351_ops = {
  2660. .set_switch_mac = mv88e6xxx_g2_set_switch_mac,
  2661. .phy_read = mv88e6xxx_g2_smi_phy_read,
  2662. .phy_write = mv88e6xxx_g2_smi_phy_write,
  2663. };
  2664. static const struct mv88e6xxx_ops mv88e6352_ops = {
  2665. .get_eeprom = mv88e6xxx_g2_get_eeprom16,
  2666. .set_eeprom = mv88e6xxx_g2_set_eeprom16,
  2667. .set_switch_mac = mv88e6xxx_g2_set_switch_mac,
  2668. .phy_read = mv88e6xxx_g2_smi_phy_read,
  2669. .phy_write = mv88e6xxx_g2_smi_phy_write,
  2670. };
  2671. static const struct mv88e6xxx_info mv88e6xxx_table[] = {
  2672. [MV88E6085] = {
  2673. .prod_num = PORT_SWITCH_ID_PROD_NUM_6085,
  2674. .family = MV88E6XXX_FAMILY_6097,
  2675. .name = "Marvell 88E6085",
  2676. .num_databases = 4096,
  2677. .num_ports = 10,
  2678. .port_base_addr = 0x10,
  2679. .global1_addr = 0x1b,
  2680. .age_time_coeff = 15000,
  2681. .flags = MV88E6XXX_FLAGS_FAMILY_6097,
  2682. .ops = &mv88e6085_ops,
  2683. },
  2684. [MV88E6095] = {
  2685. .prod_num = PORT_SWITCH_ID_PROD_NUM_6095,
  2686. .family = MV88E6XXX_FAMILY_6095,
  2687. .name = "Marvell 88E6095/88E6095F",
  2688. .num_databases = 256,
  2689. .num_ports = 11,
  2690. .port_base_addr = 0x10,
  2691. .global1_addr = 0x1b,
  2692. .age_time_coeff = 15000,
  2693. .flags = MV88E6XXX_FLAGS_FAMILY_6095,
  2694. .ops = &mv88e6095_ops,
  2695. },
  2696. [MV88E6123] = {
  2697. .prod_num = PORT_SWITCH_ID_PROD_NUM_6123,
  2698. .family = MV88E6XXX_FAMILY_6165,
  2699. .name = "Marvell 88E6123",
  2700. .num_databases = 4096,
  2701. .num_ports = 3,
  2702. .port_base_addr = 0x10,
  2703. .global1_addr = 0x1b,
  2704. .age_time_coeff = 15000,
  2705. .flags = MV88E6XXX_FLAGS_FAMILY_6165,
  2706. .ops = &mv88e6123_ops,
  2707. },
  2708. [MV88E6131] = {
  2709. .prod_num = PORT_SWITCH_ID_PROD_NUM_6131,
  2710. .family = MV88E6XXX_FAMILY_6185,
  2711. .name = "Marvell 88E6131",
  2712. .num_databases = 256,
  2713. .num_ports = 8,
  2714. .port_base_addr = 0x10,
  2715. .global1_addr = 0x1b,
  2716. .age_time_coeff = 15000,
  2717. .flags = MV88E6XXX_FLAGS_FAMILY_6185,
  2718. .ops = &mv88e6131_ops,
  2719. },
  2720. [MV88E6161] = {
  2721. .prod_num = PORT_SWITCH_ID_PROD_NUM_6161,
  2722. .family = MV88E6XXX_FAMILY_6165,
  2723. .name = "Marvell 88E6161",
  2724. .num_databases = 4096,
  2725. .num_ports = 6,
  2726. .port_base_addr = 0x10,
  2727. .global1_addr = 0x1b,
  2728. .age_time_coeff = 15000,
  2729. .flags = MV88E6XXX_FLAGS_FAMILY_6165,
  2730. .ops = &mv88e6161_ops,
  2731. },
  2732. [MV88E6165] = {
  2733. .prod_num = PORT_SWITCH_ID_PROD_NUM_6165,
  2734. .family = MV88E6XXX_FAMILY_6165,
  2735. .name = "Marvell 88E6165",
  2736. .num_databases = 4096,
  2737. .num_ports = 6,
  2738. .port_base_addr = 0x10,
  2739. .global1_addr = 0x1b,
  2740. .age_time_coeff = 15000,
  2741. .flags = MV88E6XXX_FLAGS_FAMILY_6165,
  2742. .ops = &mv88e6165_ops,
  2743. },
  2744. [MV88E6171] = {
  2745. .prod_num = PORT_SWITCH_ID_PROD_NUM_6171,
  2746. .family = MV88E6XXX_FAMILY_6351,
  2747. .name = "Marvell 88E6171",
  2748. .num_databases = 4096,
  2749. .num_ports = 7,
  2750. .port_base_addr = 0x10,
  2751. .global1_addr = 0x1b,
  2752. .age_time_coeff = 15000,
  2753. .flags = MV88E6XXX_FLAGS_FAMILY_6351,
  2754. .ops = &mv88e6171_ops,
  2755. },
  2756. [MV88E6172] = {
  2757. .prod_num = PORT_SWITCH_ID_PROD_NUM_6172,
  2758. .family = MV88E6XXX_FAMILY_6352,
  2759. .name = "Marvell 88E6172",
  2760. .num_databases = 4096,
  2761. .num_ports = 7,
  2762. .port_base_addr = 0x10,
  2763. .global1_addr = 0x1b,
  2764. .age_time_coeff = 15000,
  2765. .flags = MV88E6XXX_FLAGS_FAMILY_6352,
  2766. .ops = &mv88e6172_ops,
  2767. },
  2768. [MV88E6175] = {
  2769. .prod_num = PORT_SWITCH_ID_PROD_NUM_6175,
  2770. .family = MV88E6XXX_FAMILY_6351,
  2771. .name = "Marvell 88E6175",
  2772. .num_databases = 4096,
  2773. .num_ports = 7,
  2774. .port_base_addr = 0x10,
  2775. .global1_addr = 0x1b,
  2776. .age_time_coeff = 15000,
  2777. .flags = MV88E6XXX_FLAGS_FAMILY_6351,
  2778. .ops = &mv88e6175_ops,
  2779. },
  2780. [MV88E6176] = {
  2781. .prod_num = PORT_SWITCH_ID_PROD_NUM_6176,
  2782. .family = MV88E6XXX_FAMILY_6352,
  2783. .name = "Marvell 88E6176",
  2784. .num_databases = 4096,
  2785. .num_ports = 7,
  2786. .port_base_addr = 0x10,
  2787. .global1_addr = 0x1b,
  2788. .age_time_coeff = 15000,
  2789. .flags = MV88E6XXX_FLAGS_FAMILY_6352,
  2790. .ops = &mv88e6176_ops,
  2791. },
  2792. [MV88E6185] = {
  2793. .prod_num = PORT_SWITCH_ID_PROD_NUM_6185,
  2794. .family = MV88E6XXX_FAMILY_6185,
  2795. .name = "Marvell 88E6185",
  2796. .num_databases = 256,
  2797. .num_ports = 10,
  2798. .port_base_addr = 0x10,
  2799. .global1_addr = 0x1b,
  2800. .age_time_coeff = 15000,
  2801. .flags = MV88E6XXX_FLAGS_FAMILY_6185,
  2802. .ops = &mv88e6185_ops,
  2803. },
  2804. [MV88E6240] = {
  2805. .prod_num = PORT_SWITCH_ID_PROD_NUM_6240,
  2806. .family = MV88E6XXX_FAMILY_6352,
  2807. .name = "Marvell 88E6240",
  2808. .num_databases = 4096,
  2809. .num_ports = 7,
  2810. .port_base_addr = 0x10,
  2811. .global1_addr = 0x1b,
  2812. .age_time_coeff = 15000,
  2813. .flags = MV88E6XXX_FLAGS_FAMILY_6352,
  2814. .ops = &mv88e6240_ops,
  2815. },
  2816. [MV88E6320] = {
  2817. .prod_num = PORT_SWITCH_ID_PROD_NUM_6320,
  2818. .family = MV88E6XXX_FAMILY_6320,
  2819. .name = "Marvell 88E6320",
  2820. .num_databases = 4096,
  2821. .num_ports = 7,
  2822. .port_base_addr = 0x10,
  2823. .global1_addr = 0x1b,
  2824. .age_time_coeff = 15000,
  2825. .flags = MV88E6XXX_FLAGS_FAMILY_6320,
  2826. .ops = &mv88e6320_ops,
  2827. },
  2828. [MV88E6321] = {
  2829. .prod_num = PORT_SWITCH_ID_PROD_NUM_6321,
  2830. .family = MV88E6XXX_FAMILY_6320,
  2831. .name = "Marvell 88E6321",
  2832. .num_databases = 4096,
  2833. .num_ports = 7,
  2834. .port_base_addr = 0x10,
  2835. .global1_addr = 0x1b,
  2836. .age_time_coeff = 15000,
  2837. .flags = MV88E6XXX_FLAGS_FAMILY_6320,
  2838. .ops = &mv88e6321_ops,
  2839. },
  2840. [MV88E6350] = {
  2841. .prod_num = PORT_SWITCH_ID_PROD_NUM_6350,
  2842. .family = MV88E6XXX_FAMILY_6351,
  2843. .name = "Marvell 88E6350",
  2844. .num_databases = 4096,
  2845. .num_ports = 7,
  2846. .port_base_addr = 0x10,
  2847. .global1_addr = 0x1b,
  2848. .age_time_coeff = 15000,
  2849. .flags = MV88E6XXX_FLAGS_FAMILY_6351,
  2850. .ops = &mv88e6350_ops,
  2851. },
  2852. [MV88E6351] = {
  2853. .prod_num = PORT_SWITCH_ID_PROD_NUM_6351,
  2854. .family = MV88E6XXX_FAMILY_6351,
  2855. .name = "Marvell 88E6351",
  2856. .num_databases = 4096,
  2857. .num_ports = 7,
  2858. .port_base_addr = 0x10,
  2859. .global1_addr = 0x1b,
  2860. .age_time_coeff = 15000,
  2861. .flags = MV88E6XXX_FLAGS_FAMILY_6351,
  2862. .ops = &mv88e6351_ops,
  2863. },
  2864. [MV88E6352] = {
  2865. .prod_num = PORT_SWITCH_ID_PROD_NUM_6352,
  2866. .family = MV88E6XXX_FAMILY_6352,
  2867. .name = "Marvell 88E6352",
  2868. .num_databases = 4096,
  2869. .num_ports = 7,
  2870. .port_base_addr = 0x10,
  2871. .global1_addr = 0x1b,
  2872. .age_time_coeff = 15000,
  2873. .flags = MV88E6XXX_FLAGS_FAMILY_6352,
  2874. .ops = &mv88e6352_ops,
  2875. },
  2876. };
  2877. static const struct mv88e6xxx_info *mv88e6xxx_lookup_info(unsigned int prod_num)
  2878. {
  2879. int i;
  2880. for (i = 0; i < ARRAY_SIZE(mv88e6xxx_table); ++i)
  2881. if (mv88e6xxx_table[i].prod_num == prod_num)
  2882. return &mv88e6xxx_table[i];
  2883. return NULL;
  2884. }
  2885. static int mv88e6xxx_detect(struct mv88e6xxx_chip *chip)
  2886. {
  2887. const struct mv88e6xxx_info *info;
  2888. unsigned int prod_num, rev;
  2889. u16 id;
  2890. int err;
  2891. mutex_lock(&chip->reg_lock);
  2892. err = mv88e6xxx_port_read(chip, 0, PORT_SWITCH_ID, &id);
  2893. mutex_unlock(&chip->reg_lock);
  2894. if (err)
  2895. return err;
  2896. prod_num = (id & 0xfff0) >> 4;
  2897. rev = id & 0x000f;
  2898. info = mv88e6xxx_lookup_info(prod_num);
  2899. if (!info)
  2900. return -ENODEV;
  2901. /* Update the compatible info with the probed one */
  2902. chip->info = info;
  2903. err = mv88e6xxx_g2_require(chip);
  2904. if (err)
  2905. return err;
  2906. dev_info(chip->dev, "switch 0x%x detected: %s, revision %u\n",
  2907. chip->info->prod_num, chip->info->name, rev);
  2908. return 0;
  2909. }
  2910. static struct mv88e6xxx_chip *mv88e6xxx_alloc_chip(struct device *dev)
  2911. {
  2912. struct mv88e6xxx_chip *chip;
  2913. chip = devm_kzalloc(dev, sizeof(*chip), GFP_KERNEL);
  2914. if (!chip)
  2915. return NULL;
  2916. chip->dev = dev;
  2917. mutex_init(&chip->reg_lock);
  2918. return chip;
  2919. }
  2920. static void mv88e6xxx_phy_init(struct mv88e6xxx_chip *chip)
  2921. {
  2922. if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_PPU))
  2923. mv88e6xxx_ppu_state_init(chip);
  2924. }
  2925. static void mv88e6xxx_phy_destroy(struct mv88e6xxx_chip *chip)
  2926. {
  2927. if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_PPU))
  2928. mv88e6xxx_ppu_state_destroy(chip);
  2929. }
  2930. static int mv88e6xxx_smi_init(struct mv88e6xxx_chip *chip,
  2931. struct mii_bus *bus, int sw_addr)
  2932. {
  2933. /* ADDR[0] pin is unavailable externally and considered zero */
  2934. if (sw_addr & 0x1)
  2935. return -EINVAL;
  2936. if (sw_addr == 0)
  2937. chip->smi_ops = &mv88e6xxx_smi_single_chip_ops;
  2938. else if (mv88e6xxx_has(chip, MV88E6XXX_FLAGS_MULTI_CHIP))
  2939. chip->smi_ops = &mv88e6xxx_smi_multi_chip_ops;
  2940. else
  2941. return -EINVAL;
  2942. chip->bus = bus;
  2943. chip->sw_addr = sw_addr;
  2944. return 0;
  2945. }
  2946. static enum dsa_tag_protocol mv88e6xxx_get_tag_protocol(struct dsa_switch *ds)
  2947. {
  2948. struct mv88e6xxx_chip *chip = ds->priv;
  2949. if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_EDSA))
  2950. return DSA_TAG_PROTO_EDSA;
  2951. return DSA_TAG_PROTO_DSA;
  2952. }
  2953. static const char *mv88e6xxx_drv_probe(struct device *dsa_dev,
  2954. struct device *host_dev, int sw_addr,
  2955. void **priv)
  2956. {
  2957. struct mv88e6xxx_chip *chip;
  2958. struct mii_bus *bus;
  2959. int err;
  2960. bus = dsa_host_dev_to_mii_bus(host_dev);
  2961. if (!bus)
  2962. return NULL;
  2963. chip = mv88e6xxx_alloc_chip(dsa_dev);
  2964. if (!chip)
  2965. return NULL;
  2966. /* Legacy SMI probing will only support chips similar to 88E6085 */
  2967. chip->info = &mv88e6xxx_table[MV88E6085];
  2968. err = mv88e6xxx_smi_init(chip, bus, sw_addr);
  2969. if (err)
  2970. goto free;
  2971. err = mv88e6xxx_detect(chip);
  2972. if (err)
  2973. goto free;
  2974. mv88e6xxx_phy_init(chip);
  2975. err = mv88e6xxx_mdio_register(chip, NULL);
  2976. if (err)
  2977. goto free;
  2978. *priv = chip;
  2979. return chip->info->name;
  2980. free:
  2981. devm_kfree(dsa_dev, chip);
  2982. return NULL;
  2983. }
  2984. static int mv88e6xxx_port_mdb_prepare(struct dsa_switch *ds, int port,
  2985. const struct switchdev_obj_port_mdb *mdb,
  2986. struct switchdev_trans *trans)
  2987. {
  2988. /* We don't need any dynamic resource from the kernel (yet),
  2989. * so skip the prepare phase.
  2990. */
  2991. return 0;
  2992. }
  2993. static void mv88e6xxx_port_mdb_add(struct dsa_switch *ds, int port,
  2994. const struct switchdev_obj_port_mdb *mdb,
  2995. struct switchdev_trans *trans)
  2996. {
  2997. struct mv88e6xxx_chip *chip = ds->priv;
  2998. mutex_lock(&chip->reg_lock);
  2999. if (mv88e6xxx_port_db_load_purge(chip, port, mdb->addr, mdb->vid,
  3000. GLOBAL_ATU_DATA_STATE_MC_STATIC))
  3001. netdev_err(ds->ports[port].netdev, "failed to load multicast MAC address\n");
  3002. mutex_unlock(&chip->reg_lock);
  3003. }
  3004. static int mv88e6xxx_port_mdb_del(struct dsa_switch *ds, int port,
  3005. const struct switchdev_obj_port_mdb *mdb)
  3006. {
  3007. struct mv88e6xxx_chip *chip = ds->priv;
  3008. int err;
  3009. mutex_lock(&chip->reg_lock);
  3010. err = mv88e6xxx_port_db_load_purge(chip, port, mdb->addr, mdb->vid,
  3011. GLOBAL_ATU_DATA_STATE_UNUSED);
  3012. mutex_unlock(&chip->reg_lock);
  3013. return err;
  3014. }
  3015. static int mv88e6xxx_port_mdb_dump(struct dsa_switch *ds, int port,
  3016. struct switchdev_obj_port_mdb *mdb,
  3017. int (*cb)(struct switchdev_obj *obj))
  3018. {
  3019. struct mv88e6xxx_chip *chip = ds->priv;
  3020. int err;
  3021. mutex_lock(&chip->reg_lock);
  3022. err = mv88e6xxx_port_db_dump(chip, port, &mdb->obj, cb);
  3023. mutex_unlock(&chip->reg_lock);
  3024. return err;
  3025. }
  3026. static struct dsa_switch_ops mv88e6xxx_switch_ops = {
  3027. .probe = mv88e6xxx_drv_probe,
  3028. .get_tag_protocol = mv88e6xxx_get_tag_protocol,
  3029. .setup = mv88e6xxx_setup,
  3030. .set_addr = mv88e6xxx_set_addr,
  3031. .adjust_link = mv88e6xxx_adjust_link,
  3032. .get_strings = mv88e6xxx_get_strings,
  3033. .get_ethtool_stats = mv88e6xxx_get_ethtool_stats,
  3034. .get_sset_count = mv88e6xxx_get_sset_count,
  3035. .set_eee = mv88e6xxx_set_eee,
  3036. .get_eee = mv88e6xxx_get_eee,
  3037. #ifdef CONFIG_NET_DSA_HWMON
  3038. .get_temp = mv88e6xxx_get_temp,
  3039. .get_temp_limit = mv88e6xxx_get_temp_limit,
  3040. .set_temp_limit = mv88e6xxx_set_temp_limit,
  3041. .get_temp_alarm = mv88e6xxx_get_temp_alarm,
  3042. #endif
  3043. .get_eeprom_len = mv88e6xxx_get_eeprom_len,
  3044. .get_eeprom = mv88e6xxx_get_eeprom,
  3045. .set_eeprom = mv88e6xxx_set_eeprom,
  3046. .get_regs_len = mv88e6xxx_get_regs_len,
  3047. .get_regs = mv88e6xxx_get_regs,
  3048. .set_ageing_time = mv88e6xxx_set_ageing_time,
  3049. .port_bridge_join = mv88e6xxx_port_bridge_join,
  3050. .port_bridge_leave = mv88e6xxx_port_bridge_leave,
  3051. .port_stp_state_set = mv88e6xxx_port_stp_state_set,
  3052. .port_fast_age = mv88e6xxx_port_fast_age,
  3053. .port_vlan_filtering = mv88e6xxx_port_vlan_filtering,
  3054. .port_vlan_prepare = mv88e6xxx_port_vlan_prepare,
  3055. .port_vlan_add = mv88e6xxx_port_vlan_add,
  3056. .port_vlan_del = mv88e6xxx_port_vlan_del,
  3057. .port_vlan_dump = mv88e6xxx_port_vlan_dump,
  3058. .port_fdb_prepare = mv88e6xxx_port_fdb_prepare,
  3059. .port_fdb_add = mv88e6xxx_port_fdb_add,
  3060. .port_fdb_del = mv88e6xxx_port_fdb_del,
  3061. .port_fdb_dump = mv88e6xxx_port_fdb_dump,
  3062. .port_mdb_prepare = mv88e6xxx_port_mdb_prepare,
  3063. .port_mdb_add = mv88e6xxx_port_mdb_add,
  3064. .port_mdb_del = mv88e6xxx_port_mdb_del,
  3065. .port_mdb_dump = mv88e6xxx_port_mdb_dump,
  3066. };
  3067. static int mv88e6xxx_register_switch(struct mv88e6xxx_chip *chip,
  3068. struct device_node *np)
  3069. {
  3070. struct device *dev = chip->dev;
  3071. struct dsa_switch *ds;
  3072. ds = devm_kzalloc(dev, sizeof(*ds), GFP_KERNEL);
  3073. if (!ds)
  3074. return -ENOMEM;
  3075. ds->dev = dev;
  3076. ds->priv = chip;
  3077. ds->ops = &mv88e6xxx_switch_ops;
  3078. dev_set_drvdata(dev, ds);
  3079. return dsa_register_switch(ds, np);
  3080. }
  3081. static void mv88e6xxx_unregister_switch(struct mv88e6xxx_chip *chip)
  3082. {
  3083. dsa_unregister_switch(chip->ds);
  3084. }
  3085. static int mv88e6xxx_probe(struct mdio_device *mdiodev)
  3086. {
  3087. struct device *dev = &mdiodev->dev;
  3088. struct device_node *np = dev->of_node;
  3089. const struct mv88e6xxx_info *compat_info;
  3090. struct mv88e6xxx_chip *chip;
  3091. u32 eeprom_len;
  3092. int err;
  3093. compat_info = of_device_get_match_data(dev);
  3094. if (!compat_info)
  3095. return -EINVAL;
  3096. chip = mv88e6xxx_alloc_chip(dev);
  3097. if (!chip)
  3098. return -ENOMEM;
  3099. chip->info = compat_info;
  3100. err = mv88e6xxx_smi_init(chip, mdiodev->bus, mdiodev->addr);
  3101. if (err)
  3102. return err;
  3103. err = mv88e6xxx_detect(chip);
  3104. if (err)
  3105. return err;
  3106. mv88e6xxx_phy_init(chip);
  3107. chip->reset = devm_gpiod_get_optional(dev, "reset", GPIOD_ASIS);
  3108. if (IS_ERR(chip->reset))
  3109. return PTR_ERR(chip->reset);
  3110. if (chip->info->ops->get_eeprom &&
  3111. !of_property_read_u32(np, "eeprom-length", &eeprom_len))
  3112. chip->eeprom_len = eeprom_len;
  3113. err = mv88e6xxx_mdio_register(chip, np);
  3114. if (err)
  3115. return err;
  3116. err = mv88e6xxx_register_switch(chip, np);
  3117. if (err) {
  3118. mv88e6xxx_mdio_unregister(chip);
  3119. return err;
  3120. }
  3121. return 0;
  3122. }
  3123. static void mv88e6xxx_remove(struct mdio_device *mdiodev)
  3124. {
  3125. struct dsa_switch *ds = dev_get_drvdata(&mdiodev->dev);
  3126. struct mv88e6xxx_chip *chip = ds->priv;
  3127. mv88e6xxx_phy_destroy(chip);
  3128. mv88e6xxx_unregister_switch(chip);
  3129. mv88e6xxx_mdio_unregister(chip);
  3130. }
  3131. static const struct of_device_id mv88e6xxx_of_match[] = {
  3132. {
  3133. .compatible = "marvell,mv88e6085",
  3134. .data = &mv88e6xxx_table[MV88E6085],
  3135. },
  3136. { /* sentinel */ },
  3137. };
  3138. MODULE_DEVICE_TABLE(of, mv88e6xxx_of_match);
  3139. static struct mdio_driver mv88e6xxx_driver = {
  3140. .probe = mv88e6xxx_probe,
  3141. .remove = mv88e6xxx_remove,
  3142. .mdiodrv.driver = {
  3143. .name = "mv88e6085",
  3144. .of_match_table = mv88e6xxx_of_match,
  3145. },
  3146. };
  3147. static int __init mv88e6xxx_init(void)
  3148. {
  3149. register_switch_driver(&mv88e6xxx_switch_ops);
  3150. return mdio_driver_register(&mv88e6xxx_driver);
  3151. }
  3152. module_init(mv88e6xxx_init);
  3153. static void __exit mv88e6xxx_cleanup(void)
  3154. {
  3155. mdio_driver_unregister(&mv88e6xxx_driver);
  3156. unregister_switch_driver(&mv88e6xxx_switch_ops);
  3157. }
  3158. module_exit(mv88e6xxx_cleanup);
  3159. MODULE_AUTHOR("Lennert Buytenhek <buytenh@wantstofly.org>");
  3160. MODULE_DESCRIPTION("Driver for Marvell 88E6XXX ethernet switch chips");
  3161. MODULE_LICENSE("GPL");