blob: c8f986876229b9626fb841fe51300b38a5cae13b [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
30#include <linux/module.h>
31#include <linux/ioport.h>
Andrew Mortonc0443912010-09-30 15:15:29 -070032#include <linux/irq.h>
Feng Tang22510992010-06-16 14:46:09 +010033#include <linux/init.h>
34#include <linux/console.h>
Feng Tang22510992010-06-16 14:46:09 +010035#include <linux/tty.h>
36#include <linux/tty_flip.h>
37#include <linux/serial_core.h>
38#include <linux/serial_reg.h>
39
40#include <linux/kthread.h>
Feng Tang22510992010-06-16 14:46:09 +010041#include <linux/spi/spi.h>
Feng Tang22510992010-06-16 14:46:09 +010042
43#include "mrst_max3110.h"
44
45#define PR_FMT "mrst_max3110: "
46
Arjan van de Vend6e679b2010-06-17 11:02:15 +010047#define UART_TX_NEEDED 1
48#define CON_TX_NEEDED 2
49#define BIT_IRQ_PENDING 3
50
Feng Tang22510992010-06-16 14:46:09 +010051struct uart_max3110 {
52 struct uart_port port;
53 struct spi_device *spi;
Dan Carpenterd8653d32011-01-25 14:15:11 +000054 char name[SPI_NAME_SIZE];
Feng Tang22510992010-06-16 14:46:09 +010055
56 wait_queue_head_t wq;
57 struct task_struct *main_thread;
58 struct task_struct *read_thread;
Justin P. Mattock6eab04a2011-04-08 19:49:08 -070059 struct mutex thread_mutex;
Feng Tang22510992010-06-16 14:46:09 +010060
61 u32 baud;
62 u16 cur_conf;
63 u8 clock;
64 u8 parity, word_7bits;
Feng Tangee9b4502010-09-13 15:39:48 +080065 u16 irq;
Feng Tang22510992010-06-16 14:46:09 +010066
Arjan van de Vend6e679b2010-06-17 11:02:15 +010067 unsigned long uart_flags;
Feng Tang22510992010-06-16 14:46:09 +010068
69 /* console related */
70 struct circ_buf con_xmit;
Feng Tang22510992010-06-16 14:46:09 +010071};
72
73/* global data structure, may need be removed */
Feng Tangee9b4502010-09-13 15:39:48 +080074static struct uart_max3110 *pmax;
75
Feng Tang22510992010-06-16 14:46:09 +010076static void receive_chars(struct uart_max3110 *max,
77 unsigned char *str, int len);
Feng Tangee9b4502010-09-13 15:39:48 +080078static int max3110_read_multi(struct uart_max3110 *max, u8 *buf);
79static void max3110_con_receive(struct uart_max3110 *max);
Feng Tang22510992010-06-16 14:46:09 +010080
Feng Tangee9b4502010-09-13 15:39:48 +080081static int max3110_write_then_read(struct uart_max3110 *max,
82 const void *txbuf, void *rxbuf, unsigned len, int always_fast)
Feng Tang22510992010-06-16 14:46:09 +010083{
84 struct spi_device *spi = max->spi;
85 struct spi_message message;
86 struct spi_transfer x;
87 int ret;
88
Feng Tang22510992010-06-16 14:46:09 +010089 spi_message_init(&message);
90 memset(&x, 0, sizeof x);
91 x.len = len;
92 x.tx_buf = txbuf;
93 x.rx_buf = rxbuf;
94 spi_message_add_tail(&x, &message);
95
96 if (always_fast)
Feng Tangee9b4502010-09-13 15:39:48 +080097 x.speed_hz = spi->max_speed_hz;
Feng Tang22510992010-06-16 14:46:09 +010098 else if (max->baud)
99 x.speed_hz = max->baud;
100
101 /* Do the i/o */
102 ret = spi_sync(spi, &message);
103 return ret;
104}
105
Feng Tangee9b4502010-09-13 15:39:48 +0800106/* Write a 16b word to the device */
107static int max3110_out(struct uart_max3110 *max, const u16 out)
Feng Tang22510992010-06-16 14:46:09 +0100108{
Feng Tangee9b4502010-09-13 15:39:48 +0800109 void *buf;
110 u16 *obuf, *ibuf;
111 u8 ch;
Feng Tang22510992010-06-16 14:46:09 +0100112 int ret;
113
Feng Tangee9b4502010-09-13 15:39:48 +0800114 buf = kzalloc(8, GFP_KERNEL | GFP_DMA);
115 if (!buf)
116 return -ENOMEM;
117
118 obuf = buf;
119 ibuf = buf + 4;
120 *obuf = out;
121 ret = max3110_write_then_read(max, obuf, ibuf, 2, 1);
122 if (ret) {
123 pr_warning(PR_FMT "%s(): get err msg %d when sending 0x%x\n",
124 __func__, ret, out);
125 goto exit;
126 }
Feng Tang22510992010-06-16 14:46:09 +0100127
128 /* If some valid data is read back */
Feng Tangee9b4502010-09-13 15:39:48 +0800129 if (*ibuf & MAX3110_READ_DATA_AVAILABLE) {
130 ch = *ibuf & 0xff;
131 receive_chars(max, &ch, 1);
132 }
Feng Tang22510992010-06-16 14:46:09 +0100133
Feng Tangee9b4502010-09-13 15:39:48 +0800134exit:
135 kfree(buf);
Feng Tang22510992010-06-16 14:46:09 +0100136 return ret;
137}
138
Feng Tang22510992010-06-16 14:46:09 +0100139/*
140 * This is usually used to read data from SPIC RX FIFO, which doesn't
Feng Tangee9b4502010-09-13 15:39:48 +0800141 * need any delay like flushing character out.
142 *
143 * Return how many valide bytes are read back
Feng Tang22510992010-06-16 14:46:09 +0100144 */
Feng Tangee9b4502010-09-13 15:39:48 +0800145static int max3110_read_multi(struct uart_max3110 *max, u8 *rxbuf)
Feng Tang22510992010-06-16 14:46:09 +0100146{
Feng Tangee9b4502010-09-13 15:39:48 +0800147 void *buf;
148 u16 *obuf, *ibuf;
149 u8 *pbuf, valid_str[M3110_RX_FIFO_DEPTH];
150 int i, j, blen;
Feng Tang22510992010-06-16 14:46:09 +0100151
Feng Tangee9b4502010-09-13 15:39:48 +0800152 blen = M3110_RX_FIFO_DEPTH * sizeof(u16);
153 buf = kzalloc(blen * 2, GFP_KERNEL | GFP_DMA);
154 if (!buf) {
155 pr_warning(PR_FMT "%s(): fail to alloc dma buffer\n", __func__);
Feng Tang22510992010-06-16 14:46:09 +0100156 return 0;
157 }
158
Feng Tangee9b4502010-09-13 15:39:48 +0800159 /* tx/rx always have the same length */
160 obuf = buf;
161 ibuf = buf + blen;
Feng Tang22510992010-06-16 14:46:09 +0100162
Feng Tangee9b4502010-09-13 15:39:48 +0800163 if (max3110_write_then_read(max, obuf, ibuf, blen, 1)) {
164 kfree(buf);
Feng Tang22510992010-06-16 14:46:09 +0100165 return 0;
Feng Tangee9b4502010-09-13 15:39:48 +0800166 }
Feng Tang22510992010-06-16 14:46:09 +0100167
Feng Tangee9b4502010-09-13 15:39:48 +0800168 /* If caller doesn't provide a buffer, then handle received char */
169 pbuf = rxbuf ? rxbuf : valid_str;
Feng Tang22510992010-06-16 14:46:09 +0100170
Feng Tangee9b4502010-09-13 15:39:48 +0800171 for (i = 0, j = 0; i < M3110_RX_FIFO_DEPTH; i++) {
172 if (ibuf[i] & MAX3110_READ_DATA_AVAILABLE)
173 pbuf[j++] = ibuf[i] & 0xff;
Feng Tang22510992010-06-16 14:46:09 +0100174 }
175
176 if (j && (pbuf == valid_str))
177 receive_chars(max, valid_str, j);
178
Feng Tangee9b4502010-09-13 15:39:48 +0800179 kfree(buf);
Feng Tang22510992010-06-16 14:46:09 +0100180 return j;
181}
182
183static void serial_m3110_con_putchar(struct uart_port *port, int ch)
184{
185 struct uart_max3110 *max =
186 container_of(port, struct uart_max3110, port);
187 struct circ_buf *xmit = &max->con_xmit;
188
189 if (uart_circ_chars_free(xmit)) {
190 xmit->buf[xmit->head] = (char)ch;
191 xmit->head = (xmit->head + 1) & (PAGE_SIZE - 1);
192 }
Feng Tang22510992010-06-16 14:46:09 +0100193}
194
195/*
196 * Print a string to the serial port trying not to disturb
197 * any possible real use of the port...
198 *
199 * The console_lock must be held when we get here.
200 */
201static void serial_m3110_con_write(struct console *co,
202 const char *s, unsigned int count)
203{
204 if (!pmax)
205 return;
206
207 uart_console_write(&pmax->port, s, count, serial_m3110_con_putchar);
Feng Tangee9b4502010-09-13 15:39:48 +0800208
209 if (!test_and_set_bit(CON_TX_NEEDED, &pmax->uart_flags))
Dirk Brandewie7b18bd52011-08-26 11:24:49 +0100210 wake_up(&pmax->wq);
Feng Tang22510992010-06-16 14:46:09 +0100211}
212
213static int __init
214serial_m3110_con_setup(struct console *co, char *options)
215{
216 struct uart_max3110 *max = pmax;
217 int baud = 115200;
218 int bits = 8;
219 int parity = 'n';
220 int flow = 'n';
221
222 pr_info(PR_FMT "setting up console\n");
223
Feng Tangee9b4502010-09-13 15:39:48 +0800224 if (co->index == -1)
225 co->index = 0;
226
Feng Tang22510992010-06-16 14:46:09 +0100227 if (!max) {
228 pr_err(PR_FMT "pmax is NULL, return");
229 return -ENODEV;
230 }
231
232 if (options)
233 uart_parse_options(options, &baud, &parity, &bits, &flow);
234
235 return uart_set_options(&max->port, co, baud, parity, bits, flow);
236}
237
238static struct tty_driver *serial_m3110_con_device(struct console *co,
239 int *index)
240{
241 struct uart_driver *p = co->data;
242 *index = co->index;
243 return p->tty_driver;
244}
245
246static struct uart_driver serial_m3110_reg;
247static struct console serial_m3110_console = {
248 .name = "ttyS",
249 .write = serial_m3110_con_write,
250 .device = serial_m3110_con_device,
251 .setup = serial_m3110_con_setup,
252 .flags = CON_PRINTBUFFER,
253 .index = -1,
254 .data = &serial_m3110_reg,
255};
256
Feng Tang22510992010-06-16 14:46:09 +0100257static unsigned int serial_m3110_tx_empty(struct uart_port *port)
258{
259 return 1;
260}
261
262static void serial_m3110_stop_tx(struct uart_port *port)
263{
264 return;
265}
266
267/* stop_rx will be called in spin_lock env */
268static void serial_m3110_stop_rx(struct uart_port *port)
269{
270 return;
271}
272
273#define WORDS_PER_XFER 128
Feng Tangee9b4502010-09-13 15:39:48 +0800274static void send_circ_buf(struct uart_max3110 *max,
Feng Tang22510992010-06-16 14:46:09 +0100275 struct circ_buf *xmit)
276{
Feng Tangee9b4502010-09-13 15:39:48 +0800277 void *buf;
278 u16 *obuf, *ibuf;
Feng Tang22510992010-06-16 14:46:09 +0100279 u8 valid_str[WORDS_PER_XFER];
Feng Tangee9b4502010-09-13 15:39:48 +0800280 int i, j, len, blen, dma_size, left, ret = 0;
281
282
283 dma_size = WORDS_PER_XFER * sizeof(u16) * 2;
284 buf = kzalloc(dma_size, GFP_KERNEL | GFP_DMA);
285 if (!buf)
286 return;
287 obuf = buf;
288 ibuf = buf + dma_size/2;
Feng Tang22510992010-06-16 14:46:09 +0100289
290 while (!uart_circ_empty(xmit)) {
291 left = uart_circ_chars_pending(xmit);
292 while (left) {
Feng Tangee9b4502010-09-13 15:39:48 +0800293 len = min(left, WORDS_PER_XFER);
294 blen = len * sizeof(u16);
295 memset(ibuf, 0, blen);
Feng Tang22510992010-06-16 14:46:09 +0100296
Feng Tang22510992010-06-16 14:46:09 +0100297 for (i = 0; i < len; i++) {
298 obuf[i] = (u8)xmit->buf[xmit->tail] | WD_TAG;
299 xmit->tail = (xmit->tail + 1) &
300 (UART_XMIT_SIZE - 1);
301 }
Feng Tangee9b4502010-09-13 15:39:48 +0800302
303 /* Fail to send msg to console is not very critical */
Dirk Brandewie7b18bd52011-08-26 11:24:49 +0100304
Feng Tangee9b4502010-09-13 15:39:48 +0800305 ret = max3110_write_then_read(max, obuf, ibuf, blen, 0);
306 if (ret)
307 pr_warning(PR_FMT "%s(): get err msg %d\n",
308 __func__, ret);
Feng Tang22510992010-06-16 14:46:09 +0100309
310 for (i = 0, j = 0; i < len; i++) {
311 if (ibuf[i] & MAX3110_READ_DATA_AVAILABLE)
Feng Tangee9b4502010-09-13 15:39:48 +0800312 valid_str[j++] = ibuf[i] & 0xff;
Feng Tang22510992010-06-16 14:46:09 +0100313 }
314
315 if (j)
316 receive_chars(max, valid_str, j);
317
318 max->port.icount.tx += len;
319 left -= len;
320 }
321 }
Feng Tangee9b4502010-09-13 15:39:48 +0800322
323 kfree(buf);
Feng Tang22510992010-06-16 14:46:09 +0100324}
325
326static void transmit_char(struct uart_max3110 *max)
327{
328 struct uart_port *port = &max->port;
329 struct circ_buf *xmit = &port->state->xmit;
330
331 if (uart_circ_empty(xmit) || uart_tx_stopped(port))
332 return;
333
334 send_circ_buf(max, xmit);
335
336 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
337 uart_write_wakeup(port);
338
339 if (uart_circ_empty(xmit))
340 serial_m3110_stop_tx(port);
341}
342
Feng Tangee9b4502010-09-13 15:39:48 +0800343/*
344 * This will be called by uart_write() and tty_write, can't
345 * go to sleep
346 */
Feng Tang22510992010-06-16 14:46:09 +0100347static void serial_m3110_start_tx(struct uart_port *port)
348{
349 struct uart_max3110 *max =
350 container_of(port, struct uart_max3110, port);
351
Arjan van de Vend6e679b2010-06-17 11:02:15 +0100352 if (!test_and_set_bit(UART_TX_NEEDED, &max->uart_flags))
Dirk Brandewie7b18bd52011-08-26 11:24:49 +0100353 wake_up(&max->wq);
Feng Tang22510992010-06-16 14:46:09 +0100354}
355
356static void receive_chars(struct uart_max3110 *max, unsigned char *str, int len)
357{
358 struct uart_port *port = &max->port;
359 struct tty_struct *tty;
360 int usable;
361
362 /* If uart is not opened, just return */
363 if (!port->state)
364 return;
365
366 tty = port->state->port.tty;
367 if (!tty)
Feng Tangee9b4502010-09-13 15:39:48 +0800368 return;
Feng Tang22510992010-06-16 14:46:09 +0100369
370 while (len) {
371 usable = tty_buffer_request_room(tty, len);
372 if (usable) {
373 tty_insert_flip_string(tty, str, usable);
374 str += usable;
375 port->icount.rx += usable;
Feng Tang22510992010-06-16 14:46:09 +0100376 }
377 len -= usable;
378 }
Feng Tangee9b4502010-09-13 15:39:48 +0800379 tty_flip_buffer_push(tty);
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{
394 int loop = 1, num, total = 0;
395 u8 recv_buf[512], *pbuf;
396
397 pbuf = recv_buf;
398 do {
Feng Tangee9b4502010-09-13 15:39:48 +0800399 num = max3110_read_multi(max, pbuf);
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 pbuf += num;
404 total += num;
405
Feng Tangee9b4502010-09-13 15:39:48 +0800406 if (total >= 504) {
Feng Tang22510992010-06-16 14:46:09 +0100407 receive_chars(max, recv_buf, total);
408 pbuf = recv_buf;
409 total = 0;
410 }
411 }
412 } while (--loop);
413
414 if (total)
415 receive_chars(max, recv_buf, total);
416}
417
418static int max3110_main_thread(void *_max)
419{
420 struct uart_max3110 *max = _max;
421 wait_queue_head_t *wq = &max->wq;
422 int ret = 0;
423 struct circ_buf *xmit = &max->con_xmit;
424
Feng Tang22510992010-06-16 14:46:09 +0100425 pr_info(PR_FMT "start main thread\n");
426
427 do {
Dirk Brandewie7b18bd52011-08-26 11:24:49 +0100428 wait_event_interruptible(*wq,
429 max->uart_flags || kthread_should_stop());
Arjan van de Ven68c16b42010-06-17 11:02:06 +0100430
431 mutex_lock(&max->thread_mutex);
Feng Tang22510992010-06-16 14:46:09 +0100432
Arjan van de Vend6e679b2010-06-17 11:02:15 +0100433 if (test_and_clear_bit(BIT_IRQ_PENDING, &max->uart_flags))
Feng Tangee9b4502010-09-13 15:39:48 +0800434 max3110_con_receive(max);
Feng Tang22510992010-06-16 14:46:09 +0100435
436 /* first handle console output */
Arjan van de Vend6e679b2010-06-17 11:02:15 +0100437 if (test_and_clear_bit(CON_TX_NEEDED, &max->uart_flags))
Feng Tang22510992010-06-16 14:46:09 +0100438 send_circ_buf(max, xmit);
Feng Tang22510992010-06-16 14:46:09 +0100439
440 /* handle uart output */
Arjan van de Vend6e679b2010-06-17 11:02:15 +0100441 if (test_and_clear_bit(UART_TX_NEEDED, &max->uart_flags))
Feng Tang22510992010-06-16 14:46:09 +0100442 transmit_char(max);
Arjan van de Vend6e679b2010-06-17 11:02:15 +0100443
Arjan van de Ven68c16b42010-06-17 11:02:06 +0100444 mutex_unlock(&max->thread_mutex);
Arjan van de Vend6e679b2010-06-17 11:02:15 +0100445
Feng Tang22510992010-06-16 14:46:09 +0100446 } while (!kthread_should_stop());
447
448 return ret;
449}
450
Feng Tang22510992010-06-16 14:46:09 +0100451static irqreturn_t serial_m3110_irq(int irq, void *dev_id)
452{
453 struct uart_max3110 *max = dev_id;
454
455 /* max3110's irq is a falling edge, not level triggered,
456 * so no need to disable the irq */
Dirk Brandewie7b18bd52011-08-26 11:24:49 +0100457
Arjan van de Vend6e679b2010-06-17 11:02:15 +0100458 if (!test_and_set_bit(BIT_IRQ_PENDING, &max->uart_flags))
Dirk Brandewie7b18bd52011-08-26 11:24:49 +0100459 wake_up(&max->wq);
Arjan van de Vend6e679b2010-06-17 11:02:15 +0100460
Feng Tang22510992010-06-16 14:46:09 +0100461 return IRQ_HANDLED;
462}
Alan Cox91efa752010-09-13 15:39:56 +0800463
Feng Tang22510992010-06-16 14:46:09 +0100464/* if don't use RX IRQ, then need a thread to polling read */
465static int max3110_read_thread(void *_max)
466{
467 struct uart_max3110 *max = _max;
468
469 pr_info(PR_FMT "start read thread\n");
470 do {
Feng Tangee9b4502010-09-13 15:39:48 +0800471 /*
472 * If can't acquire the mutex, it means the main thread
473 * is running which will also perform the rx job
474 */
475 if (mutex_trylock(&max->thread_mutex)) {
476 max3110_con_receive(max);
477 mutex_unlock(&max->thread_mutex);
478 }
Feng Tang22510992010-06-16 14:46:09 +0100479
480 set_current_state(TASK_INTERRUPTIBLE);
481 schedule_timeout(HZ / 20);
482 } while (!kthread_should_stop());
483
484 return 0;
485}
Feng Tang22510992010-06-16 14:46:09 +0100486
487static int serial_m3110_startup(struct uart_port *port)
488{
489 struct uart_max3110 *max =
490 container_of(port, struct uart_max3110, port);
491 u16 config = 0;
492 int ret = 0;
493
Feng Tangee9b4502010-09-13 15:39:48 +0800494 if (port->line != 0) {
Feng Tang22510992010-06-16 14:46:09 +0100495 pr_err(PR_FMT "uart port startup failed\n");
Feng Tangee9b4502010-09-13 15:39:48 +0800496 return -1;
497 }
Feng Tang22510992010-06-16 14:46:09 +0100498
Feng Tangee9b4502010-09-13 15:39:48 +0800499 /* Disable all IRQ and config it to 115200, 8n1 */
Feng Tang22510992010-06-16 14:46:09 +0100500 config = WC_TAG | WC_FIFO_ENABLE
501 | WC_1_STOPBITS
502 | WC_8BIT_WORD
503 | WC_BAUD_DR2;
Feng Tang22510992010-06-16 14:46:09 +0100504
505 /* as we use thread to handle tx/rx, need set low latency */
506 port->state->port.tty->low_latency = 1;
507
Alan Cox91efa752010-09-13 15:39:56 +0800508 if (max->irq) {
Feng Tangee9b4502010-09-13 15:39:48 +0800509 max->read_thread = NULL;
Alan Cox91efa752010-09-13 15:39:56 +0800510 ret = request_irq(max->irq, serial_m3110_irq,
511 IRQ_TYPE_EDGE_FALLING, "max3110", max);
512 if (ret) {
513 max->irq = 0;
514 pr_err(PR_FMT "unable to allocate IRQ, polling\n");
515 } else {
516 /* Enable RX IRQ only */
517 config |= WC_RXA_IRQ_ENABLE;
518 }
Feng Tangee9b4502010-09-13 15:39:48 +0800519 }
Alan Cox91efa752010-09-13 15:39:56 +0800520
521 if (max->irq == 0) {
522 /* If IRQ is disabled, start a read thread for input data */
523 max->read_thread =
524 kthread_run(max3110_read_thread, max, "max3110_read");
525 if (IS_ERR(max->read_thread)) {
526 ret = PTR_ERR(max->read_thread);
527 max->read_thread = NULL;
528 pr_err(PR_FMT "Can't create read thread!\n");
529 return ret;
530 }
531 }
Feng Tang22510992010-06-16 14:46:09 +0100532
Feng Tangee9b4502010-09-13 15:39:48 +0800533 ret = max3110_out(max, config);
534 if (ret) {
Alan Cox91efa752010-09-13 15:39:56 +0800535 if (max->irq)
536 free_irq(max->irq, max);
537 if (max->read_thread)
538 kthread_stop(max->read_thread);
Feng Tangee9b4502010-09-13 15:39:48 +0800539 max->read_thread = NULL;
Feng Tangee9b4502010-09-13 15:39:48 +0800540 return ret;
541 }
542
Feng Tang22510992010-06-16 14:46:09 +0100543 max->cur_conf = config;
544 return 0;
545}
546
547static void serial_m3110_shutdown(struct uart_port *port)
548{
549 struct uart_max3110 *max =
550 container_of(port, struct uart_max3110, port);
551 u16 config;
552
553 if (max->read_thread) {
554 kthread_stop(max->read_thread);
555 max->read_thread = NULL;
556 }
557
Alan Cox91efa752010-09-13 15:39:56 +0800558 if (max->irq)
559 free_irq(max->irq, max);
Feng Tang22510992010-06-16 14:46:09 +0100560
561 /* Disable interrupts from this port */
562 config = WC_TAG | WC_SW_SHDI;
563 max3110_out(max, config);
564}
565
566static void serial_m3110_release_port(struct uart_port *port)
567{
568}
569
570static int serial_m3110_request_port(struct uart_port *port)
571{
572 return 0;
573}
574
575static void serial_m3110_config_port(struct uart_port *port, int flags)
576{
Feng Tangee9b4502010-09-13 15:39:48 +0800577 port->type = PORT_MAX3100;
Feng Tang22510992010-06-16 14:46:09 +0100578}
579
580static int
581serial_m3110_verify_port(struct uart_port *port, struct serial_struct *ser)
582{
583 /* we don't want the core code to modify any port params */
584 return -EINVAL;
585}
586
587
588static const char *serial_m3110_type(struct uart_port *port)
589{
590 struct uart_max3110 *max =
591 container_of(port, struct uart_max3110, port);
592 return max->name;
593}
594
595static void
596serial_m3110_set_termios(struct uart_port *port, struct ktermios *termios,
597 struct ktermios *old)
598{
599 struct uart_max3110 *max =
600 container_of(port, struct uart_max3110, port);
601 unsigned char cval;
602 unsigned int baud, parity = 0;
603 int clk_div = -1;
604 u16 new_conf = max->cur_conf;
605
606 switch (termios->c_cflag & CSIZE) {
607 case CS7:
608 cval = UART_LCR_WLEN7;
609 new_conf |= WC_7BIT_WORD;
610 break;
611 default:
Feng Tangee9b4502010-09-13 15:39:48 +0800612 /* We only support CS7 & CS8 */
613 termios->c_cflag &= ~CSIZE;
614 termios->c_cflag |= CS8;
Feng Tang22510992010-06-16 14:46:09 +0100615 case CS8:
616 cval = UART_LCR_WLEN8;
617 new_conf |= WC_8BIT_WORD;
618 break;
619 }
620
621 baud = uart_get_baud_rate(port, termios, old, 0, 230400);
622
Feng Tangee9b4502010-09-13 15:39:48 +0800623 /* First calc the div for 1.8MHZ clock case */
Feng Tang22510992010-06-16 14:46:09 +0100624 switch (baud) {
625 case 300:
626 clk_div = WC_BAUD_DR384;
627 break;
628 case 600:
629 clk_div = WC_BAUD_DR192;
630 break;
631 case 1200:
632 clk_div = WC_BAUD_DR96;
633 break;
634 case 2400:
635 clk_div = WC_BAUD_DR48;
636 break;
637 case 4800:
638 clk_div = WC_BAUD_DR24;
639 break;
640 case 9600:
641 clk_div = WC_BAUD_DR12;
642 break;
643 case 19200:
644 clk_div = WC_BAUD_DR6;
645 break;
646 case 38400:
647 clk_div = WC_BAUD_DR3;
648 break;
649 case 57600:
650 clk_div = WC_BAUD_DR2;
651 break;
652 case 115200:
653 clk_div = WC_BAUD_DR1;
654 break;
655 case 230400:
656 if (max->clock & MAX3110_HIGH_CLK)
657 break;
658 default:
Feng Tangee9b4502010-09-13 15:39:48 +0800659 /* Pick the previous baud rate */
Feng Tang22510992010-06-16 14:46:09 +0100660 baud = max->baud;
661 clk_div = max->cur_conf & WC_BAUD_DIV_MASK;
662 tty_termios_encode_baud_rate(termios, baud, baud);
663 }
664
665 if (max->clock & MAX3110_HIGH_CLK) {
666 clk_div += 1;
Feng Tangee9b4502010-09-13 15:39:48 +0800667 /* High clk version max3110 doesn't support B300 */
668 if (baud == 300) {
Feng Tang22510992010-06-16 14:46:09 +0100669 baud = 600;
Feng Tangee9b4502010-09-13 15:39:48 +0800670 clk_div = WC_BAUD_DR384;
671 }
Feng Tang22510992010-06-16 14:46:09 +0100672 if (baud == 230400)
673 clk_div = WC_BAUD_DR1;
674 tty_termios_encode_baud_rate(termios, baud, baud);
675 }
676
677 new_conf = (new_conf & ~WC_BAUD_DIV_MASK) | clk_div;
Feng Tangee9b4502010-09-13 15:39:48 +0800678
679 if (unlikely(termios->c_cflag & CMSPAR))
680 termios->c_cflag &= ~CMSPAR;
681
Feng Tang22510992010-06-16 14:46:09 +0100682 if (termios->c_cflag & CSTOPB)
683 new_conf |= WC_2_STOPBITS;
684 else
685 new_conf &= ~WC_2_STOPBITS;
686
687 if (termios->c_cflag & PARENB) {
688 new_conf |= WC_PARITY_ENABLE;
689 parity |= UART_LCR_PARITY;
690 } else
691 new_conf &= ~WC_PARITY_ENABLE;
692
693 if (!(termios->c_cflag & PARODD))
694 parity |= UART_LCR_EPAR;
695 max->parity = parity;
696
697 uart_update_timeout(port, termios->c_cflag, baud);
698
699 new_conf |= WC_TAG;
700 if (new_conf != max->cur_conf) {
Feng Tangee9b4502010-09-13 15:39:48 +0800701 if (!max3110_out(max, new_conf)) {
702 max->cur_conf = new_conf;
703 max->baud = baud;
704 }
Feng Tang22510992010-06-16 14:46:09 +0100705 }
706}
707
Feng Tangee9b4502010-09-13 15:39:48 +0800708/* Don't handle hw handshaking */
Feng Tang22510992010-06-16 14:46:09 +0100709static unsigned int serial_m3110_get_mctrl(struct uart_port *port)
710{
711 return TIOCM_DSR | TIOCM_CAR | TIOCM_DSR;
712}
713
714static void serial_m3110_set_mctrl(struct uart_port *port, unsigned int mctrl)
715{
716}
717
718static void serial_m3110_break_ctl(struct uart_port *port, int break_state)
719{
720}
721
722static void serial_m3110_pm(struct uart_port *port, unsigned int state,
723 unsigned int oldstate)
724{
725}
726
727static void serial_m3110_enable_ms(struct uart_port *port)
728{
729}
730
731struct uart_ops serial_m3110_ops = {
732 .tx_empty = serial_m3110_tx_empty,
733 .set_mctrl = serial_m3110_set_mctrl,
734 .get_mctrl = serial_m3110_get_mctrl,
735 .stop_tx = serial_m3110_stop_tx,
736 .start_tx = serial_m3110_start_tx,
737 .stop_rx = serial_m3110_stop_rx,
738 .enable_ms = serial_m3110_enable_ms,
739 .break_ctl = serial_m3110_break_ctl,
740 .startup = serial_m3110_startup,
741 .shutdown = serial_m3110_shutdown,
Feng Tangee9b4502010-09-13 15:39:48 +0800742 .set_termios = serial_m3110_set_termios,
Feng Tang22510992010-06-16 14:46:09 +0100743 .pm = serial_m3110_pm,
744 .type = serial_m3110_type,
745 .release_port = serial_m3110_release_port,
746 .request_port = serial_m3110_request_port,
747 .config_port = serial_m3110_config_port,
748 .verify_port = serial_m3110_verify_port,
749};
750
751static struct uart_driver serial_m3110_reg = {
752 .owner = THIS_MODULE,
753 .driver_name = "MRST serial",
754 .dev_name = "ttyS",
755 .major = TTY_MAJOR,
756 .minor = 64,
757 .nr = 1,
Feng Tangee9b4502010-09-13 15:39:48 +0800758 .cons = &serial_m3110_console,
Feng Tang22510992010-06-16 14:46:09 +0100759};
760
Feng Tangee9b4502010-09-13 15:39:48 +0800761#ifdef CONFIG_PM
Feng Tang22510992010-06-16 14:46:09 +0100762static int serial_m3110_suspend(struct spi_device *spi, pm_message_t state)
763{
Feng Tangee9b4502010-09-13 15:39:48 +0800764 struct uart_max3110 *max = spi_get_drvdata(spi);
765
766 disable_irq(max->irq);
767 uart_suspend_port(&serial_m3110_reg, &max->port);
768 max3110_out(max, max->cur_conf | WC_SW_SHDI);
Feng Tang22510992010-06-16 14:46:09 +0100769 return 0;
770}
771
772static int serial_m3110_resume(struct spi_device *spi)
773{
Feng Tangee9b4502010-09-13 15:39:48 +0800774 struct uart_max3110 *max = spi_get_drvdata(spi);
775
776 max3110_out(max, max->cur_conf);
777 uart_resume_port(&serial_m3110_reg, &max->port);
778 enable_irq(max->irq);
Feng Tang22510992010-06-16 14:46:09 +0100779 return 0;
780}
Feng Tangee9b4502010-09-13 15:39:48 +0800781#else
782#define serial_m3110_suspend NULL
783#define serial_m3110_resume NULL
784#endif
Feng Tang22510992010-06-16 14:46:09 +0100785
Feng Tangee9b4502010-09-13 15:39:48 +0800786static int __devinit serial_m3110_probe(struct spi_device *spi)
Feng Tang22510992010-06-16 14:46:09 +0100787{
788 struct uart_max3110 *max;
Feng Tangee9b4502010-09-13 15:39:48 +0800789 void *buffer;
jianwei.yang99dd3f62010-06-16 14:46:49 +0100790 u16 res;
Feng Tangee9b4502010-09-13 15:39:48 +0800791 int ret = 0;
792
Feng Tang22510992010-06-16 14:46:09 +0100793 max = kzalloc(sizeof(*max), GFP_KERNEL);
794 if (!max)
795 return -ENOMEM;
796
Feng Tangee9b4502010-09-13 15:39:48 +0800797 /* Set spi info */
Feng Tang22510992010-06-16 14:46:09 +0100798 spi->bits_per_word = 16;
799 max->clock = MAX3110_HIGH_CLK;
Feng Tang22510992010-06-16 14:46:09 +0100800
801 spi_setup(spi);
802
Feng Tangee9b4502010-09-13 15:39:48 +0800803 max->port.type = PORT_MAX3100;
804 max->port.fifosize = 2; /* Only have 16b buffer */
Feng Tang22510992010-06-16 14:46:09 +0100805 max->port.ops = &serial_m3110_ops;
806 max->port.line = 0;
807 max->port.dev = &spi->dev;
808 max->port.uartclk = 115200;
809
810 max->spi = spi;
Feng Tangee9b4502010-09-13 15:39:48 +0800811 strcpy(max->name, spi->modalias);
Feng Tang22510992010-06-16 14:46:09 +0100812 max->irq = (u16)spi->irq;
813
Arjan van de Ven68c16b42010-06-17 11:02:06 +0100814 mutex_init(&max->thread_mutex);
Feng Tang22510992010-06-16 14:46:09 +0100815
816 max->word_7bits = 0;
817 max->parity = 0;
818 max->baud = 0;
819
820 max->cur_conf = 0;
Arjan van de Vend6e679b2010-06-17 11:02:15 +0100821 max->uart_flags = 0;
822
jianwei.yang99dd3f62010-06-16 14:46:49 +0100823 /* Check if reading configuration register returns something sane */
Feng Tang22510992010-06-16 14:46:09 +0100824
jianwei.yang99dd3f62010-06-16 14:46:49 +0100825 res = RC_TAG;
826 ret = max3110_write_then_read(max, (u8 *)&res, (u8 *)&res, 2, 0);
827 if (ret < 0 || res == 0 || res == 0xffff) {
William Douglas0bb04bf2011-06-23 13:38:36 +0100828 dev_dbg(&spi->dev, "MAX3111 deemed not present (conf reg %04x)",
jianwei.yang99dd3f62010-06-16 14:46:49 +0100829 res);
830 ret = -ENODEV;
831 goto err_get_page;
832 }
Feng Tangee9b4502010-09-13 15:39:48 +0800833
834 buffer = (void *)__get_free_page(GFP_KERNEL);
Feng Tang22510992010-06-16 14:46:09 +0100835 if (!buffer) {
836 ret = -ENOMEM;
837 goto err_get_page;
838 }
Feng Tangee9b4502010-09-13 15:39:48 +0800839 max->con_xmit.buf = buffer;
840 max->con_xmit.head = 0;
841 max->con_xmit.tail = 0;
Feng Tang22510992010-06-16 14:46:09 +0100842
Mika Westerberg33b1e692011-06-23 13:39:00 +0100843 init_waitqueue_head(&max->wq);
844
Feng Tang22510992010-06-16 14:46:09 +0100845 max->main_thread = kthread_run(max3110_main_thread,
846 max, "max3110_main");
847 if (IS_ERR(max->main_thread)) {
848 ret = PTR_ERR(max->main_thread);
849 goto err_kthread;
850 }
851
Feng Tangee9b4502010-09-13 15:39:48 +0800852 spi_set_drvdata(spi, max);
Feng Tang22510992010-06-16 14:46:09 +0100853 pmax = max;
Feng Tangee9b4502010-09-13 15:39:48 +0800854
855 /* Give membase a psudo value to pass serial_core's check */
Feng Tang22510992010-06-16 14:46:09 +0100856 max->port.membase = (void *)0xff110000;
857 uart_add_one_port(&serial_m3110_reg, &max->port);
858
859 return 0;
860
861err_kthread:
862 free_page((unsigned long)buffer);
863err_get_page:
Feng Tang22510992010-06-16 14:46:09 +0100864 kfree(max);
865 return ret;
866}
867
Feng Tangee9b4502010-09-13 15:39:48 +0800868static int __devexit serial_m3110_remove(struct spi_device *dev)
Feng Tang22510992010-06-16 14:46:09 +0100869{
Feng Tangee9b4502010-09-13 15:39:48 +0800870 struct uart_max3110 *max = spi_get_drvdata(dev);
Feng Tang22510992010-06-16 14:46:09 +0100871
Feng Tangee9b4502010-09-13 15:39:48 +0800872 if (!max)
Feng Tang22510992010-06-16 14:46:09 +0100873 return 0;
874
Feng Tang22510992010-06-16 14:46:09 +0100875 uart_remove_one_port(&serial_m3110_reg, &max->port);
876
877 free_page((unsigned long)max->con_xmit.buf);
878
879 if (max->main_thread)
880 kthread_stop(max->main_thread);
881
882 kfree(max);
883 return 0;
884}
885
886static struct spi_driver uart_max3110_driver = {
887 .driver = {
888 .name = "spi_max3111",
889 .bus = &spi_bus_type,
890 .owner = THIS_MODULE,
891 },
892 .probe = serial_m3110_probe,
Feng Tangee9b4502010-09-13 15:39:48 +0800893 .remove = __devexit_p(serial_m3110_remove),
Feng Tang22510992010-06-16 14:46:09 +0100894 .suspend = serial_m3110_suspend,
895 .resume = serial_m3110_resume,
896};
897
Feng Tangee9b4502010-09-13 15:39:48 +0800898static int __init serial_m3110_init(void)
Feng Tang22510992010-06-16 14:46:09 +0100899{
900 int ret = 0;
901
902 ret = uart_register_driver(&serial_m3110_reg);
903 if (ret)
904 return ret;
905
906 ret = spi_register_driver(&uart_max3110_driver);
907 if (ret)
908 uart_unregister_driver(&serial_m3110_reg);
909
910 return ret;
911}
912
Feng Tangee9b4502010-09-13 15:39:48 +0800913static void __exit serial_m3110_exit(void)
Feng Tang22510992010-06-16 14:46:09 +0100914{
915 spi_unregister_driver(&uart_max3110_driver);
916 uart_unregister_driver(&serial_m3110_reg);
917}
918
919module_init(serial_m3110_init);
920module_exit(serial_m3110_exit);
921
Feng Tangee9b4502010-09-13 15:39:48 +0800922MODULE_LICENSE("GPL v2");
Feng Tang22510992010-06-16 14:46:09 +0100923MODULE_ALIAS("max3110-uart");