2 * drivers/net/ethernet/ibm/emac/mal.c
4 * Memory Access Layer (MAL) support
6 * Copyright 2007 Benjamin Herrenschmidt, IBM Corp.
9 * Based on the arch/ppc version of the driver:
11 * Copyright (c) 2004, 2005 Zultys Technologies.
14 * Based on original work by
19 * Copyright 2002 MontaVista Softare Inc.
21 * This program is free software; you can redistribute it and/or modify it
22 * under the terms of the GNU General Public License as published by the
23 * Free Software Foundation; either version 2 of the License, or (at your
24 * option) any later version.
28 #include <linux/delay.h>
29 #include <linux/slab.h>
30 #include <linux/of_irq.h>
33 #include <asm/dcr-regs.h>
37 int mal_register_commac(struct mal_instance *mal, struct mal_commac *commac)
41 spin_lock_irqsave(&mal->lock, flags);
43 MAL_DBG(mal, "reg(%08x, %08x)" NL,
44 commac->tx_chan_mask, commac->rx_chan_mask);
46 /* Don't let multiple commacs claim the same channel(s) */
47 if ((mal->tx_chan_mask & commac->tx_chan_mask) ||
48 (mal->rx_chan_mask & commac->rx_chan_mask)) {
49 spin_unlock_irqrestore(&mal->lock, flags);
50 printk(KERN_WARNING "mal%d: COMMAC channels conflict!\n",
55 if (list_empty(&mal->list))
56 napi_enable(&mal->napi);
57 mal->tx_chan_mask |= commac->tx_chan_mask;
58 mal->rx_chan_mask |= commac->rx_chan_mask;
59 list_add(&commac->list, &mal->list);
61 spin_unlock_irqrestore(&mal->lock, flags);
66 void mal_unregister_commac(struct mal_instance *mal,
67 struct mal_commac *commac)
71 spin_lock_irqsave(&mal->lock, flags);
73 MAL_DBG(mal, "unreg(%08x, %08x)" NL,
74 commac->tx_chan_mask, commac->rx_chan_mask);
76 mal->tx_chan_mask &= ~commac->tx_chan_mask;
77 mal->rx_chan_mask &= ~commac->rx_chan_mask;
78 list_del_init(&commac->list);
79 if (list_empty(&mal->list))
80 napi_disable(&mal->napi);
82 spin_unlock_irqrestore(&mal->lock, flags);
85 int mal_set_rcbs(struct mal_instance *mal, int channel, unsigned long size)
87 BUG_ON(channel < 0 || channel >= mal->num_rx_chans ||
88 size > MAL_MAX_RX_SIZE);
90 MAL_DBG(mal, "set_rbcs(%d, %lu)" NL, channel, size);
94 "mal%d: incorrect RX size %lu for the channel %d\n",
95 mal->index, size, channel);
99 set_mal_dcrn(mal, MAL_RCBS(channel), size >> 4);
103 int mal_tx_bd_offset(struct mal_instance *mal, int channel)
105 BUG_ON(channel < 0 || channel >= mal->num_tx_chans);
107 return channel * NUM_TX_BUFF;
110 int mal_rx_bd_offset(struct mal_instance *mal, int channel)
112 BUG_ON(channel < 0 || channel >= mal->num_rx_chans);
113 return mal->num_tx_chans * NUM_TX_BUFF + channel * NUM_RX_BUFF;
116 void mal_enable_tx_channel(struct mal_instance *mal, int channel)
120 spin_lock_irqsave(&mal->lock, flags);
122 MAL_DBG(mal, "enable_tx(%d)" NL, channel);
124 set_mal_dcrn(mal, MAL_TXCASR,
125 get_mal_dcrn(mal, MAL_TXCASR) | MAL_CHAN_MASK(channel));
127 spin_unlock_irqrestore(&mal->lock, flags);
130 void mal_disable_tx_channel(struct mal_instance *mal, int channel)
132 set_mal_dcrn(mal, MAL_TXCARR, MAL_CHAN_MASK(channel));
134 MAL_DBG(mal, "disable_tx(%d)" NL, channel);
137 void mal_enable_rx_channel(struct mal_instance *mal, int channel)
142 * On some 4xx PPC's (e.g. 460EX/GT), the rx channel is a multiple
143 * of 8, but enabling in MAL_RXCASR needs the divided by 8 value
149 spin_lock_irqsave(&mal->lock, flags);
151 MAL_DBG(mal, "enable_rx(%d)" NL, channel);
153 set_mal_dcrn(mal, MAL_RXCASR,
154 get_mal_dcrn(mal, MAL_RXCASR) | MAL_CHAN_MASK(channel));
156 spin_unlock_irqrestore(&mal->lock, flags);
159 void mal_disable_rx_channel(struct mal_instance *mal, int channel)
162 * On some 4xx PPC's (e.g. 460EX/GT), the rx channel is a multiple
163 * of 8, but enabling in MAL_RXCASR needs the divided by 8 value
169 set_mal_dcrn(mal, MAL_RXCARR, MAL_CHAN_MASK(channel));
171 MAL_DBG(mal, "disable_rx(%d)" NL, channel);
174 void mal_poll_add(struct mal_instance *mal, struct mal_commac *commac)
178 spin_lock_irqsave(&mal->lock, flags);
180 MAL_DBG(mal, "poll_add(%p)" NL, commac);
182 /* starts disabled */
183 set_bit(MAL_COMMAC_POLL_DISABLED, &commac->flags);
185 list_add_tail(&commac->poll_list, &mal->poll_list);
187 spin_unlock_irqrestore(&mal->lock, flags);
190 void mal_poll_del(struct mal_instance *mal, struct mal_commac *commac)
194 spin_lock_irqsave(&mal->lock, flags);
196 MAL_DBG(mal, "poll_del(%p)" NL, commac);
198 list_del(&commac->poll_list);
200 spin_unlock_irqrestore(&mal->lock, flags);
203 /* synchronized by mal_poll() */
204 static inline void mal_enable_eob_irq(struct mal_instance *mal)
206 MAL_DBG2(mal, "enable_irq" NL);
208 // XXX might want to cache MAL_CFG as the DCR read can be slooooow
209 set_mal_dcrn(mal, MAL_CFG, get_mal_dcrn(mal, MAL_CFG) | MAL_CFG_EOPIE);
212 /* synchronized by NAPI state */
213 static inline void mal_disable_eob_irq(struct mal_instance *mal)
215 // XXX might want to cache MAL_CFG as the DCR read can be slooooow
216 set_mal_dcrn(mal, MAL_CFG, get_mal_dcrn(mal, MAL_CFG) & ~MAL_CFG_EOPIE);
218 MAL_DBG2(mal, "disable_irq" NL);
221 static irqreturn_t mal_serr(int irq, void *dev_instance)
223 struct mal_instance *mal = dev_instance;
225 u32 esr = get_mal_dcrn(mal, MAL_ESR);
227 /* Clear the error status register */
228 set_mal_dcrn(mal, MAL_ESR, esr);
230 MAL_DBG(mal, "SERR %08x" NL, esr);
232 if (esr & MAL_ESR_EVB) {
233 if (esr & MAL_ESR_DE) {
234 /* We ignore Descriptor error,
235 * TXDE or RXDE interrupt will be generated anyway.
240 if (esr & MAL_ESR_PEIN) {
241 /* PLB error, it's probably buggy hardware or
242 * incorrect physical address in BD (i.e. bug)
246 "mal%d: system error, "
247 "PLB (ESR = 0x%08x)\n",
252 /* OPB error, it's probably buggy hardware or incorrect
257 "mal%d: system error, OPB (ESR = 0x%08x)\n",
263 static inline void mal_schedule_poll(struct mal_instance *mal)
265 if (likely(napi_schedule_prep(&mal->napi))) {
266 MAL_DBG2(mal, "schedule_poll" NL);
267 spin_lock(&mal->lock);
268 mal_disable_eob_irq(mal);
269 spin_unlock(&mal->lock);
270 __napi_schedule(&mal->napi);
272 MAL_DBG2(mal, "already in poll" NL);
275 static irqreturn_t mal_txeob(int irq, void *dev_instance)
277 struct mal_instance *mal = dev_instance;
279 u32 r = get_mal_dcrn(mal, MAL_TXEOBISR);
281 MAL_DBG2(mal, "txeob %08x" NL, r);
283 mal_schedule_poll(mal);
284 set_mal_dcrn(mal, MAL_TXEOBISR, r);
286 #ifdef CONFIG_PPC_DCR_NATIVE
287 if (mal_has_feature(mal, MAL_FTR_CLEAR_ICINTSTAT))
288 mtdcri(SDR0, DCRN_SDR_ICINTSTAT,
289 (mfdcri(SDR0, DCRN_SDR_ICINTSTAT) | ICINTSTAT_ICTX));
295 static irqreturn_t mal_rxeob(int irq, void *dev_instance)
297 struct mal_instance *mal = dev_instance;
299 u32 r = get_mal_dcrn(mal, MAL_RXEOBISR);
301 MAL_DBG2(mal, "rxeob %08x" NL, r);
303 mal_schedule_poll(mal);
304 set_mal_dcrn(mal, MAL_RXEOBISR, r);
306 #ifdef CONFIG_PPC_DCR_NATIVE
307 if (mal_has_feature(mal, MAL_FTR_CLEAR_ICINTSTAT))
308 mtdcri(SDR0, DCRN_SDR_ICINTSTAT,
309 (mfdcri(SDR0, DCRN_SDR_ICINTSTAT) | ICINTSTAT_ICRX));
315 static irqreturn_t mal_txde(int irq, void *dev_instance)
317 struct mal_instance *mal = dev_instance;
319 u32 deir = get_mal_dcrn(mal, MAL_TXDEIR);
320 set_mal_dcrn(mal, MAL_TXDEIR, deir);
322 MAL_DBG(mal, "txde %08x" NL, deir);
326 "mal%d: TX descriptor error (TXDEIR = 0x%08x)\n",
332 static irqreturn_t mal_rxde(int irq, void *dev_instance)
334 struct mal_instance *mal = dev_instance;
337 u32 deir = get_mal_dcrn(mal, MAL_RXDEIR);
339 MAL_DBG(mal, "rxde %08x" NL, deir);
341 list_for_each(l, &mal->list) {
342 struct mal_commac *mc = list_entry(l, struct mal_commac, list);
343 if (deir & mc->rx_chan_mask) {
344 set_bit(MAL_COMMAC_RX_STOPPED, &mc->flags);
345 mc->ops->rxde(mc->dev);
349 mal_schedule_poll(mal);
350 set_mal_dcrn(mal, MAL_RXDEIR, deir);
355 static irqreturn_t mal_int(int irq, void *dev_instance)
357 struct mal_instance *mal = dev_instance;
358 u32 esr = get_mal_dcrn(mal, MAL_ESR);
360 if (esr & MAL_ESR_EVB) {
361 /* descriptor error */
362 if (esr & MAL_ESR_DE) {
363 if (esr & MAL_ESR_CIDT)
364 return mal_rxde(irq, dev_instance);
366 return mal_txde(irq, dev_instance);
368 return mal_serr(irq, dev_instance);
374 void mal_poll_disable(struct mal_instance *mal, struct mal_commac *commac)
376 /* Spinlock-type semantics: only one caller disable poll at a time */
377 while (test_and_set_bit(MAL_COMMAC_POLL_DISABLED, &commac->flags))
380 /* Synchronize with the MAL NAPI poller */
381 napi_synchronize(&mal->napi);
384 void mal_poll_enable(struct mal_instance *mal, struct mal_commac *commac)
387 clear_bit(MAL_COMMAC_POLL_DISABLED, &commac->flags);
389 /* Feels better to trigger a poll here to catch up with events that
390 * may have happened on this channel while disabled. It will most
391 * probably be delayed until the next interrupt but that's mostly a
392 * non-issue in the context where this is called.
394 napi_schedule(&mal->napi);
397 static int mal_poll(struct napi_struct *napi, int budget)
399 struct mal_instance *mal = container_of(napi, struct mal_instance, napi);
404 MAL_DBG2(mal, "poll(%d)" NL, budget);
406 /* Process TX skbs */
407 list_for_each(l, &mal->poll_list) {
408 struct mal_commac *mc =
409 list_entry(l, struct mal_commac, poll_list);
410 mc->ops->poll_tx(mc->dev);
415 * We _might_ need something more smart here to enforce polling
418 list_for_each(l, &mal->poll_list) {
419 struct mal_commac *mc =
420 list_entry(l, struct mal_commac, poll_list);
422 if (unlikely(test_bit(MAL_COMMAC_POLL_DISABLED, &mc->flags)))
424 n = mc->ops->poll_rx(mc->dev, budget - received);
427 if (received >= budget)
432 if (napi_complete_done(napi, received)) {
433 /* We need to disable IRQs to protect from RXDE IRQ here */
434 spin_lock_irqsave(&mal->lock, flags);
435 mal_enable_eob_irq(mal);
436 spin_unlock_irqrestore(&mal->lock, flags);
439 /* Check for "rotting" packet(s) */
440 list_for_each(l, &mal->poll_list) {
441 struct mal_commac *mc =
442 list_entry(l, struct mal_commac, poll_list);
443 if (unlikely(test_bit(MAL_COMMAC_POLL_DISABLED, &mc->flags)))
445 if (unlikely(mc->ops->peek_rx(mc->dev) ||
446 test_bit(MAL_COMMAC_RX_STOPPED, &mc->flags))) {
447 MAL_DBG2(mal, "rotting packet" NL);
448 if (!napi_reschedule(napi))
451 spin_lock_irqsave(&mal->lock, flags);
452 mal_disable_eob_irq(mal);
453 spin_unlock_irqrestore(&mal->lock, flags);
455 mc->ops->poll_tx(mc->dev);
459 MAL_DBG2(mal, "poll() %d <- %d" NL, budget, received);
463 static void mal_reset(struct mal_instance *mal)
467 MAL_DBG(mal, "reset" NL);
469 set_mal_dcrn(mal, MAL_CFG, MAL_CFG_SR);
471 /* Wait for reset to complete (1 system clock) */
472 while ((get_mal_dcrn(mal, MAL_CFG) & MAL_CFG_SR) && n)
476 printk(KERN_ERR "mal%d: reset timeout\n", mal->index);
479 int mal_get_regs_len(struct mal_instance *mal)
481 return sizeof(struct emac_ethtool_regs_subhdr) +
482 sizeof(struct mal_regs);
485 void *mal_dump_regs(struct mal_instance *mal, void *buf)
487 struct emac_ethtool_regs_subhdr *hdr = buf;
488 struct mal_regs *regs = (struct mal_regs *)(hdr + 1);
491 hdr->version = mal->version;
492 hdr->index = mal->index;
494 regs->tx_count = mal->num_tx_chans;
495 regs->rx_count = mal->num_rx_chans;
497 regs->cfg = get_mal_dcrn(mal, MAL_CFG);
498 regs->esr = get_mal_dcrn(mal, MAL_ESR);
499 regs->ier = get_mal_dcrn(mal, MAL_IER);
500 regs->tx_casr = get_mal_dcrn(mal, MAL_TXCASR);
501 regs->tx_carr = get_mal_dcrn(mal, MAL_TXCARR);
502 regs->tx_eobisr = get_mal_dcrn(mal, MAL_TXEOBISR);
503 regs->tx_deir = get_mal_dcrn(mal, MAL_TXDEIR);
504 regs->rx_casr = get_mal_dcrn(mal, MAL_RXCASR);
505 regs->rx_carr = get_mal_dcrn(mal, MAL_RXCARR);
506 regs->rx_eobisr = get_mal_dcrn(mal, MAL_RXEOBISR);
507 regs->rx_deir = get_mal_dcrn(mal, MAL_RXDEIR);
509 for (i = 0; i < regs->tx_count; ++i)
510 regs->tx_ctpr[i] = get_mal_dcrn(mal, MAL_TXCTPR(i));
512 for (i = 0; i < regs->rx_count; ++i) {
513 regs->rx_ctpr[i] = get_mal_dcrn(mal, MAL_RXCTPR(i));
514 regs->rcbs[i] = get_mal_dcrn(mal, MAL_RCBS(i));
519 static int mal_probe(struct platform_device *ofdev)
521 struct mal_instance *mal;
522 int err = 0, i, bd_size;
523 int index = mal_count++;
524 unsigned int dcr_base;
527 unsigned long irqflags;
528 irq_handler_t hdlr_serr, hdlr_txde, hdlr_rxde;
530 mal = kzalloc(sizeof(struct mal_instance), GFP_KERNEL);
536 mal->version = of_device_is_compatible(ofdev->dev.of_node, "ibm,mcmal2") ? 2 : 1;
538 MAL_DBG(mal, "probe" NL);
540 prop = of_get_property(ofdev->dev.of_node, "num-tx-chans", NULL);
543 "mal%d: can't find MAL num-tx-chans property!\n",
548 mal->num_tx_chans = prop[0];
550 prop = of_get_property(ofdev->dev.of_node, "num-rx-chans", NULL);
553 "mal%d: can't find MAL num-rx-chans property!\n",
558 mal->num_rx_chans = prop[0];
560 dcr_base = dcr_resource_start(ofdev->dev.of_node, 0);
563 "mal%d: can't find DCR resource!\n", index);
567 mal->dcr_host = dcr_map(ofdev->dev.of_node, dcr_base, 0x100);
568 if (!DCR_MAP_OK(mal->dcr_host)) {
570 "mal%d: failed to map DCRs !\n", index);
575 if (of_device_is_compatible(ofdev->dev.of_node, "ibm,mcmal-405ez")) {
576 #if defined(CONFIG_IBM_EMAC_MAL_CLR_ICINTSTAT) && \
577 defined(CONFIG_IBM_EMAC_MAL_COMMON_ERR)
578 mal->features |= (MAL_FTR_CLEAR_ICINTSTAT |
579 MAL_FTR_COMMON_ERR_INT);
581 printk(KERN_ERR "%pOF: Support for 405EZ not enabled!\n",
588 mal->txeob_irq = irq_of_parse_and_map(ofdev->dev.of_node, 0);
589 mal->rxeob_irq = irq_of_parse_and_map(ofdev->dev.of_node, 1);
590 mal->serr_irq = irq_of_parse_and_map(ofdev->dev.of_node, 2);
592 if (mal_has_feature(mal, MAL_FTR_COMMON_ERR_INT)) {
593 mal->txde_irq = mal->rxde_irq = mal->serr_irq;
595 mal->txde_irq = irq_of_parse_and_map(ofdev->dev.of_node, 3);
596 mal->rxde_irq = irq_of_parse_and_map(ofdev->dev.of_node, 4);
599 if (!mal->txeob_irq || !mal->rxeob_irq || !mal->serr_irq ||
600 !mal->txde_irq || !mal->rxde_irq) {
602 "mal%d: failed to map interrupts !\n", index);
607 INIT_LIST_HEAD(&mal->poll_list);
608 INIT_LIST_HEAD(&mal->list);
609 spin_lock_init(&mal->lock);
611 init_dummy_netdev(&mal->dummy_dev);
613 netif_napi_add(&mal->dummy_dev, &mal->napi, mal_poll,
614 CONFIG_IBM_EMAC_POLL_WEIGHT);
616 /* Load power-on reset defaults */
619 /* Set the MAL configuration register */
620 cfg = (mal->version == 2) ? MAL2_CFG_DEFAULT : MAL1_CFG_DEFAULT;
621 cfg |= MAL_CFG_PLBB | MAL_CFG_OPBBL | MAL_CFG_LEA;
623 /* Current Axon is not happy with priority being non-0, it can
624 * deadlock, fix it up here
626 if (of_device_is_compatible(ofdev->dev.of_node, "ibm,mcmal-axon"))
627 cfg &= ~(MAL2_CFG_RPP_10 | MAL2_CFG_WPP_10);
629 /* Apply configuration */
630 set_mal_dcrn(mal, MAL_CFG, cfg);
632 /* Allocate space for BD rings */
633 BUG_ON(mal->num_tx_chans <= 0 || mal->num_tx_chans > 32);
634 BUG_ON(mal->num_rx_chans <= 0 || mal->num_rx_chans > 32);
636 bd_size = sizeof(struct mal_descriptor) *
637 (NUM_TX_BUFF * mal->num_tx_chans +
638 NUM_RX_BUFF * mal->num_rx_chans);
639 mal->bd_virt = dma_zalloc_coherent(&ofdev->dev, bd_size, &mal->bd_dma,
641 if (mal->bd_virt == NULL) {
646 for (i = 0; i < mal->num_tx_chans; ++i)
647 set_mal_dcrn(mal, MAL_TXCTPR(i), mal->bd_dma +
648 sizeof(struct mal_descriptor) *
649 mal_tx_bd_offset(mal, i));
651 for (i = 0; i < mal->num_rx_chans; ++i)
652 set_mal_dcrn(mal, MAL_RXCTPR(i), mal->bd_dma +
653 sizeof(struct mal_descriptor) *
654 mal_rx_bd_offset(mal, i));
656 if (mal_has_feature(mal, MAL_FTR_COMMON_ERR_INT)) {
657 irqflags = IRQF_SHARED;
658 hdlr_serr = hdlr_txde = hdlr_rxde = mal_int;
661 hdlr_serr = mal_serr;
662 hdlr_txde = mal_txde;
663 hdlr_rxde = mal_rxde;
666 err = request_irq(mal->serr_irq, hdlr_serr, irqflags, "MAL SERR", mal);
669 err = request_irq(mal->txde_irq, hdlr_txde, irqflags, "MAL TX DE", mal);
672 err = request_irq(mal->txeob_irq, mal_txeob, 0, "MAL TX EOB", mal);
675 err = request_irq(mal->rxde_irq, hdlr_rxde, irqflags, "MAL RX DE", mal);
678 err = request_irq(mal->rxeob_irq, mal_rxeob, 0, "MAL RX EOB", mal);
682 /* Enable all MAL SERR interrupt sources */
683 set_mal_dcrn(mal, MAL_IER, MAL_IER_EVENTS);
685 /* Enable EOB interrupt */
686 mal_enable_eob_irq(mal);
689 "MAL v%d %pOF, %d TX channels, %d RX channels\n",
690 mal->version, ofdev->dev.of_node,
691 mal->num_tx_chans, mal->num_rx_chans);
693 /* Advertise this instance to the rest of the world */
695 platform_set_drvdata(ofdev, mal);
700 free_irq(mal->rxde_irq, mal);
702 free_irq(mal->txeob_irq, mal);
704 free_irq(mal->txde_irq, mal);
706 free_irq(mal->serr_irq, mal);
708 dma_free_coherent(&ofdev->dev, bd_size, mal->bd_virt, mal->bd_dma);
710 dcr_unmap(mal->dcr_host, 0x100);
717 static int mal_remove(struct platform_device *ofdev)
719 struct mal_instance *mal = platform_get_drvdata(ofdev);
721 MAL_DBG(mal, "remove" NL);
723 /* Synchronize with scheduled polling */
724 napi_disable(&mal->napi);
726 if (!list_empty(&mal->list))
727 /* This is *very* bad */
729 "mal%d: commac list is not empty on remove!\n",
732 free_irq(mal->serr_irq, mal);
733 free_irq(mal->txde_irq, mal);
734 free_irq(mal->txeob_irq, mal);
735 free_irq(mal->rxde_irq, mal);
736 free_irq(mal->rxeob_irq, mal);
740 dma_free_coherent(&ofdev->dev,
741 sizeof(struct mal_descriptor) *
742 (NUM_TX_BUFF * mal->num_tx_chans +
743 NUM_RX_BUFF * mal->num_rx_chans), mal->bd_virt,
750 static const struct of_device_id mal_platform_match[] =
753 .compatible = "ibm,mcmal",
756 .compatible = "ibm,mcmal2",
758 /* Backward compat */
761 .compatible = "ibm,mcmal",
765 .compatible = "ibm,mcmal2",
770 static struct platform_driver mal_of_driver = {
773 .of_match_table = mal_platform_match,
776 .remove = mal_remove,
779 int __init mal_init(void)
781 return platform_driver_register(&mal_of_driver);
786 platform_driver_unregister(&mal_of_driver);