bus.c 16 KB

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