ethoc.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330
  1. /*
  2. * linux/drivers/net/ethernet/ethoc.c
  3. *
  4. * Copyright (C) 2007-2008 Avionic Design Development GmbH
  5. * Copyright (C) 2008-2009 Avionic Design GmbH
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. *
  11. * Written by Thierry Reding <thierry.reding@avionic-design.de>
  12. */
  13. #include <linux/dma-mapping.h>
  14. #include <linux/etherdevice.h>
  15. #include <linux/clk.h>
  16. #include <linux/crc32.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/io.h>
  19. #include <linux/mii.h>
  20. #include <linux/phy.h>
  21. #include <linux/platform_device.h>
  22. #include <linux/sched.h>
  23. #include <linux/slab.h>
  24. #include <linux/of.h>
  25. #include <linux/module.h>
  26. #include <net/ethoc.h>
  27. static int buffer_size = 0x8000; /* 32 KBytes */
  28. module_param(buffer_size, int, 0);
  29. MODULE_PARM_DESC(buffer_size, "DMA buffer allocation size");
  30. /* register offsets */
  31. #define MODER 0x00
  32. #define INT_SOURCE 0x04
  33. #define INT_MASK 0x08
  34. #define IPGT 0x0c
  35. #define IPGR1 0x10
  36. #define IPGR2 0x14
  37. #define PACKETLEN 0x18
  38. #define COLLCONF 0x1c
  39. #define TX_BD_NUM 0x20
  40. #define CTRLMODER 0x24
  41. #define MIIMODER 0x28
  42. #define MIICOMMAND 0x2c
  43. #define MIIADDRESS 0x30
  44. #define MIITX_DATA 0x34
  45. #define MIIRX_DATA 0x38
  46. #define MIISTATUS 0x3c
  47. #define MAC_ADDR0 0x40
  48. #define MAC_ADDR1 0x44
  49. #define ETH_HASH0 0x48
  50. #define ETH_HASH1 0x4c
  51. #define ETH_TXCTRL 0x50
  52. #define ETH_END 0x54
  53. /* mode register */
  54. #define MODER_RXEN (1 << 0) /* receive enable */
  55. #define MODER_TXEN (1 << 1) /* transmit enable */
  56. #define MODER_NOPRE (1 << 2) /* no preamble */
  57. #define MODER_BRO (1 << 3) /* broadcast address */
  58. #define MODER_IAM (1 << 4) /* individual address mode */
  59. #define MODER_PRO (1 << 5) /* promiscuous mode */
  60. #define MODER_IFG (1 << 6) /* interframe gap for incoming frames */
  61. #define MODER_LOOP (1 << 7) /* loopback */
  62. #define MODER_NBO (1 << 8) /* no back-off */
  63. #define MODER_EDE (1 << 9) /* excess defer enable */
  64. #define MODER_FULLD (1 << 10) /* full duplex */
  65. #define MODER_RESET (1 << 11) /* FIXME: reset (undocumented) */
  66. #define MODER_DCRC (1 << 12) /* delayed CRC enable */
  67. #define MODER_CRC (1 << 13) /* CRC enable */
  68. #define MODER_HUGE (1 << 14) /* huge packets enable */
  69. #define MODER_PAD (1 << 15) /* padding enabled */
  70. #define MODER_RSM (1 << 16) /* receive small packets */
  71. /* interrupt source and mask registers */
  72. #define INT_MASK_TXF (1 << 0) /* transmit frame */
  73. #define INT_MASK_TXE (1 << 1) /* transmit error */
  74. #define INT_MASK_RXF (1 << 2) /* receive frame */
  75. #define INT_MASK_RXE (1 << 3) /* receive error */
  76. #define INT_MASK_BUSY (1 << 4)
  77. #define INT_MASK_TXC (1 << 5) /* transmit control frame */
  78. #define INT_MASK_RXC (1 << 6) /* receive control frame */
  79. #define INT_MASK_TX (INT_MASK_TXF | INT_MASK_TXE)
  80. #define INT_MASK_RX (INT_MASK_RXF | INT_MASK_RXE)
  81. #define INT_MASK_ALL ( \
  82. INT_MASK_TXF | INT_MASK_TXE | \
  83. INT_MASK_RXF | INT_MASK_RXE | \
  84. INT_MASK_TXC | INT_MASK_RXC | \
  85. INT_MASK_BUSY \
  86. )
  87. /* packet length register */
  88. #define PACKETLEN_MIN(min) (((min) & 0xffff) << 16)
  89. #define PACKETLEN_MAX(max) (((max) & 0xffff) << 0)
  90. #define PACKETLEN_MIN_MAX(min, max) (PACKETLEN_MIN(min) | \
  91. PACKETLEN_MAX(max))
  92. /* transmit buffer number register */
  93. #define TX_BD_NUM_VAL(x) (((x) <= 0x80) ? (x) : 0x80)
  94. /* control module mode register */
  95. #define CTRLMODER_PASSALL (1 << 0) /* pass all receive frames */
  96. #define CTRLMODER_RXFLOW (1 << 1) /* receive control flow */
  97. #define CTRLMODER_TXFLOW (1 << 2) /* transmit control flow */
  98. /* MII mode register */
  99. #define MIIMODER_CLKDIV(x) ((x) & 0xfe) /* needs to be an even number */
  100. #define MIIMODER_NOPRE (1 << 8) /* no preamble */
  101. /* MII command register */
  102. #define MIICOMMAND_SCAN (1 << 0) /* scan status */
  103. #define MIICOMMAND_READ (1 << 1) /* read status */
  104. #define MIICOMMAND_WRITE (1 << 2) /* write control data */
  105. /* MII address register */
  106. #define MIIADDRESS_FIAD(x) (((x) & 0x1f) << 0)
  107. #define MIIADDRESS_RGAD(x) (((x) & 0x1f) << 8)
  108. #define MIIADDRESS_ADDR(phy, reg) (MIIADDRESS_FIAD(phy) | \
  109. MIIADDRESS_RGAD(reg))
  110. /* MII transmit data register */
  111. #define MIITX_DATA_VAL(x) ((x) & 0xffff)
  112. /* MII receive data register */
  113. #define MIIRX_DATA_VAL(x) ((x) & 0xffff)
  114. /* MII status register */
  115. #define MIISTATUS_LINKFAIL (1 << 0)
  116. #define MIISTATUS_BUSY (1 << 1)
  117. #define MIISTATUS_INVALID (1 << 2)
  118. /* TX buffer descriptor */
  119. #define TX_BD_CS (1 << 0) /* carrier sense lost */
  120. #define TX_BD_DF (1 << 1) /* defer indication */
  121. #define TX_BD_LC (1 << 2) /* late collision */
  122. #define TX_BD_RL (1 << 3) /* retransmission limit */
  123. #define TX_BD_RETRY_MASK (0x00f0)
  124. #define TX_BD_RETRY(x) (((x) & 0x00f0) >> 4)
  125. #define TX_BD_UR (1 << 8) /* transmitter underrun */
  126. #define TX_BD_CRC (1 << 11) /* TX CRC enable */
  127. #define TX_BD_PAD (1 << 12) /* pad enable for short packets */
  128. #define TX_BD_WRAP (1 << 13)
  129. #define TX_BD_IRQ (1 << 14) /* interrupt request enable */
  130. #define TX_BD_READY (1 << 15) /* TX buffer ready */
  131. #define TX_BD_LEN(x) (((x) & 0xffff) << 16)
  132. #define TX_BD_LEN_MASK (0xffff << 16)
  133. #define TX_BD_STATS (TX_BD_CS | TX_BD_DF | TX_BD_LC | \
  134. TX_BD_RL | TX_BD_RETRY_MASK | TX_BD_UR)
  135. /* RX buffer descriptor */
  136. #define RX_BD_LC (1 << 0) /* late collision */
  137. #define RX_BD_CRC (1 << 1) /* RX CRC error */
  138. #define RX_BD_SF (1 << 2) /* short frame */
  139. #define RX_BD_TL (1 << 3) /* too long */
  140. #define RX_BD_DN (1 << 4) /* dribble nibble */
  141. #define RX_BD_IS (1 << 5) /* invalid symbol */
  142. #define RX_BD_OR (1 << 6) /* receiver overrun */
  143. #define RX_BD_MISS (1 << 7)
  144. #define RX_BD_CF (1 << 8) /* control frame */
  145. #define RX_BD_WRAP (1 << 13)
  146. #define RX_BD_IRQ (1 << 14) /* interrupt request enable */
  147. #define RX_BD_EMPTY (1 << 15)
  148. #define RX_BD_LEN(x) (((x) & 0xffff) << 16)
  149. #define RX_BD_STATS (RX_BD_LC | RX_BD_CRC | RX_BD_SF | RX_BD_TL | \
  150. RX_BD_DN | RX_BD_IS | RX_BD_OR | RX_BD_MISS)
  151. #define ETHOC_BUFSIZ 1536
  152. #define ETHOC_ZLEN 64
  153. #define ETHOC_BD_BASE 0x400
  154. #define ETHOC_TIMEOUT (HZ / 2)
  155. #define ETHOC_MII_TIMEOUT (1 + (HZ / 5))
  156. /**
  157. * struct ethoc - driver-private device structure
  158. * @iobase: pointer to I/O memory region
  159. * @membase: pointer to buffer memory region
  160. * @dma_alloc: dma allocated buffer size
  161. * @io_region_size: I/O memory region size
  162. * @num_bd: number of buffer descriptors
  163. * @num_tx: number of send buffers
  164. * @cur_tx: last send buffer written
  165. * @dty_tx: last buffer actually sent
  166. * @num_rx: number of receive buffers
  167. * @cur_rx: current receive buffer
  168. * @vma: pointer to array of virtual memory addresses for buffers
  169. * @netdev: pointer to network device structure
  170. * @napi: NAPI structure
  171. * @msg_enable: device state flags
  172. * @lock: device lock
  173. * @phy: attached PHY
  174. * @mdio: MDIO bus for PHY access
  175. * @phy_id: address of attached PHY
  176. */
  177. struct ethoc {
  178. void __iomem *iobase;
  179. void __iomem *membase;
  180. int dma_alloc;
  181. resource_size_t io_region_size;
  182. bool big_endian;
  183. unsigned int num_bd;
  184. unsigned int num_tx;
  185. unsigned int cur_tx;
  186. unsigned int dty_tx;
  187. unsigned int num_rx;
  188. unsigned int cur_rx;
  189. void **vma;
  190. struct net_device *netdev;
  191. struct napi_struct napi;
  192. u32 msg_enable;
  193. spinlock_t lock;
  194. struct phy_device *phy;
  195. struct mii_bus *mdio;
  196. struct clk *clk;
  197. s8 phy_id;
  198. };
  199. /**
  200. * struct ethoc_bd - buffer descriptor
  201. * @stat: buffer statistics
  202. * @addr: physical memory address
  203. */
  204. struct ethoc_bd {
  205. u32 stat;
  206. u32 addr;
  207. };
  208. static inline u32 ethoc_read(struct ethoc *dev, loff_t offset)
  209. {
  210. if (dev->big_endian)
  211. return ioread32be(dev->iobase + offset);
  212. else
  213. return ioread32(dev->iobase + offset);
  214. }
  215. static inline void ethoc_write(struct ethoc *dev, loff_t offset, u32 data)
  216. {
  217. if (dev->big_endian)
  218. iowrite32be(data, dev->iobase + offset);
  219. else
  220. iowrite32(data, dev->iobase + offset);
  221. }
  222. static inline void ethoc_read_bd(struct ethoc *dev, int index,
  223. struct ethoc_bd *bd)
  224. {
  225. loff_t offset = ETHOC_BD_BASE + (index * sizeof(struct ethoc_bd));
  226. bd->stat = ethoc_read(dev, offset + 0);
  227. bd->addr = ethoc_read(dev, offset + 4);
  228. }
  229. static inline void ethoc_write_bd(struct ethoc *dev, int index,
  230. const struct ethoc_bd *bd)
  231. {
  232. loff_t offset = ETHOC_BD_BASE + (index * sizeof(struct ethoc_bd));
  233. ethoc_write(dev, offset + 0, bd->stat);
  234. ethoc_write(dev, offset + 4, bd->addr);
  235. }
  236. static inline void ethoc_enable_irq(struct ethoc *dev, u32 mask)
  237. {
  238. u32 imask = ethoc_read(dev, INT_MASK);
  239. imask |= mask;
  240. ethoc_write(dev, INT_MASK, imask);
  241. }
  242. static inline void ethoc_disable_irq(struct ethoc *dev, u32 mask)
  243. {
  244. u32 imask = ethoc_read(dev, INT_MASK);
  245. imask &= ~mask;
  246. ethoc_write(dev, INT_MASK, imask);
  247. }
  248. static inline void ethoc_ack_irq(struct ethoc *dev, u32 mask)
  249. {
  250. ethoc_write(dev, INT_SOURCE, mask);
  251. }
  252. static inline void ethoc_enable_rx_and_tx(struct ethoc *dev)
  253. {
  254. u32 mode = ethoc_read(dev, MODER);
  255. mode |= MODER_RXEN | MODER_TXEN;
  256. ethoc_write(dev, MODER, mode);
  257. }
  258. static inline void ethoc_disable_rx_and_tx(struct ethoc *dev)
  259. {
  260. u32 mode = ethoc_read(dev, MODER);
  261. mode &= ~(MODER_RXEN | MODER_TXEN);
  262. ethoc_write(dev, MODER, mode);
  263. }
  264. static int ethoc_init_ring(struct ethoc *dev, unsigned long mem_start)
  265. {
  266. struct ethoc_bd bd;
  267. int i;
  268. void *vma;
  269. dev->cur_tx = 0;
  270. dev->dty_tx = 0;
  271. dev->cur_rx = 0;
  272. ethoc_write(dev, TX_BD_NUM, dev->num_tx);
  273. /* setup transmission buffers */
  274. bd.addr = mem_start;
  275. bd.stat = TX_BD_IRQ | TX_BD_CRC;
  276. vma = dev->membase;
  277. for (i = 0; i < dev->num_tx; i++) {
  278. if (i == dev->num_tx - 1)
  279. bd.stat |= TX_BD_WRAP;
  280. ethoc_write_bd(dev, i, &bd);
  281. bd.addr += ETHOC_BUFSIZ;
  282. dev->vma[i] = vma;
  283. vma += ETHOC_BUFSIZ;
  284. }
  285. bd.stat = RX_BD_EMPTY | RX_BD_IRQ;
  286. for (i = 0; i < dev->num_rx; i++) {
  287. if (i == dev->num_rx - 1)
  288. bd.stat |= RX_BD_WRAP;
  289. ethoc_write_bd(dev, dev->num_tx + i, &bd);
  290. bd.addr += ETHOC_BUFSIZ;
  291. dev->vma[dev->num_tx + i] = vma;
  292. vma += ETHOC_BUFSIZ;
  293. }
  294. return 0;
  295. }
  296. static int ethoc_reset(struct ethoc *dev)
  297. {
  298. u32 mode;
  299. /* TODO: reset controller? */
  300. ethoc_disable_rx_and_tx(dev);
  301. /* TODO: setup registers */
  302. /* enable FCS generation and automatic padding */
  303. mode = ethoc_read(dev, MODER);
  304. mode |= MODER_CRC | MODER_PAD;
  305. ethoc_write(dev, MODER, mode);
  306. /* set full-duplex mode */
  307. mode = ethoc_read(dev, MODER);
  308. mode |= MODER_FULLD;
  309. ethoc_write(dev, MODER, mode);
  310. ethoc_write(dev, IPGT, 0x15);
  311. ethoc_ack_irq(dev, INT_MASK_ALL);
  312. ethoc_enable_irq(dev, INT_MASK_ALL);
  313. ethoc_enable_rx_and_tx(dev);
  314. return 0;
  315. }
  316. static unsigned int ethoc_update_rx_stats(struct ethoc *dev,
  317. struct ethoc_bd *bd)
  318. {
  319. struct net_device *netdev = dev->netdev;
  320. unsigned int ret = 0;
  321. if (bd->stat & RX_BD_TL) {
  322. dev_err(&netdev->dev, "RX: frame too long\n");
  323. netdev->stats.rx_length_errors++;
  324. ret++;
  325. }
  326. if (bd->stat & RX_BD_SF) {
  327. dev_err(&netdev->dev, "RX: frame too short\n");
  328. netdev->stats.rx_length_errors++;
  329. ret++;
  330. }
  331. if (bd->stat & RX_BD_DN) {
  332. dev_err(&netdev->dev, "RX: dribble nibble\n");
  333. netdev->stats.rx_frame_errors++;
  334. }
  335. if (bd->stat & RX_BD_CRC) {
  336. dev_err(&netdev->dev, "RX: wrong CRC\n");
  337. netdev->stats.rx_crc_errors++;
  338. ret++;
  339. }
  340. if (bd->stat & RX_BD_OR) {
  341. dev_err(&netdev->dev, "RX: overrun\n");
  342. netdev->stats.rx_over_errors++;
  343. ret++;
  344. }
  345. if (bd->stat & RX_BD_MISS)
  346. netdev->stats.rx_missed_errors++;
  347. if (bd->stat & RX_BD_LC) {
  348. dev_err(&netdev->dev, "RX: late collision\n");
  349. netdev->stats.collisions++;
  350. ret++;
  351. }
  352. return ret;
  353. }
  354. static int ethoc_rx(struct net_device *dev, int limit)
  355. {
  356. struct ethoc *priv = netdev_priv(dev);
  357. int count;
  358. for (count = 0; count < limit; ++count) {
  359. unsigned int entry;
  360. struct ethoc_bd bd;
  361. entry = priv->num_tx + priv->cur_rx;
  362. ethoc_read_bd(priv, entry, &bd);
  363. if (bd.stat & RX_BD_EMPTY) {
  364. ethoc_ack_irq(priv, INT_MASK_RX);
  365. /* If packet (interrupt) came in between checking
  366. * BD_EMTPY and clearing the interrupt source, then we
  367. * risk missing the packet as the RX interrupt won't
  368. * trigger right away when we reenable it; hence, check
  369. * BD_EMTPY here again to make sure there isn't such a
  370. * packet waiting for us...
  371. */
  372. ethoc_read_bd(priv, entry, &bd);
  373. if (bd.stat & RX_BD_EMPTY)
  374. break;
  375. }
  376. if (ethoc_update_rx_stats(priv, &bd) == 0) {
  377. int size = bd.stat >> 16;
  378. struct sk_buff *skb;
  379. size -= 4; /* strip the CRC */
  380. skb = netdev_alloc_skb_ip_align(dev, size);
  381. if (likely(skb)) {
  382. void *src = priv->vma[entry];
  383. memcpy_fromio(skb_put(skb, size), src, size);
  384. skb->protocol = eth_type_trans(skb, dev);
  385. dev->stats.rx_packets++;
  386. dev->stats.rx_bytes += size;
  387. netif_receive_skb(skb);
  388. } else {
  389. if (net_ratelimit())
  390. dev_warn(&dev->dev,
  391. "low on memory - packet dropped\n");
  392. dev->stats.rx_dropped++;
  393. break;
  394. }
  395. }
  396. /* clear the buffer descriptor so it can be reused */
  397. bd.stat &= ~RX_BD_STATS;
  398. bd.stat |= RX_BD_EMPTY;
  399. ethoc_write_bd(priv, entry, &bd);
  400. if (++priv->cur_rx == priv->num_rx)
  401. priv->cur_rx = 0;
  402. }
  403. return count;
  404. }
  405. static void ethoc_update_tx_stats(struct ethoc *dev, struct ethoc_bd *bd)
  406. {
  407. struct net_device *netdev = dev->netdev;
  408. if (bd->stat & TX_BD_LC) {
  409. dev_err(&netdev->dev, "TX: late collision\n");
  410. netdev->stats.tx_window_errors++;
  411. }
  412. if (bd->stat & TX_BD_RL) {
  413. dev_err(&netdev->dev, "TX: retransmit limit\n");
  414. netdev->stats.tx_aborted_errors++;
  415. }
  416. if (bd->stat & TX_BD_UR) {
  417. dev_err(&netdev->dev, "TX: underrun\n");
  418. netdev->stats.tx_fifo_errors++;
  419. }
  420. if (bd->stat & TX_BD_CS) {
  421. dev_err(&netdev->dev, "TX: carrier sense lost\n");
  422. netdev->stats.tx_carrier_errors++;
  423. }
  424. if (bd->stat & TX_BD_STATS)
  425. netdev->stats.tx_errors++;
  426. netdev->stats.collisions += (bd->stat >> 4) & 0xf;
  427. netdev->stats.tx_bytes += bd->stat >> 16;
  428. netdev->stats.tx_packets++;
  429. }
  430. static int ethoc_tx(struct net_device *dev, int limit)
  431. {
  432. struct ethoc *priv = netdev_priv(dev);
  433. int count;
  434. struct ethoc_bd bd;
  435. for (count = 0; count < limit; ++count) {
  436. unsigned int entry;
  437. entry = priv->dty_tx & (priv->num_tx-1);
  438. ethoc_read_bd(priv, entry, &bd);
  439. if (bd.stat & TX_BD_READY || (priv->dty_tx == priv->cur_tx)) {
  440. ethoc_ack_irq(priv, INT_MASK_TX);
  441. /* If interrupt came in between reading in the BD
  442. * and clearing the interrupt source, then we risk
  443. * missing the event as the TX interrupt won't trigger
  444. * right away when we reenable it; hence, check
  445. * BD_EMPTY here again to make sure there isn't such an
  446. * event pending...
  447. */
  448. ethoc_read_bd(priv, entry, &bd);
  449. if (bd.stat & TX_BD_READY ||
  450. (priv->dty_tx == priv->cur_tx))
  451. break;
  452. }
  453. ethoc_update_tx_stats(priv, &bd);
  454. priv->dty_tx++;
  455. }
  456. if ((priv->cur_tx - priv->dty_tx) <= (priv->num_tx / 2))
  457. netif_wake_queue(dev);
  458. return count;
  459. }
  460. static irqreturn_t ethoc_interrupt(int irq, void *dev_id)
  461. {
  462. struct net_device *dev = dev_id;
  463. struct ethoc *priv = netdev_priv(dev);
  464. u32 pending;
  465. u32 mask;
  466. /* Figure out what triggered the interrupt...
  467. * The tricky bit here is that the interrupt source bits get
  468. * set in INT_SOURCE for an event regardless of whether that
  469. * event is masked or not. Thus, in order to figure out what
  470. * triggered the interrupt, we need to remove the sources
  471. * for all events that are currently masked. This behaviour
  472. * is not particularly well documented but reasonable...
  473. */
  474. mask = ethoc_read(priv, INT_MASK);
  475. pending = ethoc_read(priv, INT_SOURCE);
  476. pending &= mask;
  477. if (unlikely(pending == 0))
  478. return IRQ_NONE;
  479. ethoc_ack_irq(priv, pending);
  480. /* We always handle the dropped packet interrupt */
  481. if (pending & INT_MASK_BUSY) {
  482. dev_err(&dev->dev, "packet dropped\n");
  483. dev->stats.rx_dropped++;
  484. }
  485. /* Handle receive/transmit event by switching to polling */
  486. if (pending & (INT_MASK_TX | INT_MASK_RX)) {
  487. ethoc_disable_irq(priv, INT_MASK_TX | INT_MASK_RX);
  488. napi_schedule(&priv->napi);
  489. }
  490. return IRQ_HANDLED;
  491. }
  492. static int ethoc_get_mac_address(struct net_device *dev, void *addr)
  493. {
  494. struct ethoc *priv = netdev_priv(dev);
  495. u8 *mac = (u8 *)addr;
  496. u32 reg;
  497. reg = ethoc_read(priv, MAC_ADDR0);
  498. mac[2] = (reg >> 24) & 0xff;
  499. mac[3] = (reg >> 16) & 0xff;
  500. mac[4] = (reg >> 8) & 0xff;
  501. mac[5] = (reg >> 0) & 0xff;
  502. reg = ethoc_read(priv, MAC_ADDR1);
  503. mac[0] = (reg >> 8) & 0xff;
  504. mac[1] = (reg >> 0) & 0xff;
  505. return 0;
  506. }
  507. static int ethoc_poll(struct napi_struct *napi, int budget)
  508. {
  509. struct ethoc *priv = container_of(napi, struct ethoc, napi);
  510. int rx_work_done = 0;
  511. int tx_work_done = 0;
  512. rx_work_done = ethoc_rx(priv->netdev, budget);
  513. tx_work_done = ethoc_tx(priv->netdev, budget);
  514. if (rx_work_done < budget && tx_work_done < budget) {
  515. napi_complete(napi);
  516. ethoc_enable_irq(priv, INT_MASK_TX | INT_MASK_RX);
  517. }
  518. return rx_work_done;
  519. }
  520. static int ethoc_mdio_read(struct mii_bus *bus, int phy, int reg)
  521. {
  522. struct ethoc *priv = bus->priv;
  523. int i;
  524. ethoc_write(priv, MIIADDRESS, MIIADDRESS_ADDR(phy, reg));
  525. ethoc_write(priv, MIICOMMAND, MIICOMMAND_READ);
  526. for (i = 0; i < 5; i++) {
  527. u32 status = ethoc_read(priv, MIISTATUS);
  528. if (!(status & MIISTATUS_BUSY)) {
  529. u32 data = ethoc_read(priv, MIIRX_DATA);
  530. /* reset MII command register */
  531. ethoc_write(priv, MIICOMMAND, 0);
  532. return data;
  533. }
  534. usleep_range(100, 200);
  535. }
  536. return -EBUSY;
  537. }
  538. static int ethoc_mdio_write(struct mii_bus *bus, int phy, int reg, u16 val)
  539. {
  540. struct ethoc *priv = bus->priv;
  541. int i;
  542. ethoc_write(priv, MIIADDRESS, MIIADDRESS_ADDR(phy, reg));
  543. ethoc_write(priv, MIITX_DATA, val);
  544. ethoc_write(priv, MIICOMMAND, MIICOMMAND_WRITE);
  545. for (i = 0; i < 5; i++) {
  546. u32 stat = ethoc_read(priv, MIISTATUS);
  547. if (!(stat & MIISTATUS_BUSY)) {
  548. /* reset MII command register */
  549. ethoc_write(priv, MIICOMMAND, 0);
  550. return 0;
  551. }
  552. usleep_range(100, 200);
  553. }
  554. return -EBUSY;
  555. }
  556. static void ethoc_mdio_poll(struct net_device *dev)
  557. {
  558. }
  559. static int ethoc_mdio_probe(struct net_device *dev)
  560. {
  561. struct ethoc *priv = netdev_priv(dev);
  562. struct phy_device *phy;
  563. int err;
  564. if (priv->phy_id != -1)
  565. phy = priv->mdio->phy_map[priv->phy_id];
  566. else
  567. phy = phy_find_first(priv->mdio);
  568. if (!phy) {
  569. dev_err(&dev->dev, "no PHY found\n");
  570. return -ENXIO;
  571. }
  572. err = phy_connect_direct(dev, phy, ethoc_mdio_poll,
  573. PHY_INTERFACE_MODE_GMII);
  574. if (err) {
  575. dev_err(&dev->dev, "could not attach to PHY\n");
  576. return err;
  577. }
  578. priv->phy = phy;
  579. phy->advertising &= ~(ADVERTISED_1000baseT_Full |
  580. ADVERTISED_1000baseT_Half);
  581. phy->supported &= ~(SUPPORTED_1000baseT_Full |
  582. SUPPORTED_1000baseT_Half);
  583. return 0;
  584. }
  585. static int ethoc_open(struct net_device *dev)
  586. {
  587. struct ethoc *priv = netdev_priv(dev);
  588. int ret;
  589. ret = request_irq(dev->irq, ethoc_interrupt, IRQF_SHARED,
  590. dev->name, dev);
  591. if (ret)
  592. return ret;
  593. napi_enable(&priv->napi);
  594. ethoc_init_ring(priv, dev->mem_start);
  595. ethoc_reset(priv);
  596. if (netif_queue_stopped(dev)) {
  597. dev_dbg(&dev->dev, " resuming queue\n");
  598. netif_wake_queue(dev);
  599. } else {
  600. dev_dbg(&dev->dev, " starting queue\n");
  601. netif_start_queue(dev);
  602. }
  603. phy_start(priv->phy);
  604. if (netif_msg_ifup(priv)) {
  605. dev_info(&dev->dev, "I/O: %08lx Memory: %08lx-%08lx\n",
  606. dev->base_addr, dev->mem_start, dev->mem_end);
  607. }
  608. return 0;
  609. }
  610. static int ethoc_stop(struct net_device *dev)
  611. {
  612. struct ethoc *priv = netdev_priv(dev);
  613. napi_disable(&priv->napi);
  614. if (priv->phy)
  615. phy_stop(priv->phy);
  616. ethoc_disable_rx_and_tx(priv);
  617. free_irq(dev->irq, dev);
  618. if (!netif_queue_stopped(dev))
  619. netif_stop_queue(dev);
  620. return 0;
  621. }
  622. static int ethoc_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
  623. {
  624. struct ethoc *priv = netdev_priv(dev);
  625. struct mii_ioctl_data *mdio = if_mii(ifr);
  626. struct phy_device *phy = NULL;
  627. if (!netif_running(dev))
  628. return -EINVAL;
  629. if (cmd != SIOCGMIIPHY) {
  630. if (mdio->phy_id >= PHY_MAX_ADDR)
  631. return -ERANGE;
  632. phy = priv->mdio->phy_map[mdio->phy_id];
  633. if (!phy)
  634. return -ENODEV;
  635. } else {
  636. phy = priv->phy;
  637. }
  638. return phy_mii_ioctl(phy, ifr, cmd);
  639. }
  640. static void ethoc_do_set_mac_address(struct net_device *dev)
  641. {
  642. struct ethoc *priv = netdev_priv(dev);
  643. unsigned char *mac = dev->dev_addr;
  644. ethoc_write(priv, MAC_ADDR0, (mac[2] << 24) | (mac[3] << 16) |
  645. (mac[4] << 8) | (mac[5] << 0));
  646. ethoc_write(priv, MAC_ADDR1, (mac[0] << 8) | (mac[1] << 0));
  647. }
  648. static int ethoc_set_mac_address(struct net_device *dev, void *p)
  649. {
  650. const struct sockaddr *addr = p;
  651. if (!is_valid_ether_addr(addr->sa_data))
  652. return -EADDRNOTAVAIL;
  653. memcpy(dev->dev_addr, addr->sa_data, ETH_ALEN);
  654. ethoc_do_set_mac_address(dev);
  655. return 0;
  656. }
  657. static void ethoc_set_multicast_list(struct net_device *dev)
  658. {
  659. struct ethoc *priv = netdev_priv(dev);
  660. u32 mode = ethoc_read(priv, MODER);
  661. struct netdev_hw_addr *ha;
  662. u32 hash[2] = { 0, 0 };
  663. /* set loopback mode if requested */
  664. if (dev->flags & IFF_LOOPBACK)
  665. mode |= MODER_LOOP;
  666. else
  667. mode &= ~MODER_LOOP;
  668. /* receive broadcast frames if requested */
  669. if (dev->flags & IFF_BROADCAST)
  670. mode &= ~MODER_BRO;
  671. else
  672. mode |= MODER_BRO;
  673. /* enable promiscuous mode if requested */
  674. if (dev->flags & IFF_PROMISC)
  675. mode |= MODER_PRO;
  676. else
  677. mode &= ~MODER_PRO;
  678. ethoc_write(priv, MODER, mode);
  679. /* receive multicast frames */
  680. if (dev->flags & IFF_ALLMULTI) {
  681. hash[0] = 0xffffffff;
  682. hash[1] = 0xffffffff;
  683. } else {
  684. netdev_for_each_mc_addr(ha, dev) {
  685. u32 crc = ether_crc(ETH_ALEN, ha->addr);
  686. int bit = (crc >> 26) & 0x3f;
  687. hash[bit >> 5] |= 1 << (bit & 0x1f);
  688. }
  689. }
  690. ethoc_write(priv, ETH_HASH0, hash[0]);
  691. ethoc_write(priv, ETH_HASH1, hash[1]);
  692. }
  693. static int ethoc_change_mtu(struct net_device *dev, int new_mtu)
  694. {
  695. return -ENOSYS;
  696. }
  697. static void ethoc_tx_timeout(struct net_device *dev)
  698. {
  699. struct ethoc *priv = netdev_priv(dev);
  700. u32 pending = ethoc_read(priv, INT_SOURCE);
  701. if (likely(pending))
  702. ethoc_interrupt(dev->irq, dev);
  703. }
  704. static netdev_tx_t ethoc_start_xmit(struct sk_buff *skb, struct net_device *dev)
  705. {
  706. struct ethoc *priv = netdev_priv(dev);
  707. struct ethoc_bd bd;
  708. unsigned int entry;
  709. void *dest;
  710. if (unlikely(skb->len > ETHOC_BUFSIZ)) {
  711. dev->stats.tx_errors++;
  712. goto out;
  713. }
  714. entry = priv->cur_tx % priv->num_tx;
  715. spin_lock_irq(&priv->lock);
  716. priv->cur_tx++;
  717. ethoc_read_bd(priv, entry, &bd);
  718. if (unlikely(skb->len < ETHOC_ZLEN))
  719. bd.stat |= TX_BD_PAD;
  720. else
  721. bd.stat &= ~TX_BD_PAD;
  722. dest = priv->vma[entry];
  723. memcpy_toio(dest, skb->data, skb->len);
  724. bd.stat &= ~(TX_BD_STATS | TX_BD_LEN_MASK);
  725. bd.stat |= TX_BD_LEN(skb->len);
  726. ethoc_write_bd(priv, entry, &bd);
  727. bd.stat |= TX_BD_READY;
  728. ethoc_write_bd(priv, entry, &bd);
  729. if (priv->cur_tx == (priv->dty_tx + priv->num_tx)) {
  730. dev_dbg(&dev->dev, "stopping queue\n");
  731. netif_stop_queue(dev);
  732. }
  733. spin_unlock_irq(&priv->lock);
  734. skb_tx_timestamp(skb);
  735. out:
  736. dev_kfree_skb(skb);
  737. return NETDEV_TX_OK;
  738. }
  739. static int ethoc_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  740. {
  741. struct ethoc *priv = netdev_priv(dev);
  742. struct phy_device *phydev = priv->phy;
  743. if (!phydev)
  744. return -EOPNOTSUPP;
  745. return phy_ethtool_gset(phydev, cmd);
  746. }
  747. static int ethoc_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  748. {
  749. struct ethoc *priv = netdev_priv(dev);
  750. struct phy_device *phydev = priv->phy;
  751. if (!phydev)
  752. return -EOPNOTSUPP;
  753. return phy_ethtool_sset(phydev, cmd);
  754. }
  755. static int ethoc_get_regs_len(struct net_device *netdev)
  756. {
  757. return ETH_END;
  758. }
  759. static void ethoc_get_regs(struct net_device *dev, struct ethtool_regs *regs,
  760. void *p)
  761. {
  762. struct ethoc *priv = netdev_priv(dev);
  763. u32 *regs_buff = p;
  764. unsigned i;
  765. regs->version = 0;
  766. for (i = 0; i < ETH_END / sizeof(u32); ++i)
  767. regs_buff[i] = ethoc_read(priv, i * sizeof(u32));
  768. }
  769. static void ethoc_get_ringparam(struct net_device *dev,
  770. struct ethtool_ringparam *ring)
  771. {
  772. struct ethoc *priv = netdev_priv(dev);
  773. ring->rx_max_pending = priv->num_bd - 1;
  774. ring->rx_mini_max_pending = 0;
  775. ring->rx_jumbo_max_pending = 0;
  776. ring->tx_max_pending = priv->num_bd - 1;
  777. ring->rx_pending = priv->num_rx;
  778. ring->rx_mini_pending = 0;
  779. ring->rx_jumbo_pending = 0;
  780. ring->tx_pending = priv->num_tx;
  781. }
  782. static int ethoc_set_ringparam(struct net_device *dev,
  783. struct ethtool_ringparam *ring)
  784. {
  785. struct ethoc *priv = netdev_priv(dev);
  786. if (ring->tx_pending < 1 || ring->rx_pending < 1 ||
  787. ring->tx_pending + ring->rx_pending > priv->num_bd)
  788. return -EINVAL;
  789. if (ring->rx_mini_pending || ring->rx_jumbo_pending)
  790. return -EINVAL;
  791. if (netif_running(dev)) {
  792. netif_tx_disable(dev);
  793. ethoc_disable_rx_and_tx(priv);
  794. ethoc_disable_irq(priv, INT_MASK_TX | INT_MASK_RX);
  795. synchronize_irq(dev->irq);
  796. }
  797. priv->num_tx = rounddown_pow_of_two(ring->tx_pending);
  798. priv->num_rx = ring->rx_pending;
  799. ethoc_init_ring(priv, dev->mem_start);
  800. if (netif_running(dev)) {
  801. ethoc_enable_irq(priv, INT_MASK_TX | INT_MASK_RX);
  802. ethoc_enable_rx_and_tx(priv);
  803. netif_wake_queue(dev);
  804. }
  805. return 0;
  806. }
  807. const struct ethtool_ops ethoc_ethtool_ops = {
  808. .get_settings = ethoc_get_settings,
  809. .set_settings = ethoc_set_settings,
  810. .get_regs_len = ethoc_get_regs_len,
  811. .get_regs = ethoc_get_regs,
  812. .get_link = ethtool_op_get_link,
  813. .get_ringparam = ethoc_get_ringparam,
  814. .set_ringparam = ethoc_set_ringparam,
  815. .get_ts_info = ethtool_op_get_ts_info,
  816. };
  817. static const struct net_device_ops ethoc_netdev_ops = {
  818. .ndo_open = ethoc_open,
  819. .ndo_stop = ethoc_stop,
  820. .ndo_do_ioctl = ethoc_ioctl,
  821. .ndo_set_mac_address = ethoc_set_mac_address,
  822. .ndo_set_rx_mode = ethoc_set_multicast_list,
  823. .ndo_change_mtu = ethoc_change_mtu,
  824. .ndo_tx_timeout = ethoc_tx_timeout,
  825. .ndo_start_xmit = ethoc_start_xmit,
  826. };
  827. /**
  828. * ethoc_probe - initialize OpenCores ethernet MAC
  829. * pdev: platform device
  830. */
  831. static int ethoc_probe(struct platform_device *pdev)
  832. {
  833. struct net_device *netdev = NULL;
  834. struct resource *res = NULL;
  835. struct resource *mmio = NULL;
  836. struct resource *mem = NULL;
  837. struct ethoc *priv = NULL;
  838. unsigned int phy;
  839. int num_bd;
  840. int ret = 0;
  841. bool random_mac = false;
  842. struct ethoc_platform_data *pdata = dev_get_platdata(&pdev->dev);
  843. u32 eth_clkfreq = pdata ? pdata->eth_clkfreq : 0;
  844. /* allocate networking device */
  845. netdev = alloc_etherdev(sizeof(struct ethoc));
  846. if (!netdev) {
  847. ret = -ENOMEM;
  848. goto out;
  849. }
  850. SET_NETDEV_DEV(netdev, &pdev->dev);
  851. platform_set_drvdata(pdev, netdev);
  852. /* obtain I/O memory space */
  853. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  854. if (!res) {
  855. dev_err(&pdev->dev, "cannot obtain I/O memory space\n");
  856. ret = -ENXIO;
  857. goto free;
  858. }
  859. mmio = devm_request_mem_region(&pdev->dev, res->start,
  860. resource_size(res), res->name);
  861. if (!mmio) {
  862. dev_err(&pdev->dev, "cannot request I/O memory space\n");
  863. ret = -ENXIO;
  864. goto free;
  865. }
  866. netdev->base_addr = mmio->start;
  867. /* obtain buffer memory space */
  868. res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  869. if (res) {
  870. mem = devm_request_mem_region(&pdev->dev, res->start,
  871. resource_size(res), res->name);
  872. if (!mem) {
  873. dev_err(&pdev->dev, "cannot request memory space\n");
  874. ret = -ENXIO;
  875. goto free;
  876. }
  877. netdev->mem_start = mem->start;
  878. netdev->mem_end = mem->end;
  879. }
  880. /* obtain device IRQ number */
  881. res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  882. if (!res) {
  883. dev_err(&pdev->dev, "cannot obtain IRQ\n");
  884. ret = -ENXIO;
  885. goto free;
  886. }
  887. netdev->irq = res->start;
  888. /* setup driver-private data */
  889. priv = netdev_priv(netdev);
  890. priv->netdev = netdev;
  891. priv->dma_alloc = 0;
  892. priv->io_region_size = resource_size(mmio);
  893. priv->iobase = devm_ioremap_nocache(&pdev->dev, netdev->base_addr,
  894. resource_size(mmio));
  895. if (!priv->iobase) {
  896. dev_err(&pdev->dev, "cannot remap I/O memory space\n");
  897. ret = -ENXIO;
  898. goto error;
  899. }
  900. if (netdev->mem_end) {
  901. priv->membase = devm_ioremap_nocache(&pdev->dev,
  902. netdev->mem_start, resource_size(mem));
  903. if (!priv->membase) {
  904. dev_err(&pdev->dev, "cannot remap memory space\n");
  905. ret = -ENXIO;
  906. goto error;
  907. }
  908. } else {
  909. /* Allocate buffer memory */
  910. priv->membase = dmam_alloc_coherent(&pdev->dev,
  911. buffer_size, (void *)&netdev->mem_start,
  912. GFP_KERNEL);
  913. if (!priv->membase) {
  914. dev_err(&pdev->dev, "cannot allocate %dB buffer\n",
  915. buffer_size);
  916. ret = -ENOMEM;
  917. goto error;
  918. }
  919. netdev->mem_end = netdev->mem_start + buffer_size;
  920. priv->dma_alloc = buffer_size;
  921. }
  922. priv->big_endian = pdata ? pdata->big_endian :
  923. of_device_is_big_endian(pdev->dev.of_node);
  924. /* calculate the number of TX/RX buffers, maximum 128 supported */
  925. num_bd = min_t(unsigned int,
  926. 128, (netdev->mem_end - netdev->mem_start + 1) / ETHOC_BUFSIZ);
  927. if (num_bd < 4) {
  928. ret = -ENODEV;
  929. goto error;
  930. }
  931. priv->num_bd = num_bd;
  932. /* num_tx must be a power of two */
  933. priv->num_tx = rounddown_pow_of_two(num_bd >> 1);
  934. priv->num_rx = num_bd - priv->num_tx;
  935. dev_dbg(&pdev->dev, "ethoc: num_tx: %d num_rx: %d\n",
  936. priv->num_tx, priv->num_rx);
  937. priv->vma = devm_kzalloc(&pdev->dev, num_bd*sizeof(void *), GFP_KERNEL);
  938. if (!priv->vma) {
  939. ret = -ENOMEM;
  940. goto error;
  941. }
  942. /* Allow the platform setup code to pass in a MAC address. */
  943. if (pdata) {
  944. memcpy(netdev->dev_addr, pdata->hwaddr, IFHWADDRLEN);
  945. priv->phy_id = pdata->phy_id;
  946. } else {
  947. const uint8_t *mac;
  948. mac = of_get_property(pdev->dev.of_node,
  949. "local-mac-address",
  950. NULL);
  951. if (mac)
  952. memcpy(netdev->dev_addr, mac, IFHWADDRLEN);
  953. priv->phy_id = -1;
  954. }
  955. /* Check that the given MAC address is valid. If it isn't, read the
  956. * current MAC from the controller.
  957. */
  958. if (!is_valid_ether_addr(netdev->dev_addr))
  959. ethoc_get_mac_address(netdev, netdev->dev_addr);
  960. /* Check the MAC again for validity, if it still isn't choose and
  961. * program a random one.
  962. */
  963. if (!is_valid_ether_addr(netdev->dev_addr)) {
  964. eth_random_addr(netdev->dev_addr);
  965. random_mac = true;
  966. }
  967. ethoc_do_set_mac_address(netdev);
  968. if (random_mac)
  969. netdev->addr_assign_type = NET_ADDR_RANDOM;
  970. /* Allow the platform setup code to adjust MII management bus clock. */
  971. if (!eth_clkfreq) {
  972. struct clk *clk = devm_clk_get(&pdev->dev, NULL);
  973. if (!IS_ERR(clk)) {
  974. priv->clk = clk;
  975. clk_prepare_enable(clk);
  976. eth_clkfreq = clk_get_rate(clk);
  977. }
  978. }
  979. if (eth_clkfreq) {
  980. u32 clkdiv = MIIMODER_CLKDIV(eth_clkfreq / 2500000 + 1);
  981. if (!clkdiv)
  982. clkdiv = 2;
  983. dev_dbg(&pdev->dev, "setting MII clkdiv to %u\n", clkdiv);
  984. ethoc_write(priv, MIIMODER,
  985. (ethoc_read(priv, MIIMODER) & MIIMODER_NOPRE) |
  986. clkdiv);
  987. }
  988. /* register MII bus */
  989. priv->mdio = mdiobus_alloc();
  990. if (!priv->mdio) {
  991. ret = -ENOMEM;
  992. goto free;
  993. }
  994. priv->mdio->name = "ethoc-mdio";
  995. snprintf(priv->mdio->id, MII_BUS_ID_SIZE, "%s-%d",
  996. priv->mdio->name, pdev->id);
  997. priv->mdio->read = ethoc_mdio_read;
  998. priv->mdio->write = ethoc_mdio_write;
  999. priv->mdio->priv = priv;
  1000. priv->mdio->irq = kmalloc(sizeof(int) * PHY_MAX_ADDR, GFP_KERNEL);
  1001. if (!priv->mdio->irq) {
  1002. ret = -ENOMEM;
  1003. goto free_mdio;
  1004. }
  1005. for (phy = 0; phy < PHY_MAX_ADDR; phy++)
  1006. priv->mdio->irq[phy] = PHY_POLL;
  1007. ret = mdiobus_register(priv->mdio);
  1008. if (ret) {
  1009. dev_err(&netdev->dev, "failed to register MDIO bus\n");
  1010. goto free_mdio;
  1011. }
  1012. ret = ethoc_mdio_probe(netdev);
  1013. if (ret) {
  1014. dev_err(&netdev->dev, "failed to probe MDIO bus\n");
  1015. goto error;
  1016. }
  1017. /* setup the net_device structure */
  1018. netdev->netdev_ops = &ethoc_netdev_ops;
  1019. netdev->watchdog_timeo = ETHOC_TIMEOUT;
  1020. netdev->features |= 0;
  1021. netdev->ethtool_ops = &ethoc_ethtool_ops;
  1022. /* setup NAPI */
  1023. netif_napi_add(netdev, &priv->napi, ethoc_poll, 64);
  1024. spin_lock_init(&priv->lock);
  1025. ret = register_netdev(netdev);
  1026. if (ret < 0) {
  1027. dev_err(&netdev->dev, "failed to register interface\n");
  1028. goto error2;
  1029. }
  1030. goto out;
  1031. error2:
  1032. netif_napi_del(&priv->napi);
  1033. error:
  1034. mdiobus_unregister(priv->mdio);
  1035. free_mdio:
  1036. kfree(priv->mdio->irq);
  1037. mdiobus_free(priv->mdio);
  1038. free:
  1039. if (priv->clk)
  1040. clk_disable_unprepare(priv->clk);
  1041. free_netdev(netdev);
  1042. out:
  1043. return ret;
  1044. }
  1045. /**
  1046. * ethoc_remove - shutdown OpenCores ethernet MAC
  1047. * @pdev: platform device
  1048. */
  1049. static int ethoc_remove(struct platform_device *pdev)
  1050. {
  1051. struct net_device *netdev = platform_get_drvdata(pdev);
  1052. struct ethoc *priv = netdev_priv(netdev);
  1053. if (netdev) {
  1054. netif_napi_del(&priv->napi);
  1055. phy_disconnect(priv->phy);
  1056. priv->phy = NULL;
  1057. if (priv->mdio) {
  1058. mdiobus_unregister(priv->mdio);
  1059. kfree(priv->mdio->irq);
  1060. mdiobus_free(priv->mdio);
  1061. }
  1062. if (priv->clk)
  1063. clk_disable_unprepare(priv->clk);
  1064. unregister_netdev(netdev);
  1065. free_netdev(netdev);
  1066. }
  1067. return 0;
  1068. }
  1069. #ifdef CONFIG_PM
  1070. static int ethoc_suspend(struct platform_device *pdev, pm_message_t state)
  1071. {
  1072. return -ENOSYS;
  1073. }
  1074. static int ethoc_resume(struct platform_device *pdev)
  1075. {
  1076. return -ENOSYS;
  1077. }
  1078. #else
  1079. # define ethoc_suspend NULL
  1080. # define ethoc_resume NULL
  1081. #endif
  1082. static const struct of_device_id ethoc_match[] = {
  1083. { .compatible = "opencores,ethoc", },
  1084. {},
  1085. };
  1086. MODULE_DEVICE_TABLE(of, ethoc_match);
  1087. static struct platform_driver ethoc_driver = {
  1088. .probe = ethoc_probe,
  1089. .remove = ethoc_remove,
  1090. .suspend = ethoc_suspend,
  1091. .resume = ethoc_resume,
  1092. .driver = {
  1093. .name = "ethoc",
  1094. .of_match_table = ethoc_match,
  1095. },
  1096. };
  1097. module_platform_driver(ethoc_driver);
  1098. MODULE_AUTHOR("Thierry Reding <thierry.reding@avionic-design.de>");
  1099. MODULE_DESCRIPTION("OpenCores Ethernet MAC driver");
  1100. MODULE_LICENSE("GPL v2");