1 /*
2 * Copyright (c) 2022 ITE Corporation. All Rights Reserved.
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #define DT_DRV_COMPAT ite_enhance_i2c
8
9 #include <zephyr/drivers/gpio.h>
10 #include <zephyr/drivers/i2c.h>
11 #include <zephyr/drivers/pinctrl.h>
12 #include <zephyr/irq.h>
13 #include <zephyr/kernel.h>
14 #include <zephyr/pm/policy.h>
15 #include <errno.h>
16 #include <soc.h>
17 #include <soc_dt.h>
18 #include <zephyr/sys/util.h>
19
20 #include <zephyr/logging/log.h>
21 LOG_MODULE_REGISTER(i2c_ite_enhance, CONFIG_I2C_LOG_LEVEL);
22
23 #include "i2c-priv.h"
24
25 /* Start smbus session from idle state */
26 #define I2C_MSG_START BIT(5)
27
28 #define I2C_LINE_SCL_HIGH BIT(0)
29 #define I2C_LINE_SDA_HIGH BIT(1)
30 #define I2C_LINE_IDLE (I2C_LINE_SCL_HIGH | I2C_LINE_SDA_HIGH)
31
32 #ifdef CONFIG_I2C_IT8XXX2_CQ_MODE
33 /* Reserved 5 bytes for ID and CMD_x. */
34 #define I2C_CQ_MODE_TX_MAX_PAYLOAD_SIZE (CONFIG_I2C_CQ_MODE_MAX_PAYLOAD_SIZE - 5)
35
36 /* Repeat Start. */
37 #define I2C_CQ_CMD_L_RS BIT(7)
38 /*
39 * R/W (Read/ Write) decides the I2C read or write direction.
40 * 1: read, 0: write
41 */
42 #define I2C_CQ_CMD_L_RW BIT(6)
43 /* P (STOP) is the I2C STOP condition. */
44 #define I2C_CQ_CMD_L_P BIT(5)
45 /* E (End) is this device end flag. */
46 #define I2C_CQ_CMD_L_E BIT(4)
47 /* LA (Last ACK) is Last ACK in master receiver. */
48 #define I2C_CQ_CMD_L_LA BIT(3)
49 /* bit[2:0] are number of transfer out or receive data which depends on R/W. */
50 #define I2C_CQ_CMD_L_NUM_BIT_2_0 GENMASK(2, 0)
51
52 struct i2c_cq_packet {
53 uint8_t id;
54 uint8_t cmd_l;
55 uint8_t cmd_h;
56 FLEXIBLE_ARRAY_DECLARE(uint8_t, wdata);
57 };
58 #endif /* CONFIG_I2C_IT8XXX2_CQ_MODE */
59
60 struct i2c_enhance_config {
61 void (*irq_config_func)(void);
62 uint32_t bitrate;
63 uint8_t *base;
64 uint8_t i2c_irq_base;
65 uint8_t port;
66 uint8_t channel_switch_sel;
67 /* SCL GPIO cells */
68 struct gpio_dt_spec scl_gpios;
69 /* SDA GPIO cells */
70 struct gpio_dt_spec sda_gpios;
71 /* I2C alternate configuration */
72 const struct pinctrl_dev_config *pcfg;
73 uint8_t prescale_scl_low;
74 uint8_t data_hold_time;
75 uint32_t clock_gate_offset;
76 int transfer_timeout_ms;
77 bool target_enable;
78 bool target_pio_mode;
79 bool push_pull_recovery;
80 };
81
82 enum i2c_pin_fun {
83 SCL = 0,
84 SDA,
85 };
86
87 enum i2c_ch_status {
88 I2C_CH_NORMAL = 0,
89 I2C_CH_REPEAT_START,
90 I2C_CH_WAIT_READ,
91 I2C_CH_WAIT_NEXT_XFER,
92 };
93
94 #ifdef CONFIG_I2C_IT8XXX2_CQ_MODE
95 struct i2c_host_cq_buffer {
96 /* Command queue tx payload. */
97 uint8_t i2c_cq_mode_tx_dlm[CONFIG_I2C_CQ_MODE_MAX_PAYLOAD_SIZE] __aligned(4);
98 /* Command queue rx payload. */
99 uint8_t i2c_cq_mode_rx_dlm[CONFIG_I2C_CQ_MODE_MAX_PAYLOAD_SIZE] __aligned(4);
100 };
101 #endif
102 #ifdef CONFIG_I2C_TARGET
103 /*
104 * When accessing data exceeds the maximum buffer, the actual reload address
105 * is one byte more than the maximum buffer size. Therefore, it is necessary to
106 * have a buffer in place to prevent overwriting other memory.
107 */
108 #define PROTECT_MEM_BUF 4
109 struct i2c_target_dma_buffer {
110 /* Target mode DMA output buffer. */
111 uint8_t __aligned(4)
112 out_buffer[CONFIG_I2C_TARGET_IT8XXX2_MAX_BUF_SIZE + PROTECT_MEM_BUF];
113 /* Target mode DMA input buffer. */
114 uint8_t __aligned(4)
115 in_buffer[CONFIG_I2C_TARGET_IT8XXX2_MAX_BUF_SIZE + PROTECT_MEM_BUF];
116 };
117 #endif
118
119 struct i2c_enhance_data {
120 enum i2c_ch_status i2ccs;
121 struct i2c_msg *active_msg;
122 struct k_mutex mutex;
123 struct k_sem device_sync_sem;
124 /* Index into output data */
125 size_t widx;
126 /* Index into input data */
127 size_t ridx;
128 /* operation freq of i2c */
129 uint32_t bus_freq;
130 /* Error code, if any */
131 uint32_t err;
132 /* address of device */
133 uint16_t addr_16bit;
134 /* wait for stop bit interrupt */
135 uint8_t stop;
136 /* Number of messages. */
137 uint8_t num_msgs;
138 #ifdef CONFIG_I2C_IT8XXX2_CQ_MODE
139 /* Store command queue mode messages. */
140 struct i2c_msg *cq_msgs;
141 #endif
142 #ifdef CONFIG_I2C_TARGET
143 struct i2c_target_config *target_cfg;
144 uint32_t buffer_size;
145 int target_nack;
146 bool target_attached;
147 #endif
148 union {
149 #ifdef CONFIG_I2C_IT8XXX2_CQ_MODE
150 struct i2c_host_cq_buffer host_buffer;
151 #endif
152 #ifdef CONFIG_I2C_TARGET
153 struct i2c_target_dma_buffer target_buffer;
154 #endif
155 };
156 };
157
158 enum enhanced_i2c_transfer_direct {
159 TX_DIRECT,
160 RX_DIRECT,
161 };
162
163 enum enhanced_i2c_ctl {
164 /* Hardware reset */
165 E_HW_RST = 0x01,
166 /* Stop */
167 E_STOP = 0x02,
168 /* Start & Repeat start */
169 E_START = 0x04,
170 /* Acknowledge */
171 E_ACK = 0x08,
172 /* State reset */
173 E_STS_RST = 0x10,
174 /* Mode select */
175 E_MODE_SEL = 0x20,
176 /* I2C interrupt enable */
177 E_INT_EN = 0x40,
178 /* 0 : Standard mode , 1 : Receive mode */
179 E_RX_MODE = 0x80,
180 /* State reset and hardware reset */
181 E_STS_AND_HW_RST = (E_STS_RST | E_HW_RST),
182 /* Generate start condition and transmit slave address */
183 E_START_ID = (E_INT_EN | E_MODE_SEL | E_ACK | E_START | E_HW_RST),
184 /* Generate stop condition */
185 E_FINISH = (E_INT_EN | E_MODE_SEL | E_ACK | E_STOP | E_HW_RST),
186 /* Start with command queue mode */
187 E_START_CQ = (E_INT_EN | E_MODE_SEL | E_ACK | E_START),
188 };
189
190 enum enhanced_i2c_host_status {
191 /* ACK receive */
192 E_HOSTA_ACK = 0x01,
193 /* Interrupt pending */
194 E_HOSTA_INTP = 0x02,
195 /* Read/Write */
196 E_HOSTA_RW = 0x04,
197 /* Time out error */
198 E_HOSTA_TMOE = 0x08,
199 /* Arbitration lost */
200 E_HOSTA_ARB = 0x10,
201 /* Bus busy */
202 E_HOSTA_BB = 0x20,
203 /* Address match */
204 E_HOSTA_AM = 0x40,
205 /* Byte done status */
206 E_HOSTA_BDS = 0x80,
207 /* time out or lost arbitration */
208 E_HOSTA_ANY_ERROR = (E_HOSTA_TMOE | E_HOSTA_ARB),
209 /* Byte transfer done and ACK receive */
210 E_HOSTA_BDS_AND_ACK = (E_HOSTA_BDS | E_HOSTA_ACK),
211 };
212
213 enum i2c_reset_cause {
214 I2C_RC_NO_IDLE_FOR_START = 1,
215 I2C_RC_TIMEOUT,
216 };
217
218 #ifdef CONFIG_I2C_TARGET
219 enum enhanced_i2c_target_status {
220 /* Time out error */
221 E_TARGET_TMOE = 0x08,
222 /* Arbitration lost */
223 E_TARGET_ARB = 0x10,
224 /* Time out or lost arbitration */
225 E_TARGET_ANY_ERROR = (E_TARGET_TMOE | E_TARGET_ARB),
226 };
227 #endif
228
i2c_parsing_return_value(const struct device * dev)229 static int i2c_parsing_return_value(const struct device *dev)
230 {
231 struct i2c_enhance_data *data = dev->data;
232
233 if (!data->err) {
234 return 0;
235 }
236
237 /* Connection timed out */
238 if (data->err == ETIMEDOUT) {
239 return -ETIMEDOUT;
240 }
241
242 /* The device does not respond ACK */
243 if (data->err == E_HOSTA_ACK) {
244 return -ENXIO;
245 } else {
246 return -EIO;
247 }
248 }
249
i2c_get_line_levels(const struct device * dev)250 static int i2c_get_line_levels(const struct device *dev)
251 {
252 const struct i2c_enhance_config *config = dev->config;
253 uint8_t *base = config->base;
254 int pin_sts = 0;
255
256 if (IT8XXX2_I2C_TOS(base) & IT8XXX2_I2C_SCL_IN) {
257 pin_sts |= I2C_LINE_SCL_HIGH;
258 }
259
260 if (IT8XXX2_I2C_TOS(base) & IT8XXX2_I2C_SDA_IN) {
261 pin_sts |= I2C_LINE_SDA_HIGH;
262 }
263
264 return pin_sts;
265 }
266
i2c_is_busy(const struct device * dev)267 static int i2c_is_busy(const struct device *dev)
268 {
269 const struct i2c_enhance_config *config = dev->config;
270 uint8_t *base = config->base;
271
272 return (IT8XXX2_I2C_STR(base) & E_HOSTA_BB);
273 }
274
i2c_bus_not_available(const struct device * dev)275 static int i2c_bus_not_available(const struct device *dev)
276 {
277 if (i2c_is_busy(dev) ||
278 (i2c_get_line_levels(dev) != I2C_LINE_IDLE)) {
279 return -EIO;
280 }
281
282 return 0;
283 }
284
i2c_reset(const struct device * dev)285 static void i2c_reset(const struct device *dev)
286 {
287 const struct i2c_enhance_config *config = dev->config;
288 uint8_t *base = config->base;
289
290 /* State reset and hardware reset */
291 IT8XXX2_I2C_CTR(base) = E_STS_AND_HW_RST;
292 }
293
294 /* Set clock frequency for i2c port D, E , or F */
i2c_enhanced_port_set_frequency(const struct device * dev,int freq_hz)295 static void i2c_enhanced_port_set_frequency(const struct device *dev,
296 int freq_hz)
297 {
298 const struct i2c_enhance_config *config = dev->config;
299 uint32_t clk_div, psr, pll_clock, psr_h, psr_l;
300 uint8_t *base = config->base;
301 uint8_t prescale_scl = config->prescale_scl_low;
302
303 pll_clock = chip_get_pll_freq();
304 /*
305 * Let psr(Prescale) = IT8XXX2_I2C_PSR(p_ch)
306 * Then, 1 SCL cycle = 2 x (psr + 2) x SMBus clock cycle
307 * SMBus clock = pll_clock / clk_div
308 * SMBus clock cycle = 1 / SMBus clock
309 * 1 SCL cycle = 1 / freq
310 * 1 / freq = 2 x (psr + 2) x (1 / (pll_clock / clk_div))
311 * psr = ((pll_clock / clk_div) x (1 / freq) x (1 / 2)) - 2
312 */
313 if (freq_hz) {
314 /* Get SMBus clock divide value */
315 clk_div = (IT8XXX2_ECPM_SCDCR2 & 0x0F) + 1U;
316 /* Calculate PSR value */
317 psr = (pll_clock / (clk_div * (2U * freq_hz))) - 2U;
318 /* Set psr value under 0xFD */
319 if (psr > 0xFD) {
320 psr = 0xFD;
321 }
322
323 /* Adjust SCL low period prescale */
324 psr_l = psr + prescale_scl;
325 if (psr_l > 0xFD) {
326 psr_l = 0xFD;
327 LOG_WRN("(psr + prescale_scl) can not be greater than 0xfd.");
328 }
329
330 /*
331 * Adjust SCL high period prescale
332 * The property setting prescale_scl must be less than psr and
333 * the minimum value of psr_h is 2.
334 */
335 if (psr > (prescale_scl + 2)) {
336 psr_h = psr - prescale_scl;
337 } else {
338 psr_h = 2;
339 LOG_WRN("prescale_scl_low should be less than (psr - 2).");
340 }
341
342 /* Set I2C Speed for SCL low period. */
343 IT8XXX2_I2C_PSR(base) = psr_l & 0xFF;
344 /* Set I2C Speed for SCL high period. */
345 IT8XXX2_I2C_HSPR(base) = psr_h & 0xFF;
346 }
347
348 }
349
i2c_enhance_configure(const struct device * dev,uint32_t dev_config_raw)350 static int i2c_enhance_configure(const struct device *dev,
351 uint32_t dev_config_raw)
352 {
353 const struct i2c_enhance_config *config = dev->config;
354 struct i2c_enhance_data *const data = dev->data;
355
356 if (!(I2C_MODE_CONTROLLER & dev_config_raw)) {
357 return -EINVAL;
358 }
359
360 if (I2C_ADDR_10_BITS & dev_config_raw) {
361 return -EINVAL;
362 }
363
364 data->bus_freq = I2C_SPEED_GET(dev_config_raw);
365
366 i2c_enhanced_port_set_frequency(dev, config->bitrate);
367
368 return 0;
369 }
370
i2c_enhance_get_config(const struct device * dev,uint32_t * dev_config)371 static int i2c_enhance_get_config(const struct device *dev, uint32_t *dev_config)
372 {
373 struct i2c_enhance_data *const data = dev->data;
374 uint32_t speed;
375
376 if (!data->bus_freq) {
377 LOG_ERR("The bus frequency is not initially configured.");
378 return -EIO;
379 }
380
381 switch (data->bus_freq) {
382 case I2C_SPEED_DT:
383 case I2C_SPEED_STANDARD:
384 case I2C_SPEED_FAST:
385 case I2C_SPEED_FAST_PLUS:
386 speed = I2C_SPEED_SET(data->bus_freq);
387 break;
388 default:
389 return -ERANGE;
390 }
391
392 *dev_config = (I2C_MODE_CONTROLLER | speed);
393
394 return 0;
395 }
396
enhanced_i2c_error(const struct device * dev)397 static int enhanced_i2c_error(const struct device *dev)
398 {
399 struct i2c_enhance_data *data = dev->data;
400 const struct i2c_enhance_config *config = dev->config;
401 uint8_t *base = config->base;
402 uint32_t i2c_str = IT8XXX2_I2C_STR(base);
403
404 if (i2c_str & E_HOSTA_ANY_ERROR) {
405 data->err = i2c_str & E_HOSTA_ANY_ERROR;
406 /* device does not respond ACK */
407 } else if ((i2c_str & E_HOSTA_BDS_AND_ACK) == E_HOSTA_BDS) {
408 if (IT8XXX2_I2C_CTR(base) & E_ACK) {
409 data->err = E_HOSTA_ACK;
410 /* STOP */
411 IT8XXX2_I2C_CTR(base) = E_FINISH;
412 }
413 }
414
415 return data->err;
416 }
417
enhanced_i2c_start(const struct device * dev)418 static void enhanced_i2c_start(const struct device *dev)
419 {
420 const struct i2c_enhance_config *config = dev->config;
421 uint8_t *base = config->base;
422
423 /* reset i2c port */
424 i2c_reset(dev);
425 /* Set i2c frequency */
426 i2c_enhanced_port_set_frequency(dev, config->bitrate);
427 /*
428 * Set time out register.
429 * I2C D/E/F clock/data low timeout.
430 */
431 IT8XXX2_I2C_TOR(base) = I2C_CLK_LOW_TIMEOUT;
432 /* bit1: Enable enhanced i2c module */
433 IT8XXX2_I2C_CTR1(base) = IT8XXX2_I2C_MDL_EN;
434 }
435
i2c_pio_trans_data(const struct device * dev,enum enhanced_i2c_transfer_direct direct,uint16_t trans_data,int first_byte)436 static void i2c_pio_trans_data(const struct device *dev,
437 enum enhanced_i2c_transfer_direct direct,
438 uint16_t trans_data, int first_byte)
439 {
440 struct i2c_enhance_data *data = dev->data;
441 const struct i2c_enhance_config *config = dev->config;
442 uint8_t *base = config->base;
443 uint32_t nack = 0;
444
445 if (first_byte) {
446 /* First byte must be slave address. */
447 IT8XXX2_I2C_DTR(base) = trans_data |
448 (direct == RX_DIRECT ? BIT(0) : 0);
449 /* start or repeat start signal. */
450 IT8XXX2_I2C_CTR(base) = E_START_ID;
451 } else {
452 if (direct == TX_DIRECT) {
453 /* Transmit data */
454 IT8XXX2_I2C_DTR(base) = (uint8_t)trans_data;
455 } else {
456 /*
457 * Receive data.
458 * Last byte should be NACK in the end of read cycle
459 */
460 if (((data->ridx + 1) == data->active_msg->len) &&
461 (data->active_msg->flags & I2C_MSG_STOP)) {
462 nack = 1;
463 }
464 }
465 /* Set hardware reset to start next transmission */
466 IT8XXX2_I2C_CTR(base) = E_INT_EN | E_MODE_SEL |
467 E_HW_RST | (nack ? 0 : E_ACK);
468 }
469 }
470
enhanced_i2c_tran_read(const struct device * dev)471 static int enhanced_i2c_tran_read(const struct device *dev)
472 {
473 struct i2c_enhance_data *data = dev->data;
474 const struct i2c_enhance_config *config = dev->config;
475 uint8_t *base = config->base;
476 uint8_t in_data = 0;
477
478 if (data->active_msg->flags & I2C_MSG_START) {
479 /* clear start flag */
480 data->active_msg->flags &= ~I2C_MSG_START;
481 enhanced_i2c_start(dev);
482 /* Direct read */
483 data->i2ccs = I2C_CH_WAIT_READ;
484 /* Send ID */
485 i2c_pio_trans_data(dev, RX_DIRECT, data->addr_16bit << 1, 1);
486 } else {
487 if (data->i2ccs) {
488 if (data->i2ccs == I2C_CH_WAIT_READ) {
489 data->i2ccs = I2C_CH_NORMAL;
490 /* Receive data */
491 i2c_pio_trans_data(dev, RX_DIRECT, in_data, 0);
492
493 /* data->active_msg->flags == I2C_MSG_RESTART */
494 } else {
495 /* Write to read */
496 data->i2ccs = I2C_CH_WAIT_READ;
497 /* Send ID */
498 i2c_pio_trans_data(dev, RX_DIRECT,
499 data->addr_16bit << 1, 1);
500 }
501 } else {
502 if (data->ridx < data->active_msg->len) {
503 /* read data */
504 *(data->active_msg->buf++) = IT8XXX2_I2C_DRR(base);
505 data->ridx++;
506 /* done */
507 if (data->ridx == data->active_msg->len) {
508 data->active_msg->len = 0;
509 if (data->active_msg->flags & I2C_MSG_STOP) {
510 data->i2ccs = I2C_CH_NORMAL;
511 IT8XXX2_I2C_CTR(base) = E_FINISH;
512 /* wait for stop bit interrupt */
513 data->stop = 1;
514 return 1;
515 }
516 /* End the transaction */
517 data->i2ccs = I2C_CH_WAIT_READ;
518 return 0;
519 }
520 /* read next byte */
521 i2c_pio_trans_data(dev, RX_DIRECT, in_data, 0);
522 }
523 }
524 }
525 return 1;
526 }
527
enhanced_i2c_tran_write(const struct device * dev)528 static int enhanced_i2c_tran_write(const struct device *dev)
529 {
530 struct i2c_enhance_data *data = dev->data;
531 const struct i2c_enhance_config *config = dev->config;
532 uint8_t *base = config->base;
533 uint8_t out_data;
534
535 if (data->active_msg->flags & I2C_MSG_START) {
536 /* Clear start bit */
537 data->active_msg->flags &= ~I2C_MSG_START;
538 enhanced_i2c_start(dev);
539 /* Send ID */
540 i2c_pio_trans_data(dev, TX_DIRECT, data->addr_16bit << 1, 1);
541 } else {
542 /* Host has completed the transmission of a byte */
543 if (data->widx < data->active_msg->len) {
544 out_data = *(data->active_msg->buf++);
545 data->widx++;
546
547 /* Send Byte */
548 i2c_pio_trans_data(dev, TX_DIRECT, out_data, 0);
549 if (data->i2ccs == I2C_CH_WAIT_NEXT_XFER) {
550 data->i2ccs = I2C_CH_NORMAL;
551 }
552 } else {
553 /* done */
554 data->active_msg->len = 0;
555 if (data->active_msg->flags & I2C_MSG_STOP) {
556 IT8XXX2_I2C_CTR(base) = E_FINISH;
557 /* wait for stop bit interrupt */
558 data->stop = 1;
559 } else {
560 /* Direct write with direct read */
561 data->i2ccs = I2C_CH_WAIT_NEXT_XFER;
562 return 0;
563 }
564 }
565 }
566 return 1;
567 }
568
i2c_transaction(const struct device * dev)569 static int i2c_transaction(const struct device *dev)
570 {
571 struct i2c_enhance_data *data = dev->data;
572 const struct i2c_enhance_config *config = dev->config;
573 uint8_t *base = config->base;
574
575 /* no error */
576 if (!(enhanced_i2c_error(dev))) {
577 if (!data->stop) {
578 /*
579 * The return value indicates if there is more data
580 * to be read or written. If the return value = 1,
581 * it means that the interrupt cannot be disable and
582 * continue to transmit data.
583 */
584 if (data->active_msg->flags & I2C_MSG_READ) {
585 return enhanced_i2c_tran_read(dev);
586 } else {
587 return enhanced_i2c_tran_write(dev);
588 }
589 }
590 }
591 /* reset i2c port */
592 i2c_reset(dev);
593 IT8XXX2_I2C_CTR1(base) = 0;
594
595 data->stop = 0;
596 /* done doing work */
597 return 0;
598 }
599
i2c_enhance_pio_transfer(const struct device * dev,struct i2c_msg * msgs)600 static int i2c_enhance_pio_transfer(const struct device *dev,
601 struct i2c_msg *msgs)
602 {
603 struct i2c_enhance_data *data = dev->data;
604 const struct i2c_enhance_config *config = dev->config;
605 int res;
606
607 if (data->i2ccs == I2C_CH_NORMAL) {
608 struct i2c_msg *start_msg = &msgs[0];
609
610 start_msg->flags |= I2C_MSG_START;
611 }
612
613 for (int i = 0; i < data->num_msgs; i++) {
614
615 data->widx = 0;
616 data->ridx = 0;
617 data->err = 0;
618 data->active_msg = &msgs[i];
619
620 /*
621 * Start transaction.
622 * The return value indicates if the initial configuration
623 * of I2C transaction for read or write has been completed.
624 */
625 if (i2c_transaction(dev)) {
626 /* Enable I2C interrupt. */
627 irq_enable(config->i2c_irq_base);
628 }
629 /* Wait for the transfer to complete */
630 res = k_sem_take(&data->device_sync_sem, K_MSEC(config->transfer_timeout_ms));
631 /*
632 * The irq will be enabled at the condition of start or
633 * repeat start of I2C. If timeout occurs without being
634 * wake up during suspend(ex: interrupt is not fired),
635 * the irq should be disabled immediately.
636 */
637 irq_disable(config->i2c_irq_base);
638 /*
639 * The transaction is dropped on any error(timeout, NACK, fail,
640 * bus error, device error).
641 */
642 if (data->err) {
643 break;
644 }
645
646 if (res != 0) {
647 data->err = ETIMEDOUT;
648 /* reset i2c port */
649 i2c_reset(dev);
650 LOG_ERR("I2C ch%d:0x%X reset cause %d",
651 config->port, data->addr_16bit, I2C_RC_TIMEOUT);
652 /* If this message is sent fail, drop the transaction. */
653 break;
654 }
655 }
656
657 /* reset i2c channel status */
658 if (data->err || (data->active_msg->flags & I2C_MSG_STOP)) {
659 data->i2ccs = I2C_CH_NORMAL;
660 }
661
662 return data->err;
663 }
664
665 #ifdef CONFIG_I2C_IT8XXX2_CQ_MODE
enhanced_i2c_set_cmd_addr_regs(const struct device * dev)666 static void enhanced_i2c_set_cmd_addr_regs(const struct device *dev)
667 {
668 const struct i2c_enhance_config *config = dev->config;
669 struct i2c_enhance_data *data = dev->data;
670 struct i2c_host_cq_buffer *host_buffer = &data->host_buffer;
671 uint32_t dlm_base;
672 uint8_t *base = config->base;
673
674 /* Set "Address Register" to store the I2C data. */
675 dlm_base = (uint32_t)host_buffer->i2c_cq_mode_rx_dlm & 0xffffff;
676 IT8XXX2_I2C_RAMH2A(base) = (dlm_base >> 16) & 0xff;
677 IT8XXX2_I2C_RAMHA(base) = (dlm_base >> 8) & 0xff;
678 IT8XXX2_I2C_RAMLA(base) = dlm_base & 0xff;
679
680 /* Set "Command Address Register" to get commands. */
681 dlm_base = (uint32_t)host_buffer->i2c_cq_mode_tx_dlm & 0xffffff;
682 IT8XXX2_I2C_CMD_ADDH2(base) = (dlm_base >> 16) & 0xff;
683 IT8XXX2_I2C_CMD_ADDH(base) = (dlm_base >> 8) & 0xff;
684 IT8XXX2_I2C_CMD_ADDL(base) = dlm_base & 0xff;
685 }
686
enhanced_i2c_cq_write(const struct device * dev)687 static void enhanced_i2c_cq_write(const struct device *dev)
688 {
689 struct i2c_enhance_data *data = dev->data;
690 struct i2c_host_cq_buffer *host_buffer = &data->host_buffer;
691 struct i2c_cq_packet *i2c_cq_pckt;
692 uint8_t num_bit_2_0 = (data->cq_msgs[0].len - 1) & I2C_CQ_CMD_L_NUM_BIT_2_0;
693 uint8_t num_bit_10_3 = ((data->cq_msgs[0].len - 1) >> 3) & 0xff;
694
695 i2c_cq_pckt = (struct i2c_cq_packet *)host_buffer->i2c_cq_mode_tx_dlm;
696 /* Set commands in RAM. */
697 i2c_cq_pckt->id = data->addr_16bit << 1;
698 i2c_cq_pckt->cmd_l = I2C_CQ_CMD_L_P | I2C_CQ_CMD_L_E | num_bit_2_0;
699 i2c_cq_pckt->cmd_h = num_bit_10_3;
700 for (int i = 0; i < data->cq_msgs[0].len; i++) {
701 i2c_cq_pckt->wdata[i] = data->cq_msgs[0].buf[i];
702 }
703 }
704
enhanced_i2c_cq_read(const struct device * dev)705 static void enhanced_i2c_cq_read(const struct device *dev)
706 {
707 struct i2c_enhance_data *data = dev->data;
708 struct i2c_host_cq_buffer *host_buffer = &data->host_buffer;
709 struct i2c_cq_packet *i2c_cq_pckt;
710 uint8_t num_bit_2_0 = (data->cq_msgs[0].len - 1) & I2C_CQ_CMD_L_NUM_BIT_2_0;
711 uint8_t num_bit_10_3 = ((data->cq_msgs[0].len - 1) >> 3) & 0xff;
712
713 i2c_cq_pckt = (struct i2c_cq_packet *)host_buffer->i2c_cq_mode_tx_dlm;
714 /* Set commands in RAM. */
715 i2c_cq_pckt->id = data->addr_16bit << 1;
716 i2c_cq_pckt->cmd_l = I2C_CQ_CMD_L_RW | I2C_CQ_CMD_L_P |
717 I2C_CQ_CMD_L_E | num_bit_2_0;
718 i2c_cq_pckt->cmd_h = num_bit_10_3;
719 }
720
enhanced_i2c_cq_write_to_read(const struct device * dev)721 static void enhanced_i2c_cq_write_to_read(const struct device *dev)
722 {
723 struct i2c_enhance_data *data = dev->data;
724 struct i2c_host_cq_buffer *host_buffer = &data->host_buffer;
725 struct i2c_cq_packet *i2c_cq_pckt;
726 uint8_t num_bit_2_0 = (data->cq_msgs[0].len - 1) & I2C_CQ_CMD_L_NUM_BIT_2_0;
727 uint8_t num_bit_10_3 = ((data->cq_msgs[0].len - 1) >> 3) & 0xff;
728 int i;
729
730 i2c_cq_pckt = (struct i2c_cq_packet *)host_buffer->i2c_cq_mode_tx_dlm;
731 /* Set commands in RAM. (command byte for write) */
732 i2c_cq_pckt->id = data->addr_16bit << 1;
733 i2c_cq_pckt->cmd_l = num_bit_2_0;
734 i2c_cq_pckt->cmd_h = num_bit_10_3;
735 for (i = 0; i < data->cq_msgs[0].len; i++) {
736 i2c_cq_pckt->wdata[i] = data->cq_msgs[0].buf[i];
737 }
738
739 /* Set commands in RAM. (command byte for read) */
740 num_bit_2_0 = (data->cq_msgs[1].len - 1) & I2C_CQ_CMD_L_NUM_BIT_2_0;
741 num_bit_10_3 = ((data->cq_msgs[1].len - 1) >> 3) & 0xff;
742 i2c_cq_pckt->wdata[i++] = I2C_CQ_CMD_L_RS | I2C_CQ_CMD_L_RW |
743 I2C_CQ_CMD_L_P | I2C_CQ_CMD_L_E | num_bit_2_0;
744 i2c_cq_pckt->wdata[i] = num_bit_10_3;
745 }
746
enhanced_i2c_cq_isr(const struct device * dev)747 static int enhanced_i2c_cq_isr(const struct device *dev)
748 {
749 struct i2c_enhance_data *data = dev->data;
750 struct i2c_host_cq_buffer *host_buffer = &data->host_buffer;
751 const struct i2c_enhance_config *config = dev->config;
752 uint8_t *base = config->base;
753
754 /* Device 1 finish IRQ. */
755 if (IT8XXX2_I2C_FST(base) & IT8XXX2_I2C_FST_DEV1_IRQ) {
756 uint8_t msgs_idx = data->num_msgs - 1;
757
758 /* Get data if this is a read transaction. */
759 if (data->cq_msgs[msgs_idx].flags & I2C_MSG_READ) {
760 for (int i = 0; i < data->cq_msgs[msgs_idx].len; i++) {
761 data->cq_msgs[msgs_idx].buf[i] =
762 host_buffer->i2c_cq_mode_rx_dlm[i];
763 }
764 }
765 } else {
766 /* Device 1 error have occurred. eg. nack, timeout... */
767 if (IT8XXX2_I2C_NST(base) & IT8XXX2_I2C_NST_ID_NACK) {
768 data->err = E_HOSTA_ACK;
769 } else {
770 data->err = IT8XXX2_I2C_STR(base) &
771 E_HOSTA_ANY_ERROR;
772 }
773 }
774 /* Reset bus. */
775 IT8XXX2_I2C_CTR(base) = E_STS_AND_HW_RST;
776 IT8XXX2_I2C_CTR1(base) = 0;
777
778 return 0;
779 }
780
enhanced_i2c_cmd_queue_trans(const struct device * dev)781 static int enhanced_i2c_cmd_queue_trans(const struct device *dev)
782 {
783 struct i2c_enhance_data *data = dev->data;
784 const struct i2c_enhance_config *config = dev->config;
785 uint8_t *base = config->base;
786
787 /* State reset and hardware reset. */
788 IT8XXX2_I2C_CTR(base) = E_STS_AND_HW_RST;
789 /* Set "PSR" registers to decide the i2c speed. */
790 i2c_enhanced_port_set_frequency(dev, config->bitrate);
791 /* Set time out register. port D, E, or F clock/data low timeout. */
792 IT8XXX2_I2C_TOR(base) = I2C_CLK_LOW_TIMEOUT;
793
794 if (data->num_msgs == 2) {
795 /* I2C write to read of command queue mode. */
796 enhanced_i2c_cq_write_to_read(dev);
797 } else {
798 /* I2C read of command queue mode. */
799 if (data->cq_msgs[0].flags & I2C_MSG_READ) {
800 enhanced_i2c_cq_read(dev);
801 /* I2C write of command queue mode. */
802 } else {
803 enhanced_i2c_cq_write(dev);
804 }
805 }
806
807 /* Enable i2c module with command queue mode. */
808 IT8XXX2_I2C_CTR1(base) = IT8XXX2_I2C_MDL_EN | IT8XXX2_I2C_COMQ_EN;
809 /* One shot on device 1. */
810 IT8XXX2_I2C_MODE_SEL(base) = 0;
811 IT8XXX2_I2C_CTR2(base) = 1;
812 /*
813 * The EC processor(CPU) cannot be in the k_cpu_idle() and power
814 * policy during the transactions with the CQ mode(DMA mode).
815 * Otherwise, the EC processor would be clock gated.
816 */
817 chip_block_idle();
818 pm_policy_state_lock_get(PM_STATE_STANDBY, PM_ALL_SUBSTATES);
819 /* Start */
820 IT8XXX2_I2C_CTR(base) = E_START_CQ;
821
822 return 1;
823 }
824
i2c_enhance_cq_transfer(const struct device * dev,struct i2c_msg * msgs)825 static int i2c_enhance_cq_transfer(const struct device *dev,
826 struct i2c_msg *msgs)
827 {
828 struct i2c_enhance_data *data = dev->data;
829 const struct i2c_enhance_config *config = dev->config;
830 int res = 0;
831
832 data->err = 0;
833 data->cq_msgs = msgs;
834
835 /* Start transaction */
836 if (enhanced_i2c_cmd_queue_trans(dev)) {
837 /* Enable i2c interrupt */
838 irq_enable(config->i2c_irq_base);
839 }
840 /* Wait for the transfer to complete */
841 res = k_sem_take(&data->device_sync_sem, K_MSEC(config->transfer_timeout_ms));
842
843 irq_disable(config->i2c_irq_base);
844
845 if (res != 0) {
846 data->err = ETIMEDOUT;
847 /* Reset i2c port. */
848 i2c_reset(dev);
849 LOG_ERR("I2C ch%d:0x%X reset cause %d",
850 config->port, data->addr_16bit, I2C_RC_TIMEOUT);
851 }
852
853 /* Permit to enter power policy and idle mode. */
854 pm_policy_state_lock_put(PM_STATE_STANDBY, PM_ALL_SUBSTATES);
855 chip_permit_idle();
856
857 return data->err;
858 }
859
cq_mode_allowed(const struct device * dev,struct i2c_msg * msgs)860 static bool cq_mode_allowed(const struct device *dev, struct i2c_msg *msgs)
861 {
862 struct i2c_enhance_data *data = dev->data;
863
864 /*
865 * If the transaction of write or read is divided into two
866 * transfers(not two messages), the command queue mode does
867 * not support.
868 */
869 if (data->i2ccs != I2C_CH_NORMAL) {
870 return false;
871 }
872 /*
873 * When there is only one message, use the command queue transfer
874 * directly.
875 */
876 if (data->num_msgs == 1 && (msgs[0].flags & I2C_MSG_STOP)) {
877 /* Read transfer payload too long, use PIO mode */
878 if (((msgs[0].flags & I2C_MSG_RW_MASK) == I2C_MSG_READ) &&
879 (msgs[0].len > CONFIG_I2C_CQ_MODE_MAX_PAYLOAD_SIZE)) {
880 return false;
881 }
882 /* Write transfer payload too long, use PIO mode */
883 if (((msgs[0].flags & I2C_MSG_RW_MASK) == I2C_MSG_WRITE) &&
884 (msgs[0].len > I2C_CQ_MODE_TX_MAX_PAYLOAD_SIZE)) {
885 return false;
886 }
887 /*
888 * Write of I2C target address without writing data, used by
889 * cmd_i2c_scan. Use PIO mode.
890 */
891 if (((msgs[0].flags & I2C_MSG_RW_MASK) == I2C_MSG_WRITE) &&
892 (msgs[0].len == 0)) {
893 return false;
894 }
895 return true;
896 }
897 /*
898 * When there are two messages, we need to judge whether or not there
899 * is I2C_MSG_RESTART flag from the second message, and then decide to
900 * do the command queue or PIO mode transfer.
901 */
902 if (data->num_msgs == 2) {
903 /*
904 * The first of two messages must be write.
905 * If the length of write to read transfer is greater than
906 * command queue payload size, there will execute PIO mode.
907 */
908 if (((msgs[0].flags & I2C_MSG_RW_MASK) == I2C_MSG_WRITE) &&
909 (msgs[0].len <= I2C_CQ_MODE_TX_MAX_PAYLOAD_SIZE)) {
910 /*
911 * The transfer is i2c_burst_read().
912 *
913 * e.g. msg[0].flags = I2C_MSG_WRITE;
914 * msg[1].flags = I2C_MSG_RESTART | I2C_MSG_READ |
915 * I2C_MSG_STOP;
916 */
917 if ((msgs[1].flags & I2C_MSG_RESTART) &&
918 ((msgs[1].flags & I2C_MSG_RW_MASK) == I2C_MSG_READ) &&
919 (msgs[1].flags & I2C_MSG_STOP) &&
920 (msgs[1].len <= CONFIG_I2C_CQ_MODE_MAX_PAYLOAD_SIZE)) {
921 return true;
922 }
923 }
924 }
925
926 return false;
927 }
928 #endif /* CONFIG_I2C_IT8XXX2_CQ_MODE */
929
i2c_enhance_transfer(const struct device * dev,struct i2c_msg * msgs,uint8_t num_msgs,uint16_t addr)930 static int i2c_enhance_transfer(const struct device *dev,
931 struct i2c_msg *msgs,
932 uint8_t num_msgs, uint16_t addr)
933 {
934 struct i2c_enhance_data *data = dev->data;
935 int ret;
936
937 #ifdef CONFIG_I2C_TARGET
938 if (data->target_attached) {
939 LOG_ERR("Device is registered as target");
940 return -EBUSY;
941 }
942 #endif
943 /* Lock mutex of i2c controller */
944 k_mutex_lock(&data->mutex, K_FOREVER);
945
946 data->num_msgs = num_msgs;
947 data->addr_16bit = addr;
948 /*
949 * If the transaction of write to read is divided into two
950 * transfers, the repeat start transfer uses this flag to
951 * exclude checking bus busy.
952 */
953 if (data->i2ccs == I2C_CH_NORMAL) {
954 /* Make sure we're in a good state to start */
955 if (i2c_bus_not_available(dev)) {
956 /* Recovery I2C bus */
957 i2c_recover_bus(dev);
958 /*
959 * After resetting I2C bus, if I2C bus is not available
960 * (No external pull-up), drop the transaction.
961 */
962 if (i2c_bus_not_available(dev)) {
963 /* Unlock mutex of i2c controller */
964 k_mutex_unlock(&data->mutex);
965 return -EIO;
966 }
967 }
968 }
969
970 #ifdef CONFIG_I2C_IT8XXX2_CQ_MODE
971 if (cq_mode_allowed(dev, msgs)) {
972 data->err = i2c_enhance_cq_transfer(dev, msgs);
973 } else
974 #endif
975 {
976 data->err = i2c_enhance_pio_transfer(dev, msgs);
977 }
978 /* Save return value. */
979 ret = i2c_parsing_return_value(dev);
980 /* Unlock mutex of i2c controller */
981 k_mutex_unlock(&data->mutex);
982
983 return ret;
984 }
985
986 #ifdef CONFIG_I2C_TARGET
target_i2c_isr_dma(const struct device * dev,uint8_t interrupt_status)987 static void target_i2c_isr_dma(const struct device *dev,
988 uint8_t interrupt_status)
989 {
990 struct i2c_enhance_data *data = dev->data;
991 const struct i2c_enhance_config *config = dev->config;
992 const struct i2c_target_callbacks *target_cb = data->target_cfg->callbacks;
993 struct i2c_target_dma_buffer *target_buffer = &data->target_buffer;
994 uint8_t *base = config->base;
995
996 /* Byte counter enable */
997 if (interrupt_status & IT8XXX2_I2C_IDW_CLR) {
998 IT8XXX2_I2C_BYTE_CNT_L(base) |=
999 (IT8XXX2_I2C_DMA_ADDR_RELOAD |
1000 IT8XXX2_I2C_BYTE_CNT_ENABLE);
1001 }
1002 /* The number of received data exceeds the byte counter setting */
1003 if (interrupt_status & IT8XXX2_I2C_CNT_HOLD) {
1004 LOG_ERR("The excess data written starts "
1005 "from the memory address:%p",
1006 target_buffer->in_buffer +
1007 CONFIG_I2C_TARGET_IT8XXX2_MAX_BUF_SIZE);
1008 }
1009 /* Controller to write data */
1010 if (interrupt_status & IT8XXX2_I2C_SLVDATAFLG) {
1011 /* Number of receive data in target mode */
1012 data->buffer_size =
1013 ((IT8XXX2_I2C_SLV_NUM_H(base) << 8) |
1014 IT8XXX2_I2C_SLV_NUM_L(base)) + 1;
1015
1016 /* Write data done callback function */
1017 target_cb->buf_write_received(data->target_cfg,
1018 target_buffer->in_buffer, data->buffer_size);
1019 }
1020 /* Peripheral finish */
1021 if (interrupt_status & IT8XXX2_I2C_P_CLR) {
1022 /* Transfer done callback function */
1023 target_cb->stop(data->target_cfg);
1024 }
1025 /* Controller to read data */
1026 if (interrupt_status & IT8XXX2_I2C_IDR_CLR) {
1027 uint32_t len;
1028 uint8_t *rdata = NULL;
1029
1030 /* Clear byte counter setting */
1031 IT8XXX2_I2C_BYTE_CNT_L(base) &=
1032 ~(IT8XXX2_I2C_DMA_ADDR_RELOAD |
1033 IT8XXX2_I2C_BYTE_CNT_ENABLE);
1034 /* Read data callback function */
1035 target_cb->buf_read_requested(data->target_cfg,
1036 &rdata, &len);
1037
1038 if (len > CONFIG_I2C_TARGET_IT8XXX2_MAX_BUF_SIZE) {
1039 LOG_ERR("The buffer size exceeds "
1040 "I2C_TARGET_IT8XXX2_MAX_BUF_SIZE: len=%d",
1041 len);
1042 } else {
1043 memcpy(target_buffer->out_buffer, rdata, len);
1044 }
1045 }
1046
1047 /* Write clear the peripheral status */
1048 IT8XXX2_I2C_IRQ_ST(base) = interrupt_status;
1049 }
1050
target_i2c_isr_pio(const struct device * dev,uint8_t interrupt_status,uint8_t target_status)1051 static int target_i2c_isr_pio(const struct device *dev,
1052 uint8_t interrupt_status,
1053 uint8_t target_status)
1054 {
1055 struct i2c_enhance_data *data = dev->data;
1056 const struct i2c_enhance_config *config = dev->config;
1057 const struct i2c_target_callbacks *target_cb = data->target_cfg->callbacks;
1058 int ret = 0;
1059 uint8_t *base = config->base;
1060 uint8_t val;
1061
1062 /* Target ID write flag */
1063 if (interrupt_status & IT8XXX2_I2C_IDW_CLR) {
1064 ret = target_cb->write_requested(data->target_cfg);
1065 }
1066 /* Target ID read flag */
1067 else if (interrupt_status & IT8XXX2_I2C_IDR_CLR) {
1068 if (!target_cb->read_requested(data->target_cfg, &val)) {
1069 IT8XXX2_I2C_DTR(base) = val;
1070 }
1071 }
1072 /* Byte transfer done */
1073 else if (target_status & IT8XXX2_I2C_BYTE_DONE) {
1074 /* Read of write */
1075 if (target_status & IT8XXX2_I2C_RW) {
1076 /* Host receiving, target transmitting */
1077 if (!target_cb->read_processed(data->target_cfg, &val)) {
1078 IT8XXX2_I2C_DTR(base) = val;
1079 }
1080 } else {
1081 /* Host transmitting, target receiving */
1082 val = IT8XXX2_I2C_DRR(base);
1083 ret = target_cb->write_received(data->target_cfg, val);
1084 }
1085 }
1086
1087 return ret;
1088 }
1089
target_i2c_isr(const struct device * dev)1090 static void target_i2c_isr(const struct device *dev)
1091 {
1092 struct i2c_enhance_data *data = dev->data;
1093 const struct i2c_enhance_config *config = dev->config;
1094 const struct i2c_target_callbacks *target_cb = data->target_cfg->callbacks;
1095 uint8_t *base = config->base;
1096 uint8_t target_status = IT8XXX2_I2C_STR(base);
1097
1098 /* Any error */
1099 if (target_status & E_TARGET_ANY_ERROR) {
1100 /* Hardware reset */
1101 IT8XXX2_I2C_CTR(base) |= IT8XXX2_I2C_HALT;
1102 return;
1103 }
1104
1105 /* Interrupt pending */
1106 if (target_status & IT8XXX2_I2C_INT_PEND) {
1107 uint8_t interrupt_status = IT8XXX2_I2C_IRQ_ST(base);
1108
1109 /* Determine whether the transaction uses PIO or DMA mode */
1110 if (config->target_pio_mode) {
1111 if (target_i2c_isr_pio(dev, interrupt_status, target_status) < 0) {
1112 /* NACK */
1113 IT8XXX2_I2C_CTR(base) &= ~IT8XXX2_I2C_ACK;
1114 IT8XXX2_I2C_CTR(base) |= IT8XXX2_I2C_HALT;
1115 data->target_nack = 1;
1116 }
1117 /* Peripheral finish */
1118 if (interrupt_status & IT8XXX2_I2C_P_CLR) {
1119 /* Transfer done callback function */
1120 target_cb->stop(data->target_cfg);
1121
1122 if (data->target_nack) {
1123 /* Set acknowledge */
1124 IT8XXX2_I2C_CTR(base) |=
1125 IT8XXX2_I2C_ACK;
1126 data->target_nack = 0;
1127 }
1128 }
1129 /* Write clear the peripheral status */
1130 IT8XXX2_I2C_IRQ_ST(base) = interrupt_status;
1131 /* Hardware reset */
1132 IT8XXX2_I2C_CTR(base) |= IT8XXX2_I2C_HALT;
1133 } else {
1134 target_i2c_isr_dma(dev, interrupt_status);
1135 }
1136 }
1137 }
1138 #endif
1139
i2c_enhance_isr(void * arg)1140 static void i2c_enhance_isr(void *arg)
1141 {
1142 struct device *dev = (struct device *)arg;
1143 struct i2c_enhance_data *data = dev->data;
1144 const struct i2c_enhance_config *config = dev->config;
1145
1146 #ifdef CONFIG_I2C_TARGET
1147 if (data->target_attached) {
1148 target_i2c_isr(dev);
1149 } else {
1150 #endif
1151 #ifdef CONFIG_I2C_IT8XXX2_CQ_MODE
1152 uint8_t *base = config->base;
1153
1154 /* If done doing work, wake up the task waiting for the transfer */
1155 if (IT8XXX2_I2C_CTR1(base) & IT8XXX2_I2C_COMQ_EN) {
1156 if (enhanced_i2c_cq_isr(dev)) {
1157 return;
1158 }
1159 } else
1160 #endif
1161 {
1162 if (i2c_transaction(dev)) {
1163 return;
1164 }
1165 }
1166 irq_disable(config->i2c_irq_base);
1167 k_sem_give(&data->device_sync_sem);
1168 #ifdef CONFIG_I2C_TARGET
1169 }
1170 #endif
1171 }
1172
i2c_enhance_init(const struct device * dev)1173 static int i2c_enhance_init(const struct device *dev)
1174 {
1175 struct i2c_enhance_data *data = dev->data;
1176 const struct i2c_enhance_config *config = dev->config;
1177 uint8_t *base = config->base;
1178 uint8_t data_hold_time = config->data_hold_time;
1179 uint32_t bitrate_cfg;
1180 int error, status;
1181
1182 #ifdef CONFIG_I2C_TARGET
1183 if (!config->target_enable) {
1184 #endif
1185 /* Initialize mutex and semaphore */
1186 k_mutex_init(&data->mutex);
1187 k_sem_init(&data->device_sync_sem, 0, K_SEM_MAX_LIMIT);
1188
1189 /* Enable clock to specified peripheral */
1190 volatile uint8_t *reg = (volatile uint8_t *)
1191 (IT8XXX2_ECPM_BASE + (config->clock_gate_offset >> 8));
1192 uint8_t reg_mask = config->clock_gate_offset & 0xff;
1193 *reg &= ~reg_mask;
1194
1195 /* Enable I2C function */
1196 /* Software reset */
1197 IT8XXX2_I2C_DHTR(base) |= IT8XXX2_I2C_SOFT_RST;
1198 IT8XXX2_I2C_DHTR(base) &= ~IT8XXX2_I2C_SOFT_RST;
1199 /* reset i2c port */
1200 i2c_reset(dev);
1201 /* bit1, Module enable */
1202 IT8XXX2_I2C_CTR1(base) = 0;
1203
1204 #ifdef CONFIG_I2C_IT8XXX2_CQ_MODE
1205 /* Set command address registers. */
1206 enhanced_i2c_set_cmd_addr_regs(dev);
1207 #endif
1208
1209 /* ChannelA-F switch selection of I2C pin */
1210 if (config->port == SMB_CHANNEL_A) {
1211 IT8XXX2_SMB_SMB01CHS = (IT8XXX2_SMB_SMB01CHS &= ~GENMASK(2, 0)) |
1212 config->channel_switch_sel;
1213 } else if (config->port == SMB_CHANNEL_B) {
1214 IT8XXX2_SMB_SMB01CHS = (config->channel_switch_sel << 4) |
1215 (IT8XXX2_SMB_SMB01CHS &= ~GENMASK(6, 4));
1216 } else if (config->port == SMB_CHANNEL_C) {
1217 IT8XXX2_SMB_SMB23CHS = (IT8XXX2_SMB_SMB23CHS &= ~GENMASK(2, 0)) |
1218 config->channel_switch_sel;
1219 } else if (config->port == I2C_CHANNEL_D) {
1220 IT8XXX2_SMB_SMB23CHS = (config->channel_switch_sel << 4) |
1221 (IT8XXX2_SMB_SMB23CHS &= ~GENMASK(6, 4));
1222 } else if (config->port == I2C_CHANNEL_E) {
1223 IT8XXX2_SMB_SMB45CHS = (IT8XXX2_SMB_SMB45CHS &= ~GENMASK(2, 0)) |
1224 config->channel_switch_sel;
1225 } else if (config->port == I2C_CHANNEL_F) {
1226 IT8XXX2_SMB_SMB45CHS = (config->channel_switch_sel << 4) |
1227 (IT8XXX2_SMB_SMB45CHS &= ~GENMASK(6, 4));
1228 }
1229
1230 /* Set I2C data hold time. */
1231 IT8XXX2_I2C_DHTR(base) = (IT8XXX2_I2C_DHTR(base) & ~GENMASK(2, 0)) |
1232 (data_hold_time - 3);
1233
1234 /* Set clock frequency for I2C ports */
1235 if (config->bitrate == I2C_BITRATE_STANDARD ||
1236 config->bitrate == I2C_BITRATE_FAST ||
1237 config->bitrate == I2C_BITRATE_FAST_PLUS) {
1238 bitrate_cfg = i2c_map_dt_bitrate(config->bitrate);
1239 } else {
1240 /* Device tree specified speed */
1241 bitrate_cfg = I2C_SPEED_DT << I2C_SPEED_SHIFT;
1242 }
1243
1244 error = i2c_enhance_configure(dev, I2C_MODE_CONTROLLER | bitrate_cfg);
1245 data->i2ccs = I2C_CH_NORMAL;
1246
1247 if (error) {
1248 LOG_ERR("i2c: failure initializing");
1249 return error;
1250 }
1251 #ifdef CONFIG_I2C_TARGET
1252 }
1253 #endif
1254
1255 /* Set the pin to I2C alternate function. */
1256 status = pinctrl_apply_state(config->pcfg, PINCTRL_STATE_DEFAULT);
1257 if (status < 0) {
1258 LOG_ERR("Failed to configure I2C pins");
1259 return status;
1260 }
1261
1262
1263 return 0;
1264 }
1265
i2c_enhance_recover_bus(const struct device * dev)1266 static int i2c_enhance_recover_bus(const struct device *dev)
1267 {
1268 const struct i2c_enhance_config *config = dev->config;
1269 int i, status;
1270
1271 /* Output type selection */
1272 gpio_flags_t flags = GPIO_OUTPUT | (config->push_pull_recovery ? 0 : GPIO_OPEN_DRAIN);
1273 /* Set SCL of I2C as GPIO pin */
1274 gpio_pin_configure_dt(&config->scl_gpios, flags);
1275 /* Set SDA of I2C as GPIO pin */
1276 gpio_pin_configure_dt(&config->sda_gpios, flags);
1277
1278 /*
1279 * In I2C recovery bus, 1ms sleep interval for bitbanging i2c
1280 * is mainly to ensure that gpio has enough time to go from
1281 * low to high or high to low.
1282 */
1283 /* Pull SCL and SDA pin to high */
1284 gpio_pin_set_dt(&config->scl_gpios, 1);
1285 gpio_pin_set_dt(&config->sda_gpios, 1);
1286 k_msleep(1);
1287
1288 /* Start condition */
1289 gpio_pin_set_dt(&config->sda_gpios, 0);
1290 k_msleep(1);
1291 gpio_pin_set_dt(&config->scl_gpios, 0);
1292 k_msleep(1);
1293
1294 /* 9 cycles of SCL with SDA held high */
1295 for (i = 0; i < 9; i++) {
1296 /* SDA */
1297 gpio_pin_set_dt(&config->sda_gpios, 1);
1298 /* SCL */
1299 gpio_pin_set_dt(&config->scl_gpios, 1);
1300 k_msleep(1);
1301 /* SCL */
1302 gpio_pin_set_dt(&config->scl_gpios, 0);
1303 k_msleep(1);
1304 }
1305 /* SDA */
1306 gpio_pin_set_dt(&config->sda_gpios, 0);
1307 k_msleep(1);
1308
1309 /* Stop condition */
1310 gpio_pin_set_dt(&config->scl_gpios, 1);
1311 k_msleep(1);
1312 gpio_pin_set_dt(&config->sda_gpios, 1);
1313 k_msleep(1);
1314
1315 /* Set GPIO back to I2C alternate function */
1316 status = pinctrl_apply_state(config->pcfg, PINCTRL_STATE_DEFAULT);
1317 if (status < 0) {
1318 LOG_ERR("Failed to configure I2C pins");
1319 return status;
1320 }
1321
1322 /* reset i2c port */
1323 i2c_reset(dev);
1324 LOG_ERR("I2C ch%d reset cause %d", config->port,
1325 I2C_RC_NO_IDLE_FOR_START);
1326
1327 return 0;
1328 }
1329
1330 #ifdef CONFIG_I2C_TARGET
i2c_enhance_target_register(const struct device * dev,struct i2c_target_config * target_cfg)1331 static int i2c_enhance_target_register(const struct device *dev,
1332 struct i2c_target_config *target_cfg)
1333 {
1334 const struct i2c_enhance_config *config = dev->config;
1335 struct i2c_enhance_data *data = dev->data;
1336 uint8_t *base = config->base;
1337
1338 if (!target_cfg) {
1339 return -EINVAL;
1340 }
1341
1342 if (target_cfg->flags & I2C_TARGET_FLAGS_ADDR_10_BITS) {
1343 return -ENOTSUP;
1344 }
1345
1346 if (data->target_attached) {
1347 return -EBUSY;
1348 }
1349
1350 data->target_cfg = target_cfg;
1351 data->target_attached = true;
1352
1353 /* Software reset */
1354 IT8XXX2_I2C_DHTR(base) |= IT8XXX2_I2C_SOFT_RST;
1355 IT8XXX2_I2C_DHTR(base) &= ~IT8XXX2_I2C_SOFT_RST;
1356 /* Disable the timeout setting when clock/data are in a low state */
1357 IT8XXX2_I2C_TO_ARB_ST(base) &= ~(IT8XXX2_I2C_SCL_TIMEOUT_EN |
1358 IT8XXX2_I2C_SDA_TIMEOUT_EN);
1359 /* Bit stretching */
1360 IT8XXX2_I2C_TOS(base) |= IT8XXX2_I2C_CLK_STRETCH;
1361 /* Peripheral address(8-bit) */
1362 IT8XXX2_I2C_IDR(base) = target_cfg->address << 1;
1363 /* I2C interrupt enable and set acknowledge */
1364 IT8XXX2_I2C_CTR(base) = IT8XXX2_I2C_INT_EN | IT8XXX2_I2C_HALT |
1365 IT8XXX2_I2C_ACK;
1366 /* Interrupt status write clear */
1367 IT8XXX2_I2C_IRQ_ST(base) = 0xff;
1368
1369 /* I2C target initial configuration of PIO mode */
1370 if (config->target_pio_mode) {
1371 /* Block to enter power policy. */
1372 pm_policy_state_lock_get(PM_STATE_STANDBY, PM_ALL_SUBSTATES);
1373
1374 /* I2C module enable */
1375 IT8XXX2_I2C_CTR1(base) = IT8XXX2_I2C_MDL_EN;
1376 /* I2C target initial configuration of DMA mode */
1377 } else {
1378 struct i2c_target_dma_buffer *target_buffer = &data->target_buffer;
1379 uint32_t in_data_addr, out_data_addr;
1380 int buf_size = CONFIG_I2C_TARGET_IT8XXX2_MAX_BUF_SIZE;
1381
1382 /* Clear read and write data buffer of DMA */
1383 memset(target_buffer->in_buffer, 0, buf_size);
1384 memset(target_buffer->out_buffer, 0, buf_size);
1385
1386 in_data_addr = (uint32_t)target_buffer->in_buffer & 0xffffff;
1387 out_data_addr = (uint32_t)target_buffer->out_buffer & 0xffffff;
1388 /*
1389 * DMA write target address register
1390 * for high order byte
1391 */
1392 IT8XXX2_I2C_RAMH2A(base) = in_data_addr >> 16;
1393 IT8XXX2_I2C_RAMHA(base) = in_data_addr >> 8;
1394 IT8XXX2_I2C_RAMLA(base) = in_data_addr;
1395 /*
1396 * DMA read target address register
1397 * for high order byte
1398 */
1399 IT8XXX2_I2C_CMD_ADDH2(base) = out_data_addr >> 16;
1400 IT8XXX2_I2C_RAMHA2(base) = out_data_addr >> 8;
1401 IT8XXX2_I2C_RAMLA2(base) = out_data_addr;
1402
1403 /* Byte counter setting */
1404 /* This register indicates byte count[10:3]. */
1405 IT8XXX2_I2C_BYTE_CNT_H(base) =
1406 CONFIG_I2C_TARGET_IT8XXX2_MAX_BUF_SIZE >> 3;
1407 /* This register indicates byte count[2:0]. */
1408 IT8XXX2_I2C_BYTE_CNT_L(base) =
1409 CONFIG_I2C_TARGET_IT8XXX2_MAX_BUF_SIZE & GENMASK(2, 0);
1410
1411 /*
1412 * The EC processor(CPU) cannot be in the k_cpu_idle() and power
1413 * policy during the transactions with the CQ mode(DMA mode).
1414 * Otherwise, the EC processor would be clock gated.
1415 */
1416 chip_block_idle();
1417 pm_policy_state_lock_get(PM_STATE_STANDBY, PM_ALL_SUBSTATES);
1418
1419 /* I2C module enable and command queue mode */
1420 IT8XXX2_I2C_CTR1(base) = IT8XXX2_I2C_COMQ_EN | IT8XXX2_I2C_MDL_EN;
1421 }
1422
1423 ite_intc_isr_clear(config->i2c_irq_base);
1424 irq_enable(config->i2c_irq_base);
1425
1426 return 0;
1427 }
1428
i2c_enhance_target_unregister(const struct device * dev,struct i2c_target_config * cfg)1429 static int i2c_enhance_target_unregister(const struct device *dev,
1430 struct i2c_target_config *cfg)
1431 {
1432 const struct i2c_enhance_config *config = dev->config;
1433 struct i2c_enhance_data *data = dev->data;
1434
1435 if (!data->target_attached) {
1436 return -EINVAL;
1437 }
1438
1439 irq_disable(config->i2c_irq_base);
1440
1441 /* Permit to enter power policy and idle mode. */
1442 pm_policy_state_lock_put(PM_STATE_STANDBY, PM_ALL_SUBSTATES);
1443 if (!config->target_pio_mode) {
1444 chip_permit_idle();
1445 }
1446
1447 data->target_cfg = NULL;
1448 data->target_attached = false;
1449 data->target_nack = 0;
1450
1451 return 0;
1452 }
1453 #endif
1454
1455 static DEVICE_API(i2c, i2c_enhance_driver_api) = {
1456 .configure = i2c_enhance_configure,
1457 .get_config = i2c_enhance_get_config,
1458 .transfer = i2c_enhance_transfer,
1459 .recover_bus = i2c_enhance_recover_bus,
1460 #ifdef CONFIG_I2C_TARGET
1461 .target_register = i2c_enhance_target_register,
1462 .target_unregister = i2c_enhance_target_unregister,
1463 #endif
1464 #ifdef CONFIG_I2C_RTIO
1465 .iodev_submit = i2c_iodev_submit_fallback,
1466 #endif
1467 };
1468
1469 #ifdef CONFIG_I2C_TARGET
1470 BUILD_ASSERT(IS_ENABLED(CONFIG_I2C_TARGET_BUFFER_MODE),
1471 "When I2C target config is enabled, the buffer mode must be used.");
1472 #endif
1473
1474 #define I2C_ITE_ENHANCE_INIT(inst) \
1475 PINCTRL_DT_INST_DEFINE(inst); \
1476 BUILD_ASSERT((DT_INST_PROP(inst, clock_frequency) == \
1477 50000) || \
1478 (DT_INST_PROP(inst, clock_frequency) == \
1479 I2C_BITRATE_STANDARD) || \
1480 (DT_INST_PROP(inst, clock_frequency) == \
1481 I2C_BITRATE_FAST) || \
1482 (DT_INST_PROP(inst, clock_frequency) == \
1483 I2C_BITRATE_FAST_PLUS), "Not support I2C bit rate value"); \
1484 static void i2c_enhance_config_func_##inst(void); \
1485 \
1486 static const struct i2c_enhance_config i2c_enhance_cfg_##inst = { \
1487 .base = (uint8_t *)(DT_INST_REG_ADDR(inst)), \
1488 .irq_config_func = i2c_enhance_config_func_##inst, \
1489 .bitrate = DT_INST_PROP(inst, clock_frequency), \
1490 .i2c_irq_base = DT_INST_IRQN(inst), \
1491 .port = DT_INST_PROP(inst, port_num), \
1492 .channel_switch_sel = DT_INST_PROP(inst, channel_switch_sel), \
1493 .scl_gpios = GPIO_DT_SPEC_INST_GET(inst, scl_gpios), \
1494 .sda_gpios = GPIO_DT_SPEC_INST_GET(inst, sda_gpios), \
1495 .prescale_scl_low = DT_INST_PROP_OR(inst, prescale_scl_low, 0), \
1496 .data_hold_time = DT_INST_PROP_OR(inst, data_hold_time, 0), \
1497 .clock_gate_offset = DT_INST_PROP(inst, clock_gate_offset), \
1498 .transfer_timeout_ms = DT_INST_PROP(inst, transfer_timeout_ms), \
1499 .pcfg = PINCTRL_DT_INST_DEV_CONFIG_GET(inst), \
1500 .target_enable = DT_INST_PROP(inst, target_enable), \
1501 .target_pio_mode = DT_INST_PROP(inst, target_pio_mode), \
1502 .push_pull_recovery = DT_INST_PROP(inst, push_pull_recovery), \
1503 }; \
1504 \
1505 static struct i2c_enhance_data i2c_enhance_data_##inst; \
1506 \
1507 I2C_DEVICE_DT_INST_DEFINE(inst, i2c_enhance_init, \
1508 NULL, \
1509 &i2c_enhance_data_##inst, \
1510 &i2c_enhance_cfg_##inst, \
1511 POST_KERNEL, \
1512 CONFIG_I2C_INIT_PRIORITY, \
1513 &i2c_enhance_driver_api); \
1514 \
1515 static void i2c_enhance_config_func_##inst(void) \
1516 { \
1517 IRQ_CONNECT(DT_INST_IRQN(inst), \
1518 0, \
1519 i2c_enhance_isr, \
1520 DEVICE_DT_INST_GET(inst), 0); \
1521 }
1522
1523 DT_INST_FOREACH_STATUS_OKAY(I2C_ITE_ENHANCE_INIT)
1524