console.c 5.0 KB
Newer Older
1
/*
2
 * arch/xtensa/platforms/iss/console.c
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
 *
 * This file is subject to the terms and conditions of the GNU General Public
 * License.  See the file "COPYING" in the main directory of this archive
 * for more details.
 *
 * Copyright (C) 2001-2005 Tensilica Inc.
 *   Authors	Christian Zankel, Joe Taylor
 */

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/sched.h>
#include <linux/console.h>
#include <linux/init.h>
#include <linux/mm.h>
#include <linux/major.h>
#include <linux/param.h>
20
#include <linux/seq_file.h>
21 22
#include <linux/serial.h>

23
#include <linux/uaccess.h>
24 25
#include <asm/irq.h>

26
#include <platform/simcall.h>
27 28 29 30 31

#include <linux/tty.h>
#include <linux/tty_flip.h>

#define SERIAL_MAX_NUM_LINES 1
32
#define SERIAL_TIMER_VALUE (HZ / 10)
33

34 35
static void rs_poll(struct timer_list *);

36
static struct tty_driver *serial_driver;
J
Jiri Slaby 已提交
37
static struct tty_port serial_port;
38
static DEFINE_TIMER(serial_timer, rs_poll);
39 40 41 42
static DEFINE_SPINLOCK(timer_lock);

static int rs_open(struct tty_struct *tty, struct file * filp)
{
43
	spin_lock_bh(&timer_lock);
44
	if (tty->count == 1)
45
		mod_timer(&serial_timer, jiffies + SERIAL_TIMER_VALUE);
46
	spin_unlock_bh(&timer_lock);
47 48 49 50 51 52

	return 0;
}

static void rs_close(struct tty_struct *tty, struct file * filp)
{
53
	spin_lock_bh(&timer_lock);
54 55
	if (tty->count == 1)
		del_timer_sync(&serial_timer);
56
	spin_unlock_bh(&timer_lock);
57 58 59 60 61 62 63 64
}


static int rs_write(struct tty_struct * tty,
		    const unsigned char *buf, int count)
{
	/* see drivers/char/serialX.c to reference original version */

M
Max Filippov 已提交
65
	simc_write(1, buf, count);
66 67 68
	return count;
}

69
static void rs_poll(struct timer_list *unused)
70
{
71
	struct tty_port *port = &serial_port;
72
	int i = 0;
73
	int rd = 1;
74 75 76 77
	unsigned char c;

	spin_lock(&timer_lock);

M
Max Filippov 已提交
78
	while (simc_poll(0)) {
79 80 81
		rd = simc_read(0, &c, 1);
		if (rd <= 0)
			break;
J
Jiri Slaby 已提交
82
		tty_insert_flip_char(port, c, TTY_NORMAL);
83 84 85 86
		i++;
	}

	if (i)
J
Jiri Slaby 已提交
87
		tty_flip_buffer_push(port);
88 89
	if (rd)
		mod_timer(&serial_timer, jiffies + SERIAL_TIMER_VALUE);
90 91 92 93
	spin_unlock(&timer_lock);
}


94
static int rs_put_char(struct tty_struct *tty, unsigned char ch)
95
{
M
Max Filippov 已提交
96
	return rs_write(tty, &ch, 1);
97 98 99 100 101 102
}

static void rs_flush_chars(struct tty_struct *tty)
{
}

103
static unsigned int rs_write_room(struct tty_struct *tty)
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
{
	/* Let's say iss can always accept 2K characters.. */
	return 2 * 1024;
}

static void rs_hangup(struct tty_struct *tty)
{
	/* Stub, once again.. */
}

static void rs_wait_until_sent(struct tty_struct *tty, int timeout)
{
	/* Stub, once again.. */
}

119
static int rs_proc_show(struct seq_file *m, void *v)
120
{
121
	seq_printf(m, "serinfo:1.0 driver:0.1\n");
122 123
	return 0;
}
124

125
static const struct tty_operations serial_ops = {
126 127 128 129 130 131 132 133
	.open = rs_open,
	.close = rs_close,
	.write = rs_write,
	.put_char = rs_put_char,
	.flush_chars = rs_flush_chars,
	.write_room = rs_write_room,
	.hangup = rs_hangup,
	.wait_until_sent = rs_wait_until_sent,
134
	.proc_show = rs_proc_show,
135 136
};

137
static int __init rs_init(void)
138
{
139
	struct tty_driver *driver;
J
Jiri Slaby 已提交
140
	int ret;
J
Jiri Slaby 已提交
141

142 143
	driver = alloc_tty_driver(SERIAL_MAX_NUM_LINES);
	if (!driver)
J
Jiri Slaby 已提交
144 145 146
		return -ENOMEM;

	tty_port_init(&serial_port);
147 148 149

	/* Initialize the tty_driver structure */

150 151 152 153 154 155 156 157
	driver->driver_name = "iss_serial";
	driver->name = "ttyS";
	driver->major = TTY_MAJOR;
	driver->minor_start = 64;
	driver->type = TTY_DRIVER_TYPE_SERIAL;
	driver->subtype = SERIAL_TYPE_NORMAL;
	driver->init_termios = tty_std_termios;
	driver->init_termios.c_cflag =
158
		B9600 | CS8 | CREAD | HUPCL | CLOCAL;
159
	driver->flags = TTY_DRIVER_REAL_RAW;
160

161 162
	tty_set_operations(driver, &serial_ops);
	tty_port_link_device(&serial_port, driver, 0);
163

164
	ret = tty_register_driver(driver);
J
Jiri Slaby 已提交
165 166
	if (ret) {
		pr_err("Couldn't register serial driver\n");
167
		tty_driver_kref_put(driver);
J
Jiri Slaby 已提交
168 169 170 171 172
		tty_port_destroy(&serial_port);

		return ret;
	}

173 174
	serial_driver = driver;

175 176 177 178 179 180
	return 0;
}


static __exit void rs_exit(void)
{
181
	tty_unregister_driver(serial_driver);
182
	put_tty_driver(serial_driver);
183
	tty_port_destroy(&serial_port);
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
}


/* We use `late_initcall' instead of just `__initcall' as a workaround for
 * the fact that (1) simcons_tty_init can't be called before tty_init,
 * (2) tty_init is called via `module_init', (3) if statically linked,
 * module_init == device_init, and (4) there's no ordering of init lists.
 * We can do this easily because simcons is always statically linked, but
 * other tty drivers that depend on tty_init and which must use
 * `module_init' to declare their init routines are likely to be broken.
 */

late_initcall(rs_init);


#ifdef CONFIG_SERIAL_CONSOLE

static void iss_console_write(struct console *co, const char *s, unsigned count)
{
	int len = strlen(s);

	if (s != 0 && *s != 0)
M
Max Filippov 已提交
206
		simc_write(1, s, count < len ? count : len);
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
}

static struct tty_driver* iss_console_device(struct console *c, int *index)
{
	*index = c->index;
	return serial_driver;
}


static struct console sercons = {
	.name = "ttyS",
	.write = iss_console_write,
	.device = iss_console_device,
	.flags = CON_PRINTBUFFER,
	.index = -1
};

static int __init iss_console_init(void)
{
	register_console(&sercons);
	return 0;
}

console_initcall(iss_console_init);

#endif /* CONFIG_SERIAL_CONSOLE */