Maxime Coquelin | 48a6092 | 2015-06-10 21:19:36 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) Maxime Coquelin 2015 |
Alexandre TORGUE | ada8618 | 2016-09-15 18:42:33 +0200 | [diff] [blame] | 3 | * Authors: Maxime Coquelin <mcoquelin.stm32@gmail.com> |
| 4 | * Gerald Baeza <gerald.baeza@st.com> |
Maxime Coquelin | 48a6092 | 2015-06-10 21:19:36 +0200 | [diff] [blame] | 5 | * License terms: GNU General Public License (GPL), version 2 |
| 6 | * |
| 7 | * Inspired by st-asc.c from STMicroelectronics (c) |
| 8 | */ |
| 9 | |
Maxime Coquelin | 6b596a8 | 2015-06-16 11:12:19 +0200 | [diff] [blame] | 10 | #if defined(CONFIG_SERIAL_STM32_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ) |
Maxime Coquelin | 48a6092 | 2015-06-10 21:19:36 +0200 | [diff] [blame] | 11 | #define SUPPORT_SYSRQ |
| 12 | #endif |
| 13 | |
| 14 | #include <linux/module.h> |
| 15 | #include <linux/serial.h> |
| 16 | #include <linux/console.h> |
| 17 | #include <linux/sysrq.h> |
| 18 | #include <linux/platform_device.h> |
| 19 | #include <linux/io.h> |
| 20 | #include <linux/irq.h> |
| 21 | #include <linux/tty.h> |
| 22 | #include <linux/tty_flip.h> |
| 23 | #include <linux/delay.h> |
| 24 | #include <linux/spinlock.h> |
| 25 | #include <linux/pm_runtime.h> |
| 26 | #include <linux/of.h> |
| 27 | #include <linux/of_platform.h> |
| 28 | #include <linux/serial_core.h> |
| 29 | #include <linux/clk.h> |
| 30 | |
Alexandre TORGUE | bc5a0b5 | 2016-09-15 18:42:35 +0200 | [diff] [blame] | 31 | #include "stm32-usart.h" |
Maxime Coquelin | 48a6092 | 2015-06-10 21:19:36 +0200 | [diff] [blame] | 32 | |
| 33 | static void stm32_stop_tx(struct uart_port *port); |
| 34 | |
| 35 | static inline struct stm32_port *to_stm32_port(struct uart_port *port) |
| 36 | { |
| 37 | return container_of(port, struct stm32_port, port); |
| 38 | } |
| 39 | |
| 40 | static void stm32_set_bits(struct uart_port *port, u32 reg, u32 bits) |
| 41 | { |
| 42 | u32 val; |
| 43 | |
| 44 | val = readl_relaxed(port->membase + reg); |
| 45 | val |= bits; |
| 46 | writel_relaxed(val, port->membase + reg); |
| 47 | } |
| 48 | |
| 49 | static void stm32_clr_bits(struct uart_port *port, u32 reg, u32 bits) |
| 50 | { |
| 51 | u32 val; |
| 52 | |
| 53 | val = readl_relaxed(port->membase + reg); |
| 54 | val &= ~bits; |
| 55 | writel_relaxed(val, port->membase + reg); |
| 56 | } |
| 57 | |
| 58 | static void stm32_receive_chars(struct uart_port *port) |
| 59 | { |
| 60 | struct tty_port *tport = &port->state->port; |
Alexandre TORGUE | ada8618 | 2016-09-15 18:42:33 +0200 | [diff] [blame] | 61 | struct stm32_port *stm32_port = to_stm32_port(port); |
| 62 | struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; |
Maxime Coquelin | 48a6092 | 2015-06-10 21:19:36 +0200 | [diff] [blame] | 63 | unsigned long c; |
| 64 | u32 sr; |
| 65 | char flag; |
| 66 | |
| 67 | if (port->irq_wake) |
| 68 | pm_wakeup_event(tport->tty->dev, 0); |
| 69 | |
Alexandre TORGUE | ada8618 | 2016-09-15 18:42:33 +0200 | [diff] [blame] | 70 | while ((sr = readl_relaxed(port->membase + ofs->isr)) & USART_SR_RXNE) { |
Maxime Coquelin | 48a6092 | 2015-06-10 21:19:36 +0200 | [diff] [blame] | 71 | sr |= USART_SR_DUMMY_RX; |
Alexandre TORGUE | ada8618 | 2016-09-15 18:42:33 +0200 | [diff] [blame] | 72 | c = readl_relaxed(port->membase + ofs->rdr); |
Maxime Coquelin | 48a6092 | 2015-06-10 21:19:36 +0200 | [diff] [blame] | 73 | flag = TTY_NORMAL; |
| 74 | port->icount.rx++; |
| 75 | |
| 76 | if (sr & USART_SR_ERR_MASK) { |
| 77 | if (sr & USART_SR_LBD) { |
| 78 | port->icount.brk++; |
| 79 | if (uart_handle_break(port)) |
| 80 | continue; |
| 81 | } else if (sr & USART_SR_ORE) { |
Alexandre TORGUE | ada8618 | 2016-09-15 18:42:33 +0200 | [diff] [blame] | 82 | if (ofs->icr != UNDEF_REG) |
| 83 | writel_relaxed(USART_ICR_ORECF, |
| 84 | port->membase + |
| 85 | ofs->icr); |
Maxime Coquelin | 48a6092 | 2015-06-10 21:19:36 +0200 | [diff] [blame] | 86 | port->icount.overrun++; |
| 87 | } else if (sr & USART_SR_PE) { |
| 88 | port->icount.parity++; |
| 89 | } else if (sr & USART_SR_FE) { |
| 90 | port->icount.frame++; |
| 91 | } |
| 92 | |
| 93 | sr &= port->read_status_mask; |
| 94 | |
| 95 | if (sr & USART_SR_LBD) |
| 96 | flag = TTY_BREAK; |
| 97 | else if (sr & USART_SR_PE) |
| 98 | flag = TTY_PARITY; |
| 99 | else if (sr & USART_SR_FE) |
| 100 | flag = TTY_FRAME; |
| 101 | } |
| 102 | |
| 103 | if (uart_handle_sysrq_char(port, c)) |
| 104 | continue; |
| 105 | uart_insert_char(port, sr, USART_SR_ORE, c, flag); |
| 106 | } |
| 107 | |
| 108 | spin_unlock(&port->lock); |
| 109 | tty_flip_buffer_push(tport); |
| 110 | spin_lock(&port->lock); |
| 111 | } |
| 112 | |
| 113 | static void stm32_transmit_chars(struct uart_port *port) |
| 114 | { |
Alexandre TORGUE | ada8618 | 2016-09-15 18:42:33 +0200 | [diff] [blame] | 115 | struct stm32_port *stm32_port = to_stm32_port(port); |
| 116 | struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; |
Maxime Coquelin | 48a6092 | 2015-06-10 21:19:36 +0200 | [diff] [blame] | 117 | struct circ_buf *xmit = &port->state->xmit; |
| 118 | |
| 119 | if (port->x_char) { |
Alexandre TORGUE | ada8618 | 2016-09-15 18:42:33 +0200 | [diff] [blame] | 120 | writel_relaxed(port->x_char, port->membase + ofs->tdr); |
Maxime Coquelin | 48a6092 | 2015-06-10 21:19:36 +0200 | [diff] [blame] | 121 | port->x_char = 0; |
| 122 | port->icount.tx++; |
| 123 | return; |
| 124 | } |
| 125 | |
| 126 | if (uart_tx_stopped(port)) { |
| 127 | stm32_stop_tx(port); |
| 128 | return; |
| 129 | } |
| 130 | |
| 131 | if (uart_circ_empty(xmit)) { |
| 132 | stm32_stop_tx(port); |
| 133 | return; |
| 134 | } |
| 135 | |
Alexandre TORGUE | ada8618 | 2016-09-15 18:42:33 +0200 | [diff] [blame] | 136 | writel_relaxed(xmit->buf[xmit->tail], port->membase + ofs->tdr); |
Maxime Coquelin | 48a6092 | 2015-06-10 21:19:36 +0200 | [diff] [blame] | 137 | xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1); |
| 138 | port->icount.tx++; |
| 139 | |
| 140 | if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS) |
| 141 | uart_write_wakeup(port); |
| 142 | |
| 143 | if (uart_circ_empty(xmit)) |
| 144 | stm32_stop_tx(port); |
| 145 | } |
| 146 | |
| 147 | static irqreturn_t stm32_interrupt(int irq, void *ptr) |
| 148 | { |
| 149 | struct uart_port *port = ptr; |
Alexandre TORGUE | ada8618 | 2016-09-15 18:42:33 +0200 | [diff] [blame] | 150 | struct stm32_port *stm32_port = to_stm32_port(port); |
| 151 | struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; |
Maxime Coquelin | 48a6092 | 2015-06-10 21:19:36 +0200 | [diff] [blame] | 152 | u32 sr; |
| 153 | |
| 154 | spin_lock(&port->lock); |
| 155 | |
Alexandre TORGUE | ada8618 | 2016-09-15 18:42:33 +0200 | [diff] [blame] | 156 | sr = readl_relaxed(port->membase + ofs->isr); |
Maxime Coquelin | 48a6092 | 2015-06-10 21:19:36 +0200 | [diff] [blame] | 157 | |
| 158 | if (sr & USART_SR_RXNE) |
| 159 | stm32_receive_chars(port); |
| 160 | |
| 161 | if (sr & USART_SR_TXE) |
| 162 | stm32_transmit_chars(port); |
| 163 | |
| 164 | spin_unlock(&port->lock); |
| 165 | |
| 166 | return IRQ_HANDLED; |
| 167 | } |
| 168 | |
| 169 | static unsigned int stm32_tx_empty(struct uart_port *port) |
| 170 | { |
Alexandre TORGUE | ada8618 | 2016-09-15 18:42:33 +0200 | [diff] [blame] | 171 | struct stm32_port *stm32_port = to_stm32_port(port); |
| 172 | struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; |
| 173 | |
| 174 | return readl_relaxed(port->membase + ofs->isr) & USART_SR_TXE; |
Maxime Coquelin | 48a6092 | 2015-06-10 21:19:36 +0200 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | static void stm32_set_mctrl(struct uart_port *port, unsigned int mctrl) |
| 178 | { |
Alexandre TORGUE | ada8618 | 2016-09-15 18:42:33 +0200 | [diff] [blame] | 179 | struct stm32_port *stm32_port = to_stm32_port(port); |
| 180 | struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; |
| 181 | |
Maxime Coquelin | 48a6092 | 2015-06-10 21:19:36 +0200 | [diff] [blame] | 182 | if ((mctrl & TIOCM_RTS) && (port->status & UPSTAT_AUTORTS)) |
Alexandre TORGUE | ada8618 | 2016-09-15 18:42:33 +0200 | [diff] [blame] | 183 | stm32_set_bits(port, ofs->cr3, USART_CR3_RTSE); |
Maxime Coquelin | 48a6092 | 2015-06-10 21:19:36 +0200 | [diff] [blame] | 184 | else |
Alexandre TORGUE | ada8618 | 2016-09-15 18:42:33 +0200 | [diff] [blame] | 185 | stm32_clr_bits(port, ofs->cr3, USART_CR3_RTSE); |
Maxime Coquelin | 48a6092 | 2015-06-10 21:19:36 +0200 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | static unsigned int stm32_get_mctrl(struct uart_port *port) |
| 189 | { |
| 190 | /* This routine is used to get signals of: DCD, DSR, RI, and CTS */ |
| 191 | return TIOCM_CAR | TIOCM_DSR | TIOCM_CTS; |
| 192 | } |
| 193 | |
| 194 | /* Transmit stop */ |
| 195 | static void stm32_stop_tx(struct uart_port *port) |
| 196 | { |
Alexandre TORGUE | ada8618 | 2016-09-15 18:42:33 +0200 | [diff] [blame] | 197 | struct stm32_port *stm32_port = to_stm32_port(port); |
| 198 | struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; |
| 199 | |
| 200 | stm32_clr_bits(port, ofs->cr1, USART_CR1_TXEIE); |
Maxime Coquelin | 48a6092 | 2015-06-10 21:19:36 +0200 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | /* There are probably characters waiting to be transmitted. */ |
| 204 | static void stm32_start_tx(struct uart_port *port) |
| 205 | { |
Alexandre TORGUE | ada8618 | 2016-09-15 18:42:33 +0200 | [diff] [blame] | 206 | struct stm32_port *stm32_port = to_stm32_port(port); |
| 207 | struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; |
Maxime Coquelin | 48a6092 | 2015-06-10 21:19:36 +0200 | [diff] [blame] | 208 | struct circ_buf *xmit = &port->state->xmit; |
| 209 | |
| 210 | if (uart_circ_empty(xmit)) |
| 211 | return; |
| 212 | |
Alexandre TORGUE | ada8618 | 2016-09-15 18:42:33 +0200 | [diff] [blame] | 213 | stm32_set_bits(port, ofs->cr1, USART_CR1_TXEIE | USART_CR1_TE); |
Maxime Coquelin | 48a6092 | 2015-06-10 21:19:36 +0200 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | /* Throttle the remote when input buffer is about to overflow. */ |
| 217 | static void stm32_throttle(struct uart_port *port) |
| 218 | { |
Alexandre TORGUE | ada8618 | 2016-09-15 18:42:33 +0200 | [diff] [blame] | 219 | struct stm32_port *stm32_port = to_stm32_port(port); |
| 220 | struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; |
Maxime Coquelin | 48a6092 | 2015-06-10 21:19:36 +0200 | [diff] [blame] | 221 | unsigned long flags; |
| 222 | |
| 223 | spin_lock_irqsave(&port->lock, flags); |
Alexandre TORGUE | ada8618 | 2016-09-15 18:42:33 +0200 | [diff] [blame] | 224 | stm32_clr_bits(port, ofs->cr1, USART_CR1_RXNEIE); |
Maxime Coquelin | 48a6092 | 2015-06-10 21:19:36 +0200 | [diff] [blame] | 225 | spin_unlock_irqrestore(&port->lock, flags); |
| 226 | } |
| 227 | |
| 228 | /* Unthrottle the remote, the input buffer can now accept data. */ |
| 229 | static void stm32_unthrottle(struct uart_port *port) |
| 230 | { |
Alexandre TORGUE | ada8618 | 2016-09-15 18:42:33 +0200 | [diff] [blame] | 231 | struct stm32_port *stm32_port = to_stm32_port(port); |
| 232 | struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; |
Maxime Coquelin | 48a6092 | 2015-06-10 21:19:36 +0200 | [diff] [blame] | 233 | unsigned long flags; |
| 234 | |
| 235 | spin_lock_irqsave(&port->lock, flags); |
Alexandre TORGUE | ada8618 | 2016-09-15 18:42:33 +0200 | [diff] [blame] | 236 | stm32_set_bits(port, ofs->cr1, USART_CR1_RXNEIE); |
Maxime Coquelin | 48a6092 | 2015-06-10 21:19:36 +0200 | [diff] [blame] | 237 | spin_unlock_irqrestore(&port->lock, flags); |
| 238 | } |
| 239 | |
| 240 | /* Receive stop */ |
| 241 | static void stm32_stop_rx(struct uart_port *port) |
| 242 | { |
Alexandre TORGUE | ada8618 | 2016-09-15 18:42:33 +0200 | [diff] [blame] | 243 | struct stm32_port *stm32_port = to_stm32_port(port); |
| 244 | struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; |
| 245 | |
| 246 | stm32_clr_bits(port, ofs->cr1, USART_CR1_RXNEIE); |
Maxime Coquelin | 48a6092 | 2015-06-10 21:19:36 +0200 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | /* Handle breaks - ignored by us */ |
| 250 | static void stm32_break_ctl(struct uart_port *port, int break_state) |
| 251 | { |
| 252 | } |
| 253 | |
| 254 | static int stm32_startup(struct uart_port *port) |
| 255 | { |
Alexandre TORGUE | ada8618 | 2016-09-15 18:42:33 +0200 | [diff] [blame] | 256 | struct stm32_port *stm32_port = to_stm32_port(port); |
| 257 | struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; |
Maxime Coquelin | 48a6092 | 2015-06-10 21:19:36 +0200 | [diff] [blame] | 258 | const char *name = to_platform_device(port->dev)->name; |
| 259 | u32 val; |
| 260 | int ret; |
| 261 | |
Sudeep Holla | 616ea8d | 2015-09-21 16:47:06 +0100 | [diff] [blame] | 262 | ret = request_irq(port->irq, stm32_interrupt, 0, name, port); |
Maxime Coquelin | 48a6092 | 2015-06-10 21:19:36 +0200 | [diff] [blame] | 263 | if (ret) |
| 264 | return ret; |
| 265 | |
| 266 | val = USART_CR1_RXNEIE | USART_CR1_TE | USART_CR1_RE; |
Alexandre TORGUE | ada8618 | 2016-09-15 18:42:33 +0200 | [diff] [blame] | 267 | stm32_set_bits(port, ofs->cr1, val); |
Maxime Coquelin | 48a6092 | 2015-06-10 21:19:36 +0200 | [diff] [blame] | 268 | |
| 269 | return 0; |
| 270 | } |
| 271 | |
| 272 | static void stm32_shutdown(struct uart_port *port) |
| 273 | { |
Alexandre TORGUE | ada8618 | 2016-09-15 18:42:33 +0200 | [diff] [blame] | 274 | struct stm32_port *stm32_port = to_stm32_port(port); |
| 275 | struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; |
Maxime Coquelin | 48a6092 | 2015-06-10 21:19:36 +0200 | [diff] [blame] | 276 | u32 val; |
| 277 | |
| 278 | val = USART_CR1_TXEIE | USART_CR1_RXNEIE | USART_CR1_TE | USART_CR1_RE; |
Alexandre TORGUE | a14f66a | 2016-09-15 18:42:36 +0200 | [diff] [blame] | 279 | stm32_clr_bits(port, ofs->cr1, val); |
Maxime Coquelin | 48a6092 | 2015-06-10 21:19:36 +0200 | [diff] [blame] | 280 | |
| 281 | free_irq(port->irq, port); |
| 282 | } |
| 283 | |
| 284 | static void stm32_set_termios(struct uart_port *port, struct ktermios *termios, |
| 285 | struct ktermios *old) |
| 286 | { |
| 287 | struct stm32_port *stm32_port = to_stm32_port(port); |
Alexandre TORGUE | ada8618 | 2016-09-15 18:42:33 +0200 | [diff] [blame] | 288 | struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; |
| 289 | struct stm32_usart_config *cfg = &stm32_port->info->cfg; |
Maxime Coquelin | 48a6092 | 2015-06-10 21:19:36 +0200 | [diff] [blame] | 290 | unsigned int baud; |
| 291 | u32 usartdiv, mantissa, fraction, oversampling; |
| 292 | tcflag_t cflag = termios->c_cflag; |
| 293 | u32 cr1, cr2, cr3; |
| 294 | unsigned long flags; |
| 295 | |
| 296 | if (!stm32_port->hw_flow_control) |
| 297 | cflag &= ~CRTSCTS; |
| 298 | |
| 299 | baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk / 8); |
| 300 | |
| 301 | spin_lock_irqsave(&port->lock, flags); |
| 302 | |
| 303 | /* Stop serial port and reset value */ |
Alexandre TORGUE | ada8618 | 2016-09-15 18:42:33 +0200 | [diff] [blame] | 304 | writel_relaxed(0, port->membase + ofs->cr1); |
Maxime Coquelin | 48a6092 | 2015-06-10 21:19:36 +0200 | [diff] [blame] | 305 | |
Alexandre TORGUE | ada8618 | 2016-09-15 18:42:33 +0200 | [diff] [blame] | 306 | cr1 = USART_CR1_TE | USART_CR1_RE | USART_CR1_RXNEIE; |
| 307 | cr1 |= BIT(cfg->uart_enable_bit); |
Maxime Coquelin | 48a6092 | 2015-06-10 21:19:36 +0200 | [diff] [blame] | 308 | cr2 = 0; |
| 309 | cr3 = 0; |
| 310 | |
| 311 | if (cflag & CSTOPB) |
| 312 | cr2 |= USART_CR2_STOP_2B; |
| 313 | |
| 314 | if (cflag & PARENB) { |
| 315 | cr1 |= USART_CR1_PCE; |
Alexandre TORGUE | ada8618 | 2016-09-15 18:42:33 +0200 | [diff] [blame] | 316 | if ((cflag & CSIZE) == CS8) { |
| 317 | if (cfg->has_7bits_data) |
| 318 | cr1 |= USART_CR1_M0; |
| 319 | else |
| 320 | cr1 |= USART_CR1_M; |
| 321 | } |
Maxime Coquelin | 48a6092 | 2015-06-10 21:19:36 +0200 | [diff] [blame] | 322 | } |
| 323 | |
| 324 | if (cflag & PARODD) |
| 325 | cr1 |= USART_CR1_PS; |
| 326 | |
| 327 | port->status &= ~(UPSTAT_AUTOCTS | UPSTAT_AUTORTS); |
| 328 | if (cflag & CRTSCTS) { |
| 329 | port->status |= UPSTAT_AUTOCTS | UPSTAT_AUTORTS; |
| 330 | cr3 |= USART_CR3_CTSE; |
| 331 | } |
| 332 | |
| 333 | usartdiv = DIV_ROUND_CLOSEST(port->uartclk, baud); |
| 334 | |
| 335 | /* |
| 336 | * The USART supports 16 or 8 times oversampling. |
| 337 | * By default we prefer 16 times oversampling, so that the receiver |
| 338 | * has a better tolerance to clock deviations. |
| 339 | * 8 times oversampling is only used to achieve higher speeds. |
| 340 | */ |
| 341 | if (usartdiv < 16) { |
| 342 | oversampling = 8; |
Alexandre TORGUE | ada8618 | 2016-09-15 18:42:33 +0200 | [diff] [blame] | 343 | stm32_set_bits(port, ofs->cr1, USART_CR1_OVER8); |
Maxime Coquelin | 48a6092 | 2015-06-10 21:19:36 +0200 | [diff] [blame] | 344 | } else { |
| 345 | oversampling = 16; |
Alexandre TORGUE | ada8618 | 2016-09-15 18:42:33 +0200 | [diff] [blame] | 346 | stm32_clr_bits(port, ofs->cr1, USART_CR1_OVER8); |
Maxime Coquelin | 48a6092 | 2015-06-10 21:19:36 +0200 | [diff] [blame] | 347 | } |
| 348 | |
| 349 | mantissa = (usartdiv / oversampling) << USART_BRR_DIV_M_SHIFT; |
| 350 | fraction = usartdiv % oversampling; |
Alexandre TORGUE | ada8618 | 2016-09-15 18:42:33 +0200 | [diff] [blame] | 351 | writel_relaxed(mantissa | fraction, port->membase + ofs->brr); |
Maxime Coquelin | 48a6092 | 2015-06-10 21:19:36 +0200 | [diff] [blame] | 352 | |
| 353 | uart_update_timeout(port, cflag, baud); |
| 354 | |
| 355 | port->read_status_mask = USART_SR_ORE; |
| 356 | if (termios->c_iflag & INPCK) |
| 357 | port->read_status_mask |= USART_SR_PE | USART_SR_FE; |
| 358 | if (termios->c_iflag & (IGNBRK | BRKINT | PARMRK)) |
| 359 | port->read_status_mask |= USART_SR_LBD; |
| 360 | |
| 361 | /* Characters to ignore */ |
| 362 | port->ignore_status_mask = 0; |
| 363 | if (termios->c_iflag & IGNPAR) |
| 364 | port->ignore_status_mask = USART_SR_PE | USART_SR_FE; |
| 365 | if (termios->c_iflag & IGNBRK) { |
| 366 | port->ignore_status_mask |= USART_SR_LBD; |
| 367 | /* |
| 368 | * If we're ignoring parity and break indicators, |
| 369 | * ignore overruns too (for real raw support). |
| 370 | */ |
| 371 | if (termios->c_iflag & IGNPAR) |
| 372 | port->ignore_status_mask |= USART_SR_ORE; |
| 373 | } |
| 374 | |
| 375 | /* Ignore all characters if CREAD is not set */ |
| 376 | if ((termios->c_cflag & CREAD) == 0) |
| 377 | port->ignore_status_mask |= USART_SR_DUMMY_RX; |
| 378 | |
Alexandre TORGUE | ada8618 | 2016-09-15 18:42:33 +0200 | [diff] [blame] | 379 | writel_relaxed(cr3, port->membase + ofs->cr3); |
| 380 | writel_relaxed(cr2, port->membase + ofs->cr2); |
| 381 | writel_relaxed(cr1, port->membase + ofs->cr1); |
Maxime Coquelin | 48a6092 | 2015-06-10 21:19:36 +0200 | [diff] [blame] | 382 | |
| 383 | spin_unlock_irqrestore(&port->lock, flags); |
| 384 | } |
| 385 | |
| 386 | static const char *stm32_type(struct uart_port *port) |
| 387 | { |
| 388 | return (port->type == PORT_STM32) ? DRIVER_NAME : NULL; |
| 389 | } |
| 390 | |
| 391 | static void stm32_release_port(struct uart_port *port) |
| 392 | { |
| 393 | } |
| 394 | |
| 395 | static int stm32_request_port(struct uart_port *port) |
| 396 | { |
| 397 | return 0; |
| 398 | } |
| 399 | |
| 400 | static void stm32_config_port(struct uart_port *port, int flags) |
| 401 | { |
| 402 | if (flags & UART_CONFIG_TYPE) |
| 403 | port->type = PORT_STM32; |
| 404 | } |
| 405 | |
| 406 | static int |
| 407 | stm32_verify_port(struct uart_port *port, struct serial_struct *ser) |
| 408 | { |
| 409 | /* No user changeable parameters */ |
| 410 | return -EINVAL; |
| 411 | } |
| 412 | |
| 413 | static void stm32_pm(struct uart_port *port, unsigned int state, |
| 414 | unsigned int oldstate) |
| 415 | { |
| 416 | struct stm32_port *stm32port = container_of(port, |
| 417 | struct stm32_port, port); |
Alexandre TORGUE | ada8618 | 2016-09-15 18:42:33 +0200 | [diff] [blame] | 418 | struct stm32_usart_offsets *ofs = &stm32port->info->ofs; |
| 419 | struct stm32_usart_config *cfg = &stm32port->info->cfg; |
Maxime Coquelin | 48a6092 | 2015-06-10 21:19:36 +0200 | [diff] [blame] | 420 | unsigned long flags = 0; |
| 421 | |
| 422 | switch (state) { |
| 423 | case UART_PM_STATE_ON: |
| 424 | clk_prepare_enable(stm32port->clk); |
| 425 | break; |
| 426 | case UART_PM_STATE_OFF: |
| 427 | spin_lock_irqsave(&port->lock, flags); |
Alexandre TORGUE | ada8618 | 2016-09-15 18:42:33 +0200 | [diff] [blame] | 428 | stm32_clr_bits(port, ofs->cr1, BIT(cfg->uart_enable_bit)); |
Maxime Coquelin | 48a6092 | 2015-06-10 21:19:36 +0200 | [diff] [blame] | 429 | spin_unlock_irqrestore(&port->lock, flags); |
| 430 | clk_disable_unprepare(stm32port->clk); |
| 431 | break; |
| 432 | } |
| 433 | } |
| 434 | |
| 435 | static const struct uart_ops stm32_uart_ops = { |
| 436 | .tx_empty = stm32_tx_empty, |
| 437 | .set_mctrl = stm32_set_mctrl, |
| 438 | .get_mctrl = stm32_get_mctrl, |
| 439 | .stop_tx = stm32_stop_tx, |
| 440 | .start_tx = stm32_start_tx, |
| 441 | .throttle = stm32_throttle, |
| 442 | .unthrottle = stm32_unthrottle, |
| 443 | .stop_rx = stm32_stop_rx, |
| 444 | .break_ctl = stm32_break_ctl, |
| 445 | .startup = stm32_startup, |
| 446 | .shutdown = stm32_shutdown, |
| 447 | .set_termios = stm32_set_termios, |
| 448 | .pm = stm32_pm, |
| 449 | .type = stm32_type, |
| 450 | .release_port = stm32_release_port, |
| 451 | .request_port = stm32_request_port, |
| 452 | .config_port = stm32_config_port, |
| 453 | .verify_port = stm32_verify_port, |
| 454 | }; |
| 455 | |
| 456 | static int stm32_init_port(struct stm32_port *stm32port, |
| 457 | struct platform_device *pdev) |
| 458 | { |
| 459 | struct uart_port *port = &stm32port->port; |
| 460 | struct resource *res; |
| 461 | int ret; |
| 462 | |
| 463 | port->iotype = UPIO_MEM; |
| 464 | port->flags = UPF_BOOT_AUTOCONF; |
| 465 | port->ops = &stm32_uart_ops; |
| 466 | port->dev = &pdev->dev; |
| 467 | port->irq = platform_get_irq(pdev, 0); |
| 468 | |
| 469 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 470 | port->membase = devm_ioremap_resource(&pdev->dev, res); |
| 471 | if (IS_ERR(port->membase)) |
| 472 | return PTR_ERR(port->membase); |
| 473 | port->mapbase = res->start; |
| 474 | |
| 475 | spin_lock_init(&port->lock); |
| 476 | |
| 477 | stm32port->clk = devm_clk_get(&pdev->dev, NULL); |
| 478 | if (IS_ERR(stm32port->clk)) |
| 479 | return PTR_ERR(stm32port->clk); |
| 480 | |
| 481 | /* Ensure that clk rate is correct by enabling the clk */ |
| 482 | ret = clk_prepare_enable(stm32port->clk); |
| 483 | if (ret) |
| 484 | return ret; |
| 485 | |
| 486 | stm32port->port.uartclk = clk_get_rate(stm32port->clk); |
| 487 | if (!stm32port->port.uartclk) |
| 488 | ret = -EINVAL; |
| 489 | |
Maxime Coquelin | 48a6092 | 2015-06-10 21:19:36 +0200 | [diff] [blame] | 490 | return ret; |
| 491 | } |
| 492 | |
| 493 | static struct stm32_port *stm32_of_get_stm32_port(struct platform_device *pdev) |
| 494 | { |
| 495 | struct device_node *np = pdev->dev.of_node; |
| 496 | int id; |
| 497 | |
| 498 | if (!np) |
| 499 | return NULL; |
| 500 | |
| 501 | id = of_alias_get_id(np, "serial"); |
| 502 | if (id < 0) |
| 503 | id = 0; |
| 504 | |
| 505 | if (WARN_ON(id >= STM32_MAX_PORTS)) |
| 506 | return NULL; |
| 507 | |
| 508 | stm32_ports[id].hw_flow_control = of_property_read_bool(np, |
Alexandre TORGUE | 59bed2d | 2016-09-15 18:42:37 +0200 | [diff] [blame] | 509 | "st,hw-flow-ctrl"); |
Maxime Coquelin | 48a6092 | 2015-06-10 21:19:36 +0200 | [diff] [blame] | 510 | stm32_ports[id].port.line = id; |
| 511 | return &stm32_ports[id]; |
| 512 | } |
| 513 | |
| 514 | #ifdef CONFIG_OF |
| 515 | static const struct of_device_id stm32_match[] = { |
Alexandre TORGUE | ada8618 | 2016-09-15 18:42:33 +0200 | [diff] [blame] | 516 | { .compatible = "st,stm32-usart", .data = &stm32f4_info}, |
| 517 | { .compatible = "st,stm32-uart", .data = &stm32f4_info}, |
| 518 | { .compatible = "st,stm32f7-usart", .data = &stm32f7_info}, |
| 519 | { .compatible = "st,stm32f7-uart", .data = &stm32f7_info}, |
Maxime Coquelin | 48a6092 | 2015-06-10 21:19:36 +0200 | [diff] [blame] | 520 | {}, |
| 521 | }; |
| 522 | |
| 523 | MODULE_DEVICE_TABLE(of, stm32_match); |
| 524 | #endif |
| 525 | |
| 526 | static int stm32_serial_probe(struct platform_device *pdev) |
| 527 | { |
Alexandre TORGUE | ada8618 | 2016-09-15 18:42:33 +0200 | [diff] [blame] | 528 | const struct of_device_id *match; |
Maxime Coquelin | 48a6092 | 2015-06-10 21:19:36 +0200 | [diff] [blame] | 529 | struct stm32_port *stm32port; |
Alexandre TORGUE | ada8618 | 2016-09-15 18:42:33 +0200 | [diff] [blame] | 530 | int ret; |
Maxime Coquelin | 48a6092 | 2015-06-10 21:19:36 +0200 | [diff] [blame] | 531 | |
| 532 | stm32port = stm32_of_get_stm32_port(pdev); |
| 533 | if (!stm32port) |
| 534 | return -ENODEV; |
| 535 | |
Alexandre TORGUE | ada8618 | 2016-09-15 18:42:33 +0200 | [diff] [blame] | 536 | match = of_match_device(stm32_match, &pdev->dev); |
| 537 | if (match && match->data) |
| 538 | stm32port->info = (struct stm32_usart_info *)match->data; |
| 539 | else |
| 540 | return -EINVAL; |
| 541 | |
Maxime Coquelin | 48a6092 | 2015-06-10 21:19:36 +0200 | [diff] [blame] | 542 | ret = stm32_init_port(stm32port, pdev); |
| 543 | if (ret) |
| 544 | return ret; |
| 545 | |
| 546 | ret = uart_add_one_port(&stm32_usart_driver, &stm32port->port); |
| 547 | if (ret) |
| 548 | return ret; |
| 549 | |
| 550 | platform_set_drvdata(pdev, &stm32port->port); |
| 551 | |
| 552 | return 0; |
| 553 | } |
| 554 | |
| 555 | static int stm32_serial_remove(struct platform_device *pdev) |
| 556 | { |
| 557 | struct uart_port *port = platform_get_drvdata(pdev); |
Alexandre TORGUE | 511c7b1 | 2016-09-15 18:42:38 +0200 | [diff] [blame] | 558 | struct stm32_port *stm32_port = to_stm32_port(port); |
| 559 | |
| 560 | clk_disable_unprepare(stm32_port->clk); |
Maxime Coquelin | 48a6092 | 2015-06-10 21:19:36 +0200 | [diff] [blame] | 561 | |
| 562 | return uart_remove_one_port(&stm32_usart_driver, port); |
| 563 | } |
| 564 | |
| 565 | |
| 566 | #ifdef CONFIG_SERIAL_STM32_CONSOLE |
| 567 | static void stm32_console_putchar(struct uart_port *port, int ch) |
| 568 | { |
Alexandre TORGUE | ada8618 | 2016-09-15 18:42:33 +0200 | [diff] [blame] | 569 | struct stm32_port *stm32_port = to_stm32_port(port); |
| 570 | struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; |
| 571 | |
| 572 | while (!(readl_relaxed(port->membase + ofs->isr) & USART_SR_TXE)) |
Maxime Coquelin | 48a6092 | 2015-06-10 21:19:36 +0200 | [diff] [blame] | 573 | cpu_relax(); |
| 574 | |
Alexandre TORGUE | ada8618 | 2016-09-15 18:42:33 +0200 | [diff] [blame] | 575 | writel_relaxed(ch, port->membase + ofs->tdr); |
Maxime Coquelin | 48a6092 | 2015-06-10 21:19:36 +0200 | [diff] [blame] | 576 | } |
| 577 | |
| 578 | static void stm32_console_write(struct console *co, const char *s, unsigned cnt) |
| 579 | { |
| 580 | struct uart_port *port = &stm32_ports[co->index].port; |
Alexandre TORGUE | ada8618 | 2016-09-15 18:42:33 +0200 | [diff] [blame] | 581 | struct stm32_port *stm32_port = to_stm32_port(port); |
| 582 | struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; |
Maxime Coquelin | 48a6092 | 2015-06-10 21:19:36 +0200 | [diff] [blame] | 583 | unsigned long flags; |
| 584 | u32 old_cr1, new_cr1; |
| 585 | int locked = 1; |
| 586 | |
| 587 | local_irq_save(flags); |
| 588 | if (port->sysrq) |
| 589 | locked = 0; |
| 590 | else if (oops_in_progress) |
| 591 | locked = spin_trylock(&port->lock); |
| 592 | else |
| 593 | spin_lock(&port->lock); |
| 594 | |
| 595 | /* Save and disable interrupts */ |
Alexandre TORGUE | ada8618 | 2016-09-15 18:42:33 +0200 | [diff] [blame] | 596 | old_cr1 = readl_relaxed(port->membase + ofs->cr1); |
Maxime Coquelin | 48a6092 | 2015-06-10 21:19:36 +0200 | [diff] [blame] | 597 | new_cr1 = old_cr1 & ~USART_CR1_IE_MASK; |
Alexandre TORGUE | ada8618 | 2016-09-15 18:42:33 +0200 | [diff] [blame] | 598 | writel_relaxed(new_cr1, port->membase + ofs->cr1); |
Maxime Coquelin | 48a6092 | 2015-06-10 21:19:36 +0200 | [diff] [blame] | 599 | |
| 600 | uart_console_write(port, s, cnt, stm32_console_putchar); |
| 601 | |
| 602 | /* Restore interrupt state */ |
Alexandre TORGUE | ada8618 | 2016-09-15 18:42:33 +0200 | [diff] [blame] | 603 | writel_relaxed(old_cr1, port->membase + ofs->cr1); |
Maxime Coquelin | 48a6092 | 2015-06-10 21:19:36 +0200 | [diff] [blame] | 604 | |
| 605 | if (locked) |
| 606 | spin_unlock(&port->lock); |
| 607 | local_irq_restore(flags); |
| 608 | } |
| 609 | |
| 610 | static int stm32_console_setup(struct console *co, char *options) |
| 611 | { |
| 612 | struct stm32_port *stm32port; |
| 613 | int baud = 9600; |
| 614 | int bits = 8; |
| 615 | int parity = 'n'; |
| 616 | int flow = 'n'; |
| 617 | |
| 618 | if (co->index >= STM32_MAX_PORTS) |
| 619 | return -ENODEV; |
| 620 | |
| 621 | stm32port = &stm32_ports[co->index]; |
| 622 | |
| 623 | /* |
| 624 | * This driver does not support early console initialization |
| 625 | * (use ARM early printk support instead), so we only expect |
| 626 | * this to be called during the uart port registration when the |
| 627 | * driver gets probed and the port should be mapped at that point. |
| 628 | */ |
| 629 | if (stm32port->port.mapbase == 0 || stm32port->port.membase == NULL) |
| 630 | return -ENXIO; |
| 631 | |
| 632 | if (options) |
| 633 | uart_parse_options(options, &baud, &parity, &bits, &flow); |
| 634 | |
| 635 | return uart_set_options(&stm32port->port, co, baud, parity, bits, flow); |
| 636 | } |
| 637 | |
| 638 | static struct console stm32_console = { |
| 639 | .name = STM32_SERIAL_NAME, |
| 640 | .device = uart_console_device, |
| 641 | .write = stm32_console_write, |
| 642 | .setup = stm32_console_setup, |
| 643 | .flags = CON_PRINTBUFFER, |
| 644 | .index = -1, |
| 645 | .data = &stm32_usart_driver, |
| 646 | }; |
| 647 | |
| 648 | #define STM32_SERIAL_CONSOLE (&stm32_console) |
| 649 | |
| 650 | #else |
| 651 | #define STM32_SERIAL_CONSOLE NULL |
| 652 | #endif /* CONFIG_SERIAL_STM32_CONSOLE */ |
| 653 | |
| 654 | static struct uart_driver stm32_usart_driver = { |
| 655 | .driver_name = DRIVER_NAME, |
| 656 | .dev_name = STM32_SERIAL_NAME, |
| 657 | .major = 0, |
| 658 | .minor = 0, |
| 659 | .nr = STM32_MAX_PORTS, |
| 660 | .cons = STM32_SERIAL_CONSOLE, |
| 661 | }; |
| 662 | |
| 663 | static struct platform_driver stm32_serial_driver = { |
| 664 | .probe = stm32_serial_probe, |
| 665 | .remove = stm32_serial_remove, |
| 666 | .driver = { |
| 667 | .name = DRIVER_NAME, |
| 668 | .of_match_table = of_match_ptr(stm32_match), |
| 669 | }, |
| 670 | }; |
| 671 | |
| 672 | static int __init usart_init(void) |
| 673 | { |
| 674 | static char banner[] __initdata = "STM32 USART driver initialized"; |
| 675 | int ret; |
| 676 | |
| 677 | pr_info("%s\n", banner); |
| 678 | |
| 679 | ret = uart_register_driver(&stm32_usart_driver); |
| 680 | if (ret) |
| 681 | return ret; |
| 682 | |
| 683 | ret = platform_driver_register(&stm32_serial_driver); |
| 684 | if (ret) |
| 685 | uart_unregister_driver(&stm32_usart_driver); |
| 686 | |
| 687 | return ret; |
| 688 | } |
| 689 | |
| 690 | static void __exit usart_exit(void) |
| 691 | { |
| 692 | platform_driver_unregister(&stm32_serial_driver); |
| 693 | uart_unregister_driver(&stm32_usart_driver); |
| 694 | } |
| 695 | |
| 696 | module_init(usart_init); |
| 697 | module_exit(usart_exit); |
| 698 | |
| 699 | MODULE_ALIAS("platform:" DRIVER_NAME); |
| 700 | MODULE_DESCRIPTION("STMicroelectronics STM32 serial port driver"); |
| 701 | MODULE_LICENSE("GPL v2"); |