summaryrefslogtreecommitdiff
path: root/firmware/drivers/lcd.c
blob: fa072ac3f56ea801457cd8bbcb900cf75a006134 (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
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * Copyright (C) 2002 by Alan Korr, speedup by Jörg Hohensohn
 *
 * All files in this archive are subject to the GNU General Public License.
 * See the file COPYING in the source tree root for full license agreement.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 ****************************************************************************/

#include <stdbool.h>
#include "system.h"

#define LCDR (PBDR_ADDR+1)

#ifdef HAVE_LCD_CHARCELLS
#define LCD_DS  1 /* PB0 = 1 --- 0001 ---  LCD-DS */
#define LCD_CS  2 /* PB1 = 1 --- 0010 --- /LCD-CS */
#define LCD_SD  4 /* PB2 = 1 --- 0100 ---  LCD-SD */
#define LCD_SC  8 /* PB3 = 1 --- 1000 ---  LCD-SC */
#else
#define LCD_SD  1 /* PB0 = 1 --- 0001 */
#define LCD_SC  2 /* PB1 = 1 --- 0010 */
#define LCD_RS  4 /* PB2 = 1 --- 0100 */
#define LCD_CS  8 /* PB3 = 1 --- 1000 */
#define LCD_DS LCD_RS
#endif

/*
 * About /CS,DS,SC,SD
 * ------------------
 *
 * LCD on JBP and JBR uses a SPI protocol to receive orders (SDA and SCK lines)
 *
 * - /CS -> Chip Selection line :
 *            0 : LCD chipset is activated.
 * -  DS -> Data Selection line, latched at the rising edge
 *          of the 8th serial clock (*) :
 *            0 : instruction register,
 *            1 : data register; 
 * -  SC -> Serial Clock line (SDA).
 * -  SD -> Serial Data line (SCK), latched at the rising edge
 *          of each serial clock (*).  
 *
 *    _                                                          _
 * /CS \                                                        /
 *      \______________________________________________________/
 *    _____  ____  ____  ____  ____  ____  ____  ____  ____  _____ 
 *  SD     \/ D7 \/ D6 \/ D5 \/ D4 \/ D3 \/ D2 \/ D1 \/ D0 \/
 *    _____/\____/\____/\____/\____/\____/\____/\____/\____/\_____
 *
 *    _____     _     _     _     _     _     _     _     ________ 
 *  SC     \   * \   * \   * \   * \   * \   * \   * \   *
 *          \_/   \_/   \_/   \_/   \_/   \_/   \_/   \_/
 *    _  _________________________________________________________ 
 *  DS \/                                                  
 *    _/\_________________________________________________________
 *
 */

/*
 * The only way to do logical operations in an atomic way
 * on SH1 is using :
 *
 *   or.b/and.b/tst.b/xor.b #imm,@(r0,gbr)
 *
 * but GCC doesn't generate them at all so some assembly
 * codes are needed here.
 *
 * The Global Base Register gbr is expected to be zero
 * and r0 is the address of one register in the on-chip
 * peripheral module.
 *
 */ 

void lcd_write(bool command, int byte) __attribute__ ((section (".icode")));
void lcd_write(bool command, int byte)
{
    asm("and.b %0, @(r0,gbr)"
         :
         : /* %0 */ "I"(~(LCD_CS|LCD_DS|LCD_SD|LCD_SC)),
           /* %1 */ "z"(LCDR));

    if (command)
        asm ("shll8 %0\n"
             "0:      \n\t"
             "and.b %2,@(r0,gbr)\n\t"
             "shll  %0\n\t"  
             "bf    1f\n\t"
             "or.b  %3,@(r0,gbr)\n"
             "1:      \n\t"
             "or.b  %4,@(r0,gbr)\n"
             "add   #-1,%1\n\t"
             "cmp/pl %1\n\t"
             "bt    0b"
             :
             : /* %0 */ "r"(((unsigned)byte)<<16),
             /* %1 */ "r"(8),
             /* %2 */ "I"(~(LCD_SC|LCD_SD|LCD_DS)),
             /* %3 */ "I"(LCD_SD),
             /* %4 */ "I"(LCD_SC),
             /* %5 */ "z"(LCDR));
    else
        asm ("shll8  %0\n"
             "0:       \n\t"
             "and.b  %2, @(r0,gbr)\n\t"
             "shll   %0\n\t"  
             "bf     1f\n\t"
             "or.b   %3, @(r0,gbr)\n"
             "1:       \n\t"
             "or.b   %4, @(r0,gbr)\n"
             "and.b  %2, @(r0,gbr)\n\t"
             "shll   %0\n\t"  
             "bf     1f\n\t"
             "or.b   %3, @(r0,gbr)\n"
             "1:       \n\t"
             "or.b   %4, @(r0,gbr)\n"
             "and.b  %2, @(r0,gbr)\n\t"
             "shll   %0\n\t"  
             "bf     1f\n\t"
             "or.b   %3, @(r0,gbr)\n"
             "1:       \n\t"
             "or.b   %4, @(r0,gbr)\n"
             "and.b  %2, @(r0,gbr)\n\t"
             "shll   %0\n\t"  
             "bf     1f\n\t"
             "or.b   %3, @(r0,gbr)\n"
             "1:       \n\t"
             "or.b   %4, @(r0,gbr)\n"
             "and.b  %2, @(r0,gbr)\n\t"
             "shll   %0\n\t"  
             "bf     1f\n\t"
             "or.b   %3, @(r0,gbr)\n"
             "1:       \n\t"
             "or.b   %4, @(r0,gbr)\n"
             "and.b  %2, @(r0,gbr)\n\t"
             "shll   %0\n\t"  
             "bf     1f\n\t"
             "or.b   %3, @(r0,gbr)\n"
             "1:       \n\t"
             "or.b   %4, @(r0,gbr)\n"
             "and.b  %2, @(r0,gbr)\n\t"
             "shll   %0\n\t"  
             "bf     1f\n\t"
             "or.b   %3, @(r0,gbr)\n"
             "1:       \n\t"
             "or.b   %4, @(r0,gbr)\n"
             "and.b  %2, @(r0,gbr)\n\t"
             "shll   %0\n\t"  
             "bf     1f\n\t"
             "or.b   %3, @(r0,gbr)\n"
             "1:       \n\t"
             "or.b   %4, @(r0,gbr)\n"
             :
             : /* %0 */ "r"(((unsigned)byte)<<16),
             /* %1 */ "r"(8),
             /* %2 */ "I"(~(LCD_SC|LCD_SD)),
             /* %3 */ "I"(LCD_SD|LCD_DS),
             /* %4 */ "I"(LCD_SC|LCD_DS),
             /* %5 */ "z"(LCDR));

    asm("or.b %0, @(r0,gbr)"
         :
         : /* %0 */ "I"(LCD_CS|LCD_DS|LCD_SD|LCD_SC),
           /* %1 */ "z"(LCDR));
}


/* A high performance function to write data to the display, 
   one or multiple bytes.
   Ultimately, all calls to lcd_write(false, xxx) should be substituted by 
   this, it will be most efficient if the LCD buffer is tilted to have the
   X row as consecutive bytes, so we can write a whole row */
void lcd_write_data(unsigned char* p_bytes, int count) __attribute__ ((section (".icode")));

#ifdef HAVE_LCD_CHARCELLS
/* This version works for both Player and Recorder models */
void lcd_write_data(unsigned char* p_bytes, int count)
{
    do
    {
        unsigned int byte;
        unsigned int sda1;     /* precalculated SC=low,SD=1 */
        unsigned int clk0sda0; /* precalculated SC and SD low */
        unsigned int oldlevel;

        byte = *p_bytes++ << 24; /* fetch to MSB position */

        /* This code will fail if an interrupt changes the contents of PBDRL.
           If so, we must disable the interrupt here. */

        /* precalculate the values for later bit toggling, init data write */
        asm (
            "mov.b  @%2,%0\n" /* sda1 = PBDRL */
            "or     %4,%0\n"  /* sda1 |= LCD_DS | LCD_SD     DS and SD high, */
            "and    %3,%0\n"  /* sda1 &= ~(LCD_CS | LCD_SC)  CS and SC low   */
            "mov    %0,%1\n"  /* sda1 -> clk0sda0 */
            "and    %5,%1\n"  /* clk0sda0 &= ~LCD_SD  both low */
            "mov.b  %1,@%2\n" /* PBDRL = clk0sda0 */
            : // outputs
            /* %0 */ "=r"(sda1),
            /* %1 */ "=r"(clk0sda0)
            : // inputs
            /* %2 */ "r"(LCDR),
            /* %3 */ "r"(~(LCD_CS | LCD_SC)),
            /* %4 */ "r"(LCD_DS | LCD_SD),
            /* %5 */ "r"(~LCD_SD)
        );
        
        /* unrolled loop to serialize the byte */
        asm (
            "mov    %4,r0\n" /* we need &PBDRL in r0 for "or.b x,@(r0,gbr)" */
            
            "shll   %0\n" /* shift the MSB into carry */
            "bf     1f\n"
            "mov.b  %1,@%4\n" /* if it was a "1": set SD high, SC low still */
            "1:       \n"
            "or.b   %2, @(r0,gbr)\n" /* rise SC (independent of SD level) */
            "shll   %0\n" /* shift for next round, now for longer hold time */
            "mov.b  %3,@%4\n" /* SC and SD low again */

            "bf     1f\n"
            "mov.b  %1,@%4\n"
            "1:       \n"
            "or.b   %2, @(r0,gbr)\n"
            "shll   %0\n"
            "mov.b  %3,@%4\n"
            
            "bf     1f\n"
            "mov.b  %1,@%4\n"
            "1:       \n"
            "or.b   %2, @(r0,gbr)\n"
            "shll   %0\n"
            "mov.b  %3,@%4\n"
            
            "bf     1f\n"
            "mov.b  %1,@%4\n"
            "1:       \n"
            "or.b   %2, @(r0,gbr)\n"
            "shll   %0\n"
            "mov.b  %3,@%4\n"
            
            "bf     1f\n"
            "mov.b  %1,@%4\n"
            "1:       \n"
            "or.b   %2, @(r0,gbr)\n"
            "shll   %0\n"
            "mov.b  %3,@%4\n"
            
            "bf     1f\n"
            "mov.b  %1,@%4\n"
            "1:       \n"
            "or.b   %2, @(r0,gbr)\n"
            "shll   %0\n"
            "mov.b  %3,@%4\n"
            
            "bf     1f\n"
            "mov.b  %1,@%4\n"
            "1:       \n"
            "or.b   %2, @(r0,gbr)\n"
            "shll   %0\n"
            "mov.b  %3,@%4\n"
            
            "bf     1f\n"
            "mov.b  %1,@%4\n" /* set SD high, SC low still */
            "1:       \n"
            "or.b   %2, @(r0,gbr)\n" /* rise SC (independent of SD level) */

            "or.b   %5, @(r0,gbr)\n" /* restore port */
            :
            : /* %0 */ "r"(byte),
            /* %1 */ "r"(sda1),
            /* %2 */ "I"(LCD_SC),
            /* %3 */ "r"(clk0sda0),
            /* %4 */ "r"(LCDR),
            /* %5 */ "I"(LCD_CS|LCD_DS|LCD_SD|LCD_SC)
            : "r0"
        );

        /* This is the place to reenable the interrupts, if we have disabled
           them. See above. */

    } while (--count); /* tail loop is faster */
}

#else /* #ifdef HAVE_LCD_CHARCELLS */
/* A further optimized version, exploits that SD is on bit 0 for recorders */
void lcd_write_data(unsigned char* p_bytes, int count)
{
    do
    {
        unsigned byte;
        unsigned sda1;     /* precalculated SC=low,SD=1 */
        unsigned int oldlevel;

        /* take inverse data, so I can use the NEGC instruction below, it is
           the only carry add/sub which does not destroy a source register */
        byte = ~(*p_bytes++ << 24); /* fetch to MSB position */

        /* This code will fail if an interrupt changes the contents of PBDRL.
           If so, we must disable the interrupt here. */

        /* precalculate the values for later bit toggling, init data write */
        asm (
            "mov.b  @%1,r0\n" /* r0 = PBDRL */
            "or     %3,r0\n"  /* r0 |= LCD_DS | LCD_SD     DS and SD high, */
            "and    %2,r0\n"  /* r0 &= ~(LCD_CS | LCD_SC)  CS and SC low   */
            "mov.b  r0,@%1\n" /* PBDRL = r0 */
            "neg    r0,%0\n"  /* sda1 = 0-r0 */
            : /* outputs: */
            /* %0 */ "=r"(sda1)
            : /* inputs: */
            /* %1 */ "r"(LCDR),
            /* %2 */ "I"(~(LCD_CS | LCD_SC)),
            /* %3 */ "I"(LCD_DS | LCD_SD)
            : /* trashed */
            "r0"
        );

        /* unrolled loop to serialize the byte */
        asm (
            "shll   %0    \n" /* shift the MSB into carry */
            "negc   %1, r0\n" /* carry to SD, SC low */
            "mov.b  r0,@%3\n" /* set data to port */
            "or     %2, r0\n" /* rise SC (independent of SD level) */
            "mov.b  r0,@%3\n" /* set to port */

            "shll   %0    \n"
            "negc   %1, r0\n"
            "mov.b  r0,@%3\n"
            "or     %2, r0\n"
            "mov.b  r0,@%3\n"

            "shll   %0    \n"
            "negc   %1, r0\n"
            "mov.b  r0,@%3\n"
            "or     %2, r0\n"
            "mov.b  r0,@%3\n"

            "shll   %0    \n"
            "negc   %1, r0\n"
            "mov.b  r0,@%3\n"
            "or     %2, r0\n"
            "mov.b  r0,@%3\n"

            "shll   %0    \n"
            "negc   %1, r0\n"
            "mov.b  r0,@%3\n"
            "or     %2, r0\n"
            "mov.b  r0,@%3\n"

            "shll   %0    \n"
            "negc   %1, r0\n"
            "mov.b  r0,@%3\n"
            "or     %2, r0\n"
            "mov.b  r0,@%3\n"

            "shll   %0    \n"
            "negc   %1, r0\n"
            "mov.b  r0,@%3\n"
            "or     %2, r0\n"
            "mov.b  r0,@%3\n"

            "shll   %0    \n"
            "negc   %1, r0\n"
            "mov.b  r0,@%3\n"
            "or     %2, r0\n"
            "mov.b  r0,@%3\n"

            "or     %4, r0\n" /* restore port */
            "mov.b  r0,@%3\n"
            : /* outputs: */
            : /* inputs: */
            /* %0 */ "r"(byte),
            /* %1 */ "r"(sda1),
            /* %2 */ "I"(LCD_SC),
            /* %3 */ "r"(LCDR),
            /* %4 */ "I"(LCD_CS|LCD_DS|LCD_SD|LCD_SC)
            :  /* trashed: */
            "r0"
        );

        /* This is the place to reenable the interrupts, if we have disabled
           them. See above. */

    } while (--count); /* tail loop is faster */
}
#endif /* #ifdef HAVE_LCD_CHARCELLS */