stm32_i2c.c 26.9 KB
Newer Older
Yann Gautier's avatar
Yann Gautier committed
1
/*
2
 * Copyright (c) 2016-2019, STMicroelectronics - All Rights Reserved
Yann Gautier's avatar
Yann Gautier committed
3
4
5
6
7
8
9
 *
 * SPDX-License-Identifier: BSD-3-Clause
 */

#include <errno.h>
#include <stdbool.h>
#include <stdlib.h>
10

11
12
13
14
15
#include <libfdt.h>

#include <platform_def.h>

#include <common/debug.h>
16
#include <drivers/delay_timer.h>
17
#include <drivers/st/stm32_gpio.h>
18
19
#include <drivers/st/stm32_i2c.h>
#include <lib/mmio.h>
20
#include <lib/utils.h>
Yann Gautier's avatar
Yann Gautier committed
21
22
23
24
25
26
27
28
29
30
31
32
33
34

/* STM32 I2C registers offsets */
#define I2C_CR1			0x00U
#define I2C_CR2			0x04U
#define I2C_OAR1		0x08U
#define I2C_OAR2		0x0CU
#define I2C_TIMINGR		0x10U
#define I2C_TIMEOUTR		0x14U
#define I2C_ISR			0x18U
#define I2C_ICR			0x1CU
#define I2C_PECR		0x20U
#define I2C_RXDR		0x24U
#define I2C_TXDR		0x28U

35
#define TIMINGR_CLEAR_MASK	0xF0FFFFFFU
Yann Gautier's avatar
Yann Gautier committed
36
37
38

#define MAX_NBYTE_SIZE		255U

39
#define I2C_NSEC_PER_SEC	1000000000L
Yann Gautier's avatar
Yann Gautier committed
40

41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/* I2C Timing hard-coded value, for I2C clock source is HSI at 64MHz */
#define I2C_TIMING			0x10D07DB5

static void notif_i2c_timeout(struct i2c_handle_s *hi2c)
{
	hi2c->i2c_err |= I2C_ERROR_TIMEOUT;
	hi2c->i2c_mode = I2C_MODE_NONE;
	hi2c->i2c_state = I2C_STATE_READY;
}

/*
 * @brief  Configure I2C Analog noise filter.
 * @param  hi2c: Pointer to a struct i2c_handle_s structure that contains
 *               the configuration information for the specified I2C peripheral.
 * @param  analog_filter: New state of the Analog filter
 * @retval 0 if OK, negative value else
 */
static int i2c_config_analog_filter(struct i2c_handle_s *hi2c,
				    uint32_t analog_filter)
{
	if ((hi2c->i2c_state != I2C_STATE_READY) || (hi2c->lock != 0U)) {
		return -EBUSY;
	}

	hi2c->lock = 1;

	hi2c->i2c_state = I2C_STATE_BUSY;

	/* Disable the selected I2C peripheral */
	mmio_clrbits_32(hi2c->i2c_base_addr + I2C_CR1, I2C_CR1_PE);

	/* Reset I2Cx ANOFF bit */
	mmio_clrbits_32(hi2c->i2c_base_addr + I2C_CR1, I2C_CR1_ANFOFF);

	/* Set analog filter bit*/
	mmio_setbits_32(hi2c->i2c_base_addr + I2C_CR1, analog_filter);

	/* Enable the selected I2C peripheral */
	mmio_setbits_32(hi2c->i2c_base_addr + I2C_CR1, I2C_CR1_PE);

	hi2c->i2c_state = I2C_STATE_READY;

	hi2c->lock = 0;

	return 0;
}

/*
 * @brief  Get I2C setup information from the device tree and set pinctrl
 *         configuration.
 * @param  fdt: Pointer to the device tree
 * @param  node: I2C node offset
 * @param  init: Ref to the initialization configuration structure
 * @retval 0 if OK, negative value else
 */
int stm32_i2c_get_setup_from_fdt(void *fdt, int node,
				 struct stm32_i2c_init_s *init)
{
	const fdt32_t *cuint;

	cuint = fdt_getprop(fdt, node, "i2c-scl-rising-time-ns", NULL);
	if (cuint == NULL) {
		init->rise_time = STM32_I2C_RISE_TIME_DEFAULT;
	} else {
		init->rise_time = fdt32_to_cpu(*cuint);
	}

	cuint = fdt_getprop(fdt, node, "i2c-scl-falling-time-ns", NULL);
	if (cuint == NULL) {
		init->fall_time = STM32_I2C_FALL_TIME_DEFAULT;
	} else {
		init->fall_time = fdt32_to_cpu(*cuint);
	}

	cuint = fdt_getprop(fdt, node, "clock-frequency", NULL);
	if (cuint == NULL) {
		init->speed_mode = STM32_I2C_SPEED_DEFAULT;
	} else {
		switch (fdt32_to_cpu(*cuint)) {
		case STANDARD_RATE:
			init->speed_mode = I2C_SPEED_STANDARD;
			break;
		case FAST_RATE:
			init->speed_mode = I2C_SPEED_FAST;
			break;
		case FAST_PLUS_RATE:
			init->speed_mode = I2C_SPEED_FAST_PLUS;
			break;
		default:
			init->speed_mode = STM32_I2C_SPEED_DEFAULT;
			break;
		}
	}

	return dt_set_pinctrl_config(node);
}
Yann Gautier's avatar
Yann Gautier committed
137
138
139
140
141

/*
 * @brief  Initialize the I2C device.
 * @param  hi2c: Pointer to a struct i2c_handle_s structure that contains
 *               the configuration information for the specified I2C.
142
 * @param  init_data: Initialization configuration structure
Yann Gautier's avatar
Yann Gautier committed
143
144
 * @retval 0 if OK, negative value else
 */
145
146
int stm32_i2c_init(struct i2c_handle_s *hi2c,
		   struct stm32_i2c_init_s *init_data)
Yann Gautier's avatar
Yann Gautier committed
147
{
148
149
150
	int rc = 0;
	uint32_t timing = I2C_TIMING;

Yann Gautier's avatar
Yann Gautier committed
151
152
153
154
155
156
157
158
159
160
	if (hi2c == NULL) {
		return -ENOENT;
	}

	if (hi2c->i2c_state == I2C_STATE_RESET) {
		hi2c->lock = 0;
	}

	hi2c->i2c_state = I2C_STATE_BUSY;

161
162
	stm32mp_clk_enable(hi2c->clock);

Yann Gautier's avatar
Yann Gautier committed
163
164
165
166
167
	/* Disable the selected I2C peripheral */
	mmio_clrbits_32(hi2c->i2c_base_addr + I2C_CR1, I2C_CR1_PE);

	/* Configure I2Cx: Frequency range */
	mmio_write_32(hi2c->i2c_base_addr + I2C_TIMINGR,
168
		      timing & TIMINGR_CLEAR_MASK);
Yann Gautier's avatar
Yann Gautier committed
169
170
171
172
173

	/* Disable Own Address1 before set the Own Address1 configuration */
	mmio_clrbits_32(hi2c->i2c_base_addr + I2C_OAR1, I2C_OAR1_OA1EN);

	/* Configure I2Cx: Own Address1 and ack own address1 mode */
174
	if (init_data->addressing_mode == I2C_ADDRESSINGMODE_7BIT) {
Yann Gautier's avatar
Yann Gautier committed
175
		mmio_write_32(hi2c->i2c_base_addr + I2C_OAR1,
176
			      I2C_OAR1_OA1EN | init_data->own_address1);
Yann Gautier's avatar
Yann Gautier committed
177
178
179
	} else { /* I2C_ADDRESSINGMODE_10BIT */
		mmio_write_32(hi2c->i2c_base_addr + I2C_OAR1,
			      I2C_OAR1_OA1EN | I2C_OAR1_OA1MODE |
180
			      init_data->own_address1);
Yann Gautier's avatar
Yann Gautier committed
181
182
	}

183
184
	mmio_write_32(hi2c->i2c_base_addr + I2C_CR2, 0);

Yann Gautier's avatar
Yann Gautier committed
185
	/* Configure I2Cx: Addressing Master mode */
186
187
	if (init_data->addressing_mode == I2C_ADDRESSINGMODE_10BIT) {
		mmio_setbits_32(hi2c->i2c_base_addr + I2C_CR2, I2C_CR2_ADD10);
Yann Gautier's avatar
Yann Gautier committed
188
189
190
191
	}

	/*
	 * Enable the AUTOEND by default, and enable NACK
192
	 * (should be disabled only during Slave process).
Yann Gautier's avatar
Yann Gautier committed
193
194
195
196
197
198
199
200
201
	 */
	mmio_setbits_32(hi2c->i2c_base_addr + I2C_CR2,
			I2C_CR2_AUTOEND | I2C_CR2_NACK);

	/* Disable Own Address2 before set the Own Address2 configuration */
	mmio_clrbits_32(hi2c->i2c_base_addr + I2C_OAR2, I2C_DUALADDRESS_ENABLE);

	/* Configure I2Cx: Dual mode and Own Address2 */
	mmio_write_32(hi2c->i2c_base_addr + I2C_OAR2,
202
203
204
		      init_data->dual_address_mode |
		      init_data->own_address2 |
		      (init_data->own_address2_masks << 8));
Yann Gautier's avatar
Yann Gautier committed
205
206
207

	/* Configure I2Cx: Generalcall and NoStretch mode */
	mmio_write_32(hi2c->i2c_base_addr + I2C_CR1,
208
209
		      init_data->general_call_mode |
		      init_data->no_stretch_mode);
Yann Gautier's avatar
Yann Gautier committed
210
211
212
213
214
215
216
217

	/* Enable the selected I2C peripheral */
	mmio_setbits_32(hi2c->i2c_base_addr + I2C_CR1, I2C_CR1_PE);

	hi2c->i2c_err = I2C_ERROR_NONE;
	hi2c->i2c_state = I2C_STATE_READY;
	hi2c->i2c_mode = I2C_MODE_NONE;

218
219
220
221
222
223
224
225
226
227
228
229
	rc = i2c_config_analog_filter(hi2c, init_data->analog_filter ?
						I2C_ANALOGFILTER_ENABLE :
						I2C_ANALOGFILTER_DISABLE);
	if (rc != 0) {
		ERROR("Cannot initialize I2C analog filter (%d)\n", rc);
		stm32mp_clk_disable(hi2c->clock);
		return rc;
	}

	stm32mp_clk_disable(hi2c->clock);

	return rc;
Yann Gautier's avatar
Yann Gautier committed
230
231
232
}

/*
233
234
235
 * @brief  I2C Tx data register flush process.
 * @param  hi2c: I2C handle
 * @retval None
Yann Gautier's avatar
Yann Gautier committed
236
 */
237
static void i2c_flush_txdr(struct i2c_handle_s *hi2c)
Yann Gautier's avatar
Yann Gautier committed
238
{
239
240
241
242
243
244
245
	/*
	 * If a pending TXIS flag is set,
	 * write a dummy data in TXDR to clear it.
	 */
	if ((mmio_read_32(hi2c->i2c_base_addr + I2C_ISR) & I2C_FLAG_TXIS) !=
	    0U) {
		mmio_write_32(hi2c->i2c_base_addr + I2C_TXDR, 0);
Yann Gautier's avatar
Yann Gautier committed
246
247
	}

248
249
250
251
252
	/* Flush TX register if not empty */
	if ((mmio_read_32(hi2c->i2c_base_addr + I2C_ISR) & I2C_FLAG_TXE) ==
	    0U) {
		mmio_setbits_32(hi2c->i2c_base_addr + I2C_ISR,
				I2C_FLAG_TXE);
Yann Gautier's avatar
Yann Gautier committed
253
	}
254
}
Yann Gautier's avatar
Yann Gautier committed
255

256
257
258
259
260
261
262
263
264
265
266
267
268
269
/*
 * @brief  This function handles I2C Communication timeout.
 * @param  hi2c: Pointer to a struct i2c_handle_s structure that contains
 *               the configuration information for the specified I2C.
 * @param  flag: Specifies the I2C flag to check
 * @param  awaited_value: The awaited bit value for the flag (0 or 1)
 * @param  timeout_ref: Reference to target timeout
 * @retval 0 if OK, negative value else
 */
static int i2c_wait_flag(struct i2c_handle_s *hi2c, uint32_t flag,
			 uint8_t awaited_value, uint64_t timeout_ref)
{
	for ( ; ; ) {
		uint32_t isr = mmio_read_32(hi2c->i2c_base_addr + I2C_ISR);
Yann Gautier's avatar
Yann Gautier committed
270

271
272
273
		if (!!(isr & flag) != !!awaited_value) {
			return 0;
		}
Yann Gautier's avatar
Yann Gautier committed
274

275
276
277
		if (timeout_elapsed(timeout_ref)) {
			notif_i2c_timeout(hi2c);
			hi2c->lock = 0;
Yann Gautier's avatar
Yann Gautier committed
278

279
280
281
282
			return -EIO;
		}
	}
}
Yann Gautier's avatar
Yann Gautier committed
283

284
285
286
287
288
289
290
291
292
293
294
295
/*
 * @brief  This function handles Acknowledge failed detection during
 *	   an I2C Communication.
 * @param  hi2c: Pointer to a struct i2c_handle_s structure that contains
 *               the configuration information for the specified I2C.
 * @param  timeout_ref: Reference to target timeout
 * @retval 0 if OK, negative value else
 */
static int i2c_ack_failed(struct i2c_handle_s *hi2c, uint64_t timeout_ref)
{
	if ((mmio_read_32(hi2c->i2c_base_addr + I2C_ISR) & I2C_FLAG_AF) == 0U) {
		return 0;
Yann Gautier's avatar
Yann Gautier committed
296
297
298
	}

	/*
299
300
	 * Wait until STOP Flag is reset.
	 * AutoEnd should be initiate after AF.
Yann Gautier's avatar
Yann Gautier committed
301
	 */
302
303
304
305
306
	while ((mmio_read_32(hi2c->i2c_base_addr + I2C_ISR) &
		I2C_FLAG_STOPF) == 0U) {
		if (timeout_elapsed(timeout_ref)) {
			notif_i2c_timeout(hi2c);
			hi2c->lock = 0;
Yann Gautier's avatar
Yann Gautier committed
307
308
309
310
311

			return -EIO;
		}
	}

312
313
	mmio_write_32(hi2c->i2c_base_addr + I2C_ICR, I2C_FLAG_AF);

Yann Gautier's avatar
Yann Gautier committed
314
315
	mmio_write_32(hi2c->i2c_base_addr + I2C_ICR, I2C_FLAG_STOPF);

316
317
	i2c_flush_txdr(hi2c);

Yann Gautier's avatar
Yann Gautier committed
318
319
	mmio_clrbits_32(hi2c->i2c_base_addr + I2C_CR2, I2C_RESET_CR2);

320
	hi2c->i2c_err |= I2C_ERROR_AF;
Yann Gautier's avatar
Yann Gautier committed
321
	hi2c->i2c_state = I2C_STATE_READY;
322
	hi2c->i2c_mode = I2C_MODE_NONE;
Yann Gautier's avatar
Yann Gautier committed
323
324
325

	hi2c->lock = 0;

326
	return -EIO;
Yann Gautier's avatar
Yann Gautier committed
327
328
329
}

/*
330
331
 * @brief  This function handles I2C Communication timeout for specific usage
 *	   of TXIS flag.
Yann Gautier's avatar
Yann Gautier committed
332
333
 * @param  hi2c: Pointer to a struct i2c_handle_s structure that contains
 *               the configuration information for the specified I2C.
334
 * @param  timeout_ref: Reference to target timeout
Yann Gautier's avatar
Yann Gautier committed
335
336
 * @retval 0 if OK, negative value else
 */
337
static int i2c_wait_txis(struct i2c_handle_s *hi2c, uint64_t timeout_ref)
Yann Gautier's avatar
Yann Gautier committed
338
{
339
340
341
	while ((mmio_read_32(hi2c->i2c_base_addr + I2C_ISR) &
		I2C_FLAG_TXIS) == 0U) {
		if (i2c_ack_failed(hi2c, timeout_ref) != 0) {
Yann Gautier's avatar
Yann Gautier committed
342
343
344
			return -EIO;
		}

345
346
347
		if (timeout_elapsed(timeout_ref)) {
			notif_i2c_timeout(hi2c);
			hi2c->lock = 0;
Yann Gautier's avatar
Yann Gautier committed
348

349
			return -EIO;
Yann Gautier's avatar
Yann Gautier committed
350
351
352
353
354
355
356
		}
	}

	return 0;
}

/*
357
358
 * @brief  This function handles I2C Communication timeout for specific
 *	   usage of STOP flag.
Yann Gautier's avatar
Yann Gautier committed
359
360
 * @param  hi2c: Pointer to a struct i2c_handle_s structure that contains
 *               the configuration information for the specified I2C.
361
 * @param  timeout_ref: Reference to target timeout
Yann Gautier's avatar
Yann Gautier committed
362
363
 * @retval 0 if OK, negative value else
 */
364
static int i2c_wait_stop(struct i2c_handle_s *hi2c, uint64_t timeout_ref)
Yann Gautier's avatar
Yann Gautier committed
365
{
366
367
368
369
370
	while ((mmio_read_32(hi2c->i2c_base_addr + I2C_ISR) &
		 I2C_FLAG_STOPF) == 0U) {
		if (i2c_ack_failed(hi2c, timeout_ref) != 0) {
			return -EIO;
		}
Yann Gautier's avatar
Yann Gautier committed
371

372
373
		if (timeout_elapsed(timeout_ref)) {
			notif_i2c_timeout(hi2c);
Yann Gautier's avatar
Yann Gautier committed
374
375
376
377
			hi2c->lock = 0;

			return -EIO;
		}
378
	}
Yann Gautier's avatar
Yann Gautier committed
379

380
381
	return 0;
}
Yann Gautier's avatar
Yann Gautier committed
382

383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
/*
 * @brief  Handles I2Cx communication when starting transfer or during transfer
 *	   (TC or TCR flag are set).
 * @param  hi2c: I2C handle
 * @param  dev_addr: Specifies the slave address to be programmed
 * @param  size: Specifies the number of bytes to be programmed.
 *   This parameter must be a value between 0 and 255.
 * @param  i2c_mode: New state of the I2C START condition generation.
 *   This parameter can be one of the following values:
 *     @arg @ref I2C_RELOAD_MODE: Enable Reload mode.
 *     @arg @ref I2C_AUTOEND_MODE: Enable Automatic end mode.
 *     @arg @ref I2C_SOFTEND_MODE: Enable Software end mode.
 * @param  request: New state of the I2C START condition generation.
 *   This parameter can be one of the following values:
 *     @arg @ref I2C_NO_STARTSTOP: Don't Generate stop and start condition.
 *     @arg @ref I2C_GENERATE_STOP: Generate stop condition
 *                                  (size should be set to 0).
 *     @arg @ref I2C_GENERATE_START_READ: Generate Restart for read request.
 *     @arg @ref I2C_GENERATE_START_WRITE: Generate Restart for write request.
 * @retval None
 */
static void i2c_transfer_config(struct i2c_handle_s *hi2c, uint16_t dev_addr,
				uint16_t size, uint32_t i2c_mode,
				uint32_t request)
{
	uint32_t clr_value, set_value;
Yann Gautier's avatar
Yann Gautier committed
409

410
411
412
	clr_value = (I2C_CR2_SADD | I2C_CR2_NBYTES | I2C_CR2_RELOAD |
		     I2C_CR2_AUTOEND | I2C_CR2_START | I2C_CR2_STOP) |
		(I2C_CR2_RD_WRN & (request >> (31U - I2C_CR2_RD_WRN_OFFSET)));
Yann Gautier's avatar
Yann Gautier committed
413

414
415
416
	set_value = ((uint32_t)dev_addr & I2C_CR2_SADD) |
		(((uint32_t)size << I2C_CR2_NBYTES_OFFSET) & I2C_CR2_NBYTES) |
		i2c_mode | request;
Yann Gautier's avatar
Yann Gautier committed
417

418
	mmio_clrsetbits_32(hi2c->i2c_base_addr + I2C_CR2, clr_value, set_value);
Yann Gautier's avatar
Yann Gautier committed
419
420
421
422
423
424
425
426
427
}

/*
 * @brief  Master sends target device address followed by internal memory
 *	   address for write request.
 * @param  hi2c: Pointer to a struct i2c_handle_s structure that contains
 *               the configuration information for the specified I2C.
 * @param  dev_addr: Target device address
 * @param  mem_addr: Internal memory address
428
429
 * @param  mem_add_size: Size of internal memory address
 * @param  timeout_ref: Reference to target timeout
Yann Gautier's avatar
Yann Gautier committed
430
431
432
433
 * @retval 0 if OK, negative value else
 */
static int i2c_request_memory_write(struct i2c_handle_s *hi2c,
				    uint16_t dev_addr, uint16_t mem_addr,
434
				    uint16_t mem_add_size, uint64_t timeout_ref)
Yann Gautier's avatar
Yann Gautier committed
435
436
437
438
{
	i2c_transfer_config(hi2c, dev_addr, mem_add_size, I2C_RELOAD_MODE,
			    I2C_GENERATE_START_WRITE);

439
	if (i2c_wait_txis(hi2c, timeout_ref) != 0) {
Yann Gautier's avatar
Yann Gautier committed
440
441
442
443
444
445
446
447
448
449
450
451
		return -EIO;
	}

	if (mem_add_size == I2C_MEMADD_SIZE_8BIT) {
		/* Send Memory Address */
		mmio_write_8(hi2c->i2c_base_addr + I2C_TXDR,
			     (uint8_t)(mem_addr & 0x00FFU));
	} else {
		/* Send MSB of Memory Address */
		mmio_write_8(hi2c->i2c_base_addr + I2C_TXDR,
			     (uint8_t)((mem_addr & 0xFF00U) >> 8));

452
		if (i2c_wait_txis(hi2c, timeout_ref) != 0) {
Yann Gautier's avatar
Yann Gautier committed
453
454
455
456
457
458
459
460
			return -EIO;
		}

		/* Send LSB of Memory Address */
		mmio_write_8(hi2c->i2c_base_addr + I2C_TXDR,
			     (uint8_t)(mem_addr & 0x00FFU));
	}

461
	if (i2c_wait_flag(hi2c, I2C_FLAG_TCR, 0, timeout_ref) != 0) {
Yann Gautier's avatar
Yann Gautier committed
462
463
464
465
466
467
468
469
470
471
472
473
474
		return -EIO;
	}

	return 0;
}

/*
 * @brief  Master sends target device address followed by internal memory
 *	   address for read request.
 * @param  hi2c: Pointer to a struct i2c_handle_s structure that contains
 *               the configuration information for the specified I2C.
 * @param  dev_addr: Target device address
 * @param  mem_addr: Internal memory address
475
476
 * @param  mem_add_size: Size of internal memory address
 * @param  timeout_ref: Reference to target timeout
Yann Gautier's avatar
Yann Gautier committed
477
478
479
480
 * @retval 0 if OK, negative value else
 */
static int i2c_request_memory_read(struct i2c_handle_s *hi2c, uint16_t dev_addr,
				   uint16_t mem_addr, uint16_t mem_add_size,
481
				   uint64_t timeout_ref)
Yann Gautier's avatar
Yann Gautier committed
482
483
484
485
{
	i2c_transfer_config(hi2c, dev_addr, mem_add_size, I2C_SOFTEND_MODE,
			    I2C_GENERATE_START_WRITE);

486
	if (i2c_wait_txis(hi2c, timeout_ref) != 0) {
Yann Gautier's avatar
Yann Gautier committed
487
488
489
490
491
492
493
494
495
496
497
498
		return -EIO;
	}

	if (mem_add_size == I2C_MEMADD_SIZE_8BIT) {
		/* Send Memory Address */
		mmio_write_8(hi2c->i2c_base_addr + I2C_TXDR,
			     (uint8_t)(mem_addr & 0x00FFU));
	} else {
		/* Send MSB of Memory Address */
		mmio_write_8(hi2c->i2c_base_addr + I2C_TXDR,
			     (uint8_t)((mem_addr & 0xFF00U) >> 8));

499
		if (i2c_wait_txis(hi2c, timeout_ref) != 0) {
Yann Gautier's avatar
Yann Gautier committed
500
501
502
503
504
505
506
507
			return -EIO;
		}

		/* Send LSB of Memory Address */
		mmio_write_8(hi2c->i2c_base_addr + I2C_TXDR,
			     (uint8_t)(mem_addr & 0x00FFU));
	}

508
	if (i2c_wait_flag(hi2c, I2C_FLAG_TC, 0, timeout_ref) != 0) {
Yann Gautier's avatar
Yann Gautier committed
509
510
511
512
513
514
		return -EIO;
	}

	return 0;
}
/*
515
516
517
518
519
520
521
522
523
524
525
526
 * @brief  Generic function to write an amount of data in blocking mode
 *         (for Memory Mode and Master Mode)
 * @param  hi2c: Pointer to a struct i2c_handle_s structure that contains
 *               the configuration information for the specified I2C.
 * @param  dev_addr: Target device address
 * @param  mem_addr: Internal memory address (if Memory Mode)
 * @param  mem_add_size: Size of internal memory address (if Memory Mode)
 * @param  p_data: Pointer to data buffer
 * @param  size: Amount of data to be sent
 * @param  timeout_ms: Timeout duration in milliseconds
 * @param  mode: Communication mode
 * @retval 0 if OK, negative value else
Yann Gautier's avatar
Yann Gautier committed
527
 */
528
529
530
531
static int i2c_write(struct i2c_handle_s *hi2c, uint16_t dev_addr,
		     uint16_t mem_addr, uint16_t mem_add_size,
		     uint8_t *p_data, uint16_t size, uint32_t timeout_ms,
		     enum i2c_mode_e mode)
Yann Gautier's avatar
Yann Gautier committed
532
{
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
	uint64_t timeout_ref;
	int rc = -EIO;
	uint8_t *p_buff = p_data;
	uint32_t xfer_size;
	uint32_t xfer_count = size;

	if ((mode != I2C_MODE_MASTER) && (mode != I2C_MODE_MEM)) {
		return -1;
	}

	if ((hi2c->i2c_state != I2C_STATE_READY) || (hi2c->lock != 0U)) {
		return -EBUSY;
	}

	if ((p_data == NULL) || (size == 0U)) {
		return -EINVAL;
	}

	stm32mp_clk_enable(hi2c->clock);

	hi2c->lock = 1;

	timeout_ref = timeout_init_us(I2C_TIMEOUT_BUSY_MS * 1000);
	if (i2c_wait_flag(hi2c, I2C_FLAG_BUSY, 1, timeout_ref) != 0) {
		goto bail;
	}

	hi2c->i2c_state = I2C_STATE_BUSY_TX;
	hi2c->i2c_mode = mode;
	hi2c->i2c_err = I2C_ERROR_NONE;

	timeout_ref = timeout_init_us(timeout_ms * 1000);

	if (mode == I2C_MODE_MEM) {
		/* In Memory Mode, Send Slave Address and Memory Address */
		if (i2c_request_memory_write(hi2c, dev_addr, mem_addr,
					     mem_add_size, timeout_ref) != 0) {
			goto bail;
		}

		if (xfer_count > MAX_NBYTE_SIZE) {
			xfer_size = MAX_NBYTE_SIZE;
			i2c_transfer_config(hi2c, dev_addr, xfer_size,
					    I2C_RELOAD_MODE, I2C_NO_STARTSTOP);
		} else {
			xfer_size = xfer_count;
			i2c_transfer_config(hi2c, dev_addr, xfer_size,
					    I2C_AUTOEND_MODE, I2C_NO_STARTSTOP);
		}
	} else {
		/* In Master Mode, Send Slave Address */
		if (xfer_count > MAX_NBYTE_SIZE) {
			xfer_size = MAX_NBYTE_SIZE;
			i2c_transfer_config(hi2c, dev_addr, xfer_size,
					    I2C_RELOAD_MODE,
					    I2C_GENERATE_START_WRITE);
		} else {
			xfer_size = xfer_count;
			i2c_transfer_config(hi2c, dev_addr, xfer_size,
					    I2C_AUTOEND_MODE,
					    I2C_GENERATE_START_WRITE);
		}
	}

	do {
		if (i2c_wait_txis(hi2c, timeout_ref) != 0) {
			goto bail;
		}

		mmio_write_8(hi2c->i2c_base_addr + I2C_TXDR, *p_buff);
		p_buff++;
		xfer_count--;
		xfer_size--;

		if ((xfer_count != 0U) && (xfer_size == 0U)) {
			/* Wait until TCR flag is set */
			if (i2c_wait_flag(hi2c, I2C_FLAG_TCR, 0,
					  timeout_ref) != 0) {
				goto bail;
			}

			if (xfer_count > MAX_NBYTE_SIZE) {
				xfer_size = MAX_NBYTE_SIZE;
				i2c_transfer_config(hi2c, dev_addr,
						    xfer_size,
						    I2C_RELOAD_MODE,
						    I2C_NO_STARTSTOP);
			} else {
				xfer_size = xfer_count;
				i2c_transfer_config(hi2c, dev_addr,
						    xfer_size,
						    I2C_AUTOEND_MODE,
						    I2C_NO_STARTSTOP);
			}
		}

	} while (xfer_count > 0U);

Yann Gautier's avatar
Yann Gautier committed
631
	/*
632
633
634
	 * No need to Check TC flag, with AUTOEND mode the stop
	 * is automatically generated.
	 * Wait until STOPF flag is reset.
Yann Gautier's avatar
Yann Gautier committed
635
	 */
636
637
	if (i2c_wait_stop(hi2c, timeout_ref) != 0) {
		goto bail;
Yann Gautier's avatar
Yann Gautier committed
638
639
	}

640
641
642
643
644
645
646
647
648
649
650
651
652
653
	mmio_write_32(hi2c->i2c_base_addr + I2C_ICR, I2C_FLAG_STOPF);

	mmio_clrbits_32(hi2c->i2c_base_addr + I2C_CR2, I2C_RESET_CR2);

	hi2c->i2c_state = I2C_STATE_READY;
	hi2c->i2c_mode  = I2C_MODE_NONE;

	rc = 0;

bail:
	hi2c->lock = 0;
	stm32mp_clk_disable(hi2c->clock);

	return rc;
Yann Gautier's avatar
Yann Gautier committed
654
655
656
}

/*
657
658
 * @brief  Write an amount of data in blocking mode to a specific memory
 *         address.
Yann Gautier's avatar
Yann Gautier committed
659
660
 * @param  hi2c: Pointer to a struct i2c_handle_s structure that contains
 *               the configuration information for the specified I2C.
661
662
663
664
665
666
 * @param  dev_addr: Target device address
 * @param  mem_addr: Internal memory address
 * @param  mem_add_size: Size of internal memory address
 * @param  p_data: Pointer to data buffer
 * @param  size: Amount of data to be sent
 * @param  timeout_ms: Timeout duration in milliseconds
Yann Gautier's avatar
Yann Gautier committed
667
668
 * @retval 0 if OK, negative value else
 */
669
670
671
int stm32_i2c_mem_write(struct i2c_handle_s *hi2c, uint16_t dev_addr,
			uint16_t mem_addr, uint16_t mem_add_size,
			uint8_t *p_data, uint16_t size, uint32_t timeout_ms)
Yann Gautier's avatar
Yann Gautier committed
672
{
673
674
	return i2c_write(hi2c, dev_addr, mem_addr, mem_add_size,
			 p_data, size, timeout_ms, I2C_MODE_MEM);
Yann Gautier's avatar
Yann Gautier committed
675
676
677
}

/*
678
 * @brief  Transmits in master mode an amount of data in blocking mode.
Yann Gautier's avatar
Yann Gautier committed
679
680
 * @param  hi2c: Pointer to a struct i2c_handle_s structure that contains
 *               the configuration information for the specified I2C.
681
682
683
684
 * @param  dev_addr: Target device address
 * @param  p_data: Pointer to data buffer
 * @param  size: Amount of data to be sent
 * @param  timeout_ms: Timeout duration in milliseconds
Yann Gautier's avatar
Yann Gautier committed
685
686
 * @retval 0 if OK, negative value else
 */
687
688
689
int stm32_i2c_master_transmit(struct i2c_handle_s *hi2c, uint16_t dev_addr,
			      uint8_t *p_data, uint16_t size,
			      uint32_t timeout_ms)
Yann Gautier's avatar
Yann Gautier committed
690
{
691
692
	return i2c_write(hi2c, dev_addr, 0, 0,
			 p_data, size, timeout_ms, I2C_MODE_MASTER);
Yann Gautier's avatar
Yann Gautier committed
693
694
695
}

/*
696
697
 * @brief  Generic function to read an amount of data in blocking mode
 *         (for Memory Mode and Master Mode)
Yann Gautier's avatar
Yann Gautier committed
698
699
 * @param  hi2c: Pointer to a struct i2c_handle_s structure that contains
 *               the configuration information for the specified I2C.
700
701
702
703
704
705
706
 * @param  dev_addr: Target device address
 * @param  mem_addr: Internal memory address (if Memory Mode)
 * @param  mem_add_size: Size of internal memory address (if Memory Mode)
 * @param  p_data: Pointer to data buffer
 * @param  size: Amount of data to be sent
 * @param  timeout_ms: Timeout duration in milliseconds
 * @param  mode: Communication mode
Yann Gautier's avatar
Yann Gautier committed
707
708
 * @retval 0 if OK, negative value else
 */
709
710
711
712
static int i2c_read(struct i2c_handle_s *hi2c, uint16_t dev_addr,
		    uint16_t mem_addr, uint16_t mem_add_size,
		    uint8_t *p_data, uint16_t size, uint32_t timeout_ms,
		    enum i2c_mode_e mode)
Yann Gautier's avatar
Yann Gautier committed
713
{
714
715
716
717
718
719
720
721
722
	uint64_t timeout_ref;
	int rc = -EIO;
	uint8_t *p_buff = p_data;
	uint32_t xfer_count = size;
	uint32_t xfer_size;

	if ((mode != I2C_MODE_MASTER) && (mode != I2C_MODE_MEM)) {
		return -1;
	}
Yann Gautier's avatar
Yann Gautier committed
723

724
725
726
	if ((hi2c->i2c_state != I2C_STATE_READY) || (hi2c->lock != 0U)) {
		return -EBUSY;
	}
Yann Gautier's avatar
Yann Gautier committed
727

728
729
730
	if ((p_data == NULL) || (size == 0U)) {
		return  -EINVAL;
	}
Yann Gautier's avatar
Yann Gautier committed
731

732
733
734
735
736
737
738
	stm32mp_clk_enable(hi2c->clock);

	hi2c->lock = 1;

	timeout_ref = timeout_init_us(I2C_TIMEOUT_BUSY_MS * 1000);
	if (i2c_wait_flag(hi2c, I2C_FLAG_BUSY, 1, timeout_ref) != 0) {
		goto bail;
Yann Gautier's avatar
Yann Gautier committed
739
740
	}

741
742
743
	hi2c->i2c_state = I2C_STATE_BUSY_RX;
	hi2c->i2c_mode = mode;
	hi2c->i2c_err = I2C_ERROR_NONE;
Yann Gautier's avatar
Yann Gautier committed
744

745
746
747
748
749
750
	if (mode == I2C_MODE_MEM) {
		/* Send Memory Address */
		if (i2c_request_memory_read(hi2c, dev_addr, mem_addr,
					    mem_add_size, timeout_ref) != 0) {
			goto bail;
		}
Yann Gautier's avatar
Yann Gautier committed
751
752
753
	}

	/*
754
755
756
	 * Send Slave Address.
	 * Set NBYTES to write and reload if xfer_count > MAX_NBYTE_SIZE
	 * and generate RESTART.
Yann Gautier's avatar
Yann Gautier committed
757
	 */
758
759
760
761
762
763
764
765
766
767
768
769
770
771
	if (xfer_count > MAX_NBYTE_SIZE) {
		xfer_size = MAX_NBYTE_SIZE;
		i2c_transfer_config(hi2c, dev_addr, xfer_size,
				    I2C_RELOAD_MODE, I2C_GENERATE_START_READ);
	} else {
		xfer_size = xfer_count;
		i2c_transfer_config(hi2c, dev_addr, xfer_size,
				    I2C_AUTOEND_MODE, I2C_GENERATE_START_READ);
	}

	do {
		if (i2c_wait_flag(hi2c, I2C_FLAG_RXNE, 0, timeout_ref) != 0) {
			goto bail;
		}
Yann Gautier's avatar
Yann Gautier committed
772

773
774
775
776
		*p_buff = mmio_read_8(hi2c->i2c_base_addr + I2C_RXDR);
		p_buff++;
		xfer_size--;
		xfer_count--;
Yann Gautier's avatar
Yann Gautier committed
777

778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
		if ((xfer_count != 0U) && (xfer_size == 0U)) {
			if (i2c_wait_flag(hi2c, I2C_FLAG_TCR, 0,
					  timeout_ref) != 0) {
				goto bail;
			}

			if (xfer_count > MAX_NBYTE_SIZE) {
				xfer_size = MAX_NBYTE_SIZE;
				i2c_transfer_config(hi2c, dev_addr,
						    xfer_size,
						    I2C_RELOAD_MODE,
						    I2C_NO_STARTSTOP);
			} else {
				xfer_size = xfer_count;
				i2c_transfer_config(hi2c, dev_addr,
						    xfer_size,
						    I2C_AUTOEND_MODE,
						    I2C_NO_STARTSTOP);
Yann Gautier's avatar
Yann Gautier committed
796
797
			}
		}
798
	} while (xfer_count > 0U);
Yann Gautier's avatar
Yann Gautier committed
799

800
801
802
803
804
805
806
807
	/*
	 * No need to Check TC flag, with AUTOEND mode the stop
	 * is automatically generated.
	 * Wait until STOPF flag is reset.
	 */
	if (i2c_wait_stop(hi2c, timeout_ref) != 0) {
		goto bail;
	}
Yann Gautier's avatar
Yann Gautier committed
808
809
810
811
812
813
814
815

	mmio_write_32(hi2c->i2c_base_addr + I2C_ICR, I2C_FLAG_STOPF);

	mmio_clrbits_32(hi2c->i2c_base_addr + I2C_CR2, I2C_RESET_CR2);

	hi2c->i2c_state = I2C_STATE_READY;
	hi2c->i2c_mode = I2C_MODE_NONE;

816
817
818
	rc = 0;

bail:
Yann Gautier's avatar
Yann Gautier committed
819
	hi2c->lock = 0;
820
	stm32mp_clk_disable(hi2c->clock);
Yann Gautier's avatar
Yann Gautier committed
821

822
	return rc;
Yann Gautier's avatar
Yann Gautier committed
823
824
825
}

/*
826
827
828
829
830
831
832
833
834
835
836
 * @brief  Read an amount of data in blocking mode from a specific memory
 *	   address.
 * @param  hi2c: Pointer to a struct i2c_handle_s structure that contains
 *               the configuration information for the specified I2C.
 * @param  dev_addr: Target device address
 * @param  mem_addr: Internal memory address
 * @param  mem_add_size: Size of internal memory address
 * @param  p_data: Pointer to data buffer
 * @param  size: Amount of data to be sent
 * @param  timeout_ms: Timeout duration in milliseconds
 * @retval 0 if OK, negative value else
Yann Gautier's avatar
Yann Gautier committed
837
 */
838
839
840
int stm32_i2c_mem_read(struct i2c_handle_s *hi2c, uint16_t dev_addr,
		       uint16_t mem_addr, uint16_t mem_add_size,
		       uint8_t *p_data, uint16_t size, uint32_t timeout_ms)
Yann Gautier's avatar
Yann Gautier committed
841
{
842
843
	return i2c_read(hi2c, dev_addr, mem_addr, mem_add_size,
			p_data, size, timeout_ms, I2C_MODE_MEM);
Yann Gautier's avatar
Yann Gautier committed
844
845
846
}

/*
847
 * @brief  Receives in master mode an amount of data in blocking mode.
Yann Gautier's avatar
Yann Gautier committed
848
 * @param  hi2c: Pointer to a struct i2c_handle_s structure that contains
849
850
851
852
853
 *               the configuration information for the specified I2C.
 * @param  dev_addr: Target device address
 * @param  p_data: Pointer to data buffer
 * @param  size: Amount of data to be sent
 * @param  timeout_ms: Timeout duration in milliseconds
Yann Gautier's avatar
Yann Gautier committed
854
855
 * @retval 0 if OK, negative value else
 */
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
int stm32_i2c_master_receive(struct i2c_handle_s *hi2c, uint16_t dev_addr,
			     uint8_t *p_data, uint16_t size,
			     uint32_t timeout_ms)
{
	return i2c_read(hi2c, dev_addr, 0, 0,
			p_data, size, timeout_ms, I2C_MODE_MASTER);
}

/*
 * @brief  Checks if target device is ready for communication.
 * @note   This function is used with Memory devices
 * @param  hi2c: Pointer to a struct i2c_handle_s structure that contains
 *               the configuration information for the specified I2C.
 * @param  dev_addr: Target device address
 * @param  trials: Number of trials
 * @param  timeout_ms: Timeout duration in milliseconds
 * @retval True if device is ready, false else
 */
bool stm32_i2c_is_device_ready(struct i2c_handle_s *hi2c,
			       uint16_t dev_addr, uint32_t trials,
			       uint32_t timeout_ms)
Yann Gautier's avatar
Yann Gautier committed
877
{
878
879
880
	uint32_t i2c_trials = 0U;
	bool rc = false;

Yann Gautier's avatar
Yann Gautier committed
881
	if ((hi2c->i2c_state != I2C_STATE_READY) || (hi2c->lock != 0U)) {
882
		return rc;
Yann Gautier's avatar
Yann Gautier committed
883
884
	}

885
886
	stm32mp_clk_enable(hi2c->clock);

Yann Gautier's avatar
Yann Gautier committed
887
	hi2c->lock = 1;
888
889
890
891
892
893
	hi2c->i2c_mode = I2C_MODE_NONE;

	if ((mmio_read_32(hi2c->i2c_base_addr + I2C_ISR) & I2C_FLAG_BUSY) !=
	    0U) {
		goto bail;
	}
Yann Gautier's avatar
Yann Gautier committed
894
895

	hi2c->i2c_state = I2C_STATE_BUSY;
896
	hi2c->i2c_err = I2C_ERROR_NONE;
Yann Gautier's avatar
Yann Gautier committed
897

898
899
	do {
		uint64_t timeout_ref;
Yann Gautier's avatar
Yann Gautier committed
900

901
902
903
904
905
906
907
908
909
910
911
912
913
		/* Generate Start */
		if ((mmio_read_32(hi2c->i2c_base_addr + I2C_OAR1) &
		     I2C_OAR1_OA1MODE) == 0) {
			mmio_write_32(hi2c->i2c_base_addr + I2C_CR2,
				      (((uint32_t)dev_addr & I2C_CR2_SADD) |
				       I2C_CR2_START | I2C_CR2_AUTOEND) &
				      ~I2C_CR2_RD_WRN);
		} else {
			mmio_write_32(hi2c->i2c_base_addr + I2C_CR2,
				      (((uint32_t)dev_addr & I2C_CR2_SADD) |
				       I2C_CR2_START | I2C_CR2_ADD10) &
				      ~I2C_CR2_RD_WRN);
		}
Yann Gautier's avatar
Yann Gautier committed
914

915
916
917
918
919
920
921
922
923
924
925
		/*
		 * No need to Check TC flag, with AUTOEND mode the stop
		 * is automatically generated.
		 * Wait until STOPF flag is set or a NACK flag is set.
		 */
		timeout_ref = timeout_init_us(timeout_ms * 1000);
		do {
			if ((mmio_read_32(hi2c->i2c_base_addr + I2C_ISR) &
			     (I2C_FLAG_STOPF | I2C_FLAG_AF)) != 0U) {
				break;
			}
Yann Gautier's avatar
Yann Gautier committed
926

927
928
929
930
931
			if (timeout_elapsed(timeout_ref)) {
				notif_i2c_timeout(hi2c);
				goto bail;
			}
		} while (true);
Yann Gautier's avatar
Yann Gautier committed
932

933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
		if ((mmio_read_32(hi2c->i2c_base_addr + I2C_ISR) &
		     I2C_FLAG_AF) == 0U) {
			if (i2c_wait_flag(hi2c, I2C_FLAG_STOPF, 0,
					  timeout_ref) != 0) {
				goto bail;
			}

			mmio_write_32(hi2c->i2c_base_addr + I2C_ICR,
				      I2C_FLAG_STOPF);

			hi2c->i2c_state = I2C_STATE_READY;

			rc = true;
			goto bail;
		}

		if (i2c_wait_flag(hi2c, I2C_FLAG_STOPF, 0, timeout_ref) != 0) {
			goto bail;
		}

		mmio_write_32(hi2c->i2c_base_addr + I2C_ICR, I2C_FLAG_AF);

		mmio_write_32(hi2c->i2c_base_addr + I2C_ICR, I2C_FLAG_STOPF);

		if (i2c_trials == trials) {
			mmio_setbits_32(hi2c->i2c_base_addr + I2C_CR2,
					I2C_CR2_STOP);
Yann Gautier's avatar
Yann Gautier committed
960

961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
			if (i2c_wait_flag(hi2c, I2C_FLAG_STOPF, 0,
					  timeout_ref) != 0) {
				goto bail;
			}

			mmio_write_32(hi2c->i2c_base_addr + I2C_ICR,
				      I2C_FLAG_STOPF);
		}

		i2c_trials++;
	} while (i2c_trials < trials);

	notif_i2c_timeout(hi2c);

bail:
Yann Gautier's avatar
Yann Gautier committed
976
	hi2c->lock = 0;
977
	stm32mp_clk_disable(hi2c->clock);
Yann Gautier's avatar
Yann Gautier committed
978

979
	return rc;
Yann Gautier's avatar
Yann Gautier committed
980
}
981