ethoc.c 31 KB

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