sunhv.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  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/vdev.h>
  22. #include <asm/oplib.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, struct pt_regs *regs)
  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, regs);
  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, regs))
  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, struct pt_regs *regs)
  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, regs);
  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 termios *termios,
  225. struct termios *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. .devfs_name = "tts/",
  285. .dev_name = "ttyS",
  286. .major = TTY_MAJOR,
  287. };
  288. static struct uart_port *sunhv_port;
  289. static inline void sunhv_console_putchar(struct uart_port *port, char c)
  290. {
  291. unsigned long flags;
  292. int limit = 1000000;
  293. spin_lock_irqsave(&port->lock, flags);
  294. while (limit-- > 0) {
  295. long status = hypervisor_con_putchar(c);
  296. if (status == HV_EOK)
  297. break;
  298. udelay(2);
  299. }
  300. spin_unlock_irqrestore(&port->lock, flags);
  301. }
  302. static void sunhv_console_write(struct console *con, const char *s, unsigned n)
  303. {
  304. struct uart_port *port = sunhv_port;
  305. int i;
  306. for (i = 0; i < n; i++) {
  307. if (*s == '\n')
  308. sunhv_console_putchar(port, '\r');
  309. sunhv_console_putchar(port, *s++);
  310. }
  311. }
  312. static struct console sunhv_console = {
  313. .name = "ttyHV",
  314. .write = sunhv_console_write,
  315. .device = uart_console_device,
  316. .flags = CON_PRINTBUFFER,
  317. .index = -1,
  318. .data = &sunhv_reg,
  319. };
  320. static inline struct console *SUNHV_CONSOLE(void)
  321. {
  322. if (con_is_present())
  323. return NULL;
  324. sunhv_console.index = 0;
  325. return &sunhv_console;
  326. }
  327. static int __init hv_console_compatible(char *buf, int len)
  328. {
  329. while (len) {
  330. int this_len;
  331. if (!strcmp(buf, "qcn"))
  332. return 1;
  333. this_len = strlen(buf) + 1;
  334. buf += this_len;
  335. len -= this_len;
  336. }
  337. return 0;
  338. }
  339. static unsigned int __init get_interrupt(void)
  340. {
  341. const char *cons_str = "console";
  342. const char *compat_str = "compatible";
  343. int node = prom_getchild(sun4v_vdev_root);
  344. char buf[64];
  345. int err, len;
  346. node = prom_searchsiblings(node, cons_str);
  347. if (!node)
  348. return 0;
  349. len = prom_getproplen(node, compat_str);
  350. if (len == 0 || len == -1)
  351. return 0;
  352. err = prom_getproperty(node, compat_str, buf, 64);
  353. if (err == -1)
  354. return 0;
  355. if (!hv_console_compatible(buf, len))
  356. return 0;
  357. /* Ok, the this is the OBP node for the sun4v hypervisor
  358. * console device. Decode the interrupt.
  359. */
  360. return sun4v_vdev_device_interrupt(node);
  361. }
  362. static int __init sunhv_init(void)
  363. {
  364. struct uart_port *port;
  365. int ret;
  366. if (tlb_type != hypervisor)
  367. return -ENODEV;
  368. port = kmalloc(sizeof(struct uart_port), GFP_KERNEL);
  369. if (unlikely(!port))
  370. return -ENOMEM;
  371. memset(port, 0, sizeof(struct uart_port));
  372. port->line = 0;
  373. port->ops = &sunhv_pops;
  374. port->type = PORT_SUNHV;
  375. port->uartclk = ( 29491200 / 16 ); /* arbitrary */
  376. /* Set this just to make uart_configure_port() happy. */
  377. port->membase = (unsigned char __iomem *) __pa(port);
  378. port->irq = get_interrupt();
  379. if (!port->irq) {
  380. kfree(port);
  381. return -ENODEV;
  382. }
  383. sunhv_reg.minor = sunserial_current_minor;
  384. sunhv_reg.nr = 1;
  385. ret = uart_register_driver(&sunhv_reg);
  386. if (ret < 0) {
  387. printk(KERN_ERR "SUNHV: uart_register_driver() failed %d\n",
  388. ret);
  389. kfree(port);
  390. return ret;
  391. }
  392. sunhv_reg.tty_driver->name_base = sunhv_reg.minor - 64;
  393. sunserial_current_minor += 1;
  394. sunhv_reg.cons = SUNHV_CONSOLE();
  395. sunhv_port = port;
  396. ret = uart_add_one_port(&sunhv_reg, port);
  397. if (ret < 0) {
  398. printk(KERN_ERR "SUNHV: uart_add_one_port() failed %d\n", ret);
  399. sunserial_current_minor -= 1;
  400. uart_unregister_driver(&sunhv_reg);
  401. kfree(port);
  402. sunhv_port = NULL;
  403. return -ENODEV;
  404. }
  405. if (request_irq(port->irq, sunhv_interrupt,
  406. SA_SHIRQ, "serial(sunhv)", port)) {
  407. printk(KERN_ERR "sunhv: Cannot register IRQ\n");
  408. uart_remove_one_port(&sunhv_reg, port);
  409. sunserial_current_minor -= 1;
  410. uart_unregister_driver(&sunhv_reg);
  411. kfree(port);
  412. sunhv_port = NULL;
  413. return -ENODEV;
  414. }
  415. return 0;
  416. }
  417. static void __exit sunhv_exit(void)
  418. {
  419. struct uart_port *port = sunhv_port;
  420. BUG_ON(!port);
  421. free_irq(port->irq, port);
  422. uart_remove_one_port(&sunhv_reg, port);
  423. sunserial_current_minor -= 1;
  424. uart_unregister_driver(&sunhv_reg);
  425. kfree(sunhv_port);
  426. sunhv_port = NULL;
  427. }
  428. module_init(sunhv_init);
  429. module_exit(sunhv_exit);
  430. MODULE_AUTHOR("David S. Miller");
  431. MODULE_DESCRIPTION("SUN4V Hypervisor console driver")
  432. MODULE_LICENSE("GPL");