bus.c 15.8 KB
Newer Older
Linus Torvalds's avatar
Linus Torvalds committed
1 2 3 4 5 6 7 8 9 10 11 12
/*
 *  linux/arch/arm/common/amba.c
 *
 *  Copyright (C) 2003 Deep Blue Solutions Ltd, All Rights Reserved.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 */
#include <linux/module.h>
#include <linux/init.h>
#include <linux/device.h>
Tim Schmielau's avatar
Tim Schmielau committed
13 14
#include <linux/string.h>
#include <linux/slab.h>
Russell King's avatar
Russell King committed
15
#include <linux/io.h>
16 17
#include <linux/pm.h>
#include <linux/pm_runtime.h>
18
#include <linux/pm_domain.h>
19
#include <linux/amba/bus.h>
20
#include <linux/sizes.h>
21
#include <linux/limits.h>
Linus Torvalds's avatar
Linus Torvalds committed
22

Russell King's avatar
Russell King committed
23
#include <asm/irq.h>
Linus Torvalds's avatar
Linus Torvalds committed
24 25 26

#define to_amba_driver(d)	container_of(d, struct amba_driver, drv)

27 28
static const struct amba_id *
amba_lookup(const struct amba_id *table, struct amba_device *dev)
Linus Torvalds's avatar
Linus Torvalds committed
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
{
	int ret = 0;

	while (table->mask) {
		ret = (dev->periphid & table->mask) == table->id;
		if (ret)
			break;
		table++;
	}

	return ret ? table : NULL;
}

static int amba_match(struct device *dev, struct device_driver *drv)
{
	struct amba_device *pcdev = to_amba_device(dev);
	struct amba_driver *pcdrv = to_amba_driver(drv);

47 48 49 50
	/* When driver_override is set, only bind to the matching driver */
	if (pcdev->driver_override)
		return !strcmp(pcdev->driver_override, drv->name);

Linus Torvalds's avatar
Linus Torvalds committed
51 52 53
	return amba_lookup(pcdrv->id_table, pcdev) != NULL;
}

54
static int amba_uevent(struct device *dev, struct kobj_uevent_env *env)
Linus Torvalds's avatar
Linus Torvalds committed
55 56
{
	struct amba_device *pcdev = to_amba_device(dev);
57
	int retval = 0;
Linus Torvalds's avatar
Linus Torvalds committed
58

59
	retval = add_uevent_var(env, "AMBA_ID=%08x", pcdev->periphid);
60 61 62 63
	if (retval)
		return retval;

	retval = add_uevent_var(env, "MODALIAS=amba:d%08X", pcdev->periphid);
64
	return retval;
Linus Torvalds's avatar
Linus Torvalds committed
65 66
}

67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
static ssize_t driver_override_show(struct device *_dev,
				    struct device_attribute *attr, char *buf)
{
	struct amba_device *dev = to_amba_device(_dev);

	return sprintf(buf, "%s\n", dev->driver_override);
}

static ssize_t driver_override_store(struct device *_dev,
				     struct device_attribute *attr,
				     const char *buf, size_t count)
{
	struct amba_device *dev = to_amba_device(_dev);
	char *driver_override, *old = dev->driver_override, *cp;

	if (count > PATH_MAX)
		return -EINVAL;

	driver_override = kstrndup(buf, count, GFP_KERNEL);
	if (!driver_override)
		return -ENOMEM;

	cp = strchr(driver_override, '\n');
	if (cp)
		*cp = '\0';

	if (strlen(driver_override)) {
		dev->driver_override = driver_override;
	} else {
	       kfree(driver_override);
	       dev->driver_override = NULL;
	}

	kfree(old);

	return count;
}

105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
#define amba_attr_func(name,fmt,arg...)					\
static ssize_t name##_show(struct device *_dev,				\
			   struct device_attribute *attr, char *buf)	\
{									\
	struct amba_device *dev = to_amba_device(_dev);			\
	return sprintf(buf, fmt, arg);					\
}

#define amba_attr(name,fmt,arg...)	\
amba_attr_func(name,fmt,arg)		\
static DEVICE_ATTR(name, S_IRUGO, name##_show, NULL)

amba_attr_func(id, "%08x\n", dev->periphid);
amba_attr(irq0, "%u\n", dev->irq[0]);
amba_attr(irq1, "%u\n", dev->irq[1]);
amba_attr_func(resource, "\t%016llx\t%016llx\t%016lx\n",
	 (unsigned long long)dev->res.start, (unsigned long long)dev->res.end,
	 dev->res.flags);

static struct device_attribute amba_dev_attrs[] = {
	__ATTR_RO(id),
	__ATTR_RO(resource),
127
	__ATTR_RW(driver_override),
128 129 130
	__ATTR_NULL,
};

131
#ifdef CONFIG_PM
132 133 134
/*
 * Hooks to provide runtime PM of the pclk (bus clock).  It is safe to
 * enable/disable the bus clock at runtime PM suspend/resume as this
135
 * does not result in loss of context.
136 137 138 139 140 141
 */
static int amba_pm_runtime_suspend(struct device *dev)
{
	struct amba_device *pcdev = to_amba_device(dev);
	int ret = pm_generic_runtime_suspend(dev);

142 143 144 145 146 147
	if (ret == 0 && dev->driver) {
		if (pm_runtime_is_irq_safe(dev))
			clk_disable(pcdev->pclk);
		else
			clk_disable_unprepare(pcdev->pclk);
	}
148 149 150 151 152 153 154 155 156 157

	return ret;
}

static int amba_pm_runtime_resume(struct device *dev)
{
	struct amba_device *pcdev = to_amba_device(dev);
	int ret;

	if (dev->driver) {
158 159 160 161
		if (pm_runtime_is_irq_safe(dev))
			ret = clk_enable(pcdev->pclk);
		else
			ret = clk_prepare_enable(pcdev->pclk);
162 163 164 165 166 167 168
		/* Failure is probably fatal to the system, but... */
		if (ret)
			return ret;
	}

	return pm_generic_runtime_resume(dev);
}
169
#endif /* CONFIG_PM */
170

171
static const struct dev_pm_ops amba_pm = {
172 173 174 175 176 177
	.suspend	= pm_generic_suspend,
	.resume		= pm_generic_resume,
	.freeze		= pm_generic_freeze,
	.thaw		= pm_generic_thaw,
	.poweroff	= pm_generic_poweroff,
	.restore	= pm_generic_restore,
178
	SET_RUNTIME_PM_OPS(
179 180
		amba_pm_runtime_suspend,
		amba_pm_runtime_resume,
181
		NULL
182 183 184
	)
};

Linus Torvalds's avatar
Linus Torvalds committed
185 186 187 188
/*
 * Primecells are part of the Advanced Microcontroller Bus Architecture,
 * so we call the bus "amba".
 */
189
struct bus_type amba_bustype = {
Linus Torvalds's avatar
Linus Torvalds committed
190
	.name		= "amba",
191
	.dev_attrs	= amba_dev_attrs,
Linus Torvalds's avatar
Linus Torvalds committed
192
	.match		= amba_match,
193
	.uevent		= amba_uevent,
194
	.pm		= &amba_pm,
Linus Torvalds's avatar
Linus Torvalds committed
195 196 197 198 199 200 201 202 203
};

static int __init amba_init(void)
{
	return bus_register(&amba_bustype);
}

postcore_initcall(amba_init);

204 205 206 207
static int amba_get_enable_pclk(struct amba_device *pcdev)
{
	int ret;

208 209 210
	pcdev->pclk = clk_get(&pcdev->dev, "apb_pclk");
	if (IS_ERR(pcdev->pclk))
		return PTR_ERR(pcdev->pclk);
211

212 213 214
	ret = clk_prepare_enable(pcdev->pclk);
	if (ret)
		clk_put(pcdev->pclk);
215 216 217 218 219 220

	return ret;
}

static void amba_put_disable_pclk(struct amba_device *pcdev)
{
221 222
	clk_disable_unprepare(pcdev->pclk);
	clk_put(pcdev->pclk);
223 224
}

Linus Torvalds's avatar
Linus Torvalds committed
225 226 227 228 229 230 231 232
/*
 * These are the device model conversion veneers; they convert the
 * device model structures to our more specific structures.
 */
static int amba_probe(struct device *dev)
{
	struct amba_device *pcdev = to_amba_device(dev);
	struct amba_driver *pcdrv = to_amba_driver(dev->driver);
233
	const struct amba_id *id = amba_lookup(pcdrv->id_table, pcdev);
234
	int ret;
Linus Torvalds's avatar
Linus Torvalds committed
235

236
	do {
237 238 239 240
		ret = dev_pm_domain_attach(dev, true);
		if (ret == -EPROBE_DEFER)
			break;

241
		ret = amba_get_enable_pclk(pcdev);
242 243
		if (ret) {
			dev_pm_domain_detach(dev, true);
244
			break;
245
		}
246

247 248 249 250
		pm_runtime_get_noresume(dev);
		pm_runtime_set_active(dev);
		pm_runtime_enable(dev);

251 252 253
		ret = pcdrv->probe(pcdev, id);
		if (ret == 0)
			break;
Linus Torvalds's avatar
Linus Torvalds committed
254

255 256 257 258
		pm_runtime_disable(dev);
		pm_runtime_set_suspended(dev);
		pm_runtime_put_noidle(dev);

259
		amba_put_disable_pclk(pcdev);
260
		dev_pm_domain_detach(dev, true);
261 262 263
	} while (0);

	return ret;
Linus Torvalds's avatar
Linus Torvalds committed
264 265 266 267
}

static int amba_remove(struct device *dev)
{
268
	struct amba_device *pcdev = to_amba_device(dev);
Linus Torvalds's avatar
Linus Torvalds committed
269
	struct amba_driver *drv = to_amba_driver(dev->driver);
270 271 272 273 274 275 276 277 278 279
	int ret;

	pm_runtime_get_sync(dev);
	ret = drv->remove(pcdev);
	pm_runtime_put_noidle(dev);

	/* Undo the runtime PM settings in amba_probe() */
	pm_runtime_disable(dev);
	pm_runtime_set_suspended(dev);
	pm_runtime_put_noidle(dev);
280 281

	amba_put_disable_pclk(pcdev);
282
	dev_pm_domain_detach(dev, true);
283 284

	return ret;
Linus Torvalds's avatar
Linus Torvalds committed
285 286 287 288 289 290 291 292 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 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336
}

static void amba_shutdown(struct device *dev)
{
	struct amba_driver *drv = to_amba_driver(dev->driver);
	drv->shutdown(to_amba_device(dev));
}

/**
 *	amba_driver_register - register an AMBA device driver
 *	@drv: amba device driver structure
 *
 *	Register an AMBA device driver with the Linux device model
 *	core.  If devices pre-exist, the drivers probe function will
 *	be called.
 */
int amba_driver_register(struct amba_driver *drv)
{
	drv->drv.bus = &amba_bustype;

#define SETFN(fn)	if (drv->fn) drv->drv.fn = amba_##fn
	SETFN(probe);
	SETFN(remove);
	SETFN(shutdown);

	return driver_register(&drv->drv);
}

/**
 *	amba_driver_unregister - remove an AMBA device driver
 *	@drv: AMBA device driver structure to remove
 *
 *	Unregister an AMBA device driver from the Linux device
 *	model.  The device model will call the drivers remove function
 *	for each device the device driver is currently handling.
 */
void amba_driver_unregister(struct amba_driver *drv)
{
	driver_unregister(&drv->drv);
}


static void amba_device_release(struct device *dev)
{
	struct amba_device *d = to_amba_device(dev);

	if (d->res.parent)
		release_resource(&d->res);
	kfree(d);
}

/**
337 338 339
 *	amba_device_add - add a previously allocated AMBA device structure
 *	@dev: AMBA device allocated by amba_device_alloc
 *	@parent: resource parent for this devices resources
Linus Torvalds's avatar
Linus Torvalds committed
340
 *
341 342 343
 *	Claim the resource, and read the device cell ID if not already
 *	initialized.  Register the AMBA device with the Linux device
 *	manager.
Linus Torvalds's avatar
Linus Torvalds committed
344
 */
345
int amba_device_add(struct amba_device *dev, struct resource *parent)
Linus Torvalds's avatar
Linus Torvalds committed
346
{
347
	u32 size;
Linus Torvalds's avatar
Linus Torvalds committed
348 349 350
	void __iomem *tmp;
	int i, ret;

351 352 353
	WARN_ON(dev->irq[0] == (unsigned int)-1);
	WARN_ON(dev->irq[1] == (unsigned int)-1);

Linus Torvalds's avatar
Linus Torvalds committed
354
	ret = request_resource(parent, &dev->res);
355 356 357
	if (ret)
		goto err_out;

358 359 360 361
	/* Hard-coded primecell ID instead of plug-n-play */
	if (dev->periphid != 0)
		goto skip_probe;

362 363 364 365 366 367
	/*
	 * Dynamically calculate the size of the resource
	 * and use this for iomap
	 */
	size = resource_size(&dev->res);
	tmp = ioremap(dev->res.start, size);
368 369 370
	if (!tmp) {
		ret = -ENOMEM;
		goto err_release;
Linus Torvalds's avatar
Linus Torvalds committed
371
	}
372

373 374 375
	ret = amba_get_enable_pclk(dev);
	if (ret == 0) {
		u32 pid, cid;
376

377 378 379 380 381 382 383 384 385 386
		/*
		 * Read pid and cid based on size of resource
		 * they are located at end of region
		 */
		for (pid = 0, i = 0; i < 4; i++)
			pid |= (readl(tmp + size - 0x20 + 4 * i) & 255) <<
				(i * 8);
		for (cid = 0, i = 0; i < 4; i++)
			cid |= (readl(tmp + size - 0x10 + 4 * i) & 255) <<
				(i * 8);
387

388
		amba_put_disable_pclk(dev);
389

390
		if (cid == AMBA_CID || cid == CORESIGHT_CID)
391 392 393 394
			dev->periphid = pid;

		if (!dev->periphid)
			ret = -ENODEV;
395 396
	}

397 398 399 400 401
	iounmap(tmp);

	if (ret)
		goto err_release;

402
 skip_probe:
403
	ret = device_add(&dev->dev);
404 405 406
	if (ret)
		goto err_release;

407
	if (dev->irq[0])
408
		ret = device_create_file(&dev->dev, &dev_attr_irq0);
409
	if (ret == 0 && dev->irq[1])
410 411 412 413 414 415 416 417 418
		ret = device_create_file(&dev->dev, &dev_attr_irq1);
	if (ret == 0)
		return ret;

	device_unregister(&dev->dev);

 err_release:
	release_resource(&dev->res);
 err_out:
Linus Torvalds's avatar
Linus Torvalds committed
419 420
	return ret;
}
421 422
EXPORT_SYMBOL_GPL(amba_device_add);

423 424 425
static struct amba_device *
amba_aphb_device_add(struct device *parent, const char *name,
		     resource_size_t base, size_t size, int irq1, int irq2,
426 427
		     void *pdata, unsigned int periphid, u64 dma_mask,
		     struct resource *resbase)
428 429 430 431 432 433 434 435 436 437 438 439 440 441 442
{
	struct amba_device *dev;
	int ret;

	dev = amba_device_alloc(name, base, size);
	if (!dev)
		return ERR_PTR(-ENOMEM);

	dev->dev.coherent_dma_mask = dma_mask;
	dev->irq[0] = irq1;
	dev->irq[1] = irq2;
	dev->periphid = periphid;
	dev->dev.platform_data = pdata;
	dev->dev.parent = parent;

443
	ret = amba_device_add(dev, resbase);
444 445 446 447 448 449 450 451 452 453 454 455 456 457
	if (ret) {
		amba_device_put(dev);
		return ERR_PTR(ret);
	}

	return dev;
}

struct amba_device *
amba_apb_device_add(struct device *parent, const char *name,
		    resource_size_t base, size_t size, int irq1, int irq2,
		    void *pdata, unsigned int periphid)
{
	return amba_aphb_device_add(parent, name, base, size, irq1, irq2, pdata,
458
				    periphid, 0, &iomem_resource);
459 460 461 462 463 464 465 466 467
}
EXPORT_SYMBOL_GPL(amba_apb_device_add);

struct amba_device *
amba_ahb_device_add(struct device *parent, const char *name,
		    resource_size_t base, size_t size, int irq1, int irq2,
		    void *pdata, unsigned int periphid)
{
	return amba_aphb_device_add(parent, name, base, size, irq1, irq2, pdata,
468
				    periphid, ~0ULL, &iomem_resource);
469 470 471
}
EXPORT_SYMBOL_GPL(amba_ahb_device_add);

472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494
struct amba_device *
amba_apb_device_add_res(struct device *parent, const char *name,
			resource_size_t base, size_t size, int irq1,
			int irq2, void *pdata, unsigned int periphid,
			struct resource *resbase)
{
	return amba_aphb_device_add(parent, name, base, size, irq1, irq2, pdata,
				    periphid, 0, resbase);
}
EXPORT_SYMBOL_GPL(amba_apb_device_add_res);

struct amba_device *
amba_ahb_device_add_res(struct device *parent, const char *name,
			resource_size_t base, size_t size, int irq1,
			int irq2, void *pdata, unsigned int periphid,
			struct resource *resbase)
{
	return amba_aphb_device_add(parent, name, base, size, irq1, irq2, pdata,
				    periphid, ~0ULL, resbase);
}
EXPORT_SYMBOL_GPL(amba_ahb_device_add_res);


495 496 497 498 499 500 501
static void amba_device_initialize(struct amba_device *dev, const char *name)
{
	device_initialize(&dev->dev);
	if (name)
		dev_set_name(&dev->dev, "%s", name);
	dev->dev.release = amba_device_release;
	dev->dev.bus = &amba_bustype;
502
	dev->dev.dma_mask = &dev->dev.coherent_dma_mask;
503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557
	dev->res.name = dev_name(&dev->dev);
}

/**
 *	amba_device_alloc - allocate an AMBA device
 *	@name: sysfs name of the AMBA device
 *	@base: base of AMBA device
 *	@size: size of AMBA device
 *
 *	Allocate and initialize an AMBA device structure.  Returns %NULL
 *	on failure.
 */
struct amba_device *amba_device_alloc(const char *name, resource_size_t base,
	size_t size)
{
	struct amba_device *dev;

	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
	if (dev) {
		amba_device_initialize(dev, name);
		dev->res.start = base;
		dev->res.end = base + size - 1;
		dev->res.flags = IORESOURCE_MEM;
	}

	return dev;
}
EXPORT_SYMBOL_GPL(amba_device_alloc);

/**
 *	amba_device_register - register an AMBA device
 *	@dev: AMBA device to register
 *	@parent: parent memory resource
 *
 *	Setup the AMBA device, reading the cell ID if present.
 *	Claim the resource, and register the AMBA device with
 *	the Linux device manager.
 */
int amba_device_register(struct amba_device *dev, struct resource *parent)
{
	amba_device_initialize(dev, dev->dev.init_name);
	dev->dev.init_name = NULL;

	return amba_device_add(dev, parent);
}

/**
 *	amba_device_put - put an AMBA device
 *	@dev: AMBA device to put
 */
void amba_device_put(struct amba_device *dev)
{
	put_device(&dev->dev);
}
EXPORT_SYMBOL_GPL(amba_device_put);
Linus Torvalds's avatar
Linus Torvalds committed
558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593

/**
 *	amba_device_unregister - unregister an AMBA device
 *	@dev: AMBA device to remove
 *
 *	Remove the specified AMBA device from the Linux device
 *	manager.  All files associated with this object will be
 *	destroyed, and device drivers notified that the device has
 *	been removed.  The AMBA device's resources including
 *	the amba_device structure will be freed once all
 *	references to it have been dropped.
 */
void amba_device_unregister(struct amba_device *dev)
{
	device_unregister(&dev->dev);
}


struct find_data {
	struct amba_device *dev;
	struct device *parent;
	const char *busid;
	unsigned int id;
	unsigned int mask;
};

static int amba_find_match(struct device *dev, void *data)
{
	struct find_data *d = data;
	struct amba_device *pcdev = to_amba_device(dev);
	int r;

	r = (pcdev->periphid & d->mask) == d->id;
	if (d->parent)
		r &= d->parent == dev->parent;
	if (d->busid)
594
		r &= strcmp(dev_name(dev), d->busid) == 0;
Linus Torvalds's avatar
Linus Torvalds committed
595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 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

	if (r) {
		get_device(dev);
		d->dev = pcdev;
	}

	return r;
}

/**
 *	amba_find_device - locate an AMBA device given a bus id
 *	@busid: bus id for device (or NULL)
 *	@parent: parent device (or NULL)
 *	@id: peripheral ID (or 0)
 *	@mask: peripheral ID mask (or 0)
 *
 *	Return the AMBA device corresponding to the supplied parameters.
 *	If no device matches, returns NULL.
 *
 *	NOTE: When a valid device is found, its refcount is
 *	incremented, and must be decremented before the returned
 *	reference.
 */
struct amba_device *
amba_find_device(const char *busid, struct device *parent, unsigned int id,
		 unsigned int mask)
{
	struct find_data data;

	data.dev = NULL;
	data.parent = parent;
	data.busid = busid;
	data.id = id;
	data.mask = mask;

	bus_for_each_dev(&amba_bustype, NULL, &data, amba_find_match);

	return data.dev;
}

/**
 *	amba_request_regions - request all mem regions associated with device
 *	@dev: amba_device structure for device
 *	@name: name, or NULL to use driver name
 */
int amba_request_regions(struct amba_device *dev, const char *name)
{
	int ret = 0;
643
	u32 size;
Linus Torvalds's avatar
Linus Torvalds committed
644 645 646 647

	if (!name)
		name = dev->dev.driver->name;

648 649 650
	size = resource_size(&dev->res);

	if (!request_mem_region(dev->res.start, size, name))
Linus Torvalds's avatar
Linus Torvalds committed
651 652 653 654 655 656
		ret = -EBUSY;

	return ret;
}

/**
Lucas De Marchi's avatar
Lucas De Marchi committed
657
 *	amba_release_regions - release mem regions associated with device
Linus Torvalds's avatar
Linus Torvalds committed
658 659 660 661 662 663
 *	@dev: amba_device structure for device
 *
 *	Release regions claimed by a successful call to amba_request_regions.
 */
void amba_release_regions(struct amba_device *dev)
{
664 665 666 667
	u32 size;

	size = resource_size(&dev->res);
	release_mem_region(dev->res.start, size);
Linus Torvalds's avatar
Linus Torvalds committed
668 669 670 671 672 673 674 675 676
}

EXPORT_SYMBOL(amba_driver_register);
EXPORT_SYMBOL(amba_driver_unregister);
EXPORT_SYMBOL(amba_device_register);
EXPORT_SYMBOL(amba_device_unregister);
EXPORT_SYMBOL(amba_find_device);
EXPORT_SYMBOL(amba_request_regions);
EXPORT_SYMBOL(amba_release_regions);