platform.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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/platform_device.h>
  18. #include <linux/dmaengine.h>
  19. #include <linux/dma-mapping.h>
  20. #include <linux/of.h>
  21. #include <linux/of_dma.h>
  22. #include <linux/acpi.h>
  23. #include <linux/acpi_dma.h>
  24. #include "internal.h"
  25. #define DRV_NAME "dw_dmac"
  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. err = dw_dma_probe(chip, pdata);
  155. if (err)
  156. goto err_dw_dma_probe;
  157. platform_set_drvdata(pdev, chip);
  158. if (pdev->dev.of_node) {
  159. err = of_dma_controller_register(pdev->dev.of_node,
  160. dw_dma_of_xlate, chip->dw);
  161. if (err)
  162. dev_err(&pdev->dev,
  163. "could not register of_dma_controller\n");
  164. }
  165. if (ACPI_HANDLE(&pdev->dev))
  166. dw_dma_acpi_controller_register(chip->dw);
  167. return 0;
  168. err_dw_dma_probe:
  169. clk_disable_unprepare(chip->clk);
  170. return err;
  171. }
  172. static int dw_remove(struct platform_device *pdev)
  173. {
  174. struct dw_dma_chip *chip = platform_get_drvdata(pdev);
  175. if (pdev->dev.of_node)
  176. of_dma_controller_free(pdev->dev.of_node);
  177. dw_dma_remove(chip);
  178. clk_disable_unprepare(chip->clk);
  179. return 0;
  180. }
  181. static void dw_shutdown(struct platform_device *pdev)
  182. {
  183. struct dw_dma_chip *chip = platform_get_drvdata(pdev);
  184. dw_dma_disable(chip);
  185. clk_disable_unprepare(chip->clk);
  186. }
  187. #ifdef CONFIG_OF
  188. static const struct of_device_id dw_dma_of_id_table[] = {
  189. { .compatible = "snps,dma-spear1340" },
  190. {}
  191. };
  192. MODULE_DEVICE_TABLE(of, dw_dma_of_id_table);
  193. #endif
  194. #ifdef CONFIG_ACPI
  195. static const struct acpi_device_id dw_dma_acpi_id_table[] = {
  196. { "INTL9C60", 0 },
  197. { }
  198. };
  199. MODULE_DEVICE_TABLE(acpi, dw_dma_acpi_id_table);
  200. #endif
  201. #ifdef CONFIG_PM_SLEEP
  202. static int dw_suspend_late(struct device *dev)
  203. {
  204. struct platform_device *pdev = to_platform_device(dev);
  205. struct dw_dma_chip *chip = platform_get_drvdata(pdev);
  206. dw_dma_disable(chip);
  207. clk_disable_unprepare(chip->clk);
  208. return 0;
  209. }
  210. static int dw_resume_early(struct device *dev)
  211. {
  212. struct platform_device *pdev = to_platform_device(dev);
  213. struct dw_dma_chip *chip = platform_get_drvdata(pdev);
  214. clk_prepare_enable(chip->clk);
  215. return dw_dma_enable(chip);
  216. }
  217. #endif /* CONFIG_PM_SLEEP */
  218. static const struct dev_pm_ops dw_dev_pm_ops = {
  219. SET_LATE_SYSTEM_SLEEP_PM_OPS(dw_suspend_late, dw_resume_early)
  220. };
  221. static struct platform_driver dw_driver = {
  222. .probe = dw_probe,
  223. .remove = dw_remove,
  224. .shutdown = dw_shutdown,
  225. .driver = {
  226. .name = DRV_NAME,
  227. .pm = &dw_dev_pm_ops,
  228. .of_match_table = of_match_ptr(dw_dma_of_id_table),
  229. .acpi_match_table = ACPI_PTR(dw_dma_acpi_id_table),
  230. },
  231. };
  232. static int __init dw_init(void)
  233. {
  234. return platform_driver_register(&dw_driver);
  235. }
  236. subsys_initcall(dw_init);
  237. static void __exit dw_exit(void)
  238. {
  239. platform_driver_unregister(&dw_driver);
  240. }
  241. module_exit(dw_exit);
  242. MODULE_LICENSE("GPL v2");
  243. MODULE_DESCRIPTION("Synopsys DesignWare DMA Controller platform driver");
  244. MODULE_ALIAS("platform:" DRV_NAME);