1 // SPDX-License-Identifier: GPL-2.0+
4 * Arthur Li, Cortina Access, arthur.li@cortina-access.com.
13 #include "i2c-cortina.h"
15 static void set_speed(struct i2c_regs *regs, int i2c_spd)
17 union ca_biw_cfg i2c_cfg;
19 i2c_cfg.wrd = readl(®s->i2c_cfg);
20 i2c_cfg.bf.core_en = 0;
21 writel(i2c_cfg.wrd, ®s->i2c_cfg);
24 case IC_SPEED_MODE_FAST_PLUS:
25 i2c_cfg.bf.prer = CORTINA_PER_IO_FREQ /
26 (5 * I2C_SPEED_FAST_PLUS_RATE) - 1;
29 case IC_SPEED_MODE_STANDARD:
30 i2c_cfg.bf.prer = CORTINA_PER_IO_FREQ /
31 (5 * I2C_SPEED_STANDARD_RATE) - 1;
34 case IC_SPEED_MODE_FAST:
36 i2c_cfg.bf.prer = CORTINA_PER_IO_FREQ /
37 (5 * I2C_SPEED_FAST_RATE) - 1;
41 i2c_cfg.bf.core_en = 1;
42 writel(i2c_cfg.wrd, ®s->i2c_cfg);
45 static int ca_i2c_set_bus_speed(struct udevice *bus, unsigned int speed)
47 struct ca_i2c *priv = dev_get_priv(bus);
50 if (speed >= I2C_SPEED_FAST_PLUS_RATE) {
51 i2c_spd = IC_SPEED_MODE_FAST_PLUS;
52 priv->speed = I2C_SPEED_FAST_PLUS_RATE;
53 } else if (speed >= I2C_SPEED_FAST_RATE) {
54 i2c_spd = IC_SPEED_MODE_FAST;
55 priv->speed = I2C_SPEED_FAST_RATE;
57 i2c_spd = IC_SPEED_MODE_STANDARD;
58 priv->speed = I2C_SPEED_STANDARD_RATE;
61 set_speed(priv->regs, i2c_spd);
66 static int ca_i2c_get_bus_speed(struct udevice *bus)
68 struct ca_i2c *priv = dev_get_priv(bus);
73 static void ca_i2c_init(struct i2c_regs *regs)
75 union ca_biw_cfg i2c_cfg;
77 i2c_cfg.wrd = readl(®s->i2c_cfg);
78 i2c_cfg.bf.core_en = 0;
79 i2c_cfg.bf.biw_soft_reset = 1;
80 writel(i2c_cfg.wrd, ®s->i2c_cfg);
82 i2c_cfg.bf.biw_soft_reset = 0;
83 writel(i2c_cfg.wrd, ®s->i2c_cfg);
85 set_speed(regs, IC_SPEED_MODE_STANDARD);
87 i2c_cfg.wrd = readl(®s->i2c_cfg);
88 i2c_cfg.bf.core_en = 1;
89 writel(i2c_cfg.wrd, ®s->i2c_cfg);
92 static int i2c_wait_complete(struct i2c_regs *regs)
94 union ca_biw_ctrl i2c_ctrl;
95 unsigned long start_time_bb = get_timer(0);
97 i2c_ctrl.wrd = readl(®s->i2c_ctrl);
99 while (i2c_ctrl.bf.biwdone == 0) {
100 i2c_ctrl.wrd = readl(®s->i2c_ctrl);
102 if (get_timer(start_time_bb) >
103 (unsigned long)(I2C_BYTE_TO_BB)) {
104 printf("%s not done!!!\n", __func__);
110 writel(i2c_ctrl.wrd, ®s->i2c_ctrl);
115 static void i2c_setaddress(struct i2c_regs *regs, unsigned int i2c_addr,
118 writel(i2c_addr | write_read, ®s->i2c_txr);
120 writel(BIW_CTRL_START | BIW_CTRL_WRITE,
123 i2c_wait_complete(regs);
126 static int i2c_wait_for_bus_busy(struct i2c_regs *regs)
128 union ca_biw_ack i2c_ack;
129 unsigned long start_time_bb = get_timer(0);
131 i2c_ack.wrd = readl(®s->i2c_ack);
133 while (i2c_ack.bf.biw_busy) {
134 i2c_ack.wrd = readl(®s->i2c_ack);
136 if (get_timer(start_time_bb) >
137 (unsigned long)(I2C_BYTE_TO_BB)) {
138 printf("%s: timeout!\n", __func__);
146 static int i2c_xfer_init(struct i2c_regs *regs, uint8_t chip, uint addr,
147 int alen, int write_read)
151 if (i2c_wait_for_bus_busy(regs))
154 /* First cycle must write addr + offset */
155 chip = ((chip & 0x7F) << 1);
156 if (alen == 0 && write_read == I2C_CMD_RD)
157 i2c_setaddress(regs, chip, I2C_CMD_RD);
159 i2c_setaddress(regs, chip, I2C_CMD_WT);
163 writel(addr, ®s->i2c_txr);
164 if (write_read == I2C_CMD_RD)
165 writel(BIW_CTRL_WRITE | BIW_CTRL_STOP,
168 writel(BIW_CTRL_WRITE, ®s->i2c_ctrl);
169 i2c_wait_complete(regs);
172 /* Send address again with Read flag if it's read command */
173 if (write_read == I2C_CMD_RD && addr_len > 0)
174 i2c_setaddress(regs, chip, I2C_CMD_RD);
179 static int i2c_xfer_finish(struct i2c_regs *regs)
181 /* Dummy read makes bus free */
182 writel(BIW_CTRL_READ | BIW_CTRL_STOP, ®s->i2c_ctrl);
183 i2c_wait_complete(regs);
185 if (i2c_wait_for_bus_busy(regs)) {
186 printf("Timed out waiting for bus\n");
193 static int ca_i2c_read(struct i2c_regs *regs, uint8_t chip, uint addr,
194 int alen, uint8_t *buffer, int len)
196 unsigned long start_time_rx;
199 rc = i2c_xfer_init(regs, chip, addr, alen, I2C_CMD_RD);
203 start_time_rx = get_timer(0);
205 /* ACK_IN is ack value to send during read.
206 * ack high only on the very last byte!
209 writel(BIW_CTRL_READ | BIW_CTRL_ACK_IN | BIW_CTRL_STOP,
212 writel(BIW_CTRL_READ, ®s->i2c_ctrl);
214 rc = i2c_wait_complete(regs);
219 (uchar) readl(®s->i2c_rxr);
221 start_time_rx = get_timer(0);
223 } else if (get_timer(start_time_rx) > I2C_BYTE_TO) {
227 i2c_xfer_finish(regs);
231 static int ca_i2c_write(struct i2c_regs *regs, uint8_t chip, uint addr,
232 int alen, uint8_t *buffer, int len)
235 unsigned long start_time_tx;
237 rc = i2c_xfer_init(regs, chip, addr, alen, I2C_CMD_WT);
241 start_time_tx = get_timer(0);
243 writel(*buffer, ®s->i2c_txr);
245 writel(BIW_CTRL_WRITE | BIW_CTRL_STOP,
248 writel(BIW_CTRL_WRITE, ®s->i2c_ctrl);
250 rc = i2c_wait_complete(regs);
255 start_time_tx = get_timer(0);
256 } else if (get_timer(start_time_tx) > (nb * I2C_BYTE_TO)) {
264 static int ca_i2c_probe_chip(struct udevice *bus, uint chip_addr,
267 struct ca_i2c *priv = dev_get_priv(bus);
271 /* Try to read the first location of the chip */
272 ret = ca_i2c_read(priv->regs, chip_addr, 0, 1, (uchar *)&tmp, 1);
274 ca_i2c_init(priv->regs);
279 static int ca_i2c_xfer(struct udevice *bus, struct i2c_msg *msg, int nmsgs)
281 struct ca_i2c *priv = dev_get_priv(bus);
284 debug("i2c_xfer: %d messages\n", nmsgs);
285 for (; nmsgs > 0; nmsgs--, msg++) {
286 debug("i2c_xfer: chip=0x%x, len=0x%x\n", msg->addr, msg->len);
287 if (msg->flags & I2C_M_RD)
288 ret = ca_i2c_read(priv->regs, msg->addr, 0, 0,
291 ret = ca_i2c_write(priv->regs, msg->addr, 0, 0,
295 printf("i2c_xfer: %s error\n",
296 msg->flags & I2C_M_RD ? "read" : "write");
304 static const struct dm_i2c_ops ca_i2c_ops = {
306 .probe_chip = ca_i2c_probe_chip,
307 .set_bus_speed = ca_i2c_set_bus_speed,
308 .get_bus_speed = ca_i2c_get_bus_speed,
311 static const struct udevice_id ca_i2c_ids[] = {
312 { .compatible = "cortina,ca-i2c", },
316 static int ca_i2c_probe(struct udevice *bus)
318 struct ca_i2c *priv = dev_get_priv(bus);
320 ca_i2c_init(priv->regs);
325 static int ca_i2c_of_to_plat(struct udevice *bus)
327 struct ca_i2c *priv = dev_get_priv(bus);
329 priv->regs = map_sysmem(dev_read_addr(bus), sizeof(struct i2c_regs));
331 printf("I2C: base address is invalid\n");
338 U_BOOT_DRIVER(i2c_cortina) = {
339 .name = "i2c_cortina",
341 .of_match = ca_i2c_ids,
342 .of_to_plat = ca_i2c_of_to_plat,
343 .probe = ca_i2c_probe,
344 .priv_auto = sizeof(struct ca_i2c),
346 .flags = DM_FLAG_PRE_RELOC,