fsl_rio.c 20.5 KB
Newer Older
M
Matt Porter 已提交
1
/*
2
 * Freescale MPC85xx/MPC86xx RapidIO support
M
Matt Porter 已提交
3
 *
4 5 6 7
 * Copyright 2009 Sysgo AG
 * Thomas Moll <thomas.moll@sysgo.com>
 * - fixed maintenance access routines, check for aligned access
 *
8 9 10 11 12
 * Copyright 2009 Integrated Device Technology, Inc.
 * Alex Bounine <alexandre.bounine@idt.com>
 * - Added Port-Write message handling
 * - Added Machine Check exception handling
 *
13
 * Copyright (C) 2007, 2008, 2010, 2011 Freescale Semiconductor, Inc.
14 15
 * Zhang Wei <wei.zhang@freescale.com>
 *
M
Matt Porter 已提交
16 17 18 19 20 21 22 23 24 25
 * Copyright 2005 MontaVista Software, Inc.
 * Matt Porter <mporter@kernel.crashing.org>
 *
 * This program is free software; you can redistribute  it and/or modify it
 * under  the terms of  the GNU General  Public License as published by the
 * Free Software Foundation;  either version 2 of the  License, or (at your
 * option) any later version.
 */

#include <linux/init.h>
L
Liu Gang 已提交
26
#include <linux/module.h>
M
Matt Porter 已提交
27 28 29
#include <linux/types.h>
#include <linux/dma-mapping.h>
#include <linux/interrupt.h>
30
#include <linux/device.h>
R
Rob Herring 已提交
31 32
#include <linux/of_address.h>
#include <linux/of_irq.h>
33
#include <linux/of_platform.h>
34
#include <linux/delay.h>
35
#include <linux/slab.h>
M
Matt Porter 已提交
36

37 38
#include <linux/io.h>
#include <linux/uaccess.h>
39
#include <asm/machdep.h>
M
Matt Porter 已提交
40

41
#include "fsl_rio.h"
42

43
#undef DEBUG_PW	/* Port-Write debugging */
44 45 46 47 48 49

#define RIO_PORT1_EDCSR		0x0640
#define RIO_PORT2_EDCSR		0x0680
#define RIO_PORT1_IECSR		0x10130
#define RIO_PORT2_IECSR		0x101B0

50
#define RIO_GCCSR		0x13c
51
#define RIO_ESCSR		0x158
52
#define ESCSR_CLEAR		0x07120204
53
#define RIO_PORT2_ESCSR		0x178
54
#define RIO_CCSR		0x15c
55 56
#define RIO_LTLEDCSR_IER	0x80000000
#define RIO_LTLEDCSR_PRT	0x01000000
57
#define IECSR_CLEAR		0x80000000
58 59
#define RIO_ISR_AACR		0x10120
#define RIO_ISR_AACR_AA		0x1	/* Accept All ID */
M
Matt Porter 已提交
60

61 62 63 64 65 66 67 68 69 70 71 72 73
#define RIWTAR_TRAD_VAL_SHIFT	12
#define RIWTAR_TRAD_MASK	0x00FFFFFF
#define RIWBAR_BADD_VAL_SHIFT	12
#define RIWBAR_BADD_MASK	0x003FFFFF
#define RIWAR_ENABLE		0x80000000
#define RIWAR_TGINT_LOCAL	0x00F00000
#define RIWAR_RDTYP_NO_SNOOP	0x00040000
#define RIWAR_RDTYP_SNOOP	0x00050000
#define RIWAR_WRTYP_NO_SNOOP	0x00004000
#define RIWAR_WRTYP_SNOOP	0x00005000
#define RIWAR_WRTYP_ALLOC	0x00006000
#define RIWAR_SIZE_MASK		0x0000003F

74 75 76 77 78 79 80 81 82 83
#define __fsl_read_rio_config(x, addr, err, op)		\
	__asm__ __volatile__(				\
		"1:	"op" %1,0(%2)\n"		\
		"	eieio\n"			\
		"2:\n"					\
		".section .fixup,\"ax\"\n"		\
		"3:	li %1,-1\n"			\
		"	li %0,%3\n"			\
		"	b 2b\n"				\
		".section __ex_table,\"a\"\n"		\
84 85
			PPC_LONG_ALIGN "\n"		\
			PPC_LONG "1b,3b\n"		\
86 87 88 89
		".text"					\
		: "=r" (err), "=r" (x)			\
		: "b" (addr), "i" (-EFAULT), "0" (err))

90
void __iomem *rio_regs_win;
91 92 93 94 95
void __iomem *rmu_regs_win;
resource_size_t rio_law_start;

struct fsl_rio_dbell *dbell;
struct fsl_rio_pw *pw;
96

L
Li Yang 已提交
97
#ifdef CONFIG_E500
98
int fsl_rio_mcheck_exception(struct pt_regs *regs)
99
{
100 101
	const struct exception_table_entry *entry;
	unsigned long reason;
102

103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
	if (!rio_regs_win)
		return 0;

	reason = in_be32((u32 *)(rio_regs_win + RIO_LTLEDCSR));
	if (reason & (RIO_LTLEDCSR_IER | RIO_LTLEDCSR_PRT)) {
		/* Check if we are prepared to handle this fault */
		entry = search_exception_tables(regs->nip);
		if (entry) {
			pr_debug("RIO: %s - MC Exception handled\n",
				 __func__);
			out_be32((u32 *)(rio_regs_win + RIO_LTLEDCSR),
				 0);
			regs->msr |= MSR_RI;
			regs->nip = entry->fixup;
			return 1;
118 119 120
		}
	}

121
	return 0;
122
}
123
EXPORT_SYMBOL_GPL(fsl_rio_mcheck_exception);
L
Li Yang 已提交
124
#endif
125

M
Matt Porter 已提交
126
/**
127
 * fsl_local_config_read - Generate a MPC85xx local config space read
128
 * @mport: RapidIO master port info
M
Matt Porter 已提交
129 130 131 132 133 134 135 136
 * @index: ID of RapdiIO interface
 * @offset: Offset into configuration space
 * @len: Length (in bytes) of the maintenance transaction
 * @data: Value to be read into
 *
 * Generates a MPC85xx local configuration space read. Returns %0 on
 * success or %-EINVAL on failure.
 */
137 138
static int fsl_local_config_read(struct rio_mport *mport,
				int index, u32 offset, int len, u32 *data)
M
Matt Porter 已提交
139
{
140
	struct rio_priv *priv = mport->priv;
141
	pr_debug("fsl_local_config_read: index %d offset %8.8x\n", index,
142
		 offset);
143
	*data = in_be32(priv->regs_win + offset);
M
Matt Porter 已提交
144 145 146 147 148

	return 0;
}

/**
149
 * fsl_local_config_write - Generate a MPC85xx local config space write
150
 * @mport: RapidIO master port info
M
Matt Porter 已提交
151 152 153 154 155 156 157 158
 * @index: ID of RapdiIO interface
 * @offset: Offset into configuration space
 * @len: Length (in bytes) of the maintenance transaction
 * @data: Value to be written
 *
 * Generates a MPC85xx local configuration space write. Returns %0 on
 * success or %-EINVAL on failure.
 */
159 160
static int fsl_local_config_write(struct rio_mport *mport,
				int index, u32 offset, int len, u32 data)
M
Matt Porter 已提交
161
{
162
	struct rio_priv *priv = mport->priv;
M
Matt Porter 已提交
163
	pr_debug
164 165
		("fsl_local_config_write: index %d offset %8.8x data %8.8x\n",
		index, offset, data);
166
	out_be32(priv->regs_win + offset, data);
M
Matt Porter 已提交
167 168 169 170 171

	return 0;
}

/**
172
 * fsl_rio_config_read - Generate a MPC85xx read maintenance transaction
173
 * @mport: RapidIO master port info
M
Matt Porter 已提交
174 175 176 177 178 179 180 181 182 183 184
 * @index: ID of RapdiIO interface
 * @destid: Destination ID of transaction
 * @hopcount: Number of hops to target device
 * @offset: Offset into configuration space
 * @len: Length (in bytes) of the maintenance transaction
 * @val: Location to be read into
 *
 * Generates a MPC85xx read maintenance transaction. Returns %0 on
 * success or %-EINVAL on failure.
 */
static int
185 186
fsl_rio_config_read(struct rio_mport *mport, int index, u16 destid,
			u8 hopcount, u32 offset, int len, u32 *val)
M
Matt Porter 已提交
187
{
188
	struct rio_priv *priv = mport->priv;
M
Matt Porter 已提交
189
	u8 *data;
190
	u32 rval, err = 0;
M
Matt Porter 已提交
191 192

	pr_debug
193 194
		("fsl_rio_config_read:"
		" index %d destid %d hopcount %d offset %8.8x len %d\n",
195
		index, destid, hopcount, offset, len);
196 197 198 199 200 201

	/* 16MB maintenance window possible */
	/* allow only aligned access to maintenance registers */
	if (offset > (0x1000000 - len) || !IS_ALIGNED(offset, len))
		return -EINVAL;

202
	out_be32(&priv->maint_atmu_regs->rowtar,
203
		 (destid << 22) | (hopcount << 12) | (offset >> 12));
204
	out_be32(&priv->maint_atmu_regs->rowtear, (destid >> 10));
M
Matt Porter 已提交
205

206
	data = (u8 *) priv->maint_win + (offset & (RIO_MAINT_WIN_SIZE - 1));
M
Matt Porter 已提交
207 208
	switch (len) {
	case 1:
209
		__fsl_read_rio_config(rval, data, err, "lbz");
M
Matt Porter 已提交
210 211
		break;
	case 2:
212
		__fsl_read_rio_config(rval, data, err, "lhz");
M
Matt Porter 已提交
213
		break;
214
	case 4:
215
		__fsl_read_rio_config(rval, data, err, "lwz");
M
Matt Porter 已提交
216
		break;
217 218
	default:
		return -EINVAL;
M
Matt Porter 已提交
219 220
	}

221 222 223 224 225 226 227 228
	if (err) {
		pr_debug("RIO: cfg_read error %d for %x:%x:%x\n",
			 err, destid, hopcount, offset);
	}

	*val = rval;

	return err;
M
Matt Porter 已提交
229 230 231
}

/**
232
 * fsl_rio_config_write - Generate a MPC85xx write maintenance transaction
233
 * @mport: RapidIO master port info
M
Matt Porter 已提交
234 235 236 237 238 239 240 241 242 243 244
 * @index: ID of RapdiIO interface
 * @destid: Destination ID of transaction
 * @hopcount: Number of hops to target device
 * @offset: Offset into configuration space
 * @len: Length (in bytes) of the maintenance transaction
 * @val: Value to be written
 *
 * Generates an MPC85xx write maintenance transaction. Returns %0 on
 * success or %-EINVAL on failure.
 */
static int
245 246
fsl_rio_config_write(struct rio_mport *mport, int index, u16 destid,
			u8 hopcount, u32 offset, int len, u32 val)
M
Matt Porter 已提交
247
{
248
	struct rio_priv *priv = mport->priv;
M
Matt Porter 已提交
249 250
	u8 *data;
	pr_debug
251
		("fsl_rio_config_write:"
252 253
		" index %d destid %d hopcount %d offset %8.8x len %d val %8.8x\n",
		index, destid, hopcount, offset, len, val);
254 255 256 257 258 259

	/* 16MB maintenance windows possible */
	/* allow only aligned access to maintenance registers */
	if (offset > (0x1000000 - len) || !IS_ALIGNED(offset, len))
		return -EINVAL;

260
	out_be32(&priv->maint_atmu_regs->rowtar,
261
		 (destid << 22) | (hopcount << 12) | (offset >> 12));
262
	out_be32(&priv->maint_atmu_regs->rowtear, (destid >> 10));
M
Matt Porter 已提交
263

264
	data = (u8 *) priv->maint_win + (offset & (RIO_MAINT_WIN_SIZE - 1));
M
Matt Porter 已提交
265 266 267 268 269 270 271
	switch (len) {
	case 1:
		out_8((u8 *) data, val);
		break;
	case 2:
		out_be16((u16 *) data, val);
		break;
272
	case 4:
M
Matt Porter 已提交
273 274
		out_be32((u32 *) data, val);
		break;
275 276
	default:
		return -EINVAL;
M
Matt Porter 已提交
277 278 279 280 281
	}

	return 0;
}

282 283 284 285 286 287 288 289 290 291
static void fsl_rio_inbound_mem_init(struct rio_priv *priv)
{
	int i;

	/* close inbound windows */
	for (i = 0; i < RIO_INB_ATMU_COUNT; i++)
		out_be32(&priv->inb_atmu_regs[i].riwar, 0);
}

int fsl_map_inb_mem(struct rio_mport *mport, dma_addr_t lstart,
292
	u64 rstart, u64 size, u32 flags)
293 294 295 296 297 298 299 300
{
	struct rio_priv *priv = mport->priv;
	u32 base_size;
	unsigned int base_size_log;
	u64 win_start, win_end;
	u32 riwar;
	int i;

301
	if ((size & (size - 1)) != 0 || size > 0x400000000ULL)
302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364
		return -EINVAL;

	base_size_log = ilog2(size);
	base_size = 1 << base_size_log;

	/* check if addresses are aligned with the window size */
	if (lstart & (base_size - 1))
		return -EINVAL;
	if (rstart & (base_size - 1))
		return -EINVAL;

	/* check for conflicting ranges */
	for (i = 0; i < RIO_INB_ATMU_COUNT; i++) {
		riwar = in_be32(&priv->inb_atmu_regs[i].riwar);
		if ((riwar & RIWAR_ENABLE) == 0)
			continue;
		win_start = ((u64)(in_be32(&priv->inb_atmu_regs[i].riwbar) & RIWBAR_BADD_MASK))
			<< RIWBAR_BADD_VAL_SHIFT;
		win_end = win_start + ((1 << ((riwar & RIWAR_SIZE_MASK) + 1)) - 1);
		if (rstart < win_end && (rstart + size) > win_start)
			return -EINVAL;
	}

	/* find unused atmu */
	for (i = 0; i < RIO_INB_ATMU_COUNT; i++) {
		riwar = in_be32(&priv->inb_atmu_regs[i].riwar);
		if ((riwar & RIWAR_ENABLE) == 0)
			break;
	}
	if (i >= RIO_INB_ATMU_COUNT)
		return -ENOMEM;

	out_be32(&priv->inb_atmu_regs[i].riwtar, lstart >> RIWTAR_TRAD_VAL_SHIFT);
	out_be32(&priv->inb_atmu_regs[i].riwbar, rstart >> RIWBAR_BADD_VAL_SHIFT);
	out_be32(&priv->inb_atmu_regs[i].riwar, RIWAR_ENABLE | RIWAR_TGINT_LOCAL |
		RIWAR_RDTYP_SNOOP | RIWAR_WRTYP_SNOOP | (base_size_log - 1));

	return 0;
}

void fsl_unmap_inb_mem(struct rio_mport *mport, dma_addr_t lstart)
{
	u32 win_start_shift, base_start_shift;
	struct rio_priv *priv = mport->priv;
	u32 riwar, riwtar;
	int i;

	/* skip default window */
	base_start_shift = lstart >> RIWTAR_TRAD_VAL_SHIFT;
	for (i = 0; i < RIO_INB_ATMU_COUNT; i++) {
		riwar = in_be32(&priv->inb_atmu_regs[i].riwar);
		if ((riwar & RIWAR_ENABLE) == 0)
			continue;

		riwtar = in_be32(&priv->inb_atmu_regs[i].riwtar);
		win_start_shift = riwtar & RIWTAR_TRAD_MASK;
		if (win_start_shift == base_start_shift) {
			out_be32(&priv->inb_atmu_regs[i].riwar, riwar & ~RIWAR_ENABLE);
			return;
		}
	}
}

365
void fsl_rio_port_error_handler(int offset)
366 367 368 369 370 371
{
	/*XXX: Error recovery is not implemented, we just clear errors */
	out_be32((u32 *)(rio_regs_win + RIO_LTLEDCSR), 0);

	if (offset == 0) {
		out_be32((u32 *)(rio_regs_win + RIO_PORT1_EDCSR), 0);
372
		out_be32((u32 *)(rio_regs_win + RIO_PORT1_IECSR), IECSR_CLEAR);
373 374 375
		out_be32((u32 *)(rio_regs_win + RIO_ESCSR), ESCSR_CLEAR);
	} else {
		out_be32((u32 *)(rio_regs_win + RIO_PORT2_EDCSR), 0);
376
		out_be32((u32 *)(rio_regs_win + RIO_PORT2_IECSR), IECSR_CLEAR);
377 378 379
		out_be32((u32 *)(rio_regs_win + RIO_PORT2_ESCSR), ESCSR_CLEAR);
	}
}
380 381 382 383 384 385 386 387 388 389 390 391 392 393
static inline void fsl_rio_info(struct device *dev, u32 ccsr)
{
	const char *str;
	if (ccsr & 1) {
		/* Serial phy */
		switch (ccsr >> 30) {
		case 0:
			str = "1";
			break;
		case 1:
			str = "4";
			break;
		default:
			str = "Unknown";
394
			break;
395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421
		}
		dev_info(dev, "Hardware port width: %s\n", str);

		switch ((ccsr >> 27) & 7) {
		case 0:
			str = "Single-lane 0";
			break;
		case 1:
			str = "Single-lane 2";
			break;
		case 2:
			str = "Four-lane";
			break;
		default:
			str = "Unknown";
			break;
		}
		dev_info(dev, "Training connection status: %s\n", str);
	} else {
		/* Parallel phy */
		if (!(ccsr & 0x80000000))
			dev_info(dev, "Output port operating in 8-bit mode\n");
		if (!(ccsr & 0x08000000))
			dev_info(dev, "Input port operating in 8-bit mode\n");
	}
}

M
Matt Porter 已提交
422
/**
423
 * fsl_rio_setup - Setup Freescale PowerPC RapidIO interface
424
 * @dev: platform_device pointer
M
Matt Porter 已提交
425 426 427 428 429
 *
 * Initializes MPC85xx RapidIO hardware interface, configures
 * master port with system-specific info, and registers the
 * master port with the RapidIO subsystem.
 */
430
int fsl_rio_setup(struct platform_device *dev)
M
Matt Porter 已提交
431 432 433
{
	struct rio_ops *ops;
	struct rio_mport *port;
434 435
	struct rio_priv *priv;
	int rc = 0;
436 437 438 439
	const u32 *dt_range, *cell, *port_index;
	u32 active_ports = 0;
	struct resource regs, rmu_regs;
	struct device_node *np, *rmu_node;
440
	int rlen;
441
	u32 ccsr;
442
	u64 range_start, range_size;
443
	int paw, aw, sw;
444 445 446
	u32 i;
	static int tmp;
	struct device_node *rmu_np[MAX_MSG_UNIT_NUM] = {NULL};
447

448
	if (!dev->dev.of_node) {
449
		dev_err(&dev->dev, "Device OF-Node is NULL");
450
		return -ENODEV;
451 452
	}

453
	rc = of_address_to_resource(dev->dev.of_node, 0, &regs);
454 455
	if (rc) {
		dev_err(&dev->dev, "Can't get %s property 'reg'\n",
456
				dev->dev.of_node->full_name);
457 458
		return -EFAULT;
	}
459 460
	dev_info(&dev->dev, "Of-device full name %s\n",
			dev->dev.of_node->full_name);
461
	dev_info(&dev->dev, "Regs: %pR\n", &regs);
462

463 464 465 466 467
	rio_regs_win = ioremap(regs.start, resource_size(&regs));
	if (!rio_regs_win) {
		dev_err(&dev->dev, "Unable to map rio register window\n");
		rc = -ENOMEM;
		goto err_rio_regs;
468 469
	}

470
	ops = kzalloc(sizeof(struct rio_ops), GFP_KERNEL);
471 472 473 474
	if (!ops) {
		rc = -ENOMEM;
		goto err_ops;
	}
475 476 477 478
	ops->lcread = fsl_local_config_read;
	ops->lcwrite = fsl_local_config_write;
	ops->cread = fsl_rio_config_read;
	ops->cwrite = fsl_rio_config_write;
479
	ops->dsend = fsl_rio_doorbell_send;
480
	ops->pwenable = fsl_rio_pw_enable;
481 482 483 484 485 486 487
	ops->open_outb_mbox = fsl_open_outb_mbox;
	ops->open_inb_mbox = fsl_open_inb_mbox;
	ops->close_outb_mbox = fsl_close_outb_mbox;
	ops->close_inb_mbox = fsl_close_inb_mbox;
	ops->add_outb_message = fsl_add_outb_message;
	ops->add_inb_buffer = fsl_add_inb_buffer;
	ops->get_inb_message = fsl_get_inb_message;
488 489
	ops->map_inb = fsl_map_inb_mem;
	ops->unmap_inb = fsl_unmap_inb_mem;
490 491

	rmu_node = of_parse_phandle(dev->dev.of_node, "fsl,srio-rmu-handle", 0);
492 493
	if (!rmu_node) {
		dev_err(&dev->dev, "No valid fsl,srio-rmu-handle property\n");
494
		rc = -ENOENT;
495
		goto err_rmu;
496
	}
497 498 499 500 501 502 503 504 505
	rc = of_address_to_resource(rmu_node, 0, &rmu_regs);
	if (rc) {
		dev_err(&dev->dev, "Can't get %s property 'reg'\n",
				rmu_node->full_name);
		goto err_rmu;
	}
	rmu_regs_win = ioremap(rmu_regs.start, resource_size(&rmu_regs));
	if (!rmu_regs_win) {
		dev_err(&dev->dev, "Unable to map rmu register window\n");
506
		rc = -ENOMEM;
507 508 509 510 511
		goto err_rmu;
	}
	for_each_compatible_node(np, NULL, "fsl,srio-msg-unit") {
		rmu_np[tmp] = np;
		tmp++;
512
	}
513

514 515 516
	/*set up doobell node*/
	np = of_find_compatible_node(NULL, NULL, "fsl,srio-dbell-unit");
	if (!np) {
517
		dev_err(&dev->dev, "No fsl,srio-dbell-unit node\n");
518 519 520 521 522 523
		rc = -ENODEV;
		goto err_dbell;
	}
	dbell = kzalloc(sizeof(struct fsl_rio_dbell), GFP_KERNEL);
	if (!(dbell)) {
		dev_err(&dev->dev, "Can't alloc memory for 'fsl_rio_dbell'\n");
524
		rc = -ENOMEM;
525
		goto err_dbell;
526
	}
527 528 529
	dbell->dev = &dev->dev;
	dbell->bellirq = irq_of_parse_and_map(np, 1);
	dev_info(&dev->dev, "bellirq: %d\n", dbell->bellirq);
530

531 532 533 534 535 536 537 538 539 540 541 542 543 544 545
	aw = of_n_addr_cells(np);
	dt_range = of_get_property(np, "reg", &rlen);
	if (!dt_range) {
		pr_err("%s: unable to find 'reg' property\n",
			np->full_name);
		rc = -ENOMEM;
		goto err_pw;
	}
	range_start = of_read_number(dt_range, aw);
	dbell->dbell_regs = (struct rio_dbell_regs *)(rmu_regs_win +
				(u32)range_start);

	/*set up port write node*/
	np = of_find_compatible_node(NULL, NULL, "fsl,srio-port-write-unit");
	if (!np) {
546
		dev_err(&dev->dev, "No fsl,srio-port-write-unit node\n");
547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565
		rc = -ENODEV;
		goto err_pw;
	}
	pw = kzalloc(sizeof(struct fsl_rio_pw), GFP_KERNEL);
	if (!(pw)) {
		dev_err(&dev->dev, "Can't alloc memory for 'fsl_rio_pw'\n");
		rc = -ENOMEM;
		goto err_pw;
	}
	pw->dev = &dev->dev;
	pw->pwirq = irq_of_parse_and_map(np, 0);
	dev_info(&dev->dev, "pwirq: %d\n", pw->pwirq);
	aw = of_n_addr_cells(np);
	dt_range = of_get_property(np, "reg", &rlen);
	if (!dt_range) {
		pr_err("%s: unable to find 'reg' property\n",
			np->full_name);
		rc = -ENOMEM;
		goto err;
566
	}
567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584
	range_start = of_read_number(dt_range, aw);
	pw->pw_regs = (struct rio_pw_regs *)(rmu_regs_win + (u32)range_start);

	/*set up ports node*/
	for_each_child_of_node(dev->dev.of_node, np) {
		port_index = of_get_property(np, "cell-index", NULL);
		if (!port_index) {
			dev_err(&dev->dev, "Can't get %s property 'cell-index'\n",
					np->full_name);
			continue;
		}

		dt_range = of_get_property(np, "ranges", &rlen);
		if (!dt_range) {
			dev_err(&dev->dev, "Can't get %s property 'ranges'\n",
					np->full_name);
			continue;
		}
585

586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609
		/* Get node address wide */
		cell = of_get_property(np, "#address-cells", NULL);
		if (cell)
			aw = *cell;
		else
			aw = of_n_addr_cells(np);
		/* Get node size wide */
		cell = of_get_property(np, "#size-cells", NULL);
		if (cell)
			sw = *cell;
		else
			sw = of_n_size_cells(np);
		/* Get parent address wide wide */
		paw = of_n_addr_cells(np);
		range_start = of_read_number(dt_range + aw, paw);
		range_size = of_read_number(dt_range + aw + paw, sw);

		dev_info(&dev->dev, "%s: LAW start 0x%016llx, size 0x%016llx.\n",
				np->full_name, range_start, range_size);

		port = kzalloc(sizeof(struct rio_mport), GFP_KERNEL);
		if (!port)
			continue;

610 611 612 613 614 615
		rc = rio_mport_initialize(port);
		if (rc) {
			kfree(port);
			continue;
		}

616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642
		i = *port_index - 1;
		port->index = (unsigned char)i;

		priv = kzalloc(sizeof(struct rio_priv), GFP_KERNEL);
		if (!priv) {
			dev_err(&dev->dev, "Can't alloc memory for 'priv'\n");
			kfree(port);
			continue;
		}

		INIT_LIST_HEAD(&port->dbells);
		port->iores.start = range_start;
		port->iores.end = port->iores.start + range_size - 1;
		port->iores.flags = IORESOURCE_MEM;
		port->iores.name = "rio_io_win";

		if (request_resource(&iomem_resource, &port->iores) < 0) {
			dev_err(&dev->dev, "RIO: Error requesting master port region"
				" 0x%016llx-0x%016llx\n",
				(u64)port->iores.start, (u64)port->iores.end);
				kfree(priv);
				kfree(port);
				continue;
		}
		sprintf(port->name, "RIO mport %d", i);

		priv->dev = &dev->dev;
643
		port->dev.parent = &dev->dev;
644 645 646
		port->ops = ops;
		port->priv = priv;
		port->phys_efptr = 0x100;
647
		port->phys_rmap = 1;
648 649 650
		priv->regs_win = rio_regs_win;

		ccsr = in_be32(priv->regs_win + RIO_CCSR + i*0x20);
651

652 653 654 655
		/* Checking the port training status */
		if (in_be32((priv->regs_win + RIO_ESCSR + i*0x20)) & 1) {
			dev_err(&dev->dev, "Port %d is not ready. "
			"Try to restart connection...\n", i);
656
			/* Disable ports */
657 658
			out_be32(priv->regs_win
				+ RIO_CCSR + i*0x20, 0);
659
			/* Set 1x lane */
660 661
			setbits32(priv->regs_win
				+ RIO_CCSR + i*0x20, 0x02000000);
662
			/* Enable ports */
663 664 665 666 667 668 669 670 671 672 673 674 675
			setbits32(priv->regs_win
				+ RIO_CCSR + i*0x20, 0x00600000);
			msleep(100);
			if (in_be32((priv->regs_win
					+ RIO_ESCSR + i*0x20)) & 1) {
				dev_err(&dev->dev,
					"Port %d restart failed.\n", i);
				release_resource(&port->iores);
				kfree(priv);
				kfree(port);
				continue;
			}
			dev_info(&dev->dev, "Port %d restart success!\n", i);
676
		}
677
		fsl_rio_info(&dev->dev, ccsr);
678

679
		port->sys_size = (in_be32((priv->regs_win + RIO_PEF_CAR))
680
					& RIO_PEF_CTLS) >> 4;
681 682 683 684 685 686 687 688 689
		dev_info(&dev->dev, "RapidIO Common Transport System size: %d\n",
				port->sys_size ? 65536 : 256);

		if (port->host_deviceid >= 0)
			out_be32(priv->regs_win + RIO_GCCSR, RIO_PORT_GEN_HOST |
				RIO_PORT_GEN_MASTER | RIO_PORT_GEN_DISCOVERED);
		else
			out_be32(priv->regs_win + RIO_GCCSR,
				RIO_PORT_GEN_MASTER);
690

691 692 693 694 695
		priv->atmu_regs = (struct rio_atmu_regs *)(priv->regs_win
			+ ((i == 0) ? RIO_ATMU_REGS_PORT1_OFFSET :
			RIO_ATMU_REGS_PORT2_OFFSET));

		priv->maint_atmu_regs = priv->atmu_regs + 1;
696 697 698 699 700
		priv->inb_atmu_regs = (struct rio_inb_atmu_regs __iomem *)
			(priv->regs_win +
			((i == 0) ? RIO_INB_ATMU_REGS_PORT1_OFFSET :
			RIO_INB_ATMU_REGS_PORT2_OFFSET));

701 702 703
		/* Set to receive packets with any dest ID */
		out_be32((priv->regs_win + RIO_ISR_AACR + i*0x80),
			 RIO_ISR_AACR_AA);
704

705 706 707 708 709
		/* Configure maintenance transaction window */
		out_be32(&priv->maint_atmu_regs->rowbar,
			port->iores.start >> 12);
		out_be32(&priv->maint_atmu_regs->rowar,
			 0x80077000 | (ilog2(RIO_MAINT_WIN_SIZE) - 1));
710

711 712
		priv->maint_win = ioremap(port->iores.start,
				RIO_MAINT_WIN_SIZE);
M
Matt Porter 已提交
713

714
		rio_law_start = range_start;
M
Matt Porter 已提交
715

716
		fsl_rio_setup_rmu(port, rmu_np[i]);
717
		fsl_rio_inbound_mem_init(priv);
M
Matt Porter 已提交
718

719
		dbell->mport[i] = port;
720
		pw->mport[i] = port;
721

722 723 724 725 726 727
		if (rio_register_mport(port)) {
			release_resource(&port->iores);
			kfree(priv);
			kfree(port);
			continue;
		}
728 729 730 731 732 733 734
		active_ports++;
	}

	if (!active_ports) {
		rc = -ENOLINK;
		goto err;
	}
735

736 737
	fsl_rio_doorbell_init(dbell);
	fsl_rio_port_write_init(pw);
738

739
	return 0;
740
err:
741
	kfree(pw);
742
	pw = NULL;
743 744
err_pw:
	kfree(dbell);
745
	dbell = NULL;
746 747
err_dbell:
	iounmap(rmu_regs_win);
748
	rmu_regs_win = NULL;
749
err_rmu:
750 751
	kfree(ops);
err_ops:
752
	iounmap(rio_regs_win);
753
	rio_regs_win = NULL;
754
err_rio_regs:
755
	return rc;
M
Matt Porter 已提交
756
}
757 758 759

/* The probe function for RapidIO peer-to-peer network.
 */
760
static int fsl_of_rio_rpn_probe(struct platform_device *dev)
761 762
{
	printk(KERN_INFO "Setting up RapidIO peer-to-peer network %s\n",
763
			dev->dev.of_node->full_name);
764

765
	return fsl_rio_setup(dev);
766 767 768 769
};

static const struct of_device_id fsl_of_rio_rpn_ids[] = {
	{
770
		.compatible = "fsl,srio",
771 772 773 774
	},
	{},
};

775
static struct platform_driver fsl_of_rio_rpn_driver = {
776 777 778 779
	.driver = {
		.name = "fsl-of-rio",
		.of_match_table = fsl_of_rio_rpn_ids,
	},
780 781 782 783 784
	.probe = fsl_of_rio_rpn_probe,
};

static __init int fsl_of_rio_rpn_init(void)
{
785
	return platform_driver_register(&fsl_of_rio_rpn_driver);
786 787 788
}

subsys_initcall(fsl_of_rio_rpn_init);