1 /*
2  * arch/xtensa/platforms/iss/console.c
3  *
4  * This file is subject to the terms and conditions of the GNU General Public
5  * License.  See the file "COPYING" in the main directory of this archive
6  * for more details.
7  *
8  * Copyright (C) 2001-2005 Tensilica Inc.
9  *   Authors	Christian Zankel, Joe Taylor
10  */
11 
12 #include <linux/module.h>
13 #include <linux/kernel.h>
14 #include <linux/sched.h>
15 #include <linux/console.h>
16 #include <linux/init.h>
17 #include <linux/mm.h>
18 #include <linux/major.h>
19 #include <linux/param.h>
20 #include <linux/seq_file.h>
21 #include <linux/serial.h>
22 
23 #include <linux/uaccess.h>
24 #include <asm/irq.h>
25 
26 #include <platform/simcall.h>
27 
28 #include <linux/tty.h>
29 #include <linux/tty_flip.h>
30 
31 #define SERIAL_MAX_NUM_LINES 1
32 #define SERIAL_TIMER_VALUE (HZ / 10)
33 
34 static void rs_poll(struct timer_list *);
35 
36 static struct tty_driver *serial_driver;
37 static struct tty_port serial_port;
38 static DEFINE_TIMER(serial_timer, rs_poll);
39 
40 static int rs_open(struct tty_struct *tty, struct file * filp)
41 {
42 	if (tty->count == 1)
43 		mod_timer(&serial_timer, jiffies + SERIAL_TIMER_VALUE);
44 
45 	return 0;
46 }
47 
48 static void rs_close(struct tty_struct *tty, struct file * filp)
49 {
50 	if (tty->count == 1)
51 		del_timer_sync(&serial_timer);
52 }
53 
54 
55 static int rs_write(struct tty_struct * tty,
56 		    const unsigned char *buf, int count)
57 {
58 	/* see drivers/char/serialX.c to reference original version */
59 
60 	simc_write(1, buf, count);
61 	return count;
62 }
63 
64 static void rs_poll(struct timer_list *unused)
65 {
66 	struct tty_port *port = &serial_port;
67 	int i = 0;
68 	int rd = 1;
69 	unsigned char c;
70 
71 	while (simc_poll(0)) {
72 		rd = simc_read(0, &c, 1);
73 		if (rd <= 0)
74 			break;
75 		tty_insert_flip_char(port, c, TTY_NORMAL);
76 		i++;
77 	}
78 
79 	if (i)
80 		tty_flip_buffer_push(port);
81 	if (rd)
82 		mod_timer(&serial_timer, jiffies + SERIAL_TIMER_VALUE);
83 }
84 
85 
86 static int rs_put_char(struct tty_struct *tty, unsigned char ch)
87 {
88 	return rs_write(tty, &ch, 1);
89 }
90 
91 static void rs_flush_chars(struct tty_struct *tty)
92 {
93 }
94 
95 static unsigned int rs_write_room(struct tty_struct *tty)
96 {
97 	/* Let's say iss can always accept 2K characters.. */
98 	return 2 * 1024;
99 }
100 
101 static void rs_hangup(struct tty_struct *tty)
102 {
103 	/* Stub, once again.. */
104 }
105 
106 static void rs_wait_until_sent(struct tty_struct *tty, int timeout)
107 {
108 	/* Stub, once again.. */
109 }
110 
111 static int rs_proc_show(struct seq_file *m, void *v)
112 {
113 	seq_printf(m, "serinfo:1.0 driver:0.1\n");
114 	return 0;
115 }
116 
117 static const struct tty_operations serial_ops = {
118 	.open = rs_open,
119 	.close = rs_close,
120 	.write = rs_write,
121 	.put_char = rs_put_char,
122 	.flush_chars = rs_flush_chars,
123 	.write_room = rs_write_room,
124 	.hangup = rs_hangup,
125 	.wait_until_sent = rs_wait_until_sent,
126 	.proc_show = rs_proc_show,
127 };
128 
129 static int __init rs_init(void)
130 {
131 	struct tty_driver *driver;
132 	int ret;
133 
134 	driver = tty_alloc_driver(SERIAL_MAX_NUM_LINES, TTY_DRIVER_REAL_RAW);
135 	if (IS_ERR(driver))
136 		return PTR_ERR(driver);
137 
138 	tty_port_init(&serial_port);
139 
140 	/* Initialize the tty_driver structure */
141 
142 	driver->driver_name = "iss_serial";
143 	driver->name = "ttyS";
144 	driver->major = TTY_MAJOR;
145 	driver->minor_start = 64;
146 	driver->type = TTY_DRIVER_TYPE_SERIAL;
147 	driver->subtype = SERIAL_TYPE_NORMAL;
148 	driver->init_termios = tty_std_termios;
149 	driver->init_termios.c_cflag =
150 		B9600 | CS8 | CREAD | HUPCL | CLOCAL;
151 
152 	tty_set_operations(driver, &serial_ops);
153 	tty_port_link_device(&serial_port, driver, 0);
154 
155 	ret = tty_register_driver(driver);
156 	if (ret) {
157 		pr_err("Couldn't register serial driver\n");
158 		tty_driver_kref_put(driver);
159 		tty_port_destroy(&serial_port);
160 
161 		return ret;
162 	}
163 
164 	serial_driver = driver;
165 
166 	return 0;
167 }
168 
169 
170 static __exit void rs_exit(void)
171 {
172 	tty_unregister_driver(serial_driver);
173 	tty_driver_kref_put(serial_driver);
174 	tty_port_destroy(&serial_port);
175 }
176 
177 
178 /* We use `late_initcall' instead of just `__initcall' as a workaround for
179  * the fact that (1) simcons_tty_init can't be called before tty_init,
180  * (2) tty_init is called via `module_init', (3) if statically linked,
181  * module_init == device_init, and (4) there's no ordering of init lists.
182  * We can do this easily because simcons is always statically linked, but
183  * other tty drivers that depend on tty_init and which must use
184  * `module_init' to declare their init routines are likely to be broken.
185  */
186 
187 late_initcall(rs_init);
188 
189 
190 #ifdef CONFIG_SERIAL_CONSOLE
191 
192 static void iss_console_write(struct console *co, const char *s, unsigned count)
193 {
194 	if (s && *s != 0) {
195 		int len = strlen(s);
196 		simc_write(1, s, count < len ? count : len);
197 	}
198 }
199 
200 static struct tty_driver* iss_console_device(struct console *c, int *index)
201 {
202 	*index = c->index;
203 	return serial_driver;
204 }
205 
206 
207 static struct console sercons = {
208 	.name = "ttyS",
209 	.write = iss_console_write,
210 	.device = iss_console_device,
211 	.flags = CON_PRINTBUFFER,
212 	.index = -1
213 };
214 
215 static int __init iss_console_init(void)
216 {
217 	register_console(&sercons);
218 	return 0;
219 }
220 
221 console_initcall(iss_console_init);
222 
223 #endif /* CONFIG_SERIAL_CONSOLE */
224 
225