ep93xx_eth.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893
  1. /*
  2. * EP93xx ethernet network device driver
  3. * Copyright (C) 2006 Lennert Buytenhek <buytenh@wantstofly.org>
  4. * Dedicated to Marija Kulikova.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. */
  11. #define pr_fmt(fmt) KBUILD_MODNAME ":%s: " fmt, __func__
  12. #include <linux/dma-mapping.h>
  13. #include <linux/module.h>
  14. #include <linux/kernel.h>
  15. #include <linux/netdevice.h>
  16. #include <linux/mii.h>
  17. #include <linux/etherdevice.h>
  18. #include <linux/ethtool.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/moduleparam.h>
  21. #include <linux/platform_device.h>
  22. #include <linux/delay.h>
  23. #include <linux/io.h>
  24. #include <linux/slab.h>
  25. #include <mach/hardware.h>
  26. #define DRV_MODULE_NAME "ep93xx-eth"
  27. #define DRV_MODULE_VERSION "0.1"
  28. #define RX_QUEUE_ENTRIES 64
  29. #define TX_QUEUE_ENTRIES 8
  30. #define MAX_PKT_SIZE 2044
  31. #define PKT_BUF_SIZE 2048
  32. #define REG_RXCTL 0x0000
  33. #define REG_RXCTL_DEFAULT 0x00073800
  34. #define REG_TXCTL 0x0004
  35. #define REG_TXCTL_ENABLE 0x00000001
  36. #define REG_MIICMD 0x0010
  37. #define REG_MIICMD_READ 0x00008000
  38. #define REG_MIICMD_WRITE 0x00004000
  39. #define REG_MIIDATA 0x0014
  40. #define REG_MIISTS 0x0018
  41. #define REG_MIISTS_BUSY 0x00000001
  42. #define REG_SELFCTL 0x0020
  43. #define REG_SELFCTL_RESET 0x00000001
  44. #define REG_INTEN 0x0024
  45. #define REG_INTEN_TX 0x00000008
  46. #define REG_INTEN_RX 0x00000007
  47. #define REG_INTSTSP 0x0028
  48. #define REG_INTSTS_TX 0x00000008
  49. #define REG_INTSTS_RX 0x00000004
  50. #define REG_INTSTSC 0x002c
  51. #define REG_AFP 0x004c
  52. #define REG_INDAD0 0x0050
  53. #define REG_INDAD1 0x0051
  54. #define REG_INDAD2 0x0052
  55. #define REG_INDAD3 0x0053
  56. #define REG_INDAD4 0x0054
  57. #define REG_INDAD5 0x0055
  58. #define REG_GIINTMSK 0x0064
  59. #define REG_GIINTMSK_ENABLE 0x00008000
  60. #define REG_BMCTL 0x0080
  61. #define REG_BMCTL_ENABLE_TX 0x00000100
  62. #define REG_BMCTL_ENABLE_RX 0x00000001
  63. #define REG_BMSTS 0x0084
  64. #define REG_BMSTS_RX_ACTIVE 0x00000008
  65. #define REG_RXDQBADD 0x0090
  66. #define REG_RXDQBLEN 0x0094
  67. #define REG_RXDCURADD 0x0098
  68. #define REG_RXDENQ 0x009c
  69. #define REG_RXSTSQBADD 0x00a0
  70. #define REG_RXSTSQBLEN 0x00a4
  71. #define REG_RXSTSQCURADD 0x00a8
  72. #define REG_RXSTSENQ 0x00ac
  73. #define REG_TXDQBADD 0x00b0
  74. #define REG_TXDQBLEN 0x00b4
  75. #define REG_TXDQCURADD 0x00b8
  76. #define REG_TXDENQ 0x00bc
  77. #define REG_TXSTSQBADD 0x00c0
  78. #define REG_TXSTSQBLEN 0x00c4
  79. #define REG_TXSTSQCURADD 0x00c8
  80. #define REG_MAXFRMLEN 0x00e8
  81. struct ep93xx_rdesc
  82. {
  83. u32 buf_addr;
  84. u32 rdesc1;
  85. };
  86. #define RDESC1_NSOF 0x80000000
  87. #define RDESC1_BUFFER_INDEX 0x7fff0000
  88. #define RDESC1_BUFFER_LENGTH 0x0000ffff
  89. struct ep93xx_rstat
  90. {
  91. u32 rstat0;
  92. u32 rstat1;
  93. };
  94. #define RSTAT0_RFP 0x80000000
  95. #define RSTAT0_RWE 0x40000000
  96. #define RSTAT0_EOF 0x20000000
  97. #define RSTAT0_EOB 0x10000000
  98. #define RSTAT0_AM 0x00c00000
  99. #define RSTAT0_RX_ERR 0x00200000
  100. #define RSTAT0_OE 0x00100000
  101. #define RSTAT0_FE 0x00080000
  102. #define RSTAT0_RUNT 0x00040000
  103. #define RSTAT0_EDATA 0x00020000
  104. #define RSTAT0_CRCE 0x00010000
  105. #define RSTAT0_CRCI 0x00008000
  106. #define RSTAT0_HTI 0x00003f00
  107. #define RSTAT1_RFP 0x80000000
  108. #define RSTAT1_BUFFER_INDEX 0x7fff0000
  109. #define RSTAT1_FRAME_LENGTH 0x0000ffff
  110. struct ep93xx_tdesc
  111. {
  112. u32 buf_addr;
  113. u32 tdesc1;
  114. };
  115. #define TDESC1_EOF 0x80000000
  116. #define TDESC1_BUFFER_INDEX 0x7fff0000
  117. #define TDESC1_BUFFER_ABORT 0x00008000
  118. #define TDESC1_BUFFER_LENGTH 0x00000fff
  119. struct ep93xx_tstat
  120. {
  121. u32 tstat0;
  122. };
  123. #define TSTAT0_TXFP 0x80000000
  124. #define TSTAT0_TXWE 0x40000000
  125. #define TSTAT0_FA 0x20000000
  126. #define TSTAT0_LCRS 0x10000000
  127. #define TSTAT0_OW 0x04000000
  128. #define TSTAT0_TXU 0x02000000
  129. #define TSTAT0_ECOLL 0x01000000
  130. #define TSTAT0_NCOLL 0x001f0000
  131. #define TSTAT0_BUFFER_INDEX 0x00007fff
  132. struct ep93xx_descs
  133. {
  134. struct ep93xx_rdesc rdesc[RX_QUEUE_ENTRIES];
  135. struct ep93xx_tdesc tdesc[TX_QUEUE_ENTRIES];
  136. struct ep93xx_rstat rstat[RX_QUEUE_ENTRIES];
  137. struct ep93xx_tstat tstat[TX_QUEUE_ENTRIES];
  138. };
  139. struct ep93xx_priv
  140. {
  141. struct resource *res;
  142. void __iomem *base_addr;
  143. int irq;
  144. struct ep93xx_descs *descs;
  145. dma_addr_t descs_dma_addr;
  146. void *rx_buf[RX_QUEUE_ENTRIES];
  147. void *tx_buf[TX_QUEUE_ENTRIES];
  148. spinlock_t rx_lock;
  149. unsigned int rx_pointer;
  150. unsigned int tx_clean_pointer;
  151. unsigned int tx_pointer;
  152. spinlock_t tx_pending_lock;
  153. unsigned int tx_pending;
  154. struct net_device *dev;
  155. struct napi_struct napi;
  156. struct mii_if_info mii;
  157. u8 mdc_divisor;
  158. };
  159. #define rdb(ep, off) __raw_readb((ep)->base_addr + (off))
  160. #define rdw(ep, off) __raw_readw((ep)->base_addr + (off))
  161. #define rdl(ep, off) __raw_readl((ep)->base_addr + (off))
  162. #define wrb(ep, off, val) __raw_writeb((val), (ep)->base_addr + (off))
  163. #define wrw(ep, off, val) __raw_writew((val), (ep)->base_addr + (off))
  164. #define wrl(ep, off, val) __raw_writel((val), (ep)->base_addr + (off))
  165. static int ep93xx_mdio_read(struct net_device *dev, int phy_id, int reg)
  166. {
  167. struct ep93xx_priv *ep = netdev_priv(dev);
  168. int data;
  169. int i;
  170. wrl(ep, REG_MIICMD, REG_MIICMD_READ | (phy_id << 5) | reg);
  171. for (i = 0; i < 10; i++) {
  172. if ((rdl(ep, REG_MIISTS) & REG_MIISTS_BUSY) == 0)
  173. break;
  174. msleep(1);
  175. }
  176. if (i == 10) {
  177. pr_info("mdio read timed out\n");
  178. data = 0xffff;
  179. } else {
  180. data = rdl(ep, REG_MIIDATA);
  181. }
  182. return data;
  183. }
  184. static void ep93xx_mdio_write(struct net_device *dev, int phy_id, int reg, int data)
  185. {
  186. struct ep93xx_priv *ep = netdev_priv(dev);
  187. int i;
  188. wrl(ep, REG_MIIDATA, data);
  189. wrl(ep, REG_MIICMD, REG_MIICMD_WRITE | (phy_id << 5) | reg);
  190. for (i = 0; i < 10; i++) {
  191. if ((rdl(ep, REG_MIISTS) & REG_MIISTS_BUSY) == 0)
  192. break;
  193. msleep(1);
  194. }
  195. if (i == 10)
  196. pr_info("mdio write timed out\n");
  197. }
  198. static int ep93xx_rx(struct net_device *dev, int processed, int budget)
  199. {
  200. struct ep93xx_priv *ep = netdev_priv(dev);
  201. while (processed < budget) {
  202. int entry;
  203. struct ep93xx_rstat *rstat;
  204. u32 rstat0;
  205. u32 rstat1;
  206. int length;
  207. struct sk_buff *skb;
  208. entry = ep->rx_pointer;
  209. rstat = ep->descs->rstat + entry;
  210. rstat0 = rstat->rstat0;
  211. rstat1 = rstat->rstat1;
  212. if (!(rstat0 & RSTAT0_RFP) || !(rstat1 & RSTAT1_RFP))
  213. break;
  214. rstat->rstat0 = 0;
  215. rstat->rstat1 = 0;
  216. if (!(rstat0 & RSTAT0_EOF))
  217. pr_crit("not end-of-frame %.8x %.8x\n", rstat0, rstat1);
  218. if (!(rstat0 & RSTAT0_EOB))
  219. pr_crit("not end-of-buffer %.8x %.8x\n", rstat0, rstat1);
  220. if ((rstat1 & RSTAT1_BUFFER_INDEX) >> 16 != entry)
  221. pr_crit("entry mismatch %.8x %.8x\n", rstat0, rstat1);
  222. if (!(rstat0 & RSTAT0_RWE)) {
  223. dev->stats.rx_errors++;
  224. if (rstat0 & RSTAT0_OE)
  225. dev->stats.rx_fifo_errors++;
  226. if (rstat0 & RSTAT0_FE)
  227. dev->stats.rx_frame_errors++;
  228. if (rstat0 & (RSTAT0_RUNT | RSTAT0_EDATA))
  229. dev->stats.rx_length_errors++;
  230. if (rstat0 & RSTAT0_CRCE)
  231. dev->stats.rx_crc_errors++;
  232. goto err;
  233. }
  234. length = rstat1 & RSTAT1_FRAME_LENGTH;
  235. if (length > MAX_PKT_SIZE) {
  236. pr_notice("invalid length %.8x %.8x\n", rstat0, rstat1);
  237. goto err;
  238. }
  239. /* Strip FCS. */
  240. if (rstat0 & RSTAT0_CRCI)
  241. length -= 4;
  242. skb = netdev_alloc_skb(dev, length + 2);
  243. if (likely(skb != NULL)) {
  244. struct ep93xx_rdesc *rxd = &ep->descs->rdesc[entry];
  245. skb_reserve(skb, 2);
  246. dma_sync_single_for_cpu(dev->dev.parent, rxd->buf_addr,
  247. length, DMA_FROM_DEVICE);
  248. skb_copy_to_linear_data(skb, ep->rx_buf[entry], length);
  249. dma_sync_single_for_device(dev->dev.parent,
  250. rxd->buf_addr, length,
  251. DMA_FROM_DEVICE);
  252. skb_put(skb, length);
  253. skb->protocol = eth_type_trans(skb, dev);
  254. netif_receive_skb(skb);
  255. dev->stats.rx_packets++;
  256. dev->stats.rx_bytes += length;
  257. } else {
  258. dev->stats.rx_dropped++;
  259. }
  260. err:
  261. ep->rx_pointer = (entry + 1) & (RX_QUEUE_ENTRIES - 1);
  262. processed++;
  263. }
  264. return processed;
  265. }
  266. static int ep93xx_have_more_rx(struct ep93xx_priv *ep)
  267. {
  268. struct ep93xx_rstat *rstat = ep->descs->rstat + ep->rx_pointer;
  269. return !!((rstat->rstat0 & RSTAT0_RFP) && (rstat->rstat1 & RSTAT1_RFP));
  270. }
  271. static int ep93xx_poll(struct napi_struct *napi, int budget)
  272. {
  273. struct ep93xx_priv *ep = container_of(napi, struct ep93xx_priv, napi);
  274. struct net_device *dev = ep->dev;
  275. int rx = 0;
  276. poll_some_more:
  277. rx = ep93xx_rx(dev, rx, budget);
  278. if (rx < budget) {
  279. int more = 0;
  280. spin_lock_irq(&ep->rx_lock);
  281. __napi_complete(napi);
  282. wrl(ep, REG_INTEN, REG_INTEN_TX | REG_INTEN_RX);
  283. if (ep93xx_have_more_rx(ep)) {
  284. wrl(ep, REG_INTEN, REG_INTEN_TX);
  285. wrl(ep, REG_INTSTSP, REG_INTSTS_RX);
  286. more = 1;
  287. }
  288. spin_unlock_irq(&ep->rx_lock);
  289. if (more && napi_reschedule(napi))
  290. goto poll_some_more;
  291. }
  292. if (rx) {
  293. wrw(ep, REG_RXDENQ, rx);
  294. wrw(ep, REG_RXSTSENQ, rx);
  295. }
  296. return rx;
  297. }
  298. static int ep93xx_xmit(struct sk_buff *skb, struct net_device *dev)
  299. {
  300. struct ep93xx_priv *ep = netdev_priv(dev);
  301. struct ep93xx_tdesc *txd;
  302. int entry;
  303. if (unlikely(skb->len > MAX_PKT_SIZE)) {
  304. dev->stats.tx_dropped++;
  305. dev_kfree_skb(skb);
  306. return NETDEV_TX_OK;
  307. }
  308. entry = ep->tx_pointer;
  309. ep->tx_pointer = (ep->tx_pointer + 1) & (TX_QUEUE_ENTRIES - 1);
  310. txd = &ep->descs->tdesc[entry];
  311. txd->tdesc1 = TDESC1_EOF | (entry << 16) | (skb->len & 0xfff);
  312. dma_sync_single_for_cpu(dev->dev.parent, txd->buf_addr, skb->len,
  313. DMA_TO_DEVICE);
  314. skb_copy_and_csum_dev(skb, ep->tx_buf[entry]);
  315. dma_sync_single_for_device(dev->dev.parent, txd->buf_addr, skb->len,
  316. DMA_TO_DEVICE);
  317. dev_kfree_skb(skb);
  318. spin_lock_irq(&ep->tx_pending_lock);
  319. ep->tx_pending++;
  320. if (ep->tx_pending == TX_QUEUE_ENTRIES)
  321. netif_stop_queue(dev);
  322. spin_unlock_irq(&ep->tx_pending_lock);
  323. wrl(ep, REG_TXDENQ, 1);
  324. return NETDEV_TX_OK;
  325. }
  326. static void ep93xx_tx_complete(struct net_device *dev)
  327. {
  328. struct ep93xx_priv *ep = netdev_priv(dev);
  329. int wake;
  330. wake = 0;
  331. spin_lock(&ep->tx_pending_lock);
  332. while (1) {
  333. int entry;
  334. struct ep93xx_tstat *tstat;
  335. u32 tstat0;
  336. entry = ep->tx_clean_pointer;
  337. tstat = ep->descs->tstat + entry;
  338. tstat0 = tstat->tstat0;
  339. if (!(tstat0 & TSTAT0_TXFP))
  340. break;
  341. tstat->tstat0 = 0;
  342. if (tstat0 & TSTAT0_FA)
  343. pr_crit("frame aborted %.8x\n", tstat0);
  344. if ((tstat0 & TSTAT0_BUFFER_INDEX) != entry)
  345. pr_crit("entry mismatch %.8x\n", tstat0);
  346. if (tstat0 & TSTAT0_TXWE) {
  347. int length = ep->descs->tdesc[entry].tdesc1 & 0xfff;
  348. dev->stats.tx_packets++;
  349. dev->stats.tx_bytes += length;
  350. } else {
  351. dev->stats.tx_errors++;
  352. }
  353. if (tstat0 & TSTAT0_OW)
  354. dev->stats.tx_window_errors++;
  355. if (tstat0 & TSTAT0_TXU)
  356. dev->stats.tx_fifo_errors++;
  357. dev->stats.collisions += (tstat0 >> 16) & 0x1f;
  358. ep->tx_clean_pointer = (entry + 1) & (TX_QUEUE_ENTRIES - 1);
  359. if (ep->tx_pending == TX_QUEUE_ENTRIES)
  360. wake = 1;
  361. ep->tx_pending--;
  362. }
  363. spin_unlock(&ep->tx_pending_lock);
  364. if (wake)
  365. netif_wake_queue(dev);
  366. }
  367. static irqreturn_t ep93xx_irq(int irq, void *dev_id)
  368. {
  369. struct net_device *dev = dev_id;
  370. struct ep93xx_priv *ep = netdev_priv(dev);
  371. u32 status;
  372. status = rdl(ep, REG_INTSTSC);
  373. if (status == 0)
  374. return IRQ_NONE;
  375. if (status & REG_INTSTS_RX) {
  376. spin_lock(&ep->rx_lock);
  377. if (likely(napi_schedule_prep(&ep->napi))) {
  378. wrl(ep, REG_INTEN, REG_INTEN_TX);
  379. __napi_schedule(&ep->napi);
  380. }
  381. spin_unlock(&ep->rx_lock);
  382. }
  383. if (status & REG_INTSTS_TX)
  384. ep93xx_tx_complete(dev);
  385. return IRQ_HANDLED;
  386. }
  387. static void ep93xx_free_buffers(struct ep93xx_priv *ep)
  388. {
  389. struct device *dev = ep->dev->dev.parent;
  390. int i;
  391. if (!ep->descs)
  392. return;
  393. for (i = 0; i < RX_QUEUE_ENTRIES; i++) {
  394. dma_addr_t d;
  395. d = ep->descs->rdesc[i].buf_addr;
  396. if (d)
  397. dma_unmap_single(dev, d, PKT_BUF_SIZE, DMA_FROM_DEVICE);
  398. kfree(ep->rx_buf[i]);
  399. }
  400. for (i = 0; i < TX_QUEUE_ENTRIES; i++) {
  401. dma_addr_t d;
  402. d = ep->descs->tdesc[i].buf_addr;
  403. if (d)
  404. dma_unmap_single(dev, d, PKT_BUF_SIZE, DMA_TO_DEVICE);
  405. kfree(ep->tx_buf[i]);
  406. }
  407. dma_free_coherent(dev, sizeof(struct ep93xx_descs), ep->descs,
  408. ep->descs_dma_addr);
  409. ep->descs = NULL;
  410. }
  411. static int ep93xx_alloc_buffers(struct ep93xx_priv *ep)
  412. {
  413. struct device *dev = ep->dev->dev.parent;
  414. int i;
  415. ep->descs = dma_alloc_coherent(dev, sizeof(struct ep93xx_descs),
  416. &ep->descs_dma_addr, GFP_KERNEL);
  417. if (ep->descs == NULL)
  418. return 1;
  419. for (i = 0; i < RX_QUEUE_ENTRIES; i++) {
  420. void *buf;
  421. dma_addr_t d;
  422. buf = kmalloc(PKT_BUF_SIZE, GFP_KERNEL);
  423. if (buf == NULL)
  424. goto err;
  425. d = dma_map_single(dev, buf, PKT_BUF_SIZE, DMA_FROM_DEVICE);
  426. if (dma_mapping_error(dev, d)) {
  427. kfree(buf);
  428. goto err;
  429. }
  430. ep->rx_buf[i] = buf;
  431. ep->descs->rdesc[i].buf_addr = d;
  432. ep->descs->rdesc[i].rdesc1 = (i << 16) | PKT_BUF_SIZE;
  433. }
  434. for (i = 0; i < TX_QUEUE_ENTRIES; i++) {
  435. void *buf;
  436. dma_addr_t d;
  437. buf = kmalloc(PKT_BUF_SIZE, GFP_KERNEL);
  438. if (buf == NULL)
  439. goto err;
  440. d = dma_map_single(dev, buf, PKT_BUF_SIZE, DMA_TO_DEVICE);
  441. if (dma_mapping_error(dev, d)) {
  442. kfree(buf);
  443. goto err;
  444. }
  445. ep->tx_buf[i] = buf;
  446. ep->descs->tdesc[i].buf_addr = d;
  447. }
  448. return 0;
  449. err:
  450. ep93xx_free_buffers(ep);
  451. return 1;
  452. }
  453. static int ep93xx_start_hw(struct net_device *dev)
  454. {
  455. struct ep93xx_priv *ep = netdev_priv(dev);
  456. unsigned long addr;
  457. int i;
  458. wrl(ep, REG_SELFCTL, REG_SELFCTL_RESET);
  459. for (i = 0; i < 10; i++) {
  460. if ((rdl(ep, REG_SELFCTL) & REG_SELFCTL_RESET) == 0)
  461. break;
  462. msleep(1);
  463. }
  464. if (i == 10) {
  465. pr_crit("hw failed to reset\n");
  466. return 1;
  467. }
  468. wrl(ep, REG_SELFCTL, ((ep->mdc_divisor - 1) << 9));
  469. /* Does the PHY support preamble suppress? */
  470. if ((ep93xx_mdio_read(dev, ep->mii.phy_id, MII_BMSR) & 0x0040) != 0)
  471. wrl(ep, REG_SELFCTL, ((ep->mdc_divisor - 1) << 9) | (1 << 8));
  472. /* Receive descriptor ring. */
  473. addr = ep->descs_dma_addr + offsetof(struct ep93xx_descs, rdesc);
  474. wrl(ep, REG_RXDQBADD, addr);
  475. wrl(ep, REG_RXDCURADD, addr);
  476. wrw(ep, REG_RXDQBLEN, RX_QUEUE_ENTRIES * sizeof(struct ep93xx_rdesc));
  477. /* Receive status ring. */
  478. addr = ep->descs_dma_addr + offsetof(struct ep93xx_descs, rstat);
  479. wrl(ep, REG_RXSTSQBADD, addr);
  480. wrl(ep, REG_RXSTSQCURADD, addr);
  481. wrw(ep, REG_RXSTSQBLEN, RX_QUEUE_ENTRIES * sizeof(struct ep93xx_rstat));
  482. /* Transmit descriptor ring. */
  483. addr = ep->descs_dma_addr + offsetof(struct ep93xx_descs, tdesc);
  484. wrl(ep, REG_TXDQBADD, addr);
  485. wrl(ep, REG_TXDQCURADD, addr);
  486. wrw(ep, REG_TXDQBLEN, TX_QUEUE_ENTRIES * sizeof(struct ep93xx_tdesc));
  487. /* Transmit status ring. */
  488. addr = ep->descs_dma_addr + offsetof(struct ep93xx_descs, tstat);
  489. wrl(ep, REG_TXSTSQBADD, addr);
  490. wrl(ep, REG_TXSTSQCURADD, addr);
  491. wrw(ep, REG_TXSTSQBLEN, TX_QUEUE_ENTRIES * sizeof(struct ep93xx_tstat));
  492. wrl(ep, REG_BMCTL, REG_BMCTL_ENABLE_TX | REG_BMCTL_ENABLE_RX);
  493. wrl(ep, REG_INTEN, REG_INTEN_TX | REG_INTEN_RX);
  494. wrl(ep, REG_GIINTMSK, 0);
  495. for (i = 0; i < 10; i++) {
  496. if ((rdl(ep, REG_BMSTS) & REG_BMSTS_RX_ACTIVE) != 0)
  497. break;
  498. msleep(1);
  499. }
  500. if (i == 10) {
  501. pr_crit("hw failed to start\n");
  502. return 1;
  503. }
  504. wrl(ep, REG_RXDENQ, RX_QUEUE_ENTRIES);
  505. wrl(ep, REG_RXSTSENQ, RX_QUEUE_ENTRIES);
  506. wrb(ep, REG_INDAD0, dev->dev_addr[0]);
  507. wrb(ep, REG_INDAD1, dev->dev_addr[1]);
  508. wrb(ep, REG_INDAD2, dev->dev_addr[2]);
  509. wrb(ep, REG_INDAD3, dev->dev_addr[3]);
  510. wrb(ep, REG_INDAD4, dev->dev_addr[4]);
  511. wrb(ep, REG_INDAD5, dev->dev_addr[5]);
  512. wrl(ep, REG_AFP, 0);
  513. wrl(ep, REG_MAXFRMLEN, (MAX_PKT_SIZE << 16) | MAX_PKT_SIZE);
  514. wrl(ep, REG_RXCTL, REG_RXCTL_DEFAULT);
  515. wrl(ep, REG_TXCTL, REG_TXCTL_ENABLE);
  516. return 0;
  517. }
  518. static void ep93xx_stop_hw(struct net_device *dev)
  519. {
  520. struct ep93xx_priv *ep = netdev_priv(dev);
  521. int i;
  522. wrl(ep, REG_SELFCTL, REG_SELFCTL_RESET);
  523. for (i = 0; i < 10; i++) {
  524. if ((rdl(ep, REG_SELFCTL) & REG_SELFCTL_RESET) == 0)
  525. break;
  526. msleep(1);
  527. }
  528. if (i == 10)
  529. pr_crit("hw failed to reset\n");
  530. }
  531. static int ep93xx_open(struct net_device *dev)
  532. {
  533. struct ep93xx_priv *ep = netdev_priv(dev);
  534. int err;
  535. if (ep93xx_alloc_buffers(ep))
  536. return -ENOMEM;
  537. napi_enable(&ep->napi);
  538. if (ep93xx_start_hw(dev)) {
  539. napi_disable(&ep->napi);
  540. ep93xx_free_buffers(ep);
  541. return -EIO;
  542. }
  543. spin_lock_init(&ep->rx_lock);
  544. ep->rx_pointer = 0;
  545. ep->tx_clean_pointer = 0;
  546. ep->tx_pointer = 0;
  547. spin_lock_init(&ep->tx_pending_lock);
  548. ep->tx_pending = 0;
  549. err = request_irq(ep->irq, ep93xx_irq, IRQF_SHARED, dev->name, dev);
  550. if (err) {
  551. napi_disable(&ep->napi);
  552. ep93xx_stop_hw(dev);
  553. ep93xx_free_buffers(ep);
  554. return err;
  555. }
  556. wrl(ep, REG_GIINTMSK, REG_GIINTMSK_ENABLE);
  557. netif_start_queue(dev);
  558. return 0;
  559. }
  560. static int ep93xx_close(struct net_device *dev)
  561. {
  562. struct ep93xx_priv *ep = netdev_priv(dev);
  563. napi_disable(&ep->napi);
  564. netif_stop_queue(dev);
  565. wrl(ep, REG_GIINTMSK, 0);
  566. free_irq(ep->irq, dev);
  567. ep93xx_stop_hw(dev);
  568. ep93xx_free_buffers(ep);
  569. return 0;
  570. }
  571. static int ep93xx_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
  572. {
  573. struct ep93xx_priv *ep = netdev_priv(dev);
  574. struct mii_ioctl_data *data = if_mii(ifr);
  575. return generic_mii_ioctl(&ep->mii, data, cmd, NULL);
  576. }
  577. static void ep93xx_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
  578. {
  579. strlcpy(info->driver, DRV_MODULE_NAME, sizeof(info->driver));
  580. strlcpy(info->version, DRV_MODULE_VERSION, sizeof(info->version));
  581. }
  582. static int ep93xx_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  583. {
  584. struct ep93xx_priv *ep = netdev_priv(dev);
  585. return mii_ethtool_gset(&ep->mii, cmd);
  586. }
  587. static int ep93xx_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  588. {
  589. struct ep93xx_priv *ep = netdev_priv(dev);
  590. return mii_ethtool_sset(&ep->mii, cmd);
  591. }
  592. static int ep93xx_nway_reset(struct net_device *dev)
  593. {
  594. struct ep93xx_priv *ep = netdev_priv(dev);
  595. return mii_nway_restart(&ep->mii);
  596. }
  597. static u32 ep93xx_get_link(struct net_device *dev)
  598. {
  599. struct ep93xx_priv *ep = netdev_priv(dev);
  600. return mii_link_ok(&ep->mii);
  601. }
  602. static const struct ethtool_ops ep93xx_ethtool_ops = {
  603. .get_drvinfo = ep93xx_get_drvinfo,
  604. .get_settings = ep93xx_get_settings,
  605. .set_settings = ep93xx_set_settings,
  606. .nway_reset = ep93xx_nway_reset,
  607. .get_link = ep93xx_get_link,
  608. };
  609. static const struct net_device_ops ep93xx_netdev_ops = {
  610. .ndo_open = ep93xx_open,
  611. .ndo_stop = ep93xx_close,
  612. .ndo_start_xmit = ep93xx_xmit,
  613. .ndo_do_ioctl = ep93xx_ioctl,
  614. .ndo_validate_addr = eth_validate_addr,
  615. .ndo_change_mtu = eth_change_mtu,
  616. .ndo_set_mac_address = eth_mac_addr,
  617. };
  618. static struct net_device *ep93xx_dev_alloc(struct ep93xx_eth_data *data)
  619. {
  620. struct net_device *dev;
  621. dev = alloc_etherdev(sizeof(struct ep93xx_priv));
  622. if (dev == NULL)
  623. return NULL;
  624. memcpy(dev->dev_addr, data->dev_addr, ETH_ALEN);
  625. dev->ethtool_ops = &ep93xx_ethtool_ops;
  626. dev->netdev_ops = &ep93xx_netdev_ops;
  627. dev->features |= NETIF_F_SG | NETIF_F_HW_CSUM;
  628. return dev;
  629. }
  630. static int ep93xx_eth_remove(struct platform_device *pdev)
  631. {
  632. struct net_device *dev;
  633. struct ep93xx_priv *ep;
  634. dev = platform_get_drvdata(pdev);
  635. if (dev == NULL)
  636. return 0;
  637. ep = netdev_priv(dev);
  638. /* @@@ Force down. */
  639. unregister_netdev(dev);
  640. ep93xx_free_buffers(ep);
  641. if (ep->base_addr != NULL)
  642. iounmap(ep->base_addr);
  643. if (ep->res != NULL) {
  644. release_resource(ep->res);
  645. kfree(ep->res);
  646. }
  647. free_netdev(dev);
  648. return 0;
  649. }
  650. static int ep93xx_eth_probe(struct platform_device *pdev)
  651. {
  652. struct ep93xx_eth_data *data;
  653. struct net_device *dev;
  654. struct ep93xx_priv *ep;
  655. struct resource *mem;
  656. int irq;
  657. int err;
  658. if (pdev == NULL)
  659. return -ENODEV;
  660. data = dev_get_platdata(&pdev->dev);
  661. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  662. irq = platform_get_irq(pdev, 0);
  663. if (!mem || irq < 0)
  664. return -ENXIO;
  665. dev = ep93xx_dev_alloc(data);
  666. if (dev == NULL) {
  667. err = -ENOMEM;
  668. goto err_out;
  669. }
  670. ep = netdev_priv(dev);
  671. ep->dev = dev;
  672. SET_NETDEV_DEV(dev, &pdev->dev);
  673. netif_napi_add(dev, &ep->napi, ep93xx_poll, 64);
  674. platform_set_drvdata(pdev, dev);
  675. ep->res = request_mem_region(mem->start, resource_size(mem),
  676. dev_name(&pdev->dev));
  677. if (ep->res == NULL) {
  678. dev_err(&pdev->dev, "Could not reserve memory region\n");
  679. err = -ENOMEM;
  680. goto err_out;
  681. }
  682. ep->base_addr = ioremap(mem->start, resource_size(mem));
  683. if (ep->base_addr == NULL) {
  684. dev_err(&pdev->dev, "Failed to ioremap ethernet registers\n");
  685. err = -EIO;
  686. goto err_out;
  687. }
  688. ep->irq = irq;
  689. ep->mii.phy_id = data->phy_id;
  690. ep->mii.phy_id_mask = 0x1f;
  691. ep->mii.reg_num_mask = 0x1f;
  692. ep->mii.dev = dev;
  693. ep->mii.mdio_read = ep93xx_mdio_read;
  694. ep->mii.mdio_write = ep93xx_mdio_write;
  695. ep->mdc_divisor = 40; /* Max HCLK 100 MHz, min MDIO clk 2.5 MHz. */
  696. if (is_zero_ether_addr(dev->dev_addr))
  697. eth_hw_addr_random(dev);
  698. err = register_netdev(dev);
  699. if (err) {
  700. dev_err(&pdev->dev, "Failed to register netdev\n");
  701. goto err_out;
  702. }
  703. printk(KERN_INFO "%s: ep93xx on-chip ethernet, IRQ %d, %pM\n",
  704. dev->name, ep->irq, dev->dev_addr);
  705. return 0;
  706. err_out:
  707. ep93xx_eth_remove(pdev);
  708. return err;
  709. }
  710. static struct platform_driver ep93xx_eth_driver = {
  711. .probe = ep93xx_eth_probe,
  712. .remove = ep93xx_eth_remove,
  713. .driver = {
  714. .name = "ep93xx-eth",
  715. },
  716. };
  717. module_platform_driver(ep93xx_eth_driver);
  718. MODULE_LICENSE("GPL");
  719. MODULE_ALIAS("platform:ep93xx-eth");