blob: a0fa4ebb39c62b2b2a569bcf03204cde7db0e1b6 [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>
Zhang Wei61b26912008-04-18 13:33:44 -070024#include <linux/delay.h>
Matt Porter2b0c28d7f2005-11-07 01:00:19 -080025
26#include <asm/io.h>
27
Zhang Weiad1e9382008-04-18 13:33:41 -070028/* 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)
32
Matt Porter2b0c28d7f2005-11-07 01:00:19 -080033#define RIO_ATMU_REGS_OFFSET 0x10c00
Zhang Wei61b26912008-04-18 13:33:44 -070034#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 */
Matt Porter2b0c28d7f2005-11-07 01:00:19 -080040#define RIO_MAINT_WIN_SIZE 0x400000
41#define RIO_DBELL_WIN_SIZE 0x1000
42
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
50
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
55
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
62
63#define DOORBELL_DMR_DI 0x00000002
64#define DOORBELL_DSR_TE 0x00000080
65#define DOORBELL_DSR_QFI 0x00000010
66#define DOORBELL_DSR_DIQI 0x00000001
Zhang Wei6c391032008-04-18 13:33:48 -070067#define DOORBELL_TID_OFFSET 0x02
68#define DOORBELL_SID_OFFSET 0x04
Matt Porter2b0c28d7f2005-11-07 01:00:19 -080069#define DOORBELL_INFO_OFFSET 0x06
70
71#define DOORBELL_MESSAGE_SIZE 0x08
Zhang Wei6c391032008-04-18 13:33:48 -070072#define DBELL_SID(x) (*(u16 *)(x + DOORBELL_SID_OFFSET))
73#define DBELL_TID(x) (*(u16 *)(x + DOORBELL_TID_OFFSET))
Matt Porter2b0c28d7f2005-11-07 01:00:19 -080074#define DBELL_INF(x) (*(u16 *)(x + DOORBELL_INFO_OFFSET))
75
Matt Porter2b0c28d7f2005-11-07 01:00:19 -080076struct rio_atmu_regs {
77 u32 rowtar;
Zhang Wei61b26912008-04-18 13:33:44 -070078 u32 rowtear;
Matt Porter2b0c28d7f2005-11-07 01:00:19 -080079 u32 rowbar;
80 u32 pad2;
81 u32 rowar;
82 u32 pad3[3];
83};
84
85struct rio_msg_regs {
86 u32 omr;
87 u32 osr;
88 u32 pad1;
89 u32 odqdpar;
90 u32 pad2;
91 u32 osar;
92 u32 odpr;
93 u32 odatr;
94 u32 odcr;
95 u32 pad3;
96 u32 odqepar;
97 u32 pad4[13];
98 u32 imr;
99 u32 isr;
100 u32 pad5;
101 u32 ifqdpar;
102 u32 pad6;
103 u32 ifqepar;
Zhang Wei61b26912008-04-18 13:33:44 -0700104 u32 pad7[226];
105 u32 odmr;
106 u32 odsr;
107 u32 res0[4];
108 u32 oddpr;
109 u32 oddatr;
110 u32 res1[3];
111 u32 odretcr;
112 u32 res2[12];
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800113 u32 dmr;
114 u32 dsr;
115 u32 pad8;
116 u32 dqdpar;
117 u32 pad9;
118 u32 dqepar;
119 u32 pad10[26];
120 u32 pwmr;
121 u32 pwsr;
122 u32 pad11;
123 u32 pwqbar;
124};
125
126struct rio_tx_desc {
127 u32 res1;
128 u32 saddr;
129 u32 dport;
130 u32 dattr;
131 u32 res2;
132 u32 res3;
133 u32 dwcnt;
134 u32 res4;
135};
136
Zhang Weiad1e9382008-04-18 13:33:41 -0700137struct rio_dbell_ring {
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800138 void *virt;
139 dma_addr_t phys;
Zhang Weiad1e9382008-04-18 13:33:41 -0700140};
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800141
Zhang Weiad1e9382008-04-18 13:33:41 -0700142struct rio_msg_tx_ring {
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800143 void *virt;
144 dma_addr_t phys;
145 void *virt_buffer[RIO_MAX_TX_RING_SIZE];
146 dma_addr_t phys_buffer[RIO_MAX_TX_RING_SIZE];
147 int tx_slot;
148 int size;
Matt Porter6978bbc2005-11-07 01:00:20 -0800149 void *dev_id;
Zhang Weiad1e9382008-04-18 13:33:41 -0700150};
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800151
Zhang Weiad1e9382008-04-18 13:33:41 -0700152struct rio_msg_rx_ring {
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800153 void *virt;
154 dma_addr_t phys;
155 void *virt_buffer[RIO_MAX_RX_RING_SIZE];
156 int rx_slot;
157 int size;
Matt Porter6978bbc2005-11-07 01:00:20 -0800158 void *dev_id;
Zhang Weiad1e9382008-04-18 13:33:41 -0700159};
160
161struct rio_priv {
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;
172 int bellirq;
173 int txirq;
174 int rxirq;
175};
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800176
177/**
Zhang Weid02443a2008-04-18 13:33:38 -0700178 * fsl_rio_doorbell_send - Send a MPC85xx doorbell message
Randy Dunlap9941d942008-04-30 16:45:58 -0700179 * @mport: RapidIO master port info
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800180 * @index: ID of RapidIO interface
181 * @destid: Destination ID of target device
182 * @data: 16-bit info field of RapidIO doorbell message
183 *
184 * Sends a MPC85xx doorbell message. Returns %0 on success or
185 * %-EINVAL on failure.
186 */
Zhang Weiad1e9382008-04-18 13:33:41 -0700187static int fsl_rio_doorbell_send(struct rio_mport *mport,
188 int index, u16 destid, u16 data)
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800189{
Zhang Weiad1e9382008-04-18 13:33:41 -0700190 struct rio_priv *priv = mport->priv;
Zhang Weid02443a2008-04-18 13:33:38 -0700191 pr_debug("fsl_doorbell_send: index %d destid %4.4x data %4.4x\n",
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800192 index, destid, data);
Zhang Wei61b26912008-04-18 13:33:44 -0700193 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);
197 break;
198 case RIO_PHY_SERIAL:
199 /* In the serial version silicons, such as MPC8548, MPC8641,
200 * below operations is must be.
201 */
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);
207 break;
208 }
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800209
210 return 0;
211}
212
213/**
Zhang Weid02443a2008-04-18 13:33:38 -0700214 * fsl_local_config_read - Generate a MPC85xx local config space read
Randy Dunlap9941d942008-04-30 16:45:58 -0700215 * @mport: RapidIO master port info
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800216 * @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
220 *
221 * Generates a MPC85xx local configuration space read. Returns %0 on
222 * success or %-EINVAL on failure.
223 */
Zhang Weiad1e9382008-04-18 13:33:41 -0700224static int fsl_local_config_read(struct rio_mport *mport,
225 int index, u32 offset, int len, u32 *data)
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800226{
Zhang Weiad1e9382008-04-18 13:33:41 -0700227 struct rio_priv *priv = mport->priv;
Zhang Weid02443a2008-04-18 13:33:38 -0700228 pr_debug("fsl_local_config_read: index %d offset %8.8x\n", index,
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800229 offset);
Zhang Weiad1e9382008-04-18 13:33:41 -0700230 *data = in_be32(priv->regs_win + offset);
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800231
232 return 0;
233}
234
235/**
Zhang Weid02443a2008-04-18 13:33:38 -0700236 * fsl_local_config_write - Generate a MPC85xx local config space write
Randy Dunlap9941d942008-04-30 16:45:58 -0700237 * @mport: RapidIO master port info
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800238 * @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
242 *
243 * Generates a MPC85xx local configuration space write. Returns %0 on
244 * success or %-EINVAL on failure.
245 */
Zhang Weiad1e9382008-04-18 13:33:41 -0700246static int fsl_local_config_write(struct rio_mport *mport,
247 int index, u32 offset, int len, u32 data)
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800248{
Zhang Weiad1e9382008-04-18 13:33:41 -0700249 struct rio_priv *priv = mport->priv;
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800250 pr_debug
Zhang Weid02443a2008-04-18 13:33:38 -0700251 ("fsl_local_config_write: index %d offset %8.8x data %8.8x\n",
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800252 index, offset, data);
Zhang Weiad1e9382008-04-18 13:33:41 -0700253 out_be32(priv->regs_win + offset, data);
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800254
255 return 0;
256}
257
258/**
Zhang Weid02443a2008-04-18 13:33:38 -0700259 * fsl_rio_config_read - Generate a MPC85xx read maintenance transaction
Randy Dunlap9941d942008-04-30 16:45:58 -0700260 * @mport: RapidIO master port info
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800261 * @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
267 *
268 * Generates a MPC85xx read maintenance transaction. Returns %0 on
269 * success or %-EINVAL on failure.
270 */
271static int
Zhang Weiad1e9382008-04-18 13:33:41 -0700272fsl_rio_config_read(struct rio_mport *mport, int index, u16 destid,
273 u8 hopcount, u32 offset, int len, u32 *val)
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800274{
Zhang Weiad1e9382008-04-18 13:33:41 -0700275 struct rio_priv *priv = mport->priv;
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800276 u8 *data;
277
278 pr_debug
Zhang Weid02443a2008-04-18 13:33:38 -0700279 ("fsl_rio_config_read: index %d destid %d hopcount %d offset %8.8x len %d\n",
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800280 index, destid, hopcount, offset, len);
Zhang Weiad1e9382008-04-18 13:33:41 -0700281 out_be32(&priv->maint_atmu_regs->rowtar,
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800282 (destid << 22) | (hopcount << 12) | ((offset & ~0x3) >> 9));
283
Zhang Weiad1e9382008-04-18 13:33:41 -0700284 data = (u8 *) priv->maint_win + offset;
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800285 switch (len) {
286 case 1:
287 *val = in_8((u8 *) data);
288 break;
289 case 2:
290 *val = in_be16((u16 *) data);
291 break;
292 default:
293 *val = in_be32((u32 *) data);
294 break;
295 }
296
297 return 0;
298}
299
300/**
Zhang Weid02443a2008-04-18 13:33:38 -0700301 * fsl_rio_config_write - Generate a MPC85xx write maintenance transaction
Randy Dunlap9941d942008-04-30 16:45:58 -0700302 * @mport: RapidIO master port info
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800303 * @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
309 *
310 * Generates an MPC85xx write maintenance transaction. Returns %0 on
311 * success or %-EINVAL on failure.
312 */
313static int
Zhang Weiad1e9382008-04-18 13:33:41 -0700314fsl_rio_config_write(struct rio_mport *mport, int index, u16 destid,
315 u8 hopcount, u32 offset, int len, u32 val)
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800316{
Zhang Weiad1e9382008-04-18 13:33:41 -0700317 struct rio_priv *priv = mport->priv;
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800318 u8 *data;
319 pr_debug
Zhang Weid02443a2008-04-18 13:33:38 -0700320 ("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 -0800321 index, destid, hopcount, offset, len, val);
Zhang Weiad1e9382008-04-18 13:33:41 -0700322 out_be32(&priv->maint_atmu_regs->rowtar,
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800323 (destid << 22) | (hopcount << 12) | ((offset & ~0x3) >> 9));
324
Zhang Weiad1e9382008-04-18 13:33:41 -0700325 data = (u8 *) priv->maint_win + offset;
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800326 switch (len) {
327 case 1:
328 out_8((u8 *) data, val);
329 break;
330 case 2:
331 out_be16((u16 *) data, val);
332 break;
333 default:
334 out_be32((u32 *) data, val);
335 break;
336 }
337
338 return 0;
339}
340
341/**
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
348 *
349 * Adds the @buffer message to the MPC85xx outbound message queue. Returns
350 * %0 on success or %-EINVAL on failure.
351 */
352int
353rio_hw_add_outb_message(struct rio_mport *mport, struct rio_dev *rdev, int mbox,
354 void *buffer, size_t len)
355{
Zhang Weiad1e9382008-04-18 13:33:41 -0700356 struct rio_priv *priv = mport->priv;
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800357 u32 omr;
Zhang Weiad1e9382008-04-18 13:33:41 -0700358 struct rio_tx_desc *desc = (struct rio_tx_desc *)priv->msg_tx_ring.virt
359 + priv->msg_tx_ring.tx_slot;
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800360 int ret = 0;
361
362 pr_debug
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);
365
366 if ((len < 8) || (len > RIO_MAX_MSG_SIZE)) {
367 ret = -EINVAL;
368 goto out;
369 }
370
371 /* Copy and clear rest of buffer */
Zhang Weiad1e9382008-04-18 13:33:41 -0700372 memcpy(priv->msg_tx_ring.virt_buffer[priv->msg_tx_ring.tx_slot], buffer,
373 len);
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800374 if (len < (RIO_MAX_MSG_SIZE - 4))
Zhang Weiad1e9382008-04-18 13:33:41 -0700375 memset(priv->msg_tx_ring.virt_buffer[priv->msg_tx_ring.tx_slot]
376 + len, 0, RIO_MAX_MSG_SIZE - len);
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800377
Zhang Wei61b26912008-04-18 13:33:44 -0700378 switch (mport->phy_type) {
379 case RIO_PHY_PARALLEL:
380 /* Set mbox field for message */
381 desc->dport = mbox & 0x3;
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800382
Zhang Wei61b26912008-04-18 13:33:44 -0700383 /* Enable EOMI interrupt, set priority, and set destid */
384 desc->dattr = 0x28000000 | (rdev->destid << 2);
385 break;
386 case RIO_PHY_SERIAL:
387 /* Set mbox field for message, and set destid */
388 desc->dport = (rdev->destid << 16) | (mbox & 0x3);
389
390 /* Enable EOMI interrupt and priority */
391 desc->dattr = 0x28000000;
392 break;
393 }
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800394
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);
397
398 /* Set snooping and source buffer address */
Zhang Weiad1e9382008-04-18 13:33:41 -0700399 desc->saddr = 0x00000004
400 | priv->msg_tx_ring.phys_buffer[priv->msg_tx_ring.tx_slot];
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800401
402 /* Increment enqueue pointer */
Zhang Weiad1e9382008-04-18 13:33:41 -0700403 omr = in_be32(&priv->msg_regs->omr);
404 out_be32(&priv->msg_regs->omr, omr | RIO_MSG_OMR_MUI);
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800405
406 /* Go to next descriptor */
Zhang Weiad1e9382008-04-18 13:33:41 -0700407 if (++priv->msg_tx_ring.tx_slot == priv->msg_tx_ring.size)
408 priv->msg_tx_ring.tx_slot = 0;
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800409
410 out:
411 return ret;
412}
413
414EXPORT_SYMBOL_GPL(rio_hw_add_outb_message);
415
416/**
Zhang Weid02443a2008-04-18 13:33:38 -0700417 * fsl_rio_tx_handler - MPC85xx outbound message interrupt handler
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800418 * @irq: Linux interrupt number
419 * @dev_instance: Pointer to interrupt-specific data
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800420 *
421 * Handles outbound message interrupts. Executes a register outbound
Simon Arlotta8de5ce2007-05-12 05:42:54 +1000422 * mailbox event handler and acks the interrupt occurrence.
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800423 */
424static irqreturn_t
Zhang Weid02443a2008-04-18 13:33:38 -0700425fsl_rio_tx_handler(int irq, void *dev_instance)
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800426{
427 int osr;
428 struct rio_mport *port = (struct rio_mport *)dev_instance;
Zhang Weiad1e9382008-04-18 13:33:41 -0700429 struct rio_priv *priv = port->priv;
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800430
Zhang Weiad1e9382008-04-18 13:33:41 -0700431 osr = in_be32(&priv->msg_regs->osr);
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800432
433 if (osr & RIO_MSG_OSR_TE) {
434 pr_info("RIO: outbound message transmission error\n");
Zhang Weiad1e9382008-04-18 13:33:41 -0700435 out_be32(&priv->msg_regs->osr, RIO_MSG_OSR_TE);
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800436 goto out;
437 }
438
439 if (osr & RIO_MSG_OSR_QOI) {
440 pr_info("RIO: outbound message queue overflow\n");
Zhang Weiad1e9382008-04-18 13:33:41 -0700441 out_be32(&priv->msg_regs->osr, RIO_MSG_OSR_QOI);
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800442 goto out;
443 }
444
445 if (osr & RIO_MSG_OSR_EOMI) {
Zhang Weiad1e9382008-04-18 13:33:41 -0700446 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,
449 slot);
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800450
451 /* Ack the end-of-message interrupt */
Zhang Weiad1e9382008-04-18 13:33:41 -0700452 out_be32(&priv->msg_regs->osr, RIO_MSG_OSR_EOMI);
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800453 }
454
455 out:
456 return IRQ_HANDLED;
457}
458
459/**
460 * rio_open_outb_mbox - Initialize MPC85xx outbound mailbox
461 * @mport: Master port implementing the outbound message unit
Matt Porter6978bbc2005-11-07 01:00:20 -0800462 * @dev_id: Device specific pointer to pass on event
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800463 * @mbox: Mailbox to open
464 * @entries: Number of entries in the outbound mailbox ring
465 *
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.
469 */
Matt Porter6978bbc2005-11-07 01:00:20 -0800470int rio_open_outb_mbox(struct rio_mport *mport, void *dev_id, int mbox, int entries)
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800471{
472 int i, j, rc = 0;
Zhang Weiad1e9382008-04-18 13:33:41 -0700473 struct rio_priv *priv = mport->priv;
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800474
475 if ((entries < RIO_MIN_TX_RING_SIZE) ||
476 (entries > RIO_MAX_TX_RING_SIZE) || (!is_power_of_2(entries))) {
477 rc = -EINVAL;
478 goto out;
479 }
480
481 /* Initialize shadow copy ring */
Zhang Weiad1e9382008-04-18 13:33:41 -0700482 priv->msg_tx_ring.dev_id = dev_id;
483 priv->msg_tx_ring.size = entries;
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800484
Zhang Weiad1e9382008-04-18 13:33:41 -0700485 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]) {
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800490 rc = -ENOMEM;
Zhang Weiad1e9382008-04-18 13:33:41 -0700491 for (j = 0; j < priv->msg_tx_ring.size; j++)
492 if (priv->msg_tx_ring.virt_buffer[j])
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800493 dma_free_coherent(NULL,
Zhang Weiad1e9382008-04-18 13:33:41 -0700494 RIO_MSG_BUFFER_SIZE,
495 priv->msg_tx_ring.
496 virt_buffer[j],
497 priv->msg_tx_ring.
498 phys_buffer[j]);
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800499 goto out;
500 }
501 }
502
503 /* Initialize outbound message descriptor ring */
Zhang Weiad1e9382008-04-18 13:33:41 -0700504 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) {
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800508 rc = -ENOMEM;
509 goto out_dma;
510 }
Zhang Weiad1e9382008-04-18 13:33:41 -0700511 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;
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800514
515 /* Point dequeue/enqueue pointers at first entry in ring */
Zhang Weiad1e9382008-04-18 13:33:41 -0700516 out_be32(&priv->msg_regs->odqdpar, priv->msg_tx_ring.phys);
517 out_be32(&priv->msg_regs->odqepar, priv->msg_tx_ring.phys);
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800518
519 /* Configure for snooping */
Zhang Weiad1e9382008-04-18 13:33:41 -0700520 out_be32(&priv->msg_regs->osar, 0x00000004);
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800521
522 /* Clear interrupt status */
Zhang Weiad1e9382008-04-18 13:33:41 -0700523 out_be32(&priv->msg_regs->osr, 0x000000b3);
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800524
525 /* Hook up outbound message handler */
Zhang Weiad1e9382008-04-18 13:33:41 -0700526 rc = request_irq(IRQ_RIO_TX(mport), fsl_rio_tx_handler, 0,
527 "msg_tx", (void *)mport);
528 if (rc < 0)
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800529 goto out_irq;
530
531 /*
532 * Configure outbound message unit
533 * Snooping
534 * Interrupts (all enabled, except QEIE)
535 * Chaining mode
536 * Disable
537 */
Zhang Weiad1e9382008-04-18 13:33:41 -0700538 out_be32(&priv->msg_regs->omr, 0x00100220);
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800539
540 /* Set number of entries */
Zhang Weiad1e9382008-04-18 13:33:41 -0700541 out_be32(&priv->msg_regs->omr,
542 in_be32(&priv->msg_regs->omr) |
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800543 ((get_bitmask_order(entries) - 2) << 12));
544
545 /* Now enable the unit */
Zhang Weiad1e9382008-04-18 13:33:41 -0700546 out_be32(&priv->msg_regs->omr, in_be32(&priv->msg_regs->omr) | 0x1);
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800547
548 out:
549 return rc;
550
551 out_irq:
Zhang Weiad1e9382008-04-18 13:33:41 -0700552 dma_free_coherent(NULL, priv->msg_tx_ring.size * RIO_MSG_DESC_SIZE,
553 priv->msg_tx_ring.virt, priv->msg_tx_ring.phys);
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800554
555 out_dma:
Zhang Weiad1e9382008-04-18 13:33:41 -0700556 for (i = 0; i < priv->msg_tx_ring.size; i++)
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800557 dma_free_coherent(NULL, RIO_MSG_BUFFER_SIZE,
Zhang Weiad1e9382008-04-18 13:33:41 -0700558 priv->msg_tx_ring.virt_buffer[i],
559 priv->msg_tx_ring.phys_buffer[i]);
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800560
561 return rc;
562}
563
564/**
565 * rio_close_outb_mbox - Shut down MPC85xx outbound mailbox
566 * @mport: Master port implementing the outbound message unit
567 * @mbox: Mailbox to close
568 *
569 * Disables the outbound message unit, free all buffers, and
570 * frees the outbound message interrupt.
571 */
572void rio_close_outb_mbox(struct rio_mport *mport, int mbox)
573{
Zhang Weiad1e9382008-04-18 13:33:41 -0700574 struct rio_priv *priv = mport->priv;
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800575 /* Disable inbound message unit */
Zhang Weiad1e9382008-04-18 13:33:41 -0700576 out_be32(&priv->msg_regs->omr, 0);
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800577
578 /* Free ring */
Zhang Weiad1e9382008-04-18 13:33:41 -0700579 dma_free_coherent(NULL, priv->msg_tx_ring.size * RIO_MSG_DESC_SIZE,
580 priv->msg_tx_ring.virt, priv->msg_tx_ring.phys);
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800581
582 /* Free interrupt */
Zhang Weiad1e9382008-04-18 13:33:41 -0700583 free_irq(IRQ_RIO_TX(mport), (void *)mport);
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800584}
585
586/**
Zhang Weid02443a2008-04-18 13:33:38 -0700587 * fsl_rio_rx_handler - MPC85xx inbound message interrupt handler
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800588 * @irq: Linux interrupt number
589 * @dev_instance: Pointer to interrupt-specific data
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800590 *
591 * Handles inbound message interrupts. Executes a registered inbound
Simon Arlotta8de5ce2007-05-12 05:42:54 +1000592 * mailbox event handler and acks the interrupt occurrence.
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800593 */
594static irqreturn_t
Zhang Weid02443a2008-04-18 13:33:38 -0700595fsl_rio_rx_handler(int irq, void *dev_instance)
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800596{
597 int isr;
598 struct rio_mport *port = (struct rio_mport *)dev_instance;
Zhang Weiad1e9382008-04-18 13:33:41 -0700599 struct rio_priv *priv = port->priv;
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800600
Zhang Weiad1e9382008-04-18 13:33:41 -0700601 isr = in_be32(&priv->msg_regs->isr);
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800602
603 if (isr & RIO_MSG_ISR_TE) {
604 pr_info("RIO: inbound message reception error\n");
Zhang Weiad1e9382008-04-18 13:33:41 -0700605 out_be32((void *)&priv->msg_regs->isr, RIO_MSG_ISR_TE);
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800606 goto out;
607 }
608
609 /* XXX Need to check/dispatch until queue empty */
610 if (isr & RIO_MSG_ISR_DIQI) {
611 /*
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
615 * argument.
616 */
Zhang Weiad1e9382008-04-18 13:33:41 -0700617 port->inb_msg[0].mcback(port, priv->msg_rx_ring.dev_id, -1, -1);
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800618
619 /* Ack the queueing interrupt */
Zhang Weiad1e9382008-04-18 13:33:41 -0700620 out_be32(&priv->msg_regs->isr, RIO_MSG_ISR_DIQI);
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800621 }
622
623 out:
624 return IRQ_HANDLED;
625}
626
627/**
628 * rio_open_inb_mbox - Initialize MPC85xx inbound mailbox
629 * @mport: Master port implementing the inbound message unit
Matt Porter6978bbc2005-11-07 01:00:20 -0800630 * @dev_id: Device specific pointer to pass on event
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800631 * @mbox: Mailbox to open
632 * @entries: Number of entries in the inbound mailbox ring
633 *
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.
637 */
Matt Porter6978bbc2005-11-07 01:00:20 -0800638int rio_open_inb_mbox(struct rio_mport *mport, void *dev_id, int mbox, int entries)
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800639{
640 int i, rc = 0;
Zhang Weiad1e9382008-04-18 13:33:41 -0700641 struct rio_priv *priv = mport->priv;
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800642
643 if ((entries < RIO_MIN_RX_RING_SIZE) ||
644 (entries > RIO_MAX_RX_RING_SIZE) || (!is_power_of_2(entries))) {
645 rc = -EINVAL;
646 goto out;
647 }
648
649 /* Initialize client buffer ring */
Zhang Weiad1e9382008-04-18 13:33:41 -0700650 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;
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800655
656 /* Initialize inbound message ring */
Zhang Weiad1e9382008-04-18 13:33:41 -0700657 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) {
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800661 rc = -ENOMEM;
662 goto out;
663 }
664
665 /* Point dequeue/enqueue pointers at first entry in ring */
Zhang Weiad1e9382008-04-18 13:33:41 -0700666 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);
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800668
669 /* Clear interrupt status */
Zhang Weiad1e9382008-04-18 13:33:41 -0700670 out_be32(&priv->msg_regs->isr, 0x00000091);
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800671
672 /* Hook up inbound message handler */
Zhang Weiad1e9382008-04-18 13:33:41 -0700673 rc = request_irq(IRQ_RIO_RX(mport), fsl_rio_rx_handler, 0,
674 "msg_rx", (void *)mport);
675 if (rc < 0) {
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800676 dma_free_coherent(NULL, RIO_MSG_BUFFER_SIZE,
Zhang Weiad1e9382008-04-18 13:33:41 -0700677 priv->msg_tx_ring.virt_buffer[i],
678 priv->msg_tx_ring.phys_buffer[i]);
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800679 goto out;
680 }
681
682 /*
683 * Configure inbound message unit:
684 * Snooping
685 * 4KB max message size
686 * Unmask all interrupt sources
687 * Disable
688 */
Zhang Weiad1e9382008-04-18 13:33:41 -0700689 out_be32(&priv->msg_regs->imr, 0x001b0060);
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800690
691 /* Set number of queue entries */
Zhang Weiad1e9382008-04-18 13:33:41 -0700692 setbits32(&priv->msg_regs->imr, (get_bitmask_order(entries) - 2) << 12);
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800693
694 /* Now enable the unit */
Zhang Weiad1e9382008-04-18 13:33:41 -0700695 setbits32(&priv->msg_regs->imr, 0x1);
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800696
697 out:
698 return rc;
699}
700
701/**
702 * rio_close_inb_mbox - Shut down MPC85xx inbound mailbox
703 * @mport: Master port implementing the inbound message unit
704 * @mbox: Mailbox to close
705 *
706 * Disables the inbound message unit, free all buffers, and
707 * frees the inbound message interrupt.
708 */
709void rio_close_inb_mbox(struct rio_mport *mport, int mbox)
710{
Zhang Weiad1e9382008-04-18 13:33:41 -0700711 struct rio_priv *priv = mport->priv;
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800712 /* Disable inbound message unit */
Zhang Weiad1e9382008-04-18 13:33:41 -0700713 out_be32(&priv->msg_regs->imr, 0);
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800714
715 /* Free ring */
Zhang Weiad1e9382008-04-18 13:33:41 -0700716 dma_free_coherent(NULL, priv->msg_rx_ring.size * RIO_MAX_MSG_SIZE,
717 priv->msg_rx_ring.virt, priv->msg_rx_ring.phys);
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800718
719 /* Free interrupt */
Zhang Weiad1e9382008-04-18 13:33:41 -0700720 free_irq(IRQ_RIO_RX(mport), (void *)mport);
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800721}
722
723/**
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
728 *
729 * Adds the @buf buffer to the MPC85xx inbound message queue. Returns
730 * %0 on success or %-EINVAL on failure.
731 */
732int rio_hw_add_inb_buffer(struct rio_mport *mport, int mbox, void *buf)
733{
734 int rc = 0;
Zhang Weiad1e9382008-04-18 13:33:41 -0700735 struct rio_priv *priv = mport->priv;
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800736
737 pr_debug("RIO: rio_hw_add_inb_buffer(), msg_rx_ring.rx_slot %d\n",
Zhang Weiad1e9382008-04-18 13:33:41 -0700738 priv->msg_rx_ring.rx_slot);
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800739
Zhang Weiad1e9382008-04-18 13:33:41 -0700740 if (priv->msg_rx_ring.virt_buffer[priv->msg_rx_ring.rx_slot]) {
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800741 printk(KERN_ERR
742 "RIO: error adding inbound buffer %d, buffer exists\n",
Zhang Weiad1e9382008-04-18 13:33:41 -0700743 priv->msg_rx_ring.rx_slot);
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800744 rc = -EINVAL;
745 goto out;
746 }
747
Zhang Weiad1e9382008-04-18 13:33:41 -0700748 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;
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800751
752 out:
753 return rc;
754}
755
756EXPORT_SYMBOL_GPL(rio_hw_add_inb_buffer);
757
758/**
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
762 *
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.
765 */
766void *rio_hw_get_inb_message(struct rio_mport *mport, int mbox)
767{
Zhang Weiad1e9382008-04-18 13:33:41 -0700768 struct rio_priv *priv = mport->priv;
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800769 u32 phys_buf, virt_buf;
770 void *buf = NULL;
771 int buf_idx;
772
Zhang Weiad1e9382008-04-18 13:33:41 -0700773 phys_buf = in_be32(&priv->msg_regs->ifqdpar);
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800774
775 /* If no more messages, then bail out */
Zhang Weiad1e9382008-04-18 13:33:41 -0700776 if (phys_buf == in_be32(&priv->msg_regs->ifqepar))
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800777 goto out2;
778
Zhang Weiad1e9382008-04-18 13:33:41 -0700779 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];
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800783
784 if (!buf) {
785 printk(KERN_ERR
786 "RIO: inbound message copy failed, no buffers\n");
787 goto out1;
788 }
789
790 /* Copy max message size, caller is expected to allocate that big */
791 memcpy(buf, (void *)virt_buf, RIO_MAX_MSG_SIZE);
792
793 /* Clear the available buffer */
Zhang Weiad1e9382008-04-18 13:33:41 -0700794 priv->msg_rx_ring.virt_buffer[buf_idx] = NULL;
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800795
796 out1:
Zhang Weiad1e9382008-04-18 13:33:41 -0700797 setbits32(&priv->msg_regs->imr, RIO_MSG_IMR_MI);
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800798
799 out2:
800 return buf;
801}
802
803EXPORT_SYMBOL_GPL(rio_hw_get_inb_message);
804
805/**
Zhang Weid02443a2008-04-18 13:33:38 -0700806 * fsl_rio_dbell_handler - MPC85xx doorbell interrupt handler
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800807 * @irq: Linux interrupt number
808 * @dev_instance: Pointer to interrupt-specific data
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800809 *
810 * Handles doorbell interrupts. Parses a list of registered
811 * doorbell event handlers and executes a matching event handler.
812 */
813static irqreturn_t
Zhang Weid02443a2008-04-18 13:33:38 -0700814fsl_rio_dbell_handler(int irq, void *dev_instance)
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800815{
816 int dsr;
817 struct rio_mport *port = (struct rio_mport *)dev_instance;
Zhang Weiad1e9382008-04-18 13:33:41 -0700818 struct rio_priv *priv = port->priv;
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800819
Zhang Weiad1e9382008-04-18 13:33:41 -0700820 dsr = in_be32(&priv->msg_regs->dsr);
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800821
822 if (dsr & DOORBELL_DSR_TE) {
823 pr_info("RIO: doorbell reception error\n");
Zhang Weiad1e9382008-04-18 13:33:41 -0700824 out_be32(&priv->msg_regs->dsr, DOORBELL_DSR_TE);
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800825 goto out;
826 }
827
828 if (dsr & DOORBELL_DSR_QFI) {
829 pr_info("RIO: doorbell queue full\n");
Zhang Weiad1e9382008-04-18 13:33:41 -0700830 out_be32(&priv->msg_regs->dsr, DOORBELL_DSR_QFI);
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800831 goto out;
832 }
833
834 /* XXX Need to check/dispatch until queue empty */
835 if (dsr & DOORBELL_DSR_DIQI) {
836 u32 dmsg =
Zhang Weiad1e9382008-04-18 13:33:41 -0700837 (u32) priv->dbell_ring.virt +
838 (in_be32(&priv->msg_regs->dqdpar) & 0xfff);
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800839 struct rio_dbell *dbell;
840 int found = 0;
841
842 pr_debug
843 ("RIO: processing doorbell, sid %2.2x tid %2.2x info %4.4x\n",
844 DBELL_SID(dmsg), DBELL_TID(dmsg), DBELL_INF(dmsg));
845
846 list_for_each_entry(dbell, &port->dbells, node) {
847 if ((dbell->res->start <= DBELL_INF(dmsg)) &&
848 (dbell->res->end >= DBELL_INF(dmsg))) {
849 found = 1;
850 break;
851 }
852 }
853 if (found) {
Matt Porter6978bbc2005-11-07 01:00:20 -0800854 dbell->dinb(port, dbell->dev_id, DBELL_SID(dmsg), DBELL_TID(dmsg),
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800855 DBELL_INF(dmsg));
856 } else {
857 pr_debug
858 ("RIO: spurious doorbell, sid %2.2x tid %2.2x info %4.4x\n",
859 DBELL_SID(dmsg), DBELL_TID(dmsg), DBELL_INF(dmsg));
860 }
Zhang Weiad1e9382008-04-18 13:33:41 -0700861 setbits32(&priv->msg_regs->dmr, DOORBELL_DMR_DI);
862 out_be32(&priv->msg_regs->dsr, DOORBELL_DSR_DIQI);
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800863 }
864
865 out:
866 return IRQ_HANDLED;
867}
868
869/**
Zhang Weid02443a2008-04-18 13:33:38 -0700870 * fsl_rio_doorbell_init - MPC85xx doorbell interface init
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800871 * @mport: Master port implementing the inbound doorbell unit
872 *
873 * Initializes doorbell unit hardware and inbound DMA buffer
Zhang Weid02443a2008-04-18 13:33:38 -0700874 * ring. Called from fsl_rio_setup(). Returns %0 on success
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800875 * or %-ENOMEM on failure.
876 */
Zhang Weid02443a2008-04-18 13:33:38 -0700877static int fsl_rio_doorbell_init(struct rio_mport *mport)
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800878{
Zhang Weiad1e9382008-04-18 13:33:41 -0700879 struct rio_priv *priv = mport->priv;
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800880 int rc = 0;
881
882 /* Map outbound doorbell window immediately after maintenance window */
Zhang Weiad1e9382008-04-18 13:33:41 -0700883 priv->dbell_win = ioremap(mport->iores.start + RIO_MAINT_WIN_SIZE,
884 RIO_DBELL_WIN_SIZE);
885 if (!priv->dbell_win) {
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800886 printk(KERN_ERR
887 "RIO: unable to map outbound doorbell window\n");
888 rc = -ENOMEM;
889 goto out;
890 }
891
892 /* Initialize inbound doorbells */
Zhang Weiad1e9382008-04-18 13:33:41 -0700893 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) {
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800896 printk(KERN_ERR "RIO: unable allocate inbound doorbell ring\n");
897 rc = -ENOMEM;
Zhang Weiad1e9382008-04-18 13:33:41 -0700898 iounmap(priv->dbell_win);
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800899 goto out;
900 }
901
902 /* Point dequeue/enqueue pointers at first entry in ring */
Zhang Weiad1e9382008-04-18 13:33:41 -0700903 out_be32(&priv->msg_regs->dqdpar, (u32) priv->dbell_ring.phys);
904 out_be32(&priv->msg_regs->dqepar, (u32) priv->dbell_ring.phys);
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800905
906 /* Clear interrupt status */
Zhang Weiad1e9382008-04-18 13:33:41 -0700907 out_be32(&priv->msg_regs->dsr, 0x00000091);
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800908
909 /* Hook up doorbell handler */
Zhang Weiad1e9382008-04-18 13:33:41 -0700910 rc = request_irq(IRQ_RIO_BELL(mport), fsl_rio_dbell_handler, 0,
911 "dbell_rx", (void *)mport);
912 if (rc < 0) {
913 iounmap(priv->dbell_win);
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800914 dma_free_coherent(NULL, 512 * DOORBELL_MESSAGE_SIZE,
Zhang Weiad1e9382008-04-18 13:33:41 -0700915 priv->dbell_ring.virt, priv->dbell_ring.phys);
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800916 printk(KERN_ERR
917 "MPC85xx RIO: unable to request inbound doorbell irq");
918 goto out;
919 }
920
921 /* Configure doorbells for snooping, 512 entries, and enable */
Zhang Weiad1e9382008-04-18 13:33:41 -0700922 out_be32(&priv->msg_regs->dmr, 0x00108161);
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800923
924 out:
925 return rc;
926}
927
928static char *cmdline = NULL;
929
Zhang Weid02443a2008-04-18 13:33:38 -0700930static int fsl_rio_get_hdid(int index)
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800931{
932 /* XXX Need to parse multiple entries in some format */
933 if (!cmdline)
934 return -1;
935
936 return simple_strtol(cmdline, NULL, 0);
937}
938
Zhang Weid02443a2008-04-18 13:33:38 -0700939static int fsl_rio_get_cmdline(char *s)
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800940{
941 if (!s)
942 return 0;
943
944 cmdline = s;
945 return 1;
946}
947
Zhang Weid02443a2008-04-18 13:33:38 -0700948__setup("riohdid=", fsl_rio_get_cmdline);
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800949
Zhang Wei7f620df2008-04-18 13:33:44 -0700950static inline void fsl_rio_info(struct device *dev, u32 ccsr)
951{
952 const char *str;
953 if (ccsr & 1) {
954 /* Serial phy */
955 switch (ccsr >> 30) {
956 case 0:
957 str = "1";
958 break;
959 case 1:
960 str = "4";
961 break;
962 default:
963 str = "Unknown";
964 break;;
965 }
966 dev_info(dev, "Hardware port width: %s\n", str);
967
968 switch ((ccsr >> 27) & 7) {
969 case 0:
970 str = "Single-lane 0";
971 break;
972 case 1:
973 str = "Single-lane 2";
974 break;
975 case 2:
976 str = "Four-lane";
977 break;
978 default:
979 str = "Unknown";
980 break;
981 }
982 dev_info(dev, "Training connection status: %s\n", str);
983 } else {
984 /* Parallel phy */
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");
989 }
990}
991
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800992/**
Randy Dunlap9941d942008-04-30 16:45:58 -0700993 * fsl_rio_setup - Setup Freescale PowerPC RapidIO interface
994 * @dev: of_device pointer
Matt Porter2b0c28d7f2005-11-07 01:00:19 -0800995 *
996 * Initializes MPC85xx RapidIO hardware interface, configures
997 * master port with system-specific info, and registers the
998 * master port with the RapidIO subsystem.
999 */
Zhang Weicc2bb692008-04-18 13:33:41 -07001000int fsl_rio_setup(struct of_device *dev)
Matt Porter2b0c28d7f2005-11-07 01:00:19 -08001001{
1002 struct rio_ops *ops;
1003 struct rio_mport *port;
Zhang Weicc2bb692008-04-18 13:33:41 -07001004 struct rio_priv *priv;
1005 int rc = 0;
1006 const u32 *dt_range, *cell;
1007 struct resource regs;
1008 int rlen;
Zhang Wei61b26912008-04-18 13:33:44 -07001009 u32 ccsr;
Zhang Weicc2bb692008-04-18 13:33:41 -07001010 u64 law_start, law_size;
1011 int paw, aw, sw;
1012
1013 if (!dev->node) {
1014 dev_err(&dev->dev, "Device OF-Node is NULL");
1015 return -EFAULT;
1016 }
1017
1018 rc = of_address_to_resource(dev->node, 0, &regs);
1019 if (rc) {
1020 dev_err(&dev->dev, "Can't get %s property 'reg'\n",
1021 dev->node->full_name);
1022 return -EFAULT;
1023 }
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);
1027
1028 dt_range = of_get_property(dev->node, "ranges", &rlen);
1029 if (!dt_range) {
1030 dev_err(&dev->dev, "Can't get %s property 'ranges'\n",
1031 dev->node->full_name);
1032 return -EFAULT;
1033 }
1034
1035 /* Get node address wide */
1036 cell = of_get_property(dev->node, "#address-cells", NULL);
1037 if (cell)
1038 aw = *cell;
1039 else
1040 aw = of_n_addr_cells(dev->node);
1041 /* Get node size wide */
1042 cell = of_get_property(dev->node, "#size-cells", NULL);
1043 if (cell)
1044 sw = *cell;
1045 else
1046 sw = of_n_size_cells(dev->node);
1047 /* Get parent address wide wide */
1048 paw = of_n_addr_cells(dev->node);
1049
1050 law_start = of_read_number(dt_range + aw, paw);
1051 law_size = of_read_number(dt_range + aw + paw, sw);
1052
1053 dev_info(&dev->dev, "LAW start 0x%016llx, size 0x%016llx.\n",
1054 law_start, law_size);
Matt Porter2b0c28d7f2005-11-07 01:00:19 -08001055
1056 ops = kmalloc(sizeof(struct rio_ops), GFP_KERNEL);
Zhang Weid02443a2008-04-18 13:33:38 -07001057 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;
Matt Porter2b0c28d7f2005-11-07 01:00:19 -08001062
Zhang Weiad1e9382008-04-18 13:33:41 -07001063 port = kzalloc(sizeof(struct rio_mport), GFP_KERNEL);
Matt Porter2b0c28d7f2005-11-07 01:00:19 -08001064 port->id = 0;
1065 port->index = 0;
Zhang Weiad1e9382008-04-18 13:33:41 -07001066
1067 priv = kzalloc(sizeof(struct rio_priv), GFP_KERNEL);
1068 if (!priv) {
1069 printk(KERN_ERR "Can't alloc memory for 'priv'\n");
1070 rc = -ENOMEM;
1071 goto err;
1072 }
1073
Matt Porter2b0c28d7f2005-11-07 01:00:19 -08001074 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;
1078
Zhang Weicc2bb692008-04-18 13:33:41 -07001079 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);
1084
Matt Porter2b0c28d7f2005-11-07 01:00:19 -08001085 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");
1089
1090 port->ops = ops;
Zhang Weid02443a2008-04-18 13:33:38 -07001091 port->host_deviceid = fsl_rio_get_hdid(port->id);
Matt Porter2b0c28d7f2005-11-07 01:00:19 -08001092
Zhang Weiad1e9382008-04-18 13:33:41 -07001093 port->priv = priv;
Matt Porter2b0c28d7f2005-11-07 01:00:19 -08001094 rio_register_mport(port);
1095
Zhang Weicc2bb692008-04-18 13:33:41 -07001096 priv->regs_win = ioremap(regs.start, regs.end - regs.start + 1);
Zhang Weie0423232008-04-18 13:33:42 -07001097
Zhang Wei61b26912008-04-18 13:33:44 -07001098 /* 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" :
1104 "unknown"));
Zhang Wei7f620df2008-04-18 13:33:44 -07001105 /* 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:
1111 /* Disable ports */
1112 out_be32(priv->regs_win + RIO_CCSR, 0);
1113 /* Set 1x lane */
1114 setbits32(priv->regs_win + RIO_CCSR, 0x02000000);
1115 /* Enable ports */
1116 setbits32(priv->regs_win + RIO_CCSR, 0x00600000);
1117 break;
1118 case RIO_PHY_PARALLEL:
1119 /* Disable ports */
1120 out_be32(priv->regs_win + RIO_CCSR, 0x22000000);
1121 /* Enable ports */
1122 out_be32(priv->regs_win + RIO_CCSR, 0x44000000);
1123 break;
1124 }
1125 msleep(100);
1126 if (in_be32((priv->regs_win + RIO_ESCSR)) & 1) {
1127 dev_err(&dev->dev, "Port restart failed.\n");
1128 rc = -ENOLINK;
1129 goto err;
1130 }
1131 dev_info(&dev->dev, "Port restart success!\n");
1132 }
1133 fsl_rio_info(&dev->dev, ccsr);
Zhang Wei61b26912008-04-18 13:33:44 -07001134
Zhang Weie0423232008-04-18 13:33:42 -07001135 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);
1139
Zhang Weiad1e9382008-04-18 13:33:41 -07001140 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;
Zhang Wei61b26912008-04-18 13:33:44 -07001144 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));
1147
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);
Matt Porter2b0c28d7f2005-11-07 01:00:19 -08001151
1152 /* Configure maintenance transaction window */
Zhang Weiad1e9382008-04-18 13:33:41 -07001153 out_be32(&priv->maint_atmu_regs->rowbar, 0x000c0000);
1154 out_be32(&priv->maint_atmu_regs->rowar, 0x80077015);
Matt Porter2b0c28d7f2005-11-07 01:00:19 -08001155
Zhang Weiad1e9382008-04-18 13:33:41 -07001156 priv->maint_win = ioremap(law_start, RIO_MAINT_WIN_SIZE);
Matt Porter2b0c28d7f2005-11-07 01:00:19 -08001157
1158 /* Configure outbound doorbell window */
Zhang Weiad1e9382008-04-18 13:33:41 -07001159 out_be32(&priv->dbell_atmu_regs->rowbar, 0x000c0400);
1160 out_be32(&priv->dbell_atmu_regs->rowar, 0x8004200b);
Zhang Weid02443a2008-04-18 13:33:38 -07001161 fsl_rio_doorbell_init(port);
Zhang Weiad1e9382008-04-18 13:33:41 -07001162
Zhang Weicc2bb692008-04-18 13:33:41 -07001163 return 0;
Zhang Weiad1e9382008-04-18 13:33:41 -07001164err:
1165 if (priv)
1166 iounmap(priv->regs_win);
Zhang Weicc2bb692008-04-18 13:33:41 -07001167 kfree(ops);
Zhang Weiad1e9382008-04-18 13:33:41 -07001168 kfree(priv);
1169 kfree(port);
Zhang Weicc2bb692008-04-18 13:33:41 -07001170 return rc;
Matt Porter2b0c28d7f2005-11-07 01:00:19 -08001171}
Zhang Weicc2bb692008-04-18 13:33:41 -07001172
1173/* The probe function for RapidIO peer-to-peer network.
1174 */
1175static int __devinit fsl_of_rio_rpn_probe(struct of_device *dev,
1176 const struct of_device_id *match)
1177{
1178 int rc;
1179 printk(KERN_INFO "Setting up RapidIO peer-to-peer network %s\n",
1180 dev->node->full_name);
1181
1182 rc = fsl_rio_setup(dev);
1183 if (rc)
1184 goto out;
1185
1186 /* Enumerate all registered ports */
1187 rc = rio_init_mports();
1188out:
1189 return rc;
1190};
1191
1192static const struct of_device_id fsl_of_rio_rpn_ids[] = {
1193 {
1194 .compatible = "fsl,rapidio-delta",
1195 },
1196 {},
1197};
1198
1199static 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,
1203};
1204
1205static __init int fsl_of_rio_rpn_init(void)
1206{
1207 return of_register_platform_driver(&fsl_of_rio_rpn_driver);
1208}
1209
1210subsys_initcall(fsl_of_rio_rpn_init);