bus.c 15 KB

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