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> 2029e5c442SRob Herring #include <linux/of.h> 2129e5c442SRob Herring #include <linux/platform_device.h> 22ab4382d2SGreg Kroah-Hartman 23ab4382d2SGreg Kroah-Hartman #include <asm/hypervisor.h> 24ab4382d2SGreg Kroah-Hartman #include <asm/spitfire.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 42*1788cf6aSJiri Slaby (SUSE) static void transmit_chars_putchar(struct uart_port *port, 43*1788cf6aSJiri Slaby (SUSE) struct tty_port *tport) 44ab4382d2SGreg Kroah-Hartman { 45*1788cf6aSJiri Slaby (SUSE) unsigned char ch; 46*1788cf6aSJiri Slaby (SUSE) 47*1788cf6aSJiri Slaby (SUSE) while (kfifo_peek(&tport->xmit_fifo, &ch)) { 48*1788cf6aSJiri Slaby (SUSE) long status = sun4v_con_putchar(ch); 49ab4382d2SGreg Kroah-Hartman 50ab4382d2SGreg Kroah-Hartman if (status != HV_EOK) 51ab4382d2SGreg Kroah-Hartman break; 52ab4382d2SGreg Kroah-Hartman 53c5fd4b7dSIlpo Järvinen uart_xmit_advance(port, 1); 54ab4382d2SGreg Kroah-Hartman } 55ab4382d2SGreg Kroah-Hartman } 56ab4382d2SGreg Kroah-Hartman 57*1788cf6aSJiri Slaby (SUSE) static void transmit_chars_write(struct uart_port *port, struct tty_port *tport) 58ab4382d2SGreg Kroah-Hartman { 59*1788cf6aSJiri Slaby (SUSE) while (!kfifo_is_empty(&tport->xmit_fifo)) { 60*1788cf6aSJiri Slaby (SUSE) unsigned long len, ra, status, sent; 61*1788cf6aSJiri Slaby (SUSE) unsigned char *tail; 62ab4382d2SGreg Kroah-Hartman 63*1788cf6aSJiri Slaby (SUSE) len = kfifo_out_linear_ptr(&tport->xmit_fifo, &tail, 64ab4382d2SGreg Kroah-Hartman UART_XMIT_SIZE); 65*1788cf6aSJiri Slaby (SUSE) ra = __pa(tail); 66*1788cf6aSJiri Slaby (SUSE) 67ab4382d2SGreg Kroah-Hartman status = sun4v_con_write(ra, len, &sent); 68ab4382d2SGreg Kroah-Hartman if (status != HV_EOK) 69ab4382d2SGreg Kroah-Hartman break; 70c5fd4b7dSIlpo Järvinen uart_xmit_advance(port, 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; 950388a152SIlpo Järvinen uart_handle_dcd_change(port, false); 96ab4382d2SGreg Kroah-Hartman } else if (hung_up) { 97ab4382d2SGreg Kroah-Hartman hung_up = 0; 980388a152SIlpo Järvinen uart_handle_dcd_change(port, true); 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 { 1197db60d05SVijay Kumar static int saw_console_brk; 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) { 1317db60d05SVijay Kumar if (saw_console_brk) 1327db60d05SVijay Kumar sun_do_break(); 1337db60d05SVijay Kumar 134ab4382d2SGreg Kroah-Hartman if (uart_handle_break(port)) 135ab4382d2SGreg Kroah-Hartman continue; 136ab4382d2SGreg Kroah-Hartman saw_console_brk = 1; 137ab4382d2SGreg Kroah-Hartman *con_read_page = 0; 138ab4382d2SGreg Kroah-Hartman bytes_read = 1; 139ab4382d2SGreg Kroah-Hartman } else if (stat == CON_HUP) { 140ab4382d2SGreg Kroah-Hartman hung_up = 1; 1410388a152SIlpo Järvinen uart_handle_dcd_change(port, false); 142ab4382d2SGreg Kroah-Hartman continue; 143ab4382d2SGreg Kroah-Hartman } else { 144ab4382d2SGreg Kroah-Hartman /* HV_EWOULDBLOCK, etc. */ 145ab4382d2SGreg Kroah-Hartman break; 146ab4382d2SGreg Kroah-Hartman } 147ab4382d2SGreg Kroah-Hartman } 148ab4382d2SGreg Kroah-Hartman 149ab4382d2SGreg Kroah-Hartman if (hung_up) { 150ab4382d2SGreg Kroah-Hartman hung_up = 0; 1510388a152SIlpo Järvinen uart_handle_dcd_change(port, true); 152ab4382d2SGreg Kroah-Hartman } 153ab4382d2SGreg Kroah-Hartman 154079317a6SVijay Kumar if (port->sysrq != 0 && *con_read_page) { 155ab4382d2SGreg Kroah-Hartman for (i = 0; i < bytes_read; i++) 156ab4382d2SGreg Kroah-Hartman uart_handle_sysrq_char(port, con_read_page[i]); 1577db60d05SVijay Kumar saw_console_brk = 0; 158079317a6SVijay Kumar } 159ab4382d2SGreg Kroah-Hartman 16005c7cd39SJiri Slaby if (port->state == NULL) 161ab4382d2SGreg Kroah-Hartman continue; 162ab4382d2SGreg Kroah-Hartman 163ab4382d2SGreg Kroah-Hartman port->icount.rx += bytes_read; 164ab4382d2SGreg Kroah-Hartman 16505c7cd39SJiri Slaby tty_insert_flip_string(&port->state->port, con_read_page, 16605c7cd39SJiri Slaby bytes_read); 167ab4382d2SGreg Kroah-Hartman } 168ab4382d2SGreg Kroah-Hartman 169ab4382d2SGreg Kroah-Hartman return saw_console_brk; 170ab4382d2SGreg Kroah-Hartman } 171ab4382d2SGreg Kroah-Hartman 172ab4382d2SGreg Kroah-Hartman struct sunhv_ops { 173*1788cf6aSJiri Slaby (SUSE) void (*transmit_chars)(struct uart_port *port, struct tty_port *tport); 17405c7cd39SJiri Slaby int (*receive_chars)(struct uart_port *port); 175ab4382d2SGreg Kroah-Hartman }; 176ab4382d2SGreg Kroah-Hartman 17701fd3c27SAya Mahfouz static const struct sunhv_ops bychar_ops = { 178ab4382d2SGreg Kroah-Hartman .transmit_chars = transmit_chars_putchar, 179ab4382d2SGreg Kroah-Hartman .receive_chars = receive_chars_getchar, 180ab4382d2SGreg Kroah-Hartman }; 181ab4382d2SGreg Kroah-Hartman 18201fd3c27SAya Mahfouz static const struct sunhv_ops bywrite_ops = { 183ab4382d2SGreg Kroah-Hartman .transmit_chars = transmit_chars_write, 184ab4382d2SGreg Kroah-Hartman .receive_chars = receive_chars_read, 185ab4382d2SGreg Kroah-Hartman }; 186ab4382d2SGreg Kroah-Hartman 18701fd3c27SAya Mahfouz static const struct sunhv_ops *sunhv_ops = &bychar_ops; 188ab4382d2SGreg Kroah-Hartman 1892e124b4aSJiri Slaby static struct tty_port *receive_chars(struct uart_port *port) 190ab4382d2SGreg Kroah-Hartman { 1912e124b4aSJiri Slaby struct tty_port *tport = NULL; 192ab4382d2SGreg Kroah-Hartman 193ab4382d2SGreg Kroah-Hartman if (port->state != NULL) /* Unopened serial console */ 1942e124b4aSJiri Slaby tport = &port->state->port; 195ab4382d2SGreg Kroah-Hartman 19605c7cd39SJiri Slaby if (sunhv_ops->receive_chars(port)) 197ab4382d2SGreg Kroah-Hartman sun_do_break(); 198ab4382d2SGreg Kroah-Hartman 1992e124b4aSJiri Slaby return tport; 200ab4382d2SGreg Kroah-Hartman } 201ab4382d2SGreg Kroah-Hartman 202ab4382d2SGreg Kroah-Hartman static void transmit_chars(struct uart_port *port) 203ab4382d2SGreg Kroah-Hartman { 204*1788cf6aSJiri Slaby (SUSE) struct tty_port *tport; 205ab4382d2SGreg Kroah-Hartman 206ab4382d2SGreg Kroah-Hartman if (!port->state) 207ab4382d2SGreg Kroah-Hartman return; 208ab4382d2SGreg Kroah-Hartman 209*1788cf6aSJiri Slaby (SUSE) tport = &port->state->port; 210*1788cf6aSJiri Slaby (SUSE) if (kfifo_is_empty(&tport->xmit_fifo) || uart_tx_stopped(port)) 211ab4382d2SGreg Kroah-Hartman return; 212ab4382d2SGreg Kroah-Hartman 213*1788cf6aSJiri Slaby (SUSE) sunhv_ops->transmit_chars(port, tport); 214ab4382d2SGreg Kroah-Hartman 215*1788cf6aSJiri Slaby (SUSE) if (kfifo_len(&tport->xmit_fifo) < WAKEUP_CHARS) 216ab4382d2SGreg Kroah-Hartman uart_write_wakeup(port); 217ab4382d2SGreg Kroah-Hartman } 218ab4382d2SGreg Kroah-Hartman 219ab4382d2SGreg Kroah-Hartman static irqreturn_t sunhv_interrupt(int irq, void *dev_id) 220ab4382d2SGreg Kroah-Hartman { 221ab4382d2SGreg Kroah-Hartman struct uart_port *port = dev_id; 2222e124b4aSJiri Slaby struct tty_port *tport; 223ab4382d2SGreg Kroah-Hartman unsigned long flags; 224ab4382d2SGreg Kroah-Hartman 22571007c5eSThomas Gleixner uart_port_lock_irqsave(port, &flags); 2262e124b4aSJiri Slaby tport = receive_chars(port); 227ab4382d2SGreg Kroah-Hartman transmit_chars(port); 22871007c5eSThomas Gleixner uart_port_unlock_irqrestore(port, flags); 229ab4382d2SGreg Kroah-Hartman 2302e124b4aSJiri Slaby if (tport) 2312e124b4aSJiri Slaby tty_flip_buffer_push(tport); 232ab4382d2SGreg Kroah-Hartman 233ab4382d2SGreg Kroah-Hartman return IRQ_HANDLED; 234ab4382d2SGreg Kroah-Hartman } 235ab4382d2SGreg Kroah-Hartman 236ab4382d2SGreg Kroah-Hartman /* port->lock is not held. */ 237ab4382d2SGreg Kroah-Hartman static unsigned int sunhv_tx_empty(struct uart_port *port) 238ab4382d2SGreg Kroah-Hartman { 239ab4382d2SGreg Kroah-Hartman /* Transmitter is always empty for us. If the circ buffer 240ab4382d2SGreg Kroah-Hartman * is non-empty or there is an x_char pending, our caller 241ab4382d2SGreg Kroah-Hartman * will do the right thing and ignore what we return here. 242ab4382d2SGreg Kroah-Hartman */ 243ab4382d2SGreg Kroah-Hartman return TIOCSER_TEMT; 244ab4382d2SGreg Kroah-Hartman } 245ab4382d2SGreg Kroah-Hartman 246ab4382d2SGreg Kroah-Hartman /* port->lock held by caller. */ 247ab4382d2SGreg Kroah-Hartman static void sunhv_set_mctrl(struct uart_port *port, unsigned int mctrl) 248ab4382d2SGreg Kroah-Hartman { 249ab4382d2SGreg Kroah-Hartman return; 250ab4382d2SGreg Kroah-Hartman } 251ab4382d2SGreg Kroah-Hartman 252ab4382d2SGreg Kroah-Hartman /* port->lock is held by caller and interrupts are disabled. */ 253ab4382d2SGreg Kroah-Hartman static unsigned int sunhv_get_mctrl(struct uart_port *port) 254ab4382d2SGreg Kroah-Hartman { 255ab4382d2SGreg Kroah-Hartman return TIOCM_DSR | TIOCM_CAR | TIOCM_CTS; 256ab4382d2SGreg Kroah-Hartman } 257ab4382d2SGreg Kroah-Hartman 258ab4382d2SGreg Kroah-Hartman /* port->lock held by caller. */ 259ab4382d2SGreg Kroah-Hartman static void sunhv_stop_tx(struct uart_port *port) 260ab4382d2SGreg Kroah-Hartman { 261ab4382d2SGreg Kroah-Hartman return; 262ab4382d2SGreg Kroah-Hartman } 263ab4382d2SGreg Kroah-Hartman 264ab4382d2SGreg Kroah-Hartman /* port->lock held by caller. */ 265ab4382d2SGreg Kroah-Hartman static void sunhv_start_tx(struct uart_port *port) 266ab4382d2SGreg Kroah-Hartman { 267ab4382d2SGreg Kroah-Hartman transmit_chars(port); 268ab4382d2SGreg Kroah-Hartman } 269ab4382d2SGreg Kroah-Hartman 270ab4382d2SGreg Kroah-Hartman /* port->lock is not held. */ 271ab4382d2SGreg Kroah-Hartman static void sunhv_send_xchar(struct uart_port *port, char ch) 272ab4382d2SGreg Kroah-Hartman { 273ab4382d2SGreg Kroah-Hartman unsigned long flags; 274ab4382d2SGreg Kroah-Hartman int limit = 10000; 275ab4382d2SGreg Kroah-Hartman 276db106df3SPeter Hurley if (ch == __DISABLED_CHAR) 277db106df3SPeter Hurley return; 278db106df3SPeter Hurley 27971007c5eSThomas Gleixner uart_port_lock_irqsave(port, &flags); 280ab4382d2SGreg Kroah-Hartman 281ab4382d2SGreg Kroah-Hartman while (limit-- > 0) { 282ab4382d2SGreg Kroah-Hartman long status = sun4v_con_putchar(ch); 283ab4382d2SGreg Kroah-Hartman if (status == HV_EOK) 284ab4382d2SGreg Kroah-Hartman break; 285ab4382d2SGreg Kroah-Hartman udelay(1); 286ab4382d2SGreg Kroah-Hartman } 287ab4382d2SGreg Kroah-Hartman 28871007c5eSThomas Gleixner uart_port_unlock_irqrestore(port, flags); 289ab4382d2SGreg Kroah-Hartman } 290ab4382d2SGreg Kroah-Hartman 291ab4382d2SGreg Kroah-Hartman /* port->lock held by caller. */ 292ab4382d2SGreg Kroah-Hartman static void sunhv_stop_rx(struct uart_port *port) 293ab4382d2SGreg Kroah-Hartman { 294ab4382d2SGreg Kroah-Hartman } 295ab4382d2SGreg Kroah-Hartman 296ab4382d2SGreg Kroah-Hartman /* port->lock is not held. */ 297ab4382d2SGreg Kroah-Hartman static void sunhv_break_ctl(struct uart_port *port, int break_state) 298ab4382d2SGreg Kroah-Hartman { 299ab4382d2SGreg Kroah-Hartman if (break_state) { 300ab4382d2SGreg Kroah-Hartman unsigned long flags; 301ab4382d2SGreg Kroah-Hartman int limit = 10000; 302ab4382d2SGreg Kroah-Hartman 30371007c5eSThomas Gleixner uart_port_lock_irqsave(port, &flags); 304ab4382d2SGreg Kroah-Hartman 305ab4382d2SGreg Kroah-Hartman while (limit-- > 0) { 306ab4382d2SGreg Kroah-Hartman long status = sun4v_con_putchar(CON_BREAK); 307ab4382d2SGreg Kroah-Hartman if (status == HV_EOK) 308ab4382d2SGreg Kroah-Hartman break; 309ab4382d2SGreg Kroah-Hartman udelay(1); 310ab4382d2SGreg Kroah-Hartman } 311ab4382d2SGreg Kroah-Hartman 31271007c5eSThomas Gleixner uart_port_unlock_irqrestore(port, flags); 313ab4382d2SGreg Kroah-Hartman } 314ab4382d2SGreg Kroah-Hartman } 315ab4382d2SGreg Kroah-Hartman 316ab4382d2SGreg Kroah-Hartman /* port->lock is not held. */ 317ab4382d2SGreg Kroah-Hartman static int sunhv_startup(struct uart_port *port) 318ab4382d2SGreg Kroah-Hartman { 319ab4382d2SGreg Kroah-Hartman return 0; 320ab4382d2SGreg Kroah-Hartman } 321ab4382d2SGreg Kroah-Hartman 322ab4382d2SGreg Kroah-Hartman /* port->lock is not held. */ 323ab4382d2SGreg Kroah-Hartman static void sunhv_shutdown(struct uart_port *port) 324ab4382d2SGreg Kroah-Hartman { 325ab4382d2SGreg Kroah-Hartman } 326ab4382d2SGreg Kroah-Hartman 327ab4382d2SGreg Kroah-Hartman /* port->lock is not held. */ 328ab4382d2SGreg Kroah-Hartman static void sunhv_set_termios(struct uart_port *port, struct ktermios *termios, 329bec5b814SIlpo Järvinen const struct ktermios *old) 330ab4382d2SGreg Kroah-Hartman { 331ab4382d2SGreg Kroah-Hartman unsigned int baud = uart_get_baud_rate(port, termios, old, 0, 4000000); 332ab4382d2SGreg Kroah-Hartman unsigned int quot = uart_get_divisor(port, baud); 333ab4382d2SGreg Kroah-Hartman unsigned int iflag, cflag; 334ab4382d2SGreg Kroah-Hartman unsigned long flags; 335ab4382d2SGreg Kroah-Hartman 33671007c5eSThomas Gleixner uart_port_lock_irqsave(port, &flags); 337ab4382d2SGreg Kroah-Hartman 338ab4382d2SGreg Kroah-Hartman iflag = termios->c_iflag; 339ab4382d2SGreg Kroah-Hartman cflag = termios->c_cflag; 340ab4382d2SGreg Kroah-Hartman 341ab4382d2SGreg Kroah-Hartman port->ignore_status_mask = 0; 342ab4382d2SGreg Kroah-Hartman if (iflag & IGNBRK) 343ab4382d2SGreg Kroah-Hartman port->ignore_status_mask |= IGNORE_BREAK; 344ab4382d2SGreg Kroah-Hartman if ((cflag & CREAD) == 0) 345ab4382d2SGreg Kroah-Hartman port->ignore_status_mask |= IGNORE_ALL; 346ab4382d2SGreg Kroah-Hartman 347ab4382d2SGreg Kroah-Hartman /* XXX */ 348ab4382d2SGreg Kroah-Hartman uart_update_timeout(port, cflag, 349ab4382d2SGreg Kroah-Hartman (port->uartclk / (16 * quot))); 350ab4382d2SGreg Kroah-Hartman 35171007c5eSThomas Gleixner uart_port_unlock_irqrestore(port, flags); 352ab4382d2SGreg Kroah-Hartman } 353ab4382d2SGreg Kroah-Hartman 354ab4382d2SGreg Kroah-Hartman static const char *sunhv_type(struct uart_port *port) 355ab4382d2SGreg Kroah-Hartman { 356ab4382d2SGreg Kroah-Hartman return "SUN4V HCONS"; 357ab4382d2SGreg Kroah-Hartman } 358ab4382d2SGreg Kroah-Hartman 359ab4382d2SGreg Kroah-Hartman static void sunhv_release_port(struct uart_port *port) 360ab4382d2SGreg Kroah-Hartman { 361ab4382d2SGreg Kroah-Hartman } 362ab4382d2SGreg Kroah-Hartman 363ab4382d2SGreg Kroah-Hartman static int sunhv_request_port(struct uart_port *port) 364ab4382d2SGreg Kroah-Hartman { 365ab4382d2SGreg Kroah-Hartman return 0; 366ab4382d2SGreg Kroah-Hartman } 367ab4382d2SGreg Kroah-Hartman 368ab4382d2SGreg Kroah-Hartman static void sunhv_config_port(struct uart_port *port, int flags) 369ab4382d2SGreg Kroah-Hartman { 370ab4382d2SGreg Kroah-Hartman } 371ab4382d2SGreg Kroah-Hartman 372ab4382d2SGreg Kroah-Hartman static int sunhv_verify_port(struct uart_port *port, struct serial_struct *ser) 373ab4382d2SGreg Kroah-Hartman { 374ab4382d2SGreg Kroah-Hartman return -EINVAL; 375ab4382d2SGreg Kroah-Hartman } 376ab4382d2SGreg Kroah-Hartman 3772331e068SBhumika Goyal static const struct uart_ops sunhv_pops = { 378ab4382d2SGreg Kroah-Hartman .tx_empty = sunhv_tx_empty, 379ab4382d2SGreg Kroah-Hartman .set_mctrl = sunhv_set_mctrl, 380ab4382d2SGreg Kroah-Hartman .get_mctrl = sunhv_get_mctrl, 381ab4382d2SGreg Kroah-Hartman .stop_tx = sunhv_stop_tx, 382ab4382d2SGreg Kroah-Hartman .start_tx = sunhv_start_tx, 383ab4382d2SGreg Kroah-Hartman .send_xchar = sunhv_send_xchar, 384ab4382d2SGreg Kroah-Hartman .stop_rx = sunhv_stop_rx, 385ab4382d2SGreg Kroah-Hartman .break_ctl = sunhv_break_ctl, 386ab4382d2SGreg Kroah-Hartman .startup = sunhv_startup, 387ab4382d2SGreg Kroah-Hartman .shutdown = sunhv_shutdown, 388ab4382d2SGreg Kroah-Hartman .set_termios = sunhv_set_termios, 389ab4382d2SGreg Kroah-Hartman .type = sunhv_type, 390ab4382d2SGreg Kroah-Hartman .release_port = sunhv_release_port, 391ab4382d2SGreg Kroah-Hartman .request_port = sunhv_request_port, 392ab4382d2SGreg Kroah-Hartman .config_port = sunhv_config_port, 393ab4382d2SGreg Kroah-Hartman .verify_port = sunhv_verify_port, 394ab4382d2SGreg Kroah-Hartman }; 395ab4382d2SGreg Kroah-Hartman 396ab4382d2SGreg Kroah-Hartman static struct uart_driver sunhv_reg = { 397ab4382d2SGreg Kroah-Hartman .owner = THIS_MODULE, 398ab4382d2SGreg Kroah-Hartman .driver_name = "sunhv", 39907a6d63eSJohn Paul Adrian Glaubitz .dev_name = "ttyHV", 400ab4382d2SGreg Kroah-Hartman .major = TTY_MAJOR, 401ab4382d2SGreg Kroah-Hartman }; 402ab4382d2SGreg Kroah-Hartman 403ab4382d2SGreg Kroah-Hartman static struct uart_port *sunhv_port; 404ab4382d2SGreg Kroah-Hartman 4057dd4fcf5SVijay Kumar void sunhv_migrate_hvcons_irq(int cpu) 4067dd4fcf5SVijay Kumar { 4077dd4fcf5SVijay Kumar /* Migrate hvcons irq to param cpu */ 4087dd4fcf5SVijay Kumar irq_force_affinity(sunhv_port->irq, cpumask_of(cpu)); 4097dd4fcf5SVijay Kumar } 4107dd4fcf5SVijay Kumar 411ab4382d2SGreg Kroah-Hartman /* Copy 's' into the con_write_page, decoding "\n" into 412ab4382d2SGreg Kroah-Hartman * "\r\n" along the way. We have to return two lengths 413ab4382d2SGreg Kroah-Hartman * because the caller needs to know how much to advance 414ab4382d2SGreg Kroah-Hartman * 's' and also how many bytes to output via con_write_page. 415ab4382d2SGreg Kroah-Hartman */ 416ab4382d2SGreg Kroah-Hartman static int fill_con_write_page(const char *s, unsigned int n, 417ab4382d2SGreg Kroah-Hartman unsigned long *page_bytes) 418ab4382d2SGreg Kroah-Hartman { 419ab4382d2SGreg Kroah-Hartman const char *orig_s = s; 420ab4382d2SGreg Kroah-Hartman char *p = con_write_page; 421ab4382d2SGreg Kroah-Hartman int left = PAGE_SIZE; 422ab4382d2SGreg Kroah-Hartman 423ab4382d2SGreg Kroah-Hartman while (n--) { 424ab4382d2SGreg Kroah-Hartman if (*s == '\n') { 425ab4382d2SGreg Kroah-Hartman if (left < 2) 426ab4382d2SGreg Kroah-Hartman break; 427ab4382d2SGreg Kroah-Hartman *p++ = '\r'; 428ab4382d2SGreg Kroah-Hartman left--; 429ab4382d2SGreg Kroah-Hartman } else if (left < 1) 430ab4382d2SGreg Kroah-Hartman break; 431ab4382d2SGreg Kroah-Hartman *p++ = *s++; 432ab4382d2SGreg Kroah-Hartman left--; 433ab4382d2SGreg Kroah-Hartman } 434ab4382d2SGreg Kroah-Hartman *page_bytes = p - con_write_page; 435ab4382d2SGreg Kroah-Hartman return s - orig_s; 436ab4382d2SGreg Kroah-Hartman } 437ab4382d2SGreg Kroah-Hartman 438ab4382d2SGreg Kroah-Hartman static void sunhv_console_write_paged(struct console *con, const char *s, unsigned n) 439ab4382d2SGreg Kroah-Hartman { 440ab4382d2SGreg Kroah-Hartman struct uart_port *port = sunhv_port; 441ab4382d2SGreg Kroah-Hartman unsigned long flags; 442ab4382d2SGreg Kroah-Hartman int locked = 1; 443ab4382d2SGreg Kroah-Hartman 444e58e241cSDavid Miller if (port->sysrq || oops_in_progress) 44571007c5eSThomas Gleixner locked = uart_port_trylock_irqsave(port, &flags); 446e58e241cSDavid Miller else 44771007c5eSThomas Gleixner uart_port_lock_irqsave(port, &flags); 448ab4382d2SGreg Kroah-Hartman 449ab4382d2SGreg Kroah-Hartman while (n > 0) { 450ab4382d2SGreg Kroah-Hartman unsigned long ra = __pa(con_write_page); 451ab4382d2SGreg Kroah-Hartman unsigned long page_bytes; 452ab4382d2SGreg Kroah-Hartman unsigned int cpy = fill_con_write_page(s, n, 453ab4382d2SGreg Kroah-Hartman &page_bytes); 454ab4382d2SGreg Kroah-Hartman 455ab4382d2SGreg Kroah-Hartman n -= cpy; 456ab4382d2SGreg Kroah-Hartman s += cpy; 457ab4382d2SGreg Kroah-Hartman while (page_bytes > 0) { 458ab4382d2SGreg Kroah-Hartman unsigned long written; 459ab4382d2SGreg Kroah-Hartman int limit = 1000000; 460ab4382d2SGreg Kroah-Hartman 461ab4382d2SGreg Kroah-Hartman while (limit--) { 462ab4382d2SGreg Kroah-Hartman unsigned long stat; 463ab4382d2SGreg Kroah-Hartman 464ab4382d2SGreg Kroah-Hartman stat = sun4v_con_write(ra, page_bytes, 465ab4382d2SGreg Kroah-Hartman &written); 466ab4382d2SGreg Kroah-Hartman if (stat == HV_EOK) 467ab4382d2SGreg Kroah-Hartman break; 468ab4382d2SGreg Kroah-Hartman udelay(1); 469ab4382d2SGreg Kroah-Hartman } 470ab4382d2SGreg Kroah-Hartman if (limit < 0) 471ab4382d2SGreg Kroah-Hartman break; 472ab4382d2SGreg Kroah-Hartman page_bytes -= written; 473ab4382d2SGreg Kroah-Hartman ra += written; 474ab4382d2SGreg Kroah-Hartman } 475ab4382d2SGreg Kroah-Hartman } 476ab4382d2SGreg Kroah-Hartman 477ab4382d2SGreg Kroah-Hartman if (locked) 47871007c5eSThomas Gleixner uart_port_unlock_irqrestore(port, flags); 479ab4382d2SGreg Kroah-Hartman } 480ab4382d2SGreg Kroah-Hartman 481ab4382d2SGreg Kroah-Hartman static inline void sunhv_console_putchar(struct uart_port *port, char c) 482ab4382d2SGreg Kroah-Hartman { 483ab4382d2SGreg Kroah-Hartman int limit = 1000000; 484ab4382d2SGreg Kroah-Hartman 485ab4382d2SGreg Kroah-Hartman while (limit-- > 0) { 486ab4382d2SGreg Kroah-Hartman long status = sun4v_con_putchar(c); 487ab4382d2SGreg Kroah-Hartman if (status == HV_EOK) 488ab4382d2SGreg Kroah-Hartman break; 489ab4382d2SGreg Kroah-Hartman udelay(1); 490ab4382d2SGreg Kroah-Hartman } 491ab4382d2SGreg Kroah-Hartman } 492ab4382d2SGreg Kroah-Hartman 493ab4382d2SGreg Kroah-Hartman static void sunhv_console_write_bychar(struct console *con, const char *s, unsigned n) 494ab4382d2SGreg Kroah-Hartman { 495ab4382d2SGreg Kroah-Hartman struct uart_port *port = sunhv_port; 496ab4382d2SGreg Kroah-Hartman unsigned long flags; 497ab4382d2SGreg Kroah-Hartman int i, locked = 1; 498ab4382d2SGreg Kroah-Hartman 499e58e241cSDavid Miller if (port->sysrq || oops_in_progress) 50071007c5eSThomas Gleixner locked = uart_port_trylock_irqsave(port, &flags); 501e58e241cSDavid Miller else 50271007c5eSThomas Gleixner uart_port_lock_irqsave(port, &flags); 503ab4382d2SGreg Kroah-Hartman 504ab4382d2SGreg Kroah-Hartman for (i = 0; i < n; i++) { 505ab4382d2SGreg Kroah-Hartman if (*s == '\n') 506ab4382d2SGreg Kroah-Hartman sunhv_console_putchar(port, '\r'); 507ab4382d2SGreg Kroah-Hartman sunhv_console_putchar(port, *s++); 508ab4382d2SGreg Kroah-Hartman } 509ab4382d2SGreg Kroah-Hartman 510ab4382d2SGreg Kroah-Hartman if (locked) 51171007c5eSThomas Gleixner uart_port_unlock_irqrestore(port, flags); 512ab4382d2SGreg Kroah-Hartman } 513ab4382d2SGreg Kroah-Hartman 514ab4382d2SGreg Kroah-Hartman static struct console sunhv_console = { 515ab4382d2SGreg Kroah-Hartman .name = "ttyHV", 516ab4382d2SGreg Kroah-Hartman .write = sunhv_console_write_bychar, 517ab4382d2SGreg Kroah-Hartman .device = uart_console_device, 518ab4382d2SGreg Kroah-Hartman .flags = CON_PRINTBUFFER, 519ab4382d2SGreg Kroah-Hartman .index = -1, 520ab4382d2SGreg Kroah-Hartman .data = &sunhv_reg, 521ab4382d2SGreg Kroah-Hartman }; 522ab4382d2SGreg Kroah-Hartman 5239671f099SBill Pemberton static int hv_probe(struct platform_device *op) 524ab4382d2SGreg Kroah-Hartman { 525ab4382d2SGreg Kroah-Hartman struct uart_port *port; 526ab4382d2SGreg Kroah-Hartman unsigned long minor; 527ab4382d2SGreg Kroah-Hartman int err; 528ab4382d2SGreg Kroah-Hartman 529ab4382d2SGreg Kroah-Hartman if (op->archdata.irqs[0] == 0xffffffff) 530ab4382d2SGreg Kroah-Hartman return -ENODEV; 531ab4382d2SGreg Kroah-Hartman 532ab4382d2SGreg Kroah-Hartman port = kzalloc(sizeof(struct uart_port), GFP_KERNEL); 533ab4382d2SGreg Kroah-Hartman if (unlikely(!port)) 534ab4382d2SGreg Kroah-Hartman return -ENOMEM; 535ab4382d2SGreg Kroah-Hartman 536ab4382d2SGreg Kroah-Hartman minor = 1; 537ab4382d2SGreg Kroah-Hartman if (sun4v_hvapi_register(HV_GRP_CORE, 1, &minor) == 0 && 538ab4382d2SGreg Kroah-Hartman minor >= 1) { 539ab4382d2SGreg Kroah-Hartman err = -ENOMEM; 540ab4382d2SGreg Kroah-Hartman con_write_page = kzalloc(PAGE_SIZE, GFP_KERNEL); 541ab4382d2SGreg Kroah-Hartman if (!con_write_page) 542ab4382d2SGreg Kroah-Hartman goto out_free_port; 543ab4382d2SGreg Kroah-Hartman 544ab4382d2SGreg Kroah-Hartman con_read_page = kzalloc(PAGE_SIZE, GFP_KERNEL); 545ab4382d2SGreg Kroah-Hartman if (!con_read_page) 546ab4382d2SGreg Kroah-Hartman goto out_free_con_write_page; 547ab4382d2SGreg Kroah-Hartman 548ab4382d2SGreg Kroah-Hartman sunhv_console.write = sunhv_console_write_paged; 549ab4382d2SGreg Kroah-Hartman sunhv_ops = &bywrite_ops; 550ab4382d2SGreg Kroah-Hartman } 551ab4382d2SGreg Kroah-Hartman 552ab4382d2SGreg Kroah-Hartman sunhv_port = port; 553ab4382d2SGreg Kroah-Hartman 554b071126bSDmitry Safonov port->has_sysrq = 1; 555ab4382d2SGreg Kroah-Hartman port->line = 0; 556ab4382d2SGreg Kroah-Hartman port->ops = &sunhv_pops; 557ab4382d2SGreg Kroah-Hartman port->type = PORT_SUNHV; 558ab4382d2SGreg Kroah-Hartman port->uartclk = ( 29491200 / 16 ); /* arbitrary */ 559ab4382d2SGreg Kroah-Hartman 560ab4382d2SGreg Kroah-Hartman port->membase = (unsigned char __iomem *) __pa(port); 561ab4382d2SGreg Kroah-Hartman 562ab4382d2SGreg Kroah-Hartman port->irq = op->archdata.irqs[0]; 563ab4382d2SGreg Kroah-Hartman 564ab4382d2SGreg Kroah-Hartman port->dev = &op->dev; 565ab4382d2SGreg Kroah-Hartman 566ab4382d2SGreg Kroah-Hartman err = sunserial_register_minors(&sunhv_reg, 1); 567ab4382d2SGreg Kroah-Hartman if (err) 568ab4382d2SGreg Kroah-Hartman goto out_free_con_read_page; 569ab4382d2SGreg Kroah-Hartman 570ab4382d2SGreg Kroah-Hartman sunserial_console_match(&sunhv_console, op->dev.of_node, 571ab4382d2SGreg Kroah-Hartman &sunhv_reg, port->line, false); 572ab4382d2SGreg Kroah-Hartman 573ab4382d2SGreg Kroah-Hartman err = uart_add_one_port(&sunhv_reg, port); 574ab4382d2SGreg Kroah-Hartman if (err) 575ab4382d2SGreg Kroah-Hartman goto out_unregister_driver; 576ab4382d2SGreg Kroah-Hartman 577ab4382d2SGreg Kroah-Hartman err = request_irq(port->irq, sunhv_interrupt, 0, "hvcons", port); 578ab4382d2SGreg Kroah-Hartman if (err) 579ab4382d2SGreg Kroah-Hartman goto out_remove_port; 580ab4382d2SGreg Kroah-Hartman 581696faeddSJingoo Han platform_set_drvdata(op, port); 582ab4382d2SGreg Kroah-Hartman 583ab4382d2SGreg Kroah-Hartman return 0; 584ab4382d2SGreg Kroah-Hartman 585ab4382d2SGreg Kroah-Hartman out_remove_port: 586ab4382d2SGreg Kroah-Hartman uart_remove_one_port(&sunhv_reg, port); 587ab4382d2SGreg Kroah-Hartman 588ab4382d2SGreg Kroah-Hartman out_unregister_driver: 589ab4382d2SGreg Kroah-Hartman sunserial_unregister_minors(&sunhv_reg, 1); 590ab4382d2SGreg Kroah-Hartman 591ab4382d2SGreg Kroah-Hartman out_free_con_read_page: 592ab4382d2SGreg Kroah-Hartman kfree(con_read_page); 593ab4382d2SGreg Kroah-Hartman 594ab4382d2SGreg Kroah-Hartman out_free_con_write_page: 595ab4382d2SGreg Kroah-Hartman kfree(con_write_page); 596ab4382d2SGreg Kroah-Hartman 597ab4382d2SGreg Kroah-Hartman out_free_port: 598ab4382d2SGreg Kroah-Hartman kfree(port); 599ab4382d2SGreg Kroah-Hartman sunhv_port = NULL; 600ab4382d2SGreg Kroah-Hartman return err; 601ab4382d2SGreg Kroah-Hartman } 602ab4382d2SGreg Kroah-Hartman 6033cfff33aSUwe Kleine-König static void hv_remove(struct platform_device *dev) 604ab4382d2SGreg Kroah-Hartman { 605696faeddSJingoo Han struct uart_port *port = platform_get_drvdata(dev); 606ab4382d2SGreg Kroah-Hartman 607ab4382d2SGreg Kroah-Hartman free_irq(port->irq, port); 608ab4382d2SGreg Kroah-Hartman 609ab4382d2SGreg Kroah-Hartman uart_remove_one_port(&sunhv_reg, port); 610ab4382d2SGreg Kroah-Hartman 611ab4382d2SGreg Kroah-Hartman sunserial_unregister_minors(&sunhv_reg, 1); 612fbb7d2e3SSouptick Joarder kfree(con_read_page); 613fbb7d2e3SSouptick Joarder kfree(con_write_page); 614ab4382d2SGreg Kroah-Hartman kfree(port); 615ab4382d2SGreg Kroah-Hartman sunhv_port = NULL; 616ab4382d2SGreg Kroah-Hartman } 617ab4382d2SGreg Kroah-Hartman 618ab4382d2SGreg Kroah-Hartman static const struct of_device_id hv_match[] = { 619ab4382d2SGreg Kroah-Hartman { 620ab4382d2SGreg Kroah-Hartman .name = "console", 621ab4382d2SGreg Kroah-Hartman .compatible = "qcn", 622ab4382d2SGreg Kroah-Hartman }, 623ab4382d2SGreg Kroah-Hartman { 624ab4382d2SGreg Kroah-Hartman .name = "console", 625ab4382d2SGreg Kroah-Hartman .compatible = "SUNW,sun4v-console", 626ab4382d2SGreg Kroah-Hartman }, 627ab4382d2SGreg Kroah-Hartman {}, 628ab4382d2SGreg Kroah-Hartman }; 629ab4382d2SGreg Kroah-Hartman 630793218dfSGrant Likely static struct platform_driver hv_driver = { 631ab4382d2SGreg Kroah-Hartman .driver = { 632ab4382d2SGreg Kroah-Hartman .name = "hv", 633ab4382d2SGreg Kroah-Hartman .of_match_table = hv_match, 634ab4382d2SGreg Kroah-Hartman }, 635ab4382d2SGreg Kroah-Hartman .probe = hv_probe, 6363cfff33aSUwe Kleine-König .remove_new = hv_remove, 637ab4382d2SGreg Kroah-Hartman }; 638ab4382d2SGreg Kroah-Hartman 639ab4382d2SGreg Kroah-Hartman static int __init sunhv_init(void) 640ab4382d2SGreg Kroah-Hartman { 641ab4382d2SGreg Kroah-Hartman if (tlb_type != hypervisor) 642ab4382d2SGreg Kroah-Hartman return -ENODEV; 643ab4382d2SGreg Kroah-Hartman 644793218dfSGrant Likely return platform_driver_register(&hv_driver); 645ab4382d2SGreg Kroah-Hartman } 6464fef5351SPaul Gortmaker device_initcall(sunhv_init); 647ab4382d2SGreg Kroah-Hartman 6484fef5351SPaul Gortmaker #if 0 /* ...def MODULE ; never supported as such */ 649ab4382d2SGreg Kroah-Hartman MODULE_AUTHOR("David S. Miller"); 650ab4382d2SGreg Kroah-Hartman MODULE_DESCRIPTION("SUN4V Hypervisor console driver"); 651ab4382d2SGreg Kroah-Hartman MODULE_VERSION("2.0"); 652ab4382d2SGreg Kroah-Hartman MODULE_LICENSE("GPL"); 6534fef5351SPaul Gortmaker #endif 654