bus.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616
  1. /*
  2. * linux/arch/arm/common/amba.c
  3. *
  4. * Copyright (C) 2003 Deep Blue Solutions Ltd, All Rights Reserved.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. */
  10. #include <linux/module.h>
  11. #include <linux/init.h>
  12. #include <linux/device.h>
  13. #include <linux/string.h>
  14. #include <linux/slab.h>
  15. #include <linux/io.h>
  16. #include <linux/pm.h>
  17. #include <linux/pm_runtime.h>
  18. #include <linux/amba/bus.h>
  19. #include <linux/sizes.h>
  20. #include <asm/irq.h>
  21. #define to_amba_driver(d) container_of(d, struct amba_driver, drv)
  22. static const struct amba_id *
  23. amba_lookup(const struct amba_id *table, struct amba_device *dev)
  24. {
  25. int ret = 0;
  26. while (table->mask) {
  27. ret = (dev->periphid & table->mask) == table->id;
  28. if (ret)
  29. break;
  30. table++;
  31. }
  32. return ret ? table : NULL;
  33. }
  34. static int amba_match(struct device *dev, struct device_driver *drv)
  35. {
  36. struct amba_device *pcdev = to_amba_device(dev);
  37. struct amba_driver *pcdrv = to_amba_driver(drv);
  38. return amba_lookup(pcdrv->id_table, pcdev) != NULL;
  39. }
  40. static int amba_uevent(struct device *dev, struct kobj_uevent_env *env)
  41. {
  42. struct amba_device *pcdev = to_amba_device(dev);
  43. int retval = 0;
  44. retval = add_uevent_var(env, "AMBA_ID=%08x", pcdev->periphid);
  45. if (retval)
  46. return retval;
  47. retval = add_uevent_var(env, "MODALIAS=amba:d%08X", pcdev->periphid);
  48. return retval;
  49. }
  50. #define amba_attr_func(name,fmt,arg...) \
  51. static ssize_t name##_show(struct device *_dev, \
  52. struct device_attribute *attr, char *buf) \
  53. { \
  54. struct amba_device *dev = to_amba_device(_dev); \
  55. return sprintf(buf, fmt, arg); \
  56. }
  57. #define amba_attr(name,fmt,arg...) \
  58. amba_attr_func(name,fmt,arg) \
  59. static DEVICE_ATTR(name, S_IRUGO, name##_show, NULL)
  60. amba_attr_func(id, "%08x\n", dev->periphid);
  61. amba_attr(irq0, "%u\n", dev->irq[0]);
  62. amba_attr(irq1, "%u\n", dev->irq[1]);
  63. amba_attr_func(resource, "\t%016llx\t%016llx\t%016lx\n",
  64. (unsigned long long)dev->res.start, (unsigned long long)dev->res.end,
  65. dev->res.flags);
  66. static struct device_attribute amba_dev_attrs[] = {
  67. __ATTR_RO(id),
  68. __ATTR_RO(resource),
  69. __ATTR_NULL,
  70. };
  71. #ifdef CONFIG_PM
  72. /*
  73. * Hooks to provide runtime PM of the pclk (bus clock). It is safe to
  74. * enable/disable the bus clock at runtime PM suspend/resume as this
  75. * does not result in loss of context.
  76. */
  77. static int amba_pm_runtime_suspend(struct device *dev)
  78. {
  79. struct amba_device *pcdev = to_amba_device(dev);
  80. int ret = pm_generic_runtime_suspend(dev);
  81. if (ret == 0 && dev->driver)
  82. clk_disable_unprepare(pcdev->pclk);
  83. return ret;
  84. }
  85. static int amba_pm_runtime_resume(struct device *dev)
  86. {
  87. struct amba_device *pcdev = to_amba_device(dev);
  88. int ret;
  89. if (dev->driver) {
  90. ret = clk_prepare_enable(pcdev->pclk);
  91. /* Failure is probably fatal to the system, but... */
  92. if (ret)
  93. return ret;
  94. }
  95. return pm_generic_runtime_resume(dev);
  96. }
  97. #endif
  98. static const struct dev_pm_ops amba_pm = {
  99. .suspend = pm_generic_suspend,
  100. .resume = pm_generic_resume,
  101. .freeze = pm_generic_freeze,
  102. .thaw = pm_generic_thaw,
  103. .poweroff = pm_generic_poweroff,
  104. .restore = pm_generic_restore,
  105. SET_PM_RUNTIME_PM_OPS(
  106. amba_pm_runtime_suspend,
  107. amba_pm_runtime_resume,
  108. NULL
  109. )
  110. };
  111. /*
  112. * Primecells are part of the Advanced Microcontroller Bus Architecture,
  113. * so we call the bus "amba".
  114. */
  115. struct bus_type amba_bustype = {
  116. .name = "amba",
  117. .dev_attrs = amba_dev_attrs,
  118. .match = amba_match,
  119. .uevent = amba_uevent,
  120. .pm = &amba_pm,
  121. };
  122. static int __init amba_init(void)
  123. {
  124. return bus_register(&amba_bustype);
  125. }
  126. postcore_initcall(amba_init);
  127. static int amba_get_enable_pclk(struct amba_device *pcdev)
  128. {
  129. int ret;
  130. pcdev->pclk = clk_get(&pcdev->dev, "apb_pclk");
  131. if (IS_ERR(pcdev->pclk))
  132. return PTR_ERR(pcdev->pclk);
  133. ret = clk_prepare_enable(pcdev->pclk);
  134. if (ret)
  135. clk_put(pcdev->pclk);
  136. return ret;
  137. }
  138. static void amba_put_disable_pclk(struct amba_device *pcdev)
  139. {
  140. clk_disable_unprepare(pcdev->pclk);
  141. clk_put(pcdev->pclk);
  142. }
  143. /*
  144. * These are the device model conversion veneers; they convert the
  145. * device model structures to our more specific structures.
  146. */
  147. static int amba_probe(struct device *dev)
  148. {
  149. struct amba_device *pcdev = to_amba_device(dev);
  150. struct amba_driver *pcdrv = to_amba_driver(dev->driver);
  151. const struct amba_id *id = amba_lookup(pcdrv->id_table, pcdev);
  152. int ret;
  153. do {
  154. ret = amba_get_enable_pclk(pcdev);
  155. if (ret)
  156. break;
  157. pm_runtime_get_noresume(dev);
  158. pm_runtime_set_active(dev);
  159. pm_runtime_enable(dev);
  160. ret = pcdrv->probe(pcdev, id);
  161. if (ret == 0)
  162. break;
  163. pm_runtime_disable(dev);
  164. pm_runtime_set_suspended(dev);
  165. pm_runtime_put_noidle(dev);
  166. amba_put_disable_pclk(pcdev);
  167. } while (0);
  168. return ret;
  169. }
  170. static int amba_remove(struct device *dev)
  171. {
  172. struct amba_device *pcdev = to_amba_device(dev);
  173. struct amba_driver *drv = to_amba_driver(dev->driver);
  174. int ret;
  175. pm_runtime_get_sync(dev);
  176. ret = drv->remove(pcdev);
  177. pm_runtime_put_noidle(dev);
  178. /* Undo the runtime PM settings in amba_probe() */
  179. pm_runtime_disable(dev);
  180. pm_runtime_set_suspended(dev);
  181. pm_runtime_put_noidle(dev);
  182. amba_put_disable_pclk(pcdev);
  183. return ret;
  184. }
  185. static void amba_shutdown(struct device *dev)
  186. {
  187. struct amba_driver *drv = to_amba_driver(dev->driver);
  188. drv->shutdown(to_amba_device(dev));
  189. }
  190. /**
  191. * amba_driver_register - register an AMBA device driver
  192. * @drv: amba device driver structure
  193. *
  194. * Register an AMBA device driver with the Linux device model
  195. * core. If devices pre-exist, the drivers probe function will
  196. * be called.
  197. */
  198. int amba_driver_register(struct amba_driver *drv)
  199. {
  200. drv->drv.bus = &amba_bustype;
  201. #define SETFN(fn) if (drv->fn) drv->drv.fn = amba_##fn
  202. SETFN(probe);
  203. SETFN(remove);
  204. SETFN(shutdown);
  205. return driver_register(&drv->drv);
  206. }
  207. /**
  208. * amba_driver_unregister - remove an AMBA device driver
  209. * @drv: AMBA device driver structure to remove
  210. *
  211. * Unregister an AMBA device driver from the Linux device
  212. * model. The device model will call the drivers remove function
  213. * for each device the device driver is currently handling.
  214. */
  215. void amba_driver_unregister(struct amba_driver *drv)
  216. {
  217. driver_unregister(&drv->drv);
  218. }
  219. static void amba_device_release(struct device *dev)
  220. {
  221. struct amba_device *d = to_amba_device(dev);
  222. if (d->res.parent)
  223. release_resource(&d->res);
  224. kfree(d);
  225. }
  226. /**
  227. * amba_device_add - add a previously allocated AMBA device structure
  228. * @dev: AMBA device allocated by amba_device_alloc
  229. * @parent: resource parent for this devices resources
  230. *
  231. * Claim the resource, and read the device cell ID if not already
  232. * initialized. Register the AMBA device with the Linux device
  233. * manager.
  234. */
  235. int amba_device_add(struct amba_device *dev, struct resource *parent)
  236. {
  237. u32 size;
  238. void __iomem *tmp;
  239. int i, ret;
  240. WARN_ON(dev->irq[0] == (unsigned int)-1);
  241. WARN_ON(dev->irq[1] == (unsigned int)-1);
  242. ret = request_resource(parent, &dev->res);
  243. if (ret)
  244. goto err_out;
  245. /* Hard-coded primecell ID instead of plug-n-play */
  246. if (dev->periphid != 0)
  247. goto skip_probe;
  248. /*
  249. * Dynamically calculate the size of the resource
  250. * and use this for iomap
  251. */
  252. size = resource_size(&dev->res);
  253. tmp = ioremap(dev->res.start, size);
  254. if (!tmp) {
  255. ret = -ENOMEM;
  256. goto err_release;
  257. }
  258. ret = amba_get_enable_pclk(dev);
  259. if (ret == 0) {
  260. u32 pid, cid;
  261. /*
  262. * Read pid and cid based on size of resource
  263. * they are located at end of region
  264. */
  265. for (pid = 0, i = 0; i < 4; i++)
  266. pid |= (readl(tmp + size - 0x20 + 4 * i) & 255) <<
  267. (i * 8);
  268. for (cid = 0, i = 0; i < 4; i++)
  269. cid |= (readl(tmp + size - 0x10 + 4 * i) & 255) <<
  270. (i * 8);
  271. amba_put_disable_pclk(dev);
  272. if (cid == AMBA_CID)
  273. dev->periphid = pid;
  274. if (!dev->periphid)
  275. ret = -ENODEV;
  276. }
  277. iounmap(tmp);
  278. if (ret)
  279. goto err_release;
  280. skip_probe:
  281. ret = device_add(&dev->dev);
  282. if (ret)
  283. goto err_release;
  284. if (dev->irq[0])
  285. ret = device_create_file(&dev->dev, &dev_attr_irq0);
  286. if (ret == 0 && dev->irq[1])
  287. ret = device_create_file(&dev->dev, &dev_attr_irq1);
  288. if (ret == 0)
  289. return ret;
  290. device_unregister(&dev->dev);
  291. err_release:
  292. release_resource(&dev->res);
  293. err_out:
  294. return ret;
  295. }
  296. EXPORT_SYMBOL_GPL(amba_device_add);
  297. static struct amba_device *
  298. amba_aphb_device_add(struct device *parent, const char *name,
  299. resource_size_t base, size_t size, int irq1, int irq2,
  300. void *pdata, unsigned int periphid, u64 dma_mask,
  301. struct resource *resbase)
  302. {
  303. struct amba_device *dev;
  304. int ret;
  305. dev = amba_device_alloc(name, base, size);
  306. if (!dev)
  307. return ERR_PTR(-ENOMEM);
  308. dev->dev.coherent_dma_mask = dma_mask;
  309. dev->irq[0] = irq1;
  310. dev->irq[1] = irq2;
  311. dev->periphid = periphid;
  312. dev->dev.platform_data = pdata;
  313. dev->dev.parent = parent;
  314. ret = amba_device_add(dev, resbase);
  315. if (ret) {
  316. amba_device_put(dev);
  317. return ERR_PTR(ret);
  318. }
  319. return dev;
  320. }
  321. struct amba_device *
  322. amba_apb_device_add(struct device *parent, const char *name,
  323. resource_size_t base, size_t size, int irq1, int irq2,
  324. void *pdata, unsigned int periphid)
  325. {
  326. return amba_aphb_device_add(parent, name, base, size, irq1, irq2, pdata,
  327. periphid, 0, &iomem_resource);
  328. }
  329. EXPORT_SYMBOL_GPL(amba_apb_device_add);
  330. struct amba_device *
  331. amba_ahb_device_add(struct device *parent, const char *name,
  332. resource_size_t base, size_t size, int irq1, int irq2,
  333. void *pdata, unsigned int periphid)
  334. {
  335. return amba_aphb_device_add(parent, name, base, size, irq1, irq2, pdata,
  336. periphid, ~0ULL, &iomem_resource);
  337. }
  338. EXPORT_SYMBOL_GPL(amba_ahb_device_add);
  339. struct amba_device *
  340. amba_apb_device_add_res(struct device *parent, const char *name,
  341. resource_size_t base, size_t size, int irq1,
  342. int irq2, void *pdata, unsigned int periphid,
  343. struct resource *resbase)
  344. {
  345. return amba_aphb_device_add(parent, name, base, size, irq1, irq2, pdata,
  346. periphid, 0, resbase);
  347. }
  348. EXPORT_SYMBOL_GPL(amba_apb_device_add_res);
  349. struct amba_device *
  350. amba_ahb_device_add_res(struct device *parent, const char *name,
  351. resource_size_t base, size_t size, int irq1,
  352. int irq2, void *pdata, unsigned int periphid,
  353. struct resource *resbase)
  354. {
  355. return amba_aphb_device_add(parent, name, base, size, irq1, irq2, pdata,
  356. periphid, ~0ULL, resbase);
  357. }
  358. EXPORT_SYMBOL_GPL(amba_ahb_device_add_res);
  359. static void amba_device_initialize(struct amba_device *dev, const char *name)
  360. {
  361. device_initialize(&dev->dev);
  362. if (name)
  363. dev_set_name(&dev->dev, "%s", name);
  364. dev->dev.release = amba_device_release;
  365. dev->dev.bus = &amba_bustype;
  366. dev->dev.dma_mask = &dev->dev.coherent_dma_mask;
  367. dev->res.name = dev_name(&dev->dev);
  368. }
  369. /**
  370. * amba_device_alloc - allocate an AMBA device
  371. * @name: sysfs name of the AMBA device
  372. * @base: base of AMBA device
  373. * @size: size of AMBA device
  374. *
  375. * Allocate and initialize an AMBA device structure. Returns %NULL
  376. * on failure.
  377. */
  378. struct amba_device *amba_device_alloc(const char *name, resource_size_t base,
  379. size_t size)
  380. {
  381. struct amba_device *dev;
  382. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  383. if (dev) {
  384. amba_device_initialize(dev, name);
  385. dev->res.start = base;
  386. dev->res.end = base + size - 1;
  387. dev->res.flags = IORESOURCE_MEM;
  388. }
  389. return dev;
  390. }
  391. EXPORT_SYMBOL_GPL(amba_device_alloc);
  392. /**
  393. * amba_device_register - register an AMBA device
  394. * @dev: AMBA device to register
  395. * @parent: parent memory resource
  396. *
  397. * Setup the AMBA device, reading the cell ID if present.
  398. * Claim the resource, and register the AMBA device with
  399. * the Linux device manager.
  400. */
  401. int amba_device_register(struct amba_device *dev, struct resource *parent)
  402. {
  403. amba_device_initialize(dev, dev->dev.init_name);
  404. dev->dev.init_name = NULL;
  405. return amba_device_add(dev, parent);
  406. }
  407. /**
  408. * amba_device_put - put an AMBA device
  409. * @dev: AMBA device to put
  410. */
  411. void amba_device_put(struct amba_device *dev)
  412. {
  413. put_device(&dev->dev);
  414. }
  415. EXPORT_SYMBOL_GPL(amba_device_put);
  416. /**
  417. * amba_device_unregister - unregister an AMBA device
  418. * @dev: AMBA device to remove
  419. *
  420. * Remove the specified AMBA device from the Linux device
  421. * manager. All files associated with this object will be
  422. * destroyed, and device drivers notified that the device has
  423. * been removed. The AMBA device's resources including
  424. * the amba_device structure will be freed once all
  425. * references to it have been dropped.
  426. */
  427. void amba_device_unregister(struct amba_device *dev)
  428. {
  429. device_unregister(&dev->dev);
  430. }
  431. struct find_data {
  432. struct amba_device *dev;
  433. struct device *parent;
  434. const char *busid;
  435. unsigned int id;
  436. unsigned int mask;
  437. };
  438. static int amba_find_match(struct device *dev, void *data)
  439. {
  440. struct find_data *d = data;
  441. struct amba_device *pcdev = to_amba_device(dev);
  442. int r;
  443. r = (pcdev->periphid & d->mask) == d->id;
  444. if (d->parent)
  445. r &= d->parent == dev->parent;
  446. if (d->busid)
  447. r &= strcmp(dev_name(dev), d->busid) == 0;
  448. if (r) {
  449. get_device(dev);
  450. d->dev = pcdev;
  451. }
  452. return r;
  453. }
  454. /**
  455. * amba_find_device - locate an AMBA device given a bus id
  456. * @busid: bus id for device (or NULL)
  457. * @parent: parent device (or NULL)
  458. * @id: peripheral ID (or 0)
  459. * @mask: peripheral ID mask (or 0)
  460. *
  461. * Return the AMBA device corresponding to the supplied parameters.
  462. * If no device matches, returns NULL.
  463. *
  464. * NOTE: When a valid device is found, its refcount is
  465. * incremented, and must be decremented before the returned
  466. * reference.
  467. */
  468. struct amba_device *
  469. amba_find_device(const char *busid, struct device *parent, unsigned int id,
  470. unsigned int mask)
  471. {
  472. struct find_data data;
  473. data.dev = NULL;
  474. data.parent = parent;
  475. data.busid = busid;
  476. data.id = id;
  477. data.mask = mask;
  478. bus_for_each_dev(&amba_bustype, NULL, &data, amba_find_match);
  479. return data.dev;
  480. }
  481. /**
  482. * amba_request_regions - request all mem regions associated with device
  483. * @dev: amba_device structure for device
  484. * @name: name, or NULL to use driver name
  485. */
  486. int amba_request_regions(struct amba_device *dev, const char *name)
  487. {
  488. int ret = 0;
  489. u32 size;
  490. if (!name)
  491. name = dev->dev.driver->name;
  492. size = resource_size(&dev->res);
  493. if (!request_mem_region(dev->res.start, size, name))
  494. ret = -EBUSY;
  495. return ret;
  496. }
  497. /**
  498. * amba_release_regions - release mem regions associated with device
  499. * @dev: amba_device structure for device
  500. *
  501. * Release regions claimed by a successful call to amba_request_regions.
  502. */
  503. void amba_release_regions(struct amba_device *dev)
  504. {
  505. u32 size;
  506. size = resource_size(&dev->res);
  507. release_mem_region(dev->res.start, size);
  508. }
  509. EXPORT_SYMBOL(amba_driver_register);
  510. EXPORT_SYMBOL(amba_driver_unregister);
  511. EXPORT_SYMBOL(amba_device_register);
  512. EXPORT_SYMBOL(amba_device_unregister);
  513. EXPORT_SYMBOL(amba_find_device);
  514. EXPORT_SYMBOL(amba_request_regions);
  515. EXPORT_SYMBOL(amba_release_regions);