platform.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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. static struct dma_chan *dw_dma_of_xlate(struct of_phandle_args *dma_spec,
  27. struct of_dma *ofdma)
  28. {
  29. struct dw_dma *dw = ofdma->of_dma_data;
  30. struct dw_dma_slave slave = {
  31. .dma_dev = dw->dma.dev,
  32. };
  33. dma_cap_mask_t cap;
  34. if (dma_spec->args_count != 3)
  35. return NULL;
  36. slave.src_id = dma_spec->args[0];
  37. slave.dst_id = dma_spec->args[0];
  38. slave.src_master = dma_spec->args[1];
  39. slave.dst_master = dma_spec->args[2];
  40. if (WARN_ON(slave.src_id >= DW_DMA_MAX_NR_REQUESTS ||
  41. slave.dst_id >= DW_DMA_MAX_NR_REQUESTS ||
  42. slave.src_master >= dw->nr_masters ||
  43. slave.dst_master >= dw->nr_masters))
  44. return NULL;
  45. dma_cap_zero(cap);
  46. dma_cap_set(DMA_SLAVE, cap);
  47. /* TODO: there should be a simpler way to do this */
  48. return dma_request_channel(cap, dw_dma_filter, &slave);
  49. }
  50. #ifdef CONFIG_ACPI
  51. static bool dw_dma_acpi_filter(struct dma_chan *chan, void *param)
  52. {
  53. struct acpi_dma_spec *dma_spec = param;
  54. struct dw_dma_slave slave = {
  55. .dma_dev = dma_spec->dev,
  56. .src_id = dma_spec->slave_id,
  57. .dst_id = dma_spec->slave_id,
  58. .src_master = 1,
  59. .dst_master = 0,
  60. };
  61. return dw_dma_filter(chan, &slave);
  62. }
  63. static void dw_dma_acpi_controller_register(struct dw_dma *dw)
  64. {
  65. struct device *dev = dw->dma.dev;
  66. struct acpi_dma_filter_info *info;
  67. int ret;
  68. info = devm_kzalloc(dev, sizeof(*info), GFP_KERNEL);
  69. if (!info)
  70. return;
  71. dma_cap_zero(info->dma_cap);
  72. dma_cap_set(DMA_SLAVE, info->dma_cap);
  73. info->filter_fn = dw_dma_acpi_filter;
  74. ret = devm_acpi_dma_controller_register(dev, acpi_dma_simple_xlate,
  75. info);
  76. if (ret)
  77. dev_err(dev, "could not register acpi_dma_controller\n");
  78. }
  79. #else /* !CONFIG_ACPI */
  80. static inline void dw_dma_acpi_controller_register(struct dw_dma *dw) {}
  81. #endif /* !CONFIG_ACPI */
  82. #ifdef CONFIG_OF
  83. static struct dw_dma_platform_data *
  84. dw_dma_parse_dt(struct platform_device *pdev)
  85. {
  86. struct device_node *np = pdev->dev.of_node;
  87. struct dw_dma_platform_data *pdata;
  88. u32 tmp, arr[4];
  89. if (!np) {
  90. dev_err(&pdev->dev, "Missing DT data\n");
  91. return NULL;
  92. }
  93. pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
  94. if (!pdata)
  95. return NULL;
  96. if (of_property_read_u32(np, "dma-channels", &pdata->nr_channels))
  97. return NULL;
  98. if (of_property_read_bool(np, "is_private"))
  99. pdata->is_private = true;
  100. if (!of_property_read_u32(np, "chan_allocation_order", &tmp))
  101. pdata->chan_allocation_order = (unsigned char)tmp;
  102. if (!of_property_read_u32(np, "chan_priority", &tmp))
  103. pdata->chan_priority = tmp;
  104. if (!of_property_read_u32(np, "block_size", &tmp))
  105. pdata->block_size = tmp;
  106. if (!of_property_read_u32(np, "dma-masters", &tmp)) {
  107. if (tmp > 4)
  108. return NULL;
  109. pdata->nr_masters = tmp;
  110. }
  111. if (!of_property_read_u32_array(np, "data_width", arr,
  112. pdata->nr_masters))
  113. for (tmp = 0; tmp < pdata->nr_masters; tmp++)
  114. pdata->data_width[tmp] = arr[tmp];
  115. return pdata;
  116. }
  117. #else
  118. static inline struct dw_dma_platform_data *
  119. dw_dma_parse_dt(struct platform_device *pdev)
  120. {
  121. return NULL;
  122. }
  123. #endif
  124. static int dw_probe(struct platform_device *pdev)
  125. {
  126. struct dw_dma_chip *chip;
  127. struct device *dev = &pdev->dev;
  128. struct resource *mem;
  129. struct dw_dma_platform_data *pdata;
  130. int err;
  131. chip = devm_kzalloc(dev, sizeof(*chip), GFP_KERNEL);
  132. if (!chip)
  133. return -ENOMEM;
  134. chip->irq = platform_get_irq(pdev, 0);
  135. if (chip->irq < 0)
  136. return chip->irq;
  137. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  138. chip->regs = devm_ioremap_resource(dev, mem);
  139. if (IS_ERR(chip->regs))
  140. return PTR_ERR(chip->regs);
  141. err = dma_coerce_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
  142. if (err)
  143. return err;
  144. pdata = dev_get_platdata(dev);
  145. if (!pdata)
  146. pdata = dw_dma_parse_dt(pdev);
  147. chip->dev = dev;
  148. chip->clk = devm_clk_get(chip->dev, "hclk");
  149. if (IS_ERR(chip->clk))
  150. return PTR_ERR(chip->clk);
  151. err = clk_prepare_enable(chip->clk);
  152. if (err)
  153. return err;
  154. pm_runtime_enable(&pdev->dev);
  155. err = dw_dma_probe(chip, pdata);
  156. if (err)
  157. goto err_dw_dma_probe;
  158. platform_set_drvdata(pdev, chip);
  159. if (pdev->dev.of_node) {
  160. err = of_dma_controller_register(pdev->dev.of_node,
  161. dw_dma_of_xlate, chip->dw);
  162. if (err)
  163. dev_err(&pdev->dev,
  164. "could not register of_dma_controller\n");
  165. }
  166. if (ACPI_HANDLE(&pdev->dev))
  167. dw_dma_acpi_controller_register(chip->dw);
  168. return 0;
  169. err_dw_dma_probe:
  170. pm_runtime_disable(&pdev->dev);
  171. clk_disable_unprepare(chip->clk);
  172. return err;
  173. }
  174. static int dw_remove(struct platform_device *pdev)
  175. {
  176. struct dw_dma_chip *chip = platform_get_drvdata(pdev);
  177. if (pdev->dev.of_node)
  178. of_dma_controller_free(pdev->dev.of_node);
  179. dw_dma_remove(chip);
  180. pm_runtime_disable(&pdev->dev);
  181. clk_disable_unprepare(chip->clk);
  182. return 0;
  183. }
  184. static void dw_shutdown(struct platform_device *pdev)
  185. {
  186. struct dw_dma_chip *chip = platform_get_drvdata(pdev);
  187. dw_dma_disable(chip);
  188. clk_disable_unprepare(chip->clk);
  189. }
  190. #ifdef CONFIG_OF
  191. static const struct of_device_id dw_dma_of_id_table[] = {
  192. { .compatible = "snps,dma-spear1340" },
  193. {}
  194. };
  195. MODULE_DEVICE_TABLE(of, dw_dma_of_id_table);
  196. #endif
  197. #ifdef CONFIG_ACPI
  198. static const struct acpi_device_id dw_dma_acpi_id_table[] = {
  199. { "INTL9C60", 0 },
  200. { }
  201. };
  202. MODULE_DEVICE_TABLE(acpi, dw_dma_acpi_id_table);
  203. #endif
  204. #ifdef CONFIG_PM_SLEEP
  205. static int dw_suspend_late(struct device *dev)
  206. {
  207. struct platform_device *pdev = to_platform_device(dev);
  208. struct dw_dma_chip *chip = platform_get_drvdata(pdev);
  209. dw_dma_disable(chip);
  210. clk_disable_unprepare(chip->clk);
  211. return 0;
  212. }
  213. static int dw_resume_early(struct device *dev)
  214. {
  215. struct platform_device *pdev = to_platform_device(dev);
  216. struct dw_dma_chip *chip = platform_get_drvdata(pdev);
  217. clk_prepare_enable(chip->clk);
  218. return dw_dma_enable(chip);
  219. }
  220. #endif /* CONFIG_PM_SLEEP */
  221. static const struct dev_pm_ops dw_dev_pm_ops = {
  222. SET_LATE_SYSTEM_SLEEP_PM_OPS(dw_suspend_late, dw_resume_early)
  223. };
  224. static struct platform_driver dw_driver = {
  225. .probe = dw_probe,
  226. .remove = dw_remove,
  227. .shutdown = dw_shutdown,
  228. .driver = {
  229. .name = "dw_dmac",
  230. .pm = &dw_dev_pm_ops,
  231. .of_match_table = of_match_ptr(dw_dma_of_id_table),
  232. .acpi_match_table = ACPI_PTR(dw_dma_acpi_id_table),
  233. },
  234. };
  235. static int __init dw_init(void)
  236. {
  237. return platform_driver_register(&dw_driver);
  238. }
  239. subsys_initcall(dw_init);
  240. static void __exit dw_exit(void)
  241. {
  242. platform_driver_unregister(&dw_driver);
  243. }
  244. module_exit(dw_exit);
  245. MODULE_LICENSE("GPL v2");
  246. MODULE_DESCRIPTION("Synopsys DesignWare DMA Controller platform driver");