macb.c 50 KB

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