bus.c 15 KB

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