ar8216.c 59 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565
  1. /*
  2. * ar8216.c: AR8216 switch driver
  3. *
  4. * Copyright (C) 2009 Felix Fietkau <[email protected]>
  5. * Copyright (C) 2011-2012 Gabor Juhos <[email protected]>
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version 2
  10. * of the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. */
  17. #include <linux/if.h>
  18. #include <linux/module.h>
  19. #include <linux/init.h>
  20. #include <linux/list.h>
  21. #include <linux/if_ether.h>
  22. #include <linux/skbuff.h>
  23. #include <linux/netdevice.h>
  24. #include <linux/netlink.h>
  25. #include <linux/bitops.h>
  26. #include <net/genetlink.h>
  27. #include <linux/switch.h>
  28. #include <linux/delay.h>
  29. #include <linux/phy.h>
  30. #include <linux/netdevice.h>
  31. #include <linux/etherdevice.h>
  32. #include <linux/lockdep.h>
  33. #include <linux/ar8216_platform.h>
  34. #include <linux/workqueue.h>
  35. #include <linux/of_device.h>
  36. #include "ar8216.h"
  37. /* size of the vlan table */
  38. #define AR8X16_MAX_VLANS 128
  39. #define AR8X16_PROBE_RETRIES 10
  40. #define AR8X16_MAX_PORTS 8
  41. #define AR8XXX_MIB_WORK_DELAY 2000 /* msecs */
  42. struct ar8xxx_priv;
  43. #define AR8XXX_CAP_GIGE BIT(0)
  44. #define AR8XXX_CAP_MIB_COUNTERS BIT(1)
  45. enum {
  46. AR8XXX_VER_AR8216 = 0x01,
  47. AR8XXX_VER_AR8236 = 0x03,
  48. AR8XXX_VER_AR8316 = 0x10,
  49. AR8XXX_VER_AR8327 = 0x12,
  50. };
  51. struct ar8xxx_mib_desc {
  52. unsigned int size;
  53. unsigned int offset;
  54. const char *name;
  55. };
  56. struct ar8xxx_chip {
  57. unsigned long caps;
  58. int (*hw_init)(struct ar8xxx_priv *priv);
  59. void (*init_globals)(struct ar8xxx_priv *priv);
  60. void (*init_port)(struct ar8xxx_priv *priv, int port);
  61. void (*setup_port)(struct ar8xxx_priv *priv, int port, u32 egress,
  62. u32 ingress, u32 members, u32 pvid);
  63. u32 (*read_port_status)(struct ar8xxx_priv *priv, int port);
  64. int (*atu_flush)(struct ar8xxx_priv *priv);
  65. void (*vtu_flush)(struct ar8xxx_priv *priv);
  66. void (*vtu_load_vlan)(struct ar8xxx_priv *priv, u32 vid, u32 port_mask);
  67. const struct ar8xxx_mib_desc *mib_decs;
  68. unsigned num_mibs;
  69. };
  70. struct ar8327_data {
  71. u32 port0_status;
  72. u32 port6_status;
  73. };
  74. struct ar8xxx_priv {
  75. struct switch_dev dev;
  76. struct mii_bus *mii_bus;
  77. struct phy_device *phy;
  78. u32 (*read)(struct ar8xxx_priv *priv, int reg);
  79. void (*write)(struct ar8xxx_priv *priv, int reg, u32 val);
  80. int (*get_port_link)(unsigned port);
  81. const struct net_device_ops *ndo_old;
  82. struct net_device_ops ndo;
  83. struct mutex reg_mutex;
  84. u8 chip_ver;
  85. u8 chip_rev;
  86. const struct ar8xxx_chip *chip;
  87. union {
  88. struct ar8327_data ar8327;
  89. } chip_data;
  90. bool initialized;
  91. bool port4_phy;
  92. char buf[2048];
  93. bool init;
  94. bool mii_lo_first;
  95. struct mutex mib_lock;
  96. struct delayed_work mib_work;
  97. int mib_next_port;
  98. u64 *mib_stats;
  99. struct list_head list;
  100. unsigned int use_count;
  101. /* all fields below are cleared on reset */
  102. bool vlan;
  103. u16 vlan_id[AR8X16_MAX_VLANS];
  104. u8 vlan_table[AR8X16_MAX_VLANS];
  105. u8 vlan_tagged;
  106. u16 pvid[AR8X16_MAX_PORTS];
  107. /* mirroring */
  108. bool mirror_rx;
  109. bool mirror_tx;
  110. int source_port;
  111. int monitor_port;
  112. };
  113. #define MIB_DESC(_s , _o, _n) \
  114. { \
  115. .size = (_s), \
  116. .offset = (_o), \
  117. .name = (_n), \
  118. }
  119. static const struct ar8xxx_mib_desc ar8216_mibs[] = {
  120. MIB_DESC(1, AR8216_STATS_RXBROAD, "RxBroad"),
  121. MIB_DESC(1, AR8216_STATS_RXPAUSE, "RxPause"),
  122. MIB_DESC(1, AR8216_STATS_RXMULTI, "RxMulti"),
  123. MIB_DESC(1, AR8216_STATS_RXFCSERR, "RxFcsErr"),
  124. MIB_DESC(1, AR8216_STATS_RXALIGNERR, "RxAlignErr"),
  125. MIB_DESC(1, AR8216_STATS_RXRUNT, "RxRunt"),
  126. MIB_DESC(1, AR8216_STATS_RXFRAGMENT, "RxFragment"),
  127. MIB_DESC(1, AR8216_STATS_RX64BYTE, "Rx64Byte"),
  128. MIB_DESC(1, AR8216_STATS_RX128BYTE, "Rx128Byte"),
  129. MIB_DESC(1, AR8216_STATS_RX256BYTE, "Rx256Byte"),
  130. MIB_DESC(1, AR8216_STATS_RX512BYTE, "Rx512Byte"),
  131. MIB_DESC(1, AR8216_STATS_RX1024BYTE, "Rx1024Byte"),
  132. MIB_DESC(1, AR8216_STATS_RXMAXBYTE, "RxMaxByte"),
  133. MIB_DESC(1, AR8216_STATS_RXTOOLONG, "RxTooLong"),
  134. MIB_DESC(2, AR8216_STATS_RXGOODBYTE, "RxGoodByte"),
  135. MIB_DESC(2, AR8216_STATS_RXBADBYTE, "RxBadByte"),
  136. MIB_DESC(1, AR8216_STATS_RXOVERFLOW, "RxOverFlow"),
  137. MIB_DESC(1, AR8216_STATS_FILTERED, "Filtered"),
  138. MIB_DESC(1, AR8216_STATS_TXBROAD, "TxBroad"),
  139. MIB_DESC(1, AR8216_STATS_TXPAUSE, "TxPause"),
  140. MIB_DESC(1, AR8216_STATS_TXMULTI, "TxMulti"),
  141. MIB_DESC(1, AR8216_STATS_TXUNDERRUN, "TxUnderRun"),
  142. MIB_DESC(1, AR8216_STATS_TX64BYTE, "Tx64Byte"),
  143. MIB_DESC(1, AR8216_STATS_TX128BYTE, "Tx128Byte"),
  144. MIB_DESC(1, AR8216_STATS_TX256BYTE, "Tx256Byte"),
  145. MIB_DESC(1, AR8216_STATS_TX512BYTE, "Tx512Byte"),
  146. MIB_DESC(1, AR8216_STATS_TX1024BYTE, "Tx1024Byte"),
  147. MIB_DESC(1, AR8216_STATS_TXMAXBYTE, "TxMaxByte"),
  148. MIB_DESC(1, AR8216_STATS_TXOVERSIZE, "TxOverSize"),
  149. MIB_DESC(2, AR8216_STATS_TXBYTE, "TxByte"),
  150. MIB_DESC(1, AR8216_STATS_TXCOLLISION, "TxCollision"),
  151. MIB_DESC(1, AR8216_STATS_TXABORTCOL, "TxAbortCol"),
  152. MIB_DESC(1, AR8216_STATS_TXMULTICOL, "TxMultiCol"),
  153. MIB_DESC(1, AR8216_STATS_TXSINGLECOL, "TxSingleCol"),
  154. MIB_DESC(1, AR8216_STATS_TXEXCDEFER, "TxExcDefer"),
  155. MIB_DESC(1, AR8216_STATS_TXDEFER, "TxDefer"),
  156. MIB_DESC(1, AR8216_STATS_TXLATECOL, "TxLateCol"),
  157. };
  158. static const struct ar8xxx_mib_desc ar8236_mibs[] = {
  159. MIB_DESC(1, AR8236_STATS_RXBROAD, "RxBroad"),
  160. MIB_DESC(1, AR8236_STATS_RXPAUSE, "RxPause"),
  161. MIB_DESC(1, AR8236_STATS_RXMULTI, "RxMulti"),
  162. MIB_DESC(1, AR8236_STATS_RXFCSERR, "RxFcsErr"),
  163. MIB_DESC(1, AR8236_STATS_RXALIGNERR, "RxAlignErr"),
  164. MIB_DESC(1, AR8236_STATS_RXRUNT, "RxRunt"),
  165. MIB_DESC(1, AR8236_STATS_RXFRAGMENT, "RxFragment"),
  166. MIB_DESC(1, AR8236_STATS_RX64BYTE, "Rx64Byte"),
  167. MIB_DESC(1, AR8236_STATS_RX128BYTE, "Rx128Byte"),
  168. MIB_DESC(1, AR8236_STATS_RX256BYTE, "Rx256Byte"),
  169. MIB_DESC(1, AR8236_STATS_RX512BYTE, "Rx512Byte"),
  170. MIB_DESC(1, AR8236_STATS_RX1024BYTE, "Rx1024Byte"),
  171. MIB_DESC(1, AR8236_STATS_RX1518BYTE, "Rx1518Byte"),
  172. MIB_DESC(1, AR8236_STATS_RXMAXBYTE, "RxMaxByte"),
  173. MIB_DESC(1, AR8236_STATS_RXTOOLONG, "RxTooLong"),
  174. MIB_DESC(2, AR8236_STATS_RXGOODBYTE, "RxGoodByte"),
  175. MIB_DESC(2, AR8236_STATS_RXBADBYTE, "RxBadByte"),
  176. MIB_DESC(1, AR8236_STATS_RXOVERFLOW, "RxOverFlow"),
  177. MIB_DESC(1, AR8236_STATS_FILTERED, "Filtered"),
  178. MIB_DESC(1, AR8236_STATS_TXBROAD, "TxBroad"),
  179. MIB_DESC(1, AR8236_STATS_TXPAUSE, "TxPause"),
  180. MIB_DESC(1, AR8236_STATS_TXMULTI, "TxMulti"),
  181. MIB_DESC(1, AR8236_STATS_TXUNDERRUN, "TxUnderRun"),
  182. MIB_DESC(1, AR8236_STATS_TX64BYTE, "Tx64Byte"),
  183. MIB_DESC(1, AR8236_STATS_TX128BYTE, "Tx128Byte"),
  184. MIB_DESC(1, AR8236_STATS_TX256BYTE, "Tx256Byte"),
  185. MIB_DESC(1, AR8236_STATS_TX512BYTE, "Tx512Byte"),
  186. MIB_DESC(1, AR8236_STATS_TX1024BYTE, "Tx1024Byte"),
  187. MIB_DESC(1, AR8236_STATS_TX1518BYTE, "Tx1518Byte"),
  188. MIB_DESC(1, AR8236_STATS_TXMAXBYTE, "TxMaxByte"),
  189. MIB_DESC(1, AR8236_STATS_TXOVERSIZE, "TxOverSize"),
  190. MIB_DESC(2, AR8236_STATS_TXBYTE, "TxByte"),
  191. MIB_DESC(1, AR8236_STATS_TXCOLLISION, "TxCollision"),
  192. MIB_DESC(1, AR8236_STATS_TXABORTCOL, "TxAbortCol"),
  193. MIB_DESC(1, AR8236_STATS_TXMULTICOL, "TxMultiCol"),
  194. MIB_DESC(1, AR8236_STATS_TXSINGLECOL, "TxSingleCol"),
  195. MIB_DESC(1, AR8236_STATS_TXEXCDEFER, "TxExcDefer"),
  196. MIB_DESC(1, AR8236_STATS_TXDEFER, "TxDefer"),
  197. MIB_DESC(1, AR8236_STATS_TXLATECOL, "TxLateCol"),
  198. };
  199. static DEFINE_MUTEX(ar8xxx_dev_list_lock);
  200. static LIST_HEAD(ar8xxx_dev_list);
  201. static inline struct ar8xxx_priv *
  202. swdev_to_ar8xxx(struct switch_dev *swdev)
  203. {
  204. return container_of(swdev, struct ar8xxx_priv, dev);
  205. }
  206. static inline bool ar8xxx_has_gige(struct ar8xxx_priv *priv)
  207. {
  208. return priv->chip->caps & AR8XXX_CAP_GIGE;
  209. }
  210. static inline bool ar8xxx_has_mib_counters(struct ar8xxx_priv *priv)
  211. {
  212. return priv->chip->caps & AR8XXX_CAP_MIB_COUNTERS;
  213. }
  214. static inline bool chip_is_ar8216(struct ar8xxx_priv *priv)
  215. {
  216. return priv->chip_ver == AR8XXX_VER_AR8216;
  217. }
  218. static inline bool chip_is_ar8236(struct ar8xxx_priv *priv)
  219. {
  220. return priv->chip_ver == AR8XXX_VER_AR8236;
  221. }
  222. static inline bool chip_is_ar8316(struct ar8xxx_priv *priv)
  223. {
  224. return priv->chip_ver == AR8XXX_VER_AR8316;
  225. }
  226. static inline bool chip_is_ar8327(struct ar8xxx_priv *priv)
  227. {
  228. return priv->chip_ver == AR8XXX_VER_AR8327;
  229. }
  230. static inline void
  231. split_addr(u32 regaddr, u16 *r1, u16 *r2, u16 *page)
  232. {
  233. regaddr >>= 1;
  234. *r1 = regaddr & 0x1e;
  235. regaddr >>= 5;
  236. *r2 = regaddr & 0x7;
  237. regaddr >>= 3;
  238. *page = regaddr & 0x1ff;
  239. }
  240. static u32
  241. ar8xxx_mii_read(struct ar8xxx_priv *priv, int reg)
  242. {
  243. struct mii_bus *bus = priv->mii_bus;
  244. u16 r1, r2, page;
  245. u16 lo, hi;
  246. split_addr((u32) reg, &r1, &r2, &page);
  247. mutex_lock(&bus->mdio_lock);
  248. bus->write(bus, 0x18, 0, page);
  249. usleep_range(1000, 2000); /* wait for the page switch to propagate */
  250. lo = bus->read(bus, 0x10 | r2, r1);
  251. hi = bus->read(bus, 0x10 | r2, r1 + 1);
  252. mutex_unlock(&bus->mdio_lock);
  253. return (hi << 16) | lo;
  254. }
  255. static void
  256. ar8xxx_mii_write(struct ar8xxx_priv *priv, int reg, u32 val)
  257. {
  258. struct mii_bus *bus = priv->mii_bus;
  259. u16 r1, r2, r3;
  260. u16 lo, hi;
  261. split_addr((u32) reg, &r1, &r2, &r3);
  262. lo = val & 0xffff;
  263. hi = (u16) (val >> 16);
  264. mutex_lock(&bus->mdio_lock);
  265. bus->write(bus, 0x18, 0, r3);
  266. usleep_range(1000, 2000); /* wait for the page switch to propagate */
  267. if (priv->mii_lo_first) {
  268. bus->write(bus, 0x10 | r2, r1, lo);
  269. bus->write(bus, 0x10 | r2, r1 + 1, hi);
  270. } else {
  271. bus->write(bus, 0x10 | r2, r1 + 1, hi);
  272. bus->write(bus, 0x10 | r2, r1, lo);
  273. }
  274. mutex_unlock(&bus->mdio_lock);
  275. }
  276. static void
  277. ar8xxx_phy_dbg_write(struct ar8xxx_priv *priv, int phy_addr,
  278. u16 dbg_addr, u16 dbg_data)
  279. {
  280. struct mii_bus *bus = priv->mii_bus;
  281. mutex_lock(&bus->mdio_lock);
  282. bus->write(bus, phy_addr, MII_ATH_DBG_ADDR, dbg_addr);
  283. bus->write(bus, phy_addr, MII_ATH_DBG_DATA, dbg_data);
  284. mutex_unlock(&bus->mdio_lock);
  285. }
  286. static void
  287. ar8xxx_phy_mmd_write(struct ar8xxx_priv *priv, int phy_addr, u16 addr, u16 data)
  288. {
  289. struct mii_bus *bus = priv->mii_bus;
  290. mutex_lock(&bus->mdio_lock);
  291. bus->write(bus, phy_addr, MII_ATH_MMD_ADDR, addr);
  292. bus->write(bus, phy_addr, MII_ATH_MMD_DATA, data);
  293. mutex_unlock(&bus->mdio_lock);
  294. }
  295. static u32
  296. ar8xxx_rmw(struct ar8xxx_priv *priv, int reg, u32 mask, u32 val)
  297. {
  298. u32 v;
  299. lockdep_assert_held(&priv->reg_mutex);
  300. v = priv->read(priv, reg);
  301. v &= ~mask;
  302. v |= val;
  303. priv->write(priv, reg, v);
  304. return v;
  305. }
  306. static inline void
  307. ar8xxx_reg_set(struct ar8xxx_priv *priv, int reg, u32 val)
  308. {
  309. u32 v;
  310. lockdep_assert_held(&priv->reg_mutex);
  311. v = priv->read(priv, reg);
  312. v |= val;
  313. priv->write(priv, reg, v);
  314. }
  315. static int
  316. ar8xxx_reg_wait(struct ar8xxx_priv *priv, u32 reg, u32 mask, u32 val,
  317. unsigned timeout)
  318. {
  319. int i;
  320. for (i = 0; i < timeout; i++) {
  321. u32 t;
  322. t = priv->read(priv, reg);
  323. if ((t & mask) == val)
  324. return 0;
  325. usleep_range(1000, 2000);
  326. }
  327. return -ETIMEDOUT;
  328. }
  329. static int
  330. ar8xxx_mib_op(struct ar8xxx_priv *priv, u32 op)
  331. {
  332. unsigned mib_func;
  333. int ret;
  334. lockdep_assert_held(&priv->mib_lock);
  335. if (chip_is_ar8327(priv))
  336. mib_func = AR8327_REG_MIB_FUNC;
  337. else
  338. mib_func = AR8216_REG_MIB_FUNC;
  339. mutex_lock(&priv->reg_mutex);
  340. /* Capture the hardware statistics for all ports */
  341. ar8xxx_rmw(priv, mib_func, AR8216_MIB_FUNC, (op << AR8216_MIB_FUNC_S));
  342. mutex_unlock(&priv->reg_mutex);
  343. /* Wait for the capturing to complete. */
  344. ret = ar8xxx_reg_wait(priv, mib_func, AR8216_MIB_BUSY, 0, 10);
  345. if (ret)
  346. goto out;
  347. ret = 0;
  348. out:
  349. return ret;
  350. }
  351. static int
  352. ar8xxx_mib_capture(struct ar8xxx_priv *priv)
  353. {
  354. return ar8xxx_mib_op(priv, AR8216_MIB_FUNC_CAPTURE);
  355. }
  356. static int
  357. ar8xxx_mib_flush(struct ar8xxx_priv *priv)
  358. {
  359. return ar8xxx_mib_op(priv, AR8216_MIB_FUNC_FLUSH);
  360. }
  361. static void
  362. ar8xxx_mib_fetch_port_stat(struct ar8xxx_priv *priv, int port, bool flush)
  363. {
  364. unsigned int base;
  365. u64 *mib_stats;
  366. int i;
  367. WARN_ON(port >= priv->dev.ports);
  368. lockdep_assert_held(&priv->mib_lock);
  369. if (chip_is_ar8327(priv))
  370. base = AR8327_REG_PORT_STATS_BASE(port);
  371. else if (chip_is_ar8236(priv) ||
  372. chip_is_ar8316(priv))
  373. base = AR8236_REG_PORT_STATS_BASE(port);
  374. else
  375. base = AR8216_REG_PORT_STATS_BASE(port);
  376. mib_stats = &priv->mib_stats[port * priv->chip->num_mibs];
  377. for (i = 0; i < priv->chip->num_mibs; i++) {
  378. const struct ar8xxx_mib_desc *mib;
  379. u64 t;
  380. mib = &priv->chip->mib_decs[i];
  381. t = priv->read(priv, base + mib->offset);
  382. if (mib->size == 2) {
  383. u64 hi;
  384. hi = priv->read(priv, base + mib->offset + 4);
  385. t |= hi << 32;
  386. }
  387. if (flush)
  388. mib_stats[i] = 0;
  389. else
  390. mib_stats[i] += t;
  391. }
  392. }
  393. static void
  394. ar8216_read_port_link(struct ar8xxx_priv *priv, int port,
  395. struct switch_port_link *link)
  396. {
  397. u32 status;
  398. u32 speed;
  399. memset(link, '\0', sizeof(*link));
  400. status = priv->chip->read_port_status(priv, port);
  401. link->aneg = !!(status & AR8216_PORT_STATUS_LINK_AUTO);
  402. if (link->aneg) {
  403. link->link = !!(status & AR8216_PORT_STATUS_LINK_UP);
  404. } else {
  405. link->link = true;
  406. if (priv->get_port_link) {
  407. int err;
  408. err = priv->get_port_link(port);
  409. if (err >= 0)
  410. link->link = !!err;
  411. }
  412. }
  413. if (!link->link)
  414. return;
  415. link->duplex = !!(status & AR8216_PORT_STATUS_DUPLEX);
  416. link->tx_flow = !!(status & AR8216_PORT_STATUS_TXFLOW);
  417. link->rx_flow = !!(status & AR8216_PORT_STATUS_RXFLOW);
  418. speed = (status & AR8216_PORT_STATUS_SPEED) >>
  419. AR8216_PORT_STATUS_SPEED_S;
  420. switch (speed) {
  421. case AR8216_PORT_SPEED_10M:
  422. link->speed = SWITCH_PORT_SPEED_10;
  423. break;
  424. case AR8216_PORT_SPEED_100M:
  425. link->speed = SWITCH_PORT_SPEED_100;
  426. break;
  427. case AR8216_PORT_SPEED_1000M:
  428. link->speed = SWITCH_PORT_SPEED_1000;
  429. break;
  430. default:
  431. link->speed = SWITCH_PORT_SPEED_UNKNOWN;
  432. break;
  433. }
  434. }
  435. static struct sk_buff *
  436. ar8216_mangle_tx(struct net_device *dev, struct sk_buff *skb)
  437. {
  438. struct ar8xxx_priv *priv = dev->phy_ptr;
  439. unsigned char *buf;
  440. if (unlikely(!priv))
  441. goto error;
  442. if (!priv->vlan)
  443. goto send;
  444. if (unlikely(skb_headroom(skb) < 2)) {
  445. if (pskb_expand_head(skb, 2, 0, GFP_ATOMIC) < 0)
  446. goto error;
  447. }
  448. buf = skb_push(skb, 2);
  449. buf[0] = 0x10;
  450. buf[1] = 0x80;
  451. send:
  452. return skb;
  453. error:
  454. dev_kfree_skb_any(skb);
  455. return NULL;
  456. }
  457. static void
  458. ar8216_mangle_rx(struct net_device *dev, struct sk_buff *skb)
  459. {
  460. struct ar8xxx_priv *priv;
  461. unsigned char *buf;
  462. int port, vlan;
  463. priv = dev->phy_ptr;
  464. if (!priv)
  465. return;
  466. /* don't strip the header if vlan mode is disabled */
  467. if (!priv->vlan)
  468. return;
  469. /* strip header, get vlan id */
  470. buf = skb->data;
  471. skb_pull(skb, 2);
  472. /* check for vlan header presence */
  473. if ((buf[12 + 2] != 0x81) || (buf[13 + 2] != 0x00))
  474. return;
  475. port = buf[0] & 0xf;
  476. /* no need to fix up packets coming from a tagged source */
  477. if (priv->vlan_tagged & (1 << port))
  478. return;
  479. /* lookup port vid from local table, the switch passes an invalid vlan id */
  480. vlan = priv->vlan_id[priv->pvid[port]];
  481. buf[14 + 2] &= 0xf0;
  482. buf[14 + 2] |= vlan >> 8;
  483. buf[15 + 2] = vlan & 0xff;
  484. }
  485. static int
  486. ar8216_wait_bit(struct ar8xxx_priv *priv, int reg, u32 mask, u32 val)
  487. {
  488. int timeout = 20;
  489. u32 t = 0;
  490. while (1) {
  491. t = priv->read(priv, reg);
  492. if ((t & mask) == val)
  493. return 0;
  494. if (timeout-- <= 0)
  495. break;
  496. udelay(10);
  497. }
  498. pr_err("ar8216: timeout on reg %08x: %08x & %08x != %08x\n",
  499. (unsigned int) reg, t, mask, val);
  500. return -ETIMEDOUT;
  501. }
  502. static void
  503. ar8216_vtu_op(struct ar8xxx_priv *priv, u32 op, u32 val)
  504. {
  505. if (ar8216_wait_bit(priv, AR8216_REG_VTU, AR8216_VTU_ACTIVE, 0))
  506. return;
  507. if ((op & AR8216_VTU_OP) == AR8216_VTU_OP_LOAD) {
  508. val &= AR8216_VTUDATA_MEMBER;
  509. val |= AR8216_VTUDATA_VALID;
  510. priv->write(priv, AR8216_REG_VTU_DATA, val);
  511. }
  512. op |= AR8216_VTU_ACTIVE;
  513. priv->write(priv, AR8216_REG_VTU, op);
  514. }
  515. static void
  516. ar8216_vtu_flush(struct ar8xxx_priv *priv)
  517. {
  518. ar8216_vtu_op(priv, AR8216_VTU_OP_FLUSH, 0);
  519. }
  520. static void
  521. ar8216_vtu_load_vlan(struct ar8xxx_priv *priv, u32 vid, u32 port_mask)
  522. {
  523. u32 op;
  524. op = AR8216_VTU_OP_LOAD | (vid << AR8216_VTU_VID_S);
  525. ar8216_vtu_op(priv, op, port_mask);
  526. }
  527. static int
  528. ar8216_atu_flush(struct ar8xxx_priv *priv)
  529. {
  530. int ret;
  531. ret = ar8216_wait_bit(priv, AR8216_REG_ATU, AR8216_ATU_ACTIVE, 0);
  532. if (!ret)
  533. priv->write(priv, AR8216_REG_ATU, AR8216_ATU_OP_FLUSH);
  534. return ret;
  535. }
  536. static u32
  537. ar8216_read_port_status(struct ar8xxx_priv *priv, int port)
  538. {
  539. return priv->read(priv, AR8216_REG_PORT_STATUS(port));
  540. }
  541. static void
  542. ar8216_setup_port(struct ar8xxx_priv *priv, int port, u32 egress, u32 ingress,
  543. u32 members, u32 pvid)
  544. {
  545. u32 header;
  546. if (chip_is_ar8216(priv) && priv->vlan && port == AR8216_PORT_CPU)
  547. header = AR8216_PORT_CTRL_HEADER;
  548. else
  549. header = 0;
  550. ar8xxx_rmw(priv, AR8216_REG_PORT_CTRL(port),
  551. AR8216_PORT_CTRL_LEARN | AR8216_PORT_CTRL_VLAN_MODE |
  552. AR8216_PORT_CTRL_SINGLE_VLAN | AR8216_PORT_CTRL_STATE |
  553. AR8216_PORT_CTRL_HEADER | AR8216_PORT_CTRL_LEARN_LOCK,
  554. AR8216_PORT_CTRL_LEARN | header |
  555. (egress << AR8216_PORT_CTRL_VLAN_MODE_S) |
  556. (AR8216_PORT_STATE_FORWARD << AR8216_PORT_CTRL_STATE_S));
  557. ar8xxx_rmw(priv, AR8216_REG_PORT_VLAN(port),
  558. AR8216_PORT_VLAN_DEST_PORTS | AR8216_PORT_VLAN_MODE |
  559. AR8216_PORT_VLAN_DEFAULT_ID,
  560. (members << AR8216_PORT_VLAN_DEST_PORTS_S) |
  561. (ingress << AR8216_PORT_VLAN_MODE_S) |
  562. (pvid << AR8216_PORT_VLAN_DEFAULT_ID_S));
  563. }
  564. static int
  565. ar8216_hw_init(struct ar8xxx_priv *priv)
  566. {
  567. return 0;
  568. }
  569. static void
  570. ar8216_init_globals(struct ar8xxx_priv *priv)
  571. {
  572. /* standard atheros magic */
  573. priv->write(priv, 0x38, 0xc000050e);
  574. ar8xxx_rmw(priv, AR8216_REG_GLOBAL_CTRL,
  575. AR8216_GCTRL_MTU, 1518 + 8 + 2);
  576. }
  577. static void
  578. ar8216_init_port(struct ar8xxx_priv *priv, int port)
  579. {
  580. /* Enable port learning and tx */
  581. priv->write(priv, AR8216_REG_PORT_CTRL(port),
  582. AR8216_PORT_CTRL_LEARN |
  583. (4 << AR8216_PORT_CTRL_STATE_S));
  584. priv->write(priv, AR8216_REG_PORT_VLAN(port), 0);
  585. if (port == AR8216_PORT_CPU) {
  586. priv->write(priv, AR8216_REG_PORT_STATUS(port),
  587. AR8216_PORT_STATUS_LINK_UP |
  588. (ar8xxx_has_gige(priv) ?
  589. AR8216_PORT_SPEED_1000M : AR8216_PORT_SPEED_100M) |
  590. AR8216_PORT_STATUS_TXMAC |
  591. AR8216_PORT_STATUS_RXMAC |
  592. (chip_is_ar8316(priv) ? AR8216_PORT_STATUS_RXFLOW : 0) |
  593. (chip_is_ar8316(priv) ? AR8216_PORT_STATUS_TXFLOW : 0) |
  594. AR8216_PORT_STATUS_DUPLEX);
  595. } else {
  596. priv->write(priv, AR8216_REG_PORT_STATUS(port),
  597. AR8216_PORT_STATUS_LINK_AUTO);
  598. }
  599. }
  600. static const struct ar8xxx_chip ar8216_chip = {
  601. .caps = AR8XXX_CAP_MIB_COUNTERS,
  602. .hw_init = ar8216_hw_init,
  603. .init_globals = ar8216_init_globals,
  604. .init_port = ar8216_init_port,
  605. .setup_port = ar8216_setup_port,
  606. .read_port_status = ar8216_read_port_status,
  607. .atu_flush = ar8216_atu_flush,
  608. .vtu_flush = ar8216_vtu_flush,
  609. .vtu_load_vlan = ar8216_vtu_load_vlan,
  610. .num_mibs = ARRAY_SIZE(ar8216_mibs),
  611. .mib_decs = ar8216_mibs,
  612. };
  613. static void
  614. ar8236_setup_port(struct ar8xxx_priv *priv, int port, u32 egress, u32 ingress,
  615. u32 members, u32 pvid)
  616. {
  617. ar8xxx_rmw(priv, AR8216_REG_PORT_CTRL(port),
  618. AR8216_PORT_CTRL_LEARN | AR8216_PORT_CTRL_VLAN_MODE |
  619. AR8216_PORT_CTRL_SINGLE_VLAN | AR8216_PORT_CTRL_STATE |
  620. AR8216_PORT_CTRL_HEADER | AR8216_PORT_CTRL_LEARN_LOCK,
  621. AR8216_PORT_CTRL_LEARN |
  622. (egress << AR8216_PORT_CTRL_VLAN_MODE_S) |
  623. (AR8216_PORT_STATE_FORWARD << AR8216_PORT_CTRL_STATE_S));
  624. ar8xxx_rmw(priv, AR8236_REG_PORT_VLAN(port),
  625. AR8236_PORT_VLAN_DEFAULT_ID,
  626. (pvid << AR8236_PORT_VLAN_DEFAULT_ID_S));
  627. ar8xxx_rmw(priv, AR8236_REG_PORT_VLAN2(port),
  628. AR8236_PORT_VLAN2_VLAN_MODE |
  629. AR8236_PORT_VLAN2_MEMBER,
  630. (ingress << AR8236_PORT_VLAN2_VLAN_MODE_S) |
  631. (members << AR8236_PORT_VLAN2_MEMBER_S));
  632. }
  633. static int
  634. ar8236_hw_init(struct ar8xxx_priv *priv)
  635. {
  636. int i;
  637. struct mii_bus *bus;
  638. if (priv->initialized)
  639. return 0;
  640. /* Initialize the PHYs */
  641. bus = priv->mii_bus;
  642. for (i = 0; i < 5; i++) {
  643. mdiobus_write(bus, i, MII_ADVERTISE,
  644. ADVERTISE_ALL | ADVERTISE_PAUSE_CAP |
  645. ADVERTISE_PAUSE_ASYM);
  646. mdiobus_write(bus, i, MII_BMCR, BMCR_RESET | BMCR_ANENABLE);
  647. }
  648. msleep(1000);
  649. priv->initialized = true;
  650. return 0;
  651. }
  652. static void
  653. ar8236_init_globals(struct ar8xxx_priv *priv)
  654. {
  655. /* enable jumbo frames */
  656. ar8xxx_rmw(priv, AR8216_REG_GLOBAL_CTRL,
  657. AR8316_GCTRL_MTU, 9018 + 8 + 2);
  658. /* Enable MIB counters */
  659. ar8xxx_rmw(priv, AR8216_REG_MIB_FUNC, AR8216_MIB_FUNC | AR8236_MIB_EN,
  660. (AR8216_MIB_FUNC_NO_OP << AR8216_MIB_FUNC_S) |
  661. AR8236_MIB_EN);
  662. }
  663. static const struct ar8xxx_chip ar8236_chip = {
  664. .caps = AR8XXX_CAP_MIB_COUNTERS,
  665. .hw_init = ar8236_hw_init,
  666. .init_globals = ar8236_init_globals,
  667. .init_port = ar8216_init_port,
  668. .setup_port = ar8236_setup_port,
  669. .read_port_status = ar8216_read_port_status,
  670. .atu_flush = ar8216_atu_flush,
  671. .vtu_flush = ar8216_vtu_flush,
  672. .vtu_load_vlan = ar8216_vtu_load_vlan,
  673. .num_mibs = ARRAY_SIZE(ar8236_mibs),
  674. .mib_decs = ar8236_mibs,
  675. };
  676. static int
  677. ar8316_hw_init(struct ar8xxx_priv *priv)
  678. {
  679. int i;
  680. u32 val, newval;
  681. struct mii_bus *bus;
  682. val = priv->read(priv, AR8316_REG_POSTRIP);
  683. if (priv->phy->interface == PHY_INTERFACE_MODE_RGMII) {
  684. if (priv->port4_phy) {
  685. /* value taken from Ubiquiti RouterStation Pro */
  686. newval = 0x81461bea;
  687. pr_info("ar8316: Using port 4 as PHY\n");
  688. } else {
  689. newval = 0x01261be2;
  690. pr_info("ar8316: Using port 4 as switch port\n");
  691. }
  692. } else if (priv->phy->interface == PHY_INTERFACE_MODE_GMII) {
  693. /* value taken from AVM Fritz!Box 7390 sources */
  694. newval = 0x010e5b71;
  695. } else {
  696. /* no known value for phy interface */
  697. pr_err("ar8316: unsupported mii mode: %d.\n",
  698. priv->phy->interface);
  699. return -EINVAL;
  700. }
  701. if (val == newval)
  702. goto out;
  703. priv->write(priv, AR8316_REG_POSTRIP, newval);
  704. if (priv->port4_phy &&
  705. priv->phy->interface == PHY_INTERFACE_MODE_RGMII) {
  706. /* work around for phy4 rgmii mode */
  707. ar8xxx_phy_dbg_write(priv, 4, 0x12, 0x480c);
  708. /* rx delay */
  709. ar8xxx_phy_dbg_write(priv, 4, 0x0, 0x824e);
  710. /* tx delay */
  711. ar8xxx_phy_dbg_write(priv, 4, 0x5, 0x3d47);
  712. msleep(1000);
  713. }
  714. /* Initialize the ports */
  715. bus = priv->mii_bus;
  716. for (i = 0; i < 5; i++) {
  717. /* initialize the port itself */
  718. mdiobus_write(bus, i, MII_ADVERTISE,
  719. ADVERTISE_ALL | ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM);
  720. mdiobus_write(bus, i, MII_CTRL1000, ADVERTISE_1000FULL);
  721. mdiobus_write(bus, i, MII_BMCR, BMCR_RESET | BMCR_ANENABLE);
  722. }
  723. msleep(1000);
  724. out:
  725. priv->initialized = true;
  726. return 0;
  727. }
  728. static void
  729. ar8316_init_globals(struct ar8xxx_priv *priv)
  730. {
  731. /* standard atheros magic */
  732. priv->write(priv, 0x38, 0xc000050e);
  733. /* enable cpu port to receive multicast and broadcast frames */
  734. priv->write(priv, AR8216_REG_FLOOD_MASK, 0x003f003f);
  735. /* enable jumbo frames */
  736. ar8xxx_rmw(priv, AR8216_REG_GLOBAL_CTRL,
  737. AR8316_GCTRL_MTU, 9018 + 8 + 2);
  738. /* Enable MIB counters */
  739. ar8xxx_rmw(priv, AR8216_REG_MIB_FUNC, AR8216_MIB_FUNC | AR8236_MIB_EN,
  740. (AR8216_MIB_FUNC_NO_OP << AR8216_MIB_FUNC_S) |
  741. AR8236_MIB_EN);
  742. }
  743. static const struct ar8xxx_chip ar8316_chip = {
  744. .caps = AR8XXX_CAP_GIGE | AR8XXX_CAP_MIB_COUNTERS,
  745. .hw_init = ar8316_hw_init,
  746. .init_globals = ar8316_init_globals,
  747. .init_port = ar8216_init_port,
  748. .setup_port = ar8216_setup_port,
  749. .read_port_status = ar8216_read_port_status,
  750. .atu_flush = ar8216_atu_flush,
  751. .vtu_flush = ar8216_vtu_flush,
  752. .vtu_load_vlan = ar8216_vtu_load_vlan,
  753. .num_mibs = ARRAY_SIZE(ar8236_mibs),
  754. .mib_decs = ar8236_mibs,
  755. };
  756. static u32
  757. ar8327_get_pad_cfg(struct ar8327_pad_cfg *cfg)
  758. {
  759. u32 t;
  760. if (!cfg)
  761. return 0;
  762. t = 0;
  763. switch (cfg->mode) {
  764. case AR8327_PAD_NC:
  765. break;
  766. case AR8327_PAD_MAC2MAC_MII:
  767. t = AR8327_PAD_MAC_MII_EN;
  768. if (cfg->rxclk_sel)
  769. t |= AR8327_PAD_MAC_MII_RXCLK_SEL;
  770. if (cfg->txclk_sel)
  771. t |= AR8327_PAD_MAC_MII_TXCLK_SEL;
  772. break;
  773. case AR8327_PAD_MAC2MAC_GMII:
  774. t = AR8327_PAD_MAC_GMII_EN;
  775. if (cfg->rxclk_sel)
  776. t |= AR8327_PAD_MAC_GMII_RXCLK_SEL;
  777. if (cfg->txclk_sel)
  778. t |= AR8327_PAD_MAC_GMII_TXCLK_SEL;
  779. break;
  780. case AR8327_PAD_MAC_SGMII:
  781. t = AR8327_PAD_SGMII_EN;
  782. /*
  783. * WAR for the QUalcomm Atheros AP136 board.
  784. * It seems that RGMII TX/RX delay settings needs to be
  785. * applied for SGMII mode as well, The ethernet is not
  786. * reliable without this.
  787. */
  788. t |= cfg->txclk_delay_sel << AR8327_PAD_RGMII_TXCLK_DELAY_SEL_S;
  789. t |= cfg->rxclk_delay_sel << AR8327_PAD_RGMII_RXCLK_DELAY_SEL_S;
  790. if (cfg->rxclk_delay_en)
  791. t |= AR8327_PAD_RGMII_RXCLK_DELAY_EN;
  792. if (cfg->txclk_delay_en)
  793. t |= AR8327_PAD_RGMII_TXCLK_DELAY_EN;
  794. if (cfg->sgmii_delay_en)
  795. t |= AR8327_PAD_SGMII_DELAY_EN;
  796. break;
  797. case AR8327_PAD_MAC2PHY_MII:
  798. t = AR8327_PAD_PHY_MII_EN;
  799. if (cfg->rxclk_sel)
  800. t |= AR8327_PAD_PHY_MII_RXCLK_SEL;
  801. if (cfg->txclk_sel)
  802. t |= AR8327_PAD_PHY_MII_TXCLK_SEL;
  803. break;
  804. case AR8327_PAD_MAC2PHY_GMII:
  805. t = AR8327_PAD_PHY_GMII_EN;
  806. if (cfg->pipe_rxclk_sel)
  807. t |= AR8327_PAD_PHY_GMII_PIPE_RXCLK_SEL;
  808. if (cfg->rxclk_sel)
  809. t |= AR8327_PAD_PHY_GMII_RXCLK_SEL;
  810. if (cfg->txclk_sel)
  811. t |= AR8327_PAD_PHY_GMII_TXCLK_SEL;
  812. break;
  813. case AR8327_PAD_MAC_RGMII:
  814. t = AR8327_PAD_RGMII_EN;
  815. t |= cfg->txclk_delay_sel << AR8327_PAD_RGMII_TXCLK_DELAY_SEL_S;
  816. t |= cfg->rxclk_delay_sel << AR8327_PAD_RGMII_RXCLK_DELAY_SEL_S;
  817. if (cfg->rxclk_delay_en)
  818. t |= AR8327_PAD_RGMII_RXCLK_DELAY_EN;
  819. if (cfg->txclk_delay_en)
  820. t |= AR8327_PAD_RGMII_TXCLK_DELAY_EN;
  821. break;
  822. case AR8327_PAD_PHY_GMII:
  823. t = AR8327_PAD_PHYX_GMII_EN;
  824. break;
  825. case AR8327_PAD_PHY_RGMII:
  826. t = AR8327_PAD_PHYX_RGMII_EN;
  827. break;
  828. case AR8327_PAD_PHY_MII:
  829. t = AR8327_PAD_PHYX_MII_EN;
  830. break;
  831. }
  832. return t;
  833. }
  834. static void
  835. ar8327_phy_fixup(struct ar8xxx_priv *priv, int phy)
  836. {
  837. switch (priv->chip_rev) {
  838. case 1:
  839. /* For 100M waveform */
  840. ar8xxx_phy_dbg_write(priv, phy, 0, 0x02ea);
  841. /* Turn on Gigabit clock */
  842. ar8xxx_phy_dbg_write(priv, phy, 0x3d, 0x68a0);
  843. break;
  844. case 2:
  845. ar8xxx_phy_mmd_write(priv, phy, 0x7, 0x3c);
  846. ar8xxx_phy_mmd_write(priv, phy, 0x4007, 0x0);
  847. /* fallthrough */
  848. case 4:
  849. ar8xxx_phy_mmd_write(priv, phy, 0x3, 0x800d);
  850. ar8xxx_phy_mmd_write(priv, phy, 0x4003, 0x803f);
  851. ar8xxx_phy_dbg_write(priv, phy, 0x3d, 0x6860);
  852. ar8xxx_phy_dbg_write(priv, phy, 0x5, 0x2c46);
  853. ar8xxx_phy_dbg_write(priv, phy, 0x3c, 0x6000);
  854. break;
  855. }
  856. }
  857. static u32
  858. ar8327_get_port_init_status(struct ar8327_port_cfg *cfg)
  859. {
  860. u32 t;
  861. if (!cfg->force_link)
  862. return AR8216_PORT_STATUS_LINK_AUTO;
  863. t = AR8216_PORT_STATUS_TXMAC | AR8216_PORT_STATUS_RXMAC;
  864. t |= cfg->duplex ? AR8216_PORT_STATUS_DUPLEX : 0;
  865. t |= cfg->rxpause ? AR8216_PORT_STATUS_RXFLOW : 0;
  866. t |= cfg->txpause ? AR8216_PORT_STATUS_TXFLOW : 0;
  867. switch (cfg->speed) {
  868. case AR8327_PORT_SPEED_10:
  869. t |= AR8216_PORT_SPEED_10M;
  870. break;
  871. case AR8327_PORT_SPEED_100:
  872. t |= AR8216_PORT_SPEED_100M;
  873. break;
  874. case AR8327_PORT_SPEED_1000:
  875. t |= AR8216_PORT_SPEED_1000M;
  876. break;
  877. }
  878. return t;
  879. }
  880. static int
  881. ar8327_hw_config_pdata(struct ar8xxx_priv *priv,
  882. struct ar8327_platform_data *pdata)
  883. {
  884. struct ar8327_led_cfg *led_cfg;
  885. struct ar8327_data *data;
  886. u32 pos, new_pos;
  887. u32 t;
  888. if (!pdata)
  889. return -EINVAL;
  890. priv->get_port_link = pdata->get_port_link;
  891. data = &priv->chip_data.ar8327;
  892. data->port0_status = ar8327_get_port_init_status(&pdata->port0_cfg);
  893. data->port6_status = ar8327_get_port_init_status(&pdata->port6_cfg);
  894. t = ar8327_get_pad_cfg(pdata->pad0_cfg);
  895. priv->write(priv, AR8327_REG_PAD0_MODE, t);
  896. t = ar8327_get_pad_cfg(pdata->pad5_cfg);
  897. priv->write(priv, AR8327_REG_PAD5_MODE, t);
  898. t = ar8327_get_pad_cfg(pdata->pad6_cfg);
  899. priv->write(priv, AR8327_REG_PAD6_MODE, t);
  900. pos = priv->read(priv, AR8327_REG_POWER_ON_STRIP);
  901. new_pos = pos;
  902. led_cfg = pdata->led_cfg;
  903. if (led_cfg) {
  904. if (led_cfg->open_drain)
  905. new_pos |= AR8327_POWER_ON_STRIP_LED_OPEN_EN;
  906. else
  907. new_pos &= ~AR8327_POWER_ON_STRIP_LED_OPEN_EN;
  908. priv->write(priv, AR8327_REG_LED_CTRL0, led_cfg->led_ctrl0);
  909. priv->write(priv, AR8327_REG_LED_CTRL1, led_cfg->led_ctrl1);
  910. priv->write(priv, AR8327_REG_LED_CTRL2, led_cfg->led_ctrl2);
  911. priv->write(priv, AR8327_REG_LED_CTRL3, led_cfg->led_ctrl3);
  912. if (new_pos != pos)
  913. new_pos |= AR8327_POWER_ON_STRIP_POWER_ON_SEL;
  914. }
  915. if (pdata->sgmii_cfg) {
  916. t = pdata->sgmii_cfg->sgmii_ctrl;
  917. if (priv->chip_rev == 1)
  918. t |= AR8327_SGMII_CTRL_EN_PLL |
  919. AR8327_SGMII_CTRL_EN_RX |
  920. AR8327_SGMII_CTRL_EN_TX;
  921. else
  922. t &= ~(AR8327_SGMII_CTRL_EN_PLL |
  923. AR8327_SGMII_CTRL_EN_RX |
  924. AR8327_SGMII_CTRL_EN_TX);
  925. priv->write(priv, AR8327_REG_SGMII_CTRL, t);
  926. if (pdata->sgmii_cfg->serdes_aen)
  927. new_pos &= ~AR8327_POWER_ON_STRIP_SERDES_AEN;
  928. else
  929. new_pos |= AR8327_POWER_ON_STRIP_SERDES_AEN;
  930. }
  931. priv->write(priv, AR8327_REG_POWER_ON_STRIP, new_pos);
  932. return 0;
  933. }
  934. #ifdef CONFIG_OF
  935. static int
  936. ar8327_hw_config_of(struct ar8xxx_priv *priv, struct device_node *np)
  937. {
  938. const __be32 *paddr;
  939. int len;
  940. int i;
  941. paddr = of_get_property(np, "qca,ar8327-initvals", &len);
  942. if (!paddr || len < (2 * sizeof(*paddr)))
  943. return -EINVAL;
  944. len /= sizeof(*paddr);
  945. for (i = 0; i < len - 1; i += 2) {
  946. u32 reg;
  947. u32 val;
  948. reg = be32_to_cpup(paddr + i);
  949. val = be32_to_cpup(paddr + i + 1);
  950. switch (reg) {
  951. case AR8327_REG_PORT_STATUS(0):
  952. priv->chip_data.ar8327.port0_status = val;
  953. break;
  954. case AR8327_REG_PORT_STATUS(6):
  955. priv->chip_data.ar8327.port6_status = val;
  956. break;
  957. default:
  958. priv->write(priv, reg, val);
  959. break;
  960. }
  961. }
  962. return 0;
  963. }
  964. #else
  965. static inline int
  966. ar8327_hw_config_of(struct ar8xxx_priv *priv, struct device_node *np)
  967. {
  968. return -EINVAL;
  969. }
  970. #endif
  971. static int
  972. ar8327_hw_init(struct ar8xxx_priv *priv)
  973. {
  974. struct mii_bus *bus;
  975. int ret;
  976. int i;
  977. if (priv->phy->dev.of_node)
  978. ret = ar8327_hw_config_of(priv, priv->phy->dev.of_node);
  979. else
  980. ret = ar8327_hw_config_pdata(priv,
  981. priv->phy->dev.platform_data);
  982. if (ret)
  983. return ret;
  984. bus = priv->mii_bus;
  985. for (i = 0; i < AR8327_NUM_PHYS; i++) {
  986. ar8327_phy_fixup(priv, i);
  987. /* start aneg on the PHY */
  988. mdiobus_write(bus, i, MII_ADVERTISE, ADVERTISE_ALL |
  989. ADVERTISE_PAUSE_CAP |
  990. ADVERTISE_PAUSE_ASYM);
  991. mdiobus_write(bus, i, MII_CTRL1000, ADVERTISE_1000FULL);
  992. mdiobus_write(bus, i, MII_BMCR, BMCR_RESET | BMCR_ANENABLE);
  993. }
  994. msleep(1000);
  995. return 0;
  996. }
  997. static void
  998. ar8327_init_globals(struct ar8xxx_priv *priv)
  999. {
  1000. u32 t;
  1001. /* enable CPU port and disable mirror port */
  1002. t = AR8327_FWD_CTRL0_CPU_PORT_EN |
  1003. AR8327_FWD_CTRL0_MIRROR_PORT;
  1004. priv->write(priv, AR8327_REG_FWD_CTRL0, t);
  1005. /* forward multicast and broadcast frames to CPU */
  1006. t = (AR8327_PORTS_ALL << AR8327_FWD_CTRL1_UC_FLOOD_S) |
  1007. (AR8327_PORTS_ALL << AR8327_FWD_CTRL1_MC_FLOOD_S) |
  1008. (AR8327_PORTS_ALL << AR8327_FWD_CTRL1_BC_FLOOD_S);
  1009. priv->write(priv, AR8327_REG_FWD_CTRL1, t);
  1010. /* setup MTU */
  1011. ar8xxx_rmw(priv, AR8327_REG_MAX_FRAME_SIZE,
  1012. AR8327_MAX_FRAME_SIZE_MTU, 1518 + 8 + 2);
  1013. /* Enable MIB counters */
  1014. ar8xxx_reg_set(priv, AR8327_REG_MODULE_EN,
  1015. AR8327_MODULE_EN_MIB);
  1016. }
  1017. static void
  1018. ar8327_init_port(struct ar8xxx_priv *priv, int port)
  1019. {
  1020. u32 t;
  1021. if (port == AR8216_PORT_CPU)
  1022. t = priv->chip_data.ar8327.port0_status;
  1023. else if (port == 6)
  1024. t = priv->chip_data.ar8327.port6_status;
  1025. else
  1026. t = AR8216_PORT_STATUS_LINK_AUTO;
  1027. priv->write(priv, AR8327_REG_PORT_STATUS(port), t);
  1028. priv->write(priv, AR8327_REG_PORT_HEADER(port), 0);
  1029. t = 1 << AR8327_PORT_VLAN0_DEF_SVID_S;
  1030. t |= 1 << AR8327_PORT_VLAN0_DEF_CVID_S;
  1031. priv->write(priv, AR8327_REG_PORT_VLAN0(port), t);
  1032. t = AR8327_PORT_VLAN1_OUT_MODE_UNTOUCH << AR8327_PORT_VLAN1_OUT_MODE_S;
  1033. priv->write(priv, AR8327_REG_PORT_VLAN1(port), t);
  1034. t = AR8327_PORT_LOOKUP_LEARN;
  1035. t |= AR8216_PORT_STATE_FORWARD << AR8327_PORT_LOOKUP_STATE_S;
  1036. priv->write(priv, AR8327_REG_PORT_LOOKUP(port), t);
  1037. }
  1038. static u32
  1039. ar8327_read_port_status(struct ar8xxx_priv *priv, int port)
  1040. {
  1041. return priv->read(priv, AR8327_REG_PORT_STATUS(port));
  1042. }
  1043. static int
  1044. ar8327_atu_flush(struct ar8xxx_priv *priv)
  1045. {
  1046. int ret;
  1047. ret = ar8216_wait_bit(priv, AR8327_REG_ATU_FUNC,
  1048. AR8327_ATU_FUNC_BUSY, 0);
  1049. if (!ret)
  1050. priv->write(priv, AR8327_REG_ATU_FUNC,
  1051. AR8327_ATU_FUNC_OP_FLUSH);
  1052. return ret;
  1053. }
  1054. static void
  1055. ar8327_vtu_op(struct ar8xxx_priv *priv, u32 op, u32 val)
  1056. {
  1057. if (ar8216_wait_bit(priv, AR8327_REG_VTU_FUNC1,
  1058. AR8327_VTU_FUNC1_BUSY, 0))
  1059. return;
  1060. if ((op & AR8327_VTU_FUNC1_OP) == AR8327_VTU_FUNC1_OP_LOAD)
  1061. priv->write(priv, AR8327_REG_VTU_FUNC0, val);
  1062. op |= AR8327_VTU_FUNC1_BUSY;
  1063. priv->write(priv, AR8327_REG_VTU_FUNC1, op);
  1064. }
  1065. static void
  1066. ar8327_vtu_flush(struct ar8xxx_priv *priv)
  1067. {
  1068. ar8327_vtu_op(priv, AR8327_VTU_FUNC1_OP_FLUSH, 0);
  1069. }
  1070. static void
  1071. ar8327_vtu_load_vlan(struct ar8xxx_priv *priv, u32 vid, u32 port_mask)
  1072. {
  1073. u32 op;
  1074. u32 val;
  1075. int i;
  1076. op = AR8327_VTU_FUNC1_OP_LOAD | (vid << AR8327_VTU_FUNC1_VID_S);
  1077. val = AR8327_VTU_FUNC0_VALID | AR8327_VTU_FUNC0_IVL;
  1078. for (i = 0; i < AR8327_NUM_PORTS; i++) {
  1079. u32 mode;
  1080. if ((port_mask & BIT(i)) == 0)
  1081. mode = AR8327_VTU_FUNC0_EG_MODE_NOT;
  1082. else if (priv->vlan == 0)
  1083. mode = AR8327_VTU_FUNC0_EG_MODE_KEEP;
  1084. else if (priv->vlan_tagged & BIT(i))
  1085. mode = AR8327_VTU_FUNC0_EG_MODE_TAG;
  1086. else
  1087. mode = AR8327_VTU_FUNC0_EG_MODE_UNTAG;
  1088. val |= mode << AR8327_VTU_FUNC0_EG_MODE_S(i);
  1089. }
  1090. ar8327_vtu_op(priv, op, val);
  1091. }
  1092. static void
  1093. ar8327_setup_port(struct ar8xxx_priv *priv, int port, u32 egress, u32 ingress,
  1094. u32 members, u32 pvid)
  1095. {
  1096. u32 t;
  1097. u32 mode;
  1098. t = pvid << AR8327_PORT_VLAN0_DEF_SVID_S;
  1099. t |= pvid << AR8327_PORT_VLAN0_DEF_CVID_S;
  1100. priv->write(priv, AR8327_REG_PORT_VLAN0(port), t);
  1101. mode = AR8327_PORT_VLAN1_OUT_MODE_UNMOD;
  1102. switch (egress) {
  1103. case AR8216_OUT_KEEP:
  1104. mode = AR8327_PORT_VLAN1_OUT_MODE_UNTOUCH;
  1105. break;
  1106. case AR8216_OUT_STRIP_VLAN:
  1107. mode = AR8327_PORT_VLAN1_OUT_MODE_UNTAG;
  1108. break;
  1109. case AR8216_OUT_ADD_VLAN:
  1110. mode = AR8327_PORT_VLAN1_OUT_MODE_TAG;
  1111. break;
  1112. }
  1113. t = AR8327_PORT_VLAN1_PORT_VLAN_PROP;
  1114. t |= mode << AR8327_PORT_VLAN1_OUT_MODE_S;
  1115. priv->write(priv, AR8327_REG_PORT_VLAN1(port), t);
  1116. t = members;
  1117. t |= AR8327_PORT_LOOKUP_LEARN;
  1118. t |= ingress << AR8327_PORT_LOOKUP_IN_MODE_S;
  1119. t |= AR8216_PORT_STATE_FORWARD << AR8327_PORT_LOOKUP_STATE_S;
  1120. priv->write(priv, AR8327_REG_PORT_LOOKUP(port), t);
  1121. }
  1122. static const struct ar8xxx_chip ar8327_chip = {
  1123. .caps = AR8XXX_CAP_GIGE | AR8XXX_CAP_MIB_COUNTERS,
  1124. .hw_init = ar8327_hw_init,
  1125. .init_globals = ar8327_init_globals,
  1126. .init_port = ar8327_init_port,
  1127. .setup_port = ar8327_setup_port,
  1128. .read_port_status = ar8327_read_port_status,
  1129. .atu_flush = ar8327_atu_flush,
  1130. .vtu_flush = ar8327_vtu_flush,
  1131. .vtu_load_vlan = ar8327_vtu_load_vlan,
  1132. .num_mibs = ARRAY_SIZE(ar8236_mibs),
  1133. .mib_decs = ar8236_mibs,
  1134. };
  1135. static int
  1136. ar8xxx_sw_set_vlan(struct switch_dev *dev, const struct switch_attr *attr,
  1137. struct switch_val *val)
  1138. {
  1139. struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
  1140. priv->vlan = !!val->value.i;
  1141. return 0;
  1142. }
  1143. static int
  1144. ar8xxx_sw_get_vlan(struct switch_dev *dev, const struct switch_attr *attr,
  1145. struct switch_val *val)
  1146. {
  1147. struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
  1148. val->value.i = priv->vlan;
  1149. return 0;
  1150. }
  1151. static int
  1152. ar8xxx_sw_set_pvid(struct switch_dev *dev, int port, int vlan)
  1153. {
  1154. struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
  1155. /* make sure no invalid PVIDs get set */
  1156. if (vlan >= dev->vlans)
  1157. return -EINVAL;
  1158. priv->pvid[port] = vlan;
  1159. return 0;
  1160. }
  1161. static int
  1162. ar8xxx_sw_get_pvid(struct switch_dev *dev, int port, int *vlan)
  1163. {
  1164. struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
  1165. *vlan = priv->pvid[port];
  1166. return 0;
  1167. }
  1168. static int
  1169. ar8xxx_sw_set_vid(struct switch_dev *dev, const struct switch_attr *attr,
  1170. struct switch_val *val)
  1171. {
  1172. struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
  1173. priv->vlan_id[val->port_vlan] = val->value.i;
  1174. return 0;
  1175. }
  1176. static int
  1177. ar8xxx_sw_get_vid(struct switch_dev *dev, const struct switch_attr *attr,
  1178. struct switch_val *val)
  1179. {
  1180. struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
  1181. val->value.i = priv->vlan_id[val->port_vlan];
  1182. return 0;
  1183. }
  1184. static int
  1185. ar8xxx_sw_get_port_link(struct switch_dev *dev, int port,
  1186. struct switch_port_link *link)
  1187. {
  1188. struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
  1189. ar8216_read_port_link(priv, port, link);
  1190. return 0;
  1191. }
  1192. static int
  1193. ar8xxx_sw_get_ports(struct switch_dev *dev, struct switch_val *val)
  1194. {
  1195. struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
  1196. u8 ports = priv->vlan_table[val->port_vlan];
  1197. int i;
  1198. val->len = 0;
  1199. for (i = 0; i < dev->ports; i++) {
  1200. struct switch_port *p;
  1201. if (!(ports & (1 << i)))
  1202. continue;
  1203. p = &val->value.ports[val->len++];
  1204. p->id = i;
  1205. if (priv->vlan_tagged & (1 << i))
  1206. p->flags = (1 << SWITCH_PORT_FLAG_TAGGED);
  1207. else
  1208. p->flags = 0;
  1209. }
  1210. return 0;
  1211. }
  1212. static int
  1213. ar8xxx_sw_set_ports(struct switch_dev *dev, struct switch_val *val)
  1214. {
  1215. struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
  1216. u8 *vt = &priv->vlan_table[val->port_vlan];
  1217. int i, j;
  1218. *vt = 0;
  1219. for (i = 0; i < val->len; i++) {
  1220. struct switch_port *p = &val->value.ports[i];
  1221. if (p->flags & (1 << SWITCH_PORT_FLAG_TAGGED)) {
  1222. priv->vlan_tagged |= (1 << p->id);
  1223. } else {
  1224. priv->vlan_tagged &= ~(1 << p->id);
  1225. priv->pvid[p->id] = val->port_vlan;
  1226. /* make sure that an untagged port does not
  1227. * appear in other vlans */
  1228. for (j = 0; j < AR8X16_MAX_VLANS; j++) {
  1229. if (j == val->port_vlan)
  1230. continue;
  1231. priv->vlan_table[j] &= ~(1 << p->id);
  1232. }
  1233. }
  1234. *vt |= 1 << p->id;
  1235. }
  1236. return 0;
  1237. }
  1238. static void
  1239. ar8327_set_mirror_regs(struct ar8xxx_priv *priv)
  1240. {
  1241. int port;
  1242. /* reset all mirror registers */
  1243. ar8xxx_rmw(priv, AR8327_REG_FWD_CTRL0,
  1244. AR8327_FWD_CTRL0_MIRROR_PORT,
  1245. (0xF << AR8327_FWD_CTRL0_MIRROR_PORT_S));
  1246. for (port = 0; port < AR8327_NUM_PORTS; port++) {
  1247. ar8xxx_rmw(priv, AR8327_REG_PORT_LOOKUP(port),
  1248. AR8327_PORT_LOOKUP_ING_MIRROR_EN,
  1249. 0);
  1250. ar8xxx_rmw(priv, AR8327_REG_PORT_HOL_CTRL1(port),
  1251. AR8327_PORT_HOL_CTRL1_EG_MIRROR_EN,
  1252. 0);
  1253. }
  1254. /* now enable mirroring if necessary */
  1255. if (priv->source_port >= AR8327_NUM_PORTS ||
  1256. priv->monitor_port >= AR8327_NUM_PORTS ||
  1257. priv->source_port == priv->monitor_port) {
  1258. return;
  1259. }
  1260. ar8xxx_rmw(priv, AR8327_REG_FWD_CTRL0,
  1261. AR8327_FWD_CTRL0_MIRROR_PORT,
  1262. (priv->monitor_port << AR8327_FWD_CTRL0_MIRROR_PORT_S));
  1263. if (priv->mirror_rx)
  1264. ar8xxx_rmw(priv, AR8327_REG_PORT_LOOKUP(priv->source_port),
  1265. AR8327_PORT_LOOKUP_ING_MIRROR_EN,
  1266. AR8327_PORT_LOOKUP_ING_MIRROR_EN);
  1267. if (priv->mirror_tx)
  1268. ar8xxx_rmw(priv, AR8327_REG_PORT_HOL_CTRL1(priv->source_port),
  1269. AR8327_PORT_HOL_CTRL1_EG_MIRROR_EN,
  1270. AR8327_PORT_HOL_CTRL1_EG_MIRROR_EN);
  1271. }
  1272. static void
  1273. ar8216_set_mirror_regs(struct ar8xxx_priv *priv)
  1274. {
  1275. int port;
  1276. /* reset all mirror registers */
  1277. ar8xxx_rmw(priv, AR8216_REG_GLOBAL_CPUPORT,
  1278. AR8216_GLOBAL_CPUPORT_MIRROR_PORT,
  1279. (0xF << AR8216_GLOBAL_CPUPORT_MIRROR_PORT_S));
  1280. for (port = 0; port < AR8216_NUM_PORTS; port++) {
  1281. ar8xxx_rmw(priv, AR8216_REG_PORT_CTRL(port),
  1282. AR8216_PORT_CTRL_MIRROR_RX,
  1283. 0);
  1284. ar8xxx_rmw(priv, AR8216_REG_PORT_CTRL(port),
  1285. AR8216_PORT_CTRL_MIRROR_TX,
  1286. 0);
  1287. }
  1288. /* now enable mirroring if necessary */
  1289. if (priv->source_port >= AR8216_NUM_PORTS ||
  1290. priv->monitor_port >= AR8216_NUM_PORTS ||
  1291. priv->source_port == priv->monitor_port) {
  1292. return;
  1293. }
  1294. ar8xxx_rmw(priv, AR8216_REG_GLOBAL_CPUPORT,
  1295. AR8216_GLOBAL_CPUPORT_MIRROR_PORT,
  1296. (priv->monitor_port << AR8216_GLOBAL_CPUPORT_MIRROR_PORT_S));
  1297. if (priv->mirror_rx)
  1298. ar8xxx_rmw(priv, AR8216_REG_PORT_CTRL(priv->source_port),
  1299. AR8216_PORT_CTRL_MIRROR_RX,
  1300. AR8216_PORT_CTRL_MIRROR_RX);
  1301. if (priv->mirror_tx)
  1302. ar8xxx_rmw(priv, AR8216_REG_PORT_CTRL(priv->source_port),
  1303. AR8216_PORT_CTRL_MIRROR_TX,
  1304. AR8216_PORT_CTRL_MIRROR_TX);
  1305. }
  1306. static void
  1307. ar8xxx_set_mirror_regs(struct ar8xxx_priv *priv)
  1308. {
  1309. if (chip_is_ar8327(priv)) {
  1310. ar8327_set_mirror_regs(priv);
  1311. } else {
  1312. ar8216_set_mirror_regs(priv);
  1313. }
  1314. }
  1315. static int
  1316. ar8xxx_sw_hw_apply(struct switch_dev *dev)
  1317. {
  1318. struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
  1319. u8 portmask[AR8X16_MAX_PORTS];
  1320. int i, j;
  1321. mutex_lock(&priv->reg_mutex);
  1322. /* flush all vlan translation unit entries */
  1323. priv->chip->vtu_flush(priv);
  1324. memset(portmask, 0, sizeof(portmask));
  1325. if (!priv->init) {
  1326. /* calculate the port destination masks and load vlans
  1327. * into the vlan translation unit */
  1328. for (j = 0; j < AR8X16_MAX_VLANS; j++) {
  1329. u8 vp = priv->vlan_table[j];
  1330. if (!vp)
  1331. continue;
  1332. for (i = 0; i < dev->ports; i++) {
  1333. u8 mask = (1 << i);
  1334. if (vp & mask)
  1335. portmask[i] |= vp & ~mask;
  1336. }
  1337. priv->chip->vtu_load_vlan(priv, priv->vlan_id[j],
  1338. priv->vlan_table[j]);
  1339. }
  1340. } else {
  1341. /* vlan disabled:
  1342. * isolate all ports, but connect them to the cpu port */
  1343. for (i = 0; i < dev->ports; i++) {
  1344. if (i == AR8216_PORT_CPU)
  1345. continue;
  1346. portmask[i] = 1 << AR8216_PORT_CPU;
  1347. portmask[AR8216_PORT_CPU] |= (1 << i);
  1348. }
  1349. }
  1350. /* update the port destination mask registers and tag settings */
  1351. for (i = 0; i < dev->ports; i++) {
  1352. int egress, ingress;
  1353. int pvid;
  1354. if (priv->vlan) {
  1355. pvid = priv->vlan_id[priv->pvid[i]];
  1356. if (priv->vlan_tagged & (1 << i))
  1357. egress = AR8216_OUT_ADD_VLAN;
  1358. else
  1359. egress = AR8216_OUT_STRIP_VLAN;
  1360. ingress = AR8216_IN_SECURE;
  1361. } else {
  1362. pvid = i;
  1363. egress = AR8216_OUT_KEEP;
  1364. ingress = AR8216_IN_PORT_ONLY;
  1365. }
  1366. priv->chip->setup_port(priv, i, egress, ingress, portmask[i],
  1367. pvid);
  1368. }
  1369. ar8xxx_set_mirror_regs(priv);
  1370. mutex_unlock(&priv->reg_mutex);
  1371. return 0;
  1372. }
  1373. static int
  1374. ar8xxx_sw_reset_switch(struct switch_dev *dev)
  1375. {
  1376. struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
  1377. int i;
  1378. mutex_lock(&priv->reg_mutex);
  1379. memset(&priv->vlan, 0, sizeof(struct ar8xxx_priv) -
  1380. offsetof(struct ar8xxx_priv, vlan));
  1381. for (i = 0; i < AR8X16_MAX_VLANS; i++)
  1382. priv->vlan_id[i] = i;
  1383. /* Configure all ports */
  1384. for (i = 0; i < dev->ports; i++)
  1385. priv->chip->init_port(priv, i);
  1386. priv->mirror_rx = false;
  1387. priv->mirror_tx = false;
  1388. priv->source_port = 0;
  1389. priv->monitor_port = 0;
  1390. priv->chip->init_globals(priv);
  1391. mutex_unlock(&priv->reg_mutex);
  1392. return ar8xxx_sw_hw_apply(dev);
  1393. }
  1394. static int
  1395. ar8xxx_sw_set_reset_mibs(struct switch_dev *dev,
  1396. const struct switch_attr *attr,
  1397. struct switch_val *val)
  1398. {
  1399. struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
  1400. unsigned int len;
  1401. int ret;
  1402. if (!ar8xxx_has_mib_counters(priv))
  1403. return -EOPNOTSUPP;
  1404. mutex_lock(&priv->mib_lock);
  1405. len = priv->dev.ports * priv->chip->num_mibs *
  1406. sizeof(*priv->mib_stats);
  1407. memset(priv->mib_stats, '\0', len);
  1408. ret = ar8xxx_mib_flush(priv);
  1409. if (ret)
  1410. goto unlock;
  1411. ret = 0;
  1412. unlock:
  1413. mutex_unlock(&priv->mib_lock);
  1414. return ret;
  1415. }
  1416. static int
  1417. ar8xxx_sw_set_mirror_rx_enable(struct switch_dev *dev,
  1418. const struct switch_attr *attr,
  1419. struct switch_val *val)
  1420. {
  1421. struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
  1422. mutex_lock(&priv->reg_mutex);
  1423. priv->mirror_rx = !!val->value.i;
  1424. ar8xxx_set_mirror_regs(priv);
  1425. mutex_unlock(&priv->reg_mutex);
  1426. return 0;
  1427. }
  1428. static int
  1429. ar8xxx_sw_get_mirror_rx_enable(struct switch_dev *dev,
  1430. const struct switch_attr *attr,
  1431. struct switch_val *val)
  1432. {
  1433. struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
  1434. val->value.i = priv->mirror_rx;
  1435. return 0;
  1436. }
  1437. static int
  1438. ar8xxx_sw_set_mirror_tx_enable(struct switch_dev *dev,
  1439. const struct switch_attr *attr,
  1440. struct switch_val *val)
  1441. {
  1442. struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
  1443. mutex_lock(&priv->reg_mutex);
  1444. priv->mirror_tx = !!val->value.i;
  1445. ar8xxx_set_mirror_regs(priv);
  1446. mutex_unlock(&priv->reg_mutex);
  1447. return 0;
  1448. }
  1449. static int
  1450. ar8xxx_sw_get_mirror_tx_enable(struct switch_dev *dev,
  1451. const struct switch_attr *attr,
  1452. struct switch_val *val)
  1453. {
  1454. struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
  1455. val->value.i = priv->mirror_tx;
  1456. return 0;
  1457. }
  1458. static int
  1459. ar8xxx_sw_set_mirror_monitor_port(struct switch_dev *dev,
  1460. const struct switch_attr *attr,
  1461. struct switch_val *val)
  1462. {
  1463. struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
  1464. mutex_lock(&priv->reg_mutex);
  1465. priv->monitor_port = val->value.i;
  1466. ar8xxx_set_mirror_regs(priv);
  1467. mutex_unlock(&priv->reg_mutex);
  1468. return 0;
  1469. }
  1470. static int
  1471. ar8xxx_sw_get_mirror_monitor_port(struct switch_dev *dev,
  1472. const struct switch_attr *attr,
  1473. struct switch_val *val)
  1474. {
  1475. struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
  1476. val->value.i = priv->monitor_port;
  1477. return 0;
  1478. }
  1479. static int
  1480. ar8xxx_sw_set_mirror_source_port(struct switch_dev *dev,
  1481. const struct switch_attr *attr,
  1482. struct switch_val *val)
  1483. {
  1484. struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
  1485. mutex_lock(&priv->reg_mutex);
  1486. priv->source_port = val->value.i;
  1487. ar8xxx_set_mirror_regs(priv);
  1488. mutex_unlock(&priv->reg_mutex);
  1489. return 0;
  1490. }
  1491. static int
  1492. ar8xxx_sw_get_mirror_source_port(struct switch_dev *dev,
  1493. const struct switch_attr *attr,
  1494. struct switch_val *val)
  1495. {
  1496. struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
  1497. val->value.i = priv->source_port;
  1498. return 0;
  1499. }
  1500. static int
  1501. ar8xxx_sw_set_port_reset_mib(struct switch_dev *dev,
  1502. const struct switch_attr *attr,
  1503. struct switch_val *val)
  1504. {
  1505. struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
  1506. int port;
  1507. int ret;
  1508. if (!ar8xxx_has_mib_counters(priv))
  1509. return -EOPNOTSUPP;
  1510. port = val->port_vlan;
  1511. if (port >= dev->ports)
  1512. return -EINVAL;
  1513. mutex_lock(&priv->mib_lock);
  1514. ret = ar8xxx_mib_capture(priv);
  1515. if (ret)
  1516. goto unlock;
  1517. ar8xxx_mib_fetch_port_stat(priv, port, true);
  1518. ret = 0;
  1519. unlock:
  1520. mutex_unlock(&priv->mib_lock);
  1521. return ret;
  1522. }
  1523. static int
  1524. ar8xxx_sw_get_port_mib(struct switch_dev *dev,
  1525. const struct switch_attr *attr,
  1526. struct switch_val *val)
  1527. {
  1528. struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
  1529. const struct ar8xxx_chip *chip = priv->chip;
  1530. u64 *mib_stats;
  1531. int port;
  1532. int ret;
  1533. char *buf = priv->buf;
  1534. int i, len = 0;
  1535. if (!ar8xxx_has_mib_counters(priv))
  1536. return -EOPNOTSUPP;
  1537. port = val->port_vlan;
  1538. if (port >= dev->ports)
  1539. return -EINVAL;
  1540. mutex_lock(&priv->mib_lock);
  1541. ret = ar8xxx_mib_capture(priv);
  1542. if (ret)
  1543. goto unlock;
  1544. ar8xxx_mib_fetch_port_stat(priv, port, false);
  1545. len += snprintf(buf + len, sizeof(priv->buf) - len,
  1546. "Port %d MIB counters\n",
  1547. port);
  1548. mib_stats = &priv->mib_stats[port * chip->num_mibs];
  1549. for (i = 0; i < chip->num_mibs; i++)
  1550. len += snprintf(buf + len, sizeof(priv->buf) - len,
  1551. "%-12s: %llu\n",
  1552. chip->mib_decs[i].name,
  1553. mib_stats[i]);
  1554. val->value.s = buf;
  1555. val->len = len;
  1556. ret = 0;
  1557. unlock:
  1558. mutex_unlock(&priv->mib_lock);
  1559. return ret;
  1560. }
  1561. static struct switch_attr ar8xxx_sw_attr_globals[] = {
  1562. {
  1563. .type = SWITCH_TYPE_INT,
  1564. .name = "enable_vlan",
  1565. .description = "Enable VLAN mode",
  1566. .set = ar8xxx_sw_set_vlan,
  1567. .get = ar8xxx_sw_get_vlan,
  1568. .max = 1
  1569. },
  1570. {
  1571. .type = SWITCH_TYPE_NOVAL,
  1572. .name = "reset_mibs",
  1573. .description = "Reset all MIB counters",
  1574. .set = ar8xxx_sw_set_reset_mibs,
  1575. },
  1576. {
  1577. .type = SWITCH_TYPE_INT,
  1578. .name = "enable_mirror_rx",
  1579. .description = "Enable mirroring of RX packets",
  1580. .set = ar8xxx_sw_set_mirror_rx_enable,
  1581. .get = ar8xxx_sw_get_mirror_rx_enable,
  1582. .max = 1
  1583. },
  1584. {
  1585. .type = SWITCH_TYPE_INT,
  1586. .name = "enable_mirror_tx",
  1587. .description = "Enable mirroring of TX packets",
  1588. .set = ar8xxx_sw_set_mirror_tx_enable,
  1589. .get = ar8xxx_sw_get_mirror_tx_enable,
  1590. .max = 1
  1591. },
  1592. {
  1593. .type = SWITCH_TYPE_INT,
  1594. .name = "mirror_monitor_port",
  1595. .description = "Mirror monitor port",
  1596. .set = ar8xxx_sw_set_mirror_monitor_port,
  1597. .get = ar8xxx_sw_get_mirror_monitor_port,
  1598. .max = AR8216_NUM_PORTS - 1
  1599. },
  1600. {
  1601. .type = SWITCH_TYPE_INT,
  1602. .name = "mirror_source_port",
  1603. .description = "Mirror source port",
  1604. .set = ar8xxx_sw_set_mirror_source_port,
  1605. .get = ar8xxx_sw_get_mirror_source_port,
  1606. .max = AR8216_NUM_PORTS - 1
  1607. },
  1608. };
  1609. static struct switch_attr ar8327_sw_attr_globals[] = {
  1610. {
  1611. .type = SWITCH_TYPE_INT,
  1612. .name = "enable_vlan",
  1613. .description = "Enable VLAN mode",
  1614. .set = ar8xxx_sw_set_vlan,
  1615. .get = ar8xxx_sw_get_vlan,
  1616. .max = 1
  1617. },
  1618. {
  1619. .type = SWITCH_TYPE_NOVAL,
  1620. .name = "reset_mibs",
  1621. .description = "Reset all MIB counters",
  1622. .set = ar8xxx_sw_set_reset_mibs,
  1623. },
  1624. {
  1625. .type = SWITCH_TYPE_INT,
  1626. .name = "enable_mirror_rx",
  1627. .description = "Enable mirroring of RX packets",
  1628. .set = ar8xxx_sw_set_mirror_rx_enable,
  1629. .get = ar8xxx_sw_get_mirror_rx_enable,
  1630. .max = 1
  1631. },
  1632. {
  1633. .type = SWITCH_TYPE_INT,
  1634. .name = "enable_mirror_tx",
  1635. .description = "Enable mirroring of TX packets",
  1636. .set = ar8xxx_sw_set_mirror_tx_enable,
  1637. .get = ar8xxx_sw_get_mirror_tx_enable,
  1638. .max = 1
  1639. },
  1640. {
  1641. .type = SWITCH_TYPE_INT,
  1642. .name = "mirror_monitor_port",
  1643. .description = "Mirror monitor port",
  1644. .set = ar8xxx_sw_set_mirror_monitor_port,
  1645. .get = ar8xxx_sw_get_mirror_monitor_port,
  1646. .max = AR8327_NUM_PORTS - 1
  1647. },
  1648. {
  1649. .type = SWITCH_TYPE_INT,
  1650. .name = "mirror_source_port",
  1651. .description = "Mirror source port",
  1652. .set = ar8xxx_sw_set_mirror_source_port,
  1653. .get = ar8xxx_sw_get_mirror_source_port,
  1654. .max = AR8327_NUM_PORTS - 1
  1655. },
  1656. };
  1657. static struct switch_attr ar8xxx_sw_attr_port[] = {
  1658. {
  1659. .type = SWITCH_TYPE_NOVAL,
  1660. .name = "reset_mib",
  1661. .description = "Reset single port MIB counters",
  1662. .set = ar8xxx_sw_set_port_reset_mib,
  1663. },
  1664. {
  1665. .type = SWITCH_TYPE_STRING,
  1666. .name = "mib",
  1667. .description = "Get port's MIB counters",
  1668. .set = NULL,
  1669. .get = ar8xxx_sw_get_port_mib,
  1670. },
  1671. };
  1672. static struct switch_attr ar8xxx_sw_attr_vlan[] = {
  1673. {
  1674. .type = SWITCH_TYPE_INT,
  1675. .name = "vid",
  1676. .description = "VLAN ID (0-4094)",
  1677. .set = ar8xxx_sw_set_vid,
  1678. .get = ar8xxx_sw_get_vid,
  1679. .max = 4094,
  1680. },
  1681. };
  1682. static const struct switch_dev_ops ar8xxx_sw_ops = {
  1683. .attr_global = {
  1684. .attr = ar8xxx_sw_attr_globals,
  1685. .n_attr = ARRAY_SIZE(ar8xxx_sw_attr_globals),
  1686. },
  1687. .attr_port = {
  1688. .attr = ar8xxx_sw_attr_port,
  1689. .n_attr = ARRAY_SIZE(ar8xxx_sw_attr_port),
  1690. },
  1691. .attr_vlan = {
  1692. .attr = ar8xxx_sw_attr_vlan,
  1693. .n_attr = ARRAY_SIZE(ar8xxx_sw_attr_vlan),
  1694. },
  1695. .get_port_pvid = ar8xxx_sw_get_pvid,
  1696. .set_port_pvid = ar8xxx_sw_set_pvid,
  1697. .get_vlan_ports = ar8xxx_sw_get_ports,
  1698. .set_vlan_ports = ar8xxx_sw_set_ports,
  1699. .apply_config = ar8xxx_sw_hw_apply,
  1700. .reset_switch = ar8xxx_sw_reset_switch,
  1701. .get_port_link = ar8xxx_sw_get_port_link,
  1702. };
  1703. static const struct switch_dev_ops ar8327_sw_ops = {
  1704. .attr_global = {
  1705. .attr = ar8327_sw_attr_globals,
  1706. .n_attr = ARRAY_SIZE(ar8327_sw_attr_globals),
  1707. },
  1708. .attr_port = {
  1709. .attr = ar8xxx_sw_attr_port,
  1710. .n_attr = ARRAY_SIZE(ar8xxx_sw_attr_port),
  1711. },
  1712. .attr_vlan = {
  1713. .attr = ar8xxx_sw_attr_vlan,
  1714. .n_attr = ARRAY_SIZE(ar8xxx_sw_attr_vlan),
  1715. },
  1716. .get_port_pvid = ar8xxx_sw_get_pvid,
  1717. .set_port_pvid = ar8xxx_sw_set_pvid,
  1718. .get_vlan_ports = ar8xxx_sw_get_ports,
  1719. .set_vlan_ports = ar8xxx_sw_set_ports,
  1720. .apply_config = ar8xxx_sw_hw_apply,
  1721. .reset_switch = ar8xxx_sw_reset_switch,
  1722. .get_port_link = ar8xxx_sw_get_port_link,
  1723. };
  1724. static int
  1725. ar8xxx_id_chip(struct ar8xxx_priv *priv)
  1726. {
  1727. u32 val;
  1728. u16 id;
  1729. int i;
  1730. val = priv->read(priv, AR8216_REG_CTRL);
  1731. if (val == ~0)
  1732. return -ENODEV;
  1733. id = val & (AR8216_CTRL_REVISION | AR8216_CTRL_VERSION);
  1734. for (i = 0; i < AR8X16_PROBE_RETRIES; i++) {
  1735. u16 t;
  1736. val = priv->read(priv, AR8216_REG_CTRL);
  1737. if (val == ~0)
  1738. return -ENODEV;
  1739. t = val & (AR8216_CTRL_REVISION | AR8216_CTRL_VERSION);
  1740. if (t != id)
  1741. return -ENODEV;
  1742. }
  1743. priv->chip_ver = (id & AR8216_CTRL_VERSION) >> AR8216_CTRL_VERSION_S;
  1744. priv->chip_rev = (id & AR8216_CTRL_REVISION);
  1745. switch (priv->chip_ver) {
  1746. case AR8XXX_VER_AR8216:
  1747. priv->chip = &ar8216_chip;
  1748. break;
  1749. case AR8XXX_VER_AR8236:
  1750. priv->chip = &ar8236_chip;
  1751. break;
  1752. case AR8XXX_VER_AR8316:
  1753. priv->chip = &ar8316_chip;
  1754. break;
  1755. case AR8XXX_VER_AR8327:
  1756. priv->mii_lo_first = true;
  1757. priv->chip = &ar8327_chip;
  1758. break;
  1759. default:
  1760. pr_err("ar8216: Unknown Atheros device [ver=%d, rev=%d]\n",
  1761. priv->chip_ver, priv->chip_rev);
  1762. return -ENODEV;
  1763. }
  1764. return 0;
  1765. }
  1766. static void
  1767. ar8xxx_mib_work_func(struct work_struct *work)
  1768. {
  1769. struct ar8xxx_priv *priv;
  1770. int err;
  1771. priv = container_of(work, struct ar8xxx_priv, mib_work.work);
  1772. mutex_lock(&priv->mib_lock);
  1773. err = ar8xxx_mib_capture(priv);
  1774. if (err)
  1775. goto next_port;
  1776. ar8xxx_mib_fetch_port_stat(priv, priv->mib_next_port, false);
  1777. next_port:
  1778. priv->mib_next_port++;
  1779. if (priv->mib_next_port >= priv->dev.ports)
  1780. priv->mib_next_port = 0;
  1781. mutex_unlock(&priv->mib_lock);
  1782. schedule_delayed_work(&priv->mib_work,
  1783. msecs_to_jiffies(AR8XXX_MIB_WORK_DELAY));
  1784. }
  1785. static int
  1786. ar8xxx_mib_init(struct ar8xxx_priv *priv)
  1787. {
  1788. unsigned int len;
  1789. if (!ar8xxx_has_mib_counters(priv))
  1790. return 0;
  1791. BUG_ON(!priv->chip->mib_decs || !priv->chip->num_mibs);
  1792. len = priv->dev.ports * priv->chip->num_mibs *
  1793. sizeof(*priv->mib_stats);
  1794. priv->mib_stats = kzalloc(len, GFP_KERNEL);
  1795. if (!priv->mib_stats)
  1796. return -ENOMEM;
  1797. return 0;
  1798. }
  1799. static void
  1800. ar8xxx_mib_start(struct ar8xxx_priv *priv)
  1801. {
  1802. if (!ar8xxx_has_mib_counters(priv))
  1803. return;
  1804. schedule_delayed_work(&priv->mib_work,
  1805. msecs_to_jiffies(AR8XXX_MIB_WORK_DELAY));
  1806. }
  1807. static void
  1808. ar8xxx_mib_stop(struct ar8xxx_priv *priv)
  1809. {
  1810. if (!ar8xxx_has_mib_counters(priv))
  1811. return;
  1812. cancel_delayed_work(&priv->mib_work);
  1813. }
  1814. static struct ar8xxx_priv *
  1815. ar8xxx_create(void)
  1816. {
  1817. struct ar8xxx_priv *priv;
  1818. priv = kzalloc(sizeof(struct ar8xxx_priv), GFP_KERNEL);
  1819. if (priv == NULL)
  1820. return NULL;
  1821. mutex_init(&priv->reg_mutex);
  1822. mutex_init(&priv->mib_lock);
  1823. INIT_DELAYED_WORK(&priv->mib_work, ar8xxx_mib_work_func);
  1824. return priv;
  1825. }
  1826. static void
  1827. ar8xxx_free(struct ar8xxx_priv *priv)
  1828. {
  1829. kfree(priv->mib_stats);
  1830. kfree(priv);
  1831. }
  1832. static struct ar8xxx_priv *
  1833. ar8xxx_create_mii(struct mii_bus *bus)
  1834. {
  1835. struct ar8xxx_priv *priv;
  1836. priv = ar8xxx_create();
  1837. if (priv) {
  1838. priv->mii_bus = bus;
  1839. priv->read = ar8xxx_mii_read;
  1840. priv->write = ar8xxx_mii_write;
  1841. }
  1842. return priv;
  1843. }
  1844. static int
  1845. ar8xxx_probe_switch(struct ar8xxx_priv *priv)
  1846. {
  1847. struct switch_dev *swdev;
  1848. int ret;
  1849. ret = ar8xxx_id_chip(priv);
  1850. if (ret)
  1851. return ret;
  1852. swdev = &priv->dev;
  1853. swdev->cpu_port = AR8216_PORT_CPU;
  1854. swdev->ops = &ar8xxx_sw_ops;
  1855. if (chip_is_ar8316(priv)) {
  1856. swdev->name = "Atheros AR8316";
  1857. swdev->vlans = AR8X16_MAX_VLANS;
  1858. swdev->ports = AR8216_NUM_PORTS;
  1859. } else if (chip_is_ar8236(priv)) {
  1860. swdev->name = "Atheros AR8236";
  1861. swdev->vlans = AR8216_NUM_VLANS;
  1862. swdev->ports = AR8216_NUM_PORTS;
  1863. } else if (chip_is_ar8327(priv)) {
  1864. swdev->name = "Atheros AR8327";
  1865. swdev->vlans = AR8X16_MAX_VLANS;
  1866. swdev->ports = AR8327_NUM_PORTS;
  1867. swdev->ops = &ar8327_sw_ops;
  1868. } else {
  1869. swdev->name = "Atheros AR8216";
  1870. swdev->vlans = AR8216_NUM_VLANS;
  1871. swdev->ports = AR8216_NUM_PORTS;
  1872. }
  1873. ret = ar8xxx_mib_init(priv);
  1874. if (ret)
  1875. return ret;
  1876. return 0;
  1877. }
  1878. static int
  1879. ar8xxx_start(struct ar8xxx_priv *priv)
  1880. {
  1881. int ret;
  1882. priv->init = true;
  1883. ret = priv->chip->hw_init(priv);
  1884. if (ret)
  1885. return ret;
  1886. ret = ar8xxx_sw_reset_switch(&priv->dev);
  1887. if (ret)
  1888. return ret;
  1889. priv->init = false;
  1890. ar8xxx_mib_start(priv);
  1891. return 0;
  1892. }
  1893. static int
  1894. ar8xxx_phy_config_init(struct phy_device *phydev)
  1895. {
  1896. struct ar8xxx_priv *priv = phydev->priv;
  1897. struct net_device *dev = phydev->attached_dev;
  1898. int ret;
  1899. if (WARN_ON(!priv))
  1900. return -ENODEV;
  1901. if (chip_is_ar8327(priv))
  1902. return 0;
  1903. priv->phy = phydev;
  1904. if (phydev->addr != 0) {
  1905. if (chip_is_ar8316(priv)) {
  1906. /* switch device has been initialized, reinit */
  1907. priv->dev.ports = (AR8216_NUM_PORTS - 1);
  1908. priv->initialized = false;
  1909. priv->port4_phy = true;
  1910. ar8316_hw_init(priv);
  1911. return 0;
  1912. }
  1913. return 0;
  1914. }
  1915. ret = ar8xxx_start(priv);
  1916. if (ret)
  1917. return ret;
  1918. /* VID fixup only needed on ar8216 */
  1919. if (chip_is_ar8216(priv)) {
  1920. dev->phy_ptr = priv;
  1921. dev->priv_flags |= IFF_NO_IP_ALIGN;
  1922. dev->eth_mangle_rx = ar8216_mangle_rx;
  1923. dev->eth_mangle_tx = ar8216_mangle_tx;
  1924. }
  1925. return 0;
  1926. }
  1927. static int
  1928. ar8xxx_phy_read_status(struct phy_device *phydev)
  1929. {
  1930. struct ar8xxx_priv *priv = phydev->priv;
  1931. struct switch_port_link link;
  1932. int ret;
  1933. if (phydev->addr != 0)
  1934. return genphy_read_status(phydev);
  1935. ar8216_read_port_link(priv, phydev->addr, &link);
  1936. phydev->link = !!link.link;
  1937. if (!phydev->link)
  1938. return 0;
  1939. switch (link.speed) {
  1940. case SWITCH_PORT_SPEED_10:
  1941. phydev->speed = SPEED_10;
  1942. break;
  1943. case SWITCH_PORT_SPEED_100:
  1944. phydev->speed = SPEED_100;
  1945. break;
  1946. case SWITCH_PORT_SPEED_1000:
  1947. phydev->speed = SPEED_1000;
  1948. break;
  1949. default:
  1950. phydev->speed = 0;
  1951. }
  1952. phydev->duplex = link.duplex ? DUPLEX_FULL : DUPLEX_HALF;
  1953. /* flush the address translation unit */
  1954. mutex_lock(&priv->reg_mutex);
  1955. ret = priv->chip->atu_flush(priv);
  1956. mutex_unlock(&priv->reg_mutex);
  1957. phydev->state = PHY_RUNNING;
  1958. netif_carrier_on(phydev->attached_dev);
  1959. phydev->adjust_link(phydev->attached_dev);
  1960. return ret;
  1961. }
  1962. static int
  1963. ar8xxx_phy_config_aneg(struct phy_device *phydev)
  1964. {
  1965. if (phydev->addr == 0)
  1966. return 0;
  1967. return genphy_config_aneg(phydev);
  1968. }
  1969. static const u32 ar8xxx_phy_ids[] = {
  1970. 0x004dd033,
  1971. 0x004dd034,
  1972. 0x004dd041,
  1973. 0x004dd042,
  1974. };
  1975. static bool
  1976. ar8xxx_phy_match(u32 phy_id)
  1977. {
  1978. int i;
  1979. for (i = 0; i < ARRAY_SIZE(ar8xxx_phy_ids); i++)
  1980. if (phy_id == ar8xxx_phy_ids[i])
  1981. return true;
  1982. return false;
  1983. }
  1984. static bool
  1985. ar8xxx_is_possible(struct mii_bus *bus)
  1986. {
  1987. unsigned i;
  1988. for (i = 0; i < 4; i++) {
  1989. u32 phy_id;
  1990. phy_id = mdiobus_read(bus, i, MII_PHYSID1) << 16;
  1991. phy_id |= mdiobus_read(bus, i, MII_PHYSID2);
  1992. if (!ar8xxx_phy_match(phy_id)) {
  1993. pr_debug("ar8xxx: unknown PHY at %s:%02x id:%08x\n",
  1994. dev_name(&bus->dev), i, phy_id);
  1995. return false;
  1996. }
  1997. }
  1998. return true;
  1999. }
  2000. static int
  2001. ar8xxx_phy_probe(struct phy_device *phydev)
  2002. {
  2003. struct ar8xxx_priv *priv;
  2004. struct switch_dev *swdev;
  2005. int ret;
  2006. /* skip PHYs at unused adresses */
  2007. if (phydev->addr != 0 && phydev->addr != 4)
  2008. return -ENODEV;
  2009. if (!ar8xxx_is_possible(phydev->bus))
  2010. return -ENODEV;
  2011. mutex_lock(&ar8xxx_dev_list_lock);
  2012. list_for_each_entry(priv, &ar8xxx_dev_list, list)
  2013. if (priv->mii_bus == phydev->bus)
  2014. goto found;
  2015. priv = ar8xxx_create_mii(phydev->bus);
  2016. if (priv == NULL) {
  2017. ret = -ENOMEM;
  2018. goto unlock;
  2019. }
  2020. ret = ar8xxx_probe_switch(priv);
  2021. if (ret)
  2022. goto free_priv;
  2023. swdev = &priv->dev;
  2024. swdev->alias = dev_name(&priv->mii_bus->dev);
  2025. ret = register_switch(swdev, NULL);
  2026. if (ret)
  2027. goto free_priv;
  2028. pr_info("%s: %s rev. %u switch registered on %s\n",
  2029. swdev->devname, swdev->name, priv->chip_rev,
  2030. dev_name(&priv->mii_bus->dev));
  2031. found:
  2032. priv->use_count++;
  2033. if (phydev->addr == 0) {
  2034. if (ar8xxx_has_gige(priv)) {
  2035. phydev->supported = SUPPORTED_1000baseT_Full;
  2036. phydev->advertising = ADVERTISED_1000baseT_Full;
  2037. } else {
  2038. phydev->supported = SUPPORTED_100baseT_Full;
  2039. phydev->advertising = ADVERTISED_100baseT_Full;
  2040. }
  2041. if (chip_is_ar8327(priv)) {
  2042. priv->phy = phydev;
  2043. ret = ar8xxx_start(priv);
  2044. if (ret)
  2045. goto err_unregister_switch;
  2046. }
  2047. } else {
  2048. if (ar8xxx_has_gige(priv)) {
  2049. phydev->supported |= SUPPORTED_1000baseT_Full;
  2050. phydev->advertising |= ADVERTISED_1000baseT_Full;
  2051. }
  2052. }
  2053. phydev->priv = priv;
  2054. list_add(&priv->list, &ar8xxx_dev_list);
  2055. mutex_unlock(&ar8xxx_dev_list_lock);
  2056. return 0;
  2057. err_unregister_switch:
  2058. if (--priv->use_count)
  2059. goto unlock;
  2060. unregister_switch(&priv->dev);
  2061. free_priv:
  2062. ar8xxx_free(priv);
  2063. unlock:
  2064. mutex_unlock(&ar8xxx_dev_list_lock);
  2065. return ret;
  2066. }
  2067. static void
  2068. ar8xxx_phy_detach(struct phy_device *phydev)
  2069. {
  2070. struct net_device *dev = phydev->attached_dev;
  2071. if (!dev)
  2072. return;
  2073. dev->phy_ptr = NULL;
  2074. dev->priv_flags &= ~IFF_NO_IP_ALIGN;
  2075. dev->eth_mangle_rx = NULL;
  2076. dev->eth_mangle_tx = NULL;
  2077. }
  2078. static void
  2079. ar8xxx_phy_remove(struct phy_device *phydev)
  2080. {
  2081. struct ar8xxx_priv *priv = phydev->priv;
  2082. if (WARN_ON(!priv))
  2083. return;
  2084. phydev->priv = NULL;
  2085. if (--priv->use_count > 0)
  2086. return;
  2087. mutex_lock(&ar8xxx_dev_list_lock);
  2088. list_del(&priv->list);
  2089. mutex_unlock(&ar8xxx_dev_list_lock);
  2090. unregister_switch(&priv->dev);
  2091. ar8xxx_mib_stop(priv);
  2092. ar8xxx_free(priv);
  2093. }
  2094. static struct phy_driver ar8xxx_phy_driver = {
  2095. .phy_id = 0x004d0000,
  2096. .name = "Atheros AR8216/AR8236/AR8316",
  2097. .phy_id_mask = 0xffff0000,
  2098. .features = PHY_BASIC_FEATURES,
  2099. .probe = ar8xxx_phy_probe,
  2100. .remove = ar8xxx_phy_remove,
  2101. .detach = ar8xxx_phy_detach,
  2102. .config_init = ar8xxx_phy_config_init,
  2103. .config_aneg = ar8xxx_phy_config_aneg,
  2104. .read_status = ar8xxx_phy_read_status,
  2105. .driver = { .owner = THIS_MODULE },
  2106. };
  2107. int __init
  2108. ar8xxx_init(void)
  2109. {
  2110. return phy_driver_register(&ar8xxx_phy_driver);
  2111. }
  2112. void __exit
  2113. ar8xxx_exit(void)
  2114. {
  2115. phy_driver_unregister(&ar8xxx_phy_driver);
  2116. }
  2117. module_init(ar8xxx_init);
  2118. module_exit(ar8xxx_exit);
  2119. MODULE_LICENSE("GPL");