blob: 42aa43934b20c0fd1cbaaa79953bb0aceaba25d1 [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 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030026 * 2. Currently only RX available interrrupt 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>
Feng Tang22510992010-06-16 14:46:09 +010046
47#include "mrst_max3110.h"
48
49#define PR_FMT "mrst_max3110: "
50
Arjan van de Vend6e679b2010-06-17 11:02:15 +010051#define UART_TX_NEEDED 1
52#define CON_TX_NEEDED 2
53#define BIT_IRQ_PENDING 3
54
Feng Tang22510992010-06-16 14:46:09 +010055struct uart_max3110 {
56 struct uart_port port;
57 struct spi_device *spi;
Dan Carpenterd8653d32011-01-25 14:15:11 +000058 char name[SPI_NAME_SIZE];
Feng Tang22510992010-06-16 14:46:09 +010059
60 wait_queue_head_t wq;
61 struct task_struct *main_thread;
62 struct task_struct *read_thread;
Justin P. Mattock6eab04a2011-04-08 19:49:08 -070063 struct mutex thread_mutex;
Feng Tang22510992010-06-16 14:46:09 +010064
65 u32 baud;
66 u16 cur_conf;
67 u8 clock;
68 u8 parity, word_7bits;
Feng Tangee9b4502010-09-13 15:39:48 +080069 u16 irq;
Feng Tang22510992010-06-16 14:46:09 +010070
Arjan van de Vend6e679b2010-06-17 11:02:15 +010071 unsigned long uart_flags;
Feng Tang22510992010-06-16 14:46:09 +010072
73 /* console related */
74 struct circ_buf con_xmit;
Feng Tang22510992010-06-16 14:46:09 +010075};
76
77/* global data structure, may need be removed */
Feng Tangee9b4502010-09-13 15:39:48 +080078static struct uart_max3110 *pmax;
79
Alexander Shishkin51808f02011-08-26 11:25:35 +010080static int receive_chars(struct uart_max3110 *max,
81 unsigned short *str, int len);
82static int max3110_read_multi(struct uart_max3110 *max);
Feng Tangee9b4502010-09-13 15:39:48 +080083static void max3110_con_receive(struct uart_max3110 *max);
Feng Tang22510992010-06-16 14:46:09 +010084
Feng Tangee9b4502010-09-13 15:39:48 +080085static int max3110_write_then_read(struct uart_max3110 *max,
86 const void *txbuf, void *rxbuf, unsigned len, int always_fast)
Feng Tang22510992010-06-16 14:46:09 +010087{
88 struct spi_device *spi = max->spi;
89 struct spi_message message;
90 struct spi_transfer x;
91 int ret;
92
Feng Tang22510992010-06-16 14:46:09 +010093 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 Tangee9b4502010-09-13 15:39:48 +0800101 x.speed_hz = spi->max_speed_hz;
Feng Tang22510992010-06-16 14:46:09 +0100102 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 Tangee9b4502010-09-13 15:39:48 +0800110/* Write a 16b word to the device */
111static int max3110_out(struct uart_max3110 *max, const u16 out)
Feng Tang22510992010-06-16 14:46:09 +0100112{
Feng Tangee9b4502010-09-13 15:39:48 +0800113 void *buf;
114 u16 *obuf, *ibuf;
Feng Tang22510992010-06-16 14:46:09 +0100115 int ret;
116
Feng Tangee9b4502010-09-13 15:39:48 +0800117 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 Tang22510992010-06-16 14:46:09 +0100130
Alexander Shishkin51808f02011-08-26 11:25:35 +0100131 receive_chars(max, ibuf, 1);
Feng Tang22510992010-06-16 14:46:09 +0100132
Feng Tangee9b4502010-09-13 15:39:48 +0800133exit:
134 kfree(buf);
Feng Tang22510992010-06-16 14:46:09 +0100135 return ret;
136}
137
Feng Tang22510992010-06-16 14:46:09 +0100138/*
139 * This is usually used to read data from SPIC RX FIFO, which doesn't
Feng Tangee9b4502010-09-13 15:39:48 +0800140 * need any delay like flushing character out.
141 *
142 * Return how many valide bytes are read back
Feng Tang22510992010-06-16 14:46:09 +0100143 */
Alexander Shishkin51808f02011-08-26 11:25:35 +0100144static int max3110_read_multi(struct uart_max3110 *max)
Feng Tang22510992010-06-16 14:46:09 +0100145{
Feng Tangee9b4502010-09-13 15:39:48 +0800146 void *buf;
147 u16 *obuf, *ibuf;
Alexander Shishkin51808f02011-08-26 11:25:35 +0100148 int ret, blen;
Feng Tang22510992010-06-16 14:46:09 +0100149
Feng Tangee9b4502010-09-13 15:39:48 +0800150 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 Tang22510992010-06-16 14:46:09 +0100154 return 0;
155 }
156
Feng Tangee9b4502010-09-13 15:39:48 +0800157 /* tx/rx always have the same length */
158 obuf = buf;
159 ibuf = buf + blen;
Feng Tang22510992010-06-16 14:46:09 +0100160
Feng Tangee9b4502010-09-13 15:39:48 +0800161 if (max3110_write_then_read(max, obuf, ibuf, blen, 1)) {
162 kfree(buf);
Feng Tang22510992010-06-16 14:46:09 +0100163 return 0;
Feng Tangee9b4502010-09-13 15:39:48 +0800164 }
Feng Tang22510992010-06-16 14:46:09 +0100165
Alexander Shishkin51808f02011-08-26 11:25:35 +0100166 ret = receive_chars(max, ibuf, M3110_RX_FIFO_DEPTH);
Feng Tang22510992010-06-16 14:46:09 +0100167
Feng Tangee9b4502010-09-13 15:39:48 +0800168 kfree(buf);
Alexander Shishkin51808f02011-08-26 11:25:35 +0100169 return ret;
Feng Tang22510992010-06-16 14:46:09 +0100170}
171
172static 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 Tang22510992010-06-16 14:46:09 +0100182}
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 */
190static 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 Tangee9b4502010-09-13 15:39:48 +0800197
198 if (!test_and_set_bit(CON_TX_NEEDED, &pmax->uart_flags))
Dirk Brandewie7b18bd52011-08-26 11:24:49 +0100199 wake_up(&pmax->wq);
Feng Tang22510992010-06-16 14:46:09 +0100200}
201
202static int __init
203serial_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 Tangee9b4502010-09-13 15:39:48 +0800213 if (co->index == -1)
214 co->index = 0;
215
Feng Tang22510992010-06-16 14:46:09 +0100216 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
227static 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
235static struct uart_driver serial_m3110_reg;
236static 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 Tang22510992010-06-16 14:46:09 +0100246static unsigned int serial_m3110_tx_empty(struct uart_port *port)
247{
248 return 1;
249}
250
251static void serial_m3110_stop_tx(struct uart_port *port)
252{
253 return;
254}
255
256/* stop_rx will be called in spin_lock env */
257static void serial_m3110_stop_rx(struct uart_port *port)
258{
259 return;
260}
261
262#define WORDS_PER_XFER 128
Feng Tangee9b4502010-09-13 15:39:48 +0800263static void send_circ_buf(struct uart_max3110 *max,
Feng Tang22510992010-06-16 14:46:09 +0100264 struct circ_buf *xmit)
265{
Feng Tangee9b4502010-09-13 15:39:48 +0800266 void *buf;
267 u16 *obuf, *ibuf;
Alexander Shishkin51808f02011-08-26 11:25:35 +0100268 int i, len, blen, dma_size, left, ret = 0;
Feng Tangee9b4502010-09-13 15:39:48 +0800269
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 Tang22510992010-06-16 14:46:09 +0100277
278 while (!uart_circ_empty(xmit)) {
279 left = uart_circ_chars_pending(xmit);
280 while (left) {
Feng Tangee9b4502010-09-13 15:39:48 +0800281 len = min(left, WORDS_PER_XFER);
282 blen = len * sizeof(u16);
283 memset(ibuf, 0, blen);
Feng Tang22510992010-06-16 14:46:09 +0100284
Feng Tang22510992010-06-16 14:46:09 +0100285 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 Tangee9b4502010-09-13 15:39:48 +0800290
291 /* Fail to send msg to console is not very critical */
Dirk Brandewie7b18bd52011-08-26 11:24:49 +0100292
Feng Tangee9b4502010-09-13 15:39:48 +0800293 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 Tang22510992010-06-16 14:46:09 +0100297
Alexander Shishkin51808f02011-08-26 11:25:35 +0100298 receive_chars(max, ibuf, len);
Feng Tang22510992010-06-16 14:46:09 +0100299
300 max->port.icount.tx += len;
301 left -= len;
302 }
303 }
Feng Tangee9b4502010-09-13 15:39:48 +0800304
305 kfree(buf);
Feng Tang22510992010-06-16 14:46:09 +0100306}
307
308static 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 Tangee9b4502010-09-13 15:39:48 +0800325/*
326 * This will be called by uart_write() and tty_write, can't
327 * go to sleep
328 */
Feng Tang22510992010-06-16 14:46:09 +0100329static 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 Vend6e679b2010-06-17 11:02:15 +0100334 if (!test_and_set_bit(UART_TX_NEEDED, &max->uart_flags))
Dirk Brandewie7b18bd52011-08-26 11:24:49 +0100335 wake_up(&max->wq);
Feng Tang22510992010-06-16 14:46:09 +0100336}
337
Alexander Shishkin51808f02011-08-26 11:25:35 +0100338static int
339receive_chars(struct uart_max3110 *max, unsigned short *str, int len)
Feng Tang22510992010-06-16 14:46:09 +0100340{
341 struct uart_port *port = &max->port;
342 struct tty_struct *tty;
Alexander Shishkin51808f02011-08-26 11:25:35 +0100343 char buf[M3110_RX_FIFO_DEPTH];
344 int r, w, usable;
Feng Tang22510992010-06-16 14:46:09 +0100345
346 /* If uart is not opened, just return */
347 if (!port->state)
Alexander Shishkin51808f02011-08-26 11:25:35 +0100348 return 0;
Feng Tang22510992010-06-16 14:46:09 +0100349
350 tty = port->state->port.tty;
351 if (!tty)
Alexander Shishkin51808f02011-08-26 11:25:35 +0100352 return 0;
Feng Tang22510992010-06-16 14:46:09 +0100353
Alexander Shishkin51808f02011-08-26 11:25:35 +0100354 for (r = 0, w = 0; r < len; r++) {
355 if (str[r] & MAX3110_BREAK &&
356 uart_handle_break(port))
357 continue;
358
359 if (str[r] & MAX3110_READ_DATA_AVAILABLE) {
360 if (uart_handle_sysrq_char(port, str[r] & 0xff))
361 continue;
362
363 buf[w++] = str[r] & 0xff;
364 }
365 }
366
367 if (!w)
368 return 0;
369
370 for (r = 0; w; r += usable, w -= usable) {
371 usable = tty_buffer_request_room(tty, w);
Feng Tang22510992010-06-16 14:46:09 +0100372 if (usable) {
Alexander Shishkin51808f02011-08-26 11:25:35 +0100373 tty_insert_flip_string(tty, buf + r, usable);
Feng Tang22510992010-06-16 14:46:09 +0100374 port->icount.rx += usable;
Feng Tang22510992010-06-16 14:46:09 +0100375 }
Feng Tang22510992010-06-16 14:46:09 +0100376 }
Feng Tangee9b4502010-09-13 15:39:48 +0800377 tty_flip_buffer_push(tty);
Alexander Shishkin51808f02011-08-26 11:25:35 +0100378
379 return r;
Feng Tang22510992010-06-16 14:46:09 +0100380}
381
Feng Tangee9b4502010-09-13 15:39:48 +0800382/*
383 * This routine will be used in read_thread or RX IRQ handling,
384 * it will first do one round buffer read(8 words), if there is some
385 * valid RX data, will try to read 5 more rounds till all data
386 * is read out.
387 *
388 * Use stack space as data buffer to save some system load, and chose
389 * 504 Btyes as a threadhold to do a bulk push to upper tty layer when
390 * receiving bulk data, a much bigger buffer may cause stack overflow
391 */
392static void max3110_con_receive(struct uart_max3110 *max)
Feng Tang22510992010-06-16 14:46:09 +0100393{
Alexander Shishkin51808f02011-08-26 11:25:35 +0100394 int loop = 1, num;
Feng Tang22510992010-06-16 14:46:09 +0100395
Feng Tang22510992010-06-16 14:46:09 +0100396 do {
Alexander Shishkin51808f02011-08-26 11:25:35 +0100397 num = max3110_read_multi(max);
Feng Tang22510992010-06-16 14:46:09 +0100398
399 if (num) {
Feng Tangee9b4502010-09-13 15:39:48 +0800400 loop = 5;
Feng Tang22510992010-06-16 14:46:09 +0100401 }
402 } while (--loop);
Feng Tang22510992010-06-16 14:46:09 +0100403}
404
405static int max3110_main_thread(void *_max)
406{
407 struct uart_max3110 *max = _max;
408 wait_queue_head_t *wq = &max->wq;
409 int ret = 0;
410 struct circ_buf *xmit = &max->con_xmit;
411
Feng Tang22510992010-06-16 14:46:09 +0100412 pr_info(PR_FMT "start main thread\n");
413
414 do {
Dirk Brandewie7b18bd52011-08-26 11:24:49 +0100415 wait_event_interruptible(*wq,
416 max->uart_flags || kthread_should_stop());
Arjan van de Ven68c16b42010-06-17 11:02:06 +0100417
418 mutex_lock(&max->thread_mutex);
Feng Tang22510992010-06-16 14:46:09 +0100419
Arjan van de Vend6e679b2010-06-17 11:02:15 +0100420 if (test_and_clear_bit(BIT_IRQ_PENDING, &max->uart_flags))
Feng Tangee9b4502010-09-13 15:39:48 +0800421 max3110_con_receive(max);
Feng Tang22510992010-06-16 14:46:09 +0100422
423 /* first handle console output */
Arjan van de Vend6e679b2010-06-17 11:02:15 +0100424 if (test_and_clear_bit(CON_TX_NEEDED, &max->uart_flags))
Feng Tang22510992010-06-16 14:46:09 +0100425 send_circ_buf(max, xmit);
Feng Tang22510992010-06-16 14:46:09 +0100426
427 /* handle uart output */
Arjan van de Vend6e679b2010-06-17 11:02:15 +0100428 if (test_and_clear_bit(UART_TX_NEEDED, &max->uart_flags))
Feng Tang22510992010-06-16 14:46:09 +0100429 transmit_char(max);
Arjan van de Vend6e679b2010-06-17 11:02:15 +0100430
Arjan van de Ven68c16b42010-06-17 11:02:06 +0100431 mutex_unlock(&max->thread_mutex);
Arjan van de Vend6e679b2010-06-17 11:02:15 +0100432
Feng Tang22510992010-06-16 14:46:09 +0100433 } while (!kthread_should_stop());
434
435 return ret;
436}
437
Feng Tang22510992010-06-16 14:46:09 +0100438static irqreturn_t serial_m3110_irq(int irq, void *dev_id)
439{
440 struct uart_max3110 *max = dev_id;
441
442 /* max3110's irq is a falling edge, not level triggered,
443 * so no need to disable the irq */
Dirk Brandewie7b18bd52011-08-26 11:24:49 +0100444
Arjan van de Vend6e679b2010-06-17 11:02:15 +0100445 if (!test_and_set_bit(BIT_IRQ_PENDING, &max->uart_flags))
Dirk Brandewie7b18bd52011-08-26 11:24:49 +0100446 wake_up(&max->wq);
Arjan van de Vend6e679b2010-06-17 11:02:15 +0100447
Feng Tang22510992010-06-16 14:46:09 +0100448 return IRQ_HANDLED;
449}
Alan Cox91efa752010-09-13 15:39:56 +0800450
Feng Tang22510992010-06-16 14:46:09 +0100451/* if don't use RX IRQ, then need a thread to polling read */
452static int max3110_read_thread(void *_max)
453{
454 struct uart_max3110 *max = _max;
455
456 pr_info(PR_FMT "start read thread\n");
457 do {
Feng Tangee9b4502010-09-13 15:39:48 +0800458 /*
459 * If can't acquire the mutex, it means the main thread
460 * is running which will also perform the rx job
461 */
462 if (mutex_trylock(&max->thread_mutex)) {
463 max3110_con_receive(max);
464 mutex_unlock(&max->thread_mutex);
465 }
Feng Tang22510992010-06-16 14:46:09 +0100466
467 set_current_state(TASK_INTERRUPTIBLE);
468 schedule_timeout(HZ / 20);
469 } while (!kthread_should_stop());
470
471 return 0;
472}
Feng Tang22510992010-06-16 14:46:09 +0100473
474static int serial_m3110_startup(struct uart_port *port)
475{
476 struct uart_max3110 *max =
477 container_of(port, struct uart_max3110, port);
478 u16 config = 0;
479 int ret = 0;
480
Feng Tangee9b4502010-09-13 15:39:48 +0800481 if (port->line != 0) {
Feng Tang22510992010-06-16 14:46:09 +0100482 pr_err(PR_FMT "uart port startup failed\n");
Feng Tangee9b4502010-09-13 15:39:48 +0800483 return -1;
484 }
Feng Tang22510992010-06-16 14:46:09 +0100485
Feng Tangee9b4502010-09-13 15:39:48 +0800486 /* Disable all IRQ and config it to 115200, 8n1 */
Feng Tang22510992010-06-16 14:46:09 +0100487 config = WC_TAG | WC_FIFO_ENABLE
488 | WC_1_STOPBITS
489 | WC_8BIT_WORD
490 | WC_BAUD_DR2;
Feng Tang22510992010-06-16 14:46:09 +0100491
492 /* as we use thread to handle tx/rx, need set low latency */
493 port->state->port.tty->low_latency = 1;
494
Alan Cox91efa752010-09-13 15:39:56 +0800495 if (max->irq) {
Feng Tangee9b4502010-09-13 15:39:48 +0800496 max->read_thread = NULL;
Alan Cox91efa752010-09-13 15:39:56 +0800497 ret = request_irq(max->irq, serial_m3110_irq,
498 IRQ_TYPE_EDGE_FALLING, "max3110", max);
499 if (ret) {
500 max->irq = 0;
501 pr_err(PR_FMT "unable to allocate IRQ, polling\n");
502 } else {
503 /* Enable RX IRQ only */
504 config |= WC_RXA_IRQ_ENABLE;
505 }
Feng Tangee9b4502010-09-13 15:39:48 +0800506 }
Alan Cox91efa752010-09-13 15:39:56 +0800507
508 if (max->irq == 0) {
509 /* If IRQ is disabled, start a read thread for input data */
510 max->read_thread =
511 kthread_run(max3110_read_thread, max, "max3110_read");
512 if (IS_ERR(max->read_thread)) {
513 ret = PTR_ERR(max->read_thread);
514 max->read_thread = NULL;
515 pr_err(PR_FMT "Can't create read thread!\n");
516 return ret;
517 }
518 }
Feng Tang22510992010-06-16 14:46:09 +0100519
Feng Tangee9b4502010-09-13 15:39:48 +0800520 ret = max3110_out(max, config);
521 if (ret) {
Alan Cox91efa752010-09-13 15:39:56 +0800522 if (max->irq)
523 free_irq(max->irq, max);
524 if (max->read_thread)
525 kthread_stop(max->read_thread);
Feng Tangee9b4502010-09-13 15:39:48 +0800526 max->read_thread = NULL;
Feng Tangee9b4502010-09-13 15:39:48 +0800527 return ret;
528 }
529
Feng Tang22510992010-06-16 14:46:09 +0100530 max->cur_conf = config;
531 return 0;
532}
533
534static void serial_m3110_shutdown(struct uart_port *port)
535{
536 struct uart_max3110 *max =
537 container_of(port, struct uart_max3110, port);
538 u16 config;
539
540 if (max->read_thread) {
541 kthread_stop(max->read_thread);
542 max->read_thread = NULL;
543 }
544
Alan Cox91efa752010-09-13 15:39:56 +0800545 if (max->irq)
546 free_irq(max->irq, max);
Feng Tang22510992010-06-16 14:46:09 +0100547
548 /* Disable interrupts from this port */
549 config = WC_TAG | WC_SW_SHDI;
550 max3110_out(max, config);
551}
552
553static void serial_m3110_release_port(struct uart_port *port)
554{
555}
556
557static int serial_m3110_request_port(struct uart_port *port)
558{
559 return 0;
560}
561
562static void serial_m3110_config_port(struct uart_port *port, int flags)
563{
Feng Tangee9b4502010-09-13 15:39:48 +0800564 port->type = PORT_MAX3100;
Feng Tang22510992010-06-16 14:46:09 +0100565}
566
567static int
568serial_m3110_verify_port(struct uart_port *port, struct serial_struct *ser)
569{
570 /* we don't want the core code to modify any port params */
571 return -EINVAL;
572}
573
574
575static const char *serial_m3110_type(struct uart_port *port)
576{
577 struct uart_max3110 *max =
578 container_of(port, struct uart_max3110, port);
579 return max->name;
580}
581
582static void
583serial_m3110_set_termios(struct uart_port *port, struct ktermios *termios,
584 struct ktermios *old)
585{
586 struct uart_max3110 *max =
587 container_of(port, struct uart_max3110, port);
588 unsigned char cval;
589 unsigned int baud, parity = 0;
590 int clk_div = -1;
591 u16 new_conf = max->cur_conf;
592
593 switch (termios->c_cflag & CSIZE) {
594 case CS7:
595 cval = UART_LCR_WLEN7;
596 new_conf |= WC_7BIT_WORD;
597 break;
598 default:
Feng Tangee9b4502010-09-13 15:39:48 +0800599 /* We only support CS7 & CS8 */
600 termios->c_cflag &= ~CSIZE;
601 termios->c_cflag |= CS8;
Feng Tang22510992010-06-16 14:46:09 +0100602 case CS8:
603 cval = UART_LCR_WLEN8;
604 new_conf |= WC_8BIT_WORD;
605 break;
606 }
607
608 baud = uart_get_baud_rate(port, termios, old, 0, 230400);
609
Feng Tangee9b4502010-09-13 15:39:48 +0800610 /* First calc the div for 1.8MHZ clock case */
Feng Tang22510992010-06-16 14:46:09 +0100611 switch (baud) {
612 case 300:
613 clk_div = WC_BAUD_DR384;
614 break;
615 case 600:
616 clk_div = WC_BAUD_DR192;
617 break;
618 case 1200:
619 clk_div = WC_BAUD_DR96;
620 break;
621 case 2400:
622 clk_div = WC_BAUD_DR48;
623 break;
624 case 4800:
625 clk_div = WC_BAUD_DR24;
626 break;
627 case 9600:
628 clk_div = WC_BAUD_DR12;
629 break;
630 case 19200:
631 clk_div = WC_BAUD_DR6;
632 break;
633 case 38400:
634 clk_div = WC_BAUD_DR3;
635 break;
636 case 57600:
637 clk_div = WC_BAUD_DR2;
638 break;
639 case 115200:
640 clk_div = WC_BAUD_DR1;
641 break;
642 case 230400:
643 if (max->clock & MAX3110_HIGH_CLK)
644 break;
645 default:
Feng Tangee9b4502010-09-13 15:39:48 +0800646 /* Pick the previous baud rate */
Feng Tang22510992010-06-16 14:46:09 +0100647 baud = max->baud;
648 clk_div = max->cur_conf & WC_BAUD_DIV_MASK;
649 tty_termios_encode_baud_rate(termios, baud, baud);
650 }
651
652 if (max->clock & MAX3110_HIGH_CLK) {
653 clk_div += 1;
Feng Tangee9b4502010-09-13 15:39:48 +0800654 /* High clk version max3110 doesn't support B300 */
655 if (baud == 300) {
Feng Tang22510992010-06-16 14:46:09 +0100656 baud = 600;
Feng Tangee9b4502010-09-13 15:39:48 +0800657 clk_div = WC_BAUD_DR384;
658 }
Feng Tang22510992010-06-16 14:46:09 +0100659 if (baud == 230400)
660 clk_div = WC_BAUD_DR1;
661 tty_termios_encode_baud_rate(termios, baud, baud);
662 }
663
664 new_conf = (new_conf & ~WC_BAUD_DIV_MASK) | clk_div;
Feng Tangee9b4502010-09-13 15:39:48 +0800665
666 if (unlikely(termios->c_cflag & CMSPAR))
667 termios->c_cflag &= ~CMSPAR;
668
Feng Tang22510992010-06-16 14:46:09 +0100669 if (termios->c_cflag & CSTOPB)
670 new_conf |= WC_2_STOPBITS;
671 else
672 new_conf &= ~WC_2_STOPBITS;
673
674 if (termios->c_cflag & PARENB) {
675 new_conf |= WC_PARITY_ENABLE;
676 parity |= UART_LCR_PARITY;
677 } else
678 new_conf &= ~WC_PARITY_ENABLE;
679
680 if (!(termios->c_cflag & PARODD))
681 parity |= UART_LCR_EPAR;
682 max->parity = parity;
683
684 uart_update_timeout(port, termios->c_cflag, baud);
685
686 new_conf |= WC_TAG;
687 if (new_conf != max->cur_conf) {
Feng Tangee9b4502010-09-13 15:39:48 +0800688 if (!max3110_out(max, new_conf)) {
689 max->cur_conf = new_conf;
690 max->baud = baud;
691 }
Feng Tang22510992010-06-16 14:46:09 +0100692 }
693}
694
Feng Tangee9b4502010-09-13 15:39:48 +0800695/* Don't handle hw handshaking */
Feng Tang22510992010-06-16 14:46:09 +0100696static unsigned int serial_m3110_get_mctrl(struct uart_port *port)
697{
698 return TIOCM_DSR | TIOCM_CAR | TIOCM_DSR;
699}
700
701static void serial_m3110_set_mctrl(struct uart_port *port, unsigned int mctrl)
702{
703}
704
705static void serial_m3110_break_ctl(struct uart_port *port, int break_state)
706{
707}
708
709static void serial_m3110_pm(struct uart_port *port, unsigned int state,
710 unsigned int oldstate)
711{
712}
713
714static void serial_m3110_enable_ms(struct uart_port *port)
715{
716}
717
718struct uart_ops serial_m3110_ops = {
719 .tx_empty = serial_m3110_tx_empty,
720 .set_mctrl = serial_m3110_set_mctrl,
721 .get_mctrl = serial_m3110_get_mctrl,
722 .stop_tx = serial_m3110_stop_tx,
723 .start_tx = serial_m3110_start_tx,
724 .stop_rx = serial_m3110_stop_rx,
725 .enable_ms = serial_m3110_enable_ms,
726 .break_ctl = serial_m3110_break_ctl,
727 .startup = serial_m3110_startup,
728 .shutdown = serial_m3110_shutdown,
Feng Tangee9b4502010-09-13 15:39:48 +0800729 .set_termios = serial_m3110_set_termios,
Feng Tang22510992010-06-16 14:46:09 +0100730 .pm = serial_m3110_pm,
731 .type = serial_m3110_type,
732 .release_port = serial_m3110_release_port,
733 .request_port = serial_m3110_request_port,
734 .config_port = serial_m3110_config_port,
735 .verify_port = serial_m3110_verify_port,
736};
737
738static struct uart_driver serial_m3110_reg = {
739 .owner = THIS_MODULE,
740 .driver_name = "MRST serial",
741 .dev_name = "ttyS",
742 .major = TTY_MAJOR,
743 .minor = 64,
744 .nr = 1,
Feng Tangee9b4502010-09-13 15:39:48 +0800745 .cons = &serial_m3110_console,
Feng Tang22510992010-06-16 14:46:09 +0100746};
747
Feng Tangee9b4502010-09-13 15:39:48 +0800748#ifdef CONFIG_PM
Feng Tang22510992010-06-16 14:46:09 +0100749static int serial_m3110_suspend(struct spi_device *spi, pm_message_t state)
750{
Feng Tangee9b4502010-09-13 15:39:48 +0800751 struct uart_max3110 *max = spi_get_drvdata(spi);
752
753 disable_irq(max->irq);
754 uart_suspend_port(&serial_m3110_reg, &max->port);
755 max3110_out(max, max->cur_conf | WC_SW_SHDI);
Feng Tang22510992010-06-16 14:46:09 +0100756 return 0;
757}
758
759static int serial_m3110_resume(struct spi_device *spi)
760{
Feng Tangee9b4502010-09-13 15:39:48 +0800761 struct uart_max3110 *max = spi_get_drvdata(spi);
762
763 max3110_out(max, max->cur_conf);
764 uart_resume_port(&serial_m3110_reg, &max->port);
765 enable_irq(max->irq);
Feng Tang22510992010-06-16 14:46:09 +0100766 return 0;
767}
Feng Tangee9b4502010-09-13 15:39:48 +0800768#else
769#define serial_m3110_suspend NULL
770#define serial_m3110_resume NULL
771#endif
Feng Tang22510992010-06-16 14:46:09 +0100772
Feng Tangee9b4502010-09-13 15:39:48 +0800773static int __devinit serial_m3110_probe(struct spi_device *spi)
Feng Tang22510992010-06-16 14:46:09 +0100774{
775 struct uart_max3110 *max;
Feng Tangee9b4502010-09-13 15:39:48 +0800776 void *buffer;
jianwei.yang99dd3f62010-06-16 14:46:49 +0100777 u16 res;
Feng Tangee9b4502010-09-13 15:39:48 +0800778 int ret = 0;
779
Feng Tang22510992010-06-16 14:46:09 +0100780 max = kzalloc(sizeof(*max), GFP_KERNEL);
781 if (!max)
782 return -ENOMEM;
783
Feng Tangee9b4502010-09-13 15:39:48 +0800784 /* Set spi info */
Feng Tang22510992010-06-16 14:46:09 +0100785 spi->bits_per_word = 16;
786 max->clock = MAX3110_HIGH_CLK;
Feng Tang22510992010-06-16 14:46:09 +0100787
788 spi_setup(spi);
789
Feng Tangee9b4502010-09-13 15:39:48 +0800790 max->port.type = PORT_MAX3100;
791 max->port.fifosize = 2; /* Only have 16b buffer */
Feng Tang22510992010-06-16 14:46:09 +0100792 max->port.ops = &serial_m3110_ops;
793 max->port.line = 0;
794 max->port.dev = &spi->dev;
795 max->port.uartclk = 115200;
796
797 max->spi = spi;
Feng Tangee9b4502010-09-13 15:39:48 +0800798 strcpy(max->name, spi->modalias);
Feng Tang22510992010-06-16 14:46:09 +0100799 max->irq = (u16)spi->irq;
800
Arjan van de Ven68c16b42010-06-17 11:02:06 +0100801 mutex_init(&max->thread_mutex);
Feng Tang22510992010-06-16 14:46:09 +0100802
803 max->word_7bits = 0;
804 max->parity = 0;
805 max->baud = 0;
806
807 max->cur_conf = 0;
Arjan van de Vend6e679b2010-06-17 11:02:15 +0100808 max->uart_flags = 0;
809
jianwei.yang99dd3f62010-06-16 14:46:49 +0100810 /* Check if reading configuration register returns something sane */
Feng Tang22510992010-06-16 14:46:09 +0100811
jianwei.yang99dd3f62010-06-16 14:46:49 +0100812 res = RC_TAG;
813 ret = max3110_write_then_read(max, (u8 *)&res, (u8 *)&res, 2, 0);
814 if (ret < 0 || res == 0 || res == 0xffff) {
William Douglas0bb04bf2011-06-23 13:38:36 +0100815 dev_dbg(&spi->dev, "MAX3111 deemed not present (conf reg %04x)",
jianwei.yang99dd3f62010-06-16 14:46:49 +0100816 res);
817 ret = -ENODEV;
818 goto err_get_page;
819 }
Feng Tangee9b4502010-09-13 15:39:48 +0800820
821 buffer = (void *)__get_free_page(GFP_KERNEL);
Feng Tang22510992010-06-16 14:46:09 +0100822 if (!buffer) {
823 ret = -ENOMEM;
824 goto err_get_page;
825 }
Feng Tangee9b4502010-09-13 15:39:48 +0800826 max->con_xmit.buf = buffer;
827 max->con_xmit.head = 0;
828 max->con_xmit.tail = 0;
Feng Tang22510992010-06-16 14:46:09 +0100829
Mika Westerberg33b1e692011-06-23 13:39:00 +0100830 init_waitqueue_head(&max->wq);
831
Feng Tang22510992010-06-16 14:46:09 +0100832 max->main_thread = kthread_run(max3110_main_thread,
833 max, "max3110_main");
834 if (IS_ERR(max->main_thread)) {
835 ret = PTR_ERR(max->main_thread);
836 goto err_kthread;
837 }
838
Feng Tangee9b4502010-09-13 15:39:48 +0800839 spi_set_drvdata(spi, max);
Feng Tang22510992010-06-16 14:46:09 +0100840 pmax = max;
Feng Tangee9b4502010-09-13 15:39:48 +0800841
842 /* Give membase a psudo value to pass serial_core's check */
Feng Tang22510992010-06-16 14:46:09 +0100843 max->port.membase = (void *)0xff110000;
844 uart_add_one_port(&serial_m3110_reg, &max->port);
845
846 return 0;
847
848err_kthread:
849 free_page((unsigned long)buffer);
850err_get_page:
Feng Tang22510992010-06-16 14:46:09 +0100851 kfree(max);
852 return ret;
853}
854
Feng Tangee9b4502010-09-13 15:39:48 +0800855static int __devexit serial_m3110_remove(struct spi_device *dev)
Feng Tang22510992010-06-16 14:46:09 +0100856{
Feng Tangee9b4502010-09-13 15:39:48 +0800857 struct uart_max3110 *max = spi_get_drvdata(dev);
Feng Tang22510992010-06-16 14:46:09 +0100858
Feng Tangee9b4502010-09-13 15:39:48 +0800859 if (!max)
Feng Tang22510992010-06-16 14:46:09 +0100860 return 0;
861
Feng Tang22510992010-06-16 14:46:09 +0100862 uart_remove_one_port(&serial_m3110_reg, &max->port);
863
864 free_page((unsigned long)max->con_xmit.buf);
865
866 if (max->main_thread)
867 kthread_stop(max->main_thread);
868
869 kfree(max);
870 return 0;
871}
872
873static struct spi_driver uart_max3110_driver = {
874 .driver = {
875 .name = "spi_max3111",
876 .bus = &spi_bus_type,
877 .owner = THIS_MODULE,
878 },
879 .probe = serial_m3110_probe,
Feng Tangee9b4502010-09-13 15:39:48 +0800880 .remove = __devexit_p(serial_m3110_remove),
Feng Tang22510992010-06-16 14:46:09 +0100881 .suspend = serial_m3110_suspend,
882 .resume = serial_m3110_resume,
883};
884
Feng Tangee9b4502010-09-13 15:39:48 +0800885static int __init serial_m3110_init(void)
Feng Tang22510992010-06-16 14:46:09 +0100886{
887 int ret = 0;
888
889 ret = uart_register_driver(&serial_m3110_reg);
890 if (ret)
891 return ret;
892
893 ret = spi_register_driver(&uart_max3110_driver);
894 if (ret)
895 uart_unregister_driver(&serial_m3110_reg);
896
897 return ret;
898}
899
Feng Tangee9b4502010-09-13 15:39:48 +0800900static void __exit serial_m3110_exit(void)
Feng Tang22510992010-06-16 14:46:09 +0100901{
902 spi_unregister_driver(&uart_max3110_driver);
903 uart_unregister_driver(&serial_m3110_reg);
904}
905
906module_init(serial_m3110_init);
907module_exit(serial_m3110_exit);
908
Feng Tangee9b4502010-09-13 15:39:48 +0800909MODULE_LICENSE("GPL v2");
Feng Tang22510992010-06-16 14:46:09 +0100910MODULE_ALIAS("max3110-uart");