8250_pxa.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * drivers/tty/serial/8250/8250_pxa.c -- driver for PXA on-board UARTS
  4. * Copyright: (C) 2013 Sergei Ianovich <ynvich@gmail.com>
  5. *
  6. * replaces drivers/serial/pxa.c by Nicolas Pitre
  7. * Created: Feb 20, 2003
  8. * Copyright: (C) 2003 Monta Vista Software, Inc.
  9. *
  10. * Based on drivers/serial/8250.c by Russell King.
  11. */
  12. #include <linux/device.h>
  13. #include <linux/init.h>
  14. #include <linux/io.h>
  15. #include <linux/module.h>
  16. #include <linux/serial_8250.h>
  17. #include <linux/serial_core.h>
  18. #include <linux/serial_reg.h>
  19. #include <linux/of.h>
  20. #include <linux/of_irq.h>
  21. #include <linux/of_platform.h>
  22. #include <linux/platform_device.h>
  23. #include <linux/slab.h>
  24. #include <linux/clk.h>
  25. #include <linux/pm_runtime.h>
  26. #include "8250.h"
  27. struct pxa8250_data {
  28. int line;
  29. struct clk *clk;
  30. };
  31. static int __maybe_unused serial_pxa_suspend(struct device *dev)
  32. {
  33. struct pxa8250_data *data = dev_get_drvdata(dev);
  34. serial8250_suspend_port(data->line);
  35. return 0;
  36. }
  37. static int __maybe_unused serial_pxa_resume(struct device *dev)
  38. {
  39. struct pxa8250_data *data = dev_get_drvdata(dev);
  40. serial8250_resume_port(data->line);
  41. return 0;
  42. }
  43. static const struct dev_pm_ops serial_pxa_pm_ops = {
  44. SET_SYSTEM_SLEEP_PM_OPS(serial_pxa_suspend, serial_pxa_resume)
  45. };
  46. static const struct of_device_id serial_pxa_dt_ids[] = {
  47. { .compatible = "mrvl,pxa-uart", },
  48. { .compatible = "mrvl,mmp-uart", },
  49. {}
  50. };
  51. MODULE_DEVICE_TABLE(of, serial_pxa_dt_ids);
  52. /* Uart divisor latch write */
  53. static void serial_pxa_dl_write(struct uart_8250_port *up, int value)
  54. {
  55. unsigned int dll;
  56. serial_out(up, UART_DLL, value & 0xff);
  57. /*
  58. * work around Erratum #74 according to Marvel(R) PXA270M Processor
  59. * Specification Update (April 19, 2010)
  60. */
  61. dll = serial_in(up, UART_DLL);
  62. WARN_ON(dll != (value & 0xff));
  63. serial_out(up, UART_DLM, value >> 8 & 0xff);
  64. }
  65. static void serial_pxa_pm(struct uart_port *port, unsigned int state,
  66. unsigned int oldstate)
  67. {
  68. struct pxa8250_data *data = port->private_data;
  69. if (!state)
  70. clk_prepare_enable(data->clk);
  71. else
  72. clk_disable_unprepare(data->clk);
  73. }
  74. static int serial_pxa_probe(struct platform_device *pdev)
  75. {
  76. struct uart_8250_port uart = {};
  77. struct pxa8250_data *data;
  78. struct resource *mmres, *irqres;
  79. int ret;
  80. mmres = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  81. irqres = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  82. if (!mmres || !irqres)
  83. return -ENODEV;
  84. data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
  85. if (!data)
  86. return -ENOMEM;
  87. data->clk = devm_clk_get(&pdev->dev, NULL);
  88. if (IS_ERR(data->clk))
  89. return PTR_ERR(data->clk);
  90. ret = clk_prepare(data->clk);
  91. if (ret)
  92. return ret;
  93. ret = of_alias_get_id(pdev->dev.of_node, "serial");
  94. if (ret >= 0)
  95. uart.port.line = ret;
  96. uart.port.type = PORT_XSCALE;
  97. uart.port.iotype = UPIO_MEM32;
  98. uart.port.mapbase = mmres->start;
  99. uart.port.regshift = 2;
  100. uart.port.irq = irqres->start;
  101. uart.port.fifosize = 64;
  102. uart.port.flags = UPF_IOREMAP | UPF_SKIP_TEST;
  103. uart.port.dev = &pdev->dev;
  104. uart.port.uartclk = clk_get_rate(data->clk);
  105. uart.port.pm = serial_pxa_pm;
  106. uart.port.private_data = data;
  107. uart.dl_write = serial_pxa_dl_write;
  108. ret = serial8250_register_8250_port(&uart);
  109. if (ret < 0)
  110. goto err_clk;
  111. data->line = ret;
  112. platform_set_drvdata(pdev, data);
  113. return 0;
  114. err_clk:
  115. clk_unprepare(data->clk);
  116. return ret;
  117. }
  118. static int serial_pxa_remove(struct platform_device *pdev)
  119. {
  120. struct pxa8250_data *data = platform_get_drvdata(pdev);
  121. serial8250_unregister_port(data->line);
  122. clk_unprepare(data->clk);
  123. return 0;
  124. }
  125. static struct platform_driver serial_pxa_driver = {
  126. .probe = serial_pxa_probe,
  127. .remove = serial_pxa_remove,
  128. .driver = {
  129. .name = "pxa2xx-uart",
  130. .pm = &serial_pxa_pm_ops,
  131. .of_match_table = serial_pxa_dt_ids,
  132. },
  133. };
  134. module_platform_driver(serial_pxa_driver);
  135. #ifdef CONFIG_SERIAL_8250_CONSOLE
  136. static int __init early_serial_pxa_setup(struct earlycon_device *device,
  137. const char *options)
  138. {
  139. struct uart_port *port = &device->port;
  140. if (!(device->port.membase || device->port.iobase))
  141. return -ENODEV;
  142. port->regshift = 2;
  143. return early_serial8250_setup(device, NULL);
  144. }
  145. OF_EARLYCON_DECLARE(early_pxa, "mrvl,pxa-uart", early_serial_pxa_setup);
  146. #endif
  147. MODULE_AUTHOR("Sergei Ianovich");
  148. MODULE_LICENSE("GPL");
  149. MODULE_ALIAS("platform:pxa2xx-uart");