1e3b3d0f5SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0 2ab4382d2SGreg Kroah-Hartman /* sunhv.c: Serial driver for SUN4V hypervisor console. 3ab4382d2SGreg Kroah-Hartman * 4ab4382d2SGreg Kroah-Hartman * Copyright (C) 2006, 2007 David S. Miller (davem@davemloft.net) 5ab4382d2SGreg Kroah-Hartman */ 6ab4382d2SGreg Kroah-Hartman 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 #include <linux/serial_core.h> 296816383aSPaul Gortmaker #include <linux/sunserialcore.h> 30ab4382d2SGreg Kroah-Hartman 31ab4382d2SGreg Kroah-Hartman #define CON_BREAK ((long)-1) 32ab4382d2SGreg Kroah-Hartman #define CON_HUP ((long)-2) 33ab4382d2SGreg Kroah-Hartman 34ab4382d2SGreg Kroah-Hartman #define IGNORE_BREAK 0x1 35ab4382d2SGreg Kroah-Hartman #define IGNORE_ALL 0x2 36ab4382d2SGreg Kroah-Hartman 37ab4382d2SGreg Kroah-Hartman static char *con_write_page; 38ab4382d2SGreg Kroah-Hartman static char *con_read_page; 39ab4382d2SGreg Kroah-Hartman 40ab4382d2SGreg Kroah-Hartman static int hung_up = 0; 41ab4382d2SGreg Kroah-Hartman 42ab4382d2SGreg Kroah-Hartman static void transmit_chars_putchar(struct uart_port *port, struct circ_buf *xmit) 43ab4382d2SGreg Kroah-Hartman { 44ab4382d2SGreg Kroah-Hartman while (!uart_circ_empty(xmit)) { 45ab4382d2SGreg Kroah-Hartman long status = sun4v_con_putchar(xmit->buf[xmit->tail]); 46ab4382d2SGreg Kroah-Hartman 47ab4382d2SGreg Kroah-Hartman if (status != HV_EOK) 48ab4382d2SGreg Kroah-Hartman break; 49ab4382d2SGreg Kroah-Hartman 50ab4382d2SGreg Kroah-Hartman xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1); 51ab4382d2SGreg Kroah-Hartman port->icount.tx++; 52ab4382d2SGreg Kroah-Hartman } 53ab4382d2SGreg Kroah-Hartman } 54ab4382d2SGreg Kroah-Hartman 55ab4382d2SGreg Kroah-Hartman static void transmit_chars_write(struct uart_port *port, struct circ_buf *xmit) 56ab4382d2SGreg Kroah-Hartman { 57ab4382d2SGreg Kroah-Hartman while (!uart_circ_empty(xmit)) { 58ab4382d2SGreg Kroah-Hartman unsigned long ra = __pa(xmit->buf + xmit->tail); 59ab4382d2SGreg Kroah-Hartman unsigned long len, status, sent; 60ab4382d2SGreg Kroah-Hartman 61ab4382d2SGreg Kroah-Hartman len = CIRC_CNT_TO_END(xmit->head, xmit->tail, 62ab4382d2SGreg Kroah-Hartman UART_XMIT_SIZE); 63ab4382d2SGreg Kroah-Hartman status = sun4v_con_write(ra, len, &sent); 64ab4382d2SGreg Kroah-Hartman if (status != HV_EOK) 65ab4382d2SGreg Kroah-Hartman break; 66ab4382d2SGreg Kroah-Hartman xmit->tail = (xmit->tail + sent) & (UART_XMIT_SIZE - 1); 67ab4382d2SGreg Kroah-Hartman port->icount.tx += sent; 68ab4382d2SGreg Kroah-Hartman } 69ab4382d2SGreg Kroah-Hartman } 70ab4382d2SGreg Kroah-Hartman 7105c7cd39SJiri Slaby static int receive_chars_getchar(struct uart_port *port) 72ab4382d2SGreg Kroah-Hartman { 73ab4382d2SGreg Kroah-Hartman int saw_console_brk = 0; 74ab4382d2SGreg Kroah-Hartman int limit = 10000; 75ab4382d2SGreg Kroah-Hartman 76ab4382d2SGreg Kroah-Hartman while (limit-- > 0) { 77ab4382d2SGreg Kroah-Hartman long status; 78ab4382d2SGreg Kroah-Hartman long c = sun4v_con_getchar(&status); 79ab4382d2SGreg Kroah-Hartman 80ab4382d2SGreg Kroah-Hartman if (status == HV_EWOULDBLOCK) 81ab4382d2SGreg Kroah-Hartman break; 82ab4382d2SGreg Kroah-Hartman 83ab4382d2SGreg Kroah-Hartman if (c == CON_BREAK) { 84ab4382d2SGreg Kroah-Hartman if (uart_handle_break(port)) 85ab4382d2SGreg Kroah-Hartman continue; 86ab4382d2SGreg Kroah-Hartman saw_console_brk = 1; 87ab4382d2SGreg Kroah-Hartman c = 0; 88ab4382d2SGreg Kroah-Hartman } 89ab4382d2SGreg Kroah-Hartman 90ab4382d2SGreg Kroah-Hartman if (c == CON_HUP) { 91ab4382d2SGreg Kroah-Hartman hung_up = 1; 92ab4382d2SGreg Kroah-Hartman uart_handle_dcd_change(port, 0); 93ab4382d2SGreg Kroah-Hartman } else if (hung_up) { 94ab4382d2SGreg Kroah-Hartman hung_up = 0; 95ab4382d2SGreg Kroah-Hartman uart_handle_dcd_change(port, 1); 96ab4382d2SGreg Kroah-Hartman } 97ab4382d2SGreg Kroah-Hartman 9892a19f9cSJiri Slaby if (port->state == NULL) { 99ab4382d2SGreg Kroah-Hartman uart_handle_sysrq_char(port, c); 100ab4382d2SGreg Kroah-Hartman continue; 101ab4382d2SGreg Kroah-Hartman } 102ab4382d2SGreg Kroah-Hartman 103ab4382d2SGreg Kroah-Hartman port->icount.rx++; 104ab4382d2SGreg Kroah-Hartman 105ab4382d2SGreg Kroah-Hartman if (uart_handle_sysrq_char(port, c)) 106ab4382d2SGreg Kroah-Hartman continue; 107ab4382d2SGreg Kroah-Hartman 10892a19f9cSJiri Slaby tty_insert_flip_char(&port->state->port, c, TTY_NORMAL); 109ab4382d2SGreg Kroah-Hartman } 110ab4382d2SGreg Kroah-Hartman 111ab4382d2SGreg Kroah-Hartman return saw_console_brk; 112ab4382d2SGreg Kroah-Hartman } 113ab4382d2SGreg Kroah-Hartman 11405c7cd39SJiri Slaby static int receive_chars_read(struct uart_port *port) 115ab4382d2SGreg Kroah-Hartman { 1167db60d05SVijay Kumar static int saw_console_brk; 117ab4382d2SGreg Kroah-Hartman int limit = 10000; 118ab4382d2SGreg Kroah-Hartman 119ab4382d2SGreg Kroah-Hartman while (limit-- > 0) { 120ab4382d2SGreg Kroah-Hartman unsigned long ra = __pa(con_read_page); 121ab4382d2SGreg Kroah-Hartman unsigned long bytes_read, i; 122ab4382d2SGreg Kroah-Hartman long stat = sun4v_con_read(ra, PAGE_SIZE, &bytes_read); 123ab4382d2SGreg Kroah-Hartman 124ab4382d2SGreg Kroah-Hartman if (stat != HV_EOK) { 125ab4382d2SGreg Kroah-Hartman bytes_read = 0; 126ab4382d2SGreg Kroah-Hartman 127ab4382d2SGreg Kroah-Hartman if (stat == CON_BREAK) { 1287db60d05SVijay Kumar if (saw_console_brk) 1297db60d05SVijay Kumar sun_do_break(); 1307db60d05SVijay Kumar 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]); 1547db60d05SVijay Kumar saw_console_brk = 0; 155079317a6SVijay Kumar } 156ab4382d2SGreg Kroah-Hartman 15705c7cd39SJiri Slaby if (port->state == NULL) 158ab4382d2SGreg Kroah-Hartman continue; 159ab4382d2SGreg Kroah-Hartman 160ab4382d2SGreg Kroah-Hartman port->icount.rx += bytes_read; 161ab4382d2SGreg Kroah-Hartman 16205c7cd39SJiri Slaby tty_insert_flip_string(&port->state->port, con_read_page, 16305c7cd39SJiri Slaby bytes_read); 164ab4382d2SGreg Kroah-Hartman } 165ab4382d2SGreg Kroah-Hartman 166ab4382d2SGreg Kroah-Hartman return saw_console_brk; 167ab4382d2SGreg Kroah-Hartman } 168ab4382d2SGreg Kroah-Hartman 169ab4382d2SGreg Kroah-Hartman struct sunhv_ops { 170ab4382d2SGreg Kroah-Hartman void (*transmit_chars)(struct uart_port *port, struct circ_buf *xmit); 17105c7cd39SJiri Slaby int (*receive_chars)(struct uart_port *port); 172ab4382d2SGreg Kroah-Hartman }; 173ab4382d2SGreg Kroah-Hartman 17401fd3c27SAya Mahfouz static const struct sunhv_ops bychar_ops = { 175ab4382d2SGreg Kroah-Hartman .transmit_chars = transmit_chars_putchar, 176ab4382d2SGreg Kroah-Hartman .receive_chars = receive_chars_getchar, 177ab4382d2SGreg Kroah-Hartman }; 178ab4382d2SGreg Kroah-Hartman 17901fd3c27SAya Mahfouz static const struct sunhv_ops bywrite_ops = { 180ab4382d2SGreg Kroah-Hartman .transmit_chars = transmit_chars_write, 181ab4382d2SGreg Kroah-Hartman .receive_chars = receive_chars_read, 182ab4382d2SGreg Kroah-Hartman }; 183ab4382d2SGreg Kroah-Hartman 18401fd3c27SAya Mahfouz static const struct sunhv_ops *sunhv_ops = &bychar_ops; 185ab4382d2SGreg Kroah-Hartman 1862e124b4aSJiri Slaby static struct tty_port *receive_chars(struct uart_port *port) 187ab4382d2SGreg Kroah-Hartman { 1882e124b4aSJiri Slaby struct tty_port *tport = NULL; 189ab4382d2SGreg Kroah-Hartman 190ab4382d2SGreg Kroah-Hartman if (port->state != NULL) /* Unopened serial console */ 1912e124b4aSJiri Slaby tport = &port->state->port; 192ab4382d2SGreg Kroah-Hartman 19305c7cd39SJiri Slaby if (sunhv_ops->receive_chars(port)) 194ab4382d2SGreg Kroah-Hartman sun_do_break(); 195ab4382d2SGreg Kroah-Hartman 1962e124b4aSJiri Slaby return tport; 197ab4382d2SGreg Kroah-Hartman } 198ab4382d2SGreg Kroah-Hartman 199ab4382d2SGreg Kroah-Hartman static void transmit_chars(struct uart_port *port) 200ab4382d2SGreg Kroah-Hartman { 201ab4382d2SGreg Kroah-Hartman struct circ_buf *xmit; 202ab4382d2SGreg Kroah-Hartman 203ab4382d2SGreg Kroah-Hartman if (!port->state) 204ab4382d2SGreg Kroah-Hartman return; 205ab4382d2SGreg Kroah-Hartman 206ab4382d2SGreg Kroah-Hartman xmit = &port->state->xmit; 207ab4382d2SGreg Kroah-Hartman if (uart_circ_empty(xmit) || uart_tx_stopped(port)) 208ab4382d2SGreg Kroah-Hartman return; 209ab4382d2SGreg Kroah-Hartman 210ab4382d2SGreg Kroah-Hartman sunhv_ops->transmit_chars(port, xmit); 211ab4382d2SGreg Kroah-Hartman 212ab4382d2SGreg Kroah-Hartman if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS) 213ab4382d2SGreg Kroah-Hartman uart_write_wakeup(port); 214ab4382d2SGreg Kroah-Hartman } 215ab4382d2SGreg Kroah-Hartman 216ab4382d2SGreg Kroah-Hartman static irqreturn_t sunhv_interrupt(int irq, void *dev_id) 217ab4382d2SGreg Kroah-Hartman { 218ab4382d2SGreg Kroah-Hartman struct uart_port *port = dev_id; 2192e124b4aSJiri Slaby struct tty_port *tport; 220ab4382d2SGreg Kroah-Hartman unsigned long flags; 221ab4382d2SGreg Kroah-Hartman 222ab4382d2SGreg Kroah-Hartman spin_lock_irqsave(&port->lock, flags); 2232e124b4aSJiri Slaby tport = receive_chars(port); 224ab4382d2SGreg Kroah-Hartman transmit_chars(port); 225ab4382d2SGreg Kroah-Hartman spin_unlock_irqrestore(&port->lock, flags); 226ab4382d2SGreg Kroah-Hartman 2272e124b4aSJiri Slaby if (tport) 2282e124b4aSJiri Slaby tty_flip_buffer_push(tport); 229ab4382d2SGreg Kroah-Hartman 230ab4382d2SGreg Kroah-Hartman return IRQ_HANDLED; 231ab4382d2SGreg Kroah-Hartman } 232ab4382d2SGreg Kroah-Hartman 233ab4382d2SGreg Kroah-Hartman /* port->lock is not held. */ 234ab4382d2SGreg Kroah-Hartman static unsigned int sunhv_tx_empty(struct uart_port *port) 235ab4382d2SGreg Kroah-Hartman { 236ab4382d2SGreg Kroah-Hartman /* Transmitter is always empty for us. If the circ buffer 237ab4382d2SGreg Kroah-Hartman * is non-empty or there is an x_char pending, our caller 238ab4382d2SGreg Kroah-Hartman * will do the right thing and ignore what we return here. 239ab4382d2SGreg Kroah-Hartman */ 240ab4382d2SGreg Kroah-Hartman return TIOCSER_TEMT; 241ab4382d2SGreg Kroah-Hartman } 242ab4382d2SGreg Kroah-Hartman 243ab4382d2SGreg Kroah-Hartman /* port->lock held by caller. */ 244ab4382d2SGreg Kroah-Hartman static void sunhv_set_mctrl(struct uart_port *port, unsigned int mctrl) 245ab4382d2SGreg Kroah-Hartman { 246ab4382d2SGreg Kroah-Hartman return; 247ab4382d2SGreg Kroah-Hartman } 248ab4382d2SGreg Kroah-Hartman 249ab4382d2SGreg Kroah-Hartman /* port->lock is held by caller and interrupts are disabled. */ 250ab4382d2SGreg Kroah-Hartman static unsigned int sunhv_get_mctrl(struct uart_port *port) 251ab4382d2SGreg Kroah-Hartman { 252ab4382d2SGreg Kroah-Hartman return TIOCM_DSR | TIOCM_CAR | TIOCM_CTS; 253ab4382d2SGreg Kroah-Hartman } 254ab4382d2SGreg Kroah-Hartman 255ab4382d2SGreg Kroah-Hartman /* port->lock held by caller. */ 256ab4382d2SGreg Kroah-Hartman static void sunhv_stop_tx(struct uart_port *port) 257ab4382d2SGreg Kroah-Hartman { 258ab4382d2SGreg Kroah-Hartman return; 259ab4382d2SGreg Kroah-Hartman } 260ab4382d2SGreg Kroah-Hartman 261ab4382d2SGreg Kroah-Hartman /* port->lock held by caller. */ 262ab4382d2SGreg Kroah-Hartman static void sunhv_start_tx(struct uart_port *port) 263ab4382d2SGreg Kroah-Hartman { 264ab4382d2SGreg Kroah-Hartman transmit_chars(port); 265ab4382d2SGreg Kroah-Hartman } 266ab4382d2SGreg Kroah-Hartman 267ab4382d2SGreg Kroah-Hartman /* port->lock is not held. */ 268ab4382d2SGreg Kroah-Hartman static void sunhv_send_xchar(struct uart_port *port, char ch) 269ab4382d2SGreg Kroah-Hartman { 270ab4382d2SGreg Kroah-Hartman unsigned long flags; 271ab4382d2SGreg Kroah-Hartman int limit = 10000; 272ab4382d2SGreg Kroah-Hartman 273db106df3SPeter Hurley if (ch == __DISABLED_CHAR) 274db106df3SPeter Hurley return; 275db106df3SPeter Hurley 276ab4382d2SGreg Kroah-Hartman spin_lock_irqsave(&port->lock, flags); 277ab4382d2SGreg Kroah-Hartman 278ab4382d2SGreg Kroah-Hartman while (limit-- > 0) { 279ab4382d2SGreg Kroah-Hartman long status = sun4v_con_putchar(ch); 280ab4382d2SGreg Kroah-Hartman if (status == HV_EOK) 281ab4382d2SGreg Kroah-Hartman break; 282ab4382d2SGreg Kroah-Hartman udelay(1); 283ab4382d2SGreg Kroah-Hartman } 284ab4382d2SGreg Kroah-Hartman 285ab4382d2SGreg Kroah-Hartman spin_unlock_irqrestore(&port->lock, flags); 286ab4382d2SGreg Kroah-Hartman } 287ab4382d2SGreg Kroah-Hartman 288ab4382d2SGreg Kroah-Hartman /* port->lock held by caller. */ 289ab4382d2SGreg Kroah-Hartman static void sunhv_stop_rx(struct uart_port *port) 290ab4382d2SGreg Kroah-Hartman { 291ab4382d2SGreg Kroah-Hartman } 292ab4382d2SGreg Kroah-Hartman 293ab4382d2SGreg Kroah-Hartman /* port->lock is not held. */ 294ab4382d2SGreg Kroah-Hartman static void sunhv_break_ctl(struct uart_port *port, int break_state) 295ab4382d2SGreg Kroah-Hartman { 296ab4382d2SGreg Kroah-Hartman if (break_state) { 297ab4382d2SGreg Kroah-Hartman unsigned long flags; 298ab4382d2SGreg Kroah-Hartman int limit = 10000; 299ab4382d2SGreg Kroah-Hartman 300ab4382d2SGreg Kroah-Hartman spin_lock_irqsave(&port->lock, flags); 301ab4382d2SGreg Kroah-Hartman 302ab4382d2SGreg Kroah-Hartman while (limit-- > 0) { 303ab4382d2SGreg Kroah-Hartman long status = sun4v_con_putchar(CON_BREAK); 304ab4382d2SGreg Kroah-Hartman if (status == HV_EOK) 305ab4382d2SGreg Kroah-Hartman break; 306ab4382d2SGreg Kroah-Hartman udelay(1); 307ab4382d2SGreg Kroah-Hartman } 308ab4382d2SGreg Kroah-Hartman 309ab4382d2SGreg Kroah-Hartman spin_unlock_irqrestore(&port->lock, flags); 310ab4382d2SGreg Kroah-Hartman } 311ab4382d2SGreg Kroah-Hartman } 312ab4382d2SGreg Kroah-Hartman 313ab4382d2SGreg Kroah-Hartman /* port->lock is not held. */ 314ab4382d2SGreg Kroah-Hartman static int sunhv_startup(struct uart_port *port) 315ab4382d2SGreg Kroah-Hartman { 316ab4382d2SGreg Kroah-Hartman return 0; 317ab4382d2SGreg Kroah-Hartman } 318ab4382d2SGreg Kroah-Hartman 319ab4382d2SGreg Kroah-Hartman /* port->lock is not held. */ 320ab4382d2SGreg Kroah-Hartman static void sunhv_shutdown(struct uart_port *port) 321ab4382d2SGreg Kroah-Hartman { 322ab4382d2SGreg Kroah-Hartman } 323ab4382d2SGreg Kroah-Hartman 324ab4382d2SGreg Kroah-Hartman /* port->lock is not held. */ 325ab4382d2SGreg Kroah-Hartman static void sunhv_set_termios(struct uart_port *port, struct ktermios *termios, 326ab4382d2SGreg Kroah-Hartman struct ktermios *old) 327ab4382d2SGreg Kroah-Hartman { 328ab4382d2SGreg Kroah-Hartman unsigned int baud = uart_get_baud_rate(port, termios, old, 0, 4000000); 329ab4382d2SGreg Kroah-Hartman unsigned int quot = uart_get_divisor(port, baud); 330ab4382d2SGreg Kroah-Hartman unsigned int iflag, cflag; 331ab4382d2SGreg Kroah-Hartman unsigned long flags; 332ab4382d2SGreg Kroah-Hartman 333ab4382d2SGreg Kroah-Hartman spin_lock_irqsave(&port->lock, flags); 334ab4382d2SGreg Kroah-Hartman 335ab4382d2SGreg Kroah-Hartman iflag = termios->c_iflag; 336ab4382d2SGreg Kroah-Hartman cflag = termios->c_cflag; 337ab4382d2SGreg Kroah-Hartman 338ab4382d2SGreg Kroah-Hartman port->ignore_status_mask = 0; 339ab4382d2SGreg Kroah-Hartman if (iflag & IGNBRK) 340ab4382d2SGreg Kroah-Hartman port->ignore_status_mask |= IGNORE_BREAK; 341ab4382d2SGreg Kroah-Hartman if ((cflag & CREAD) == 0) 342ab4382d2SGreg Kroah-Hartman port->ignore_status_mask |= IGNORE_ALL; 343ab4382d2SGreg Kroah-Hartman 344ab4382d2SGreg Kroah-Hartman /* XXX */ 345ab4382d2SGreg Kroah-Hartman uart_update_timeout(port, cflag, 346ab4382d2SGreg Kroah-Hartman (port->uartclk / (16 * quot))); 347ab4382d2SGreg Kroah-Hartman 348ab4382d2SGreg Kroah-Hartman spin_unlock_irqrestore(&port->lock, flags); 349ab4382d2SGreg Kroah-Hartman } 350ab4382d2SGreg Kroah-Hartman 351ab4382d2SGreg Kroah-Hartman static const char *sunhv_type(struct uart_port *port) 352ab4382d2SGreg Kroah-Hartman { 353ab4382d2SGreg Kroah-Hartman return "SUN4V HCONS"; 354ab4382d2SGreg Kroah-Hartman } 355ab4382d2SGreg Kroah-Hartman 356ab4382d2SGreg Kroah-Hartman static void sunhv_release_port(struct uart_port *port) 357ab4382d2SGreg Kroah-Hartman { 358ab4382d2SGreg Kroah-Hartman } 359ab4382d2SGreg Kroah-Hartman 360ab4382d2SGreg Kroah-Hartman static int sunhv_request_port(struct uart_port *port) 361ab4382d2SGreg Kroah-Hartman { 362ab4382d2SGreg Kroah-Hartman return 0; 363ab4382d2SGreg Kroah-Hartman } 364ab4382d2SGreg Kroah-Hartman 365ab4382d2SGreg Kroah-Hartman static void sunhv_config_port(struct uart_port *port, int flags) 366ab4382d2SGreg Kroah-Hartman { 367ab4382d2SGreg Kroah-Hartman } 368ab4382d2SGreg Kroah-Hartman 369ab4382d2SGreg Kroah-Hartman static int sunhv_verify_port(struct uart_port *port, struct serial_struct *ser) 370ab4382d2SGreg Kroah-Hartman { 371ab4382d2SGreg Kroah-Hartman return -EINVAL; 372ab4382d2SGreg Kroah-Hartman } 373ab4382d2SGreg Kroah-Hartman 3742331e068SBhumika Goyal static const struct uart_ops sunhv_pops = { 375ab4382d2SGreg Kroah-Hartman .tx_empty = sunhv_tx_empty, 376ab4382d2SGreg Kroah-Hartman .set_mctrl = sunhv_set_mctrl, 377ab4382d2SGreg Kroah-Hartman .get_mctrl = sunhv_get_mctrl, 378ab4382d2SGreg Kroah-Hartman .stop_tx = sunhv_stop_tx, 379ab4382d2SGreg Kroah-Hartman .start_tx = sunhv_start_tx, 380ab4382d2SGreg Kroah-Hartman .send_xchar = sunhv_send_xchar, 381ab4382d2SGreg Kroah-Hartman .stop_rx = sunhv_stop_rx, 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", 39607a6d63eSJohn Paul Adrian Glaubitz .dev_name = "ttyHV", 397ab4382d2SGreg Kroah-Hartman .major = TTY_MAJOR, 398ab4382d2SGreg Kroah-Hartman }; 399ab4382d2SGreg Kroah-Hartman 400ab4382d2SGreg Kroah-Hartman static struct uart_port *sunhv_port; 401ab4382d2SGreg Kroah-Hartman 4027dd4fcf5SVijay Kumar void sunhv_migrate_hvcons_irq(int cpu) 4037dd4fcf5SVijay Kumar { 4047dd4fcf5SVijay Kumar /* Migrate hvcons irq to param cpu */ 4057dd4fcf5SVijay Kumar irq_force_affinity(sunhv_port->irq, cpumask_of(cpu)); 4067dd4fcf5SVijay Kumar } 4077dd4fcf5SVijay Kumar 408ab4382d2SGreg Kroah-Hartman /* Copy 's' into the con_write_page, decoding "\n" into 409ab4382d2SGreg Kroah-Hartman * "\r\n" along the way. We have to return two lengths 410ab4382d2SGreg Kroah-Hartman * because the caller needs to know how much to advance 411ab4382d2SGreg Kroah-Hartman * 's' and also how many bytes to output via con_write_page. 412ab4382d2SGreg Kroah-Hartman */ 413ab4382d2SGreg Kroah-Hartman static int fill_con_write_page(const char *s, unsigned int n, 414ab4382d2SGreg Kroah-Hartman unsigned long *page_bytes) 415ab4382d2SGreg Kroah-Hartman { 416ab4382d2SGreg Kroah-Hartman const char *orig_s = s; 417ab4382d2SGreg Kroah-Hartman char *p = con_write_page; 418ab4382d2SGreg Kroah-Hartman int left = PAGE_SIZE; 419ab4382d2SGreg Kroah-Hartman 420ab4382d2SGreg Kroah-Hartman while (n--) { 421ab4382d2SGreg Kroah-Hartman if (*s == '\n') { 422ab4382d2SGreg Kroah-Hartman if (left < 2) 423ab4382d2SGreg Kroah-Hartman break; 424ab4382d2SGreg Kroah-Hartman *p++ = '\r'; 425ab4382d2SGreg Kroah-Hartman left--; 426ab4382d2SGreg Kroah-Hartman } else if (left < 1) 427ab4382d2SGreg Kroah-Hartman break; 428ab4382d2SGreg Kroah-Hartman *p++ = *s++; 429ab4382d2SGreg Kroah-Hartman left--; 430ab4382d2SGreg Kroah-Hartman } 431ab4382d2SGreg Kroah-Hartman *page_bytes = p - con_write_page; 432ab4382d2SGreg Kroah-Hartman return s - orig_s; 433ab4382d2SGreg Kroah-Hartman } 434ab4382d2SGreg Kroah-Hartman 435ab4382d2SGreg Kroah-Hartman static void sunhv_console_write_paged(struct console *con, const char *s, unsigned n) 436ab4382d2SGreg Kroah-Hartman { 437ab4382d2SGreg Kroah-Hartman struct uart_port *port = sunhv_port; 438ab4382d2SGreg Kroah-Hartman unsigned long flags; 439ab4382d2SGreg Kroah-Hartman int locked = 1; 440ab4382d2SGreg Kroah-Hartman 441e58e241cSDavid Miller if (port->sysrq || oops_in_progress) 442e58e241cSDavid Miller locked = spin_trylock_irqsave(&port->lock, flags); 443e58e241cSDavid Miller else 444e58e241cSDavid Miller spin_lock_irqsave(&port->lock, flags); 445ab4382d2SGreg Kroah-Hartman 446ab4382d2SGreg Kroah-Hartman while (n > 0) { 447ab4382d2SGreg Kroah-Hartman unsigned long ra = __pa(con_write_page); 448ab4382d2SGreg Kroah-Hartman unsigned long page_bytes; 449ab4382d2SGreg Kroah-Hartman unsigned int cpy = fill_con_write_page(s, n, 450ab4382d2SGreg Kroah-Hartman &page_bytes); 451ab4382d2SGreg Kroah-Hartman 452ab4382d2SGreg Kroah-Hartman n -= cpy; 453ab4382d2SGreg Kroah-Hartman s += cpy; 454ab4382d2SGreg Kroah-Hartman while (page_bytes > 0) { 455ab4382d2SGreg Kroah-Hartman unsigned long written; 456ab4382d2SGreg Kroah-Hartman int limit = 1000000; 457ab4382d2SGreg Kroah-Hartman 458ab4382d2SGreg Kroah-Hartman while (limit--) { 459ab4382d2SGreg Kroah-Hartman unsigned long stat; 460ab4382d2SGreg Kroah-Hartman 461ab4382d2SGreg Kroah-Hartman stat = sun4v_con_write(ra, page_bytes, 462ab4382d2SGreg Kroah-Hartman &written); 463ab4382d2SGreg Kroah-Hartman if (stat == HV_EOK) 464ab4382d2SGreg Kroah-Hartman break; 465ab4382d2SGreg Kroah-Hartman udelay(1); 466ab4382d2SGreg Kroah-Hartman } 467ab4382d2SGreg Kroah-Hartman if (limit < 0) 468ab4382d2SGreg Kroah-Hartman break; 469ab4382d2SGreg Kroah-Hartman page_bytes -= written; 470ab4382d2SGreg Kroah-Hartman ra += written; 471ab4382d2SGreg Kroah-Hartman } 472ab4382d2SGreg Kroah-Hartman } 473ab4382d2SGreg Kroah-Hartman 474ab4382d2SGreg Kroah-Hartman if (locked) 475e58e241cSDavid Miller spin_unlock_irqrestore(&port->lock, flags); 476ab4382d2SGreg Kroah-Hartman } 477ab4382d2SGreg Kroah-Hartman 478ab4382d2SGreg Kroah-Hartman static inline void sunhv_console_putchar(struct uart_port *port, char c) 479ab4382d2SGreg Kroah-Hartman { 480ab4382d2SGreg Kroah-Hartman int limit = 1000000; 481ab4382d2SGreg Kroah-Hartman 482ab4382d2SGreg Kroah-Hartman while (limit-- > 0) { 483ab4382d2SGreg Kroah-Hartman long status = sun4v_con_putchar(c); 484ab4382d2SGreg Kroah-Hartman if (status == HV_EOK) 485ab4382d2SGreg Kroah-Hartman break; 486ab4382d2SGreg Kroah-Hartman udelay(1); 487ab4382d2SGreg Kroah-Hartman } 488ab4382d2SGreg Kroah-Hartman } 489ab4382d2SGreg Kroah-Hartman 490ab4382d2SGreg Kroah-Hartman static void sunhv_console_write_bychar(struct console *con, const char *s, unsigned n) 491ab4382d2SGreg Kroah-Hartman { 492ab4382d2SGreg Kroah-Hartman struct uart_port *port = sunhv_port; 493ab4382d2SGreg Kroah-Hartman unsigned long flags; 494ab4382d2SGreg Kroah-Hartman int i, locked = 1; 495ab4382d2SGreg Kroah-Hartman 496e58e241cSDavid Miller if (port->sysrq || oops_in_progress) 497e58e241cSDavid Miller locked = spin_trylock_irqsave(&port->lock, flags); 498e58e241cSDavid Miller else 499e58e241cSDavid Miller spin_lock_irqsave(&port->lock, flags); 500ab4382d2SGreg Kroah-Hartman 501ab4382d2SGreg Kroah-Hartman for (i = 0; i < n; i++) { 502ab4382d2SGreg Kroah-Hartman if (*s == '\n') 503ab4382d2SGreg Kroah-Hartman sunhv_console_putchar(port, '\r'); 504ab4382d2SGreg Kroah-Hartman sunhv_console_putchar(port, *s++); 505ab4382d2SGreg Kroah-Hartman } 506ab4382d2SGreg Kroah-Hartman 507ab4382d2SGreg Kroah-Hartman if (locked) 508e58e241cSDavid Miller spin_unlock_irqrestore(&port->lock, flags); 509ab4382d2SGreg Kroah-Hartman } 510ab4382d2SGreg Kroah-Hartman 511ab4382d2SGreg Kroah-Hartman static struct console sunhv_console = { 512ab4382d2SGreg Kroah-Hartman .name = "ttyHV", 513ab4382d2SGreg Kroah-Hartman .write = sunhv_console_write_bychar, 514ab4382d2SGreg Kroah-Hartman .device = uart_console_device, 515ab4382d2SGreg Kroah-Hartman .flags = CON_PRINTBUFFER, 516ab4382d2SGreg Kroah-Hartman .index = -1, 517ab4382d2SGreg Kroah-Hartman .data = &sunhv_reg, 518ab4382d2SGreg Kroah-Hartman }; 519ab4382d2SGreg Kroah-Hartman 5209671f099SBill Pemberton static int hv_probe(struct platform_device *op) 521ab4382d2SGreg Kroah-Hartman { 522ab4382d2SGreg Kroah-Hartman struct uart_port *port; 523ab4382d2SGreg Kroah-Hartman unsigned long minor; 524ab4382d2SGreg Kroah-Hartman int err; 525ab4382d2SGreg Kroah-Hartman 526ab4382d2SGreg Kroah-Hartman if (op->archdata.irqs[0] == 0xffffffff) 527ab4382d2SGreg Kroah-Hartman return -ENODEV; 528ab4382d2SGreg Kroah-Hartman 529ab4382d2SGreg Kroah-Hartman port = kzalloc(sizeof(struct uart_port), GFP_KERNEL); 530ab4382d2SGreg Kroah-Hartman if (unlikely(!port)) 531ab4382d2SGreg Kroah-Hartman return -ENOMEM; 532ab4382d2SGreg Kroah-Hartman 533ab4382d2SGreg Kroah-Hartman minor = 1; 534ab4382d2SGreg Kroah-Hartman if (sun4v_hvapi_register(HV_GRP_CORE, 1, &minor) == 0 && 535ab4382d2SGreg Kroah-Hartman minor >= 1) { 536ab4382d2SGreg Kroah-Hartman err = -ENOMEM; 537ab4382d2SGreg Kroah-Hartman con_write_page = kzalloc(PAGE_SIZE, GFP_KERNEL); 538ab4382d2SGreg Kroah-Hartman if (!con_write_page) 539ab4382d2SGreg Kroah-Hartman goto out_free_port; 540ab4382d2SGreg Kroah-Hartman 541ab4382d2SGreg Kroah-Hartman con_read_page = kzalloc(PAGE_SIZE, GFP_KERNEL); 542ab4382d2SGreg Kroah-Hartman if (!con_read_page) 543ab4382d2SGreg Kroah-Hartman goto out_free_con_write_page; 544ab4382d2SGreg Kroah-Hartman 545ab4382d2SGreg Kroah-Hartman sunhv_console.write = sunhv_console_write_paged; 546ab4382d2SGreg Kroah-Hartman sunhv_ops = &bywrite_ops; 547ab4382d2SGreg Kroah-Hartman } 548ab4382d2SGreg Kroah-Hartman 549ab4382d2SGreg Kroah-Hartman sunhv_port = port; 550ab4382d2SGreg Kroah-Hartman 551*b071126bSDmitry Safonov port->has_sysrq = 1; 552ab4382d2SGreg Kroah-Hartman port->line = 0; 553ab4382d2SGreg Kroah-Hartman port->ops = &sunhv_pops; 554ab4382d2SGreg Kroah-Hartman port->type = PORT_SUNHV; 555ab4382d2SGreg Kroah-Hartman port->uartclk = ( 29491200 / 16 ); /* arbitrary */ 556ab4382d2SGreg Kroah-Hartman 557ab4382d2SGreg Kroah-Hartman port->membase = (unsigned char __iomem *) __pa(port); 558ab4382d2SGreg Kroah-Hartman 559ab4382d2SGreg Kroah-Hartman port->irq = op->archdata.irqs[0]; 560ab4382d2SGreg Kroah-Hartman 561ab4382d2SGreg Kroah-Hartman port->dev = &op->dev; 562ab4382d2SGreg Kroah-Hartman 563ab4382d2SGreg Kroah-Hartman err = sunserial_register_minors(&sunhv_reg, 1); 564ab4382d2SGreg Kroah-Hartman if (err) 565ab4382d2SGreg Kroah-Hartman goto out_free_con_read_page; 566ab4382d2SGreg Kroah-Hartman 567ab4382d2SGreg Kroah-Hartman sunserial_console_match(&sunhv_console, op->dev.of_node, 568ab4382d2SGreg Kroah-Hartman &sunhv_reg, port->line, false); 569ab4382d2SGreg Kroah-Hartman 570ab4382d2SGreg Kroah-Hartman err = uart_add_one_port(&sunhv_reg, port); 571ab4382d2SGreg Kroah-Hartman if (err) 572ab4382d2SGreg Kroah-Hartman goto out_unregister_driver; 573ab4382d2SGreg Kroah-Hartman 574ab4382d2SGreg Kroah-Hartman err = request_irq(port->irq, sunhv_interrupt, 0, "hvcons", port); 575ab4382d2SGreg Kroah-Hartman if (err) 576ab4382d2SGreg Kroah-Hartman goto out_remove_port; 577ab4382d2SGreg Kroah-Hartman 578696faeddSJingoo Han platform_set_drvdata(op, port); 579ab4382d2SGreg Kroah-Hartman 580ab4382d2SGreg Kroah-Hartman return 0; 581ab4382d2SGreg Kroah-Hartman 582ab4382d2SGreg Kroah-Hartman out_remove_port: 583ab4382d2SGreg Kroah-Hartman uart_remove_one_port(&sunhv_reg, port); 584ab4382d2SGreg Kroah-Hartman 585ab4382d2SGreg Kroah-Hartman out_unregister_driver: 586ab4382d2SGreg Kroah-Hartman sunserial_unregister_minors(&sunhv_reg, 1); 587ab4382d2SGreg Kroah-Hartman 588ab4382d2SGreg Kroah-Hartman out_free_con_read_page: 589ab4382d2SGreg Kroah-Hartman kfree(con_read_page); 590ab4382d2SGreg Kroah-Hartman 591ab4382d2SGreg Kroah-Hartman out_free_con_write_page: 592ab4382d2SGreg Kroah-Hartman kfree(con_write_page); 593ab4382d2SGreg Kroah-Hartman 594ab4382d2SGreg Kroah-Hartman out_free_port: 595ab4382d2SGreg Kroah-Hartman kfree(port); 596ab4382d2SGreg Kroah-Hartman sunhv_port = NULL; 597ab4382d2SGreg Kroah-Hartman return err; 598ab4382d2SGreg Kroah-Hartman } 599ab4382d2SGreg Kroah-Hartman 600ae8d8a14SBill Pemberton static int hv_remove(struct platform_device *dev) 601ab4382d2SGreg Kroah-Hartman { 602696faeddSJingoo Han struct uart_port *port = platform_get_drvdata(dev); 603ab4382d2SGreg Kroah-Hartman 604ab4382d2SGreg Kroah-Hartman free_irq(port->irq, port); 605ab4382d2SGreg Kroah-Hartman 606ab4382d2SGreg Kroah-Hartman uart_remove_one_port(&sunhv_reg, port); 607ab4382d2SGreg Kroah-Hartman 608ab4382d2SGreg Kroah-Hartman sunserial_unregister_minors(&sunhv_reg, 1); 609fbb7d2e3SSouptick Joarder kfree(con_read_page); 610fbb7d2e3SSouptick Joarder kfree(con_write_page); 611ab4382d2SGreg Kroah-Hartman kfree(port); 612ab4382d2SGreg Kroah-Hartman sunhv_port = NULL; 613ab4382d2SGreg Kroah-Hartman 614ab4382d2SGreg Kroah-Hartman return 0; 615ab4382d2SGreg Kroah-Hartman } 616ab4382d2SGreg Kroah-Hartman 617ab4382d2SGreg Kroah-Hartman static const struct of_device_id hv_match[] = { 618ab4382d2SGreg Kroah-Hartman { 619ab4382d2SGreg Kroah-Hartman .name = "console", 620ab4382d2SGreg Kroah-Hartman .compatible = "qcn", 621ab4382d2SGreg Kroah-Hartman }, 622ab4382d2SGreg Kroah-Hartman { 623ab4382d2SGreg Kroah-Hartman .name = "console", 624ab4382d2SGreg Kroah-Hartman .compatible = "SUNW,sun4v-console", 625ab4382d2SGreg Kroah-Hartman }, 626ab4382d2SGreg Kroah-Hartman {}, 627ab4382d2SGreg Kroah-Hartman }; 628ab4382d2SGreg Kroah-Hartman 629793218dfSGrant Likely static struct platform_driver hv_driver = { 630ab4382d2SGreg Kroah-Hartman .driver = { 631ab4382d2SGreg Kroah-Hartman .name = "hv", 632ab4382d2SGreg Kroah-Hartman .of_match_table = hv_match, 633ab4382d2SGreg Kroah-Hartman }, 634ab4382d2SGreg Kroah-Hartman .probe = hv_probe, 6352d47b716SBill Pemberton .remove = hv_remove, 636ab4382d2SGreg Kroah-Hartman }; 637ab4382d2SGreg Kroah-Hartman 638ab4382d2SGreg Kroah-Hartman static int __init sunhv_init(void) 639ab4382d2SGreg Kroah-Hartman { 640ab4382d2SGreg Kroah-Hartman if (tlb_type != hypervisor) 641ab4382d2SGreg Kroah-Hartman return -ENODEV; 642ab4382d2SGreg Kroah-Hartman 643793218dfSGrant Likely return platform_driver_register(&hv_driver); 644ab4382d2SGreg Kroah-Hartman } 6454fef5351SPaul Gortmaker device_initcall(sunhv_init); 646ab4382d2SGreg Kroah-Hartman 6474fef5351SPaul Gortmaker #if 0 /* ...def MODULE ; never supported as such */ 648ab4382d2SGreg Kroah-Hartman MODULE_AUTHOR("David S. Miller"); 649ab4382d2SGreg Kroah-Hartman MODULE_DESCRIPTION("SUN4V Hypervisor console driver"); 650ab4382d2SGreg Kroah-Hartman MODULE_VERSION("2.0"); 651ab4382d2SGreg Kroah-Hartman MODULE_LICENSE("GPL"); 6524fef5351SPaul Gortmaker #endif 653