ne.c 29 KB

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