blob: 859a623e73e7d064bc58cf0d48f5dc7a5e2a4d3c [file] [log] [blame]
Ray Juie6e5dd32015-02-07 21:25:24 -08001/*
2 * Copyright (C) 2014 Broadcom Corporation
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation version 2.
7 *
8 * This program is distributed "as is" WITHOUT ANY WARRANTY of any
9 * kind, whether express or implied; without even the implied warranty
10 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 */
13
14#include <linux/delay.h>
15#include <linux/i2c.h>
16#include <linux/interrupt.h>
17#include <linux/io.h>
18#include <linux/kernel.h>
19#include <linux/module.h>
20#include <linux/platform_device.h>
21#include <linux/slab.h>
22
23#define CFG_OFFSET 0x00
24#define CFG_RESET_SHIFT 31
25#define CFG_EN_SHIFT 30
26#define CFG_M_RETRY_CNT_SHIFT 16
27#define CFG_M_RETRY_CNT_MASK 0x0f
28
29#define TIM_CFG_OFFSET 0x04
30#define TIM_CFG_MODE_400_SHIFT 31
31
32#define M_FIFO_CTRL_OFFSET 0x0c
33#define M_FIFO_RX_FLUSH_SHIFT 31
34#define M_FIFO_TX_FLUSH_SHIFT 30
35#define M_FIFO_RX_CNT_SHIFT 16
36#define M_FIFO_RX_CNT_MASK 0x7f
37#define M_FIFO_RX_THLD_SHIFT 8
38#define M_FIFO_RX_THLD_MASK 0x3f
39
40#define M_CMD_OFFSET 0x30
41#define M_CMD_START_BUSY_SHIFT 31
42#define M_CMD_STATUS_SHIFT 25
43#define M_CMD_STATUS_MASK 0x07
44#define M_CMD_STATUS_SUCCESS 0x0
45#define M_CMD_STATUS_LOST_ARB 0x1
46#define M_CMD_STATUS_NACK_ADDR 0x2
47#define M_CMD_STATUS_NACK_DATA 0x3
48#define M_CMD_STATUS_TIMEOUT 0x4
49#define M_CMD_PROTOCOL_SHIFT 9
50#define M_CMD_PROTOCOL_MASK 0xf
51#define M_CMD_PROTOCOL_BLK_WR 0x7
52#define M_CMD_PROTOCOL_BLK_RD 0x8
53#define M_CMD_PEC_SHIFT 8
54#define M_CMD_RD_CNT_SHIFT 0
55#define M_CMD_RD_CNT_MASK 0xff
56
57#define IE_OFFSET 0x38
58#define IE_M_RX_FIFO_FULL_SHIFT 31
59#define IE_M_RX_THLD_SHIFT 30
60#define IE_M_START_BUSY_SHIFT 28
61
62#define IS_OFFSET 0x3c
63#define IS_M_RX_FIFO_FULL_SHIFT 31
64#define IS_M_RX_THLD_SHIFT 30
65#define IS_M_START_BUSY_SHIFT 28
66
67#define M_TX_OFFSET 0x40
68#define M_TX_WR_STATUS_SHIFT 31
69#define M_TX_DATA_SHIFT 0
70#define M_TX_DATA_MASK 0xff
71
72#define M_RX_OFFSET 0x44
73#define M_RX_STATUS_SHIFT 30
74#define M_RX_STATUS_MASK 0x03
75#define M_RX_PEC_ERR_SHIFT 29
76#define M_RX_DATA_SHIFT 0
77#define M_RX_DATA_MASK 0xff
78
Ray Juied36bfc2016-02-12 13:10:42 -080079#define I2C_TIMEOUT_MSEC 100
Ray Juie6e5dd32015-02-07 21:25:24 -080080#define M_TX_RX_FIFO_SIZE 64
81
82enum bus_speed_index {
83 I2C_SPD_100K = 0,
84 I2C_SPD_400K,
85};
86
87struct bcm_iproc_i2c_dev {
88 struct device *device;
89 int irq;
90
91 void __iomem *base;
92
93 struct i2c_adapter adapter;
Ray Jui0ee04e92015-05-14 15:36:04 -070094 unsigned int bus_speed;
Ray Juie6e5dd32015-02-07 21:25:24 -080095
96 struct completion done;
97 int xfer_is_done;
98};
99
100/*
101 * Can be expanded in the future if more interrupt status bits are utilized
102 */
103#define ISR_MASK (1 << IS_M_START_BUSY_SHIFT)
104
105static irqreturn_t bcm_iproc_i2c_isr(int irq, void *data)
106{
107 struct bcm_iproc_i2c_dev *iproc_i2c = data;
108 u32 status = readl(iproc_i2c->base + IS_OFFSET);
109
110 status &= ISR_MASK;
111
112 if (!status)
113 return IRQ_NONE;
114
115 writel(status, iproc_i2c->base + IS_OFFSET);
116 iproc_i2c->xfer_is_done = 1;
117 complete_all(&iproc_i2c->done);
118
119 return IRQ_HANDLED;
120}
121
Ray Jui6ee608c2016-02-12 13:10:41 -0800122static int bcm_iproc_i2c_init(struct bcm_iproc_i2c_dev *iproc_i2c)
123{
124 u32 val;
125
126 /* put controller in reset */
127 val = readl(iproc_i2c->base + CFG_OFFSET);
128 val |= 1 << CFG_RESET_SHIFT;
129 val &= ~(1 << CFG_EN_SHIFT);
130 writel(val, iproc_i2c->base + CFG_OFFSET);
131
132 /* wait 100 usec per spec */
133 udelay(100);
134
135 /* bring controller out of reset */
136 val &= ~(1 << CFG_RESET_SHIFT);
137 writel(val, iproc_i2c->base + CFG_OFFSET);
138
139 /* flush TX/RX FIFOs and set RX FIFO threshold to zero */
140 val = (1 << M_FIFO_RX_FLUSH_SHIFT) | (1 << M_FIFO_TX_FLUSH_SHIFT);
141 writel(val, iproc_i2c->base + M_FIFO_CTRL_OFFSET);
142 /* disable all interrupts */
143 writel(0, iproc_i2c->base + IE_OFFSET);
144
145 /* clear all pending interrupts */
146 writel(0xffffffff, iproc_i2c->base + IS_OFFSET);
147
148 return 0;
149}
150
151static void bcm_iproc_i2c_enable_disable(struct bcm_iproc_i2c_dev *iproc_i2c,
152 bool enable)
153{
154 u32 val;
155
156 val = readl(iproc_i2c->base + CFG_OFFSET);
157 if (enable)
158 val |= BIT(CFG_EN_SHIFT);
159 else
160 val &= ~BIT(CFG_EN_SHIFT);
161 writel(val, iproc_i2c->base + CFG_OFFSET);
162}
163
Ray Juie6e5dd32015-02-07 21:25:24 -0800164static int bcm_iproc_i2c_check_status(struct bcm_iproc_i2c_dev *iproc_i2c,
165 struct i2c_msg *msg)
166{
167 u32 val;
168
169 val = readl(iproc_i2c->base + M_CMD_OFFSET);
170 val = (val >> M_CMD_STATUS_SHIFT) & M_CMD_STATUS_MASK;
171
172 switch (val) {
173 case M_CMD_STATUS_SUCCESS:
174 return 0;
175
176 case M_CMD_STATUS_LOST_ARB:
177 dev_dbg(iproc_i2c->device, "lost bus arbitration\n");
178 return -EAGAIN;
179
180 case M_CMD_STATUS_NACK_ADDR:
181 dev_dbg(iproc_i2c->device, "NAK addr:0x%02x\n", msg->addr);
182 return -ENXIO;
183
184 case M_CMD_STATUS_NACK_DATA:
185 dev_dbg(iproc_i2c->device, "NAK data\n");
186 return -ENXIO;
187
188 case M_CMD_STATUS_TIMEOUT:
189 dev_dbg(iproc_i2c->device, "bus timeout\n");
190 return -ETIMEDOUT;
191
192 default:
193 dev_dbg(iproc_i2c->device, "unknown error code=%d\n", val);
Ray Jui6ee608c2016-02-12 13:10:41 -0800194
195 /* re-initialize i2c for recovery */
196 bcm_iproc_i2c_enable_disable(iproc_i2c, false);
197 bcm_iproc_i2c_init(iproc_i2c);
198 bcm_iproc_i2c_enable_disable(iproc_i2c, true);
199
Ray Juie6e5dd32015-02-07 21:25:24 -0800200 return -EIO;
201 }
202}
203
204static int bcm_iproc_i2c_xfer_single_msg(struct bcm_iproc_i2c_dev *iproc_i2c,
205 struct i2c_msg *msg)
206{
207 int ret, i;
208 u8 addr;
209 u32 val;
Ray Juied36bfc2016-02-12 13:10:42 -0800210 unsigned long time_left = msecs_to_jiffies(I2C_TIMEOUT_MSEC);
Ray Juie6e5dd32015-02-07 21:25:24 -0800211
Ray Juie6e5dd32015-02-07 21:25:24 -0800212 /* check if bus is busy */
213 if (!!(readl(iproc_i2c->base + M_CMD_OFFSET) &
214 BIT(M_CMD_START_BUSY_SHIFT))) {
215 dev_warn(iproc_i2c->device, "bus is busy\n");
216 return -EBUSY;
217 }
218
219 /* format and load slave address into the TX FIFO */
220 addr = msg->addr << 1 | (msg->flags & I2C_M_RD ? 1 : 0);
221 writel(addr, iproc_i2c->base + M_TX_OFFSET);
222
223 /* for a write transaction, load data into the TX FIFO */
224 if (!(msg->flags & I2C_M_RD)) {
225 for (i = 0; i < msg->len; i++) {
226 val = msg->buf[i];
227
228 /* mark the last byte */
229 if (i == msg->len - 1)
230 val |= 1 << M_TX_WR_STATUS_SHIFT;
231
232 writel(val, iproc_i2c->base + M_TX_OFFSET);
233 }
234 }
235
236 /* mark as incomplete before starting the transaction */
237 reinit_completion(&iproc_i2c->done);
238 iproc_i2c->xfer_is_done = 0;
239
240 /*
241 * Enable the "start busy" interrupt, which will be triggered after the
242 * transaction is done, i.e., the internal start_busy bit, transitions
243 * from 1 to 0.
244 */
245 writel(1 << IE_M_START_BUSY_SHIFT, iproc_i2c->base + IE_OFFSET);
246
247 /*
248 * Now we can activate the transfer. For a read operation, specify the
249 * number of bytes to read
250 */
251 val = 1 << M_CMD_START_BUSY_SHIFT;
252 if (msg->flags & I2C_M_RD) {
253 val |= (M_CMD_PROTOCOL_BLK_RD << M_CMD_PROTOCOL_SHIFT) |
254 (msg->len << M_CMD_RD_CNT_SHIFT);
255 } else {
256 val |= (M_CMD_PROTOCOL_BLK_WR << M_CMD_PROTOCOL_SHIFT);
257 }
258 writel(val, iproc_i2c->base + M_CMD_OFFSET);
259
260 time_left = wait_for_completion_timeout(&iproc_i2c->done, time_left);
261
262 /* disable all interrupts */
263 writel(0, iproc_i2c->base + IE_OFFSET);
264 /* read it back to flush the write */
265 readl(iproc_i2c->base + IE_OFFSET);
266
267 /* make sure the interrupt handler isn't running */
268 synchronize_irq(iproc_i2c->irq);
269
270 if (!time_left && !iproc_i2c->xfer_is_done) {
271 dev_err(iproc_i2c->device, "transaction timed out\n");
272
273 /* flush FIFOs */
274 val = (1 << M_FIFO_RX_FLUSH_SHIFT) |
275 (1 << M_FIFO_TX_FLUSH_SHIFT);
276 writel(val, iproc_i2c->base + M_FIFO_CTRL_OFFSET);
277 return -ETIMEDOUT;
278 }
279
280 ret = bcm_iproc_i2c_check_status(iproc_i2c, msg);
281 if (ret) {
282 /* flush both TX/RX FIFOs */
283 val = (1 << M_FIFO_RX_FLUSH_SHIFT) |
284 (1 << M_FIFO_TX_FLUSH_SHIFT);
285 writel(val, iproc_i2c->base + M_FIFO_CTRL_OFFSET);
286 return ret;
287 }
288
289 /*
290 * For a read operation, we now need to load the data from FIFO
291 * into the memory buffer
292 */
293 if (msg->flags & I2C_M_RD) {
294 for (i = 0; i < msg->len; i++) {
295 msg->buf[i] = (readl(iproc_i2c->base + M_RX_OFFSET) >>
296 M_RX_DATA_SHIFT) & M_RX_DATA_MASK;
297 }
298 }
299
300 return 0;
301}
302
303static int bcm_iproc_i2c_xfer(struct i2c_adapter *adapter,
304 struct i2c_msg msgs[], int num)
305{
306 struct bcm_iproc_i2c_dev *iproc_i2c = i2c_get_adapdata(adapter);
307 int ret, i;
308
309 /* go through all messages */
310 for (i = 0; i < num; i++) {
311 ret = bcm_iproc_i2c_xfer_single_msg(iproc_i2c, &msgs[i]);
312 if (ret) {
313 dev_dbg(iproc_i2c->device, "xfer failed\n");
314 return ret;
315 }
316 }
317
318 return num;
319}
320
321static uint32_t bcm_iproc_i2c_functionality(struct i2c_adapter *adap)
322{
323 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
324}
325
326static const struct i2c_algorithm bcm_iproc_algo = {
327 .master_xfer = bcm_iproc_i2c_xfer,
328 .functionality = bcm_iproc_i2c_functionality,
329};
330
Wolfram Sang338f1ab2015-01-07 12:24:10 +0100331static struct i2c_adapter_quirks bcm_iproc_i2c_quirks = {
332 /* need to reserve one byte in the FIFO for the slave address */
333 .max_read_len = M_TX_RX_FIFO_SIZE - 1,
334 .max_write_len = M_TX_RX_FIFO_SIZE - 1,
335};
336
Ray Juie6e5dd32015-02-07 21:25:24 -0800337static int bcm_iproc_i2c_cfg_speed(struct bcm_iproc_i2c_dev *iproc_i2c)
338{
339 unsigned int bus_speed;
340 u32 val;
341 int ret = of_property_read_u32(iproc_i2c->device->of_node,
342 "clock-frequency", &bus_speed);
343 if (ret < 0) {
344 dev_info(iproc_i2c->device,
345 "unable to interpret clock-frequency DT property\n");
346 bus_speed = 100000;
347 }
348
349 if (bus_speed < 100000) {
350 dev_err(iproc_i2c->device, "%d Hz bus speed not supported\n",
351 bus_speed);
352 dev_err(iproc_i2c->device,
353 "valid speeds are 100khz and 400khz\n");
354 return -EINVAL;
355 } else if (bus_speed < 400000) {
356 bus_speed = 100000;
357 } else {
358 bus_speed = 400000;
359 }
360
Ray Jui0ee04e92015-05-14 15:36:04 -0700361 iproc_i2c->bus_speed = bus_speed;
Ray Juie6e5dd32015-02-07 21:25:24 -0800362 val = readl(iproc_i2c->base + TIM_CFG_OFFSET);
363 val &= ~(1 << TIM_CFG_MODE_400_SHIFT);
364 val |= (bus_speed == 400000) << TIM_CFG_MODE_400_SHIFT;
365 writel(val, iproc_i2c->base + TIM_CFG_OFFSET);
366
367 dev_info(iproc_i2c->device, "bus set to %u Hz\n", bus_speed);
368
369 return 0;
370}
371
Ray Juie6e5dd32015-02-07 21:25:24 -0800372static int bcm_iproc_i2c_probe(struct platform_device *pdev)
373{
374 int irq, ret = 0;
375 struct bcm_iproc_i2c_dev *iproc_i2c;
376 struct i2c_adapter *adap;
377 struct resource *res;
378
379 iproc_i2c = devm_kzalloc(&pdev->dev, sizeof(*iproc_i2c),
380 GFP_KERNEL);
381 if (!iproc_i2c)
382 return -ENOMEM;
383
384 platform_set_drvdata(pdev, iproc_i2c);
385 iproc_i2c->device = &pdev->dev;
386 init_completion(&iproc_i2c->done);
387
388 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
389 iproc_i2c->base = devm_ioremap_resource(iproc_i2c->device, res);
390 if (IS_ERR(iproc_i2c->base))
391 return PTR_ERR(iproc_i2c->base);
392
393 ret = bcm_iproc_i2c_init(iproc_i2c);
394 if (ret)
395 return ret;
396
397 ret = bcm_iproc_i2c_cfg_speed(iproc_i2c);
398 if (ret)
399 return ret;
400
401 irq = platform_get_irq(pdev, 0);
402 if (irq <= 0) {
403 dev_err(iproc_i2c->device, "no irq resource\n");
404 return irq;
405 }
406 iproc_i2c->irq = irq;
407
408 ret = devm_request_irq(iproc_i2c->device, irq, bcm_iproc_i2c_isr, 0,
409 pdev->name, iproc_i2c);
410 if (ret < 0) {
411 dev_err(iproc_i2c->device, "unable to request irq %i\n", irq);
412 return ret;
413 }
414
415 bcm_iproc_i2c_enable_disable(iproc_i2c, true);
416
417 adap = &iproc_i2c->adapter;
418 i2c_set_adapdata(adap, iproc_i2c);
419 strlcpy(adap->name, "Broadcom iProc I2C adapter", sizeof(adap->name));
420 adap->algo = &bcm_iproc_algo;
Wolfram Sang338f1ab2015-01-07 12:24:10 +0100421 adap->quirks = &bcm_iproc_i2c_quirks;
Ray Juie6e5dd32015-02-07 21:25:24 -0800422 adap->dev.parent = &pdev->dev;
423 adap->dev.of_node = pdev->dev.of_node;
424
425 ret = i2c_add_adapter(adap);
426 if (ret) {
427 dev_err(iproc_i2c->device, "failed to add adapter\n");
428 return ret;
429 }
430
431 return 0;
432}
433
434static int bcm_iproc_i2c_remove(struct platform_device *pdev)
435{
436 struct bcm_iproc_i2c_dev *iproc_i2c = platform_get_drvdata(pdev);
437
438 /* make sure there's no pending interrupt when we remove the adapter */
439 writel(0, iproc_i2c->base + IE_OFFSET);
440 readl(iproc_i2c->base + IE_OFFSET);
441 synchronize_irq(iproc_i2c->irq);
442
443 i2c_del_adapter(&iproc_i2c->adapter);
444 bcm_iproc_i2c_enable_disable(iproc_i2c, false);
445
446 return 0;
447}
448
Ray Jui0ee04e92015-05-14 15:36:04 -0700449#ifdef CONFIG_PM_SLEEP
450
451static int bcm_iproc_i2c_suspend(struct device *dev)
452{
453 struct platform_device *pdev = to_platform_device(dev);
454 struct bcm_iproc_i2c_dev *iproc_i2c = platform_get_drvdata(pdev);
455
456 /* make sure there's no pending interrupt when we go into suspend */
457 writel(0, iproc_i2c->base + IE_OFFSET);
458 readl(iproc_i2c->base + IE_OFFSET);
459 synchronize_irq(iproc_i2c->irq);
460
461 /* now disable the controller */
462 bcm_iproc_i2c_enable_disable(iproc_i2c, false);
463
464 return 0;
465}
466
467static int bcm_iproc_i2c_resume(struct device *dev)
468{
469 struct platform_device *pdev = to_platform_device(dev);
470 struct bcm_iproc_i2c_dev *iproc_i2c = platform_get_drvdata(pdev);
471 int ret;
472 u32 val;
473
474 /*
475 * Power domain could have been shut off completely in system deep
476 * sleep, so re-initialize the block here
477 */
478 ret = bcm_iproc_i2c_init(iproc_i2c);
479 if (ret)
480 return ret;
481
482 /* configure to the desired bus speed */
483 val = readl(iproc_i2c->base + TIM_CFG_OFFSET);
484 val &= ~(1 << TIM_CFG_MODE_400_SHIFT);
485 val |= (iproc_i2c->bus_speed == 400000) << TIM_CFG_MODE_400_SHIFT;
486 writel(val, iproc_i2c->base + TIM_CFG_OFFSET);
487
488 bcm_iproc_i2c_enable_disable(iproc_i2c, true);
489
490 return 0;
491}
492
493static const struct dev_pm_ops bcm_iproc_i2c_pm_ops = {
494 .suspend_late = &bcm_iproc_i2c_suspend,
495 .resume_early = &bcm_iproc_i2c_resume
496};
497
498#define BCM_IPROC_I2C_PM_OPS (&bcm_iproc_i2c_pm_ops)
499#else
500#define BCM_IPROC_I2C_PM_OPS NULL
501#endif /* CONFIG_PM_SLEEP */
502
Ray Juie6e5dd32015-02-07 21:25:24 -0800503static const struct of_device_id bcm_iproc_i2c_of_match[] = {
504 { .compatible = "brcm,iproc-i2c" },
505 { /* sentinel */ }
506};
507MODULE_DEVICE_TABLE(of, bcm_iproc_i2c_of_match);
508
509static struct platform_driver bcm_iproc_i2c_driver = {
510 .driver = {
511 .name = "bcm-iproc-i2c",
512 .of_match_table = bcm_iproc_i2c_of_match,
Ray Jui0ee04e92015-05-14 15:36:04 -0700513 .pm = BCM_IPROC_I2C_PM_OPS,
Ray Juie6e5dd32015-02-07 21:25:24 -0800514 },
515 .probe = bcm_iproc_i2c_probe,
516 .remove = bcm_iproc_i2c_remove,
517};
518module_platform_driver(bcm_iproc_i2c_driver);
519
520MODULE_AUTHOR("Ray Jui <rjui@broadcom.com>");
521MODULE_DESCRIPTION("Broadcom iProc I2C Driver");
522MODULE_LICENSE("GPL v2");