ne.c 29 KB

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