blob: 9725369d432a752bcf12436f708a64fcd1c540e3 [file] [log] [blame]
Matt Porter2b0c28d2005-11-07 01:00:19 -08001/*
Zhang Weid02443a2008-04-18 13:33:38 -07002 * Freescale MPC85xx/MPC86xx RapidIO support
Matt Porter2b0c28d2005-11-07 01:00:19 -08003 *
Thomas Mollbd4fb652010-05-26 14:44:05 -07004 * Copyright 2009 Sysgo AG
5 * Thomas Moll <thomas.moll@sysgo.com>
6 * - fixed maintenance access routines, check for aligned access
7 *
Alexandre Bounine5b2074a2010-05-26 14:44:00 -07008 * Copyright 2009 Integrated Device Technology, Inc.
9 * Alex Bounine <alexandre.bounine@idt.com>
10 * - Added Port-Write message handling
11 * - Added Machine Check exception handling
12 *
Zhang Weiad1e9382008-04-18 13:33:41 -070013 * Copyright (C) 2007, 2008 Freescale Semiconductor, Inc.
14 * Zhang Wei <wei.zhang@freescale.com>
15 *
Matt Porter2b0c28d2005-11-07 01:00:19 -080016 * Copyright 2005 MontaVista Software, Inc.
17 * Matt Porter <mporter@kernel.crashing.org>
18 *
19 * This program is free software; you can redistribute it and/or modify it
20 * under the terms of the GNU General Public License as published by the
21 * Free Software Foundation; either version 2 of the License, or (at your
22 * option) any later version.
23 */
24
Matt Porter2b0c28d2005-11-07 01:00:19 -080025#include <linux/init.h>
26#include <linux/module.h>
27#include <linux/types.h>
28#include <linux/dma-mapping.h>
29#include <linux/interrupt.h>
Anton Vorontsov0dbbbf12009-04-18 21:48:52 +040030#include <linux/device.h>
Matt Porter2b0c28d2005-11-07 01:00:19 -080031#include <linux/rio.h>
32#include <linux/rio_drv.h>
Zhang Weicc2bb692008-04-18 13:33:41 -070033#include <linux/of_platform.h>
Zhang Wei61b26912008-04-18 13:33:44 -070034#include <linux/delay.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090035#include <linux/slab.h>
Alexandre Bounine5b2074a2010-05-26 14:44:00 -070036#include <linux/kfifo.h>
Matt Porter2b0c28d2005-11-07 01:00:19 -080037
38#include <asm/io.h>
Alexandre Bouninea52c8f52010-05-26 14:44:00 -070039#include <asm/machdep.h>
40#include <asm/uaccess.h>
Matt Porter2b0c28d2005-11-07 01:00:19 -080041
Alexandre Bounine5b2074a2010-05-26 14:44:00 -070042#undef DEBUG_PW /* Port-Write debugging */
43
Zhang Weiad1e9382008-04-18 13:33:41 -070044/* RapidIO definition irq, which read from OF-tree */
45#define IRQ_RIO_BELL(m) (((struct rio_priv *)(m->priv))->bellirq)
46#define IRQ_RIO_TX(m) (((struct rio_priv *)(m->priv))->txirq)
47#define IRQ_RIO_RX(m) (((struct rio_priv *)(m->priv))->rxirq)
Alexandre Bounine5b2074a2010-05-26 14:44:00 -070048#define IRQ_RIO_PW(m) (((struct rio_priv *)(m->priv))->pwirq)
Zhang Weiad1e9382008-04-18 13:33:41 -070049
Matt Porter2b0c28d2005-11-07 01:00:19 -080050#define RIO_ATMU_REGS_OFFSET 0x10c00
Zhang Wei61b26912008-04-18 13:33:44 -070051#define RIO_P_MSG_REGS_OFFSET 0x11000
52#define RIO_S_MSG_REGS_OFFSET 0x13000
Alexandre Bounineaf84ca32010-10-27 15:34:34 -070053#define RIO_GCCSR 0x13c
Zhang Wei61b26912008-04-18 13:33:44 -070054#define RIO_ESCSR 0x158
55#define RIO_CCSR 0x15c
Alexandre Bounine5b2074a2010-05-26 14:44:00 -070056#define RIO_LTLEDCSR 0x0608
Alexandre Bouninea52c8f52010-05-26 14:44:00 -070057#define RIO_LTLEDCSR_IER 0x80000000
58#define RIO_LTLEDCSR_PRT 0x01000000
Alexandre Bounine5b2074a2010-05-26 14:44:00 -070059#define RIO_LTLEECSR 0x060c
60#define RIO_EPWISR 0x10010
Zhang Wei61b26912008-04-18 13:33:44 -070061#define RIO_ISR_AACR 0x10120
62#define RIO_ISR_AACR_AA 0x1 /* Accept All ID */
Matt Porter2b0c28d2005-11-07 01:00:19 -080063#define RIO_MAINT_WIN_SIZE 0x400000
64#define RIO_DBELL_WIN_SIZE 0x1000
65
66#define RIO_MSG_OMR_MUI 0x00000002
67#define RIO_MSG_OSR_TE 0x00000080
68#define RIO_MSG_OSR_QOI 0x00000020
69#define RIO_MSG_OSR_QFI 0x00000010
70#define RIO_MSG_OSR_MUB 0x00000004
71#define RIO_MSG_OSR_EOMI 0x00000002
72#define RIO_MSG_OSR_QEI 0x00000001
73
74#define RIO_MSG_IMR_MI 0x00000002
75#define RIO_MSG_ISR_TE 0x00000080
76#define RIO_MSG_ISR_QFI 0x00000010
77#define RIO_MSG_ISR_DIQI 0x00000001
78
Alexandre Bounine5b2074a2010-05-26 14:44:00 -070079#define RIO_IPWMR_SEN 0x00100000
80#define RIO_IPWMR_QFIE 0x00000100
81#define RIO_IPWMR_EIE 0x00000020
82#define RIO_IPWMR_CQ 0x00000002
83#define RIO_IPWMR_PWE 0x00000001
84
85#define RIO_IPWSR_QF 0x00100000
86#define RIO_IPWSR_TE 0x00000080
87#define RIO_IPWSR_QFI 0x00000010
88#define RIO_IPWSR_PWD 0x00000008
89#define RIO_IPWSR_PWB 0x00000004
90
Alexandre Bounine93e2cbd2010-10-27 15:34:28 -070091#define RIO_EPWISR_PINT 0x80000000
92#define RIO_EPWISR_PW 0x00000001
93
Matt Porter2b0c28d2005-11-07 01:00:19 -080094#define RIO_MSG_DESC_SIZE 32
95#define RIO_MSG_BUFFER_SIZE 4096
96#define RIO_MIN_TX_RING_SIZE 2
97#define RIO_MAX_TX_RING_SIZE 2048
98#define RIO_MIN_RX_RING_SIZE 2
99#define RIO_MAX_RX_RING_SIZE 2048
100
101#define DOORBELL_DMR_DI 0x00000002
102#define DOORBELL_DSR_TE 0x00000080
103#define DOORBELL_DSR_QFI 0x00000010
104#define DOORBELL_DSR_DIQI 0x00000001
Zhang Wei6c391032008-04-18 13:33:48 -0700105#define DOORBELL_TID_OFFSET 0x02
106#define DOORBELL_SID_OFFSET 0x04
Matt Porter2b0c28d2005-11-07 01:00:19 -0800107#define DOORBELL_INFO_OFFSET 0x06
108
109#define DOORBELL_MESSAGE_SIZE 0x08
Zhang Wei6c391032008-04-18 13:33:48 -0700110#define DBELL_SID(x) (*(u16 *)(x + DOORBELL_SID_OFFSET))
111#define DBELL_TID(x) (*(u16 *)(x + DOORBELL_TID_OFFSET))
Matt Porter2b0c28d2005-11-07 01:00:19 -0800112#define DBELL_INF(x) (*(u16 *)(x + DOORBELL_INFO_OFFSET))
113
Matt Porter2b0c28d2005-11-07 01:00:19 -0800114struct rio_atmu_regs {
115 u32 rowtar;
Zhang Wei61b26912008-04-18 13:33:44 -0700116 u32 rowtear;
Matt Porter2b0c28d2005-11-07 01:00:19 -0800117 u32 rowbar;
118 u32 pad2;
119 u32 rowar;
120 u32 pad3[3];
121};
122
123struct rio_msg_regs {
Shaohui Xieabd12fe2010-10-14 10:04:02 +0800124 u32 omr; /* 0xD_3000 - Outbound message 0 mode register */
125 u32 osr; /* 0xD_3004 - Outbound message 0 status register */
Matt Porter2b0c28d2005-11-07 01:00:19 -0800126 u32 pad1;
Shaohui Xieabd12fe2010-10-14 10:04:02 +0800127 u32 odqdpar; /* 0xD_300C - Outbound message 0 descriptor queue
128 dequeue pointer address register */
Matt Porter2b0c28d2005-11-07 01:00:19 -0800129 u32 pad2;
Shaohui Xieabd12fe2010-10-14 10:04:02 +0800130 u32 osar; /* 0xD_3014 - Outbound message 0 source address
131 register */
132 u32 odpr; /* 0xD_3018 - Outbound message 0 destination port
133 register */
134 u32 odatr; /* 0xD_301C - Outbound message 0 destination attributes
135 Register*/
136 u32 odcr; /* 0xD_3020 - Outbound message 0 double-word count
137 register */
Matt Porter2b0c28d2005-11-07 01:00:19 -0800138 u32 pad3;
Shaohui Xieabd12fe2010-10-14 10:04:02 +0800139 u32 odqepar; /* 0xD_3028 - Outbound message 0 descriptor queue
140 enqueue pointer address register */
Matt Porter2b0c28d2005-11-07 01:00:19 -0800141 u32 pad4[13];
Shaohui Xieabd12fe2010-10-14 10:04:02 +0800142 u32 imr; /* 0xD_3060 - Inbound message 0 mode register */
143 u32 isr; /* 0xD_3064 - Inbound message 0 status register */
Matt Porter2b0c28d2005-11-07 01:00:19 -0800144 u32 pad5;
Shaohui Xieabd12fe2010-10-14 10:04:02 +0800145 u32 ifqdpar; /* 0xD_306C - Inbound message 0 frame queue dequeue
146 pointer address register*/
Matt Porter2b0c28d2005-11-07 01:00:19 -0800147 u32 pad6;
Shaohui Xieabd12fe2010-10-14 10:04:02 +0800148 u32 ifqepar; /* 0xD_3074 - Inbound message 0 frame queue enqueue
149 pointer address register */
Zhang Wei61b26912008-04-18 13:33:44 -0700150 u32 pad7[226];
Shaohui Xieabd12fe2010-10-14 10:04:02 +0800151 u32 odmr; /* 0xD_3400 - Outbound doorbell mode register */
152 u32 odsr; /* 0xD_3404 - Outbound doorbell status register */
Zhang Wei61b26912008-04-18 13:33:44 -0700153 u32 res0[4];
Shaohui Xieabd12fe2010-10-14 10:04:02 +0800154 u32 oddpr; /* 0xD_3418 - Outbound doorbell destination port
155 register */
156 u32 oddatr; /* 0xD_341c - Outbound doorbell destination attributes
157 register */
Zhang Wei61b26912008-04-18 13:33:44 -0700158 u32 res1[3];
Shaohui Xieabd12fe2010-10-14 10:04:02 +0800159 u32 odretcr; /* 0xD_342C - Outbound doorbell retry error threshold
160 configuration register */
Zhang Wei61b26912008-04-18 13:33:44 -0700161 u32 res2[12];
Shaohui Xieabd12fe2010-10-14 10:04:02 +0800162 u32 dmr; /* 0xD_3460 - Inbound doorbell mode register */
163 u32 dsr; /* 0xD_3464 - Inbound doorbell status register */
Matt Porter2b0c28d2005-11-07 01:00:19 -0800164 u32 pad8;
Shaohui Xieabd12fe2010-10-14 10:04:02 +0800165 u32 dqdpar; /* 0xD_346C - Inbound doorbell queue dequeue Pointer
166 address register */
Matt Porter2b0c28d2005-11-07 01:00:19 -0800167 u32 pad9;
Shaohui Xieabd12fe2010-10-14 10:04:02 +0800168 u32 dqepar; /* 0xD_3474 - Inbound doorbell Queue enqueue pointer
169 address register */
Matt Porter2b0c28d2005-11-07 01:00:19 -0800170 u32 pad10[26];
Shaohui Xieabd12fe2010-10-14 10:04:02 +0800171 u32 pwmr; /* 0xD_34E0 - Inbound port-write mode register */
172 u32 pwsr; /* 0xD_34E4 - Inbound port-write status register */
173 u32 epwqbar; /* 0xD_34E8 - Extended Port-Write Queue Base Address
174 register */
175 u32 pwqbar; /* 0xD_34EC - Inbound port-write queue base address
176 register */
Matt Porter2b0c28d2005-11-07 01:00:19 -0800177};
178
179struct rio_tx_desc {
180 u32 res1;
181 u32 saddr;
182 u32 dport;
183 u32 dattr;
184 u32 res2;
185 u32 res3;
186 u32 dwcnt;
187 u32 res4;
188};
189
Zhang Weiad1e9382008-04-18 13:33:41 -0700190struct rio_dbell_ring {
Matt Porter2b0c28d2005-11-07 01:00:19 -0800191 void *virt;
192 dma_addr_t phys;
Zhang Weiad1e9382008-04-18 13:33:41 -0700193};
Matt Porter2b0c28d2005-11-07 01:00:19 -0800194
Zhang Weiad1e9382008-04-18 13:33:41 -0700195struct rio_msg_tx_ring {
Matt Porter2b0c28d2005-11-07 01:00:19 -0800196 void *virt;
197 dma_addr_t phys;
198 void *virt_buffer[RIO_MAX_TX_RING_SIZE];
199 dma_addr_t phys_buffer[RIO_MAX_TX_RING_SIZE];
200 int tx_slot;
201 int size;
Matt Porter6978bbc2005-11-07 01:00:20 -0800202 void *dev_id;
Zhang Weiad1e9382008-04-18 13:33:41 -0700203};
Matt Porter2b0c28d2005-11-07 01:00:19 -0800204
Zhang Weiad1e9382008-04-18 13:33:41 -0700205struct rio_msg_rx_ring {
Matt Porter2b0c28d2005-11-07 01:00:19 -0800206 void *virt;
207 dma_addr_t phys;
208 void *virt_buffer[RIO_MAX_RX_RING_SIZE];
209 int rx_slot;
210 int size;
Matt Porter6978bbc2005-11-07 01:00:20 -0800211 void *dev_id;
Zhang Weiad1e9382008-04-18 13:33:41 -0700212};
213
Alexandre Bounine5b2074a2010-05-26 14:44:00 -0700214struct rio_port_write_msg {
215 void *virt;
216 dma_addr_t phys;
217 u32 msg_count;
218 u32 err_count;
219 u32 discard_count;
220};
221
Zhang Weiad1e9382008-04-18 13:33:41 -0700222struct rio_priv {
Anton Vorontsov0dbbbf12009-04-18 21:48:52 +0400223 struct device *dev;
Zhang Weiad1e9382008-04-18 13:33:41 -0700224 void __iomem *regs_win;
225 struct rio_atmu_regs __iomem *atmu_regs;
226 struct rio_atmu_regs __iomem *maint_atmu_regs;
227 struct rio_atmu_regs __iomem *dbell_atmu_regs;
228 void __iomem *dbell_win;
229 void __iomem *maint_win;
230 struct rio_msg_regs __iomem *msg_regs;
231 struct rio_dbell_ring dbell_ring;
232 struct rio_msg_tx_ring msg_tx_ring;
233 struct rio_msg_rx_ring msg_rx_ring;
Alexandre Bounine5b2074a2010-05-26 14:44:00 -0700234 struct rio_port_write_msg port_write_msg;
Zhang Weiad1e9382008-04-18 13:33:41 -0700235 int bellirq;
236 int txirq;
237 int rxirq;
Alexandre Bounine5b2074a2010-05-26 14:44:00 -0700238 int pwirq;
239 struct work_struct pw_work;
240 struct kfifo pw_fifo;
241 spinlock_t pw_fifo_lock;
Zhang Weiad1e9382008-04-18 13:33:41 -0700242};
Matt Porter2b0c28d2005-11-07 01:00:19 -0800243
Alexandre Bouninea52c8f52010-05-26 14:44:00 -0700244#define __fsl_read_rio_config(x, addr, err, op) \
245 __asm__ __volatile__( \
246 "1: "op" %1,0(%2)\n" \
247 " eieio\n" \
248 "2:\n" \
249 ".section .fixup,\"ax\"\n" \
250 "3: li %1,-1\n" \
251 " li %0,%3\n" \
252 " b 2b\n" \
253 ".section __ex_table,\"a\"\n" \
254 " .align 2\n" \
255 " .long 1b,3b\n" \
256 ".text" \
257 : "=r" (err), "=r" (x) \
258 : "b" (addr), "i" (-EFAULT), "0" (err))
259
260static void __iomem *rio_regs_win;
261
Li Yangff33f182010-06-18 14:24:20 +0800262#ifdef CONFIG_E500
Alexandre Bouninea52c8f52010-05-26 14:44:00 -0700263static int (*saved_mcheck_exception)(struct pt_regs *regs);
264
265static int fsl_rio_mcheck_exception(struct pt_regs *regs)
266{
267 const struct exception_table_entry *entry = NULL;
Li Yangff33f182010-06-18 14:24:20 +0800268 unsigned long reason = mfspr(SPRN_MCSR);
Alexandre Bouninea52c8f52010-05-26 14:44:00 -0700269
270 if (reason & MCSR_BUS_RBERR) {
271 reason = in_be32((u32 *)(rio_regs_win + RIO_LTLEDCSR));
272 if (reason & (RIO_LTLEDCSR_IER | RIO_LTLEDCSR_PRT)) {
273 /* Check if we are prepared to handle this fault */
274 entry = search_exception_tables(regs->nip);
275 if (entry) {
276 pr_debug("RIO: %s - MC Exception handled\n",
277 __func__);
278 out_be32((u32 *)(rio_regs_win + RIO_LTLEDCSR),
279 0);
280 regs->msr |= MSR_RI;
281 regs->nip = entry->fixup;
282 return 1;
283 }
284 }
285 }
286
287 if (saved_mcheck_exception)
288 return saved_mcheck_exception(regs);
289 else
290 return cur_cpu_spec->machine_check(regs);
291}
Li Yangff33f182010-06-18 14:24:20 +0800292#endif
Alexandre Bouninea52c8f52010-05-26 14:44:00 -0700293
Matt Porter2b0c28d2005-11-07 01:00:19 -0800294/**
Zhang Weid02443a2008-04-18 13:33:38 -0700295 * fsl_rio_doorbell_send - Send a MPC85xx doorbell message
Randy Dunlap9941d942008-04-30 16:45:58 -0700296 * @mport: RapidIO master port info
Matt Porter2b0c28d2005-11-07 01:00:19 -0800297 * @index: ID of RapidIO interface
298 * @destid: Destination ID of target device
299 * @data: 16-bit info field of RapidIO doorbell message
300 *
301 * Sends a MPC85xx doorbell message. Returns %0 on success or
302 * %-EINVAL on failure.
303 */
Zhang Weiad1e9382008-04-18 13:33:41 -0700304static int fsl_rio_doorbell_send(struct rio_mport *mport,
305 int index, u16 destid, u16 data)
Matt Porter2b0c28d2005-11-07 01:00:19 -0800306{
Zhang Weiad1e9382008-04-18 13:33:41 -0700307 struct rio_priv *priv = mport->priv;
Zhang Weid02443a2008-04-18 13:33:38 -0700308 pr_debug("fsl_doorbell_send: index %d destid %4.4x data %4.4x\n",
Matt Porter2b0c28d2005-11-07 01:00:19 -0800309 index, destid, data);
Zhang Wei61b26912008-04-18 13:33:44 -0700310 switch (mport->phy_type) {
311 case RIO_PHY_PARALLEL:
312 out_be32(&priv->dbell_atmu_regs->rowtar, destid << 22);
313 out_be16(priv->dbell_win, data);
314 break;
315 case RIO_PHY_SERIAL:
316 /* In the serial version silicons, such as MPC8548, MPC8641,
317 * below operations is must be.
318 */
319 out_be32(&priv->msg_regs->odmr, 0x00000000);
320 out_be32(&priv->msg_regs->odretcr, 0x00000004);
321 out_be32(&priv->msg_regs->oddpr, destid << 16);
322 out_be32(&priv->msg_regs->oddatr, data);
323 out_be32(&priv->msg_regs->odmr, 0x00000001);
324 break;
325 }
Matt Porter2b0c28d2005-11-07 01:00:19 -0800326
327 return 0;
328}
329
330/**
Zhang Weid02443a2008-04-18 13:33:38 -0700331 * fsl_local_config_read - Generate a MPC85xx local config space read
Randy Dunlap9941d942008-04-30 16:45:58 -0700332 * @mport: RapidIO master port info
Matt Porter2b0c28d2005-11-07 01:00:19 -0800333 * @index: ID of RapdiIO interface
334 * @offset: Offset into configuration space
335 * @len: Length (in bytes) of the maintenance transaction
336 * @data: Value to be read into
337 *
338 * Generates a MPC85xx local configuration space read. Returns %0 on
339 * success or %-EINVAL on failure.
340 */
Zhang Weiad1e9382008-04-18 13:33:41 -0700341static int fsl_local_config_read(struct rio_mport *mport,
342 int index, u32 offset, int len, u32 *data)
Matt Porter2b0c28d2005-11-07 01:00:19 -0800343{
Zhang Weiad1e9382008-04-18 13:33:41 -0700344 struct rio_priv *priv = mport->priv;
Zhang Weid02443a2008-04-18 13:33:38 -0700345 pr_debug("fsl_local_config_read: index %d offset %8.8x\n", index,
Matt Porter2b0c28d2005-11-07 01:00:19 -0800346 offset);
Zhang Weiad1e9382008-04-18 13:33:41 -0700347 *data = in_be32(priv->regs_win + offset);
Matt Porter2b0c28d2005-11-07 01:00:19 -0800348
349 return 0;
350}
351
352/**
Zhang Weid02443a2008-04-18 13:33:38 -0700353 * fsl_local_config_write - Generate a MPC85xx local config space write
Randy Dunlap9941d942008-04-30 16:45:58 -0700354 * @mport: RapidIO master port info
Matt Porter2b0c28d2005-11-07 01:00:19 -0800355 * @index: ID of RapdiIO interface
356 * @offset: Offset into configuration space
357 * @len: Length (in bytes) of the maintenance transaction
358 * @data: Value to be written
359 *
360 * Generates a MPC85xx local configuration space write. Returns %0 on
361 * success or %-EINVAL on failure.
362 */
Zhang Weiad1e9382008-04-18 13:33:41 -0700363static int fsl_local_config_write(struct rio_mport *mport,
364 int index, u32 offset, int len, u32 data)
Matt Porter2b0c28d2005-11-07 01:00:19 -0800365{
Zhang Weiad1e9382008-04-18 13:33:41 -0700366 struct rio_priv *priv = mport->priv;
Matt Porter2b0c28d2005-11-07 01:00:19 -0800367 pr_debug
Zhang Weid02443a2008-04-18 13:33:38 -0700368 ("fsl_local_config_write: index %d offset %8.8x data %8.8x\n",
Matt Porter2b0c28d2005-11-07 01:00:19 -0800369 index, offset, data);
Zhang Weiad1e9382008-04-18 13:33:41 -0700370 out_be32(priv->regs_win + offset, data);
Matt Porter2b0c28d2005-11-07 01:00:19 -0800371
372 return 0;
373}
374
375/**
Zhang Weid02443a2008-04-18 13:33:38 -0700376 * fsl_rio_config_read - Generate a MPC85xx read maintenance transaction
Randy Dunlap9941d942008-04-30 16:45:58 -0700377 * @mport: RapidIO master port info
Matt Porter2b0c28d2005-11-07 01:00:19 -0800378 * @index: ID of RapdiIO interface
379 * @destid: Destination ID of transaction
380 * @hopcount: Number of hops to target device
381 * @offset: Offset into configuration space
382 * @len: Length (in bytes) of the maintenance transaction
383 * @val: Location to be read into
384 *
385 * Generates a MPC85xx read maintenance transaction. Returns %0 on
386 * success or %-EINVAL on failure.
387 */
388static int
Zhang Weiad1e9382008-04-18 13:33:41 -0700389fsl_rio_config_read(struct rio_mport *mport, int index, u16 destid,
390 u8 hopcount, u32 offset, int len, u32 *val)
Matt Porter2b0c28d2005-11-07 01:00:19 -0800391{
Zhang Weiad1e9382008-04-18 13:33:41 -0700392 struct rio_priv *priv = mport->priv;
Matt Porter2b0c28d2005-11-07 01:00:19 -0800393 u8 *data;
Alexandre Bouninea52c8f52010-05-26 14:44:00 -0700394 u32 rval, err = 0;
Matt Porter2b0c28d2005-11-07 01:00:19 -0800395
396 pr_debug
Zhang Weid02443a2008-04-18 13:33:38 -0700397 ("fsl_rio_config_read: index %d destid %d hopcount %d offset %8.8x len %d\n",
Matt Porter2b0c28d2005-11-07 01:00:19 -0800398 index, destid, hopcount, offset, len);
Matt Porter2b0c28d2005-11-07 01:00:19 -0800399
Thomas Mollbd4fb652010-05-26 14:44:05 -0700400 /* 16MB maintenance window possible */
401 /* allow only aligned access to maintenance registers */
402 if (offset > (0x1000000 - len) || !IS_ALIGNED(offset, len))
403 return -EINVAL;
404
405 out_be32(&priv->maint_atmu_regs->rowtar,
406 (destid << 22) | (hopcount << 12) | (offset >> 12));
407 out_be32(&priv->maint_atmu_regs->rowtear, (destid >> 10));
408
409 data = (u8 *) priv->maint_win + (offset & (RIO_MAINT_WIN_SIZE - 1));
Matt Porter2b0c28d2005-11-07 01:00:19 -0800410 switch (len) {
411 case 1:
Alexandre Bouninea52c8f52010-05-26 14:44:00 -0700412 __fsl_read_rio_config(rval, data, err, "lbz");
Matt Porter2b0c28d2005-11-07 01:00:19 -0800413 break;
414 case 2:
Alexandre Bouninea52c8f52010-05-26 14:44:00 -0700415 __fsl_read_rio_config(rval, data, err, "lhz");
Matt Porter2b0c28d2005-11-07 01:00:19 -0800416 break;
Thomas Mollbd4fb652010-05-26 14:44:05 -0700417 case 4:
Alexandre Bouninea52c8f52010-05-26 14:44:00 -0700418 __fsl_read_rio_config(rval, data, err, "lwz");
Matt Porter2b0c28d2005-11-07 01:00:19 -0800419 break;
Thomas Mollbd4fb652010-05-26 14:44:05 -0700420 default:
421 return -EINVAL;
Matt Porter2b0c28d2005-11-07 01:00:19 -0800422 }
423
Alexandre Bouninea52c8f52010-05-26 14:44:00 -0700424 if (err) {
425 pr_debug("RIO: cfg_read error %d for %x:%x:%x\n",
426 err, destid, hopcount, offset);
427 }
428
429 *val = rval;
430
431 return err;
Matt Porter2b0c28d2005-11-07 01:00:19 -0800432}
433
434/**
Zhang Weid02443a2008-04-18 13:33:38 -0700435 * fsl_rio_config_write - Generate a MPC85xx write maintenance transaction
Randy Dunlap9941d942008-04-30 16:45:58 -0700436 * @mport: RapidIO master port info
Matt Porter2b0c28d2005-11-07 01:00:19 -0800437 * @index: ID of RapdiIO interface
438 * @destid: Destination ID of transaction
439 * @hopcount: Number of hops to target device
440 * @offset: Offset into configuration space
441 * @len: Length (in bytes) of the maintenance transaction
442 * @val: Value to be written
443 *
444 * Generates an MPC85xx write maintenance transaction. Returns %0 on
445 * success or %-EINVAL on failure.
446 */
447static int
Zhang Weiad1e9382008-04-18 13:33:41 -0700448fsl_rio_config_write(struct rio_mport *mport, int index, u16 destid,
449 u8 hopcount, u32 offset, int len, u32 val)
Matt Porter2b0c28d2005-11-07 01:00:19 -0800450{
Zhang Weiad1e9382008-04-18 13:33:41 -0700451 struct rio_priv *priv = mport->priv;
Matt Porter2b0c28d2005-11-07 01:00:19 -0800452 u8 *data;
453 pr_debug
Zhang Weid02443a2008-04-18 13:33:38 -0700454 ("fsl_rio_config_write: index %d destid %d hopcount %d offset %8.8x len %d val %8.8x\n",
Matt Porter2b0c28d2005-11-07 01:00:19 -0800455 index, destid, hopcount, offset, len, val);
Matt Porter2b0c28d2005-11-07 01:00:19 -0800456
Thomas Mollbd4fb652010-05-26 14:44:05 -0700457 /* 16MB maintenance windows possible */
458 /* allow only aligned access to maintenance registers */
459 if (offset > (0x1000000 - len) || !IS_ALIGNED(offset, len))
460 return -EINVAL;
461
462 out_be32(&priv->maint_atmu_regs->rowtar,
463 (destid << 22) | (hopcount << 12) | (offset >> 12));
464 out_be32(&priv->maint_atmu_regs->rowtear, (destid >> 10));
465
466 data = (u8 *) priv->maint_win + (offset & (RIO_MAINT_WIN_SIZE - 1));
Matt Porter2b0c28d2005-11-07 01:00:19 -0800467 switch (len) {
468 case 1:
469 out_8((u8 *) data, val);
470 break;
471 case 2:
472 out_be16((u16 *) data, val);
473 break;
Thomas Mollbd4fb652010-05-26 14:44:05 -0700474 case 4:
Matt Porter2b0c28d2005-11-07 01:00:19 -0800475 out_be32((u32 *) data, val);
476 break;
Thomas Mollbd4fb652010-05-26 14:44:05 -0700477 default:
478 return -EINVAL;
Matt Porter2b0c28d2005-11-07 01:00:19 -0800479 }
480
481 return 0;
482}
483
484/**
485 * rio_hw_add_outb_message - Add message to the MPC85xx outbound message queue
486 * @mport: Master port with outbound message queue
487 * @rdev: Target of outbound message
488 * @mbox: Outbound mailbox
489 * @buffer: Message to add to outbound queue
490 * @len: Length of message
491 *
492 * Adds the @buffer message to the MPC85xx outbound message queue. Returns
493 * %0 on success or %-EINVAL on failure.
494 */
495int
496rio_hw_add_outb_message(struct rio_mport *mport, struct rio_dev *rdev, int mbox,
497 void *buffer, size_t len)
498{
Zhang Weiad1e9382008-04-18 13:33:41 -0700499 struct rio_priv *priv = mport->priv;
Matt Porter2b0c28d2005-11-07 01:00:19 -0800500 u32 omr;
Zhang Weiad1e9382008-04-18 13:33:41 -0700501 struct rio_tx_desc *desc = (struct rio_tx_desc *)priv->msg_tx_ring.virt
502 + priv->msg_tx_ring.tx_slot;
Matt Porter2b0c28d2005-11-07 01:00:19 -0800503 int ret = 0;
504
505 pr_debug
506 ("RIO: rio_hw_add_outb_message(): destid %4.4x mbox %d buffer %8.8x len %8.8x\n",
507 rdev->destid, mbox, (int)buffer, len);
508
509 if ((len < 8) || (len > RIO_MAX_MSG_SIZE)) {
510 ret = -EINVAL;
511 goto out;
512 }
513
514 /* Copy and clear rest of buffer */
Zhang Weiad1e9382008-04-18 13:33:41 -0700515 memcpy(priv->msg_tx_ring.virt_buffer[priv->msg_tx_ring.tx_slot], buffer,
516 len);
Matt Porter2b0c28d2005-11-07 01:00:19 -0800517 if (len < (RIO_MAX_MSG_SIZE - 4))
Zhang Weiad1e9382008-04-18 13:33:41 -0700518 memset(priv->msg_tx_ring.virt_buffer[priv->msg_tx_ring.tx_slot]
519 + len, 0, RIO_MAX_MSG_SIZE - len);
Matt Porter2b0c28d2005-11-07 01:00:19 -0800520
Zhang Wei61b26912008-04-18 13:33:44 -0700521 switch (mport->phy_type) {
522 case RIO_PHY_PARALLEL:
523 /* Set mbox field for message */
524 desc->dport = mbox & 0x3;
Matt Porter2b0c28d2005-11-07 01:00:19 -0800525
Zhang Wei61b26912008-04-18 13:33:44 -0700526 /* Enable EOMI interrupt, set priority, and set destid */
527 desc->dattr = 0x28000000 | (rdev->destid << 2);
528 break;
529 case RIO_PHY_SERIAL:
530 /* Set mbox field for message, and set destid */
531 desc->dport = (rdev->destid << 16) | (mbox & 0x3);
532
533 /* Enable EOMI interrupt and priority */
534 desc->dattr = 0x28000000;
535 break;
536 }
Matt Porter2b0c28d2005-11-07 01:00:19 -0800537
538 /* Set transfer size aligned to next power of 2 (in double words) */
539 desc->dwcnt = is_power_of_2(len) ? len : 1 << get_bitmask_order(len);
540
541 /* Set snooping and source buffer address */
Zhang Weiad1e9382008-04-18 13:33:41 -0700542 desc->saddr = 0x00000004
543 | priv->msg_tx_ring.phys_buffer[priv->msg_tx_ring.tx_slot];
Matt Porter2b0c28d2005-11-07 01:00:19 -0800544
545 /* Increment enqueue pointer */
Zhang Weiad1e9382008-04-18 13:33:41 -0700546 omr = in_be32(&priv->msg_regs->omr);
547 out_be32(&priv->msg_regs->omr, omr | RIO_MSG_OMR_MUI);
Matt Porter2b0c28d2005-11-07 01:00:19 -0800548
549 /* Go to next descriptor */
Zhang Weiad1e9382008-04-18 13:33:41 -0700550 if (++priv->msg_tx_ring.tx_slot == priv->msg_tx_ring.size)
551 priv->msg_tx_ring.tx_slot = 0;
Matt Porter2b0c28d2005-11-07 01:00:19 -0800552
553 out:
554 return ret;
555}
556
557EXPORT_SYMBOL_GPL(rio_hw_add_outb_message);
558
559/**
Zhang Weid02443a2008-04-18 13:33:38 -0700560 * fsl_rio_tx_handler - MPC85xx outbound message interrupt handler
Matt Porter2b0c28d2005-11-07 01:00:19 -0800561 * @irq: Linux interrupt number
562 * @dev_instance: Pointer to interrupt-specific data
Matt Porter2b0c28d2005-11-07 01:00:19 -0800563 *
564 * Handles outbound message interrupts. Executes a register outbound
Simon Arlotta8de5ce2007-05-12 05:42:54 +1000565 * mailbox event handler and acks the interrupt occurrence.
Matt Porter2b0c28d2005-11-07 01:00:19 -0800566 */
567static irqreturn_t
Zhang Weid02443a2008-04-18 13:33:38 -0700568fsl_rio_tx_handler(int irq, void *dev_instance)
Matt Porter2b0c28d2005-11-07 01:00:19 -0800569{
570 int osr;
571 struct rio_mport *port = (struct rio_mport *)dev_instance;
Zhang Weiad1e9382008-04-18 13:33:41 -0700572 struct rio_priv *priv = port->priv;
Matt Porter2b0c28d2005-11-07 01:00:19 -0800573
Zhang Weiad1e9382008-04-18 13:33:41 -0700574 osr = in_be32(&priv->msg_regs->osr);
Matt Porter2b0c28d2005-11-07 01:00:19 -0800575
576 if (osr & RIO_MSG_OSR_TE) {
577 pr_info("RIO: outbound message transmission error\n");
Zhang Weiad1e9382008-04-18 13:33:41 -0700578 out_be32(&priv->msg_regs->osr, RIO_MSG_OSR_TE);
Matt Porter2b0c28d2005-11-07 01:00:19 -0800579 goto out;
580 }
581
582 if (osr & RIO_MSG_OSR_QOI) {
583 pr_info("RIO: outbound message queue overflow\n");
Zhang Weiad1e9382008-04-18 13:33:41 -0700584 out_be32(&priv->msg_regs->osr, RIO_MSG_OSR_QOI);
Matt Porter2b0c28d2005-11-07 01:00:19 -0800585 goto out;
586 }
587
588 if (osr & RIO_MSG_OSR_EOMI) {
Zhang Weiad1e9382008-04-18 13:33:41 -0700589 u32 dqp = in_be32(&priv->msg_regs->odqdpar);
590 int slot = (dqp - priv->msg_tx_ring.phys) >> 5;
591 port->outb_msg[0].mcback(port, priv->msg_tx_ring.dev_id, -1,
592 slot);
Matt Porter2b0c28d2005-11-07 01:00:19 -0800593
594 /* Ack the end-of-message interrupt */
Zhang Weiad1e9382008-04-18 13:33:41 -0700595 out_be32(&priv->msg_regs->osr, RIO_MSG_OSR_EOMI);
Matt Porter2b0c28d2005-11-07 01:00:19 -0800596 }
597
598 out:
599 return IRQ_HANDLED;
600}
601
602/**
603 * rio_open_outb_mbox - Initialize MPC85xx outbound mailbox
604 * @mport: Master port implementing the outbound message unit
Matt Porter6978bbc2005-11-07 01:00:20 -0800605 * @dev_id: Device specific pointer to pass on event
Matt Porter2b0c28d2005-11-07 01:00:19 -0800606 * @mbox: Mailbox to open
607 * @entries: Number of entries in the outbound mailbox ring
608 *
609 * Initializes buffer ring, request the outbound message interrupt,
610 * and enables the outbound message unit. Returns %0 on success and
611 * %-EINVAL or %-ENOMEM on failure.
612 */
Matt Porter6978bbc2005-11-07 01:00:20 -0800613int rio_open_outb_mbox(struct rio_mport *mport, void *dev_id, int mbox, int entries)
Matt Porter2b0c28d2005-11-07 01:00:19 -0800614{
615 int i, j, rc = 0;
Zhang Weiad1e9382008-04-18 13:33:41 -0700616 struct rio_priv *priv = mport->priv;
Matt Porter2b0c28d2005-11-07 01:00:19 -0800617
618 if ((entries < RIO_MIN_TX_RING_SIZE) ||
619 (entries > RIO_MAX_TX_RING_SIZE) || (!is_power_of_2(entries))) {
620 rc = -EINVAL;
621 goto out;
622 }
623
624 /* Initialize shadow copy ring */
Zhang Weiad1e9382008-04-18 13:33:41 -0700625 priv->msg_tx_ring.dev_id = dev_id;
626 priv->msg_tx_ring.size = entries;
Matt Porter2b0c28d2005-11-07 01:00:19 -0800627
Zhang Weiad1e9382008-04-18 13:33:41 -0700628 for (i = 0; i < priv->msg_tx_ring.size; i++) {
629 priv->msg_tx_ring.virt_buffer[i] =
Anton Vorontsov0dbbbf12009-04-18 21:48:52 +0400630 dma_alloc_coherent(priv->dev, RIO_MSG_BUFFER_SIZE,
Zhang Weiad1e9382008-04-18 13:33:41 -0700631 &priv->msg_tx_ring.phys_buffer[i], GFP_KERNEL);
632 if (!priv->msg_tx_ring.virt_buffer[i]) {
Matt Porter2b0c28d2005-11-07 01:00:19 -0800633 rc = -ENOMEM;
Zhang Weiad1e9382008-04-18 13:33:41 -0700634 for (j = 0; j < priv->msg_tx_ring.size; j++)
635 if (priv->msg_tx_ring.virt_buffer[j])
Anton Vorontsov0dbbbf12009-04-18 21:48:52 +0400636 dma_free_coherent(priv->dev,
Zhang Weiad1e9382008-04-18 13:33:41 -0700637 RIO_MSG_BUFFER_SIZE,
638 priv->msg_tx_ring.
639 virt_buffer[j],
640 priv->msg_tx_ring.
641 phys_buffer[j]);
Matt Porter2b0c28d2005-11-07 01:00:19 -0800642 goto out;
643 }
644 }
645
646 /* Initialize outbound message descriptor ring */
Anton Vorontsov0dbbbf12009-04-18 21:48:52 +0400647 priv->msg_tx_ring.virt = dma_alloc_coherent(priv->dev,
Zhang Weiad1e9382008-04-18 13:33:41 -0700648 priv->msg_tx_ring.size * RIO_MSG_DESC_SIZE,
649 &priv->msg_tx_ring.phys, GFP_KERNEL);
650 if (!priv->msg_tx_ring.virt) {
Matt Porter2b0c28d2005-11-07 01:00:19 -0800651 rc = -ENOMEM;
652 goto out_dma;
653 }
Zhang Weiad1e9382008-04-18 13:33:41 -0700654 memset(priv->msg_tx_ring.virt, 0,
655 priv->msg_tx_ring.size * RIO_MSG_DESC_SIZE);
656 priv->msg_tx_ring.tx_slot = 0;
Matt Porter2b0c28d2005-11-07 01:00:19 -0800657
658 /* Point dequeue/enqueue pointers at first entry in ring */
Zhang Weiad1e9382008-04-18 13:33:41 -0700659 out_be32(&priv->msg_regs->odqdpar, priv->msg_tx_ring.phys);
660 out_be32(&priv->msg_regs->odqepar, priv->msg_tx_ring.phys);
Matt Porter2b0c28d2005-11-07 01:00:19 -0800661
662 /* Configure for snooping */
Zhang Weiad1e9382008-04-18 13:33:41 -0700663 out_be32(&priv->msg_regs->osar, 0x00000004);
Matt Porter2b0c28d2005-11-07 01:00:19 -0800664
665 /* Clear interrupt status */
Zhang Weiad1e9382008-04-18 13:33:41 -0700666 out_be32(&priv->msg_regs->osr, 0x000000b3);
Matt Porter2b0c28d2005-11-07 01:00:19 -0800667
668 /* Hook up outbound message handler */
Zhang Weiad1e9382008-04-18 13:33:41 -0700669 rc = request_irq(IRQ_RIO_TX(mport), fsl_rio_tx_handler, 0,
670 "msg_tx", (void *)mport);
671 if (rc < 0)
Matt Porter2b0c28d2005-11-07 01:00:19 -0800672 goto out_irq;
673
674 /*
675 * Configure outbound message unit
676 * Snooping
677 * Interrupts (all enabled, except QEIE)
678 * Chaining mode
679 * Disable
680 */
Zhang Weiad1e9382008-04-18 13:33:41 -0700681 out_be32(&priv->msg_regs->omr, 0x00100220);
Matt Porter2b0c28d2005-11-07 01:00:19 -0800682
683 /* Set number of entries */
Zhang Weiad1e9382008-04-18 13:33:41 -0700684 out_be32(&priv->msg_regs->omr,
685 in_be32(&priv->msg_regs->omr) |
Matt Porter2b0c28d2005-11-07 01:00:19 -0800686 ((get_bitmask_order(entries) - 2) << 12));
687
688 /* Now enable the unit */
Zhang Weiad1e9382008-04-18 13:33:41 -0700689 out_be32(&priv->msg_regs->omr, in_be32(&priv->msg_regs->omr) | 0x1);
Matt Porter2b0c28d2005-11-07 01:00:19 -0800690
691 out:
692 return rc;
693
694 out_irq:
Anton Vorontsov0dbbbf12009-04-18 21:48:52 +0400695 dma_free_coherent(priv->dev,
696 priv->msg_tx_ring.size * RIO_MSG_DESC_SIZE,
Zhang Weiad1e9382008-04-18 13:33:41 -0700697 priv->msg_tx_ring.virt, priv->msg_tx_ring.phys);
Matt Porter2b0c28d2005-11-07 01:00:19 -0800698
699 out_dma:
Zhang Weiad1e9382008-04-18 13:33:41 -0700700 for (i = 0; i < priv->msg_tx_ring.size; i++)
Anton Vorontsov0dbbbf12009-04-18 21:48:52 +0400701 dma_free_coherent(priv->dev, RIO_MSG_BUFFER_SIZE,
Zhang Weiad1e9382008-04-18 13:33:41 -0700702 priv->msg_tx_ring.virt_buffer[i],
703 priv->msg_tx_ring.phys_buffer[i]);
Matt Porter2b0c28d2005-11-07 01:00:19 -0800704
705 return rc;
706}
707
708/**
709 * rio_close_outb_mbox - Shut down MPC85xx outbound mailbox
710 * @mport: Master port implementing the outbound message unit
711 * @mbox: Mailbox to close
712 *
713 * Disables the outbound message unit, free all buffers, and
714 * frees the outbound message interrupt.
715 */
716void rio_close_outb_mbox(struct rio_mport *mport, int mbox)
717{
Zhang Weiad1e9382008-04-18 13:33:41 -0700718 struct rio_priv *priv = mport->priv;
Matt Porter2b0c28d2005-11-07 01:00:19 -0800719 /* Disable inbound message unit */
Zhang Weiad1e9382008-04-18 13:33:41 -0700720 out_be32(&priv->msg_regs->omr, 0);
Matt Porter2b0c28d2005-11-07 01:00:19 -0800721
722 /* Free ring */
Anton Vorontsov0dbbbf12009-04-18 21:48:52 +0400723 dma_free_coherent(priv->dev,
724 priv->msg_tx_ring.size * RIO_MSG_DESC_SIZE,
Zhang Weiad1e9382008-04-18 13:33:41 -0700725 priv->msg_tx_ring.virt, priv->msg_tx_ring.phys);
Matt Porter2b0c28d2005-11-07 01:00:19 -0800726
727 /* Free interrupt */
Zhang Weiad1e9382008-04-18 13:33:41 -0700728 free_irq(IRQ_RIO_TX(mport), (void *)mport);
Matt Porter2b0c28d2005-11-07 01:00:19 -0800729}
730
731/**
Zhang Weid02443a2008-04-18 13:33:38 -0700732 * fsl_rio_rx_handler - MPC85xx inbound message interrupt handler
Matt Porter2b0c28d2005-11-07 01:00:19 -0800733 * @irq: Linux interrupt number
734 * @dev_instance: Pointer to interrupt-specific data
Matt Porter2b0c28d2005-11-07 01:00:19 -0800735 *
736 * Handles inbound message interrupts. Executes a registered inbound
Simon Arlotta8de5ce2007-05-12 05:42:54 +1000737 * mailbox event handler and acks the interrupt occurrence.
Matt Porter2b0c28d2005-11-07 01:00:19 -0800738 */
739static irqreturn_t
Zhang Weid02443a2008-04-18 13:33:38 -0700740fsl_rio_rx_handler(int irq, void *dev_instance)
Matt Porter2b0c28d2005-11-07 01:00:19 -0800741{
742 int isr;
743 struct rio_mport *port = (struct rio_mport *)dev_instance;
Zhang Weiad1e9382008-04-18 13:33:41 -0700744 struct rio_priv *priv = port->priv;
Matt Porter2b0c28d2005-11-07 01:00:19 -0800745
Zhang Weiad1e9382008-04-18 13:33:41 -0700746 isr = in_be32(&priv->msg_regs->isr);
Matt Porter2b0c28d2005-11-07 01:00:19 -0800747
748 if (isr & RIO_MSG_ISR_TE) {
749 pr_info("RIO: inbound message reception error\n");
Zhang Weiad1e9382008-04-18 13:33:41 -0700750 out_be32((void *)&priv->msg_regs->isr, RIO_MSG_ISR_TE);
Matt Porter2b0c28d2005-11-07 01:00:19 -0800751 goto out;
752 }
753
754 /* XXX Need to check/dispatch until queue empty */
755 if (isr & RIO_MSG_ISR_DIQI) {
756 /*
757 * We implement *only* mailbox 0, but can receive messages
758 * for any mailbox/letter to that mailbox destination. So,
759 * make the callback with an unknown/invalid mailbox number
760 * argument.
761 */
Zhang Weiad1e9382008-04-18 13:33:41 -0700762 port->inb_msg[0].mcback(port, priv->msg_rx_ring.dev_id, -1, -1);
Matt Porter2b0c28d2005-11-07 01:00:19 -0800763
764 /* Ack the queueing interrupt */
Zhang Weiad1e9382008-04-18 13:33:41 -0700765 out_be32(&priv->msg_regs->isr, RIO_MSG_ISR_DIQI);
Matt Porter2b0c28d2005-11-07 01:00:19 -0800766 }
767
768 out:
769 return IRQ_HANDLED;
770}
771
772/**
773 * rio_open_inb_mbox - Initialize MPC85xx inbound mailbox
774 * @mport: Master port implementing the inbound message unit
Matt Porter6978bbc2005-11-07 01:00:20 -0800775 * @dev_id: Device specific pointer to pass on event
Matt Porter2b0c28d2005-11-07 01:00:19 -0800776 * @mbox: Mailbox to open
777 * @entries: Number of entries in the inbound mailbox ring
778 *
779 * Initializes buffer ring, request the inbound message interrupt,
780 * and enables the inbound message unit. Returns %0 on success
781 * and %-EINVAL or %-ENOMEM on failure.
782 */
Matt Porter6978bbc2005-11-07 01:00:20 -0800783int rio_open_inb_mbox(struct rio_mport *mport, void *dev_id, int mbox, int entries)
Matt Porter2b0c28d2005-11-07 01:00:19 -0800784{
785 int i, rc = 0;
Zhang Weiad1e9382008-04-18 13:33:41 -0700786 struct rio_priv *priv = mport->priv;
Matt Porter2b0c28d2005-11-07 01:00:19 -0800787
788 if ((entries < RIO_MIN_RX_RING_SIZE) ||
789 (entries > RIO_MAX_RX_RING_SIZE) || (!is_power_of_2(entries))) {
790 rc = -EINVAL;
791 goto out;
792 }
793
794 /* Initialize client buffer ring */
Zhang Weiad1e9382008-04-18 13:33:41 -0700795 priv->msg_rx_ring.dev_id = dev_id;
796 priv->msg_rx_ring.size = entries;
797 priv->msg_rx_ring.rx_slot = 0;
798 for (i = 0; i < priv->msg_rx_ring.size; i++)
799 priv->msg_rx_ring.virt_buffer[i] = NULL;
Matt Porter2b0c28d2005-11-07 01:00:19 -0800800
801 /* Initialize inbound message ring */
Anton Vorontsov0dbbbf12009-04-18 21:48:52 +0400802 priv->msg_rx_ring.virt = dma_alloc_coherent(priv->dev,
Zhang Weiad1e9382008-04-18 13:33:41 -0700803 priv->msg_rx_ring.size * RIO_MAX_MSG_SIZE,
804 &priv->msg_rx_ring.phys, GFP_KERNEL);
805 if (!priv->msg_rx_ring.virt) {
Matt Porter2b0c28d2005-11-07 01:00:19 -0800806 rc = -ENOMEM;
807 goto out;
808 }
809
810 /* Point dequeue/enqueue pointers at first entry in ring */
Zhang Weiad1e9382008-04-18 13:33:41 -0700811 out_be32(&priv->msg_regs->ifqdpar, (u32) priv->msg_rx_ring.phys);
812 out_be32(&priv->msg_regs->ifqepar, (u32) priv->msg_rx_ring.phys);
Matt Porter2b0c28d2005-11-07 01:00:19 -0800813
814 /* Clear interrupt status */
Zhang Weiad1e9382008-04-18 13:33:41 -0700815 out_be32(&priv->msg_regs->isr, 0x00000091);
Matt Porter2b0c28d2005-11-07 01:00:19 -0800816
817 /* Hook up inbound message handler */
Zhang Weiad1e9382008-04-18 13:33:41 -0700818 rc = request_irq(IRQ_RIO_RX(mport), fsl_rio_rx_handler, 0,
819 "msg_rx", (void *)mport);
820 if (rc < 0) {
Anton Vorontsov0dbbbf12009-04-18 21:48:52 +0400821 dma_free_coherent(priv->dev, RIO_MSG_BUFFER_SIZE,
Zhang Weiad1e9382008-04-18 13:33:41 -0700822 priv->msg_tx_ring.virt_buffer[i],
823 priv->msg_tx_ring.phys_buffer[i]);
Matt Porter2b0c28d2005-11-07 01:00:19 -0800824 goto out;
825 }
826
827 /*
828 * Configure inbound message unit:
829 * Snooping
830 * 4KB max message size
831 * Unmask all interrupt sources
832 * Disable
833 */
Zhang Weiad1e9382008-04-18 13:33:41 -0700834 out_be32(&priv->msg_regs->imr, 0x001b0060);
Matt Porter2b0c28d2005-11-07 01:00:19 -0800835
836 /* Set number of queue entries */
Zhang Weiad1e9382008-04-18 13:33:41 -0700837 setbits32(&priv->msg_regs->imr, (get_bitmask_order(entries) - 2) << 12);
Matt Porter2b0c28d2005-11-07 01:00:19 -0800838
839 /* Now enable the unit */
Zhang Weiad1e9382008-04-18 13:33:41 -0700840 setbits32(&priv->msg_regs->imr, 0x1);
Matt Porter2b0c28d2005-11-07 01:00:19 -0800841
842 out:
843 return rc;
844}
845
846/**
847 * rio_close_inb_mbox - Shut down MPC85xx inbound mailbox
848 * @mport: Master port implementing the inbound message unit
849 * @mbox: Mailbox to close
850 *
851 * Disables the inbound message unit, free all buffers, and
852 * frees the inbound message interrupt.
853 */
854void rio_close_inb_mbox(struct rio_mport *mport, int mbox)
855{
Zhang Weiad1e9382008-04-18 13:33:41 -0700856 struct rio_priv *priv = mport->priv;
Matt Porter2b0c28d2005-11-07 01:00:19 -0800857 /* Disable inbound message unit */
Zhang Weiad1e9382008-04-18 13:33:41 -0700858 out_be32(&priv->msg_regs->imr, 0);
Matt Porter2b0c28d2005-11-07 01:00:19 -0800859
860 /* Free ring */
Anton Vorontsov0dbbbf12009-04-18 21:48:52 +0400861 dma_free_coherent(priv->dev, priv->msg_rx_ring.size * RIO_MAX_MSG_SIZE,
Zhang Weiad1e9382008-04-18 13:33:41 -0700862 priv->msg_rx_ring.virt, priv->msg_rx_ring.phys);
Matt Porter2b0c28d2005-11-07 01:00:19 -0800863
864 /* Free interrupt */
Zhang Weiad1e9382008-04-18 13:33:41 -0700865 free_irq(IRQ_RIO_RX(mport), (void *)mport);
Matt Porter2b0c28d2005-11-07 01:00:19 -0800866}
867
868/**
869 * rio_hw_add_inb_buffer - Add buffer to the MPC85xx inbound message queue
870 * @mport: Master port implementing the inbound message unit
871 * @mbox: Inbound mailbox number
872 * @buf: Buffer to add to inbound queue
873 *
874 * Adds the @buf buffer to the MPC85xx inbound message queue. Returns
875 * %0 on success or %-EINVAL on failure.
876 */
877int rio_hw_add_inb_buffer(struct rio_mport *mport, int mbox, void *buf)
878{
879 int rc = 0;
Zhang Weiad1e9382008-04-18 13:33:41 -0700880 struct rio_priv *priv = mport->priv;
Matt Porter2b0c28d2005-11-07 01:00:19 -0800881
882 pr_debug("RIO: rio_hw_add_inb_buffer(), msg_rx_ring.rx_slot %d\n",
Zhang Weiad1e9382008-04-18 13:33:41 -0700883 priv->msg_rx_ring.rx_slot);
Matt Porter2b0c28d2005-11-07 01:00:19 -0800884
Zhang Weiad1e9382008-04-18 13:33:41 -0700885 if (priv->msg_rx_ring.virt_buffer[priv->msg_rx_ring.rx_slot]) {
Matt Porter2b0c28d2005-11-07 01:00:19 -0800886 printk(KERN_ERR
887 "RIO: error adding inbound buffer %d, buffer exists\n",
Zhang Weiad1e9382008-04-18 13:33:41 -0700888 priv->msg_rx_ring.rx_slot);
Matt Porter2b0c28d2005-11-07 01:00:19 -0800889 rc = -EINVAL;
890 goto out;
891 }
892
Zhang Weiad1e9382008-04-18 13:33:41 -0700893 priv->msg_rx_ring.virt_buffer[priv->msg_rx_ring.rx_slot] = buf;
894 if (++priv->msg_rx_ring.rx_slot == priv->msg_rx_ring.size)
895 priv->msg_rx_ring.rx_slot = 0;
Matt Porter2b0c28d2005-11-07 01:00:19 -0800896
897 out:
898 return rc;
899}
900
901EXPORT_SYMBOL_GPL(rio_hw_add_inb_buffer);
902
903/**
904 * rio_hw_get_inb_message - Fetch inbound message from the MPC85xx message unit
905 * @mport: Master port implementing the inbound message unit
906 * @mbox: Inbound mailbox number
907 *
908 * Gets the next available inbound message from the inbound message queue.
909 * A pointer to the message is returned on success or NULL on failure.
910 */
911void *rio_hw_get_inb_message(struct rio_mport *mport, int mbox)
912{
Zhang Weiad1e9382008-04-18 13:33:41 -0700913 struct rio_priv *priv = mport->priv;
Matt Porter2b0c28d2005-11-07 01:00:19 -0800914 u32 phys_buf, virt_buf;
915 void *buf = NULL;
916 int buf_idx;
917
Zhang Weiad1e9382008-04-18 13:33:41 -0700918 phys_buf = in_be32(&priv->msg_regs->ifqdpar);
Matt Porter2b0c28d2005-11-07 01:00:19 -0800919
920 /* If no more messages, then bail out */
Zhang Weiad1e9382008-04-18 13:33:41 -0700921 if (phys_buf == in_be32(&priv->msg_regs->ifqepar))
Matt Porter2b0c28d2005-11-07 01:00:19 -0800922 goto out2;
923
Zhang Weiad1e9382008-04-18 13:33:41 -0700924 virt_buf = (u32) priv->msg_rx_ring.virt + (phys_buf
925 - priv->msg_rx_ring.phys);
926 buf_idx = (phys_buf - priv->msg_rx_ring.phys) / RIO_MAX_MSG_SIZE;
927 buf = priv->msg_rx_ring.virt_buffer[buf_idx];
Matt Porter2b0c28d2005-11-07 01:00:19 -0800928
929 if (!buf) {
930 printk(KERN_ERR
931 "RIO: inbound message copy failed, no buffers\n");
932 goto out1;
933 }
934
935 /* Copy max message size, caller is expected to allocate that big */
936 memcpy(buf, (void *)virt_buf, RIO_MAX_MSG_SIZE);
937
938 /* Clear the available buffer */
Zhang Weiad1e9382008-04-18 13:33:41 -0700939 priv->msg_rx_ring.virt_buffer[buf_idx] = NULL;
Matt Porter2b0c28d2005-11-07 01:00:19 -0800940
941 out1:
Zhang Weiad1e9382008-04-18 13:33:41 -0700942 setbits32(&priv->msg_regs->imr, RIO_MSG_IMR_MI);
Matt Porter2b0c28d2005-11-07 01:00:19 -0800943
944 out2:
945 return buf;
946}
947
948EXPORT_SYMBOL_GPL(rio_hw_get_inb_message);
949
950/**
Zhang Weid02443a2008-04-18 13:33:38 -0700951 * fsl_rio_dbell_handler - MPC85xx doorbell interrupt handler
Matt Porter2b0c28d2005-11-07 01:00:19 -0800952 * @irq: Linux interrupt number
953 * @dev_instance: Pointer to interrupt-specific data
Matt Porter2b0c28d2005-11-07 01:00:19 -0800954 *
955 * Handles doorbell interrupts. Parses a list of registered
956 * doorbell event handlers and executes a matching event handler.
957 */
958static irqreturn_t
Zhang Weid02443a2008-04-18 13:33:38 -0700959fsl_rio_dbell_handler(int irq, void *dev_instance)
Matt Porter2b0c28d2005-11-07 01:00:19 -0800960{
961 int dsr;
962 struct rio_mport *port = (struct rio_mport *)dev_instance;
Zhang Weiad1e9382008-04-18 13:33:41 -0700963 struct rio_priv *priv = port->priv;
Matt Porter2b0c28d2005-11-07 01:00:19 -0800964
Zhang Weiad1e9382008-04-18 13:33:41 -0700965 dsr = in_be32(&priv->msg_regs->dsr);
Matt Porter2b0c28d2005-11-07 01:00:19 -0800966
967 if (dsr & DOORBELL_DSR_TE) {
968 pr_info("RIO: doorbell reception error\n");
Zhang Weiad1e9382008-04-18 13:33:41 -0700969 out_be32(&priv->msg_regs->dsr, DOORBELL_DSR_TE);
Matt Porter2b0c28d2005-11-07 01:00:19 -0800970 goto out;
971 }
972
973 if (dsr & DOORBELL_DSR_QFI) {
974 pr_info("RIO: doorbell queue full\n");
Zhang Weiad1e9382008-04-18 13:33:41 -0700975 out_be32(&priv->msg_regs->dsr, DOORBELL_DSR_QFI);
Matt Porter2b0c28d2005-11-07 01:00:19 -0800976 goto out;
977 }
978
979 /* XXX Need to check/dispatch until queue empty */
980 if (dsr & DOORBELL_DSR_DIQI) {
981 u32 dmsg =
Zhang Weiad1e9382008-04-18 13:33:41 -0700982 (u32) priv->dbell_ring.virt +
983 (in_be32(&priv->msg_regs->dqdpar) & 0xfff);
Matt Porter2b0c28d2005-11-07 01:00:19 -0800984 struct rio_dbell *dbell;
985 int found = 0;
986
987 pr_debug
988 ("RIO: processing doorbell, sid %2.2x tid %2.2x info %4.4x\n",
989 DBELL_SID(dmsg), DBELL_TID(dmsg), DBELL_INF(dmsg));
990
991 list_for_each_entry(dbell, &port->dbells, node) {
992 if ((dbell->res->start <= DBELL_INF(dmsg)) &&
993 (dbell->res->end >= DBELL_INF(dmsg))) {
994 found = 1;
995 break;
996 }
997 }
998 if (found) {
Matt Porter6978bbc2005-11-07 01:00:20 -0800999 dbell->dinb(port, dbell->dev_id, DBELL_SID(dmsg), DBELL_TID(dmsg),
Matt Porter2b0c28d2005-11-07 01:00:19 -08001000 DBELL_INF(dmsg));
1001 } else {
1002 pr_debug
1003 ("RIO: spurious doorbell, sid %2.2x tid %2.2x info %4.4x\n",
1004 DBELL_SID(dmsg), DBELL_TID(dmsg), DBELL_INF(dmsg));
1005 }
Zhang Weiad1e9382008-04-18 13:33:41 -07001006 setbits32(&priv->msg_regs->dmr, DOORBELL_DMR_DI);
1007 out_be32(&priv->msg_regs->dsr, DOORBELL_DSR_DIQI);
Matt Porter2b0c28d2005-11-07 01:00:19 -08001008 }
1009
1010 out:
1011 return IRQ_HANDLED;
1012}
1013
1014/**
Zhang Weid02443a2008-04-18 13:33:38 -07001015 * fsl_rio_doorbell_init - MPC85xx doorbell interface init
Matt Porter2b0c28d2005-11-07 01:00:19 -08001016 * @mport: Master port implementing the inbound doorbell unit
1017 *
1018 * Initializes doorbell unit hardware and inbound DMA buffer
Zhang Weid02443a2008-04-18 13:33:38 -07001019 * ring. Called from fsl_rio_setup(). Returns %0 on success
Matt Porter2b0c28d2005-11-07 01:00:19 -08001020 * or %-ENOMEM on failure.
1021 */
Zhang Weid02443a2008-04-18 13:33:38 -07001022static int fsl_rio_doorbell_init(struct rio_mport *mport)
Matt Porter2b0c28d2005-11-07 01:00:19 -08001023{
Zhang Weiad1e9382008-04-18 13:33:41 -07001024 struct rio_priv *priv = mport->priv;
Matt Porter2b0c28d2005-11-07 01:00:19 -08001025 int rc = 0;
1026
1027 /* Map outbound doorbell window immediately after maintenance window */
Zhang Weiad1e9382008-04-18 13:33:41 -07001028 priv->dbell_win = ioremap(mport->iores.start + RIO_MAINT_WIN_SIZE,
1029 RIO_DBELL_WIN_SIZE);
1030 if (!priv->dbell_win) {
Matt Porter2b0c28d2005-11-07 01:00:19 -08001031 printk(KERN_ERR
1032 "RIO: unable to map outbound doorbell window\n");
1033 rc = -ENOMEM;
1034 goto out;
1035 }
1036
1037 /* Initialize inbound doorbells */
Anton Vorontsov0dbbbf12009-04-18 21:48:52 +04001038 priv->dbell_ring.virt = dma_alloc_coherent(priv->dev, 512 *
Zhang Weiad1e9382008-04-18 13:33:41 -07001039 DOORBELL_MESSAGE_SIZE, &priv->dbell_ring.phys, GFP_KERNEL);
1040 if (!priv->dbell_ring.virt) {
Matt Porter2b0c28d2005-11-07 01:00:19 -08001041 printk(KERN_ERR "RIO: unable allocate inbound doorbell ring\n");
1042 rc = -ENOMEM;
Zhang Weiad1e9382008-04-18 13:33:41 -07001043 iounmap(priv->dbell_win);
Matt Porter2b0c28d2005-11-07 01:00:19 -08001044 goto out;
1045 }
1046
1047 /* Point dequeue/enqueue pointers at first entry in ring */
Zhang Weiad1e9382008-04-18 13:33:41 -07001048 out_be32(&priv->msg_regs->dqdpar, (u32) priv->dbell_ring.phys);
1049 out_be32(&priv->msg_regs->dqepar, (u32) priv->dbell_ring.phys);
Matt Porter2b0c28d2005-11-07 01:00:19 -08001050
1051 /* Clear interrupt status */
Zhang Weiad1e9382008-04-18 13:33:41 -07001052 out_be32(&priv->msg_regs->dsr, 0x00000091);
Matt Porter2b0c28d2005-11-07 01:00:19 -08001053
1054 /* Hook up doorbell handler */
Zhang Weiad1e9382008-04-18 13:33:41 -07001055 rc = request_irq(IRQ_RIO_BELL(mport), fsl_rio_dbell_handler, 0,
1056 "dbell_rx", (void *)mport);
1057 if (rc < 0) {
1058 iounmap(priv->dbell_win);
Anton Vorontsov0dbbbf12009-04-18 21:48:52 +04001059 dma_free_coherent(priv->dev, 512 * DOORBELL_MESSAGE_SIZE,
Zhang Weiad1e9382008-04-18 13:33:41 -07001060 priv->dbell_ring.virt, priv->dbell_ring.phys);
Matt Porter2b0c28d2005-11-07 01:00:19 -08001061 printk(KERN_ERR
1062 "MPC85xx RIO: unable to request inbound doorbell irq");
1063 goto out;
1064 }
1065
1066 /* Configure doorbells for snooping, 512 entries, and enable */
Zhang Weiad1e9382008-04-18 13:33:41 -07001067 out_be32(&priv->msg_regs->dmr, 0x00108161);
Matt Porter2b0c28d2005-11-07 01:00:19 -08001068
1069 out:
1070 return rc;
1071}
1072
Alexandre Bounine5b2074a2010-05-26 14:44:00 -07001073/**
1074 * fsl_rio_port_write_handler - MPC85xx port write interrupt handler
1075 * @irq: Linux interrupt number
1076 * @dev_instance: Pointer to interrupt-specific data
1077 *
1078 * Handles port write interrupts. Parses a list of registered
1079 * port write event handlers and executes a matching event handler.
1080 */
1081static irqreturn_t
1082fsl_rio_port_write_handler(int irq, void *dev_instance)
1083{
1084 u32 ipwmr, ipwsr;
1085 struct rio_mport *port = (struct rio_mport *)dev_instance;
1086 struct rio_priv *priv = port->priv;
1087 u32 epwisr, tmp;
1088
Alexandre Bounine93e2cbd2010-10-27 15:34:28 -07001089 epwisr = in_be32(priv->regs_win + RIO_EPWISR);
1090 if (!(epwisr & RIO_EPWISR_PW))
1091 goto pw_done;
1092
Alexandre Bounine5b2074a2010-05-26 14:44:00 -07001093 ipwmr = in_be32(&priv->msg_regs->pwmr);
1094 ipwsr = in_be32(&priv->msg_regs->pwsr);
1095
Alexandre Bounine5b2074a2010-05-26 14:44:00 -07001096#ifdef DEBUG_PW
1097 pr_debug("PW Int->IPWMR: 0x%08x IPWSR: 0x%08x (", ipwmr, ipwsr);
1098 if (ipwsr & RIO_IPWSR_QF)
1099 pr_debug(" QF");
1100 if (ipwsr & RIO_IPWSR_TE)
1101 pr_debug(" TE");
1102 if (ipwsr & RIO_IPWSR_QFI)
1103 pr_debug(" QFI");
1104 if (ipwsr & RIO_IPWSR_PWD)
1105 pr_debug(" PWD");
1106 if (ipwsr & RIO_IPWSR_PWB)
1107 pr_debug(" PWB");
1108 pr_debug(" )\n");
1109#endif
Alexandre Bounine5b2074a2010-05-26 14:44:00 -07001110 /* Schedule deferred processing if PW was received */
1111 if (ipwsr & RIO_IPWSR_QFI) {
1112 /* Save PW message (if there is room in FIFO),
1113 * otherwise discard it.
1114 */
1115 if (kfifo_avail(&priv->pw_fifo) >= RIO_PW_MSG_SIZE) {
1116 priv->port_write_msg.msg_count++;
1117 kfifo_in(&priv->pw_fifo, priv->port_write_msg.virt,
1118 RIO_PW_MSG_SIZE);
1119 } else {
1120 priv->port_write_msg.discard_count++;
Alexandre Bounine93e2cbd2010-10-27 15:34:28 -07001121 pr_debug("RIO: ISR Discarded Port-Write Msg(s) (%d)\n",
Alexandre Bounine5b2074a2010-05-26 14:44:00 -07001122 priv->port_write_msg.discard_count);
1123 }
Alexandre Bounine93e2cbd2010-10-27 15:34:28 -07001124 /* Clear interrupt and issue Clear Queue command. This allows
1125 * another port-write to be received.
1126 */
1127 out_be32(&priv->msg_regs->pwsr, RIO_IPWSR_QFI);
1128 out_be32(&priv->msg_regs->pwmr, ipwmr | RIO_IPWMR_CQ);
1129
Alexandre Bounine5b2074a2010-05-26 14:44:00 -07001130 schedule_work(&priv->pw_work);
1131 }
1132
Alexandre Bounine93e2cbd2010-10-27 15:34:28 -07001133 if ((ipwmr & RIO_IPWMR_EIE) && (ipwsr & RIO_IPWSR_TE)) {
1134 priv->port_write_msg.err_count++;
1135 pr_debug("RIO: Port-Write Transaction Err (%d)\n",
1136 priv->port_write_msg.err_count);
1137 /* Clear Transaction Error: port-write controller should be
1138 * disabled when clearing this error
1139 */
1140 out_be32(&priv->msg_regs->pwmr, ipwmr & ~RIO_IPWMR_PWE);
1141 out_be32(&priv->msg_regs->pwsr, RIO_IPWSR_TE);
1142 out_be32(&priv->msg_regs->pwmr, ipwmr);
1143 }
1144
1145 if (ipwsr & RIO_IPWSR_PWD) {
1146 priv->port_write_msg.discard_count++;
1147 pr_debug("RIO: Port Discarded Port-Write Msg(s) (%d)\n",
1148 priv->port_write_msg.discard_count);
1149 out_be32(&priv->msg_regs->pwsr, RIO_IPWSR_PWD);
1150 }
1151
1152pw_done:
1153 if (epwisr & RIO_EPWISR_PINT) {
1154 tmp = in_be32(priv->regs_win + RIO_LTLEDCSR);
1155 pr_debug("RIO_LTLEDCSR = 0x%x\n", tmp);
1156 out_be32(priv->regs_win + RIO_LTLEDCSR, 0);
1157 }
Alexandre Bounine5b2074a2010-05-26 14:44:00 -07001158
1159 return IRQ_HANDLED;
1160}
1161
1162static void fsl_pw_dpc(struct work_struct *work)
1163{
1164 struct rio_priv *priv = container_of(work, struct rio_priv, pw_work);
1165 unsigned long flags;
1166 u32 msg_buffer[RIO_PW_MSG_SIZE/sizeof(u32)];
1167
1168 /*
1169 * Process port-write messages
1170 */
1171 spin_lock_irqsave(&priv->pw_fifo_lock, flags);
1172 while (kfifo_out(&priv->pw_fifo, (unsigned char *)msg_buffer,
1173 RIO_PW_MSG_SIZE)) {
1174 /* Process one message */
1175 spin_unlock_irqrestore(&priv->pw_fifo_lock, flags);
1176#ifdef DEBUG_PW
1177 {
1178 u32 i;
1179 pr_debug("%s : Port-Write Message:", __func__);
1180 for (i = 0; i < RIO_PW_MSG_SIZE/sizeof(u32); i++) {
1181 if ((i%4) == 0)
1182 pr_debug("\n0x%02x: 0x%08x", i*4,
1183 msg_buffer[i]);
1184 else
1185 pr_debug(" 0x%08x", msg_buffer[i]);
1186 }
1187 pr_debug("\n");
1188 }
1189#endif
1190 /* Pass the port-write message to RIO core for processing */
1191 rio_inb_pwrite_handler((union rio_pw_msg *)msg_buffer);
1192 spin_lock_irqsave(&priv->pw_fifo_lock, flags);
1193 }
1194 spin_unlock_irqrestore(&priv->pw_fifo_lock, flags);
1195}
1196
1197/**
1198 * fsl_rio_pw_enable - enable/disable port-write interface init
1199 * @mport: Master port implementing the port write unit
1200 * @enable: 1=enable; 0=disable port-write message handling
1201 */
1202static int fsl_rio_pw_enable(struct rio_mport *mport, int enable)
1203{
1204 struct rio_priv *priv = mport->priv;
1205 u32 rval;
1206
1207 rval = in_be32(&priv->msg_regs->pwmr);
1208
1209 if (enable)
1210 rval |= RIO_IPWMR_PWE;
1211 else
1212 rval &= ~RIO_IPWMR_PWE;
1213
1214 out_be32(&priv->msg_regs->pwmr, rval);
1215
1216 return 0;
1217}
1218
1219/**
1220 * fsl_rio_port_write_init - MPC85xx port write interface init
1221 * @mport: Master port implementing the port write unit
1222 *
1223 * Initializes port write unit hardware and DMA buffer
1224 * ring. Called from fsl_rio_setup(). Returns %0 on success
1225 * or %-ENOMEM on failure.
1226 */
1227static int fsl_rio_port_write_init(struct rio_mport *mport)
1228{
1229 struct rio_priv *priv = mport->priv;
1230 int rc = 0;
1231
1232 /* Following configurations require a disabled port write controller */
1233 out_be32(&priv->msg_regs->pwmr,
1234 in_be32(&priv->msg_regs->pwmr) & ~RIO_IPWMR_PWE);
1235
1236 /* Initialize port write */
1237 priv->port_write_msg.virt = dma_alloc_coherent(priv->dev,
1238 RIO_PW_MSG_SIZE,
1239 &priv->port_write_msg.phys, GFP_KERNEL);
1240 if (!priv->port_write_msg.virt) {
1241 pr_err("RIO: unable allocate port write queue\n");
1242 return -ENOMEM;
1243 }
1244
1245 priv->port_write_msg.err_count = 0;
1246 priv->port_write_msg.discard_count = 0;
1247
1248 /* Point dequeue/enqueue pointers at first entry */
1249 out_be32(&priv->msg_regs->epwqbar, 0);
1250 out_be32(&priv->msg_regs->pwqbar, (u32) priv->port_write_msg.phys);
1251
1252 pr_debug("EIPWQBAR: 0x%08x IPWQBAR: 0x%08x\n",
1253 in_be32(&priv->msg_regs->epwqbar),
1254 in_be32(&priv->msg_regs->pwqbar));
1255
1256 /* Clear interrupt status IPWSR */
1257 out_be32(&priv->msg_regs->pwsr,
1258 (RIO_IPWSR_TE | RIO_IPWSR_QFI | RIO_IPWSR_PWD));
1259
1260 /* Configure port write contoller for snooping enable all reporting,
1261 clear queue full */
1262 out_be32(&priv->msg_regs->pwmr,
1263 RIO_IPWMR_SEN | RIO_IPWMR_QFIE | RIO_IPWMR_EIE | RIO_IPWMR_CQ);
1264
1265
1266 /* Hook up port-write handler */
1267 rc = request_irq(IRQ_RIO_PW(mport), fsl_rio_port_write_handler, 0,
1268 "port-write", (void *)mport);
1269 if (rc < 0) {
1270 pr_err("MPC85xx RIO: unable to request inbound doorbell irq");
1271 goto err_out;
1272 }
1273
1274 INIT_WORK(&priv->pw_work, fsl_pw_dpc);
1275 spin_lock_init(&priv->pw_fifo_lock);
1276 if (kfifo_alloc(&priv->pw_fifo, RIO_PW_MSG_SIZE * 32, GFP_KERNEL)) {
1277 pr_err("FIFO allocation failed\n");
1278 rc = -ENOMEM;
1279 goto err_out_irq;
1280 }
1281
1282 pr_debug("IPWMR: 0x%08x IPWSR: 0x%08x\n",
1283 in_be32(&priv->msg_regs->pwmr),
1284 in_be32(&priv->msg_regs->pwsr));
1285
1286 return rc;
1287
1288err_out_irq:
1289 free_irq(IRQ_RIO_PW(mport), (void *)mport);
1290err_out:
1291 dma_free_coherent(priv->dev, RIO_PW_MSG_SIZE,
1292 priv->port_write_msg.virt,
1293 priv->port_write_msg.phys);
1294 return rc;
1295}
1296
Matt Porter2b0c28d2005-11-07 01:00:19 -08001297static char *cmdline = NULL;
1298
Zhang Weid02443a2008-04-18 13:33:38 -07001299static int fsl_rio_get_hdid(int index)
Matt Porter2b0c28d2005-11-07 01:00:19 -08001300{
1301 /* XXX Need to parse multiple entries in some format */
1302 if (!cmdline)
1303 return -1;
1304
1305 return simple_strtol(cmdline, NULL, 0);
1306}
1307
Zhang Weid02443a2008-04-18 13:33:38 -07001308static int fsl_rio_get_cmdline(char *s)
Matt Porter2b0c28d2005-11-07 01:00:19 -08001309{
1310 if (!s)
1311 return 0;
1312
1313 cmdline = s;
1314 return 1;
1315}
1316
Zhang Weid02443a2008-04-18 13:33:38 -07001317__setup("riohdid=", fsl_rio_get_cmdline);
Matt Porter2b0c28d2005-11-07 01:00:19 -08001318
Zhang Wei7f620df2008-04-18 13:33:44 -07001319static inline void fsl_rio_info(struct device *dev, u32 ccsr)
1320{
1321 const char *str;
1322 if (ccsr & 1) {
1323 /* Serial phy */
1324 switch (ccsr >> 30) {
1325 case 0:
1326 str = "1";
1327 break;
1328 case 1:
1329 str = "4";
1330 break;
1331 default:
1332 str = "Unknown";
Joe Perchesd258e642009-06-28 06:26:10 +00001333 break;
Zhang Wei7f620df2008-04-18 13:33:44 -07001334 }
1335 dev_info(dev, "Hardware port width: %s\n", str);
1336
1337 switch ((ccsr >> 27) & 7) {
1338 case 0:
1339 str = "Single-lane 0";
1340 break;
1341 case 1:
1342 str = "Single-lane 2";
1343 break;
1344 case 2:
1345 str = "Four-lane";
1346 break;
1347 default:
1348 str = "Unknown";
1349 break;
1350 }
1351 dev_info(dev, "Training connection status: %s\n", str);
1352 } else {
1353 /* Parallel phy */
1354 if (!(ccsr & 0x80000000))
1355 dev_info(dev, "Output port operating in 8-bit mode\n");
1356 if (!(ccsr & 0x08000000))
1357 dev_info(dev, "Input port operating in 8-bit mode\n");
1358 }
1359}
1360
Matt Porter2b0c28d2005-11-07 01:00:19 -08001361/**
Randy Dunlap9941d942008-04-30 16:45:58 -07001362 * fsl_rio_setup - Setup Freescale PowerPC RapidIO interface
Grant Likely2dc11582010-08-06 09:25:50 -06001363 * @dev: platform_device pointer
Matt Porter2b0c28d2005-11-07 01:00:19 -08001364 *
1365 * Initializes MPC85xx RapidIO hardware interface, configures
1366 * master port with system-specific info, and registers the
1367 * master port with the RapidIO subsystem.
1368 */
Grant Likelya454dc52010-07-22 15:52:34 -06001369int fsl_rio_setup(struct platform_device *dev)
Matt Porter2b0c28d2005-11-07 01:00:19 -08001370{
1371 struct rio_ops *ops;
1372 struct rio_mport *port;
Zhang Weicc2bb692008-04-18 13:33:41 -07001373 struct rio_priv *priv;
1374 int rc = 0;
1375 const u32 *dt_range, *cell;
1376 struct resource regs;
1377 int rlen;
Zhang Wei61b26912008-04-18 13:33:44 -07001378 u32 ccsr;
Zhang Weicc2bb692008-04-18 13:33:41 -07001379 u64 law_start, law_size;
1380 int paw, aw, sw;
1381
Grant Likely61c7a082010-04-13 16:12:29 -07001382 if (!dev->dev.of_node) {
Zhang Weicc2bb692008-04-18 13:33:41 -07001383 dev_err(&dev->dev, "Device OF-Node is NULL");
1384 return -EFAULT;
1385 }
1386
Grant Likely61c7a082010-04-13 16:12:29 -07001387 rc = of_address_to_resource(dev->dev.of_node, 0, &regs);
Zhang Weicc2bb692008-04-18 13:33:41 -07001388 if (rc) {
1389 dev_err(&dev->dev, "Can't get %s property 'reg'\n",
Grant Likely61c7a082010-04-13 16:12:29 -07001390 dev->dev.of_node->full_name);
Zhang Weicc2bb692008-04-18 13:33:41 -07001391 return -EFAULT;
1392 }
Grant Likely61c7a082010-04-13 16:12:29 -07001393 dev_info(&dev->dev, "Of-device full name %s\n", dev->dev.of_node->full_name);
Kumar Galafc274a12009-05-13 17:02:24 -05001394 dev_info(&dev->dev, "Regs: %pR\n", &regs);
Zhang Weicc2bb692008-04-18 13:33:41 -07001395
Grant Likely61c7a082010-04-13 16:12:29 -07001396 dt_range = of_get_property(dev->dev.of_node, "ranges", &rlen);
Zhang Weicc2bb692008-04-18 13:33:41 -07001397 if (!dt_range) {
1398 dev_err(&dev->dev, "Can't get %s property 'ranges'\n",
Grant Likely61c7a082010-04-13 16:12:29 -07001399 dev->dev.of_node->full_name);
Zhang Weicc2bb692008-04-18 13:33:41 -07001400 return -EFAULT;
1401 }
1402
1403 /* Get node address wide */
Grant Likely61c7a082010-04-13 16:12:29 -07001404 cell = of_get_property(dev->dev.of_node, "#address-cells", NULL);
Zhang Weicc2bb692008-04-18 13:33:41 -07001405 if (cell)
1406 aw = *cell;
1407 else
Grant Likely61c7a082010-04-13 16:12:29 -07001408 aw = of_n_addr_cells(dev->dev.of_node);
Zhang Weicc2bb692008-04-18 13:33:41 -07001409 /* Get node size wide */
Grant Likely61c7a082010-04-13 16:12:29 -07001410 cell = of_get_property(dev->dev.of_node, "#size-cells", NULL);
Zhang Weicc2bb692008-04-18 13:33:41 -07001411 if (cell)
1412 sw = *cell;
1413 else
Grant Likely61c7a082010-04-13 16:12:29 -07001414 sw = of_n_size_cells(dev->dev.of_node);
Zhang Weicc2bb692008-04-18 13:33:41 -07001415 /* Get parent address wide wide */
Grant Likely61c7a082010-04-13 16:12:29 -07001416 paw = of_n_addr_cells(dev->dev.of_node);
Zhang Weicc2bb692008-04-18 13:33:41 -07001417
1418 law_start = of_read_number(dt_range + aw, paw);
1419 law_size = of_read_number(dt_range + aw + paw, sw);
1420
1421 dev_info(&dev->dev, "LAW start 0x%016llx, size 0x%016llx.\n",
1422 law_start, law_size);
Matt Porter2b0c28d2005-11-07 01:00:19 -08001423
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -07001424 ops = kzalloc(sizeof(struct rio_ops), GFP_KERNEL);
Julia Lawall6c759332009-08-07 09:00:34 +02001425 if (!ops) {
1426 rc = -ENOMEM;
1427 goto err_ops;
1428 }
Zhang Weid02443a2008-04-18 13:33:38 -07001429 ops->lcread = fsl_local_config_read;
1430 ops->lcwrite = fsl_local_config_write;
1431 ops->cread = fsl_rio_config_read;
1432 ops->cwrite = fsl_rio_config_write;
1433 ops->dsend = fsl_rio_doorbell_send;
Alexandre Bounine5b2074a2010-05-26 14:44:00 -07001434 ops->pwenable = fsl_rio_pw_enable;
Matt Porter2b0c28d2005-11-07 01:00:19 -08001435
Zhang Weiad1e9382008-04-18 13:33:41 -07001436 port = kzalloc(sizeof(struct rio_mport), GFP_KERNEL);
Julia Lawall6c759332009-08-07 09:00:34 +02001437 if (!port) {
1438 rc = -ENOMEM;
1439 goto err_port;
1440 }
Matt Porter2b0c28d2005-11-07 01:00:19 -08001441 port->id = 0;
1442 port->index = 0;
Zhang Weiad1e9382008-04-18 13:33:41 -07001443
1444 priv = kzalloc(sizeof(struct rio_priv), GFP_KERNEL);
1445 if (!priv) {
1446 printk(KERN_ERR "Can't alloc memory for 'priv'\n");
1447 rc = -ENOMEM;
Julia Lawall6c759332009-08-07 09:00:34 +02001448 goto err_priv;
Zhang Weiad1e9382008-04-18 13:33:41 -07001449 }
1450
Matt Porter2b0c28d2005-11-07 01:00:19 -08001451 INIT_LIST_HEAD(&port->dbells);
1452 port->iores.start = law_start;
Li Yang186e74b2009-05-12 16:35:59 +08001453 port->iores.end = law_start + law_size - 1;
Matt Porter2b0c28d2005-11-07 01:00:19 -08001454 port->iores.flags = IORESOURCE_MEM;
Li Yang186e74b2009-05-12 16:35:59 +08001455 port->iores.name = "rio_io_win";
Matt Porter2b0c28d2005-11-07 01:00:19 -08001456
Alexandre Bounine45fdf002010-05-28 13:56:17 -04001457 priv->pwirq = irq_of_parse_and_map(dev->dev.of_node, 0);
Grant Likely61c7a082010-04-13 16:12:29 -07001458 priv->bellirq = irq_of_parse_and_map(dev->dev.of_node, 2);
1459 priv->txirq = irq_of_parse_and_map(dev->dev.of_node, 3);
1460 priv->rxirq = irq_of_parse_and_map(dev->dev.of_node, 4);
Alexandre Bounine5b2074a2010-05-26 14:44:00 -07001461 dev_info(&dev->dev, "pwirq: %d, bellirq: %d, txirq: %d, rxirq %d\n",
1462 priv->pwirq, priv->bellirq, priv->txirq, priv->rxirq);
Zhang Weicc2bb692008-04-18 13:33:41 -07001463
Matt Porter2b0c28d2005-11-07 01:00:19 -08001464 rio_init_dbell_res(&port->riores[RIO_DOORBELL_RESOURCE], 0, 0xffff);
1465 rio_init_mbox_res(&port->riores[RIO_INB_MBOX_RESOURCE], 0, 0);
1466 rio_init_mbox_res(&port->riores[RIO_OUTB_MBOX_RESOURCE], 0, 0);
1467 strcpy(port->name, "RIO0 mport");
1468
Anton Vorontsov0dbbbf12009-04-18 21:48:52 +04001469 priv->dev = &dev->dev;
1470
Matt Porter2b0c28d2005-11-07 01:00:19 -08001471 port->ops = ops;
Zhang Weid02443a2008-04-18 13:33:38 -07001472 port->host_deviceid = fsl_rio_get_hdid(port->id);
Matt Porter2b0c28d2005-11-07 01:00:19 -08001473
Zhang Weiad1e9382008-04-18 13:33:41 -07001474 port->priv = priv;
Alexandre Bounineaf84ca32010-10-27 15:34:34 -07001475 port->phys_efptr = 0x100;
Matt Porter2b0c28d2005-11-07 01:00:19 -08001476 rio_register_mport(port);
1477
Zhang Weicc2bb692008-04-18 13:33:41 -07001478 priv->regs_win = ioremap(regs.start, regs.end - regs.start + 1);
Alexandre Bouninea52c8f52010-05-26 14:44:00 -07001479 rio_regs_win = priv->regs_win;
Zhang Weie0423232008-04-18 13:33:42 -07001480
Zhang Wei61b26912008-04-18 13:33:44 -07001481 /* Probe the master port phy type */
1482 ccsr = in_be32(priv->regs_win + RIO_CCSR);
1483 port->phy_type = (ccsr & 1) ? RIO_PHY_SERIAL : RIO_PHY_PARALLEL;
1484 dev_info(&dev->dev, "RapidIO PHY type: %s\n",
1485 (port->phy_type == RIO_PHY_PARALLEL) ? "parallel" :
1486 ((port->phy_type == RIO_PHY_SERIAL) ? "serial" :
1487 "unknown"));
Zhang Wei7f620df2008-04-18 13:33:44 -07001488 /* Checking the port training status */
1489 if (in_be32((priv->regs_win + RIO_ESCSR)) & 1) {
1490 dev_err(&dev->dev, "Port is not ready. "
1491 "Try to restart connection...\n");
1492 switch (port->phy_type) {
1493 case RIO_PHY_SERIAL:
1494 /* Disable ports */
1495 out_be32(priv->regs_win + RIO_CCSR, 0);
1496 /* Set 1x lane */
1497 setbits32(priv->regs_win + RIO_CCSR, 0x02000000);
1498 /* Enable ports */
1499 setbits32(priv->regs_win + RIO_CCSR, 0x00600000);
1500 break;
1501 case RIO_PHY_PARALLEL:
1502 /* Disable ports */
1503 out_be32(priv->regs_win + RIO_CCSR, 0x22000000);
1504 /* Enable ports */
1505 out_be32(priv->regs_win + RIO_CCSR, 0x44000000);
1506 break;
1507 }
1508 msleep(100);
1509 if (in_be32((priv->regs_win + RIO_ESCSR)) & 1) {
1510 dev_err(&dev->dev, "Port restart failed.\n");
1511 rc = -ENOLINK;
1512 goto err;
1513 }
1514 dev_info(&dev->dev, "Port restart success!\n");
1515 }
1516 fsl_rio_info(&dev->dev, ccsr);
Zhang Wei61b26912008-04-18 13:33:44 -07001517
Zhang Weie0423232008-04-18 13:33:42 -07001518 port->sys_size = (in_be32((priv->regs_win + RIO_PEF_CAR))
1519 & RIO_PEF_CTLS) >> 4;
1520 dev_info(&dev->dev, "RapidIO Common Transport System size: %d\n",
1521 port->sys_size ? 65536 : 256);
1522
Alexandre Bounineaf84ca32010-10-27 15:34:34 -07001523 if (port->host_deviceid >= 0)
1524 out_be32(priv->regs_win + RIO_GCCSR, RIO_PORT_GEN_HOST |
1525 RIO_PORT_GEN_MASTER | RIO_PORT_GEN_DISCOVERED);
1526 else
1527 out_be32(priv->regs_win + RIO_GCCSR, 0x00000000);
1528
Zhang Weiad1e9382008-04-18 13:33:41 -07001529 priv->atmu_regs = (struct rio_atmu_regs *)(priv->regs_win
1530 + RIO_ATMU_REGS_OFFSET);
1531 priv->maint_atmu_regs = priv->atmu_regs + 1;
1532 priv->dbell_atmu_regs = priv->atmu_regs + 2;
Zhang Wei61b26912008-04-18 13:33:44 -07001533 priv->msg_regs = (struct rio_msg_regs *)(priv->regs_win +
1534 ((port->phy_type == RIO_PHY_SERIAL) ?
1535 RIO_S_MSG_REGS_OFFSET : RIO_P_MSG_REGS_OFFSET));
1536
1537 /* Set to receive any dist ID for serial RapidIO controller. */
1538 if (port->phy_type == RIO_PHY_SERIAL)
1539 out_be32((priv->regs_win + RIO_ISR_AACR), RIO_ISR_AACR_AA);
Matt Porter2b0c28d2005-11-07 01:00:19 -08001540
1541 /* Configure maintenance transaction window */
Li Yang186e74b2009-05-12 16:35:59 +08001542 out_be32(&priv->maint_atmu_regs->rowbar, law_start >> 12);
Thomas Mollbd4fb652010-05-26 14:44:05 -07001543 out_be32(&priv->maint_atmu_regs->rowar,
1544 0x80077000 | (ilog2(RIO_MAINT_WIN_SIZE) - 1));
Matt Porter2b0c28d2005-11-07 01:00:19 -08001545
Zhang Weiad1e9382008-04-18 13:33:41 -07001546 priv->maint_win = ioremap(law_start, RIO_MAINT_WIN_SIZE);
Matt Porter2b0c28d2005-11-07 01:00:19 -08001547
1548 /* Configure outbound doorbell window */
Li Yang186e74b2009-05-12 16:35:59 +08001549 out_be32(&priv->dbell_atmu_regs->rowbar,
1550 (law_start + RIO_MAINT_WIN_SIZE) >> 12);
1551 out_be32(&priv->dbell_atmu_regs->rowar, 0x8004200b); /* 4k */
Zhang Weid02443a2008-04-18 13:33:38 -07001552 fsl_rio_doorbell_init(port);
Alexandre Bounine5b2074a2010-05-26 14:44:00 -07001553 fsl_rio_port_write_init(port);
Zhang Weiad1e9382008-04-18 13:33:41 -07001554
Li Yangff33f182010-06-18 14:24:20 +08001555#ifdef CONFIG_E500
Alexandre Bouninea52c8f52010-05-26 14:44:00 -07001556 saved_mcheck_exception = ppc_md.machine_check_exception;
1557 ppc_md.machine_check_exception = fsl_rio_mcheck_exception;
Li Yangff33f182010-06-18 14:24:20 +08001558#endif
Alexandre Bouninea52c8f52010-05-26 14:44:00 -07001559 /* Ensure that RFXE is set */
1560 mtspr(SPRN_HID1, (mfspr(SPRN_HID1) | 0x20000));
1561
Zhang Weicc2bb692008-04-18 13:33:41 -07001562 return 0;
Zhang Weiad1e9382008-04-18 13:33:41 -07001563err:
Julia Lawall6c759332009-08-07 09:00:34 +02001564 iounmap(priv->regs_win);
Zhang Weiad1e9382008-04-18 13:33:41 -07001565 kfree(priv);
Julia Lawall6c759332009-08-07 09:00:34 +02001566err_priv:
Zhang Weiad1e9382008-04-18 13:33:41 -07001567 kfree(port);
Julia Lawall6c759332009-08-07 09:00:34 +02001568err_port:
1569 kfree(ops);
1570err_ops:
Zhang Weicc2bb692008-04-18 13:33:41 -07001571 return rc;
Matt Porter2b0c28d2005-11-07 01:00:19 -08001572}
Zhang Weicc2bb692008-04-18 13:33:41 -07001573
1574/* The probe function for RapidIO peer-to-peer network.
1575 */
Grant Likelya454dc52010-07-22 15:52:34 -06001576static int __devinit fsl_of_rio_rpn_probe(struct platform_device *dev,
Zhang Weicc2bb692008-04-18 13:33:41 -07001577 const struct of_device_id *match)
1578{
1579 int rc;
1580 printk(KERN_INFO "Setting up RapidIO peer-to-peer network %s\n",
Grant Likely61c7a082010-04-13 16:12:29 -07001581 dev->dev.of_node->full_name);
Zhang Weicc2bb692008-04-18 13:33:41 -07001582
1583 rc = fsl_rio_setup(dev);
1584 if (rc)
1585 goto out;
1586
1587 /* Enumerate all registered ports */
1588 rc = rio_init_mports();
1589out:
1590 return rc;
1591};
1592
1593static const struct of_device_id fsl_of_rio_rpn_ids[] = {
1594 {
1595 .compatible = "fsl,rapidio-delta",
1596 },
1597 {},
1598};
1599
1600static struct of_platform_driver fsl_of_rio_rpn_driver = {
Grant Likely40182942010-04-13 16:13:02 -07001601 .driver = {
1602 .name = "fsl-of-rio",
1603 .owner = THIS_MODULE,
1604 .of_match_table = fsl_of_rio_rpn_ids,
1605 },
Zhang Weicc2bb692008-04-18 13:33:41 -07001606 .probe = fsl_of_rio_rpn_probe,
1607};
1608
1609static __init int fsl_of_rio_rpn_init(void)
1610{
1611 return of_register_platform_driver(&fsl_of_rio_rpn_driver);
1612}
1613
1614subsys_initcall(fsl_of_rio_rpn_init);