macb.c 75 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027
  1. /*
  2. * Cadence MACB/GEM Ethernet Controller driver
  3. *
  4. * Copyright (C) 2004-2006 Atmel Corporation
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. */
  10. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  11. #include <linux/clk.h>
  12. #include <linux/module.h>
  13. #include <linux/moduleparam.h>
  14. #include <linux/kernel.h>
  15. #include <linux/types.h>
  16. #include <linux/circ_buf.h>
  17. #include <linux/slab.h>
  18. #include <linux/init.h>
  19. #include <linux/io.h>
  20. #include <linux/gpio.h>
  21. #include <linux/interrupt.h>
  22. #include <linux/netdevice.h>
  23. #include <linux/etherdevice.h>
  24. #include <linux/dma-mapping.h>
  25. #include <linux/platform_data/macb.h>
  26. #include <linux/platform_device.h>
  27. #include <linux/phy.h>
  28. #include <linux/of.h>
  29. #include <linux/of_device.h>
  30. #include <linux/of_mdio.h>
  31. #include <linux/of_net.h>
  32. #include "macb.h"
  33. #define MACB_RX_BUFFER_SIZE 128
  34. #define RX_BUFFER_MULTIPLE 64 /* bytes */
  35. #define RX_RING_SIZE 512 /* must be power of 2 */
  36. #define RX_RING_BYTES (sizeof(struct macb_dma_desc) * RX_RING_SIZE)
  37. #define TX_RING_SIZE 128 /* must be power of 2 */
  38. #define TX_RING_BYTES (sizeof(struct macb_dma_desc) * TX_RING_SIZE)
  39. /* level of occupied TX descriptors under which we wake up TX process */
  40. #define MACB_TX_WAKEUP_THRESH (3 * TX_RING_SIZE / 4)
  41. #define MACB_RX_INT_FLAGS (MACB_BIT(RCOMP) | MACB_BIT(RXUBR) \
  42. | MACB_BIT(ISR_ROVR))
  43. #define MACB_TX_ERR_FLAGS (MACB_BIT(ISR_TUND) \
  44. | MACB_BIT(ISR_RLE) \
  45. | MACB_BIT(TXERR))
  46. #define MACB_TX_INT_FLAGS (MACB_TX_ERR_FLAGS | MACB_BIT(TCOMP))
  47. #define MACB_MAX_TX_LEN ((unsigned int)((1 << MACB_TX_FRMLEN_SIZE) - 1))
  48. #define GEM_MAX_TX_LEN ((unsigned int)((1 << GEM_TX_FRMLEN_SIZE) - 1))
  49. #define GEM_MTU_MIN_SIZE 68
  50. /*
  51. * Graceful stop timeouts in us. We should allow up to
  52. * 1 frame time (10 Mbits/s, full-duplex, ignoring collisions)
  53. */
  54. #define MACB_HALT_TIMEOUT 1230
  55. /* Ring buffer accessors */
  56. static unsigned int macb_tx_ring_wrap(unsigned int index)
  57. {
  58. return index & (TX_RING_SIZE - 1);
  59. }
  60. static struct macb_dma_desc *macb_tx_desc(struct macb_queue *queue,
  61. unsigned int index)
  62. {
  63. return &queue->tx_ring[macb_tx_ring_wrap(index)];
  64. }
  65. static struct macb_tx_skb *macb_tx_skb(struct macb_queue *queue,
  66. unsigned int index)
  67. {
  68. return &queue->tx_skb[macb_tx_ring_wrap(index)];
  69. }
  70. static dma_addr_t macb_tx_dma(struct macb_queue *queue, unsigned int index)
  71. {
  72. dma_addr_t offset;
  73. offset = macb_tx_ring_wrap(index) * sizeof(struct macb_dma_desc);
  74. return queue->tx_ring_dma + offset;
  75. }
  76. static unsigned int macb_rx_ring_wrap(unsigned int index)
  77. {
  78. return index & (RX_RING_SIZE - 1);
  79. }
  80. static struct macb_dma_desc *macb_rx_desc(struct macb *bp, unsigned int index)
  81. {
  82. return &bp->rx_ring[macb_rx_ring_wrap(index)];
  83. }
  84. static void *macb_rx_buffer(struct macb *bp, unsigned int index)
  85. {
  86. return bp->rx_buffers + bp->rx_buffer_size * macb_rx_ring_wrap(index);
  87. }
  88. /* I/O accessors */
  89. static u32 hw_readl_native(struct macb *bp, int offset)
  90. {
  91. return __raw_readl(bp->regs + offset);
  92. }
  93. static void hw_writel_native(struct macb *bp, int offset, u32 value)
  94. {
  95. __raw_writel(value, bp->regs + offset);
  96. }
  97. static u32 hw_readl(struct macb *bp, int offset)
  98. {
  99. return readl_relaxed(bp->regs + offset);
  100. }
  101. static void hw_writel(struct macb *bp, int offset, u32 value)
  102. {
  103. writel_relaxed(value, bp->regs + offset);
  104. }
  105. /*
  106. * Find the CPU endianness by using the loopback bit of NCR register. When the
  107. * CPU is in big endian we need to program swaped mode for management
  108. * descriptor access.
  109. */
  110. static bool hw_is_native_io(void __iomem *addr)
  111. {
  112. u32 value = MACB_BIT(LLB);
  113. __raw_writel(value, addr + MACB_NCR);
  114. value = __raw_readl(addr + MACB_NCR);
  115. /* Write 0 back to disable everything */
  116. __raw_writel(0, addr + MACB_NCR);
  117. return value == MACB_BIT(LLB);
  118. }
  119. static bool hw_is_gem(void __iomem *addr, bool native_io)
  120. {
  121. u32 id;
  122. if (native_io)
  123. id = __raw_readl(addr + MACB_MID);
  124. else
  125. id = readl_relaxed(addr + MACB_MID);
  126. return MACB_BFEXT(IDNUM, id) >= 0x2;
  127. }
  128. static void macb_set_hwaddr(struct macb *bp)
  129. {
  130. u32 bottom;
  131. u16 top;
  132. bottom = cpu_to_le32(*((u32 *)bp->dev->dev_addr));
  133. macb_or_gem_writel(bp, SA1B, bottom);
  134. top = cpu_to_le16(*((u16 *)(bp->dev->dev_addr + 4)));
  135. macb_or_gem_writel(bp, SA1T, top);
  136. /* Clear unused address register sets */
  137. macb_or_gem_writel(bp, SA2B, 0);
  138. macb_or_gem_writel(bp, SA2T, 0);
  139. macb_or_gem_writel(bp, SA3B, 0);
  140. macb_or_gem_writel(bp, SA3T, 0);
  141. macb_or_gem_writel(bp, SA4B, 0);
  142. macb_or_gem_writel(bp, SA4T, 0);
  143. }
  144. static void macb_get_hwaddr(struct macb *bp)
  145. {
  146. struct macb_platform_data *pdata;
  147. u32 bottom;
  148. u16 top;
  149. u8 addr[6];
  150. int i;
  151. pdata = dev_get_platdata(&bp->pdev->dev);
  152. /* Check all 4 address register for vaild address */
  153. for (i = 0; i < 4; i++) {
  154. bottom = macb_or_gem_readl(bp, SA1B + i * 8);
  155. top = macb_or_gem_readl(bp, SA1T + i * 8);
  156. if (pdata && pdata->rev_eth_addr) {
  157. addr[5] = bottom & 0xff;
  158. addr[4] = (bottom >> 8) & 0xff;
  159. addr[3] = (bottom >> 16) & 0xff;
  160. addr[2] = (bottom >> 24) & 0xff;
  161. addr[1] = top & 0xff;
  162. addr[0] = (top & 0xff00) >> 8;
  163. } else {
  164. addr[0] = bottom & 0xff;
  165. addr[1] = (bottom >> 8) & 0xff;
  166. addr[2] = (bottom >> 16) & 0xff;
  167. addr[3] = (bottom >> 24) & 0xff;
  168. addr[4] = top & 0xff;
  169. addr[5] = (top >> 8) & 0xff;
  170. }
  171. if (is_valid_ether_addr(addr)) {
  172. memcpy(bp->dev->dev_addr, addr, sizeof(addr));
  173. return;
  174. }
  175. }
  176. dev_info(&bp->pdev->dev, "invalid hw address, using random\n");
  177. eth_hw_addr_random(bp->dev);
  178. }
  179. static int macb_mdio_read(struct mii_bus *bus, int mii_id, int regnum)
  180. {
  181. struct macb *bp = bus->priv;
  182. int value;
  183. macb_writel(bp, MAN, (MACB_BF(SOF, MACB_MAN_SOF)
  184. | MACB_BF(RW, MACB_MAN_READ)
  185. | MACB_BF(PHYA, mii_id)
  186. | MACB_BF(REGA, regnum)
  187. | MACB_BF(CODE, MACB_MAN_CODE)));
  188. /* wait for end of transfer */
  189. while (!MACB_BFEXT(IDLE, macb_readl(bp, NSR)))
  190. cpu_relax();
  191. value = MACB_BFEXT(DATA, macb_readl(bp, MAN));
  192. return value;
  193. }
  194. static int macb_mdio_write(struct mii_bus *bus, int mii_id, int regnum,
  195. u16 value)
  196. {
  197. struct macb *bp = bus->priv;
  198. macb_writel(bp, MAN, (MACB_BF(SOF, MACB_MAN_SOF)
  199. | MACB_BF(RW, MACB_MAN_WRITE)
  200. | MACB_BF(PHYA, mii_id)
  201. | MACB_BF(REGA, regnum)
  202. | MACB_BF(CODE, MACB_MAN_CODE)
  203. | MACB_BF(DATA, value)));
  204. /* wait for end of transfer */
  205. while (!MACB_BFEXT(IDLE, macb_readl(bp, NSR)))
  206. cpu_relax();
  207. return 0;
  208. }
  209. /**
  210. * macb_set_tx_clk() - Set a clock to a new frequency
  211. * @clk Pointer to the clock to change
  212. * @rate New frequency in Hz
  213. * @dev Pointer to the struct net_device
  214. */
  215. static void macb_set_tx_clk(struct clk *clk, int speed, struct net_device *dev)
  216. {
  217. long ferr, rate, rate_rounded;
  218. if (!clk)
  219. return;
  220. switch (speed) {
  221. case SPEED_10:
  222. rate = 2500000;
  223. break;
  224. case SPEED_100:
  225. rate = 25000000;
  226. break;
  227. case SPEED_1000:
  228. rate = 125000000;
  229. break;
  230. default:
  231. return;
  232. }
  233. rate_rounded = clk_round_rate(clk, rate);
  234. if (rate_rounded < 0)
  235. return;
  236. /* RGMII allows 50 ppm frequency error. Test and warn if this limit
  237. * is not satisfied.
  238. */
  239. ferr = abs(rate_rounded - rate);
  240. ferr = DIV_ROUND_UP(ferr, rate / 100000);
  241. if (ferr > 5)
  242. netdev_warn(dev, "unable to generate target frequency: %ld Hz\n",
  243. rate);
  244. if (clk_set_rate(clk, rate_rounded))
  245. netdev_err(dev, "adjusting tx_clk failed.\n");
  246. }
  247. static void macb_handle_link_change(struct net_device *dev)
  248. {
  249. struct macb *bp = netdev_priv(dev);
  250. struct phy_device *phydev = bp->phy_dev;
  251. unsigned long flags;
  252. int status_change = 0;
  253. spin_lock_irqsave(&bp->lock, flags);
  254. if (phydev->link) {
  255. if ((bp->speed != phydev->speed) ||
  256. (bp->duplex != phydev->duplex)) {
  257. u32 reg;
  258. reg = macb_readl(bp, NCFGR);
  259. reg &= ~(MACB_BIT(SPD) | MACB_BIT(FD));
  260. if (macb_is_gem(bp))
  261. reg &= ~GEM_BIT(GBE);
  262. if (phydev->duplex)
  263. reg |= MACB_BIT(FD);
  264. if (phydev->speed == SPEED_100)
  265. reg |= MACB_BIT(SPD);
  266. if (phydev->speed == SPEED_1000 &&
  267. bp->caps & MACB_CAPS_GIGABIT_MODE_AVAILABLE)
  268. reg |= GEM_BIT(GBE);
  269. macb_or_gem_writel(bp, NCFGR, reg);
  270. bp->speed = phydev->speed;
  271. bp->duplex = phydev->duplex;
  272. status_change = 1;
  273. }
  274. }
  275. if (phydev->link != bp->link) {
  276. if (!phydev->link) {
  277. bp->speed = 0;
  278. bp->duplex = -1;
  279. }
  280. bp->link = phydev->link;
  281. status_change = 1;
  282. }
  283. spin_unlock_irqrestore(&bp->lock, flags);
  284. if (status_change) {
  285. if (phydev->link) {
  286. /* Update the TX clock rate if and only if the link is
  287. * up and there has been a link change.
  288. */
  289. macb_set_tx_clk(bp->tx_clk, phydev->speed, dev);
  290. netif_carrier_on(dev);
  291. netdev_info(dev, "link up (%d/%s)\n",
  292. phydev->speed,
  293. phydev->duplex == DUPLEX_FULL ?
  294. "Full" : "Half");
  295. } else {
  296. netif_carrier_off(dev);
  297. netdev_info(dev, "link down\n");
  298. }
  299. }
  300. }
  301. /* based on au1000_eth. c*/
  302. static int macb_mii_probe(struct net_device *dev)
  303. {
  304. struct macb *bp = netdev_priv(dev);
  305. struct macb_platform_data *pdata;
  306. struct phy_device *phydev;
  307. int phy_irq;
  308. int ret;
  309. phydev = phy_find_first(bp->mii_bus);
  310. if (!phydev) {
  311. netdev_err(dev, "no PHY found\n");
  312. return -ENXIO;
  313. }
  314. pdata = dev_get_platdata(&bp->pdev->dev);
  315. if (pdata && gpio_is_valid(pdata->phy_irq_pin)) {
  316. ret = devm_gpio_request(&bp->pdev->dev, pdata->phy_irq_pin, "phy int");
  317. if (!ret) {
  318. phy_irq = gpio_to_irq(pdata->phy_irq_pin);
  319. phydev->irq = (phy_irq < 0) ? PHY_POLL : phy_irq;
  320. }
  321. }
  322. /* attach the mac to the phy */
  323. ret = phy_connect_direct(dev, phydev, &macb_handle_link_change,
  324. bp->phy_interface);
  325. if (ret) {
  326. netdev_err(dev, "Could not attach to PHY\n");
  327. return ret;
  328. }
  329. /* mask with MAC supported features */
  330. if (macb_is_gem(bp) && bp->caps & MACB_CAPS_GIGABIT_MODE_AVAILABLE)
  331. phydev->supported &= PHY_GBIT_FEATURES;
  332. else
  333. phydev->supported &= PHY_BASIC_FEATURES;
  334. if (bp->caps & MACB_CAPS_NO_GIGABIT_HALF)
  335. phydev->supported &= ~SUPPORTED_1000baseT_Half;
  336. phydev->advertising = phydev->supported;
  337. bp->link = 0;
  338. bp->speed = 0;
  339. bp->duplex = -1;
  340. bp->phy_dev = phydev;
  341. return 0;
  342. }
  343. static int macb_mii_init(struct macb *bp)
  344. {
  345. struct macb_platform_data *pdata;
  346. struct device_node *np;
  347. int err = -ENXIO, i;
  348. /* Enable management port */
  349. macb_writel(bp, NCR, MACB_BIT(MPE));
  350. bp->mii_bus = mdiobus_alloc();
  351. if (bp->mii_bus == NULL) {
  352. err = -ENOMEM;
  353. goto err_out;
  354. }
  355. bp->mii_bus->name = "MACB_mii_bus";
  356. bp->mii_bus->read = &macb_mdio_read;
  357. bp->mii_bus->write = &macb_mdio_write;
  358. snprintf(bp->mii_bus->id, MII_BUS_ID_SIZE, "%s-%x",
  359. bp->pdev->name, bp->pdev->id);
  360. bp->mii_bus->priv = bp;
  361. bp->mii_bus->parent = &bp->dev->dev;
  362. pdata = dev_get_platdata(&bp->pdev->dev);
  363. bp->mii_bus->irq = kmalloc(sizeof(int)*PHY_MAX_ADDR, GFP_KERNEL);
  364. if (!bp->mii_bus->irq) {
  365. err = -ENOMEM;
  366. goto err_out_free_mdiobus;
  367. }
  368. dev_set_drvdata(&bp->dev->dev, bp->mii_bus);
  369. np = bp->pdev->dev.of_node;
  370. if (np) {
  371. /* try dt phy registration */
  372. err = of_mdiobus_register(bp->mii_bus, np);
  373. /* fallback to standard phy registration if no phy were
  374. found during dt phy registration */
  375. if (!err && !phy_find_first(bp->mii_bus)) {
  376. for (i = 0; i < PHY_MAX_ADDR; i++) {
  377. struct phy_device *phydev;
  378. phydev = mdiobus_scan(bp->mii_bus, i);
  379. if (IS_ERR(phydev)) {
  380. err = PTR_ERR(phydev);
  381. break;
  382. }
  383. }
  384. if (err)
  385. goto err_out_unregister_bus;
  386. }
  387. } else {
  388. for (i = 0; i < PHY_MAX_ADDR; i++)
  389. bp->mii_bus->irq[i] = PHY_POLL;
  390. if (pdata)
  391. bp->mii_bus->phy_mask = pdata->phy_mask;
  392. err = mdiobus_register(bp->mii_bus);
  393. }
  394. if (err)
  395. goto err_out_free_mdio_irq;
  396. err = macb_mii_probe(bp->dev);
  397. if (err)
  398. goto err_out_unregister_bus;
  399. return 0;
  400. err_out_unregister_bus:
  401. mdiobus_unregister(bp->mii_bus);
  402. err_out_free_mdio_irq:
  403. kfree(bp->mii_bus->irq);
  404. err_out_free_mdiobus:
  405. mdiobus_free(bp->mii_bus);
  406. err_out:
  407. return err;
  408. }
  409. static void macb_update_stats(struct macb *bp)
  410. {
  411. u32 *p = &bp->hw_stats.macb.rx_pause_frames;
  412. u32 *end = &bp->hw_stats.macb.tx_pause_frames + 1;
  413. int offset = MACB_PFR;
  414. WARN_ON((unsigned long)(end - p - 1) != (MACB_TPF - MACB_PFR) / 4);
  415. for(; p < end; p++, offset += 4)
  416. *p += bp->macb_reg_readl(bp, offset);
  417. }
  418. static int macb_halt_tx(struct macb *bp)
  419. {
  420. unsigned long halt_time, timeout;
  421. u32 status;
  422. macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(THALT));
  423. timeout = jiffies + usecs_to_jiffies(MACB_HALT_TIMEOUT);
  424. do {
  425. halt_time = jiffies;
  426. status = macb_readl(bp, TSR);
  427. if (!(status & MACB_BIT(TGO)))
  428. return 0;
  429. usleep_range(10, 250);
  430. } while (time_before(halt_time, timeout));
  431. return -ETIMEDOUT;
  432. }
  433. static void macb_tx_unmap(struct macb *bp, struct macb_tx_skb *tx_skb)
  434. {
  435. if (tx_skb->mapping) {
  436. if (tx_skb->mapped_as_page)
  437. dma_unmap_page(&bp->pdev->dev, tx_skb->mapping,
  438. tx_skb->size, DMA_TO_DEVICE);
  439. else
  440. dma_unmap_single(&bp->pdev->dev, tx_skb->mapping,
  441. tx_skb->size, DMA_TO_DEVICE);
  442. tx_skb->mapping = 0;
  443. }
  444. if (tx_skb->skb) {
  445. dev_kfree_skb_any(tx_skb->skb);
  446. tx_skb->skb = NULL;
  447. }
  448. }
  449. static void macb_tx_error_task(struct work_struct *work)
  450. {
  451. struct macb_queue *queue = container_of(work, struct macb_queue,
  452. tx_error_task);
  453. struct macb *bp = queue->bp;
  454. struct macb_tx_skb *tx_skb;
  455. struct macb_dma_desc *desc;
  456. struct sk_buff *skb;
  457. unsigned int tail;
  458. unsigned long flags;
  459. netdev_vdbg(bp->dev, "macb_tx_error_task: q = %u, t = %u, h = %u\n",
  460. (unsigned int)(queue - bp->queues),
  461. queue->tx_tail, queue->tx_head);
  462. /* Prevent the queue IRQ handlers from running: each of them may call
  463. * macb_tx_interrupt(), which in turn may call netif_wake_subqueue().
  464. * As explained below, we have to halt the transmission before updating
  465. * TBQP registers so we call netif_tx_stop_all_queues() to notify the
  466. * network engine about the macb/gem being halted.
  467. */
  468. spin_lock_irqsave(&bp->lock, flags);
  469. /* Make sure nobody is trying to queue up new packets */
  470. netif_tx_stop_all_queues(bp->dev);
  471. /*
  472. * Stop transmission now
  473. * (in case we have just queued new packets)
  474. * macb/gem must be halted to write TBQP register
  475. */
  476. if (macb_halt_tx(bp))
  477. /* Just complain for now, reinitializing TX path can be good */
  478. netdev_err(bp->dev, "BUG: halt tx timed out\n");
  479. /*
  480. * Treat frames in TX queue including the ones that caused the error.
  481. * Free transmit buffers in upper layer.
  482. */
  483. for (tail = queue->tx_tail; tail != queue->tx_head; tail++) {
  484. u32 ctrl;
  485. desc = macb_tx_desc(queue, tail);
  486. ctrl = desc->ctrl;
  487. tx_skb = macb_tx_skb(queue, tail);
  488. skb = tx_skb->skb;
  489. if (ctrl & MACB_BIT(TX_USED)) {
  490. /* skb is set for the last buffer of the frame */
  491. while (!skb) {
  492. macb_tx_unmap(bp, tx_skb);
  493. tail++;
  494. tx_skb = macb_tx_skb(queue, tail);
  495. skb = tx_skb->skb;
  496. }
  497. /* ctrl still refers to the first buffer descriptor
  498. * since it's the only one written back by the hardware
  499. */
  500. if (!(ctrl & MACB_BIT(TX_BUF_EXHAUSTED))) {
  501. netdev_vdbg(bp->dev, "txerr skb %u (data %p) TX complete\n",
  502. macb_tx_ring_wrap(tail), skb->data);
  503. bp->stats.tx_packets++;
  504. bp->stats.tx_bytes += skb->len;
  505. }
  506. } else {
  507. /*
  508. * "Buffers exhausted mid-frame" errors may only happen
  509. * if the driver is buggy, so complain loudly about those.
  510. * Statistics are updated by hardware.
  511. */
  512. if (ctrl & MACB_BIT(TX_BUF_EXHAUSTED))
  513. netdev_err(bp->dev,
  514. "BUG: TX buffers exhausted mid-frame\n");
  515. desc->ctrl = ctrl | MACB_BIT(TX_USED);
  516. }
  517. macb_tx_unmap(bp, tx_skb);
  518. }
  519. /* Set end of TX queue */
  520. desc = macb_tx_desc(queue, 0);
  521. desc->addr = 0;
  522. desc->ctrl = MACB_BIT(TX_USED);
  523. /* Make descriptor updates visible to hardware */
  524. wmb();
  525. /* Reinitialize the TX desc queue */
  526. queue_writel(queue, TBQP, queue->tx_ring_dma);
  527. /* Make TX ring reflect state of hardware */
  528. queue->tx_head = 0;
  529. queue->tx_tail = 0;
  530. /* Housework before enabling TX IRQ */
  531. macb_writel(bp, TSR, macb_readl(bp, TSR));
  532. queue_writel(queue, IER, MACB_TX_INT_FLAGS);
  533. /* Now we are ready to start transmission again */
  534. netif_tx_start_all_queues(bp->dev);
  535. macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(TSTART));
  536. spin_unlock_irqrestore(&bp->lock, flags);
  537. }
  538. static void macb_tx_interrupt(struct macb_queue *queue)
  539. {
  540. unsigned int tail;
  541. unsigned int head;
  542. u32 status;
  543. struct macb *bp = queue->bp;
  544. u16 queue_index = queue - bp->queues;
  545. status = macb_readl(bp, TSR);
  546. macb_writel(bp, TSR, status);
  547. if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
  548. queue_writel(queue, ISR, MACB_BIT(TCOMP));
  549. netdev_vdbg(bp->dev, "macb_tx_interrupt status = 0x%03lx\n",
  550. (unsigned long)status);
  551. head = queue->tx_head;
  552. for (tail = queue->tx_tail; tail != head; tail++) {
  553. struct macb_tx_skb *tx_skb;
  554. struct sk_buff *skb;
  555. struct macb_dma_desc *desc;
  556. u32 ctrl;
  557. desc = macb_tx_desc(queue, tail);
  558. /* Make hw descriptor updates visible to CPU */
  559. rmb();
  560. ctrl = desc->ctrl;
  561. /* TX_USED bit is only set by hardware on the very first buffer
  562. * descriptor of the transmitted frame.
  563. */
  564. if (!(ctrl & MACB_BIT(TX_USED)))
  565. break;
  566. /* Process all buffers of the current transmitted frame */
  567. for (;; tail++) {
  568. tx_skb = macb_tx_skb(queue, tail);
  569. skb = tx_skb->skb;
  570. /* First, update TX stats if needed */
  571. if (skb) {
  572. netdev_vdbg(bp->dev, "skb %u (data %p) TX complete\n",
  573. macb_tx_ring_wrap(tail), skb->data);
  574. bp->stats.tx_packets++;
  575. bp->stats.tx_bytes += skb->len;
  576. }
  577. /* Now we can safely release resources */
  578. macb_tx_unmap(bp, tx_skb);
  579. /* skb is set only for the last buffer of the frame.
  580. * WARNING: at this point skb has been freed by
  581. * macb_tx_unmap().
  582. */
  583. if (skb)
  584. break;
  585. }
  586. }
  587. queue->tx_tail = tail;
  588. if (__netif_subqueue_stopped(bp->dev, queue_index) &&
  589. CIRC_CNT(queue->tx_head, queue->tx_tail,
  590. TX_RING_SIZE) <= MACB_TX_WAKEUP_THRESH)
  591. netif_wake_subqueue(bp->dev, queue_index);
  592. }
  593. static void gem_rx_refill(struct macb *bp)
  594. {
  595. unsigned int entry;
  596. struct sk_buff *skb;
  597. dma_addr_t paddr;
  598. while (CIRC_SPACE(bp->rx_prepared_head, bp->rx_tail, RX_RING_SIZE) > 0) {
  599. entry = macb_rx_ring_wrap(bp->rx_prepared_head);
  600. /* Make hw descriptor updates visible to CPU */
  601. rmb();
  602. bp->rx_prepared_head++;
  603. if (bp->rx_skbuff[entry] == NULL) {
  604. /* allocate sk_buff for this free entry in ring */
  605. skb = netdev_alloc_skb(bp->dev, bp->rx_buffer_size);
  606. if (unlikely(skb == NULL)) {
  607. netdev_err(bp->dev,
  608. "Unable to allocate sk_buff\n");
  609. break;
  610. }
  611. /* now fill corresponding descriptor entry */
  612. paddr = dma_map_single(&bp->pdev->dev, skb->data,
  613. bp->rx_buffer_size, DMA_FROM_DEVICE);
  614. if (dma_mapping_error(&bp->pdev->dev, paddr)) {
  615. dev_kfree_skb(skb);
  616. break;
  617. }
  618. bp->rx_skbuff[entry] = skb;
  619. if (entry == RX_RING_SIZE - 1)
  620. paddr |= MACB_BIT(RX_WRAP);
  621. bp->rx_ring[entry].addr = paddr;
  622. bp->rx_ring[entry].ctrl = 0;
  623. /* properly align Ethernet header */
  624. skb_reserve(skb, NET_IP_ALIGN);
  625. } else {
  626. bp->rx_ring[entry].addr &= ~MACB_BIT(RX_USED);
  627. bp->rx_ring[entry].ctrl = 0;
  628. }
  629. }
  630. /* Make descriptor updates visible to hardware */
  631. wmb();
  632. netdev_vdbg(bp->dev, "rx ring: prepared head %d, tail %d\n",
  633. bp->rx_prepared_head, bp->rx_tail);
  634. }
  635. /* Mark DMA descriptors from begin up to and not including end as unused */
  636. static void discard_partial_frame(struct macb *bp, unsigned int begin,
  637. unsigned int end)
  638. {
  639. unsigned int frag;
  640. for (frag = begin; frag != end; frag++) {
  641. struct macb_dma_desc *desc = macb_rx_desc(bp, frag);
  642. desc->addr &= ~MACB_BIT(RX_USED);
  643. }
  644. /* Make descriptor updates visible to hardware */
  645. wmb();
  646. /*
  647. * When this happens, the hardware stats registers for
  648. * whatever caused this is updated, so we don't have to record
  649. * anything.
  650. */
  651. }
  652. static int gem_rx(struct macb *bp, int budget)
  653. {
  654. unsigned int len;
  655. unsigned int entry;
  656. struct sk_buff *skb;
  657. struct macb_dma_desc *desc;
  658. int count = 0;
  659. while (count < budget) {
  660. u32 addr, ctrl;
  661. entry = macb_rx_ring_wrap(bp->rx_tail);
  662. desc = &bp->rx_ring[entry];
  663. /* Make hw descriptor updates visible to CPU */
  664. rmb();
  665. addr = desc->addr;
  666. ctrl = desc->ctrl;
  667. if (!(addr & MACB_BIT(RX_USED)))
  668. break;
  669. bp->rx_tail++;
  670. count++;
  671. if (!(ctrl & MACB_BIT(RX_SOF) && ctrl & MACB_BIT(RX_EOF))) {
  672. netdev_err(bp->dev,
  673. "not whole frame pointed by descriptor\n");
  674. bp->stats.rx_dropped++;
  675. break;
  676. }
  677. skb = bp->rx_skbuff[entry];
  678. if (unlikely(!skb)) {
  679. netdev_err(bp->dev,
  680. "inconsistent Rx descriptor chain\n");
  681. bp->stats.rx_dropped++;
  682. break;
  683. }
  684. /* now everything is ready for receiving packet */
  685. bp->rx_skbuff[entry] = NULL;
  686. len = ctrl & bp->rx_frm_len_mask;
  687. netdev_vdbg(bp->dev, "gem_rx %u (len %u)\n", entry, len);
  688. skb_put(skb, len);
  689. addr = MACB_BF(RX_WADDR, MACB_BFEXT(RX_WADDR, addr));
  690. dma_unmap_single(&bp->pdev->dev, addr,
  691. bp->rx_buffer_size, DMA_FROM_DEVICE);
  692. skb->protocol = eth_type_trans(skb, bp->dev);
  693. skb_checksum_none_assert(skb);
  694. if (bp->dev->features & NETIF_F_RXCSUM &&
  695. !(bp->dev->flags & IFF_PROMISC) &&
  696. GEM_BFEXT(RX_CSUM, ctrl) & GEM_RX_CSUM_CHECKED_MASK)
  697. skb->ip_summed = CHECKSUM_UNNECESSARY;
  698. bp->stats.rx_packets++;
  699. bp->stats.rx_bytes += skb->len;
  700. #if defined(DEBUG) && defined(VERBOSE_DEBUG)
  701. netdev_vdbg(bp->dev, "received skb of length %u, csum: %08x\n",
  702. skb->len, skb->csum);
  703. print_hex_dump(KERN_DEBUG, " mac: ", DUMP_PREFIX_ADDRESS, 16, 1,
  704. skb_mac_header(skb), 16, true);
  705. print_hex_dump(KERN_DEBUG, "data: ", DUMP_PREFIX_ADDRESS, 16, 1,
  706. skb->data, 32, true);
  707. #endif
  708. netif_receive_skb(skb);
  709. }
  710. gem_rx_refill(bp);
  711. return count;
  712. }
  713. static int macb_rx_frame(struct macb *bp, unsigned int first_frag,
  714. unsigned int last_frag)
  715. {
  716. unsigned int len;
  717. unsigned int frag;
  718. unsigned int offset;
  719. struct sk_buff *skb;
  720. struct macb_dma_desc *desc;
  721. desc = macb_rx_desc(bp, last_frag);
  722. len = desc->ctrl & bp->rx_frm_len_mask;
  723. netdev_vdbg(bp->dev, "macb_rx_frame frags %u - %u (len %u)\n",
  724. macb_rx_ring_wrap(first_frag),
  725. macb_rx_ring_wrap(last_frag), len);
  726. /*
  727. * The ethernet header starts NET_IP_ALIGN bytes into the
  728. * first buffer. Since the header is 14 bytes, this makes the
  729. * payload word-aligned.
  730. *
  731. * Instead of calling skb_reserve(NET_IP_ALIGN), we just copy
  732. * the two padding bytes into the skb so that we avoid hitting
  733. * the slowpath in memcpy(), and pull them off afterwards.
  734. */
  735. skb = netdev_alloc_skb(bp->dev, len + NET_IP_ALIGN);
  736. if (!skb) {
  737. bp->stats.rx_dropped++;
  738. for (frag = first_frag; ; frag++) {
  739. desc = macb_rx_desc(bp, frag);
  740. desc->addr &= ~MACB_BIT(RX_USED);
  741. if (frag == last_frag)
  742. break;
  743. }
  744. /* Make descriptor updates visible to hardware */
  745. wmb();
  746. return 1;
  747. }
  748. offset = 0;
  749. len += NET_IP_ALIGN;
  750. skb_checksum_none_assert(skb);
  751. skb_put(skb, len);
  752. for (frag = first_frag; ; frag++) {
  753. unsigned int frag_len = bp->rx_buffer_size;
  754. if (offset + frag_len > len) {
  755. BUG_ON(frag != last_frag);
  756. frag_len = len - offset;
  757. }
  758. skb_copy_to_linear_data_offset(skb, offset,
  759. macb_rx_buffer(bp, frag), frag_len);
  760. offset += bp->rx_buffer_size;
  761. desc = macb_rx_desc(bp, frag);
  762. desc->addr &= ~MACB_BIT(RX_USED);
  763. if (frag == last_frag)
  764. break;
  765. }
  766. /* Make descriptor updates visible to hardware */
  767. wmb();
  768. __skb_pull(skb, NET_IP_ALIGN);
  769. skb->protocol = eth_type_trans(skb, bp->dev);
  770. bp->stats.rx_packets++;
  771. bp->stats.rx_bytes += skb->len;
  772. netdev_vdbg(bp->dev, "received skb of length %u, csum: %08x\n",
  773. skb->len, skb->csum);
  774. netif_receive_skb(skb);
  775. return 0;
  776. }
  777. static int macb_rx(struct macb *bp, int budget)
  778. {
  779. int received = 0;
  780. unsigned int tail;
  781. int first_frag = -1;
  782. for (tail = bp->rx_tail; budget > 0; tail++) {
  783. struct macb_dma_desc *desc = macb_rx_desc(bp, tail);
  784. u32 addr, ctrl;
  785. /* Make hw descriptor updates visible to CPU */
  786. rmb();
  787. addr = desc->addr;
  788. ctrl = desc->ctrl;
  789. if (!(addr & MACB_BIT(RX_USED)))
  790. break;
  791. if (ctrl & MACB_BIT(RX_SOF)) {
  792. if (first_frag != -1)
  793. discard_partial_frame(bp, first_frag, tail);
  794. first_frag = tail;
  795. }
  796. if (ctrl & MACB_BIT(RX_EOF)) {
  797. int dropped;
  798. BUG_ON(first_frag == -1);
  799. dropped = macb_rx_frame(bp, first_frag, tail);
  800. first_frag = -1;
  801. if (!dropped) {
  802. received++;
  803. budget--;
  804. }
  805. }
  806. }
  807. if (first_frag != -1)
  808. bp->rx_tail = first_frag;
  809. else
  810. bp->rx_tail = tail;
  811. return received;
  812. }
  813. static int macb_poll(struct napi_struct *napi, int budget)
  814. {
  815. struct macb *bp = container_of(napi, struct macb, napi);
  816. int work_done;
  817. u32 status;
  818. status = macb_readl(bp, RSR);
  819. macb_writel(bp, RSR, status);
  820. work_done = 0;
  821. netdev_vdbg(bp->dev, "poll: status = %08lx, budget = %d\n",
  822. (unsigned long)status, budget);
  823. work_done = bp->macbgem_ops.mog_rx(bp, budget);
  824. if (work_done < budget) {
  825. napi_complete(napi);
  826. /* Packets received while interrupts were disabled */
  827. status = macb_readl(bp, RSR);
  828. if (status) {
  829. if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
  830. macb_writel(bp, ISR, MACB_BIT(RCOMP));
  831. napi_reschedule(napi);
  832. } else {
  833. macb_writel(bp, IER, MACB_RX_INT_FLAGS);
  834. }
  835. }
  836. /* TODO: Handle errors */
  837. return work_done;
  838. }
  839. static irqreturn_t macb_interrupt(int irq, void *dev_id)
  840. {
  841. struct macb_queue *queue = dev_id;
  842. struct macb *bp = queue->bp;
  843. struct net_device *dev = bp->dev;
  844. u32 status, ctrl;
  845. status = queue_readl(queue, ISR);
  846. if (unlikely(!status))
  847. return IRQ_NONE;
  848. spin_lock(&bp->lock);
  849. while (status) {
  850. /* close possible race with dev_close */
  851. if (unlikely(!netif_running(dev))) {
  852. queue_writel(queue, IDR, -1);
  853. break;
  854. }
  855. netdev_vdbg(bp->dev, "queue = %u, isr = 0x%08lx\n",
  856. (unsigned int)(queue - bp->queues),
  857. (unsigned long)status);
  858. if (status & MACB_RX_INT_FLAGS) {
  859. /*
  860. * There's no point taking any more interrupts
  861. * until we have processed the buffers. The
  862. * scheduling call may fail if the poll routine
  863. * is already scheduled, so disable interrupts
  864. * now.
  865. */
  866. queue_writel(queue, IDR, MACB_RX_INT_FLAGS);
  867. if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
  868. queue_writel(queue, ISR, MACB_BIT(RCOMP));
  869. if (napi_schedule_prep(&bp->napi)) {
  870. netdev_vdbg(bp->dev, "scheduling RX softirq\n");
  871. __napi_schedule(&bp->napi);
  872. }
  873. }
  874. if (unlikely(status & (MACB_TX_ERR_FLAGS))) {
  875. queue_writel(queue, IDR, MACB_TX_INT_FLAGS);
  876. schedule_work(&queue->tx_error_task);
  877. if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
  878. queue_writel(queue, ISR, MACB_TX_ERR_FLAGS);
  879. break;
  880. }
  881. if (status & MACB_BIT(TCOMP))
  882. macb_tx_interrupt(queue);
  883. /*
  884. * Link change detection isn't possible with RMII, so we'll
  885. * add that if/when we get our hands on a full-blown MII PHY.
  886. */
  887. /* There is a hardware issue under heavy load where DMA can
  888. * stop, this causes endless "used buffer descriptor read"
  889. * interrupts but it can be cleared by re-enabling RX. See
  890. * the at91 manual, section 41.3.1 or the Zynq manual
  891. * section 16.7.4 for details.
  892. */
  893. if (status & MACB_BIT(RXUBR)) {
  894. ctrl = macb_readl(bp, NCR);
  895. macb_writel(bp, NCR, ctrl & ~MACB_BIT(RE));
  896. macb_writel(bp, NCR, ctrl | MACB_BIT(RE));
  897. if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
  898. macb_writel(bp, ISR, MACB_BIT(RXUBR));
  899. }
  900. if (status & MACB_BIT(ISR_ROVR)) {
  901. /* We missed at least one packet */
  902. if (macb_is_gem(bp))
  903. bp->hw_stats.gem.rx_overruns++;
  904. else
  905. bp->hw_stats.macb.rx_overruns++;
  906. if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
  907. queue_writel(queue, ISR, MACB_BIT(ISR_ROVR));
  908. }
  909. if (status & MACB_BIT(HRESP)) {
  910. /*
  911. * TODO: Reset the hardware, and maybe move the
  912. * netdev_err to a lower-priority context as well
  913. * (work queue?)
  914. */
  915. netdev_err(dev, "DMA bus error: HRESP not OK\n");
  916. if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
  917. queue_writel(queue, ISR, MACB_BIT(HRESP));
  918. }
  919. status = queue_readl(queue, ISR);
  920. }
  921. spin_unlock(&bp->lock);
  922. return IRQ_HANDLED;
  923. }
  924. #ifdef CONFIG_NET_POLL_CONTROLLER
  925. /*
  926. * Polling receive - used by netconsole and other diagnostic tools
  927. * to allow network i/o with interrupts disabled.
  928. */
  929. static void macb_poll_controller(struct net_device *dev)
  930. {
  931. struct macb *bp = netdev_priv(dev);
  932. struct macb_queue *queue;
  933. unsigned long flags;
  934. unsigned int q;
  935. local_irq_save(flags);
  936. for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue)
  937. macb_interrupt(dev->irq, queue);
  938. local_irq_restore(flags);
  939. }
  940. #endif
  941. static unsigned int macb_tx_map(struct macb *bp,
  942. struct macb_queue *queue,
  943. struct sk_buff *skb)
  944. {
  945. dma_addr_t mapping;
  946. unsigned int len, entry, i, tx_head = queue->tx_head;
  947. struct macb_tx_skb *tx_skb = NULL;
  948. struct macb_dma_desc *desc;
  949. unsigned int offset, size, count = 0;
  950. unsigned int f, nr_frags = skb_shinfo(skb)->nr_frags;
  951. unsigned int eof = 1;
  952. u32 ctrl;
  953. /* First, map non-paged data */
  954. len = skb_headlen(skb);
  955. offset = 0;
  956. while (len) {
  957. size = min(len, bp->max_tx_length);
  958. entry = macb_tx_ring_wrap(tx_head);
  959. tx_skb = &queue->tx_skb[entry];
  960. mapping = dma_map_single(&bp->pdev->dev,
  961. skb->data + offset,
  962. size, DMA_TO_DEVICE);
  963. if (dma_mapping_error(&bp->pdev->dev, mapping))
  964. goto dma_error;
  965. /* Save info to properly release resources */
  966. tx_skb->skb = NULL;
  967. tx_skb->mapping = mapping;
  968. tx_skb->size = size;
  969. tx_skb->mapped_as_page = false;
  970. len -= size;
  971. offset += size;
  972. count++;
  973. tx_head++;
  974. }
  975. /* Then, map paged data from fragments */
  976. for (f = 0; f < nr_frags; f++) {
  977. const skb_frag_t *frag = &skb_shinfo(skb)->frags[f];
  978. len = skb_frag_size(frag);
  979. offset = 0;
  980. while (len) {
  981. size = min(len, bp->max_tx_length);
  982. entry = macb_tx_ring_wrap(tx_head);
  983. tx_skb = &queue->tx_skb[entry];
  984. mapping = skb_frag_dma_map(&bp->pdev->dev, frag,
  985. offset, size, DMA_TO_DEVICE);
  986. if (dma_mapping_error(&bp->pdev->dev, mapping))
  987. goto dma_error;
  988. /* Save info to properly release resources */
  989. tx_skb->skb = NULL;
  990. tx_skb->mapping = mapping;
  991. tx_skb->size = size;
  992. tx_skb->mapped_as_page = true;
  993. len -= size;
  994. offset += size;
  995. count++;
  996. tx_head++;
  997. }
  998. }
  999. /* Should never happen */
  1000. if (unlikely(tx_skb == NULL)) {
  1001. netdev_err(bp->dev, "BUG! empty skb!\n");
  1002. return 0;
  1003. }
  1004. /* This is the last buffer of the frame: save socket buffer */
  1005. tx_skb->skb = skb;
  1006. /* Update TX ring: update buffer descriptors in reverse order
  1007. * to avoid race condition
  1008. */
  1009. /* Set 'TX_USED' bit in buffer descriptor at tx_head position
  1010. * to set the end of TX queue
  1011. */
  1012. i = tx_head;
  1013. entry = macb_tx_ring_wrap(i);
  1014. ctrl = MACB_BIT(TX_USED);
  1015. desc = &queue->tx_ring[entry];
  1016. desc->ctrl = ctrl;
  1017. do {
  1018. i--;
  1019. entry = macb_tx_ring_wrap(i);
  1020. tx_skb = &queue->tx_skb[entry];
  1021. desc = &queue->tx_ring[entry];
  1022. ctrl = (u32)tx_skb->size;
  1023. if (eof) {
  1024. ctrl |= MACB_BIT(TX_LAST);
  1025. eof = 0;
  1026. }
  1027. if (unlikely(entry == (TX_RING_SIZE - 1)))
  1028. ctrl |= MACB_BIT(TX_WRAP);
  1029. /* Set TX buffer descriptor */
  1030. desc->addr = tx_skb->mapping;
  1031. /* desc->addr must be visible to hardware before clearing
  1032. * 'TX_USED' bit in desc->ctrl.
  1033. */
  1034. wmb();
  1035. desc->ctrl = ctrl;
  1036. } while (i != queue->tx_head);
  1037. queue->tx_head = tx_head;
  1038. return count;
  1039. dma_error:
  1040. netdev_err(bp->dev, "TX DMA map failed\n");
  1041. for (i = queue->tx_head; i != tx_head; i++) {
  1042. tx_skb = macb_tx_skb(queue, i);
  1043. macb_tx_unmap(bp, tx_skb);
  1044. }
  1045. return 0;
  1046. }
  1047. static int macb_start_xmit(struct sk_buff *skb, struct net_device *dev)
  1048. {
  1049. u16 queue_index = skb_get_queue_mapping(skb);
  1050. struct macb *bp = netdev_priv(dev);
  1051. struct macb_queue *queue = &bp->queues[queue_index];
  1052. unsigned long flags;
  1053. unsigned int count, nr_frags, frag_size, f;
  1054. #if defined(DEBUG) && defined(VERBOSE_DEBUG)
  1055. netdev_vdbg(bp->dev,
  1056. "start_xmit: queue %hu len %u head %p data %p tail %p end %p\n",
  1057. queue_index, skb->len, skb->head, skb->data,
  1058. skb_tail_pointer(skb), skb_end_pointer(skb));
  1059. print_hex_dump(KERN_DEBUG, "data: ", DUMP_PREFIX_OFFSET, 16, 1,
  1060. skb->data, 16, true);
  1061. #endif
  1062. /* Count how many TX buffer descriptors are needed to send this
  1063. * socket buffer: skb fragments of jumbo frames may need to be
  1064. * splitted into many buffer descriptors.
  1065. */
  1066. count = DIV_ROUND_UP(skb_headlen(skb), bp->max_tx_length);
  1067. nr_frags = skb_shinfo(skb)->nr_frags;
  1068. for (f = 0; f < nr_frags; f++) {
  1069. frag_size = skb_frag_size(&skb_shinfo(skb)->frags[f]);
  1070. count += DIV_ROUND_UP(frag_size, bp->max_tx_length);
  1071. }
  1072. spin_lock_irqsave(&bp->lock, flags);
  1073. /* This is a hard error, log it. */
  1074. if (CIRC_SPACE(queue->tx_head, queue->tx_tail, TX_RING_SIZE) < count) {
  1075. netif_stop_subqueue(dev, queue_index);
  1076. spin_unlock_irqrestore(&bp->lock, flags);
  1077. netdev_dbg(bp->dev, "tx_head = %u, tx_tail = %u\n",
  1078. queue->tx_head, queue->tx_tail);
  1079. return NETDEV_TX_BUSY;
  1080. }
  1081. /* Map socket buffer for DMA transfer */
  1082. if (!macb_tx_map(bp, queue, skb)) {
  1083. dev_kfree_skb_any(skb);
  1084. goto unlock;
  1085. }
  1086. /* Make newly initialized descriptor visible to hardware */
  1087. wmb();
  1088. skb_tx_timestamp(skb);
  1089. macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(TSTART));
  1090. if (CIRC_SPACE(queue->tx_head, queue->tx_tail, TX_RING_SIZE) < 1)
  1091. netif_stop_subqueue(dev, queue_index);
  1092. unlock:
  1093. spin_unlock_irqrestore(&bp->lock, flags);
  1094. return NETDEV_TX_OK;
  1095. }
  1096. static void macb_init_rx_buffer_size(struct macb *bp, size_t size)
  1097. {
  1098. if (!macb_is_gem(bp)) {
  1099. bp->rx_buffer_size = MACB_RX_BUFFER_SIZE;
  1100. } else {
  1101. bp->rx_buffer_size = size;
  1102. if (bp->rx_buffer_size % RX_BUFFER_MULTIPLE) {
  1103. netdev_dbg(bp->dev,
  1104. "RX buffer must be multiple of %d bytes, expanding\n",
  1105. RX_BUFFER_MULTIPLE);
  1106. bp->rx_buffer_size =
  1107. roundup(bp->rx_buffer_size, RX_BUFFER_MULTIPLE);
  1108. }
  1109. }
  1110. netdev_dbg(bp->dev, "mtu [%u] rx_buffer_size [%Zu]\n",
  1111. bp->dev->mtu, bp->rx_buffer_size);
  1112. }
  1113. static void gem_free_rx_buffers(struct macb *bp)
  1114. {
  1115. struct sk_buff *skb;
  1116. struct macb_dma_desc *desc;
  1117. dma_addr_t addr;
  1118. int i;
  1119. if (!bp->rx_skbuff)
  1120. return;
  1121. for (i = 0; i < RX_RING_SIZE; i++) {
  1122. skb = bp->rx_skbuff[i];
  1123. if (skb == NULL)
  1124. continue;
  1125. desc = &bp->rx_ring[i];
  1126. addr = MACB_BF(RX_WADDR, MACB_BFEXT(RX_WADDR, desc->addr));
  1127. dma_unmap_single(&bp->pdev->dev, addr, bp->rx_buffer_size,
  1128. DMA_FROM_DEVICE);
  1129. dev_kfree_skb_any(skb);
  1130. skb = NULL;
  1131. }
  1132. kfree(bp->rx_skbuff);
  1133. bp->rx_skbuff = NULL;
  1134. }
  1135. static void macb_free_rx_buffers(struct macb *bp)
  1136. {
  1137. if (bp->rx_buffers) {
  1138. dma_free_coherent(&bp->pdev->dev,
  1139. RX_RING_SIZE * bp->rx_buffer_size,
  1140. bp->rx_buffers, bp->rx_buffers_dma);
  1141. bp->rx_buffers = NULL;
  1142. }
  1143. }
  1144. static void macb_free_consistent(struct macb *bp)
  1145. {
  1146. struct macb_queue *queue;
  1147. unsigned int q;
  1148. bp->macbgem_ops.mog_free_rx_buffers(bp);
  1149. if (bp->rx_ring) {
  1150. dma_free_coherent(&bp->pdev->dev, RX_RING_BYTES,
  1151. bp->rx_ring, bp->rx_ring_dma);
  1152. bp->rx_ring = NULL;
  1153. }
  1154. for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
  1155. kfree(queue->tx_skb);
  1156. queue->tx_skb = NULL;
  1157. if (queue->tx_ring) {
  1158. dma_free_coherent(&bp->pdev->dev, TX_RING_BYTES,
  1159. queue->tx_ring, queue->tx_ring_dma);
  1160. queue->tx_ring = NULL;
  1161. }
  1162. }
  1163. }
  1164. static int gem_alloc_rx_buffers(struct macb *bp)
  1165. {
  1166. int size;
  1167. size = RX_RING_SIZE * sizeof(struct sk_buff *);
  1168. bp->rx_skbuff = kzalloc(size, GFP_KERNEL);
  1169. if (!bp->rx_skbuff)
  1170. return -ENOMEM;
  1171. else
  1172. netdev_dbg(bp->dev,
  1173. "Allocated %d RX struct sk_buff entries at %p\n",
  1174. RX_RING_SIZE, bp->rx_skbuff);
  1175. return 0;
  1176. }
  1177. static int macb_alloc_rx_buffers(struct macb *bp)
  1178. {
  1179. int size;
  1180. size = RX_RING_SIZE * bp->rx_buffer_size;
  1181. bp->rx_buffers = dma_alloc_coherent(&bp->pdev->dev, size,
  1182. &bp->rx_buffers_dma, GFP_KERNEL);
  1183. if (!bp->rx_buffers)
  1184. return -ENOMEM;
  1185. else
  1186. netdev_dbg(bp->dev,
  1187. "Allocated RX buffers of %d bytes at %08lx (mapped %p)\n",
  1188. size, (unsigned long)bp->rx_buffers_dma, bp->rx_buffers);
  1189. return 0;
  1190. }
  1191. static int macb_alloc_consistent(struct macb *bp)
  1192. {
  1193. struct macb_queue *queue;
  1194. unsigned int q;
  1195. int size;
  1196. for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
  1197. size = TX_RING_BYTES;
  1198. queue->tx_ring = dma_alloc_coherent(&bp->pdev->dev, size,
  1199. &queue->tx_ring_dma,
  1200. GFP_KERNEL);
  1201. if (!queue->tx_ring)
  1202. goto out_err;
  1203. netdev_dbg(bp->dev,
  1204. "Allocated TX ring for queue %u of %d bytes at %08lx (mapped %p)\n",
  1205. q, size, (unsigned long)queue->tx_ring_dma,
  1206. queue->tx_ring);
  1207. size = TX_RING_SIZE * sizeof(struct macb_tx_skb);
  1208. queue->tx_skb = kmalloc(size, GFP_KERNEL);
  1209. if (!queue->tx_skb)
  1210. goto out_err;
  1211. }
  1212. size = RX_RING_BYTES;
  1213. bp->rx_ring = dma_alloc_coherent(&bp->pdev->dev, size,
  1214. &bp->rx_ring_dma, GFP_KERNEL);
  1215. if (!bp->rx_ring)
  1216. goto out_err;
  1217. netdev_dbg(bp->dev,
  1218. "Allocated RX ring of %d bytes at %08lx (mapped %p)\n",
  1219. size, (unsigned long)bp->rx_ring_dma, bp->rx_ring);
  1220. if (bp->macbgem_ops.mog_alloc_rx_buffers(bp))
  1221. goto out_err;
  1222. return 0;
  1223. out_err:
  1224. macb_free_consistent(bp);
  1225. return -ENOMEM;
  1226. }
  1227. static void gem_init_rings(struct macb *bp)
  1228. {
  1229. struct macb_queue *queue;
  1230. unsigned int q;
  1231. int i;
  1232. for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
  1233. for (i = 0; i < TX_RING_SIZE; i++) {
  1234. queue->tx_ring[i].addr = 0;
  1235. queue->tx_ring[i].ctrl = MACB_BIT(TX_USED);
  1236. }
  1237. queue->tx_ring[TX_RING_SIZE - 1].ctrl |= MACB_BIT(TX_WRAP);
  1238. queue->tx_head = 0;
  1239. queue->tx_tail = 0;
  1240. }
  1241. bp->rx_tail = 0;
  1242. bp->rx_prepared_head = 0;
  1243. gem_rx_refill(bp);
  1244. }
  1245. static void macb_init_rings(struct macb *bp)
  1246. {
  1247. int i;
  1248. dma_addr_t addr;
  1249. addr = bp->rx_buffers_dma;
  1250. for (i = 0; i < RX_RING_SIZE; i++) {
  1251. bp->rx_ring[i].addr = addr;
  1252. bp->rx_ring[i].ctrl = 0;
  1253. addr += bp->rx_buffer_size;
  1254. }
  1255. bp->rx_ring[RX_RING_SIZE - 1].addr |= MACB_BIT(RX_WRAP);
  1256. for (i = 0; i < TX_RING_SIZE; i++) {
  1257. bp->queues[0].tx_ring[i].addr = 0;
  1258. bp->queues[0].tx_ring[i].ctrl = MACB_BIT(TX_USED);
  1259. }
  1260. bp->queues[0].tx_head = 0;
  1261. bp->queues[0].tx_tail = 0;
  1262. bp->queues[0].tx_ring[TX_RING_SIZE - 1].ctrl |= MACB_BIT(TX_WRAP);
  1263. bp->rx_tail = 0;
  1264. }
  1265. static void macb_reset_hw(struct macb *bp)
  1266. {
  1267. struct macb_queue *queue;
  1268. unsigned int q;
  1269. /*
  1270. * Disable RX and TX (XXX: Should we halt the transmission
  1271. * more gracefully?)
  1272. */
  1273. macb_writel(bp, NCR, 0);
  1274. /* Clear the stats registers (XXX: Update stats first?) */
  1275. macb_writel(bp, NCR, MACB_BIT(CLRSTAT));
  1276. /* Clear all status flags */
  1277. macb_writel(bp, TSR, -1);
  1278. macb_writel(bp, RSR, -1);
  1279. /* Disable all interrupts */
  1280. for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
  1281. queue_writel(queue, IDR, -1);
  1282. queue_readl(queue, ISR);
  1283. }
  1284. }
  1285. static u32 gem_mdc_clk_div(struct macb *bp)
  1286. {
  1287. u32 config;
  1288. unsigned long pclk_hz = clk_get_rate(bp->pclk);
  1289. if (pclk_hz <= 20000000)
  1290. config = GEM_BF(CLK, GEM_CLK_DIV8);
  1291. else if (pclk_hz <= 40000000)
  1292. config = GEM_BF(CLK, GEM_CLK_DIV16);
  1293. else if (pclk_hz <= 80000000)
  1294. config = GEM_BF(CLK, GEM_CLK_DIV32);
  1295. else if (pclk_hz <= 120000000)
  1296. config = GEM_BF(CLK, GEM_CLK_DIV48);
  1297. else if (pclk_hz <= 160000000)
  1298. config = GEM_BF(CLK, GEM_CLK_DIV64);
  1299. else
  1300. config = GEM_BF(CLK, GEM_CLK_DIV96);
  1301. return config;
  1302. }
  1303. static u32 macb_mdc_clk_div(struct macb *bp)
  1304. {
  1305. u32 config;
  1306. unsigned long pclk_hz;
  1307. if (macb_is_gem(bp))
  1308. return gem_mdc_clk_div(bp);
  1309. pclk_hz = clk_get_rate(bp->pclk);
  1310. if (pclk_hz <= 20000000)
  1311. config = MACB_BF(CLK, MACB_CLK_DIV8);
  1312. else if (pclk_hz <= 40000000)
  1313. config = MACB_BF(CLK, MACB_CLK_DIV16);
  1314. else if (pclk_hz <= 80000000)
  1315. config = MACB_BF(CLK, MACB_CLK_DIV32);
  1316. else
  1317. config = MACB_BF(CLK, MACB_CLK_DIV64);
  1318. return config;
  1319. }
  1320. /*
  1321. * Get the DMA bus width field of the network configuration register that we
  1322. * should program. We find the width from decoding the design configuration
  1323. * register to find the maximum supported data bus width.
  1324. */
  1325. static u32 macb_dbw(struct macb *bp)
  1326. {
  1327. if (!macb_is_gem(bp))
  1328. return 0;
  1329. switch (GEM_BFEXT(DBWDEF, gem_readl(bp, DCFG1))) {
  1330. case 4:
  1331. return GEM_BF(DBW, GEM_DBW128);
  1332. case 2:
  1333. return GEM_BF(DBW, GEM_DBW64);
  1334. case 1:
  1335. default:
  1336. return GEM_BF(DBW, GEM_DBW32);
  1337. }
  1338. }
  1339. /*
  1340. * Configure the receive DMA engine
  1341. * - use the correct receive buffer size
  1342. * - set best burst length for DMA operations
  1343. * (if not supported by FIFO, it will fallback to default)
  1344. * - set both rx/tx packet buffers to full memory size
  1345. * These are configurable parameters for GEM.
  1346. */
  1347. static void macb_configure_dma(struct macb *bp)
  1348. {
  1349. u32 dmacfg;
  1350. if (macb_is_gem(bp)) {
  1351. dmacfg = gem_readl(bp, DMACFG) & ~GEM_BF(RXBS, -1L);
  1352. dmacfg |= GEM_BF(RXBS, bp->rx_buffer_size / RX_BUFFER_MULTIPLE);
  1353. if (bp->dma_burst_length)
  1354. dmacfg = GEM_BFINS(FBLDO, bp->dma_burst_length, dmacfg);
  1355. dmacfg |= GEM_BIT(TXPBMS) | GEM_BF(RXBMS, -1L);
  1356. dmacfg &= ~GEM_BIT(ENDIA_PKT);
  1357. if (bp->native_io)
  1358. dmacfg &= ~GEM_BIT(ENDIA_DESC);
  1359. else
  1360. dmacfg |= GEM_BIT(ENDIA_DESC); /* CPU in big endian */
  1361. if (bp->dev->features & NETIF_F_HW_CSUM)
  1362. dmacfg |= GEM_BIT(TXCOEN);
  1363. else
  1364. dmacfg &= ~GEM_BIT(TXCOEN);
  1365. netdev_dbg(bp->dev, "Cadence configure DMA with 0x%08x\n",
  1366. dmacfg);
  1367. gem_writel(bp, DMACFG, dmacfg);
  1368. }
  1369. }
  1370. static void macb_init_hw(struct macb *bp)
  1371. {
  1372. struct macb_queue *queue;
  1373. unsigned int q;
  1374. u32 config;
  1375. macb_reset_hw(bp);
  1376. macb_set_hwaddr(bp);
  1377. config = macb_mdc_clk_div(bp);
  1378. config |= MACB_BF(RBOF, NET_IP_ALIGN); /* Make eth data aligned */
  1379. config |= MACB_BIT(PAE); /* PAuse Enable */
  1380. config |= MACB_BIT(DRFCS); /* Discard Rx FCS */
  1381. if (bp->caps & MACB_CAPS_JUMBO)
  1382. config |= MACB_BIT(JFRAME); /* Enable jumbo frames */
  1383. else
  1384. config |= MACB_BIT(BIG); /* Receive oversized frames */
  1385. if (bp->dev->flags & IFF_PROMISC)
  1386. config |= MACB_BIT(CAF); /* Copy All Frames */
  1387. else if (macb_is_gem(bp) && bp->dev->features & NETIF_F_RXCSUM)
  1388. config |= GEM_BIT(RXCOEN);
  1389. if (!(bp->dev->flags & IFF_BROADCAST))
  1390. config |= MACB_BIT(NBC); /* No BroadCast */
  1391. config |= macb_dbw(bp);
  1392. macb_writel(bp, NCFGR, config);
  1393. if ((bp->caps & MACB_CAPS_JUMBO) && bp->jumbo_max_len)
  1394. gem_writel(bp, JML, bp->jumbo_max_len);
  1395. bp->speed = SPEED_10;
  1396. bp->duplex = DUPLEX_HALF;
  1397. bp->rx_frm_len_mask = MACB_RX_FRMLEN_MASK;
  1398. if (bp->caps & MACB_CAPS_JUMBO)
  1399. bp->rx_frm_len_mask = MACB_RX_JFRMLEN_MASK;
  1400. macb_configure_dma(bp);
  1401. /* Initialize TX and RX buffers */
  1402. macb_writel(bp, RBQP, bp->rx_ring_dma);
  1403. for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
  1404. queue_writel(queue, TBQP, queue->tx_ring_dma);
  1405. /* Enable interrupts */
  1406. queue_writel(queue, IER,
  1407. MACB_RX_INT_FLAGS |
  1408. MACB_TX_INT_FLAGS |
  1409. MACB_BIT(HRESP));
  1410. }
  1411. /* Enable TX and RX */
  1412. macb_writel(bp, NCR, MACB_BIT(RE) | MACB_BIT(TE) | MACB_BIT(MPE));
  1413. }
  1414. /*
  1415. * The hash address register is 64 bits long and takes up two
  1416. * locations in the memory map. The least significant bits are stored
  1417. * in EMAC_HSL and the most significant bits in EMAC_HSH.
  1418. *
  1419. * The unicast hash enable and the multicast hash enable bits in the
  1420. * network configuration register enable the reception of hash matched
  1421. * frames. The destination address is reduced to a 6 bit index into
  1422. * the 64 bit hash register using the following hash function. The
  1423. * hash function is an exclusive or of every sixth bit of the
  1424. * destination address.
  1425. *
  1426. * hi[5] = da[5] ^ da[11] ^ da[17] ^ da[23] ^ da[29] ^ da[35] ^ da[41] ^ da[47]
  1427. * hi[4] = da[4] ^ da[10] ^ da[16] ^ da[22] ^ da[28] ^ da[34] ^ da[40] ^ da[46]
  1428. * hi[3] = da[3] ^ da[09] ^ da[15] ^ da[21] ^ da[27] ^ da[33] ^ da[39] ^ da[45]
  1429. * hi[2] = da[2] ^ da[08] ^ da[14] ^ da[20] ^ da[26] ^ da[32] ^ da[38] ^ da[44]
  1430. * hi[1] = da[1] ^ da[07] ^ da[13] ^ da[19] ^ da[25] ^ da[31] ^ da[37] ^ da[43]
  1431. * hi[0] = da[0] ^ da[06] ^ da[12] ^ da[18] ^ da[24] ^ da[30] ^ da[36] ^ da[42]
  1432. *
  1433. * da[0] represents the least significant bit of the first byte
  1434. * received, that is, the multicast/unicast indicator, and da[47]
  1435. * represents the most significant bit of the last byte received. If
  1436. * the hash index, hi[n], points to a bit that is set in the hash
  1437. * register then the frame will be matched according to whether the
  1438. * frame is multicast or unicast. A multicast match will be signalled
  1439. * if the multicast hash enable bit is set, da[0] is 1 and the hash
  1440. * index points to a bit set in the hash register. A unicast match
  1441. * will be signalled if the unicast hash enable bit is set, da[0] is 0
  1442. * and the hash index points to a bit set in the hash register. To
  1443. * receive all multicast frames, the hash register should be set with
  1444. * all ones and the multicast hash enable bit should be set in the
  1445. * network configuration register.
  1446. */
  1447. static inline int hash_bit_value(int bitnr, __u8 *addr)
  1448. {
  1449. if (addr[bitnr / 8] & (1 << (bitnr % 8)))
  1450. return 1;
  1451. return 0;
  1452. }
  1453. /*
  1454. * Return the hash index value for the specified address.
  1455. */
  1456. static int hash_get_index(__u8 *addr)
  1457. {
  1458. int i, j, bitval;
  1459. int hash_index = 0;
  1460. for (j = 0; j < 6; j++) {
  1461. for (i = 0, bitval = 0; i < 8; i++)
  1462. bitval ^= hash_bit_value(i * 6 + j, addr);
  1463. hash_index |= (bitval << j);
  1464. }
  1465. return hash_index;
  1466. }
  1467. /*
  1468. * Add multicast addresses to the internal multicast-hash table.
  1469. */
  1470. static void macb_sethashtable(struct net_device *dev)
  1471. {
  1472. struct netdev_hw_addr *ha;
  1473. unsigned long mc_filter[2];
  1474. unsigned int bitnr;
  1475. struct macb *bp = netdev_priv(dev);
  1476. mc_filter[0] = mc_filter[1] = 0;
  1477. netdev_for_each_mc_addr(ha, dev) {
  1478. bitnr = hash_get_index(ha->addr);
  1479. mc_filter[bitnr >> 5] |= 1 << (bitnr & 31);
  1480. }
  1481. macb_or_gem_writel(bp, HRB, mc_filter[0]);
  1482. macb_or_gem_writel(bp, HRT, mc_filter[1]);
  1483. }
  1484. /*
  1485. * Enable/Disable promiscuous and multicast modes.
  1486. */
  1487. static void macb_set_rx_mode(struct net_device *dev)
  1488. {
  1489. unsigned long cfg;
  1490. struct macb *bp = netdev_priv(dev);
  1491. cfg = macb_readl(bp, NCFGR);
  1492. if (dev->flags & IFF_PROMISC) {
  1493. /* Enable promiscuous mode */
  1494. cfg |= MACB_BIT(CAF);
  1495. /* Disable RX checksum offload */
  1496. if (macb_is_gem(bp))
  1497. cfg &= ~GEM_BIT(RXCOEN);
  1498. } else {
  1499. /* Disable promiscuous mode */
  1500. cfg &= ~MACB_BIT(CAF);
  1501. /* Enable RX checksum offload only if requested */
  1502. if (macb_is_gem(bp) && dev->features & NETIF_F_RXCSUM)
  1503. cfg |= GEM_BIT(RXCOEN);
  1504. }
  1505. if (dev->flags & IFF_ALLMULTI) {
  1506. /* Enable all multicast mode */
  1507. macb_or_gem_writel(bp, HRB, -1);
  1508. macb_or_gem_writel(bp, HRT, -1);
  1509. cfg |= MACB_BIT(NCFGR_MTI);
  1510. } else if (!netdev_mc_empty(dev)) {
  1511. /* Enable specific multicasts */
  1512. macb_sethashtable(dev);
  1513. cfg |= MACB_BIT(NCFGR_MTI);
  1514. } else if (dev->flags & (~IFF_ALLMULTI)) {
  1515. /* Disable all multicast mode */
  1516. macb_or_gem_writel(bp, HRB, 0);
  1517. macb_or_gem_writel(bp, HRT, 0);
  1518. cfg &= ~MACB_BIT(NCFGR_MTI);
  1519. }
  1520. macb_writel(bp, NCFGR, cfg);
  1521. }
  1522. static int macb_open(struct net_device *dev)
  1523. {
  1524. struct macb *bp = netdev_priv(dev);
  1525. size_t bufsz = dev->mtu + ETH_HLEN + ETH_FCS_LEN + NET_IP_ALIGN;
  1526. int err;
  1527. netdev_dbg(bp->dev, "open\n");
  1528. /* carrier starts down */
  1529. netif_carrier_off(dev);
  1530. /* if the phy is not yet register, retry later*/
  1531. if (!bp->phy_dev)
  1532. return -EAGAIN;
  1533. /* RX buffers initialization */
  1534. macb_init_rx_buffer_size(bp, bufsz);
  1535. err = macb_alloc_consistent(bp);
  1536. if (err) {
  1537. netdev_err(dev, "Unable to allocate DMA memory (error %d)\n",
  1538. err);
  1539. return err;
  1540. }
  1541. napi_enable(&bp->napi);
  1542. bp->macbgem_ops.mog_init_rings(bp);
  1543. macb_init_hw(bp);
  1544. /* schedule a link state check */
  1545. phy_start(bp->phy_dev);
  1546. netif_tx_start_all_queues(dev);
  1547. return 0;
  1548. }
  1549. static int macb_close(struct net_device *dev)
  1550. {
  1551. struct macb *bp = netdev_priv(dev);
  1552. unsigned long flags;
  1553. netif_tx_stop_all_queues(dev);
  1554. napi_disable(&bp->napi);
  1555. if (bp->phy_dev)
  1556. phy_stop(bp->phy_dev);
  1557. spin_lock_irqsave(&bp->lock, flags);
  1558. macb_reset_hw(bp);
  1559. netif_carrier_off(dev);
  1560. spin_unlock_irqrestore(&bp->lock, flags);
  1561. macb_free_consistent(bp);
  1562. return 0;
  1563. }
  1564. static int macb_change_mtu(struct net_device *dev, int new_mtu)
  1565. {
  1566. struct macb *bp = netdev_priv(dev);
  1567. u32 max_mtu;
  1568. if (netif_running(dev))
  1569. return -EBUSY;
  1570. max_mtu = ETH_DATA_LEN;
  1571. if (bp->caps & MACB_CAPS_JUMBO)
  1572. max_mtu = gem_readl(bp, JML) - ETH_HLEN - ETH_FCS_LEN;
  1573. if ((new_mtu > max_mtu) || (new_mtu < GEM_MTU_MIN_SIZE))
  1574. return -EINVAL;
  1575. dev->mtu = new_mtu;
  1576. return 0;
  1577. }
  1578. static void gem_update_stats(struct macb *bp)
  1579. {
  1580. unsigned int i;
  1581. u32 *p = &bp->hw_stats.gem.tx_octets_31_0;
  1582. for (i = 0; i < GEM_STATS_LEN; ++i, ++p) {
  1583. u32 offset = gem_statistics[i].offset;
  1584. u64 val = bp->macb_reg_readl(bp, offset);
  1585. bp->ethtool_stats[i] += val;
  1586. *p += val;
  1587. if (offset == GEM_OCTTXL || offset == GEM_OCTRXL) {
  1588. /* Add GEM_OCTTXH, GEM_OCTRXH */
  1589. val = bp->macb_reg_readl(bp, offset + 4);
  1590. bp->ethtool_stats[i] += ((u64)val) << 32;
  1591. *(++p) += val;
  1592. }
  1593. }
  1594. }
  1595. static struct net_device_stats *gem_get_stats(struct macb *bp)
  1596. {
  1597. struct gem_stats *hwstat = &bp->hw_stats.gem;
  1598. struct net_device_stats *nstat = &bp->stats;
  1599. gem_update_stats(bp);
  1600. nstat->rx_errors = (hwstat->rx_frame_check_sequence_errors +
  1601. hwstat->rx_alignment_errors +
  1602. hwstat->rx_resource_errors +
  1603. hwstat->rx_overruns +
  1604. hwstat->rx_oversize_frames +
  1605. hwstat->rx_jabbers +
  1606. hwstat->rx_undersized_frames +
  1607. hwstat->rx_length_field_frame_errors);
  1608. nstat->tx_errors = (hwstat->tx_late_collisions +
  1609. hwstat->tx_excessive_collisions +
  1610. hwstat->tx_underrun +
  1611. hwstat->tx_carrier_sense_errors);
  1612. nstat->multicast = hwstat->rx_multicast_frames;
  1613. nstat->collisions = (hwstat->tx_single_collision_frames +
  1614. hwstat->tx_multiple_collision_frames +
  1615. hwstat->tx_excessive_collisions);
  1616. nstat->rx_length_errors = (hwstat->rx_oversize_frames +
  1617. hwstat->rx_jabbers +
  1618. hwstat->rx_undersized_frames +
  1619. hwstat->rx_length_field_frame_errors);
  1620. nstat->rx_over_errors = hwstat->rx_resource_errors;
  1621. nstat->rx_crc_errors = hwstat->rx_frame_check_sequence_errors;
  1622. nstat->rx_frame_errors = hwstat->rx_alignment_errors;
  1623. nstat->rx_fifo_errors = hwstat->rx_overruns;
  1624. nstat->tx_aborted_errors = hwstat->tx_excessive_collisions;
  1625. nstat->tx_carrier_errors = hwstat->tx_carrier_sense_errors;
  1626. nstat->tx_fifo_errors = hwstat->tx_underrun;
  1627. return nstat;
  1628. }
  1629. static void gem_get_ethtool_stats(struct net_device *dev,
  1630. struct ethtool_stats *stats, u64 *data)
  1631. {
  1632. struct macb *bp;
  1633. bp = netdev_priv(dev);
  1634. gem_update_stats(bp);
  1635. memcpy(data, &bp->ethtool_stats, sizeof(u64) * GEM_STATS_LEN);
  1636. }
  1637. static int gem_get_sset_count(struct net_device *dev, int sset)
  1638. {
  1639. switch (sset) {
  1640. case ETH_SS_STATS:
  1641. return GEM_STATS_LEN;
  1642. default:
  1643. return -EOPNOTSUPP;
  1644. }
  1645. }
  1646. static void gem_get_ethtool_strings(struct net_device *dev, u32 sset, u8 *p)
  1647. {
  1648. unsigned int i;
  1649. switch (sset) {
  1650. case ETH_SS_STATS:
  1651. for (i = 0; i < GEM_STATS_LEN; i++, p += ETH_GSTRING_LEN)
  1652. memcpy(p, gem_statistics[i].stat_string,
  1653. ETH_GSTRING_LEN);
  1654. break;
  1655. }
  1656. }
  1657. static struct net_device_stats *macb_get_stats(struct net_device *dev)
  1658. {
  1659. struct macb *bp = netdev_priv(dev);
  1660. struct net_device_stats *nstat = &bp->stats;
  1661. struct macb_stats *hwstat = &bp->hw_stats.macb;
  1662. if (macb_is_gem(bp))
  1663. return gem_get_stats(bp);
  1664. /* read stats from hardware */
  1665. macb_update_stats(bp);
  1666. /* Convert HW stats into netdevice stats */
  1667. nstat->rx_errors = (hwstat->rx_fcs_errors +
  1668. hwstat->rx_align_errors +
  1669. hwstat->rx_resource_errors +
  1670. hwstat->rx_overruns +
  1671. hwstat->rx_oversize_pkts +
  1672. hwstat->rx_jabbers +
  1673. hwstat->rx_undersize_pkts +
  1674. hwstat->rx_length_mismatch);
  1675. nstat->tx_errors = (hwstat->tx_late_cols +
  1676. hwstat->tx_excessive_cols +
  1677. hwstat->tx_underruns +
  1678. hwstat->tx_carrier_errors +
  1679. hwstat->sqe_test_errors);
  1680. nstat->collisions = (hwstat->tx_single_cols +
  1681. hwstat->tx_multiple_cols +
  1682. hwstat->tx_excessive_cols);
  1683. nstat->rx_length_errors = (hwstat->rx_oversize_pkts +
  1684. hwstat->rx_jabbers +
  1685. hwstat->rx_undersize_pkts +
  1686. hwstat->rx_length_mismatch);
  1687. nstat->rx_over_errors = hwstat->rx_resource_errors +
  1688. hwstat->rx_overruns;
  1689. nstat->rx_crc_errors = hwstat->rx_fcs_errors;
  1690. nstat->rx_frame_errors = hwstat->rx_align_errors;
  1691. nstat->rx_fifo_errors = hwstat->rx_overruns;
  1692. /* XXX: What does "missed" mean? */
  1693. nstat->tx_aborted_errors = hwstat->tx_excessive_cols;
  1694. nstat->tx_carrier_errors = hwstat->tx_carrier_errors;
  1695. nstat->tx_fifo_errors = hwstat->tx_underruns;
  1696. /* Don't know about heartbeat or window errors... */
  1697. return nstat;
  1698. }
  1699. static int macb_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  1700. {
  1701. struct macb *bp = netdev_priv(dev);
  1702. struct phy_device *phydev = bp->phy_dev;
  1703. if (!phydev)
  1704. return -ENODEV;
  1705. return phy_ethtool_gset(phydev, cmd);
  1706. }
  1707. static int macb_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  1708. {
  1709. struct macb *bp = netdev_priv(dev);
  1710. struct phy_device *phydev = bp->phy_dev;
  1711. if (!phydev)
  1712. return -ENODEV;
  1713. return phy_ethtool_sset(phydev, cmd);
  1714. }
  1715. static int macb_get_regs_len(struct net_device *netdev)
  1716. {
  1717. return MACB_GREGS_NBR * sizeof(u32);
  1718. }
  1719. static void macb_get_regs(struct net_device *dev, struct ethtool_regs *regs,
  1720. void *p)
  1721. {
  1722. struct macb *bp = netdev_priv(dev);
  1723. unsigned int tail, head;
  1724. u32 *regs_buff = p;
  1725. regs->version = (macb_readl(bp, MID) & ((1 << MACB_REV_SIZE) - 1))
  1726. | MACB_GREGS_VERSION;
  1727. tail = macb_tx_ring_wrap(bp->queues[0].tx_tail);
  1728. head = macb_tx_ring_wrap(bp->queues[0].tx_head);
  1729. regs_buff[0] = macb_readl(bp, NCR);
  1730. regs_buff[1] = macb_or_gem_readl(bp, NCFGR);
  1731. regs_buff[2] = macb_readl(bp, NSR);
  1732. regs_buff[3] = macb_readl(bp, TSR);
  1733. regs_buff[4] = macb_readl(bp, RBQP);
  1734. regs_buff[5] = macb_readl(bp, TBQP);
  1735. regs_buff[6] = macb_readl(bp, RSR);
  1736. regs_buff[7] = macb_readl(bp, IMR);
  1737. regs_buff[8] = tail;
  1738. regs_buff[9] = head;
  1739. regs_buff[10] = macb_tx_dma(&bp->queues[0], tail);
  1740. regs_buff[11] = macb_tx_dma(&bp->queues[0], head);
  1741. regs_buff[12] = macb_or_gem_readl(bp, USRIO);
  1742. if (macb_is_gem(bp)) {
  1743. regs_buff[13] = gem_readl(bp, DMACFG);
  1744. }
  1745. }
  1746. static const struct ethtool_ops macb_ethtool_ops = {
  1747. .get_settings = macb_get_settings,
  1748. .set_settings = macb_set_settings,
  1749. .get_regs_len = macb_get_regs_len,
  1750. .get_regs = macb_get_regs,
  1751. .get_link = ethtool_op_get_link,
  1752. .get_ts_info = ethtool_op_get_ts_info,
  1753. };
  1754. static const struct ethtool_ops gem_ethtool_ops = {
  1755. .get_settings = macb_get_settings,
  1756. .set_settings = macb_set_settings,
  1757. .get_regs_len = macb_get_regs_len,
  1758. .get_regs = macb_get_regs,
  1759. .get_link = ethtool_op_get_link,
  1760. .get_ts_info = ethtool_op_get_ts_info,
  1761. .get_ethtool_stats = gem_get_ethtool_stats,
  1762. .get_strings = gem_get_ethtool_strings,
  1763. .get_sset_count = gem_get_sset_count,
  1764. };
  1765. static int macb_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
  1766. {
  1767. struct macb *bp = netdev_priv(dev);
  1768. struct phy_device *phydev = bp->phy_dev;
  1769. if (!netif_running(dev))
  1770. return -EINVAL;
  1771. if (!phydev)
  1772. return -ENODEV;
  1773. return phy_mii_ioctl(phydev, rq, cmd);
  1774. }
  1775. static int macb_set_features(struct net_device *netdev,
  1776. netdev_features_t features)
  1777. {
  1778. struct macb *bp = netdev_priv(netdev);
  1779. netdev_features_t changed = features ^ netdev->features;
  1780. /* TX checksum offload */
  1781. if ((changed & NETIF_F_HW_CSUM) && macb_is_gem(bp)) {
  1782. u32 dmacfg;
  1783. dmacfg = gem_readl(bp, DMACFG);
  1784. if (features & NETIF_F_HW_CSUM)
  1785. dmacfg |= GEM_BIT(TXCOEN);
  1786. else
  1787. dmacfg &= ~GEM_BIT(TXCOEN);
  1788. gem_writel(bp, DMACFG, dmacfg);
  1789. }
  1790. /* RX checksum offload */
  1791. if ((changed & NETIF_F_RXCSUM) && macb_is_gem(bp)) {
  1792. u32 netcfg;
  1793. netcfg = gem_readl(bp, NCFGR);
  1794. if (features & NETIF_F_RXCSUM &&
  1795. !(netdev->flags & IFF_PROMISC))
  1796. netcfg |= GEM_BIT(RXCOEN);
  1797. else
  1798. netcfg &= ~GEM_BIT(RXCOEN);
  1799. gem_writel(bp, NCFGR, netcfg);
  1800. }
  1801. return 0;
  1802. }
  1803. static const struct net_device_ops macb_netdev_ops = {
  1804. .ndo_open = macb_open,
  1805. .ndo_stop = macb_close,
  1806. .ndo_start_xmit = macb_start_xmit,
  1807. .ndo_set_rx_mode = macb_set_rx_mode,
  1808. .ndo_get_stats = macb_get_stats,
  1809. .ndo_do_ioctl = macb_ioctl,
  1810. .ndo_validate_addr = eth_validate_addr,
  1811. .ndo_change_mtu = macb_change_mtu,
  1812. .ndo_set_mac_address = eth_mac_addr,
  1813. #ifdef CONFIG_NET_POLL_CONTROLLER
  1814. .ndo_poll_controller = macb_poll_controller,
  1815. #endif
  1816. .ndo_set_features = macb_set_features,
  1817. };
  1818. /*
  1819. * Configure peripheral capabilities according to device tree
  1820. * and integration options used
  1821. */
  1822. static void macb_configure_caps(struct macb *bp, const struct macb_config *dt_conf)
  1823. {
  1824. u32 dcfg;
  1825. if (dt_conf)
  1826. bp->caps = dt_conf->caps;
  1827. if (hw_is_gem(bp->regs, bp->native_io)) {
  1828. bp->caps |= MACB_CAPS_MACB_IS_GEM;
  1829. dcfg = gem_readl(bp, DCFG1);
  1830. if (GEM_BFEXT(IRQCOR, dcfg) == 0)
  1831. bp->caps |= MACB_CAPS_ISR_CLEAR_ON_WRITE;
  1832. dcfg = gem_readl(bp, DCFG2);
  1833. if ((dcfg & (GEM_BIT(RX_PKT_BUFF) | GEM_BIT(TX_PKT_BUFF))) == 0)
  1834. bp->caps |= MACB_CAPS_FIFO_MODE;
  1835. }
  1836. dev_dbg(&bp->pdev->dev, "Cadence caps 0x%08x\n", bp->caps);
  1837. }
  1838. static void macb_probe_queues(void __iomem *mem,
  1839. bool native_io,
  1840. unsigned int *queue_mask,
  1841. unsigned int *num_queues)
  1842. {
  1843. unsigned int hw_q;
  1844. *queue_mask = 0x1;
  1845. *num_queues = 1;
  1846. /* is it macb or gem ?
  1847. *
  1848. * We need to read directly from the hardware here because
  1849. * we are early in the probe process and don't have the
  1850. * MACB_CAPS_MACB_IS_GEM flag positioned
  1851. */
  1852. if (!hw_is_gem(mem, native_io))
  1853. return;
  1854. /* bit 0 is never set but queue 0 always exists */
  1855. *queue_mask = readl_relaxed(mem + GEM_DCFG6) & 0xff;
  1856. *queue_mask |= 0x1;
  1857. for (hw_q = 1; hw_q < MACB_MAX_QUEUES; ++hw_q)
  1858. if (*queue_mask & (1 << hw_q))
  1859. (*num_queues)++;
  1860. }
  1861. static int macb_clk_init(struct platform_device *pdev, struct clk **pclk,
  1862. struct clk **hclk, struct clk **tx_clk)
  1863. {
  1864. int err;
  1865. *pclk = devm_clk_get(&pdev->dev, "pclk");
  1866. if (IS_ERR(*pclk)) {
  1867. err = PTR_ERR(*pclk);
  1868. dev_err(&pdev->dev, "failed to get macb_clk (%u)\n", err);
  1869. return err;
  1870. }
  1871. *hclk = devm_clk_get(&pdev->dev, "hclk");
  1872. if (IS_ERR(*hclk)) {
  1873. err = PTR_ERR(*hclk);
  1874. dev_err(&pdev->dev, "failed to get hclk (%u)\n", err);
  1875. return err;
  1876. }
  1877. *tx_clk = devm_clk_get(&pdev->dev, "tx_clk");
  1878. if (IS_ERR(*tx_clk))
  1879. *tx_clk = NULL;
  1880. err = clk_prepare_enable(*pclk);
  1881. if (err) {
  1882. dev_err(&pdev->dev, "failed to enable pclk (%u)\n", err);
  1883. return err;
  1884. }
  1885. err = clk_prepare_enable(*hclk);
  1886. if (err) {
  1887. dev_err(&pdev->dev, "failed to enable hclk (%u)\n", err);
  1888. goto err_disable_pclk;
  1889. }
  1890. err = clk_prepare_enable(*tx_clk);
  1891. if (err) {
  1892. dev_err(&pdev->dev, "failed to enable tx_clk (%u)\n", err);
  1893. goto err_disable_hclk;
  1894. }
  1895. return 0;
  1896. err_disable_hclk:
  1897. clk_disable_unprepare(*hclk);
  1898. err_disable_pclk:
  1899. clk_disable_unprepare(*pclk);
  1900. return err;
  1901. }
  1902. static int macb_init(struct platform_device *pdev)
  1903. {
  1904. struct net_device *dev = platform_get_drvdata(pdev);
  1905. unsigned int hw_q, q;
  1906. struct macb *bp = netdev_priv(dev);
  1907. struct macb_queue *queue;
  1908. int err;
  1909. u32 val;
  1910. /* set the queue register mapping once for all: queue0 has a special
  1911. * register mapping but we don't want to test the queue index then
  1912. * compute the corresponding register offset at run time.
  1913. */
  1914. for (hw_q = 0, q = 0; hw_q < MACB_MAX_QUEUES; ++hw_q) {
  1915. if (!(bp->queue_mask & (1 << hw_q)))
  1916. continue;
  1917. queue = &bp->queues[q];
  1918. queue->bp = bp;
  1919. if (hw_q) {
  1920. queue->ISR = GEM_ISR(hw_q - 1);
  1921. queue->IER = GEM_IER(hw_q - 1);
  1922. queue->IDR = GEM_IDR(hw_q - 1);
  1923. queue->IMR = GEM_IMR(hw_q - 1);
  1924. queue->TBQP = GEM_TBQP(hw_q - 1);
  1925. } else {
  1926. /* queue0 uses legacy registers */
  1927. queue->ISR = MACB_ISR;
  1928. queue->IER = MACB_IER;
  1929. queue->IDR = MACB_IDR;
  1930. queue->IMR = MACB_IMR;
  1931. queue->TBQP = MACB_TBQP;
  1932. }
  1933. /* get irq: here we use the linux queue index, not the hardware
  1934. * queue index. the queue irq definitions in the device tree
  1935. * must remove the optional gaps that could exist in the
  1936. * hardware queue mask.
  1937. */
  1938. queue->irq = platform_get_irq(pdev, q);
  1939. err = devm_request_irq(&pdev->dev, queue->irq, macb_interrupt,
  1940. IRQF_SHARED, dev->name, queue);
  1941. if (err) {
  1942. dev_err(&pdev->dev,
  1943. "Unable to request IRQ %d (error %d)\n",
  1944. queue->irq, err);
  1945. return err;
  1946. }
  1947. INIT_WORK(&queue->tx_error_task, macb_tx_error_task);
  1948. q++;
  1949. }
  1950. dev->netdev_ops = &macb_netdev_ops;
  1951. netif_napi_add(dev, &bp->napi, macb_poll, 64);
  1952. /* setup appropriated routines according to adapter type */
  1953. if (macb_is_gem(bp)) {
  1954. bp->max_tx_length = GEM_MAX_TX_LEN;
  1955. bp->macbgem_ops.mog_alloc_rx_buffers = gem_alloc_rx_buffers;
  1956. bp->macbgem_ops.mog_free_rx_buffers = gem_free_rx_buffers;
  1957. bp->macbgem_ops.mog_init_rings = gem_init_rings;
  1958. bp->macbgem_ops.mog_rx = gem_rx;
  1959. dev->ethtool_ops = &gem_ethtool_ops;
  1960. } else {
  1961. bp->max_tx_length = MACB_MAX_TX_LEN;
  1962. bp->macbgem_ops.mog_alloc_rx_buffers = macb_alloc_rx_buffers;
  1963. bp->macbgem_ops.mog_free_rx_buffers = macb_free_rx_buffers;
  1964. bp->macbgem_ops.mog_init_rings = macb_init_rings;
  1965. bp->macbgem_ops.mog_rx = macb_rx;
  1966. dev->ethtool_ops = &macb_ethtool_ops;
  1967. }
  1968. /* Set features */
  1969. dev->hw_features = NETIF_F_SG;
  1970. /* Checksum offload is only available on gem with packet buffer */
  1971. if (macb_is_gem(bp) && !(bp->caps & MACB_CAPS_FIFO_MODE))
  1972. dev->hw_features |= NETIF_F_HW_CSUM | NETIF_F_RXCSUM;
  1973. if (bp->caps & MACB_CAPS_SG_DISABLED)
  1974. dev->hw_features &= ~NETIF_F_SG;
  1975. dev->features = dev->hw_features;
  1976. val = 0;
  1977. if (bp->phy_interface == PHY_INTERFACE_MODE_RGMII)
  1978. val = GEM_BIT(RGMII);
  1979. else if (bp->phy_interface == PHY_INTERFACE_MODE_RMII &&
  1980. (bp->caps & MACB_CAPS_USRIO_DEFAULT_IS_MII))
  1981. val = MACB_BIT(RMII);
  1982. else if (!(bp->caps & MACB_CAPS_USRIO_DEFAULT_IS_MII))
  1983. val = MACB_BIT(MII);
  1984. if (bp->caps & MACB_CAPS_USRIO_HAS_CLKEN)
  1985. val |= MACB_BIT(CLKEN);
  1986. macb_or_gem_writel(bp, USRIO, val);
  1987. /* Set MII management clock divider */
  1988. val = macb_mdc_clk_div(bp);
  1989. val |= macb_dbw(bp);
  1990. macb_writel(bp, NCFGR, val);
  1991. return 0;
  1992. }
  1993. #if defined(CONFIG_OF)
  1994. /* 1518 rounded up */
  1995. #define AT91ETHER_MAX_RBUFF_SZ 0x600
  1996. /* max number of receive buffers */
  1997. #define AT91ETHER_MAX_RX_DESCR 9
  1998. /* Initialize and start the Receiver and Transmit subsystems */
  1999. static int at91ether_start(struct net_device *dev)
  2000. {
  2001. struct macb *lp = netdev_priv(dev);
  2002. dma_addr_t addr;
  2003. u32 ctl;
  2004. int i;
  2005. lp->rx_ring = dma_alloc_coherent(&lp->pdev->dev,
  2006. (AT91ETHER_MAX_RX_DESCR *
  2007. sizeof(struct macb_dma_desc)),
  2008. &lp->rx_ring_dma, GFP_KERNEL);
  2009. if (!lp->rx_ring)
  2010. return -ENOMEM;
  2011. lp->rx_buffers = dma_alloc_coherent(&lp->pdev->dev,
  2012. AT91ETHER_MAX_RX_DESCR *
  2013. AT91ETHER_MAX_RBUFF_SZ,
  2014. &lp->rx_buffers_dma, GFP_KERNEL);
  2015. if (!lp->rx_buffers) {
  2016. dma_free_coherent(&lp->pdev->dev,
  2017. AT91ETHER_MAX_RX_DESCR *
  2018. sizeof(struct macb_dma_desc),
  2019. lp->rx_ring, lp->rx_ring_dma);
  2020. lp->rx_ring = NULL;
  2021. return -ENOMEM;
  2022. }
  2023. addr = lp->rx_buffers_dma;
  2024. for (i = 0; i < AT91ETHER_MAX_RX_DESCR; i++) {
  2025. lp->rx_ring[i].addr = addr;
  2026. lp->rx_ring[i].ctrl = 0;
  2027. addr += AT91ETHER_MAX_RBUFF_SZ;
  2028. }
  2029. /* Set the Wrap bit on the last descriptor */
  2030. lp->rx_ring[AT91ETHER_MAX_RX_DESCR - 1].addr |= MACB_BIT(RX_WRAP);
  2031. /* Reset buffer index */
  2032. lp->rx_tail = 0;
  2033. /* Program address of descriptor list in Rx Buffer Queue register */
  2034. macb_writel(lp, RBQP, lp->rx_ring_dma);
  2035. /* Enable Receive and Transmit */
  2036. ctl = macb_readl(lp, NCR);
  2037. macb_writel(lp, NCR, ctl | MACB_BIT(RE) | MACB_BIT(TE));
  2038. return 0;
  2039. }
  2040. /* Open the ethernet interface */
  2041. static int at91ether_open(struct net_device *dev)
  2042. {
  2043. struct macb *lp = netdev_priv(dev);
  2044. u32 ctl;
  2045. int ret;
  2046. /* Clear internal statistics */
  2047. ctl = macb_readl(lp, NCR);
  2048. macb_writel(lp, NCR, ctl | MACB_BIT(CLRSTAT));
  2049. macb_set_hwaddr(lp);
  2050. ret = at91ether_start(dev);
  2051. if (ret)
  2052. return ret;
  2053. /* Enable MAC interrupts */
  2054. macb_writel(lp, IER, MACB_BIT(RCOMP) |
  2055. MACB_BIT(RXUBR) |
  2056. MACB_BIT(ISR_TUND) |
  2057. MACB_BIT(ISR_RLE) |
  2058. MACB_BIT(TCOMP) |
  2059. MACB_BIT(ISR_ROVR) |
  2060. MACB_BIT(HRESP));
  2061. /* schedule a link state check */
  2062. phy_start(lp->phy_dev);
  2063. netif_start_queue(dev);
  2064. return 0;
  2065. }
  2066. /* Close the interface */
  2067. static int at91ether_close(struct net_device *dev)
  2068. {
  2069. struct macb *lp = netdev_priv(dev);
  2070. u32 ctl;
  2071. /* Disable Receiver and Transmitter */
  2072. ctl = macb_readl(lp, NCR);
  2073. macb_writel(lp, NCR, ctl & ~(MACB_BIT(TE) | MACB_BIT(RE)));
  2074. /* Disable MAC interrupts */
  2075. macb_writel(lp, IDR, MACB_BIT(RCOMP) |
  2076. MACB_BIT(RXUBR) |
  2077. MACB_BIT(ISR_TUND) |
  2078. MACB_BIT(ISR_RLE) |
  2079. MACB_BIT(TCOMP) |
  2080. MACB_BIT(ISR_ROVR) |
  2081. MACB_BIT(HRESP));
  2082. netif_stop_queue(dev);
  2083. dma_free_coherent(&lp->pdev->dev,
  2084. AT91ETHER_MAX_RX_DESCR *
  2085. sizeof(struct macb_dma_desc),
  2086. lp->rx_ring, lp->rx_ring_dma);
  2087. lp->rx_ring = NULL;
  2088. dma_free_coherent(&lp->pdev->dev,
  2089. AT91ETHER_MAX_RX_DESCR * AT91ETHER_MAX_RBUFF_SZ,
  2090. lp->rx_buffers, lp->rx_buffers_dma);
  2091. lp->rx_buffers = NULL;
  2092. return 0;
  2093. }
  2094. /* Transmit packet */
  2095. static int at91ether_start_xmit(struct sk_buff *skb, struct net_device *dev)
  2096. {
  2097. struct macb *lp = netdev_priv(dev);
  2098. if (macb_readl(lp, TSR) & MACB_BIT(RM9200_BNQ)) {
  2099. netif_stop_queue(dev);
  2100. /* Store packet information (to free when Tx completed) */
  2101. lp->skb = skb;
  2102. lp->skb_length = skb->len;
  2103. lp->skb_physaddr = dma_map_single(NULL, skb->data, skb->len,
  2104. DMA_TO_DEVICE);
  2105. /* Set address of the data in the Transmit Address register */
  2106. macb_writel(lp, TAR, lp->skb_physaddr);
  2107. /* Set length of the packet in the Transmit Control register */
  2108. macb_writel(lp, TCR, skb->len);
  2109. } else {
  2110. netdev_err(dev, "%s called, but device is busy!\n", __func__);
  2111. return NETDEV_TX_BUSY;
  2112. }
  2113. return NETDEV_TX_OK;
  2114. }
  2115. /* Extract received frame from buffer descriptors and sent to upper layers.
  2116. * (Called from interrupt context)
  2117. */
  2118. static void at91ether_rx(struct net_device *dev)
  2119. {
  2120. struct macb *lp = netdev_priv(dev);
  2121. unsigned char *p_recv;
  2122. struct sk_buff *skb;
  2123. unsigned int pktlen;
  2124. while (lp->rx_ring[lp->rx_tail].addr & MACB_BIT(RX_USED)) {
  2125. p_recv = lp->rx_buffers + lp->rx_tail * AT91ETHER_MAX_RBUFF_SZ;
  2126. pktlen = MACB_BF(RX_FRMLEN, lp->rx_ring[lp->rx_tail].ctrl);
  2127. skb = netdev_alloc_skb(dev, pktlen + 2);
  2128. if (skb) {
  2129. skb_reserve(skb, 2);
  2130. memcpy(skb_put(skb, pktlen), p_recv, pktlen);
  2131. skb->protocol = eth_type_trans(skb, dev);
  2132. lp->stats.rx_packets++;
  2133. lp->stats.rx_bytes += pktlen;
  2134. netif_rx(skb);
  2135. } else {
  2136. lp->stats.rx_dropped++;
  2137. }
  2138. if (lp->rx_ring[lp->rx_tail].ctrl & MACB_BIT(RX_MHASH_MATCH))
  2139. lp->stats.multicast++;
  2140. /* reset ownership bit */
  2141. lp->rx_ring[lp->rx_tail].addr &= ~MACB_BIT(RX_USED);
  2142. /* wrap after last buffer */
  2143. if (lp->rx_tail == AT91ETHER_MAX_RX_DESCR - 1)
  2144. lp->rx_tail = 0;
  2145. else
  2146. lp->rx_tail++;
  2147. }
  2148. }
  2149. /* MAC interrupt handler */
  2150. static irqreturn_t at91ether_interrupt(int irq, void *dev_id)
  2151. {
  2152. struct net_device *dev = dev_id;
  2153. struct macb *lp = netdev_priv(dev);
  2154. u32 intstatus, ctl;
  2155. /* MAC Interrupt Status register indicates what interrupts are pending.
  2156. * It is automatically cleared once read.
  2157. */
  2158. intstatus = macb_readl(lp, ISR);
  2159. /* Receive complete */
  2160. if (intstatus & MACB_BIT(RCOMP))
  2161. at91ether_rx(dev);
  2162. /* Transmit complete */
  2163. if (intstatus & MACB_BIT(TCOMP)) {
  2164. /* The TCOM bit is set even if the transmission failed */
  2165. if (intstatus & (MACB_BIT(ISR_TUND) | MACB_BIT(ISR_RLE)))
  2166. lp->stats.tx_errors++;
  2167. if (lp->skb) {
  2168. dev_kfree_skb_irq(lp->skb);
  2169. lp->skb = NULL;
  2170. dma_unmap_single(NULL, lp->skb_physaddr,
  2171. lp->skb_length, DMA_TO_DEVICE);
  2172. lp->stats.tx_packets++;
  2173. lp->stats.tx_bytes += lp->skb_length;
  2174. }
  2175. netif_wake_queue(dev);
  2176. }
  2177. /* Work-around for EMAC Errata section 41.3.1 */
  2178. if (intstatus & MACB_BIT(RXUBR)) {
  2179. ctl = macb_readl(lp, NCR);
  2180. macb_writel(lp, NCR, ctl & ~MACB_BIT(RE));
  2181. macb_writel(lp, NCR, ctl | MACB_BIT(RE));
  2182. }
  2183. if (intstatus & MACB_BIT(ISR_ROVR))
  2184. netdev_err(dev, "ROVR error\n");
  2185. return IRQ_HANDLED;
  2186. }
  2187. #ifdef CONFIG_NET_POLL_CONTROLLER
  2188. static void at91ether_poll_controller(struct net_device *dev)
  2189. {
  2190. unsigned long flags;
  2191. local_irq_save(flags);
  2192. at91ether_interrupt(dev->irq, dev);
  2193. local_irq_restore(flags);
  2194. }
  2195. #endif
  2196. static const struct net_device_ops at91ether_netdev_ops = {
  2197. .ndo_open = at91ether_open,
  2198. .ndo_stop = at91ether_close,
  2199. .ndo_start_xmit = at91ether_start_xmit,
  2200. .ndo_get_stats = macb_get_stats,
  2201. .ndo_set_rx_mode = macb_set_rx_mode,
  2202. .ndo_set_mac_address = eth_mac_addr,
  2203. .ndo_do_ioctl = macb_ioctl,
  2204. .ndo_validate_addr = eth_validate_addr,
  2205. .ndo_change_mtu = eth_change_mtu,
  2206. #ifdef CONFIG_NET_POLL_CONTROLLER
  2207. .ndo_poll_controller = at91ether_poll_controller,
  2208. #endif
  2209. };
  2210. static int at91ether_clk_init(struct platform_device *pdev, struct clk **pclk,
  2211. struct clk **hclk, struct clk **tx_clk)
  2212. {
  2213. int err;
  2214. *hclk = NULL;
  2215. *tx_clk = NULL;
  2216. *pclk = devm_clk_get(&pdev->dev, "ether_clk");
  2217. if (IS_ERR(*pclk))
  2218. return PTR_ERR(*pclk);
  2219. err = clk_prepare_enable(*pclk);
  2220. if (err) {
  2221. dev_err(&pdev->dev, "failed to enable pclk (%u)\n", err);
  2222. return err;
  2223. }
  2224. return 0;
  2225. }
  2226. static int at91ether_init(struct platform_device *pdev)
  2227. {
  2228. struct net_device *dev = platform_get_drvdata(pdev);
  2229. struct macb *bp = netdev_priv(dev);
  2230. int err;
  2231. u32 reg;
  2232. dev->netdev_ops = &at91ether_netdev_ops;
  2233. dev->ethtool_ops = &macb_ethtool_ops;
  2234. err = devm_request_irq(&pdev->dev, dev->irq, at91ether_interrupt,
  2235. 0, dev->name, dev);
  2236. if (err)
  2237. return err;
  2238. macb_writel(bp, NCR, 0);
  2239. reg = MACB_BF(CLK, MACB_CLK_DIV32) | MACB_BIT(BIG);
  2240. if (bp->phy_interface == PHY_INTERFACE_MODE_RMII)
  2241. reg |= MACB_BIT(RM9200_RMII);
  2242. macb_writel(bp, NCFGR, reg);
  2243. return 0;
  2244. }
  2245. static const struct macb_config at91sam9260_config = {
  2246. .caps = MACB_CAPS_USRIO_HAS_CLKEN | MACB_CAPS_USRIO_DEFAULT_IS_MII,
  2247. .clk_init = macb_clk_init,
  2248. .init = macb_init,
  2249. };
  2250. static const struct macb_config pc302gem_config = {
  2251. .caps = MACB_CAPS_SG_DISABLED | MACB_CAPS_GIGABIT_MODE_AVAILABLE,
  2252. .dma_burst_length = 16,
  2253. .clk_init = macb_clk_init,
  2254. .init = macb_init,
  2255. };
  2256. static const struct macb_config sama5d2_config = {
  2257. .caps = 0,
  2258. .dma_burst_length = 16,
  2259. .clk_init = macb_clk_init,
  2260. .init = macb_init,
  2261. };
  2262. static const struct macb_config sama5d3_config = {
  2263. .caps = MACB_CAPS_SG_DISABLED | MACB_CAPS_GIGABIT_MODE_AVAILABLE,
  2264. .dma_burst_length = 16,
  2265. .clk_init = macb_clk_init,
  2266. .init = macb_init,
  2267. };
  2268. static const struct macb_config sama5d4_config = {
  2269. .caps = 0,
  2270. .dma_burst_length = 4,
  2271. .clk_init = macb_clk_init,
  2272. .init = macb_init,
  2273. };
  2274. static const struct macb_config emac_config = {
  2275. .clk_init = at91ether_clk_init,
  2276. .init = at91ether_init,
  2277. };
  2278. static const struct macb_config zynqmp_config = {
  2279. .caps = MACB_CAPS_GIGABIT_MODE_AVAILABLE | MACB_CAPS_JUMBO,
  2280. .dma_burst_length = 16,
  2281. .clk_init = macb_clk_init,
  2282. .init = macb_init,
  2283. .jumbo_max_len = 10240,
  2284. };
  2285. static const struct macb_config zynq_config = {
  2286. .caps = MACB_CAPS_GIGABIT_MODE_AVAILABLE | MACB_CAPS_NO_GIGABIT_HALF,
  2287. .dma_burst_length = 16,
  2288. .clk_init = macb_clk_init,
  2289. .init = macb_init,
  2290. };
  2291. static const struct of_device_id macb_dt_ids[] = {
  2292. { .compatible = "cdns,at32ap7000-macb" },
  2293. { .compatible = "cdns,at91sam9260-macb", .data = &at91sam9260_config },
  2294. { .compatible = "cdns,macb" },
  2295. { .compatible = "cdns,pc302-gem", .data = &pc302gem_config },
  2296. { .compatible = "cdns,gem", .data = &pc302gem_config },
  2297. { .compatible = "atmel,sama5d2-gem", .data = &sama5d2_config },
  2298. { .compatible = "atmel,sama5d3-gem", .data = &sama5d3_config },
  2299. { .compatible = "atmel,sama5d4-gem", .data = &sama5d4_config },
  2300. { .compatible = "cdns,at91rm9200-emac", .data = &emac_config },
  2301. { .compatible = "cdns,emac", .data = &emac_config },
  2302. { .compatible = "cdns,zynqmp-gem", .data = &zynqmp_config},
  2303. { .compatible = "cdns,zynq-gem", .data = &zynq_config },
  2304. { /* sentinel */ }
  2305. };
  2306. MODULE_DEVICE_TABLE(of, macb_dt_ids);
  2307. #endif /* CONFIG_OF */
  2308. static int macb_probe(struct platform_device *pdev)
  2309. {
  2310. int (*clk_init)(struct platform_device *, struct clk **,
  2311. struct clk **, struct clk **)
  2312. = macb_clk_init;
  2313. int (*init)(struct platform_device *) = macb_init;
  2314. struct device_node *np = pdev->dev.of_node;
  2315. const struct macb_config *macb_config = NULL;
  2316. struct clk *pclk, *hclk, *tx_clk;
  2317. unsigned int queue_mask, num_queues;
  2318. struct macb_platform_data *pdata;
  2319. bool native_io;
  2320. struct phy_device *phydev;
  2321. struct net_device *dev;
  2322. struct resource *regs;
  2323. void __iomem *mem;
  2324. const char *mac;
  2325. struct macb *bp;
  2326. int err;
  2327. regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  2328. mem = devm_ioremap_resource(&pdev->dev, regs);
  2329. if (IS_ERR(mem))
  2330. return PTR_ERR(mem);
  2331. if (np) {
  2332. const struct of_device_id *match;
  2333. match = of_match_node(macb_dt_ids, np);
  2334. if (match && match->data) {
  2335. macb_config = match->data;
  2336. clk_init = macb_config->clk_init;
  2337. init = macb_config->init;
  2338. }
  2339. }
  2340. err = clk_init(pdev, &pclk, &hclk, &tx_clk);
  2341. if (err)
  2342. return err;
  2343. native_io = hw_is_native_io(mem);
  2344. macb_probe_queues(mem, native_io, &queue_mask, &num_queues);
  2345. dev = alloc_etherdev_mq(sizeof(*bp), num_queues);
  2346. if (!dev) {
  2347. err = -ENOMEM;
  2348. goto err_disable_clocks;
  2349. }
  2350. dev->base_addr = regs->start;
  2351. SET_NETDEV_DEV(dev, &pdev->dev);
  2352. bp = netdev_priv(dev);
  2353. bp->pdev = pdev;
  2354. bp->dev = dev;
  2355. bp->regs = mem;
  2356. bp->native_io = native_io;
  2357. if (native_io) {
  2358. bp->macb_reg_readl = hw_readl_native;
  2359. bp->macb_reg_writel = hw_writel_native;
  2360. } else {
  2361. bp->macb_reg_readl = hw_readl;
  2362. bp->macb_reg_writel = hw_writel;
  2363. }
  2364. bp->num_queues = num_queues;
  2365. bp->queue_mask = queue_mask;
  2366. if (macb_config)
  2367. bp->dma_burst_length = macb_config->dma_burst_length;
  2368. bp->pclk = pclk;
  2369. bp->hclk = hclk;
  2370. bp->tx_clk = tx_clk;
  2371. if (macb_config)
  2372. bp->jumbo_max_len = macb_config->jumbo_max_len;
  2373. spin_lock_init(&bp->lock);
  2374. /* setup capabilities */
  2375. macb_configure_caps(bp, macb_config);
  2376. platform_set_drvdata(pdev, dev);
  2377. dev->irq = platform_get_irq(pdev, 0);
  2378. if (dev->irq < 0) {
  2379. err = dev->irq;
  2380. goto err_disable_clocks;
  2381. }
  2382. mac = of_get_mac_address(np);
  2383. if (mac)
  2384. memcpy(bp->dev->dev_addr, mac, ETH_ALEN);
  2385. else
  2386. macb_get_hwaddr(bp);
  2387. err = of_get_phy_mode(np);
  2388. if (err < 0) {
  2389. pdata = dev_get_platdata(&pdev->dev);
  2390. if (pdata && pdata->is_rmii)
  2391. bp->phy_interface = PHY_INTERFACE_MODE_RMII;
  2392. else
  2393. bp->phy_interface = PHY_INTERFACE_MODE_MII;
  2394. } else {
  2395. bp->phy_interface = err;
  2396. }
  2397. /* IP specific init */
  2398. err = init(pdev);
  2399. if (err)
  2400. goto err_out_free_netdev;
  2401. err = register_netdev(dev);
  2402. if (err) {
  2403. dev_err(&pdev->dev, "Cannot register net device, aborting.\n");
  2404. goto err_out_unregister_netdev;
  2405. }
  2406. err = macb_mii_init(bp);
  2407. if (err)
  2408. goto err_out_unregister_netdev;
  2409. netif_carrier_off(dev);
  2410. netdev_info(dev, "Cadence %s rev 0x%08x at 0x%08lx irq %d (%pM)\n",
  2411. macb_is_gem(bp) ? "GEM" : "MACB", macb_readl(bp, MID),
  2412. dev->base_addr, dev->irq, dev->dev_addr);
  2413. phydev = bp->phy_dev;
  2414. netdev_info(dev, "attached PHY driver [%s] (mii_bus:phy_addr=%s, irq=%d)\n",
  2415. phydev->drv->name, dev_name(&phydev->dev), phydev->irq);
  2416. return 0;
  2417. err_out_unregister_netdev:
  2418. unregister_netdev(dev);
  2419. err_out_free_netdev:
  2420. free_netdev(dev);
  2421. err_disable_clocks:
  2422. clk_disable_unprepare(tx_clk);
  2423. clk_disable_unprepare(hclk);
  2424. clk_disable_unprepare(pclk);
  2425. return err;
  2426. }
  2427. static int macb_remove(struct platform_device *pdev)
  2428. {
  2429. struct net_device *dev;
  2430. struct macb *bp;
  2431. dev = platform_get_drvdata(pdev);
  2432. if (dev) {
  2433. bp = netdev_priv(dev);
  2434. if (bp->phy_dev)
  2435. phy_disconnect(bp->phy_dev);
  2436. mdiobus_unregister(bp->mii_bus);
  2437. kfree(bp->mii_bus->irq);
  2438. mdiobus_free(bp->mii_bus);
  2439. unregister_netdev(dev);
  2440. clk_disable_unprepare(bp->tx_clk);
  2441. clk_disable_unprepare(bp->hclk);
  2442. clk_disable_unprepare(bp->pclk);
  2443. free_netdev(dev);
  2444. }
  2445. return 0;
  2446. }
  2447. static int __maybe_unused macb_suspend(struct device *dev)
  2448. {
  2449. struct platform_device *pdev = to_platform_device(dev);
  2450. struct net_device *netdev = platform_get_drvdata(pdev);
  2451. struct macb *bp = netdev_priv(netdev);
  2452. netif_carrier_off(netdev);
  2453. netif_device_detach(netdev);
  2454. clk_disable_unprepare(bp->tx_clk);
  2455. clk_disable_unprepare(bp->hclk);
  2456. clk_disable_unprepare(bp->pclk);
  2457. return 0;
  2458. }
  2459. static int __maybe_unused macb_resume(struct device *dev)
  2460. {
  2461. struct platform_device *pdev = to_platform_device(dev);
  2462. struct net_device *netdev = platform_get_drvdata(pdev);
  2463. struct macb *bp = netdev_priv(netdev);
  2464. clk_prepare_enable(bp->pclk);
  2465. clk_prepare_enable(bp->hclk);
  2466. clk_prepare_enable(bp->tx_clk);
  2467. netif_device_attach(netdev);
  2468. return 0;
  2469. }
  2470. static SIMPLE_DEV_PM_OPS(macb_pm_ops, macb_suspend, macb_resume);
  2471. static struct platform_driver macb_driver = {
  2472. .probe = macb_probe,
  2473. .remove = macb_remove,
  2474. .driver = {
  2475. .name = "macb",
  2476. .of_match_table = of_match_ptr(macb_dt_ids),
  2477. .pm = &macb_pm_ops,
  2478. },
  2479. };
  2480. module_platform_driver(macb_driver);
  2481. MODULE_LICENSE("GPL");
  2482. MODULE_DESCRIPTION("Cadence MACB/GEM Ethernet driver");
  2483. MODULE_AUTHOR("Haavard Skinnemoen (Atmel)");
  2484. MODULE_ALIAS("platform:macb");