1 // SPDX-License-Identifier: BSD-2-Clause 2 /* 3 * Copyright (c) 2017-2018, STMicroelectronics 4 */ 5 6 #include <compiler.h> 7 #include <console.h> 8 #include <drivers/clk.h> 9 #include <drivers/clk_dt.h> 10 #include <drivers/serial.h> 11 #include <drivers/stm32_gpio.h> 12 #include <drivers/stm32_uart.h> 13 #include <io.h> 14 #include <keep.h> 15 #include <kernel/delay.h> 16 #include <kernel/dt.h> 17 #include <kernel/panic.h> 18 #include <libfdt.h> 19 #include <stm32_util.h> 20 #include <util.h> 21 22 #define UART_REG_CR1 0x00 /* Control register 1 */ 23 #define UART_REG_CR2 0x04 /* Control register 2 */ 24 #define UART_REG_CR3 0x08 /* Control register 3 */ 25 #define UART_REG_BRR 0x0c /* Baud rate register */ 26 #define UART_REG_RQR 0x18 /* Request register */ 27 #define UART_REG_ISR 0x1c /* Interrupt & status reg. */ 28 #define UART_REG_ICR 0x20 /* Interrupt flag clear reg. */ 29 #define UART_REG_RDR 0x24 /* Receive data register */ 30 #define UART_REG_TDR 0x28 /* Transmit data register */ 31 #define UART_REG_PRESC 0x2c /* Prescaler register */ 32 33 #define PUTC_TIMEOUT_US 1000 34 #define FLUSH_TIMEOUT_US 16000 35 36 /* 37 * Uart Interrupt & status register bits 38 * 39 * Bit 5 RXNE: Read data register not empty/RXFIFO not empty 40 * Bit 6 TC: Transmission complete 41 * Bit 7 TXE/TXFNF: Transmit data register empty/TXFIFO not full 42 * Bit 23 TXFE: TXFIFO empty 43 */ 44 #define USART_ISR_RXNE_RXFNE BIT(5) 45 #define USART_ISR_TC BIT(6) 46 #define USART_ISR_TXE_TXFNF BIT(7) 47 #define USART_ISR_TXFE BIT(23) 48 49 static vaddr_t loc_chip_to_base(struct serial_chip *chip) 50 { 51 struct stm32_uart_pdata *pd = NULL; 52 53 pd = container_of(chip, struct stm32_uart_pdata, chip); 54 55 return io_pa_or_va(&pd->base, 1); 56 } 57 58 static void loc_flush(struct serial_chip *chip) 59 { 60 vaddr_t base = loc_chip_to_base(chip); 61 uint64_t timeout = timeout_init_us(FLUSH_TIMEOUT_US); 62 63 while (!(io_read32(base + UART_REG_ISR) & USART_ISR_TXFE)) 64 if (timeout_elapsed(timeout)) 65 return; 66 } 67 68 static void loc_putc(struct serial_chip *chip, int ch) 69 { 70 vaddr_t base = loc_chip_to_base(chip); 71 uint64_t timeout = timeout_init_us(PUTC_TIMEOUT_US); 72 73 while (!(io_read32(base + UART_REG_ISR) & USART_ISR_TXE_TXFNF)) 74 if (timeout_elapsed(timeout)) 75 return; 76 77 io_write32(base + UART_REG_TDR, ch); 78 } 79 80 static bool loc_have_rx_data(struct serial_chip *chip) 81 { 82 vaddr_t base = loc_chip_to_base(chip); 83 84 return io_read32(base + UART_REG_ISR) & USART_ISR_RXNE_RXFNE; 85 } 86 87 static int loc_getchar(struct serial_chip *chip) 88 { 89 vaddr_t base = loc_chip_to_base(chip); 90 91 while (!loc_have_rx_data(chip)) 92 ; 93 94 return io_read32(base + UART_REG_RDR) & 0xff; 95 } 96 97 static const struct serial_ops stm32_uart_serial_ops = { 98 .flush = loc_flush, 99 .putc = loc_putc, 100 .have_rx_data = loc_have_rx_data, 101 .getchar = loc_getchar, 102 103 }; 104 DECLARE_KEEP_PAGER(stm32_uart_serial_ops); 105 106 void stm32_uart_init(struct stm32_uart_pdata *pd, vaddr_t base) 107 { 108 pd->base.pa = base; 109 pd->chip.ops = &stm32_uart_serial_ops; 110 } 111 112 static void register_secure_uart(struct stm32_uart_pdata *pd __maybe_unused) 113 { 114 #ifndef CFG_STM32MP25 115 stm32mp_register_secure_periph_iomem(pd->base.pa); 116 #endif 117 } 118 119 static void register_non_secure_uart(struct stm32_uart_pdata *pd __maybe_unused) 120 { 121 #ifndef CFG_STM32MP25 122 stm32mp_register_non_secure_periph_iomem(pd->base.pa); 123 #endif 124 125 } 126 127 struct stm32_uart_pdata *stm32_uart_init_from_dt_node(void *fdt, int node) 128 { 129 TEE_Result res = TEE_ERROR_GENERIC; 130 struct stm32_uart_pdata *pd = NULL; 131 struct dt_node_info info = { }; 132 133 fdt_fill_device_info(fdt, &info, node); 134 135 if (info.status == DT_STATUS_DISABLED) 136 return NULL; 137 138 assert(info.reg != DT_INFO_INVALID_REG && 139 info.reg_size != DT_INFO_INVALID_REG_SIZE); 140 141 pd = calloc(1, sizeof(*pd)); 142 if (!pd) 143 panic(); 144 145 pd->chip.ops = &stm32_uart_serial_ops; 146 pd->base.pa = info.reg; 147 pd->secure = (info.status == DT_STATUS_OK_SEC); 148 149 res = clk_dt_get_by_index(fdt, node, 0, &pd->clock); 150 if (res) { 151 EMSG("Failed to get clock: %#"PRIx32, res); 152 panic(); 153 } 154 155 res = clk_enable(pd->clock); 156 if (res) 157 panic(); 158 159 assert(cpu_mmu_enabled()); 160 pd->base.va = (vaddr_t)phys_to_virt(pd->base.pa, 161 pd->secure ? MEM_AREA_IO_SEC : 162 MEM_AREA_IO_NSEC, info.reg_size); 163 164 res = pinctrl_get_state_by_name(fdt, node, "default", &pd->pinctrl); 165 if (res) 166 panic(); 167 168 res = pinctrl_get_state_by_name(fdt, node, "sleep", &pd->pinctrl_sleep); 169 if (res && res != TEE_ERROR_ITEM_NOT_FOUND) 170 panic(); 171 172 res = pinctrl_apply_state(pd->pinctrl); 173 if (res) 174 panic(); 175 176 if (pd->secure) 177 register_secure_uart(pd); 178 else 179 register_non_secure_uart(pd); 180 181 return pd; 182 } 183