1 // SPDX-License-Identifier: GPL-2.0
3 * Microchip CoreI2C I2C controller driver
5 * Copyright (c) 2018-2022 Microchip Corporation. All rights reserved.
7 * Author: Daire McNamara <daire.mcnamara@microchip.com>
8 * Author: Conor Dooley <conor.dooley@microchip.com>
10 #include <linux/clk.h>
11 #include <linux/clkdev.h>
12 #include <linux/err.h>
13 #include <linux/i2c.h>
14 #include <linux/interrupt.h>
16 #include <linux/kernel.h>
17 #include <linux/module.h>
18 #include <linux/platform_device.h>
20 #define CORE_I2C_CTRL (0x00)
21 #define CTRL_CR0 BIT(0)
22 #define CTRL_CR1 BIT(1)
23 #define CTRL_AA BIT(2)
24 #define CTRL_SI BIT(3)
25 #define CTRL_STO BIT(4)
26 #define CTRL_STA BIT(5)
27 #define CTRL_ENS1 BIT(6)
28 #define CTRL_CR2 BIT(7)
30 #define STATUS_BUS_ERROR (0x00)
31 #define STATUS_M_START_SENT (0x08)
32 #define STATUS_M_REPEATED_START_SENT (0x10)
33 #define STATUS_M_SLAW_ACK (0x18)
34 #define STATUS_M_SLAW_NACK (0x20)
35 #define STATUS_M_TX_DATA_ACK (0x28)
36 #define STATUS_M_TX_DATA_NACK (0x30)
37 #define STATUS_M_ARB_LOST (0x38)
38 #define STATUS_M_SLAR_ACK (0x40)
39 #define STATUS_M_SLAR_NACK (0x48)
40 #define STATUS_M_RX_DATA_ACKED (0x50)
41 #define STATUS_M_RX_DATA_NACKED (0x58)
42 #define STATUS_S_SLAW_ACKED (0x60)
43 #define STATUS_S_ARB_LOST_SLAW_ACKED (0x68)
44 #define STATUS_S_GENERAL_CALL_ACKED (0x70)
45 #define STATUS_S_ARB_LOST_GENERAL_CALL_ACKED (0x78)
46 #define STATUS_S_RX_DATA_ACKED (0x80)
47 #define STATUS_S_RX_DATA_NACKED (0x88)
48 #define STATUS_S_GENERAL_CALL_RX_DATA_ACKED (0x90)
49 #define STATUS_S_GENERAL_CALL_RX_DATA_NACKED (0x98)
50 #define STATUS_S_RX_STOP (0xA0)
51 #define STATUS_S_SLAR_ACKED (0xA8)
52 #define STATUS_S_ARB_LOST_SLAR_ACKED (0xB0)
53 #define STATUS_S_TX_DATA_ACK (0xB8)
54 #define STATUS_S_TX_DATA_NACK (0xC0)
55 #define STATUS_LAST_DATA_ACK (0xC8)
56 #define STATUS_M_SMB_MASTER_RESET (0xD0)
57 #define STATUS_S_SCL_LOW_TIMEOUT (0xD8) /* 25 ms */
58 #define STATUS_NO_STATE_INFO (0xF8)
60 #define CORE_I2C_STATUS (0x04)
61 #define CORE_I2C_DATA (0x08)
62 #define WRITE_BIT (0x0)
63 #define READ_BIT (0x1)
64 #define SLAVE_ADDR_SHIFT (1)
65 #define CORE_I2C_SLAVE0_ADDR (0x0c)
66 #define GENERAL_CALL_BIT (0x0)
67 #define CORE_I2C_SMBUS (0x10)
68 #define SMBALERT_INT_ENB (0x0)
69 #define SMBSUS_INT_ENB (0x1)
70 #define SMBUS_ENB (0x2)
71 #define SMBALERT_NI_STATUS (0x3)
72 #define SMBALERT_NO_CTRL (0x4)
73 #define SMBSUS_NI_STATUS (0x5)
74 #define SMBSUS_NO_CTRL (0x6)
75 #define SMBUS_RESET (0x7)
76 #define CORE_I2C_FREQ (0x14)
77 #define CORE_I2C_GLITCHREG (0x18)
78 #define CORE_I2C_SLAVE1_ADDR (0x1c)
80 #define PCLK_DIV_960 (CTRL_CR2)
81 #define PCLK_DIV_256 (0)
82 #define PCLK_DIV_224 (CTRL_CR0)
83 #define PCLK_DIV_192 (CTRL_CR1)
84 #define PCLK_DIV_160 (CTRL_CR0 | CTRL_CR1)
85 #define PCLK_DIV_120 (CTRL_CR0 | CTRL_CR2)
86 #define PCLK_DIV_60 (CTRL_CR1 | CTRL_CR2)
87 #define BCLK_DIV_8 (CTRL_CR0 | CTRL_CR1 | CTRL_CR2)
88 #define CLK_MASK (CTRL_CR0 | CTRL_CR1 | CTRL_CR2)
91 * struct mchp_corei2c_dev - Microchip CoreI2C device private data
93 * @base: pointer to register struct
94 * @dev: device reference
95 * @i2c_clk: clock reference for i2c input clock
96 * @buf: pointer to msg buffer for easier use
97 * @msg_complete: xfer completion object
98 * @adapter: core i2c abstraction
99 * @msg_err: error code for completed message
100 * @bus_clk_rate: current i2c bus clock rate
101 * @isr_status: cached copy of local ISR status
102 * @msg_len: number of bytes transferred in msg
103 * @addr: address of the current slave
105 struct mchp_corei2c_dev
{
110 struct completion msg_complete
;
111 struct i2c_adapter adapter
;
119 static void mchp_corei2c_core_disable(struct mchp_corei2c_dev
*idev
)
121 u8 ctrl
= readb(idev
->base
+ CORE_I2C_CTRL
);
124 writeb(ctrl
, idev
->base
+ CORE_I2C_CTRL
);
127 static void mchp_corei2c_core_enable(struct mchp_corei2c_dev
*idev
)
129 u8 ctrl
= readb(idev
->base
+ CORE_I2C_CTRL
);
132 writeb(ctrl
, idev
->base
+ CORE_I2C_CTRL
);
135 static void mchp_corei2c_reset(struct mchp_corei2c_dev
*idev
)
137 mchp_corei2c_core_disable(idev
);
138 mchp_corei2c_core_enable(idev
);
141 static inline void mchp_corei2c_stop(struct mchp_corei2c_dev
*idev
)
143 u8 ctrl
= readb(idev
->base
+ CORE_I2C_CTRL
);
146 writeb(ctrl
, idev
->base
+ CORE_I2C_CTRL
);
149 static inline int mchp_corei2c_set_divisor(u32 rate
,
150 struct mchp_corei2c_dev
*idev
)
155 clkval
= PCLK_DIV_960
;
156 else if (rate
>= 256)
157 clkval
= PCLK_DIV_256
;
158 else if (rate
>= 224)
159 clkval
= PCLK_DIV_224
;
160 else if (rate
>= 192)
161 clkval
= PCLK_DIV_192
;
162 else if (rate
>= 160)
163 clkval
= PCLK_DIV_160
;
164 else if (rate
>= 120)
165 clkval
= PCLK_DIV_120
;
167 clkval
= PCLK_DIV_60
;
173 ctrl
= readb(idev
->base
+ CORE_I2C_CTRL
);
176 writeb(ctrl
, idev
->base
+ CORE_I2C_CTRL
);
178 ctrl
= readb(idev
->base
+ CORE_I2C_CTRL
);
179 if ((ctrl
& CLK_MASK
) != clkval
)
185 static int mchp_corei2c_init(struct mchp_corei2c_dev
*idev
)
187 u32 clk_rate
= clk_get_rate(idev
->i2c_clk
);
188 u32 divisor
= clk_rate
/ idev
->bus_clk_rate
;
191 ret
= mchp_corei2c_set_divisor(divisor
, idev
);
195 mchp_corei2c_reset(idev
);
200 static void mchp_corei2c_empty_rx(struct mchp_corei2c_dev
*idev
)
204 if (idev
->msg_len
> 0) {
205 *idev
->buf
++ = readb(idev
->base
+ CORE_I2C_DATA
);
209 if (idev
->msg_len
<= 1) {
210 ctrl
= readb(idev
->base
+ CORE_I2C_CTRL
);
212 writeb(ctrl
, idev
->base
+ CORE_I2C_CTRL
);
216 static int mchp_corei2c_fill_tx(struct mchp_corei2c_dev
*idev
)
218 if (idev
->msg_len
> 0)
219 writeb(*idev
->buf
++, idev
->base
+ CORE_I2C_DATA
);
225 static irqreturn_t
mchp_corei2c_handle_isr(struct mchp_corei2c_dev
*idev
)
227 u32 status
= idev
->isr_status
;
229 bool last_byte
= false, finished
= false;
235 case STATUS_M_START_SENT
:
236 case STATUS_M_REPEATED_START_SENT
:
237 ctrl
= readb(idev
->base
+ CORE_I2C_CTRL
);
239 writeb(idev
->addr
, idev
->base
+ CORE_I2C_DATA
);
240 writeb(ctrl
, idev
->base
+ CORE_I2C_CTRL
);
241 if (idev
->msg_len
== 0)
244 case STATUS_M_ARB_LOST
:
245 idev
->msg_err
= -EAGAIN
;
248 case STATUS_M_SLAW_ACK
:
249 case STATUS_M_TX_DATA_ACK
:
250 if (idev
->msg_len
> 0)
251 mchp_corei2c_fill_tx(idev
);
255 case STATUS_M_TX_DATA_NACK
:
256 case STATUS_M_SLAR_NACK
:
257 case STATUS_M_SLAW_NACK
:
258 idev
->msg_err
= -ENXIO
;
261 case STATUS_M_SLAR_ACK
:
262 ctrl
= readb(idev
->base
+ CORE_I2C_CTRL
);
263 if (idev
->msg_len
== 1u) {
265 writeb(ctrl
, idev
->base
+ CORE_I2C_CTRL
);
268 writeb(ctrl
, idev
->base
+ CORE_I2C_CTRL
);
270 if (idev
->msg_len
< 1u)
273 case STATUS_M_RX_DATA_ACKED
:
274 mchp_corei2c_empty_rx(idev
);
276 case STATUS_M_RX_DATA_NACKED
:
277 mchp_corei2c_empty_rx(idev
);
278 if (idev
->msg_len
== 0)
285 /* On the last byte to be transmitted, send STOP */
287 mchp_corei2c_stop(idev
);
289 if (last_byte
|| finished
)
290 complete(&idev
->msg_complete
);
295 static irqreturn_t
mchp_corei2c_isr(int irq
, void *_dev
)
297 struct mchp_corei2c_dev
*idev
= _dev
;
298 irqreturn_t ret
= IRQ_NONE
;
301 ctrl
= readb(idev
->base
+ CORE_I2C_CTRL
);
302 if (ctrl
& CTRL_SI
) {
303 idev
->isr_status
= readb(idev
->base
+ CORE_I2C_STATUS
);
304 ret
= mchp_corei2c_handle_isr(idev
);
307 ctrl
= readb(idev
->base
+ CORE_I2C_CTRL
);
309 writeb(ctrl
, idev
->base
+ CORE_I2C_CTRL
);
314 static int mchp_corei2c_xfer_msg(struct mchp_corei2c_dev
*idev
,
318 unsigned long time_left
;
320 idev
->addr
= i2c_8bit_addr_from_msg(msg
);
321 idev
->msg_len
= msg
->len
;
322 idev
->buf
= msg
->buf
;
325 reinit_completion(&idev
->msg_complete
);
327 mchp_corei2c_core_enable(idev
);
329 ctrl
= readb(idev
->base
+ CORE_I2C_CTRL
);
331 writeb(ctrl
, idev
->base
+ CORE_I2C_CTRL
);
333 time_left
= wait_for_completion_timeout(&idev
->msg_complete
,
334 idev
->adapter
.timeout
);
338 return idev
->msg_err
;
341 static int mchp_corei2c_xfer(struct i2c_adapter
*adap
, struct i2c_msg
*msgs
,
344 struct mchp_corei2c_dev
*idev
= i2c_get_adapdata(adap
);
347 for (i
= 0; i
< num
; i
++) {
348 ret
= mchp_corei2c_xfer_msg(idev
, msgs
++);
356 static u32
mchp_corei2c_func(struct i2c_adapter
*adap
)
358 return I2C_FUNC_I2C
| I2C_FUNC_SMBUS_EMUL
;
361 static const struct i2c_algorithm mchp_corei2c_algo
= {
362 .master_xfer
= mchp_corei2c_xfer
,
363 .functionality
= mchp_corei2c_func
,
366 static int mchp_corei2c_probe(struct platform_device
*pdev
)
368 struct mchp_corei2c_dev
*idev
;
369 struct resource
*res
;
372 idev
= devm_kzalloc(&pdev
->dev
, sizeof(*idev
), GFP_KERNEL
);
376 idev
->base
= devm_platform_get_and_ioremap_resource(pdev
, 0, &res
);
377 if (IS_ERR(idev
->base
))
378 return PTR_ERR(idev
->base
);
380 irq
= platform_get_irq(pdev
, 0);
384 idev
->i2c_clk
= devm_clk_get(&pdev
->dev
, NULL
);
385 if (IS_ERR(idev
->i2c_clk
))
386 return dev_err_probe(&pdev
->dev
, PTR_ERR(idev
->i2c_clk
),
389 idev
->dev
= &pdev
->dev
;
390 init_completion(&idev
->msg_complete
);
392 ret
= device_property_read_u32(idev
->dev
, "clock-frequency",
393 &idev
->bus_clk_rate
);
394 if (ret
|| !idev
->bus_clk_rate
) {
395 dev_info(&pdev
->dev
, "default to 100kHz\n");
396 idev
->bus_clk_rate
= 100000;
399 if (idev
->bus_clk_rate
> 400000)
400 return dev_err_probe(&pdev
->dev
, -EINVAL
,
401 "clock-frequency too high: %d\n",
405 * This driver supports both the hard peripherals & soft FPGA cores.
406 * The hard peripherals do not have shared IRQs, but we don't have
407 * control over what way the interrupts are wired for the soft cores.
409 ret
= devm_request_irq(&pdev
->dev
, irq
, mchp_corei2c_isr
, IRQF_SHARED
,
412 return dev_err_probe(&pdev
->dev
, ret
,
413 "failed to claim irq %d\n", irq
);
415 ret
= clk_prepare_enable(idev
->i2c_clk
);
417 return dev_err_probe(&pdev
->dev
, ret
,
418 "failed to enable clock\n");
420 ret
= mchp_corei2c_init(idev
);
422 clk_disable_unprepare(idev
->i2c_clk
);
423 return dev_err_probe(&pdev
->dev
, ret
, "failed to program clock divider\n");
426 i2c_set_adapdata(&idev
->adapter
, idev
);
427 snprintf(idev
->adapter
.name
, sizeof(idev
->adapter
.name
),
428 "Microchip I2C hw bus at %08lx", (unsigned long)res
->start
);
429 idev
->adapter
.owner
= THIS_MODULE
;
430 idev
->adapter
.algo
= &mchp_corei2c_algo
;
431 idev
->adapter
.dev
.parent
= &pdev
->dev
;
432 idev
->adapter
.dev
.of_node
= pdev
->dev
.of_node
;
433 idev
->adapter
.timeout
= HZ
;
435 platform_set_drvdata(pdev
, idev
);
437 ret
= i2c_add_adapter(&idev
->adapter
);
439 clk_disable_unprepare(idev
->i2c_clk
);
443 dev_info(&pdev
->dev
, "registered CoreI2C bus driver\n");
448 static void mchp_corei2c_remove(struct platform_device
*pdev
)
450 struct mchp_corei2c_dev
*idev
= platform_get_drvdata(pdev
);
452 clk_disable_unprepare(idev
->i2c_clk
);
453 i2c_del_adapter(&idev
->adapter
);
456 static const struct of_device_id mchp_corei2c_of_match
[] = {
457 { .compatible
= "microchip,mpfs-i2c" },
458 { .compatible
= "microchip,corei2c-rtl-v7" },
461 MODULE_DEVICE_TABLE(of
, mchp_corei2c_of_match
);
463 static struct platform_driver mchp_corei2c_driver
= {
464 .probe
= mchp_corei2c_probe
,
465 .remove
= mchp_corei2c_remove
,
467 .name
= "microchip-corei2c",
468 .of_match_table
= mchp_corei2c_of_match
,
472 module_platform_driver(mchp_corei2c_driver
);
474 MODULE_DESCRIPTION("Microchip CoreI2C bus driver");
475 MODULE_AUTHOR("Daire McNamara <daire.mcnamara@microchip.com>");
476 MODULE_AUTHOR("Conor Dooley <conor.dooley@microchip.com>");
477 MODULE_LICENSE("GPL");