blob: 14c106cf4aa2c629c307beca7a8873404dcbff02 [file] [log] [blame]
Matt Porter2b0c28d7f2005-11-07 01:00:19 -08001/*
Zhang Weid02443a2008-04-18 13:33:38 -07002 * Freescale MPC85xx/MPC86xx RapidIO support
Matt Porter2b0c28d7f2005-11-07 01:00:19 -08003 *
Zhang Weiad1e9382008-04-18 13:33:41 -07004 * Copyright (C) 2007, 2008 Freescale Semiconductor, Inc.
5 * Zhang Wei <wei.zhang@freescale.com>
6 *
Matt Porter2b0c28d7f2005-11-07 01:00:19 -08007 * Copyright 2005 MontaVista Software, Inc.
8 * Matt Porter <mporter@kernel.crashing.org>
9 *
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.
14 */
15
Matt Porter2b0c28d7f2005-11-07 01:00:19 -080016#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>
Zhang Weicc2bb692008-04-18 13:33:41 -070023#include <linux/of_platform.h>
Matt Porter2b0c28d7f2005-11-07 01:00:19 -080024
25#include <asm/io.h>
26
Zhang Weiad1e9382008-04-18 13:33:41 -070027/* RapidIO definition irq, which read from OF-tree */
28#define IRQ_RIO_BELL(m) (((struct rio_priv *)(m->priv))->bellirq)
29#define IRQ_RIO_TX(m) (((struct rio_priv *)(m->priv))->txirq)
30#define IRQ_RIO_RX(m) (((struct rio_priv *)(m->priv))->rxirq)
31
Matt Porter2b0c28d7f2005-11-07 01:00:19 -080032#define RIO_ATMU_REGS_OFFSET 0x10c00
33#define RIO_MSG_REGS_OFFSET 0x11000
34#define RIO_MAINT_WIN_SIZE 0x400000
35#define RIO_DBELL_WIN_SIZE 0x1000
36
37#define RIO_MSG_OMR_MUI 0x00000002
38#define RIO_MSG_OSR_TE 0x00000080
39#define RIO_MSG_OSR_QOI 0x00000020
40#define RIO_MSG_OSR_QFI 0x00000010
41#define RIO_MSG_OSR_MUB 0x00000004
42#define RIO_MSG_OSR_EOMI 0x00000002
43#define RIO_MSG_OSR_QEI 0x00000001
44
45#define RIO_MSG_IMR_MI 0x00000002
46#define RIO_MSG_ISR_TE 0x00000080
47#define RIO_MSG_ISR_QFI 0x00000010
48#define RIO_MSG_ISR_DIQI 0x00000001
49
50#define RIO_MSG_DESC_SIZE 32
51#define RIO_MSG_BUFFER_SIZE 4096
52#define RIO_MIN_TX_RING_SIZE 2
53#define RIO_MAX_TX_RING_SIZE 2048
54#define RIO_MIN_RX_RING_SIZE 2
55#define RIO_MAX_RX_RING_SIZE 2048
56
57#define DOORBELL_DMR_DI 0x00000002
58#define DOORBELL_DSR_TE 0x00000080
59#define DOORBELL_DSR_QFI 0x00000010
60#define DOORBELL_DSR_DIQI 0x00000001
61#define DOORBELL_TID_OFFSET 0x03
62#define DOORBELL_SID_OFFSET 0x05
63#define DOORBELL_INFO_OFFSET 0x06
64
65#define DOORBELL_MESSAGE_SIZE 0x08
66#define DBELL_SID(x) (*(u8 *)(x + DOORBELL_SID_OFFSET))
67#define DBELL_TID(x) (*(u8 *)(x + DOORBELL_TID_OFFSET))
68#define DBELL_INF(x) (*(u16 *)(x + DOORBELL_INFO_OFFSET))
69
Matt Porter2b0c28d7f2005-11-07 01:00:19 -080070struct rio_atmu_regs {
71 u32 rowtar;
72 u32 pad1;
73 u32 rowbar;
74 u32 pad2;
75 u32 rowar;
76 u32 pad3[3];
77};
78
79struct rio_msg_regs {
80 u32 omr;
81 u32 osr;
82 u32 pad1;
83 u32 odqdpar;
84 u32 pad2;
85 u32 osar;
86 u32 odpr;
87 u32 odatr;
88 u32 odcr;
89 u32 pad3;
90 u32 odqepar;
91 u32 pad4[13];
92 u32 imr;
93 u32 isr;
94 u32 pad5;
95 u32 ifqdpar;
96 u32 pad6;
97 u32 ifqepar;
98 u32 pad7[250];
99 u32 dmr;
100 u32 dsr;
101 u32 pad8;
102 u32 dqdpar;
103 u32 pad9;
104 u32 dqepar;
105 u32 pad10[26];
106 u32 pwmr;
107 u32 pwsr;
108 u32 pad11;
109 u32 pwqbar;
110};
111
112struct rio_tx_desc {
113 u32 res1;
114 u32 saddr;
115 u32 dport;
116 u32 dattr;
117 u32 res2;
118 u32 res3;
119 u32 dwcnt;
120 u32 res4;
121};
122
Zhang Weiad1e9382008-04-18 13:33:41 -0700123struct rio_dbell_ring {
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800124 void *virt;
125 dma_addr_t phys;
Zhang Weiad1e9382008-04-18 13:33:41 -0700126};
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800127
Zhang Weiad1e9382008-04-18 13:33:41 -0700128struct rio_msg_tx_ring {
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800129 void *virt;
130 dma_addr_t phys;
131 void *virt_buffer[RIO_MAX_TX_RING_SIZE];
132 dma_addr_t phys_buffer[RIO_MAX_TX_RING_SIZE];
133 int tx_slot;
134 int size;
Matt Porter6978bbc2005-11-07 01:00:20 -0800135 void *dev_id;
Zhang Weiad1e9382008-04-18 13:33:41 -0700136};
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800137
Zhang Weiad1e9382008-04-18 13:33:41 -0700138struct rio_msg_rx_ring {
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800139 void *virt;
140 dma_addr_t phys;
141 void *virt_buffer[RIO_MAX_RX_RING_SIZE];
142 int rx_slot;
143 int size;
Matt Porter6978bbc2005-11-07 01:00:20 -0800144 void *dev_id;
Zhang Weiad1e9382008-04-18 13:33:41 -0700145};
146
147struct rio_priv {
148 void __iomem *regs_win;
149 struct rio_atmu_regs __iomem *atmu_regs;
150 struct rio_atmu_regs __iomem *maint_atmu_regs;
151 struct rio_atmu_regs __iomem *dbell_atmu_regs;
152 void __iomem *dbell_win;
153 void __iomem *maint_win;
154 struct rio_msg_regs __iomem *msg_regs;
155 struct rio_dbell_ring dbell_ring;
156 struct rio_msg_tx_ring msg_tx_ring;
157 struct rio_msg_rx_ring msg_rx_ring;
158 int bellirq;
159 int txirq;
160 int rxirq;
161};
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800162
163/**
Zhang Weid02443a2008-04-18 13:33:38 -0700164 * fsl_rio_doorbell_send - Send a MPC85xx doorbell message
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800165 * @index: ID of RapidIO interface
166 * @destid: Destination ID of target device
167 * @data: 16-bit info field of RapidIO doorbell message
168 *
169 * Sends a MPC85xx doorbell message. Returns %0 on success or
170 * %-EINVAL on failure.
171 */
Zhang Weiad1e9382008-04-18 13:33:41 -0700172static int fsl_rio_doorbell_send(struct rio_mport *mport,
173 int index, u16 destid, u16 data)
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800174{
Zhang Weiad1e9382008-04-18 13:33:41 -0700175 struct rio_priv *priv = mport->priv;
Zhang Weid02443a2008-04-18 13:33:38 -0700176 pr_debug("fsl_doorbell_send: index %d destid %4.4x data %4.4x\n",
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800177 index, destid, data);
Zhang Weiad1e9382008-04-18 13:33:41 -0700178 out_be32(&priv->dbell_atmu_regs->rowtar, destid << 22);
179 out_be16(priv->dbell_win, data);
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800180
181 return 0;
182}
183
184/**
Zhang Weid02443a2008-04-18 13:33:38 -0700185 * fsl_local_config_read - Generate a MPC85xx local config space read
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800186 * @index: ID of RapdiIO interface
187 * @offset: Offset into configuration space
188 * @len: Length (in bytes) of the maintenance transaction
189 * @data: Value to be read into
190 *
191 * Generates a MPC85xx local configuration space read. Returns %0 on
192 * success or %-EINVAL on failure.
193 */
Zhang Weiad1e9382008-04-18 13:33:41 -0700194static int fsl_local_config_read(struct rio_mport *mport,
195 int index, u32 offset, int len, u32 *data)
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800196{
Zhang Weiad1e9382008-04-18 13:33:41 -0700197 struct rio_priv *priv = mport->priv;
Zhang Weid02443a2008-04-18 13:33:38 -0700198 pr_debug("fsl_local_config_read: index %d offset %8.8x\n", index,
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800199 offset);
Zhang Weiad1e9382008-04-18 13:33:41 -0700200 *data = in_be32(priv->regs_win + offset);
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800201
202 return 0;
203}
204
205/**
Zhang Weid02443a2008-04-18 13:33:38 -0700206 * fsl_local_config_write - Generate a MPC85xx local config space write
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800207 * @index: ID of RapdiIO interface
208 * @offset: Offset into configuration space
209 * @len: Length (in bytes) of the maintenance transaction
210 * @data: Value to be written
211 *
212 * Generates a MPC85xx local configuration space write. Returns %0 on
213 * success or %-EINVAL on failure.
214 */
Zhang Weiad1e9382008-04-18 13:33:41 -0700215static int fsl_local_config_write(struct rio_mport *mport,
216 int index, u32 offset, int len, u32 data)
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800217{
Zhang Weiad1e9382008-04-18 13:33:41 -0700218 struct rio_priv *priv = mport->priv;
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800219 pr_debug
Zhang Weid02443a2008-04-18 13:33:38 -0700220 ("fsl_local_config_write: index %d offset %8.8x data %8.8x\n",
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800221 index, offset, data);
Zhang Weiad1e9382008-04-18 13:33:41 -0700222 out_be32(priv->regs_win + offset, data);
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800223
224 return 0;
225}
226
227/**
Zhang Weid02443a2008-04-18 13:33:38 -0700228 * fsl_rio_config_read - Generate a MPC85xx read maintenance transaction
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800229 * @index: ID of RapdiIO interface
230 * @destid: Destination ID of transaction
231 * @hopcount: Number of hops to target device
232 * @offset: Offset into configuration space
233 * @len: Length (in bytes) of the maintenance transaction
234 * @val: Location to be read into
235 *
236 * Generates a MPC85xx read maintenance transaction. Returns %0 on
237 * success or %-EINVAL on failure.
238 */
239static int
Zhang Weiad1e9382008-04-18 13:33:41 -0700240fsl_rio_config_read(struct rio_mport *mport, int index, u16 destid,
241 u8 hopcount, u32 offset, int len, u32 *val)
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800242{
Zhang Weiad1e9382008-04-18 13:33:41 -0700243 struct rio_priv *priv = mport->priv;
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800244 u8 *data;
245
246 pr_debug
Zhang Weid02443a2008-04-18 13:33:38 -0700247 ("fsl_rio_config_read: index %d destid %d hopcount %d offset %8.8x len %d\n",
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800248 index, destid, hopcount, offset, len);
Zhang Weiad1e9382008-04-18 13:33:41 -0700249 out_be32(&priv->maint_atmu_regs->rowtar,
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800250 (destid << 22) | (hopcount << 12) | ((offset & ~0x3) >> 9));
251
Zhang Weiad1e9382008-04-18 13:33:41 -0700252 data = (u8 *) priv->maint_win + offset;
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800253 switch (len) {
254 case 1:
255 *val = in_8((u8 *) data);
256 break;
257 case 2:
258 *val = in_be16((u16 *) data);
259 break;
260 default:
261 *val = in_be32((u32 *) data);
262 break;
263 }
264
265 return 0;
266}
267
268/**
Zhang Weid02443a2008-04-18 13:33:38 -0700269 * fsl_rio_config_write - Generate a MPC85xx write maintenance transaction
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800270 * @index: ID of RapdiIO interface
271 * @destid: Destination ID of transaction
272 * @hopcount: Number of hops to target device
273 * @offset: Offset into configuration space
274 * @len: Length (in bytes) of the maintenance transaction
275 * @val: Value to be written
276 *
277 * Generates an MPC85xx write maintenance transaction. Returns %0 on
278 * success or %-EINVAL on failure.
279 */
280static int
Zhang Weiad1e9382008-04-18 13:33:41 -0700281fsl_rio_config_write(struct rio_mport *mport, int index, u16 destid,
282 u8 hopcount, u32 offset, int len, u32 val)
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800283{
Zhang Weiad1e9382008-04-18 13:33:41 -0700284 struct rio_priv *priv = mport->priv;
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800285 u8 *data;
286 pr_debug
Zhang Weid02443a2008-04-18 13:33:38 -0700287 ("fsl_rio_config_write: index %d destid %d hopcount %d offset %8.8x len %d val %8.8x\n",
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800288 index, destid, hopcount, offset, len, val);
Zhang Weiad1e9382008-04-18 13:33:41 -0700289 out_be32(&priv->maint_atmu_regs->rowtar,
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800290 (destid << 22) | (hopcount << 12) | ((offset & ~0x3) >> 9));
291
Zhang Weiad1e9382008-04-18 13:33:41 -0700292 data = (u8 *) priv->maint_win + offset;
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800293 switch (len) {
294 case 1:
295 out_8((u8 *) data, val);
296 break;
297 case 2:
298 out_be16((u16 *) data, val);
299 break;
300 default:
301 out_be32((u32 *) data, val);
302 break;
303 }
304
305 return 0;
306}
307
308/**
309 * rio_hw_add_outb_message - Add message to the MPC85xx outbound message queue
310 * @mport: Master port with outbound message queue
311 * @rdev: Target of outbound message
312 * @mbox: Outbound mailbox
313 * @buffer: Message to add to outbound queue
314 * @len: Length of message
315 *
316 * Adds the @buffer message to the MPC85xx outbound message queue. Returns
317 * %0 on success or %-EINVAL on failure.
318 */
319int
320rio_hw_add_outb_message(struct rio_mport *mport, struct rio_dev *rdev, int mbox,
321 void *buffer, size_t len)
322{
Zhang Weiad1e9382008-04-18 13:33:41 -0700323 struct rio_priv *priv = mport->priv;
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800324 u32 omr;
Zhang Weiad1e9382008-04-18 13:33:41 -0700325 struct rio_tx_desc *desc = (struct rio_tx_desc *)priv->msg_tx_ring.virt
326 + priv->msg_tx_ring.tx_slot;
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800327 int ret = 0;
328
329 pr_debug
330 ("RIO: rio_hw_add_outb_message(): destid %4.4x mbox %d buffer %8.8x len %8.8x\n",
331 rdev->destid, mbox, (int)buffer, len);
332
333 if ((len < 8) || (len > RIO_MAX_MSG_SIZE)) {
334 ret = -EINVAL;
335 goto out;
336 }
337
338 /* Copy and clear rest of buffer */
Zhang Weiad1e9382008-04-18 13:33:41 -0700339 memcpy(priv->msg_tx_ring.virt_buffer[priv->msg_tx_ring.tx_slot], buffer,
340 len);
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800341 if (len < (RIO_MAX_MSG_SIZE - 4))
Zhang Weiad1e9382008-04-18 13:33:41 -0700342 memset(priv->msg_tx_ring.virt_buffer[priv->msg_tx_ring.tx_slot]
343 + len, 0, RIO_MAX_MSG_SIZE - len);
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800344
345 /* Set mbox field for message */
346 desc->dport = mbox & 0x3;
347
348 /* Enable EOMI interrupt, set priority, and set destid */
349 desc->dattr = 0x28000000 | (rdev->destid << 2);
350
351 /* Set transfer size aligned to next power of 2 (in double words) */
352 desc->dwcnt = is_power_of_2(len) ? len : 1 << get_bitmask_order(len);
353
354 /* Set snooping and source buffer address */
Zhang Weiad1e9382008-04-18 13:33:41 -0700355 desc->saddr = 0x00000004
356 | priv->msg_tx_ring.phys_buffer[priv->msg_tx_ring.tx_slot];
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800357
358 /* Increment enqueue pointer */
Zhang Weiad1e9382008-04-18 13:33:41 -0700359 omr = in_be32(&priv->msg_regs->omr);
360 out_be32(&priv->msg_regs->omr, omr | RIO_MSG_OMR_MUI);
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800361
362 /* Go to next descriptor */
Zhang Weiad1e9382008-04-18 13:33:41 -0700363 if (++priv->msg_tx_ring.tx_slot == priv->msg_tx_ring.size)
364 priv->msg_tx_ring.tx_slot = 0;
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800365
366 out:
367 return ret;
368}
369
370EXPORT_SYMBOL_GPL(rio_hw_add_outb_message);
371
372/**
Zhang Weid02443a2008-04-18 13:33:38 -0700373 * fsl_rio_tx_handler - MPC85xx outbound message interrupt handler
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800374 * @irq: Linux interrupt number
375 * @dev_instance: Pointer to interrupt-specific data
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800376 *
377 * Handles outbound message interrupts. Executes a register outbound
Simon Arlotta8de5ce2007-05-12 05:42:54 +1000378 * mailbox event handler and acks the interrupt occurrence.
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800379 */
380static irqreturn_t
Zhang Weid02443a2008-04-18 13:33:38 -0700381fsl_rio_tx_handler(int irq, void *dev_instance)
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800382{
383 int osr;
384 struct rio_mport *port = (struct rio_mport *)dev_instance;
Zhang Weiad1e9382008-04-18 13:33:41 -0700385 struct rio_priv *priv = port->priv;
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800386
Zhang Weiad1e9382008-04-18 13:33:41 -0700387 osr = in_be32(&priv->msg_regs->osr);
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800388
389 if (osr & RIO_MSG_OSR_TE) {
390 pr_info("RIO: outbound message transmission error\n");
Zhang Weiad1e9382008-04-18 13:33:41 -0700391 out_be32(&priv->msg_regs->osr, RIO_MSG_OSR_TE);
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800392 goto out;
393 }
394
395 if (osr & RIO_MSG_OSR_QOI) {
396 pr_info("RIO: outbound message queue overflow\n");
Zhang Weiad1e9382008-04-18 13:33:41 -0700397 out_be32(&priv->msg_regs->osr, RIO_MSG_OSR_QOI);
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800398 goto out;
399 }
400
401 if (osr & RIO_MSG_OSR_EOMI) {
Zhang Weiad1e9382008-04-18 13:33:41 -0700402 u32 dqp = in_be32(&priv->msg_regs->odqdpar);
403 int slot = (dqp - priv->msg_tx_ring.phys) >> 5;
404 port->outb_msg[0].mcback(port, priv->msg_tx_ring.dev_id, -1,
405 slot);
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800406
407 /* Ack the end-of-message interrupt */
Zhang Weiad1e9382008-04-18 13:33:41 -0700408 out_be32(&priv->msg_regs->osr, RIO_MSG_OSR_EOMI);
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800409 }
410
411 out:
412 return IRQ_HANDLED;
413}
414
415/**
416 * rio_open_outb_mbox - Initialize MPC85xx outbound mailbox
417 * @mport: Master port implementing the outbound message unit
Matt Porter6978bbc2005-11-07 01:00:20 -0800418 * @dev_id: Device specific pointer to pass on event
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800419 * @mbox: Mailbox to open
420 * @entries: Number of entries in the outbound mailbox ring
421 *
422 * Initializes buffer ring, request the outbound message interrupt,
423 * and enables the outbound message unit. Returns %0 on success and
424 * %-EINVAL or %-ENOMEM on failure.
425 */
Matt Porter6978bbc2005-11-07 01:00:20 -0800426int rio_open_outb_mbox(struct rio_mport *mport, void *dev_id, int mbox, int entries)
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800427{
428 int i, j, rc = 0;
Zhang Weiad1e9382008-04-18 13:33:41 -0700429 struct rio_priv *priv = mport->priv;
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800430
431 if ((entries < RIO_MIN_TX_RING_SIZE) ||
432 (entries > RIO_MAX_TX_RING_SIZE) || (!is_power_of_2(entries))) {
433 rc = -EINVAL;
434 goto out;
435 }
436
437 /* Initialize shadow copy ring */
Zhang Weiad1e9382008-04-18 13:33:41 -0700438 priv->msg_tx_ring.dev_id = dev_id;
439 priv->msg_tx_ring.size = entries;
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800440
Zhang Weiad1e9382008-04-18 13:33:41 -0700441 for (i = 0; i < priv->msg_tx_ring.size; i++) {
442 priv->msg_tx_ring.virt_buffer[i] =
443 dma_alloc_coherent(NULL, RIO_MSG_BUFFER_SIZE,
444 &priv->msg_tx_ring.phys_buffer[i], GFP_KERNEL);
445 if (!priv->msg_tx_ring.virt_buffer[i]) {
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800446 rc = -ENOMEM;
Zhang Weiad1e9382008-04-18 13:33:41 -0700447 for (j = 0; j < priv->msg_tx_ring.size; j++)
448 if (priv->msg_tx_ring.virt_buffer[j])
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800449 dma_free_coherent(NULL,
Zhang Weiad1e9382008-04-18 13:33:41 -0700450 RIO_MSG_BUFFER_SIZE,
451 priv->msg_tx_ring.
452 virt_buffer[j],
453 priv->msg_tx_ring.
454 phys_buffer[j]);
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800455 goto out;
456 }
457 }
458
459 /* Initialize outbound message descriptor ring */
Zhang Weiad1e9382008-04-18 13:33:41 -0700460 priv->msg_tx_ring.virt = dma_alloc_coherent(NULL,
461 priv->msg_tx_ring.size * RIO_MSG_DESC_SIZE,
462 &priv->msg_tx_ring.phys, GFP_KERNEL);
463 if (!priv->msg_tx_ring.virt) {
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800464 rc = -ENOMEM;
465 goto out_dma;
466 }
Zhang Weiad1e9382008-04-18 13:33:41 -0700467 memset(priv->msg_tx_ring.virt, 0,
468 priv->msg_tx_ring.size * RIO_MSG_DESC_SIZE);
469 priv->msg_tx_ring.tx_slot = 0;
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800470
471 /* Point dequeue/enqueue pointers at first entry in ring */
Zhang Weiad1e9382008-04-18 13:33:41 -0700472 out_be32(&priv->msg_regs->odqdpar, priv->msg_tx_ring.phys);
473 out_be32(&priv->msg_regs->odqepar, priv->msg_tx_ring.phys);
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800474
475 /* Configure for snooping */
Zhang Weiad1e9382008-04-18 13:33:41 -0700476 out_be32(&priv->msg_regs->osar, 0x00000004);
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800477
478 /* Clear interrupt status */
Zhang Weiad1e9382008-04-18 13:33:41 -0700479 out_be32(&priv->msg_regs->osr, 0x000000b3);
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800480
481 /* Hook up outbound message handler */
Zhang Weiad1e9382008-04-18 13:33:41 -0700482 rc = request_irq(IRQ_RIO_TX(mport), fsl_rio_tx_handler, 0,
483 "msg_tx", (void *)mport);
484 if (rc < 0)
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800485 goto out_irq;
486
487 /*
488 * Configure outbound message unit
489 * Snooping
490 * Interrupts (all enabled, except QEIE)
491 * Chaining mode
492 * Disable
493 */
Zhang Weiad1e9382008-04-18 13:33:41 -0700494 out_be32(&priv->msg_regs->omr, 0x00100220);
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800495
496 /* Set number of entries */
Zhang Weiad1e9382008-04-18 13:33:41 -0700497 out_be32(&priv->msg_regs->omr,
498 in_be32(&priv->msg_regs->omr) |
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800499 ((get_bitmask_order(entries) - 2) << 12));
500
501 /* Now enable the unit */
Zhang Weiad1e9382008-04-18 13:33:41 -0700502 out_be32(&priv->msg_regs->omr, in_be32(&priv->msg_regs->omr) | 0x1);
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800503
504 out:
505 return rc;
506
507 out_irq:
Zhang Weiad1e9382008-04-18 13:33:41 -0700508 dma_free_coherent(NULL, priv->msg_tx_ring.size * RIO_MSG_DESC_SIZE,
509 priv->msg_tx_ring.virt, priv->msg_tx_ring.phys);
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800510
511 out_dma:
Zhang Weiad1e9382008-04-18 13:33:41 -0700512 for (i = 0; i < priv->msg_tx_ring.size; i++)
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800513 dma_free_coherent(NULL, RIO_MSG_BUFFER_SIZE,
Zhang Weiad1e9382008-04-18 13:33:41 -0700514 priv->msg_tx_ring.virt_buffer[i],
515 priv->msg_tx_ring.phys_buffer[i]);
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800516
517 return rc;
518}
519
520/**
521 * rio_close_outb_mbox - Shut down MPC85xx outbound mailbox
522 * @mport: Master port implementing the outbound message unit
523 * @mbox: Mailbox to close
524 *
525 * Disables the outbound message unit, free all buffers, and
526 * frees the outbound message interrupt.
527 */
528void rio_close_outb_mbox(struct rio_mport *mport, int mbox)
529{
Zhang Weiad1e9382008-04-18 13:33:41 -0700530 struct rio_priv *priv = mport->priv;
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800531 /* Disable inbound message unit */
Zhang Weiad1e9382008-04-18 13:33:41 -0700532 out_be32(&priv->msg_regs->omr, 0);
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800533
534 /* Free ring */
Zhang Weiad1e9382008-04-18 13:33:41 -0700535 dma_free_coherent(NULL, priv->msg_tx_ring.size * RIO_MSG_DESC_SIZE,
536 priv->msg_tx_ring.virt, priv->msg_tx_ring.phys);
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800537
538 /* Free interrupt */
Zhang Weiad1e9382008-04-18 13:33:41 -0700539 free_irq(IRQ_RIO_TX(mport), (void *)mport);
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800540}
541
542/**
Zhang Weid02443a2008-04-18 13:33:38 -0700543 * fsl_rio_rx_handler - MPC85xx inbound message interrupt handler
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800544 * @irq: Linux interrupt number
545 * @dev_instance: Pointer to interrupt-specific data
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800546 *
547 * Handles inbound message interrupts. Executes a registered inbound
Simon Arlotta8de5ce2007-05-12 05:42:54 +1000548 * mailbox event handler and acks the interrupt occurrence.
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800549 */
550static irqreturn_t
Zhang Weid02443a2008-04-18 13:33:38 -0700551fsl_rio_rx_handler(int irq, void *dev_instance)
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800552{
553 int isr;
554 struct rio_mport *port = (struct rio_mport *)dev_instance;
Zhang Weiad1e9382008-04-18 13:33:41 -0700555 struct rio_priv *priv = port->priv;
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800556
Zhang Weiad1e9382008-04-18 13:33:41 -0700557 isr = in_be32(&priv->msg_regs->isr);
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800558
559 if (isr & RIO_MSG_ISR_TE) {
560 pr_info("RIO: inbound message reception error\n");
Zhang Weiad1e9382008-04-18 13:33:41 -0700561 out_be32((void *)&priv->msg_regs->isr, RIO_MSG_ISR_TE);
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800562 goto out;
563 }
564
565 /* XXX Need to check/dispatch until queue empty */
566 if (isr & RIO_MSG_ISR_DIQI) {
567 /*
568 * We implement *only* mailbox 0, but can receive messages
569 * for any mailbox/letter to that mailbox destination. So,
570 * make the callback with an unknown/invalid mailbox number
571 * argument.
572 */
Zhang Weiad1e9382008-04-18 13:33:41 -0700573 port->inb_msg[0].mcback(port, priv->msg_rx_ring.dev_id, -1, -1);
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800574
575 /* Ack the queueing interrupt */
Zhang Weiad1e9382008-04-18 13:33:41 -0700576 out_be32(&priv->msg_regs->isr, RIO_MSG_ISR_DIQI);
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800577 }
578
579 out:
580 return IRQ_HANDLED;
581}
582
583/**
584 * rio_open_inb_mbox - Initialize MPC85xx inbound mailbox
585 * @mport: Master port implementing the inbound message unit
Matt Porter6978bbc2005-11-07 01:00:20 -0800586 * @dev_id: Device specific pointer to pass on event
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800587 * @mbox: Mailbox to open
588 * @entries: Number of entries in the inbound mailbox ring
589 *
590 * Initializes buffer ring, request the inbound message interrupt,
591 * and enables the inbound message unit. Returns %0 on success
592 * and %-EINVAL or %-ENOMEM on failure.
593 */
Matt Porter6978bbc2005-11-07 01:00:20 -0800594int rio_open_inb_mbox(struct rio_mport *mport, void *dev_id, int mbox, int entries)
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800595{
596 int i, rc = 0;
Zhang Weiad1e9382008-04-18 13:33:41 -0700597 struct rio_priv *priv = mport->priv;
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800598
599 if ((entries < RIO_MIN_RX_RING_SIZE) ||
600 (entries > RIO_MAX_RX_RING_SIZE) || (!is_power_of_2(entries))) {
601 rc = -EINVAL;
602 goto out;
603 }
604
605 /* Initialize client buffer ring */
Zhang Weiad1e9382008-04-18 13:33:41 -0700606 priv->msg_rx_ring.dev_id = dev_id;
607 priv->msg_rx_ring.size = entries;
608 priv->msg_rx_ring.rx_slot = 0;
609 for (i = 0; i < priv->msg_rx_ring.size; i++)
610 priv->msg_rx_ring.virt_buffer[i] = NULL;
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800611
612 /* Initialize inbound message ring */
Zhang Weiad1e9382008-04-18 13:33:41 -0700613 priv->msg_rx_ring.virt = dma_alloc_coherent(NULL,
614 priv->msg_rx_ring.size * RIO_MAX_MSG_SIZE,
615 &priv->msg_rx_ring.phys, GFP_KERNEL);
616 if (!priv->msg_rx_ring.virt) {
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800617 rc = -ENOMEM;
618 goto out;
619 }
620
621 /* Point dequeue/enqueue pointers at first entry in ring */
Zhang Weiad1e9382008-04-18 13:33:41 -0700622 out_be32(&priv->msg_regs->ifqdpar, (u32) priv->msg_rx_ring.phys);
623 out_be32(&priv->msg_regs->ifqepar, (u32) priv->msg_rx_ring.phys);
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800624
625 /* Clear interrupt status */
Zhang Weiad1e9382008-04-18 13:33:41 -0700626 out_be32(&priv->msg_regs->isr, 0x00000091);
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800627
628 /* Hook up inbound message handler */
Zhang Weiad1e9382008-04-18 13:33:41 -0700629 rc = request_irq(IRQ_RIO_RX(mport), fsl_rio_rx_handler, 0,
630 "msg_rx", (void *)mport);
631 if (rc < 0) {
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800632 dma_free_coherent(NULL, RIO_MSG_BUFFER_SIZE,
Zhang Weiad1e9382008-04-18 13:33:41 -0700633 priv->msg_tx_ring.virt_buffer[i],
634 priv->msg_tx_ring.phys_buffer[i]);
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800635 goto out;
636 }
637
638 /*
639 * Configure inbound message unit:
640 * Snooping
641 * 4KB max message size
642 * Unmask all interrupt sources
643 * Disable
644 */
Zhang Weiad1e9382008-04-18 13:33:41 -0700645 out_be32(&priv->msg_regs->imr, 0x001b0060);
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800646
647 /* Set number of queue entries */
Zhang Weiad1e9382008-04-18 13:33:41 -0700648 setbits32(&priv->msg_regs->imr, (get_bitmask_order(entries) - 2) << 12);
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800649
650 /* Now enable the unit */
Zhang Weiad1e9382008-04-18 13:33:41 -0700651 setbits32(&priv->msg_regs->imr, 0x1);
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800652
653 out:
654 return rc;
655}
656
657/**
658 * rio_close_inb_mbox - Shut down MPC85xx inbound mailbox
659 * @mport: Master port implementing the inbound message unit
660 * @mbox: Mailbox to close
661 *
662 * Disables the inbound message unit, free all buffers, and
663 * frees the inbound message interrupt.
664 */
665void rio_close_inb_mbox(struct rio_mport *mport, int mbox)
666{
Zhang Weiad1e9382008-04-18 13:33:41 -0700667 struct rio_priv *priv = mport->priv;
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800668 /* Disable inbound message unit */
Zhang Weiad1e9382008-04-18 13:33:41 -0700669 out_be32(&priv->msg_regs->imr, 0);
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800670
671 /* Free ring */
Zhang Weiad1e9382008-04-18 13:33:41 -0700672 dma_free_coherent(NULL, priv->msg_rx_ring.size * RIO_MAX_MSG_SIZE,
673 priv->msg_rx_ring.virt, priv->msg_rx_ring.phys);
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800674
675 /* Free interrupt */
Zhang Weiad1e9382008-04-18 13:33:41 -0700676 free_irq(IRQ_RIO_RX(mport), (void *)mport);
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800677}
678
679/**
680 * rio_hw_add_inb_buffer - Add buffer to the MPC85xx inbound message queue
681 * @mport: Master port implementing the inbound message unit
682 * @mbox: Inbound mailbox number
683 * @buf: Buffer to add to inbound queue
684 *
685 * Adds the @buf buffer to the MPC85xx inbound message queue. Returns
686 * %0 on success or %-EINVAL on failure.
687 */
688int rio_hw_add_inb_buffer(struct rio_mport *mport, int mbox, void *buf)
689{
690 int rc = 0;
Zhang Weiad1e9382008-04-18 13:33:41 -0700691 struct rio_priv *priv = mport->priv;
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800692
693 pr_debug("RIO: rio_hw_add_inb_buffer(), msg_rx_ring.rx_slot %d\n",
Zhang Weiad1e9382008-04-18 13:33:41 -0700694 priv->msg_rx_ring.rx_slot);
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800695
Zhang Weiad1e9382008-04-18 13:33:41 -0700696 if (priv->msg_rx_ring.virt_buffer[priv->msg_rx_ring.rx_slot]) {
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800697 printk(KERN_ERR
698 "RIO: error adding inbound buffer %d, buffer exists\n",
Zhang Weiad1e9382008-04-18 13:33:41 -0700699 priv->msg_rx_ring.rx_slot);
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800700 rc = -EINVAL;
701 goto out;
702 }
703
Zhang Weiad1e9382008-04-18 13:33:41 -0700704 priv->msg_rx_ring.virt_buffer[priv->msg_rx_ring.rx_slot] = buf;
705 if (++priv->msg_rx_ring.rx_slot == priv->msg_rx_ring.size)
706 priv->msg_rx_ring.rx_slot = 0;
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800707
708 out:
709 return rc;
710}
711
712EXPORT_SYMBOL_GPL(rio_hw_add_inb_buffer);
713
714/**
715 * rio_hw_get_inb_message - Fetch inbound message from the MPC85xx message unit
716 * @mport: Master port implementing the inbound message unit
717 * @mbox: Inbound mailbox number
718 *
719 * Gets the next available inbound message from the inbound message queue.
720 * A pointer to the message is returned on success or NULL on failure.
721 */
722void *rio_hw_get_inb_message(struct rio_mport *mport, int mbox)
723{
Zhang Weiad1e9382008-04-18 13:33:41 -0700724 struct rio_priv *priv = mport->priv;
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800725 u32 phys_buf, virt_buf;
726 void *buf = NULL;
727 int buf_idx;
728
Zhang Weiad1e9382008-04-18 13:33:41 -0700729 phys_buf = in_be32(&priv->msg_regs->ifqdpar);
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800730
731 /* If no more messages, then bail out */
Zhang Weiad1e9382008-04-18 13:33:41 -0700732 if (phys_buf == in_be32(&priv->msg_regs->ifqepar))
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800733 goto out2;
734
Zhang Weiad1e9382008-04-18 13:33:41 -0700735 virt_buf = (u32) priv->msg_rx_ring.virt + (phys_buf
736 - priv->msg_rx_ring.phys);
737 buf_idx = (phys_buf - priv->msg_rx_ring.phys) / RIO_MAX_MSG_SIZE;
738 buf = priv->msg_rx_ring.virt_buffer[buf_idx];
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800739
740 if (!buf) {
741 printk(KERN_ERR
742 "RIO: inbound message copy failed, no buffers\n");
743 goto out1;
744 }
745
746 /* Copy max message size, caller is expected to allocate that big */
747 memcpy(buf, (void *)virt_buf, RIO_MAX_MSG_SIZE);
748
749 /* Clear the available buffer */
Zhang Weiad1e9382008-04-18 13:33:41 -0700750 priv->msg_rx_ring.virt_buffer[buf_idx] = NULL;
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800751
752 out1:
Zhang Weiad1e9382008-04-18 13:33:41 -0700753 setbits32(&priv->msg_regs->imr, RIO_MSG_IMR_MI);
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800754
755 out2:
756 return buf;
757}
758
759EXPORT_SYMBOL_GPL(rio_hw_get_inb_message);
760
761/**
Zhang Weid02443a2008-04-18 13:33:38 -0700762 * fsl_rio_dbell_handler - MPC85xx doorbell interrupt handler
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800763 * @irq: Linux interrupt number
764 * @dev_instance: Pointer to interrupt-specific data
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800765 *
766 * Handles doorbell interrupts. Parses a list of registered
767 * doorbell event handlers and executes a matching event handler.
768 */
769static irqreturn_t
Zhang Weid02443a2008-04-18 13:33:38 -0700770fsl_rio_dbell_handler(int irq, void *dev_instance)
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800771{
772 int dsr;
773 struct rio_mport *port = (struct rio_mport *)dev_instance;
Zhang Weiad1e9382008-04-18 13:33:41 -0700774 struct rio_priv *priv = port->priv;
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800775
Zhang Weiad1e9382008-04-18 13:33:41 -0700776 dsr = in_be32(&priv->msg_regs->dsr);
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800777
778 if (dsr & DOORBELL_DSR_TE) {
779 pr_info("RIO: doorbell reception error\n");
Zhang Weiad1e9382008-04-18 13:33:41 -0700780 out_be32(&priv->msg_regs->dsr, DOORBELL_DSR_TE);
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800781 goto out;
782 }
783
784 if (dsr & DOORBELL_DSR_QFI) {
785 pr_info("RIO: doorbell queue full\n");
Zhang Weiad1e9382008-04-18 13:33:41 -0700786 out_be32(&priv->msg_regs->dsr, DOORBELL_DSR_QFI);
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800787 goto out;
788 }
789
790 /* XXX Need to check/dispatch until queue empty */
791 if (dsr & DOORBELL_DSR_DIQI) {
792 u32 dmsg =
Zhang Weiad1e9382008-04-18 13:33:41 -0700793 (u32) priv->dbell_ring.virt +
794 (in_be32(&priv->msg_regs->dqdpar) & 0xfff);
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800795 struct rio_dbell *dbell;
796 int found = 0;
797
798 pr_debug
799 ("RIO: processing doorbell, sid %2.2x tid %2.2x info %4.4x\n",
800 DBELL_SID(dmsg), DBELL_TID(dmsg), DBELL_INF(dmsg));
801
802 list_for_each_entry(dbell, &port->dbells, node) {
803 if ((dbell->res->start <= DBELL_INF(dmsg)) &&
804 (dbell->res->end >= DBELL_INF(dmsg))) {
805 found = 1;
806 break;
807 }
808 }
809 if (found) {
Matt Porter6978bbc2005-11-07 01:00:20 -0800810 dbell->dinb(port, dbell->dev_id, DBELL_SID(dmsg), DBELL_TID(dmsg),
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800811 DBELL_INF(dmsg));
812 } else {
813 pr_debug
814 ("RIO: spurious doorbell, sid %2.2x tid %2.2x info %4.4x\n",
815 DBELL_SID(dmsg), DBELL_TID(dmsg), DBELL_INF(dmsg));
816 }
Zhang Weiad1e9382008-04-18 13:33:41 -0700817 setbits32(&priv->msg_regs->dmr, DOORBELL_DMR_DI);
818 out_be32(&priv->msg_regs->dsr, DOORBELL_DSR_DIQI);
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800819 }
820
821 out:
822 return IRQ_HANDLED;
823}
824
825/**
Zhang Weid02443a2008-04-18 13:33:38 -0700826 * fsl_rio_doorbell_init - MPC85xx doorbell interface init
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800827 * @mport: Master port implementing the inbound doorbell unit
828 *
829 * Initializes doorbell unit hardware and inbound DMA buffer
Zhang Weid02443a2008-04-18 13:33:38 -0700830 * ring. Called from fsl_rio_setup(). Returns %0 on success
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800831 * or %-ENOMEM on failure.
832 */
Zhang Weid02443a2008-04-18 13:33:38 -0700833static int fsl_rio_doorbell_init(struct rio_mport *mport)
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800834{
Zhang Weiad1e9382008-04-18 13:33:41 -0700835 struct rio_priv *priv = mport->priv;
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800836 int rc = 0;
837
838 /* Map outbound doorbell window immediately after maintenance window */
Zhang Weiad1e9382008-04-18 13:33:41 -0700839 priv->dbell_win = ioremap(mport->iores.start + RIO_MAINT_WIN_SIZE,
840 RIO_DBELL_WIN_SIZE);
841 if (!priv->dbell_win) {
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800842 printk(KERN_ERR
843 "RIO: unable to map outbound doorbell window\n");
844 rc = -ENOMEM;
845 goto out;
846 }
847
848 /* Initialize inbound doorbells */
Zhang Weiad1e9382008-04-18 13:33:41 -0700849 priv->dbell_ring.virt = dma_alloc_coherent(NULL, 512 *
850 DOORBELL_MESSAGE_SIZE, &priv->dbell_ring.phys, GFP_KERNEL);
851 if (!priv->dbell_ring.virt) {
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800852 printk(KERN_ERR "RIO: unable allocate inbound doorbell ring\n");
853 rc = -ENOMEM;
Zhang Weiad1e9382008-04-18 13:33:41 -0700854 iounmap(priv->dbell_win);
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800855 goto out;
856 }
857
858 /* Point dequeue/enqueue pointers at first entry in ring */
Zhang Weiad1e9382008-04-18 13:33:41 -0700859 out_be32(&priv->msg_regs->dqdpar, (u32) priv->dbell_ring.phys);
860 out_be32(&priv->msg_regs->dqepar, (u32) priv->dbell_ring.phys);
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800861
862 /* Clear interrupt status */
Zhang Weiad1e9382008-04-18 13:33:41 -0700863 out_be32(&priv->msg_regs->dsr, 0x00000091);
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800864
865 /* Hook up doorbell handler */
Zhang Weiad1e9382008-04-18 13:33:41 -0700866 rc = request_irq(IRQ_RIO_BELL(mport), fsl_rio_dbell_handler, 0,
867 "dbell_rx", (void *)mport);
868 if (rc < 0) {
869 iounmap(priv->dbell_win);
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800870 dma_free_coherent(NULL, 512 * DOORBELL_MESSAGE_SIZE,
Zhang Weiad1e9382008-04-18 13:33:41 -0700871 priv->dbell_ring.virt, priv->dbell_ring.phys);
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800872 printk(KERN_ERR
873 "MPC85xx RIO: unable to request inbound doorbell irq");
874 goto out;
875 }
876
877 /* Configure doorbells for snooping, 512 entries, and enable */
Zhang Weiad1e9382008-04-18 13:33:41 -0700878 out_be32(&priv->msg_regs->dmr, 0x00108161);
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800879
880 out:
881 return rc;
882}
883
884static char *cmdline = NULL;
885
Zhang Weid02443a2008-04-18 13:33:38 -0700886static int fsl_rio_get_hdid(int index)
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800887{
888 /* XXX Need to parse multiple entries in some format */
889 if (!cmdline)
890 return -1;
891
892 return simple_strtol(cmdline, NULL, 0);
893}
894
Zhang Weid02443a2008-04-18 13:33:38 -0700895static int fsl_rio_get_cmdline(char *s)
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800896{
897 if (!s)
898 return 0;
899
900 cmdline = s;
901 return 1;
902}
903
Zhang Weid02443a2008-04-18 13:33:38 -0700904__setup("riohdid=", fsl_rio_get_cmdline);
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800905
906/**
Zhang Weid02443a2008-04-18 13:33:38 -0700907 * fsl_rio_setup - Setup MPC85xx RapidIO interface
Zhang Weicc2bb692008-04-18 13:33:41 -0700908 * @fsl_rio_setup - Setup Freescale PowerPC RapidIO interface
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800909 *
910 * Initializes MPC85xx RapidIO hardware interface, configures
911 * master port with system-specific info, and registers the
912 * master port with the RapidIO subsystem.
913 */
Zhang Weicc2bb692008-04-18 13:33:41 -0700914int fsl_rio_setup(struct of_device *dev)
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800915{
916 struct rio_ops *ops;
917 struct rio_mport *port;
Zhang Weicc2bb692008-04-18 13:33:41 -0700918 struct rio_priv *priv;
919 int rc = 0;
920 const u32 *dt_range, *cell;
921 struct resource regs;
922 int rlen;
923 u64 law_start, law_size;
924 int paw, aw, sw;
925
926 if (!dev->node) {
927 dev_err(&dev->dev, "Device OF-Node is NULL");
928 return -EFAULT;
929 }
930
931 rc = of_address_to_resource(dev->node, 0, &regs);
932 if (rc) {
933 dev_err(&dev->dev, "Can't get %s property 'reg'\n",
934 dev->node->full_name);
935 return -EFAULT;
936 }
937 dev_info(&dev->dev, "Of-device full name %s\n", dev->node->full_name);
938 dev_info(&dev->dev, "Regs start 0x%08x size 0x%08x\n", regs.start,
939 regs.end - regs.start + 1);
940
941 dt_range = of_get_property(dev->node, "ranges", &rlen);
942 if (!dt_range) {
943 dev_err(&dev->dev, "Can't get %s property 'ranges'\n",
944 dev->node->full_name);
945 return -EFAULT;
946 }
947
948 /* Get node address wide */
949 cell = of_get_property(dev->node, "#address-cells", NULL);
950 if (cell)
951 aw = *cell;
952 else
953 aw = of_n_addr_cells(dev->node);
954 /* Get node size wide */
955 cell = of_get_property(dev->node, "#size-cells", NULL);
956 if (cell)
957 sw = *cell;
958 else
959 sw = of_n_size_cells(dev->node);
960 /* Get parent address wide wide */
961 paw = of_n_addr_cells(dev->node);
962
963 law_start = of_read_number(dt_range + aw, paw);
964 law_size = of_read_number(dt_range + aw + paw, sw);
965
966 dev_info(&dev->dev, "LAW start 0x%016llx, size 0x%016llx.\n",
967 law_start, law_size);
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800968
969 ops = kmalloc(sizeof(struct rio_ops), GFP_KERNEL);
Zhang Weid02443a2008-04-18 13:33:38 -0700970 ops->lcread = fsl_local_config_read;
971 ops->lcwrite = fsl_local_config_write;
972 ops->cread = fsl_rio_config_read;
973 ops->cwrite = fsl_rio_config_write;
974 ops->dsend = fsl_rio_doorbell_send;
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800975
Zhang Weiad1e9382008-04-18 13:33:41 -0700976 port = kzalloc(sizeof(struct rio_mport), GFP_KERNEL);
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800977 port->id = 0;
978 port->index = 0;
Zhang Weiad1e9382008-04-18 13:33:41 -0700979
980 priv = kzalloc(sizeof(struct rio_priv), GFP_KERNEL);
981 if (!priv) {
982 printk(KERN_ERR "Can't alloc memory for 'priv'\n");
983 rc = -ENOMEM;
984 goto err;
985 }
986
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800987 INIT_LIST_HEAD(&port->dbells);
988 port->iores.start = law_start;
989 port->iores.end = law_start + law_size;
990 port->iores.flags = IORESOURCE_MEM;
991
Zhang Weicc2bb692008-04-18 13:33:41 -0700992 priv->bellirq = irq_of_parse_and_map(dev->node, 2);
993 priv->txirq = irq_of_parse_and_map(dev->node, 3);
994 priv->rxirq = irq_of_parse_and_map(dev->node, 4);
995 dev_info(&dev->dev, "bellirq: %d, txirq: %d, rxirq %d\n", priv->bellirq,
996 priv->txirq, priv->rxirq);
997
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800998 rio_init_dbell_res(&port->riores[RIO_DOORBELL_RESOURCE], 0, 0xffff);
999 rio_init_mbox_res(&port->riores[RIO_INB_MBOX_RESOURCE], 0, 0);
1000 rio_init_mbox_res(&port->riores[RIO_OUTB_MBOX_RESOURCE], 0, 0);
1001 strcpy(port->name, "RIO0 mport");
1002
1003 port->ops = ops;
Zhang Weid02443a2008-04-18 13:33:38 -07001004 port->host_deviceid = fsl_rio_get_hdid(port->id);
Matt Porter2b0c28d7f2005-11-07 01:00:19 -08001005
Zhang Weiad1e9382008-04-18 13:33:41 -07001006 port->priv = priv;
Matt Porter2b0c28d7f2005-11-07 01:00:19 -08001007 rio_register_mport(port);
1008
Zhang Weicc2bb692008-04-18 13:33:41 -07001009 priv->regs_win = ioremap(regs.start, regs.end - regs.start + 1);
Zhang Weie0423232008-04-18 13:33:42 -07001010
1011 port->sys_size = (in_be32((priv->regs_win + RIO_PEF_CAR))
1012 & RIO_PEF_CTLS) >> 4;
1013 dev_info(&dev->dev, "RapidIO Common Transport System size: %d\n",
1014 port->sys_size ? 65536 : 256);
1015
Zhang Weiad1e9382008-04-18 13:33:41 -07001016 priv->atmu_regs = (struct rio_atmu_regs *)(priv->regs_win
1017 + RIO_ATMU_REGS_OFFSET);
1018 priv->maint_atmu_regs = priv->atmu_regs + 1;
1019 priv->dbell_atmu_regs = priv->atmu_regs + 2;
1020 priv->msg_regs = (struct rio_msg_regs *)(priv->regs_win
1021 + RIO_MSG_REGS_OFFSET);
Matt Porter2b0c28d7f2005-11-07 01:00:19 -08001022
1023 /* Configure maintenance transaction window */
Zhang Weiad1e9382008-04-18 13:33:41 -07001024 out_be32(&priv->maint_atmu_regs->rowbar, 0x000c0000);
1025 out_be32(&priv->maint_atmu_regs->rowar, 0x80077015);
Matt Porter2b0c28d7f2005-11-07 01:00:19 -08001026
Zhang Weiad1e9382008-04-18 13:33:41 -07001027 priv->maint_win = ioremap(law_start, RIO_MAINT_WIN_SIZE);
Matt Porter2b0c28d7f2005-11-07 01:00:19 -08001028
1029 /* Configure outbound doorbell window */
Zhang Weiad1e9382008-04-18 13:33:41 -07001030 out_be32(&priv->dbell_atmu_regs->rowbar, 0x000c0400);
1031 out_be32(&priv->dbell_atmu_regs->rowar, 0x8004200b);
Zhang Weid02443a2008-04-18 13:33:38 -07001032 fsl_rio_doorbell_init(port);
Zhang Weiad1e9382008-04-18 13:33:41 -07001033
Zhang Weicc2bb692008-04-18 13:33:41 -07001034 return 0;
Zhang Weiad1e9382008-04-18 13:33:41 -07001035err:
1036 if (priv)
1037 iounmap(priv->regs_win);
Zhang Weicc2bb692008-04-18 13:33:41 -07001038 kfree(ops);
Zhang Weiad1e9382008-04-18 13:33:41 -07001039 kfree(priv);
1040 kfree(port);
Zhang Weicc2bb692008-04-18 13:33:41 -07001041 return rc;
Matt Porter2b0c28d7f2005-11-07 01:00:19 -08001042}
Zhang Weicc2bb692008-04-18 13:33:41 -07001043
1044/* The probe function for RapidIO peer-to-peer network.
1045 */
1046static int __devinit fsl_of_rio_rpn_probe(struct of_device *dev,
1047 const struct of_device_id *match)
1048{
1049 int rc;
1050 printk(KERN_INFO "Setting up RapidIO peer-to-peer network %s\n",
1051 dev->node->full_name);
1052
1053 rc = fsl_rio_setup(dev);
1054 if (rc)
1055 goto out;
1056
1057 /* Enumerate all registered ports */
1058 rc = rio_init_mports();
1059out:
1060 return rc;
1061};
1062
1063static const struct of_device_id fsl_of_rio_rpn_ids[] = {
1064 {
1065 .compatible = "fsl,rapidio-delta",
1066 },
1067 {},
1068};
1069
1070static struct of_platform_driver fsl_of_rio_rpn_driver = {
1071 .name = "fsl-of-rio",
1072 .match_table = fsl_of_rio_rpn_ids,
1073 .probe = fsl_of_rio_rpn_probe,
1074};
1075
1076static __init int fsl_of_rio_rpn_init(void)
1077{
1078 return of_register_platform_driver(&fsl_of_rio_rpn_driver);
1079}
1080
1081subsys_initcall(fsl_of_rio_rpn_init);