1 // SPDX-License-Identifier: GPL-2.0-only
3 * LocalPlus Bus FIFO driver for the Freescale MPC52xx.
5 * Copyright (C) 2009 Secret Lab Technologies Ltd.
8 * - Add support for multiple requests to be queued.
11 #include <linux/interrupt.h>
12 #include <linux/kernel.h>
14 #include <linux/of_platform.h>
15 #include <linux/spinlock.h>
16 #include <linux/module.h>
19 #include <asm/mpc52xx.h>
22 #include <linux/fsl/bestcomm/bestcomm.h>
23 #include <linux/fsl/bestcomm/bestcomm_priv.h>
24 #include <linux/fsl/bestcomm/gen_bd.h>
26 MODULE_AUTHOR("Grant Likely <grant.likely@secretlab.ca>");
27 MODULE_DESCRIPTION("MPC5200 LocalPlus FIFO device driver");
28 MODULE_LICENSE("GPL");
30 #define LPBFIFO_REG_PACKET_SIZE (0x00)
31 #define LPBFIFO_REG_START_ADDRESS (0x04)
32 #define LPBFIFO_REG_CONTROL (0x08)
33 #define LPBFIFO_REG_ENABLE (0x0C)
34 #define LPBFIFO_REG_BYTES_DONE_STATUS (0x14)
35 #define LPBFIFO_REG_FIFO_DATA (0x40)
36 #define LPBFIFO_REG_FIFO_STATUS (0x44)
37 #define LPBFIFO_REG_FIFO_CONTROL (0x48)
38 #define LPBFIFO_REG_FIFO_ALARM (0x4C)
40 struct mpc52xx_lpbfifo {
42 phys_addr_t regs_phys;
47 struct bcom_task *bcom_tx_task;
48 struct bcom_task *bcom_rx_task;
49 struct bcom_task *bcom_cur_task;
51 /* Current state data */
52 struct mpc52xx_lpbfifo_request *req;
56 /* The MPC5200 has only one fifo, so only need one instance structure */
57 static struct mpc52xx_lpbfifo lpbfifo;
60 * mpc52xx_lpbfifo_kick - Trigger the next block of data to be transferred
62 static void mpc52xx_lpbfifo_kick(struct mpc52xx_lpbfifo_request *req)
64 size_t transfer_size = req->size - req->pos;
70 int dma = !(req->flags & MPC52XX_LPBFIFO_FLAG_NO_DMA);
71 int write = req->flags & MPC52XX_LPBFIFO_FLAG_WRITE;
72 int poll_dma = req->flags & MPC52XX_LPBFIFO_FLAG_POLL_DMA;
74 /* Set and clear the reset bits; is good practice in User Manual */
75 out_be32(lpbfifo.regs + LPBFIFO_REG_ENABLE, 0x01010000);
77 /* set master enable bit */
78 out_be32(lpbfifo.regs + LPBFIFO_REG_ENABLE, 0x00000001);
80 /* While the FIFO can be setup for transfer sizes as large as
81 * 16M-1, the FIFO itself is only 512 bytes deep and it does
82 * not generate interrupts for FIFO full events (only transfer
83 * complete will raise an IRQ). Therefore when not using
84 * Bestcomm to drive the FIFO it needs to either be polled, or
85 * transfers need to constrained to the size of the fifo.
87 * This driver restricts the size of the transfer
89 if (transfer_size > 512)
92 /* Load the FIFO with data */
94 reg = lpbfifo.regs + LPBFIFO_REG_FIFO_DATA;
95 data = req->data + req->pos;
96 for (i = 0; i < transfer_size; i += 4)
97 out_be32(reg, *data++);
100 /* Unmask both error and completion irqs */
101 out_be32(lpbfifo.regs + LPBFIFO_REG_ENABLE, 0x00000301);
103 /* Choose the correct direction
105 * Configure the watermarks so DMA will always complete correctly.
106 * It may be worth experimenting with the ALARM value to see if
107 * there is a performance impacit. However, if it is wrong there
108 * is a risk of DMA not transferring the last chunk of data
111 out_be32(lpbfifo.regs + LPBFIFO_REG_FIFO_ALARM, 0x1e4);
112 out_8(lpbfifo.regs + LPBFIFO_REG_FIFO_CONTROL, 7);
113 lpbfifo.bcom_cur_task = lpbfifo.bcom_tx_task;
115 out_be32(lpbfifo.regs + LPBFIFO_REG_FIFO_ALARM, 0x1ff);
116 out_8(lpbfifo.regs + LPBFIFO_REG_FIFO_CONTROL, 0);
117 lpbfifo.bcom_cur_task = lpbfifo.bcom_rx_task;
120 if (lpbfifo.dma_irqs_enabled) {
121 disable_irq(bcom_get_task_irq(lpbfifo.bcom_rx_task));
122 lpbfifo.dma_irqs_enabled = 0;
125 if (!lpbfifo.dma_irqs_enabled) {
126 enable_irq(bcom_get_task_irq(lpbfifo.bcom_rx_task));
127 lpbfifo.dma_irqs_enabled = 1;
132 bd = bcom_prepare_next_buffer(lpbfifo.bcom_cur_task);
133 bd->status = transfer_size;
136 * In the DMA read case, the DMA doesn't complete,
137 * possibly due to incorrect watermarks in the ALARM
138 * and CONTROL regs. For now instead of trying to
139 * determine the right watermarks that will make this
140 * work, just increase the number of bytes the FIFO is
143 * When submitting another operation, the FIFO will get
144 * reset, so the condition of the FIFO waiting for a
145 * non-existent 4 bytes will get cleared.
147 transfer_size += 4; /* BLECH! */
149 bd->data[0] = req->data_phys + req->pos;
150 bcom_submit_next_buffer(lpbfifo.bcom_cur_task, NULL);
152 /* error irq & master enabled bit */
153 bit_fields = 0x00000201;
156 if (write && (!poll_dma))
157 bit_fields |= 0x00000100; /* completion irq too */
158 out_be32(lpbfifo.regs + LPBFIFO_REG_ENABLE, bit_fields);
161 /* Set transfer size, width, chip select and READ mode */
162 out_be32(lpbfifo.regs + LPBFIFO_REG_START_ADDRESS,
163 req->offset + req->pos);
164 out_be32(lpbfifo.regs + LPBFIFO_REG_PACKET_SIZE, transfer_size);
166 bit_fields = req->cs << 24 | 0x000008;
168 bit_fields |= 0x010000; /* read mode */
169 out_be32(lpbfifo.regs + LPBFIFO_REG_CONTROL, bit_fields);
172 if (!lpbfifo.req->defer_xfer_start)
173 out_8(lpbfifo.regs + LPBFIFO_REG_PACKET_SIZE, 0x01);
175 bcom_enable(lpbfifo.bcom_cur_task);
179 * mpc52xx_lpbfifo_irq - IRQ handler for LPB FIFO
181 * On transmit, the dma completion irq triggers before the fifo completion
182 * triggers. Handle the dma completion here instead of the LPB FIFO Bestcomm
183 * task completion irq because everything is not really done until the LPB FIFO
184 * completion irq triggers.
187 * For DMA, on receive, the "Fat Lady" is the bestcom completion irq. on
188 * transmit, the fifo completion irq is the "Fat Lady". The opera (or in this
189 * case the DMA/FIFO operation) is not finished until the "Fat Lady" sings.
191 * Reasons for entering this routine:
192 * 1) PIO mode rx and tx completion irq
193 * 2) DMA interrupt mode tx completion irq
194 * 3) DMA polled mode tx
197 * 1) Transfer aborted
198 * 2) FIFO complete without DMA; more data to do
199 * 3) FIFO complete without DMA; all data transferred
200 * 4) FIFO complete using DMA
202 * Condition 1 can occur regardless of whether or not DMA is used.
203 * It requires executing the callback to report the error and exiting
206 * Condition 2 requires programming the FIFO with the next block of data
208 * Condition 3 requires executing the callback to report completion
210 * Condition 4 means the same as 3, except that we also retrieve the bcom
211 * buffer so DMA doesn't get clogged up.
213 * To make things trickier, the spinlock must be dropped before
214 * executing the callback, otherwise we could end up with a deadlock
215 * or nested spinlock condition. The out path is non-trivial, so
216 * extra fiddling is done to make sure all paths lead to the same
219 static irqreturn_t mpc52xx_lpbfifo_irq(int irq, void *dev_id)
221 struct mpc52xx_lpbfifo_request *req;
222 u32 status = in_8(lpbfifo.regs + LPBFIFO_REG_BYTES_DONE_STATUS);
229 int dma, write, poll_dma;
231 spin_lock_irqsave(&lpbfifo.lock, flags);
236 spin_unlock_irqrestore(&lpbfifo.lock, flags);
237 pr_err("bogus LPBFIFO IRQ\n");
241 dma = !(req->flags & MPC52XX_LPBFIFO_FLAG_NO_DMA);
242 write = req->flags & MPC52XX_LPBFIFO_FLAG_WRITE;
243 poll_dma = req->flags & MPC52XX_LPBFIFO_FLAG_POLL_DMA;
246 spin_unlock_irqrestore(&lpbfifo.lock, flags);
247 pr_err("bogus LPBFIFO IRQ (dma and not writing)\n");
251 if ((status & 0x01) == 0) {
255 /* check abort bit */
257 out_be32(lpbfifo.regs + LPBFIFO_REG_ENABLE, 0x01010000);
262 /* Read result from hardware */
263 count = in_be32(lpbfifo.regs + LPBFIFO_REG_BYTES_DONE_STATUS);
266 if (!dma && !write) {
267 /* copy the data out of the FIFO */
268 reg = lpbfifo.regs + LPBFIFO_REG_FIFO_DATA;
269 data = req->data + req->pos;
270 for (i = 0; i < count; i += 4)
271 *data++ = in_be32(reg);
274 /* Update transfer position and count */
277 /* Decide what to do next */
278 if (req->size - req->pos)
279 mpc52xx_lpbfifo_kick(req); /* more work to do */
285 out_8(lpbfifo.regs + LPBFIFO_REG_BYTES_DONE_STATUS, 0x01);
287 if (dma && (status & 0x11)) {
289 * Count the DMA as complete only when the FIFO completion
290 * status or abort bits are set.
292 * (status & 0x01) should always be the case except sometimes
293 * when using polled DMA.
295 * (status & 0x10) {transfer aborted}: This case needs more
298 bcom_retrieve_buffer(lpbfifo.bcom_cur_task, &status, NULL);
300 req->last_byte = ((u8 *)req->data)[req->size - 1];
302 /* When the do_callback flag is set; it means the transfer is finished
303 * so set the FIFO as idle */
307 if (irq != 0) /* don't increment on polled case */
310 req->irq_ticks += get_tbl() - ts;
311 spin_unlock_irqrestore(&lpbfifo.lock, flags);
313 /* Spinlock is released; it is now safe to call the callback */
314 if (do_callback && req->callback)
321 * mpc52xx_lpbfifo_bcom_irq - IRQ handler for LPB FIFO Bestcomm task
323 * Only used when receiving data.
325 static irqreturn_t mpc52xx_lpbfifo_bcom_irq(int irq, void *dev_id)
327 struct mpc52xx_lpbfifo_request *req;
332 spin_lock_irqsave(&lpbfifo.lock, flags);
336 if (!req || (req->flags & MPC52XX_LPBFIFO_FLAG_NO_DMA)) {
337 spin_unlock_irqrestore(&lpbfifo.lock, flags);
341 if (irq != 0) /* don't increment on polled case */
344 if (!bcom_buffer_done(lpbfifo.bcom_cur_task)) {
345 spin_unlock_irqrestore(&lpbfifo.lock, flags);
347 req->buffer_not_done_cnt++;
348 if ((req->buffer_not_done_cnt % 1000) == 0)
349 pr_err("transfer stalled\n");
354 bcom_retrieve_buffer(lpbfifo.bcom_cur_task, &status, NULL);
356 req->last_byte = ((u8 *)req->data)[req->size - 1];
358 req->pos = status & 0x00ffffff;
360 /* Mark the FIFO as idle */
363 /* Release the lock before calling out to the callback. */
364 req->irq_ticks += get_tbl() - ts;
365 spin_unlock_irqrestore(&lpbfifo.lock, flags);
374 * mpc52xx_lpbfifo_bcom_poll - Poll for DMA completion
376 void mpc52xx_lpbfifo_poll(void)
378 struct mpc52xx_lpbfifo_request *req = lpbfifo.req;
379 int dma = !(req->flags & MPC52XX_LPBFIFO_FLAG_NO_DMA);
380 int write = req->flags & MPC52XX_LPBFIFO_FLAG_WRITE;
383 * For more information, see comments on the "Fat Lady"
386 mpc52xx_lpbfifo_irq(0, NULL);
388 mpc52xx_lpbfifo_bcom_irq(0, NULL);
390 EXPORT_SYMBOL(mpc52xx_lpbfifo_poll);
393 * mpc52xx_lpbfifo_submit - Submit an LPB FIFO transfer request.
394 * @req: Pointer to request structure
396 int mpc52xx_lpbfifo_submit(struct mpc52xx_lpbfifo_request *req)
403 spin_lock_irqsave(&lpbfifo.lock, flags);
405 /* If the req pointer is already set, then a transfer is in progress */
407 spin_unlock_irqrestore(&lpbfifo.lock, flags);
411 /* Setup the transfer */
415 req->buffer_not_done_cnt = 0;
418 mpc52xx_lpbfifo_kick(req);
419 spin_unlock_irqrestore(&lpbfifo.lock, flags);
422 EXPORT_SYMBOL(mpc52xx_lpbfifo_submit);
424 int mpc52xx_lpbfifo_start_xfer(struct mpc52xx_lpbfifo_request *req)
431 spin_lock_irqsave(&lpbfifo.lock, flags);
434 * If the req pointer is already set and a transfer was
435 * started on submit, then this transfer is in progress
437 if (lpbfifo.req && !lpbfifo.req->defer_xfer_start) {
438 spin_unlock_irqrestore(&lpbfifo.lock, flags);
443 * If the req was previously submitted but not
444 * started, start it now
446 if (lpbfifo.req && lpbfifo.req == req &&
447 lpbfifo.req->defer_xfer_start) {
448 out_8(lpbfifo.regs + LPBFIFO_REG_PACKET_SIZE, 0x01);
451 spin_unlock_irqrestore(&lpbfifo.lock, flags);
454 EXPORT_SYMBOL(mpc52xx_lpbfifo_start_xfer);
456 void mpc52xx_lpbfifo_abort(struct mpc52xx_lpbfifo_request *req)
460 spin_lock_irqsave(&lpbfifo.lock, flags);
461 if (lpbfifo.req == req) {
462 /* Put it into reset and clear the state */
463 bcom_gen_bd_rx_reset(lpbfifo.bcom_rx_task);
464 bcom_gen_bd_tx_reset(lpbfifo.bcom_tx_task);
465 out_be32(lpbfifo.regs + LPBFIFO_REG_ENABLE, 0x01010000);
468 spin_unlock_irqrestore(&lpbfifo.lock, flags);
470 EXPORT_SYMBOL(mpc52xx_lpbfifo_abort);
472 static int mpc52xx_lpbfifo_probe(struct platform_device *op)
477 if (lpbfifo.dev != NULL)
480 lpbfifo.irq = irq_of_parse_and_map(op->dev.of_node, 0);
484 if (of_address_to_resource(op->dev.of_node, 0, &res))
486 lpbfifo.regs_phys = res.start;
487 lpbfifo.regs = of_iomap(op->dev.of_node, 0);
491 spin_lock_init(&lpbfifo.lock);
493 /* Put FIFO into reset */
494 out_be32(lpbfifo.regs + LPBFIFO_REG_ENABLE, 0x01010000);
496 /* Register the interrupt handler */
497 rc = request_irq(lpbfifo.irq, mpc52xx_lpbfifo_irq, 0,
498 "mpc52xx-lpbfifo", &lpbfifo);
502 /* Request the Bestcomm receive (fifo --> memory) task and IRQ */
503 lpbfifo.bcom_rx_task =
504 bcom_gen_bd_rx_init(2, res.start + LPBFIFO_REG_FIFO_DATA,
505 BCOM_INITIATOR_SCLPC, BCOM_IPR_SCLPC,
507 if (!lpbfifo.bcom_rx_task)
510 rc = request_irq(bcom_get_task_irq(lpbfifo.bcom_rx_task),
511 mpc52xx_lpbfifo_bcom_irq, 0,
512 "mpc52xx-lpbfifo-rx", &lpbfifo);
514 goto err_bcom_rx_irq;
516 lpbfifo.dma_irqs_enabled = 1;
518 /* Request the Bestcomm transmit (memory --> fifo) task and IRQ */
519 lpbfifo.bcom_tx_task =
520 bcom_gen_bd_tx_init(2, res.start + LPBFIFO_REG_FIFO_DATA,
521 BCOM_INITIATOR_SCLPC, BCOM_IPR_SCLPC);
522 if (!lpbfifo.bcom_tx_task)
525 lpbfifo.dev = &op->dev;
529 free_irq(bcom_get_task_irq(lpbfifo.bcom_rx_task), &lpbfifo);
531 bcom_gen_bd_rx_release(lpbfifo.bcom_rx_task);
533 free_irq(lpbfifo.irq, &lpbfifo);
535 iounmap(lpbfifo.regs);
538 dev_err(&op->dev, "mpc52xx_lpbfifo_probe() failed\n");
543 static int mpc52xx_lpbfifo_remove(struct platform_device *op)
545 if (lpbfifo.dev != &op->dev)
548 /* Put FIFO in reset */
549 out_be32(lpbfifo.regs + LPBFIFO_REG_ENABLE, 0x01010000);
551 /* Release the bestcomm transmit task */
552 free_irq(bcom_get_task_irq(lpbfifo.bcom_tx_task), &lpbfifo);
553 bcom_gen_bd_tx_release(lpbfifo.bcom_tx_task);
555 /* Release the bestcomm receive task */
556 free_irq(bcom_get_task_irq(lpbfifo.bcom_rx_task), &lpbfifo);
557 bcom_gen_bd_rx_release(lpbfifo.bcom_rx_task);
559 free_irq(lpbfifo.irq, &lpbfifo);
560 iounmap(lpbfifo.regs);
567 static const struct of_device_id mpc52xx_lpbfifo_match[] = {
568 { .compatible = "fsl,mpc5200-lpbfifo", },
571 MODULE_DEVICE_TABLE(of, mpc52xx_lpbfifo_match);
573 static struct platform_driver mpc52xx_lpbfifo_driver = {
575 .name = "mpc52xx-lpbfifo",
576 .of_match_table = mpc52xx_lpbfifo_match,
578 .probe = mpc52xx_lpbfifo_probe,
579 .remove = mpc52xx_lpbfifo_remove,
581 module_platform_driver(mpc52xx_lpbfifo_driver);