zorro8390.c 13 KB

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