1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 * Copyright (C) 2006 by Linus Nielsen Feltzing
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
29 /* Define the approprate bits for SDA and SCL being the only difference in
30 config between each player. */
31 #if defined(IRIVER_H300_SERIES)
32 #define SDA_BITNUM 13 /* LRCK3/GPIO45 */
33 #define SCL_BITNUM 12 /* SWE/GPIO12 */
34 #elif defined(IAUDIO_X5) || defined(IAUDIO_M5)
35 #define SDA_BITNUM 12 /* SDA1/RXD1/GPIO44 */
36 #define SCL_BITNUM 10 /* SCL1/TXD1/GPIO10 */
40 #define SDA_GPIO_READ GPIO1_READ /* MBAR2 + 0x0b0 */
41 #define SDA_GPIO_OUT GPIO1_OUT /* MBAR2 + 0x0b4 */
42 #define SDA_GPIO_ENABLE GPIO1_ENABLE /* MBAR2 + 0x0b8 */
43 #define SDA_GPIO_FUNCTION GPIO1_FUNCTION /* MBAR2 + 0x0bc */
46 #define SCL_GPIO_READ GPIO_READ /* MBAR2 + 0x000 */
47 #define SCL_GPIO_OUT GPIO_OUT /* MBAR2 + 0x004 */
48 #define SCL_GPIO_ENABLE GPIO_ENABLE /* MBAR2 + 0x008 */
49 #define SCL_GPIO_FUNCTION GPIO_FUNCTION /* MBAR2 + 0x00c */
51 #define PCF50606_ADDR 0x10
52 #define SCL_BIT (1ul << SCL_BITNUM)
53 #define SDA_BIT (1ul << SDA_BITNUM)
55 #define SDA ( SDA_BIT & SDA_GPIO_READ)
56 #define SDA_LO_OUT or_l( SDA_BIT, &SDA_GPIO_ENABLE)
57 #define SDA_HI_IN and_l(~SDA_BIT, &SDA_GPIO_ENABLE)
59 #define SCL ( SCL_BIT & SCL_GPIO_READ)
60 #define SCL_LO_OUT or_l( SCL_BIT, &SCL_GPIO_ENABLE)
61 #define SCL_HI_IN and_l(~SCL_BIT, &SCL_GPIO_ENABLE); while(!SCL);
65 "move.l %[dly],%%d0 \n" \
69 : : [dly]"d"(i2c_delay) : "d0" );
71 void pcf50606_i2c_init(void)
74 or_l(SDA_BIT
, &SDA_GPIO_FUNCTION
);
75 or_l(SCL_BIT
, &SCL_GPIO_FUNCTION
);
76 and_l(~SDA_BIT
, &SDA_GPIO_OUT
);
77 and_l(~SCL_BIT
, &SCL_GPIO_OUT
);
78 and_l(~SDA_BIT
, &SDA_GPIO_ENABLE
);
79 and_l(~SCL_BIT
, &SCL_GPIO_ENABLE
);
82 static int i2c_delay IDATA_ATTR
= 44;
84 void pcf50606_i2c_recalc_delay(int cpu_clock
)
86 i2c_delay
= MAX(cpu_clock
/ (400000*2*3) - 7, 1);
89 inline void pcf50606_i2c_start(void)
93 "not.l %[sdab] \n" /* SDA_HI_IN */
94 "and.l %[sdab],(8,%[sdard]) \n"
97 "not.l %[sclb] \n" /* SCL_HI_IN */
98 "and.l %[sclb],(8,%[sclrd]) \n"
101 "move.l (%[sclrd]),%%d0 \n"
102 "btst.l %[sclbnum], %%d0 \n"
105 "move.l %[dly],%%d0 \n" /* DELAY */
110 "or.l %[sdab],(8,%[sdard]) \n" /* SDA_LO_OUT */
112 "move.l %[dly],%%d0 \n" /* DELAY */
117 "or.l %[sclb],(8,%[sclrd]) \n" /* SCL_LO_OUT */
120 [sclrd
] "a"(&SCL_GPIO_READ
),
122 [sclbnum
] "i"(SCL_BITNUM
),
123 [sdard
] "a"(&SDA_GPIO_READ
),
139 inline void pcf50606_i2c_stop(void)
143 "or.l %[sdab],(8,%[sdard]) \n" /* SDA_LO_OUT */
145 "move.l %[dly],%%d0 \n" /* DELAY */
150 "not.l %[sclb] \n" /* SCL_HI_IN */
151 "and.l %[sclb],(8,%[sclrd]) \n"
154 "move.l (%[sclrd]),%%d0 \n"
155 "btst.l %[sclbnum],%%d0 \n"
158 "move.l %[dly],%%d0 \n" /* DELAY */
163 "not.l %[sdab] \n" /* SDA_HI_IN */
164 "and.l %[sdab],(8,%[sdard]) \n"
168 [sclrd
] "a"(&SCL_GPIO_READ
),
170 [sclbnum
] "i"(SCL_BITNUM
),
171 [sdard
] "a"(&SDA_GPIO_READ
),
186 inline void pcf50606_i2c_ack(bool ack
)
190 "tst.b %[ack] \n" /* if (!ack) */
193 "not.l %[sdab] \n" /* SDA_HI_IN */
194 "and.l %[sdab],(8,%[sdard]) \n"
196 ".word 0x51fb \n" /* trapf.l : else */
198 "or.l %[sdab],(8,%[sdard]) \n" /* SDA_LO_OUT */
200 "move.l %[dly],%%d0 \n" /* DELAY */
205 "not.l %[sclb] \n" /* SCL_HI_IN */
206 "and.l %[sclb],(8,%[sclrd]) \n"
209 "move.l (%[sclrd]),%%d0 \n"
210 "btst.l %[sclbnum],%%d0 \n"
213 "move.l %[dly],%%d0 \n" /* DELAY */
218 "or.l %[sclb],(8,%[sclrd]) \n" /* SCL_LO_OUT */
221 [sclrd
] "a"(&SCL_GPIO_READ
),
223 [sclbnum
] "i"(SCL_BITNUM
),
224 [sdard
] "a"(&SDA_GPIO_READ
),
226 [dly
] "d"(i2c_delay
),
243 inline bool pcf50606_i2c_getack(void)
249 "not.l %[sdab] \n" /* SDA_HI_IN */
250 "and.l %[sdab],(8,%[sdard]) \n"
253 "move.l %[dly],%%d0 \n" /* DELAY */
258 "not.l %[sclb] \n" /* SCL_HI_IN */
259 "and.l %[sclb],(8,%[sclrd]) \n"
262 "move.l (%[sclrd]),%%d0 \n"
263 "btst.l %[sclbnum],%%d0 \n"
266 "move.l %[dly],%%d0 \n" /* DELAY */
271 "move.l (%[sdard]),%%d0 \n" /* ret = !SDA */
272 "btst.l %[sdabnum],%%d0 \n"
275 "or.l %[sclb],(8,%[sclrd]) \n" /* SCL_LO_OUT */
279 [sclrd
] "a"(&SCL_GPIO_READ
),
281 [sclbnum
] "i"(SCL_BITNUM
),
282 [sdard
] "a"(&SDA_GPIO_READ
),
284 [sdabnum
] "i"(SDA_BITNUM
),
302 void pcf50606_i2c_outb(unsigned char byte
)
306 "moveq.l #24,%%d0 \n" /* byte <<= 24 */
307 "lsl.l %%d0,%[byte] \n"
308 "moveq.l #8,%%d1 \n" /* i = 8 */
311 "lsl.l #1,%[byte] \n" /* if ((byte <<= 1) carry) */
314 "not.l %[sdab] \n" /* SDA_HI_IN */
315 "and.l %[sdab],(8,%[sdard]) \n"
317 ".word 0x51fb \n" /* trapf.l; else */
319 "or.l %[sdab],(8,%[sdard]) \n" /* SDA_LO_OUT */
321 "move.l %[dly],%%d0 \n" /* DELAY */
326 "not.l %[sclb] \n" /* SCL_HI_IN */
327 "and.l %[sclb],(8,%[sclrd]) \n"
330 "move.l (%[sclrd]),%%d0 \n"
331 "btst.l %[sclbnum],%%d0 \n"
334 "move.l %[dly],%%d0 \n" /* DELAY */
339 "or.l %[sclb],(8,%[sclrd]) \n" /* SCL_LO_OUT */
341 "subq.l #1,%%d1 \n" /* i-- */
342 "bne.s 2b \n" /* while (i != 0) */
346 [sclrd
] "a"(&SCL_GPIO_READ
),
348 [sclbnum
] "i"(SCL_BITNUM
),
349 [sdard
] "a"(&SDA_GPIO_READ
),
358 /* clock out each bit, MSB first */
359 for ( i
=0x80; i
; i
>>=1 )
373 unsigned char pcf50606_i2c_inb(bool ack
)
375 unsigned char byte
= 0;
379 "not.l %[sdab] \n" /* SDA_HI_IN */
380 "and.l %[sdab],(8,%[sdard]) \n"
383 "moveq.l #8,%%d1 \n" /* i = 8 */
384 "clr.l %[byte] \n" /* byte = 0 */
387 "move.l %[dly],%%d0 \n" /* DELAY */
392 "not.l %[sclb] \n" /* SCL_HI_IN */
393 "and.l %[sclb],(8,%[sclrd]) \n"
396 "move.l (%[sclrd]),%%d0 \n"
397 "btst.l %[sclbnum],%%d0 \n"
400 "move.l %[dly],%%d0 \n" /* DELAY */
405 "lsl.l #1,%[byte] \n" /* byte <<= 1 */
406 "move.l (%[sdard]),%%d0 \n" /* if (SDA) */
407 "btst.l %[sdabnum],%%d0 \n"
409 "addq.l #1,%[byte] \n" /* byte++ */
412 "or.l %[sclb],(8,%[sclrd]) \n" /* SCL_LO_OUT */
414 "subq.l #1,%%d1 \n" /* i-- */
415 "bne.s 2b \n" /* while (i != 0) */
419 [sclrd
] "a"(&SCL_GPIO_READ
),
421 [sclbnum
] "i"(SCL_BITNUM
),
422 [sdard
] "a"(&SDA_GPIO_READ
),
424 [sdabnum
] "i"(SDA_BITNUM
),
432 /* clock in each bit, MSB first */
434 for ( i
=0x80; i
; i
>>=1 )
445 pcf50606_i2c_ack(ack
);
450 static int pcf50606_i2c_write(int address
, const unsigned char* buf
, int count
)
454 pcf50606_i2c_start();
455 pcf50606_i2c_outb(address
& 0xfe);
456 if (pcf50606_i2c_getack())
458 for (i
=0; i
<count
; i
++)
460 pcf50606_i2c_outb(buf
[i
]);
461 if (!pcf50606_i2c_getack())
470 logf("pcf50606_i2c_write() - no ack\n");
476 int pcf50606_read_multiple(int address
, unsigned char* buf
, int count
)
480 unsigned char obuf
[1];
484 /* send read command */
485 if (pcf50606_i2c_write(PCF50606_ADDR
, obuf
, 1) >= 0)
487 pcf50606_i2c_start();
488 pcf50606_i2c_outb(0x11);
489 if (pcf50606_i2c_getack())
491 for(i
= 0;i
< count
-1;i
++)
492 buf
[i
] = pcf50606_i2c_inb(true);
494 buf
[i
] = pcf50606_i2c_inb(false);
507 int pcf50606_read(int address
)
512 ret
= pcf50606_read_multiple(address
, &c
, 1);
519 int pcf50606_write_multiple(int address
, const unsigned char* buf
, int count
)
521 unsigned char obuf
[1];
527 /* send write command */
528 if (pcf50606_i2c_write(PCF50606_ADDR
, obuf
, 1) >= 0)
530 for (i
=0; i
<count
; i
++)
532 pcf50606_i2c_outb(buf
[i
]);
533 if (!pcf50606_i2c_getack())
549 int pcf50606_write(int address
, unsigned char val
)
551 return pcf50606_write_multiple(address
, &val
, 1);