ethoc.c 32 KB

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