2 * max3110.c - spi uart protocol driver for Maxim 3110 on Moorestown
4 * Copyright (C) Intel 2008 Feng Tang <feng.tang@intel.com>
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/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>
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>
47 #include "mrst_max3110.h"
49 #define PR_FMT "mrst_max3110: "
52 struct uart_port port;
53 struct spi_device *spi;
57 struct task_struct *main_thread;
58 struct task_struct *read_thread;
65 u8 parity, word_7bits;
67 atomic_t uart_tx_need;
70 struct circ_buf con_xmit;
78 /* global data structure, may need be removed */
79 struct uart_max3110 *pmax;
80 static inline void receive_char(struct uart_max3110 *max, u8 ch);
81 static void receive_chars(struct uart_max3110 *max,
82 unsigned char *str, int len);
83 static int max3110_read_multi(struct uart_max3110 *max, int len, u8 *buf);
84 static void max3110_console_receive(struct uart_max3110 *max);
86 int max3110_write_then_read(struct uart_max3110 *max,
87 const u8 *txbuf, u8 *rxbuf, unsigned len, int always_fast)
89 struct spi_device *spi = max->spi;
90 struct spi_message message;
91 struct spi_transfer x;
97 spi_message_init(&message);
98 memset(&x, 0, sizeof x);
102 spi_message_add_tail(&x, &message);
105 x.speed_hz = 3125000;
107 x.speed_hz = max->baud;
110 ret = spi_sync(spi, &message);
114 /* Write a u16 to the device, and return one u16 read back */
115 int max3110_out(struct uart_max3110 *max, const u16 out)
120 ret = max3110_write_then_read(max, (u8 *)&out, (u8 *)&tmp, 2, 1);
124 /* If some valid data is read back */
125 if (tmp & MAX3110_READ_DATA_AVAILABLE)
126 receive_char(max, (tmp & 0xff));
131 #define MAX_READ_LEN 20
133 * This is usually used to read data from SPIC RX FIFO, which doesn't
134 * need any delay like flushing character out. It returns how many
135 * valide bytes are read back
137 static int max3110_read_multi(struct uart_max3110 *max, int len, u8 *buf)
139 u16 out[MAX_READ_LEN], in[MAX_READ_LEN];
140 u8 *pbuf, valid_str[MAX_READ_LEN];
143 if (len > MAX_READ_LEN) {
144 pr_err(PR_FMT "read len %d is too large\n", len);
149 memset(out, 0, bytelen);
150 memset(in, 0, bytelen);
152 if (max3110_write_then_read(max, (u8 *)out, (u8 *)in, bytelen, 1))
155 /* If caller don't provide a buffer, then handle received char */
156 pbuf = buf ? buf : valid_str;
158 for (i = 0, j = 0; i < len; i++) {
159 if (in[i] & MAX3110_READ_DATA_AVAILABLE)
160 pbuf[j++] = (u8)(in[i] & 0xff);
163 if (j && (pbuf == valid_str))
164 receive_chars(max, valid_str, j);
169 static void serial_m3110_con_putchar(struct uart_port *port, int ch)
171 struct uart_max3110 *max =
172 container_of(port, struct uart_max3110, port);
173 struct circ_buf *xmit = &max->con_xmit;
175 if (uart_circ_chars_free(xmit)) {
176 xmit->buf[xmit->head] = (char)ch;
177 xmit->head = (xmit->head + 1) & (PAGE_SIZE - 1);
180 if (!atomic_read(&max->con_tx_need)) {
181 atomic_set(&max->con_tx_need, 1);
182 wake_up_process(max->main_thread);
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);
202 serial_m3110_con_setup(struct console *co, char *options)
204 struct uart_max3110 *max = pmax;
210 pr_info(PR_FMT "setting up console\n");
213 pr_err(PR_FMT "pmax is NULL, return");
218 uart_parse_options(options, &baud, &parity, &bits, &flow);
220 return uart_set_options(&max->port, co, baud, parity, bits, flow);
223 static struct tty_driver *serial_m3110_con_device(struct console *co,
226 struct uart_driver *p = co->data;
228 return p->tty_driver;
231 static struct uart_driver serial_m3110_reg;
232 static struct console serial_m3110_console = {
234 .write = serial_m3110_con_write,
235 .device = serial_m3110_con_device,
236 .setup = serial_m3110_con_setup,
237 .flags = CON_PRINTBUFFER,
239 .data = &serial_m3110_reg,
242 #define MRST_CONSOLE (&serial_m3110_console)
244 static unsigned int serial_m3110_tx_empty(struct uart_port *port)
249 static void serial_m3110_stop_tx(struct uart_port *port)
254 /* stop_rx will be called in spin_lock env */
255 static void serial_m3110_stop_rx(struct uart_port *port)
260 #define WORDS_PER_XFER 128
261 static inline void send_circ_buf(struct uart_max3110 *max,
262 struct circ_buf *xmit)
265 u16 obuf[WORDS_PER_XFER], ibuf[WORDS_PER_XFER];
266 u8 valid_str[WORDS_PER_XFER];
269 while (!uart_circ_empty(xmit)) {
270 left = uart_circ_chars_pending(xmit);
272 len = (left >= WORDS_PER_XFER) ? WORDS_PER_XFER : left;
274 memset(obuf, 0, len * 2);
275 memset(ibuf, 0, len * 2);
276 for (i = 0; i < len; i++) {
277 obuf[i] = (u8)xmit->buf[xmit->tail] | WD_TAG;
278 xmit->tail = (xmit->tail + 1) &
279 (UART_XMIT_SIZE - 1);
281 max3110_write_then_read(max, (u8 *)obuf,
282 (u8 *)ibuf, len * 2, 0);
284 for (i = 0, j = 0; i < len; i++) {
285 if (ibuf[i] & MAX3110_READ_DATA_AVAILABLE)
286 valid_str[j++] = (u8)(ibuf[i] & 0xff);
290 receive_chars(max, valid_str, j);
292 max->port.icount.tx += len;
298 static void transmit_char(struct uart_max3110 *max)
300 struct uart_port *port = &max->port;
301 struct circ_buf *xmit = &port->state->xmit;
303 if (uart_circ_empty(xmit) || uart_tx_stopped(port))
306 send_circ_buf(max, xmit);
308 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
309 uart_write_wakeup(port);
311 if (uart_circ_empty(xmit))
312 serial_m3110_stop_tx(port);
315 /* This will be called by uart_write() and tty_write, can't
317 static void serial_m3110_start_tx(struct uart_port *port)
319 struct uart_max3110 *max =
320 container_of(port, struct uart_max3110, port);
322 if (!atomic_read(&max->uart_tx_need)) {
323 atomic_set(&max->uart_tx_need, 1);
324 wake_up_process(max->main_thread);
328 static void receive_chars(struct uart_max3110 *max, unsigned char *str, int len)
330 struct uart_port *port = &max->port;
331 struct tty_struct *tty;
334 /* If uart is not opened, just return */
338 tty = port->state->port.tty;
340 return; /* receive some char before the tty is opened */
343 usable = tty_buffer_request_room(tty, len);
345 tty_insert_flip_string(tty, str, usable);
347 port->icount.rx += usable;
348 tty_flip_buffer_push(tty);
354 static inline void receive_char(struct uart_max3110 *max, u8 ch)
356 receive_chars(max, &ch, 1);
359 static void max3110_console_receive(struct uart_max3110 *max)
361 int loop = 1, num, total = 0;
362 u8 recv_buf[512], *pbuf;
366 num = max3110_read_multi(max, 8, pbuf);
374 receive_chars(max, recv_buf, total);
382 receive_chars(max, recv_buf, total);
385 static int max3110_main_thread(void *_max)
387 struct uart_max3110 *max = _max;
388 wait_queue_head_t *wq = &max->wq;
390 struct circ_buf *xmit = &max->con_xmit;
392 init_waitqueue_head(wq);
393 pr_info(PR_FMT "start main thread\n");
396 wait_event_interruptible(*wq, (atomic_read(&max->irq_pending) ||
397 atomic_read(&max->con_tx_need) ||
398 atomic_read(&max->uart_tx_need)) ||
399 kthread_should_stop());
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);
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);
415 /* handle uart output */
416 if (atomic_read(&max->uart_tx_need)) {
418 atomic_set(&max->uart_tx_need, 0);
421 } while (!kthread_should_stop());
426 #ifdef CONFIG_MRST_MAX3110_IRQ
427 static irqreturn_t serial_m3110_irq(int irq, void *dev_id)
429 struct uart_max3110 *max = dev_id;
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);
440 /* if don't use RX IRQ, then need a thread to polling read */
441 static int max3110_read_thread(void *_max)
443 struct uart_max3110 *max = _max;
445 pr_info(PR_FMT "start read thread\n");
447 if (!max->mthread_up)
448 max3110_console_receive(max);
450 set_current_state(TASK_INTERRUPTIBLE);
451 schedule_timeout(HZ / 20);
452 } while (!kthread_should_stop());
458 static int serial_m3110_startup(struct uart_port *port)
460 struct uart_max3110 *max =
461 container_of(port, struct uart_max3110, port);
466 pr_err(PR_FMT "uart port startup failed\n");
468 /* firstly disable all IRQ and config it to 115200, 8n1 */
469 config = WC_TAG | WC_FIFO_ENABLE
473 ret = max3110_out(max, config);
475 /* as we use thread to handle tx/rx, need set low latency */
476 port->state->port.tty->low_latency = 1;
478 #ifdef CONFIG_MRST_MAX3110_IRQ
479 ret = request_irq(max->irq, serial_m3110_irq,
480 IRQ_TYPE_EDGE_FALLING, "max3110", max);
484 /* enable RX IRQ only */
485 config |= WC_RXA_IRQ_ENABLE;
486 max3110_out(max, config);
488 /* if IRQ is disabled, start a read thread for input data */
490 kthread_run(max3110_read_thread, max, "max3110_read");
493 max->cur_conf = config;
497 static void serial_m3110_shutdown(struct uart_port *port)
499 struct uart_max3110 *max =
500 container_of(port, struct uart_max3110, port);
503 if (max->read_thread) {
504 kthread_stop(max->read_thread);
505 max->read_thread = NULL;
508 #ifdef CONFIG_MRST_MAX3110_IRQ
509 free_irq(max->irq, max);
512 /* Disable interrupts from this port */
513 config = WC_TAG | WC_SW_SHDI;
514 max3110_out(max, config);
517 static void serial_m3110_release_port(struct uart_port *port)
521 static int serial_m3110_request_port(struct uart_port *port)
526 static void serial_m3110_config_port(struct uart_port *port, int flags)
528 /* give it fake type */
529 port->type = PORT_PXA;
533 serial_m3110_verify_port(struct uart_port *port, struct serial_struct *ser)
535 /* we don't want the core code to modify any port params */
540 static const char *serial_m3110_type(struct uart_port *port)
542 struct uart_max3110 *max =
543 container_of(port, struct uart_max3110, port);
548 serial_m3110_set_termios(struct uart_port *port, struct ktermios *termios,
549 struct ktermios *old)
551 struct uart_max3110 *max =
552 container_of(port, struct uart_max3110, port);
554 unsigned int baud, parity = 0;
556 u16 new_conf = max->cur_conf;
558 switch (termios->c_cflag & CSIZE) {
560 cval = UART_LCR_WLEN7;
561 new_conf |= WC_7BIT_WORD;
565 cval = UART_LCR_WLEN8;
566 new_conf |= WC_8BIT_WORD;
570 baud = uart_get_baud_rate(port, termios, old, 0, 230400);
572 /* first calc the div for 1.8MHZ clock case */
575 clk_div = WC_BAUD_DR384;
578 clk_div = WC_BAUD_DR192;
581 clk_div = WC_BAUD_DR96;
584 clk_div = WC_BAUD_DR48;
587 clk_div = WC_BAUD_DR24;
590 clk_div = WC_BAUD_DR12;
593 clk_div = WC_BAUD_DR6;
596 clk_div = WC_BAUD_DR3;
599 clk_div = WC_BAUD_DR2;
602 clk_div = WC_BAUD_DR1;
605 if (max->clock & MAX3110_HIGH_CLK)
608 /* pick the previous baud rate */
610 clk_div = max->cur_conf & WC_BAUD_DIV_MASK;
611 tty_termios_encode_baud_rate(termios, baud, baud);
614 if (max->clock & MAX3110_HIGH_CLK) {
616 /* high clk version max3110 doesn't support B300 */
620 clk_div = WC_BAUD_DR1;
621 tty_termios_encode_baud_rate(termios, baud, baud);
624 new_conf = (new_conf & ~WC_BAUD_DIV_MASK) | clk_div;
625 if (termios->c_cflag & CSTOPB)
626 new_conf |= WC_2_STOPBITS;
628 new_conf &= ~WC_2_STOPBITS;
630 if (termios->c_cflag & PARENB) {
631 new_conf |= WC_PARITY_ENABLE;
632 parity |= UART_LCR_PARITY;
634 new_conf &= ~WC_PARITY_ENABLE;
636 if (!(termios->c_cflag & PARODD))
637 parity |= UART_LCR_EPAR;
638 max->parity = parity;
640 uart_update_timeout(port, termios->c_cflag, baud);
643 if (new_conf != max->cur_conf) {
644 max3110_out(max, new_conf);
645 max->cur_conf = new_conf;
650 /* don't handle hw handshaking */
651 static unsigned int serial_m3110_get_mctrl(struct uart_port *port)
653 return TIOCM_DSR | TIOCM_CAR | TIOCM_DSR;
656 static void serial_m3110_set_mctrl(struct uart_port *port, unsigned int mctrl)
660 static void serial_m3110_break_ctl(struct uart_port *port, int break_state)
664 static void serial_m3110_pm(struct uart_port *port, unsigned int state,
665 unsigned int oldstate)
669 static void serial_m3110_enable_ms(struct uart_port *port)
673 struct uart_ops serial_m3110_ops = {
674 .tx_empty = serial_m3110_tx_empty,
675 .set_mctrl = serial_m3110_set_mctrl,
676 .get_mctrl = serial_m3110_get_mctrl,
677 .stop_tx = serial_m3110_stop_tx,
678 .start_tx = serial_m3110_start_tx,
679 .stop_rx = serial_m3110_stop_rx,
680 .enable_ms = serial_m3110_enable_ms,
681 .break_ctl = serial_m3110_break_ctl,
682 .startup = serial_m3110_startup,
683 .shutdown = serial_m3110_shutdown,
684 .set_termios = serial_m3110_set_termios, /* must have */
685 .pm = serial_m3110_pm,
686 .type = serial_m3110_type,
687 .release_port = serial_m3110_release_port,
688 .request_port = serial_m3110_request_port,
689 .config_port = serial_m3110_config_port,
690 .verify_port = serial_m3110_verify_port,
693 static struct uart_driver serial_m3110_reg = {
694 .owner = THIS_MODULE,
695 .driver_name = "MRST serial",
700 .cons = MRST_CONSOLE,
703 static int serial_m3110_suspend(struct spi_device *spi, pm_message_t state)
708 static int serial_m3110_resume(struct spi_device *spi)
713 static struct dw_spi_chip spi0_uart = {
719 static int serial_m3110_probe(struct spi_device *spi)
721 struct uart_max3110 *max;
723 unsigned char *buffer;
725 max = kzalloc(sizeof(*max), GFP_KERNEL);
730 spi->mode = SPI_MODE_0;
731 spi->bits_per_word = 16;
732 max->clock = MAX3110_HIGH_CLK;
733 spi->controller_data = &spi0_uart;
737 max->port.type = PORT_PXA; /* need apply for a max3110 type */
738 max->port.fifosize = 2; /* only have 16b buffer */
739 max->port.ops = &serial_m3110_ops;
741 max->port.dev = &spi->dev;
742 max->port.uartclk = 115200;
745 max->name = spi->modalias; /* use spi name as the name */
746 max->irq = (u16)spi->irq;
748 spin_lock_init(&max->lock);
755 atomic_set(&max->irq_pending, 0);
757 buffer = (unsigned char *)__get_free_page(GFP_KERNEL);
762 max->con_xmit.buf = (unsigned char *)buffer;
763 max->con_xmit.head = max->con_xmit.tail = 0;
765 max->main_thread = kthread_run(max3110_main_thread,
766 max, "max3110_main");
767 if (IS_ERR(max->main_thread)) {
768 ret = PTR_ERR(max->main_thread);
773 /* give membase a psudo value to pass serial_core's check */
774 max->port.membase = (void *)0xff110000;
775 uart_add_one_port(&serial_m3110_reg, &max->port);
780 free_page((unsigned long)buffer);
787 static int max3110_remove(struct spi_device *dev)
789 struct uart_max3110 *max = pmax;
795 uart_remove_one_port(&serial_m3110_reg, &max->port);
797 free_page((unsigned long)max->con_xmit.buf);
799 if (max->main_thread)
800 kthread_stop(max->main_thread);
806 static struct spi_driver uart_max3110_driver = {
808 .name = "spi_max3111",
809 .bus = &spi_bus_type,
810 .owner = THIS_MODULE,
812 .probe = serial_m3110_probe,
813 .remove = __devexit_p(max3110_remove),
814 .suspend = serial_m3110_suspend,
815 .resume = serial_m3110_resume,
819 int __init serial_m3110_init(void)
823 ret = uart_register_driver(&serial_m3110_reg);
827 ret = spi_register_driver(&uart_max3110_driver);
829 uart_unregister_driver(&serial_m3110_reg);
834 void __exit serial_m3110_exit(void)
836 spi_unregister_driver(&uart_max3110_driver);
837 uart_unregister_driver(&serial_m3110_reg);
840 module_init(serial_m3110_init);
841 module_exit(serial_m3110_exit);
843 MODULE_LICENSE("GPL");
844 MODULE_ALIAS("max3110-uart");