zorro8390.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Amiga Linux/m68k and Linux/PPC Zorro NS8390 Ethernet Driver
  4. *
  5. * (C) Copyright 1998-2000 by some Elitist 680x0 Users(TM)
  6. *
  7. * ---------------------------------------------------------------------------
  8. *
  9. * This program is based on all the other NE2000 drivers for Linux
  10. *
  11. * ---------------------------------------------------------------------------
  12. *
  13. * The Ariadne II and X-Surf are Zorro-II boards containing Realtek RTL8019AS
  14. * Ethernet Controllers.
  15. */
  16. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  17. #include <linux/module.h>
  18. #include <linux/kernel.h>
  19. #include <linux/errno.h>
  20. #include <linux/init.h>
  21. #include <linux/delay.h>
  22. #include <linux/netdevice.h>
  23. #include <linux/etherdevice.h>
  24. #include <linux/zorro.h>
  25. #include <linux/jiffies.h>
  26. #include <asm/irq.h>
  27. #include <asm/amigaints.h>
  28. #include <asm/amigahw.h>
  29. #define EI_SHIFT(x) (ei_local->reg_offset[x])
  30. #define ei_inb(port) in_8(port)
  31. #define ei_outb(val, port) out_8(port, val)
  32. #define ei_inb_p(port) in_8(port)
  33. #define ei_outb_p(val, port) out_8(port, val)
  34. static const char version[] =
  35. "8390.c:v1.10cvs 9/23/94 Donald Becker (becker@cesdis.gsfc.nasa.gov)\n";
  36. #include "lib8390.c"
  37. #define DRV_NAME "zorro8390"
  38. #define NE_BASE (dev->base_addr)
  39. #define NE_CMD (0x00 * 2)
  40. #define NE_DATAPORT (0x10 * 2) /* NatSemi-defined port window offset */
  41. #define NE_RESET (0x1f * 2) /* Issue a read to reset,
  42. * a write to clear. */
  43. #define NE_IO_EXTENT (0x20 * 2)
  44. #define NE_EN0_ISR (0x07 * 2)
  45. #define NE_EN0_DCFG (0x0e * 2)
  46. #define NE_EN0_RSARLO (0x08 * 2)
  47. #define NE_EN0_RSARHI (0x09 * 2)
  48. #define NE_EN0_RCNTLO (0x0a * 2)
  49. #define NE_EN0_RXCR (0x0c * 2)
  50. #define NE_EN0_TXCR (0x0d * 2)
  51. #define NE_EN0_RCNTHI (0x0b * 2)
  52. #define NE_EN0_IMR (0x0f * 2)
  53. #define NESM_START_PG 0x40 /* First page of TX buffer */
  54. #define NESM_STOP_PG 0x80 /* Last page +1 of RX ring */
  55. #define WORDSWAP(a) ((((a) >> 8) & 0xff) | ((a) << 8))
  56. static struct card_info {
  57. zorro_id id;
  58. const char *name;
  59. unsigned int offset;
  60. } cards[] = {
  61. { ZORRO_PROD_VILLAGE_TRONIC_ARIADNE2, "Ariadne II", 0x0600 },
  62. { ZORRO_PROD_INDIVIDUAL_COMPUTERS_X_SURF, "X-Surf", 0x8600 },
  63. };
  64. /* Hard reset the card. This used to pause for the same period that a
  65. * 8390 reset command required, but that shouldn't be necessary.
  66. */
  67. static void zorro8390_reset_8390(struct net_device *dev)
  68. {
  69. unsigned long reset_start_time = jiffies;
  70. struct ei_device *ei_local = netdev_priv(dev);
  71. netif_dbg(ei_local, hw, dev, "resetting - t=%ld...\n", jiffies);
  72. z_writeb(z_readb(NE_BASE + NE_RESET), NE_BASE + NE_RESET);
  73. ei_status.txing = 0;
  74. ei_status.dmaing = 0;
  75. /* This check _should_not_ be necessary, omit eventually. */
  76. while ((z_readb(NE_BASE + NE_EN0_ISR) & ENISR_RESET) == 0)
  77. if (time_after(jiffies, reset_start_time + 2 * HZ / 100)) {
  78. netdev_warn(dev, "%s: did not complete\n", __func__);
  79. break;
  80. }
  81. z_writeb(ENISR_RESET, NE_BASE + NE_EN0_ISR); /* Ack intr */
  82. }
  83. /* Grab the 8390 specific header. Similar to the block_input routine, but
  84. * we don't need to be concerned with ring wrap as the header will be at
  85. * the start of a page, so we optimize accordingly.
  86. */
  87. static void zorro8390_get_8390_hdr(struct net_device *dev,
  88. struct e8390_pkt_hdr *hdr, int ring_page)
  89. {
  90. int nic_base = dev->base_addr;
  91. int cnt;
  92. short *ptrs;
  93. /* This *shouldn't* happen.
  94. * If it does, it's the last thing you'll see
  95. */
  96. if (ei_status.dmaing) {
  97. netdev_warn(dev,
  98. "%s: DMAing conflict [DMAstat:%d][irqlock:%d]\n",
  99. __func__, ei_status.dmaing, ei_status.irqlock);
  100. return;
  101. }
  102. ei_status.dmaing |= 0x01;
  103. z_writeb(E8390_NODMA + E8390_PAGE0 + E8390_START, nic_base + NE_CMD);
  104. z_writeb(ENISR_RDC, nic_base + NE_EN0_ISR);
  105. z_writeb(sizeof(struct e8390_pkt_hdr), nic_base + NE_EN0_RCNTLO);
  106. z_writeb(0, nic_base + NE_EN0_RCNTHI);
  107. z_writeb(0, nic_base + NE_EN0_RSARLO); /* On page boundary */
  108. z_writeb(ring_page, nic_base + NE_EN0_RSARHI);
  109. z_writeb(E8390_RREAD+E8390_START, nic_base + NE_CMD);
  110. ptrs = (short *)hdr;
  111. for (cnt = 0; cnt < sizeof(struct e8390_pkt_hdr) >> 1; cnt++)
  112. *ptrs++ = z_readw(NE_BASE + NE_DATAPORT);
  113. z_writeb(ENISR_RDC, nic_base + NE_EN0_ISR); /* Ack intr */
  114. hdr->count = WORDSWAP(hdr->count);
  115. ei_status.dmaing &= ~0x01;
  116. }
  117. /* Block input and output, similar to the Crynwr packet driver.
  118. * If you are porting to a new ethercard, look at the packet driver source
  119. * for hints. The NEx000 doesn't share the on-board packet memory --
  120. * you have to put the packet out through the "remote DMA" dataport
  121. * using z_writeb.
  122. */
  123. static void zorro8390_block_input(struct net_device *dev, int count,
  124. struct sk_buff *skb, int ring_offset)
  125. {
  126. int nic_base = dev->base_addr;
  127. char *buf = skb->data;
  128. short *ptrs;
  129. int cnt;
  130. /* This *shouldn't* happen.
  131. * If it does, it's the last thing you'll see
  132. */
  133. if (ei_status.dmaing) {
  134. netdev_err(dev, "%s: DMAing conflict [DMAstat:%d][irqlock:%d]\n",
  135. __func__, ei_status.dmaing, ei_status.irqlock);
  136. return;
  137. }
  138. ei_status.dmaing |= 0x01;
  139. z_writeb(E8390_NODMA + E8390_PAGE0 + E8390_START, nic_base + NE_CMD);
  140. z_writeb(ENISR_RDC, nic_base + NE_EN0_ISR);
  141. z_writeb(count & 0xff, nic_base + NE_EN0_RCNTLO);
  142. z_writeb(count >> 8, nic_base + NE_EN0_RCNTHI);
  143. z_writeb(ring_offset & 0xff, nic_base + NE_EN0_RSARLO);
  144. z_writeb(ring_offset >> 8, nic_base + NE_EN0_RSARHI);
  145. z_writeb(E8390_RREAD+E8390_START, nic_base + NE_CMD);
  146. ptrs = (short *)buf;
  147. for (cnt = 0; cnt < count >> 1; cnt++)
  148. *ptrs++ = z_readw(NE_BASE + NE_DATAPORT);
  149. if (count & 0x01)
  150. buf[count - 1] = z_readb(NE_BASE + NE_DATAPORT);
  151. z_writeb(ENISR_RDC, nic_base + NE_EN0_ISR); /* Ack intr */
  152. ei_status.dmaing &= ~0x01;
  153. }
  154. static void zorro8390_block_output(struct net_device *dev, int count,
  155. const unsigned char *buf,
  156. const int start_page)
  157. {
  158. int nic_base = NE_BASE;
  159. unsigned long dma_start;
  160. short *ptrs;
  161. int cnt;
  162. /* Round the count up for word writes. Do we need to do this?
  163. * What effect will an odd byte count have on the 8390?
  164. * I should check someday.
  165. */
  166. if (count & 0x01)
  167. count++;
  168. /* This *shouldn't* happen.
  169. * If it does, it's the last thing you'll see
  170. */
  171. if (ei_status.dmaing) {
  172. netdev_err(dev, "%s: DMAing conflict [DMAstat:%d][irqlock:%d]\n",
  173. __func__, ei_status.dmaing, ei_status.irqlock);
  174. return;
  175. }
  176. ei_status.dmaing |= 0x01;
  177. /* We should already be in page 0, but to be safe... */
  178. z_writeb(E8390_PAGE0+E8390_START+E8390_NODMA, nic_base + NE_CMD);
  179. z_writeb(ENISR_RDC, nic_base + NE_EN0_ISR);
  180. /* Now the normal output. */
  181. z_writeb(count & 0xff, nic_base + NE_EN0_RCNTLO);
  182. z_writeb(count >> 8, nic_base + NE_EN0_RCNTHI);
  183. z_writeb(0x00, nic_base + NE_EN0_RSARLO);
  184. z_writeb(start_page, nic_base + NE_EN0_RSARHI);
  185. z_writeb(E8390_RWRITE + E8390_START, nic_base + NE_CMD);
  186. ptrs = (short *)buf;
  187. for (cnt = 0; cnt < count >> 1; cnt++)
  188. z_writew(*ptrs++, NE_BASE + NE_DATAPORT);
  189. dma_start = jiffies;
  190. while ((z_readb(NE_BASE + NE_EN0_ISR) & ENISR_RDC) == 0)
  191. if (time_after(jiffies, dma_start + 2 * HZ / 100)) {
  192. /* 20ms */
  193. netdev_warn(dev, "timeout waiting for Tx RDC\n");
  194. zorro8390_reset_8390(dev);
  195. __NS8390_init(dev, 1);
  196. break;
  197. }
  198. z_writeb(ENISR_RDC, nic_base + NE_EN0_ISR); /* Ack intr */
  199. ei_status.dmaing &= ~0x01;
  200. }
  201. static int zorro8390_open(struct net_device *dev)
  202. {
  203. __ei_open(dev);
  204. return 0;
  205. }
  206. static int zorro8390_close(struct net_device *dev)
  207. {
  208. struct ei_device *ei_local = netdev_priv(dev);
  209. netif_dbg(ei_local, ifdown, dev, "Shutting down ethercard\n");
  210. __ei_close(dev);
  211. return 0;
  212. }
  213. static void zorro8390_remove_one(struct zorro_dev *z)
  214. {
  215. struct net_device *dev = zorro_get_drvdata(z);
  216. unregister_netdev(dev);
  217. free_irq(IRQ_AMIGA_PORTS, dev);
  218. release_mem_region(ZTWO_PADDR(dev->base_addr), NE_IO_EXTENT * 2);
  219. free_netdev(dev);
  220. }
  221. static struct zorro_device_id zorro8390_zorro_tbl[] = {
  222. { ZORRO_PROD_VILLAGE_TRONIC_ARIADNE2, },
  223. { ZORRO_PROD_INDIVIDUAL_COMPUTERS_X_SURF, },
  224. { 0 }
  225. };
  226. MODULE_DEVICE_TABLE(zorro, zorro8390_zorro_tbl);
  227. static const struct net_device_ops zorro8390_netdev_ops = {
  228. .ndo_open = zorro8390_open,
  229. .ndo_stop = zorro8390_close,
  230. .ndo_start_xmit = __ei_start_xmit,
  231. .ndo_tx_timeout = __ei_tx_timeout,
  232. .ndo_get_stats = __ei_get_stats,
  233. .ndo_set_rx_mode = __ei_set_multicast_list,
  234. .ndo_validate_addr = eth_validate_addr,
  235. .ndo_set_mac_address = eth_mac_addr,
  236. #ifdef CONFIG_NET_POLL_CONTROLLER
  237. .ndo_poll_controller = __ei_poll,
  238. #endif
  239. };
  240. static int zorro8390_init(struct net_device *dev, unsigned long board,
  241. const char *name, void __iomem *ioaddr)
  242. {
  243. int i;
  244. int err;
  245. unsigned char SA_prom[32];
  246. int start_page, stop_page;
  247. static u32 zorro8390_offsets[16] = {
  248. 0x00, 0x02, 0x04, 0x06, 0x08, 0x0a, 0x0c, 0x0e,
  249. 0x10, 0x12, 0x14, 0x16, 0x18, 0x1a, 0x1c, 0x1e,
  250. };
  251. /* Reset card. Who knows what dain-bramaged state it was left in. */
  252. {
  253. unsigned long reset_start_time = jiffies;
  254. z_writeb(z_readb(ioaddr + NE_RESET), ioaddr + NE_RESET);
  255. while ((z_readb(ioaddr + NE_EN0_ISR) & ENISR_RESET) == 0)
  256. if (time_after(jiffies,
  257. reset_start_time + 2 * HZ / 100)) {
  258. netdev_warn(dev, "not found (no reset ack)\n");
  259. return -ENODEV;
  260. }
  261. z_writeb(0xff, ioaddr + NE_EN0_ISR); /* Ack all intr. */
  262. }
  263. /* Read the 16 bytes of station address PROM.
  264. * We must first initialize registers,
  265. * similar to NS8390_init(eifdev, 0).
  266. * We can't reliably read the SAPROM address without this.
  267. * (I learned the hard way!).
  268. */
  269. {
  270. static const struct {
  271. u32 value;
  272. u32 offset;
  273. } program_seq[] = {
  274. {E8390_NODMA + E8390_PAGE0 + E8390_STOP, NE_CMD},
  275. /* Select page 0 */
  276. {0x48, NE_EN0_DCFG}, /* 0x48: Set byte-wide access */
  277. {0x00, NE_EN0_RCNTLO}, /* Clear the count regs */
  278. {0x00, NE_EN0_RCNTHI},
  279. {0x00, NE_EN0_IMR}, /* Mask completion irq */
  280. {0xFF, NE_EN0_ISR},
  281. {E8390_RXOFF, NE_EN0_RXCR}, /* 0x20 Set to monitor */
  282. {E8390_TXOFF, NE_EN0_TXCR}, /* 0x02 and loopback mode */
  283. {32, NE_EN0_RCNTLO},
  284. {0x00, NE_EN0_RCNTHI},
  285. {0x00, NE_EN0_RSARLO}, /* DMA starting at 0x0000 */
  286. {0x00, NE_EN0_RSARHI},
  287. {E8390_RREAD + E8390_START, NE_CMD},
  288. };
  289. for (i = 0; i < ARRAY_SIZE(program_seq); i++)
  290. z_writeb(program_seq[i].value,
  291. ioaddr + program_seq[i].offset);
  292. }
  293. for (i = 0; i < 16; i++) {
  294. SA_prom[i] = z_readb(ioaddr + NE_DATAPORT);
  295. (void)z_readb(ioaddr + NE_DATAPORT);
  296. }
  297. /* We must set the 8390 for word mode. */
  298. z_writeb(0x49, ioaddr + NE_EN0_DCFG);
  299. start_page = NESM_START_PG;
  300. stop_page = NESM_STOP_PG;
  301. dev->base_addr = (unsigned long)ioaddr;
  302. dev->irq = IRQ_AMIGA_PORTS;
  303. /* Install the Interrupt handler */
  304. i = request_irq(IRQ_AMIGA_PORTS, __ei_interrupt,
  305. IRQF_SHARED, DRV_NAME, dev);
  306. if (i)
  307. return i;
  308. eth_hw_addr_set(dev, SA_prom);
  309. pr_debug("Found ethernet address: %pM\n", dev->dev_addr);
  310. ei_status.name = name;
  311. ei_status.tx_start_page = start_page;
  312. ei_status.stop_page = stop_page;
  313. ei_status.word16 = 1;
  314. ei_status.rx_start_page = start_page + TX_PAGES;
  315. ei_status.reset_8390 = zorro8390_reset_8390;
  316. ei_status.block_input = zorro8390_block_input;
  317. ei_status.block_output = zorro8390_block_output;
  318. ei_status.get_8390_hdr = zorro8390_get_8390_hdr;
  319. ei_status.reg_offset = zorro8390_offsets;
  320. dev->netdev_ops = &zorro8390_netdev_ops;
  321. __NS8390_init(dev, 0);
  322. err = register_netdev(dev);
  323. if (err) {
  324. free_irq(IRQ_AMIGA_PORTS, dev);
  325. return err;
  326. }
  327. netdev_info(dev, "%s at 0x%08lx, Ethernet Address %pM\n",
  328. name, board, dev->dev_addr);
  329. return 0;
  330. }
  331. static int zorro8390_init_one(struct zorro_dev *z,
  332. const struct zorro_device_id *ent)
  333. {
  334. struct net_device *dev;
  335. unsigned long board, ioaddr;
  336. int err, i;
  337. for (i = ARRAY_SIZE(cards) - 1; i >= 0; i--)
  338. if (z->id == cards[i].id)
  339. break;
  340. if (i < 0)
  341. return -ENODEV;
  342. board = z->resource.start;
  343. ioaddr = board + cards[i].offset;
  344. dev = ____alloc_ei_netdev(0);
  345. if (!dev)
  346. return -ENOMEM;
  347. if (!request_mem_region(ioaddr, NE_IO_EXTENT * 2, DRV_NAME)) {
  348. free_netdev(dev);
  349. return -EBUSY;
  350. }
  351. err = zorro8390_init(dev, board, cards[i].name, ZTWO_VADDR(ioaddr));
  352. if (err) {
  353. release_mem_region(ioaddr, NE_IO_EXTENT * 2);
  354. free_netdev(dev);
  355. return err;
  356. }
  357. zorro_set_drvdata(z, dev);
  358. return 0;
  359. }
  360. static struct zorro_driver zorro8390_driver = {
  361. .name = "zorro8390",
  362. .id_table = zorro8390_zorro_tbl,
  363. .probe = zorro8390_init_one,
  364. .remove = zorro8390_remove_one,
  365. };
  366. static int __init zorro8390_init_module(void)
  367. {
  368. return zorro_register_driver(&zorro8390_driver);
  369. }
  370. static void __exit zorro8390_cleanup_module(void)
  371. {
  372. zorro_unregister_driver(&zorro8390_driver);
  373. }
  374. module_init(zorro8390_init_module);
  375. module_exit(zorro8390_cleanup_module);
  376. MODULE_DESCRIPTION("Zorro NS8390-based ethernet driver");
  377. MODULE_LICENSE("GPL");