1 /* SPDX-License-Identifier: GPL-2.0-only */
3 #include <acpi/acpigen.h>
4 #include <device/mmio.h>
5 #include <console/console.h>
6 #include <device/device.h>
7 #include <device/i2c_bus.h>
8 #include <device/i2c_simple.h>
14 /* Use a ~10ms timeout for various operations */
15 #define DW_I2C_TIMEOUT_US 10000
16 /* Timeout for waiting for FIFO to flush */
17 #define DW_I2C_FLUSH_TIMEOUT_US 160000
19 /* High and low times in different speed modes (in ns) */
22 DEFAULT_SDA_HOLD_TIME
= 300,
24 MIN_SS_SCL_HIGHTIME
= 4000,
25 MIN_SS_SCL_LOWTIME
= 4700,
27 MIN_FS_SCL_HIGHTIME
= 600,
28 MIN_FS_SCL_LOWTIME
= 1300,
30 MIN_FP_SCL_HIGHTIME
= 260,
31 MIN_FP_SCL_LOWTIME
= 500,
33 MIN_HS_SCL_HIGHTIME
= 60,
34 MIN_HS_SCL_LOWTIME
= 160,
37 /* Frequency represented as ticks per ns. Can also be used to calculate
38 * the number of ticks to meet a time target or the period. */
44 /* Control register definitions */
46 CONTROL_MASTER_MODE
= (1 << 0),
47 CONTROL_SPEED_SS
= (1 << 1),
48 CONTROL_SPEED_FS
= (2 << 1),
49 CONTROL_SPEED_HS
= (3 << 1),
50 CONTROL_SPEED_MASK
= (3 << 1),
51 CONTROL_10BIT_SLAVE
= (1 << 3),
52 CONTROL_10BIT_MASTER
= (1 << 4),
53 CONTROL_RESTART_ENABLE
= (1 << 5),
54 CONTROL_SLAVE_DISABLE
= (1 << 6),
57 /* Command/Data register definitions */
59 CMD_DATA_CMD
= (1 << 8),
60 CMD_DATA_STOP
= (1 << 9),
63 /* Status register definitions */
65 STATUS_ACTIVITY
= (1 << 0),
66 STATUS_TX_FIFO_NOT_FULL
= (1 << 1),
67 STATUS_TX_FIFO_EMPTY
= (1 << 2),
68 STATUS_RX_FIFO_NOT_EMPTY
= (1 << 3),
69 STATUS_RX_FIFO_FULL
= (1 << 4),
70 STATUS_MASTER_ACTIVITY
= (1 << 5),
71 STATUS_SLAVE_ACTIVITY
= (1 << 6),
74 /* Enable register definitions */
76 ENABLE_CONTROLLER
= (1 << 0),
79 /* Interrupt status register definitions */
81 INTR_STAT_RX_UNDER
= (1 << 0),
82 INTR_STAT_RX_OVER
= (1 << 1),
83 INTR_STAT_RX_FULL
= (1 << 2),
84 INTR_STAT_TX_OVER
= (1 << 3),
85 INTR_STAT_TX_EMPTY
= (1 << 4),
86 INTR_STAT_RD_REQ
= (1 << 5),
87 INTR_STAT_TX_ABORT
= (1 << 6),
88 INTR_STAT_RX_DONE
= (1 << 7),
89 INTR_STAT_ACTIVITY
= (1 << 8),
90 INTR_STAT_STOP_DET
= (1 << 9),
91 INTR_STAT_START_DET
= (1 << 10),
92 INTR_STAT_GEN_CALL
= (1 << 11),
95 /* I2C Controller MMIO register space */
97 uint32_t control
; /* 0x0 */
98 uint32_t target_addr
; /* 0x4 */
99 uint32_t slave_addr
; /* 0x8 */
100 uint32_t master_addr
; /* 0xc */
101 uint32_t cmd_data
; /* 0x10 */
102 uint32_t ss_scl_hcnt
; /* 0x14 */
103 uint32_t ss_scl_lcnt
; /* 0x18 */
104 uint32_t fs_scl_hcnt
; /* 0x1c */
105 uint32_t fs_scl_lcnt
; /* 0x20 */
106 uint32_t hs_scl_hcnt
; /* 0x24 */
107 uint32_t hs_scl_lcnt
; /* 0x28 */
108 uint32_t intr_stat
; /* 0x2c */
109 uint32_t intr_mask
; /* 0x30 */
110 uint32_t raw_intr_stat
; /* 0x34 */
111 uint32_t rx_thresh
; /* 0x38 */
112 uint32_t tx_thresh
; /* 0x3c */
113 uint32_t clear_intr
; /* 0x40 */
114 uint32_t clear_rx_under_intr
; /* 0x44 */
115 uint32_t clear_rx_over_intr
; /* 0x48 */
116 uint32_t clear_tx_over_intr
; /* 0x4c */
117 uint32_t clear_rd_req_intr
; /* 0x50 */
118 uint32_t clear_tx_abrt_intr
; /* 0x54 */
119 uint32_t clear_rx_done_intr
; /* 0x58 */
120 uint32_t clear_activity_intr
; /* 0x5c */
121 uint32_t clear_stop_det_intr
; /* 0x60 */
122 uint32_t clear_start_det_intr
; /* 0x64 */
123 uint32_t clear_gen_call_intr
; /* 0x68 */
124 uint32_t enable
; /* 0x6c */
125 uint32_t status
; /* 0x70 */
126 uint32_t tx_level
; /* 0x74 */
127 uint32_t rx_level
; /* 0x78 */
128 uint32_t sda_hold
; /* 0x7c */
129 uint32_t tx_abort_source
; /* 0x80 */
130 uint32_t slv_data_nak_only
; /* 0x84 */
131 uint32_t dma_cr
; /* 0x88 */
132 uint32_t dma_tdlr
; /* 0x8c */
133 uint32_t dma_rdlr
; /* 0x90 */
134 uint32_t sda_setup
; /* 0x94 */
135 uint32_t ack_general_call
; /* 0x98 */
136 uint32_t enable_status
; /* 0x9c */
137 uint32_t fs_spklen
; /* 0xa0 */
138 uint32_t hs_spklen
; /* 0xa4 */
139 uint32_t clr_restart_det
; /* 0xa8 */
140 uint32_t reserved
[18]; /* 0xac - 0xf0 */
141 uint32_t comp_param1
; /* 0xf4 */
142 uint32_t comp_version
; /* 0xf8 */
143 uint32_t comp_type
; /* 0xfc */
146 /* Constant value defined in the DesignWare DW_apb_i2c Databook. */
147 #define DW_I2C_COMP_TYPE 0x44570140
149 static const struct i2c_descriptor
{
150 enum i2c_speed speed
;
154 } speed_descriptors
[] = {
156 .speed
= I2C_SPEED_STANDARD
,
161 .min_thigh_ns
= MIN_SS_SCL_HIGHTIME
,
162 .min_tlow_ns
= MIN_SS_SCL_LOWTIME
,
165 .speed
= I2C_SPEED_FAST
,
170 .min_thigh_ns
= MIN_FS_SCL_HIGHTIME
,
171 .min_tlow_ns
= MIN_FS_SCL_LOWTIME
,
174 .speed
= I2C_SPEED_FAST_PLUS
,
179 .min_thigh_ns
= MIN_FP_SCL_HIGHTIME
,
180 .min_tlow_ns
= MIN_FP_SCL_LOWTIME
,
183 /* 100pF max capacitance */
184 .speed
= I2C_SPEED_HIGH
,
189 .min_thigh_ns
= MIN_HS_SCL_HIGHTIME
,
190 .min_tlow_ns
= MIN_HS_SCL_LOWTIME
,
194 static const struct soc_clock
{
199 .clk_speed_mhz
= 100,
206 .clk_speed_mhz
= 120,
213 .clk_speed_mhz
= 133,
220 .clk_speed_mhz
= 150,
227 .clk_speed_mhz
= 216,
235 static const struct i2c_descriptor
*get_bus_descriptor(enum i2c_speed speed
)
239 for (i
= 0; i
< ARRAY_SIZE(speed_descriptors
); i
++)
240 if (speed
== speed_descriptors
[i
].speed
)
241 return &speed_descriptors
[i
];
246 static const struct soc_clock
*get_soc_descriptor(int ic_clk
)
250 for (i
= 0; i
< ARRAY_SIZE(soc_clocks
); i
++)
251 if (ic_clk
== soc_clocks
[i
].clk_speed_mhz
)
252 return &soc_clocks
[i
];
257 static int counts_from_time(const struct freq
*f
, int ns
)
259 return DIV_ROUND_UP(f
->ticks
* ns
, f
->ns
);
262 static int counts_from_freq(const struct freq
*fast
, const struct freq
*slow
)
264 return DIV_ROUND_UP(fast
->ticks
* slow
->ns
, fast
->ns
* slow
->ticks
);
267 /* Enable this I2C controller */
268 static void dw_i2c_enable(struct dw_i2c_regs
*regs
)
270 uint32_t enable
= read32(®s
->enable
);
272 if (!(enable
& ENABLE_CONTROLLER
))
273 write32(®s
->enable
, enable
| ENABLE_CONTROLLER
);
276 /* Disable this I2C controller */
277 static enum cb_err
dw_i2c_disable(struct dw_i2c_regs
*regs
)
279 uint32_t enable
= read32(®s
->enable
);
281 if (enable
& ENABLE_CONTROLLER
) {
284 write32(®s
->enable
, enable
& ~ENABLE_CONTROLLER
);
286 /* Wait for enable bit to clear */
287 stopwatch_init_usecs_expire(&sw
, DW_I2C_TIMEOUT_US
);
288 while (read32(®s
->enable_status
) & ENABLE_CONTROLLER
)
289 if (stopwatch_expired(&sw
))
296 /* Wait for this I2C controller to go idle for transmit */
297 static enum cb_err
dw_i2c_wait_for_bus_idle(struct dw_i2c_regs
*regs
)
301 /* Start timeout for up to 16 bytes in FIFO */
302 stopwatch_init_usecs_expire(&sw
, DW_I2C_FLUSH_TIMEOUT_US
);
304 while (!stopwatch_expired(&sw
)) {
305 uint32_t status
= read32(®s
->status
);
307 /* Check for master activity and keep waiting */
308 if (status
& STATUS_MASTER_ACTIVITY
)
311 /* Check for TX FIFO empty to indicate TX idle */
312 if (status
& STATUS_TX_FIFO_EMPTY
)
316 /* Timed out while waiting for bus to go idle */
320 /* Transfer one byte of one segment, sending stop bit if requested */
321 static enum cb_err
dw_i2c_transfer_byte(struct dw_i2c_regs
*regs
,
322 const struct i2c_msg
*segment
,
323 size_t byte
, int send_stop
)
326 uint32_t cmd
= CMD_DATA_CMD
; /* Read op */
328 stopwatch_init_usecs_expire(&sw
, CONFIG_I2C_TRANSFER_TIMEOUT_US
);
330 if (!(segment
->flags
& I2C_M_RD
)) {
331 /* Write op only: Wait for FIFO not full */
332 while (!(read32(®s
->status
) & STATUS_TX_FIFO_NOT_FULL
)) {
333 if (stopwatch_expired(&sw
)) {
334 printk(BIOS_ERR
, "I2C transmit timeout\n");
338 cmd
= segment
->buf
[byte
];
341 /* Send stop on last byte, if desired */
342 if (send_stop
&& byte
== segment
->len
- 1)
343 cmd
|= CMD_DATA_STOP
;
345 write32(®s
->cmd_data
, cmd
);
347 if (segment
->flags
& I2C_M_RD
) {
348 /* Read op only: Wait for FIFO data and store it */
349 while (!(read32(®s
->status
) & STATUS_RX_FIFO_NOT_EMPTY
)) {
350 if (stopwatch_expired(&sw
)) {
351 printk(BIOS_ERR
, "I2C receive timeout\n");
355 segment
->buf
[byte
] = read32(®s
->cmd_data
);
361 static enum cb_err
_dw_i2c_transfer(unsigned int bus
, const struct i2c_msg
*segments
,
365 struct dw_i2c_regs
*regs
;
367 enum cb_err ret
= CB_ERR
;
368 bool seg_zero_len
= segments
->len
== 0;
370 regs
= (struct dw_i2c_regs
*)dw_i2c_base_address(bus
);
372 printk(BIOS_ERR
, "I2C bus %u base address not found\n", bus
);
376 /* The assumption is that the host controller is disabled -- either
377 after running this function or from performing the initialization
378 sequence in dw_i2c_init(). */
380 /* Set target slave address */
381 write32(®s
->target_addr
, segments
->slave
);
386 /* stop immediately */
387 write32(®s
->cmd_data
, CMD_DATA_STOP
);
389 /* Process each segment */
391 if (CONFIG(DRIVERS_I2C_DESIGNWARE_DEBUG
)) {
392 printk(BIOS_DEBUG
, "i2c %u:%02x %s %d bytes : ",
393 bus
, segments
->slave
,
394 (segments
->flags
& I2C_M_RD
) ? "R" : "W",
398 /* Read or write each byte in segment */
399 for (byte
= 0; byte
< segments
->len
; byte
++) {
401 * Set stop condition on final segment only.
402 * Repeated start will be automatically generated
403 * by the controller on R->W or W->R switch.
405 if (dw_i2c_transfer_byte(regs
, segments
, byte
, count
== 0) !=
407 printk(BIOS_ERR
, "I2C %s failed: bus %u "
409 (segments
->flags
& I2C_M_RD
) ?
410 "read" : "write", bus
, segments
->slave
);
415 if (CONFIG(DRIVERS_I2C_DESIGNWARE_DEBUG
)) {
417 for (j
= 0; j
< segments
->len
; j
++)
418 printk(BIOS_DEBUG
, "%02x ", segments
->buf
[j
]);
419 printk(BIOS_DEBUG
, "\n");
425 /* Wait for interrupt status to indicate transfer is complete */
426 stopwatch_init_usecs_expire(&sw
, CONFIG_I2C_TRANSFER_TIMEOUT_US
);
427 while (!(read32(®s
->raw_intr_stat
) & INTR_STAT_STOP_DET
)) {
428 if (stopwatch_expired(&sw
)) {
429 printk(BIOS_ERR
, "I2C stop bit not received\n");
434 /* Read to clear INTR_STAT_STOP_DET */
435 read32(®s
->clear_stop_det_intr
);
438 if (read32(®s
->raw_intr_stat
) & INTR_STAT_TX_ABORT
) {
439 printk(seg_zero_len
? BIOS_SPEW
: BIOS_ERR
, "I2C TX abort detected (%08x)\n",
440 read32(®s
->tx_abort_source
));
441 /* clear INTR_STAT_TX_ABORT */
442 read32(®s
->clear_tx_abrt_intr
);
446 /* Wait for the bus to go idle */
447 if (dw_i2c_wait_for_bus_idle(regs
) != CB_SUCCESS
) {
448 printk(BIOS_ERR
, "I2C timeout waiting for bus %u idle\n", bus
);
452 /* Flush the RX FIFO in case it is not empty */
453 stopwatch_init_usecs_expire(&sw
, DW_I2C_FLUSH_TIMEOUT_US
);
454 while (read32(®s
->status
) & STATUS_RX_FIFO_NOT_EMPTY
) {
455 if (stopwatch_expired(&sw
)) {
456 printk(BIOS_ERR
, "I2C timeout flushing RX FIFO\n");
459 read32(®s
->cmd_data
);
465 read32(®s
->clear_intr
);
466 dw_i2c_disable(regs
);
470 static enum cb_err
dw_i2c_transfer(unsigned int bus
, const struct i2c_msg
*msg
, size_t count
)
472 const struct i2c_msg
*orig_msg
= msg
;
477 if (count
== 0 || !msg
)
480 /* Break up the transfers at the differing slave address boundary. */
481 addr
= orig_msg
->slave
;
483 for (i
= 0, start
= 0; i
< count
; i
++, msg
++) {
484 if (addr
!= msg
->slave
) {
485 if (_dw_i2c_transfer(bus
, &orig_msg
[start
], i
- start
) != CB_SUCCESS
)
492 return _dw_i2c_transfer(bus
, &orig_msg
[start
], count
- start
);
495 /* Global I2C bus handler, defined in include/device/i2c_simple.h */
496 int platform_i2c_transfer(unsigned int bus
, struct i2c_msg
*msg
, int count
)
498 return dw_i2c_transfer(bus
, msg
, count
< 0 ? 0 : count
) == CB_SUCCESS
? 0 : -1;
501 static enum cb_err
dw_i2c_set_speed_config(unsigned int bus
,
502 const struct dw_i2c_speed_config
*config
)
504 struct dw_i2c_regs
*regs
;
505 void *hcnt_reg
, *lcnt_reg
;
507 regs
= (struct dw_i2c_regs
*)dw_i2c_base_address(bus
);
508 if (!regs
|| !config
)
511 /* Nothing to do if no values are set */
512 if (!config
->scl_lcnt
&& !config
->scl_hcnt
&& !config
->sda_hold
)
515 if (config
->speed
>= I2C_SPEED_HIGH
) {
516 /* High and Fast Ultra speed */
517 hcnt_reg
= ®s
->hs_scl_hcnt
;
518 lcnt_reg
= ®s
->hs_scl_lcnt
;
519 } else if (config
->speed
>= I2C_SPEED_FAST
) {
520 /* Fast and Fast-Plus speed */
521 hcnt_reg
= ®s
->fs_scl_hcnt
;
522 lcnt_reg
= ®s
->fs_scl_lcnt
;
525 hcnt_reg
= ®s
->ss_scl_hcnt
;
526 lcnt_reg
= ®s
->ss_scl_lcnt
;
529 /* SCL count must be set after the speed is selected */
530 if (config
->scl_hcnt
)
531 write32(hcnt_reg
, config
->scl_hcnt
);
532 if (config
->scl_lcnt
)
533 write32(lcnt_reg
, config
->scl_lcnt
);
535 /* Set SDA Hold Time register */
536 if (config
->sda_hold
)
537 write32(®s
->sda_hold
, config
->sda_hold
);
542 static enum cb_err
dw_i2c_gen_config_rise_fall_time(struct dw_i2c_regs
*regs
,
543 enum i2c_speed speed
,
544 const struct dw_i2c_bus_config
*bcfg
,
546 struct dw_i2c_speed_config
*config
)
548 const struct i2c_descriptor
*bus
;
549 const struct soc_clock
*soc
;
550 int fall_cnt
, rise_cnt
, min_tlow_cnt
, min_thigh_cnt
, spk_cnt
;
551 int hcnt
, lcnt
, period_cnt
, diff
, tot
;
552 int data_hold_time_ns
;
554 bus
= get_bus_descriptor(speed
);
555 soc
= get_soc_descriptor(ic_clk
);
558 printk(BIOS_ERR
, "dw_i2c: invalid bus speed %d\n", speed
);
563 printk(BIOS_ERR
, "dw_i2c: invalid SoC clock speed %d MHz\n",
568 /* Get the proper spike suppression count based on target speed. */
569 if (speed
>= I2C_SPEED_HIGH
)
570 spk_cnt
= read32(®s
->hs_spklen
);
572 spk_cnt
= read32(®s
->fs_spklen
);
574 /* Find the period, rise, fall, min tlow, and min thigh in terms of
575 * counts of SoC clock. */
576 period_cnt
= counts_from_freq(&soc
->freq
, &bus
->freq
);
577 rise_cnt
= counts_from_time(&soc
->freq
, bcfg
->rise_time_ns
);
578 fall_cnt
= counts_from_time(&soc
->freq
, bcfg
->fall_time_ns
);
579 min_tlow_cnt
= counts_from_time(&soc
->freq
, bus
->min_tlow_ns
);
580 min_thigh_cnt
= counts_from_time(&soc
->freq
, bus
->min_thigh_ns
);
582 printk(DW_I2C_DEBUG
, "dw_i2c: SoC %d/%d ns Bus: %d/%d ns\n",
583 soc
->freq
.ticks
, soc
->freq
.ns
, bus
->freq
.ticks
, bus
->freq
.ns
);
585 "dw_i2c: period %d rise %d fall %d tlow %d thigh %d spk %d\n",
586 period_cnt
, rise_cnt
, fall_cnt
, min_tlow_cnt
, min_thigh_cnt
,
590 * Back solve for hcnt and lcnt according to the following equations.
591 * SCL_High_time = [(HCNT + IC_*_SPKLEN + 7) * ic_clk] + SCL_Fall_time
592 * SCL_Low_time = [(LCNT + 1) * ic_clk] - SCL_Fall_time + SCL_Rise_time
594 hcnt
= min_thigh_cnt
- fall_cnt
- 7 - spk_cnt
;
595 lcnt
= min_tlow_cnt
- rise_cnt
+ fall_cnt
- 1;
597 if (hcnt
< 0 || lcnt
< 0) {
598 printk(BIOS_ERR
, "dw_i2c: bad counts. hcnt = %d lcnt = %d\n",
603 /* Now add things back up to ensure the period is hit. If off,
604 * split the difference and bias to lcnt for remainder. */
605 tot
= hcnt
+ lcnt
+ 7 + spk_cnt
+ rise_cnt
+ 1;
607 if (tot
< period_cnt
) {
608 diff
= (period_cnt
- tot
) / 2;
611 tot
= hcnt
+ lcnt
+ 7 + spk_cnt
+ rise_cnt
+ 1;
612 lcnt
+= period_cnt
- tot
;
615 config
->speed
= speed
;
616 config
->scl_lcnt
= lcnt
;
617 config
->scl_hcnt
= hcnt
;
619 /* Use internal default unless other value is specified. */
620 data_hold_time_ns
= DEFAULT_SDA_HOLD_TIME
;
621 if (bcfg
->data_hold_time_ns
)
622 data_hold_time_ns
= bcfg
->data_hold_time_ns
;
624 config
->sda_hold
= counts_from_time(&soc
->freq
, data_hold_time_ns
);
626 printk(DW_I2C_DEBUG
, "dw_i2c: hcnt = %d lcnt = %d sda hold = %d\n",
627 hcnt
, lcnt
, config
->sda_hold
);
632 enum cb_err
dw_i2c_gen_speed_config(uintptr_t dw_i2c_addr
,
633 enum i2c_speed speed
,
634 const struct dw_i2c_bus_config
*bcfg
,
635 struct dw_i2c_speed_config
*config
)
637 const int ic_clk
= CONFIG_DRIVERS_I2C_DESIGNWARE_CLOCK_MHZ
;
638 struct dw_i2c_regs
*regs
;
641 regs
= (struct dw_i2c_regs
*)dw_i2c_addr
;
643 _Static_assert(CONFIG_DRIVERS_I2C_DESIGNWARE_CLOCK_MHZ
!= 0,
644 "DRIVERS_I2C_DESIGNWARE_CLOCK_MHZ can't be zero!");
646 /* Apply board specific override for this speed if found */
647 for (i
= 0; i
< DW_I2C_SPEED_CONFIG_COUNT
; i
++) {
648 if (bcfg
->speed_config
[i
].speed
!= speed
)
650 memcpy(config
, &bcfg
->speed_config
[i
], sizeof(*config
));
654 /* Use the time calculation. */
655 return dw_i2c_gen_config_rise_fall_time(regs
, speed
, bcfg
, ic_clk
, config
);
658 static enum cb_err
dw_i2c_set_speed(unsigned int bus
, enum i2c_speed speed
,
659 const struct dw_i2c_bus_config
*bcfg
)
661 struct dw_i2c_regs
*regs
;
662 struct dw_i2c_speed_config config
;
665 /* Clock must be provided by Kconfig */
666 regs
= (struct dw_i2c_regs
*)dw_i2c_base_address(bus
);
670 control
= read32(®s
->control
);
671 control
&= ~CONTROL_SPEED_MASK
;
673 if (speed
>= I2C_SPEED_HIGH
) {
674 /* High and Fast-Ultra speed share config registers */
675 control
|= CONTROL_SPEED_HS
;
676 } else if (speed
>= I2C_SPEED_FAST
) {
677 /* Fast speed and Fast-Plus */
678 control
|= CONTROL_SPEED_FS
;
681 control
|= CONTROL_SPEED_SS
;
684 /* Generate speed config based on clock */
685 if (dw_i2c_gen_speed_config((uintptr_t)regs
, speed
, bcfg
, &config
) != CB_SUCCESS
)
688 /* Select this speed in the control register */
689 write32(®s
->control
, control
);
691 /* Write the speed config that was generated earlier */
692 dw_i2c_set_speed_config(bus
, &config
);
698 * Initialize this bus controller and set the speed.
700 * The bus speed can be passed in Hz or using values from device/i2c.h and
701 * will default to I2C_SPEED_FAST if it is not provided.
703 enum cb_err
dw_i2c_init(unsigned int bus
, const struct dw_i2c_bus_config
*bcfg
)
705 struct dw_i2c_regs
*regs
;
706 enum i2c_speed speed
;
711 speed
= bcfg
->speed
? : I2C_SPEED_FAST
;
713 regs
= (struct dw_i2c_regs
*)dw_i2c_base_address(bus
);
715 printk(BIOS_ERR
, "I2C bus %u base address not found\n", bus
);
719 if (read32(®s
->comp_type
) != DW_I2C_COMP_TYPE
) {
720 printk(BIOS_ERR
, "I2C bus %u has unknown type 0x%x.\n", bus
,
721 read32(®s
->comp_type
));
725 printk(BIOS_DEBUG
, "I2C bus %u version 0x%x\n", bus
, read32(®s
->comp_version
));
727 if (dw_i2c_disable(regs
) != CB_SUCCESS
) {
728 printk(BIOS_ERR
, "I2C timeout disabling bus %u\n", bus
);
732 /* Put controller in master mode with restart enabled */
733 write32(®s
->control
, CONTROL_MASTER_MODE
| CONTROL_SLAVE_DISABLE
|
734 CONTROL_RESTART_ENABLE
);
736 /* Set bus speed to FAST by default */
737 if (dw_i2c_set_speed(bus
, speed
, bcfg
) != CB_SUCCESS
) {
738 printk(BIOS_ERR
, "I2C failed to set speed for bus %u\n", bus
);
742 /* Set RX/TX thresholds to smallest values */
743 write32(®s
->rx_thresh
, 0);
744 write32(®s
->tx_thresh
, 0);
746 /* Enable stop detection and TX abort interrupt */
747 write32(®s
->intr_mask
, INTR_STAT_STOP_DET
| INTR_STAT_TX_ABORT
);
749 printk(BIOS_INFO
, "DW I2C bus %u at %p (%u KHz)\n",
750 bus
, regs
, speed
/ KHz
);
756 * Write ACPI object to describe speed configuration.
758 * ACPI Object: Name ("xxxx", Package () { scl_lcnt, scl_hcnt, sda_hold }
760 * SSCN: I2C_SPEED_STANDARD
761 * FMCN: I2C_SPEED_FAST
762 * FPCN: I2C_SPEED_FAST_PLUS
763 * HSCN: I2C_SPEED_HIGH
765 static void dw_i2c_acpi_write_speed_config(
766 const struct dw_i2c_speed_config
*config
)
770 if (!config
->scl_lcnt
&& !config
->scl_hcnt
&& !config
->sda_hold
)
773 if (config
->speed
>= I2C_SPEED_HIGH
)
774 acpigen_write_name("HSCN");
775 else if (config
->speed
>= I2C_SPEED_FAST_PLUS
)
776 acpigen_write_name("FPCN");
777 else if (config
->speed
>= I2C_SPEED_FAST
)
778 acpigen_write_name("FMCN");
780 acpigen_write_name("SSCN");
782 /* Package () { scl_lcnt, scl_hcnt, sda_hold } */
783 acpigen_write_package(3);
784 acpigen_write_word(config
->scl_hcnt
);
785 acpigen_write_word(config
->scl_lcnt
);
786 acpigen_write_dword(config
->sda_hold
);
791 * The device should already be enabled and out of reset,
792 * either from early init in coreboot or SiliconInit in FSP.
794 void dw_i2c_dev_init(struct device
*dev
)
796 const struct dw_i2c_bus_config
*config
;
797 int bus
= dw_i2c_soc_dev_to_bus(dev
);
802 config
= dw_i2c_get_soc_cfg(bus
);
807 dw_i2c_init(bus
, config
);
811 * Generate I2C timing information into the SSDT for the OS driver to consume,
812 * optionally applying override values provided by the caller.
814 void dw_i2c_acpi_fill_ssdt(const struct device
*dev
)
816 const struct dw_i2c_bus_config
*bcfg
;
817 uintptr_t dw_i2c_addr
;
818 struct dw_i2c_speed_config sgen
;
821 unsigned int speed
, i
;
823 bus
= dw_i2c_soc_dev_to_bus(dev
);
828 bcfg
= dw_i2c_get_soc_cfg(bus
);
833 dw_i2c_addr
= dw_i2c_base_address(bus
);
837 path
= acpi_device_path(dev
);
841 /* Ensure a default speed is available */
842 speed
= (bcfg
->speed
== 0) ? I2C_SPEED_FAST
: bcfg
->speed
;
844 /* Report currently used timing values for the OS driver */
845 acpigen_write_scope(path
);
846 if (dw_i2c_gen_speed_config(dw_i2c_addr
, speed
, bcfg
, &sgen
) == CB_SUCCESS
) {
847 dw_i2c_acpi_write_speed_config(&sgen
);
849 /* Now check if there are more speed settings available and report them as well. */
850 for (i
= 0; i
< DW_I2C_SPEED_CONFIG_COUNT
; i
++) {
851 if (bcfg
->speed_config
[i
].speed
&& speed
!= bcfg
->speed_config
[i
].speed
)
852 dw_i2c_acpi_write_speed_config(&bcfg
->speed_config
[i
]);
854 acpigen_write_scope_end();
857 static int dw_i2c_dev_transfer(struct device
*dev
,
858 const struct i2c_msg
*msg
, size_t count
)
860 int bus
= dw_i2c_soc_dev_to_bus(dev
);
862 printk(BIOS_ERR
, "Invalid I2C bus number.\n");
865 return dw_i2c_transfer(bus
, msg
, count
) == CB_SUCCESS
? 0 : -1;
868 const struct i2c_bus_operations dw_i2c_bus_ops
= {
869 .transfer
= dw_i2c_dev_transfer
,