bus.c 16 KB

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