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>
20*29e5c442SRob Herring #include <linux/of.h>
21*29e5c442SRob 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
transmit_chars_putchar(struct uart_port * port,struct circ_buf * xmit)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
50c5fd4b7dSIlpo Järvinen uart_xmit_advance(port, 1);
51ab4382d2SGreg Kroah-Hartman }
52ab4382d2SGreg Kroah-Hartman }
53ab4382d2SGreg Kroah-Hartman
transmit_chars_write(struct uart_port * port,struct circ_buf * xmit)54ab4382d2SGreg Kroah-Hartman static void transmit_chars_write(struct uart_port *port, struct circ_buf *xmit)
55ab4382d2SGreg Kroah-Hartman {
56ab4382d2SGreg Kroah-Hartman while (!uart_circ_empty(xmit)) {
57ab4382d2SGreg Kroah-Hartman unsigned long ra = __pa(xmit->buf + xmit->tail);
58ab4382d2SGreg Kroah-Hartman unsigned long len, status, sent;
59ab4382d2SGreg Kroah-Hartman
60ab4382d2SGreg Kroah-Hartman len = CIRC_CNT_TO_END(xmit->head, xmit->tail,
61ab4382d2SGreg Kroah-Hartman UART_XMIT_SIZE);
62ab4382d2SGreg Kroah-Hartman status = sun4v_con_write(ra, len, &sent);
63ab4382d2SGreg Kroah-Hartman if (status != HV_EOK)
64ab4382d2SGreg Kroah-Hartman break;
65c5fd4b7dSIlpo Järvinen uart_xmit_advance(port, sent);
66ab4382d2SGreg Kroah-Hartman }
67ab4382d2SGreg Kroah-Hartman }
68ab4382d2SGreg Kroah-Hartman
receive_chars_getchar(struct uart_port * port)6905c7cd39SJiri Slaby static int receive_chars_getchar(struct uart_port *port)
70ab4382d2SGreg Kroah-Hartman {
71ab4382d2SGreg Kroah-Hartman int saw_console_brk = 0;
72ab4382d2SGreg Kroah-Hartman int limit = 10000;
73ab4382d2SGreg Kroah-Hartman
74ab4382d2SGreg Kroah-Hartman while (limit-- > 0) {
75ab4382d2SGreg Kroah-Hartman long status;
76ab4382d2SGreg Kroah-Hartman long c = sun4v_con_getchar(&status);
77ab4382d2SGreg Kroah-Hartman
78ab4382d2SGreg Kroah-Hartman if (status == HV_EWOULDBLOCK)
79ab4382d2SGreg Kroah-Hartman break;
80ab4382d2SGreg Kroah-Hartman
81ab4382d2SGreg Kroah-Hartman if (c == CON_BREAK) {
82ab4382d2SGreg Kroah-Hartman if (uart_handle_break(port))
83ab4382d2SGreg Kroah-Hartman continue;
84ab4382d2SGreg Kroah-Hartman saw_console_brk = 1;
85ab4382d2SGreg Kroah-Hartman c = 0;
86ab4382d2SGreg Kroah-Hartman }
87ab4382d2SGreg Kroah-Hartman
88ab4382d2SGreg Kroah-Hartman if (c == CON_HUP) {
89ab4382d2SGreg Kroah-Hartman hung_up = 1;
900388a152SIlpo Järvinen uart_handle_dcd_change(port, false);
91ab4382d2SGreg Kroah-Hartman } else if (hung_up) {
92ab4382d2SGreg Kroah-Hartman hung_up = 0;
930388a152SIlpo Järvinen uart_handle_dcd_change(port, true);
94ab4382d2SGreg Kroah-Hartman }
95ab4382d2SGreg Kroah-Hartman
9692a19f9cSJiri Slaby if (port->state == NULL) {
97ab4382d2SGreg Kroah-Hartman uart_handle_sysrq_char(port, c);
98ab4382d2SGreg Kroah-Hartman continue;
99ab4382d2SGreg Kroah-Hartman }
100ab4382d2SGreg Kroah-Hartman
101ab4382d2SGreg Kroah-Hartman port->icount.rx++;
102ab4382d2SGreg Kroah-Hartman
103ab4382d2SGreg Kroah-Hartman if (uart_handle_sysrq_char(port, c))
104ab4382d2SGreg Kroah-Hartman continue;
105ab4382d2SGreg Kroah-Hartman
10692a19f9cSJiri Slaby tty_insert_flip_char(&port->state->port, c, TTY_NORMAL);
107ab4382d2SGreg Kroah-Hartman }
108ab4382d2SGreg Kroah-Hartman
109ab4382d2SGreg Kroah-Hartman return saw_console_brk;
110ab4382d2SGreg Kroah-Hartman }
111ab4382d2SGreg Kroah-Hartman
receive_chars_read(struct uart_port * port)11205c7cd39SJiri Slaby static int receive_chars_read(struct uart_port *port)
113ab4382d2SGreg Kroah-Hartman {
1147db60d05SVijay Kumar static int saw_console_brk;
115ab4382d2SGreg Kroah-Hartman int limit = 10000;
116ab4382d2SGreg Kroah-Hartman
117ab4382d2SGreg Kroah-Hartman while (limit-- > 0) {
118ab4382d2SGreg Kroah-Hartman unsigned long ra = __pa(con_read_page);
119ab4382d2SGreg Kroah-Hartman unsigned long bytes_read, i;
120ab4382d2SGreg Kroah-Hartman long stat = sun4v_con_read(ra, PAGE_SIZE, &bytes_read);
121ab4382d2SGreg Kroah-Hartman
122ab4382d2SGreg Kroah-Hartman if (stat != HV_EOK) {
123ab4382d2SGreg Kroah-Hartman bytes_read = 0;
124ab4382d2SGreg Kroah-Hartman
125ab4382d2SGreg Kroah-Hartman if (stat == CON_BREAK) {
1267db60d05SVijay Kumar if (saw_console_brk)
1277db60d05SVijay Kumar sun_do_break();
1287db60d05SVijay Kumar
129ab4382d2SGreg Kroah-Hartman if (uart_handle_break(port))
130ab4382d2SGreg Kroah-Hartman continue;
131ab4382d2SGreg Kroah-Hartman saw_console_brk = 1;
132ab4382d2SGreg Kroah-Hartman *con_read_page = 0;
133ab4382d2SGreg Kroah-Hartman bytes_read = 1;
134ab4382d2SGreg Kroah-Hartman } else if (stat == CON_HUP) {
135ab4382d2SGreg Kroah-Hartman hung_up = 1;
1360388a152SIlpo Järvinen uart_handle_dcd_change(port, false);
137ab4382d2SGreg Kroah-Hartman continue;
138ab4382d2SGreg Kroah-Hartman } else {
139ab4382d2SGreg Kroah-Hartman /* HV_EWOULDBLOCK, etc. */
140ab4382d2SGreg Kroah-Hartman break;
141ab4382d2SGreg Kroah-Hartman }
142ab4382d2SGreg Kroah-Hartman }
143ab4382d2SGreg Kroah-Hartman
144ab4382d2SGreg Kroah-Hartman if (hung_up) {
145ab4382d2SGreg Kroah-Hartman hung_up = 0;
1460388a152SIlpo Järvinen uart_handle_dcd_change(port, true);
147ab4382d2SGreg Kroah-Hartman }
148ab4382d2SGreg Kroah-Hartman
149079317a6SVijay Kumar if (port->sysrq != 0 && *con_read_page) {
150ab4382d2SGreg Kroah-Hartman for (i = 0; i < bytes_read; i++)
151ab4382d2SGreg Kroah-Hartman uart_handle_sysrq_char(port, con_read_page[i]);
1527db60d05SVijay Kumar saw_console_brk = 0;
153079317a6SVijay Kumar }
154ab4382d2SGreg Kroah-Hartman
15505c7cd39SJiri Slaby if (port->state == NULL)
156ab4382d2SGreg Kroah-Hartman continue;
157ab4382d2SGreg Kroah-Hartman
158ab4382d2SGreg Kroah-Hartman port->icount.rx += bytes_read;
159ab4382d2SGreg Kroah-Hartman
16005c7cd39SJiri Slaby tty_insert_flip_string(&port->state->port, con_read_page,
16105c7cd39SJiri Slaby bytes_read);
162ab4382d2SGreg Kroah-Hartman }
163ab4382d2SGreg Kroah-Hartman
164ab4382d2SGreg Kroah-Hartman return saw_console_brk;
165ab4382d2SGreg Kroah-Hartman }
166ab4382d2SGreg Kroah-Hartman
167ab4382d2SGreg Kroah-Hartman struct sunhv_ops {
168ab4382d2SGreg Kroah-Hartman void (*transmit_chars)(struct uart_port *port, struct circ_buf *xmit);
16905c7cd39SJiri Slaby int (*receive_chars)(struct uart_port *port);
170ab4382d2SGreg Kroah-Hartman };
171ab4382d2SGreg Kroah-Hartman
17201fd3c27SAya Mahfouz static const struct sunhv_ops bychar_ops = {
173ab4382d2SGreg Kroah-Hartman .transmit_chars = transmit_chars_putchar,
174ab4382d2SGreg Kroah-Hartman .receive_chars = receive_chars_getchar,
175ab4382d2SGreg Kroah-Hartman };
176ab4382d2SGreg Kroah-Hartman
17701fd3c27SAya Mahfouz static const struct sunhv_ops bywrite_ops = {
178ab4382d2SGreg Kroah-Hartman .transmit_chars = transmit_chars_write,
179ab4382d2SGreg Kroah-Hartman .receive_chars = receive_chars_read,
180ab4382d2SGreg Kroah-Hartman };
181ab4382d2SGreg Kroah-Hartman
18201fd3c27SAya Mahfouz static const struct sunhv_ops *sunhv_ops = &bychar_ops;
183ab4382d2SGreg Kroah-Hartman
receive_chars(struct uart_port * port)1842e124b4aSJiri Slaby static struct tty_port *receive_chars(struct uart_port *port)
185ab4382d2SGreg Kroah-Hartman {
1862e124b4aSJiri Slaby struct tty_port *tport = NULL;
187ab4382d2SGreg Kroah-Hartman
188ab4382d2SGreg Kroah-Hartman if (port->state != NULL) /* Unopened serial console */
1892e124b4aSJiri Slaby tport = &port->state->port;
190ab4382d2SGreg Kroah-Hartman
19105c7cd39SJiri Slaby if (sunhv_ops->receive_chars(port))
192ab4382d2SGreg Kroah-Hartman sun_do_break();
193ab4382d2SGreg Kroah-Hartman
1942e124b4aSJiri Slaby return tport;
195ab4382d2SGreg Kroah-Hartman }
196ab4382d2SGreg Kroah-Hartman
transmit_chars(struct uart_port * port)197ab4382d2SGreg Kroah-Hartman static void transmit_chars(struct uart_port *port)
198ab4382d2SGreg Kroah-Hartman {
199ab4382d2SGreg Kroah-Hartman struct circ_buf *xmit;
200ab4382d2SGreg Kroah-Hartman
201ab4382d2SGreg Kroah-Hartman if (!port->state)
202ab4382d2SGreg Kroah-Hartman return;
203ab4382d2SGreg Kroah-Hartman
204ab4382d2SGreg Kroah-Hartman xmit = &port->state->xmit;
205ab4382d2SGreg Kroah-Hartman if (uart_circ_empty(xmit) || uart_tx_stopped(port))
206ab4382d2SGreg Kroah-Hartman return;
207ab4382d2SGreg Kroah-Hartman
208ab4382d2SGreg Kroah-Hartman sunhv_ops->transmit_chars(port, xmit);
209ab4382d2SGreg Kroah-Hartman
210ab4382d2SGreg Kroah-Hartman if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
211ab4382d2SGreg Kroah-Hartman uart_write_wakeup(port);
212ab4382d2SGreg Kroah-Hartman }
213ab4382d2SGreg Kroah-Hartman
sunhv_interrupt(int irq,void * dev_id)214ab4382d2SGreg Kroah-Hartman static irqreturn_t sunhv_interrupt(int irq, void *dev_id)
215ab4382d2SGreg Kroah-Hartman {
216ab4382d2SGreg Kroah-Hartman struct uart_port *port = dev_id;
2172e124b4aSJiri Slaby struct tty_port *tport;
218ab4382d2SGreg Kroah-Hartman unsigned long flags;
219ab4382d2SGreg Kroah-Hartman
220ab4382d2SGreg Kroah-Hartman spin_lock_irqsave(&port->lock, flags);
2212e124b4aSJiri Slaby tport = receive_chars(port);
222ab4382d2SGreg Kroah-Hartman transmit_chars(port);
223ab4382d2SGreg Kroah-Hartman spin_unlock_irqrestore(&port->lock, flags);
224ab4382d2SGreg Kroah-Hartman
2252e124b4aSJiri Slaby if (tport)
2262e124b4aSJiri Slaby tty_flip_buffer_push(tport);
227ab4382d2SGreg Kroah-Hartman
228ab4382d2SGreg Kroah-Hartman return IRQ_HANDLED;
229ab4382d2SGreg Kroah-Hartman }
230ab4382d2SGreg Kroah-Hartman
231ab4382d2SGreg Kroah-Hartman /* port->lock is not held. */
sunhv_tx_empty(struct uart_port * port)232ab4382d2SGreg Kroah-Hartman static unsigned int sunhv_tx_empty(struct uart_port *port)
233ab4382d2SGreg Kroah-Hartman {
234ab4382d2SGreg Kroah-Hartman /* Transmitter is always empty for us. If the circ buffer
235ab4382d2SGreg Kroah-Hartman * is non-empty or there is an x_char pending, our caller
236ab4382d2SGreg Kroah-Hartman * will do the right thing and ignore what we return here.
237ab4382d2SGreg Kroah-Hartman */
238ab4382d2SGreg Kroah-Hartman return TIOCSER_TEMT;
239ab4382d2SGreg Kroah-Hartman }
240ab4382d2SGreg Kroah-Hartman
241ab4382d2SGreg Kroah-Hartman /* port->lock held by caller. */
sunhv_set_mctrl(struct uart_port * port,unsigned int mctrl)242ab4382d2SGreg Kroah-Hartman static void sunhv_set_mctrl(struct uart_port *port, unsigned int mctrl)
243ab4382d2SGreg Kroah-Hartman {
244ab4382d2SGreg Kroah-Hartman return;
245ab4382d2SGreg Kroah-Hartman }
246ab4382d2SGreg Kroah-Hartman
247ab4382d2SGreg Kroah-Hartman /* port->lock is held by caller and interrupts are disabled. */
sunhv_get_mctrl(struct uart_port * port)248ab4382d2SGreg Kroah-Hartman static unsigned int sunhv_get_mctrl(struct uart_port *port)
249ab4382d2SGreg Kroah-Hartman {
250ab4382d2SGreg Kroah-Hartman return TIOCM_DSR | TIOCM_CAR | TIOCM_CTS;
251ab4382d2SGreg Kroah-Hartman }
252ab4382d2SGreg Kroah-Hartman
253ab4382d2SGreg Kroah-Hartman /* port->lock held by caller. */
sunhv_stop_tx(struct uart_port * port)254ab4382d2SGreg Kroah-Hartman static void sunhv_stop_tx(struct uart_port *port)
255ab4382d2SGreg Kroah-Hartman {
256ab4382d2SGreg Kroah-Hartman return;
257ab4382d2SGreg Kroah-Hartman }
258ab4382d2SGreg Kroah-Hartman
259ab4382d2SGreg Kroah-Hartman /* port->lock held by caller. */
sunhv_start_tx(struct uart_port * port)260ab4382d2SGreg Kroah-Hartman static void sunhv_start_tx(struct uart_port *port)
261ab4382d2SGreg Kroah-Hartman {
262ab4382d2SGreg Kroah-Hartman transmit_chars(port);
263ab4382d2SGreg Kroah-Hartman }
264ab4382d2SGreg Kroah-Hartman
265ab4382d2SGreg Kroah-Hartman /* port->lock is not held. */
sunhv_send_xchar(struct uart_port * port,char ch)266ab4382d2SGreg Kroah-Hartman static void sunhv_send_xchar(struct uart_port *port, char ch)
267ab4382d2SGreg Kroah-Hartman {
268ab4382d2SGreg Kroah-Hartman unsigned long flags;
269ab4382d2SGreg Kroah-Hartman int limit = 10000;
270ab4382d2SGreg Kroah-Hartman
271db106df3SPeter Hurley if (ch == __DISABLED_CHAR)
272db106df3SPeter Hurley return;
273db106df3SPeter Hurley
274ab4382d2SGreg Kroah-Hartman spin_lock_irqsave(&port->lock, flags);
275ab4382d2SGreg Kroah-Hartman
276ab4382d2SGreg Kroah-Hartman while (limit-- > 0) {
277ab4382d2SGreg Kroah-Hartman long status = sun4v_con_putchar(ch);
278ab4382d2SGreg Kroah-Hartman if (status == HV_EOK)
279ab4382d2SGreg Kroah-Hartman break;
280ab4382d2SGreg Kroah-Hartman udelay(1);
281ab4382d2SGreg Kroah-Hartman }
282ab4382d2SGreg Kroah-Hartman
283ab4382d2SGreg Kroah-Hartman spin_unlock_irqrestore(&port->lock, flags);
284ab4382d2SGreg Kroah-Hartman }
285ab4382d2SGreg Kroah-Hartman
286ab4382d2SGreg Kroah-Hartman /* port->lock held by caller. */
sunhv_stop_rx(struct uart_port * port)287ab4382d2SGreg Kroah-Hartman static void sunhv_stop_rx(struct uart_port *port)
288ab4382d2SGreg Kroah-Hartman {
289ab4382d2SGreg Kroah-Hartman }
290ab4382d2SGreg Kroah-Hartman
291ab4382d2SGreg Kroah-Hartman /* port->lock is not held. */
sunhv_break_ctl(struct uart_port * port,int break_state)292ab4382d2SGreg Kroah-Hartman static void sunhv_break_ctl(struct uart_port *port, int break_state)
293ab4382d2SGreg Kroah-Hartman {
294ab4382d2SGreg Kroah-Hartman if (break_state) {
295ab4382d2SGreg Kroah-Hartman unsigned long flags;
296ab4382d2SGreg Kroah-Hartman int limit = 10000;
297ab4382d2SGreg Kroah-Hartman
298ab4382d2SGreg Kroah-Hartman spin_lock_irqsave(&port->lock, flags);
299ab4382d2SGreg Kroah-Hartman
300ab4382d2SGreg Kroah-Hartman while (limit-- > 0) {
301ab4382d2SGreg Kroah-Hartman long status = sun4v_con_putchar(CON_BREAK);
302ab4382d2SGreg Kroah-Hartman if (status == HV_EOK)
303ab4382d2SGreg Kroah-Hartman break;
304ab4382d2SGreg Kroah-Hartman udelay(1);
305ab4382d2SGreg Kroah-Hartman }
306ab4382d2SGreg Kroah-Hartman
307ab4382d2SGreg Kroah-Hartman spin_unlock_irqrestore(&port->lock, flags);
308ab4382d2SGreg Kroah-Hartman }
309ab4382d2SGreg Kroah-Hartman }
310ab4382d2SGreg Kroah-Hartman
311ab4382d2SGreg Kroah-Hartman /* port->lock is not held. */
sunhv_startup(struct uart_port * port)312ab4382d2SGreg Kroah-Hartman static int sunhv_startup(struct uart_port *port)
313ab4382d2SGreg Kroah-Hartman {
314ab4382d2SGreg Kroah-Hartman return 0;
315ab4382d2SGreg Kroah-Hartman }
316ab4382d2SGreg Kroah-Hartman
317ab4382d2SGreg Kroah-Hartman /* port->lock is not held. */
sunhv_shutdown(struct uart_port * port)318ab4382d2SGreg Kroah-Hartman static void sunhv_shutdown(struct uart_port *port)
319ab4382d2SGreg Kroah-Hartman {
320ab4382d2SGreg Kroah-Hartman }
321ab4382d2SGreg Kroah-Hartman
322ab4382d2SGreg Kroah-Hartman /* port->lock is not held. */
sunhv_set_termios(struct uart_port * port,struct ktermios * termios,const struct ktermios * old)323ab4382d2SGreg Kroah-Hartman static void sunhv_set_termios(struct uart_port *port, struct ktermios *termios,
324bec5b814SIlpo Järvinen const struct ktermios *old)
325ab4382d2SGreg Kroah-Hartman {
326ab4382d2SGreg Kroah-Hartman unsigned int baud = uart_get_baud_rate(port, termios, old, 0, 4000000);
327ab4382d2SGreg Kroah-Hartman unsigned int quot = uart_get_divisor(port, baud);
328ab4382d2SGreg Kroah-Hartman unsigned int iflag, cflag;
329ab4382d2SGreg Kroah-Hartman unsigned long flags;
330ab4382d2SGreg Kroah-Hartman
331ab4382d2SGreg Kroah-Hartman spin_lock_irqsave(&port->lock, flags);
332ab4382d2SGreg Kroah-Hartman
333ab4382d2SGreg Kroah-Hartman iflag = termios->c_iflag;
334ab4382d2SGreg Kroah-Hartman cflag = termios->c_cflag;
335ab4382d2SGreg Kroah-Hartman
336ab4382d2SGreg Kroah-Hartman port->ignore_status_mask = 0;
337ab4382d2SGreg Kroah-Hartman if (iflag & IGNBRK)
338ab4382d2SGreg Kroah-Hartman port->ignore_status_mask |= IGNORE_BREAK;
339ab4382d2SGreg Kroah-Hartman if ((cflag & CREAD) == 0)
340ab4382d2SGreg Kroah-Hartman port->ignore_status_mask |= IGNORE_ALL;
341ab4382d2SGreg Kroah-Hartman
342ab4382d2SGreg Kroah-Hartman /* XXX */
343ab4382d2SGreg Kroah-Hartman uart_update_timeout(port, cflag,
344ab4382d2SGreg Kroah-Hartman (port->uartclk / (16 * quot)));
345ab4382d2SGreg Kroah-Hartman
346ab4382d2SGreg Kroah-Hartman spin_unlock_irqrestore(&port->lock, flags);
347ab4382d2SGreg Kroah-Hartman }
348ab4382d2SGreg Kroah-Hartman
sunhv_type(struct uart_port * port)349ab4382d2SGreg Kroah-Hartman static const char *sunhv_type(struct uart_port *port)
350ab4382d2SGreg Kroah-Hartman {
351ab4382d2SGreg Kroah-Hartman return "SUN4V HCONS";
352ab4382d2SGreg Kroah-Hartman }
353ab4382d2SGreg Kroah-Hartman
sunhv_release_port(struct uart_port * port)354ab4382d2SGreg Kroah-Hartman static void sunhv_release_port(struct uart_port *port)
355ab4382d2SGreg Kroah-Hartman {
356ab4382d2SGreg Kroah-Hartman }
357ab4382d2SGreg Kroah-Hartman
sunhv_request_port(struct uart_port * port)358ab4382d2SGreg Kroah-Hartman static int sunhv_request_port(struct uart_port *port)
359ab4382d2SGreg Kroah-Hartman {
360ab4382d2SGreg Kroah-Hartman return 0;
361ab4382d2SGreg Kroah-Hartman }
362ab4382d2SGreg Kroah-Hartman
sunhv_config_port(struct uart_port * port,int flags)363ab4382d2SGreg Kroah-Hartman static void sunhv_config_port(struct uart_port *port, int flags)
364ab4382d2SGreg Kroah-Hartman {
365ab4382d2SGreg Kroah-Hartman }
366ab4382d2SGreg Kroah-Hartman
sunhv_verify_port(struct uart_port * port,struct serial_struct * ser)367ab4382d2SGreg Kroah-Hartman static int sunhv_verify_port(struct uart_port *port, struct serial_struct *ser)
368ab4382d2SGreg Kroah-Hartman {
369ab4382d2SGreg Kroah-Hartman return -EINVAL;
370ab4382d2SGreg Kroah-Hartman }
371ab4382d2SGreg Kroah-Hartman
3722331e068SBhumika Goyal static const struct uart_ops sunhv_pops = {
373ab4382d2SGreg Kroah-Hartman .tx_empty = sunhv_tx_empty,
374ab4382d2SGreg Kroah-Hartman .set_mctrl = sunhv_set_mctrl,
375ab4382d2SGreg Kroah-Hartman .get_mctrl = sunhv_get_mctrl,
376ab4382d2SGreg Kroah-Hartman .stop_tx = sunhv_stop_tx,
377ab4382d2SGreg Kroah-Hartman .start_tx = sunhv_start_tx,
378ab4382d2SGreg Kroah-Hartman .send_xchar = sunhv_send_xchar,
379ab4382d2SGreg Kroah-Hartman .stop_rx = sunhv_stop_rx,
380ab4382d2SGreg Kroah-Hartman .break_ctl = sunhv_break_ctl,
381ab4382d2SGreg Kroah-Hartman .startup = sunhv_startup,
382ab4382d2SGreg Kroah-Hartman .shutdown = sunhv_shutdown,
383ab4382d2SGreg Kroah-Hartman .set_termios = sunhv_set_termios,
384ab4382d2SGreg Kroah-Hartman .type = sunhv_type,
385ab4382d2SGreg Kroah-Hartman .release_port = sunhv_release_port,
386ab4382d2SGreg Kroah-Hartman .request_port = sunhv_request_port,
387ab4382d2SGreg Kroah-Hartman .config_port = sunhv_config_port,
388ab4382d2SGreg Kroah-Hartman .verify_port = sunhv_verify_port,
389ab4382d2SGreg Kroah-Hartman };
390ab4382d2SGreg Kroah-Hartman
391ab4382d2SGreg Kroah-Hartman static struct uart_driver sunhv_reg = {
392ab4382d2SGreg Kroah-Hartman .owner = THIS_MODULE,
393ab4382d2SGreg Kroah-Hartman .driver_name = "sunhv",
39407a6d63eSJohn Paul Adrian Glaubitz .dev_name = "ttyHV",
395ab4382d2SGreg Kroah-Hartman .major = TTY_MAJOR,
396ab4382d2SGreg Kroah-Hartman };
397ab4382d2SGreg Kroah-Hartman
398ab4382d2SGreg Kroah-Hartman static struct uart_port *sunhv_port;
399ab4382d2SGreg Kroah-Hartman
sunhv_migrate_hvcons_irq(int cpu)4007dd4fcf5SVijay Kumar void sunhv_migrate_hvcons_irq(int cpu)
4017dd4fcf5SVijay Kumar {
4027dd4fcf5SVijay Kumar /* Migrate hvcons irq to param cpu */
4037dd4fcf5SVijay Kumar irq_force_affinity(sunhv_port->irq, cpumask_of(cpu));
4047dd4fcf5SVijay Kumar }
4057dd4fcf5SVijay Kumar
406ab4382d2SGreg Kroah-Hartman /* Copy 's' into the con_write_page, decoding "\n" into
407ab4382d2SGreg Kroah-Hartman * "\r\n" along the way. We have to return two lengths
408ab4382d2SGreg Kroah-Hartman * because the caller needs to know how much to advance
409ab4382d2SGreg Kroah-Hartman * 's' and also how many bytes to output via con_write_page.
410ab4382d2SGreg Kroah-Hartman */
fill_con_write_page(const char * s,unsigned int n,unsigned long * page_bytes)411ab4382d2SGreg Kroah-Hartman static int fill_con_write_page(const char *s, unsigned int n,
412ab4382d2SGreg Kroah-Hartman unsigned long *page_bytes)
413ab4382d2SGreg Kroah-Hartman {
414ab4382d2SGreg Kroah-Hartman const char *orig_s = s;
415ab4382d2SGreg Kroah-Hartman char *p = con_write_page;
416ab4382d2SGreg Kroah-Hartman int left = PAGE_SIZE;
417ab4382d2SGreg Kroah-Hartman
418ab4382d2SGreg Kroah-Hartman while (n--) {
419ab4382d2SGreg Kroah-Hartman if (*s == '\n') {
420ab4382d2SGreg Kroah-Hartman if (left < 2)
421ab4382d2SGreg Kroah-Hartman break;
422ab4382d2SGreg Kroah-Hartman *p++ = '\r';
423ab4382d2SGreg Kroah-Hartman left--;
424ab4382d2SGreg Kroah-Hartman } else if (left < 1)
425ab4382d2SGreg Kroah-Hartman break;
426ab4382d2SGreg Kroah-Hartman *p++ = *s++;
427ab4382d2SGreg Kroah-Hartman left--;
428ab4382d2SGreg Kroah-Hartman }
429ab4382d2SGreg Kroah-Hartman *page_bytes = p - con_write_page;
430ab4382d2SGreg Kroah-Hartman return s - orig_s;
431ab4382d2SGreg Kroah-Hartman }
432ab4382d2SGreg Kroah-Hartman
sunhv_console_write_paged(struct console * con,const char * s,unsigned n)433ab4382d2SGreg Kroah-Hartman static void sunhv_console_write_paged(struct console *con, const char *s, unsigned n)
434ab4382d2SGreg Kroah-Hartman {
435ab4382d2SGreg Kroah-Hartman struct uart_port *port = sunhv_port;
436ab4382d2SGreg Kroah-Hartman unsigned long flags;
437ab4382d2SGreg Kroah-Hartman int locked = 1;
438ab4382d2SGreg Kroah-Hartman
439e58e241cSDavid Miller if (port->sysrq || oops_in_progress)
440e58e241cSDavid Miller locked = spin_trylock_irqsave(&port->lock, flags);
441e58e241cSDavid Miller else
442e58e241cSDavid Miller spin_lock_irqsave(&port->lock, flags);
443ab4382d2SGreg Kroah-Hartman
444ab4382d2SGreg Kroah-Hartman while (n > 0) {
445ab4382d2SGreg Kroah-Hartman unsigned long ra = __pa(con_write_page);
446ab4382d2SGreg Kroah-Hartman unsigned long page_bytes;
447ab4382d2SGreg Kroah-Hartman unsigned int cpy = fill_con_write_page(s, n,
448ab4382d2SGreg Kroah-Hartman &page_bytes);
449ab4382d2SGreg Kroah-Hartman
450ab4382d2SGreg Kroah-Hartman n -= cpy;
451ab4382d2SGreg Kroah-Hartman s += cpy;
452ab4382d2SGreg Kroah-Hartman while (page_bytes > 0) {
453ab4382d2SGreg Kroah-Hartman unsigned long written;
454ab4382d2SGreg Kroah-Hartman int limit = 1000000;
455ab4382d2SGreg Kroah-Hartman
456ab4382d2SGreg Kroah-Hartman while (limit--) {
457ab4382d2SGreg Kroah-Hartman unsigned long stat;
458ab4382d2SGreg Kroah-Hartman
459ab4382d2SGreg Kroah-Hartman stat = sun4v_con_write(ra, page_bytes,
460ab4382d2SGreg Kroah-Hartman &written);
461ab4382d2SGreg Kroah-Hartman if (stat == HV_EOK)
462ab4382d2SGreg Kroah-Hartman break;
463ab4382d2SGreg Kroah-Hartman udelay(1);
464ab4382d2SGreg Kroah-Hartman }
465ab4382d2SGreg Kroah-Hartman if (limit < 0)
466ab4382d2SGreg Kroah-Hartman break;
467ab4382d2SGreg Kroah-Hartman page_bytes -= written;
468ab4382d2SGreg Kroah-Hartman ra += written;
469ab4382d2SGreg Kroah-Hartman }
470ab4382d2SGreg Kroah-Hartman }
471ab4382d2SGreg Kroah-Hartman
472ab4382d2SGreg Kroah-Hartman if (locked)
473e58e241cSDavid Miller spin_unlock_irqrestore(&port->lock, flags);
474ab4382d2SGreg Kroah-Hartman }
475ab4382d2SGreg Kroah-Hartman
sunhv_console_putchar(struct uart_port * port,char c)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
sunhv_console_write_bychar(struct console * con,const char * s,unsigned n)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
494e58e241cSDavid Miller if (port->sysrq || oops_in_progress)
495e58e241cSDavid Miller locked = spin_trylock_irqsave(&port->lock, flags);
496e58e241cSDavid Miller else
497e58e241cSDavid Miller spin_lock_irqsave(&port->lock, flags);
498ab4382d2SGreg Kroah-Hartman
499ab4382d2SGreg Kroah-Hartman for (i = 0; i < n; i++) {
500ab4382d2SGreg Kroah-Hartman if (*s == '\n')
501ab4382d2SGreg Kroah-Hartman sunhv_console_putchar(port, '\r');
502ab4382d2SGreg Kroah-Hartman sunhv_console_putchar(port, *s++);
503ab4382d2SGreg Kroah-Hartman }
504ab4382d2SGreg Kroah-Hartman
505ab4382d2SGreg Kroah-Hartman if (locked)
506e58e241cSDavid Miller spin_unlock_irqrestore(&port->lock, flags);
507ab4382d2SGreg Kroah-Hartman }
508ab4382d2SGreg Kroah-Hartman
509ab4382d2SGreg Kroah-Hartman static struct console sunhv_console = {
510ab4382d2SGreg Kroah-Hartman .name = "ttyHV",
511ab4382d2SGreg Kroah-Hartman .write = sunhv_console_write_bychar,
512ab4382d2SGreg Kroah-Hartman .device = uart_console_device,
513ab4382d2SGreg Kroah-Hartman .flags = CON_PRINTBUFFER,
514ab4382d2SGreg Kroah-Hartman .index = -1,
515ab4382d2SGreg Kroah-Hartman .data = &sunhv_reg,
516ab4382d2SGreg Kroah-Hartman };
517ab4382d2SGreg Kroah-Hartman
hv_probe(struct platform_device * op)5189671f099SBill Pemberton static int hv_probe(struct platform_device *op)
519ab4382d2SGreg Kroah-Hartman {
520ab4382d2SGreg Kroah-Hartman struct uart_port *port;
521ab4382d2SGreg Kroah-Hartman unsigned long minor;
522ab4382d2SGreg Kroah-Hartman int err;
523ab4382d2SGreg Kroah-Hartman
524ab4382d2SGreg Kroah-Hartman if (op->archdata.irqs[0] == 0xffffffff)
525ab4382d2SGreg Kroah-Hartman return -ENODEV;
526ab4382d2SGreg Kroah-Hartman
527ab4382d2SGreg Kroah-Hartman port = kzalloc(sizeof(struct uart_port), GFP_KERNEL);
528ab4382d2SGreg Kroah-Hartman if (unlikely(!port))
529ab4382d2SGreg Kroah-Hartman return -ENOMEM;
530ab4382d2SGreg Kroah-Hartman
531ab4382d2SGreg Kroah-Hartman minor = 1;
532ab4382d2SGreg Kroah-Hartman if (sun4v_hvapi_register(HV_GRP_CORE, 1, &minor) == 0 &&
533ab4382d2SGreg Kroah-Hartman minor >= 1) {
534ab4382d2SGreg Kroah-Hartman err = -ENOMEM;
535ab4382d2SGreg Kroah-Hartman con_write_page = kzalloc(PAGE_SIZE, GFP_KERNEL);
536ab4382d2SGreg Kroah-Hartman if (!con_write_page)
537ab4382d2SGreg Kroah-Hartman goto out_free_port;
538ab4382d2SGreg Kroah-Hartman
539ab4382d2SGreg Kroah-Hartman con_read_page = kzalloc(PAGE_SIZE, GFP_KERNEL);
540ab4382d2SGreg Kroah-Hartman if (!con_read_page)
541ab4382d2SGreg Kroah-Hartman goto out_free_con_write_page;
542ab4382d2SGreg Kroah-Hartman
543ab4382d2SGreg Kroah-Hartman sunhv_console.write = sunhv_console_write_paged;
544ab4382d2SGreg Kroah-Hartman sunhv_ops = &bywrite_ops;
545ab4382d2SGreg Kroah-Hartman }
546ab4382d2SGreg Kroah-Hartman
547ab4382d2SGreg Kroah-Hartman sunhv_port = port;
548ab4382d2SGreg Kroah-Hartman
549b071126bSDmitry Safonov port->has_sysrq = 1;
550ab4382d2SGreg Kroah-Hartman port->line = 0;
551ab4382d2SGreg Kroah-Hartman port->ops = &sunhv_pops;
552ab4382d2SGreg Kroah-Hartman port->type = PORT_SUNHV;
553ab4382d2SGreg Kroah-Hartman port->uartclk = ( 29491200 / 16 ); /* arbitrary */
554ab4382d2SGreg Kroah-Hartman
555ab4382d2SGreg Kroah-Hartman port->membase = (unsigned char __iomem *) __pa(port);
556ab4382d2SGreg Kroah-Hartman
557ab4382d2SGreg Kroah-Hartman port->irq = op->archdata.irqs[0];
558ab4382d2SGreg Kroah-Hartman
559ab4382d2SGreg Kroah-Hartman port->dev = &op->dev;
560ab4382d2SGreg Kroah-Hartman
561ab4382d2SGreg Kroah-Hartman err = sunserial_register_minors(&sunhv_reg, 1);
562ab4382d2SGreg Kroah-Hartman if (err)
563ab4382d2SGreg Kroah-Hartman goto out_free_con_read_page;
564ab4382d2SGreg Kroah-Hartman
565ab4382d2SGreg Kroah-Hartman sunserial_console_match(&sunhv_console, op->dev.of_node,
566ab4382d2SGreg Kroah-Hartman &sunhv_reg, port->line, false);
567ab4382d2SGreg Kroah-Hartman
568ab4382d2SGreg Kroah-Hartman err = uart_add_one_port(&sunhv_reg, port);
569ab4382d2SGreg Kroah-Hartman if (err)
570ab4382d2SGreg Kroah-Hartman goto out_unregister_driver;
571ab4382d2SGreg Kroah-Hartman
572ab4382d2SGreg Kroah-Hartman err = request_irq(port->irq, sunhv_interrupt, 0, "hvcons", port);
573ab4382d2SGreg Kroah-Hartman if (err)
574ab4382d2SGreg Kroah-Hartman goto out_remove_port;
575ab4382d2SGreg Kroah-Hartman
576696faeddSJingoo Han platform_set_drvdata(op, port);
577ab4382d2SGreg Kroah-Hartman
578ab4382d2SGreg Kroah-Hartman return 0;
579ab4382d2SGreg Kroah-Hartman
580ab4382d2SGreg Kroah-Hartman out_remove_port:
581ab4382d2SGreg Kroah-Hartman uart_remove_one_port(&sunhv_reg, port);
582ab4382d2SGreg Kroah-Hartman
583ab4382d2SGreg Kroah-Hartman out_unregister_driver:
584ab4382d2SGreg Kroah-Hartman sunserial_unregister_minors(&sunhv_reg, 1);
585ab4382d2SGreg Kroah-Hartman
586ab4382d2SGreg Kroah-Hartman out_free_con_read_page:
587ab4382d2SGreg Kroah-Hartman kfree(con_read_page);
588ab4382d2SGreg Kroah-Hartman
589ab4382d2SGreg Kroah-Hartman out_free_con_write_page:
590ab4382d2SGreg Kroah-Hartman kfree(con_write_page);
591ab4382d2SGreg Kroah-Hartman
592ab4382d2SGreg Kroah-Hartman out_free_port:
593ab4382d2SGreg Kroah-Hartman kfree(port);
594ab4382d2SGreg Kroah-Hartman sunhv_port = NULL;
595ab4382d2SGreg Kroah-Hartman return err;
596ab4382d2SGreg Kroah-Hartman }
597ab4382d2SGreg Kroah-Hartman
hv_remove(struct platform_device * dev)598ae8d8a14SBill Pemberton static int hv_remove(struct platform_device *dev)
599ab4382d2SGreg Kroah-Hartman {
600696faeddSJingoo Han struct uart_port *port = platform_get_drvdata(dev);
601ab4382d2SGreg Kroah-Hartman
602ab4382d2SGreg Kroah-Hartman free_irq(port->irq, port);
603ab4382d2SGreg Kroah-Hartman
604ab4382d2SGreg Kroah-Hartman uart_remove_one_port(&sunhv_reg, port);
605ab4382d2SGreg Kroah-Hartman
606ab4382d2SGreg Kroah-Hartman sunserial_unregister_minors(&sunhv_reg, 1);
607fbb7d2e3SSouptick Joarder kfree(con_read_page);
608fbb7d2e3SSouptick Joarder kfree(con_write_page);
609ab4382d2SGreg Kroah-Hartman kfree(port);
610ab4382d2SGreg Kroah-Hartman sunhv_port = NULL;
611ab4382d2SGreg Kroah-Hartman
612ab4382d2SGreg Kroah-Hartman return 0;
613ab4382d2SGreg Kroah-Hartman }
614ab4382d2SGreg Kroah-Hartman
615ab4382d2SGreg Kroah-Hartman static const struct of_device_id hv_match[] = {
616ab4382d2SGreg Kroah-Hartman {
617ab4382d2SGreg Kroah-Hartman .name = "console",
618ab4382d2SGreg Kroah-Hartman .compatible = "qcn",
619ab4382d2SGreg Kroah-Hartman },
620ab4382d2SGreg Kroah-Hartman {
621ab4382d2SGreg Kroah-Hartman .name = "console",
622ab4382d2SGreg Kroah-Hartman .compatible = "SUNW,sun4v-console",
623ab4382d2SGreg Kroah-Hartman },
624ab4382d2SGreg Kroah-Hartman {},
625ab4382d2SGreg Kroah-Hartman };
626ab4382d2SGreg Kroah-Hartman
627793218dfSGrant Likely static struct platform_driver hv_driver = {
628ab4382d2SGreg Kroah-Hartman .driver = {
629ab4382d2SGreg Kroah-Hartman .name = "hv",
630ab4382d2SGreg Kroah-Hartman .of_match_table = hv_match,
631ab4382d2SGreg Kroah-Hartman },
632ab4382d2SGreg Kroah-Hartman .probe = hv_probe,
6332d47b716SBill Pemberton .remove = hv_remove,
634ab4382d2SGreg Kroah-Hartman };
635ab4382d2SGreg Kroah-Hartman
sunhv_init(void)636ab4382d2SGreg Kroah-Hartman static int __init sunhv_init(void)
637ab4382d2SGreg Kroah-Hartman {
638ab4382d2SGreg Kroah-Hartman if (tlb_type != hypervisor)
639ab4382d2SGreg Kroah-Hartman return -ENODEV;
640ab4382d2SGreg Kroah-Hartman
641793218dfSGrant Likely return platform_driver_register(&hv_driver);
642ab4382d2SGreg Kroah-Hartman }
6434fef5351SPaul Gortmaker device_initcall(sunhv_init);
644ab4382d2SGreg Kroah-Hartman
6454fef5351SPaul Gortmaker #if 0 /* ...def MODULE ; never supported as such */
646ab4382d2SGreg Kroah-Hartman MODULE_AUTHOR("David S. Miller");
647ab4382d2SGreg Kroah-Hartman MODULE_DESCRIPTION("SUN4V Hypervisor console driver");
648ab4382d2SGreg Kroah-Hartman MODULE_VERSION("2.0");
649ab4382d2SGreg Kroah-Hartman MODULE_LICENSE("GPL");
6504fef5351SPaul Gortmaker #endif
651