2 * mrst_max3110.c - spi uart protocol driver for Maxim 3110
4 * Copyright (c) 2008-2010, Intel Corporation.
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.
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
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.
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
26 * 2. Currently only RX availabe interrrupt is used, no need for waiting TXE
27 * interrupt for a low speed UART device
30 #include <linux/module.h>
31 #include <linux/ioport.h>
32 #include <linux/irq.h>
33 #include <linux/init.h>
34 #include <linux/console.h>
35 #include <linux/tty.h>
36 #include <linux/tty_flip.h>
37 #include <linux/serial_core.h>
38 #include <linux/serial_reg.h>
40 #include <linux/kthread.h>
41 #include <linux/spi/spi.h>
43 #include "mrst_max3110.h"
45 #define PR_FMT "mrst_max3110: "
47 #define UART_TX_NEEDED 1
48 #define CON_TX_NEEDED 2
49 #define BIT_IRQ_PENDING 3
52 struct uart_port port
;
53 struct spi_device
*spi
;
54 char name
[SPI_NAME_SIZE
];
57 struct task_struct
*main_thread
;
58 struct task_struct
*read_thread
;
59 struct mutex thread_mutex
;;
64 u8 parity
, word_7bits
;
67 unsigned long uart_flags
;
70 struct circ_buf con_xmit
;
73 /* global data structure, may need be removed */
74 static struct uart_max3110
*pmax
;
76 static void receive_chars(struct uart_max3110
*max
,
77 unsigned char *str
, int len
);
78 static int max3110_read_multi(struct uart_max3110
*max
, u8
*buf
);
79 static void max3110_con_receive(struct uart_max3110
*max
);
81 static int max3110_write_then_read(struct uart_max3110
*max
,
82 const void *txbuf
, void *rxbuf
, unsigned len
, int always_fast
)
84 struct spi_device
*spi
= max
->spi
;
85 struct spi_message message
;
86 struct spi_transfer x
;
89 spi_message_init(&message
);
90 memset(&x
, 0, sizeof x
);
94 spi_message_add_tail(&x
, &message
);
97 x
.speed_hz
= spi
->max_speed_hz
;
99 x
.speed_hz
= max
->baud
;
102 ret
= spi_sync(spi
, &message
);
106 /* Write a 16b word to the device */
107 static int max3110_out(struct uart_max3110
*max
, const u16 out
)
114 buf
= kzalloc(8, GFP_KERNEL
| GFP_DMA
);
121 ret
= max3110_write_then_read(max
, obuf
, ibuf
, 2, 1);
123 pr_warning(PR_FMT
"%s(): get err msg %d when sending 0x%x\n",
128 /* If some valid data is read back */
129 if (*ibuf
& MAX3110_READ_DATA_AVAILABLE
) {
131 receive_chars(max
, &ch
, 1);
140 * This is usually used to read data from SPIC RX FIFO, which doesn't
141 * need any delay like flushing character out.
143 * Return how many valide bytes are read back
145 static int max3110_read_multi(struct uart_max3110
*max
, u8
*rxbuf
)
149 u8
*pbuf
, valid_str
[M3110_RX_FIFO_DEPTH
];
152 blen
= M3110_RX_FIFO_DEPTH
* sizeof(u16
);
153 buf
= kzalloc(blen
* 2, GFP_KERNEL
| GFP_DMA
);
155 pr_warning(PR_FMT
"%s(): fail to alloc dma buffer\n", __func__
);
159 /* tx/rx always have the same length */
163 if (max3110_write_then_read(max
, obuf
, ibuf
, blen
, 1)) {
168 /* If caller doesn't provide a buffer, then handle received char */
169 pbuf
= rxbuf
? rxbuf
: valid_str
;
171 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;
176 if (j
&& (pbuf
== valid_str
))
177 receive_chars(max
, valid_str
, j
);
183 static void serial_m3110_con_putchar(struct uart_port
*port
, int ch
)
185 struct uart_max3110
*max
=
186 container_of(port
, struct uart_max3110
, port
);
187 struct circ_buf
*xmit
= &max
->con_xmit
;
189 if (uart_circ_chars_free(xmit
)) {
190 xmit
->buf
[xmit
->head
] = (char)ch
;
191 xmit
->head
= (xmit
->head
+ 1) & (PAGE_SIZE
- 1);
196 * Print a string to the serial port trying not to disturb
197 * any possible real use of the port...
199 * The console_lock must be held when we get here.
201 static void serial_m3110_con_write(struct console
*co
,
202 const char *s
, unsigned int count
)
207 uart_console_write(&pmax
->port
, s
, count
, serial_m3110_con_putchar
);
209 if (!test_and_set_bit(CON_TX_NEEDED
, &pmax
->uart_flags
))
210 wake_up_process(pmax
->main_thread
);
214 serial_m3110_con_setup(struct console
*co
, char *options
)
216 struct uart_max3110
*max
= pmax
;
222 pr_info(PR_FMT
"setting up console\n");
228 pr_err(PR_FMT
"pmax is NULL, return");
233 uart_parse_options(options
, &baud
, &parity
, &bits
, &flow
);
235 return uart_set_options(&max
->port
, co
, baud
, parity
, bits
, flow
);
238 static struct tty_driver
*serial_m3110_con_device(struct console
*co
,
241 struct uart_driver
*p
= co
->data
;
243 return p
->tty_driver
;
246 static struct uart_driver serial_m3110_reg
;
247 static struct console serial_m3110_console
= {
249 .write
= serial_m3110_con_write
,
250 .device
= serial_m3110_con_device
,
251 .setup
= serial_m3110_con_setup
,
252 .flags
= CON_PRINTBUFFER
,
254 .data
= &serial_m3110_reg
,
257 static unsigned int serial_m3110_tx_empty(struct uart_port
*port
)
262 static void serial_m3110_stop_tx(struct uart_port
*port
)
267 /* stop_rx will be called in spin_lock env */
268 static void serial_m3110_stop_rx(struct uart_port
*port
)
273 #define WORDS_PER_XFER 128
274 static void send_circ_buf(struct uart_max3110
*max
,
275 struct circ_buf
*xmit
)
279 u8 valid_str
[WORDS_PER_XFER
];
280 int i
, j
, len
, blen
, dma_size
, left
, ret
= 0;
283 dma_size
= WORDS_PER_XFER
* sizeof(u16
) * 2;
284 buf
= kzalloc(dma_size
, GFP_KERNEL
| GFP_DMA
);
288 ibuf
= buf
+ dma_size
/2;
290 while (!uart_circ_empty(xmit
)) {
291 left
= uart_circ_chars_pending(xmit
);
293 len
= min(left
, WORDS_PER_XFER
);
294 blen
= len
* sizeof(u16
);
295 memset(ibuf
, 0, blen
);
297 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);
303 /* Fail to send msg to console is not very critical */
304 ret
= max3110_write_then_read(max
, obuf
, ibuf
, blen
, 0);
306 pr_warning(PR_FMT
"%s(): get err msg %d\n",
309 for (i
= 0, j
= 0; i
< len
; i
++) {
310 if (ibuf
[i
] & MAX3110_READ_DATA_AVAILABLE
)
311 valid_str
[j
++] = ibuf
[i
] & 0xff;
315 receive_chars(max
, valid_str
, j
);
317 max
->port
.icount
.tx
+= len
;
325 static void transmit_char(struct uart_max3110
*max
)
327 struct uart_port
*port
= &max
->port
;
328 struct circ_buf
*xmit
= &port
->state
->xmit
;
330 if (uart_circ_empty(xmit
) || uart_tx_stopped(port
))
333 send_circ_buf(max
, xmit
);
335 if (uart_circ_chars_pending(xmit
) < WAKEUP_CHARS
)
336 uart_write_wakeup(port
);
338 if (uart_circ_empty(xmit
))
339 serial_m3110_stop_tx(port
);
343 * This will be called by uart_write() and tty_write, can't
346 static void serial_m3110_start_tx(struct uart_port
*port
)
348 struct uart_max3110
*max
=
349 container_of(port
, struct uart_max3110
, port
);
351 if (!test_and_set_bit(UART_TX_NEEDED
, &max
->uart_flags
))
352 wake_up_process(max
->main_thread
);
355 static void receive_chars(struct uart_max3110
*max
, unsigned char *str
, int len
)
357 struct uart_port
*port
= &max
->port
;
358 struct tty_struct
*tty
;
361 /* If uart is not opened, just return */
365 tty
= port
->state
->port
.tty
;
370 usable
= tty_buffer_request_room(tty
, len
);
372 tty_insert_flip_string(tty
, str
, usable
);
374 port
->icount
.rx
+= usable
;
378 tty_flip_buffer_push(tty
);
382 * This routine will be used in read_thread or RX IRQ handling,
383 * it will first do one round buffer read(8 words), if there is some
384 * valid RX data, will try to read 5 more rounds till all data
387 * Use stack space as data buffer to save some system load, and chose
388 * 504 Btyes as a threadhold to do a bulk push to upper tty layer when
389 * receiving bulk data, a much bigger buffer may cause stack overflow
391 static void max3110_con_receive(struct uart_max3110
*max
)
393 int loop
= 1, num
, total
= 0;
394 u8 recv_buf
[512], *pbuf
;
398 num
= max3110_read_multi(max
, pbuf
);
406 receive_chars(max
, recv_buf
, total
);
414 receive_chars(max
, recv_buf
, total
);
417 static int max3110_main_thread(void *_max
)
419 struct uart_max3110
*max
= _max
;
420 wait_queue_head_t
*wq
= &max
->wq
;
422 struct circ_buf
*xmit
= &max
->con_xmit
;
424 init_waitqueue_head(wq
);
425 pr_info(PR_FMT
"start main thread\n");
428 wait_event_interruptible(*wq
, max
->uart_flags
|| kthread_should_stop());
430 mutex_lock(&max
->thread_mutex
);
432 if (test_and_clear_bit(BIT_IRQ_PENDING
, &max
->uart_flags
))
433 max3110_con_receive(max
);
435 /* first handle console output */
436 if (test_and_clear_bit(CON_TX_NEEDED
, &max
->uart_flags
))
437 send_circ_buf(max
, xmit
);
439 /* handle uart output */
440 if (test_and_clear_bit(UART_TX_NEEDED
, &max
->uart_flags
))
443 mutex_unlock(&max
->thread_mutex
);
445 } while (!kthread_should_stop());
450 static irqreturn_t
serial_m3110_irq(int irq
, void *dev_id
)
452 struct uart_max3110
*max
= dev_id
;
454 /* max3110's irq is a falling edge, not level triggered,
455 * so no need to disable the irq */
456 if (!test_and_set_bit(BIT_IRQ_PENDING
, &max
->uart_flags
))
457 wake_up_process(max
->main_thread
);
462 /* if don't use RX IRQ, then need a thread to polling read */
463 static int max3110_read_thread(void *_max
)
465 struct uart_max3110
*max
= _max
;
467 pr_info(PR_FMT
"start read thread\n");
470 * If can't acquire the mutex, it means the main thread
471 * is running which will also perform the rx job
473 if (mutex_trylock(&max
->thread_mutex
)) {
474 max3110_con_receive(max
);
475 mutex_unlock(&max
->thread_mutex
);
478 set_current_state(TASK_INTERRUPTIBLE
);
479 schedule_timeout(HZ
/ 20);
480 } while (!kthread_should_stop());
485 static int serial_m3110_startup(struct uart_port
*port
)
487 struct uart_max3110
*max
=
488 container_of(port
, struct uart_max3110
, port
);
492 if (port
->line
!= 0) {
493 pr_err(PR_FMT
"uart port startup failed\n");
497 /* Disable all IRQ and config it to 115200, 8n1 */
498 config
= WC_TAG
| WC_FIFO_ENABLE
503 /* as we use thread to handle tx/rx, need set low latency */
504 port
->state
->port
.tty
->low_latency
= 1;
507 max
->read_thread
= NULL
;
508 ret
= request_irq(max
->irq
, serial_m3110_irq
,
509 IRQ_TYPE_EDGE_FALLING
, "max3110", max
);
512 pr_err(PR_FMT
"unable to allocate IRQ, polling\n");
514 /* Enable RX IRQ only */
515 config
|= WC_RXA_IRQ_ENABLE
;
520 /* If IRQ is disabled, start a read thread for input data */
522 kthread_run(max3110_read_thread
, max
, "max3110_read");
523 if (IS_ERR(max
->read_thread
)) {
524 ret
= PTR_ERR(max
->read_thread
);
525 max
->read_thread
= NULL
;
526 pr_err(PR_FMT
"Can't create read thread!\n");
531 ret
= max3110_out(max
, config
);
534 free_irq(max
->irq
, max
);
535 if (max
->read_thread
)
536 kthread_stop(max
->read_thread
);
537 max
->read_thread
= NULL
;
541 max
->cur_conf
= config
;
545 static void serial_m3110_shutdown(struct uart_port
*port
)
547 struct uart_max3110
*max
=
548 container_of(port
, struct uart_max3110
, port
);
551 if (max
->read_thread
) {
552 kthread_stop(max
->read_thread
);
553 max
->read_thread
= NULL
;
557 free_irq(max
->irq
, max
);
559 /* Disable interrupts from this port */
560 config
= WC_TAG
| WC_SW_SHDI
;
561 max3110_out(max
, config
);
564 static void serial_m3110_release_port(struct uart_port
*port
)
568 static int serial_m3110_request_port(struct uart_port
*port
)
573 static void serial_m3110_config_port(struct uart_port
*port
, int flags
)
575 port
->type
= PORT_MAX3100
;
579 serial_m3110_verify_port(struct uart_port
*port
, struct serial_struct
*ser
)
581 /* we don't want the core code to modify any port params */
586 static const char *serial_m3110_type(struct uart_port
*port
)
588 struct uart_max3110
*max
=
589 container_of(port
, struct uart_max3110
, port
);
594 serial_m3110_set_termios(struct uart_port
*port
, struct ktermios
*termios
,
595 struct ktermios
*old
)
597 struct uart_max3110
*max
=
598 container_of(port
, struct uart_max3110
, port
);
600 unsigned int baud
, parity
= 0;
602 u16 new_conf
= max
->cur_conf
;
604 switch (termios
->c_cflag
& CSIZE
) {
606 cval
= UART_LCR_WLEN7
;
607 new_conf
|= WC_7BIT_WORD
;
610 /* We only support CS7 & CS8 */
611 termios
->c_cflag
&= ~CSIZE
;
612 termios
->c_cflag
|= CS8
;
614 cval
= UART_LCR_WLEN8
;
615 new_conf
|= WC_8BIT_WORD
;
619 baud
= uart_get_baud_rate(port
, termios
, old
, 0, 230400);
621 /* First calc the div for 1.8MHZ clock case */
624 clk_div
= WC_BAUD_DR384
;
627 clk_div
= WC_BAUD_DR192
;
630 clk_div
= WC_BAUD_DR96
;
633 clk_div
= WC_BAUD_DR48
;
636 clk_div
= WC_BAUD_DR24
;
639 clk_div
= WC_BAUD_DR12
;
642 clk_div
= WC_BAUD_DR6
;
645 clk_div
= WC_BAUD_DR3
;
648 clk_div
= WC_BAUD_DR2
;
651 clk_div
= WC_BAUD_DR1
;
654 if (max
->clock
& MAX3110_HIGH_CLK
)
657 /* Pick the previous baud rate */
659 clk_div
= max
->cur_conf
& WC_BAUD_DIV_MASK
;
660 tty_termios_encode_baud_rate(termios
, baud
, baud
);
663 if (max
->clock
& MAX3110_HIGH_CLK
) {
665 /* High clk version max3110 doesn't support B300 */
668 clk_div
= WC_BAUD_DR384
;
671 clk_div
= WC_BAUD_DR1
;
672 tty_termios_encode_baud_rate(termios
, baud
, baud
);
675 new_conf
= (new_conf
& ~WC_BAUD_DIV_MASK
) | clk_div
;
677 if (unlikely(termios
->c_cflag
& CMSPAR
))
678 termios
->c_cflag
&= ~CMSPAR
;
680 if (termios
->c_cflag
& CSTOPB
)
681 new_conf
|= WC_2_STOPBITS
;
683 new_conf
&= ~WC_2_STOPBITS
;
685 if (termios
->c_cflag
& PARENB
) {
686 new_conf
|= WC_PARITY_ENABLE
;
687 parity
|= UART_LCR_PARITY
;
689 new_conf
&= ~WC_PARITY_ENABLE
;
691 if (!(termios
->c_cflag
& PARODD
))
692 parity
|= UART_LCR_EPAR
;
693 max
->parity
= parity
;
695 uart_update_timeout(port
, termios
->c_cflag
, baud
);
698 if (new_conf
!= max
->cur_conf
) {
699 if (!max3110_out(max
, new_conf
)) {
700 max
->cur_conf
= new_conf
;
706 /* Don't handle hw handshaking */
707 static unsigned int serial_m3110_get_mctrl(struct uart_port
*port
)
709 return TIOCM_DSR
| TIOCM_CAR
| TIOCM_DSR
;
712 static void serial_m3110_set_mctrl(struct uart_port
*port
, unsigned int mctrl
)
716 static void serial_m3110_break_ctl(struct uart_port
*port
, int break_state
)
720 static void serial_m3110_pm(struct uart_port
*port
, unsigned int state
,
721 unsigned int oldstate
)
725 static void serial_m3110_enable_ms(struct uart_port
*port
)
729 struct uart_ops serial_m3110_ops
= {
730 .tx_empty
= serial_m3110_tx_empty
,
731 .set_mctrl
= serial_m3110_set_mctrl
,
732 .get_mctrl
= serial_m3110_get_mctrl
,
733 .stop_tx
= serial_m3110_stop_tx
,
734 .start_tx
= serial_m3110_start_tx
,
735 .stop_rx
= serial_m3110_stop_rx
,
736 .enable_ms
= serial_m3110_enable_ms
,
737 .break_ctl
= serial_m3110_break_ctl
,
738 .startup
= serial_m3110_startup
,
739 .shutdown
= serial_m3110_shutdown
,
740 .set_termios
= serial_m3110_set_termios
,
741 .pm
= serial_m3110_pm
,
742 .type
= serial_m3110_type
,
743 .release_port
= serial_m3110_release_port
,
744 .request_port
= serial_m3110_request_port
,
745 .config_port
= serial_m3110_config_port
,
746 .verify_port
= serial_m3110_verify_port
,
749 static struct uart_driver serial_m3110_reg
= {
750 .owner
= THIS_MODULE
,
751 .driver_name
= "MRST serial",
756 .cons
= &serial_m3110_console
,
760 static int serial_m3110_suspend(struct spi_device
*spi
, pm_message_t state
)
762 struct uart_max3110
*max
= spi_get_drvdata(spi
);
764 disable_irq(max
->irq
);
765 uart_suspend_port(&serial_m3110_reg
, &max
->port
);
766 max3110_out(max
, max
->cur_conf
| WC_SW_SHDI
);
770 static int serial_m3110_resume(struct spi_device
*spi
)
772 struct uart_max3110
*max
= spi_get_drvdata(spi
);
774 max3110_out(max
, max
->cur_conf
);
775 uart_resume_port(&serial_m3110_reg
, &max
->port
);
776 enable_irq(max
->irq
);
780 #define serial_m3110_suspend NULL
781 #define serial_m3110_resume NULL
784 static int __devinit
serial_m3110_probe(struct spi_device
*spi
)
786 struct uart_max3110
*max
;
791 max
= kzalloc(sizeof(*max
), GFP_KERNEL
);
796 spi
->bits_per_word
= 16;
797 max
->clock
= MAX3110_HIGH_CLK
;
801 max
->port
.type
= PORT_MAX3100
;
802 max
->port
.fifosize
= 2; /* Only have 16b buffer */
803 max
->port
.ops
= &serial_m3110_ops
;
805 max
->port
.dev
= &spi
->dev
;
806 max
->port
.uartclk
= 115200;
809 strcpy(max
->name
, spi
->modalias
);
810 max
->irq
= (u16
)spi
->irq
;
812 mutex_init(&max
->thread_mutex
);
821 /* Check if reading configuration register returns something sane */
824 ret
= max3110_write_then_read(max
, (u8
*)&res
, (u8
*)&res
, 2, 0);
825 if (ret
< 0 || res
== 0 || res
== 0xffff) {
826 printk(KERN_ERR
"MAX3111 deemed not present (conf reg %04x)",
832 buffer
= (void *)__get_free_page(GFP_KERNEL
);
837 max
->con_xmit
.buf
= buffer
;
838 max
->con_xmit
.head
= 0;
839 max
->con_xmit
.tail
= 0;
841 max
->main_thread
= kthread_run(max3110_main_thread
,
842 max
, "max3110_main");
843 if (IS_ERR(max
->main_thread
)) {
844 ret
= PTR_ERR(max
->main_thread
);
848 spi_set_drvdata(spi
, max
);
851 /* Give membase a psudo value to pass serial_core's check */
852 max
->port
.membase
= (void *)0xff110000;
853 uart_add_one_port(&serial_m3110_reg
, &max
->port
);
858 free_page((unsigned long)buffer
);
864 static int __devexit
serial_m3110_remove(struct spi_device
*dev
)
866 struct uart_max3110
*max
= spi_get_drvdata(dev
);
871 uart_remove_one_port(&serial_m3110_reg
, &max
->port
);
873 free_page((unsigned long)max
->con_xmit
.buf
);
875 if (max
->main_thread
)
876 kthread_stop(max
->main_thread
);
882 static struct spi_driver uart_max3110_driver
= {
884 .name
= "spi_max3111",
885 .bus
= &spi_bus_type
,
886 .owner
= THIS_MODULE
,
888 .probe
= serial_m3110_probe
,
889 .remove
= __devexit_p(serial_m3110_remove
),
890 .suspend
= serial_m3110_suspend
,
891 .resume
= serial_m3110_resume
,
894 static int __init
serial_m3110_init(void)
898 ret
= uart_register_driver(&serial_m3110_reg
);
902 ret
= spi_register_driver(&uart_max3110_driver
);
904 uart_unregister_driver(&serial_m3110_reg
);
909 static void __exit
serial_m3110_exit(void)
911 spi_unregister_driver(&uart_max3110_driver
);
912 uart_unregister_driver(&serial_m3110_reg
);
915 module_init(serial_m3110_init
);
916 module_exit(serial_m3110_exit
);
918 MODULE_LICENSE("GPL v2");
919 MODULE_ALIAS("max3110-uart");