21
21
#include <soc.h>
22
22
#include <fsl_device_registers.h>
23
23
#include <zephyr/drivers/pinctrl.h>
24
+ #include <zephyr/drivers/clock_management.h>
24
25
#ifdef CONFIG_UART_ASYNC_API
25
26
#include <zephyr/drivers/dma.h>
26
27
#include <fsl_inputmux.h>
@@ -37,14 +38,19 @@ struct mcux_flexcomm_uart_dma_config {
37
38
38
39
struct mcux_flexcomm_config {
39
40
USART_Type * base ;
40
- const struct device * clock_dev ;
41
- clock_control_subsys_t clock_subsys ;
42
41
uint32_t baud_rate ;
43
42
uint8_t parity ;
44
43
#ifdef CONFIG_UART_MCUX_FLEXCOMM_ISR_SUPPORT
45
44
void (* irq_config_func )(const struct device * dev );
46
45
#endif
47
46
const struct pinctrl_dev_config * pincfg ;
47
+ #ifdef CONFIG_CLOCK_MANAGEMENT
48
+ const struct clock_output * clock_output ;
49
+ clock_management_state_t clock_state ;
50
+ #else
51
+ const struct device * clock_dev ;
52
+ clock_control_subsys_t clock_subsys ;
53
+ #endif
48
54
#ifdef CONFIG_UART_ASYNC_API
49
55
struct mcux_flexcomm_uart_dma_config tx_dma ;
50
56
struct mcux_flexcomm_uart_dma_config rx_dma ;
@@ -395,8 +401,12 @@ static int mcux_flexcomm_uart_configure(const struct device *dev, const struct u
395
401
USART_Deinit (config -> base );
396
402
397
403
/* Get UART clock frequency */
404
+ #ifdef CONFIG_CLOCK_MANAGEMENT
405
+ clock_freq = clock_management_get_rate (config -> clock_output );
406
+ #else
398
407
clock_control_get_rate (config -> clock_dev ,
399
408
config -> clock_subsys , & clock_freq );
409
+ #endif
400
410
401
411
/* Handle 9 bit mode */
402
412
USART_Enable9bitMode (config -> base , nine_bit_mode );
@@ -1051,15 +1061,53 @@ static void mcux_flexcomm_isr(const struct device *dev)
1051
1061
}
1052
1062
#endif /* CONFIG_UART_MCUX_FLEXCOMM_ISR_SUPPORT */
1053
1063
1064
+ static void mcux_flexcomm_uart_setup (const struct device * dev , uint32_t clock_rate )
1065
+ {
1066
+ const struct mcux_flexcomm_config * config = dev -> config ;
1067
+ usart_config_t usart_config ;
1068
+ usart_parity_mode_t parity_mode ;
1069
+
1070
+ if (config -> parity == UART_CFG_PARITY_ODD ) {
1071
+ parity_mode = kUSART_ParityOdd ;
1072
+ } else if (config -> parity == UART_CFG_PARITY_EVEN ) {
1073
+ parity_mode = kUSART_ParityEven ;
1074
+ } else {
1075
+ parity_mode = kUSART_ParityDisabled ;
1076
+ }
1077
+
1078
+ USART_GetDefaultConfig (& usart_config );
1079
+ usart_config .enableTx = true;
1080
+ usart_config .enableRx = true;
1081
+ usart_config .parityMode = parity_mode ;
1082
+ usart_config .baudRate_Bps = config -> baud_rate ;
1083
+
1084
+ USART_Init (config -> base , & usart_config , clock_rate );
1085
+ }
1086
+
1087
+ #ifdef CONFIG_CLOCK_MANAGEMENT_RUNTIME
1088
+ int uart_mcux_flexcomm_clock_cb (const struct clock_management_event * ev , const void * data )
1089
+ {
1090
+ const struct device * uart_dev = data ;
1091
+ const struct mcux_flexcomm_config * config = uart_dev -> config ;
1092
+
1093
+ if (ev -> type == CLOCK_MANAGEMENT_PRE_RATE_CHANGE ) {
1094
+ /* Deinit USART */
1095
+ USART_Deinit (config -> base );
1096
+ } else if (ev -> type == CLOCK_MANAGEMENT_POST_RATE_CHANGE ) {
1097
+ /* Reconfigure USART */
1098
+ mcux_flexcomm_uart_setup (uart_dev , ev -> new_rate );
1099
+ }
1100
+ return 0 ;
1101
+ }
1102
+ #endif
1103
+
1054
1104
static int mcux_flexcomm_init_common (const struct device * dev )
1055
1105
{
1056
1106
const struct mcux_flexcomm_config * config = dev -> config ;
1057
1107
#ifdef CONFIG_UART_USE_RUNTIME_CONFIGURE
1058
1108
struct mcux_flexcomm_data * data = dev -> data ;
1059
1109
struct uart_config * cfg = & data -> uart_config ;
1060
1110
#endif /* CONFIG_UART_USE_RUNTIME_CONFIGURE */
1061
- usart_config_t usart_config ;
1062
- usart_parity_mode_t parity_mode ;
1063
1111
uint32_t clock_freq ;
1064
1112
int err ;
1065
1113
@@ -1068,6 +1116,14 @@ static int mcux_flexcomm_init_common(const struct device *dev)
1068
1116
return err ;
1069
1117
}
1070
1118
1119
+ #ifdef CONFIG_CLOCK_MANAGEMENT
1120
+ clock_freq = clock_management_apply_state (config -> clock_output ,
1121
+ config -> clock_state );
1122
+ #ifdef CONFIG_CLOCK_MANAGEMENT_RUNTIME
1123
+ clock_management_set_callback (config -> clock_output ,
1124
+ uart_mcux_flexcomm_clock_cb , dev );
1125
+ #endif
1126
+ #else
1071
1127
if (!device_is_ready (config -> clock_dev )) {
1072
1128
return - ENODEV ;
1073
1129
}
@@ -1077,20 +1133,8 @@ static int mcux_flexcomm_init_common(const struct device *dev)
1077
1133
& clock_freq )) {
1078
1134
return - EINVAL ;
1079
1135
}
1080
-
1081
- if (config -> parity == UART_CFG_PARITY_ODD ) {
1082
- parity_mode = kUSART_ParityOdd ;
1083
- } else if (config -> parity == UART_CFG_PARITY_EVEN ) {
1084
- parity_mode = kUSART_ParityEven ;
1085
- } else {
1086
- parity_mode = kUSART_ParityDisabled ;
1087
- }
1088
-
1089
- USART_GetDefaultConfig (& usart_config );
1090
- usart_config .enableTx = true;
1091
- usart_config .enableRx = true;
1092
- usart_config .parityMode = parity_mode ;
1093
- usart_config .baudRate_Bps = config -> baud_rate ;
1136
+ #endif
1137
+ mcux_flexcomm_uart_setup (dev , clock_freq );
1094
1138
1095
1139
#ifdef CONFIG_UART_USE_RUNTIME_CONFIGURE
1096
1140
cfg -> baudrate = config -> baud_rate ;
@@ -1101,8 +1145,6 @@ static int mcux_flexcomm_init_common(const struct device *dev)
1101
1145
cfg -> flow_ctrl = UART_CFG_FLOW_CTRL_NONE ;
1102
1146
#endif /* CONFIG_UART_USE_RUNTIME_CONFIGURE */
1103
1147
1104
- USART_Init (config -> base , & usart_config , clock_freq );
1105
-
1106
1148
#ifdef CONFIG_UART_MCUX_FLEXCOMM_ISR_SUPPORT
1107
1149
config -> irq_config_func (dev );
1108
1150
#endif
@@ -1266,20 +1308,31 @@ DT_INST_FOREACH_STATUS_OKAY(UART_MCUX_FLEXCOMM_RX_TIMEOUT_FUNC);
1266
1308
#define UART_MCUX_FLEXCOMM_ASYNC_CFG (n )
1267
1309
#endif /* CONFIG_UART_ASYNC_API */
1268
1310
1311
+ #ifdef CONFIG_CLOCK_MANAGEMENT
1312
+ #define UART_MCUX_FLEXCOMM_CLK_DEFINE (n ) CLOCK_MANAGEMENT_DT_INST_DEFINE_OUTPUT(n)
1313
+ #define UART_MCUX_FLEXCOMM_CLK_INIT (n ) \
1314
+ .clock_output = CLOCK_MANAGEMENT_DT_INST_GET_OUTPUT(n), \
1315
+ .clock_state = CLOCK_MANAGEMENT_DT_INST_GET_STATE(n, default, default),
1316
+ #else
1317
+ #define UART_MCUX_FLEXCOMM_CLK_DEFINE (n )
1318
+ #define UART_MCUX_FLEXCOMM_CLK_INIT (n ) \
1319
+ .clock_dev = DEVICE_DT_GET(DT_INST_CLOCKS_CTLR(n)), \
1320
+ .clock_subsys = (clock_control_subsys_t)DT_INST_CLOCKS_CELL(n, name),
1321
+ #endif
1322
+
1269
1323
#define UART_MCUX_FLEXCOMM_INIT_CFG (n ) \
1270
1324
static const struct mcux_flexcomm_config mcux_flexcomm_##n##_config = { \
1271
1325
.base = (USART_Type *)DT_INST_REG_ADDR(n), \
1272
- .clock_dev = DEVICE_DT_GET(DT_INST_CLOCKS_CTLR(n)), \
1273
- .clock_subsys = \
1274
- (clock_control_subsys_t)DT_INST_CLOCKS_CELL(n, name), \
1275
1326
.baud_rate = DT_INST_PROP(n, current_speed), \
1276
1327
.parity = DT_INST_ENUM_IDX(n, parity), \
1277
1328
.pincfg = PINCTRL_DT_INST_DEV_CONFIG_GET(n), \
1329
+ UART_MCUX_FLEXCOMM_CLK_INIT(n) \
1278
1330
UART_MCUX_FLEXCOMM_IRQ_CFG_FUNC_INIT(n) \
1279
1331
UART_MCUX_FLEXCOMM_ASYNC_CFG(n) \
1280
1332
};
1281
1333
1282
1334
#define UART_MCUX_FLEXCOMM_INIT (n ) \
1335
+ UART_MCUX_FLEXCOMM_CLK_DEFINE(n); \
1283
1336
\
1284
1337
PINCTRL_DT_INST_DEFINE(n); \
1285
1338
\
0 commit comments