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>
48 #include "mrst_max3110.h"
50 #define PR_FMT "mrst_max3110: "
52 #define UART_TX_NEEDED 1
53 #define CON_TX_NEEDED 2
54 #define BIT_IRQ_PENDING 3
57 struct uart_port port
;
58 struct spi_device
*spi
;
59 char name
[SPI_NAME_SIZE
];
62 struct task_struct
*main_thread
;
63 struct task_struct
*read_thread
;
64 struct mutex thread_mutex
;
65 struct mutex io_mutex
;
70 u8 parity
, word_7bits
;
73 unsigned long uart_flags
;
76 struct circ_buf con_xmit
;
79 /* global data structure, may need be removed */
80 static struct uart_max3110
*pmax
;
82 static int receive_chars(struct uart_max3110
*max
,
83 unsigned short *str
, int len
);
84 static int max3110_read_multi(struct uart_max3110
*max
);
85 static void max3110_con_receive(struct uart_max3110
*max
);
87 static int max3110_write_then_read(struct uart_max3110
*max
,
88 const void *txbuf
, void *rxbuf
, unsigned len
, int always_fast
)
90 struct spi_device
*spi
= max
->spi
;
91 struct spi_message message
;
92 struct spi_transfer x
;
95 mutex_lock(&max
->io_mutex
);
96 spi_message_init(&message
);
97 memset(&x
, 0, sizeof x
);
101 spi_message_add_tail(&x
, &message
);
104 x
.speed_hz
= spi
->max_speed_hz
;
106 x
.speed_hz
= max
->baud
;
109 ret
= spi_sync(spi
, &message
);
110 mutex_unlock(&max
->io_mutex
);
114 /* Write a 16b word to the device */
115 static int max3110_out(struct uart_max3110
*max
, const u16 out
)
121 buf
= kzalloc(8, GFP_KERNEL
| GFP_DMA
);
128 ret
= max3110_write_then_read(max
, obuf
, ibuf
, 2, 1);
130 pr_warning(PR_FMT
"%s(): get err msg %d when sending 0x%x\n",
135 receive_chars(max
, ibuf
, 1);
143 * This is usually used to read data from SPIC RX FIFO, which doesn't
144 * need any delay like flushing character out.
146 * Return how many valide bytes are read back
148 static int max3110_read_multi(struct uart_max3110
*max
)
154 blen
= M3110_RX_FIFO_DEPTH
* sizeof(u16
);
155 buf
= kzalloc(blen
* 2, GFP_KERNEL
| GFP_DMA
);
157 pr_warning(PR_FMT
"%s(): fail to alloc dma buffer\n", __func__
);
161 /* tx/rx always have the same length */
165 if (max3110_write_then_read(max
, obuf
, ibuf
, blen
, 1)) {
170 ret
= receive_chars(max
, ibuf
, M3110_RX_FIFO_DEPTH
);
176 static void serial_m3110_con_putchar(struct uart_port
*port
, int ch
)
178 struct uart_max3110
*max
=
179 container_of(port
, struct uart_max3110
, port
);
180 struct circ_buf
*xmit
= &max
->con_xmit
;
182 if (uart_circ_chars_free(xmit
)) {
183 xmit
->buf
[xmit
->head
] = (char)ch
;
184 xmit
->head
= (xmit
->head
+ 1) & (PAGE_SIZE
- 1);
189 * Print a string to the serial port trying not to disturb
190 * any possible real use of the port...
192 * The console_lock must be held when we get here.
194 static void serial_m3110_con_write(struct console
*co
,
195 const char *s
, unsigned int count
)
200 uart_console_write(&pmax
->port
, s
, count
, serial_m3110_con_putchar
);
202 if (!test_and_set_bit(CON_TX_NEEDED
, &pmax
->uart_flags
))
207 serial_m3110_con_setup(struct console
*co
, char *options
)
209 struct uart_max3110
*max
= pmax
;
215 pr_info(PR_FMT
"setting up console\n");
221 pr_err(PR_FMT
"pmax is NULL, return");
226 uart_parse_options(options
, &baud
, &parity
, &bits
, &flow
);
228 return uart_set_options(&max
->port
, co
, baud
, parity
, bits
, flow
);
231 static struct tty_driver
*serial_m3110_con_device(struct console
*co
,
234 struct uart_driver
*p
= co
->data
;
236 return p
->tty_driver
;
239 static struct uart_driver serial_m3110_reg
;
240 static struct console serial_m3110_console
= {
242 .write
= serial_m3110_con_write
,
243 .device
= serial_m3110_con_device
,
244 .setup
= serial_m3110_con_setup
,
245 .flags
= CON_PRINTBUFFER
,
247 .data
= &serial_m3110_reg
,
250 static unsigned int serial_m3110_tx_empty(struct uart_port
*port
)
255 static void serial_m3110_stop_tx(struct uart_port
*port
)
260 /* stop_rx will be called in spin_lock env */
261 static void serial_m3110_stop_rx(struct uart_port
*port
)
266 #define WORDS_PER_XFER 128
267 static void send_circ_buf(struct uart_max3110
*max
,
268 struct circ_buf
*xmit
)
272 int i
, len
, blen
, dma_size
, left
, ret
= 0;
275 dma_size
= WORDS_PER_XFER
* sizeof(u16
) * 2;
276 buf
= kzalloc(dma_size
, GFP_KERNEL
| GFP_DMA
);
280 ibuf
= buf
+ dma_size
/2;
282 while (!uart_circ_empty(xmit
)) {
283 left
= uart_circ_chars_pending(xmit
);
285 len
= min(left
, WORDS_PER_XFER
);
286 blen
= len
* sizeof(u16
);
287 memset(ibuf
, 0, blen
);
289 for (i
= 0; i
< len
; i
++) {
290 obuf
[i
] = (u8
)xmit
->buf
[xmit
->tail
] | WD_TAG
;
291 xmit
->tail
= (xmit
->tail
+ 1) &
292 (UART_XMIT_SIZE
- 1);
295 /* Fail to send msg to console is not very critical */
297 ret
= max3110_write_then_read(max
, obuf
, ibuf
, blen
, 0);
299 pr_warning(PR_FMT
"%s(): get err msg %d\n",
302 receive_chars(max
, ibuf
, len
);
304 max
->port
.icount
.tx
+= len
;
312 static void transmit_char(struct uart_max3110
*max
)
314 struct uart_port
*port
= &max
->port
;
315 struct circ_buf
*xmit
= &port
->state
->xmit
;
317 if (uart_circ_empty(xmit
) || uart_tx_stopped(port
))
320 send_circ_buf(max
, xmit
);
322 if (uart_circ_chars_pending(xmit
) < WAKEUP_CHARS
)
323 uart_write_wakeup(port
);
325 if (uart_circ_empty(xmit
))
326 serial_m3110_stop_tx(port
);
330 * This will be called by uart_write() and tty_write, can't
333 static void serial_m3110_start_tx(struct uart_port
*port
)
335 struct uart_max3110
*max
=
336 container_of(port
, struct uart_max3110
, port
);
338 if (!test_and_set_bit(UART_TX_NEEDED
, &max
->uart_flags
))
343 receive_chars(struct uart_max3110
*max
, unsigned short *str
, int len
)
345 struct uart_port
*port
= &max
->port
;
346 struct tty_port
*tport
;
347 char buf
[M3110_RX_FIFO_DEPTH
];
350 /* If uart is not opened, just return */
354 tport
= &port
->state
->port
;
356 for (r
= 0, w
= 0; r
< len
; r
++) {
357 if (str
[r
] & MAX3110_BREAK
&&
358 uart_handle_break(port
))
361 if (str
[r
] & MAX3110_READ_DATA_AVAILABLE
) {
362 if (uart_handle_sysrq_char(port
, str
[r
] & 0xff))
365 buf
[w
++] = str
[r
] & 0xff;
372 for (r
= 0; w
; r
+= usable
, w
-= usable
) {
373 usable
= tty_buffer_request_room(tport
, w
);
375 tty_insert_flip_string(tport
, buf
+ r
, usable
);
376 port
->icount
.rx
+= usable
;
379 tty_flip_buffer_push(tport
);
385 * This routine will be used in read_thread or RX IRQ handling,
386 * it will first do one round buffer read(8 words), if there is some
387 * valid RX data, will try to read 5 more rounds till all data
390 * Use stack space as data buffer to save some system load, and chose
391 * 504 Btyes as a threadhold to do a bulk push to upper tty layer when
392 * receiving bulk data, a much bigger buffer may cause stack overflow
394 static void max3110_con_receive(struct uart_max3110
*max
)
399 num
= max3110_read_multi(max
);
407 static int max3110_main_thread(void *_max
)
409 struct uart_max3110
*max
= _max
;
410 wait_queue_head_t
*wq
= &max
->wq
;
412 struct circ_buf
*xmit
= &max
->con_xmit
;
414 pr_info(PR_FMT
"start main thread\n");
417 wait_event_interruptible(*wq
,
418 max
->uart_flags
|| kthread_should_stop());
420 mutex_lock(&max
->thread_mutex
);
422 if (test_and_clear_bit(BIT_IRQ_PENDING
, &max
->uart_flags
))
423 max3110_con_receive(max
);
425 /* first handle console output */
426 if (test_and_clear_bit(CON_TX_NEEDED
, &max
->uart_flags
))
427 send_circ_buf(max
, xmit
);
429 /* handle uart output */
430 if (test_and_clear_bit(UART_TX_NEEDED
, &max
->uart_flags
))
433 mutex_unlock(&max
->thread_mutex
);
435 } while (!kthread_should_stop());
440 static irqreturn_t
serial_m3110_irq(int irq
, void *dev_id
)
442 struct uart_max3110
*max
= dev_id
;
444 /* max3110's irq is a falling edge, not level triggered,
445 * so no need to disable the irq */
447 if (!test_and_set_bit(BIT_IRQ_PENDING
, &max
->uart_flags
))
453 /* if don't use RX IRQ, then need a thread to polling read */
454 static int max3110_read_thread(void *_max
)
456 struct uart_max3110
*max
= _max
;
458 pr_info(PR_FMT
"start read thread\n");
461 * If can't acquire the mutex, it means the main thread
462 * is running which will also perform the rx job
464 if (mutex_trylock(&max
->thread_mutex
)) {
465 max3110_con_receive(max
);
466 mutex_unlock(&max
->thread_mutex
);
469 set_current_state(TASK_INTERRUPTIBLE
);
470 schedule_timeout(HZ
/ 20);
471 } while (!kthread_should_stop());
476 static int serial_m3110_startup(struct uart_port
*port
)
478 struct uart_max3110
*max
=
479 container_of(port
, struct uart_max3110
, port
);
483 if (port
->line
!= 0) {
484 pr_err(PR_FMT
"uart port startup failed\n");
488 /* Disable all IRQ and config it to 115200, 8n1 */
489 config
= WC_TAG
| WC_FIFO_ENABLE
494 /* as we use thread to handle tx/rx, need set low latency */
495 port
->state
->port
.low_latency
= 1;
498 /* Enable RX IRQ only */
499 config
|= WC_RXA_IRQ_ENABLE
;
501 /* If IRQ is disabled, start a read thread for input data */
503 kthread_run(max3110_read_thread
, max
, "max3110_read");
504 if (IS_ERR(max
->read_thread
)) {
505 ret
= PTR_ERR(max
->read_thread
);
506 max
->read_thread
= NULL
;
507 pr_err(PR_FMT
"Can't create read thread!\n");
512 ret
= max3110_out(max
, config
);
514 if (max
->read_thread
)
515 kthread_stop(max
->read_thread
);
516 max
->read_thread
= NULL
;
520 max
->cur_conf
= config
;
524 static void serial_m3110_shutdown(struct uart_port
*port
)
526 struct uart_max3110
*max
=
527 container_of(port
, struct uart_max3110
, port
);
530 if (max
->read_thread
) {
531 kthread_stop(max
->read_thread
);
532 max
->read_thread
= NULL
;
535 /* Disable interrupts from this port */
536 config
= WC_TAG
| WC_SW_SHDI
;
537 max3110_out(max
, config
);
540 static void serial_m3110_release_port(struct uart_port
*port
)
544 static int serial_m3110_request_port(struct uart_port
*port
)
549 static void serial_m3110_config_port(struct uart_port
*port
, int flags
)
551 port
->type
= PORT_MAX3100
;
555 serial_m3110_verify_port(struct uart_port
*port
, struct serial_struct
*ser
)
557 /* we don't want the core code to modify any port params */
562 static const char *serial_m3110_type(struct uart_port
*port
)
564 struct uart_max3110
*max
=
565 container_of(port
, struct uart_max3110
, port
);
570 serial_m3110_set_termios(struct uart_port
*port
, struct ktermios
*termios
,
571 struct ktermios
*old
)
573 struct uart_max3110
*max
=
574 container_of(port
, struct uart_max3110
, port
);
576 unsigned int baud
, parity
= 0;
578 u16 new_conf
= max
->cur_conf
;
580 switch (termios
->c_cflag
& CSIZE
) {
582 cval
= UART_LCR_WLEN7
;
583 new_conf
|= WC_7BIT_WORD
;
586 /* We only support CS7 & CS8 */
587 termios
->c_cflag
&= ~CSIZE
;
588 termios
->c_cflag
|= CS8
;
590 cval
= UART_LCR_WLEN8
;
591 new_conf
|= WC_8BIT_WORD
;
595 baud
= uart_get_baud_rate(port
, termios
, old
, 0, 230400);
597 /* First calc the div for 1.8MHZ clock case */
600 clk_div
= WC_BAUD_DR384
;
603 clk_div
= WC_BAUD_DR192
;
606 clk_div
= WC_BAUD_DR96
;
609 clk_div
= WC_BAUD_DR48
;
612 clk_div
= WC_BAUD_DR24
;
615 clk_div
= WC_BAUD_DR12
;
618 clk_div
= WC_BAUD_DR6
;
621 clk_div
= WC_BAUD_DR3
;
624 clk_div
= WC_BAUD_DR2
;
627 clk_div
= WC_BAUD_DR1
;
630 if (max
->clock
& MAX3110_HIGH_CLK
)
633 /* Pick the previous baud rate */
635 clk_div
= max
->cur_conf
& WC_BAUD_DIV_MASK
;
636 tty_termios_encode_baud_rate(termios
, baud
, baud
);
639 if (max
->clock
& MAX3110_HIGH_CLK
) {
641 /* High clk version max3110 doesn't support B300 */
644 clk_div
= WC_BAUD_DR384
;
647 clk_div
= WC_BAUD_DR1
;
648 tty_termios_encode_baud_rate(termios
, baud
, baud
);
651 new_conf
= (new_conf
& ~WC_BAUD_DIV_MASK
) | clk_div
;
653 if (unlikely(termios
->c_cflag
& CMSPAR
))
654 termios
->c_cflag
&= ~CMSPAR
;
656 if (termios
->c_cflag
& CSTOPB
)
657 new_conf
|= WC_2_STOPBITS
;
659 new_conf
&= ~WC_2_STOPBITS
;
661 if (termios
->c_cflag
& PARENB
) {
662 new_conf
|= WC_PARITY_ENABLE
;
663 parity
|= UART_LCR_PARITY
;
665 new_conf
&= ~WC_PARITY_ENABLE
;
667 if (!(termios
->c_cflag
& PARODD
))
668 parity
|= UART_LCR_EPAR
;
669 max
->parity
= parity
;
671 uart_update_timeout(port
, termios
->c_cflag
, baud
);
674 if (new_conf
!= max
->cur_conf
) {
675 if (!max3110_out(max
, new_conf
)) {
676 max
->cur_conf
= new_conf
;
682 /* Don't handle hw handshaking */
683 static unsigned int serial_m3110_get_mctrl(struct uart_port
*port
)
685 return TIOCM_DSR
| TIOCM_CAR
| TIOCM_DSR
;
688 static void serial_m3110_set_mctrl(struct uart_port
*port
, unsigned int mctrl
)
692 static void serial_m3110_break_ctl(struct uart_port
*port
, int break_state
)
696 static void serial_m3110_pm(struct uart_port
*port
, unsigned int state
,
697 unsigned int oldstate
)
701 static void serial_m3110_enable_ms(struct uart_port
*port
)
705 static struct uart_ops serial_m3110_ops
= {
706 .tx_empty
= serial_m3110_tx_empty
,
707 .set_mctrl
= serial_m3110_set_mctrl
,
708 .get_mctrl
= serial_m3110_get_mctrl
,
709 .stop_tx
= serial_m3110_stop_tx
,
710 .start_tx
= serial_m3110_start_tx
,
711 .stop_rx
= serial_m3110_stop_rx
,
712 .enable_ms
= serial_m3110_enable_ms
,
713 .break_ctl
= serial_m3110_break_ctl
,
714 .startup
= serial_m3110_startup
,
715 .shutdown
= serial_m3110_shutdown
,
716 .set_termios
= serial_m3110_set_termios
,
717 .pm
= serial_m3110_pm
,
718 .type
= serial_m3110_type
,
719 .release_port
= serial_m3110_release_port
,
720 .request_port
= serial_m3110_request_port
,
721 .config_port
= serial_m3110_config_port
,
722 .verify_port
= serial_m3110_verify_port
,
725 static struct uart_driver serial_m3110_reg
= {
726 .owner
= THIS_MODULE
,
727 .driver_name
= "MRST serial",
732 .cons
= &serial_m3110_console
,
735 #ifdef CONFIG_PM_SLEEP
736 static int serial_m3110_suspend(struct device
*dev
)
738 struct spi_device
*spi
= to_spi_device(dev
);
739 struct uart_max3110
*max
= spi_get_drvdata(spi
);
742 disable_irq(max
->irq
);
743 uart_suspend_port(&serial_m3110_reg
, &max
->port
);
744 max3110_out(max
, max
->cur_conf
| WC_SW_SHDI
);
748 static int serial_m3110_resume(struct device
*dev
)
750 struct spi_device
*spi
= to_spi_device(dev
);
751 struct uart_max3110
*max
= spi_get_drvdata(spi
);
753 max3110_out(max
, max
->cur_conf
);
754 uart_resume_port(&serial_m3110_reg
, &max
->port
);
756 enable_irq(max
->irq
);
760 static SIMPLE_DEV_PM_OPS(serial_m3110_pm_ops
, serial_m3110_suspend
,
761 serial_m3110_resume
);
762 #define SERIAL_M3110_PM_OPS (&serial_m3110_pm_ops)
765 #define SERIAL_M3110_PM_OPS NULL
768 static int serial_m3110_probe(struct spi_device
*spi
)
770 struct uart_max3110
*max
;
775 max
= kzalloc(sizeof(*max
), GFP_KERNEL
);
780 spi
->bits_per_word
= 16;
781 max
->clock
= MAX3110_HIGH_CLK
;
785 max
->port
.type
= PORT_MAX3100
;
786 max
->port
.fifosize
= 2; /* Only have 16b buffer */
787 max
->port
.ops
= &serial_m3110_ops
;
789 max
->port
.dev
= &spi
->dev
;
790 max
->port
.uartclk
= 115200;
793 strcpy(max
->name
, spi
->modalias
);
794 max
->irq
= (u16
)spi
->irq
;
796 mutex_init(&max
->thread_mutex
);
797 mutex_init(&max
->io_mutex
);
806 /* Check if reading configuration register returns something sane */
809 ret
= max3110_write_then_read(max
, (u8
*)&res
, (u8
*)&res
, 2, 0);
810 if (ret
< 0 || res
== 0 || res
== 0xffff) {
811 dev_dbg(&spi
->dev
, "MAX3111 deemed not present (conf reg %04x)",
817 buffer
= (void *)__get_free_page(GFP_KERNEL
);
822 max
->con_xmit
.buf
= buffer
;
823 max
->con_xmit
.head
= 0;
824 max
->con_xmit
.tail
= 0;
826 init_waitqueue_head(&max
->wq
);
828 max
->main_thread
= kthread_run(max3110_main_thread
,
829 max
, "max3110_main");
830 if (IS_ERR(max
->main_thread
)) {
831 ret
= PTR_ERR(max
->main_thread
);
836 ret
= request_irq(max
->irq
, serial_m3110_irq
,
837 IRQ_TYPE_EDGE_FALLING
, "max3110", max
);
841 "unable to allocate IRQ, will use polling method\n");
845 spi_set_drvdata(spi
, max
);
848 /* Give membase a psudo value to pass serial_core's check */
849 max
->port
.membase
= (unsigned char __iomem
*)0xff110000;
850 uart_add_one_port(&serial_m3110_reg
, &max
->port
);
855 free_page((unsigned long)buffer
);
861 static int serial_m3110_remove(struct spi_device
*dev
)
863 struct uart_max3110
*max
= spi_get_drvdata(dev
);
868 uart_remove_one_port(&serial_m3110_reg
, &max
->port
);
870 free_page((unsigned long)max
->con_xmit
.buf
);
873 free_irq(max
->irq
, max
);
875 if (max
->main_thread
)
876 kthread_stop(max
->main_thread
);
882 static struct spi_driver uart_max3110_driver
= {
884 .name
= "spi_max3111",
885 .owner
= THIS_MODULE
,
886 .pm
= SERIAL_M3110_PM_OPS
,
888 .probe
= serial_m3110_probe
,
889 .remove
= serial_m3110_remove
,
892 static int __init
serial_m3110_init(void)
896 ret
= uart_register_driver(&serial_m3110_reg
);
900 ret
= spi_register_driver(&uart_max3110_driver
);
902 uart_unregister_driver(&serial_m3110_reg
);
907 static void __exit
serial_m3110_exit(void)
909 spi_unregister_driver(&uart_max3110_driver
);
910 uart_unregister_driver(&serial_m3110_reg
);
913 module_init(serial_m3110_init
);
914 module_exit(serial_m3110_exit
);
916 MODULE_LICENSE("GPL v2");
917 MODULE_ALIAS("spi:max3110-uart");