brintos

brintos / linux-shallow public Read only

0
0
Text · 10.8 KiB · be7ff07 Raw
424 lines · c
1// SPDX-License-Identifier: GPL-2.0+2/*3 *  Universal/legacy platform driver for 8250/16550-type serial ports4 *5 *  Supports:6 *	      ISA-compatible 8250/16550 ports7 *	      ACPI 8250/16550 ports8 *	      PNP 8250/16550 ports9 *	      "serial8250" platform devices10 */11#include <linux/acpi.h>12#include <linux/array_size.h>13#include <linux/io.h>14#include <linux/module.h>15#include <linux/moduleparam.h>16#include <linux/once.h>17#include <linux/platform_device.h>18 19#include <linux/serial_8250.h>20 21#ifdef CONFIG_SPARC22#include <linux/sunserialcore.h>23#endif24 25#include "8250.h"26 27/*28 * Configuration:29 * share_irqs:     Whether we pass IRQF_SHARED to request_irq().30 *                 This option is unsafe when used on edge-triggered interrupts.31 * skip_txen_test: Force skip of txen test at init time.32 */33unsigned int share_irqs = SERIAL8250_SHARE_IRQS;34unsigned int skip_txen_test;35 36unsigned int nr_uarts = CONFIG_SERIAL_8250_RUNTIME_UARTS;37 38#include <asm/serial.h>39 40/*41 * SERIAL_PORT_DFNS tells us about built-in ports that have no42 * standard enumeration mechanism. Platforms that can find all43 * serial ports via mechanisms like ACPI or PCI need not supply it.44 */45#ifndef SERIAL_PORT_DFNS46#define SERIAL_PORT_DFNS47#endif48 49static const struct old_serial_port old_serial_port[] = {50	SERIAL_PORT_DFNS /* defined in asm/serial.h */51};52 53serial8250_isa_config_fn serial8250_isa_config;54void serial8250_set_isa_configurator(serial8250_isa_config_fn v)55{56	serial8250_isa_config = v;57}58EXPORT_SYMBOL(serial8250_set_isa_configurator);59 60static void __init __serial8250_isa_init_ports(void)61{62	int i, irqflag = 0;63 64	if (nr_uarts > UART_NR)65		nr_uarts = UART_NR;66 67	/*68	 * Set up initial ISA ports based on nr_uart module param, or else69	 * default to CONFIG_SERIAL_8250_RUNTIME_UARTS. Note that we do not70	 * need to increase nr_uarts when setting up the initial ISA ports.71	 */72	for (i = 0; i < nr_uarts; i++)73		serial8250_setup_port(i);74 75	/* chain base port ops to support Remote Supervisor Adapter */76	univ8250_port_ops = *univ8250_port_base_ops;77	univ8250_rsa_support(&univ8250_port_ops);78 79	if (share_irqs)80		irqflag = IRQF_SHARED;81 82	for (i = 0; i < ARRAY_SIZE(old_serial_port) && i < nr_uarts; i++) {83		struct uart_8250_port *up = serial8250_get_port(i);84		struct uart_port *port = &up->port;85 86		port->iobase   = old_serial_port[i].port;87		port->irq      = irq_canonicalize(old_serial_port[i].irq);88		port->irqflags = 0;89		port->uartclk  = old_serial_port[i].baud_base * 16;90		port->flags    = old_serial_port[i].flags;91		port->hub6     = 0;92		port->membase  = old_serial_port[i].iomem_base;93		port->iotype   = old_serial_port[i].io_type;94		port->regshift = old_serial_port[i].iomem_reg_shift;95 96		port->irqflags |= irqflag;97		if (serial8250_isa_config != NULL)98			serial8250_isa_config(i, &up->port, &up->capabilities);99	}100}101 102void __init serial8250_isa_init_ports(void)103{104	DO_ONCE(__serial8250_isa_init_ports);105}106 107/*108 * Generic 16550A platform devices109 */110static int serial8250_probe_acpi(struct platform_device *pdev)111{112	struct device *dev = &pdev->dev;113	struct uart_8250_port uart = { };114	struct resource *regs;115	unsigned char iotype;116	int ret, line;117 118	regs = platform_get_mem_or_io(pdev, 0);119	if (!regs)120		return dev_err_probe(dev, -EINVAL, "no registers defined\n");121 122	switch (resource_type(regs)) {123	case IORESOURCE_IO:124		uart.port.iobase = regs->start;125		iotype = UPIO_PORT;126		break;127	case IORESOURCE_MEM:128		uart.port.mapbase = regs->start;129		uart.port.mapsize = resource_size(regs);130		uart.port.flags = UPF_IOREMAP;131		iotype = UPIO_MEM;132		break;133	default:134		return -EINVAL;135	}136 137	/* default clock frequency */138	uart.port.uartclk = 1843200;139	uart.port.type = PORT_16550A;140	uart.port.dev = &pdev->dev;141	uart.port.flags |= UPF_SKIP_TEST | UPF_BOOT_AUTOCONF;142 143	ret = uart_read_and_validate_port_properties(&uart.port);144	/* no interrupt -> fall back to polling */145	if (ret == -ENXIO)146		ret = 0;147	if (ret)148		return ret;149 150	/*151	 * The previous call may not set iotype correctly when reg-io-width152	 * property is absent and it doesn't support IO port resource.153	 */154	uart.port.iotype = iotype;155 156	line = serial8250_register_8250_port(&uart);157	if (line < 0)158		return line;159 160	return 0;161}162 163static int serial8250_probe_platform(struct platform_device *dev, struct plat_serial8250_port *p)164{165	struct uart_8250_port uart;166	int ret, i, irqflag = 0;167 168	memset(&uart, 0, sizeof(uart));169 170	if (share_irqs)171		irqflag = IRQF_SHARED;172 173	for (i = 0; p && p->flags != 0; p++, i++) {174		uart.port.iobase	= p->iobase;175		uart.port.membase	= p->membase;176		uart.port.irq		= p->irq;177		uart.port.irqflags	= p->irqflags;178		uart.port.uartclk	= p->uartclk;179		uart.port.regshift	= p->regshift;180		uart.port.iotype	= p->iotype;181		uart.port.flags		= p->flags;182		uart.port.mapbase	= p->mapbase;183		uart.port.mapsize	= p->mapsize;184		uart.port.hub6		= p->hub6;185		uart.port.has_sysrq	= p->has_sysrq;186		uart.port.private_data	= p->private_data;187		uart.port.type		= p->type;188		uart.bugs		= p->bugs;189		uart.port.serial_in	= p->serial_in;190		uart.port.serial_out	= p->serial_out;191		uart.dl_read		= p->dl_read;192		uart.dl_write		= p->dl_write;193		uart.port.handle_irq	= p->handle_irq;194		uart.port.handle_break	= p->handle_break;195		uart.port.set_termios	= p->set_termios;196		uart.port.set_ldisc	= p->set_ldisc;197		uart.port.get_mctrl	= p->get_mctrl;198		uart.port.pm		= p->pm;199		uart.port.dev		= &dev->dev;200		uart.port.irqflags	|= irqflag;201		ret = serial8250_register_8250_port(&uart);202		if (ret < 0) {203			dev_err(&dev->dev, "unable to register port at index %d "204				"(IO%lx MEM%llx IRQ%d): %d\n", i,205				p->iobase, (unsigned long long)p->mapbase,206				p->irq, ret);207		}208	}209	return 0;210}211 212/*213 * Register a set of serial devices attached to a platform device.214 * The list is terminated with a zero flags entry, which means we expect215 * all entries to have at least UPF_BOOT_AUTOCONF set.216 */217static int serial8250_probe(struct platform_device *pdev)218{219	struct device *dev = &pdev->dev;220	struct plat_serial8250_port *p;221 222	p = dev_get_platdata(dev);223	if (p)224		return serial8250_probe_platform(pdev, p);225 226	/*227	 * Probe platform UART devices defined using standard hardware228	 * discovery mechanism like ACPI or DT. Support only ACPI based229	 * serial device for now.230	 */231	if (has_acpi_companion(dev))232		return serial8250_probe_acpi(pdev);233 234	return 0;235}236 237/*238 * Remove serial ports registered against a platform device.239 */240static void serial8250_remove(struct platform_device *dev)241{242	int i;243 244	for (i = 0; i < nr_uarts; i++) {245		struct uart_8250_port *up = serial8250_get_port(i);246 247		if (up->port.dev == &dev->dev)248			serial8250_unregister_port(i);249	}250}251 252static int serial8250_suspend(struct platform_device *dev, pm_message_t state)253{254	int i;255 256	for (i = 0; i < UART_NR; i++) {257		struct uart_8250_port *up = serial8250_get_port(i);258 259		if (up->port.type != PORT_UNKNOWN && up->port.dev == &dev->dev)260			uart_suspend_port(&serial8250_reg, &up->port);261	}262 263	return 0;264}265 266static int serial8250_resume(struct platform_device *dev)267{268	int i;269 270	for (i = 0; i < UART_NR; i++) {271		struct uart_8250_port *up = serial8250_get_port(i);272 273		if (up->port.type != PORT_UNKNOWN && up->port.dev == &dev->dev)274			serial8250_resume_port(i);275	}276 277	return 0;278}279 280static const struct acpi_device_id acpi_platform_serial_table[] = {281	{ "RSCV0003" }, /* RISC-V Generic 16550A UART */282	{ }283};284MODULE_DEVICE_TABLE(acpi, acpi_platform_serial_table);285 286static struct platform_driver serial8250_isa_driver = {287	.probe		= serial8250_probe,288	.remove_new	= serial8250_remove,289	.suspend	= serial8250_suspend,290	.resume		= serial8250_resume,291	.driver		= {292		.name	= "serial8250",293		.acpi_match_table = acpi_platform_serial_table,294	},295};296 297/*298 * This "device" covers _all_ ISA 8250-compatible serial devices listed299 * in the table in include/asm/serial.h.300 */301struct platform_device *serial8250_isa_devs;302 303static int __init serial8250_init(void)304{305	int ret;306 307	if (nr_uarts == 0)308		return -ENODEV;309 310	serial8250_isa_init_ports();311 312	pr_info("Serial: 8250/16550 driver, %d ports, IRQ sharing %s\n",313		nr_uarts, str_enabled_disabled(share_irqs));314 315#ifdef CONFIG_SPARC316	ret = sunserial_register_minors(&serial8250_reg, UART_NR);317#else318	serial8250_reg.nr = UART_NR;319	ret = uart_register_driver(&serial8250_reg);320#endif321	if (ret)322		goto out;323 324	ret = serial8250_pnp_init();325	if (ret)326		goto unreg_uart_drv;327 328	serial8250_isa_devs = platform_device_alloc("serial8250", PLAT8250_DEV_LEGACY);329	if (!serial8250_isa_devs) {330		ret = -ENOMEM;331		goto unreg_pnp;332	}333 334	ret = platform_device_add(serial8250_isa_devs);335	if (ret)336		goto put_dev;337 338	serial8250_register_ports(&serial8250_reg, &serial8250_isa_devs->dev);339 340	ret = platform_driver_register(&serial8250_isa_driver);341	if (ret == 0)342		goto out;343 344	platform_device_del(serial8250_isa_devs);345put_dev:346	platform_device_put(serial8250_isa_devs);347unreg_pnp:348	serial8250_pnp_exit();349unreg_uart_drv:350#ifdef CONFIG_SPARC351	sunserial_unregister_minors(&serial8250_reg, UART_NR);352#else353	uart_unregister_driver(&serial8250_reg);354#endif355out:356	return ret;357}358module_init(serial8250_init);359 360static void __exit serial8250_exit(void)361{362	struct platform_device *isa_dev = serial8250_isa_devs;363 364	/*365	 * This tells serial8250_unregister_port() not to re-register366	 * the ports (thereby making serial8250_isa_driver permanently367	 * in use).368	 */369	serial8250_isa_devs = NULL;370 371	platform_driver_unregister(&serial8250_isa_driver);372	platform_device_unregister(isa_dev);373 374	serial8250_pnp_exit();375 376#ifdef CONFIG_SPARC377	sunserial_unregister_minors(&serial8250_reg, UART_NR);378#else379	uart_unregister_driver(&serial8250_reg);380#endif381}382module_exit(serial8250_exit);383 384MODULE_LICENSE("GPL");385MODULE_DESCRIPTION("Generic 8250/16x50 serial platform driver");386 387module_param_hw(share_irqs, uint, other, 0644);388MODULE_PARM_DESC(share_irqs, "Share IRQs with other non-8250/16x50 devices (unsafe)");389 390module_param(nr_uarts, uint, 0644);391MODULE_PARM_DESC(nr_uarts, "Maximum number of UARTs supported. (1-" __MODULE_STRING(CONFIG_SERIAL_8250_NR_UARTS) ")");392 393module_param(skip_txen_test, uint, 0644);394MODULE_PARM_DESC(skip_txen_test, "Skip checking for the TXEN bug at init time");395 396MODULE_ALIAS_CHARDEV_MAJOR(TTY_MAJOR);397 398#ifdef CONFIG_SERIAL_8250_DEPRECATED_OPTIONS399#ifndef MODULE400/*401 * This module was renamed to 8250_core in 3.7. Keep the old "8250" name402 * working as well for the module options so we don't break people. We403 * need to keep the names identical and the convenient macros will happily404 * refuse to let us do that by failing the build with redefinition errors405 * of global variables. So we stick them inside a dummy function to avoid406 * those conflicts. The options still get parsed, and the redefined407 * MODULE_PARAM_PREFIX lets us keep the "8250." syntax alive.408 *409 * This is hacky.  I'm sorry.410 */411static void __used s8250_options(void)412{413#undef MODULE_PARAM_PREFIX414#define MODULE_PARAM_PREFIX "8250_core."415 416	module_param_cb(share_irqs, &param_ops_uint, &share_irqs, 0644);417	module_param_cb(nr_uarts, &param_ops_uint, &nr_uarts, 0644);418	module_param_cb(skip_txen_test, &param_ops_uint, &skip_txen_test, 0644);419}420#else421MODULE_ALIAS("8250_core");422#endif423#endif424