2 * DMAEngine driver for Xilinx Framebuffer IP
4 * Copyright (C) 2016,2017 Xilinx, Inc. All rights reserved.
6 * Authors: Radhey Shyam Pandey <radheys@xilinx.com>
7 * John Nichols <jnichol@xilinx.com>
8 * Jeffrey Mouroux <jmouroux@xilinx.com>
10 * Based on the Freescale DMA driver.
13 * The AXI Framebuffer core is a soft Xilinx IP core that
14 * provides high-bandwidth direct memory access between memory
17 * This program is free software: you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation, either version 2 of the License, or
20 * (at your option) any later version.
23 #include <linux/bitops.h>
24 #include <linux/dma/xilinx_frmbuf.h>
25 #include <linux/dmapool.h>
26 #include <linux/gpio/consumer.h>
27 #include <linux/init.h>
28 #include <linux/interrupt.h>
30 #include <linux/iopoll.h>
31 #include <linux/module.h>
32 #include <linux/of_address.h>
33 #include <linux/of_dma.h>
34 #include <linux/of_irq.h>
35 #include <linux/of_platform.h>
36 #include <linux/slab.h>
37 #include <linux/videodev2.h>
39 #include <drm/drm_fourcc.h>
41 #include "../dmaengine.h"
43 /* Register/Descriptor Offsets */
44 #define XILINX_FRMBUF_CTRL_OFFSET 0x00
45 #define XILINX_FRMBUF_GIE_OFFSET 0x04
46 #define XILINX_FRMBUF_IE_OFFSET 0x08
47 #define XILINX_FRMBUF_ISR_OFFSET 0x0c
48 #define XILINX_FRMBUF_WIDTH_OFFSET 0x10
49 #define XILINX_FRMBUF_HEIGHT_OFFSET 0x18
50 #define XILINX_FRMBUF_STRIDE_OFFSET 0x20
51 #define XILINX_FRMBUF_FMT_OFFSET 0x28
52 #define XILINX_FRMBUF_ADDR_OFFSET 0x30
53 #define XILINX_FRMBUF_ADDR2_OFFSET 0x3c
55 /* Control Registers */
56 #define XILINX_FRMBUF_CTRL_AP_START BIT(0)
57 #define XILINX_FRMBUF_CTRL_AP_DONE BIT(1)
58 #define XILINX_FRMBUF_CTRL_AP_IDLE BIT(2)
59 #define XILINX_FRMBUF_CTRL_AP_READY BIT(3)
60 #define XILINX_FRMBUF_CTRL_AUTO_RESTART BIT(7)
61 #define XILINX_FRMBUF_GIE_EN BIT(0)
63 /* Interrupt Status and Control */
64 #define XILINX_FRMBUF_IE_AP_DONE BIT(0)
65 #define XILINX_FRMBUF_IE_AP_READY BIT(1)
67 #define XILINX_FRMBUF_ISR_AP_DONE_IRQ BIT(0)
68 #define XILINX_FRMBUF_ISR_AP_READY_IRQ BIT(1)
70 #define XILINX_FRMBUF_ISR_ALL_IRQ_MASK \
71 (XILINX_FRMBUF_ISR_AP_DONE_IRQ | \
72 XILINX_FRMBUF_ISR_AP_READY_IRQ)
74 /* Video Format Register Settings */
75 #define XILINX_FRMBUF_FMT_RGBX8 10
76 #define XILINX_FRMBUF_FMT_YUVX8 11
77 #define XILINX_FRMBUF_FMT_YUYV8 12
78 #define XILINX_FRMBUF_FMT_RGBA8 13
79 #define XILINX_FRMBUF_FMT_YUVA8 14
80 #define XILINX_FRMBUF_FMT_RGBX10 15
81 #define XILINX_FRMBUF_FMT_YUVX10 16
82 #define XILINX_FRMBUF_FMT_Y_UV8 18
83 #define XILINX_FRMBUF_FMT_Y_UV8_420 19
84 #define XILINX_FRMBUF_FMT_RGB8 20
85 #define XILINX_FRMBUF_FMT_YUV8 21
86 #define XILINX_FRMBUF_FMT_Y_UV10 22
87 #define XILINX_FRMBUF_FMT_Y_UV10_420 23
88 #define XILINX_FRMBUF_FMT_Y8 24
89 #define XILINX_FRMBUF_FMT_Y10 25
90 #define XILINX_FRMBUF_FMT_BGRA8 26
91 #define XILINX_FRMBUF_FMT_BGRX8 27
92 #define XILINX_FRMBUF_FMT_UYVY8 28
93 #define XILINX_FRMBUF_FMT_BGR8 29
96 * struct xilinx_frmbuf_desc_hw - Hardware Descriptor
97 * @luma_plane_addr: Luma or packed plane buffer address
98 * @chroma_plane_addr: Chroma plane buffer address
99 * @vsize: Vertical Size
100 * @hsize: Horizontal Size
101 * @stride: Number of bytes between the first
102 * pixels of each horizontal line
104 struct xilinx_frmbuf_desc_hw {
105 dma_addr_t luma_plane_addr;
106 dma_addr_t chroma_plane_addr;
113 * struct xilinx_frmbuf_tx_descriptor - Per Transaction structure
114 * @async_tx: Async transaction descriptor
115 * @hw: Hardware descriptor
116 * @node: Node in the channel descriptors list
118 struct xilinx_frmbuf_tx_descriptor {
119 struct dma_async_tx_descriptor async_tx;
120 struct xilinx_frmbuf_desc_hw hw;
121 struct list_head node;
125 * struct xilinx_frmbuf_chan - Driver specific dma channel structure
126 * @xdev: Driver specific device structure
127 * @lock: Descriptor operation lock
128 * @chan_node: Member of a list of framebuffer channel instances
129 * @pending_list: Descriptors waiting
130 * @done_list: Complete descriptors
131 * @staged_desc: Next buffer to be programmed
132 * @active_desc: Currently active buffer being read/written to
133 * @common: DMA common channel
134 * @dev: The dma device
135 * @write_addr: callback that will write dma addresses to IP (32 or 64 bit)
137 * @direction: Transfer direction
138 * @idle: Channel idle state
139 * @tasklet: Cleanup work after irq
140 * @vid_fmt: Reference to currently assigned video format description
142 struct xilinx_frmbuf_chan {
143 struct xilinx_frmbuf_device *xdev;
144 /* Descriptor operation lock */
146 struct list_head chan_node;
147 struct list_head pending_list;
148 struct list_head done_list;
149 struct xilinx_frmbuf_tx_descriptor *staged_desc;
150 struct xilinx_frmbuf_tx_descriptor *active_desc;
151 struct dma_chan common;
153 void (*write_addr)(struct xilinx_frmbuf_chan *chan, u32 reg,
156 enum dma_transfer_direction direction;
158 struct tasklet_struct tasklet;
159 const struct xilinx_frmbuf_format_desc *vid_fmt;
163 * struct xilinx_frmbuf_format_desc - lookup table to match fourcc to format
164 * @dts_name: Device tree name for this entry.
166 * @bpw: Bits of pixel data + padding in a 32-bit word (luma plane for semi-pl)
167 * @ppw: Number of pixels represented in a 32-bit word (luma plane for semi-pl)
168 * @num_planes: Expected number of plane buffers in framebuffer for this format
169 * @drm_fmt: DRM video framework equivalent fourcc code
170 * @v4l2_fmt: Video 4 Linux framework equivalent fourcc code
171 * @fmt_bitmask: Flag identifying this format in device-specific "enabled"
174 struct xilinx_frmbuf_format_desc {
175 const char *dts_name;
185 static LIST_HEAD(frmbuf_chan_list);
186 static DEFINE_MUTEX(frmbuf_chan_list_lock);
188 static const struct xilinx_frmbuf_format_desc xilinx_frmbuf_formats[] = {
190 .dts_name = "xbgr8888",
191 .id = XILINX_FRMBUF_FMT_RGBX8,
195 .drm_fmt = DRM_FORMAT_XBGR8888,
196 .v4l2_fmt = V4L2_PIX_FMT_BGRX32,
197 .fmt_bitmask = BIT(0),
200 .dts_name = "xbgr2101010",
201 .id = XILINX_FRMBUF_FMT_RGBX10,
205 .drm_fmt = DRM_FORMAT_XBGR2101010,
206 .v4l2_fmt = V4L2_PIX_FMT_XBGR30,
207 .fmt_bitmask = BIT(1),
210 .dts_name = "xrgb8888",
211 .id = XILINX_FRMBUF_FMT_BGRX8,
215 .drm_fmt = DRM_FORMAT_XRGB8888,
216 .v4l2_fmt = V4L2_PIX_FMT_XBGR32,
217 .fmt_bitmask = BIT(2),
220 .dts_name = "xvuy8888",
221 .id = XILINX_FRMBUF_FMT_YUVX8,
225 .drm_fmt = DRM_FORMAT_XVUY8888,
226 .v4l2_fmt = V4L2_PIX_FMT_XVUY32,
227 .fmt_bitmask = BIT(5),
230 .dts_name = "vuy888",
231 .id = XILINX_FRMBUF_FMT_YUV8,
235 .drm_fmt = DRM_FORMAT_VUY888,
236 .v4l2_fmt = V4L2_PIX_FMT_VUY24,
237 .fmt_bitmask = BIT(6),
240 .dts_name = "yuvx2101010",
241 .id = XILINX_FRMBUF_FMT_YUVX10,
245 .drm_fmt = DRM_FORMAT_XVUY2101010,
246 .v4l2_fmt = V4L2_PIX_FMT_XVUY10,
247 .fmt_bitmask = BIT(7),
251 .id = XILINX_FRMBUF_FMT_YUYV8,
255 .drm_fmt = DRM_FORMAT_YUYV,
256 .v4l2_fmt = V4L2_PIX_FMT_YUYV,
257 .fmt_bitmask = BIT(8),
261 .id = XILINX_FRMBUF_FMT_UYVY8,
265 .drm_fmt = DRM_FORMAT_UYVY,
266 .v4l2_fmt = V4L2_PIX_FMT_UYVY,
267 .fmt_bitmask = BIT(9),
271 .id = XILINX_FRMBUF_FMT_Y_UV8,
275 .drm_fmt = DRM_FORMAT_NV16,
276 .v4l2_fmt = V4L2_PIX_FMT_NV16M,
277 .fmt_bitmask = BIT(11),
281 .id = XILINX_FRMBUF_FMT_Y_UV8,
286 .v4l2_fmt = V4L2_PIX_FMT_NV16,
287 .fmt_bitmask = BIT(11),
291 .id = XILINX_FRMBUF_FMT_Y_UV8_420,
295 .drm_fmt = DRM_FORMAT_NV12,
296 .v4l2_fmt = V4L2_PIX_FMT_NV12M,
297 .fmt_bitmask = BIT(12),
301 .id = XILINX_FRMBUF_FMT_Y_UV8_420,
306 .v4l2_fmt = V4L2_PIX_FMT_NV12,
307 .fmt_bitmask = BIT(12),
311 .id = XILINX_FRMBUF_FMT_Y_UV10_420,
315 .drm_fmt = DRM_FORMAT_XV15,
316 .v4l2_fmt = V4L2_PIX_FMT_XV15M,
317 .fmt_bitmask = BIT(13),
321 .id = XILINX_FRMBUF_FMT_Y_UV10,
325 .drm_fmt = DRM_FORMAT_XV20,
326 .v4l2_fmt = V4L2_PIX_FMT_XV20M,
327 .fmt_bitmask = BIT(14),
330 .dts_name = "bgr888",
331 .id = XILINX_FRMBUF_FMT_RGB8,
335 .drm_fmt = DRM_FORMAT_BGR888,
336 .v4l2_fmt = V4L2_PIX_FMT_RGB24,
337 .fmt_bitmask = BIT(15),
341 .id = XILINX_FRMBUF_FMT_Y8,
345 .drm_fmt = DRM_FORMAT_Y8,
346 .v4l2_fmt = V4L2_PIX_FMT_GREY,
347 .fmt_bitmask = BIT(16),
351 .id = XILINX_FRMBUF_FMT_Y10,
355 .drm_fmt = DRM_FORMAT_Y10,
356 .v4l2_fmt = V4L2_PIX_FMT_Y10,
357 .fmt_bitmask = BIT(17),
360 .dts_name = "rgb888",
361 .id = XILINX_FRMBUF_FMT_BGR8,
365 .drm_fmt = DRM_FORMAT_RGB888,
366 .v4l2_fmt = V4L2_PIX_FMT_BGR24,
367 .fmt_bitmask = BIT(18),
372 * struct xilinx_frmbuf_device - dma device structure
373 * @regs: I/O mapped base address
374 * @dev: Device Structure
375 * @common: DMA device structure
376 * @chan: Driver specific dma channel
377 * @rst_gpio: GPIO reset
378 * @enabled_vid_fmts: Bitmask of video formats enabled in hardware
379 * @drm_memory_fmts: Array of supported DRM fourcc codes
380 * @drm_fmt_cnt: Count of supported DRM fourcc codes
381 * @v4l2_memory_fmts: Array of supported V4L2 fourcc codes
382 * @v4l2_fmt_cnt: Count of supported V4L2 fourcc codes
384 struct xilinx_frmbuf_device {
387 struct dma_device common;
388 struct xilinx_frmbuf_chan chan;
389 struct gpio_desc *rst_gpio;
390 u32 enabled_vid_fmts;
391 u32 drm_memory_fmts[ARRAY_SIZE(xilinx_frmbuf_formats)];
393 u32 v4l2_memory_fmts[ARRAY_SIZE(xilinx_frmbuf_formats)];
397 static const struct of_device_id xilinx_frmbuf_of_ids[] = {
398 { .compatible = "xlnx,axi-frmbuf-wr-v2",
399 .data = (void *)DMA_DEV_TO_MEM},
400 { .compatible = "xlnx,axi-frmbuf-rd-v2",
401 .data = (void *)DMA_MEM_TO_DEV},
405 /******************************PROTOTYPES*************************************/
406 #define to_xilinx_chan(chan) \
407 container_of(chan, struct xilinx_frmbuf_chan, common)
408 #define to_dma_tx_descriptor(tx) \
409 container_of(tx, struct xilinx_frmbuf_tx_descriptor, async_tx)
411 static inline u32 frmbuf_read(struct xilinx_frmbuf_chan *chan, u32 reg)
413 return ioread32(chan->xdev->regs + reg);
416 static inline void frmbuf_write(struct xilinx_frmbuf_chan *chan, u32 reg,
419 iowrite32(value, chan->xdev->regs + reg);
422 static inline void frmbuf_writeq(struct xilinx_frmbuf_chan *chan, u32 reg,
425 iowrite32(lower_32_bits(value), chan->xdev->regs + reg);
426 iowrite32(upper_32_bits(value), chan->xdev->regs + reg + 4);
429 static void writeq_addr(struct xilinx_frmbuf_chan *chan, u32 reg,
432 frmbuf_writeq(chan, reg, (u64)addr);
435 static void write_addr(struct xilinx_frmbuf_chan *chan, u32 reg,
438 frmbuf_write(chan, reg, addr);
441 static inline void frmbuf_clr(struct xilinx_frmbuf_chan *chan, u32 reg,
444 frmbuf_write(chan, reg, frmbuf_read(chan, reg) & ~clr);
447 static inline void frmbuf_set(struct xilinx_frmbuf_chan *chan, u32 reg,
450 frmbuf_write(chan, reg, frmbuf_read(chan, reg) | set);
453 static void frmbuf_init_format_array(struct xilinx_frmbuf_device *xdev)
457 for (i = 0; i < ARRAY_SIZE(xilinx_frmbuf_formats); i++) {
458 if (!(xdev->enabled_vid_fmts &
459 xilinx_frmbuf_formats[i].fmt_bitmask))
462 if (xilinx_frmbuf_formats[i].drm_fmt) {
463 cnt = xdev->drm_fmt_cnt++;
464 xdev->drm_memory_fmts[cnt] =
465 xilinx_frmbuf_formats[i].drm_fmt;
468 if (xilinx_frmbuf_formats[i].v4l2_fmt) {
469 cnt = xdev->v4l2_fmt_cnt++;
470 xdev->v4l2_memory_fmts[cnt] =
471 xilinx_frmbuf_formats[i].v4l2_fmt;
476 static struct xilinx_frmbuf_device *frmbuf_find_dev(struct dma_chan *chan)
478 struct xilinx_frmbuf_chan *xchan, *temp;
479 struct xilinx_frmbuf_device *xdev;
480 bool is_frmbuf_chan = false;
482 list_for_each_entry_safe(xchan, temp, &frmbuf_chan_list, chan_node) {
483 if (chan == &xchan->common)
484 is_frmbuf_chan = true;
488 return ERR_PTR(-ENODEV);
490 xchan = to_xilinx_chan(chan);
491 xdev = container_of(xchan, struct xilinx_frmbuf_device, chan);
496 static int frmbuf_verify_format(struct dma_chan *chan, u32 fourcc, u32 type)
498 struct xilinx_frmbuf_chan *xil_chan = to_xilinx_chan(chan);
499 u32 i, sz = ARRAY_SIZE(xilinx_frmbuf_formats);
501 for (i = 0; i < sz; i++) {
502 if ((type == XDMA_DRM &&
503 fourcc != xilinx_frmbuf_formats[i].drm_fmt) ||
504 (type == XDMA_V4L2 &&
505 fourcc != xilinx_frmbuf_formats[i].v4l2_fmt))
508 if (!(xilinx_frmbuf_formats[i].fmt_bitmask &
509 xil_chan->xdev->enabled_vid_fmts))
512 xil_chan->vid_fmt = &xilinx_frmbuf_formats[i];
518 static void xilinx_xdma_set_config(struct dma_chan *chan, u32 fourcc, u32 type)
520 struct xilinx_frmbuf_chan *xil_chan;
521 bool found_xchan = false;
524 mutex_lock(&frmbuf_chan_list_lock);
525 list_for_each_entry(xil_chan, &frmbuf_chan_list, chan_node) {
526 if (chan == &xil_chan->common) {
531 mutex_unlock(&frmbuf_chan_list_lock);
534 dev_dbg(chan->device->dev,
535 "dma chan not a Video Framebuffer channel instance\n");
539 ret = frmbuf_verify_format(chan, fourcc, type);
540 if (ret == -EINVAL) {
541 dev_err(chan->device->dev,
542 "Framebuffer not configured for fourcc 0x%x\n",
548 void xilinx_xdma_drm_config(struct dma_chan *chan, u32 drm_fourcc)
550 xilinx_xdma_set_config(chan, drm_fourcc, XDMA_DRM);
552 } EXPORT_SYMBOL_GPL(xilinx_xdma_drm_config);
554 void xilinx_xdma_v4l2_config(struct dma_chan *chan, u32 v4l2_fourcc)
556 xilinx_xdma_set_config(chan, v4l2_fourcc, XDMA_V4L2);
558 } EXPORT_SYMBOL_GPL(xilinx_xdma_v4l2_config);
560 int xilinx_xdma_get_drm_vid_fmts(struct dma_chan *chan, u32 *fmt_cnt,
563 struct xilinx_frmbuf_device *xdev;
565 xdev = frmbuf_find_dev(chan);
568 return PTR_ERR(xdev);
570 *fmt_cnt = xdev->drm_fmt_cnt;
571 *fmts = xdev->drm_memory_fmts;
575 EXPORT_SYMBOL(xilinx_xdma_get_drm_vid_fmts);
577 int xilinx_xdma_get_v4l2_vid_fmts(struct dma_chan *chan, u32 *fmt_cnt,
580 struct xilinx_frmbuf_device *xdev;
582 xdev = frmbuf_find_dev(chan);
585 return PTR_ERR(xdev);
587 *fmt_cnt = xdev->v4l2_fmt_cnt;
588 *fmts = xdev->v4l2_memory_fmts;
592 EXPORT_SYMBOL(xilinx_xdma_get_v4l2_vid_fmts);
595 * of_dma_xilinx_xlate - Translation function
596 * @dma_spec: Pointer to DMA specifier as found in the device tree
597 * @ofdma: Pointer to DMA controller data
599 * Return: DMA channel pointer on success or error code on error
601 static struct dma_chan *of_dma_xilinx_xlate(struct of_phandle_args *dma_spec,
602 struct of_dma *ofdma)
604 struct xilinx_frmbuf_device *xdev = ofdma->of_dma_data;
606 return dma_get_slave_channel(&xdev->chan.common);
609 /* -----------------------------------------------------------------------------
610 * Descriptors alloc and free
614 * xilinx_frmbuf_tx_descriptor - Allocate transaction descriptor
615 * @chan: Driver specific dma channel
617 * Return: The allocated descriptor on success and NULL on failure.
619 static struct xilinx_frmbuf_tx_descriptor *
620 xilinx_frmbuf_alloc_tx_descriptor(struct xilinx_frmbuf_chan *chan)
622 struct xilinx_frmbuf_tx_descriptor *desc;
624 desc = kzalloc(sizeof(*desc), GFP_KERNEL);
632 * xilinx_frmbuf_free_desc_list - Free descriptors list
633 * @chan: Driver specific dma channel
634 * @list: List to parse and delete the descriptor
636 static void xilinx_frmbuf_free_desc_list(struct xilinx_frmbuf_chan *chan,
637 struct list_head *list)
639 struct xilinx_frmbuf_tx_descriptor *desc, *next;
641 list_for_each_entry_safe(desc, next, list, node) {
642 list_del(&desc->node);
648 * xilinx_frmbuf_free_descriptors - Free channel descriptors
649 * @chan: Driver specific dma channel
651 static void xilinx_frmbuf_free_descriptors(struct xilinx_frmbuf_chan *chan)
655 spin_lock_irqsave(&chan->lock, flags);
657 xilinx_frmbuf_free_desc_list(chan, &chan->pending_list);
658 xilinx_frmbuf_free_desc_list(chan, &chan->done_list);
659 kfree(chan->active_desc);
660 kfree(chan->staged_desc);
662 chan->staged_desc = NULL;
663 chan->active_desc = NULL;
664 INIT_LIST_HEAD(&chan->pending_list);
665 INIT_LIST_HEAD(&chan->done_list);
667 spin_unlock_irqrestore(&chan->lock, flags);
671 * xilinx_frmbuf_free_chan_resources - Free channel resources
672 * @dchan: DMA channel
674 static void xilinx_frmbuf_free_chan_resources(struct dma_chan *dchan)
676 struct xilinx_frmbuf_chan *chan = to_xilinx_chan(dchan);
678 xilinx_frmbuf_free_descriptors(chan);
682 * xilinx_frmbuf_chan_desc_cleanup - Clean channel descriptors
683 * @chan: Driver specific dma channel
685 static void xilinx_frmbuf_chan_desc_cleanup(struct xilinx_frmbuf_chan *chan)
687 struct xilinx_frmbuf_tx_descriptor *desc, *next;
690 spin_lock_irqsave(&chan->lock, flags);
692 list_for_each_entry_safe(desc, next, &chan->done_list, node) {
693 dma_async_tx_callback callback;
694 void *callback_param;
696 list_del(&desc->node);
698 /* Run the link descriptor callback function */
699 callback = desc->async_tx.callback;
700 callback_param = desc->async_tx.callback_param;
702 spin_unlock_irqrestore(&chan->lock, flags);
703 callback(callback_param);
704 spin_lock_irqsave(&chan->lock, flags);
707 /* Run any dependencies, then free the descriptor */
708 dma_run_dependencies(&desc->async_tx);
712 spin_unlock_irqrestore(&chan->lock, flags);
716 * xilinx_frmbuf_do_tasklet - Schedule completion tasklet
717 * @data: Pointer to the Xilinx frmbuf channel structure
719 static void xilinx_frmbuf_do_tasklet(unsigned long data)
721 struct xilinx_frmbuf_chan *chan = (struct xilinx_frmbuf_chan *)data;
723 xilinx_frmbuf_chan_desc_cleanup(chan);
727 * xilinx_frmbuf_alloc_chan_resources - Allocate channel resources
728 * @dchan: DMA channel
730 * Return: '0' on success and failure value on error
732 static int xilinx_frmbuf_alloc_chan_resources(struct dma_chan *dchan)
734 dma_cookie_init(dchan);
740 * xilinx_frmbuf_tx_status - Get frmbuf transaction status
741 * @dchan: DMA channel
742 * @cookie: Transaction identifier
743 * @txstate: Transaction state
745 * Return: fmrbuf transaction status
747 static enum dma_status xilinx_frmbuf_tx_status(struct dma_chan *dchan,
749 struct dma_tx_state *txstate)
751 return dma_cookie_status(dchan, cookie, txstate);
755 * xilinx_frmbuf_halt - Halt frmbuf channel
756 * @chan: Driver specific dma channel
758 static void xilinx_frmbuf_halt(struct xilinx_frmbuf_chan *chan)
760 frmbuf_clr(chan, XILINX_FRMBUF_CTRL_OFFSET,
761 XILINX_FRMBUF_CTRL_AP_START |
762 XILINX_FRMBUF_CTRL_AUTO_RESTART);
767 * xilinx_frmbuf_start - Start dma channel
768 * @chan: Driver specific dma channel
770 static void xilinx_frmbuf_start(struct xilinx_frmbuf_chan *chan)
772 frmbuf_set(chan, XILINX_FRMBUF_CTRL_OFFSET,
773 XILINX_FRMBUF_CTRL_AP_START |
774 XILINX_FRMBUF_CTRL_AUTO_RESTART);
779 * xilinx_frmbuf_complete_descriptor - Mark the active descriptor as complete
780 * This function is invoked with spinlock held
781 * @chan : xilinx frmbuf channel
785 static void xilinx_frmbuf_complete_descriptor(struct xilinx_frmbuf_chan *chan)
787 struct xilinx_frmbuf_tx_descriptor *desc = chan->active_desc;
789 dma_cookie_complete(&desc->async_tx);
790 list_add_tail(&desc->node, &chan->done_list);
794 * xilinx_frmbuf_start_transfer - Starts frmbuf transfer
795 * @chan: Driver specific channel struct pointer
797 static void xilinx_frmbuf_start_transfer(struct xilinx_frmbuf_chan *chan)
799 struct xilinx_frmbuf_tx_descriptor *desc;
804 if (chan->active_desc) {
805 xilinx_frmbuf_complete_descriptor(chan);
806 chan->active_desc = NULL;
809 if (chan->staged_desc) {
810 chan->active_desc = chan->staged_desc;
811 chan->staged_desc = NULL;
814 if (list_empty(&chan->pending_list))
817 desc = list_first_entry(&chan->pending_list,
818 struct xilinx_frmbuf_tx_descriptor,
821 /* Start the transfer */
822 chan->write_addr(chan, XILINX_FRMBUF_ADDR_OFFSET,
823 desc->hw.luma_plane_addr);
824 chan->write_addr(chan, XILINX_FRMBUF_ADDR2_OFFSET,
825 desc->hw.chroma_plane_addr);
827 /* HW expects these parameters to be same for one transaction */
828 frmbuf_write(chan, XILINX_FRMBUF_WIDTH_OFFSET, desc->hw.hsize);
829 frmbuf_write(chan, XILINX_FRMBUF_STRIDE_OFFSET, desc->hw.stride);
830 frmbuf_write(chan, XILINX_FRMBUF_HEIGHT_OFFSET, desc->hw.vsize);
831 frmbuf_write(chan, XILINX_FRMBUF_FMT_OFFSET, chan->vid_fmt->id);
833 /* Start the hardware */
834 xilinx_frmbuf_start(chan);
835 list_del(&desc->node);
836 chan->staged_desc = desc;
840 * xilinx_frmbuf_issue_pending - Issue pending transactions
841 * @dchan: DMA channel
843 static void xilinx_frmbuf_issue_pending(struct dma_chan *dchan)
845 struct xilinx_frmbuf_chan *chan = to_xilinx_chan(dchan);
848 spin_lock_irqsave(&chan->lock, flags);
849 xilinx_frmbuf_start_transfer(chan);
850 spin_unlock_irqrestore(&chan->lock, flags);
854 * xilinx_frmbuf_reset - Reset frmbuf channel
855 * @chan: Driver specific dma channel
857 static void xilinx_frmbuf_reset(struct xilinx_frmbuf_chan *chan)
860 gpiod_set_value(chan->xdev->rst_gpio, 1);
862 gpiod_set_value(chan->xdev->rst_gpio, 0);
866 * xilinx_frmbuf_chan_reset - Reset frmbuf channel and enable interrupts
867 * @chan: Driver specific frmbuf channel
869 static void xilinx_frmbuf_chan_reset(struct xilinx_frmbuf_chan *chan)
871 xilinx_frmbuf_reset(chan);
872 frmbuf_write(chan, XILINX_FRMBUF_IE_OFFSET, XILINX_FRMBUF_IE_AP_READY);
873 frmbuf_write(chan, XILINX_FRMBUF_GIE_OFFSET, XILINX_FRMBUF_GIE_EN);
877 * xilinx_frmbuf_irq_handler - frmbuf Interrupt handler
879 * @data: Pointer to the Xilinx frmbuf channel structure
881 * Return: IRQ_HANDLED/IRQ_NONE
883 static irqreturn_t xilinx_frmbuf_irq_handler(int irq, void *data)
885 struct xilinx_frmbuf_chan *chan = data;
888 status = frmbuf_read(chan, XILINX_FRMBUF_ISR_OFFSET);
889 if (!(status & XILINX_FRMBUF_ISR_ALL_IRQ_MASK))
892 frmbuf_write(chan, XILINX_FRMBUF_ISR_OFFSET,
893 status & XILINX_FRMBUF_ISR_ALL_IRQ_MASK);
895 if (status & XILINX_FRMBUF_ISR_AP_READY_IRQ) {
896 spin_lock(&chan->lock);
898 xilinx_frmbuf_start_transfer(chan);
899 spin_unlock(&chan->lock);
902 tasklet_schedule(&chan->tasklet);
907 * xilinx_frmbuf_tx_submit - Submit DMA transaction
908 * @tx: Async transaction descriptor
910 * Return: cookie value on success and failure value on error
912 static dma_cookie_t xilinx_frmbuf_tx_submit(struct dma_async_tx_descriptor *tx)
914 struct xilinx_frmbuf_tx_descriptor *desc = to_dma_tx_descriptor(tx);
915 struct xilinx_frmbuf_chan *chan = to_xilinx_chan(tx->chan);
919 spin_lock_irqsave(&chan->lock, flags);
920 cookie = dma_cookie_assign(tx);
921 list_add_tail(&desc->node, &chan->pending_list);
922 spin_unlock_irqrestore(&chan->lock, flags);
928 * xilinx_frmbuf_dma_prep_interleaved - prepare a descriptor for a
929 * DMA_SLAVE transaction
930 * @dchan: DMA channel
931 * @xt: Interleaved template pointer
932 * @flags: transfer ack flags
934 * Return: Async transaction descriptor on success and NULL on failure
936 static struct dma_async_tx_descriptor *
937 xilinx_frmbuf_dma_prep_interleaved(struct dma_chan *dchan,
938 struct dma_interleaved_template *xt,
941 struct xilinx_frmbuf_chan *chan = to_xilinx_chan(dchan);
942 struct xilinx_frmbuf_tx_descriptor *desc;
943 struct xilinx_frmbuf_desc_hw *hw;
945 if (chan->direction != xt->dir || !chan->vid_fmt)
948 if (!xt->numf || !xt->sgl[0].size)
951 if (xt->frame_size != chan->vid_fmt->num_planes)
954 desc = xilinx_frmbuf_alloc_tx_descriptor(chan);
958 dma_async_tx_descriptor_init(&desc->async_tx, &chan->common);
959 desc->async_tx.tx_submit = xilinx_frmbuf_tx_submit;
960 async_tx_ack(&desc->async_tx);
963 hw->vsize = xt->numf;
964 hw->stride = xt->sgl[0].icg + xt->sgl[0].size;
965 hw->hsize = (xt->sgl[0].size * chan->vid_fmt->ppw * 8) /
968 /* hsize calc should not have resulted in an odd number */
972 if (chan->direction == DMA_MEM_TO_DEV) {
973 hw->luma_plane_addr = xt->src_start;
974 if (xt->frame_size == 2)
975 hw->chroma_plane_addr =
977 xt->numf * hw->stride +
980 hw->luma_plane_addr = xt->dst_start;
981 if (xt->frame_size == 2)
982 hw->chroma_plane_addr =
984 xt->numf * hw->stride +
988 return &desc->async_tx;
991 dev_err(chan->xdev->dev,
992 "Invalid dma template or missing dma video fmt config\n");
997 * xilinx_frmbuf_terminate_all - Halt the channel and free descriptors
998 * @dchan: Driver specific dma channel pointer
1002 static int xilinx_frmbuf_terminate_all(struct dma_chan *dchan)
1004 struct xilinx_frmbuf_chan *chan = to_xilinx_chan(dchan);
1006 xilinx_frmbuf_halt(chan);
1007 xilinx_frmbuf_free_descriptors(chan);
1008 /* worst case frame-to-frame boundary; ensure frame output complete */
1010 xilinx_frmbuf_chan_reset(chan);
1016 * xilinx_frmbuf_synchronize - kill tasklet to stop further descr processing
1017 * @dchan: Driver specific dma channel pointer
1019 static void xilinx_frmbuf_synchronize(struct dma_chan *dchan)
1021 struct xilinx_frmbuf_chan *chan = to_xilinx_chan(dchan);
1023 tasklet_kill(&chan->tasklet);
1026 /* -----------------------------------------------------------------------------
1031 * xilinx_frmbuf_chan_remove - Per Channel remove function
1032 * @chan: Driver specific dma channel
1034 static void xilinx_frmbuf_chan_remove(struct xilinx_frmbuf_chan *chan)
1036 /* Disable all interrupts */
1037 frmbuf_clr(chan, XILINX_FRMBUF_IE_OFFSET,
1038 XILINX_FRMBUF_ISR_ALL_IRQ_MASK);
1040 tasklet_kill(&chan->tasklet);
1041 list_del(&chan->common.device_node);
1043 mutex_lock(&frmbuf_chan_list_lock);
1044 list_del(&chan->chan_node);
1045 mutex_unlock(&frmbuf_chan_list_lock);
1049 * xilinx_frmbuf_chan_probe - Per Channel Probing
1050 * It get channel features from the device tree entry and
1051 * initialize special channel handling routines
1053 * @xdev: Driver specific device structure
1054 * @node: Device node
1056 * Return: '0' on success and failure value on error
1058 static int xilinx_frmbuf_chan_probe(struct xilinx_frmbuf_device *xdev,
1059 struct device_node *node)
1061 struct xilinx_frmbuf_chan *chan;
1067 chan->dev = xdev->dev;
1071 err = of_property_read_u32(node, "xlnx,dma-addr-width",
1073 if (err || (dma_addr_size != 32 && dma_addr_size != 64)) {
1074 dev_err(xdev->dev, "missing or invalid addr width dts prop\n");
1078 if (dma_addr_size == 64 && sizeof(dma_addr_t) == sizeof(u64))
1079 chan->write_addr = writeq_addr;
1081 chan->write_addr = write_addr;
1083 spin_lock_init(&chan->lock);
1084 INIT_LIST_HEAD(&chan->pending_list);
1085 INIT_LIST_HEAD(&chan->done_list);
1087 chan->irq = irq_of_parse_and_map(node, 0);
1088 err = devm_request_irq(xdev->dev, chan->irq, xilinx_frmbuf_irq_handler,
1089 IRQF_SHARED, "xilinx_framebuffer", chan);
1092 dev_err(xdev->dev, "unable to request IRQ %d\n", chan->irq);
1096 tasklet_init(&chan->tasklet, xilinx_frmbuf_do_tasklet,
1097 (unsigned long)chan);
1100 * Initialize the DMA channel and add it to the DMA engine channels
1103 chan->common.device = &xdev->common;
1105 list_add_tail(&chan->common.device_node, &xdev->common.channels);
1107 mutex_lock(&frmbuf_chan_list_lock);
1108 list_add_tail(&chan->chan_node, &frmbuf_chan_list);
1109 mutex_unlock(&frmbuf_chan_list_lock);
1111 xilinx_frmbuf_chan_reset(chan);
1117 * xilinx_frmbuf_probe - Driver probe function
1118 * @pdev: Pointer to the platform_device structure
1120 * Return: '0' on success and failure value on error
1122 static int xilinx_frmbuf_probe(struct platform_device *pdev)
1124 struct device_node *node = pdev->dev.of_node;
1125 struct xilinx_frmbuf_device *xdev;
1126 struct resource *io;
1127 enum dma_transfer_direction dma_dir;
1128 const struct of_device_id *match;
1132 const char *vid_fmts[ARRAY_SIZE(xilinx_frmbuf_formats)];
1134 xdev = devm_kzalloc(&pdev->dev, sizeof(*xdev), GFP_KERNEL);
1138 xdev->dev = &pdev->dev;
1140 match = of_match_node(xilinx_frmbuf_of_ids, node);
1144 dma_dir = (enum dma_transfer_direction)match->data;
1146 xdev->rst_gpio = devm_gpiod_get(&pdev->dev, "reset",
1148 if (IS_ERR(xdev->rst_gpio)) {
1149 err = PTR_ERR(xdev->rst_gpio);
1150 if (err == -EPROBE_DEFER)
1151 dev_info(&pdev->dev,
1152 "Probe deferred due to GPIO reset defer\n");
1155 "Unable to locate reset property in dt\n");
1159 gpiod_set_value_cansleep(xdev->rst_gpio, 0x0);
1161 io = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1162 xdev->regs = devm_ioremap_resource(&pdev->dev, io);
1163 if (IS_ERR(xdev->regs))
1164 return PTR_ERR(xdev->regs);
1166 /* Initialize the DMA engine */
1167 /* TODO: Get DMA alignment from device tree property */
1168 xdev->common.copy_align = 4;
1169 xdev->common.dev = &pdev->dev;
1171 INIT_LIST_HEAD(&xdev->common.channels);
1172 dma_cap_set(DMA_SLAVE, xdev->common.cap_mask);
1173 dma_cap_set(DMA_PRIVATE, xdev->common.cap_mask);
1175 /* Initialize the channels */
1176 err = xilinx_frmbuf_chan_probe(xdev, node);
1180 xdev->chan.direction = dma_dir;
1182 if (xdev->chan.direction == DMA_DEV_TO_MEM) {
1183 xdev->common.directions = BIT(DMA_DEV_TO_MEM);
1184 dev_info(&pdev->dev, "Xilinx AXI frmbuf DMA_DEV_TO_MEM\n");
1185 } else if (xdev->chan.direction == DMA_MEM_TO_DEV) {
1186 xdev->common.directions = BIT(DMA_MEM_TO_DEV);
1187 dev_info(&pdev->dev, "Xilinx AXI frmbuf DMA_MEM_TO_DEV\n");
1189 xilinx_frmbuf_chan_remove(&xdev->chan);
1193 /* read supported video formats and update internal table */
1194 hw_vid_fmt_cnt = of_property_count_strings(node, "xlnx,vid-formats");
1196 err = of_property_read_string_array(node, "xlnx,vid-formats",
1197 vid_fmts, hw_vid_fmt_cnt);
1200 "Missing or invalid xlnx,vid-formats dts prop\n");
1204 for (i = 0; i < hw_vid_fmt_cnt; i++) {
1205 const char *vid_fmt_name = vid_fmts[i];
1207 for (j = 0; j < ARRAY_SIZE(xilinx_frmbuf_formats); j++) {
1208 const char *dts_name =
1209 xilinx_frmbuf_formats[j].dts_name;
1211 if (strcmp(vid_fmt_name, dts_name))
1214 xdev->enabled_vid_fmts |=
1215 xilinx_frmbuf_formats[j].fmt_bitmask;
1219 /* Determine supported vid framework formats */
1220 frmbuf_init_format_array(xdev);
1222 xdev->common.device_alloc_chan_resources =
1223 xilinx_frmbuf_alloc_chan_resources;
1224 xdev->common.device_free_chan_resources =
1225 xilinx_frmbuf_free_chan_resources;
1226 xdev->common.device_prep_interleaved_dma =
1227 xilinx_frmbuf_dma_prep_interleaved;
1228 xdev->common.device_terminate_all = xilinx_frmbuf_terminate_all;
1229 xdev->common.device_synchronize = xilinx_frmbuf_synchronize;
1230 xdev->common.device_tx_status = xilinx_frmbuf_tx_status;
1231 xdev->common.device_issue_pending = xilinx_frmbuf_issue_pending;
1233 platform_set_drvdata(pdev, xdev);
1235 /* Register the DMA engine with the core */
1236 dma_async_device_register(&xdev->common);
1237 err = of_dma_controller_register(node, of_dma_xilinx_xlate, xdev);
1240 dev_err(&pdev->dev, "Unable to register DMA to DT\n");
1241 xilinx_frmbuf_chan_remove(&xdev->chan);
1242 dma_async_device_unregister(&xdev->common);
1246 dev_info(&pdev->dev, "Xilinx AXI FrameBuffer Engine Driver Probed!!\n");
1252 * xilinx_frmbuf_remove - Driver remove function
1253 * @pdev: Pointer to the platform_device structure
1255 * Return: Always '0'
1257 static int xilinx_frmbuf_remove(struct platform_device *pdev)
1259 struct xilinx_frmbuf_device *xdev = platform_get_drvdata(pdev);
1261 dma_async_device_unregister(&xdev->common);
1262 xilinx_frmbuf_chan_remove(&xdev->chan);
1267 MODULE_DEVICE_TABLE(of, xilinx_frmbuf_of_ids);
1269 static struct platform_driver xilinx_frmbuf_driver = {
1271 .name = "xilinx-frmbuf",
1272 .of_match_table = xilinx_frmbuf_of_ids,
1274 .probe = xilinx_frmbuf_probe,
1275 .remove = xilinx_frmbuf_remove,
1278 module_platform_driver(xilinx_frmbuf_driver);
1280 MODULE_AUTHOR("Xilinx, Inc.");
1281 MODULE_DESCRIPTION("Xilinx Framebuffer driver");
1282 MODULE_LICENSE("GPL v2");