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