summaryrefslogtreecommitdiff
path: root/Source/DirectFB/gfxdrivers/sh772x/kernel-module/sh7723.c
blob: ea64cf4f1ddbfa387d865550ad435279a5fb5aa5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
/*
 *	SH7723 Graphics Device
 *
 *	Copyright (C) 2006-2008  IGEL Co.,Ltd
 *
 *      Written by Janine Kropp <nin@directfb.org>,
 *                 Denis Oliver Kropp <dok@directfb.org>
 *                 
 *
 *
 *	This program is free software; you can redistribute it and/or
 *	modify it under the terms of the GNU General Public License v2
 *	as published by the Free Software Foundation.
 */

#include <linux/module.h>
#include <linux/version.h>

#include <linux/delay.h>
#include <linux/fs.h>
#include <linux/interrupt.h>
#include <linux/ioctl.h>
#include <linux/miscdevice.h>
#include <linux/mm.h>
#include <linux/sched.h>

#include <asm/io.h>
#include <asm/uaccess.h>
#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 27)
#include <asm/mach/irq.h>
#endif

#include <sh772x_gfx.h>


//#define SH7723GFX_DEBUG_2DG
//#define SH7723GFX_IRQ_POLLER


/**********************************************************************************************************************/

#ifndef SH7723_BEU_IRQ
#define SH7723_BEU_IRQ 53
#endif

#ifndef SH7723_TDG_IRQ
#define SH7723_TDG_IRQ 44
#endif

/**********************************************************************************************************************/

#define	ENGINE_REG_TOP   0xA4680000
#define SH7723_BEU_BASE  0xFE930000

#define M2DG_REG(x)      (*(volatile u32*)((x)+ENGINE_REG_TOP))
#define BEU_REG(x)       (*(volatile u32*)((x)+SH7723_BEU_BASE))

#define	M2DG_SCLR             M2DG_REG(0x000)
#define	M2DG_DLSAR            M2DG_REG(0x048)


#define M2DG_STATUS           M2DG_REG(0x004)
#define M2DG_STATUS_CLEAR     M2DG_REG(0x008)
#define M2DG_INT_ENABLE       M2DG_REG(0x00c)

#define M2DG_SCLR_START       0x00000001 
#define M2DG_SCLR_RESET       0x80000000

#define M2DG_INT_TRAP         0x0001 
#define M2DG_INT_INTERRUPT    0x0002
#define M2DG_INT_ERROR        0x0004
#define M2DG_INT_ANY          0x0007 

#define BEVTR                 BEU_REG(0x0018C)

/**********************************************************************************************************************/

#ifdef SH7723GFX_DEBUG_2DG
#define QPRINT(x...)     do {                                                        \
     char buf[128];                                                                  \
     struct timeval tv;                                                              \
     do_gettimeofday( &tv );                                                         \
     snprintf( buf, sizeof(buf), x );                                                \
     printk( KERN_DEBUG "%ld.%03ld.%03ld - %-17s: %s\n",                             \
             tv.tv_sec - base_time.tv_sec,                                           \
             tv.tv_usec / 1000, tv.tv_usec % 1000, __FUNCTION__, buf );              \
} while (0)
#else
#define QPRINT(x...)     do {} while (0)
#endif

#define QDUMP(msg)       QPRINT( "%-12s (%s, hw %5d-%5d, next %5d-%5d, %svalid, "    \
                                 "STATUS 0x%07x)", msg,                          \
                                 shared->hw_running ? "running" : "   idle",         \
                                 shared->hw_start,                                   \
                                 shared->hw_end,                                     \
                                 shared->next_start,                                 \
                                 shared->next_end,                                   \
                                 shared->next_valid ? "  " : "in",                   \
                                 M2DG_STATUS & M2DG_INT_ANY );

/**********************************************************************************************************************/

static DECLARE_WAIT_QUEUE_HEAD( wait_idle );
static DECLARE_WAIT_QUEUE_HEAD( wait_next );

static SH772xGfxSharedArea *shared;

static struct timeval       base_time;

#ifndef SHARED_AREA_PHYS
static struct page         *shared_page;
static unsigned int         shared_order;
#endif

#ifdef SH7723GFX_IRQ_POLLER
static int                  stop_poller;
#endif

/**********************************************************************************************************************/

static int
sh7723_reset( SH772xGfxSharedArea *shared )
{
     do_gettimeofday( &base_time );

     QPRINT( "Resetting hardware..." );

     M2DG_SCLR = M2DG_SCLR_RESET;
     udelay( 5 );
     M2DG_SCLR = 0;

     QPRINT( "Initializing shared area..." );

     memset( (void*) shared, 0, sizeof(SH772xGfxSharedArea) );

     shared->buffer_phys = virt_to_phys(&shared->buffer[0]);
     shared->magic       = SH7723GFX_SHARED_MAGIC;


     QPRINT( "Clearing interrupts..." );

     M2DG_STATUS_CLEAR = M2DG_INT_ANY;

     M2DG_INT_ENABLE   = M2DG_INT_ANY;

     QDUMP( "Ready" );

     return 0;
}

/* copied from board-ap325rxa.c */
#define PORT_PSCR        0xA405011E
#define PORT_PSDR        0xA405013E
#define FPGA_LCDREG      0xB4100180
#define FPGA_BKLREG      0xB4100212

static int
sh7723_power_display( void )
{
     msleep(100);

     /* ASD AP-320/325 LCD ON */
     ctrl_outw(0x0018, FPGA_LCDREG);

     /* backlight */
     ctrl_outw((ctrl_inw(PORT_PSCR) & ~0x00C0) | 0x40, PORT_PSCR);
     ctrl_outb(ctrl_inb(PORT_PSDR) & ~0x08, PORT_PSDR);
     ctrl_outw(0x100, FPGA_BKLREG);
     
     return 0;
}

static int
sh7723_wait_idle( SH772xGfxSharedArea *shared )
{
     int ret;

     QDUMP( "Waiting....." );

     /* Does not need to be atomic. There's a lock in user space,
      * but anyhow, this is just for statistics. */
     shared->num_wait_idle++;

     ret = wait_event_interruptible_timeout( wait_idle, !shared->hw_running, 42*HZ );
     if (!ret) {
          printk( KERN_ERR "%s: TIMEOUT! (%srunning, hw %d-%d, next %d-%d - %svalid, "
                           "STATUS 0x%08x)\n",
                  __FUNCTION__,
                  shared->hw_running ? "" : "not ",
                  shared->hw_start,
                  shared->hw_end,
                  shared->next_start,
                  shared->next_end,
                  shared->next_valid ? "" : "not ",
                  M2DG_STATUS & M2DG_INT_ANY );
     }

     QDUMP( "........done" );

     return (ret > 0) ? 0 : (ret < 0) ? ret : -ETIMEDOUT;
}

static int
sh7723_wait_next( SH772xGfxSharedArea *shared )
{
     int ret;

     QDUMP( "Waiting....." );

     /* Does not need to be atomic. There's a lock in user space,
      * but anyhow, this is just for statistics. */
     shared->num_wait_next++;

     ret = wait_event_interruptible_timeout( wait_next, !shared->hw_running ||
                                             shared->next_start == shared->next_end, 42*HZ );
     if (!ret) {
          printk( KERN_ERR "%s: TIMEOUT! (%srunning, hw %d-%d, next %d-%d - %svalid, "
                           "STATUS 0x%08x)\n",
                  __FUNCTION__,
                  shared->hw_running ? "" : "not ",
                  shared->hw_start,
                  shared->hw_end,
                  shared->next_start,
                  shared->next_end,
                  shared->next_valid ? "" : "not ",
                  M2DG_STATUS & M2DG_INT_ANY );
     }

     QDUMP( "........done" );

     return (ret > 0) ? 0 : (ret < 0) ? ret : -ETIMEDOUT;
}

/**********************************************************************************************************************/

static irqreturn_t
sh7723_beu_irq( int irq, void *ctx )
{
     BEVTR = 0;

     /* Nothing here so far. But Vsync could be added. */

     return IRQ_HANDLED;
}

static irqreturn_t
sh7723_tdg_irq( int irq, void *ctx )
{
     SH772xGfxSharedArea *shared = ctx;
     u32                  status = M2DG_STATUS & M2DG_INT_ANY;

     if (! (status & M2DG_INT_ANY)) {
#ifndef SH7723GFX_IRQ_POLLER
          printk( KERN_WARNING "%s: bogus interrupt, STATUS 0x%08x!\n", __FUNCTION__, status );
#endif
          return IRQ_NONE;
     }

//     if (status & ~0x100)
          QDUMP( "-Interrupt" );

     if (status & M2DG_INT_ERROR)
          printk( KERN_ERR "%s: error! STATUS 0x%08x!\n", __FUNCTION__, status );

     shared->num_interrupts++;

     /* Clear the interrupt. */
     M2DG_STATUS_CLEAR = status;

     if (status & (M2DG_INT_TRAP | M2DG_INT_ERROR)) {
          if (!shared->hw_running)
               printk( KERN_WARNING "%s: huh, hw running? STATUS 0x%08x!\n", __FUNCTION__, status );

          if (status & M2DG_INT_ERROR) {
               printk( KERN_ERR "%s: ERROR! (%srunning, hw %d-%d, next %d-%d - %svalid, "
                                "STATUS 0x%08x)\n",
                       __FUNCTION__,
                       shared->hw_running ? "" : "not ",
                       shared->hw_start,
                       shared->hw_end,
                       shared->next_start,
                       shared->next_end,
                       shared->next_valid ? "" : "not ",
                       status );

               M2DG_SCLR = M2DG_SCLR_RESET;
          }

          /* Next valid means user space is not in the process of extending the buffer. */
          if (shared->next_valid && shared->next_start != shared->next_end) {
               shared->hw_start = shared->next_start;
               shared->hw_end   = shared->next_end;

               shared->next_start = shared->next_end = (shared->hw_end + 1 + 3) & ~3;
               shared->next_valid = 0;

               shared->num_words += shared->hw_end - shared->hw_start;

               shared->num_starts++;

               QDUMP( " '-> Start!" );

               M2DG_DLSAR = shared->buffer_phys + shared->hw_start*4;
               M2DG_SCLR  = M2DG_SCLR_START;

               wake_up_all( &wait_next );
          }
          else {
               shared->num_idle++;

               QDUMP( " '-> Idle." );

//check if needed
//               BEM_PE_CACHE = 1;

               shared->hw_running = 0;

               wake_up_all( &wait_next );
               wake_up_all( &wait_idle );
          }

          shared->num_done++;
     }

     return IRQ_HANDLED;
}

#ifdef SH7723GFX_IRQ_POLLER
static int
sh7723_tdg_irq_poller( void *arg )
{
     daemonize( "%s", __FUNCTION__ );

     sigfillset( &current->blocked );

     while (!stop_poller) {
          set_current_state( TASK_UNINTERRUPTIBLE );
          schedule_timeout( 1 );

          sh7723_tdg_irq( SH7723_TDG_IRQ, (void*) arg );
     }

     stop_poller = 0;

     return 0;
}
#endif

/**********************************************************************************************************************/

static int
sh7723gfx_ioctl( struct inode  *inode,
                 struct file   *filp,
                 unsigned int   cmd,
                 unsigned long  arg )
{
     SH772xRegister reg;

     switch (cmd) {
          case SH772xGFX_IOCTL_RESET:
               return sh7723_reset( shared );

          case SH772xGFX_IOCTL_WAIT_IDLE:
               return sh7723_wait_idle( shared );

          case SH772xGFX_IOCTL_WAIT_NEXT:
               return sh7723_wait_next( shared );

          case SH772xGFX_IOCTL_SETREG32:
               if (copy_from_user( &reg, (void*)arg, sizeof(SH772xRegister) ))
                    return -EFAULT;

               /* BEU, LCDC, VOU, 2DG */
               if ((reg.address < 0xFE930000 || reg.address > 0xFEA102D0) &&
                   (reg.address < 0xA4680000 || reg.address > 0xA468FFFF))
                    return -EACCES;

               *(volatile __u32 *) reg.address = reg.value;

               return 0;

          case SH772xGFX_IOCTL_GETREG32:
               if (copy_from_user( &reg, (void*)arg, sizeof(SH772xRegister) ))
                    return -EFAULT;

               /* BEU, LCDC, VOU */
               if ((reg.address < 0xFE930000 || reg.address > 0xFEA102D0) &&
                   (reg.address < 0xA4680000 || reg.address > 0xA468FFFF))
                    return -EACCES;

               reg.value = *(volatile __u32 *) reg.address;

               if (copy_to_user( (void*)arg, &reg, sizeof(SH772xRegister) ))
                    return -EFAULT;

               return 0;
          
          case SH772xGFX_IOCTL_POWER_DISPLAY:
               return sh7723_power_display( );
     }

     return -ENOSYS;
}

static int
sh7723gfx_mmap( struct file           *file,
                struct vm_area_struct *vma )
{
     unsigned int size;

     /* Just allow mapping at offset 0. */
     if (vma->vm_pgoff)
          return -EINVAL;

     /* Check size of requested mapping. */
     size = vma->vm_end - vma->vm_start;
     if (size != PAGE_ALIGN(sizeof(SH772xGfxSharedArea)))
          return -EINVAL;

     /* Set reserved and I/O flag for the area. */
     vma->vm_flags |= VM_RESERVED | VM_IO;

     /* Select uncached access. */
     vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);

#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 9)
     return remap_pfn_range( vma, vma->vm_start,
                             virt_to_phys((void*)shared) >> PAGE_SHIFT,
                             size, vma->vm_page_prot );
#elif LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0)
     return remap_page_range( vma, vma->vm_start,
                              virt_to_phys((void*)shared),
                              size, vma->vm_page_prot );
#else
     return io_remap_page_range( vma->vm_start,
                                 virt_to_phys((void*)shared),
                                 size, vma->vm_page_prot );
#endif
}

/**********************************************************************************************************************/

static struct file_operations sh7723gfx_fops = {
     ioctl:    sh7723gfx_ioctl,
     mmap:     sh7723gfx_mmap
};

static struct miscdevice sh7723gfx_miscdev = {
     minor:    196,           // 7*7*2*2
     name:     "sh772x_gfx",
     fops:     &sh7723gfx_fops
};

/**********************************************************************************************************************/

int
sh7723_init( void )
{
#ifndef SHARED_AREA_PHYS
     int i;
#endif
     int ret;

     /* Register the SH7723 graphics device. */
     ret = misc_register( &sh7723gfx_miscdev );
     if (ret < 0) {
          printk( KERN_ERR "%s: misc_register() for minor %d failed! (error %d)\n",
                  __FUNCTION__, sh7723gfx_miscdev.minor, ret );
          return ret;
     }

     /* Allocate and initialize the shared area. */
#ifdef SHARED_AREA_PHYS
#if SHARED_AREA_SIZE < PAGE_ALIGN(sizeof(SH772xGfxSharedArea))
#error SHARED_AREA_SIZE < PAGE_ALIGN(sizeof(SH772xGfxSharedArea))!
#endif
     shared = ioremap( SHARED_AREA_PHYS, PAGE_ALIGN(sizeof(SH772xGfxSharedArea)) );
#else
     shared_order = get_order(sizeof(SH772xGfxSharedArea));
     shared_page  = alloc_pages( GFP_DMA | GFP_KERNEL, shared_order );
     shared       = ioremap( virt_to_phys( page_address(shared_page) ),
                             PAGE_ALIGN(sizeof(SH772xGfxSharedArea)) );

     for (i=0; i<1<<shared_order; i++)
          SetPageReserved( shared_page + i );
#endif

     printk( KERN_INFO "sh7723gfx: shared area (order %d) at %p [%lx] using %d bytes\n",
             shared_order, shared, virt_to_phys(shared), sizeof(SH772xGfxSharedArea) );

     /* Register the BEU interrupt handler. */
     ret = request_irq( SH7723_BEU_IRQ, sh7723_beu_irq, IRQF_DISABLED, "BEU", (void*) shared );
     if (ret) {
          printk( KERN_ERR "%s: request_irq() for BEU interrupt %d failed! (error %d)\n",
                  __FUNCTION__, SH7723_BEU_IRQ, ret );
          goto error_beu;
     }

#ifdef SH7723GFX_IRQ_POLLER
     kernel_thread( sh7723_tdg_irq_poller, (void*) shared, CLONE_KERNEL );
#else
     /* Register the TDG interrupt handler. */
     ret = request_irq( SH7723_TDG_IRQ, sh7723_tdg_irq, IRQF_DISABLED, "TDG", (void*) shared );
     if (ret) {
          printk( KERN_ERR "%s: request_irq() for TDG interrupt %d failed! (error %d)\n",
                  __FUNCTION__, SH7723_TDG_IRQ, ret );
          goto error_tdg;
     }
#endif

     sh7723_reset( shared );

     return 0;


error_tdg:
     free_irq( SH7723_BEU_IRQ, (void*) shared );

error_beu:
#ifndef SHARED_AREA_PHYS
     for (i=0; i<1<<shared_order; i++)
          ClearPageReserved( shared_page + i );

     __free_pages( shared_page, shared_order );
#endif

     misc_deregister( &sh7723gfx_miscdev );

     return ret;
}

/**********************************************************************************************************************/

void
sh7723_exit( void )
{
#ifndef SHARED_AREA_PHYS
     int i;
#endif


#ifdef SH7723GFX_IRQ_POLLER
     stop_poller = 1;

     while (stop_poller) {
          set_current_state( TASK_UNINTERRUPTIBLE );
          schedule_timeout( 1 );
     }
#else
     free_irq( SH7723_TDG_IRQ, (void*) shared );
#endif

     free_irq( SH7723_BEU_IRQ, (void*) shared );

     misc_deregister( &sh7723gfx_miscdev );


#ifndef SHARED_AREA_PHYS
     for (i=0; i<1<<shared_order; i++)
          ClearPageReserved( shared_page + i );

     __free_pages( shared_page, shared_order );
#endif
}