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>
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 rk2818_mux_api_set(GPIOB2_U0CTSN_SEL_NAME, IOMUXB_UART0_CTS_N);
344 rk2818_mux_api_set(GPIOB3_U0RTSN_SEL_NAME, IOMUXB_UART0_RTS_N);
345 printk("start CRTSCTS control and baudrate is %d\n",baud);
346 umcon=rk2818_uart_read(port,UART_MCR);
347 printk("UART_GET_MCR umcon=0x%x\n",umcon);
348 umcon |= UART_MCR_AFCEN;
349 umcon |= UART_MCR_URRTS;
350 umcon &=~UART_SIR_ENABLE;
351 rk2818_uart_write(port,umcon,UART_MCR);
352 printk("UART_GET_MCR umcon=0x%x\n",umcon);
353 fcr=rk2818_uart_read(port,UART_FCR);
354 printk("UART_GET_MCR fcr=0x%x\n",fcr);
355 fcr |= UART_FCR_FIFO_ENABLE;
356 rk2818_uart_write(port,fcr,UART_FCR);
357 printk("UART_GET_MCR fcr=0x%x\n",fcr);
359 mode = mode | LCR_DLA_EN;
360 while(rk2818_uart_read(port,UART_USR)&UART_USR_BUSY)
362 rk2818_uart_write(port,mode,UART_LCR);
363 baud = rk2818_set_baud_rate(port, baud);
364 uart_update_timeout(port, termios->c_cflag, baud);
365 spin_unlock_irqrestore(&port->lock, flags);
369 static void rk2818_serial_release_port(struct uart_port *port)
371 struct platform_device *pdev = to_platform_device(port->dev);
372 struct resource *resource;
373 resource_size_t size;
375 resource = platform_get_resource(pdev, IORESOURCE_MEM, 0);
376 if (unlikely(!resource))
378 size = resource->end - resource->start + 1;
380 release_mem_region(port->mapbase, size);
381 iounmap(port->membase);
382 port->membase = NULL;
385 static int rk2818_serial_request_port(struct uart_port *port)
387 struct platform_device *pdev = to_platform_device(port->dev);
388 struct resource *resource;
389 resource_size_t size;
391 resource = platform_get_resource(pdev, IORESOURCE_MEM, 0);
392 if (unlikely(!resource))
394 size = resource->end - resource->start + 1;
396 if (unlikely(!request_mem_region(port->mapbase, size, "rk2818_serial")))
399 port->membase = ioremap(port->mapbase, size);
400 if (!port->membase) {
401 release_mem_region(port->mapbase, size);
409 * Configure/autoconfigure the port.
411 static void rk2818_serial_config_port(struct uart_port *port, int flags)
413 if (flags & UART_CONFIG_TYPE) {
414 port->type = PORT_RK2818;
415 rk2818_serial_request_port(port);
420 * Verify the new serial_struct (for TIOCSSERIAL).
422 static int rk2818_serial_verify_port(struct uart_port *port, struct serial_struct *ser)
425 if (ser->type != PORT_UNKNOWN && ser->type != PORT_RK2818)
427 if (port->irq != ser->irq)
429 if (ser->io_type != SERIAL_IO_MEM)
431 if (port->uartclk / 16 != ser->baud_base)
433 if ((void *)port->mapbase != ser->iomem_base)
435 if (port->iobase != ser->port)
442 #ifdef CONFIG_CONSOLE_POLL
444 * Console polling routines for writing and reading from the uart while
445 * in an interrupt or debug context.
448 static int rk2818_serial_poll_get_char(struct uart_port *port)
450 while (!((rk2818_uart_read(port, UART_USR) & UART_RECEIVE_FIFO_NOT_EMPTY) == UART_RECEIVE_FIFO_NOT_EMPTY))
452 return rk2818_uart_read(port, UART_RBR);
455 static void rk2818_serial_poll_put_char(struct uart_port *port, unsigned char c)
457 while (!(rk2818_uart_read(port, UART_USR) & UART_TRANSMIT_FIFO_NOT_FULL))
459 rk2818_uart_write(port, c, UART_THR);
461 #endif /* CONFIG_CONSOLE_POLL */
463 static struct uart_ops rk2818_uart_pops = {
464 .tx_empty = rk2818_serial_tx_empty,
465 .set_mctrl = rk2818_serial_set_mctrl,
466 .get_mctrl = rk2818_serial_get_mctrl,
467 .stop_tx = rk2818_serial_stop_tx,
468 .start_tx = rk2818_serial_start_tx,
469 .stop_rx = rk2818_serial_stop_rx,
470 .enable_ms = rk2818_serial_enable_ms,
471 .break_ctl = rk2818_serial_break_ctl,
472 .startup = rk2818_serial_startup,
473 .shutdown = rk2818_serial_shutdown,
474 .set_termios = rk2818_serial_set_termios,
475 .type = rk2818_serial_type,
476 .release_port = rk2818_serial_release_port,
477 .request_port = rk2818_serial_request_port,
478 .config_port = rk2818_serial_config_port,
479 .verify_port = rk2818_serial_verify_port,
480 .pm = rk2818_serial_pm,
481 #ifdef CONFIG_CONSOLE_POLL
482 .poll_get_char = rk2818_serial_poll_get_char,
483 .poll_put_char = rk2818_serial_poll_put_char,
488 static struct rk2818_port rk2818_uart_ports[] = {
492 .ops = &rk2818_uart_pops,
493 .flags = UPF_BOOT_AUTOCONF,
501 .ops = &rk2818_uart_pops,
502 .flags = UPF_BOOT_AUTOCONF,
510 .ops = &rk2818_uart_pops,
511 .flags = UPF_BOOT_AUTOCONF,
519 .ops = &rk2818_uart_pops,
520 .flags = UPF_BOOT_AUTOCONF,
527 #define UART_NR ARRAY_SIZE(rk2818_uart_ports)
529 static inline struct uart_port *get_port_from_line(unsigned int line)
531 return &rk2818_uart_ports[line].uart;
534 #ifdef CONFIG_SERIAL_RK2818_CONSOLE
535 static void rk2818_console_putchar(struct uart_port *port, int ch)
537 while (!(rk2818_uart_read(port,UART_USR) & UART_TRANSMIT_FIFO_NOT_FULL))
539 rk2818_uart_write(port,ch,UART_THR);
543 * Interrupts are disabled on entering
545 static void rk2818_console_write(struct console *co, const char *s, u_int count)
547 struct uart_port *port;
548 struct rk2818_port *rk2818_port;
550 BUG_ON(co->index < 0 || co->index >= UART_NR);
552 port = get_port_from_line(co->index);
553 rk2818_port = UART_TO_RK2818(port);
555 spin_lock(&port->lock);
556 uart_console_write(port, s, count, rk2818_console_putchar);
557 spin_unlock(&port->lock);
560 static int __init rk2818_console_setup(struct console *co, char *options)
562 struct uart_port *port;
563 int baud, flow, bits, parity;
565 if (unlikely(co->index >= UART_NR || co->index < 0))
568 port = get_port_from_line(co->index);
570 if (unlikely(!port->membase))
575 //rk2818_init_clock(port);
578 uart_parse_options(options, &baud, &parity, &bits, &flow);
583 rk2818_uart_write(port,rk2818_uart_read(port,UART_LCR) | LCR_WLS_8 | PARITY_DISABLED | ONE_STOP_BIT,UART_LCR); /* 8N1 */
584 if (baud < 300 || baud > 115200)
586 rk2818_set_baud_rate(port, baud);
588 printk(KERN_INFO "rk2818_serial: console setup on port %d\n", port->line);
590 return uart_set_options(port, co, baud, parity, bits, flow);
593 static struct uart_driver rk2818_uart_driver;
595 static struct console rk2818_console = {
597 .write = rk2818_console_write,
598 .device = uart_console_device,
599 .setup = rk2818_console_setup,
600 .flags = CON_PRINTBUFFER,
602 .data = &rk2818_uart_driver,
605 #define RK2818_CONSOLE (&rk2818_console)
608 #define RK2818_CONSOLE NULL
611 static struct uart_driver rk2818_uart_driver = {
612 .owner = THIS_MODULE,
613 .driver_name = "rk2818_serial",
616 .cons = RK2818_CONSOLE,
617 .major = RK2818_SERIAL_MAJOR,
618 .minor = RK2818_SERIAL_MINOR,
621 static int __devinit rk2818_serial_probe(struct platform_device *pdev)
623 struct rk2818_port *rk2818_port;
624 struct resource *resource;
625 struct uart_port *port;
628 if (unlikely(pdev->id < 0 || pdev->id >= UART_NR))
631 printk(KERN_INFO "rk2818_serial: detected port %d\n", pdev->id);
637 //UART0µÄËĸö¹Ü½ÅÏÈIOMUX³ÉGPIO
638 //È»ºó·Ö±ðÉèÖÃÊäÈëÊä³ö/À¸ßÀµÍ´¦Àí
640 //·ÀÖ¹Ö±½ÓIOMUX³ÉUARTºóËĸö¹Ü½ÅµÄ״̬²»¶Ôʱ
641 //²Ù×÷UARTµ¼ÖÂUART_USR_BUSYʼÖÕΪ1Ôì³ÉÈçÏÂËÀÑ»·
642 //while(rk2818_uart_read(port,UART_USR)&UART_USR_BUSY)
643 //UARTËĸö¹Ü½ÅÔÚδ´«ÊäʱÕý³£×´Ì¬Ó¦¸ÃΪ£º
646 //×¢Ò⣺CTS/RTSΪµÍÓÐЧ£¬Ó²¼þÉϲ»Ó¦¸ÃÇ¿ÐÐ×öÉÏÀ
649 rk2818_mux_api_set(GPIOG1_UART0_MMC1WPT_NAME, IOMUXA_GPIO1_C1 /*IOMUXA_UART0_SOUT*/);
650 rk2818_mux_api_set(GPIOG0_UART0_MMC1DET_NAME, IOMUXA_GPIO1_C0 /*IOMUXA_UART0_SIN*/);
652 ret = gpio_request(RK2818_PIN_PG0, NULL);
655 gpio_free(RK2818_PIN_PG0);
657 gpio_direction_output(RK2818_PIN_PG0,GPIO_HIGH);
660 ret = gpio_request(RK2818_PIN_PG1, NULL);
663 gpio_free(RK2818_PIN_PG1);
665 gpio_direction_output(RK2818_PIN_PG1,GPIO_HIGH);
667 gpio_pull_updown(RK2818_PIN_PG1,GPIOPullUp);
668 gpio_pull_updown(RK2818_PIN_PG0,GPIOPullUp);
670 rk2818_mux_api_set(GPIOG1_UART0_MMC1WPT_NAME, IOMUXA_UART0_SOUT);
671 rk2818_mux_api_set(GPIOG0_UART0_MMC1DET_NAME, IOMUXA_UART0_SIN);
673 rk2818_mux_api_set(GPIOB2_U0CTSN_SEL_NAME, IOMUXB_GPIO0_B2/*IOMUXB_UART0_CTS_N*/);
674 rk2818_mux_api_set(GPIOB3_U0RTSN_SEL_NAME, IOMUXB_GPIO0_B3/*IOMUXB_UART0_RTS_N*/);
676 ret = gpio_request(RK2818_PIN_PB2, NULL);
679 gpio_free(RK2818_PIN_PB2);
681 gpio_direction_input(RK2818_PIN_PB2);
682 // gpio_direction_output(RK2818_PIN_PB2,GPIO_LOW);
684 ret = gpio_request(RK2818_PIN_PB3, NULL);
687 gpio_free(RK2818_PIN_PB3);
689 gpio_direction_output(RK2818_PIN_PB3,GPIO_LOW);
694 port = get_port_from_line(pdev->id);
695 port->dev = &pdev->dev;
696 rk2818_port = UART_TO_RK2818(port);
698 rk2818_port->clk = clk_get(&pdev->dev, "uart");
699 if (unlikely(IS_ERR(rk2818_port->clk)))
700 return PTR_ERR(rk2818_port->clk);
701 port->uartclk = clk_get_rate(rk2818_port->clk);
703 resource = platform_get_resource(pdev, IORESOURCE_MEM, 0);
704 if (unlikely(!resource))
706 port->mapbase = resource->start;
708 port->irq = platform_get_irq(pdev, 0);
709 if (unlikely(port->irq < 0))
712 platform_set_drvdata(pdev, port);
714 return uart_add_one_port(&rk2818_uart_driver, port);
717 static int __devexit rk2818_serial_remove(struct platform_device *pdev)
719 struct rk2818_port *rk2818_port = platform_get_drvdata(pdev);
721 clk_put(rk2818_port->clk);
726 static struct platform_driver rk2818_platform_driver = {
727 .remove = rk2818_serial_remove,
729 .name = "rk2818_serial",
730 .owner = THIS_MODULE,
734 static int __init rk2818_serial_init(void)
737 ret = uart_register_driver(&rk2818_uart_driver);
741 ret = platform_driver_probe(&rk2818_platform_driver, rk2818_serial_probe);
743 uart_unregister_driver(&rk2818_uart_driver);
745 printk(KERN_INFO "rk2818_serial: driver initialized\n");
750 static void __exit rk2818_serial_exit(void)
752 #ifdef CONFIG_SERIAL_RK2818_CONSOLE
753 unregister_console(&rk2818_console);
755 platform_driver_unregister(&rk2818_platform_driver);
756 uart_unregister_driver(&rk2818_uart_driver);
760 * While this can be a module, if builtin it's most likely the console
761 * So let's leave module_exit but move module_init to an earlier place
763 arch_initcall(rk2818_serial_init);
764 module_exit(rk2818_serial_exit);
766 MODULE_AUTHOR("lhh lhh@rock-chips.com");
767 MODULE_DESCRIPTION("Rockchip RK2818 Serial port driver");
768 MODULE_LICENSE("GPL");