mpc512x_lpbfifo.c 14 KB

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