fsl_rio.c 14.4 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>
31
#include <linux/of_platform.h>
32
#include <linux/delay.h>
33
#include <linux/slab.h>
M
Matt Porter 已提交
34

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

39
#include "fsl_rio.h"
40

41
#undef DEBUG_PW	/* Port-Write debugging */
42 43 44 45 46 47

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

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

60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
#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"		\
		"	.align 2\n"			\
		"	.long 1b,3b\n"			\
		".text"					\
		: "=r" (err), "=r" (x)			\
		: "b" (addr), "i" (-EFAULT), "0" (err))

76
void __iomem *rio_regs_win;
77

L
Li Yang 已提交
78
#ifdef CONFIG_E500
79
int fsl_rio_mcheck_exception(struct pt_regs *regs)
80
{
81 82
	const struct exception_table_entry *entry;
	unsigned long reason;
83

84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
	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;
99 100 101
		}
	}

102
	return 0;
103
}
104
EXPORT_SYMBOL_GPL(fsl_rio_mcheck_exception);
L
Li Yang 已提交
105
#endif
106

M
Matt Porter 已提交
107
/**
108
 * fsl_local_config_read - Generate a MPC85xx local config space read
109
 * @mport: RapidIO master port info
M
Matt Porter 已提交
110 111 112 113 114 115 116 117
 * @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.
 */
118 119
static int fsl_local_config_read(struct rio_mport *mport,
				int index, u32 offset, int len, u32 *data)
M
Matt Porter 已提交
120
{
121
	struct rio_priv *priv = mport->priv;
122
	pr_debug("fsl_local_config_read: index %d offset %8.8x\n", index,
123
		offset);
124
	*data = in_be32(priv->regs_win + offset);
M
Matt Porter 已提交
125 126 127 128 129

	return 0;
}

/**
130
 * fsl_local_config_write - Generate a MPC85xx local config space write
131
 * @mport: RapidIO master port info
M
Matt Porter 已提交
132 133 134 135 136 137 138 139
 * @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.
 */
140 141
static int fsl_local_config_write(struct rio_mport *mport,
				int index, u32 offset, int len, u32 data)
M
Matt Porter 已提交
142
{
143
	struct rio_priv *priv = mport->priv;
M
Matt Porter 已提交
144
	pr_debug
145 146
		("fsl_local_config_write: index %d offset %8.8x data %8.8x\n",
		index, offset, data);
147
	out_be32(priv->regs_win + offset, data);
M
Matt Porter 已提交
148 149 150 151 152

	return 0;
}

/**
153
 * fsl_rio_config_read - Generate a MPC85xx read maintenance transaction
154
 * @mport: RapidIO master port info
M
Matt Porter 已提交
155 156 157 158 159 160 161 162 163 164 165
 * @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
166 167
fsl_rio_config_read(struct rio_mport *mport, int index, u16 destid,
			u8 hopcount, u32 offset, int len, u32 *val)
M
Matt Porter 已提交
168
{
169
	struct rio_priv *priv = mport->priv;
M
Matt Porter 已提交
170
	u8 *data;
171
	u32 rval, err = 0;
M
Matt Porter 已提交
172 173

	pr_debug
174 175
		("fsl_rio_config_read:"
		" index %d destid %d hopcount %d offset %8.8x len %d\n",
M
Matt Porter 已提交
176
	     index, destid, hopcount, offset, len);
177 178 179 180 181 182

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

183
	out_be32(&priv->maint_atmu_regs->rowtar,
184
		 (destid << 22) | (hopcount << 12) | (offset >> 12));
185
	out_be32(&priv->maint_atmu_regs->rowtear, (destid >> 10));
M
Matt Porter 已提交
186

187
	data = (u8 *) priv->maint_win + (offset & (RIO_MAINT_WIN_SIZE - 1));
M
Matt Porter 已提交
188 189
	switch (len) {
	case 1:
190
		__fsl_read_rio_config(rval, data, err, "lbz");
M
Matt Porter 已提交
191 192
		break;
	case 2:
193
		__fsl_read_rio_config(rval, data, err, "lhz");
M
Matt Porter 已提交
194
		break;
195
	case 4:
196
		__fsl_read_rio_config(rval, data, err, "lwz");
M
Matt Porter 已提交
197
		break;
198 199
	default:
		return -EINVAL;
M
Matt Porter 已提交
200 201
	}

202 203 204 205 206 207 208 209
	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 已提交
210 211 212
}

/**
213
 * fsl_rio_config_write - Generate a MPC85xx write maintenance transaction
214
 * @mport: RapidIO master port info
M
Matt Porter 已提交
215 216 217 218 219 220 221 222 223 224 225
 * @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
226 227
fsl_rio_config_write(struct rio_mport *mport, int index, u16 destid,
			u8 hopcount, u32 offset, int len, u32 val)
M
Matt Porter 已提交
228
{
229
	struct rio_priv *priv = mport->priv;
M
Matt Porter 已提交
230 231
	u8 *data;
	pr_debug
232 233
		("fsl_rio_config_write:"
		"index %d destid %d hopcount %d offset %8.8x len %d val %8.8x\n",
M
Matt Porter 已提交
234
	     index, destid, hopcount, offset, len, val);
235 236 237 238 239 240

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

241
	out_be32(&priv->maint_atmu_regs->rowtar,
242
		 (destid << 22) | (hopcount << 12) | (offset >> 12));
243
	out_be32(&priv->maint_atmu_regs->rowtear, (destid >> 10));
M
Matt Porter 已提交
244

245
	data = (u8 *) priv->maint_win + (offset & (RIO_MAINT_WIN_SIZE - 1));
M
Matt Porter 已提交
246 247 248 249 250 251 252
	switch (len) {
	case 1:
		out_8((u8 *) data, val);
		break;
	case 2:
		out_be16((u16 *) data, val);
		break;
253
	case 4:
M
Matt Porter 已提交
254 255
		out_be32((u32 *) data, val);
		break;
256 257
	default:
		return -EINVAL;
M
Matt Porter 已提交
258 259 260 261 262
	}

	return 0;
}

263
void fsl_rio_port_error_handler(struct rio_mport *port, int offset)
264 265 266 267 268 269
{
	/*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);
270
		out_be32((u32 *)(rio_regs_win + RIO_PORT1_IECSR), IECSR_CLEAR);
271 272 273
		out_be32((u32 *)(rio_regs_win + RIO_ESCSR), ESCSR_CLEAR);
	} else {
		out_be32((u32 *)(rio_regs_win + RIO_PORT2_EDCSR), 0);
274
		out_be32((u32 *)(rio_regs_win + RIO_PORT2_IECSR), IECSR_CLEAR);
275 276 277
		out_be32((u32 *)(rio_regs_win + RIO_PORT2_ESCSR), ESCSR_CLEAR);
	}
}
278 279 280 281 282 283 284 285 286 287 288 289 290 291
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";
292
			break;
293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319
		}
		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 已提交
320
/**
321
 * fsl_rio_setup - Setup Freescale PowerPC RapidIO interface
322
 * @dev: platform_device pointer
M
Matt Porter 已提交
323 324 325 326 327
 *
 * Initializes MPC85xx RapidIO hardware interface, configures
 * master port with system-specific info, and registers the
 * master port with the RapidIO subsystem.
 */
328
int fsl_rio_setup(struct platform_device *dev)
M
Matt Porter 已提交
329 330 331
{
	struct rio_ops *ops;
	struct rio_mport *port;
332 333 334 335 336
	struct rio_priv *priv;
	int rc = 0;
	const u32 *dt_range, *cell;
	struct resource regs;
	int rlen;
337
	u32 ccsr;
338 339 340
	u64 law_start, law_size;
	int paw, aw, sw;

341
	if (!dev->dev.of_node) {
342 343 344 345
		dev_err(&dev->dev, "Device OF-Node is NULL");
		return -EFAULT;
	}

346
	rc = of_address_to_resource(dev->dev.of_node, 0, &regs);
347 348
	if (rc) {
		dev_err(&dev->dev, "Can't get %s property 'reg'\n",
349
				dev->dev.of_node->full_name);
350 351
		return -EFAULT;
	}
352 353
	dev_info(&dev->dev, "Of-device full name %s\n",
			dev->dev.of_node->full_name);
354
	dev_info(&dev->dev, "Regs: %pR\n", &regs);
355

356
	dt_range = of_get_property(dev->dev.of_node, "ranges", &rlen);
357 358
	if (!dt_range) {
		dev_err(&dev->dev, "Can't get %s property 'ranges'\n",
359
				dev->dev.of_node->full_name);
360 361 362 363
		return -EFAULT;
	}

	/* Get node address wide */
364
	cell = of_get_property(dev->dev.of_node, "#address-cells", NULL);
365 366 367
	if (cell)
		aw = *cell;
	else
368
		aw = of_n_addr_cells(dev->dev.of_node);
369
	/* Get node size wide */
370
	cell = of_get_property(dev->dev.of_node, "#size-cells", NULL);
371 372 373
	if (cell)
		sw = *cell;
	else
374
		sw = of_n_size_cells(dev->dev.of_node);
375
	/* Get parent address wide wide */
376
	paw = of_n_addr_cells(dev->dev.of_node);
377 378 379 380 381 382

	law_start = of_read_number(dt_range + aw, paw);
	law_size = of_read_number(dt_range + aw + paw, sw);

	dev_info(&dev->dev, "LAW start 0x%016llx, size 0x%016llx.\n",
			law_start, law_size);
M
Matt Porter 已提交
383

384
	ops = kzalloc(sizeof(struct rio_ops), GFP_KERNEL);
385 386 387 388
	if (!ops) {
		rc = -ENOMEM;
		goto err_ops;
	}
389 390 391 392
	ops->lcread = fsl_local_config_read;
	ops->lcwrite = fsl_local_config_write;
	ops->cread = fsl_rio_config_read;
	ops->cwrite = fsl_rio_config_write;
393
	ops->pwenable = fsl_rio_pw_enable;
M
Matt Porter 已提交
394

395
	port = kzalloc(sizeof(struct rio_mport), GFP_KERNEL);
396 397 398 399
	if (!port) {
		rc = -ENOMEM;
		goto err_port;
	}
M
Matt Porter 已提交
400
	port->index = 0;
401 402 403 404 405

	priv = kzalloc(sizeof(struct rio_priv), GFP_KERNEL);
	if (!priv) {
		printk(KERN_ERR "Can't alloc memory for 'priv'\n");
		rc = -ENOMEM;
406
		goto err_priv;
407 408
	}

M
Matt Porter 已提交
409 410
	INIT_LIST_HEAD(&port->dbells);
	port->iores.start = law_start;
411
	port->iores.end = law_start + law_size - 1;
M
Matt Porter 已提交
412
	port->iores.flags = IORESOURCE_MEM;
413
	port->iores.name = "rio_io_win";
M
Matt Porter 已提交
414

415 416 417 418 419 420 421 422
	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);
			rc = -ENOMEM;
			goto err_res;
	}

423 424
	priv->pwirq = irq_of_parse_and_map(dev->dev.of_node, 0);
	dev_info(&dev->dev, "pwirq: %d\n", priv->pwirq);
M
Matt Porter 已提交
425 426
	strcpy(port->name, "RIO0 mport");

427 428
	priv->dev = &dev->dev;

M
Matt Porter 已提交
429
	port->ops = ops;
430
	port->priv = priv;
431
	port->phys_efptr = 0x100;
M
Matt Porter 已提交
432

433
	priv->regs_win = ioremap(regs.start, resource_size(&regs));
434
	rio_regs_win = priv->regs_win;
435

436 437 438 439 440 441 442
	/* Probe the master port phy type */
	ccsr = in_be32(priv->regs_win + RIO_CCSR);
	port->phy_type = (ccsr & 1) ? RIO_PHY_SERIAL : RIO_PHY_PARALLEL;
	dev_info(&dev->dev, "RapidIO PHY type: %s\n",
			(port->phy_type == RIO_PHY_PARALLEL) ? "parallel" :
			((port->phy_type == RIO_PHY_SERIAL) ? "serial" :
			 "unknown"));
443 444 445
	/* Checking the port training status */
	if (in_be32((priv->regs_win + RIO_ESCSR)) & 1) {
		dev_err(&dev->dev, "Port is not ready. "
446
			"Try to restart connection...\n");
447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471
		switch (port->phy_type) {
		case RIO_PHY_SERIAL:
			/* Disable ports */
			out_be32(priv->regs_win + RIO_CCSR, 0);
			/* Set 1x lane */
			setbits32(priv->regs_win + RIO_CCSR, 0x02000000);
			/* Enable ports */
			setbits32(priv->regs_win + RIO_CCSR, 0x00600000);
			break;
		case RIO_PHY_PARALLEL:
			/* Disable ports */
			out_be32(priv->regs_win + RIO_CCSR, 0x22000000);
			/* Enable ports */
			out_be32(priv->regs_win + RIO_CCSR, 0x44000000);
			break;
		}
		msleep(100);
		if (in_be32((priv->regs_win + RIO_ESCSR)) & 1) {
			dev_err(&dev->dev, "Port restart failed.\n");
			rc = -ENOLINK;
			goto err;
		}
		dev_info(&dev->dev, "Port restart success!\n");
	}
	fsl_rio_info(&dev->dev, ccsr);
472

473 474 475 476 477
	port->sys_size = (in_be32((priv->regs_win + RIO_PEF_CAR))
					& RIO_PEF_CTLS) >> 4;
	dev_info(&dev->dev, "RapidIO Common Transport System size: %d\n",
			port->sys_size ? 65536 : 256);

478 479 480
	if (rio_register_mport(port))
		goto err;

481 482 483 484 485 486
	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, 0x00000000);

487 488 489
	priv->atmu_regs = (struct rio_atmu_regs *)(priv->regs_win
					+ RIO_ATMU_REGS_OFFSET);
	priv->maint_atmu_regs = priv->atmu_regs + 1;
490 491 492 493

	/* Set to receive any dist ID for serial RapidIO controller. */
	if (port->phy_type == RIO_PHY_SERIAL)
		out_be32((priv->regs_win + RIO_ISR_AACR), RIO_ISR_AACR_AA);
M
Matt Porter 已提交
494 495

	/* Configure maintenance transaction window */
496
	out_be32(&priv->maint_atmu_regs->rowbar, law_start >> 12);
497 498
	out_be32(&priv->maint_atmu_regs->rowar,
		 0x80077000 | (ilog2(RIO_MAINT_WIN_SIZE) - 1));
M
Matt Porter 已提交
499

500
	priv->maint_win = ioremap(law_start, RIO_MAINT_WIN_SIZE);
M
Matt Porter 已提交
501

502 503
	fsl_rio_setup_rmu(port, dev->dev.of_node);

504
	fsl_rio_port_write_init(port);
505

506
	return 0;
507
err:
508
	iounmap(priv->regs_win);
509
	release_resource(&port->iores);
510
err_res:
511
	kfree(priv);
512
err_priv:
513
	kfree(port);
514 515 516
err_port:
	kfree(ops);
err_ops:
517
	return rc;
M
Matt Porter 已提交
518
}
519 520 521

/* The probe function for RapidIO peer-to-peer network.
 */
522
static int __devinit fsl_of_rio_rpn_probe(struct platform_device *dev)
523 524
{
	printk(KERN_INFO "Setting up RapidIO peer-to-peer network %s\n",
525
			dev->dev.of_node->full_name);
526

527
	return fsl_rio_setup(dev);
528 529 530 531 532 533 534 535 536
};

static const struct of_device_id fsl_of_rio_rpn_ids[] = {
	{
		.compatible = "fsl,rapidio-delta",
	},
	{},
};

537
static struct platform_driver fsl_of_rio_rpn_driver = {
538 539 540 541 542
	.driver = {
		.name = "fsl-of-rio",
		.owner = THIS_MODULE,
		.of_match_table = fsl_of_rio_rpn_ids,
	},
543 544 545 546 547
	.probe = fsl_of_rio_rpn_probe,
};

static __init int fsl_of_rio_rpn_init(void)
{
548
	return platform_driver_register(&fsl_of_rio_rpn_driver);
549 550 551
}

subsys_initcall(fsl_of_rio_rpn_init);