1 // SPDX-License-Identifier: GPL-2.0+ 2 /* 3 * Driver for Motorola/Freescale IMX serial ports 4 * 5 * Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o. 6 * 7 * Author: Sascha Hauer <sascha@saschahauer.de> 8 * Copyright (C) 2004 Pengutronix 9 */ 10 11 #include <linux/module.h> 12 #include <linux/ioport.h> 13 #include <linux/init.h> 14 #include <linux/console.h> 15 #include <linux/sysrq.h> 16 #include <linux/platform_device.h> 17 #include <linux/tty.h> 18 #include <linux/tty_flip.h> 19 #include <linux/serial_core.h> 20 #include <linux/serial.h> 21 #include <linux/clk.h> 22 #include <linux/delay.h> 23 #include <linux/ktime.h> 24 #include <linux/pinctrl/consumer.h> 25 #include <linux/rational.h> 26 #include <linux/slab.h> 27 #include <linux/of.h> 28 #include <linux/io.h> 29 #include <linux/dma-mapping.h> 30 31 #include <asm/irq.h> 32 #include <linux/dma/imx-dma.h> 33 34 #include "serial_mctrl_gpio.h" 35 36 /* Register definitions */ 37 #define URXD0 0x0 /* Receiver Register */ 38 #define URTX0 0x40 /* Transmitter Register */ 39 #define UCR1 0x80 /* Control Register 1 */ 40 #define UCR2 0x84 /* Control Register 2 */ 41 #define UCR3 0x88 /* Control Register 3 */ 42 #define UCR4 0x8c /* Control Register 4 */ 43 #define UFCR 0x90 /* FIFO Control Register */ 44 #define USR1 0x94 /* Status Register 1 */ 45 #define USR2 0x98 /* Status Register 2 */ 46 #define UESC 0x9c /* Escape Character Register */ 47 #define UTIM 0xa0 /* Escape Timer Register */ 48 #define UBIR 0xa4 /* BRM Incremental Register */ 49 #define UBMR 0xa8 /* BRM Modulator Register */ 50 #define UBRC 0xac /* Baud Rate Count Register */ 51 #define IMX21_ONEMS 0xb0 /* One Millisecond register */ 52 #define IMX1_UTS 0xd0 /* UART Test Register on i.mx1 */ 53 #define IMX21_UTS 0xb4 /* UART Test Register on all other i.mx*/ 54 55 /* UART Control Register Bit Fields.*/ 56 #define URXD_DUMMY_READ (1<<16) 57 #define URXD_CHARRDY (1<<15) 58 #define URXD_ERR (1<<14) 59 #define URXD_OVRRUN (1<<13) 60 #define URXD_FRMERR (1<<12) 61 #define URXD_BRK (1<<11) 62 #define URXD_PRERR (1<<10) 63 #define URXD_RX_DATA (0xFF<<0) 64 #define UCR1_ADEN (1<<15) /* Auto detect interrupt */ 65 #define UCR1_ADBR (1<<14) /* Auto detect baud rate */ 66 #define UCR1_TRDYEN (1<<13) /* Transmitter ready interrupt enable */ 67 #define UCR1_IDEN (1<<12) /* Idle condition interrupt */ 68 #define UCR1_ICD_REG(x) (((x) & 3) << 10) /* idle condition detect */ 69 #define UCR1_RRDYEN (1<<9) /* Recv ready interrupt enable */ 70 #define UCR1_RXDMAEN (1<<8) /* Recv ready DMA enable */ 71 #define UCR1_IREN (1<<7) /* Infrared interface enable */ 72 #define UCR1_TXMPTYEN (1<<6) /* Transimitter empty interrupt enable */ 73 #define UCR1_RTSDEN (1<<5) /* RTS delta interrupt enable */ 74 #define UCR1_SNDBRK (1<<4) /* Send break */ 75 #define UCR1_TXDMAEN (1<<3) /* Transmitter ready DMA enable */ 76 #define IMX1_UCR1_UARTCLKEN (1<<2) /* UART clock enabled, i.mx1 only */ 77 #define UCR1_ATDMAEN (1<<2) /* Aging DMA Timer Enable */ 78 #define UCR1_DOZE (1<<1) /* Doze */ 79 #define UCR1_UARTEN (1<<0) /* UART enabled */ 80 #define UCR2_ESCI (1<<15) /* Escape seq interrupt enable */ 81 #define UCR2_IRTS (1<<14) /* Ignore RTS pin */ 82 #define UCR2_CTSC (1<<13) /* CTS pin control */ 83 #define UCR2_CTS (1<<12) /* Clear to send */ 84 #define UCR2_ESCEN (1<<11) /* Escape enable */ 85 #define UCR2_PREN (1<<8) /* Parity enable */ 86 #define UCR2_PROE (1<<7) /* Parity odd/even */ 87 #define UCR2_STPB (1<<6) /* Stop */ 88 #define UCR2_WS (1<<5) /* Word size */ 89 #define UCR2_RTSEN (1<<4) /* Request to send interrupt enable */ 90 #define UCR2_ATEN (1<<3) /* Aging Timer Enable */ 91 #define UCR2_TXEN (1<<2) /* Transmitter enabled */ 92 #define UCR2_RXEN (1<<1) /* Receiver enabled */ 93 #define UCR2_SRST (1<<0) /* SW reset */ 94 #define UCR3_DTREN (1<<13) /* DTR interrupt enable */ 95 #define UCR3_PARERREN (1<<12) /* Parity enable */ 96 #define UCR3_FRAERREN (1<<11) /* Frame error interrupt enable */ 97 #define UCR3_DSR (1<<10) /* Data set ready */ 98 #define UCR3_DCD (1<<9) /* Data carrier detect */ 99 #define UCR3_RI (1<<8) /* Ring indicator */ 100 #define UCR3_ADNIMP (1<<7) /* Autobaud Detection Not Improved */ 101 #define UCR3_RXDSEN (1<<6) /* Receive status interrupt enable */ 102 #define UCR3_AIRINTEN (1<<5) /* Async IR wake interrupt enable */ 103 #define UCR3_AWAKEN (1<<4) /* Async wake interrupt enable */ 104 #define UCR3_DTRDEN (1<<3) /* Data Terminal Ready Delta Enable. */ 105 #define IMX21_UCR3_RXDMUXSEL (1<<2) /* RXD Muxed Input Select */ 106 #define UCR3_INVT (1<<1) /* Inverted Infrared transmission */ 107 #define UCR3_BPEN (1<<0) /* Preset registers enable */ 108 #define UCR4_CTSTL_SHF 10 /* CTS trigger level shift */ 109 #define UCR4_CTSTL_MASK 0x3F /* CTS trigger is 6 bits wide */ 110 #define UCR4_INVR (1<<9) /* Inverted infrared reception */ 111 #define UCR4_ENIRI (1<<8) /* Serial infrared interrupt enable */ 112 #define UCR4_WKEN (1<<7) /* Wake interrupt enable */ 113 #define UCR4_REF16 (1<<6) /* Ref freq 16 MHz */ 114 #define UCR4_IDDMAEN (1<<6) /* DMA IDLE Condition Detected */ 115 #define UCR4_IRSC (1<<5) /* IR special case */ 116 #define UCR4_TCEN (1<<3) /* Transmit complete interrupt enable */ 117 #define UCR4_BKEN (1<<2) /* Break condition interrupt enable */ 118 #define UCR4_OREN (1<<1) /* Receiver overrun interrupt enable */ 119 #define UCR4_DREN (1<<0) /* Recv data ready interrupt enable */ 120 #define UFCR_RXTL_SHF 0 /* Receiver trigger level shift */ 121 #define UFCR_DCEDTE (1<<6) /* DCE/DTE mode select */ 122 #define UFCR_RFDIV (7<<7) /* Reference freq divider mask */ 123 #define UFCR_RFDIV_REG(x) (((x) < 7 ? 6 - (x) : 6) << 7) 124 #define UFCR_TXTL_SHF 10 /* Transmitter trigger level shift */ 125 #define USR1_PARITYERR (1<<15) /* Parity error interrupt flag */ 126 #define USR1_RTSS (1<<14) /* RTS pin status */ 127 #define USR1_TRDY (1<<13) /* Transmitter ready interrupt/dma flag */ 128 #define USR1_RTSD (1<<12) /* RTS delta */ 129 #define USR1_ESCF (1<<11) /* Escape seq interrupt flag */ 130 #define USR1_FRAMERR (1<<10) /* Frame error interrupt flag */ 131 #define USR1_RRDY (1<<9) /* Receiver ready interrupt/dma flag */ 132 #define USR1_AGTIM (1<<8) /* Ageing timer interrupt flag */ 133 #define USR1_DTRD (1<<7) /* DTR Delta */ 134 #define USR1_RXDS (1<<6) /* Receiver idle interrupt flag */ 135 #define USR1_AIRINT (1<<5) /* Async IR wake interrupt flag */ 136 #define USR1_AWAKE (1<<4) /* Aysnc wake interrupt flag */ 137 #define USR2_ADET (1<<15) /* Auto baud rate detect complete */ 138 #define USR2_TXFE (1<<14) /* Transmit buffer FIFO empty */ 139 #define USR2_DTRF (1<<13) /* DTR edge interrupt flag */ 140 #define USR2_IDLE (1<<12) /* Idle condition */ 141 #define USR2_RIDELT (1<<10) /* Ring Interrupt Delta */ 142 #define USR2_RIIN (1<<9) /* Ring Indicator Input */ 143 #define USR2_IRINT (1<<8) /* Serial infrared interrupt flag */ 144 #define USR2_WAKE (1<<7) /* Wake */ 145 #define USR2_DCDIN (1<<5) /* Data Carrier Detect Input */ 146 #define USR2_RTSF (1<<4) /* RTS edge interrupt flag */ 147 #define USR2_TXDC (1<<3) /* Transmitter complete */ 148 #define USR2_BRCD (1<<2) /* Break condition */ 149 #define USR2_ORE (1<<1) /* Overrun error */ 150 #define USR2_RDR (1<<0) /* Recv data ready */ 151 #define UTS_FRCPERR (1<<13) /* Force parity error */ 152 #define UTS_LOOP (1<<12) /* Loop tx and rx */ 153 #define UTS_TXEMPTY (1<<6) /* TxFIFO empty */ 154 #define UTS_RXEMPTY (1<<5) /* RxFIFO empty */ 155 #define UTS_TXFULL (1<<4) /* TxFIFO full */ 156 #define UTS_RXFULL (1<<3) /* RxFIFO full */ 157 #define UTS_SOFTRST (1<<0) /* Software reset */ 158 159 /* We've been assigned a range on the "Low-density serial ports" major */ 160 #define SERIAL_IMX_MAJOR 207 161 #define MINOR_START 16 162 #define DEV_NAME "ttymxc" 163 164 /* 165 * This determines how often we check the modem status signals 166 * for any change. They generally aren't connected to an IRQ 167 * so we have to poll them. We also check immediately before 168 * filling the TX fifo incase CTS has been dropped. 169 */ 170 #define MCTRL_TIMEOUT (250*HZ/1000) 171 172 #define DRIVER_NAME "IMX-uart" 173 174 #define UART_NR 8 175 176 /* i.MX21 type uart runs on all i.mx except i.MX1 and i.MX6q */ 177 enum imx_uart_type { 178 IMX1_UART, 179 IMX21_UART, 180 IMX53_UART, 181 IMX6Q_UART, 182 }; 183 184 /* device type dependent stuff */ 185 struct imx_uart_data { 186 unsigned uts_reg; 187 enum imx_uart_type devtype; 188 }; 189 190 enum imx_tx_state { 191 OFF, 192 WAIT_AFTER_RTS, 193 SEND, 194 WAIT_AFTER_SEND, 195 }; 196 197 struct imx_port { 198 struct uart_port port; 199 struct timer_list timer; 200 unsigned int old_status; 201 unsigned int have_rtscts:1; 202 unsigned int have_rtsgpio:1; 203 unsigned int dte_mode:1; 204 unsigned int inverted_tx:1; 205 unsigned int inverted_rx:1; 206 struct clk *clk_ipg; 207 struct clk *clk_per; 208 const struct imx_uart_data *devdata; 209 210 struct mctrl_gpios *gpios; 211 212 /* counter to stop 0xff flood */ 213 int idle_counter; 214 215 /* DMA fields */ 216 unsigned int dma_is_enabled:1; 217 unsigned int dma_is_rxing:1; 218 unsigned int dma_is_txing:1; 219 struct dma_chan *dma_chan_rx, *dma_chan_tx; 220 struct scatterlist rx_sgl, tx_sgl[2]; 221 void *rx_buf; 222 struct circ_buf rx_ring; 223 unsigned int rx_buf_size; 224 unsigned int rx_period_length; 225 unsigned int rx_periods; 226 dma_cookie_t rx_cookie; 227 unsigned int tx_bytes; 228 unsigned int dma_tx_nents; 229 unsigned int saved_reg[10]; 230 bool context_saved; 231 232 enum imx_tx_state tx_state; 233 struct hrtimer trigger_start_tx; 234 struct hrtimer trigger_stop_tx; 235 }; 236 237 struct imx_port_ucrs { 238 unsigned int ucr1; 239 unsigned int ucr2; 240 unsigned int ucr3; 241 }; 242 243 static struct imx_uart_data imx_uart_devdata[] = { 244 [IMX1_UART] = { 245 .uts_reg = IMX1_UTS, 246 .devtype = IMX1_UART, 247 }, 248 [IMX21_UART] = { 249 .uts_reg = IMX21_UTS, 250 .devtype = IMX21_UART, 251 }, 252 [IMX53_UART] = { 253 .uts_reg = IMX21_UTS, 254 .devtype = IMX53_UART, 255 }, 256 [IMX6Q_UART] = { 257 .uts_reg = IMX21_UTS, 258 .devtype = IMX6Q_UART, 259 }, 260 }; 261 262 static const struct of_device_id imx_uart_dt_ids[] = { 263 { .compatible = "fsl,imx6q-uart", .data = &imx_uart_devdata[IMX6Q_UART], }, 264 { .compatible = "fsl,imx53-uart", .data = &imx_uart_devdata[IMX53_UART], }, 265 { .compatible = "fsl,imx1-uart", .data = &imx_uart_devdata[IMX1_UART], }, 266 { .compatible = "fsl,imx21-uart", .data = &imx_uart_devdata[IMX21_UART], }, 267 { /* sentinel */ } 268 }; 269 MODULE_DEVICE_TABLE(of, imx_uart_dt_ids); 270 271 static inline void imx_uart_writel(struct imx_port *sport, u32 val, u32 offset) 272 { 273 writel(val, sport->port.membase + offset); 274 } 275 276 static inline u32 imx_uart_readl(struct imx_port *sport, u32 offset) 277 { 278 return readl(sport->port.membase + offset); 279 } 280 281 static inline unsigned imx_uart_uts_reg(struct imx_port *sport) 282 { 283 return sport->devdata->uts_reg; 284 } 285 286 static inline int imx_uart_is_imx1(struct imx_port *sport) 287 { 288 return sport->devdata->devtype == IMX1_UART; 289 } 290 291 /* 292 * Save and restore functions for UCR1, UCR2 and UCR3 registers 293 */ 294 #if IS_ENABLED(CONFIG_SERIAL_IMX_CONSOLE) 295 static void imx_uart_ucrs_save(struct imx_port *sport, 296 struct imx_port_ucrs *ucr) 297 { 298 /* save control registers */ 299 ucr->ucr1 = imx_uart_readl(sport, UCR1); 300 ucr->ucr2 = imx_uart_readl(sport, UCR2); 301 ucr->ucr3 = imx_uart_readl(sport, UCR3); 302 } 303 304 static void imx_uart_ucrs_restore(struct imx_port *sport, 305 struct imx_port_ucrs *ucr) 306 { 307 /* restore control registers */ 308 imx_uart_writel(sport, ucr->ucr1, UCR1); 309 imx_uart_writel(sport, ucr->ucr2, UCR2); 310 imx_uart_writel(sport, ucr->ucr3, UCR3); 311 } 312 #endif 313 314 /* called with port.lock taken and irqs caller dependent */ 315 static void imx_uart_rts_active(struct imx_port *sport, u32 *ucr2) 316 { 317 *ucr2 &= ~(UCR2_CTSC | UCR2_CTS); 318 319 mctrl_gpio_set(sport->gpios, sport->port.mctrl | TIOCM_RTS); 320 } 321 322 /* called with port.lock taken and irqs caller dependent */ 323 static void imx_uart_rts_inactive(struct imx_port *sport, u32 *ucr2) 324 { 325 *ucr2 &= ~UCR2_CTSC; 326 *ucr2 |= UCR2_CTS; 327 328 mctrl_gpio_set(sport->gpios, sport->port.mctrl & ~TIOCM_RTS); 329 } 330 331 static void start_hrtimer_ms(struct hrtimer *hrt, unsigned long msec) 332 { 333 hrtimer_start(hrt, ms_to_ktime(msec), HRTIMER_MODE_REL); 334 } 335 336 /* called with port.lock taken and irqs off */ 337 static void imx_uart_soft_reset(struct imx_port *sport) 338 { 339 int i = 10; 340 u32 ucr2, ubir, ubmr, uts; 341 342 /* 343 * According to the Reference Manual description of the UART SRST bit: 344 * 345 * "Reset the transmit and receive state machines, 346 * all FIFOs and register USR1, USR2, UBIR, UBMR, UBRC, URXD, UTXD 347 * and UTS[6-3]". 348 * 349 * We don't need to restore the old values from USR1, USR2, URXD and 350 * UTXD. UBRC is read only, so only save/restore the other three 351 * registers. 352 */ 353 ubir = imx_uart_readl(sport, UBIR); 354 ubmr = imx_uart_readl(sport, UBMR); 355 uts = imx_uart_readl(sport, IMX21_UTS); 356 357 ucr2 = imx_uart_readl(sport, UCR2); 358 imx_uart_writel(sport, ucr2 & ~UCR2_SRST, UCR2); 359 360 while (!(imx_uart_readl(sport, UCR2) & UCR2_SRST) && (--i > 0)) 361 udelay(1); 362 363 /* Restore the registers */ 364 imx_uart_writel(sport, ubir, UBIR); 365 imx_uart_writel(sport, ubmr, UBMR); 366 imx_uart_writel(sport, uts, IMX21_UTS); 367 368 sport->idle_counter = 0; 369 } 370 371 static void imx_uart_disable_loopback_rs485(struct imx_port *sport) 372 { 373 unsigned int uts; 374 375 /* See SER_RS485_ENABLED/UTS_LOOP comment in imx_uart_probe() */ 376 uts = imx_uart_readl(sport, imx_uart_uts_reg(sport)); 377 uts &= ~UTS_LOOP; 378 imx_uart_writel(sport, uts, imx_uart_uts_reg(sport)); 379 } 380 381 /* called with port.lock taken and irqs off */ 382 static void imx_uart_start_rx(struct uart_port *port) 383 { 384 struct imx_port *sport = (struct imx_port *)port; 385 unsigned int ucr1, ucr2; 386 387 ucr1 = imx_uart_readl(sport, UCR1); 388 ucr2 = imx_uart_readl(sport, UCR2); 389 390 ucr2 |= UCR2_RXEN; 391 392 if (sport->dma_is_enabled) { 393 ucr1 |= UCR1_RXDMAEN | UCR1_ATDMAEN; 394 } else { 395 ucr1 |= UCR1_RRDYEN; 396 ucr2 |= UCR2_ATEN; 397 } 398 399 /* Write UCR2 first as it includes RXEN */ 400 imx_uart_writel(sport, ucr2, UCR2); 401 imx_uart_writel(sport, ucr1, UCR1); 402 imx_uart_disable_loopback_rs485(sport); 403 } 404 405 /* called with port.lock taken and irqs off */ 406 static void imx_uart_stop_tx(struct uart_port *port) 407 { 408 struct imx_port *sport = (struct imx_port *)port; 409 u32 ucr1, ucr4, usr2; 410 411 if (sport->tx_state == OFF) 412 return; 413 414 /* 415 * We are maybe in the SMP context, so if the DMA TX thread is running 416 * on other cpu, we have to wait for it to finish. 417 */ 418 if (sport->dma_is_txing) 419 return; 420 421 ucr1 = imx_uart_readl(sport, UCR1); 422 imx_uart_writel(sport, ucr1 & ~UCR1_TRDYEN, UCR1); 423 424 ucr4 = imx_uart_readl(sport, UCR4); 425 usr2 = imx_uart_readl(sport, USR2); 426 if ((!(usr2 & USR2_TXDC)) && (ucr4 & UCR4_TCEN)) { 427 /* The shifter is still busy, so retry once TC triggers */ 428 return; 429 } 430 431 ucr4 &= ~UCR4_TCEN; 432 imx_uart_writel(sport, ucr4, UCR4); 433 434 /* in rs485 mode disable transmitter */ 435 if (port->rs485.flags & SER_RS485_ENABLED) { 436 if (sport->tx_state == SEND) { 437 sport->tx_state = WAIT_AFTER_SEND; 438 439 if (port->rs485.delay_rts_after_send > 0) { 440 start_hrtimer_ms(&sport->trigger_stop_tx, 441 port->rs485.delay_rts_after_send); 442 return; 443 } 444 445 /* continue without any delay */ 446 } 447 448 if (sport->tx_state == WAIT_AFTER_RTS || 449 sport->tx_state == WAIT_AFTER_SEND) { 450 u32 ucr2; 451 452 hrtimer_try_to_cancel(&sport->trigger_start_tx); 453 454 ucr2 = imx_uart_readl(sport, UCR2); 455 if (port->rs485.flags & SER_RS485_RTS_AFTER_SEND) 456 imx_uart_rts_active(sport, &ucr2); 457 else 458 imx_uart_rts_inactive(sport, &ucr2); 459 imx_uart_writel(sport, ucr2, UCR2); 460 461 if (!port->rs485_rx_during_tx_gpio) 462 imx_uart_start_rx(port); 463 464 sport->tx_state = OFF; 465 } 466 } else { 467 sport->tx_state = OFF; 468 } 469 } 470 471 /* called with port.lock taken and irqs off */ 472 static void imx_uart_stop_rx(struct uart_port *port) 473 { 474 struct imx_port *sport = (struct imx_port *)port; 475 u32 ucr1, ucr2, ucr4, uts; 476 477 ucr1 = imx_uart_readl(sport, UCR1); 478 ucr2 = imx_uart_readl(sport, UCR2); 479 ucr4 = imx_uart_readl(sport, UCR4); 480 481 if (sport->dma_is_enabled) { 482 ucr1 &= ~(UCR1_RXDMAEN | UCR1_ATDMAEN); 483 } else { 484 ucr1 &= ~UCR1_RRDYEN; 485 ucr2 &= ~UCR2_ATEN; 486 ucr4 &= ~UCR4_OREN; 487 } 488 imx_uart_writel(sport, ucr1, UCR1); 489 imx_uart_writel(sport, ucr4, UCR4); 490 491 /* See SER_RS485_ENABLED/UTS_LOOP comment in imx_uart_probe() */ 492 if (port->rs485.flags & SER_RS485_ENABLED && 493 port->rs485.flags & SER_RS485_RTS_ON_SEND && 494 sport->have_rtscts && !sport->have_rtsgpio) { 495 uts = imx_uart_readl(sport, imx_uart_uts_reg(sport)); 496 uts |= UTS_LOOP; 497 imx_uart_writel(sport, uts, imx_uart_uts_reg(sport)); 498 ucr2 |= UCR2_RXEN; 499 } else { 500 ucr2 &= ~UCR2_RXEN; 501 } 502 503 imx_uart_writel(sport, ucr2, UCR2); 504 } 505 506 /* called with port.lock taken and irqs off */ 507 static void imx_uart_enable_ms(struct uart_port *port) 508 { 509 struct imx_port *sport = (struct imx_port *)port; 510 511 mod_timer(&sport->timer, jiffies); 512 513 mctrl_gpio_enable_ms(sport->gpios); 514 } 515 516 static void imx_uart_dma_tx(struct imx_port *sport); 517 518 /* called with port.lock taken and irqs off */ 519 static inline void imx_uart_transmit_buffer(struct imx_port *sport) 520 { 521 struct circ_buf *xmit = &sport->port.state->xmit; 522 523 if (sport->port.x_char) { 524 /* Send next char */ 525 imx_uart_writel(sport, sport->port.x_char, URTX0); 526 sport->port.icount.tx++; 527 sport->port.x_char = 0; 528 return; 529 } 530 531 if (uart_circ_empty(xmit) || uart_tx_stopped(&sport->port)) { 532 imx_uart_stop_tx(&sport->port); 533 return; 534 } 535 536 if (sport->dma_is_enabled) { 537 u32 ucr1; 538 /* 539 * We've just sent a X-char Ensure the TX DMA is enabled 540 * and the TX IRQ is disabled. 541 **/ 542 ucr1 = imx_uart_readl(sport, UCR1); 543 ucr1 &= ~UCR1_TRDYEN; 544 if (sport->dma_is_txing) { 545 ucr1 |= UCR1_TXDMAEN; 546 imx_uart_writel(sport, ucr1, UCR1); 547 } else { 548 imx_uart_writel(sport, ucr1, UCR1); 549 imx_uart_dma_tx(sport); 550 } 551 552 return; 553 } 554 555 while (!uart_circ_empty(xmit) && 556 !(imx_uart_readl(sport, imx_uart_uts_reg(sport)) & UTS_TXFULL)) { 557 /* send xmit->buf[xmit->tail] 558 * out the port here */ 559 imx_uart_writel(sport, xmit->buf[xmit->tail], URTX0); 560 uart_xmit_advance(&sport->port, 1); 561 } 562 563 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS) 564 uart_write_wakeup(&sport->port); 565 566 if (uart_circ_empty(xmit)) 567 imx_uart_stop_tx(&sport->port); 568 } 569 570 static void imx_uart_dma_tx_callback(void *data) 571 { 572 struct imx_port *sport = data; 573 struct scatterlist *sgl = &sport->tx_sgl[0]; 574 struct circ_buf *xmit = &sport->port.state->xmit; 575 unsigned long flags; 576 u32 ucr1; 577 578 spin_lock_irqsave(&sport->port.lock, flags); 579 580 dma_unmap_sg(sport->port.dev, sgl, sport->dma_tx_nents, DMA_TO_DEVICE); 581 582 ucr1 = imx_uart_readl(sport, UCR1); 583 ucr1 &= ~UCR1_TXDMAEN; 584 imx_uart_writel(sport, ucr1, UCR1); 585 586 uart_xmit_advance(&sport->port, sport->tx_bytes); 587 588 dev_dbg(sport->port.dev, "we finish the TX DMA.\n"); 589 590 sport->dma_is_txing = 0; 591 592 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS) 593 uart_write_wakeup(&sport->port); 594 595 if (!uart_circ_empty(xmit) && !uart_tx_stopped(&sport->port)) 596 imx_uart_dma_tx(sport); 597 else if (sport->port.rs485.flags & SER_RS485_ENABLED) { 598 u32 ucr4 = imx_uart_readl(sport, UCR4); 599 ucr4 |= UCR4_TCEN; 600 imx_uart_writel(sport, ucr4, UCR4); 601 } 602 603 spin_unlock_irqrestore(&sport->port.lock, flags); 604 } 605 606 /* called with port.lock taken and irqs off */ 607 static void imx_uart_dma_tx(struct imx_port *sport) 608 { 609 struct circ_buf *xmit = &sport->port.state->xmit; 610 struct scatterlist *sgl = sport->tx_sgl; 611 struct dma_async_tx_descriptor *desc; 612 struct dma_chan *chan = sport->dma_chan_tx; 613 struct device *dev = sport->port.dev; 614 u32 ucr1, ucr4; 615 int ret; 616 617 if (sport->dma_is_txing) 618 return; 619 620 ucr4 = imx_uart_readl(sport, UCR4); 621 ucr4 &= ~UCR4_TCEN; 622 imx_uart_writel(sport, ucr4, UCR4); 623 624 sport->tx_bytes = uart_circ_chars_pending(xmit); 625 626 if (xmit->tail < xmit->head || xmit->head == 0) { 627 sport->dma_tx_nents = 1; 628 sg_init_one(sgl, xmit->buf + xmit->tail, sport->tx_bytes); 629 } else { 630 sport->dma_tx_nents = 2; 631 sg_init_table(sgl, 2); 632 sg_set_buf(sgl, xmit->buf + xmit->tail, 633 UART_XMIT_SIZE - xmit->tail); 634 sg_set_buf(sgl + 1, xmit->buf, xmit->head); 635 } 636 637 ret = dma_map_sg(dev, sgl, sport->dma_tx_nents, DMA_TO_DEVICE); 638 if (ret == 0) { 639 dev_err(dev, "DMA mapping error for TX.\n"); 640 return; 641 } 642 desc = dmaengine_prep_slave_sg(chan, sgl, ret, 643 DMA_MEM_TO_DEV, DMA_PREP_INTERRUPT); 644 if (!desc) { 645 dma_unmap_sg(dev, sgl, sport->dma_tx_nents, 646 DMA_TO_DEVICE); 647 dev_err(dev, "We cannot prepare for the TX slave dma!\n"); 648 return; 649 } 650 desc->callback = imx_uart_dma_tx_callback; 651 desc->callback_param = sport; 652 653 dev_dbg(dev, "TX: prepare to send %lu bytes by DMA.\n", 654 uart_circ_chars_pending(xmit)); 655 656 ucr1 = imx_uart_readl(sport, UCR1); 657 ucr1 |= UCR1_TXDMAEN; 658 imx_uart_writel(sport, ucr1, UCR1); 659 660 /* fire it */ 661 sport->dma_is_txing = 1; 662 dmaengine_submit(desc); 663 dma_async_issue_pending(chan); 664 return; 665 } 666 667 /* called with port.lock taken and irqs off */ 668 static void imx_uart_start_tx(struct uart_port *port) 669 { 670 struct imx_port *sport = (struct imx_port *)port; 671 u32 ucr1; 672 673 if (!sport->port.x_char && uart_circ_empty(&port->state->xmit)) 674 return; 675 676 /* 677 * We cannot simply do nothing here if sport->tx_state == SEND already 678 * because UCR1_TXMPTYEN might already have been cleared in 679 * imx_uart_stop_tx(), but tx_state is still SEND. 680 */ 681 682 if (port->rs485.flags & SER_RS485_ENABLED) { 683 if (sport->tx_state == OFF) { 684 u32 ucr2 = imx_uart_readl(sport, UCR2); 685 if (port->rs485.flags & SER_RS485_RTS_ON_SEND) 686 imx_uart_rts_active(sport, &ucr2); 687 else 688 imx_uart_rts_inactive(sport, &ucr2); 689 imx_uart_writel(sport, ucr2, UCR2); 690 691 if (!(port->rs485.flags & SER_RS485_RX_DURING_TX) && 692 !port->rs485_rx_during_tx_gpio) 693 imx_uart_stop_rx(port); 694 695 sport->tx_state = WAIT_AFTER_RTS; 696 697 if (port->rs485.delay_rts_before_send > 0) { 698 start_hrtimer_ms(&sport->trigger_start_tx, 699 port->rs485.delay_rts_before_send); 700 return; 701 } 702 703 /* continue without any delay */ 704 } 705 706 if (sport->tx_state == WAIT_AFTER_SEND 707 || sport->tx_state == WAIT_AFTER_RTS) { 708 709 hrtimer_try_to_cancel(&sport->trigger_stop_tx); 710 711 /* 712 * Enable transmitter and shifter empty irq only if DMA 713 * is off. In the DMA case this is done in the 714 * tx-callback. 715 */ 716 if (!sport->dma_is_enabled) { 717 u32 ucr4 = imx_uart_readl(sport, UCR4); 718 ucr4 |= UCR4_TCEN; 719 imx_uart_writel(sport, ucr4, UCR4); 720 } 721 722 sport->tx_state = SEND; 723 } 724 } else { 725 sport->tx_state = SEND; 726 } 727 728 if (!sport->dma_is_enabled) { 729 ucr1 = imx_uart_readl(sport, UCR1); 730 imx_uart_writel(sport, ucr1 | UCR1_TRDYEN, UCR1); 731 } 732 733 if (sport->dma_is_enabled) { 734 if (sport->port.x_char) { 735 /* We have X-char to send, so enable TX IRQ and 736 * disable TX DMA to let TX interrupt to send X-char */ 737 ucr1 = imx_uart_readl(sport, UCR1); 738 ucr1 &= ~UCR1_TXDMAEN; 739 ucr1 |= UCR1_TRDYEN; 740 imx_uart_writel(sport, ucr1, UCR1); 741 return; 742 } 743 744 if (!uart_circ_empty(&port->state->xmit) && 745 !uart_tx_stopped(port)) 746 imx_uart_dma_tx(sport); 747 return; 748 } 749 } 750 751 static irqreturn_t __imx_uart_rtsint(int irq, void *dev_id) 752 { 753 struct imx_port *sport = dev_id; 754 u32 usr1; 755 756 imx_uart_writel(sport, USR1_RTSD, USR1); 757 usr1 = imx_uart_readl(sport, USR1) & USR1_RTSS; 758 uart_handle_cts_change(&sport->port, usr1); 759 wake_up_interruptible(&sport->port.state->port.delta_msr_wait); 760 761 return IRQ_HANDLED; 762 } 763 764 static irqreturn_t imx_uart_rtsint(int irq, void *dev_id) 765 { 766 struct imx_port *sport = dev_id; 767 irqreturn_t ret; 768 769 spin_lock(&sport->port.lock); 770 771 ret = __imx_uart_rtsint(irq, dev_id); 772 773 spin_unlock(&sport->port.lock); 774 775 return ret; 776 } 777 778 static irqreturn_t imx_uart_txint(int irq, void *dev_id) 779 { 780 struct imx_port *sport = dev_id; 781 782 spin_lock(&sport->port.lock); 783 imx_uart_transmit_buffer(sport); 784 spin_unlock(&sport->port.lock); 785 return IRQ_HANDLED; 786 } 787 788 /* Check if hardware Rx flood is in progress, and issue soft reset to stop it. 789 * This is to be called from Rx ISRs only when some bytes were actually 790 * received. 791 * 792 * A way to reproduce the flood (checked on iMX6SX) is: open iMX UART at 9600 793 * 8N1, and from external source send 0xf0 char at 115200 8N1. In about 90% of 794 * cases this starts a flood of "receiving" of 0xff characters by the iMX6 UART 795 * that is terminated by any activity on RxD line, or could be stopped by 796 * issuing soft reset to the UART (just stop/start of RX does not help). Note 797 * that what we do here is sending isolated start bit about 2.4 times shorter 798 * than it is to be on UART configured baud rate. 799 */ 800 static void imx_uart_check_flood(struct imx_port *sport, u32 usr2) 801 { 802 /* To detect hardware 0xff flood we monitor RxD line between RX 803 * interrupts to isolate "receiving" of char(s) with no activity 804 * on RxD line, that'd never happen on actual data transfers. 805 * 806 * We use USR2_WAKE bit to check for activity on RxD line, but we have a 807 * race here if we clear USR2_WAKE when receiving of a char is in 808 * progress, so we might get RX interrupt later with USR2_WAKE bit 809 * cleared. Note though that as we don't try to clear USR2_WAKE when we 810 * detected no activity, this race may hide actual activity only once. 811 * 812 * Yet another case where receive interrupt may occur without RxD 813 * activity is expiration of aging timer, so we consider this as well. 814 * 815 * We use 'idle_counter' to ensure that we got at least so many RX 816 * interrupts without any detected activity on RxD line. 2 cases 817 * described plus 1 to be on the safe side gives us a margin of 3, 818 * below. In practice I was not able to produce a false positive to 819 * induce soft reset at regular data transfers even using 1 as the 820 * margin, so 3 is actually very strong. 821 * 822 * We count interrupts, not chars in 'idle-counter' for simplicity. 823 */ 824 825 if (usr2 & USR2_WAKE) { 826 imx_uart_writel(sport, USR2_WAKE, USR2); 827 sport->idle_counter = 0; 828 } else if (++sport->idle_counter > 3) { 829 dev_warn(sport->port.dev, "RX flood detected: soft reset."); 830 imx_uart_soft_reset(sport); /* also clears 'sport->idle_counter' */ 831 } 832 } 833 834 static irqreturn_t __imx_uart_rxint(int irq, void *dev_id) 835 { 836 struct imx_port *sport = dev_id; 837 struct tty_port *port = &sport->port.state->port; 838 u32 usr2, rx; 839 840 /* If we received something, check for 0xff flood */ 841 usr2 = imx_uart_readl(sport, USR2); 842 if (usr2 & USR2_RDR) 843 imx_uart_check_flood(sport, usr2); 844 845 while ((rx = imx_uart_readl(sport, URXD0)) & URXD_CHARRDY) { 846 unsigned int flg = TTY_NORMAL; 847 sport->port.icount.rx++; 848 849 if (unlikely(rx & URXD_ERR)) { 850 if (rx & URXD_BRK) { 851 sport->port.icount.brk++; 852 if (uart_handle_break(&sport->port)) 853 continue; 854 } 855 else if (rx & URXD_PRERR) 856 sport->port.icount.parity++; 857 else if (rx & URXD_FRMERR) 858 sport->port.icount.frame++; 859 if (rx & URXD_OVRRUN) 860 sport->port.icount.overrun++; 861 862 if (rx & sport->port.ignore_status_mask) 863 continue; 864 865 rx &= (sport->port.read_status_mask | 0xFF); 866 867 if (rx & URXD_BRK) 868 flg = TTY_BREAK; 869 else if (rx & URXD_PRERR) 870 flg = TTY_PARITY; 871 else if (rx & URXD_FRMERR) 872 flg = TTY_FRAME; 873 if (rx & URXD_OVRRUN) 874 flg = TTY_OVERRUN; 875 876 sport->port.sysrq = 0; 877 } else if (uart_handle_sysrq_char(&sport->port, (unsigned char)rx)) { 878 continue; 879 } 880 881 if (sport->port.ignore_status_mask & URXD_DUMMY_READ) 882 continue; 883 884 if (tty_insert_flip_char(port, rx, flg) == 0) 885 sport->port.icount.buf_overrun++; 886 } 887 888 tty_flip_buffer_push(port); 889 890 return IRQ_HANDLED; 891 } 892 893 static irqreturn_t imx_uart_rxint(int irq, void *dev_id) 894 { 895 struct imx_port *sport = dev_id; 896 irqreturn_t ret; 897 898 spin_lock(&sport->port.lock); 899 900 ret = __imx_uart_rxint(irq, dev_id); 901 902 spin_unlock(&sport->port.lock); 903 904 return ret; 905 } 906 907 static void imx_uart_clear_rx_errors(struct imx_port *sport); 908 909 /* 910 * We have a modem side uart, so the meanings of RTS and CTS are inverted. 911 */ 912 static unsigned int imx_uart_get_hwmctrl(struct imx_port *sport) 913 { 914 unsigned int tmp = TIOCM_DSR; 915 unsigned usr1 = imx_uart_readl(sport, USR1); 916 unsigned usr2 = imx_uart_readl(sport, USR2); 917 918 if (usr1 & USR1_RTSS) 919 tmp |= TIOCM_CTS; 920 921 /* in DCE mode DCDIN is always 0 */ 922 if (!(usr2 & USR2_DCDIN)) 923 tmp |= TIOCM_CAR; 924 925 if (sport->dte_mode) 926 if (!(imx_uart_readl(sport, USR2) & USR2_RIIN)) 927 tmp |= TIOCM_RI; 928 929 return tmp; 930 } 931 932 /* 933 * Handle any change of modem status signal since we were last called. 934 */ 935 static void imx_uart_mctrl_check(struct imx_port *sport) 936 { 937 unsigned int status, changed; 938 939 status = imx_uart_get_hwmctrl(sport); 940 changed = status ^ sport->old_status; 941 942 if (changed == 0) 943 return; 944 945 sport->old_status = status; 946 947 if (changed & TIOCM_RI && status & TIOCM_RI) 948 sport->port.icount.rng++; 949 if (changed & TIOCM_DSR) 950 sport->port.icount.dsr++; 951 if (changed & TIOCM_CAR) 952 uart_handle_dcd_change(&sport->port, status & TIOCM_CAR); 953 if (changed & TIOCM_CTS) 954 uart_handle_cts_change(&sport->port, status & TIOCM_CTS); 955 956 wake_up_interruptible(&sport->port.state->port.delta_msr_wait); 957 } 958 959 static irqreturn_t imx_uart_int(int irq, void *dev_id) 960 { 961 struct imx_port *sport = dev_id; 962 unsigned int usr1, usr2, ucr1, ucr2, ucr3, ucr4; 963 irqreturn_t ret = IRQ_NONE; 964 965 spin_lock(&sport->port.lock); 966 967 usr1 = imx_uart_readl(sport, USR1); 968 usr2 = imx_uart_readl(sport, USR2); 969 ucr1 = imx_uart_readl(sport, UCR1); 970 ucr2 = imx_uart_readl(sport, UCR2); 971 ucr3 = imx_uart_readl(sport, UCR3); 972 ucr4 = imx_uart_readl(sport, UCR4); 973 974 /* 975 * Even if a condition is true that can trigger an irq only handle it if 976 * the respective irq source is enabled. This prevents some undesired 977 * actions, for example if a character that sits in the RX FIFO and that 978 * should be fetched via DMA is tried to be fetched using PIO. Or the 979 * receiver is currently off and so reading from URXD0 results in an 980 * exception. So just mask the (raw) status bits for disabled irqs. 981 */ 982 if ((ucr1 & UCR1_RRDYEN) == 0) 983 usr1 &= ~USR1_RRDY; 984 if ((ucr2 & UCR2_ATEN) == 0) 985 usr1 &= ~USR1_AGTIM; 986 if ((ucr1 & UCR1_TRDYEN) == 0) 987 usr1 &= ~USR1_TRDY; 988 if ((ucr4 & UCR4_TCEN) == 0) 989 usr2 &= ~USR2_TXDC; 990 if ((ucr3 & UCR3_DTRDEN) == 0) 991 usr1 &= ~USR1_DTRD; 992 if ((ucr1 & UCR1_RTSDEN) == 0) 993 usr1 &= ~USR1_RTSD; 994 if ((ucr3 & UCR3_AWAKEN) == 0) 995 usr1 &= ~USR1_AWAKE; 996 if ((ucr4 & UCR4_OREN) == 0) 997 usr2 &= ~USR2_ORE; 998 999 if (usr1 & (USR1_RRDY | USR1_AGTIM)) { 1000 imx_uart_writel(sport, USR1_AGTIM, USR1); 1001 1002 __imx_uart_rxint(irq, dev_id); 1003 ret = IRQ_HANDLED; 1004 } 1005 1006 if ((usr1 & USR1_TRDY) || (usr2 & USR2_TXDC)) { 1007 imx_uart_transmit_buffer(sport); 1008 ret = IRQ_HANDLED; 1009 } 1010 1011 if (usr1 & USR1_DTRD) { 1012 imx_uart_writel(sport, USR1_DTRD, USR1); 1013 1014 imx_uart_mctrl_check(sport); 1015 1016 ret = IRQ_HANDLED; 1017 } 1018 1019 if (usr1 & USR1_RTSD) { 1020 __imx_uart_rtsint(irq, dev_id); 1021 ret = IRQ_HANDLED; 1022 } 1023 1024 if (usr1 & USR1_AWAKE) { 1025 imx_uart_writel(sport, USR1_AWAKE, USR1); 1026 ret = IRQ_HANDLED; 1027 } 1028 1029 if (usr2 & USR2_ORE) { 1030 sport->port.icount.overrun++; 1031 imx_uart_writel(sport, USR2_ORE, USR2); 1032 ret = IRQ_HANDLED; 1033 } 1034 1035 spin_unlock(&sport->port.lock); 1036 1037 return ret; 1038 } 1039 1040 /* 1041 * Return TIOCSER_TEMT when transmitter is not busy. 1042 */ 1043 static unsigned int imx_uart_tx_empty(struct uart_port *port) 1044 { 1045 struct imx_port *sport = (struct imx_port *)port; 1046 unsigned int ret; 1047 1048 ret = (imx_uart_readl(sport, USR2) & USR2_TXDC) ? TIOCSER_TEMT : 0; 1049 1050 /* If the TX DMA is working, return 0. */ 1051 if (sport->dma_is_txing) 1052 ret = 0; 1053 1054 return ret; 1055 } 1056 1057 /* called with port.lock taken and irqs off */ 1058 static unsigned int imx_uart_get_mctrl(struct uart_port *port) 1059 { 1060 struct imx_port *sport = (struct imx_port *)port; 1061 unsigned int ret = imx_uart_get_hwmctrl(sport); 1062 1063 mctrl_gpio_get(sport->gpios, &ret); 1064 1065 return ret; 1066 } 1067 1068 /* called with port.lock taken and irqs off */ 1069 static void imx_uart_set_mctrl(struct uart_port *port, unsigned int mctrl) 1070 { 1071 struct imx_port *sport = (struct imx_port *)port; 1072 u32 ucr3, uts; 1073 1074 if (!(port->rs485.flags & SER_RS485_ENABLED)) { 1075 u32 ucr2; 1076 1077 /* 1078 * Turn off autoRTS if RTS is lowered and restore autoRTS 1079 * setting if RTS is raised. 1080 */ 1081 ucr2 = imx_uart_readl(sport, UCR2); 1082 ucr2 &= ~(UCR2_CTS | UCR2_CTSC); 1083 if (mctrl & TIOCM_RTS) { 1084 ucr2 |= UCR2_CTS; 1085 /* 1086 * UCR2_IRTS is unset if and only if the port is 1087 * configured for CRTSCTS, so we use inverted UCR2_IRTS 1088 * to get the state to restore to. 1089 */ 1090 if (!(ucr2 & UCR2_IRTS)) 1091 ucr2 |= UCR2_CTSC; 1092 } 1093 imx_uart_writel(sport, ucr2, UCR2); 1094 } 1095 1096 ucr3 = imx_uart_readl(sport, UCR3) & ~UCR3_DSR; 1097 if (!(mctrl & TIOCM_DTR)) 1098 ucr3 |= UCR3_DSR; 1099 imx_uart_writel(sport, ucr3, UCR3); 1100 1101 uts = imx_uart_readl(sport, imx_uart_uts_reg(sport)) & ~UTS_LOOP; 1102 if (mctrl & TIOCM_LOOP) 1103 uts |= UTS_LOOP; 1104 imx_uart_writel(sport, uts, imx_uart_uts_reg(sport)); 1105 1106 mctrl_gpio_set(sport->gpios, mctrl); 1107 } 1108 1109 /* 1110 * Interrupts always disabled. 1111 */ 1112 static void imx_uart_break_ctl(struct uart_port *port, int break_state) 1113 { 1114 struct imx_port *sport = (struct imx_port *)port; 1115 unsigned long flags; 1116 u32 ucr1; 1117 1118 spin_lock_irqsave(&sport->port.lock, flags); 1119 1120 ucr1 = imx_uart_readl(sport, UCR1) & ~UCR1_SNDBRK; 1121 1122 if (break_state != 0) 1123 ucr1 |= UCR1_SNDBRK; 1124 1125 imx_uart_writel(sport, ucr1, UCR1); 1126 1127 spin_unlock_irqrestore(&sport->port.lock, flags); 1128 } 1129 1130 /* 1131 * This is our per-port timeout handler, for checking the 1132 * modem status signals. 1133 */ 1134 static void imx_uart_timeout(struct timer_list *t) 1135 { 1136 struct imx_port *sport = from_timer(sport, t, timer); 1137 unsigned long flags; 1138 1139 if (sport->port.state) { 1140 spin_lock_irqsave(&sport->port.lock, flags); 1141 imx_uart_mctrl_check(sport); 1142 spin_unlock_irqrestore(&sport->port.lock, flags); 1143 1144 mod_timer(&sport->timer, jiffies + MCTRL_TIMEOUT); 1145 } 1146 } 1147 1148 /* 1149 * There are two kinds of RX DMA interrupts(such as in the MX6Q): 1150 * [1] the RX DMA buffer is full. 1151 * [2] the aging timer expires 1152 * 1153 * Condition [2] is triggered when a character has been sitting in the FIFO 1154 * for at least 8 byte durations. 1155 */ 1156 static void imx_uart_dma_rx_callback(void *data) 1157 { 1158 struct imx_port *sport = data; 1159 struct dma_chan *chan = sport->dma_chan_rx; 1160 struct scatterlist *sgl = &sport->rx_sgl; 1161 struct tty_port *port = &sport->port.state->port; 1162 struct dma_tx_state state; 1163 struct circ_buf *rx_ring = &sport->rx_ring; 1164 enum dma_status status; 1165 unsigned int w_bytes = 0; 1166 unsigned int r_bytes; 1167 unsigned int bd_size; 1168 1169 status = dmaengine_tx_status(chan, sport->rx_cookie, &state); 1170 1171 if (status == DMA_ERROR) { 1172 spin_lock(&sport->port.lock); 1173 imx_uart_clear_rx_errors(sport); 1174 spin_unlock(&sport->port.lock); 1175 return; 1176 } 1177 1178 /* 1179 * The state-residue variable represents the empty space 1180 * relative to the entire buffer. Taking this in consideration 1181 * the head is always calculated base on the buffer total 1182 * length - DMA transaction residue. The UART script from the 1183 * SDMA firmware will jump to the next buffer descriptor, 1184 * once a DMA transaction if finalized (IMX53 RM - A.4.1.2.4). 1185 * Taking this in consideration the tail is always at the 1186 * beginning of the buffer descriptor that contains the head. 1187 */ 1188 1189 /* Calculate the head */ 1190 rx_ring->head = sg_dma_len(sgl) - state.residue; 1191 1192 /* Calculate the tail. */ 1193 bd_size = sg_dma_len(sgl) / sport->rx_periods; 1194 rx_ring->tail = ((rx_ring->head-1) / bd_size) * bd_size; 1195 1196 if (rx_ring->head <= sg_dma_len(sgl) && 1197 rx_ring->head > rx_ring->tail) { 1198 1199 /* Move data from tail to head */ 1200 r_bytes = rx_ring->head - rx_ring->tail; 1201 1202 /* If we received something, check for 0xff flood */ 1203 spin_lock(&sport->port.lock); 1204 imx_uart_check_flood(sport, imx_uart_readl(sport, USR2)); 1205 spin_unlock(&sport->port.lock); 1206 1207 if (!(sport->port.ignore_status_mask & URXD_DUMMY_READ)) { 1208 1209 /* CPU claims ownership of RX DMA buffer */ 1210 dma_sync_sg_for_cpu(sport->port.dev, sgl, 1, 1211 DMA_FROM_DEVICE); 1212 1213 w_bytes = tty_insert_flip_string(port, 1214 sport->rx_buf + rx_ring->tail, r_bytes); 1215 1216 /* UART retrieves ownership of RX DMA buffer */ 1217 dma_sync_sg_for_device(sport->port.dev, sgl, 1, 1218 DMA_FROM_DEVICE); 1219 1220 if (w_bytes != r_bytes) 1221 sport->port.icount.buf_overrun++; 1222 1223 sport->port.icount.rx += w_bytes; 1224 } 1225 } else { 1226 WARN_ON(rx_ring->head > sg_dma_len(sgl)); 1227 WARN_ON(rx_ring->head <= rx_ring->tail); 1228 } 1229 1230 if (w_bytes) { 1231 tty_flip_buffer_push(port); 1232 dev_dbg(sport->port.dev, "We get %d bytes.\n", w_bytes); 1233 } 1234 } 1235 1236 static int imx_uart_start_rx_dma(struct imx_port *sport) 1237 { 1238 struct scatterlist *sgl = &sport->rx_sgl; 1239 struct dma_chan *chan = sport->dma_chan_rx; 1240 struct device *dev = sport->port.dev; 1241 struct dma_async_tx_descriptor *desc; 1242 int ret; 1243 1244 sport->rx_ring.head = 0; 1245 sport->rx_ring.tail = 0; 1246 1247 sg_init_one(sgl, sport->rx_buf, sport->rx_buf_size); 1248 ret = dma_map_sg(dev, sgl, 1, DMA_FROM_DEVICE); 1249 if (ret == 0) { 1250 dev_err(dev, "DMA mapping error for RX.\n"); 1251 return -EINVAL; 1252 } 1253 1254 desc = dmaengine_prep_dma_cyclic(chan, sg_dma_address(sgl), 1255 sg_dma_len(sgl), sg_dma_len(sgl) / sport->rx_periods, 1256 DMA_DEV_TO_MEM, DMA_PREP_INTERRUPT); 1257 1258 if (!desc) { 1259 dma_unmap_sg(dev, sgl, 1, DMA_FROM_DEVICE); 1260 dev_err(dev, "We cannot prepare for the RX slave dma!\n"); 1261 return -EINVAL; 1262 } 1263 desc->callback = imx_uart_dma_rx_callback; 1264 desc->callback_param = sport; 1265 1266 dev_dbg(dev, "RX: prepare for the DMA.\n"); 1267 sport->dma_is_rxing = 1; 1268 sport->rx_cookie = dmaengine_submit(desc); 1269 dma_async_issue_pending(chan); 1270 return 0; 1271 } 1272 1273 static void imx_uart_clear_rx_errors(struct imx_port *sport) 1274 { 1275 struct tty_port *port = &sport->port.state->port; 1276 u32 usr1, usr2; 1277 1278 usr1 = imx_uart_readl(sport, USR1); 1279 usr2 = imx_uart_readl(sport, USR2); 1280 1281 if (usr2 & USR2_BRCD) { 1282 sport->port.icount.brk++; 1283 imx_uart_writel(sport, USR2_BRCD, USR2); 1284 uart_handle_break(&sport->port); 1285 if (tty_insert_flip_char(port, 0, TTY_BREAK) == 0) 1286 sport->port.icount.buf_overrun++; 1287 tty_flip_buffer_push(port); 1288 } else { 1289 if (usr1 & USR1_FRAMERR) { 1290 sport->port.icount.frame++; 1291 imx_uart_writel(sport, USR1_FRAMERR, USR1); 1292 } else if (usr1 & USR1_PARITYERR) { 1293 sport->port.icount.parity++; 1294 imx_uart_writel(sport, USR1_PARITYERR, USR1); 1295 } 1296 } 1297 1298 if (usr2 & USR2_ORE) { 1299 sport->port.icount.overrun++; 1300 imx_uart_writel(sport, USR2_ORE, USR2); 1301 } 1302 1303 sport->idle_counter = 0; 1304 1305 } 1306 1307 #define TXTL_DEFAULT 2 /* reset default */ 1308 #define RXTL_DEFAULT 8 /* 8 characters or aging timer */ 1309 #define TXTL_DMA 8 /* DMA burst setting */ 1310 #define RXTL_DMA 9 /* DMA burst setting */ 1311 1312 static void imx_uart_setup_ufcr(struct imx_port *sport, 1313 unsigned char txwl, unsigned char rxwl) 1314 { 1315 unsigned int val; 1316 1317 /* set receiver / transmitter trigger level */ 1318 val = imx_uart_readl(sport, UFCR) & (UFCR_RFDIV | UFCR_DCEDTE); 1319 val |= txwl << UFCR_TXTL_SHF | rxwl; 1320 imx_uart_writel(sport, val, UFCR); 1321 } 1322 1323 static void imx_uart_dma_exit(struct imx_port *sport) 1324 { 1325 if (sport->dma_chan_rx) { 1326 dmaengine_terminate_sync(sport->dma_chan_rx); 1327 dma_release_channel(sport->dma_chan_rx); 1328 sport->dma_chan_rx = NULL; 1329 sport->rx_cookie = -EINVAL; 1330 kfree(sport->rx_buf); 1331 sport->rx_buf = NULL; 1332 } 1333 1334 if (sport->dma_chan_tx) { 1335 dmaengine_terminate_sync(sport->dma_chan_tx); 1336 dma_release_channel(sport->dma_chan_tx); 1337 sport->dma_chan_tx = NULL; 1338 } 1339 } 1340 1341 static int imx_uart_dma_init(struct imx_port *sport) 1342 { 1343 struct dma_slave_config slave_config = {}; 1344 struct device *dev = sport->port.dev; 1345 int ret; 1346 1347 /* Prepare for RX : */ 1348 sport->dma_chan_rx = dma_request_slave_channel(dev, "rx"); 1349 if (!sport->dma_chan_rx) { 1350 dev_dbg(dev, "cannot get the DMA channel.\n"); 1351 ret = -EINVAL; 1352 goto err; 1353 } 1354 1355 slave_config.direction = DMA_DEV_TO_MEM; 1356 slave_config.src_addr = sport->port.mapbase + URXD0; 1357 slave_config.src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE; 1358 /* one byte less than the watermark level to enable the aging timer */ 1359 slave_config.src_maxburst = RXTL_DMA - 1; 1360 ret = dmaengine_slave_config(sport->dma_chan_rx, &slave_config); 1361 if (ret) { 1362 dev_err(dev, "error in RX dma configuration.\n"); 1363 goto err; 1364 } 1365 1366 sport->rx_buf_size = sport->rx_period_length * sport->rx_periods; 1367 sport->rx_buf = kzalloc(sport->rx_buf_size, GFP_KERNEL); 1368 if (!sport->rx_buf) { 1369 ret = -ENOMEM; 1370 goto err; 1371 } 1372 sport->rx_ring.buf = sport->rx_buf; 1373 1374 /* Prepare for TX : */ 1375 sport->dma_chan_tx = dma_request_slave_channel(dev, "tx"); 1376 if (!sport->dma_chan_tx) { 1377 dev_err(dev, "cannot get the TX DMA channel!\n"); 1378 ret = -EINVAL; 1379 goto err; 1380 } 1381 1382 slave_config.direction = DMA_MEM_TO_DEV; 1383 slave_config.dst_addr = sport->port.mapbase + URTX0; 1384 slave_config.dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE; 1385 slave_config.dst_maxburst = TXTL_DMA; 1386 ret = dmaengine_slave_config(sport->dma_chan_tx, &slave_config); 1387 if (ret) { 1388 dev_err(dev, "error in TX dma configuration."); 1389 goto err; 1390 } 1391 1392 return 0; 1393 err: 1394 imx_uart_dma_exit(sport); 1395 return ret; 1396 } 1397 1398 static void imx_uart_enable_dma(struct imx_port *sport) 1399 { 1400 u32 ucr1; 1401 1402 imx_uart_setup_ufcr(sport, TXTL_DMA, RXTL_DMA); 1403 1404 /* set UCR1 */ 1405 ucr1 = imx_uart_readl(sport, UCR1); 1406 ucr1 |= UCR1_RXDMAEN | UCR1_TXDMAEN | UCR1_ATDMAEN; 1407 imx_uart_writel(sport, ucr1, UCR1); 1408 1409 sport->dma_is_enabled = 1; 1410 } 1411 1412 static void imx_uart_disable_dma(struct imx_port *sport) 1413 { 1414 u32 ucr1; 1415 1416 /* clear UCR1 */ 1417 ucr1 = imx_uart_readl(sport, UCR1); 1418 ucr1 &= ~(UCR1_RXDMAEN | UCR1_TXDMAEN | UCR1_ATDMAEN); 1419 imx_uart_writel(sport, ucr1, UCR1); 1420 1421 imx_uart_setup_ufcr(sport, TXTL_DEFAULT, RXTL_DEFAULT); 1422 1423 sport->dma_is_enabled = 0; 1424 } 1425 1426 /* half the RX buffer size */ 1427 #define CTSTL 16 1428 1429 static int imx_uart_startup(struct uart_port *port) 1430 { 1431 struct imx_port *sport = (struct imx_port *)port; 1432 int retval; 1433 unsigned long flags; 1434 int dma_is_inited = 0; 1435 u32 ucr1, ucr2, ucr3, ucr4; 1436 1437 retval = clk_prepare_enable(sport->clk_per); 1438 if (retval) 1439 return retval; 1440 retval = clk_prepare_enable(sport->clk_ipg); 1441 if (retval) { 1442 clk_disable_unprepare(sport->clk_per); 1443 return retval; 1444 } 1445 1446 imx_uart_setup_ufcr(sport, TXTL_DEFAULT, RXTL_DEFAULT); 1447 1448 /* disable the DREN bit (Data Ready interrupt enable) before 1449 * requesting IRQs 1450 */ 1451 ucr4 = imx_uart_readl(sport, UCR4); 1452 1453 /* set the trigger level for CTS */ 1454 ucr4 &= ~(UCR4_CTSTL_MASK << UCR4_CTSTL_SHF); 1455 ucr4 |= CTSTL << UCR4_CTSTL_SHF; 1456 1457 imx_uart_writel(sport, ucr4 & ~UCR4_DREN, UCR4); 1458 1459 /* Can we enable the DMA support? */ 1460 if (!uart_console(port) && imx_uart_dma_init(sport) == 0) 1461 dma_is_inited = 1; 1462 1463 spin_lock_irqsave(&sport->port.lock, flags); 1464 1465 /* Reset fifo's and state machines */ 1466 imx_uart_soft_reset(sport); 1467 1468 /* 1469 * Finally, clear and enable interrupts 1470 */ 1471 imx_uart_writel(sport, USR1_RTSD | USR1_DTRD, USR1); 1472 imx_uart_writel(sport, USR2_ORE, USR2); 1473 1474 ucr1 = imx_uart_readl(sport, UCR1) & ~UCR1_RRDYEN; 1475 ucr1 |= UCR1_UARTEN; 1476 if (sport->have_rtscts) 1477 ucr1 |= UCR1_RTSDEN; 1478 1479 imx_uart_writel(sport, ucr1, UCR1); 1480 1481 ucr4 = imx_uart_readl(sport, UCR4) & ~(UCR4_OREN | UCR4_INVR); 1482 if (!dma_is_inited) 1483 ucr4 |= UCR4_OREN; 1484 if (sport->inverted_rx) 1485 ucr4 |= UCR4_INVR; 1486 imx_uart_writel(sport, ucr4, UCR4); 1487 1488 ucr3 = imx_uart_readl(sport, UCR3) & ~UCR3_INVT; 1489 /* 1490 * configure tx polarity before enabling tx 1491 */ 1492 if (sport->inverted_tx) 1493 ucr3 |= UCR3_INVT; 1494 1495 if (!imx_uart_is_imx1(sport)) { 1496 ucr3 |= UCR3_DTRDEN | UCR3_RI | UCR3_DCD; 1497 1498 if (sport->dte_mode) 1499 /* disable broken interrupts */ 1500 ucr3 &= ~(UCR3_RI | UCR3_DCD); 1501 } 1502 imx_uart_writel(sport, ucr3, UCR3); 1503 1504 ucr2 = imx_uart_readl(sport, UCR2) & ~UCR2_ATEN; 1505 ucr2 |= (UCR2_RXEN | UCR2_TXEN); 1506 if (!sport->have_rtscts) 1507 ucr2 |= UCR2_IRTS; 1508 /* 1509 * make sure the edge sensitive RTS-irq is disabled, 1510 * we're using RTSD instead. 1511 */ 1512 if (!imx_uart_is_imx1(sport)) 1513 ucr2 &= ~UCR2_RTSEN; 1514 imx_uart_writel(sport, ucr2, UCR2); 1515 1516 /* 1517 * Enable modem status interrupts 1518 */ 1519 imx_uart_enable_ms(&sport->port); 1520 1521 if (dma_is_inited) { 1522 imx_uart_enable_dma(sport); 1523 imx_uart_start_rx_dma(sport); 1524 } else { 1525 ucr1 = imx_uart_readl(sport, UCR1); 1526 ucr1 |= UCR1_RRDYEN; 1527 imx_uart_writel(sport, ucr1, UCR1); 1528 1529 ucr2 = imx_uart_readl(sport, UCR2); 1530 ucr2 |= UCR2_ATEN; 1531 imx_uart_writel(sport, ucr2, UCR2); 1532 } 1533 1534 imx_uart_disable_loopback_rs485(sport); 1535 1536 spin_unlock_irqrestore(&sport->port.lock, flags); 1537 1538 return 0; 1539 } 1540 1541 static void imx_uart_shutdown(struct uart_port *port) 1542 { 1543 struct imx_port *sport = (struct imx_port *)port; 1544 unsigned long flags; 1545 u32 ucr1, ucr2, ucr4, uts; 1546 1547 if (sport->dma_is_enabled) { 1548 dmaengine_terminate_sync(sport->dma_chan_tx); 1549 if (sport->dma_is_txing) { 1550 dma_unmap_sg(sport->port.dev, &sport->tx_sgl[0], 1551 sport->dma_tx_nents, DMA_TO_DEVICE); 1552 sport->dma_is_txing = 0; 1553 } 1554 dmaengine_terminate_sync(sport->dma_chan_rx); 1555 if (sport->dma_is_rxing) { 1556 dma_unmap_sg(sport->port.dev, &sport->rx_sgl, 1557 1, DMA_FROM_DEVICE); 1558 sport->dma_is_rxing = 0; 1559 } 1560 1561 spin_lock_irqsave(&sport->port.lock, flags); 1562 imx_uart_stop_tx(port); 1563 imx_uart_stop_rx(port); 1564 imx_uart_disable_dma(sport); 1565 spin_unlock_irqrestore(&sport->port.lock, flags); 1566 imx_uart_dma_exit(sport); 1567 } 1568 1569 mctrl_gpio_disable_ms(sport->gpios); 1570 1571 spin_lock_irqsave(&sport->port.lock, flags); 1572 ucr2 = imx_uart_readl(sport, UCR2); 1573 ucr2 &= ~(UCR2_TXEN | UCR2_ATEN); 1574 imx_uart_writel(sport, ucr2, UCR2); 1575 spin_unlock_irqrestore(&sport->port.lock, flags); 1576 1577 /* 1578 * Stop our timer. 1579 */ 1580 del_timer_sync(&sport->timer); 1581 1582 /* 1583 * Disable all interrupts, port and break condition. 1584 */ 1585 1586 spin_lock_irqsave(&sport->port.lock, flags); 1587 1588 ucr1 = imx_uart_readl(sport, UCR1); 1589 ucr1 &= ~(UCR1_TRDYEN | UCR1_RRDYEN | UCR1_RTSDEN | UCR1_RXDMAEN | 1590 UCR1_ATDMAEN | UCR1_SNDBRK); 1591 /* See SER_RS485_ENABLED/UTS_LOOP comment in imx_uart_probe() */ 1592 if (port->rs485.flags & SER_RS485_ENABLED && 1593 port->rs485.flags & SER_RS485_RTS_ON_SEND && 1594 sport->have_rtscts && !sport->have_rtsgpio) { 1595 uts = imx_uart_readl(sport, imx_uart_uts_reg(sport)); 1596 uts |= UTS_LOOP; 1597 imx_uart_writel(sport, uts, imx_uart_uts_reg(sport)); 1598 ucr1 |= UCR1_UARTEN; 1599 } else { 1600 ucr1 &= ~UCR1_UARTEN; 1601 } 1602 imx_uart_writel(sport, ucr1, UCR1); 1603 1604 ucr4 = imx_uart_readl(sport, UCR4); 1605 ucr4 &= ~UCR4_TCEN; 1606 imx_uart_writel(sport, ucr4, UCR4); 1607 1608 spin_unlock_irqrestore(&sport->port.lock, flags); 1609 1610 clk_disable_unprepare(sport->clk_per); 1611 clk_disable_unprepare(sport->clk_ipg); 1612 } 1613 1614 /* called with port.lock taken and irqs off */ 1615 static void imx_uart_flush_buffer(struct uart_port *port) 1616 { 1617 struct imx_port *sport = (struct imx_port *)port; 1618 struct scatterlist *sgl = &sport->tx_sgl[0]; 1619 1620 if (!sport->dma_chan_tx) 1621 return; 1622 1623 sport->tx_bytes = 0; 1624 dmaengine_terminate_all(sport->dma_chan_tx); 1625 if (sport->dma_is_txing) { 1626 u32 ucr1; 1627 1628 dma_unmap_sg(sport->port.dev, sgl, sport->dma_tx_nents, 1629 DMA_TO_DEVICE); 1630 ucr1 = imx_uart_readl(sport, UCR1); 1631 ucr1 &= ~UCR1_TXDMAEN; 1632 imx_uart_writel(sport, ucr1, UCR1); 1633 sport->dma_is_txing = 0; 1634 } 1635 1636 imx_uart_soft_reset(sport); 1637 1638 } 1639 1640 static void 1641 imx_uart_set_termios(struct uart_port *port, struct ktermios *termios, 1642 const struct ktermios *old) 1643 { 1644 struct imx_port *sport = (struct imx_port *)port; 1645 unsigned long flags; 1646 u32 ucr2, old_ucr2, ufcr; 1647 unsigned int baud, quot; 1648 unsigned int old_csize = old ? old->c_cflag & CSIZE : CS8; 1649 unsigned long div; 1650 unsigned long num, denom, old_ubir, old_ubmr; 1651 uint64_t tdiv64; 1652 1653 /* 1654 * We only support CS7 and CS8. 1655 */ 1656 while ((termios->c_cflag & CSIZE) != CS7 && 1657 (termios->c_cflag & CSIZE) != CS8) { 1658 termios->c_cflag &= ~CSIZE; 1659 termios->c_cflag |= old_csize; 1660 old_csize = CS8; 1661 } 1662 1663 del_timer_sync(&sport->timer); 1664 1665 /* 1666 * Ask the core to calculate the divisor for us. 1667 */ 1668 baud = uart_get_baud_rate(port, termios, old, 50, port->uartclk / 16); 1669 quot = uart_get_divisor(port, baud); 1670 1671 spin_lock_irqsave(&sport->port.lock, flags); 1672 1673 /* 1674 * Read current UCR2 and save it for future use, then clear all the bits 1675 * except those we will or may need to preserve. 1676 */ 1677 old_ucr2 = imx_uart_readl(sport, UCR2); 1678 ucr2 = old_ucr2 & (UCR2_TXEN | UCR2_RXEN | UCR2_ATEN | UCR2_CTS); 1679 1680 ucr2 |= UCR2_SRST | UCR2_IRTS; 1681 if ((termios->c_cflag & CSIZE) == CS8) 1682 ucr2 |= UCR2_WS; 1683 1684 if (!sport->have_rtscts) 1685 termios->c_cflag &= ~CRTSCTS; 1686 1687 if (port->rs485.flags & SER_RS485_ENABLED) { 1688 /* 1689 * RTS is mandatory for rs485 operation, so keep 1690 * it under manual control and keep transmitter 1691 * disabled. 1692 */ 1693 if (port->rs485.flags & SER_RS485_RTS_AFTER_SEND) 1694 imx_uart_rts_active(sport, &ucr2); 1695 else 1696 imx_uart_rts_inactive(sport, &ucr2); 1697 1698 } else if (termios->c_cflag & CRTSCTS) { 1699 /* 1700 * Only let receiver control RTS output if we were not requested 1701 * to have RTS inactive (which then should take precedence). 1702 */ 1703 if (ucr2 & UCR2_CTS) 1704 ucr2 |= UCR2_CTSC; 1705 } 1706 1707 if (termios->c_cflag & CRTSCTS) 1708 ucr2 &= ~UCR2_IRTS; 1709 if (termios->c_cflag & CSTOPB) 1710 ucr2 |= UCR2_STPB; 1711 if (termios->c_cflag & PARENB) { 1712 ucr2 |= UCR2_PREN; 1713 if (termios->c_cflag & PARODD) 1714 ucr2 |= UCR2_PROE; 1715 } 1716 1717 sport->port.read_status_mask = 0; 1718 if (termios->c_iflag & INPCK) 1719 sport->port.read_status_mask |= (URXD_FRMERR | URXD_PRERR); 1720 if (termios->c_iflag & (BRKINT | PARMRK)) 1721 sport->port.read_status_mask |= URXD_BRK; 1722 1723 /* 1724 * Characters to ignore 1725 */ 1726 sport->port.ignore_status_mask = 0; 1727 if (termios->c_iflag & IGNPAR) 1728 sport->port.ignore_status_mask |= URXD_PRERR | URXD_FRMERR; 1729 if (termios->c_iflag & IGNBRK) { 1730 sport->port.ignore_status_mask |= URXD_BRK; 1731 /* 1732 * If we're ignoring parity and break indicators, 1733 * ignore overruns too (for real raw support). 1734 */ 1735 if (termios->c_iflag & IGNPAR) 1736 sport->port.ignore_status_mask |= URXD_OVRRUN; 1737 } 1738 1739 if ((termios->c_cflag & CREAD) == 0) 1740 sport->port.ignore_status_mask |= URXD_DUMMY_READ; 1741 1742 /* 1743 * Update the per-port timeout. 1744 */ 1745 uart_update_timeout(port, termios->c_cflag, baud); 1746 1747 /* custom-baudrate handling */ 1748 div = sport->port.uartclk / (baud * 16); 1749 if (baud == 38400 && quot != div) 1750 baud = sport->port.uartclk / (quot * 16); 1751 1752 div = sport->port.uartclk / (baud * 16); 1753 if (div > 7) 1754 div = 7; 1755 if (!div) 1756 div = 1; 1757 1758 rational_best_approximation(16 * div * baud, sport->port.uartclk, 1759 1 << 16, 1 << 16, &num, &denom); 1760 1761 tdiv64 = sport->port.uartclk; 1762 tdiv64 *= num; 1763 do_div(tdiv64, denom * 16 * div); 1764 tty_termios_encode_baud_rate(termios, 1765 (speed_t)tdiv64, (speed_t)tdiv64); 1766 1767 num -= 1; 1768 denom -= 1; 1769 1770 ufcr = imx_uart_readl(sport, UFCR); 1771 ufcr = (ufcr & (~UFCR_RFDIV)) | UFCR_RFDIV_REG(div); 1772 imx_uart_writel(sport, ufcr, UFCR); 1773 1774 /* 1775 * Two registers below should always be written both and in this 1776 * particular order. One consequence is that we need to check if any of 1777 * them changes and then update both. We do need the check for change 1778 * as even writing the same values seem to "restart" 1779 * transmission/receiving logic in the hardware, that leads to data 1780 * breakage even when rate doesn't in fact change. E.g., user switches 1781 * RTS/CTS handshake and suddenly gets broken bytes. 1782 */ 1783 old_ubir = imx_uart_readl(sport, UBIR); 1784 old_ubmr = imx_uart_readl(sport, UBMR); 1785 if (old_ubir != num || old_ubmr != denom) { 1786 imx_uart_writel(sport, num, UBIR); 1787 imx_uart_writel(sport, denom, UBMR); 1788 } 1789 1790 if (!imx_uart_is_imx1(sport)) 1791 imx_uart_writel(sport, sport->port.uartclk / div / 1000, 1792 IMX21_ONEMS); 1793 1794 imx_uart_writel(sport, ucr2, UCR2); 1795 1796 if (UART_ENABLE_MS(&sport->port, termios->c_cflag)) 1797 imx_uart_enable_ms(&sport->port); 1798 1799 spin_unlock_irqrestore(&sport->port.lock, flags); 1800 } 1801 1802 static const char *imx_uart_type(struct uart_port *port) 1803 { 1804 return port->type == PORT_IMX ? "IMX" : NULL; 1805 } 1806 1807 /* 1808 * Configure/autoconfigure the port. 1809 */ 1810 static void imx_uart_config_port(struct uart_port *port, int flags) 1811 { 1812 if (flags & UART_CONFIG_TYPE) 1813 port->type = PORT_IMX; 1814 } 1815 1816 /* 1817 * Verify the new serial_struct (for TIOCSSERIAL). 1818 * The only change we allow are to the flags and type, and 1819 * even then only between PORT_IMX and PORT_UNKNOWN 1820 */ 1821 static int 1822 imx_uart_verify_port(struct uart_port *port, struct serial_struct *ser) 1823 { 1824 int ret = 0; 1825 1826 if (ser->type != PORT_UNKNOWN && ser->type != PORT_IMX) 1827 ret = -EINVAL; 1828 if (port->irq != ser->irq) 1829 ret = -EINVAL; 1830 if (ser->io_type != UPIO_MEM) 1831 ret = -EINVAL; 1832 if (port->uartclk / 16 != ser->baud_base) 1833 ret = -EINVAL; 1834 if (port->mapbase != (unsigned long)ser->iomem_base) 1835 ret = -EINVAL; 1836 if (port->iobase != ser->port) 1837 ret = -EINVAL; 1838 if (ser->hub6 != 0) 1839 ret = -EINVAL; 1840 return ret; 1841 } 1842 1843 #if defined(CONFIG_CONSOLE_POLL) 1844 1845 static int imx_uart_poll_init(struct uart_port *port) 1846 { 1847 struct imx_port *sport = (struct imx_port *)port; 1848 unsigned long flags; 1849 u32 ucr1, ucr2; 1850 int retval; 1851 1852 retval = clk_prepare_enable(sport->clk_ipg); 1853 if (retval) 1854 return retval; 1855 retval = clk_prepare_enable(sport->clk_per); 1856 if (retval) 1857 clk_disable_unprepare(sport->clk_ipg); 1858 1859 imx_uart_setup_ufcr(sport, TXTL_DEFAULT, RXTL_DEFAULT); 1860 1861 spin_lock_irqsave(&sport->port.lock, flags); 1862 1863 /* 1864 * Be careful about the order of enabling bits here. First enable the 1865 * receiver (UARTEN + RXEN) and only then the corresponding irqs. 1866 * This prevents that a character that already sits in the RX fifo is 1867 * triggering an irq but the try to fetch it from there results in an 1868 * exception because UARTEN or RXEN is still off. 1869 */ 1870 ucr1 = imx_uart_readl(sport, UCR1); 1871 ucr2 = imx_uart_readl(sport, UCR2); 1872 1873 if (imx_uart_is_imx1(sport)) 1874 ucr1 |= IMX1_UCR1_UARTCLKEN; 1875 1876 ucr1 |= UCR1_UARTEN; 1877 ucr1 &= ~(UCR1_TRDYEN | UCR1_RTSDEN | UCR1_RRDYEN); 1878 1879 ucr2 |= UCR2_RXEN | UCR2_TXEN; 1880 ucr2 &= ~UCR2_ATEN; 1881 1882 imx_uart_writel(sport, ucr1, UCR1); 1883 imx_uart_writel(sport, ucr2, UCR2); 1884 1885 /* now enable irqs */ 1886 imx_uart_writel(sport, ucr1 | UCR1_RRDYEN, UCR1); 1887 imx_uart_writel(sport, ucr2 | UCR2_ATEN, UCR2); 1888 1889 spin_unlock_irqrestore(&sport->port.lock, flags); 1890 1891 return 0; 1892 } 1893 1894 static int imx_uart_poll_get_char(struct uart_port *port) 1895 { 1896 struct imx_port *sport = (struct imx_port *)port; 1897 if (!(imx_uart_readl(sport, USR2) & USR2_RDR)) 1898 return NO_POLL_CHAR; 1899 1900 return imx_uart_readl(sport, URXD0) & URXD_RX_DATA; 1901 } 1902 1903 static void imx_uart_poll_put_char(struct uart_port *port, unsigned char c) 1904 { 1905 struct imx_port *sport = (struct imx_port *)port; 1906 unsigned int status; 1907 1908 /* drain */ 1909 do { 1910 status = imx_uart_readl(sport, USR1); 1911 } while (~status & USR1_TRDY); 1912 1913 /* write */ 1914 imx_uart_writel(sport, c, URTX0); 1915 1916 /* flush */ 1917 do { 1918 status = imx_uart_readl(sport, USR2); 1919 } while (~status & USR2_TXDC); 1920 } 1921 #endif 1922 1923 /* called with port.lock taken and irqs off or from .probe without locking */ 1924 static int imx_uart_rs485_config(struct uart_port *port, struct ktermios *termios, 1925 struct serial_rs485 *rs485conf) 1926 { 1927 struct imx_port *sport = (struct imx_port *)port; 1928 u32 ucr2; 1929 1930 if (rs485conf->flags & SER_RS485_ENABLED) { 1931 /* Enable receiver if low-active RTS signal is requested */ 1932 if (sport->have_rtscts && !sport->have_rtsgpio && 1933 !(rs485conf->flags & SER_RS485_RTS_ON_SEND)) 1934 rs485conf->flags |= SER_RS485_RX_DURING_TX; 1935 1936 /* disable transmitter */ 1937 ucr2 = imx_uart_readl(sport, UCR2); 1938 if (rs485conf->flags & SER_RS485_RTS_AFTER_SEND) 1939 imx_uart_rts_active(sport, &ucr2); 1940 else 1941 imx_uart_rts_inactive(sport, &ucr2); 1942 imx_uart_writel(sport, ucr2, UCR2); 1943 } 1944 1945 /* Make sure Rx is enabled in case Tx is active with Rx disabled */ 1946 if (!(rs485conf->flags & SER_RS485_ENABLED) || 1947 rs485conf->flags & SER_RS485_RX_DURING_TX) 1948 imx_uart_start_rx(port); 1949 1950 return 0; 1951 } 1952 1953 static const struct uart_ops imx_uart_pops = { 1954 .tx_empty = imx_uart_tx_empty, 1955 .set_mctrl = imx_uart_set_mctrl, 1956 .get_mctrl = imx_uart_get_mctrl, 1957 .stop_tx = imx_uart_stop_tx, 1958 .start_tx = imx_uart_start_tx, 1959 .stop_rx = imx_uart_stop_rx, 1960 .enable_ms = imx_uart_enable_ms, 1961 .break_ctl = imx_uart_break_ctl, 1962 .startup = imx_uart_startup, 1963 .shutdown = imx_uart_shutdown, 1964 .flush_buffer = imx_uart_flush_buffer, 1965 .set_termios = imx_uart_set_termios, 1966 .type = imx_uart_type, 1967 .config_port = imx_uart_config_port, 1968 .verify_port = imx_uart_verify_port, 1969 #if defined(CONFIG_CONSOLE_POLL) 1970 .poll_init = imx_uart_poll_init, 1971 .poll_get_char = imx_uart_poll_get_char, 1972 .poll_put_char = imx_uart_poll_put_char, 1973 #endif 1974 }; 1975 1976 static struct imx_port *imx_uart_ports[UART_NR]; 1977 1978 #if IS_ENABLED(CONFIG_SERIAL_IMX_CONSOLE) 1979 static void imx_uart_console_putchar(struct uart_port *port, unsigned char ch) 1980 { 1981 struct imx_port *sport = (struct imx_port *)port; 1982 1983 while (imx_uart_readl(sport, imx_uart_uts_reg(sport)) & UTS_TXFULL) 1984 barrier(); 1985 1986 imx_uart_writel(sport, ch, URTX0); 1987 } 1988 1989 /* 1990 * Interrupts are disabled on entering 1991 */ 1992 static void 1993 imx_uart_console_write(struct console *co, const char *s, unsigned int count) 1994 { 1995 struct imx_port *sport = imx_uart_ports[co->index]; 1996 struct imx_port_ucrs old_ucr; 1997 unsigned long flags; 1998 unsigned int ucr1; 1999 int locked = 1; 2000 2001 if (sport->port.sysrq) 2002 locked = 0; 2003 else if (oops_in_progress) 2004 locked = spin_trylock_irqsave(&sport->port.lock, flags); 2005 else 2006 spin_lock_irqsave(&sport->port.lock, flags); 2007 2008 /* 2009 * First, save UCR1/2/3 and then disable interrupts 2010 */ 2011 imx_uart_ucrs_save(sport, &old_ucr); 2012 ucr1 = old_ucr.ucr1; 2013 2014 if (imx_uart_is_imx1(sport)) 2015 ucr1 |= IMX1_UCR1_UARTCLKEN; 2016 ucr1 |= UCR1_UARTEN; 2017 ucr1 &= ~(UCR1_TRDYEN | UCR1_RRDYEN | UCR1_RTSDEN); 2018 2019 imx_uart_writel(sport, ucr1, UCR1); 2020 2021 imx_uart_writel(sport, old_ucr.ucr2 | UCR2_TXEN, UCR2); 2022 2023 uart_console_write(&sport->port, s, count, imx_uart_console_putchar); 2024 2025 /* 2026 * Finally, wait for transmitter to become empty 2027 * and restore UCR1/2/3 2028 */ 2029 while (!(imx_uart_readl(sport, USR2) & USR2_TXDC)); 2030 2031 imx_uart_ucrs_restore(sport, &old_ucr); 2032 2033 if (locked) 2034 spin_unlock_irqrestore(&sport->port.lock, flags); 2035 } 2036 2037 /* 2038 * If the port was already initialised (eg, by a boot loader), 2039 * try to determine the current setup. 2040 */ 2041 static void 2042 imx_uart_console_get_options(struct imx_port *sport, int *baud, 2043 int *parity, int *bits) 2044 { 2045 2046 if (imx_uart_readl(sport, UCR1) & UCR1_UARTEN) { 2047 /* ok, the port was enabled */ 2048 unsigned int ucr2, ubir, ubmr, uartclk; 2049 unsigned int baud_raw; 2050 unsigned int ucfr_rfdiv; 2051 2052 ucr2 = imx_uart_readl(sport, UCR2); 2053 2054 *parity = 'n'; 2055 if (ucr2 & UCR2_PREN) { 2056 if (ucr2 & UCR2_PROE) 2057 *parity = 'o'; 2058 else 2059 *parity = 'e'; 2060 } 2061 2062 if (ucr2 & UCR2_WS) 2063 *bits = 8; 2064 else 2065 *bits = 7; 2066 2067 ubir = imx_uart_readl(sport, UBIR) & 0xffff; 2068 ubmr = imx_uart_readl(sport, UBMR) & 0xffff; 2069 2070 ucfr_rfdiv = (imx_uart_readl(sport, UFCR) & UFCR_RFDIV) >> 7; 2071 if (ucfr_rfdiv == 6) 2072 ucfr_rfdiv = 7; 2073 else 2074 ucfr_rfdiv = 6 - ucfr_rfdiv; 2075 2076 uartclk = clk_get_rate(sport->clk_per); 2077 uartclk /= ucfr_rfdiv; 2078 2079 { /* 2080 * The next code provides exact computation of 2081 * baud_raw = round(((uartclk/16) * (ubir + 1)) / (ubmr + 1)) 2082 * without need of float support or long long division, 2083 * which would be required to prevent 32bit arithmetic overflow 2084 */ 2085 unsigned int mul = ubir + 1; 2086 unsigned int div = 16 * (ubmr + 1); 2087 unsigned int rem = uartclk % div; 2088 2089 baud_raw = (uartclk / div) * mul; 2090 baud_raw += (rem * mul + div / 2) / div; 2091 *baud = (baud_raw + 50) / 100 * 100; 2092 } 2093 2094 if (*baud != baud_raw) 2095 dev_info(sport->port.dev, "Console IMX rounded baud rate from %d to %d\n", 2096 baud_raw, *baud); 2097 } 2098 } 2099 2100 static int 2101 imx_uart_console_setup(struct console *co, char *options) 2102 { 2103 struct imx_port *sport; 2104 int baud = 9600; 2105 int bits = 8; 2106 int parity = 'n'; 2107 int flow = 'n'; 2108 int retval; 2109 2110 /* 2111 * Check whether an invalid uart number has been specified, and 2112 * if so, search for the first available port that does have 2113 * console support. 2114 */ 2115 if (co->index == -1 || co->index >= ARRAY_SIZE(imx_uart_ports)) 2116 co->index = 0; 2117 sport = imx_uart_ports[co->index]; 2118 if (sport == NULL) 2119 return -ENODEV; 2120 2121 /* For setting the registers, we only need to enable the ipg clock. */ 2122 retval = clk_prepare_enable(sport->clk_ipg); 2123 if (retval) 2124 goto error_console; 2125 2126 if (options) 2127 uart_parse_options(options, &baud, &parity, &bits, &flow); 2128 else 2129 imx_uart_console_get_options(sport, &baud, &parity, &bits); 2130 2131 imx_uart_setup_ufcr(sport, TXTL_DEFAULT, RXTL_DEFAULT); 2132 2133 retval = uart_set_options(&sport->port, co, baud, parity, bits, flow); 2134 2135 if (retval) { 2136 clk_disable_unprepare(sport->clk_ipg); 2137 goto error_console; 2138 } 2139 2140 retval = clk_prepare_enable(sport->clk_per); 2141 if (retval) 2142 clk_disable_unprepare(sport->clk_ipg); 2143 2144 error_console: 2145 return retval; 2146 } 2147 2148 static int 2149 imx_uart_console_exit(struct console *co) 2150 { 2151 struct imx_port *sport = imx_uart_ports[co->index]; 2152 2153 clk_disable_unprepare(sport->clk_per); 2154 clk_disable_unprepare(sport->clk_ipg); 2155 2156 return 0; 2157 } 2158 2159 static struct uart_driver imx_uart_uart_driver; 2160 static struct console imx_uart_console = { 2161 .name = DEV_NAME, 2162 .write = imx_uart_console_write, 2163 .device = uart_console_device, 2164 .setup = imx_uart_console_setup, 2165 .exit = imx_uart_console_exit, 2166 .flags = CON_PRINTBUFFER, 2167 .index = -1, 2168 .data = &imx_uart_uart_driver, 2169 }; 2170 2171 #define IMX_CONSOLE &imx_uart_console 2172 2173 #else 2174 #define IMX_CONSOLE NULL 2175 #endif 2176 2177 static struct uart_driver imx_uart_uart_driver = { 2178 .owner = THIS_MODULE, 2179 .driver_name = DRIVER_NAME, 2180 .dev_name = DEV_NAME, 2181 .major = SERIAL_IMX_MAJOR, 2182 .minor = MINOR_START, 2183 .nr = ARRAY_SIZE(imx_uart_ports), 2184 .cons = IMX_CONSOLE, 2185 }; 2186 2187 static enum hrtimer_restart imx_trigger_start_tx(struct hrtimer *t) 2188 { 2189 struct imx_port *sport = container_of(t, struct imx_port, trigger_start_tx); 2190 unsigned long flags; 2191 2192 spin_lock_irqsave(&sport->port.lock, flags); 2193 if (sport->tx_state == WAIT_AFTER_RTS) 2194 imx_uart_start_tx(&sport->port); 2195 spin_unlock_irqrestore(&sport->port.lock, flags); 2196 2197 return HRTIMER_NORESTART; 2198 } 2199 2200 static enum hrtimer_restart imx_trigger_stop_tx(struct hrtimer *t) 2201 { 2202 struct imx_port *sport = container_of(t, struct imx_port, trigger_stop_tx); 2203 unsigned long flags; 2204 2205 spin_lock_irqsave(&sport->port.lock, flags); 2206 if (sport->tx_state == WAIT_AFTER_SEND) 2207 imx_uart_stop_tx(&sport->port); 2208 spin_unlock_irqrestore(&sport->port.lock, flags); 2209 2210 return HRTIMER_NORESTART; 2211 } 2212 2213 static const struct serial_rs485 imx_rs485_supported = { 2214 .flags = SER_RS485_ENABLED | SER_RS485_RTS_ON_SEND | SER_RS485_RTS_AFTER_SEND | 2215 SER_RS485_RX_DURING_TX, 2216 .delay_rts_before_send = 1, 2217 .delay_rts_after_send = 1, 2218 }; 2219 2220 /* Default RX DMA buffer configuration */ 2221 #define RX_DMA_PERIODS 16 2222 #define RX_DMA_PERIOD_LEN (PAGE_SIZE / 4) 2223 2224 static int imx_uart_probe(struct platform_device *pdev) 2225 { 2226 struct device_node *np = pdev->dev.of_node; 2227 struct imx_port *sport; 2228 void __iomem *base; 2229 u32 dma_buf_conf[2]; 2230 int ret = 0; 2231 u32 ucr1, ucr2, uts; 2232 struct resource *res; 2233 int txirq, rxirq, rtsirq; 2234 2235 sport = devm_kzalloc(&pdev->dev, sizeof(*sport), GFP_KERNEL); 2236 if (!sport) 2237 return -ENOMEM; 2238 2239 sport->devdata = of_device_get_match_data(&pdev->dev); 2240 2241 ret = of_alias_get_id(np, "serial"); 2242 if (ret < 0) { 2243 dev_err(&pdev->dev, "failed to get alias id, errno %d\n", ret); 2244 return ret; 2245 } 2246 sport->port.line = ret; 2247 2248 sport->have_rtscts = of_property_read_bool(np, "uart-has-rtscts") || 2249 of_property_read_bool(np, "fsl,uart-has-rtscts"); /* deprecated */ 2250 2251 sport->dte_mode = of_property_read_bool(np, "fsl,dte-mode"); 2252 2253 sport->have_rtsgpio = of_property_present(np, "rts-gpios"); 2254 2255 sport->inverted_tx = of_property_read_bool(np, "fsl,inverted-tx"); 2256 2257 sport->inverted_rx = of_property_read_bool(np, "fsl,inverted-rx"); 2258 2259 if (!of_property_read_u32_array(np, "fsl,dma-info", dma_buf_conf, 2)) { 2260 sport->rx_period_length = dma_buf_conf[0]; 2261 sport->rx_periods = dma_buf_conf[1]; 2262 } else { 2263 sport->rx_period_length = RX_DMA_PERIOD_LEN; 2264 sport->rx_periods = RX_DMA_PERIODS; 2265 } 2266 2267 if (sport->port.line >= ARRAY_SIZE(imx_uart_ports)) { 2268 dev_err(&pdev->dev, "serial%d out of range\n", 2269 sport->port.line); 2270 return -EINVAL; 2271 } 2272 2273 base = devm_platform_get_and_ioremap_resource(pdev, 0, &res); 2274 if (IS_ERR(base)) 2275 return PTR_ERR(base); 2276 2277 rxirq = platform_get_irq(pdev, 0); 2278 if (rxirq < 0) 2279 return rxirq; 2280 txirq = platform_get_irq_optional(pdev, 1); 2281 rtsirq = platform_get_irq_optional(pdev, 2); 2282 2283 sport->port.dev = &pdev->dev; 2284 sport->port.mapbase = res->start; 2285 sport->port.membase = base; 2286 sport->port.type = PORT_IMX; 2287 sport->port.iotype = UPIO_MEM; 2288 sport->port.irq = rxirq; 2289 sport->port.fifosize = 32; 2290 sport->port.has_sysrq = IS_ENABLED(CONFIG_SERIAL_IMX_CONSOLE); 2291 sport->port.ops = &imx_uart_pops; 2292 sport->port.rs485_config = imx_uart_rs485_config; 2293 /* RTS is required to control the RS485 transmitter */ 2294 if (sport->have_rtscts || sport->have_rtsgpio) 2295 sport->port.rs485_supported = imx_rs485_supported; 2296 sport->port.flags = UPF_BOOT_AUTOCONF; 2297 timer_setup(&sport->timer, imx_uart_timeout, 0); 2298 2299 sport->gpios = mctrl_gpio_init(&sport->port, 0); 2300 if (IS_ERR(sport->gpios)) 2301 return PTR_ERR(sport->gpios); 2302 2303 sport->clk_ipg = devm_clk_get(&pdev->dev, "ipg"); 2304 if (IS_ERR(sport->clk_ipg)) { 2305 ret = PTR_ERR(sport->clk_ipg); 2306 dev_err(&pdev->dev, "failed to get ipg clk: %d\n", ret); 2307 return ret; 2308 } 2309 2310 sport->clk_per = devm_clk_get(&pdev->dev, "per"); 2311 if (IS_ERR(sport->clk_per)) { 2312 ret = PTR_ERR(sport->clk_per); 2313 dev_err(&pdev->dev, "failed to get per clk: %d\n", ret); 2314 return ret; 2315 } 2316 2317 sport->port.uartclk = clk_get_rate(sport->clk_per); 2318 2319 /* For register access, we only need to enable the ipg clock. */ 2320 ret = clk_prepare_enable(sport->clk_ipg); 2321 if (ret) { 2322 dev_err(&pdev->dev, "failed to enable ipg clk: %d\n", ret); 2323 return ret; 2324 } 2325 2326 ret = uart_get_rs485_mode(&sport->port); 2327 if (ret) 2328 goto err_clk; 2329 2330 /* 2331 * If using the i.MX UART RTS/CTS control then the RTS (CTS_B) 2332 * signal cannot be set low during transmission in case the 2333 * receiver is off (limitation of the i.MX UART IP). 2334 */ 2335 if (sport->port.rs485.flags & SER_RS485_ENABLED && 2336 sport->have_rtscts && !sport->have_rtsgpio && 2337 (!(sport->port.rs485.flags & SER_RS485_RTS_ON_SEND) && 2338 !(sport->port.rs485.flags & SER_RS485_RX_DURING_TX))) 2339 dev_err(&pdev->dev, 2340 "low-active RTS not possible when receiver is off, enabling receiver\n"); 2341 2342 /* Disable interrupts before requesting them */ 2343 ucr1 = imx_uart_readl(sport, UCR1); 2344 ucr1 &= ~(UCR1_ADEN | UCR1_TRDYEN | UCR1_IDEN | UCR1_RRDYEN | UCR1_RTSDEN); 2345 imx_uart_writel(sport, ucr1, UCR1); 2346 2347 /* Disable Ageing Timer interrupt */ 2348 ucr2 = imx_uart_readl(sport, UCR2); 2349 ucr2 &= ~UCR2_ATEN; 2350 imx_uart_writel(sport, ucr2, UCR2); 2351 2352 /* 2353 * In case RS485 is enabled without GPIO RTS control, the UART IP 2354 * is used to control CTS signal. Keep both the UART and Receiver 2355 * enabled, otherwise the UART IP pulls CTS signal always HIGH no 2356 * matter how the UCR2 CTSC and CTS bits are set. To prevent any 2357 * data from being fed into the RX FIFO, enable loopback mode in 2358 * UTS register, which disconnects the RX path from external RXD 2359 * pin and connects it to the Transceiver, which is disabled, so 2360 * no data can be fed to the RX FIFO that way. 2361 */ 2362 if (sport->port.rs485.flags & SER_RS485_ENABLED && 2363 sport->have_rtscts && !sport->have_rtsgpio) { 2364 uts = imx_uart_readl(sport, imx_uart_uts_reg(sport)); 2365 uts |= UTS_LOOP; 2366 imx_uart_writel(sport, uts, imx_uart_uts_reg(sport)); 2367 2368 ucr1 = imx_uart_readl(sport, UCR1); 2369 ucr1 |= UCR1_UARTEN; 2370 imx_uart_writel(sport, ucr1, UCR1); 2371 2372 ucr2 = imx_uart_readl(sport, UCR2); 2373 ucr2 |= UCR2_RXEN; 2374 imx_uart_writel(sport, ucr2, UCR2); 2375 } 2376 2377 if (!imx_uart_is_imx1(sport) && sport->dte_mode) { 2378 /* 2379 * The DCEDTE bit changes the direction of DSR, DCD, DTR and RI 2380 * and influences if UCR3_RI and UCR3_DCD changes the level of RI 2381 * and DCD (when they are outputs) or enables the respective 2382 * irqs. So set this bit early, i.e. before requesting irqs. 2383 */ 2384 u32 ufcr = imx_uart_readl(sport, UFCR); 2385 if (!(ufcr & UFCR_DCEDTE)) 2386 imx_uart_writel(sport, ufcr | UFCR_DCEDTE, UFCR); 2387 2388 /* 2389 * Disable UCR3_RI and UCR3_DCD irqs. They are also not 2390 * enabled later because they cannot be cleared 2391 * (confirmed on i.MX25) which makes them unusable. 2392 */ 2393 imx_uart_writel(sport, 2394 IMX21_UCR3_RXDMUXSEL | UCR3_ADNIMP | UCR3_DSR, 2395 UCR3); 2396 2397 } else { 2398 u32 ucr3 = UCR3_DSR; 2399 u32 ufcr = imx_uart_readl(sport, UFCR); 2400 if (ufcr & UFCR_DCEDTE) 2401 imx_uart_writel(sport, ufcr & ~UFCR_DCEDTE, UFCR); 2402 2403 if (!imx_uart_is_imx1(sport)) 2404 ucr3 |= IMX21_UCR3_RXDMUXSEL | UCR3_ADNIMP; 2405 imx_uart_writel(sport, ucr3, UCR3); 2406 } 2407 2408 hrtimer_init(&sport->trigger_start_tx, CLOCK_MONOTONIC, HRTIMER_MODE_REL); 2409 hrtimer_init(&sport->trigger_stop_tx, CLOCK_MONOTONIC, HRTIMER_MODE_REL); 2410 sport->trigger_start_tx.function = imx_trigger_start_tx; 2411 sport->trigger_stop_tx.function = imx_trigger_stop_tx; 2412 2413 /* 2414 * Allocate the IRQ(s) i.MX1 has three interrupts whereas later 2415 * chips only have one interrupt. 2416 */ 2417 if (txirq > 0) { 2418 ret = devm_request_irq(&pdev->dev, rxirq, imx_uart_rxint, 0, 2419 dev_name(&pdev->dev), sport); 2420 if (ret) { 2421 dev_err(&pdev->dev, "failed to request rx irq: %d\n", 2422 ret); 2423 goto err_clk; 2424 } 2425 2426 ret = devm_request_irq(&pdev->dev, txirq, imx_uart_txint, 0, 2427 dev_name(&pdev->dev), sport); 2428 if (ret) { 2429 dev_err(&pdev->dev, "failed to request tx irq: %d\n", 2430 ret); 2431 goto err_clk; 2432 } 2433 2434 ret = devm_request_irq(&pdev->dev, rtsirq, imx_uart_rtsint, 0, 2435 dev_name(&pdev->dev), sport); 2436 if (ret) { 2437 dev_err(&pdev->dev, "failed to request rts irq: %d\n", 2438 ret); 2439 goto err_clk; 2440 } 2441 } else { 2442 ret = devm_request_irq(&pdev->dev, rxirq, imx_uart_int, 0, 2443 dev_name(&pdev->dev), sport); 2444 if (ret) { 2445 dev_err(&pdev->dev, "failed to request irq: %d\n", ret); 2446 goto err_clk; 2447 } 2448 } 2449 2450 imx_uart_ports[sport->port.line] = sport; 2451 2452 platform_set_drvdata(pdev, sport); 2453 2454 ret = uart_add_one_port(&imx_uart_uart_driver, &sport->port); 2455 2456 err_clk: 2457 clk_disable_unprepare(sport->clk_ipg); 2458 2459 return ret; 2460 } 2461 2462 static int imx_uart_remove(struct platform_device *pdev) 2463 { 2464 struct imx_port *sport = platform_get_drvdata(pdev); 2465 2466 uart_remove_one_port(&imx_uart_uart_driver, &sport->port); 2467 2468 return 0; 2469 } 2470 2471 static void imx_uart_restore_context(struct imx_port *sport) 2472 { 2473 unsigned long flags; 2474 2475 spin_lock_irqsave(&sport->port.lock, flags); 2476 if (!sport->context_saved) { 2477 spin_unlock_irqrestore(&sport->port.lock, flags); 2478 return; 2479 } 2480 2481 imx_uart_writel(sport, sport->saved_reg[4], UFCR); 2482 imx_uart_writel(sport, sport->saved_reg[5], UESC); 2483 imx_uart_writel(sport, sport->saved_reg[6], UTIM); 2484 imx_uart_writel(sport, sport->saved_reg[7], UBIR); 2485 imx_uart_writel(sport, sport->saved_reg[8], UBMR); 2486 imx_uart_writel(sport, sport->saved_reg[9], IMX21_UTS); 2487 imx_uart_writel(sport, sport->saved_reg[0], UCR1); 2488 imx_uart_writel(sport, sport->saved_reg[1] | UCR2_SRST, UCR2); 2489 imx_uart_writel(sport, sport->saved_reg[2], UCR3); 2490 imx_uart_writel(sport, sport->saved_reg[3], UCR4); 2491 sport->context_saved = false; 2492 spin_unlock_irqrestore(&sport->port.lock, flags); 2493 } 2494 2495 static void imx_uart_save_context(struct imx_port *sport) 2496 { 2497 unsigned long flags; 2498 2499 /* Save necessary regs */ 2500 spin_lock_irqsave(&sport->port.lock, flags); 2501 sport->saved_reg[0] = imx_uart_readl(sport, UCR1); 2502 sport->saved_reg[1] = imx_uart_readl(sport, UCR2); 2503 sport->saved_reg[2] = imx_uart_readl(sport, UCR3); 2504 sport->saved_reg[3] = imx_uart_readl(sport, UCR4); 2505 sport->saved_reg[4] = imx_uart_readl(sport, UFCR); 2506 sport->saved_reg[5] = imx_uart_readl(sport, UESC); 2507 sport->saved_reg[6] = imx_uart_readl(sport, UTIM); 2508 sport->saved_reg[7] = imx_uart_readl(sport, UBIR); 2509 sport->saved_reg[8] = imx_uart_readl(sport, UBMR); 2510 sport->saved_reg[9] = imx_uart_readl(sport, IMX21_UTS); 2511 sport->context_saved = true; 2512 spin_unlock_irqrestore(&sport->port.lock, flags); 2513 } 2514 2515 static void imx_uart_enable_wakeup(struct imx_port *sport, bool on) 2516 { 2517 u32 ucr3; 2518 2519 ucr3 = imx_uart_readl(sport, UCR3); 2520 if (on) { 2521 imx_uart_writel(sport, USR1_AWAKE, USR1); 2522 ucr3 |= UCR3_AWAKEN; 2523 } else { 2524 ucr3 &= ~UCR3_AWAKEN; 2525 } 2526 imx_uart_writel(sport, ucr3, UCR3); 2527 2528 if (sport->have_rtscts) { 2529 u32 ucr1 = imx_uart_readl(sport, UCR1); 2530 if (on) { 2531 imx_uart_writel(sport, USR1_RTSD, USR1); 2532 ucr1 |= UCR1_RTSDEN; 2533 } else { 2534 ucr1 &= ~UCR1_RTSDEN; 2535 } 2536 imx_uart_writel(sport, ucr1, UCR1); 2537 } 2538 } 2539 2540 static int imx_uart_suspend_noirq(struct device *dev) 2541 { 2542 struct imx_port *sport = dev_get_drvdata(dev); 2543 2544 imx_uart_save_context(sport); 2545 2546 clk_disable(sport->clk_ipg); 2547 2548 pinctrl_pm_select_sleep_state(dev); 2549 2550 return 0; 2551 } 2552 2553 static int imx_uart_resume_noirq(struct device *dev) 2554 { 2555 struct imx_port *sport = dev_get_drvdata(dev); 2556 int ret; 2557 2558 pinctrl_pm_select_default_state(dev); 2559 2560 ret = clk_enable(sport->clk_ipg); 2561 if (ret) 2562 return ret; 2563 2564 imx_uart_restore_context(sport); 2565 2566 return 0; 2567 } 2568 2569 static int imx_uart_suspend(struct device *dev) 2570 { 2571 struct imx_port *sport = dev_get_drvdata(dev); 2572 int ret; 2573 2574 uart_suspend_port(&imx_uart_uart_driver, &sport->port); 2575 disable_irq(sport->port.irq); 2576 2577 ret = clk_prepare_enable(sport->clk_ipg); 2578 if (ret) 2579 return ret; 2580 2581 /* enable wakeup from i.MX UART */ 2582 imx_uart_enable_wakeup(sport, true); 2583 2584 return 0; 2585 } 2586 2587 static int imx_uart_resume(struct device *dev) 2588 { 2589 struct imx_port *sport = dev_get_drvdata(dev); 2590 2591 /* disable wakeup from i.MX UART */ 2592 imx_uart_enable_wakeup(sport, false); 2593 2594 uart_resume_port(&imx_uart_uart_driver, &sport->port); 2595 enable_irq(sport->port.irq); 2596 2597 clk_disable_unprepare(sport->clk_ipg); 2598 2599 return 0; 2600 } 2601 2602 static int imx_uart_freeze(struct device *dev) 2603 { 2604 struct imx_port *sport = dev_get_drvdata(dev); 2605 2606 uart_suspend_port(&imx_uart_uart_driver, &sport->port); 2607 2608 return clk_prepare_enable(sport->clk_ipg); 2609 } 2610 2611 static int imx_uart_thaw(struct device *dev) 2612 { 2613 struct imx_port *sport = dev_get_drvdata(dev); 2614 2615 uart_resume_port(&imx_uart_uart_driver, &sport->port); 2616 2617 clk_disable_unprepare(sport->clk_ipg); 2618 2619 return 0; 2620 } 2621 2622 static const struct dev_pm_ops imx_uart_pm_ops = { 2623 .suspend_noirq = imx_uart_suspend_noirq, 2624 .resume_noirq = imx_uart_resume_noirq, 2625 .freeze_noirq = imx_uart_suspend_noirq, 2626 .thaw_noirq = imx_uart_resume_noirq, 2627 .restore_noirq = imx_uart_resume_noirq, 2628 .suspend = imx_uart_suspend, 2629 .resume = imx_uart_resume, 2630 .freeze = imx_uart_freeze, 2631 .thaw = imx_uart_thaw, 2632 .restore = imx_uart_thaw, 2633 }; 2634 2635 static struct platform_driver imx_uart_platform_driver = { 2636 .probe = imx_uart_probe, 2637 .remove = imx_uart_remove, 2638 2639 .driver = { 2640 .name = "imx-uart", 2641 .of_match_table = imx_uart_dt_ids, 2642 .pm = &imx_uart_pm_ops, 2643 }, 2644 }; 2645 2646 static int __init imx_uart_init(void) 2647 { 2648 int ret = uart_register_driver(&imx_uart_uart_driver); 2649 2650 if (ret) 2651 return ret; 2652 2653 ret = platform_driver_register(&imx_uart_platform_driver); 2654 if (ret != 0) 2655 uart_unregister_driver(&imx_uart_uart_driver); 2656 2657 return ret; 2658 } 2659 2660 static void __exit imx_uart_exit(void) 2661 { 2662 platform_driver_unregister(&imx_uart_platform_driver); 2663 uart_unregister_driver(&imx_uart_uart_driver); 2664 } 2665 2666 module_init(imx_uart_init); 2667 module_exit(imx_uart_exit); 2668 2669 MODULE_AUTHOR("Sascha Hauer"); 2670 MODULE_DESCRIPTION("IMX generic serial port driver"); 2671 MODULE_LICENSE("GPL"); 2672 MODULE_ALIAS("platform:imx-uart"); 2673