mace.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023
  1. /*
  2. * Network device driver for the MACE ethernet controller on
  3. * Apple Powermacs. Assumes it's under a DBDMA controller.
  4. *
  5. * Copyright (C) 1996 Paul Mackerras.
  6. */
  7. #include <linux/module.h>
  8. #include <linux/kernel.h>
  9. #include <linux/netdevice.h>
  10. #include <linux/etherdevice.h>
  11. #include <linux/delay.h>
  12. #include <linux/string.h>
  13. #include <linux/timer.h>
  14. #include <linux/init.h>
  15. #include <linux/crc32.h>
  16. #include <linux/spinlock.h>
  17. #include <linux/bitrev.h>
  18. #include <asm/prom.h>
  19. #include <asm/dbdma.h>
  20. #include <asm/io.h>
  21. #include <asm/pgtable.h>
  22. #include <asm/macio.h>
  23. #include "mace.h"
  24. static int port_aaui = -1;
  25. #define N_RX_RING 8
  26. #define N_TX_RING 6
  27. #define MAX_TX_ACTIVE 1
  28. #define NCMDS_TX 1 /* dma commands per element in tx ring */
  29. #define RX_BUFLEN (ETH_FRAME_LEN + 8)
  30. #define TX_TIMEOUT HZ /* 1 second */
  31. /* Chip rev needs workaround on HW & multicast addr change */
  32. #define BROKEN_ADDRCHG_REV 0x0941
  33. /* Bits in transmit DMA status */
  34. #define TX_DMA_ERR 0x80
  35. struct mace_data {
  36. volatile struct mace __iomem *mace;
  37. volatile struct dbdma_regs __iomem *tx_dma;
  38. int tx_dma_intr;
  39. volatile struct dbdma_regs __iomem *rx_dma;
  40. int rx_dma_intr;
  41. volatile struct dbdma_cmd *tx_cmds; /* xmit dma command list */
  42. volatile struct dbdma_cmd *rx_cmds; /* recv dma command list */
  43. struct sk_buff *rx_bufs[N_RX_RING];
  44. int rx_fill;
  45. int rx_empty;
  46. struct sk_buff *tx_bufs[N_TX_RING];
  47. int tx_fill;
  48. int tx_empty;
  49. unsigned char maccc;
  50. unsigned char tx_fullup;
  51. unsigned char tx_active;
  52. unsigned char tx_bad_runt;
  53. struct timer_list tx_timeout;
  54. int timeout_active;
  55. int port_aaui;
  56. int chipid;
  57. struct macio_dev *mdev;
  58. spinlock_t lock;
  59. };
  60. /*
  61. * Number of bytes of private data per MACE: allow enough for
  62. * the rx and tx dma commands plus a branch dma command each,
  63. * and another 16 bytes to allow us to align the dma command
  64. * buffers on a 16 byte boundary.
  65. */
  66. #define PRIV_BYTES (sizeof(struct mace_data) \
  67. + (N_RX_RING + NCMDS_TX * N_TX_RING + 3) * sizeof(struct dbdma_cmd))
  68. static int mace_open(struct net_device *dev);
  69. static int mace_close(struct net_device *dev);
  70. static int mace_xmit_start(struct sk_buff *skb, struct net_device *dev);
  71. static void mace_set_multicast(struct net_device *dev);
  72. static void mace_reset(struct net_device *dev);
  73. static int mace_set_address(struct net_device *dev, void *addr);
  74. static irqreturn_t mace_interrupt(int irq, void *dev_id);
  75. static irqreturn_t mace_txdma_intr(int irq, void *dev_id);
  76. static irqreturn_t mace_rxdma_intr(int irq, void *dev_id);
  77. static void mace_set_timeout(struct net_device *dev);
  78. static void mace_tx_timeout(unsigned long data);
  79. static inline void dbdma_reset(volatile struct dbdma_regs __iomem *dma);
  80. static inline void mace_clean_rings(struct mace_data *mp);
  81. static void __mace_set_address(struct net_device *dev, void *addr);
  82. /*
  83. * If we can't get a skbuff when we need it, we use this area for DMA.
  84. */
  85. static unsigned char *dummy_buf;
  86. static int __devinit mace_probe(struct macio_dev *mdev, const struct of_device_id *match)
  87. {
  88. struct device_node *mace = macio_get_of_node(mdev);
  89. struct net_device *dev;
  90. struct mace_data *mp;
  91. const unsigned char *addr;
  92. int j, rev, rc = -EBUSY;
  93. DECLARE_MAC_BUF(mac);
  94. if (macio_resource_count(mdev) != 3 || macio_irq_count(mdev) != 3) {
  95. printk(KERN_ERR "can't use MACE %s: need 3 addrs and 3 irqs\n",
  96. mace->full_name);
  97. return -ENODEV;
  98. }
  99. addr = of_get_property(mace, "mac-address", NULL);
  100. if (addr == NULL) {
  101. addr = of_get_property(mace, "local-mac-address", NULL);
  102. if (addr == NULL) {
  103. printk(KERN_ERR "Can't get mac-address for MACE %s\n",
  104. mace->full_name);
  105. return -ENODEV;
  106. }
  107. }
  108. /*
  109. * lazy allocate the driver-wide dummy buffer. (Note that we
  110. * never have more than one MACE in the system anyway)
  111. */
  112. if (dummy_buf == NULL) {
  113. dummy_buf = kmalloc(RX_BUFLEN+2, GFP_KERNEL);
  114. if (dummy_buf == NULL) {
  115. printk(KERN_ERR "MACE: couldn't allocate dummy buffer\n");
  116. return -ENOMEM;
  117. }
  118. }
  119. if (macio_request_resources(mdev, "mace")) {
  120. printk(KERN_ERR "MACE: can't request IO resources !\n");
  121. return -EBUSY;
  122. }
  123. dev = alloc_etherdev(PRIV_BYTES);
  124. if (!dev) {
  125. printk(KERN_ERR "MACE: can't allocate ethernet device !\n");
  126. rc = -ENOMEM;
  127. goto err_release;
  128. }
  129. SET_NETDEV_DEV(dev, &mdev->ofdev.dev);
  130. mp = dev->priv;
  131. mp->mdev = mdev;
  132. macio_set_drvdata(mdev, dev);
  133. dev->base_addr = macio_resource_start(mdev, 0);
  134. mp->mace = ioremap(dev->base_addr, 0x1000);
  135. if (mp->mace == NULL) {
  136. printk(KERN_ERR "MACE: can't map IO resources !\n");
  137. rc = -ENOMEM;
  138. goto err_free;
  139. }
  140. dev->irq = macio_irq(mdev, 0);
  141. rev = addr[0] == 0 && addr[1] == 0xA0;
  142. for (j = 0; j < 6; ++j) {
  143. dev->dev_addr[j] = rev ? bitrev8(addr[j]): addr[j];
  144. }
  145. mp->chipid = (in_8(&mp->mace->chipid_hi) << 8) |
  146. in_8(&mp->mace->chipid_lo);
  147. mp = (struct mace_data *) dev->priv;
  148. mp->maccc = ENXMT | ENRCV;
  149. mp->tx_dma = ioremap(macio_resource_start(mdev, 1), 0x1000);
  150. if (mp->tx_dma == NULL) {
  151. printk(KERN_ERR "MACE: can't map TX DMA resources !\n");
  152. rc = -ENOMEM;
  153. goto err_unmap_io;
  154. }
  155. mp->tx_dma_intr = macio_irq(mdev, 1);
  156. mp->rx_dma = ioremap(macio_resource_start(mdev, 2), 0x1000);
  157. if (mp->rx_dma == NULL) {
  158. printk(KERN_ERR "MACE: can't map RX DMA resources !\n");
  159. rc = -ENOMEM;
  160. goto err_unmap_tx_dma;
  161. }
  162. mp->rx_dma_intr = macio_irq(mdev, 2);
  163. mp->tx_cmds = (volatile struct dbdma_cmd *) DBDMA_ALIGN(mp + 1);
  164. mp->rx_cmds = mp->tx_cmds + NCMDS_TX * N_TX_RING + 1;
  165. memset((char *) mp->tx_cmds, 0,
  166. (NCMDS_TX*N_TX_RING + N_RX_RING + 2) * sizeof(struct dbdma_cmd));
  167. init_timer(&mp->tx_timeout);
  168. spin_lock_init(&mp->lock);
  169. mp->timeout_active = 0;
  170. if (port_aaui >= 0)
  171. mp->port_aaui = port_aaui;
  172. else {
  173. /* Apple Network Server uses the AAUI port */
  174. if (machine_is_compatible("AAPL,ShinerESB"))
  175. mp->port_aaui = 1;
  176. else {
  177. #ifdef CONFIG_MACE_AAUI_PORT
  178. mp->port_aaui = 1;
  179. #else
  180. mp->port_aaui = 0;
  181. #endif
  182. }
  183. }
  184. dev->open = mace_open;
  185. dev->stop = mace_close;
  186. dev->hard_start_xmit = mace_xmit_start;
  187. dev->set_multicast_list = mace_set_multicast;
  188. dev->set_mac_address = mace_set_address;
  189. /*
  190. * Most of what is below could be moved to mace_open()
  191. */
  192. mace_reset(dev);
  193. rc = request_irq(dev->irq, mace_interrupt, 0, "MACE", dev);
  194. if (rc) {
  195. printk(KERN_ERR "MACE: can't get irq %d\n", dev->irq);
  196. goto err_unmap_rx_dma;
  197. }
  198. rc = request_irq(mp->tx_dma_intr, mace_txdma_intr, 0, "MACE-txdma", dev);
  199. if (rc) {
  200. printk(KERN_ERR "MACE: can't get irq %d\n", mp->tx_dma_intr);
  201. goto err_free_irq;
  202. }
  203. rc = request_irq(mp->rx_dma_intr, mace_rxdma_intr, 0, "MACE-rxdma", dev);
  204. if (rc) {
  205. printk(KERN_ERR "MACE: can't get irq %d\n", mp->rx_dma_intr);
  206. goto err_free_tx_irq;
  207. }
  208. rc = register_netdev(dev);
  209. if (rc) {
  210. printk(KERN_ERR "MACE: Cannot register net device, aborting.\n");
  211. goto err_free_rx_irq;
  212. }
  213. printk(KERN_INFO "%s: MACE at %s, chip revision %d.%d\n",
  214. dev->name, print_mac(mac, dev->dev_addr),
  215. mp->chipid >> 8, mp->chipid & 0xff);
  216. return 0;
  217. err_free_rx_irq:
  218. free_irq(macio_irq(mdev, 2), dev);
  219. err_free_tx_irq:
  220. free_irq(macio_irq(mdev, 1), dev);
  221. err_free_irq:
  222. free_irq(macio_irq(mdev, 0), dev);
  223. err_unmap_rx_dma:
  224. iounmap(mp->rx_dma);
  225. err_unmap_tx_dma:
  226. iounmap(mp->tx_dma);
  227. err_unmap_io:
  228. iounmap(mp->mace);
  229. err_free:
  230. free_netdev(dev);
  231. err_release:
  232. macio_release_resources(mdev);
  233. return rc;
  234. }
  235. static int __devexit mace_remove(struct macio_dev *mdev)
  236. {
  237. struct net_device *dev = macio_get_drvdata(mdev);
  238. struct mace_data *mp;
  239. BUG_ON(dev == NULL);
  240. macio_set_drvdata(mdev, NULL);
  241. mp = dev->priv;
  242. unregister_netdev(dev);
  243. free_irq(dev->irq, dev);
  244. free_irq(mp->tx_dma_intr, dev);
  245. free_irq(mp->rx_dma_intr, dev);
  246. iounmap(mp->rx_dma);
  247. iounmap(mp->tx_dma);
  248. iounmap(mp->mace);
  249. free_netdev(dev);
  250. macio_release_resources(mdev);
  251. return 0;
  252. }
  253. static void dbdma_reset(volatile struct dbdma_regs __iomem *dma)
  254. {
  255. int i;
  256. out_le32(&dma->control, (WAKE|FLUSH|PAUSE|RUN) << 16);
  257. /*
  258. * Yes this looks peculiar, but apparently it needs to be this
  259. * way on some machines.
  260. */
  261. for (i = 200; i > 0; --i)
  262. if (ld_le32(&dma->control) & RUN)
  263. udelay(1);
  264. }
  265. static void mace_reset(struct net_device *dev)
  266. {
  267. struct mace_data *mp = (struct mace_data *) dev->priv;
  268. volatile struct mace __iomem *mb = mp->mace;
  269. int i;
  270. /* soft-reset the chip */
  271. i = 200;
  272. while (--i) {
  273. out_8(&mb->biucc, SWRST);
  274. if (in_8(&mb->biucc) & SWRST) {
  275. udelay(10);
  276. continue;
  277. }
  278. break;
  279. }
  280. if (!i) {
  281. printk(KERN_ERR "mace: cannot reset chip!\n");
  282. return;
  283. }
  284. out_8(&mb->imr, 0xff); /* disable all intrs for now */
  285. i = in_8(&mb->ir);
  286. out_8(&mb->maccc, 0); /* turn off tx, rx */
  287. out_8(&mb->biucc, XMTSP_64);
  288. out_8(&mb->utr, RTRD);
  289. out_8(&mb->fifocc, RCVFW_32 | XMTFW_16 | XMTFWU | RCVFWU | XMTBRST);
  290. out_8(&mb->xmtfc, AUTO_PAD_XMIT); /* auto-pad short frames */
  291. out_8(&mb->rcvfc, 0);
  292. /* load up the hardware address */
  293. __mace_set_address(dev, dev->dev_addr);
  294. /* clear the multicast filter */
  295. if (mp->chipid == BROKEN_ADDRCHG_REV)
  296. out_8(&mb->iac, LOGADDR);
  297. else {
  298. out_8(&mb->iac, ADDRCHG | LOGADDR);
  299. while ((in_8(&mb->iac) & ADDRCHG) != 0)
  300. ;
  301. }
  302. for (i = 0; i < 8; ++i)
  303. out_8(&mb->ladrf, 0);
  304. /* done changing address */
  305. if (mp->chipid != BROKEN_ADDRCHG_REV)
  306. out_8(&mb->iac, 0);
  307. if (mp->port_aaui)
  308. out_8(&mb->plscc, PORTSEL_AUI + ENPLSIO);
  309. else
  310. out_8(&mb->plscc, PORTSEL_GPSI + ENPLSIO);
  311. }
  312. static void __mace_set_address(struct net_device *dev, void *addr)
  313. {
  314. struct mace_data *mp = (struct mace_data *) dev->priv;
  315. volatile struct mace __iomem *mb = mp->mace;
  316. unsigned char *p = addr;
  317. int i;
  318. /* load up the hardware address */
  319. if (mp->chipid == BROKEN_ADDRCHG_REV)
  320. out_8(&mb->iac, PHYADDR);
  321. else {
  322. out_8(&mb->iac, ADDRCHG | PHYADDR);
  323. while ((in_8(&mb->iac) & ADDRCHG) != 0)
  324. ;
  325. }
  326. for (i = 0; i < 6; ++i)
  327. out_8(&mb->padr, dev->dev_addr[i] = p[i]);
  328. if (mp->chipid != BROKEN_ADDRCHG_REV)
  329. out_8(&mb->iac, 0);
  330. }
  331. static int mace_set_address(struct net_device *dev, void *addr)
  332. {
  333. struct mace_data *mp = (struct mace_data *) dev->priv;
  334. volatile struct mace __iomem *mb = mp->mace;
  335. unsigned long flags;
  336. spin_lock_irqsave(&mp->lock, flags);
  337. __mace_set_address(dev, addr);
  338. /* note: setting ADDRCHG clears ENRCV */
  339. out_8(&mb->maccc, mp->maccc);
  340. spin_unlock_irqrestore(&mp->lock, flags);
  341. return 0;
  342. }
  343. static inline void mace_clean_rings(struct mace_data *mp)
  344. {
  345. int i;
  346. /* free some skb's */
  347. for (i = 0; i < N_RX_RING; ++i) {
  348. if (mp->rx_bufs[i] != 0) {
  349. dev_kfree_skb(mp->rx_bufs[i]);
  350. mp->rx_bufs[i] = NULL;
  351. }
  352. }
  353. for (i = mp->tx_empty; i != mp->tx_fill; ) {
  354. dev_kfree_skb(mp->tx_bufs[i]);
  355. if (++i >= N_TX_RING)
  356. i = 0;
  357. }
  358. }
  359. static int mace_open(struct net_device *dev)
  360. {
  361. struct mace_data *mp = (struct mace_data *) dev->priv;
  362. volatile struct mace __iomem *mb = mp->mace;
  363. volatile struct dbdma_regs __iomem *rd = mp->rx_dma;
  364. volatile struct dbdma_regs __iomem *td = mp->tx_dma;
  365. volatile struct dbdma_cmd *cp;
  366. int i;
  367. struct sk_buff *skb;
  368. unsigned char *data;
  369. /* reset the chip */
  370. mace_reset(dev);
  371. /* initialize list of sk_buffs for receiving and set up recv dma */
  372. mace_clean_rings(mp);
  373. memset((char *)mp->rx_cmds, 0, N_RX_RING * sizeof(struct dbdma_cmd));
  374. cp = mp->rx_cmds;
  375. for (i = 0; i < N_RX_RING - 1; ++i) {
  376. skb = dev_alloc_skb(RX_BUFLEN + 2);
  377. if (skb == 0) {
  378. data = dummy_buf;
  379. } else {
  380. skb_reserve(skb, 2); /* so IP header lands on 4-byte bdry */
  381. data = skb->data;
  382. }
  383. mp->rx_bufs[i] = skb;
  384. st_le16(&cp->req_count, RX_BUFLEN);
  385. st_le16(&cp->command, INPUT_LAST + INTR_ALWAYS);
  386. st_le32(&cp->phy_addr, virt_to_bus(data));
  387. cp->xfer_status = 0;
  388. ++cp;
  389. }
  390. mp->rx_bufs[i] = NULL;
  391. st_le16(&cp->command, DBDMA_STOP);
  392. mp->rx_fill = i;
  393. mp->rx_empty = 0;
  394. /* Put a branch back to the beginning of the receive command list */
  395. ++cp;
  396. st_le16(&cp->command, DBDMA_NOP + BR_ALWAYS);
  397. st_le32(&cp->cmd_dep, virt_to_bus(mp->rx_cmds));
  398. /* start rx dma */
  399. out_le32(&rd->control, (RUN|PAUSE|FLUSH|WAKE) << 16); /* clear run bit */
  400. out_le32(&rd->cmdptr, virt_to_bus(mp->rx_cmds));
  401. out_le32(&rd->control, (RUN << 16) | RUN);
  402. /* put a branch at the end of the tx command list */
  403. cp = mp->tx_cmds + NCMDS_TX * N_TX_RING;
  404. st_le16(&cp->command, DBDMA_NOP + BR_ALWAYS);
  405. st_le32(&cp->cmd_dep, virt_to_bus(mp->tx_cmds));
  406. /* reset tx dma */
  407. out_le32(&td->control, (RUN|PAUSE|FLUSH|WAKE) << 16);
  408. out_le32(&td->cmdptr, virt_to_bus(mp->tx_cmds));
  409. mp->tx_fill = 0;
  410. mp->tx_empty = 0;
  411. mp->tx_fullup = 0;
  412. mp->tx_active = 0;
  413. mp->tx_bad_runt = 0;
  414. /* turn it on! */
  415. out_8(&mb->maccc, mp->maccc);
  416. /* enable all interrupts except receive interrupts */
  417. out_8(&mb->imr, RCVINT);
  418. return 0;
  419. }
  420. static int mace_close(struct net_device *dev)
  421. {
  422. struct mace_data *mp = (struct mace_data *) dev->priv;
  423. volatile struct mace __iomem *mb = mp->mace;
  424. volatile struct dbdma_regs __iomem *rd = mp->rx_dma;
  425. volatile struct dbdma_regs __iomem *td = mp->tx_dma;
  426. /* disable rx and tx */
  427. out_8(&mb->maccc, 0);
  428. out_8(&mb->imr, 0xff); /* disable all intrs */
  429. /* disable rx and tx dma */
  430. st_le32(&rd->control, (RUN|PAUSE|FLUSH|WAKE) << 16); /* clear run bit */
  431. st_le32(&td->control, (RUN|PAUSE|FLUSH|WAKE) << 16); /* clear run bit */
  432. mace_clean_rings(mp);
  433. return 0;
  434. }
  435. static inline void mace_set_timeout(struct net_device *dev)
  436. {
  437. struct mace_data *mp = (struct mace_data *) dev->priv;
  438. if (mp->timeout_active)
  439. del_timer(&mp->tx_timeout);
  440. mp->tx_timeout.expires = jiffies + TX_TIMEOUT;
  441. mp->tx_timeout.function = mace_tx_timeout;
  442. mp->tx_timeout.data = (unsigned long) dev;
  443. add_timer(&mp->tx_timeout);
  444. mp->timeout_active = 1;
  445. }
  446. static int mace_xmit_start(struct sk_buff *skb, struct net_device *dev)
  447. {
  448. struct mace_data *mp = (struct mace_data *) dev->priv;
  449. volatile struct dbdma_regs __iomem *td = mp->tx_dma;
  450. volatile struct dbdma_cmd *cp, *np;
  451. unsigned long flags;
  452. int fill, next, len;
  453. /* see if there's a free slot in the tx ring */
  454. spin_lock_irqsave(&mp->lock, flags);
  455. fill = mp->tx_fill;
  456. next = fill + 1;
  457. if (next >= N_TX_RING)
  458. next = 0;
  459. if (next == mp->tx_empty) {
  460. netif_stop_queue(dev);
  461. mp->tx_fullup = 1;
  462. spin_unlock_irqrestore(&mp->lock, flags);
  463. return 1; /* can't take it at the moment */
  464. }
  465. spin_unlock_irqrestore(&mp->lock, flags);
  466. /* partially fill in the dma command block */
  467. len = skb->len;
  468. if (len > ETH_FRAME_LEN) {
  469. printk(KERN_DEBUG "mace: xmit frame too long (%d)\n", len);
  470. len = ETH_FRAME_LEN;
  471. }
  472. mp->tx_bufs[fill] = skb;
  473. cp = mp->tx_cmds + NCMDS_TX * fill;
  474. st_le16(&cp->req_count, len);
  475. st_le32(&cp->phy_addr, virt_to_bus(skb->data));
  476. np = mp->tx_cmds + NCMDS_TX * next;
  477. out_le16(&np->command, DBDMA_STOP);
  478. /* poke the tx dma channel */
  479. spin_lock_irqsave(&mp->lock, flags);
  480. mp->tx_fill = next;
  481. if (!mp->tx_bad_runt && mp->tx_active < MAX_TX_ACTIVE) {
  482. out_le16(&cp->xfer_status, 0);
  483. out_le16(&cp->command, OUTPUT_LAST);
  484. out_le32(&td->control, ((RUN|WAKE) << 16) + (RUN|WAKE));
  485. ++mp->tx_active;
  486. mace_set_timeout(dev);
  487. }
  488. if (++next >= N_TX_RING)
  489. next = 0;
  490. if (next == mp->tx_empty)
  491. netif_stop_queue(dev);
  492. spin_unlock_irqrestore(&mp->lock, flags);
  493. return 0;
  494. }
  495. static void mace_set_multicast(struct net_device *dev)
  496. {
  497. struct mace_data *mp = (struct mace_data *) dev->priv;
  498. volatile struct mace __iomem *mb = mp->mace;
  499. int i, j;
  500. u32 crc;
  501. unsigned long flags;
  502. spin_lock_irqsave(&mp->lock, flags);
  503. mp->maccc &= ~PROM;
  504. if (dev->flags & IFF_PROMISC) {
  505. mp->maccc |= PROM;
  506. } else {
  507. unsigned char multicast_filter[8];
  508. struct dev_mc_list *dmi = dev->mc_list;
  509. if (dev->flags & IFF_ALLMULTI) {
  510. for (i = 0; i < 8; i++)
  511. multicast_filter[i] = 0xff;
  512. } else {
  513. for (i = 0; i < 8; i++)
  514. multicast_filter[i] = 0;
  515. for (i = 0; i < dev->mc_count; i++) {
  516. crc = ether_crc_le(6, dmi->dmi_addr);
  517. j = crc >> 26; /* bit number in multicast_filter */
  518. multicast_filter[j >> 3] |= 1 << (j & 7);
  519. dmi = dmi->next;
  520. }
  521. }
  522. #if 0
  523. printk("Multicast filter :");
  524. for (i = 0; i < 8; i++)
  525. printk("%02x ", multicast_filter[i]);
  526. printk("\n");
  527. #endif
  528. if (mp->chipid == BROKEN_ADDRCHG_REV)
  529. out_8(&mb->iac, LOGADDR);
  530. else {
  531. out_8(&mb->iac, ADDRCHG | LOGADDR);
  532. while ((in_8(&mb->iac) & ADDRCHG) != 0)
  533. ;
  534. }
  535. for (i = 0; i < 8; ++i)
  536. out_8(&mb->ladrf, multicast_filter[i]);
  537. if (mp->chipid != BROKEN_ADDRCHG_REV)
  538. out_8(&mb->iac, 0);
  539. }
  540. /* reset maccc */
  541. out_8(&mb->maccc, mp->maccc);
  542. spin_unlock_irqrestore(&mp->lock, flags);
  543. }
  544. static void mace_handle_misc_intrs(struct mace_data *mp, int intr, struct net_device *dev)
  545. {
  546. volatile struct mace __iomem *mb = mp->mace;
  547. static int mace_babbles, mace_jabbers;
  548. if (intr & MPCO)
  549. dev->stats.rx_missed_errors += 256;
  550. dev->stats.rx_missed_errors += in_8(&mb->mpc); /* reading clears it */
  551. if (intr & RNTPCO)
  552. dev->stats.rx_length_errors += 256;
  553. dev->stats.rx_length_errors += in_8(&mb->rntpc); /* reading clears it */
  554. if (intr & CERR)
  555. ++dev->stats.tx_heartbeat_errors;
  556. if (intr & BABBLE)
  557. if (mace_babbles++ < 4)
  558. printk(KERN_DEBUG "mace: babbling transmitter\n");
  559. if (intr & JABBER)
  560. if (mace_jabbers++ < 4)
  561. printk(KERN_DEBUG "mace: jabbering transceiver\n");
  562. }
  563. static irqreturn_t mace_interrupt(int irq, void *dev_id)
  564. {
  565. struct net_device *dev = (struct net_device *) dev_id;
  566. struct mace_data *mp = (struct mace_data *) dev->priv;
  567. volatile struct mace __iomem *mb = mp->mace;
  568. volatile struct dbdma_regs __iomem *td = mp->tx_dma;
  569. volatile struct dbdma_cmd *cp;
  570. int intr, fs, i, stat, x;
  571. int xcount, dstat;
  572. unsigned long flags;
  573. /* static int mace_last_fs, mace_last_xcount; */
  574. spin_lock_irqsave(&mp->lock, flags);
  575. intr = in_8(&mb->ir); /* read interrupt register */
  576. in_8(&mb->xmtrc); /* get retries */
  577. mace_handle_misc_intrs(mp, intr, dev);
  578. i = mp->tx_empty;
  579. while (in_8(&mb->pr) & XMTSV) {
  580. del_timer(&mp->tx_timeout);
  581. mp->timeout_active = 0;
  582. /*
  583. * Clear any interrupt indication associated with this status
  584. * word. This appears to unlatch any error indication from
  585. * the DMA controller.
  586. */
  587. intr = in_8(&mb->ir);
  588. if (intr != 0)
  589. mace_handle_misc_intrs(mp, intr, dev);
  590. if (mp->tx_bad_runt) {
  591. fs = in_8(&mb->xmtfs);
  592. mp->tx_bad_runt = 0;
  593. out_8(&mb->xmtfc, AUTO_PAD_XMIT);
  594. continue;
  595. }
  596. dstat = ld_le32(&td->status);
  597. /* stop DMA controller */
  598. out_le32(&td->control, RUN << 16);
  599. /*
  600. * xcount is the number of complete frames which have been
  601. * written to the fifo but for which status has not been read.
  602. */
  603. xcount = (in_8(&mb->fifofc) >> XMTFC_SH) & XMTFC_MASK;
  604. if (xcount == 0 || (dstat & DEAD)) {
  605. /*
  606. * If a packet was aborted before the DMA controller has
  607. * finished transferring it, it seems that there are 2 bytes
  608. * which are stuck in some buffer somewhere. These will get
  609. * transmitted as soon as we read the frame status (which
  610. * reenables the transmit data transfer request). Turning
  611. * off the DMA controller and/or resetting the MACE doesn't
  612. * help. So we disable auto-padding and FCS transmission
  613. * so the two bytes will only be a runt packet which should
  614. * be ignored by other stations.
  615. */
  616. out_8(&mb->xmtfc, DXMTFCS);
  617. }
  618. fs = in_8(&mb->xmtfs);
  619. if ((fs & XMTSV) == 0) {
  620. printk(KERN_ERR "mace: xmtfs not valid! (fs=%x xc=%d ds=%x)\n",
  621. fs, xcount, dstat);
  622. mace_reset(dev);
  623. /*
  624. * XXX mace likes to hang the machine after a xmtfs error.
  625. * This is hard to reproduce, reseting *may* help
  626. */
  627. }
  628. cp = mp->tx_cmds + NCMDS_TX * i;
  629. stat = ld_le16(&cp->xfer_status);
  630. if ((fs & (UFLO|LCOL|LCAR|RTRY)) || (dstat & DEAD) || xcount == 0) {
  631. /*
  632. * Check whether there were in fact 2 bytes written to
  633. * the transmit FIFO.
  634. */
  635. udelay(1);
  636. x = (in_8(&mb->fifofc) >> XMTFC_SH) & XMTFC_MASK;
  637. if (x != 0) {
  638. /* there were two bytes with an end-of-packet indication */
  639. mp->tx_bad_runt = 1;
  640. mace_set_timeout(dev);
  641. } else {
  642. /*
  643. * Either there weren't the two bytes buffered up, or they
  644. * didn't have an end-of-packet indication.
  645. * We flush the transmit FIFO just in case (by setting the
  646. * XMTFWU bit with the transmitter disabled).
  647. */
  648. out_8(&mb->maccc, in_8(&mb->maccc) & ~ENXMT);
  649. out_8(&mb->fifocc, in_8(&mb->fifocc) | XMTFWU);
  650. udelay(1);
  651. out_8(&mb->maccc, in_8(&mb->maccc) | ENXMT);
  652. out_8(&mb->xmtfc, AUTO_PAD_XMIT);
  653. }
  654. }
  655. /* dma should have finished */
  656. if (i == mp->tx_fill) {
  657. printk(KERN_DEBUG "mace: tx ring ran out? (fs=%x xc=%d ds=%x)\n",
  658. fs, xcount, dstat);
  659. continue;
  660. }
  661. /* Update stats */
  662. if (fs & (UFLO|LCOL|LCAR|RTRY)) {
  663. ++dev->stats.tx_errors;
  664. if (fs & LCAR)
  665. ++dev->stats.tx_carrier_errors;
  666. if (fs & (UFLO|LCOL|RTRY))
  667. ++dev->stats.tx_aborted_errors;
  668. } else {
  669. dev->stats.tx_bytes += mp->tx_bufs[i]->len;
  670. ++dev->stats.tx_packets;
  671. }
  672. dev_kfree_skb_irq(mp->tx_bufs[i]);
  673. --mp->tx_active;
  674. if (++i >= N_TX_RING)
  675. i = 0;
  676. #if 0
  677. mace_last_fs = fs;
  678. mace_last_xcount = xcount;
  679. #endif
  680. }
  681. if (i != mp->tx_empty) {
  682. mp->tx_fullup = 0;
  683. netif_wake_queue(dev);
  684. }
  685. mp->tx_empty = i;
  686. i += mp->tx_active;
  687. if (i >= N_TX_RING)
  688. i -= N_TX_RING;
  689. if (!mp->tx_bad_runt && i != mp->tx_fill && mp->tx_active < MAX_TX_ACTIVE) {
  690. do {
  691. /* set up the next one */
  692. cp = mp->tx_cmds + NCMDS_TX * i;
  693. out_le16(&cp->xfer_status, 0);
  694. out_le16(&cp->command, OUTPUT_LAST);
  695. ++mp->tx_active;
  696. if (++i >= N_TX_RING)
  697. i = 0;
  698. } while (i != mp->tx_fill && mp->tx_active < MAX_TX_ACTIVE);
  699. out_le32(&td->control, ((RUN|WAKE) << 16) + (RUN|WAKE));
  700. mace_set_timeout(dev);
  701. }
  702. spin_unlock_irqrestore(&mp->lock, flags);
  703. return IRQ_HANDLED;
  704. }
  705. static void mace_tx_timeout(unsigned long data)
  706. {
  707. struct net_device *dev = (struct net_device *) data;
  708. struct mace_data *mp = (struct mace_data *) dev->priv;
  709. volatile struct mace __iomem *mb = mp->mace;
  710. volatile struct dbdma_regs __iomem *td = mp->tx_dma;
  711. volatile struct dbdma_regs __iomem *rd = mp->rx_dma;
  712. volatile struct dbdma_cmd *cp;
  713. unsigned long flags;
  714. int i;
  715. spin_lock_irqsave(&mp->lock, flags);
  716. mp->timeout_active = 0;
  717. if (mp->tx_active == 0 && !mp->tx_bad_runt)
  718. goto out;
  719. /* update various counters */
  720. mace_handle_misc_intrs(mp, in_8(&mb->ir), dev);
  721. cp = mp->tx_cmds + NCMDS_TX * mp->tx_empty;
  722. /* turn off both tx and rx and reset the chip */
  723. out_8(&mb->maccc, 0);
  724. printk(KERN_ERR "mace: transmit timeout - resetting\n");
  725. dbdma_reset(td);
  726. mace_reset(dev);
  727. /* restart rx dma */
  728. cp = bus_to_virt(ld_le32(&rd->cmdptr));
  729. dbdma_reset(rd);
  730. out_le16(&cp->xfer_status, 0);
  731. out_le32(&rd->cmdptr, virt_to_bus(cp));
  732. out_le32(&rd->control, (RUN << 16) | RUN);
  733. /* fix up the transmit side */
  734. i = mp->tx_empty;
  735. mp->tx_active = 0;
  736. ++dev->stats.tx_errors;
  737. if (mp->tx_bad_runt) {
  738. mp->tx_bad_runt = 0;
  739. } else if (i != mp->tx_fill) {
  740. dev_kfree_skb(mp->tx_bufs[i]);
  741. if (++i >= N_TX_RING)
  742. i = 0;
  743. mp->tx_empty = i;
  744. }
  745. mp->tx_fullup = 0;
  746. netif_wake_queue(dev);
  747. if (i != mp->tx_fill) {
  748. cp = mp->tx_cmds + NCMDS_TX * i;
  749. out_le16(&cp->xfer_status, 0);
  750. out_le16(&cp->command, OUTPUT_LAST);
  751. out_le32(&td->cmdptr, virt_to_bus(cp));
  752. out_le32(&td->control, (RUN << 16) | RUN);
  753. ++mp->tx_active;
  754. mace_set_timeout(dev);
  755. }
  756. /* turn it back on */
  757. out_8(&mb->imr, RCVINT);
  758. out_8(&mb->maccc, mp->maccc);
  759. out:
  760. spin_unlock_irqrestore(&mp->lock, flags);
  761. }
  762. static irqreturn_t mace_txdma_intr(int irq, void *dev_id)
  763. {
  764. return IRQ_HANDLED;
  765. }
  766. static irqreturn_t mace_rxdma_intr(int irq, void *dev_id)
  767. {
  768. struct net_device *dev = (struct net_device *) dev_id;
  769. struct mace_data *mp = (struct mace_data *) dev->priv;
  770. volatile struct dbdma_regs __iomem *rd = mp->rx_dma;
  771. volatile struct dbdma_cmd *cp, *np;
  772. int i, nb, stat, next;
  773. struct sk_buff *skb;
  774. unsigned frame_status;
  775. static int mace_lost_status;
  776. unsigned char *data;
  777. unsigned long flags;
  778. spin_lock_irqsave(&mp->lock, flags);
  779. for (i = mp->rx_empty; i != mp->rx_fill; ) {
  780. cp = mp->rx_cmds + i;
  781. stat = ld_le16(&cp->xfer_status);
  782. if ((stat & ACTIVE) == 0) {
  783. next = i + 1;
  784. if (next >= N_RX_RING)
  785. next = 0;
  786. np = mp->rx_cmds + next;
  787. if (next != mp->rx_fill
  788. && (ld_le16(&np->xfer_status) & ACTIVE) != 0) {
  789. printk(KERN_DEBUG "mace: lost a status word\n");
  790. ++mace_lost_status;
  791. } else
  792. break;
  793. }
  794. nb = ld_le16(&cp->req_count) - ld_le16(&cp->res_count);
  795. out_le16(&cp->command, DBDMA_STOP);
  796. /* got a packet, have a look at it */
  797. skb = mp->rx_bufs[i];
  798. if (skb == 0) {
  799. ++dev->stats.rx_dropped;
  800. } else if (nb > 8) {
  801. data = skb->data;
  802. frame_status = (data[nb-3] << 8) + data[nb-4];
  803. if (frame_status & (RS_OFLO|RS_CLSN|RS_FRAMERR|RS_FCSERR)) {
  804. ++dev->stats.rx_errors;
  805. if (frame_status & RS_OFLO)
  806. ++dev->stats.rx_over_errors;
  807. if (frame_status & RS_FRAMERR)
  808. ++dev->stats.rx_frame_errors;
  809. if (frame_status & RS_FCSERR)
  810. ++dev->stats.rx_crc_errors;
  811. } else {
  812. /* Mace feature AUTO_STRIP_RCV is on by default, dropping the
  813. * FCS on frames with 802.3 headers. This means that Ethernet
  814. * frames have 8 extra octets at the end, while 802.3 frames
  815. * have only 4. We need to correctly account for this. */
  816. if (*(unsigned short *)(data+12) < 1536) /* 802.3 header */
  817. nb -= 4;
  818. else /* Ethernet header; mace includes FCS */
  819. nb -= 8;
  820. skb_put(skb, nb);
  821. skb->protocol = eth_type_trans(skb, dev);
  822. dev->stats.rx_bytes += skb->len;
  823. netif_rx(skb);
  824. dev->last_rx = jiffies;
  825. mp->rx_bufs[i] = NULL;
  826. ++dev->stats.rx_packets;
  827. }
  828. } else {
  829. ++dev->stats.rx_errors;
  830. ++dev->stats.rx_length_errors;
  831. }
  832. /* advance to next */
  833. if (++i >= N_RX_RING)
  834. i = 0;
  835. }
  836. mp->rx_empty = i;
  837. i = mp->rx_fill;
  838. for (;;) {
  839. next = i + 1;
  840. if (next >= N_RX_RING)
  841. next = 0;
  842. if (next == mp->rx_empty)
  843. break;
  844. cp = mp->rx_cmds + i;
  845. skb = mp->rx_bufs[i];
  846. if (skb == 0) {
  847. skb = dev_alloc_skb(RX_BUFLEN + 2);
  848. if (skb != 0) {
  849. skb_reserve(skb, 2);
  850. mp->rx_bufs[i] = skb;
  851. }
  852. }
  853. st_le16(&cp->req_count, RX_BUFLEN);
  854. data = skb? skb->data: dummy_buf;
  855. st_le32(&cp->phy_addr, virt_to_bus(data));
  856. out_le16(&cp->xfer_status, 0);
  857. out_le16(&cp->command, INPUT_LAST + INTR_ALWAYS);
  858. #if 0
  859. if ((ld_le32(&rd->status) & ACTIVE) != 0) {
  860. out_le32(&rd->control, (PAUSE << 16) | PAUSE);
  861. while ((in_le32(&rd->status) & ACTIVE) != 0)
  862. ;
  863. }
  864. #endif
  865. i = next;
  866. }
  867. if (i != mp->rx_fill) {
  868. out_le32(&rd->control, ((RUN|WAKE) << 16) | (RUN|WAKE));
  869. mp->rx_fill = i;
  870. }
  871. spin_unlock_irqrestore(&mp->lock, flags);
  872. return IRQ_HANDLED;
  873. }
  874. static struct of_device_id mace_match[] =
  875. {
  876. {
  877. .name = "mace",
  878. },
  879. {},
  880. };
  881. MODULE_DEVICE_TABLE (of, mace_match);
  882. static struct macio_driver mace_driver =
  883. {
  884. .name = "mace",
  885. .match_table = mace_match,
  886. .probe = mace_probe,
  887. .remove = mace_remove,
  888. };
  889. static int __init mace_init(void)
  890. {
  891. return macio_register_driver(&mace_driver);
  892. }
  893. static void __exit mace_cleanup(void)
  894. {
  895. macio_unregister_driver(&mace_driver);
  896. kfree(dummy_buf);
  897. dummy_buf = NULL;
  898. }
  899. MODULE_AUTHOR("Paul Mackerras");
  900. MODULE_DESCRIPTION("PowerMac MACE driver.");
  901. module_param(port_aaui, int, 0);
  902. MODULE_PARM_DESC(port_aaui, "MACE uses AAUI port (0-1)");
  903. MODULE_LICENSE("GPL");
  904. module_init(mace_init);
  905. module_exit(mace_cleanup);