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 available interrupt is used, no need for waiting TXE
27 * interrupt for a low speed UART device
30 #ifdef CONFIG_MAGIC_SYSRQ
34 #include <linux/module.h>
35 #include <linux/ioport.h>
36 #include <linux/irq.h>
37 #include <linux/init.h>
38 #include <linux/console.h>
39 #include <linux/tty.h>
40 #include <linux/tty_flip.h>
41 #include <linux/serial_core.h>
42 #include <linux/serial_reg.h>
44 #include <linux/kthread.h>
45 #include <linux/spi/spi.h>
47 #include "mrst_max3110.h"
49 #define PR_FMT "mrst_max3110: "
51 #define UART_TX_NEEDED 1
52 #define CON_TX_NEEDED 2
53 #define BIT_IRQ_PENDING 3
56 struct uart_port port
;
57 struct spi_device
*spi
;
58 char name
[SPI_NAME_SIZE
];
61 struct task_struct
*main_thread
;
62 struct task_struct
*read_thread
;
63 struct mutex thread_mutex
;
68 u8 parity
, word_7bits
;
71 unsigned long uart_flags
;
74 struct circ_buf con_xmit
;
77 /* global data structure, may need be removed */
78 static struct uart_max3110
*pmax
;
80 static int receive_chars(struct uart_max3110
*max
,
81 unsigned short *str
, int len
);
82 static int max3110_read_multi(struct uart_max3110
*max
);
83 static void max3110_con_receive(struct uart_max3110
*max
);
85 static int max3110_write_then_read(struct uart_max3110
*max
,
86 const void *txbuf
, void *rxbuf
, unsigned len
, int always_fast
)
88 struct spi_device
*spi
= max
->spi
;
89 struct spi_message message
;
90 struct spi_transfer x
;
93 spi_message_init(&message
);
94 memset(&x
, 0, sizeof x
);
98 spi_message_add_tail(&x
, &message
);
101 x
.speed_hz
= spi
->max_speed_hz
;
103 x
.speed_hz
= max
->baud
;
106 ret
= spi_sync(spi
, &message
);
110 /* Write a 16b word to the device */
111 static int max3110_out(struct uart_max3110
*max
, const u16 out
)
117 buf
= kzalloc(8, GFP_KERNEL
| GFP_DMA
);
124 ret
= max3110_write_then_read(max
, obuf
, ibuf
, 2, 1);
126 pr_warning(PR_FMT
"%s(): get err msg %d when sending 0x%x\n",
131 receive_chars(max
, ibuf
, 1);
139 * This is usually used to read data from SPIC RX FIFO, which doesn't
140 * need any delay like flushing character out.
142 * Return how many valide bytes are read back
144 static int max3110_read_multi(struct uart_max3110
*max
)
150 blen
= M3110_RX_FIFO_DEPTH
* sizeof(u16
);
151 buf
= kzalloc(blen
* 2, GFP_KERNEL
| GFP_DMA
);
153 pr_warning(PR_FMT
"%s(): fail to alloc dma buffer\n", __func__
);
157 /* tx/rx always have the same length */
161 if (max3110_write_then_read(max
, obuf
, ibuf
, blen
, 1)) {
166 ret
= receive_chars(max
, ibuf
, M3110_RX_FIFO_DEPTH
);
172 static void serial_m3110_con_putchar(struct uart_port
*port
, int ch
)
174 struct uart_max3110
*max
=
175 container_of(port
, struct uart_max3110
, port
);
176 struct circ_buf
*xmit
= &max
->con_xmit
;
178 if (uart_circ_chars_free(xmit
)) {
179 xmit
->buf
[xmit
->head
] = (char)ch
;
180 xmit
->head
= (xmit
->head
+ 1) & (PAGE_SIZE
- 1);
185 * Print a string to the serial port trying not to disturb
186 * any possible real use of the port...
188 * The console_lock must be held when we get here.
190 static void serial_m3110_con_write(struct console
*co
,
191 const char *s
, unsigned int count
)
196 uart_console_write(&pmax
->port
, s
, count
, serial_m3110_con_putchar
);
198 if (!test_and_set_bit(CON_TX_NEEDED
, &pmax
->uart_flags
))
203 serial_m3110_con_setup(struct console
*co
, char *options
)
205 struct uart_max3110
*max
= pmax
;
211 pr_info(PR_FMT
"setting up console\n");
217 pr_err(PR_FMT
"pmax is NULL, return");
222 uart_parse_options(options
, &baud
, &parity
, &bits
, &flow
);
224 return uart_set_options(&max
->port
, co
, baud
, parity
, bits
, flow
);
227 static struct tty_driver
*serial_m3110_con_device(struct console
*co
,
230 struct uart_driver
*p
= co
->data
;
232 return p
->tty_driver
;
235 static struct uart_driver serial_m3110_reg
;
236 static struct console serial_m3110_console
= {
238 .write
= serial_m3110_con_write
,
239 .device
= serial_m3110_con_device
,
240 .setup
= serial_m3110_con_setup
,
241 .flags
= CON_PRINTBUFFER
,
243 .data
= &serial_m3110_reg
,
246 static unsigned int serial_m3110_tx_empty(struct uart_port
*port
)
251 static void serial_m3110_stop_tx(struct uart_port
*port
)
256 /* stop_rx will be called in spin_lock env */
257 static void serial_m3110_stop_rx(struct uart_port
*port
)
262 #define WORDS_PER_XFER 128
263 static void send_circ_buf(struct uart_max3110
*max
,
264 struct circ_buf
*xmit
)
268 int i
, len
, blen
, dma_size
, left
, ret
= 0;
271 dma_size
= WORDS_PER_XFER
* sizeof(u16
) * 2;
272 buf
= kzalloc(dma_size
, GFP_KERNEL
| GFP_DMA
);
276 ibuf
= buf
+ dma_size
/2;
278 while (!uart_circ_empty(xmit
)) {
279 left
= uart_circ_chars_pending(xmit
);
281 len
= min(left
, WORDS_PER_XFER
);
282 blen
= len
* sizeof(u16
);
283 memset(ibuf
, 0, blen
);
285 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);
291 /* Fail to send msg to console is not very critical */
293 ret
= max3110_write_then_read(max
, obuf
, ibuf
, blen
, 0);
295 pr_warning(PR_FMT
"%s(): get err msg %d\n",
298 receive_chars(max
, ibuf
, len
);
300 max
->port
.icount
.tx
+= len
;
308 static void transmit_char(struct uart_max3110
*max
)
310 struct uart_port
*port
= &max
->port
;
311 struct circ_buf
*xmit
= &port
->state
->xmit
;
313 if (uart_circ_empty(xmit
) || uart_tx_stopped(port
))
316 send_circ_buf(max
, xmit
);
318 if (uart_circ_chars_pending(xmit
) < WAKEUP_CHARS
)
319 uart_write_wakeup(port
);
321 if (uart_circ_empty(xmit
))
322 serial_m3110_stop_tx(port
);
326 * This will be called by uart_write() and tty_write, can't
329 static void serial_m3110_start_tx(struct uart_port
*port
)
331 struct uart_max3110
*max
=
332 container_of(port
, struct uart_max3110
, port
);
334 if (!test_and_set_bit(UART_TX_NEEDED
, &max
->uart_flags
))
339 receive_chars(struct uart_max3110
*max
, unsigned short *str
, int len
)
341 struct uart_port
*port
= &max
->port
;
342 struct tty_port
*tport
;
343 char buf
[M3110_RX_FIFO_DEPTH
];
346 /* If uart is not opened, just return */
350 tport
= &port
->state
->port
;
352 for (r
= 0, w
= 0; r
< len
; r
++) {
353 if (str
[r
] & MAX3110_BREAK
&&
354 uart_handle_break(port
))
357 if (str
[r
] & MAX3110_READ_DATA_AVAILABLE
) {
358 if (uart_handle_sysrq_char(port
, str
[r
] & 0xff))
361 buf
[w
++] = str
[r
] & 0xff;
368 for (r
= 0; w
; r
+= usable
, w
-= usable
) {
369 usable
= tty_buffer_request_room(tport
, w
);
371 tty_insert_flip_string(tport
, buf
+ r
, usable
);
372 port
->icount
.rx
+= usable
;
375 tty_flip_buffer_push(tport
);
381 * This routine will be used in read_thread or RX IRQ handling,
382 * it will first do one round buffer read(8 words), if there is some
383 * valid RX data, will try to read 5 more rounds till all data
386 * Use stack space as data buffer to save some system load, and chose
387 * 504 Btyes as a threadhold to do a bulk push to upper tty layer when
388 * receiving bulk data, a much bigger buffer may cause stack overflow
390 static void max3110_con_receive(struct uart_max3110
*max
)
395 num
= max3110_read_multi(max
);
403 static int max3110_main_thread(void *_max
)
405 struct uart_max3110
*max
= _max
;
406 wait_queue_head_t
*wq
= &max
->wq
;
408 struct circ_buf
*xmit
= &max
->con_xmit
;
410 pr_info(PR_FMT
"start main thread\n");
413 wait_event_interruptible(*wq
,
414 max
->uart_flags
|| kthread_should_stop());
416 mutex_lock(&max
->thread_mutex
);
418 if (test_and_clear_bit(BIT_IRQ_PENDING
, &max
->uart_flags
))
419 max3110_con_receive(max
);
421 /* first handle console output */
422 if (test_and_clear_bit(CON_TX_NEEDED
, &max
->uart_flags
))
423 send_circ_buf(max
, xmit
);
425 /* handle uart output */
426 if (test_and_clear_bit(UART_TX_NEEDED
, &max
->uart_flags
))
429 mutex_unlock(&max
->thread_mutex
);
431 } while (!kthread_should_stop());
436 static irqreturn_t
serial_m3110_irq(int irq
, void *dev_id
)
438 struct uart_max3110
*max
= dev_id
;
440 /* max3110's irq is a falling edge, not level triggered,
441 * so no need to disable the irq */
443 if (!test_and_set_bit(BIT_IRQ_PENDING
, &max
->uart_flags
))
449 /* if don't use RX IRQ, then need a thread to polling read */
450 static int max3110_read_thread(void *_max
)
452 struct uart_max3110
*max
= _max
;
454 pr_info(PR_FMT
"start read thread\n");
457 * If can't acquire the mutex, it means the main thread
458 * is running which will also perform the rx job
460 if (mutex_trylock(&max
->thread_mutex
)) {
461 max3110_con_receive(max
);
462 mutex_unlock(&max
->thread_mutex
);
465 set_current_state(TASK_INTERRUPTIBLE
);
466 schedule_timeout(HZ
/ 20);
467 } while (!kthread_should_stop());
472 static int serial_m3110_startup(struct uart_port
*port
)
474 struct uart_max3110
*max
=
475 container_of(port
, struct uart_max3110
, port
);
479 if (port
->line
!= 0) {
480 pr_err(PR_FMT
"uart port startup failed\n");
484 /* Disable all IRQ and config it to 115200, 8n1 */
485 config
= WC_TAG
| WC_FIFO_ENABLE
490 /* as we use thread to handle tx/rx, need set low latency */
491 port
->state
->port
.low_latency
= 1;
494 max
->read_thread
= NULL
;
495 ret
= request_irq(max
->irq
, serial_m3110_irq
,
496 IRQ_TYPE_EDGE_FALLING
, "max3110", max
);
499 pr_err(PR_FMT
"unable to allocate IRQ, polling\n");
501 /* Enable RX IRQ only */
502 config
|= WC_RXA_IRQ_ENABLE
;
507 /* If IRQ is disabled, start a read thread for input data */
509 kthread_run(max3110_read_thread
, max
, "max3110_read");
510 if (IS_ERR(max
->read_thread
)) {
511 ret
= PTR_ERR(max
->read_thread
);
512 max
->read_thread
= NULL
;
513 pr_err(PR_FMT
"Can't create read thread!\n");
518 ret
= max3110_out(max
, config
);
521 free_irq(max
->irq
, max
);
522 if (max
->read_thread
)
523 kthread_stop(max
->read_thread
);
524 max
->read_thread
= NULL
;
528 max
->cur_conf
= config
;
532 static void serial_m3110_shutdown(struct uart_port
*port
)
534 struct uart_max3110
*max
=
535 container_of(port
, struct uart_max3110
, port
);
538 if (max
->read_thread
) {
539 kthread_stop(max
->read_thread
);
540 max
->read_thread
= NULL
;
544 free_irq(max
->irq
, max
);
546 /* Disable interrupts from this port */
547 config
= WC_TAG
| WC_SW_SHDI
;
548 max3110_out(max
, config
);
551 static void serial_m3110_release_port(struct uart_port
*port
)
555 static int serial_m3110_request_port(struct uart_port
*port
)
560 static void serial_m3110_config_port(struct uart_port
*port
, int flags
)
562 port
->type
= PORT_MAX3100
;
566 serial_m3110_verify_port(struct uart_port
*port
, struct serial_struct
*ser
)
568 /* we don't want the core code to modify any port params */
573 static const char *serial_m3110_type(struct uart_port
*port
)
575 struct uart_max3110
*max
=
576 container_of(port
, struct uart_max3110
, port
);
581 serial_m3110_set_termios(struct uart_port
*port
, struct ktermios
*termios
,
582 struct ktermios
*old
)
584 struct uart_max3110
*max
=
585 container_of(port
, struct uart_max3110
, port
);
587 unsigned int baud
, parity
= 0;
589 u16 new_conf
= max
->cur_conf
;
591 switch (termios
->c_cflag
& CSIZE
) {
593 cval
= UART_LCR_WLEN7
;
594 new_conf
|= WC_7BIT_WORD
;
597 /* We only support CS7 & CS8 */
598 termios
->c_cflag
&= ~CSIZE
;
599 termios
->c_cflag
|= CS8
;
601 cval
= UART_LCR_WLEN8
;
602 new_conf
|= WC_8BIT_WORD
;
606 baud
= uart_get_baud_rate(port
, termios
, old
, 0, 230400);
608 /* First calc the div for 1.8MHZ clock case */
611 clk_div
= WC_BAUD_DR384
;
614 clk_div
= WC_BAUD_DR192
;
617 clk_div
= WC_BAUD_DR96
;
620 clk_div
= WC_BAUD_DR48
;
623 clk_div
= WC_BAUD_DR24
;
626 clk_div
= WC_BAUD_DR12
;
629 clk_div
= WC_BAUD_DR6
;
632 clk_div
= WC_BAUD_DR3
;
635 clk_div
= WC_BAUD_DR2
;
638 clk_div
= WC_BAUD_DR1
;
641 if (max
->clock
& MAX3110_HIGH_CLK
)
644 /* Pick the previous baud rate */
646 clk_div
= max
->cur_conf
& WC_BAUD_DIV_MASK
;
647 tty_termios_encode_baud_rate(termios
, baud
, baud
);
650 if (max
->clock
& MAX3110_HIGH_CLK
) {
652 /* High clk version max3110 doesn't support B300 */
655 clk_div
= WC_BAUD_DR384
;
658 clk_div
= WC_BAUD_DR1
;
659 tty_termios_encode_baud_rate(termios
, baud
, baud
);
662 new_conf
= (new_conf
& ~WC_BAUD_DIV_MASK
) | clk_div
;
664 if (unlikely(termios
->c_cflag
& CMSPAR
))
665 termios
->c_cflag
&= ~CMSPAR
;
667 if (termios
->c_cflag
& CSTOPB
)
668 new_conf
|= WC_2_STOPBITS
;
670 new_conf
&= ~WC_2_STOPBITS
;
672 if (termios
->c_cflag
& PARENB
) {
673 new_conf
|= WC_PARITY_ENABLE
;
674 parity
|= UART_LCR_PARITY
;
676 new_conf
&= ~WC_PARITY_ENABLE
;
678 if (!(termios
->c_cflag
& PARODD
))
679 parity
|= UART_LCR_EPAR
;
680 max
->parity
= parity
;
682 uart_update_timeout(port
, termios
->c_cflag
, baud
);
685 if (new_conf
!= max
->cur_conf
) {
686 if (!max3110_out(max
, new_conf
)) {
687 max
->cur_conf
= new_conf
;
693 /* Don't handle hw handshaking */
694 static unsigned int serial_m3110_get_mctrl(struct uart_port
*port
)
696 return TIOCM_DSR
| TIOCM_CAR
| TIOCM_DSR
;
699 static void serial_m3110_set_mctrl(struct uart_port
*port
, unsigned int mctrl
)
703 static void serial_m3110_break_ctl(struct uart_port
*port
, int break_state
)
707 static void serial_m3110_pm(struct uart_port
*port
, unsigned int state
,
708 unsigned int oldstate
)
712 static void serial_m3110_enable_ms(struct uart_port
*port
)
716 struct uart_ops serial_m3110_ops
= {
717 .tx_empty
= serial_m3110_tx_empty
,
718 .set_mctrl
= serial_m3110_set_mctrl
,
719 .get_mctrl
= serial_m3110_get_mctrl
,
720 .stop_tx
= serial_m3110_stop_tx
,
721 .start_tx
= serial_m3110_start_tx
,
722 .stop_rx
= serial_m3110_stop_rx
,
723 .enable_ms
= serial_m3110_enable_ms
,
724 .break_ctl
= serial_m3110_break_ctl
,
725 .startup
= serial_m3110_startup
,
726 .shutdown
= serial_m3110_shutdown
,
727 .set_termios
= serial_m3110_set_termios
,
728 .pm
= serial_m3110_pm
,
729 .type
= serial_m3110_type
,
730 .release_port
= serial_m3110_release_port
,
731 .request_port
= serial_m3110_request_port
,
732 .config_port
= serial_m3110_config_port
,
733 .verify_port
= serial_m3110_verify_port
,
736 static struct uart_driver serial_m3110_reg
= {
737 .owner
= THIS_MODULE
,
738 .driver_name
= "MRST serial",
743 .cons
= &serial_m3110_console
,
746 #ifdef CONFIG_PM_SLEEP
747 static int serial_m3110_suspend(struct device
*dev
)
749 struct spi_device
*spi
= to_spi_device(dev
);
750 struct uart_max3110
*max
= spi_get_drvdata(spi
);
752 disable_irq(max
->irq
);
753 uart_suspend_port(&serial_m3110_reg
, &max
->port
);
754 max3110_out(max
, max
->cur_conf
| WC_SW_SHDI
);
758 static int serial_m3110_resume(struct device
*dev
)
760 struct spi_device
*spi
= to_spi_device(dev
);
761 struct uart_max3110
*max
= spi_get_drvdata(spi
);
763 max3110_out(max
, max
->cur_conf
);
764 uart_resume_port(&serial_m3110_reg
, &max
->port
);
765 enable_irq(max
->irq
);
769 static SIMPLE_DEV_PM_OPS(serial_m3110_pm_ops
, serial_m3110_suspend
,
770 serial_m3110_resume
);
771 #define SERIAL_M3110_PM_OPS (&serial_m3110_pm_ops)
774 #define SERIAL_M3110_PM_OPS NULL
777 static int serial_m3110_probe(struct spi_device
*spi
)
779 struct uart_max3110
*max
;
784 max
= kzalloc(sizeof(*max
), GFP_KERNEL
);
789 spi
->bits_per_word
= 16;
790 max
->clock
= MAX3110_HIGH_CLK
;
794 max
->port
.type
= PORT_MAX3100
;
795 max
->port
.fifosize
= 2; /* Only have 16b buffer */
796 max
->port
.ops
= &serial_m3110_ops
;
798 max
->port
.dev
= &spi
->dev
;
799 max
->port
.uartclk
= 115200;
802 strcpy(max
->name
, spi
->modalias
);
803 max
->irq
= (u16
)spi
->irq
;
805 mutex_init(&max
->thread_mutex
);
814 /* Check if reading configuration register returns something sane */
817 ret
= max3110_write_then_read(max
, (u8
*)&res
, (u8
*)&res
, 2, 0);
818 if (ret
< 0 || res
== 0 || res
== 0xffff) {
819 dev_dbg(&spi
->dev
, "MAX3111 deemed not present (conf reg %04x)",
825 buffer
= (void *)__get_free_page(GFP_KERNEL
);
830 max
->con_xmit
.buf
= buffer
;
831 max
->con_xmit
.head
= 0;
832 max
->con_xmit
.tail
= 0;
834 init_waitqueue_head(&max
->wq
);
836 max
->main_thread
= kthread_run(max3110_main_thread
,
837 max
, "max3110_main");
838 if (IS_ERR(max
->main_thread
)) {
839 ret
= PTR_ERR(max
->main_thread
);
843 spi_set_drvdata(spi
, max
);
846 /* Give membase a psudo value to pass serial_core's check */
847 max
->port
.membase
= (void *)0xff110000;
848 uart_add_one_port(&serial_m3110_reg
, &max
->port
);
853 free_page((unsigned long)buffer
);
859 static int serial_m3110_remove(struct spi_device
*dev
)
861 struct uart_max3110
*max
= spi_get_drvdata(dev
);
866 uart_remove_one_port(&serial_m3110_reg
, &max
->port
);
868 free_page((unsigned long)max
->con_xmit
.buf
);
870 if (max
->main_thread
)
871 kthread_stop(max
->main_thread
);
877 static struct spi_driver uart_max3110_driver
= {
879 .name
= "spi_max3111",
880 .owner
= THIS_MODULE
,
881 .pm
= SERIAL_M3110_PM_OPS
,
883 .probe
= serial_m3110_probe
,
884 .remove
= serial_m3110_remove
,
887 static int __init
serial_m3110_init(void)
891 ret
= uart_register_driver(&serial_m3110_reg
);
895 ret
= spi_register_driver(&uart_max3110_driver
);
897 uart_unregister_driver(&serial_m3110_reg
);
902 static void __exit
serial_m3110_exit(void)
904 spi_unregister_driver(&uart_max3110_driver
);
905 uart_unregister_driver(&serial_m3110_reg
);
908 module_init(serial_m3110_init
);
909 module_exit(serial_m3110_exit
);
911 MODULE_LICENSE("GPL v2");
912 MODULE_ALIAS("spi:max3110-uart");