macb.c 80 KB

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