summaryrefslogtreecommitdiff
path: root/drivers/i2c/aspeed_i2c.c
blob: ff6c7565babc725ef89d196feaec4d42e8a5a39e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
/*
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include <common.h>
#include <asm/arch/aspeed_i2c.h>

#ifdef CONFIG_DRIVER_ASPEED_I2C

void i2c_init (int speed, int slaveadd)
{
	unsigned long SCURegister;
//I2C Reset
        SCURegister = inl (SCU_BASE + SCU_RESET_CONTROL);
        outl (SCURegister & ~(0x04), SCU_BASE + SCU_RESET_CONTROL);
//I2C Multi-Pin
        SCURegister = inl (SCU_BASE + SCU_MULTIFUNCTION_PIN_CTL5_REG);
        outl ((SCURegister | 0x30000), SCU_BASE + SCU_MULTIFUNCTION_PIN_CTL5_REG);
//Reset
	outl (0, I2C_FUNCTION_CONTROL_REGISTER);
//Set AC Timing, we use fix AC timing for eeprom in u-boot
	outl (AC_TIMING, I2C_AC_TIMING_REGISTER_1);
	outl (0, I2C_AC_TIMING_REGISTER_2);
//Clear Interrupt
	outl (ALL_CLEAR, I2C_INTERRUPT_STATUS_REGISTER);
//Enable Master Mode
	outl (MASTER_ENABLE, I2C_FUNCTION_CONTROL_REGISTER);
//Enable Interrupt, STOP Interrupt has bug in AST2000
	outl (0xAF, I2C_INTERRUPT_CONTROL_REGISTER);
//Set Slave address, should not use for eeprom
	outl (slaveadd, I2C_DEVICE_ADDRESS_REGISTER);
}

static int i2c_read_byte (u8 devaddr, u16 regoffset, u8 * value, int alen)
{
	int i2c_error = 0;
	u32 status, count = 0;

//Start and Send Device Address
	outl (devaddr, I2C_BYTE_BUFFER_REGISTER);
	outl (MASTER_START_COMMAND | MASTER_TX_COMMAND, I2C_COMMAND_REGISTER);
//Wait Tx ACK
        do {
            status = (inl (I2C_INTERRUPT_STATUS_REGISTER) & (TX_ACK | TX_NACK));
            count++;
            if (count == LOOP_COUNT) {
                i2c_error = 1;
                printf ("Start and Send Device Address can't get ACK back\n");
                return i2c_error;
            }
        } while (status != TX_ACK);
        count = 0;
//Clear Interrupt
	outl (ALL_CLEAR, I2C_INTERRUPT_STATUS_REGISTER);
//Check if address length equals to 16bits
        if (alen != 1) {
//Send Device Register Offset (HIGH BYTE)
	    outl ((regoffset & 0xFF00) >> 8, I2C_BYTE_BUFFER_REGISTER);
	    outl (MASTER_TX_COMMAND, I2C_COMMAND_REGISTER);
//Wait Tx ACK
            do {
                status = (inl (I2C_INTERRUPT_STATUS_REGISTER) & (TX_ACK | TX_NACK));
                count++;
                if (count == LOOP_COUNT) {
                    i2c_error = 1;
                    printf ("Send Device Register Offset can't get ACK back\n");
                    return i2c_error;
                }
            } while (status != TX_ACK);
            count = 0;
//Clear Interrupt
	    outl (ALL_CLEAR, I2C_INTERRUPT_STATUS_REGISTER);
	}
//Send Device Register Offset(LOW)
	outl (regoffset & 0xFF, I2C_BYTE_BUFFER_REGISTER);
	outl (MASTER_TX_COMMAND, I2C_COMMAND_REGISTER);
//Wait Tx ACK
        do {
            status = (inl (I2C_INTERRUPT_STATUS_REGISTER) & (TX_ACK | TX_NACK));
            count++;
            if (count == LOOP_COUNT) {
                i2c_error = 1;
                printf ("Send Device Register Offset can't get ACK back\n");
                return i2c_error;
            }
        } while (status != TX_ACK);
        count = 0;
//Clear Interrupt
	outl (ALL_CLEAR, I2C_INTERRUPT_STATUS_REGISTER);
//Start, Send Device Address + 1 (Read Mode), Receive Data
	outl (devaddr + 1, I2C_BYTE_BUFFER_REGISTER);
	outl (MASTER_START_COMMAND | MASTER_TX_COMMAND | MASTER_RX_COMMAND | RX_COMMAND_LIST, I2C_COMMAND_REGISTER);
//Wait Rx Done
        do {
            status = (inl (I2C_INTERRUPT_STATUS_REGISTER) & RX_DONE);
            count++;
            if (count == LOOP_COUNT) {
                i2c_error = 1;
                printf ("Can't get RX_DONE back\n");
                return i2c_error;
            }
        } while (status != RX_DONE);
        count = 0;
//Clear Interrupt
	outl (ALL_CLEAR, I2C_INTERRUPT_STATUS_REGISTER);
//Enable Interrupt + Stop Interrupt
	outl (0xBF, I2C_INTERRUPT_CONTROL_REGISTER);
//Issue Stop Command
	outl (MASTER_STOP_COMMAND, I2C_COMMAND_REGISTER);
//Wait Stop
        do {
            status = (inl (I2C_INTERRUPT_STATUS_REGISTER) & STOP_DONE);
            count++;
            if (count == LOOP_COUNT) {
                i2c_error = 1;
                printf ("Can't get STOP back\n");
                return i2c_error;
            }
        } while (status != STOP_DONE);
//Disable Stop Interrupt
	outl (0xAF, I2C_INTERRUPT_CONTROL_REGISTER);
//Clear Interrupt
	outl (ALL_CLEAR, I2C_INTERRUPT_STATUS_REGISTER);
//Read Received Data
        *value = ((inl (I2C_BYTE_BUFFER_REGISTER) & 0xFF00) >> 8);

	return i2c_error;
}

static int i2c_write_byte (u8 devaddr, u16 regoffset, u8 value, int alen)
{
	int i2c_error = 0;
        u32 status, count = 0;

//Start and Send Device Address
	outl (devaddr, I2C_BYTE_BUFFER_REGISTER);
	outl (MASTER_START_COMMAND | MASTER_TX_COMMAND, I2C_COMMAND_REGISTER);
//Wait Tx ACK
        do {
            status = (inl (I2C_INTERRUPT_STATUS_REGISTER) & (TX_ACK | TX_NACK));
            count++;
            if (status == TX_NACK) {
//Clear Interrupt
   	        outl (ALL_CLEAR, I2C_INTERRUPT_STATUS_REGISTER);
//Re-send Start and Send Device Address while NACK return
	        outl (devaddr, I2C_BYTE_BUFFER_REGISTER);
        	outl (MASTER_START_COMMAND | MASTER_TX_COMMAND, I2C_COMMAND_REGISTER);
            }
            else {
            	if (count == LOOP_COUNT) {
            	    i2c_error = 1;
                    printf ("Start and Send Device Address can't get ACK back\n");
                    return i2c_error;
            	}
            }
        } while (status != TX_ACK);
        count = 0;
//Clear Interrupt
	outl (ALL_CLEAR, I2C_INTERRUPT_STATUS_REGISTER);
//Check if address length equals to 16bits
        if (alen != 1) {
//Send Device Register Offset (HIGH BYTE)
	    outl ((regoffset & 0xFF00) >> 8, I2C_BYTE_BUFFER_REGISTER);
	    outl (MASTER_TX_COMMAND, I2C_COMMAND_REGISTER);
//Wait Tx ACK
            do {
                status = (inl (I2C_INTERRUPT_STATUS_REGISTER) & (TX_ACK | TX_NACK));
                count++;
                if (count == LOOP_COUNT) {
                    i2c_error = 1;
                    printf ("Send Device Register Offset can't get ACK back\n");
                    return i2c_error;
                }
            } while (status != TX_ACK);
            count = 0;
//Clear Interrupt
	    outl (ALL_CLEAR, I2C_INTERRUPT_STATUS_REGISTER);
	}
//Send Device Register Offset
	outl (regoffset & 0xFF, I2C_BYTE_BUFFER_REGISTER);
	outl (MASTER_TX_COMMAND, I2C_COMMAND_REGISTER);
//Wait Tx ACK
        do {
            status = (inl (I2C_INTERRUPT_STATUS_REGISTER) & (TX_ACK | TX_NACK));
            count++;
            if (count == LOOP_COUNT) {
                i2c_error = 1;
                printf ("Send Device Register Offset can't get ACK back\n");
                return i2c_error;
            }
        } while (status != TX_ACK);
        count = 0;
//Clear Interrupt
	outl (ALL_CLEAR, I2C_INTERRUPT_STATUS_REGISTER);
//Send Device Register Value
	outl (value, I2C_BYTE_BUFFER_REGISTER);
	outl (MASTER_TX_COMMAND, I2C_COMMAND_REGISTER);
//Wait Tx ACK
        do {
            status = (inl (I2C_INTERRUPT_STATUS_REGISTER) & (TX_ACK | TX_NACK));
            count++;
            if (count == LOOP_COUNT) {
                i2c_error = 1;
                printf ("Send Device Register Value can't get ACK back\n");
                return i2c_error;
            }
        } while (status != TX_ACK);
        count = 0;
//Clear Interrupt
	outl (ALL_CLEAR, I2C_INTERRUPT_STATUS_REGISTER);
//Enable Interrupt + Stop Interrupt
	outl (0xBF, I2C_INTERRUPT_CONTROL_REGISTER);
//Issue Stop Command
	outl (MASTER_STOP_COMMAND, I2C_COMMAND_REGISTER);
//Wait Stop
        do {
            status = (inl (I2C_INTERRUPT_STATUS_REGISTER) & STOP_DONE);
            count++;
            if (count == LOOP_COUNT) {
                i2c_error = 1;
                printf ("Can't get STOP back\n");
                return i2c_error;
            }
        } while (status != STOP_DONE);
//Disable Stop Interrupt
	outl (0xAF, I2C_INTERRUPT_CONTROL_REGISTER);
//Clear Interrupt
	outl (ALL_CLEAR, I2C_INTERRUPT_STATUS_REGISTER);

	return i2c_error;
}

int i2c_probe (uchar chip)
{
//Suppose IP is always on chip
	int res = 0;
	
	return res;
}

int i2c_read (uchar device_addr, uint register_offset, int alen, uchar * buffer, int len)
{
	int i;

        if ((alen == 1) && ((register_offset + len) > 256)) {
        	printf ("Register index overflow\n");
        }
        
        for (i = 0; i < len; i++) {
	        if (i2c_read_byte (device_addr, register_offset + i, &buffer[i], alen)) {
		        printf ("I2C read: I/O error\n");
			i2c_init (CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE);
			return 1;
		}
	}

	return 0;
}

int i2c_write (uchar device_addr, uint register_offset, int alen, uchar * buffer, int len)
{
	int i;

        if ((alen == 1) && ((register_offset + len) > 256)) {
        	printf ("Register index overflow\n");
        }

        for (i = 0; i < len; i++) {
	        if (i2c_write_byte (device_addr, register_offset + i, buffer[i], alen)) {
		        printf ("I2C read: I/O error\n");
			i2c_init (CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE);
			return 1;
   	        }
	}

	return 0;
}

#endif /* CONFIG_DRIVER_ASPEED_I2C */