1ab4382d2SGreg Kroah-Hartman /* sunhv.c: Serial driver for SUN4V hypervisor console. 2ab4382d2SGreg Kroah-Hartman * 3ab4382d2SGreg Kroah-Hartman * Copyright (C) 2006, 2007 David S. Miller (davem@davemloft.net) 4ab4382d2SGreg Kroah-Hartman */ 5ab4382d2SGreg Kroah-Hartman 6ab4382d2SGreg Kroah-Hartman #include <linux/kernel.h> 7ab4382d2SGreg Kroah-Hartman #include <linux/errno.h> 8ab4382d2SGreg Kroah-Hartman #include <linux/tty.h> 9ab4382d2SGreg Kroah-Hartman #include <linux/tty_flip.h> 10ab4382d2SGreg Kroah-Hartman #include <linux/major.h> 11ab4382d2SGreg Kroah-Hartman #include <linux/circ_buf.h> 12ab4382d2SGreg Kroah-Hartman #include <linux/serial.h> 13ab4382d2SGreg Kroah-Hartman #include <linux/sysrq.h> 14ab4382d2SGreg Kroah-Hartman #include <linux/console.h> 15ab4382d2SGreg Kroah-Hartman #include <linux/spinlock.h> 16ab4382d2SGreg Kroah-Hartman #include <linux/slab.h> 17ab4382d2SGreg Kroah-Hartman #include <linux/delay.h> 18ab4382d2SGreg Kroah-Hartman #include <linux/init.h> 19ab4382d2SGreg Kroah-Hartman #include <linux/of_device.h> 20ab4382d2SGreg Kroah-Hartman 21ab4382d2SGreg Kroah-Hartman #include <asm/hypervisor.h> 22ab4382d2SGreg Kroah-Hartman #include <asm/spitfire.h> 23ab4382d2SGreg Kroah-Hartman #include <asm/prom.h> 24ab4382d2SGreg Kroah-Hartman #include <asm/irq.h> 25d550bbd4SDavid Howells #include <asm/setup.h> 26ab4382d2SGreg Kroah-Hartman 27ab4382d2SGreg Kroah-Hartman #if defined(CONFIG_MAGIC_SYSRQ) 28ab4382d2SGreg Kroah-Hartman #define SUPPORT_SYSRQ 29ab4382d2SGreg Kroah-Hartman #endif 30ab4382d2SGreg Kroah-Hartman 31ab4382d2SGreg Kroah-Hartman #include <linux/serial_core.h> 326816383aSPaul Gortmaker #include <linux/sunserialcore.h> 33ab4382d2SGreg Kroah-Hartman 34ab4382d2SGreg Kroah-Hartman #define CON_BREAK ((long)-1) 35ab4382d2SGreg Kroah-Hartman #define CON_HUP ((long)-2) 36ab4382d2SGreg Kroah-Hartman 37ab4382d2SGreg Kroah-Hartman #define IGNORE_BREAK 0x1 38ab4382d2SGreg Kroah-Hartman #define IGNORE_ALL 0x2 39ab4382d2SGreg Kroah-Hartman 40ab4382d2SGreg Kroah-Hartman static char *con_write_page; 41ab4382d2SGreg Kroah-Hartman static char *con_read_page; 42ab4382d2SGreg Kroah-Hartman 43ab4382d2SGreg Kroah-Hartman static int hung_up = 0; 44ab4382d2SGreg Kroah-Hartman 45ab4382d2SGreg Kroah-Hartman static void transmit_chars_putchar(struct uart_port *port, struct circ_buf *xmit) 46ab4382d2SGreg Kroah-Hartman { 47ab4382d2SGreg Kroah-Hartman while (!uart_circ_empty(xmit)) { 48ab4382d2SGreg Kroah-Hartman long status = sun4v_con_putchar(xmit->buf[xmit->tail]); 49ab4382d2SGreg Kroah-Hartman 50ab4382d2SGreg Kroah-Hartman if (status != HV_EOK) 51ab4382d2SGreg Kroah-Hartman break; 52ab4382d2SGreg Kroah-Hartman 53ab4382d2SGreg Kroah-Hartman xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1); 54ab4382d2SGreg Kroah-Hartman port->icount.tx++; 55ab4382d2SGreg Kroah-Hartman } 56ab4382d2SGreg Kroah-Hartman } 57ab4382d2SGreg Kroah-Hartman 58ab4382d2SGreg Kroah-Hartman static void transmit_chars_write(struct uart_port *port, struct circ_buf *xmit) 59ab4382d2SGreg Kroah-Hartman { 60ab4382d2SGreg Kroah-Hartman while (!uart_circ_empty(xmit)) { 61ab4382d2SGreg Kroah-Hartman unsigned long ra = __pa(xmit->buf + xmit->tail); 62ab4382d2SGreg Kroah-Hartman unsigned long len, status, sent; 63ab4382d2SGreg Kroah-Hartman 64ab4382d2SGreg Kroah-Hartman len = CIRC_CNT_TO_END(xmit->head, xmit->tail, 65ab4382d2SGreg Kroah-Hartman UART_XMIT_SIZE); 66ab4382d2SGreg Kroah-Hartman status = sun4v_con_write(ra, len, &sent); 67ab4382d2SGreg Kroah-Hartman if (status != HV_EOK) 68ab4382d2SGreg Kroah-Hartman break; 69ab4382d2SGreg Kroah-Hartman xmit->tail = (xmit->tail + sent) & (UART_XMIT_SIZE - 1); 70ab4382d2SGreg Kroah-Hartman port->icount.tx += sent; 71ab4382d2SGreg Kroah-Hartman } 72ab4382d2SGreg Kroah-Hartman } 73ab4382d2SGreg Kroah-Hartman 7405c7cd39SJiri Slaby static int receive_chars_getchar(struct uart_port *port) 75ab4382d2SGreg Kroah-Hartman { 76ab4382d2SGreg Kroah-Hartman int saw_console_brk = 0; 77ab4382d2SGreg Kroah-Hartman int limit = 10000; 78ab4382d2SGreg Kroah-Hartman 79ab4382d2SGreg Kroah-Hartman while (limit-- > 0) { 80ab4382d2SGreg Kroah-Hartman long status; 81ab4382d2SGreg Kroah-Hartman long c = sun4v_con_getchar(&status); 82ab4382d2SGreg Kroah-Hartman 83ab4382d2SGreg Kroah-Hartman if (status == HV_EWOULDBLOCK) 84ab4382d2SGreg Kroah-Hartman break; 85ab4382d2SGreg Kroah-Hartman 86ab4382d2SGreg Kroah-Hartman if (c == CON_BREAK) { 87ab4382d2SGreg Kroah-Hartman if (uart_handle_break(port)) 88ab4382d2SGreg Kroah-Hartman continue; 89ab4382d2SGreg Kroah-Hartman saw_console_brk = 1; 90ab4382d2SGreg Kroah-Hartman c = 0; 91ab4382d2SGreg Kroah-Hartman } 92ab4382d2SGreg Kroah-Hartman 93ab4382d2SGreg Kroah-Hartman if (c == CON_HUP) { 94ab4382d2SGreg Kroah-Hartman hung_up = 1; 95ab4382d2SGreg Kroah-Hartman uart_handle_dcd_change(port, 0); 96ab4382d2SGreg Kroah-Hartman } else if (hung_up) { 97ab4382d2SGreg Kroah-Hartman hung_up = 0; 98ab4382d2SGreg Kroah-Hartman uart_handle_dcd_change(port, 1); 99ab4382d2SGreg Kroah-Hartman } 100ab4382d2SGreg Kroah-Hartman 10192a19f9cSJiri Slaby if (port->state == NULL) { 102ab4382d2SGreg Kroah-Hartman uart_handle_sysrq_char(port, c); 103ab4382d2SGreg Kroah-Hartman continue; 104ab4382d2SGreg Kroah-Hartman } 105ab4382d2SGreg Kroah-Hartman 106ab4382d2SGreg Kroah-Hartman port->icount.rx++; 107ab4382d2SGreg Kroah-Hartman 108ab4382d2SGreg Kroah-Hartman if (uart_handle_sysrq_char(port, c)) 109ab4382d2SGreg Kroah-Hartman continue; 110ab4382d2SGreg Kroah-Hartman 11192a19f9cSJiri Slaby tty_insert_flip_char(&port->state->port, c, TTY_NORMAL); 112ab4382d2SGreg Kroah-Hartman } 113ab4382d2SGreg Kroah-Hartman 114ab4382d2SGreg Kroah-Hartman return saw_console_brk; 115ab4382d2SGreg Kroah-Hartman } 116ab4382d2SGreg Kroah-Hartman 11705c7cd39SJiri Slaby static int receive_chars_read(struct uart_port *port) 118ab4382d2SGreg Kroah-Hartman { 119ab4382d2SGreg Kroah-Hartman int saw_console_brk = 0; 120ab4382d2SGreg Kroah-Hartman int limit = 10000; 121ab4382d2SGreg Kroah-Hartman 122ab4382d2SGreg Kroah-Hartman while (limit-- > 0) { 123ab4382d2SGreg Kroah-Hartman unsigned long ra = __pa(con_read_page); 124ab4382d2SGreg Kroah-Hartman unsigned long bytes_read, i; 125ab4382d2SGreg Kroah-Hartman long stat = sun4v_con_read(ra, PAGE_SIZE, &bytes_read); 126ab4382d2SGreg Kroah-Hartman 127ab4382d2SGreg Kroah-Hartman if (stat != HV_EOK) { 128ab4382d2SGreg Kroah-Hartman bytes_read = 0; 129ab4382d2SGreg Kroah-Hartman 130ab4382d2SGreg Kroah-Hartman if (stat == CON_BREAK) { 131ab4382d2SGreg Kroah-Hartman if (uart_handle_break(port)) 132ab4382d2SGreg Kroah-Hartman continue; 133ab4382d2SGreg Kroah-Hartman saw_console_brk = 1; 134ab4382d2SGreg Kroah-Hartman *con_read_page = 0; 135ab4382d2SGreg Kroah-Hartman bytes_read = 1; 136ab4382d2SGreg Kroah-Hartman } else if (stat == CON_HUP) { 137ab4382d2SGreg Kroah-Hartman hung_up = 1; 138ab4382d2SGreg Kroah-Hartman uart_handle_dcd_change(port, 0); 139ab4382d2SGreg Kroah-Hartman continue; 140ab4382d2SGreg Kroah-Hartman } else { 141ab4382d2SGreg Kroah-Hartman /* HV_EWOULDBLOCK, etc. */ 142ab4382d2SGreg Kroah-Hartman break; 143ab4382d2SGreg Kroah-Hartman } 144ab4382d2SGreg Kroah-Hartman } 145ab4382d2SGreg Kroah-Hartman 146ab4382d2SGreg Kroah-Hartman if (hung_up) { 147ab4382d2SGreg Kroah-Hartman hung_up = 0; 148ab4382d2SGreg Kroah-Hartman uart_handle_dcd_change(port, 1); 149ab4382d2SGreg Kroah-Hartman } 150ab4382d2SGreg Kroah-Hartman 151079317a6SVijay Kumar if (port->sysrq != 0 && *con_read_page) { 152ab4382d2SGreg Kroah-Hartman for (i = 0; i < bytes_read; i++) 153ab4382d2SGreg Kroah-Hartman uart_handle_sysrq_char(port, con_read_page[i]); 154079317a6SVijay Kumar } 155ab4382d2SGreg Kroah-Hartman 15605c7cd39SJiri Slaby if (port->state == NULL) 157ab4382d2SGreg Kroah-Hartman continue; 158ab4382d2SGreg Kroah-Hartman 159ab4382d2SGreg Kroah-Hartman port->icount.rx += bytes_read; 160ab4382d2SGreg Kroah-Hartman 16105c7cd39SJiri Slaby tty_insert_flip_string(&port->state->port, con_read_page, 16205c7cd39SJiri Slaby bytes_read); 163ab4382d2SGreg Kroah-Hartman } 164ab4382d2SGreg Kroah-Hartman 165ab4382d2SGreg Kroah-Hartman return saw_console_brk; 166ab4382d2SGreg Kroah-Hartman } 167ab4382d2SGreg Kroah-Hartman 168ab4382d2SGreg Kroah-Hartman struct sunhv_ops { 169ab4382d2SGreg Kroah-Hartman void (*transmit_chars)(struct uart_port *port, struct circ_buf *xmit); 17005c7cd39SJiri Slaby int (*receive_chars)(struct uart_port *port); 171ab4382d2SGreg Kroah-Hartman }; 172ab4382d2SGreg Kroah-Hartman 17301fd3c27SAya Mahfouz static const struct sunhv_ops bychar_ops = { 174ab4382d2SGreg Kroah-Hartman .transmit_chars = transmit_chars_putchar, 175ab4382d2SGreg Kroah-Hartman .receive_chars = receive_chars_getchar, 176ab4382d2SGreg Kroah-Hartman }; 177ab4382d2SGreg Kroah-Hartman 17801fd3c27SAya Mahfouz static const struct sunhv_ops bywrite_ops = { 179ab4382d2SGreg Kroah-Hartman .transmit_chars = transmit_chars_write, 180ab4382d2SGreg Kroah-Hartman .receive_chars = receive_chars_read, 181ab4382d2SGreg Kroah-Hartman }; 182ab4382d2SGreg Kroah-Hartman 18301fd3c27SAya Mahfouz static const struct sunhv_ops *sunhv_ops = &bychar_ops; 184ab4382d2SGreg Kroah-Hartman 1852e124b4aSJiri Slaby static struct tty_port *receive_chars(struct uart_port *port) 186ab4382d2SGreg Kroah-Hartman { 1872e124b4aSJiri Slaby struct tty_port *tport = NULL; 188ab4382d2SGreg Kroah-Hartman 189ab4382d2SGreg Kroah-Hartman if (port->state != NULL) /* Unopened serial console */ 1902e124b4aSJiri Slaby tport = &port->state->port; 191ab4382d2SGreg Kroah-Hartman 19205c7cd39SJiri Slaby if (sunhv_ops->receive_chars(port)) 193ab4382d2SGreg Kroah-Hartman sun_do_break(); 194ab4382d2SGreg Kroah-Hartman 1952e124b4aSJiri Slaby return tport; 196ab4382d2SGreg Kroah-Hartman } 197ab4382d2SGreg Kroah-Hartman 198ab4382d2SGreg Kroah-Hartman static void transmit_chars(struct uart_port *port) 199ab4382d2SGreg Kroah-Hartman { 200ab4382d2SGreg Kroah-Hartman struct circ_buf *xmit; 201ab4382d2SGreg Kroah-Hartman 202ab4382d2SGreg Kroah-Hartman if (!port->state) 203ab4382d2SGreg Kroah-Hartman return; 204ab4382d2SGreg Kroah-Hartman 205ab4382d2SGreg Kroah-Hartman xmit = &port->state->xmit; 206ab4382d2SGreg Kroah-Hartman if (uart_circ_empty(xmit) || uart_tx_stopped(port)) 207ab4382d2SGreg Kroah-Hartman return; 208ab4382d2SGreg Kroah-Hartman 209ab4382d2SGreg Kroah-Hartman sunhv_ops->transmit_chars(port, xmit); 210ab4382d2SGreg Kroah-Hartman 211ab4382d2SGreg Kroah-Hartman if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS) 212ab4382d2SGreg Kroah-Hartman uart_write_wakeup(port); 213ab4382d2SGreg Kroah-Hartman } 214ab4382d2SGreg Kroah-Hartman 215ab4382d2SGreg Kroah-Hartman static irqreturn_t sunhv_interrupt(int irq, void *dev_id) 216ab4382d2SGreg Kroah-Hartman { 217ab4382d2SGreg Kroah-Hartman struct uart_port *port = dev_id; 2182e124b4aSJiri Slaby struct tty_port *tport; 219ab4382d2SGreg Kroah-Hartman unsigned long flags; 220ab4382d2SGreg Kroah-Hartman 221ab4382d2SGreg Kroah-Hartman spin_lock_irqsave(&port->lock, flags); 2222e124b4aSJiri Slaby tport = receive_chars(port); 223ab4382d2SGreg Kroah-Hartman transmit_chars(port); 224ab4382d2SGreg Kroah-Hartman spin_unlock_irqrestore(&port->lock, flags); 225ab4382d2SGreg Kroah-Hartman 2262e124b4aSJiri Slaby if (tport) 2272e124b4aSJiri Slaby tty_flip_buffer_push(tport); 228ab4382d2SGreg Kroah-Hartman 229ab4382d2SGreg Kroah-Hartman return IRQ_HANDLED; 230ab4382d2SGreg Kroah-Hartman } 231ab4382d2SGreg Kroah-Hartman 232ab4382d2SGreg Kroah-Hartman /* port->lock is not held. */ 233ab4382d2SGreg Kroah-Hartman static unsigned int sunhv_tx_empty(struct uart_port *port) 234ab4382d2SGreg Kroah-Hartman { 235ab4382d2SGreg Kroah-Hartman /* Transmitter is always empty for us. If the circ buffer 236ab4382d2SGreg Kroah-Hartman * is non-empty or there is an x_char pending, our caller 237ab4382d2SGreg Kroah-Hartman * will do the right thing and ignore what we return here. 238ab4382d2SGreg Kroah-Hartman */ 239ab4382d2SGreg Kroah-Hartman return TIOCSER_TEMT; 240ab4382d2SGreg Kroah-Hartman } 241ab4382d2SGreg Kroah-Hartman 242ab4382d2SGreg Kroah-Hartman /* port->lock held by caller. */ 243ab4382d2SGreg Kroah-Hartman static void sunhv_set_mctrl(struct uart_port *port, unsigned int mctrl) 244ab4382d2SGreg Kroah-Hartman { 245ab4382d2SGreg Kroah-Hartman return; 246ab4382d2SGreg Kroah-Hartman } 247ab4382d2SGreg Kroah-Hartman 248ab4382d2SGreg Kroah-Hartman /* port->lock is held by caller and interrupts are disabled. */ 249ab4382d2SGreg Kroah-Hartman static unsigned int sunhv_get_mctrl(struct uart_port *port) 250ab4382d2SGreg Kroah-Hartman { 251ab4382d2SGreg Kroah-Hartman return TIOCM_DSR | TIOCM_CAR | TIOCM_CTS; 252ab4382d2SGreg Kroah-Hartman } 253ab4382d2SGreg Kroah-Hartman 254ab4382d2SGreg Kroah-Hartman /* port->lock held by caller. */ 255ab4382d2SGreg Kroah-Hartman static void sunhv_stop_tx(struct uart_port *port) 256ab4382d2SGreg Kroah-Hartman { 257ab4382d2SGreg Kroah-Hartman return; 258ab4382d2SGreg Kroah-Hartman } 259ab4382d2SGreg Kroah-Hartman 260ab4382d2SGreg Kroah-Hartman /* port->lock held by caller. */ 261ab4382d2SGreg Kroah-Hartman static void sunhv_start_tx(struct uart_port *port) 262ab4382d2SGreg Kroah-Hartman { 263ab4382d2SGreg Kroah-Hartman transmit_chars(port); 264ab4382d2SGreg Kroah-Hartman } 265ab4382d2SGreg Kroah-Hartman 266ab4382d2SGreg Kroah-Hartman /* port->lock is not held. */ 267ab4382d2SGreg Kroah-Hartman static void sunhv_send_xchar(struct uart_port *port, char ch) 268ab4382d2SGreg Kroah-Hartman { 269ab4382d2SGreg Kroah-Hartman unsigned long flags; 270ab4382d2SGreg Kroah-Hartman int limit = 10000; 271ab4382d2SGreg Kroah-Hartman 272db106df3SPeter Hurley if (ch == __DISABLED_CHAR) 273db106df3SPeter Hurley return; 274db106df3SPeter Hurley 275ab4382d2SGreg Kroah-Hartman spin_lock_irqsave(&port->lock, flags); 276ab4382d2SGreg Kroah-Hartman 277ab4382d2SGreg Kroah-Hartman while (limit-- > 0) { 278ab4382d2SGreg Kroah-Hartman long status = sun4v_con_putchar(ch); 279ab4382d2SGreg Kroah-Hartman if (status == HV_EOK) 280ab4382d2SGreg Kroah-Hartman break; 281ab4382d2SGreg Kroah-Hartman udelay(1); 282ab4382d2SGreg Kroah-Hartman } 283ab4382d2SGreg Kroah-Hartman 284ab4382d2SGreg Kroah-Hartman spin_unlock_irqrestore(&port->lock, flags); 285ab4382d2SGreg Kroah-Hartman } 286ab4382d2SGreg Kroah-Hartman 287ab4382d2SGreg Kroah-Hartman /* port->lock held by caller. */ 288ab4382d2SGreg Kroah-Hartman static void sunhv_stop_rx(struct uart_port *port) 289ab4382d2SGreg Kroah-Hartman { 290ab4382d2SGreg Kroah-Hartman } 291ab4382d2SGreg Kroah-Hartman 292ab4382d2SGreg Kroah-Hartman /* port->lock is not held. */ 293ab4382d2SGreg Kroah-Hartman static void sunhv_break_ctl(struct uart_port *port, int break_state) 294ab4382d2SGreg Kroah-Hartman { 295ab4382d2SGreg Kroah-Hartman if (break_state) { 296ab4382d2SGreg Kroah-Hartman unsigned long flags; 297ab4382d2SGreg Kroah-Hartman int limit = 10000; 298ab4382d2SGreg Kroah-Hartman 299ab4382d2SGreg Kroah-Hartman spin_lock_irqsave(&port->lock, flags); 300ab4382d2SGreg Kroah-Hartman 301ab4382d2SGreg Kroah-Hartman while (limit-- > 0) { 302ab4382d2SGreg Kroah-Hartman long status = sun4v_con_putchar(CON_BREAK); 303ab4382d2SGreg Kroah-Hartman if (status == HV_EOK) 304ab4382d2SGreg Kroah-Hartman break; 305ab4382d2SGreg Kroah-Hartman udelay(1); 306ab4382d2SGreg Kroah-Hartman } 307ab4382d2SGreg Kroah-Hartman 308ab4382d2SGreg Kroah-Hartman spin_unlock_irqrestore(&port->lock, flags); 309ab4382d2SGreg Kroah-Hartman } 310ab4382d2SGreg Kroah-Hartman } 311ab4382d2SGreg Kroah-Hartman 312ab4382d2SGreg Kroah-Hartman /* port->lock is not held. */ 313ab4382d2SGreg Kroah-Hartman static int sunhv_startup(struct uart_port *port) 314ab4382d2SGreg Kroah-Hartman { 315ab4382d2SGreg Kroah-Hartman return 0; 316ab4382d2SGreg Kroah-Hartman } 317ab4382d2SGreg Kroah-Hartman 318ab4382d2SGreg Kroah-Hartman /* port->lock is not held. */ 319ab4382d2SGreg Kroah-Hartman static void sunhv_shutdown(struct uart_port *port) 320ab4382d2SGreg Kroah-Hartman { 321ab4382d2SGreg Kroah-Hartman } 322ab4382d2SGreg Kroah-Hartman 323ab4382d2SGreg Kroah-Hartman /* port->lock is not held. */ 324ab4382d2SGreg Kroah-Hartman static void sunhv_set_termios(struct uart_port *port, struct ktermios *termios, 325ab4382d2SGreg Kroah-Hartman struct ktermios *old) 326ab4382d2SGreg Kroah-Hartman { 327ab4382d2SGreg Kroah-Hartman unsigned int baud = uart_get_baud_rate(port, termios, old, 0, 4000000); 328ab4382d2SGreg Kroah-Hartman unsigned int quot = uart_get_divisor(port, baud); 329ab4382d2SGreg Kroah-Hartman unsigned int iflag, cflag; 330ab4382d2SGreg Kroah-Hartman unsigned long flags; 331ab4382d2SGreg Kroah-Hartman 332ab4382d2SGreg Kroah-Hartman spin_lock_irqsave(&port->lock, flags); 333ab4382d2SGreg Kroah-Hartman 334ab4382d2SGreg Kroah-Hartman iflag = termios->c_iflag; 335ab4382d2SGreg Kroah-Hartman cflag = termios->c_cflag; 336ab4382d2SGreg Kroah-Hartman 337ab4382d2SGreg Kroah-Hartman port->ignore_status_mask = 0; 338ab4382d2SGreg Kroah-Hartman if (iflag & IGNBRK) 339ab4382d2SGreg Kroah-Hartman port->ignore_status_mask |= IGNORE_BREAK; 340ab4382d2SGreg Kroah-Hartman if ((cflag & CREAD) == 0) 341ab4382d2SGreg Kroah-Hartman port->ignore_status_mask |= IGNORE_ALL; 342ab4382d2SGreg Kroah-Hartman 343ab4382d2SGreg Kroah-Hartman /* XXX */ 344ab4382d2SGreg Kroah-Hartman uart_update_timeout(port, cflag, 345ab4382d2SGreg Kroah-Hartman (port->uartclk / (16 * quot))); 346ab4382d2SGreg Kroah-Hartman 347ab4382d2SGreg Kroah-Hartman spin_unlock_irqrestore(&port->lock, flags); 348ab4382d2SGreg Kroah-Hartman } 349ab4382d2SGreg Kroah-Hartman 350ab4382d2SGreg Kroah-Hartman static const char *sunhv_type(struct uart_port *port) 351ab4382d2SGreg Kroah-Hartman { 352ab4382d2SGreg Kroah-Hartman return "SUN4V HCONS"; 353ab4382d2SGreg Kroah-Hartman } 354ab4382d2SGreg Kroah-Hartman 355ab4382d2SGreg Kroah-Hartman static void sunhv_release_port(struct uart_port *port) 356ab4382d2SGreg Kroah-Hartman { 357ab4382d2SGreg Kroah-Hartman } 358ab4382d2SGreg Kroah-Hartman 359ab4382d2SGreg Kroah-Hartman static int sunhv_request_port(struct uart_port *port) 360ab4382d2SGreg Kroah-Hartman { 361ab4382d2SGreg Kroah-Hartman return 0; 362ab4382d2SGreg Kroah-Hartman } 363ab4382d2SGreg Kroah-Hartman 364ab4382d2SGreg Kroah-Hartman static void sunhv_config_port(struct uart_port *port, int flags) 365ab4382d2SGreg Kroah-Hartman { 366ab4382d2SGreg Kroah-Hartman } 367ab4382d2SGreg Kroah-Hartman 368ab4382d2SGreg Kroah-Hartman static int sunhv_verify_port(struct uart_port *port, struct serial_struct *ser) 369ab4382d2SGreg Kroah-Hartman { 370ab4382d2SGreg Kroah-Hartman return -EINVAL; 371ab4382d2SGreg Kroah-Hartman } 372ab4382d2SGreg Kroah-Hartman 373ab4382d2SGreg Kroah-Hartman static struct uart_ops sunhv_pops = { 374ab4382d2SGreg Kroah-Hartman .tx_empty = sunhv_tx_empty, 375ab4382d2SGreg Kroah-Hartman .set_mctrl = sunhv_set_mctrl, 376ab4382d2SGreg Kroah-Hartman .get_mctrl = sunhv_get_mctrl, 377ab4382d2SGreg Kroah-Hartman .stop_tx = sunhv_stop_tx, 378ab4382d2SGreg Kroah-Hartman .start_tx = sunhv_start_tx, 379ab4382d2SGreg Kroah-Hartman .send_xchar = sunhv_send_xchar, 380ab4382d2SGreg Kroah-Hartman .stop_rx = sunhv_stop_rx, 381ab4382d2SGreg Kroah-Hartman .break_ctl = sunhv_break_ctl, 382ab4382d2SGreg Kroah-Hartman .startup = sunhv_startup, 383ab4382d2SGreg Kroah-Hartman .shutdown = sunhv_shutdown, 384ab4382d2SGreg Kroah-Hartman .set_termios = sunhv_set_termios, 385ab4382d2SGreg Kroah-Hartman .type = sunhv_type, 386ab4382d2SGreg Kroah-Hartman .release_port = sunhv_release_port, 387ab4382d2SGreg Kroah-Hartman .request_port = sunhv_request_port, 388ab4382d2SGreg Kroah-Hartman .config_port = sunhv_config_port, 389ab4382d2SGreg Kroah-Hartman .verify_port = sunhv_verify_port, 390ab4382d2SGreg Kroah-Hartman }; 391ab4382d2SGreg Kroah-Hartman 392ab4382d2SGreg Kroah-Hartman static struct uart_driver sunhv_reg = { 393ab4382d2SGreg Kroah-Hartman .owner = THIS_MODULE, 394ab4382d2SGreg Kroah-Hartman .driver_name = "sunhv", 395ab4382d2SGreg Kroah-Hartman .dev_name = "ttyS", 396ab4382d2SGreg Kroah-Hartman .major = TTY_MAJOR, 397ab4382d2SGreg Kroah-Hartman }; 398ab4382d2SGreg Kroah-Hartman 399ab4382d2SGreg Kroah-Hartman static struct uart_port *sunhv_port; 400ab4382d2SGreg Kroah-Hartman 401ab4382d2SGreg Kroah-Hartman /* Copy 's' into the con_write_page, decoding "\n" into 402ab4382d2SGreg Kroah-Hartman * "\r\n" along the way. We have to return two lengths 403ab4382d2SGreg Kroah-Hartman * because the caller needs to know how much to advance 404ab4382d2SGreg Kroah-Hartman * 's' and also how many bytes to output via con_write_page. 405ab4382d2SGreg Kroah-Hartman */ 406ab4382d2SGreg Kroah-Hartman static int fill_con_write_page(const char *s, unsigned int n, 407ab4382d2SGreg Kroah-Hartman unsigned long *page_bytes) 408ab4382d2SGreg Kroah-Hartman { 409ab4382d2SGreg Kroah-Hartman const char *orig_s = s; 410ab4382d2SGreg Kroah-Hartman char *p = con_write_page; 411ab4382d2SGreg Kroah-Hartman int left = PAGE_SIZE; 412ab4382d2SGreg Kroah-Hartman 413ab4382d2SGreg Kroah-Hartman while (n--) { 414ab4382d2SGreg Kroah-Hartman if (*s == '\n') { 415ab4382d2SGreg Kroah-Hartman if (left < 2) 416ab4382d2SGreg Kroah-Hartman break; 417ab4382d2SGreg Kroah-Hartman *p++ = '\r'; 418ab4382d2SGreg Kroah-Hartman left--; 419ab4382d2SGreg Kroah-Hartman } else if (left < 1) 420ab4382d2SGreg Kroah-Hartman break; 421ab4382d2SGreg Kroah-Hartman *p++ = *s++; 422ab4382d2SGreg Kroah-Hartman left--; 423ab4382d2SGreg Kroah-Hartman } 424ab4382d2SGreg Kroah-Hartman *page_bytes = p - con_write_page; 425ab4382d2SGreg Kroah-Hartman return s - orig_s; 426ab4382d2SGreg Kroah-Hartman } 427ab4382d2SGreg Kroah-Hartman 428ab4382d2SGreg Kroah-Hartman static void sunhv_console_write_paged(struct console *con, const char *s, unsigned n) 429ab4382d2SGreg Kroah-Hartman { 430ab4382d2SGreg Kroah-Hartman struct uart_port *port = sunhv_port; 431ab4382d2SGreg Kroah-Hartman unsigned long flags; 432ab4382d2SGreg Kroah-Hartman int locked = 1; 433ab4382d2SGreg Kroah-Hartman 434e58e241cSDavid Miller if (port->sysrq || oops_in_progress) 435e58e241cSDavid Miller locked = spin_trylock_irqsave(&port->lock, flags); 436e58e241cSDavid Miller else 437e58e241cSDavid Miller spin_lock_irqsave(&port->lock, flags); 438ab4382d2SGreg Kroah-Hartman 439ab4382d2SGreg Kroah-Hartman while (n > 0) { 440ab4382d2SGreg Kroah-Hartman unsigned long ra = __pa(con_write_page); 441ab4382d2SGreg Kroah-Hartman unsigned long page_bytes; 442ab4382d2SGreg Kroah-Hartman unsigned int cpy = fill_con_write_page(s, n, 443ab4382d2SGreg Kroah-Hartman &page_bytes); 444ab4382d2SGreg Kroah-Hartman 445ab4382d2SGreg Kroah-Hartman n -= cpy; 446ab4382d2SGreg Kroah-Hartman s += cpy; 447ab4382d2SGreg Kroah-Hartman while (page_bytes > 0) { 448ab4382d2SGreg Kroah-Hartman unsigned long written; 449ab4382d2SGreg Kroah-Hartman int limit = 1000000; 450ab4382d2SGreg Kroah-Hartman 451ab4382d2SGreg Kroah-Hartman while (limit--) { 452ab4382d2SGreg Kroah-Hartman unsigned long stat; 453ab4382d2SGreg Kroah-Hartman 454ab4382d2SGreg Kroah-Hartman stat = sun4v_con_write(ra, page_bytes, 455ab4382d2SGreg Kroah-Hartman &written); 456ab4382d2SGreg Kroah-Hartman if (stat == HV_EOK) 457ab4382d2SGreg Kroah-Hartman break; 458ab4382d2SGreg Kroah-Hartman udelay(1); 459ab4382d2SGreg Kroah-Hartman } 460ab4382d2SGreg Kroah-Hartman if (limit < 0) 461ab4382d2SGreg Kroah-Hartman break; 462ab4382d2SGreg Kroah-Hartman page_bytes -= written; 463ab4382d2SGreg Kroah-Hartman ra += written; 464ab4382d2SGreg Kroah-Hartman } 465ab4382d2SGreg Kroah-Hartman } 466ab4382d2SGreg Kroah-Hartman 467ab4382d2SGreg Kroah-Hartman if (locked) 468e58e241cSDavid Miller spin_unlock_irqrestore(&port->lock, flags); 469ab4382d2SGreg Kroah-Hartman } 470ab4382d2SGreg Kroah-Hartman 471ab4382d2SGreg Kroah-Hartman static inline void sunhv_console_putchar(struct uart_port *port, char c) 472ab4382d2SGreg Kroah-Hartman { 473ab4382d2SGreg Kroah-Hartman int limit = 1000000; 474ab4382d2SGreg Kroah-Hartman 475ab4382d2SGreg Kroah-Hartman while (limit-- > 0) { 476ab4382d2SGreg Kroah-Hartman long status = sun4v_con_putchar(c); 477ab4382d2SGreg Kroah-Hartman if (status == HV_EOK) 478ab4382d2SGreg Kroah-Hartman break; 479ab4382d2SGreg Kroah-Hartman udelay(1); 480ab4382d2SGreg Kroah-Hartman } 481ab4382d2SGreg Kroah-Hartman } 482ab4382d2SGreg Kroah-Hartman 483ab4382d2SGreg Kroah-Hartman static void sunhv_console_write_bychar(struct console *con, const char *s, unsigned n) 484ab4382d2SGreg Kroah-Hartman { 485ab4382d2SGreg Kroah-Hartman struct uart_port *port = sunhv_port; 486ab4382d2SGreg Kroah-Hartman unsigned long flags; 487ab4382d2SGreg Kroah-Hartman int i, locked = 1; 488ab4382d2SGreg Kroah-Hartman 489e58e241cSDavid Miller if (port->sysrq || oops_in_progress) 490e58e241cSDavid Miller locked = spin_trylock_irqsave(&port->lock, flags); 491e58e241cSDavid Miller else 492e58e241cSDavid Miller spin_lock_irqsave(&port->lock, flags); 493ab4382d2SGreg Kroah-Hartman 494ab4382d2SGreg Kroah-Hartman for (i = 0; i < n; i++) { 495ab4382d2SGreg Kroah-Hartman if (*s == '\n') 496ab4382d2SGreg Kroah-Hartman sunhv_console_putchar(port, '\r'); 497ab4382d2SGreg Kroah-Hartman sunhv_console_putchar(port, *s++); 498ab4382d2SGreg Kroah-Hartman } 499ab4382d2SGreg Kroah-Hartman 500ab4382d2SGreg Kroah-Hartman if (locked) 501e58e241cSDavid Miller spin_unlock_irqrestore(&port->lock, flags); 502ab4382d2SGreg Kroah-Hartman } 503ab4382d2SGreg Kroah-Hartman 504ab4382d2SGreg Kroah-Hartman static struct console sunhv_console = { 505ab4382d2SGreg Kroah-Hartman .name = "ttyHV", 506ab4382d2SGreg Kroah-Hartman .write = sunhv_console_write_bychar, 507ab4382d2SGreg Kroah-Hartman .device = uart_console_device, 508ab4382d2SGreg Kroah-Hartman .flags = CON_PRINTBUFFER, 509ab4382d2SGreg Kroah-Hartman .index = -1, 510ab4382d2SGreg Kroah-Hartman .data = &sunhv_reg, 511ab4382d2SGreg Kroah-Hartman }; 512ab4382d2SGreg Kroah-Hartman 5139671f099SBill Pemberton static int hv_probe(struct platform_device *op) 514ab4382d2SGreg Kroah-Hartman { 515ab4382d2SGreg Kroah-Hartman struct uart_port *port; 516ab4382d2SGreg Kroah-Hartman unsigned long minor; 517ab4382d2SGreg Kroah-Hartman int err; 518ab4382d2SGreg Kroah-Hartman 519ab4382d2SGreg Kroah-Hartman if (op->archdata.irqs[0] == 0xffffffff) 520ab4382d2SGreg Kroah-Hartman return -ENODEV; 521ab4382d2SGreg Kroah-Hartman 522ab4382d2SGreg Kroah-Hartman port = kzalloc(sizeof(struct uart_port), GFP_KERNEL); 523ab4382d2SGreg Kroah-Hartman if (unlikely(!port)) 524ab4382d2SGreg Kroah-Hartman return -ENOMEM; 525ab4382d2SGreg Kroah-Hartman 526ab4382d2SGreg Kroah-Hartman minor = 1; 527ab4382d2SGreg Kroah-Hartman if (sun4v_hvapi_register(HV_GRP_CORE, 1, &minor) == 0 && 528ab4382d2SGreg Kroah-Hartman minor >= 1) { 529ab4382d2SGreg Kroah-Hartman err = -ENOMEM; 530ab4382d2SGreg Kroah-Hartman con_write_page = kzalloc(PAGE_SIZE, GFP_KERNEL); 531ab4382d2SGreg Kroah-Hartman if (!con_write_page) 532ab4382d2SGreg Kroah-Hartman goto out_free_port; 533ab4382d2SGreg Kroah-Hartman 534ab4382d2SGreg Kroah-Hartman con_read_page = kzalloc(PAGE_SIZE, GFP_KERNEL); 535ab4382d2SGreg Kroah-Hartman if (!con_read_page) 536ab4382d2SGreg Kroah-Hartman goto out_free_con_write_page; 537ab4382d2SGreg Kroah-Hartman 538ab4382d2SGreg Kroah-Hartman sunhv_console.write = sunhv_console_write_paged; 539ab4382d2SGreg Kroah-Hartman sunhv_ops = &bywrite_ops; 540ab4382d2SGreg Kroah-Hartman } 541ab4382d2SGreg Kroah-Hartman 542ab4382d2SGreg Kroah-Hartman sunhv_port = port; 543ab4382d2SGreg Kroah-Hartman 544ab4382d2SGreg Kroah-Hartman port->line = 0; 545ab4382d2SGreg Kroah-Hartman port->ops = &sunhv_pops; 546ab4382d2SGreg Kroah-Hartman port->type = PORT_SUNHV; 547ab4382d2SGreg Kroah-Hartman port->uartclk = ( 29491200 / 16 ); /* arbitrary */ 548ab4382d2SGreg Kroah-Hartman 549ab4382d2SGreg Kroah-Hartman port->membase = (unsigned char __iomem *) __pa(port); 550ab4382d2SGreg Kroah-Hartman 551ab4382d2SGreg Kroah-Hartman port->irq = op->archdata.irqs[0]; 552ab4382d2SGreg Kroah-Hartman 553ab4382d2SGreg Kroah-Hartman port->dev = &op->dev; 554ab4382d2SGreg Kroah-Hartman 555ab4382d2SGreg Kroah-Hartman err = sunserial_register_minors(&sunhv_reg, 1); 556ab4382d2SGreg Kroah-Hartman if (err) 557ab4382d2SGreg Kroah-Hartman goto out_free_con_read_page; 558ab4382d2SGreg Kroah-Hartman 559ab4382d2SGreg Kroah-Hartman sunserial_console_match(&sunhv_console, op->dev.of_node, 560ab4382d2SGreg Kroah-Hartman &sunhv_reg, port->line, false); 561ab4382d2SGreg Kroah-Hartman 562ab4382d2SGreg Kroah-Hartman err = uart_add_one_port(&sunhv_reg, port); 563ab4382d2SGreg Kroah-Hartman if (err) 564ab4382d2SGreg Kroah-Hartman goto out_unregister_driver; 565ab4382d2SGreg Kroah-Hartman 566ab4382d2SGreg Kroah-Hartman err = request_irq(port->irq, sunhv_interrupt, 0, "hvcons", port); 567ab4382d2SGreg Kroah-Hartman if (err) 568ab4382d2SGreg Kroah-Hartman goto out_remove_port; 569ab4382d2SGreg Kroah-Hartman 570696faeddSJingoo Han platform_set_drvdata(op, port); 571ab4382d2SGreg Kroah-Hartman 572ab4382d2SGreg Kroah-Hartman return 0; 573ab4382d2SGreg Kroah-Hartman 574ab4382d2SGreg Kroah-Hartman out_remove_port: 575ab4382d2SGreg Kroah-Hartman uart_remove_one_port(&sunhv_reg, port); 576ab4382d2SGreg Kroah-Hartman 577ab4382d2SGreg Kroah-Hartman out_unregister_driver: 578ab4382d2SGreg Kroah-Hartman sunserial_unregister_minors(&sunhv_reg, 1); 579ab4382d2SGreg Kroah-Hartman 580ab4382d2SGreg Kroah-Hartman out_free_con_read_page: 581ab4382d2SGreg Kroah-Hartman kfree(con_read_page); 582ab4382d2SGreg Kroah-Hartman 583ab4382d2SGreg Kroah-Hartman out_free_con_write_page: 584ab4382d2SGreg Kroah-Hartman kfree(con_write_page); 585ab4382d2SGreg Kroah-Hartman 586ab4382d2SGreg Kroah-Hartman out_free_port: 587ab4382d2SGreg Kroah-Hartman kfree(port); 588ab4382d2SGreg Kroah-Hartman sunhv_port = NULL; 589ab4382d2SGreg Kroah-Hartman return err; 590ab4382d2SGreg Kroah-Hartman } 591ab4382d2SGreg Kroah-Hartman 592ae8d8a14SBill Pemberton static int hv_remove(struct platform_device *dev) 593ab4382d2SGreg Kroah-Hartman { 594696faeddSJingoo Han struct uart_port *port = platform_get_drvdata(dev); 595ab4382d2SGreg Kroah-Hartman 596ab4382d2SGreg Kroah-Hartman free_irq(port->irq, port); 597ab4382d2SGreg Kroah-Hartman 598ab4382d2SGreg Kroah-Hartman uart_remove_one_port(&sunhv_reg, port); 599ab4382d2SGreg Kroah-Hartman 600ab4382d2SGreg Kroah-Hartman sunserial_unregister_minors(&sunhv_reg, 1); 601*fbb7d2e3SSouptick Joarder kfree(con_read_page); 602*fbb7d2e3SSouptick Joarder kfree(con_write_page); 603ab4382d2SGreg Kroah-Hartman kfree(port); 604ab4382d2SGreg Kroah-Hartman sunhv_port = NULL; 605ab4382d2SGreg Kroah-Hartman 606ab4382d2SGreg Kroah-Hartman return 0; 607ab4382d2SGreg Kroah-Hartman } 608ab4382d2SGreg Kroah-Hartman 609ab4382d2SGreg Kroah-Hartman static const struct of_device_id hv_match[] = { 610ab4382d2SGreg Kroah-Hartman { 611ab4382d2SGreg Kroah-Hartman .name = "console", 612ab4382d2SGreg Kroah-Hartman .compatible = "qcn", 613ab4382d2SGreg Kroah-Hartman }, 614ab4382d2SGreg Kroah-Hartman { 615ab4382d2SGreg Kroah-Hartman .name = "console", 616ab4382d2SGreg Kroah-Hartman .compatible = "SUNW,sun4v-console", 617ab4382d2SGreg Kroah-Hartman }, 618ab4382d2SGreg Kroah-Hartman {}, 619ab4382d2SGreg Kroah-Hartman }; 620ab4382d2SGreg Kroah-Hartman 621793218dfSGrant Likely static struct platform_driver hv_driver = { 622ab4382d2SGreg Kroah-Hartman .driver = { 623ab4382d2SGreg Kroah-Hartman .name = "hv", 624ab4382d2SGreg Kroah-Hartman .of_match_table = hv_match, 625ab4382d2SGreg Kroah-Hartman }, 626ab4382d2SGreg Kroah-Hartman .probe = hv_probe, 6272d47b716SBill Pemberton .remove = hv_remove, 628ab4382d2SGreg Kroah-Hartman }; 629ab4382d2SGreg Kroah-Hartman 630ab4382d2SGreg Kroah-Hartman static int __init sunhv_init(void) 631ab4382d2SGreg Kroah-Hartman { 632ab4382d2SGreg Kroah-Hartman if (tlb_type != hypervisor) 633ab4382d2SGreg Kroah-Hartman return -ENODEV; 634ab4382d2SGreg Kroah-Hartman 635793218dfSGrant Likely return platform_driver_register(&hv_driver); 636ab4382d2SGreg Kroah-Hartman } 6374fef5351SPaul Gortmaker device_initcall(sunhv_init); 638ab4382d2SGreg Kroah-Hartman 6394fef5351SPaul Gortmaker #if 0 /* ...def MODULE ; never supported as such */ 640ab4382d2SGreg Kroah-Hartman MODULE_AUTHOR("David S. Miller"); 641ab4382d2SGreg Kroah-Hartman MODULE_DESCRIPTION("SUN4V Hypervisor console driver"); 642ab4382d2SGreg Kroah-Hartman MODULE_VERSION("2.0"); 643ab4382d2SGreg Kroah-Hartman MODULE_LICENSE("GPL"); 6444fef5351SPaul Gortmaker #endif 645