]>
Commit | Line | Data |
---|---|---|
02fd473b DM |
1 | /* sunhv.c: Serial driver for SUN4V hypervisor console. |
2 | * | |
3 | * Copyright (C) 2006 David S. Miller (davem@davemloft.net) | |
4 | */ | |
5 | ||
6 | #include <linux/module.h> | |
7 | #include <linux/kernel.h> | |
8 | #include <linux/errno.h> | |
9 | #include <linux/tty.h> | |
10 | #include <linux/tty_flip.h> | |
11 | #include <linux/major.h> | |
12 | #include <linux/circ_buf.h> | |
13 | #include <linux/serial.h> | |
14 | #include <linux/sysrq.h> | |
15 | #include <linux/console.h> | |
16 | #include <linux/spinlock.h> | |
17 | #include <linux/slab.h> | |
18 | #include <linux/delay.h> | |
19 | #include <linux/init.h> | |
20 | ||
21 | #include <asm/hypervisor.h> | |
22 | #include <asm/spitfire.h> | |
23 | ||
24 | #if defined(CONFIG_MAGIC_SYSRQ) | |
25 | #define SUPPORT_SYSRQ | |
26 | #endif | |
27 | ||
28 | #include <linux/serial_core.h> | |
29 | ||
30 | #include "suncore.h" | |
31 | ||
32 | #define CON_BREAK ((long)-1) | |
33 | #define CON_HUP ((long)-2) | |
34 | ||
35 | static inline long hypervisor_con_getchar(long *status) | |
36 | { | |
37 | register unsigned long func asm("%o5"); | |
38 | register unsigned long arg0 asm("%o0"); | |
39 | register unsigned long arg1 asm("%o1"); | |
40 | ||
41 | func = HV_FAST_CONS_GETCHAR; | |
42 | arg0 = 0; | |
43 | arg1 = 0; | |
44 | __asm__ __volatile__("ta %6" | |
45 | : "=&r" (func), "=&r" (arg0), "=&r" (arg1) | |
46 | : "0" (func), "1" (arg0), "2" (arg1), | |
47 | "i" (HV_FAST_TRAP)); | |
48 | ||
49 | *status = arg0; | |
50 | ||
51 | return (long) arg1; | |
52 | } | |
53 | ||
54 | static inline long hypervisor_con_putchar(long ch) | |
55 | { | |
56 | register unsigned long func asm("%o5"); | |
57 | register unsigned long arg0 asm("%o0"); | |
58 | ||
59 | func = HV_FAST_CONS_PUTCHAR; | |
60 | arg0 = ch; | |
61 | __asm__ __volatile__("ta %4" | |
62 | : "=&r" (func), "=&r" (arg0) | |
63 | : "0" (func), "1" (arg0), "i" (HV_FAST_TRAP)); | |
64 | ||
65 | return (long) arg0; | |
66 | } | |
67 | ||
68 | #define IGNORE_BREAK 0x1 | |
69 | #define IGNORE_ALL 0x2 | |
70 | ||
71 | static int hung_up = 0; | |
72 | ||
73 | static struct tty_struct *receive_chars(struct uart_port *port, struct pt_regs *regs) | |
74 | { | |
75 | struct tty_struct *tty = NULL; | |
76 | int saw_console_brk = 0; | |
77 | int limit = 10000; | |
78 | ||
79 | if (port->info != NULL) /* Unopened serial console */ | |
80 | tty = port->info->tty; | |
81 | ||
82 | while (limit-- > 0) { | |
83 | long status; | |
84 | long c = hypervisor_con_getchar(&status); | |
85 | unsigned char flag; | |
86 | ||
87 | if (status == HV_EWOULDBLOCK) | |
88 | break; | |
89 | ||
90 | if (c == CON_BREAK) { | |
91 | saw_console_brk = 1; | |
92 | c = 0; | |
93 | } | |
94 | ||
95 | if (c == CON_HUP) { | |
96 | hung_up = 1; | |
97 | uart_handle_dcd_change(port, 0); | |
98 | } else if (hung_up) { | |
99 | hung_up = 0; | |
100 | uart_handle_dcd_change(port, 1); | |
101 | } | |
102 | ||
103 | if (tty == NULL) { | |
104 | uart_handle_sysrq_char(port, c, regs); | |
105 | continue; | |
106 | } | |
107 | ||
108 | flag = TTY_NORMAL; | |
109 | port->icount.rx++; | |
110 | if (c == CON_BREAK) { | |
111 | port->icount.brk++; | |
112 | if (uart_handle_break(port)) | |
113 | continue; | |
114 | flag = TTY_BREAK; | |
115 | } | |
116 | ||
117 | if (uart_handle_sysrq_char(port, c, regs)) | |
118 | continue; | |
119 | ||
120 | if ((port->ignore_status_mask & IGNORE_ALL) || | |
121 | ((port->ignore_status_mask & IGNORE_BREAK) && | |
122 | (c == CON_BREAK))) | |
123 | continue; | |
124 | ||
125 | tty_insert_flip_char(tty, c, flag); | |
126 | } | |
127 | ||
128 | if (saw_console_brk) | |
129 | sun_do_break(); | |
130 | ||
131 | return tty; | |
132 | } | |
133 | ||
134 | static void transmit_chars(struct uart_port *port) | |
135 | { | |
136 | struct circ_buf *xmit = &port->info->xmit; | |
137 | ||
138 | if (uart_circ_empty(xmit) || uart_tx_stopped(port)) | |
139 | return; | |
140 | ||
141 | while (!uart_circ_empty(xmit)) { | |
142 | long status = hypervisor_con_putchar(xmit->buf[xmit->tail]); | |
143 | ||
144 | if (status != HV_EOK) | |
145 | break; | |
146 | ||
147 | xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1); | |
148 | port->icount.tx++; | |
149 | } | |
150 | ||
151 | if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS) | |
152 | uart_write_wakeup(port); | |
153 | } | |
154 | ||
155 | static irqreturn_t sunhv_interrupt(int irq, void *dev_id, struct pt_regs *regs) | |
156 | { | |
157 | struct uart_port *port = dev_id; | |
158 | struct tty_struct *tty; | |
159 | unsigned long flags; | |
160 | ||
161 | spin_lock_irqsave(&port->lock, flags); | |
162 | tty = receive_chars(port, regs); | |
163 | transmit_chars(port); | |
164 | spin_unlock_irqrestore(&port->lock, flags); | |
165 | ||
166 | if (tty) | |
167 | tty_flip_buffer_push(tty); | |
168 | ||
169 | return IRQ_HANDLED; | |
170 | } | |
171 | ||
172 | /* port->lock is not held. */ | |
173 | static unsigned int sunhv_tx_empty(struct uart_port *port) | |
174 | { | |
175 | /* Transmitter is always empty for us. If the circ buffer | |
176 | * is non-empty or there is an x_char pending, our caller | |
177 | * will do the right thing and ignore what we return here. | |
178 | */ | |
179 | return TIOCSER_TEMT; | |
180 | } | |
181 | ||
182 | /* port->lock held by caller. */ | |
183 | static void sunhv_set_mctrl(struct uart_port *port, unsigned int mctrl) | |
184 | { | |
185 | return; | |
186 | } | |
187 | ||
188 | /* port->lock is held by caller and interrupts are disabled. */ | |
189 | static unsigned int sunhv_get_mctrl(struct uart_port *port) | |
190 | { | |
191 | return TIOCM_DSR | TIOCM_CAR | TIOCM_CTS; | |
192 | } | |
193 | ||
194 | /* port->lock held by caller. */ | |
195 | static void sunhv_stop_tx(struct uart_port *port) | |
196 | { | |
197 | return; | |
198 | } | |
199 | ||
200 | /* port->lock held by caller. */ | |
201 | static void sunhv_start_tx(struct uart_port *port) | |
202 | { | |
203 | struct circ_buf *xmit = &port->info->xmit; | |
204 | unsigned long flags; | |
205 | ||
206 | spin_lock_irqsave(&port->lock, flags); | |
207 | ||
208 | while (!uart_circ_empty(xmit)) { | |
209 | long status = hypervisor_con_putchar(xmit->buf[xmit->tail]); | |
210 | ||
211 | if (status != HV_EOK) | |
212 | break; | |
213 | ||
214 | xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1); | |
215 | port->icount.tx++; | |
216 | } | |
217 | ||
218 | spin_unlock_irqrestore(&port->lock, flags); | |
219 | } | |
220 | ||
221 | /* port->lock is not held. */ | |
222 | static void sunhv_send_xchar(struct uart_port *port, char ch) | |
223 | { | |
224 | unsigned long flags; | |
225 | int limit = 10000; | |
226 | ||
227 | spin_lock_irqsave(&port->lock, flags); | |
228 | ||
229 | while (limit-- > 0) { | |
230 | long status = hypervisor_con_putchar(ch); | |
231 | if (status == HV_EOK) | |
232 | break; | |
233 | } | |
234 | ||
235 | spin_unlock_irqrestore(&port->lock, flags); | |
236 | } | |
237 | ||
238 | /* port->lock held by caller. */ | |
239 | static void sunhv_stop_rx(struct uart_port *port) | |
240 | { | |
241 | } | |
242 | ||
243 | /* port->lock held by caller. */ | |
244 | static void sunhv_enable_ms(struct uart_port *port) | |
245 | { | |
246 | } | |
247 | ||
248 | /* port->lock is not held. */ | |
249 | static void sunhv_break_ctl(struct uart_port *port, int break_state) | |
250 | { | |
251 | if (break_state) { | |
252 | unsigned long flags; | |
253 | int limit = 10000; | |
254 | ||
255 | spin_lock_irqsave(&port->lock, flags); | |
256 | ||
257 | while (limit-- > 0) { | |
258 | long status = hypervisor_con_putchar(CON_BREAK); | |
259 | if (status == HV_EOK) | |
260 | break; | |
261 | } | |
262 | ||
263 | spin_unlock_irqrestore(&port->lock, flags); | |
264 | } | |
265 | } | |
266 | ||
267 | /* port->lock is not held. */ | |
268 | static int sunhv_startup(struct uart_port *port) | |
269 | { | |
270 | return 0; | |
271 | } | |
272 | ||
273 | /* port->lock is not held. */ | |
274 | static void sunhv_shutdown(struct uart_port *port) | |
275 | { | |
276 | } | |
277 | ||
278 | /* port->lock is not held. */ | |
279 | static void sunhv_set_termios(struct uart_port *port, struct termios *termios, | |
280 | struct termios *old) | |
281 | { | |
282 | unsigned int baud = uart_get_baud_rate(port, termios, old, 0, 4000000); | |
283 | unsigned int quot = uart_get_divisor(port, baud); | |
284 | unsigned int iflag, cflag; | |
285 | unsigned long flags; | |
286 | ||
287 | spin_lock_irqsave(&port->lock, flags); | |
288 | ||
289 | iflag = termios->c_iflag; | |
290 | cflag = termios->c_cflag; | |
291 | ||
292 | port->ignore_status_mask = 0; | |
293 | if (iflag & IGNBRK) | |
294 | port->ignore_status_mask |= IGNORE_BREAK; | |
295 | if ((cflag & CREAD) == 0) | |
296 | port->ignore_status_mask |= IGNORE_ALL; | |
297 | ||
298 | /* XXX */ | |
299 | uart_update_timeout(port, cflag, | |
300 | (port->uartclk / (16 * quot))); | |
301 | ||
302 | spin_unlock_irqrestore(&port->lock, flags); | |
303 | } | |
304 | ||
305 | static const char *sunhv_type(struct uart_port *port) | |
306 | { | |
307 | return "SUN4V HCONS"; | |
308 | } | |
309 | ||
310 | static void sunhv_release_port(struct uart_port *port) | |
311 | { | |
312 | } | |
313 | ||
314 | static int sunhv_request_port(struct uart_port *port) | |
315 | { | |
316 | return 0; | |
317 | } | |
318 | ||
319 | static void sunhv_config_port(struct uart_port *port, int flags) | |
320 | { | |
321 | } | |
322 | ||
323 | static int sunhv_verify_port(struct uart_port *port, struct serial_struct *ser) | |
324 | { | |
325 | return -EINVAL; | |
326 | } | |
327 | ||
328 | static struct uart_ops sunhv_pops = { | |
329 | .tx_empty = sunhv_tx_empty, | |
330 | .set_mctrl = sunhv_set_mctrl, | |
331 | .get_mctrl = sunhv_get_mctrl, | |
332 | .stop_tx = sunhv_stop_tx, | |
333 | .start_tx = sunhv_start_tx, | |
334 | .send_xchar = sunhv_send_xchar, | |
335 | .stop_rx = sunhv_stop_rx, | |
336 | .enable_ms = sunhv_enable_ms, | |
337 | .break_ctl = sunhv_break_ctl, | |
338 | .startup = sunhv_startup, | |
339 | .shutdown = sunhv_shutdown, | |
340 | .set_termios = sunhv_set_termios, | |
341 | .type = sunhv_type, | |
342 | .release_port = sunhv_release_port, | |
343 | .request_port = sunhv_request_port, | |
344 | .config_port = sunhv_config_port, | |
345 | .verify_port = sunhv_verify_port, | |
346 | }; | |
347 | ||
348 | static struct uart_driver sunhv_reg = { | |
349 | .owner = THIS_MODULE, | |
350 | .driver_name = "serial", | |
351 | .devfs_name = "tts/", | |
352 | .dev_name = "ttyS", | |
353 | .major = TTY_MAJOR, | |
354 | }; | |
355 | ||
356 | static struct uart_port *sunhv_port; | |
357 | ||
358 | static inline void sunhv_console_putchar(struct uart_port *port, char c) | |
359 | { | |
360 | unsigned long flags; | |
361 | int limit = 10000; | |
362 | ||
363 | spin_lock_irqsave(&port->lock, flags); | |
364 | ||
365 | while (limit-- > 0) { | |
366 | long status = hypervisor_con_putchar(c); | |
367 | if (status == HV_EOK) | |
368 | break; | |
369 | } | |
370 | ||
371 | spin_unlock_irqrestore(&port->lock, flags); | |
372 | } | |
373 | ||
374 | static void sunhv_console_write(struct console *con, const char *s, unsigned n) | |
375 | { | |
376 | struct uart_port *port = sunhv_port; | |
377 | int i; | |
378 | ||
379 | for (i = 0; i < n; i++) { | |
380 | if (*s == '\n') | |
381 | sunhv_console_putchar(port, '\r'); | |
382 | sunhv_console_putchar(port, *s++); | |
383 | } | |
384 | } | |
385 | ||
386 | static int sunhv_console_setup(struct console *con, char *options) | |
387 | { | |
388 | return 0; | |
389 | } | |
390 | ||
391 | static struct console sunhv_console = { | |
392 | .name = "ttyS", | |
393 | .write = sunhv_console_write, | |
394 | .device = uart_console_device, | |
395 | .setup = sunhv_console_setup, | |
396 | .flags = CON_PRINTBUFFER, | |
397 | .index = -1, | |
398 | .data = &sunhv_reg, | |
399 | }; | |
400 | ||
401 | static void __init sunhv_console_init(void) | |
402 | { | |
403 | if (con_is_present()) | |
404 | return; | |
405 | ||
406 | sunhv_console.index = 0; | |
407 | register_console(&sunhv_console); | |
408 | } | |
409 | ||
410 | static int __init sunhv_init(void) | |
411 | { | |
412 | struct uart_port *port; | |
413 | int ret; | |
414 | ||
415 | if (tlb_type != hypervisor) | |
416 | return -ENODEV; | |
417 | ||
418 | port = kmalloc(sizeof(struct uart_port), GFP_KERNEL); | |
419 | if (unlikely(!port)) | |
420 | return -ENOMEM; | |
421 | ||
422 | port->line = 0; | |
423 | port->ops = &sunhv_pops; | |
424 | port->type = PORT_SUNHV; | |
425 | port->uartclk = ( 29491200 / 16 ); /* arbitrary */ | |
426 | ||
427 | /* XXX Get interrupt. XXX */ | |
428 | if (request_irq(0 /* XXX */, sunhv_interrupt, | |
429 | SA_SHIRQ, "serial(sunhv)", port)) { | |
430 | printk("sunhv: Cannot get IRQ %x\n", | |
431 | 0 /* XXX */); | |
432 | kfree(port); | |
433 | return -ENODEV; | |
434 | } | |
435 | ||
436 | sunhv_reg.minor = sunserial_current_minor; | |
437 | sunhv_reg.nr = 1; | |
438 | sunhv_reg.cons = &sunhv_console; | |
439 | ||
440 | ret = uart_register_driver(&sunhv_reg); | |
441 | if (ret < 0) { | |
442 | free_irq(0 /* XXX */, up); | |
443 | kfree(port); | |
444 | ||
445 | return ret; | |
446 | } | |
447 | ||
448 | sunhv_port = port; | |
449 | ||
450 | sunserial_current_minor += 1; | |
451 | ||
452 | sunhv_console_init(); | |
453 | ||
454 | uart_add_one_port(&sunhv_reg, port); | |
455 | ||
456 | return 0; | |
457 | } | |
458 | ||
459 | static void __exit sunhv_exit(void) | |
460 | { | |
461 | struct uart_port *port = sunhv_port; | |
462 | ||
463 | BUG_ON(!port); | |
464 | ||
465 | uart_remove_one_port(&sunhv_reg, port); | |
466 | free_irq(0 /* XXX */, port); | |
467 | ||
468 | sunserial_current_minor -= 1; | |
469 | ||
470 | uart_unregister_driver(&sunhv_reg); | |
471 | ||
472 | kfree(sunhv_port); | |
473 | sunhv_port = NULL; | |
474 | } | |
475 | ||
476 | module_init(sunhv_init); | |
477 | module_exit(sunhv_exit); | |
478 | ||
479 | MODULE_AUTHOR("David S. Miller"); | |
480 | MODULE_DESCRIPTION("SUN4V Hypervisor console driver") | |
481 | MODULE_LICENSE("GPL"); |