8250_platform.c 11 KB

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