2 * Faraday I2C Controller
4 * (C) Copyright 2010 Faraday Technology
5 * Dante Su <dantesu@faraday-tech.com>
7 * SPDX-License-Identifier: GPL-2.0+
16 #ifndef CONFIG_SYS_I2C_SPEED
17 #define CONFIG_SYS_I2C_SPEED 5000
20 #ifndef CONFIG_SYS_I2C_SLAVE
21 #define CONFIG_SYS_I2C_SLAVE 0
24 #ifndef CONFIG_FTI2C010_CLOCK
25 #define CONFIG_FTI2C010_CLOCK clk_get_rate("I2C")
28 #ifndef CONFIG_FTI2C010_TIMEOUT
29 #define CONFIG_FTI2C010_TIMEOUT 10 /* ms */
32 /* 7-bit dev address + 1-bit read/write */
33 #define I2C_RD(dev) ((((dev) << 1) & 0xfe) | 1)
34 #define I2C_WR(dev) (((dev) << 1) & 0xfe)
36 struct fti2c010_chip {
37 struct fti2c010_regs *regs;
40 static struct fti2c010_chip chip_list[] = {
42 .regs = (struct fti2c010_regs *)CONFIG_FTI2C010_BASE,
44 #ifdef CONFIG_FTI2C010_BASE1
46 .regs = (struct fti2c010_regs *)CONFIG_FTI2C010_BASE1,
49 #ifdef CONFIG_FTI2C010_BASE2
51 .regs = (struct fti2c010_regs *)CONFIG_FTI2C010_BASE2,
54 #ifdef CONFIG_FTI2C010_BASE3
56 .regs = (struct fti2c010_regs *)CONFIG_FTI2C010_BASE3,
61 static int fti2c010_reset(struct fti2c010_chip *chip)
65 struct fti2c010_regs *regs = chip->regs;
67 writel(CR_I2CRST, ®s->cr);
68 for (ts = get_timer(0); get_timer(ts) < CONFIG_FTI2C010_TIMEOUT; ) {
69 if (!(readl(®s->cr) & CR_I2CRST)) {
76 printf("fti2c010: reset timeout\n");
81 static int fti2c010_wait(struct fti2c010_chip *chip, uint32_t mask)
85 struct fti2c010_regs *regs = chip->regs;
87 for (ts = get_timer(0); get_timer(ts) < CONFIG_FTI2C010_TIMEOUT; ) {
88 stat = readl(®s->sr);
89 if ((stat & mask) == mask) {
98 static unsigned int set_i2c_bus_speed(struct fti2c010_chip *chip,
101 struct fti2c010_regs *regs = chip->regs;
102 unsigned int clk = CONFIG_FTI2C010_CLOCK;
103 unsigned int gsr = 0;
104 unsigned int tsr = 32;
105 unsigned int div, rate;
107 for (div = 0; div < 0x3ffff; ++div) {
108 /* SCLout = PCLK/(2*(COUNT + 2) + GSR) */
109 rate = clk / (2 * (div + 2) + gsr);
114 writel(TGSR_GSR(gsr) | TGSR_TSR(tsr), ®s->tgsr);
115 writel(CDR_DIV(div), ®s->cdr);
121 * Initialization, must be called once on start up, may be called
122 * repeatedly to change the speed and slave addresses.
124 static void fti2c010_init(struct i2c_adapter *adap, int speed, int slaveaddr)
126 struct fti2c010_chip *chip = chip_list + adap->hwadapnr;
131 #ifdef CONFIG_SYS_I2C_INIT_BOARD
132 /* Call board specific i2c bus reset routine before accessing the
133 * environment, which might be in a chip on that bus. For details
134 * about this problem see doc/I2C_Edge_Conditions.
141 fti2c010_reset(chip);
143 set_i2c_bus_speed(chip, speed);
145 /* slave init, don't care */
147 #ifdef CONFIG_SYS_I2C_BOARD_LATE_INIT
148 /* Call board specific i2c bus reset routine AFTER the bus has been
149 * initialized. Use either this callpoint or i2c_init_board;
150 * which is called before fti2c010_init operations.
151 * For details about this problem see doc/I2C_Edge_Conditions.
153 i2c_board_late_init();
158 * Probe the given I2C chip address. Returns 0 if a chip responded,
161 static int fti2c010_probe(struct i2c_adapter *adap, u8 dev)
163 struct fti2c010_chip *chip = chip_list + adap->hwadapnr;
164 struct fti2c010_regs *regs = chip->regs;
167 /* 1. Select slave device (7bits Address + 1bit R/W) */
168 writel(I2C_WR(dev), ®s->dr);
169 writel(CR_ENABLE | CR_TBEN | CR_START, ®s->cr);
170 ret = fti2c010_wait(chip, SR_DT);
174 /* 2. Select device register */
175 writel(0, ®s->dr);
176 writel(CR_ENABLE | CR_TBEN, ®s->cr);
177 ret = fti2c010_wait(chip, SR_DT);
182 static void to_i2c_addr(u8 *buf, uint32_t addr, int alen)
186 if (!buf || alen <= 0)
191 shift = (alen - 1) * 8;
193 buf[i] = (u8)(addr >> shift);
198 static int fti2c010_read(struct i2c_adapter *adap,
199 u8 dev, uint addr, int alen, uchar *buf, int len)
201 struct fti2c010_chip *chip = chip_list + adap->hwadapnr;
202 struct fti2c010_regs *regs = chip->regs;
204 uchar paddr[4] = { 0 };
206 to_i2c_addr(paddr, addr, alen);
209 * Phase A. Set register address
212 /* A.1 Select slave device (7bits Address + 1bit R/W) */
213 writel(I2C_WR(dev), ®s->dr);
214 writel(CR_ENABLE | CR_TBEN | CR_START, ®s->cr);
215 ret = fti2c010_wait(chip, SR_DT);
219 /* A.2 Select device register */
220 for (pos = 0; pos < alen; ++pos) {
221 uint32_t ctrl = CR_ENABLE | CR_TBEN;
223 writel(paddr[pos], ®s->dr);
224 writel(ctrl, ®s->cr);
225 ret = fti2c010_wait(chip, SR_DT);
231 * Phase B. Get register data
234 /* B.1 Select slave device (7bits Address + 1bit R/W) */
235 writel(I2C_RD(dev), ®s->dr);
236 writel(CR_ENABLE | CR_TBEN | CR_START, ®s->cr);
237 ret = fti2c010_wait(chip, SR_DT);
241 /* B.2 Get register data */
242 for (pos = 0; pos < len; ++pos) {
243 uint32_t ctrl = CR_ENABLE | CR_TBEN;
244 uint32_t stat = SR_DR;
246 if (pos == len - 1) {
247 ctrl |= CR_NAK | CR_STOP;
250 writel(ctrl, ®s->cr);
251 ret = fti2c010_wait(chip, stat);
254 buf[pos] = (uchar)(readl(®s->dr) & 0xFF);
260 static int fti2c010_write(struct i2c_adapter *adap,
261 u8 dev, uint addr, int alen, u8 *buf, int len)
263 struct fti2c010_chip *chip = chip_list + adap->hwadapnr;
264 struct fti2c010_regs *regs = chip->regs;
266 uchar paddr[4] = { 0 };
268 to_i2c_addr(paddr, addr, alen);
271 * Phase A. Set register address
273 * A.1 Select slave device (7bits Address + 1bit R/W)
275 writel(I2C_WR(dev), ®s->dr);
276 writel(CR_ENABLE | CR_TBEN | CR_START, ®s->cr);
277 ret = fti2c010_wait(chip, SR_DT);
281 /* A.2 Select device register */
282 for (pos = 0; pos < alen; ++pos) {
283 uint32_t ctrl = CR_ENABLE | CR_TBEN;
285 writel(paddr[pos], ®s->dr);
286 writel(ctrl, ®s->cr);
287 ret = fti2c010_wait(chip, SR_DT);
293 * Phase B. Set register data
295 for (pos = 0; pos < len; ++pos) {
296 uint32_t ctrl = CR_ENABLE | CR_TBEN;
300 writel(buf[pos], ®s->dr);
301 writel(ctrl, ®s->cr);
302 ret = fti2c010_wait(chip, SR_DT);
310 static unsigned int fti2c010_set_bus_speed(struct i2c_adapter *adap,
313 struct fti2c010_chip *chip = chip_list + adap->hwadapnr;
316 fti2c010_reset(chip);
317 ret = set_i2c_bus_speed(chip, speed);
323 * Register i2c adapters
325 U_BOOT_I2C_ADAP_COMPLETE(i2c_0, fti2c010_init, fti2c010_probe, fti2c010_read,
326 fti2c010_write, fti2c010_set_bus_speed,
327 CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE,
329 #ifdef CONFIG_FTI2C010_BASE1
330 U_BOOT_I2C_ADAP_COMPLETE(i2c_1, fti2c010_init, fti2c010_probe, fti2c010_read,
331 fti2c010_write, fti2c010_set_bus_speed,
332 CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE,
335 #ifdef CONFIG_FTI2C010_BASE2
336 U_BOOT_I2C_ADAP_COMPLETE(i2c_2, fti2c010_init, fti2c010_probe, fti2c010_read,
337 fti2c010_write, fti2c010_set_bus_speed,
338 CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE,
341 #ifdef CONFIG_FTI2C010_BASE3
342 U_BOOT_I2C_ADAP_COMPLETE(i2c_3, fti2c010_init, fti2c010_probe, fti2c010_read,
343 fti2c010_write, fti2c010_set_bus_speed,
344 CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE,