etherh.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:16k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

  1. /*
  2.  *  linux/drivers/acorn/net/etherh.c
  3.  *
  4.  *  Copyright (C) 2000 Russell King
  5.  *
  6.  * This program is free software; you can redistribute it and/or modify
  7.  * it under the terms of the GNU General Public License version 2 as
  8.  * published by the Free Software Foundation.
  9.  *
  10.  * NS8390 I-cubed EtherH and ANT EtherM specific driver
  11.  * Thanks to I-Cubed for information on their cards.
  12.  * EtherM conversion (C) 1999 Chris Kemp and Tim Watterton
  13.  * EtherM integration (C) 2000 Aleph One Ltd (Tak-Shing Chan)
  14.  * EtherM integration re-engineered by Russell King.
  15.  *
  16.  * Changelog:
  17.  *  08-12-1996 RMK 1.00 Created
  18.  * RMK 1.03 Added support for EtherLan500 cards
  19.  *  23-11-1997 RMK 1.04 Added media autodetection
  20.  *  16-04-1998 RMK 1.05 Improved media autodetection
  21.  *  10-02-2000 RMK 1.06 Updated for 2.3.43
  22.  *  13-05-2000 RMK 1.07 Updated for 2.3.99-pre8
  23.  *  12-10-1999  CK/TEW EtherM driver first release
  24.  *  21-12-2000 TTC EtherH/EtherM integration
  25.  *  25-12-2000 RMK 1.08 Clean integration of EtherM into this driver.
  26.  */
  27. #include <linux/module.h>
  28. #include <linux/kernel.h>
  29. #include <linux/sched.h>
  30. #include <linux/types.h>
  31. #include <linux/fcntl.h>
  32. #include <linux/interrupt.h>
  33. #include <linux/ptrace.h>
  34. #include <linux/ioport.h>
  35. #include <linux/in.h>
  36. #include <linux/slab.h>
  37. #include <linux/string.h>
  38. #include <linux/errno.h>
  39. #include <linux/netdevice.h>
  40. #include <linux/etherdevice.h>
  41. #include <linux/skbuff.h>
  42. #include <linux/delay.h>
  43. #include <linux/init.h>
  44. #include <asm/system.h>
  45. #include <asm/bitops.h>
  46. #include <asm/ecard.h>
  47. #include <asm/io.h>
  48. #include <asm/irq.h>
  49. #include "../../net/8390.h"
  50. #define NET_DEBUG  0
  51. #define DEBUG_INIT 2
  52. static unsigned int net_debug = NET_DEBUG;
  53. static const card_ids __init etherh_cids[] = {
  54. { MANU_ANT, PROD_ANT_ETHERM      },
  55. { MANU_I3,  PROD_I3_ETHERLAN500  },
  56. { MANU_I3,  PROD_I3_ETHERLAN600  },
  57. { MANU_I3,  PROD_I3_ETHERLAN600A },
  58. { 0xffff,   0xffff }
  59. };
  60. MODULE_AUTHOR("Russell King");
  61. MODULE_DESCRIPTION("EtherH/EtherM driver");
  62. MODULE_LICENSE("GPL");
  63. static char version[] __initdata =
  64. "EtherH/EtherM Driver (c) 2000 Russell King v1.08n";
  65. #define ETHERH500_DATAPORT 0x200 /* MEMC */
  66. #define ETHERH500_NS8390 0x000 /* MEMC */
  67. #define ETHERH500_CTRLPORT 0x200 /* IOC  */
  68. #define ETHERH600_DATAPORT 16 /* MEMC */
  69. #define ETHERH600_NS8390 0x200 /* MEMC */
  70. #define ETHERH600_CTRLPORT 0x080 /* MEMC */
  71. #define ETHERH_CP_IE 1
  72. #define ETHERH_CP_IF 2
  73. #define ETHERH_CP_HEARTBEAT 2
  74. #define ETHERH_TX_START_PAGE 1
  75. #define ETHERH_STOP_PAGE 127
  76. /*
  77.  * These came from CK/TEW
  78.  */
  79. #define ETHERM_DATAPORT 0x080 /* MEMC */
  80. #define ETHERM_NS8390 0x200 /* MEMC */
  81. #define ETHERM_CTRLPORT 0x08f /* MEMC */
  82. #define ETHERM_TX_START_PAGE 64
  83. #define ETHERM_STOP_PAGE 127
  84. /* --------------------------------------------------------------------------- */
  85. static void
  86. etherh_setif(struct net_device *dev)
  87. {
  88. struct ei_device *ei_local = (struct ei_device *) dev->priv;
  89. unsigned long addr, flags;
  90. save_flags_cli(flags);
  91. /* set the interface type */
  92. switch (dev->mem_end) {
  93. case PROD_I3_ETHERLAN600:
  94. case PROD_I3_ETHERLAN600A:
  95. addr = dev->base_addr + EN0_RCNTHI;
  96. switch (dev->if_port) {
  97. case IF_PORT_10BASE2:
  98. outb((inb(addr) & 0xf8) | 1, addr);
  99. break;
  100. case IF_PORT_10BASET:
  101. outb((inb(addr) & 0xf8), addr);
  102. break;
  103. }
  104. break;
  105. case PROD_I3_ETHERLAN500:
  106. addr = dev->rmem_start;
  107. switch (dev->if_port) {
  108. case IF_PORT_10BASE2:
  109. outb(inb(addr) & ~ETHERH_CP_IF, addr);
  110. break;
  111. case IF_PORT_10BASET:
  112. outb(inb(addr) | ETHERH_CP_IF, addr);
  113. break;
  114. }
  115. break;
  116. default:
  117. break;
  118. }
  119. restore_flags(flags);
  120. }
  121. static int
  122. etherh_getifstat(struct net_device *dev)
  123. {
  124. struct ei_device *ei_local = (struct ei_device *) dev->priv;
  125. int stat = 0;
  126. switch (dev->mem_end) {
  127. case PROD_I3_ETHERLAN600:
  128. case PROD_I3_ETHERLAN600A:
  129. switch (dev->if_port) {
  130. case IF_PORT_10BASE2:
  131. stat = 1;
  132. break;
  133. case IF_PORT_10BASET:
  134. stat = inb(dev->base_addr+EN0_RCNTHI) & 4;
  135. break;
  136. }
  137. break;
  138. case PROD_I3_ETHERLAN500:
  139. switch (dev->if_port) {
  140. case IF_PORT_10BASE2:
  141. stat = 1;
  142. break;
  143. case IF_PORT_10BASET:
  144. stat = inb(dev->rmem_start) & ETHERH_CP_HEARTBEAT;
  145. break;
  146. }
  147. break;
  148. default:
  149. stat = 0;
  150. break;
  151. }
  152. return stat != 0;
  153. }
  154. /*
  155.  * Configure the interface.  Note that we ignore the other
  156.  * parts of ifmap, since its mostly meaningless for this driver.
  157.  */
  158. static int etherh_set_config(struct net_device *dev, struct ifmap *map)
  159. {
  160. switch (map->port) {
  161. case IF_PORT_10BASE2:
  162. case IF_PORT_10BASET:
  163. /*
  164.  * If the user explicitly sets the interface
  165.  * media type, turn off automedia detection.
  166.  */
  167. dev->flags &= ~IFF_AUTOMEDIA;
  168. dev->if_port = map->port;
  169. break;
  170. default:
  171. return -EINVAL;
  172. }
  173. etherh_setif(dev);
  174. return 0;
  175. }
  176. /*
  177.  * Reset the 8390 (hard reset).  Note that we can't actually do this.
  178.  */
  179. static void
  180. etherh_reset(struct net_device *dev)
  181. {
  182. struct ei_device *ei_local = (struct ei_device *) dev->priv;
  183. outb_p(E8390_NODMA+E8390_PAGE0+E8390_STOP, dev->base_addr);
  184. /*
  185.  * See if we need to change the interface type.
  186.  * Note that we use 'interface_num' as a flag
  187.  * to indicate that we need to change the media.
  188.  */
  189. if (dev->flags & IFF_AUTOMEDIA && ei_local->interface_num) {
  190. ei_local->interface_num = 0;
  191. if (dev->if_port == IF_PORT_10BASET)
  192. dev->if_port = IF_PORT_10BASE2;
  193. else
  194. dev->if_port = IF_PORT_10BASET;
  195. etherh_setif(dev);
  196. }
  197. }
  198. /*
  199.  * Write a block of data out to the 8390
  200.  */
  201. static void
  202. etherh_block_output (struct net_device *dev, int count, const unsigned char *buf, int start_page)
  203. {
  204. struct ei_device *ei_local = (struct ei_device *) dev->priv;
  205. unsigned int addr, dma_addr;
  206. unsigned long dma_start;
  207. if (ei_local->dmaing) {
  208. printk(KERN_ERR "%s: DMAing conflict in etherh_block_input: "
  209. " DMAstat %d irqlock %dn", dev->name,
  210. ei_local->dmaing, ei_local->irqlock);
  211. return;
  212. }
  213. ei_local->dmaing |= 1;
  214. addr = dev->base_addr;
  215. dma_addr = dev->mem_start;
  216. count = (count + 1) & ~1;
  217. outb (E8390_NODMA | E8390_PAGE0 | E8390_START, addr + E8390_CMD);
  218. outb (0x42, addr + EN0_RCNTLO);
  219. outb (0x00, addr + EN0_RCNTHI);
  220. outb (0x42, addr + EN0_RSARLO);
  221. outb (0x00, addr + EN0_RSARHI);
  222. outb (E8390_RREAD | E8390_START, addr + E8390_CMD);
  223. udelay (1);
  224. outb (ENISR_RDC, addr + EN0_ISR);
  225. outb (count, addr + EN0_RCNTLO);
  226. outb (count >> 8, addr + EN0_RCNTHI);
  227. outb (0, addr + EN0_RSARLO);
  228. outb (start_page, addr + EN0_RSARHI);
  229. outb (E8390_RWRITE | E8390_START, addr + E8390_CMD);
  230. if (ei_local->word16)
  231. outsw (dma_addr, buf, count >> 1);
  232. else
  233. outsb (dma_addr, buf, count);
  234. dma_start = jiffies;
  235. while ((inb (addr + EN0_ISR) & ENISR_RDC) == 0)
  236. if (jiffies - dma_start > 2*HZ/100) { /* 20ms */
  237. printk(KERN_ERR "%s: timeout waiting for TX RDCn",
  238. dev->name);
  239. etherh_reset (dev);
  240. NS8390_init (dev, 1);
  241. break;
  242. }
  243. outb (ENISR_RDC, addr + EN0_ISR);
  244. ei_local->dmaing &= ~1;
  245. }
  246. /*
  247.  * Read a block of data from the 8390
  248.  */
  249. static void
  250. etherh_block_input (struct net_device *dev, int count, struct sk_buff *skb, int ring_offset)
  251. {
  252. struct ei_device *ei_local = (struct ei_device *) dev->priv;
  253. unsigned int addr, dma_addr;
  254. unsigned char *buf;
  255. if (ei_local->dmaing) {
  256. printk(KERN_ERR "%s: DMAing conflict in etherh_block_input: "
  257. " DMAstat %d irqlock %dn", dev->name,
  258. ei_local->dmaing, ei_local->irqlock);
  259. return;
  260. }
  261. ei_local->dmaing |= 1;
  262. addr = dev->base_addr;
  263. dma_addr = dev->mem_start;
  264. buf = skb->data;
  265. outb (E8390_NODMA | E8390_PAGE0 | E8390_START, addr + E8390_CMD);
  266. outb (count, addr + EN0_RCNTLO);
  267. outb (count >> 8, addr + EN0_RCNTHI);
  268. outb (ring_offset, addr + EN0_RSARLO);
  269. outb (ring_offset >> 8, addr + EN0_RSARHI);
  270. outb (E8390_RREAD | E8390_START, addr + E8390_CMD);
  271. if (ei_local->word16) {
  272. insw (dma_addr, buf, count >> 1);
  273. if (count & 1)
  274. buf[count - 1] = inb (dma_addr);
  275. } else
  276. insb (dma_addr, buf, count);
  277. outb (ENISR_RDC, addr + EN0_ISR);
  278. ei_local->dmaing &= ~1;
  279. }
  280. /*
  281.  * Read a header from the 8390
  282.  */
  283. static void
  284. etherh_get_header (struct net_device *dev, struct e8390_pkt_hdr *hdr, int ring_page)
  285. {
  286. struct ei_device *ei_local = (struct ei_device *) dev->priv;
  287. unsigned int addr, dma_addr;
  288. if (ei_local->dmaing) {
  289. printk(KERN_ERR "%s: DMAing conflict in etherh_get_header: "
  290. " DMAstat %d irqlock %dn", dev->name,
  291. ei_local->dmaing, ei_local->irqlock);
  292. return;
  293. }
  294. ei_local->dmaing |= 1;
  295. addr = dev->base_addr;
  296. dma_addr = dev->mem_start;
  297. outb (E8390_NODMA | E8390_PAGE0 | E8390_START, addr + E8390_CMD);
  298. outb (sizeof (*hdr), addr + EN0_RCNTLO);
  299. outb (0, addr + EN0_RCNTHI);
  300. outb (0, addr + EN0_RSARLO);
  301. outb (ring_page, addr + EN0_RSARHI);
  302. outb (E8390_RREAD | E8390_START, addr + E8390_CMD);
  303. if (ei_local->word16)
  304. insw (dma_addr, hdr, sizeof (*hdr) >> 1);
  305. else
  306. insb (dma_addr, hdr, sizeof (*hdr));
  307. outb (ENISR_RDC, addr + EN0_ISR);
  308. ei_local->dmaing &= ~1;
  309. }
  310. /*
  311.  * Open/initialize the board.  This is called (in the current kernel)
  312.  * sometime after booting when the 'ifconfig' program is run.
  313.  *
  314.  * This routine should set everything up anew at each open, even
  315.  * registers that "should" only need to be set once at boot, so that
  316.  * there is non-reboot way to recover if something goes wrong.
  317.  */
  318. static int
  319. etherh_open(struct net_device *dev)
  320. {
  321. struct ei_device *ei_local = (struct ei_device *) dev->priv;
  322. if (request_irq(dev->irq, ei_interrupt, 0, dev->name, dev))
  323. return -EAGAIN;
  324. /*
  325.  * Make sure that we aren't going to change the
  326.  * media type on the next reset - we are about to
  327.  * do automedia manually now.
  328.  */
  329. ei_local->interface_num = 0;
  330. /*
  331.  * If we are doing automedia detection, do it now.
  332.  * This is more reliable than the 8390's detection.
  333.  */
  334. if (dev->flags & IFF_AUTOMEDIA) {
  335. dev->if_port = IF_PORT_10BASET;
  336. etherh_setif(dev);
  337. mdelay(1);
  338. if (!etherh_getifstat(dev)) {
  339. dev->if_port = IF_PORT_10BASE2;
  340. etherh_setif(dev);
  341. }
  342. } else
  343. etherh_setif(dev);
  344. etherh_reset(dev);
  345. ei_open(dev);
  346. return 0;
  347. }
  348. /*
  349.  * The inverse routine to etherh_open().
  350.  */
  351. static int
  352. etherh_close(struct net_device *dev)
  353. {
  354. ei_close (dev);
  355. free_irq (dev->irq, dev);
  356. return 0;
  357. }
  358. static void etherh_irq_enable(ecard_t *ec, int irqnr)
  359. {
  360. unsigned int ctrl_addr = (unsigned int)ec->irq_data;
  361. outb(inb(ctrl_addr) | ETHERH_CP_IE, ctrl_addr);
  362. }
  363. static void etherh_irq_disable(ecard_t *ec, int irqnr)
  364. {
  365. unsigned int ctrl_addr = (unsigned int)ec->irq_data;
  366. outb(inb(ctrl_addr) & ~ETHERH_CP_IE, ctrl_addr);
  367. }
  368. static expansioncard_ops_t etherh_ops = {
  369. irqenable: etherh_irq_enable,
  370. irqdisable: etherh_irq_disable,
  371. };
  372. /*
  373.  * Initialisation
  374.  */
  375. static void __init etherh_banner(void)
  376. {
  377. static int version_printed;
  378. if (net_debug && version_printed++ == 0)
  379. printk(KERN_INFO "%s", version);
  380. }
  381. /*
  382.  * Read the ethernet address string from the on board rom.
  383.  * This is an ascii string...
  384.  */
  385. static int __init etherh_addr(char *addr, struct expansion_card *ec)
  386. {
  387. struct in_chunk_dir cd;
  388. char *s;
  389. if (ecard_readchunk(&cd, ec, 0xf5, 0) && (s = strchr(cd.d.string, '('))) {
  390. int i;
  391. for (i = 0; i < 6; i++) {
  392. addr[i] = simple_strtoul(s + 1, &s, 0x10);
  393. if (*s != (i == 5? ')' : ':'))
  394. break;
  395. }
  396. if (i == 6)
  397. return 0;
  398. }
  399. return -ENODEV;
  400. }
  401. /*
  402.  * Create an ethernet address from the system serial number.
  403.  */
  404. static int __init etherm_addr(char *addr)
  405. {
  406. unsigned int serial;
  407. if (system_serial_low == 0 && system_serial_high == 0)
  408. return -ENODEV;
  409. serial = system_serial_low | system_serial_high;
  410. addr[0] = 0;
  411. addr[1] = 0;
  412. addr[2] = 0xa4;
  413. addr[3] = 0x10 + (serial >> 24);
  414. addr[4] = serial >> 16;
  415. addr[5] = serial >> 8;
  416. return 0;
  417. }
  418. static u32 etherh_regoffsets[16];
  419. static u32 etherm_regoffsets[16];
  420. static struct net_device * __init etherh_init_one(struct expansion_card *ec)
  421. {
  422. struct ei_device *ei_local;
  423. struct net_device *dev;
  424. const char *dev_type;
  425. int i, size;
  426. etherh_banner();
  427. ecard_claim(ec);
  428. dev = init_etherdev(NULL, 0);
  429. if (!dev)
  430. goto out;
  431. SET_MODULE_OWNER(dev);
  432. dev->open = etherh_open;
  433. dev->stop = etherh_close;
  434. dev->set_config = etherh_set_config;
  435. dev->irq = ec->irq;
  436. dev->base_addr = ecard_address(ec, ECARD_MEMC, 0);
  437. dev->mem_end = ec->cid.product;
  438. ec->ops = &etherh_ops;
  439. switch (ec->cid.product) {
  440. case PROD_ANT_ETHERM:
  441. if (etherm_addr(dev->dev_addr))
  442. goto free;
  443. dev->base_addr += ETHERM_NS8390;
  444. dev->mem_start  = dev->base_addr + ETHERM_DATAPORT;
  445. ec->irq_data    = (void *)(dev->base_addr + ETHERM_CTRLPORT);
  446. break;
  447. case PROD_I3_ETHERLAN500:
  448. if (etherh_addr(dev->dev_addr, ec))
  449. goto free;
  450. dev->base_addr += ETHERH500_NS8390;
  451. dev->mem_start  = dev->base_addr + ETHERH500_DATAPORT;
  452. dev->rmem_start = (unsigned long)
  453. ec->irq_data    = (void *)ecard_address (ec, ECARD_IOC, ECARD_FAST)
  454.   + ETHERH500_CTRLPORT;
  455. break;
  456. case PROD_I3_ETHERLAN600:
  457. case PROD_I3_ETHERLAN600A:
  458. if (etherh_addr(dev->dev_addr, ec))
  459. goto free;
  460. dev->base_addr += ETHERH600_NS8390;
  461. dev->mem_start = dev->base_addr + ETHERH600_DATAPORT;
  462. ec->irq_data   = (void *)(dev->base_addr + ETHERH600_CTRLPORT);
  463. break;
  464. default:
  465. printk(KERN_ERR "%s: unknown card type %xn",
  466.        dev->name, ec->cid.product);
  467. goto free;
  468. }
  469. size = 16;
  470. if (ec->cid.product == PROD_ANT_ETHERM)
  471. size <<= 3;
  472. if (!request_region(dev->base_addr, size, dev->name))
  473. goto free;
  474. if (ethdev_init(dev))
  475. goto release;
  476. /*
  477.  * Unfortunately, ethdev_init eventually calls
  478.  * ether_setup, which re-writes dev->flags.
  479.  */
  480. switch (ec->cid.product) {
  481. case PROD_ANT_ETHERM:
  482. dev_type = "ANT EtherM";
  483. dev->if_port = IF_PORT_UNKNOWN;
  484. break;
  485. case PROD_I3_ETHERLAN500:
  486. dev_type = "i3 EtherH 500";
  487. dev->if_port = IF_PORT_UNKNOWN;
  488. break;
  489. case PROD_I3_ETHERLAN600:
  490. dev_type = "i3 EtherH 600";
  491. dev->flags  |= IFF_PORTSEL | IFF_AUTOMEDIA;
  492. dev->if_port = IF_PORT_10BASET;
  493. break;
  494. case PROD_I3_ETHERLAN600A:
  495. dev_type = "i3 EtherH 600A";
  496. dev->flags  |= IFF_PORTSEL | IFF_AUTOMEDIA;
  497. dev->if_port = IF_PORT_10BASET;
  498. break;
  499. default:
  500. dev_type = "unknown";
  501. break;
  502. }
  503. printk(KERN_INFO "%s: %s in slot %d, ",
  504. dev->name, dev_type, ec->slot_no);
  505. for (i = 0; i < 6; i++)
  506. printk("%2.2x%c", dev->dev_addr[i], i == 5 ? 'n' : ':');
  507. ei_local = (struct ei_device *) dev->priv;
  508. if (ec->cid.product == PROD_ANT_ETHERM) {
  509. ei_local->tx_start_page = ETHERM_TX_START_PAGE;
  510. ei_local->stop_page     = ETHERM_STOP_PAGE;
  511. ei_local->reg_offset    = etherm_regoffsets;
  512. } else {
  513. ei_local->tx_start_page = ETHERH_TX_START_PAGE;
  514. ei_local->stop_page     = ETHERH_STOP_PAGE;
  515. ei_local->reg_offset    = etherh_regoffsets;
  516. }
  517. ei_local->name          = dev->name;
  518. ei_local->word16        = 1;
  519. ei_local->rx_start_page = ei_local->tx_start_page + TX_PAGES;
  520. ei_local->reset_8390    = etherh_reset;
  521. ei_local->block_input   = etherh_block_input;
  522. ei_local->block_output  = etherh_block_output;
  523. ei_local->get_8390_hdr  = etherh_get_header;
  524. ei_local->interface_num = 0;
  525. etherh_reset(dev);
  526. NS8390_init(dev, 0);
  527. return dev;
  528. release:
  529. release_region(dev->base_addr, 16);
  530. free:
  531. unregister_netdev(dev);
  532. kfree(dev);
  533. out:
  534. ecard_release(ec);
  535. return NULL;
  536. }
  537. #define MAX_ETHERH_CARDS 2
  538. static struct net_device *e_dev[MAX_ETHERH_CARDS];
  539. static struct expansion_card *e_card[MAX_ETHERH_CARDS];
  540. static int __init etherh_init(void)
  541. {
  542. int i, ret = -ENODEV;
  543. for (i = 0; i < 16; i++) {
  544. etherh_regoffsets[i] = i;
  545. etherm_regoffsets[i] = i << 3;
  546. }
  547. ecard_startfind();
  548. for (i = 0; i < MAX_ECARDS; i++) {
  549. struct expansion_card *ec;
  550. struct net_device *dev;
  551. ec = ecard_find(0, etherh_cids);
  552. if (!ec)
  553. break;
  554. dev = etherh_init_one(ec);
  555. if (!dev)
  556. break;
  557. e_card[i] = ec;
  558. e_dev[i]  = dev;
  559. ret = 0;
  560. }
  561. return ret;
  562. }
  563. static void __exit etherh_exit(void)
  564. {
  565. int i;
  566. for (i = 0; i < MAX_ETHERH_CARDS; i++) {
  567. if (e_dev[i]) {
  568. int size;
  569. unregister_netdev(e_dev[i]);
  570. size = 16;
  571. if (e_card[i]->cid.product == PROD_ANT_ETHERM)
  572. size <<= 3;
  573. release_region(e_dev[i]->base_addr, size);
  574. kfree(e_dev[i]);
  575. e_dev[i] = NULL;
  576. }
  577. if (e_card[i]) {
  578. e_card[i]->ops = NULL;
  579. ecard_release(e_card[i]);
  580. e_card[i] = NULL;
  581. }
  582. }
  583. }
  584. module_init(etherh_init);
  585. module_exit(etherh_exit);