174ba9207SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2f7018c21STomi Valkeinen /*
3f7018c21STomi Valkeinen * pxa3xx-gcu.c - Linux kernel module for PXA3xx graphics controllers
4f7018c21STomi Valkeinen *
5f7018c21STomi Valkeinen * This driver needs a DirectFB counterpart in user space, communication
6f7018c21STomi Valkeinen * is handled via mmap()ed memory areas and an ioctl.
7f7018c21STomi Valkeinen *
8f7018c21STomi Valkeinen * Copyright (c) 2009 Daniel Mack <daniel@caiaq.de>
9f7018c21STomi Valkeinen * Copyright (c) 2009 Janine Kropp <nin@directfb.org>
10f7018c21STomi Valkeinen * Copyright (c) 2009 Denis Oliver Kropp <dok@directfb.org>
11f7018c21STomi Valkeinen */
12f7018c21STomi Valkeinen
13f7018c21STomi Valkeinen /*
14f7018c21STomi Valkeinen * WARNING: This controller is attached to System Bus 2 of the PXA which
15f7018c21STomi Valkeinen * needs its arbiter to be enabled explicitly (CKENB & 1<<9).
16f7018c21STomi Valkeinen * There is currently no way to do this from Linux, so you need to teach
17f7018c21STomi Valkeinen * your bootloader for now.
18f7018c21STomi Valkeinen */
19f7018c21STomi Valkeinen
20f7018c21STomi Valkeinen #include <linux/module.h>
21f7018c21STomi Valkeinen #include <linux/platform_device.h>
22f7018c21STomi Valkeinen #include <linux/dma-mapping.h>
23f7018c21STomi Valkeinen #include <linux/miscdevice.h>
24f7018c21STomi Valkeinen #include <linux/interrupt.h>
25f7018c21STomi Valkeinen #include <linux/spinlock.h>
26f7018c21STomi Valkeinen #include <linux/uaccess.h>
27f7018c21STomi Valkeinen #include <linux/ioctl.h>
28f7018c21STomi Valkeinen #include <linux/delay.h>
29f7018c21STomi Valkeinen #include <linux/sched.h>
30f7018c21STomi Valkeinen #include <linux/slab.h>
31f7018c21STomi Valkeinen #include <linux/clk.h>
32f7018c21STomi Valkeinen #include <linux/fs.h>
33f7018c21STomi Valkeinen #include <linux/io.h>
34aa45ee8eSDaniel Mack #include <linux/of.h>
35f7018c21STomi Valkeinen
36f7018c21STomi Valkeinen #include "pxa3xx-gcu.h"
37f7018c21STomi Valkeinen
38f7018c21STomi Valkeinen #define DRV_NAME "pxa3xx-gcu"
39f7018c21STomi Valkeinen
40f7018c21STomi Valkeinen #define REG_GCCR 0x00
41f7018c21STomi Valkeinen #define GCCR_SYNC_CLR (1 << 9)
42f7018c21STomi Valkeinen #define GCCR_BP_RST (1 << 8)
43f7018c21STomi Valkeinen #define GCCR_ABORT (1 << 6)
44f7018c21STomi Valkeinen #define GCCR_STOP (1 << 4)
45f7018c21STomi Valkeinen
46f7018c21STomi Valkeinen #define REG_GCISCR 0x04
47f7018c21STomi Valkeinen #define REG_GCIECR 0x08
48f7018c21STomi Valkeinen #define REG_GCRBBR 0x20
49f7018c21STomi Valkeinen #define REG_GCRBLR 0x24
50f7018c21STomi Valkeinen #define REG_GCRBHR 0x28
51f7018c21STomi Valkeinen #define REG_GCRBTR 0x2C
52f7018c21STomi Valkeinen #define REG_GCRBEXHR 0x30
53f7018c21STomi Valkeinen
54f7018c21STomi Valkeinen #define IE_EOB (1 << 0)
55f7018c21STomi Valkeinen #define IE_EEOB (1 << 5)
56f7018c21STomi Valkeinen #define IE_ALL 0xff
57f7018c21STomi Valkeinen
58f7018c21STomi Valkeinen #define SHARED_SIZE PAGE_ALIGN(sizeof(struct pxa3xx_gcu_shared))
59f7018c21STomi Valkeinen
60f7018c21STomi Valkeinen /* #define PXA3XX_GCU_DEBUG */
61f7018c21STomi Valkeinen /* #define PXA3XX_GCU_DEBUG_TIMER */
62f7018c21STomi Valkeinen
63f7018c21STomi Valkeinen #ifdef PXA3XX_GCU_DEBUG
64f7018c21STomi Valkeinen #define QDUMP(msg) \
65f7018c21STomi Valkeinen do { \
66f7018c21STomi Valkeinen QPRINT(priv, KERN_DEBUG, msg); \
67f7018c21STomi Valkeinen } while (0)
68f7018c21STomi Valkeinen #else
69f7018c21STomi Valkeinen #define QDUMP(msg) do {} while (0)
70f7018c21STomi Valkeinen #endif
71f7018c21STomi Valkeinen
72f7018c21STomi Valkeinen #define QERROR(msg) \
73f7018c21STomi Valkeinen do { \
74f7018c21STomi Valkeinen QPRINT(priv, KERN_ERR, msg); \
75f7018c21STomi Valkeinen } while (0)
76f7018c21STomi Valkeinen
77f7018c21STomi Valkeinen struct pxa3xx_gcu_batch {
78f7018c21STomi Valkeinen struct pxa3xx_gcu_batch *next;
79f7018c21STomi Valkeinen u32 *ptr;
80f7018c21STomi Valkeinen dma_addr_t phys;
81f7018c21STomi Valkeinen unsigned long length;
82f7018c21STomi Valkeinen };
83f7018c21STomi Valkeinen
84f7018c21STomi Valkeinen struct pxa3xx_gcu_priv {
8502c486f4SChristoph Hellwig struct device *dev;
86f7018c21STomi Valkeinen void __iomem *mmio_base;
87f7018c21STomi Valkeinen struct clk *clk;
88f7018c21STomi Valkeinen struct pxa3xx_gcu_shared *shared;
89f7018c21STomi Valkeinen dma_addr_t shared_phys;
90f7018c21STomi Valkeinen struct resource *resource_mem;
91f7018c21STomi Valkeinen struct miscdevice misc_dev;
92f7018c21STomi Valkeinen wait_queue_head_t wait_idle;
93f7018c21STomi Valkeinen wait_queue_head_t wait_free;
94f7018c21STomi Valkeinen spinlock_t spinlock;
95f7a75354SArnd Bergmann struct timespec64 base_time;
96f7018c21STomi Valkeinen
97f7018c21STomi Valkeinen struct pxa3xx_gcu_batch *free;
98f7018c21STomi Valkeinen struct pxa3xx_gcu_batch *ready;
99f7018c21STomi Valkeinen struct pxa3xx_gcu_batch *ready_last;
100f7018c21STomi Valkeinen struct pxa3xx_gcu_batch *running;
101f7018c21STomi Valkeinen };
102f7018c21STomi Valkeinen
103f7018c21STomi Valkeinen static inline unsigned long
gc_readl(struct pxa3xx_gcu_priv * priv,unsigned int off)104f7018c21STomi Valkeinen gc_readl(struct pxa3xx_gcu_priv *priv, unsigned int off)
105f7018c21STomi Valkeinen {
106f7018c21STomi Valkeinen return __raw_readl(priv->mmio_base + off);
107f7018c21STomi Valkeinen }
108f7018c21STomi Valkeinen
109f7018c21STomi Valkeinen static inline void
gc_writel(struct pxa3xx_gcu_priv * priv,unsigned int off,unsigned long val)110f7018c21STomi Valkeinen gc_writel(struct pxa3xx_gcu_priv *priv, unsigned int off, unsigned long val)
111f7018c21STomi Valkeinen {
112f7018c21STomi Valkeinen __raw_writel(val, priv->mmio_base + off);
113f7018c21STomi Valkeinen }
114f7018c21STomi Valkeinen
115f7018c21STomi Valkeinen #define QPRINT(priv, level, msg) \
116f7018c21STomi Valkeinen do { \
117f7a75354SArnd Bergmann struct timespec64 ts; \
118f7018c21STomi Valkeinen struct pxa3xx_gcu_shared *shared = priv->shared; \
119f7018c21STomi Valkeinen u32 base = gc_readl(priv, REG_GCRBBR); \
120f7018c21STomi Valkeinen \
121f7a75354SArnd Bergmann ktime_get_ts64(&ts); \
122f7a75354SArnd Bergmann ts = timespec64_sub(ts, priv->base_time); \
123f7018c21STomi Valkeinen \
124f7a75354SArnd Bergmann printk(level "%lld.%03ld.%03ld - %-17s: %-21s (%s, " \
125f7018c21STomi Valkeinen "STATUS " \
126f7018c21STomi Valkeinen "0x%02lx, B 0x%08lx [%ld], E %5ld, H %5ld, " \
127f7018c21STomi Valkeinen "T %5ld)\n", \
128f7a75354SArnd Bergmann (s64)(ts.tv_sec), \
129f7a75354SArnd Bergmann ts.tv_nsec / NSEC_PER_MSEC, \
130f7a75354SArnd Bergmann (ts.tv_nsec % NSEC_PER_MSEC) / USEC_PER_MSEC, \
131f7018c21STomi Valkeinen __func__, msg, \
132f7018c21STomi Valkeinen shared->hw_running ? "running" : " idle", \
133f7018c21STomi Valkeinen gc_readl(priv, REG_GCISCR), \
134f7018c21STomi Valkeinen gc_readl(priv, REG_GCRBBR), \
135f7018c21STomi Valkeinen gc_readl(priv, REG_GCRBLR), \
136f7018c21STomi Valkeinen (gc_readl(priv, REG_GCRBEXHR) - base) / 4, \
137f7018c21STomi Valkeinen (gc_readl(priv, REG_GCRBHR) - base) / 4, \
138f7018c21STomi Valkeinen (gc_readl(priv, REG_GCRBTR) - base) / 4); \
139f7018c21STomi Valkeinen } while (0)
140f7018c21STomi Valkeinen
141f7018c21STomi Valkeinen static void
pxa3xx_gcu_reset(struct pxa3xx_gcu_priv * priv)142f7018c21STomi Valkeinen pxa3xx_gcu_reset(struct pxa3xx_gcu_priv *priv)
143f7018c21STomi Valkeinen {
144f7018c21STomi Valkeinen QDUMP("RESET");
145f7018c21STomi Valkeinen
146f7018c21STomi Valkeinen /* disable interrupts */
147f7018c21STomi Valkeinen gc_writel(priv, REG_GCIECR, 0);
148f7018c21STomi Valkeinen
149f7018c21STomi Valkeinen /* reset hardware */
150f7018c21STomi Valkeinen gc_writel(priv, REG_GCCR, GCCR_ABORT);
151f7018c21STomi Valkeinen gc_writel(priv, REG_GCCR, 0);
152f7018c21STomi Valkeinen
153f7018c21STomi Valkeinen memset(priv->shared, 0, SHARED_SIZE);
154f7018c21STomi Valkeinen priv->shared->buffer_phys = priv->shared_phys;
155f7018c21STomi Valkeinen priv->shared->magic = PXA3XX_GCU_SHARED_MAGIC;
156f7018c21STomi Valkeinen
157f7a75354SArnd Bergmann ktime_get_ts64(&priv->base_time);
158f7018c21STomi Valkeinen
159f7018c21STomi Valkeinen /* set up the ring buffer pointers */
160f7018c21STomi Valkeinen gc_writel(priv, REG_GCRBLR, 0);
161f7018c21STomi Valkeinen gc_writel(priv, REG_GCRBBR, priv->shared_phys);
162f7018c21STomi Valkeinen gc_writel(priv, REG_GCRBTR, priv->shared_phys);
163f7018c21STomi Valkeinen
164f7018c21STomi Valkeinen /* enable all IRQs except EOB */
165f7018c21STomi Valkeinen gc_writel(priv, REG_GCIECR, IE_ALL & ~IE_EOB);
166f7018c21STomi Valkeinen }
167f7018c21STomi Valkeinen
168f7018c21STomi Valkeinen static void
dump_whole_state(struct pxa3xx_gcu_priv * priv)169f7018c21STomi Valkeinen dump_whole_state(struct pxa3xx_gcu_priv *priv)
170f7018c21STomi Valkeinen {
171f7018c21STomi Valkeinen struct pxa3xx_gcu_shared *sh = priv->shared;
172f7018c21STomi Valkeinen u32 base = gc_readl(priv, REG_GCRBBR);
173f7018c21STomi Valkeinen
174f7018c21STomi Valkeinen QDUMP("DUMP");
175f7018c21STomi Valkeinen
176f7018c21STomi Valkeinen printk(KERN_DEBUG "== PXA3XX-GCU DUMP ==\n"
177f7018c21STomi Valkeinen "%s, STATUS 0x%02lx, B 0x%08lx [%ld], E %5ld, H %5ld, T %5ld\n",
178f7018c21STomi Valkeinen sh->hw_running ? "running" : "idle ",
179f7018c21STomi Valkeinen gc_readl(priv, REG_GCISCR),
180f7018c21STomi Valkeinen gc_readl(priv, REG_GCRBBR),
181f7018c21STomi Valkeinen gc_readl(priv, REG_GCRBLR),
182f7018c21STomi Valkeinen (gc_readl(priv, REG_GCRBEXHR) - base) / 4,
183f7018c21STomi Valkeinen (gc_readl(priv, REG_GCRBHR) - base) / 4,
184f7018c21STomi Valkeinen (gc_readl(priv, REG_GCRBTR) - base) / 4);
185f7018c21STomi Valkeinen }
186f7018c21STomi Valkeinen
187f7018c21STomi Valkeinen static void
flush_running(struct pxa3xx_gcu_priv * priv)188f7018c21STomi Valkeinen flush_running(struct pxa3xx_gcu_priv *priv)
189f7018c21STomi Valkeinen {
190f7018c21STomi Valkeinen struct pxa3xx_gcu_batch *running = priv->running;
191f7018c21STomi Valkeinen struct pxa3xx_gcu_batch *next;
192f7018c21STomi Valkeinen
193f7018c21STomi Valkeinen while (running) {
194f7018c21STomi Valkeinen next = running->next;
195f7018c21STomi Valkeinen running->next = priv->free;
196f7018c21STomi Valkeinen priv->free = running;
197f7018c21STomi Valkeinen running = next;
198f7018c21STomi Valkeinen }
199f7018c21STomi Valkeinen
200f7018c21STomi Valkeinen priv->running = NULL;
201f7018c21STomi Valkeinen }
202f7018c21STomi Valkeinen
203f7018c21STomi Valkeinen static void
run_ready(struct pxa3xx_gcu_priv * priv)204f7018c21STomi Valkeinen run_ready(struct pxa3xx_gcu_priv *priv)
205f7018c21STomi Valkeinen {
206f7018c21STomi Valkeinen unsigned int num = 0;
207f7018c21STomi Valkeinen struct pxa3xx_gcu_shared *shared = priv->shared;
208f7018c21STomi Valkeinen struct pxa3xx_gcu_batch *ready = priv->ready;
209f7018c21STomi Valkeinen
210f7018c21STomi Valkeinen QDUMP("Start");
211f7018c21STomi Valkeinen
212f7018c21STomi Valkeinen BUG_ON(!ready);
213f7018c21STomi Valkeinen
214f7018c21STomi Valkeinen shared->buffer[num++] = 0x05000000;
215f7018c21STomi Valkeinen
216f7018c21STomi Valkeinen while (ready) {
217f7018c21STomi Valkeinen shared->buffer[num++] = 0x00000001;
218f7018c21STomi Valkeinen shared->buffer[num++] = ready->phys;
219f7018c21STomi Valkeinen ready = ready->next;
220f7018c21STomi Valkeinen }
221f7018c21STomi Valkeinen
222f7018c21STomi Valkeinen shared->buffer[num++] = 0x05000000;
223f7018c21STomi Valkeinen priv->running = priv->ready;
224f7018c21STomi Valkeinen priv->ready = priv->ready_last = NULL;
225f7018c21STomi Valkeinen gc_writel(priv, REG_GCRBLR, 0);
226f7018c21STomi Valkeinen shared->hw_running = 1;
227f7018c21STomi Valkeinen
228f7018c21STomi Valkeinen /* ring base address */
229f7018c21STomi Valkeinen gc_writel(priv, REG_GCRBBR, shared->buffer_phys);
230f7018c21STomi Valkeinen
231f7018c21STomi Valkeinen /* ring tail address */
232f7018c21STomi Valkeinen gc_writel(priv, REG_GCRBTR, shared->buffer_phys + num * 4);
233f7018c21STomi Valkeinen
234f7018c21STomi Valkeinen /* ring length */
235f7018c21STomi Valkeinen gc_writel(priv, REG_GCRBLR, ((num + 63) & ~63) * 4);
236f7018c21STomi Valkeinen }
237f7018c21STomi Valkeinen
238f7018c21STomi Valkeinen static irqreturn_t
pxa3xx_gcu_handle_irq(int irq,void * ctx)239f7018c21STomi Valkeinen pxa3xx_gcu_handle_irq(int irq, void *ctx)
240f7018c21STomi Valkeinen {
241f7018c21STomi Valkeinen struct pxa3xx_gcu_priv *priv = ctx;
242f7018c21STomi Valkeinen struct pxa3xx_gcu_shared *shared = priv->shared;
243f7018c21STomi Valkeinen u32 status = gc_readl(priv, REG_GCISCR) & IE_ALL;
244f7018c21STomi Valkeinen
245f7018c21STomi Valkeinen QDUMP("-Interrupt");
246f7018c21STomi Valkeinen
247f7018c21STomi Valkeinen if (!status)
248f7018c21STomi Valkeinen return IRQ_NONE;
249f7018c21STomi Valkeinen
250f7018c21STomi Valkeinen spin_lock(&priv->spinlock);
251f7018c21STomi Valkeinen shared->num_interrupts++;
252f7018c21STomi Valkeinen
253f7018c21STomi Valkeinen if (status & IE_EEOB) {
254f7018c21STomi Valkeinen QDUMP(" [EEOB]");
255f7018c21STomi Valkeinen
256f7018c21STomi Valkeinen flush_running(priv);
257f7018c21STomi Valkeinen wake_up_all(&priv->wait_free);
258f7018c21STomi Valkeinen
259f7018c21STomi Valkeinen if (priv->ready) {
260f7018c21STomi Valkeinen run_ready(priv);
261f7018c21STomi Valkeinen } else {
262f7018c21STomi Valkeinen /* There is no more data prepared by the userspace.
263f7018c21STomi Valkeinen * Set hw_running = 0 and wait for the next userspace
264f7018c21STomi Valkeinen * kick-off */
265f7018c21STomi Valkeinen shared->num_idle++;
266f7018c21STomi Valkeinen shared->hw_running = 0;
267f7018c21STomi Valkeinen
268f7018c21STomi Valkeinen QDUMP(" '-> Idle.");
269f7018c21STomi Valkeinen
270f7018c21STomi Valkeinen /* set ring buffer length to zero */
271f7018c21STomi Valkeinen gc_writel(priv, REG_GCRBLR, 0);
272f7018c21STomi Valkeinen
273f7018c21STomi Valkeinen wake_up_all(&priv->wait_idle);
274f7018c21STomi Valkeinen }
275f7018c21STomi Valkeinen
276f7018c21STomi Valkeinen shared->num_done++;
277f7018c21STomi Valkeinen } else {
278f7018c21STomi Valkeinen QERROR(" [???]");
279f7018c21STomi Valkeinen dump_whole_state(priv);
280f7018c21STomi Valkeinen }
281f7018c21STomi Valkeinen
282f7018c21STomi Valkeinen /* Clear the interrupt */
283f7018c21STomi Valkeinen gc_writel(priv, REG_GCISCR, status);
284f7018c21STomi Valkeinen spin_unlock(&priv->spinlock);
285f7018c21STomi Valkeinen
286f7018c21STomi Valkeinen return IRQ_HANDLED;
287f7018c21STomi Valkeinen }
288f7018c21STomi Valkeinen
289f7018c21STomi Valkeinen static int
pxa3xx_gcu_wait_idle(struct pxa3xx_gcu_priv * priv)290f7018c21STomi Valkeinen pxa3xx_gcu_wait_idle(struct pxa3xx_gcu_priv *priv)
291f7018c21STomi Valkeinen {
292f7018c21STomi Valkeinen int ret = 0;
293f7018c21STomi Valkeinen
294f7018c21STomi Valkeinen QDUMP("Waiting for idle...");
295f7018c21STomi Valkeinen
296f7018c21STomi Valkeinen /* Does not need to be atomic. There's a lock in user space,
297f7018c21STomi Valkeinen * but anyhow, this is just for statistics. */
298f7018c21STomi Valkeinen priv->shared->num_wait_idle++;
299f7018c21STomi Valkeinen
300f7018c21STomi Valkeinen while (priv->shared->hw_running) {
301f7018c21STomi Valkeinen int num = priv->shared->num_interrupts;
302f7018c21STomi Valkeinen u32 rbexhr = gc_readl(priv, REG_GCRBEXHR);
303f7018c21STomi Valkeinen
304f7018c21STomi Valkeinen ret = wait_event_interruptible_timeout(priv->wait_idle,
305f7018c21STomi Valkeinen !priv->shared->hw_running, HZ*4);
306f7018c21STomi Valkeinen
307f7018c21STomi Valkeinen if (ret != 0)
308f7018c21STomi Valkeinen break;
309f7018c21STomi Valkeinen
310f7018c21STomi Valkeinen if (gc_readl(priv, REG_GCRBEXHR) == rbexhr &&
311f7018c21STomi Valkeinen priv->shared->num_interrupts == num) {
312f7018c21STomi Valkeinen QERROR("TIMEOUT");
313f7018c21STomi Valkeinen ret = -ETIMEDOUT;
314f7018c21STomi Valkeinen break;
315f7018c21STomi Valkeinen }
316f7018c21STomi Valkeinen }
317f7018c21STomi Valkeinen
318f7018c21STomi Valkeinen QDUMP("done");
319f7018c21STomi Valkeinen
320f7018c21STomi Valkeinen return ret;
321f7018c21STomi Valkeinen }
322f7018c21STomi Valkeinen
323f7018c21STomi Valkeinen static int
pxa3xx_gcu_wait_free(struct pxa3xx_gcu_priv * priv)324f7018c21STomi Valkeinen pxa3xx_gcu_wait_free(struct pxa3xx_gcu_priv *priv)
325f7018c21STomi Valkeinen {
326f7018c21STomi Valkeinen int ret = 0;
327f7018c21STomi Valkeinen
328f7018c21STomi Valkeinen QDUMP("Waiting for free...");
329f7018c21STomi Valkeinen
330f7018c21STomi Valkeinen /* Does not need to be atomic. There's a lock in user space,
331f7018c21STomi Valkeinen * but anyhow, this is just for statistics. */
332f7018c21STomi Valkeinen priv->shared->num_wait_free++;
333f7018c21STomi Valkeinen
334f7018c21STomi Valkeinen while (!priv->free) {
335f7018c21STomi Valkeinen u32 rbexhr = gc_readl(priv, REG_GCRBEXHR);
336f7018c21STomi Valkeinen
337f7018c21STomi Valkeinen ret = wait_event_interruptible_timeout(priv->wait_free,
338f7018c21STomi Valkeinen priv->free, HZ*4);
339f7018c21STomi Valkeinen
340f7018c21STomi Valkeinen if (ret < 0)
341f7018c21STomi Valkeinen break;
342f7018c21STomi Valkeinen
343f7018c21STomi Valkeinen if (ret > 0)
344f7018c21STomi Valkeinen continue;
345f7018c21STomi Valkeinen
346f7018c21STomi Valkeinen if (gc_readl(priv, REG_GCRBEXHR) == rbexhr) {
347f7018c21STomi Valkeinen QERROR("TIMEOUT");
348f7018c21STomi Valkeinen ret = -ETIMEDOUT;
349f7018c21STomi Valkeinen break;
350f7018c21STomi Valkeinen }
351f7018c21STomi Valkeinen }
352f7018c21STomi Valkeinen
353f7018c21STomi Valkeinen QDUMP("done");
354f7018c21STomi Valkeinen
355f7018c21STomi Valkeinen return ret;
356f7018c21STomi Valkeinen }
357f7018c21STomi Valkeinen
358f7018c21STomi Valkeinen /* Misc device layer */
359f7018c21STomi Valkeinen
to_pxa3xx_gcu_priv(struct file * file)360f7018c21STomi Valkeinen static inline struct pxa3xx_gcu_priv *to_pxa3xx_gcu_priv(struct file *file)
361f7018c21STomi Valkeinen {
362f7018c21STomi Valkeinen struct miscdevice *dev = file->private_data;
363f7018c21STomi Valkeinen return container_of(dev, struct pxa3xx_gcu_priv, misc_dev);
364f7018c21STomi Valkeinen }
365f7018c21STomi Valkeinen
366f7018c21STomi Valkeinen /*
367f7018c21STomi Valkeinen * provide an empty .open callback, so the core sets file->private_data
368f7018c21STomi Valkeinen * for us.
369f7018c21STomi Valkeinen */
pxa3xx_gcu_open(struct inode * inode,struct file * file)370f7018c21STomi Valkeinen static int pxa3xx_gcu_open(struct inode *inode, struct file *file)
371f7018c21STomi Valkeinen {
372f7018c21STomi Valkeinen return 0;
373f7018c21STomi Valkeinen }
374f7018c21STomi Valkeinen
375f7018c21STomi Valkeinen static ssize_t
pxa3xx_gcu_write(struct file * file,const char * buff,size_t count,loff_t * offp)376f7018c21STomi Valkeinen pxa3xx_gcu_write(struct file *file, const char *buff,
377f7018c21STomi Valkeinen size_t count, loff_t *offp)
378f7018c21STomi Valkeinen {
379f7018c21STomi Valkeinen int ret;
380f7018c21STomi Valkeinen unsigned long flags;
381f7018c21STomi Valkeinen struct pxa3xx_gcu_batch *buffer;
382f7018c21STomi Valkeinen struct pxa3xx_gcu_priv *priv = to_pxa3xx_gcu_priv(file);
383f7018c21STomi Valkeinen
384a09d2d00SHyunwoo Kim size_t words = count / 4;
385f7018c21STomi Valkeinen
386f7018c21STomi Valkeinen /* Does not need to be atomic. There's a lock in user space,
387f7018c21STomi Valkeinen * but anyhow, this is just for statistics. */
388f7018c21STomi Valkeinen priv->shared->num_writes++;
389f7018c21STomi Valkeinen priv->shared->num_words += words;
390f7018c21STomi Valkeinen
391f7018c21STomi Valkeinen /* Last word reserved for batch buffer end command */
392f7018c21STomi Valkeinen if (words >= PXA3XX_GCU_BATCH_WORDS)
393f7018c21STomi Valkeinen return -E2BIG;
394f7018c21STomi Valkeinen
395f7018c21STomi Valkeinen /* Wait for a free buffer */
396f7018c21STomi Valkeinen if (!priv->free) {
397f7018c21STomi Valkeinen ret = pxa3xx_gcu_wait_free(priv);
398f7018c21STomi Valkeinen if (ret < 0)
399f7018c21STomi Valkeinen return ret;
400f7018c21STomi Valkeinen }
401f7018c21STomi Valkeinen
402f7018c21STomi Valkeinen /*
403f7018c21STomi Valkeinen * Get buffer from free list
404f7018c21STomi Valkeinen */
405f7018c21STomi Valkeinen spin_lock_irqsave(&priv->spinlock, flags);
406f7018c21STomi Valkeinen buffer = priv->free;
407f7018c21STomi Valkeinen priv->free = buffer->next;
408f7018c21STomi Valkeinen spin_unlock_irqrestore(&priv->spinlock, flags);
409f7018c21STomi Valkeinen
410f7018c21STomi Valkeinen
411f7018c21STomi Valkeinen /* Copy data from user into buffer */
412f7018c21STomi Valkeinen ret = copy_from_user(buffer->ptr, buff, words * 4);
413f7018c21STomi Valkeinen if (ret) {
414f7018c21STomi Valkeinen spin_lock_irqsave(&priv->spinlock, flags);
415f7018c21STomi Valkeinen buffer->next = priv->free;
416f7018c21STomi Valkeinen priv->free = buffer;
417f7018c21STomi Valkeinen spin_unlock_irqrestore(&priv->spinlock, flags);
418f7018c21STomi Valkeinen return -EFAULT;
419f7018c21STomi Valkeinen }
420f7018c21STomi Valkeinen
421f7018c21STomi Valkeinen buffer->length = words;
422f7018c21STomi Valkeinen
423f7018c21STomi Valkeinen /* Append batch buffer end command */
424f7018c21STomi Valkeinen buffer->ptr[words] = 0x01000000;
425f7018c21STomi Valkeinen
426f7018c21STomi Valkeinen /*
427f7018c21STomi Valkeinen * Add buffer to ready list
428f7018c21STomi Valkeinen */
429f7018c21STomi Valkeinen spin_lock_irqsave(&priv->spinlock, flags);
430f7018c21STomi Valkeinen
431f7018c21STomi Valkeinen buffer->next = NULL;
432f7018c21STomi Valkeinen
433f7018c21STomi Valkeinen if (priv->ready) {
434f7018c21STomi Valkeinen BUG_ON(priv->ready_last == NULL);
435f7018c21STomi Valkeinen
436f7018c21STomi Valkeinen priv->ready_last->next = buffer;
437f7018c21STomi Valkeinen } else
438f7018c21STomi Valkeinen priv->ready = buffer;
439f7018c21STomi Valkeinen
440f7018c21STomi Valkeinen priv->ready_last = buffer;
441f7018c21STomi Valkeinen
442f7018c21STomi Valkeinen if (!priv->shared->hw_running)
443f7018c21STomi Valkeinen run_ready(priv);
444f7018c21STomi Valkeinen
445f7018c21STomi Valkeinen spin_unlock_irqrestore(&priv->spinlock, flags);
446f7018c21STomi Valkeinen
447f7018c21STomi Valkeinen return words * 4;
448f7018c21STomi Valkeinen }
449f7018c21STomi Valkeinen
450f7018c21STomi Valkeinen
451f7018c21STomi Valkeinen static long
pxa3xx_gcu_ioctl(struct file * file,unsigned int cmd,unsigned long arg)452f7018c21STomi Valkeinen pxa3xx_gcu_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
453f7018c21STomi Valkeinen {
454f7018c21STomi Valkeinen unsigned long flags;
455f7018c21STomi Valkeinen struct pxa3xx_gcu_priv *priv = to_pxa3xx_gcu_priv(file);
456f7018c21STomi Valkeinen
457f7018c21STomi Valkeinen switch (cmd) {
458f7018c21STomi Valkeinen case PXA3XX_GCU_IOCTL_RESET:
459f7018c21STomi Valkeinen spin_lock_irqsave(&priv->spinlock, flags);
460f7018c21STomi Valkeinen pxa3xx_gcu_reset(priv);
461f7018c21STomi Valkeinen spin_unlock_irqrestore(&priv->spinlock, flags);
462f7018c21STomi Valkeinen return 0;
463f7018c21STomi Valkeinen
464f7018c21STomi Valkeinen case PXA3XX_GCU_IOCTL_WAIT_IDLE:
465f7018c21STomi Valkeinen return pxa3xx_gcu_wait_idle(priv);
466f7018c21STomi Valkeinen }
467f7018c21STomi Valkeinen
468f7018c21STomi Valkeinen return -ENOSYS;
469f7018c21STomi Valkeinen }
470f7018c21STomi Valkeinen
471f7018c21STomi Valkeinen static int
pxa3xx_gcu_mmap(struct file * file,struct vm_area_struct * vma)472f7018c21STomi Valkeinen pxa3xx_gcu_mmap(struct file *file, struct vm_area_struct *vma)
473f7018c21STomi Valkeinen {
474f7018c21STomi Valkeinen unsigned int size = vma->vm_end - vma->vm_start;
475f7018c21STomi Valkeinen struct pxa3xx_gcu_priv *priv = to_pxa3xx_gcu_priv(file);
476f7018c21STomi Valkeinen
477f7018c21STomi Valkeinen switch (vma->vm_pgoff) {
478f7018c21STomi Valkeinen case 0:
479f7018c21STomi Valkeinen /* hand out the shared data area */
480f7018c21STomi Valkeinen if (size != SHARED_SIZE)
481f7018c21STomi Valkeinen return -EINVAL;
482f7018c21STomi Valkeinen
48302c486f4SChristoph Hellwig return dma_mmap_coherent(priv->dev, vma,
484f7018c21STomi Valkeinen priv->shared, priv->shared_phys, size);
485f7018c21STomi Valkeinen
486f7018c21STomi Valkeinen case SHARED_SIZE >> PAGE_SHIFT:
487f7018c21STomi Valkeinen /* hand out the MMIO base for direct register access
488f7018c21STomi Valkeinen * from userspace */
489f7018c21STomi Valkeinen if (size != resource_size(priv->resource_mem))
490f7018c21STomi Valkeinen return -EINVAL;
491f7018c21STomi Valkeinen
492f7018c21STomi Valkeinen vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
493f7018c21STomi Valkeinen
494f7018c21STomi Valkeinen return io_remap_pfn_range(vma, vma->vm_start,
495f7018c21STomi Valkeinen priv->resource_mem->start >> PAGE_SHIFT,
496f7018c21STomi Valkeinen size, vma->vm_page_prot);
497f7018c21STomi Valkeinen }
498f7018c21STomi Valkeinen
499f7018c21STomi Valkeinen return -EINVAL;
500f7018c21STomi Valkeinen }
501f7018c21STomi Valkeinen
502f7018c21STomi Valkeinen
503f7018c21STomi Valkeinen #ifdef PXA3XX_GCU_DEBUG_TIMER
504f7018c21STomi Valkeinen static struct timer_list pxa3xx_gcu_debug_timer;
505e4a67df7SKees Cook static struct pxa3xx_gcu_priv *debug_timer_priv;
506f7018c21STomi Valkeinen
pxa3xx_gcu_debug_timedout(struct timer_list * unused)507e4a67df7SKees Cook static void pxa3xx_gcu_debug_timedout(struct timer_list *unused)
508f7018c21STomi Valkeinen {
509e4a67df7SKees Cook struct pxa3xx_gcu_priv *priv = debug_timer_priv;
510f7018c21STomi Valkeinen
511f7018c21STomi Valkeinen QERROR("Timer DUMP");
512f7018c21STomi Valkeinen
5135eabff1cSHimanshu Jha mod_timer(&pxa3xx_gcu_debug_timer, jiffies + 5 * HZ);
514f7018c21STomi Valkeinen }
515f7018c21STomi Valkeinen
pxa3xx_gcu_init_debug_timer(struct pxa3xx_gcu_priv * priv)516e4a67df7SKees Cook static void pxa3xx_gcu_init_debug_timer(struct pxa3xx_gcu_priv *priv)
517f7018c21STomi Valkeinen {
518e4a67df7SKees Cook /* init the timer structure */
519e4a67df7SKees Cook debug_timer_priv = priv;
520e4a67df7SKees Cook timer_setup(&pxa3xx_gcu_debug_timer, pxa3xx_gcu_debug_timedout, 0);
521e4a67df7SKees Cook pxa3xx_gcu_debug_timedout(NULL);
522f7018c21STomi Valkeinen }
523f7018c21STomi Valkeinen #else
pxa3xx_gcu_init_debug_timer(struct pxa3xx_gcu_priv * priv)524e4a67df7SKees Cook static inline void pxa3xx_gcu_init_debug_timer(struct pxa3xx_gcu_priv *priv) {}
525f7018c21STomi Valkeinen #endif
526f7018c21STomi Valkeinen
527f7018c21STomi Valkeinen static int
pxa3xx_gcu_add_buffer(struct device * dev,struct pxa3xx_gcu_priv * priv)528f7018c21STomi Valkeinen pxa3xx_gcu_add_buffer(struct device *dev,
529f7018c21STomi Valkeinen struct pxa3xx_gcu_priv *priv)
530f7018c21STomi Valkeinen {
531f7018c21STomi Valkeinen struct pxa3xx_gcu_batch *buffer;
532f7018c21STomi Valkeinen
533f7018c21STomi Valkeinen buffer = kzalloc(sizeof(struct pxa3xx_gcu_batch), GFP_KERNEL);
534f7018c21STomi Valkeinen if (!buffer)
535f7018c21STomi Valkeinen return -ENOMEM;
536f7018c21STomi Valkeinen
537f7018c21STomi Valkeinen buffer->ptr = dma_alloc_coherent(dev, PXA3XX_GCU_BATCH_WORDS * 4,
538f7018c21STomi Valkeinen &buffer->phys, GFP_KERNEL);
539f7018c21STomi Valkeinen if (!buffer->ptr) {
540f7018c21STomi Valkeinen kfree(buffer);
541f7018c21STomi Valkeinen return -ENOMEM;
542f7018c21STomi Valkeinen }
543f7018c21STomi Valkeinen
544f7018c21STomi Valkeinen buffer->next = priv->free;
545f7018c21STomi Valkeinen priv->free = buffer;
546f7018c21STomi Valkeinen
547f7018c21STomi Valkeinen return 0;
548f7018c21STomi Valkeinen }
549f7018c21STomi Valkeinen
550f7018c21STomi Valkeinen static void
pxa3xx_gcu_free_buffers(struct device * dev,struct pxa3xx_gcu_priv * priv)551f7018c21STomi Valkeinen pxa3xx_gcu_free_buffers(struct device *dev,
552f7018c21STomi Valkeinen struct pxa3xx_gcu_priv *priv)
553f7018c21STomi Valkeinen {
554f7018c21STomi Valkeinen struct pxa3xx_gcu_batch *next, *buffer = priv->free;
555f7018c21STomi Valkeinen
556f7018c21STomi Valkeinen while (buffer) {
557f7018c21STomi Valkeinen next = buffer->next;
558f7018c21STomi Valkeinen
559f7018c21STomi Valkeinen dma_free_coherent(dev, PXA3XX_GCU_BATCH_WORDS * 4,
560f7018c21STomi Valkeinen buffer->ptr, buffer->phys);
561f7018c21STomi Valkeinen
562f7018c21STomi Valkeinen kfree(buffer);
563f7018c21STomi Valkeinen buffer = next;
564f7018c21STomi Valkeinen }
565f7018c21STomi Valkeinen
566f7018c21STomi Valkeinen priv->free = NULL;
567f7018c21STomi Valkeinen }
568f7018c21STomi Valkeinen
569f7018c21STomi Valkeinen static const struct file_operations pxa3xx_gcu_miscdev_fops = {
570f7018c21STomi Valkeinen .owner = THIS_MODULE,
571f7018c21STomi Valkeinen .open = pxa3xx_gcu_open,
572f7018c21STomi Valkeinen .write = pxa3xx_gcu_write,
573f7018c21STomi Valkeinen .unlocked_ioctl = pxa3xx_gcu_ioctl,
574f7018c21STomi Valkeinen .mmap = pxa3xx_gcu_mmap,
575f7018c21STomi Valkeinen };
576f7018c21STomi Valkeinen
pxa3xx_gcu_probe(struct platform_device * pdev)577f7018c21STomi Valkeinen static int pxa3xx_gcu_probe(struct platform_device *pdev)
578f7018c21STomi Valkeinen {
579f7018c21STomi Valkeinen int i, ret, irq;
580f7018c21STomi Valkeinen struct resource *r;
581f7018c21STomi Valkeinen struct pxa3xx_gcu_priv *priv;
582f7018c21STomi Valkeinen struct device *dev = &pdev->dev;
583f7018c21STomi Valkeinen
584f7018c21STomi Valkeinen priv = devm_kzalloc(dev, sizeof(struct pxa3xx_gcu_priv), GFP_KERNEL);
585f7018c21STomi Valkeinen if (!priv)
586f7018c21STomi Valkeinen return -ENOMEM;
587f7018c21STomi Valkeinen
588f7018c21STomi Valkeinen init_waitqueue_head(&priv->wait_idle);
589f7018c21STomi Valkeinen init_waitqueue_head(&priv->wait_free);
590f7018c21STomi Valkeinen spin_lock_init(&priv->spinlock);
591f7018c21STomi Valkeinen
592f7018c21STomi Valkeinen /* we allocate the misc device structure as part of our own allocation,
593f7018c21STomi Valkeinen * so we can get a pointer to our priv structure later on with
594f7018c21STomi Valkeinen * container_of(). This isn't really necessary as we have a fixed minor
595f7018c21STomi Valkeinen * number anyway, but this is to avoid statics. */
596f7018c21STomi Valkeinen
5976ce6ae7cSZhenzhong Duan priv->misc_dev.minor = PXA3XX_GCU_MINOR,
598f7018c21STomi Valkeinen priv->misc_dev.name = DRV_NAME,
599f7018c21STomi Valkeinen priv->misc_dev.fops = &pxa3xx_gcu_miscdev_fops;
600f7018c21STomi Valkeinen
601f7018c21STomi Valkeinen /* handle IO resources */
6020db0a1ebSYang Li priv->mmio_base = devm_platform_get_and_ioremap_resource(pdev, 0, &r);
6039b22b8c5SJingoo Han if (IS_ERR(priv->mmio_base))
604f7018c21STomi Valkeinen return PTR_ERR(priv->mmio_base);
605f7018c21STomi Valkeinen
606f7018c21STomi Valkeinen /* enable the clock */
607f7018c21STomi Valkeinen priv->clk = devm_clk_get(dev, NULL);
608626e021dSCai Huoqing if (IS_ERR(priv->clk))
609626e021dSCai Huoqing return dev_err_probe(dev, PTR_ERR(priv->clk), "failed to get clock\n");
610f7018c21STomi Valkeinen
611f7018c21STomi Valkeinen /* request the IRQ */
612f7018c21STomi Valkeinen irq = platform_get_irq(pdev, 0);
613e2bc5533SYang Li if (irq < 0)
6147588f1ecSGustavo A. R. Silva return irq;
615f7018c21STomi Valkeinen
616f7018c21STomi Valkeinen ret = devm_request_irq(dev, irq, pxa3xx_gcu_handle_irq,
617f7018c21STomi Valkeinen 0, DRV_NAME, priv);
618f7018c21STomi Valkeinen if (ret < 0) {
619f7018c21STomi Valkeinen dev_err(dev, "request_irq failed\n");
620f7018c21STomi Valkeinen return ret;
621f7018c21STomi Valkeinen }
622f7018c21STomi Valkeinen
623f7018c21STomi Valkeinen /* allocate dma memory */
624f7018c21STomi Valkeinen priv->shared = dma_alloc_coherent(dev, SHARED_SIZE,
625f7018c21STomi Valkeinen &priv->shared_phys, GFP_KERNEL);
626f7018c21STomi Valkeinen if (!priv->shared) {
627f7018c21STomi Valkeinen dev_err(dev, "failed to allocate DMA memory\n");
628f7018c21STomi Valkeinen return -ENOMEM;
629f7018c21STomi Valkeinen }
630f7018c21STomi Valkeinen
631f7018c21STomi Valkeinen /* register misc device */
632f7018c21STomi Valkeinen ret = misc_register(&priv->misc_dev);
633f7018c21STomi Valkeinen if (ret < 0) {
634f7018c21STomi Valkeinen dev_err(dev, "misc_register() for minor %d failed\n",
6356ce6ae7cSZhenzhong Duan PXA3XX_GCU_MINOR);
636f7018c21STomi Valkeinen goto err_free_dma;
637f7018c21STomi Valkeinen }
638f7018c21STomi Valkeinen
6399e6e35edSRobert Jarzmik ret = clk_prepare_enable(priv->clk);
640f7018c21STomi Valkeinen if (ret < 0) {
641f7018c21STomi Valkeinen dev_err(dev, "failed to enable clock\n");
642f7018c21STomi Valkeinen goto err_misc_deregister;
643f7018c21STomi Valkeinen }
644f7018c21STomi Valkeinen
645f7018c21STomi Valkeinen for (i = 0; i < 8; i++) {
646f7018c21STomi Valkeinen ret = pxa3xx_gcu_add_buffer(dev, priv);
647f7018c21STomi Valkeinen if (ret) {
648d87ad457SYang Yingliang pxa3xx_gcu_free_buffers(dev, priv);
649f7018c21STomi Valkeinen dev_err(dev, "failed to allocate DMA memory\n");
650f7018c21STomi Valkeinen goto err_disable_clk;
651f7018c21STomi Valkeinen }
652f7018c21STomi Valkeinen }
653f7018c21STomi Valkeinen
654f7018c21STomi Valkeinen platform_set_drvdata(pdev, priv);
655f7018c21STomi Valkeinen priv->resource_mem = r;
65602c486f4SChristoph Hellwig priv->dev = dev;
657f7018c21STomi Valkeinen pxa3xx_gcu_reset(priv);
658e4a67df7SKees Cook pxa3xx_gcu_init_debug_timer(priv);
659f7018c21STomi Valkeinen
660f7018c21STomi Valkeinen dev_info(dev, "registered @0x%p, DMA 0x%p (%d bytes), IRQ %d\n",
661f7018c21STomi Valkeinen (void *) r->start, (void *) priv->shared_phys,
662f7018c21STomi Valkeinen SHARED_SIZE, irq);
663f7018c21STomi Valkeinen return 0;
664f7018c21STomi Valkeinen
665d87ad457SYang Yingliang err_disable_clk:
666d87ad457SYang Yingliang clk_disable_unprepare(priv->clk);
667f7018c21STomi Valkeinen
668f7018c21STomi Valkeinen err_misc_deregister:
669f7018c21STomi Valkeinen misc_deregister(&priv->misc_dev);
670f7018c21STomi Valkeinen
671d87ad457SYang Yingliang err_free_dma:
672d87ad457SYang Yingliang dma_free_coherent(dev, SHARED_SIZE,
673d87ad457SYang Yingliang priv->shared, priv->shared_phys);
674f7018c21STomi Valkeinen
675f7018c21STomi Valkeinen return ret;
676f7018c21STomi Valkeinen }
677f7018c21STomi Valkeinen
pxa3xx_gcu_remove(struct platform_device * pdev)678*2872c291SUwe Kleine-König static void pxa3xx_gcu_remove(struct platform_device *pdev)
679f7018c21STomi Valkeinen {
680f7018c21STomi Valkeinen struct pxa3xx_gcu_priv *priv = platform_get_drvdata(pdev);
681f7018c21STomi Valkeinen struct device *dev = &pdev->dev;
682f7018c21STomi Valkeinen
683f7018c21STomi Valkeinen pxa3xx_gcu_wait_idle(priv);
684f7018c21STomi Valkeinen misc_deregister(&priv->misc_dev);
685f7018c21STomi Valkeinen dma_free_coherent(dev, SHARED_SIZE, priv->shared, priv->shared_phys);
686d87ad457SYang Yingliang clk_disable_unprepare(priv->clk);
687f7018c21STomi Valkeinen pxa3xx_gcu_free_buffers(dev, priv);
688f7018c21STomi Valkeinen }
689f7018c21STomi Valkeinen
690aa45ee8eSDaniel Mack #ifdef CONFIG_OF
691aa45ee8eSDaniel Mack static const struct of_device_id pxa3xx_gcu_of_match[] = {
692aa45ee8eSDaniel Mack { .compatible = "marvell,pxa300-gcu", },
693aa45ee8eSDaniel Mack { }
694aa45ee8eSDaniel Mack };
695aa45ee8eSDaniel Mack MODULE_DEVICE_TABLE(of, pxa3xx_gcu_of_match);
696aa45ee8eSDaniel Mack #endif
697aa45ee8eSDaniel Mack
698f7018c21STomi Valkeinen static struct platform_driver pxa3xx_gcu_driver = {
699f7018c21STomi Valkeinen .probe = pxa3xx_gcu_probe,
700*2872c291SUwe Kleine-König .remove_new = pxa3xx_gcu_remove,
701f7018c21STomi Valkeinen .driver = {
702f7018c21STomi Valkeinen .name = DRV_NAME,
703aa45ee8eSDaniel Mack .of_match_table = of_match_ptr(pxa3xx_gcu_of_match),
704f7018c21STomi Valkeinen },
705f7018c21STomi Valkeinen };
706f7018c21STomi Valkeinen
707f7018c21STomi Valkeinen module_platform_driver(pxa3xx_gcu_driver);
708f7018c21STomi Valkeinen
709f7018c21STomi Valkeinen MODULE_DESCRIPTION("PXA3xx graphics controller unit driver");
710f7018c21STomi Valkeinen MODULE_LICENSE("GPL");
7116ce6ae7cSZhenzhong Duan MODULE_ALIAS_MISCDEV(PXA3XX_GCU_MINOR);
712f7018c21STomi Valkeinen MODULE_AUTHOR("Janine Kropp <nin@directfb.org>, "
713f7018c21STomi Valkeinen "Denis Oliver Kropp <dok@directfb.org>, "
714f7018c21STomi Valkeinen "Daniel Mack <daniel@caiaq.de>");
715