Carlo Caione | ff7693d | 2014-08-17 12:49:49 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Based on meson_uart.c, by AMLOGIC, INC. |
| 3 | * |
| 4 | * Copyright (C) 2014 Carlo Caione <carlo@caione.org> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify it |
| 7 | * under the terms of the GNU General Public License version 2 as published |
| 8 | * by the Free Software Foundation. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | */ |
| 16 | |
| 17 | #include <linux/clk.h> |
| 18 | #include <linux/console.h> |
| 19 | #include <linux/delay.h> |
| 20 | #include <linux/init.h> |
| 21 | #include <linux/io.h> |
| 22 | #include <linux/module.h> |
| 23 | #include <linux/kernel.h> |
| 24 | #include <linux/of.h> |
| 25 | #include <linux/platform_device.h> |
| 26 | #include <linux/serial.h> |
| 27 | #include <linux/serial_core.h> |
| 28 | #include <linux/tty.h> |
| 29 | #include <linux/tty_flip.h> |
| 30 | |
| 31 | /* Register offsets */ |
| 32 | #define AML_UART_WFIFO 0x00 |
| 33 | #define AML_UART_RFIFO 0x04 |
| 34 | #define AML_UART_CONTROL 0x08 |
| 35 | #define AML_UART_STATUS 0x0c |
| 36 | #define AML_UART_MISC 0x10 |
| 37 | #define AML_UART_REG5 0x14 |
| 38 | |
| 39 | /* AML_UART_CONTROL bits */ |
| 40 | #define AML_UART_TX_EN BIT(12) |
| 41 | #define AML_UART_RX_EN BIT(13) |
| 42 | #define AML_UART_TX_RST BIT(22) |
| 43 | #define AML_UART_RX_RST BIT(23) |
| 44 | #define AML_UART_CLR_ERR BIT(24) |
| 45 | #define AML_UART_RX_INT_EN BIT(27) |
| 46 | #define AML_UART_TX_INT_EN BIT(28) |
| 47 | #define AML_UART_DATA_LEN_MASK (0x03 << 20) |
| 48 | #define AML_UART_DATA_LEN_8BIT (0x00 << 20) |
| 49 | #define AML_UART_DATA_LEN_7BIT (0x01 << 20) |
| 50 | #define AML_UART_DATA_LEN_6BIT (0x02 << 20) |
| 51 | #define AML_UART_DATA_LEN_5BIT (0x03 << 20) |
| 52 | |
| 53 | /* AML_UART_STATUS bits */ |
| 54 | #define AML_UART_PARITY_ERR BIT(16) |
| 55 | #define AML_UART_FRAME_ERR BIT(17) |
| 56 | #define AML_UART_TX_FIFO_WERR BIT(18) |
| 57 | #define AML_UART_RX_EMPTY BIT(20) |
| 58 | #define AML_UART_TX_FULL BIT(21) |
| 59 | #define AML_UART_TX_EMPTY BIT(22) |
Ben Dooks | 8867973 | 2015-11-18 14:41:13 +0000 | [diff] [blame] | 60 | #define AML_UART_XMIT_BUSY BIT(25) |
Carlo Caione | ff7693d | 2014-08-17 12:49:49 +0200 | [diff] [blame] | 61 | #define AML_UART_ERR (AML_UART_PARITY_ERR | \ |
| 62 | AML_UART_FRAME_ERR | \ |
| 63 | AML_UART_TX_FIFO_WERR) |
| 64 | |
| 65 | /* AML_UART_CONTROL bits */ |
| 66 | #define AML_UART_TWO_WIRE_EN BIT(15) |
| 67 | #define AML_UART_PARITY_TYPE BIT(18) |
| 68 | #define AML_UART_PARITY_EN BIT(19) |
| 69 | #define AML_UART_CLEAR_ERR BIT(24) |
| 70 | #define AML_UART_STOP_BIN_LEN_MASK (0x03 << 16) |
| 71 | #define AML_UART_STOP_BIN_1SB (0x00 << 16) |
| 72 | #define AML_UART_STOP_BIN_2SB (0x01 << 16) |
| 73 | |
| 74 | /* AML_UART_MISC bits */ |
| 75 | #define AML_UART_XMIT_IRQ(c) (((c) & 0xff) << 8) |
| 76 | #define AML_UART_RECV_IRQ(c) ((c) & 0xff) |
| 77 | |
| 78 | /* AML_UART_REG5 bits */ |
| 79 | #define AML_UART_BAUD_MASK 0x7fffff |
| 80 | #define AML_UART_BAUD_USE BIT(23) |
| 81 | |
| 82 | #define AML_UART_PORT_NUM 6 |
| 83 | #define AML_UART_DEV_NAME "ttyAML" |
| 84 | |
| 85 | |
| 86 | static struct uart_driver meson_uart_driver; |
| 87 | |
| 88 | static struct uart_port *meson_ports[AML_UART_PORT_NUM]; |
| 89 | |
| 90 | static void meson_uart_set_mctrl(struct uart_port *port, unsigned int mctrl) |
| 91 | { |
| 92 | } |
| 93 | |
| 94 | static unsigned int meson_uart_get_mctrl(struct uart_port *port) |
| 95 | { |
| 96 | return TIOCM_CTS; |
| 97 | } |
| 98 | |
| 99 | static unsigned int meson_uart_tx_empty(struct uart_port *port) |
| 100 | { |
| 101 | u32 val; |
| 102 | |
| 103 | val = readl(port->membase + AML_UART_STATUS); |
Ben Dooks | 8867973 | 2015-11-18 14:41:13 +0000 | [diff] [blame] | 104 | val &= (AML_UART_TX_EMPTY | AML_UART_XMIT_BUSY); |
| 105 | return (val == AML_UART_TX_EMPTY) ? TIOCSER_TEMT : 0; |
Carlo Caione | ff7693d | 2014-08-17 12:49:49 +0200 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | static void meson_uart_stop_tx(struct uart_port *port) |
| 109 | { |
| 110 | u32 val; |
| 111 | |
| 112 | val = readl(port->membase + AML_UART_CONTROL); |
Ben Dooks | 855ddca | 2015-11-18 14:41:15 +0000 | [diff] [blame] | 113 | val &= ~AML_UART_TX_INT_EN; |
Carlo Caione | ff7693d | 2014-08-17 12:49:49 +0200 | [diff] [blame] | 114 | writel(val, port->membase + AML_UART_CONTROL); |
| 115 | } |
| 116 | |
| 117 | static void meson_uart_stop_rx(struct uart_port *port) |
| 118 | { |
| 119 | u32 val; |
| 120 | |
| 121 | val = readl(port->membase + AML_UART_CONTROL); |
| 122 | val &= ~AML_UART_RX_EN; |
| 123 | writel(val, port->membase + AML_UART_CONTROL); |
| 124 | } |
| 125 | |
| 126 | static void meson_uart_shutdown(struct uart_port *port) |
| 127 | { |
| 128 | unsigned long flags; |
| 129 | u32 val; |
| 130 | |
| 131 | free_irq(port->irq, port); |
| 132 | |
| 133 | spin_lock_irqsave(&port->lock, flags); |
| 134 | |
| 135 | val = readl(port->membase + AML_UART_CONTROL); |
Ben Dooks | 855ddca | 2015-11-18 14:41:15 +0000 | [diff] [blame] | 136 | val &= ~AML_UART_RX_EN; |
Carlo Caione | ff7693d | 2014-08-17 12:49:49 +0200 | [diff] [blame] | 137 | val &= ~(AML_UART_RX_INT_EN | AML_UART_TX_INT_EN); |
| 138 | writel(val, port->membase + AML_UART_CONTROL); |
| 139 | |
| 140 | spin_unlock_irqrestore(&port->lock, flags); |
| 141 | } |
| 142 | |
| 143 | static void meson_uart_start_tx(struct uart_port *port) |
| 144 | { |
| 145 | struct circ_buf *xmit = &port->state->xmit; |
| 146 | unsigned int ch; |
| 147 | |
| 148 | if (uart_tx_stopped(port)) { |
| 149 | meson_uart_stop_tx(port); |
| 150 | return; |
| 151 | } |
| 152 | |
| 153 | while (!(readl(port->membase + AML_UART_STATUS) & AML_UART_TX_FULL)) { |
| 154 | if (port->x_char) { |
| 155 | writel(port->x_char, port->membase + AML_UART_WFIFO); |
| 156 | port->icount.tx++; |
| 157 | port->x_char = 0; |
| 158 | continue; |
| 159 | } |
| 160 | |
| 161 | if (uart_circ_empty(xmit)) |
| 162 | break; |
| 163 | |
| 164 | ch = xmit->buf[xmit->tail]; |
| 165 | writel(ch, port->membase + AML_UART_WFIFO); |
| 166 | xmit->tail = (xmit->tail+1) & (SERIAL_XMIT_SIZE - 1); |
| 167 | port->icount.tx++; |
| 168 | } |
| 169 | |
| 170 | if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS) |
| 171 | uart_write_wakeup(port); |
| 172 | } |
| 173 | |
| 174 | static void meson_receive_chars(struct uart_port *port) |
| 175 | { |
| 176 | struct tty_port *tport = &port->state->port; |
| 177 | char flag; |
| 178 | u32 status, ch, mode; |
| 179 | |
| 180 | do { |
| 181 | flag = TTY_NORMAL; |
| 182 | port->icount.rx++; |
| 183 | status = readl(port->membase + AML_UART_STATUS); |
| 184 | |
| 185 | if (status & AML_UART_ERR) { |
| 186 | if (status & AML_UART_TX_FIFO_WERR) |
| 187 | port->icount.overrun++; |
| 188 | else if (status & AML_UART_FRAME_ERR) |
| 189 | port->icount.frame++; |
| 190 | else if (status & AML_UART_PARITY_ERR) |
| 191 | port->icount.frame++; |
| 192 | |
| 193 | mode = readl(port->membase + AML_UART_CONTROL); |
| 194 | mode |= AML_UART_CLEAR_ERR; |
| 195 | writel(mode, port->membase + AML_UART_CONTROL); |
| 196 | |
| 197 | /* It doesn't clear to 0 automatically */ |
| 198 | mode &= ~AML_UART_CLEAR_ERR; |
| 199 | writel(mode, port->membase + AML_UART_CONTROL); |
| 200 | |
| 201 | status &= port->read_status_mask; |
| 202 | if (status & AML_UART_FRAME_ERR) |
| 203 | flag = TTY_FRAME; |
| 204 | else if (status & AML_UART_PARITY_ERR) |
| 205 | flag = TTY_PARITY; |
| 206 | } |
| 207 | |
| 208 | ch = readl(port->membase + AML_UART_RFIFO); |
| 209 | ch &= 0xff; |
| 210 | |
| 211 | if ((status & port->ignore_status_mask) == 0) |
| 212 | tty_insert_flip_char(tport, ch, flag); |
| 213 | |
| 214 | if (status & AML_UART_TX_FIFO_WERR) |
| 215 | tty_insert_flip_char(tport, 0, TTY_OVERRUN); |
| 216 | |
| 217 | } while (!(readl(port->membase + AML_UART_STATUS) & AML_UART_RX_EMPTY)); |
| 218 | |
| 219 | spin_unlock(&port->lock); |
| 220 | tty_flip_buffer_push(tport); |
| 221 | spin_lock(&port->lock); |
| 222 | } |
| 223 | |
| 224 | static irqreturn_t meson_uart_interrupt(int irq, void *dev_id) |
| 225 | { |
| 226 | struct uart_port *port = (struct uart_port *)dev_id; |
| 227 | |
| 228 | spin_lock(&port->lock); |
| 229 | |
| 230 | if (!(readl(port->membase + AML_UART_STATUS) & AML_UART_RX_EMPTY)) |
| 231 | meson_receive_chars(port); |
| 232 | |
| 233 | if (!(readl(port->membase + AML_UART_STATUS) & AML_UART_TX_FULL)) |
| 234 | meson_uart_start_tx(port); |
| 235 | |
| 236 | spin_unlock(&port->lock); |
| 237 | |
| 238 | return IRQ_HANDLED; |
| 239 | } |
| 240 | |
| 241 | static const char *meson_uart_type(struct uart_port *port) |
| 242 | { |
| 243 | return (port->type == PORT_MESON) ? "meson_uart" : NULL; |
| 244 | } |
| 245 | |
Ben Dooks | 00661dd | 2015-11-18 14:41:12 +0000 | [diff] [blame] | 246 | static void meson_uart_reset(struct uart_port *port) |
Carlo Caione | ff7693d | 2014-08-17 12:49:49 +0200 | [diff] [blame] | 247 | { |
| 248 | u32 val; |
Carlo Caione | ff7693d | 2014-08-17 12:49:49 +0200 | [diff] [blame] | 249 | |
| 250 | val = readl(port->membase + AML_UART_CONTROL); |
| 251 | val |= (AML_UART_RX_RST | AML_UART_TX_RST | AML_UART_CLR_ERR); |
| 252 | writel(val, port->membase + AML_UART_CONTROL); |
| 253 | |
| 254 | val &= ~(AML_UART_RX_RST | AML_UART_TX_RST | AML_UART_CLR_ERR); |
| 255 | writel(val, port->membase + AML_UART_CONTROL); |
Ben Dooks | 00661dd | 2015-11-18 14:41:12 +0000 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | static int meson_uart_startup(struct uart_port *port) |
| 259 | { |
| 260 | u32 val; |
| 261 | int ret = 0; |
| 262 | |
| 263 | val = readl(port->membase + AML_UART_CONTROL); |
| 264 | val |= AML_UART_CLR_ERR; |
| 265 | writel(val, port->membase + AML_UART_CONTROL); |
| 266 | val &= ~AML_UART_CLR_ERR; |
| 267 | writel(val, port->membase + AML_UART_CONTROL); |
Carlo Caione | ff7693d | 2014-08-17 12:49:49 +0200 | [diff] [blame] | 268 | |
| 269 | val |= (AML_UART_RX_EN | AML_UART_TX_EN); |
| 270 | writel(val, port->membase + AML_UART_CONTROL); |
| 271 | |
| 272 | val |= (AML_UART_RX_INT_EN | AML_UART_TX_INT_EN); |
| 273 | writel(val, port->membase + AML_UART_CONTROL); |
| 274 | |
| 275 | val = (AML_UART_RECV_IRQ(1) | AML_UART_XMIT_IRQ(port->fifosize / 2)); |
| 276 | writel(val, port->membase + AML_UART_MISC); |
| 277 | |
| 278 | ret = request_irq(port->irq, meson_uart_interrupt, 0, |
| 279 | meson_uart_type(port), port); |
| 280 | |
| 281 | return ret; |
| 282 | } |
| 283 | |
| 284 | static void meson_uart_change_speed(struct uart_port *port, unsigned long baud) |
| 285 | { |
| 286 | u32 val; |
| 287 | |
Ben Dooks | f1f5c14 | 2015-11-18 14:41:16 +0000 | [diff] [blame] | 288 | while (!meson_uart_tx_empty(port)) |
Carlo Caione | ff7693d | 2014-08-17 12:49:49 +0200 | [diff] [blame] | 289 | cpu_relax(); |
| 290 | |
| 291 | val = readl(port->membase + AML_UART_REG5); |
| 292 | val &= ~AML_UART_BAUD_MASK; |
| 293 | val = ((port->uartclk * 10 / (baud * 4) + 5) / 10) - 1; |
| 294 | val |= AML_UART_BAUD_USE; |
| 295 | writel(val, port->membase + AML_UART_REG5); |
| 296 | } |
| 297 | |
| 298 | static void meson_uart_set_termios(struct uart_port *port, |
| 299 | struct ktermios *termios, |
| 300 | struct ktermios *old) |
| 301 | { |
| 302 | unsigned int cflags, iflags, baud; |
| 303 | unsigned long flags; |
| 304 | u32 val; |
| 305 | |
| 306 | spin_lock_irqsave(&port->lock, flags); |
| 307 | |
| 308 | cflags = termios->c_cflag; |
| 309 | iflags = termios->c_iflag; |
| 310 | |
| 311 | val = readl(port->membase + AML_UART_CONTROL); |
| 312 | |
| 313 | val &= ~AML_UART_DATA_LEN_MASK; |
| 314 | switch (cflags & CSIZE) { |
| 315 | case CS8: |
| 316 | val |= AML_UART_DATA_LEN_8BIT; |
| 317 | break; |
| 318 | case CS7: |
| 319 | val |= AML_UART_DATA_LEN_7BIT; |
| 320 | break; |
| 321 | case CS6: |
| 322 | val |= AML_UART_DATA_LEN_6BIT; |
| 323 | break; |
| 324 | case CS5: |
| 325 | val |= AML_UART_DATA_LEN_5BIT; |
| 326 | break; |
| 327 | } |
| 328 | |
| 329 | if (cflags & PARENB) |
| 330 | val |= AML_UART_PARITY_EN; |
| 331 | else |
| 332 | val &= ~AML_UART_PARITY_EN; |
| 333 | |
| 334 | if (cflags & PARODD) |
| 335 | val |= AML_UART_PARITY_TYPE; |
| 336 | else |
| 337 | val &= ~AML_UART_PARITY_TYPE; |
| 338 | |
| 339 | val &= ~AML_UART_STOP_BIN_LEN_MASK; |
| 340 | if (cflags & CSTOPB) |
| 341 | val |= AML_UART_STOP_BIN_2SB; |
| 342 | else |
| 343 | val &= ~AML_UART_STOP_BIN_1SB; |
| 344 | |
| 345 | if (cflags & CRTSCTS) |
| 346 | val &= ~AML_UART_TWO_WIRE_EN; |
| 347 | else |
| 348 | val |= AML_UART_TWO_WIRE_EN; |
| 349 | |
| 350 | writel(val, port->membase + AML_UART_CONTROL); |
| 351 | |
| 352 | baud = uart_get_baud_rate(port, termios, old, 9600, 115200); |
| 353 | meson_uart_change_speed(port, baud); |
| 354 | |
| 355 | port->read_status_mask = AML_UART_TX_FIFO_WERR; |
| 356 | if (iflags & INPCK) |
| 357 | port->read_status_mask |= AML_UART_PARITY_ERR | |
| 358 | AML_UART_FRAME_ERR; |
| 359 | |
| 360 | port->ignore_status_mask = 0; |
| 361 | if (iflags & IGNPAR) |
| 362 | port->ignore_status_mask |= AML_UART_PARITY_ERR | |
| 363 | AML_UART_FRAME_ERR; |
| 364 | |
| 365 | uart_update_timeout(port, termios->c_cflag, baud); |
| 366 | spin_unlock_irqrestore(&port->lock, flags); |
| 367 | } |
| 368 | |
| 369 | static int meson_uart_verify_port(struct uart_port *port, |
| 370 | struct serial_struct *ser) |
| 371 | { |
| 372 | int ret = 0; |
| 373 | |
| 374 | if (port->type != PORT_MESON) |
| 375 | ret = -EINVAL; |
| 376 | if (port->irq != ser->irq) |
| 377 | ret = -EINVAL; |
| 378 | if (ser->baud_base < 9600) |
| 379 | ret = -EINVAL; |
| 380 | return ret; |
| 381 | } |
| 382 | |
Ben Dooks | 1bc1f17 | 2015-11-18 14:41:11 +0000 | [diff] [blame] | 383 | static int meson_uart_res_size(struct uart_port *port) |
| 384 | { |
| 385 | struct platform_device *pdev = to_platform_device(port->dev); |
| 386 | struct resource *res; |
| 387 | |
| 388 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 389 | if (!res) { |
| 390 | dev_err(port->dev, "cannot obtain I/O memory region"); |
| 391 | return -ENODEV; |
| 392 | } |
| 393 | |
| 394 | return resource_size(res); |
| 395 | } |
| 396 | |
Carlo Caione | ff7693d | 2014-08-17 12:49:49 +0200 | [diff] [blame] | 397 | static void meson_uart_release_port(struct uart_port *port) |
| 398 | { |
Ben Dooks | 1bc1f17 | 2015-11-18 14:41:11 +0000 | [diff] [blame] | 399 | int size = meson_uart_res_size(port); |
| 400 | |
Carlo Caione | ff7693d | 2014-08-17 12:49:49 +0200 | [diff] [blame] | 401 | if (port->flags & UPF_IOREMAP) { |
Ben Dooks | 1bc1f17 | 2015-11-18 14:41:11 +0000 | [diff] [blame] | 402 | devm_release_mem_region(port->dev, port->mapbase, size); |
Firo Yang | c547630f | 2015-04-26 18:46:06 +0800 | [diff] [blame] | 403 | devm_iounmap(port->dev, port->membase); |
Carlo Caione | ff7693d | 2014-08-17 12:49:49 +0200 | [diff] [blame] | 404 | port->membase = NULL; |
| 405 | } |
| 406 | } |
| 407 | |
| 408 | static int meson_uart_request_port(struct uart_port *port) |
| 409 | { |
Ben Dooks | 1bc1f17 | 2015-11-18 14:41:11 +0000 | [diff] [blame] | 410 | int size = meson_uart_res_size(port); |
Carlo Caione | ff7693d | 2014-08-17 12:49:49 +0200 | [diff] [blame] | 411 | |
Ben Dooks | 1bc1f17 | 2015-11-18 14:41:11 +0000 | [diff] [blame] | 412 | if (size < 0) |
| 413 | return size; |
Carlo Caione | ff7693d | 2014-08-17 12:49:49 +0200 | [diff] [blame] | 414 | |
| 415 | if (!devm_request_mem_region(port->dev, port->mapbase, size, |
| 416 | dev_name(port->dev))) { |
| 417 | dev_err(port->dev, "Memory region busy\n"); |
| 418 | return -EBUSY; |
| 419 | } |
| 420 | |
| 421 | if (port->flags & UPF_IOREMAP) { |
| 422 | port->membase = devm_ioremap_nocache(port->dev, |
| 423 | port->mapbase, |
| 424 | size); |
| 425 | if (port->membase == NULL) |
| 426 | return -ENOMEM; |
| 427 | } |
| 428 | |
| 429 | return 0; |
| 430 | } |
| 431 | |
| 432 | static void meson_uart_config_port(struct uart_port *port, int flags) |
| 433 | { |
| 434 | if (flags & UART_CONFIG_TYPE) { |
| 435 | port->type = PORT_MESON; |
| 436 | meson_uart_request_port(port); |
| 437 | } |
| 438 | } |
| 439 | |
| 440 | static struct uart_ops meson_uart_ops = { |
| 441 | .set_mctrl = meson_uart_set_mctrl, |
| 442 | .get_mctrl = meson_uart_get_mctrl, |
| 443 | .tx_empty = meson_uart_tx_empty, |
| 444 | .start_tx = meson_uart_start_tx, |
| 445 | .stop_tx = meson_uart_stop_tx, |
| 446 | .stop_rx = meson_uart_stop_rx, |
| 447 | .startup = meson_uart_startup, |
| 448 | .shutdown = meson_uart_shutdown, |
| 449 | .set_termios = meson_uart_set_termios, |
| 450 | .type = meson_uart_type, |
| 451 | .config_port = meson_uart_config_port, |
| 452 | .request_port = meson_uart_request_port, |
| 453 | .release_port = meson_uart_release_port, |
| 454 | .verify_port = meson_uart_verify_port, |
| 455 | }; |
| 456 | |
| 457 | #ifdef CONFIG_SERIAL_MESON_CONSOLE |
| 458 | |
| 459 | static void meson_console_putchar(struct uart_port *port, int ch) |
| 460 | { |
| 461 | if (!port->membase) |
| 462 | return; |
| 463 | |
| 464 | while (readl(port->membase + AML_UART_STATUS) & AML_UART_TX_FULL) |
| 465 | cpu_relax(); |
| 466 | writel(ch, port->membase + AML_UART_WFIFO); |
| 467 | } |
| 468 | |
| 469 | static void meson_serial_console_write(struct console *co, const char *s, |
| 470 | u_int count) |
| 471 | { |
| 472 | struct uart_port *port; |
| 473 | unsigned long flags; |
| 474 | int locked; |
Ben Dooks | 2561f06 | 2015-11-18 14:41:17 +0000 | [diff] [blame^] | 475 | u32 val, tmp; |
Carlo Caione | ff7693d | 2014-08-17 12:49:49 +0200 | [diff] [blame] | 476 | |
| 477 | port = meson_ports[co->index]; |
| 478 | if (!port) |
| 479 | return; |
| 480 | |
| 481 | local_irq_save(flags); |
| 482 | if (port->sysrq) { |
| 483 | locked = 0; |
| 484 | } else if (oops_in_progress) { |
| 485 | locked = spin_trylock(&port->lock); |
| 486 | } else { |
| 487 | spin_lock(&port->lock); |
| 488 | locked = 1; |
| 489 | } |
| 490 | |
Ben Dooks | 41788f0 | 2015-11-18 14:41:14 +0000 | [diff] [blame] | 491 | val = readl(port->membase + AML_UART_CONTROL); |
Ben Dooks | 2561f06 | 2015-11-18 14:41:17 +0000 | [diff] [blame^] | 492 | val |= AML_UART_TX_EN; |
| 493 | tmp = val & ~(AML_UART_TX_INT_EN | AML_UART_RX_INT_EN); |
| 494 | writel(tmp, port->membase + AML_UART_CONTROL); |
Ben Dooks | 41788f0 | 2015-11-18 14:41:14 +0000 | [diff] [blame] | 495 | |
Carlo Caione | ff7693d | 2014-08-17 12:49:49 +0200 | [diff] [blame] | 496 | uart_console_write(port, s, count, meson_console_putchar); |
Ben Dooks | 2561f06 | 2015-11-18 14:41:17 +0000 | [diff] [blame^] | 497 | writel(val, port->membase + AML_UART_CONTROL); |
Carlo Caione | ff7693d | 2014-08-17 12:49:49 +0200 | [diff] [blame] | 498 | |
| 499 | if (locked) |
| 500 | spin_unlock(&port->lock); |
| 501 | local_irq_restore(flags); |
| 502 | } |
| 503 | |
| 504 | static int meson_serial_console_setup(struct console *co, char *options) |
| 505 | { |
| 506 | struct uart_port *port; |
| 507 | int baud = 115200; |
| 508 | int bits = 8; |
| 509 | int parity = 'n'; |
| 510 | int flow = 'n'; |
| 511 | |
| 512 | if (co->index < 0 || co->index >= AML_UART_PORT_NUM) |
| 513 | return -EINVAL; |
| 514 | |
| 515 | port = meson_ports[co->index]; |
| 516 | if (!port || !port->membase) |
| 517 | return -ENODEV; |
| 518 | |
| 519 | if (options) |
| 520 | uart_parse_options(options, &baud, &parity, &bits, &flow); |
| 521 | |
| 522 | return uart_set_options(port, co, baud, parity, bits, flow); |
| 523 | } |
| 524 | |
| 525 | static struct console meson_serial_console = { |
| 526 | .name = AML_UART_DEV_NAME, |
| 527 | .write = meson_serial_console_write, |
| 528 | .device = uart_console_device, |
| 529 | .setup = meson_serial_console_setup, |
| 530 | .flags = CON_PRINTBUFFER, |
| 531 | .index = -1, |
| 532 | .data = &meson_uart_driver, |
| 533 | }; |
| 534 | |
| 535 | static int __init meson_serial_console_init(void) |
| 536 | { |
| 537 | register_console(&meson_serial_console); |
| 538 | return 0; |
| 539 | } |
| 540 | console_initcall(meson_serial_console_init); |
| 541 | |
| 542 | #define MESON_SERIAL_CONSOLE (&meson_serial_console) |
| 543 | #else |
| 544 | #define MESON_SERIAL_CONSOLE NULL |
| 545 | #endif |
| 546 | |
| 547 | static struct uart_driver meson_uart_driver = { |
| 548 | .owner = THIS_MODULE, |
| 549 | .driver_name = "meson_uart", |
| 550 | .dev_name = AML_UART_DEV_NAME, |
| 551 | .nr = AML_UART_PORT_NUM, |
| 552 | .cons = MESON_SERIAL_CONSOLE, |
| 553 | }; |
| 554 | |
| 555 | static int meson_uart_probe(struct platform_device *pdev) |
| 556 | { |
| 557 | struct resource *res_mem, *res_irq; |
| 558 | struct uart_port *port; |
| 559 | struct clk *clk; |
| 560 | int ret = 0; |
| 561 | |
| 562 | if (pdev->dev.of_node) |
| 563 | pdev->id = of_alias_get_id(pdev->dev.of_node, "serial"); |
| 564 | |
| 565 | if (pdev->id < 0 || pdev->id >= AML_UART_PORT_NUM) |
| 566 | return -EINVAL; |
| 567 | |
| 568 | res_mem = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 569 | if (!res_mem) |
| 570 | return -ENODEV; |
| 571 | |
| 572 | res_irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0); |
| 573 | if (!res_irq) |
| 574 | return -ENODEV; |
| 575 | |
| 576 | if (meson_ports[pdev->id]) { |
| 577 | dev_err(&pdev->dev, "port %d already allocated\n", pdev->id); |
| 578 | return -EBUSY; |
| 579 | } |
| 580 | |
| 581 | port = devm_kzalloc(&pdev->dev, sizeof(struct uart_port), GFP_KERNEL); |
| 582 | if (!port) |
| 583 | return -ENOMEM; |
| 584 | |
| 585 | clk = clk_get(&pdev->dev, NULL); |
| 586 | if (IS_ERR(clk)) |
| 587 | return PTR_ERR(clk); |
| 588 | |
| 589 | port->uartclk = clk_get_rate(clk); |
| 590 | port->iotype = UPIO_MEM; |
| 591 | port->mapbase = res_mem->start; |
| 592 | port->irq = res_irq->start; |
| 593 | port->flags = UPF_BOOT_AUTOCONF | UPF_IOREMAP | UPF_LOW_LATENCY; |
| 594 | port->dev = &pdev->dev; |
| 595 | port->line = pdev->id; |
| 596 | port->type = PORT_MESON; |
| 597 | port->x_char = 0; |
| 598 | port->ops = &meson_uart_ops; |
| 599 | port->fifosize = 64; |
| 600 | |
| 601 | meson_ports[pdev->id] = port; |
| 602 | platform_set_drvdata(pdev, port); |
| 603 | |
Ben Dooks | 00661dd | 2015-11-18 14:41:12 +0000 | [diff] [blame] | 604 | /* reset port before registering (and possibly registering console) */ |
| 605 | if (meson_uart_request_port(port) >= 0) { |
| 606 | meson_uart_reset(port); |
| 607 | meson_uart_release_port(port); |
| 608 | } |
| 609 | |
Carlo Caione | ff7693d | 2014-08-17 12:49:49 +0200 | [diff] [blame] | 610 | ret = uart_add_one_port(&meson_uart_driver, port); |
| 611 | if (ret) |
| 612 | meson_ports[pdev->id] = NULL; |
| 613 | |
| 614 | return ret; |
| 615 | } |
| 616 | |
| 617 | static int meson_uart_remove(struct platform_device *pdev) |
| 618 | { |
| 619 | struct uart_port *port; |
| 620 | |
| 621 | port = platform_get_drvdata(pdev); |
| 622 | uart_remove_one_port(&meson_uart_driver, port); |
| 623 | meson_ports[pdev->id] = NULL; |
| 624 | |
| 625 | return 0; |
| 626 | } |
| 627 | |
| 628 | |
| 629 | static const struct of_device_id meson_uart_dt_match[] = { |
| 630 | { .compatible = "amlogic,meson-uart" }, |
| 631 | { /* sentinel */ }, |
| 632 | }; |
| 633 | MODULE_DEVICE_TABLE(of, meson_uart_dt_match); |
| 634 | |
| 635 | static struct platform_driver meson_uart_platform_driver = { |
| 636 | .probe = meson_uart_probe, |
| 637 | .remove = meson_uart_remove, |
| 638 | .driver = { |
Carlo Caione | ff7693d | 2014-08-17 12:49:49 +0200 | [diff] [blame] | 639 | .name = "meson_uart", |
| 640 | .of_match_table = meson_uart_dt_match, |
| 641 | }, |
| 642 | }; |
| 643 | |
| 644 | static int __init meson_uart_init(void) |
| 645 | { |
| 646 | int ret; |
| 647 | |
| 648 | ret = uart_register_driver(&meson_uart_driver); |
| 649 | if (ret) |
| 650 | return ret; |
| 651 | |
| 652 | ret = platform_driver_register(&meson_uart_platform_driver); |
| 653 | if (ret) |
| 654 | uart_unregister_driver(&meson_uart_driver); |
| 655 | |
| 656 | return ret; |
| 657 | } |
| 658 | |
| 659 | static void __exit meson_uart_exit(void) |
| 660 | { |
| 661 | platform_driver_unregister(&meson_uart_platform_driver); |
| 662 | uart_unregister_driver(&meson_uart_driver); |
| 663 | } |
| 664 | |
| 665 | module_init(meson_uart_init); |
| 666 | module_exit(meson_uart_exit); |
| 667 | |
| 668 | MODULE_AUTHOR("Carlo Caione <carlo@caione.org>"); |
| 669 | MODULE_DESCRIPTION("Amlogic Meson serial port driver"); |
| 670 | MODULE_LICENSE("GPL v2"); |