macb.c 73 KB

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