-
Notifications
You must be signed in to change notification settings - Fork 6.1k
/
ucpd_stm32.c
1485 lines (1283 loc) · 40.8 KB
/
ucpd_stm32.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 2022 The Chromium OS Authors
*
* SPDX-License-Identifier: Apache-2.0
*/
#define DT_DRV_COMPAT st_stm32_ucpd
#include <zephyr/logging/log.h>
LOG_MODULE_REGISTER(ucpd_stm32, CONFIG_USBC_LOG_LEVEL);
#include <zephyr/device.h>
#include <zephyr/sys/util.h>
#include <zephyr/kernel.h>
#include <soc.h>
#include <stddef.h>
#include <zephyr/math/ilog2.h>
#include <stm32_ll_system.h>
#include <zephyr/irq.h>
#include "ucpd_stm32_priv.h"
static void config_tcpc_irq(void);
/**
* @brief UCPD TX ORDSET values
*/
static int ucpd_txorderset[] = {
/* SOP ORDSET */
LL_UCPD_ORDERED_SET_SOP,
/* SOP PRIME ORDSET */
LL_UCPD_ORDERED_SET_SOP1,
/* SOP PRIME PRIME ORDSET */
LL_UCPD_ORDERED_SET_SOP2,
/* SOP PRIME DEBUG ORDSET */
LL_UCPD_ORDERED_SET_SOP1_DEBUG,
/* SOP PRIME PRIME DEBUG ORDSET */
LL_UCPD_ORDERED_SET_SOP2_DEBUG,
/* HARD RESET ORDSET */
LL_UCPD_ORDERED_SET_HARD_RESET,
/* CABLE RESET ORDSET */
LL_UCPD_ORDERED_SET_CABLE_RESET,
};
/**
* @brief Test for a goodCRC message
*
* @retval true if message is goodCRC, else false
*/
static bool ucpd_msg_is_good_crc(union pd_header header)
{
/*
* Good CRC is a control message (no data objects) with GOOD_CRC
* message type in the header.
*/
return (header.number_of_data_objects == 0 &&
header.extended == 0 &&
header.message_type == PD_CTRL_GOOD_CRC);
}
#ifdef CONFIG_SOC_SERIES_STM32G0X
/**
* @brief Apply the UCPD CC1 and CC2 pin configurations.
*
* UCPDx_STROBE: UCPDx pull-down configuration strobe:
* when UCPDx is enabled, with CC1 and CC2 pin UCPD
* control bits configured: apply that configuration.
*/
static void update_stm32g0x_cc_line(UCPD_TypeDef *ucpd_port)
{
if ((uint32_t)(ucpd_port) == UCPD1_BASE) {
SYSCFG->CFGR1 |= SYSCFG_CFGR1_UCPD1_STROBE_Msk;
} else {
SYSCFG->CFGR1 |= SYSCFG_CFGR1_UCPD2_STROBE_Msk;
}
}
#endif
/**
* @brief Transmits a data byte from the TX data buffer
*/
static void ucpd_tx_data_byte(const struct device *dev)
{
struct tcpc_data *data = dev->data;
const struct tcpc_config *const config = dev->config;
int index = data->ucpd_tx_active_buffer->msg_index++;
LL_UCPD_WriteData(config->ucpd_port,
data->ucpd_tx_active_buffer->data.msg[index]);
}
/**
* @brief Receives a data byte and store it in the RX data buffer
*/
static void ucpd_rx_data_byte(const struct device *dev)
{
struct tcpc_data *data = dev->data;
const struct tcpc_config *const config = dev->config;
if (data->ucpd_rx_byte_count < UCPD_BUF_LEN) {
data->ucpd_rx_buffer[data->ucpd_rx_byte_count++] =
LL_UCPD_ReadData(config->ucpd_port);
}
}
/**
* @brief Enables or Disables TX interrupts
*/
static void ucpd_tx_interrupts_enable(const struct device *dev, bool enable)
{
const struct tcpc_config *const config = dev->config;
uint32_t imr;
imr = LL_UCPD_ReadReg(config->ucpd_port, IMR);
if (enable) {
LL_UCPD_WriteReg(config->ucpd_port, ICR, UCPD_ICR_TX_INT_MASK);
LL_UCPD_WriteReg(config->ucpd_port, IMR,
imr | UCPD_IMR_TX_INT_MASK);
} else {
LL_UCPD_WriteReg(config->ucpd_port, IMR,
imr & ~UCPD_IMR_TX_INT_MASK);
}
}
/**
* @brief Initializes the RX and TX state machine variables
*/
static void stm32_ucpd_state_init(const struct device *dev)
{
struct tcpc_data *data = dev->data;
/* Init variables used to manage tx process */
data->ucpd_tx_request = 0;
data->tx_retry_count = 0;
data->ucpd_tx_state = STATE_IDLE;
/* Init variables used to manage rx */
data->ucpd_rx_sop_prime_enabled = false;
data->ucpd_rx_msg_active = false;
data->ucpd_rx_bist_mode = false;
/* Vconn tracking variable */
data->ucpd_vconn_enable = false;
}
/**
* @brief Get the CC enable mask. The mask indicates which CC line
* is enabled.
*
* @retval CC Enable mask (bit 0: CC1, bit 1: CC2)
*/
static uint32_t ucpd_get_cc_enable_mask(const struct device *dev)
{
struct tcpc_data *data = dev->data;
const struct tcpc_config *const config = dev->config;
uint32_t mask = UCPD_CR_CCENABLE_Msk;
/*
* When VCONN is enabled, it is supplied on the CC line that's
* not being used for Power Delivery messages.
*/
if (data->ucpd_vconn_enable) {
uint32_t cr = LL_UCPD_ReadReg(config->ucpd_port, CR);
int pol = (cr & UCPD_CR_PHYCCSEL);
/* Dissable CC line that's used for VCONN */
mask &= ~BIT(UCPD_CR_CCENABLE_Pos + !pol);
}
return mask;
}
/**
* @brief Get the state of the CC1 and CC2 lines
*
* @retval 0 on success
* @retval -EIO on failure
*/
static int ucpd_get_cc(const struct device *dev,
enum tc_cc_voltage_state *cc1,
enum tc_cc_voltage_state *cc2)
{
const struct tcpc_config *const config = dev->config;
int vstate_cc1;
int vstate_cc2;
int anamode;
uint32_t sr;
uint32_t cc_msk;
/*
* cc_voltage_state is determined from vstate_cc bit field in the
* status register. The meaning of the value vstate_cc depends on
* current value of ANAMODE (src/snk).
*
* vstate_cc maps directly to cc_state from tcpci spec when
* ANAMODE(snk) = 1, but needs to be modified slightly for case
* ANAMODE(src) = 0.
*
* If presenting Rp (source), then need to do a circular shift of
* vstate_ccx value:
* vstate_cc | cc_state
* ------------------
* 0 -> 1
* 1 -> 2
* 2 -> 0
*/
/* Get vstate_ccx values and power role */
sr = LL_UCPD_ReadReg(config->ucpd_port, SR);
/* Get Rp or Rd active */
anamode = LL_UCPD_GetRole(config->ucpd_port);
vstate_cc1 = (sr & UCPD_SR_TYPEC_VSTATE_CC1_Msk) >>
UCPD_SR_TYPEC_VSTATE_CC1_Pos;
vstate_cc2 = (sr & UCPD_SR_TYPEC_VSTATE_CC2_Msk) >>
UCPD_SR_TYPEC_VSTATE_CC2_Pos;
/* Do circular shift if port == source */
if (anamode) {
if (vstate_cc1 != STM32_UCPD_SR_VSTATE_RA) {
vstate_cc1 += 4;
}
if (vstate_cc2 != STM32_UCPD_SR_VSTATE_RA) {
vstate_cc2 += 4;
}
} else {
if (vstate_cc1 != STM32_UCPD_SR_VSTATE_OPEN) {
vstate_cc1 = (vstate_cc1 + 1) % 3;
}
if (vstate_cc2 != STM32_UCPD_SR_VSTATE_OPEN) {
vstate_cc2 = (vstate_cc2 + 1) % 3;
}
}
/* CC connection detection */
cc_msk = ucpd_get_cc_enable_mask(dev);
/* CC1 connection detection */
if (cc_msk & UCPD_CR_CCENABLE_0) {
*cc1 = vstate_cc1;
} else {
*cc1 = TC_CC_VOLT_OPEN;
}
/* CC2 connection detection */
if (cc_msk & UCPD_CR_CCENABLE_1) {
*cc2 = vstate_cc2;
} else {
*cc2 = TC_CC_VOLT_OPEN;
}
return 0;
}
/**
* @brief Enable or Disable VCONN
*
* @retval 0 on success
* @retval -EIO on failure
* @retval -ENOTSUP if not supported
*/
static int ucpd_set_vconn(const struct device *dev, bool enable)
{
struct tcpc_data *data = dev->data;
const struct tcpc_config *const config = dev->config;
int cr;
int ret;
if (data->vconn_cb == NULL) {
return -ENOTSUP;
}
/* Update VCONN on/off status. Do this before getting cc enable mask */
data->ucpd_vconn_enable = enable;
cr = LL_UCPD_ReadReg(config->ucpd_port, CR);
cr &= ~UCPD_CR_CCENABLE_Msk;
cr |= ucpd_get_cc_enable_mask(dev);
/* Apply cc pull resistor change */
LL_UCPD_WriteReg(config->ucpd_port, CR, cr);
#ifdef CONFIG_SOC_SERIES_STM32G0X
update_stm32g0x_cc_line(config->ucpd_port);
#endif
/* Get CC line that VCONN is active on */
data->ucpd_vconn_cc = (cr & UCPD_CR_CCENABLE_0) ?
TC_POLARITY_CC2 : TC_POLARITY_CC1;
/* Call user supplied callback to set vconn */
ret = data->vconn_cb(dev, data->ucpd_vconn_cc, enable);
return ret;
}
/**
* @brief Discharge VCONN
*
* @retval 0 on success
* @retval -EIO on failure
* @retval -ENOTSUP if not supported
*/
static int ucpd_vconn_discharge(const struct device *dev, bool enable)
{
struct tcpc_data *data = dev->data;
const struct tcpc_config *const config = dev->config;
/* VCONN should not be discharged while it's enabled */
if (data->ucpd_vconn_enable) {
return -EIO;
}
if (data->vconn_discharge_cb) {
/* Use DPM supplied VCONN Discharge */
return data->vconn_discharge_cb(dev, data->ucpd_vconn_cc, enable);
}
/* Use TCPC VCONN Discharge */
if (enable) {
LL_UCPD_VconnDischargeEnable(config->ucpd_port);
} else {
LL_UCPD_VconnDischargeDisable(config->ucpd_port);
}
#ifdef CONFIG_SOC_SERIES_STM32G0X
update_stm32g0x_cc_line(config->ucpd_port);
#endif
return 0;
}
/**
* @brief Sets the value of the CC pull up resistor used when operating as a Source
*
* @retval 0 on success
*/
static int ucpd_select_rp_value(const struct device *dev, enum tc_rp_value rp)
{
struct tcpc_data *data = dev->data;
data->rp = rp;
return 0;
}
/**
* @brief Gets the value of the CC pull up resistor used when operating as a Source
*
* @retval 0 on success
*/
static int ucpd_get_rp_value(const struct device *dev, enum tc_rp_value *rp)
{
struct tcpc_data *data = dev->data;
*rp = data->rp;
return 0;
}
/**
* @brief Enable or disable Dead Battery resistors
*/
static void dead_battery(const struct device *dev, bool en)
{
struct tcpc_data *data = dev->data;
#ifdef CONFIG_SOC_SERIES_STM32G0X
const struct tcpc_config *const config = dev->config;
uint32_t cr;
cr = LL_UCPD_ReadReg(config->ucpd_port, CR);
if (en) {
cr |= UCPD_CR_DBATTEN;
} else {
cr &= ~UCPD_CR_DBATTEN;
}
LL_UCPD_WriteReg(config->ucpd_port, CR, cr);
update_stm32g0x_cc_line(config->ucpd_port);
#else
if (en) {
CLEAR_BIT(PWR->CR3, PWR_CR3_UCPD_DBDIS);
} else {
SET_BIT(PWR->CR3, PWR_CR3_UCPD_DBDIS);
}
#endif
data->dead_battery_active = en;
}
/**
* @brief Set the CC pull up or pull down resistors
*
* @retval 0 on success
* @retval -EIO on failure
*/
static int ucpd_set_cc(const struct device *dev,
enum tc_cc_pull cc_pull)
{
const struct tcpc_config *const config = dev->config;
struct tcpc_data *data = dev->data;
uint32_t cr;
/* Disable dead battery if it's active */
if (data->dead_battery_active) {
dead_battery(dev, false);
}
cr = LL_UCPD_ReadReg(config->ucpd_port, CR);
/*
* Always set ANASUBMODE to match desired Rp. TCPM layer has a valid
* range of 0, 1, or 2. This range maps to 1, 2, or 3 in ucpd for
* ANASUBMODE.
*/
cr &= ~UCPD_CR_ANASUBMODE_Msk;
cr |= STM32_UCPD_CR_ANASUBMODE_VAL(UCPD_RP_TO_ANASUB(data->rp));
/* Disconnect both pull from both CC lines for R_open case */
cr &= ~UCPD_CR_CCENABLE_Msk;
/* Set ANAMODE if cc_pull is Rd */
if (cc_pull == TC_CC_RD) {
cr |= (UCPD_CR_ANAMODE | UCPD_CR_CCENABLE_Msk);
/* Clear ANAMODE if cc_pull is Rp */
} else if (cc_pull == TC_CC_RP) {
cr &= ~(UCPD_CR_ANAMODE);
cr |= ucpd_get_cc_enable_mask(dev);
}
/* Update pull values */
LL_UCPD_WriteReg(config->ucpd_port, CR, cr);
#ifdef CONFIG_SOC_SERIES_STM32G0X
update_stm32g0x_cc_line(config->ucpd_port);
#endif
return 0;
}
/**
* @brief Set the polarity of the CC line, which is the active CC line
* used for power delivery.
*
* @retval 0 on success
* @retval -EIO on failure
* @retval -ENOTSUP if polarity is not supported
*/
static int ucpd_cc_set_polarity(const struct device *dev,
enum tc_cc_polarity polarity)
{
const struct tcpc_config *const config = dev->config;
uint32_t cr;
cr = LL_UCPD_ReadReg(config->ucpd_port, CR);
/*
* Polarity impacts the PHYCCSEL, CCENABLE, and CCxTCDIS fields. This
* function is called when polarity is updated at TCPM layer. STM32Gx
* only supports POLARITY_CC1 or POLARITY_CC2 and this is stored in the
* PHYCCSEL bit in the CR register.
*/
if (polarity == TC_POLARITY_CC1) {
cr &= ~UCPD_CR_PHYCCSEL;
} else if (polarity == TC_POLARITY_CC2) {
cr |= UCPD_CR_PHYCCSEL;
} else {
return -ENOTSUP;
}
/* Update polarity */
LL_UCPD_WriteReg(config->ucpd_port, CR, cr);
return 0;
}
/**
* @brief Enable or Disable Power Delivery
*
* @retval 0 on success
* @retval -EIO on failure
*/
static int ucpd_set_rx_enable(const struct device *dev, bool enable)
{
const struct tcpc_config *const config = dev->config;
uint32_t imr;
uint32_t cr;
imr = LL_UCPD_ReadReg(config->ucpd_port, IMR);
cr = LL_UCPD_ReadReg(config->ucpd_port, CR);
/*
* USB PD receiver enable is controlled by the bit PHYRXEN in
* UCPD_CR. Enable Rx interrupts when RX PD decoder is active.
*/
if (enable) {
/* Clear the RX alerts bits */
LL_UCPD_WriteReg(config->ucpd_port, ICR, UCPD_ICR_RX_INT_MASK);
imr |= UCPD_IMR_RX_INT_MASK;
cr |= UCPD_CR_PHYRXEN;
LL_UCPD_WriteReg(config->ucpd_port, IMR, imr);
LL_UCPD_WriteReg(config->ucpd_port, CR, cr);
} else {
imr &= ~UCPD_IMR_RX_INT_MASK;
cr &= ~UCPD_CR_PHYRXEN;
LL_UCPD_WriteReg(config->ucpd_port, CR, cr);
LL_UCPD_WriteReg(config->ucpd_port, IMR, imr);
}
return 0;
}
/**
* @brief Set the Power and Data role used when sending goodCRC messages
*
* @retval 0 on success
* @retval -EIO on failure
*/
static int ucpd_set_roles(const struct device *dev,
enum tc_power_role power_role,
enum tc_data_role data_role)
{
struct tcpc_data *data = dev->data;
data->msg_header.pr = power_role;
data->msg_header.dr = data_role;
return 0;
}
/**
* @brief Enable the reception of SOP Prime messages
*
* @retval 0 on success
* @retval -EIO on failure
*/
static int ucpd_sop_prime_enable(const struct device *dev, bool enable)
{
struct tcpc_data *data = dev->data;
/* Update static variable used to filter SOP//SOP'' messages */
data->ucpd_rx_sop_prime_enabled = enable;
return 0;
}
/**
* @brief State transmitting a message
*/
static void ucpd_start_transmit(const struct device *dev,
enum ucpd_tx_msg msg_type)
{
struct tcpc_data *data = dev->data;
const struct tcpc_config *const config = dev->config;
enum pd_packet_type type;
uint32_t cr;
uint32_t imr;
cr = LL_UCPD_ReadReg(config->ucpd_port, CR);
/* Select the correct tx descriptor */
data->ucpd_tx_active_buffer = &data->ucpd_tx_buffers[msg_type];
type = data->ucpd_tx_active_buffer->type;
if (type == PD_PACKET_TX_HARD_RESET) {
/*
* From RM0440 45.4.4:
* In order to facilitate generation of a Hard Reset, a special
* code of TXMODE field is used. No other fields need to be
* written. On writing the correct code, the hardware forces
* Hard Reset Tx under the correct (optimal) timings with
* respect to an on-going Tx message, which (if still in
* progress) is cleanly terminated by truncating the current
* sequence and directly appending an EOP K-code sequence. No
* specific interrupt is generated relating to this truncation
* event.
*
* Because Hard Reset can interrupt ongoing Tx operations, it is
* started differently than all other tx messages. Only need to
* enable hard reset interrupts, and then set a bit in the CR
* register to initiate.
*/
/* Enable interrupt for Hard Reset sent/discarded */
LL_UCPD_WriteReg(config->ucpd_port, ICR,
UCPD_ICR_HRSTDISCCF | UCPD_ICR_HRSTSENTCF);
imr = LL_UCPD_ReadReg(config->ucpd_port, IMR);
imr |= UCPD_IMR_HRSTDISCIE | UCPD_IMR_HRSTSENTIE;
LL_UCPD_WriteReg(config->ucpd_port, IMR, imr);
/* Initiate Hard Reset */
cr |= UCPD_CR_TXHRST;
LL_UCPD_WriteReg(config->ucpd_port, CR, cr);
} else if (type != PD_PACKET_MSG_INVALID) {
int msg_len = 0;
int mode;
/*
* These types are normal transmission, TXMODE = 0. To transmit
* regular message, control or data, requires the following:
* 1. Set TXMODE:
* Normal -> 0
* Cable Reset -> 1
* Bist -> 2
* 2. Set TX_ORDSETR based on message type
* 3. Set TX_PAYSZR which must account for 2 bytes of header
* 4. Configure DMA (optional if DMA is desired)
* 5. Enable transmit interrupts
* 6. Start TX by setting TXSEND in CR
*
*/
/*
* Set tx length parameter (in bytes). Note the count field in
* the header is number of 32 bit objects. Also, the length
* field must account for the 2 header bytes.
*/
if (type == PD_PACKET_TX_BIST_MODE_2) {
mode = LL_UCPD_TXMODE_BIST_CARRIER2;
} else if (type == PD_PACKET_CABLE_RESET) {
mode = LL_UCPD_TXMODE_CABLE_RESET;
} else {
mode = LL_UCPD_TXMODE_NORMAL;
msg_len = data->ucpd_tx_active_buffer->msg_len;
}
LL_UCPD_WriteTxPaySize(config->ucpd_port, msg_len);
/* Set tx mode */
cr &= ~UCPD_CR_TXMODE_Msk;
cr |= mode;
LL_UCPD_WriteReg(config->ucpd_port, CR, cr);
/* Index into ordset enum for start of packet */
if (type <= PD_PACKET_CABLE_RESET) {
LL_UCPD_WriteTxOrderSet(config->ucpd_port,
ucpd_txorderset[type]);
}
/* Reset msg byte index */
data->ucpd_tx_active_buffer->msg_index = 0;
/* Enable interrupts */
ucpd_tx_interrupts_enable(dev, 1);
/* Trigger ucpd peripheral to start pd message transmit */
LL_UCPD_SendMessage(config->ucpd_port);
}
}
/**
* @brief Set the current state of the TX state machine
*/
static void ucpd_set_tx_state(const struct device *dev, enum ucpd_state state)
{
struct tcpc_data *data = dev->data;
data->ucpd_tx_state = state;
}
/**
* @brief Wrapper function for calling alert handler
*/
static void ucpd_notify_handler(struct alert_info *info, enum tcpc_alert alert)
{
if (info->handler) {
info->handler(info->dev, info->data, alert);
}
}
/**
* @brief This is the TX state machine
*/
static void ucpd_manage_tx(struct alert_info *info)
{
struct tcpc_data *data = info->dev->data;
enum ucpd_tx_msg msg_src = TX_MSG_NONE;
union pd_header hdr;
if (atomic_test_and_clear_bit(&info->evt, UCPD_EVT_HR_REQ)) {
/*
* Hard reset control messages are treated as a priority. The
* control message will already be set up as it comes from the
* PRL layer like any other PD ctrl/data message. So just need
* to indicate the correct message source and set the state to
* hard reset here.
*/
ucpd_set_tx_state(info->dev, STATE_HARD_RESET);
msg_src = TX_MSG_TCPM;
data->ucpd_tx_request &= ~BIT(msg_src);
}
switch (data->ucpd_tx_state) {
case STATE_IDLE:
if (data->ucpd_tx_request & MSG_GOOD_CRC_MASK) {
ucpd_set_tx_state(info->dev, STATE_ACTIVE_CRC);
msg_src = TX_MSG_GOOD_CRC;
} else if (data->ucpd_tx_request & MSG_TCPM_MASK) {
if (atomic_test_and_clear_bit(&info->evt, UCPD_EVT_RX_MSG)) {
/*
* USB-PD Specification rev 3.0, section 6.10
* On receiving a received message, the protocol
* layer shall discard any pending message.
*
* Since the pending message from the PRL has
* not been sent yet, it needs to be discarded
* based on the received message event.
*/
ucpd_notify_handler(info, TCPC_ALERT_TRANSMIT_MSG_DISCARDED);
data->ucpd_tx_request &= ~MSG_TCPM_MASK;
} else if (!data->ucpd_rx_msg_active) {
ucpd_set_tx_state(info->dev, STATE_ACTIVE_TCPM);
msg_src = TX_MSG_TCPM;
/* Save msgID required for GoodCRC check */
hdr.raw_value =
data->ucpd_tx_buffers[TX_MSG_TCPM].data.header;
data->msg_id_match = hdr.message_id;
data->tx_retry_max = hdr.specification_revision == PD_REV30 ?
UCPD_N_RETRY_COUNT_REV30 :
UCPD_N_RETRY_COUNT_REV20;
}
}
/* If state is not idle, then start tx message */
if (data->ucpd_tx_state != STATE_IDLE) {
data->ucpd_tx_request &= ~BIT(msg_src);
data->tx_retry_count = 0;
}
break;
case STATE_ACTIVE_TCPM:
/*
* Check if tx msg has finished. For TCPM messages
* transmit is not complete until a GoodCRC message
* matching the msgID just sent is received. But, a tx
* message can fail due to collision or underrun,
* etc. If that failure occurs, dont' wait for GoodCrc
* and just go to failure path.
*/
if (atomic_test_and_clear_bit(&info->evt, UCPD_EVT_TX_MSG_SUCCESS)) {
ucpd_set_tx_state(info->dev, STATE_WAIT_CRC_ACK);
/* Start the GoodCRC RX Timer */
k_timer_start(&data->goodcrc_rx_timer, K_USEC(1000), K_NO_WAIT);
} else if (atomic_test_and_clear_bit(&info->evt, UCPD_EVT_TX_MSG_DISC) ||
atomic_test_and_clear_bit(&info->evt, UCPD_EVT_TX_MSG_FAIL)) {
if (data->tx_retry_count < data->tx_retry_max) {
if (atomic_test_and_clear_bit(&info->evt, UCPD_EVT_RX_MSG)) {
/*
* A message was received so there is no
* need to retry this tx message which
* had failed to send previously.
* Likely, due to the wire
* being active from the message that
* was just received.
*/
ucpd_set_tx_state(info->dev,
STATE_IDLE);
ucpd_notify_handler(info,
TCPC_ALERT_TRANSMIT_MSG_DISCARDED);
ucpd_set_tx_state(info->dev,
STATE_IDLE);
} else {
/*
* Tx attempt failed. Remain in this
* state, but trigger new tx attempt.
*/
msg_src = TX_MSG_TCPM;
data->tx_retry_count++;
}
} else {
enum tcpc_alert status;
status = (atomic_test_and_clear_bit(&info->evt,
UCPD_EVT_TX_MSG_FAIL)) ?
TCPC_ALERT_TRANSMIT_MSG_FAILED :
TCPC_ALERT_TRANSMIT_MSG_DISCARDED;
ucpd_set_tx_state(info->dev, STATE_IDLE);
ucpd_notify_handler(info, status);
}
}
break;
case STATE_ACTIVE_CRC:
if (atomic_test_bit(&info->evt, UCPD_EVT_TX_MSG_SUCCESS) ||
atomic_test_bit(&info->evt, UCPD_EVT_TX_MSG_FAIL) ||
atomic_test_bit(&info->evt, UCPD_EVT_TX_MSG_DISC)) {
atomic_clear_bit(&info->evt, UCPD_EVT_TX_MSG_SUCCESS);
atomic_clear_bit(&info->evt, UCPD_EVT_TX_MSG_FAIL);
atomic_clear_bit(&info->evt, UCPD_EVT_TX_MSG_DISC);
ucpd_set_tx_state(info->dev, STATE_IDLE);
if (atomic_test_and_clear_bit(&info->evt, UCPD_EVT_TX_MSG_FAIL)) {
LOG_INF("ucpd: Failed to send GoodCRC!");
} else if (atomic_test_and_clear_bit(&info->evt, UCPD_EVT_TX_MSG_DISC)) {
LOG_INF("ucpd: GoodCRC message discarded!");
}
}
break;
case STATE_WAIT_CRC_ACK:
if (atomic_test_and_clear_bit(&info->evt, UCPD_EVT_RX_GOOD_CRC) &&
data->ucpd_crc_id == data->msg_id_match) {
/* GoodCRC with matching ID was received */
ucpd_notify_handler(info, TCPC_ALERT_TRANSMIT_MSG_SUCCESS);
ucpd_set_tx_state(info->dev, STATE_IDLE);
} else if (k_timer_status_get(&data->goodcrc_rx_timer)) {
/* Stop the GoodCRC RX Timer */
k_timer_stop(&data->goodcrc_rx_timer);
/* GoodCRC w/out match or timeout waiting */
if (data->tx_retry_count < data->tx_retry_max) {
ucpd_set_tx_state(info->dev, STATE_ACTIVE_TCPM);
msg_src = TX_MSG_TCPM;
data->tx_retry_count++;
} else {
ucpd_set_tx_state(info->dev, STATE_IDLE);
ucpd_notify_handler(info, TCPC_ALERT_TRANSMIT_MSG_FAILED);
}
} else if (atomic_test_and_clear_bit(&info->evt, UCPD_EVT_RX_MSG)) {
/*
* In the case of a collision, it's possible the port
* partner may not send a GoodCRC and instead send the
* message that was colliding. If a message is received
* in this state, then treat it as a discard from an
* incoming message.
*/
ucpd_notify_handler(info, TCPC_ALERT_TRANSMIT_MSG_DISCARDED);
ucpd_set_tx_state(info->dev, STATE_IDLE);
}
break;
case STATE_HARD_RESET:
if (atomic_test_bit(&info->evt, UCPD_EVT_HR_DONE) ||
atomic_test_bit(&info->evt, UCPD_EVT_HR_FAIL)) {
atomic_clear_bit(&info->evt, UCPD_EVT_HR_DONE);
atomic_clear_bit(&info->evt, UCPD_EVT_HR_FAIL);
/* HR complete, reset tx state values */
ucpd_set_tx_state(info->dev, STATE_IDLE);
data->ucpd_tx_request = 0;
data->tx_retry_count = 0;
}
break;
}
/*
* NOTE: TX_MSG_GOOD_CRC messages are sent from the ISR to reduce latency
* when sending those messages, so don't resend them here.
*
* If msg_src is valid and not a TX_MSG_GOOD_CRC, then start transmit.
*/
if (msg_src != TX_MSG_GOOD_CRC && msg_src > TX_MSG_NONE) {
ucpd_start_transmit(info->dev, msg_src);
}
}
/**
* @brief Alert handler
*/
static void ucpd_alert_handler(struct k_work *item)
{
struct alert_info *info = CONTAINER_OF(item, struct alert_info, work);
struct tcpc_data *data = info->dev->data;
if (atomic_test_and_clear_bit(&info->evt, UCPD_EVT_EVENT_CC)) {
ucpd_notify_handler(info, TCPC_ALERT_CC_STATUS);
}
if (atomic_test_and_clear_bit(&info->evt, UCPD_EVT_HARD_RESET_RECEIVED)) {
ucpd_notify_handler(info, TCPC_ALERT_HARD_RESET_RECEIVED);
}
if (atomic_test_and_clear_bit(&info->evt, UCPD_EVT_RX_MSG)) {
ucpd_notify_handler(info, TCPC_ALERT_MSG_STATUS);
}
/*
* USB-PD messages are initiated in TCPM stack (PRL
* layer). However, GoodCRC messages are initiated within the
* UCPD driver based on USB-PD rx messages. These 2 types of
* transmit paths are managed via events.
*
* UCPD generated GoodCRC messages, are the priority path as
* they must be sent immediately following a successful USB-PD
* rx message. As long as a transmit operation is not underway,
* then a transmit message will be started upon request. The ISR
* routine sets the event to indicate that the transmit
* operation is complete.
*
* Hard reset requests are sent as a TCPM message, but in terms
* of the ucpd transmitter, they are treated as a 3rd tx msg
* source since they can interrupt an ongoing tx msg, and there
* is no requirement to wait for a GoodCRC reply message.
*/
if (atomic_test_and_clear_bit(&info->evt, UCPD_EVT_GOOD_CRC_REQ)) {
data->ucpd_tx_request |= MSG_GOOD_CRC_MASK;
}
if (atomic_test_and_clear_bit(&info->evt, UCPD_EVT_TCPM_MSG_REQ)) {
data->ucpd_tx_request |= MSG_TCPM_MASK;
}
/*
* Manage PD tx messages. The state machine may need to be
* called more than once. For instance, if
* the task is woken at the completion of sending a GoodCRC,
* there may be a TCPM message request pending and just changing
* the state back to idle would not trigger start of transmit.
*/
do {
ucpd_manage_tx(info);
} while (data->ucpd_tx_state != STATE_IDLE);
}
/**
* @brief Sends a goodCRC message
*/
static void ucpd_send_good_crc(const struct device *dev,
union pd_header rx_header)
{
struct tcpc_data *data = dev->data;
const struct tcpc_config *const config = dev->config;
union pd_header tx_header;
enum pd_packet_type tx_type;
struct alert_info *info = &data->alert_info;
/*
* A GoodCRC message shall be sent by receiver to ack that the previous
* message was correctly received. The GoodCRC message shall return the
* rx message's msg_id field. The one exception is for GoodCRC messages,
* which do not generate a GoodCRC response
*/
if (ucpd_msg_is_good_crc(rx_header)) {
return;
}
/*
* Get the rx ordered set code just detected. SOP -> SOP''_Debug are in
* the same order as enum tcpc_packet_type and so can be used
* directly.
*/
tx_type = LL_UCPD_ReadRxOrderSet(config->ucpd_port);
/*
* PD Header(SOP):
* Extended b15 -> set to 0 for control messages
* Count b14:12 -> number of 32 bit data objects = 0 for ctrl msg
* MsgID b11:9 -> running byte counter (extracted from rx msg)
* Power Role b8 -> stored in static, from set_msg_header()
* Spec Rev b7:b6 -> PD spec revision (extracted from rx msg)
* Data Role b5 -> stored in static, from set_msg_header
* Msg Type b4:b0 -> data or ctrl type = PD_CTRL_GOOD_CRC
*/
/* construct header message */
tx_header.message_type = PD_CTRL_GOOD_CRC;
if (tx_type == PD_PACKET_SOP) {
tx_header.port_power_role = data->msg_header.pr;
tx_header.port_data_role = data->msg_header.dr;
} else {
tx_header.port_power_role = 0;
tx_header.port_data_role = 0;
}
tx_header.message_id = rx_header.message_id;
tx_header.number_of_data_objects = 0;
tx_header.specification_revision = rx_header.specification_revision;
tx_header.extended = 0;
/* Good CRC is header with no other objects */
data->ucpd_tx_buffers[TX_MSG_GOOD_CRC].msg_len = MSG_HEADER_SIZE;
data->ucpd_tx_buffers[TX_MSG_GOOD_CRC].data.header =
tx_header.raw_value;
data->ucpd_tx_buffers[TX_MSG_GOOD_CRC].type = tx_type;
/* Notify ucpd task that a GoodCRC message tx request is pending */
atomic_set_bit(&info->evt, UCPD_EVT_GOOD_CRC_REQ);
/* Send TX_MSG_GOOD_CRC message here to reduce latency */
ucpd_start_transmit(dev, TX_MSG_GOOD_CRC);
}
/**
* @brief Transmit a power delivery message
*
* @retval 0 on success
* @retval -EFAULT on failure
*/
static int ucpd_transmit_data(const struct device *dev,
struct pd_msg *msg)
{
struct tcpc_data *data = dev->data;
/* Length in bytes = (4 * object len) + 2 header bytes */
int len = PD_CONVERT_PD_HEADER_COUNT_TO_BYTES(msg->header.number_of_data_objects) + 2;
if (len > UCPD_BUF_LEN) {
return -EFAULT;
}