add the correct input file to makeindex call
[Rockbox.git] / firmware / drivers / mas.c
blob1630255348d935abfd05fb865640776a8b609cc5
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2002 by Linus Nielsen Feltzing
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.
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
18 ****************************************************************************/
19 #include "stdbool.h"
20 #include "config.h"
21 #include "sh7034.h"
22 #include "i2c.h"
23 #include "debug.h"
24 #include "mas.h"
25 #include "kernel.h"
26 #include "system.h"
28 extern bool old_recorder;
30 static int mas_devread(unsigned long *dest, int len);
32 int mas_default_read(unsigned short *buf)
34 unsigned char *dest = (unsigned char *)buf;
35 int ret = 0;
37 i2c_begin();
39 i2c_start();
40 i2c_outb(MAS_DEV_WRITE);
41 if (i2c_getack()) {
42 i2c_outb(MAS_DATA_READ);
43 if (i2c_getack()) {
44 i2c_start();
45 i2c_outb(MAS_DEV_READ);
46 if (i2c_getack()) {
47 dest[0] = i2c_inb(0);
48 dest[1] = i2c_inb(1);
50 else
51 ret = -3;
53 else
54 ret = -2;
56 else
57 ret = -1;
59 i2c_stop();
61 i2c_end();
62 return ret;
65 int mas_run(unsigned short address)
67 int ret = 0;
68 unsigned char buf[3];
70 i2c_begin();
72 buf[0] = MAS_DATA_WRITE;
73 buf[1] = address >> 8;
74 buf[2] = address & 0xff;
76 /* send run command */
77 if (i2c_write(MAS_DEV_WRITE,buf,3))
79 ret = -1;
82 i2c_end();
83 return ret;
86 /* note: 'len' is number of 32-bit words, not number of bytes! */
87 int mas_readmem(int bank, int addr, unsigned long* dest, int len)
89 int ret = 0;
90 unsigned char buf[7];
92 i2c_begin();
94 buf[0] = MAS_DATA_WRITE;
95 buf[1] = bank?MAS_CMD_READ_D1_MEM:MAS_CMD_READ_D0_MEM;
96 buf[2] = 0x00;
97 buf[3] = (len & 0xff00) >> 8;
98 buf[4] = len & 0xff;
99 buf[5] = (addr & 0xff00) >> 8;
100 buf[6] = addr & 0xff;
102 /* send read command */
103 if (i2c_write(MAS_DEV_WRITE,buf,7))
105 ret = -1;
108 ret = mas_devread(dest, len);
110 i2c_end();
111 return ret;
114 /* note: 'len' is number of 32-bit words, not number of bytes! */
115 int mas_writemem(int bank, int addr, const unsigned long* src, int len)
117 int ret = 0;
118 int i, j;
119 unsigned char buf[60];
120 const unsigned char* ptr = (const unsigned char*)src;
122 i2c_begin();
124 i=0;
125 buf[i++] = MAS_DATA_WRITE;
126 buf[i++] = bank?MAS_CMD_WRITE_D1_MEM:MAS_CMD_WRITE_D0_MEM;
127 buf[i++] = 0x00;
128 buf[i++] = (len & 0xff00) >> 8;
129 buf[i++] = len & 0xff;
130 buf[i++] = (addr & 0xff00) >> 8;
131 buf[i++] = addr & 0xff;
133 j = 0;
134 while(len--) {
135 #if (CONFIG_CODEC == MAS3587F) || (CONFIG_CODEC == MAS3539F)
136 buf[i++] = 0;
137 buf[i++] = ptr[j+1];
138 buf[i++] = ptr[j+2];
139 buf[i++] = ptr[j+3];
140 #else
141 buf[i++] = ptr[j+2];
142 buf[i++] = ptr[j+3];
143 buf[i++] = 0;
144 buf[i++] = ptr[j+1];
145 #endif
146 j += 4;
149 /* send write command */
150 if (i2c_write(MAS_DEV_WRITE,buf,i))
152 ret = -1;
155 i2c_end();
156 return ret;
159 int mas_readreg(int reg)
161 int ret = 0;
162 unsigned char buf[16];
163 unsigned long value;
165 i2c_begin();
167 buf[0] = MAS_DATA_WRITE;
168 buf[1] = MAS_CMD_READ_REG | (reg >> 4);
169 buf[2] = (reg & 0x0f) << 4;
171 /* send read command */
172 if (i2c_write(MAS_DEV_WRITE,buf,3))
174 ret = -1;
176 else
178 if(mas_devread(&value, 1))
180 ret = -2;
182 else
184 ret = value;
188 i2c_end();
189 return ret;
192 int mas_writereg(int reg, unsigned int val)
194 int ret = 0;
195 unsigned char buf[5];
197 i2c_begin();
199 buf[0] = MAS_DATA_WRITE;
200 buf[1] = MAS_CMD_WRITE_REG | (reg >> 4);
201 #if (CONFIG_CODEC == MAS3587F) || (CONFIG_CODEC == MAS3539F)
202 buf[2] = ((reg & 0x0f) << 4) | (val >> 16 & 0x0f);
203 buf[3] = (val >> 8) & 0xff;
204 buf[4] = val & 0xff;
205 #else
206 buf[2] = ((reg & 0x0f) << 4) | (val & 0x0f);
207 buf[3] = (val >> 12) & 0xff;
208 buf[4] = (val >> 4) & 0xff;
209 #endif
211 /* send write command */
212 if (i2c_write(MAS_DEV_WRITE,buf,5))
214 ret = -1;
217 i2c_end();
218 return ret;
221 /* note: 'len' is number of 32-bit words, not number of bytes! */
222 static int mas_devread(unsigned long *dest, int len)
224 int ret = 0;
225 unsigned char* ptr = (unsigned char*)dest;
226 int i;
228 /* handle read-back */
229 /* Remember, the MAS values are only 20 bits, so we set
230 the upper 12 bits to 0 */
231 i2c_start();
232 i2c_outb(MAS_DEV_WRITE);
233 if (i2c_getack()) {
234 i2c_outb(MAS_DATA_READ);
235 if (i2c_getack()) {
236 i2c_start();
237 i2c_outb(MAS_DEV_READ);
238 if (i2c_getack()) {
239 for (i=0;len;i++) {
240 len--;
241 #if (CONFIG_CODEC == MAS3587F) || (CONFIG_CODEC == MAS3539F)
242 i2c_inb(0); /* Dummy read */
243 ptr[i*4+0] = 0;
244 ptr[i*4+1] = i2c_inb(0) & 0x0f;
245 ptr[i*4+2] = i2c_inb(0);
246 if(len)
247 ptr[i*4+3] = i2c_inb(0);
248 else
249 ptr[i*4+3] = i2c_inb(1); /* NAK the last byte */
250 #else
251 ptr[i*4+2] = i2c_inb(0);
252 ptr[i*4+3] = i2c_inb(0);
253 ptr[i*4+0] = i2c_inb(0);
254 if(len)
255 ptr[i*4+1] = i2c_inb(0);
256 else
257 ptr[i*4+1] = i2c_inb(1); /* NAK the last byte */
258 #endif
261 else
262 ret = -3;
264 else
265 ret = -2;
267 else
268 ret = -1;
270 i2c_stop();
272 return ret;
275 void mas_reset(void)
277 or_b(0x01, &PAIORH);
279 #if CONFIG_CODEC == MAS3507D
280 /* PB5 is "MAS enable". make it GPIO output and high */
281 PBCR2 &= ~0x0c00;
282 or_b(0x20, &PBIORL);
283 or_b(0x20, &PBDRL);
285 and_b(~0x01, &PADRH);
286 sleep(HZ/100);
287 or_b(0x01, &PADRH);
288 sleep(HZ/5);
289 #elif (CONFIG_CODEC == MAS3587F) || (CONFIG_CODEC == MAS3539F)
290 if(old_recorder)
292 /* Older recorder models don't invert the POR signal */
293 or_b(0x01, &PADRH);
294 sleep(HZ/100);
295 and_b(~0x01, &PADRH);
296 sleep(HZ/5);
298 else
300 and_b(~0x01, &PADRH);
301 sleep(HZ/100);
302 or_b(0x01, &PADRH);
303 sleep(HZ/5);
305 #endif
308 #if (CONFIG_CODEC == MAS3587F) || (CONFIG_CODEC == MAS3539F)
309 int mas_direct_config_read(unsigned char reg)
311 int ret = 0;
312 unsigned char tmp[2];
314 i2c_begin();
316 i2c_start();
317 i2c_outb(MAS_DEV_WRITE);
318 if (i2c_getack()) {
319 i2c_outb(reg);
320 if (i2c_getack()) {
321 i2c_start();
322 i2c_outb(MAS_DEV_READ);
323 if (i2c_getack()) {
324 tmp[0] = i2c_inb(0);
325 tmp[1] = i2c_inb(1); /* NAK the last byte */
326 ret = (tmp[0] << 8) | tmp[1];
328 else
329 ret = -3;
331 else
332 ret = -2;
334 else
335 ret = -1;
337 i2c_stop();
339 i2c_end();
340 return ret;
343 int mas_direct_config_write(unsigned char reg, unsigned int val)
345 int ret = 0;
346 unsigned char buf[3];
348 i2c_begin();
350 buf[0] = reg;
351 buf[1] = (val >> 8) & 0xff;
352 buf[2] = val & 0xff;
354 /* send write command */
355 if (i2c_write(MAS_DEV_WRITE,buf,3))
357 ret = -1;
360 i2c_end();
361 return ret;
364 int mas_codec_writereg(int reg, unsigned int val)
366 int ret = 0;
367 unsigned char buf[5];
369 i2c_begin();
371 buf[0] = MAS_CODEC_WRITE;
372 buf[1] = (reg >> 8) & 0xff;
373 buf[2] = reg & 0xff;
374 buf[3] = (val >> 8) & 0xff;
375 buf[4] = val & 0xff;
377 /* send write command */
378 if (i2c_write(MAS_DEV_WRITE,buf,5))
380 ret = -1;
383 i2c_end();
384 return ret;
387 int mas_codec_readreg(int reg)
389 int ret = 0;
390 unsigned char buf[16];
391 unsigned char tmp[2];
393 i2c_begin();
395 buf[0] = MAS_CODEC_WRITE;
396 buf[1] = (reg >> 8) & 0xff;
397 buf[2] = reg & 0xff;
399 /* send read command */
400 if (i2c_write(MAS_DEV_WRITE,buf,3))
402 ret = -1;
404 else
406 i2c_start();
407 i2c_outb(MAS_DEV_WRITE);
408 if (i2c_getack()) {
409 i2c_outb(MAS_CODEC_READ);
410 if (i2c_getack()) {
411 i2c_start();
412 i2c_outb(MAS_DEV_READ);
413 if (i2c_getack()) {
414 tmp[0] = i2c_inb(0);
415 tmp[1] = i2c_inb(1); /* NAK the last byte */
416 ret = (tmp[0] << 8) | tmp[1];
418 else
419 ret = -4;
421 else
422 ret = -3;
424 else
425 ret = -2;
427 i2c_stop();
430 i2c_end();
431 return ret;
434 unsigned long mas_readver(void)
436 int ret = 0;
437 unsigned char buf[16];
438 unsigned long value;
440 i2c_begin();
442 buf[0] = MAS_DATA_WRITE;
443 buf[1] = MAS_CMD_READ_IC_VER;
444 buf[2] = 0;
446 /* send read command */
447 if (i2c_write(MAS_DEV_WRITE,buf,3))
449 ret = -1;
451 else
453 if(mas_devread(&value, 1))
455 ret = -2;
457 else
459 ret = value;
463 i2c_end();
464 return ret;
467 #endif