mac89x0.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575
  1. /* mac89x0.c: A Crystal Semiconductor CS89[02]0 driver for linux. */
  2. /*
  3. Written 1996 by Russell Nelson, with reference to skeleton.c
  4. written 1993-1994 by Donald Becker.
  5. This software may be used and distributed according to the terms
  6. of the GNU General Public License, incorporated herein by reference.
  7. The author may be reached at nelson@crynwr.com, Crynwr
  8. Software, 11 Grant St., Potsdam, NY 13676
  9. Changelog:
  10. Mike Cruse : mcruse@cti-ltd.com
  11. : Changes for Linux 2.0 compatibility.
  12. : Added dev_id parameter in net_interrupt(),
  13. : request_irq() and free_irq(). Just NULL for now.
  14. Mike Cruse : Added MOD_INC_USE_COUNT and MOD_DEC_USE_COUNT macros
  15. : in net_open() and net_close() so kerneld would know
  16. : that the module is in use and wouldn't eject the
  17. : driver prematurely.
  18. Mike Cruse : Rewrote init_module() and cleanup_module using 8390.c
  19. : as an example. Disabled autoprobing in init_module(),
  20. : not a good thing to do to other devices while Linux
  21. : is running from all accounts.
  22. Alan Cox : Removed 1.2 support, added 2.1 extra counters.
  23. David Huggins-Daines <dhd@debian.org>
  24. Split this off into mac89x0.c, and gutted it of all parts which are
  25. not relevant to the existing CS8900 cards on the Macintosh
  26. (i.e. basically the Daynaport CS and LC cards). To be precise:
  27. * Removed all the media-detection stuff, because these cards are
  28. TP-only.
  29. * Lobotomized the ISA interrupt bogosity, because these cards use
  30. a hardwired NuBus interrupt and a magic ISAIRQ value in the card.
  31. * Basically eliminated everything not relevant to getting the
  32. cards minimally functioning on the Macintosh.
  33. I might add that these cards are badly designed even from the Mac
  34. standpoint, in that Dayna, in their infinite wisdom, used NuBus slot
  35. I/O space and NuBus interrupts for these cards, but neglected to
  36. provide anything even remotely resembling a NuBus ROM. Therefore we
  37. have to probe for them in a brain-damaged ISA-like fashion.
  38. Arnaldo Carvalho de Melo <acme@conectiva.com.br> - 11/01/2001
  39. check kmalloc and release the allocated memory on failure in
  40. mac89x0_probe and in init_module
  41. use local_irq_{save,restore}(flags) in net_get_stat, not just
  42. local_irq_{dis,en}able()
  43. */
  44. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  45. static const char version[] =
  46. "cs89x0.c:v1.02 11/26/96 Russell Nelson <nelson@crynwr.com>\n";
  47. #include <linux/module.h>
  48. /*
  49. Sources:
  50. Crynwr packet driver epktisa.
  51. Crystal Semiconductor data sheets.
  52. */
  53. #include <linux/kernel.h>
  54. #include <linux/types.h>
  55. #include <linux/fcntl.h>
  56. #include <linux/interrupt.h>
  57. #include <linux/ioport.h>
  58. #include <linux/in.h>
  59. #include <linux/string.h>
  60. #include <linux/nubus.h>
  61. #include <linux/errno.h>
  62. #include <linux/init.h>
  63. #include <linux/netdevice.h>
  64. #include <linux/platform_device.h>
  65. #include <linux/etherdevice.h>
  66. #include <linux/skbuff.h>
  67. #include <linux/delay.h>
  68. #include <linux/bitops.h>
  69. #include <linux/gfp.h>
  70. #include <asm/io.h>
  71. #include <asm/hwtest.h>
  72. #include <asm/macints.h>
  73. #include "cs89x0.h"
  74. static int debug = -1;
  75. module_param(debug, int, 0);
  76. MODULE_PARM_DESC(debug, "debug message level");
  77. /* Information that need to be kept for each board. */
  78. struct net_local {
  79. int msg_enable;
  80. int chip_type; /* one of: CS8900, CS8920, CS8920M */
  81. char chip_revision; /* revision letter of the chip ('A'...) */
  82. int send_cmd; /* the propercommand used to send a packet. */
  83. int rx_mode;
  84. int curr_rx_cfg;
  85. int send_underrun; /* keep track of how many underruns in a row we get */
  86. };
  87. /* Index to functions, as function prototypes. */
  88. static int net_open(struct net_device *dev);
  89. static netdev_tx_t net_send_packet(struct sk_buff *skb, struct net_device *dev);
  90. static irqreturn_t net_interrupt(int irq, void *dev_id);
  91. static void set_multicast_list(struct net_device *dev);
  92. static void net_rx(struct net_device *dev);
  93. static int net_close(struct net_device *dev);
  94. static struct net_device_stats *net_get_stats(struct net_device *dev);
  95. static int set_mac_address(struct net_device *dev, void *addr);
  96. /* For reading/writing registers ISA-style */
  97. static inline int
  98. readreg_io(struct net_device *dev, int portno)
  99. {
  100. nubus_writew(swab16(portno), dev->base_addr + ADD_PORT);
  101. return swab16(nubus_readw(dev->base_addr + DATA_PORT));
  102. }
  103. static inline void
  104. writereg_io(struct net_device *dev, int portno, int value)
  105. {
  106. nubus_writew(swab16(portno), dev->base_addr + ADD_PORT);
  107. nubus_writew(swab16(value), dev->base_addr + DATA_PORT);
  108. }
  109. /* These are for reading/writing registers in shared memory */
  110. static inline int
  111. readreg(struct net_device *dev, int portno)
  112. {
  113. return swab16(nubus_readw(dev->mem_start + portno));
  114. }
  115. static inline void
  116. writereg(struct net_device *dev, int portno, int value)
  117. {
  118. nubus_writew(swab16(value), dev->mem_start + portno);
  119. }
  120. static const struct net_device_ops mac89x0_netdev_ops = {
  121. .ndo_open = net_open,
  122. .ndo_stop = net_close,
  123. .ndo_start_xmit = net_send_packet,
  124. .ndo_get_stats = net_get_stats,
  125. .ndo_set_rx_mode = set_multicast_list,
  126. .ndo_set_mac_address = set_mac_address,
  127. .ndo_validate_addr = eth_validate_addr,
  128. };
  129. /* Probe for the CS8900 card in slot E. We won't bother looking
  130. anywhere else until we have a really good reason to do so. */
  131. static int mac89x0_device_probe(struct platform_device *pdev)
  132. {
  133. struct net_device *dev;
  134. struct net_local *lp;
  135. int i, slot;
  136. unsigned rev_type = 0;
  137. unsigned long ioaddr;
  138. unsigned short sig;
  139. int err = -ENODEV;
  140. struct nubus_rsrc *fres;
  141. dev = alloc_etherdev(sizeof(struct net_local));
  142. if (!dev)
  143. return -ENOMEM;
  144. /* We might have to parameterize this later */
  145. slot = 0xE;
  146. /* Get out now if there's a real NuBus card in slot E */
  147. for_each_func_rsrc(fres)
  148. if (fres->board->slot == slot)
  149. goto out;
  150. /* The pseudo-ISA bits always live at offset 0x300 (gee,
  151. wonder why...) */
  152. ioaddr = (unsigned long)
  153. nubus_slot_addr(slot) | (((slot&0xf) << 20) + DEFAULTIOBASE);
  154. {
  155. int card_present;
  156. card_present = (hwreg_present((void *)ioaddr + 4) &&
  157. hwreg_present((void *)ioaddr + DATA_PORT));
  158. if (!card_present)
  159. goto out;
  160. }
  161. nubus_writew(0, ioaddr + ADD_PORT);
  162. sig = nubus_readw(ioaddr + DATA_PORT);
  163. if (sig != swab16(CHIP_EISA_ID_SIG))
  164. goto out;
  165. SET_NETDEV_DEV(dev, &pdev->dev);
  166. /* Initialize the net_device structure. */
  167. lp = netdev_priv(dev);
  168. lp->msg_enable = netif_msg_init(debug, 0);
  169. /* Fill in the 'dev' fields. */
  170. dev->base_addr = ioaddr;
  171. dev->mem_start = (unsigned long)
  172. nubus_slot_addr(slot) | (((slot&0xf) << 20) + MMIOBASE);
  173. dev->mem_end = dev->mem_start + 0x1000;
  174. /* Turn on shared memory */
  175. writereg_io(dev, PP_BusCTL, MEMORY_ON);
  176. /* get the chip type */
  177. rev_type = readreg(dev, PRODUCT_ID_ADD);
  178. lp->chip_type = rev_type &~ REVISON_BITS;
  179. lp->chip_revision = ((rev_type & REVISON_BITS) >> 8) + 'A';
  180. /* Check the chip type and revision in order to set the correct send command
  181. CS8920 revision C and CS8900 revision F can use the faster send. */
  182. lp->send_cmd = TX_AFTER_381;
  183. if (lp->chip_type == CS8900 && lp->chip_revision >= 'F')
  184. lp->send_cmd = TX_NOW;
  185. if (lp->chip_type != CS8900 && lp->chip_revision >= 'C')
  186. lp->send_cmd = TX_NOW;
  187. netif_dbg(lp, drv, dev, "%s", version);
  188. pr_info("cs89%c0%s rev %c found at %#8lx\n",
  189. lp->chip_type == CS8900 ? '0' : '2',
  190. lp->chip_type == CS8920M ? "M" : "",
  191. lp->chip_revision, dev->base_addr);
  192. /* Try to read the MAC address */
  193. if ((readreg(dev, PP_SelfST) & (EEPROM_PRESENT | EEPROM_OK)) == 0) {
  194. pr_info("No EEPROM, giving up now.\n");
  195. goto out1;
  196. } else {
  197. for (i = 0; i < ETH_ALEN; i += 2) {
  198. /* Big-endian (why??!) */
  199. unsigned short s = readreg(dev, PP_IA + i);
  200. dev->dev_addr[i] = s >> 8;
  201. dev->dev_addr[i+1] = s & 0xff;
  202. }
  203. }
  204. dev->irq = SLOT2IRQ(slot);
  205. /* print the IRQ and ethernet address. */
  206. pr_info("MAC %pM, IRQ %d\n", dev->dev_addr, dev->irq);
  207. dev->netdev_ops = &mac89x0_netdev_ops;
  208. err = register_netdev(dev);
  209. if (err)
  210. goto out1;
  211. platform_set_drvdata(pdev, dev);
  212. return 0;
  213. out1:
  214. nubus_writew(0, dev->base_addr + ADD_PORT);
  215. out:
  216. free_netdev(dev);
  217. return err;
  218. }
  219. /* Open/initialize the board. This is called (in the current kernel)
  220. sometime after booting when the 'ifconfig' program is run.
  221. This routine should set everything up anew at each open, even
  222. registers that "should" only need to be set once at boot, so that
  223. there is non-reboot way to recover if something goes wrong.
  224. */
  225. static int
  226. net_open(struct net_device *dev)
  227. {
  228. struct net_local *lp = netdev_priv(dev);
  229. int i;
  230. /* Disable the interrupt for now */
  231. writereg(dev, PP_BusCTL, readreg(dev, PP_BusCTL) & ~ENABLE_IRQ);
  232. /* Grab the interrupt */
  233. if (request_irq(dev->irq, net_interrupt, 0, "cs89x0", dev))
  234. return -EAGAIN;
  235. /* Set up the IRQ - Apparently magic */
  236. if (lp->chip_type == CS8900)
  237. writereg(dev, PP_CS8900_ISAINT, 0);
  238. else
  239. writereg(dev, PP_CS8920_ISAINT, 0);
  240. /* set the Ethernet address */
  241. for (i=0; i < ETH_ALEN/2; i++)
  242. writereg(dev, PP_IA+i*2, dev->dev_addr[i*2] | (dev->dev_addr[i*2+1] << 8));
  243. /* Turn on both receive and transmit operations */
  244. writereg(dev, PP_LineCTL, readreg(dev, PP_LineCTL) | SERIAL_RX_ON | SERIAL_TX_ON);
  245. /* Receive only error free packets addressed to this card */
  246. lp->rx_mode = 0;
  247. writereg(dev, PP_RxCTL, DEF_RX_ACCEPT);
  248. lp->curr_rx_cfg = RX_OK_ENBL | RX_CRC_ERROR_ENBL;
  249. writereg(dev, PP_RxCFG, lp->curr_rx_cfg);
  250. writereg(dev, PP_TxCFG, TX_LOST_CRS_ENBL | TX_SQE_ERROR_ENBL | TX_OK_ENBL |
  251. TX_LATE_COL_ENBL | TX_JBR_ENBL | TX_ANY_COL_ENBL | TX_16_COL_ENBL);
  252. writereg(dev, PP_BufCFG, READY_FOR_TX_ENBL | RX_MISS_COUNT_OVRFLOW_ENBL |
  253. TX_COL_COUNT_OVRFLOW_ENBL | TX_UNDERRUN_ENBL);
  254. /* now that we've got our act together, enable everything */
  255. writereg(dev, PP_BusCTL, readreg(dev, PP_BusCTL) | ENABLE_IRQ);
  256. netif_start_queue(dev);
  257. return 0;
  258. }
  259. static netdev_tx_t
  260. net_send_packet(struct sk_buff *skb, struct net_device *dev)
  261. {
  262. struct net_local *lp = netdev_priv(dev);
  263. unsigned long flags;
  264. netif_dbg(lp, tx_queued, dev, "sent %d byte packet of type %x\n",
  265. skb->len, skb->data[ETH_ALEN + ETH_ALEN] << 8 |
  266. skb->data[ETH_ALEN + ETH_ALEN + 1]);
  267. /* keep the upload from being interrupted, since we
  268. ask the chip to start transmitting before the
  269. whole packet has been completely uploaded. */
  270. local_irq_save(flags);
  271. netif_stop_queue(dev);
  272. /* initiate a transmit sequence */
  273. writereg(dev, PP_TxCMD, lp->send_cmd);
  274. writereg(dev, PP_TxLength, skb->len);
  275. /* Test to see if the chip has allocated memory for the packet */
  276. if ((readreg(dev, PP_BusST) & READY_FOR_TX_NOW) == 0) {
  277. /* Gasp! It hasn't. But that shouldn't happen since
  278. we're waiting for TxOk, so return 1 and requeue this packet. */
  279. local_irq_restore(flags);
  280. return NETDEV_TX_BUSY;
  281. }
  282. /* Write the contents of the packet */
  283. skb_copy_from_linear_data(skb, (void *)(dev->mem_start + PP_TxFrame),
  284. skb->len+1);
  285. local_irq_restore(flags);
  286. dev_kfree_skb (skb);
  287. return NETDEV_TX_OK;
  288. }
  289. /* The typical workload of the driver:
  290. Handle the network interface interrupts. */
  291. static irqreturn_t net_interrupt(int irq, void *dev_id)
  292. {
  293. struct net_device *dev = dev_id;
  294. struct net_local *lp;
  295. int ioaddr, status;
  296. ioaddr = dev->base_addr;
  297. lp = netdev_priv(dev);
  298. /* we MUST read all the events out of the ISQ, otherwise we'll never
  299. get interrupted again. As a consequence, we can't have any limit
  300. on the number of times we loop in the interrupt handler. The
  301. hardware guarantees that eventually we'll run out of events. Of
  302. course, if you're on a slow machine, and packets are arriving
  303. faster than you can read them off, you're screwed. Hasta la
  304. vista, baby! */
  305. while ((status = swab16(nubus_readw(dev->base_addr + ISQ_PORT)))) {
  306. netif_dbg(lp, intr, dev, "status=%04x\n", status);
  307. switch(status & ISQ_EVENT_MASK) {
  308. case ISQ_RECEIVER_EVENT:
  309. /* Got a packet(s). */
  310. net_rx(dev);
  311. break;
  312. case ISQ_TRANSMITTER_EVENT:
  313. dev->stats.tx_packets++;
  314. netif_wake_queue(dev);
  315. if ((status & TX_OK) == 0)
  316. dev->stats.tx_errors++;
  317. if (status & TX_LOST_CRS)
  318. dev->stats.tx_carrier_errors++;
  319. if (status & TX_SQE_ERROR)
  320. dev->stats.tx_heartbeat_errors++;
  321. if (status & TX_LATE_COL)
  322. dev->stats.tx_window_errors++;
  323. if (status & TX_16_COL)
  324. dev->stats.tx_aborted_errors++;
  325. break;
  326. case ISQ_BUFFER_EVENT:
  327. if (status & READY_FOR_TX) {
  328. /* we tried to transmit a packet earlier,
  329. but inexplicably ran out of buffers.
  330. That shouldn't happen since we only ever
  331. load one packet. Shrug. Do the right
  332. thing anyway. */
  333. netif_wake_queue(dev);
  334. }
  335. if (status & TX_UNDERRUN) {
  336. netif_dbg(lp, tx_err, dev, "transmit underrun\n");
  337. lp->send_underrun++;
  338. if (lp->send_underrun == 3) lp->send_cmd = TX_AFTER_381;
  339. else if (lp->send_underrun == 6) lp->send_cmd = TX_AFTER_ALL;
  340. }
  341. break;
  342. case ISQ_RX_MISS_EVENT:
  343. dev->stats.rx_missed_errors += (status >> 6);
  344. break;
  345. case ISQ_TX_COL_EVENT:
  346. dev->stats.collisions += (status >> 6);
  347. break;
  348. }
  349. }
  350. return IRQ_HANDLED;
  351. }
  352. /* We have a good packet(s), get it/them out of the buffers. */
  353. static void
  354. net_rx(struct net_device *dev)
  355. {
  356. struct net_local *lp = netdev_priv(dev);
  357. struct sk_buff *skb;
  358. int status, length;
  359. status = readreg(dev, PP_RxStatus);
  360. if ((status & RX_OK) == 0) {
  361. dev->stats.rx_errors++;
  362. if (status & RX_RUNT)
  363. dev->stats.rx_length_errors++;
  364. if (status & RX_EXTRA_DATA)
  365. dev->stats.rx_length_errors++;
  366. if ((status & RX_CRC_ERROR) &&
  367. !(status & (RX_EXTRA_DATA|RX_RUNT)))
  368. /* per str 172 */
  369. dev->stats.rx_crc_errors++;
  370. if (status & RX_DRIBBLE)
  371. dev->stats.rx_frame_errors++;
  372. return;
  373. }
  374. length = readreg(dev, PP_RxLength);
  375. /* Malloc up new buffer. */
  376. skb = alloc_skb(length, GFP_ATOMIC);
  377. if (skb == NULL) {
  378. dev->stats.rx_dropped++;
  379. return;
  380. }
  381. skb_put(skb, length);
  382. skb_copy_to_linear_data(skb, (void *)(dev->mem_start + PP_RxFrame),
  383. length);
  384. netif_dbg(lp, rx_status, dev, "received %d byte packet of type %x\n",
  385. length, skb->data[ETH_ALEN + ETH_ALEN] << 8 |
  386. skb->data[ETH_ALEN + ETH_ALEN + 1]);
  387. skb->protocol=eth_type_trans(skb,dev);
  388. netif_rx(skb);
  389. dev->stats.rx_packets++;
  390. dev->stats.rx_bytes += length;
  391. }
  392. /* The inverse routine to net_open(). */
  393. static int
  394. net_close(struct net_device *dev)
  395. {
  396. writereg(dev, PP_RxCFG, 0);
  397. writereg(dev, PP_TxCFG, 0);
  398. writereg(dev, PP_BufCFG, 0);
  399. writereg(dev, PP_BusCTL, 0);
  400. netif_stop_queue(dev);
  401. free_irq(dev->irq, dev);
  402. /* Update the statistics here. */
  403. return 0;
  404. }
  405. /* Get the current statistics. This may be called with the card open or
  406. closed. */
  407. static struct net_device_stats *
  408. net_get_stats(struct net_device *dev)
  409. {
  410. unsigned long flags;
  411. local_irq_save(flags);
  412. /* Update the statistics from the device registers. */
  413. dev->stats.rx_missed_errors += (readreg(dev, PP_RxMiss) >> 6);
  414. dev->stats.collisions += (readreg(dev, PP_TxCol) >> 6);
  415. local_irq_restore(flags);
  416. return &dev->stats;
  417. }
  418. static void set_multicast_list(struct net_device *dev)
  419. {
  420. struct net_local *lp = netdev_priv(dev);
  421. if(dev->flags&IFF_PROMISC)
  422. {
  423. lp->rx_mode = RX_ALL_ACCEPT;
  424. } else if ((dev->flags & IFF_ALLMULTI) || !netdev_mc_empty(dev)) {
  425. /* The multicast-accept list is initialized to accept-all, and we
  426. rely on higher-level filtering for now. */
  427. lp->rx_mode = RX_MULTCAST_ACCEPT;
  428. }
  429. else
  430. lp->rx_mode = 0;
  431. writereg(dev, PP_RxCTL, DEF_RX_ACCEPT | lp->rx_mode);
  432. /* in promiscuous mode, we accept errored packets, so we have to enable interrupts on them also */
  433. writereg(dev, PP_RxCFG, lp->curr_rx_cfg |
  434. (lp->rx_mode == RX_ALL_ACCEPT? (RX_CRC_ERROR_ENBL|RX_RUNT_ENBL|RX_EXTRA_DATA_ENBL) : 0));
  435. }
  436. static int set_mac_address(struct net_device *dev, void *addr)
  437. {
  438. struct sockaddr *saddr = addr;
  439. int i;
  440. if (!is_valid_ether_addr(saddr->sa_data))
  441. return -EADDRNOTAVAIL;
  442. memcpy(dev->dev_addr, saddr->sa_data, ETH_ALEN);
  443. netdev_info(dev, "Setting MAC address to %pM\n", dev->dev_addr);
  444. /* set the Ethernet address */
  445. for (i=0; i < ETH_ALEN/2; i++)
  446. writereg(dev, PP_IA+i*2, dev->dev_addr[i*2] | (dev->dev_addr[i*2+1] << 8));
  447. return 0;
  448. }
  449. MODULE_LICENSE("GPL");
  450. static int mac89x0_device_remove(struct platform_device *pdev)
  451. {
  452. struct net_device *dev = platform_get_drvdata(pdev);
  453. unregister_netdev(dev);
  454. nubus_writew(0, dev->base_addr + ADD_PORT);
  455. free_netdev(dev);
  456. return 0;
  457. }
  458. static struct platform_driver mac89x0_platform_driver = {
  459. .probe = mac89x0_device_probe,
  460. .remove = mac89x0_device_remove,
  461. .driver = {
  462. .name = "mac89x0",
  463. },
  464. };
  465. module_platform_driver(mac89x0_platform_driver);