ti-dma-crossbar.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. /*
  2. * Copyright (C) 2015 Texas Instruments Incorporated - http://www.ti.com
  3. * Author: Peter Ujfalusi <peter.ujfalusi@ti.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. *
  9. */
  10. #include <linux/slab.h>
  11. #include <linux/err.h>
  12. #include <linux/init.h>
  13. #include <linux/list.h>
  14. #include <linux/io.h>
  15. #include <linux/of_address.h>
  16. #include <linux/of_device.h>
  17. #include <linux/of_dma.h>
  18. #define TI_XBAR_DRA7 0
  19. #define TI_XBAR_AM335X 1
  20. static const struct of_device_id ti_dma_xbar_match[] = {
  21. {
  22. .compatible = "ti,dra7-dma-crossbar",
  23. .data = (void *)TI_XBAR_DRA7,
  24. },
  25. {
  26. .compatible = "ti,am335x-edma-crossbar",
  27. .data = (void *)TI_XBAR_AM335X,
  28. },
  29. {},
  30. };
  31. /* Crossbar on AM335x/AM437x family */
  32. #define TI_AM335X_XBAR_LINES 64
  33. struct ti_am335x_xbar_data {
  34. void __iomem *iomem;
  35. struct dma_router dmarouter;
  36. u32 xbar_events; /* maximum number of events to select in xbar */
  37. u32 dma_requests; /* number of DMA requests on eDMA */
  38. };
  39. struct ti_am335x_xbar_map {
  40. u16 dma_line;
  41. u16 mux_val;
  42. };
  43. static inline void ti_am335x_xbar_write(void __iomem *iomem, int event, u16 val)
  44. {
  45. writeb_relaxed(val & 0x1f, iomem + event);
  46. }
  47. static void ti_am335x_xbar_free(struct device *dev, void *route_data)
  48. {
  49. struct ti_am335x_xbar_data *xbar = dev_get_drvdata(dev);
  50. struct ti_am335x_xbar_map *map = route_data;
  51. dev_dbg(dev, "Unmapping XBAR event %u on channel %u\n",
  52. map->mux_val, map->dma_line);
  53. ti_am335x_xbar_write(xbar->iomem, map->dma_line, 0);
  54. kfree(map);
  55. }
  56. static void *ti_am335x_xbar_route_allocate(struct of_phandle_args *dma_spec,
  57. struct of_dma *ofdma)
  58. {
  59. struct platform_device *pdev = of_find_device_by_node(ofdma->of_node);
  60. struct ti_am335x_xbar_data *xbar = platform_get_drvdata(pdev);
  61. struct ti_am335x_xbar_map *map;
  62. if (dma_spec->args_count != 3)
  63. return ERR_PTR(-EINVAL);
  64. if (dma_spec->args[2] >= xbar->xbar_events) {
  65. dev_err(&pdev->dev, "Invalid XBAR event number: %d\n",
  66. dma_spec->args[2]);
  67. return ERR_PTR(-EINVAL);
  68. }
  69. if (dma_spec->args[0] >= xbar->dma_requests) {
  70. dev_err(&pdev->dev, "Invalid DMA request line number: %d\n",
  71. dma_spec->args[0]);
  72. return ERR_PTR(-EINVAL);
  73. }
  74. /* The of_node_put() will be done in the core for the node */
  75. dma_spec->np = of_parse_phandle(ofdma->of_node, "dma-masters", 0);
  76. if (!dma_spec->np) {
  77. dev_err(&pdev->dev, "Can't get DMA master\n");
  78. return ERR_PTR(-EINVAL);
  79. }
  80. map = kzalloc(sizeof(*map), GFP_KERNEL);
  81. if (!map) {
  82. of_node_put(dma_spec->np);
  83. return ERR_PTR(-ENOMEM);
  84. }
  85. map->dma_line = (u16)dma_spec->args[0];
  86. map->mux_val = (u16)dma_spec->args[2];
  87. dma_spec->args[2] = 0;
  88. dma_spec->args_count = 2;
  89. dev_dbg(&pdev->dev, "Mapping XBAR event%u to DMA%u\n",
  90. map->mux_val, map->dma_line);
  91. ti_am335x_xbar_write(xbar->iomem, map->dma_line, map->mux_val);
  92. return map;
  93. }
  94. static const struct of_device_id ti_am335x_master_match[] = {
  95. { .compatible = "ti,edma3-tpcc", },
  96. {},
  97. };
  98. static int ti_am335x_xbar_probe(struct platform_device *pdev)
  99. {
  100. struct device_node *node = pdev->dev.of_node;
  101. const struct of_device_id *match;
  102. struct device_node *dma_node;
  103. struct ti_am335x_xbar_data *xbar;
  104. struct resource *res;
  105. void __iomem *iomem;
  106. int i, ret;
  107. if (!node)
  108. return -ENODEV;
  109. xbar = devm_kzalloc(&pdev->dev, sizeof(*xbar), GFP_KERNEL);
  110. if (!xbar)
  111. return -ENOMEM;
  112. dma_node = of_parse_phandle(node, "dma-masters", 0);
  113. if (!dma_node) {
  114. dev_err(&pdev->dev, "Can't get DMA master node\n");
  115. return -ENODEV;
  116. }
  117. match = of_match_node(ti_am335x_master_match, dma_node);
  118. if (!match) {
  119. dev_err(&pdev->dev, "DMA master is not supported\n");
  120. return -EINVAL;
  121. }
  122. if (of_property_read_u32(dma_node, "dma-requests",
  123. &xbar->dma_requests)) {
  124. dev_info(&pdev->dev,
  125. "Missing XBAR output information, using %u.\n",
  126. TI_AM335X_XBAR_LINES);
  127. xbar->dma_requests = TI_AM335X_XBAR_LINES;
  128. }
  129. of_node_put(dma_node);
  130. if (of_property_read_u32(node, "dma-requests", &xbar->xbar_events)) {
  131. dev_info(&pdev->dev,
  132. "Missing XBAR input information, using %u.\n",
  133. TI_AM335X_XBAR_LINES);
  134. xbar->xbar_events = TI_AM335X_XBAR_LINES;
  135. }
  136. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  137. iomem = devm_ioremap_resource(&pdev->dev, res);
  138. if (IS_ERR(iomem))
  139. return PTR_ERR(iomem);
  140. xbar->iomem = iomem;
  141. xbar->dmarouter.dev = &pdev->dev;
  142. xbar->dmarouter.route_free = ti_am335x_xbar_free;
  143. platform_set_drvdata(pdev, xbar);
  144. /* Reset the crossbar */
  145. for (i = 0; i < xbar->dma_requests; i++)
  146. ti_am335x_xbar_write(xbar->iomem, i, 0);
  147. ret = of_dma_router_register(node, ti_am335x_xbar_route_allocate,
  148. &xbar->dmarouter);
  149. return ret;
  150. }
  151. /* Crossbar on DRA7xx family */
  152. #define TI_DRA7_XBAR_OUTPUTS 127
  153. #define TI_DRA7_XBAR_INPUTS 256
  154. #define TI_XBAR_EDMA_OFFSET 0
  155. #define TI_XBAR_SDMA_OFFSET 1
  156. struct ti_dra7_xbar_data {
  157. void __iomem *iomem;
  158. struct dma_router dmarouter;
  159. struct mutex mutex;
  160. unsigned long *dma_inuse;
  161. u16 safe_val; /* Value to rest the crossbar lines */
  162. u32 xbar_requests; /* number of DMA requests connected to XBAR */
  163. u32 dma_requests; /* number of DMA requests forwarded to DMA */
  164. u32 dma_offset;
  165. };
  166. struct ti_dra7_xbar_map {
  167. u16 xbar_in;
  168. int xbar_out;
  169. };
  170. static inline void ti_dra7_xbar_write(void __iomem *iomem, int xbar, u16 val)
  171. {
  172. writew_relaxed(val, iomem + (xbar * 2));
  173. }
  174. static void ti_dra7_xbar_free(struct device *dev, void *route_data)
  175. {
  176. struct ti_dra7_xbar_data *xbar = dev_get_drvdata(dev);
  177. struct ti_dra7_xbar_map *map = route_data;
  178. dev_dbg(dev, "Unmapping XBAR%u (was routed to %d)\n",
  179. map->xbar_in, map->xbar_out);
  180. ti_dra7_xbar_write(xbar->iomem, map->xbar_out, xbar->safe_val);
  181. mutex_lock(&xbar->mutex);
  182. clear_bit(map->xbar_out, xbar->dma_inuse);
  183. mutex_unlock(&xbar->mutex);
  184. kfree(map);
  185. }
  186. static void *ti_dra7_xbar_route_allocate(struct of_phandle_args *dma_spec,
  187. struct of_dma *ofdma)
  188. {
  189. struct platform_device *pdev = of_find_device_by_node(ofdma->of_node);
  190. struct ti_dra7_xbar_data *xbar = platform_get_drvdata(pdev);
  191. struct ti_dra7_xbar_map *map;
  192. if (dma_spec->args[0] >= xbar->xbar_requests) {
  193. dev_err(&pdev->dev, "Invalid XBAR request number: %d\n",
  194. dma_spec->args[0]);
  195. return ERR_PTR(-EINVAL);
  196. }
  197. /* The of_node_put() will be done in the core for the node */
  198. dma_spec->np = of_parse_phandle(ofdma->of_node, "dma-masters", 0);
  199. if (!dma_spec->np) {
  200. dev_err(&pdev->dev, "Can't get DMA master\n");
  201. return ERR_PTR(-EINVAL);
  202. }
  203. map = kzalloc(sizeof(*map), GFP_KERNEL);
  204. if (!map) {
  205. of_node_put(dma_spec->np);
  206. return ERR_PTR(-ENOMEM);
  207. }
  208. mutex_lock(&xbar->mutex);
  209. map->xbar_out = find_first_zero_bit(xbar->dma_inuse,
  210. xbar->dma_requests);
  211. mutex_unlock(&xbar->mutex);
  212. if (map->xbar_out == xbar->dma_requests) {
  213. dev_err(&pdev->dev, "Run out of free DMA requests\n");
  214. kfree(map);
  215. return ERR_PTR(-ENOMEM);
  216. }
  217. set_bit(map->xbar_out, xbar->dma_inuse);
  218. map->xbar_in = (u16)dma_spec->args[0];
  219. dma_spec->args[0] = map->xbar_out + xbar->dma_offset;
  220. dev_dbg(&pdev->dev, "Mapping XBAR%u to DMA%d\n",
  221. map->xbar_in, map->xbar_out);
  222. ti_dra7_xbar_write(xbar->iomem, map->xbar_out, map->xbar_in);
  223. return map;
  224. }
  225. static const struct of_device_id ti_dra7_master_match[] = {
  226. {
  227. .compatible = "ti,omap4430-sdma",
  228. .data = (void *)TI_XBAR_SDMA_OFFSET,
  229. },
  230. {
  231. .compatible = "ti,edma3",
  232. .data = (void *)TI_XBAR_EDMA_OFFSET,
  233. },
  234. {
  235. .compatible = "ti,edma3-tpcc",
  236. .data = (void *)TI_XBAR_EDMA_OFFSET,
  237. },
  238. {},
  239. };
  240. static inline void ti_dra7_xbar_reserve(int offset, int len, unsigned long *p)
  241. {
  242. for (; len > 0; len--)
  243. clear_bit(offset + (len - 1), p);
  244. }
  245. static int ti_dra7_xbar_probe(struct platform_device *pdev)
  246. {
  247. struct device_node *node = pdev->dev.of_node;
  248. const struct of_device_id *match;
  249. struct device_node *dma_node;
  250. struct ti_dra7_xbar_data *xbar;
  251. struct property *prop;
  252. struct resource *res;
  253. u32 safe_val;
  254. size_t sz;
  255. void __iomem *iomem;
  256. int i, ret;
  257. if (!node)
  258. return -ENODEV;
  259. xbar = devm_kzalloc(&pdev->dev, sizeof(*xbar), GFP_KERNEL);
  260. if (!xbar)
  261. return -ENOMEM;
  262. dma_node = of_parse_phandle(node, "dma-masters", 0);
  263. if (!dma_node) {
  264. dev_err(&pdev->dev, "Can't get DMA master node\n");
  265. return -ENODEV;
  266. }
  267. match = of_match_node(ti_dra7_master_match, dma_node);
  268. if (!match) {
  269. dev_err(&pdev->dev, "DMA master is not supported\n");
  270. return -EINVAL;
  271. }
  272. if (of_property_read_u32(dma_node, "dma-requests",
  273. &xbar->dma_requests)) {
  274. dev_info(&pdev->dev,
  275. "Missing XBAR output information, using %u.\n",
  276. TI_DRA7_XBAR_OUTPUTS);
  277. xbar->dma_requests = TI_DRA7_XBAR_OUTPUTS;
  278. }
  279. of_node_put(dma_node);
  280. xbar->dma_inuse = devm_kcalloc(&pdev->dev,
  281. BITS_TO_LONGS(xbar->dma_requests),
  282. sizeof(unsigned long), GFP_KERNEL);
  283. if (!xbar->dma_inuse)
  284. return -ENOMEM;
  285. if (of_property_read_u32(node, "dma-requests", &xbar->xbar_requests)) {
  286. dev_info(&pdev->dev,
  287. "Missing XBAR input information, using %u.\n",
  288. TI_DRA7_XBAR_INPUTS);
  289. xbar->xbar_requests = TI_DRA7_XBAR_INPUTS;
  290. }
  291. if (!of_property_read_u32(node, "ti,dma-safe-map", &safe_val))
  292. xbar->safe_val = (u16)safe_val;
  293. prop = of_find_property(node, "ti,reserved-dma-request-ranges", &sz);
  294. if (prop) {
  295. const char pname[] = "ti,reserved-dma-request-ranges";
  296. u32 (*rsv_events)[2];
  297. size_t nelm = sz / sizeof(*rsv_events);
  298. int i;
  299. if (!nelm)
  300. return -EINVAL;
  301. rsv_events = kcalloc(nelm, sizeof(*rsv_events), GFP_KERNEL);
  302. if (!rsv_events)
  303. return -ENOMEM;
  304. ret = of_property_read_u32_array(node, pname, (u32 *)rsv_events,
  305. nelm * 2);
  306. if (ret)
  307. return ret;
  308. for (i = 0; i < nelm; i++) {
  309. ti_dra7_xbar_reserve(rsv_events[i][0], rsv_events[i][1],
  310. xbar->dma_inuse);
  311. }
  312. kfree(rsv_events);
  313. }
  314. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  315. iomem = devm_ioremap_resource(&pdev->dev, res);
  316. if (IS_ERR(iomem))
  317. return PTR_ERR(iomem);
  318. xbar->iomem = iomem;
  319. xbar->dmarouter.dev = &pdev->dev;
  320. xbar->dmarouter.route_free = ti_dra7_xbar_free;
  321. xbar->dma_offset = (u32)match->data;
  322. mutex_init(&xbar->mutex);
  323. platform_set_drvdata(pdev, xbar);
  324. /* Reset the crossbar */
  325. for (i = 0; i < xbar->dma_requests; i++) {
  326. if (!test_bit(i, xbar->dma_inuse))
  327. ti_dra7_xbar_write(xbar->iomem, i, xbar->safe_val);
  328. }
  329. ret = of_dma_router_register(node, ti_dra7_xbar_route_allocate,
  330. &xbar->dmarouter);
  331. if (ret) {
  332. /* Restore the defaults for the crossbar */
  333. for (i = 0; i < xbar->dma_requests; i++) {
  334. if (!test_bit(i, xbar->dma_inuse))
  335. ti_dra7_xbar_write(xbar->iomem, i, i);
  336. }
  337. }
  338. return ret;
  339. }
  340. static int ti_dma_xbar_probe(struct platform_device *pdev)
  341. {
  342. const struct of_device_id *match;
  343. int ret;
  344. match = of_match_node(ti_dma_xbar_match, pdev->dev.of_node);
  345. if (unlikely(!match))
  346. return -EINVAL;
  347. switch ((u32)match->data) {
  348. case TI_XBAR_DRA7:
  349. ret = ti_dra7_xbar_probe(pdev);
  350. break;
  351. case TI_XBAR_AM335X:
  352. ret = ti_am335x_xbar_probe(pdev);
  353. break;
  354. default:
  355. dev_err(&pdev->dev, "Unsupported crossbar\n");
  356. ret = -ENODEV;
  357. break;
  358. }
  359. return ret;
  360. }
  361. static struct platform_driver ti_dma_xbar_driver = {
  362. .driver = {
  363. .name = "ti-dma-crossbar",
  364. .of_match_table = of_match_ptr(ti_dma_xbar_match),
  365. },
  366. .probe = ti_dma_xbar_probe,
  367. };
  368. int omap_dmaxbar_init(void)
  369. {
  370. return platform_driver_register(&ti_dma_xbar_driver);
  371. }
  372. arch_initcall(omap_dmaxbar_init);