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 static DEFINE_SPINLOCK(timer_lock);
40 
41 static int rs_open(struct tty_struct *tty, struct file * filp)
42 {
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 	return 0;
49 }
50 
51 static void rs_close(struct tty_struct *tty, struct file * filp)
52 {
53 	spin_lock_bh(&timer_lock);
54 	if (tty->count == 1)
55 		del_timer_sync(&serial_timer);
56 	spin_unlock_bh(&timer_lock);
57 }
58 
59 
60 static int rs_write(struct tty_struct * tty,
61 		    const unsigned char *buf, int count)
62 {
63 	/* see drivers/char/serialX.c to reference original version */
64 
65 	simc_write(1, buf, count);
66 	return count;
67 }
68 
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 	unsigned char c;
75 
76 	spin_lock(&timer_lock);
77 
78 	while (simc_poll(0)) {
79 		rd = simc_read(0, &c, 1);
80 		if (rd <= 0)
81 			break;
82 		tty_insert_flip_char(port, c, TTY_NORMAL);
83 		i++;
84 	}
85 
86 	if (i)
87 		tty_flip_buffer_push(port);
88 	if (rd)
89 		mod_timer(&serial_timer, jiffies + SERIAL_TIMER_VALUE);
90 	spin_unlock(&timer_lock);
91 }
92 
93 
94 static int rs_put_char(struct tty_struct *tty, unsigned char ch)
95 {
96 	return rs_write(tty, &ch, 1);
97 }
98 
99 static void rs_flush_chars(struct tty_struct *tty)
100 {
101 }
102 
103 static unsigned int rs_write_room(struct tty_struct *tty)
104 {
105 	/* Let's say iss can always accept 2K characters.. */
106 	return 2 * 1024;
107 }
108 
109 static void rs_hangup(struct tty_struct *tty)
110 {
111 	/* Stub, once again.. */
112 }
113 
114 static void rs_wait_until_sent(struct tty_struct *tty, int timeout)
115 {
116 	/* Stub, once again.. */
117 }
118 
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 	return 0;
123 }
124 
125 static const struct tty_operations serial_ops = {
126 	.open = rs_open,
127 	.close = rs_close,
128 	.write = rs_write,
129 	.put_char = rs_put_char,
130 	.flush_chars = rs_flush_chars,
131 	.write_room = rs_write_room,
132 	.hangup = rs_hangup,
133 	.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;
140 	int ret;
141 
142 	driver = tty_alloc_driver(SERIAL_MAX_NUM_LINES, TTY_DRIVER_REAL_RAW);
143 	if (IS_ERR(driver))
144 		return PTR_ERR(driver);
145 
146 	tty_port_init(&serial_port);
147 
148 	/* Initialize the tty_driver structure */
149 
150 	driver->driver_name = "iss_serial";
151 	driver->name = "ttyS";
152 	driver->major = TTY_MAJOR;
153 	driver->minor_start = 64;
154 	driver->type = TTY_DRIVER_TYPE_SERIAL;
155 	driver->subtype = SERIAL_TYPE_NORMAL;
156 	driver->init_termios = tty_std_termios;
157 	driver->init_termios.c_cflag =
158 		B9600 | CS8 | CREAD | HUPCL | CLOCAL;
159 
160 	tty_set_operations(driver, &serial_ops);
161 	tty_port_link_device(&serial_port, driver, 0);
162 
163 	ret = tty_register_driver(driver);
164 	if (ret) {
165 		pr_err("Couldn't register serial driver\n");
166 		tty_driver_kref_put(driver);
167 		tty_port_destroy(&serial_port);
168 
169 		return ret;
170 	}
171 
172 	serial_driver = driver;
173 
174 	return 0;
175 }
176 
177 
178 static __exit void rs_exit(void)
179 {
180 	tty_unregister_driver(serial_driver);
181 	tty_driver_kref_put(serial_driver);
182 	tty_port_destroy(&serial_port);
183 }
184 
185 
186 /* We use `late_initcall' instead of just `__initcall' as a workaround for
187  * the fact that (1) simcons_tty_init can't be called before tty_init,
188  * (2) tty_init is called via `module_init', (3) if statically linked,
189  * module_init == device_init, and (4) there's no ordering of init lists.
190  * We can do this easily because simcons is always statically linked, but
191  * other tty drivers that depend on tty_init and which must use
192  * `module_init' to declare their init routines are likely to be broken.
193  */
194 
195 late_initcall(rs_init);
196 
197 
198 #ifdef CONFIG_SERIAL_CONSOLE
199 
200 static void iss_console_write(struct console *co, const char *s, unsigned count)
201 {
202 	if (s && *s != 0) {
203 		int len = strlen(s);
204 		simc_write(1, s, count < len ? count : len);
205 	}
206 }
207 
208 static struct tty_driver* iss_console_device(struct console *c, int *index)
209 {
210 	*index = c->index;
211 	return serial_driver;
212 }
213 
214 
215 static struct console sercons = {
216 	.name = "ttyS",
217 	.write = iss_console_write,
218 	.device = iss_console_device,
219 	.flags = CON_PRINTBUFFER,
220 	.index = -1
221 };
222 
223 static int __init iss_console_init(void)
224 {
225 	register_console(&sercons);
226 	return 0;
227 }
228 
229 console_initcall(iss_console_init);
230 
231 #endif /* CONFIG_SERIAL_CONSOLE */
232 
233