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/slab.h>
18 #include <linux/mm.h>
19 #include <linux/major.h>
20 #include <linux/param.h>
21 #include <linux/seq_file.h>
22 #include <linux/serial.h>
23 #include <linux/serialP.h>
24 
25 #include <asm/uaccess.h>
26 #include <asm/irq.h>
27 
28 #include <platform/simcall.h>
29 
30 #include <linux/tty.h>
31 #include <linux/tty_flip.h>
32 
33 #ifdef SERIAL_INLINE
34 #define _INLINE_ inline
35 #endif
36 
37 #define SERIAL_MAX_NUM_LINES 1
38 #define SERIAL_TIMER_VALUE (20 * HZ)
39 
40 static struct tty_driver *serial_driver;
41 static struct timer_list serial_timer;
42 
43 static DEFINE_SPINLOCK(timer_lock);
44 
45 int errno;
46 
47 static int __simc (int a, int b, int c, int d, int e, int f) __attribute__((__noinline__));
48 static int __simc (int a, int b, int c, int d, int e, int f)
49 {
50 	int ret;
51 	__asm__ __volatile__ ("simcall\n"
52 			"mov %0, a2\n"
53 			"mov %1, a3\n" : "=a" (ret), "=a" (errno)
54 			: : "a2", "a3");
55 	return ret;
56 }
57 
58 static char *serial_version = "0.1";
59 static char *serial_name = "ISS serial driver";
60 
61 /*
62  * This routine is called whenever a serial port is opened.  It
63  * enables interrupts for a serial port, linking in its async structure into
64  * the IRQ chain.   It also performs the serial-specific
65  * initialization for the tty structure.
66  */
67 
68 static void rs_poll(unsigned long);
69 
70 static int rs_open(struct tty_struct *tty, struct file * filp)
71 {
72 	int line = tty->index;
73 
74 	if ((line < 0) || (line >= SERIAL_MAX_NUM_LINES))
75 		return -ENODEV;
76 
77 	spin_lock(&timer_lock);
78 
79 	if (tty->count == 1) {
80 		init_timer(&serial_timer);
81 		serial_timer.data = (unsigned long) tty;
82 		serial_timer.function = rs_poll;
83 		mod_timer(&serial_timer, jiffies + SERIAL_TIMER_VALUE);
84 	}
85 	spin_unlock(&timer_lock);
86 
87 	return 0;
88 }
89 
90 
91 /*
92  * ------------------------------------------------------------
93  * iss_serial_close()
94  *
95  * This routine is called when the serial port gets closed.  First, we
96  * wait for the last remaining data to be sent.  Then, we unlink its
97  * async structure from the interrupt chain if necessary, and we free
98  * that IRQ if nothing is left in the chain.
99  * ------------------------------------------------------------
100  */
101 static void rs_close(struct tty_struct *tty, struct file * filp)
102 {
103 	spin_lock(&timer_lock);
104 	if (tty->count == 1)
105 		del_timer_sync(&serial_timer);
106 	spin_unlock(&timer_lock);
107 }
108 
109 
110 static int rs_write(struct tty_struct * tty,
111 		    const unsigned char *buf, int count)
112 {
113 	/* see drivers/char/serialX.c to reference original version */
114 
115 	__simc (SYS_write, 1, (unsigned long)buf, count, 0, 0);
116 	return count;
117 }
118 
119 static void rs_poll(unsigned long priv)
120 {
121 	struct tty_struct* tty = (struct tty_struct*) priv;
122 
123 	struct timeval tv = { .tv_sec = 0, .tv_usec = 0 };
124 	int i = 0;
125 	unsigned char c;
126 
127 	spin_lock(&timer_lock);
128 
129 	while (__simc(SYS_select_one, 0, XTISS_SELECT_ONE_READ, (int)&tv,0,0)){
130 		__simc (SYS_read, 0, (unsigned long)&c, 1, 0, 0);
131 		tty_insert_flip_char(tty, c, TTY_NORMAL);
132 		i++;
133 	}
134 
135 	if (i)
136 		tty_flip_buffer_push(tty);
137 
138 
139 	mod_timer(&serial_timer, jiffies + SERIAL_TIMER_VALUE);
140 	spin_unlock(&timer_lock);
141 }
142 
143 
144 static int rs_put_char(struct tty_struct *tty, unsigned char ch)
145 {
146 	char buf[2];
147 
148 	buf[0] = ch;
149 	buf[1] = '\0';		/* Is this NULL necessary? */
150 	__simc (SYS_write, 1, (unsigned long) buf, 1, 0, 0);
151 	return 1;
152 }
153 
154 static void rs_flush_chars(struct tty_struct *tty)
155 {
156 }
157 
158 static int rs_write_room(struct tty_struct *tty)
159 {
160 	/* Let's say iss can always accept 2K characters.. */
161 	return 2 * 1024;
162 }
163 
164 static int rs_chars_in_buffer(struct tty_struct *tty)
165 {
166 	/* the iss doesn't buffer characters */
167 	return 0;
168 }
169 
170 static void rs_hangup(struct tty_struct *tty)
171 {
172 	/* Stub, once again.. */
173 }
174 
175 static void rs_wait_until_sent(struct tty_struct *tty, int timeout)
176 {
177 	/* Stub, once again.. */
178 }
179 
180 static int rs_proc_show(struct seq_file *m, void *v)
181 {
182 	seq_printf(m, "serinfo:1.0 driver:%s\n", serial_version);
183 	return 0;
184 }
185 
186 static int rs_proc_open(struct inode *inode, struct file *file)
187 {
188 	return single_open(file, rs_proc_show, NULL);
189 }
190 
191 static const struct file_operations rs_proc_fops = {
192 	.owner		= THIS_MODULE,
193 	.open		= rs_proc_open,
194 	.read		= seq_read,
195 	.llseek		= seq_lseek,
196 	.release	= single_release,
197 };
198 
199 static struct tty_operations serial_ops = {
200 	.open = rs_open,
201 	.close = rs_close,
202 	.write = rs_write,
203 	.put_char = rs_put_char,
204 	.flush_chars = rs_flush_chars,
205 	.write_room = rs_write_room,
206 	.chars_in_buffer = rs_chars_in_buffer,
207 	.hangup = rs_hangup,
208 	.wait_until_sent = rs_wait_until_sent,
209 	.proc_fops = &rs_proc_fops,
210 };
211 
212 int __init rs_init(void)
213 {
214 	serial_driver = alloc_tty_driver(1);
215 
216 	printk ("%s %s\n", serial_name, serial_version);
217 
218 	/* Initialize the tty_driver structure */
219 
220 	serial_driver->owner = THIS_MODULE;
221 	serial_driver->driver_name = "iss_serial";
222 	serial_driver->name = "ttyS";
223 	serial_driver->major = TTY_MAJOR;
224 	serial_driver->minor_start = 64;
225 	serial_driver->type = TTY_DRIVER_TYPE_SERIAL;
226 	serial_driver->subtype = SERIAL_TYPE_NORMAL;
227 	serial_driver->init_termios = tty_std_termios;
228 	serial_driver->init_termios.c_cflag =
229 		B9600 | CS8 | CREAD | HUPCL | CLOCAL;
230 	serial_driver->flags = TTY_DRIVER_REAL_RAW;
231 
232 	tty_set_operations(serial_driver, &serial_ops);
233 
234 	if (tty_register_driver(serial_driver))
235 		panic("Couldn't register serial driver\n");
236 	return 0;
237 }
238 
239 
240 static __exit void rs_exit(void)
241 {
242 	int error;
243 
244 	if ((error = tty_unregister_driver(serial_driver)))
245 		printk("ISS_SERIAL: failed to unregister serial driver (%d)\n",
246 		       error);
247 	put_tty_driver(serial_driver);
248 }
249 
250 
251 /* We use `late_initcall' instead of just `__initcall' as a workaround for
252  * the fact that (1) simcons_tty_init can't be called before tty_init,
253  * (2) tty_init is called via `module_init', (3) if statically linked,
254  * module_init == device_init, and (4) there's no ordering of init lists.
255  * We can do this easily because simcons is always statically linked, but
256  * other tty drivers that depend on tty_init and which must use
257  * `module_init' to declare their init routines are likely to be broken.
258  */
259 
260 late_initcall(rs_init);
261 
262 
263 #ifdef CONFIG_SERIAL_CONSOLE
264 
265 static void iss_console_write(struct console *co, const char *s, unsigned count)
266 {
267 	int len = strlen(s);
268 
269 	if (s != 0 && *s != 0)
270 		__simc (SYS_write, 1, (unsigned long)s,
271 			count < len ? count : len,0,0);
272 }
273 
274 static struct tty_driver* iss_console_device(struct console *c, int *index)
275 {
276 	*index = c->index;
277 	return serial_driver;
278 }
279 
280 
281 static struct console sercons = {
282 	.name = "ttyS",
283 	.write = iss_console_write,
284 	.device = iss_console_device,
285 	.flags = CON_PRINTBUFFER,
286 	.index = -1
287 };
288 
289 static int __init iss_console_init(void)
290 {
291 	register_console(&sercons);
292 	return 0;
293 }
294 
295 console_initcall(iss_console_init);
296 
297 #endif /* CONFIG_SERIAL_CONSOLE */
298 
299