macmace.c 19 KB

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