bus.c 15 KB

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