1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 * Copyright (C) 2006 by Miika Pekkarinen
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 ****************************************************************************/
29 #include "eeprom_24cxx.h"
32 * I2C-functions are copied and ported from fmradio.c.
35 #define SW_I2C_WRITE 0
38 /* h1x0 needs its own i2c driver,
39 h3x0 uses the pcf i2c driver */
41 #ifdef IRIVER_H100_SERIES
43 /* cute little functions, atomic read-modify-write */
46 #define SCL ( 0x00001000 & GPIO_READ)
47 #define SCL_OUT_LO and_l(~0x00001000, &GPIO_OUT)
48 #define SCL_LO or_l( 0x00001000, &GPIO_ENABLE)
49 #define SCL_HI and_l(~0x00001000, &GPIO_ENABLE)
51 /* SDA is GPIO1, 13 */
52 #define SDA ( 0x00002000 & GPIO1_READ)
53 #define SDA_OUT_LO and_l(~0x00002000, &GPIO1_OUT)
54 #define SDA_LO or_l( 0x00002000, &GPIO1_ENABLE)
55 #define SDA_HI and_l(~0x00002000, &GPIO1_ENABLE)
57 /* delay loop to achieve 400kHz at 120MHz CPU frequency */
58 #define DELAY do { int _x; for(_x=0;_x<22;_x++);} while(0)
60 static void sw_i2c_init(void)
63 or_l(0x00001000, &GPIO_FUNCTION
);
64 or_l(0x00002000, &GPIO1_FUNCTION
);
71 static void sw_i2c_start(void)
84 static void sw_i2c_stop(void)
92 static void sw_i2c_ack(void)
103 static bool sw_i2c_getack(void)
110 SDA_HI
; /* sets to input */
115 while (SDA
&& count
--)
129 static void sw_i2c_outb(unsigned char byte
)
133 /* clock out each bit, MSB first */
134 for ( i
=0x80; i
; i
>>=1 )
148 static unsigned char sw_i2c_inb(void)
151 unsigned char byte
= 0;
153 SDA_HI
; /* sets to input */
155 /* clock in each bit, MSB first */
156 for ( i
=0x80; i
; i
>>=1 )
173 #include "pcf50606.h"
175 #define sw_i2c_init() /* no extra init required */
176 #define sw_i2c_start() pcf50606_i2c_start()
177 #define sw_i2c_stop() pcf50606_i2c_stop()
178 #define sw_i2c_ack() pcf50606_i2c_ack(true)
179 #define sw_i2c_getack() pcf50606_i2c_getack()
180 #define sw_i2c_outb(x) pcf50606_i2c_outb(x)
181 #define sw_i2c_inb() pcf50606_i2c_inb(false)
183 #endif /* IRIVER_H100_SERIES */
186 int sw_i2c_write(int location
, const unsigned char* buf
, int count
)
191 sw_i2c_outb((EEPROM_ADDR
& 0xfe) | SW_I2C_WRITE
);
192 if (!sw_i2c_getack())
198 sw_i2c_outb(location
);
199 if (!sw_i2c_getack())
205 for (i
=0; i
<count
; i
++)
208 if (!sw_i2c_getack())
220 int sw_i2c_write_byte(int location
, unsigned char byte
)
223 sw_i2c_outb((EEPROM_ADDR
& 0xfe) | SW_I2C_WRITE
);
224 if (!sw_i2c_getack())
230 sw_i2c_outb(location
);
231 if (!sw_i2c_getack())
238 if (!sw_i2c_getack())
249 int sw_i2c_read(unsigned char location
, unsigned char* byte
)
252 sw_i2c_outb((EEPROM_ADDR
& 0xfe) | SW_I2C_WRITE
);
253 if (!sw_i2c_getack())
259 sw_i2c_outb(location
);
260 if (!sw_i2c_getack())
267 sw_i2c_outb((EEPROM_ADDR
& 0xfe) | SW_I2C_READ
);
268 if (!sw_i2c_getack())
274 *byte
= sw_i2c_inb();
280 void eeprom_24cxx_init(void)
285 int eeprom_24cxx_read_byte(unsigned int address
, char *c
)
291 if (address
>= EEPROM_SIZE
)
293 logf("EEPROM address: %d", address
);
300 ret
= sw_i2c_read(address
, &byte
);
301 } while (ret
< 0 && count
++ < 200);
305 logf("EEPROM RFail: %d/%d/%d", ret
, address
, count
);
311 /* keep between {} as logf is whitespace in normal builds */
312 logf("EEPROM rOK: %d retries", count
);
319 int eeprom_24cxx_write_byte(unsigned int address
, char c
)
324 if (address
>= EEPROM_SIZE
)
326 logf("EEPROM address: %d", address
);
332 ret
= sw_i2c_write_byte(address
, c
);
333 } while (ret
< 0 && count
++ < 200) ;
337 logf("EEPROM WFail: %d/%d", ret
, address
);
343 /* keep between {} as logf is whitespace in normal builds */
344 logf("EEPROM wOK: %d retries", count
);
350 int eeprom_24cxx_read(unsigned char address
, void *dest
, int length
)
352 char *buf
= (char *)dest
;
356 for (i
= 0; i
< length
; i
++)
358 ret
= eeprom_24cxx_read_byte(address
+i
, &buf
[i
]);
366 int eeprom_24cxx_write(unsigned char address
, const void *src
, int length
)
368 const char *buf
= (const char *)src
;
375 for (i
= 0; i
< length
; i
++)
376 eeprom_24cxx_write_byte(address
+i
, buf
[i
]);
379 for (i
= 0; i
< length
; i
++)
383 eeprom_24cxx_read_byte(address
+i
, &byte
);
386 logf("Verify failed: %d/%d", address
+i
, count
);