📄 uart.c
字号:
#endif return ret;}int rs_8xx_read_proc(char *page, char **start, off_t off, int count, int *eof, void *data){ int i, len = 0; off_t begin = 0; len += sprintf(page, "serinfo:1.0 driver:%s\n", serial_version); for (i = 0; i < NR_PORTS && len < 4000; i++) { len += line_info(page + len, &rs_table[i]); if (len+begin > off+count) goto done; if (len+begin < off) { begin += len; len = 0; } } *eof = 1;done: if (off >= len+begin) return 0; *start = page + (begin-off); return ((count < begin+len-off) ? count : begin+len-off);}/* * --------------------------------------------------------------------- * rs_init() and friends * * rs_init() is called at boot-time to initialize the serial driver. * --------------------------------------------------------------------- *//* * This routine prints out the appropriate serial driver version * number, and identifies which options were configured into this * driver. */static _INLINE_ void show_serial_version(void){ printk(KERN_INFO "%s version %s\n", serial_name, serial_version);}/* * The serial console driver used during boot. Note that these names * clash with those found in "serial.c", so we currently can't support * the 16xxx uarts and these at the same time. I will fix this to become * an indirect function call from tty_io.c (or something). */#ifdef CONFIG_SERIAL_CONSOLE/* * Print a string to the serial port trying not to disturb any possible * real use of the port... */static void my_console_write(int idx, const char *s, unsigned count){ struct serial_state *ser; ser_info_t *info; unsigned i; volatile cbd_t *bdp, *bdbase; volatile smc_uart_t *up; volatile u_char *cp; ser = rs_table + idx; /* If the port has been initialized for general use, we have * to use the buffer descriptors allocated there. Otherwise, * we simply use the single buffer allocated. */ if ((info = (ser_info_t *)ser->info) != NULL) { bdp = info->tx_cur; bdbase = info->tx_bd_base; } else { /* Pointer to UART in parameter ram. */ up = (smc_uart_t *)&cpmp->cp_dparam[ser->port]; /* Get the address of the host memory buffer. */ bdp = bdbase = (cbd_t *)&cpmp->cp_dpmem[up->smc_tbase]; } /* * We need to gracefully shut down the transmitter, disable * interrupts, then send our bytes out. */ /* * Now, do each character. This is not as bad as it looks * since this is a holding FIFO and not a transmitting FIFO. * We could add the complexity of filling the entire transmit * buffer, but we would just wait longer between accesses...... */ for (i = 0; i < count; i++, s++) { /* Wait for transmitter fifo to empty. * Ready indicates output is ready, and xmt is doing * that, not that it is ready for us to send. */ while (bdp->cbd_sc & BD_SC_READY); /* Send the character out. * If the buffer address is in the CPM DPRAM, don't * convert it. */ if ((uint)(bdp->cbd_bufaddr) > (uint)IMAP_ADDR) cp = (u_char *)(bdp->cbd_bufaddr); else cp = __va(bdp->cbd_bufaddr); *cp = *s; bdp->cbd_datlen = 1; bdp->cbd_sc |= BD_SC_READY; if (bdp->cbd_sc & BD_SC_WRAP) bdp = bdbase; else bdp++; /* if a LF, also do CR... */ if (*s == 10) { while (bdp->cbd_sc & BD_SC_READY); cp = __va(bdp->cbd_bufaddr); *cp = 13; bdp->cbd_datlen = 1; bdp->cbd_sc |= BD_SC_READY; if (bdp->cbd_sc & BD_SC_WRAP) { bdp = bdbase; } else { bdp++; } } } /* * Finally, Wait for transmitter & holding register to empty * and restore the IER */ while (bdp->cbd_sc & BD_SC_READY); if (info) info->tx_cur = (cbd_t *)bdp;}static void serial_console_write(struct console *c, const char *s, unsigned count){#ifdef CONFIG_KGDB /* Try to let stub handle output. Returns true if it did. */ if (kgdb_output_string(s, count)) return;#endif my_console_write(c->index, s, count);}#ifdef CONFIG_XMONintxmon_8xx_write(const char *s, unsigned count){ my_console_write(0, s, count); return(count);}#endif#ifdef CONFIG_KGDBvoidputDebugChar(char ch){ my_console_write(0, &ch, 1);}#endif/* * Receive character from the serial port. This only works well * before the port is initialized for real use. */static int my_console_wait_key(int idx, int xmon, char *obuf){ struct serial_state *ser; u_char c, *cp; ser_info_t *info; volatile cbd_t *bdp; volatile smc_uart_t *up; int i; ser = rs_table + idx; /* Pointer to UART in parameter ram. */ up = (smc_uart_t *)&cpmp->cp_dparam[ser->port]; /* Get the address of the host memory buffer. * If the port has been initialized for general use, we must * use information from the port structure. */ if ((info = (ser_info_t *)ser->info)) bdp = info->rx_cur; else bdp = (cbd_t *)&cpmp->cp_dpmem[up->smc_rbase]; /* * We need to gracefully shut down the receiver, disable * interrupts, then read the input. * XMON just wants a poll. If no character, return -1, else * return the character. */ if (!xmon) { while (bdp->cbd_sc & BD_SC_EMPTY); } else { if (bdp->cbd_sc & BD_SC_EMPTY) return -1; } /* If the buffer address is in the CPM DPRAM, don't * convert it. */ if ((uint)(bdp->cbd_bufaddr) > (uint)IMAP_ADDR) cp = (u_char *)(bdp->cbd_bufaddr); else cp = __va(bdp->cbd_bufaddr); if (obuf) { i = c = bdp->cbd_datlen; while (i-- > 0) *obuf++ = *cp++; } else { c = *cp; } bdp->cbd_sc |= BD_SC_EMPTY; if (info) { if (bdp->cbd_sc & BD_SC_WRAP) { bdp = info->rx_bd_base; } else { bdp++; } info->rx_cur = (cbd_t *)bdp; } return((int)c);}static int serial_console_wait_key(struct console *co){ return(my_console_wait_key(co->index, 0, NULL));}#ifdef CONFIG_XMONintxmon_8xx_read_poll(void){ return(my_console_wait_key(0, 1, NULL));}intxmon_8xx_read_char(void){ return(my_console_wait_key(0, 0, NULL));}#endif#ifdef CONFIG_KGDBstatic char kgdb_buf[RX_BUF_SIZE], *kgdp;static int kgdb_chars;unsigned chargetDebugChar(void){ if (kgdb_chars <= 0) { kgdb_chars = my_console_wait_key(0, 0, kgdb_buf); kgdp = kgdb_buf; } kgdb_chars--; return(*kgdp++);}void kgdb_interruptible(int state){}void kgdb_map_scc(void){ struct serial_state *ser; uint mem_addr; volatile cbd_t *bdp; volatile smc_uart_t *up; cpmp = (cpm8xx_t *)&(((immap_t *)IMAP_ADDR)->im_cpm); /* To avoid data cache CPM DMA coherency problems, allocate a * buffer in the CPM DPRAM. This will work until the CPM and * serial ports are initialized. At that time a memory buffer * will be allocated. * The port is already initialized from the boot procedure, all * we do here is give it a different buffer and make it a FIFO. */ ser = rs_table; /* Right now, assume we are using SMCs. */ up = (smc_uart_t *)&cpmp->cp_dparam[ser->port]; /* Allocate space for an input FIFO, plus a few bytes for output. * Allocate bytes to maintain word alignment. */ mem_addr = (uint)(&cpmp->cp_dpmem[0x1000]); /* Set the physical address of the host memory buffers in * the buffer descriptors. */ bdp = (cbd_t *)&cpmp->cp_dpmem[up->smc_rbase]; bdp->cbd_bufaddr = mem_addr; bdp = (cbd_t *)&cpmp->cp_dpmem[up->smc_tbase]; bdp->cbd_bufaddr = mem_addr+RX_BUF_SIZE; up->smc_mrblr = RX_BUF_SIZE; /* receive buffer length */ up->smc_maxidl = RX_BUF_SIZE;}#endifstatic kdev_t serial_console_device(struct console *c){ return MKDEV(TTY_MAJOR, 64 + c->index);}static struct console sercons = { name: "ttyS", write: serial_console_write, device: serial_console_device, wait_key: serial_console_wait_key, setup: serial_console_setup, flags: CON_PRINTBUFFER, index: CONFIG_SERIAL_CONSOLE_PORT,};/* * Register console. */long __init console_8xx_init(long kmem_start, long kmem_end){ register_console(&sercons); return kmem_start;}#endif/* Index in baud rate table of the default console baud rate.*/static int baud_idx;/* * The serial driver boot-time initialization code! */int __init rs_8xx_init(void){ struct serial_state * state; ser_info_t *info; uint mem_addr, dp_addr, iobits; int i, j, idx; ushort chan; volatile cbd_t *bdp; volatile cpm8xx_t *cp; volatile smc_t *sp; volatile smc_uart_t *up; volatile scc_t *scp; volatile scc_uart_t *sup; volatile immap_t *immap; init_bh(SERIAL_BH, do_serial_bh); show_serial_version(); /* Initialize the tty_driver structure */ __clear_user(&serial_driver,sizeof(struct tty_driver)); serial_driver.magic = TTY_DRIVER_MAGIC; serial_driver.driver_name = "serial"; serial_driver.name = "ttyS"; serial_driver.major = TTY_MAJOR; serial_driver.minor_start = 64; serial_driver.num = NR_PORTS; serial_driver.type = TTY_DRIVER_TYPE_SERIAL; serial_driver.subtype = SERIAL_TYPE_NORMAL; serial_driver.init_termios = tty_std_termios; serial_driver.init_termios.c_cflag = baud_idx | CS8 | CREAD | HUPCL | CLOCAL; serial_driver.flags = TTY_DRIVER_REAL_RAW; serial_driver.refcount = &serial_refcount; serial_driver.table = serial_table; serial_driver.termios = serial_termios; serial_driver.termios_locked = serial_termios_locked; serial_driver.open = rs_8xx_open; serial_driver.close = rs_8xx_close; serial_driver.write = rs_8xx_write; serial_driver.put_char = rs_8xx_put_char; serial_driver.write_room = rs_8xx_write_room; serial_driver.chars_in_buffer = rs_8xx_chars_in_buffer; serial_driver.flush_buffer = rs_8xx_flush_buffer; serial_driver.ioctl = rs_8xx_ioctl; serial_driver.throttle = rs_8xx_throttle; serial_driver.unthrottle = rs_8xx_unthrottle; serial_driver.send_xchar = rs_8xx_send_xchar; serial_driver.set_termios = rs_8xx_set_termios; serial_driver.stop = rs_8xx_stop; serial_driver.start = rs_8xx_start; serial_driver.hangup = rs_8xx_hangup; serial_driver.wait_until_sent = rs_8xx_wait_until_sent; serial_driver.read_proc = rs_8xx_read_proc; /* * The callout device is just like normal device except for * major number and the subtype code. */ callout_driver = serial_driver; callout_driver.name = "cua"; callout_driver.major = TTYAUX_MAJOR; callout_driver.subtype = SERIAL_TYPE_CALLOUT; callout_driver.read_proc = 0; callout_driver.proc_entry = 0; if (tty_register_driver(&serial_driver)) panic("Couldn't register serial driver\n"); if (tty_register_driver(&callout_driver)) panic("Couldn't register callout driver\n"); cp = cpmp; /* Get pointer to Communication Processor */ immap = (immap_t *)IMAP_ADDR; /* and to internal registers */ /* Configure SCC2, SCC3, and SCC4 instead of port A parallel I/O. */#ifdef CONFIG_8xxSCC#ifndef CONFIG_MBX /* The "standard" configuration through the 860. */ immap->im_ioport.iop_papar |= 0x00fc; immap->im_ioport.iop_padir &= ~0x00fc; immap->im_ioport.iop_paodr &= ~0x00fc;#else /* On the MBX, SCC3 is through Port D. */ immap->im_ioport.iop_papar |= 0x000c; /* SCC2 on port A */ immap->im_ioport.iop_padir &= ~0x000c; immap->im_ioport.iop_paodr &= ~0x000c; immap->im_ioport.iop_pdpar |= 0x0030; /* SCC3 on port D */#endif /* Since we don't yet do modem control, connect the port C pins * as general purpose I/O. This will assert CTS and CD for the * SCC ports. */ immap->im_ioport.iop_pcdir |= 0x03c6; immap->im_ioport.iop_pcpar &= ~0x03c6; /* Connect SCC2 and SCC3 to NMSI. Connect BRG3 to SCC2 and * BRG4 to SCC3. */ cp->cp_sicr &= ~0x00ffff00; cp->cp_sicr |= 0x001b1200;#ifdef CONFIG_PP04 /* Frequentis PP04 forced to RS-232 until we know better. * Port C 12 and 13 low enables RS-232 on SCC3 and SCC4. */ immap->im_ioport.iop_pcdir |= 0x000c; immap->im_ioport.iop_pcpar &= ~0x000c; immap->im_ioport.iop_pcdat &= ~0x000c; /* This enables the TX driver. */ cp->cp_pbpar &= ~0x6000; cp->cp_pbdat &= ~0x6000;#endif#endif for (i = 0, state = rs_table; i < NR_PORTS; i++,state++) { state->magic = SSTATE_MAGIC; state->line = i; state->type = PORT_UNKNOWN; state->custom_divisor = 0; state->close_delay = 5*HZ/10; state->closing_wait = 30*HZ; state->callout_termios = callout_driver.init_termios; state->normal_termios = serial_driver.init_termios; state->icount.cts = state->icount.dsr = state->icount.rng = state->icount.dcd = 0; state->icount.rx = state->icount.tx = 0; state->icount.frame = state->icount.parity = 0; state->icount.overrun = state->icount.brk = 0; printk(KERN_INFO "ttyS%02d at 0x%04x is a %s\n", i, (unsigned int)(state->port), (state->smc_scc_num & NUM_IS_SCC) ? "SCC" : "SMC");#ifdef CONFIG_SERIAL_CONSOLE /* If we just printed the message on the console port, and * we are about to initialize it for general use, we have * to wait a couple of character times for
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -