macmace.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817
  1. /*
  2. * Driver for the Macintosh 68K onboard MACE controller with PSC
  3. * driven DMA. The MACE driver code is derived from mace.c. The
  4. * Mac68k theory of operation is courtesy of the MacBSD wizards.
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the License, or (at your option) any later version.
  10. *
  11. * Copyright (C) 1996 Paul Mackerras.
  12. * Copyright (C) 1998 Alan Cox <alan@redhat.com>
  13. *
  14. * Modified heavily by Joshua M. Thompson based on Dave Huang's NetBSD driver
  15. *
  16. * Copyright (C) 2007 Finn Thain
  17. *
  18. * Converted to DMA API, converted to unified driver model,
  19. * sync'd some routines with mace.c and fixed various bugs.
  20. */
  21. #include <linux/kernel.h>
  22. #include <linux/module.h>
  23. #include <linux/netdevice.h>
  24. #include <linux/etherdevice.h>
  25. #include <linux/delay.h>
  26. #include <linux/string.h>
  27. #include <linux/crc32.h>
  28. #include <linux/bitrev.h>
  29. #include <linux/dma-mapping.h>
  30. #include <linux/platform_device.h>
  31. #include <asm/io.h>
  32. #include <asm/irq.h>
  33. #include <asm/macintosh.h>
  34. #include <asm/macints.h>
  35. #include <asm/mac_psc.h>
  36. #include <asm/page.h>
  37. #include "mace.h"
  38. static char mac_mace_string[] = "macmace";
  39. static struct platform_device *mac_mace_device;
  40. #define N_TX_BUFF_ORDER 0
  41. #define N_TX_RING (1 << N_TX_BUFF_ORDER)
  42. #define N_RX_BUFF_ORDER 3
  43. #define N_RX_RING (1 << N_RX_BUFF_ORDER)
  44. #define TX_TIMEOUT HZ
  45. #define MACE_BUFF_SIZE 0x800
  46. /* Chip rev needs workaround on HW & multicast addr change */
  47. #define BROKEN_ADDRCHG_REV 0x0941
  48. /* The MACE is simply wired down on a Mac68K box */
  49. #define MACE_BASE (void *)(0x50F1C000)
  50. #define MACE_PROM (void *)(0x50F08001)
  51. struct mace_data {
  52. volatile struct mace *mace;
  53. unsigned char *tx_ring;
  54. dma_addr_t tx_ring_phys;
  55. unsigned char *rx_ring;
  56. dma_addr_t rx_ring_phys;
  57. int dma_intr;
  58. int rx_slot, rx_tail;
  59. int tx_slot, tx_sloti, tx_count;
  60. int chipid;
  61. struct device *device;
  62. };
  63. struct mace_frame {
  64. u8 rcvcnt;
  65. u8 pad1;
  66. u8 rcvsts;
  67. u8 pad2;
  68. u8 rntpc;
  69. u8 pad3;
  70. u8 rcvcc;
  71. u8 pad4;
  72. u32 pad5;
  73. u32 pad6;
  74. u8 data[1];
  75. /* And frame continues.. */
  76. };
  77. #define PRIV_BYTES sizeof(struct mace_data)
  78. static int mace_open(struct net_device *dev);
  79. static int mace_close(struct net_device *dev);
  80. static int mace_xmit_start(struct sk_buff *skb, struct net_device *dev);
  81. static void mace_set_multicast(struct net_device *dev);
  82. static int mace_set_address(struct net_device *dev, void *addr);
  83. static void mace_reset(struct net_device *dev);
  84. static irqreturn_t mace_interrupt(int irq, void *dev_id);
  85. static irqreturn_t mace_dma_intr(int irq, void *dev_id);
  86. static void mace_tx_timeout(struct net_device *dev);
  87. static void __mace_set_address(struct net_device *dev, void *addr);
  88. /*
  89. * Load a receive DMA channel with a base address and ring length
  90. */
  91. static void mace_load_rxdma_base(struct net_device *dev, int set)
  92. {
  93. struct mace_data *mp = netdev_priv(dev);
  94. psc_write_word(PSC_ENETRD_CMD + set, 0x0100);
  95. psc_write_long(PSC_ENETRD_ADDR + set, (u32) mp->rx_ring_phys);
  96. psc_write_long(PSC_ENETRD_LEN + set, N_RX_RING);
  97. psc_write_word(PSC_ENETRD_CMD + set, 0x9800);
  98. mp->rx_tail = 0;
  99. }
  100. /*
  101. * Reset the receive DMA subsystem
  102. */
  103. static void mace_rxdma_reset(struct net_device *dev)
  104. {
  105. struct mace_data *mp = netdev_priv(dev);
  106. volatile struct mace *mace = mp->mace;
  107. u8 maccc = mace->maccc;
  108. mace->maccc = maccc & ~ENRCV;
  109. psc_write_word(PSC_ENETRD_CTL, 0x8800);
  110. mace_load_rxdma_base(dev, 0x00);
  111. psc_write_word(PSC_ENETRD_CTL, 0x0400);
  112. psc_write_word(PSC_ENETRD_CTL, 0x8800);
  113. mace_load_rxdma_base(dev, 0x10);
  114. psc_write_word(PSC_ENETRD_CTL, 0x0400);
  115. mace->maccc = maccc;
  116. mp->rx_slot = 0;
  117. psc_write_word(PSC_ENETRD_CMD + PSC_SET0, 0x9800);
  118. psc_write_word(PSC_ENETRD_CMD + PSC_SET1, 0x9800);
  119. }
  120. /*
  121. * Reset the transmit DMA subsystem
  122. */
  123. static void mace_txdma_reset(struct net_device *dev)
  124. {
  125. struct mace_data *mp = netdev_priv(dev);
  126. volatile struct mace *mace = mp->mace;
  127. u8 maccc;
  128. psc_write_word(PSC_ENETWR_CTL, 0x8800);
  129. maccc = mace->maccc;
  130. mace->maccc = maccc & ~ENXMT;
  131. mp->tx_slot = mp->tx_sloti = 0;
  132. mp->tx_count = N_TX_RING;
  133. psc_write_word(PSC_ENETWR_CTL, 0x0400);
  134. mace->maccc = maccc;
  135. }
  136. /*
  137. * Disable DMA
  138. */
  139. static void mace_dma_off(struct net_device *dev)
  140. {
  141. psc_write_word(PSC_ENETRD_CTL, 0x8800);
  142. psc_write_word(PSC_ENETRD_CTL, 0x1000);
  143. psc_write_word(PSC_ENETRD_CMD + PSC_SET0, 0x1100);
  144. psc_write_word(PSC_ENETRD_CMD + PSC_SET1, 0x1100);
  145. psc_write_word(PSC_ENETWR_CTL, 0x8800);
  146. psc_write_word(PSC_ENETWR_CTL, 0x1000);
  147. psc_write_word(PSC_ENETWR_CMD + PSC_SET0, 0x1100);
  148. psc_write_word(PSC_ENETWR_CMD + PSC_SET1, 0x1100);
  149. }
  150. /*
  151. * Not really much of a probe. The hardware table tells us if this
  152. * model of Macintrash has a MACE (AV macintoshes)
  153. */
  154. static int __devinit mace_probe(struct platform_device *pdev)
  155. {
  156. int j;
  157. struct mace_data *mp;
  158. unsigned char *addr;
  159. struct net_device *dev;
  160. unsigned char checksum = 0;
  161. static int found = 0;
  162. int err;
  163. DECLARE_MAC_BUF(mac);
  164. if (found || macintosh_config->ether_type != MAC_ETHER_MACE)
  165. return -ENODEV;
  166. found = 1; /* prevent 'finding' one on every device probe */
  167. dev = alloc_etherdev(PRIV_BYTES);
  168. if (!dev)
  169. return -ENOMEM;
  170. mp = netdev_priv(dev);
  171. mp->device = &pdev->dev;
  172. SET_NETDEV_DEV(dev, &pdev->dev);
  173. dev->base_addr = (u32)MACE_BASE;
  174. mp->mace = (volatile struct mace *) MACE_BASE;
  175. dev->irq = IRQ_MAC_MACE;
  176. mp->dma_intr = IRQ_MAC_MACE_DMA;
  177. mp->chipid = mp->mace->chipid_hi << 8 | mp->mace->chipid_lo;
  178. /*
  179. * The PROM contains 8 bytes which total 0xFF when XOR'd
  180. * together. Due to the usual peculiar apple brain damage
  181. * the bytes are spaced out in a strange boundary and the
  182. * bits are reversed.
  183. */
  184. addr = (void *)MACE_PROM;
  185. for (j = 0; j < 6; ++j) {
  186. u8 v = bitrev8(addr[j<<4]);
  187. checksum ^= v;
  188. dev->dev_addr[j] = v;
  189. }
  190. for (; j < 8; ++j) {
  191. checksum ^= bitrev8(addr[j<<4]);
  192. }
  193. if (checksum != 0xFF) {
  194. free_netdev(dev);
  195. return -ENODEV;
  196. }
  197. dev->open = mace_open;
  198. dev->stop = mace_close;
  199. dev->hard_start_xmit = mace_xmit_start;
  200. dev->tx_timeout = mace_tx_timeout;
  201. dev->watchdog_timeo = TX_TIMEOUT;
  202. dev->set_multicast_list = mace_set_multicast;
  203. dev->set_mac_address = mace_set_address;
  204. printk(KERN_INFO "%s: 68K MACE, hardware address %s\n",
  205. dev->name, print_mac(mac, dev->dev_addr));
  206. err = register_netdev(dev);
  207. if (!err)
  208. return 0;
  209. free_netdev(dev);
  210. return err;
  211. }
  212. /*
  213. * Reset the chip.
  214. */
  215. static void mace_reset(struct net_device *dev)
  216. {
  217. struct mace_data *mp = netdev_priv(dev);
  218. volatile struct mace *mb = mp->mace;
  219. int i;
  220. /* soft-reset the chip */
  221. i = 200;
  222. while (--i) {
  223. mb->biucc = SWRST;
  224. if (mb->biucc & SWRST) {
  225. udelay(10);
  226. continue;
  227. }
  228. break;
  229. }
  230. if (!i) {
  231. printk(KERN_ERR "macmace: cannot reset chip!\n");
  232. return;
  233. }
  234. mb->maccc = 0; /* turn off tx, rx */
  235. mb->imr = 0xFF; /* disable all intrs for now */
  236. i = mb->ir;
  237. mb->biucc = XMTSP_64;
  238. mb->utr = RTRD;
  239. mb->fifocc = XMTFW_8 | RCVFW_64 | XMTFWU | RCVFWU;
  240. mb->xmtfc = AUTO_PAD_XMIT; /* auto-pad short frames */
  241. mb->rcvfc = 0;
  242. /* load up the hardware address */
  243. __mace_set_address(dev, dev->dev_addr);
  244. /* clear the multicast filter */
  245. if (mp->chipid == BROKEN_ADDRCHG_REV)
  246. mb->iac = LOGADDR;
  247. else {
  248. mb->iac = ADDRCHG | LOGADDR;
  249. while ((mb->iac & ADDRCHG) != 0)
  250. ;
  251. }
  252. for (i = 0; i < 8; ++i)
  253. mb->ladrf = 0;
  254. /* done changing address */
  255. if (mp->chipid != BROKEN_ADDRCHG_REV)
  256. mb->iac = 0;
  257. mb->plscc = PORTSEL_AUI;
  258. }
  259. /*
  260. * Load the address on a mace controller.
  261. */
  262. static void __mace_set_address(struct net_device *dev, void *addr)
  263. {
  264. struct mace_data *mp = netdev_priv(dev);
  265. volatile struct mace *mb = mp->mace;
  266. unsigned char *p = addr;
  267. int i;
  268. /* load up the hardware address */
  269. if (mp->chipid == BROKEN_ADDRCHG_REV)
  270. mb->iac = PHYADDR;
  271. else {
  272. mb->iac = ADDRCHG | PHYADDR;
  273. while ((mb->iac & ADDRCHG) != 0)
  274. ;
  275. }
  276. for (i = 0; i < 6; ++i)
  277. mb->padr = dev->dev_addr[i] = p[i];
  278. if (mp->chipid != BROKEN_ADDRCHG_REV)
  279. mb->iac = 0;
  280. }
  281. static int mace_set_address(struct net_device *dev, void *addr)
  282. {
  283. struct mace_data *mp = netdev_priv(dev);
  284. volatile struct mace *mb = mp->mace;
  285. unsigned long flags;
  286. u8 maccc;
  287. local_irq_save(flags);
  288. maccc = mb->maccc;
  289. __mace_set_address(dev, addr);
  290. mb->maccc = maccc;
  291. local_irq_restore(flags);
  292. return 0;
  293. }
  294. /*
  295. * Open the Macintosh MACE. Most of this is playing with the DMA
  296. * engine. The ethernet chip is quite friendly.
  297. */
  298. static int mace_open(struct net_device *dev)
  299. {
  300. struct mace_data *mp = netdev_priv(dev);
  301. volatile struct mace *mb = mp->mace;
  302. /* reset the chip */
  303. mace_reset(dev);
  304. if (request_irq(dev->irq, mace_interrupt, 0, dev->name, dev)) {
  305. printk(KERN_ERR "%s: can't get irq %d\n", dev->name, dev->irq);
  306. return -EAGAIN;
  307. }
  308. if (request_irq(mp->dma_intr, mace_dma_intr, 0, dev->name, dev)) {
  309. printk(KERN_ERR "%s: can't get irq %d\n", dev->name, mp->dma_intr);
  310. free_irq(dev->irq, dev);
  311. return -EAGAIN;
  312. }
  313. /* Allocate the DMA ring buffers */
  314. mp->tx_ring = dma_alloc_coherent(mp->device,
  315. N_TX_RING * MACE_BUFF_SIZE,
  316. &mp->tx_ring_phys, GFP_KERNEL);
  317. if (mp->tx_ring == NULL) {
  318. printk(KERN_ERR "%s: unable to allocate DMA tx buffers\n", dev->name);
  319. goto out1;
  320. }
  321. mp->rx_ring = dma_alloc_coherent(mp->device,
  322. N_RX_RING * MACE_BUFF_SIZE,
  323. &mp->rx_ring_phys, GFP_KERNEL);
  324. if (mp->rx_ring == NULL) {
  325. printk(KERN_ERR "%s: unable to allocate DMA rx buffers\n", dev->name);
  326. goto out2;
  327. }
  328. mace_dma_off(dev);
  329. /* Not sure what these do */
  330. psc_write_word(PSC_ENETWR_CTL, 0x9000);
  331. psc_write_word(PSC_ENETRD_CTL, 0x9000);
  332. psc_write_word(PSC_ENETWR_CTL, 0x0400);
  333. psc_write_word(PSC_ENETRD_CTL, 0x0400);
  334. mace_rxdma_reset(dev);
  335. mace_txdma_reset(dev);
  336. /* turn it on! */
  337. mb->maccc = ENXMT | ENRCV;
  338. /* enable all interrupts except receive interrupts */
  339. mb->imr = RCVINT;
  340. return 0;
  341. out2:
  342. dma_free_coherent(mp->device, N_TX_RING * MACE_BUFF_SIZE,
  343. mp->tx_ring, mp->tx_ring_phys);
  344. out1:
  345. free_irq(dev->irq, dev);
  346. free_irq(mp->dma_intr, dev);
  347. return -ENOMEM;
  348. }
  349. /*
  350. * Shut down the mace and its interrupt channel
  351. */
  352. static int mace_close(struct net_device *dev)
  353. {
  354. struct mace_data *mp = netdev_priv(dev);
  355. volatile struct mace *mb = mp->mace;
  356. mb->maccc = 0; /* disable rx and tx */
  357. mb->imr = 0xFF; /* disable all irqs */
  358. mace_dma_off(dev); /* disable rx and tx dma */
  359. return 0;
  360. }
  361. /*
  362. * Transmit a frame
  363. */
  364. static int mace_xmit_start(struct sk_buff *skb, struct net_device *dev)
  365. {
  366. struct mace_data *mp = netdev_priv(dev);
  367. unsigned long flags;
  368. /* Stop the queue since there's only the one buffer */
  369. local_irq_save(flags);
  370. netif_stop_queue(dev);
  371. if (!mp->tx_count) {
  372. printk(KERN_ERR "macmace: tx queue running but no free buffers.\n");
  373. local_irq_restore(flags);
  374. return NETDEV_TX_BUSY;
  375. }
  376. mp->tx_count--;
  377. local_irq_restore(flags);
  378. dev->stats.tx_packets++;
  379. dev->stats.tx_bytes += skb->len;
  380. /* We need to copy into our xmit buffer to take care of alignment and caching issues */
  381. skb_copy_from_linear_data(skb, mp->tx_ring, skb->len);
  382. /* load the Tx DMA and fire it off */
  383. psc_write_long(PSC_ENETWR_ADDR + mp->tx_slot, (u32) mp->tx_ring_phys);
  384. psc_write_long(PSC_ENETWR_LEN + mp->tx_slot, skb->len);
  385. psc_write_word(PSC_ENETWR_CMD + mp->tx_slot, 0x9800);
  386. mp->tx_slot ^= 0x10;
  387. dev_kfree_skb(skb);
  388. dev->trans_start = jiffies;
  389. return NETDEV_TX_OK;
  390. }
  391. static void mace_set_multicast(struct net_device *dev)
  392. {
  393. struct mace_data *mp = netdev_priv(dev);
  394. volatile struct mace *mb = mp->mace;
  395. int i, j;
  396. u32 crc;
  397. u8 maccc;
  398. unsigned long flags;
  399. local_irq_save(flags);
  400. maccc = mb->maccc;
  401. mb->maccc &= ~PROM;
  402. if (dev->flags & IFF_PROMISC) {
  403. mb->maccc |= PROM;
  404. } else {
  405. unsigned char multicast_filter[8];
  406. struct dev_mc_list *dmi = dev->mc_list;
  407. if (dev->flags & IFF_ALLMULTI) {
  408. for (i = 0; i < 8; i++) {
  409. multicast_filter[i] = 0xFF;
  410. }
  411. } else {
  412. for (i = 0; i < 8; i++)
  413. multicast_filter[i] = 0;
  414. for (i = 0; i < dev->mc_count; i++) {
  415. crc = ether_crc_le(6, dmi->dmi_addr);
  416. j = crc >> 26; /* bit number in multicast_filter */
  417. multicast_filter[j >> 3] |= 1 << (j & 7);
  418. dmi = dmi->next;
  419. }
  420. }
  421. if (mp->chipid == BROKEN_ADDRCHG_REV)
  422. mb->iac = LOGADDR;
  423. else {
  424. mb->iac = ADDRCHG | LOGADDR;
  425. while ((mb->iac & ADDRCHG) != 0)
  426. ;
  427. }
  428. for (i = 0; i < 8; ++i)
  429. mb->ladrf = multicast_filter[i];
  430. if (mp->chipid != BROKEN_ADDRCHG_REV)
  431. mb->iac = 0;
  432. }
  433. mb->maccc = maccc;
  434. local_irq_restore(flags);
  435. }
  436. static void mace_handle_misc_intrs(struct net_device *dev, int intr)
  437. {
  438. struct mace_data *mp = netdev_priv(dev);
  439. volatile struct mace *mb = mp->mace;
  440. static int mace_babbles, mace_jabbers;
  441. if (intr & MPCO)
  442. dev->stats.rx_missed_errors += 256;
  443. dev->stats.rx_missed_errors += mb->mpc; /* reading clears it */
  444. if (intr & RNTPCO)
  445. dev->stats.rx_length_errors += 256;
  446. dev->stats.rx_length_errors += mb->rntpc; /* reading clears it */
  447. if (intr & CERR)
  448. ++dev->stats.tx_heartbeat_errors;
  449. if (intr & BABBLE)
  450. if (mace_babbles++ < 4)
  451. printk(KERN_DEBUG "macmace: babbling transmitter\n");
  452. if (intr & JABBER)
  453. if (mace_jabbers++ < 4)
  454. printk(KERN_DEBUG "macmace: jabbering transceiver\n");
  455. }
  456. static irqreturn_t mace_interrupt(int irq, void *dev_id)
  457. {
  458. struct net_device *dev = (struct net_device *) dev_id;
  459. struct mace_data *mp = netdev_priv(dev);
  460. volatile struct mace *mb = mp->mace;
  461. int intr, fs;
  462. unsigned long flags;
  463. /* don't want the dma interrupt handler to fire */
  464. local_irq_save(flags);
  465. intr = mb->ir; /* read interrupt register */
  466. mace_handle_misc_intrs(dev, intr);
  467. if (intr & XMTINT) {
  468. fs = mb->xmtfs;
  469. if ((fs & XMTSV) == 0) {
  470. printk(KERN_ERR "macmace: xmtfs not valid! (fs=%x)\n", fs);
  471. mace_reset(dev);
  472. /*
  473. * XXX mace likes to hang the machine after a xmtfs error.
  474. * This is hard to reproduce, reseting *may* help
  475. */
  476. }
  477. /* dma should have finished */
  478. if (!mp->tx_count) {
  479. printk(KERN_DEBUG "macmace: tx ring ran out? (fs=%x)\n", fs);
  480. }
  481. /* Update stats */
  482. if (fs & (UFLO|LCOL|LCAR|RTRY)) {
  483. ++dev->stats.tx_errors;
  484. if (fs & LCAR)
  485. ++dev->stats.tx_carrier_errors;
  486. else if (fs & (UFLO|LCOL|RTRY)) {
  487. ++dev->stats.tx_aborted_errors;
  488. if (mb->xmtfs & UFLO) {
  489. printk(KERN_ERR "%s: DMA underrun.\n", dev->name);
  490. dev->stats.tx_fifo_errors++;
  491. mace_txdma_reset(dev);
  492. }
  493. }
  494. }
  495. }
  496. if (mp->tx_count)
  497. netif_wake_queue(dev);
  498. local_irq_restore(flags);
  499. return IRQ_HANDLED;
  500. }
  501. static void mace_tx_timeout(struct net_device *dev)
  502. {
  503. struct mace_data *mp = netdev_priv(dev);
  504. volatile struct mace *mb = mp->mace;
  505. unsigned long flags;
  506. local_irq_save(flags);
  507. /* turn off both tx and rx and reset the chip */
  508. mb->maccc = 0;
  509. printk(KERN_ERR "macmace: transmit timeout - resetting\n");
  510. mace_txdma_reset(dev);
  511. mace_reset(dev);
  512. /* restart rx dma */
  513. mace_rxdma_reset(dev);
  514. mp->tx_count = N_TX_RING;
  515. netif_wake_queue(dev);
  516. /* turn it on! */
  517. mb->maccc = ENXMT | ENRCV;
  518. /* enable all interrupts except receive interrupts */
  519. mb->imr = RCVINT;
  520. local_irq_restore(flags);
  521. }
  522. /*
  523. * Handle a newly arrived frame
  524. */
  525. static void mace_dma_rx_frame(struct net_device *dev, struct mace_frame *mf)
  526. {
  527. struct sk_buff *skb;
  528. unsigned int frame_status = mf->rcvsts;
  529. if (frame_status & (RS_OFLO | RS_CLSN | RS_FRAMERR | RS_FCSERR)) {
  530. dev->stats.rx_errors++;
  531. if (frame_status & RS_OFLO) {
  532. printk(KERN_DEBUG "%s: fifo overflow.\n", dev->name);
  533. dev->stats.rx_fifo_errors++;
  534. }
  535. if (frame_status & RS_CLSN)
  536. dev->stats.collisions++;
  537. if (frame_status & RS_FRAMERR)
  538. dev->stats.rx_frame_errors++;
  539. if (frame_status & RS_FCSERR)
  540. dev->stats.rx_crc_errors++;
  541. } else {
  542. unsigned int frame_length = mf->rcvcnt + ((frame_status & 0x0F) << 8 );
  543. skb = dev_alloc_skb(frame_length + 2);
  544. if (!skb) {
  545. dev->stats.rx_dropped++;
  546. return;
  547. }
  548. skb_reserve(skb, 2);
  549. memcpy(skb_put(skb, frame_length), mf->data, frame_length);
  550. skb->protocol = eth_type_trans(skb, dev);
  551. netif_rx(skb);
  552. dev->last_rx = jiffies;
  553. dev->stats.rx_packets++;
  554. dev->stats.rx_bytes += frame_length;
  555. }
  556. }
  557. /*
  558. * The PSC has passed us a DMA interrupt event.
  559. */
  560. static irqreturn_t mace_dma_intr(int irq, void *dev_id)
  561. {
  562. struct net_device *dev = (struct net_device *) dev_id;
  563. struct mace_data *mp = netdev_priv(dev);
  564. int left, head;
  565. u16 status;
  566. u32 baka;
  567. /* Not sure what this does */
  568. while ((baka = psc_read_long(PSC_MYSTERY)) != psc_read_long(PSC_MYSTERY));
  569. if (!(baka & 0x60000000)) return IRQ_NONE;
  570. /*
  571. * Process the read queue
  572. */
  573. status = psc_read_word(PSC_ENETRD_CTL);
  574. if (status & 0x2000) {
  575. mace_rxdma_reset(dev);
  576. } else if (status & 0x0100) {
  577. psc_write_word(PSC_ENETRD_CMD + mp->rx_slot, 0x1100);
  578. left = psc_read_long(PSC_ENETRD_LEN + mp->rx_slot);
  579. head = N_RX_RING - left;
  580. /* Loop through the ring buffer and process new packages */
  581. while (mp->rx_tail < head) {
  582. mace_dma_rx_frame(dev, (struct mace_frame*) (mp->rx_ring
  583. + (mp->rx_tail * MACE_BUFF_SIZE)));
  584. mp->rx_tail++;
  585. }
  586. /* If we're out of buffers in this ring then switch to */
  587. /* the other set, otherwise just reactivate this one. */
  588. if (!left) {
  589. mace_load_rxdma_base(dev, mp->rx_slot);
  590. mp->rx_slot ^= 0x10;
  591. } else {
  592. psc_write_word(PSC_ENETRD_CMD + mp->rx_slot, 0x9800);
  593. }
  594. }
  595. /*
  596. * Process the write queue
  597. */
  598. status = psc_read_word(PSC_ENETWR_CTL);
  599. if (status & 0x2000) {
  600. mace_txdma_reset(dev);
  601. } else if (status & 0x0100) {
  602. psc_write_word(PSC_ENETWR_CMD + mp->tx_sloti, 0x0100);
  603. mp->tx_sloti ^= 0x10;
  604. mp->tx_count++;
  605. }
  606. return IRQ_HANDLED;
  607. }
  608. MODULE_LICENSE("GPL");
  609. MODULE_DESCRIPTION("Macintosh MACE ethernet driver");
  610. static int __devexit mac_mace_device_remove (struct platform_device *pdev)
  611. {
  612. struct net_device *dev = platform_get_drvdata(pdev);
  613. struct mace_data *mp = netdev_priv(dev);
  614. unregister_netdev(dev);
  615. free_irq(dev->irq, dev);
  616. free_irq(IRQ_MAC_MACE_DMA, dev);
  617. dma_free_coherent(mp->device, N_RX_RING * MACE_BUFF_SIZE,
  618. mp->rx_ring, mp->rx_ring_phys);
  619. dma_free_coherent(mp->device, N_TX_RING * MACE_BUFF_SIZE,
  620. mp->tx_ring, mp->tx_ring_phys);
  621. free_netdev(dev);
  622. return 0;
  623. }
  624. static struct platform_driver mac_mace_driver = {
  625. .probe = mace_probe,
  626. .remove = __devexit_p(mac_mace_device_remove),
  627. .driver = {
  628. .name = mac_mace_string,
  629. },
  630. };
  631. static int __init mac_mace_init_module(void)
  632. {
  633. int err;
  634. if ((err = platform_driver_register(&mac_mace_driver))) {
  635. printk(KERN_ERR "Driver registration failed\n");
  636. return err;
  637. }
  638. mac_mace_device = platform_device_alloc(mac_mace_string, 0);
  639. if (!mac_mace_device)
  640. goto out_unregister;
  641. if (platform_device_add(mac_mace_device)) {
  642. platform_device_put(mac_mace_device);
  643. mac_mace_device = NULL;
  644. }
  645. return 0;
  646. out_unregister:
  647. platform_driver_unregister(&mac_mace_driver);
  648. return -ENOMEM;
  649. }
  650. static void __exit mac_mace_cleanup_module(void)
  651. {
  652. platform_driver_unregister(&mac_mace_driver);
  653. if (mac_mace_device) {
  654. platform_device_unregister(mac_mace_device);
  655. mac_mace_device = NULL;
  656. }
  657. }
  658. module_init(mac_mace_init_module);
  659. module_exit(mac_mace_cleanup_module);