1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 * Copyright (C) 2002 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 /* cute little functions, atomic read-modify-write */
32 #define SDA_LO and_b(~0x80, &PBDRL)
33 #define SDA_HI or_b(0x80, &PBDRL)
34 #define SDA_INPUT and_b(~0x80, &PBIORL)
35 #define SDA_OUTPUT or_b(0x80, &PBIORL)
36 #define SDA (PBDRL & 0x80)
38 #if CONFIG_I2C == I2C_ONDIO
39 /* Ondio pinout, SCL moved to PB6 */
40 #define SCL_INPUT and_b(~0x40, &PBIORL)
41 #define SCL_OUTPUT or_b(0x40, &PBIORL)
42 #define SCL_LO and_b(~0x40, &PBDRL)
43 #define SCL_HI or_b(0x40, &PBDRL)
44 #define SCL (PBDRL & 0x40)
46 /* "classic" pinout, SCL is PB13 */
47 #define SCL_INPUT and_b(~0x20, &PBIORH)
48 #define SCL_OUTPUT or_b(0x20, &PBIORH)
49 #define SCL_LO and_b(~0x20, &PBDRH)
50 #define SCL_HI or_b(0x20, &PBDRH)
51 #define SCL (PBDRH & 0x20)
54 /* arbitrary delay loop */
55 #define DELAY do { int _x; for(_x=0;_x<20;_x++);} while (0)
57 static struct mutex i2c_mtx
;
66 mutex_unlock(&i2c_mtx
);
91 #if CONFIG_I2C == I2C_ONDIO
92 /* make PB6 & PB7 general I/O */
95 /* make PB7 & PB13 general I/O */
96 PBCR1
&= ~0x0c00; /* PB13 */
97 PBCR2
&= ~0xc000; /* PB7 */
108 void i2c_ack(int bit
)
110 /* Here's the deal. The MAS is slow, and sometimes needs to wait
111 before it can receive the acknowledge. Therefore it forces the clock
112 low until it is ready. We need to poll the clock line until it goes
113 high before we release the ack. */
115 SCL_LO
; /* Set the clock low */
125 SCL_INPUT
; /* Set the clock to input */
126 while(!SCL
) /* and wait for the MAS to release it */
138 /* Here's the deal. The MAS is slow, and sometimes needs to wait
139 before it can send the acknowledge. Therefore it forces the clock
140 low until it is ready. We need to poll the clock line until it goes
141 high before we read the ack. */
143 #ifdef HAVE_I2C_LOW_FIRST
144 SDA_LO
; /* First, discharge the data line */
146 SDA_INPUT
; /* And set to input */
147 SCL_INPUT
; /* Set the clock to input */
148 while(!SCL
) /* and wait for the MAS to release it */
162 void i2c_outb(unsigned char byte
)
166 /* clock out each bit, MSB first */
167 for ( i
=0x80; i
; i
>>=1 ) {
183 unsigned char i2c_inb(int ack
)
186 unsigned char byte
= 0;
188 /* clock in each bit, MSB first */
189 for ( i
=0x80; i
; i
>>=1 ) {
190 #ifdef HAVE_I2C_LOW_FIRST
191 /* Tricky business. Here we discharge the data line by driving it low
192 and then set it to input to see if it stays low or goes high */
193 SDA_LO
; /* First, discharge the data line */
195 SDA_INPUT
; /* And set to input */
208 int i2c_write(int address
, const unsigned char* buf
, int count
)
213 i2c_outb(address
& 0xfe);
216 for (i
=0; i
<count
; i
++)
228 debugf("i2c_write() - no ack\n");
235 int i2c_read(int address
, unsigned char* buf
, int count
)
240 i2c_outb(address
| 1);
242 for (i
=0; i
<count
; i
++) {