📄 at91_serial.c
字号:
/* * linux/drivers/char/at91_serial.c * * Driver for Atmel AT91RM9200 Serial ports * * Copyright (c) Rick Bronson * * Based on drivers/char/serial_sa1100.c, by Deep Blue Solutions Ltd. * Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o. * * 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 * */#include <linux/config.h>#include <linux/module.h>#include <linux/tty.h>#include <linux/ioport.h>#include <linux/slab.h>#include <linux/init.h>#include <linux/serial.h>#include <linux/console.h>#include <linux/sysrq.h>#include <asm/arch/AT91RM9200_USART.h>#include <asm/arch/pio.h>/* * This is a temporary structure for registering these * functions; it is intended to be discarded after boot. */struct uart_port;struct uart_info;struct at91_port_fns { void (*set_mctrl)(struct uart_port *, u_int); u_int (*get_mctrl)(struct uart_port *); void (*enable_ms)(struct uart_port *); void (*pm)(struct uart_port *, u_int, u_int); int (*set_wake)(struct uart_port *, u_int); int (*open)(struct uart_port *, struct uart_info *); void (*close)(struct uart_port *, struct uart_info *);};#if defined(CONFIG_SERIAL_AT91_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)#define SUPPORT_SYSRQ#endif#include <linux/serial_core.h>#define SERIAL_AT91_MAJOR TTY_MAJOR#define CALLOUT_AT91_MAJOR TTYAUX_MAJOR#define MINOR_START 64#define AT91C_VA_BASE_DBGU ((unsigned long) &(AT91_SYS->DBGU_CR))#define AT91_ISR_PASS_LIMIT 256#define UART_PUT_CR(port,v) ((AT91PS_USART)(port)->membase)->US_CR = v#define UART_GET_MR(port) ((AT91PS_USART)(port)->membase)->US_MR#define UART_PUT_MR(port,v) ((AT91PS_USART)(port)->membase)->US_MR = v#define UART_PUT_IER(port,v) ((AT91PS_USART)(port)->membase)->US_IER = v#define UART_PUT_IDR(port,v) ((AT91PS_USART)(port)->membase)->US_IDR = v#define UART_GET_IMR(port) ((AT91PS_USART)(port)->membase)->US_IMR#define UART_GET_CSR(port) ((AT91PS_USART)(port)->membase)->US_CSR#define UART_GET_CHAR(port) ((AT91PS_USART)(port)->membase)->US_RHR#define UART_PUT_CHAR(port,v) ((AT91PS_USART)(port)->membase)->US_THR = v#define UART_GET_BRGR(port) ((AT91PS_USART)(port)->membase)->US_BRGR#define UART_PUT_BRGR(port,v) ((AT91PS_USART)(port)->membase)->US_BRGR = v// #define UART_GET_CR(port) ((AT91PS_USART)(port)->membase)->US_CR // is write-onlystatic struct tty_driver normal, callout;static struct tty_struct *at91_table[AT91C_NR_UART];static struct termios *at91_termios[AT91C_NR_UART], *at91_termios_locked[AT91C_NR_UART];static int (*at91_open)(struct uart_port *, struct uart_info *);static void (*at91_close)(struct uart_port *, struct uart_info *);#ifdef SUPPORT_SYSRQstatic struct console at91_console;#endif/* * Return TIOCSER_TEMT when transmitter FIFO and Shift register is empty. */static u_int at91_tx_empty(struct uart_port *port){ return UART_GET_CSR(port) & AT91C_US_TXEMPTY ? TIOCSER_TEMT : 0;}/* * Set state of the modem control output lines */static void at91_set_mctrl(struct uart_port *port, u_int mctrl){ unsigned int control = 0; if (mctrl & TIOCM_RTS) control |= AT91C_US_RTSEN; else control |= AT91C_US_RTSDIS; if (mctrl & TIOCM_DTR) control |= AT91C_US_DTREN; else control |= AT91C_US_DTRDIS; UART_PUT_CR(port,control);}/* * Get state of the modem control input lines */static u_int at91_get_mctrl(struct uart_port *port){ unsigned int status, ret = 0; status = UART_GET_CSR(port); if (status & AT91C_US_DCD) ret |= TIOCM_CD; if (status & AT91C_US_CTS) ret |= TIOCM_CTS; if (status & AT91C_US_DSR) ret |= TIOCM_DSR; if (status & AT91C_US_RI) ret |= TIOCM_RI; return ret;}/* * Stop transmitting. */static void at91_stop_tx(struct uart_port *port, u_int from_tty){ UART_PUT_IDR(port, AT91C_US_TXRDY); port->read_status_mask &= ~AT91C_US_TXRDY;}/* * Start transmitting. */static void at91_start_tx(struct uart_port *port, u_int nonempty, u_int from_tty){ if (nonempty) { unsigned long flags; local_irq_save(flags); port->read_status_mask |= AT91C_US_TXRDY; UART_PUT_IER(port, AT91C_US_TXRDY); local_irq_restore(flags); }}/* * Stop receiving - port is in process of being closed. */static void at91_stop_rx(struct uart_port *port){ UART_PUT_IDR(port, AT91C_US_RXRDY);}/* * Enable modem status interrupts */static void at91_enable_ms(struct uart_port *port){ UART_PUT_IER(port, AT91C_US_RIIC | AT91C_US_DSRIC | AT91C_US_DCDIC | AT91C_US_CTSIC);}/* * Control the transmission of a break signal */static void at91_break_ctl(struct uart_port *port, int break_state){ if (break_state != 0) UART_PUT_CR(port, AT91C_US_STTBRK); /* start break */ else UART_PUT_CR(port, AT91C_US_STPBRK); /* stop break */}/* * Characters received (called from interrupt handler) */static void at91_rx_chars(struct uart_info *info, struct pt_regs *regs){ struct tty_struct *tty = info->tty; struct uart_port *port = info->port; unsigned int status, ch, flg, ignored = 0; status = UART_GET_CSR(port); while (status & (AT91C_US_RXRDY)) { ch = UART_GET_CHAR(port); if (tty->flip.count >= TTY_FLIPBUF_SIZE) goto ignore_char; port->icount.rx++; flg = TTY_NORMAL; /* * note that the error handling code is * out of the main execution path */ if (status & (AT91C_US_PARE | AT91C_US_FRAME | AT91C_US_OVRE)) goto handle_error; if (uart_handle_sysrq_char(info, ch, regs)) goto ignore_char; error_return: *tty->flip.flag_buf_ptr++ = flg; *tty->flip.char_buf_ptr++ = ch; tty->flip.count++; ignore_char: status = UART_GET_CSR(port); }out: tty_flip_buffer_push(tty); return;handle_error: if (status & (AT91C_US_PARE | AT91C_US_FRAME | AT91C_US_OVRE)) UART_PUT_CR(port, AT91C_US_RSTSTA); /* clear error */ if (status & (AT91C_US_PARE)) port->icount.parity++; else if (status & (AT91C_US_FRAME)) port->icount.frame++; if (status & (AT91C_US_OVRE)) port->icount.overrun++; if (status & port->ignore_status_mask) { if (++ignored > 100) goto out; goto ignore_char; } status &= port->read_status_mask; UART_PUT_CR(port, AT91C_US_RSTSTA); /* clear error */ if (status & AT91C_US_PARE) flg = TTY_PARITY; else if (status & AT91C_US_FRAME) flg = TTY_FRAME; if (status & AT91C_US_OVRE) { /* * overrun does *not* affect the character * we read from the FIFO */ *tty->flip.flag_buf_ptr++ = flg; *tty->flip.char_buf_ptr++ = ch; tty->flip.count++; if (tty->flip.count >= TTY_FLIPBUF_SIZE) goto ignore_char; ch = 0; flg = TTY_OVERRUN; }#ifdef SUPPORT_SYSRQ info->sysrq = 0;#endif goto error_return;}/* * Transmit characters (called from interrupt handler) */static void at91_tx_chars(struct uart_info *info){ struct uart_port *port = info->port; if (port->x_char) { UART_PUT_CHAR(port, port->x_char); port->icount.tx++; port->x_char = 0; return; } if (info->xmit.head == info->xmit.tail || info->tty->stopped || info->tty->hw_stopped) { at91_stop_tx(info->port, 0); return; } while (UART_GET_CSR(port) & AT91C_US_TXRDY) { UART_PUT_CHAR(port, info->xmit.buf[info->xmit.tail]); info->xmit.tail = (info->xmit.tail + 1) & (UART_XMIT_SIZE - 1); port->icount.tx++; if (info->xmit.head == info->xmit.tail) break; } if (CIRC_CNT(info->xmit.head, info->xmit.tail, UART_XMIT_SIZE) < WAKEUP_CHARS) uart_event(info, EVT_WRITE_WAKEUP); if (info->xmit.head == info->xmit.tail) at91_stop_tx(info->port, 0);}/* * Interrupt handler */static void at91_interrupt(int irq, void *dev_id, struct pt_regs *regs){ struct uart_info *info = dev_id; struct uart_port *port = info->port; unsigned int status, pending, pass_counter = 0; status = UART_GET_CSR(port); pending = status & port->read_status_mask; if (pending) { do { if (pending & AT91C_US_RXRDY) at91_rx_chars(info, regs); /* Clear the relevent break bits */ if (pending & AT91C_US_RXBRK) { UART_PUT_CR(port, AT91C_US_RSTSTA); port->icount.brk++;#ifdef SUPPORT_SYSRQ if (port->line == at91_console.index && !info->sysrq) { info->sysrq = jiffies + HZ*5; }#endif } // TODO: All reads to CSR will clear these interrupts! if (pending & AT91C_US_RIIC) port->icount.rng++; if (pending & AT91C_US_DSRIC) port->icount.dsr++; if (pending & AT91C_US_DCDIC) { port->icount.dcd++; uart_handle_dcd_change(info, status & AT91C_US_DCD); } if (pending & AT91C_US_CTSIC) { port->icount.cts++; uart_handle_cts_change(info, status & AT91C_US_CTS); } if (pending & (AT91C_US_RIIC | AT91C_US_DSRIC | AT91C_US_DCDIC | AT91C_US_CTSIC)) wake_up_interruptible(&info->delta_msr_wait); if (pending & AT91C_US_TXRDY) at91_tx_chars(info); if (pass_counter++ > AT91_ISR_PASS_LIMIT) break; status = UART_GET_CSR(port); pending = status & port->read_status_mask; } while (pending); }}/* * Perform initialization and enable port for reception */static int at91_startup(struct uart_port *port, struct uart_info *info){ int retval; /* * Allocate the IRQ */ retval = request_irq(port->irq, at91_interrupt, SA_SHIRQ, "at91_serial", info); if (retval) { printk("at91_serial: at91_startup - Can't get irq\n"); return retval; } /* * If there is a specific "open" function (to register * control line interrupts) */ if (at91_open) { retval = at91_open(port, info); if (retval) { free_irq(port->irq, info); return retval; } } /* Enable peripheral clock if required */ if (port->irq != AT91C_ID_SYS) AT91_SYS->PMC_PCER = 1 << port->irq; port->read_status_mask = AT91C_US_RXRDY | AT91C_US_TXRDY | AT91C_US_OVRE | AT91C_US_FRAME | AT91C_US_PARE | AT91C_US_RXBRK; /* * Finally, clear and enable interrupts */ UART_PUT_IDR(port, -1); UART_PUT_CR(port, AT91C_US_TXEN | AT91C_US_RXEN); /* enable xmit & rcvr */ UART_PUT_IER(port, AT91C_US_RXRDY); /* do receive only */ return 0;}/* * Disable the port */static void at91_shutdown(struct uart_port *port, struct uart_info *info){ /* * Free the interrupt */ free_irq(port->irq, info); /* * If there is a specific "close" function (to unregister * control line interrupts) */ if (at91_close) at91_close(port, info);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -