1 /* SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause) */ 2 /* 3 * Copyright (c) 2017-2019, STMicroelectronics 4 */ 5 6 #ifndef __STM32_I2C_H 7 #define __STM32_I2C_H 8 9 #include <drivers/stm32_gpio.h> 10 #include <kernel/dt.h> 11 #include <mm/core_memprot.h> 12 #include <stdbool.h> 13 #include <stdint.h> 14 #include <util.h> 15 #include <types_ext.h> 16 17 /* 18 * I2C specification values as per version 6.0, 4th of April 2014 [1], 19 * table 10 page 48: Characteristics of the SDA and SCL bus lines for 20 * Standard, Fast, and Fast-mode Plus I2C-bus devices. 21 * 22 * [1] https://www.nxp.com/docs/en/user-guide/UM10204.pdf 23 */ 24 enum i2c_speed_e { 25 I2C_SPEED_STANDARD, /* 100 kHz */ 26 I2C_SPEED_FAST, /* 400 kHz */ 27 I2C_SPEED_FAST_PLUS, /* 1 MHz */ 28 }; 29 30 #define I2C_STANDARD_RATE 100000 31 #define I2C_FAST_RATE 400000 32 #define I2C_FAST_PLUS_RATE 1000000 33 34 /* 35 * Initialization configuration structure for the STM32 I2C bus. 36 * Refer to the SoC Reference Manual for more details on configuration items. 37 * 38 * @dt_status: non-secure/secure status read from DT 39 * @pbase: I2C interface base address 40 * @clock: I2C bus/interface clock 41 * @addr_mode_10b_not_7b: True if 10bit addressing mode, otherwise 7bit mode 42 * @own_address1: 7-bit or 10-bit first device own address. 43 * @dual_address_mode: True if enabling Dual-Addressing mode 44 * @own_address2: 7-bit second device own address (Dual-Addressing mode) 45 * @own_address2_masks: Acknowledge mask address (Dual-Addressing mode) 46 * @general_call_mode: True if enbling General-Call mode 47 * @no_stretch_mode: If enabling the No-Stretch mode 48 * @rise_time: SCL clock pin rising time in nanoseconds 49 * @fall_time: SCL clock pin falling time in nanoseconds 50 * @speed_mode: I2C clock source frequency mode 51 * @analog_filter: True if enabling analog filter 52 * @digital_filter_coef: filter coef (below STM32_I2C_DIGITAL_FILTER_MAX) 53 */ 54 struct stm32_i2c_init_s { 55 unsigned int dt_status; 56 paddr_t pbase; 57 unsigned int clock; 58 bool addr_mode_10b_not_7b; 59 uint32_t own_address1; 60 bool dual_address_mode; 61 uint32_t own_address2; 62 uint32_t own_address2_masks; 63 bool general_call_mode; 64 bool no_stretch_mode; 65 uint32_t rise_time; 66 uint32_t fall_time; 67 enum i2c_speed_e speed_mode; 68 bool analog_filter; 69 uint8_t digital_filter_coef; 70 }; 71 72 enum i2c_state_e { 73 I2C_STATE_RESET, /* Not yet initialized */ 74 I2C_STATE_READY, /* Ready for use */ 75 I2C_STATE_BUSY, /* Internal process ongoing */ 76 I2C_STATE_BUSY_TX, /* Data Transmission ongoing */ 77 I2C_STATE_BUSY_RX, /* Data Reception ongoing */ 78 I2C_STATE_SUSPENDED, /* Bus is supended */ 79 }; 80 81 enum i2c_mode_e { 82 I2C_MODE_NONE, /* No active communication */ 83 I2C_MODE_MASTER, /* Communication in Master Mode */ 84 I2C_MODE_SLAVE, /* Communication in Slave Mode */ 85 I2C_MODE_MEM, /* Communication in Memory Mode */ 86 }; 87 88 #define I2C_ERROR_NONE 0x0 89 #define I2C_ERROR_BERR BIT(0) 90 #define I2C_ERROR_ARLO BIT(1) 91 #define I2C_ERROR_ACKF BIT(2) 92 #define I2C_ERROR_OVR BIT(3) 93 #define I2C_ERROR_DMA BIT(4) 94 #define I2C_ERROR_TIMEOUT BIT(5) 95 #define I2C_ERROR_SIZE BIT(6) 96 97 /* I2C interface registers state */ 98 struct i2c_cfg { 99 uint32_t timingr; 100 uint32_t oar1; 101 uint32_t oar2; 102 uint32_t cr1; 103 uint32_t cr2; 104 }; 105 106 /* 107 * I2C bus device 108 * @base: I2C SoC registers base address 109 * @dt_status: non-secure/secure status read from DT 110 * @clock: clock ID 111 * @i2c_state: Driver state ID I2C_STATE_* 112 * @i2c_err: Last error code I2C_ERROR_* 113 * @sec_cfg: I2C regsiters configuration storage 114 * @saved_timing: Saved timing value if already computed 115 * @saved_frequency: Saved frequency value if already computed 116 * @pinctrl: PINCTRLs configuration for the I2C PINs 117 * @pinctrl_count: Number of PINCTRLs elements 118 */ 119 struct i2c_handle_s { 120 struct io_pa_va base; 121 unsigned int dt_status; 122 unsigned long clock; 123 enum i2c_state_e i2c_state; 124 uint32_t i2c_err; 125 uint32_t saved_timing; 126 unsigned long saved_frequency; 127 struct i2c_cfg sec_cfg; 128 struct stm32_pinctrl *pinctrl; 129 size_t pinctrl_count; 130 }; 131 132 /* STM32 specific defines */ 133 #define STM32_I2C_SPEED_DEFAULT I2C_SPEED_STANDARD 134 #define STM32_I2C_RISE_TIME_DEFAULT 25 /* ns */ 135 #define STM32_I2C_FALL_TIME_DEFAULT 10 /* ns */ 136 #define STM32_I2C_ANALOG_FILTER_DELAY_MIN 50 /* ns */ 137 #define STM32_I2C_ANALOG_FILTER_DELAY_MAX 260 /* ns */ 138 #define STM32_I2C_DIGITAL_FILTER_MAX 16 139 140 /* 141 * Fill struct stm32_i2c_init_s from DT content for a given I2C node 142 * 143 * @fdt: Reference to DT 144 * @node: Target I2C node in the DT 145 * @init: Output stm32_i2c_init_s structure 146 * @pinctrl: Reference to output pinctrl array 147 * @pinctrl_count: Input @pinctrl array size, output expected size 148 * Return 0 on success else a negative value 149 */ 150 int stm32_i2c_get_setup_from_fdt(void *fdt, int node, 151 struct stm32_i2c_init_s *init, 152 struct stm32_pinctrl **pinctrl, 153 size_t *pinctrl_count); 154 155 /* 156 * Initialize I2C bus handle from input configuration directives 157 * 158 * @hi2c: Reference to I2C bus handle structure 159 * @init_data: Input stm32_i2c_init_s structure 160 * Return 0 on success else a negative value 161 */ 162 int stm32_i2c_init(struct i2c_handle_s *hi2c, 163 struct stm32_i2c_init_s *init_data); 164 165 /* 166 * Send a memory write request in the I2C bus 167 * 168 * @hi2c: Reference to I2C bus handle structure 169 * @dev_addr: Target device I2C address 170 * @mem_addr: Target device memory address 171 * @mem_addr_size: Byte size of internal memory address 172 * @p_data: Data to be written 173 * @size: Byte size of the data to be written 174 * @timeout_ms: Timeout value in milliseconds 175 * Return 0 on success else a negative value 176 */ 177 int stm32_i2c_mem_write(struct i2c_handle_s *hi2c, uint32_t dev_addr, 178 uint32_t mem_addr, uint32_t mem_addr_size, 179 uint8_t *p_data, size_t size, unsigned int timeout_ms); 180 181 /* 182 * Send a memory read request in the I2C bus 183 * 184 * @hi2c: Reference to I2C bus handle structure 185 * @dev_addr: Target device I2C address 186 * @mem_addr: Target device memory address 187 * @mem_addr_size: Byte size of internal memory address 188 * @p_data: Data to be read 189 * @size: Byte size of the data to be read 190 * @timeout_ms: Timeout value in milliseconds 191 * Return 0 on success else a negative value 192 */ 193 int stm32_i2c_mem_read(struct i2c_handle_s *hi2c, uint32_t dev_addr, 194 uint32_t mem_addr, uint32_t mem_addr_size, 195 uint8_t *p_data, size_t size, unsigned int timeout_ms); 196 197 /* 198 * Send a data buffer in master mode on the I2C bus 199 * 200 * @hi2c: Reference to I2C bus handle structure 201 * @dev_addr: Target device I2C address 202 * @p_data: Data to be sent 203 * @size: Byte size of the data to be sent 204 * @timeout_ms: Timeout value in milliseconds 205 * Return 0 on success else a negative value 206 */ 207 int stm32_i2c_master_transmit(struct i2c_handle_s *hi2c, uint32_t dev_addr, 208 uint8_t *p_data, size_t size, 209 unsigned int timeout_ms); 210 211 /* 212 * Receive a data buffer in master mode on the I2C bus 213 * 214 * @hi2c: Reference to I2C bus handle structure 215 * @dev_addr: Target device I2C address 216 * @p_data: Buffer for the received data 217 * @size: Byte size of the data to be received 218 * @timeout_ms: Timeout value in milliseconds 219 * Return 0 on success else a negative value 220 */ 221 int stm32_i2c_master_receive(struct i2c_handle_s *hi2c, uint32_t dev_addr, 222 uint8_t *p_data, size_t size, 223 unsigned int timeout_ms); 224 225 /* 226 * Optimized 1 byte read/write function for unpaged sequences. 227 * 8-bit addressing mode / single byte transferred / use default I2C timeout. 228 * Return 0 on success else a negative value 229 */ 230 int stm32_i2c_read_write_membyte(struct i2c_handle_s *hi2c, uint16_t dev_addr, 231 unsigned int mem_addr, uint8_t *p_data, 232 bool write); 233 234 /* 235 * Check link with the I2C device 236 * 237 * @hi2c: Reference to I2C bus handle structure 238 * @dev_addr: Target device I2C address 239 * @trials: Number of attempts of I2C request 240 * @timeout_ms: Timeout value in milliseconds for each I2C request 241 * Return 0 on success else a negative value 242 */ 243 bool stm32_i2c_is_device_ready(struct i2c_handle_s *hi2c, uint32_t dev_addr, 244 unsigned int trials, unsigned int timeout_ms); 245 246 /* 247 * Suspend I2C bus. 248 * Bus owner is reponsible for calling stm32_i2c_suspend(). 249 * 250 * @hi2c: Reference to I2C bus handle structure 251 */ 252 void stm32_i2c_suspend(struct i2c_handle_s *hi2c); 253 254 /* 255 * Resume I2C bus. 256 * Bus owner is reponsible for calling stm32_i2c_resume(). 257 * 258 * @hi2c: Reference to I2C bus handle structure 259 */ 260 void stm32_i2c_resume(struct i2c_handle_s *hi2c); 261 262 /* 263 * Return true if I2C bus is enabled for secure world only, false otherwise 264 */ 265 static inline bool i2c_is_secure(struct i2c_handle_s *hi2c) 266 { 267 return hi2c->dt_status == DT_STATUS_OK_SEC; 268 } 269 270 #endif /* __STM32_I2C_H */ 271