ethoc.c 29 KB

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