3c589_cs.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969
  1. /* ======================================================================
  2. *
  3. * A PCMCIA ethernet driver for the 3com 3c589 card.
  4. *
  5. * Copyright (C) 1999 David A. Hinds -- dahinds@users.sourceforge.net
  6. *
  7. * 3c589_cs.c 1.162 2001/10/13 00:08:50
  8. *
  9. * The network driver code is based on Donald Becker's 3c589 code:
  10. *
  11. * Written 1994 by Donald Becker.
  12. * Copyright 1993 United States Government as represented by the
  13. * Director, National Security Agency. This software may be used and
  14. * distributed according to the terms of the GNU General Public License,
  15. * incorporated herein by reference.
  16. * Donald Becker may be reached at becker@scyld.com
  17. *
  18. * Updated for 2.5.x by Alan Cox <alan@lxorguk.ukuu.org.uk>
  19. *
  20. * ======================================================================
  21. */
  22. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  23. #define DRV_NAME "3c589_cs"
  24. #define DRV_VERSION "1.162-ac"
  25. #include <linux/module.h>
  26. #include <linux/kernel.h>
  27. #include <linux/ptrace.h>
  28. #include <linux/slab.h>
  29. #include <linux/string.h>
  30. #include <linux/timer.h>
  31. #include <linux/interrupt.h>
  32. #include <linux/in.h>
  33. #include <linux/delay.h>
  34. #include <linux/ethtool.h>
  35. #include <linux/netdevice.h>
  36. #include <linux/etherdevice.h>
  37. #include <linux/skbuff.h>
  38. #include <linux/if_arp.h>
  39. #include <linux/ioport.h>
  40. #include <linux/bitops.h>
  41. #include <linux/jiffies.h>
  42. #include <linux/uaccess.h>
  43. #include <linux/io.h>
  44. #include <pcmcia/cistpl.h>
  45. #include <pcmcia/cisreg.h>
  46. #include <pcmcia/ciscode.h>
  47. #include <pcmcia/ds.h>
  48. /* To minimize the size of the driver source I only define operating
  49. * constants if they are used several times. You'll need the manual
  50. * if you want to understand driver details.
  51. */
  52. /* Offsets from base I/O address. */
  53. #define EL3_DATA 0x00
  54. #define EL3_TIMER 0x0a
  55. #define EL3_CMD 0x0e
  56. #define EL3_STATUS 0x0e
  57. #define EEPROM_READ 0x0080
  58. #define EEPROM_BUSY 0x8000
  59. #define EL3WINDOW(win_num) outw(SelectWindow + (win_num), ioaddr + EL3_CMD)
  60. /* The top five bits written to EL3_CMD are a command, the lower
  61. * 11 bits are the parameter, if applicable.
  62. */
  63. enum c509cmd {
  64. TotalReset = 0<<11,
  65. SelectWindow = 1<<11,
  66. StartCoax = 2<<11,
  67. RxDisable = 3<<11,
  68. RxEnable = 4<<11,
  69. RxReset = 5<<11,
  70. RxDiscard = 8<<11,
  71. TxEnable = 9<<11,
  72. TxDisable = 10<<11,
  73. TxReset = 11<<11,
  74. FakeIntr = 12<<11,
  75. AckIntr = 13<<11,
  76. SetIntrEnb = 14<<11,
  77. SetStatusEnb = 15<<11,
  78. SetRxFilter = 16<<11,
  79. SetRxThreshold = 17<<11,
  80. SetTxThreshold = 18<<11,
  81. SetTxStart = 19<<11,
  82. StatsEnable = 21<<11,
  83. StatsDisable = 22<<11,
  84. StopCoax = 23<<11
  85. };
  86. enum c509status {
  87. IntLatch = 0x0001,
  88. AdapterFailure = 0x0002,
  89. TxComplete = 0x0004,
  90. TxAvailable = 0x0008,
  91. RxComplete = 0x0010,
  92. RxEarly = 0x0020,
  93. IntReq = 0x0040,
  94. StatsFull = 0x0080,
  95. CmdBusy = 0x1000
  96. };
  97. /* The SetRxFilter command accepts the following classes: */
  98. enum RxFilter {
  99. RxStation = 1,
  100. RxMulticast = 2,
  101. RxBroadcast = 4,
  102. RxProm = 8
  103. };
  104. /* Register window 1 offsets, the window used in normal operation. */
  105. #define TX_FIFO 0x00
  106. #define RX_FIFO 0x00
  107. #define RX_STATUS 0x08
  108. #define TX_STATUS 0x0B
  109. #define TX_FREE 0x0C /* Remaining free bytes in Tx buffer. */
  110. #define WN0_IRQ 0x08 /* Window 0: Set IRQ line in bits 12-15. */
  111. #define WN4_MEDIA 0x0A /* Window 4: Various transcvr/media bits. */
  112. #define MEDIA_TP 0x00C0 /* Enable link beat and jabber for 10baseT. */
  113. #define MEDIA_LED 0x0001 /* Enable link light on 3C589E cards. */
  114. /* Time in jiffies before concluding Tx hung */
  115. #define TX_TIMEOUT ((400*HZ)/1000)
  116. struct el3_private {
  117. struct pcmcia_device *p_dev;
  118. /* For transceiver monitoring */
  119. struct timer_list media;
  120. u16 media_status;
  121. u16 fast_poll;
  122. unsigned long last_irq;
  123. spinlock_t lock;
  124. };
  125. static const char *if_names[] = { "auto", "10baseT", "10base2", "AUI" };
  126. /*====================================================================*/
  127. /* Module parameters */
  128. MODULE_AUTHOR("David Hinds <dahinds@users.sourceforge.net>");
  129. MODULE_DESCRIPTION("3Com 3c589 series PCMCIA ethernet driver");
  130. MODULE_LICENSE("GPL");
  131. #define INT_MODULE_PARM(n, v) static int n = v; module_param(n, int, 0)
  132. /* Special hook for setting if_port when module is loaded */
  133. INT_MODULE_PARM(if_port, 0);
  134. /*====================================================================*/
  135. static int tc589_config(struct pcmcia_device *link);
  136. static void tc589_release(struct pcmcia_device *link);
  137. static u16 read_eeprom(unsigned int ioaddr, int index);
  138. static void tc589_reset(struct net_device *dev);
  139. static void media_check(unsigned long arg);
  140. static int el3_config(struct net_device *dev, struct ifmap *map);
  141. static int el3_open(struct net_device *dev);
  142. static netdev_tx_t el3_start_xmit(struct sk_buff *skb,
  143. struct net_device *dev);
  144. static irqreturn_t el3_interrupt(int irq, void *dev_id);
  145. static void update_stats(struct net_device *dev);
  146. static struct net_device_stats *el3_get_stats(struct net_device *dev);
  147. static int el3_rx(struct net_device *dev);
  148. static int el3_close(struct net_device *dev);
  149. static void el3_tx_timeout(struct net_device *dev);
  150. static void set_rx_mode(struct net_device *dev);
  151. static void set_multicast_list(struct net_device *dev);
  152. static const struct ethtool_ops netdev_ethtool_ops;
  153. static void tc589_detach(struct pcmcia_device *p_dev);
  154. static const struct net_device_ops el3_netdev_ops = {
  155. .ndo_open = el3_open,
  156. .ndo_stop = el3_close,
  157. .ndo_start_xmit = el3_start_xmit,
  158. .ndo_tx_timeout = el3_tx_timeout,
  159. .ndo_set_config = el3_config,
  160. .ndo_get_stats = el3_get_stats,
  161. .ndo_set_rx_mode = set_multicast_list,
  162. .ndo_change_mtu = eth_change_mtu,
  163. .ndo_set_mac_address = eth_mac_addr,
  164. .ndo_validate_addr = eth_validate_addr,
  165. };
  166. static int tc589_probe(struct pcmcia_device *link)
  167. {
  168. struct el3_private *lp;
  169. struct net_device *dev;
  170. dev_dbg(&link->dev, "3c589_attach()\n");
  171. /* Create new ethernet device */
  172. dev = alloc_etherdev(sizeof(struct el3_private));
  173. if (!dev)
  174. return -ENOMEM;
  175. lp = netdev_priv(dev);
  176. link->priv = dev;
  177. lp->p_dev = link;
  178. spin_lock_init(&lp->lock);
  179. link->resource[0]->end = 16;
  180. link->resource[0]->flags |= IO_DATA_PATH_WIDTH_16;
  181. link->config_flags |= CONF_ENABLE_IRQ;
  182. link->config_index = 1;
  183. dev->netdev_ops = &el3_netdev_ops;
  184. dev->watchdog_timeo = TX_TIMEOUT;
  185. dev->ethtool_ops = &netdev_ethtool_ops;
  186. return tc589_config(link);
  187. }
  188. static void tc589_detach(struct pcmcia_device *link)
  189. {
  190. struct net_device *dev = link->priv;
  191. dev_dbg(&link->dev, "3c589_detach\n");
  192. unregister_netdev(dev);
  193. tc589_release(link);
  194. free_netdev(dev);
  195. } /* tc589_detach */
  196. static int tc589_config(struct pcmcia_device *link)
  197. {
  198. struct net_device *dev = link->priv;
  199. __be16 *phys_addr;
  200. int ret, i, j, multi = 0, fifo;
  201. unsigned int ioaddr;
  202. static const char * const ram_split[] = {"5:3", "3:1", "1:1", "3:5"};
  203. u8 *buf;
  204. size_t len;
  205. dev_dbg(&link->dev, "3c589_config\n");
  206. phys_addr = (__be16 *)dev->dev_addr;
  207. /* Is this a 3c562? */
  208. if (link->manf_id != MANFID_3COM)
  209. dev_info(&link->dev, "hmmm, is this really a 3Com card??\n");
  210. multi = (link->card_id == PRODID_3COM_3C562);
  211. link->io_lines = 16;
  212. /* For the 3c562, the base address must be xx00-xx7f */
  213. for (i = j = 0; j < 0x400; j += 0x10) {
  214. if (multi && (j & 0x80))
  215. continue;
  216. link->resource[0]->start = j ^ 0x300;
  217. i = pcmcia_request_io(link);
  218. if (i == 0)
  219. break;
  220. }
  221. if (i != 0)
  222. goto failed;
  223. ret = pcmcia_request_irq(link, el3_interrupt);
  224. if (ret)
  225. goto failed;
  226. ret = pcmcia_enable_device(link);
  227. if (ret)
  228. goto failed;
  229. dev->irq = link->irq;
  230. dev->base_addr = link->resource[0]->start;
  231. ioaddr = dev->base_addr;
  232. EL3WINDOW(0);
  233. /* The 3c589 has an extra EEPROM for configuration info, including
  234. * the hardware address. The 3c562 puts the address in the CIS.
  235. */
  236. len = pcmcia_get_tuple(link, 0x88, &buf);
  237. if (buf && len >= 6) {
  238. for (i = 0; i < 3; i++)
  239. phys_addr[i] = htons(le16_to_cpu(buf[i*2]));
  240. kfree(buf);
  241. } else {
  242. kfree(buf); /* 0 < len < 6 */
  243. for (i = 0; i < 3; i++)
  244. phys_addr[i] = htons(read_eeprom(ioaddr, i));
  245. if (phys_addr[0] == htons(0x6060)) {
  246. dev_err(&link->dev, "IO port conflict at 0x%03lx-0x%03lx\n",
  247. dev->base_addr, dev->base_addr+15);
  248. goto failed;
  249. }
  250. }
  251. /* The address and resource configuration register aren't loaded from
  252. * the EEPROM and *must* be set to 0 and IRQ3 for the PCMCIA version.
  253. */
  254. outw(0x3f00, ioaddr + 8);
  255. fifo = inl(ioaddr);
  256. /* The if_port symbol can be set when the module is loaded */
  257. if ((if_port >= 0) && (if_port <= 3))
  258. dev->if_port = if_port;
  259. else
  260. dev_err(&link->dev, "invalid if_port requested\n");
  261. SET_NETDEV_DEV(dev, &link->dev);
  262. if (register_netdev(dev) != 0) {
  263. dev_err(&link->dev, "register_netdev() failed\n");
  264. goto failed;
  265. }
  266. netdev_info(dev, "3Com 3c%s, io %#3lx, irq %d, hw_addr %pM\n",
  267. (multi ? "562" : "589"), dev->base_addr, dev->irq,
  268. dev->dev_addr);
  269. netdev_info(dev, " %dK FIFO split %s Rx:Tx, %s xcvr\n",
  270. (fifo & 7) ? 32 : 8, ram_split[(fifo >> 16) & 3],
  271. if_names[dev->if_port]);
  272. return 0;
  273. failed:
  274. tc589_release(link);
  275. return -ENODEV;
  276. } /* tc589_config */
  277. static void tc589_release(struct pcmcia_device *link)
  278. {
  279. pcmcia_disable_device(link);
  280. }
  281. static int tc589_suspend(struct pcmcia_device *link)
  282. {
  283. struct net_device *dev = link->priv;
  284. if (link->open)
  285. netif_device_detach(dev);
  286. return 0;
  287. }
  288. static int tc589_resume(struct pcmcia_device *link)
  289. {
  290. struct net_device *dev = link->priv;
  291. if (link->open) {
  292. tc589_reset(dev);
  293. netif_device_attach(dev);
  294. }
  295. return 0;
  296. }
  297. /*====================================================================*/
  298. /* Use this for commands that may take time to finish */
  299. static void tc589_wait_for_completion(struct net_device *dev, int cmd)
  300. {
  301. int i = 100;
  302. outw(cmd, dev->base_addr + EL3_CMD);
  303. while (--i > 0)
  304. if (!(inw(dev->base_addr + EL3_STATUS) & 0x1000))
  305. break;
  306. if (i == 0)
  307. netdev_warn(dev, "command 0x%04x did not complete!\n", cmd);
  308. }
  309. /* Read a word from the EEPROM using the regular EEPROM access register.
  310. * Assume that we are in register window zero.
  311. */
  312. static u16 read_eeprom(unsigned int ioaddr, int index)
  313. {
  314. int i;
  315. outw(EEPROM_READ + index, ioaddr + 10);
  316. /* Reading the eeprom takes 162 us */
  317. for (i = 1620; i >= 0; i--)
  318. if ((inw(ioaddr + 10) & EEPROM_BUSY) == 0)
  319. break;
  320. return inw(ioaddr + 12);
  321. }
  322. /* Set transceiver type, perhaps to something other than what the user
  323. * specified in dev->if_port.
  324. */
  325. static void tc589_set_xcvr(struct net_device *dev, int if_port)
  326. {
  327. struct el3_private *lp = netdev_priv(dev);
  328. unsigned int ioaddr = dev->base_addr;
  329. EL3WINDOW(0);
  330. switch (if_port) {
  331. case 0:
  332. case 1:
  333. outw(0, ioaddr + 6);
  334. break;
  335. case 2:
  336. outw(3<<14, ioaddr + 6);
  337. break;
  338. case 3:
  339. outw(1<<14, ioaddr + 6);
  340. break;
  341. }
  342. /* On PCMCIA, this just turns on the LED */
  343. outw((if_port == 2) ? StartCoax : StopCoax, ioaddr + EL3_CMD);
  344. /* 10baseT interface, enable link beat and jabber check. */
  345. EL3WINDOW(4);
  346. outw(MEDIA_LED | ((if_port < 2) ? MEDIA_TP : 0), ioaddr + WN4_MEDIA);
  347. EL3WINDOW(1);
  348. if (if_port == 2)
  349. lp->media_status = ((dev->if_port == 0) ? 0x8000 : 0x4000);
  350. else
  351. lp->media_status = ((dev->if_port == 0) ? 0x4010 : 0x8800);
  352. }
  353. static void dump_status(struct net_device *dev)
  354. {
  355. unsigned int ioaddr = dev->base_addr;
  356. EL3WINDOW(1);
  357. netdev_info(dev, " irq status %04x, rx status %04x, tx status %02x tx free %04x\n",
  358. inw(ioaddr+EL3_STATUS), inw(ioaddr+RX_STATUS),
  359. inb(ioaddr+TX_STATUS), inw(ioaddr+TX_FREE));
  360. EL3WINDOW(4);
  361. netdev_info(dev, " diagnostics: fifo %04x net %04x ethernet %04x media %04x\n",
  362. inw(ioaddr+0x04), inw(ioaddr+0x06), inw(ioaddr+0x08),
  363. inw(ioaddr+0x0a));
  364. EL3WINDOW(1);
  365. }
  366. /* Reset and restore all of the 3c589 registers. */
  367. static void tc589_reset(struct net_device *dev)
  368. {
  369. unsigned int ioaddr = dev->base_addr;
  370. int i;
  371. EL3WINDOW(0);
  372. outw(0x0001, ioaddr + 4); /* Activate board. */
  373. outw(0x3f00, ioaddr + 8); /* Set the IRQ line. */
  374. /* Set the station address in window 2. */
  375. EL3WINDOW(2);
  376. for (i = 0; i < 6; i++)
  377. outb(dev->dev_addr[i], ioaddr + i);
  378. tc589_set_xcvr(dev, dev->if_port);
  379. /* Switch to the stats window, and clear all stats by reading. */
  380. outw(StatsDisable, ioaddr + EL3_CMD);
  381. EL3WINDOW(6);
  382. for (i = 0; i < 9; i++)
  383. inb(ioaddr+i);
  384. inw(ioaddr + 10);
  385. inw(ioaddr + 12);
  386. /* Switch to register set 1 for normal use. */
  387. EL3WINDOW(1);
  388. set_rx_mode(dev);
  389. outw(StatsEnable, ioaddr + EL3_CMD); /* Turn on statistics. */
  390. outw(RxEnable, ioaddr + EL3_CMD); /* Enable the receiver. */
  391. outw(TxEnable, ioaddr + EL3_CMD); /* Enable transmitter. */
  392. /* Allow status bits to be seen. */
  393. outw(SetStatusEnb | 0xff, ioaddr + EL3_CMD);
  394. /* Ack all pending events, and set active indicator mask. */
  395. outw(AckIntr | IntLatch | TxAvailable | RxEarly | IntReq,
  396. ioaddr + EL3_CMD);
  397. outw(SetIntrEnb | IntLatch | TxAvailable | RxComplete | StatsFull
  398. | AdapterFailure, ioaddr + EL3_CMD);
  399. }
  400. static void netdev_get_drvinfo(struct net_device *dev,
  401. struct ethtool_drvinfo *info)
  402. {
  403. strlcpy(info->driver, DRV_NAME, sizeof(info->driver));
  404. strlcpy(info->version, DRV_VERSION, sizeof(info->version));
  405. snprintf(info->bus_info, sizeof(info->bus_info),
  406. "PCMCIA 0x%lx", dev->base_addr);
  407. }
  408. static const struct ethtool_ops netdev_ethtool_ops = {
  409. .get_drvinfo = netdev_get_drvinfo,
  410. };
  411. static int el3_config(struct net_device *dev, struct ifmap *map)
  412. {
  413. if ((map->port != (u_char)(-1)) && (map->port != dev->if_port)) {
  414. if (map->port <= 3) {
  415. dev->if_port = map->port;
  416. netdev_info(dev, "switched to %s port\n", if_names[dev->if_port]);
  417. tc589_set_xcvr(dev, dev->if_port);
  418. } else {
  419. return -EINVAL;
  420. }
  421. }
  422. return 0;
  423. }
  424. static int el3_open(struct net_device *dev)
  425. {
  426. struct el3_private *lp = netdev_priv(dev);
  427. struct pcmcia_device *link = lp->p_dev;
  428. if (!pcmcia_dev_present(link))
  429. return -ENODEV;
  430. link->open++;
  431. netif_start_queue(dev);
  432. tc589_reset(dev);
  433. setup_timer(&lp->media, media_check, (unsigned long)dev);
  434. mod_timer(&lp->media, jiffies + HZ);
  435. dev_dbg(&link->dev, "%s: opened, status %4.4x.\n",
  436. dev->name, inw(dev->base_addr + EL3_STATUS));
  437. return 0;
  438. }
  439. static void el3_tx_timeout(struct net_device *dev)
  440. {
  441. unsigned int ioaddr = dev->base_addr;
  442. netdev_warn(dev, "Transmit timed out!\n");
  443. dump_status(dev);
  444. dev->stats.tx_errors++;
  445. netif_trans_update(dev); /* prevent tx timeout */
  446. /* Issue TX_RESET and TX_START commands. */
  447. tc589_wait_for_completion(dev, TxReset);
  448. outw(TxEnable, ioaddr + EL3_CMD);
  449. netif_wake_queue(dev);
  450. }
  451. static void pop_tx_status(struct net_device *dev)
  452. {
  453. unsigned int ioaddr = dev->base_addr;
  454. int i;
  455. /* Clear the Tx status stack. */
  456. for (i = 32; i > 0; i--) {
  457. u_char tx_status = inb(ioaddr + TX_STATUS);
  458. if (!(tx_status & 0x84))
  459. break;
  460. /* reset transmitter on jabber error or underrun */
  461. if (tx_status & 0x30)
  462. tc589_wait_for_completion(dev, TxReset);
  463. if (tx_status & 0x38) {
  464. netdev_dbg(dev, "transmit error: status 0x%02x\n", tx_status);
  465. outw(TxEnable, ioaddr + EL3_CMD);
  466. dev->stats.tx_aborted_errors++;
  467. }
  468. outb(0x00, ioaddr + TX_STATUS); /* Pop the status stack. */
  469. }
  470. }
  471. static netdev_tx_t el3_start_xmit(struct sk_buff *skb,
  472. struct net_device *dev)
  473. {
  474. unsigned int ioaddr = dev->base_addr;
  475. struct el3_private *priv = netdev_priv(dev);
  476. unsigned long flags;
  477. netdev_dbg(dev, "el3_start_xmit(length = %ld) called, status %4.4x.\n",
  478. (long)skb->len, inw(ioaddr + EL3_STATUS));
  479. spin_lock_irqsave(&priv->lock, flags);
  480. dev->stats.tx_bytes += skb->len;
  481. /* Put out the doubleword header... */
  482. outw(skb->len, ioaddr + TX_FIFO);
  483. outw(0x00, ioaddr + TX_FIFO);
  484. /* ... and the packet rounded to a doubleword. */
  485. outsl(ioaddr + TX_FIFO, skb->data, (skb->len + 3) >> 2);
  486. if (inw(ioaddr + TX_FREE) <= 1536) {
  487. netif_stop_queue(dev);
  488. /* Interrupt us when the FIFO has room for max-sized packet. */
  489. outw(SetTxThreshold + 1536, ioaddr + EL3_CMD);
  490. }
  491. pop_tx_status(dev);
  492. spin_unlock_irqrestore(&priv->lock, flags);
  493. dev_kfree_skb(skb);
  494. return NETDEV_TX_OK;
  495. }
  496. /* The EL3 interrupt handler. */
  497. static irqreturn_t el3_interrupt(int irq, void *dev_id)
  498. {
  499. struct net_device *dev = (struct net_device *) dev_id;
  500. struct el3_private *lp = netdev_priv(dev);
  501. unsigned int ioaddr;
  502. __u16 status;
  503. int i = 0, handled = 1;
  504. if (!netif_device_present(dev))
  505. return IRQ_NONE;
  506. ioaddr = dev->base_addr;
  507. netdev_dbg(dev, "interrupt, status %4.4x.\n", inw(ioaddr + EL3_STATUS));
  508. spin_lock(&lp->lock);
  509. while ((status = inw(ioaddr + EL3_STATUS)) &
  510. (IntLatch | RxComplete | StatsFull)) {
  511. if ((status & 0xe000) != 0x2000) {
  512. netdev_dbg(dev, "interrupt from dead card\n");
  513. handled = 0;
  514. break;
  515. }
  516. if (status & RxComplete)
  517. el3_rx(dev);
  518. if (status & TxAvailable) {
  519. netdev_dbg(dev, " TX room bit was handled.\n");
  520. /* There's room in the FIFO for a full-sized packet. */
  521. outw(AckIntr | TxAvailable, ioaddr + EL3_CMD);
  522. netif_wake_queue(dev);
  523. }
  524. if (status & TxComplete)
  525. pop_tx_status(dev);
  526. if (status & (AdapterFailure | RxEarly | StatsFull)) {
  527. /* Handle all uncommon interrupts. */
  528. if (status & StatsFull) /* Empty statistics. */
  529. update_stats(dev);
  530. if (status & RxEarly) {
  531. /* Rx early is unused. */
  532. el3_rx(dev);
  533. outw(AckIntr | RxEarly, ioaddr + EL3_CMD);
  534. }
  535. if (status & AdapterFailure) {
  536. u16 fifo_diag;
  537. EL3WINDOW(4);
  538. fifo_diag = inw(ioaddr + 4);
  539. EL3WINDOW(1);
  540. netdev_warn(dev, "adapter failure, FIFO diagnostic register %04x.\n",
  541. fifo_diag);
  542. if (fifo_diag & 0x0400) {
  543. /* Tx overrun */
  544. tc589_wait_for_completion(dev, TxReset);
  545. outw(TxEnable, ioaddr + EL3_CMD);
  546. }
  547. if (fifo_diag & 0x2000) {
  548. /* Rx underrun */
  549. tc589_wait_for_completion(dev, RxReset);
  550. set_rx_mode(dev);
  551. outw(RxEnable, ioaddr + EL3_CMD);
  552. }
  553. outw(AckIntr | AdapterFailure, ioaddr + EL3_CMD);
  554. }
  555. }
  556. if (++i > 10) {
  557. netdev_err(dev, "infinite loop in interrupt, status %4.4x.\n",
  558. status);
  559. /* Clear all interrupts */
  560. outw(AckIntr | 0xFF, ioaddr + EL3_CMD);
  561. break;
  562. }
  563. /* Acknowledge the IRQ. */
  564. outw(AckIntr | IntReq | IntLatch, ioaddr + EL3_CMD);
  565. }
  566. lp->last_irq = jiffies;
  567. spin_unlock(&lp->lock);
  568. netdev_dbg(dev, "exiting interrupt, status %4.4x.\n",
  569. inw(ioaddr + EL3_STATUS));
  570. return IRQ_RETVAL(handled);
  571. }
  572. static void media_check(unsigned long arg)
  573. {
  574. struct net_device *dev = (struct net_device *)(arg);
  575. struct el3_private *lp = netdev_priv(dev);
  576. unsigned int ioaddr = dev->base_addr;
  577. u16 media, errs;
  578. unsigned long flags;
  579. if (!netif_device_present(dev))
  580. goto reschedule;
  581. /* Check for pending interrupt with expired latency timer: with
  582. * this, we can limp along even if the interrupt is blocked
  583. */
  584. if ((inw(ioaddr + EL3_STATUS) & IntLatch) &&
  585. (inb(ioaddr + EL3_TIMER) == 0xff)) {
  586. if (!lp->fast_poll)
  587. netdev_warn(dev, "interrupt(s) dropped!\n");
  588. local_irq_save(flags);
  589. el3_interrupt(dev->irq, dev);
  590. local_irq_restore(flags);
  591. lp->fast_poll = HZ;
  592. }
  593. if (lp->fast_poll) {
  594. lp->fast_poll--;
  595. lp->media.expires = jiffies + HZ/100;
  596. add_timer(&lp->media);
  597. return;
  598. }
  599. /* lp->lock guards the EL3 window. Window should always be 1 except
  600. * when the lock is held
  601. */
  602. spin_lock_irqsave(&lp->lock, flags);
  603. EL3WINDOW(4);
  604. media = inw(ioaddr+WN4_MEDIA) & 0xc810;
  605. /* Ignore collisions unless we've had no irq's recently */
  606. if (time_before(jiffies, lp->last_irq + HZ)) {
  607. media &= ~0x0010;
  608. } else {
  609. /* Try harder to detect carrier errors */
  610. EL3WINDOW(6);
  611. outw(StatsDisable, ioaddr + EL3_CMD);
  612. errs = inb(ioaddr + 0);
  613. outw(StatsEnable, ioaddr + EL3_CMD);
  614. dev->stats.tx_carrier_errors += errs;
  615. if (errs || (lp->media_status & 0x0010))
  616. media |= 0x0010;
  617. }
  618. if (media != lp->media_status) {
  619. if ((media & lp->media_status & 0x8000) &&
  620. ((lp->media_status ^ media) & 0x0800))
  621. netdev_info(dev, "%s link beat\n",
  622. (lp->media_status & 0x0800 ? "lost" : "found"));
  623. else if ((media & lp->media_status & 0x4000) &&
  624. ((lp->media_status ^ media) & 0x0010))
  625. netdev_info(dev, "coax cable %s\n",
  626. (lp->media_status & 0x0010 ? "ok" : "problem"));
  627. if (dev->if_port == 0) {
  628. if (media & 0x8000) {
  629. if (media & 0x0800)
  630. netdev_info(dev, "flipped to 10baseT\n");
  631. else
  632. tc589_set_xcvr(dev, 2);
  633. } else if (media & 0x4000) {
  634. if (media & 0x0010)
  635. tc589_set_xcvr(dev, 1);
  636. else
  637. netdev_info(dev, "flipped to 10base2\n");
  638. }
  639. }
  640. lp->media_status = media;
  641. }
  642. EL3WINDOW(1);
  643. spin_unlock_irqrestore(&lp->lock, flags);
  644. reschedule:
  645. lp->media.expires = jiffies + HZ;
  646. add_timer(&lp->media);
  647. }
  648. static struct net_device_stats *el3_get_stats(struct net_device *dev)
  649. {
  650. struct el3_private *lp = netdev_priv(dev);
  651. unsigned long flags;
  652. struct pcmcia_device *link = lp->p_dev;
  653. if (pcmcia_dev_present(link)) {
  654. spin_lock_irqsave(&lp->lock, flags);
  655. update_stats(dev);
  656. spin_unlock_irqrestore(&lp->lock, flags);
  657. }
  658. return &dev->stats;
  659. }
  660. /* Update statistics. We change to register window 6, so this should be run
  661. * single-threaded if the device is active. This is expected to be a rare
  662. * operation, and it's simpler for the rest of the driver to assume that
  663. * window 1 is always valid rather than use a special window-state variable.
  664. *
  665. * Caller must hold the lock for this
  666. */
  667. static void update_stats(struct net_device *dev)
  668. {
  669. unsigned int ioaddr = dev->base_addr;
  670. netdev_dbg(dev, "updating the statistics.\n");
  671. /* Turn off statistics updates while reading. */
  672. outw(StatsDisable, ioaddr + EL3_CMD);
  673. /* Switch to the stats window, and read everything. */
  674. EL3WINDOW(6);
  675. dev->stats.tx_carrier_errors += inb(ioaddr + 0);
  676. dev->stats.tx_heartbeat_errors += inb(ioaddr + 1);
  677. /* Multiple collisions. */
  678. inb(ioaddr + 2);
  679. dev->stats.collisions += inb(ioaddr + 3);
  680. dev->stats.tx_window_errors += inb(ioaddr + 4);
  681. dev->stats.rx_fifo_errors += inb(ioaddr + 5);
  682. dev->stats.tx_packets += inb(ioaddr + 6);
  683. /* Rx packets */
  684. inb(ioaddr + 7);
  685. /* Tx deferrals */
  686. inb(ioaddr + 8);
  687. /* Rx octets */
  688. inw(ioaddr + 10);
  689. /* Tx octets */
  690. inw(ioaddr + 12);
  691. /* Back to window 1, and turn statistics back on. */
  692. EL3WINDOW(1);
  693. outw(StatsEnable, ioaddr + EL3_CMD);
  694. }
  695. static int el3_rx(struct net_device *dev)
  696. {
  697. unsigned int ioaddr = dev->base_addr;
  698. int worklimit = 32;
  699. short rx_status;
  700. netdev_dbg(dev, "in rx_packet(), status %4.4x, rx_status %4.4x.\n",
  701. inw(ioaddr+EL3_STATUS), inw(ioaddr+RX_STATUS));
  702. while (!((rx_status = inw(ioaddr + RX_STATUS)) & 0x8000) &&
  703. worklimit > 0) {
  704. worklimit--;
  705. if (rx_status & 0x4000) { /* Error, update stats. */
  706. short error = rx_status & 0x3800;
  707. dev->stats.rx_errors++;
  708. switch (error) {
  709. case 0x0000:
  710. dev->stats.rx_over_errors++;
  711. break;
  712. case 0x0800:
  713. dev->stats.rx_length_errors++;
  714. break;
  715. case 0x1000:
  716. dev->stats.rx_frame_errors++;
  717. break;
  718. case 0x1800:
  719. dev->stats.rx_length_errors++;
  720. break;
  721. case 0x2000:
  722. dev->stats.rx_frame_errors++;
  723. break;
  724. case 0x2800:
  725. dev->stats.rx_crc_errors++;
  726. break;
  727. }
  728. } else {
  729. short pkt_len = rx_status & 0x7ff;
  730. struct sk_buff *skb;
  731. skb = netdev_alloc_skb(dev, pkt_len + 5);
  732. netdev_dbg(dev, " Receiving packet size %d status %4.4x.\n",
  733. pkt_len, rx_status);
  734. if (skb != NULL) {
  735. skb_reserve(skb, 2);
  736. insl(ioaddr+RX_FIFO, skb_put(skb, pkt_len),
  737. (pkt_len+3)>>2);
  738. skb->protocol = eth_type_trans(skb, dev);
  739. netif_rx(skb);
  740. dev->stats.rx_packets++;
  741. dev->stats.rx_bytes += pkt_len;
  742. } else {
  743. netdev_dbg(dev, "couldn't allocate a sk_buff of size %d.\n",
  744. pkt_len);
  745. dev->stats.rx_dropped++;
  746. }
  747. }
  748. /* Pop the top of the Rx FIFO */
  749. tc589_wait_for_completion(dev, RxDiscard);
  750. }
  751. if (worklimit == 0)
  752. netdev_warn(dev, "too much work in el3_rx!\n");
  753. return 0;
  754. }
  755. static void set_rx_mode(struct net_device *dev)
  756. {
  757. unsigned int ioaddr = dev->base_addr;
  758. u16 opts = SetRxFilter | RxStation | RxBroadcast;
  759. if (dev->flags & IFF_PROMISC)
  760. opts |= RxMulticast | RxProm;
  761. else if (!netdev_mc_empty(dev) || (dev->flags & IFF_ALLMULTI))
  762. opts |= RxMulticast;
  763. outw(opts, ioaddr + EL3_CMD);
  764. }
  765. static void set_multicast_list(struct net_device *dev)
  766. {
  767. struct el3_private *priv = netdev_priv(dev);
  768. unsigned long flags;
  769. spin_lock_irqsave(&priv->lock, flags);
  770. set_rx_mode(dev);
  771. spin_unlock_irqrestore(&priv->lock, flags);
  772. }
  773. static int el3_close(struct net_device *dev)
  774. {
  775. struct el3_private *lp = netdev_priv(dev);
  776. struct pcmcia_device *link = lp->p_dev;
  777. unsigned int ioaddr = dev->base_addr;
  778. dev_dbg(&link->dev, "%s: shutting down ethercard.\n", dev->name);
  779. if (pcmcia_dev_present(link)) {
  780. /* Turn off statistics ASAP. We update dev->stats below. */
  781. outw(StatsDisable, ioaddr + EL3_CMD);
  782. /* Disable the receiver and transmitter. */
  783. outw(RxDisable, ioaddr + EL3_CMD);
  784. outw(TxDisable, ioaddr + EL3_CMD);
  785. if (dev->if_port == 2)
  786. /* Turn off thinnet power. Green! */
  787. outw(StopCoax, ioaddr + EL3_CMD);
  788. else if (dev->if_port == 1) {
  789. /* Disable link beat and jabber */
  790. EL3WINDOW(4);
  791. outw(0, ioaddr + WN4_MEDIA);
  792. }
  793. /* Switching back to window 0 disables the IRQ. */
  794. EL3WINDOW(0);
  795. /* But we explicitly zero the IRQ line select anyway. */
  796. outw(0x0f00, ioaddr + WN0_IRQ);
  797. /* Check if the card still exists */
  798. if ((inw(ioaddr+EL3_STATUS) & 0xe000) == 0x2000)
  799. update_stats(dev);
  800. }
  801. link->open--;
  802. netif_stop_queue(dev);
  803. del_timer_sync(&lp->media);
  804. return 0;
  805. }
  806. static const struct pcmcia_device_id tc589_ids[] = {
  807. PCMCIA_MFC_DEVICE_MANF_CARD(0, 0x0101, 0x0562),
  808. PCMCIA_MFC_DEVICE_PROD_ID1(0, "Motorola MARQUIS", 0xf03e4e77),
  809. PCMCIA_DEVICE_MANF_CARD(0x0101, 0x0589),
  810. PCMCIA_DEVICE_PROD_ID12("Farallon", "ENet", 0x58d93fc4, 0x992c2202),
  811. PCMCIA_MFC_DEVICE_CIS_MANF_CARD(0, 0x0101, 0x0035, "cis/3CXEM556.cis"),
  812. PCMCIA_MFC_DEVICE_CIS_MANF_CARD(0, 0x0101, 0x003d, "cis/3CXEM556.cis"),
  813. PCMCIA_DEVICE_NULL,
  814. };
  815. MODULE_DEVICE_TABLE(pcmcia, tc589_ids);
  816. static struct pcmcia_driver tc589_driver = {
  817. .owner = THIS_MODULE,
  818. .name = "3c589_cs",
  819. .probe = tc589_probe,
  820. .remove = tc589_detach,
  821. .id_table = tc589_ids,
  822. .suspend = tc589_suspend,
  823. .resume = tc589_resume,
  824. };
  825. module_pcmcia_driver(tc589_driver);