2 * This driver implements I2C master functionality using the LSI API2C
5 * NOTE: The controller has a limitation in that it can only do transfers of
6 * maximum 255 bytes at a time. If a larger transfer is attempted, error code
7 * (-EINVAL) is returned.
9 * This software is licensed under the terms of the GNU General Public
10 * License version 2, as published by the Free Software Foundation, and
11 * may be copied, distributed, and modified under those terms.
13 #include <linux/clk.h>
14 #include <linux/clkdev.h>
15 #include <linux/err.h>
16 #include <linux/i2c.h>
17 #include <linux/init.h>
18 #include <linux/interrupt.h>
19 #include <linux/module.h>
21 #include <linux/kernel.h>
22 #include <linux/platform_device.h>
24 #define SCL_WAIT_TIMEOUT_NS 25000000
25 #define I2C_XFER_TIMEOUT (msecs_to_jiffies(250))
26 #define I2C_STOP_TIMEOUT (msecs_to_jiffies(100))
29 #define GLOBAL_CONTROL 0x00
30 #define GLOBAL_MST_EN BIT(0)
31 #define GLOBAL_SLV_EN BIT(1)
32 #define GLOBAL_IBML_EN BIT(2)
33 #define INTERRUPT_STATUS 0x04
34 #define INTERRUPT_ENABLE 0x08
35 #define INT_SLV BIT(1)
36 #define INT_MST BIT(0)
37 #define WAIT_TIMER_CONTROL 0x0c
39 #define WT_VALUE(_x) ((_x) & 0x7fff)
40 #define IBML_TIMEOUT 0x10
41 #define IBML_LOW_MEXT 0x14
42 #define IBML_LOW_SEXT 0x18
43 #define TIMER_CLOCK_DIV 0x1c
44 #define I2C_BUS_MONITOR 0x20
45 #define BM_SDAC BIT(3)
46 #define BM_SCLC BIT(2)
47 #define BM_SDAS BIT(1)
48 #define BM_SCLS BIT(0)
49 #define SOFT_RESET 0x24
50 #define MST_COMMAND 0x28
51 #define CMD_BUSY (1<<3)
52 #define CMD_MANUAL (0x00 | CMD_BUSY)
53 #define CMD_AUTO (0x01 | CMD_BUSY)
54 #define MST_RX_XFER 0x2c
55 #define MST_TX_XFER 0x30
56 #define MST_ADDR_1 0x34
57 #define MST_ADDR_2 0x38
59 #define MST_TX_FIFO 0x40
60 #define MST_RX_FIFO 0x44
61 #define MST_INT_ENABLE 0x48
62 #define MST_INT_STATUS 0x4c
63 #define MST_STATUS_RFL (1 << 13) /* RX FIFO serivce */
64 #define MST_STATUS_TFL (1 << 12) /* TX FIFO service */
65 #define MST_STATUS_SNS (1 << 11) /* Manual mode done */
66 #define MST_STATUS_SS (1 << 10) /* Automatic mode done */
67 #define MST_STATUS_SCC (1 << 9) /* Stop complete */
68 #define MST_STATUS_IP (1 << 8) /* Invalid parameter */
69 #define MST_STATUS_TSS (1 << 7) /* Timeout */
70 #define MST_STATUS_AL (1 << 6) /* Arbitration lost */
71 #define MST_STATUS_ND (1 << 5) /* NAK on data phase */
72 #define MST_STATUS_NA (1 << 4) /* NAK on address phase */
73 #define MST_STATUS_NAK (MST_STATUS_NA | \
75 #define MST_STATUS_ERR (MST_STATUS_NAK | \
78 #define MST_TX_BYTES_XFRD 0x50
79 #define MST_RX_BYTES_XFRD 0x54
80 #define SCL_HIGH_PERIOD 0x80
81 #define SCL_LOW_PERIOD 0x84
82 #define SPIKE_FLTR_LEN 0x88
83 #define SDA_SETUP_TIME 0x8c
84 #define SDA_HOLD_TIME 0x90
87 * axxia_i2c_dev - I2C device context
88 * @base: pointer to register struct
89 * @msg: pointer to current message
90 * @msg_xfrd: number of bytes transferred in msg
91 * @msg_err: error code for completed message
92 * @msg_complete: xfer completion object
93 * @dev: device reference
94 * @adapter: core i2c abstraction
95 * @i2c_clk: clock reference for i2c input clock
96 * @bus_clk_rate: current i2c bus clock rate
98 struct axxia_i2c_dev
{
103 struct completion msg_complete
;
105 struct i2c_adapter adapter
;
110 static void i2c_int_disable(struct axxia_i2c_dev
*idev
, u32 mask
)
114 int_en
= readl(idev
->base
+ MST_INT_ENABLE
);
115 writel(int_en
& ~mask
, idev
->base
+ MST_INT_ENABLE
);
118 static void i2c_int_enable(struct axxia_i2c_dev
*idev
, u32 mask
)
122 int_en
= readl(idev
->base
+ MST_INT_ENABLE
);
123 writel(int_en
| mask
, idev
->base
+ MST_INT_ENABLE
);
127 * ns_to_clk - Convert time (ns) to clock cycles for the given clock frequency.
129 static u32
ns_to_clk(u64 ns
, u32 clk_mhz
)
131 return div_u64(ns
* clk_mhz
, 1000);
134 static int axxia_i2c_init(struct axxia_i2c_dev
*idev
)
136 u32 divisor
= clk_get_rate(idev
->i2c_clk
) / idev
->bus_clk_rate
;
137 u32 clk_mhz
= clk_get_rate(idev
->i2c_clk
) / 1000000;
142 unsigned long timeout
;
144 dev_dbg(idev
->dev
, "rate=%uHz per_clk=%uMHz -> ratio=1:%u\n",
145 idev
->bus_clk_rate
, clk_mhz
, divisor
);
147 /* Reset controller */
148 writel(0x01, idev
->base
+ SOFT_RESET
);
149 timeout
= jiffies
+ msecs_to_jiffies(100);
150 while (readl(idev
->base
+ SOFT_RESET
) & 1) {
151 if (time_after(jiffies
, timeout
)) {
152 dev_warn(idev
->dev
, "Soft reset failed\n");
157 /* Enable Master Mode */
158 writel(0x1, idev
->base
+ GLOBAL_CONTROL
);
160 if (idev
->bus_clk_rate
<= 100000) {
161 /* Standard mode SCL 50/50, tSU:DAT = 250 ns */
162 t_high
= divisor
* 1 / 2;
163 t_low
= divisor
* 1 / 2;
164 t_setup
= ns_to_clk(250, clk_mhz
);
166 /* Fast mode SCL 33/66, tSU:DAT = 100 ns */
167 t_high
= divisor
* 1 / 3;
168 t_low
= divisor
* 2 / 3;
169 t_setup
= ns_to_clk(100, clk_mhz
);
173 writel(t_high
, idev
->base
+ SCL_HIGH_PERIOD
);
175 writel(t_low
, idev
->base
+ SCL_LOW_PERIOD
);
177 writel(t_setup
, idev
->base
+ SDA_SETUP_TIME
);
178 /* SDA Hold Time, 300ns */
179 writel(ns_to_clk(300, clk_mhz
), idev
->base
+ SDA_HOLD_TIME
);
180 /* Filter <50ns spikes */
181 writel(ns_to_clk(50, clk_mhz
), idev
->base
+ SPIKE_FLTR_LEN
);
183 /* Configure Time-Out Registers */
184 tmo_clk
= ns_to_clk(SCL_WAIT_TIMEOUT_NS
, clk_mhz
);
186 /* Find prescaler value that makes tmo_clk fit in 15-bits counter. */
187 for (prescale
= 0; prescale
< 15; ++prescale
) {
188 if (tmo_clk
<= 0x7fff)
192 if (tmo_clk
> 0x7fff)
195 /* Prescale divider (log2) */
196 writel(prescale
, idev
->base
+ TIMER_CLOCK_DIV
);
197 /* Timeout in divided clocks */
198 writel(WT_EN
| WT_VALUE(tmo_clk
), idev
->base
+ WAIT_TIMER_CONTROL
);
200 /* Mask all master interrupt bits */
201 i2c_int_disable(idev
, ~0);
203 /* Interrupt enable */
204 writel(0x01, idev
->base
+ INTERRUPT_ENABLE
);
209 static int i2c_m_rd(const struct i2c_msg
*msg
)
211 return (msg
->flags
& I2C_M_RD
) != 0;
214 static int i2c_m_ten(const struct i2c_msg
*msg
)
216 return (msg
->flags
& I2C_M_TEN
) != 0;
219 static int i2c_m_recv_len(const struct i2c_msg
*msg
)
221 return (msg
->flags
& I2C_M_RECV_LEN
) != 0;
225 * axxia_i2c_empty_rx_fifo - Fetch data from RX FIFO and update SMBus block
226 * transfer length if this is the first byte of such a transfer.
228 static int axxia_i2c_empty_rx_fifo(struct axxia_i2c_dev
*idev
)
230 struct i2c_msg
*msg
= idev
->msg
;
231 size_t rx_fifo_avail
= readl(idev
->base
+ MST_RX_FIFO
);
232 int bytes_to_transfer
= min(rx_fifo_avail
, msg
->len
- idev
->msg_xfrd
);
234 while (bytes_to_transfer
-- > 0) {
235 int c
= readl(idev
->base
+ MST_DATA
);
237 if (idev
->msg_xfrd
== 0 && i2c_m_recv_len(msg
)) {
239 * Check length byte for SMBus block read
241 if (c
<= 0 || c
> I2C_SMBUS_BLOCK_MAX
) {
242 idev
->msg_err
= -EPROTO
;
243 i2c_int_disable(idev
, ~MST_STATUS_TSS
);
244 complete(&idev
->msg_complete
);
248 writel(msg
->len
, idev
->base
+ MST_RX_XFER
);
250 msg
->buf
[idev
->msg_xfrd
++] = c
;
257 * axxia_i2c_fill_tx_fifo - Fill TX FIFO from current message buffer.
258 * @return: Number of bytes left to transfer.
260 static int axxia_i2c_fill_tx_fifo(struct axxia_i2c_dev
*idev
)
262 struct i2c_msg
*msg
= idev
->msg
;
263 size_t tx_fifo_avail
= FIFO_SIZE
- readl(idev
->base
+ MST_TX_FIFO
);
264 int bytes_to_transfer
= min(tx_fifo_avail
, msg
->len
- idev
->msg_xfrd
);
265 int ret
= msg
->len
- idev
->msg_xfrd
- bytes_to_transfer
;
267 while (bytes_to_transfer
-- > 0)
268 writel(msg
->buf
[idev
->msg_xfrd
++], idev
->base
+ MST_DATA
);
273 static irqreturn_t
axxia_i2c_isr(int irq
, void *_dev
)
275 struct axxia_i2c_dev
*idev
= _dev
;
278 if (!(readl(idev
->base
+ INTERRUPT_STATUS
) & INT_MST
))
281 /* Read interrupt status bits */
282 status
= readl(idev
->base
+ MST_INT_STATUS
);
285 dev_warn(idev
->dev
, "unexpected interrupt\n");
289 /* RX FIFO needs service? */
290 if (i2c_m_rd(idev
->msg
) && (status
& MST_STATUS_RFL
))
291 axxia_i2c_empty_rx_fifo(idev
);
293 /* TX FIFO needs service? */
294 if (!i2c_m_rd(idev
->msg
) && (status
& MST_STATUS_TFL
)) {
295 if (axxia_i2c_fill_tx_fifo(idev
) == 0)
296 i2c_int_disable(idev
, MST_STATUS_TFL
);
299 if (unlikely(status
& MST_STATUS_ERR
)) {
301 i2c_int_disable(idev
, ~0);
302 if (status
& MST_STATUS_AL
)
303 idev
->msg_err
= -EAGAIN
;
304 else if (status
& MST_STATUS_NAK
)
305 idev
->msg_err
= -ENXIO
;
307 idev
->msg_err
= -EIO
;
308 dev_dbg(idev
->dev
, "error %#x, addr=%#x rx=%u/%u tx=%u/%u\n",
311 readl(idev
->base
+ MST_RX_BYTES_XFRD
),
312 readl(idev
->base
+ MST_RX_XFER
),
313 readl(idev
->base
+ MST_TX_BYTES_XFRD
),
314 readl(idev
->base
+ MST_TX_XFER
));
315 complete(&idev
->msg_complete
);
316 } else if (status
& MST_STATUS_SCC
) {
318 i2c_int_disable(idev
, ~MST_STATUS_TSS
);
319 complete(&idev
->msg_complete
);
320 } else if (status
& MST_STATUS_SNS
) {
322 i2c_int_disable(idev
, ~MST_STATUS_TSS
);
323 if (i2c_m_rd(idev
->msg
) && idev
->msg_xfrd
< idev
->msg
->len
)
324 axxia_i2c_empty_rx_fifo(idev
);
325 complete(&idev
->msg_complete
);
326 } else if (status
& MST_STATUS_TSS
) {
327 /* Transfer timeout */
328 idev
->msg_err
= -ETIMEDOUT
;
329 i2c_int_disable(idev
, ~MST_STATUS_TSS
);
330 complete(&idev
->msg_complete
);
334 /* Clear interrupt */
335 writel(INT_MST
, idev
->base
+ INTERRUPT_STATUS
);
340 static int axxia_i2c_xfer_msg(struct axxia_i2c_dev
*idev
, struct i2c_msg
*msg
)
342 u32 int_mask
= MST_STATUS_ERR
| MST_STATUS_SNS
;
343 u32 rx_xfer
, tx_xfer
;
345 unsigned long time_left
;
346 unsigned int wt_value
;
350 reinit_completion(&idev
->msg_complete
);
352 if (i2c_m_ten(msg
)) {
354 * addr_1: 5'b11110 | addr[9:8] | (R/nW)
357 addr_1
= 0xF0 | ((msg
->addr
>> 7) & 0x06);
359 addr_1
|= 1; /* Set the R/nW bit of the address */
360 addr_2
= msg
->addr
& 0xFF;
363 * addr_1: addr[6:0] | (R/nW)
366 addr_1
= i2c_8bit_addr_from_msg(msg
);
371 /* I2C read transfer */
372 rx_xfer
= i2c_m_recv_len(msg
) ? I2C_SMBUS_BLOCK_MAX
: msg
->len
;
375 /* I2C write transfer */
380 writel(rx_xfer
, idev
->base
+ MST_RX_XFER
);
381 writel(tx_xfer
, idev
->base
+ MST_TX_XFER
);
382 writel(addr_1
, idev
->base
+ MST_ADDR_1
);
383 writel(addr_2
, idev
->base
+ MST_ADDR_2
);
386 int_mask
|= MST_STATUS_RFL
;
387 else if (axxia_i2c_fill_tx_fifo(idev
) != 0)
388 int_mask
|= MST_STATUS_TFL
;
390 wt_value
= WT_VALUE(readl(idev
->base
+ WAIT_TIMER_CONTROL
));
391 /* Disable wait timer temporarly */
392 writel(wt_value
, idev
->base
+ WAIT_TIMER_CONTROL
);
393 /* Check if timeout error happened */
397 /* Start manual mode */
398 writel(CMD_MANUAL
, idev
->base
+ MST_COMMAND
);
400 writel(WT_EN
| wt_value
, idev
->base
+ WAIT_TIMER_CONTROL
);
402 i2c_int_enable(idev
, int_mask
);
404 time_left
= wait_for_completion_timeout(&idev
->msg_complete
,
407 i2c_int_disable(idev
, int_mask
);
409 if (readl(idev
->base
+ MST_COMMAND
) & CMD_BUSY
)
410 dev_warn(idev
->dev
, "busy after xfer\n");
412 if (time_left
== 0) {
413 idev
->msg_err
= -ETIMEDOUT
;
414 i2c_recover_bus(&idev
->adapter
);
415 axxia_i2c_init(idev
);
419 if (unlikely(idev
->msg_err
) && idev
->msg_err
!= -ENXIO
&&
420 idev
->msg_err
!= -ETIMEDOUT
)
421 axxia_i2c_init(idev
);
423 return idev
->msg_err
;
426 static int axxia_i2c_stop(struct axxia_i2c_dev
*idev
)
428 u32 int_mask
= MST_STATUS_ERR
| MST_STATUS_SCC
| MST_STATUS_TSS
;
429 unsigned long time_left
;
431 reinit_completion(&idev
->msg_complete
);
434 writel(0xb, idev
->base
+ MST_COMMAND
);
435 i2c_int_enable(idev
, int_mask
);
436 time_left
= wait_for_completion_timeout(&idev
->msg_complete
,
438 i2c_int_disable(idev
, int_mask
);
442 if (readl(idev
->base
+ MST_COMMAND
) & CMD_BUSY
)
443 dev_warn(idev
->dev
, "busy after stop\n");
449 axxia_i2c_xfer(struct i2c_adapter
*adap
, struct i2c_msg msgs
[], int num
)
451 struct axxia_i2c_dev
*idev
= i2c_get_adapdata(adap
);
456 i2c_int_enable(idev
, MST_STATUS_TSS
);
458 for (i
= 0; ret
== 0 && i
< num
; ++i
)
459 ret
= axxia_i2c_xfer_msg(idev
, &msgs
[i
]);
461 axxia_i2c_stop(idev
);
466 static int axxia_i2c_get_scl(struct i2c_adapter
*adap
)
468 struct axxia_i2c_dev
*idev
= i2c_get_adapdata(adap
);
470 return !!(readl(idev
->base
+ I2C_BUS_MONITOR
) & BM_SCLS
);
473 static void axxia_i2c_set_scl(struct i2c_adapter
*adap
, int val
)
475 struct axxia_i2c_dev
*idev
= i2c_get_adapdata(adap
);
478 /* Preserve SDA Control */
479 tmp
= readl(idev
->base
+ I2C_BUS_MONITOR
) & BM_SDAC
;
482 writel(tmp
, idev
->base
+ I2C_BUS_MONITOR
);
485 static int axxia_i2c_get_sda(struct i2c_adapter
*adap
)
487 struct axxia_i2c_dev
*idev
= i2c_get_adapdata(adap
);
489 return !!(readl(idev
->base
+ I2C_BUS_MONITOR
) & BM_SDAS
);
492 static struct i2c_bus_recovery_info axxia_i2c_recovery_info
= {
493 .recover_bus
= i2c_generic_scl_recovery
,
494 .get_scl
= axxia_i2c_get_scl
,
495 .set_scl
= axxia_i2c_set_scl
,
496 .get_sda
= axxia_i2c_get_sda
,
499 static u32
axxia_i2c_func(struct i2c_adapter
*adap
)
501 u32 caps
= (I2C_FUNC_I2C
| I2C_FUNC_10BIT_ADDR
|
502 I2C_FUNC_SMBUS_EMUL
| I2C_FUNC_SMBUS_BLOCK_DATA
);
506 static const struct i2c_algorithm axxia_i2c_algo
= {
507 .master_xfer
= axxia_i2c_xfer
,
508 .functionality
= axxia_i2c_func
,
511 static const struct i2c_adapter_quirks axxia_i2c_quirks
= {
513 .max_write_len
= 255,
516 static int axxia_i2c_probe(struct platform_device
*pdev
)
518 struct device_node
*np
= pdev
->dev
.of_node
;
519 struct axxia_i2c_dev
*idev
= NULL
;
520 struct resource
*res
;
525 idev
= devm_kzalloc(&pdev
->dev
, sizeof(*idev
), GFP_KERNEL
);
529 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
530 base
= devm_ioremap_resource(&pdev
->dev
, res
);
532 return PTR_ERR(base
);
534 irq
= platform_get_irq(pdev
, 0);
536 dev_err(&pdev
->dev
, "missing interrupt resource\n");
540 idev
->i2c_clk
= devm_clk_get(&pdev
->dev
, "i2c");
541 if (IS_ERR(idev
->i2c_clk
)) {
542 dev_err(&pdev
->dev
, "missing clock\n");
543 return PTR_ERR(idev
->i2c_clk
);
547 idev
->dev
= &pdev
->dev
;
548 init_completion(&idev
->msg_complete
);
550 of_property_read_u32(np
, "clock-frequency", &idev
->bus_clk_rate
);
551 if (idev
->bus_clk_rate
== 0)
552 idev
->bus_clk_rate
= 100000; /* default clock rate */
554 ret
= clk_prepare_enable(idev
->i2c_clk
);
556 dev_err(&pdev
->dev
, "failed to enable clock\n");
560 ret
= axxia_i2c_init(idev
);
562 dev_err(&pdev
->dev
, "failed to initialize\n");
563 goto error_disable_clk
;
566 ret
= devm_request_irq(&pdev
->dev
, irq
, axxia_i2c_isr
, 0,
569 dev_err(&pdev
->dev
, "failed to claim IRQ%d\n", irq
);
570 goto error_disable_clk
;
573 i2c_set_adapdata(&idev
->adapter
, idev
);
574 strlcpy(idev
->adapter
.name
, pdev
->name
, sizeof(idev
->adapter
.name
));
575 idev
->adapter
.owner
= THIS_MODULE
;
576 idev
->adapter
.algo
= &axxia_i2c_algo
;
577 idev
->adapter
.bus_recovery_info
= &axxia_i2c_recovery_info
;
578 idev
->adapter
.quirks
= &axxia_i2c_quirks
;
579 idev
->adapter
.dev
.parent
= &pdev
->dev
;
580 idev
->adapter
.dev
.of_node
= pdev
->dev
.of_node
;
582 platform_set_drvdata(pdev
, idev
);
584 ret
= i2c_add_adapter(&idev
->adapter
);
586 goto error_disable_clk
;
591 clk_disable_unprepare(idev
->i2c_clk
);
595 static int axxia_i2c_remove(struct platform_device
*pdev
)
597 struct axxia_i2c_dev
*idev
= platform_get_drvdata(pdev
);
599 clk_disable_unprepare(idev
->i2c_clk
);
600 i2c_del_adapter(&idev
->adapter
);
605 /* Match table for of_platform binding */
606 static const struct of_device_id axxia_i2c_of_match
[] = {
607 { .compatible
= "lsi,api2c", },
611 MODULE_DEVICE_TABLE(of
, axxia_i2c_of_match
);
613 static struct platform_driver axxia_i2c_driver
= {
614 .probe
= axxia_i2c_probe
,
615 .remove
= axxia_i2c_remove
,
618 .of_match_table
= axxia_i2c_of_match
,
622 module_platform_driver(axxia_i2c_driver
);
624 MODULE_DESCRIPTION("Axxia I2C Bus driver");
625 MODULE_AUTHOR("Anders Berg <anders.berg@lsi.com>");
626 MODULE_LICENSE("GPL v2");