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