mcf8390.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  1. /*
  2. * Support for ColdFire CPU based boards using a NS8390 Ethernet device.
  3. *
  4. * Derived from the many other 8390 drivers.
  5. *
  6. * (C) Copyright 2012, Greg Ungerer <gerg@uclinux.org>
  7. *
  8. * This file is subject to the terms and conditions of the GNU General Public
  9. * License. See the file COPYING in the main directory of the Linux
  10. * distribution for more details.
  11. */
  12. #include <linux/module.h>
  13. #include <linux/kernel.h>
  14. #include <linux/errno.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/netdevice.h>
  17. #include <linux/etherdevice.h>
  18. #include <linux/jiffies.h>
  19. #include <linux/io.h>
  20. #include <asm/mcf8390.h>
  21. static const char version[] =
  22. "mcf8390.c: (15-06-2012) Greg Ungerer <gerg@uclinux.org>";
  23. #define NE_CMD 0x00
  24. #define NE_DATAPORT 0x10 /* NatSemi-defined port window offset */
  25. #define NE_RESET 0x1f /* Issue a read to reset ,a write to clear */
  26. #define NE_EN0_ISR 0x07
  27. #define NE_EN0_DCFG 0x0e
  28. #define NE_EN0_RSARLO 0x08
  29. #define NE_EN0_RSARHI 0x09
  30. #define NE_EN0_RCNTLO 0x0a
  31. #define NE_EN0_RXCR 0x0c
  32. #define NE_EN0_TXCR 0x0d
  33. #define NE_EN0_RCNTHI 0x0b
  34. #define NE_EN0_IMR 0x0f
  35. #define NESM_START_PG 0x40 /* First page of TX buffer */
  36. #define NESM_STOP_PG 0x80 /* Last page +1 of RX ring */
  37. static u32 mcf8390_msg_enable;
  38. #ifdef NE2000_ODDOFFSET
  39. /*
  40. * A lot of the ColdFire boards use a separate address region for odd offset
  41. * register addresses. The following functions convert and map as required.
  42. * Note that the data port accesses are treated a little differently, and
  43. * always accessed via the insX/outsX functions.
  44. */
  45. static inline u32 NE_PTR(u32 addr)
  46. {
  47. if (addr & 1)
  48. return addr - 1 + NE2000_ODDOFFSET;
  49. return addr;
  50. }
  51. static inline u32 NE_DATA_PTR(u32 addr)
  52. {
  53. return addr;
  54. }
  55. void ei_outb(u32 val, u32 addr)
  56. {
  57. NE2000_BYTE *rp;
  58. rp = (NE2000_BYTE *) NE_PTR(addr);
  59. *rp = RSWAP(val);
  60. }
  61. #define ei_inb ei_inb
  62. u8 ei_inb(u32 addr)
  63. {
  64. NE2000_BYTE *rp, val;
  65. rp = (NE2000_BYTE *) NE_PTR(addr);
  66. val = *rp;
  67. return (u8) (RSWAP(val) & 0xff);
  68. }
  69. void ei_insb(u32 addr, void *vbuf, int len)
  70. {
  71. NE2000_BYTE *rp, val;
  72. u8 *buf;
  73. buf = (u8 *) vbuf;
  74. rp = (NE2000_BYTE *) NE_DATA_PTR(addr);
  75. for (; (len > 0); len--) {
  76. val = *rp;
  77. *buf++ = RSWAP(val);
  78. }
  79. }
  80. void ei_insw(u32 addr, void *vbuf, int len)
  81. {
  82. volatile u16 *rp;
  83. u16 w, *buf;
  84. buf = (u16 *) vbuf;
  85. rp = (volatile u16 *) NE_DATA_PTR(addr);
  86. for (; (len > 0); len--) {
  87. w = *rp;
  88. *buf++ = BSWAP(w);
  89. }
  90. }
  91. void ei_outsb(u32 addr, const void *vbuf, int len)
  92. {
  93. NE2000_BYTE *rp, val;
  94. u8 *buf;
  95. buf = (u8 *) vbuf;
  96. rp = (NE2000_BYTE *) NE_DATA_PTR(addr);
  97. for (; (len > 0); len--) {
  98. val = *buf++;
  99. *rp = RSWAP(val);
  100. }
  101. }
  102. void ei_outsw(u32 addr, const void *vbuf, int len)
  103. {
  104. volatile u16 *rp;
  105. u16 w, *buf;
  106. buf = (u16 *) vbuf;
  107. rp = (volatile u16 *) NE_DATA_PTR(addr);
  108. for (; (len > 0); len--) {
  109. w = *buf++;
  110. *rp = BSWAP(w);
  111. }
  112. }
  113. #else /* !NE2000_ODDOFFSET */
  114. #define ei_inb inb
  115. #define ei_outb outb
  116. #define ei_insb insb
  117. #define ei_insw insw
  118. #define ei_outsb outsb
  119. #define ei_outsw outsw
  120. #endif /* !NE2000_ODDOFFSET */
  121. #define ei_inb_p ei_inb
  122. #define ei_outb_p ei_outb
  123. #include "lib8390.c"
  124. /*
  125. * Hard reset the card. This used to pause for the same period that a
  126. * 8390 reset command required, but that shouldn't be necessary.
  127. */
  128. static void mcf8390_reset_8390(struct net_device *dev)
  129. {
  130. unsigned long reset_start_time = jiffies;
  131. u32 addr = dev->base_addr;
  132. struct ei_device *ei_local = netdev_priv(dev);
  133. netif_dbg(ei_local, hw, dev, "resetting the 8390 t=%ld...\n", jiffies);
  134. ei_outb(ei_inb(addr + NE_RESET), addr + NE_RESET);
  135. ei_status.txing = 0;
  136. ei_status.dmaing = 0;
  137. /* This check _should_not_ be necessary, omit eventually. */
  138. while ((ei_inb(addr + NE_EN0_ISR) & ENISR_RESET) == 0) {
  139. if (time_after(jiffies, reset_start_time + 2 * HZ / 100)) {
  140. netdev_warn(dev, "%s: did not complete\n", __func__);
  141. break;
  142. }
  143. }
  144. ei_outb(ENISR_RESET, addr + NE_EN0_ISR);
  145. }
  146. /*
  147. * This *shouldn't* happen.
  148. * If it does, it's the last thing you'll see
  149. */
  150. static void mcf8390_dmaing_err(const char *func, struct net_device *dev,
  151. struct ei_device *ei_local)
  152. {
  153. netdev_err(dev, "%s: DMAing conflict [DMAstat:%d][irqlock:%d]\n",
  154. func, ei_local->dmaing, ei_local->irqlock);
  155. }
  156. /*
  157. * Grab the 8390 specific header. Similar to the block_input routine, but
  158. * we don't need to be concerned with ring wrap as the header will be at
  159. * the start of a page, so we optimize accordingly.
  160. */
  161. static void mcf8390_get_8390_hdr(struct net_device *dev,
  162. struct e8390_pkt_hdr *hdr, int ring_page)
  163. {
  164. struct ei_device *ei_local = netdev_priv(dev);
  165. u32 addr = dev->base_addr;
  166. if (ei_local->dmaing) {
  167. mcf8390_dmaing_err(__func__, dev, ei_local);
  168. return;
  169. }
  170. ei_local->dmaing |= 0x01;
  171. ei_outb(E8390_NODMA + E8390_PAGE0 + E8390_START, addr + NE_CMD);
  172. ei_outb(ENISR_RDC, addr + NE_EN0_ISR);
  173. ei_outb(sizeof(struct e8390_pkt_hdr), addr + NE_EN0_RCNTLO);
  174. ei_outb(0, addr + NE_EN0_RCNTHI);
  175. ei_outb(0, addr + NE_EN0_RSARLO); /* On page boundary */
  176. ei_outb(ring_page, addr + NE_EN0_RSARHI);
  177. ei_outb(E8390_RREAD + E8390_START, addr + NE_CMD);
  178. ei_insw(addr + NE_DATAPORT, hdr, sizeof(struct e8390_pkt_hdr) >> 1);
  179. outb(ENISR_RDC, addr + NE_EN0_ISR); /* Ack intr */
  180. ei_local->dmaing &= ~0x01;
  181. hdr->count = cpu_to_le16(hdr->count);
  182. }
  183. /*
  184. * Block input and output, similar to the Crynwr packet driver.
  185. * If you are porting to a new ethercard, look at the packet driver source
  186. * for hints. The NEx000 doesn't share the on-board packet memory --
  187. * you have to put the packet out through the "remote DMA" dataport
  188. * using z_writeb.
  189. */
  190. static void mcf8390_block_input(struct net_device *dev, int count,
  191. struct sk_buff *skb, int ring_offset)
  192. {
  193. struct ei_device *ei_local = netdev_priv(dev);
  194. u32 addr = dev->base_addr;
  195. char *buf = skb->data;
  196. if (ei_local->dmaing) {
  197. mcf8390_dmaing_err(__func__, dev, ei_local);
  198. return;
  199. }
  200. ei_local->dmaing |= 0x01;
  201. ei_outb(E8390_NODMA + E8390_PAGE0 + E8390_START, addr + NE_CMD);
  202. ei_outb(ENISR_RDC, addr + NE_EN0_ISR);
  203. ei_outb(count & 0xff, addr + NE_EN0_RCNTLO);
  204. ei_outb(count >> 8, addr + NE_EN0_RCNTHI);
  205. ei_outb(ring_offset & 0xff, addr + NE_EN0_RSARLO);
  206. ei_outb(ring_offset >> 8, addr + NE_EN0_RSARHI);
  207. ei_outb(E8390_RREAD + E8390_START, addr + NE_CMD);
  208. ei_insw(addr + NE_DATAPORT, buf, count >> 1);
  209. if (count & 1)
  210. buf[count - 1] = ei_inb(addr + NE_DATAPORT);
  211. ei_outb(ENISR_RDC, addr + NE_EN0_ISR); /* Ack intr */
  212. ei_local->dmaing &= ~0x01;
  213. }
  214. static void mcf8390_block_output(struct net_device *dev, int count,
  215. const unsigned char *buf,
  216. const int start_page)
  217. {
  218. struct ei_device *ei_local = netdev_priv(dev);
  219. u32 addr = dev->base_addr;
  220. unsigned long dma_start;
  221. /* Make sure we transfer all bytes if 16bit IO writes */
  222. if (count & 0x1)
  223. count++;
  224. if (ei_local->dmaing) {
  225. mcf8390_dmaing_err(__func__, dev, ei_local);
  226. return;
  227. }
  228. ei_local->dmaing |= 0x01;
  229. /* We should already be in page 0, but to be safe... */
  230. ei_outb(E8390_PAGE0 + E8390_START + E8390_NODMA, addr + NE_CMD);
  231. ei_outb(ENISR_RDC, addr + NE_EN0_ISR);
  232. /* Now the normal output. */
  233. ei_outb(count & 0xff, addr + NE_EN0_RCNTLO);
  234. ei_outb(count >> 8, addr + NE_EN0_RCNTHI);
  235. ei_outb(0x00, addr + NE_EN0_RSARLO);
  236. ei_outb(start_page, addr + NE_EN0_RSARHI);
  237. ei_outb(E8390_RWRITE + E8390_START, addr + NE_CMD);
  238. ei_outsw(addr + NE_DATAPORT, buf, count >> 1);
  239. dma_start = jiffies;
  240. while ((ei_inb(addr + NE_EN0_ISR) & ENISR_RDC) == 0) {
  241. if (time_after(jiffies, dma_start + 2 * HZ / 100)) { /* 20ms */
  242. netdev_warn(dev, "timeout waiting for Tx RDC\n");
  243. mcf8390_reset_8390(dev);
  244. __NS8390_init(dev, 1);
  245. break;
  246. }
  247. }
  248. ei_outb(ENISR_RDC, addr + NE_EN0_ISR); /* Ack intr */
  249. ei_local->dmaing &= ~0x01;
  250. }
  251. static const struct net_device_ops mcf8390_netdev_ops = {
  252. .ndo_open = __ei_open,
  253. .ndo_stop = __ei_close,
  254. .ndo_start_xmit = __ei_start_xmit,
  255. .ndo_tx_timeout = __ei_tx_timeout,
  256. .ndo_get_stats = __ei_get_stats,
  257. .ndo_set_rx_mode = __ei_set_multicast_list,
  258. .ndo_validate_addr = eth_validate_addr,
  259. .ndo_set_mac_address = eth_mac_addr,
  260. .ndo_change_mtu = eth_change_mtu,
  261. #ifdef CONFIG_NET_POLL_CONTROLLER
  262. .ndo_poll_controller = __ei_poll,
  263. #endif
  264. };
  265. static int mcf8390_init(struct net_device *dev)
  266. {
  267. static u32 offsets[] = {
  268. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  269. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  270. };
  271. struct ei_device *ei_local = netdev_priv(dev);
  272. unsigned char SA_prom[32];
  273. u32 addr = dev->base_addr;
  274. int start_page, stop_page;
  275. int i, ret;
  276. mcf8390_reset_8390(dev);
  277. /*
  278. * Read the 16 bytes of station address PROM.
  279. * We must first initialize registers,
  280. * similar to NS8390_init(eifdev, 0).
  281. * We can't reliably read the SAPROM address without this.
  282. * (I learned the hard way!).
  283. */
  284. {
  285. static const struct {
  286. u32 value;
  287. u32 offset;
  288. } program_seq[] = {
  289. {E8390_NODMA + E8390_PAGE0 + E8390_STOP, NE_CMD},
  290. /* Select page 0 */
  291. {0x48, NE_EN0_DCFG}, /* 0x48: Set byte-wide access */
  292. {0x00, NE_EN0_RCNTLO}, /* Clear the count regs */
  293. {0x00, NE_EN0_RCNTHI},
  294. {0x00, NE_EN0_IMR}, /* Mask completion irq */
  295. {0xFF, NE_EN0_ISR},
  296. {E8390_RXOFF, NE_EN0_RXCR}, /* 0x20 Set to monitor */
  297. {E8390_TXOFF, NE_EN0_TXCR}, /* 0x02 and loopback mode */
  298. {32, NE_EN0_RCNTLO},
  299. {0x00, NE_EN0_RCNTHI},
  300. {0x00, NE_EN0_RSARLO}, /* DMA starting at 0x0000 */
  301. {0x00, NE_EN0_RSARHI},
  302. {E8390_RREAD + E8390_START, NE_CMD},
  303. };
  304. for (i = 0; i < ARRAY_SIZE(program_seq); i++) {
  305. ei_outb(program_seq[i].value,
  306. addr + program_seq[i].offset);
  307. }
  308. }
  309. for (i = 0; i < 16; i++) {
  310. SA_prom[i] = ei_inb(addr + NE_DATAPORT);
  311. ei_inb(addr + NE_DATAPORT);
  312. }
  313. /* We must set the 8390 for word mode. */
  314. ei_outb(0x49, addr + NE_EN0_DCFG);
  315. start_page = NESM_START_PG;
  316. stop_page = NESM_STOP_PG;
  317. /* Install the Interrupt handler */
  318. ret = request_irq(dev->irq, __ei_interrupt, 0, dev->name, dev);
  319. if (ret)
  320. return ret;
  321. for (i = 0; i < ETH_ALEN; i++)
  322. dev->dev_addr[i] = SA_prom[i];
  323. netdev_dbg(dev, "Found ethernet address: %pM\n", dev->dev_addr);
  324. ei_local->name = "mcf8390";
  325. ei_local->tx_start_page = start_page;
  326. ei_local->stop_page = stop_page;
  327. ei_local->word16 = 1;
  328. ei_local->rx_start_page = start_page + TX_PAGES;
  329. ei_local->reset_8390 = mcf8390_reset_8390;
  330. ei_local->block_input = mcf8390_block_input;
  331. ei_local->block_output = mcf8390_block_output;
  332. ei_local->get_8390_hdr = mcf8390_get_8390_hdr;
  333. ei_local->reg_offset = offsets;
  334. dev->netdev_ops = &mcf8390_netdev_ops;
  335. __NS8390_init(dev, 0);
  336. ret = register_netdev(dev);
  337. if (ret) {
  338. free_irq(dev->irq, dev);
  339. return ret;
  340. }
  341. netdev_info(dev, "addr=0x%08x irq=%d, Ethernet Address %pM\n",
  342. addr, dev->irq, dev->dev_addr);
  343. return 0;
  344. }
  345. static int mcf8390_probe(struct platform_device *pdev)
  346. {
  347. struct net_device *dev;
  348. struct ei_device *ei_local;
  349. struct resource *mem, *irq;
  350. resource_size_t msize;
  351. int ret;
  352. irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  353. if (irq == NULL) {
  354. dev_err(&pdev->dev, "no IRQ specified?\n");
  355. return -ENXIO;
  356. }
  357. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  358. if (mem == NULL) {
  359. dev_err(&pdev->dev, "no memory address specified?\n");
  360. return -ENXIO;
  361. }
  362. msize = resource_size(mem);
  363. if (!request_mem_region(mem->start, msize, pdev->name))
  364. return -EBUSY;
  365. dev = ____alloc_ei_netdev(0);
  366. if (dev == NULL) {
  367. release_mem_region(mem->start, msize);
  368. return -ENOMEM;
  369. }
  370. SET_NETDEV_DEV(dev, &pdev->dev);
  371. platform_set_drvdata(pdev, dev);
  372. ei_local = netdev_priv(dev);
  373. ei_local->msg_enable = mcf8390_msg_enable;
  374. dev->irq = irq->start;
  375. dev->base_addr = mem->start;
  376. ret = mcf8390_init(dev);
  377. if (ret) {
  378. release_mem_region(mem->start, msize);
  379. free_netdev(dev);
  380. return ret;
  381. }
  382. return 0;
  383. }
  384. static int mcf8390_remove(struct platform_device *pdev)
  385. {
  386. struct net_device *dev = platform_get_drvdata(pdev);
  387. struct resource *mem;
  388. unregister_netdev(dev);
  389. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  390. if (mem)
  391. release_mem_region(mem->start, resource_size(mem));
  392. free_netdev(dev);
  393. return 0;
  394. }
  395. static struct platform_driver mcf8390_drv = {
  396. .driver = {
  397. .name = "mcf8390",
  398. },
  399. .probe = mcf8390_probe,
  400. .remove = mcf8390_remove,
  401. };
  402. module_platform_driver(mcf8390_drv);
  403. MODULE_DESCRIPTION("MCF8390 ColdFire NS8390 driver");
  404. MODULE_AUTHOR("Greg Ungerer <gerg@uclinux.org>");
  405. MODULE_LICENSE("GPL");
  406. MODULE_ALIAS("platform:mcf8390");