2 * drivers/net/ibm_newemac/mal.c
4 * Memory Access Layer (MAL) support
6 * Copyright 2007 Benjamin Herrenschmidt, IBM Corp.
7 * <benh@kernel.crashing.org>
9 * Based on the arch/ppc version of the driver:
11 * Copyright (c) 2004, 2005 Zultys Technologies.
12 * Eugene Surovegin <eugene.surovegin@zultys.com> or <ebs@ebshome.net>
14 * Based on original work by
15 * Benjamin Herrenschmidt <benh@kernel.crashing.org>,
16 * David Gibson <hermes@gibson.dropbear.id.au>,
18 * Armin Kuster <akuster@mvista.com>
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>
34 int __devinit
mal_register_commac(struct mal_instance
*mal
,
35 struct mal_commac
*commac
)
39 spin_lock_irqsave(&mal
->lock
, flags
);
41 MAL_DBG(mal
, "reg(%08x, %08x)" NL
,
42 commac
->tx_chan_mask
, commac
->rx_chan_mask
);
44 /* Don't let multiple commacs claim the same channel(s) */
45 if ((mal
->tx_chan_mask
& commac
->tx_chan_mask
) ||
46 (mal
->rx_chan_mask
& commac
->rx_chan_mask
)) {
47 spin_unlock_irqrestore(&mal
->lock
, flags
);
48 printk(KERN_WARNING
"mal%d: COMMAC channels conflict!\n",
53 if (list_empty(&mal
->list
))
54 napi_enable(&mal
->napi
);
55 mal
->tx_chan_mask
|= commac
->tx_chan_mask
;
56 mal
->rx_chan_mask
|= commac
->rx_chan_mask
;
57 list_add(&commac
->list
, &mal
->list
);
59 spin_unlock_irqrestore(&mal
->lock
, flags
);
64 void mal_unregister_commac(struct mal_instance
*mal
,
65 struct mal_commac
*commac
)
69 spin_lock_irqsave(&mal
->lock
, flags
);
71 MAL_DBG(mal
, "unreg(%08x, %08x)" NL
,
72 commac
->tx_chan_mask
, commac
->rx_chan_mask
);
74 mal
->tx_chan_mask
&= ~commac
->tx_chan_mask
;
75 mal
->rx_chan_mask
&= ~commac
->rx_chan_mask
;
76 list_del_init(&commac
->list
);
77 if (list_empty(&mal
->list
))
78 napi_disable(&mal
->napi
);
80 spin_unlock_irqrestore(&mal
->lock
, flags
);
83 int mal_set_rcbs(struct mal_instance
*mal
, int channel
, unsigned long size
)
85 BUG_ON(channel
< 0 || channel
>= mal
->num_rx_chans
||
86 size
> MAL_MAX_RX_SIZE
);
88 MAL_DBG(mal
, "set_rbcs(%d, %lu)" NL
, channel
, size
);
92 "mal%d: incorrect RX size %lu for the channel %d\n",
93 mal
->index
, size
, channel
);
97 set_mal_dcrn(mal
, MAL_RCBS(channel
), size
>> 4);
101 int mal_tx_bd_offset(struct mal_instance
*mal
, int channel
)
103 BUG_ON(channel
< 0 || channel
>= mal
->num_tx_chans
);
105 return channel
* NUM_TX_BUFF
;
108 int mal_rx_bd_offset(struct mal_instance
*mal
, int channel
)
110 BUG_ON(channel
< 0 || channel
>= mal
->num_rx_chans
);
111 return mal
->num_tx_chans
* NUM_TX_BUFF
+ channel
* NUM_RX_BUFF
;
114 void mal_enable_tx_channel(struct mal_instance
*mal
, int channel
)
118 spin_lock_irqsave(&mal
->lock
, flags
);
120 MAL_DBG(mal
, "enable_tx(%d)" NL
, channel
);
122 set_mal_dcrn(mal
, MAL_TXCASR
,
123 get_mal_dcrn(mal
, MAL_TXCASR
) | MAL_CHAN_MASK(channel
));
125 spin_unlock_irqrestore(&mal
->lock
, flags
);
128 void mal_disable_tx_channel(struct mal_instance
*mal
, int channel
)
130 set_mal_dcrn(mal
, MAL_TXCARR
, MAL_CHAN_MASK(channel
));
132 MAL_DBG(mal
, "disable_tx(%d)" NL
, channel
);
135 void mal_enable_rx_channel(struct mal_instance
*mal
, int channel
)
140 * On some 4xx PPC's (e.g. 460EX/GT), the rx channel is a multiple
141 * of 8, but enabling in MAL_RXCASR needs the divided by 8 value
147 spin_lock_irqsave(&mal
->lock
, flags
);
149 MAL_DBG(mal
, "enable_rx(%d)" NL
, channel
);
151 set_mal_dcrn(mal
, MAL_RXCASR
,
152 get_mal_dcrn(mal
, MAL_RXCASR
) | MAL_CHAN_MASK(channel
));
154 spin_unlock_irqrestore(&mal
->lock
, flags
);
157 void mal_disable_rx_channel(struct mal_instance
*mal
, int channel
)
160 * On some 4xx PPC's (e.g. 460EX/GT), the rx channel is a multiple
161 * of 8, but enabling in MAL_RXCASR needs the divided by 8 value
167 set_mal_dcrn(mal
, MAL_RXCARR
, MAL_CHAN_MASK(channel
));
169 MAL_DBG(mal
, "disable_rx(%d)" NL
, channel
);
172 void mal_poll_add(struct mal_instance
*mal
, struct mal_commac
*commac
)
176 spin_lock_irqsave(&mal
->lock
, flags
);
178 MAL_DBG(mal
, "poll_add(%p)" NL
, commac
);
180 /* starts disabled */
181 set_bit(MAL_COMMAC_POLL_DISABLED
, &commac
->flags
);
183 list_add_tail(&commac
->poll_list
, &mal
->poll_list
);
185 spin_unlock_irqrestore(&mal
->lock
, flags
);
188 void mal_poll_del(struct mal_instance
*mal
, struct mal_commac
*commac
)
192 spin_lock_irqsave(&mal
->lock
, flags
);
194 MAL_DBG(mal
, "poll_del(%p)" NL
, commac
);
196 list_del(&commac
->poll_list
);
198 spin_unlock_irqrestore(&mal
->lock
, flags
);
201 /* synchronized by mal_poll() */
202 static inline void mal_enable_eob_irq(struct mal_instance
*mal
)
204 MAL_DBG2(mal
, "enable_irq" NL
);
206 // XXX might want to cache MAL_CFG as the DCR read can be slooooow
207 set_mal_dcrn(mal
, MAL_CFG
, get_mal_dcrn(mal
, MAL_CFG
) | MAL_CFG_EOPIE
);
210 /* synchronized by NAPI state */
211 static inline void mal_disable_eob_irq(struct mal_instance
*mal
)
213 // XXX might want to cache MAL_CFG as the DCR read can be slooooow
214 set_mal_dcrn(mal
, MAL_CFG
, get_mal_dcrn(mal
, MAL_CFG
) & ~MAL_CFG_EOPIE
);
216 MAL_DBG2(mal
, "disable_irq" NL
);
219 static irqreturn_t
mal_serr(int irq
, void *dev_instance
)
221 struct mal_instance
*mal
= dev_instance
;
223 u32 esr
= get_mal_dcrn(mal
, MAL_ESR
);
225 /* Clear the error status register */
226 set_mal_dcrn(mal
, MAL_ESR
, esr
);
228 MAL_DBG(mal
, "SERR %08x" NL
, esr
);
230 if (esr
& MAL_ESR_EVB
) {
231 if (esr
& MAL_ESR_DE
) {
232 /* We ignore Descriptor error,
233 * TXDE or RXDE interrupt will be generated anyway.
238 if (esr
& MAL_ESR_PEIN
) {
239 /* PLB error, it's probably buggy hardware or
240 * incorrect physical address in BD (i.e. bug)
244 "mal%d: system error, "
245 "PLB (ESR = 0x%08x)\n",
250 /* OPB error, it's probably buggy hardware or incorrect
255 "mal%d: system error, OPB (ESR = 0x%08x)\n",
261 static inline void mal_schedule_poll(struct mal_instance
*mal
)
263 if (likely(napi_schedule_prep(&mal
->napi
))) {
264 MAL_DBG2(mal
, "schedule_poll" NL
);
265 mal_disable_eob_irq(mal
);
266 __napi_schedule(&mal
->napi
);
268 MAL_DBG2(mal
, "already in poll" NL
);
271 static irqreturn_t
mal_txeob(int irq
, void *dev_instance
)
273 struct mal_instance
*mal
= dev_instance
;
275 u32 r
= get_mal_dcrn(mal
, MAL_TXEOBISR
);
277 MAL_DBG2(mal
, "txeob %08x" NL
, r
);
279 mal_schedule_poll(mal
);
280 set_mal_dcrn(mal
, MAL_TXEOBISR
, r
);
285 static irqreturn_t
mal_rxeob(int irq
, void *dev_instance
)
287 struct mal_instance
*mal
= dev_instance
;
289 u32 r
= get_mal_dcrn(mal
, MAL_RXEOBISR
);
291 MAL_DBG2(mal
, "rxeob %08x" NL
, r
);
293 mal_schedule_poll(mal
);
294 set_mal_dcrn(mal
, MAL_RXEOBISR
, r
);
299 static irqreturn_t
mal_txde(int irq
, void *dev_instance
)
301 struct mal_instance
*mal
= dev_instance
;
303 u32 deir
= get_mal_dcrn(mal
, MAL_TXDEIR
);
304 set_mal_dcrn(mal
, MAL_TXDEIR
, deir
);
306 MAL_DBG(mal
, "txde %08x" NL
, deir
);
310 "mal%d: TX descriptor error (TXDEIR = 0x%08x)\n",
316 static irqreturn_t
mal_rxde(int irq
, void *dev_instance
)
318 struct mal_instance
*mal
= dev_instance
;
321 u32 deir
= get_mal_dcrn(mal
, MAL_RXDEIR
);
323 MAL_DBG(mal
, "rxde %08x" NL
, deir
);
325 list_for_each(l
, &mal
->list
) {
326 struct mal_commac
*mc
= list_entry(l
, struct mal_commac
, list
);
327 if (deir
& mc
->rx_chan_mask
) {
328 set_bit(MAL_COMMAC_RX_STOPPED
, &mc
->flags
);
329 mc
->ops
->rxde(mc
->dev
);
333 mal_schedule_poll(mal
);
334 set_mal_dcrn(mal
, MAL_RXDEIR
, deir
);
339 void mal_poll_disable(struct mal_instance
*mal
, struct mal_commac
*commac
)
341 /* Spinlock-type semantics: only one caller disable poll at a time */
342 while (test_and_set_bit(MAL_COMMAC_POLL_DISABLED
, &commac
->flags
))
345 /* Synchronize with the MAL NAPI poller */
346 napi_synchronize(&mal
->napi
);
349 void mal_poll_enable(struct mal_instance
*mal
, struct mal_commac
*commac
)
352 clear_bit(MAL_COMMAC_POLL_DISABLED
, &commac
->flags
);
354 /* Feels better to trigger a poll here to catch up with events that
355 * may have happened on this channel while disabled. It will most
356 * probably be delayed until the next interrupt but that's mostly a
357 * non-issue in the context where this is called.
359 napi_schedule(&mal
->napi
);
362 static int mal_poll(struct napi_struct
*napi
, int budget
)
364 struct mal_instance
*mal
= container_of(napi
, struct mal_instance
, napi
);
369 MAL_DBG2(mal
, "poll(%d)" NL
, budget
);
371 /* Process TX skbs */
372 list_for_each(l
, &mal
->poll_list
) {
373 struct mal_commac
*mc
=
374 list_entry(l
, struct mal_commac
, poll_list
);
375 mc
->ops
->poll_tx(mc
->dev
);
380 * We _might_ need something more smart here to enforce polling
383 list_for_each(l
, &mal
->poll_list
) {
384 struct mal_commac
*mc
=
385 list_entry(l
, struct mal_commac
, poll_list
);
387 if (unlikely(test_bit(MAL_COMMAC_POLL_DISABLED
, &mc
->flags
)))
389 n
= mc
->ops
->poll_rx(mc
->dev
, budget
);
394 goto more_work
; // XXX What if this is the last one ?
398 /* We need to disable IRQs to protect from RXDE IRQ here */
399 spin_lock_irqsave(&mal
->lock
, flags
);
400 __napi_complete(napi
);
401 mal_enable_eob_irq(mal
);
402 spin_unlock_irqrestore(&mal
->lock
, flags
);
404 /* Check for "rotting" packet(s) */
405 list_for_each(l
, &mal
->poll_list
) {
406 struct mal_commac
*mc
=
407 list_entry(l
, struct mal_commac
, poll_list
);
408 if (unlikely(test_bit(MAL_COMMAC_POLL_DISABLED
, &mc
->flags
)))
410 if (unlikely(mc
->ops
->peek_rx(mc
->dev
) ||
411 test_bit(MAL_COMMAC_RX_STOPPED
, &mc
->flags
))) {
412 MAL_DBG2(mal
, "rotting packet" NL
);
413 if (napi_reschedule(napi
))
414 mal_disable_eob_irq(mal
);
416 MAL_DBG2(mal
, "already in poll list" NL
);
423 mc
->ops
->poll_tx(mc
->dev
);
427 MAL_DBG2(mal
, "poll() %d <- %d" NL
, budget
, received
);
431 static void mal_reset(struct mal_instance
*mal
)
435 MAL_DBG(mal
, "reset" NL
);
437 set_mal_dcrn(mal
, MAL_CFG
, MAL_CFG_SR
);
439 /* Wait for reset to complete (1 system clock) */
440 while ((get_mal_dcrn(mal
, MAL_CFG
) & MAL_CFG_SR
) && n
)
444 printk(KERN_ERR
"mal%d: reset timeout\n", mal
->index
);
447 int mal_get_regs_len(struct mal_instance
*mal
)
449 return sizeof(struct emac_ethtool_regs_subhdr
) +
450 sizeof(struct mal_regs
);
453 void *mal_dump_regs(struct mal_instance
*mal
, void *buf
)
455 struct emac_ethtool_regs_subhdr
*hdr
= buf
;
456 struct mal_regs
*regs
= (struct mal_regs
*)(hdr
+ 1);
459 hdr
->version
= mal
->version
;
460 hdr
->index
= mal
->index
;
462 regs
->tx_count
= mal
->num_tx_chans
;
463 regs
->rx_count
= mal
->num_rx_chans
;
465 regs
->cfg
= get_mal_dcrn(mal
, MAL_CFG
);
466 regs
->esr
= get_mal_dcrn(mal
, MAL_ESR
);
467 regs
->ier
= get_mal_dcrn(mal
, MAL_IER
);
468 regs
->tx_casr
= get_mal_dcrn(mal
, MAL_TXCASR
);
469 regs
->tx_carr
= get_mal_dcrn(mal
, MAL_TXCARR
);
470 regs
->tx_eobisr
= get_mal_dcrn(mal
, MAL_TXEOBISR
);
471 regs
->tx_deir
= get_mal_dcrn(mal
, MAL_TXDEIR
);
472 regs
->rx_casr
= get_mal_dcrn(mal
, MAL_RXCASR
);
473 regs
->rx_carr
= get_mal_dcrn(mal
, MAL_RXCARR
);
474 regs
->rx_eobisr
= get_mal_dcrn(mal
, MAL_RXEOBISR
);
475 regs
->rx_deir
= get_mal_dcrn(mal
, MAL_RXDEIR
);
477 for (i
= 0; i
< regs
->tx_count
; ++i
)
478 regs
->tx_ctpr
[i
] = get_mal_dcrn(mal
, MAL_TXCTPR(i
));
480 for (i
= 0; i
< regs
->rx_count
; ++i
) {
481 regs
->rx_ctpr
[i
] = get_mal_dcrn(mal
, MAL_RXCTPR(i
));
482 regs
->rcbs
[i
] = get_mal_dcrn(mal
, MAL_RCBS(i
));
487 static int __devinit
mal_probe(struct of_device
*ofdev
,
488 const struct of_device_id
*match
)
490 struct mal_instance
*mal
;
491 int err
= 0, i
, bd_size
;
492 int index
= mal_count
++;
493 unsigned int dcr_base
;
497 mal
= kzalloc(sizeof(struct mal_instance
), GFP_KERNEL
);
500 "mal%d: out of memory allocating MAL structure!\n",
506 mal
->version
= of_device_is_compatible(ofdev
->node
, "ibm,mcmal2") ? 2 : 1;
508 MAL_DBG(mal
, "probe" NL
);
510 prop
= of_get_property(ofdev
->node
, "num-tx-chans", NULL
);
513 "mal%d: can't find MAL num-tx-chans property!\n",
518 mal
->num_tx_chans
= prop
[0];
520 prop
= of_get_property(ofdev
->node
, "num-rx-chans", NULL
);
523 "mal%d: can't find MAL num-rx-chans property!\n",
528 mal
->num_rx_chans
= prop
[0];
530 dcr_base
= dcr_resource_start(ofdev
->node
, 0);
533 "mal%d: can't find DCR resource!\n", index
);
537 mal
->dcr_host
= dcr_map(ofdev
->node
, dcr_base
, 0x100);
538 if (!DCR_MAP_OK(mal
->dcr_host
)) {
540 "mal%d: failed to map DCRs !\n", index
);
545 mal
->txeob_irq
= irq_of_parse_and_map(ofdev
->node
, 0);
546 mal
->rxeob_irq
= irq_of_parse_and_map(ofdev
->node
, 1);
547 mal
->serr_irq
= irq_of_parse_and_map(ofdev
->node
, 2);
548 mal
->txde_irq
= irq_of_parse_and_map(ofdev
->node
, 3);
549 mal
->rxde_irq
= irq_of_parse_and_map(ofdev
->node
, 4);
550 if (mal
->txeob_irq
== NO_IRQ
|| mal
->rxeob_irq
== NO_IRQ
||
551 mal
->serr_irq
== NO_IRQ
|| mal
->txde_irq
== NO_IRQ
||
552 mal
->rxde_irq
== NO_IRQ
) {
554 "mal%d: failed to map interrupts !\n", index
);
559 INIT_LIST_HEAD(&mal
->poll_list
);
560 INIT_LIST_HEAD(&mal
->list
);
561 spin_lock_init(&mal
->lock
);
563 netif_napi_add(NULL
, &mal
->napi
, mal_poll
,
564 CONFIG_IBM_NEW_EMAC_POLL_WEIGHT
);
566 /* Load power-on reset defaults */
569 /* Set the MAL configuration register */
570 cfg
= (mal
->version
== 2) ? MAL2_CFG_DEFAULT
: MAL1_CFG_DEFAULT
;
571 cfg
|= MAL_CFG_PLBB
| MAL_CFG_OPBBL
| MAL_CFG_LEA
;
573 /* Current Axon is not happy with priority being non-0, it can
574 * deadlock, fix it up here
576 if (of_device_is_compatible(ofdev
->node
, "ibm,mcmal-axon"))
577 cfg
&= ~(MAL2_CFG_RPP_10
| MAL2_CFG_WPP_10
);
579 /* Apply configuration */
580 set_mal_dcrn(mal
, MAL_CFG
, cfg
);
582 /* Allocate space for BD rings */
583 BUG_ON(mal
->num_tx_chans
<= 0 || mal
->num_tx_chans
> 32);
584 BUG_ON(mal
->num_rx_chans
<= 0 || mal
->num_rx_chans
> 32);
586 bd_size
= sizeof(struct mal_descriptor
) *
587 (NUM_TX_BUFF
* mal
->num_tx_chans
+
588 NUM_RX_BUFF
* mal
->num_rx_chans
);
590 dma_alloc_coherent(&ofdev
->dev
, bd_size
, &mal
->bd_dma
,
592 if (mal
->bd_virt
== NULL
) {
594 "mal%d: out of memory allocating RX/TX descriptors!\n",
599 memset(mal
->bd_virt
, 0, bd_size
);
601 for (i
= 0; i
< mal
->num_tx_chans
; ++i
)
602 set_mal_dcrn(mal
, MAL_TXCTPR(i
), mal
->bd_dma
+
603 sizeof(struct mal_descriptor
) *
604 mal_tx_bd_offset(mal
, i
));
606 for (i
= 0; i
< mal
->num_rx_chans
; ++i
)
607 set_mal_dcrn(mal
, MAL_RXCTPR(i
), mal
->bd_dma
+
608 sizeof(struct mal_descriptor
) *
609 mal_rx_bd_offset(mal
, i
));
611 err
= request_irq(mal
->serr_irq
, mal_serr
, 0, "MAL SERR", mal
);
614 err
= request_irq(mal
->txde_irq
, mal_txde
, 0, "MAL TX DE", mal
);
617 err
= request_irq(mal
->txeob_irq
, mal_txeob
, 0, "MAL TX EOB", mal
);
620 err
= request_irq(mal
->rxde_irq
, mal_rxde
, 0, "MAL RX DE", mal
);
623 err
= request_irq(mal
->rxeob_irq
, mal_rxeob
, 0, "MAL RX EOB", mal
);
627 /* Enable all MAL SERR interrupt sources */
628 if (mal
->version
== 2)
629 set_mal_dcrn(mal
, MAL_IER
, MAL2_IER_EVENTS
);
631 set_mal_dcrn(mal
, MAL_IER
, MAL1_IER_EVENTS
);
633 /* Enable EOB interrupt */
634 mal_enable_eob_irq(mal
);
637 "MAL v%d %s, %d TX channels, %d RX channels\n",
638 mal
->version
, ofdev
->node
->full_name
,
639 mal
->num_tx_chans
, mal
->num_rx_chans
);
641 /* Advertise this instance to the rest of the world */
643 dev_set_drvdata(&ofdev
->dev
, mal
);
645 mal_dbg_register(mal
);
650 free_irq(mal
->rxde_irq
, mal
);
652 free_irq(mal
->txeob_irq
, mal
);
654 free_irq(mal
->txde_irq
, mal
);
656 free_irq(mal
->serr_irq
, mal
);
658 dma_free_coherent(&ofdev
->dev
, bd_size
, mal
->bd_virt
, mal
->bd_dma
);
660 dcr_unmap(mal
->dcr_host
, 0x100);
667 static int __devexit
mal_remove(struct of_device
*ofdev
)
669 struct mal_instance
*mal
= dev_get_drvdata(&ofdev
->dev
);
671 MAL_DBG(mal
, "remove" NL
);
673 /* Synchronize with scheduled polling */
674 napi_disable(&mal
->napi
);
676 if (!list_empty(&mal
->list
)) {
677 /* This is *very* bad */
679 "mal%d: commac list is not empty on remove!\n",
684 dev_set_drvdata(&ofdev
->dev
, NULL
);
686 free_irq(mal
->serr_irq
, mal
);
687 free_irq(mal
->txde_irq
, mal
);
688 free_irq(mal
->txeob_irq
, mal
);
689 free_irq(mal
->rxde_irq
, mal
);
690 free_irq(mal
->rxeob_irq
, mal
);
694 mal_dbg_unregister(mal
);
696 dma_free_coherent(&ofdev
->dev
,
697 sizeof(struct mal_descriptor
) *
698 (NUM_TX_BUFF
* mal
->num_tx_chans
+
699 NUM_RX_BUFF
* mal
->num_rx_chans
), mal
->bd_virt
,
706 static struct of_device_id mal_platform_match
[] =
709 .compatible
= "ibm,mcmal",
712 .compatible
= "ibm,mcmal2",
714 /* Backward compat */
717 .compatible
= "ibm,mcmal",
721 .compatible
= "ibm,mcmal2",
726 static struct of_platform_driver mal_of_driver
= {
728 .match_table
= mal_platform_match
,
731 .remove
= mal_remove
,
734 int __init
mal_init(void)
736 return of_register_platform_driver(&mal_of_driver
);
741 of_unregister_platform_driver(&mal_of_driver
);