zorro8390.c 13 KB

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