Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Sascha Hauer | 9b56f4f | 2008-03-26 20:40:42 +0100 | [diff] [blame] | 2 | /* |
| 3 | * (c) 2007 Sascha Hauer <s.hauer@pengutronix.de> |
Sascha Hauer | 9b56f4f | 2008-03-26 20:40:42 +0100 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #include <common.h> |
Simon Glass | a8ba569 | 2014-10-01 19:57:27 -0600 | [diff] [blame] | 7 | #include <dm.h> |
| 8 | #include <errno.h> |
Stefano Babic | 4ec3d2a | 2010-08-18 10:22:42 +0200 | [diff] [blame] | 9 | #include <watchdog.h> |
Ilya Yanok | 47d19da | 2009-06-08 04:12:46 +0400 | [diff] [blame] | 10 | #include <asm/arch/imx-regs.h> |
| 11 | #include <asm/arch/clock.h> |
Simon Glass | 401d1c4 | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 12 | #include <asm/global_data.h> |
Masahiro Yamada | 86256b7 | 2014-10-24 12:41:19 +0900 | [diff] [blame] | 13 | #include <dm/platform_data/serial_mxc.h> |
Marek Vasut | a943472 | 2012-09-14 22:37:43 +0200 | [diff] [blame] | 14 | #include <serial.h> |
| 15 | #include <linux/compiler.h> |
Loic Poulain | 7150f56 | 2023-01-12 18:19:50 +0100 | [diff] [blame] | 16 | #include <linux/delay.h> |
Sascha Hauer | 9b56f4f | 2008-03-26 20:40:42 +0100 | [diff] [blame] | 17 | |
Sascha Hauer | 9b56f4f | 2008-03-26 20:40:42 +0100 | [diff] [blame] | 18 | /* UART Control Register Bit Fields.*/ |
Jagan Teki | 52c14ca | 2017-06-06 05:31:50 +0000 | [diff] [blame] | 19 | #define URXD_CHARRDY (1<<15) |
| 20 | #define URXD_ERR (1<<14) |
| 21 | #define URXD_OVRRUN (1<<13) |
| 22 | #define URXD_FRMERR (1<<12) |
| 23 | #define URXD_BRK (1<<11) |
| 24 | #define URXD_PRERR (1<<10) |
| 25 | #define URXD_RX_DATA (0xFF) |
| 26 | #define UCR1_ADEN (1<<15) /* Auto dectect interrupt */ |
| 27 | #define UCR1_ADBR (1<<14) /* Auto detect baud rate */ |
| 28 | #define UCR1_TRDYEN (1<<13) /* Transmitter ready interrupt enable */ |
| 29 | #define UCR1_IDEN (1<<12) /* Idle condition interrupt */ |
| 30 | #define UCR1_RRDYEN (1<<9) /* Recv ready interrupt enable */ |
| 31 | #define UCR1_RDMAEN (1<<8) /* Recv ready DMA enable */ |
| 32 | #define UCR1_IREN (1<<7) /* Infrared interface enable */ |
| 33 | #define UCR1_TXMPTYEN (1<<6) /* Transimitter empty interrupt enable */ |
| 34 | #define UCR1_RTSDEN (1<<5) /* RTS delta interrupt enable */ |
| 35 | #define UCR1_SNDBRK (1<<4) /* Send break */ |
| 36 | #define UCR1_TDMAEN (1<<3) /* Transmitter ready DMA enable */ |
| 37 | #define UCR1_UARTCLKEN (1<<2) /* UART clock enabled */ |
| 38 | #define UCR1_DOZE (1<<1) /* Doze */ |
| 39 | #define UCR1_UARTEN (1<<0) /* UART enabled */ |
| 40 | #define UCR2_ESCI (1<<15) /* Escape seq interrupt enable */ |
| 41 | #define UCR2_IRTS (1<<14) /* Ignore RTS pin */ |
| 42 | #define UCR2_CTSC (1<<13) /* CTS pin control */ |
| 43 | #define UCR2_CTS (1<<12) /* Clear to send */ |
| 44 | #define UCR2_ESCEN (1<<11) /* Escape enable */ |
| 45 | #define UCR2_PREN (1<<8) /* Parity enable */ |
| 46 | #define UCR2_PROE (1<<7) /* Parity odd/even */ |
| 47 | #define UCR2_STPB (1<<6) /* Stop */ |
| 48 | #define UCR2_WS (1<<5) /* Word size */ |
| 49 | #define UCR2_RTSEN (1<<4) /* Request to send interrupt enable */ |
| 50 | #define UCR2_TXEN (1<<2) /* Transmitter enabled */ |
| 51 | #define UCR2_RXEN (1<<1) /* Receiver enabled */ |
| 52 | #define UCR2_SRST (1<<0) /* SW reset */ |
| 53 | #define UCR3_DTREN (1<<13) /* DTR interrupt enable */ |
| 54 | #define UCR3_PARERREN (1<<12) /* Parity enable */ |
| 55 | #define UCR3_FRAERREN (1<<11) /* Frame error interrupt enable */ |
| 56 | #define UCR3_DSR (1<<10) /* Data set ready */ |
| 57 | #define UCR3_DCD (1<<9) /* Data carrier detect */ |
| 58 | #define UCR3_RI (1<<8) /* Ring indicator */ |
| 59 | #define UCR3_ADNIMP (1<<7) /* Autobaud Detection Not Improved */ |
| 60 | #define UCR3_RXDSEN (1<<6) /* Receive status interrupt enable */ |
| 61 | #define UCR3_AIRINTEN (1<<5) /* Async IR wake interrupt enable */ |
| 62 | #define UCR3_AWAKEN (1<<4) /* Async wake interrupt enable */ |
| 63 | #define UCR3_REF25 (1<<3) /* Ref freq 25 MHz */ |
| 64 | #define UCR3_REF30 (1<<2) /* Ref Freq 30 MHz */ |
Johannes Schneider | 25b7ce3 | 2022-09-06 14:15:03 +0200 | [diff] [blame] | 65 | |
| 66 | /* imx8 names these bitsfields instead: */ |
| 67 | #define UCR3_DTRDEN BIT(3) /* bit not used in this chip */ |
| 68 | #define UCR3_RXDMUXSEL BIT(2) /* RXD muxed input selected; 'should always be set' */ |
| 69 | |
Jagan Teki | 52c14ca | 2017-06-06 05:31:50 +0000 | [diff] [blame] | 70 | #define UCR3_INVT (1<<1) /* Inverted Infrared transmission */ |
| 71 | #define UCR3_BPEN (1<<0) /* Preset registers enable */ |
| 72 | #define UCR4_CTSTL_32 (32<<10) /* CTS trigger level (32 chars) */ |
| 73 | #define UCR4_INVR (1<<9) /* Inverted infrared reception */ |
| 74 | #define UCR4_ENIRI (1<<8) /* Serial infrared interrupt enable */ |
| 75 | #define UCR4_WKEN (1<<7) /* Wake interrupt enable */ |
| 76 | #define UCR4_REF16 (1<<6) /* Ref freq 16 MHz */ |
| 77 | #define UCR4_IRSC (1<<5) /* IR special case */ |
| 78 | #define UCR4_TCEN (1<<3) /* Transmit complete interrupt enable */ |
| 79 | #define UCR4_BKEN (1<<2) /* Break condition interrupt enable */ |
| 80 | #define UCR4_OREN (1<<1) /* Receiver overrun interrupt enable */ |
| 81 | #define UCR4_DREN (1<<0) /* Recv data ready interrupt enable */ |
| 82 | #define UFCR_RXTL_SHF 0 /* Receiver trigger level shift */ |
| 83 | #define UFCR_RFDIV (7<<7) /* Reference freq divider mask */ |
| 84 | #define UFCR_RFDIV_SHF 7 /* Reference freq divider shift */ |
| 85 | #define RFDIV 4 /* divide input clock by 2 */ |
| 86 | #define UFCR_DCEDTE (1<<6) /* DTE mode select */ |
| 87 | #define UFCR_TXTL_SHF 10 /* Transmitter trigger level shift */ |
| 88 | #define USR1_PARITYERR (1<<15) /* Parity error interrupt flag */ |
| 89 | #define USR1_RTSS (1<<14) /* RTS pin status */ |
| 90 | #define USR1_TRDY (1<<13) /* Transmitter ready interrupt/dma flag */ |
| 91 | #define USR1_RTSD (1<<12) /* RTS delta */ |
| 92 | #define USR1_ESCF (1<<11) /* Escape seq interrupt flag */ |
| 93 | #define USR1_FRAMERR (1<<10) /* Frame error interrupt flag */ |
| 94 | #define USR1_RRDY (1<<9) /* Receiver ready interrupt/dma flag */ |
| 95 | #define USR1_TIMEOUT (1<<7) /* Receive timeout interrupt status */ |
| 96 | #define USR1_RXDS (1<<6) /* Receiver idle interrupt flag */ |
| 97 | #define USR1_AIRINT (1<<5) /* Async IR wake interrupt flag */ |
| 98 | #define USR1_AWAKE (1<<4) /* Aysnc wake interrupt flag */ |
| 99 | #define USR2_ADET (1<<15) /* Auto baud rate detect complete */ |
| 100 | #define USR2_TXFE (1<<14) /* Transmit buffer FIFO empty */ |
| 101 | #define USR2_DTRF (1<<13) /* DTR edge interrupt flag */ |
| 102 | #define USR2_IDLE (1<<12) /* Idle condition */ |
| 103 | #define USR2_IRINT (1<<8) /* Serial infrared interrupt flag */ |
| 104 | #define USR2_WAKE (1<<7) /* Wake */ |
| 105 | #define USR2_RTSF (1<<4) /* RTS edge interrupt flag */ |
| 106 | #define USR2_TXDC (1<<3) /* Transmitter complete */ |
| 107 | #define USR2_BRCD (1<<2) /* Break condition */ |
| 108 | #define USR2_ORE (1<<1) /* Overrun error */ |
| 109 | #define USR2_RDR (1<<0) /* Recv data ready */ |
| 110 | #define UTS_FRCPERR (1<<13) /* Force parity error */ |
| 111 | #define UTS_LOOP (1<<12) /* Loop tx and rx */ |
| 112 | #define UTS_TXEMPTY (1<<6) /* TxFIFO empty */ |
| 113 | #define UTS_RXEMPTY (1<<5) /* RxFIFO empty */ |
| 114 | #define UTS_TXFULL (1<<4) /* TxFIFO full */ |
| 115 | #define UTS_RXFULL (1<<3) /* RxFIFO full */ |
| 116 | #define UTS_SOFTRS (1<<0) /* Software reset */ |
Jagan Teki | 45d9751 | 2017-06-06 05:31:49 +0000 | [diff] [blame] | 117 | #define TXTL 2 /* reset default */ |
| 118 | #define RXTL 1 /* reset default */ |
Sascha Hauer | 9b56f4f | 2008-03-26 20:40:42 +0100 | [diff] [blame] | 119 | |
Stefan Agner | a99546a | 2016-10-05 15:27:03 -0700 | [diff] [blame] | 120 | DECLARE_GLOBAL_DATA_PTR; |
| 121 | |
Jagan Teki | ffa8bcd | 2017-06-06 05:31:45 +0000 | [diff] [blame] | 122 | struct mxc_uart { |
| 123 | u32 rxd; |
| 124 | u32 spare0[15]; |
| 125 | |
| 126 | u32 txd; |
| 127 | u32 spare1[15]; |
| 128 | |
| 129 | u32 cr1; |
| 130 | u32 cr2; |
| 131 | u32 cr3; |
| 132 | u32 cr4; |
| 133 | |
| 134 | u32 fcr; |
| 135 | u32 sr1; |
| 136 | u32 sr2; |
| 137 | u32 esc; |
| 138 | |
| 139 | u32 tim; |
| 140 | u32 bir; |
| 141 | u32 bmr; |
| 142 | u32 brc; |
| 143 | |
| 144 | u32 onems; |
| 145 | u32 ts; |
| 146 | }; |
| 147 | |
Loic Poulain | 7150f56 | 2023-01-12 18:19:50 +0100 | [diff] [blame] | 148 | static void _mxc_serial_flush(struct mxc_uart *base) |
| 149 | { |
| 150 | unsigned int timeout = 4000; |
| 151 | |
| 152 | if (!(readl(&base->cr1) & UCR1_UARTEN) || |
| 153 | !(readl(&base->cr2) & UCR2_TXEN)) |
| 154 | return; |
| 155 | |
| 156 | while (!(readl(&base->sr2) & USR2_TXDC) && --timeout) |
| 157 | udelay(1); |
| 158 | } |
| 159 | |
Max Krummenacher | a245320 | 2019-02-01 16:04:50 +0100 | [diff] [blame] | 160 | static void _mxc_serial_init(struct mxc_uart *base, int use_dte) |
Jagan Teki | 97548d5 | 2017-06-06 05:31:48 +0000 | [diff] [blame] | 161 | { |
Loic Poulain | 7150f56 | 2023-01-12 18:19:50 +0100 | [diff] [blame] | 162 | _mxc_serial_flush(base); |
| 163 | |
Jagan Teki | 97548d5 | 2017-06-06 05:31:48 +0000 | [diff] [blame] | 164 | writel(0, &base->cr1); |
| 165 | writel(0, &base->cr2); |
| 166 | |
| 167 | while (!(readl(&base->cr2) & UCR2_SRST)); |
| 168 | |
Max Krummenacher | a245320 | 2019-02-01 16:04:50 +0100 | [diff] [blame] | 169 | if (use_dte) |
| 170 | writel(0x404 | UCR3_ADNIMP, &base->cr3); |
| 171 | else |
| 172 | writel(0x704 | UCR3_ADNIMP, &base->cr3); |
| 173 | |
Jagan Teki | 97548d5 | 2017-06-06 05:31:48 +0000 | [diff] [blame] | 174 | writel(0x704 | UCR3_ADNIMP, &base->cr3); |
| 175 | writel(0x8000, &base->cr4); |
| 176 | writel(0x2b, &base->esc); |
| 177 | writel(0, &base->tim); |
| 178 | |
| 179 | writel(0, &base->ts); |
| 180 | } |
| 181 | |
Jagan Teki | 45d9751 | 2017-06-06 05:31:49 +0000 | [diff] [blame] | 182 | static void _mxc_serial_setbrg(struct mxc_uart *base, unsigned long clk, |
| 183 | unsigned long baudrate, bool use_dte) |
| 184 | { |
| 185 | u32 tmp; |
| 186 | |
Loic Poulain | 7150f56 | 2023-01-12 18:19:50 +0100 | [diff] [blame] | 187 | _mxc_serial_flush(base); |
| 188 | |
Jagan Teki | 45d9751 | 2017-06-06 05:31:49 +0000 | [diff] [blame] | 189 | tmp = RFDIV << UFCR_RFDIV_SHF; |
| 190 | if (use_dte) |
| 191 | tmp |= UFCR_DCEDTE; |
| 192 | else |
| 193 | tmp |= (TXTL << UFCR_TXTL_SHF) | (RXTL << UFCR_RXTL_SHF); |
| 194 | writel(tmp, &base->fcr); |
| 195 | |
| 196 | writel(0xf, &base->bir); |
| 197 | writel(clk / (2 * baudrate), &base->bmr); |
| 198 | |
| 199 | writel(UCR2_WS | UCR2_IRTS | UCR2_RXEN | UCR2_TXEN | UCR2_SRST, |
| 200 | &base->cr2); |
Johannes Schneider | 25b7ce3 | 2022-09-06 14:15:03 +0200 | [diff] [blame] | 201 | |
| 202 | /* |
| 203 | * setting the baudrate triggers a reset, returning cr3 to its |
| 204 | * reset value but UCR3_RXDMUXSEL "should always be set." |
| 205 | * according to the imx8 reference-manual |
| 206 | */ |
| 207 | writel(readl(&base->cr3) | UCR3_RXDMUXSEL, &base->cr3); |
| 208 | |
Jagan Teki | 45d9751 | 2017-06-06 05:31:49 +0000 | [diff] [blame] | 209 | writel(UCR1_UARTEN, &base->cr1); |
| 210 | } |
| 211 | |
Adam Ford | e434b41 | 2019-02-19 22:07:22 -0600 | [diff] [blame] | 212 | #if !CONFIG_IS_ENABLED(DM_SERIAL) |
Simon Glass | a8ba569 | 2014-10-01 19:57:27 -0600 | [diff] [blame] | 213 | |
Tom Rini | 4db3866 | 2022-12-04 10:04:55 -0500 | [diff] [blame] | 214 | #ifndef CFG_MXC_UART_BASE |
| 215 | #error "define CFG_MXC_UART_BASE to use the MXC UART driver" |
Simon Glass | a8ba569 | 2014-10-01 19:57:27 -0600 | [diff] [blame] | 216 | #endif |
| 217 | |
Tom Rini | 4db3866 | 2022-12-04 10:04:55 -0500 | [diff] [blame] | 218 | #define mxc_base ((struct mxc_uart *)CFG_MXC_UART_BASE) |
Simon Glass | a8ba569 | 2014-10-01 19:57:27 -0600 | [diff] [blame] | 219 | |
Marek Vasut | a943472 | 2012-09-14 22:37:43 +0200 | [diff] [blame] | 220 | static void mxc_serial_setbrg(void) |
Sascha Hauer | 9b56f4f | 2008-03-26 20:40:42 +0100 | [diff] [blame] | 221 | { |
Stefano Babic | 71d64c0 | 2010-01-20 18:20:19 +0100 | [diff] [blame] | 222 | u32 clk = imx_get_uartclk(); |
Sascha Hauer | 9b56f4f | 2008-03-26 20:40:42 +0100 | [diff] [blame] | 223 | |
| 224 | if (!gd->baudrate) |
| 225 | gd->baudrate = CONFIG_BAUDRATE; |
| 226 | |
Jagan Teki | 45d9751 | 2017-06-06 05:31:49 +0000 | [diff] [blame] | 227 | _mxc_serial_setbrg(mxc_base, clk, gd->baudrate, false); |
Sascha Hauer | 9b56f4f | 2008-03-26 20:40:42 +0100 | [diff] [blame] | 228 | } |
| 229 | |
Marek Vasut | a943472 | 2012-09-14 22:37:43 +0200 | [diff] [blame] | 230 | static int mxc_serial_getc(void) |
Sascha Hauer | 9b56f4f | 2008-03-26 20:40:42 +0100 | [diff] [blame] | 231 | { |
Jagan Teki | ffa8bcd | 2017-06-06 05:31:45 +0000 | [diff] [blame] | 232 | while (readl(&mxc_base->ts) & UTS_RXEMPTY) |
Stefan Roese | 29caf93 | 2022-09-02 14:10:46 +0200 | [diff] [blame] | 233 | schedule(); |
Jagan Teki | ffa8bcd | 2017-06-06 05:31:45 +0000 | [diff] [blame] | 234 | return (readl(&mxc_base->rxd) & URXD_RX_DATA); /* mask out status from upper word */ |
Sascha Hauer | 9b56f4f | 2008-03-26 20:40:42 +0100 | [diff] [blame] | 235 | } |
| 236 | |
Marek Vasut | a943472 | 2012-09-14 22:37:43 +0200 | [diff] [blame] | 237 | static void mxc_serial_putc(const char c) |
Sascha Hauer | 9b56f4f | 2008-03-26 20:40:42 +0100 | [diff] [blame] | 238 | { |
Alison Wang | 055457e | 2016-03-02 11:00:37 +0800 | [diff] [blame] | 239 | /* If \n, also do \r */ |
| 240 | if (c == '\n') |
| 241 | serial_putc('\r'); |
| 242 | |
Sascha Hauer | 9b56f4f | 2008-03-26 20:40:42 +0100 | [diff] [blame] | 243 | /* wait for transmitter to be ready */ |
Loic Poulain | ad72507 | 2023-01-12 18:19:51 +0100 | [diff] [blame] | 244 | while (readl(&mxc_base->ts) & UTS_TXFULL) |
Stefan Roese | 29caf93 | 2022-09-02 14:10:46 +0200 | [diff] [blame] | 245 | schedule(); |
Loic Poulain | ad72507 | 2023-01-12 18:19:51 +0100 | [diff] [blame] | 246 | |
| 247 | writel(c, &mxc_base->txd); |
Sascha Hauer | 9b56f4f | 2008-03-26 20:40:42 +0100 | [diff] [blame] | 248 | } |
| 249 | |
Jagan Teki | 52c14ca | 2017-06-06 05:31:50 +0000 | [diff] [blame] | 250 | /* Test whether a character is in the RX buffer */ |
Marek Vasut | a943472 | 2012-09-14 22:37:43 +0200 | [diff] [blame] | 251 | static int mxc_serial_tstc(void) |
Sascha Hauer | 9b56f4f | 2008-03-26 20:40:42 +0100 | [diff] [blame] | 252 | { |
| 253 | /* If receive fifo is empty, return false */ |
Jagan Teki | ffa8bcd | 2017-06-06 05:31:45 +0000 | [diff] [blame] | 254 | if (readl(&mxc_base->ts) & UTS_RXEMPTY) |
Sascha Hauer | 9b56f4f | 2008-03-26 20:40:42 +0100 | [diff] [blame] | 255 | return 0; |
| 256 | return 1; |
| 257 | } |
| 258 | |
Sascha Hauer | 9b56f4f | 2008-03-26 20:40:42 +0100 | [diff] [blame] | 259 | /* |
| 260 | * Initialise the serial port with the given baudrate. The settings |
| 261 | * are always 8 data bits, no parity, 1 stop bit, no start bits. |
Sascha Hauer | 9b56f4f | 2008-03-26 20:40:42 +0100 | [diff] [blame] | 262 | */ |
Marek Vasut | a943472 | 2012-09-14 22:37:43 +0200 | [diff] [blame] | 263 | static int mxc_serial_init(void) |
Sascha Hauer | 9b56f4f | 2008-03-26 20:40:42 +0100 | [diff] [blame] | 264 | { |
Max Krummenacher | a245320 | 2019-02-01 16:04:50 +0100 | [diff] [blame] | 265 | _mxc_serial_init(mxc_base, false); |
Sascha Hauer | 9b56f4f | 2008-03-26 20:40:42 +0100 | [diff] [blame] | 266 | |
| 267 | serial_setbrg(); |
| 268 | |
Sascha Hauer | 9b56f4f | 2008-03-26 20:40:42 +0100 | [diff] [blame] | 269 | return 0; |
| 270 | } |
Marek Vasut | a943472 | 2012-09-14 22:37:43 +0200 | [diff] [blame] | 271 | |
Loic Poulain | 7150f56 | 2023-01-12 18:19:50 +0100 | [diff] [blame] | 272 | static int mxc_serial_stop(void) |
| 273 | { |
| 274 | _mxc_serial_flush(mxc_base); |
| 275 | |
| 276 | return 0; |
| 277 | } |
| 278 | |
Marek Vasut | a943472 | 2012-09-14 22:37:43 +0200 | [diff] [blame] | 279 | static struct serial_device mxc_serial_drv = { |
| 280 | .name = "mxc_serial", |
| 281 | .start = mxc_serial_init, |
Loic Poulain | 7150f56 | 2023-01-12 18:19:50 +0100 | [diff] [blame] | 282 | .stop = mxc_serial_stop, |
Marek Vasut | a943472 | 2012-09-14 22:37:43 +0200 | [diff] [blame] | 283 | .setbrg = mxc_serial_setbrg, |
| 284 | .putc = mxc_serial_putc, |
Marek Vasut | ec3fd68 | 2012-10-06 14:07:02 +0000 | [diff] [blame] | 285 | .puts = default_serial_puts, |
Marek Vasut | a943472 | 2012-09-14 22:37:43 +0200 | [diff] [blame] | 286 | .getc = mxc_serial_getc, |
| 287 | .tstc = mxc_serial_tstc, |
| 288 | }; |
| 289 | |
| 290 | void mxc_serial_initialize(void) |
| 291 | { |
| 292 | serial_register(&mxc_serial_drv); |
| 293 | } |
| 294 | |
| 295 | __weak struct serial_device *default_serial_console(void) |
| 296 | { |
| 297 | return &mxc_serial_drv; |
| 298 | } |
Simon Glass | a8ba569 | 2014-10-01 19:57:27 -0600 | [diff] [blame] | 299 | #endif |
| 300 | |
Adam Ford | e434b41 | 2019-02-19 22:07:22 -0600 | [diff] [blame] | 301 | #if CONFIG_IS_ENABLED(DM_SERIAL) |
Simon Glass | a8ba569 | 2014-10-01 19:57:27 -0600 | [diff] [blame] | 302 | |
Simon Glass | a8ba569 | 2014-10-01 19:57:27 -0600 | [diff] [blame] | 303 | int mxc_serial_setbrg(struct udevice *dev, int baudrate) |
| 304 | { |
Simon Glass | 0fd3d91 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 305 | struct mxc_serial_plat *plat = dev_get_plat(dev); |
Simon Glass | a8ba569 | 2014-10-01 19:57:27 -0600 | [diff] [blame] | 306 | u32 clk = imx_get_uartclk(); |
| 307 | |
Jagan Teki | 45d9751 | 2017-06-06 05:31:49 +0000 | [diff] [blame] | 308 | _mxc_serial_setbrg(plat->reg, clk, baudrate, plat->use_dte); |
Simon Glass | a8ba569 | 2014-10-01 19:57:27 -0600 | [diff] [blame] | 309 | |
| 310 | return 0; |
| 311 | } |
| 312 | |
| 313 | static int mxc_serial_probe(struct udevice *dev) |
| 314 | { |
Simon Glass | 0fd3d91 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 315 | struct mxc_serial_plat *plat = dev_get_plat(dev); |
Simon Glass | a8ba569 | 2014-10-01 19:57:27 -0600 | [diff] [blame] | 316 | |
Max Krummenacher | a245320 | 2019-02-01 16:04:50 +0100 | [diff] [blame] | 317 | _mxc_serial_init(plat->reg, plat->use_dte); |
Simon Glass | a8ba569 | 2014-10-01 19:57:27 -0600 | [diff] [blame] | 318 | |
| 319 | return 0; |
| 320 | } |
| 321 | |
| 322 | static int mxc_serial_getc(struct udevice *dev) |
| 323 | { |
Simon Glass | 0fd3d91 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 324 | struct mxc_serial_plat *plat = dev_get_plat(dev); |
Simon Glass | a8ba569 | 2014-10-01 19:57:27 -0600 | [diff] [blame] | 325 | struct mxc_uart *const uart = plat->reg; |
| 326 | |
| 327 | if (readl(&uart->ts) & UTS_RXEMPTY) |
| 328 | return -EAGAIN; |
| 329 | |
| 330 | return readl(&uart->rxd) & URXD_RX_DATA; |
| 331 | } |
| 332 | |
| 333 | static int mxc_serial_putc(struct udevice *dev, const char ch) |
| 334 | { |
Simon Glass | 0fd3d91 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 335 | struct mxc_serial_plat *plat = dev_get_plat(dev); |
Simon Glass | a8ba569 | 2014-10-01 19:57:27 -0600 | [diff] [blame] | 336 | struct mxc_uart *const uart = plat->reg; |
| 337 | |
Loic Poulain | ad72507 | 2023-01-12 18:19:51 +0100 | [diff] [blame] | 338 | if (readl(&uart->ts) & UTS_TXFULL) |
Simon Glass | a8ba569 | 2014-10-01 19:57:27 -0600 | [diff] [blame] | 339 | return -EAGAIN; |
| 340 | |
| 341 | writel(ch, &uart->txd); |
| 342 | |
| 343 | return 0; |
| 344 | } |
| 345 | |
| 346 | static int mxc_serial_pending(struct udevice *dev, bool input) |
| 347 | { |
Simon Glass | 0fd3d91 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 348 | struct mxc_serial_plat *plat = dev_get_plat(dev); |
Simon Glass | a8ba569 | 2014-10-01 19:57:27 -0600 | [diff] [blame] | 349 | struct mxc_uart *const uart = plat->reg; |
| 350 | uint32_t sr2 = readl(&uart->sr2); |
| 351 | |
| 352 | if (input) |
| 353 | return sr2 & USR2_RDR ? 1 : 0; |
| 354 | else |
| 355 | return sr2 & USR2_TXDC ? 0 : 1; |
| 356 | } |
| 357 | |
| 358 | static const struct dm_serial_ops mxc_serial_ops = { |
| 359 | .putc = mxc_serial_putc, |
| 360 | .pending = mxc_serial_pending, |
| 361 | .getc = mxc_serial_getc, |
| 362 | .setbrg = mxc_serial_setbrg, |
| 363 | }; |
| 364 | |
Stefan Agner | a99546a | 2016-10-05 15:27:03 -0700 | [diff] [blame] | 365 | #if CONFIG_IS_ENABLED(OF_CONTROL) |
Simon Glass | d1998a9 | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 366 | static int mxc_serial_of_to_plat(struct udevice *dev) |
Stefan Agner | a99546a | 2016-10-05 15:27:03 -0700 | [diff] [blame] | 367 | { |
Simon Glass | 0fd3d91 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 368 | struct mxc_serial_plat *plat = dev_get_plat(dev); |
Stefan Agner | a99546a | 2016-10-05 15:27:03 -0700 | [diff] [blame] | 369 | fdt_addr_t addr; |
| 370 | |
Masahiro Yamada | 2548493 | 2020-07-17 14:36:48 +0900 | [diff] [blame] | 371 | addr = dev_read_addr(dev); |
Stefan Agner | a99546a | 2016-10-05 15:27:03 -0700 | [diff] [blame] | 372 | if (addr == FDT_ADDR_T_NONE) |
| 373 | return -EINVAL; |
| 374 | |
| 375 | plat->reg = (struct mxc_uart *)addr; |
| 376 | |
Simon Glass | e160f7d | 2017-01-17 16:52:55 -0700 | [diff] [blame] | 377 | plat->use_dte = fdtdec_get_bool(gd->fdt_blob, dev_of_offset(dev), |
Stefan Agner | a99546a | 2016-10-05 15:27:03 -0700 | [diff] [blame] | 378 | "fsl,dte-mode"); |
| 379 | return 0; |
| 380 | } |
| 381 | |
| 382 | static const struct udevice_id mxc_serial_ids[] = { |
Lukasz Majewski | 2756fd1 | 2019-07-09 17:00:05 +0200 | [diff] [blame] | 383 | { .compatible = "fsl,imx21-uart" }, |
| 384 | { .compatible = "fsl,imx53-uart" }, |
Marek Vasut | 6757fa5 | 2019-05-17 21:56:40 +0200 | [diff] [blame] | 385 | { .compatible = "fsl,imx6sx-uart" }, |
Sébastien Szymanski | 3a5d636 | 2017-03-07 14:33:24 +0100 | [diff] [blame] | 386 | { .compatible = "fsl,imx6ul-uart" }, |
Stefan Agner | a99546a | 2016-10-05 15:27:03 -0700 | [diff] [blame] | 387 | { .compatible = "fsl,imx7d-uart" }, |
Bernhard Messerklinger | 4684fa8 | 2018-09-03 10:17:35 +0200 | [diff] [blame] | 388 | { .compatible = "fsl,imx6q-uart" }, |
Stefan Agner | a99546a | 2016-10-05 15:27:03 -0700 | [diff] [blame] | 389 | { } |
| 390 | }; |
| 391 | #endif |
| 392 | |
Simon Glass | a8ba569 | 2014-10-01 19:57:27 -0600 | [diff] [blame] | 393 | U_BOOT_DRIVER(serial_mxc) = { |
| 394 | .name = "serial_mxc", |
| 395 | .id = UCLASS_SERIAL, |
Stefan Agner | a99546a | 2016-10-05 15:27:03 -0700 | [diff] [blame] | 396 | #if CONFIG_IS_ENABLED(OF_CONTROL) |
| 397 | .of_match = mxc_serial_ids, |
Simon Glass | d1998a9 | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 398 | .of_to_plat = mxc_serial_of_to_plat, |
Simon Glass | 8a8d24b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 399 | .plat_auto = sizeof(struct mxc_serial_plat), |
Stefan Agner | a99546a | 2016-10-05 15:27:03 -0700 | [diff] [blame] | 400 | #endif |
Simon Glass | a8ba569 | 2014-10-01 19:57:27 -0600 | [diff] [blame] | 401 | .probe = mxc_serial_probe, |
| 402 | .ops = &mxc_serial_ops, |
| 403 | .flags = DM_FLAG_PRE_RELOC, |
| 404 | }; |
| 405 | #endif |
Jagan Teki | 61366b7 | 2017-06-06 05:31:51 +0000 | [diff] [blame] | 406 | |
| 407 | #ifdef CONFIG_DEBUG_UART_MXC |
| 408 | #include <debug_uart.h> |
| 409 | |
| 410 | static inline void _debug_uart_init(void) |
| 411 | { |
Pali Rohár | b62450c | 2022-05-27 22:15:24 +0200 | [diff] [blame] | 412 | struct mxc_uart *base = (struct mxc_uart *)CONFIG_VAL(DEBUG_UART_BASE); |
Jagan Teki | 61366b7 | 2017-06-06 05:31:51 +0000 | [diff] [blame] | 413 | |
Max Krummenacher | a245320 | 2019-02-01 16:04:50 +0100 | [diff] [blame] | 414 | _mxc_serial_init(base, false); |
Jagan Teki | 61366b7 | 2017-06-06 05:31:51 +0000 | [diff] [blame] | 415 | _mxc_serial_setbrg(base, CONFIG_DEBUG_UART_CLOCK, |
| 416 | CONFIG_BAUDRATE, false); |
| 417 | } |
| 418 | |
| 419 | static inline void _debug_uart_putc(int ch) |
| 420 | { |
Pali Rohár | b62450c | 2022-05-27 22:15:24 +0200 | [diff] [blame] | 421 | struct mxc_uart *base = (struct mxc_uart *)CONFIG_VAL(DEBUG_UART_BASE); |
Jagan Teki | 61366b7 | 2017-06-06 05:31:51 +0000 | [diff] [blame] | 422 | |
| 423 | while (!(readl(&base->ts) & UTS_TXEMPTY)) |
Stefan Roese | 29caf93 | 2022-09-02 14:10:46 +0200 | [diff] [blame] | 424 | schedule(); |
Jagan Teki | 61366b7 | 2017-06-06 05:31:51 +0000 | [diff] [blame] | 425 | |
| 426 | writel(ch, &base->txd); |
| 427 | } |
| 428 | |
| 429 | DEBUG_UART_FUNCS |
| 430 | |
| 431 | #endif |