Blame view

drivers/amba/bus.c 14.7 KB
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
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>
4e57b6817   Tim Schmielau   [PATCH] fix missi...
13
14
  #include <linux/string.h>
  #include <linux/slab.h>
934848daa   Russell King   [ARM] Fix realvie...
15
  #include <linux/io.h>
ba74ec7f6   Rabin Vincent   ARM: 6758/1: amba...
16
17
  #include <linux/pm.h>
  #include <linux/pm_runtime.h>
a62c80e55   Russell King   [ARM] Move AMBA i...
18
  #include <linux/amba/bus.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
19

934848daa   Russell King   [ARM] Fix realvie...
20
  #include <asm/irq.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
21
  #include <asm/sizes.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
22
  #define to_amba_driver(d)	container_of(d, struct amba_driver, drv)
c862aab0b   Russell King   ARM: amba: make i...
23
24
  static const struct amba_id *
  amba_lookup(const struct amba_id *table, struct amba_device *dev)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
25
26
27
28
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);
  
  	return amba_lookup(pcdrv->id_table, pcdev) != NULL;
  }
  
  #ifdef CONFIG_HOTPLUG
7eff2e7a8   Kay Sievers   Driver core: chan...
47
  static int amba_uevent(struct device *dev, struct kobj_uevent_env *env)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
48
49
  {
  	struct amba_device *pcdev = to_amba_device(dev);
7eff2e7a8   Kay Sievers   Driver core: chan...
50
  	int retval = 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
51

7eff2e7a8   Kay Sievers   Driver core: chan...
52
  	retval = add_uevent_var(env, "AMBA_ID=%08x", pcdev->periphid);
523817bd2   Dave Martin   ARM: amba: Auto-g...
53
54
55
56
  	if (retval)
  		return retval;
  
  	retval = add_uevent_var(env, "MODALIAS=amba:d%08X", pcdev->periphid);
bf62456eb   Eric Rannaud   uevent: use add_u...
57
  	return retval;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
58
59
  }
  #else
6d20b035d   Paul Jackson   [PATCH] driver ki...
60
  #define amba_uevent NULL
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
61
  #endif
96b13f5c0   Russell King   [ARM] Fix __must_...
62
63
64
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
  #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
  ", dev->periphid);
  amba_attr(irq0, "%u
  ", dev->irq[0]);
  amba_attr(irq1, "%u
  ", dev->irq[1]);
  amba_attr_func(resource, "\t%016llx\t%016llx\t%016lx
  ",
  	 (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),
  	__ATTR_NULL,
  };
ba74ec7f6   Rabin Vincent   ARM: 6758/1: amba...
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
  #ifdef CONFIG_PM_SLEEP
  
  static int amba_legacy_suspend(struct device *dev, pm_message_t mesg)
  {
  	struct amba_driver *adrv = to_amba_driver(dev->driver);
  	struct amba_device *adev = to_amba_device(dev);
  	int ret = 0;
  
  	if (dev->driver && adrv->suspend)
  		ret = adrv->suspend(adev, mesg);
  
  	return ret;
  }
  
  static int amba_legacy_resume(struct device *dev)
  {
  	struct amba_driver *adrv = to_amba_driver(dev->driver);
  	struct amba_device *adev = to_amba_device(dev);
  	int ret = 0;
  
  	if (dev->driver && adrv->resume)
  		ret = adrv->resume(adev);
  
  	return ret;
  }
8114ab763   Rafael J. Wysocki   PM / Sleep: Remov...
115
  #endif /* CONFIG_PM_SLEEP */
ba74ec7f6   Rabin Vincent   ARM: 6758/1: amba...
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
  
  #ifdef CONFIG_SUSPEND
  
  static int amba_pm_suspend(struct device *dev)
  {
  	struct device_driver *drv = dev->driver;
  	int ret = 0;
  
  	if (!drv)
  		return 0;
  
  	if (drv->pm) {
  		if (drv->pm->suspend)
  			ret = drv->pm->suspend(dev);
  	} else {
  		ret = amba_legacy_suspend(dev, PMSG_SUSPEND);
  	}
  
  	return ret;
  }
ba74ec7f6   Rabin Vincent   ARM: 6758/1: amba...
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
  static int amba_pm_resume(struct device *dev)
  {
  	struct device_driver *drv = dev->driver;
  	int ret = 0;
  
  	if (!drv)
  		return 0;
  
  	if (drv->pm) {
  		if (drv->pm->resume)
  			ret = drv->pm->resume(dev);
  	} else {
  		ret = amba_legacy_resume(dev);
  	}
  
  	return ret;
  }
ba74ec7f6   Rabin Vincent   ARM: 6758/1: amba...
153
154
155
156
  #else /* !CONFIG_SUSPEND */
  
  #define amba_pm_suspend		NULL
  #define amba_pm_resume		NULL
ba74ec7f6   Rabin Vincent   ARM: 6758/1: amba...
157
158
  
  #endif /* !CONFIG_SUSPEND */
1f112cee0   Rafael J. Wysocki   PM / Hibernate: I...
159
  #ifdef CONFIG_HIBERNATE_CALLBACKS
ba74ec7f6   Rabin Vincent   ARM: 6758/1: amba...
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
  
  static int amba_pm_freeze(struct device *dev)
  {
  	struct device_driver *drv = dev->driver;
  	int ret = 0;
  
  	if (!drv)
  		return 0;
  
  	if (drv->pm) {
  		if (drv->pm->freeze)
  			ret = drv->pm->freeze(dev);
  	} else {
  		ret = amba_legacy_suspend(dev, PMSG_FREEZE);
  	}
  
  	return ret;
  }
ba74ec7f6   Rabin Vincent   ARM: 6758/1: amba...
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
  static int amba_pm_thaw(struct device *dev)
  {
  	struct device_driver *drv = dev->driver;
  	int ret = 0;
  
  	if (!drv)
  		return 0;
  
  	if (drv->pm) {
  		if (drv->pm->thaw)
  			ret = drv->pm->thaw(dev);
  	} else {
  		ret = amba_legacy_resume(dev);
  	}
  
  	return ret;
  }
ba74ec7f6   Rabin Vincent   ARM: 6758/1: amba...
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
  static int amba_pm_poweroff(struct device *dev)
  {
  	struct device_driver *drv = dev->driver;
  	int ret = 0;
  
  	if (!drv)
  		return 0;
  
  	if (drv->pm) {
  		if (drv->pm->poweroff)
  			ret = drv->pm->poweroff(dev);
  	} else {
  		ret = amba_legacy_suspend(dev, PMSG_HIBERNATE);
  	}
  
  	return ret;
  }
ba74ec7f6   Rabin Vincent   ARM: 6758/1: amba...
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
  static int amba_pm_restore(struct device *dev)
  {
  	struct device_driver *drv = dev->driver;
  	int ret = 0;
  
  	if (!drv)
  		return 0;
  
  	if (drv->pm) {
  		if (drv->pm->restore)
  			ret = drv->pm->restore(dev);
  	} else {
  		ret = amba_legacy_resume(dev);
  	}
  
  	return ret;
  }
1f112cee0   Rafael J. Wysocki   PM / Hibernate: I...
229
  #else /* !CONFIG_HIBERNATE_CALLBACKS */
ba74ec7f6   Rabin Vincent   ARM: 6758/1: amba...
230
231
232
233
234
  
  #define amba_pm_freeze		NULL
  #define amba_pm_thaw		NULL
  #define amba_pm_poweroff		NULL
  #define amba_pm_restore		NULL
ba74ec7f6   Rabin Vincent   ARM: 6758/1: amba...
235

1f112cee0   Rafael J. Wysocki   PM / Hibernate: I...
236
  #endif /* !CONFIG_HIBERNATE_CALLBACKS */
ba74ec7f6   Rabin Vincent   ARM: 6758/1: amba...
237

92b97f0aa   Russell King   PM: add runtime P...
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
  #ifdef CONFIG_PM_RUNTIME
  /*
   * 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
   * does not result in loss of context.  However, disabling vcore power
   * would do, so we leave that to the driver.
   */
  static int amba_pm_runtime_suspend(struct device *dev)
  {
  	struct amba_device *pcdev = to_amba_device(dev);
  	int ret = pm_generic_runtime_suspend(dev);
  
  	if (ret == 0 && dev->driver)
  		clk_disable(pcdev->pclk);
  
  	return ret;
  }
  
  static int amba_pm_runtime_resume(struct device *dev)
  {
  	struct amba_device *pcdev = to_amba_device(dev);
  	int ret;
  
  	if (dev->driver) {
  		ret = clk_enable(pcdev->pclk);
  		/* Failure is probably fatal to the system, but... */
  		if (ret)
  			return ret;
  	}
  
  	return pm_generic_runtime_resume(dev);
  }
  #endif
ba74ec7f6   Rabin Vincent   ARM: 6758/1: amba...
271
272
273
  #ifdef CONFIG_PM
  
  static const struct dev_pm_ops amba_pm = {
ba74ec7f6   Rabin Vincent   ARM: 6758/1: amba...
274
275
276
277
278
279
  	.suspend	= amba_pm_suspend,
  	.resume		= amba_pm_resume,
  	.freeze		= amba_pm_freeze,
  	.thaw		= amba_pm_thaw,
  	.poweroff	= amba_pm_poweroff,
  	.restore	= amba_pm_restore,
ba74ec7f6   Rabin Vincent   ARM: 6758/1: amba...
280
  	SET_RUNTIME_PM_OPS(
92b97f0aa   Russell King   PM: add runtime P...
281
282
  		amba_pm_runtime_suspend,
  		amba_pm_runtime_resume,
ba74ec7f6   Rabin Vincent   ARM: 6758/1: amba...
283
284
285
286
287
288
289
290
291
292
293
  		pm_generic_runtime_idle
  	)
  };
  
  #define AMBA_PM (&amba_pm)
  
  #else /* !CONFIG_PM */
  
  #define AMBA_PM	NULL
  
  #endif /* !CONFIG_PM */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
294
295
296
297
  /*
   * Primecells are part of the Advanced Microcontroller Bus Architecture,
   * so we call the bus "amba".
   */
394d5aefc   Rob Herring   ARM: 6662/1: amba...
298
  struct bus_type amba_bustype = {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
299
  	.name		= "amba",
96b13f5c0   Russell King   [ARM] Fix __must_...
300
  	.dev_attrs	= amba_dev_attrs,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
301
  	.match		= amba_match,
6d20b035d   Paul Jackson   [PATCH] driver ki...
302
  	.uevent		= amba_uevent,
ba74ec7f6   Rabin Vincent   ARM: 6758/1: amba...
303
  	.pm		= AMBA_PM,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
304
305
306
307
308
309
310
311
  };
  
  static int __init amba_init(void)
  {
  	return bus_register(&amba_bustype);
  }
  
  postcore_initcall(amba_init);
7cfe24947   Russell King   ARM: AMBA: Add pc...
312
313
314
315
316
317
318
319
320
  static int amba_get_enable_pclk(struct amba_device *pcdev)
  {
  	struct clk *pclk = clk_get(&pcdev->dev, "apb_pclk");
  	int ret;
  
  	pcdev->pclk = pclk;
  
  	if (IS_ERR(pclk))
  		return PTR_ERR(pclk);
ac3e2fa67   Russell King   clk: amba bus: co...
321
322
323
324
325
  	ret = clk_prepare(pclk);
  	if (ret) {
  		clk_put(pclk);
  		return ret;
  	}
7cfe24947   Russell King   ARM: AMBA: Add pc...
326
  	ret = clk_enable(pclk);
ac3e2fa67   Russell King   clk: amba bus: co...
327
328
  	if (ret) {
  		clk_unprepare(pclk);
7cfe24947   Russell King   ARM: AMBA: Add pc...
329
  		clk_put(pclk);
ac3e2fa67   Russell King   clk: amba bus: co...
330
  	}
7cfe24947   Russell King   ARM: AMBA: Add pc...
331
332
333
334
335
336
337
338
339
  
  	return ret;
  }
  
  static void amba_put_disable_pclk(struct amba_device *pcdev)
  {
  	struct clk *pclk = pcdev->pclk;
  
  	clk_disable(pclk);
ac3e2fa67   Russell King   clk: amba bus: co...
340
  	clk_unprepare(pclk);
7cfe24947   Russell King   ARM: AMBA: Add pc...
341
342
  	clk_put(pclk);
  }
65500fa94   Linus Walleij   ARM: 6467/1: amba...
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
  static int amba_get_enable_vcore(struct amba_device *pcdev)
  {
  	struct regulator *vcore = regulator_get(&pcdev->dev, "vcore");
  	int ret;
  
  	pcdev->vcore = vcore;
  
  	if (IS_ERR(vcore)) {
  		/* It is OK not to supply a vcore regulator */
  		if (PTR_ERR(vcore) == -ENODEV)
  			return 0;
  		return PTR_ERR(vcore);
  	}
  
  	ret = regulator_enable(vcore);
  	if (ret) {
  		regulator_put(vcore);
  		pcdev->vcore = ERR_PTR(-ENODEV);
  	}
  
  	return ret;
  }
  
  static void amba_put_disable_vcore(struct amba_device *pcdev)
  {
  	struct regulator *vcore = pcdev->vcore;
  
  	if (!IS_ERR(vcore)) {
  		regulator_disable(vcore);
  		regulator_put(vcore);
  	}
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
375
376
377
378
379
380
381
382
  /*
   * 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);
c862aab0b   Russell King   ARM: amba: make i...
383
  	const struct amba_id *id = amba_lookup(pcdrv->id_table, pcdev);
7cfe24947   Russell King   ARM: AMBA: Add pc...
384
  	int ret;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
385

7cfe24947   Russell King   ARM: AMBA: Add pc...
386
  	do {
65500fa94   Linus Walleij   ARM: 6467/1: amba...
387
388
389
  		ret = amba_get_enable_vcore(pcdev);
  		if (ret)
  			break;
7cfe24947   Russell King   ARM: AMBA: Add pc...
390
391
392
  		ret = amba_get_enable_pclk(pcdev);
  		if (ret)
  			break;
92b97f0aa   Russell King   PM: add runtime P...
393
394
395
  		pm_runtime_get_noresume(dev);
  		pm_runtime_set_active(dev);
  		pm_runtime_enable(dev);
7cfe24947   Russell King   ARM: AMBA: Add pc...
396
397
398
  		ret = pcdrv->probe(pcdev, id);
  		if (ret == 0)
  			break;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
399

92b97f0aa   Russell King   PM: add runtime P...
400
401
402
  		pm_runtime_disable(dev);
  		pm_runtime_set_suspended(dev);
  		pm_runtime_put_noidle(dev);
7cfe24947   Russell King   ARM: AMBA: Add pc...
403
  		amba_put_disable_pclk(pcdev);
65500fa94   Linus Walleij   ARM: 6467/1: amba...
404
  		amba_put_disable_vcore(pcdev);
7cfe24947   Russell King   ARM: AMBA: Add pc...
405
406
407
  	} while (0);
  
  	return ret;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
408
409
410
411
  }
  
  static int amba_remove(struct device *dev)
  {
7cfe24947   Russell King   ARM: AMBA: Add pc...
412
  	struct amba_device *pcdev = to_amba_device(dev);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
413
  	struct amba_driver *drv = to_amba_driver(dev->driver);
92b97f0aa   Russell King   PM: add runtime P...
414
415
416
417
418
419
420
421
422
423
  	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);
7cfe24947   Russell King   ARM: AMBA: Add pc...
424
425
  
  	amba_put_disable_pclk(pcdev);
65500fa94   Linus Walleij   ARM: 6467/1: amba...
426
  	amba_put_disable_vcore(pcdev);
7cfe24947   Russell King   ARM: AMBA: Add pc...
427
428
  
  	return ret;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
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
472
473
474
475
476
477
478
  }
  
  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);
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
479
480
481
482
483
484
485
486
487
488
489
  /**
   *	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)
  {
8afe0b96b   Leo Chen   ARM: 5625/1: fix ...
490
  	u32 size;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
491
492
  	void __iomem *tmp;
  	int i, ret;
557dca5f4   Russell King   [ARM] amba: fix a...
493
494
495
496
497
498
499
500
501
  	device_initialize(&dev->dev);
  
  	/*
  	 * Copy from device_add
  	 */
  	if (dev->dev.init_name) {
  		dev_set_name(&dev->dev, "%s", dev->dev.init_name);
  		dev->dev.init_name = NULL;
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
502
503
504
  	dev->dev.release = amba_device_release;
  	dev->dev.bus = &amba_bustype;
  	dev->dev.dma_mask = &dev->dma_mask;
9d6b4c82b   Kay Sievers   amba: struct devi...
505
  	dev->res.name = dev_name(&dev->dev);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
506
507
508
509
510
511
  
  	if (!dev->dev.coherent_dma_mask && dev->dma_mask)
  		dev_warn(&dev->dev, "coherent dma mask is unset
  ");
  
  	ret = request_resource(parent, &dev->res);
96b13f5c0   Russell King   [ARM] Fix __must_...
512
513
  	if (ret)
  		goto err_out;
97ceed1fc   Linus Walleij   ARM: 6829/1: amba...
514
515
516
  	/* Hard-coded primecell ID instead of plug-n-play */
  	if (dev->periphid != 0)
  		goto skip_probe;
8afe0b96b   Leo Chen   ARM: 5625/1: fix ...
517
518
519
520
521
522
  	/*
  	 * Dynamically calculate the size of the resource
  	 * and use this for iomap
  	 */
  	size = resource_size(&dev->res);
  	tmp = ioremap(dev->res.start, size);
96b13f5c0   Russell King   [ARM] Fix __must_...
523
524
525
  	if (!tmp) {
  		ret = -ENOMEM;
  		goto err_release;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
526
  	}
96b13f5c0   Russell King   [ARM] Fix __must_...
527

7cfe24947   Russell King   ARM: AMBA: Add pc...
528
529
530
  	ret = amba_get_enable_pclk(dev);
  	if (ret == 0) {
  		u32 pid, cid;
96b13f5c0   Russell King   [ARM] Fix __must_...
531

7cfe24947   Russell King   ARM: AMBA: Add pc...
532
533
534
535
536
537
538
539
540
541
  		/*
  		 * 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);
96b13f5c0   Russell King   [ARM] Fix __must_...
542

7cfe24947   Russell King   ARM: AMBA: Add pc...
543
  		amba_put_disable_pclk(dev);
96b13f5c0   Russell King   [ARM] Fix __must_...
544

01723a956   Linus Walleij   ARM: 6368/1: move...
545
  		if (cid == AMBA_CID)
7cfe24947   Russell King   ARM: AMBA: Add pc...
546
547
548
549
  			dev->periphid = pid;
  
  		if (!dev->periphid)
  			ret = -ENODEV;
96b13f5c0   Russell King   [ARM] Fix __must_...
550
  	}
7cfe24947   Russell King   ARM: AMBA: Add pc...
551
552
553
554
  	iounmap(tmp);
  
  	if (ret)
  		goto err_release;
97ceed1fc   Linus Walleij   ARM: 6829/1: amba...
555
   skip_probe:
557dca5f4   Russell King   [ARM] amba: fix a...
556
  	ret = device_add(&dev->dev);
96b13f5c0   Russell King   [ARM] Fix __must_...
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
  	if (ret)
  		goto err_release;
  
  	if (dev->irq[0] != NO_IRQ)
  		ret = device_create_file(&dev->dev, &dev_attr_irq0);
  	if (ret == 0 && dev->irq[1] != NO_IRQ)
  		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:
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
572
573
574
575
576
577
578
579
580
581
582
583
584
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
  	return ret;
  }
  
  /**
   *	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)
9d6b4c82b   Kay Sievers   amba: struct devi...
610
  		r &= strcmp(dev_name(dev), d->busid) == 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
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
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
  
  	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;
8afe0b96b   Leo Chen   ARM: 5625/1: fix ...
659
  	u32 size;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
660
661
662
  
  	if (!name)
  		name = dev->dev.driver->name;
8afe0b96b   Leo Chen   ARM: 5625/1: fix ...
663
664
665
  	size = resource_size(&dev->res);
  
  	if (!request_mem_region(dev->res.start, size, name))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
666
667
668
669
670
671
  		ret = -EBUSY;
  
  	return ret;
  }
  
  /**
25985edce   Lucas De Marchi   Fix common misspe...
672
   *	amba_release_regions - release mem regions associated with device
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
673
674
675
676
677
678
   *	@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)
  {
8afe0b96b   Leo Chen   ARM: 5625/1: fix ...
679
680
681
682
  	u32 size;
  
  	size = resource_size(&dev->res);
  	release_mem_region(dev->res.start, size);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
683
684
685
686
687
688
689
690
691
  }
  
  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);