Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | * Driver for CLPS711x serial ports |
| 3 | * |
| 4 | * Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o. |
| 5 | * |
| 6 | * Copyright 1999 ARM Limited |
| 7 | * Copyright (C) 2000 Deep Blue Solutions Ltd. |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License as published by |
| 11 | * the Free Software Foundation; either version 2 of the License, or |
| 12 | * (at your option) any later version. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | |
| 15 | #if defined(CONFIG_SERIAL_CLPS711X_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ) |
| 16 | #define SUPPORT_SYSRQ |
| 17 | #endif |
| 18 | |
| 19 | #include <linux/module.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | #include <linux/device.h> |
Alexander Shiyan | a1c25f2 | 2012-10-14 11:05:34 +0400 | [diff] [blame] | 21 | #include <linux/console.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | #include <linux/serial_core.h> |
| 23 | #include <linux/serial.h> |
Russell King | 9973022 | 2009-03-25 10:21:35 +0000 | [diff] [blame] | 24 | #include <linux/io.h> |
Alexander Shiyan | c08f015 | 2012-10-14 11:05:26 +0400 | [diff] [blame] | 25 | #include <linux/clk.h> |
Alexander Shiyan | a1c25f2 | 2012-10-14 11:05:34 +0400 | [diff] [blame] | 26 | #include <linux/tty.h> |
| 27 | #include <linux/tty_flip.h> |
| 28 | #include <linux/ioport.h> |
Alexander Shiyan | 9511372 | 2012-10-14 11:05:23 +0400 | [diff] [blame] | 29 | #include <linux/platform_device.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | |
Russell King | a09e64f | 2008-08-05 16:14:15 +0100 | [diff] [blame] | 31 | #include <mach/hardware.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | |
Alexander Shiyan | 9511372 | 2012-10-14 11:05:23 +0400 | [diff] [blame] | 33 | #define UART_CLPS711X_NAME "uart-clps711x" |
Alexander Shiyan | 117d5d4 | 2012-10-14 11:05:24 +0400 | [diff] [blame] | 34 | #define UART_CLPS711X_NR 2 |
| 35 | #define UART_CLPS711X_MAJOR 204 |
| 36 | #define UART_CLPS711X_MINOR 40 |
Alexander Shiyan | 9511372 | 2012-10-14 11:05:23 +0400 | [diff] [blame] | 37 | |
Alexander Shiyan | 117d5d4 | 2012-10-14 11:05:24 +0400 | [diff] [blame] | 38 | #define UBRLCR(port) ((port)->line ? UBRLCR2 : UBRLCR1) |
| 39 | #define UARTDR(port) ((port)->line ? UARTDR2 : UARTDR1) |
| 40 | #define SYSFLG(port) ((port)->line ? SYSFLG2 : SYSFLG1) |
| 41 | #define SYSCON(port) ((port)->line ? SYSCON2 : SYSCON1) |
| 42 | #define TX_IRQ(port) ((port)->line ? IRQ_UTXINT2 : IRQ_UTXINT1) |
| 43 | #define RX_IRQ(port) ((port)->line ? IRQ_URXINT2 : IRQ_URXINT1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | |
Alexander Shiyan | 117d5d4 | 2012-10-14 11:05:24 +0400 | [diff] [blame] | 45 | struct clps711x_port { |
| 46 | struct uart_driver uart; |
Alexander Shiyan | c08f015 | 2012-10-14 11:05:26 +0400 | [diff] [blame] | 47 | struct clk *uart_clk; |
Alexander Shiyan | 117d5d4 | 2012-10-14 11:05:24 +0400 | [diff] [blame] | 48 | struct uart_port port[UART_CLPS711X_NR]; |
Alexander Shiyan | 3c7e9eb | 2012-10-14 11:05:25 +0400 | [diff] [blame] | 49 | int tx_enabled[UART_CLPS711X_NR]; |
Alexander Shiyan | 117d5d4 | 2012-10-14 11:05:24 +0400 | [diff] [blame] | 50 | #ifdef CONFIG_SERIAL_CLPS711X_CONSOLE |
| 51 | struct console console; |
| 52 | #endif |
| 53 | }; |
| 54 | |
Alexander Shiyan | a1c25f2 | 2012-10-14 11:05:34 +0400 | [diff] [blame] | 55 | static void uart_clps711x_stop_tx(struct uart_port *port) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | { |
Alexander Shiyan | 3c7e9eb | 2012-10-14 11:05:25 +0400 | [diff] [blame] | 57 | struct clps711x_port *s = dev_get_drvdata(port->dev); |
| 58 | |
| 59 | if (s->tx_enabled[port->line]) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | disable_irq(TX_IRQ(port)); |
Alexander Shiyan | 3c7e9eb | 2012-10-14 11:05:25 +0400 | [diff] [blame] | 61 | s->tx_enabled[port->line] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | } |
| 63 | } |
| 64 | |
Alexander Shiyan | a1c25f2 | 2012-10-14 11:05:34 +0400 | [diff] [blame] | 65 | static void uart_clps711x_start_tx(struct uart_port *port) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | { |
Alexander Shiyan | 3c7e9eb | 2012-10-14 11:05:25 +0400 | [diff] [blame] | 67 | struct clps711x_port *s = dev_get_drvdata(port->dev); |
| 68 | |
| 69 | if (!s->tx_enabled[port->line]) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | enable_irq(TX_IRQ(port)); |
Alexander Shiyan | 3c7e9eb | 2012-10-14 11:05:25 +0400 | [diff] [blame] | 71 | s->tx_enabled[port->line] = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | } |
| 73 | } |
| 74 | |
Alexander Shiyan | a1c25f2 | 2012-10-14 11:05:34 +0400 | [diff] [blame] | 75 | static void uart_clps711x_stop_rx(struct uart_port *port) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | { |
| 77 | disable_irq(RX_IRQ(port)); |
| 78 | } |
| 79 | |
Alexander Shiyan | a1c25f2 | 2012-10-14 11:05:34 +0400 | [diff] [blame] | 80 | static void uart_clps711x_enable_ms(struct uart_port *port) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | { |
Alexander Shiyan | a1c25f2 | 2012-10-14 11:05:34 +0400 | [diff] [blame] | 82 | /* Do nothing */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | } |
| 84 | |
Alexander Shiyan | 135cc79 | 2012-10-14 11:05:31 +0400 | [diff] [blame] | 85 | static irqreturn_t uart_clps711x_int_rx(int irq, void *dev_id) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | { |
| 87 | struct uart_port *port = dev_id; |
Russell King | f993724 | 2005-09-24 10:12:47 +0100 | [diff] [blame] | 88 | unsigned int status, ch, flg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | |
Alexander Shiyan | f27de95 | 2012-10-14 11:05:30 +0400 | [diff] [blame] | 90 | for (;;) { |
| 91 | status = clps_readl(SYSFLG(port)); |
| 92 | if (status & SYSFLG_URXFE) |
| 93 | break; |
| 94 | |
| 95 | ch = clps_readw(UARTDR(port)); |
| 96 | status = ch & (UARTDR_FRMERR | UARTDR_PARERR | UARTDR_OVERR); |
| 97 | ch &= 0xff; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | port->icount.rx++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | flg = TTY_NORMAL; |
| 101 | |
Alexander Shiyan | f27de95 | 2012-10-14 11:05:30 +0400 | [diff] [blame] | 102 | if (unlikely(status)) { |
| 103 | if (status & UARTDR_PARERR) |
Russell King | 2a9604b | 2005-04-26 15:32:00 +0100 | [diff] [blame] | 104 | port->icount.parity++; |
Alexander Shiyan | f27de95 | 2012-10-14 11:05:30 +0400 | [diff] [blame] | 105 | else if (status & UARTDR_FRMERR) |
Russell King | 2a9604b | 2005-04-26 15:32:00 +0100 | [diff] [blame] | 106 | port->icount.frame++; |
Alexander Shiyan | f27de95 | 2012-10-14 11:05:30 +0400 | [diff] [blame] | 107 | else if (status & UARTDR_OVERR) |
Russell King | 2a9604b | 2005-04-26 15:32:00 +0100 | [diff] [blame] | 108 | port->icount.overrun++; |
| 109 | |
Alexander Shiyan | f27de95 | 2012-10-14 11:05:30 +0400 | [diff] [blame] | 110 | status &= port->read_status_mask; |
Russell King | 2a9604b | 2005-04-26 15:32:00 +0100 | [diff] [blame] | 111 | |
Alexander Shiyan | f27de95 | 2012-10-14 11:05:30 +0400 | [diff] [blame] | 112 | if (status & UARTDR_PARERR) |
Russell King | 2a9604b | 2005-04-26 15:32:00 +0100 | [diff] [blame] | 113 | flg = TTY_PARITY; |
Alexander Shiyan | f27de95 | 2012-10-14 11:05:30 +0400 | [diff] [blame] | 114 | else if (status & UARTDR_FRMERR) |
Russell King | 2a9604b | 2005-04-26 15:32:00 +0100 | [diff] [blame] | 115 | flg = TTY_FRAME; |
Alexander Shiyan | f27de95 | 2012-10-14 11:05:30 +0400 | [diff] [blame] | 116 | else if (status & UARTDR_OVERR) |
| 117 | flg = TTY_OVERRUN; |
Russell King | 2a9604b | 2005-04-26 15:32:00 +0100 | [diff] [blame] | 118 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 120 | if (uart_handle_sysrq_char(port, ch)) |
Alexander Shiyan | f27de95 | 2012-10-14 11:05:30 +0400 | [diff] [blame] | 121 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | |
Alexander Shiyan | f27de95 | 2012-10-14 11:05:30 +0400 | [diff] [blame] | 123 | if (status & port->ignore_status_mask) |
| 124 | continue; |
Russell King | 2a9604b | 2005-04-26 15:32:00 +0100 | [diff] [blame] | 125 | |
Alexander Shiyan | f27de95 | 2012-10-14 11:05:30 +0400 | [diff] [blame] | 126 | uart_insert_char(port, status, UARTDR_OVERR, ch, flg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | } |
Alexander Shiyan | f27de95 | 2012-10-14 11:05:30 +0400 | [diff] [blame] | 128 | |
Jiri Slaby | 2e124b4 | 2013-01-03 15:53:06 +0100 | [diff] [blame] | 129 | tty_flip_buffer_push(&port->state->port); |
Alexander Shiyan | f27de95 | 2012-10-14 11:05:30 +0400 | [diff] [blame] | 130 | |
Russell King | 2a9604b | 2005-04-26 15:32:00 +0100 | [diff] [blame] | 131 | return IRQ_HANDLED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | } |
| 133 | |
Alexander Shiyan | 135cc79 | 2012-10-14 11:05:31 +0400 | [diff] [blame] | 134 | static irqreturn_t uart_clps711x_int_tx(int irq, void *dev_id) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | { |
| 136 | struct uart_port *port = dev_id; |
Alexander Shiyan | 3c7e9eb | 2012-10-14 11:05:25 +0400 | [diff] [blame] | 137 | struct clps711x_port *s = dev_get_drvdata(port->dev); |
Alan Cox | ebd2c8f | 2009-09-19 13:13:28 -0700 | [diff] [blame] | 138 | struct circ_buf *xmit = &port->state->xmit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | |
| 140 | if (port->x_char) { |
Alexander Shiyan | a1c25f2 | 2012-10-14 11:05:34 +0400 | [diff] [blame] | 141 | clps_writew(port->x_char, UARTDR(port)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | port->icount.tx++; |
| 143 | port->x_char = 0; |
| 144 | return IRQ_HANDLED; |
| 145 | } |
Alexander Shiyan | 7a6fbc9 | 2012-03-27 12:22:49 +0400 | [diff] [blame] | 146 | |
Alexander Shiyan | 3c7e9eb | 2012-10-14 11:05:25 +0400 | [diff] [blame] | 147 | if (uart_circ_empty(xmit) || uart_tx_stopped(port)) { |
| 148 | disable_irq_nosync(TX_IRQ(port)); |
| 149 | s->tx_enabled[port->line] = 0; |
| 150 | return IRQ_HANDLED; |
| 151 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | |
Alexander Shiyan | cf03a88 | 2012-10-14 11:05:27 +0400 | [diff] [blame] | 153 | while (!uart_circ_empty(xmit)) { |
| 154 | clps_writew(xmit->buf[xmit->tail], UARTDR(port)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1); |
| 156 | port->icount.tx++; |
Alexander Shiyan | cf03a88 | 2012-10-14 11:05:27 +0400 | [diff] [blame] | 157 | if (clps_readl(SYSFLG(port) & SYSFLG_UTXFF)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | break; |
Alexander Shiyan | cf03a88 | 2012-10-14 11:05:27 +0400 | [diff] [blame] | 159 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | |
| 161 | if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS) |
| 162 | uart_write_wakeup(port); |
| 163 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | return IRQ_HANDLED; |
| 165 | } |
| 166 | |
Alexander Shiyan | a1c25f2 | 2012-10-14 11:05:34 +0400 | [diff] [blame] | 167 | static unsigned int uart_clps711x_tx_empty(struct uart_port *port) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | { |
Alexander Shiyan | a1c25f2 | 2012-10-14 11:05:34 +0400 | [diff] [blame] | 169 | return (clps_readl(SYSFLG(port) & SYSFLG_UBUSY)) ? 0 : TIOCSER_TEMT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | } |
| 171 | |
Alexander Shiyan | a1c25f2 | 2012-10-14 11:05:34 +0400 | [diff] [blame] | 172 | static unsigned int uart_clps711x_get_mctrl(struct uart_port *port) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 173 | { |
Alexander Shiyan | 1593daf | 2012-10-14 11:05:28 +0400 | [diff] [blame] | 174 | unsigned int status, result = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | |
Alexander Shiyan | 1593daf | 2012-10-14 11:05:28 +0400 | [diff] [blame] | 176 | if (port->line == 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | status = clps_readl(SYSFLG1); |
| 178 | if (status & SYSFLG1_DCD) |
| 179 | result |= TIOCM_CAR; |
| 180 | if (status & SYSFLG1_DSR) |
| 181 | result |= TIOCM_DSR; |
| 182 | if (status & SYSFLG1_CTS) |
| 183 | result |= TIOCM_CTS; |
Alexander Shiyan | 1593daf | 2012-10-14 11:05:28 +0400 | [diff] [blame] | 184 | } else |
| 185 | result = TIOCM_DSR | TIOCM_CTS | TIOCM_CAR; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | |
| 187 | return result; |
| 188 | } |
| 189 | |
Alexander Shiyan | a1c25f2 | 2012-10-14 11:05:34 +0400 | [diff] [blame] | 190 | static void uart_clps711x_set_mctrl(struct uart_port *port, unsigned int mctrl) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | { |
Alexander Shiyan | a1c25f2 | 2012-10-14 11:05:34 +0400 | [diff] [blame] | 192 | /* Do nothing */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 193 | } |
| 194 | |
Alexander Shiyan | a1c25f2 | 2012-10-14 11:05:34 +0400 | [diff] [blame] | 195 | static void uart_clps711x_break_ctl(struct uart_port *port, int break_state) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | { |
| 197 | unsigned long flags; |
| 198 | unsigned int ubrlcr; |
| 199 | |
| 200 | spin_lock_irqsave(&port->lock, flags); |
Alexander Shiyan | ec33552 | 2012-10-14 11:05:29 +0400 | [diff] [blame] | 201 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 202 | ubrlcr = clps_readl(UBRLCR(port)); |
Alexander Shiyan | ec33552 | 2012-10-14 11:05:29 +0400 | [diff] [blame] | 203 | if (break_state) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | ubrlcr |= UBRLCR_BREAK; |
| 205 | else |
| 206 | ubrlcr &= ~UBRLCR_BREAK; |
| 207 | clps_writel(ubrlcr, UBRLCR(port)); |
Alexander Shiyan | ec33552 | 2012-10-14 11:05:29 +0400 | [diff] [blame] | 208 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 209 | spin_unlock_irqrestore(&port->lock, flags); |
| 210 | } |
| 211 | |
Alexander Shiyan | a1c25f2 | 2012-10-14 11:05:34 +0400 | [diff] [blame] | 212 | static int uart_clps711x_startup(struct uart_port *port) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 213 | { |
Alexander Shiyan | 3c7e9eb | 2012-10-14 11:05:25 +0400 | [diff] [blame] | 214 | struct clps711x_port *s = dev_get_drvdata(port->dev); |
Alexander Shiyan | 135cc79 | 2012-10-14 11:05:31 +0400 | [diff] [blame] | 215 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 216 | |
Alexander Shiyan | 3c7e9eb | 2012-10-14 11:05:25 +0400 | [diff] [blame] | 217 | s->tx_enabled[port->line] = 1; |
Alexander Shiyan | 135cc79 | 2012-10-14 11:05:31 +0400 | [diff] [blame] | 218 | /* Allocate the IRQs */ |
| 219 | ret = devm_request_irq(port->dev, TX_IRQ(port), uart_clps711x_int_tx, |
| 220 | 0, UART_CLPS711X_NAME " TX", port); |
| 221 | if (ret) |
| 222 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 223 | |
Alexander Shiyan | 135cc79 | 2012-10-14 11:05:31 +0400 | [diff] [blame] | 224 | ret = devm_request_irq(port->dev, RX_IRQ(port), uart_clps711x_int_rx, |
| 225 | 0, UART_CLPS711X_NAME " RX", port); |
| 226 | if (ret) { |
| 227 | devm_free_irq(port->dev, TX_IRQ(port), port); |
| 228 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | } |
| 230 | |
Alexander Shiyan | f52ede2 | 2012-10-14 11:05:32 +0400 | [diff] [blame] | 231 | /* Disable break */ |
| 232 | clps_writel(clps_readl(UBRLCR(port)) & ~UBRLCR_BREAK, UBRLCR(port)); |
| 233 | |
| 234 | /* Enable the port */ |
| 235 | clps_writel(clps_readl(SYSCON(port)) | SYSCON_UARTEN, SYSCON(port)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | |
| 237 | return 0; |
| 238 | } |
| 239 | |
Alexander Shiyan | a1c25f2 | 2012-10-14 11:05:34 +0400 | [diff] [blame] | 240 | static void uart_clps711x_shutdown(struct uart_port *port) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 241 | { |
Alexander Shiyan | 135cc79 | 2012-10-14 11:05:31 +0400 | [diff] [blame] | 242 | /* Free the interrupts */ |
| 243 | devm_free_irq(port->dev, TX_IRQ(port), port); |
| 244 | devm_free_irq(port->dev, RX_IRQ(port), port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 245 | |
Alexander Shiyan | f52ede2 | 2012-10-14 11:05:32 +0400 | [diff] [blame] | 246 | /* Disable the port */ |
| 247 | clps_writel(clps_readl(SYSCON(port)) & ~SYSCON_UARTEN, SYSCON(port)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 248 | } |
| 249 | |
Alexander Shiyan | a1c25f2 | 2012-10-14 11:05:34 +0400 | [diff] [blame] | 250 | static void uart_clps711x_set_termios(struct uart_port *port, |
| 251 | struct ktermios *termios, |
| 252 | struct ktermios *old) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | { |
| 254 | unsigned int ubrlcr, baud, quot; |
| 255 | unsigned long flags; |
| 256 | |
Alexander Shiyan | 7ae75e9 | 2012-10-14 11:05:33 +0400 | [diff] [blame] | 257 | /* Mask termios capabilities we don't support */ |
| 258 | termios->c_cflag &= ~CMSPAR; |
| 259 | termios->c_iflag &= ~(BRKINT | IGNBRK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 260 | |
Alexander Shiyan | c08f015 | 2012-10-14 11:05:26 +0400 | [diff] [blame] | 261 | /* Ask the core to calculate the divisor for us */ |
| 262 | baud = uart_get_baud_rate(port, termios, old, port->uartclk / 4096, |
| 263 | port->uartclk / 16); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 264 | quot = uart_get_divisor(port, baud); |
| 265 | |
| 266 | switch (termios->c_cflag & CSIZE) { |
| 267 | case CS5: |
| 268 | ubrlcr = UBRLCR_WRDLEN5; |
| 269 | break; |
| 270 | case CS6: |
| 271 | ubrlcr = UBRLCR_WRDLEN6; |
| 272 | break; |
| 273 | case CS7: |
| 274 | ubrlcr = UBRLCR_WRDLEN7; |
| 275 | break; |
Alexander Shiyan | a1c25f2 | 2012-10-14 11:05:34 +0400 | [diff] [blame] | 276 | case CS8: |
| 277 | default: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 278 | ubrlcr = UBRLCR_WRDLEN8; |
| 279 | break; |
| 280 | } |
Alexander Shiyan | 7ae75e9 | 2012-10-14 11:05:33 +0400 | [diff] [blame] | 281 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 282 | if (termios->c_cflag & CSTOPB) |
| 283 | ubrlcr |= UBRLCR_XSTOP; |
Alexander Shiyan | 7ae75e9 | 2012-10-14 11:05:33 +0400 | [diff] [blame] | 284 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 285 | if (termios->c_cflag & PARENB) { |
| 286 | ubrlcr |= UBRLCR_PRTEN; |
| 287 | if (!(termios->c_cflag & PARODD)) |
| 288 | ubrlcr |= UBRLCR_EVENPRT; |
| 289 | } |
Alexander Shiyan | cf03a88 | 2012-10-14 11:05:27 +0400 | [diff] [blame] | 290 | |
| 291 | /* Enable FIFO */ |
| 292 | ubrlcr |= UBRLCR_FIFOEN; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 293 | |
| 294 | spin_lock_irqsave(&port->lock, flags); |
| 295 | |
Alexander Shiyan | 7ae75e9 | 2012-10-14 11:05:33 +0400 | [diff] [blame] | 296 | /* Set read status mask */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 297 | port->read_status_mask = UARTDR_OVERR; |
| 298 | if (termios->c_iflag & INPCK) |
| 299 | port->read_status_mask |= UARTDR_PARERR | UARTDR_FRMERR; |
| 300 | |
Alexander Shiyan | 7ae75e9 | 2012-10-14 11:05:33 +0400 | [diff] [blame] | 301 | /* Set status ignore mask */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 302 | port->ignore_status_mask = 0; |
Alexander Shiyan | 7ae75e9 | 2012-10-14 11:05:33 +0400 | [diff] [blame] | 303 | if (!(termios->c_cflag & CREAD)) |
| 304 | port->ignore_status_mask |= UARTDR_OVERR | UARTDR_PARERR | |
| 305 | UARTDR_FRMERR; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 306 | |
Alexander Shiyan | 7ae75e9 | 2012-10-14 11:05:33 +0400 | [diff] [blame] | 307 | uart_update_timeout(port, termios->c_cflag, baud); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 308 | |
Alexander Shiyan | 7ae75e9 | 2012-10-14 11:05:33 +0400 | [diff] [blame] | 309 | clps_writel(ubrlcr | (quot - 1), UBRLCR(port)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 310 | |
| 311 | spin_unlock_irqrestore(&port->lock, flags); |
| 312 | } |
| 313 | |
Alexander Shiyan | a1c25f2 | 2012-10-14 11:05:34 +0400 | [diff] [blame] | 314 | static const char *uart_clps711x_type(struct uart_port *port) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 315 | { |
Alexander Shiyan | a1c25f2 | 2012-10-14 11:05:34 +0400 | [diff] [blame] | 316 | return (port->type == PORT_CLPS711X) ? "CLPS711X" : NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 317 | } |
| 318 | |
Alexander Shiyan | a1c25f2 | 2012-10-14 11:05:34 +0400 | [diff] [blame] | 319 | static void uart_clps711x_config_port(struct uart_port *port, int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 320 | { |
| 321 | if (flags & UART_CONFIG_TYPE) |
| 322 | port->type = PORT_CLPS711X; |
| 323 | } |
| 324 | |
Alexander Shiyan | a1c25f2 | 2012-10-14 11:05:34 +0400 | [diff] [blame] | 325 | static void uart_clps711x_release_port(struct uart_port *port) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 326 | { |
Alexander Shiyan | a1c25f2 | 2012-10-14 11:05:34 +0400 | [diff] [blame] | 327 | /* Do nothing */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 328 | } |
| 329 | |
Alexander Shiyan | a1c25f2 | 2012-10-14 11:05:34 +0400 | [diff] [blame] | 330 | static int uart_clps711x_request_port(struct uart_port *port) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 331 | { |
Alexander Shiyan | a1c25f2 | 2012-10-14 11:05:34 +0400 | [diff] [blame] | 332 | /* Do nothing */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 333 | return 0; |
| 334 | } |
| 335 | |
Alexander Shiyan | a1c25f2 | 2012-10-14 11:05:34 +0400 | [diff] [blame] | 336 | static const struct uart_ops uart_clps711x_ops = { |
| 337 | .tx_empty = uart_clps711x_tx_empty, |
| 338 | .set_mctrl = uart_clps711x_set_mctrl, |
| 339 | .get_mctrl = uart_clps711x_get_mctrl, |
| 340 | .stop_tx = uart_clps711x_stop_tx, |
| 341 | .start_tx = uart_clps711x_start_tx, |
| 342 | .stop_rx = uart_clps711x_stop_rx, |
| 343 | .enable_ms = uart_clps711x_enable_ms, |
| 344 | .break_ctl = uart_clps711x_break_ctl, |
| 345 | .startup = uart_clps711x_startup, |
| 346 | .shutdown = uart_clps711x_shutdown, |
| 347 | .set_termios = uart_clps711x_set_termios, |
| 348 | .type = uart_clps711x_type, |
| 349 | .config_port = uart_clps711x_config_port, |
| 350 | .release_port = uart_clps711x_release_port, |
| 351 | .request_port = uart_clps711x_request_port, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 352 | }; |
| 353 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 354 | #ifdef CONFIG_SERIAL_CLPS711X_CONSOLE |
Alexander Shiyan | 117d5d4 | 2012-10-14 11:05:24 +0400 | [diff] [blame] | 355 | static void uart_clps711x_console_putchar(struct uart_port *port, int ch) |
Russell King | d358788 | 2006-03-20 20:00:09 +0000 | [diff] [blame] | 356 | { |
| 357 | while (clps_readl(SYSFLG(port)) & SYSFLG_UTXFF) |
| 358 | barrier(); |
Alexander Shiyan | 117d5d4 | 2012-10-14 11:05:24 +0400 | [diff] [blame] | 359 | |
| 360 | clps_writew(ch, UARTDR(port)); |
Russell King | d358788 | 2006-03-20 20:00:09 +0000 | [diff] [blame] | 361 | } |
| 362 | |
Alexander Shiyan | 117d5d4 | 2012-10-14 11:05:24 +0400 | [diff] [blame] | 363 | static void uart_clps711x_console_write(struct console *co, const char *c, |
| 364 | unsigned n) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 365 | { |
Alexander Shiyan | 117d5d4 | 2012-10-14 11:05:24 +0400 | [diff] [blame] | 366 | struct clps711x_port *s = (struct clps711x_port *)co->data; |
| 367 | struct uart_port *port = &s->port[co->index]; |
| 368 | u32 syscon; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 369 | |
Alexander Shiyan | 117d5d4 | 2012-10-14 11:05:24 +0400 | [diff] [blame] | 370 | /* Ensure that the port is enabled */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 371 | syscon = clps_readl(SYSCON(port)); |
| 372 | clps_writel(syscon | SYSCON_UARTEN, SYSCON(port)); |
| 373 | |
Alexander Shiyan | 117d5d4 | 2012-10-14 11:05:24 +0400 | [diff] [blame] | 374 | uart_console_write(port, c, n, uart_clps711x_console_putchar); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 375 | |
Alexander Shiyan | 117d5d4 | 2012-10-14 11:05:24 +0400 | [diff] [blame] | 376 | /* Wait for transmitter to become empty */ |
| 377 | while (clps_readl(SYSFLG(port)) & SYSFLG_UBUSY) |
| 378 | barrier(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 379 | |
Alexander Shiyan | 117d5d4 | 2012-10-14 11:05:24 +0400 | [diff] [blame] | 380 | /* Restore the uart state */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 381 | clps_writel(syscon, SYSCON(port)); |
| 382 | } |
| 383 | |
Alexander Shiyan | 117d5d4 | 2012-10-14 11:05:24 +0400 | [diff] [blame] | 384 | static void uart_clps711x_console_get_options(struct uart_port *port, |
| 385 | int *baud, int *parity, |
| 386 | int *bits) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 387 | { |
| 388 | if (clps_readl(SYSCON(port)) & SYSCON_UARTEN) { |
| 389 | unsigned int ubrlcr, quot; |
| 390 | |
| 391 | ubrlcr = clps_readl(UBRLCR(port)); |
| 392 | |
| 393 | *parity = 'n'; |
| 394 | if (ubrlcr & UBRLCR_PRTEN) { |
| 395 | if (ubrlcr & UBRLCR_EVENPRT) |
| 396 | *parity = 'e'; |
| 397 | else |
| 398 | *parity = 'o'; |
| 399 | } |
| 400 | |
| 401 | if ((ubrlcr & UBRLCR_WRDLEN_MASK) == UBRLCR_WRDLEN7) |
| 402 | *bits = 7; |
| 403 | else |
| 404 | *bits = 8; |
| 405 | |
| 406 | quot = ubrlcr & UBRLCR_BAUD_MASK; |
| 407 | *baud = port->uartclk / (16 * (quot + 1)); |
| 408 | } |
| 409 | } |
| 410 | |
Alexander Shiyan | 117d5d4 | 2012-10-14 11:05:24 +0400 | [diff] [blame] | 411 | static int uart_clps711x_console_setup(struct console *co, char *options) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 412 | { |
Alexander Shiyan | 117d5d4 | 2012-10-14 11:05:24 +0400 | [diff] [blame] | 413 | int baud = 38400, bits = 8, parity = 'n', flow = 'n'; |
| 414 | struct clps711x_port *s = (struct clps711x_port *)co->data; |
| 415 | struct uart_port *port = &s->port[(co->index > 0) ? co->index : 0]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 416 | |
| 417 | if (options) |
| 418 | uart_parse_options(options, &baud, &parity, &bits, &flow); |
| 419 | else |
Alexander Shiyan | 117d5d4 | 2012-10-14 11:05:24 +0400 | [diff] [blame] | 420 | uart_clps711x_console_get_options(port, &baud, &parity, &bits); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 421 | |
| 422 | return uart_set_options(port, co, baud, parity, bits, flow); |
| 423 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 424 | #endif |
| 425 | |
Bill Pemberton | 9671f09 | 2012-11-19 13:21:50 -0500 | [diff] [blame] | 426 | static int uart_clps711x_probe(struct platform_device *pdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 427 | { |
Alexander Shiyan | 117d5d4 | 2012-10-14 11:05:24 +0400 | [diff] [blame] | 428 | struct clps711x_port *s; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 429 | int ret, i; |
| 430 | |
Alexander Shiyan | 117d5d4 | 2012-10-14 11:05:24 +0400 | [diff] [blame] | 431 | s = devm_kzalloc(&pdev->dev, sizeof(struct clps711x_port), GFP_KERNEL); |
| 432 | if (!s) { |
| 433 | dev_err(&pdev->dev, "Error allocating port structure\n"); |
| 434 | return -ENOMEM; |
| 435 | } |
| 436 | platform_set_drvdata(pdev, s); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 437 | |
Alexander Shiyan | c08f015 | 2012-10-14 11:05:26 +0400 | [diff] [blame] | 438 | s->uart_clk = devm_clk_get(&pdev->dev, "uart"); |
| 439 | if (IS_ERR(s->uart_clk)) { |
| 440 | dev_err(&pdev->dev, "Can't get UART clocks\n"); |
Jingoo Han | 43b829b | 2013-06-25 10:08:49 +0900 | [diff] [blame] | 441 | return PTR_ERR(s->uart_clk); |
Alexander Shiyan | c08f015 | 2012-10-14 11:05:26 +0400 | [diff] [blame] | 442 | } |
| 443 | |
Alexander Shiyan | 117d5d4 | 2012-10-14 11:05:24 +0400 | [diff] [blame] | 444 | s->uart.owner = THIS_MODULE; |
| 445 | s->uart.dev_name = "ttyCL"; |
| 446 | s->uart.major = UART_CLPS711X_MAJOR; |
| 447 | s->uart.minor = UART_CLPS711X_MINOR; |
| 448 | s->uart.nr = UART_CLPS711X_NR; |
| 449 | #ifdef CONFIG_SERIAL_CLPS711X_CONSOLE |
| 450 | s->uart.cons = &s->console; |
| 451 | s->uart.cons->device = uart_console_device; |
| 452 | s->uart.cons->write = uart_clps711x_console_write; |
| 453 | s->uart.cons->setup = uart_clps711x_console_setup; |
| 454 | s->uart.cons->flags = CON_PRINTBUFFER; |
| 455 | s->uart.cons->index = -1; |
| 456 | s->uart.cons->data = s; |
| 457 | strcpy(s->uart.cons->name, "ttyCL"); |
| 458 | #endif |
| 459 | ret = uart_register_driver(&s->uart); |
| 460 | if (ret) { |
| 461 | dev_err(&pdev->dev, "Registering UART driver failed\n"); |
Jingoo Han | 43b829b | 2013-06-25 10:08:49 +0900 | [diff] [blame] | 462 | return ret; |
Alexander Shiyan | 117d5d4 | 2012-10-14 11:05:24 +0400 | [diff] [blame] | 463 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 464 | |
Alexander Shiyan | 117d5d4 | 2012-10-14 11:05:24 +0400 | [diff] [blame] | 465 | for (i = 0; i < UART_CLPS711X_NR; i++) { |
| 466 | s->port[i].line = i; |
| 467 | s->port[i].dev = &pdev->dev; |
| 468 | s->port[i].irq = TX_IRQ(&s->port[i]); |
| 469 | s->port[i].iobase = SYSCON(&s->port[i]); |
| 470 | s->port[i].type = PORT_CLPS711X; |
| 471 | s->port[i].fifosize = 16; |
| 472 | s->port[i].flags = UPF_SKIP_TEST | UPF_FIXED_TYPE; |
Alexander Shiyan | c08f015 | 2012-10-14 11:05:26 +0400 | [diff] [blame] | 473 | s->port[i].uartclk = clk_get_rate(s->uart_clk); |
Alexander Shiyan | 117d5d4 | 2012-10-14 11:05:24 +0400 | [diff] [blame] | 474 | s->port[i].ops = &uart_clps711x_ops; |
| 475 | WARN_ON(uart_add_one_port(&s->uart, &s->port[i])); |
| 476 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 477 | |
| 478 | return 0; |
| 479 | } |
| 480 | |
Bill Pemberton | ae8d8a1 | 2012-11-19 13:26:18 -0500 | [diff] [blame] | 481 | static int uart_clps711x_remove(struct platform_device *pdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 482 | { |
Alexander Shiyan | 117d5d4 | 2012-10-14 11:05:24 +0400 | [diff] [blame] | 483 | struct clps711x_port *s = platform_get_drvdata(pdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 484 | int i; |
| 485 | |
Alexander Shiyan | 117d5d4 | 2012-10-14 11:05:24 +0400 | [diff] [blame] | 486 | for (i = 0; i < UART_CLPS711X_NR; i++) |
| 487 | uart_remove_one_port(&s->uart, &s->port[i]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 488 | |
Alexander Shiyan | 117d5d4 | 2012-10-14 11:05:24 +0400 | [diff] [blame] | 489 | uart_unregister_driver(&s->uart); |
Alexander Shiyan | 9511372 | 2012-10-14 11:05:23 +0400 | [diff] [blame] | 490 | |
| 491 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 492 | } |
| 493 | |
Alexander Shiyan | 9511372 | 2012-10-14 11:05:23 +0400 | [diff] [blame] | 494 | static struct platform_driver clps711x_uart_driver = { |
| 495 | .driver = { |
| 496 | .name = UART_CLPS711X_NAME, |
| 497 | .owner = THIS_MODULE, |
| 498 | }, |
| 499 | .probe = uart_clps711x_probe, |
Bill Pemberton | 2d47b71 | 2012-11-19 13:21:34 -0500 | [diff] [blame] | 500 | .remove = uart_clps711x_remove, |
Alexander Shiyan | 9511372 | 2012-10-14 11:05:23 +0400 | [diff] [blame] | 501 | }; |
| 502 | module_platform_driver(clps711x_uart_driver); |
| 503 | |
| 504 | static struct platform_device clps711x_uart_device = { |
| 505 | .name = UART_CLPS711X_NAME, |
| 506 | }; |
| 507 | |
| 508 | static int __init uart_clps711x_init(void) |
| 509 | { |
| 510 | return platform_device_register(&clps711x_uart_device); |
| 511 | } |
| 512 | module_init(uart_clps711x_init); |
| 513 | |
| 514 | static void __exit uart_clps711x_exit(void) |
| 515 | { |
| 516 | platform_device_unregister(&clps711x_uart_device); |
| 517 | } |
| 518 | module_exit(uart_clps711x_exit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 519 | |
| 520 | MODULE_AUTHOR("Deep Blue Solutions Ltd"); |
Alexander Shiyan | 9511372 | 2012-10-14 11:05:23 +0400 | [diff] [blame] | 521 | MODULE_DESCRIPTION("CLPS711X serial driver"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 522 | MODULE_LICENSE("GPL"); |