ep93xx_eth.c 20 KB

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