mace.c 27 KB

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