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 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
32 #ifdef CONFIG_MAGIC_SYSRQ
36 #include <linux/module.h>
37 #include <linux/ioport.h>
38 #include <linux/irq.h>
39 #include <linux/init.h>
40 #include <linux/console.h>
41 #include <linux/tty.h>
42 #include <linux/tty_flip.h>
43 #include <linux/serial_core.h>
44 #include <linux/serial_reg.h>
46 #include <linux/kthread.h>
47 #include <linux/spi/spi.h>
50 #include "mrst_max3110.h"
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_warn("%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);
159 /* tx/rx always have the same length */
163 if (max3110_write_then_read(max, obuf, ibuf, blen, 1)) {
168 ret = receive_chars(max, ibuf, M3110_RX_FIFO_DEPTH);
174 static void serial_m3110_con_putchar(struct uart_port *port, int ch)
176 struct uart_max3110 *max =
177 container_of(port, struct uart_max3110, port);
178 struct circ_buf *xmit = &max->con_xmit;
180 if (uart_circ_chars_free(xmit)) {
181 xmit->buf[xmit->head] = (char)ch;
182 xmit->head = (xmit->head + 1) & (PAGE_SIZE - 1);
187 * Print a string to the serial port trying not to disturb
188 * any possible real use of the port...
190 * The console_lock must be held when we get here.
192 static void serial_m3110_con_write(struct console *co,
193 const char *s, unsigned int count)
198 uart_console_write(&pmax->port, s, count, serial_m3110_con_putchar);
200 if (!test_and_set_bit(CON_TX_NEEDED, &pmax->uart_flags))
205 serial_m3110_con_setup(struct console *co, char *options)
207 struct uart_max3110 *max = pmax;
213 pr_info("setting up console\n");
219 pr_err("pmax is NULL, return\n");
224 uart_parse_options(options, &baud, &parity, &bits, &flow);
226 return uart_set_options(&max->port, co, baud, parity, bits, flow);
229 static struct tty_driver *serial_m3110_con_device(struct console *co,
232 struct uart_driver *p = co->data;
234 return p->tty_driver;
237 static struct uart_driver serial_m3110_reg;
238 static struct console serial_m3110_console = {
240 .write = serial_m3110_con_write,
241 .device = serial_m3110_con_device,
242 .setup = serial_m3110_con_setup,
243 .flags = CON_PRINTBUFFER,
245 .data = &serial_m3110_reg,
248 static unsigned int serial_m3110_tx_empty(struct uart_port *port)
253 static void serial_m3110_stop_tx(struct uart_port *port)
258 /* stop_rx will be called in spin_lock env */
259 static void serial_m3110_stop_rx(struct uart_port *port)
264 #define WORDS_PER_XFER 128
265 static void send_circ_buf(struct uart_max3110 *max,
266 struct circ_buf *xmit)
270 int i, len, blen, dma_size, left, ret = 0;
273 dma_size = WORDS_PER_XFER * sizeof(u16) * 2;
274 buf = kzalloc(dma_size, GFP_KERNEL | GFP_DMA);
278 ibuf = buf + dma_size/2;
280 while (!uart_circ_empty(xmit)) {
281 left = uart_circ_chars_pending(xmit);
283 len = min(left, WORDS_PER_XFER);
284 blen = len * sizeof(u16);
285 memset(ibuf, 0, blen);
287 for (i = 0; i < len; i++) {
288 obuf[i] = (u8)xmit->buf[xmit->tail] | WD_TAG;
289 xmit->tail = (xmit->tail + 1) &
290 (UART_XMIT_SIZE - 1);
293 /* Fail to send msg to console is not very critical */
295 ret = max3110_write_then_read(max, obuf, ibuf, blen, 0);
297 pr_warn("%s: get err msg %d\n", __func__, ret);
299 receive_chars(max, ibuf, len);
301 max->port.icount.tx += len;
309 static void transmit_char(struct uart_max3110 *max)
311 struct uart_port *port = &max->port;
312 struct circ_buf *xmit = &port->state->xmit;
314 if (uart_circ_empty(xmit) || uart_tx_stopped(port))
317 send_circ_buf(max, xmit);
319 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
320 uart_write_wakeup(port);
322 if (uart_circ_empty(xmit))
323 serial_m3110_stop_tx(port);
327 * This will be called by uart_write() and tty_write, can't
330 static void serial_m3110_start_tx(struct uart_port *port)
332 struct uart_max3110 *max =
333 container_of(port, struct uart_max3110, port);
335 if (!test_and_set_bit(UART_TX_NEEDED, &max->uart_flags))
340 receive_chars(struct uart_max3110 *max, unsigned short *str, int len)
342 struct uart_port *port = &max->port;
343 struct tty_port *tport;
344 char buf[M3110_RX_FIFO_DEPTH];
347 /* If uart is not opened, just return */
351 tport = &port->state->port;
353 for (r = 0, w = 0; r < len; r++) {
354 if (str[r] & MAX3110_BREAK &&
355 uart_handle_break(port))
358 if (str[r] & MAX3110_READ_DATA_AVAILABLE) {
359 if (uart_handle_sysrq_char(port, str[r] & 0xff))
362 buf[w++] = str[r] & 0xff;
369 for (r = 0; w; r += usable, w -= usable) {
370 usable = tty_buffer_request_room(tport, w);
372 tty_insert_flip_string(tport, buf + r, usable);
373 port->icount.rx += usable;
376 tty_flip_buffer_push(tport);
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)
396 num = max3110_read_multi(max);
404 static int max3110_main_thread(void *_max)
406 struct uart_max3110 *max = _max;
407 wait_queue_head_t *wq = &max->wq;
409 struct circ_buf *xmit = &max->con_xmit;
411 pr_info("start main thread\n");
414 wait_event_interruptible(*wq,
415 max->uart_flags || kthread_should_stop());
417 mutex_lock(&max->thread_mutex);
419 if (test_and_clear_bit(BIT_IRQ_PENDING, &max->uart_flags))
420 max3110_con_receive(max);
422 /* first handle console output */
423 if (test_and_clear_bit(CON_TX_NEEDED, &max->uart_flags))
424 send_circ_buf(max, xmit);
426 /* handle uart output */
427 if (test_and_clear_bit(UART_TX_NEEDED, &max->uart_flags))
430 mutex_unlock(&max->thread_mutex);
432 } while (!kthread_should_stop());
437 static irqreturn_t serial_m3110_irq(int irq, void *dev_id)
439 struct uart_max3110 *max = dev_id;
441 /* max3110's irq is a falling edge, not level triggered,
442 * so no need to disable the irq */
444 if (!test_and_set_bit(BIT_IRQ_PENDING, &max->uart_flags))
450 /* if don't use RX IRQ, then need a thread to polling read */
451 static int max3110_read_thread(void *_max)
453 struct uart_max3110 *max = _max;
455 pr_info("start read thread\n");
458 * If can't acquire the mutex, it means the main thread
459 * is running which will also perform the rx job
461 if (mutex_trylock(&max->thread_mutex)) {
462 max3110_con_receive(max);
463 mutex_unlock(&max->thread_mutex);
466 set_current_state(TASK_INTERRUPTIBLE);
467 schedule_timeout(HZ / 20);
468 } while (!kthread_should_stop());
473 static int serial_m3110_startup(struct uart_port *port)
475 struct uart_max3110 *max =
476 container_of(port, struct uart_max3110, port);
480 if (port->line != 0) {
481 pr_err("uart port startup failed\n");
485 /* Disable all IRQ and config it to 115200, 8n1 */
486 config = WC_TAG | WC_FIFO_ENABLE
491 /* as we use thread to handle tx/rx, need set low latency */
492 port->state->port.low_latency = 1;
495 /* Enable RX IRQ only */
496 config |= WC_RXA_IRQ_ENABLE;
498 /* If IRQ is disabled, start a read thread for input data */
500 kthread_run(max3110_read_thread, max, "max3110_read");
501 if (IS_ERR(max->read_thread)) {
502 ret = PTR_ERR(max->read_thread);
503 max->read_thread = NULL;
504 pr_err("Can't create read thread!\n");
509 ret = max3110_out(max, config);
511 if (max->read_thread)
512 kthread_stop(max->read_thread);
513 max->read_thread = NULL;
517 max->cur_conf = config;
521 static void serial_m3110_shutdown(struct uart_port *port)
523 struct uart_max3110 *max =
524 container_of(port, struct uart_max3110, port);
527 if (max->read_thread) {
528 kthread_stop(max->read_thread);
529 max->read_thread = NULL;
532 /* Disable interrupts from this port */
533 config = WC_TAG | WC_SW_SHDI;
534 max3110_out(max, config);
537 static void serial_m3110_release_port(struct uart_port *port)
541 static int serial_m3110_request_port(struct uart_port *port)
546 static void serial_m3110_config_port(struct uart_port *port, int flags)
548 port->type = PORT_MAX3100;
552 serial_m3110_verify_port(struct uart_port *port, struct serial_struct *ser)
554 /* we don't want the core code to modify any port params */
559 static const char *serial_m3110_type(struct uart_port *port)
561 struct uart_max3110 *max =
562 container_of(port, struct uart_max3110, port);
567 serial_m3110_set_termios(struct uart_port *port, struct ktermios *termios,
568 struct ktermios *old)
570 struct uart_max3110 *max =
571 container_of(port, struct uart_max3110, port);
573 unsigned int baud, parity = 0;
575 u16 new_conf = max->cur_conf;
577 switch (termios->c_cflag & CSIZE) {
579 cval = UART_LCR_WLEN7;
580 new_conf |= WC_7BIT_WORD;
583 /* We only support CS7 & CS8 */
584 termios->c_cflag &= ~CSIZE;
585 termios->c_cflag |= CS8;
587 cval = UART_LCR_WLEN8;
588 new_conf |= WC_8BIT_WORD;
592 baud = uart_get_baud_rate(port, termios, old, 0, 230400);
594 /* First calc the div for 1.8MHZ clock case */
597 clk_div = WC_BAUD_DR384;
600 clk_div = WC_BAUD_DR192;
603 clk_div = WC_BAUD_DR96;
606 clk_div = WC_BAUD_DR48;
609 clk_div = WC_BAUD_DR24;
612 clk_div = WC_BAUD_DR12;
615 clk_div = WC_BAUD_DR6;
618 clk_div = WC_BAUD_DR3;
621 clk_div = WC_BAUD_DR2;
624 clk_div = WC_BAUD_DR1;
627 if (max->clock & MAX3110_HIGH_CLK)
630 /* Pick the previous baud rate */
632 clk_div = max->cur_conf & WC_BAUD_DIV_MASK;
633 tty_termios_encode_baud_rate(termios, baud, baud);
636 if (max->clock & MAX3110_HIGH_CLK) {
638 /* High clk version max3110 doesn't support B300 */
641 clk_div = WC_BAUD_DR384;
644 clk_div = WC_BAUD_DR1;
645 tty_termios_encode_baud_rate(termios, baud, baud);
648 new_conf = (new_conf & ~WC_BAUD_DIV_MASK) | clk_div;
650 if (unlikely(termios->c_cflag & CMSPAR))
651 termios->c_cflag &= ~CMSPAR;
653 if (termios->c_cflag & CSTOPB)
654 new_conf |= WC_2_STOPBITS;
656 new_conf &= ~WC_2_STOPBITS;
658 if (termios->c_cflag & PARENB) {
659 new_conf |= WC_PARITY_ENABLE;
660 parity |= UART_LCR_PARITY;
662 new_conf &= ~WC_PARITY_ENABLE;
664 if (!(termios->c_cflag & PARODD))
665 parity |= UART_LCR_EPAR;
666 max->parity = parity;
668 uart_update_timeout(port, termios->c_cflag, baud);
671 if (new_conf != max->cur_conf) {
672 if (!max3110_out(max, new_conf)) {
673 max->cur_conf = new_conf;
679 /* Don't handle hw handshaking */
680 static unsigned int serial_m3110_get_mctrl(struct uart_port *port)
682 return TIOCM_DSR | TIOCM_CAR | TIOCM_DSR;
685 static void serial_m3110_set_mctrl(struct uart_port *port, unsigned int mctrl)
689 static void serial_m3110_break_ctl(struct uart_port *port, int break_state)
693 static void serial_m3110_pm(struct uart_port *port, unsigned int state,
694 unsigned int oldstate)
698 static struct uart_ops serial_m3110_ops = {
699 .tx_empty = serial_m3110_tx_empty,
700 .set_mctrl = serial_m3110_set_mctrl,
701 .get_mctrl = serial_m3110_get_mctrl,
702 .stop_tx = serial_m3110_stop_tx,
703 .start_tx = serial_m3110_start_tx,
704 .stop_rx = serial_m3110_stop_rx,
705 .break_ctl = serial_m3110_break_ctl,
706 .startup = serial_m3110_startup,
707 .shutdown = serial_m3110_shutdown,
708 .set_termios = serial_m3110_set_termios,
709 .pm = serial_m3110_pm,
710 .type = serial_m3110_type,
711 .release_port = serial_m3110_release_port,
712 .request_port = serial_m3110_request_port,
713 .config_port = serial_m3110_config_port,
714 .verify_port = serial_m3110_verify_port,
717 static struct uart_driver serial_m3110_reg = {
718 .owner = THIS_MODULE,
719 .driver_name = "MRST serial",
724 .cons = &serial_m3110_console,
727 #ifdef CONFIG_PM_SLEEP
728 static int serial_m3110_suspend(struct device *dev)
730 struct spi_device *spi = to_spi_device(dev);
731 struct uart_max3110 *max = spi_get_drvdata(spi);
734 disable_irq(max->irq);
735 uart_suspend_port(&serial_m3110_reg, &max->port);
736 max3110_out(max, max->cur_conf | WC_SW_SHDI);
740 static int serial_m3110_resume(struct device *dev)
742 struct spi_device *spi = to_spi_device(dev);
743 struct uart_max3110 *max = spi_get_drvdata(spi);
745 max3110_out(max, max->cur_conf);
746 uart_resume_port(&serial_m3110_reg, &max->port);
748 enable_irq(max->irq);
752 static SIMPLE_DEV_PM_OPS(serial_m3110_pm_ops, serial_m3110_suspend,
753 serial_m3110_resume);
754 #define SERIAL_M3110_PM_OPS (&serial_m3110_pm_ops)
757 #define SERIAL_M3110_PM_OPS NULL
760 static int serial_m3110_probe(struct spi_device *spi)
762 struct uart_max3110 *max;
767 max = kzalloc(sizeof(*max), GFP_KERNEL);
772 spi->bits_per_word = 16;
773 max->clock = MAX3110_HIGH_CLK;
777 max->port.type = PORT_MAX3100;
778 max->port.fifosize = 2; /* Only have 16b buffer */
779 max->port.ops = &serial_m3110_ops;
781 max->port.dev = &spi->dev;
782 max->port.uartclk = 115200;
785 strcpy(max->name, spi->modalias);
786 max->irq = (u16)spi->irq;
788 mutex_init(&max->thread_mutex);
789 mutex_init(&max->io_mutex);
798 /* Check if reading configuration register returns something sane */
801 ret = max3110_write_then_read(max, (u8 *)&res, (u8 *)&res, 2, 0);
802 if (ret < 0 || res == 0 || res == 0xffff) {
803 dev_dbg(&spi->dev, "MAX3111 deemed not present (conf reg %04x)",
809 buffer = (void *)__get_free_page(GFP_KERNEL);
814 max->con_xmit.buf = buffer;
815 max->con_xmit.head = 0;
816 max->con_xmit.tail = 0;
818 init_waitqueue_head(&max->wq);
820 max->main_thread = kthread_run(max3110_main_thread,
821 max, "max3110_main");
822 if (IS_ERR(max->main_thread)) {
823 ret = PTR_ERR(max->main_thread);
828 ret = request_irq(max->irq, serial_m3110_irq,
829 IRQ_TYPE_EDGE_FALLING, "max3110", max);
833 "unable to allocate IRQ, will use polling method\n");
837 spi_set_drvdata(spi, max);
840 /* Give membase a psudo value to pass serial_core's check */
841 max->port.membase = (unsigned char __iomem *)0xff110000;
842 uart_add_one_port(&serial_m3110_reg, &max->port);
847 free_page((unsigned long)buffer);
853 static int serial_m3110_remove(struct spi_device *dev)
855 struct uart_max3110 *max = spi_get_drvdata(dev);
860 uart_remove_one_port(&serial_m3110_reg, &max->port);
862 free_page((unsigned long)max->con_xmit.buf);
865 free_irq(max->irq, max);
867 if (max->main_thread)
868 kthread_stop(max->main_thread);
874 static struct spi_driver uart_max3110_driver = {
876 .name = "spi_max3111",
877 .owner = THIS_MODULE,
878 .pm = SERIAL_M3110_PM_OPS,
880 .probe = serial_m3110_probe,
881 .remove = serial_m3110_remove,
884 static int __init serial_m3110_init(void)
888 ret = uart_register_driver(&serial_m3110_reg);
892 ret = spi_register_driver(&uart_max3110_driver);
894 uart_unregister_driver(&serial_m3110_reg);
899 static void __exit serial_m3110_exit(void)
901 spi_unregister_driver(&uart_max3110_driver);
902 uart_unregister_driver(&serial_m3110_reg);
905 module_init(serial_m3110_init);
906 module_exit(serial_m3110_exit);
908 MODULE_LICENSE("GPL v2");
909 MODULE_ALIAS("spi:max3110-uart");