18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci/* ePAPR hypervisor byte channel device driver
38c2ecf20Sopenharmony_ci *
48c2ecf20Sopenharmony_ci * Copyright 2009-2011 Freescale Semiconductor, Inc.
58c2ecf20Sopenharmony_ci *
68c2ecf20Sopenharmony_ci * Author: Timur Tabi <timur@freescale.com>
78c2ecf20Sopenharmony_ci *
88c2ecf20Sopenharmony_ci * This driver support three distinct interfaces, all of which are related to
98c2ecf20Sopenharmony_ci * ePAPR hypervisor byte channels.
108c2ecf20Sopenharmony_ci *
118c2ecf20Sopenharmony_ci * 1) An early-console (udbg) driver.  This provides early console output
128c2ecf20Sopenharmony_ci * through a byte channel.  The byte channel handle must be specified in a
138c2ecf20Sopenharmony_ci * Kconfig option.
148c2ecf20Sopenharmony_ci *
158c2ecf20Sopenharmony_ci * 2) A normal console driver.  Output is sent to the byte channel designated
168c2ecf20Sopenharmony_ci * for stdout in the device tree.  The console driver is for handling kernel
178c2ecf20Sopenharmony_ci * printk calls.
188c2ecf20Sopenharmony_ci *
198c2ecf20Sopenharmony_ci * 3) A tty driver, which is used to handle user-space input and output.  The
208c2ecf20Sopenharmony_ci * byte channel used for the console is designated as the default tty.
218c2ecf20Sopenharmony_ci */
228c2ecf20Sopenharmony_ci
238c2ecf20Sopenharmony_ci#include <linux/init.h>
248c2ecf20Sopenharmony_ci#include <linux/slab.h>
258c2ecf20Sopenharmony_ci#include <linux/err.h>
268c2ecf20Sopenharmony_ci#include <linux/interrupt.h>
278c2ecf20Sopenharmony_ci#include <linux/fs.h>
288c2ecf20Sopenharmony_ci#include <linux/poll.h>
298c2ecf20Sopenharmony_ci#include <asm/epapr_hcalls.h>
308c2ecf20Sopenharmony_ci#include <linux/of.h>
318c2ecf20Sopenharmony_ci#include <linux/of_irq.h>
328c2ecf20Sopenharmony_ci#include <linux/platform_device.h>
338c2ecf20Sopenharmony_ci#include <linux/cdev.h>
348c2ecf20Sopenharmony_ci#include <linux/console.h>
358c2ecf20Sopenharmony_ci#include <linux/tty.h>
368c2ecf20Sopenharmony_ci#include <linux/tty_flip.h>
378c2ecf20Sopenharmony_ci#include <linux/circ_buf.h>
388c2ecf20Sopenharmony_ci#include <asm/udbg.h>
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_ci/* The size of the transmit circular buffer.  This must be a power of two. */
418c2ecf20Sopenharmony_ci#define BUF_SIZE	2048
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_ci/* Per-byte channel private data */
448c2ecf20Sopenharmony_cistruct ehv_bc_data {
458c2ecf20Sopenharmony_ci	struct device *dev;
468c2ecf20Sopenharmony_ci	struct tty_port port;
478c2ecf20Sopenharmony_ci	uint32_t handle;
488c2ecf20Sopenharmony_ci	unsigned int rx_irq;
498c2ecf20Sopenharmony_ci	unsigned int tx_irq;
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_ci	spinlock_t lock;	/* lock for transmit buffer */
528c2ecf20Sopenharmony_ci	unsigned char buf[BUF_SIZE];	/* transmit circular buffer */
538c2ecf20Sopenharmony_ci	unsigned int head;	/* circular buffer head */
548c2ecf20Sopenharmony_ci	unsigned int tail;	/* circular buffer tail */
558c2ecf20Sopenharmony_ci
568c2ecf20Sopenharmony_ci	int tx_irq_enabled;	/* true == TX interrupt is enabled */
578c2ecf20Sopenharmony_ci};
588c2ecf20Sopenharmony_ci
598c2ecf20Sopenharmony_ci/* Array of byte channel objects */
608c2ecf20Sopenharmony_cistatic struct ehv_bc_data *bcs;
618c2ecf20Sopenharmony_ci
628c2ecf20Sopenharmony_ci/* Byte channel handle for stdout (and stdin), taken from device tree */
638c2ecf20Sopenharmony_cistatic unsigned int stdout_bc;
648c2ecf20Sopenharmony_ci
658c2ecf20Sopenharmony_ci/* Virtual IRQ for the byte channel handle for stdin, taken from device tree */
668c2ecf20Sopenharmony_cistatic unsigned int stdout_irq;
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_ci/**************************** SUPPORT FUNCTIONS ****************************/
698c2ecf20Sopenharmony_ci
708c2ecf20Sopenharmony_ci/*
718c2ecf20Sopenharmony_ci * Enable the transmit interrupt
728c2ecf20Sopenharmony_ci *
738c2ecf20Sopenharmony_ci * Unlike a serial device, byte channels have no mechanism for disabling their
748c2ecf20Sopenharmony_ci * own receive or transmit interrupts.  To emulate that feature, we toggle
758c2ecf20Sopenharmony_ci * the IRQ in the kernel.
768c2ecf20Sopenharmony_ci *
778c2ecf20Sopenharmony_ci * We cannot just blindly call enable_irq() or disable_irq(), because these
788c2ecf20Sopenharmony_ci * calls are reference counted.  This means that we cannot call enable_irq()
798c2ecf20Sopenharmony_ci * if interrupts are already enabled.  This can happen in two situations:
808c2ecf20Sopenharmony_ci *
818c2ecf20Sopenharmony_ci * 1. The tty layer makes two back-to-back calls to ehv_bc_tty_write()
828c2ecf20Sopenharmony_ci * 2. A transmit interrupt occurs while executing ehv_bc_tx_dequeue()
838c2ecf20Sopenharmony_ci *
848c2ecf20Sopenharmony_ci * To work around this, we keep a flag to tell us if the IRQ is enabled or not.
858c2ecf20Sopenharmony_ci */
868c2ecf20Sopenharmony_cistatic void enable_tx_interrupt(struct ehv_bc_data *bc)
878c2ecf20Sopenharmony_ci{
888c2ecf20Sopenharmony_ci	if (!bc->tx_irq_enabled) {
898c2ecf20Sopenharmony_ci		enable_irq(bc->tx_irq);
908c2ecf20Sopenharmony_ci		bc->tx_irq_enabled = 1;
918c2ecf20Sopenharmony_ci	}
928c2ecf20Sopenharmony_ci}
938c2ecf20Sopenharmony_ci
948c2ecf20Sopenharmony_cistatic void disable_tx_interrupt(struct ehv_bc_data *bc)
958c2ecf20Sopenharmony_ci{
968c2ecf20Sopenharmony_ci	if (bc->tx_irq_enabled) {
978c2ecf20Sopenharmony_ci		disable_irq_nosync(bc->tx_irq);
988c2ecf20Sopenharmony_ci		bc->tx_irq_enabled = 0;
998c2ecf20Sopenharmony_ci	}
1008c2ecf20Sopenharmony_ci}
1018c2ecf20Sopenharmony_ci
1028c2ecf20Sopenharmony_ci/*
1038c2ecf20Sopenharmony_ci * find the byte channel handle to use for the console
1048c2ecf20Sopenharmony_ci *
1058c2ecf20Sopenharmony_ci * The byte channel to be used for the console is specified via a "stdout"
1068c2ecf20Sopenharmony_ci * property in the /chosen node.
1078c2ecf20Sopenharmony_ci */
1088c2ecf20Sopenharmony_cistatic int find_console_handle(void)
1098c2ecf20Sopenharmony_ci{
1108c2ecf20Sopenharmony_ci	struct device_node *np = of_stdout;
1118c2ecf20Sopenharmony_ci	const uint32_t *iprop;
1128c2ecf20Sopenharmony_ci
1138c2ecf20Sopenharmony_ci	/* We don't care what the aliased node is actually called.  We only
1148c2ecf20Sopenharmony_ci	 * care if it's compatible with "epapr,hv-byte-channel", because that
1158c2ecf20Sopenharmony_ci	 * indicates that it's a byte channel node.
1168c2ecf20Sopenharmony_ci	 */
1178c2ecf20Sopenharmony_ci	if (!np || !of_device_is_compatible(np, "epapr,hv-byte-channel"))
1188c2ecf20Sopenharmony_ci		return 0;
1198c2ecf20Sopenharmony_ci
1208c2ecf20Sopenharmony_ci	stdout_irq = irq_of_parse_and_map(np, 0);
1218c2ecf20Sopenharmony_ci	if (stdout_irq == NO_IRQ) {
1228c2ecf20Sopenharmony_ci		pr_err("ehv-bc: no 'interrupts' property in %pOF node\n", np);
1238c2ecf20Sopenharmony_ci		return 0;
1248c2ecf20Sopenharmony_ci	}
1258c2ecf20Sopenharmony_ci
1268c2ecf20Sopenharmony_ci	/*
1278c2ecf20Sopenharmony_ci	 * The 'hv-handle' property contains the handle for this byte channel.
1288c2ecf20Sopenharmony_ci	 */
1298c2ecf20Sopenharmony_ci	iprop = of_get_property(np, "hv-handle", NULL);
1308c2ecf20Sopenharmony_ci	if (!iprop) {
1318c2ecf20Sopenharmony_ci		pr_err("ehv-bc: no 'hv-handle' property in %pOFn node\n",
1328c2ecf20Sopenharmony_ci		       np);
1338c2ecf20Sopenharmony_ci		return 0;
1348c2ecf20Sopenharmony_ci	}
1358c2ecf20Sopenharmony_ci	stdout_bc = be32_to_cpu(*iprop);
1368c2ecf20Sopenharmony_ci	return 1;
1378c2ecf20Sopenharmony_ci}
1388c2ecf20Sopenharmony_ci
1398c2ecf20Sopenharmony_cistatic unsigned int local_ev_byte_channel_send(unsigned int handle,
1408c2ecf20Sopenharmony_ci					       unsigned int *count,
1418c2ecf20Sopenharmony_ci					       const char *p)
1428c2ecf20Sopenharmony_ci{
1438c2ecf20Sopenharmony_ci	char buffer[EV_BYTE_CHANNEL_MAX_BYTES];
1448c2ecf20Sopenharmony_ci	unsigned int c = *count;
1458c2ecf20Sopenharmony_ci
1468c2ecf20Sopenharmony_ci	if (c < sizeof(buffer)) {
1478c2ecf20Sopenharmony_ci		memcpy(buffer, p, c);
1488c2ecf20Sopenharmony_ci		memset(&buffer[c], 0, sizeof(buffer) - c);
1498c2ecf20Sopenharmony_ci		p = buffer;
1508c2ecf20Sopenharmony_ci	}
1518c2ecf20Sopenharmony_ci	return ev_byte_channel_send(handle, count, p);
1528c2ecf20Sopenharmony_ci}
1538c2ecf20Sopenharmony_ci
1548c2ecf20Sopenharmony_ci/*************************** EARLY CONSOLE DRIVER ***************************/
1558c2ecf20Sopenharmony_ci
1568c2ecf20Sopenharmony_ci#ifdef CONFIG_PPC_EARLY_DEBUG_EHV_BC
1578c2ecf20Sopenharmony_ci
1588c2ecf20Sopenharmony_ci/*
1598c2ecf20Sopenharmony_ci * send a byte to a byte channel, wait if necessary
1608c2ecf20Sopenharmony_ci *
1618c2ecf20Sopenharmony_ci * This function sends a byte to a byte channel, and it waits and
1628c2ecf20Sopenharmony_ci * retries if the byte channel is full.  It returns if the character
1638c2ecf20Sopenharmony_ci * has been sent, or if some error has occurred.
1648c2ecf20Sopenharmony_ci *
1658c2ecf20Sopenharmony_ci */
1668c2ecf20Sopenharmony_cistatic void byte_channel_spin_send(const char data)
1678c2ecf20Sopenharmony_ci{
1688c2ecf20Sopenharmony_ci	int ret, count;
1698c2ecf20Sopenharmony_ci
1708c2ecf20Sopenharmony_ci	do {
1718c2ecf20Sopenharmony_ci		count = 1;
1728c2ecf20Sopenharmony_ci		ret = local_ev_byte_channel_send(CONFIG_PPC_EARLY_DEBUG_EHV_BC_HANDLE,
1738c2ecf20Sopenharmony_ci					   &count, &data);
1748c2ecf20Sopenharmony_ci	} while (ret == EV_EAGAIN);
1758c2ecf20Sopenharmony_ci}
1768c2ecf20Sopenharmony_ci
1778c2ecf20Sopenharmony_ci/*
1788c2ecf20Sopenharmony_ci * The udbg subsystem calls this function to display a single character.
1798c2ecf20Sopenharmony_ci * We convert CR to a CR/LF.
1808c2ecf20Sopenharmony_ci */
1818c2ecf20Sopenharmony_cistatic void ehv_bc_udbg_putc(char c)
1828c2ecf20Sopenharmony_ci{
1838c2ecf20Sopenharmony_ci	if (c == '\n')
1848c2ecf20Sopenharmony_ci		byte_channel_spin_send('\r');
1858c2ecf20Sopenharmony_ci
1868c2ecf20Sopenharmony_ci	byte_channel_spin_send(c);
1878c2ecf20Sopenharmony_ci}
1888c2ecf20Sopenharmony_ci
1898c2ecf20Sopenharmony_ci/*
1908c2ecf20Sopenharmony_ci * early console initialization
1918c2ecf20Sopenharmony_ci *
1928c2ecf20Sopenharmony_ci * PowerPC kernels support an early printk console, also known as udbg.
1938c2ecf20Sopenharmony_ci * This function must be called via the ppc_md.init_early function pointer.
1948c2ecf20Sopenharmony_ci * At this point, the device tree has been unflattened, so we can obtain the
1958c2ecf20Sopenharmony_ci * byte channel handle for stdout.
1968c2ecf20Sopenharmony_ci *
1978c2ecf20Sopenharmony_ci * We only support displaying of characters (putc).  We do not support
1988c2ecf20Sopenharmony_ci * keyboard input.
1998c2ecf20Sopenharmony_ci */
2008c2ecf20Sopenharmony_civoid __init udbg_init_ehv_bc(void)
2018c2ecf20Sopenharmony_ci{
2028c2ecf20Sopenharmony_ci	unsigned int rx_count, tx_count;
2038c2ecf20Sopenharmony_ci	unsigned int ret;
2048c2ecf20Sopenharmony_ci
2058c2ecf20Sopenharmony_ci	/* Verify the byte channel handle */
2068c2ecf20Sopenharmony_ci	ret = ev_byte_channel_poll(CONFIG_PPC_EARLY_DEBUG_EHV_BC_HANDLE,
2078c2ecf20Sopenharmony_ci				   &rx_count, &tx_count);
2088c2ecf20Sopenharmony_ci	if (ret)
2098c2ecf20Sopenharmony_ci		return;
2108c2ecf20Sopenharmony_ci
2118c2ecf20Sopenharmony_ci	udbg_putc = ehv_bc_udbg_putc;
2128c2ecf20Sopenharmony_ci	register_early_udbg_console();
2138c2ecf20Sopenharmony_ci
2148c2ecf20Sopenharmony_ci	udbg_printf("ehv-bc: early console using byte channel handle %u\n",
2158c2ecf20Sopenharmony_ci		    CONFIG_PPC_EARLY_DEBUG_EHV_BC_HANDLE);
2168c2ecf20Sopenharmony_ci}
2178c2ecf20Sopenharmony_ci
2188c2ecf20Sopenharmony_ci#endif
2198c2ecf20Sopenharmony_ci
2208c2ecf20Sopenharmony_ci/****************************** CONSOLE DRIVER ******************************/
2218c2ecf20Sopenharmony_ci
2228c2ecf20Sopenharmony_cistatic struct tty_driver *ehv_bc_driver;
2238c2ecf20Sopenharmony_ci
2248c2ecf20Sopenharmony_ci/*
2258c2ecf20Sopenharmony_ci * Byte channel console sending worker function.
2268c2ecf20Sopenharmony_ci *
2278c2ecf20Sopenharmony_ci * For consoles, if the output buffer is full, we should just spin until it
2288c2ecf20Sopenharmony_ci * clears.
2298c2ecf20Sopenharmony_ci */
2308c2ecf20Sopenharmony_cistatic int ehv_bc_console_byte_channel_send(unsigned int handle, const char *s,
2318c2ecf20Sopenharmony_ci			     unsigned int count)
2328c2ecf20Sopenharmony_ci{
2338c2ecf20Sopenharmony_ci	unsigned int len;
2348c2ecf20Sopenharmony_ci	int ret = 0;
2358c2ecf20Sopenharmony_ci
2368c2ecf20Sopenharmony_ci	while (count) {
2378c2ecf20Sopenharmony_ci		len = min_t(unsigned int, count, EV_BYTE_CHANNEL_MAX_BYTES);
2388c2ecf20Sopenharmony_ci		do {
2398c2ecf20Sopenharmony_ci			ret = local_ev_byte_channel_send(handle, &len, s);
2408c2ecf20Sopenharmony_ci		} while (ret == EV_EAGAIN);
2418c2ecf20Sopenharmony_ci		count -= len;
2428c2ecf20Sopenharmony_ci		s += len;
2438c2ecf20Sopenharmony_ci	}
2448c2ecf20Sopenharmony_ci
2458c2ecf20Sopenharmony_ci	return ret;
2468c2ecf20Sopenharmony_ci}
2478c2ecf20Sopenharmony_ci
2488c2ecf20Sopenharmony_ci/*
2498c2ecf20Sopenharmony_ci * write a string to the console
2508c2ecf20Sopenharmony_ci *
2518c2ecf20Sopenharmony_ci * This function gets called to write a string from the kernel, typically from
2528c2ecf20Sopenharmony_ci * a printk().  This function spins until all data is written.
2538c2ecf20Sopenharmony_ci *
2548c2ecf20Sopenharmony_ci * We copy the data to a temporary buffer because we need to insert a \r in
2558c2ecf20Sopenharmony_ci * front of every \n.  It's more efficient to copy the data to the buffer than
2568c2ecf20Sopenharmony_ci * it is to make multiple hcalls for each character or each newline.
2578c2ecf20Sopenharmony_ci */
2588c2ecf20Sopenharmony_cistatic void ehv_bc_console_write(struct console *co, const char *s,
2598c2ecf20Sopenharmony_ci				 unsigned int count)
2608c2ecf20Sopenharmony_ci{
2618c2ecf20Sopenharmony_ci	char s2[EV_BYTE_CHANNEL_MAX_BYTES];
2628c2ecf20Sopenharmony_ci	unsigned int i, j = 0;
2638c2ecf20Sopenharmony_ci	char c;
2648c2ecf20Sopenharmony_ci
2658c2ecf20Sopenharmony_ci	for (i = 0; i < count; i++) {
2668c2ecf20Sopenharmony_ci		c = *s++;
2678c2ecf20Sopenharmony_ci
2688c2ecf20Sopenharmony_ci		if (c == '\n')
2698c2ecf20Sopenharmony_ci			s2[j++] = '\r';
2708c2ecf20Sopenharmony_ci
2718c2ecf20Sopenharmony_ci		s2[j++] = c;
2728c2ecf20Sopenharmony_ci		if (j >= (EV_BYTE_CHANNEL_MAX_BYTES - 1)) {
2738c2ecf20Sopenharmony_ci			if (ehv_bc_console_byte_channel_send(stdout_bc, s2, j))
2748c2ecf20Sopenharmony_ci				return;
2758c2ecf20Sopenharmony_ci			j = 0;
2768c2ecf20Sopenharmony_ci		}
2778c2ecf20Sopenharmony_ci	}
2788c2ecf20Sopenharmony_ci
2798c2ecf20Sopenharmony_ci	if (j)
2808c2ecf20Sopenharmony_ci		ehv_bc_console_byte_channel_send(stdout_bc, s2, j);
2818c2ecf20Sopenharmony_ci}
2828c2ecf20Sopenharmony_ci
2838c2ecf20Sopenharmony_ci/*
2848c2ecf20Sopenharmony_ci * When /dev/console is opened, the kernel iterates the console list looking
2858c2ecf20Sopenharmony_ci * for one with ->device and then calls that method. On success, it expects
2868c2ecf20Sopenharmony_ci * the passed-in int* to contain the minor number to use.
2878c2ecf20Sopenharmony_ci */
2888c2ecf20Sopenharmony_cistatic struct tty_driver *ehv_bc_console_device(struct console *co, int *index)
2898c2ecf20Sopenharmony_ci{
2908c2ecf20Sopenharmony_ci	*index = co->index;
2918c2ecf20Sopenharmony_ci
2928c2ecf20Sopenharmony_ci	return ehv_bc_driver;
2938c2ecf20Sopenharmony_ci}
2948c2ecf20Sopenharmony_ci
2958c2ecf20Sopenharmony_cistatic struct console ehv_bc_console = {
2968c2ecf20Sopenharmony_ci	.name		= "ttyEHV",
2978c2ecf20Sopenharmony_ci	.write		= ehv_bc_console_write,
2988c2ecf20Sopenharmony_ci	.device		= ehv_bc_console_device,
2998c2ecf20Sopenharmony_ci	.flags		= CON_PRINTBUFFER | CON_ENABLED,
3008c2ecf20Sopenharmony_ci};
3018c2ecf20Sopenharmony_ci
3028c2ecf20Sopenharmony_ci/*
3038c2ecf20Sopenharmony_ci * Console initialization
3048c2ecf20Sopenharmony_ci *
3058c2ecf20Sopenharmony_ci * This is the first function that is called after the device tree is
3068c2ecf20Sopenharmony_ci * available, so here is where we determine the byte channel handle and IRQ for
3078c2ecf20Sopenharmony_ci * stdout/stdin, even though that information is used by the tty and character
3088c2ecf20Sopenharmony_ci * drivers.
3098c2ecf20Sopenharmony_ci */
3108c2ecf20Sopenharmony_cistatic int __init ehv_bc_console_init(void)
3118c2ecf20Sopenharmony_ci{
3128c2ecf20Sopenharmony_ci	if (!find_console_handle()) {
3138c2ecf20Sopenharmony_ci		pr_debug("ehv-bc: stdout is not a byte channel\n");
3148c2ecf20Sopenharmony_ci		return -ENODEV;
3158c2ecf20Sopenharmony_ci	}
3168c2ecf20Sopenharmony_ci
3178c2ecf20Sopenharmony_ci#ifdef CONFIG_PPC_EARLY_DEBUG_EHV_BC
3188c2ecf20Sopenharmony_ci	/* Print a friendly warning if the user chose the wrong byte channel
3198c2ecf20Sopenharmony_ci	 * handle for udbg.
3208c2ecf20Sopenharmony_ci	 */
3218c2ecf20Sopenharmony_ci	if (stdout_bc != CONFIG_PPC_EARLY_DEBUG_EHV_BC_HANDLE)
3228c2ecf20Sopenharmony_ci		pr_warn("ehv-bc: udbg handle %u is not the stdout handle\n",
3238c2ecf20Sopenharmony_ci			CONFIG_PPC_EARLY_DEBUG_EHV_BC_HANDLE);
3248c2ecf20Sopenharmony_ci#endif
3258c2ecf20Sopenharmony_ci
3268c2ecf20Sopenharmony_ci	/* add_preferred_console() must be called before register_console(),
3278c2ecf20Sopenharmony_ci	   otherwise it won't work.  However, we don't want to enumerate all the
3288c2ecf20Sopenharmony_ci	   byte channels here, either, since we only care about one. */
3298c2ecf20Sopenharmony_ci
3308c2ecf20Sopenharmony_ci	add_preferred_console(ehv_bc_console.name, ehv_bc_console.index, NULL);
3318c2ecf20Sopenharmony_ci	register_console(&ehv_bc_console);
3328c2ecf20Sopenharmony_ci
3338c2ecf20Sopenharmony_ci	pr_info("ehv-bc: registered console driver for byte channel %u\n",
3348c2ecf20Sopenharmony_ci		stdout_bc);
3358c2ecf20Sopenharmony_ci
3368c2ecf20Sopenharmony_ci	return 0;
3378c2ecf20Sopenharmony_ci}
3388c2ecf20Sopenharmony_ciconsole_initcall(ehv_bc_console_init);
3398c2ecf20Sopenharmony_ci
3408c2ecf20Sopenharmony_ci/******************************** TTY DRIVER ********************************/
3418c2ecf20Sopenharmony_ci
3428c2ecf20Sopenharmony_ci/*
3438c2ecf20Sopenharmony_ci * byte channel receive interrupt handler
3448c2ecf20Sopenharmony_ci *
3458c2ecf20Sopenharmony_ci * This ISR is called whenever data is available on a byte channel.
3468c2ecf20Sopenharmony_ci */
3478c2ecf20Sopenharmony_cistatic irqreturn_t ehv_bc_tty_rx_isr(int irq, void *data)
3488c2ecf20Sopenharmony_ci{
3498c2ecf20Sopenharmony_ci	struct ehv_bc_data *bc = data;
3508c2ecf20Sopenharmony_ci	unsigned int rx_count, tx_count, len;
3518c2ecf20Sopenharmony_ci	int count;
3528c2ecf20Sopenharmony_ci	char buffer[EV_BYTE_CHANNEL_MAX_BYTES];
3538c2ecf20Sopenharmony_ci	int ret;
3548c2ecf20Sopenharmony_ci
3558c2ecf20Sopenharmony_ci	/* Find out how much data needs to be read, and then ask the TTY layer
3568c2ecf20Sopenharmony_ci	 * if it can handle that much.  We want to ensure that every byte we
3578c2ecf20Sopenharmony_ci	 * read from the byte channel will be accepted by the TTY layer.
3588c2ecf20Sopenharmony_ci	 */
3598c2ecf20Sopenharmony_ci	ev_byte_channel_poll(bc->handle, &rx_count, &tx_count);
3608c2ecf20Sopenharmony_ci	count = tty_buffer_request_room(&bc->port, rx_count);
3618c2ecf20Sopenharmony_ci
3628c2ecf20Sopenharmony_ci	/* 'count' is the maximum amount of data the TTY layer can accept at
3638c2ecf20Sopenharmony_ci	 * this time.  However, during testing, I was never able to get 'count'
3648c2ecf20Sopenharmony_ci	 * to be less than 'rx_count'.  I'm not sure whether I'm calling it
3658c2ecf20Sopenharmony_ci	 * correctly.
3668c2ecf20Sopenharmony_ci	 */
3678c2ecf20Sopenharmony_ci
3688c2ecf20Sopenharmony_ci	while (count > 0) {
3698c2ecf20Sopenharmony_ci		len = min_t(unsigned int, count, sizeof(buffer));
3708c2ecf20Sopenharmony_ci
3718c2ecf20Sopenharmony_ci		/* Read some data from the byte channel.  This function will
3728c2ecf20Sopenharmony_ci		 * never return more than EV_BYTE_CHANNEL_MAX_BYTES bytes.
3738c2ecf20Sopenharmony_ci		 */
3748c2ecf20Sopenharmony_ci		ev_byte_channel_receive(bc->handle, &len, buffer);
3758c2ecf20Sopenharmony_ci
3768c2ecf20Sopenharmony_ci		/* 'len' is now the amount of data that's been received. 'len'
3778c2ecf20Sopenharmony_ci		 * can't be zero, and most likely it's equal to one.
3788c2ecf20Sopenharmony_ci		 */
3798c2ecf20Sopenharmony_ci
3808c2ecf20Sopenharmony_ci		/* Pass the received data to the tty layer. */
3818c2ecf20Sopenharmony_ci		ret = tty_insert_flip_string(&bc->port, buffer, len);
3828c2ecf20Sopenharmony_ci
3838c2ecf20Sopenharmony_ci		/* 'ret' is the number of bytes that the TTY layer accepted.
3848c2ecf20Sopenharmony_ci		 * If it's not equal to 'len', then it means the buffer is
3858c2ecf20Sopenharmony_ci		 * full, which should never happen.  If it does happen, we can
3868c2ecf20Sopenharmony_ci		 * exit gracefully, but we drop the last 'len - ret' characters
3878c2ecf20Sopenharmony_ci		 * that we read from the byte channel.
3888c2ecf20Sopenharmony_ci		 */
3898c2ecf20Sopenharmony_ci		if (ret != len)
3908c2ecf20Sopenharmony_ci			break;
3918c2ecf20Sopenharmony_ci
3928c2ecf20Sopenharmony_ci		count -= len;
3938c2ecf20Sopenharmony_ci	}
3948c2ecf20Sopenharmony_ci
3958c2ecf20Sopenharmony_ci	/* Tell the tty layer that we're done. */
3968c2ecf20Sopenharmony_ci	tty_flip_buffer_push(&bc->port);
3978c2ecf20Sopenharmony_ci
3988c2ecf20Sopenharmony_ci	return IRQ_HANDLED;
3998c2ecf20Sopenharmony_ci}
4008c2ecf20Sopenharmony_ci
4018c2ecf20Sopenharmony_ci/*
4028c2ecf20Sopenharmony_ci * dequeue the transmit buffer to the hypervisor
4038c2ecf20Sopenharmony_ci *
4048c2ecf20Sopenharmony_ci * This function, which can be called in interrupt context, dequeues as much
4058c2ecf20Sopenharmony_ci * data as possible from the transmit buffer to the byte channel.
4068c2ecf20Sopenharmony_ci */
4078c2ecf20Sopenharmony_cistatic void ehv_bc_tx_dequeue(struct ehv_bc_data *bc)
4088c2ecf20Sopenharmony_ci{
4098c2ecf20Sopenharmony_ci	unsigned int count;
4108c2ecf20Sopenharmony_ci	unsigned int len, ret;
4118c2ecf20Sopenharmony_ci	unsigned long flags;
4128c2ecf20Sopenharmony_ci
4138c2ecf20Sopenharmony_ci	do {
4148c2ecf20Sopenharmony_ci		spin_lock_irqsave(&bc->lock, flags);
4158c2ecf20Sopenharmony_ci		len = min_t(unsigned int,
4168c2ecf20Sopenharmony_ci			    CIRC_CNT_TO_END(bc->head, bc->tail, BUF_SIZE),
4178c2ecf20Sopenharmony_ci			    EV_BYTE_CHANNEL_MAX_BYTES);
4188c2ecf20Sopenharmony_ci
4198c2ecf20Sopenharmony_ci		ret = local_ev_byte_channel_send(bc->handle, &len, bc->buf + bc->tail);
4208c2ecf20Sopenharmony_ci
4218c2ecf20Sopenharmony_ci		/* 'len' is valid only if the return code is 0 or EV_EAGAIN */
4228c2ecf20Sopenharmony_ci		if (!ret || (ret == EV_EAGAIN))
4238c2ecf20Sopenharmony_ci			bc->tail = (bc->tail + len) & (BUF_SIZE - 1);
4248c2ecf20Sopenharmony_ci
4258c2ecf20Sopenharmony_ci		count = CIRC_CNT(bc->head, bc->tail, BUF_SIZE);
4268c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(&bc->lock, flags);
4278c2ecf20Sopenharmony_ci	} while (count && !ret);
4288c2ecf20Sopenharmony_ci
4298c2ecf20Sopenharmony_ci	spin_lock_irqsave(&bc->lock, flags);
4308c2ecf20Sopenharmony_ci	if (CIRC_CNT(bc->head, bc->tail, BUF_SIZE))
4318c2ecf20Sopenharmony_ci		/*
4328c2ecf20Sopenharmony_ci		 * If we haven't emptied the buffer, then enable the TX IRQ.
4338c2ecf20Sopenharmony_ci		 * We'll get an interrupt when there's more room in the
4348c2ecf20Sopenharmony_ci		 * hypervisor's output buffer.
4358c2ecf20Sopenharmony_ci		 */
4368c2ecf20Sopenharmony_ci		enable_tx_interrupt(bc);
4378c2ecf20Sopenharmony_ci	else
4388c2ecf20Sopenharmony_ci		disable_tx_interrupt(bc);
4398c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&bc->lock, flags);
4408c2ecf20Sopenharmony_ci}
4418c2ecf20Sopenharmony_ci
4428c2ecf20Sopenharmony_ci/*
4438c2ecf20Sopenharmony_ci * byte channel transmit interrupt handler
4448c2ecf20Sopenharmony_ci *
4458c2ecf20Sopenharmony_ci * This ISR is called whenever space becomes available for transmitting
4468c2ecf20Sopenharmony_ci * characters on a byte channel.
4478c2ecf20Sopenharmony_ci */
4488c2ecf20Sopenharmony_cistatic irqreturn_t ehv_bc_tty_tx_isr(int irq, void *data)
4498c2ecf20Sopenharmony_ci{
4508c2ecf20Sopenharmony_ci	struct ehv_bc_data *bc = data;
4518c2ecf20Sopenharmony_ci
4528c2ecf20Sopenharmony_ci	ehv_bc_tx_dequeue(bc);
4538c2ecf20Sopenharmony_ci	tty_port_tty_wakeup(&bc->port);
4548c2ecf20Sopenharmony_ci
4558c2ecf20Sopenharmony_ci	return IRQ_HANDLED;
4568c2ecf20Sopenharmony_ci}
4578c2ecf20Sopenharmony_ci
4588c2ecf20Sopenharmony_ci/*
4598c2ecf20Sopenharmony_ci * This function is called when the tty layer has data for us send.  We store
4608c2ecf20Sopenharmony_ci * the data first in a circular buffer, and then dequeue as much of that data
4618c2ecf20Sopenharmony_ci * as possible.
4628c2ecf20Sopenharmony_ci *
4638c2ecf20Sopenharmony_ci * We don't need to worry about whether there is enough room in the buffer for
4648c2ecf20Sopenharmony_ci * all the data.  The purpose of ehv_bc_tty_write_room() is to tell the tty
4658c2ecf20Sopenharmony_ci * layer how much data it can safely send to us.  We guarantee that
4668c2ecf20Sopenharmony_ci * ehv_bc_tty_write_room() will never lie, so the tty layer will never send us
4678c2ecf20Sopenharmony_ci * too much data.
4688c2ecf20Sopenharmony_ci */
4698c2ecf20Sopenharmony_cistatic int ehv_bc_tty_write(struct tty_struct *ttys, const unsigned char *s,
4708c2ecf20Sopenharmony_ci			    int count)
4718c2ecf20Sopenharmony_ci{
4728c2ecf20Sopenharmony_ci	struct ehv_bc_data *bc = ttys->driver_data;
4738c2ecf20Sopenharmony_ci	unsigned long flags;
4748c2ecf20Sopenharmony_ci	unsigned int len;
4758c2ecf20Sopenharmony_ci	unsigned int written = 0;
4768c2ecf20Sopenharmony_ci
4778c2ecf20Sopenharmony_ci	while (1) {
4788c2ecf20Sopenharmony_ci		spin_lock_irqsave(&bc->lock, flags);
4798c2ecf20Sopenharmony_ci		len = CIRC_SPACE_TO_END(bc->head, bc->tail, BUF_SIZE);
4808c2ecf20Sopenharmony_ci		if (count < len)
4818c2ecf20Sopenharmony_ci			len = count;
4828c2ecf20Sopenharmony_ci		if (len) {
4838c2ecf20Sopenharmony_ci			memcpy(bc->buf + bc->head, s, len);
4848c2ecf20Sopenharmony_ci			bc->head = (bc->head + len) & (BUF_SIZE - 1);
4858c2ecf20Sopenharmony_ci		}
4868c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(&bc->lock, flags);
4878c2ecf20Sopenharmony_ci		if (!len)
4888c2ecf20Sopenharmony_ci			break;
4898c2ecf20Sopenharmony_ci
4908c2ecf20Sopenharmony_ci		s += len;
4918c2ecf20Sopenharmony_ci		count -= len;
4928c2ecf20Sopenharmony_ci		written += len;
4938c2ecf20Sopenharmony_ci	}
4948c2ecf20Sopenharmony_ci
4958c2ecf20Sopenharmony_ci	ehv_bc_tx_dequeue(bc);
4968c2ecf20Sopenharmony_ci
4978c2ecf20Sopenharmony_ci	return written;
4988c2ecf20Sopenharmony_ci}
4998c2ecf20Sopenharmony_ci
5008c2ecf20Sopenharmony_ci/*
5018c2ecf20Sopenharmony_ci * This function can be called multiple times for a given tty_struct, which is
5028c2ecf20Sopenharmony_ci * why we initialize bc->ttys in ehv_bc_tty_port_activate() instead.
5038c2ecf20Sopenharmony_ci *
5048c2ecf20Sopenharmony_ci * The tty layer will still call this function even if the device was not
5058c2ecf20Sopenharmony_ci * registered (i.e. tty_register_device() was not called).  This happens
5068c2ecf20Sopenharmony_ci * because tty_register_device() is optional and some legacy drivers don't
5078c2ecf20Sopenharmony_ci * use it.  So we need to check for that.
5088c2ecf20Sopenharmony_ci */
5098c2ecf20Sopenharmony_cistatic int ehv_bc_tty_open(struct tty_struct *ttys, struct file *filp)
5108c2ecf20Sopenharmony_ci{
5118c2ecf20Sopenharmony_ci	struct ehv_bc_data *bc = &bcs[ttys->index];
5128c2ecf20Sopenharmony_ci
5138c2ecf20Sopenharmony_ci	if (!bc->dev)
5148c2ecf20Sopenharmony_ci		return -ENODEV;
5158c2ecf20Sopenharmony_ci
5168c2ecf20Sopenharmony_ci	return tty_port_open(&bc->port, ttys, filp);
5178c2ecf20Sopenharmony_ci}
5188c2ecf20Sopenharmony_ci
5198c2ecf20Sopenharmony_ci/*
5208c2ecf20Sopenharmony_ci * Amazingly, if ehv_bc_tty_open() returns an error code, the tty layer will
5218c2ecf20Sopenharmony_ci * still call this function to close the tty device.  So we can't assume that
5228c2ecf20Sopenharmony_ci * the tty port has been initialized.
5238c2ecf20Sopenharmony_ci */
5248c2ecf20Sopenharmony_cistatic void ehv_bc_tty_close(struct tty_struct *ttys, struct file *filp)
5258c2ecf20Sopenharmony_ci{
5268c2ecf20Sopenharmony_ci	struct ehv_bc_data *bc = &bcs[ttys->index];
5278c2ecf20Sopenharmony_ci
5288c2ecf20Sopenharmony_ci	if (bc->dev)
5298c2ecf20Sopenharmony_ci		tty_port_close(&bc->port, ttys, filp);
5308c2ecf20Sopenharmony_ci}
5318c2ecf20Sopenharmony_ci
5328c2ecf20Sopenharmony_ci/*
5338c2ecf20Sopenharmony_ci * Return the amount of space in the output buffer
5348c2ecf20Sopenharmony_ci *
5358c2ecf20Sopenharmony_ci * This is actually a contract between the driver and the tty layer outlining
5368c2ecf20Sopenharmony_ci * how much write room the driver can guarantee will be sent OR BUFFERED.  This
5378c2ecf20Sopenharmony_ci * driver MUST honor the return value.
5388c2ecf20Sopenharmony_ci */
5398c2ecf20Sopenharmony_cistatic int ehv_bc_tty_write_room(struct tty_struct *ttys)
5408c2ecf20Sopenharmony_ci{
5418c2ecf20Sopenharmony_ci	struct ehv_bc_data *bc = ttys->driver_data;
5428c2ecf20Sopenharmony_ci	unsigned long flags;
5438c2ecf20Sopenharmony_ci	int count;
5448c2ecf20Sopenharmony_ci
5458c2ecf20Sopenharmony_ci	spin_lock_irqsave(&bc->lock, flags);
5468c2ecf20Sopenharmony_ci	count = CIRC_SPACE(bc->head, bc->tail, BUF_SIZE);
5478c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&bc->lock, flags);
5488c2ecf20Sopenharmony_ci
5498c2ecf20Sopenharmony_ci	return count;
5508c2ecf20Sopenharmony_ci}
5518c2ecf20Sopenharmony_ci
5528c2ecf20Sopenharmony_ci/*
5538c2ecf20Sopenharmony_ci * Stop sending data to the tty layer
5548c2ecf20Sopenharmony_ci *
5558c2ecf20Sopenharmony_ci * This function is called when the tty layer's input buffers are getting full,
5568c2ecf20Sopenharmony_ci * so the driver should stop sending it data.  The easiest way to do this is to
5578c2ecf20Sopenharmony_ci * disable the RX IRQ, which will prevent ehv_bc_tty_rx_isr() from being
5588c2ecf20Sopenharmony_ci * called.
5598c2ecf20Sopenharmony_ci *
5608c2ecf20Sopenharmony_ci * The hypervisor will continue to queue up any incoming data.  If there is any
5618c2ecf20Sopenharmony_ci * data in the queue when the RX interrupt is enabled, we'll immediately get an
5628c2ecf20Sopenharmony_ci * RX interrupt.
5638c2ecf20Sopenharmony_ci */
5648c2ecf20Sopenharmony_cistatic void ehv_bc_tty_throttle(struct tty_struct *ttys)
5658c2ecf20Sopenharmony_ci{
5668c2ecf20Sopenharmony_ci	struct ehv_bc_data *bc = ttys->driver_data;
5678c2ecf20Sopenharmony_ci
5688c2ecf20Sopenharmony_ci	disable_irq(bc->rx_irq);
5698c2ecf20Sopenharmony_ci}
5708c2ecf20Sopenharmony_ci
5718c2ecf20Sopenharmony_ci/*
5728c2ecf20Sopenharmony_ci * Resume sending data to the tty layer
5738c2ecf20Sopenharmony_ci *
5748c2ecf20Sopenharmony_ci * This function is called after previously calling ehv_bc_tty_throttle().  The
5758c2ecf20Sopenharmony_ci * tty layer's input buffers now have more room, so the driver can resume
5768c2ecf20Sopenharmony_ci * sending it data.
5778c2ecf20Sopenharmony_ci */
5788c2ecf20Sopenharmony_cistatic void ehv_bc_tty_unthrottle(struct tty_struct *ttys)
5798c2ecf20Sopenharmony_ci{
5808c2ecf20Sopenharmony_ci	struct ehv_bc_data *bc = ttys->driver_data;
5818c2ecf20Sopenharmony_ci
5828c2ecf20Sopenharmony_ci	/* If there is any data in the queue when the RX interrupt is enabled,
5838c2ecf20Sopenharmony_ci	 * we'll immediately get an RX interrupt.
5848c2ecf20Sopenharmony_ci	 */
5858c2ecf20Sopenharmony_ci	enable_irq(bc->rx_irq);
5868c2ecf20Sopenharmony_ci}
5878c2ecf20Sopenharmony_ci
5888c2ecf20Sopenharmony_cistatic void ehv_bc_tty_hangup(struct tty_struct *ttys)
5898c2ecf20Sopenharmony_ci{
5908c2ecf20Sopenharmony_ci	struct ehv_bc_data *bc = ttys->driver_data;
5918c2ecf20Sopenharmony_ci
5928c2ecf20Sopenharmony_ci	ehv_bc_tx_dequeue(bc);
5938c2ecf20Sopenharmony_ci	tty_port_hangup(&bc->port);
5948c2ecf20Sopenharmony_ci}
5958c2ecf20Sopenharmony_ci
5968c2ecf20Sopenharmony_ci/*
5978c2ecf20Sopenharmony_ci * TTY driver operations
5988c2ecf20Sopenharmony_ci *
5998c2ecf20Sopenharmony_ci * If we could ask the hypervisor how much data is still in the TX buffer, or
6008c2ecf20Sopenharmony_ci * at least how big the TX buffers are, then we could implement the
6018c2ecf20Sopenharmony_ci * .wait_until_sent and .chars_in_buffer functions.
6028c2ecf20Sopenharmony_ci */
6038c2ecf20Sopenharmony_cistatic const struct tty_operations ehv_bc_ops = {
6048c2ecf20Sopenharmony_ci	.open		= ehv_bc_tty_open,
6058c2ecf20Sopenharmony_ci	.close		= ehv_bc_tty_close,
6068c2ecf20Sopenharmony_ci	.write		= ehv_bc_tty_write,
6078c2ecf20Sopenharmony_ci	.write_room	= ehv_bc_tty_write_room,
6088c2ecf20Sopenharmony_ci	.throttle	= ehv_bc_tty_throttle,
6098c2ecf20Sopenharmony_ci	.unthrottle	= ehv_bc_tty_unthrottle,
6108c2ecf20Sopenharmony_ci	.hangup		= ehv_bc_tty_hangup,
6118c2ecf20Sopenharmony_ci};
6128c2ecf20Sopenharmony_ci
6138c2ecf20Sopenharmony_ci/*
6148c2ecf20Sopenharmony_ci * initialize the TTY port
6158c2ecf20Sopenharmony_ci *
6168c2ecf20Sopenharmony_ci * This function will only be called once, no matter how many times
6178c2ecf20Sopenharmony_ci * ehv_bc_tty_open() is called.  That's why we register the ISR here, and also
6188c2ecf20Sopenharmony_ci * why we initialize tty_struct-related variables here.
6198c2ecf20Sopenharmony_ci */
6208c2ecf20Sopenharmony_cistatic int ehv_bc_tty_port_activate(struct tty_port *port,
6218c2ecf20Sopenharmony_ci				    struct tty_struct *ttys)
6228c2ecf20Sopenharmony_ci{
6238c2ecf20Sopenharmony_ci	struct ehv_bc_data *bc = container_of(port, struct ehv_bc_data, port);
6248c2ecf20Sopenharmony_ci	int ret;
6258c2ecf20Sopenharmony_ci
6268c2ecf20Sopenharmony_ci	ttys->driver_data = bc;
6278c2ecf20Sopenharmony_ci
6288c2ecf20Sopenharmony_ci	ret = request_irq(bc->rx_irq, ehv_bc_tty_rx_isr, 0, "ehv-bc", bc);
6298c2ecf20Sopenharmony_ci	if (ret < 0) {
6308c2ecf20Sopenharmony_ci		dev_err(bc->dev, "could not request rx irq %u (ret=%i)\n",
6318c2ecf20Sopenharmony_ci		       bc->rx_irq, ret);
6328c2ecf20Sopenharmony_ci		return ret;
6338c2ecf20Sopenharmony_ci	}
6348c2ecf20Sopenharmony_ci
6358c2ecf20Sopenharmony_ci	/* request_irq also enables the IRQ */
6368c2ecf20Sopenharmony_ci	bc->tx_irq_enabled = 1;
6378c2ecf20Sopenharmony_ci
6388c2ecf20Sopenharmony_ci	ret = request_irq(bc->tx_irq, ehv_bc_tty_tx_isr, 0, "ehv-bc", bc);
6398c2ecf20Sopenharmony_ci	if (ret < 0) {
6408c2ecf20Sopenharmony_ci		dev_err(bc->dev, "could not request tx irq %u (ret=%i)\n",
6418c2ecf20Sopenharmony_ci		       bc->tx_irq, ret);
6428c2ecf20Sopenharmony_ci		free_irq(bc->rx_irq, bc);
6438c2ecf20Sopenharmony_ci		return ret;
6448c2ecf20Sopenharmony_ci	}
6458c2ecf20Sopenharmony_ci
6468c2ecf20Sopenharmony_ci	/* The TX IRQ is enabled only when we can't write all the data to the
6478c2ecf20Sopenharmony_ci	 * byte channel at once, so by default it's disabled.
6488c2ecf20Sopenharmony_ci	 */
6498c2ecf20Sopenharmony_ci	disable_tx_interrupt(bc);
6508c2ecf20Sopenharmony_ci
6518c2ecf20Sopenharmony_ci	return 0;
6528c2ecf20Sopenharmony_ci}
6538c2ecf20Sopenharmony_ci
6548c2ecf20Sopenharmony_cistatic void ehv_bc_tty_port_shutdown(struct tty_port *port)
6558c2ecf20Sopenharmony_ci{
6568c2ecf20Sopenharmony_ci	struct ehv_bc_data *bc = container_of(port, struct ehv_bc_data, port);
6578c2ecf20Sopenharmony_ci
6588c2ecf20Sopenharmony_ci	free_irq(bc->tx_irq, bc);
6598c2ecf20Sopenharmony_ci	free_irq(bc->rx_irq, bc);
6608c2ecf20Sopenharmony_ci}
6618c2ecf20Sopenharmony_ci
6628c2ecf20Sopenharmony_cistatic const struct tty_port_operations ehv_bc_tty_port_ops = {
6638c2ecf20Sopenharmony_ci	.activate = ehv_bc_tty_port_activate,
6648c2ecf20Sopenharmony_ci	.shutdown = ehv_bc_tty_port_shutdown,
6658c2ecf20Sopenharmony_ci};
6668c2ecf20Sopenharmony_ci
6678c2ecf20Sopenharmony_cistatic int ehv_bc_tty_probe(struct platform_device *pdev)
6688c2ecf20Sopenharmony_ci{
6698c2ecf20Sopenharmony_ci	struct device_node *np = pdev->dev.of_node;
6708c2ecf20Sopenharmony_ci	struct ehv_bc_data *bc;
6718c2ecf20Sopenharmony_ci	const uint32_t *iprop;
6728c2ecf20Sopenharmony_ci	unsigned int handle;
6738c2ecf20Sopenharmony_ci	int ret;
6748c2ecf20Sopenharmony_ci	static unsigned int index = 1;
6758c2ecf20Sopenharmony_ci	unsigned int i;
6768c2ecf20Sopenharmony_ci
6778c2ecf20Sopenharmony_ci	iprop = of_get_property(np, "hv-handle", NULL);
6788c2ecf20Sopenharmony_ci	if (!iprop) {
6798c2ecf20Sopenharmony_ci		dev_err(&pdev->dev, "no 'hv-handle' property in %pOFn node\n",
6808c2ecf20Sopenharmony_ci			np);
6818c2ecf20Sopenharmony_ci		return -ENODEV;
6828c2ecf20Sopenharmony_ci	}
6838c2ecf20Sopenharmony_ci
6848c2ecf20Sopenharmony_ci	/* We already told the console layer that the index for the console
6858c2ecf20Sopenharmony_ci	 * device is zero, so we need to make sure that we use that index when
6868c2ecf20Sopenharmony_ci	 * we probe the console byte channel node.
6878c2ecf20Sopenharmony_ci	 */
6888c2ecf20Sopenharmony_ci	handle = be32_to_cpu(*iprop);
6898c2ecf20Sopenharmony_ci	i = (handle == stdout_bc) ? 0 : index++;
6908c2ecf20Sopenharmony_ci	bc = &bcs[i];
6918c2ecf20Sopenharmony_ci
6928c2ecf20Sopenharmony_ci	bc->handle = handle;
6938c2ecf20Sopenharmony_ci	bc->head = 0;
6948c2ecf20Sopenharmony_ci	bc->tail = 0;
6958c2ecf20Sopenharmony_ci	spin_lock_init(&bc->lock);
6968c2ecf20Sopenharmony_ci
6978c2ecf20Sopenharmony_ci	bc->rx_irq = irq_of_parse_and_map(np, 0);
6988c2ecf20Sopenharmony_ci	bc->tx_irq = irq_of_parse_and_map(np, 1);
6998c2ecf20Sopenharmony_ci	if ((bc->rx_irq == NO_IRQ) || (bc->tx_irq == NO_IRQ)) {
7008c2ecf20Sopenharmony_ci		dev_err(&pdev->dev, "no 'interrupts' property in %pOFn node\n",
7018c2ecf20Sopenharmony_ci			np);
7028c2ecf20Sopenharmony_ci		ret = -ENODEV;
7038c2ecf20Sopenharmony_ci		goto error;
7048c2ecf20Sopenharmony_ci	}
7058c2ecf20Sopenharmony_ci
7068c2ecf20Sopenharmony_ci	tty_port_init(&bc->port);
7078c2ecf20Sopenharmony_ci	bc->port.ops = &ehv_bc_tty_port_ops;
7088c2ecf20Sopenharmony_ci
7098c2ecf20Sopenharmony_ci	bc->dev = tty_port_register_device(&bc->port, ehv_bc_driver, i,
7108c2ecf20Sopenharmony_ci			&pdev->dev);
7118c2ecf20Sopenharmony_ci	if (IS_ERR(bc->dev)) {
7128c2ecf20Sopenharmony_ci		ret = PTR_ERR(bc->dev);
7138c2ecf20Sopenharmony_ci		dev_err(&pdev->dev, "could not register tty (ret=%i)\n", ret);
7148c2ecf20Sopenharmony_ci		goto error;
7158c2ecf20Sopenharmony_ci	}
7168c2ecf20Sopenharmony_ci
7178c2ecf20Sopenharmony_ci	dev_set_drvdata(&pdev->dev, bc);
7188c2ecf20Sopenharmony_ci
7198c2ecf20Sopenharmony_ci	dev_info(&pdev->dev, "registered /dev/%s%u for byte channel %u\n",
7208c2ecf20Sopenharmony_ci		ehv_bc_driver->name, i, bc->handle);
7218c2ecf20Sopenharmony_ci
7228c2ecf20Sopenharmony_ci	return 0;
7238c2ecf20Sopenharmony_ci
7248c2ecf20Sopenharmony_cierror:
7258c2ecf20Sopenharmony_ci	tty_port_destroy(&bc->port);
7268c2ecf20Sopenharmony_ci	irq_dispose_mapping(bc->tx_irq);
7278c2ecf20Sopenharmony_ci	irq_dispose_mapping(bc->rx_irq);
7288c2ecf20Sopenharmony_ci
7298c2ecf20Sopenharmony_ci	memset(bc, 0, sizeof(struct ehv_bc_data));
7308c2ecf20Sopenharmony_ci	return ret;
7318c2ecf20Sopenharmony_ci}
7328c2ecf20Sopenharmony_ci
7338c2ecf20Sopenharmony_cistatic const struct of_device_id ehv_bc_tty_of_ids[] = {
7348c2ecf20Sopenharmony_ci	{ .compatible = "epapr,hv-byte-channel" },
7358c2ecf20Sopenharmony_ci	{}
7368c2ecf20Sopenharmony_ci};
7378c2ecf20Sopenharmony_ci
7388c2ecf20Sopenharmony_cistatic struct platform_driver ehv_bc_tty_driver = {
7398c2ecf20Sopenharmony_ci	.driver = {
7408c2ecf20Sopenharmony_ci		.name = "ehv-bc",
7418c2ecf20Sopenharmony_ci		.of_match_table = ehv_bc_tty_of_ids,
7428c2ecf20Sopenharmony_ci		.suppress_bind_attrs = true,
7438c2ecf20Sopenharmony_ci	},
7448c2ecf20Sopenharmony_ci	.probe		= ehv_bc_tty_probe,
7458c2ecf20Sopenharmony_ci};
7468c2ecf20Sopenharmony_ci
7478c2ecf20Sopenharmony_ci/**
7488c2ecf20Sopenharmony_ci * ehv_bc_init - ePAPR hypervisor byte channel driver initialization
7498c2ecf20Sopenharmony_ci *
7508c2ecf20Sopenharmony_ci * This function is called when this driver is loaded.
7518c2ecf20Sopenharmony_ci */
7528c2ecf20Sopenharmony_cistatic int __init ehv_bc_init(void)
7538c2ecf20Sopenharmony_ci{
7548c2ecf20Sopenharmony_ci	struct device_node *np;
7558c2ecf20Sopenharmony_ci	unsigned int count = 0; /* Number of elements in bcs[] */
7568c2ecf20Sopenharmony_ci	int ret;
7578c2ecf20Sopenharmony_ci
7588c2ecf20Sopenharmony_ci	pr_info("ePAPR hypervisor byte channel driver\n");
7598c2ecf20Sopenharmony_ci
7608c2ecf20Sopenharmony_ci	/* Count the number of byte channels */
7618c2ecf20Sopenharmony_ci	for_each_compatible_node(np, NULL, "epapr,hv-byte-channel")
7628c2ecf20Sopenharmony_ci		count++;
7638c2ecf20Sopenharmony_ci
7648c2ecf20Sopenharmony_ci	if (!count)
7658c2ecf20Sopenharmony_ci		return -ENODEV;
7668c2ecf20Sopenharmony_ci
7678c2ecf20Sopenharmony_ci	/* The array index of an element in bcs[] is the same as the tty index
7688c2ecf20Sopenharmony_ci	 * for that element.  If you know the address of an element in the
7698c2ecf20Sopenharmony_ci	 * array, then you can use pointer math (e.g. "bc - bcs") to get its
7708c2ecf20Sopenharmony_ci	 * tty index.
7718c2ecf20Sopenharmony_ci	 */
7728c2ecf20Sopenharmony_ci	bcs = kcalloc(count, sizeof(struct ehv_bc_data), GFP_KERNEL);
7738c2ecf20Sopenharmony_ci	if (!bcs)
7748c2ecf20Sopenharmony_ci		return -ENOMEM;
7758c2ecf20Sopenharmony_ci
7768c2ecf20Sopenharmony_ci	ehv_bc_driver = alloc_tty_driver(count);
7778c2ecf20Sopenharmony_ci	if (!ehv_bc_driver) {
7788c2ecf20Sopenharmony_ci		ret = -ENOMEM;
7798c2ecf20Sopenharmony_ci		goto err_free_bcs;
7808c2ecf20Sopenharmony_ci	}
7818c2ecf20Sopenharmony_ci
7828c2ecf20Sopenharmony_ci	ehv_bc_driver->driver_name = "ehv-bc";
7838c2ecf20Sopenharmony_ci	ehv_bc_driver->name = ehv_bc_console.name;
7848c2ecf20Sopenharmony_ci	ehv_bc_driver->type = TTY_DRIVER_TYPE_CONSOLE;
7858c2ecf20Sopenharmony_ci	ehv_bc_driver->subtype = SYSTEM_TYPE_CONSOLE;
7868c2ecf20Sopenharmony_ci	ehv_bc_driver->init_termios = tty_std_termios;
7878c2ecf20Sopenharmony_ci	ehv_bc_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
7888c2ecf20Sopenharmony_ci	tty_set_operations(ehv_bc_driver, &ehv_bc_ops);
7898c2ecf20Sopenharmony_ci
7908c2ecf20Sopenharmony_ci	ret = tty_register_driver(ehv_bc_driver);
7918c2ecf20Sopenharmony_ci	if (ret) {
7928c2ecf20Sopenharmony_ci		pr_err("ehv-bc: could not register tty driver (ret=%i)\n", ret);
7938c2ecf20Sopenharmony_ci		goto err_put_tty_driver;
7948c2ecf20Sopenharmony_ci	}
7958c2ecf20Sopenharmony_ci
7968c2ecf20Sopenharmony_ci	ret = platform_driver_register(&ehv_bc_tty_driver);
7978c2ecf20Sopenharmony_ci	if (ret) {
7988c2ecf20Sopenharmony_ci		pr_err("ehv-bc: could not register platform driver (ret=%i)\n",
7998c2ecf20Sopenharmony_ci		       ret);
8008c2ecf20Sopenharmony_ci		goto err_deregister_tty_driver;
8018c2ecf20Sopenharmony_ci	}
8028c2ecf20Sopenharmony_ci
8038c2ecf20Sopenharmony_ci	return 0;
8048c2ecf20Sopenharmony_ci
8058c2ecf20Sopenharmony_cierr_deregister_tty_driver:
8068c2ecf20Sopenharmony_ci	tty_unregister_driver(ehv_bc_driver);
8078c2ecf20Sopenharmony_cierr_put_tty_driver:
8088c2ecf20Sopenharmony_ci	put_tty_driver(ehv_bc_driver);
8098c2ecf20Sopenharmony_cierr_free_bcs:
8108c2ecf20Sopenharmony_ci	kfree(bcs);
8118c2ecf20Sopenharmony_ci
8128c2ecf20Sopenharmony_ci	return ret;
8138c2ecf20Sopenharmony_ci}
8148c2ecf20Sopenharmony_cidevice_initcall(ehv_bc_init);
815