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
;
111 struct pxa3xx_gcu_batch
*ready
;
112 struct pxa3xx_gcu_batch
*ready_last
;
113 struct pxa3xx_gcu_batch
*running
;
116 static inline unsigned long
117 gc_readl(struct pxa3xx_gcu_priv
*priv
, unsigned int off
)
119 return __raw_readl(priv
->mmio_base
+ off
);
123 gc_writel(struct pxa3xx_gcu_priv
*priv
, unsigned int off
, unsigned long val
)
125 __raw_writel(val
, priv
->mmio_base
+ off
);
128 #define QPRINT(priv, level, msg) \
131 struct pxa3xx_gcu_shared *shared = priv->shared; \
132 u32 base = gc_readl(priv, REG_GCRBBR); \
134 do_gettimeofday(&tv); \
136 printk(level "%ld.%03ld.%03ld - %-17s: %-21s (%s, " \
138 "0x%02lx, B 0x%08lx [%ld], E %5ld, H %5ld, " \
140 tv.tv_sec - priv->base_time.tv_sec, \
141 tv.tv_usec / 1000, tv.tv_usec % 1000, \
143 shared->hw_running ? "running" : " idle", \
144 gc_readl(priv, REG_GCISCR), \
145 gc_readl(priv, REG_GCRBBR), \
146 gc_readl(priv, REG_GCRBLR), \
147 (gc_readl(priv, REG_GCRBEXHR) - base) / 4, \
148 (gc_readl(priv, REG_GCRBHR) - base) / 4, \
149 (gc_readl(priv, REG_GCRBTR) - base) / 4); \
153 pxa3xx_gcu_reset(struct pxa3xx_gcu_priv
*priv
)
157 /* disable interrupts */
158 gc_writel(priv
, REG_GCIECR
, 0);
161 gc_writel(priv
, REG_GCCR
, GCCR_ABORT
);
162 gc_writel(priv
, REG_GCCR
, 0);
164 memset(priv
->shared
, 0, SHARED_SIZE
);
165 priv
->shared
->buffer_phys
= priv
->shared_phys
;
166 priv
->shared
->magic
= PXA3XX_GCU_SHARED_MAGIC
;
168 do_gettimeofday(&priv
->base_time
);
170 /* set up the ring buffer pointers */
171 gc_writel(priv
, REG_GCRBLR
, 0);
172 gc_writel(priv
, REG_GCRBBR
, priv
->shared_phys
);
173 gc_writel(priv
, REG_GCRBTR
, priv
->shared_phys
);
175 /* enable all IRQs except EOB */
176 gc_writel(priv
, REG_GCIECR
, IE_ALL
& ~IE_EOB
);
180 dump_whole_state(struct pxa3xx_gcu_priv
*priv
)
182 struct pxa3xx_gcu_shared
*sh
= priv
->shared
;
183 u32 base
= gc_readl(priv
, REG_GCRBBR
);
187 printk(KERN_DEBUG
"== PXA3XX-GCU DUMP ==\n"
188 "%s, STATUS 0x%02lx, B 0x%08lx [%ld], E %5ld, H %5ld, T %5ld\n",
189 sh
->hw_running
? "running" : "idle ",
190 gc_readl(priv
, REG_GCISCR
),
191 gc_readl(priv
, REG_GCRBBR
),
192 gc_readl(priv
, REG_GCRBLR
),
193 (gc_readl(priv
, REG_GCRBEXHR
) - base
) / 4,
194 (gc_readl(priv
, REG_GCRBHR
) - base
) / 4,
195 (gc_readl(priv
, REG_GCRBTR
) - base
) / 4);
199 flush_running(struct pxa3xx_gcu_priv
*priv
)
201 struct pxa3xx_gcu_batch
*running
= priv
->running
;
202 struct pxa3xx_gcu_batch
*next
;
205 next
= running
->next
;
206 running
->next
= priv
->free
;
207 priv
->free
= running
;
211 priv
->running
= NULL
;
215 run_ready(struct pxa3xx_gcu_priv
*priv
)
217 unsigned int num
= 0;
218 struct pxa3xx_gcu_shared
*shared
= priv
->shared
;
219 struct pxa3xx_gcu_batch
*ready
= priv
->ready
;
225 shared
->buffer
[num
++] = 0x05000000;
228 shared
->buffer
[num
++] = 0x00000001;
229 shared
->buffer
[num
++] = ready
->phys
;
233 shared
->buffer
[num
++] = 0x05000000;
234 priv
->running
= priv
->ready
;
235 priv
->ready
= priv
->ready_last
= NULL
;
236 gc_writel(priv
, REG_GCRBLR
, 0);
237 shared
->hw_running
= 1;
239 /* ring base address */
240 gc_writel(priv
, REG_GCRBBR
, shared
->buffer_phys
);
242 /* ring tail address */
243 gc_writel(priv
, REG_GCRBTR
, shared
->buffer_phys
+ num
* 4);
246 gc_writel(priv
, REG_GCRBLR
, ((num
+ 63) & ~63) * 4);
250 pxa3xx_gcu_handle_irq(int irq
, void *ctx
)
252 struct pxa3xx_gcu_priv
*priv
= ctx
;
253 struct pxa3xx_gcu_shared
*shared
= priv
->shared
;
254 u32 status
= gc_readl(priv
, REG_GCISCR
) & IE_ALL
;
261 spin_lock(&priv
->spinlock
);
262 shared
->num_interrupts
++;
264 if (status
& IE_EEOB
) {
268 wake_up_all(&priv
->wait_free
);
273 /* There is no more data prepared by the userspace.
274 * Set hw_running = 0 and wait for the next userspace
277 shared
->hw_running
= 0;
281 /* set ring buffer length to zero */
282 gc_writel(priv
, REG_GCRBLR
, 0);
284 wake_up_all(&priv
->wait_idle
);
290 dump_whole_state(priv
);
293 /* Clear the interrupt */
294 gc_writel(priv
, REG_GCISCR
, status
);
295 spin_unlock(&priv
->spinlock
);
301 pxa3xx_gcu_wait_idle(struct pxa3xx_gcu_priv
*priv
)
305 QDUMP("Waiting for idle...");
307 /* Does not need to be atomic. There's a lock in user space,
308 * but anyhow, this is just for statistics. */
309 priv
->shared
->num_wait_idle
++;
311 while (priv
->shared
->hw_running
) {
312 int num
= priv
->shared
->num_interrupts
;
313 u32 rbexhr
= gc_readl(priv
, REG_GCRBEXHR
);
315 ret
= wait_event_interruptible_timeout(priv
->wait_idle
,
316 !priv
->shared
->hw_running
, HZ
*4);
321 if (gc_readl(priv
, REG_GCRBEXHR
) == rbexhr
&&
322 priv
->shared
->num_interrupts
== num
) {
335 pxa3xx_gcu_wait_free(struct pxa3xx_gcu_priv
*priv
)
339 QDUMP("Waiting for free...");
341 /* Does not need to be atomic. There's a lock in user space,
342 * but anyhow, this is just for statistics. */
343 priv
->shared
->num_wait_free
++;
345 while (!priv
->free
) {
346 u32 rbexhr
= gc_readl(priv
, REG_GCRBEXHR
);
348 ret
= wait_event_interruptible_timeout(priv
->wait_free
,
357 if (gc_readl(priv
, REG_GCRBEXHR
) == rbexhr
) {
369 /* Misc device layer */
371 static inline struct pxa3xx_gcu_priv
*file_dev(struct file
*file
)
373 struct miscdevice
*dev
= file
->private_data
;
374 return container_of(dev
, struct pxa3xx_gcu_priv
, misc_dev
);
378 pxa3xx_gcu_misc_write(struct file
*file
, const char *buff
,
379 size_t count
, loff_t
*offp
)
383 struct pxa3xx_gcu_batch
*buffer
;
384 struct pxa3xx_gcu_priv
*priv
= file_dev(file
);
386 int words
= count
/ 4;
388 /* Does not need to be atomic. There's a lock in user space,
389 * but anyhow, this is just for statistics. */
390 priv
->shared
->num_writes
++;
392 priv
->shared
->num_words
+= words
;
394 /* Last word reserved for batch buffer end command */
395 if (words
>= PXA3XX_GCU_BATCH_WORDS
)
398 /* Wait for a free buffer */
400 ret
= pxa3xx_gcu_wait_free(priv
);
406 * Get buffer from free list
408 spin_lock_irqsave(&priv
->spinlock
, flags
);
411 priv
->free
= buffer
->next
;
413 spin_unlock_irqrestore(&priv
->spinlock
, flags
);
416 /* Copy data from user into buffer */
417 ret
= copy_from_user(buffer
->ptr
, buff
, words
* 4);
419 spin_lock_irqsave(&priv
->spinlock
, flags
);
420 buffer
->next
= priv
->free
;
422 spin_unlock_irqrestore(&priv
->spinlock
, flags
);
426 buffer
->length
= words
;
428 /* Append batch buffer end command */
429 buffer
->ptr
[words
] = 0x01000000;
432 * Add buffer to ready list
434 spin_lock_irqsave(&priv
->spinlock
, flags
);
439 BUG_ON(priv
->ready_last
== NULL
);
441 priv
->ready_last
->next
= buffer
;
443 priv
->ready
= buffer
;
445 priv
->ready_last
= buffer
;
447 if (!priv
->shared
->hw_running
)
450 spin_unlock_irqrestore(&priv
->spinlock
, flags
);
457 pxa3xx_gcu_misc_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
460 struct pxa3xx_gcu_priv
*priv
= file_dev(file
);
463 case PXA3XX_GCU_IOCTL_RESET
:
464 spin_lock_irqsave(&priv
->spinlock
, flags
);
465 pxa3xx_gcu_reset(priv
);
466 spin_unlock_irqrestore(&priv
->spinlock
, flags
);
469 case PXA3XX_GCU_IOCTL_WAIT_IDLE
:
470 return pxa3xx_gcu_wait_idle(priv
);
477 pxa3xx_gcu_misc_mmap(struct file
*file
, struct vm_area_struct
*vma
)
479 unsigned int size
= vma
->vm_end
- vma
->vm_start
;
480 struct pxa3xx_gcu_priv
*priv
= file_dev(file
);
482 switch (vma
->vm_pgoff
) {
484 /* hand out the shared data area */
485 if (size
!= SHARED_SIZE
)
488 return dma_mmap_coherent(NULL
, vma
,
489 priv
->shared
, priv
->shared_phys
, size
);
491 case SHARED_SIZE
>> PAGE_SHIFT
:
492 /* hand out the MMIO base for direct register access
494 if (size
!= resource_size(priv
->resource_mem
))
497 vma
->vm_flags
|= VM_IO
;
498 vma
->vm_page_prot
= pgprot_noncached(vma
->vm_page_prot
);
500 return io_remap_pfn_range(vma
, vma
->vm_start
,
501 priv
->resource_mem
->start
>> PAGE_SHIFT
,
502 size
, vma
->vm_page_prot
);
509 #ifdef PXA3XX_GCU_DEBUG_TIMER
510 static struct timer_list pxa3xx_gcu_debug_timer
;
512 static void pxa3xx_gcu_debug_timedout(unsigned long ptr
)
514 struct pxa3xx_gcu_priv
*priv
= (struct pxa3xx_gcu_priv
*) ptr
;
516 QERROR("Timer DUMP");
518 /* init the timer structure */
519 init_timer(&pxa3xx_gcu_debug_timer
);
520 pxa3xx_gcu_debug_timer
.function
= pxa3xx_gcu_debug_timedout
;
521 pxa3xx_gcu_debug_timer
.data
= ptr
;
522 pxa3xx_gcu_debug_timer
.expires
= jiffies
+ 5*HZ
; /* one second */
524 add_timer(&pxa3xx_gcu_debug_timer
);
527 static void pxa3xx_gcu_init_debug_timer(void)
529 pxa3xx_gcu_debug_timedout((unsigned long) &pxa3xx_gcu_debug_timer
);
532 static inline void pxa3xx_gcu_init_debug_timer(void) {}
536 add_buffer(struct platform_device
*dev
,
537 struct pxa3xx_gcu_priv
*priv
)
539 struct pxa3xx_gcu_batch
*buffer
;
541 buffer
= kzalloc(sizeof(struct pxa3xx_gcu_batch
), GFP_KERNEL
);
545 buffer
->ptr
= dma_alloc_coherent(&dev
->dev
, PXA3XX_GCU_BATCH_WORDS
* 4,
546 &buffer
->phys
, GFP_KERNEL
);
552 buffer
->next
= priv
->free
;
560 free_buffers(struct platform_device
*dev
,
561 struct pxa3xx_gcu_priv
*priv
)
563 struct pxa3xx_gcu_batch
*next
, *buffer
= priv
->free
;
568 dma_free_coherent(&dev
->dev
, PXA3XX_GCU_BATCH_WORDS
* 4,
569 buffer
->ptr
, buffer
->phys
);
579 static const struct file_operations misc_fops
= {
580 .owner
= THIS_MODULE
,
581 .write
= pxa3xx_gcu_misc_write
,
582 .unlocked_ioctl
= pxa3xx_gcu_misc_ioctl
,
583 .mmap
= pxa3xx_gcu_misc_mmap
586 static int pxa3xx_gcu_probe(struct platform_device
*dev
)
590 struct pxa3xx_gcu_priv
*priv
;
592 priv
= kzalloc(sizeof(struct pxa3xx_gcu_priv
), GFP_KERNEL
);
596 for (i
= 0; i
< 8; i
++) {
597 ret
= add_buffer(dev
, priv
);
599 dev_err(&dev
->dev
, "failed to allocate DMA memory\n");
604 init_waitqueue_head(&priv
->wait_idle
);
605 init_waitqueue_head(&priv
->wait_free
);
606 spin_lock_init(&priv
->spinlock
);
608 /* we allocate the misc device structure as part of our own allocation,
609 * so we can get a pointer to our priv structure later on with
610 * container_of(). This isn't really necessary as we have a fixed minor
611 * number anyway, but this is to avoid statics. */
613 priv
->misc_dev
.minor
= MISCDEV_MINOR
,
614 priv
->misc_dev
.name
= DRV_NAME
,
615 priv
->misc_dev
.fops
= &misc_fops
,
617 /* register misc device */
618 ret
= misc_register(&priv
->misc_dev
);
620 dev_err(&dev
->dev
, "misc_register() for minor %d failed\n",
625 /* handle IO resources */
626 r
= platform_get_resource(dev
, IORESOURCE_MEM
, 0);
628 dev_err(&dev
->dev
, "no I/O memory resource defined\n");
630 goto err_misc_deregister
;
633 if (!request_mem_region(r
->start
, resource_size(r
), dev
->name
)) {
634 dev_err(&dev
->dev
, "failed to request I/O memory\n");
636 goto err_misc_deregister
;
639 priv
->mmio_base
= ioremap_nocache(r
->start
, resource_size(r
));
640 if (!priv
->mmio_base
) {
641 dev_err(&dev
->dev
, "failed to map I/O memory\n");
643 goto err_free_mem_region
;
646 /* allocate dma memory */
647 priv
->shared
= dma_alloc_coherent(&dev
->dev
, SHARED_SIZE
,
648 &priv
->shared_phys
, GFP_KERNEL
);
651 dev_err(&dev
->dev
, "failed to allocate DMA memory\n");
656 /* enable the clock */
657 priv
->clk
= clk_get(&dev
->dev
, NULL
);
658 if (IS_ERR(priv
->clk
)) {
659 dev_err(&dev
->dev
, "failed to get clock\n");
664 ret
= clk_enable(priv
->clk
);
666 dev_err(&dev
->dev
, "failed to enable clock\n");
670 /* request the IRQ */
671 irq
= platform_get_irq(dev
, 0);
673 dev_err(&dev
->dev
, "no IRQ defined\n");
678 ret
= request_irq(irq
, pxa3xx_gcu_handle_irq
,
681 dev_err(&dev
->dev
, "request_irq failed\n");
686 platform_set_drvdata(dev
, priv
);
687 priv
->resource_mem
= r
;
688 pxa3xx_gcu_reset(priv
);
689 pxa3xx_gcu_init_debug_timer();
691 dev_info(&dev
->dev
, "registered @0x%p, DMA 0x%p (%d bytes), IRQ %d\n",
692 (void *) r
->start
, (void *) priv
->shared_phys
,
697 clk_disable(priv
->clk
);
701 dma_free_coherent(&dev
->dev
, SHARED_SIZE
,
702 priv
->shared
, priv
->shared_phys
);
705 iounmap(priv
->mmio_base
);
708 release_mem_region(r
->start
, resource_size(r
));
711 misc_deregister(&priv
->misc_dev
);
714 platform_set_drvdata(dev
, NULL
);
715 free_buffers(dev
, priv
);
720 static int pxa3xx_gcu_remove(struct platform_device
*dev
)
722 struct pxa3xx_gcu_priv
*priv
= platform_get_drvdata(dev
);
723 struct resource
*r
= priv
->resource_mem
;
725 pxa3xx_gcu_wait_idle(priv
);
727 misc_deregister(&priv
->misc_dev
);
728 dma_free_coherent(&dev
->dev
, SHARED_SIZE
,
729 priv
->shared
, priv
->shared_phys
);
730 iounmap(priv
->mmio_base
);
731 release_mem_region(r
->start
, resource_size(r
));
732 platform_set_drvdata(dev
, NULL
);
733 clk_disable(priv
->clk
);
734 free_buffers(dev
, priv
);
740 static struct platform_driver pxa3xx_gcu_driver
= {
741 .probe
= pxa3xx_gcu_probe
,
742 .remove
= pxa3xx_gcu_remove
,
744 .owner
= THIS_MODULE
,
749 module_platform_driver(pxa3xx_gcu_driver
);
751 MODULE_DESCRIPTION("PXA3xx graphics controller unit driver");
752 MODULE_LICENSE("GPL");
753 MODULE_ALIAS_MISCDEV(MISCDEV_MINOR
);
754 MODULE_AUTHOR("Janine Kropp <nin@directfb.org>, "
755 "Denis Oliver Kropp <dok@directfb.org>, "
756 "Daniel Mack <daniel@caiaq.de>");