summaryrefslogtreecommitdiffstats
path: root/c/src/lib/libbsp/lm32/shared/milkymist_tmu/tmu.c
blob: 838b45e383a0f10ce3c282977742020fde5f522f (plain) (blame)
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
/*  tmu.c
 *
 *  Milkymist TMU driver for RTEMS
 *
 *  The license and distribution terms for this file may be
 *  found in the file LICENSE in this distribution or at
 *  http://www.rtems.org/license/LICENSE.
 *
 *  COPYRIGHT (c) 2010, 2011 Sebastien Bourdeauducq
 */

#define RTEMS_STATUS_CHECKS_USE_PRINTK

#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <sys/types.h>
#include <rtems.h>
#include <bsp.h>
#include <bsp/irq-generic.h>
#include <rtems/libio.h>
#include <rtems/status-checks.h>
#include "../include/system_conf.h"
#include "milkymist_tmu.h"

#define DEVICE_NAME "/dev/tmu"

static rtems_id done_sem;

static rtems_isr done_handler(rtems_vector_number n)
{
  rtems_semaphore_release(done_sem);
  lm32_interrupt_ack(1 << MM_IRQ_TMU);
}

rtems_device_driver tmu_initialize(
  rtems_device_major_number major,
  rtems_device_minor_number minor,
  void *arg
)
{
  rtems_status_code sc;
  rtems_isr_entry dummy;

  sc = rtems_io_register_name(DEVICE_NAME, major, 0);
  RTEMS_CHECK_SC(sc, "create TMU device");

  sc = rtems_semaphore_create(
    rtems_build_name('T', 'M', 'U', ' '),
    0,
    RTEMS_SIMPLE_BINARY_SEMAPHORE,
    0,
    &done_sem
  );
  RTEMS_CHECK_SC(sc, "create TMU done semaphore");

  rtems_interrupt_catch(done_handler, MM_IRQ_TMU, &dummy);
  bsp_interrupt_vector_enable(MM_IRQ_TMU);

  return RTEMS_SUCCESSFUL;
}

static void invalidate_l2(void)
{
  volatile char *flushbase = (char *)FMLBRG_FLUSH_BASE;
  int i, offset;

  offset = 0;
  for (i=0;i<FMLBRG_LINE_COUNT;i++) {
    flushbase[offset] = 0;
    offset += FMLBRG_LINE_LENGTH;
  }
}

static bool invalidate_after;

static void tmu_start(struct tmu_td *td)
{
  if (td->invalidate_before)
    invalidate_l2();

  MM_WRITE(MM_TMU_HMESHLAST, td->hmeshlast);
  MM_WRITE(MM_TMU_VMESHLAST, td->vmeshlast);
  MM_WRITE(MM_TMU_BRIGHTNESS, td->brightness);
  MM_WRITE(MM_TMU_CHROMAKEY, td->chromakey);

  MM_WRITE(MM_TMU_VERTICESADR, (unsigned int)td->vertices);
  MM_WRITE(MM_TMU_TEXFBUF, (unsigned int)td->texfbuf);
  MM_WRITE(MM_TMU_TEXHRES, td->texhres);
  MM_WRITE(MM_TMU_TEXVRES, td->texvres);
  MM_WRITE(MM_TMU_TEXHMASK, td->texhmask);
  MM_WRITE(MM_TMU_TEXVMASK, td->texvmask);

  MM_WRITE(MM_TMU_DSTFBUF, (unsigned int)td->dstfbuf);
  MM_WRITE(MM_TMU_DSTHRES, td->dsthres);
  MM_WRITE(MM_TMU_DSTVRES, td->dstvres);
  MM_WRITE(MM_TMU_DSTHOFFSET, td->dsthoffset);
  MM_WRITE(MM_TMU_DSTVOFFSET, td->dstvoffset);
  MM_WRITE(MM_TMU_DSTSQUAREW, td->dstsquarew);
  MM_WRITE(MM_TMU_DSTSQUAREH, td->dstsquareh);

  MM_WRITE(MM_TMU_ALPHA, td->alpha);

  MM_WRITE(MM_TMU_CTL, td->flags|TMU_CTL_START);

  invalidate_after = td->invalidate_after;
}

static rtems_status_code tmu_finalize(void)
{
  rtems_status_code sc;

  sc = rtems_semaphore_obtain(done_sem, RTEMS_WAIT, 100);
  if (sc != RTEMS_SUCCESSFUL)
    return sc;

  if (invalidate_after) {
    invalidate_l2();
    __asm__ volatile( /* Invalidate Level-1 data cache */
      "wcsr DCC, r0\n"
      "nop\n"
    );
  }

  return RTEMS_SUCCESSFUL;
}

rtems_device_driver tmu_control(
  rtems_device_major_number major,
  rtems_device_minor_number minor,
  void *arg
)
{
  rtems_libio_ioctl_args_t *args = arg;
  struct tmu_td *td = (struct tmu_td *)args->buffer;
  rtems_status_code sc;

  switch (args->command) {
    case TMU_EXECUTE:
      tmu_start(td);
      sc = tmu_finalize();
      break;
    case TMU_EXECUTE_NONBLOCK:
      tmu_start(td);
      sc = RTEMS_SUCCESSFUL;
      break;
    case TMU_EXECUTE_WAIT:
      sc = tmu_finalize();
      break;
    default:
      sc = RTEMS_UNSATISFIED;
      break;
  }

  if (sc == RTEMS_SUCCESSFUL)
    args->ioctl_return = 0;
  else
    args->ioctl_return = -1;

  return sc;
}