sunhv.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  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. saw_console_brk = 1;
  75. c = 0;
  76. }
  77. if (c == CON_HUP) {
  78. hung_up = 1;
  79. uart_handle_dcd_change(port, 0);
  80. } else if (hung_up) {
  81. hung_up = 0;
  82. uart_handle_dcd_change(port, 1);
  83. }
  84. if (tty == NULL) {
  85. uart_handle_sysrq_char(port, c, regs);
  86. continue;
  87. }
  88. flag = TTY_NORMAL;
  89. port->icount.rx++;
  90. if (c == CON_BREAK) {
  91. port->icount.brk++;
  92. if (uart_handle_break(port))
  93. continue;
  94. flag = TTY_BREAK;
  95. }
  96. if (uart_handle_sysrq_char(port, c, regs))
  97. continue;
  98. if ((port->ignore_status_mask & IGNORE_ALL) ||
  99. ((port->ignore_status_mask & IGNORE_BREAK) &&
  100. (c == CON_BREAK)))
  101. continue;
  102. tty_insert_flip_char(tty, c, flag);
  103. }
  104. if (saw_console_brk)
  105. sun_do_break();
  106. return tty;
  107. }
  108. static void transmit_chars(struct uart_port *port)
  109. {
  110. struct circ_buf *xmit;
  111. if (!port->info)
  112. return;
  113. xmit = &port->info->xmit;
  114. if (uart_circ_empty(xmit) || uart_tx_stopped(port))
  115. return;
  116. while (!uart_circ_empty(xmit)) {
  117. long status = hypervisor_con_putchar(xmit->buf[xmit->tail]);
  118. if (status != HV_EOK)
  119. break;
  120. xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
  121. port->icount.tx++;
  122. }
  123. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  124. uart_write_wakeup(port);
  125. }
  126. static irqreturn_t sunhv_interrupt(int irq, void *dev_id, struct pt_regs *regs)
  127. {
  128. struct uart_port *port = dev_id;
  129. struct tty_struct *tty;
  130. unsigned long flags;
  131. spin_lock_irqsave(&port->lock, flags);
  132. tty = receive_chars(port, regs);
  133. transmit_chars(port);
  134. spin_unlock_irqrestore(&port->lock, flags);
  135. if (tty)
  136. tty_flip_buffer_push(tty);
  137. return IRQ_HANDLED;
  138. }
  139. /* port->lock is not held. */
  140. static unsigned int sunhv_tx_empty(struct uart_port *port)
  141. {
  142. /* Transmitter is always empty for us. If the circ buffer
  143. * is non-empty or there is an x_char pending, our caller
  144. * will do the right thing and ignore what we return here.
  145. */
  146. return TIOCSER_TEMT;
  147. }
  148. /* port->lock held by caller. */
  149. static void sunhv_set_mctrl(struct uart_port *port, unsigned int mctrl)
  150. {
  151. return;
  152. }
  153. /* port->lock is held by caller and interrupts are disabled. */
  154. static unsigned int sunhv_get_mctrl(struct uart_port *port)
  155. {
  156. return TIOCM_DSR | TIOCM_CAR | TIOCM_CTS;
  157. }
  158. /* port->lock held by caller. */
  159. static void sunhv_stop_tx(struct uart_port *port)
  160. {
  161. return;
  162. }
  163. /* port->lock held by caller. */
  164. static void sunhv_start_tx(struct uart_port *port)
  165. {
  166. struct circ_buf *xmit = &port->info->xmit;
  167. unsigned long flags;
  168. spin_lock_irqsave(&port->lock, flags);
  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. spin_unlock_irqrestore(&port->lock, flags);
  177. }
  178. /* port->lock is not held. */
  179. static void sunhv_send_xchar(struct uart_port *port, char ch)
  180. {
  181. unsigned long flags;
  182. int limit = 10000;
  183. spin_lock_irqsave(&port->lock, flags);
  184. while (limit-- > 0) {
  185. long status = hypervisor_con_putchar(ch);
  186. if (status == HV_EOK)
  187. break;
  188. }
  189. spin_unlock_irqrestore(&port->lock, flags);
  190. }
  191. /* port->lock held by caller. */
  192. static void sunhv_stop_rx(struct uart_port *port)
  193. {
  194. }
  195. /* port->lock held by caller. */
  196. static void sunhv_enable_ms(struct uart_port *port)
  197. {
  198. }
  199. /* port->lock is not held. */
  200. static void sunhv_break_ctl(struct uart_port *port, int break_state)
  201. {
  202. if (break_state) {
  203. unsigned long flags;
  204. int limit = 10000;
  205. spin_lock_irqsave(&port->lock, flags);
  206. while (limit-- > 0) {
  207. long status = hypervisor_con_putchar(CON_BREAK);
  208. if (status == HV_EOK)
  209. break;
  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. sunserial_current_minor += 1;
  393. sunhv_reg.cons = SUNHV_CONSOLE();
  394. sunhv_port = port;
  395. ret = uart_add_one_port(&sunhv_reg, port);
  396. if (ret < 0) {
  397. printk(KERN_ERR "SUNHV: uart_add_one_port() failed %d\n", ret);
  398. sunserial_current_minor -= 1;
  399. uart_unregister_driver(&sunhv_reg);
  400. kfree(port);
  401. sunhv_port = NULL;
  402. return -ENODEV;
  403. }
  404. if (request_irq(port->irq, sunhv_interrupt,
  405. SA_SHIRQ, "serial(sunhv)", port)) {
  406. printk(KERN_ERR "sunhv: Cannot register IRQ\n");
  407. uart_remove_one_port(&sunhv_reg, port);
  408. sunserial_current_minor -= 1;
  409. uart_unregister_driver(&sunhv_reg);
  410. kfree(port);
  411. sunhv_port = NULL;
  412. return -ENODEV;
  413. }
  414. return 0;
  415. }
  416. static void __exit sunhv_exit(void)
  417. {
  418. struct uart_port *port = sunhv_port;
  419. BUG_ON(!port);
  420. free_irq(port->irq, port);
  421. uart_remove_one_port(&sunhv_reg, port);
  422. sunserial_current_minor -= 1;
  423. uart_unregister_driver(&sunhv_reg);
  424. kfree(sunhv_port);
  425. sunhv_port = NULL;
  426. }
  427. module_init(sunhv_init);
  428. module_exit(sunhv_exit);
  429. MODULE_AUTHOR("David S. Miller");
  430. MODULE_DESCRIPTION("SUN4V Hypervisor console driver")
  431. MODULE_LICENSE("GPL");