1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * pxa3xx-gcu.c - Linux kernel module for PXA3xx graphics controllers
5 * This driver needs a DirectFB counterpart in user space, communication
6 * is handled via mmap()ed memory areas and an ioctl.
8 * Copyright (c) 2009 Daniel Mack <daniel@caiaq.de>
9 * Copyright (c) 2009 Janine Kropp <nin@directfb.org>
10 * Copyright (c) 2009 Denis Oliver Kropp <dok@directfb.org>
14 * WARNING: This controller is attached to System Bus 2 of the PXA which
15 * needs its arbiter to be enabled explicitly (CKENB & 1<<9).
16 * There is currently no way to do this from Linux, so you need to teach
17 * your bootloader for now.
20 #include <linux/module.h>
21 #include <linux/platform_device.h>
22 #include <linux/dma-mapping.h>
23 #include <linux/miscdevice.h>
24 #include <linux/interrupt.h>
25 #include <linux/spinlock.h>
26 #include <linux/uaccess.h>
27 #include <linux/ioctl.h>
28 #include <linux/delay.h>
29 #include <linux/sched.h>
30 #include <linux/slab.h>
31 #include <linux/clk.h>
36 #include "pxa3xx-gcu.h"
38 #define DRV_NAME "pxa3xx-gcu"
41 #define GCCR_SYNC_CLR (1 << 9)
42 #define GCCR_BP_RST (1 << 8)
43 #define GCCR_ABORT (1 << 6)
44 #define GCCR_STOP (1 << 4)
46 #define REG_GCISCR 0x04
47 #define REG_GCIECR 0x08
48 #define REG_GCRBBR 0x20
49 #define REG_GCRBLR 0x24
50 #define REG_GCRBHR 0x28
51 #define REG_GCRBTR 0x2C
52 #define REG_GCRBEXHR 0x30
54 #define IE_EOB (1 << 0)
55 #define IE_EEOB (1 << 5)
58 #define SHARED_SIZE PAGE_ALIGN(sizeof(struct pxa3xx_gcu_shared))
60 /* #define PXA3XX_GCU_DEBUG */
61 /* #define PXA3XX_GCU_DEBUG_TIMER */
63 #ifdef PXA3XX_GCU_DEBUG
66 QPRINT(priv, KERN_DEBUG, msg); \
69 #define QDUMP(msg) do {} while (0)
74 QPRINT(priv, KERN_ERR, msg); \
77 struct pxa3xx_gcu_batch
{
78 struct pxa3xx_gcu_batch
*next
;
84 struct pxa3xx_gcu_priv
{
86 void __iomem
*mmio_base
;
88 struct pxa3xx_gcu_shared
*shared
;
89 dma_addr_t shared_phys
;
90 struct resource
*resource_mem
;
91 struct miscdevice misc_dev
;
92 wait_queue_head_t wait_idle
;
93 wait_queue_head_t wait_free
;
95 struct timespec64 base_time
;
97 struct pxa3xx_gcu_batch
*free
;
98 struct pxa3xx_gcu_batch
*ready
;
99 struct pxa3xx_gcu_batch
*ready_last
;
100 struct pxa3xx_gcu_batch
*running
;
103 static inline unsigned long
104 gc_readl(struct pxa3xx_gcu_priv
*priv
, unsigned int off
)
106 return __raw_readl(priv
->mmio_base
+ off
);
110 gc_writel(struct pxa3xx_gcu_priv
*priv
, unsigned int off
, unsigned long val
)
112 __raw_writel(val
, priv
->mmio_base
+ off
);
115 #define QPRINT(priv, level, msg) \
117 struct timespec64 ts; \
118 struct pxa3xx_gcu_shared *shared = priv->shared; \
119 u32 base = gc_readl(priv, REG_GCRBBR); \
121 ktime_get_ts64(&ts); \
122 ts = timespec64_sub(ts, priv->base_time); \
124 printk(level "%lld.%03ld.%03ld - %-17s: %-21s (%s, " \
126 "0x%02lx, B 0x%08lx [%ld], E %5ld, H %5ld, " \
129 ts.tv_nsec / NSEC_PER_MSEC, \
130 (ts.tv_nsec % NSEC_PER_MSEC) / USEC_PER_MSEC, \
132 shared->hw_running ? "running" : " idle", \
133 gc_readl(priv, REG_GCISCR), \
134 gc_readl(priv, REG_GCRBBR), \
135 gc_readl(priv, REG_GCRBLR), \
136 (gc_readl(priv, REG_GCRBEXHR) - base) / 4, \
137 (gc_readl(priv, REG_GCRBHR) - base) / 4, \
138 (gc_readl(priv, REG_GCRBTR) - base) / 4); \
142 pxa3xx_gcu_reset(struct pxa3xx_gcu_priv
*priv
)
146 /* disable interrupts */
147 gc_writel(priv
, REG_GCIECR
, 0);
150 gc_writel(priv
, REG_GCCR
, GCCR_ABORT
);
151 gc_writel(priv
, REG_GCCR
, 0);
153 memset(priv
->shared
, 0, SHARED_SIZE
);
154 priv
->shared
->buffer_phys
= priv
->shared_phys
;
155 priv
->shared
->magic
= PXA3XX_GCU_SHARED_MAGIC
;
157 ktime_get_ts64(&priv
->base_time
);
159 /* set up the ring buffer pointers */
160 gc_writel(priv
, REG_GCRBLR
, 0);
161 gc_writel(priv
, REG_GCRBBR
, priv
->shared_phys
);
162 gc_writel(priv
, REG_GCRBTR
, priv
->shared_phys
);
164 /* enable all IRQs except EOB */
165 gc_writel(priv
, REG_GCIECR
, IE_ALL
& ~IE_EOB
);
169 dump_whole_state(struct pxa3xx_gcu_priv
*priv
)
171 struct pxa3xx_gcu_shared
*sh
= priv
->shared
;
172 u32 base
= gc_readl(priv
, REG_GCRBBR
);
176 printk(KERN_DEBUG
"== PXA3XX-GCU DUMP ==\n"
177 "%s, STATUS 0x%02lx, B 0x%08lx [%ld], E %5ld, H %5ld, T %5ld\n",
178 sh
->hw_running
? "running" : "idle ",
179 gc_readl(priv
, REG_GCISCR
),
180 gc_readl(priv
, REG_GCRBBR
),
181 gc_readl(priv
, REG_GCRBLR
),
182 (gc_readl(priv
, REG_GCRBEXHR
) - base
) / 4,
183 (gc_readl(priv
, REG_GCRBHR
) - base
) / 4,
184 (gc_readl(priv
, REG_GCRBTR
) - base
) / 4);
188 flush_running(struct pxa3xx_gcu_priv
*priv
)
190 struct pxa3xx_gcu_batch
*running
= priv
->running
;
191 struct pxa3xx_gcu_batch
*next
;
194 next
= running
->next
;
195 running
->next
= priv
->free
;
196 priv
->free
= running
;
200 priv
->running
= NULL
;
204 run_ready(struct pxa3xx_gcu_priv
*priv
)
206 unsigned int num
= 0;
207 struct pxa3xx_gcu_shared
*shared
= priv
->shared
;
208 struct pxa3xx_gcu_batch
*ready
= priv
->ready
;
214 shared
->buffer
[num
++] = 0x05000000;
217 shared
->buffer
[num
++] = 0x00000001;
218 shared
->buffer
[num
++] = ready
->phys
;
222 shared
->buffer
[num
++] = 0x05000000;
223 priv
->running
= priv
->ready
;
224 priv
->ready
= priv
->ready_last
= NULL
;
225 gc_writel(priv
, REG_GCRBLR
, 0);
226 shared
->hw_running
= 1;
228 /* ring base address */
229 gc_writel(priv
, REG_GCRBBR
, shared
->buffer_phys
);
231 /* ring tail address */
232 gc_writel(priv
, REG_GCRBTR
, shared
->buffer_phys
+ num
* 4);
235 gc_writel(priv
, REG_GCRBLR
, ((num
+ 63) & ~63) * 4);
239 pxa3xx_gcu_handle_irq(int irq
, void *ctx
)
241 struct pxa3xx_gcu_priv
*priv
= ctx
;
242 struct pxa3xx_gcu_shared
*shared
= priv
->shared
;
243 u32 status
= gc_readl(priv
, REG_GCISCR
) & IE_ALL
;
250 spin_lock(&priv
->spinlock
);
251 shared
->num_interrupts
++;
253 if (status
& IE_EEOB
) {
257 wake_up_all(&priv
->wait_free
);
262 /* There is no more data prepared by the userspace.
263 * Set hw_running = 0 and wait for the next userspace
266 shared
->hw_running
= 0;
270 /* set ring buffer length to zero */
271 gc_writel(priv
, REG_GCRBLR
, 0);
273 wake_up_all(&priv
->wait_idle
);
279 dump_whole_state(priv
);
282 /* Clear the interrupt */
283 gc_writel(priv
, REG_GCISCR
, status
);
284 spin_unlock(&priv
->spinlock
);
290 pxa3xx_gcu_wait_idle(struct pxa3xx_gcu_priv
*priv
)
294 QDUMP("Waiting for idle...");
296 /* Does not need to be atomic. There's a lock in user space,
297 * but anyhow, this is just for statistics. */
298 priv
->shared
->num_wait_idle
++;
300 while (priv
->shared
->hw_running
) {
301 int num
= priv
->shared
->num_interrupts
;
302 u32 rbexhr
= gc_readl(priv
, REG_GCRBEXHR
);
304 ret
= wait_event_interruptible_timeout(priv
->wait_idle
,
305 !priv
->shared
->hw_running
, HZ
*4);
310 if (gc_readl(priv
, REG_GCRBEXHR
) == rbexhr
&&
311 priv
->shared
->num_interrupts
== num
) {
324 pxa3xx_gcu_wait_free(struct pxa3xx_gcu_priv
*priv
)
328 QDUMP("Waiting for free...");
330 /* Does not need to be atomic. There's a lock in user space,
331 * but anyhow, this is just for statistics. */
332 priv
->shared
->num_wait_free
++;
334 while (!priv
->free
) {
335 u32 rbexhr
= gc_readl(priv
, REG_GCRBEXHR
);
337 ret
= wait_event_interruptible_timeout(priv
->wait_free
,
346 if (gc_readl(priv
, REG_GCRBEXHR
) == rbexhr
) {
358 /* Misc device layer */
360 static inline struct pxa3xx_gcu_priv
*to_pxa3xx_gcu_priv(struct file
*file
)
362 struct miscdevice
*dev
= file
->private_data
;
363 return container_of(dev
, struct pxa3xx_gcu_priv
, misc_dev
);
367 * provide an empty .open callback, so the core sets file->private_data
370 static int pxa3xx_gcu_open(struct inode
*inode
, struct file
*file
)
376 pxa3xx_gcu_write(struct file
*file
, const char *buff
,
377 size_t count
, loff_t
*offp
)
381 struct pxa3xx_gcu_batch
*buffer
;
382 struct pxa3xx_gcu_priv
*priv
= to_pxa3xx_gcu_priv(file
);
384 int words
= count
/ 4;
386 /* Does not need to be atomic. There's a lock in user space,
387 * but anyhow, this is just for statistics. */
388 priv
->shared
->num_writes
++;
389 priv
->shared
->num_words
+= words
;
391 /* Last word reserved for batch buffer end command */
392 if (words
>= PXA3XX_GCU_BATCH_WORDS
)
395 /* Wait for a free buffer */
397 ret
= pxa3xx_gcu_wait_free(priv
);
403 * Get buffer from free list
405 spin_lock_irqsave(&priv
->spinlock
, flags
);
407 priv
->free
= buffer
->next
;
408 spin_unlock_irqrestore(&priv
->spinlock
, flags
);
411 /* Copy data from user into buffer */
412 ret
= copy_from_user(buffer
->ptr
, buff
, words
* 4);
414 spin_lock_irqsave(&priv
->spinlock
, flags
);
415 buffer
->next
= priv
->free
;
417 spin_unlock_irqrestore(&priv
->spinlock
, flags
);
421 buffer
->length
= words
;
423 /* Append batch buffer end command */
424 buffer
->ptr
[words
] = 0x01000000;
427 * Add buffer to ready list
429 spin_lock_irqsave(&priv
->spinlock
, flags
);
434 BUG_ON(priv
->ready_last
== NULL
);
436 priv
->ready_last
->next
= buffer
;
438 priv
->ready
= buffer
;
440 priv
->ready_last
= buffer
;
442 if (!priv
->shared
->hw_running
)
445 spin_unlock_irqrestore(&priv
->spinlock
, flags
);
452 pxa3xx_gcu_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
455 struct pxa3xx_gcu_priv
*priv
= to_pxa3xx_gcu_priv(file
);
458 case PXA3XX_GCU_IOCTL_RESET
:
459 spin_lock_irqsave(&priv
->spinlock
, flags
);
460 pxa3xx_gcu_reset(priv
);
461 spin_unlock_irqrestore(&priv
->spinlock
, flags
);
464 case PXA3XX_GCU_IOCTL_WAIT_IDLE
:
465 return pxa3xx_gcu_wait_idle(priv
);
472 pxa3xx_gcu_mmap(struct file
*file
, struct vm_area_struct
*vma
)
474 unsigned int size
= vma
->vm_end
- vma
->vm_start
;
475 struct pxa3xx_gcu_priv
*priv
= to_pxa3xx_gcu_priv(file
);
477 switch (vma
->vm_pgoff
) {
479 /* hand out the shared data area */
480 if (size
!= SHARED_SIZE
)
483 return dma_mmap_coherent(priv
->dev
, vma
,
484 priv
->shared
, priv
->shared_phys
, size
);
486 case SHARED_SIZE
>> PAGE_SHIFT
:
487 /* hand out the MMIO base for direct register access
489 if (size
!= resource_size(priv
->resource_mem
))
492 vma
->vm_page_prot
= pgprot_noncached(vma
->vm_page_prot
);
494 return io_remap_pfn_range(vma
, vma
->vm_start
,
495 priv
->resource_mem
->start
>> PAGE_SHIFT
,
496 size
, vma
->vm_page_prot
);
503 #ifdef PXA3XX_GCU_DEBUG_TIMER
504 static struct timer_list pxa3xx_gcu_debug_timer
;
505 static struct pxa3xx_gcu_priv
*debug_timer_priv
;
507 static void pxa3xx_gcu_debug_timedout(struct timer_list
*unused
)
509 struct pxa3xx_gcu_priv
*priv
= debug_timer_priv
;
511 QERROR("Timer DUMP");
513 mod_timer(&pxa3xx_gcu_debug_timer
, jiffies
+ 5 * HZ
);
516 static void pxa3xx_gcu_init_debug_timer(struct pxa3xx_gcu_priv
*priv
)
518 /* init the timer structure */
519 debug_timer_priv
= priv
;
520 timer_setup(&pxa3xx_gcu_debug_timer
, pxa3xx_gcu_debug_timedout
, 0);
521 pxa3xx_gcu_debug_timedout(NULL
);
524 static inline void pxa3xx_gcu_init_debug_timer(struct pxa3xx_gcu_priv
*priv
) {}
528 pxa3xx_gcu_add_buffer(struct device
*dev
,
529 struct pxa3xx_gcu_priv
*priv
)
531 struct pxa3xx_gcu_batch
*buffer
;
533 buffer
= kzalloc(sizeof(struct pxa3xx_gcu_batch
), GFP_KERNEL
);
537 buffer
->ptr
= dma_alloc_coherent(dev
, PXA3XX_GCU_BATCH_WORDS
* 4,
538 &buffer
->phys
, GFP_KERNEL
);
544 buffer
->next
= priv
->free
;
551 pxa3xx_gcu_free_buffers(struct device
*dev
,
552 struct pxa3xx_gcu_priv
*priv
)
554 struct pxa3xx_gcu_batch
*next
, *buffer
= priv
->free
;
559 dma_free_coherent(dev
, PXA3XX_GCU_BATCH_WORDS
* 4,
560 buffer
->ptr
, buffer
->phys
);
569 static const struct file_operations pxa3xx_gcu_miscdev_fops
= {
570 .owner
= THIS_MODULE
,
571 .open
= pxa3xx_gcu_open
,
572 .write
= pxa3xx_gcu_write
,
573 .unlocked_ioctl
= pxa3xx_gcu_ioctl
,
574 .mmap
= pxa3xx_gcu_mmap
,
577 static int pxa3xx_gcu_probe(struct platform_device
*pdev
)
581 struct pxa3xx_gcu_priv
*priv
;
582 struct device
*dev
= &pdev
->dev
;
584 priv
= devm_kzalloc(dev
, sizeof(struct pxa3xx_gcu_priv
), GFP_KERNEL
);
588 init_waitqueue_head(&priv
->wait_idle
);
589 init_waitqueue_head(&priv
->wait_free
);
590 spin_lock_init(&priv
->spinlock
);
592 /* we allocate the misc device structure as part of our own allocation,
593 * so we can get a pointer to our priv structure later on with
594 * container_of(). This isn't really necessary as we have a fixed minor
595 * number anyway, but this is to avoid statics. */
597 priv
->misc_dev
.minor
= PXA3XX_GCU_MINOR
,
598 priv
->misc_dev
.name
= DRV_NAME
,
599 priv
->misc_dev
.fops
= &pxa3xx_gcu_miscdev_fops
;
601 /* handle IO resources */
602 r
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
603 priv
->mmio_base
= devm_ioremap_resource(dev
, r
);
604 if (IS_ERR(priv
->mmio_base
))
605 return PTR_ERR(priv
->mmio_base
);
607 /* enable the clock */
608 priv
->clk
= devm_clk_get(dev
, NULL
);
609 if (IS_ERR(priv
->clk
)) {
610 dev_err(dev
, "failed to get clock\n");
611 return PTR_ERR(priv
->clk
);
614 /* request the IRQ */
615 irq
= platform_get_irq(pdev
, 0);
617 dev_err(dev
, "no IRQ defined: %d\n", irq
);
621 ret
= devm_request_irq(dev
, irq
, pxa3xx_gcu_handle_irq
,
624 dev_err(dev
, "request_irq failed\n");
628 /* allocate dma memory */
629 priv
->shared
= dma_alloc_coherent(dev
, SHARED_SIZE
,
630 &priv
->shared_phys
, GFP_KERNEL
);
632 dev_err(dev
, "failed to allocate DMA memory\n");
636 /* register misc device */
637 ret
= misc_register(&priv
->misc_dev
);
639 dev_err(dev
, "misc_register() for minor %d failed\n",
644 ret
= clk_prepare_enable(priv
->clk
);
646 dev_err(dev
, "failed to enable clock\n");
647 goto err_misc_deregister
;
650 for (i
= 0; i
< 8; i
++) {
651 ret
= pxa3xx_gcu_add_buffer(dev
, priv
);
653 dev_err(dev
, "failed to allocate DMA memory\n");
654 goto err_disable_clk
;
658 platform_set_drvdata(pdev
, priv
);
659 priv
->resource_mem
= r
;
661 pxa3xx_gcu_reset(priv
);
662 pxa3xx_gcu_init_debug_timer(priv
);
664 dev_info(dev
, "registered @0x%p, DMA 0x%p (%d bytes), IRQ %d\n",
665 (void *) r
->start
, (void *) priv
->shared_phys
,
670 dma_free_coherent(dev
, SHARED_SIZE
,
671 priv
->shared
, priv
->shared_phys
);
674 misc_deregister(&priv
->misc_dev
);
677 clk_disable_unprepare(priv
->clk
);
682 static int pxa3xx_gcu_remove(struct platform_device
*pdev
)
684 struct pxa3xx_gcu_priv
*priv
= platform_get_drvdata(pdev
);
685 struct device
*dev
= &pdev
->dev
;
687 pxa3xx_gcu_wait_idle(priv
);
688 misc_deregister(&priv
->misc_dev
);
689 dma_free_coherent(dev
, SHARED_SIZE
, priv
->shared
, priv
->shared_phys
);
690 pxa3xx_gcu_free_buffers(dev
, priv
);
696 static const struct of_device_id pxa3xx_gcu_of_match
[] = {
697 { .compatible
= "marvell,pxa300-gcu", },
700 MODULE_DEVICE_TABLE(of
, pxa3xx_gcu_of_match
);
703 static struct platform_driver pxa3xx_gcu_driver
= {
704 .probe
= pxa3xx_gcu_probe
,
705 .remove
= pxa3xx_gcu_remove
,
708 .of_match_table
= of_match_ptr(pxa3xx_gcu_of_match
),
712 module_platform_driver(pxa3xx_gcu_driver
);
714 MODULE_DESCRIPTION("PXA3xx graphics controller unit driver");
715 MODULE_LICENSE("GPL");
716 MODULE_ALIAS_MISCDEV(PXA3XX_GCU_MINOR
);
717 MODULE_AUTHOR("Janine Kropp <nin@directfb.org>, "
718 "Denis Oliver Kropp <dok@directfb.org>, "
719 "Daniel Mack <daniel@caiaq.de>");