macb.c 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067
  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 <linux/pinctrl/consumer.h>
  33. #include "macb.h"
  34. #define MACB_RX_BUFFER_SIZE 128
  35. #define RX_BUFFER_MULTIPLE 64 /* bytes */
  36. #define RX_RING_SIZE 512 /* must be power of 2 */
  37. #define RX_RING_BYTES (sizeof(struct macb_dma_desc) * RX_RING_SIZE)
  38. #define TX_RING_SIZE 128 /* must be power of 2 */
  39. #define TX_RING_BYTES (sizeof(struct macb_dma_desc) * TX_RING_SIZE)
  40. /* level of occupied TX descriptors under which we wake up TX process */
  41. #define MACB_TX_WAKEUP_THRESH (3 * TX_RING_SIZE / 4)
  42. #define MACB_RX_INT_FLAGS (MACB_BIT(RCOMP) | MACB_BIT(RXUBR) \
  43. | MACB_BIT(ISR_ROVR))
  44. #define MACB_TX_ERR_FLAGS (MACB_BIT(ISR_TUND) \
  45. | MACB_BIT(ISR_RLE) \
  46. | MACB_BIT(TXERR))
  47. #define MACB_TX_INT_FLAGS (MACB_TX_ERR_FLAGS | MACB_BIT(TCOMP))
  48. /*
  49. * Graceful stop timeouts in us. We should allow up to
  50. * 1 frame time (10 Mbits/s, full-duplex, ignoring collisions)
  51. */
  52. #define MACB_HALT_TIMEOUT 1230
  53. /* Ring buffer accessors */
  54. static unsigned int macb_tx_ring_wrap(unsigned int index)
  55. {
  56. return index & (TX_RING_SIZE - 1);
  57. }
  58. static struct macb_dma_desc *macb_tx_desc(struct macb *bp, unsigned int index)
  59. {
  60. return &bp->tx_ring[macb_tx_ring_wrap(index)];
  61. }
  62. static struct macb_tx_skb *macb_tx_skb(struct macb *bp, unsigned int index)
  63. {
  64. return &bp->tx_skb[macb_tx_ring_wrap(index)];
  65. }
  66. static dma_addr_t macb_tx_dma(struct macb *bp, unsigned int index)
  67. {
  68. dma_addr_t offset;
  69. offset = macb_tx_ring_wrap(index) * sizeof(struct macb_dma_desc);
  70. return bp->tx_ring_dma + offset;
  71. }
  72. static unsigned int macb_rx_ring_wrap(unsigned int index)
  73. {
  74. return index & (RX_RING_SIZE - 1);
  75. }
  76. static struct macb_dma_desc *macb_rx_desc(struct macb *bp, unsigned int index)
  77. {
  78. return &bp->rx_ring[macb_rx_ring_wrap(index)];
  79. }
  80. static void *macb_rx_buffer(struct macb *bp, unsigned int index)
  81. {
  82. return bp->rx_buffers + bp->rx_buffer_size * macb_rx_ring_wrap(index);
  83. }
  84. void macb_set_hwaddr(struct macb *bp)
  85. {
  86. u32 bottom;
  87. u16 top;
  88. bottom = cpu_to_le32(*((u32 *)bp->dev->dev_addr));
  89. macb_or_gem_writel(bp, SA1B, bottom);
  90. top = cpu_to_le16(*((u16 *)(bp->dev->dev_addr + 4)));
  91. macb_or_gem_writel(bp, SA1T, top);
  92. /* Clear unused address register sets */
  93. macb_or_gem_writel(bp, SA2B, 0);
  94. macb_or_gem_writel(bp, SA2T, 0);
  95. macb_or_gem_writel(bp, SA3B, 0);
  96. macb_or_gem_writel(bp, SA3T, 0);
  97. macb_or_gem_writel(bp, SA4B, 0);
  98. macb_or_gem_writel(bp, SA4T, 0);
  99. }
  100. EXPORT_SYMBOL_GPL(macb_set_hwaddr);
  101. void macb_get_hwaddr(struct macb *bp)
  102. {
  103. struct macb_platform_data *pdata;
  104. u32 bottom;
  105. u16 top;
  106. u8 addr[6];
  107. int i;
  108. pdata = dev_get_platdata(&bp->pdev->dev);
  109. /* Check all 4 address register for vaild address */
  110. for (i = 0; i < 4; i++) {
  111. bottom = macb_or_gem_readl(bp, SA1B + i * 8);
  112. top = macb_or_gem_readl(bp, SA1T + i * 8);
  113. if (pdata && pdata->rev_eth_addr) {
  114. addr[5] = bottom & 0xff;
  115. addr[4] = (bottom >> 8) & 0xff;
  116. addr[3] = (bottom >> 16) & 0xff;
  117. addr[2] = (bottom >> 24) & 0xff;
  118. addr[1] = top & 0xff;
  119. addr[0] = (top & 0xff00) >> 8;
  120. } else {
  121. addr[0] = bottom & 0xff;
  122. addr[1] = (bottom >> 8) & 0xff;
  123. addr[2] = (bottom >> 16) & 0xff;
  124. addr[3] = (bottom >> 24) & 0xff;
  125. addr[4] = top & 0xff;
  126. addr[5] = (top >> 8) & 0xff;
  127. }
  128. if (is_valid_ether_addr(addr)) {
  129. memcpy(bp->dev->dev_addr, addr, sizeof(addr));
  130. return;
  131. }
  132. }
  133. netdev_info(bp->dev, "invalid hw address, using random\n");
  134. eth_hw_addr_random(bp->dev);
  135. }
  136. EXPORT_SYMBOL_GPL(macb_get_hwaddr);
  137. static int macb_mdio_read(struct mii_bus *bus, int mii_id, int regnum)
  138. {
  139. struct macb *bp = bus->priv;
  140. int value;
  141. macb_writel(bp, MAN, (MACB_BF(SOF, MACB_MAN_SOF)
  142. | MACB_BF(RW, MACB_MAN_READ)
  143. | MACB_BF(PHYA, mii_id)
  144. | MACB_BF(REGA, regnum)
  145. | MACB_BF(CODE, MACB_MAN_CODE)));
  146. /* wait for end of transfer */
  147. while (!MACB_BFEXT(IDLE, macb_readl(bp, NSR)))
  148. cpu_relax();
  149. value = MACB_BFEXT(DATA, macb_readl(bp, MAN));
  150. return value;
  151. }
  152. static int macb_mdio_write(struct mii_bus *bus, int mii_id, int regnum,
  153. u16 value)
  154. {
  155. struct macb *bp = bus->priv;
  156. macb_writel(bp, MAN, (MACB_BF(SOF, MACB_MAN_SOF)
  157. | MACB_BF(RW, MACB_MAN_WRITE)
  158. | MACB_BF(PHYA, mii_id)
  159. | MACB_BF(REGA, regnum)
  160. | MACB_BF(CODE, MACB_MAN_CODE)
  161. | MACB_BF(DATA, value)));
  162. /* wait for end of transfer */
  163. while (!MACB_BFEXT(IDLE, macb_readl(bp, NSR)))
  164. cpu_relax();
  165. return 0;
  166. }
  167. /**
  168. * macb_set_tx_clk() - Set a clock to a new frequency
  169. * @clk Pointer to the clock to change
  170. * @rate New frequency in Hz
  171. * @dev Pointer to the struct net_device
  172. */
  173. static void macb_set_tx_clk(struct clk *clk, int speed, struct net_device *dev)
  174. {
  175. long ferr, rate, rate_rounded;
  176. switch (speed) {
  177. case SPEED_10:
  178. rate = 2500000;
  179. break;
  180. case SPEED_100:
  181. rate = 25000000;
  182. break;
  183. case SPEED_1000:
  184. rate = 125000000;
  185. break;
  186. default:
  187. return;
  188. }
  189. rate_rounded = clk_round_rate(clk, rate);
  190. if (rate_rounded < 0)
  191. return;
  192. /* RGMII allows 50 ppm frequency error. Test and warn if this limit
  193. * is not satisfied.
  194. */
  195. ferr = abs(rate_rounded - rate);
  196. ferr = DIV_ROUND_UP(ferr, rate / 100000);
  197. if (ferr > 5)
  198. netdev_warn(dev, "unable to generate target frequency: %ld Hz\n",
  199. rate);
  200. if (clk_set_rate(clk, rate_rounded))
  201. netdev_err(dev, "adjusting tx_clk failed.\n");
  202. }
  203. static void macb_handle_link_change(struct net_device *dev)
  204. {
  205. struct macb *bp = netdev_priv(dev);
  206. struct phy_device *phydev = bp->phy_dev;
  207. unsigned long flags;
  208. int status_change = 0;
  209. spin_lock_irqsave(&bp->lock, flags);
  210. if (phydev->link) {
  211. if ((bp->speed != phydev->speed) ||
  212. (bp->duplex != phydev->duplex)) {
  213. u32 reg;
  214. reg = macb_readl(bp, NCFGR);
  215. reg &= ~(MACB_BIT(SPD) | MACB_BIT(FD));
  216. if (macb_is_gem(bp))
  217. reg &= ~GEM_BIT(GBE);
  218. if (phydev->duplex)
  219. reg |= MACB_BIT(FD);
  220. if (phydev->speed == SPEED_100)
  221. reg |= MACB_BIT(SPD);
  222. if (phydev->speed == SPEED_1000)
  223. reg |= GEM_BIT(GBE);
  224. macb_or_gem_writel(bp, NCFGR, reg);
  225. bp->speed = phydev->speed;
  226. bp->duplex = phydev->duplex;
  227. status_change = 1;
  228. }
  229. }
  230. if (phydev->link != bp->link) {
  231. if (!phydev->link) {
  232. bp->speed = 0;
  233. bp->duplex = -1;
  234. }
  235. bp->link = phydev->link;
  236. status_change = 1;
  237. }
  238. spin_unlock_irqrestore(&bp->lock, flags);
  239. if (!IS_ERR(bp->tx_clk))
  240. macb_set_tx_clk(bp->tx_clk, phydev->speed, dev);
  241. if (status_change) {
  242. if (phydev->link) {
  243. netif_carrier_on(dev);
  244. netdev_info(dev, "link up (%d/%s)\n",
  245. phydev->speed,
  246. phydev->duplex == DUPLEX_FULL ?
  247. "Full" : "Half");
  248. } else {
  249. netif_carrier_off(dev);
  250. netdev_info(dev, "link down\n");
  251. }
  252. }
  253. }
  254. /* based on au1000_eth. c*/
  255. static int macb_mii_probe(struct net_device *dev)
  256. {
  257. struct macb *bp = netdev_priv(dev);
  258. struct macb_platform_data *pdata;
  259. struct phy_device *phydev;
  260. int phy_irq;
  261. int ret;
  262. phydev = phy_find_first(bp->mii_bus);
  263. if (!phydev) {
  264. netdev_err(dev, "no PHY found\n");
  265. return -ENXIO;
  266. }
  267. pdata = dev_get_platdata(&bp->pdev->dev);
  268. if (pdata && gpio_is_valid(pdata->phy_irq_pin)) {
  269. ret = devm_gpio_request(&bp->pdev->dev, pdata->phy_irq_pin, "phy int");
  270. if (!ret) {
  271. phy_irq = gpio_to_irq(pdata->phy_irq_pin);
  272. phydev->irq = (phy_irq < 0) ? PHY_POLL : phy_irq;
  273. }
  274. }
  275. /* attach the mac to the phy */
  276. ret = phy_connect_direct(dev, phydev, &macb_handle_link_change,
  277. bp->phy_interface);
  278. if (ret) {
  279. netdev_err(dev, "Could not attach to PHY\n");
  280. return ret;
  281. }
  282. /* mask with MAC supported features */
  283. if (macb_is_gem(bp))
  284. phydev->supported &= PHY_GBIT_FEATURES;
  285. else
  286. phydev->supported &= PHY_BASIC_FEATURES;
  287. phydev->advertising = phydev->supported;
  288. bp->link = 0;
  289. bp->speed = 0;
  290. bp->duplex = -1;
  291. bp->phy_dev = phydev;
  292. return 0;
  293. }
  294. int macb_mii_init(struct macb *bp)
  295. {
  296. struct macb_platform_data *pdata;
  297. struct device_node *np;
  298. int err = -ENXIO, i;
  299. /* Enable management port */
  300. macb_writel(bp, NCR, MACB_BIT(MPE));
  301. bp->mii_bus = mdiobus_alloc();
  302. if (bp->mii_bus == NULL) {
  303. err = -ENOMEM;
  304. goto err_out;
  305. }
  306. bp->mii_bus->name = "MACB_mii_bus";
  307. bp->mii_bus->read = &macb_mdio_read;
  308. bp->mii_bus->write = &macb_mdio_write;
  309. snprintf(bp->mii_bus->id, MII_BUS_ID_SIZE, "%s-%x",
  310. bp->pdev->name, bp->pdev->id);
  311. bp->mii_bus->priv = bp;
  312. bp->mii_bus->parent = &bp->dev->dev;
  313. pdata = dev_get_platdata(&bp->pdev->dev);
  314. bp->mii_bus->irq = kmalloc(sizeof(int)*PHY_MAX_ADDR, GFP_KERNEL);
  315. if (!bp->mii_bus->irq) {
  316. err = -ENOMEM;
  317. goto err_out_free_mdiobus;
  318. }
  319. dev_set_drvdata(&bp->dev->dev, bp->mii_bus);
  320. np = bp->pdev->dev.of_node;
  321. if (np) {
  322. /* try dt phy registration */
  323. err = of_mdiobus_register(bp->mii_bus, np);
  324. /* fallback to standard phy registration if no phy were
  325. found during dt phy registration */
  326. if (!err && !phy_find_first(bp->mii_bus)) {
  327. for (i = 0; i < PHY_MAX_ADDR; i++) {
  328. struct phy_device *phydev;
  329. phydev = mdiobus_scan(bp->mii_bus, i);
  330. if (IS_ERR(phydev)) {
  331. err = PTR_ERR(phydev);
  332. break;
  333. }
  334. }
  335. if (err)
  336. goto err_out_unregister_bus;
  337. }
  338. } else {
  339. for (i = 0; i < PHY_MAX_ADDR; i++)
  340. bp->mii_bus->irq[i] = PHY_POLL;
  341. if (pdata)
  342. bp->mii_bus->phy_mask = pdata->phy_mask;
  343. err = mdiobus_register(bp->mii_bus);
  344. }
  345. if (err)
  346. goto err_out_free_mdio_irq;
  347. err = macb_mii_probe(bp->dev);
  348. if (err)
  349. goto err_out_unregister_bus;
  350. return 0;
  351. err_out_unregister_bus:
  352. mdiobus_unregister(bp->mii_bus);
  353. err_out_free_mdio_irq:
  354. kfree(bp->mii_bus->irq);
  355. err_out_free_mdiobus:
  356. mdiobus_free(bp->mii_bus);
  357. err_out:
  358. return err;
  359. }
  360. EXPORT_SYMBOL_GPL(macb_mii_init);
  361. static void macb_update_stats(struct macb *bp)
  362. {
  363. u32 __iomem *reg = bp->regs + MACB_PFR;
  364. u32 *p = &bp->hw_stats.macb.rx_pause_frames;
  365. u32 *end = &bp->hw_stats.macb.tx_pause_frames + 1;
  366. WARN_ON((unsigned long)(end - p - 1) != (MACB_TPF - MACB_PFR) / 4);
  367. for(; p < end; p++, reg++)
  368. *p += __raw_readl(reg);
  369. }
  370. static int macb_halt_tx(struct macb *bp)
  371. {
  372. unsigned long halt_time, timeout;
  373. u32 status;
  374. macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(THALT));
  375. timeout = jiffies + usecs_to_jiffies(MACB_HALT_TIMEOUT);
  376. do {
  377. halt_time = jiffies;
  378. status = macb_readl(bp, TSR);
  379. if (!(status & MACB_BIT(TGO)))
  380. return 0;
  381. usleep_range(10, 250);
  382. } while (time_before(halt_time, timeout));
  383. return -ETIMEDOUT;
  384. }
  385. static void macb_tx_error_task(struct work_struct *work)
  386. {
  387. struct macb *bp = container_of(work, struct macb, tx_error_task);
  388. struct macb_tx_skb *tx_skb;
  389. struct sk_buff *skb;
  390. unsigned int tail;
  391. netdev_vdbg(bp->dev, "macb_tx_error_task: t = %u, h = %u\n",
  392. bp->tx_tail, bp->tx_head);
  393. /* Make sure nobody is trying to queue up new packets */
  394. netif_stop_queue(bp->dev);
  395. /*
  396. * Stop transmission now
  397. * (in case we have just queued new packets)
  398. */
  399. if (macb_halt_tx(bp))
  400. /* Just complain for now, reinitializing TX path can be good */
  401. netdev_err(bp->dev, "BUG: halt tx timed out\n");
  402. /* No need for the lock here as nobody will interrupt us anymore */
  403. /*
  404. * Treat frames in TX queue including the ones that caused the error.
  405. * Free transmit buffers in upper layer.
  406. */
  407. for (tail = bp->tx_tail; tail != bp->tx_head; tail++) {
  408. struct macb_dma_desc *desc;
  409. u32 ctrl;
  410. desc = macb_tx_desc(bp, tail);
  411. ctrl = desc->ctrl;
  412. tx_skb = macb_tx_skb(bp, tail);
  413. skb = tx_skb->skb;
  414. if (ctrl & MACB_BIT(TX_USED)) {
  415. netdev_vdbg(bp->dev, "txerr skb %u (data %p) TX complete\n",
  416. macb_tx_ring_wrap(tail), skb->data);
  417. bp->stats.tx_packets++;
  418. bp->stats.tx_bytes += skb->len;
  419. } else {
  420. /*
  421. * "Buffers exhausted mid-frame" errors may only happen
  422. * if the driver is buggy, so complain loudly about those.
  423. * Statistics are updated by hardware.
  424. */
  425. if (ctrl & MACB_BIT(TX_BUF_EXHAUSTED))
  426. netdev_err(bp->dev,
  427. "BUG: TX buffers exhausted mid-frame\n");
  428. desc->ctrl = ctrl | MACB_BIT(TX_USED);
  429. }
  430. dma_unmap_single(&bp->pdev->dev, tx_skb->mapping, skb->len,
  431. DMA_TO_DEVICE);
  432. tx_skb->skb = NULL;
  433. dev_kfree_skb(skb);
  434. }
  435. /* Make descriptor updates visible to hardware */
  436. wmb();
  437. /* Reinitialize the TX desc queue */
  438. macb_writel(bp, TBQP, bp->tx_ring_dma);
  439. /* Make TX ring reflect state of hardware */
  440. bp->tx_head = bp->tx_tail = 0;
  441. /* Now we are ready to start transmission again */
  442. netif_wake_queue(bp->dev);
  443. /* Housework before enabling TX IRQ */
  444. macb_writel(bp, TSR, macb_readl(bp, TSR));
  445. macb_writel(bp, IER, MACB_TX_INT_FLAGS);
  446. }
  447. static void macb_tx_interrupt(struct macb *bp)
  448. {
  449. unsigned int tail;
  450. unsigned int head;
  451. u32 status;
  452. status = macb_readl(bp, TSR);
  453. macb_writel(bp, TSR, status);
  454. if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
  455. macb_writel(bp, ISR, MACB_BIT(TCOMP));
  456. netdev_vdbg(bp->dev, "macb_tx_interrupt status = 0x%03lx\n",
  457. (unsigned long)status);
  458. head = bp->tx_head;
  459. for (tail = bp->tx_tail; tail != head; tail++) {
  460. struct macb_tx_skb *tx_skb;
  461. struct sk_buff *skb;
  462. struct macb_dma_desc *desc;
  463. u32 ctrl;
  464. desc = macb_tx_desc(bp, tail);
  465. /* Make hw descriptor updates visible to CPU */
  466. rmb();
  467. ctrl = desc->ctrl;
  468. if (!(ctrl & MACB_BIT(TX_USED)))
  469. break;
  470. tx_skb = macb_tx_skb(bp, tail);
  471. skb = tx_skb->skb;
  472. netdev_vdbg(bp->dev, "skb %u (data %p) TX complete\n",
  473. macb_tx_ring_wrap(tail), skb->data);
  474. dma_unmap_single(&bp->pdev->dev, tx_skb->mapping, skb->len,
  475. DMA_TO_DEVICE);
  476. bp->stats.tx_packets++;
  477. bp->stats.tx_bytes += skb->len;
  478. tx_skb->skb = NULL;
  479. dev_kfree_skb_irq(skb);
  480. }
  481. bp->tx_tail = tail;
  482. if (netif_queue_stopped(bp->dev)
  483. && CIRC_CNT(bp->tx_head, bp->tx_tail,
  484. TX_RING_SIZE) <= MACB_TX_WAKEUP_THRESH)
  485. netif_wake_queue(bp->dev);
  486. }
  487. static void gem_rx_refill(struct macb *bp)
  488. {
  489. unsigned int entry;
  490. struct sk_buff *skb;
  491. dma_addr_t paddr;
  492. while (CIRC_SPACE(bp->rx_prepared_head, bp->rx_tail, RX_RING_SIZE) > 0) {
  493. entry = macb_rx_ring_wrap(bp->rx_prepared_head);
  494. /* Make hw descriptor updates visible to CPU */
  495. rmb();
  496. bp->rx_prepared_head++;
  497. if (bp->rx_skbuff[entry] == NULL) {
  498. /* allocate sk_buff for this free entry in ring */
  499. skb = netdev_alloc_skb(bp->dev, bp->rx_buffer_size);
  500. if (unlikely(skb == NULL)) {
  501. netdev_err(bp->dev,
  502. "Unable to allocate sk_buff\n");
  503. break;
  504. }
  505. /* now fill corresponding descriptor entry */
  506. paddr = dma_map_single(&bp->pdev->dev, skb->data,
  507. bp->rx_buffer_size, DMA_FROM_DEVICE);
  508. if (dma_mapping_error(&bp->pdev->dev, paddr)) {
  509. dev_kfree_skb(skb);
  510. break;
  511. }
  512. bp->rx_skbuff[entry] = skb;
  513. if (entry == RX_RING_SIZE - 1)
  514. paddr |= MACB_BIT(RX_WRAP);
  515. bp->rx_ring[entry].addr = paddr;
  516. bp->rx_ring[entry].ctrl = 0;
  517. /* properly align Ethernet header */
  518. skb_reserve(skb, NET_IP_ALIGN);
  519. }
  520. }
  521. /* Make descriptor updates visible to hardware */
  522. wmb();
  523. netdev_vdbg(bp->dev, "rx ring: prepared head %d, tail %d\n",
  524. bp->rx_prepared_head, bp->rx_tail);
  525. }
  526. /* Mark DMA descriptors from begin up to and not including end as unused */
  527. static void discard_partial_frame(struct macb *bp, unsigned int begin,
  528. unsigned int end)
  529. {
  530. unsigned int frag;
  531. for (frag = begin; frag != end; frag++) {
  532. struct macb_dma_desc *desc = macb_rx_desc(bp, frag);
  533. desc->addr &= ~MACB_BIT(RX_USED);
  534. }
  535. /* Make descriptor updates visible to hardware */
  536. wmb();
  537. /*
  538. * When this happens, the hardware stats registers for
  539. * whatever caused this is updated, so we don't have to record
  540. * anything.
  541. */
  542. }
  543. static int gem_rx(struct macb *bp, int budget)
  544. {
  545. unsigned int len;
  546. unsigned int entry;
  547. struct sk_buff *skb;
  548. struct macb_dma_desc *desc;
  549. int count = 0;
  550. while (count < budget) {
  551. u32 addr, ctrl;
  552. entry = macb_rx_ring_wrap(bp->rx_tail);
  553. desc = &bp->rx_ring[entry];
  554. /* Make hw descriptor updates visible to CPU */
  555. rmb();
  556. addr = desc->addr;
  557. ctrl = desc->ctrl;
  558. if (!(addr & MACB_BIT(RX_USED)))
  559. break;
  560. bp->rx_tail++;
  561. count++;
  562. if (!(ctrl & MACB_BIT(RX_SOF) && ctrl & MACB_BIT(RX_EOF))) {
  563. netdev_err(bp->dev,
  564. "not whole frame pointed by descriptor\n");
  565. bp->stats.rx_dropped++;
  566. break;
  567. }
  568. skb = bp->rx_skbuff[entry];
  569. if (unlikely(!skb)) {
  570. netdev_err(bp->dev,
  571. "inconsistent Rx descriptor chain\n");
  572. bp->stats.rx_dropped++;
  573. break;
  574. }
  575. /* now everything is ready for receiving packet */
  576. bp->rx_skbuff[entry] = NULL;
  577. len = MACB_BFEXT(RX_FRMLEN, ctrl);
  578. netdev_vdbg(bp->dev, "gem_rx %u (len %u)\n", entry, len);
  579. skb_put(skb, len);
  580. addr = MACB_BF(RX_WADDR, MACB_BFEXT(RX_WADDR, addr));
  581. dma_unmap_single(&bp->pdev->dev, addr,
  582. bp->rx_buffer_size, DMA_FROM_DEVICE);
  583. skb->protocol = eth_type_trans(skb, bp->dev);
  584. skb_checksum_none_assert(skb);
  585. bp->stats.rx_packets++;
  586. bp->stats.rx_bytes += skb->len;
  587. #if defined(DEBUG) && defined(VERBOSE_DEBUG)
  588. netdev_vdbg(bp->dev, "received skb of length %u, csum: %08x\n",
  589. skb->len, skb->csum);
  590. print_hex_dump(KERN_DEBUG, " mac: ", DUMP_PREFIX_ADDRESS, 16, 1,
  591. skb->mac_header, 16, true);
  592. print_hex_dump(KERN_DEBUG, "data: ", DUMP_PREFIX_ADDRESS, 16, 1,
  593. skb->data, 32, true);
  594. #endif
  595. netif_receive_skb(skb);
  596. }
  597. gem_rx_refill(bp);
  598. return count;
  599. }
  600. static int macb_rx_frame(struct macb *bp, unsigned int first_frag,
  601. unsigned int last_frag)
  602. {
  603. unsigned int len;
  604. unsigned int frag;
  605. unsigned int offset;
  606. struct sk_buff *skb;
  607. struct macb_dma_desc *desc;
  608. desc = macb_rx_desc(bp, last_frag);
  609. len = MACB_BFEXT(RX_FRMLEN, desc->ctrl);
  610. netdev_vdbg(bp->dev, "macb_rx_frame frags %u - %u (len %u)\n",
  611. macb_rx_ring_wrap(first_frag),
  612. macb_rx_ring_wrap(last_frag), len);
  613. /*
  614. * The ethernet header starts NET_IP_ALIGN bytes into the
  615. * first buffer. Since the header is 14 bytes, this makes the
  616. * payload word-aligned.
  617. *
  618. * Instead of calling skb_reserve(NET_IP_ALIGN), we just copy
  619. * the two padding bytes into the skb so that we avoid hitting
  620. * the slowpath in memcpy(), and pull them off afterwards.
  621. */
  622. skb = netdev_alloc_skb(bp->dev, len + NET_IP_ALIGN);
  623. if (!skb) {
  624. bp->stats.rx_dropped++;
  625. for (frag = first_frag; ; frag++) {
  626. desc = macb_rx_desc(bp, frag);
  627. desc->addr &= ~MACB_BIT(RX_USED);
  628. if (frag == last_frag)
  629. break;
  630. }
  631. /* Make descriptor updates visible to hardware */
  632. wmb();
  633. return 1;
  634. }
  635. offset = 0;
  636. len += NET_IP_ALIGN;
  637. skb_checksum_none_assert(skb);
  638. skb_put(skb, len);
  639. for (frag = first_frag; ; frag++) {
  640. unsigned int frag_len = bp->rx_buffer_size;
  641. if (offset + frag_len > len) {
  642. BUG_ON(frag != last_frag);
  643. frag_len = len - offset;
  644. }
  645. skb_copy_to_linear_data_offset(skb, offset,
  646. macb_rx_buffer(bp, frag), frag_len);
  647. offset += bp->rx_buffer_size;
  648. desc = macb_rx_desc(bp, frag);
  649. desc->addr &= ~MACB_BIT(RX_USED);
  650. if (frag == last_frag)
  651. break;
  652. }
  653. /* Make descriptor updates visible to hardware */
  654. wmb();
  655. __skb_pull(skb, NET_IP_ALIGN);
  656. skb->protocol = eth_type_trans(skb, bp->dev);
  657. bp->stats.rx_packets++;
  658. bp->stats.rx_bytes += skb->len;
  659. netdev_vdbg(bp->dev, "received skb of length %u, csum: %08x\n",
  660. skb->len, skb->csum);
  661. netif_receive_skb(skb);
  662. return 0;
  663. }
  664. static int macb_rx(struct macb *bp, int budget)
  665. {
  666. int received = 0;
  667. unsigned int tail;
  668. int first_frag = -1;
  669. for (tail = bp->rx_tail; budget > 0; tail++) {
  670. struct macb_dma_desc *desc = macb_rx_desc(bp, tail);
  671. u32 addr, ctrl;
  672. /* Make hw descriptor updates visible to CPU */
  673. rmb();
  674. addr = desc->addr;
  675. ctrl = desc->ctrl;
  676. if (!(addr & MACB_BIT(RX_USED)))
  677. break;
  678. if (ctrl & MACB_BIT(RX_SOF)) {
  679. if (first_frag != -1)
  680. discard_partial_frame(bp, first_frag, tail);
  681. first_frag = tail;
  682. }
  683. if (ctrl & MACB_BIT(RX_EOF)) {
  684. int dropped;
  685. BUG_ON(first_frag == -1);
  686. dropped = macb_rx_frame(bp, first_frag, tail);
  687. first_frag = -1;
  688. if (!dropped) {
  689. received++;
  690. budget--;
  691. }
  692. }
  693. }
  694. if (first_frag != -1)
  695. bp->rx_tail = first_frag;
  696. else
  697. bp->rx_tail = tail;
  698. return received;
  699. }
  700. static int macb_poll(struct napi_struct *napi, int budget)
  701. {
  702. struct macb *bp = container_of(napi, struct macb, napi);
  703. int work_done;
  704. u32 status;
  705. status = macb_readl(bp, RSR);
  706. macb_writel(bp, RSR, status);
  707. work_done = 0;
  708. netdev_vdbg(bp->dev, "poll: status = %08lx, budget = %d\n",
  709. (unsigned long)status, budget);
  710. work_done = bp->macbgem_ops.mog_rx(bp, budget);
  711. if (work_done < budget) {
  712. napi_complete(napi);
  713. /* Packets received while interrupts were disabled */
  714. status = macb_readl(bp, RSR);
  715. if (status) {
  716. if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
  717. macb_writel(bp, ISR, MACB_BIT(RCOMP));
  718. napi_reschedule(napi);
  719. } else {
  720. macb_writel(bp, IER, MACB_RX_INT_FLAGS);
  721. }
  722. }
  723. /* TODO: Handle errors */
  724. return work_done;
  725. }
  726. static irqreturn_t macb_interrupt(int irq, void *dev_id)
  727. {
  728. struct net_device *dev = dev_id;
  729. struct macb *bp = netdev_priv(dev);
  730. u32 status;
  731. status = macb_readl(bp, ISR);
  732. if (unlikely(!status))
  733. return IRQ_NONE;
  734. spin_lock(&bp->lock);
  735. while (status) {
  736. /* close possible race with dev_close */
  737. if (unlikely(!netif_running(dev))) {
  738. macb_writel(bp, IDR, -1);
  739. break;
  740. }
  741. netdev_vdbg(bp->dev, "isr = 0x%08lx\n", (unsigned long)status);
  742. if (status & MACB_RX_INT_FLAGS) {
  743. /*
  744. * There's no point taking any more interrupts
  745. * until we have processed the buffers. The
  746. * scheduling call may fail if the poll routine
  747. * is already scheduled, so disable interrupts
  748. * now.
  749. */
  750. macb_writel(bp, IDR, MACB_RX_INT_FLAGS);
  751. if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
  752. macb_writel(bp, ISR, MACB_BIT(RCOMP));
  753. if (napi_schedule_prep(&bp->napi)) {
  754. netdev_vdbg(bp->dev, "scheduling RX softirq\n");
  755. __napi_schedule(&bp->napi);
  756. }
  757. }
  758. if (unlikely(status & (MACB_TX_ERR_FLAGS))) {
  759. macb_writel(bp, IDR, MACB_TX_INT_FLAGS);
  760. schedule_work(&bp->tx_error_task);
  761. if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
  762. macb_writel(bp, ISR, MACB_TX_ERR_FLAGS);
  763. break;
  764. }
  765. if (status & MACB_BIT(TCOMP))
  766. macb_tx_interrupt(bp);
  767. /*
  768. * Link change detection isn't possible with RMII, so we'll
  769. * add that if/when we get our hands on a full-blown MII PHY.
  770. */
  771. if (status & MACB_BIT(ISR_ROVR)) {
  772. /* We missed at least one packet */
  773. if (macb_is_gem(bp))
  774. bp->hw_stats.gem.rx_overruns++;
  775. else
  776. bp->hw_stats.macb.rx_overruns++;
  777. if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
  778. macb_writel(bp, ISR, MACB_BIT(ISR_ROVR));
  779. }
  780. if (status & MACB_BIT(HRESP)) {
  781. /*
  782. * TODO: Reset the hardware, and maybe move the
  783. * netdev_err to a lower-priority context as well
  784. * (work queue?)
  785. */
  786. netdev_err(dev, "DMA bus error: HRESP not OK\n");
  787. if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
  788. macb_writel(bp, ISR, MACB_BIT(HRESP));
  789. }
  790. status = macb_readl(bp, ISR);
  791. }
  792. spin_unlock(&bp->lock);
  793. return IRQ_HANDLED;
  794. }
  795. #ifdef CONFIG_NET_POLL_CONTROLLER
  796. /*
  797. * Polling receive - used by netconsole and other diagnostic tools
  798. * to allow network i/o with interrupts disabled.
  799. */
  800. static void macb_poll_controller(struct net_device *dev)
  801. {
  802. unsigned long flags;
  803. local_irq_save(flags);
  804. macb_interrupt(dev->irq, dev);
  805. local_irq_restore(flags);
  806. }
  807. #endif
  808. static int macb_start_xmit(struct sk_buff *skb, struct net_device *dev)
  809. {
  810. struct macb *bp = netdev_priv(dev);
  811. dma_addr_t mapping;
  812. unsigned int len, entry;
  813. struct macb_dma_desc *desc;
  814. struct macb_tx_skb *tx_skb;
  815. u32 ctrl;
  816. unsigned long flags;
  817. #if defined(DEBUG) && defined(VERBOSE_DEBUG)
  818. netdev_vdbg(bp->dev,
  819. "start_xmit: len %u head %p data %p tail %p end %p\n",
  820. skb->len, skb->head, skb->data,
  821. skb_tail_pointer(skb), skb_end_pointer(skb));
  822. print_hex_dump(KERN_DEBUG, "data: ", DUMP_PREFIX_OFFSET, 16, 1,
  823. skb->data, 16, true);
  824. #endif
  825. len = skb->len;
  826. spin_lock_irqsave(&bp->lock, flags);
  827. /* This is a hard error, log it. */
  828. if (CIRC_SPACE(bp->tx_head, bp->tx_tail, TX_RING_SIZE) < 1) {
  829. netif_stop_queue(dev);
  830. spin_unlock_irqrestore(&bp->lock, flags);
  831. netdev_err(bp->dev, "BUG! Tx Ring full when queue awake!\n");
  832. netdev_dbg(bp->dev, "tx_head = %u, tx_tail = %u\n",
  833. bp->tx_head, bp->tx_tail);
  834. return NETDEV_TX_BUSY;
  835. }
  836. entry = macb_tx_ring_wrap(bp->tx_head);
  837. netdev_vdbg(bp->dev, "Allocated ring entry %u\n", entry);
  838. mapping = dma_map_single(&bp->pdev->dev, skb->data,
  839. len, DMA_TO_DEVICE);
  840. if (dma_mapping_error(&bp->pdev->dev, mapping)) {
  841. dev_kfree_skb_any(skb);
  842. goto unlock;
  843. }
  844. bp->tx_head++;
  845. tx_skb = &bp->tx_skb[entry];
  846. tx_skb->skb = skb;
  847. tx_skb->mapping = mapping;
  848. netdev_vdbg(bp->dev, "Mapped skb data %p to DMA addr %08lx\n",
  849. skb->data, (unsigned long)mapping);
  850. ctrl = MACB_BF(TX_FRMLEN, len);
  851. ctrl |= MACB_BIT(TX_LAST);
  852. if (entry == (TX_RING_SIZE - 1))
  853. ctrl |= MACB_BIT(TX_WRAP);
  854. desc = &bp->tx_ring[entry];
  855. desc->addr = mapping;
  856. desc->ctrl = ctrl;
  857. /* Make newly initialized descriptor visible to hardware */
  858. wmb();
  859. skb_tx_timestamp(skb);
  860. macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(TSTART));
  861. if (CIRC_SPACE(bp->tx_head, bp->tx_tail, TX_RING_SIZE) < 1)
  862. netif_stop_queue(dev);
  863. unlock:
  864. spin_unlock_irqrestore(&bp->lock, flags);
  865. return NETDEV_TX_OK;
  866. }
  867. static void macb_init_rx_buffer_size(struct macb *bp, size_t size)
  868. {
  869. if (!macb_is_gem(bp)) {
  870. bp->rx_buffer_size = MACB_RX_BUFFER_SIZE;
  871. } else {
  872. bp->rx_buffer_size = size;
  873. if (bp->rx_buffer_size % RX_BUFFER_MULTIPLE) {
  874. netdev_dbg(bp->dev,
  875. "RX buffer must be multiple of %d bytes, expanding\n",
  876. RX_BUFFER_MULTIPLE);
  877. bp->rx_buffer_size =
  878. roundup(bp->rx_buffer_size, RX_BUFFER_MULTIPLE);
  879. }
  880. }
  881. netdev_dbg(bp->dev, "mtu [%u] rx_buffer_size [%Zu]\n",
  882. bp->dev->mtu, bp->rx_buffer_size);
  883. }
  884. static void gem_free_rx_buffers(struct macb *bp)
  885. {
  886. struct sk_buff *skb;
  887. struct macb_dma_desc *desc;
  888. dma_addr_t addr;
  889. int i;
  890. if (!bp->rx_skbuff)
  891. return;
  892. for (i = 0; i < RX_RING_SIZE; i++) {
  893. skb = bp->rx_skbuff[i];
  894. if (skb == NULL)
  895. continue;
  896. desc = &bp->rx_ring[i];
  897. addr = MACB_BF(RX_WADDR, MACB_BFEXT(RX_WADDR, desc->addr));
  898. dma_unmap_single(&bp->pdev->dev, addr, bp->rx_buffer_size,
  899. DMA_FROM_DEVICE);
  900. dev_kfree_skb_any(skb);
  901. skb = NULL;
  902. }
  903. kfree(bp->rx_skbuff);
  904. bp->rx_skbuff = NULL;
  905. }
  906. static void macb_free_rx_buffers(struct macb *bp)
  907. {
  908. if (bp->rx_buffers) {
  909. dma_free_coherent(&bp->pdev->dev,
  910. RX_RING_SIZE * bp->rx_buffer_size,
  911. bp->rx_buffers, bp->rx_buffers_dma);
  912. bp->rx_buffers = NULL;
  913. }
  914. }
  915. static void macb_free_consistent(struct macb *bp)
  916. {
  917. if (bp->tx_skb) {
  918. kfree(bp->tx_skb);
  919. bp->tx_skb = NULL;
  920. }
  921. bp->macbgem_ops.mog_free_rx_buffers(bp);
  922. if (bp->rx_ring) {
  923. dma_free_coherent(&bp->pdev->dev, RX_RING_BYTES,
  924. bp->rx_ring, bp->rx_ring_dma);
  925. bp->rx_ring = NULL;
  926. }
  927. if (bp->tx_ring) {
  928. dma_free_coherent(&bp->pdev->dev, TX_RING_BYTES,
  929. bp->tx_ring, bp->tx_ring_dma);
  930. bp->tx_ring = NULL;
  931. }
  932. }
  933. static int gem_alloc_rx_buffers(struct macb *bp)
  934. {
  935. int size;
  936. size = RX_RING_SIZE * sizeof(struct sk_buff *);
  937. bp->rx_skbuff = kzalloc(size, GFP_KERNEL);
  938. if (!bp->rx_skbuff)
  939. return -ENOMEM;
  940. else
  941. netdev_dbg(bp->dev,
  942. "Allocated %d RX struct sk_buff entries at %p\n",
  943. RX_RING_SIZE, bp->rx_skbuff);
  944. return 0;
  945. }
  946. static int macb_alloc_rx_buffers(struct macb *bp)
  947. {
  948. int size;
  949. size = RX_RING_SIZE * bp->rx_buffer_size;
  950. bp->rx_buffers = dma_alloc_coherent(&bp->pdev->dev, size,
  951. &bp->rx_buffers_dma, GFP_KERNEL);
  952. if (!bp->rx_buffers)
  953. return -ENOMEM;
  954. else
  955. netdev_dbg(bp->dev,
  956. "Allocated RX buffers of %d bytes at %08lx (mapped %p)\n",
  957. size, (unsigned long)bp->rx_buffers_dma, bp->rx_buffers);
  958. return 0;
  959. }
  960. static int macb_alloc_consistent(struct macb *bp)
  961. {
  962. int size;
  963. size = TX_RING_SIZE * sizeof(struct macb_tx_skb);
  964. bp->tx_skb = kmalloc(size, GFP_KERNEL);
  965. if (!bp->tx_skb)
  966. goto out_err;
  967. size = RX_RING_BYTES;
  968. bp->rx_ring = dma_alloc_coherent(&bp->pdev->dev, size,
  969. &bp->rx_ring_dma, GFP_KERNEL);
  970. if (!bp->rx_ring)
  971. goto out_err;
  972. netdev_dbg(bp->dev,
  973. "Allocated RX ring of %d bytes at %08lx (mapped %p)\n",
  974. size, (unsigned long)bp->rx_ring_dma, bp->rx_ring);
  975. size = TX_RING_BYTES;
  976. bp->tx_ring = dma_alloc_coherent(&bp->pdev->dev, size,
  977. &bp->tx_ring_dma, GFP_KERNEL);
  978. if (!bp->tx_ring)
  979. goto out_err;
  980. netdev_dbg(bp->dev,
  981. "Allocated TX ring of %d bytes at %08lx (mapped %p)\n",
  982. size, (unsigned long)bp->tx_ring_dma, bp->tx_ring);
  983. if (bp->macbgem_ops.mog_alloc_rx_buffers(bp))
  984. goto out_err;
  985. return 0;
  986. out_err:
  987. macb_free_consistent(bp);
  988. return -ENOMEM;
  989. }
  990. static void gem_init_rings(struct macb *bp)
  991. {
  992. int i;
  993. for (i = 0; i < TX_RING_SIZE; i++) {
  994. bp->tx_ring[i].addr = 0;
  995. bp->tx_ring[i].ctrl = MACB_BIT(TX_USED);
  996. }
  997. bp->tx_ring[TX_RING_SIZE - 1].ctrl |= MACB_BIT(TX_WRAP);
  998. bp->rx_tail = bp->rx_prepared_head = bp->tx_head = bp->tx_tail = 0;
  999. gem_rx_refill(bp);
  1000. }
  1001. static void macb_init_rings(struct macb *bp)
  1002. {
  1003. int i;
  1004. dma_addr_t addr;
  1005. addr = bp->rx_buffers_dma;
  1006. for (i = 0; i < RX_RING_SIZE; i++) {
  1007. bp->rx_ring[i].addr = addr;
  1008. bp->rx_ring[i].ctrl = 0;
  1009. addr += bp->rx_buffer_size;
  1010. }
  1011. bp->rx_ring[RX_RING_SIZE - 1].addr |= MACB_BIT(RX_WRAP);
  1012. for (i = 0; i < TX_RING_SIZE; i++) {
  1013. bp->tx_ring[i].addr = 0;
  1014. bp->tx_ring[i].ctrl = MACB_BIT(TX_USED);
  1015. }
  1016. bp->tx_ring[TX_RING_SIZE - 1].ctrl |= MACB_BIT(TX_WRAP);
  1017. bp->rx_tail = bp->tx_head = bp->tx_tail = 0;
  1018. }
  1019. static void macb_reset_hw(struct macb *bp)
  1020. {
  1021. /*
  1022. * Disable RX and TX (XXX: Should we halt the transmission
  1023. * more gracefully?)
  1024. */
  1025. macb_writel(bp, NCR, 0);
  1026. /* Clear the stats registers (XXX: Update stats first?) */
  1027. macb_writel(bp, NCR, MACB_BIT(CLRSTAT));
  1028. /* Clear all status flags */
  1029. macb_writel(bp, TSR, -1);
  1030. macb_writel(bp, RSR, -1);
  1031. /* Disable all interrupts */
  1032. macb_writel(bp, IDR, -1);
  1033. macb_readl(bp, ISR);
  1034. }
  1035. static u32 gem_mdc_clk_div(struct macb *bp)
  1036. {
  1037. u32 config;
  1038. unsigned long pclk_hz = clk_get_rate(bp->pclk);
  1039. if (pclk_hz <= 20000000)
  1040. config = GEM_BF(CLK, GEM_CLK_DIV8);
  1041. else if (pclk_hz <= 40000000)
  1042. config = GEM_BF(CLK, GEM_CLK_DIV16);
  1043. else if (pclk_hz <= 80000000)
  1044. config = GEM_BF(CLK, GEM_CLK_DIV32);
  1045. else if (pclk_hz <= 120000000)
  1046. config = GEM_BF(CLK, GEM_CLK_DIV48);
  1047. else if (pclk_hz <= 160000000)
  1048. config = GEM_BF(CLK, GEM_CLK_DIV64);
  1049. else
  1050. config = GEM_BF(CLK, GEM_CLK_DIV96);
  1051. return config;
  1052. }
  1053. static u32 macb_mdc_clk_div(struct macb *bp)
  1054. {
  1055. u32 config;
  1056. unsigned long pclk_hz;
  1057. if (macb_is_gem(bp))
  1058. return gem_mdc_clk_div(bp);
  1059. pclk_hz = clk_get_rate(bp->pclk);
  1060. if (pclk_hz <= 20000000)
  1061. config = MACB_BF(CLK, MACB_CLK_DIV8);
  1062. else if (pclk_hz <= 40000000)
  1063. config = MACB_BF(CLK, MACB_CLK_DIV16);
  1064. else if (pclk_hz <= 80000000)
  1065. config = MACB_BF(CLK, MACB_CLK_DIV32);
  1066. else
  1067. config = MACB_BF(CLK, MACB_CLK_DIV64);
  1068. return config;
  1069. }
  1070. /*
  1071. * Get the DMA bus width field of the network configuration register that we
  1072. * should program. We find the width from decoding the design configuration
  1073. * register to find the maximum supported data bus width.
  1074. */
  1075. static u32 macb_dbw(struct macb *bp)
  1076. {
  1077. if (!macb_is_gem(bp))
  1078. return 0;
  1079. switch (GEM_BFEXT(DBWDEF, gem_readl(bp, DCFG1))) {
  1080. case 4:
  1081. return GEM_BF(DBW, GEM_DBW128);
  1082. case 2:
  1083. return GEM_BF(DBW, GEM_DBW64);
  1084. case 1:
  1085. default:
  1086. return GEM_BF(DBW, GEM_DBW32);
  1087. }
  1088. }
  1089. /*
  1090. * Configure the receive DMA engine
  1091. * - use the correct receive buffer size
  1092. * - set the possibility to use INCR16 bursts
  1093. * (if not supported by FIFO, it will fallback to default)
  1094. * - set both rx/tx packet buffers to full memory size
  1095. * These are configurable parameters for GEM.
  1096. */
  1097. static void macb_configure_dma(struct macb *bp)
  1098. {
  1099. u32 dmacfg;
  1100. if (macb_is_gem(bp)) {
  1101. dmacfg = gem_readl(bp, DMACFG) & ~GEM_BF(RXBS, -1L);
  1102. dmacfg |= GEM_BF(RXBS, bp->rx_buffer_size / RX_BUFFER_MULTIPLE);
  1103. dmacfg |= GEM_BF(FBLDO, 16);
  1104. dmacfg |= GEM_BIT(TXPBMS) | GEM_BF(RXBMS, -1L);
  1105. dmacfg &= ~GEM_BIT(ENDIA);
  1106. gem_writel(bp, DMACFG, dmacfg);
  1107. }
  1108. }
  1109. /*
  1110. * Configure peripheral capacities according to integration options used
  1111. */
  1112. static void macb_configure_caps(struct macb *bp)
  1113. {
  1114. if (macb_is_gem(bp)) {
  1115. if (GEM_BFEXT(IRQCOR, gem_readl(bp, DCFG1)) == 0)
  1116. bp->caps |= MACB_CAPS_ISR_CLEAR_ON_WRITE;
  1117. }
  1118. }
  1119. static void macb_init_hw(struct macb *bp)
  1120. {
  1121. u32 config;
  1122. macb_reset_hw(bp);
  1123. macb_set_hwaddr(bp);
  1124. config = macb_mdc_clk_div(bp);
  1125. config |= MACB_BF(RBOF, NET_IP_ALIGN); /* Make eth data aligned */
  1126. config |= MACB_BIT(PAE); /* PAuse Enable */
  1127. config |= MACB_BIT(DRFCS); /* Discard Rx FCS */
  1128. config |= MACB_BIT(BIG); /* Receive oversized frames */
  1129. if (bp->dev->flags & IFF_PROMISC)
  1130. config |= MACB_BIT(CAF); /* Copy All Frames */
  1131. if (!(bp->dev->flags & IFF_BROADCAST))
  1132. config |= MACB_BIT(NBC); /* No BroadCast */
  1133. config |= macb_dbw(bp);
  1134. macb_writel(bp, NCFGR, config);
  1135. bp->speed = SPEED_10;
  1136. bp->duplex = DUPLEX_HALF;
  1137. macb_configure_dma(bp);
  1138. macb_configure_caps(bp);
  1139. /* Initialize TX and RX buffers */
  1140. macb_writel(bp, RBQP, bp->rx_ring_dma);
  1141. macb_writel(bp, TBQP, bp->tx_ring_dma);
  1142. /* Enable TX and RX */
  1143. macb_writel(bp, NCR, MACB_BIT(RE) | MACB_BIT(TE) | MACB_BIT(MPE));
  1144. /* Enable interrupts */
  1145. macb_writel(bp, IER, (MACB_RX_INT_FLAGS
  1146. | MACB_TX_INT_FLAGS
  1147. | MACB_BIT(HRESP)));
  1148. }
  1149. /*
  1150. * The hash address register is 64 bits long and takes up two
  1151. * locations in the memory map. The least significant bits are stored
  1152. * in EMAC_HSL and the most significant bits in EMAC_HSH.
  1153. *
  1154. * The unicast hash enable and the multicast hash enable bits in the
  1155. * network configuration register enable the reception of hash matched
  1156. * frames. The destination address is reduced to a 6 bit index into
  1157. * the 64 bit hash register using the following hash function. The
  1158. * hash function is an exclusive or of every sixth bit of the
  1159. * destination address.
  1160. *
  1161. * hi[5] = da[5] ^ da[11] ^ da[17] ^ da[23] ^ da[29] ^ da[35] ^ da[41] ^ da[47]
  1162. * hi[4] = da[4] ^ da[10] ^ da[16] ^ da[22] ^ da[28] ^ da[34] ^ da[40] ^ da[46]
  1163. * hi[3] = da[3] ^ da[09] ^ da[15] ^ da[21] ^ da[27] ^ da[33] ^ da[39] ^ da[45]
  1164. * hi[2] = da[2] ^ da[08] ^ da[14] ^ da[20] ^ da[26] ^ da[32] ^ da[38] ^ da[44]
  1165. * hi[1] = da[1] ^ da[07] ^ da[13] ^ da[19] ^ da[25] ^ da[31] ^ da[37] ^ da[43]
  1166. * hi[0] = da[0] ^ da[06] ^ da[12] ^ da[18] ^ da[24] ^ da[30] ^ da[36] ^ da[42]
  1167. *
  1168. * da[0] represents the least significant bit of the first byte
  1169. * received, that is, the multicast/unicast indicator, and da[47]
  1170. * represents the most significant bit of the last byte received. If
  1171. * the hash index, hi[n], points to a bit that is set in the hash
  1172. * register then the frame will be matched according to whether the
  1173. * frame is multicast or unicast. A multicast match will be signalled
  1174. * if the multicast hash enable bit is set, da[0] is 1 and the hash
  1175. * index points to a bit set in the hash register. A unicast match
  1176. * will be signalled if the unicast hash enable bit is set, da[0] is 0
  1177. * and the hash index points to a bit set in the hash register. To
  1178. * receive all multicast frames, the hash register should be set with
  1179. * all ones and the multicast hash enable bit should be set in the
  1180. * network configuration register.
  1181. */
  1182. static inline int hash_bit_value(int bitnr, __u8 *addr)
  1183. {
  1184. if (addr[bitnr / 8] & (1 << (bitnr % 8)))
  1185. return 1;
  1186. return 0;
  1187. }
  1188. /*
  1189. * Return the hash index value for the specified address.
  1190. */
  1191. static int hash_get_index(__u8 *addr)
  1192. {
  1193. int i, j, bitval;
  1194. int hash_index = 0;
  1195. for (j = 0; j < 6; j++) {
  1196. for (i = 0, bitval = 0; i < 8; i++)
  1197. bitval ^= hash_bit_value(i*6 + j, addr);
  1198. hash_index |= (bitval << j);
  1199. }
  1200. return hash_index;
  1201. }
  1202. /*
  1203. * Add multicast addresses to the internal multicast-hash table.
  1204. */
  1205. static void macb_sethashtable(struct net_device *dev)
  1206. {
  1207. struct netdev_hw_addr *ha;
  1208. unsigned long mc_filter[2];
  1209. unsigned int bitnr;
  1210. struct macb *bp = netdev_priv(dev);
  1211. mc_filter[0] = mc_filter[1] = 0;
  1212. netdev_for_each_mc_addr(ha, dev) {
  1213. bitnr = hash_get_index(ha->addr);
  1214. mc_filter[bitnr >> 5] |= 1 << (bitnr & 31);
  1215. }
  1216. macb_or_gem_writel(bp, HRB, mc_filter[0]);
  1217. macb_or_gem_writel(bp, HRT, mc_filter[1]);
  1218. }
  1219. /*
  1220. * Enable/Disable promiscuous and multicast modes.
  1221. */
  1222. void macb_set_rx_mode(struct net_device *dev)
  1223. {
  1224. unsigned long cfg;
  1225. struct macb *bp = netdev_priv(dev);
  1226. cfg = macb_readl(bp, NCFGR);
  1227. if (dev->flags & IFF_PROMISC)
  1228. /* Enable promiscuous mode */
  1229. cfg |= MACB_BIT(CAF);
  1230. else if (dev->flags & (~IFF_PROMISC))
  1231. /* Disable promiscuous mode */
  1232. cfg &= ~MACB_BIT(CAF);
  1233. if (dev->flags & IFF_ALLMULTI) {
  1234. /* Enable all multicast mode */
  1235. macb_or_gem_writel(bp, HRB, -1);
  1236. macb_or_gem_writel(bp, HRT, -1);
  1237. cfg |= MACB_BIT(NCFGR_MTI);
  1238. } else if (!netdev_mc_empty(dev)) {
  1239. /* Enable specific multicasts */
  1240. macb_sethashtable(dev);
  1241. cfg |= MACB_BIT(NCFGR_MTI);
  1242. } else if (dev->flags & (~IFF_ALLMULTI)) {
  1243. /* Disable all multicast mode */
  1244. macb_or_gem_writel(bp, HRB, 0);
  1245. macb_or_gem_writel(bp, HRT, 0);
  1246. cfg &= ~MACB_BIT(NCFGR_MTI);
  1247. }
  1248. macb_writel(bp, NCFGR, cfg);
  1249. }
  1250. EXPORT_SYMBOL_GPL(macb_set_rx_mode);
  1251. static int macb_open(struct net_device *dev)
  1252. {
  1253. struct macb *bp = netdev_priv(dev);
  1254. size_t bufsz = dev->mtu + ETH_HLEN + ETH_FCS_LEN + NET_IP_ALIGN;
  1255. int err;
  1256. netdev_dbg(bp->dev, "open\n");
  1257. /* carrier starts down */
  1258. netif_carrier_off(dev);
  1259. /* if the phy is not yet register, retry later*/
  1260. if (!bp->phy_dev)
  1261. return -EAGAIN;
  1262. /* RX buffers initialization */
  1263. macb_init_rx_buffer_size(bp, bufsz);
  1264. err = macb_alloc_consistent(bp);
  1265. if (err) {
  1266. netdev_err(dev, "Unable to allocate DMA memory (error %d)\n",
  1267. err);
  1268. return err;
  1269. }
  1270. napi_enable(&bp->napi);
  1271. bp->macbgem_ops.mog_init_rings(bp);
  1272. macb_init_hw(bp);
  1273. /* schedule a link state check */
  1274. phy_start(bp->phy_dev);
  1275. netif_start_queue(dev);
  1276. return 0;
  1277. }
  1278. static int macb_close(struct net_device *dev)
  1279. {
  1280. struct macb *bp = netdev_priv(dev);
  1281. unsigned long flags;
  1282. netif_stop_queue(dev);
  1283. napi_disable(&bp->napi);
  1284. if (bp->phy_dev)
  1285. phy_stop(bp->phy_dev);
  1286. spin_lock_irqsave(&bp->lock, flags);
  1287. macb_reset_hw(bp);
  1288. netif_carrier_off(dev);
  1289. spin_unlock_irqrestore(&bp->lock, flags);
  1290. macb_free_consistent(bp);
  1291. return 0;
  1292. }
  1293. static void gem_update_stats(struct macb *bp)
  1294. {
  1295. u32 __iomem *reg = bp->regs + GEM_OTX;
  1296. u32 *p = &bp->hw_stats.gem.tx_octets_31_0;
  1297. u32 *end = &bp->hw_stats.gem.rx_udp_checksum_errors + 1;
  1298. for (; p < end; p++, reg++)
  1299. *p += __raw_readl(reg);
  1300. }
  1301. static struct net_device_stats *gem_get_stats(struct macb *bp)
  1302. {
  1303. struct gem_stats *hwstat = &bp->hw_stats.gem;
  1304. struct net_device_stats *nstat = &bp->stats;
  1305. gem_update_stats(bp);
  1306. nstat->rx_errors = (hwstat->rx_frame_check_sequence_errors +
  1307. hwstat->rx_alignment_errors +
  1308. hwstat->rx_resource_errors +
  1309. hwstat->rx_overruns +
  1310. hwstat->rx_oversize_frames +
  1311. hwstat->rx_jabbers +
  1312. hwstat->rx_undersized_frames +
  1313. hwstat->rx_length_field_frame_errors);
  1314. nstat->tx_errors = (hwstat->tx_late_collisions +
  1315. hwstat->tx_excessive_collisions +
  1316. hwstat->tx_underrun +
  1317. hwstat->tx_carrier_sense_errors);
  1318. nstat->multicast = hwstat->rx_multicast_frames;
  1319. nstat->collisions = (hwstat->tx_single_collision_frames +
  1320. hwstat->tx_multiple_collision_frames +
  1321. hwstat->tx_excessive_collisions);
  1322. nstat->rx_length_errors = (hwstat->rx_oversize_frames +
  1323. hwstat->rx_jabbers +
  1324. hwstat->rx_undersized_frames +
  1325. hwstat->rx_length_field_frame_errors);
  1326. nstat->rx_over_errors = hwstat->rx_resource_errors;
  1327. nstat->rx_crc_errors = hwstat->rx_frame_check_sequence_errors;
  1328. nstat->rx_frame_errors = hwstat->rx_alignment_errors;
  1329. nstat->rx_fifo_errors = hwstat->rx_overruns;
  1330. nstat->tx_aborted_errors = hwstat->tx_excessive_collisions;
  1331. nstat->tx_carrier_errors = hwstat->tx_carrier_sense_errors;
  1332. nstat->tx_fifo_errors = hwstat->tx_underrun;
  1333. return nstat;
  1334. }
  1335. struct net_device_stats *macb_get_stats(struct net_device *dev)
  1336. {
  1337. struct macb *bp = netdev_priv(dev);
  1338. struct net_device_stats *nstat = &bp->stats;
  1339. struct macb_stats *hwstat = &bp->hw_stats.macb;
  1340. if (macb_is_gem(bp))
  1341. return gem_get_stats(bp);
  1342. /* read stats from hardware */
  1343. macb_update_stats(bp);
  1344. /* Convert HW stats into netdevice stats */
  1345. nstat->rx_errors = (hwstat->rx_fcs_errors +
  1346. hwstat->rx_align_errors +
  1347. hwstat->rx_resource_errors +
  1348. hwstat->rx_overruns +
  1349. hwstat->rx_oversize_pkts +
  1350. hwstat->rx_jabbers +
  1351. hwstat->rx_undersize_pkts +
  1352. hwstat->sqe_test_errors +
  1353. hwstat->rx_length_mismatch);
  1354. nstat->tx_errors = (hwstat->tx_late_cols +
  1355. hwstat->tx_excessive_cols +
  1356. hwstat->tx_underruns +
  1357. hwstat->tx_carrier_errors);
  1358. nstat->collisions = (hwstat->tx_single_cols +
  1359. hwstat->tx_multiple_cols +
  1360. hwstat->tx_excessive_cols);
  1361. nstat->rx_length_errors = (hwstat->rx_oversize_pkts +
  1362. hwstat->rx_jabbers +
  1363. hwstat->rx_undersize_pkts +
  1364. hwstat->rx_length_mismatch);
  1365. nstat->rx_over_errors = hwstat->rx_resource_errors +
  1366. hwstat->rx_overruns;
  1367. nstat->rx_crc_errors = hwstat->rx_fcs_errors;
  1368. nstat->rx_frame_errors = hwstat->rx_align_errors;
  1369. nstat->rx_fifo_errors = hwstat->rx_overruns;
  1370. /* XXX: What does "missed" mean? */
  1371. nstat->tx_aborted_errors = hwstat->tx_excessive_cols;
  1372. nstat->tx_carrier_errors = hwstat->tx_carrier_errors;
  1373. nstat->tx_fifo_errors = hwstat->tx_underruns;
  1374. /* Don't know about heartbeat or window errors... */
  1375. return nstat;
  1376. }
  1377. EXPORT_SYMBOL_GPL(macb_get_stats);
  1378. static int macb_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  1379. {
  1380. struct macb *bp = netdev_priv(dev);
  1381. struct phy_device *phydev = bp->phy_dev;
  1382. if (!phydev)
  1383. return -ENODEV;
  1384. return phy_ethtool_gset(phydev, cmd);
  1385. }
  1386. static int macb_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  1387. {
  1388. struct macb *bp = netdev_priv(dev);
  1389. struct phy_device *phydev = bp->phy_dev;
  1390. if (!phydev)
  1391. return -ENODEV;
  1392. return phy_ethtool_sset(phydev, cmd);
  1393. }
  1394. static int macb_get_regs_len(struct net_device *netdev)
  1395. {
  1396. return MACB_GREGS_NBR * sizeof(u32);
  1397. }
  1398. static void macb_get_regs(struct net_device *dev, struct ethtool_regs *regs,
  1399. void *p)
  1400. {
  1401. struct macb *bp = netdev_priv(dev);
  1402. unsigned int tail, head;
  1403. u32 *regs_buff = p;
  1404. regs->version = (macb_readl(bp, MID) & ((1 << MACB_REV_SIZE) - 1))
  1405. | MACB_GREGS_VERSION;
  1406. tail = macb_tx_ring_wrap(bp->tx_tail);
  1407. head = macb_tx_ring_wrap(bp->tx_head);
  1408. regs_buff[0] = macb_readl(bp, NCR);
  1409. regs_buff[1] = macb_or_gem_readl(bp, NCFGR);
  1410. regs_buff[2] = macb_readl(bp, NSR);
  1411. regs_buff[3] = macb_readl(bp, TSR);
  1412. regs_buff[4] = macb_readl(bp, RBQP);
  1413. regs_buff[5] = macb_readl(bp, TBQP);
  1414. regs_buff[6] = macb_readl(bp, RSR);
  1415. regs_buff[7] = macb_readl(bp, IMR);
  1416. regs_buff[8] = tail;
  1417. regs_buff[9] = head;
  1418. regs_buff[10] = macb_tx_dma(bp, tail);
  1419. regs_buff[11] = macb_tx_dma(bp, head);
  1420. if (macb_is_gem(bp)) {
  1421. regs_buff[12] = gem_readl(bp, USRIO);
  1422. regs_buff[13] = gem_readl(bp, DMACFG);
  1423. }
  1424. }
  1425. const struct ethtool_ops macb_ethtool_ops = {
  1426. .get_settings = macb_get_settings,
  1427. .set_settings = macb_set_settings,
  1428. .get_regs_len = macb_get_regs_len,
  1429. .get_regs = macb_get_regs,
  1430. .get_link = ethtool_op_get_link,
  1431. .get_ts_info = ethtool_op_get_ts_info,
  1432. };
  1433. EXPORT_SYMBOL_GPL(macb_ethtool_ops);
  1434. int macb_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
  1435. {
  1436. struct macb *bp = netdev_priv(dev);
  1437. struct phy_device *phydev = bp->phy_dev;
  1438. if (!netif_running(dev))
  1439. return -EINVAL;
  1440. if (!phydev)
  1441. return -ENODEV;
  1442. return phy_mii_ioctl(phydev, rq, cmd);
  1443. }
  1444. EXPORT_SYMBOL_GPL(macb_ioctl);
  1445. static const struct net_device_ops macb_netdev_ops = {
  1446. .ndo_open = macb_open,
  1447. .ndo_stop = macb_close,
  1448. .ndo_start_xmit = macb_start_xmit,
  1449. .ndo_set_rx_mode = macb_set_rx_mode,
  1450. .ndo_get_stats = macb_get_stats,
  1451. .ndo_do_ioctl = macb_ioctl,
  1452. .ndo_validate_addr = eth_validate_addr,
  1453. .ndo_change_mtu = eth_change_mtu,
  1454. .ndo_set_mac_address = eth_mac_addr,
  1455. #ifdef CONFIG_NET_POLL_CONTROLLER
  1456. .ndo_poll_controller = macb_poll_controller,
  1457. #endif
  1458. };
  1459. #if defined(CONFIG_OF)
  1460. static const struct of_device_id macb_dt_ids[] = {
  1461. { .compatible = "cdns,at32ap7000-macb" },
  1462. { .compatible = "cdns,at91sam9260-macb" },
  1463. { .compatible = "cdns,macb" },
  1464. { .compatible = "cdns,pc302-gem" },
  1465. { .compatible = "cdns,gem" },
  1466. { /* sentinel */ }
  1467. };
  1468. MODULE_DEVICE_TABLE(of, macb_dt_ids);
  1469. #endif
  1470. static int __init macb_probe(struct platform_device *pdev)
  1471. {
  1472. struct macb_platform_data *pdata;
  1473. struct resource *regs;
  1474. struct net_device *dev;
  1475. struct macb *bp;
  1476. struct phy_device *phydev;
  1477. u32 config;
  1478. int err = -ENXIO;
  1479. struct pinctrl *pinctrl;
  1480. const char *mac;
  1481. regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1482. if (!regs) {
  1483. dev_err(&pdev->dev, "no mmio resource defined\n");
  1484. goto err_out;
  1485. }
  1486. pinctrl = devm_pinctrl_get_select_default(&pdev->dev);
  1487. if (IS_ERR(pinctrl)) {
  1488. err = PTR_ERR(pinctrl);
  1489. if (err == -EPROBE_DEFER)
  1490. goto err_out;
  1491. dev_warn(&pdev->dev, "No pinctrl provided\n");
  1492. }
  1493. err = -ENOMEM;
  1494. dev = alloc_etherdev(sizeof(*bp));
  1495. if (!dev)
  1496. goto err_out;
  1497. SET_NETDEV_DEV(dev, &pdev->dev);
  1498. /* TODO: Actually, we have some interesting features... */
  1499. dev->features |= 0;
  1500. bp = netdev_priv(dev);
  1501. bp->pdev = pdev;
  1502. bp->dev = dev;
  1503. spin_lock_init(&bp->lock);
  1504. INIT_WORK(&bp->tx_error_task, macb_tx_error_task);
  1505. bp->pclk = devm_clk_get(&pdev->dev, "pclk");
  1506. if (IS_ERR(bp->pclk)) {
  1507. err = PTR_ERR(bp->pclk);
  1508. dev_err(&pdev->dev, "failed to get macb_clk (%u)\n", err);
  1509. goto err_out_free_dev;
  1510. }
  1511. bp->hclk = devm_clk_get(&pdev->dev, "hclk");
  1512. if (IS_ERR(bp->hclk)) {
  1513. err = PTR_ERR(bp->hclk);
  1514. dev_err(&pdev->dev, "failed to get hclk (%u)\n", err);
  1515. goto err_out_free_dev;
  1516. }
  1517. bp->tx_clk = devm_clk_get(&pdev->dev, "tx_clk");
  1518. err = clk_prepare_enable(bp->pclk);
  1519. if (err) {
  1520. dev_err(&pdev->dev, "failed to enable pclk (%u)\n", err);
  1521. goto err_out_free_dev;
  1522. }
  1523. err = clk_prepare_enable(bp->hclk);
  1524. if (err) {
  1525. dev_err(&pdev->dev, "failed to enable hclk (%u)\n", err);
  1526. goto err_out_disable_pclk;
  1527. }
  1528. if (!IS_ERR(bp->tx_clk)) {
  1529. err = clk_prepare_enable(bp->tx_clk);
  1530. if (err) {
  1531. dev_err(&pdev->dev, "failed to enable tx_clk (%u)\n",
  1532. err);
  1533. goto err_out_disable_hclk;
  1534. }
  1535. }
  1536. bp->regs = devm_ioremap(&pdev->dev, regs->start, resource_size(regs));
  1537. if (!bp->regs) {
  1538. dev_err(&pdev->dev, "failed to map registers, aborting.\n");
  1539. err = -ENOMEM;
  1540. goto err_out_disable_clocks;
  1541. }
  1542. dev->irq = platform_get_irq(pdev, 0);
  1543. err = devm_request_irq(&pdev->dev, dev->irq, macb_interrupt, 0,
  1544. dev->name, dev);
  1545. if (err) {
  1546. dev_err(&pdev->dev, "Unable to request IRQ %d (error %d)\n",
  1547. dev->irq, err);
  1548. goto err_out_disable_clocks;
  1549. }
  1550. dev->netdev_ops = &macb_netdev_ops;
  1551. netif_napi_add(dev, &bp->napi, macb_poll, 64);
  1552. dev->ethtool_ops = &macb_ethtool_ops;
  1553. dev->base_addr = regs->start;
  1554. /* setup appropriated routines according to adapter type */
  1555. if (macb_is_gem(bp)) {
  1556. bp->macbgem_ops.mog_alloc_rx_buffers = gem_alloc_rx_buffers;
  1557. bp->macbgem_ops.mog_free_rx_buffers = gem_free_rx_buffers;
  1558. bp->macbgem_ops.mog_init_rings = gem_init_rings;
  1559. bp->macbgem_ops.mog_rx = gem_rx;
  1560. } else {
  1561. bp->macbgem_ops.mog_alloc_rx_buffers = macb_alloc_rx_buffers;
  1562. bp->macbgem_ops.mog_free_rx_buffers = macb_free_rx_buffers;
  1563. bp->macbgem_ops.mog_init_rings = macb_init_rings;
  1564. bp->macbgem_ops.mog_rx = macb_rx;
  1565. }
  1566. /* Set MII management clock divider */
  1567. config = macb_mdc_clk_div(bp);
  1568. config |= macb_dbw(bp);
  1569. macb_writel(bp, NCFGR, config);
  1570. mac = of_get_mac_address(pdev->dev.of_node);
  1571. if (mac)
  1572. memcpy(bp->dev->dev_addr, mac, ETH_ALEN);
  1573. else
  1574. macb_get_hwaddr(bp);
  1575. err = of_get_phy_mode(pdev->dev.of_node);
  1576. if (err < 0) {
  1577. pdata = dev_get_platdata(&pdev->dev);
  1578. if (pdata && pdata->is_rmii)
  1579. bp->phy_interface = PHY_INTERFACE_MODE_RMII;
  1580. else
  1581. bp->phy_interface = PHY_INTERFACE_MODE_MII;
  1582. } else {
  1583. bp->phy_interface = err;
  1584. }
  1585. if (bp->phy_interface == PHY_INTERFACE_MODE_RGMII)
  1586. macb_or_gem_writel(bp, USRIO, GEM_BIT(RGMII));
  1587. else if (bp->phy_interface == PHY_INTERFACE_MODE_RMII)
  1588. #if defined(CONFIG_ARCH_AT91)
  1589. macb_or_gem_writel(bp, USRIO, (MACB_BIT(RMII) |
  1590. MACB_BIT(CLKEN)));
  1591. #else
  1592. macb_or_gem_writel(bp, USRIO, 0);
  1593. #endif
  1594. else
  1595. #if defined(CONFIG_ARCH_AT91)
  1596. macb_or_gem_writel(bp, USRIO, MACB_BIT(CLKEN));
  1597. #else
  1598. macb_or_gem_writel(bp, USRIO, MACB_BIT(MII));
  1599. #endif
  1600. err = register_netdev(dev);
  1601. if (err) {
  1602. dev_err(&pdev->dev, "Cannot register net device, aborting.\n");
  1603. goto err_out_disable_clocks;
  1604. }
  1605. err = macb_mii_init(bp);
  1606. if (err)
  1607. goto err_out_unregister_netdev;
  1608. platform_set_drvdata(pdev, dev);
  1609. netif_carrier_off(dev);
  1610. netdev_info(dev, "Cadence %s at 0x%08lx irq %d (%pM)\n",
  1611. macb_is_gem(bp) ? "GEM" : "MACB", dev->base_addr,
  1612. dev->irq, dev->dev_addr);
  1613. phydev = bp->phy_dev;
  1614. netdev_info(dev, "attached PHY driver [%s] (mii_bus:phy_addr=%s, irq=%d)\n",
  1615. phydev->drv->name, dev_name(&phydev->dev), phydev->irq);
  1616. return 0;
  1617. err_out_unregister_netdev:
  1618. unregister_netdev(dev);
  1619. err_out_disable_clocks:
  1620. if (!IS_ERR(bp->tx_clk))
  1621. clk_disable_unprepare(bp->tx_clk);
  1622. err_out_disable_hclk:
  1623. clk_disable_unprepare(bp->hclk);
  1624. err_out_disable_pclk:
  1625. clk_disable_unprepare(bp->pclk);
  1626. err_out_free_dev:
  1627. free_netdev(dev);
  1628. err_out:
  1629. return err;
  1630. }
  1631. static int __exit macb_remove(struct platform_device *pdev)
  1632. {
  1633. struct net_device *dev;
  1634. struct macb *bp;
  1635. dev = platform_get_drvdata(pdev);
  1636. if (dev) {
  1637. bp = netdev_priv(dev);
  1638. if (bp->phy_dev)
  1639. phy_disconnect(bp->phy_dev);
  1640. mdiobus_unregister(bp->mii_bus);
  1641. kfree(bp->mii_bus->irq);
  1642. mdiobus_free(bp->mii_bus);
  1643. unregister_netdev(dev);
  1644. if (!IS_ERR(bp->tx_clk))
  1645. clk_disable_unprepare(bp->tx_clk);
  1646. clk_disable_unprepare(bp->hclk);
  1647. clk_disable_unprepare(bp->pclk);
  1648. free_netdev(dev);
  1649. }
  1650. return 0;
  1651. }
  1652. #ifdef CONFIG_PM
  1653. static int macb_suspend(struct device *dev)
  1654. {
  1655. struct platform_device *pdev = to_platform_device(dev);
  1656. struct net_device *netdev = platform_get_drvdata(pdev);
  1657. struct macb *bp = netdev_priv(netdev);
  1658. netif_carrier_off(netdev);
  1659. netif_device_detach(netdev);
  1660. if (!IS_ERR(bp->tx_clk))
  1661. clk_disable_unprepare(bp->tx_clk);
  1662. clk_disable_unprepare(bp->hclk);
  1663. clk_disable_unprepare(bp->pclk);
  1664. return 0;
  1665. }
  1666. static int macb_resume(struct device *dev)
  1667. {
  1668. struct platform_device *pdev = to_platform_device(dev);
  1669. struct net_device *netdev = platform_get_drvdata(pdev);
  1670. struct macb *bp = netdev_priv(netdev);
  1671. clk_prepare_enable(bp->pclk);
  1672. clk_prepare_enable(bp->hclk);
  1673. if (!IS_ERR(bp->tx_clk))
  1674. clk_prepare_enable(bp->tx_clk);
  1675. netif_device_attach(netdev);
  1676. return 0;
  1677. }
  1678. #endif
  1679. static SIMPLE_DEV_PM_OPS(macb_pm_ops, macb_suspend, macb_resume);
  1680. static struct platform_driver macb_driver = {
  1681. .remove = __exit_p(macb_remove),
  1682. .driver = {
  1683. .name = "macb",
  1684. .owner = THIS_MODULE,
  1685. .of_match_table = of_match_ptr(macb_dt_ids),
  1686. .pm = &macb_pm_ops,
  1687. },
  1688. };
  1689. module_platform_driver_probe(macb_driver, macb_probe);
  1690. MODULE_LICENSE("GPL");
  1691. MODULE_DESCRIPTION("Cadence MACB/GEM Ethernet driver");
  1692. MODULE_AUTHOR("Haavard Skinnemoen (Atmel)");
  1693. MODULE_ALIAS("platform:macb");