2 * pxa3xx-gcu.c - Linux kernel module for PXA3xx graphics controllers
4 * This driver needs a DirectFB counterpart in user space, communication
5 * is handled via mmap()ed memory areas and an ioctl.
7 * Copyright (c) 2009 Daniel Mack <daniel@caiaq.de>
8 * Copyright (c) 2009 Janine Kropp <nin@directfb.org>
9 * Copyright (c) 2009 Denis Oliver Kropp <dok@directfb.org>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27 * WARNING: This controller is attached to System Bus 2 of the PXA which
28 * needs its arbiter to be enabled explicitly (CKENB & 1<<9).
29 * There is currently no way to do this from Linux, so you need to teach
30 * your bootloader for now.
33 #include <linux/module.h>
34 #include <linux/platform_device.h>
35 #include <linux/dma-mapping.h>
36 #include <linux/miscdevice.h>
37 #include <linux/interrupt.h>
38 #include <linux/spinlock.h>
39 #include <linux/uaccess.h>
40 #include <linux/ioctl.h>
41 #include <linux/delay.h>
42 #include <linux/sched.h>
43 #include <linux/slab.h>
44 #include <linux/clk.h>
48 #include "pxa3xx-gcu.h"
50 #define DRV_NAME "pxa3xx-gcu"
51 #define MISCDEV_MINOR 197
54 #define GCCR_SYNC_CLR (1 << 9)
55 #define GCCR_BP_RST (1 << 8)
56 #define GCCR_ABORT (1 << 6)
57 #define GCCR_STOP (1 << 4)
59 #define REG_GCISCR 0x04
60 #define REG_GCIECR 0x08
61 #define REG_GCRBBR 0x20
62 #define REG_GCRBLR 0x24
63 #define REG_GCRBHR 0x28
64 #define REG_GCRBTR 0x2C
65 #define REG_GCRBEXHR 0x30
67 #define IE_EOB (1 << 0)
68 #define IE_EEOB (1 << 5)
71 #define SHARED_SIZE PAGE_ALIGN(sizeof(struct pxa3xx_gcu_shared))
73 /* #define PXA3XX_GCU_DEBUG */
74 /* #define PXA3XX_GCU_DEBUG_TIMER */
76 #ifdef PXA3XX_GCU_DEBUG
79 QPRINT(priv, KERN_DEBUG, msg); \
82 #define QDUMP(msg) do {} while (0)
87 QPRINT(priv, KERN_ERR, msg); \
90 struct pxa3xx_gcu_batch
{
91 struct pxa3xx_gcu_batch
*next
;
97 struct pxa3xx_gcu_priv
{
98 void __iomem
*mmio_base
;
100 struct pxa3xx_gcu_shared
*shared
;
101 dma_addr_t shared_phys
;
102 struct resource
*resource_mem
;
103 struct miscdevice misc_dev
;
104 wait_queue_head_t wait_idle
;
105 wait_queue_head_t wait_free
;
107 struct timeval base_time
;
109 struct pxa3xx_gcu_batch
*free
;
110 struct pxa3xx_gcu_batch
*ready
;
111 struct pxa3xx_gcu_batch
*ready_last
;
112 struct pxa3xx_gcu_batch
*running
;
115 static inline unsigned long
116 gc_readl(struct pxa3xx_gcu_priv
*priv
, unsigned int off
)
118 return __raw_readl(priv
->mmio_base
+ off
);
122 gc_writel(struct pxa3xx_gcu_priv
*priv
, unsigned int off
, unsigned long val
)
124 __raw_writel(val
, priv
->mmio_base
+ off
);
127 #define QPRINT(priv, level, msg) \
130 struct pxa3xx_gcu_shared *shared = priv->shared; \
131 u32 base = gc_readl(priv, REG_GCRBBR); \
133 do_gettimeofday(&tv); \
135 printk(level "%ld.%03ld.%03ld - %-17s: %-21s (%s, " \
137 "0x%02lx, B 0x%08lx [%ld], E %5ld, H %5ld, " \
139 tv.tv_sec - priv->base_time.tv_sec, \
140 tv.tv_usec / 1000, tv.tv_usec % 1000, \
142 shared->hw_running ? "running" : " idle", \
143 gc_readl(priv, REG_GCISCR), \
144 gc_readl(priv, REG_GCRBBR), \
145 gc_readl(priv, REG_GCRBLR), \
146 (gc_readl(priv, REG_GCRBEXHR) - base) / 4, \
147 (gc_readl(priv, REG_GCRBHR) - base) / 4, \
148 (gc_readl(priv, REG_GCRBTR) - base) / 4); \
152 pxa3xx_gcu_reset(struct pxa3xx_gcu_priv
*priv
)
156 /* disable interrupts */
157 gc_writel(priv
, REG_GCIECR
, 0);
160 gc_writel(priv
, REG_GCCR
, GCCR_ABORT
);
161 gc_writel(priv
, REG_GCCR
, 0);
163 memset(priv
->shared
, 0, SHARED_SIZE
);
164 priv
->shared
->buffer_phys
= priv
->shared_phys
;
165 priv
->shared
->magic
= PXA3XX_GCU_SHARED_MAGIC
;
167 do_gettimeofday(&priv
->base_time
);
169 /* set up the ring buffer pointers */
170 gc_writel(priv
, REG_GCRBLR
, 0);
171 gc_writel(priv
, REG_GCRBBR
, priv
->shared_phys
);
172 gc_writel(priv
, REG_GCRBTR
, priv
->shared_phys
);
174 /* enable all IRQs except EOB */
175 gc_writel(priv
, REG_GCIECR
, IE_ALL
& ~IE_EOB
);
179 dump_whole_state(struct pxa3xx_gcu_priv
*priv
)
181 struct pxa3xx_gcu_shared
*sh
= priv
->shared
;
182 u32 base
= gc_readl(priv
, REG_GCRBBR
);
186 printk(KERN_DEBUG
"== PXA3XX-GCU DUMP ==\n"
187 "%s, STATUS 0x%02lx, B 0x%08lx [%ld], E %5ld, H %5ld, T %5ld\n",
188 sh
->hw_running
? "running" : "idle ",
189 gc_readl(priv
, REG_GCISCR
),
190 gc_readl(priv
, REG_GCRBBR
),
191 gc_readl(priv
, REG_GCRBLR
),
192 (gc_readl(priv
, REG_GCRBEXHR
) - base
) / 4,
193 (gc_readl(priv
, REG_GCRBHR
) - base
) / 4,
194 (gc_readl(priv
, REG_GCRBTR
) - base
) / 4);
198 flush_running(struct pxa3xx_gcu_priv
*priv
)
200 struct pxa3xx_gcu_batch
*running
= priv
->running
;
201 struct pxa3xx_gcu_batch
*next
;
204 next
= running
->next
;
205 running
->next
= priv
->free
;
206 priv
->free
= running
;
210 priv
->running
= NULL
;
214 run_ready(struct pxa3xx_gcu_priv
*priv
)
216 unsigned int num
= 0;
217 struct pxa3xx_gcu_shared
*shared
= priv
->shared
;
218 struct pxa3xx_gcu_batch
*ready
= priv
->ready
;
224 shared
->buffer
[num
++] = 0x05000000;
227 shared
->buffer
[num
++] = 0x00000001;
228 shared
->buffer
[num
++] = ready
->phys
;
232 shared
->buffer
[num
++] = 0x05000000;
233 priv
->running
= priv
->ready
;
234 priv
->ready
= priv
->ready_last
= NULL
;
235 gc_writel(priv
, REG_GCRBLR
, 0);
236 shared
->hw_running
= 1;
238 /* ring base address */
239 gc_writel(priv
, REG_GCRBBR
, shared
->buffer_phys
);
241 /* ring tail address */
242 gc_writel(priv
, REG_GCRBTR
, shared
->buffer_phys
+ num
* 4);
245 gc_writel(priv
, REG_GCRBLR
, ((num
+ 63) & ~63) * 4);
249 pxa3xx_gcu_handle_irq(int irq
, void *ctx
)
251 struct pxa3xx_gcu_priv
*priv
= ctx
;
252 struct pxa3xx_gcu_shared
*shared
= priv
->shared
;
253 u32 status
= gc_readl(priv
, REG_GCISCR
) & IE_ALL
;
260 spin_lock(&priv
->spinlock
);
261 shared
->num_interrupts
++;
263 if (status
& IE_EEOB
) {
267 wake_up_all(&priv
->wait_free
);
272 /* There is no more data prepared by the userspace.
273 * Set hw_running = 0 and wait for the next userspace
276 shared
->hw_running
= 0;
280 /* set ring buffer length to zero */
281 gc_writel(priv
, REG_GCRBLR
, 0);
283 wake_up_all(&priv
->wait_idle
);
289 dump_whole_state(priv
);
292 /* Clear the interrupt */
293 gc_writel(priv
, REG_GCISCR
, status
);
294 spin_unlock(&priv
->spinlock
);
300 pxa3xx_gcu_wait_idle(struct pxa3xx_gcu_priv
*priv
)
304 QDUMP("Waiting for idle...");
306 /* Does not need to be atomic. There's a lock in user space,
307 * but anyhow, this is just for statistics. */
308 priv
->shared
->num_wait_idle
++;
310 while (priv
->shared
->hw_running
) {
311 int num
= priv
->shared
->num_interrupts
;
312 u32 rbexhr
= gc_readl(priv
, REG_GCRBEXHR
);
314 ret
= wait_event_interruptible_timeout(priv
->wait_idle
,
315 !priv
->shared
->hw_running
, HZ
*4);
320 if (gc_readl(priv
, REG_GCRBEXHR
) == rbexhr
&&
321 priv
->shared
->num_interrupts
== num
) {
334 pxa3xx_gcu_wait_free(struct pxa3xx_gcu_priv
*priv
)
338 QDUMP("Waiting for free...");
340 /* Does not need to be atomic. There's a lock in user space,
341 * but anyhow, this is just for statistics. */
342 priv
->shared
->num_wait_free
++;
344 while (!priv
->free
) {
345 u32 rbexhr
= gc_readl(priv
, REG_GCRBEXHR
);
347 ret
= wait_event_interruptible_timeout(priv
->wait_free
,
356 if (gc_readl(priv
, REG_GCRBEXHR
) == rbexhr
) {
368 /* Misc device layer */
370 static inline struct pxa3xx_gcu_priv
*to_pxa3xx_gcu_priv(struct file
*file
)
372 struct miscdevice
*dev
= file
->private_data
;
373 return container_of(dev
, struct pxa3xx_gcu_priv
, misc_dev
);
377 * provide an empty .open callback, so the core sets file->private_data
380 static int pxa3xx_gcu_open(struct inode
*inode
, struct file
*file
)
386 pxa3xx_gcu_write(struct file
*file
, const char *buff
,
387 size_t count
, loff_t
*offp
)
391 struct pxa3xx_gcu_batch
*buffer
;
392 struct pxa3xx_gcu_priv
*priv
= to_pxa3xx_gcu_priv(file
);
394 int words
= count
/ 4;
396 /* Does not need to be atomic. There's a lock in user space,
397 * but anyhow, this is just for statistics. */
398 priv
->shared
->num_writes
++;
399 priv
->shared
->num_words
+= words
;
401 /* Last word reserved for batch buffer end command */
402 if (words
>= PXA3XX_GCU_BATCH_WORDS
)
405 /* Wait for a free buffer */
407 ret
= pxa3xx_gcu_wait_free(priv
);
413 * Get buffer from free list
415 spin_lock_irqsave(&priv
->spinlock
, flags
);
417 priv
->free
= buffer
->next
;
418 spin_unlock_irqrestore(&priv
->spinlock
, flags
);
421 /* Copy data from user into buffer */
422 ret
= copy_from_user(buffer
->ptr
, buff
, words
* 4);
424 spin_lock_irqsave(&priv
->spinlock
, flags
);
425 buffer
->next
= priv
->free
;
427 spin_unlock_irqrestore(&priv
->spinlock
, flags
);
431 buffer
->length
= words
;
433 /* Append batch buffer end command */
434 buffer
->ptr
[words
] = 0x01000000;
437 * Add buffer to ready list
439 spin_lock_irqsave(&priv
->spinlock
, flags
);
444 BUG_ON(priv
->ready_last
== NULL
);
446 priv
->ready_last
->next
= buffer
;
448 priv
->ready
= buffer
;
450 priv
->ready_last
= buffer
;
452 if (!priv
->shared
->hw_running
)
455 spin_unlock_irqrestore(&priv
->spinlock
, flags
);
462 pxa3xx_gcu_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
465 struct pxa3xx_gcu_priv
*priv
= to_pxa3xx_gcu_priv(file
);
468 case PXA3XX_GCU_IOCTL_RESET
:
469 spin_lock_irqsave(&priv
->spinlock
, flags
);
470 pxa3xx_gcu_reset(priv
);
471 spin_unlock_irqrestore(&priv
->spinlock
, flags
);
474 case PXA3XX_GCU_IOCTL_WAIT_IDLE
:
475 return pxa3xx_gcu_wait_idle(priv
);
482 pxa3xx_gcu_mmap(struct file
*file
, struct vm_area_struct
*vma
)
484 unsigned int size
= vma
->vm_end
- vma
->vm_start
;
485 struct pxa3xx_gcu_priv
*priv
= to_pxa3xx_gcu_priv(file
);
487 switch (vma
->vm_pgoff
) {
489 /* hand out the shared data area */
490 if (size
!= SHARED_SIZE
)
493 return dma_mmap_coherent(NULL
, vma
,
494 priv
->shared
, priv
->shared_phys
, size
);
496 case SHARED_SIZE
>> PAGE_SHIFT
:
497 /* hand out the MMIO base for direct register access
499 if (size
!= resource_size(priv
->resource_mem
))
502 vma
->vm_page_prot
= pgprot_noncached(vma
->vm_page_prot
);
504 return io_remap_pfn_range(vma
, vma
->vm_start
,
505 priv
->resource_mem
->start
>> PAGE_SHIFT
,
506 size
, vma
->vm_page_prot
);
513 #ifdef PXA3XX_GCU_DEBUG_TIMER
514 static struct timer_list pxa3xx_gcu_debug_timer
;
516 static void pxa3xx_gcu_debug_timedout(unsigned long ptr
)
518 struct pxa3xx_gcu_priv
*priv
= (struct pxa3xx_gcu_priv
*) ptr
;
520 QERROR("Timer DUMP");
522 /* init the timer structure */
523 init_timer(&pxa3xx_gcu_debug_timer
);
524 pxa3xx_gcu_debug_timer
.function
= pxa3xx_gcu_debug_timedout
;
525 pxa3xx_gcu_debug_timer
.data
= ptr
;
526 pxa3xx_gcu_debug_timer
.expires
= jiffies
+ 5*HZ
; /* one second */
528 add_timer(&pxa3xx_gcu_debug_timer
);
531 static void pxa3xx_gcu_init_debug_timer(void)
533 pxa3xx_gcu_debug_timedout((unsigned long) &pxa3xx_gcu_debug_timer
);
536 static inline void pxa3xx_gcu_init_debug_timer(void) {}
540 pxa3xx_gcu_add_buffer(struct device
*dev
,
541 struct pxa3xx_gcu_priv
*priv
)
543 struct pxa3xx_gcu_batch
*buffer
;
545 buffer
= kzalloc(sizeof(struct pxa3xx_gcu_batch
), GFP_KERNEL
);
549 buffer
->ptr
= dma_alloc_coherent(dev
, PXA3XX_GCU_BATCH_WORDS
* 4,
550 &buffer
->phys
, GFP_KERNEL
);
556 buffer
->next
= priv
->free
;
563 pxa3xx_gcu_free_buffers(struct device
*dev
,
564 struct pxa3xx_gcu_priv
*priv
)
566 struct pxa3xx_gcu_batch
*next
, *buffer
= priv
->free
;
571 dma_free_coherent(dev
, PXA3XX_GCU_BATCH_WORDS
* 4,
572 buffer
->ptr
, buffer
->phys
);
581 static const struct file_operations pxa3xx_gcu_miscdev_fops
= {
582 .owner
= THIS_MODULE
,
583 .open
= pxa3xx_gcu_open
,
584 .write
= pxa3xx_gcu_write
,
585 .unlocked_ioctl
= pxa3xx_gcu_ioctl
,
586 .mmap
= pxa3xx_gcu_mmap
,
589 static int pxa3xx_gcu_probe(struct platform_device
*pdev
)
593 struct pxa3xx_gcu_priv
*priv
;
594 struct device
*dev
= &pdev
->dev
;
596 priv
= devm_kzalloc(dev
, sizeof(struct pxa3xx_gcu_priv
), GFP_KERNEL
);
600 init_waitqueue_head(&priv
->wait_idle
);
601 init_waitqueue_head(&priv
->wait_free
);
602 spin_lock_init(&priv
->spinlock
);
604 /* we allocate the misc device structure as part of our own allocation,
605 * so we can get a pointer to our priv structure later on with
606 * container_of(). This isn't really necessary as we have a fixed minor
607 * number anyway, but this is to avoid statics. */
609 priv
->misc_dev
.minor
= MISCDEV_MINOR
,
610 priv
->misc_dev
.name
= DRV_NAME
,
611 priv
->misc_dev
.fops
= &pxa3xx_gcu_miscdev_fops
;
613 /* handle IO resources */
614 r
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
615 priv
->mmio_base
= devm_ioremap_resource(dev
, r
);
616 if (IS_ERR(priv
->mmio_base
))
617 return PTR_ERR(priv
->mmio_base
);
619 /* enable the clock */
620 priv
->clk
= devm_clk_get(dev
, NULL
);
621 if (IS_ERR(priv
->clk
)) {
622 dev_err(dev
, "failed to get clock\n");
623 return PTR_ERR(priv
->clk
);
626 /* request the IRQ */
627 irq
= platform_get_irq(pdev
, 0);
629 dev_err(dev
, "no IRQ defined\n");
633 ret
= devm_request_irq(dev
, irq
, pxa3xx_gcu_handle_irq
,
636 dev_err(dev
, "request_irq failed\n");
640 /* allocate dma memory */
641 priv
->shared
= dma_alloc_coherent(dev
, SHARED_SIZE
,
642 &priv
->shared_phys
, GFP_KERNEL
);
644 dev_err(dev
, "failed to allocate DMA memory\n");
648 /* register misc device */
649 ret
= misc_register(&priv
->misc_dev
);
651 dev_err(dev
, "misc_register() for minor %d failed\n",
656 ret
= clk_prepare_enable(priv
->clk
);
658 dev_err(dev
, "failed to enable clock\n");
659 goto err_misc_deregister
;
662 for (i
= 0; i
< 8; i
++) {
663 ret
= pxa3xx_gcu_add_buffer(dev
, priv
);
665 dev_err(dev
, "failed to allocate DMA memory\n");
666 goto err_disable_clk
;
670 platform_set_drvdata(pdev
, priv
);
671 priv
->resource_mem
= r
;
672 pxa3xx_gcu_reset(priv
);
673 pxa3xx_gcu_init_debug_timer();
675 dev_info(dev
, "registered @0x%p, DMA 0x%p (%d bytes), IRQ %d\n",
676 (void *) r
->start
, (void *) priv
->shared_phys
,
681 dma_free_coherent(dev
, SHARED_SIZE
,
682 priv
->shared
, priv
->shared_phys
);
685 misc_deregister(&priv
->misc_dev
);
688 clk_disable_unprepare(priv
->clk
);
693 static int pxa3xx_gcu_remove(struct platform_device
*pdev
)
695 struct pxa3xx_gcu_priv
*priv
= platform_get_drvdata(pdev
);
696 struct device
*dev
= &pdev
->dev
;
698 pxa3xx_gcu_wait_idle(priv
);
699 misc_deregister(&priv
->misc_dev
);
700 dma_free_coherent(dev
, SHARED_SIZE
, priv
->shared
, priv
->shared_phys
);
701 pxa3xx_gcu_free_buffers(dev
, priv
);
706 static struct platform_driver pxa3xx_gcu_driver
= {
707 .probe
= pxa3xx_gcu_probe
,
708 .remove
= pxa3xx_gcu_remove
,
714 module_platform_driver(pxa3xx_gcu_driver
);
716 MODULE_DESCRIPTION("PXA3xx graphics controller unit driver");
717 MODULE_LICENSE("GPL");
718 MODULE_ALIAS_MISCDEV(MISCDEV_MINOR
);
719 MODULE_AUTHOR("Janine Kropp <nin@directfb.org>, "
720 "Denis Oliver Kropp <dok@directfb.org>, "
721 "Daniel Mack <daniel@caiaq.de>");