blob: 1504a14ec1a6aa2a60a3ec00949607e2587ecd43 [file] [log] [blame]
Feng Tang22510992010-06-16 14:46:09 +01001/*
Feng Tangee9b4502010-09-13 15:39:48 +08002 * mrst_max3110.c - spi uart protocol driver for Maxim 3110
Feng Tang22510992010-06-16 14:46:09 +01003 *
Feng Tangee9b4502010-09-13 15:39:48 +08004 * Copyright (c) 2008-2010, Intel Corporation.
Feng Tang22510992010-06-16 14:46:09 +01005 *
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 Ivanovfb914eb2011-06-23 20:01:55 +030026 * 2. Currently only RX available interrupt is used, no need for waiting TXE
Feng Tang22510992010-06-16 14:46:09 +010027 * interrupt for a low speed UART device
28 */
29
Alexander Shishkin51808f02011-08-26 11:25:35 +010030#ifdef CONFIG_MAGIC_SYSRQ
31#define SUPPORT_SYSRQ
32#endif
33
Feng Tang22510992010-06-16 14:46:09 +010034#include <linux/module.h>
35#include <linux/ioport.h>
Andrew Mortonc0443912010-09-30 15:15:29 -070036#include <linux/irq.h>
Feng Tang22510992010-06-16 14:46:09 +010037#include <linux/init.h>
38#include <linux/console.h>
Feng Tang22510992010-06-16 14:46:09 +010039#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 Tang22510992010-06-16 14:46:09 +010045#include <linux/spi/spi.h>
Chen, Jie68357c72013-10-22 12:42:09 -070046#include <linux/pm.h>
Feng Tang22510992010-06-16 14:46:09 +010047
48#include "mrst_max3110.h"
49
50#define PR_FMT "mrst_max3110: "
51
Arjan van de Vend6e679b2010-06-17 11:02:15 +010052#define UART_TX_NEEDED 1
53#define CON_TX_NEEDED 2
54#define BIT_IRQ_PENDING 3
55
Feng Tang22510992010-06-16 14:46:09 +010056struct uart_max3110 {
57 struct uart_port port;
58 struct spi_device *spi;
Dan Carpenterd8653d32011-01-25 14:15:11 +000059 char name[SPI_NAME_SIZE];
Feng Tang22510992010-06-16 14:46:09 +010060
61 wait_queue_head_t wq;
62 struct task_struct *main_thread;
63 struct task_struct *read_thread;
Justin P. Mattock6eab04a2011-04-08 19:49:08 -070064 struct mutex thread_mutex;
Bin Gaob6951b82013-10-09 10:39:17 +080065 struct mutex io_mutex;
Feng Tang22510992010-06-16 14:46:09 +010066
67 u32 baud;
68 u16 cur_conf;
69 u8 clock;
70 u8 parity, word_7bits;
Feng Tangee9b4502010-09-13 15:39:48 +080071 u16 irq;
Feng Tang22510992010-06-16 14:46:09 +010072
Arjan van de Vend6e679b2010-06-17 11:02:15 +010073 unsigned long uart_flags;
Feng Tang22510992010-06-16 14:46:09 +010074
75 /* console related */
76 struct circ_buf con_xmit;
Feng Tang22510992010-06-16 14:46:09 +010077};
78
79/* global data structure, may need be removed */
Feng Tangee9b4502010-09-13 15:39:48 +080080static struct uart_max3110 *pmax;
81
Alexander Shishkin51808f02011-08-26 11:25:35 +010082static int receive_chars(struct uart_max3110 *max,
83 unsigned short *str, int len);
84static int max3110_read_multi(struct uart_max3110 *max);
Feng Tangee9b4502010-09-13 15:39:48 +080085static void max3110_con_receive(struct uart_max3110 *max);
Feng Tang22510992010-06-16 14:46:09 +010086
Feng Tangee9b4502010-09-13 15:39:48 +080087static int max3110_write_then_read(struct uart_max3110 *max,
88 const void *txbuf, void *rxbuf, unsigned len, int always_fast)
Feng Tang22510992010-06-16 14:46:09 +010089{
90 struct spi_device *spi = max->spi;
91 struct spi_message message;
92 struct spi_transfer x;
93 int ret;
94
Bin Gaob6951b82013-10-09 10:39:17 +080095 mutex_lock(&max->io_mutex);
Feng Tang22510992010-06-16 14:46:09 +010096 spi_message_init(&message);
97 memset(&x, 0, sizeof x);
98 x.len = len;
99 x.tx_buf = txbuf;
100 x.rx_buf = rxbuf;
101 spi_message_add_tail(&x, &message);
102
103 if (always_fast)
Feng Tangee9b4502010-09-13 15:39:48 +0800104 x.speed_hz = spi->max_speed_hz;
Feng Tang22510992010-06-16 14:46:09 +0100105 else if (max->baud)
106 x.speed_hz = max->baud;
107
108 /* Do the i/o */
109 ret = spi_sync(spi, &message);
Bin Gaob6951b82013-10-09 10:39:17 +0800110 mutex_unlock(&max->io_mutex);
Feng Tang22510992010-06-16 14:46:09 +0100111 return ret;
112}
113
Feng Tangee9b4502010-09-13 15:39:48 +0800114/* Write a 16b word to the device */
115static int max3110_out(struct uart_max3110 *max, const u16 out)
Feng Tang22510992010-06-16 14:46:09 +0100116{
Feng Tangee9b4502010-09-13 15:39:48 +0800117 void *buf;
118 u16 *obuf, *ibuf;
Feng Tang22510992010-06-16 14:46:09 +0100119 int ret;
120
Feng Tangee9b4502010-09-13 15:39:48 +0800121 buf = kzalloc(8, GFP_KERNEL | GFP_DMA);
122 if (!buf)
123 return -ENOMEM;
124
125 obuf = buf;
126 ibuf = buf + 4;
127 *obuf = out;
128 ret = max3110_write_then_read(max, obuf, ibuf, 2, 1);
129 if (ret) {
130 pr_warning(PR_FMT "%s(): get err msg %d when sending 0x%x\n",
131 __func__, ret, out);
132 goto exit;
133 }
Feng Tang22510992010-06-16 14:46:09 +0100134
Alexander Shishkin51808f02011-08-26 11:25:35 +0100135 receive_chars(max, ibuf, 1);
Feng Tang22510992010-06-16 14:46:09 +0100136
Feng Tangee9b4502010-09-13 15:39:48 +0800137exit:
138 kfree(buf);
Feng Tang22510992010-06-16 14:46:09 +0100139 return ret;
140}
141
Feng Tang22510992010-06-16 14:46:09 +0100142/*
143 * This is usually used to read data from SPIC RX FIFO, which doesn't
Feng Tangee9b4502010-09-13 15:39:48 +0800144 * need any delay like flushing character out.
145 *
146 * Return how many valide bytes are read back
Feng Tang22510992010-06-16 14:46:09 +0100147 */
Alexander Shishkin51808f02011-08-26 11:25:35 +0100148static int max3110_read_multi(struct uart_max3110 *max)
Feng Tang22510992010-06-16 14:46:09 +0100149{
Feng Tangee9b4502010-09-13 15:39:48 +0800150 void *buf;
151 u16 *obuf, *ibuf;
Alexander Shishkin51808f02011-08-26 11:25:35 +0100152 int ret, blen;
Feng Tang22510992010-06-16 14:46:09 +0100153
Feng Tangee9b4502010-09-13 15:39:48 +0800154 blen = M3110_RX_FIFO_DEPTH * sizeof(u16);
155 buf = kzalloc(blen * 2, GFP_KERNEL | GFP_DMA);
156 if (!buf) {
157 pr_warning(PR_FMT "%s(): fail to alloc dma buffer\n", __func__);
Feng Tang22510992010-06-16 14:46:09 +0100158 return 0;
159 }
160
Feng Tangee9b4502010-09-13 15:39:48 +0800161 /* tx/rx always have the same length */
162 obuf = buf;
163 ibuf = buf + blen;
Feng Tang22510992010-06-16 14:46:09 +0100164
Feng Tangee9b4502010-09-13 15:39:48 +0800165 if (max3110_write_then_read(max, obuf, ibuf, blen, 1)) {
166 kfree(buf);
Feng Tang22510992010-06-16 14:46:09 +0100167 return 0;
Feng Tangee9b4502010-09-13 15:39:48 +0800168 }
Feng Tang22510992010-06-16 14:46:09 +0100169
Alexander Shishkin51808f02011-08-26 11:25:35 +0100170 ret = receive_chars(max, ibuf, M3110_RX_FIFO_DEPTH);
Feng Tang22510992010-06-16 14:46:09 +0100171
Feng Tangee9b4502010-09-13 15:39:48 +0800172 kfree(buf);
Alexander Shishkin51808f02011-08-26 11:25:35 +0100173 return ret;
Feng Tang22510992010-06-16 14:46:09 +0100174}
175
176static void serial_m3110_con_putchar(struct uart_port *port, int ch)
177{
178 struct uart_max3110 *max =
179 container_of(port, struct uart_max3110, port);
180 struct circ_buf *xmit = &max->con_xmit;
181
182 if (uart_circ_chars_free(xmit)) {
183 xmit->buf[xmit->head] = (char)ch;
184 xmit->head = (xmit->head + 1) & (PAGE_SIZE - 1);
185 }
Feng Tang22510992010-06-16 14:46:09 +0100186}
187
188/*
189 * Print a string to the serial port trying not to disturb
190 * any possible real use of the port...
191 *
192 * The console_lock must be held when we get here.
193 */
194static void serial_m3110_con_write(struct console *co,
195 const char *s, unsigned int count)
196{
197 if (!pmax)
198 return;
199
200 uart_console_write(&pmax->port, s, count, serial_m3110_con_putchar);
Feng Tangee9b4502010-09-13 15:39:48 +0800201
202 if (!test_and_set_bit(CON_TX_NEEDED, &pmax->uart_flags))
Dirk Brandewie7b18bd52011-08-26 11:24:49 +0100203 wake_up(&pmax->wq);
Feng Tang22510992010-06-16 14:46:09 +0100204}
205
206static int __init
207serial_m3110_con_setup(struct console *co, char *options)
208{
209 struct uart_max3110 *max = pmax;
210 int baud = 115200;
211 int bits = 8;
212 int parity = 'n';
213 int flow = 'n';
214
215 pr_info(PR_FMT "setting up console\n");
216
Feng Tangee9b4502010-09-13 15:39:48 +0800217 if (co->index == -1)
218 co->index = 0;
219
Feng Tang22510992010-06-16 14:46:09 +0100220 if (!max) {
221 pr_err(PR_FMT "pmax is NULL, return");
222 return -ENODEV;
223 }
224
225 if (options)
226 uart_parse_options(options, &baud, &parity, &bits, &flow);
227
228 return uart_set_options(&max->port, co, baud, parity, bits, flow);
229}
230
231static struct tty_driver *serial_m3110_con_device(struct console *co,
232 int *index)
233{
234 struct uart_driver *p = co->data;
235 *index = co->index;
236 return p->tty_driver;
237}
238
239static struct uart_driver serial_m3110_reg;
240static struct console serial_m3110_console = {
241 .name = "ttyS",
242 .write = serial_m3110_con_write,
243 .device = serial_m3110_con_device,
244 .setup = serial_m3110_con_setup,
245 .flags = CON_PRINTBUFFER,
246 .index = -1,
247 .data = &serial_m3110_reg,
248};
249
Feng Tang22510992010-06-16 14:46:09 +0100250static unsigned int serial_m3110_tx_empty(struct uart_port *port)
251{
252 return 1;
253}
254
255static void serial_m3110_stop_tx(struct uart_port *port)
256{
257 return;
258}
259
260/* stop_rx will be called in spin_lock env */
261static void serial_m3110_stop_rx(struct uart_port *port)
262{
263 return;
264}
265
266#define WORDS_PER_XFER 128
Feng Tangee9b4502010-09-13 15:39:48 +0800267static void send_circ_buf(struct uart_max3110 *max,
Feng Tang22510992010-06-16 14:46:09 +0100268 struct circ_buf *xmit)
269{
Feng Tangee9b4502010-09-13 15:39:48 +0800270 void *buf;
271 u16 *obuf, *ibuf;
Alexander Shishkin51808f02011-08-26 11:25:35 +0100272 int i, len, blen, dma_size, left, ret = 0;
Feng Tangee9b4502010-09-13 15:39:48 +0800273
274
275 dma_size = WORDS_PER_XFER * sizeof(u16) * 2;
276 buf = kzalloc(dma_size, GFP_KERNEL | GFP_DMA);
277 if (!buf)
278 return;
279 obuf = buf;
280 ibuf = buf + dma_size/2;
Feng Tang22510992010-06-16 14:46:09 +0100281
282 while (!uart_circ_empty(xmit)) {
283 left = uart_circ_chars_pending(xmit);
284 while (left) {
Feng Tangee9b4502010-09-13 15:39:48 +0800285 len = min(left, WORDS_PER_XFER);
286 blen = len * sizeof(u16);
287 memset(ibuf, 0, blen);
Feng Tang22510992010-06-16 14:46:09 +0100288
Feng Tang22510992010-06-16 14:46:09 +0100289 for (i = 0; i < len; i++) {
290 obuf[i] = (u8)xmit->buf[xmit->tail] | WD_TAG;
291 xmit->tail = (xmit->tail + 1) &
292 (UART_XMIT_SIZE - 1);
293 }
Feng Tangee9b4502010-09-13 15:39:48 +0800294
295 /* Fail to send msg to console is not very critical */
Dirk Brandewie7b18bd52011-08-26 11:24:49 +0100296
Feng Tangee9b4502010-09-13 15:39:48 +0800297 ret = max3110_write_then_read(max, obuf, ibuf, blen, 0);
298 if (ret)
299 pr_warning(PR_FMT "%s(): get err msg %d\n",
300 __func__, ret);
Feng Tang22510992010-06-16 14:46:09 +0100301
Alexander Shishkin51808f02011-08-26 11:25:35 +0100302 receive_chars(max, ibuf, len);
Feng Tang22510992010-06-16 14:46:09 +0100303
304 max->port.icount.tx += len;
305 left -= len;
306 }
307 }
Feng Tangee9b4502010-09-13 15:39:48 +0800308
309 kfree(buf);
Feng Tang22510992010-06-16 14:46:09 +0100310}
311
312static void transmit_char(struct uart_max3110 *max)
313{
314 struct uart_port *port = &max->port;
315 struct circ_buf *xmit = &port->state->xmit;
316
317 if (uart_circ_empty(xmit) || uart_tx_stopped(port))
318 return;
319
320 send_circ_buf(max, xmit);
321
322 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
323 uart_write_wakeup(port);
324
325 if (uart_circ_empty(xmit))
326 serial_m3110_stop_tx(port);
327}
328
Feng Tangee9b4502010-09-13 15:39:48 +0800329/*
330 * This will be called by uart_write() and tty_write, can't
331 * go to sleep
332 */
Feng Tang22510992010-06-16 14:46:09 +0100333static void serial_m3110_start_tx(struct uart_port *port)
334{
335 struct uart_max3110 *max =
336 container_of(port, struct uart_max3110, port);
337
Arjan van de Vend6e679b2010-06-17 11:02:15 +0100338 if (!test_and_set_bit(UART_TX_NEEDED, &max->uart_flags))
Dirk Brandewie7b18bd52011-08-26 11:24:49 +0100339 wake_up(&max->wq);
Feng Tang22510992010-06-16 14:46:09 +0100340}
341
Alexander Shishkin51808f02011-08-26 11:25:35 +0100342static int
343receive_chars(struct uart_max3110 *max, unsigned short *str, int len)
Feng Tang22510992010-06-16 14:46:09 +0100344{
345 struct uart_port *port = &max->port;
Jiri Slaby227434f2013-01-03 15:53:01 +0100346 struct tty_port *tport;
Alexander Shishkin51808f02011-08-26 11:25:35 +0100347 char buf[M3110_RX_FIFO_DEPTH];
348 int r, w, usable;
Feng Tang22510992010-06-16 14:46:09 +0100349
350 /* If uart is not opened, just return */
351 if (!port->state)
Alexander Shishkin51808f02011-08-26 11:25:35 +0100352 return 0;
Feng Tang22510992010-06-16 14:46:09 +0100353
Jiri Slaby227434f2013-01-03 15:53:01 +0100354 tport = &port->state->port;
Feng Tang22510992010-06-16 14:46:09 +0100355
Alexander Shishkin51808f02011-08-26 11:25:35 +0100356 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
Jiri Slaby2e124b42013-01-03 15:53:06 +0100369 if (!w)
Alexander Shishkin51808f02011-08-26 11:25:35 +0100370 return 0;
371
372 for (r = 0; w; r += usable, w -= usable) {
Jiri Slaby227434f2013-01-03 15:53:01 +0100373 usable = tty_buffer_request_room(tport, w);
Feng Tang22510992010-06-16 14:46:09 +0100374 if (usable) {
Jiri Slaby05c7cd32013-01-03 15:53:04 +0100375 tty_insert_flip_string(tport, buf + r, usable);
Feng Tang22510992010-06-16 14:46:09 +0100376 port->icount.rx += usable;
Feng Tang22510992010-06-16 14:46:09 +0100377 }
Feng Tang22510992010-06-16 14:46:09 +0100378 }
Jiri Slaby2e124b42013-01-03 15:53:06 +0100379 tty_flip_buffer_push(tport);
Alexander Shishkin51808f02011-08-26 11:25:35 +0100380
381 return r;
Feng Tang22510992010-06-16 14:46:09 +0100382}
383
Feng Tangee9b4502010-09-13 15:39:48 +0800384/*
385 * This routine will be used in read_thread or RX IRQ handling,
386 * it will first do one round buffer read(8 words), if there is some
387 * valid RX data, will try to read 5 more rounds till all data
388 * is read out.
389 *
390 * Use stack space as data buffer to save some system load, and chose
391 * 504 Btyes as a threadhold to do a bulk push to upper tty layer when
392 * receiving bulk data, a much bigger buffer may cause stack overflow
393 */
394static void max3110_con_receive(struct uart_max3110 *max)
Feng Tang22510992010-06-16 14:46:09 +0100395{
Alexander Shishkin51808f02011-08-26 11:25:35 +0100396 int loop = 1, num;
Feng Tang22510992010-06-16 14:46:09 +0100397
Feng Tang22510992010-06-16 14:46:09 +0100398 do {
Alexander Shishkin51808f02011-08-26 11:25:35 +0100399 num = max3110_read_multi(max);
Feng Tang22510992010-06-16 14:46:09 +0100400
401 if (num) {
Feng Tangee9b4502010-09-13 15:39:48 +0800402 loop = 5;
Feng Tang22510992010-06-16 14:46:09 +0100403 }
404 } while (--loop);
Feng Tang22510992010-06-16 14:46:09 +0100405}
406
407static int max3110_main_thread(void *_max)
408{
409 struct uart_max3110 *max = _max;
410 wait_queue_head_t *wq = &max->wq;
411 int ret = 0;
412 struct circ_buf *xmit = &max->con_xmit;
413
Feng Tang22510992010-06-16 14:46:09 +0100414 pr_info(PR_FMT "start main thread\n");
415
416 do {
Dirk Brandewie7b18bd52011-08-26 11:24:49 +0100417 wait_event_interruptible(*wq,
418 max->uart_flags || kthread_should_stop());
Arjan van de Ven68c16b42010-06-17 11:02:06 +0100419
420 mutex_lock(&max->thread_mutex);
Feng Tang22510992010-06-16 14:46:09 +0100421
Arjan van de Vend6e679b2010-06-17 11:02:15 +0100422 if (test_and_clear_bit(BIT_IRQ_PENDING, &max->uart_flags))
Feng Tangee9b4502010-09-13 15:39:48 +0800423 max3110_con_receive(max);
Feng Tang22510992010-06-16 14:46:09 +0100424
425 /* first handle console output */
Arjan van de Vend6e679b2010-06-17 11:02:15 +0100426 if (test_and_clear_bit(CON_TX_NEEDED, &max->uart_flags))
Feng Tang22510992010-06-16 14:46:09 +0100427 send_circ_buf(max, xmit);
Feng Tang22510992010-06-16 14:46:09 +0100428
429 /* handle uart output */
Arjan van de Vend6e679b2010-06-17 11:02:15 +0100430 if (test_and_clear_bit(UART_TX_NEEDED, &max->uart_flags))
Feng Tang22510992010-06-16 14:46:09 +0100431 transmit_char(max);
Arjan van de Vend6e679b2010-06-17 11:02:15 +0100432
Arjan van de Ven68c16b42010-06-17 11:02:06 +0100433 mutex_unlock(&max->thread_mutex);
Arjan van de Vend6e679b2010-06-17 11:02:15 +0100434
Feng Tang22510992010-06-16 14:46:09 +0100435 } while (!kthread_should_stop());
436
437 return ret;
438}
439
Feng Tang22510992010-06-16 14:46:09 +0100440static irqreturn_t serial_m3110_irq(int irq, void *dev_id)
441{
442 struct uart_max3110 *max = dev_id;
443
444 /* max3110's irq is a falling edge, not level triggered,
445 * so no need to disable the irq */
Dirk Brandewie7b18bd52011-08-26 11:24:49 +0100446
Arjan van de Vend6e679b2010-06-17 11:02:15 +0100447 if (!test_and_set_bit(BIT_IRQ_PENDING, &max->uart_flags))
Dirk Brandewie7b18bd52011-08-26 11:24:49 +0100448 wake_up(&max->wq);
Arjan van de Vend6e679b2010-06-17 11:02:15 +0100449
Feng Tang22510992010-06-16 14:46:09 +0100450 return IRQ_HANDLED;
451}
Alan Cox91efa752010-09-13 15:39:56 +0800452
Feng Tang22510992010-06-16 14:46:09 +0100453/* if don't use RX IRQ, then need a thread to polling read */
454static int max3110_read_thread(void *_max)
455{
456 struct uart_max3110 *max = _max;
457
458 pr_info(PR_FMT "start read thread\n");
459 do {
Feng Tangee9b4502010-09-13 15:39:48 +0800460 /*
461 * If can't acquire the mutex, it means the main thread
462 * is running which will also perform the rx job
463 */
464 if (mutex_trylock(&max->thread_mutex)) {
465 max3110_con_receive(max);
466 mutex_unlock(&max->thread_mutex);
467 }
Feng Tang22510992010-06-16 14:46:09 +0100468
469 set_current_state(TASK_INTERRUPTIBLE);
470 schedule_timeout(HZ / 20);
471 } while (!kthread_should_stop());
472
473 return 0;
474}
Feng Tang22510992010-06-16 14:46:09 +0100475
476static int serial_m3110_startup(struct uart_port *port)
477{
478 struct uart_max3110 *max =
479 container_of(port, struct uart_max3110, port);
480 u16 config = 0;
481 int ret = 0;
482
Feng Tangee9b4502010-09-13 15:39:48 +0800483 if (port->line != 0) {
Feng Tang22510992010-06-16 14:46:09 +0100484 pr_err(PR_FMT "uart port startup failed\n");
Feng Tangee9b4502010-09-13 15:39:48 +0800485 return -1;
486 }
Feng Tang22510992010-06-16 14:46:09 +0100487
Feng Tangee9b4502010-09-13 15:39:48 +0800488 /* Disable all IRQ and config it to 115200, 8n1 */
Feng Tang22510992010-06-16 14:46:09 +0100489 config = WC_TAG | WC_FIFO_ENABLE
490 | WC_1_STOPBITS
491 | WC_8BIT_WORD
492 | WC_BAUD_DR2;
Feng Tang22510992010-06-16 14:46:09 +0100493
494 /* as we use thread to handle tx/rx, need set low latency */
Jiri Slabyd6c53c02013-01-03 15:53:05 +0100495 port->state->port.low_latency = 1;
Feng Tang22510992010-06-16 14:46:09 +0100496
Alan Cox91efa752010-09-13 15:39:56 +0800497 if (max->irq) {
Chen, Jie68357c72013-10-22 12:42:09 -0700498 /* Enable RX IRQ only */
499 config |= WC_RXA_IRQ_ENABLE;
500 } else {
Alan Cox91efa752010-09-13 15:39:56 +0800501 /* If IRQ is disabled, start a read thread for input data */
502 max->read_thread =
503 kthread_run(max3110_read_thread, max, "max3110_read");
504 if (IS_ERR(max->read_thread)) {
505 ret = PTR_ERR(max->read_thread);
506 max->read_thread = NULL;
507 pr_err(PR_FMT "Can't create read thread!\n");
508 return ret;
509 }
510 }
Feng Tang22510992010-06-16 14:46:09 +0100511
Feng Tangee9b4502010-09-13 15:39:48 +0800512 ret = max3110_out(max, config);
513 if (ret) {
Alan Cox91efa752010-09-13 15:39:56 +0800514 if (max->read_thread)
515 kthread_stop(max->read_thread);
Feng Tangee9b4502010-09-13 15:39:48 +0800516 max->read_thread = NULL;
Feng Tangee9b4502010-09-13 15:39:48 +0800517 return ret;
518 }
519
Feng Tang22510992010-06-16 14:46:09 +0100520 max->cur_conf = config;
521 return 0;
522}
523
524static void serial_m3110_shutdown(struct uart_port *port)
525{
526 struct uart_max3110 *max =
527 container_of(port, struct uart_max3110, port);
528 u16 config;
529
530 if (max->read_thread) {
531 kthread_stop(max->read_thread);
532 max->read_thread = NULL;
533 }
534
Feng Tang22510992010-06-16 14:46:09 +0100535 /* Disable interrupts from this port */
536 config = WC_TAG | WC_SW_SHDI;
537 max3110_out(max, config);
538}
539
540static void serial_m3110_release_port(struct uart_port *port)
541{
542}
543
544static int serial_m3110_request_port(struct uart_port *port)
545{
546 return 0;
547}
548
549static void serial_m3110_config_port(struct uart_port *port, int flags)
550{
Feng Tangee9b4502010-09-13 15:39:48 +0800551 port->type = PORT_MAX3100;
Feng Tang22510992010-06-16 14:46:09 +0100552}
553
554static int
555serial_m3110_verify_port(struct uart_port *port, struct serial_struct *ser)
556{
557 /* we don't want the core code to modify any port params */
558 return -EINVAL;
559}
560
561
562static const char *serial_m3110_type(struct uart_port *port)
563{
564 struct uart_max3110 *max =
565 container_of(port, struct uart_max3110, port);
566 return max->name;
567}
568
569static void
570serial_m3110_set_termios(struct uart_port *port, struct ktermios *termios,
571 struct ktermios *old)
572{
573 struct uart_max3110 *max =
574 container_of(port, struct uart_max3110, port);
575 unsigned char cval;
576 unsigned int baud, parity = 0;
577 int clk_div = -1;
578 u16 new_conf = max->cur_conf;
579
580 switch (termios->c_cflag & CSIZE) {
581 case CS7:
582 cval = UART_LCR_WLEN7;
583 new_conf |= WC_7BIT_WORD;
584 break;
585 default:
Feng Tangee9b4502010-09-13 15:39:48 +0800586 /* We only support CS7 & CS8 */
587 termios->c_cflag &= ~CSIZE;
588 termios->c_cflag |= CS8;
Feng Tang22510992010-06-16 14:46:09 +0100589 case CS8:
590 cval = UART_LCR_WLEN8;
591 new_conf |= WC_8BIT_WORD;
592 break;
593 }
594
595 baud = uart_get_baud_rate(port, termios, old, 0, 230400);
596
Feng Tangee9b4502010-09-13 15:39:48 +0800597 /* First calc the div for 1.8MHZ clock case */
Feng Tang22510992010-06-16 14:46:09 +0100598 switch (baud) {
599 case 300:
600 clk_div = WC_BAUD_DR384;
601 break;
602 case 600:
603 clk_div = WC_BAUD_DR192;
604 break;
605 case 1200:
606 clk_div = WC_BAUD_DR96;
607 break;
608 case 2400:
609 clk_div = WC_BAUD_DR48;
610 break;
611 case 4800:
612 clk_div = WC_BAUD_DR24;
613 break;
614 case 9600:
615 clk_div = WC_BAUD_DR12;
616 break;
617 case 19200:
618 clk_div = WC_BAUD_DR6;
619 break;
620 case 38400:
621 clk_div = WC_BAUD_DR3;
622 break;
623 case 57600:
624 clk_div = WC_BAUD_DR2;
625 break;
626 case 115200:
627 clk_div = WC_BAUD_DR1;
628 break;
629 case 230400:
630 if (max->clock & MAX3110_HIGH_CLK)
631 break;
632 default:
Feng Tangee9b4502010-09-13 15:39:48 +0800633 /* Pick the previous baud rate */
Feng Tang22510992010-06-16 14:46:09 +0100634 baud = max->baud;
635 clk_div = max->cur_conf & WC_BAUD_DIV_MASK;
636 tty_termios_encode_baud_rate(termios, baud, baud);
637 }
638
639 if (max->clock & MAX3110_HIGH_CLK) {
640 clk_div += 1;
Feng Tangee9b4502010-09-13 15:39:48 +0800641 /* High clk version max3110 doesn't support B300 */
642 if (baud == 300) {
Feng Tang22510992010-06-16 14:46:09 +0100643 baud = 600;
Feng Tangee9b4502010-09-13 15:39:48 +0800644 clk_div = WC_BAUD_DR384;
645 }
Feng Tang22510992010-06-16 14:46:09 +0100646 if (baud == 230400)
647 clk_div = WC_BAUD_DR1;
648 tty_termios_encode_baud_rate(termios, baud, baud);
649 }
650
651 new_conf = (new_conf & ~WC_BAUD_DIV_MASK) | clk_div;
Feng Tangee9b4502010-09-13 15:39:48 +0800652
653 if (unlikely(termios->c_cflag & CMSPAR))
654 termios->c_cflag &= ~CMSPAR;
655
Feng Tang22510992010-06-16 14:46:09 +0100656 if (termios->c_cflag & CSTOPB)
657 new_conf |= WC_2_STOPBITS;
658 else
659 new_conf &= ~WC_2_STOPBITS;
660
661 if (termios->c_cflag & PARENB) {
662 new_conf |= WC_PARITY_ENABLE;
663 parity |= UART_LCR_PARITY;
664 } else
665 new_conf &= ~WC_PARITY_ENABLE;
666
667 if (!(termios->c_cflag & PARODD))
668 parity |= UART_LCR_EPAR;
669 max->parity = parity;
670
671 uart_update_timeout(port, termios->c_cflag, baud);
672
673 new_conf |= WC_TAG;
674 if (new_conf != max->cur_conf) {
Feng Tangee9b4502010-09-13 15:39:48 +0800675 if (!max3110_out(max, new_conf)) {
676 max->cur_conf = new_conf;
677 max->baud = baud;
678 }
Feng Tang22510992010-06-16 14:46:09 +0100679 }
680}
681
Feng Tangee9b4502010-09-13 15:39:48 +0800682/* Don't handle hw handshaking */
Feng Tang22510992010-06-16 14:46:09 +0100683static unsigned int serial_m3110_get_mctrl(struct uart_port *port)
684{
685 return TIOCM_DSR | TIOCM_CAR | TIOCM_DSR;
686}
687
688static void serial_m3110_set_mctrl(struct uart_port *port, unsigned int mctrl)
689{
690}
691
692static void serial_m3110_break_ctl(struct uart_port *port, int break_state)
693{
694}
695
696static void serial_m3110_pm(struct uart_port *port, unsigned int state,
697 unsigned int oldstate)
698{
699}
700
Jingoo Han66027692013-08-08 17:32:52 +0900701static struct uart_ops serial_m3110_ops = {
Feng Tang22510992010-06-16 14:46:09 +0100702 .tx_empty = serial_m3110_tx_empty,
703 .set_mctrl = serial_m3110_set_mctrl,
704 .get_mctrl = serial_m3110_get_mctrl,
705 .stop_tx = serial_m3110_stop_tx,
706 .start_tx = serial_m3110_start_tx,
707 .stop_rx = serial_m3110_stop_rx,
Feng Tang22510992010-06-16 14:46:09 +0100708 .break_ctl = serial_m3110_break_ctl,
709 .startup = serial_m3110_startup,
710 .shutdown = serial_m3110_shutdown,
Feng Tangee9b4502010-09-13 15:39:48 +0800711 .set_termios = serial_m3110_set_termios,
Feng Tang22510992010-06-16 14:46:09 +0100712 .pm = serial_m3110_pm,
713 .type = serial_m3110_type,
714 .release_port = serial_m3110_release_port,
715 .request_port = serial_m3110_request_port,
716 .config_port = serial_m3110_config_port,
717 .verify_port = serial_m3110_verify_port,
718};
719
720static struct uart_driver serial_m3110_reg = {
721 .owner = THIS_MODULE,
722 .driver_name = "MRST serial",
723 .dev_name = "ttyS",
724 .major = TTY_MAJOR,
725 .minor = 64,
726 .nr = 1,
Feng Tangee9b4502010-09-13 15:39:48 +0800727 .cons = &serial_m3110_console,
Feng Tang22510992010-06-16 14:46:09 +0100728};
729
Lars-Peter Clausenc2ee91b2013-03-11 18:44:51 +0100730#ifdef CONFIG_PM_SLEEP
731static int serial_m3110_suspend(struct device *dev)
Feng Tang22510992010-06-16 14:46:09 +0100732{
Lars-Peter Clausenc2ee91b2013-03-11 18:44:51 +0100733 struct spi_device *spi = to_spi_device(dev);
Feng Tangee9b4502010-09-13 15:39:48 +0800734 struct uart_max3110 *max = spi_get_drvdata(spi);
735
Feng Tang09238442013-10-09 10:39:16 +0800736 if (max->irq > 0)
737 disable_irq(max->irq);
Feng Tangee9b4502010-09-13 15:39:48 +0800738 uart_suspend_port(&serial_m3110_reg, &max->port);
739 max3110_out(max, max->cur_conf | WC_SW_SHDI);
Feng Tang22510992010-06-16 14:46:09 +0100740 return 0;
741}
742
Lars-Peter Clausenc2ee91b2013-03-11 18:44:51 +0100743static int serial_m3110_resume(struct device *dev)
Feng Tang22510992010-06-16 14:46:09 +0100744{
Lars-Peter Clausenc2ee91b2013-03-11 18:44:51 +0100745 struct spi_device *spi = to_spi_device(dev);
Feng Tangee9b4502010-09-13 15:39:48 +0800746 struct uart_max3110 *max = spi_get_drvdata(spi);
747
748 max3110_out(max, max->cur_conf);
749 uart_resume_port(&serial_m3110_reg, &max->port);
Feng Tang09238442013-10-09 10:39:16 +0800750 if (max->irq > 0)
751 enable_irq(max->irq);
Feng Tang22510992010-06-16 14:46:09 +0100752 return 0;
753}
Lars-Peter Clausenc2ee91b2013-03-11 18:44:51 +0100754
755static SIMPLE_DEV_PM_OPS(serial_m3110_pm_ops, serial_m3110_suspend,
756 serial_m3110_resume);
757#define SERIAL_M3110_PM_OPS (&serial_m3110_pm_ops)
758
Feng Tangee9b4502010-09-13 15:39:48 +0800759#else
Lars-Peter Clausenc2ee91b2013-03-11 18:44:51 +0100760#define SERIAL_M3110_PM_OPS NULL
Feng Tangee9b4502010-09-13 15:39:48 +0800761#endif
Feng Tang22510992010-06-16 14:46:09 +0100762
Bill Pemberton9671f092012-11-19 13:21:50 -0500763static int serial_m3110_probe(struct spi_device *spi)
Feng Tang22510992010-06-16 14:46:09 +0100764{
765 struct uart_max3110 *max;
Feng Tangee9b4502010-09-13 15:39:48 +0800766 void *buffer;
jianwei.yang99dd3f62010-06-16 14:46:49 +0100767 u16 res;
Feng Tangee9b4502010-09-13 15:39:48 +0800768 int ret = 0;
769
Feng Tang22510992010-06-16 14:46:09 +0100770 max = kzalloc(sizeof(*max), GFP_KERNEL);
771 if (!max)
772 return -ENOMEM;
773
Feng Tangee9b4502010-09-13 15:39:48 +0800774 /* Set spi info */
Feng Tang22510992010-06-16 14:46:09 +0100775 spi->bits_per_word = 16;
776 max->clock = MAX3110_HIGH_CLK;
Feng Tang22510992010-06-16 14:46:09 +0100777
778 spi_setup(spi);
779
Feng Tangee9b4502010-09-13 15:39:48 +0800780 max->port.type = PORT_MAX3100;
781 max->port.fifosize = 2; /* Only have 16b buffer */
Feng Tang22510992010-06-16 14:46:09 +0100782 max->port.ops = &serial_m3110_ops;
783 max->port.line = 0;
784 max->port.dev = &spi->dev;
785 max->port.uartclk = 115200;
786
787 max->spi = spi;
Feng Tangee9b4502010-09-13 15:39:48 +0800788 strcpy(max->name, spi->modalias);
Feng Tang22510992010-06-16 14:46:09 +0100789 max->irq = (u16)spi->irq;
790
Arjan van de Ven68c16b42010-06-17 11:02:06 +0100791 mutex_init(&max->thread_mutex);
Bin Gaob6951b82013-10-09 10:39:17 +0800792 mutex_init(&max->io_mutex);
Feng Tang22510992010-06-16 14:46:09 +0100793
794 max->word_7bits = 0;
795 max->parity = 0;
796 max->baud = 0;
797
798 max->cur_conf = 0;
Arjan van de Vend6e679b2010-06-17 11:02:15 +0100799 max->uart_flags = 0;
800
jianwei.yang99dd3f62010-06-16 14:46:49 +0100801 /* Check if reading configuration register returns something sane */
Feng Tang22510992010-06-16 14:46:09 +0100802
jianwei.yang99dd3f62010-06-16 14:46:49 +0100803 res = RC_TAG;
804 ret = max3110_write_then_read(max, (u8 *)&res, (u8 *)&res, 2, 0);
805 if (ret < 0 || res == 0 || res == 0xffff) {
William Douglas0bb04bf2011-06-23 13:38:36 +0100806 dev_dbg(&spi->dev, "MAX3111 deemed not present (conf reg %04x)",
jianwei.yang99dd3f62010-06-16 14:46:49 +0100807 res);
808 ret = -ENODEV;
809 goto err_get_page;
810 }
Feng Tangee9b4502010-09-13 15:39:48 +0800811
812 buffer = (void *)__get_free_page(GFP_KERNEL);
Feng Tang22510992010-06-16 14:46:09 +0100813 if (!buffer) {
814 ret = -ENOMEM;
815 goto err_get_page;
816 }
Feng Tangee9b4502010-09-13 15:39:48 +0800817 max->con_xmit.buf = buffer;
818 max->con_xmit.head = 0;
819 max->con_xmit.tail = 0;
Feng Tang22510992010-06-16 14:46:09 +0100820
Mika Westerberg33b1e692011-06-23 13:39:00 +0100821 init_waitqueue_head(&max->wq);
822
Feng Tang22510992010-06-16 14:46:09 +0100823 max->main_thread = kthread_run(max3110_main_thread,
824 max, "max3110_main");
825 if (IS_ERR(max->main_thread)) {
826 ret = PTR_ERR(max->main_thread);
827 goto err_kthread;
828 }
829
Chen, Jie68357c72013-10-22 12:42:09 -0700830 if (max->irq) {
831 ret = request_irq(max->irq, serial_m3110_irq,
832 IRQ_TYPE_EDGE_FALLING, "max3110", max);
833 if (ret) {
834 max->irq = 0;
835 dev_warn(&spi->dev,
836 "unable to allocate IRQ, will use polling method\n");
837 }
838 }
839
Feng Tangee9b4502010-09-13 15:39:48 +0800840 spi_set_drvdata(spi, max);
Feng Tang22510992010-06-16 14:46:09 +0100841 pmax = max;
Feng Tangee9b4502010-09-13 15:39:48 +0800842
843 /* Give membase a psudo value to pass serial_core's check */
Jingoo Han66027692013-08-08 17:32:52 +0900844 max->port.membase = (unsigned char __iomem *)0xff110000;
Feng Tang22510992010-06-16 14:46:09 +0100845 uart_add_one_port(&serial_m3110_reg, &max->port);
846
847 return 0;
848
849err_kthread:
850 free_page((unsigned long)buffer);
851err_get_page:
Feng Tang22510992010-06-16 14:46:09 +0100852 kfree(max);
853 return ret;
854}
855
Bill Pembertonae8d8a12012-11-19 13:26:18 -0500856static int serial_m3110_remove(struct spi_device *dev)
Feng Tang22510992010-06-16 14:46:09 +0100857{
Feng Tangee9b4502010-09-13 15:39:48 +0800858 struct uart_max3110 *max = spi_get_drvdata(dev);
Feng Tang22510992010-06-16 14:46:09 +0100859
Feng Tangee9b4502010-09-13 15:39:48 +0800860 if (!max)
Feng Tang22510992010-06-16 14:46:09 +0100861 return 0;
862
Feng Tang22510992010-06-16 14:46:09 +0100863 uart_remove_one_port(&serial_m3110_reg, &max->port);
864
865 free_page((unsigned long)max->con_xmit.buf);
866
Chen, Jie68357c72013-10-22 12:42:09 -0700867 if (max->irq)
868 free_irq(max->irq, max);
869
Feng Tang22510992010-06-16 14:46:09 +0100870 if (max->main_thread)
871 kthread_stop(max->main_thread);
872
873 kfree(max);
874 return 0;
875}
876
877static struct spi_driver uart_max3110_driver = {
878 .driver = {
879 .name = "spi_max3111",
Feng Tang22510992010-06-16 14:46:09 +0100880 .owner = THIS_MODULE,
Lars-Peter Clausenc2ee91b2013-03-11 18:44:51 +0100881 .pm = SERIAL_M3110_PM_OPS,
Feng Tang22510992010-06-16 14:46:09 +0100882 },
883 .probe = serial_m3110_probe,
Bill Pemberton2d47b712012-11-19 13:21:34 -0500884 .remove = serial_m3110_remove,
Feng Tang22510992010-06-16 14:46:09 +0100885};
886
Feng Tangee9b4502010-09-13 15:39:48 +0800887static int __init serial_m3110_init(void)
Feng Tang22510992010-06-16 14:46:09 +0100888{
889 int ret = 0;
890
891 ret = uart_register_driver(&serial_m3110_reg);
892 if (ret)
893 return ret;
894
895 ret = spi_register_driver(&uart_max3110_driver);
896 if (ret)
897 uart_unregister_driver(&serial_m3110_reg);
898
899 return ret;
900}
901
Feng Tangee9b4502010-09-13 15:39:48 +0800902static void __exit serial_m3110_exit(void)
Feng Tang22510992010-06-16 14:46:09 +0100903{
904 spi_unregister_driver(&uart_max3110_driver);
905 uart_unregister_driver(&serial_m3110_reg);
906}
907
908module_init(serial_m3110_init);
909module_exit(serial_m3110_exit);
910
Feng Tangee9b4502010-09-13 15:39:48 +0800911MODULE_LICENSE("GPL v2");
Axel Lin8c4074c2011-08-01 21:20:10 +0800912MODULE_ALIAS("spi:max3110-uart");