1 // SPDX-License-Identifier: GPL-2.0+ 2 /* 3 * SiFive UART driver 4 * Copyright (C) 2018 Paul Walmsley <paul@pwsan.com> 5 * Copyright (C) 2018-2019 SiFive 6 * 7 * Based partially on: 8 * - drivers/tty/serial/pxa.c 9 * - drivers/tty/serial/amba-pl011.c 10 * - drivers/tty/serial/uartlite.c 11 * - drivers/tty/serial/omap-serial.c 12 * - drivers/pwm/pwm-sifive.c 13 * 14 * See the following sources for further documentation: 15 * - Chapter 19 "Universal Asynchronous Receiver/Transmitter (UART)" of 16 * SiFive FE310-G000 v2p3 17 * - The tree/master/src/main/scala/devices/uart directory of 18 * https://github.com/sifive/sifive-blocks/ 19 * 20 * The SiFive UART design is not 8250-compatible. The following common 21 * features are not supported: 22 * - Word lengths other than 8 bits 23 * - Break handling 24 * - Parity 25 * - Flow control 26 * - Modem signals (DSR, RI, etc.) 27 * On the other hand, the design is free from the baggage of the 8250 28 * programming model. 29 */ 30 31 #include <linux/clk.h> 32 #include <linux/console.h> 33 #include <linux/delay.h> 34 #include <linux/init.h> 35 #include <linux/io.h> 36 #include <linux/irq.h> 37 #include <linux/module.h> 38 #include <linux/of.h> 39 #include <linux/of_irq.h> 40 #include <linux/platform_device.h> 41 #include <linux/serial_core.h> 42 #include <linux/serial_reg.h> 43 #include <linux/slab.h> 44 #include <linux/tty.h> 45 #include <linux/tty_flip.h> 46 47 /* 48 * Register offsets 49 */ 50 51 /* TXDATA */ 52 #define SIFIVE_SERIAL_TXDATA_OFFS 0x0 53 #define SIFIVE_SERIAL_TXDATA_FULL_SHIFT 31 54 #define SIFIVE_SERIAL_TXDATA_FULL_MASK (1 << SIFIVE_SERIAL_TXDATA_FULL_SHIFT) 55 #define SIFIVE_SERIAL_TXDATA_DATA_SHIFT 0 56 #define SIFIVE_SERIAL_TXDATA_DATA_MASK (0xff << SIFIVE_SERIAL_TXDATA_DATA_SHIFT) 57 58 /* RXDATA */ 59 #define SIFIVE_SERIAL_RXDATA_OFFS 0x4 60 #define SIFIVE_SERIAL_RXDATA_EMPTY_SHIFT 31 61 #define SIFIVE_SERIAL_RXDATA_EMPTY_MASK (1 << SIFIVE_SERIAL_RXDATA_EMPTY_SHIFT) 62 #define SIFIVE_SERIAL_RXDATA_DATA_SHIFT 0 63 #define SIFIVE_SERIAL_RXDATA_DATA_MASK (0xff << SIFIVE_SERIAL_RXDATA_DATA_SHIFT) 64 65 /* TXCTRL */ 66 #define SIFIVE_SERIAL_TXCTRL_OFFS 0x8 67 #define SIFIVE_SERIAL_TXCTRL_TXCNT_SHIFT 16 68 #define SIFIVE_SERIAL_TXCTRL_TXCNT_MASK (0x7 << SIFIVE_SERIAL_TXCTRL_TXCNT_SHIFT) 69 #define SIFIVE_SERIAL_TXCTRL_NSTOP_SHIFT 1 70 #define SIFIVE_SERIAL_TXCTRL_NSTOP_MASK (1 << SIFIVE_SERIAL_TXCTRL_NSTOP_SHIFT) 71 #define SIFIVE_SERIAL_TXCTRL_TXEN_SHIFT 0 72 #define SIFIVE_SERIAL_TXCTRL_TXEN_MASK (1 << SIFIVE_SERIAL_TXCTRL_TXEN_SHIFT) 73 74 /* RXCTRL */ 75 #define SIFIVE_SERIAL_RXCTRL_OFFS 0xC 76 #define SIFIVE_SERIAL_RXCTRL_RXCNT_SHIFT 16 77 #define SIFIVE_SERIAL_RXCTRL_RXCNT_MASK (0x7 << SIFIVE_SERIAL_TXCTRL_TXCNT_SHIFT) 78 #define SIFIVE_SERIAL_RXCTRL_RXEN_SHIFT 0 79 #define SIFIVE_SERIAL_RXCTRL_RXEN_MASK (1 << SIFIVE_SERIAL_RXCTRL_RXEN_SHIFT) 80 81 /* IE */ 82 #define SIFIVE_SERIAL_IE_OFFS 0x10 83 #define SIFIVE_SERIAL_IE_RXWM_SHIFT 1 84 #define SIFIVE_SERIAL_IE_RXWM_MASK (1 << SIFIVE_SERIAL_IE_RXWM_SHIFT) 85 #define SIFIVE_SERIAL_IE_TXWM_SHIFT 0 86 #define SIFIVE_SERIAL_IE_TXWM_MASK (1 << SIFIVE_SERIAL_IE_TXWM_SHIFT) 87 88 /* IP */ 89 #define SIFIVE_SERIAL_IP_OFFS 0x14 90 #define SIFIVE_SERIAL_IP_RXWM_SHIFT 1 91 #define SIFIVE_SERIAL_IP_RXWM_MASK (1 << SIFIVE_SERIAL_IP_RXWM_SHIFT) 92 #define SIFIVE_SERIAL_IP_TXWM_SHIFT 0 93 #define SIFIVE_SERIAL_IP_TXWM_MASK (1 << SIFIVE_SERIAL_IP_TXWM_SHIFT) 94 95 /* DIV */ 96 #define SIFIVE_SERIAL_DIV_OFFS 0x18 97 #define SIFIVE_SERIAL_DIV_DIV_SHIFT 0 98 #define SIFIVE_SERIAL_DIV_DIV_MASK (0xffff << SIFIVE_SERIAL_IP_DIV_SHIFT) 99 100 /* 101 * Config macros 102 */ 103 104 /* 105 * SIFIVE_SERIAL_MAX_PORTS: maximum number of UARTs on a device that can 106 * host a serial console 107 */ 108 #define SIFIVE_SERIAL_MAX_PORTS 8 109 110 /* 111 * SIFIVE_DEFAULT_BAUD_RATE: default baud rate that the driver should 112 * configure itself to use 113 */ 114 #define SIFIVE_DEFAULT_BAUD_RATE 115200 115 116 /* SIFIVE_SERIAL_NAME: our driver's name that we pass to the operating system */ 117 #define SIFIVE_SERIAL_NAME "sifive-serial" 118 119 /* SIFIVE_TTY_PREFIX: tty name prefix for SiFive serial ports */ 120 #define SIFIVE_TTY_PREFIX "ttySIF" 121 122 /* SIFIVE_TX_FIFO_DEPTH: depth of the TX FIFO (in bytes) */ 123 #define SIFIVE_TX_FIFO_DEPTH 8 124 125 /* SIFIVE_RX_FIFO_DEPTH: depth of the TX FIFO (in bytes) */ 126 #define SIFIVE_RX_FIFO_DEPTH 8 127 128 #if (SIFIVE_TX_FIFO_DEPTH != SIFIVE_RX_FIFO_DEPTH) 129 #error Driver does not support configurations with different TX, RX FIFO sizes 130 #endif 131 132 /* 133 * 134 */ 135 136 /** 137 * struct sifive_serial_port - driver-specific data extension to struct uart_port 138 * @port: struct uart_port embedded in this struct 139 * @dev: struct device * 140 * @ier: shadowed copy of the interrupt enable register 141 * @baud_rate: UART serial line rate (e.g., 115200 baud) 142 * @clk: reference to this device's clock 143 * @clk_notifier: clock rate change notifier for upstream clock changes 144 * 145 * Configuration data specific to this SiFive UART. 146 */ 147 struct sifive_serial_port { 148 struct uart_port port; 149 struct device *dev; 150 unsigned char ier; 151 unsigned long baud_rate; 152 struct clk *clk; 153 struct notifier_block clk_notifier; 154 }; 155 156 /* 157 * Structure container-of macros 158 */ 159 160 #define port_to_sifive_serial_port(p) (container_of((p), \ 161 struct sifive_serial_port, \ 162 port)) 163 164 #define notifier_to_sifive_serial_port(nb) (container_of((nb), \ 165 struct sifive_serial_port, \ 166 clk_notifier)) 167 168 /* 169 * Forward declarations 170 */ 171 static void sifive_serial_stop_tx(struct uart_port *port); 172 173 /* 174 * Internal functions 175 */ 176 177 /** 178 * __ssp_early_writel() - write to a SiFive serial port register (early) 179 * @port: pointer to a struct uart_port record 180 * @offs: register address offset from the IP block base address 181 * @v: value to write to the register 182 * 183 * Given a pointer @port to a struct uart_port record, write the value 184 * @v to the IP block register address offset @offs. This function is 185 * intended for early console use. 186 * 187 * Context: Intended to be used only by the earlyconsole code. 188 */ 189 static void __ssp_early_writel(u32 v, u16 offs, struct uart_port *port) 190 { 191 writel_relaxed(v, port->membase + offs); 192 } 193 194 /** 195 * __ssp_early_readl() - read from a SiFive serial port register (early) 196 * @port: pointer to a struct uart_port record 197 * @offs: register address offset from the IP block base address 198 * 199 * Given a pointer @port to a struct uart_port record, read the 200 * contents of the IP block register located at offset @offs from the 201 * IP block base and return it. This function is intended for early 202 * console use. 203 * 204 * Context: Intended to be called only by the earlyconsole code or by 205 * __ssp_readl() or __ssp_writel() (in this driver) 206 * 207 * Returns: the register value read from the UART. 208 */ 209 static u32 __ssp_early_readl(struct uart_port *port, u16 offs) 210 { 211 return readl_relaxed(port->membase + offs); 212 } 213 214 /** 215 * __ssp_writel() - write to a SiFive serial port register 216 * @v: value to write to the register 217 * @offs: register address offset from the IP block base address 218 * @ssp: pointer to a struct sifive_serial_port record 219 * 220 * Write the value @v to the IP block register located at offset @offs from the 221 * IP block base, given a pointer @ssp to a struct sifive_serial_port record. 222 * 223 * Context: Any context. 224 */ 225 static void __ssp_writel(u32 v, u16 offs, struct sifive_serial_port *ssp) 226 { 227 __ssp_early_writel(v, offs, &ssp->port); 228 } 229 230 /** 231 * __ssp_readl() - read from a SiFive serial port register 232 * @ssp: pointer to a struct sifive_serial_port record 233 * @offs: register address offset from the IP block base address 234 * 235 * Read the contents of the IP block register located at offset @offs from the 236 * IP block base, given a pointer @ssp to a struct sifive_serial_port record. 237 * 238 * Context: Any context. 239 * 240 * Returns: the value of the UART register 241 */ 242 static u32 __ssp_readl(struct sifive_serial_port *ssp, u16 offs) 243 { 244 return __ssp_early_readl(&ssp->port, offs); 245 } 246 247 /** 248 * sifive_serial_is_txfifo_full() - is the TXFIFO full? 249 * @ssp: pointer to a struct sifive_serial_port 250 * 251 * Read the transmit FIFO "full" bit, returning a non-zero value if the 252 * TX FIFO is full, or zero if space remains. Intended to be used to prevent 253 * writes to the TX FIFO when it's full. 254 * 255 * Returns: SIFIVE_SERIAL_TXDATA_FULL_MASK (non-zero) if the transmit FIFO 256 * is full, or 0 if space remains. 257 */ 258 static int sifive_serial_is_txfifo_full(struct sifive_serial_port *ssp) 259 { 260 return __ssp_readl(ssp, SIFIVE_SERIAL_TXDATA_OFFS) & 261 SIFIVE_SERIAL_TXDATA_FULL_MASK; 262 } 263 264 /** 265 * __ssp_transmit_char() - enqueue a byte to transmit onto the TX FIFO 266 * @ssp: pointer to a struct sifive_serial_port 267 * @ch: character to transmit 268 * 269 * Enqueue a byte @ch onto the transmit FIFO, given a pointer @ssp to the 270 * struct sifive_serial_port * to transmit on. Caller should first check to 271 * ensure that the TXFIFO has space; see sifive_serial_is_txfifo_full(). 272 * 273 * Context: Any context. 274 */ 275 static void __ssp_transmit_char(struct sifive_serial_port *ssp, int ch) 276 { 277 __ssp_writel(ch, SIFIVE_SERIAL_TXDATA_OFFS, ssp); 278 } 279 280 /** 281 * __ssp_transmit_chars() - enqueue multiple bytes onto the TX FIFO 282 * @ssp: pointer to a struct sifive_serial_port 283 * 284 * Transfer up to a TX FIFO size's worth of characters from the Linux serial 285 * transmit buffer to the SiFive UART TX FIFO. 286 * 287 * Context: Any context. Expects @ssp->port.lock to be held by caller. 288 */ 289 static void __ssp_transmit_chars(struct sifive_serial_port *ssp) 290 { 291 struct circ_buf *xmit = &ssp->port.state->xmit; 292 int count; 293 294 if (ssp->port.x_char) { 295 __ssp_transmit_char(ssp, ssp->port.x_char); 296 ssp->port.icount.tx++; 297 ssp->port.x_char = 0; 298 return; 299 } 300 if (uart_circ_empty(xmit) || uart_tx_stopped(&ssp->port)) { 301 sifive_serial_stop_tx(&ssp->port); 302 return; 303 } 304 count = SIFIVE_TX_FIFO_DEPTH; 305 do { 306 __ssp_transmit_char(ssp, xmit->buf[xmit->tail]); 307 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1); 308 ssp->port.icount.tx++; 309 if (uart_circ_empty(xmit)) 310 break; 311 } while (--count > 0); 312 313 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS) 314 uart_write_wakeup(&ssp->port); 315 316 if (uart_circ_empty(xmit)) 317 sifive_serial_stop_tx(&ssp->port); 318 } 319 320 /** 321 * __ssp_enable_txwm() - enable transmit watermark interrupts 322 * @ssp: pointer to a struct sifive_serial_port 323 * 324 * Enable interrupt generation when the transmit FIFO watermark is reached 325 * on the SiFive UART referred to by @ssp. 326 */ 327 static void __ssp_enable_txwm(struct sifive_serial_port *ssp) 328 { 329 if (ssp->ier & SIFIVE_SERIAL_IE_TXWM_MASK) 330 return; 331 332 ssp->ier |= SIFIVE_SERIAL_IE_TXWM_MASK; 333 __ssp_writel(ssp->ier, SIFIVE_SERIAL_IE_OFFS, ssp); 334 } 335 336 /** 337 * __ssp_enable_rxwm() - enable receive watermark interrupts 338 * @ssp: pointer to a struct sifive_serial_port 339 * 340 * Enable interrupt generation when the receive FIFO watermark is reached 341 * on the SiFive UART referred to by @ssp. 342 */ 343 static void __ssp_enable_rxwm(struct sifive_serial_port *ssp) 344 { 345 if (ssp->ier & SIFIVE_SERIAL_IE_RXWM_MASK) 346 return; 347 348 ssp->ier |= SIFIVE_SERIAL_IE_RXWM_MASK; 349 __ssp_writel(ssp->ier, SIFIVE_SERIAL_IE_OFFS, ssp); 350 } 351 352 /** 353 * __ssp_disable_txwm() - disable transmit watermark interrupts 354 * @ssp: pointer to a struct sifive_serial_port 355 * 356 * Disable interrupt generation when the transmit FIFO watermark is reached 357 * on the UART referred to by @ssp. 358 */ 359 static void __ssp_disable_txwm(struct sifive_serial_port *ssp) 360 { 361 if (!(ssp->ier & SIFIVE_SERIAL_IE_TXWM_MASK)) 362 return; 363 364 ssp->ier &= ~SIFIVE_SERIAL_IE_TXWM_MASK; 365 __ssp_writel(ssp->ier, SIFIVE_SERIAL_IE_OFFS, ssp); 366 } 367 368 /** 369 * __ssp_disable_rxwm() - disable receive watermark interrupts 370 * @ssp: pointer to a struct sifive_serial_port 371 * 372 * Disable interrupt generation when the receive FIFO watermark is reached 373 * on the UART referred to by @ssp. 374 */ 375 static void __ssp_disable_rxwm(struct sifive_serial_port *ssp) 376 { 377 if (!(ssp->ier & SIFIVE_SERIAL_IE_RXWM_MASK)) 378 return; 379 380 ssp->ier &= ~SIFIVE_SERIAL_IE_RXWM_MASK; 381 __ssp_writel(ssp->ier, SIFIVE_SERIAL_IE_OFFS, ssp); 382 } 383 384 /** 385 * __ssp_receive_char() - receive a byte from the UART 386 * @ssp: pointer to a struct sifive_serial_port 387 * @is_empty: char pointer to return whether the RX FIFO is empty 388 * 389 * Try to read a byte from the SiFive UART RX FIFO, referenced by 390 * @ssp, and to return it. Also returns the RX FIFO empty bit in 391 * the char pointed to by @ch. The caller must pass the byte back to the 392 * Linux serial layer if needed. 393 * 394 * Returns: the byte read from the UART RX FIFO. 395 */ 396 static char __ssp_receive_char(struct sifive_serial_port *ssp, char *is_empty) 397 { 398 u32 v; 399 u8 ch; 400 401 v = __ssp_readl(ssp, SIFIVE_SERIAL_RXDATA_OFFS); 402 403 if (!is_empty) 404 WARN_ON(1); 405 else 406 *is_empty = (v & SIFIVE_SERIAL_RXDATA_EMPTY_MASK) >> 407 SIFIVE_SERIAL_RXDATA_EMPTY_SHIFT; 408 409 ch = (v & SIFIVE_SERIAL_RXDATA_DATA_MASK) >> 410 SIFIVE_SERIAL_RXDATA_DATA_SHIFT; 411 412 return ch; 413 } 414 415 /** 416 * __ssp_receive_chars() - receive multiple bytes from the UART 417 * @ssp: pointer to a struct sifive_serial_port 418 * 419 * Receive up to an RX FIFO's worth of bytes from the SiFive UART referred 420 * to by @ssp and pass them up to the Linux serial layer. 421 * 422 * Context: Expects ssp->port.lock to be held by caller. 423 */ 424 static void __ssp_receive_chars(struct sifive_serial_port *ssp) 425 { 426 unsigned char ch; 427 char is_empty; 428 int c; 429 430 for (c = SIFIVE_RX_FIFO_DEPTH; c > 0; --c) { 431 ch = __ssp_receive_char(ssp, &is_empty); 432 if (is_empty) 433 break; 434 435 ssp->port.icount.rx++; 436 uart_insert_char(&ssp->port, 0, 0, ch, TTY_NORMAL); 437 } 438 439 tty_flip_buffer_push(&ssp->port.state->port); 440 } 441 442 /** 443 * __ssp_update_div() - calculate the divisor setting by the line rate 444 * @ssp: pointer to a struct sifive_serial_port 445 * 446 * Calculate the appropriate value of the clock divisor for the UART 447 * and target line rate referred to by @ssp and write it into the 448 * hardware. 449 */ 450 static void __ssp_update_div(struct sifive_serial_port *ssp) 451 { 452 u16 div; 453 454 div = DIV_ROUND_UP(ssp->port.uartclk, ssp->baud_rate) - 1; 455 456 __ssp_writel(div, SIFIVE_SERIAL_DIV_OFFS, ssp); 457 } 458 459 /** 460 * __ssp_update_baud_rate() - set the UART "baud rate" 461 * @ssp: pointer to a struct sifive_serial_port 462 * @rate: new target bit rate 463 * 464 * Calculate the UART divisor value for the target bit rate @rate for the 465 * SiFive UART described by @ssp and program it into the UART. There may 466 * be some error between the target bit rate and the actual bit rate implemented 467 * by the UART due to clock ratio granularity. 468 */ 469 static void __ssp_update_baud_rate(struct sifive_serial_port *ssp, 470 unsigned int rate) 471 { 472 if (ssp->baud_rate == rate) 473 return; 474 475 ssp->baud_rate = rate; 476 __ssp_update_div(ssp); 477 } 478 479 /** 480 * __ssp_set_stop_bits() - set the number of stop bits 481 * @ssp: pointer to a struct sifive_serial_port 482 * @nstop: 1 or 2 (stop bits) 483 * 484 * Program the SiFive UART referred to by @ssp to use @nstop stop bits. 485 */ 486 static void __ssp_set_stop_bits(struct sifive_serial_port *ssp, char nstop) 487 { 488 u32 v; 489 490 if (nstop < 1 || nstop > 2) { 491 WARN_ON(1); 492 return; 493 } 494 495 v = __ssp_readl(ssp, SIFIVE_SERIAL_TXCTRL_OFFS); 496 v &= ~SIFIVE_SERIAL_TXCTRL_NSTOP_MASK; 497 v |= (nstop - 1) << SIFIVE_SERIAL_TXCTRL_NSTOP_SHIFT; 498 __ssp_writel(v, SIFIVE_SERIAL_TXCTRL_OFFS, ssp); 499 } 500 501 /** 502 * __ssp_wait_for_xmitr() - wait for an empty slot on the TX FIFO 503 * @ssp: pointer to a struct sifive_serial_port 504 * 505 * Delay while the UART TX FIFO referred to by @ssp is marked as full. 506 * 507 * Context: Any context. 508 */ 509 static void __maybe_unused __ssp_wait_for_xmitr(struct sifive_serial_port *ssp) 510 { 511 while (sifive_serial_is_txfifo_full(ssp)) 512 udelay(1); /* XXX Could probably be more intelligent here */ 513 } 514 515 /* 516 * Linux serial API functions 517 */ 518 519 static void sifive_serial_stop_tx(struct uart_port *port) 520 { 521 struct sifive_serial_port *ssp = port_to_sifive_serial_port(port); 522 523 __ssp_disable_txwm(ssp); 524 } 525 526 static void sifive_serial_stop_rx(struct uart_port *port) 527 { 528 struct sifive_serial_port *ssp = port_to_sifive_serial_port(port); 529 530 __ssp_disable_rxwm(ssp); 531 } 532 533 static void sifive_serial_start_tx(struct uart_port *port) 534 { 535 struct sifive_serial_port *ssp = port_to_sifive_serial_port(port); 536 537 __ssp_enable_txwm(ssp); 538 } 539 540 static irqreturn_t sifive_serial_irq(int irq, void *dev_id) 541 { 542 struct sifive_serial_port *ssp = dev_id; 543 u32 ip; 544 545 spin_lock(&ssp->port.lock); 546 547 ip = __ssp_readl(ssp, SIFIVE_SERIAL_IP_OFFS); 548 if (!ip) { 549 spin_unlock(&ssp->port.lock); 550 return IRQ_NONE; 551 } 552 553 if (ip & SIFIVE_SERIAL_IP_RXWM_MASK) 554 __ssp_receive_chars(ssp); 555 if (ip & SIFIVE_SERIAL_IP_TXWM_MASK) 556 __ssp_transmit_chars(ssp); 557 558 spin_unlock(&ssp->port.lock); 559 560 return IRQ_HANDLED; 561 } 562 563 static unsigned int sifive_serial_tx_empty(struct uart_port *port) 564 { 565 return TIOCSER_TEMT; 566 } 567 568 static unsigned int sifive_serial_get_mctrl(struct uart_port *port) 569 { 570 return TIOCM_CAR | TIOCM_CTS | TIOCM_DSR; 571 } 572 573 static void sifive_serial_set_mctrl(struct uart_port *port, unsigned int mctrl) 574 { 575 /* IP block does not support these signals */ 576 } 577 578 static void sifive_serial_break_ctl(struct uart_port *port, int break_state) 579 { 580 /* IP block does not support sending a break */ 581 } 582 583 static int sifive_serial_startup(struct uart_port *port) 584 { 585 struct sifive_serial_port *ssp = port_to_sifive_serial_port(port); 586 587 __ssp_enable_rxwm(ssp); 588 589 return 0; 590 } 591 592 static void sifive_serial_shutdown(struct uart_port *port) 593 { 594 struct sifive_serial_port *ssp = port_to_sifive_serial_port(port); 595 596 __ssp_disable_rxwm(ssp); 597 __ssp_disable_txwm(ssp); 598 } 599 600 /** 601 * sifive_serial_clk_notifier() - clock post-rate-change notifier 602 * @nb: pointer to the struct notifier_block, from the notifier code 603 * @event: event mask from the notifier code 604 * @data: pointer to the struct clk_notifier_data from the notifier code 605 * 606 * On the V0 SoC, the UART IP block is derived from the CPU clock source 607 * after a synchronous divide-by-two divider, so any CPU clock rate change 608 * requires the UART baud rate to be updated. This presumably corrupts any 609 * serial word currently being transmitted or received. In order to avoid 610 * corrupting the output data stream, we drain the transmit queue before 611 * allowing the clock's rate to be changed. 612 */ 613 static int sifive_serial_clk_notifier(struct notifier_block *nb, 614 unsigned long event, void *data) 615 { 616 struct clk_notifier_data *cnd = data; 617 struct sifive_serial_port *ssp = notifier_to_sifive_serial_port(nb); 618 619 if (event == PRE_RATE_CHANGE) { 620 /* 621 * The TX watermark is always set to 1 by this driver, which 622 * means that the TX busy bit will lower when there are 0 bytes 623 * left in the TX queue -- in other words, when the TX FIFO is 624 * empty. 625 */ 626 __ssp_wait_for_xmitr(ssp); 627 /* 628 * On the cycle the TX FIFO goes empty there is still a full 629 * UART frame left to be transmitted in the shift register. 630 * The UART provides no way for software to directly determine 631 * when that last frame has been transmitted, so we just sleep 632 * here instead. As we're not tracking the number of stop bits 633 * they're just worst cased here. The rest of the serial 634 * framing parameters aren't configurable by software. 635 */ 636 udelay(DIV_ROUND_UP(12 * 1000 * 1000, ssp->baud_rate)); 637 } 638 639 if (event == POST_RATE_CHANGE && ssp->port.uartclk != cnd->new_rate) { 640 ssp->port.uartclk = cnd->new_rate; 641 __ssp_update_div(ssp); 642 } 643 644 return NOTIFY_OK; 645 } 646 647 static void sifive_serial_set_termios(struct uart_port *port, 648 struct ktermios *termios, 649 struct ktermios *old) 650 { 651 struct sifive_serial_port *ssp = port_to_sifive_serial_port(port); 652 unsigned long flags; 653 u32 v, old_v; 654 int rate; 655 char nstop; 656 657 if ((termios->c_cflag & CSIZE) != CS8) { 658 dev_err_once(ssp->port.dev, "only 8-bit words supported\n"); 659 termios->c_cflag &= ~CSIZE; 660 termios->c_cflag |= CS8; 661 } 662 if (termios->c_iflag & (INPCK | PARMRK)) 663 dev_err_once(ssp->port.dev, "parity checking not supported\n"); 664 if (termios->c_iflag & BRKINT) 665 dev_err_once(ssp->port.dev, "BREAK detection not supported\n"); 666 termios->c_iflag &= ~(INPCK|PARMRK|BRKINT); 667 668 /* Set number of stop bits */ 669 nstop = (termios->c_cflag & CSTOPB) ? 2 : 1; 670 __ssp_set_stop_bits(ssp, nstop); 671 672 /* Set line rate */ 673 rate = uart_get_baud_rate(port, termios, old, 0, 674 ssp->port.uartclk / 16); 675 __ssp_update_baud_rate(ssp, rate); 676 677 spin_lock_irqsave(&ssp->port.lock, flags); 678 679 /* Update the per-port timeout */ 680 uart_update_timeout(port, termios->c_cflag, rate); 681 682 ssp->port.read_status_mask = 0; 683 684 /* Ignore all characters if CREAD is not set */ 685 v = __ssp_readl(ssp, SIFIVE_SERIAL_RXCTRL_OFFS); 686 old_v = v; 687 if ((termios->c_cflag & CREAD) == 0) 688 v &= SIFIVE_SERIAL_RXCTRL_RXEN_MASK; 689 else 690 v |= SIFIVE_SERIAL_RXCTRL_RXEN_MASK; 691 if (v != old_v) 692 __ssp_writel(v, SIFIVE_SERIAL_RXCTRL_OFFS, ssp); 693 694 spin_unlock_irqrestore(&ssp->port.lock, flags); 695 } 696 697 static void sifive_serial_release_port(struct uart_port *port) 698 { 699 } 700 701 static int sifive_serial_request_port(struct uart_port *port) 702 { 703 return 0; 704 } 705 706 static void sifive_serial_config_port(struct uart_port *port, int flags) 707 { 708 struct sifive_serial_port *ssp = port_to_sifive_serial_port(port); 709 710 ssp->port.type = PORT_SIFIVE_V0; 711 } 712 713 static int sifive_serial_verify_port(struct uart_port *port, 714 struct serial_struct *ser) 715 { 716 return -EINVAL; 717 } 718 719 static const char *sifive_serial_type(struct uart_port *port) 720 { 721 return port->type == PORT_SIFIVE_V0 ? "SiFive UART v0" : NULL; 722 } 723 724 #ifdef CONFIG_CONSOLE_POLL 725 static int sifive_serial_poll_get_char(struct uart_port *port) 726 { 727 struct sifive_serial_port *ssp = port_to_sifive_serial_port(port); 728 char is_empty, ch; 729 730 ch = __ssp_receive_char(ssp, &is_empty); 731 if (is_empty) 732 return NO_POLL_CHAR; 733 734 return ch; 735 } 736 737 static void sifive_serial_poll_put_char(struct uart_port *port, 738 unsigned char c) 739 { 740 struct sifive_serial_port *ssp = port_to_sifive_serial_port(port); 741 742 __ssp_wait_for_xmitr(ssp); 743 __ssp_transmit_char(ssp, c); 744 } 745 #endif /* CONFIG_CONSOLE_POLL */ 746 747 /* 748 * Early console support 749 */ 750 751 #ifdef CONFIG_SERIAL_EARLYCON 752 static void early_sifive_serial_putc(struct uart_port *port, unsigned char c) 753 { 754 while (__ssp_early_readl(port, SIFIVE_SERIAL_TXDATA_OFFS) & 755 SIFIVE_SERIAL_TXDATA_FULL_MASK) 756 cpu_relax(); 757 758 __ssp_early_writel(c, SIFIVE_SERIAL_TXDATA_OFFS, port); 759 } 760 761 static void early_sifive_serial_write(struct console *con, const char *s, 762 unsigned int n) 763 { 764 struct earlycon_device *dev = con->data; 765 struct uart_port *port = &dev->port; 766 767 uart_console_write(port, s, n, early_sifive_serial_putc); 768 } 769 770 static int __init early_sifive_serial_setup(struct earlycon_device *dev, 771 const char *options) 772 { 773 struct uart_port *port = &dev->port; 774 775 if (!port->membase) 776 return -ENODEV; 777 778 dev->con->write = early_sifive_serial_write; 779 780 return 0; 781 } 782 783 OF_EARLYCON_DECLARE(sifive, "sifive,uart0", early_sifive_serial_setup); 784 OF_EARLYCON_DECLARE(sifive, "sifive,fu540-c000-uart0", 785 early_sifive_serial_setup); 786 #endif /* CONFIG_SERIAL_EARLYCON */ 787 788 /* 789 * Linux console interface 790 */ 791 792 #ifdef CONFIG_SERIAL_SIFIVE_CONSOLE 793 794 static struct sifive_serial_port *sifive_serial_console_ports[SIFIVE_SERIAL_MAX_PORTS]; 795 796 static void sifive_serial_console_putchar(struct uart_port *port, unsigned char ch) 797 { 798 struct sifive_serial_port *ssp = port_to_sifive_serial_port(port); 799 800 __ssp_wait_for_xmitr(ssp); 801 __ssp_transmit_char(ssp, ch); 802 } 803 804 static void sifive_serial_console_write(struct console *co, const char *s, 805 unsigned int count) 806 { 807 struct sifive_serial_port *ssp = sifive_serial_console_ports[co->index]; 808 unsigned long flags; 809 unsigned int ier; 810 int locked = 1; 811 812 if (!ssp) 813 return; 814 815 local_irq_save(flags); 816 if (ssp->port.sysrq) 817 locked = 0; 818 else if (oops_in_progress) 819 locked = spin_trylock(&ssp->port.lock); 820 else 821 spin_lock(&ssp->port.lock); 822 823 ier = __ssp_readl(ssp, SIFIVE_SERIAL_IE_OFFS); 824 __ssp_writel(0, SIFIVE_SERIAL_IE_OFFS, ssp); 825 826 uart_console_write(&ssp->port, s, count, sifive_serial_console_putchar); 827 828 __ssp_writel(ier, SIFIVE_SERIAL_IE_OFFS, ssp); 829 830 if (locked) 831 spin_unlock(&ssp->port.lock); 832 local_irq_restore(flags); 833 } 834 835 static int __init sifive_serial_console_setup(struct console *co, char *options) 836 { 837 struct sifive_serial_port *ssp; 838 int baud = SIFIVE_DEFAULT_BAUD_RATE; 839 int bits = 8; 840 int parity = 'n'; 841 int flow = 'n'; 842 843 if (co->index < 0 || co->index >= SIFIVE_SERIAL_MAX_PORTS) 844 return -ENODEV; 845 846 ssp = sifive_serial_console_ports[co->index]; 847 if (!ssp) 848 return -ENODEV; 849 850 if (options) 851 uart_parse_options(options, &baud, &parity, &bits, &flow); 852 853 return uart_set_options(&ssp->port, co, baud, parity, bits, flow); 854 } 855 856 static struct uart_driver sifive_serial_uart_driver; 857 858 static struct console sifive_serial_console = { 859 .name = SIFIVE_TTY_PREFIX, 860 .write = sifive_serial_console_write, 861 .device = uart_console_device, 862 .setup = sifive_serial_console_setup, 863 .flags = CON_PRINTBUFFER, 864 .index = -1, 865 .data = &sifive_serial_uart_driver, 866 }; 867 868 static int __init sifive_console_init(void) 869 { 870 register_console(&sifive_serial_console); 871 return 0; 872 } 873 874 console_initcall(sifive_console_init); 875 876 static void __ssp_add_console_port(struct sifive_serial_port *ssp) 877 { 878 sifive_serial_console_ports[ssp->port.line] = ssp; 879 } 880 881 static void __ssp_remove_console_port(struct sifive_serial_port *ssp) 882 { 883 sifive_serial_console_ports[ssp->port.line] = NULL; 884 } 885 886 #define SIFIVE_SERIAL_CONSOLE (&sifive_serial_console) 887 888 #else 889 890 #define SIFIVE_SERIAL_CONSOLE NULL 891 892 static void __ssp_add_console_port(struct sifive_serial_port *ssp) 893 {} 894 static void __ssp_remove_console_port(struct sifive_serial_port *ssp) 895 {} 896 897 #endif 898 899 static const struct uart_ops sifive_serial_uops = { 900 .tx_empty = sifive_serial_tx_empty, 901 .set_mctrl = sifive_serial_set_mctrl, 902 .get_mctrl = sifive_serial_get_mctrl, 903 .stop_tx = sifive_serial_stop_tx, 904 .start_tx = sifive_serial_start_tx, 905 .stop_rx = sifive_serial_stop_rx, 906 .break_ctl = sifive_serial_break_ctl, 907 .startup = sifive_serial_startup, 908 .shutdown = sifive_serial_shutdown, 909 .set_termios = sifive_serial_set_termios, 910 .type = sifive_serial_type, 911 .release_port = sifive_serial_release_port, 912 .request_port = sifive_serial_request_port, 913 .config_port = sifive_serial_config_port, 914 .verify_port = sifive_serial_verify_port, 915 #ifdef CONFIG_CONSOLE_POLL 916 .poll_get_char = sifive_serial_poll_get_char, 917 .poll_put_char = sifive_serial_poll_put_char, 918 #endif 919 }; 920 921 static struct uart_driver sifive_serial_uart_driver = { 922 .owner = THIS_MODULE, 923 .driver_name = SIFIVE_SERIAL_NAME, 924 .dev_name = SIFIVE_TTY_PREFIX, 925 .nr = SIFIVE_SERIAL_MAX_PORTS, 926 .cons = SIFIVE_SERIAL_CONSOLE, 927 }; 928 929 static int sifive_serial_probe(struct platform_device *pdev) 930 { 931 struct sifive_serial_port *ssp; 932 struct resource *mem; 933 struct clk *clk; 934 void __iomem *base; 935 int irq, id, r; 936 937 irq = platform_get_irq(pdev, 0); 938 if (irq < 0) 939 return -EPROBE_DEFER; 940 941 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0); 942 base = devm_ioremap_resource(&pdev->dev, mem); 943 if (IS_ERR(base)) { 944 dev_err(&pdev->dev, "could not acquire device memory\n"); 945 return PTR_ERR(base); 946 } 947 948 clk = devm_clk_get(&pdev->dev, NULL); 949 if (IS_ERR(clk)) { 950 dev_err(&pdev->dev, "unable to find controller clock\n"); 951 return PTR_ERR(clk); 952 } 953 954 id = of_alias_get_id(pdev->dev.of_node, "serial"); 955 if (id < 0) { 956 dev_err(&pdev->dev, "missing aliases entry\n"); 957 return id; 958 } 959 960 #ifdef CONFIG_SERIAL_SIFIVE_CONSOLE 961 if (id > SIFIVE_SERIAL_MAX_PORTS) { 962 dev_err(&pdev->dev, "too many UARTs (%d)\n", id); 963 return -EINVAL; 964 } 965 #endif 966 967 ssp = devm_kzalloc(&pdev->dev, sizeof(*ssp), GFP_KERNEL); 968 if (!ssp) 969 return -ENOMEM; 970 971 ssp->port.dev = &pdev->dev; 972 ssp->port.type = PORT_SIFIVE_V0; 973 ssp->port.iotype = UPIO_MEM; 974 ssp->port.irq = irq; 975 ssp->port.fifosize = SIFIVE_TX_FIFO_DEPTH; 976 ssp->port.ops = &sifive_serial_uops; 977 ssp->port.line = id; 978 ssp->port.mapbase = mem->start; 979 ssp->port.membase = base; 980 ssp->dev = &pdev->dev; 981 ssp->clk = clk; 982 ssp->clk_notifier.notifier_call = sifive_serial_clk_notifier; 983 984 r = clk_notifier_register(ssp->clk, &ssp->clk_notifier); 985 if (r) { 986 dev_err(&pdev->dev, "could not register clock notifier: %d\n", 987 r); 988 goto probe_out1; 989 } 990 991 /* Set up clock divider */ 992 ssp->port.uartclk = clk_get_rate(ssp->clk); 993 ssp->baud_rate = SIFIVE_DEFAULT_BAUD_RATE; 994 __ssp_update_div(ssp); 995 996 platform_set_drvdata(pdev, ssp); 997 998 /* Enable transmits and set the watermark level to 1 */ 999 __ssp_writel((1 << SIFIVE_SERIAL_TXCTRL_TXCNT_SHIFT) | 1000 SIFIVE_SERIAL_TXCTRL_TXEN_MASK, 1001 SIFIVE_SERIAL_TXCTRL_OFFS, ssp); 1002 1003 /* Enable receives and set the watermark level to 0 */ 1004 __ssp_writel((0 << SIFIVE_SERIAL_RXCTRL_RXCNT_SHIFT) | 1005 SIFIVE_SERIAL_RXCTRL_RXEN_MASK, 1006 SIFIVE_SERIAL_RXCTRL_OFFS, ssp); 1007 1008 r = request_irq(ssp->port.irq, sifive_serial_irq, ssp->port.irqflags, 1009 dev_name(&pdev->dev), ssp); 1010 if (r) { 1011 dev_err(&pdev->dev, "could not attach interrupt: %d\n", r); 1012 goto probe_out2; 1013 } 1014 1015 __ssp_add_console_port(ssp); 1016 1017 r = uart_add_one_port(&sifive_serial_uart_driver, &ssp->port); 1018 if (r != 0) { 1019 dev_err(&pdev->dev, "could not add uart: %d\n", r); 1020 goto probe_out3; 1021 } 1022 1023 return 0; 1024 1025 probe_out3: 1026 __ssp_remove_console_port(ssp); 1027 free_irq(ssp->port.irq, ssp); 1028 probe_out2: 1029 clk_notifier_unregister(ssp->clk, &ssp->clk_notifier); 1030 probe_out1: 1031 return r; 1032 } 1033 1034 static int sifive_serial_remove(struct platform_device *dev) 1035 { 1036 struct sifive_serial_port *ssp = platform_get_drvdata(dev); 1037 1038 __ssp_remove_console_port(ssp); 1039 uart_remove_one_port(&sifive_serial_uart_driver, &ssp->port); 1040 free_irq(ssp->port.irq, ssp); 1041 clk_notifier_unregister(ssp->clk, &ssp->clk_notifier); 1042 1043 return 0; 1044 } 1045 1046 static const struct of_device_id sifive_serial_of_match[] = { 1047 { .compatible = "sifive,fu540-c000-uart0" }, 1048 { .compatible = "sifive,uart0" }, 1049 {}, 1050 }; 1051 MODULE_DEVICE_TABLE(of, sifive_serial_of_match); 1052 1053 static struct platform_driver sifive_serial_platform_driver = { 1054 .probe = sifive_serial_probe, 1055 .remove = sifive_serial_remove, 1056 .driver = { 1057 .name = SIFIVE_SERIAL_NAME, 1058 .of_match_table = of_match_ptr(sifive_serial_of_match), 1059 }, 1060 }; 1061 1062 static int __init sifive_serial_init(void) 1063 { 1064 int r; 1065 1066 r = uart_register_driver(&sifive_serial_uart_driver); 1067 if (r) 1068 goto init_out1; 1069 1070 r = platform_driver_register(&sifive_serial_platform_driver); 1071 if (r) 1072 goto init_out2; 1073 1074 return 0; 1075 1076 init_out2: 1077 uart_unregister_driver(&sifive_serial_uart_driver); 1078 init_out1: 1079 return r; 1080 } 1081 1082 static void __exit sifive_serial_exit(void) 1083 { 1084 platform_driver_unregister(&sifive_serial_platform_driver); 1085 uart_unregister_driver(&sifive_serial_uart_driver); 1086 } 1087 1088 module_init(sifive_serial_init); 1089 module_exit(sifive_serial_exit); 1090 1091 MODULE_DESCRIPTION("SiFive UART serial driver"); 1092 MODULE_LICENSE("GPL"); 1093 MODULE_AUTHOR("Paul Walmsley <paul@pwsan.com>"); 1094