summaryrefslogtreecommitdiff
path: root/firmware/drivers/mas.c
blob: 4d2c35be0f9f67f57f390416260f022d0265e75e (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
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * Copyright (C) 2002 by Linus Nielsen Feltzing
 *
 * 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 "config.h"
#include "sh7034.h"
#include "i2c.h"
#include "debug.h"
#include "mas.h"
#include "kernel.h"
#include "system.h"

extern bool old_recorder;

static int mas_devread(unsigned long *dest, int len);

int mas_default_read(unsigned short *buf)
{
    unsigned char *dest = (unsigned char *)buf;
    int ret = 0;

    i2c_begin();
    
    i2c_start();
    i2c_outb(MAS_DEV_WRITE);
    if (i2c_getack()) {
        i2c_outb(MAS_DATA_READ);
        if (i2c_getack()) {
            i2c_start();
            i2c_outb(MAS_DEV_READ);
            if (i2c_getack()) {
                    dest[0] = i2c_inb(0);
                    dest[1] = i2c_inb(1);
            }
            else
                ret = -3;
        }
        else
            ret = -2;
    }
    else
        ret = -1;
    
    i2c_stop();

    i2c_end();
    return ret;
}

int mas_run(unsigned short address)
{
    int ret = 0;
    unsigned char buf[3];

    i2c_begin();
    
    buf[0] = MAS_DATA_WRITE;
    buf[1] = address >> 8;
    buf[2] = address & 0xff;

    /* send run command */
    if (i2c_write(MAS_DEV_WRITE,buf,3))
    {
        ret = -1;
    }

    i2c_end();
    return ret;
}

/* note: 'len' is number of 32-bit words, not number of bytes! */
int mas_readmem(int bank, int addr, unsigned long* dest, int len)
{
    int ret = 0;
    unsigned char buf[7];

    i2c_begin();

    buf[0] = MAS_DATA_WRITE;
    buf[1] = bank?MAS_CMD_READ_D1_MEM:MAS_CMD_READ_D0_MEM;
    buf[2] = 0x00;
    buf[3] = (len & 0xff00) >> 8;
    buf[4] = len & 0xff;
    buf[5] = (addr & 0xff00) >> 8;
    buf[6] = addr & 0xff;

    /* send read command */
    if (i2c_write(MAS_DEV_WRITE,buf,7))
    {
        ret = -1;
    }

    ret = mas_devread(dest, len);

    i2c_end();
    return ret;
}

/* note: 'len' is number of 32-bit words, not number of bytes! */
int mas_writemem(int bank, int addr, unsigned long* src, int len)
{
    int ret = 0;
    int i, j;
    unsigned char buf[60];
    unsigned char* ptr = (unsigned char*)src;

    i2c_begin();

    i=0;
    buf[i++] = MAS_DATA_WRITE;
    buf[i++] = bank?MAS_CMD_WRITE_D1_MEM:MAS_CMD_WRITE_D0_MEM;
    buf[i++] = 0x00;
    buf[i++] = (len & 0xff00) >> 8;
    buf[i++] = len & 0xff;
    buf[i++] = (addr & 0xff00) >> 8;
    buf[i++] = addr & 0xff;

    j = 0;
    while(len--) {
#ifdef HAVE_MAS3587F
        buf[i++] = 0;
        buf[i++] = ptr[j+1];
        buf[i++] = ptr[j+2];
        buf[i++] = ptr[j+3];
#else
        buf[i++] = ptr[j+2];
        buf[i++] = ptr[j+3];
        buf[i++] = 0;
        buf[i++] = ptr[j+1];
#endif
        j += 4;
    }
    
    /* send write command */
    if (i2c_write(MAS_DEV_WRITE,buf,i))
    {
        ret = -1;
    }

    i2c_end();
    return ret;
}

int mas_readreg(int reg)
{
    int ret = 0;
    unsigned char buf[16];
    unsigned long value;

    i2c_begin();

    buf[0] = MAS_DATA_WRITE;
    buf[1] = MAS_CMD_READ_REG | (reg >> 4);
    buf[2] = (reg & 0x0f) << 4;

    /* send read command */
    if (i2c_write(MAS_DEV_WRITE,buf,3))
    {
        ret = -1;
    }
    else
    {
        if(mas_devread(&value, 1))
        {
            ret = -2;
        }
        else
        {
            ret = value;
        }
    }

    i2c_end();
    return ret;
}

int mas_writereg(int reg, unsigned int val)
{
    int ret = 0;
    unsigned char buf[5];

    i2c_begin();

    buf[0] = MAS_DATA_WRITE;
    buf[1] = MAS_CMD_WRITE_REG | (reg >> 4);
    buf[2] = ((reg & 0x0f) << 4) | (val & 0x0f);
    buf[3] = (val >> 12) & 0xff;
    buf[4] = (val >> 4) & 0xff;

    /* send write command */
    if (i2c_write(MAS_DEV_WRITE,buf,5))
    {
        ret = -1;
    }

    i2c_end();
    return ret;
}

/* note: 'len' is number of 32-bit words, not number of bytes! */
static int mas_devread(unsigned long *dest, int len)
{
    int ret = 0;
    unsigned char* ptr = (unsigned char*)dest;
    int i;
    
    /* handle read-back */
    /* Remember, the MAS values are only 20 bits, so we set
       the upper 12 bits to 0 */
    i2c_start();
    i2c_outb(MAS_DEV_WRITE);
    if (i2c_getack()) {
        i2c_outb(MAS_DATA_READ);
        if (i2c_getack()) {
            i2c_start();
            i2c_outb(MAS_DEV_READ);
            if (i2c_getack()) {
                for (i=0;len;i++) {
                    len--;
#ifdef HAVE_MAS3587F
                    i2c_inb(0); /* Dummy read */
                    ptr[i*4+0] = 0;
                    ptr[i*4+1] = i2c_inb(0) & 0x0f;
                    ptr[i*4+2] = i2c_inb(0);
                    if(len)
                        ptr[i*4+3] = i2c_inb(0);
                    else
                        ptr[i*4+3] = i2c_inb(1); /* NAK the last byte */
#else
                    ptr[i*4+2] = i2c_inb(0);
                    ptr[i*4+3] = i2c_inb(0);
                    ptr[i*4+0] = i2c_inb(0);
                    if(len)
                        ptr[i*4+1] = i2c_inb(0);
                    else
                        ptr[i*4+1] = i2c_inb(1); /* NAK the last byte */
#endif
                }
            }
            else
                ret = -3;
        }
        else
            ret = -2;
    }
    else
        ret = -1;
    
    i2c_stop();

    return ret;
}

#ifdef HAVE_MAS3587F
void mas_reset(void)
{
    __set_bit_constant(8-8, &PAIORH);
    
    if(old_recorder)
    {
        /* Older recorder models don't invert the POR signal */
        __set_bit_constant(8-8, &PADRH);
        sleep(HZ/100);
        __clear_bit_constant(8-8, &PADRH);
        sleep(HZ/5);
    }
    else
    {
        __clear_bit_constant(8-8, &PADRH);
        sleep(HZ/100);
        __set_bit_constant(8-8, &PADRH);
        sleep(HZ/5);
    }
}

int mas_direct_config_read(unsigned char reg)
{
    int ret = 0;
    unsigned char tmp[2];
    
    i2c_begin();

    i2c_start();
    i2c_outb(MAS_DEV_WRITE);
    if (i2c_getack()) {
        i2c_outb(reg);
        if (i2c_getack()) {
            i2c_start();
            i2c_outb(MAS_DEV_READ);
            if (i2c_getack()) {
                tmp[0] = i2c_inb(0);
                tmp[1] = i2c_inb(1); /* NAK the last byte */
                ret = (tmp[0] << 8) | tmp[1];
            }
            else
                ret = -3;
        }
        else
            ret = -2;
    }
    else
        ret = -1;
    
    i2c_stop();

    i2c_end();
    return ret;
}

int mas_direct_config_write(unsigned char reg, unsigned int val)
{
    int ret = 0;
    unsigned char buf[3];

    i2c_begin();

    buf[0] = reg;
    buf[1] = (val >> 8) & 0xff;
    buf[2] = val & 0xff;

    /* send write command */
    if (i2c_write(MAS_DEV_WRITE,buf,3))
    {
        ret = -1;
    }

    i2c_end();
    return ret;
}

int mas_codec_writereg(int reg, unsigned int val)
{
    int ret = 0;
    unsigned char buf[5];

    i2c_begin();

    buf[0] = MAS_CODEC_WRITE;
    buf[1] = (reg >> 8) & 0xff;
    buf[2] = reg & 0xff;
    buf[3] = (val >> 8) & 0xff;
    buf[4] = val & 0xff;

    /* send write command */
    if (i2c_write(MAS_DEV_WRITE,buf,5))
    {
        ret = -1;
    }
    
    i2c_end();
    return ret;
}

int mas_codec_readreg(int reg)
{
    int ret = 0;
    unsigned char buf[16];
    unsigned char tmp[2];

    i2c_begin();

    buf[0] = MAS_CODEC_WRITE;
    buf[1] = (reg >> 8) & 0xff;
    buf[2] = reg & 0xff;

    /* send read command */
    if (i2c_write(MAS_DEV_WRITE,buf,3))
    {
        ret = -1;
    }
    else
    {
        i2c_start();
        i2c_outb(MAS_DEV_WRITE);
        if (i2c_getack()) {
            i2c_outb(MAS_CODEC_READ);
            if (i2c_getack()) {
                i2c_start();
                i2c_outb(MAS_DEV_READ);
                if (i2c_getack()) {
                    tmp[0] = i2c_inb(0);
                    tmp[1] = i2c_inb(1); /* NAK the last byte */
                    ret = (tmp[0] << 8) | tmp[1];
                }
                else
                    ret = -4;
            }
            else
                ret = -3;
        }
        else
            ret = -2;

        i2c_stop();
    }
    
    i2c_end();
    return ret;
}

unsigned long mas_readver(void)
{
    int ret = 0;
    unsigned char buf[16];
    unsigned long value;

    i2c_begin();

    buf[0] = MAS_DATA_WRITE;
    buf[1] = MAS_CMD_READ_IC_VER;
    buf[2] = 0;

    /* send read command */
    if (i2c_write(MAS_DEV_WRITE,buf,3))
    {
        ret = -1;
    }
    else
    {
        if(mas_devread(&value, 1))
        {
            ret = -2;
        }
        else
        {
            ret = value;
        }
    }

    i2c_end();
    return ret;
}

#endif