8250_platform.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Universal/legacy platform driver for 8250/16550-type serial ports
  4. *
  5. * Supports:
  6. * ISA-compatible 8250/16550 ports
  7. * ACPI 8250/16550 ports
  8. * PNP 8250/16550 ports
  9. * "serial8250" platform devices
  10. */
  11. #include <linux/acpi.h>
  12. #include <linux/array_size.h>
  13. #include <linux/cleanup.h>
  14. #include <linux/io.h>
  15. #include <linux/module.h>
  16. #include <linux/moduleparam.h>
  17. #include <linux/once.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/serial_8250.h>
  20. #ifdef CONFIG_SPARC
  21. #include <linux/sunserialcore.h>
  22. #endif
  23. #include "8250.h"
  24. /*
  25. * Configuration:
  26. * share_irqs: Whether we pass IRQF_SHARED to request_irq().
  27. * This option is unsafe when used on edge-triggered interrupts.
  28. */
  29. static bool share_irqs = IS_ENABLED(CONFIG_SERIAL_8250_SHARE_IRQ);
  30. unsigned int nr_uarts = CONFIG_SERIAL_8250_RUNTIME_UARTS;
  31. #include <asm/serial.h>
  32. /*
  33. * SERIAL_PORT_DFNS tells us about built-in ports that have no
  34. * standard enumeration mechanism. Platforms that can find all
  35. * serial ports via mechanisms like ACPI or PCI need not supply it.
  36. */
  37. #ifndef SERIAL_PORT_DFNS
  38. #define SERIAL_PORT_DFNS
  39. #endif
  40. static const struct old_serial_port old_serial_port[] = {
  41. SERIAL_PORT_DFNS /* defined in asm/serial.h */
  42. };
  43. serial8250_isa_config_fn serial8250_isa_config;
  44. void serial8250_set_isa_configurator(serial8250_isa_config_fn v)
  45. {
  46. serial8250_isa_config = v;
  47. }
  48. EXPORT_SYMBOL(serial8250_set_isa_configurator);
  49. static void __init __serial8250_isa_init_ports(void)
  50. {
  51. int i;
  52. if (nr_uarts > UART_NR)
  53. nr_uarts = UART_NR;
  54. /*
  55. * Set up initial ISA ports based on nr_uart module param, or else
  56. * default to CONFIG_SERIAL_8250_RUNTIME_UARTS. Note that we do not
  57. * need to increase nr_uarts when setting up the initial ISA ports.
  58. */
  59. for (i = 0; i < nr_uarts; i++)
  60. serial8250_setup_port(i);
  61. /* chain base port ops to support Remote Supervisor Adapter */
  62. univ8250_port_ops = *univ8250_port_base_ops;
  63. univ8250_rsa_support(&univ8250_port_ops, univ8250_port_base_ops);
  64. for (i = 0; i < ARRAY_SIZE(old_serial_port) && i < nr_uarts; i++) {
  65. struct uart_8250_port *up = serial8250_get_port(i);
  66. struct uart_port *port = &up->port;
  67. port->iobase = old_serial_port[i].port;
  68. port->irq = irq_canonicalize(old_serial_port[i].irq);
  69. port->irqflags = 0;
  70. port->uartclk = old_serial_port[i].baud_base * 16;
  71. port->flags = old_serial_port[i].flags;
  72. port->hub6 = 0;
  73. port->membase = old_serial_port[i].iomem_base;
  74. port->iotype = old_serial_port[i].io_type;
  75. port->regshift = old_serial_port[i].iomem_reg_shift;
  76. if (share_irqs)
  77. port->irqflags |= IRQF_SHARED;
  78. if (serial8250_isa_config != NULL)
  79. serial8250_isa_config(i, &up->port, &up->capabilities);
  80. }
  81. }
  82. void __init serial8250_isa_init_ports(void)
  83. {
  84. DO_ONCE(__serial8250_isa_init_ports);
  85. }
  86. /*
  87. * Generic 16550A platform devices
  88. */
  89. static int serial8250_probe_acpi(struct platform_device *pdev)
  90. {
  91. struct device *dev = &pdev->dev;
  92. struct resource *regs;
  93. int ret, line;
  94. struct uart_8250_port *uart __free(kfree) = kzalloc_obj(*uart);
  95. if (!uart)
  96. return -ENOMEM;
  97. regs = platform_get_mem_or_io(pdev, 0);
  98. if (!regs)
  99. return dev_err_probe(dev, -EINVAL, "no registers defined\n");
  100. switch (resource_type(regs)) {
  101. case IORESOURCE_IO:
  102. uart->port.iobase = regs->start;
  103. break;
  104. case IORESOURCE_MEM:
  105. uart->port.mapbase = regs->start;
  106. uart->port.mapsize = resource_size(regs);
  107. uart->port.flags = UPF_IOREMAP;
  108. break;
  109. default:
  110. return -EINVAL;
  111. }
  112. /* default clock frequency */
  113. uart->port.uartclk = 1843200;
  114. uart->port.type = PORT_16550A;
  115. uart->port.dev = &pdev->dev;
  116. uart->port.flags |= UPF_SKIP_TEST | UPF_BOOT_AUTOCONF;
  117. ret = uart_read_and_validate_port_properties(&uart->port);
  118. /* no interrupt -> fall back to polling */
  119. if (ret == -ENXIO)
  120. ret = 0;
  121. if (ret)
  122. return ret;
  123. line = serial8250_register_8250_port(uart);
  124. if (line < 0)
  125. return line;
  126. return 0;
  127. }
  128. static int serial8250_probe_platform(struct platform_device *dev, struct plat_serial8250_port *p)
  129. {
  130. int ret, i;
  131. struct uart_8250_port *uart __free(kfree) = kzalloc_obj(*uart);
  132. if (!uart)
  133. return -ENOMEM;
  134. for (i = 0; p && p->flags != 0; p++, i++) {
  135. uart->port.iobase = p->iobase;
  136. uart->port.membase = p->membase;
  137. uart->port.irq = p->irq;
  138. uart->port.irqflags = p->irqflags;
  139. uart->port.uartclk = p->uartclk;
  140. uart->port.regshift = p->regshift;
  141. uart->port.iotype = p->iotype;
  142. uart->port.flags = p->flags;
  143. uart->port.mapbase = p->mapbase;
  144. uart->port.mapsize = p->mapsize;
  145. uart->port.hub6 = p->hub6;
  146. uart->port.has_sysrq = p->has_sysrq;
  147. uart->port.private_data = p->private_data;
  148. uart->port.type = p->type;
  149. uart->bugs = p->bugs;
  150. uart->port.serial_in = p->serial_in;
  151. uart->port.serial_out = p->serial_out;
  152. uart->dl_read = p->dl_read;
  153. uart->dl_write = p->dl_write;
  154. uart->port.handle_irq = p->handle_irq;
  155. uart->port.handle_break = p->handle_break;
  156. uart->port.set_termios = p->set_termios;
  157. uart->port.set_ldisc = p->set_ldisc;
  158. uart->port.get_mctrl = p->get_mctrl;
  159. uart->port.pm = p->pm;
  160. uart->port.dev = &dev->dev;
  161. if (share_irqs)
  162. uart->port.irqflags |= IRQF_SHARED;
  163. ret = serial8250_register_8250_port(uart);
  164. if (ret < 0) {
  165. dev_err(&dev->dev, "unable to register port at index %d "
  166. "(IO%lx MEM%llx IRQ%d): %d\n", i,
  167. p->iobase, (unsigned long long)p->mapbase,
  168. p->irq, ret);
  169. }
  170. }
  171. return 0;
  172. }
  173. /*
  174. * Register a set of serial devices attached to a platform device.
  175. * The list is terminated with a zero flags entry, which means we expect
  176. * all entries to have at least UPF_BOOT_AUTOCONF set.
  177. */
  178. static int serial8250_probe(struct platform_device *pdev)
  179. {
  180. struct device *dev = &pdev->dev;
  181. struct plat_serial8250_port *p;
  182. p = dev_get_platdata(dev);
  183. if (p)
  184. return serial8250_probe_platform(pdev, p);
  185. /*
  186. * Probe platform UART devices defined using standard hardware
  187. * discovery mechanism like ACPI or DT. Support only ACPI based
  188. * serial device for now.
  189. */
  190. if (has_acpi_companion(dev))
  191. return serial8250_probe_acpi(pdev);
  192. return 0;
  193. }
  194. /*
  195. * Remove serial ports registered against a platform device.
  196. */
  197. static void serial8250_remove(struct platform_device *dev)
  198. {
  199. int i;
  200. for (i = 0; i < nr_uarts; i++) {
  201. struct uart_8250_port *up = serial8250_get_port(i);
  202. if (up->port.dev == &dev->dev)
  203. serial8250_unregister_port(i);
  204. }
  205. }
  206. static int serial8250_suspend(struct platform_device *dev, pm_message_t state)
  207. {
  208. int i;
  209. for (i = 0; i < UART_NR; i++) {
  210. struct uart_8250_port *up = serial8250_get_port(i);
  211. if (up->port.type != PORT_UNKNOWN && up->port.dev == &dev->dev)
  212. uart_suspend_port(&serial8250_reg, &up->port);
  213. }
  214. return 0;
  215. }
  216. static int serial8250_resume(struct platform_device *dev)
  217. {
  218. int i;
  219. for (i = 0; i < UART_NR; i++) {
  220. struct uart_8250_port *up = serial8250_get_port(i);
  221. if (up->port.type != PORT_UNKNOWN && up->port.dev == &dev->dev)
  222. serial8250_resume_port(i);
  223. }
  224. return 0;
  225. }
  226. static const struct acpi_device_id acpi_platform_serial_table[] = {
  227. { "RSCV0003" }, /* RISC-V Generic 16550A UART */
  228. { }
  229. };
  230. MODULE_DEVICE_TABLE(acpi, acpi_platform_serial_table);
  231. static struct platform_driver serial8250_isa_driver = {
  232. .probe = serial8250_probe,
  233. .remove = serial8250_remove,
  234. .suspend = serial8250_suspend,
  235. .resume = serial8250_resume,
  236. .driver = {
  237. .name = "serial8250",
  238. .acpi_match_table = acpi_platform_serial_table,
  239. },
  240. };
  241. /*
  242. * This "device" covers _all_ ISA 8250-compatible serial devices listed
  243. * in the table in include/asm/serial.h.
  244. */
  245. struct platform_device *serial8250_isa_devs;
  246. static int __init serial8250_init(void)
  247. {
  248. int ret;
  249. if (nr_uarts == 0)
  250. return -ENODEV;
  251. serial8250_isa_init_ports();
  252. pr_info("Serial: 8250/16550 driver, %d ports, IRQ sharing %s\n",
  253. nr_uarts, str_enabled_disabled(share_irqs));
  254. #ifdef CONFIG_SPARC
  255. ret = sunserial_register_minors(&serial8250_reg, UART_NR);
  256. #else
  257. serial8250_reg.nr = UART_NR;
  258. ret = uart_register_driver(&serial8250_reg);
  259. #endif
  260. if (ret)
  261. goto out;
  262. ret = serial8250_pnp_init();
  263. if (ret)
  264. goto unreg_uart_drv;
  265. serial8250_isa_devs = platform_device_alloc("serial8250", PLAT8250_DEV_LEGACY);
  266. if (!serial8250_isa_devs) {
  267. ret = -ENOMEM;
  268. goto unreg_pnp;
  269. }
  270. ret = platform_device_add(serial8250_isa_devs);
  271. if (ret)
  272. goto put_dev;
  273. serial8250_register_ports(&serial8250_reg, &serial8250_isa_devs->dev);
  274. ret = platform_driver_register(&serial8250_isa_driver);
  275. if (ret == 0)
  276. goto out;
  277. platform_device_del(serial8250_isa_devs);
  278. put_dev:
  279. platform_device_put(serial8250_isa_devs);
  280. unreg_pnp:
  281. serial8250_pnp_exit();
  282. unreg_uart_drv:
  283. #ifdef CONFIG_SPARC
  284. sunserial_unregister_minors(&serial8250_reg, UART_NR);
  285. #else
  286. uart_unregister_driver(&serial8250_reg);
  287. #endif
  288. out:
  289. return ret;
  290. }
  291. module_init(serial8250_init);
  292. static void __exit serial8250_exit(void)
  293. {
  294. struct platform_device *isa_dev = serial8250_isa_devs;
  295. /*
  296. * This tells serial8250_unregister_port() not to re-register
  297. * the ports (thereby making serial8250_isa_driver permanently
  298. * in use).
  299. */
  300. serial8250_isa_devs = NULL;
  301. platform_driver_unregister(&serial8250_isa_driver);
  302. platform_device_unregister(isa_dev);
  303. serial8250_pnp_exit();
  304. #ifdef CONFIG_SPARC
  305. sunserial_unregister_minors(&serial8250_reg, UART_NR);
  306. #else
  307. uart_unregister_driver(&serial8250_reg);
  308. #endif
  309. }
  310. module_exit(serial8250_exit);
  311. MODULE_LICENSE("GPL");
  312. MODULE_DESCRIPTION("Generic 8250/16x50 serial platform driver");
  313. module_param_hw(share_irqs, bool, other, 0644);
  314. MODULE_PARM_DESC(share_irqs, "Share IRQs with other non-8250/16x50 devices (unsafe)");
  315. module_param(nr_uarts, uint, 0644);
  316. MODULE_PARM_DESC(nr_uarts, "Maximum number of UARTs supported. (1-" __MODULE_STRING(CONFIG_SERIAL_8250_NR_UARTS) ")");
  317. MODULE_ALIAS_CHARDEV_MAJOR(TTY_MAJOR);