hbmc-am654.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. // SPDX-License-Identifier: GPL-2.0
  2. //
  3. // Copyright (C) 2019 Texas Instruments Incorporated - https://www.ti.com/
  4. // Author: Vignesh Raghavendra <vigneshr@ti.com>
  5. #include <linux/completion.h>
  6. #include <linux/dma-direction.h>
  7. #include <linux/dma-mapping.h>
  8. #include <linux/dmaengine.h>
  9. #include <linux/err.h>
  10. #include <linux/kernel.h>
  11. #include <linux/module.h>
  12. #include <linux/mtd/cfi.h>
  13. #include <linux/mtd/hyperbus.h>
  14. #include <linux/mtd/mtd.h>
  15. #include <linux/mux/consumer.h>
  16. #include <linux/of.h>
  17. #include <linux/of_address.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/sched/task_stack.h>
  20. #include <linux/types.h>
  21. #define AM654_HBMC_CALIB_COUNT 25
  22. struct am654_hbmc_device_priv {
  23. struct completion rx_dma_complete;
  24. phys_addr_t device_base;
  25. struct hyperbus_ctlr *ctlr;
  26. struct dma_chan *rx_chan;
  27. };
  28. struct am654_hbmc_priv {
  29. struct hyperbus_ctlr ctlr;
  30. struct hyperbus_device hbdev;
  31. struct mux_control *mux_ctrl;
  32. };
  33. static int am654_hbmc_calibrate(struct hyperbus_device *hbdev)
  34. {
  35. struct map_info *map = &hbdev->map;
  36. struct cfi_private cfi;
  37. int count = AM654_HBMC_CALIB_COUNT;
  38. int pass_count = 0;
  39. int ret;
  40. cfi.interleave = 1;
  41. cfi.device_type = CFI_DEVICETYPE_X16;
  42. cfi_send_gen_cmd(0xF0, 0, 0, map, &cfi, cfi.device_type, NULL);
  43. cfi_send_gen_cmd(0x98, 0x55, 0, map, &cfi, cfi.device_type, NULL);
  44. while (count--) {
  45. ret = cfi_qry_present(map, 0, &cfi);
  46. if (ret)
  47. pass_count++;
  48. else
  49. pass_count = 0;
  50. if (pass_count == 5)
  51. break;
  52. }
  53. cfi_qry_mode_off(0, map, &cfi);
  54. return ret;
  55. }
  56. static void am654_hbmc_dma_callback(void *param)
  57. {
  58. struct am654_hbmc_device_priv *priv = param;
  59. complete(&priv->rx_dma_complete);
  60. }
  61. static int am654_hbmc_dma_read(struct am654_hbmc_device_priv *priv, void *to,
  62. unsigned long from, ssize_t len)
  63. {
  64. enum dma_ctrl_flags flags = DMA_CTRL_ACK | DMA_PREP_INTERRUPT;
  65. struct dma_chan *rx_chan = priv->rx_chan;
  66. struct dma_async_tx_descriptor *tx;
  67. dma_addr_t dma_dst, dma_src;
  68. dma_cookie_t cookie;
  69. int ret;
  70. if (!priv->rx_chan || !virt_addr_valid(to) || object_is_on_stack(to))
  71. return -EINVAL;
  72. dma_dst = dma_map_single(rx_chan->device->dev, to, len, DMA_FROM_DEVICE);
  73. if (dma_mapping_error(rx_chan->device->dev, dma_dst)) {
  74. dev_dbg(priv->ctlr->dev, "DMA mapping failed\n");
  75. return -EIO;
  76. }
  77. dma_src = priv->device_base + from;
  78. tx = dmaengine_prep_dma_memcpy(rx_chan, dma_dst, dma_src, len, flags);
  79. if (!tx) {
  80. dev_err(priv->ctlr->dev, "device_prep_dma_memcpy error\n");
  81. ret = -EIO;
  82. goto unmap_dma;
  83. }
  84. reinit_completion(&priv->rx_dma_complete);
  85. tx->callback = am654_hbmc_dma_callback;
  86. tx->callback_param = priv;
  87. cookie = dmaengine_submit(tx);
  88. ret = dma_submit_error(cookie);
  89. if (ret) {
  90. dev_err(priv->ctlr->dev, "dma_submit_error %d\n", cookie);
  91. goto unmap_dma;
  92. }
  93. dma_async_issue_pending(rx_chan);
  94. if (!wait_for_completion_timeout(&priv->rx_dma_complete, msecs_to_jiffies(len + 1000))) {
  95. dmaengine_terminate_sync(rx_chan);
  96. dev_err(priv->ctlr->dev, "DMA wait_for_completion_timeout\n");
  97. ret = -ETIMEDOUT;
  98. }
  99. unmap_dma:
  100. dma_unmap_single(rx_chan->device->dev, dma_dst, len, DMA_FROM_DEVICE);
  101. return ret;
  102. }
  103. static void am654_hbmc_read(struct hyperbus_device *hbdev, void *to,
  104. unsigned long from, ssize_t len)
  105. {
  106. struct am654_hbmc_device_priv *priv = hbdev->priv;
  107. if (len < SZ_1K || am654_hbmc_dma_read(priv, to, from, len))
  108. memcpy_fromio(to, hbdev->map.virt + from, len);
  109. }
  110. static const struct hyperbus_ops am654_hbmc_ops = {
  111. .calibrate = am654_hbmc_calibrate,
  112. .copy_from = am654_hbmc_read,
  113. };
  114. static int am654_hbmc_request_mmap_dma(struct am654_hbmc_device_priv *priv)
  115. {
  116. struct dma_chan *rx_chan;
  117. dma_cap_mask_t mask;
  118. dma_cap_zero(mask);
  119. dma_cap_set(DMA_MEMCPY, mask);
  120. rx_chan = dma_request_chan_by_mask(&mask);
  121. if (IS_ERR(rx_chan)) {
  122. if (PTR_ERR(rx_chan) == -EPROBE_DEFER)
  123. return -EPROBE_DEFER;
  124. dev_dbg(priv->ctlr->dev, "No DMA channel available\n");
  125. return 0;
  126. }
  127. priv->rx_chan = rx_chan;
  128. init_completion(&priv->rx_dma_complete);
  129. return 0;
  130. }
  131. static int am654_hbmc_probe(struct platform_device *pdev)
  132. {
  133. struct device_node *np = pdev->dev.of_node;
  134. struct am654_hbmc_device_priv *dev_priv;
  135. struct device *dev = &pdev->dev;
  136. struct am654_hbmc_priv *priv;
  137. struct resource res;
  138. int ret;
  139. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  140. if (!priv)
  141. return -ENOMEM;
  142. platform_set_drvdata(pdev, priv);
  143. priv->hbdev.np = of_get_next_child(np, NULL);
  144. ret = of_address_to_resource(priv->hbdev.np, 0, &res);
  145. if (ret)
  146. return ret;
  147. if (of_property_read_bool(dev->of_node, "mux-controls")) {
  148. struct mux_control *control = devm_mux_control_get(dev, NULL);
  149. if (IS_ERR(control))
  150. return PTR_ERR(control);
  151. ret = mux_control_select(control, 1);
  152. if (ret) {
  153. dev_err(dev, "Failed to select HBMC mux\n");
  154. return ret;
  155. }
  156. priv->mux_ctrl = control;
  157. }
  158. priv->hbdev.map.size = resource_size(&res);
  159. priv->hbdev.map.virt = devm_ioremap_resource(dev, &res);
  160. if (IS_ERR(priv->hbdev.map.virt))
  161. return PTR_ERR(priv->hbdev.map.virt);
  162. priv->ctlr.dev = dev;
  163. priv->ctlr.ops = &am654_hbmc_ops;
  164. priv->hbdev.ctlr = &priv->ctlr;
  165. dev_priv = devm_kzalloc(dev, sizeof(*dev_priv), GFP_KERNEL);
  166. if (!dev_priv) {
  167. ret = -ENOMEM;
  168. goto disable_mux;
  169. }
  170. priv->hbdev.priv = dev_priv;
  171. dev_priv->device_base = res.start;
  172. dev_priv->ctlr = &priv->ctlr;
  173. ret = am654_hbmc_request_mmap_dma(dev_priv);
  174. if (ret)
  175. goto disable_mux;
  176. ret = hyperbus_register_device(&priv->hbdev);
  177. if (ret) {
  178. dev_err(dev, "failed to register controller\n");
  179. goto release_dma;
  180. }
  181. return 0;
  182. release_dma:
  183. if (dev_priv->rx_chan)
  184. dma_release_channel(dev_priv->rx_chan);
  185. disable_mux:
  186. if (priv->mux_ctrl)
  187. mux_control_deselect(priv->mux_ctrl);
  188. return ret;
  189. }
  190. static int am654_hbmc_remove(struct platform_device *pdev)
  191. {
  192. struct am654_hbmc_priv *priv = platform_get_drvdata(pdev);
  193. struct am654_hbmc_device_priv *dev_priv = priv->hbdev.priv;
  194. int ret;
  195. ret = hyperbus_unregister_device(&priv->hbdev);
  196. if (priv->mux_ctrl)
  197. mux_control_deselect(priv->mux_ctrl);
  198. if (dev_priv->rx_chan)
  199. dma_release_channel(dev_priv->rx_chan);
  200. return ret;
  201. }
  202. static const struct of_device_id am654_hbmc_dt_ids[] = {
  203. {
  204. .compatible = "ti,am654-hbmc",
  205. },
  206. { /* end of table */ }
  207. };
  208. MODULE_DEVICE_TABLE(of, am654_hbmc_dt_ids);
  209. static struct platform_driver am654_hbmc_platform_driver = {
  210. .probe = am654_hbmc_probe,
  211. .remove = am654_hbmc_remove,
  212. .driver = {
  213. .name = "hbmc-am654",
  214. .of_match_table = am654_hbmc_dt_ids,
  215. },
  216. };
  217. module_platform_driver(am654_hbmc_platform_driver);
  218. MODULE_DESCRIPTION("HBMC driver for AM654 SoC");
  219. MODULE_LICENSE("GPL v2");
  220. MODULE_ALIAS("platform:hbmc-am654");
  221. MODULE_AUTHOR("Vignesh Raghavendra <vigneshr@ti.com>");