macb.c 72 KB

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