Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * Copyright (C) 2007 Google, Inc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  * Copyright (C) 2012 Intel, Inc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * Copyright (C) 2017 Imagination Technologies Ltd.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) #include <linux/console.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include <linux/interrupt.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <linux/platform_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <linux/tty.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <linux/tty_flip.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <linux/io.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <linux/mod_devicetable.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include <linux/goldfish.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include <linux/mm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #include <linux/dma-mapping.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #include <linux/serial_core.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) /* Goldfish tty register's offsets */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) #define	GOLDFISH_TTY_REG_BYTES_READY	0x04
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) #define	GOLDFISH_TTY_REG_CMD		0x08
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) #define	GOLDFISH_TTY_REG_DATA_PTR	0x10
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) #define	GOLDFISH_TTY_REG_DATA_LEN	0x14
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) #define	GOLDFISH_TTY_REG_DATA_PTR_HIGH	0x18
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) #define	GOLDFISH_TTY_REG_VERSION	0x20
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) /* Goldfish tty commands */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) #define	GOLDFISH_TTY_CMD_INT_DISABLE	0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) #define	GOLDFISH_TTY_CMD_INT_ENABLE	1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) #define	GOLDFISH_TTY_CMD_WRITE_BUFFER	2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) #define	GOLDFISH_TTY_CMD_READ_BUFFER	3
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) struct goldfish_tty {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 	struct tty_port port;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 	spinlock_t lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 	void __iomem *base;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 	u32 irq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 	int opencount;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 	struct console console;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 	u32 version;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 	struct device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) static DEFINE_MUTEX(goldfish_tty_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) static struct tty_driver *goldfish_tty_driver;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) static u32 goldfish_tty_line_count = 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) static u32 goldfish_tty_current_line_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) static struct goldfish_tty *goldfish_ttys;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) static void do_rw_io(struct goldfish_tty *qtty,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 		     unsigned long address,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 		     unsigned int count,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 		     int is_write)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 	unsigned long irq_flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 	void __iomem *base = qtty->base;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 	spin_lock_irqsave(&qtty->lock, irq_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 	gf_write_ptr((void *)address, base + GOLDFISH_TTY_REG_DATA_PTR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 		     base + GOLDFISH_TTY_REG_DATA_PTR_HIGH);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 	writel(count, base + GOLDFISH_TTY_REG_DATA_LEN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 	if (is_write)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 		writel(GOLDFISH_TTY_CMD_WRITE_BUFFER,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 		       base + GOLDFISH_TTY_REG_CMD);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 		writel(GOLDFISH_TTY_CMD_READ_BUFFER,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 		       base + GOLDFISH_TTY_REG_CMD);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 	spin_unlock_irqrestore(&qtty->lock, irq_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) static void goldfish_tty_rw(struct goldfish_tty *qtty,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 			    unsigned long addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 			    unsigned int count,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 			    int is_write)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 	dma_addr_t dma_handle;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	enum dma_data_direction dma_dir;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 	dma_dir = (is_write ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 	if (qtty->version > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 		 * Goldfish TTY for Ranchu platform uses
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 		 * physical addresses and DMA for read/write operations
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 		unsigned long addr_end = addr + count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 		while (addr < addr_end) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 			unsigned long pg_end = (addr & PAGE_MASK) + PAGE_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 			unsigned long next =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 					pg_end < addr_end ? pg_end : addr_end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 			unsigned long avail = next - addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 			 * Map the buffer's virtual address to the DMA address
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 			 * so the buffer can be accessed by the device.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 			dma_handle = dma_map_single(qtty->dev, (void *)addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 						    avail, dma_dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 			if (dma_mapping_error(qtty->dev, dma_handle)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 				dev_err(qtty->dev, "tty: DMA mapping error.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 				return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 			do_rw_io(qtty, dma_handle, avail, is_write);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 			 * Unmap the previously mapped region after
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 			 * the completion of the read/write operation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 			dma_unmap_single(qtty->dev, dma_handle, avail, dma_dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 			addr += avail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 		 * Old style Goldfish TTY used on the Goldfish platform
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 		 * uses virtual addresses.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 		do_rw_io(qtty, addr, count, is_write);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) static void goldfish_tty_do_write(int line, const char *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 				  unsigned int count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 	struct goldfish_tty *qtty = &goldfish_ttys[line];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 	unsigned long address = (unsigned long)(void *)buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 	goldfish_tty_rw(qtty, address, count, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) static irqreturn_t goldfish_tty_interrupt(int irq, void *dev_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 	struct goldfish_tty *qtty = dev_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 	void __iomem *base = qtty->base;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 	unsigned long address;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 	unsigned char *buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 	u32 count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 	count = readl(base + GOLDFISH_TTY_REG_BYTES_READY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 	if (count == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 		return IRQ_NONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 	count = tty_prepare_flip_string(&qtty->port, &buf, count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 	address = (unsigned long)(void *)buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 	goldfish_tty_rw(qtty, address, count, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 	tty_schedule_flip(&qtty->port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 	return IRQ_HANDLED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) static int goldfish_tty_activate(struct tty_port *port, struct tty_struct *tty)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 	struct goldfish_tty *qtty = container_of(port, struct goldfish_tty,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 									port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 	writel(GOLDFISH_TTY_CMD_INT_ENABLE, qtty->base + GOLDFISH_TTY_REG_CMD);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) static void goldfish_tty_shutdown(struct tty_port *port)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 	struct goldfish_tty *qtty = container_of(port, struct goldfish_tty,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 									port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 	writel(GOLDFISH_TTY_CMD_INT_DISABLE, qtty->base + GOLDFISH_TTY_REG_CMD);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) static int goldfish_tty_open(struct tty_struct *tty, struct file *filp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 	struct goldfish_tty *qtty = &goldfish_ttys[tty->index];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 	return tty_port_open(&qtty->port, tty, filp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) static void goldfish_tty_close(struct tty_struct *tty, struct file *filp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 	tty_port_close(tty->port, tty, filp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) static void goldfish_tty_hangup(struct tty_struct *tty)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 	tty_port_hangup(tty->port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) static int goldfish_tty_write(struct tty_struct *tty, const unsigned char *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 								int count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 	goldfish_tty_do_write(tty->index, buf, count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 	return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) static int goldfish_tty_write_room(struct tty_struct *tty)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 	return 0x10000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) static int goldfish_tty_chars_in_buffer(struct tty_struct *tty)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 	struct goldfish_tty *qtty = &goldfish_ttys[tty->index];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 	void __iomem *base = qtty->base;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 	return readl(base + GOLDFISH_TTY_REG_BYTES_READY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) static void goldfish_tty_console_write(struct console *co, const char *b,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 								unsigned count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 	goldfish_tty_do_write(co->index, b, count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) static struct tty_driver *goldfish_tty_console_device(struct console *c,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 								int *index)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 	*index = c->index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 	return goldfish_tty_driver;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) static int goldfish_tty_console_setup(struct console *co, char *options)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 	if ((unsigned)co->index >= goldfish_tty_line_count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 	if (!goldfish_ttys[co->index].base)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) static const struct tty_port_operations goldfish_port_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 	.activate = goldfish_tty_activate,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 	.shutdown = goldfish_tty_shutdown
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) static const struct tty_operations goldfish_tty_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 	.open = goldfish_tty_open,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 	.close = goldfish_tty_close,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 	.hangup = goldfish_tty_hangup,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 	.write = goldfish_tty_write,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 	.write_room = goldfish_tty_write_room,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 	.chars_in_buffer = goldfish_tty_chars_in_buffer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) static int goldfish_tty_create_driver(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 	struct tty_driver *tty;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 	goldfish_ttys = kcalloc(goldfish_tty_line_count,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 				sizeof(*goldfish_ttys),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 				GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 	if (goldfish_ttys == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 		ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 		goto err_alloc_goldfish_ttys_failed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 	tty = alloc_tty_driver(goldfish_tty_line_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 	if (tty == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 		ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 		goto err_alloc_tty_driver_failed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 	tty->driver_name = "goldfish";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 	tty->name = "ttyGF";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 	tty->type = TTY_DRIVER_TYPE_SERIAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 	tty->subtype = SERIAL_TYPE_NORMAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 	tty->init_termios = tty_std_termios;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 	tty->flags = TTY_DRIVER_RESET_TERMIOS | TTY_DRIVER_REAL_RAW |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 						TTY_DRIVER_DYNAMIC_DEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 	tty_set_operations(tty, &goldfish_tty_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 	ret = tty_register_driver(tty);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 		goto err_tty_register_driver_failed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 	goldfish_tty_driver = tty;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) err_tty_register_driver_failed:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 	put_tty_driver(tty);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) err_alloc_tty_driver_failed:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 	kfree(goldfish_ttys);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 	goldfish_ttys = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) err_alloc_goldfish_ttys_failed:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) static void goldfish_tty_delete_driver(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 	tty_unregister_driver(goldfish_tty_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 	put_tty_driver(goldfish_tty_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 	goldfish_tty_driver = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 	kfree(goldfish_ttys);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 	goldfish_ttys = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) static int goldfish_tty_probe(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 	struct goldfish_tty *qtty;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 	int ret = -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 	struct resource *r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 	struct device *ttydev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 	void __iomem *base;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 	u32 irq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 	unsigned int line;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 	r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 	if (!r) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 		pr_err("goldfish_tty: No MEM resource available!\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 	base = ioremap(r->start, 0x1000);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 	if (!base) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 		pr_err("goldfish_tty: Unable to ioremap base!\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 	r = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 	if (!r) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 		pr_err("goldfish_tty: No IRQ resource available!\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 		goto err_unmap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 	irq = r->start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 	mutex_lock(&goldfish_tty_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 	if (pdev->id == PLATFORM_DEVID_NONE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 		line = goldfish_tty_current_line_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 		line = pdev->id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 	if (line >= goldfish_tty_line_count) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 		pr_err("goldfish_tty: Reached maximum tty number of %d.\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 		       goldfish_tty_current_line_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 		ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 		goto err_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 	if (goldfish_tty_current_line_count == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 		ret = goldfish_tty_create_driver();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 			goto err_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 	goldfish_tty_current_line_count++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 	qtty = &goldfish_ttys[line];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 	spin_lock_init(&qtty->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 	tty_port_init(&qtty->port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 	qtty->port.ops = &goldfish_port_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 	qtty->base = base;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 	qtty->irq = irq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 	qtty->dev = &pdev->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 	 * Goldfish TTY device used by the Goldfish emulator
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 	 * should identify itself with 0, forcing the driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 	 * to use virtual addresses. Goldfish TTY device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 	 * on Ranchu emulator (qemu2) returns 1 here and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 	 * driver will use physical addresses.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 	qtty->version = readl(base + GOLDFISH_TTY_REG_VERSION);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 	 * Goldfish TTY device on Ranchu emulator (qemu2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 	 * will use DMA for read/write IO operations.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 	if (qtty->version > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 		 * Initialize dma_mask to 32-bits.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 		if (!pdev->dev.dma_mask)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 			pdev->dev.dma_mask = &pdev->dev.coherent_dma_mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 		ret = dma_set_mask(&pdev->dev, DMA_BIT_MASK(32));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 		if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 			dev_err(&pdev->dev, "No suitable DMA available.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 			goto err_dec_line_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 	writel(GOLDFISH_TTY_CMD_INT_DISABLE, base + GOLDFISH_TTY_REG_CMD);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) 	ret = request_irq(irq, goldfish_tty_interrupt, IRQF_SHARED,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 			  "goldfish_tty", qtty);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) 		pr_err("goldfish_tty: No IRQ available!\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) 		goto err_dec_line_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) 	ttydev = tty_port_register_device(&qtty->port, goldfish_tty_driver,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 					  line, &pdev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) 	if (IS_ERR(ttydev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) 		ret = PTR_ERR(ttydev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) 		goto err_tty_register_device_failed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) 	strcpy(qtty->console.name, "ttyGF");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) 	qtty->console.write = goldfish_tty_console_write;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) 	qtty->console.device = goldfish_tty_console_device;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) 	qtty->console.setup = goldfish_tty_console_setup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) 	qtty->console.flags = CON_PRINTBUFFER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) 	qtty->console.index = line;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) 	register_console(&qtty->console);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) 	platform_set_drvdata(pdev, qtty);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) 	mutex_unlock(&goldfish_tty_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) err_tty_register_device_failed:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) 	free_irq(irq, qtty);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) err_dec_line_count:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) 	goldfish_tty_current_line_count--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) 	if (goldfish_tty_current_line_count == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) 		goldfish_tty_delete_driver();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) err_unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) 	mutex_unlock(&goldfish_tty_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) err_unmap:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) 	iounmap(base);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) static int goldfish_tty_remove(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) 	struct goldfish_tty *qtty = platform_get_drvdata(pdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) 	mutex_lock(&goldfish_tty_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) 	unregister_console(&qtty->console);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) 	tty_unregister_device(goldfish_tty_driver, qtty->console.index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) 	iounmap(qtty->base);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) 	qtty->base = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) 	free_irq(qtty->irq, pdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) 	goldfish_tty_current_line_count--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) 	if (goldfish_tty_current_line_count == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) 		goldfish_tty_delete_driver();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) 	mutex_unlock(&goldfish_tty_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) #ifdef CONFIG_GOLDFISH_TTY_EARLY_CONSOLE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) static void gf_early_console_putchar(struct uart_port *port, int ch)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) 	__raw_writel(ch, port->membase);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) static void gf_early_write(struct console *con, const char *s, unsigned int n)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) 	struct earlycon_device *dev = con->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) 	uart_console_write(&dev->port, s, n, gf_early_console_putchar);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) static int __init gf_earlycon_setup(struct earlycon_device *device,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) 				    const char *opt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) 	if (!device->port.membase)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) 	device->con->write = gf_early_write;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) OF_EARLYCON_DECLARE(early_gf_tty, "google,goldfish-tty", gf_earlycon_setup);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) static const struct of_device_id goldfish_tty_of_match[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) 	{ .compatible = "google,goldfish-tty", },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) 	{},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) MODULE_DEVICE_TABLE(of, goldfish_tty_of_match);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) static struct platform_driver goldfish_tty_platform_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) 	.probe = goldfish_tty_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) 	.remove = goldfish_tty_remove,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) 	.driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) 		.name = "goldfish_tty",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) 		.of_match_table = goldfish_tty_of_match,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) module_platform_driver(goldfish_tty_platform_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) MODULE_LICENSE("GPL v2");