ne.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968
  1. /* ne.c: A general non-shared-memory NS8390 ethernet driver for linux. */
  2. /*
  3. Written 1992-94 by Donald Becker.
  4. Copyright 1993 United States Government as represented by the
  5. Director, National Security Agency.
  6. This software may be used and distributed according to the terms
  7. of the GNU General Public License, incorporated herein by reference.
  8. The author may be reached as becker@scyld.com, or C/O
  9. Scyld Computing Corporation, 410 Severn Ave., Suite 210, Annapolis MD 21403
  10. This driver should work with many programmed-I/O 8390-based ethernet
  11. boards. Currently it supports the NE1000, NE2000, many clones,
  12. and some Cabletron products.
  13. Changelog:
  14. Paul Gortmaker : use ENISR_RDC to monitor Tx PIO uploads, made
  15. sanity checks and bad clone support optional.
  16. Paul Gortmaker : new reset code, reset card after probe at boot.
  17. Paul Gortmaker : multiple card support for module users.
  18. Paul Gortmaker : Support for PCI ne2k clones, similar to lance.c
  19. Paul Gortmaker : Allow users with bad cards to avoid full probe.
  20. Paul Gortmaker : PCI probe changes, more PCI cards supported.
  21. rjohnson@analogic.com : Changed init order so an interrupt will only
  22. occur after memory is allocated for dev->priv. Deallocated memory
  23. last in cleanup_modue()
  24. Richard Guenther : Added support for ISAPnP cards
  25. Paul Gortmaker : Discontinued PCI support - use ne2k-pci.c instead.
  26. Hayato Fujiwara : Add m32r support.
  27. */
  28. /* Routines for the NatSemi-based designs (NE[12]000). */
  29. static const char version1[] =
  30. "ne.c:v1.10 9/23/94 Donald Becker (becker@scyld.com)\n";
  31. static const char version2[] =
  32. "Last modified Nov 1, 2000 by Paul Gortmaker\n";
  33. #include <linux/module.h>
  34. #include <linux/kernel.h>
  35. #include <linux/errno.h>
  36. #include <linux/isapnp.h>
  37. #include <linux/init.h>
  38. #include <linux/interrupt.h>
  39. #include <linux/delay.h>
  40. #include <linux/netdevice.h>
  41. #include <linux/etherdevice.h>
  42. #include <linux/jiffies.h>
  43. #include <linux/platform_device.h>
  44. #include <asm/system.h>
  45. #include <asm/io.h>
  46. #include "8390.h"
  47. #define DRV_NAME "ne"
  48. /* Some defines that people can play with if so inclined. */
  49. /* Do we support clones that don't adhere to 14,15 of the SAprom ? */
  50. #define SUPPORT_NE_BAD_CLONES
  51. /* Do we perform extra sanity checks on stuff ? */
  52. /* #define NE_SANITY_CHECK */
  53. /* Do we implement the read before write bugfix ? */
  54. /* #define NE_RW_BUGFIX */
  55. /* Do we have a non std. amount of memory? (in units of 256 byte pages) */
  56. /* #define PACKETBUF_MEMSIZE 0x40 */
  57. #if !defined(MODULE) && (defined(CONFIG_ISA) || defined(CONFIG_M32R))
  58. /* Do we need a portlist for the ISA auto-probe ? */
  59. #define NEEDS_PORTLIST
  60. #endif
  61. /* A zero-terminated list of I/O addresses to be probed at boot. */
  62. #ifdef NEEDS_PORTLIST
  63. static unsigned int netcard_portlist[] __initdata = {
  64. 0x300, 0x280, 0x320, 0x340, 0x360, 0x380, 0
  65. };
  66. #endif
  67. static struct isapnp_device_id isapnp_clone_list[] __initdata = {
  68. { ISAPNP_CARD_ID('A','X','E',0x2011),
  69. ISAPNP_VENDOR('A','X','E'), ISAPNP_FUNCTION(0x2011),
  70. (long) "NetGear EA201" },
  71. { ISAPNP_ANY_ID, ISAPNP_ANY_ID,
  72. ISAPNP_VENDOR('E','D','I'), ISAPNP_FUNCTION(0x0216),
  73. (long) "NN NE2000" },
  74. { ISAPNP_ANY_ID, ISAPNP_ANY_ID,
  75. ISAPNP_VENDOR('P','N','P'), ISAPNP_FUNCTION(0x80d6),
  76. (long) "Generic PNP" },
  77. { } /* terminate list */
  78. };
  79. MODULE_DEVICE_TABLE(isapnp, isapnp_clone_list);
  80. #ifdef SUPPORT_NE_BAD_CLONES
  81. /* A list of bad clones that we none-the-less recognize. */
  82. static struct { const char *name8, *name16; unsigned char SAprefix[4];}
  83. bad_clone_list[] __initdata = {
  84. {"DE100", "DE200", {0x00, 0xDE, 0x01,}},
  85. {"DE120", "DE220", {0x00, 0x80, 0xc8,}},
  86. {"DFI1000", "DFI2000", {'D', 'F', 'I',}}, /* Original, eh? */
  87. {"EtherNext UTP8", "EtherNext UTP16", {0x00, 0x00, 0x79}},
  88. {"NE1000","NE2000-invalid", {0x00, 0x00, 0xd8}}, /* Ancient real NE1000. */
  89. {"NN1000", "NN2000", {0x08, 0x03, 0x08}}, /* Outlaw no-name clone. */
  90. {"4-DIM8","4-DIM16", {0x00,0x00,0x4d,}}, /* Outlaw 4-Dimension cards. */
  91. {"Con-Intl_8", "Con-Intl_16", {0x00, 0x00, 0x24}}, /* Connect Int'nl */
  92. {"ET-100","ET-200", {0x00, 0x45, 0x54}}, /* YANG and YA clone */
  93. {"COMPEX","COMPEX16",{0x00,0x80,0x48}}, /* Broken ISA Compex cards */
  94. {"E-LAN100", "E-LAN200", {0x00, 0x00, 0x5d}}, /* Broken ne1000 clones */
  95. {"PCM-4823", "PCM-4823", {0x00, 0xc0, 0x6c}}, /* Broken Advantech MoBo */
  96. {"REALTEK", "RTL8019", {0x00, 0x00, 0xe8}}, /* no-name with Realtek chip */
  97. #if defined(CONFIG_TOSHIBA_RBTX4927) || defined(CONFIG_TOSHIBA_RBTX4938)
  98. {"RBHMA4X00-RTL8019", "RBHMA4X00/RTL8019", {0x00, 0x60, 0x0a}}, /* Toshiba built-in */
  99. #endif
  100. {"LCS-8834", "LCS-8836", {0x04, 0x04, 0x37}}, /* ShinyNet (SET) */
  101. {NULL,}
  102. };
  103. #endif
  104. /* ---- No user-serviceable parts below ---- */
  105. #define NE_BASE (dev->base_addr)
  106. #define NE_CMD 0x00
  107. #define NE_DATAPORT 0x10 /* NatSemi-defined port window offset. */
  108. #define NE_RESET 0x1f /* Issue a read to reset, a write to clear. */
  109. #define NE_IO_EXTENT 0x20
  110. #define NE1SM_START_PG 0x20 /* First page of TX buffer */
  111. #define NE1SM_STOP_PG 0x40 /* Last page +1 of RX ring */
  112. #define NESM_START_PG 0x40 /* First page of TX buffer */
  113. #define NESM_STOP_PG 0x80 /* Last page +1 of RX ring */
  114. #if defined(CONFIG_PLAT_MAPPI)
  115. # define DCR_VAL 0x4b
  116. #elif defined(CONFIG_PLAT_OAKS32R) || \
  117. defined(CONFIG_TOSHIBA_RBTX4927) || defined(CONFIG_TOSHIBA_RBTX4938)
  118. # define DCR_VAL 0x48 /* 8-bit mode */
  119. #else
  120. # define DCR_VAL 0x49
  121. #endif
  122. static int ne_probe1(struct net_device *dev, unsigned long ioaddr);
  123. static int ne_probe_isapnp(struct net_device *dev);
  124. static int ne_open(struct net_device *dev);
  125. static int ne_close(struct net_device *dev);
  126. static void ne_reset_8390(struct net_device *dev);
  127. static void ne_get_8390_hdr(struct net_device *dev, struct e8390_pkt_hdr *hdr,
  128. int ring_page);
  129. static void ne_block_input(struct net_device *dev, int count,
  130. struct sk_buff *skb, int ring_offset);
  131. static void ne_block_output(struct net_device *dev, const int count,
  132. const unsigned char *buf, const int start_page);
  133. /* Probe for various non-shared-memory ethercards.
  134. NEx000-clone boards have a Station Address PROM (SAPROM) in the packet
  135. buffer memory space. NE2000 clones have 0x57,0x57 in bytes 0x0e,0x0f of
  136. the SAPROM, while other supposed NE2000 clones must be detected by their
  137. SA prefix.
  138. Reading the SAPROM from a word-wide card with the 8390 set in byte-wide
  139. mode results in doubled values, which can be detected and compensated for.
  140. The probe is also responsible for initializing the card and filling
  141. in the 'dev' and 'ei_status' structures.
  142. We use the minimum memory size for some ethercard product lines, iff we can't
  143. distinguish models. You can increase the packet buffer size by setting
  144. PACKETBUF_MEMSIZE. Reported Cabletron packet buffer locations are:
  145. E1010 starts at 0x100 and ends at 0x2000.
  146. E1010-x starts at 0x100 and ends at 0x8000. ("-x" means "more memory")
  147. E2010 starts at 0x100 and ends at 0x4000.
  148. E2010-x starts at 0x100 and ends at 0xffff. */
  149. static int __init do_ne_probe(struct net_device *dev)
  150. {
  151. unsigned long base_addr = dev->base_addr;
  152. #ifdef NEEDS_PORTLIST
  153. int orig_irq = dev->irq;
  154. #endif
  155. SET_MODULE_OWNER(dev);
  156. /* First check any supplied i/o locations. User knows best. <cough> */
  157. if (base_addr > 0x1ff) /* Check a single specified location. */
  158. return ne_probe1(dev, base_addr);
  159. else if (base_addr != 0) /* Don't probe at all. */
  160. return -ENXIO;
  161. /* Then look for any installed ISAPnP clones */
  162. if (isapnp_present() && (ne_probe_isapnp(dev) == 0))
  163. return 0;
  164. #ifdef NEEDS_PORTLIST
  165. /* Last resort. The semi-risky ISA auto-probe. */
  166. for (base_addr = 0; netcard_portlist[base_addr] != 0; base_addr++) {
  167. int ioaddr = netcard_portlist[base_addr];
  168. dev->irq = orig_irq;
  169. if (ne_probe1(dev, ioaddr) == 0)
  170. return 0;
  171. }
  172. #endif
  173. return -ENODEV;
  174. }
  175. #ifndef MODULE
  176. struct net_device * __init ne_probe(int unit)
  177. {
  178. struct net_device *dev = alloc_ei_netdev();
  179. int err;
  180. if (!dev)
  181. return ERR_PTR(-ENOMEM);
  182. sprintf(dev->name, "eth%d", unit);
  183. netdev_boot_setup_check(dev);
  184. err = do_ne_probe(dev);
  185. if (err)
  186. goto out;
  187. return dev;
  188. out:
  189. free_netdev(dev);
  190. return ERR_PTR(err);
  191. }
  192. #endif
  193. static int __init ne_probe_isapnp(struct net_device *dev)
  194. {
  195. int i;
  196. for (i = 0; isapnp_clone_list[i].vendor != 0; i++) {
  197. struct pnp_dev *idev = NULL;
  198. while ((idev = pnp_find_dev(NULL,
  199. isapnp_clone_list[i].vendor,
  200. isapnp_clone_list[i].function,
  201. idev))) {
  202. /* Avoid already found cards from previous calls */
  203. if (pnp_device_attach(idev) < 0)
  204. continue;
  205. if (pnp_activate_dev(idev) < 0) {
  206. pnp_device_detach(idev);
  207. continue;
  208. }
  209. /* if no io and irq, search for next */
  210. if (!pnp_port_valid(idev, 0) || !pnp_irq_valid(idev, 0)) {
  211. pnp_device_detach(idev);
  212. continue;
  213. }
  214. /* found it */
  215. dev->base_addr = pnp_port_start(idev, 0);
  216. dev->irq = pnp_irq(idev, 0);
  217. printk(KERN_INFO "ne.c: ISAPnP reports %s at i/o %#lx, irq %d.\n",
  218. (char *) isapnp_clone_list[i].driver_data,
  219. dev->base_addr, dev->irq);
  220. if (ne_probe1(dev, dev->base_addr) != 0) { /* Shouldn't happen. */
  221. printk(KERN_ERR "ne.c: Probe of ISAPnP card at %#lx failed.\n", dev->base_addr);
  222. pnp_device_detach(idev);
  223. return -ENXIO;
  224. }
  225. ei_status.priv = (unsigned long)idev;
  226. break;
  227. }
  228. if (!idev)
  229. continue;
  230. return 0;
  231. }
  232. return -ENODEV;
  233. }
  234. static int __init ne_probe1(struct net_device *dev, unsigned long ioaddr)
  235. {
  236. int i;
  237. unsigned char SA_prom[32];
  238. int wordlength = 2;
  239. const char *name = NULL;
  240. int start_page, stop_page;
  241. int neX000, ctron, copam, bad_card;
  242. int reg0, ret;
  243. static unsigned version_printed;
  244. if (!request_region(ioaddr, NE_IO_EXTENT, DRV_NAME))
  245. return -EBUSY;
  246. reg0 = inb_p(ioaddr);
  247. if (reg0 == 0xFF) {
  248. ret = -ENODEV;
  249. goto err_out;
  250. }
  251. /* Do a preliminary verification that we have a 8390. */
  252. {
  253. int regd;
  254. outb_p(E8390_NODMA+E8390_PAGE1+E8390_STOP, ioaddr + E8390_CMD);
  255. regd = inb_p(ioaddr + 0x0d);
  256. outb_p(0xff, ioaddr + 0x0d);
  257. outb_p(E8390_NODMA+E8390_PAGE0, ioaddr + E8390_CMD);
  258. inb_p(ioaddr + EN0_COUNTER0); /* Clear the counter by reading. */
  259. if (inb_p(ioaddr + EN0_COUNTER0) != 0) {
  260. outb_p(reg0, ioaddr);
  261. outb_p(regd, ioaddr + 0x0d); /* Restore the old values. */
  262. ret = -ENODEV;
  263. goto err_out;
  264. }
  265. }
  266. if (ei_debug && version_printed++ == 0)
  267. printk(KERN_INFO "%s" KERN_INFO "%s", version1, version2);
  268. printk(KERN_INFO "NE*000 ethercard probe at %#3lx:", ioaddr);
  269. /* A user with a poor card that fails to ack the reset, or that
  270. does not have a valid 0x57,0x57 signature can still use this
  271. without having to recompile. Specifying an i/o address along
  272. with an otherwise unused dev->mem_end value of "0xBAD" will
  273. cause the driver to skip these parts of the probe. */
  274. bad_card = ((dev->base_addr != 0) && (dev->mem_end == 0xbad));
  275. /* Reset card. Who knows what dain-bramaged state it was left in. */
  276. {
  277. unsigned long reset_start_time = jiffies;
  278. /* DON'T change these to inb_p/outb_p or reset will fail on clones. */
  279. outb(inb(ioaddr + NE_RESET), ioaddr + NE_RESET);
  280. while ((inb_p(ioaddr + EN0_ISR) & ENISR_RESET) == 0)
  281. if (time_after(jiffies, reset_start_time + 2*HZ/100)) {
  282. if (bad_card) {
  283. printk(" (warning: no reset ack)");
  284. break;
  285. } else {
  286. printk(" not found (no reset ack).\n");
  287. ret = -ENODEV;
  288. goto err_out;
  289. }
  290. }
  291. outb_p(0xff, ioaddr + EN0_ISR); /* Ack all intr. */
  292. }
  293. /* Read the 16 bytes of station address PROM.
  294. We must first initialize registers, similar to NS8390_init(eifdev, 0).
  295. We can't reliably read the SAPROM address without this.
  296. (I learned the hard way!). */
  297. {
  298. struct {unsigned char value, offset; } program_seq[] =
  299. {
  300. {E8390_NODMA+E8390_PAGE0+E8390_STOP, E8390_CMD}, /* Select page 0*/
  301. {0x48, EN0_DCFG}, /* Set byte-wide (0x48) access. */
  302. {0x00, EN0_RCNTLO}, /* Clear the count regs. */
  303. {0x00, EN0_RCNTHI},
  304. {0x00, EN0_IMR}, /* Mask completion irq. */
  305. {0xFF, EN0_ISR},
  306. {E8390_RXOFF, EN0_RXCR}, /* 0x20 Set to monitor */
  307. {E8390_TXOFF, EN0_TXCR}, /* 0x02 and loopback mode. */
  308. {32, EN0_RCNTLO},
  309. {0x00, EN0_RCNTHI},
  310. {0x00, EN0_RSARLO}, /* DMA starting at 0x0000. */
  311. {0x00, EN0_RSARHI},
  312. {E8390_RREAD+E8390_START, E8390_CMD},
  313. };
  314. for (i = 0; i < sizeof(program_seq)/sizeof(program_seq[0]); i++)
  315. outb_p(program_seq[i].value, ioaddr + program_seq[i].offset);
  316. }
  317. for(i = 0; i < 32 /*sizeof(SA_prom)*/; i+=2) {
  318. SA_prom[i] = inb(ioaddr + NE_DATAPORT);
  319. SA_prom[i+1] = inb(ioaddr + NE_DATAPORT);
  320. if (SA_prom[i] != SA_prom[i+1])
  321. wordlength = 1;
  322. }
  323. if (wordlength == 2)
  324. {
  325. for (i = 0; i < 16; i++)
  326. SA_prom[i] = SA_prom[i+i];
  327. /* We must set the 8390 for word mode. */
  328. outb_p(DCR_VAL, ioaddr + EN0_DCFG);
  329. start_page = NESM_START_PG;
  330. /*
  331. * Realtek RTL8019AS datasheet says that the PSTOP register
  332. * shouldn't exceed 0x60 in 8-bit mode.
  333. * This chip can be identified by reading the signature from
  334. * the remote byte count registers (otherwise write-only)...
  335. */
  336. if ((DCR_VAL & 0x01) == 0 && /* 8-bit mode */
  337. inb(ioaddr + EN0_RCNTLO) == 0x50 &&
  338. inb(ioaddr + EN0_RCNTHI) == 0x70)
  339. stop_page = 0x60;
  340. else
  341. stop_page = NESM_STOP_PG;
  342. } else {
  343. start_page = NE1SM_START_PG;
  344. stop_page = NE1SM_STOP_PG;
  345. }
  346. #if defined(CONFIG_PLAT_MAPPI) || defined(CONFIG_PLAT_OAKS32R)
  347. neX000 = ((SA_prom[14] == 0x57 && SA_prom[15] == 0x57)
  348. || (SA_prom[14] == 0x42 && SA_prom[15] == 0x42));
  349. #else
  350. neX000 = (SA_prom[14] == 0x57 && SA_prom[15] == 0x57);
  351. #endif
  352. ctron = (SA_prom[0] == 0x00 && SA_prom[1] == 0x00 && SA_prom[2] == 0x1d);
  353. copam = (SA_prom[14] == 0x49 && SA_prom[15] == 0x00);
  354. /* Set up the rest of the parameters. */
  355. if (neX000 || bad_card || copam) {
  356. name = (wordlength == 2) ? "NE2000" : "NE1000";
  357. }
  358. else if (ctron)
  359. {
  360. name = (wordlength == 2) ? "Ctron-8" : "Ctron-16";
  361. start_page = 0x01;
  362. stop_page = (wordlength == 2) ? 0x40 : 0x20;
  363. }
  364. else
  365. {
  366. #ifdef SUPPORT_NE_BAD_CLONES
  367. /* Ack! Well, there might be a *bad* NE*000 clone there.
  368. Check for total bogus addresses. */
  369. for (i = 0; bad_clone_list[i].name8; i++)
  370. {
  371. if (SA_prom[0] == bad_clone_list[i].SAprefix[0] &&
  372. SA_prom[1] == bad_clone_list[i].SAprefix[1] &&
  373. SA_prom[2] == bad_clone_list[i].SAprefix[2])
  374. {
  375. if (wordlength == 2)
  376. {
  377. name = bad_clone_list[i].name16;
  378. } else {
  379. name = bad_clone_list[i].name8;
  380. }
  381. break;
  382. }
  383. }
  384. if (bad_clone_list[i].name8 == NULL)
  385. {
  386. printk(" not found (invalid signature %2.2x %2.2x).\n",
  387. SA_prom[14], SA_prom[15]);
  388. ret = -ENXIO;
  389. goto err_out;
  390. }
  391. #else
  392. printk(" not found.\n");
  393. ret = -ENXIO;
  394. goto err_out;
  395. #endif
  396. }
  397. if (dev->irq < 2)
  398. {
  399. unsigned long cookie = probe_irq_on();
  400. outb_p(0x50, ioaddr + EN0_IMR); /* Enable one interrupt. */
  401. outb_p(0x00, ioaddr + EN0_RCNTLO);
  402. outb_p(0x00, ioaddr + EN0_RCNTHI);
  403. outb_p(E8390_RREAD+E8390_START, ioaddr); /* Trigger it... */
  404. mdelay(10); /* wait 10ms for interrupt to propagate */
  405. outb_p(0x00, ioaddr + EN0_IMR); /* Mask it again. */
  406. dev->irq = probe_irq_off(cookie);
  407. if (ei_debug > 2)
  408. printk(" autoirq is %d\n", dev->irq);
  409. } else if (dev->irq == 2)
  410. /* Fixup for users that don't know that IRQ 2 is really IRQ 9,
  411. or don't know which one to set. */
  412. dev->irq = 9;
  413. if (! dev->irq) {
  414. printk(" failed to detect IRQ line.\n");
  415. ret = -EAGAIN;
  416. goto err_out;
  417. }
  418. /* Snarf the interrupt now. There's no point in waiting since we cannot
  419. share and the board will usually be enabled. */
  420. ret = request_irq(dev->irq, ei_interrupt, 0, name, dev);
  421. if (ret) {
  422. printk (" unable to get IRQ %d (errno=%d).\n", dev->irq, ret);
  423. goto err_out;
  424. }
  425. dev->base_addr = ioaddr;
  426. #ifdef CONFIG_PLAT_MAPPI
  427. outb_p(E8390_NODMA + E8390_PAGE1 + E8390_STOP,
  428. ioaddr + E8390_CMD); /* 0x61 */
  429. for (i = 0 ; i < ETHER_ADDR_LEN ; i++) {
  430. dev->dev_addr[i] = SA_prom[i]
  431. = inb_p(ioaddr + EN1_PHYS_SHIFT(i));
  432. printk(" %2.2x", SA_prom[i]);
  433. }
  434. #else
  435. for(i = 0; i < ETHER_ADDR_LEN; i++) {
  436. printk(" %2.2x", SA_prom[i]);
  437. dev->dev_addr[i] = SA_prom[i];
  438. }
  439. #endif
  440. printk("\n");
  441. ei_status.name = name;
  442. ei_status.tx_start_page = start_page;
  443. ei_status.stop_page = stop_page;
  444. /* Use 16-bit mode only if this wasn't overridden by DCR_VAL */
  445. ei_status.word16 = (wordlength == 2 && (DCR_VAL & 0x01));
  446. ei_status.rx_start_page = start_page + TX_PAGES;
  447. #ifdef PACKETBUF_MEMSIZE
  448. /* Allow the packet buffer size to be overridden by know-it-alls. */
  449. ei_status.stop_page = ei_status.tx_start_page + PACKETBUF_MEMSIZE;
  450. #endif
  451. ei_status.reset_8390 = &ne_reset_8390;
  452. ei_status.block_input = &ne_block_input;
  453. ei_status.block_output = &ne_block_output;
  454. ei_status.get_8390_hdr = &ne_get_8390_hdr;
  455. ei_status.priv = 0;
  456. dev->open = &ne_open;
  457. dev->stop = &ne_close;
  458. #ifdef CONFIG_NET_POLL_CONTROLLER
  459. dev->poll_controller = ei_poll;
  460. #endif
  461. NS8390_init(dev, 0);
  462. ret = register_netdev(dev);
  463. if (ret)
  464. goto out_irq;
  465. printk(KERN_INFO "%s: %s found at %#lx, using IRQ %d.\n",
  466. dev->name, name, ioaddr, dev->irq);
  467. return 0;
  468. out_irq:
  469. free_irq(dev->irq, dev);
  470. err_out:
  471. release_region(ioaddr, NE_IO_EXTENT);
  472. return ret;
  473. }
  474. static int ne_open(struct net_device *dev)
  475. {
  476. ei_open(dev);
  477. return 0;
  478. }
  479. static int ne_close(struct net_device *dev)
  480. {
  481. if (ei_debug > 1)
  482. printk(KERN_DEBUG "%s: Shutting down ethercard.\n", dev->name);
  483. ei_close(dev);
  484. return 0;
  485. }
  486. /* Hard reset the card. This used to pause for the same period that a
  487. 8390 reset command required, but that shouldn't be necessary. */
  488. static void ne_reset_8390(struct net_device *dev)
  489. {
  490. unsigned long reset_start_time = jiffies;
  491. if (ei_debug > 1)
  492. printk(KERN_DEBUG "resetting the 8390 t=%ld...", jiffies);
  493. /* DON'T change these to inb_p/outb_p or reset will fail on clones. */
  494. outb(inb(NE_BASE + NE_RESET), NE_BASE + NE_RESET);
  495. ei_status.txing = 0;
  496. ei_status.dmaing = 0;
  497. /* This check _should_not_ be necessary, omit eventually. */
  498. while ((inb_p(NE_BASE+EN0_ISR) & ENISR_RESET) == 0)
  499. if (time_after(jiffies, reset_start_time + 2*HZ/100)) {
  500. printk(KERN_WARNING "%s: ne_reset_8390() did not complete.\n", dev->name);
  501. break;
  502. }
  503. outb_p(ENISR_RESET, NE_BASE + EN0_ISR); /* Ack intr. */
  504. }
  505. /* Grab the 8390 specific header. Similar to the block_input routine, but
  506. we don't need to be concerned with ring wrap as the header will be at
  507. the start of a page, so we optimize accordingly. */
  508. static void ne_get_8390_hdr(struct net_device *dev, struct e8390_pkt_hdr *hdr, int ring_page)
  509. {
  510. int nic_base = dev->base_addr;
  511. /* This *shouldn't* happen. If it does, it's the last thing you'll see */
  512. if (ei_status.dmaing)
  513. {
  514. printk(KERN_EMERG "%s: DMAing conflict in ne_get_8390_hdr "
  515. "[DMAstat:%d][irqlock:%d].\n",
  516. dev->name, ei_status.dmaing, ei_status.irqlock);
  517. return;
  518. }
  519. ei_status.dmaing |= 0x01;
  520. outb_p(E8390_NODMA+E8390_PAGE0+E8390_START, nic_base+ NE_CMD);
  521. outb_p(sizeof(struct e8390_pkt_hdr), nic_base + EN0_RCNTLO);
  522. outb_p(0, nic_base + EN0_RCNTHI);
  523. outb_p(0, nic_base + EN0_RSARLO); /* On page boundary */
  524. outb_p(ring_page, nic_base + EN0_RSARHI);
  525. outb_p(E8390_RREAD+E8390_START, nic_base + NE_CMD);
  526. if (ei_status.word16)
  527. insw(NE_BASE + NE_DATAPORT, hdr, sizeof(struct e8390_pkt_hdr)>>1);
  528. else
  529. insb(NE_BASE + NE_DATAPORT, hdr, sizeof(struct e8390_pkt_hdr));
  530. outb_p(ENISR_RDC, nic_base + EN0_ISR); /* Ack intr. */
  531. ei_status.dmaing &= ~0x01;
  532. le16_to_cpus(&hdr->count);
  533. }
  534. /* Block input and output, similar to the Crynwr packet driver. If you
  535. are porting to a new ethercard, look at the packet driver source for hints.
  536. The NEx000 doesn't share the on-board packet memory -- you have to put
  537. the packet out through the "remote DMA" dataport using outb. */
  538. static void ne_block_input(struct net_device *dev, int count, struct sk_buff *skb, int ring_offset)
  539. {
  540. #ifdef NE_SANITY_CHECK
  541. int xfer_count = count;
  542. #endif
  543. int nic_base = dev->base_addr;
  544. char *buf = skb->data;
  545. /* This *shouldn't* happen. If it does, it's the last thing you'll see */
  546. if (ei_status.dmaing)
  547. {
  548. printk(KERN_EMERG "%s: DMAing conflict in ne_block_input "
  549. "[DMAstat:%d][irqlock:%d].\n",
  550. dev->name, ei_status.dmaing, ei_status.irqlock);
  551. return;
  552. }
  553. ei_status.dmaing |= 0x01;
  554. outb_p(E8390_NODMA+E8390_PAGE0+E8390_START, nic_base+ NE_CMD);
  555. outb_p(count & 0xff, nic_base + EN0_RCNTLO);
  556. outb_p(count >> 8, nic_base + EN0_RCNTHI);
  557. outb_p(ring_offset & 0xff, nic_base + EN0_RSARLO);
  558. outb_p(ring_offset >> 8, nic_base + EN0_RSARHI);
  559. outb_p(E8390_RREAD+E8390_START, nic_base + NE_CMD);
  560. if (ei_status.word16)
  561. {
  562. insw(NE_BASE + NE_DATAPORT,buf,count>>1);
  563. if (count & 0x01)
  564. {
  565. buf[count-1] = inb(NE_BASE + NE_DATAPORT);
  566. #ifdef NE_SANITY_CHECK
  567. xfer_count++;
  568. #endif
  569. }
  570. } else {
  571. insb(NE_BASE + NE_DATAPORT, buf, count);
  572. }
  573. #ifdef NE_SANITY_CHECK
  574. /* This was for the ALPHA version only, but enough people have
  575. been encountering problems so it is still here. If you see
  576. this message you either 1) have a slightly incompatible clone
  577. or 2) have noise/speed problems with your bus. */
  578. if (ei_debug > 1)
  579. {
  580. /* DMA termination address check... */
  581. int addr, tries = 20;
  582. do {
  583. /* DON'T check for 'inb_p(EN0_ISR) & ENISR_RDC' here
  584. -- it's broken for Rx on some cards! */
  585. int high = inb_p(nic_base + EN0_RSARHI);
  586. int low = inb_p(nic_base + EN0_RSARLO);
  587. addr = (high << 8) + low;
  588. if (((ring_offset + xfer_count) & 0xff) == low)
  589. break;
  590. } while (--tries > 0);
  591. if (tries <= 0)
  592. printk(KERN_WARNING "%s: RX transfer address mismatch,"
  593. "%#4.4x (expected) vs. %#4.4x (actual).\n",
  594. dev->name, ring_offset + xfer_count, addr);
  595. }
  596. #endif
  597. outb_p(ENISR_RDC, nic_base + EN0_ISR); /* Ack intr. */
  598. ei_status.dmaing &= ~0x01;
  599. }
  600. static void ne_block_output(struct net_device *dev, int count,
  601. const unsigned char *buf, const int start_page)
  602. {
  603. int nic_base = NE_BASE;
  604. unsigned long dma_start;
  605. #ifdef NE_SANITY_CHECK
  606. int retries = 0;
  607. #endif
  608. /* Round the count up for word writes. Do we need to do this?
  609. What effect will an odd byte count have on the 8390?
  610. I should check someday. */
  611. if (ei_status.word16 && (count & 0x01))
  612. count++;
  613. /* This *shouldn't* happen. If it does, it's the last thing you'll see */
  614. if (ei_status.dmaing)
  615. {
  616. printk(KERN_EMERG "%s: DMAing conflict in ne_block_output."
  617. "[DMAstat:%d][irqlock:%d]\n",
  618. dev->name, ei_status.dmaing, ei_status.irqlock);
  619. return;
  620. }
  621. ei_status.dmaing |= 0x01;
  622. /* We should already be in page 0, but to be safe... */
  623. outb_p(E8390_PAGE0+E8390_START+E8390_NODMA, nic_base + NE_CMD);
  624. #ifdef NE_SANITY_CHECK
  625. retry:
  626. #endif
  627. #ifdef NE8390_RW_BUGFIX
  628. /* Handle the read-before-write bug the same way as the
  629. Crynwr packet driver -- the NatSemi method doesn't work.
  630. Actually this doesn't always work either, but if you have
  631. problems with your NEx000 this is better than nothing! */
  632. outb_p(0x42, nic_base + EN0_RCNTLO);
  633. outb_p(0x00, nic_base + EN0_RCNTHI);
  634. outb_p(0x42, nic_base + EN0_RSARLO);
  635. outb_p(0x00, nic_base + EN0_RSARHI);
  636. outb_p(E8390_RREAD+E8390_START, nic_base + NE_CMD);
  637. /* Make certain that the dummy read has occurred. */
  638. udelay(6);
  639. #endif
  640. outb_p(ENISR_RDC, nic_base + EN0_ISR);
  641. /* Now the normal output. */
  642. outb_p(count & 0xff, nic_base + EN0_RCNTLO);
  643. outb_p(count >> 8, nic_base + EN0_RCNTHI);
  644. outb_p(0x00, nic_base + EN0_RSARLO);
  645. outb_p(start_page, nic_base + EN0_RSARHI);
  646. outb_p(E8390_RWRITE+E8390_START, nic_base + NE_CMD);
  647. if (ei_status.word16) {
  648. outsw(NE_BASE + NE_DATAPORT, buf, count>>1);
  649. } else {
  650. outsb(NE_BASE + NE_DATAPORT, buf, count);
  651. }
  652. dma_start = jiffies;
  653. #ifdef NE_SANITY_CHECK
  654. /* This was for the ALPHA version only, but enough people have
  655. been encountering problems so it is still here. */
  656. if (ei_debug > 1)
  657. {
  658. /* DMA termination address check... */
  659. int addr, tries = 20;
  660. do {
  661. int high = inb_p(nic_base + EN0_RSARHI);
  662. int low = inb_p(nic_base + EN0_RSARLO);
  663. addr = (high << 8) + low;
  664. if ((start_page << 8) + count == addr)
  665. break;
  666. } while (--tries > 0);
  667. if (tries <= 0)
  668. {
  669. printk(KERN_WARNING "%s: Tx packet transfer address mismatch,"
  670. "%#4.4x (expected) vs. %#4.4x (actual).\n",
  671. dev->name, (start_page << 8) + count, addr);
  672. if (retries++ == 0)
  673. goto retry;
  674. }
  675. }
  676. #endif
  677. while ((inb_p(nic_base + EN0_ISR) & ENISR_RDC) == 0)
  678. if (time_after(jiffies, dma_start + 2*HZ/100)) { /* 20ms */
  679. printk(KERN_WARNING "%s: timeout waiting for Tx RDC.\n", dev->name);
  680. ne_reset_8390(dev);
  681. NS8390_init(dev,1);
  682. break;
  683. }
  684. outb_p(ENISR_RDC, nic_base + EN0_ISR); /* Ack intr. */
  685. ei_status.dmaing &= ~0x01;
  686. return;
  687. }
  688. static int __init ne_drv_probe(struct platform_device *pdev)
  689. {
  690. struct net_device *dev;
  691. struct resource *res;
  692. int err, irq;
  693. res = platform_get_resource(pdev, IORESOURCE_IO, 0);
  694. irq = platform_get_irq(pdev, 0);
  695. if (!res || irq < 0)
  696. return -ENODEV;
  697. dev = alloc_ei_netdev();
  698. if (!dev)
  699. return -ENOMEM;
  700. dev->irq = irq;
  701. dev->base_addr = res->start;
  702. err = do_ne_probe(dev);
  703. if (err) {
  704. free_netdev(dev);
  705. return err;
  706. }
  707. platform_set_drvdata(pdev, dev);
  708. return 0;
  709. }
  710. static int __exit ne_drv_remove(struct platform_device *pdev)
  711. {
  712. struct net_device *dev = platform_get_drvdata(pdev);
  713. unregister_netdev(dev);
  714. free_irq(dev->irq, dev);
  715. release_region(dev->base_addr, NE_IO_EXTENT);
  716. free_netdev(dev);
  717. return 0;
  718. }
  719. #ifdef CONFIG_PM
  720. static int ne_drv_suspend(struct platform_device *pdev, pm_message_t state)
  721. {
  722. struct net_device *dev = platform_get_drvdata(pdev);
  723. if (netif_running(dev))
  724. netif_device_detach(dev);
  725. return 0;
  726. }
  727. static int ne_drv_resume(struct platform_device *pdev)
  728. {
  729. struct net_device *dev = platform_get_drvdata(pdev);
  730. if (netif_running(dev)) {
  731. ne_reset_8390(dev);
  732. NS8390_init(dev, 1);
  733. netif_device_attach(dev);
  734. }
  735. return 0;
  736. }
  737. #else
  738. #define ne_drv_suspend NULL
  739. #define ne_drv_resume NULL
  740. #endif
  741. static struct platform_driver ne_driver = {
  742. .remove = __exit_p(ne_drv_remove),
  743. .suspend = ne_drv_suspend,
  744. .resume = ne_drv_resume,
  745. .driver = {
  746. .name = DRV_NAME,
  747. .owner = THIS_MODULE,
  748. },
  749. };
  750. static int __init ne_init(void)
  751. {
  752. return platform_driver_probe(&ne_driver, ne_drv_probe);
  753. }
  754. static void __exit ne_exit(void)
  755. {
  756. platform_driver_unregister(&ne_driver);
  757. }
  758. #ifdef MODULE
  759. #define MAX_NE_CARDS 4 /* Max number of NE cards per module */
  760. static struct net_device *dev_ne[MAX_NE_CARDS];
  761. static int io[MAX_NE_CARDS];
  762. static int irq[MAX_NE_CARDS];
  763. static int bad[MAX_NE_CARDS]; /* 0xbad = bad sig or no reset ack */
  764. module_param_array(io, int, NULL, 0);
  765. module_param_array(irq, int, NULL, 0);
  766. module_param_array(bad, int, NULL, 0);
  767. MODULE_PARM_DESC(io, "I/O base address(es),required");
  768. MODULE_PARM_DESC(irq, "IRQ number(s)");
  769. MODULE_PARM_DESC(bad, "Accept card(s) with bad signatures");
  770. MODULE_DESCRIPTION("NE1000/NE2000 ISA/PnP Ethernet driver");
  771. MODULE_LICENSE("GPL");
  772. /* This is set up so that no ISA autoprobe takes place. We can't guarantee
  773. that the ne2k probe is the last 8390 based probe to take place (as it
  774. is at boot) and so the probe will get confused by any other 8390 cards.
  775. ISA device autoprobes on a running machine are not recommended anyway. */
  776. int __init init_module(void)
  777. {
  778. int this_dev, found = 0;
  779. int plat_found = !ne_init();
  780. for (this_dev = 0; this_dev < MAX_NE_CARDS; this_dev++) {
  781. struct net_device *dev = alloc_ei_netdev();
  782. if (!dev)
  783. break;
  784. dev->irq = irq[this_dev];
  785. dev->mem_end = bad[this_dev];
  786. dev->base_addr = io[this_dev];
  787. if (do_ne_probe(dev) == 0) {
  788. dev_ne[found++] = dev;
  789. continue;
  790. }
  791. free_netdev(dev);
  792. if (found || plat_found)
  793. break;
  794. if (io[this_dev] != 0)
  795. printk(KERN_WARNING "ne.c: No NE*000 card found at i/o = %#x\n", io[this_dev]);
  796. else
  797. printk(KERN_NOTICE "ne.c: You must supply \"io=0xNNN\" value(s) for ISA cards.\n");
  798. return -ENXIO;
  799. }
  800. if (found || plat_found)
  801. return 0;
  802. return -ENODEV;
  803. }
  804. static void cleanup_card(struct net_device *dev)
  805. {
  806. struct pnp_dev *idev = (struct pnp_dev *)ei_status.priv;
  807. if (idev)
  808. pnp_device_detach(idev);
  809. free_irq(dev->irq, dev);
  810. release_region(dev->base_addr, NE_IO_EXTENT);
  811. }
  812. void __exit cleanup_module(void)
  813. {
  814. int this_dev;
  815. ne_exit();
  816. for (this_dev = 0; this_dev < MAX_NE_CARDS; this_dev++) {
  817. struct net_device *dev = dev_ne[this_dev];
  818. if (dev) {
  819. unregister_netdev(dev);
  820. cleanup_card(dev);
  821. free_netdev(dev);
  822. }
  823. }
  824. }
  825. #else /* MODULE */
  826. module_init(ne_init);
  827. module_exit(ne_exit);
  828. #endif /* MODULE */