summaryrefslogtreecommitdiff
path: root/firmware/drivers/mas.c
blob: decfff612b1b5afae6bf2662fb38ab32cdb3b916 (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
/***************************************************************************
 *             __________               __   ___.
 *   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 "i2c.h"
#include "debug.h"
#include "mas.h"

/* 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 i;
    unsigned char buf[16];

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

    /* send read command */
    if (i2c_write(MAS_DEV_WRITE,buf,i))
    {
        return -1;
    }

    return mas_devread(dest, len);
}

/* 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 i, j;
    unsigned char buf[60];
    unsigned char* ptr = (unsigned char*)src;

    i=0;
    buf[i++] = MAS_DATA_WRITE;
    buf[i++] = bank;
    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--) {
        buf[i++] = ptr[j*4+1];
        buf[i++] = ptr[j*4+0];
        buf[i++] = 0;
        buf[i++] = ptr[j*4+2];
        j += 4;
    }
    
    /* send write command */
    if (i2c_write(MAS_DEV_WRITE,buf,i))
    {
        return -1;
    }

    return 0;
}

int mas_readreg(int reg)
{
    int i;
    unsigned char buf[16];

    i=0;
    buf[i++] = MAS_DATA_WRITE;
    buf[i++] = 0xd0 | reg >> 4;
    buf[i++] = (reg & 0x0f) << 4;

    /* send read command */
    if (i2c_write(MAS_DEV_WRITE,buf,i))
    {
        return -1;
    }

    if(mas_devread((unsigned long *)buf, 1))
    {
        return -2;
    }

    return buf[0] | buf[1] << 8 | buf[3] << 16;
}

int mas_writereg(int reg, unsigned short val)
{
    int i;
    unsigned char buf[16];

    i=0;
    buf[i++] = MAS_DATA_WRITE;
    buf[i++] = 0x90 | reg >> 4;
    buf[i++] = ((reg & 0x0f) << 4) | (val & 0x0f);
    buf[i++] = (val >> 12) & 0xff;
    buf[i++] = (val >> 4) & 0xff;

    /* send write command */
    if (i2c_write(MAS_DEV_WRITE,buf,i))
    {
        return -1;
    }
    return 0;
}

/* note: 'len' is number of 32-bit words, not number of bytes! */
int mas_devread(unsigned long *dest, int len)
{
    unsigned char* ptr = (unsigned char*)dest;
    int ret = 0;
    int i;
    
    /* handle read-back */
    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--;
                    ptr[i*4+1] = i2c_inb(0);
                    ptr[i*4+0] = i2c_inb(0);
                    ptr[i*4+3] = i2c_inb(0);
                    if(len)
                        ptr[i*4+2] = i2c_inb(0);
                    else
                        ptr[i*4+2] = i2c_inb(1); /* NAK the last byte */
                }
            }
            else
                ret = -3;
        }
        else
            ret = -2;
    }
    else
        ret = -1;
    
    i2c_stop();

    return ret;
}