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/module.h> 7ab4382d2SGreg Kroah-Hartman #include <linux/kernel.h> 8ab4382d2SGreg Kroah-Hartman #include <linux/errno.h> 9ab4382d2SGreg Kroah-Hartman #include <linux/tty.h> 10ab4382d2SGreg Kroah-Hartman #include <linux/tty_flip.h> 11ab4382d2SGreg Kroah-Hartman #include <linux/major.h> 12ab4382d2SGreg Kroah-Hartman #include <linux/circ_buf.h> 13ab4382d2SGreg Kroah-Hartman #include <linux/serial.h> 14ab4382d2SGreg Kroah-Hartman #include <linux/sysrq.h> 15ab4382d2SGreg Kroah-Hartman #include <linux/console.h> 16ab4382d2SGreg Kroah-Hartman #include <linux/spinlock.h> 17ab4382d2SGreg Kroah-Hartman #include <linux/slab.h> 18ab4382d2SGreg Kroah-Hartman #include <linux/delay.h> 19ab4382d2SGreg Kroah-Hartman #include <linux/init.h> 20ab4382d2SGreg Kroah-Hartman #include <linux/of_device.h> 21ab4382d2SGreg Kroah-Hartman 22ab4382d2SGreg Kroah-Hartman #include <asm/hypervisor.h> 23ab4382d2SGreg Kroah-Hartman #include <asm/spitfire.h> 24ab4382d2SGreg Kroah-Hartman #include <asm/prom.h> 25ab4382d2SGreg Kroah-Hartman #include <asm/irq.h> 26d550bbd4SDavid Howells #include <asm/setup.h> 27ab4382d2SGreg Kroah-Hartman 28ab4382d2SGreg Kroah-Hartman #if defined(CONFIG_MAGIC_SYSRQ) 29ab4382d2SGreg Kroah-Hartman #define SUPPORT_SYSRQ 30ab4382d2SGreg Kroah-Hartman #endif 31ab4382d2SGreg Kroah-Hartman 32ab4382d2SGreg Kroah-Hartman #include <linux/serial_core.h> 336816383aSPaul Gortmaker #include <linux/sunserialcore.h> 34ab4382d2SGreg Kroah-Hartman 35ab4382d2SGreg Kroah-Hartman #define CON_BREAK ((long)-1) 36ab4382d2SGreg Kroah-Hartman #define CON_HUP ((long)-2) 37ab4382d2SGreg Kroah-Hartman 38ab4382d2SGreg Kroah-Hartman #define IGNORE_BREAK 0x1 39ab4382d2SGreg Kroah-Hartman #define IGNORE_ALL 0x2 40ab4382d2SGreg Kroah-Hartman 41ab4382d2SGreg Kroah-Hartman static char *con_write_page; 42ab4382d2SGreg Kroah-Hartman static char *con_read_page; 43ab4382d2SGreg Kroah-Hartman 44ab4382d2SGreg Kroah-Hartman static int hung_up = 0; 45ab4382d2SGreg Kroah-Hartman 46ab4382d2SGreg Kroah-Hartman static void transmit_chars_putchar(struct uart_port *port, struct circ_buf *xmit) 47ab4382d2SGreg Kroah-Hartman { 48ab4382d2SGreg Kroah-Hartman while (!uart_circ_empty(xmit)) { 49ab4382d2SGreg Kroah-Hartman long status = sun4v_con_putchar(xmit->buf[xmit->tail]); 50ab4382d2SGreg Kroah-Hartman 51ab4382d2SGreg Kroah-Hartman if (status != HV_EOK) 52ab4382d2SGreg Kroah-Hartman break; 53ab4382d2SGreg Kroah-Hartman 54ab4382d2SGreg Kroah-Hartman xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1); 55ab4382d2SGreg Kroah-Hartman port->icount.tx++; 56ab4382d2SGreg Kroah-Hartman } 57ab4382d2SGreg Kroah-Hartman } 58ab4382d2SGreg Kroah-Hartman 59ab4382d2SGreg Kroah-Hartman static void transmit_chars_write(struct uart_port *port, struct circ_buf *xmit) 60ab4382d2SGreg Kroah-Hartman { 61ab4382d2SGreg Kroah-Hartman while (!uart_circ_empty(xmit)) { 62ab4382d2SGreg Kroah-Hartman unsigned long ra = __pa(xmit->buf + xmit->tail); 63ab4382d2SGreg Kroah-Hartman unsigned long len, status, sent; 64ab4382d2SGreg Kroah-Hartman 65ab4382d2SGreg Kroah-Hartman len = CIRC_CNT_TO_END(xmit->head, xmit->tail, 66ab4382d2SGreg Kroah-Hartman UART_XMIT_SIZE); 67ab4382d2SGreg Kroah-Hartman status = sun4v_con_write(ra, len, &sent); 68ab4382d2SGreg Kroah-Hartman if (status != HV_EOK) 69ab4382d2SGreg Kroah-Hartman break; 70ab4382d2SGreg Kroah-Hartman xmit->tail = (xmit->tail + sent) & (UART_XMIT_SIZE - 1); 71ab4382d2SGreg Kroah-Hartman port->icount.tx += sent; 72ab4382d2SGreg Kroah-Hartman } 73ab4382d2SGreg Kroah-Hartman } 74ab4382d2SGreg Kroah-Hartman 75ab4382d2SGreg Kroah-Hartman static int receive_chars_getchar(struct uart_port *port, struct tty_struct *tty) 76ab4382d2SGreg Kroah-Hartman { 77ab4382d2SGreg Kroah-Hartman int saw_console_brk = 0; 78ab4382d2SGreg Kroah-Hartman int limit = 10000; 79ab4382d2SGreg Kroah-Hartman 80ab4382d2SGreg Kroah-Hartman while (limit-- > 0) { 81ab4382d2SGreg Kroah-Hartman long status; 82ab4382d2SGreg Kroah-Hartman long c = sun4v_con_getchar(&status); 83ab4382d2SGreg Kroah-Hartman 84ab4382d2SGreg Kroah-Hartman if (status == HV_EWOULDBLOCK) 85ab4382d2SGreg Kroah-Hartman break; 86ab4382d2SGreg Kroah-Hartman 87ab4382d2SGreg Kroah-Hartman if (c == CON_BREAK) { 88ab4382d2SGreg Kroah-Hartman if (uart_handle_break(port)) 89ab4382d2SGreg Kroah-Hartman continue; 90ab4382d2SGreg Kroah-Hartman saw_console_brk = 1; 91ab4382d2SGreg Kroah-Hartman c = 0; 92ab4382d2SGreg Kroah-Hartman } 93ab4382d2SGreg Kroah-Hartman 94ab4382d2SGreg Kroah-Hartman if (c == CON_HUP) { 95ab4382d2SGreg Kroah-Hartman hung_up = 1; 96ab4382d2SGreg Kroah-Hartman uart_handle_dcd_change(port, 0); 97ab4382d2SGreg Kroah-Hartman } else if (hung_up) { 98ab4382d2SGreg Kroah-Hartman hung_up = 0; 99ab4382d2SGreg Kroah-Hartman uart_handle_dcd_change(port, 1); 100ab4382d2SGreg Kroah-Hartman } 101ab4382d2SGreg Kroah-Hartman 102*92a19f9cSJiri Slaby if (port->state == NULL) { 103ab4382d2SGreg Kroah-Hartman uart_handle_sysrq_char(port, c); 104ab4382d2SGreg Kroah-Hartman continue; 105ab4382d2SGreg Kroah-Hartman } 106ab4382d2SGreg Kroah-Hartman 107ab4382d2SGreg Kroah-Hartman port->icount.rx++; 108ab4382d2SGreg Kroah-Hartman 109ab4382d2SGreg Kroah-Hartman if (uart_handle_sysrq_char(port, c)) 110ab4382d2SGreg Kroah-Hartman continue; 111ab4382d2SGreg Kroah-Hartman 112*92a19f9cSJiri Slaby tty_insert_flip_char(&port->state->port, c, TTY_NORMAL); 113ab4382d2SGreg Kroah-Hartman } 114ab4382d2SGreg Kroah-Hartman 115ab4382d2SGreg Kroah-Hartman return saw_console_brk; 116ab4382d2SGreg Kroah-Hartman } 117ab4382d2SGreg Kroah-Hartman 118ab4382d2SGreg Kroah-Hartman static int receive_chars_read(struct uart_port *port, struct tty_struct *tty) 119ab4382d2SGreg Kroah-Hartman { 120ab4382d2SGreg Kroah-Hartman int saw_console_brk = 0; 121ab4382d2SGreg Kroah-Hartman int limit = 10000; 122ab4382d2SGreg Kroah-Hartman 123ab4382d2SGreg Kroah-Hartman while (limit-- > 0) { 124ab4382d2SGreg Kroah-Hartman unsigned long ra = __pa(con_read_page); 125ab4382d2SGreg Kroah-Hartman unsigned long bytes_read, i; 126ab4382d2SGreg Kroah-Hartman long stat = sun4v_con_read(ra, PAGE_SIZE, &bytes_read); 127ab4382d2SGreg Kroah-Hartman 128ab4382d2SGreg Kroah-Hartman if (stat != HV_EOK) { 129ab4382d2SGreg Kroah-Hartman bytes_read = 0; 130ab4382d2SGreg Kroah-Hartman 131ab4382d2SGreg Kroah-Hartman if (stat == CON_BREAK) { 132ab4382d2SGreg Kroah-Hartman if (uart_handle_break(port)) 133ab4382d2SGreg Kroah-Hartman continue; 134ab4382d2SGreg Kroah-Hartman saw_console_brk = 1; 135ab4382d2SGreg Kroah-Hartman *con_read_page = 0; 136ab4382d2SGreg Kroah-Hartman bytes_read = 1; 137ab4382d2SGreg Kroah-Hartman } else if (stat == CON_HUP) { 138ab4382d2SGreg Kroah-Hartman hung_up = 1; 139ab4382d2SGreg Kroah-Hartman uart_handle_dcd_change(port, 0); 140ab4382d2SGreg Kroah-Hartman continue; 141ab4382d2SGreg Kroah-Hartman } else { 142ab4382d2SGreg Kroah-Hartman /* HV_EWOULDBLOCK, etc. */ 143ab4382d2SGreg Kroah-Hartman break; 144ab4382d2SGreg Kroah-Hartman } 145ab4382d2SGreg Kroah-Hartman } 146ab4382d2SGreg Kroah-Hartman 147ab4382d2SGreg Kroah-Hartman if (hung_up) { 148ab4382d2SGreg Kroah-Hartman hung_up = 0; 149ab4382d2SGreg Kroah-Hartman uart_handle_dcd_change(port, 1); 150ab4382d2SGreg Kroah-Hartman } 151ab4382d2SGreg Kroah-Hartman 152ab4382d2SGreg Kroah-Hartman for (i = 0; i < bytes_read; i++) 153ab4382d2SGreg Kroah-Hartman uart_handle_sysrq_char(port, con_read_page[i]); 154ab4382d2SGreg Kroah-Hartman 155ab4382d2SGreg Kroah-Hartman if (tty == NULL) 156ab4382d2SGreg Kroah-Hartman continue; 157ab4382d2SGreg Kroah-Hartman 158ab4382d2SGreg Kroah-Hartman port->icount.rx += bytes_read; 159ab4382d2SGreg Kroah-Hartman 160ab4382d2SGreg Kroah-Hartman tty_insert_flip_string(tty, con_read_page, bytes_read); 161ab4382d2SGreg Kroah-Hartman } 162ab4382d2SGreg Kroah-Hartman 163ab4382d2SGreg Kroah-Hartman return saw_console_brk; 164ab4382d2SGreg Kroah-Hartman } 165ab4382d2SGreg Kroah-Hartman 166ab4382d2SGreg Kroah-Hartman struct sunhv_ops { 167ab4382d2SGreg Kroah-Hartman void (*transmit_chars)(struct uart_port *port, struct circ_buf *xmit); 168ab4382d2SGreg Kroah-Hartman int (*receive_chars)(struct uart_port *port, struct tty_struct *tty); 169ab4382d2SGreg Kroah-Hartman }; 170ab4382d2SGreg Kroah-Hartman 171ab4382d2SGreg Kroah-Hartman static struct sunhv_ops bychar_ops = { 172ab4382d2SGreg Kroah-Hartman .transmit_chars = transmit_chars_putchar, 173ab4382d2SGreg Kroah-Hartman .receive_chars = receive_chars_getchar, 174ab4382d2SGreg Kroah-Hartman }; 175ab4382d2SGreg Kroah-Hartman 176ab4382d2SGreg Kroah-Hartman static struct sunhv_ops bywrite_ops = { 177ab4382d2SGreg Kroah-Hartman .transmit_chars = transmit_chars_write, 178ab4382d2SGreg Kroah-Hartman .receive_chars = receive_chars_read, 179ab4382d2SGreg Kroah-Hartman }; 180ab4382d2SGreg Kroah-Hartman 181ab4382d2SGreg Kroah-Hartman static struct sunhv_ops *sunhv_ops = &bychar_ops; 182ab4382d2SGreg Kroah-Hartman 183ab4382d2SGreg Kroah-Hartman static struct tty_struct *receive_chars(struct uart_port *port) 184ab4382d2SGreg Kroah-Hartman { 185ab4382d2SGreg Kroah-Hartman struct tty_struct *tty = NULL; 186ab4382d2SGreg Kroah-Hartman 187ab4382d2SGreg Kroah-Hartman if (port->state != NULL) /* Unopened serial console */ 188ab4382d2SGreg Kroah-Hartman tty = port->state->port.tty; 189ab4382d2SGreg Kroah-Hartman 190ab4382d2SGreg Kroah-Hartman if (sunhv_ops->receive_chars(port, tty)) 191ab4382d2SGreg Kroah-Hartman sun_do_break(); 192ab4382d2SGreg Kroah-Hartman 193ab4382d2SGreg Kroah-Hartman return tty; 194ab4382d2SGreg Kroah-Hartman } 195ab4382d2SGreg Kroah-Hartman 196ab4382d2SGreg Kroah-Hartman static void transmit_chars(struct uart_port *port) 197ab4382d2SGreg Kroah-Hartman { 198ab4382d2SGreg Kroah-Hartman struct circ_buf *xmit; 199ab4382d2SGreg Kroah-Hartman 200ab4382d2SGreg Kroah-Hartman if (!port->state) 201ab4382d2SGreg Kroah-Hartman return; 202ab4382d2SGreg Kroah-Hartman 203ab4382d2SGreg Kroah-Hartman xmit = &port->state->xmit; 204ab4382d2SGreg Kroah-Hartman if (uart_circ_empty(xmit) || uart_tx_stopped(port)) 205ab4382d2SGreg Kroah-Hartman return; 206ab4382d2SGreg Kroah-Hartman 207ab4382d2SGreg Kroah-Hartman sunhv_ops->transmit_chars(port, xmit); 208ab4382d2SGreg Kroah-Hartman 209ab4382d2SGreg Kroah-Hartman if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS) 210ab4382d2SGreg Kroah-Hartman uart_write_wakeup(port); 211ab4382d2SGreg Kroah-Hartman } 212ab4382d2SGreg Kroah-Hartman 213ab4382d2SGreg Kroah-Hartman static irqreturn_t sunhv_interrupt(int irq, void *dev_id) 214ab4382d2SGreg Kroah-Hartman { 215ab4382d2SGreg Kroah-Hartman struct uart_port *port = dev_id; 216ab4382d2SGreg Kroah-Hartman struct tty_struct *tty; 217ab4382d2SGreg Kroah-Hartman unsigned long flags; 218ab4382d2SGreg Kroah-Hartman 219ab4382d2SGreg Kroah-Hartman spin_lock_irqsave(&port->lock, flags); 220ab4382d2SGreg Kroah-Hartman tty = receive_chars(port); 221ab4382d2SGreg Kroah-Hartman transmit_chars(port); 222ab4382d2SGreg Kroah-Hartman spin_unlock_irqrestore(&port->lock, flags); 223ab4382d2SGreg Kroah-Hartman 224ab4382d2SGreg Kroah-Hartman if (tty) 225ab4382d2SGreg Kroah-Hartman tty_flip_buffer_push(tty); 226ab4382d2SGreg Kroah-Hartman 227ab4382d2SGreg Kroah-Hartman return IRQ_HANDLED; 228ab4382d2SGreg Kroah-Hartman } 229ab4382d2SGreg Kroah-Hartman 230ab4382d2SGreg Kroah-Hartman /* port->lock is not held. */ 231ab4382d2SGreg Kroah-Hartman static unsigned int sunhv_tx_empty(struct uart_port *port) 232ab4382d2SGreg Kroah-Hartman { 233ab4382d2SGreg Kroah-Hartman /* Transmitter is always empty for us. If the circ buffer 234ab4382d2SGreg Kroah-Hartman * is non-empty or there is an x_char pending, our caller 235ab4382d2SGreg Kroah-Hartman * will do the right thing and ignore what we return here. 236ab4382d2SGreg Kroah-Hartman */ 237ab4382d2SGreg Kroah-Hartman return TIOCSER_TEMT; 238ab4382d2SGreg Kroah-Hartman } 239ab4382d2SGreg Kroah-Hartman 240ab4382d2SGreg Kroah-Hartman /* port->lock held by caller. */ 241ab4382d2SGreg Kroah-Hartman static void sunhv_set_mctrl(struct uart_port *port, unsigned int mctrl) 242ab4382d2SGreg Kroah-Hartman { 243ab4382d2SGreg Kroah-Hartman return; 244ab4382d2SGreg Kroah-Hartman } 245ab4382d2SGreg Kroah-Hartman 246ab4382d2SGreg Kroah-Hartman /* port->lock is held by caller and interrupts are disabled. */ 247ab4382d2SGreg Kroah-Hartman static unsigned int sunhv_get_mctrl(struct uart_port *port) 248ab4382d2SGreg Kroah-Hartman { 249ab4382d2SGreg Kroah-Hartman return TIOCM_DSR | TIOCM_CAR | TIOCM_CTS; 250ab4382d2SGreg Kroah-Hartman } 251ab4382d2SGreg Kroah-Hartman 252ab4382d2SGreg Kroah-Hartman /* port->lock held by caller. */ 253ab4382d2SGreg Kroah-Hartman static void sunhv_stop_tx(struct uart_port *port) 254ab4382d2SGreg Kroah-Hartman { 255ab4382d2SGreg Kroah-Hartman return; 256ab4382d2SGreg Kroah-Hartman } 257ab4382d2SGreg Kroah-Hartman 258ab4382d2SGreg Kroah-Hartman /* port->lock held by caller. */ 259ab4382d2SGreg Kroah-Hartman static void sunhv_start_tx(struct uart_port *port) 260ab4382d2SGreg Kroah-Hartman { 261ab4382d2SGreg Kroah-Hartman transmit_chars(port); 262ab4382d2SGreg Kroah-Hartman } 263ab4382d2SGreg Kroah-Hartman 264ab4382d2SGreg Kroah-Hartman /* port->lock is not held. */ 265ab4382d2SGreg Kroah-Hartman static void sunhv_send_xchar(struct uart_port *port, char ch) 266ab4382d2SGreg Kroah-Hartman { 267ab4382d2SGreg Kroah-Hartman unsigned long flags; 268ab4382d2SGreg Kroah-Hartman int limit = 10000; 269ab4382d2SGreg Kroah-Hartman 270ab4382d2SGreg Kroah-Hartman spin_lock_irqsave(&port->lock, flags); 271ab4382d2SGreg Kroah-Hartman 272ab4382d2SGreg Kroah-Hartman while (limit-- > 0) { 273ab4382d2SGreg Kroah-Hartman long status = sun4v_con_putchar(ch); 274ab4382d2SGreg Kroah-Hartman if (status == HV_EOK) 275ab4382d2SGreg Kroah-Hartman break; 276ab4382d2SGreg Kroah-Hartman udelay(1); 277ab4382d2SGreg Kroah-Hartman } 278ab4382d2SGreg Kroah-Hartman 279ab4382d2SGreg Kroah-Hartman spin_unlock_irqrestore(&port->lock, flags); 280ab4382d2SGreg Kroah-Hartman } 281ab4382d2SGreg Kroah-Hartman 282ab4382d2SGreg Kroah-Hartman /* port->lock held by caller. */ 283ab4382d2SGreg Kroah-Hartman static void sunhv_stop_rx(struct uart_port *port) 284ab4382d2SGreg Kroah-Hartman { 285ab4382d2SGreg Kroah-Hartman } 286ab4382d2SGreg Kroah-Hartman 287ab4382d2SGreg Kroah-Hartman /* port->lock held by caller. */ 288ab4382d2SGreg Kroah-Hartman static void sunhv_enable_ms(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 .enable_ms = sunhv_enable_ms, 382ab4382d2SGreg Kroah-Hartman .break_ctl = sunhv_break_ctl, 383ab4382d2SGreg Kroah-Hartman .startup = sunhv_startup, 384ab4382d2SGreg Kroah-Hartman .shutdown = sunhv_shutdown, 385ab4382d2SGreg Kroah-Hartman .set_termios = sunhv_set_termios, 386ab4382d2SGreg Kroah-Hartman .type = sunhv_type, 387ab4382d2SGreg Kroah-Hartman .release_port = sunhv_release_port, 388ab4382d2SGreg Kroah-Hartman .request_port = sunhv_request_port, 389ab4382d2SGreg Kroah-Hartman .config_port = sunhv_config_port, 390ab4382d2SGreg Kroah-Hartman .verify_port = sunhv_verify_port, 391ab4382d2SGreg Kroah-Hartman }; 392ab4382d2SGreg Kroah-Hartman 393ab4382d2SGreg Kroah-Hartman static struct uart_driver sunhv_reg = { 394ab4382d2SGreg Kroah-Hartman .owner = THIS_MODULE, 395ab4382d2SGreg Kroah-Hartman .driver_name = "sunhv", 396ab4382d2SGreg Kroah-Hartman .dev_name = "ttyS", 397ab4382d2SGreg Kroah-Hartman .major = TTY_MAJOR, 398ab4382d2SGreg Kroah-Hartman }; 399ab4382d2SGreg Kroah-Hartman 400ab4382d2SGreg Kroah-Hartman static struct uart_port *sunhv_port; 401ab4382d2SGreg Kroah-Hartman 402ab4382d2SGreg Kroah-Hartman /* Copy 's' into the con_write_page, decoding "\n" into 403ab4382d2SGreg Kroah-Hartman * "\r\n" along the way. We have to return two lengths 404ab4382d2SGreg Kroah-Hartman * because the caller needs to know how much to advance 405ab4382d2SGreg Kroah-Hartman * 's' and also how many bytes to output via con_write_page. 406ab4382d2SGreg Kroah-Hartman */ 407ab4382d2SGreg Kroah-Hartman static int fill_con_write_page(const char *s, unsigned int n, 408ab4382d2SGreg Kroah-Hartman unsigned long *page_bytes) 409ab4382d2SGreg Kroah-Hartman { 410ab4382d2SGreg Kroah-Hartman const char *orig_s = s; 411ab4382d2SGreg Kroah-Hartman char *p = con_write_page; 412ab4382d2SGreg Kroah-Hartman int left = PAGE_SIZE; 413ab4382d2SGreg Kroah-Hartman 414ab4382d2SGreg Kroah-Hartman while (n--) { 415ab4382d2SGreg Kroah-Hartman if (*s == '\n') { 416ab4382d2SGreg Kroah-Hartman if (left < 2) 417ab4382d2SGreg Kroah-Hartman break; 418ab4382d2SGreg Kroah-Hartman *p++ = '\r'; 419ab4382d2SGreg Kroah-Hartman left--; 420ab4382d2SGreg Kroah-Hartman } else if (left < 1) 421ab4382d2SGreg Kroah-Hartman break; 422ab4382d2SGreg Kroah-Hartman *p++ = *s++; 423ab4382d2SGreg Kroah-Hartman left--; 424ab4382d2SGreg Kroah-Hartman } 425ab4382d2SGreg Kroah-Hartman *page_bytes = p - con_write_page; 426ab4382d2SGreg Kroah-Hartman return s - orig_s; 427ab4382d2SGreg Kroah-Hartman } 428ab4382d2SGreg Kroah-Hartman 429ab4382d2SGreg Kroah-Hartman static void sunhv_console_write_paged(struct console *con, const char *s, unsigned n) 430ab4382d2SGreg Kroah-Hartman { 431ab4382d2SGreg Kroah-Hartman struct uart_port *port = sunhv_port; 432ab4382d2SGreg Kroah-Hartman unsigned long flags; 433ab4382d2SGreg Kroah-Hartman int locked = 1; 434ab4382d2SGreg Kroah-Hartman 435ab4382d2SGreg Kroah-Hartman local_irq_save(flags); 436ab4382d2SGreg Kroah-Hartman if (port->sysrq) { 437ab4382d2SGreg Kroah-Hartman locked = 0; 438ab4382d2SGreg Kroah-Hartman } else if (oops_in_progress) { 439ab4382d2SGreg Kroah-Hartman locked = spin_trylock(&port->lock); 440ab4382d2SGreg Kroah-Hartman } else 441ab4382d2SGreg Kroah-Hartman spin_lock(&port->lock); 442ab4382d2SGreg Kroah-Hartman 443ab4382d2SGreg Kroah-Hartman while (n > 0) { 444ab4382d2SGreg Kroah-Hartman unsigned long ra = __pa(con_write_page); 445ab4382d2SGreg Kroah-Hartman unsigned long page_bytes; 446ab4382d2SGreg Kroah-Hartman unsigned int cpy = fill_con_write_page(s, n, 447ab4382d2SGreg Kroah-Hartman &page_bytes); 448ab4382d2SGreg Kroah-Hartman 449ab4382d2SGreg Kroah-Hartman n -= cpy; 450ab4382d2SGreg Kroah-Hartman s += cpy; 451ab4382d2SGreg Kroah-Hartman while (page_bytes > 0) { 452ab4382d2SGreg Kroah-Hartman unsigned long written; 453ab4382d2SGreg Kroah-Hartman int limit = 1000000; 454ab4382d2SGreg Kroah-Hartman 455ab4382d2SGreg Kroah-Hartman while (limit--) { 456ab4382d2SGreg Kroah-Hartman unsigned long stat; 457ab4382d2SGreg Kroah-Hartman 458ab4382d2SGreg Kroah-Hartman stat = sun4v_con_write(ra, page_bytes, 459ab4382d2SGreg Kroah-Hartman &written); 460ab4382d2SGreg Kroah-Hartman if (stat == HV_EOK) 461ab4382d2SGreg Kroah-Hartman break; 462ab4382d2SGreg Kroah-Hartman udelay(1); 463ab4382d2SGreg Kroah-Hartman } 464ab4382d2SGreg Kroah-Hartman if (limit < 0) 465ab4382d2SGreg Kroah-Hartman break; 466ab4382d2SGreg Kroah-Hartman page_bytes -= written; 467ab4382d2SGreg Kroah-Hartman ra += written; 468ab4382d2SGreg Kroah-Hartman } 469ab4382d2SGreg Kroah-Hartman } 470ab4382d2SGreg Kroah-Hartman 471ab4382d2SGreg Kroah-Hartman if (locked) 472ab4382d2SGreg Kroah-Hartman spin_unlock(&port->lock); 473ab4382d2SGreg Kroah-Hartman local_irq_restore(flags); 474ab4382d2SGreg Kroah-Hartman } 475ab4382d2SGreg Kroah-Hartman 476ab4382d2SGreg Kroah-Hartman static inline void sunhv_console_putchar(struct uart_port *port, char c) 477ab4382d2SGreg Kroah-Hartman { 478ab4382d2SGreg Kroah-Hartman int limit = 1000000; 479ab4382d2SGreg Kroah-Hartman 480ab4382d2SGreg Kroah-Hartman while (limit-- > 0) { 481ab4382d2SGreg Kroah-Hartman long status = sun4v_con_putchar(c); 482ab4382d2SGreg Kroah-Hartman if (status == HV_EOK) 483ab4382d2SGreg Kroah-Hartman break; 484ab4382d2SGreg Kroah-Hartman udelay(1); 485ab4382d2SGreg Kroah-Hartman } 486ab4382d2SGreg Kroah-Hartman } 487ab4382d2SGreg Kroah-Hartman 488ab4382d2SGreg Kroah-Hartman static void sunhv_console_write_bychar(struct console *con, const char *s, unsigned n) 489ab4382d2SGreg Kroah-Hartman { 490ab4382d2SGreg Kroah-Hartman struct uart_port *port = sunhv_port; 491ab4382d2SGreg Kroah-Hartman unsigned long flags; 492ab4382d2SGreg Kroah-Hartman int i, locked = 1; 493ab4382d2SGreg Kroah-Hartman 494ab4382d2SGreg Kroah-Hartman local_irq_save(flags); 495ab4382d2SGreg Kroah-Hartman if (port->sysrq) { 496ab4382d2SGreg Kroah-Hartman locked = 0; 497ab4382d2SGreg Kroah-Hartman } else if (oops_in_progress) { 498ab4382d2SGreg Kroah-Hartman locked = spin_trylock(&port->lock); 499ab4382d2SGreg Kroah-Hartman } else 500ab4382d2SGreg Kroah-Hartman spin_lock(&port->lock); 501ab4382d2SGreg Kroah-Hartman 502ab4382d2SGreg Kroah-Hartman for (i = 0; i < n; i++) { 503ab4382d2SGreg Kroah-Hartman if (*s == '\n') 504ab4382d2SGreg Kroah-Hartman sunhv_console_putchar(port, '\r'); 505ab4382d2SGreg Kroah-Hartman sunhv_console_putchar(port, *s++); 506ab4382d2SGreg Kroah-Hartman } 507ab4382d2SGreg Kroah-Hartman 508ab4382d2SGreg Kroah-Hartman if (locked) 509ab4382d2SGreg Kroah-Hartman spin_unlock(&port->lock); 510ab4382d2SGreg Kroah-Hartman local_irq_restore(flags); 511ab4382d2SGreg Kroah-Hartman } 512ab4382d2SGreg Kroah-Hartman 513ab4382d2SGreg Kroah-Hartman static struct console sunhv_console = { 514ab4382d2SGreg Kroah-Hartman .name = "ttyHV", 515ab4382d2SGreg Kroah-Hartman .write = sunhv_console_write_bychar, 516ab4382d2SGreg Kroah-Hartman .device = uart_console_device, 517ab4382d2SGreg Kroah-Hartman .flags = CON_PRINTBUFFER, 518ab4382d2SGreg Kroah-Hartman .index = -1, 519ab4382d2SGreg Kroah-Hartman .data = &sunhv_reg, 520ab4382d2SGreg Kroah-Hartman }; 521ab4382d2SGreg Kroah-Hartman 5229671f099SBill Pemberton static int hv_probe(struct platform_device *op) 523ab4382d2SGreg Kroah-Hartman { 524ab4382d2SGreg Kroah-Hartman struct uart_port *port; 525ab4382d2SGreg Kroah-Hartman unsigned long minor; 526ab4382d2SGreg Kroah-Hartman int err; 527ab4382d2SGreg Kroah-Hartman 528ab4382d2SGreg Kroah-Hartman if (op->archdata.irqs[0] == 0xffffffff) 529ab4382d2SGreg Kroah-Hartman return -ENODEV; 530ab4382d2SGreg Kroah-Hartman 531ab4382d2SGreg Kroah-Hartman port = kzalloc(sizeof(struct uart_port), GFP_KERNEL); 532ab4382d2SGreg Kroah-Hartman if (unlikely(!port)) 533ab4382d2SGreg Kroah-Hartman return -ENOMEM; 534ab4382d2SGreg Kroah-Hartman 535ab4382d2SGreg Kroah-Hartman minor = 1; 536ab4382d2SGreg Kroah-Hartman if (sun4v_hvapi_register(HV_GRP_CORE, 1, &minor) == 0 && 537ab4382d2SGreg Kroah-Hartman minor >= 1) { 538ab4382d2SGreg Kroah-Hartman err = -ENOMEM; 539ab4382d2SGreg Kroah-Hartman con_write_page = kzalloc(PAGE_SIZE, GFP_KERNEL); 540ab4382d2SGreg Kroah-Hartman if (!con_write_page) 541ab4382d2SGreg Kroah-Hartman goto out_free_port; 542ab4382d2SGreg Kroah-Hartman 543ab4382d2SGreg Kroah-Hartman con_read_page = kzalloc(PAGE_SIZE, GFP_KERNEL); 544ab4382d2SGreg Kroah-Hartman if (!con_read_page) 545ab4382d2SGreg Kroah-Hartman goto out_free_con_write_page; 546ab4382d2SGreg Kroah-Hartman 547ab4382d2SGreg Kroah-Hartman sunhv_console.write = sunhv_console_write_paged; 548ab4382d2SGreg Kroah-Hartman sunhv_ops = &bywrite_ops; 549ab4382d2SGreg Kroah-Hartman } 550ab4382d2SGreg Kroah-Hartman 551ab4382d2SGreg Kroah-Hartman sunhv_port = port; 552ab4382d2SGreg Kroah-Hartman 553ab4382d2SGreg Kroah-Hartman port->line = 0; 554ab4382d2SGreg Kroah-Hartman port->ops = &sunhv_pops; 555ab4382d2SGreg Kroah-Hartman port->type = PORT_SUNHV; 556ab4382d2SGreg Kroah-Hartman port->uartclk = ( 29491200 / 16 ); /* arbitrary */ 557ab4382d2SGreg Kroah-Hartman 558ab4382d2SGreg Kroah-Hartman port->membase = (unsigned char __iomem *) __pa(port); 559ab4382d2SGreg Kroah-Hartman 560ab4382d2SGreg Kroah-Hartman port->irq = op->archdata.irqs[0]; 561ab4382d2SGreg Kroah-Hartman 562ab4382d2SGreg Kroah-Hartman port->dev = &op->dev; 563ab4382d2SGreg Kroah-Hartman 564ab4382d2SGreg Kroah-Hartman err = sunserial_register_minors(&sunhv_reg, 1); 565ab4382d2SGreg Kroah-Hartman if (err) 566ab4382d2SGreg Kroah-Hartman goto out_free_con_read_page; 567ab4382d2SGreg Kroah-Hartman 568ab4382d2SGreg Kroah-Hartman sunserial_console_match(&sunhv_console, op->dev.of_node, 569ab4382d2SGreg Kroah-Hartman &sunhv_reg, port->line, false); 570ab4382d2SGreg Kroah-Hartman 571ab4382d2SGreg Kroah-Hartman err = uart_add_one_port(&sunhv_reg, port); 572ab4382d2SGreg Kroah-Hartman if (err) 573ab4382d2SGreg Kroah-Hartman goto out_unregister_driver; 574ab4382d2SGreg Kroah-Hartman 575ab4382d2SGreg Kroah-Hartman err = request_irq(port->irq, sunhv_interrupt, 0, "hvcons", port); 576ab4382d2SGreg Kroah-Hartman if (err) 577ab4382d2SGreg Kroah-Hartman goto out_remove_port; 578ab4382d2SGreg Kroah-Hartman 579ab4382d2SGreg Kroah-Hartman dev_set_drvdata(&op->dev, port); 580ab4382d2SGreg Kroah-Hartman 581ab4382d2SGreg Kroah-Hartman return 0; 582ab4382d2SGreg Kroah-Hartman 583ab4382d2SGreg Kroah-Hartman out_remove_port: 584ab4382d2SGreg Kroah-Hartman uart_remove_one_port(&sunhv_reg, port); 585ab4382d2SGreg Kroah-Hartman 586ab4382d2SGreg Kroah-Hartman out_unregister_driver: 587ab4382d2SGreg Kroah-Hartman sunserial_unregister_minors(&sunhv_reg, 1); 588ab4382d2SGreg Kroah-Hartman 589ab4382d2SGreg Kroah-Hartman out_free_con_read_page: 590ab4382d2SGreg Kroah-Hartman kfree(con_read_page); 591ab4382d2SGreg Kroah-Hartman 592ab4382d2SGreg Kroah-Hartman out_free_con_write_page: 593ab4382d2SGreg Kroah-Hartman kfree(con_write_page); 594ab4382d2SGreg Kroah-Hartman 595ab4382d2SGreg Kroah-Hartman out_free_port: 596ab4382d2SGreg Kroah-Hartman kfree(port); 597ab4382d2SGreg Kroah-Hartman sunhv_port = NULL; 598ab4382d2SGreg Kroah-Hartman return err; 599ab4382d2SGreg Kroah-Hartman } 600ab4382d2SGreg Kroah-Hartman 601ae8d8a14SBill Pemberton static int hv_remove(struct platform_device *dev) 602ab4382d2SGreg Kroah-Hartman { 603ab4382d2SGreg Kroah-Hartman struct uart_port *port = dev_get_drvdata(&dev->dev); 604ab4382d2SGreg Kroah-Hartman 605ab4382d2SGreg Kroah-Hartman free_irq(port->irq, port); 606ab4382d2SGreg Kroah-Hartman 607ab4382d2SGreg Kroah-Hartman uart_remove_one_port(&sunhv_reg, port); 608ab4382d2SGreg Kroah-Hartman 609ab4382d2SGreg Kroah-Hartman sunserial_unregister_minors(&sunhv_reg, 1); 610ab4382d2SGreg Kroah-Hartman 611ab4382d2SGreg Kroah-Hartman kfree(port); 612ab4382d2SGreg Kroah-Hartman sunhv_port = NULL; 613ab4382d2SGreg Kroah-Hartman 614ab4382d2SGreg Kroah-Hartman dev_set_drvdata(&dev->dev, NULL); 615ab4382d2SGreg Kroah-Hartman 616ab4382d2SGreg Kroah-Hartman return 0; 617ab4382d2SGreg Kroah-Hartman } 618ab4382d2SGreg Kroah-Hartman 619ab4382d2SGreg Kroah-Hartman static const struct of_device_id hv_match[] = { 620ab4382d2SGreg Kroah-Hartman { 621ab4382d2SGreg Kroah-Hartman .name = "console", 622ab4382d2SGreg Kroah-Hartman .compatible = "qcn", 623ab4382d2SGreg Kroah-Hartman }, 624ab4382d2SGreg Kroah-Hartman { 625ab4382d2SGreg Kroah-Hartman .name = "console", 626ab4382d2SGreg Kroah-Hartman .compatible = "SUNW,sun4v-console", 627ab4382d2SGreg Kroah-Hartman }, 628ab4382d2SGreg Kroah-Hartman {}, 629ab4382d2SGreg Kroah-Hartman }; 630ab4382d2SGreg Kroah-Hartman MODULE_DEVICE_TABLE(of, hv_match); 631ab4382d2SGreg Kroah-Hartman 632793218dfSGrant Likely static struct platform_driver hv_driver = { 633ab4382d2SGreg Kroah-Hartman .driver = { 634ab4382d2SGreg Kroah-Hartman .name = "hv", 635ab4382d2SGreg Kroah-Hartman .owner = THIS_MODULE, 636ab4382d2SGreg Kroah-Hartman .of_match_table = hv_match, 637ab4382d2SGreg Kroah-Hartman }, 638ab4382d2SGreg Kroah-Hartman .probe = hv_probe, 6392d47b716SBill Pemberton .remove = hv_remove, 640ab4382d2SGreg Kroah-Hartman }; 641ab4382d2SGreg Kroah-Hartman 642ab4382d2SGreg Kroah-Hartman static int __init sunhv_init(void) 643ab4382d2SGreg Kroah-Hartman { 644ab4382d2SGreg Kroah-Hartman if (tlb_type != hypervisor) 645ab4382d2SGreg Kroah-Hartman return -ENODEV; 646ab4382d2SGreg Kroah-Hartman 647793218dfSGrant Likely return platform_driver_register(&hv_driver); 648ab4382d2SGreg Kroah-Hartman } 649ab4382d2SGreg Kroah-Hartman 650ab4382d2SGreg Kroah-Hartman static void __exit sunhv_exit(void) 651ab4382d2SGreg Kroah-Hartman { 652793218dfSGrant Likely platform_driver_unregister(&hv_driver); 653ab4382d2SGreg Kroah-Hartman } 654ab4382d2SGreg Kroah-Hartman 655ab4382d2SGreg Kroah-Hartman module_init(sunhv_init); 656ab4382d2SGreg Kroah-Hartman module_exit(sunhv_exit); 657ab4382d2SGreg Kroah-Hartman 658ab4382d2SGreg Kroah-Hartman MODULE_AUTHOR("David S. Miller"); 659ab4382d2SGreg Kroah-Hartman MODULE_DESCRIPTION("SUN4V Hypervisor console driver"); 660ab4382d2SGreg Kroah-Hartman MODULE_VERSION("2.0"); 661ab4382d2SGreg Kroah-Hartman MODULE_LICENSE("GPL"); 662