xref: /openbmc/linux/drivers/i2c/busses/i2c-bcm2835.c (revision 5a1ea477)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * BCM2835 master mode driver
4  */
5 
6 #include <linux/clk.h>
7 #include <linux/completion.h>
8 #include <linux/err.h>
9 #include <linux/i2c.h>
10 #include <linux/interrupt.h>
11 #include <linux/io.h>
12 #include <linux/module.h>
13 #include <linux/platform_device.h>
14 #include <linux/slab.h>
15 
16 #define BCM2835_I2C_C		0x0
17 #define BCM2835_I2C_S		0x4
18 #define BCM2835_I2C_DLEN	0x8
19 #define BCM2835_I2C_A		0xc
20 #define BCM2835_I2C_FIFO	0x10
21 #define BCM2835_I2C_DIV		0x14
22 #define BCM2835_I2C_DEL		0x18
23 #define BCM2835_I2C_CLKT	0x1c
24 
25 #define BCM2835_I2C_C_READ	BIT(0)
26 #define BCM2835_I2C_C_CLEAR	BIT(4) /* bits 4 and 5 both clear */
27 #define BCM2835_I2C_C_ST	BIT(7)
28 #define BCM2835_I2C_C_INTD	BIT(8)
29 #define BCM2835_I2C_C_INTT	BIT(9)
30 #define BCM2835_I2C_C_INTR	BIT(10)
31 #define BCM2835_I2C_C_I2CEN	BIT(15)
32 
33 #define BCM2835_I2C_S_TA	BIT(0)
34 #define BCM2835_I2C_S_DONE	BIT(1)
35 #define BCM2835_I2C_S_TXW	BIT(2)
36 #define BCM2835_I2C_S_RXR	BIT(3)
37 #define BCM2835_I2C_S_TXD	BIT(4)
38 #define BCM2835_I2C_S_RXD	BIT(5)
39 #define BCM2835_I2C_S_TXE	BIT(6)
40 #define BCM2835_I2C_S_RXF	BIT(7)
41 #define BCM2835_I2C_S_ERR	BIT(8)
42 #define BCM2835_I2C_S_CLKT	BIT(9)
43 #define BCM2835_I2C_S_LEN	BIT(10) /* Fake bit for SW error reporting */
44 
45 #define BCM2835_I2C_FEDL_SHIFT	16
46 #define BCM2835_I2C_REDL_SHIFT	0
47 
48 #define BCM2835_I2C_CDIV_MIN	0x0002
49 #define BCM2835_I2C_CDIV_MAX	0xFFFE
50 
51 struct bcm2835_i2c_dev {
52 	struct device *dev;
53 	void __iomem *regs;
54 	struct clk *clk;
55 	int irq;
56 	u32 bus_clk_rate;
57 	struct i2c_adapter adapter;
58 	struct completion completion;
59 	struct i2c_msg *curr_msg;
60 	int num_msgs;
61 	u32 msg_err;
62 	u8 *msg_buf;
63 	size_t msg_buf_remaining;
64 };
65 
66 static inline void bcm2835_i2c_writel(struct bcm2835_i2c_dev *i2c_dev,
67 				      u32 reg, u32 val)
68 {
69 	writel(val, i2c_dev->regs + reg);
70 }
71 
72 static inline u32 bcm2835_i2c_readl(struct bcm2835_i2c_dev *i2c_dev, u32 reg)
73 {
74 	return readl(i2c_dev->regs + reg);
75 }
76 
77 static int bcm2835_i2c_set_divider(struct bcm2835_i2c_dev *i2c_dev)
78 {
79 	u32 divider, redl, fedl;
80 
81 	divider = DIV_ROUND_UP(clk_get_rate(i2c_dev->clk),
82 			       i2c_dev->bus_clk_rate);
83 	/*
84 	 * Per the datasheet, the register is always interpreted as an even
85 	 * number, by rounding down. In other words, the LSB is ignored. So,
86 	 * if the LSB is set, increment the divider to avoid any issue.
87 	 */
88 	if (divider & 1)
89 		divider++;
90 	if ((divider < BCM2835_I2C_CDIV_MIN) ||
91 	    (divider > BCM2835_I2C_CDIV_MAX)) {
92 		dev_err_ratelimited(i2c_dev->dev, "Invalid clock-frequency\n");
93 		return -EINVAL;
94 	}
95 
96 	bcm2835_i2c_writel(i2c_dev, BCM2835_I2C_DIV, divider);
97 
98 	/*
99 	 * Number of core clocks to wait after falling edge before
100 	 * outputting the next data bit.  Note that both FEDL and REDL
101 	 * can't be greater than CDIV/2.
102 	 */
103 	fedl = max(divider / 16, 1u);
104 
105 	/*
106 	 * Number of core clocks to wait after rising edge before
107 	 * sampling the next incoming data bit.
108 	 */
109 	redl = max(divider / 4, 1u);
110 
111 	bcm2835_i2c_writel(i2c_dev, BCM2835_I2C_DEL,
112 			   (fedl << BCM2835_I2C_FEDL_SHIFT) |
113 			   (redl << BCM2835_I2C_REDL_SHIFT));
114 	return 0;
115 }
116 
117 static void bcm2835_fill_txfifo(struct bcm2835_i2c_dev *i2c_dev)
118 {
119 	u32 val;
120 
121 	while (i2c_dev->msg_buf_remaining) {
122 		val = bcm2835_i2c_readl(i2c_dev, BCM2835_I2C_S);
123 		if (!(val & BCM2835_I2C_S_TXD))
124 			break;
125 		bcm2835_i2c_writel(i2c_dev, BCM2835_I2C_FIFO,
126 				   *i2c_dev->msg_buf);
127 		i2c_dev->msg_buf++;
128 		i2c_dev->msg_buf_remaining--;
129 	}
130 }
131 
132 static void bcm2835_drain_rxfifo(struct bcm2835_i2c_dev *i2c_dev)
133 {
134 	u32 val;
135 
136 	while (i2c_dev->msg_buf_remaining) {
137 		val = bcm2835_i2c_readl(i2c_dev, BCM2835_I2C_S);
138 		if (!(val & BCM2835_I2C_S_RXD))
139 			break;
140 		*i2c_dev->msg_buf = bcm2835_i2c_readl(i2c_dev,
141 						      BCM2835_I2C_FIFO);
142 		i2c_dev->msg_buf++;
143 		i2c_dev->msg_buf_remaining--;
144 	}
145 }
146 
147 /*
148  * Repeated Start Condition (Sr)
149  * The BCM2835 ARM Peripherals datasheet mentions a way to trigger a Sr when it
150  * talks about reading from a slave with 10 bit address. This is achieved by
151  * issuing a write, poll the I2CS.TA flag and wait for it to be set, and then
152  * issue a read.
153  * A comment in https://github.com/raspberrypi/linux/issues/254 shows how the
154  * firmware actually does it using polling and says that it's a workaround for
155  * a problem in the state machine.
156  * It turns out that it is possible to use the TXW interrupt to know when the
157  * transfer is active, provided the FIFO has not been prefilled.
158  */
159 
160 static void bcm2835_i2c_start_transfer(struct bcm2835_i2c_dev *i2c_dev)
161 {
162 	u32 c = BCM2835_I2C_C_ST | BCM2835_I2C_C_I2CEN;
163 	struct i2c_msg *msg = i2c_dev->curr_msg;
164 	bool last_msg = (i2c_dev->num_msgs == 1);
165 
166 	if (!i2c_dev->num_msgs)
167 		return;
168 
169 	i2c_dev->num_msgs--;
170 	i2c_dev->msg_buf = msg->buf;
171 	i2c_dev->msg_buf_remaining = msg->len;
172 
173 	if (msg->flags & I2C_M_RD)
174 		c |= BCM2835_I2C_C_READ | BCM2835_I2C_C_INTR;
175 	else
176 		c |= BCM2835_I2C_C_INTT;
177 
178 	if (last_msg)
179 		c |= BCM2835_I2C_C_INTD;
180 
181 	bcm2835_i2c_writel(i2c_dev, BCM2835_I2C_A, msg->addr);
182 	bcm2835_i2c_writel(i2c_dev, BCM2835_I2C_DLEN, msg->len);
183 	bcm2835_i2c_writel(i2c_dev, BCM2835_I2C_C, c);
184 }
185 
186 static void bcm2835_i2c_finish_transfer(struct bcm2835_i2c_dev *i2c_dev)
187 {
188 	i2c_dev->curr_msg = NULL;
189 	i2c_dev->num_msgs = 0;
190 
191 	i2c_dev->msg_buf = NULL;
192 	i2c_dev->msg_buf_remaining = 0;
193 }
194 
195 /*
196  * Note about I2C_C_CLEAR on error:
197  * The I2C_C_CLEAR on errors will take some time to resolve -- if you were in
198  * non-idle state and I2C_C_READ, it sets an abort_rx flag and runs through
199  * the state machine to send a NACK and a STOP. Since we're setting CLEAR
200  * without I2CEN, that NACK will be hanging around queued up for next time
201  * we start the engine.
202  */
203 
204 static irqreturn_t bcm2835_i2c_isr(int this_irq, void *data)
205 {
206 	struct bcm2835_i2c_dev *i2c_dev = data;
207 	u32 val, err;
208 
209 	val = bcm2835_i2c_readl(i2c_dev, BCM2835_I2C_S);
210 
211 	err = val & (BCM2835_I2C_S_CLKT | BCM2835_I2C_S_ERR);
212 	if (err) {
213 		i2c_dev->msg_err = err;
214 		goto complete;
215 	}
216 
217 	if (val & BCM2835_I2C_S_DONE) {
218 		if (!i2c_dev->curr_msg) {
219 			dev_err(i2c_dev->dev, "Got unexpected interrupt (from firmware?)\n");
220 		} else if (i2c_dev->curr_msg->flags & I2C_M_RD) {
221 			bcm2835_drain_rxfifo(i2c_dev);
222 			val = bcm2835_i2c_readl(i2c_dev, BCM2835_I2C_S);
223 		}
224 
225 		if ((val & BCM2835_I2C_S_RXD) || i2c_dev->msg_buf_remaining)
226 			i2c_dev->msg_err = BCM2835_I2C_S_LEN;
227 		else
228 			i2c_dev->msg_err = 0;
229 		goto complete;
230 	}
231 
232 	if (val & BCM2835_I2C_S_TXW) {
233 		if (!i2c_dev->msg_buf_remaining) {
234 			i2c_dev->msg_err = val | BCM2835_I2C_S_LEN;
235 			goto complete;
236 		}
237 
238 		bcm2835_fill_txfifo(i2c_dev);
239 
240 		if (i2c_dev->num_msgs && !i2c_dev->msg_buf_remaining) {
241 			i2c_dev->curr_msg++;
242 			bcm2835_i2c_start_transfer(i2c_dev);
243 		}
244 
245 		return IRQ_HANDLED;
246 	}
247 
248 	if (val & BCM2835_I2C_S_RXR) {
249 		if (!i2c_dev->msg_buf_remaining) {
250 			i2c_dev->msg_err = val | BCM2835_I2C_S_LEN;
251 			goto complete;
252 		}
253 
254 		bcm2835_drain_rxfifo(i2c_dev);
255 		return IRQ_HANDLED;
256 	}
257 
258 	return IRQ_NONE;
259 
260 complete:
261 	bcm2835_i2c_writel(i2c_dev, BCM2835_I2C_C, BCM2835_I2C_C_CLEAR);
262 	bcm2835_i2c_writel(i2c_dev, BCM2835_I2C_S, BCM2835_I2C_S_CLKT |
263 			   BCM2835_I2C_S_ERR | BCM2835_I2C_S_DONE);
264 	complete(&i2c_dev->completion);
265 
266 	return IRQ_HANDLED;
267 }
268 
269 static int bcm2835_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[],
270 			    int num)
271 {
272 	struct bcm2835_i2c_dev *i2c_dev = i2c_get_adapdata(adap);
273 	unsigned long time_left;
274 	int i, ret;
275 
276 	for (i = 0; i < (num - 1); i++)
277 		if (msgs[i].flags & I2C_M_RD) {
278 			dev_warn_once(i2c_dev->dev,
279 				      "only one read message supported, has to be last\n");
280 			return -EOPNOTSUPP;
281 		}
282 
283 	ret = bcm2835_i2c_set_divider(i2c_dev);
284 	if (ret)
285 		return ret;
286 
287 	i2c_dev->curr_msg = msgs;
288 	i2c_dev->num_msgs = num;
289 	reinit_completion(&i2c_dev->completion);
290 
291 	bcm2835_i2c_start_transfer(i2c_dev);
292 
293 	time_left = wait_for_completion_timeout(&i2c_dev->completion,
294 						adap->timeout);
295 
296 	bcm2835_i2c_finish_transfer(i2c_dev);
297 
298 	if (!time_left) {
299 		bcm2835_i2c_writel(i2c_dev, BCM2835_I2C_C,
300 				   BCM2835_I2C_C_CLEAR);
301 		dev_err(i2c_dev->dev, "i2c transfer timed out\n");
302 		return -ETIMEDOUT;
303 	}
304 
305 	if (!i2c_dev->msg_err)
306 		return num;
307 
308 	dev_dbg(i2c_dev->dev, "i2c transfer failed: %x\n", i2c_dev->msg_err);
309 
310 	if (i2c_dev->msg_err & BCM2835_I2C_S_ERR)
311 		return -EREMOTEIO;
312 
313 	return -EIO;
314 }
315 
316 static u32 bcm2835_i2c_func(struct i2c_adapter *adap)
317 {
318 	return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
319 }
320 
321 static const struct i2c_algorithm bcm2835_i2c_algo = {
322 	.master_xfer	= bcm2835_i2c_xfer,
323 	.functionality	= bcm2835_i2c_func,
324 };
325 
326 /*
327  * This HW was reported to have problems with clock stretching:
328  * http://www.advamation.com/knowhow/raspberrypi/rpi-i2c-bug.html
329  * https://www.raspberrypi.org/forums/viewtopic.php?p=146272
330  */
331 static const struct i2c_adapter_quirks bcm2835_i2c_quirks = {
332 	.flags = I2C_AQ_NO_CLK_STRETCH,
333 };
334 
335 static int bcm2835_i2c_probe(struct platform_device *pdev)
336 {
337 	struct bcm2835_i2c_dev *i2c_dev;
338 	struct resource *mem, *irq;
339 	int ret;
340 	struct i2c_adapter *adap;
341 
342 	i2c_dev = devm_kzalloc(&pdev->dev, sizeof(*i2c_dev), GFP_KERNEL);
343 	if (!i2c_dev)
344 		return -ENOMEM;
345 	platform_set_drvdata(pdev, i2c_dev);
346 	i2c_dev->dev = &pdev->dev;
347 	init_completion(&i2c_dev->completion);
348 
349 	mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
350 	i2c_dev->regs = devm_ioremap_resource(&pdev->dev, mem);
351 	if (IS_ERR(i2c_dev->regs))
352 		return PTR_ERR(i2c_dev->regs);
353 
354 	i2c_dev->clk = devm_clk_get(&pdev->dev, NULL);
355 	if (IS_ERR(i2c_dev->clk)) {
356 		if (PTR_ERR(i2c_dev->clk) != -EPROBE_DEFER)
357 			dev_err(&pdev->dev, "Could not get clock\n");
358 		return PTR_ERR(i2c_dev->clk);
359 	}
360 
361 	ret = of_property_read_u32(pdev->dev.of_node, "clock-frequency",
362 				   &i2c_dev->bus_clk_rate);
363 	if (ret < 0) {
364 		dev_warn(&pdev->dev,
365 			 "Could not read clock-frequency property\n");
366 		i2c_dev->bus_clk_rate = 100000;
367 	}
368 
369 	irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
370 	if (!irq) {
371 		dev_err(&pdev->dev, "No IRQ resource\n");
372 		return -ENODEV;
373 	}
374 	i2c_dev->irq = irq->start;
375 
376 	ret = request_irq(i2c_dev->irq, bcm2835_i2c_isr, IRQF_SHARED,
377 			  dev_name(&pdev->dev), i2c_dev);
378 	if (ret) {
379 		dev_err(&pdev->dev, "Could not request IRQ\n");
380 		return -ENODEV;
381 	}
382 
383 	adap = &i2c_dev->adapter;
384 	i2c_set_adapdata(adap, i2c_dev);
385 	adap->owner = THIS_MODULE;
386 	adap->class = I2C_CLASS_DEPRECATED;
387 	strlcpy(adap->name, "bcm2835 I2C adapter", sizeof(adap->name));
388 	adap->algo = &bcm2835_i2c_algo;
389 	adap->dev.parent = &pdev->dev;
390 	adap->dev.of_node = pdev->dev.of_node;
391 	adap->quirks = &bcm2835_i2c_quirks;
392 
393 	bcm2835_i2c_writel(i2c_dev, BCM2835_I2C_C, 0);
394 
395 	ret = i2c_add_adapter(adap);
396 	if (ret)
397 		free_irq(i2c_dev->irq, i2c_dev);
398 
399 	return ret;
400 }
401 
402 static int bcm2835_i2c_remove(struct platform_device *pdev)
403 {
404 	struct bcm2835_i2c_dev *i2c_dev = platform_get_drvdata(pdev);
405 
406 	free_irq(i2c_dev->irq, i2c_dev);
407 	i2c_del_adapter(&i2c_dev->adapter);
408 
409 	return 0;
410 }
411 
412 static const struct of_device_id bcm2835_i2c_of_match[] = {
413 	{ .compatible = "brcm,bcm2835-i2c" },
414 	{},
415 };
416 MODULE_DEVICE_TABLE(of, bcm2835_i2c_of_match);
417 
418 static struct platform_driver bcm2835_i2c_driver = {
419 	.probe		= bcm2835_i2c_probe,
420 	.remove		= bcm2835_i2c_remove,
421 	.driver		= {
422 		.name	= "i2c-bcm2835",
423 		.of_match_table = bcm2835_i2c_of_match,
424 	},
425 };
426 module_platform_driver(bcm2835_i2c_driver);
427 
428 MODULE_AUTHOR("Stephen Warren <swarren@wwwdotorg.org>");
429 MODULE_DESCRIPTION("BCM2835 I2C bus adapter");
430 MODULE_LICENSE("GPL v2");
431 MODULE_ALIAS("platform:i2c-bcm2835");
432