• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * i2c-ocores.c: I2C bus driver for OpenCores I2C controller
4  * (https://opencores.org/project/i2c/overview)
5  *
6  * Peter Korsgaard <peter@korsgaard.com>
7  *
8  * Support for the GRLIB port of the controller by
9  * Andreas Larsson <andreas@gaisler.com>
10  */
11 
12 #include <linux/clk.h>
13 #include <linux/delay.h>
14 #include <linux/err.h>
15 #include <linux/kernel.h>
16 #include <linux/module.h>
17 #include <linux/errno.h>
18 #include <linux/platform_device.h>
19 #include <linux/i2c.h>
20 #include <linux/interrupt.h>
21 #include <linux/wait.h>
22 #include <linux/platform_data/i2c-ocores.h>
23 #include <linux/slab.h>
24 #include <linux/io.h>
25 #include <linux/log2.h>
26 #include <linux/spinlock.h>
27 #include <linux/jiffies.h>
28 
29 /*
30  * 'process_lock' exists because ocores_process() and ocores_process_timeout()
31  * can't run in parallel.
32  */
33 struct ocores_i2c {
34 	void __iomem *base;
35 	int iobase;
36 	u32 reg_shift;
37 	u32 reg_io_width;
38 	unsigned long flags;
39 	wait_queue_head_t wait;
40 	struct i2c_adapter adap;
41 	struct i2c_msg *msg;
42 	int pos;
43 	int nmsgs;
44 	int state; /* see STATE_ */
45 	spinlock_t process_lock;
46 	struct clk *clk;
47 	int ip_clock_khz;
48 	int bus_clock_khz;
49 	void (*setreg)(struct ocores_i2c *i2c, int reg, u8 value);
50 	u8 (*getreg)(struct ocores_i2c *i2c, int reg);
51 };
52 
53 /* registers */
54 #define OCI2C_PRELOW		0
55 #define OCI2C_PREHIGH		1
56 #define OCI2C_CONTROL		2
57 #define OCI2C_DATA		3
58 #define OCI2C_CMD		4 /* write only */
59 #define OCI2C_STATUS		4 /* read only, same address as OCI2C_CMD */
60 
61 #define OCI2C_CTRL_IEN		0x40
62 #define OCI2C_CTRL_EN		0x80
63 
64 #define OCI2C_CMD_START		0x91
65 #define OCI2C_CMD_STOP		0x41
66 #define OCI2C_CMD_READ		0x21
67 #define OCI2C_CMD_WRITE		0x11
68 #define OCI2C_CMD_READ_ACK	0x21
69 #define OCI2C_CMD_READ_NACK	0x29
70 #define OCI2C_CMD_IACK		0x01
71 
72 #define OCI2C_STAT_IF		0x01
73 #define OCI2C_STAT_TIP		0x02
74 #define OCI2C_STAT_ARBLOST	0x20
75 #define OCI2C_STAT_BUSY		0x40
76 #define OCI2C_STAT_NACK		0x80
77 
78 #define STATE_DONE		0
79 #define STATE_START		1
80 #define STATE_WRITE		2
81 #define STATE_READ		3
82 #define STATE_ERROR		4
83 
84 #define TYPE_OCORES		0
85 #define TYPE_GRLIB		1
86 #define TYPE_SIFIVE_REV0	2
87 
88 #define OCORES_FLAG_BROKEN_IRQ BIT(1) /* Broken IRQ for FU540-C000 SoC */
89 
oc_setreg_8(struct ocores_i2c * i2c,int reg,u8 value)90 static void oc_setreg_8(struct ocores_i2c *i2c, int reg, u8 value)
91 {
92 	iowrite8(value, i2c->base + (reg << i2c->reg_shift));
93 }
94 
oc_setreg_16(struct ocores_i2c * i2c,int reg,u8 value)95 static void oc_setreg_16(struct ocores_i2c *i2c, int reg, u8 value)
96 {
97 	iowrite16(value, i2c->base + (reg << i2c->reg_shift));
98 }
99 
oc_setreg_32(struct ocores_i2c * i2c,int reg,u8 value)100 static void oc_setreg_32(struct ocores_i2c *i2c, int reg, u8 value)
101 {
102 	iowrite32(value, i2c->base + (reg << i2c->reg_shift));
103 }
104 
oc_setreg_16be(struct ocores_i2c * i2c,int reg,u8 value)105 static void oc_setreg_16be(struct ocores_i2c *i2c, int reg, u8 value)
106 {
107 	iowrite16be(value, i2c->base + (reg << i2c->reg_shift));
108 }
109 
oc_setreg_32be(struct ocores_i2c * i2c,int reg,u8 value)110 static void oc_setreg_32be(struct ocores_i2c *i2c, int reg, u8 value)
111 {
112 	iowrite32be(value, i2c->base + (reg << i2c->reg_shift));
113 }
114 
oc_getreg_8(struct ocores_i2c * i2c,int reg)115 static inline u8 oc_getreg_8(struct ocores_i2c *i2c, int reg)
116 {
117 	return ioread8(i2c->base + (reg << i2c->reg_shift));
118 }
119 
oc_getreg_16(struct ocores_i2c * i2c,int reg)120 static inline u8 oc_getreg_16(struct ocores_i2c *i2c, int reg)
121 {
122 	return ioread16(i2c->base + (reg << i2c->reg_shift));
123 }
124 
oc_getreg_32(struct ocores_i2c * i2c,int reg)125 static inline u8 oc_getreg_32(struct ocores_i2c *i2c, int reg)
126 {
127 	return ioread32(i2c->base + (reg << i2c->reg_shift));
128 }
129 
oc_getreg_16be(struct ocores_i2c * i2c,int reg)130 static inline u8 oc_getreg_16be(struct ocores_i2c *i2c, int reg)
131 {
132 	return ioread16be(i2c->base + (reg << i2c->reg_shift));
133 }
134 
oc_getreg_32be(struct ocores_i2c * i2c,int reg)135 static inline u8 oc_getreg_32be(struct ocores_i2c *i2c, int reg)
136 {
137 	return ioread32be(i2c->base + (reg << i2c->reg_shift));
138 }
139 
oc_setreg_io_8(struct ocores_i2c * i2c,int reg,u8 value)140 static void oc_setreg_io_8(struct ocores_i2c *i2c, int reg, u8 value)
141 {
142 	outb(value, i2c->iobase + reg);
143 }
144 
oc_getreg_io_8(struct ocores_i2c * i2c,int reg)145 static inline u8 oc_getreg_io_8(struct ocores_i2c *i2c, int reg)
146 {
147 	return inb(i2c->iobase + reg);
148 }
149 
oc_setreg(struct ocores_i2c * i2c,int reg,u8 value)150 static inline void oc_setreg(struct ocores_i2c *i2c, int reg, u8 value)
151 {
152 	i2c->setreg(i2c, reg, value);
153 }
154 
oc_getreg(struct ocores_i2c * i2c,int reg)155 static inline u8 oc_getreg(struct ocores_i2c *i2c, int reg)
156 {
157 	return i2c->getreg(i2c, reg);
158 }
159 
ocores_process(struct ocores_i2c * i2c,u8 stat)160 static void ocores_process(struct ocores_i2c *i2c, u8 stat)
161 {
162 	struct i2c_msg *msg = i2c->msg;
163 	unsigned long flags;
164 
165 	/*
166 	 * If we spin here is because we are in timeout, so we are going
167 	 * to be in STATE_ERROR. See ocores_process_timeout()
168 	 */
169 	spin_lock_irqsave(&i2c->process_lock, flags);
170 
171 	if ((i2c->state == STATE_DONE) || (i2c->state == STATE_ERROR)) {
172 		/* stop has been sent */
173 		oc_setreg(i2c, OCI2C_CMD, OCI2C_CMD_IACK);
174 		wake_up(&i2c->wait);
175 		goto out;
176 	}
177 
178 	/* error? */
179 	if (stat & OCI2C_STAT_ARBLOST) {
180 		i2c->state = STATE_ERROR;
181 		oc_setreg(i2c, OCI2C_CMD, OCI2C_CMD_STOP);
182 		goto out;
183 	}
184 
185 	if ((i2c->state == STATE_START) || (i2c->state == STATE_WRITE)) {
186 		i2c->state =
187 			(msg->flags & I2C_M_RD) ? STATE_READ : STATE_WRITE;
188 
189 		if (stat & OCI2C_STAT_NACK) {
190 			i2c->state = STATE_ERROR;
191 			oc_setreg(i2c, OCI2C_CMD, OCI2C_CMD_STOP);
192 			goto out;
193 		}
194 	} else {
195 		msg->buf[i2c->pos++] = oc_getreg(i2c, OCI2C_DATA);
196 	}
197 
198 	/* end of msg? */
199 	if (i2c->pos == msg->len) {
200 		i2c->nmsgs--;
201 		i2c->msg++;
202 		i2c->pos = 0;
203 		msg = i2c->msg;
204 
205 		if (i2c->nmsgs) {	/* end? */
206 			/* send start? */
207 			if (!(msg->flags & I2C_M_NOSTART)) {
208 				u8 addr = i2c_8bit_addr_from_msg(msg);
209 
210 				i2c->state = STATE_START;
211 
212 				oc_setreg(i2c, OCI2C_DATA, addr);
213 				oc_setreg(i2c, OCI2C_CMD, OCI2C_CMD_START);
214 				goto out;
215 			}
216 			i2c->state = (msg->flags & I2C_M_RD)
217 				? STATE_READ : STATE_WRITE;
218 		} else {
219 			i2c->state = STATE_DONE;
220 			oc_setreg(i2c, OCI2C_CMD, OCI2C_CMD_STOP);
221 			goto out;
222 		}
223 	}
224 
225 	if (i2c->state == STATE_READ) {
226 		oc_setreg(i2c, OCI2C_CMD, i2c->pos == (msg->len-1) ?
227 			  OCI2C_CMD_READ_NACK : OCI2C_CMD_READ_ACK);
228 	} else {
229 		oc_setreg(i2c, OCI2C_DATA, msg->buf[i2c->pos++]);
230 		oc_setreg(i2c, OCI2C_CMD, OCI2C_CMD_WRITE);
231 	}
232 
233 out:
234 	spin_unlock_irqrestore(&i2c->process_lock, flags);
235 }
236 
ocores_isr(int irq,void * dev_id)237 static irqreturn_t ocores_isr(int irq, void *dev_id)
238 {
239 	struct ocores_i2c *i2c = dev_id;
240 	u8 stat = oc_getreg(i2c, OCI2C_STATUS);
241 
242 	if (i2c->flags & OCORES_FLAG_BROKEN_IRQ) {
243 		if ((stat & OCI2C_STAT_IF) && !(stat & OCI2C_STAT_BUSY))
244 			return IRQ_NONE;
245 	} else if (!(stat & OCI2C_STAT_IF)) {
246 		return IRQ_NONE;
247 	}
248 	ocores_process(i2c, stat);
249 
250 	return IRQ_HANDLED;
251 }
252 
253 /**
254  * Process timeout event
255  * @i2c: ocores I2C device instance
256  */
ocores_process_timeout(struct ocores_i2c * i2c)257 static void ocores_process_timeout(struct ocores_i2c *i2c)
258 {
259 	unsigned long flags;
260 
261 	spin_lock_irqsave(&i2c->process_lock, flags);
262 	i2c->state = STATE_ERROR;
263 	oc_setreg(i2c, OCI2C_CMD, OCI2C_CMD_STOP);
264 	spin_unlock_irqrestore(&i2c->process_lock, flags);
265 }
266 
267 /**
268  * Wait until something change in a given register
269  * @i2c: ocores I2C device instance
270  * @reg: register to query
271  * @mask: bitmask to apply on register value
272  * @val: expected result
273  * @timeout: timeout in jiffies
274  *
275  * Timeout is necessary to avoid to stay here forever when the chip
276  * does not answer correctly.
277  *
278  * Return: 0 on success, -ETIMEDOUT on timeout
279  */
ocores_wait(struct ocores_i2c * i2c,int reg,u8 mask,u8 val,const unsigned long timeout)280 static int ocores_wait(struct ocores_i2c *i2c,
281 		       int reg, u8 mask, u8 val,
282 		       const unsigned long timeout)
283 {
284 	unsigned long j;
285 
286 	j = jiffies + timeout;
287 	while (1) {
288 		u8 status = oc_getreg(i2c, reg);
289 
290 		if ((status & mask) == val)
291 			break;
292 
293 		if (time_after(jiffies, j))
294 			return -ETIMEDOUT;
295 	}
296 	return 0;
297 }
298 
299 /**
300  * Wait until is possible to process some data
301  * @i2c: ocores I2C device instance
302  *
303  * Used when the device is in polling mode (interrupts disabled).
304  *
305  * Return: 0 on success, -ETIMEDOUT on timeout
306  */
ocores_poll_wait(struct ocores_i2c * i2c)307 static int ocores_poll_wait(struct ocores_i2c *i2c)
308 {
309 	u8 mask;
310 	int err;
311 
312 	if (i2c->state == STATE_DONE || i2c->state == STATE_ERROR) {
313 		/* transfer is over */
314 		mask = OCI2C_STAT_BUSY;
315 	} else {
316 		/* on going transfer */
317 		mask = OCI2C_STAT_TIP;
318 		/*
319 		 * We wait for the data to be transferred (8bit),
320 		 * then we start polling on the ACK/NACK bit
321 		 */
322 		udelay((8 * 1000) / i2c->bus_clock_khz);
323 	}
324 
325 	/*
326 	 * once we are here we expect to get the expected result immediately
327 	 * so if after 1ms we timeout then something is broken.
328 	 */
329 	err = ocores_wait(i2c, OCI2C_STATUS, mask, 0, msecs_to_jiffies(1));
330 	if (err)
331 		dev_warn(i2c->adap.dev.parent,
332 			 "%s: STATUS timeout, bit 0x%x did not clear in 1ms\n",
333 			 __func__, mask);
334 	return err;
335 }
336 
337 /**
338  * It handles an IRQ-less transfer
339  * @i2c: ocores I2C device instance
340  *
341  * Even if IRQ are disabled, the I2C OpenCore IP behavior is exactly the same
342  * (only that IRQ are not produced). This means that we can re-use entirely
343  * ocores_isr(), we just add our polling code around it.
344  *
345  * It can run in atomic context
346  *
347  * Return: 0 on success, -ETIMEDOUT on timeout
348  */
ocores_process_polling(struct ocores_i2c * i2c)349 static int ocores_process_polling(struct ocores_i2c *i2c)
350 {
351 	irqreturn_t ret;
352 	int err = 0;
353 
354 	while (1) {
355 		err = ocores_poll_wait(i2c);
356 		if (err)
357 			break; /* timeout */
358 
359 		ret = ocores_isr(-1, i2c);
360 		if (ret == IRQ_NONE)
361 			break; /* all messages have been transferred */
362 		else {
363 			if (i2c->flags & OCORES_FLAG_BROKEN_IRQ)
364 				if (i2c->state == STATE_DONE)
365 					break;
366 		}
367 	}
368 
369 	return err;
370 }
371 
ocores_xfer_core(struct ocores_i2c * i2c,struct i2c_msg * msgs,int num,bool polling)372 static int ocores_xfer_core(struct ocores_i2c *i2c,
373 			    struct i2c_msg *msgs, int num,
374 			    bool polling)
375 {
376 	int ret = 0;
377 	u8 ctrl;
378 
379 	ctrl = oc_getreg(i2c, OCI2C_CONTROL);
380 	if (polling)
381 		oc_setreg(i2c, OCI2C_CONTROL, ctrl & ~OCI2C_CTRL_IEN);
382 	else
383 		oc_setreg(i2c, OCI2C_CONTROL, ctrl | OCI2C_CTRL_IEN);
384 
385 	i2c->msg = msgs;
386 	i2c->pos = 0;
387 	i2c->nmsgs = num;
388 	i2c->state = STATE_START;
389 
390 	oc_setreg(i2c, OCI2C_DATA, i2c_8bit_addr_from_msg(i2c->msg));
391 	oc_setreg(i2c, OCI2C_CMD, OCI2C_CMD_START);
392 
393 	if (polling) {
394 		ret = ocores_process_polling(i2c);
395 	} else {
396 		if (wait_event_timeout(i2c->wait,
397 				       (i2c->state == STATE_ERROR) ||
398 				       (i2c->state == STATE_DONE), HZ) == 0)
399 			ret = -ETIMEDOUT;
400 	}
401 	if (ret) {
402 		ocores_process_timeout(i2c);
403 		return ret;
404 	}
405 
406 	return (i2c->state == STATE_DONE) ? num : -EIO;
407 }
408 
ocores_xfer_polling(struct i2c_adapter * adap,struct i2c_msg * msgs,int num)409 static int ocores_xfer_polling(struct i2c_adapter *adap,
410 			       struct i2c_msg *msgs, int num)
411 {
412 	return ocores_xfer_core(i2c_get_adapdata(adap), msgs, num, true);
413 }
414 
ocores_xfer(struct i2c_adapter * adap,struct i2c_msg * msgs,int num)415 static int ocores_xfer(struct i2c_adapter *adap,
416 		       struct i2c_msg *msgs, int num)
417 {
418 	return ocores_xfer_core(i2c_get_adapdata(adap), msgs, num, false);
419 }
420 
ocores_init(struct device * dev,struct ocores_i2c * i2c)421 static int ocores_init(struct device *dev, struct ocores_i2c *i2c)
422 {
423 	int prescale;
424 	int diff;
425 	u8 ctrl = oc_getreg(i2c, OCI2C_CONTROL);
426 
427 	/* make sure the device is disabled */
428 	ctrl &= ~(OCI2C_CTRL_EN | OCI2C_CTRL_IEN);
429 	oc_setreg(i2c, OCI2C_CONTROL, ctrl);
430 
431 	prescale = (i2c->ip_clock_khz / (5 * i2c->bus_clock_khz)) - 1;
432 	prescale = clamp(prescale, 0, 0xffff);
433 
434 	diff = i2c->ip_clock_khz / (5 * (prescale + 1)) - i2c->bus_clock_khz;
435 	if (abs(diff) > i2c->bus_clock_khz / 10) {
436 		dev_err(dev,
437 			"Unsupported clock settings: core: %d KHz, bus: %d KHz\n",
438 			i2c->ip_clock_khz, i2c->bus_clock_khz);
439 		return -EINVAL;
440 	}
441 
442 	oc_setreg(i2c, OCI2C_PRELOW, prescale & 0xff);
443 	oc_setreg(i2c, OCI2C_PREHIGH, prescale >> 8);
444 
445 	/* Init the device */
446 	oc_setreg(i2c, OCI2C_CMD, OCI2C_CMD_IACK);
447 	oc_setreg(i2c, OCI2C_CONTROL, ctrl | OCI2C_CTRL_EN);
448 
449 	return 0;
450 }
451 
452 
ocores_func(struct i2c_adapter * adap)453 static u32 ocores_func(struct i2c_adapter *adap)
454 {
455 	return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
456 }
457 
458 static struct i2c_algorithm ocores_algorithm = {
459 	.master_xfer = ocores_xfer,
460 	.master_xfer_atomic = ocores_xfer_polling,
461 	.functionality = ocores_func,
462 };
463 
464 static const struct i2c_adapter ocores_adapter = {
465 	.owner = THIS_MODULE,
466 	.name = "i2c-ocores",
467 	.class = I2C_CLASS_DEPRECATED,
468 	.algo = &ocores_algorithm,
469 };
470 
471 static const struct of_device_id ocores_i2c_match[] = {
472 	{
473 		.compatible = "opencores,i2c-ocores",
474 		.data = (void *)TYPE_OCORES,
475 	},
476 	{
477 		.compatible = "aeroflexgaisler,i2cmst",
478 		.data = (void *)TYPE_GRLIB,
479 	},
480 	{
481 		.compatible = "sifive,fu540-c000-i2c",
482 		.data = (void *)TYPE_SIFIVE_REV0,
483 	},
484 	{
485 		.compatible = "sifive,i2c0",
486 		.data = (void *)TYPE_SIFIVE_REV0,
487 	},
488 	{},
489 };
490 MODULE_DEVICE_TABLE(of, ocores_i2c_match);
491 
492 #ifdef CONFIG_OF
493 /*
494  * Read and write functions for the GRLIB port of the controller. Registers are
495  * 32-bit big endian and the PRELOW and PREHIGH registers are merged into one
496  * register. The subsequent registers have their offsets decreased accordingly.
497  */
oc_getreg_grlib(struct ocores_i2c * i2c,int reg)498 static u8 oc_getreg_grlib(struct ocores_i2c *i2c, int reg)
499 {
500 	u32 rd;
501 	int rreg = reg;
502 
503 	if (reg != OCI2C_PRELOW)
504 		rreg--;
505 	rd = ioread32be(i2c->base + (rreg << i2c->reg_shift));
506 	if (reg == OCI2C_PREHIGH)
507 		return (u8)(rd >> 8);
508 	else
509 		return (u8)rd;
510 }
511 
oc_setreg_grlib(struct ocores_i2c * i2c,int reg,u8 value)512 static void oc_setreg_grlib(struct ocores_i2c *i2c, int reg, u8 value)
513 {
514 	u32 curr, wr;
515 	int rreg = reg;
516 
517 	if (reg != OCI2C_PRELOW)
518 		rreg--;
519 	if (reg == OCI2C_PRELOW || reg == OCI2C_PREHIGH) {
520 		curr = ioread32be(i2c->base + (rreg << i2c->reg_shift));
521 		if (reg == OCI2C_PRELOW)
522 			wr = (curr & 0xff00) | value;
523 		else
524 			wr = (((u32)value) << 8) | (curr & 0xff);
525 	} else {
526 		wr = value;
527 	}
528 	iowrite32be(wr, i2c->base + (rreg << i2c->reg_shift));
529 }
530 
ocores_i2c_of_probe(struct platform_device * pdev,struct ocores_i2c * i2c)531 static int ocores_i2c_of_probe(struct platform_device *pdev,
532 				struct ocores_i2c *i2c)
533 {
534 	struct device_node *np = pdev->dev.of_node;
535 	const struct of_device_id *match;
536 	u32 val;
537 	u32 clock_frequency;
538 	bool clock_frequency_present;
539 
540 	if (of_property_read_u32(np, "reg-shift", &i2c->reg_shift)) {
541 		/* no 'reg-shift', check for deprecated 'regstep' */
542 		if (!of_property_read_u32(np, "regstep", &val)) {
543 			if (!is_power_of_2(val)) {
544 				dev_err(&pdev->dev, "invalid regstep %d\n",
545 					val);
546 				return -EINVAL;
547 			}
548 			i2c->reg_shift = ilog2(val);
549 			dev_warn(&pdev->dev,
550 				"regstep property deprecated, use reg-shift\n");
551 		}
552 	}
553 
554 	clock_frequency_present = !of_property_read_u32(np, "clock-frequency",
555 							&clock_frequency);
556 	i2c->bus_clock_khz = 100;
557 
558 	i2c->clk = devm_clk_get(&pdev->dev, NULL);
559 
560 	if (!IS_ERR(i2c->clk)) {
561 		int ret = clk_prepare_enable(i2c->clk);
562 
563 		if (ret) {
564 			dev_err(&pdev->dev,
565 				"clk_prepare_enable failed: %d\n", ret);
566 			return ret;
567 		}
568 		i2c->ip_clock_khz = clk_get_rate(i2c->clk) / 1000;
569 		if (clock_frequency_present)
570 			i2c->bus_clock_khz = clock_frequency / 1000;
571 	}
572 
573 	if (i2c->ip_clock_khz == 0) {
574 		if (of_property_read_u32(np, "opencores,ip-clock-frequency",
575 						&val)) {
576 			if (!clock_frequency_present) {
577 				dev_err(&pdev->dev,
578 					"Missing required parameter 'opencores,ip-clock-frequency'\n");
579 				clk_disable_unprepare(i2c->clk);
580 				return -ENODEV;
581 			}
582 			i2c->ip_clock_khz = clock_frequency / 1000;
583 			dev_warn(&pdev->dev,
584 				 "Deprecated usage of the 'clock-frequency' property, please update to 'opencores,ip-clock-frequency'\n");
585 		} else {
586 			i2c->ip_clock_khz = val / 1000;
587 			if (clock_frequency_present)
588 				i2c->bus_clock_khz = clock_frequency / 1000;
589 		}
590 	}
591 
592 	of_property_read_u32(pdev->dev.of_node, "reg-io-width",
593 				&i2c->reg_io_width);
594 
595 	match = of_match_node(ocores_i2c_match, pdev->dev.of_node);
596 	if (match && (long)match->data == TYPE_GRLIB) {
597 		dev_dbg(&pdev->dev, "GRLIB variant of i2c-ocores\n");
598 		i2c->setreg = oc_setreg_grlib;
599 		i2c->getreg = oc_getreg_grlib;
600 	}
601 
602 	return 0;
603 }
604 #else
605 #define ocores_i2c_of_probe(pdev, i2c) -ENODEV
606 #endif
607 
ocores_i2c_probe(struct platform_device * pdev)608 static int ocores_i2c_probe(struct platform_device *pdev)
609 {
610 	struct ocores_i2c *i2c;
611 	struct ocores_i2c_platform_data *pdata;
612 	const struct of_device_id *match;
613 	struct resource *res;
614 	int irq;
615 	int ret;
616 	int i;
617 
618 	i2c = devm_kzalloc(&pdev->dev, sizeof(*i2c), GFP_KERNEL);
619 	if (!i2c)
620 		return -ENOMEM;
621 
622 	spin_lock_init(&i2c->process_lock);
623 
624 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
625 	if (res) {
626 		i2c->base = devm_ioremap_resource(&pdev->dev, res);
627 		if (IS_ERR(i2c->base))
628 			return PTR_ERR(i2c->base);
629 	} else {
630 		res = platform_get_resource(pdev, IORESOURCE_IO, 0);
631 		if (!res)
632 			return -EINVAL;
633 		i2c->iobase = res->start;
634 		if (!devm_request_region(&pdev->dev, res->start,
635 					 resource_size(res),
636 					 pdev->name)) {
637 			dev_err(&pdev->dev, "Can't get I/O resource.\n");
638 			return -EBUSY;
639 		}
640 		i2c->setreg = oc_setreg_io_8;
641 		i2c->getreg = oc_getreg_io_8;
642 	}
643 
644 	pdata = dev_get_platdata(&pdev->dev);
645 	if (pdata) {
646 		i2c->reg_shift = pdata->reg_shift;
647 		i2c->reg_io_width = pdata->reg_io_width;
648 		i2c->ip_clock_khz = pdata->clock_khz;
649 		if (pdata->bus_khz)
650 			i2c->bus_clock_khz = pdata->bus_khz;
651 		else
652 			i2c->bus_clock_khz = 100;
653 	} else {
654 		ret = ocores_i2c_of_probe(pdev, i2c);
655 		if (ret)
656 			return ret;
657 	}
658 
659 	if (i2c->reg_io_width == 0)
660 		i2c->reg_io_width = 1; /* Set to default value */
661 
662 	if (!i2c->setreg || !i2c->getreg) {
663 		bool be = pdata ? pdata->big_endian :
664 			of_device_is_big_endian(pdev->dev.of_node);
665 
666 		switch (i2c->reg_io_width) {
667 		case 1:
668 			i2c->setreg = oc_setreg_8;
669 			i2c->getreg = oc_getreg_8;
670 			break;
671 
672 		case 2:
673 			i2c->setreg = be ? oc_setreg_16be : oc_setreg_16;
674 			i2c->getreg = be ? oc_getreg_16be : oc_getreg_16;
675 			break;
676 
677 		case 4:
678 			i2c->setreg = be ? oc_setreg_32be : oc_setreg_32;
679 			i2c->getreg = be ? oc_getreg_32be : oc_getreg_32;
680 			break;
681 
682 		default:
683 			dev_err(&pdev->dev, "Unsupported I/O width (%d)\n",
684 				i2c->reg_io_width);
685 			ret = -EINVAL;
686 			goto err_clk;
687 		}
688 	}
689 
690 	init_waitqueue_head(&i2c->wait);
691 
692 	irq = platform_get_irq(pdev, 0);
693 	if (irq == -ENXIO) {
694 		ocores_algorithm.master_xfer = ocores_xfer_polling;
695 
696 		/*
697 		 * Set in OCORES_FLAG_BROKEN_IRQ to enable workaround for
698 		 * FU540-C000 SoC in polling mode.
699 		 */
700 		match = of_match_node(ocores_i2c_match, pdev->dev.of_node);
701 		if (match && (long)match->data == TYPE_SIFIVE_REV0)
702 			i2c->flags |= OCORES_FLAG_BROKEN_IRQ;
703 	} else {
704 		if (irq < 0)
705 			return irq;
706 	}
707 
708 	if (ocores_algorithm.master_xfer != ocores_xfer_polling) {
709 		ret = devm_request_any_context_irq(&pdev->dev, irq,
710 						   ocores_isr, 0,
711 						   pdev->name, i2c);
712 		if (ret) {
713 			dev_err(&pdev->dev, "Cannot claim IRQ\n");
714 			goto err_clk;
715 		}
716 	}
717 
718 	ret = ocores_init(&pdev->dev, i2c);
719 	if (ret)
720 		goto err_clk;
721 
722 	/* hook up driver to tree */
723 	platform_set_drvdata(pdev, i2c);
724 	i2c->adap = ocores_adapter;
725 	i2c_set_adapdata(&i2c->adap, i2c);
726 	i2c->adap.dev.parent = &pdev->dev;
727 	i2c->adap.dev.of_node = pdev->dev.of_node;
728 
729 	/* add i2c adapter to i2c tree */
730 	ret = i2c_add_adapter(&i2c->adap);
731 	if (ret)
732 		goto err_clk;
733 
734 	/* add in known devices to the bus */
735 	if (pdata) {
736 		for (i = 0; i < pdata->num_devices; i++)
737 			i2c_new_client_device(&i2c->adap, pdata->devices + i);
738 	}
739 
740 	return 0;
741 
742 err_clk:
743 	clk_disable_unprepare(i2c->clk);
744 	return ret;
745 }
746 
ocores_i2c_remove(struct platform_device * pdev)747 static int ocores_i2c_remove(struct platform_device *pdev)
748 {
749 	struct ocores_i2c *i2c = platform_get_drvdata(pdev);
750 	u8 ctrl = oc_getreg(i2c, OCI2C_CONTROL);
751 
752 	/* disable i2c logic */
753 	ctrl &= ~(OCI2C_CTRL_EN | OCI2C_CTRL_IEN);
754 	oc_setreg(i2c, OCI2C_CONTROL, ctrl);
755 
756 	/* remove adapter & data */
757 	i2c_del_adapter(&i2c->adap);
758 
759 	if (!IS_ERR(i2c->clk))
760 		clk_disable_unprepare(i2c->clk);
761 
762 	return 0;
763 }
764 
765 #ifdef CONFIG_PM_SLEEP
ocores_i2c_suspend(struct device * dev)766 static int ocores_i2c_suspend(struct device *dev)
767 {
768 	struct ocores_i2c *i2c = dev_get_drvdata(dev);
769 	u8 ctrl = oc_getreg(i2c, OCI2C_CONTROL);
770 
771 	/* make sure the device is disabled */
772 	ctrl &= ~(OCI2C_CTRL_EN | OCI2C_CTRL_IEN);
773 	oc_setreg(i2c, OCI2C_CONTROL, ctrl);
774 
775 	if (!IS_ERR(i2c->clk))
776 		clk_disable_unprepare(i2c->clk);
777 	return 0;
778 }
779 
ocores_i2c_resume(struct device * dev)780 static int ocores_i2c_resume(struct device *dev)
781 {
782 	struct ocores_i2c *i2c = dev_get_drvdata(dev);
783 
784 	if (!IS_ERR(i2c->clk)) {
785 		unsigned long rate;
786 		int ret = clk_prepare_enable(i2c->clk);
787 
788 		if (ret) {
789 			dev_err(dev,
790 				"clk_prepare_enable failed: %d\n", ret);
791 			return ret;
792 		}
793 		rate = clk_get_rate(i2c->clk) / 1000;
794 		if (rate)
795 			i2c->ip_clock_khz = rate;
796 	}
797 	return ocores_init(dev, i2c);
798 }
799 
800 static SIMPLE_DEV_PM_OPS(ocores_i2c_pm, ocores_i2c_suspend, ocores_i2c_resume);
801 #define OCORES_I2C_PM	(&ocores_i2c_pm)
802 #else
803 #define OCORES_I2C_PM	NULL
804 #endif
805 
806 static struct platform_driver ocores_i2c_driver = {
807 	.probe   = ocores_i2c_probe,
808 	.remove  = ocores_i2c_remove,
809 	.driver  = {
810 		.name = "ocores-i2c",
811 		.of_match_table = ocores_i2c_match,
812 		.pm = OCORES_I2C_PM,
813 	},
814 };
815 
816 module_platform_driver(ocores_i2c_driver);
817 
818 MODULE_AUTHOR("Peter Korsgaard <peter@korsgaard.com>");
819 MODULE_DESCRIPTION("OpenCores I2C bus driver");
820 MODULE_LICENSE("GPL");
821 MODULE_ALIAS("platform:ocores-i2c");
822