macb.c 75 KB

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