hbmc-am654.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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. goto put_node;
  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. ret = PTR_ERR(control);
  151. goto put_node;
  152. }
  153. ret = mux_control_select(control, 1);
  154. if (ret) {
  155. dev_err(dev, "Failed to select HBMC mux\n");
  156. goto put_node;
  157. }
  158. priv->mux_ctrl = control;
  159. }
  160. priv->hbdev.map.size = resource_size(&res);
  161. priv->hbdev.map.virt = devm_ioremap_resource(dev, &res);
  162. if (IS_ERR(priv->hbdev.map.virt)) {
  163. ret = PTR_ERR(priv->hbdev.map.virt);
  164. goto disable_mux;
  165. }
  166. priv->ctlr.dev = dev;
  167. priv->ctlr.ops = &am654_hbmc_ops;
  168. priv->hbdev.ctlr = &priv->ctlr;
  169. dev_priv = devm_kzalloc(dev, sizeof(*dev_priv), GFP_KERNEL);
  170. if (!dev_priv) {
  171. ret = -ENOMEM;
  172. goto disable_mux;
  173. }
  174. priv->hbdev.priv = dev_priv;
  175. dev_priv->device_base = res.start;
  176. dev_priv->ctlr = &priv->ctlr;
  177. ret = am654_hbmc_request_mmap_dma(dev_priv);
  178. if (ret)
  179. goto disable_mux;
  180. ret = hyperbus_register_device(&priv->hbdev);
  181. if (ret) {
  182. dev_err(dev, "failed to register controller\n");
  183. goto release_dma;
  184. }
  185. return 0;
  186. release_dma:
  187. if (dev_priv->rx_chan)
  188. dma_release_channel(dev_priv->rx_chan);
  189. disable_mux:
  190. if (priv->mux_ctrl)
  191. mux_control_deselect(priv->mux_ctrl);
  192. put_node:
  193. of_node_put(priv->hbdev.np);
  194. return ret;
  195. }
  196. static void am654_hbmc_remove(struct platform_device *pdev)
  197. {
  198. struct am654_hbmc_priv *priv = platform_get_drvdata(pdev);
  199. struct am654_hbmc_device_priv *dev_priv = priv->hbdev.priv;
  200. hyperbus_unregister_device(&priv->hbdev);
  201. if (priv->mux_ctrl)
  202. mux_control_deselect(priv->mux_ctrl);
  203. if (dev_priv->rx_chan)
  204. dma_release_channel(dev_priv->rx_chan);
  205. of_node_put(priv->hbdev.np);
  206. }
  207. static const struct of_device_id am654_hbmc_dt_ids[] = {
  208. {
  209. .compatible = "ti,am654-hbmc",
  210. },
  211. { /* end of table */ }
  212. };
  213. MODULE_DEVICE_TABLE(of, am654_hbmc_dt_ids);
  214. static struct platform_driver am654_hbmc_platform_driver = {
  215. .probe = am654_hbmc_probe,
  216. .remove_new = am654_hbmc_remove,
  217. .driver = {
  218. .name = "hbmc-am654",
  219. .of_match_table = am654_hbmc_dt_ids,
  220. },
  221. };
  222. module_platform_driver(am654_hbmc_platform_driver);
  223. MODULE_DESCRIPTION("HBMC driver for AM654 SoC");
  224. MODULE_LICENSE("GPL v2");
  225. MODULE_ALIAS("platform:hbmc-am654");
  226. MODULE_AUTHOR("Vignesh Raghavendra <vigneshr@ti.com>");