zorro8390.c 14 KB

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