Path: blob/master/arch/xtensa/platforms/iss/console.c
15116 views
/*1* arch/xtensa/platforms/iss/console.c2*3* This file is subject to the terms and conditions of the GNU General Public4* License. See the file "COPYING" in the main directory of this archive5* for more details.6*7* Copyright (C) 2001-2005 Tensilica Inc.8* Authors Christian Zankel, Joe Taylor9*/1011#include <linux/module.h>12#include <linux/kernel.h>13#include <linux/sched.h>14#include <linux/console.h>15#include <linux/init.h>16#include <linux/mm.h>17#include <linux/major.h>18#include <linux/param.h>19#include <linux/seq_file.h>20#include <linux/serial.h>21#include <linux/serialP.h>2223#include <asm/uaccess.h>24#include <asm/irq.h>2526#include <platform/simcall.h>2728#include <linux/tty.h>29#include <linux/tty_flip.h>3031#ifdef SERIAL_INLINE32#define _INLINE_ inline33#endif3435#define SERIAL_MAX_NUM_LINES 136#define SERIAL_TIMER_VALUE (20 * HZ)3738static struct tty_driver *serial_driver;39static struct timer_list serial_timer;4041static DEFINE_SPINLOCK(timer_lock);4243int errno;4445static int __simc (int a, int b, int c, int d, int e, int f) __attribute__((__noinline__));46static int __simc (int a, int b, int c, int d, int e, int f)47{48int ret;49__asm__ __volatile__ ("simcall\n"50"mov %0, a2\n"51"mov %1, a3\n" : "=a" (ret), "=a" (errno)52: : "a2", "a3");53return ret;54}5556static char *serial_version = "0.1";57static char *serial_name = "ISS serial driver";5859/*60* This routine is called whenever a serial port is opened. It61* enables interrupts for a serial port, linking in its async structure into62* the IRQ chain. It also performs the serial-specific63* initialization for the tty structure.64*/6566static void rs_poll(unsigned long);6768static int rs_open(struct tty_struct *tty, struct file * filp)69{70int line = tty->index;7172if ((line < 0) || (line >= SERIAL_MAX_NUM_LINES))73return -ENODEV;7475spin_lock(&timer_lock);7677if (tty->count == 1) {78init_timer(&serial_timer);79serial_timer.data = (unsigned long) tty;80serial_timer.function = rs_poll;81mod_timer(&serial_timer, jiffies + SERIAL_TIMER_VALUE);82}83spin_unlock(&timer_lock);8485return 0;86}878889/*90* ------------------------------------------------------------91* iss_serial_close()92*93* This routine is called when the serial port gets closed. First, we94* wait for the last remaining data to be sent. Then, we unlink its95* async structure from the interrupt chain if necessary, and we free96* that IRQ if nothing is left in the chain.97* ------------------------------------------------------------98*/99static void rs_close(struct tty_struct *tty, struct file * filp)100{101spin_lock(&timer_lock);102if (tty->count == 1)103del_timer_sync(&serial_timer);104spin_unlock(&timer_lock);105}106107108static int rs_write(struct tty_struct * tty,109const unsigned char *buf, int count)110{111/* see drivers/char/serialX.c to reference original version */112113__simc (SYS_write, 1, (unsigned long)buf, count, 0, 0);114return count;115}116117static void rs_poll(unsigned long priv)118{119struct tty_struct* tty = (struct tty_struct*) priv;120121struct timeval tv = { .tv_sec = 0, .tv_usec = 0 };122int i = 0;123unsigned char c;124125spin_lock(&timer_lock);126127while (__simc(SYS_select_one, 0, XTISS_SELECT_ONE_READ, (int)&tv,0,0)){128__simc (SYS_read, 0, (unsigned long)&c, 1, 0, 0);129tty_insert_flip_char(tty, c, TTY_NORMAL);130i++;131}132133if (i)134tty_flip_buffer_push(tty);135136137mod_timer(&serial_timer, jiffies + SERIAL_TIMER_VALUE);138spin_unlock(&timer_lock);139}140141142static int rs_put_char(struct tty_struct *tty, unsigned char ch)143{144char buf[2];145146buf[0] = ch;147buf[1] = '\0'; /* Is this NULL necessary? */148__simc (SYS_write, 1, (unsigned long) buf, 1, 0, 0);149return 1;150}151152static void rs_flush_chars(struct tty_struct *tty)153{154}155156static int rs_write_room(struct tty_struct *tty)157{158/* Let's say iss can always accept 2K characters.. */159return 2 * 1024;160}161162static int rs_chars_in_buffer(struct tty_struct *tty)163{164/* the iss doesn't buffer characters */165return 0;166}167168static void rs_hangup(struct tty_struct *tty)169{170/* Stub, once again.. */171}172173static void rs_wait_until_sent(struct tty_struct *tty, int timeout)174{175/* Stub, once again.. */176}177178static int rs_proc_show(struct seq_file *m, void *v)179{180seq_printf(m, "serinfo:1.0 driver:%s\n", serial_version);181return 0;182}183184static int rs_proc_open(struct inode *inode, struct file *file)185{186return single_open(file, rs_proc_show, NULL);187}188189static const struct file_operations rs_proc_fops = {190.owner = THIS_MODULE,191.open = rs_proc_open,192.read = seq_read,193.llseek = seq_lseek,194.release = single_release,195};196197static const struct tty_operations serial_ops = {198.open = rs_open,199.close = rs_close,200.write = rs_write,201.put_char = rs_put_char,202.flush_chars = rs_flush_chars,203.write_room = rs_write_room,204.chars_in_buffer = rs_chars_in_buffer,205.hangup = rs_hangup,206.wait_until_sent = rs_wait_until_sent,207.proc_fops = &rs_proc_fops,208};209210int __init rs_init(void)211{212serial_driver = alloc_tty_driver(1);213214printk ("%s %s\n", serial_name, serial_version);215216/* Initialize the tty_driver structure */217218serial_driver->owner = THIS_MODULE;219serial_driver->driver_name = "iss_serial";220serial_driver->name = "ttyS";221serial_driver->major = TTY_MAJOR;222serial_driver->minor_start = 64;223serial_driver->type = TTY_DRIVER_TYPE_SERIAL;224serial_driver->subtype = SERIAL_TYPE_NORMAL;225serial_driver->init_termios = tty_std_termios;226serial_driver->init_termios.c_cflag =227B9600 | CS8 | CREAD | HUPCL | CLOCAL;228serial_driver->flags = TTY_DRIVER_REAL_RAW;229230tty_set_operations(serial_driver, &serial_ops);231232if (tty_register_driver(serial_driver))233panic("Couldn't register serial driver\n");234return 0;235}236237238static __exit void rs_exit(void)239{240int error;241242if ((error = tty_unregister_driver(serial_driver)))243printk("ISS_SERIAL: failed to unregister serial driver (%d)\n",244error);245put_tty_driver(serial_driver);246}247248249/* We use `late_initcall' instead of just `__initcall' as a workaround for250* the fact that (1) simcons_tty_init can't be called before tty_init,251* (2) tty_init is called via `module_init', (3) if statically linked,252* module_init == device_init, and (4) there's no ordering of init lists.253* We can do this easily because simcons is always statically linked, but254* other tty drivers that depend on tty_init and which must use255* `module_init' to declare their init routines are likely to be broken.256*/257258late_initcall(rs_init);259260261#ifdef CONFIG_SERIAL_CONSOLE262263static void iss_console_write(struct console *co, const char *s, unsigned count)264{265int len = strlen(s);266267if (s != 0 && *s != 0)268__simc (SYS_write, 1, (unsigned long)s,269count < len ? count : len,0,0);270}271272static struct tty_driver* iss_console_device(struct console *c, int *index)273{274*index = c->index;275return serial_driver;276}277278279static struct console sercons = {280.name = "ttyS",281.write = iss_console_write,282.device = iss_console_device,283.flags = CON_PRINTBUFFER,284.index = -1285};286287static int __init iss_console_init(void)288{289register_console(&sercons);290return 0;291}292293console_initcall(iss_console_init);294295#endif /* CONFIG_SERIAL_CONSOLE */296297298299