xref: /rk3399_rockchip-uboot/drivers/i2c/designware_i2c.c (revision ac6e2fe6e4afe54a9c7ef0d93827a86e264814f2)
1031ed2faSVipin KUMAR /*
2031ed2faSVipin KUMAR  * (C) Copyright 2009
3031ed2faSVipin KUMAR  * Vipin Kumar, ST Micoelectronics, vipin.kumar@st.com.
4031ed2faSVipin KUMAR  *
5031ed2faSVipin KUMAR  * See file CREDITS for list of people who contributed to this
6031ed2faSVipin KUMAR  * project.
7031ed2faSVipin KUMAR  *
8031ed2faSVipin KUMAR  * This program is free software; you can redistribute it and/or
9031ed2faSVipin KUMAR  * modify it under the terms of the GNU General Public License as
10031ed2faSVipin KUMAR  * published by the Free Software Foundation; either version 2 of
11031ed2faSVipin KUMAR  * the License, or (at your option) any later version.
12031ed2faSVipin KUMAR  *
13031ed2faSVipin KUMAR  * This program is distributed in the hope that it will be useful,
14031ed2faSVipin KUMAR  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15031ed2faSVipin KUMAR  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16031ed2faSVipin KUMAR  * GNU General Public License for more details.
17031ed2faSVipin KUMAR  *
18031ed2faSVipin KUMAR  * You should have received a copy of the GNU General Public License
19031ed2faSVipin KUMAR  * along with this program; if not, write to the Free Software
20031ed2faSVipin KUMAR  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
21031ed2faSVipin KUMAR  * MA 02111-1307 USA
22031ed2faSVipin KUMAR  */
23031ed2faSVipin KUMAR 
24031ed2faSVipin KUMAR #include <common.h>
25031ed2faSVipin KUMAR #include <asm/io.h>
26031ed2faSVipin KUMAR #include <asm/arch/hardware.h>
27031ed2faSVipin KUMAR #include "designware_i2c.h"
28031ed2faSVipin KUMAR 
29*ac6e2fe6SArmando Visconti #ifdef CONFIG_I2C_MULTI_BUS
30*ac6e2fe6SArmando Visconti static unsigned int bus_initialized[CONFIG_SYS_I2C_BUS_MAX];
31*ac6e2fe6SArmando Visconti static unsigned int current_bus = 0;
32*ac6e2fe6SArmando Visconti #endif
33*ac6e2fe6SArmando Visconti 
34*ac6e2fe6SArmando Visconti static struct i2c_regs *i2c_regs_p =
35031ed2faSVipin KUMAR     (struct i2c_regs *)CONFIG_SYS_I2C_BASE;
36031ed2faSVipin KUMAR 
37031ed2faSVipin KUMAR /*
38031ed2faSVipin KUMAR  * set_speed - Set the i2c speed mode (standard, high, fast)
39031ed2faSVipin KUMAR  * @i2c_spd:	required i2c speed mode
40031ed2faSVipin KUMAR  *
41031ed2faSVipin KUMAR  * Set the i2c speed mode (standard, high, fast)
42031ed2faSVipin KUMAR  */
43031ed2faSVipin KUMAR static void set_speed(int i2c_spd)
44031ed2faSVipin KUMAR {
45031ed2faSVipin KUMAR 	unsigned int cntl;
46031ed2faSVipin KUMAR 	unsigned int hcnt, lcnt;
47031ed2faSVipin KUMAR 	unsigned int high, low;
485e3e8ddaSArmando Visconti 	unsigned int enbl;
495e3e8ddaSArmando Visconti 
505e3e8ddaSArmando Visconti 	/* to set speed cltr must be disabled */
515e3e8ddaSArmando Visconti 	enbl = readl(&i2c_regs_p->ic_enable);
525e3e8ddaSArmando Visconti 	enbl &= ~IC_ENABLE_0B;
535e3e8ddaSArmando Visconti 	writel(enbl, &i2c_regs_p->ic_enable);
545e3e8ddaSArmando Visconti 
55031ed2faSVipin KUMAR 
56031ed2faSVipin KUMAR 	cntl = (readl(&i2c_regs_p->ic_con) & (~IC_CON_SPD_MSK));
57031ed2faSVipin KUMAR 
58031ed2faSVipin KUMAR 	switch (i2c_spd) {
59031ed2faSVipin KUMAR 	case IC_SPEED_MODE_MAX:
60031ed2faSVipin KUMAR 		cntl |= IC_CON_SPD_HS;
61031ed2faSVipin KUMAR 		high = MIN_HS_SCL_HIGHTIME;
62031ed2faSVipin KUMAR 		low = MIN_HS_SCL_LOWTIME;
63031ed2faSVipin KUMAR 		break;
64031ed2faSVipin KUMAR 
65031ed2faSVipin KUMAR 	case IC_SPEED_MODE_STANDARD:
66031ed2faSVipin KUMAR 		cntl |= IC_CON_SPD_SS;
67031ed2faSVipin KUMAR 		high = MIN_SS_SCL_HIGHTIME;
68031ed2faSVipin KUMAR 		low = MIN_SS_SCL_LOWTIME;
69031ed2faSVipin KUMAR 		break;
70031ed2faSVipin KUMAR 
71031ed2faSVipin KUMAR 	case IC_SPEED_MODE_FAST:
72031ed2faSVipin KUMAR 	default:
73031ed2faSVipin KUMAR 		cntl |= IC_CON_SPD_FS;
74031ed2faSVipin KUMAR 		high = MIN_FS_SCL_HIGHTIME;
75031ed2faSVipin KUMAR 		low = MIN_FS_SCL_LOWTIME;
76031ed2faSVipin KUMAR 		break;
77031ed2faSVipin KUMAR 	}
78031ed2faSVipin KUMAR 
79031ed2faSVipin KUMAR 	writel(cntl, &i2c_regs_p->ic_con);
80031ed2faSVipin KUMAR 
81031ed2faSVipin KUMAR 	hcnt = (IC_CLK * high) / NANO_TO_MICRO;
82031ed2faSVipin KUMAR 	writel(hcnt, &i2c_regs_p->ic_fs_scl_hcnt);
83031ed2faSVipin KUMAR 
84031ed2faSVipin KUMAR 	lcnt = (IC_CLK * low) / NANO_TO_MICRO;
85031ed2faSVipin KUMAR 	writel(lcnt, &i2c_regs_p->ic_fs_scl_lcnt);
865e3e8ddaSArmando Visconti 
875e3e8ddaSArmando Visconti 	/* re-enable i2c ctrl back now that speed is set */
885e3e8ddaSArmando Visconti 	enbl |= IC_ENABLE_0B;
895e3e8ddaSArmando Visconti 	writel(enbl, &i2c_regs_p->ic_enable);
90031ed2faSVipin KUMAR }
91031ed2faSVipin KUMAR 
92031ed2faSVipin KUMAR /*
93031ed2faSVipin KUMAR  * i2c_set_bus_speed - Set the i2c speed
94031ed2faSVipin KUMAR  * @speed:	required i2c speed
95031ed2faSVipin KUMAR  *
96031ed2faSVipin KUMAR  * Set the i2c speed.
97031ed2faSVipin KUMAR  */
98496ba48fSStefan Roese int i2c_set_bus_speed(int speed)
99031ed2faSVipin KUMAR {
100031ed2faSVipin KUMAR 	if (speed >= I2C_MAX_SPEED)
101031ed2faSVipin KUMAR 		set_speed(IC_SPEED_MODE_MAX);
102031ed2faSVipin KUMAR 	else if (speed >= I2C_FAST_SPEED)
103031ed2faSVipin KUMAR 		set_speed(IC_SPEED_MODE_FAST);
104031ed2faSVipin KUMAR 	else
105031ed2faSVipin KUMAR 		set_speed(IC_SPEED_MODE_STANDARD);
106496ba48fSStefan Roese 
107496ba48fSStefan Roese 	return 0;
108031ed2faSVipin KUMAR }
109031ed2faSVipin KUMAR 
110031ed2faSVipin KUMAR /*
111031ed2faSVipin KUMAR  * i2c_get_bus_speed - Gets the i2c speed
112031ed2faSVipin KUMAR  *
113031ed2faSVipin KUMAR  * Gets the i2c speed.
114031ed2faSVipin KUMAR  */
115031ed2faSVipin KUMAR int i2c_get_bus_speed(void)
116031ed2faSVipin KUMAR {
117031ed2faSVipin KUMAR 	u32 cntl;
118031ed2faSVipin KUMAR 
119031ed2faSVipin KUMAR 	cntl = (readl(&i2c_regs_p->ic_con) & IC_CON_SPD_MSK);
120031ed2faSVipin KUMAR 
121031ed2faSVipin KUMAR 	if (cntl == IC_CON_SPD_HS)
122031ed2faSVipin KUMAR 		return I2C_MAX_SPEED;
123031ed2faSVipin KUMAR 	else if (cntl == IC_CON_SPD_FS)
124031ed2faSVipin KUMAR 		return I2C_FAST_SPEED;
125031ed2faSVipin KUMAR 	else if (cntl == IC_CON_SPD_SS)
126031ed2faSVipin KUMAR 		return I2C_STANDARD_SPEED;
127031ed2faSVipin KUMAR 
128031ed2faSVipin KUMAR 	return 0;
129031ed2faSVipin KUMAR }
130031ed2faSVipin KUMAR 
131031ed2faSVipin KUMAR /*
132031ed2faSVipin KUMAR  * i2c_init - Init function
133031ed2faSVipin KUMAR  * @speed:	required i2c speed
134031ed2faSVipin KUMAR  * @slaveadd:	slave address for the device
135031ed2faSVipin KUMAR  *
136031ed2faSVipin KUMAR  * Initialization function.
137031ed2faSVipin KUMAR  */
138031ed2faSVipin KUMAR void i2c_init(int speed, int slaveadd)
139031ed2faSVipin KUMAR {
140031ed2faSVipin KUMAR 	unsigned int enbl;
141031ed2faSVipin KUMAR 
142031ed2faSVipin KUMAR 	/* Disable i2c */
143031ed2faSVipin KUMAR 	enbl = readl(&i2c_regs_p->ic_enable);
144031ed2faSVipin KUMAR 	enbl &= ~IC_ENABLE_0B;
145031ed2faSVipin KUMAR 	writel(enbl, &i2c_regs_p->ic_enable);
146031ed2faSVipin KUMAR 
147031ed2faSVipin KUMAR 	writel((IC_CON_SD | IC_CON_SPD_FS | IC_CON_MM), &i2c_regs_p->ic_con);
148031ed2faSVipin KUMAR 	writel(IC_RX_TL, &i2c_regs_p->ic_rx_tl);
149031ed2faSVipin KUMAR 	writel(IC_TX_TL, &i2c_regs_p->ic_tx_tl);
150031ed2faSVipin KUMAR 	i2c_set_bus_speed(speed);
151031ed2faSVipin KUMAR 	writel(IC_STOP_DET, &i2c_regs_p->ic_intr_mask);
152031ed2faSVipin KUMAR 	writel(slaveadd, &i2c_regs_p->ic_sar);
153031ed2faSVipin KUMAR 
154031ed2faSVipin KUMAR 	/* Enable i2c */
155031ed2faSVipin KUMAR 	enbl = readl(&i2c_regs_p->ic_enable);
156031ed2faSVipin KUMAR 	enbl |= IC_ENABLE_0B;
157031ed2faSVipin KUMAR 	writel(enbl, &i2c_regs_p->ic_enable);
158*ac6e2fe6SArmando Visconti 
159*ac6e2fe6SArmando Visconti #ifdef CONFIG_I2C_MULTI_BUS
160*ac6e2fe6SArmando Visconti 	bus_initialized[current_bus] = 1;
161*ac6e2fe6SArmando Visconti #endif
162031ed2faSVipin KUMAR }
163031ed2faSVipin KUMAR 
164031ed2faSVipin KUMAR /*
165031ed2faSVipin KUMAR  * i2c_setaddress - Sets the target slave address
166031ed2faSVipin KUMAR  * @i2c_addr:	target i2c address
167031ed2faSVipin KUMAR  *
168031ed2faSVipin KUMAR  * Sets the target slave address.
169031ed2faSVipin KUMAR  */
170031ed2faSVipin KUMAR static void i2c_setaddress(unsigned int i2c_addr)
171031ed2faSVipin KUMAR {
172031ed2faSVipin KUMAR 	writel(i2c_addr, &i2c_regs_p->ic_tar);
173031ed2faSVipin KUMAR }
174031ed2faSVipin KUMAR 
175031ed2faSVipin KUMAR /*
176031ed2faSVipin KUMAR  * i2c_flush_rxfifo - Flushes the i2c RX FIFO
177031ed2faSVipin KUMAR  *
178031ed2faSVipin KUMAR  * Flushes the i2c RX FIFO
179031ed2faSVipin KUMAR  */
180031ed2faSVipin KUMAR static void i2c_flush_rxfifo(void)
181031ed2faSVipin KUMAR {
182031ed2faSVipin KUMAR 	while (readl(&i2c_regs_p->ic_status) & IC_STATUS_RFNE)
183031ed2faSVipin KUMAR 		readl(&i2c_regs_p->ic_cmd_data);
184031ed2faSVipin KUMAR }
185031ed2faSVipin KUMAR 
186031ed2faSVipin KUMAR /*
187031ed2faSVipin KUMAR  * i2c_wait_for_bb - Waits for bus busy
188031ed2faSVipin KUMAR  *
189031ed2faSVipin KUMAR  * Waits for bus busy
190031ed2faSVipin KUMAR  */
191031ed2faSVipin KUMAR static int i2c_wait_for_bb(void)
192031ed2faSVipin KUMAR {
193031ed2faSVipin KUMAR 	unsigned long start_time_bb = get_timer(0);
194031ed2faSVipin KUMAR 
195031ed2faSVipin KUMAR 	while ((readl(&i2c_regs_p->ic_status) & IC_STATUS_MA) ||
196031ed2faSVipin KUMAR 	       !(readl(&i2c_regs_p->ic_status) & IC_STATUS_TFE)) {
197031ed2faSVipin KUMAR 
198031ed2faSVipin KUMAR 		/* Evaluate timeout */
199031ed2faSVipin KUMAR 		if (get_timer(start_time_bb) > (unsigned long)(I2C_BYTE_TO_BB))
200031ed2faSVipin KUMAR 			return 1;
201031ed2faSVipin KUMAR 	}
202031ed2faSVipin KUMAR 
203031ed2faSVipin KUMAR 	return 0;
204031ed2faSVipin KUMAR }
205031ed2faSVipin KUMAR 
206031ed2faSVipin KUMAR /* check parameters for i2c_read and i2c_write */
207031ed2faSVipin KUMAR static int check_params(uint addr, int alen, uchar *buffer, int len)
208031ed2faSVipin KUMAR {
209031ed2faSVipin KUMAR 	if (buffer == NULL) {
210031ed2faSVipin KUMAR 		printf("Buffer is invalid\n");
211031ed2faSVipin KUMAR 		return 1;
212031ed2faSVipin KUMAR 	}
213031ed2faSVipin KUMAR 
214031ed2faSVipin KUMAR 	if (alen > 1) {
215031ed2faSVipin KUMAR 		printf("addr len %d not supported\n", alen);
216031ed2faSVipin KUMAR 		return 1;
217031ed2faSVipin KUMAR 	}
218031ed2faSVipin KUMAR 
219031ed2faSVipin KUMAR 	if (addr + len > 256) {
220031ed2faSVipin KUMAR 		printf("address out of range\n");
221031ed2faSVipin KUMAR 		return 1;
222031ed2faSVipin KUMAR 	}
223031ed2faSVipin KUMAR 
224031ed2faSVipin KUMAR 	return 0;
225031ed2faSVipin KUMAR }
226031ed2faSVipin KUMAR 
227031ed2faSVipin KUMAR static int i2c_xfer_init(uchar chip, uint addr)
228031ed2faSVipin KUMAR {
229496ba48fSStefan Roese 	if (i2c_wait_for_bb())
230031ed2faSVipin KUMAR 		return 1;
231031ed2faSVipin KUMAR 
232031ed2faSVipin KUMAR 	i2c_setaddress(chip);
233031ed2faSVipin KUMAR 	writel(addr, &i2c_regs_p->ic_cmd_data);
234031ed2faSVipin KUMAR 
235031ed2faSVipin KUMAR 	return 0;
236031ed2faSVipin KUMAR }
237031ed2faSVipin KUMAR 
238031ed2faSVipin KUMAR static int i2c_xfer_finish(void)
239031ed2faSVipin KUMAR {
240031ed2faSVipin KUMAR 	ulong start_stop_det = get_timer(0);
241031ed2faSVipin KUMAR 
242031ed2faSVipin KUMAR 	while (1) {
243031ed2faSVipin KUMAR 		if ((readl(&i2c_regs_p->ic_raw_intr_stat) & IC_STOP_DET)) {
244031ed2faSVipin KUMAR 			readl(&i2c_regs_p->ic_clr_stop_det);
245031ed2faSVipin KUMAR 			break;
246031ed2faSVipin KUMAR 		} else if (get_timer(start_stop_det) > I2C_STOPDET_TO) {
247031ed2faSVipin KUMAR 			break;
248031ed2faSVipin KUMAR 		}
249031ed2faSVipin KUMAR 	}
250031ed2faSVipin KUMAR 
251031ed2faSVipin KUMAR 	if (i2c_wait_for_bb()) {
252031ed2faSVipin KUMAR 		printf("Timed out waiting for bus\n");
253031ed2faSVipin KUMAR 		return 1;
254031ed2faSVipin KUMAR 	}
255031ed2faSVipin KUMAR 
256031ed2faSVipin KUMAR 	i2c_flush_rxfifo();
257031ed2faSVipin KUMAR 
258031ed2faSVipin KUMAR 	/* Wait for read/write operation to complete on actual memory */
259031ed2faSVipin KUMAR 	udelay(10000);
260031ed2faSVipin KUMAR 
261031ed2faSVipin KUMAR 	return 0;
262031ed2faSVipin KUMAR }
263031ed2faSVipin KUMAR 
264031ed2faSVipin KUMAR /*
265031ed2faSVipin KUMAR  * i2c_read - Read from i2c memory
266031ed2faSVipin KUMAR  * @chip:	target i2c address
267031ed2faSVipin KUMAR  * @addr:	address to read from
268031ed2faSVipin KUMAR  * @alen:
269031ed2faSVipin KUMAR  * @buffer:	buffer for read data
270031ed2faSVipin KUMAR  * @len:	no of bytes to be read
271031ed2faSVipin KUMAR  *
272031ed2faSVipin KUMAR  * Read from i2c memory.
273031ed2faSVipin KUMAR  */
274031ed2faSVipin KUMAR int i2c_read(uchar chip, uint addr, int alen, uchar *buffer, int len)
275031ed2faSVipin KUMAR {
276031ed2faSVipin KUMAR 	unsigned long start_time_rx;
277031ed2faSVipin KUMAR 
278031ed2faSVipin KUMAR 	if (check_params(addr, alen, buffer, len))
279031ed2faSVipin KUMAR 		return 1;
280031ed2faSVipin KUMAR 
281031ed2faSVipin KUMAR 	if (i2c_xfer_init(chip, addr))
282031ed2faSVipin KUMAR 		return 1;
283031ed2faSVipin KUMAR 
284031ed2faSVipin KUMAR 	start_time_rx = get_timer(0);
285031ed2faSVipin KUMAR 	while (len) {
286031ed2faSVipin KUMAR 		writel(IC_CMD, &i2c_regs_p->ic_cmd_data);
287031ed2faSVipin KUMAR 
288031ed2faSVipin KUMAR 		if (readl(&i2c_regs_p->ic_status) & IC_STATUS_RFNE) {
289031ed2faSVipin KUMAR 			*buffer++ = (uchar)readl(&i2c_regs_p->ic_cmd_data);
290031ed2faSVipin KUMAR 			len--;
291031ed2faSVipin KUMAR 			start_time_rx = get_timer(0);
292031ed2faSVipin KUMAR 
293031ed2faSVipin KUMAR 		} else if (get_timer(start_time_rx) > I2C_BYTE_TO) {
294031ed2faSVipin KUMAR 				return 1;
295031ed2faSVipin KUMAR 		}
296031ed2faSVipin KUMAR 	}
297031ed2faSVipin KUMAR 
298031ed2faSVipin KUMAR 	return i2c_xfer_finish();
299031ed2faSVipin KUMAR }
300031ed2faSVipin KUMAR 
301031ed2faSVipin KUMAR /*
302031ed2faSVipin KUMAR  * i2c_write - Write to i2c memory
303031ed2faSVipin KUMAR  * @chip:	target i2c address
304031ed2faSVipin KUMAR  * @addr:	address to read from
305031ed2faSVipin KUMAR  * @alen:
306031ed2faSVipin KUMAR  * @buffer:	buffer for read data
307031ed2faSVipin KUMAR  * @len:	no of bytes to be read
308031ed2faSVipin KUMAR  *
309031ed2faSVipin KUMAR  * Write to i2c memory.
310031ed2faSVipin KUMAR  */
311031ed2faSVipin KUMAR int i2c_write(uchar chip, uint addr, int alen, uchar *buffer, int len)
312031ed2faSVipin KUMAR {
313031ed2faSVipin KUMAR 	int nb = len;
314031ed2faSVipin KUMAR 	unsigned long start_time_tx;
315031ed2faSVipin KUMAR 
316031ed2faSVipin KUMAR 	if (check_params(addr, alen, buffer, len))
317031ed2faSVipin KUMAR 		return 1;
318031ed2faSVipin KUMAR 
319031ed2faSVipin KUMAR 	if (i2c_xfer_init(chip, addr))
320031ed2faSVipin KUMAR 		return 1;
321031ed2faSVipin KUMAR 
322031ed2faSVipin KUMAR 	start_time_tx = get_timer(0);
323031ed2faSVipin KUMAR 	while (len) {
324031ed2faSVipin KUMAR 		if (readl(&i2c_regs_p->ic_status) & IC_STATUS_TFNF) {
325031ed2faSVipin KUMAR 			writel(*buffer, &i2c_regs_p->ic_cmd_data);
326031ed2faSVipin KUMAR 			buffer++;
327031ed2faSVipin KUMAR 			len--;
328031ed2faSVipin KUMAR 			start_time_tx = get_timer(0);
329031ed2faSVipin KUMAR 
330031ed2faSVipin KUMAR 		} else if (get_timer(start_time_tx) > (nb * I2C_BYTE_TO)) {
331031ed2faSVipin KUMAR 				printf("Timed out. i2c write Failed\n");
332031ed2faSVipin KUMAR 				return 1;
333031ed2faSVipin KUMAR 		}
334031ed2faSVipin KUMAR 	}
335031ed2faSVipin KUMAR 
336031ed2faSVipin KUMAR 	return i2c_xfer_finish();
337031ed2faSVipin KUMAR }
338031ed2faSVipin KUMAR 
339031ed2faSVipin KUMAR /*
340031ed2faSVipin KUMAR  * i2c_probe - Probe the i2c chip
341031ed2faSVipin KUMAR  */
342031ed2faSVipin KUMAR int i2c_probe(uchar chip)
343031ed2faSVipin KUMAR {
344031ed2faSVipin KUMAR 	u32 tmp;
345496ba48fSStefan Roese 	int ret;
346031ed2faSVipin KUMAR 
347031ed2faSVipin KUMAR 	/*
348031ed2faSVipin KUMAR 	 * Try to read the first location of the chip.
349031ed2faSVipin KUMAR 	 */
350496ba48fSStefan Roese 	ret = i2c_read(chip, 0, 1, (uchar *)&tmp, 1);
351496ba48fSStefan Roese 	if (ret)
352496ba48fSStefan Roese 		i2c_init(CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE);
353496ba48fSStefan Roese 
354496ba48fSStefan Roese 	return ret;
355031ed2faSVipin KUMAR }
356*ac6e2fe6SArmando Visconti 
357*ac6e2fe6SArmando Visconti #ifdef CONFIG_I2C_MULTI_BUS
358*ac6e2fe6SArmando Visconti int i2c_set_bus_num(unsigned int bus)
359*ac6e2fe6SArmando Visconti {
360*ac6e2fe6SArmando Visconti 	switch (bus) {
361*ac6e2fe6SArmando Visconti 	case 0:
362*ac6e2fe6SArmando Visconti 		i2c_regs_p = (void *)CONFIG_SYS_I2C_BASE;
363*ac6e2fe6SArmando Visconti 		break;
364*ac6e2fe6SArmando Visconti #ifdef CONFIG_SYS_I2C_BASE1
365*ac6e2fe6SArmando Visconti 	case 1:
366*ac6e2fe6SArmando Visconti 		i2c_regs_p = (void *)CONFIG_SYS_I2C_BASE1;
367*ac6e2fe6SArmando Visconti 		break;
368*ac6e2fe6SArmando Visconti #endif
369*ac6e2fe6SArmando Visconti #ifdef CONFIG_SYS_I2C_BASE2
370*ac6e2fe6SArmando Visconti 	case 2:
371*ac6e2fe6SArmando Visconti 		i2c_regs_p = (void *)CONFIG_SYS_I2C_BASE2;
372*ac6e2fe6SArmando Visconti 		break;
373*ac6e2fe6SArmando Visconti #endif
374*ac6e2fe6SArmando Visconti #ifdef CONFIG_SYS_I2C_BASE3
375*ac6e2fe6SArmando Visconti 	case 3:
376*ac6e2fe6SArmando Visconti 		i2c_regs_p = (void *)CONFIG_SYS_I2C_BASE3;
377*ac6e2fe6SArmando Visconti 		break;
378*ac6e2fe6SArmando Visconti #endif
379*ac6e2fe6SArmando Visconti #ifdef CONFIG_SYS_I2C_BASE4
380*ac6e2fe6SArmando Visconti 	case 4:
381*ac6e2fe6SArmando Visconti 		i2c_regs_p = (void *)CONFIG_SYS_I2C_BASE4;
382*ac6e2fe6SArmando Visconti 		break;
383*ac6e2fe6SArmando Visconti #endif
384*ac6e2fe6SArmando Visconti #ifdef CONFIG_SYS_I2C_BASE5
385*ac6e2fe6SArmando Visconti 	case 5:
386*ac6e2fe6SArmando Visconti 		i2c_regs_p = (void *)CONFIG_SYS_I2C_BASE5;
387*ac6e2fe6SArmando Visconti 		break;
388*ac6e2fe6SArmando Visconti #endif
389*ac6e2fe6SArmando Visconti #ifdef CONFIG_SYS_I2C_BASE6
390*ac6e2fe6SArmando Visconti 	case 6:
391*ac6e2fe6SArmando Visconti 		i2c_regs_p = (void *)CONFIG_SYS_I2C_BASE6;
392*ac6e2fe6SArmando Visconti 		break;
393*ac6e2fe6SArmando Visconti #endif
394*ac6e2fe6SArmando Visconti #ifdef CONFIG_SYS_I2C_BASE7
395*ac6e2fe6SArmando Visconti 	case 7:
396*ac6e2fe6SArmando Visconti 		i2c_regs_p = (void *)CONFIG_SYS_I2C_BASE7;
397*ac6e2fe6SArmando Visconti 		break;
398*ac6e2fe6SArmando Visconti #endif
399*ac6e2fe6SArmando Visconti #ifdef CONFIG_SYS_I2C_BASE8
400*ac6e2fe6SArmando Visconti 	case 8:
401*ac6e2fe6SArmando Visconti 		i2c_regs_p = (void *)CONFIG_SYS_I2C_BASE8;
402*ac6e2fe6SArmando Visconti 		break;
403*ac6e2fe6SArmando Visconti #endif
404*ac6e2fe6SArmando Visconti #ifdef CONFIG_SYS_I2C_BASE9
405*ac6e2fe6SArmando Visconti 	case 9:
406*ac6e2fe6SArmando Visconti 		i2c_regs_p = (void *)CONFIG_SYS_I2C_BASE9;
407*ac6e2fe6SArmando Visconti 		break;
408*ac6e2fe6SArmando Visconti #endif
409*ac6e2fe6SArmando Visconti 	default:
410*ac6e2fe6SArmando Visconti 		printf("Bad bus: %d\n", bus);
411*ac6e2fe6SArmando Visconti 		return -1;
412*ac6e2fe6SArmando Visconti 	}
413*ac6e2fe6SArmando Visconti 
414*ac6e2fe6SArmando Visconti 	current_bus = bus;
415*ac6e2fe6SArmando Visconti 
416*ac6e2fe6SArmando Visconti 	if (!bus_initialized[current_bus])
417*ac6e2fe6SArmando Visconti 		i2c_init(CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE);
418*ac6e2fe6SArmando Visconti 
419*ac6e2fe6SArmando Visconti 	return 0;
420*ac6e2fe6SArmando Visconti }
421*ac6e2fe6SArmando Visconti 
422*ac6e2fe6SArmando Visconti int i2c_get_bus_num(void)
423*ac6e2fe6SArmando Visconti {
424*ac6e2fe6SArmando Visconti 	return current_bus;
425*ac6e2fe6SArmando Visconti }
426*ac6e2fe6SArmando Visconti #endif
427