14f5f5887SNishad Kamdar /* SPDX-License-Identifier: GPL-2.0 */ 2bc5a0b55SAlexandre TORGUE /* 3bc5a0b55SAlexandre TORGUE * Copyright (C) Maxime Coquelin 2015 43e5fcbacSBich HEMON * Copyright (C) STMicroelectronics SA 2017 5bc5a0b55SAlexandre TORGUE * Authors: Maxime Coquelin <mcoquelin.stm32@gmail.com> 6bc5a0b55SAlexandre TORGUE * Gerald Baeza <gerald_baeza@yahoo.fr> 7bc5a0b55SAlexandre TORGUE */ 8bc5a0b55SAlexandre TORGUE 9bc5a0b55SAlexandre TORGUE #define DRIVER_NAME "stm32-usart" 10bc5a0b55SAlexandre TORGUE 11bc5a0b55SAlexandre TORGUE struct stm32_usart_offsets { 12bc5a0b55SAlexandre TORGUE u8 cr1; 13bc5a0b55SAlexandre TORGUE u8 cr2; 14bc5a0b55SAlexandre TORGUE u8 cr3; 15bc5a0b55SAlexandre TORGUE u8 brr; 16bc5a0b55SAlexandre TORGUE u8 gtpr; 17bc5a0b55SAlexandre TORGUE u8 rtor; 18bc5a0b55SAlexandre TORGUE u8 rqr; 19bc5a0b55SAlexandre TORGUE u8 isr; 20bc5a0b55SAlexandre TORGUE u8 icr; 21bc5a0b55SAlexandre TORGUE u8 rdr; 22bc5a0b55SAlexandre TORGUE u8 tdr; 23bc5a0b55SAlexandre TORGUE }; 24bc5a0b55SAlexandre TORGUE 25bc5a0b55SAlexandre TORGUE struct stm32_usart_config { 26bc5a0b55SAlexandre TORGUE u8 uart_enable_bit; /* USART_CR1_UE */ 27bc5a0b55SAlexandre TORGUE bool has_7bits_data; 283cd66593SMartin Devera bool has_swap; 29270e5a74SFabrice Gasnier bool has_wakeup; 30351a762aSGerald Baeza bool has_fifo; 31d075719eSErwan Le Ray int fifosize; 32bc5a0b55SAlexandre TORGUE }; 33bc5a0b55SAlexandre TORGUE 34bc5a0b55SAlexandre TORGUE struct stm32_usart_info { 35bc5a0b55SAlexandre TORGUE struct stm32_usart_offsets ofs; 36bc5a0b55SAlexandre TORGUE struct stm32_usart_config cfg; 37bc5a0b55SAlexandre TORGUE }; 38bc5a0b55SAlexandre TORGUE 39b20fb13cSGeert Uytterhoeven #define UNDEF_REG 0xff 40bc5a0b55SAlexandre TORGUE 41bc5a0b55SAlexandre TORGUE /* USART_SR (F4) / USART_ISR (F7) */ 42bc5a0b55SAlexandre TORGUE #define USART_SR_PE BIT(0) 43bc5a0b55SAlexandre TORGUE #define USART_SR_FE BIT(1) 4433bb2f6aSErwan Le Ray #define USART_SR_NE BIT(2) /* F7 (NF for F4) */ 45bc5a0b55SAlexandre TORGUE #define USART_SR_ORE BIT(3) 46bc5a0b55SAlexandre TORGUE #define USART_SR_IDLE BIT(4) 47bc5a0b55SAlexandre TORGUE #define USART_SR_RXNE BIT(5) 48bc5a0b55SAlexandre TORGUE #define USART_SR_TC BIT(6) 49bc5a0b55SAlexandre TORGUE #define USART_SR_TXE BIT(7) 50bc5a0b55SAlexandre TORGUE #define USART_SR_CTSIF BIT(9) 51bc5a0b55SAlexandre TORGUE #define USART_SR_CTS BIT(10) /* F7 */ 52bc5a0b55SAlexandre TORGUE #define USART_SR_RTOF BIT(11) /* F7 */ 53bc5a0b55SAlexandre TORGUE #define USART_SR_EOBF BIT(12) /* F7 */ 54bc5a0b55SAlexandre TORGUE #define USART_SR_ABRE BIT(14) /* F7 */ 55bc5a0b55SAlexandre TORGUE #define USART_SR_ABRF BIT(15) /* F7 */ 56bc5a0b55SAlexandre TORGUE #define USART_SR_BUSY BIT(16) /* F7 */ 57bc5a0b55SAlexandre TORGUE #define USART_SR_CMF BIT(17) /* F7 */ 58bc5a0b55SAlexandre TORGUE #define USART_SR_SBKF BIT(18) /* F7 */ 59270e5a74SFabrice Gasnier #define USART_SR_WUF BIT(20) /* H7 */ 60bc5a0b55SAlexandre TORGUE #define USART_SR_TEACK BIT(21) /* F7 */ 6133bb2f6aSErwan Le Ray #define USART_SR_ERR_MASK (USART_SR_ORE | USART_SR_NE | USART_SR_FE |\ 6233bb2f6aSErwan Le Ray USART_SR_PE) 63bc5a0b55SAlexandre TORGUE /* Dummy bits */ 64bc5a0b55SAlexandre TORGUE #define USART_SR_DUMMY_RX BIT(16) 65bc5a0b55SAlexandre TORGUE 66bc5a0b55SAlexandre TORGUE /* USART_DR */ 67bc5a0b55SAlexandre TORGUE #define USART_DR_MASK GENMASK(8, 0) 68bc5a0b55SAlexandre TORGUE 69bc5a0b55SAlexandre TORGUE /* USART_BRR */ 70bc5a0b55SAlexandre TORGUE #define USART_BRR_DIV_F_MASK GENMASK(3, 0) 71bc5a0b55SAlexandre TORGUE #define USART_BRR_DIV_M_MASK GENMASK(15, 4) 72bc5a0b55SAlexandre TORGUE #define USART_BRR_DIV_M_SHIFT 4 731bcda09dSBich HEMON #define USART_BRR_04_R_SHIFT 1 74bc5a0b55SAlexandre TORGUE 75bc5a0b55SAlexandre TORGUE /* USART_CR1 */ 76bc5a0b55SAlexandre TORGUE #define USART_CR1_SBK BIT(0) 77bc5a0b55SAlexandre TORGUE #define USART_CR1_RWU BIT(1) /* F4 */ 78270e5a74SFabrice Gasnier #define USART_CR1_UESM BIT(1) /* H7 */ 79bc5a0b55SAlexandre TORGUE #define USART_CR1_RE BIT(2) 80bc5a0b55SAlexandre TORGUE #define USART_CR1_TE BIT(3) 81bc5a0b55SAlexandre TORGUE #define USART_CR1_IDLEIE BIT(4) 82bc5a0b55SAlexandre TORGUE #define USART_CR1_RXNEIE BIT(5) 83bc5a0b55SAlexandre TORGUE #define USART_CR1_TCIE BIT(6) 84bc5a0b55SAlexandre TORGUE #define USART_CR1_TXEIE BIT(7) 85bc5a0b55SAlexandre TORGUE #define USART_CR1_PEIE BIT(8) 86bc5a0b55SAlexandre TORGUE #define USART_CR1_PS BIT(9) 87bc5a0b55SAlexandre TORGUE #define USART_CR1_PCE BIT(10) 88bc5a0b55SAlexandre TORGUE #define USART_CR1_WAKE BIT(11) 89c8a9d043SErwan Le Ray #define USART_CR1_M0 BIT(12) /* F7 (CR1_M for F4) */ 90bc5a0b55SAlexandre TORGUE #define USART_CR1_MME BIT(13) /* F7 */ 91bc5a0b55SAlexandre TORGUE #define USART_CR1_CMIE BIT(14) /* F7 */ 92bc5a0b55SAlexandre TORGUE #define USART_CR1_OVER8 BIT(15) 93bc5a0b55SAlexandre TORGUE #define USART_CR1_DEDT_MASK GENMASK(20, 16) /* F7 */ 94bc5a0b55SAlexandre TORGUE #define USART_CR1_DEAT_MASK GENMASK(25, 21) /* F7 */ 95bc5a0b55SAlexandre TORGUE #define USART_CR1_RTOIE BIT(26) /* F7 */ 96bc5a0b55SAlexandre TORGUE #define USART_CR1_EOBIE BIT(27) /* F7 */ 97bc5a0b55SAlexandre TORGUE #define USART_CR1_M1 BIT(28) /* F7 */ 98bc5a0b55SAlexandre TORGUE #define USART_CR1_IE_MASK (GENMASK(8, 4) | BIT(14) | BIT(26) | BIT(27)) 99351a762aSGerald Baeza #define USART_CR1_FIFOEN BIT(29) /* H7 */ 1001bcda09dSBich HEMON #define USART_CR1_DEAT_SHIFT 21 1011bcda09dSBich HEMON #define USART_CR1_DEDT_SHIFT 16 102bc5a0b55SAlexandre TORGUE 103bc5a0b55SAlexandre TORGUE /* USART_CR2 */ 104bc5a0b55SAlexandre TORGUE #define USART_CR2_ADD_MASK GENMASK(3, 0) /* F4 */ 105bc5a0b55SAlexandre TORGUE #define USART_CR2_ADDM7 BIT(4) /* F7 */ 106bc5a0b55SAlexandre TORGUE #define USART_CR2_LBCL BIT(8) 107bc5a0b55SAlexandre TORGUE #define USART_CR2_CPHA BIT(9) 108bc5a0b55SAlexandre TORGUE #define USART_CR2_CPOL BIT(10) 109bc5a0b55SAlexandre TORGUE #define USART_CR2_CLKEN BIT(11) 110bc5a0b55SAlexandre TORGUE #define USART_CR2_STOP_2B BIT(13) 111bc5a0b55SAlexandre TORGUE #define USART_CR2_STOP_MASK GENMASK(13, 12) 112bc5a0b55SAlexandre TORGUE #define USART_CR2_LINEN BIT(14) 113bc5a0b55SAlexandre TORGUE #define USART_CR2_SWAP BIT(15) /* F7 */ 114bc5a0b55SAlexandre TORGUE #define USART_CR2_RXINV BIT(16) /* F7 */ 115bc5a0b55SAlexandre TORGUE #define USART_CR2_TXINV BIT(17) /* F7 */ 116bc5a0b55SAlexandre TORGUE #define USART_CR2_DATAINV BIT(18) /* F7 */ 117bc5a0b55SAlexandre TORGUE #define USART_CR2_MSBFIRST BIT(19) /* F7 */ 118bc5a0b55SAlexandre TORGUE #define USART_CR2_ABREN BIT(20) /* F7 */ 119bc5a0b55SAlexandre TORGUE #define USART_CR2_ABRMOD_MASK GENMASK(22, 21) /* F7 */ 120bc5a0b55SAlexandre TORGUE #define USART_CR2_RTOEN BIT(23) /* F7 */ 121bc5a0b55SAlexandre TORGUE #define USART_CR2_ADD_F7_MASK GENMASK(31, 24) /* F7 */ 122bc5a0b55SAlexandre TORGUE 123bc5a0b55SAlexandre TORGUE /* USART_CR3 */ 124bc5a0b55SAlexandre TORGUE #define USART_CR3_EIE BIT(0) 125bc5a0b55SAlexandre TORGUE #define USART_CR3_IREN BIT(1) 126bc5a0b55SAlexandre TORGUE #define USART_CR3_IRLP BIT(2) 127bc5a0b55SAlexandre TORGUE #define USART_CR3_HDSEL BIT(3) 128bc5a0b55SAlexandre TORGUE #define USART_CR3_NACK BIT(4) 129bc5a0b55SAlexandre TORGUE #define USART_CR3_SCEN BIT(5) 130bc5a0b55SAlexandre TORGUE #define USART_CR3_DMAR BIT(6) 131bc5a0b55SAlexandre TORGUE #define USART_CR3_DMAT BIT(7) 132bc5a0b55SAlexandre TORGUE #define USART_CR3_RTSE BIT(8) 133bc5a0b55SAlexandre TORGUE #define USART_CR3_CTSE BIT(9) 134bc5a0b55SAlexandre TORGUE #define USART_CR3_CTSIE BIT(10) 135bc5a0b55SAlexandre TORGUE #define USART_CR3_ONEBIT BIT(11) 136bc5a0b55SAlexandre TORGUE #define USART_CR3_OVRDIS BIT(12) /* F7 */ 137bc5a0b55SAlexandre TORGUE #define USART_CR3_DDRE BIT(13) /* F7 */ 138bc5a0b55SAlexandre TORGUE #define USART_CR3_DEM BIT(14) /* F7 */ 139bc5a0b55SAlexandre TORGUE #define USART_CR3_DEP BIT(15) /* F7 */ 140bc5a0b55SAlexandre TORGUE #define USART_CR3_SCARCNT_MASK GENMASK(19, 17) /* F7 */ 141270e5a74SFabrice Gasnier #define USART_CR3_WUS_MASK GENMASK(21, 20) /* H7 */ 142270e5a74SFabrice Gasnier #define USART_CR3_WUS_START_BIT BIT(21) /* H7 */ 143270e5a74SFabrice Gasnier #define USART_CR3_WUFIE BIT(22) /* H7 */ 144d075719eSErwan Le Ray #define USART_CR3_TXFTIE BIT(23) /* H7 */ 145d075719eSErwan Le Ray #define USART_CR3_TCBGTIE BIT(24) /* H7 */ 146d0a6a7bcSErwan Le Ray #define USART_CR3_RXFTCFG_MASK GENMASK(27, 25) /* H7 */ 147d0a6a7bcSErwan Le Ray #define USART_CR3_RXFTCFG_SHIFT 25 /* H7 */ 148d075719eSErwan Le Ray #define USART_CR3_RXFTIE BIT(28) /* H7 */ 149d075719eSErwan Le Ray #define USART_CR3_TXFTCFG_MASK GENMASK(31, 29) /* H7 */ 150d075719eSErwan Le Ray #define USART_CR3_TXFTCFG_SHIFT 29 /* H7 */ 151d075719eSErwan Le Ray 152bc5a0b55SAlexandre TORGUE /* USART_GTPR */ 153bc5a0b55SAlexandre TORGUE #define USART_GTPR_PSC_MASK GENMASK(7, 0) 154bc5a0b55SAlexandre TORGUE #define USART_GTPR_GT_MASK GENMASK(15, 8) 155bc5a0b55SAlexandre TORGUE 156bc5a0b55SAlexandre TORGUE /* USART_RTOR */ 157bc5a0b55SAlexandre TORGUE #define USART_RTOR_RTO_MASK GENMASK(23, 0) /* F7 */ 158bc5a0b55SAlexandre TORGUE #define USART_RTOR_BLEN_MASK GENMASK(31, 24) /* F7 */ 159bc5a0b55SAlexandre TORGUE 160bc5a0b55SAlexandre TORGUE /* USART_RQR */ 161bc5a0b55SAlexandre TORGUE #define USART_RQR_ABRRQ BIT(0) /* F7 */ 162bc5a0b55SAlexandre TORGUE #define USART_RQR_SBKRQ BIT(1) /* F7 */ 163bc5a0b55SAlexandre TORGUE #define USART_RQR_MMRQ BIT(2) /* F7 */ 164bc5a0b55SAlexandre TORGUE #define USART_RQR_RXFRQ BIT(3) /* F7 */ 165bc5a0b55SAlexandre TORGUE #define USART_RQR_TXFRQ BIT(4) /* F7 */ 166bc5a0b55SAlexandre TORGUE 167bc5a0b55SAlexandre TORGUE /* USART_ICR */ 168bc5a0b55SAlexandre TORGUE #define USART_ICR_PECF BIT(0) /* F7 */ 1694f01d833SErwan Le Ray #define USART_ICR_FECF BIT(1) /* F7 */ 170bc5a0b55SAlexandre TORGUE #define USART_ICR_ORECF BIT(3) /* F7 */ 171bc5a0b55SAlexandre TORGUE #define USART_ICR_IDLECF BIT(4) /* F7 */ 172bc5a0b55SAlexandre TORGUE #define USART_ICR_TCCF BIT(6) /* F7 */ 173bc5a0b55SAlexandre TORGUE #define USART_ICR_CTSCF BIT(9) /* F7 */ 174bc5a0b55SAlexandre TORGUE #define USART_ICR_RTOCF BIT(11) /* F7 */ 175bc5a0b55SAlexandre TORGUE #define USART_ICR_EOBCF BIT(12) /* F7 */ 176bc5a0b55SAlexandre TORGUE #define USART_ICR_CMCF BIT(17) /* F7 */ 177270e5a74SFabrice Gasnier #define USART_ICR_WUCF BIT(20) /* H7 */ 178bc5a0b55SAlexandre TORGUE 1790858fe3cSLudovic Barre #define STM32_SERIAL_NAME "ttySTM" 180cc7aefd4SGerald Baeza #define STM32_MAX_PORTS 8 181bc5a0b55SAlexandre TORGUE 18233bb2f6aSErwan Le Ray #define RX_BUF_L 4096 /* dma rx buffer length */ 18333bb2f6aSErwan Le Ray #define RX_BUF_P (RX_BUF_L / 2) /* dma rx buffer period */ 18433bb2f6aSErwan Le Ray #define TX_BUF_L RX_BUF_L /* dma tx buffer length */ 18534891872SAlexandre TORGUE 18628fb1a92SValentin Caron #define STM32_USART_TIMEOUT_USEC USEC_PER_SEC /* 1s timeout in µs */ 18728fb1a92SValentin Caron 188bc5a0b55SAlexandre TORGUE struct stm32_port { 189bc5a0b55SAlexandre TORGUE struct uart_port port; 190bc5a0b55SAlexandre TORGUE struct clk *clk; 191d825f0beSStephen Boyd const struct stm32_usart_info *info; 19234891872SAlexandre TORGUE struct dma_chan *rx_ch; /* dma rx channel */ 19334891872SAlexandre TORGUE dma_addr_t rx_dma_buf; /* dma rx buffer bus address */ 19434891872SAlexandre TORGUE unsigned char *rx_buf; /* dma rx buffer cpu address */ 19534891872SAlexandre TORGUE struct dma_chan *tx_ch; /* dma tx channel */ 19634891872SAlexandre TORGUE dma_addr_t tx_dma_buf; /* dma tx buffer bus address */ 19734891872SAlexandre TORGUE unsigned char *tx_buf; /* dma tx buffer cpu address */ 1984cc0ed62SErwan Le Ray u32 cr1_irq; /* USART_CR1_RXNEIE or RTOIE */ 199d0a6a7bcSErwan Le Ray u32 cr3_irq; /* USART_CR3_RXFTIE */ 200e5707915SGerald Baeza int last_res; 2019a135f16SValentin Caron bool tx_dma_busy; /* dma tx transaction in progress */ 202*7f28bceaSValentin Caron bool rx_dma_busy; /* dma rx transaction in progress */ 203d1ec8a2eSErwan Le Ray bool throttled; /* port throttled */ 204bc5a0b55SAlexandre TORGUE bool hw_flow_control; 2053cd66593SMartin Devera bool swap; /* swap RX & TX pins */ 206351a762aSGerald Baeza bool fifoen; 2072aa1bbb2SFabrice Gasnier int rxftcfg; /* RX FIFO threshold CFG */ 2082aa1bbb2SFabrice Gasnier int txftcfg; /* TX FIFO threshold CFG */ 2093d530017SAlexandre Torgue bool wakeup_src; 2106c5962f3SErwan Le Ray int rdr_mask; /* receive data register mask */ 2116cf61b9bSManivannan Sadhasivam struct mctrl_gpios *gpios; /* modem control gpios */ 21233bb2f6aSErwan Le Ray struct dma_tx_state rx_dma_state; 213bc5a0b55SAlexandre TORGUE }; 214bc5a0b55SAlexandre TORGUE 215bc5a0b55SAlexandre TORGUE static struct stm32_port stm32_ports[STM32_MAX_PORTS]; 216bc5a0b55SAlexandre TORGUE static struct uart_driver stm32_usart_driver; 217