mpc512x_lpbfifo.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * The driver for Freescale MPC512x LocalPlus Bus FIFO
  4. * (called SCLPC in the Reference Manual).
  5. *
  6. * Copyright (C) 2013-2015 Alexander Popov <alex.popov@linux.com>.
  7. */
  8. #include <linux/interrupt.h>
  9. #include <linux/kernel.h>
  10. #include <linux/module.h>
  11. #include <linux/of.h>
  12. #include <linux/of_address.h>
  13. #include <linux/of_irq.h>
  14. #include <linux/platform_device.h>
  15. #include <asm/mpc5121.h>
  16. #include <asm/io.h>
  17. #include <linux/spinlock.h>
  18. #include <linux/slab.h>
  19. #include <linux/dmaengine.h>
  20. #include <linux/dma-direction.h>
  21. #include <linux/dma-mapping.h>
  22. #define DRV_NAME "mpc512x_lpbfifo"
  23. struct cs_range {
  24. u32 csnum;
  25. u32 base; /* must be zero */
  26. u32 addr;
  27. u32 size;
  28. };
  29. static struct lpbfifo_data {
  30. spinlock_t lock; /* for protecting lpbfifo_data */
  31. phys_addr_t regs_phys;
  32. resource_size_t regs_size;
  33. struct mpc512x_lpbfifo __iomem *regs;
  34. int irq;
  35. struct cs_range *cs_ranges;
  36. size_t cs_n;
  37. struct dma_chan *chan;
  38. struct mpc512x_lpbfifo_request *req;
  39. dma_addr_t ram_bus_addr;
  40. bool wait_lpbfifo_irq;
  41. bool wait_lpbfifo_callback;
  42. } lpbfifo;
  43. /*
  44. * A data transfer from RAM to some device on LPB is finished
  45. * when both mpc512x_lpbfifo_irq() and mpc512x_lpbfifo_callback()
  46. * have been called. We execute the callback registered in
  47. * mpc512x_lpbfifo_request just after that.
  48. * But for a data transfer from some device on LPB to RAM we don't enable
  49. * LPBFIFO interrupt because clearing MPC512X_SCLPC_SUCCESS interrupt flag
  50. * automatically disables LPBFIFO reading request to the DMA controller
  51. * and the data transfer hangs. So the callback registered in
  52. * mpc512x_lpbfifo_request is executed at the end of mpc512x_lpbfifo_callback().
  53. */
  54. /*
  55. * mpc512x_lpbfifo_irq - IRQ handler for LPB FIFO
  56. */
  57. static irqreturn_t mpc512x_lpbfifo_irq(int irq, void *param)
  58. {
  59. struct device *dev = (struct device *)param;
  60. struct mpc512x_lpbfifo_request *req = NULL;
  61. unsigned long flags;
  62. u32 status;
  63. spin_lock_irqsave(&lpbfifo.lock, flags);
  64. if (!lpbfifo.regs)
  65. goto end;
  66. req = lpbfifo.req;
  67. if (!req || req->dir == MPC512X_LPBFIFO_REQ_DIR_READ) {
  68. dev_err(dev, "bogus LPBFIFO IRQ\n");
  69. goto end;
  70. }
  71. status = in_be32(&lpbfifo.regs->status);
  72. if (status != MPC512X_SCLPC_SUCCESS) {
  73. dev_err(dev, "DMA transfer from RAM to peripheral failed\n");
  74. out_be32(&lpbfifo.regs->enable,
  75. MPC512X_SCLPC_RESET | MPC512X_SCLPC_FIFO_RESET);
  76. goto end;
  77. }
  78. /* Clear the interrupt flag */
  79. out_be32(&lpbfifo.regs->status, MPC512X_SCLPC_SUCCESS);
  80. lpbfifo.wait_lpbfifo_irq = false;
  81. if (lpbfifo.wait_lpbfifo_callback)
  82. goto end;
  83. /* Transfer is finished, set the FIFO as idle */
  84. lpbfifo.req = NULL;
  85. spin_unlock_irqrestore(&lpbfifo.lock, flags);
  86. if (req->callback)
  87. req->callback(req);
  88. return IRQ_HANDLED;
  89. end:
  90. spin_unlock_irqrestore(&lpbfifo.lock, flags);
  91. return IRQ_HANDLED;
  92. }
  93. /*
  94. * mpc512x_lpbfifo_callback is called by DMA driver when
  95. * DMA transaction is finished.
  96. */
  97. static void mpc512x_lpbfifo_callback(void *param)
  98. {
  99. unsigned long flags;
  100. struct mpc512x_lpbfifo_request *req = NULL;
  101. enum dma_data_direction dir;
  102. spin_lock_irqsave(&lpbfifo.lock, flags);
  103. if (!lpbfifo.regs) {
  104. spin_unlock_irqrestore(&lpbfifo.lock, flags);
  105. return;
  106. }
  107. req = lpbfifo.req;
  108. if (!req) {
  109. pr_err("bogus LPBFIFO callback\n");
  110. spin_unlock_irqrestore(&lpbfifo.lock, flags);
  111. return;
  112. }
  113. /* Release the mapping */
  114. if (req->dir == MPC512X_LPBFIFO_REQ_DIR_WRITE)
  115. dir = DMA_TO_DEVICE;
  116. else
  117. dir = DMA_FROM_DEVICE;
  118. dma_unmap_single(lpbfifo.chan->device->dev,
  119. lpbfifo.ram_bus_addr, req->size, dir);
  120. lpbfifo.wait_lpbfifo_callback = false;
  121. if (!lpbfifo.wait_lpbfifo_irq) {
  122. /* Transfer is finished, set the FIFO as idle */
  123. lpbfifo.req = NULL;
  124. spin_unlock_irqrestore(&lpbfifo.lock, flags);
  125. if (req->callback)
  126. req->callback(req);
  127. } else {
  128. spin_unlock_irqrestore(&lpbfifo.lock, flags);
  129. }
  130. }
  131. static int mpc512x_lpbfifo_kick(void)
  132. {
  133. u32 bits;
  134. bool no_incr = false;
  135. u32 bpt = 32; /* max bytes per LPBFIFO transaction involving DMA */
  136. u32 cs = 0;
  137. size_t i;
  138. struct dma_device *dma_dev = NULL;
  139. struct scatterlist sg;
  140. enum dma_data_direction dir;
  141. struct dma_slave_config dma_conf = {};
  142. struct dma_async_tx_descriptor *dma_tx = NULL;
  143. dma_cookie_t cookie;
  144. int ret;
  145. /*
  146. * 1. Fit the requirements:
  147. * - the packet size must be a multiple of 4 since FIFO Data Word
  148. * Register allows only full-word access according the Reference
  149. * Manual;
  150. * - the physical address of the device on LPB and the packet size
  151. * must be aligned on BPT (bytes per transaction) or 8-bytes
  152. * boundary according the Reference Manual;
  153. * - but we choose DMA maxburst equal (or very close to) BPT to prevent
  154. * DMA controller from overtaking FIFO and causing FIFO underflow
  155. * error. So we force the packet size to be aligned on BPT boundary
  156. * not to confuse DMA driver which requires the packet size to be
  157. * aligned on maxburst boundary;
  158. * - BPT should be set to the LPB device port size for operation with
  159. * disabled auto-incrementing according Reference Manual.
  160. */
  161. if (lpbfifo.req->size == 0 || !IS_ALIGNED(lpbfifo.req->size, 4))
  162. return -EINVAL;
  163. if (lpbfifo.req->portsize != LPB_DEV_PORTSIZE_UNDEFINED) {
  164. bpt = lpbfifo.req->portsize;
  165. no_incr = true;
  166. }
  167. while (bpt > 1) {
  168. if (IS_ALIGNED(lpbfifo.req->dev_phys_addr, min(bpt, 0x8u)) &&
  169. IS_ALIGNED(lpbfifo.req->size, bpt)) {
  170. break;
  171. }
  172. if (no_incr)
  173. return -EINVAL;
  174. bpt >>= 1;
  175. }
  176. dma_conf.dst_maxburst = max(bpt, 0x4u) / 4;
  177. dma_conf.src_maxburst = max(bpt, 0x4u) / 4;
  178. for (i = 0; i < lpbfifo.cs_n; i++) {
  179. phys_addr_t cs_start = lpbfifo.cs_ranges[i].addr;
  180. phys_addr_t cs_end = cs_start + lpbfifo.cs_ranges[i].size;
  181. phys_addr_t access_start = lpbfifo.req->dev_phys_addr;
  182. phys_addr_t access_end = access_start + lpbfifo.req->size;
  183. if (access_start >= cs_start && access_end <= cs_end) {
  184. cs = lpbfifo.cs_ranges[i].csnum;
  185. break;
  186. }
  187. }
  188. if (i == lpbfifo.cs_n)
  189. return -EFAULT;
  190. /* 2. Prepare DMA */
  191. dma_dev = lpbfifo.chan->device;
  192. if (lpbfifo.req->dir == MPC512X_LPBFIFO_REQ_DIR_WRITE) {
  193. dir = DMA_TO_DEVICE;
  194. dma_conf.direction = DMA_MEM_TO_DEV;
  195. dma_conf.dst_addr = lpbfifo.regs_phys +
  196. offsetof(struct mpc512x_lpbfifo, data_word);
  197. } else {
  198. dir = DMA_FROM_DEVICE;
  199. dma_conf.direction = DMA_DEV_TO_MEM;
  200. dma_conf.src_addr = lpbfifo.regs_phys +
  201. offsetof(struct mpc512x_lpbfifo, data_word);
  202. }
  203. dma_conf.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  204. dma_conf.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  205. /* Make DMA channel work with LPB FIFO data register */
  206. if (dma_dev->device_config(lpbfifo.chan, &dma_conf))
  207. return -EINVAL;
  208. sg_init_table(&sg, 1);
  209. sg_dma_address(&sg) = dma_map_single(dma_dev->dev,
  210. lpbfifo.req->ram_virt_addr, lpbfifo.req->size, dir);
  211. if (dma_mapping_error(dma_dev->dev, sg_dma_address(&sg)))
  212. return -EFAULT;
  213. lpbfifo.ram_bus_addr = sg_dma_address(&sg); /* For freeing later */
  214. sg_dma_len(&sg) = lpbfifo.req->size;
  215. dma_tx = dmaengine_prep_slave_sg(lpbfifo.chan, &sg,
  216. 1, dma_conf.direction, 0);
  217. if (!dma_tx) {
  218. ret = -ENOSPC;
  219. goto err_dma_prep;
  220. }
  221. dma_tx->callback = mpc512x_lpbfifo_callback;
  222. dma_tx->callback_param = NULL;
  223. /* 3. Prepare FIFO */
  224. out_be32(&lpbfifo.regs->enable,
  225. MPC512X_SCLPC_RESET | MPC512X_SCLPC_FIFO_RESET);
  226. out_be32(&lpbfifo.regs->enable, 0x0);
  227. /*
  228. * Configure the watermarks for write operation (RAM->DMA->FIFO->dev):
  229. * - high watermark 7 words according the Reference Manual,
  230. * - low watermark 512 bytes (half of the FIFO).
  231. * These watermarks don't work for read operation since the
  232. * MPC512X_SCLPC_FLUSH bit is set (according the Reference Manual).
  233. */
  234. out_be32(&lpbfifo.regs->fifo_ctrl, MPC512X_SCLPC_FIFO_CTRL(0x7));
  235. out_be32(&lpbfifo.regs->fifo_alarm, MPC512X_SCLPC_FIFO_ALARM(0x200));
  236. /*
  237. * Start address is a physical address of the region which belongs
  238. * to the device on the LocalPlus Bus
  239. */
  240. out_be32(&lpbfifo.regs->start_addr, lpbfifo.req->dev_phys_addr);
  241. /*
  242. * Configure chip select, transfer direction, address increment option
  243. * and bytes per transaction option
  244. */
  245. bits = MPC512X_SCLPC_CS(cs);
  246. if (lpbfifo.req->dir == MPC512X_LPBFIFO_REQ_DIR_READ)
  247. bits |= MPC512X_SCLPC_READ | MPC512X_SCLPC_FLUSH;
  248. if (no_incr)
  249. bits |= MPC512X_SCLPC_DAI;
  250. bits |= MPC512X_SCLPC_BPT(bpt);
  251. out_be32(&lpbfifo.regs->ctrl, bits);
  252. /* Unmask irqs */
  253. bits = MPC512X_SCLPC_ENABLE | MPC512X_SCLPC_ABORT_INT_ENABLE;
  254. if (lpbfifo.req->dir == MPC512X_LPBFIFO_REQ_DIR_WRITE)
  255. bits |= MPC512X_SCLPC_NORM_INT_ENABLE;
  256. else
  257. lpbfifo.wait_lpbfifo_irq = false;
  258. out_be32(&lpbfifo.regs->enable, bits);
  259. /* 4. Set packet size and kick FIFO off */
  260. bits = lpbfifo.req->size | MPC512X_SCLPC_START;
  261. out_be32(&lpbfifo.regs->pkt_size, bits);
  262. /* 5. Finally kick DMA off */
  263. cookie = dma_tx->tx_submit(dma_tx);
  264. if (dma_submit_error(cookie)) {
  265. ret = -ENOSPC;
  266. goto err_dma_submit;
  267. }
  268. return 0;
  269. err_dma_submit:
  270. out_be32(&lpbfifo.regs->enable,
  271. MPC512X_SCLPC_RESET | MPC512X_SCLPC_FIFO_RESET);
  272. err_dma_prep:
  273. dma_unmap_single(dma_dev->dev, sg_dma_address(&sg),
  274. lpbfifo.req->size, dir);
  275. return ret;
  276. }
  277. static int mpc512x_lpbfifo_submit_locked(struct mpc512x_lpbfifo_request *req)
  278. {
  279. int ret = 0;
  280. if (!lpbfifo.regs)
  281. return -ENODEV;
  282. /* Check whether a transfer is in progress */
  283. if (lpbfifo.req)
  284. return -EBUSY;
  285. lpbfifo.wait_lpbfifo_irq = true;
  286. lpbfifo.wait_lpbfifo_callback = true;
  287. lpbfifo.req = req;
  288. ret = mpc512x_lpbfifo_kick();
  289. if (ret != 0)
  290. lpbfifo.req = NULL; /* Set the FIFO as idle */
  291. return ret;
  292. }
  293. int mpc512x_lpbfifo_submit(struct mpc512x_lpbfifo_request *req)
  294. {
  295. unsigned long flags;
  296. int ret = 0;
  297. spin_lock_irqsave(&lpbfifo.lock, flags);
  298. ret = mpc512x_lpbfifo_submit_locked(req);
  299. spin_unlock_irqrestore(&lpbfifo.lock, flags);
  300. return ret;
  301. }
  302. EXPORT_SYMBOL(mpc512x_lpbfifo_submit);
  303. /*
  304. * LPBFIFO driver uses "ranges" property of "localbus" device tree node
  305. * for being able to determine the chip select number of a client device
  306. * ordering a DMA transfer.
  307. */
  308. static int get_cs_ranges(struct device *dev)
  309. {
  310. int ret = -ENODEV;
  311. struct device_node *lb_node;
  312. size_t i = 0;
  313. struct of_range_parser parser;
  314. struct of_range range;
  315. lb_node = of_find_compatible_node(NULL, NULL, "fsl,mpc5121-localbus");
  316. if (!lb_node)
  317. return ret;
  318. of_range_parser_init(&parser, lb_node);
  319. lpbfifo.cs_n = of_range_count(&parser);
  320. lpbfifo.cs_ranges = devm_kcalloc(dev, lpbfifo.cs_n,
  321. sizeof(struct cs_range), GFP_KERNEL);
  322. if (!lpbfifo.cs_ranges)
  323. goto end;
  324. for_each_of_range(&parser, &range) {
  325. u32 base = lower_32_bits(range.bus_addr);
  326. if (base)
  327. goto end;
  328. lpbfifo.cs_ranges[i].csnum = upper_32_bits(range.bus_addr);
  329. lpbfifo.cs_ranges[i].base = base;
  330. lpbfifo.cs_ranges[i].addr = range.cpu_addr;
  331. lpbfifo.cs_ranges[i].size = range.size;
  332. i++;
  333. }
  334. ret = 0;
  335. end:
  336. of_node_put(lb_node);
  337. return ret;
  338. }
  339. static int mpc512x_lpbfifo_probe(struct platform_device *pdev)
  340. {
  341. struct resource r;
  342. int ret = 0;
  343. memset(&lpbfifo, 0, sizeof(struct lpbfifo_data));
  344. spin_lock_init(&lpbfifo.lock);
  345. lpbfifo.chan = dma_request_chan(&pdev->dev, "rx-tx");
  346. if (IS_ERR(lpbfifo.chan))
  347. return PTR_ERR(lpbfifo.chan);
  348. if (of_address_to_resource(pdev->dev.of_node, 0, &r) != 0) {
  349. dev_err(&pdev->dev, "bad 'reg' in 'sclpc' device tree node\n");
  350. ret = -ENODEV;
  351. goto err0;
  352. }
  353. lpbfifo.regs_phys = r.start;
  354. lpbfifo.regs_size = resource_size(&r);
  355. if (!devm_request_mem_region(&pdev->dev, lpbfifo.regs_phys,
  356. lpbfifo.regs_size, DRV_NAME)) {
  357. dev_err(&pdev->dev, "unable to request region\n");
  358. ret = -EBUSY;
  359. goto err0;
  360. }
  361. lpbfifo.regs = devm_ioremap(&pdev->dev,
  362. lpbfifo.regs_phys, lpbfifo.regs_size);
  363. if (!lpbfifo.regs) {
  364. dev_err(&pdev->dev, "mapping registers failed\n");
  365. ret = -ENOMEM;
  366. goto err0;
  367. }
  368. out_be32(&lpbfifo.regs->enable,
  369. MPC512X_SCLPC_RESET | MPC512X_SCLPC_FIFO_RESET);
  370. if (get_cs_ranges(&pdev->dev) != 0) {
  371. dev_err(&pdev->dev, "bad '/localbus' device tree node\n");
  372. ret = -ENODEV;
  373. goto err0;
  374. }
  375. lpbfifo.irq = irq_of_parse_and_map(pdev->dev.of_node, 0);
  376. if (!lpbfifo.irq) {
  377. dev_err(&pdev->dev, "mapping irq failed\n");
  378. ret = -ENODEV;
  379. goto err0;
  380. }
  381. if (request_irq(lpbfifo.irq, mpc512x_lpbfifo_irq, 0,
  382. DRV_NAME, &pdev->dev) != 0) {
  383. dev_err(&pdev->dev, "requesting irq failed\n");
  384. ret = -ENODEV;
  385. goto err1;
  386. }
  387. dev_info(&pdev->dev, "probe succeeded\n");
  388. return 0;
  389. err1:
  390. irq_dispose_mapping(lpbfifo.irq);
  391. err0:
  392. dma_release_channel(lpbfifo.chan);
  393. return ret;
  394. }
  395. static void mpc512x_lpbfifo_remove(struct platform_device *pdev)
  396. {
  397. unsigned long flags;
  398. struct dma_device *dma_dev = lpbfifo.chan->device;
  399. struct mpc512x_lpbfifo __iomem *regs = NULL;
  400. spin_lock_irqsave(&lpbfifo.lock, flags);
  401. regs = lpbfifo.regs;
  402. lpbfifo.regs = NULL;
  403. spin_unlock_irqrestore(&lpbfifo.lock, flags);
  404. dma_dev->device_terminate_all(lpbfifo.chan);
  405. out_be32(&regs->enable, MPC512X_SCLPC_RESET | MPC512X_SCLPC_FIFO_RESET);
  406. free_irq(lpbfifo.irq, &pdev->dev);
  407. irq_dispose_mapping(lpbfifo.irq);
  408. dma_release_channel(lpbfifo.chan);
  409. }
  410. static const struct of_device_id mpc512x_lpbfifo_match[] = {
  411. { .compatible = "fsl,mpc512x-lpbfifo", },
  412. {},
  413. };
  414. MODULE_DEVICE_TABLE(of, mpc512x_lpbfifo_match);
  415. static struct platform_driver mpc512x_lpbfifo_driver = {
  416. .probe = mpc512x_lpbfifo_probe,
  417. .remove = mpc512x_lpbfifo_remove,
  418. .driver = {
  419. .name = DRV_NAME,
  420. .of_match_table = mpc512x_lpbfifo_match,
  421. },
  422. };
  423. module_platform_driver(mpc512x_lpbfifo_driver);
  424. MODULE_AUTHOR("Alexander Popov <alex.popov@linux.com>");
  425. MODULE_DESCRIPTION("MPC512x LocalPlus Bus FIFO device driver");
  426. MODULE_LICENSE("GPL v2");