summaryrefslogtreecommitdiff
path: root/drivers/i2c/i2c-cortina.c
blob: 960ae8c700f979ff3838576190991b3763bc3ec0 (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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
// SPDX-License-Identifier: GPL-2.0+
/*
 * (C) Copyright 2020
 * Arthur Li, Cortina Access, arthur.li@cortina-access.com.
 */

#include <common.h>
#include <i2c.h>
#include <log.h>
#include <asm/io.h>
#include <dm.h>
#include <mapmem.h>
#include "i2c-cortina.h"

static void set_speed(struct i2c_regs *regs, int i2c_spd)
{
	union ca_biw_cfg i2c_cfg;

	i2c_cfg.wrd = readl(&regs->i2c_cfg);
	i2c_cfg.bf.core_en = 0;
	writel(i2c_cfg.wrd, &regs->i2c_cfg);

	switch (i2c_spd) {
	case IC_SPEED_MODE_FAST_PLUS:
		i2c_cfg.bf.prer = CORTINA_PER_IO_FREQ /
				  (5 * I2C_SPEED_FAST_PLUS_RATE) - 1;
		break;

	case IC_SPEED_MODE_STANDARD:
		i2c_cfg.bf.prer = CORTINA_PER_IO_FREQ /
				  (5 * I2C_SPEED_STANDARD_RATE) - 1;
		break;

	case IC_SPEED_MODE_FAST:
	default:
		i2c_cfg.bf.prer = CORTINA_PER_IO_FREQ /
				  (5 * I2C_SPEED_FAST_RATE) - 1;
		break;
	}

	i2c_cfg.bf.core_en = 1;
	writel(i2c_cfg.wrd, &regs->i2c_cfg);
}

static int ca_i2c_set_bus_speed(struct udevice *bus, unsigned int speed)
{
	struct ca_i2c *priv = dev_get_priv(bus);
	int i2c_spd;

	if (speed >= I2C_SPEED_FAST_PLUS_RATE) {
		i2c_spd = IC_SPEED_MODE_FAST_PLUS;
		priv->speed = I2C_SPEED_FAST_PLUS_RATE;
	} else if (speed >= I2C_SPEED_FAST_RATE) {
		i2c_spd = IC_SPEED_MODE_FAST;
		priv->speed = I2C_SPEED_FAST_RATE;
	} else {
		i2c_spd = IC_SPEED_MODE_STANDARD;
		priv->speed = I2C_SPEED_STANDARD_RATE;
	}

	set_speed(priv->regs, i2c_spd);

	return 0;
}

static int ca_i2c_get_bus_speed(struct udevice *bus)
{
	struct ca_i2c *priv = dev_get_priv(bus);

	return priv->speed;
}

static void ca_i2c_init(struct i2c_regs *regs)
{
	union ca_biw_cfg i2c_cfg;

	i2c_cfg.wrd = readl(&regs->i2c_cfg);
	i2c_cfg.bf.core_en = 0;
	i2c_cfg.bf.biw_soft_reset = 1;
	writel(i2c_cfg.wrd, &regs->i2c_cfg);
	mdelay(10);
	i2c_cfg.bf.biw_soft_reset = 0;
	writel(i2c_cfg.wrd, &regs->i2c_cfg);

	set_speed(regs, IC_SPEED_MODE_STANDARD);

	i2c_cfg.wrd = readl(&regs->i2c_cfg);
	i2c_cfg.bf.core_en = 1;
	writel(i2c_cfg.wrd, &regs->i2c_cfg);
}

static int i2c_wait_complete(struct i2c_regs *regs)
{
	union ca_biw_ctrl i2c_ctrl;
	unsigned long start_time_bb = get_timer(0);

	i2c_ctrl.wrd = readl(&regs->i2c_ctrl);

	while (i2c_ctrl.bf.biwdone == 0) {
		i2c_ctrl.wrd = readl(&regs->i2c_ctrl);

		if (get_timer(start_time_bb) >
		   (unsigned long)(I2C_BYTE_TO_BB)) {
			printf("%s not done!!!\n", __func__);
			return -ETIMEDOUT;
		}
	}

	/* Clear done bit */
	writel(i2c_ctrl.wrd, &regs->i2c_ctrl);

	return 0;
}

static void i2c_setaddress(struct i2c_regs *regs, unsigned int i2c_addr,
			   int write_read)
{
	writel(i2c_addr | write_read, &regs->i2c_txr);

	writel(BIW_CTRL_START | BIW_CTRL_WRITE,
	       &regs->i2c_ctrl);

	i2c_wait_complete(regs);
}

static int i2c_wait_for_bus_busy(struct i2c_regs *regs)
{
	union ca_biw_ack i2c_ack;
	unsigned long start_time_bb = get_timer(0);

	i2c_ack.wrd = readl(&regs->i2c_ack);

	while (i2c_ack.bf.biw_busy) {
		i2c_ack.wrd = readl(&regs->i2c_ack);

		if (get_timer(start_time_bb) >
		   (unsigned long)(I2C_BYTE_TO_BB)) {
			printf("%s: timeout!\n", __func__);
			return -ETIMEDOUT;
		}
	}

	return 0;
}

static int i2c_xfer_init(struct i2c_regs *regs, uint8_t chip, uint addr,
			 int alen, int write_read)
{
	int addr_len = alen;

	if (i2c_wait_for_bus_busy(regs))
		return 1;

	/* First cycle must write addr + offset */
	chip = ((chip & 0x7F) << 1);
	if (alen == 0 && write_read == I2C_CMD_RD)
		i2c_setaddress(regs, chip, I2C_CMD_RD);
	else
		i2c_setaddress(regs, chip, I2C_CMD_WT);

	while (alen) {
		alen--;
		writel(addr, &regs->i2c_txr);
		if (write_read == I2C_CMD_RD)
			writel(BIW_CTRL_WRITE | BIW_CTRL_STOP,
			       &regs->i2c_ctrl);
		else
			writel(BIW_CTRL_WRITE, &regs->i2c_ctrl);
		i2c_wait_complete(regs);
	}

	/* Send address again with Read flag if it's read command */
	if (write_read == I2C_CMD_RD && addr_len > 0)
		i2c_setaddress(regs, chip, I2C_CMD_RD);

	return 0;
}

static int i2c_xfer_finish(struct i2c_regs *regs)
{
	/* Dummy read makes bus free */
	writel(BIW_CTRL_READ | BIW_CTRL_STOP, &regs->i2c_ctrl);
	i2c_wait_complete(regs);

	if (i2c_wait_for_bus_busy(regs)) {
		printf("Timed out waiting for bus\n");
		return -ETIMEDOUT;
	}

	return 0;
}

static int ca_i2c_read(struct i2c_regs *regs, uint8_t chip, uint addr,
		       int alen, uint8_t *buffer, int len)
{
	unsigned long start_time_rx;
	int rc = 0;

	rc = i2c_xfer_init(regs, chip, addr, alen, I2C_CMD_RD);
	if (rc)
		return rc;

	start_time_rx = get_timer(0);
	while (len) {
		/* ACK_IN is ack value to send during read.
		 * ack high only on the very last byte!
		 */
		if (len == 1)
			writel(BIW_CTRL_READ | BIW_CTRL_ACK_IN | BIW_CTRL_STOP,
			       &regs->i2c_ctrl);
		else
			writel(BIW_CTRL_READ, &regs->i2c_ctrl);

		rc = i2c_wait_complete(regs);
		udelay(1);

		if (rc == 0) {
			*buffer++ =
				(uchar) readl(&regs->i2c_rxr);
			len--;
			start_time_rx = get_timer(0);

		} else if (get_timer(start_time_rx) > I2C_BYTE_TO) {
			return -ETIMEDOUT;
		}
	}
	i2c_xfer_finish(regs);
	return rc;
}

static int ca_i2c_write(struct i2c_regs *regs, uint8_t chip, uint addr,
			int alen, uint8_t *buffer, int len)
{
	int rc, nb = len;
	unsigned long start_time_tx;

	rc = i2c_xfer_init(regs, chip, addr, alen, I2C_CMD_WT);
	if (rc)
		return rc;

	start_time_tx = get_timer(0);
	while (len) {
		writel(*buffer, &regs->i2c_txr);
		if (len == 1)
			writel(BIW_CTRL_WRITE | BIW_CTRL_STOP,
			       &regs->i2c_ctrl);
		else
			writel(BIW_CTRL_WRITE, &regs->i2c_ctrl);

		rc = i2c_wait_complete(regs);

		if (rc == 0) {
			len--;
			buffer++;
			start_time_tx = get_timer(0);
		} else if (get_timer(start_time_tx) > (nb * I2C_BYTE_TO)) {
			return -ETIMEDOUT;
		}
	}

	return 0;
}

static int ca_i2c_probe_chip(struct udevice *bus, uint chip_addr,
			     uint chip_flags)
{
	struct ca_i2c *priv = dev_get_priv(bus);
	int ret;
	u32 tmp;

	/* Try to read the first location of the chip */
	ret = ca_i2c_read(priv->regs, chip_addr, 0, 1, (uchar *)&tmp, 1);
	if (ret)
		ca_i2c_init(priv->regs);

	return ret;
}

static int ca_i2c_xfer(struct udevice *bus, struct i2c_msg *msg, int nmsgs)
{
	struct ca_i2c *priv = dev_get_priv(bus);
	int ret;

	debug("i2c_xfer: %d messages\n", nmsgs);
	for (; nmsgs > 0; nmsgs--, msg++) {
		debug("i2c_xfer: chip=0x%x, len=0x%x\n", msg->addr, msg->len);
		if (msg->flags & I2C_M_RD)
			ret = ca_i2c_read(priv->regs, msg->addr, 0, 0,
					  msg->buf, msg->len);
		else
			ret = ca_i2c_write(priv->regs, msg->addr, 0, 0,
					   msg->buf, msg->len);

		if (ret) {
			printf("i2c_xfer: %s error\n",
			       msg->flags & I2C_M_RD ? "read" : "write");
			return ret;
		}
	}

	return 0;
}

static const struct dm_i2c_ops ca_i2c_ops = {
	.xfer		= ca_i2c_xfer,
	.probe_chip	= ca_i2c_probe_chip,
	.set_bus_speed	= ca_i2c_set_bus_speed,
	.get_bus_speed	= ca_i2c_get_bus_speed,
};

static const struct udevice_id ca_i2c_ids[] = {
	{ .compatible = "cortina,ca-i2c", },
	{ }
};

static int ca_i2c_probe(struct udevice *bus)
{
	struct ca_i2c *priv = dev_get_priv(bus);

	ca_i2c_init(priv->regs);

	return 0;
}

static int ca_i2c_of_to_plat(struct udevice *bus)
{
	struct ca_i2c *priv = dev_get_priv(bus);

	priv->regs = map_sysmem(dev_read_addr(bus), sizeof(struct i2c_regs));
	if (!priv->regs) {
		printf("I2C: base address is invalid\n");
		return -EINVAL;
	}

	return 0;
}

U_BOOT_DRIVER(i2c_cortina) = {
	.name	= "i2c_cortina",
	.id	= UCLASS_I2C,
	.of_match = ca_i2c_ids,
	.of_to_plat = ca_i2c_of_to_plat,
	.probe	= ca_i2c_probe,
	.priv_auto	= sizeof(struct ca_i2c),
	.ops	= &ca_i2c_ops,
	.flags  = DM_FLAG_PRE_RELOC,
};