forked from zephyrproject-rtos/zephyr
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathi3c_mcux.c
2125 lines (1791 loc) · 54 KB
/
i3c_mcux.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
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
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
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
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2016 Freescale Semiconductor, Inc.
* Copyright (c) 2019 NXP
* Copyright (c) 2022 Intel Corporation
*
* SPDX-License-Identifier: Apache-2.0
*/
#define DT_DRV_COMPAT nxp_mcux_i3c
#include <string.h>
#include <zephyr/device.h>
#include <zephyr/irq.h>
#include <zephyr/sys/__assert.h>
#include <zephyr/sys/sys_io.h>
#include <zephyr/drivers/clock_control.h>
#include <zephyr/drivers/i3c.h>
#include <zephyr/drivers/pinctrl.h>
/*
* This is from NXP HAL which contains register bits macros
* which are used in this driver.
*/
#include <fsl_i3c.h>
#include <zephyr/logging/log.h>
LOG_MODULE_REGISTER(i3c_mcux, CONFIG_I3C_MCUX_LOG_LEVEL);
#define I3C_MCTRL_REQUEST_NONE I3C_MCTRL_REQUEST(0)
#define I3C_MCTRL_REQUEST_EMIT_START_ADDR I3C_MCTRL_REQUEST(1)
#define I3C_MCTRL_REQUEST_EMIT_STOP I3C_MCTRL_REQUEST(2)
#define I3C_MCTRL_REQUEST_IBI_ACK_NACK I3C_MCTRL_REQUEST(3)
#define I3C_MCTRL_REQUEST_PROCESS_DAA I3C_MCTRL_REQUEST(4)
#define I3C_MCTRL_REQUEST_FORCE_EXIT I3C_MCTRL_REQUEST(6)
#define I3C_MCTRL_REQUEST_AUTO_IBI I3C_MCTRL_REQUEST(7)
#define I3C_MCTRL_IBIRESP_ACK I3C_MCTRL_IBIRESP(0)
#define I3C_MCTRL_IBIRESP_ACK_AUTO I3C_MCTRL_IBIRESP(0)
#define I3C_MCTRL_IBIRESP_NACK I3C_MCTRL_IBIRESP(1)
#define I3C_MCTRL_IBIRESP_ACK_WITH_BYTE I3C_MCTRL_IBIRESP(2)
#define I3C_MCTRL_IBIRESP_MANUAL I3C_MCTRL_IBIRESP(3)
#define I3C_MCTRL_TYPE_I3C I3C_MCTRL_TYPE(0)
#define I3C_MCTRL_TYPE_I2C I3C_MCTRL_TYPE(1)
#define I3C_MCTRL_DIR_WRITE I3C_MCTRL_DIR(0)
#define I3C_MCTRL_DIR_READ I3C_MCTRL_DIR(1)
#define I3C_MSTATUS_STATE_IDLE I3C_MSTATUS_STATE(0)
#define I3C_MSTATUS_STATE_SLVREQ I3C_MSTATUS_STATE(1)
#define I3C_MSTATUS_STATE_MSGSDR I3C_MSTATUS_STATE(2)
#define I3C_MSTATUS_STATE_NORMACT I3C_MSTATUS_STATE(3)
#define I3C_MSTATUS_STATE_MSGDDR I3C_MSTATUS_STATE(4)
#define I3C_MSTATUS_STATE_DAA I3C_MSTATUS_STATE(5)
#define I3C_MSTATUS_STATE_IBIACK I3C_MSTATUS_STATE(6)
#define I3C_MSTATUS_STATE_IBIRCV I3C_MSTATUS_STATE(7)
#define I3C_MSTATUS_IBITYPE_NONE I3C_MSTATUS_IBITYPE(0)
#define I3C_MSTATUS_IBITYPE_IBI I3C_MSTATUS_IBITYPE(1)
#define I3C_MSTATUS_IBITYPE_MR I3C_MSTATUS_IBITYPE(2)
#define I3C_MSTATUS_IBITYPE_HJ I3C_MSTATUS_IBITYPE(3)
struct mcux_i3c_config {
/** Common I3C Driver Config */
struct i3c_driver_config common;
/** Pointer to controller registers. */
I3C_Type *base;
/** Pointer to the clock device. */
const struct device *clock_dev;
/** Clock control subsys related struct. */
clock_control_subsys_t clock_subsys;
/** Pointer to pin control device. */
const struct pinctrl_dev_config *pincfg;
/** Interrupt configuration function. */
void (*irq_config_func)(const struct device *dev);
};
struct mcux_i3c_data {
/** Common I3C Driver Data */
struct i3c_driver_data common;
/** Configuration parameter to be used with HAL. */
i3c_master_config_t ctrl_config_hal;
/** Semaphore to serialize access for applications. */
struct k_sem lock;
/** Semaphore to serialize access for IBIs. */
struct k_sem ibi_lock;
struct {
/**
* Clock divider for use when generating clock for
* I3C Push-pull mode.
*/
uint8_t clk_div_pp;
/**
* Clock divider for use when generating clock for
* I3C open drain mode.
*/
uint8_t clk_div_od;
/**
* Clock divider for the slow time control clock.
*/
uint8_t clk_div_tc;
/** I3C open drain clock frequency in Hz. */
uint32_t i3c_od_scl_hz;
} clocks;
#ifdef CONFIG_I3C_USE_IBI
struct {
/** List of addresses used in the MIBIRULES register. */
uint8_t addr[5];
/** Number of valid addresses in MIBIRULES. */
uint8_t num_addr;
/** True if all addresses have MSB set. */
bool msb;
/**
* True if all target devices require mandatory byte
* for IBI.
*/
bool has_mandatory_byte;
} ibi;
#endif
};
/**
* @brief Read a register and test for bit matches with timeout.
*
* Please be aware that this uses @see k_busy_wait.
*
* @param reg Pointer to 32-bit Register.
* @param mask Mask to the register value.
* @param match Value to match for masked register value.
* @param init_delay_us Initial delay in microsecond before reading register
* (can be 0).
* @param step_delay_us Delay in microsecond between each read of register
* (cannot be 0).
* @param total_delay_us Total delay in microsecond before bailing out.
*
* @retval 0 If masked register value matches before time out.
* @retval -ETIMEDOUT Exhausted all delays without matching.
*/
static int reg32_poll_timeout(volatile uint32_t *reg,
uint32_t mask, uint32_t match,
uint32_t init_delay_us, uint32_t step_delay_us,
uint32_t total_delay_us)
{
uint32_t delayed = init_delay_us;
int ret = -ETIMEDOUT;
if (init_delay_us > 0U) {
k_busy_wait(init_delay_us);
}
while (delayed <= total_delay_us) {
if ((sys_read32((mm_reg_t)reg) & mask) == match) {
ret = 0;
break;
}
k_busy_wait(step_delay_us);
delayed += step_delay_us;
}
return ret;
}
/**
* @brief Update register value.
*
* @param reg Pointer to 32-bit Register.
* @param mask Mask to the register value.
* @param update Value to be updated in register.
*/
static inline void reg32_update(volatile uint32_t *reg,
uint32_t mask, uint32_t update)
{
uint32_t val = sys_read32((mem_addr_t)reg);
val &= ~mask;
val |= (update & mask);
sys_write32(val, (mem_addr_t)reg);
}
/**
* @brief Test if masked register value has certain value.
*
* @param reg Pointer to 32-bit register.
* @param mask Mask to test.
* @param match Value to match.
*
* @return True if bits in @p mask mask matches @p match, false otherwise.
*/
static inline bool reg32_test_match(volatile uint32_t *reg,
uint32_t mask, uint32_t match)
{
uint32_t val = sys_read32((mem_addr_t)reg);
return (val & mask) == match;
}
/**
* @brief Test if masked register value is the same as the mask.
*
* @param reg Pointer to 32-bit register.
* @param mask Mask to test.
*
* @return True if bits in @p mask are all set, false otherwise.
*/
static inline bool reg32_test(volatile uint32_t *reg, uint32_t mask)
{
return reg32_test_match(reg, mask, mask);
}
/**
* @breif Disable all interrupts.
*
* @param base Pointer to controller registers.
*
* @return Previous enabled interrupts.
*/
static uint32_t mcux_i3c_interrupt_disable(I3C_Type *base)
{
uint32_t intmask = base->MINTSET;
base->MINTCLR = intmask;
return intmask;
}
/**
* @brief Enable interrupts according to mask.
*
* @param base Pointer to controller registers.
* @param mask Interrupts to be enabled.
*
*/
static void mcux_i3c_interrupt_enable(I3C_Type *base, uint32_t mask)
{
base->MINTSET = mask;
}
/**
* @brief Check if there are any errors.
*
* This checks if MSTATUS has ERRWARN bit set.
*
* @retval True if there are any errors.
* @retval False if no errors.
*/
static bool mcux_i3c_has_error(I3C_Type *base)
{
uint32_t mstatus, merrwarn;
mstatus = base->MSTATUS;
if ((mstatus & I3C_MSTATUS_ERRWARN_MASK) == I3C_MSTATUS_ERRWARN_MASK) {
merrwarn = base->MERRWARN;
/*
* Note that this uses LOG_DBG() for displaying
* register values for debugging. In production builds,
* printing any error messages should be handled in
* callers of this function.
*/
LOG_DBG("ERROR: MSTATUS 0x%08x MERRWARN 0x%08x",
mstatus, merrwarn);
return true;
}
return false;
}
/**
* @brief Check if there are any errors, and if one of them is time out error.
*
* @retval True if controller times out on operation.
* @retval False if no time out error.
*/
static inline bool mcux_i3c_error_is_timeout(I3C_Type *base)
{
if (mcux_i3c_has_error(base)) {
if (reg32_test(&base->MERRWARN, I3C_MERRWARN_TIMEOUT_MASK)) {
return true;
}
}
return false;
}
/**
* @brief Check if there are any errors, and if one of them is NACK.
*
* NACK is generated when:
* 1. Target does not ACK the last used address.
* 2. All targets do not ACK on 0x7E.
*
* @retval True if NACK is received.
* @retval False if no NACK error.
*/
static inline bool mcux_i3c_error_is_nack(I3C_Type *base)
{
if (mcux_i3c_has_error(base)) {
if (reg32_test(&base->MERRWARN, I3C_MERRWARN_NACK_MASK)) {
return true;
}
}
return false;
}
/**
* @brief Test if certain bits are set in MSTATUS.
*
* @param base Pointer to controller registers.
* @param mask Bits to be tested.
*
* @retval True if @p mask bits are set.
* @retval False if @p mask bits are not set.
*/
static inline bool mcux_i3c_status_is_set(I3C_Type *base, uint32_t mask)
{
return reg32_test(&base->MSTATUS, mask);
}
/**
* @brief Spin wait for MSTATUS bit to be set.
*
* This spins forever for the bits to be set.
*
* @param base Pointer to controller registers.
* @param mask Bits to be tested.
*/
static inline void mcux_i3c_status_wait(I3C_Type *base, uint32_t mask)
{
/* Wait for bits to be set */
while (!mcux_i3c_status_is_set(base, mask)) {
k_busy_wait(1);
};
}
/**
* @brief Wait for MSTATUS bits to be set with time out.
*
* @param base Pointer to controller registers.
* @param mask Bits to be tested.
* @param init_delay_us Initial delay in microsecond before reading register
* (can be 0).
* @param step_delay_us Delay in microsecond between each read of register
* (cannot be 0).
* @param total_delay_us Total delay in microsecond before bailing out.
*
* @retval 0 If bits are set before time out.
* @retval -ETIMEDOUT Exhausted all delays.
*/
static inline int mcux_i3c_status_wait_timeout(I3C_Type *base, uint32_t mask,
uint32_t init_delay_us,
uint32_t step_delay_us,
uint32_t total_delay_us)
{
return reg32_poll_timeout(&base->MSTATUS, mask, mask,
init_delay_us, step_delay_us, total_delay_us);
}
/**
* @brief Clear the MSTATUS bits and wait for them to be cleared.
*
* This spins forever for the bits to be cleared;
*
* @param base Pointer to controller registers.
* @param mask Bits to be cleared.
*/
static inline void mcux_i3c_status_clear(I3C_Type *base, uint32_t mask)
{
/* Try to clear bit until it is cleared */
while (1) {
base->MSTATUS = mask;
if (!mcux_i3c_status_is_set(base, mask)) {
break;
}
k_busy_wait(1);
}
}
/**
* @brief Clear transfer and IBI related bits in MSTATUS.
*
* This spins forever for those bits to be cleared;
*
* @see I3C_MSTATUS_SLVSTART_MASK
* @see I3C_MSTATUS_MCTRLDONE_MASK
* @see I3C_MSTATUS_COMPLETE_MASK
* @see I3C_MSTATUS_IBIWON_MASK
* @see I3C_MSTATUS_ERRWARN_MASK
*
* @param base Pointer to controller registers.
*/
static inline void mcux_i3c_status_clear_all(I3C_Type *base)
{
uint32_t mask = I3C_MSTATUS_SLVSTART_MASK |
I3C_MSTATUS_MCTRLDONE_MASK |
I3C_MSTATUS_COMPLETE_MASK |
I3C_MSTATUS_IBIWON_MASK |
I3C_MSTATUS_ERRWARN_MASK;
mcux_i3c_status_clear(base, mask);
}
/**
* @brief Clear the MSTATUS bits and wait for them to be cleared with time out.
*
* @param base Pointer to controller registers.
* @param mask Bits to be cleared.
* @param init_delay_us Initial delay in microsecond before reading register
* (can be 0).
* @param step_delay_us Delay in microsecond between each read of register
* (cannot be 0).
* @param total_delay_us Total delay in microsecond before bailing out.
*
* @retval 0 If bits are cleared before time out.
* @retval -ETIMEDOUT Exhausted all delays.
*/
static inline int mcux_i3c_status_clear_timeout(I3C_Type *base, uint32_t mask,
uint32_t init_delay_us,
uint32_t step_delay_us,
uint32_t total_delay_us)
{
uint32_t delayed = init_delay_us;
int ret = -ETIMEDOUT;
/* Try to clear bit until it is cleared */
while (delayed <= total_delay_us) {
base->MSTATUS = mask;
if (!mcux_i3c_status_is_set(base, mask)) {
ret = 0;
break;
}
k_busy_wait(step_delay_us);
delayed += step_delay_us;
}
return ret;
}
/**
* @brief Spin wait for MSTATUS bit to be set, and clear it afterwards.
*
* Note that this spins forever waiting for bits to be set, and
* to be cleared.
*
* @see mcux_i3c_status_wait
* @see mcux_i3c_status_clear
*
* @param base Pointer to controller registers.
* @param mask Bits to be set and to be cleared;
*/
static inline void mcux_i3c_status_wait_clear(I3C_Type *base, uint32_t mask)
{
mcux_i3c_status_wait(base, mask);
mcux_i3c_status_clear(base, mask);
}
/**
* @brief Wait for MSTATUS bit to be set, and clear it afterwards, with time out.
*
* @see mcux_i3c_status_wait_timeout
* @see mcux_i3c_status_clear_timeout
*
* @param base Pointer to controller registers.
* @param mask Bits to be set and to be cleared.
* @param init_delay_us Initial delay in microsecond before reading register
* (can be 0).
* @param step_delay_us Delay in microsecond between each read of register
* (cannot be 0).
* @param total_delay_us Total delay in microsecond before bailing out.
*
* @retval 0 If masked register value matches before time out.
* @retval -ETIMEDOUT Exhausted all delays without matching.
*/
static inline int mcux_i3c_status_wait_clear_timeout(I3C_Type *base, uint32_t mask,
uint32_t init_delay_us,
uint32_t step_delay_us,
uint32_t total_delay_us)
{
int ret;
ret = mcux_i3c_status_wait_timeout(base, mask, init_delay_us,
step_delay_us, total_delay_us);
if (ret != 0) {
goto out;
}
ret = mcux_i3c_status_clear_timeout(base, mask, init_delay_us,
step_delay_us, total_delay_us);
out:
return ret;
}
/**
* @brief Clear the MERRWARN register.
*
* @param base Pointer to controller registers.
*/
static inline void mcux_i3c_errwarn_clear_all_nowait(I3C_Type *base)
{
base->MERRWARN = base->MERRWARN;
}
/**
* @brief Tell controller to start DAA process.
*
* @param base Pointer to controller registers.
*/
static inline void mcux_i3c_request_daa(I3C_Type *base)
{
reg32_update(&base->MCTRL,
I3C_MCTRL_REQUEST_MASK | I3C_MCTRL_IBIRESP_MASK | I3C_MCTRL_RDTERM_MASK,
I3C_MCTRL_REQUEST_PROCESS_DAA | I3C_MCTRL_IBIRESP_NACK);
}
/**
* @brief Tell controller to start auto IBI.
*
* This also waits for the controller to indicate auto IBI
* has started before returning.
*
* @param base Pointer to controller registers.
*/
static inline void mcux_i3c_request_auto_ibi(I3C_Type *base)
{
reg32_update(&base->MCTRL,
I3C_MCTRL_REQUEST_MASK | I3C_MCTRL_IBIRESP_MASK | I3C_MCTRL_RDTERM_MASK,
I3C_MCTRL_REQUEST_AUTO_IBI | I3C_MCTRL_IBIRESP_ACK_AUTO);
mcux_i3c_status_wait_clear(base, I3C_MSTATUS_MCTRLDONE_MASK);
}
/**
* @brief Get the controller state.
*
* @param base Pointer to controller registers.
*
* @retval I3C_MSTATUS_STATE_IDLE
* @retval I3C_MSTATUS_STATE_SLVREQ
* @retval I3C_MSTATUS_STATE_MSGSDR
* @retval I3C_MSTATUS_STATE_NORMACT
* @retval I3C_MSTATUS_STATE_MSGDDR
* @retval I3C_MSTATUS_STATE_DAA
* @retval I3C_MSTATUS_STATE_IBIACK
* @retval I3C_MSTATUS_STATE_IBIRCV
*/
static inline uint32_t mcux_i3c_state_get(I3C_Type *base)
{
uint32_t mstatus = base->MSTATUS;
uint32_t state;
/* Make sure we are in a state where we can emit STOP */
state = (mstatus & I3C_MSTATUS_STATE_MASK) >> I3C_MSTATUS_STATE_SHIFT;
return state;
}
/**
* @brief Wait for MSTATUS bit to be set, and clear it afterwards with time out.
*
* @param base Pointer to controller registers.
* @param mask Bits to be set.
* @param init_delay_us Initial delay in microsecond before reading register
* (can be 0).
* @param step_delay_us Delay in microsecond between each read of register
* (cannot be 0).
* @param total_delay_us Total delay in microsecond before bailing out.
*
* @retval 0 If masked register value matches before time out.
* @retval -ETIMEDOUT Exhausted all delays without matching.
*/
static inline int mcux_i3c_state_wait_timeout(I3C_Type *base, uint32_t state,
uint32_t init_delay_us,
uint32_t step_delay_us,
uint32_t total_delay_us)
{
uint32_t delayed = init_delay_us;
int ret = -ETIMEDOUT;
/* Try to clear bit until it is cleared */
while (delayed <= total_delay_us) {
if (mcux_i3c_state_get(base) == state) {
ret = 0;
break;
}
k_busy_wait(step_delay_us);
delayed += step_delay_us;
}
return ret;
}
/**
* @brief Tell controller to emit START.
*
* @param base Pointer to controller registers.
* @param addr Target address.
* @param is_i2c True if this is I2C transactions, false if I3C.
* @param is_read True if this is a read transaction, false if write.
* @param read_sz Number of bytes to read if @p is_read is true.
*
* @return 0 if successful, or negative if error.
*/
static int mcux_i3c_request_emit_start(I3C_Type *base, uint8_t addr, bool is_i2c,
bool is_read, size_t read_sz)
{
uint32_t mctrl;
int ret = 0;
mctrl = is_i2c ? I3C_MCTRL_TYPE_I2C : I3C_MCTRL_TYPE_I3C;
mctrl |= I3C_MCTRL_IBIRESP_NACK;
if (is_read) {
mctrl |= I3C_MCTRL_DIR_READ;
/* How many bytes to read */
mctrl |= I3C_MCTRL_RDTERM(read_sz);
} else {
mctrl |= I3C_MCTRL_DIR_WRITE;
}
mctrl |= I3C_MCTRL_REQUEST_EMIT_START_ADDR | I3C_MCTRL_ADDR(addr);
base->MCTRL = mctrl;
/* Wait for controller to say the operation is done */
ret = mcux_i3c_status_wait_clear_timeout(base, I3C_MSTATUS_MCTRLDONE_MASK,
0, 10, 1000);
if (ret == 0) {
/* Check for NACK */
if (mcux_i3c_error_is_nack(base)) {
ret = -ENODEV;
}
}
return ret;
}
/**
* @brief Tell controller to emit STOP.
*
* This emits STOP when controller is in NORMACT state as this is
* the only valid state where STOP can be emitted. This also waits
* for the controller to get out of NORMACT before returning.
*
* @param base Pointer to controller registers.
* @param wait_stop True if need to wait for controller to be
* no longer in NORMACT.
*/
static inline void mcux_i3c_request_emit_stop(I3C_Type *base, bool wait_stop)
{
/* Make sure we are in a state where we can emit STOP */
if (mcux_i3c_state_get(base) != I3C_MSTATUS_STATE_NORMACT) {
return;
}
reg32_update(&base->MCTRL,
I3C_MCTRL_REQUEST_MASK | I3C_MCTRL_DIR_MASK | I3C_MCTRL_RDTERM_MASK,
I3C_MCTRL_REQUEST_EMIT_STOP);
/*
* EMIT_STOP request doesn't result in MCTRLDONE being cleared
* so don't wait for it.
*/
if (wait_stop) {
/*
* Note that we don't exactly wait for I3C_MSTATUS_STATE_IDLE.
* If there is an incoming IBI, it will get stuck forever
* as state would be I3C_MSTATUS_STATE_SLVREQ.
*/
while (reg32_test_match(&base->MSTATUS, I3C_MSTATUS_STATE_MASK,
I3C_MSTATUS_STATE_NORMACT)) {
if (mcux_i3c_has_error(base)) {
/*
* Bail out if there is any error so
* we won't loop forever.
*/
return;
}
k_busy_wait(10);
};
}
}
/**
* @brief Tell controller to NACK the incoming IBI.
*
* @param base Pointer to controller registers.
*/
static inline void mcux_i3c_ibi_respond_nack(I3C_Type *base)
{
reg32_update(&base->MCTRL,
I3C_MCTRL_REQUEST_MASK | I3C_MCTRL_IBIRESP_MASK,
I3C_MCTRL_REQUEST_IBI_ACK_NACK | I3C_MCTRL_IBIRESP_NACK);
mcux_i3c_status_wait_clear(base, I3C_MSTATUS_MCTRLDONE_MASK);
}
/**
* @brief Tell controller to ACK the incoming IBI.
*
* @param base Pointer to controller registers.
*/
static inline void mcux_i3c_ibi_respond_ack(I3C_Type *base)
{
reg32_update(&base->MCTRL,
I3C_MCTRL_REQUEST_MASK | I3C_MCTRL_IBIRESP_MASK,
I3C_MCTRL_REQUEST_IBI_ACK_NACK | I3C_MCTRL_IBIRESP_ACK_AUTO);
mcux_i3c_status_wait_clear(base, I3C_MSTATUS_MCTRLDONE_MASK);
}
/**
* @brief Get the number of bytes in RX FIFO.
*
* This returns the number of bytes in RX FIFO which
* can be read.
*
* @param base Pointer to controller registers.
*
* @return Number of bytes in RX FIFO.
*/
static inline int mcux_i3c_fifo_rx_count_get(I3C_Type *base)
{
uint32_t mdatactrl = base->MDATACTRL;
return (int)((mdatactrl & I3C_MDATACTRL_RXCOUNT_MASK) >> I3C_MDATACTRL_RXCOUNT_SHIFT);
}
/**
* @brief Tell controller to flush both TX and RX FIFOs.
*
* @param base Pointer to controller registers.
*/
static inline void mcux_i3c_fifo_flush(I3C_Type *base)
{
base->MDATACTRL = I3C_MDATACTRL_FLUSHFB_MASK | I3C_MDATACTRL_FLUSHTB_MASK;
}
/**
* @brief Prepare the controller for transfers.
*
* This is simply a wrapper to clear out status bits,
* and error bits. Also this tells the controller to
* flush both TX and RX FIFOs.
*
* @param base Pointer to controller registers.
*/
static inline void mcux_i3c_xfer_reset(I3C_Type *base)
{
mcux_i3c_status_clear_all(base);
mcux_i3c_errwarn_clear_all_nowait(base);
mcux_i3c_fifo_flush(base);
}
/**
* @brief Drain RX FIFO.
*
* @param dev Pointer to controller device driver instance.
*/
static void mcux_i3c_fifo_rx_drain(const struct device *dev)
{
const struct mcux_i3c_config *config = dev->config;
I3C_Type *base = config->base;
uint8_t buf;
/* Read from FIFO as long as RXPEND is set. */
while (mcux_i3c_status_is_set(base, I3C_MSTATUS_RXPEND_MASK)) {
buf = base->MRDATAB;
}
}
/**
* @brief Find a registered I3C target device.
*
* This returns the I3C device descriptor of the I3C device
* matching the incoming @p id.
*
* @param dev Pointer to controller device driver instance.
* @param id Pointer to I3C device ID.
*
* @return @see i3c_device_find.
*/
static
struct i3c_device_desc *mcux_i3c_device_find(const struct device *dev,
const struct i3c_device_id *id)
{
const struct mcux_i3c_config *config = dev->config;
return i3c_dev_list_find(&config->common.dev_list, id);
}
/**
* @brief Perform bus recovery.
*
* @param dev Pointer to controller device driver instance.
*/
static int mcux_i3c_recover_bus(const struct device *dev)
{
const struct mcux_i3c_config *config = dev->config;
I3C_Type *base = config->base;
int ret = 0;
/*
* If the controller is in NORMACT state, tells it to emit STOP
* so it can return to IDLE, or is ready to clear any pending
* target initiated IBIs.
*/
if (mcux_i3c_state_get(base) == I3C_MSTATUS_STATE_NORMACT) {
mcux_i3c_request_emit_stop(base, true);
};
/* Exhaust all target initiated IBI */
while (mcux_i3c_status_is_set(base, I3C_MSTATUS_SLVSTART_MASK)) {
/* Tell the controller to perform auto IBI. */
mcux_i3c_request_auto_ibi(base);
if (mcux_i3c_status_wait_clear_timeout(base, I3C_MSTATUS_COMPLETE_MASK,
0, 10, 1000) == -ETIMEDOUT) {
break;
}
/* Once auto IBI is done, discard bytes in FIFO. */
mcux_i3c_fifo_rx_drain(dev);
/*
* There might be other IBIs waiting.
* So pause a bit to let other targets initiates
* their IBIs.
*/
k_busy_wait(100);
}
if (reg32_poll_timeout(&base->MSTATUS, I3C_MSTATUS_STATE_MASK,
I3C_MSTATUS_STATE_IDLE, 0, 10, 1000) == -ETIMEDOUT) {
ret = -EBUSY;
}
return ret;
}
/**
* @brief Perform one read transaction.
*
* This reads from RX FIFO until COMPLETE bit is set in MSTATUS
* or time out.
*
* @param base Pointer to controller registers.
* @param buf Buffer to store data.
* @param buf_sz Buffer size in bytes.
*
* @return Number of bytes read, or negative if error.
*/
static int mcux_i3c_do_one_xfer_read(I3C_Type *base, uint8_t *buf, uint8_t buf_sz)
{
int rx_count;
bool completed = false;
bool overflow = false;
int ret = 0;
int offset = 0;
while (!completed) {
/*
* Test if the COMPLETE bit is set.
*/
if (mcux_i3c_status_is_set(base, I3C_MSTATUS_COMPLETE_MASK)) {
completed = true;
}
/*
* If controller says timed out, we abort the transaction.
*/
if (mcux_i3c_has_error(base)) {
if (mcux_i3c_error_is_timeout(base)) {
ret = -ETIMEDOUT;
}
base->MERRWARN = base->MERRWARN;
goto one_xfer_read_out;
}
/*
* Transfer data from FIFO into buffer.
*/
rx_count = mcux_i3c_fifo_rx_count_get(base);
while (rx_count > 0) {
uint8_t data = (uint8_t)base->MRDATAB;
if (offset < buf_sz) {
buf[offset] = data;
offset += 1;
} else {
overflow = true;
}
rx_count -= 1;
}
}
if (overflow) {
ret = -EINVAL;
} else {
ret = offset;
}
one_xfer_read_out:
return ret;
}
/**
* @brief Perform one write transaction.
*
* This writes all data in @p buf to TX FIFO or time out
* waiting for FIFO spaces.
*
* @param base Pointer to controller registers.
* @param buf Buffer containing data to be sent.
* @param buf_sz Number of bytes in @p buf to send.
* @param no_ending True if not to signal end of write message.
*
* @return Number of bytes written, or negative if error.
*/
static int mcux_i3c_do_one_xfer_write(I3C_Type *base, uint8_t *buf, uint8_t buf_sz, bool no_ending)
{
int offset = 0;
int remaining = buf_sz;
int ret = 0;
while (remaining > 0) {
ret = reg32_poll_timeout(&base->MDATACTRL, I3C_MDATACTRL_TXFULL_MASK, 0,
0, 10, 1000);
if (ret == -ETIMEDOUT) {
goto one_xfer_write_out;
}
if ((remaining > 1) || no_ending) {
base->MWDATAB = (uint32_t)buf[offset];
} else {
base->MWDATABE = (uint32_t)buf[offset];
}
offset += 1;
remaining -= 1;
}
ret = offset;
one_xfer_write_out:
return ret;
}
/**
* @brief Perform one transfer transaction.
*
* @param base Pointer to controller registers.
* @param data Pointer to controller device instance data.
* @param addr Target address.
* @param is_i2c True if this is I2C transactions, false if I3C.
* @param buf Buffer for data to be sent or received.
* @param buf_sz Buffer size in bytes.
* @param is_read True if this is a read transaction, false if write.
* @param emit_start True if START is needed before read/write.
* @param emit_stop True if STOP is needed after read/write.
* @param no_ending True if not to signal end of write message.
*
* @return Number of bytes read/written, or negative if error.
*/
static int mcux_i3c_do_one_xfer(I3C_Type *base, struct mcux_i3c_data *data,
uint8_t addr, bool is_i2c,