@@ -16,6 +16,8 @@ LOG_MODULE_REGISTER(i2c_litex_litei2c, CONFIG_I2C_LOG_LEVEL);
16
16
17
17
#include <soc.h>
18
18
19
+ #define I2C_LITEX_ANY_HAS_IRQ DT_ANY_INST_HAS_PROP_STATUS_OKAY(interrupts)
20
+
19
21
#define MASTER_STATUS_TX_READY_OFFSET 0x0
20
22
#define MASTER_STATUS_RX_READY_OFFSET 0x1
21
23
#define MASTER_STATUS_NACK_OFFSET 0x8
@@ -28,10 +30,18 @@ struct i2c_litex_litei2c_config {
28
30
uint32_t master_rxtx_addr ;
29
31
uint32_t master_status_addr ;
30
32
uint32_t bitrate ;
33
+ #if I2C_LITEX_ANY_HAS_IRQ
34
+ uint32_t master_ev_pending_addr ;
35
+ uint32_t master_ev_enable_addr ;
36
+ void (* irq_config_func )(const struct device * dev );
37
+ #endif /* I2C_LITEX_ANY_HAS_IRQ */
31
38
};
32
39
33
40
struct i2c_litex_litei2c_data {
34
41
struct k_mutex mutex ;
42
+ #if I2C_LITEX_ANY_HAS_IRQ
43
+ struct k_sem sem_rx_ready ;
44
+ #endif /* I2C_LITEX_ANY_HAS_IRQ */
35
45
};
36
46
37
47
static int i2c_litex_configure (const struct device * dev , uint32_t dev_config )
@@ -105,6 +115,26 @@ static int i2c_litex_write_settings(const struct device *dev, uint8_t len_tx, ui
105
115
return 0 ;
106
116
}
107
117
118
+ static void i2c_litex_wait_for_rx_ready (const struct device * dev )
119
+ {
120
+ const struct i2c_litex_litei2c_config * config = dev -> config ;
121
+
122
+ #if I2C_LITEX_ANY_HAS_IRQ
123
+ struct i2c_litex_litei2c_data * data = dev -> data ;
124
+
125
+ if (config -> irq_config_func ) {
126
+ /* Wait for the RX ready event */
127
+ k_sem_take (& data -> sem_rx_ready , K_FOREVER );
128
+ return ;
129
+ }
130
+ #endif /* I2C_LITEX_ANY_HAS_IRQ */
131
+
132
+ while (!(litex_read8 (config -> master_status_addr ) &
133
+ BIT (MASTER_STATUS_RX_READY_OFFSET ))) {
134
+ ;
135
+ }
136
+ }
137
+
108
138
static int i2c_litex_transfer (const struct device * dev , struct i2c_msg * msgs , uint8_t num_msgs ,
109
139
uint16_t addr )
110
140
{
@@ -130,6 +160,21 @@ static int i2c_litex_transfer(const struct device *dev, struct i2c_msg *msgs, ui
130
160
131
161
litex_write8 (1 , config -> master_active_addr );
132
162
163
+ /* Flush RX buffer */
164
+ while ((litex_read8 (config -> master_status_addr ) &
165
+ BIT (MASTER_STATUS_RX_READY_OFFSET ))) {
166
+ rx_buf = litex_read32 (config -> master_rxtx_addr );
167
+ LOG_DBG ("flushed rxd: 0x%x" , rx_buf );
168
+ }
169
+
170
+ #if I2C_LITEX_ANY_HAS_IRQ
171
+ if (config -> irq_config_func ) {
172
+ litex_write8 (BIT (0 ), config -> master_ev_enable_addr );
173
+ litex_write8 (BIT (0 ), config -> master_ev_pending_addr );
174
+ k_sem_reset (& data -> sem_rx_ready );
175
+ }
176
+ #endif /* I2C_LITEX_ANY_HAS_IRQ */
177
+
133
178
LOG_DBG ("addr: 0x%x" , addr );
134
179
litex_write8 ((uint8_t )addr , config -> master_addr_addr );
135
180
@@ -214,6 +259,8 @@ static int i2c_litex_transfer(const struct device *dev, struct i2c_msg *msgs, ui
214
259
LOG_DBG ("tx_buf: 0x%x" , tx_buf );
215
260
litex_write32 (tx_buf , config -> master_rxtx_addr );
216
261
262
+ i2c_litex_wait_for_rx_ready (dev );
263
+
217
264
while (!(litex_read8 (config -> master_status_addr ) &
218
265
BIT (MASTER_STATUS_RX_READY_OFFSET ))) {
219
266
;
@@ -269,6 +316,12 @@ static int i2c_litex_transfer(const struct device *dev, struct i2c_msg *msgs, ui
269
316
270
317
litex_write8 (0 , config -> master_active_addr );
271
318
319
+ #if I2C_LITEX_ANY_HAS_IRQ
320
+ if (config -> irq_config_func ) {
321
+ litex_write8 (0 , config -> master_ev_enable_addr );
322
+ }
323
+ #endif /* I2C_LITEX_ANY_HAS_IRQ */
324
+
272
325
k_mutex_unlock (& data -> mutex );
273
326
274
327
return ret ;
@@ -304,19 +357,40 @@ static int i2c_litex_recover_bus(const struct device *dev)
304
357
return 0 ;
305
358
}
306
359
307
- static int i2c_litex_init (const struct device * dev )
360
+ #if I2C_LITEX_ANY_HAS_IRQ
361
+ static void i2c_litex_irq_handler (const struct device * dev )
308
362
{
309
363
const struct i2c_litex_litei2c_config * config = dev -> config ;
310
364
struct i2c_litex_litei2c_data * data = dev -> data ;
311
- int ret ;
312
365
313
- k_mutex_init (& data -> mutex );
366
+ if (litex_read8 (config -> master_ev_pending_addr ) & BIT (0 )) {
367
+ k_sem_give (& data -> sem_rx_ready );
368
+
369
+ /* ack reader irq */
370
+ litex_write8 (BIT (0 ), config -> master_ev_pending_addr );
371
+ }
372
+ }
373
+ #endif /* I2C_LITEX_ANY_HAS_IRQ */
374
+
375
+ static int i2c_litex_init (const struct device * dev )
376
+ {
377
+ const struct i2c_litex_litei2c_config * config = dev -> config ;
378
+ int ret ;
314
379
315
380
ret = i2c_litex_configure (dev , I2C_MODE_CONTROLLER | i2c_map_dt_bitrate (config -> bitrate ));
316
381
if (ret != 0 ) {
317
382
LOG_ERR ("failed to configure I2C: %d" , ret );
318
383
}
319
384
385
+ #if I2C_LITEX_ANY_HAS_IRQ
386
+ if (config -> irq_config_func != NULL ) {
387
+ /* Disable interrupts initially */
388
+ litex_write8 (0 , config -> master_ev_enable_addr );
389
+
390
+ config -> irq_config_func (dev );
391
+ }
392
+ #endif /* I2C_LITEX_ANY_HAS_IRQ */
393
+
320
394
return ret ;
321
395
}
322
396
@@ -332,8 +406,34 @@ static DEVICE_API(i2c, i2c_litex_litei2c_driver_api) = {
332
406
333
407
/* Device Instantiation */
334
408
409
+ #define I2C_LITEX_IRQ (n ) \
410
+ BUILD_ASSERT(DT_INST_REG_HAS_NAME(n, master_ev_pending) && \
411
+ DT_INST_REG_HAS_NAME(n, master_ev_enable), "registers for interrupts missing"); \
412
+ \
413
+ static void i2c_litex_irq_config##n(const struct device *dev) \
414
+ { \
415
+ IRQ_CONNECT(DT_INST_IRQN(n), DT_INST_IRQ(n, priority), i2c_litex_irq_handler, \
416
+ DEVICE_DT_INST_GET(n), 0); \
417
+ \
418
+ irq_enable(DT_INST_IRQN(n)); \
419
+ };
420
+
421
+ #define I2C_LITEC_IRQ_DATA (n ) \
422
+ .sem_rx_ready = Z_SEM_INITIALIZER(i2c_litex_litei2c_data_##n.sem_rx_ready, 0, 1),
423
+
424
+ #define I2C_LITEC_IRQ_CONFIG (n ) \
425
+ .master_ev_pending_addr = DT_INST_REG_ADDR_BY_NAME_OR(n, master_ev_pending, 0), \
426
+ .master_ev_enable_addr = DT_INST_REG_ADDR_BY_NAME_OR(n, master_ev_enable, 0), \
427
+ .irq_config_func = COND_CODE_1(DT_INST_IRQ_HAS_IDX(n, 0), \
428
+ (i2c_litex_irq_config##n), (NULL)),
429
+
335
430
#define I2C_LITEX_INIT (n ) \
336
- static struct i2c_litex_litei2c_data i2c_litex_litei2c_data_##n; \
431
+ IF_ENABLED(DT_INST_IRQ_HAS_IDX(n, 0), (I2C_LITEX_IRQ(n))) \
432
+ \
433
+ static struct i2c_litex_litei2c_data i2c_litex_litei2c_data_##n = { \
434
+ .mutex = Z_MUTEX_INITIALIZER(i2c_litex_litei2c_data_##n.mutex), \
435
+ IF_ENABLED(I2C_LITEX_ANY_HAS_IRQ, (I2C_LITEC_IRQ_DATA(n))) \
436
+ }; \
337
437
\
338
438
static const struct i2c_litex_litei2c_config i2c_litex_litei2c_config_##n = { \
339
439
.phy_speed_mode_addr = DT_INST_REG_ADDR_BY_NAME(n, phy_speed_mode), \
@@ -343,6 +443,7 @@ static DEVICE_API(i2c, i2c_litex_litei2c_driver_api) = {
343
443
.master_rxtx_addr = DT_INST_REG_ADDR_BY_NAME(n, master_rxtx), \
344
444
.master_status_addr = DT_INST_REG_ADDR_BY_NAME(n, master_status), \
345
445
.bitrate = DT_INST_PROP(n, clock_frequency), \
446
+ IF_ENABLED(I2C_LITEX_ANY_HAS_IRQ, (I2C_LITEC_IRQ_CONFIG(n))) \
346
447
}; \
347
448
\
348
449
I2C_DEVICE_DT_INST_DEFINE(n, i2c_litex_init, NULL, &i2c_litex_litei2c_data_##n, \
0 commit comments