3 * Heiko Schocher, DENX Software Engineering, hs@denx.de.
5 * See file CREDITS for list of people who contributed to this
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of
11 * the License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
29 #if defined(CONFIG_OF_BOARD_SETUP) && defined(CONFIG_OF_LIBFDT)
33 #if defined(CONFIG_HARD_I2C) || defined(CONFIG_SOFT_I2C)
37 extern int i2c_soft_read_pin (void);
39 #if defined(CFG_I2C_INIT_BOARD)
40 #define DELAY_ABORT_SEQ 62
41 #define DELAY_HALF_PERIOD (500 / (CFG_I2C_SPEED / 1000))
43 #if defined(CONFIG_MGCOGE)
44 #define SDA_MASK 0x00010000
45 #define SCL_MASK 0x00020000
46 static void set_pin (int state, unsigned long mask)
48 volatile ioport_t *iop = ioport_addr ((immap_t *)CFG_IMMR, 3);
58 static int get_pin (unsigned long mask)
60 volatile ioport_t *iop = ioport_addr ((immap_t *)CFG_IMMR, 3);
63 return (0 != (iop->pdat & (mask)));
66 static void set_sda (int state)
68 set_pin (state, SDA_MASK);
71 static void set_scl (int state)
73 set_pin (state, SCL_MASK);
76 static int get_sda (void)
78 return get_pin (SDA_MASK);
81 static int get_scl (void)
83 return get_pin (SCL_MASK);
86 #if defined(CONFIG_HARD_I2C)
87 static void setports (int gpio)
89 volatile ioport_t *iop = ioport_addr ((immap_t *)CFG_IMMR, 3);
92 iop->ppar &= ~(SDA_MASK | SCL_MASK);
93 iop->podr &= ~(SDA_MASK | SCL_MASK);
95 iop->ppar |= (SDA_MASK | SCL_MASK);
96 iop->pdir &= ~(SDA_MASK | SCL_MASK);
97 iop->podr |= (SDA_MASK | SCL_MASK);
103 #if defined(CONFIG_MGSUVD)
104 static void set_sda (int state)
109 static void set_scl (int state)
114 static int get_sda (void)
116 return i2c_soft_read_pin ();
119 static int get_scl (void)
123 *(unsigned short *)(I2C_BASE_DIR) &= ~SCL_CONF;
125 val = *(unsigned char *)(I2C_BASE_PORT);
127 return ((val & SCL_BIT) == SCL_BIT);
132 static void writeStartSeq (void)
135 udelay (DELAY_HALF_PERIOD);
137 udelay (DELAY_HALF_PERIOD);
139 udelay (DELAY_HALF_PERIOD);
141 udelay (DELAY_HALF_PERIOD);
144 /* I2C is a synchronous protocol and resets of the processor in the middle
145 of an access can block the I2C Bus until a powerdown of the full unit is
146 done. This function toggles the SCL until the SCL and SCA line are
147 released, but max. 16 times, after this a I2C start-sequence is sent.
148 This I2C Deblocking mechanism was developed by Keymile in association
149 with Anatech and Atmel in 1998.
151 static int i2c_make_abort (void)
163 udelay (DELAY_ABORT_SEQ);
165 udelay (DELAY_ABORT_SEQ);
166 scl_state = get_scl ();
167 sda_state = get_sda ();
168 if (scl_state && sda_state) {
175 for (i =0; i < 5; i++) {
184 * i2c_init_board - reset i2c bus. When the board is powercycled during a
185 * bus transfer it might hang; for details see doc/I2C_Edge_Conditions.
187 void i2c_init_board(void)
189 #if defined(CONFIG_HARD_I2C)
190 volatile immap_t *immap = (immap_t *)CFG_IMMR ;
191 volatile i2c8260_t *i2c = (i2c8260_t *)&immap->im_i2c;
193 /* disable I2C controller first, otherwhise it thinks we want to */
194 /* talk to the slave port... */
195 i2c->i2c_i2mod &= ~0x01;
197 /* Set the PortPins to GPIO */
201 /* Now run the AbortSequence() */
204 #if defined(CONFIG_HARD_I2C)
205 /* Set the PortPins back to use for I2C */