summaryrefslogtreecommitdiff
path: root/firmware/mas.c
diff options
context:
space:
mode:
Diffstat (limited to 'firmware/mas.c')
-rw-r--r--firmware/mas.c164
1 files changed, 164 insertions, 0 deletions
diff --git a/firmware/mas.c b/firmware/mas.c
new file mode 100644
index 0000000000..decfff612b
--- /dev/null
+++ b/firmware/mas.c
@@ -0,0 +1,164 @@
1/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
9 *
10 * Copyright (C) 2002 by Linus Nielsen Feltzing
11 *
12 * All files in this archive are subject to the GNU General Public License.
13 * See the file COPYING in the source tree root for full license agreement.
14 *
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
17 *
18 ****************************************************************************/
19#include "i2c.h"
20#include "debug.h"
21#include "mas.h"
22
23/* note: 'len' is number of 32-bit words, not number of bytes! */
24int mas_readmem(int bank, int addr, unsigned long* dest, int len)
25{
26 int i;
27 unsigned char buf[16];
28
29 i=0;
30 buf[i++] = MAS_DATA_WRITE;
31 buf[i++] = bank?0xf0:0xe0;
32 buf[i++] = 0x00;
33 buf[i++] = (len & 0xff00) >> 8;
34 buf[i++] = len & 0xff;
35 buf[i++] = (addr & 0xff00) >> 8;
36 buf[i++] = addr & 0xff;
37
38 /* send read command */
39 if (i2c_write(MAS_DEV_WRITE,buf,i))
40 {
41 return -1;
42 }
43
44 return mas_devread(dest, len);
45}
46
47/* note: 'len' is number of 32-bit words, not number of bytes! */
48int mas_writemem(int bank, int addr, unsigned long* src, int len)
49{
50 int i, j;
51 unsigned char buf[60];
52 unsigned char* ptr = (unsigned char*)src;
53
54 i=0;
55 buf[i++] = MAS_DATA_WRITE;
56 buf[i++] = bank;
57 buf[i++] = 0x00;
58 buf[i++] = (len & 0xff00) >> 8;
59 buf[i++] = len & 0xff;
60 buf[i++] = (addr & 0xff00) >> 8;
61 buf[i++] = addr & 0xff;
62
63 j = 0;
64 while(len--) {
65 buf[i++] = ptr[j*4+1];
66 buf[i++] = ptr[j*4+0];
67 buf[i++] = 0;
68 buf[i++] = ptr[j*4+2];
69 j += 4;
70 }
71
72 /* send write command */
73 if (i2c_write(MAS_DEV_WRITE,buf,i))
74 {
75 return -1;
76 }
77
78 return 0;
79}
80
81int mas_readreg(int reg)
82{
83 int i;
84 unsigned char buf[16];
85
86 i=0;
87 buf[i++] = MAS_DATA_WRITE;
88 buf[i++] = 0xd0 | reg >> 4;
89 buf[i++] = (reg & 0x0f) << 4;
90
91 /* send read command */
92 if (i2c_write(MAS_DEV_WRITE,buf,i))
93 {
94 return -1;
95 }
96
97 if(mas_devread((unsigned long *)buf, 1))
98 {
99 return -2;
100 }
101
102 return buf[0] | buf[1] << 8 | buf[3] << 16;
103}
104
105int mas_writereg(int reg, unsigned short val)
106{
107 int i;
108 unsigned char buf[16];
109
110 i=0;
111 buf[i++] = MAS_DATA_WRITE;
112 buf[i++] = 0x90 | reg >> 4;
113 buf[i++] = ((reg & 0x0f) << 4) | (val & 0x0f);
114 buf[i++] = (val >> 12) & 0xff;
115 buf[i++] = (val >> 4) & 0xff;
116
117 /* send write command */
118 if (i2c_write(MAS_DEV_WRITE,buf,i))
119 {
120 return -1;
121 }
122 return 0;
123}
124
125/* note: 'len' is number of 32-bit words, not number of bytes! */
126int mas_devread(unsigned long *dest, int len)
127{
128 unsigned char* ptr = (unsigned char*)dest;
129 int ret = 0;
130 int i;
131
132 /* handle read-back */
133 i2c_start();
134 i2c_outb(MAS_DEV_WRITE);
135 if (i2c_getack()) {
136 i2c_outb(MAS_DATA_READ);
137 if (i2c_getack()) {
138 i2c_start();
139 i2c_outb(MAS_DEV_READ);
140 if (i2c_getack()) {
141 for (i=0;len;i++) {
142 len--;
143 ptr[i*4+1] = i2c_inb(0);
144 ptr[i*4+0] = i2c_inb(0);
145 ptr[i*4+3] = i2c_inb(0);
146 if(len)
147 ptr[i*4+2] = i2c_inb(0);
148 else
149 ptr[i*4+2] = i2c_inb(1); /* NAK the last byte */
150 }
151 }
152 else
153 ret = -3;
154 }
155 else
156 ret = -2;
157 }
158 else
159 ret = -1;
160
161 i2c_stop();
162
163 return ret;
164}