2 * drivers/serial/rk2818_serial.c - driver for rk2818 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.
18 #include <linux/hrtimer.h>
19 #include <linux/module.h>
21 #include <linux/ioport.h>
22 #include <linux/irq.h>
23 #include <linux/init.h>
24 #include <linux/console.h>
25 #include <linux/tty.h>
26 #include <linux/tty_flip.h>
27 #include <linux/serial_core.h>
28 #include <linux/serial.h>
29 #include <linux/clk.h>
30 #include <linux/platform_device.h>
32 #include "rk2818_serial.h"
35 * We wrap our port structure around the generic uart_port.
38 struct uart_port uart;
44 #if defined(CONFIG_SERIAL_RK2818_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
48 #define UART_TO_RK2818(uart_port) ((struct rk2818_port *) uart_port)
49 #define RK2818_SERIAL_MAJOR TTY_MAJOR
50 #define RK2818_SERIAL_MINOR 64
53 static inline void rk2818_uart_write(struct uart_port *port, unsigned int val,
56 __raw_writel(val, port->membase + off);
59 static inline unsigned int rk2818_uart_read(struct uart_port *port, unsigned int off)
61 return __raw_readl(port->membase + off);
64 static int rk2818_set_baud_rate(struct uart_port *port, unsigned int baud)
66 unsigned int uartTemp;
68 rk2818_uart_write(port,rk2818_uart_read(port,UART_LCR) | LCR_DLA_EN,UART_LCR);
69 uartTemp = port->uartclk / (16 * baud);
70 rk2818_uart_write(port,uartTemp & 0xff,UART_DLL);
71 rk2818_uart_write(port,(uartTemp>>8) & 0xff,UART_DLH);
72 rk2818_uart_write(port,rk2818_uart_read(port,UART_LCR) & (~LCR_DLA_EN),UART_LCR);
77 * ÅжϷ¢ËÍ»º³åÇøÊÇ·ñΪ¿Õ
78 *ÏÈÒÔFIFO´ò¿ª×ö£¬ºóÃæ¿ÉÒÔ×ö³ÉFIFO¹Ø»òFIFO¿ª¡£
80 static u_int rk2818_serial_tx_empty(struct uart_port *port)
82 while(!(rk2818_uart_read(port,UART_USR)&UART_TRANSMIT_FIFO_EMPTY))
84 if(rk2818_uart_read(port,UART_USR)&UART_TRANSMIT_FIFO_EMPTY)
93 * Power / Clock management.
95 static void rk2818_serial_pm(struct uart_port *port, unsigned int state,
96 unsigned int oldstate)
98 struct rk2818_port *rk2818_port = UART_TO_RK2818(port);
103 * Enable the peripheral clock for this serial port.
104 * This is called on uart_open() or a resume event.
106 clk_enable(rk2818_port->clk);
110 * Disable the peripheral clock for this serial port.
111 * This is called on uart_close() or a suspend event.
113 clk_disable(rk2818_port->clk);
116 printk(KERN_ERR "rk2818_serial: unknown pm %d\n", state);
121 * Return string describing the specified port
123 static const char *rk2818_serial_type(struct uart_port *port)
125 return (port->type == PORT_RK2818) ? "RK2818_SERIAL" : NULL;
128 static void rk2818_serial_enable_ms(struct uart_port *port)
131 printk("Enter::%s\n",__FUNCTION__);
135 /* no modem control lines */
136 static unsigned int rk2818_serial_get_mctrl(struct uart_port *port)
138 unsigned int result = 0;
141 status = rk2818_uart_read(port,UART_MSR);
142 if (status & UART_MSR_URCTS)
144 result = TIOCM_CAR | TIOCM_DSR | TIOCM_CTS;
145 printk("UART_GET_MSR:0x%x\n",result);
147 result = TIOCM_CAR | TIOCM_DSR;
148 printk("UART_GET_MSR:0x%x\n",result);
153 static void rk2818_serial_set_mctrl(struct uart_port *port, unsigned int mctrl)
156 printk("Enter::%s\n",__FUNCTION__);
163 static void rk2818_serial_stop_tx(struct uart_port *port)
166 printk("Enter::%s\n",__FUNCTION__);
171 * Start transmitting.
173 static void rk2818_serial_start_tx(struct uart_port *port)
175 struct circ_buf *xmit = &port->state->xmit;
176 while(!(uart_circ_empty(xmit)))
178 while (!(rk2818_uart_read(port,UART_USR) & UART_TRANSMIT_FIFO_NOT_FULL)){
179 rk2818_uart_write(port,UART_IER_RECV_DATA_AVAIL_INT_ENABLE|UART_IER_SEND_EMPTY_INT_ENABLE,UART_IER);
182 rk2818_uart_write(port,xmit->buf[xmit->tail],UART_THR);
183 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
186 if((uart_circ_empty(xmit)))
187 rk2818_uart_write(port,UART_IER_RECV_DATA_AVAIL_INT_ENABLE,UART_IER);
188 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
189 uart_write_wakeup(port);
193 * Stop receiving - port is in process of being closed.
195 static void rk2818_serial_stop_rx(struct uart_port *port)
198 printk("Enter::%s\n",__FUNCTION__);
203 * Control the transmission of a break signal
205 static void rk2818_serial_break_ctl(struct uart_port *port, int break_state)
208 temp = rk2818_uart_read(port,UART_LCR);
209 if (break_state != 0)
210 temp = temp & (~BREAK_CONTROL_BIT);/* start break */
212 temp = temp | BREAK_CONTROL_BIT; /* stop break */
213 rk2818_uart_write(port,temp,UART_LCR);
218 * Characters received (called from interrupt handler)
220 static void rk2818_rx_chars(struct uart_port *port)
222 unsigned int ch, flag;
223 while((rk2818_uart_read(port,UART_USR) & UART_RECEIVE_FIFO_NOT_EMPTY) == UART_RECEIVE_FIFO_NOT_EMPTY)
225 ch = rk2818_uart_read(port,UART_RBR);
228 if (uart_handle_sysrq_char(port, ch))
232 uart_insert_char(port, 0, 0, ch, flag);
234 spin_unlock(&port->lock);
235 tty_flip_buffer_push(port->state->port.tty);
236 spin_lock(&port->lock);
243 static irqreturn_t rk2818_uart_interrupt(int irq, void *dev_id)
245 struct uart_port *port = dev_id;
246 unsigned int status, pending;
248 status = rk2818_uart_read(port,UART_IIR);
249 pending = status & 0x0f;
250 if((pending == UART_IIR_RECV_AVAILABLE) || (pending == UART_IIR_CHAR_TIMEOUT))
251 rk2818_rx_chars(port);
252 if(pending == UART_IIR_THR_EMPTY)
253 rk2818_serial_start_tx(port);
260 static void rk2818_serial_shutdown(struct uart_port *port)
262 struct rk2818_port *rk2818_port = UART_TO_RK2818(port);
263 rk2818_uart_write(port,0x00,UART_IER);
264 clk_disable(rk2818_port->clk);
265 free_irq(port->irq, port);
268 * Perform initialization and enable port for reception
270 static int rk2818_serial_startup(struct uart_port *port)
272 struct tty_struct *tty = port->state->port.tty;
275 retval = request_irq(port->irq,rk2818_uart_interrupt,IRQF_SHARED,
276 tty ? tty->name : "rk2818_serial",port);
279 printk("\nrk2818_serial_startup err \n");
280 rk2818_serial_shutdown(port);
283 rk2818_uart_write(port,UART_IER_RECV_DATA_AVAIL_INT_ENABLE,UART_IER); //enable uart recevice IRQ
288 * Change the port parameters
290 static void rk2818_serial_set_termios(struct uart_port *port, struct ktermios *termios,
291 struct ktermios *old)
294 unsigned int mode, baud;
295 unsigned int umcon,fcr;
296 /* Get current mode register */
297 mode = rk2818_uart_read(port,UART_LCR) & (BREAK_CONTROL_BIT | EVEN_PARITY_SELECT | PARITY_ENABLED
298 | ONE_HALF_OR_TWO_BIT | UART_DATABIT_MASK);
300 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk / 16);
302 switch (termios->c_cflag & CSIZE) {
318 if (termios->c_cflag & CSTOPB)
319 mode |= ONE_STOP_BIT;
322 if (termios->c_cflag & PARENB)
324 mode |= PARITY_ENABLED;
325 if (termios->c_cflag & PARODD)
330 spin_lock_irqsave(&port->lock, flags);
331 if(termios->c_cflag & CRTSCTS)
333 /*¿ªÆôuart0Ó²¼þÁ÷¿Ø*/
334 //rk2818_mux_api_set(GPIOB2_U0CTSN_SEL_NAME, IOMUXB_UART0_CTS_N);
335 //rk2818_mux_api_set(GPIOB3_U0RTSN_SEL_NAME, IOMUXB_UART0_RTS_N);
336 printk("start CRTSCTS control and baudrate is %d\n",baud);
337 umcon=rk2818_uart_read(port,UART_MCR);
338 printk("UART_GET_MCR umcon=0x%x\n",umcon);
339 umcon |= UART_MCR_AFCEN;
340 umcon |= UART_MCR_URRTS;
341 umcon &=~UART_SIR_ENABLE;
342 rk2818_uart_write(port,umcon,UART_MCR);
343 printk("UART_GET_MCR umcon=0x%x\n",umcon);
344 fcr=rk2818_uart_read(port,UART_FCR);
345 printk("UART_GET_MCR fcr=0x%x\n",fcr);
346 fcr |= UART_FCR_FIFO_ENABLE;
347 rk2818_uart_write(port,fcr,UART_FCR);
348 printk("UART_GET_MCR fcr=0x%x\n",fcr);
350 mode = mode | LCR_DLA_EN;
351 while(rk2818_uart_read(port,UART_USR)&UART_USR_BUSY)
353 rk2818_uart_write(port,mode,UART_LCR);
354 baud = rk2818_set_baud_rate(port, baud);
355 uart_update_timeout(port, termios->c_cflag, baud);
356 spin_unlock_irqrestore(&port->lock, flags);
360 static void rk2818_serial_release_port(struct uart_port *port)
362 struct platform_device *pdev = to_platform_device(port->dev);
363 struct resource *resource;
364 resource_size_t size;
366 resource = platform_get_resource(pdev, IORESOURCE_MEM, 0);
367 if (unlikely(!resource))
369 size = resource->end - resource->start + 1;
371 release_mem_region(port->mapbase, size);
372 iounmap(port->membase);
373 port->membase = NULL;
376 static int rk2818_serial_request_port(struct uart_port *port)
378 struct platform_device *pdev = to_platform_device(port->dev);
379 struct resource *resource;
380 resource_size_t size;
382 resource = platform_get_resource(pdev, IORESOURCE_MEM, 0);
383 if (unlikely(!resource))
385 size = resource->end - resource->start + 1;
387 if (unlikely(!request_mem_region(port->mapbase, size, "rk2818_serial")))
390 port->membase = ioremap(port->mapbase, size);
391 if (!port->membase) {
392 release_mem_region(port->mapbase, size);
400 * Configure/autoconfigure the port.
402 static void rk2818_serial_config_port(struct uart_port *port, int flags)
404 if (flags & UART_CONFIG_TYPE) {
405 port->type = PORT_RK2818;
406 rk2818_serial_request_port(port);
411 * Verify the new serial_struct (for TIOCSSERIAL).
413 static int rk2818_serial_verify_port(struct uart_port *port, struct serial_struct *ser)
416 if (ser->type != PORT_UNKNOWN && ser->type != PORT_RK2818)
418 if (port->irq != ser->irq)
420 if (ser->io_type != SERIAL_IO_MEM)
422 if (port->uartclk / 16 != ser->baud_base)
424 if ((void *)port->mapbase != ser->iomem_base)
426 if (port->iobase != ser->port)
434 static struct uart_ops rk2818_uart_pops = {
435 .tx_empty = rk2818_serial_tx_empty,
436 .set_mctrl = rk2818_serial_set_mctrl,
437 .get_mctrl = rk2818_serial_get_mctrl,
438 .stop_tx = rk2818_serial_stop_tx,
439 .start_tx = rk2818_serial_start_tx,
440 .stop_rx = rk2818_serial_stop_rx,
441 .enable_ms = rk2818_serial_enable_ms,
442 .break_ctl = rk2818_serial_break_ctl,
443 .startup = rk2818_serial_startup,
444 .shutdown = rk2818_serial_shutdown,
445 .set_termios = rk2818_serial_set_termios,
446 .type = rk2818_serial_type,
447 .release_port = rk2818_serial_release_port,
448 .request_port = rk2818_serial_request_port,
449 .config_port = rk2818_serial_config_port,
450 .verify_port = rk2818_serial_verify_port,
451 .pm = rk2818_serial_pm,
455 static struct rk2818_port rk2818_uart_ports[] = {
459 .ops = &rk2818_uart_pops,
460 .flags = UPF_BOOT_AUTOCONF,
468 .ops = &rk2818_uart_pops,
469 .flags = UPF_BOOT_AUTOCONF,
477 .ops = &rk2818_uart_pops,
478 .flags = UPF_BOOT_AUTOCONF,
486 .ops = &rk2818_uart_pops,
487 .flags = UPF_BOOT_AUTOCONF,
494 #define UART_NR ARRAY_SIZE(rk2818_uart_ports)
496 static inline struct uart_port *get_port_from_line(unsigned int line)
498 return &rk2818_uart_ports[line].uart;
501 #ifdef CONFIG_SERIAL_RK2818_CONSOLE
502 static void rk2818_console_putchar(struct uart_port *port, int ch)
504 while (!(rk2818_uart_read(port,UART_USR) & UART_TRANSMIT_FIFO_NOT_FULL))
506 rk2818_uart_write(port,ch,UART_THR);
510 * Interrupts are disabled on entering
512 static void rk2818_console_write(struct console *co, const char *s, u_int count)
514 struct uart_port *port;
515 struct rk2818_port *rk2818_port;
517 BUG_ON(co->index < 0 || co->index >= UART_NR);
519 port = get_port_from_line(co->index);
520 rk2818_port = UART_TO_RK2818(port);
522 spin_lock(&port->lock);
523 uart_console_write(port, s, count, rk2818_console_putchar);
524 spin_unlock(&port->lock);
527 static int __init rk2818_console_setup(struct console *co, char *options)
529 struct uart_port *port;
530 int baud, flow, bits, parity;
532 if (unlikely(co->index >= UART_NR || co->index < 0))
535 port = get_port_from_line(co->index);
537 if (unlikely(!port->membase))
542 //rk2818_init_clock(port);
545 uart_parse_options(options, &baud, &parity, &bits, &flow);
550 rk2818_uart_write(port,rk2818_uart_read(port,UART_LCR) | LCR_WLS_8 | PARITY_DISABLED | ONE_STOP_BIT,UART_LCR); /* 8N1 */
551 if (baud < 300 || baud > 115200)
553 rk2818_set_baud_rate(port, baud);
555 printk(KERN_INFO "rk2818_serial: console setup on port %d\n", port->line);
557 return uart_set_options(port, co, baud, parity, bits, flow);
560 static struct uart_driver rk2818_uart_driver;
562 static struct console rk2818_console = {
564 .write = rk2818_console_write,
565 .device = uart_console_device,
566 .setup = rk2818_console_setup,
567 .flags = CON_PRINTBUFFER,
569 .data = &rk2818_uart_driver,
572 #define RK2818_CONSOLE (&rk2818_console)
575 #define RK2818_CONSOLE NULL
578 static struct uart_driver rk2818_uart_driver = {
579 .owner = THIS_MODULE,
580 .driver_name = "rk2818_serial",
583 .cons = RK2818_CONSOLE,
584 .major = RK2818_SERIAL_MAJOR,
585 .minor = RK2818_SERIAL_MINOR,
588 static int __devinit rk2818_serial_probe(struct platform_device *pdev)
590 struct rk2818_port *rk2818_port;
591 struct resource *resource;
592 struct uart_port *port;
594 if (unlikely(pdev->id < 0 || pdev->id >= UART_NR))
597 printk(KERN_INFO "rk2818_serial: detected port %d\n", pdev->id);
599 port = get_port_from_line(pdev->id);
600 port->dev = &pdev->dev;
601 rk2818_port = UART_TO_RK2818(port);
603 rk2818_port->clk = clk_get(&pdev->dev, "uart_clk");
604 if (unlikely(IS_ERR(rk2818_port->clk)))
605 return PTR_ERR(rk2818_port->clk);
606 port->uartclk = 24000*1000; ///clk_get_rate(rk2818_port->clk);
608 resource = platform_get_resource(pdev, IORESOURCE_MEM, 0);
609 if (unlikely(!resource))
611 port->mapbase = resource->start;
613 port->irq = platform_get_irq(pdev, 0);
614 if (unlikely(port->irq < 0))
617 platform_set_drvdata(pdev, port);
619 return uart_add_one_port(&rk2818_uart_driver, port);
622 static int __devexit rk2818_serial_remove(struct platform_device *pdev)
624 struct rk2818_port *rk2818_port = platform_get_drvdata(pdev);
626 clk_put(rk2818_port->clk);
631 static struct platform_driver rk2818_platform_driver = {
632 .remove = rk2818_serial_remove,
634 .name = "rk2818_serial",
635 .owner = THIS_MODULE,
639 static int __init rk2818_serial_init(void)
642 ret = uart_register_driver(&rk2818_uart_driver);
646 ret = platform_driver_probe(&rk2818_platform_driver, rk2818_serial_probe);
648 uart_unregister_driver(&rk2818_uart_driver);
650 printk(KERN_INFO "rk2818_serial: driver initialized\n");
655 static void __exit rk2818_serial_exit(void)
657 #ifdef CONFIG_SERIAL_RK2818_CONSOLE
658 unregister_console(&rk2818_console);
660 platform_driver_unregister(&rk2818_platform_driver);
661 uart_unregister_driver(&rk2818_uart_driver);
665 module_init(rk2818_serial_init);
666 module_exit(rk2818_serial_exit);
668 MODULE_AUTHOR("lhh lhh@rock-chips.com");
669 MODULE_DESCRIPTION("Rockchip RK2818 Serial port driver");
670 MODULE_LICENSE("GPL");