sunhv.c 11 KB

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