ti-dma-crossbar.c 11 KB

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