lpc18xx-dmamux.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /*
  2. * DMA Router driver for LPC18xx/43xx DMA MUX
  3. *
  4. * Copyright (C) 2015 Joachim Eastwood <manabian@gmail.com>
  5. *
  6. * Based on TI DMA Crossbar driver by:
  7. * Copyright (C) 2015 Texas Instruments Incorporated - http://www.ti.com
  8. * Author: Peter Ujfalusi <peter.ujfalusi@ti.com>
  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. */
  15. #include <linux/err.h>
  16. #include <linux/init.h>
  17. #include <linux/mfd/syscon.h>
  18. #include <linux/of_device.h>
  19. #include <linux/of_dma.h>
  20. #include <linux/regmap.h>
  21. #include <linux/spinlock.h>
  22. /* CREG register offset and macros for mux manipulation */
  23. #define LPC18XX_CREG_DMAMUX 0x11c
  24. #define LPC18XX_DMAMUX_VAL(v, n) ((v) << (n * 2))
  25. #define LPC18XX_DMAMUX_MASK(n) (0x3 << (n * 2))
  26. #define LPC18XX_DMAMUX_MAX_VAL 0x3
  27. struct lpc18xx_dmamux {
  28. u32 value;
  29. bool busy;
  30. };
  31. struct lpc18xx_dmamux_data {
  32. struct dma_router dmarouter;
  33. struct lpc18xx_dmamux *muxes;
  34. u32 dma_master_requests;
  35. u32 dma_mux_requests;
  36. struct regmap *reg;
  37. spinlock_t lock;
  38. };
  39. static void lpc18xx_dmamux_free(struct device *dev, void *route_data)
  40. {
  41. struct lpc18xx_dmamux_data *dmamux = dev_get_drvdata(dev);
  42. struct lpc18xx_dmamux *mux = route_data;
  43. unsigned long flags;
  44. spin_lock_irqsave(&dmamux->lock, flags);
  45. mux->busy = false;
  46. spin_unlock_irqrestore(&dmamux->lock, flags);
  47. }
  48. static void *lpc18xx_dmamux_reserve(struct of_phandle_args *dma_spec,
  49. struct of_dma *ofdma)
  50. {
  51. struct platform_device *pdev = of_find_device_by_node(ofdma->of_node);
  52. struct lpc18xx_dmamux_data *dmamux = platform_get_drvdata(pdev);
  53. unsigned long flags;
  54. unsigned mux;
  55. if (dma_spec->args_count != 3) {
  56. dev_err(&pdev->dev, "invalid number of dma mux args\n");
  57. return ERR_PTR(-EINVAL);
  58. }
  59. mux = dma_spec->args[0];
  60. if (mux >= dmamux->dma_master_requests) {
  61. dev_err(&pdev->dev, "invalid mux number: %d\n",
  62. dma_spec->args[0]);
  63. return ERR_PTR(-EINVAL);
  64. }
  65. if (dma_spec->args[1] > LPC18XX_DMAMUX_MAX_VAL) {
  66. dev_err(&pdev->dev, "invalid dma mux value: %d\n",
  67. dma_spec->args[1]);
  68. return ERR_PTR(-EINVAL);
  69. }
  70. /* The of_node_put() will be done in the core for the node */
  71. dma_spec->np = of_parse_phandle(ofdma->of_node, "dma-masters", 0);
  72. if (!dma_spec->np) {
  73. dev_err(&pdev->dev, "can't get dma master\n");
  74. return ERR_PTR(-EINVAL);
  75. }
  76. spin_lock_irqsave(&dmamux->lock, flags);
  77. if (dmamux->muxes[mux].busy) {
  78. spin_unlock_irqrestore(&dmamux->lock, flags);
  79. dev_err(&pdev->dev, "dma request %u busy with %u.%u\n",
  80. mux, mux, dmamux->muxes[mux].value);
  81. of_node_put(dma_spec->np);
  82. return ERR_PTR(-EBUSY);
  83. }
  84. dmamux->muxes[mux].busy = true;
  85. dmamux->muxes[mux].value = dma_spec->args[1];
  86. regmap_update_bits(dmamux->reg, LPC18XX_CREG_DMAMUX,
  87. LPC18XX_DMAMUX_MASK(mux),
  88. LPC18XX_DMAMUX_VAL(dmamux->muxes[mux].value, mux));
  89. spin_unlock_irqrestore(&dmamux->lock, flags);
  90. dma_spec->args[1] = dma_spec->args[2];
  91. dma_spec->args_count = 2;
  92. dev_dbg(&pdev->dev, "mapping dmamux %u.%u to dma request %u\n", mux,
  93. dmamux->muxes[mux].value, mux);
  94. return &dmamux->muxes[mux];
  95. }
  96. static int lpc18xx_dmamux_probe(struct platform_device *pdev)
  97. {
  98. struct device_node *dma_np, *np = pdev->dev.of_node;
  99. struct lpc18xx_dmamux_data *dmamux;
  100. int ret;
  101. dmamux = devm_kzalloc(&pdev->dev, sizeof(*dmamux), GFP_KERNEL);
  102. if (!dmamux)
  103. return -ENOMEM;
  104. dmamux->reg = syscon_regmap_lookup_by_compatible("nxp,lpc1850-creg");
  105. if (IS_ERR(dmamux->reg)) {
  106. dev_err(&pdev->dev, "syscon lookup failed\n");
  107. return PTR_ERR(dmamux->reg);
  108. }
  109. ret = of_property_read_u32(np, "dma-requests",
  110. &dmamux->dma_mux_requests);
  111. if (ret) {
  112. dev_err(&pdev->dev, "missing dma-requests property\n");
  113. return ret;
  114. }
  115. dma_np = of_parse_phandle(np, "dma-masters", 0);
  116. if (!dma_np) {
  117. dev_err(&pdev->dev, "can't get dma master\n");
  118. return -ENODEV;
  119. }
  120. ret = of_property_read_u32(dma_np, "dma-requests",
  121. &dmamux->dma_master_requests);
  122. of_node_put(dma_np);
  123. if (ret) {
  124. dev_err(&pdev->dev, "missing master dma-requests property\n");
  125. return ret;
  126. }
  127. dmamux->muxes = devm_kcalloc(&pdev->dev, dmamux->dma_master_requests,
  128. sizeof(struct lpc18xx_dmamux),
  129. GFP_KERNEL);
  130. if (!dmamux->muxes)
  131. return -ENOMEM;
  132. spin_lock_init(&dmamux->lock);
  133. platform_set_drvdata(pdev, dmamux);
  134. dmamux->dmarouter.dev = &pdev->dev;
  135. dmamux->dmarouter.route_free = lpc18xx_dmamux_free;
  136. return of_dma_router_register(np, lpc18xx_dmamux_reserve,
  137. &dmamux->dmarouter);
  138. }
  139. static const struct of_device_id lpc18xx_dmamux_match[] = {
  140. { .compatible = "nxp,lpc1850-dmamux" },
  141. {},
  142. };
  143. static struct platform_driver lpc18xx_dmamux_driver = {
  144. .probe = lpc18xx_dmamux_probe,
  145. .driver = {
  146. .name = "lpc18xx-dmamux",
  147. .of_match_table = lpc18xx_dmamux_match,
  148. },
  149. };
  150. static int __init lpc18xx_dmamux_init(void)
  151. {
  152. return platform_driver_register(&lpc18xx_dmamux_driver);
  153. }
  154. arch_initcall(lpc18xx_dmamux_init);