platform.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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], mb[DW_DMA_MAX_NR_CHANNELS];
  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. if (nr_channels > DW_DMA_MAX_NR_CHANNELS)
  103. return NULL;
  104. pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
  105. if (!pdata)
  106. return NULL;
  107. pdata->nr_masters = nr_masters;
  108. pdata->nr_channels = nr_channels;
  109. if (of_property_read_bool(np, "is_private"))
  110. pdata->is_private = true;
  111. /*
  112. * All known devices, which use DT for configuration, support
  113. * memory-to-memory transfers. So enable it by default.
  114. */
  115. pdata->is_memcpy = true;
  116. if (!of_property_read_u32(np, "chan_allocation_order", &tmp))
  117. pdata->chan_allocation_order = (unsigned char)tmp;
  118. if (!of_property_read_u32(np, "chan_priority", &tmp))
  119. pdata->chan_priority = tmp;
  120. if (!of_property_read_u32(np, "block_size", &tmp))
  121. pdata->block_size = tmp;
  122. if (!of_property_read_u32_array(np, "data-width", arr, nr_masters)) {
  123. for (tmp = 0; tmp < nr_masters; tmp++)
  124. pdata->data_width[tmp] = arr[tmp];
  125. } else if (!of_property_read_u32_array(np, "data_width", arr, nr_masters)) {
  126. for (tmp = 0; tmp < nr_masters; tmp++)
  127. pdata->data_width[tmp] = BIT(arr[tmp] & 0x07);
  128. }
  129. if (!of_property_read_u32_array(np, "multi-block", mb, nr_channels)) {
  130. for (tmp = 0; tmp < nr_channels; tmp++)
  131. pdata->multi_block[tmp] = mb[tmp];
  132. } else {
  133. for (tmp = 0; tmp < nr_channels; tmp++)
  134. pdata->multi_block[tmp] = 1;
  135. }
  136. return pdata;
  137. }
  138. #else
  139. static inline struct dw_dma_platform_data *
  140. dw_dma_parse_dt(struct platform_device *pdev)
  141. {
  142. return NULL;
  143. }
  144. #endif
  145. static int dw_probe(struct platform_device *pdev)
  146. {
  147. struct dw_dma_chip *chip;
  148. struct device *dev = &pdev->dev;
  149. struct resource *mem;
  150. const struct dw_dma_platform_data *pdata;
  151. int err;
  152. chip = devm_kzalloc(dev, sizeof(*chip), GFP_KERNEL);
  153. if (!chip)
  154. return -ENOMEM;
  155. chip->irq = platform_get_irq(pdev, 0);
  156. if (chip->irq < 0)
  157. return chip->irq;
  158. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  159. chip->regs = devm_ioremap_resource(dev, mem);
  160. if (IS_ERR(chip->regs))
  161. return PTR_ERR(chip->regs);
  162. err = dma_coerce_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
  163. if (err)
  164. return err;
  165. pdata = dev_get_platdata(dev);
  166. if (!pdata)
  167. pdata = dw_dma_parse_dt(pdev);
  168. chip->dev = dev;
  169. chip->pdata = pdata;
  170. chip->clk = devm_clk_get(chip->dev, "hclk");
  171. if (IS_ERR(chip->clk))
  172. return PTR_ERR(chip->clk);
  173. err = clk_prepare_enable(chip->clk);
  174. if (err)
  175. return err;
  176. pm_runtime_enable(&pdev->dev);
  177. err = dw_dma_probe(chip);
  178. if (err)
  179. goto err_dw_dma_probe;
  180. platform_set_drvdata(pdev, chip);
  181. if (pdev->dev.of_node) {
  182. err = of_dma_controller_register(pdev->dev.of_node,
  183. dw_dma_of_xlate, chip->dw);
  184. if (err)
  185. dev_err(&pdev->dev,
  186. "could not register of_dma_controller\n");
  187. }
  188. if (ACPI_HANDLE(&pdev->dev))
  189. dw_dma_acpi_controller_register(chip->dw);
  190. return 0;
  191. err_dw_dma_probe:
  192. pm_runtime_disable(&pdev->dev);
  193. clk_disable_unprepare(chip->clk);
  194. return err;
  195. }
  196. static int dw_remove(struct platform_device *pdev)
  197. {
  198. struct dw_dma_chip *chip = platform_get_drvdata(pdev);
  199. if (pdev->dev.of_node)
  200. of_dma_controller_free(pdev->dev.of_node);
  201. dw_dma_remove(chip);
  202. pm_runtime_disable(&pdev->dev);
  203. clk_disable_unprepare(chip->clk);
  204. return 0;
  205. }
  206. static void dw_shutdown(struct platform_device *pdev)
  207. {
  208. struct dw_dma_chip *chip = platform_get_drvdata(pdev);
  209. /*
  210. * We have to call dw_dma_disable() to stop any ongoing transfer. On
  211. * some platforms we can't do that since DMA device is powered off.
  212. * Moreover we have no possibility to check if the platform is affected
  213. * or not. That's why we call pm_runtime_get_sync() / pm_runtime_put()
  214. * unconditionally. On the other hand we can't use
  215. * pm_runtime_suspended() because runtime PM framework is not fully
  216. * used by the driver.
  217. */
  218. pm_runtime_get_sync(chip->dev);
  219. dw_dma_disable(chip);
  220. pm_runtime_put_sync_suspend(chip->dev);
  221. clk_disable_unprepare(chip->clk);
  222. }
  223. #ifdef CONFIG_OF
  224. static const struct of_device_id dw_dma_of_id_table[] = {
  225. { .compatible = "snps,dma-spear1340" },
  226. {}
  227. };
  228. MODULE_DEVICE_TABLE(of, dw_dma_of_id_table);
  229. #endif
  230. #ifdef CONFIG_ACPI
  231. static const struct acpi_device_id dw_dma_acpi_id_table[] = {
  232. { "INTL9C60", 0 },
  233. { }
  234. };
  235. MODULE_DEVICE_TABLE(acpi, dw_dma_acpi_id_table);
  236. #endif
  237. #ifdef CONFIG_PM_SLEEP
  238. static int dw_suspend_late(struct device *dev)
  239. {
  240. struct platform_device *pdev = to_platform_device(dev);
  241. struct dw_dma_chip *chip = platform_get_drvdata(pdev);
  242. dw_dma_disable(chip);
  243. clk_disable_unprepare(chip->clk);
  244. return 0;
  245. }
  246. static int dw_resume_early(struct device *dev)
  247. {
  248. struct platform_device *pdev = to_platform_device(dev);
  249. struct dw_dma_chip *chip = platform_get_drvdata(pdev);
  250. clk_prepare_enable(chip->clk);
  251. return dw_dma_enable(chip);
  252. }
  253. #endif /* CONFIG_PM_SLEEP */
  254. static const struct dev_pm_ops dw_dev_pm_ops = {
  255. SET_LATE_SYSTEM_SLEEP_PM_OPS(dw_suspend_late, dw_resume_early)
  256. };
  257. static struct platform_driver dw_driver = {
  258. .probe = dw_probe,
  259. .remove = dw_remove,
  260. .shutdown = dw_shutdown,
  261. .driver = {
  262. .name = DRV_NAME,
  263. .pm = &dw_dev_pm_ops,
  264. .of_match_table = of_match_ptr(dw_dma_of_id_table),
  265. .acpi_match_table = ACPI_PTR(dw_dma_acpi_id_table),
  266. },
  267. };
  268. static int __init dw_init(void)
  269. {
  270. return platform_driver_register(&dw_driver);
  271. }
  272. subsys_initcall(dw_init);
  273. static void __exit dw_exit(void)
  274. {
  275. platform_driver_unregister(&dw_driver);
  276. }
  277. module_exit(dw_exit);
  278. MODULE_LICENSE("GPL v2");
  279. MODULE_DESCRIPTION("Synopsys DesignWare DMA Controller platform driver");
  280. MODULE_ALIAS("platform:" DRV_NAME);