macb.c 78 KB

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