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/irq.h>
33 #include <linux/init.h>
34 #include <linux/console.h>
35 #include <linux/sysrq.h>
36 #include <linux/platform_device.h>
37 #include <linux/tty.h>
38 #include <linux/tty_flip.h>
39 #include <linux/serial_core.h>
40 #include <linux/serial_reg.h>
42 #include <linux/kthread.h>
43 #include <linux/delay.h>
44 #include <asm/atomic.h>
45 #include <linux/spi/spi.h>
46 #include <linux/spi/dw_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
;
62 struct task_struct
*main_thread
;
63 struct task_struct
*read_thread
;
64 struct mutex thread_mutex
;;
69 u8 parity
, word_7bits
;
71 unsigned long uart_flags
;
74 struct circ_buf con_xmit
;
80 /* global data structure, may need be removed */
81 struct uart_max3110
*pmax
;
82 static inline void receive_char(struct uart_max3110
*max
, u8 ch
);
83 static void receive_chars(struct uart_max3110
*max
,
84 unsigned char *str
, int len
);
85 static int max3110_read_multi(struct uart_max3110
*max
, int len
, u8
*buf
);
86 static void max3110_console_receive(struct uart_max3110
*max
);
88 int max3110_write_then_read(struct uart_max3110
*max
,
89 const u8
*txbuf
, u8
*rxbuf
, unsigned len
, int always_fast
)
91 struct spi_device
*spi
= max
->spi
;
92 struct spi_message message
;
93 struct spi_transfer x
;
99 spi_message_init(&message
);
100 memset(&x
, 0, sizeof x
);
104 spi_message_add_tail(&x
, &message
);
107 x
.speed_hz
= 3125000;
109 x
.speed_hz
= max
->baud
;
112 ret
= spi_sync(spi
, &message
);
116 /* Write a u16 to the device, and return one u16 read back */
117 int max3110_out(struct uart_max3110
*max
, const u16 out
)
122 ret
= max3110_write_then_read(max
, (u8
*)&out
, (u8
*)&tmp
, 2, 1);
126 /* If some valid data is read back */
127 if (tmp
& MAX3110_READ_DATA_AVAILABLE
)
128 receive_char(max
, (tmp
& 0xff));
133 #define MAX_READ_LEN 20
135 * This is usually used to read data from SPIC RX FIFO, which doesn't
136 * need any delay like flushing character out. It returns how many
137 * valide bytes are read back
139 static int max3110_read_multi(struct uart_max3110
*max
, int len
, u8
*buf
)
141 u16 out
[MAX_READ_LEN
], in
[MAX_READ_LEN
];
142 u8
*pbuf
, valid_str
[MAX_READ_LEN
];
145 if (len
> MAX_READ_LEN
) {
146 pr_err(PR_FMT
"read len %d is too large\n", len
);
151 memset(out
, 0, bytelen
);
152 memset(in
, 0, bytelen
);
154 if (max3110_write_then_read(max
, (u8
*)out
, (u8
*)in
, bytelen
, 1))
157 /* If caller don't provide a buffer, then handle received char */
158 pbuf
= buf
? buf
: valid_str
;
160 for (i
= 0, j
= 0; i
< len
; i
++) {
161 if (in
[i
] & MAX3110_READ_DATA_AVAILABLE
)
162 pbuf
[j
++] = (u8
)(in
[i
] & 0xff);
165 if (j
&& (pbuf
== valid_str
))
166 receive_chars(max
, valid_str
, j
);
171 static void serial_m3110_con_putchar(struct uart_port
*port
, int ch
)
173 struct uart_max3110
*max
=
174 container_of(port
, struct uart_max3110
, port
);
175 struct circ_buf
*xmit
= &max
->con_xmit
;
177 if (uart_circ_chars_free(xmit
)) {
178 xmit
->buf
[xmit
->head
] = (char)ch
;
179 xmit
->head
= (xmit
->head
+ 1) & (PAGE_SIZE
- 1);
183 if (!test_and_set_bit(CON_TX_NEEDED
, &max
->uart_flags
))
184 wake_up_process(max
->main_thread
);
188 * Print a string to the serial port trying not to disturb
189 * any possible real use of the port...
191 * The console_lock must be held when we get here.
193 static void serial_m3110_con_write(struct console
*co
,
194 const char *s
, unsigned int count
)
199 uart_console_write(&pmax
->port
, s
, count
, serial_m3110_con_putchar
);
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");
214 pr_err(PR_FMT
"pmax is NULL, return");
219 uart_parse_options(options
, &baud
, &parity
, &bits
, &flow
);
221 return uart_set_options(&max
->port
, co
, baud
, parity
, bits
, flow
);
224 static struct tty_driver
*serial_m3110_con_device(struct console
*co
,
227 struct uart_driver
*p
= co
->data
;
229 return p
->tty_driver
;
232 static struct uart_driver serial_m3110_reg
;
233 static struct console serial_m3110_console
= {
235 .write
= serial_m3110_con_write
,
236 .device
= serial_m3110_con_device
,
237 .setup
= serial_m3110_con_setup
,
238 .flags
= CON_PRINTBUFFER
,
240 .data
= &serial_m3110_reg
,
243 #define MRST_CONSOLE (&serial_m3110_console)
245 static unsigned int serial_m3110_tx_empty(struct uart_port
*port
)
250 static void serial_m3110_stop_tx(struct uart_port
*port
)
255 /* stop_rx will be called in spin_lock env */
256 static void serial_m3110_stop_rx(struct uart_port
*port
)
261 #define WORDS_PER_XFER 128
262 static inline void send_circ_buf(struct uart_max3110
*max
,
263 struct circ_buf
*xmit
)
266 u16 obuf
[WORDS_PER_XFER
], ibuf
[WORDS_PER_XFER
];
267 u8 valid_str
[WORDS_PER_XFER
];
270 while (!uart_circ_empty(xmit
)) {
271 left
= uart_circ_chars_pending(xmit
);
273 len
= (left
>= WORDS_PER_XFER
) ? WORDS_PER_XFER
: left
;
275 memset(obuf
, 0, len
* 2);
276 memset(ibuf
, 0, len
* 2);
277 for (i
= 0; i
< len
; i
++) {
278 obuf
[i
] = (u8
)xmit
->buf
[xmit
->tail
] | WD_TAG
;
279 xmit
->tail
= (xmit
->tail
+ 1) &
280 (UART_XMIT_SIZE
- 1);
282 max3110_write_then_read(max
, (u8
*)obuf
,
283 (u8
*)ibuf
, len
* 2, 0);
285 for (i
= 0, j
= 0; i
< len
; i
++) {
286 if (ibuf
[i
] & MAX3110_READ_DATA_AVAILABLE
)
287 valid_str
[j
++] = (u8
)(ibuf
[i
] & 0xff);
291 receive_chars(max
, valid_str
, j
);
293 max
->port
.icount
.tx
+= len
;
299 static void transmit_char(struct uart_max3110
*max
)
301 struct uart_port
*port
= &max
->port
;
302 struct circ_buf
*xmit
= &port
->state
->xmit
;
304 if (uart_circ_empty(xmit
) || uart_tx_stopped(port
))
307 send_circ_buf(max
, xmit
);
309 if (uart_circ_chars_pending(xmit
) < WAKEUP_CHARS
)
310 uart_write_wakeup(port
);
312 if (uart_circ_empty(xmit
))
313 serial_m3110_stop_tx(port
);
316 /* This will be called by uart_write() and tty_write, can't
318 static void serial_m3110_start_tx(struct uart_port
*port
)
320 struct uart_max3110
*max
=
321 container_of(port
, struct uart_max3110
, port
);
323 if (!test_and_set_bit(UART_TX_NEEDED
, &max
->uart_flags
))
324 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
, max
->uart_flags
|| kthread_should_stop());
397 mutex_lock(&max
->thread_mutex
);
399 if (test_and_clear_bit(BIT_IRQ_PENDING
, &max
->uart_flags
))
400 max3110_console_receive(max
);
402 /* first handle console output */
403 if (test_and_clear_bit(CON_TX_NEEDED
, &max
->uart_flags
))
404 send_circ_buf(max
, xmit
);
406 /* handle uart output */
407 if (test_and_clear_bit(UART_TX_NEEDED
, &max
->uart_flags
))
410 mutex_unlock(&max
->thread_mutex
);
412 } while (!kthread_should_stop());
417 #ifdef CONFIG_MRST_MAX3110_IRQ
418 static irqreturn_t
serial_m3110_irq(int irq
, void *dev_id
)
420 struct uart_max3110
*max
= dev_id
;
422 /* max3110's irq is a falling edge, not level triggered,
423 * so no need to disable the irq */
424 if (!test_and_set_bit(BIT_IRQ_PENDING
, &max
->uart_flags
))
425 wake_up_process(max
->main_thread
);
430 /* if don't use RX IRQ, then need a thread to polling read */
431 static int max3110_read_thread(void *_max
)
433 struct uart_max3110
*max
= _max
;
435 pr_info(PR_FMT
"start read thread\n");
437 mutex_lock(&max
->thread_mutex
);
438 max3110_console_receive(max
);
439 mutex_unlock(&max
->thread_mutex
);
441 set_current_state(TASK_INTERRUPTIBLE
);
442 schedule_timeout(HZ
/ 20);
443 } while (!kthread_should_stop());
449 static int serial_m3110_startup(struct uart_port
*port
)
451 struct uart_max3110
*max
=
452 container_of(port
, struct uart_max3110
, port
);
457 pr_err(PR_FMT
"uart port startup failed\n");
459 /* firstly disable all IRQ and config it to 115200, 8n1 */
460 config
= WC_TAG
| WC_FIFO_ENABLE
464 ret
= max3110_out(max
, config
);
466 /* as we use thread to handle tx/rx, need set low latency */
467 port
->state
->port
.tty
->low_latency
= 1;
469 #ifdef CONFIG_MRST_MAX3110_IRQ
470 ret
= request_irq(max
->irq
, serial_m3110_irq
,
471 IRQ_TYPE_EDGE_FALLING
, "max3110", max
);
475 /* enable RX IRQ only */
476 config
|= WC_RXA_IRQ_ENABLE
;
477 max3110_out(max
, config
);
479 /* if IRQ is disabled, start a read thread for input data */
481 kthread_run(max3110_read_thread
, max
, "max3110_read");
484 max
->cur_conf
= config
;
488 static void serial_m3110_shutdown(struct uart_port
*port
)
490 struct uart_max3110
*max
=
491 container_of(port
, struct uart_max3110
, port
);
494 if (max
->read_thread
) {
495 kthread_stop(max
->read_thread
);
496 max
->read_thread
= NULL
;
499 #ifdef CONFIG_MRST_MAX3110_IRQ
500 free_irq(max
->irq
, max
);
503 /* Disable interrupts from this port */
504 config
= WC_TAG
| WC_SW_SHDI
;
505 max3110_out(max
, config
);
508 static void serial_m3110_release_port(struct uart_port
*port
)
512 static int serial_m3110_request_port(struct uart_port
*port
)
517 static void serial_m3110_config_port(struct uart_port
*port
, int flags
)
519 /* give it fake type */
520 port
->type
= PORT_PXA
;
524 serial_m3110_verify_port(struct uart_port
*port
, struct serial_struct
*ser
)
526 /* we don't want the core code to modify any port params */
531 static const char *serial_m3110_type(struct uart_port
*port
)
533 struct uart_max3110
*max
=
534 container_of(port
, struct uart_max3110
, port
);
539 serial_m3110_set_termios(struct uart_port
*port
, struct ktermios
*termios
,
540 struct ktermios
*old
)
542 struct uart_max3110
*max
=
543 container_of(port
, struct uart_max3110
, port
);
545 unsigned int baud
, parity
= 0;
547 u16 new_conf
= max
->cur_conf
;
549 switch (termios
->c_cflag
& CSIZE
) {
551 cval
= UART_LCR_WLEN7
;
552 new_conf
|= WC_7BIT_WORD
;
556 cval
= UART_LCR_WLEN8
;
557 new_conf
|= WC_8BIT_WORD
;
561 baud
= uart_get_baud_rate(port
, termios
, old
, 0, 230400);
563 /* first calc the div for 1.8MHZ clock case */
566 clk_div
= WC_BAUD_DR384
;
569 clk_div
= WC_BAUD_DR192
;
572 clk_div
= WC_BAUD_DR96
;
575 clk_div
= WC_BAUD_DR48
;
578 clk_div
= WC_BAUD_DR24
;
581 clk_div
= WC_BAUD_DR12
;
584 clk_div
= WC_BAUD_DR6
;
587 clk_div
= WC_BAUD_DR3
;
590 clk_div
= WC_BAUD_DR2
;
593 clk_div
= WC_BAUD_DR1
;
596 if (max
->clock
& MAX3110_HIGH_CLK
)
599 /* pick the previous baud rate */
601 clk_div
= max
->cur_conf
& WC_BAUD_DIV_MASK
;
602 tty_termios_encode_baud_rate(termios
, baud
, baud
);
605 if (max
->clock
& MAX3110_HIGH_CLK
) {
607 /* high clk version max3110 doesn't support B300 */
611 clk_div
= WC_BAUD_DR1
;
612 tty_termios_encode_baud_rate(termios
, baud
, baud
);
615 new_conf
= (new_conf
& ~WC_BAUD_DIV_MASK
) | clk_div
;
616 if (termios
->c_cflag
& CSTOPB
)
617 new_conf
|= WC_2_STOPBITS
;
619 new_conf
&= ~WC_2_STOPBITS
;
621 if (termios
->c_cflag
& PARENB
) {
622 new_conf
|= WC_PARITY_ENABLE
;
623 parity
|= UART_LCR_PARITY
;
625 new_conf
&= ~WC_PARITY_ENABLE
;
627 if (!(termios
->c_cflag
& PARODD
))
628 parity
|= UART_LCR_EPAR
;
629 max
->parity
= parity
;
631 uart_update_timeout(port
, termios
->c_cflag
, baud
);
634 if (new_conf
!= max
->cur_conf
) {
635 max3110_out(max
, new_conf
);
636 max
->cur_conf
= new_conf
;
641 /* don't handle hw handshaking */
642 static unsigned int serial_m3110_get_mctrl(struct uart_port
*port
)
644 return TIOCM_DSR
| TIOCM_CAR
| TIOCM_DSR
;
647 static void serial_m3110_set_mctrl(struct uart_port
*port
, unsigned int mctrl
)
651 static void serial_m3110_break_ctl(struct uart_port
*port
, int break_state
)
655 static void serial_m3110_pm(struct uart_port
*port
, unsigned int state
,
656 unsigned int oldstate
)
660 static void serial_m3110_enable_ms(struct uart_port
*port
)
664 struct uart_ops serial_m3110_ops
= {
665 .tx_empty
= serial_m3110_tx_empty
,
666 .set_mctrl
= serial_m3110_set_mctrl
,
667 .get_mctrl
= serial_m3110_get_mctrl
,
668 .stop_tx
= serial_m3110_stop_tx
,
669 .start_tx
= serial_m3110_start_tx
,
670 .stop_rx
= serial_m3110_stop_rx
,
671 .enable_ms
= serial_m3110_enable_ms
,
672 .break_ctl
= serial_m3110_break_ctl
,
673 .startup
= serial_m3110_startup
,
674 .shutdown
= serial_m3110_shutdown
,
675 .set_termios
= serial_m3110_set_termios
, /* must have */
676 .pm
= serial_m3110_pm
,
677 .type
= serial_m3110_type
,
678 .release_port
= serial_m3110_release_port
,
679 .request_port
= serial_m3110_request_port
,
680 .config_port
= serial_m3110_config_port
,
681 .verify_port
= serial_m3110_verify_port
,
684 static struct uart_driver serial_m3110_reg
= {
685 .owner
= THIS_MODULE
,
686 .driver_name
= "MRST serial",
691 .cons
= MRST_CONSOLE
,
694 static int serial_m3110_suspend(struct spi_device
*spi
, pm_message_t state
)
699 static int serial_m3110_resume(struct spi_device
*spi
)
704 static struct dw_spi_chip spi0_uart
= {
710 static int serial_m3110_probe(struct spi_device
*spi
)
712 struct uart_max3110
*max
;
714 unsigned char *buffer
;
716 max
= kzalloc(sizeof(*max
), GFP_KERNEL
);
721 spi
->mode
= SPI_MODE_0
;
722 spi
->bits_per_word
= 16;
723 max
->clock
= MAX3110_HIGH_CLK
;
724 spi
->controller_data
= &spi0_uart
;
728 max
->port
.type
= PORT_PXA
; /* need apply for a max3110 type */
729 max
->port
.fifosize
= 2; /* only have 16b buffer */
730 max
->port
.ops
= &serial_m3110_ops
;
732 max
->port
.dev
= &spi
->dev
;
733 max
->port
.uartclk
= 115200;
736 max
->name
= spi
->modalias
; /* use spi name as the name */
737 max
->irq
= (u16
)spi
->irq
;
739 mutex_init(&max
->thread_mutex
);
748 /* Check if reading configuration register returns something sane */
751 ret
= max3110_write_then_read(max
, (u8
*)&res
, (u8
*)&res
, 2, 0);
752 if (ret
< 0 || res
== 0 || res
== 0xffff) {
753 printk(KERN_ERR
"MAX3111 deemed not present (conf reg %04x)",
758 buffer
= (unsigned char *)__get_free_page(GFP_KERNEL
);
763 max
->con_xmit
.buf
= (unsigned char *)buffer
;
764 max
->con_xmit
.head
= max
->con_xmit
.tail
= 0;
766 max
->main_thread
= kthread_run(max3110_main_thread
,
767 max
, "max3110_main");
768 if (IS_ERR(max
->main_thread
)) {
769 ret
= PTR_ERR(max
->main_thread
);
774 /* give membase a psudo value to pass serial_core's check */
775 max
->port
.membase
= (void *)0xff110000;
776 uart_add_one_port(&serial_m3110_reg
, &max
->port
);
781 free_page((unsigned long)buffer
);
788 static int max3110_remove(struct spi_device
*dev
)
790 struct uart_max3110
*max
= pmax
;
796 uart_remove_one_port(&serial_m3110_reg
, &max
->port
);
798 free_page((unsigned long)max
->con_xmit
.buf
);
800 if (max
->main_thread
)
801 kthread_stop(max
->main_thread
);
807 static struct spi_driver uart_max3110_driver
= {
809 .name
= "spi_max3111",
810 .bus
= &spi_bus_type
,
811 .owner
= THIS_MODULE
,
813 .probe
= serial_m3110_probe
,
814 .remove
= __devexit_p(max3110_remove
),
815 .suspend
= serial_m3110_suspend
,
816 .resume
= serial_m3110_resume
,
820 int __init
serial_m3110_init(void)
824 ret
= uart_register_driver(&serial_m3110_reg
);
828 ret
= spi_register_driver(&uart_max3110_driver
);
830 uart_unregister_driver(&serial_m3110_reg
);
835 void __exit
serial_m3110_exit(void)
837 spi_unregister_driver(&uart_max3110_driver
);
838 uart_unregister_driver(&serial_m3110_reg
);
841 module_init(serial_m3110_init
);
842 module_exit(serial_m3110_exit
);
844 MODULE_LICENSE("GPL");
845 MODULE_ALIAS("max3110-uart");