hp-plus.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  1. /* hp-plus.c: A HP PCLAN/plus ethernet driver for linux. */
  2. /*
  3. Written 1994 by Donald Becker.
  4. This driver is for the Hewlett Packard PC LAN (27***) plus ethercards.
  5. These cards are sold under several model numbers, usually 2724*.
  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
  10. 410 Severn Ave., Suite 210
  11. Annapolis MD 21403
  12. As is often the case, a great deal of credit is owed to Russ Nelson.
  13. The Crynwr packet driver was my primary source of HP-specific
  14. programming information.
  15. */
  16. static const char version[] =
  17. "hp-plus.c:v1.10 9/24/94 Donald Becker (becker@cesdis.gsfc.nasa.gov)\n";
  18. #include <linux/module.h>
  19. #include <linux/string.h> /* Important -- this inlines word moves. */
  20. #include <linux/kernel.h>
  21. #include <linux/errno.h>
  22. #include <linux/ioport.h>
  23. #include <linux/netdevice.h>
  24. #include <linux/etherdevice.h>
  25. #include <linux/init.h>
  26. #include <linux/delay.h>
  27. #include <asm/system.h>
  28. #include <asm/io.h>
  29. #include "8390.h"
  30. #define DRV_NAME "hp-plus"
  31. /* A zero-terminated list of I/O addresses to be probed. */
  32. static unsigned int hpplus_portlist[] __initdata =
  33. {0x200, 0x240, 0x280, 0x2C0, 0x300, 0x320, 0x340, 0};
  34. /*
  35. The HP EtherTwist chip implementation is a fairly routine DP8390
  36. implementation. It allows both shared memory and programmed-I/O buffer
  37. access, using a custom interface for both. The programmed-I/O mode is
  38. entirely implemented in the HP EtherTwist chip, bypassing the problem
  39. ridden built-in 8390 facilities used on NE2000 designs. The shared
  40. memory mode is likewise special, with an offset register used to make
  41. packets appear at the shared memory base. Both modes use a base and bounds
  42. page register to hide the Rx ring buffer wrap -- a packet that spans the
  43. end of physical buffer memory appears continuous to the driver. (c.f. the
  44. 3c503 and Cabletron E2100)
  45. A special note: the internal buffer of the board is only 8 bits wide.
  46. This lays several nasty traps for the unaware:
  47. - the 8390 must be programmed for byte-wide operations
  48. - all I/O and memory operations must work on whole words (the access
  49. latches are serially preloaded and have no byte-swapping ability).
  50. This board is laid out in I/O space much like the earlier HP boards:
  51. the first 16 locations are for the board registers, and the second 16 are
  52. for the 8390. The board is easy to identify, with both a dedicated 16 bit
  53. ID register and a constant 0x530* value in the upper bits of the paging
  54. register.
  55. */
  56. #define HP_ID 0x00 /* ID register, always 0x4850. */
  57. #define HP_PAGING 0x02 /* Registers visible @ 8-f, see PageName. */
  58. #define HPP_OPTION 0x04 /* Bitmapped options, see HP_Option. */
  59. #define HPP_OUT_ADDR 0x08 /* I/O output location in Perf_Page. */
  60. #define HPP_IN_ADDR 0x0A /* I/O input location in Perf_Page. */
  61. #define HP_DATAPORT 0x0c /* I/O data transfer in Perf_Page. */
  62. #define NIC_OFFSET 0x10 /* Offset to the 8390 registers. */
  63. #define HP_IO_EXTENT 32
  64. #define HP_START_PG 0x00 /* First page of TX buffer */
  65. #define HP_STOP_PG 0x80 /* Last page +1 of RX ring */
  66. /* The register set selected in HP_PAGING. */
  67. enum PageName {
  68. Perf_Page = 0, /* Normal operation. */
  69. MAC_Page = 1, /* The ethernet address (+checksum). */
  70. HW_Page = 2, /* EEPROM-loaded hardware parameters. */
  71. LAN_Page = 4, /* Transceiver selection, testing, etc. */
  72. ID_Page = 6 };
  73. /* The bit definitions for the HPP_OPTION register. */
  74. enum HP_Option {
  75. NICReset = 1, ChipReset = 2, /* Active low, really UNreset. */
  76. EnableIRQ = 4, FakeIntr = 8, BootROMEnb = 0x10, IOEnb = 0x20,
  77. MemEnable = 0x40, ZeroWait = 0x80, MemDisable = 0x1000, };
  78. static int hpp_probe1(struct net_device *dev, int ioaddr);
  79. static void hpp_reset_8390(struct net_device *dev);
  80. static int hpp_open(struct net_device *dev);
  81. static int hpp_close(struct net_device *dev);
  82. static void hpp_mem_block_input(struct net_device *dev, int count,
  83. struct sk_buff *skb, int ring_offset);
  84. static void hpp_mem_block_output(struct net_device *dev, int count,
  85. const unsigned char *buf, int start_page);
  86. static void hpp_mem_get_8390_hdr(struct net_device *dev, struct e8390_pkt_hdr *hdr,
  87. int ring_page);
  88. static void hpp_io_block_input(struct net_device *dev, int count,
  89. struct sk_buff *skb, int ring_offset);
  90. static void hpp_io_block_output(struct net_device *dev, int count,
  91. const unsigned char *buf, int start_page);
  92. static void hpp_io_get_8390_hdr(struct net_device *dev, struct e8390_pkt_hdr *hdr,
  93. int ring_page);
  94. /* Probe a list of addresses for an HP LAN+ adaptor.
  95. This routine is almost boilerplate. */
  96. static int __init do_hpp_probe(struct net_device *dev)
  97. {
  98. int i;
  99. int base_addr = dev->base_addr;
  100. int irq = dev->irq;
  101. SET_MODULE_OWNER(dev);
  102. if (base_addr > 0x1ff) /* Check a single specified location. */
  103. return hpp_probe1(dev, base_addr);
  104. else if (base_addr != 0) /* Don't probe at all. */
  105. return -ENXIO;
  106. for (i = 0; hpplus_portlist[i]; i++) {
  107. if (hpp_probe1(dev, hpplus_portlist[i]) == 0)
  108. return 0;
  109. dev->irq = irq;
  110. }
  111. return -ENODEV;
  112. }
  113. #ifndef MODULE
  114. struct net_device * __init hp_plus_probe(int unit)
  115. {
  116. struct net_device *dev = alloc_ei_netdev();
  117. int err;
  118. if (!dev)
  119. return ERR_PTR(-ENOMEM);
  120. sprintf(dev->name, "eth%d", unit);
  121. netdev_boot_setup_check(dev);
  122. err = do_hpp_probe(dev);
  123. if (err)
  124. goto out;
  125. return dev;
  126. out:
  127. free_netdev(dev);
  128. return ERR_PTR(err);
  129. }
  130. #endif
  131. /* Do the interesting part of the probe at a single address. */
  132. static int __init hpp_probe1(struct net_device *dev, int ioaddr)
  133. {
  134. int i, retval;
  135. unsigned char checksum = 0;
  136. const char name[] = "HP-PC-LAN+";
  137. int mem_start;
  138. static unsigned version_printed;
  139. if (!request_region(ioaddr, HP_IO_EXTENT, DRV_NAME))
  140. return -EBUSY;
  141. /* Check for the HP+ signature, 50 48 0x 53. */
  142. if (inw(ioaddr + HP_ID) != 0x4850
  143. || (inw(ioaddr + HP_PAGING) & 0xfff0) != 0x5300) {
  144. retval = -ENODEV;
  145. goto out;
  146. }
  147. if (ei_debug && version_printed++ == 0)
  148. printk(version);
  149. printk("%s: %s at %#3x,", dev->name, name, ioaddr);
  150. /* Retrieve and checksum the station address. */
  151. outw(MAC_Page, ioaddr + HP_PAGING);
  152. for(i = 0; i < ETHER_ADDR_LEN; i++) {
  153. unsigned char inval = inb(ioaddr + 8 + i);
  154. dev->dev_addr[i] = inval;
  155. checksum += inval;
  156. printk(" %2.2x", inval);
  157. }
  158. checksum += inb(ioaddr + 14);
  159. if (checksum != 0xff) {
  160. printk(" bad checksum %2.2x.\n", checksum);
  161. retval = -ENODEV;
  162. goto out;
  163. } else {
  164. /* Point at the Software Configuration Flags. */
  165. outw(ID_Page, ioaddr + HP_PAGING);
  166. printk(" ID %4.4x", inw(ioaddr + 12));
  167. }
  168. /* Read the IRQ line. */
  169. outw(HW_Page, ioaddr + HP_PAGING);
  170. {
  171. int irq = inb(ioaddr + 13) & 0x0f;
  172. int option = inw(ioaddr + HPP_OPTION);
  173. dev->irq = irq;
  174. if (option & MemEnable) {
  175. mem_start = inw(ioaddr + 9) << 8;
  176. printk(", IRQ %d, memory address %#x.\n", irq, mem_start);
  177. } else {
  178. mem_start = 0;
  179. printk(", IRQ %d, programmed-I/O mode.\n", irq);
  180. }
  181. }
  182. /* Set the wrap registers for string I/O reads. */
  183. outw((HP_START_PG + TX_PAGES/2) | ((HP_STOP_PG - 1) << 8), ioaddr + 14);
  184. /* Set the base address to point to the NIC, not the "real" base! */
  185. dev->base_addr = ioaddr + NIC_OFFSET;
  186. dev->open = &hpp_open;
  187. dev->stop = &hpp_close;
  188. #ifdef CONFIG_NET_POLL_CONTROLLER
  189. dev->poll_controller = ei_poll;
  190. #endif
  191. ei_status.name = name;
  192. ei_status.word16 = 0; /* Agggghhhhh! Debug time: 2 days! */
  193. ei_status.tx_start_page = HP_START_PG;
  194. ei_status.rx_start_page = HP_START_PG + TX_PAGES/2;
  195. ei_status.stop_page = HP_STOP_PG;
  196. ei_status.reset_8390 = &hpp_reset_8390;
  197. ei_status.block_input = &hpp_io_block_input;
  198. ei_status.block_output = &hpp_io_block_output;
  199. ei_status.get_8390_hdr = &hpp_io_get_8390_hdr;
  200. /* Check if the memory_enable flag is set in the option register. */
  201. if (mem_start) {
  202. ei_status.block_input = &hpp_mem_block_input;
  203. ei_status.block_output = &hpp_mem_block_output;
  204. ei_status.get_8390_hdr = &hpp_mem_get_8390_hdr;
  205. dev->mem_start = mem_start;
  206. ei_status.mem = ioremap(mem_start,
  207. (HP_STOP_PG - HP_START_PG)*256);
  208. if (!ei_status.mem) {
  209. retval = -ENOMEM;
  210. goto out;
  211. }
  212. ei_status.rmem_start = dev->mem_start + TX_PAGES/2*256;
  213. dev->mem_end = ei_status.rmem_end
  214. = dev->mem_start + (HP_STOP_PG - HP_START_PG)*256;
  215. }
  216. outw(Perf_Page, ioaddr + HP_PAGING);
  217. NS8390_init(dev, 0);
  218. /* Leave the 8390 and HP chip reset. */
  219. outw(inw(ioaddr + HPP_OPTION) & ~EnableIRQ, ioaddr + HPP_OPTION);
  220. retval = register_netdev(dev);
  221. if (retval)
  222. goto out1;
  223. return 0;
  224. out1:
  225. iounmap(ei_status.mem);
  226. out:
  227. release_region(ioaddr, HP_IO_EXTENT);
  228. return retval;
  229. }
  230. static int
  231. hpp_open(struct net_device *dev)
  232. {
  233. int ioaddr = dev->base_addr - NIC_OFFSET;
  234. int option_reg;
  235. int retval;
  236. if ((retval = request_irq(dev->irq, ei_interrupt, 0, dev->name, dev))) {
  237. return retval;
  238. }
  239. /* Reset the 8390 and HP chip. */
  240. option_reg = inw(ioaddr + HPP_OPTION);
  241. outw(option_reg & ~(NICReset + ChipReset), ioaddr + HPP_OPTION);
  242. udelay(5);
  243. /* Unreset the board and enable interrupts. */
  244. outw(option_reg | (EnableIRQ + NICReset + ChipReset), ioaddr + HPP_OPTION);
  245. /* Set the wrap registers for programmed-I/O operation. */
  246. outw(HW_Page, ioaddr + HP_PAGING);
  247. outw((HP_START_PG + TX_PAGES/2) | ((HP_STOP_PG - 1) << 8), ioaddr + 14);
  248. /* Select the operational page. */
  249. outw(Perf_Page, ioaddr + HP_PAGING);
  250. ei_open(dev);
  251. return 0;
  252. }
  253. static int
  254. hpp_close(struct net_device *dev)
  255. {
  256. int ioaddr = dev->base_addr - NIC_OFFSET;
  257. int option_reg = inw(ioaddr + HPP_OPTION);
  258. free_irq(dev->irq, dev);
  259. ei_close(dev);
  260. outw((option_reg & ~EnableIRQ) | MemDisable | NICReset | ChipReset,
  261. ioaddr + HPP_OPTION);
  262. return 0;
  263. }
  264. static void
  265. hpp_reset_8390(struct net_device *dev)
  266. {
  267. int ioaddr = dev->base_addr - NIC_OFFSET;
  268. int option_reg = inw(ioaddr + HPP_OPTION);
  269. if (ei_debug > 1) printk("resetting the 8390 time=%ld...", jiffies);
  270. outw(option_reg & ~(NICReset + ChipReset), ioaddr + HPP_OPTION);
  271. /* Pause a few cycles for the hardware reset to take place. */
  272. udelay(5);
  273. ei_status.txing = 0;
  274. outw(option_reg | (EnableIRQ + NICReset + ChipReset), ioaddr + HPP_OPTION);
  275. udelay(5);
  276. if ((inb_p(ioaddr+NIC_OFFSET+EN0_ISR) & ENISR_RESET) == 0)
  277. printk("%s: hp_reset_8390() did not complete.\n", dev->name);
  278. if (ei_debug > 1) printk("8390 reset done (%ld).", jiffies);
  279. return;
  280. }
  281. /* The programmed-I/O version of reading the 4 byte 8390 specific header.
  282. Note that transfer with the EtherTwist+ must be on word boundaries. */
  283. static void
  284. hpp_io_get_8390_hdr(struct net_device *dev, struct e8390_pkt_hdr *hdr, int ring_page)
  285. {
  286. int ioaddr = dev->base_addr - NIC_OFFSET;
  287. outw((ring_page<<8), ioaddr + HPP_IN_ADDR);
  288. insw(ioaddr + HP_DATAPORT, hdr, sizeof(struct e8390_pkt_hdr)>>1);
  289. }
  290. /* Block input and output, similar to the Crynwr packet driver. */
  291. static void
  292. hpp_io_block_input(struct net_device *dev, int count, struct sk_buff *skb, int ring_offset)
  293. {
  294. int ioaddr = dev->base_addr - NIC_OFFSET;
  295. char *buf = skb->data;
  296. outw(ring_offset, ioaddr + HPP_IN_ADDR);
  297. insw(ioaddr + HP_DATAPORT, buf, count>>1);
  298. if (count & 0x01)
  299. buf[count-1] = inw(ioaddr + HP_DATAPORT);
  300. }
  301. /* The corresponding shared memory versions of the above 2 functions. */
  302. static void
  303. hpp_mem_get_8390_hdr(struct net_device *dev, struct e8390_pkt_hdr *hdr, int ring_page)
  304. {
  305. int ioaddr = dev->base_addr - NIC_OFFSET;
  306. int option_reg = inw(ioaddr + HPP_OPTION);
  307. outw((ring_page<<8), ioaddr + HPP_IN_ADDR);
  308. outw(option_reg & ~(MemDisable + BootROMEnb), ioaddr + HPP_OPTION);
  309. memcpy_fromio(hdr, ei_status.mem, sizeof(struct e8390_pkt_hdr));
  310. outw(option_reg, ioaddr + HPP_OPTION);
  311. hdr->count = (le16_to_cpu(hdr->count) + 3) & ~3; /* Round up allocation. */
  312. }
  313. static void
  314. hpp_mem_block_input(struct net_device *dev, int count, struct sk_buff *skb, int ring_offset)
  315. {
  316. int ioaddr = dev->base_addr - NIC_OFFSET;
  317. int option_reg = inw(ioaddr + HPP_OPTION);
  318. outw(ring_offset, ioaddr + HPP_IN_ADDR);
  319. outw(option_reg & ~(MemDisable + BootROMEnb), ioaddr + HPP_OPTION);
  320. /* Caution: this relies on get_8390_hdr() rounding up count!
  321. Also note that we *can't* use eth_io_copy_and_sum() because
  322. it will not always copy "count" bytes (e.g. padded IP). */
  323. memcpy_fromio(skb->data, ei_status.mem, count);
  324. outw(option_reg, ioaddr + HPP_OPTION);
  325. }
  326. /* A special note: we *must* always transfer >=16 bit words.
  327. It's always safe to round up, so we do. */
  328. static void
  329. hpp_io_block_output(struct net_device *dev, int count,
  330. const unsigned char *buf, int start_page)
  331. {
  332. int ioaddr = dev->base_addr - NIC_OFFSET;
  333. outw(start_page << 8, ioaddr + HPP_OUT_ADDR);
  334. outsl(ioaddr + HP_DATAPORT, buf, (count+3)>>2);
  335. return;
  336. }
  337. static void
  338. hpp_mem_block_output(struct net_device *dev, int count,
  339. const unsigned char *buf, int start_page)
  340. {
  341. int ioaddr = dev->base_addr - NIC_OFFSET;
  342. int option_reg = inw(ioaddr + HPP_OPTION);
  343. outw(start_page << 8, ioaddr + HPP_OUT_ADDR);
  344. outw(option_reg & ~(MemDisable + BootROMEnb), ioaddr + HPP_OPTION);
  345. memcpy_toio(ei_status.mem, buf, (count + 3) & ~3);
  346. outw(option_reg, ioaddr + HPP_OPTION);
  347. return;
  348. }
  349. #ifdef MODULE
  350. #define MAX_HPP_CARDS 4 /* Max number of HPP cards per module */
  351. static struct net_device *dev_hpp[MAX_HPP_CARDS];
  352. static int io[MAX_HPP_CARDS];
  353. static int irq[MAX_HPP_CARDS];
  354. module_param_array(io, int, NULL, 0);
  355. module_param_array(irq, int, NULL, 0);
  356. MODULE_PARM_DESC(io, "I/O port address(es)");
  357. MODULE_PARM_DESC(irq, "IRQ number(s); ignored if properly detected");
  358. MODULE_DESCRIPTION("HP PC-LAN+ ISA ethernet driver");
  359. MODULE_LICENSE("GPL");
  360. /* This is set up so that only a single autoprobe takes place per call.
  361. ISA device autoprobes on a running machine are not recommended. */
  362. int __init
  363. init_module(void)
  364. {
  365. struct net_device *dev;
  366. int this_dev, found = 0;
  367. for (this_dev = 0; this_dev < MAX_HPP_CARDS; this_dev++) {
  368. if (io[this_dev] == 0) {
  369. if (this_dev != 0) break; /* only autoprobe 1st one */
  370. printk(KERN_NOTICE "hp-plus.c: Presently autoprobing (not recommended) for a single card.\n");
  371. }
  372. dev = alloc_ei_netdev();
  373. if (!dev)
  374. break;
  375. dev->irq = irq[this_dev];
  376. dev->base_addr = io[this_dev];
  377. if (do_hpp_probe(dev) == 0) {
  378. dev_hpp[found++] = dev;
  379. continue;
  380. }
  381. free_netdev(dev);
  382. printk(KERN_WARNING "hp-plus.c: No HP-Plus card found (i/o = 0x%x).\n", io[this_dev]);
  383. break;
  384. }
  385. if (found)
  386. return 0;
  387. return -ENXIO;
  388. }
  389. static void cleanup_card(struct net_device *dev)
  390. {
  391. /* NB: hpp_close() handles free_irq */
  392. iounmap(ei_status.mem);
  393. release_region(dev->base_addr - NIC_OFFSET, HP_IO_EXTENT);
  394. }
  395. void __exit
  396. cleanup_module(void)
  397. {
  398. int this_dev;
  399. for (this_dev = 0; this_dev < MAX_HPP_CARDS; this_dev++) {
  400. struct net_device *dev = dev_hpp[this_dev];
  401. if (dev) {
  402. unregister_netdev(dev);
  403. cleanup_card(dev);
  404. free_netdev(dev);
  405. }
  406. }
  407. }
  408. #endif /* MODULE */