platform.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. /*
  2. * Platform driver for the Synopsys DesignWare DMA Controller
  3. *
  4. * Copyright (C) 2007-2008 Atmel Corporation
  5. * Copyright (C) 2010-2011 ST Microelectronics
  6. * Copyright (C) 2013 Intel Corporation
  7. *
  8. * Some parts of this driver are derived from the original dw_dmac.
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License version 2 as
  12. * published by the Free Software Foundation.
  13. */
  14. #include <linux/module.h>
  15. #include <linux/device.h>
  16. #include <linux/clk.h>
  17. #include <linux/pm_runtime.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/dmaengine.h>
  20. #include <linux/dma-mapping.h>
  21. #include <linux/of.h>
  22. #include <linux/of_dma.h>
  23. #include <linux/acpi.h>
  24. #include <linux/acpi_dma.h>
  25. #include "internal.h"
  26. #define DRV_NAME "dw_dmac"
  27. static struct dma_chan *dw_dma_of_xlate(struct of_phandle_args *dma_spec,
  28. struct of_dma *ofdma)
  29. {
  30. struct dw_dma *dw = ofdma->of_dma_data;
  31. struct dw_dma_slave slave = {
  32. .dma_dev = dw->dma.dev,
  33. };
  34. dma_cap_mask_t cap;
  35. if (dma_spec->args_count != 3)
  36. return NULL;
  37. slave.src_id = dma_spec->args[0];
  38. slave.dst_id = dma_spec->args[0];
  39. slave.m_master = dma_spec->args[1];
  40. slave.p_master = dma_spec->args[2];
  41. if (WARN_ON(slave.src_id >= DW_DMA_MAX_NR_REQUESTS ||
  42. slave.dst_id >= DW_DMA_MAX_NR_REQUESTS ||
  43. slave.m_master >= dw->pdata->nr_masters ||
  44. slave.p_master >= dw->pdata->nr_masters))
  45. return NULL;
  46. dma_cap_zero(cap);
  47. dma_cap_set(DMA_SLAVE, cap);
  48. /* TODO: there should be a simpler way to do this */
  49. return dma_request_channel(cap, dw_dma_filter, &slave);
  50. }
  51. #ifdef CONFIG_ACPI
  52. static bool dw_dma_acpi_filter(struct dma_chan *chan, void *param)
  53. {
  54. struct acpi_dma_spec *dma_spec = param;
  55. struct dw_dma_slave slave = {
  56. .dma_dev = dma_spec->dev,
  57. .src_id = dma_spec->slave_id,
  58. .dst_id = dma_spec->slave_id,
  59. .m_master = 0,
  60. .p_master = 1,
  61. };
  62. return dw_dma_filter(chan, &slave);
  63. }
  64. static void dw_dma_acpi_controller_register(struct dw_dma *dw)
  65. {
  66. struct device *dev = dw->dma.dev;
  67. struct acpi_dma_filter_info *info;
  68. int ret;
  69. info = devm_kzalloc(dev, sizeof(*info), GFP_KERNEL);
  70. if (!info)
  71. return;
  72. dma_cap_zero(info->dma_cap);
  73. dma_cap_set(DMA_SLAVE, info->dma_cap);
  74. info->filter_fn = dw_dma_acpi_filter;
  75. ret = devm_acpi_dma_controller_register(dev, acpi_dma_simple_xlate,
  76. info);
  77. if (ret)
  78. dev_err(dev, "could not register acpi_dma_controller\n");
  79. }
  80. #else /* !CONFIG_ACPI */
  81. static inline void dw_dma_acpi_controller_register(struct dw_dma *dw) {}
  82. #endif /* !CONFIG_ACPI */
  83. #ifdef CONFIG_OF
  84. static struct dw_dma_platform_data *
  85. dw_dma_parse_dt(struct platform_device *pdev)
  86. {
  87. struct device_node *np = pdev->dev.of_node;
  88. struct dw_dma_platform_data *pdata;
  89. u32 tmp, arr[DW_DMA_MAX_NR_MASTERS];
  90. u32 nr_masters;
  91. u32 nr_channels;
  92. if (!np) {
  93. dev_err(&pdev->dev, "Missing DT data\n");
  94. return NULL;
  95. }
  96. if (of_property_read_u32(np, "dma-masters", &nr_masters))
  97. return NULL;
  98. if (nr_masters < 1 || nr_masters > DW_DMA_MAX_NR_MASTERS)
  99. return NULL;
  100. if (of_property_read_u32(np, "dma-channels", &nr_channels))
  101. return NULL;
  102. pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
  103. if (!pdata)
  104. return NULL;
  105. pdata->nr_masters = nr_masters;
  106. pdata->nr_channels = nr_channels;
  107. if (of_property_read_bool(np, "is_private"))
  108. pdata->is_private = true;
  109. if (!of_property_read_u32(np, "chan_allocation_order", &tmp))
  110. pdata->chan_allocation_order = (unsigned char)tmp;
  111. if (!of_property_read_u32(np, "chan_priority", &tmp))
  112. pdata->chan_priority = tmp;
  113. if (!of_property_read_u32(np, "block_size", &tmp))
  114. pdata->block_size = tmp;
  115. if (!of_property_read_u32_array(np, "data-width", arr, nr_masters)) {
  116. for (tmp = 0; tmp < nr_masters; tmp++)
  117. pdata->data_width[tmp] = arr[tmp];
  118. } else if (!of_property_read_u32_array(np, "data_width", arr, nr_masters)) {
  119. for (tmp = 0; tmp < nr_masters; tmp++)
  120. pdata->data_width[tmp] = BIT(arr[tmp] & 0x07);
  121. }
  122. return pdata;
  123. }
  124. #else
  125. static inline struct dw_dma_platform_data *
  126. dw_dma_parse_dt(struct platform_device *pdev)
  127. {
  128. return NULL;
  129. }
  130. #endif
  131. static int dw_probe(struct platform_device *pdev)
  132. {
  133. struct dw_dma_chip *chip;
  134. struct device *dev = &pdev->dev;
  135. struct resource *mem;
  136. const struct dw_dma_platform_data *pdata;
  137. int err;
  138. chip = devm_kzalloc(dev, sizeof(*chip), GFP_KERNEL);
  139. if (!chip)
  140. return -ENOMEM;
  141. chip->irq = platform_get_irq(pdev, 0);
  142. if (chip->irq < 0)
  143. return chip->irq;
  144. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  145. chip->regs = devm_ioremap_resource(dev, mem);
  146. if (IS_ERR(chip->regs))
  147. return PTR_ERR(chip->regs);
  148. err = dma_coerce_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
  149. if (err)
  150. return err;
  151. pdata = dev_get_platdata(dev);
  152. if (!pdata)
  153. pdata = dw_dma_parse_dt(pdev);
  154. chip->dev = dev;
  155. chip->pdata = pdata;
  156. chip->clk = devm_clk_get(chip->dev, "hclk");
  157. if (IS_ERR(chip->clk))
  158. return PTR_ERR(chip->clk);
  159. err = clk_prepare_enable(chip->clk);
  160. if (err)
  161. return err;
  162. pm_runtime_enable(&pdev->dev);
  163. err = dw_dma_probe(chip);
  164. if (err)
  165. goto err_dw_dma_probe;
  166. platform_set_drvdata(pdev, chip);
  167. if (pdev->dev.of_node) {
  168. err = of_dma_controller_register(pdev->dev.of_node,
  169. dw_dma_of_xlate, chip->dw);
  170. if (err)
  171. dev_err(&pdev->dev,
  172. "could not register of_dma_controller\n");
  173. }
  174. if (ACPI_HANDLE(&pdev->dev))
  175. dw_dma_acpi_controller_register(chip->dw);
  176. return 0;
  177. err_dw_dma_probe:
  178. pm_runtime_disable(&pdev->dev);
  179. clk_disable_unprepare(chip->clk);
  180. return err;
  181. }
  182. static int dw_remove(struct platform_device *pdev)
  183. {
  184. struct dw_dma_chip *chip = platform_get_drvdata(pdev);
  185. if (pdev->dev.of_node)
  186. of_dma_controller_free(pdev->dev.of_node);
  187. dw_dma_remove(chip);
  188. pm_runtime_disable(&pdev->dev);
  189. clk_disable_unprepare(chip->clk);
  190. return 0;
  191. }
  192. static void dw_shutdown(struct platform_device *pdev)
  193. {
  194. struct dw_dma_chip *chip = platform_get_drvdata(pdev);
  195. /*
  196. * We have to call dw_dma_disable() to stop any ongoing transfer. On
  197. * some platforms we can't do that since DMA device is powered off.
  198. * Moreover we have no possibility to check if the platform is affected
  199. * or not. That's why we call pm_runtime_get_sync() / pm_runtime_put()
  200. * unconditionally. On the other hand we can't use
  201. * pm_runtime_suspended() because runtime PM framework is not fully
  202. * used by the driver.
  203. */
  204. pm_runtime_get_sync(chip->dev);
  205. dw_dma_disable(chip);
  206. pm_runtime_put_sync_suspend(chip->dev);
  207. clk_disable_unprepare(chip->clk);
  208. }
  209. #ifdef CONFIG_OF
  210. static const struct of_device_id dw_dma_of_id_table[] = {
  211. { .compatible = "snps,dma-spear1340" },
  212. {}
  213. };
  214. MODULE_DEVICE_TABLE(of, dw_dma_of_id_table);
  215. #endif
  216. #ifdef CONFIG_ACPI
  217. static const struct acpi_device_id dw_dma_acpi_id_table[] = {
  218. { "INTL9C60", 0 },
  219. { }
  220. };
  221. MODULE_DEVICE_TABLE(acpi, dw_dma_acpi_id_table);
  222. #endif
  223. #ifdef CONFIG_PM_SLEEP
  224. static int dw_suspend_late(struct device *dev)
  225. {
  226. struct platform_device *pdev = to_platform_device(dev);
  227. struct dw_dma_chip *chip = platform_get_drvdata(pdev);
  228. dw_dma_disable(chip);
  229. clk_disable_unprepare(chip->clk);
  230. return 0;
  231. }
  232. static int dw_resume_early(struct device *dev)
  233. {
  234. struct platform_device *pdev = to_platform_device(dev);
  235. struct dw_dma_chip *chip = platform_get_drvdata(pdev);
  236. clk_prepare_enable(chip->clk);
  237. return dw_dma_enable(chip);
  238. }
  239. #endif /* CONFIG_PM_SLEEP */
  240. static const struct dev_pm_ops dw_dev_pm_ops = {
  241. SET_LATE_SYSTEM_SLEEP_PM_OPS(dw_suspend_late, dw_resume_early)
  242. };
  243. static struct platform_driver dw_driver = {
  244. .probe = dw_probe,
  245. .remove = dw_remove,
  246. .shutdown = dw_shutdown,
  247. .driver = {
  248. .name = DRV_NAME,
  249. .pm = &dw_dev_pm_ops,
  250. .of_match_table = of_match_ptr(dw_dma_of_id_table),
  251. .acpi_match_table = ACPI_PTR(dw_dma_acpi_id_table),
  252. },
  253. };
  254. static int __init dw_init(void)
  255. {
  256. return platform_driver_register(&dw_driver);
  257. }
  258. subsys_initcall(dw_init);
  259. static void __exit dw_exit(void)
  260. {
  261. platform_driver_unregister(&dw_driver);
  262. }
  263. module_exit(dw_exit);
  264. MODULE_LICENSE("GPL v2");
  265. MODULE_DESCRIPTION("Synopsys DesignWare DMA Controller platform driver");
  266. MODULE_ALIAS("platform:" DRV_NAME);