blob: 7ee236193daa6b0c62b7cbf5843d5c4236272e5b [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001/* SPDX-License-Identifier: GPL-2.0+ */
Vipin KUMAR2403f8f2010-01-15 19:15:44 +05302/*
3 * (C) Copyright 2009
4 * Vipin Kumar, ST Micoelectronics, vipin.kumar@st.com.
Vipin KUMAR2403f8f2010-01-15 19:15:44 +05305 */
6
Vipin KUMAR031ed2f2012-02-26 23:13:29 +00007#ifndef __DW_I2C_H_
8#define __DW_I2C_H_
Vipin KUMAR2403f8f2010-01-15 19:15:44 +05309
Simon Glassafb88652020-01-23 11:48:06 -070010#include <clk.h>
Simon Glass23ad52e2020-01-23 11:48:25 -070011#include <i2c.h>
Simon Glass457df232019-12-06 21:41:40 -070012#include <reset.h>
13
Vipin KUMAR2403f8f2010-01-15 19:15:44 +053014struct i2c_regs {
Stefan Roesee2098282016-04-21 08:19:37 +020015 u32 ic_con; /* 0x00 */
16 u32 ic_tar; /* 0x04 */
17 u32 ic_sar; /* 0x08 */
18 u32 ic_hs_maddr; /* 0x0c */
19 u32 ic_cmd_data; /* 0x10 */
20 u32 ic_ss_scl_hcnt; /* 0x14 */
21 u32 ic_ss_scl_lcnt; /* 0x18 */
22 u32 ic_fs_scl_hcnt; /* 0x1c */
23 u32 ic_fs_scl_lcnt; /* 0x20 */
24 u32 ic_hs_scl_hcnt; /* 0x24 */
25 u32 ic_hs_scl_lcnt; /* 0x28 */
26 u32 ic_intr_stat; /* 0x2c */
27 u32 ic_intr_mask; /* 0x30 */
28 u32 ic_raw_intr_stat; /* 0x34 */
29 u32 ic_rx_tl; /* 0x38 */
30 u32 ic_tx_tl; /* 0x3c */
31 u32 ic_clr_intr; /* 0x40 */
32 u32 ic_clr_rx_under; /* 0x44 */
33 u32 ic_clr_rx_over; /* 0x48 */
34 u32 ic_clr_tx_over; /* 0x4c */
35 u32 ic_clr_rd_req; /* 0x50 */
36 u32 ic_clr_tx_abrt; /* 0x54 */
37 u32 ic_clr_rx_done; /* 0x58 */
38 u32 ic_clr_activity; /* 0x5c */
39 u32 ic_clr_stop_det; /* 0x60 */
40 u32 ic_clr_start_det; /* 0x64 */
41 u32 ic_clr_gen_call; /* 0x68 */
42 u32 ic_enable; /* 0x6c */
43 u32 ic_status; /* 0x70 */
44 u32 ic_txflr; /* 0x74 */
45 u32 ic_rxflr; /* 0x78 */
46 u32 ic_sda_hold; /* 0x7c */
47 u32 ic_tx_abrt_source; /* 0x80 */
Simon Glass0fd05c92020-01-23 11:48:04 -070048 u32 slv_data_nak_only;
49 u32 dma_cr;
50 u32 dma_tdlr;
51 u32 dma_rdlr;
52 u32 sda_setup;
53 u32 ack_general_call;
Stefan Roesee2098282016-04-21 08:19:37 +020054 u32 ic_enable_status; /* 0x9c */
Simon Glass0fd05c92020-01-23 11:48:04 -070055 u32 fs_spklen;
56 u32 hs_spklen;
57 u32 clr_restart_det;
58 u8 reserved[0xf4 - 0xac];
59 u32 comp_param1; /* 0xf4 */
60 u32 comp_version;
61 u32 comp_type;
Vipin KUMAR2403f8f2010-01-15 19:15:44 +053062};
63
Simon Glassdd3c1602020-01-23 11:48:09 -070064#define IC_CLK 166666666
65#define NANO_TO_KILO 1000000
Vipin KUMAR2403f8f2010-01-15 19:15:44 +053066
67/* High and low times in different speed modes (in ns) */
68#define MIN_SS_SCL_HIGHTIME 4000
Armando Viscontiea31b7a2012-12-06 00:04:18 +000069#define MIN_SS_SCL_LOWTIME 4700
70#define MIN_FS_SCL_HIGHTIME 600
71#define MIN_FS_SCL_LOWTIME 1300
Simon Glassd96440d2020-01-23 11:48:23 -070072#define MIN_FP_SCL_HIGHTIME 260
73#define MIN_FP_SCL_LOWTIME 500
Vipin KUMAR2403f8f2010-01-15 19:15:44 +053074#define MIN_HS_SCL_HIGHTIME 60
75#define MIN_HS_SCL_LOWTIME 160
76
77/* Worst case timeout for 1 byte is kept as 2ms */
78#define I2C_BYTE_TO (CONFIG_SYS_HZ/500)
79#define I2C_STOPDET_TO (CONFIG_SYS_HZ/500)
80#define I2C_BYTE_TO_BB (I2C_BYTE_TO * 16)
81
82/* i2c control register definitions */
83#define IC_CON_SD 0x0040
84#define IC_CON_RE 0x0020
85#define IC_CON_10BITADDRMASTER 0x0010
86#define IC_CON_10BITADDR_SLAVE 0x0008
87#define IC_CON_SPD_MSK 0x0006
88#define IC_CON_SPD_SS 0x0002
89#define IC_CON_SPD_FS 0x0004
90#define IC_CON_SPD_HS 0x0006
91#define IC_CON_MM 0x0001
92
93/* i2c target address register definitions */
94#define TAR_ADDR 0x0050
95
96/* i2c slave address register definitions */
97#define IC_SLAVE_ADDR 0x0002
98
99/* i2c data buffer and command register definitions */
100#define IC_CMD 0x0100
Armando Visconti491739b2012-12-06 00:04:16 +0000101#define IC_STOP 0x0200
Vipin KUMAR2403f8f2010-01-15 19:15:44 +0530102
103/* i2c interrupt status register definitions */
104#define IC_GEN_CALL 0x0800
105#define IC_START_DET 0x0400
106#define IC_STOP_DET 0x0200
107#define IC_ACTIVITY 0x0100
108#define IC_RX_DONE 0x0080
109#define IC_TX_ABRT 0x0040
110#define IC_RD_REQ 0x0020
111#define IC_TX_EMPTY 0x0010
112#define IC_TX_OVER 0x0008
113#define IC_RX_FULL 0x0004
114#define IC_RX_OVER 0x0002
115#define IC_RX_UNDER 0x0001
116
117/* fifo threshold register definitions */
118#define IC_TL0 0x00
119#define IC_TL1 0x01
120#define IC_TL2 0x02
121#define IC_TL3 0x03
122#define IC_TL4 0x04
123#define IC_TL5 0x05
124#define IC_TL6 0x06
125#define IC_TL7 0x07
126#define IC_RX_TL IC_TL0
127#define IC_TX_TL IC_TL0
128
129/* i2c enable register definitions */
130#define IC_ENABLE_0B 0x0001
131
132/* i2c status register definitions */
133#define IC_STATUS_SA 0x0040
134#define IC_STATUS_MA 0x0020
135#define IC_STATUS_RFF 0x0010
136#define IC_STATUS_RFNE 0x0008
137#define IC_STATUS_TFE 0x0004
138#define IC_STATUS_TFNF 0x0002
139#define IC_STATUS_ACT 0x0001
140
Jun Chen565e3282020-03-02 16:58:55 +0800141#define DW_IC_COMP_PARAM_1_SPEED_MODE_HIGH (BIT(2) | BIT(3))
142#define DW_IC_COMP_PARAM_1_SPEED_MODE_MASK (BIT(2) | BIT(3))
143
Simon Glass457df232019-12-06 21:41:40 -0700144/**
145 * struct dw_scl_sda_cfg - I2C timing configuration
146 *
Simon Glass457df232019-12-06 21:41:40 -0700147 * @ss_hcnt: Standard speed high time in ns
148 * @fs_hcnt: Fast speed high time in ns
Jun Chen27d483b2020-03-02 16:58:57 +0800149 * @hs_hcnt: High speed high time in ns
Simon Glass457df232019-12-06 21:41:40 -0700150 * @ss_lcnt: Standard speed low time in ns
151 * @fs_lcnt: Fast speed low time in ns
Jun Chen27d483b2020-03-02 16:58:57 +0800152 * @hs_lcnt: High speed low time in ns
Simon Glass457df232019-12-06 21:41:40 -0700153 * @sda_hold: SDA hold time
154 */
155struct dw_scl_sda_cfg {
Simon Glass457df232019-12-06 21:41:40 -0700156 u32 ss_hcnt;
157 u32 fs_hcnt;
Jun Chen27d483b2020-03-02 16:58:57 +0800158 u32 hs_hcnt;
Simon Glass457df232019-12-06 21:41:40 -0700159 u32 ss_lcnt;
160 u32 fs_lcnt;
Jun Chen27d483b2020-03-02 16:58:57 +0800161 u32 hs_lcnt;
Simon Glass457df232019-12-06 21:41:40 -0700162 u32 sda_hold;
163};
164
Simon Glass80a03db2020-01-23 11:48:11 -0700165/**
Simon Glassa8d2b512020-01-23 11:48:24 -0700166 * struct dw_i2c_speed_config - timings to use for a particular speed
167 *
168 * This holds calculated values to be written to the I2C controller. Each value
169 * is represented as a number of IC clock cycles.
170 *
171 * @scl_lcnt: Low count value for SCL
172 * @scl_hcnt: High count value for SCL
173 * @sda_hold: Data hold count
Simon Glass23ad52e2020-01-23 11:48:25 -0700174 * @speed_mode: Speed mode being used
Simon Glassa8d2b512020-01-23 11:48:24 -0700175 */
176struct dw_i2c_speed_config {
177 /* SCL high and low period count */
178 u16 scl_lcnt;
179 u16 scl_hcnt;
180 u32 sda_hold;
Simon Glass23ad52e2020-01-23 11:48:25 -0700181 enum i2c_speed_mode speed_mode;
Simon Glassa8d2b512020-01-23 11:48:24 -0700182};
183
184/**
Simon Glass80a03db2020-01-23 11:48:11 -0700185 * struct dw_i2c - private information for the bus
186 *
187 * @regs: Registers pointer
188 * @scl_sda_cfg: Deprecated information for x86 (should move to device tree)
189 * @resets: Resets for the I2C controller
190 * @scl_rise_time_ns: Configured SCL rise time in nanoseconds
191 * @scl_fall_time_ns: Configured SCL fall time in nanoseconds
192 * @sda_hold_time_ns: Configured SDA hold time in nanoseconds
Simon Glass96fe11c2020-01-23 11:48:15 -0700193 * @has_spk_cnt: true if the spike-count register is present
Simon Glass80a03db2020-01-23 11:48:11 -0700194 * @clk: Clock input to the I2C controller
195 */
Simon Glass457df232019-12-06 21:41:40 -0700196struct dw_i2c {
197 struct i2c_regs *regs;
198 struct dw_scl_sda_cfg *scl_sda_cfg;
199 struct reset_ctl_bulk resets;
Simon Glass80a03db2020-01-23 11:48:11 -0700200 u32 scl_rise_time_ns;
201 u32 scl_fall_time_ns;
202 u32 sda_hold_time_ns;
Simon Glass96fe11c2020-01-23 11:48:15 -0700203 bool has_spk_cnt;
Simon Glass457df232019-12-06 21:41:40 -0700204#if CONFIG_IS_ENABLED(CLK)
205 struct clk clk;
206#endif
207};
208
209extern const struct dm_i2c_ops designware_i2c_ops;
210
211int designware_i2c_probe(struct udevice *bus);
212int designware_i2c_remove(struct udevice *dev);
Simon Glass80a03db2020-01-23 11:48:11 -0700213int designware_i2c_ofdata_to_platdata(struct udevice *bus);
Simon Glass457df232019-12-06 21:41:40 -0700214
Vipin KUMAR031ed2f2012-02-26 23:13:29 +0000215#endif /* __DW_I2C_H_ */