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;
59 struct mutex thread_mutex;;
64 u8 parity, word_7bits;
66 atomic_t uart_tx_need;
69 struct circ_buf con_xmit;
77 /* global data structure, may need be removed */
78 struct uart_max3110 *pmax;
79 static inline void receive_char(struct uart_max3110 *max, u8 ch);
80 static void receive_chars(struct uart_max3110 *max,
81 unsigned char *str, int len);
82 static int max3110_read_multi(struct uart_max3110 *max, int len, u8 *buf);
83 static void max3110_console_receive(struct uart_max3110 *max);
85 int max3110_write_then_read(struct uart_max3110 *max,
86 const u8 *txbuf, u8 *rxbuf, unsigned len, int always_fast)
88 struct spi_device *spi = max->spi;
89 struct spi_message message;
90 struct spi_transfer x;
96 spi_message_init(&message);
97 memset(&x, 0, sizeof x);
101 spi_message_add_tail(&x, &message);
104 x.speed_hz = 3125000;
106 x.speed_hz = max->baud;
109 ret = spi_sync(spi, &message);
113 /* Write a u16 to the device, and return one u16 read back */
114 int max3110_out(struct uart_max3110 *max, const u16 out)
119 ret = max3110_write_then_read(max, (u8 *)&out, (u8 *)&tmp, 2, 1);
123 /* If some valid data is read back */
124 if (tmp & MAX3110_READ_DATA_AVAILABLE)
125 receive_char(max, (tmp & 0xff));
130 #define MAX_READ_LEN 20
132 * This is usually used to read data from SPIC RX FIFO, which doesn't
133 * need any delay like flushing character out. It returns how many
134 * valide bytes are read back
136 static int max3110_read_multi(struct uart_max3110 *max, int len, u8 *buf)
138 u16 out[MAX_READ_LEN], in[MAX_READ_LEN];
139 u8 *pbuf, valid_str[MAX_READ_LEN];
142 if (len > MAX_READ_LEN) {
143 pr_err(PR_FMT "read len %d is too large\n", len);
148 memset(out, 0, bytelen);
149 memset(in, 0, bytelen);
151 if (max3110_write_then_read(max, (u8 *)out, (u8 *)in, bytelen, 1))
154 /* If caller don't provide a buffer, then handle received char */
155 pbuf = buf ? buf : valid_str;
157 for (i = 0, j = 0; i < len; i++) {
158 if (in[i] & MAX3110_READ_DATA_AVAILABLE)
159 pbuf[j++] = (u8)(in[i] & 0xff);
162 if (j && (pbuf == valid_str))
163 receive_chars(max, valid_str, j);
168 static void serial_m3110_con_putchar(struct uart_port *port, int ch)
170 struct uart_max3110 *max =
171 container_of(port, struct uart_max3110, port);
172 struct circ_buf *xmit = &max->con_xmit;
174 if (uart_circ_chars_free(xmit)) {
175 xmit->buf[xmit->head] = (char)ch;
176 xmit->head = (xmit->head + 1) & (PAGE_SIZE - 1);
179 if (!atomic_read(&max->con_tx_need)) {
180 atomic_set(&max->con_tx_need, 1);
181 wake_up_process(max->main_thread);
186 * Print a string to the serial port trying not to disturb
187 * any possible real use of the port...
189 * The console_lock must be held when we get here.
191 static void serial_m3110_con_write(struct console *co,
192 const char *s, unsigned int count)
197 uart_console_write(&pmax->port, s, count, serial_m3110_con_putchar);
201 serial_m3110_con_setup(struct console *co, char *options)
203 struct uart_max3110 *max = pmax;
209 pr_info(PR_FMT "setting up console\n");
212 pr_err(PR_FMT "pmax is NULL, return");
217 uart_parse_options(options, &baud, &parity, &bits, &flow);
219 return uart_set_options(&max->port, co, baud, parity, bits, flow);
222 static struct tty_driver *serial_m3110_con_device(struct console *co,
225 struct uart_driver *p = co->data;
227 return p->tty_driver;
230 static struct uart_driver serial_m3110_reg;
231 static struct console serial_m3110_console = {
233 .write = serial_m3110_con_write,
234 .device = serial_m3110_con_device,
235 .setup = serial_m3110_con_setup,
236 .flags = CON_PRINTBUFFER,
238 .data = &serial_m3110_reg,
241 #define MRST_CONSOLE (&serial_m3110_console)
243 static unsigned int serial_m3110_tx_empty(struct uart_port *port)
248 static void serial_m3110_stop_tx(struct uart_port *port)
253 /* stop_rx will be called in spin_lock env */
254 static void serial_m3110_stop_rx(struct uart_port *port)
259 #define WORDS_PER_XFER 128
260 static inline void send_circ_buf(struct uart_max3110 *max,
261 struct circ_buf *xmit)
264 u16 obuf[WORDS_PER_XFER], ibuf[WORDS_PER_XFER];
265 u8 valid_str[WORDS_PER_XFER];
268 while (!uart_circ_empty(xmit)) {
269 left = uart_circ_chars_pending(xmit);
271 len = (left >= WORDS_PER_XFER) ? WORDS_PER_XFER : left;
273 memset(obuf, 0, len * 2);
274 memset(ibuf, 0, len * 2);
275 for (i = 0; i < len; i++) {
276 obuf[i] = (u8)xmit->buf[xmit->tail] | WD_TAG;
277 xmit->tail = (xmit->tail + 1) &
278 (UART_XMIT_SIZE - 1);
280 max3110_write_then_read(max, (u8 *)obuf,
281 (u8 *)ibuf, len * 2, 0);
283 for (i = 0, j = 0; i < len; i++) {
284 if (ibuf[i] & MAX3110_READ_DATA_AVAILABLE)
285 valid_str[j++] = (u8)(ibuf[i] & 0xff);
289 receive_chars(max, valid_str, j);
291 max->port.icount.tx += len;
297 static void transmit_char(struct uart_max3110 *max)
299 struct uart_port *port = &max->port;
300 struct circ_buf *xmit = &port->state->xmit;
302 if (uart_circ_empty(xmit) || uart_tx_stopped(port))
305 send_circ_buf(max, xmit);
307 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
308 uart_write_wakeup(port);
310 if (uart_circ_empty(xmit))
311 serial_m3110_stop_tx(port);
314 /* This will be called by uart_write() and tty_write, can't
316 static void serial_m3110_start_tx(struct uart_port *port)
318 struct uart_max3110 *max =
319 container_of(port, struct uart_max3110, port);
321 if (!atomic_read(&max->uart_tx_need)) {
322 atomic_set(&max->uart_tx_need, 1);
323 wake_up_process(max->main_thread);
327 static void receive_chars(struct uart_max3110 *max, unsigned char *str, int len)
329 struct uart_port *port = &max->port;
330 struct tty_struct *tty;
333 /* If uart is not opened, just return */
337 tty = port->state->port.tty;
339 return; /* receive some char before the tty is opened */
342 usable = tty_buffer_request_room(tty, len);
344 tty_insert_flip_string(tty, str, usable);
346 port->icount.rx += usable;
347 tty_flip_buffer_push(tty);
353 static inline void receive_char(struct uart_max3110 *max, u8 ch)
355 receive_chars(max, &ch, 1);
358 static void max3110_console_receive(struct uart_max3110 *max)
360 int loop = 1, num, total = 0;
361 u8 recv_buf[512], *pbuf;
365 num = max3110_read_multi(max, 8, pbuf);
373 receive_chars(max, recv_buf, total);
381 receive_chars(max, recv_buf, total);
384 static int max3110_main_thread(void *_max)
386 struct uart_max3110 *max = _max;
387 wait_queue_head_t *wq = &max->wq;
389 struct circ_buf *xmit = &max->con_xmit;
391 init_waitqueue_head(wq);
392 pr_info(PR_FMT "start main thread\n");
395 wait_event_interruptible(*wq, (atomic_read(&max->irq_pending) ||
396 atomic_read(&max->con_tx_need) ||
397 atomic_read(&max->uart_tx_need)) ||
398 kthread_should_stop());
400 mutex_lock(&max->thread_mutex);
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);
420 mutex_unlock(&max->thread_mutex);
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 mutex_lock(&max->thread_mutex);
448 max3110_console_receive(max);
449 mutex_unlock(&max->thread_mutex);
451 set_current_state(TASK_INTERRUPTIBLE);
452 schedule_timeout(HZ / 20);
453 } while (!kthread_should_stop());
459 static int serial_m3110_startup(struct uart_port *port)
461 struct uart_max3110 *max =
462 container_of(port, struct uart_max3110, port);
467 pr_err(PR_FMT "uart port startup failed\n");
469 /* firstly disable all IRQ and config it to 115200, 8n1 */
470 config = WC_TAG | WC_FIFO_ENABLE
474 ret = max3110_out(max, config);
476 /* as we use thread to handle tx/rx, need set low latency */
477 port->state->port.tty->low_latency = 1;
479 #ifdef CONFIG_MRST_MAX3110_IRQ
480 ret = request_irq(max->irq, serial_m3110_irq,
481 IRQ_TYPE_EDGE_FALLING, "max3110", max);
485 /* enable RX IRQ only */
486 config |= WC_RXA_IRQ_ENABLE;
487 max3110_out(max, config);
489 /* if IRQ is disabled, start a read thread for input data */
491 kthread_run(max3110_read_thread, max, "max3110_read");
494 max->cur_conf = config;
498 static void serial_m3110_shutdown(struct uart_port *port)
500 struct uart_max3110 *max =
501 container_of(port, struct uart_max3110, port);
504 if (max->read_thread) {
505 kthread_stop(max->read_thread);
506 max->read_thread = NULL;
509 #ifdef CONFIG_MRST_MAX3110_IRQ
510 free_irq(max->irq, max);
513 /* Disable interrupts from this port */
514 config = WC_TAG | WC_SW_SHDI;
515 max3110_out(max, config);
518 static void serial_m3110_release_port(struct uart_port *port)
522 static int serial_m3110_request_port(struct uart_port *port)
527 static void serial_m3110_config_port(struct uart_port *port, int flags)
529 /* give it fake type */
530 port->type = PORT_PXA;
534 serial_m3110_verify_port(struct uart_port *port, struct serial_struct *ser)
536 /* we don't want the core code to modify any port params */
541 static const char *serial_m3110_type(struct uart_port *port)
543 struct uart_max3110 *max =
544 container_of(port, struct uart_max3110, port);
549 serial_m3110_set_termios(struct uart_port *port, struct ktermios *termios,
550 struct ktermios *old)
552 struct uart_max3110 *max =
553 container_of(port, struct uart_max3110, port);
555 unsigned int baud, parity = 0;
557 u16 new_conf = max->cur_conf;
559 switch (termios->c_cflag & CSIZE) {
561 cval = UART_LCR_WLEN7;
562 new_conf |= WC_7BIT_WORD;
566 cval = UART_LCR_WLEN8;
567 new_conf |= WC_8BIT_WORD;
571 baud = uart_get_baud_rate(port, termios, old, 0, 230400);
573 /* first calc the div for 1.8MHZ clock case */
576 clk_div = WC_BAUD_DR384;
579 clk_div = WC_BAUD_DR192;
582 clk_div = WC_BAUD_DR96;
585 clk_div = WC_BAUD_DR48;
588 clk_div = WC_BAUD_DR24;
591 clk_div = WC_BAUD_DR12;
594 clk_div = WC_BAUD_DR6;
597 clk_div = WC_BAUD_DR3;
600 clk_div = WC_BAUD_DR2;
603 clk_div = WC_BAUD_DR1;
606 if (max->clock & MAX3110_HIGH_CLK)
609 /* pick the previous baud rate */
611 clk_div = max->cur_conf & WC_BAUD_DIV_MASK;
612 tty_termios_encode_baud_rate(termios, baud, baud);
615 if (max->clock & MAX3110_HIGH_CLK) {
617 /* high clk version max3110 doesn't support B300 */
621 clk_div = WC_BAUD_DR1;
622 tty_termios_encode_baud_rate(termios, baud, baud);
625 new_conf = (new_conf & ~WC_BAUD_DIV_MASK) | clk_div;
626 if (termios->c_cflag & CSTOPB)
627 new_conf |= WC_2_STOPBITS;
629 new_conf &= ~WC_2_STOPBITS;
631 if (termios->c_cflag & PARENB) {
632 new_conf |= WC_PARITY_ENABLE;
633 parity |= UART_LCR_PARITY;
635 new_conf &= ~WC_PARITY_ENABLE;
637 if (!(termios->c_cflag & PARODD))
638 parity |= UART_LCR_EPAR;
639 max->parity = parity;
641 uart_update_timeout(port, termios->c_cflag, baud);
644 if (new_conf != max->cur_conf) {
645 max3110_out(max, new_conf);
646 max->cur_conf = new_conf;
651 /* don't handle hw handshaking */
652 static unsigned int serial_m3110_get_mctrl(struct uart_port *port)
654 return TIOCM_DSR | TIOCM_CAR | TIOCM_DSR;
657 static void serial_m3110_set_mctrl(struct uart_port *port, unsigned int mctrl)
661 static void serial_m3110_break_ctl(struct uart_port *port, int break_state)
665 static void serial_m3110_pm(struct uart_port *port, unsigned int state,
666 unsigned int oldstate)
670 static void serial_m3110_enable_ms(struct uart_port *port)
674 struct uart_ops serial_m3110_ops = {
675 .tx_empty = serial_m3110_tx_empty,
676 .set_mctrl = serial_m3110_set_mctrl,
677 .get_mctrl = serial_m3110_get_mctrl,
678 .stop_tx = serial_m3110_stop_tx,
679 .start_tx = serial_m3110_start_tx,
680 .stop_rx = serial_m3110_stop_rx,
681 .enable_ms = serial_m3110_enable_ms,
682 .break_ctl = serial_m3110_break_ctl,
683 .startup = serial_m3110_startup,
684 .shutdown = serial_m3110_shutdown,
685 .set_termios = serial_m3110_set_termios, /* must have */
686 .pm = serial_m3110_pm,
687 .type = serial_m3110_type,
688 .release_port = serial_m3110_release_port,
689 .request_port = serial_m3110_request_port,
690 .config_port = serial_m3110_config_port,
691 .verify_port = serial_m3110_verify_port,
694 static struct uart_driver serial_m3110_reg = {
695 .owner = THIS_MODULE,
696 .driver_name = "MRST serial",
701 .cons = MRST_CONSOLE,
704 static int serial_m3110_suspend(struct spi_device *spi, pm_message_t state)
709 static int serial_m3110_resume(struct spi_device *spi)
714 static struct dw_spi_chip spi0_uart = {
720 static int serial_m3110_probe(struct spi_device *spi)
722 struct uart_max3110 *max;
724 unsigned char *buffer;
726 max = kzalloc(sizeof(*max), GFP_KERNEL);
731 spi->mode = SPI_MODE_0;
732 spi->bits_per_word = 16;
733 max->clock = MAX3110_HIGH_CLK;
734 spi->controller_data = &spi0_uart;
738 max->port.type = PORT_PXA; /* need apply for a max3110 type */
739 max->port.fifosize = 2; /* only have 16b buffer */
740 max->port.ops = &serial_m3110_ops;
742 max->port.dev = &spi->dev;
743 max->port.uartclk = 115200;
746 max->name = spi->modalias; /* use spi name as the name */
747 max->irq = (u16)spi->irq;
749 mutex_init(&max->thread_mutex);
756 atomic_set(&max->irq_pending, 0);
757 /* Check if reading configuration register returns something sane */
760 ret = max3110_write_then_read(max, (u8 *)&res, (u8 *)&res, 2, 0);
761 if (ret < 0 || res == 0 || res == 0xffff) {
762 printk(KERN_ERR "MAX3111 deemed not present (conf reg %04x)",
767 buffer = (unsigned char *)__get_free_page(GFP_KERNEL);
772 max->con_xmit.buf = (unsigned char *)buffer;
773 max->con_xmit.head = max->con_xmit.tail = 0;
775 max->main_thread = kthread_run(max3110_main_thread,
776 max, "max3110_main");
777 if (IS_ERR(max->main_thread)) {
778 ret = PTR_ERR(max->main_thread);
783 /* give membase a psudo value to pass serial_core's check */
784 max->port.membase = (void *)0xff110000;
785 uart_add_one_port(&serial_m3110_reg, &max->port);
790 free_page((unsigned long)buffer);
797 static int max3110_remove(struct spi_device *dev)
799 struct uart_max3110 *max = pmax;
805 uart_remove_one_port(&serial_m3110_reg, &max->port);
807 free_page((unsigned long)max->con_xmit.buf);
809 if (max->main_thread)
810 kthread_stop(max->main_thread);
816 static struct spi_driver uart_max3110_driver = {
818 .name = "spi_max3111",
819 .bus = &spi_bus_type,
820 .owner = THIS_MODULE,
822 .probe = serial_m3110_probe,
823 .remove = __devexit_p(max3110_remove),
824 .suspend = serial_m3110_suspend,
825 .resume = serial_m3110_resume,
829 int __init serial_m3110_init(void)
833 ret = uart_register_driver(&serial_m3110_reg);
837 ret = spi_register_driver(&uart_max3110_driver);
839 uart_unregister_driver(&serial_m3110_reg);
844 void __exit serial_m3110_exit(void)
846 spi_unregister_driver(&uart_max3110_driver);
847 uart_unregister_driver(&serial_m3110_reg);
850 module_init(serial_m3110_init);
851 module_exit(serial_m3110_exit);
853 MODULE_LICENSE("GPL");
854 MODULE_ALIAS("max3110-uart");