2 * Freescale MPC85xx/MPC86xx RapidIO support
4 * Copyright (C) 2007, 2008 Freescale Semiconductor, Inc.
5 * Zhang Wei <wei.zhang@freescale.com>
7 * Copyright 2005 MontaVista Software, Inc.
8 * Matt Porter <mporter@kernel.crashing.org>
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published by the
12 * Free Software Foundation; either version 2 of the License, or (at your
13 * option) any later version.
16 #include <linux/init.h>
17 #include <linux/module.h>
18 #include <linux/types.h>
19 #include <linux/dma-mapping.h>
20 #include <linux/interrupt.h>
21 #include <linux/rio.h>
22 #include <linux/rio_drv.h>
23 #include <linux/of_platform.h>
24 #include <linux/delay.h>
28 /* RapidIO definition irq, which read from OF-tree */
29 #define IRQ_RIO_BELL(m) (((struct rio_priv *)(m->priv))->bellirq)
30 #define IRQ_RIO_TX(m) (((struct rio_priv *)(m->priv))->txirq)
31 #define IRQ_RIO_RX(m) (((struct rio_priv *)(m->priv))->rxirq)
33 #define RIO_ATMU_REGS_OFFSET 0x10c00
34 #define RIO_P_MSG_REGS_OFFSET 0x11000
35 #define RIO_S_MSG_REGS_OFFSET 0x13000
36 #define RIO_ESCSR 0x158
37 #define RIO_CCSR 0x15c
38 #define RIO_ISR_AACR 0x10120
39 #define RIO_ISR_AACR_AA 0x1 /* Accept All ID */
40 #define RIO_MAINT_WIN_SIZE 0x400000
41 #define RIO_DBELL_WIN_SIZE 0x1000
43 #define RIO_MSG_OMR_MUI 0x00000002
44 #define RIO_MSG_OSR_TE 0x00000080
45 #define RIO_MSG_OSR_QOI 0x00000020
46 #define RIO_MSG_OSR_QFI 0x00000010
47 #define RIO_MSG_OSR_MUB 0x00000004
48 #define RIO_MSG_OSR_EOMI 0x00000002
49 #define RIO_MSG_OSR_QEI 0x00000001
51 #define RIO_MSG_IMR_MI 0x00000002
52 #define RIO_MSG_ISR_TE 0x00000080
53 #define RIO_MSG_ISR_QFI 0x00000010
54 #define RIO_MSG_ISR_DIQI 0x00000001
56 #define RIO_MSG_DESC_SIZE 32
57 #define RIO_MSG_BUFFER_SIZE 4096
58 #define RIO_MIN_TX_RING_SIZE 2
59 #define RIO_MAX_TX_RING_SIZE 2048
60 #define RIO_MIN_RX_RING_SIZE 2
61 #define RIO_MAX_RX_RING_SIZE 2048
63 #define DOORBELL_DMR_DI 0x00000002
64 #define DOORBELL_DSR_TE 0x00000080
65 #define DOORBELL_DSR_QFI 0x00000010
66 #define DOORBELL_DSR_DIQI 0x00000001
67 #define DOORBELL_TID_OFFSET 0x02
68 #define DOORBELL_SID_OFFSET 0x04
69 #define DOORBELL_INFO_OFFSET 0x06
71 #define DOORBELL_MESSAGE_SIZE 0x08
72 #define DBELL_SID(x) (*(u16 *)(x + DOORBELL_SID_OFFSET))
73 #define DBELL_TID(x) (*(u16 *)(x + DOORBELL_TID_OFFSET))
74 #define DBELL_INF(x) (*(u16 *)(x + DOORBELL_INFO_OFFSET))
76 struct rio_atmu_regs
{
137 struct rio_dbell_ring
{
142 struct rio_msg_tx_ring
{
145 void *virt_buffer
[RIO_MAX_TX_RING_SIZE
];
146 dma_addr_t phys_buffer
[RIO_MAX_TX_RING_SIZE
];
152 struct rio_msg_rx_ring
{
155 void *virt_buffer
[RIO_MAX_RX_RING_SIZE
];
162 void __iomem
*regs_win
;
163 struct rio_atmu_regs __iomem
*atmu_regs
;
164 struct rio_atmu_regs __iomem
*maint_atmu_regs
;
165 struct rio_atmu_regs __iomem
*dbell_atmu_regs
;
166 void __iomem
*dbell_win
;
167 void __iomem
*maint_win
;
168 struct rio_msg_regs __iomem
*msg_regs
;
169 struct rio_dbell_ring dbell_ring
;
170 struct rio_msg_tx_ring msg_tx_ring
;
171 struct rio_msg_rx_ring msg_rx_ring
;
178 * fsl_rio_doorbell_send - Send a MPC85xx doorbell message
179 * @mport: RapidIO master port info
180 * @index: ID of RapidIO interface
181 * @destid: Destination ID of target device
182 * @data: 16-bit info field of RapidIO doorbell message
184 * Sends a MPC85xx doorbell message. Returns %0 on success or
185 * %-EINVAL on failure.
187 static int fsl_rio_doorbell_send(struct rio_mport
*mport
,
188 int index
, u16 destid
, u16 data
)
190 struct rio_priv
*priv
= mport
->priv
;
191 pr_debug("fsl_doorbell_send: index %d destid %4.4x data %4.4x\n",
192 index
, destid
, data
);
193 switch (mport
->phy_type
) {
194 case RIO_PHY_PARALLEL
:
195 out_be32(&priv
->dbell_atmu_regs
->rowtar
, destid
<< 22);
196 out_be16(priv
->dbell_win
, data
);
199 /* In the serial version silicons, such as MPC8548, MPC8641,
200 * below operations is must be.
202 out_be32(&priv
->msg_regs
->odmr
, 0x00000000);
203 out_be32(&priv
->msg_regs
->odretcr
, 0x00000004);
204 out_be32(&priv
->msg_regs
->oddpr
, destid
<< 16);
205 out_be32(&priv
->msg_regs
->oddatr
, data
);
206 out_be32(&priv
->msg_regs
->odmr
, 0x00000001);
214 * fsl_local_config_read - Generate a MPC85xx local config space read
215 * @mport: RapidIO master port info
216 * @index: ID of RapdiIO interface
217 * @offset: Offset into configuration space
218 * @len: Length (in bytes) of the maintenance transaction
219 * @data: Value to be read into
221 * Generates a MPC85xx local configuration space read. Returns %0 on
222 * success or %-EINVAL on failure.
224 static int fsl_local_config_read(struct rio_mport
*mport
,
225 int index
, u32 offset
, int len
, u32
*data
)
227 struct rio_priv
*priv
= mport
->priv
;
228 pr_debug("fsl_local_config_read: index %d offset %8.8x\n", index
,
230 *data
= in_be32(priv
->regs_win
+ offset
);
236 * fsl_local_config_write - Generate a MPC85xx local config space write
237 * @mport: RapidIO master port info
238 * @index: ID of RapdiIO interface
239 * @offset: Offset into configuration space
240 * @len: Length (in bytes) of the maintenance transaction
241 * @data: Value to be written
243 * Generates a MPC85xx local configuration space write. Returns %0 on
244 * success or %-EINVAL on failure.
246 static int fsl_local_config_write(struct rio_mport
*mport
,
247 int index
, u32 offset
, int len
, u32 data
)
249 struct rio_priv
*priv
= mport
->priv
;
251 ("fsl_local_config_write: index %d offset %8.8x data %8.8x\n",
252 index
, offset
, data
);
253 out_be32(priv
->regs_win
+ offset
, data
);
259 * fsl_rio_config_read - Generate a MPC85xx read maintenance transaction
260 * @mport: RapidIO master port info
261 * @index: ID of RapdiIO interface
262 * @destid: Destination ID of transaction
263 * @hopcount: Number of hops to target device
264 * @offset: Offset into configuration space
265 * @len: Length (in bytes) of the maintenance transaction
266 * @val: Location to be read into
268 * Generates a MPC85xx read maintenance transaction. Returns %0 on
269 * success or %-EINVAL on failure.
272 fsl_rio_config_read(struct rio_mport
*mport
, int index
, u16 destid
,
273 u8 hopcount
, u32 offset
, int len
, u32
*val
)
275 struct rio_priv
*priv
= mport
->priv
;
279 ("fsl_rio_config_read: index %d destid %d hopcount %d offset %8.8x len %d\n",
280 index
, destid
, hopcount
, offset
, len
);
281 out_be32(&priv
->maint_atmu_regs
->rowtar
,
282 (destid
<< 22) | (hopcount
<< 12) | ((offset
& ~0x3) >> 9));
284 data
= (u8
*) priv
->maint_win
+ offset
;
287 *val
= in_8((u8
*) data
);
290 *val
= in_be16((u16
*) data
);
293 *val
= in_be32((u32
*) data
);
301 * fsl_rio_config_write - Generate a MPC85xx write maintenance transaction
302 * @mport: RapidIO master port info
303 * @index: ID of RapdiIO interface
304 * @destid: Destination ID of transaction
305 * @hopcount: Number of hops to target device
306 * @offset: Offset into configuration space
307 * @len: Length (in bytes) of the maintenance transaction
308 * @val: Value to be written
310 * Generates an MPC85xx write maintenance transaction. Returns %0 on
311 * success or %-EINVAL on failure.
314 fsl_rio_config_write(struct rio_mport
*mport
, int index
, u16 destid
,
315 u8 hopcount
, u32 offset
, int len
, u32 val
)
317 struct rio_priv
*priv
= mport
->priv
;
320 ("fsl_rio_config_write: index %d destid %d hopcount %d offset %8.8x len %d val %8.8x\n",
321 index
, destid
, hopcount
, offset
, len
, val
);
322 out_be32(&priv
->maint_atmu_regs
->rowtar
,
323 (destid
<< 22) | (hopcount
<< 12) | ((offset
& ~0x3) >> 9));
325 data
= (u8
*) priv
->maint_win
+ offset
;
328 out_8((u8
*) data
, val
);
331 out_be16((u16
*) data
, val
);
334 out_be32((u32
*) data
, val
);
342 * rio_hw_add_outb_message - Add message to the MPC85xx outbound message queue
343 * @mport: Master port with outbound message queue
344 * @rdev: Target of outbound message
345 * @mbox: Outbound mailbox
346 * @buffer: Message to add to outbound queue
347 * @len: Length of message
349 * Adds the @buffer message to the MPC85xx outbound message queue. Returns
350 * %0 on success or %-EINVAL on failure.
353 rio_hw_add_outb_message(struct rio_mport
*mport
, struct rio_dev
*rdev
, int mbox
,
354 void *buffer
, size_t len
)
356 struct rio_priv
*priv
= mport
->priv
;
358 struct rio_tx_desc
*desc
= (struct rio_tx_desc
*)priv
->msg_tx_ring
.virt
359 + priv
->msg_tx_ring
.tx_slot
;
363 ("RIO: rio_hw_add_outb_message(): destid %4.4x mbox %d buffer %8.8x len %8.8x\n",
364 rdev
->destid
, mbox
, (int)buffer
, len
);
366 if ((len
< 8) || (len
> RIO_MAX_MSG_SIZE
)) {
371 /* Copy and clear rest of buffer */
372 memcpy(priv
->msg_tx_ring
.virt_buffer
[priv
->msg_tx_ring
.tx_slot
], buffer
,
374 if (len
< (RIO_MAX_MSG_SIZE
- 4))
375 memset(priv
->msg_tx_ring
.virt_buffer
[priv
->msg_tx_ring
.tx_slot
]
376 + len
, 0, RIO_MAX_MSG_SIZE
- len
);
378 switch (mport
->phy_type
) {
379 case RIO_PHY_PARALLEL
:
380 /* Set mbox field for message */
381 desc
->dport
= mbox
& 0x3;
383 /* Enable EOMI interrupt, set priority, and set destid */
384 desc
->dattr
= 0x28000000 | (rdev
->destid
<< 2);
387 /* Set mbox field for message, and set destid */
388 desc
->dport
= (rdev
->destid
<< 16) | (mbox
& 0x3);
390 /* Enable EOMI interrupt and priority */
391 desc
->dattr
= 0x28000000;
395 /* Set transfer size aligned to next power of 2 (in double words) */
396 desc
->dwcnt
= is_power_of_2(len
) ? len
: 1 << get_bitmask_order(len
);
398 /* Set snooping and source buffer address */
399 desc
->saddr
= 0x00000004
400 | priv
->msg_tx_ring
.phys_buffer
[priv
->msg_tx_ring
.tx_slot
];
402 /* Increment enqueue pointer */
403 omr
= in_be32(&priv
->msg_regs
->omr
);
404 out_be32(&priv
->msg_regs
->omr
, omr
| RIO_MSG_OMR_MUI
);
406 /* Go to next descriptor */
407 if (++priv
->msg_tx_ring
.tx_slot
== priv
->msg_tx_ring
.size
)
408 priv
->msg_tx_ring
.tx_slot
= 0;
414 EXPORT_SYMBOL_GPL(rio_hw_add_outb_message
);
417 * fsl_rio_tx_handler - MPC85xx outbound message interrupt handler
418 * @irq: Linux interrupt number
419 * @dev_instance: Pointer to interrupt-specific data
421 * Handles outbound message interrupts. Executes a register outbound
422 * mailbox event handler and acks the interrupt occurrence.
425 fsl_rio_tx_handler(int irq
, void *dev_instance
)
428 struct rio_mport
*port
= (struct rio_mport
*)dev_instance
;
429 struct rio_priv
*priv
= port
->priv
;
431 osr
= in_be32(&priv
->msg_regs
->osr
);
433 if (osr
& RIO_MSG_OSR_TE
) {
434 pr_info("RIO: outbound message transmission error\n");
435 out_be32(&priv
->msg_regs
->osr
, RIO_MSG_OSR_TE
);
439 if (osr
& RIO_MSG_OSR_QOI
) {
440 pr_info("RIO: outbound message queue overflow\n");
441 out_be32(&priv
->msg_regs
->osr
, RIO_MSG_OSR_QOI
);
445 if (osr
& RIO_MSG_OSR_EOMI
) {
446 u32 dqp
= in_be32(&priv
->msg_regs
->odqdpar
);
447 int slot
= (dqp
- priv
->msg_tx_ring
.phys
) >> 5;
448 port
->outb_msg
[0].mcback(port
, priv
->msg_tx_ring
.dev_id
, -1,
451 /* Ack the end-of-message interrupt */
452 out_be32(&priv
->msg_regs
->osr
, RIO_MSG_OSR_EOMI
);
460 * rio_open_outb_mbox - Initialize MPC85xx outbound mailbox
461 * @mport: Master port implementing the outbound message unit
462 * @dev_id: Device specific pointer to pass on event
463 * @mbox: Mailbox to open
464 * @entries: Number of entries in the outbound mailbox ring
466 * Initializes buffer ring, request the outbound message interrupt,
467 * and enables the outbound message unit. Returns %0 on success and
468 * %-EINVAL or %-ENOMEM on failure.
470 int rio_open_outb_mbox(struct rio_mport
*mport
, void *dev_id
, int mbox
, int entries
)
473 struct rio_priv
*priv
= mport
->priv
;
475 if ((entries
< RIO_MIN_TX_RING_SIZE
) ||
476 (entries
> RIO_MAX_TX_RING_SIZE
) || (!is_power_of_2(entries
))) {
481 /* Initialize shadow copy ring */
482 priv
->msg_tx_ring
.dev_id
= dev_id
;
483 priv
->msg_tx_ring
.size
= entries
;
485 for (i
= 0; i
< priv
->msg_tx_ring
.size
; i
++) {
486 priv
->msg_tx_ring
.virt_buffer
[i
] =
487 dma_alloc_coherent(NULL
, RIO_MSG_BUFFER_SIZE
,
488 &priv
->msg_tx_ring
.phys_buffer
[i
], GFP_KERNEL
);
489 if (!priv
->msg_tx_ring
.virt_buffer
[i
]) {
491 for (j
= 0; j
< priv
->msg_tx_ring
.size
; j
++)
492 if (priv
->msg_tx_ring
.virt_buffer
[j
])
493 dma_free_coherent(NULL
,
503 /* Initialize outbound message descriptor ring */
504 priv
->msg_tx_ring
.virt
= dma_alloc_coherent(NULL
,
505 priv
->msg_tx_ring
.size
* RIO_MSG_DESC_SIZE
,
506 &priv
->msg_tx_ring
.phys
, GFP_KERNEL
);
507 if (!priv
->msg_tx_ring
.virt
) {
511 memset(priv
->msg_tx_ring
.virt
, 0,
512 priv
->msg_tx_ring
.size
* RIO_MSG_DESC_SIZE
);
513 priv
->msg_tx_ring
.tx_slot
= 0;
515 /* Point dequeue/enqueue pointers at first entry in ring */
516 out_be32(&priv
->msg_regs
->odqdpar
, priv
->msg_tx_ring
.phys
);
517 out_be32(&priv
->msg_regs
->odqepar
, priv
->msg_tx_ring
.phys
);
519 /* Configure for snooping */
520 out_be32(&priv
->msg_regs
->osar
, 0x00000004);
522 /* Clear interrupt status */
523 out_be32(&priv
->msg_regs
->osr
, 0x000000b3);
525 /* Hook up outbound message handler */
526 rc
= request_irq(IRQ_RIO_TX(mport
), fsl_rio_tx_handler
, 0,
527 "msg_tx", (void *)mport
);
532 * Configure outbound message unit
534 * Interrupts (all enabled, except QEIE)
538 out_be32(&priv
->msg_regs
->omr
, 0x00100220);
540 /* Set number of entries */
541 out_be32(&priv
->msg_regs
->omr
,
542 in_be32(&priv
->msg_regs
->omr
) |
543 ((get_bitmask_order(entries
) - 2) << 12));
545 /* Now enable the unit */
546 out_be32(&priv
->msg_regs
->omr
, in_be32(&priv
->msg_regs
->omr
) | 0x1);
552 dma_free_coherent(NULL
, priv
->msg_tx_ring
.size
* RIO_MSG_DESC_SIZE
,
553 priv
->msg_tx_ring
.virt
, priv
->msg_tx_ring
.phys
);
556 for (i
= 0; i
< priv
->msg_tx_ring
.size
; i
++)
557 dma_free_coherent(NULL
, RIO_MSG_BUFFER_SIZE
,
558 priv
->msg_tx_ring
.virt_buffer
[i
],
559 priv
->msg_tx_ring
.phys_buffer
[i
]);
565 * rio_close_outb_mbox - Shut down MPC85xx outbound mailbox
566 * @mport: Master port implementing the outbound message unit
567 * @mbox: Mailbox to close
569 * Disables the outbound message unit, free all buffers, and
570 * frees the outbound message interrupt.
572 void rio_close_outb_mbox(struct rio_mport
*mport
, int mbox
)
574 struct rio_priv
*priv
= mport
->priv
;
575 /* Disable inbound message unit */
576 out_be32(&priv
->msg_regs
->omr
, 0);
579 dma_free_coherent(NULL
, priv
->msg_tx_ring
.size
* RIO_MSG_DESC_SIZE
,
580 priv
->msg_tx_ring
.virt
, priv
->msg_tx_ring
.phys
);
583 free_irq(IRQ_RIO_TX(mport
), (void *)mport
);
587 * fsl_rio_rx_handler - MPC85xx inbound message interrupt handler
588 * @irq: Linux interrupt number
589 * @dev_instance: Pointer to interrupt-specific data
591 * Handles inbound message interrupts. Executes a registered inbound
592 * mailbox event handler and acks the interrupt occurrence.
595 fsl_rio_rx_handler(int irq
, void *dev_instance
)
598 struct rio_mport
*port
= (struct rio_mport
*)dev_instance
;
599 struct rio_priv
*priv
= port
->priv
;
601 isr
= in_be32(&priv
->msg_regs
->isr
);
603 if (isr
& RIO_MSG_ISR_TE
) {
604 pr_info("RIO: inbound message reception error\n");
605 out_be32((void *)&priv
->msg_regs
->isr
, RIO_MSG_ISR_TE
);
609 /* XXX Need to check/dispatch until queue empty */
610 if (isr
& RIO_MSG_ISR_DIQI
) {
612 * We implement *only* mailbox 0, but can receive messages
613 * for any mailbox/letter to that mailbox destination. So,
614 * make the callback with an unknown/invalid mailbox number
617 port
->inb_msg
[0].mcback(port
, priv
->msg_rx_ring
.dev_id
, -1, -1);
619 /* Ack the queueing interrupt */
620 out_be32(&priv
->msg_regs
->isr
, RIO_MSG_ISR_DIQI
);
628 * rio_open_inb_mbox - Initialize MPC85xx inbound mailbox
629 * @mport: Master port implementing the inbound message unit
630 * @dev_id: Device specific pointer to pass on event
631 * @mbox: Mailbox to open
632 * @entries: Number of entries in the inbound mailbox ring
634 * Initializes buffer ring, request the inbound message interrupt,
635 * and enables the inbound message unit. Returns %0 on success
636 * and %-EINVAL or %-ENOMEM on failure.
638 int rio_open_inb_mbox(struct rio_mport
*mport
, void *dev_id
, int mbox
, int entries
)
641 struct rio_priv
*priv
= mport
->priv
;
643 if ((entries
< RIO_MIN_RX_RING_SIZE
) ||
644 (entries
> RIO_MAX_RX_RING_SIZE
) || (!is_power_of_2(entries
))) {
649 /* Initialize client buffer ring */
650 priv
->msg_rx_ring
.dev_id
= dev_id
;
651 priv
->msg_rx_ring
.size
= entries
;
652 priv
->msg_rx_ring
.rx_slot
= 0;
653 for (i
= 0; i
< priv
->msg_rx_ring
.size
; i
++)
654 priv
->msg_rx_ring
.virt_buffer
[i
] = NULL
;
656 /* Initialize inbound message ring */
657 priv
->msg_rx_ring
.virt
= dma_alloc_coherent(NULL
,
658 priv
->msg_rx_ring
.size
* RIO_MAX_MSG_SIZE
,
659 &priv
->msg_rx_ring
.phys
, GFP_KERNEL
);
660 if (!priv
->msg_rx_ring
.virt
) {
665 /* Point dequeue/enqueue pointers at first entry in ring */
666 out_be32(&priv
->msg_regs
->ifqdpar
, (u32
) priv
->msg_rx_ring
.phys
);
667 out_be32(&priv
->msg_regs
->ifqepar
, (u32
) priv
->msg_rx_ring
.phys
);
669 /* Clear interrupt status */
670 out_be32(&priv
->msg_regs
->isr
, 0x00000091);
672 /* Hook up inbound message handler */
673 rc
= request_irq(IRQ_RIO_RX(mport
), fsl_rio_rx_handler
, 0,
674 "msg_rx", (void *)mport
);
676 dma_free_coherent(NULL
, RIO_MSG_BUFFER_SIZE
,
677 priv
->msg_tx_ring
.virt_buffer
[i
],
678 priv
->msg_tx_ring
.phys_buffer
[i
]);
683 * Configure inbound message unit:
685 * 4KB max message size
686 * Unmask all interrupt sources
689 out_be32(&priv
->msg_regs
->imr
, 0x001b0060);
691 /* Set number of queue entries */
692 setbits32(&priv
->msg_regs
->imr
, (get_bitmask_order(entries
) - 2) << 12);
694 /* Now enable the unit */
695 setbits32(&priv
->msg_regs
->imr
, 0x1);
702 * rio_close_inb_mbox - Shut down MPC85xx inbound mailbox
703 * @mport: Master port implementing the inbound message unit
704 * @mbox: Mailbox to close
706 * Disables the inbound message unit, free all buffers, and
707 * frees the inbound message interrupt.
709 void rio_close_inb_mbox(struct rio_mport
*mport
, int mbox
)
711 struct rio_priv
*priv
= mport
->priv
;
712 /* Disable inbound message unit */
713 out_be32(&priv
->msg_regs
->imr
, 0);
716 dma_free_coherent(NULL
, priv
->msg_rx_ring
.size
* RIO_MAX_MSG_SIZE
,
717 priv
->msg_rx_ring
.virt
, priv
->msg_rx_ring
.phys
);
720 free_irq(IRQ_RIO_RX(mport
), (void *)mport
);
724 * rio_hw_add_inb_buffer - Add buffer to the MPC85xx inbound message queue
725 * @mport: Master port implementing the inbound message unit
726 * @mbox: Inbound mailbox number
727 * @buf: Buffer to add to inbound queue
729 * Adds the @buf buffer to the MPC85xx inbound message queue. Returns
730 * %0 on success or %-EINVAL on failure.
732 int rio_hw_add_inb_buffer(struct rio_mport
*mport
, int mbox
, void *buf
)
735 struct rio_priv
*priv
= mport
->priv
;
737 pr_debug("RIO: rio_hw_add_inb_buffer(), msg_rx_ring.rx_slot %d\n",
738 priv
->msg_rx_ring
.rx_slot
);
740 if (priv
->msg_rx_ring
.virt_buffer
[priv
->msg_rx_ring
.rx_slot
]) {
742 "RIO: error adding inbound buffer %d, buffer exists\n",
743 priv
->msg_rx_ring
.rx_slot
);
748 priv
->msg_rx_ring
.virt_buffer
[priv
->msg_rx_ring
.rx_slot
] = buf
;
749 if (++priv
->msg_rx_ring
.rx_slot
== priv
->msg_rx_ring
.size
)
750 priv
->msg_rx_ring
.rx_slot
= 0;
756 EXPORT_SYMBOL_GPL(rio_hw_add_inb_buffer
);
759 * rio_hw_get_inb_message - Fetch inbound message from the MPC85xx message unit
760 * @mport: Master port implementing the inbound message unit
761 * @mbox: Inbound mailbox number
763 * Gets the next available inbound message from the inbound message queue.
764 * A pointer to the message is returned on success or NULL on failure.
766 void *rio_hw_get_inb_message(struct rio_mport
*mport
, int mbox
)
768 struct rio_priv
*priv
= mport
->priv
;
769 u32 phys_buf
, virt_buf
;
773 phys_buf
= in_be32(&priv
->msg_regs
->ifqdpar
);
775 /* If no more messages, then bail out */
776 if (phys_buf
== in_be32(&priv
->msg_regs
->ifqepar
))
779 virt_buf
= (u32
) priv
->msg_rx_ring
.virt
+ (phys_buf
780 - priv
->msg_rx_ring
.phys
);
781 buf_idx
= (phys_buf
- priv
->msg_rx_ring
.phys
) / RIO_MAX_MSG_SIZE
;
782 buf
= priv
->msg_rx_ring
.virt_buffer
[buf_idx
];
786 "RIO: inbound message copy failed, no buffers\n");
790 /* Copy max message size, caller is expected to allocate that big */
791 memcpy(buf
, (void *)virt_buf
, RIO_MAX_MSG_SIZE
);
793 /* Clear the available buffer */
794 priv
->msg_rx_ring
.virt_buffer
[buf_idx
] = NULL
;
797 setbits32(&priv
->msg_regs
->imr
, RIO_MSG_IMR_MI
);
803 EXPORT_SYMBOL_GPL(rio_hw_get_inb_message
);
806 * fsl_rio_dbell_handler - MPC85xx doorbell interrupt handler
807 * @irq: Linux interrupt number
808 * @dev_instance: Pointer to interrupt-specific data
810 * Handles doorbell interrupts. Parses a list of registered
811 * doorbell event handlers and executes a matching event handler.
814 fsl_rio_dbell_handler(int irq
, void *dev_instance
)
817 struct rio_mport
*port
= (struct rio_mport
*)dev_instance
;
818 struct rio_priv
*priv
= port
->priv
;
820 dsr
= in_be32(&priv
->msg_regs
->dsr
);
822 if (dsr
& DOORBELL_DSR_TE
) {
823 pr_info("RIO: doorbell reception error\n");
824 out_be32(&priv
->msg_regs
->dsr
, DOORBELL_DSR_TE
);
828 if (dsr
& DOORBELL_DSR_QFI
) {
829 pr_info("RIO: doorbell queue full\n");
830 out_be32(&priv
->msg_regs
->dsr
, DOORBELL_DSR_QFI
);
834 /* XXX Need to check/dispatch until queue empty */
835 if (dsr
& DOORBELL_DSR_DIQI
) {
837 (u32
) priv
->dbell_ring
.virt
+
838 (in_be32(&priv
->msg_regs
->dqdpar
) & 0xfff);
839 struct rio_dbell
*dbell
;
843 ("RIO: processing doorbell, sid %2.2x tid %2.2x info %4.4x\n",
844 DBELL_SID(dmsg
), DBELL_TID(dmsg
), DBELL_INF(dmsg
));
846 list_for_each_entry(dbell
, &port
->dbells
, node
) {
847 if ((dbell
->res
->start
<= DBELL_INF(dmsg
)) &&
848 (dbell
->res
->end
>= DBELL_INF(dmsg
))) {
854 dbell
->dinb(port
, dbell
->dev_id
, DBELL_SID(dmsg
), DBELL_TID(dmsg
),
858 ("RIO: spurious doorbell, sid %2.2x tid %2.2x info %4.4x\n",
859 DBELL_SID(dmsg
), DBELL_TID(dmsg
), DBELL_INF(dmsg
));
861 setbits32(&priv
->msg_regs
->dmr
, DOORBELL_DMR_DI
);
862 out_be32(&priv
->msg_regs
->dsr
, DOORBELL_DSR_DIQI
);
870 * fsl_rio_doorbell_init - MPC85xx doorbell interface init
871 * @mport: Master port implementing the inbound doorbell unit
873 * Initializes doorbell unit hardware and inbound DMA buffer
874 * ring. Called from fsl_rio_setup(). Returns %0 on success
875 * or %-ENOMEM on failure.
877 static int fsl_rio_doorbell_init(struct rio_mport
*mport
)
879 struct rio_priv
*priv
= mport
->priv
;
882 /* Map outbound doorbell window immediately after maintenance window */
883 priv
->dbell_win
= ioremap(mport
->iores
.start
+ RIO_MAINT_WIN_SIZE
,
885 if (!priv
->dbell_win
) {
887 "RIO: unable to map outbound doorbell window\n");
892 /* Initialize inbound doorbells */
893 priv
->dbell_ring
.virt
= dma_alloc_coherent(NULL
, 512 *
894 DOORBELL_MESSAGE_SIZE
, &priv
->dbell_ring
.phys
, GFP_KERNEL
);
895 if (!priv
->dbell_ring
.virt
) {
896 printk(KERN_ERR
"RIO: unable allocate inbound doorbell ring\n");
898 iounmap(priv
->dbell_win
);
902 /* Point dequeue/enqueue pointers at first entry in ring */
903 out_be32(&priv
->msg_regs
->dqdpar
, (u32
) priv
->dbell_ring
.phys
);
904 out_be32(&priv
->msg_regs
->dqepar
, (u32
) priv
->dbell_ring
.phys
);
906 /* Clear interrupt status */
907 out_be32(&priv
->msg_regs
->dsr
, 0x00000091);
909 /* Hook up doorbell handler */
910 rc
= request_irq(IRQ_RIO_BELL(mport
), fsl_rio_dbell_handler
, 0,
911 "dbell_rx", (void *)mport
);
913 iounmap(priv
->dbell_win
);
914 dma_free_coherent(NULL
, 512 * DOORBELL_MESSAGE_SIZE
,
915 priv
->dbell_ring
.virt
, priv
->dbell_ring
.phys
);
917 "MPC85xx RIO: unable to request inbound doorbell irq");
921 /* Configure doorbells for snooping, 512 entries, and enable */
922 out_be32(&priv
->msg_regs
->dmr
, 0x00108161);
928 static char *cmdline
= NULL
;
930 static int fsl_rio_get_hdid(int index
)
932 /* XXX Need to parse multiple entries in some format */
936 return simple_strtol(cmdline
, NULL
, 0);
939 static int fsl_rio_get_cmdline(char *s
)
948 __setup("riohdid=", fsl_rio_get_cmdline
);
950 static inline void fsl_rio_info(struct device
*dev
, u32 ccsr
)
955 switch (ccsr
>> 30) {
966 dev_info(dev
, "Hardware port width: %s\n", str
);
968 switch ((ccsr
>> 27) & 7) {
970 str
= "Single-lane 0";
973 str
= "Single-lane 2";
982 dev_info(dev
, "Training connection status: %s\n", str
);
985 if (!(ccsr
& 0x80000000))
986 dev_info(dev
, "Output port operating in 8-bit mode\n");
987 if (!(ccsr
& 0x08000000))
988 dev_info(dev
, "Input port operating in 8-bit mode\n");
993 * fsl_rio_setup - Setup Freescale PowerPC RapidIO interface
994 * @dev: of_device pointer
996 * Initializes MPC85xx RapidIO hardware interface, configures
997 * master port with system-specific info, and registers the
998 * master port with the RapidIO subsystem.
1000 int fsl_rio_setup(struct of_device
*dev
)
1002 struct rio_ops
*ops
;
1003 struct rio_mport
*port
;
1004 struct rio_priv
*priv
;
1006 const u32
*dt_range
, *cell
;
1007 struct resource regs
;
1010 u64 law_start
, law_size
;
1014 dev_err(&dev
->dev
, "Device OF-Node is NULL");
1018 rc
= of_address_to_resource(dev
->node
, 0, ®s
);
1020 dev_err(&dev
->dev
, "Can't get %s property 'reg'\n",
1021 dev
->node
->full_name
);
1024 dev_info(&dev
->dev
, "Of-device full name %s\n", dev
->node
->full_name
);
1025 dev_info(&dev
->dev
, "Regs start 0x%08x size 0x%08x\n", regs
.start
,
1026 regs
.end
- regs
.start
+ 1);
1028 dt_range
= of_get_property(dev
->node
, "ranges", &rlen
);
1030 dev_err(&dev
->dev
, "Can't get %s property 'ranges'\n",
1031 dev
->node
->full_name
);
1035 /* Get node address wide */
1036 cell
= of_get_property(dev
->node
, "#address-cells", NULL
);
1040 aw
= of_n_addr_cells(dev
->node
);
1041 /* Get node size wide */
1042 cell
= of_get_property(dev
->node
, "#size-cells", NULL
);
1046 sw
= of_n_size_cells(dev
->node
);
1047 /* Get parent address wide wide */
1048 paw
= of_n_addr_cells(dev
->node
);
1050 law_start
= of_read_number(dt_range
+ aw
, paw
);
1051 law_size
= of_read_number(dt_range
+ aw
+ paw
, sw
);
1053 dev_info(&dev
->dev
, "LAW start 0x%016llx, size 0x%016llx.\n",
1054 law_start
, law_size
);
1056 ops
= kmalloc(sizeof(struct rio_ops
), GFP_KERNEL
);
1057 ops
->lcread
= fsl_local_config_read
;
1058 ops
->lcwrite
= fsl_local_config_write
;
1059 ops
->cread
= fsl_rio_config_read
;
1060 ops
->cwrite
= fsl_rio_config_write
;
1061 ops
->dsend
= fsl_rio_doorbell_send
;
1063 port
= kzalloc(sizeof(struct rio_mport
), GFP_KERNEL
);
1067 priv
= kzalloc(sizeof(struct rio_priv
), GFP_KERNEL
);
1069 printk(KERN_ERR
"Can't alloc memory for 'priv'\n");
1074 INIT_LIST_HEAD(&port
->dbells
);
1075 port
->iores
.start
= law_start
;
1076 port
->iores
.end
= law_start
+ law_size
;
1077 port
->iores
.flags
= IORESOURCE_MEM
;
1079 priv
->bellirq
= irq_of_parse_and_map(dev
->node
, 2);
1080 priv
->txirq
= irq_of_parse_and_map(dev
->node
, 3);
1081 priv
->rxirq
= irq_of_parse_and_map(dev
->node
, 4);
1082 dev_info(&dev
->dev
, "bellirq: %d, txirq: %d, rxirq %d\n", priv
->bellirq
,
1083 priv
->txirq
, priv
->rxirq
);
1085 rio_init_dbell_res(&port
->riores
[RIO_DOORBELL_RESOURCE
], 0, 0xffff);
1086 rio_init_mbox_res(&port
->riores
[RIO_INB_MBOX_RESOURCE
], 0, 0);
1087 rio_init_mbox_res(&port
->riores
[RIO_OUTB_MBOX_RESOURCE
], 0, 0);
1088 strcpy(port
->name
, "RIO0 mport");
1091 port
->host_deviceid
= fsl_rio_get_hdid(port
->id
);
1094 rio_register_mport(port
);
1096 priv
->regs_win
= ioremap(regs
.start
, regs
.end
- regs
.start
+ 1);
1098 /* Probe the master port phy type */
1099 ccsr
= in_be32(priv
->regs_win
+ RIO_CCSR
);
1100 port
->phy_type
= (ccsr
& 1) ? RIO_PHY_SERIAL
: RIO_PHY_PARALLEL
;
1101 dev_info(&dev
->dev
, "RapidIO PHY type: %s\n",
1102 (port
->phy_type
== RIO_PHY_PARALLEL
) ? "parallel" :
1103 ((port
->phy_type
== RIO_PHY_SERIAL
) ? "serial" :
1105 /* Checking the port training status */
1106 if (in_be32((priv
->regs_win
+ RIO_ESCSR
)) & 1) {
1107 dev_err(&dev
->dev
, "Port is not ready. "
1108 "Try to restart connection...\n");
1109 switch (port
->phy_type
) {
1110 case RIO_PHY_SERIAL
:
1112 out_be32(priv
->regs_win
+ RIO_CCSR
, 0);
1114 setbits32(priv
->regs_win
+ RIO_CCSR
, 0x02000000);
1116 setbits32(priv
->regs_win
+ RIO_CCSR
, 0x00600000);
1118 case RIO_PHY_PARALLEL
:
1120 out_be32(priv
->regs_win
+ RIO_CCSR
, 0x22000000);
1122 out_be32(priv
->regs_win
+ RIO_CCSR
, 0x44000000);
1126 if (in_be32((priv
->regs_win
+ RIO_ESCSR
)) & 1) {
1127 dev_err(&dev
->dev
, "Port restart failed.\n");
1131 dev_info(&dev
->dev
, "Port restart success!\n");
1133 fsl_rio_info(&dev
->dev
, ccsr
);
1135 port
->sys_size
= (in_be32((priv
->regs_win
+ RIO_PEF_CAR
))
1136 & RIO_PEF_CTLS
) >> 4;
1137 dev_info(&dev
->dev
, "RapidIO Common Transport System size: %d\n",
1138 port
->sys_size
? 65536 : 256);
1140 priv
->atmu_regs
= (struct rio_atmu_regs
*)(priv
->regs_win
1141 + RIO_ATMU_REGS_OFFSET
);
1142 priv
->maint_atmu_regs
= priv
->atmu_regs
+ 1;
1143 priv
->dbell_atmu_regs
= priv
->atmu_regs
+ 2;
1144 priv
->msg_regs
= (struct rio_msg_regs
*)(priv
->regs_win
+
1145 ((port
->phy_type
== RIO_PHY_SERIAL
) ?
1146 RIO_S_MSG_REGS_OFFSET
: RIO_P_MSG_REGS_OFFSET
));
1148 /* Set to receive any dist ID for serial RapidIO controller. */
1149 if (port
->phy_type
== RIO_PHY_SERIAL
)
1150 out_be32((priv
->regs_win
+ RIO_ISR_AACR
), RIO_ISR_AACR_AA
);
1152 /* Configure maintenance transaction window */
1153 out_be32(&priv
->maint_atmu_regs
->rowbar
, 0x000c0000);
1154 out_be32(&priv
->maint_atmu_regs
->rowar
, 0x80077015);
1156 priv
->maint_win
= ioremap(law_start
, RIO_MAINT_WIN_SIZE
);
1158 /* Configure outbound doorbell window */
1159 out_be32(&priv
->dbell_atmu_regs
->rowbar
, 0x000c0400);
1160 out_be32(&priv
->dbell_atmu_regs
->rowar
, 0x8004200b);
1161 fsl_rio_doorbell_init(port
);
1166 iounmap(priv
->regs_win
);
1173 /* The probe function for RapidIO peer-to-peer network.
1175 static int __devinit
fsl_of_rio_rpn_probe(struct of_device
*dev
,
1176 const struct of_device_id
*match
)
1179 printk(KERN_INFO
"Setting up RapidIO peer-to-peer network %s\n",
1180 dev
->node
->full_name
);
1182 rc
= fsl_rio_setup(dev
);
1186 /* Enumerate all registered ports */
1187 rc
= rio_init_mports();
1192 static const struct of_device_id fsl_of_rio_rpn_ids
[] = {
1194 .compatible
= "fsl,rapidio-delta",
1199 static struct of_platform_driver fsl_of_rio_rpn_driver
= {
1200 .name
= "fsl-of-rio",
1201 .match_table
= fsl_of_rio_rpn_ids
,
1202 .probe
= fsl_of_rio_rpn_probe
,
1205 static __init
int fsl_of_rio_rpn_init(void)
1207 return of_register_platform_driver(&fsl_of_rio_rpn_driver
);
1210 subsys_initcall(fsl_of_rio_rpn_init
);