Feng Tang | 2251099 | 2010-06-16 14:46:09 +0100 | [diff] [blame] | 1 | /* |
Feng Tang | ee9b450 | 2010-09-13 15:39:48 +0800 | [diff] [blame] | 2 | * mrst_max3110.c - spi uart protocol driver for Maxim 3110 |
Feng Tang | 2251099 | 2010-06-16 14:46:09 +0100 | [diff] [blame] | 3 | * |
Feng Tang | ee9b450 | 2010-09-13 15:39:48 +0800 | [diff] [blame] | 4 | * Copyright (c) 2008-2010, Intel Corporation. |
Feng Tang | 2251099 | 2010-06-16 14:46:09 +0100 | [diff] [blame] | 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify it |
| 7 | * under the terms and conditions of the GNU General Public License, |
| 8 | * version 2, as published by the Free Software Foundation. |
| 9 | * |
| 10 | * This program is distributed in the hope it will be useful, but WITHOUT |
| 11 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 12 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 13 | * more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License along with |
| 16 | * this program; if not, write to the Free Software Foundation, Inc., |
| 17 | * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | */ |
| 19 | |
| 20 | /* |
| 21 | * Note: |
| 22 | * 1. From Max3110 spec, the Rx FIFO has 8 words, while the Tx FIFO only has |
| 23 | * 1 word. If SPI master controller doesn't support sclk frequency change, |
| 24 | * then the char need be sent out one by one with some delay |
| 25 | * |
Vitaliy Ivanov | fb914eb | 2011-06-23 20:01:55 +0300 | [diff] [blame] | 26 | * 2. Currently only RX available interrupt is used, no need for waiting TXE |
Feng Tang | 2251099 | 2010-06-16 14:46:09 +0100 | [diff] [blame] | 27 | * interrupt for a low speed UART device |
| 28 | */ |
| 29 | |
Alexander Shishkin | 51808f0 | 2011-08-26 11:25:35 +0100 | [diff] [blame] | 30 | #ifdef CONFIG_MAGIC_SYSRQ |
| 31 | #define SUPPORT_SYSRQ |
| 32 | #endif |
| 33 | |
Feng Tang | 2251099 | 2010-06-16 14:46:09 +0100 | [diff] [blame] | 34 | #include <linux/module.h> |
| 35 | #include <linux/ioport.h> |
Andrew Morton | c044391 | 2010-09-30 15:15:29 -0700 | [diff] [blame] | 36 | #include <linux/irq.h> |
Feng Tang | 2251099 | 2010-06-16 14:46:09 +0100 | [diff] [blame] | 37 | #include <linux/init.h> |
| 38 | #include <linux/console.h> |
Feng Tang | 2251099 | 2010-06-16 14:46:09 +0100 | [diff] [blame] | 39 | #include <linux/tty.h> |
| 40 | #include <linux/tty_flip.h> |
| 41 | #include <linux/serial_core.h> |
| 42 | #include <linux/serial_reg.h> |
| 43 | |
| 44 | #include <linux/kthread.h> |
Feng Tang | 2251099 | 2010-06-16 14:46:09 +0100 | [diff] [blame] | 45 | #include <linux/spi/spi.h> |
Feng Tang | 2251099 | 2010-06-16 14:46:09 +0100 | [diff] [blame] | 46 | |
| 47 | #include "mrst_max3110.h" |
| 48 | |
| 49 | #define PR_FMT "mrst_max3110: " |
| 50 | |
Arjan van de Ven | d6e679b | 2010-06-17 11:02:15 +0100 | [diff] [blame] | 51 | #define UART_TX_NEEDED 1 |
| 52 | #define CON_TX_NEEDED 2 |
| 53 | #define BIT_IRQ_PENDING 3 |
| 54 | |
Feng Tang | 2251099 | 2010-06-16 14:46:09 +0100 | [diff] [blame] | 55 | struct uart_max3110 { |
| 56 | struct uart_port port; |
| 57 | struct spi_device *spi; |
Dan Carpenter | d8653d3 | 2011-01-25 14:15:11 +0000 | [diff] [blame] | 58 | char name[SPI_NAME_SIZE]; |
Feng Tang | 2251099 | 2010-06-16 14:46:09 +0100 | [diff] [blame] | 59 | |
| 60 | wait_queue_head_t wq; |
| 61 | struct task_struct *main_thread; |
| 62 | struct task_struct *read_thread; |
Justin P. Mattock | 6eab04a | 2011-04-08 19:49:08 -0700 | [diff] [blame] | 63 | struct mutex thread_mutex; |
Feng Tang | 2251099 | 2010-06-16 14:46:09 +0100 | [diff] [blame] | 64 | |
| 65 | u32 baud; |
| 66 | u16 cur_conf; |
| 67 | u8 clock; |
| 68 | u8 parity, word_7bits; |
Feng Tang | ee9b450 | 2010-09-13 15:39:48 +0800 | [diff] [blame] | 69 | u16 irq; |
Feng Tang | 2251099 | 2010-06-16 14:46:09 +0100 | [diff] [blame] | 70 | |
Arjan van de Ven | d6e679b | 2010-06-17 11:02:15 +0100 | [diff] [blame] | 71 | unsigned long uart_flags; |
Feng Tang | 2251099 | 2010-06-16 14:46:09 +0100 | [diff] [blame] | 72 | |
| 73 | /* console related */ |
| 74 | struct circ_buf con_xmit; |
Feng Tang | 2251099 | 2010-06-16 14:46:09 +0100 | [diff] [blame] | 75 | }; |
| 76 | |
| 77 | /* global data structure, may need be removed */ |
Feng Tang | ee9b450 | 2010-09-13 15:39:48 +0800 | [diff] [blame] | 78 | static struct uart_max3110 *pmax; |
| 79 | |
Alexander Shishkin | 51808f0 | 2011-08-26 11:25:35 +0100 | [diff] [blame] | 80 | static int receive_chars(struct uart_max3110 *max, |
| 81 | unsigned short *str, int len); |
| 82 | static int max3110_read_multi(struct uart_max3110 *max); |
Feng Tang | ee9b450 | 2010-09-13 15:39:48 +0800 | [diff] [blame] | 83 | static void max3110_con_receive(struct uart_max3110 *max); |
Feng Tang | 2251099 | 2010-06-16 14:46:09 +0100 | [diff] [blame] | 84 | |
Feng Tang | ee9b450 | 2010-09-13 15:39:48 +0800 | [diff] [blame] | 85 | static int max3110_write_then_read(struct uart_max3110 *max, |
| 86 | const void *txbuf, void *rxbuf, unsigned len, int always_fast) |
Feng Tang | 2251099 | 2010-06-16 14:46:09 +0100 | [diff] [blame] | 87 | { |
| 88 | struct spi_device *spi = max->spi; |
| 89 | struct spi_message message; |
| 90 | struct spi_transfer x; |
| 91 | int ret; |
| 92 | |
Feng Tang | 2251099 | 2010-06-16 14:46:09 +0100 | [diff] [blame] | 93 | spi_message_init(&message); |
| 94 | memset(&x, 0, sizeof x); |
| 95 | x.len = len; |
| 96 | x.tx_buf = txbuf; |
| 97 | x.rx_buf = rxbuf; |
| 98 | spi_message_add_tail(&x, &message); |
| 99 | |
| 100 | if (always_fast) |
Feng Tang | ee9b450 | 2010-09-13 15:39:48 +0800 | [diff] [blame] | 101 | x.speed_hz = spi->max_speed_hz; |
Feng Tang | 2251099 | 2010-06-16 14:46:09 +0100 | [diff] [blame] | 102 | else if (max->baud) |
| 103 | x.speed_hz = max->baud; |
| 104 | |
| 105 | /* Do the i/o */ |
| 106 | ret = spi_sync(spi, &message); |
| 107 | return ret; |
| 108 | } |
| 109 | |
Feng Tang | ee9b450 | 2010-09-13 15:39:48 +0800 | [diff] [blame] | 110 | /* Write a 16b word to the device */ |
| 111 | static int max3110_out(struct uart_max3110 *max, const u16 out) |
Feng Tang | 2251099 | 2010-06-16 14:46:09 +0100 | [diff] [blame] | 112 | { |
Feng Tang | ee9b450 | 2010-09-13 15:39:48 +0800 | [diff] [blame] | 113 | void *buf; |
| 114 | u16 *obuf, *ibuf; |
Feng Tang | 2251099 | 2010-06-16 14:46:09 +0100 | [diff] [blame] | 115 | int ret; |
| 116 | |
Feng Tang | ee9b450 | 2010-09-13 15:39:48 +0800 | [diff] [blame] | 117 | buf = kzalloc(8, GFP_KERNEL | GFP_DMA); |
| 118 | if (!buf) |
| 119 | return -ENOMEM; |
| 120 | |
| 121 | obuf = buf; |
| 122 | ibuf = buf + 4; |
| 123 | *obuf = out; |
| 124 | ret = max3110_write_then_read(max, obuf, ibuf, 2, 1); |
| 125 | if (ret) { |
| 126 | pr_warning(PR_FMT "%s(): get err msg %d when sending 0x%x\n", |
| 127 | __func__, ret, out); |
| 128 | goto exit; |
| 129 | } |
Feng Tang | 2251099 | 2010-06-16 14:46:09 +0100 | [diff] [blame] | 130 | |
Alexander Shishkin | 51808f0 | 2011-08-26 11:25:35 +0100 | [diff] [blame] | 131 | receive_chars(max, ibuf, 1); |
Feng Tang | 2251099 | 2010-06-16 14:46:09 +0100 | [diff] [blame] | 132 | |
Feng Tang | ee9b450 | 2010-09-13 15:39:48 +0800 | [diff] [blame] | 133 | exit: |
| 134 | kfree(buf); |
Feng Tang | 2251099 | 2010-06-16 14:46:09 +0100 | [diff] [blame] | 135 | return ret; |
| 136 | } |
| 137 | |
Feng Tang | 2251099 | 2010-06-16 14:46:09 +0100 | [diff] [blame] | 138 | /* |
| 139 | * This is usually used to read data from SPIC RX FIFO, which doesn't |
Feng Tang | ee9b450 | 2010-09-13 15:39:48 +0800 | [diff] [blame] | 140 | * need any delay like flushing character out. |
| 141 | * |
| 142 | * Return how many valide bytes are read back |
Feng Tang | 2251099 | 2010-06-16 14:46:09 +0100 | [diff] [blame] | 143 | */ |
Alexander Shishkin | 51808f0 | 2011-08-26 11:25:35 +0100 | [diff] [blame] | 144 | static int max3110_read_multi(struct uart_max3110 *max) |
Feng Tang | 2251099 | 2010-06-16 14:46:09 +0100 | [diff] [blame] | 145 | { |
Feng Tang | ee9b450 | 2010-09-13 15:39:48 +0800 | [diff] [blame] | 146 | void *buf; |
| 147 | u16 *obuf, *ibuf; |
Alexander Shishkin | 51808f0 | 2011-08-26 11:25:35 +0100 | [diff] [blame] | 148 | int ret, blen; |
Feng Tang | 2251099 | 2010-06-16 14:46:09 +0100 | [diff] [blame] | 149 | |
Feng Tang | ee9b450 | 2010-09-13 15:39:48 +0800 | [diff] [blame] | 150 | blen = M3110_RX_FIFO_DEPTH * sizeof(u16); |
| 151 | buf = kzalloc(blen * 2, GFP_KERNEL | GFP_DMA); |
| 152 | if (!buf) { |
| 153 | pr_warning(PR_FMT "%s(): fail to alloc dma buffer\n", __func__); |
Feng Tang | 2251099 | 2010-06-16 14:46:09 +0100 | [diff] [blame] | 154 | return 0; |
| 155 | } |
| 156 | |
Feng Tang | ee9b450 | 2010-09-13 15:39:48 +0800 | [diff] [blame] | 157 | /* tx/rx always have the same length */ |
| 158 | obuf = buf; |
| 159 | ibuf = buf + blen; |
Feng Tang | 2251099 | 2010-06-16 14:46:09 +0100 | [diff] [blame] | 160 | |
Feng Tang | ee9b450 | 2010-09-13 15:39:48 +0800 | [diff] [blame] | 161 | if (max3110_write_then_read(max, obuf, ibuf, blen, 1)) { |
| 162 | kfree(buf); |
Feng Tang | 2251099 | 2010-06-16 14:46:09 +0100 | [diff] [blame] | 163 | return 0; |
Feng Tang | ee9b450 | 2010-09-13 15:39:48 +0800 | [diff] [blame] | 164 | } |
Feng Tang | 2251099 | 2010-06-16 14:46:09 +0100 | [diff] [blame] | 165 | |
Alexander Shishkin | 51808f0 | 2011-08-26 11:25:35 +0100 | [diff] [blame] | 166 | ret = receive_chars(max, ibuf, M3110_RX_FIFO_DEPTH); |
Feng Tang | 2251099 | 2010-06-16 14:46:09 +0100 | [diff] [blame] | 167 | |
Feng Tang | ee9b450 | 2010-09-13 15:39:48 +0800 | [diff] [blame] | 168 | kfree(buf); |
Alexander Shishkin | 51808f0 | 2011-08-26 11:25:35 +0100 | [diff] [blame] | 169 | return ret; |
Feng Tang | 2251099 | 2010-06-16 14:46:09 +0100 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | static void serial_m3110_con_putchar(struct uart_port *port, int ch) |
| 173 | { |
| 174 | struct uart_max3110 *max = |
| 175 | container_of(port, struct uart_max3110, port); |
| 176 | struct circ_buf *xmit = &max->con_xmit; |
| 177 | |
| 178 | if (uart_circ_chars_free(xmit)) { |
| 179 | xmit->buf[xmit->head] = (char)ch; |
| 180 | xmit->head = (xmit->head + 1) & (PAGE_SIZE - 1); |
| 181 | } |
Feng Tang | 2251099 | 2010-06-16 14:46:09 +0100 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | /* |
| 185 | * Print a string to the serial port trying not to disturb |
| 186 | * any possible real use of the port... |
| 187 | * |
| 188 | * The console_lock must be held when we get here. |
| 189 | */ |
| 190 | static void serial_m3110_con_write(struct console *co, |
| 191 | const char *s, unsigned int count) |
| 192 | { |
| 193 | if (!pmax) |
| 194 | return; |
| 195 | |
| 196 | uart_console_write(&pmax->port, s, count, serial_m3110_con_putchar); |
Feng Tang | ee9b450 | 2010-09-13 15:39:48 +0800 | [diff] [blame] | 197 | |
| 198 | if (!test_and_set_bit(CON_TX_NEEDED, &pmax->uart_flags)) |
Dirk Brandewie | 7b18bd5 | 2011-08-26 11:24:49 +0100 | [diff] [blame] | 199 | wake_up(&pmax->wq); |
Feng Tang | 2251099 | 2010-06-16 14:46:09 +0100 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | static int __init |
| 203 | serial_m3110_con_setup(struct console *co, char *options) |
| 204 | { |
| 205 | struct uart_max3110 *max = pmax; |
| 206 | int baud = 115200; |
| 207 | int bits = 8; |
| 208 | int parity = 'n'; |
| 209 | int flow = 'n'; |
| 210 | |
| 211 | pr_info(PR_FMT "setting up console\n"); |
| 212 | |
Feng Tang | ee9b450 | 2010-09-13 15:39:48 +0800 | [diff] [blame] | 213 | if (co->index == -1) |
| 214 | co->index = 0; |
| 215 | |
Feng Tang | 2251099 | 2010-06-16 14:46:09 +0100 | [diff] [blame] | 216 | if (!max) { |
| 217 | pr_err(PR_FMT "pmax is NULL, return"); |
| 218 | return -ENODEV; |
| 219 | } |
| 220 | |
| 221 | if (options) |
| 222 | uart_parse_options(options, &baud, &parity, &bits, &flow); |
| 223 | |
| 224 | return uart_set_options(&max->port, co, baud, parity, bits, flow); |
| 225 | } |
| 226 | |
| 227 | static struct tty_driver *serial_m3110_con_device(struct console *co, |
| 228 | int *index) |
| 229 | { |
| 230 | struct uart_driver *p = co->data; |
| 231 | *index = co->index; |
| 232 | return p->tty_driver; |
| 233 | } |
| 234 | |
| 235 | static struct uart_driver serial_m3110_reg; |
| 236 | static struct console serial_m3110_console = { |
| 237 | .name = "ttyS", |
| 238 | .write = serial_m3110_con_write, |
| 239 | .device = serial_m3110_con_device, |
| 240 | .setup = serial_m3110_con_setup, |
| 241 | .flags = CON_PRINTBUFFER, |
| 242 | .index = -1, |
| 243 | .data = &serial_m3110_reg, |
| 244 | }; |
| 245 | |
Feng Tang | 2251099 | 2010-06-16 14:46:09 +0100 | [diff] [blame] | 246 | static unsigned int serial_m3110_tx_empty(struct uart_port *port) |
| 247 | { |
| 248 | return 1; |
| 249 | } |
| 250 | |
| 251 | static void serial_m3110_stop_tx(struct uart_port *port) |
| 252 | { |
| 253 | return; |
| 254 | } |
| 255 | |
| 256 | /* stop_rx will be called in spin_lock env */ |
| 257 | static void serial_m3110_stop_rx(struct uart_port *port) |
| 258 | { |
| 259 | return; |
| 260 | } |
| 261 | |
| 262 | #define WORDS_PER_XFER 128 |
Feng Tang | ee9b450 | 2010-09-13 15:39:48 +0800 | [diff] [blame] | 263 | static void send_circ_buf(struct uart_max3110 *max, |
Feng Tang | 2251099 | 2010-06-16 14:46:09 +0100 | [diff] [blame] | 264 | struct circ_buf *xmit) |
| 265 | { |
Feng Tang | ee9b450 | 2010-09-13 15:39:48 +0800 | [diff] [blame] | 266 | void *buf; |
| 267 | u16 *obuf, *ibuf; |
Alexander Shishkin | 51808f0 | 2011-08-26 11:25:35 +0100 | [diff] [blame] | 268 | int i, len, blen, dma_size, left, ret = 0; |
Feng Tang | ee9b450 | 2010-09-13 15:39:48 +0800 | [diff] [blame] | 269 | |
| 270 | |
| 271 | dma_size = WORDS_PER_XFER * sizeof(u16) * 2; |
| 272 | buf = kzalloc(dma_size, GFP_KERNEL | GFP_DMA); |
| 273 | if (!buf) |
| 274 | return; |
| 275 | obuf = buf; |
| 276 | ibuf = buf + dma_size/2; |
Feng Tang | 2251099 | 2010-06-16 14:46:09 +0100 | [diff] [blame] | 277 | |
| 278 | while (!uart_circ_empty(xmit)) { |
| 279 | left = uart_circ_chars_pending(xmit); |
| 280 | while (left) { |
Feng Tang | ee9b450 | 2010-09-13 15:39:48 +0800 | [diff] [blame] | 281 | len = min(left, WORDS_PER_XFER); |
| 282 | blen = len * sizeof(u16); |
| 283 | memset(ibuf, 0, blen); |
Feng Tang | 2251099 | 2010-06-16 14:46:09 +0100 | [diff] [blame] | 284 | |
Feng Tang | 2251099 | 2010-06-16 14:46:09 +0100 | [diff] [blame] | 285 | for (i = 0; i < len; i++) { |
| 286 | obuf[i] = (u8)xmit->buf[xmit->tail] | WD_TAG; |
| 287 | xmit->tail = (xmit->tail + 1) & |
| 288 | (UART_XMIT_SIZE - 1); |
| 289 | } |
Feng Tang | ee9b450 | 2010-09-13 15:39:48 +0800 | [diff] [blame] | 290 | |
| 291 | /* Fail to send msg to console is not very critical */ |
Dirk Brandewie | 7b18bd5 | 2011-08-26 11:24:49 +0100 | [diff] [blame] | 292 | |
Feng Tang | ee9b450 | 2010-09-13 15:39:48 +0800 | [diff] [blame] | 293 | ret = max3110_write_then_read(max, obuf, ibuf, blen, 0); |
| 294 | if (ret) |
| 295 | pr_warning(PR_FMT "%s(): get err msg %d\n", |
| 296 | __func__, ret); |
Feng Tang | 2251099 | 2010-06-16 14:46:09 +0100 | [diff] [blame] | 297 | |
Alexander Shishkin | 51808f0 | 2011-08-26 11:25:35 +0100 | [diff] [blame] | 298 | receive_chars(max, ibuf, len); |
Feng Tang | 2251099 | 2010-06-16 14:46:09 +0100 | [diff] [blame] | 299 | |
| 300 | max->port.icount.tx += len; |
| 301 | left -= len; |
| 302 | } |
| 303 | } |
Feng Tang | ee9b450 | 2010-09-13 15:39:48 +0800 | [diff] [blame] | 304 | |
| 305 | kfree(buf); |
Feng Tang | 2251099 | 2010-06-16 14:46:09 +0100 | [diff] [blame] | 306 | } |
| 307 | |
| 308 | static void transmit_char(struct uart_max3110 *max) |
| 309 | { |
| 310 | struct uart_port *port = &max->port; |
| 311 | struct circ_buf *xmit = &port->state->xmit; |
| 312 | |
| 313 | if (uart_circ_empty(xmit) || uart_tx_stopped(port)) |
| 314 | return; |
| 315 | |
| 316 | send_circ_buf(max, xmit); |
| 317 | |
| 318 | if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS) |
| 319 | uart_write_wakeup(port); |
| 320 | |
| 321 | if (uart_circ_empty(xmit)) |
| 322 | serial_m3110_stop_tx(port); |
| 323 | } |
| 324 | |
Feng Tang | ee9b450 | 2010-09-13 15:39:48 +0800 | [diff] [blame] | 325 | /* |
| 326 | * This will be called by uart_write() and tty_write, can't |
| 327 | * go to sleep |
| 328 | */ |
Feng Tang | 2251099 | 2010-06-16 14:46:09 +0100 | [diff] [blame] | 329 | static void serial_m3110_start_tx(struct uart_port *port) |
| 330 | { |
| 331 | struct uart_max3110 *max = |
| 332 | container_of(port, struct uart_max3110, port); |
| 333 | |
Arjan van de Ven | d6e679b | 2010-06-17 11:02:15 +0100 | [diff] [blame] | 334 | if (!test_and_set_bit(UART_TX_NEEDED, &max->uart_flags)) |
Dirk Brandewie | 7b18bd5 | 2011-08-26 11:24:49 +0100 | [diff] [blame] | 335 | wake_up(&max->wq); |
Feng Tang | 2251099 | 2010-06-16 14:46:09 +0100 | [diff] [blame] | 336 | } |
| 337 | |
Alexander Shishkin | 51808f0 | 2011-08-26 11:25:35 +0100 | [diff] [blame] | 338 | static int |
| 339 | receive_chars(struct uart_max3110 *max, unsigned short *str, int len) |
Feng Tang | 2251099 | 2010-06-16 14:46:09 +0100 | [diff] [blame] | 340 | { |
| 341 | struct uart_port *port = &max->port; |
Jiri Slaby | 227434f | 2013-01-03 15:53:01 +0100 | [diff] [blame] | 342 | struct tty_port *tport; |
Feng Tang | 2251099 | 2010-06-16 14:46:09 +0100 | [diff] [blame] | 343 | struct tty_struct *tty; |
Alexander Shishkin | 51808f0 | 2011-08-26 11:25:35 +0100 | [diff] [blame] | 344 | char buf[M3110_RX_FIFO_DEPTH]; |
| 345 | int r, w, usable; |
Feng Tang | 2251099 | 2010-06-16 14:46:09 +0100 | [diff] [blame] | 346 | |
| 347 | /* If uart is not opened, just return */ |
| 348 | if (!port->state) |
Alexander Shishkin | 51808f0 | 2011-08-26 11:25:35 +0100 | [diff] [blame] | 349 | return 0; |
Feng Tang | 2251099 | 2010-06-16 14:46:09 +0100 | [diff] [blame] | 350 | |
Jiri Slaby | 227434f | 2013-01-03 15:53:01 +0100 | [diff] [blame] | 351 | tport = &port->state->port; |
| 352 | tty = tty_port_tty_get(tport); |
Feng Tang | 2251099 | 2010-06-16 14:46:09 +0100 | [diff] [blame] | 353 | if (!tty) |
Alexander Shishkin | 51808f0 | 2011-08-26 11:25:35 +0100 | [diff] [blame] | 354 | return 0; |
Feng Tang | 2251099 | 2010-06-16 14:46:09 +0100 | [diff] [blame] | 355 | |
Alexander Shishkin | 51808f0 | 2011-08-26 11:25:35 +0100 | [diff] [blame] | 356 | for (r = 0, w = 0; r < len; r++) { |
| 357 | if (str[r] & MAX3110_BREAK && |
| 358 | uart_handle_break(port)) |
| 359 | continue; |
| 360 | |
| 361 | if (str[r] & MAX3110_READ_DATA_AVAILABLE) { |
| 362 | if (uart_handle_sysrq_char(port, str[r] & 0xff)) |
| 363 | continue; |
| 364 | |
| 365 | buf[w++] = str[r] & 0xff; |
| 366 | } |
| 367 | } |
| 368 | |
Alan Cox | da4e40e | 2011-08-26 11:26:00 +0100 | [diff] [blame] | 369 | if (!w) { |
| 370 | tty_kref_put(tty); |
Alexander Shishkin | 51808f0 | 2011-08-26 11:25:35 +0100 | [diff] [blame] | 371 | return 0; |
Alan Cox | da4e40e | 2011-08-26 11:26:00 +0100 | [diff] [blame] | 372 | } |
Alexander Shishkin | 51808f0 | 2011-08-26 11:25:35 +0100 | [diff] [blame] | 373 | |
| 374 | for (r = 0; w; r += usable, w -= usable) { |
Jiri Slaby | 227434f | 2013-01-03 15:53:01 +0100 | [diff] [blame] | 375 | usable = tty_buffer_request_room(tport, w); |
Feng Tang | 2251099 | 2010-06-16 14:46:09 +0100 | [diff] [blame] | 376 | if (usable) { |
Alexander Shishkin | 51808f0 | 2011-08-26 11:25:35 +0100 | [diff] [blame] | 377 | tty_insert_flip_string(tty, buf + r, usable); |
Feng Tang | 2251099 | 2010-06-16 14:46:09 +0100 | [diff] [blame] | 378 | port->icount.rx += usable; |
Feng Tang | 2251099 | 2010-06-16 14:46:09 +0100 | [diff] [blame] | 379 | } |
Feng Tang | 2251099 | 2010-06-16 14:46:09 +0100 | [diff] [blame] | 380 | } |
Feng Tang | ee9b450 | 2010-09-13 15:39:48 +0800 | [diff] [blame] | 381 | tty_flip_buffer_push(tty); |
Alan Cox | da4e40e | 2011-08-26 11:26:00 +0100 | [diff] [blame] | 382 | tty_kref_put(tty); |
Alexander Shishkin | 51808f0 | 2011-08-26 11:25:35 +0100 | [diff] [blame] | 383 | |
| 384 | return r; |
Feng Tang | 2251099 | 2010-06-16 14:46:09 +0100 | [diff] [blame] | 385 | } |
| 386 | |
Feng Tang | ee9b450 | 2010-09-13 15:39:48 +0800 | [diff] [blame] | 387 | /* |
| 388 | * This routine will be used in read_thread or RX IRQ handling, |
| 389 | * it will first do one round buffer read(8 words), if there is some |
| 390 | * valid RX data, will try to read 5 more rounds till all data |
| 391 | * is read out. |
| 392 | * |
| 393 | * Use stack space as data buffer to save some system load, and chose |
| 394 | * 504 Btyes as a threadhold to do a bulk push to upper tty layer when |
| 395 | * receiving bulk data, a much bigger buffer may cause stack overflow |
| 396 | */ |
| 397 | static void max3110_con_receive(struct uart_max3110 *max) |
Feng Tang | 2251099 | 2010-06-16 14:46:09 +0100 | [diff] [blame] | 398 | { |
Alexander Shishkin | 51808f0 | 2011-08-26 11:25:35 +0100 | [diff] [blame] | 399 | int loop = 1, num; |
Feng Tang | 2251099 | 2010-06-16 14:46:09 +0100 | [diff] [blame] | 400 | |
Feng Tang | 2251099 | 2010-06-16 14:46:09 +0100 | [diff] [blame] | 401 | do { |
Alexander Shishkin | 51808f0 | 2011-08-26 11:25:35 +0100 | [diff] [blame] | 402 | num = max3110_read_multi(max); |
Feng Tang | 2251099 | 2010-06-16 14:46:09 +0100 | [diff] [blame] | 403 | |
| 404 | if (num) { |
Feng Tang | ee9b450 | 2010-09-13 15:39:48 +0800 | [diff] [blame] | 405 | loop = 5; |
Feng Tang | 2251099 | 2010-06-16 14:46:09 +0100 | [diff] [blame] | 406 | } |
| 407 | } while (--loop); |
Feng Tang | 2251099 | 2010-06-16 14:46:09 +0100 | [diff] [blame] | 408 | } |
| 409 | |
| 410 | static int max3110_main_thread(void *_max) |
| 411 | { |
| 412 | struct uart_max3110 *max = _max; |
| 413 | wait_queue_head_t *wq = &max->wq; |
| 414 | int ret = 0; |
| 415 | struct circ_buf *xmit = &max->con_xmit; |
| 416 | |
Feng Tang | 2251099 | 2010-06-16 14:46:09 +0100 | [diff] [blame] | 417 | pr_info(PR_FMT "start main thread\n"); |
| 418 | |
| 419 | do { |
Dirk Brandewie | 7b18bd5 | 2011-08-26 11:24:49 +0100 | [diff] [blame] | 420 | wait_event_interruptible(*wq, |
| 421 | max->uart_flags || kthread_should_stop()); |
Arjan van de Ven | 68c16b4 | 2010-06-17 11:02:06 +0100 | [diff] [blame] | 422 | |
| 423 | mutex_lock(&max->thread_mutex); |
Feng Tang | 2251099 | 2010-06-16 14:46:09 +0100 | [diff] [blame] | 424 | |
Arjan van de Ven | d6e679b | 2010-06-17 11:02:15 +0100 | [diff] [blame] | 425 | if (test_and_clear_bit(BIT_IRQ_PENDING, &max->uart_flags)) |
Feng Tang | ee9b450 | 2010-09-13 15:39:48 +0800 | [diff] [blame] | 426 | max3110_con_receive(max); |
Feng Tang | 2251099 | 2010-06-16 14:46:09 +0100 | [diff] [blame] | 427 | |
| 428 | /* first handle console output */ |
Arjan van de Ven | d6e679b | 2010-06-17 11:02:15 +0100 | [diff] [blame] | 429 | if (test_and_clear_bit(CON_TX_NEEDED, &max->uart_flags)) |
Feng Tang | 2251099 | 2010-06-16 14:46:09 +0100 | [diff] [blame] | 430 | send_circ_buf(max, xmit); |
Feng Tang | 2251099 | 2010-06-16 14:46:09 +0100 | [diff] [blame] | 431 | |
| 432 | /* handle uart output */ |
Arjan van de Ven | d6e679b | 2010-06-17 11:02:15 +0100 | [diff] [blame] | 433 | if (test_and_clear_bit(UART_TX_NEEDED, &max->uart_flags)) |
Feng Tang | 2251099 | 2010-06-16 14:46:09 +0100 | [diff] [blame] | 434 | transmit_char(max); |
Arjan van de Ven | d6e679b | 2010-06-17 11:02:15 +0100 | [diff] [blame] | 435 | |
Arjan van de Ven | 68c16b4 | 2010-06-17 11:02:06 +0100 | [diff] [blame] | 436 | mutex_unlock(&max->thread_mutex); |
Arjan van de Ven | d6e679b | 2010-06-17 11:02:15 +0100 | [diff] [blame] | 437 | |
Feng Tang | 2251099 | 2010-06-16 14:46:09 +0100 | [diff] [blame] | 438 | } while (!kthread_should_stop()); |
| 439 | |
| 440 | return ret; |
| 441 | } |
| 442 | |
Feng Tang | 2251099 | 2010-06-16 14:46:09 +0100 | [diff] [blame] | 443 | static irqreturn_t serial_m3110_irq(int irq, void *dev_id) |
| 444 | { |
| 445 | struct uart_max3110 *max = dev_id; |
| 446 | |
| 447 | /* max3110's irq is a falling edge, not level triggered, |
| 448 | * so no need to disable the irq */ |
Dirk Brandewie | 7b18bd5 | 2011-08-26 11:24:49 +0100 | [diff] [blame] | 449 | |
Arjan van de Ven | d6e679b | 2010-06-17 11:02:15 +0100 | [diff] [blame] | 450 | if (!test_and_set_bit(BIT_IRQ_PENDING, &max->uart_flags)) |
Dirk Brandewie | 7b18bd5 | 2011-08-26 11:24:49 +0100 | [diff] [blame] | 451 | wake_up(&max->wq); |
Arjan van de Ven | d6e679b | 2010-06-17 11:02:15 +0100 | [diff] [blame] | 452 | |
Feng Tang | 2251099 | 2010-06-16 14:46:09 +0100 | [diff] [blame] | 453 | return IRQ_HANDLED; |
| 454 | } |
Alan Cox | 91efa75 | 2010-09-13 15:39:56 +0800 | [diff] [blame] | 455 | |
Feng Tang | 2251099 | 2010-06-16 14:46:09 +0100 | [diff] [blame] | 456 | /* if don't use RX IRQ, then need a thread to polling read */ |
| 457 | static int max3110_read_thread(void *_max) |
| 458 | { |
| 459 | struct uart_max3110 *max = _max; |
| 460 | |
| 461 | pr_info(PR_FMT "start read thread\n"); |
| 462 | do { |
Feng Tang | ee9b450 | 2010-09-13 15:39:48 +0800 | [diff] [blame] | 463 | /* |
| 464 | * If can't acquire the mutex, it means the main thread |
| 465 | * is running which will also perform the rx job |
| 466 | */ |
| 467 | if (mutex_trylock(&max->thread_mutex)) { |
| 468 | max3110_con_receive(max); |
| 469 | mutex_unlock(&max->thread_mutex); |
| 470 | } |
Feng Tang | 2251099 | 2010-06-16 14:46:09 +0100 | [diff] [blame] | 471 | |
| 472 | set_current_state(TASK_INTERRUPTIBLE); |
| 473 | schedule_timeout(HZ / 20); |
| 474 | } while (!kthread_should_stop()); |
| 475 | |
| 476 | return 0; |
| 477 | } |
Feng Tang | 2251099 | 2010-06-16 14:46:09 +0100 | [diff] [blame] | 478 | |
| 479 | static int serial_m3110_startup(struct uart_port *port) |
| 480 | { |
| 481 | struct uart_max3110 *max = |
| 482 | container_of(port, struct uart_max3110, port); |
| 483 | u16 config = 0; |
| 484 | int ret = 0; |
| 485 | |
Feng Tang | ee9b450 | 2010-09-13 15:39:48 +0800 | [diff] [blame] | 486 | if (port->line != 0) { |
Feng Tang | 2251099 | 2010-06-16 14:46:09 +0100 | [diff] [blame] | 487 | pr_err(PR_FMT "uart port startup failed\n"); |
Feng Tang | ee9b450 | 2010-09-13 15:39:48 +0800 | [diff] [blame] | 488 | return -1; |
| 489 | } |
Feng Tang | 2251099 | 2010-06-16 14:46:09 +0100 | [diff] [blame] | 490 | |
Feng Tang | ee9b450 | 2010-09-13 15:39:48 +0800 | [diff] [blame] | 491 | /* Disable all IRQ and config it to 115200, 8n1 */ |
Feng Tang | 2251099 | 2010-06-16 14:46:09 +0100 | [diff] [blame] | 492 | config = WC_TAG | WC_FIFO_ENABLE |
| 493 | | WC_1_STOPBITS |
| 494 | | WC_8BIT_WORD |
| 495 | | WC_BAUD_DR2; |
Feng Tang | 2251099 | 2010-06-16 14:46:09 +0100 | [diff] [blame] | 496 | |
| 497 | /* as we use thread to handle tx/rx, need set low latency */ |
| 498 | port->state->port.tty->low_latency = 1; |
| 499 | |
Alan Cox | 91efa75 | 2010-09-13 15:39:56 +0800 | [diff] [blame] | 500 | if (max->irq) { |
Feng Tang | ee9b450 | 2010-09-13 15:39:48 +0800 | [diff] [blame] | 501 | max->read_thread = NULL; |
Alan Cox | 91efa75 | 2010-09-13 15:39:56 +0800 | [diff] [blame] | 502 | ret = request_irq(max->irq, serial_m3110_irq, |
| 503 | IRQ_TYPE_EDGE_FALLING, "max3110", max); |
| 504 | if (ret) { |
| 505 | max->irq = 0; |
| 506 | pr_err(PR_FMT "unable to allocate IRQ, polling\n"); |
| 507 | } else { |
| 508 | /* Enable RX IRQ only */ |
| 509 | config |= WC_RXA_IRQ_ENABLE; |
| 510 | } |
Feng Tang | ee9b450 | 2010-09-13 15:39:48 +0800 | [diff] [blame] | 511 | } |
Alan Cox | 91efa75 | 2010-09-13 15:39:56 +0800 | [diff] [blame] | 512 | |
| 513 | if (max->irq == 0) { |
| 514 | /* If IRQ is disabled, start a read thread for input data */ |
| 515 | max->read_thread = |
| 516 | kthread_run(max3110_read_thread, max, "max3110_read"); |
| 517 | if (IS_ERR(max->read_thread)) { |
| 518 | ret = PTR_ERR(max->read_thread); |
| 519 | max->read_thread = NULL; |
| 520 | pr_err(PR_FMT "Can't create read thread!\n"); |
| 521 | return ret; |
| 522 | } |
| 523 | } |
Feng Tang | 2251099 | 2010-06-16 14:46:09 +0100 | [diff] [blame] | 524 | |
Feng Tang | ee9b450 | 2010-09-13 15:39:48 +0800 | [diff] [blame] | 525 | ret = max3110_out(max, config); |
| 526 | if (ret) { |
Alan Cox | 91efa75 | 2010-09-13 15:39:56 +0800 | [diff] [blame] | 527 | if (max->irq) |
| 528 | free_irq(max->irq, max); |
| 529 | if (max->read_thread) |
| 530 | kthread_stop(max->read_thread); |
Feng Tang | ee9b450 | 2010-09-13 15:39:48 +0800 | [diff] [blame] | 531 | max->read_thread = NULL; |
Feng Tang | ee9b450 | 2010-09-13 15:39:48 +0800 | [diff] [blame] | 532 | return ret; |
| 533 | } |
| 534 | |
Feng Tang | 2251099 | 2010-06-16 14:46:09 +0100 | [diff] [blame] | 535 | max->cur_conf = config; |
| 536 | return 0; |
| 537 | } |
| 538 | |
| 539 | static void serial_m3110_shutdown(struct uart_port *port) |
| 540 | { |
| 541 | struct uart_max3110 *max = |
| 542 | container_of(port, struct uart_max3110, port); |
| 543 | u16 config; |
| 544 | |
| 545 | if (max->read_thread) { |
| 546 | kthread_stop(max->read_thread); |
| 547 | max->read_thread = NULL; |
| 548 | } |
| 549 | |
Alan Cox | 91efa75 | 2010-09-13 15:39:56 +0800 | [diff] [blame] | 550 | if (max->irq) |
| 551 | free_irq(max->irq, max); |
Feng Tang | 2251099 | 2010-06-16 14:46:09 +0100 | [diff] [blame] | 552 | |
| 553 | /* Disable interrupts from this port */ |
| 554 | config = WC_TAG | WC_SW_SHDI; |
| 555 | max3110_out(max, config); |
| 556 | } |
| 557 | |
| 558 | static void serial_m3110_release_port(struct uart_port *port) |
| 559 | { |
| 560 | } |
| 561 | |
| 562 | static int serial_m3110_request_port(struct uart_port *port) |
| 563 | { |
| 564 | return 0; |
| 565 | } |
| 566 | |
| 567 | static void serial_m3110_config_port(struct uart_port *port, int flags) |
| 568 | { |
Feng Tang | ee9b450 | 2010-09-13 15:39:48 +0800 | [diff] [blame] | 569 | port->type = PORT_MAX3100; |
Feng Tang | 2251099 | 2010-06-16 14:46:09 +0100 | [diff] [blame] | 570 | } |
| 571 | |
| 572 | static int |
| 573 | serial_m3110_verify_port(struct uart_port *port, struct serial_struct *ser) |
| 574 | { |
| 575 | /* we don't want the core code to modify any port params */ |
| 576 | return -EINVAL; |
| 577 | } |
| 578 | |
| 579 | |
| 580 | static const char *serial_m3110_type(struct uart_port *port) |
| 581 | { |
| 582 | struct uart_max3110 *max = |
| 583 | container_of(port, struct uart_max3110, port); |
| 584 | return max->name; |
| 585 | } |
| 586 | |
| 587 | static void |
| 588 | serial_m3110_set_termios(struct uart_port *port, struct ktermios *termios, |
| 589 | struct ktermios *old) |
| 590 | { |
| 591 | struct uart_max3110 *max = |
| 592 | container_of(port, struct uart_max3110, port); |
| 593 | unsigned char cval; |
| 594 | unsigned int baud, parity = 0; |
| 595 | int clk_div = -1; |
| 596 | u16 new_conf = max->cur_conf; |
| 597 | |
| 598 | switch (termios->c_cflag & CSIZE) { |
| 599 | case CS7: |
| 600 | cval = UART_LCR_WLEN7; |
| 601 | new_conf |= WC_7BIT_WORD; |
| 602 | break; |
| 603 | default: |
Feng Tang | ee9b450 | 2010-09-13 15:39:48 +0800 | [diff] [blame] | 604 | /* We only support CS7 & CS8 */ |
| 605 | termios->c_cflag &= ~CSIZE; |
| 606 | termios->c_cflag |= CS8; |
Feng Tang | 2251099 | 2010-06-16 14:46:09 +0100 | [diff] [blame] | 607 | case CS8: |
| 608 | cval = UART_LCR_WLEN8; |
| 609 | new_conf |= WC_8BIT_WORD; |
| 610 | break; |
| 611 | } |
| 612 | |
| 613 | baud = uart_get_baud_rate(port, termios, old, 0, 230400); |
| 614 | |
Feng Tang | ee9b450 | 2010-09-13 15:39:48 +0800 | [diff] [blame] | 615 | /* First calc the div for 1.8MHZ clock case */ |
Feng Tang | 2251099 | 2010-06-16 14:46:09 +0100 | [diff] [blame] | 616 | switch (baud) { |
| 617 | case 300: |
| 618 | clk_div = WC_BAUD_DR384; |
| 619 | break; |
| 620 | case 600: |
| 621 | clk_div = WC_BAUD_DR192; |
| 622 | break; |
| 623 | case 1200: |
| 624 | clk_div = WC_BAUD_DR96; |
| 625 | break; |
| 626 | case 2400: |
| 627 | clk_div = WC_BAUD_DR48; |
| 628 | break; |
| 629 | case 4800: |
| 630 | clk_div = WC_BAUD_DR24; |
| 631 | break; |
| 632 | case 9600: |
| 633 | clk_div = WC_BAUD_DR12; |
| 634 | break; |
| 635 | case 19200: |
| 636 | clk_div = WC_BAUD_DR6; |
| 637 | break; |
| 638 | case 38400: |
| 639 | clk_div = WC_BAUD_DR3; |
| 640 | break; |
| 641 | case 57600: |
| 642 | clk_div = WC_BAUD_DR2; |
| 643 | break; |
| 644 | case 115200: |
| 645 | clk_div = WC_BAUD_DR1; |
| 646 | break; |
| 647 | case 230400: |
| 648 | if (max->clock & MAX3110_HIGH_CLK) |
| 649 | break; |
| 650 | default: |
Feng Tang | ee9b450 | 2010-09-13 15:39:48 +0800 | [diff] [blame] | 651 | /* Pick the previous baud rate */ |
Feng Tang | 2251099 | 2010-06-16 14:46:09 +0100 | [diff] [blame] | 652 | baud = max->baud; |
| 653 | clk_div = max->cur_conf & WC_BAUD_DIV_MASK; |
| 654 | tty_termios_encode_baud_rate(termios, baud, baud); |
| 655 | } |
| 656 | |
| 657 | if (max->clock & MAX3110_HIGH_CLK) { |
| 658 | clk_div += 1; |
Feng Tang | ee9b450 | 2010-09-13 15:39:48 +0800 | [diff] [blame] | 659 | /* High clk version max3110 doesn't support B300 */ |
| 660 | if (baud == 300) { |
Feng Tang | 2251099 | 2010-06-16 14:46:09 +0100 | [diff] [blame] | 661 | baud = 600; |
Feng Tang | ee9b450 | 2010-09-13 15:39:48 +0800 | [diff] [blame] | 662 | clk_div = WC_BAUD_DR384; |
| 663 | } |
Feng Tang | 2251099 | 2010-06-16 14:46:09 +0100 | [diff] [blame] | 664 | if (baud == 230400) |
| 665 | clk_div = WC_BAUD_DR1; |
| 666 | tty_termios_encode_baud_rate(termios, baud, baud); |
| 667 | } |
| 668 | |
| 669 | new_conf = (new_conf & ~WC_BAUD_DIV_MASK) | clk_div; |
Feng Tang | ee9b450 | 2010-09-13 15:39:48 +0800 | [diff] [blame] | 670 | |
| 671 | if (unlikely(termios->c_cflag & CMSPAR)) |
| 672 | termios->c_cflag &= ~CMSPAR; |
| 673 | |
Feng Tang | 2251099 | 2010-06-16 14:46:09 +0100 | [diff] [blame] | 674 | if (termios->c_cflag & CSTOPB) |
| 675 | new_conf |= WC_2_STOPBITS; |
| 676 | else |
| 677 | new_conf &= ~WC_2_STOPBITS; |
| 678 | |
| 679 | if (termios->c_cflag & PARENB) { |
| 680 | new_conf |= WC_PARITY_ENABLE; |
| 681 | parity |= UART_LCR_PARITY; |
| 682 | } else |
| 683 | new_conf &= ~WC_PARITY_ENABLE; |
| 684 | |
| 685 | if (!(termios->c_cflag & PARODD)) |
| 686 | parity |= UART_LCR_EPAR; |
| 687 | max->parity = parity; |
| 688 | |
| 689 | uart_update_timeout(port, termios->c_cflag, baud); |
| 690 | |
| 691 | new_conf |= WC_TAG; |
| 692 | if (new_conf != max->cur_conf) { |
Feng Tang | ee9b450 | 2010-09-13 15:39:48 +0800 | [diff] [blame] | 693 | if (!max3110_out(max, new_conf)) { |
| 694 | max->cur_conf = new_conf; |
| 695 | max->baud = baud; |
| 696 | } |
Feng Tang | 2251099 | 2010-06-16 14:46:09 +0100 | [diff] [blame] | 697 | } |
| 698 | } |
| 699 | |
Feng Tang | ee9b450 | 2010-09-13 15:39:48 +0800 | [diff] [blame] | 700 | /* Don't handle hw handshaking */ |
Feng Tang | 2251099 | 2010-06-16 14:46:09 +0100 | [diff] [blame] | 701 | static unsigned int serial_m3110_get_mctrl(struct uart_port *port) |
| 702 | { |
| 703 | return TIOCM_DSR | TIOCM_CAR | TIOCM_DSR; |
| 704 | } |
| 705 | |
| 706 | static void serial_m3110_set_mctrl(struct uart_port *port, unsigned int mctrl) |
| 707 | { |
| 708 | } |
| 709 | |
| 710 | static void serial_m3110_break_ctl(struct uart_port *port, int break_state) |
| 711 | { |
| 712 | } |
| 713 | |
| 714 | static void serial_m3110_pm(struct uart_port *port, unsigned int state, |
| 715 | unsigned int oldstate) |
| 716 | { |
| 717 | } |
| 718 | |
| 719 | static void serial_m3110_enable_ms(struct uart_port *port) |
| 720 | { |
| 721 | } |
| 722 | |
| 723 | struct uart_ops serial_m3110_ops = { |
| 724 | .tx_empty = serial_m3110_tx_empty, |
| 725 | .set_mctrl = serial_m3110_set_mctrl, |
| 726 | .get_mctrl = serial_m3110_get_mctrl, |
| 727 | .stop_tx = serial_m3110_stop_tx, |
| 728 | .start_tx = serial_m3110_start_tx, |
| 729 | .stop_rx = serial_m3110_stop_rx, |
| 730 | .enable_ms = serial_m3110_enable_ms, |
| 731 | .break_ctl = serial_m3110_break_ctl, |
| 732 | .startup = serial_m3110_startup, |
| 733 | .shutdown = serial_m3110_shutdown, |
Feng Tang | ee9b450 | 2010-09-13 15:39:48 +0800 | [diff] [blame] | 734 | .set_termios = serial_m3110_set_termios, |
Feng Tang | 2251099 | 2010-06-16 14:46:09 +0100 | [diff] [blame] | 735 | .pm = serial_m3110_pm, |
| 736 | .type = serial_m3110_type, |
| 737 | .release_port = serial_m3110_release_port, |
| 738 | .request_port = serial_m3110_request_port, |
| 739 | .config_port = serial_m3110_config_port, |
| 740 | .verify_port = serial_m3110_verify_port, |
| 741 | }; |
| 742 | |
| 743 | static struct uart_driver serial_m3110_reg = { |
| 744 | .owner = THIS_MODULE, |
| 745 | .driver_name = "MRST serial", |
| 746 | .dev_name = "ttyS", |
| 747 | .major = TTY_MAJOR, |
| 748 | .minor = 64, |
| 749 | .nr = 1, |
Feng Tang | ee9b450 | 2010-09-13 15:39:48 +0800 | [diff] [blame] | 750 | .cons = &serial_m3110_console, |
Feng Tang | 2251099 | 2010-06-16 14:46:09 +0100 | [diff] [blame] | 751 | }; |
| 752 | |
Feng Tang | ee9b450 | 2010-09-13 15:39:48 +0800 | [diff] [blame] | 753 | #ifdef CONFIG_PM |
Feng Tang | 2251099 | 2010-06-16 14:46:09 +0100 | [diff] [blame] | 754 | static int serial_m3110_suspend(struct spi_device *spi, pm_message_t state) |
| 755 | { |
Feng Tang | ee9b450 | 2010-09-13 15:39:48 +0800 | [diff] [blame] | 756 | struct uart_max3110 *max = spi_get_drvdata(spi); |
| 757 | |
| 758 | disable_irq(max->irq); |
| 759 | uart_suspend_port(&serial_m3110_reg, &max->port); |
| 760 | max3110_out(max, max->cur_conf | WC_SW_SHDI); |
Feng Tang | 2251099 | 2010-06-16 14:46:09 +0100 | [diff] [blame] | 761 | return 0; |
| 762 | } |
| 763 | |
| 764 | static int serial_m3110_resume(struct spi_device *spi) |
| 765 | { |
Feng Tang | ee9b450 | 2010-09-13 15:39:48 +0800 | [diff] [blame] | 766 | struct uart_max3110 *max = spi_get_drvdata(spi); |
| 767 | |
| 768 | max3110_out(max, max->cur_conf); |
| 769 | uart_resume_port(&serial_m3110_reg, &max->port); |
| 770 | enable_irq(max->irq); |
Feng Tang | 2251099 | 2010-06-16 14:46:09 +0100 | [diff] [blame] | 771 | return 0; |
| 772 | } |
Feng Tang | ee9b450 | 2010-09-13 15:39:48 +0800 | [diff] [blame] | 773 | #else |
| 774 | #define serial_m3110_suspend NULL |
| 775 | #define serial_m3110_resume NULL |
| 776 | #endif |
Feng Tang | 2251099 | 2010-06-16 14:46:09 +0100 | [diff] [blame] | 777 | |
Bill Pemberton | 9671f09 | 2012-11-19 13:21:50 -0500 | [diff] [blame] | 778 | static int serial_m3110_probe(struct spi_device *spi) |
Feng Tang | 2251099 | 2010-06-16 14:46:09 +0100 | [diff] [blame] | 779 | { |
| 780 | struct uart_max3110 *max; |
Feng Tang | ee9b450 | 2010-09-13 15:39:48 +0800 | [diff] [blame] | 781 | void *buffer; |
jianwei.yang | 99dd3f6 | 2010-06-16 14:46:49 +0100 | [diff] [blame] | 782 | u16 res; |
Feng Tang | ee9b450 | 2010-09-13 15:39:48 +0800 | [diff] [blame] | 783 | int ret = 0; |
| 784 | |
Feng Tang | 2251099 | 2010-06-16 14:46:09 +0100 | [diff] [blame] | 785 | max = kzalloc(sizeof(*max), GFP_KERNEL); |
| 786 | if (!max) |
| 787 | return -ENOMEM; |
| 788 | |
Feng Tang | ee9b450 | 2010-09-13 15:39:48 +0800 | [diff] [blame] | 789 | /* Set spi info */ |
Feng Tang | 2251099 | 2010-06-16 14:46:09 +0100 | [diff] [blame] | 790 | spi->bits_per_word = 16; |
| 791 | max->clock = MAX3110_HIGH_CLK; |
Feng Tang | 2251099 | 2010-06-16 14:46:09 +0100 | [diff] [blame] | 792 | |
| 793 | spi_setup(spi); |
| 794 | |
Feng Tang | ee9b450 | 2010-09-13 15:39:48 +0800 | [diff] [blame] | 795 | max->port.type = PORT_MAX3100; |
| 796 | max->port.fifosize = 2; /* Only have 16b buffer */ |
Feng Tang | 2251099 | 2010-06-16 14:46:09 +0100 | [diff] [blame] | 797 | max->port.ops = &serial_m3110_ops; |
| 798 | max->port.line = 0; |
| 799 | max->port.dev = &spi->dev; |
| 800 | max->port.uartclk = 115200; |
| 801 | |
| 802 | max->spi = spi; |
Feng Tang | ee9b450 | 2010-09-13 15:39:48 +0800 | [diff] [blame] | 803 | strcpy(max->name, spi->modalias); |
Feng Tang | 2251099 | 2010-06-16 14:46:09 +0100 | [diff] [blame] | 804 | max->irq = (u16)spi->irq; |
| 805 | |
Arjan van de Ven | 68c16b4 | 2010-06-17 11:02:06 +0100 | [diff] [blame] | 806 | mutex_init(&max->thread_mutex); |
Feng Tang | 2251099 | 2010-06-16 14:46:09 +0100 | [diff] [blame] | 807 | |
| 808 | max->word_7bits = 0; |
| 809 | max->parity = 0; |
| 810 | max->baud = 0; |
| 811 | |
| 812 | max->cur_conf = 0; |
Arjan van de Ven | d6e679b | 2010-06-17 11:02:15 +0100 | [diff] [blame] | 813 | max->uart_flags = 0; |
| 814 | |
jianwei.yang | 99dd3f6 | 2010-06-16 14:46:49 +0100 | [diff] [blame] | 815 | /* Check if reading configuration register returns something sane */ |
Feng Tang | 2251099 | 2010-06-16 14:46:09 +0100 | [diff] [blame] | 816 | |
jianwei.yang | 99dd3f6 | 2010-06-16 14:46:49 +0100 | [diff] [blame] | 817 | res = RC_TAG; |
| 818 | ret = max3110_write_then_read(max, (u8 *)&res, (u8 *)&res, 2, 0); |
| 819 | if (ret < 0 || res == 0 || res == 0xffff) { |
William Douglas | 0bb04bf | 2011-06-23 13:38:36 +0100 | [diff] [blame] | 820 | dev_dbg(&spi->dev, "MAX3111 deemed not present (conf reg %04x)", |
jianwei.yang | 99dd3f6 | 2010-06-16 14:46:49 +0100 | [diff] [blame] | 821 | res); |
| 822 | ret = -ENODEV; |
| 823 | goto err_get_page; |
| 824 | } |
Feng Tang | ee9b450 | 2010-09-13 15:39:48 +0800 | [diff] [blame] | 825 | |
| 826 | buffer = (void *)__get_free_page(GFP_KERNEL); |
Feng Tang | 2251099 | 2010-06-16 14:46:09 +0100 | [diff] [blame] | 827 | if (!buffer) { |
| 828 | ret = -ENOMEM; |
| 829 | goto err_get_page; |
| 830 | } |
Feng Tang | ee9b450 | 2010-09-13 15:39:48 +0800 | [diff] [blame] | 831 | max->con_xmit.buf = buffer; |
| 832 | max->con_xmit.head = 0; |
| 833 | max->con_xmit.tail = 0; |
Feng Tang | 2251099 | 2010-06-16 14:46:09 +0100 | [diff] [blame] | 834 | |
Mika Westerberg | 33b1e69 | 2011-06-23 13:39:00 +0100 | [diff] [blame] | 835 | init_waitqueue_head(&max->wq); |
| 836 | |
Feng Tang | 2251099 | 2010-06-16 14:46:09 +0100 | [diff] [blame] | 837 | max->main_thread = kthread_run(max3110_main_thread, |
| 838 | max, "max3110_main"); |
| 839 | if (IS_ERR(max->main_thread)) { |
| 840 | ret = PTR_ERR(max->main_thread); |
| 841 | goto err_kthread; |
| 842 | } |
| 843 | |
Feng Tang | ee9b450 | 2010-09-13 15:39:48 +0800 | [diff] [blame] | 844 | spi_set_drvdata(spi, max); |
Feng Tang | 2251099 | 2010-06-16 14:46:09 +0100 | [diff] [blame] | 845 | pmax = max; |
Feng Tang | ee9b450 | 2010-09-13 15:39:48 +0800 | [diff] [blame] | 846 | |
| 847 | /* Give membase a psudo value to pass serial_core's check */ |
Feng Tang | 2251099 | 2010-06-16 14:46:09 +0100 | [diff] [blame] | 848 | max->port.membase = (void *)0xff110000; |
| 849 | uart_add_one_port(&serial_m3110_reg, &max->port); |
| 850 | |
| 851 | return 0; |
| 852 | |
| 853 | err_kthread: |
| 854 | free_page((unsigned long)buffer); |
| 855 | err_get_page: |
Feng Tang | 2251099 | 2010-06-16 14:46:09 +0100 | [diff] [blame] | 856 | kfree(max); |
| 857 | return ret; |
| 858 | } |
| 859 | |
Bill Pemberton | ae8d8a1 | 2012-11-19 13:26:18 -0500 | [diff] [blame] | 860 | static int serial_m3110_remove(struct spi_device *dev) |
Feng Tang | 2251099 | 2010-06-16 14:46:09 +0100 | [diff] [blame] | 861 | { |
Feng Tang | ee9b450 | 2010-09-13 15:39:48 +0800 | [diff] [blame] | 862 | struct uart_max3110 *max = spi_get_drvdata(dev); |
Feng Tang | 2251099 | 2010-06-16 14:46:09 +0100 | [diff] [blame] | 863 | |
Feng Tang | ee9b450 | 2010-09-13 15:39:48 +0800 | [diff] [blame] | 864 | if (!max) |
Feng Tang | 2251099 | 2010-06-16 14:46:09 +0100 | [diff] [blame] | 865 | return 0; |
| 866 | |
Feng Tang | 2251099 | 2010-06-16 14:46:09 +0100 | [diff] [blame] | 867 | uart_remove_one_port(&serial_m3110_reg, &max->port); |
| 868 | |
| 869 | free_page((unsigned long)max->con_xmit.buf); |
| 870 | |
| 871 | if (max->main_thread) |
| 872 | kthread_stop(max->main_thread); |
| 873 | |
| 874 | kfree(max); |
| 875 | return 0; |
| 876 | } |
| 877 | |
| 878 | static struct spi_driver uart_max3110_driver = { |
| 879 | .driver = { |
| 880 | .name = "spi_max3111", |
Feng Tang | 2251099 | 2010-06-16 14:46:09 +0100 | [diff] [blame] | 881 | .owner = THIS_MODULE, |
| 882 | }, |
| 883 | .probe = serial_m3110_probe, |
Bill Pemberton | 2d47b71 | 2012-11-19 13:21:34 -0500 | [diff] [blame] | 884 | .remove = serial_m3110_remove, |
Feng Tang | 2251099 | 2010-06-16 14:46:09 +0100 | [diff] [blame] | 885 | .suspend = serial_m3110_suspend, |
| 886 | .resume = serial_m3110_resume, |
| 887 | }; |
| 888 | |
Feng Tang | ee9b450 | 2010-09-13 15:39:48 +0800 | [diff] [blame] | 889 | static int __init serial_m3110_init(void) |
Feng Tang | 2251099 | 2010-06-16 14:46:09 +0100 | [diff] [blame] | 890 | { |
| 891 | int ret = 0; |
| 892 | |
| 893 | ret = uart_register_driver(&serial_m3110_reg); |
| 894 | if (ret) |
| 895 | return ret; |
| 896 | |
| 897 | ret = spi_register_driver(&uart_max3110_driver); |
| 898 | if (ret) |
| 899 | uart_unregister_driver(&serial_m3110_reg); |
| 900 | |
| 901 | return ret; |
| 902 | } |
| 903 | |
Feng Tang | ee9b450 | 2010-09-13 15:39:48 +0800 | [diff] [blame] | 904 | static void __exit serial_m3110_exit(void) |
Feng Tang | 2251099 | 2010-06-16 14:46:09 +0100 | [diff] [blame] | 905 | { |
| 906 | spi_unregister_driver(&uart_max3110_driver); |
| 907 | uart_unregister_driver(&serial_m3110_reg); |
| 908 | } |
| 909 | |
| 910 | module_init(serial_m3110_init); |
| 911 | module_exit(serial_m3110_exit); |
| 912 | |
Feng Tang | ee9b450 | 2010-09-13 15:39:48 +0800 | [diff] [blame] | 913 | MODULE_LICENSE("GPL v2"); |
Axel Lin | 8c4074c | 2011-08-01 21:20:10 +0800 | [diff] [blame] | 914 | MODULE_ALIAS("spi:max3110-uart"); |