2 * drivers/serial/rk2818_serial.c - driver for rk2818 serial device and console
4 * Copyright (C) 2010 ROCKCHIP, Inc.
6 * This software is licensed under the terms of the GNU General Public
7 * License version 2, as published by the Free Software Foundation, and
8 * may be copied, distributed, and modified under those terms.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
17 #if defined(CONFIG_SERIAL_RK2818_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
21 #include <linux/hrtimer.h>
22 #include <linux/module.h>
24 #include <linux/ioport.h>
25 #include <linux/irq.h>
26 #include <linux/init.h>
27 #include <linux/console.h>
28 #include <linux/tty.h>
29 #include <linux/tty_flip.h>
30 #include <linux/serial_core.h>
31 #include <linux/serial.h>
32 #include <linux/clk.h>
33 #include <linux/platform_device.h>
34 #include <mach/iomux.h>
35 #include <mach/gpio.h>
36 #include <mach/board.h>
37 #include "rk2818_serial.h"
40 * We wrap our port structure around the generic uart_port.
43 struct uart_port uart;
49 #define UART_TO_RK2818(uart_port) ((struct rk2818_port *) uart_port)
50 #define RK2818_SERIAL_MAJOR TTY_MAJOR
51 #define RK2818_SERIAL_MINOR 64
54 static inline void rk2818_uart_write(struct uart_port *port, unsigned int val,
57 __raw_writel(val, port->membase + off);
60 static inline unsigned int rk2818_uart_read(struct uart_port *port, unsigned int off)
62 return __raw_readl(port->membase + off);
65 static int rk2818_set_baud_rate(struct uart_port *port, unsigned int baud)
67 unsigned int uartTemp;
69 rk2818_uart_write(port,rk2818_uart_read(port,UART_LCR) | LCR_DLA_EN,UART_LCR);
70 uartTemp = port->uartclk / (16 * baud);
71 rk2818_uart_write(port,uartTemp & 0xff,UART_DLL);
72 rk2818_uart_write(port,(uartTemp>>8) & 0xff,UART_DLH);
73 rk2818_uart_write(port,rk2818_uart_read(port,UART_LCR) & (~LCR_DLA_EN),UART_LCR);
78 * ÅжϷ¢ËÍ»º³åÇøÊÇ·ñΪ¿Õ
79 *ÏÈÒÔFIFO´ò¿ª×ö£¬ºóÃæ¿ÉÒÔ×ö³ÉFIFO¹Ø»òFIFO¿ª¡£
81 static u_int rk2818_serial_tx_empty(struct uart_port *port)
83 while(!(rk2818_uart_read(port,UART_USR)&UART_TRANSMIT_FIFO_EMPTY))
85 if(rk2818_uart_read(port,UART_USR)&UART_TRANSMIT_FIFO_EMPTY)
94 * Power / Clock management.
96 static void rk2818_serial_pm(struct uart_port *port, unsigned int state,
97 unsigned int oldstate)
99 struct rk2818_port *rk2818_port = UART_TO_RK2818(port);
104 * Enable the peripheral clock for this serial port.
105 * This is called on uart_open() or a resume event.
107 clk_enable(rk2818_port->clk);
111 * Disable the peripheral clock for this serial port.
112 * This is called on uart_close() or a suspend event.
114 clk_disable(rk2818_port->clk);
117 printk(KERN_ERR "rk2818_serial: unknown pm %d\n", state);
122 * Return string describing the specified port
124 static const char *rk2818_serial_type(struct uart_port *port)
126 return (port->type == PORT_RK2818) ? "RK2818_SERIAL" : NULL;
129 static void rk2818_serial_enable_ms(struct uart_port *port)
132 printk("Enter::%s\n",__FUNCTION__);
136 /* no modem control lines */
137 static unsigned int rk2818_serial_get_mctrl(struct uart_port *port)
139 unsigned int result = 0;
142 status = rk2818_uart_read(port,UART_MSR);
143 if (status & UART_MSR_URCTS)
145 result = TIOCM_CAR | TIOCM_DSR | TIOCM_CTS;
146 printk("UART_GET_MSR:0x%x\n",result);
148 result = TIOCM_CAR | TIOCM_DSR;
149 printk("UART_GET_MSR:0x%x\n",result);
154 static void rk2818_serial_set_mctrl(struct uart_port *port, unsigned int mctrl)
157 printk("Enter::%s\n",__FUNCTION__);
164 static void rk2818_serial_stop_tx(struct uart_port *port)
167 printk("Enter::%s\n",__FUNCTION__);
172 * Start transmitting.
174 static void rk2818_serial_start_tx(struct uart_port *port)
176 struct circ_buf *xmit = &port->state->xmit;
177 while(!(uart_circ_empty(xmit)))
179 while (!(rk2818_uart_read(port,UART_USR) & UART_TRANSMIT_FIFO_NOT_FULL)){
180 rk2818_uart_write(port,UART_IER_RECV_DATA_AVAIL_INT_ENABLE|UART_IER_SEND_EMPTY_INT_ENABLE,UART_IER);
183 rk2818_uart_write(port,xmit->buf[xmit->tail],UART_THR);
184 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
187 if((uart_circ_empty(xmit)))
188 rk2818_uart_write(port,UART_IER_RECV_DATA_AVAIL_INT_ENABLE,UART_IER);
189 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
190 uart_write_wakeup(port);
194 * Stop receiving - port is in process of being closed.
196 static void rk2818_serial_stop_rx(struct uart_port *port)
199 printk("Enter::%s\n",__FUNCTION__);
204 * Control the transmission of a break signal
206 static void rk2818_serial_break_ctl(struct uart_port *port, int break_state)
209 temp = rk2818_uart_read(port,UART_LCR);
210 if (break_state != 0)
211 temp = temp & (~BREAK_CONTROL_BIT);/* start break */
213 temp = temp | BREAK_CONTROL_BIT; /* stop break */
214 rk2818_uart_write(port,temp,UART_LCR);
219 * Characters received (called from interrupt handler)
221 static void rk2818_rx_chars(struct uart_port *port)
223 unsigned int ch, flag;
224 while((rk2818_uart_read(port,UART_USR) & UART_RECEIVE_FIFO_NOT_EMPTY) == UART_RECEIVE_FIFO_NOT_EMPTY)
226 u32 lsr = rk2818_uart_read(port, UART_LSR);
227 ch = rk2818_uart_read(port,UART_RBR);
230 if (lsr & UART_BREAK_INT_BIT) {
232 if (uart_handle_break(port))
235 if (uart_handle_sysrq_char(port, ch))
239 uart_insert_char(port, 0, 0, ch, flag);
241 tty_flip_buffer_push(port->state->port.tty);
248 static irqreturn_t rk2818_uart_interrupt(int irq, void *dev_id)
250 struct uart_port *port = dev_id;
251 unsigned int status, pending;
253 status = rk2818_uart_read(port,UART_IIR);
254 pending = status & 0x0f;
255 if((pending == UART_IIR_RECV_AVAILABLE) || (pending == UART_IIR_CHAR_TIMEOUT))
256 rk2818_rx_chars(port);
257 if(pending == UART_IIR_THR_EMPTY)
258 rk2818_serial_start_tx(port);
265 static void rk2818_serial_shutdown(struct uart_port *port)
267 struct rk2818_port *rk2818_port = UART_TO_RK2818(port);
268 rk2818_uart_write(port,0x00,UART_IER);
269 clk_disable(rk2818_port->clk);
270 free_irq(port->irq, port);
273 * Perform initialization and enable port for reception
275 static int rk2818_serial_startup(struct uart_port *port)
277 struct rk2818_port *rk2818_port = UART_TO_RK2818(port);
278 struct tty_struct *tty = port->state->port.tty;
281 retval = request_irq(port->irq,rk2818_uart_interrupt,IRQF_SHARED,
282 tty ? tty->name : "rk2818_serial",port);
285 printk("\nrk2818_serial_startup err \n");
286 rk2818_serial_shutdown(port);
289 clk_enable(rk2818_port->clk);
290 rk2818_uart_write(port,0xf1,UART_FCR);
291 rk2818_uart_write(port,0x01,UART_SFE);///enable fifo
292 rk2818_uart_write(port,UART_IER_RECV_DATA_AVAIL_INT_ENABLE,UART_IER); //enable uart recevice IRQ
297 * Change the port parameters
299 static void rk2818_serial_set_termios(struct uart_port *port, struct ktermios *termios,
300 struct ktermios *old)
303 unsigned int mode, baud;
304 unsigned int umcon,fcr;
305 /* Get current mode register */
306 mode = rk2818_uart_read(port,UART_LCR) & (BREAK_CONTROL_BIT | EVEN_PARITY_SELECT | PARITY_ENABLED
307 | ONE_HALF_OR_TWO_BIT | UART_DATABIT_MASK);
309 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk / 16);
311 switch (termios->c_cflag & CSIZE) {
327 if (termios->c_cflag & CSTOPB)
328 mode |= ONE_STOP_BIT;
331 if (termios->c_cflag & PARENB)
333 mode |= PARITY_ENABLED;
334 if (termios->c_cflag & PARODD)
339 spin_lock_irqsave(&port->lock, flags);
340 if(termios->c_cflag & CRTSCTS)
342 /*¿ªÆôuart0Ó²¼þÁ÷¿Ø*/
343 printk("start CRTSCTS control and baudrate is %d\n",baud);
344 umcon=rk2818_uart_read(port,UART_MCR);
345 printk("UART_GET_MCR umcon=0x%x\n",umcon);
346 umcon |= UART_MCR_AFCEN;
347 umcon |= UART_MCR_URRTS;
348 umcon &=~UART_SIR_ENABLE;
349 rk2818_uart_write(port,umcon,UART_MCR);
350 printk("UART_GET_MCR umcon=0x%x\n",umcon);
351 fcr=rk2818_uart_read(port,UART_FCR);
352 printk("UART_GET_MCR fcr=0x%x\n",fcr);
353 fcr |= UART_FCR_FIFO_ENABLE;
354 rk2818_uart_write(port,fcr,UART_FCR);
355 printk("UART_GET_MCR fcr=0x%x\n",fcr);
357 mode = mode | LCR_DLA_EN;
358 while(rk2818_uart_read(port,UART_USR)&UART_USR_BUSY)
360 rk2818_uart_write(port,mode,UART_LCR);
361 baud = rk2818_set_baud_rate(port, baud);
362 uart_update_timeout(port, termios->c_cflag, baud);
363 spin_unlock_irqrestore(&port->lock, flags);
367 static void rk2818_serial_release_port(struct uart_port *port)
369 struct platform_device *pdev = to_platform_device(port->dev);
370 struct resource *resource;
371 resource_size_t size;
373 resource = platform_get_resource(pdev, IORESOURCE_MEM, 0);
374 if (unlikely(!resource))
376 size = resource->end - resource->start + 1;
378 release_mem_region(port->mapbase, size);
379 iounmap(port->membase);
380 port->membase = NULL;
383 static int rk2818_serial_request_port(struct uart_port *port)
385 struct platform_device *pdev = to_platform_device(port->dev);
386 struct resource *resource;
387 resource_size_t size;
389 resource = platform_get_resource(pdev, IORESOURCE_MEM, 0);
390 if (unlikely(!resource))
392 size = resource->end - resource->start + 1;
394 if (unlikely(!request_mem_region(port->mapbase, size, "rk2818_serial")))
397 port->membase = ioremap(port->mapbase, size);
398 if (!port->membase) {
399 release_mem_region(port->mapbase, size);
407 * Configure/autoconfigure the port.
409 static void rk2818_serial_config_port(struct uart_port *port, int flags)
411 if (flags & UART_CONFIG_TYPE) {
412 port->type = PORT_RK2818;
413 rk2818_serial_request_port(port);
418 * Verify the new serial_struct (for TIOCSSERIAL).
420 static int rk2818_serial_verify_port(struct uart_port *port, struct serial_struct *ser)
423 if (ser->type != PORT_UNKNOWN && ser->type != PORT_RK2818)
425 if (port->irq != ser->irq)
427 if (ser->io_type != SERIAL_IO_MEM)
429 if (port->uartclk / 16 != ser->baud_base)
431 if ((void *)port->mapbase != ser->iomem_base)
433 if (port->iobase != ser->port)
440 #ifdef CONFIG_CONSOLE_POLL
442 * Console polling routines for writing and reading from the uart while
443 * in an interrupt or debug context.
446 static int rk2818_serial_poll_get_char(struct uart_port *port)
448 while (!((rk2818_uart_read(port, UART_USR) & UART_RECEIVE_FIFO_NOT_EMPTY) == UART_RECEIVE_FIFO_NOT_EMPTY))
450 return rk2818_uart_read(port, UART_RBR);
453 static void rk2818_serial_poll_put_char(struct uart_port *port, unsigned char c)
455 while (!(rk2818_uart_read(port, UART_USR) & UART_TRANSMIT_FIFO_NOT_FULL))
457 rk2818_uart_write(port, c, UART_THR);
459 #endif /* CONFIG_CONSOLE_POLL */
461 static struct uart_ops rk2818_uart_pops = {
462 .tx_empty = rk2818_serial_tx_empty,
463 .set_mctrl = rk2818_serial_set_mctrl,
464 .get_mctrl = rk2818_serial_get_mctrl,
465 .stop_tx = rk2818_serial_stop_tx,
466 .start_tx = rk2818_serial_start_tx,
467 .stop_rx = rk2818_serial_stop_rx,
468 .enable_ms = rk2818_serial_enable_ms,
469 .break_ctl = rk2818_serial_break_ctl,
470 .startup = rk2818_serial_startup,
471 .shutdown = rk2818_serial_shutdown,
472 .set_termios = rk2818_serial_set_termios,
473 .type = rk2818_serial_type,
474 .release_port = rk2818_serial_release_port,
475 .request_port = rk2818_serial_request_port,
476 .config_port = rk2818_serial_config_port,
477 .verify_port = rk2818_serial_verify_port,
478 .pm = rk2818_serial_pm,
479 #ifdef CONFIG_CONSOLE_POLL
480 .poll_get_char = rk2818_serial_poll_get_char,
481 .poll_put_char = rk2818_serial_poll_put_char,
486 static struct rk2818_port rk2818_uart_ports[] = {
490 .ops = &rk2818_uart_pops,
491 .flags = UPF_BOOT_AUTOCONF,
499 .ops = &rk2818_uart_pops,
500 .flags = UPF_BOOT_AUTOCONF,
508 .ops = &rk2818_uart_pops,
509 .flags = UPF_BOOT_AUTOCONF,
517 .ops = &rk2818_uart_pops,
518 .flags = UPF_BOOT_AUTOCONF,
525 #define UART_NR ARRAY_SIZE(rk2818_uart_ports)
527 static inline struct uart_port *get_port_from_line(unsigned int line)
529 return &rk2818_uart_ports[line].uart;
532 #ifdef CONFIG_SERIAL_RK2818_CONSOLE
533 static void rk2818_console_putchar(struct uart_port *port, int ch)
535 while (!(rk2818_uart_read(port,UART_USR) & UART_TRANSMIT_FIFO_NOT_FULL))
537 rk2818_uart_write(port,ch,UART_THR);
541 * Interrupts are disabled on entering
543 static void rk2818_console_write(struct console *co, const char *s, u_int count)
545 struct uart_port *port;
546 struct rk2818_port *rk2818_port;
548 BUG_ON(co->index < 0 || co->index >= UART_NR);
550 port = get_port_from_line(co->index);
551 rk2818_port = UART_TO_RK2818(port);
553 spin_lock(&port->lock);
554 uart_console_write(port, s, count, rk2818_console_putchar);
555 spin_unlock(&port->lock);
558 static int __init rk2818_console_setup(struct console *co, char *options)
560 struct uart_port *port;
561 int baud, flow, bits, parity;
563 if (unlikely(co->index >= UART_NR || co->index < 0))
566 port = get_port_from_line(co->index);
568 if (unlikely(!port->membase))
573 //rk2818_init_clock(port);
576 uart_parse_options(options, &baud, &parity, &bits, &flow);
581 rk2818_uart_write(port,rk2818_uart_read(port,UART_LCR) | LCR_WLS_8 | PARITY_DISABLED | ONE_STOP_BIT,UART_LCR); /* 8N1 */
582 if (baud < 300 || baud > 115200)
584 rk2818_set_baud_rate(port, baud);
586 printk(KERN_INFO "rk2818_serial: console setup on port %d\n", port->line);
588 return uart_set_options(port, co, baud, parity, bits, flow);
591 static struct uart_driver rk2818_uart_driver;
593 static struct console rk2818_console = {
595 .write = rk2818_console_write,
596 .device = uart_console_device,
597 .setup = rk2818_console_setup,
598 .flags = CON_PRINTBUFFER,
600 .data = &rk2818_uart_driver,
603 #define RK2818_CONSOLE (&rk2818_console)
606 #define RK2818_CONSOLE NULL
609 static struct uart_driver rk2818_uart_driver = {
610 .owner = THIS_MODULE,
611 .driver_name = "rk2818_serial",
614 .cons = RK2818_CONSOLE,
615 .major = RK2818_SERIAL_MAJOR,
616 .minor = RK2818_SERIAL_MINOR,
619 static int __devinit rk2818_serial_probe(struct platform_device *pdev)
621 struct rk2818_port *rk2818_port;
622 struct resource *resource;
623 struct uart_port *port;
624 struct rk2818_serial_platform_data *pdata = pdev->dev.platform_data;
626 if (unlikely(pdev->id < 0 || pdev->id >= UART_NR))
629 printk(KERN_INFO "rk2818_serial: detected port %d\n", pdev->id);
631 if (pdata && pdata->io_init)
634 port = get_port_from_line(pdev->id);
635 port->dev = &pdev->dev;
636 rk2818_port = UART_TO_RK2818(port);
638 rk2818_port->clk = clk_get(&pdev->dev, "uart");
639 if (unlikely(IS_ERR(rk2818_port->clk)))
640 return PTR_ERR(rk2818_port->clk);
641 port->uartclk = clk_get_rate(rk2818_port->clk);
643 resource = platform_get_resource(pdev, IORESOURCE_MEM, 0);
644 if (unlikely(!resource))
646 port->mapbase = resource->start;
648 port->irq = platform_get_irq(pdev, 0);
649 if (unlikely(port->irq < 0))
652 platform_set_drvdata(pdev, port);
654 return uart_add_one_port(&rk2818_uart_driver, port);
657 static int __devexit rk2818_serial_remove(struct platform_device *pdev)
659 struct rk2818_port *rk2818_port = platform_get_drvdata(pdev);
661 clk_put(rk2818_port->clk);
666 static struct platform_driver rk2818_platform_driver = {
667 .remove = rk2818_serial_remove,
669 .name = "rk2818_serial",
670 .owner = THIS_MODULE,
674 static int __init rk2818_serial_init(void)
677 ret = uart_register_driver(&rk2818_uart_driver);
681 ret = platform_driver_probe(&rk2818_platform_driver, rk2818_serial_probe);
683 uart_unregister_driver(&rk2818_uart_driver);
685 printk(KERN_INFO "rk2818_serial: driver initialized\n");
690 static void __exit rk2818_serial_exit(void)
692 #ifdef CONFIG_SERIAL_RK2818_CONSOLE
693 unregister_console(&rk2818_console);
695 platform_driver_unregister(&rk2818_platform_driver);
696 uart_unregister_driver(&rk2818_uart_driver);
700 * While this can be a module, if builtin it's most likely the console
701 * So let's leave module_exit but move module_init to an earlier place
703 arch_initcall(rk2818_serial_init);
704 module_exit(rk2818_serial_exit);
706 MODULE_AUTHOR("lhh lhh@rock-chips.com");
707 MODULE_DESCRIPTION("Rockchip RK2818 Serial port driver");
708 MODULE_LICENSE("GPL");