1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright (C) 2022 Hewlett-Packard Enterprise Development Company, L.P. */
7 #include <linux/mod_devicetable.h>
8 #include <linux/module.h>
9 #include <linux/platform_device.h>
10 #include <linux/regmap.h>
11 #include <linux/mfd/syscon.h>
13 #define GXP_MAX_I2C_ENGINE 10
14 static const char * const gxp_i2c_name
[] = {
15 "gxp-i2c0", "gxp-i2c1", "gxp-i2c2", "gxp-i2c3",
16 "gxp-i2c4", "gxp-i2c5", "gxp-i2c6", "gxp-i2c7",
17 "gxp-i2c8", "gxp-i2c9" };
19 /* GXP I2C Global interrupt status/enable register*/
20 #define GXP_I2CINTSTAT 0x00
21 #define GXP_I2CINTEN 0x04
23 /* GXP I2C registers */
24 #define GXP_I2CSTAT 0x00
25 #define MASK_STOP_EVENT 0x20
28 #define GXP_I2CEVTERR 0x01
29 #define MASK_SLAVE_CMD_EVENT 0x01
30 #define MASK_SLAVE_DATA_EVENT 0x02
31 #define MASK_MASTER_EVENT 0x10
32 #define GXP_I2CSNPDAT 0x02
33 #define GXP_I2CMCMD 0x04
34 #define GXP_I2CSCMD 0x06
35 #define GXP_I2CSNPAA 0x09
36 #define GXP_I2CADVFEAT 0x0A
37 #define GXP_I2COWNADR 0x0B
38 #define GXP_I2CFREQDIV 0x0C
39 #define GXP_I2CFLTFAIR 0x0D
40 #define GXP_I2CTMOEDG 0x0E
41 #define GXP_I2CCYCTIM 0x0F
44 #define SNOOP_EVT_CLR 0x80
45 #define SLAVE_EVT_CLR 0x40
46 #define SNOOP_EVT_MASK 0x20
47 #define SLAVE_EVT_MASK 0x10
48 #define SLAVE_ACK_ENAB 0x08
49 #define SLAVE_EVT_STALL 0x01
52 #define MASTER_EVT_CLR 0x80
53 #define MASTER_ACK_ENAB 0x08
56 #define START_CMD 0x01
59 #define GXP_DATA_EDGE_RST_CTRL 0x0a /* 30ns */
62 #define FILTER_CNT 0x30
63 #define FAIRNESS_CNT 0x02
76 struct gxp_i2c_drvdata
{
82 struct completion completion
;
83 struct i2c_adapter adapter
;
84 struct i2c_msg
*curr_msg
;
90 struct i2c_client
*slave
;
91 unsigned char stopped
;
94 static struct regmap
*i2cg_map
;
96 static void gxp_i2c_start(struct gxp_i2c_drvdata
*drvdata
)
100 drvdata
->buf
= drvdata
->curr_msg
->buf
;
101 drvdata
->buf_remaining
= drvdata
->curr_msg
->len
;
103 /* Note: Address in struct i2c_msg is 7 bits */
104 value
= drvdata
->curr_msg
->addr
<< 9;
107 value
|= drvdata
->curr_msg
->flags
& I2C_M_RD
? RW_CMD
| START_CMD
: START_CMD
;
109 drvdata
->state
= GXP_I2C_ADDR_PHASE
;
110 writew(value
, drvdata
->base
+ GXP_I2CMCMD
);
113 static int gxp_i2c_master_xfer(struct i2c_adapter
*adapter
,
114 struct i2c_msg
*msgs
, int num
)
117 struct gxp_i2c_drvdata
*drvdata
= i2c_get_adapdata(adapter
);
118 unsigned long time_left
;
120 drvdata
->msgs_remaining
= num
;
121 drvdata
->curr_msg
= msgs
;
122 drvdata
->msgs_num
= num
;
123 reinit_completion(&drvdata
->completion
);
125 gxp_i2c_start(drvdata
);
127 time_left
= wait_for_completion_timeout(&drvdata
->completion
,
129 ret
= num
- drvdata
->msgs_remaining
;
133 if (drvdata
->state
== GXP_I2C_ADDR_NACK
)
136 if (drvdata
->state
== GXP_I2C_DATA_NACK
)
142 static u32
gxp_i2c_func(struct i2c_adapter
*adap
)
144 if (IS_ENABLED(CONFIG_I2C_SLAVE
))
145 return I2C_FUNC_I2C
| I2C_FUNC_SMBUS_EMUL
| I2C_FUNC_SLAVE
;
147 return I2C_FUNC_I2C
| I2C_FUNC_SMBUS_EMUL
;
150 #if IS_ENABLED(CONFIG_I2C_SLAVE)
151 static int gxp_i2c_reg_slave(struct i2c_client
*slave
)
153 struct gxp_i2c_drvdata
*drvdata
= i2c_get_adapdata(slave
->adapter
);
158 if (slave
->flags
& I2C_CLIENT_TEN
)
159 return -EAFNOSUPPORT
;
161 drvdata
->slave
= slave
;
163 writeb(slave
->addr
<< 1, drvdata
->base
+ GXP_I2COWNADR
);
164 writeb(SLAVE_EVT_CLR
| SNOOP_EVT_MASK
| SLAVE_ACK_ENAB
|
165 SLAVE_EVT_STALL
, drvdata
->base
+ GXP_I2CSCMD
);
170 static int gxp_i2c_unreg_slave(struct i2c_client
*slave
)
172 struct gxp_i2c_drvdata
*drvdata
= i2c_get_adapdata(slave
->adapter
);
174 WARN_ON(!drvdata
->slave
);
176 writeb(0x00, drvdata
->base
+ GXP_I2COWNADR
);
177 writeb(SNOOP_EVT_CLR
| SLAVE_EVT_CLR
| SNOOP_EVT_MASK
|
178 SLAVE_EVT_MASK
, drvdata
->base
+ GXP_I2CSCMD
);
180 drvdata
->slave
= NULL
;
186 static const struct i2c_algorithm gxp_i2c_algo
= {
187 .master_xfer
= gxp_i2c_master_xfer
,
188 .functionality
= gxp_i2c_func
,
189 #if IS_ENABLED(CONFIG_I2C_SLAVE)
190 .reg_slave
= gxp_i2c_reg_slave
,
191 .unreg_slave
= gxp_i2c_unreg_slave
,
195 static void gxp_i2c_stop(struct gxp_i2c_drvdata
*drvdata
)
197 /* Clear event and send stop */
198 writeb(MASTER_EVT_CLR
| STOP_CMD
, drvdata
->base
+ GXP_I2CMCMD
);
200 complete(&drvdata
->completion
);
203 static void gxp_i2c_restart(struct gxp_i2c_drvdata
*drvdata
)
207 drvdata
->buf
= drvdata
->curr_msg
->buf
;
208 drvdata
->buf_remaining
= drvdata
->curr_msg
->len
;
210 value
= drvdata
->curr_msg
->addr
<< 9;
212 if (drvdata
->curr_msg
->flags
& I2C_M_RD
) {
213 /* Read and clear master event */
214 value
|= MASTER_EVT_CLR
| RW_CMD
| START_CMD
;
216 /* Write and clear master event */
217 value
|= MASTER_EVT_CLR
| START_CMD
;
220 drvdata
->state
= GXP_I2C_ADDR_PHASE
;
222 writew(value
, drvdata
->base
+ GXP_I2CMCMD
);
225 static void gxp_i2c_chk_addr_ack(struct gxp_i2c_drvdata
*drvdata
)
229 value
= readb(drvdata
->base
+ GXP_I2CSTAT
);
230 if (!(value
& MASK_ACK
)) {
231 /* Got no ack, stop */
232 drvdata
->state
= GXP_I2C_ADDR_NACK
;
233 gxp_i2c_stop(drvdata
);
237 if (drvdata
->curr_msg
->flags
& I2C_M_RD
) {
238 /* Start to read data from slave */
239 if (drvdata
->buf_remaining
== 0) {
240 /* No more data to read, stop */
241 drvdata
->msgs_remaining
--;
242 drvdata
->state
= GXP_I2C_COMP
;
243 gxp_i2c_stop(drvdata
);
246 drvdata
->state
= GXP_I2C_RDATA_PHASE
;
248 if (drvdata
->buf_remaining
== 1) {
249 /* The last data, do not ack */
250 writeb(MASTER_EVT_CLR
| RW_CMD
,
251 drvdata
->base
+ GXP_I2CMCMD
);
253 /* Read data and ack it */
254 writeb(MASTER_EVT_CLR
| MASTER_ACK_ENAB
|
255 RW_CMD
, drvdata
->base
+ GXP_I2CMCMD
);
258 /* Start to write first data to slave */
259 if (drvdata
->buf_remaining
== 0) {
260 /* No more data to write, stop */
261 drvdata
->msgs_remaining
--;
262 drvdata
->state
= GXP_I2C_COMP
;
263 gxp_i2c_stop(drvdata
);
266 value
= *drvdata
->buf
;
268 /* Clear master event */
269 value
|= MASTER_EVT_CLR
;
271 drvdata
->buf_remaining
--;
272 drvdata
->state
= GXP_I2C_WDATA_PHASE
;
273 writew(value
, drvdata
->base
+ GXP_I2CMCMD
);
277 static void gxp_i2c_ack_data(struct gxp_i2c_drvdata
*drvdata
)
281 /* Store the data returned */
282 value
= readb(drvdata
->base
+ GXP_I2CSNPDAT
);
283 *drvdata
->buf
= value
;
285 drvdata
->buf_remaining
--;
287 if (drvdata
->buf_remaining
== 0) {
288 /* No more data, this message is completed. */
289 drvdata
->msgs_remaining
--;
291 if (drvdata
->msgs_remaining
== 0) {
292 /* No more messages, stop */
293 drvdata
->state
= GXP_I2C_COMP
;
294 gxp_i2c_stop(drvdata
);
297 /* Move to next message and start transfer */
299 gxp_i2c_restart(drvdata
);
303 /* Ack the slave to make it send next byte */
304 drvdata
->state
= GXP_I2C_RDATA_PHASE
;
305 if (drvdata
->buf_remaining
== 1) {
306 /* The last data, do not ack */
307 writeb(MASTER_EVT_CLR
| RW_CMD
,
308 drvdata
->base
+ GXP_I2CMCMD
);
310 /* Read data and ack it */
311 writeb(MASTER_EVT_CLR
| MASTER_ACK_ENAB
|
312 RW_CMD
, drvdata
->base
+ GXP_I2CMCMD
);
316 static void gxp_i2c_chk_data_ack(struct gxp_i2c_drvdata
*drvdata
)
320 value
= readb(drvdata
->base
+ GXP_I2CSTAT
);
321 if (!(value
& MASK_ACK
)) {
322 /* Received No ack, stop */
323 drvdata
->state
= GXP_I2C_DATA_NACK
;
324 gxp_i2c_stop(drvdata
);
328 /* Got ack, check if there is more data to write */
329 if (drvdata
->buf_remaining
== 0) {
330 /* No more data, this message is completed */
331 drvdata
->msgs_remaining
--;
333 if (drvdata
->msgs_remaining
== 0) {
334 /* No more messages, stop */
335 drvdata
->state
= GXP_I2C_COMP
;
336 gxp_i2c_stop(drvdata
);
339 /* Move to next message and start transfer */
341 gxp_i2c_restart(drvdata
);
345 /* Write data to slave */
346 value
= *drvdata
->buf
;
349 /* Clear master event */
350 value
|= MASTER_EVT_CLR
;
352 drvdata
->buf_remaining
--;
353 drvdata
->state
= GXP_I2C_WDATA_PHASE
;
354 writew(value
, drvdata
->base
+ GXP_I2CMCMD
);
357 static bool gxp_i2c_slave_irq_handler(struct gxp_i2c_drvdata
*drvdata
)
363 value
= readb(drvdata
->base
+ GXP_I2CEVTERR
);
365 /* Received start or stop event */
366 if (value
& MASK_SLAVE_CMD_EVENT
) {
367 value
= readb(drvdata
->base
+ GXP_I2CSTAT
);
368 /* Master sent stop */
369 if (value
& MASK_STOP_EVENT
) {
370 if (drvdata
->stopped
== 0)
371 i2c_slave_event(drvdata
->slave
, I2C_SLAVE_STOP
, &buf
);
372 writeb(SLAVE_EVT_CLR
| SNOOP_EVT_MASK
|
373 SLAVE_ACK_ENAB
| SLAVE_EVT_STALL
, drvdata
->base
+ GXP_I2CSCMD
);
374 drvdata
->stopped
= 1;
376 /* Master sent start and wants to read */
377 drvdata
->stopped
= 0;
378 if (value
& MASK_RW
) {
379 i2c_slave_event(drvdata
->slave
,
380 I2C_SLAVE_READ_REQUESTED
, &buf
);
381 value
= buf
<< 8 | (SLAVE_EVT_CLR
| SNOOP_EVT_MASK
|
383 writew(value
, drvdata
->base
+ GXP_I2CSCMD
);
385 /* Master wants to write to us */
386 ret
= i2c_slave_event(drvdata
->slave
,
387 I2C_SLAVE_WRITE_REQUESTED
, &buf
);
389 /* Ack next byte from master */
390 writeb(SLAVE_EVT_CLR
| SNOOP_EVT_MASK
|
391 SLAVE_ACK_ENAB
| SLAVE_EVT_STALL
,
392 drvdata
->base
+ GXP_I2CSCMD
);
394 /* Nack next byte from master */
395 writeb(SLAVE_EVT_CLR
| SNOOP_EVT_MASK
|
396 SLAVE_EVT_STALL
, drvdata
->base
+ GXP_I2CSCMD
);
400 } else if (value
& MASK_SLAVE_DATA_EVENT
) {
401 value
= readb(drvdata
->base
+ GXP_I2CSTAT
);
402 /* Master wants to read */
403 if (value
& MASK_RW
) {
404 /* Master wants another byte */
405 if (value
& MASK_ACK
) {
406 i2c_slave_event(drvdata
->slave
,
407 I2C_SLAVE_READ_PROCESSED
, &buf
);
408 value
= buf
<< 8 | (SLAVE_EVT_CLR
| SNOOP_EVT_MASK
|
410 writew(value
, drvdata
->base
+ GXP_I2CSCMD
);
412 /* No more bytes needed */
413 writew(SLAVE_EVT_CLR
| SNOOP_EVT_MASK
|
414 SLAVE_ACK_ENAB
| SLAVE_EVT_STALL
,
415 drvdata
->base
+ GXP_I2CSCMD
);
418 /* Master wants to write to us */
419 value
= readb(drvdata
->base
+ GXP_I2CSNPDAT
);
420 buf
= (uint8_t)value
;
421 ret
= i2c_slave_event(drvdata
->slave
,
422 I2C_SLAVE_WRITE_RECEIVED
, &buf
);
424 /* Ack next byte from master */
425 writeb(SLAVE_EVT_CLR
| SNOOP_EVT_MASK
|
426 SLAVE_ACK_ENAB
| SLAVE_EVT_STALL
,
427 drvdata
->base
+ GXP_I2CSCMD
);
429 /* Nack next byte from master */
430 writeb(SLAVE_EVT_CLR
| SNOOP_EVT_MASK
|
431 SLAVE_EVT_STALL
, drvdata
->base
+ GXP_I2CSCMD
);
441 static irqreturn_t
gxp_i2c_irq_handler(int irq
, void *_drvdata
)
443 struct gxp_i2c_drvdata
*drvdata
= (struct gxp_i2c_drvdata
*)_drvdata
;
446 /* Check if the interrupt is for the current engine */
447 regmap_read(i2cg_map
, GXP_I2CINTSTAT
, &value
);
448 if (!(value
& BIT(drvdata
->engine
)))
451 value
= readb(drvdata
->base
+ GXP_I2CEVTERR
);
454 if (value
& ~(MASK_MASTER_EVENT
| MASK_SLAVE_CMD_EVENT
|
455 MASK_SLAVE_DATA_EVENT
)) {
456 /* Clear all events */
457 writeb(0x00, drvdata
->base
+ GXP_I2CEVTERR
);
458 drvdata
->state
= GXP_I2C_ERROR
;
459 gxp_i2c_stop(drvdata
);
463 if (IS_ENABLED(CONFIG_I2C_SLAVE
)) {
465 if (value
& (MASK_SLAVE_CMD_EVENT
| MASK_SLAVE_DATA_EVENT
)) {
466 if (gxp_i2c_slave_irq_handler(drvdata
))
473 switch (drvdata
->state
) {
474 case GXP_I2C_ADDR_PHASE
:
475 gxp_i2c_chk_addr_ack(drvdata
);
478 case GXP_I2C_RDATA_PHASE
:
479 gxp_i2c_ack_data(drvdata
);
482 case GXP_I2C_WDATA_PHASE
:
483 gxp_i2c_chk_data_ack(drvdata
);
490 static void gxp_i2c_init(struct gxp_i2c_drvdata
*drvdata
)
492 drvdata
->state
= GXP_I2C_IDLE
;
493 writeb(2000000 / drvdata
->t
.bus_freq_hz
,
494 drvdata
->base
+ GXP_I2CFREQDIV
);
495 writeb(FILTER_CNT
| FAIRNESS_CNT
,
496 drvdata
->base
+ GXP_I2CFLTFAIR
);
497 writeb(GXP_DATA_EDGE_RST_CTRL
, drvdata
->base
+ GXP_I2CTMOEDG
);
498 writeb(0x00, drvdata
->base
+ GXP_I2CCYCTIM
);
499 writeb(0x00, drvdata
->base
+ GXP_I2CSNPAA
);
500 writeb(0x00, drvdata
->base
+ GXP_I2CADVFEAT
);
501 writeb(SNOOP_EVT_CLR
| SLAVE_EVT_CLR
| SNOOP_EVT_MASK
|
502 SLAVE_EVT_MASK
, drvdata
->base
+ GXP_I2CSCMD
);
503 writeb(MASTER_EVT_CLR
, drvdata
->base
+ GXP_I2CMCMD
);
504 writeb(0x00, drvdata
->base
+ GXP_I2CEVTERR
);
505 writeb(0x00, drvdata
->base
+ GXP_I2COWNADR
);
508 static int gxp_i2c_probe(struct platform_device
*pdev
)
510 struct gxp_i2c_drvdata
*drvdata
;
512 struct i2c_adapter
*adapter
;
515 i2cg_map
= syscon_regmap_lookup_by_phandle(pdev
->dev
.of_node
,
517 if (IS_ERR(i2cg_map
)) {
518 return dev_err_probe(&pdev
->dev
, PTR_ERR(i2cg_map
),
519 "failed to map i2cg_handle\n");
522 /* Disable interrupt */
523 regmap_update_bits(i2cg_map
, GXP_I2CINTEN
, 0x00000FFF, 0);
526 drvdata
= devm_kzalloc(&pdev
->dev
, sizeof(*drvdata
),
531 platform_set_drvdata(pdev
, drvdata
);
532 drvdata
->dev
= &pdev
->dev
;
533 init_completion(&drvdata
->completion
);
535 drvdata
->base
= devm_platform_ioremap_resource(pdev
, 0);
536 if (IS_ERR(drvdata
->base
))
537 return PTR_ERR(drvdata
->base
);
539 /* Use physical memory address to determine which I2C engine this is. */
540 drvdata
->engine
= ((size_t)drvdata
->base
& 0xf00) >> 8;
542 if (drvdata
->engine
>= GXP_MAX_I2C_ENGINE
) {
543 return dev_err_probe(&pdev
->dev
, -EINVAL
, "i2c engine% is unsupported\n",
547 rc
= platform_get_irq(pdev
, 0);
552 rc
= devm_request_irq(&pdev
->dev
, drvdata
->irq
, gxp_i2c_irq_handler
,
553 IRQF_SHARED
, gxp_i2c_name
[drvdata
->engine
], drvdata
);
555 return dev_err_probe(&pdev
->dev
, rc
, "irq request failed\n");
557 i2c_parse_fw_timings(&pdev
->dev
, &drvdata
->t
, true);
559 gxp_i2c_init(drvdata
);
561 /* Enable interrupt */
562 regmap_update_bits(i2cg_map
, GXP_I2CINTEN
, BIT(drvdata
->engine
),
563 BIT(drvdata
->engine
));
565 adapter
= &drvdata
->adapter
;
566 i2c_set_adapdata(adapter
, drvdata
);
568 adapter
->owner
= THIS_MODULE
;
569 strscpy(adapter
->name
, "HPE GXP I2C adapter", sizeof(adapter
->name
));
570 adapter
->algo
= &gxp_i2c_algo
;
571 adapter
->dev
.parent
= &pdev
->dev
;
572 adapter
->dev
.of_node
= pdev
->dev
.of_node
;
574 rc
= i2c_add_adapter(adapter
);
576 return dev_err_probe(&pdev
->dev
, rc
, "i2c add adapter failed\n");
581 static void gxp_i2c_remove(struct platform_device
*pdev
)
583 struct gxp_i2c_drvdata
*drvdata
= platform_get_drvdata(pdev
);
585 /* Disable interrupt */
586 regmap_update_bits(i2cg_map
, GXP_I2CINTEN
, BIT(drvdata
->engine
), 0);
587 i2c_del_adapter(&drvdata
->adapter
);
590 static const struct of_device_id gxp_i2c_of_match
[] = {
591 { .compatible
= "hpe,gxp-i2c" },
594 MODULE_DEVICE_TABLE(of
, gxp_i2c_of_match
);
596 static struct platform_driver gxp_i2c_driver
= {
597 .probe
= gxp_i2c_probe
,
598 .remove
= gxp_i2c_remove
,
601 .of_match_table
= gxp_i2c_of_match
,
604 module_platform_driver(gxp_i2c_driver
);
606 MODULE_AUTHOR("Nick Hawkins <nick.hawkins@hpe.com>");
607 MODULE_DESCRIPTION("HPE GXP I2C bus driver");
608 MODULE_LICENSE("GPL");