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
);
93 #if CONFIG_I2C == I2C_ONDIO
94 /* make PB6 & PB7 general I/O */
97 /* make PB7 & PB13 general I/O */
98 PBCR1
&= ~0x0c00; /* PB13 */
99 PBCR2
&= ~0xc000; /* PB7 */
110 void i2c_ack(int bit
)
112 /* Here's the deal. The MAS is slow, and sometimes needs to wait
113 before it can receive the acknowledge. Therefore it forces the clock
114 low until it is ready. We need to poll the clock line until it goes
115 high before we release the ack. */
117 SCL_LO
; /* Set the clock low */
127 SCL_INPUT
; /* Set the clock to input */
128 while(!SCL
) /* and wait for the MAS to release it */
140 /* Here's the deal. The MAS is slow, and sometimes needs to wait
141 before it can send the acknowledge. Therefore it forces the clock
142 low until it is ready. We need to poll the clock line until it goes
143 high before we read the ack. */
145 #ifdef HAVE_I2C_LOW_FIRST
146 SDA_LO
; /* First, discharge the data line */
148 SDA_INPUT
; /* And set to input */
149 SCL_INPUT
; /* Set the clock to input */
150 while(!SCL
) /* and wait for the MAS to release it */
164 void i2c_outb(unsigned char byte
)
168 /* clock out each bit, MSB first */
169 for ( i
=0x80; i
; i
>>=1 ) {
185 unsigned char i2c_inb(int ack
)
188 unsigned char byte
= 0;
190 /* clock in each bit, MSB first */
191 for ( i
=0x80; i
; i
>>=1 ) {
192 #ifdef HAVE_I2C_LOW_FIRST
193 /* Tricky business. Here we discharge the data line by driving it low
194 and then set it to input to see if it stays low or goes high */
195 SDA_LO
; /* First, discharge the data line */
197 SDA_INPUT
; /* And set to input */
210 int i2c_write(int address
, const unsigned char* buf
, int count
)
215 i2c_outb(address
& 0xfe);
218 for (i
=0; i
<count
; i
++)
230 debugf("i2c_write() - no ack\n");
237 int i2c_read(int address
, unsigned char* buf
, int count
)
242 i2c_outb(address
| 1);
244 for (i
=0; i
<count
; i
++) {