2 * drivers/net/ibm_newemac/mal.c
4 * Memory Access Layer (MAL) support
6 * Copyright (c) 2004, 2005 Zultys Technologies.
7 * Eugene Surovegin <eugene.surovegin@zultys.com> or <ebs@ebshome.net>
9 * Based on original work by
10 * Benjamin Herrenschmidt <benh@kernel.crashing.org>,
11 * David Gibson <hermes@gibson.dropbear.id.au>,
13 * Armin Kuster <akuster@mvista.com>
14 * Copyright 2002 MontaVista Softare Inc.
16 * This program is free software; you can redistribute it and/or modify it
17 * under the terms of the GNU General Public License as published by the
18 * Free Software Foundation; either version 2 of the License, or (at your
19 * option) any later version.
23 #include <linux/delay.h>
29 int __devinit mal_register_commac(struct mal_instance *mal,
30 struct mal_commac *commac)
34 spin_lock_irqsave(&mal->lock, flags);
36 MAL_DBG(mal, "reg(%08x, %08x)" NL,
37 commac->tx_chan_mask, commac->rx_chan_mask);
39 /* Don't let multiple commacs claim the same channel(s) */
40 if ((mal->tx_chan_mask & commac->tx_chan_mask) ||
41 (mal->rx_chan_mask & commac->rx_chan_mask)) {
42 spin_unlock_irqrestore(&mal->lock, flags);
43 printk(KERN_WARNING "mal%d: COMMAC channels conflict!\n",
48 if (list_empty(&mal->list))
49 napi_enable(&mal->napi);
50 mal->tx_chan_mask |= commac->tx_chan_mask;
51 mal->rx_chan_mask |= commac->rx_chan_mask;
52 list_add(&commac->list, &mal->list);
54 spin_unlock_irqrestore(&mal->lock, flags);
59 void __devexit mal_unregister_commac(struct mal_instance *mal,
60 struct mal_commac *commac)
64 spin_lock_irqsave(&mal->lock, flags);
66 MAL_DBG(mal, "unreg(%08x, %08x)" NL,
67 commac->tx_chan_mask, commac->rx_chan_mask);
69 mal->tx_chan_mask &= ~commac->tx_chan_mask;
70 mal->rx_chan_mask &= ~commac->rx_chan_mask;
71 list_del_init(&commac->list);
72 if (list_empty(&mal->list))
73 napi_disable(&mal->napi);
75 spin_unlock_irqrestore(&mal->lock, flags);
78 int mal_set_rcbs(struct mal_instance *mal, int channel, unsigned long size)
80 BUG_ON(channel < 0 || channel >= mal->num_rx_chans ||
81 size > MAL_MAX_RX_SIZE);
83 MAL_DBG(mal, "set_rbcs(%d, %lu)" NL, channel, size);
87 "mal%d: incorrect RX size %lu for the channel %d\n",
88 mal->index, size, channel);
92 set_mal_dcrn(mal, MAL_RCBS(channel), size >> 4);
96 int mal_tx_bd_offset(struct mal_instance *mal, int channel)
98 BUG_ON(channel < 0 || channel >= mal->num_tx_chans);
100 return channel * NUM_TX_BUFF;
103 int mal_rx_bd_offset(struct mal_instance *mal, int channel)
105 BUG_ON(channel < 0 || channel >= mal->num_rx_chans);
106 return mal->num_tx_chans * NUM_TX_BUFF + channel * NUM_RX_BUFF;
109 void mal_enable_tx_channel(struct mal_instance *mal, int channel)
113 spin_lock_irqsave(&mal->lock, flags);
115 MAL_DBG(mal, "enable_tx(%d)" NL, channel);
117 set_mal_dcrn(mal, MAL_TXCASR,
118 get_mal_dcrn(mal, MAL_TXCASR) | MAL_CHAN_MASK(channel));
120 spin_unlock_irqrestore(&mal->lock, flags);
123 void mal_disable_tx_channel(struct mal_instance *mal, int channel)
125 set_mal_dcrn(mal, MAL_TXCARR, MAL_CHAN_MASK(channel));
127 MAL_DBG(mal, "disable_tx(%d)" NL, channel);
130 void mal_enable_rx_channel(struct mal_instance *mal, int channel)
134 spin_lock_irqsave(&mal->lock, flags);
136 MAL_DBG(mal, "enable_rx(%d)" NL, channel);
138 set_mal_dcrn(mal, MAL_RXCASR,
139 get_mal_dcrn(mal, MAL_RXCASR) | MAL_CHAN_MASK(channel));
141 spin_unlock_irqrestore(&mal->lock, flags);
144 void mal_disable_rx_channel(struct mal_instance *mal, int channel)
146 set_mal_dcrn(mal, MAL_RXCARR, MAL_CHAN_MASK(channel));
148 MAL_DBG(mal, "disable_rx(%d)" NL, channel);
151 void mal_poll_add(struct mal_instance *mal, struct mal_commac *commac)
155 spin_lock_irqsave(&mal->lock, flags);
157 MAL_DBG(mal, "poll_add(%p)" NL, commac);
159 /* starts disabled */
160 set_bit(MAL_COMMAC_POLL_DISABLED, &commac->flags);
162 list_add_tail(&commac->poll_list, &mal->poll_list);
164 spin_unlock_irqrestore(&mal->lock, flags);
167 void mal_poll_del(struct mal_instance *mal, struct mal_commac *commac)
171 spin_lock_irqsave(&mal->lock, flags);
173 MAL_DBG(mal, "poll_del(%p)" NL, commac);
175 list_del(&commac->poll_list);
177 spin_unlock_irqrestore(&mal->lock, flags);
180 /* synchronized by mal_poll() */
181 static inline void mal_enable_eob_irq(struct mal_instance *mal)
183 MAL_DBG2(mal, "enable_irq" NL);
185 // XXX might want to cache MAL_CFG as the DCR read can be slooooow
186 set_mal_dcrn(mal, MAL_CFG, get_mal_dcrn(mal, MAL_CFG) | MAL_CFG_EOPIE);
189 /* synchronized by NAPI state */
190 static inline void mal_disable_eob_irq(struct mal_instance *mal)
192 // XXX might want to cache MAL_CFG as the DCR read can be slooooow
193 set_mal_dcrn(mal, MAL_CFG, get_mal_dcrn(mal, MAL_CFG) & ~MAL_CFG_EOPIE);
195 MAL_DBG2(mal, "disable_irq" NL);
198 static irqreturn_t mal_serr(int irq, void *dev_instance)
200 struct mal_instance *mal = dev_instance;
202 u32 esr = get_mal_dcrn(mal, MAL_ESR);
204 /* Clear the error status register */
205 set_mal_dcrn(mal, MAL_ESR, esr);
207 MAL_DBG(mal, "SERR %08x" NL, esr);
209 if (esr & MAL_ESR_EVB) {
210 if (esr & MAL_ESR_DE) {
211 /* We ignore Descriptor error,
212 * TXDE or RXDE interrupt will be generated anyway.
217 if (esr & MAL_ESR_PEIN) {
218 /* PLB error, it's probably buggy hardware or
219 * incorrect physical address in BD (i.e. bug)
223 "mal%d: system error, "
224 "PLB (ESR = 0x%08x)\n",
229 /* OPB error, it's probably buggy hardware or incorrect
234 "mal%d: system error, OPB (ESR = 0x%08x)\n",
240 static inline void mal_schedule_poll(struct mal_instance *mal)
242 if (likely(napi_schedule_prep(&mal->napi))) {
243 MAL_DBG2(mal, "schedule_poll" NL);
244 mal_disable_eob_irq(mal);
245 __napi_schedule(&mal->napi);
247 MAL_DBG2(mal, "already in poll" NL);
250 static irqreturn_t mal_txeob(int irq, void *dev_instance)
252 struct mal_instance *mal = dev_instance;
254 u32 r = get_mal_dcrn(mal, MAL_TXEOBISR);
256 MAL_DBG2(mal, "txeob %08x" NL, r);
258 mal_schedule_poll(mal);
259 set_mal_dcrn(mal, MAL_TXEOBISR, r);
264 static irqreturn_t mal_rxeob(int irq, void *dev_instance)
266 struct mal_instance *mal = dev_instance;
268 u32 r = get_mal_dcrn(mal, MAL_RXEOBISR);
270 MAL_DBG2(mal, "rxeob %08x" NL, r);
272 mal_schedule_poll(mal);
273 set_mal_dcrn(mal, MAL_RXEOBISR, r);
278 static irqreturn_t mal_txde(int irq, void *dev_instance)
280 struct mal_instance *mal = dev_instance;
282 u32 deir = get_mal_dcrn(mal, MAL_TXDEIR);
283 set_mal_dcrn(mal, MAL_TXDEIR, deir);
285 MAL_DBG(mal, "txde %08x" NL, deir);
289 "mal%d: TX descriptor error (TXDEIR = 0x%08x)\n",
295 static irqreturn_t mal_rxde(int irq, void *dev_instance)
297 struct mal_instance *mal = dev_instance;
300 u32 deir = get_mal_dcrn(mal, MAL_RXDEIR);
302 MAL_DBG(mal, "rxde %08x" NL, deir);
304 list_for_each(l, &mal->list) {
305 struct mal_commac *mc = list_entry(l, struct mal_commac, list);
306 if (deir & mc->rx_chan_mask) {
307 set_bit(MAL_COMMAC_RX_STOPPED, &mc->flags);
308 mc->ops->rxde(mc->dev);
312 mal_schedule_poll(mal);
313 set_mal_dcrn(mal, MAL_RXDEIR, deir);
318 void mal_poll_disable(struct mal_instance *mal, struct mal_commac *commac)
320 /* Spinlock-type semantics: only one caller disable poll at a time */
321 while (test_and_set_bit(MAL_COMMAC_POLL_DISABLED, &commac->flags))
324 /* Synchronize with the MAL NAPI poller */
325 napi_synchronize(&mal->napi);
328 void mal_poll_enable(struct mal_instance *mal, struct mal_commac *commac)
331 clear_bit(MAL_COMMAC_POLL_DISABLED, &commac->flags);
333 /* Feels better to trigger a poll here to catch up with events that
334 * may have happened on this channel while disabled. It will most
335 * probably be delayed until the next interrupt but that's mostly a
336 * non-issue in the context where this is called.
338 napi_schedule(&mal->napi);
341 static int mal_poll(struct napi_struct *napi, int budget)
343 struct mal_instance *mal = container_of(napi, struct mal_instance, napi);
348 MAL_DBG2(mal, "poll(%d)" NL, budget);
350 /* Process TX skbs */
351 list_for_each(l, &mal->poll_list) {
352 struct mal_commac *mc =
353 list_entry(l, struct mal_commac, poll_list);
354 mc->ops->poll_tx(mc->dev);
359 * We _might_ need something more smart here to enforce polling
362 list_for_each(l, &mal->poll_list) {
363 struct mal_commac *mc =
364 list_entry(l, struct mal_commac, poll_list);
366 if (unlikely(test_bit(MAL_COMMAC_POLL_DISABLED, &mc->flags)))
368 n = mc->ops->poll_rx(mc->dev, budget);
373 goto more_work; // XXX What if this is the last one ?
377 /* We need to disable IRQs to protect from RXDE IRQ here */
378 spin_lock_irqsave(&mal->lock, flags);
379 __napi_complete(napi);
380 mal_enable_eob_irq(mal);
381 spin_unlock_irqrestore(&mal->lock, flags);
383 /* Check for "rotting" packet(s) */
384 list_for_each(l, &mal->poll_list) {
385 struct mal_commac *mc =
386 list_entry(l, struct mal_commac, poll_list);
387 if (unlikely(test_bit(MAL_COMMAC_POLL_DISABLED, &mc->flags)))
389 if (unlikely(mc->ops->peek_rx(mc->dev) ||
390 test_bit(MAL_COMMAC_RX_STOPPED, &mc->flags))) {
391 MAL_DBG2(mal, "rotting packet" NL);
392 if (napi_reschedule(napi))
393 mal_disable_eob_irq(mal);
395 MAL_DBG2(mal, "already in poll list" NL);
402 mc->ops->poll_tx(mc->dev);
406 MAL_DBG2(mal, "poll() %d <- %d" NL, budget, received);
410 static void mal_reset(struct mal_instance *mal)
414 MAL_DBG(mal, "reset" NL);
416 set_mal_dcrn(mal, MAL_CFG, MAL_CFG_SR);
418 /* Wait for reset to complete (1 system clock) */
419 while ((get_mal_dcrn(mal, MAL_CFG) & MAL_CFG_SR) && n)
423 printk(KERN_ERR "mal%d: reset timeout\n", mal->index);
426 int mal_get_regs_len(struct mal_instance *mal)
428 return sizeof(struct emac_ethtool_regs_subhdr) +
429 sizeof(struct mal_regs);
432 void *mal_dump_regs(struct mal_instance *mal, void *buf)
434 struct emac_ethtool_regs_subhdr *hdr = buf;
435 struct mal_regs *regs = (struct mal_regs *)(hdr + 1);
438 hdr->version = mal->version;
439 hdr->index = mal->index;
441 regs->tx_count = mal->num_tx_chans;
442 regs->rx_count = mal->num_rx_chans;
444 regs->cfg = get_mal_dcrn(mal, MAL_CFG);
445 regs->esr = get_mal_dcrn(mal, MAL_ESR);
446 regs->ier = get_mal_dcrn(mal, MAL_IER);
447 regs->tx_casr = get_mal_dcrn(mal, MAL_TXCASR);
448 regs->tx_carr = get_mal_dcrn(mal, MAL_TXCARR);
449 regs->tx_eobisr = get_mal_dcrn(mal, MAL_TXEOBISR);
450 regs->tx_deir = get_mal_dcrn(mal, MAL_TXDEIR);
451 regs->rx_casr = get_mal_dcrn(mal, MAL_RXCASR);
452 regs->rx_carr = get_mal_dcrn(mal, MAL_RXCARR);
453 regs->rx_eobisr = get_mal_dcrn(mal, MAL_RXEOBISR);
454 regs->rx_deir = get_mal_dcrn(mal, MAL_RXDEIR);
456 for (i = 0; i < regs->tx_count; ++i)
457 regs->tx_ctpr[i] = get_mal_dcrn(mal, MAL_TXCTPR(i));
459 for (i = 0; i < regs->rx_count; ++i) {
460 regs->rx_ctpr[i] = get_mal_dcrn(mal, MAL_RXCTPR(i));
461 regs->rcbs[i] = get_mal_dcrn(mal, MAL_RCBS(i));
466 static int __devinit mal_probe(struct of_device *ofdev,
467 const struct of_device_id *match)
469 struct mal_instance *mal;
470 int err = 0, i, bd_size;
471 int index = mal_count++;
472 unsigned int dcr_base;
476 mal = kzalloc(sizeof(struct mal_instance), GFP_KERNEL);
479 "mal%d: out of memory allocating MAL structure!\n",
485 mal->version = of_device_is_compatible(ofdev->node, "ibm,mcmal2") ? 2 : 1;
487 MAL_DBG(mal, "probe" NL);
489 prop = of_get_property(ofdev->node, "num-tx-chans", NULL);
492 "mal%d: can't find MAL num-tx-chans property!\n",
497 mal->num_tx_chans = prop[0];
499 prop = of_get_property(ofdev->node, "num-rx-chans", NULL);
502 "mal%d: can't find MAL num-rx-chans property!\n",
507 mal->num_rx_chans = prop[0];
509 dcr_base = dcr_resource_start(ofdev->node, 0);
512 "mal%d: can't find DCR resource!\n", index);
516 mal->dcr_host = dcr_map(ofdev->node, dcr_base, 0x100);
517 if (!DCR_MAP_OK(mal->dcr_host)) {
519 "mal%d: failed to map DCRs !\n", index);
524 mal->txeob_irq = irq_of_parse_and_map(ofdev->node, 0);
525 mal->rxeob_irq = irq_of_parse_and_map(ofdev->node, 1);
526 mal->serr_irq = irq_of_parse_and_map(ofdev->node, 2);
527 mal->txde_irq = irq_of_parse_and_map(ofdev->node, 3);
528 mal->rxde_irq = irq_of_parse_and_map(ofdev->node, 4);
529 if (mal->txeob_irq == NO_IRQ || mal->rxeob_irq == NO_IRQ ||
530 mal->serr_irq == NO_IRQ || mal->txde_irq == NO_IRQ ||
531 mal->rxde_irq == NO_IRQ) {
533 "mal%d: failed to map interrupts !\n", index);
538 INIT_LIST_HEAD(&mal->poll_list);
539 INIT_LIST_HEAD(&mal->list);
540 spin_lock_init(&mal->lock);
542 netif_napi_add(NULL, &mal->napi, mal_poll,
543 CONFIG_IBM_NEW_EMAC_POLL_WEIGHT);
545 /* Load power-on reset defaults */
548 /* Set the MAL configuration register */
549 cfg = (mal->version == 2) ? MAL2_CFG_DEFAULT : MAL1_CFG_DEFAULT;
550 cfg |= MAL_CFG_PLBB | MAL_CFG_OPBBL | MAL_CFG_LEA;
552 /* Current Axon is not happy with priority being non-0, it can
553 * deadlock, fix it up here
555 if (of_device_is_compatible(ofdev->node, "ibm,mcmal-axon"))
556 cfg &= ~(MAL2_CFG_RPP_10 | MAL2_CFG_WPP_10);
558 /* Apply configuration */
559 set_mal_dcrn(mal, MAL_CFG, cfg);
561 /* Allocate space for BD rings */
562 BUG_ON(mal->num_tx_chans <= 0 || mal->num_tx_chans > 32);
563 BUG_ON(mal->num_rx_chans <= 0 || mal->num_rx_chans > 32);
565 bd_size = sizeof(struct mal_descriptor) *
566 (NUM_TX_BUFF * mal->num_tx_chans +
567 NUM_RX_BUFF * mal->num_rx_chans);
569 dma_alloc_coherent(&ofdev->dev, bd_size, &mal->bd_dma,
571 if (mal->bd_virt == NULL) {
573 "mal%d: out of memory allocating RX/TX descriptors!\n",
578 memset(mal->bd_virt, 0, bd_size);
580 for (i = 0; i < mal->num_tx_chans; ++i)
581 set_mal_dcrn(mal, MAL_TXCTPR(i), mal->bd_dma +
582 sizeof(struct mal_descriptor) *
583 mal_tx_bd_offset(mal, i));
585 for (i = 0; i < mal->num_rx_chans; ++i)
586 set_mal_dcrn(mal, MAL_RXCTPR(i), mal->bd_dma +
587 sizeof(struct mal_descriptor) *
588 mal_rx_bd_offset(mal, i));
590 err = request_irq(mal->serr_irq, mal_serr, 0, "MAL SERR", mal);
593 err = request_irq(mal->txde_irq, mal_txde, 0, "MAL TX DE", mal);
596 err = request_irq(mal->txeob_irq, mal_txeob, 0, "MAL TX EOB", mal);
599 err = request_irq(mal->rxde_irq, mal_rxde, 0, "MAL RX DE", mal);
602 err = request_irq(mal->rxeob_irq, mal_rxeob, 0, "MAL RX EOB", mal);
606 /* Enable all MAL SERR interrupt sources */
607 if (mal->version == 2)
608 set_mal_dcrn(mal, MAL_IER, MAL2_IER_EVENTS);
610 set_mal_dcrn(mal, MAL_IER, MAL1_IER_EVENTS);
612 /* Enable EOB interrupt */
613 mal_enable_eob_irq(mal);
616 "MAL v%d %s, %d TX channels, %d RX channels\n",
617 mal->version, ofdev->node->full_name,
618 mal->num_tx_chans, mal->num_rx_chans);
620 /* Advertise this instance to the rest of the world */
622 dev_set_drvdata(&ofdev->dev, mal);
624 mal_dbg_register(mal);
629 free_irq(mal->rxde_irq, mal);
631 free_irq(mal->txeob_irq, mal);
633 free_irq(mal->txde_irq, mal);
635 free_irq(mal->serr_irq, mal);
637 dma_free_coherent(&ofdev->dev, bd_size, mal->bd_virt, mal->bd_dma);
639 dcr_unmap(mal->dcr_host, 0x100);
646 static int __devexit mal_remove(struct of_device *ofdev)
648 struct mal_instance *mal = dev_get_drvdata(&ofdev->dev);
650 MAL_DBG(mal, "remove" NL);
652 /* Synchronize with scheduled polling */
653 napi_disable(&mal->napi);
655 if (!list_empty(&mal->list)) {
656 /* This is *very* bad */
658 "mal%d: commac list is not empty on remove!\n",
663 dev_set_drvdata(&ofdev->dev, NULL);
665 free_irq(mal->serr_irq, mal);
666 free_irq(mal->txde_irq, mal);
667 free_irq(mal->txeob_irq, mal);
668 free_irq(mal->rxde_irq, mal);
669 free_irq(mal->rxeob_irq, mal);
673 mal_dbg_unregister(mal);
675 dma_free_coherent(&ofdev->dev,
676 sizeof(struct mal_descriptor) *
677 (NUM_TX_BUFF * mal->num_tx_chans +
678 NUM_RX_BUFF * mal->num_rx_chans), mal->bd_virt,
685 static struct of_device_id mal_platform_match[] =
688 .compatible = "ibm,mcmal",
691 .compatible = "ibm,mcmal2",
693 /* Backward compat */
696 .compatible = "ibm,mcmal",
700 .compatible = "ibm,mcmal2",
705 static struct of_platform_driver mal_of_driver = {
707 .match_table = mal_platform_match,
710 .remove = mal_remove,
713 int __init mal_init(void)
715 return of_register_platform_driver(&mal_of_driver);
720 of_unregister_platform_driver(&mal_of_driver);