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