📄 amba-pl011.c
字号:
/* * linux/drivers/char/amba.c * * Driver for AMBA serial ports * * Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o. * * Copyright 1999 ARM Limited * Copyright (C) 2000 Deep Blue Solutions Ltd. * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * * $Id: amba.c,v 1.41 2002/07/28 10:03:27 rmk Exp $ * * This is a generic driver for ARM AMBA-type serial ports. They * have a lot of 16550-like features, but are not register compatible. * Note that although they do have CTS, DCD and DSR inputs, they do * not have an RI input, nor do they have DTR or RTS outputs. If * required, these have to be supplied via some other means (eg, GPIO) * and hooked into this driver. */#include <linux/config.h>#include <linux/module.h>#include <linux/tty.h>#include <linux/ioport.h>#include <linux/init.h>#include <linux/serial.h>#include <linux/console.h>#include <linux/sysrq.h>#include <linux/device.h>#include <asm/io.h>#include <asm/irq.h>#include <asm/hardware/amba.h>#include <asm/hardware/clock.h>#if defined(CONFIG_SERIAL_AMBA_PL011_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)#define SUPPORT_SYSRQ#endif#include <linux/serial_core.h>#include <asm/hardware/amba_serial.h>#define UART_NR 14#define SERIAL_AMBA_MAJOR 204#define SERIAL_AMBA_MINOR 64#define SERIAL_AMBA_NR UART_NR#define AMBA_ISR_PASS_LIMIT 256#define UART_DUMMY_RSR_RX 256/* * We wrap our port structure around the generic uart_port. */struct uart_amba_port { struct uart_port port; struct clk *clk; unsigned int im; /* interrupt mask */ unsigned int old_status;};static void pl011_stop_tx(struct uart_port *port, unsigned int tty_stop){ struct uart_amba_port *uap = (struct uart_amba_port *)port; uap->im &= ~UART011_TXIM; writew(uap->im, uap->port.membase + UART011_IMSC);}static void pl011_start_tx(struct uart_port *port, unsigned int tty_start){ struct uart_amba_port *uap = (struct uart_amba_port *)port; uap->im |= UART011_TXIM; writew(uap->im, uap->port.membase + UART011_IMSC);}static void pl011_stop_rx(struct uart_port *port){ struct uart_amba_port *uap = (struct uart_amba_port *)port; uap->im &= ~(UART011_RXIM|UART011_RTIM|UART011_FEIM| UART011_PEIM|UART011_BEIM|UART011_OEIM); writew(uap->im, uap->port.membase + UART011_IMSC);}static void pl011_enable_ms(struct uart_port *port){ struct uart_amba_port *uap = (struct uart_amba_port *)port; uap->im |= UART011_RIMIM|UART011_CTSMIM|UART011_DCDMIM|UART011_DSRMIM; writew(uap->im, uap->port.membase + UART011_IMSC);}static void#ifdef SUPPORT_SYSRQpl011_rx_chars(struct uart_amba_port *uap, struct pt_regs *regs)#elsepl011_rx_chars(struct uart_amba_port *uap)#endif{ struct tty_struct *tty = uap->port.info->tty; unsigned int status, ch, rsr, max_count = 256; status = readw(uap->port.membase + UART01x_FR); while ((status & UART01x_FR_RXFE) == 0 && max_count--) { if (tty->flip.count >= TTY_FLIPBUF_SIZE) { tty->flip.work.func((void *)tty); if (tty->flip.count >= TTY_FLIPBUF_SIZE) { printk(KERN_WARNING "TTY_DONT_FLIP set\n"); return; } } ch = readw(uap->port.membase + UART01x_DR); *tty->flip.char_buf_ptr = ch; *tty->flip.flag_buf_ptr = TTY_NORMAL; uap->port.icount.rx++; /* * Note that the error handling code is * out of the main execution path */ rsr = readw(uap->port.membase + UART01x_RSR) | UART_DUMMY_RSR_RX; if (rsr & UART01x_RSR_ANY) { if (rsr & UART01x_RSR_BE) { rsr &= ~(UART01x_RSR_FE | UART01x_RSR_PE); uap->port.icount.brk++; if (uart_handle_break(&uap->port)) goto ignore_char; } else if (rsr & UART01x_RSR_PE) uap->port.icount.parity++; else if (rsr & UART01x_RSR_FE) uap->port.icount.frame++; if (rsr & UART01x_RSR_OE) uap->port.icount.overrun++; rsr &= uap->port.read_status_mask; if (rsr & UART01x_RSR_BE) *tty->flip.flag_buf_ptr = TTY_BREAK; else if (rsr & UART01x_RSR_PE) *tty->flip.flag_buf_ptr = TTY_PARITY; else if (rsr & UART01x_RSR_FE) *tty->flip.flag_buf_ptr = TTY_FRAME; } if (uart_handle_sysrq_char(&uap->port, ch, regs)) goto ignore_char; if ((rsr & uap->port.ignore_status_mask) == 0) { tty->flip.flag_buf_ptr++; tty->flip.char_buf_ptr++; tty->flip.count++; } if ((rsr & UART01x_RSR_OE) && tty->flip.count < TTY_FLIPBUF_SIZE) { /* * Overrun is special, since it's reported * immediately, and doesn't affect the current * character */ *tty->flip.char_buf_ptr++ = 0; *tty->flip.flag_buf_ptr++ = TTY_OVERRUN; tty->flip.count++; } ignore_char: status = readw(uap->port.membase + UART01x_FR); } tty_flip_buffer_push(tty); return;}static void pl011_tx_chars(struct uart_amba_port *uap){ struct circ_buf *xmit = &uap->port.info->xmit; int count; if (uap->port.x_char) { writew(uap->port.x_char, uap->port.membase + UART01x_DR); uap->port.icount.tx++; uap->port.x_char = 0; return; } if (uart_circ_empty(xmit) || uart_tx_stopped(&uap->port)) { pl011_stop_tx(&uap->port, 0); return; } count = uap->port.fifosize >> 1; do { writew(xmit->buf[xmit->tail], uap->port.membase + UART01x_DR); xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1); uap->port.icount.tx++; if (uart_circ_empty(xmit)) break; } while (--count > 0); if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS) uart_write_wakeup(&uap->port); if (uart_circ_empty(xmit)) pl011_stop_tx(&uap->port, 0);}static void pl011_modem_status(struct uart_amba_port *uap){ unsigned int status, delta; status = readw(uap->port.membase + UART01x_FR) & UART01x_FR_MODEM_ANY; delta = status ^ uap->old_status; uap->old_status = status; if (!delta) return; if (delta & UART01x_FR_DCD) uart_handle_dcd_change(&uap->port, status & UART01x_FR_DCD); if (delta & UART01x_FR_DSR) uap->port.icount.dsr++; if (delta & UART01x_FR_CTS) uart_handle_cts_change(&uap->port, status & UART01x_FR_CTS); wake_up_interruptible(&uap->port.info->delta_msr_wait);}static irqreturn_t pl011_int(int irq, void *dev_id, struct pt_regs *regs){ struct uart_amba_port *uap = dev_id; unsigned int status, pass_counter = AMBA_ISR_PASS_LIMIT; int handled = 0; spin_lock(&uap->port.lock); status = readw(uap->port.membase + UART011_MIS); if (status) { do { writew(status & ~(UART011_TXIS|UART011_RTIS| UART011_RXIS), uap->port.membase + UART011_ICR); if (status & (UART011_RTIS|UART011_RXIS))#ifdef SUPPORT_SYSRQ pl011_rx_chars(uap, regs);#else pl011_rx_chars(uap);#endif if (status & (UART011_DSRMIS|UART011_DCDMIS| UART011_CTSMIS|UART011_RIMIS)) pl011_modem_status(uap); if (status & UART011_TXIS) pl011_tx_chars(uap); if (pass_counter-- == 0) break; status = readw(uap->port.membase + UART011_MIS); } while (status != 0); handled = 1; } spin_unlock(&uap->port.lock); return IRQ_RETVAL(handled);}static unsigned int pl01x_tx_empty(struct uart_port *port){ struct uart_amba_port *uap = (struct uart_amba_port *)port; unsigned int status = readw(uap->port.membase + UART01x_FR); return status & (UART01x_FR_BUSY|UART01x_FR_TXFF) ? 0 : TIOCSER_TEMT;}static unsigned int pl01x_get_mctrl(struct uart_port *port){ struct uart_amba_port *uap = (struct uart_amba_port *)port; unsigned int result = 0; unsigned int status = readw(uap->port.membase + UART01x_FR);#define BIT(uartbit, tiocmbit) \ if (status & uartbit) \ result |= tiocmbit BIT(UART01x_FR_DCD, TIOCM_CAR); BIT(UART01x_FR_DSR, TIOCM_DSR); BIT(UART01x_FR_CTS, TIOCM_CTS); BIT(UART011_FR_RI, TIOCM_RNG);#undef BIT return result;}static void pl011_set_mctrl(struct uart_port *port, unsigned int mctrl){ struct uart_amba_port *uap = (struct uart_amba_port *)port; unsigned int cr; cr = readw(uap->port.membase + UART011_CR);#define BIT(tiocmbit, uartbit) \ if (mctrl & tiocmbit) \ cr |= uartbit; \ else \ cr &= ~uartbit BIT(TIOCM_RTS, UART011_CR_RTS); BIT(TIOCM_DTR, UART011_CR_DTR); BIT(TIOCM_OUT1, UART011_CR_OUT1); BIT(TIOCM_OUT2, UART011_CR_OUT2); BIT(TIOCM_LOOP, UART011_CR_LBE);#undef BIT writew(cr, uap->port.membase + UART011_CR);}static void pl011_break_ctl(struct uart_port *port, int break_state){ struct uart_amba_port *uap = (struct uart_amba_port *)port; unsigned long flags; unsigned int lcr_h; spin_lock_irqsave(&uap->port.lock, flags); lcr_h = readw(uap->port.membase + UART011_LCRH); if (break_state == -1) lcr_h |= UART01x_LCRH_BRK; else lcr_h &= ~UART01x_LCRH_BRK; writew(lcr_h, uap->port.membase + UART011_LCRH); spin_unlock_irqrestore(&uap->port.lock, flags);}static int pl011_startup(struct uart_port *port){ struct uart_amba_port *uap = (struct uart_amba_port *)port; unsigned int cr; int retval; /* * Try to enable the clock producer. */ retval = clk_enable(uap->clk); if (retval) goto out; uap->port.uartclk = clk_get_rate(uap->clk); /* * Allocate the IRQ */ retval = request_irq(uap->port.irq, pl011_int, 0, "uart-pl011", uap); if (retval) goto clk_dis; writew(UART011_IFLS_RX4_8|UART011_IFLS_TX4_8, uap->port.membase + UART011_IFLS); /* * Provoke TX FIFO interrupt into asserting. */ cr = UART01x_CR_UARTEN | UART011_CR_TXE | UART011_CR_LBE; writew(cr, uap->port.membase + UART011_CR); writew(0, uap->port.membase + UART011_FBRD); writew(1, uap->port.membase + UART011_IBRD); writew(0, uap->port.membase + UART011_LCRH); writew(0, uap->port.membase + UART01x_DR); while (readw(uap->port.membase + UART01x_FR) & UART01x_FR_BUSY) barrier(); cr = UART01x_CR_UARTEN | UART011_CR_RXE | UART011_CR_TXE; writew(cr, uap->port.membase + UART011_CR); /* * initialise the old status of the modem signals */ uap->old_status = readw(uap->port.membase + UART01x_FR) & UART01x_FR_MODEM_ANY; /* * Finally, enable interrupts */ spin_lock_irq(&uap->port.lock); uap->im = UART011_RXIM | UART011_RTIM; writew(uap->im, uap->port.membase + UART011_IMSC); spin_unlock_irq(&uap->port.lock); return 0; clk_dis: clk_disable(uap->clk); out: return retval;}static void pl011_shutdown(struct uart_port *port){ struct uart_amba_port *uap = (struct uart_amba_port *)port; unsigned long val; /* * disable all interrupts */ spin_lock_irq(&uap->port.lock); uap->im = 0; writew(uap->im, uap->port.membase + UART011_IMSC); writew(0xffff, uap->port.membase + UART011_ICR); spin_unlock_irq(&uap->port.lock); /* * Free the interrupt */ free_irq(uap->port.irq, uap); /* * disable the port */ writew(UART01x_CR_UARTEN | UART011_CR_TXE, uap->port.membase + UART011_CR); /* * disable break condition and fifos */ val = readw(uap->port.membase + UART011_LCRH);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -