8250_dw.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. /*
  2. * Synopsys DesignWare 8250 driver.
  3. *
  4. * Copyright 2011 Picochip, Jamie Iles.
  5. * Copyright 2013 Intel Corporation
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * The Synopsys DesignWare 8250 has an extra feature whereby it detects if the
  13. * LCR is written whilst busy. If it is, then a busy detect interrupt is
  14. * raised, the LCR needs to be rewritten and the uart status register read.
  15. */
  16. #include <linux/device.h>
  17. #include <linux/init.h>
  18. #include <linux/io.h>
  19. #include <linux/module.h>
  20. #include <linux/serial_8250.h>
  21. #include <linux/serial_core.h>
  22. #include <linux/serial_reg.h>
  23. #include <linux/of.h>
  24. #include <linux/of_irq.h>
  25. #include <linux/of_platform.h>
  26. #include <linux/platform_device.h>
  27. #include <linux/slab.h>
  28. #include <linux/acpi.h>
  29. #include <linux/clk.h>
  30. #include <linux/pm_runtime.h>
  31. #include "8250.h"
  32. /* Offsets for the DesignWare specific registers */
  33. #define DW_UART_USR 0x1f /* UART Status Register */
  34. #define DW_UART_CPR 0xf4 /* Component Parameter Register */
  35. #define DW_UART_UCV 0xf8 /* UART Component Version */
  36. /* Component Parameter Register bits */
  37. #define DW_UART_CPR_ABP_DATA_WIDTH (3 << 0)
  38. #define DW_UART_CPR_AFCE_MODE (1 << 4)
  39. #define DW_UART_CPR_THRE_MODE (1 << 5)
  40. #define DW_UART_CPR_SIR_MODE (1 << 6)
  41. #define DW_UART_CPR_SIR_LP_MODE (1 << 7)
  42. #define DW_UART_CPR_ADDITIONAL_FEATURES (1 << 8)
  43. #define DW_UART_CPR_FIFO_ACCESS (1 << 9)
  44. #define DW_UART_CPR_FIFO_STAT (1 << 10)
  45. #define DW_UART_CPR_SHADOW (1 << 11)
  46. #define DW_UART_CPR_ENCODED_PARMS (1 << 12)
  47. #define DW_UART_CPR_DMA_EXTRA (1 << 13)
  48. #define DW_UART_CPR_FIFO_MODE (0xff << 16)
  49. /* Helper for fifo size calculation */
  50. #define DW_UART_CPR_FIFO_SIZE(a) (((a >> 16) & 0xff) * 16)
  51. struct dw8250_data {
  52. int last_lcr;
  53. int line;
  54. struct clk *clk;
  55. };
  56. static void dw8250_serial_out(struct uart_port *p, int offset, int value)
  57. {
  58. struct dw8250_data *d = p->private_data;
  59. if (offset == UART_LCR)
  60. d->last_lcr = value;
  61. offset <<= p->regshift;
  62. writeb(value, p->membase + offset);
  63. }
  64. static unsigned int dw8250_serial_in(struct uart_port *p, int offset)
  65. {
  66. offset <<= p->regshift;
  67. return readb(p->membase + offset);
  68. }
  69. static void dw8250_serial_out32(struct uart_port *p, int offset, int value)
  70. {
  71. struct dw8250_data *d = p->private_data;
  72. if (offset == UART_LCR)
  73. d->last_lcr = value;
  74. offset <<= p->regshift;
  75. writel(value, p->membase + offset);
  76. }
  77. static unsigned int dw8250_serial_in32(struct uart_port *p, int offset)
  78. {
  79. offset <<= p->regshift;
  80. return readl(p->membase + offset);
  81. }
  82. static int dw8250_handle_irq(struct uart_port *p)
  83. {
  84. struct dw8250_data *d = p->private_data;
  85. unsigned int iir = p->serial_in(p, UART_IIR);
  86. if (serial8250_handle_irq(p, iir)) {
  87. return 1;
  88. } else if ((iir & UART_IIR_BUSY) == UART_IIR_BUSY) {
  89. /* Clear the USR and write the LCR again. */
  90. (void)p->serial_in(p, DW_UART_USR);
  91. p->serial_out(p, UART_LCR, d->last_lcr);
  92. return 1;
  93. }
  94. return 0;
  95. }
  96. static void
  97. dw8250_do_pm(struct uart_port *port, unsigned int state, unsigned int old)
  98. {
  99. if (!state)
  100. pm_runtime_get_sync(port->dev);
  101. serial8250_do_pm(port, state, old);
  102. if (state)
  103. pm_runtime_put_sync_suspend(port->dev);
  104. }
  105. static int dw8250_probe_of(struct uart_port *p)
  106. {
  107. struct device_node *np = p->dev->of_node;
  108. u32 val;
  109. if (!of_property_read_u32(np, "reg-io-width", &val)) {
  110. switch (val) {
  111. case 1:
  112. break;
  113. case 4:
  114. p->iotype = UPIO_MEM32;
  115. p->serial_in = dw8250_serial_in32;
  116. p->serial_out = dw8250_serial_out32;
  117. break;
  118. default:
  119. dev_err(p->dev, "unsupported reg-io-width (%u)\n", val);
  120. return -EINVAL;
  121. }
  122. }
  123. if (!of_property_read_u32(np, "reg-shift", &val))
  124. p->regshift = val;
  125. /* clock got configured through clk api, all done */
  126. if (p->uartclk)
  127. return 0;
  128. /* try to find out clock frequency from DT as fallback */
  129. if (of_property_read_u32(np, "clock-frequency", &val)) {
  130. dev_err(p->dev, "clk or clock-frequency not defined\n");
  131. return -EINVAL;
  132. }
  133. p->uartclk = val;
  134. return 0;
  135. }
  136. #ifdef CONFIG_ACPI
  137. static int dw8250_probe_acpi(struct uart_8250_port *up)
  138. {
  139. const struct acpi_device_id *id;
  140. struct uart_port *p = &up->port;
  141. id = acpi_match_device(p->dev->driver->acpi_match_table, p->dev);
  142. if (!id)
  143. return -ENODEV;
  144. p->iotype = UPIO_MEM32;
  145. p->serial_in = dw8250_serial_in32;
  146. p->serial_out = dw8250_serial_out32;
  147. p->regshift = 2;
  148. if (!p->uartclk)
  149. p->uartclk = (unsigned int)id->driver_data;
  150. up->dma = devm_kzalloc(p->dev, sizeof(*up->dma), GFP_KERNEL);
  151. if (!up->dma)
  152. return -ENOMEM;
  153. up->dma->rxconf.src_maxburst = p->fifosize / 4;
  154. up->dma->txconf.dst_maxburst = p->fifosize / 4;
  155. return 0;
  156. }
  157. #else
  158. static inline int dw8250_probe_acpi(struct uart_8250_port *up)
  159. {
  160. return -ENODEV;
  161. }
  162. #endif /* CONFIG_ACPI */
  163. static void dw8250_setup_port(struct uart_8250_port *up)
  164. {
  165. struct uart_port *p = &up->port;
  166. u32 reg = readl(p->membase + DW_UART_UCV);
  167. /*
  168. * If the Component Version Register returns zero, we know that
  169. * ADDITIONAL_FEATURES are not enabled. No need to go any further.
  170. */
  171. if (!reg)
  172. return;
  173. dev_dbg_ratelimited(p->dev, "Designware UART version %c.%c%c\n",
  174. (reg >> 24) & 0xff, (reg >> 16) & 0xff, (reg >> 8) & 0xff);
  175. reg = readl(p->membase + DW_UART_CPR);
  176. if (!reg)
  177. return;
  178. /* Select the type based on fifo */
  179. if (reg & DW_UART_CPR_FIFO_MODE) {
  180. p->type = PORT_16550A;
  181. p->flags |= UPF_FIXED_TYPE;
  182. p->fifosize = DW_UART_CPR_FIFO_SIZE(reg);
  183. up->tx_loadsz = p->fifosize;
  184. up->capabilities = UART_CAP_FIFO;
  185. }
  186. if (reg & DW_UART_CPR_AFCE_MODE)
  187. up->capabilities |= UART_CAP_AFE;
  188. }
  189. static int dw8250_probe(struct platform_device *pdev)
  190. {
  191. struct uart_8250_port uart = {};
  192. struct resource *regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  193. struct resource *irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  194. struct dw8250_data *data;
  195. int err;
  196. if (!regs || !irq) {
  197. dev_err(&pdev->dev, "no registers/irq defined\n");
  198. return -EINVAL;
  199. }
  200. spin_lock_init(&uart.port.lock);
  201. uart.port.mapbase = regs->start;
  202. uart.port.irq = irq->start;
  203. uart.port.handle_irq = dw8250_handle_irq;
  204. uart.port.pm = dw8250_do_pm;
  205. uart.port.type = PORT_8250;
  206. uart.port.flags = UPF_SHARE_IRQ | UPF_BOOT_AUTOCONF | UPF_FIXED_PORT;
  207. uart.port.dev = &pdev->dev;
  208. uart.port.membase = devm_ioremap(&pdev->dev, regs->start,
  209. resource_size(regs));
  210. if (!uart.port.membase)
  211. return -ENOMEM;
  212. data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
  213. if (!data)
  214. return -ENOMEM;
  215. data->clk = devm_clk_get(&pdev->dev, NULL);
  216. if (!IS_ERR(data->clk)) {
  217. clk_prepare_enable(data->clk);
  218. uart.port.uartclk = clk_get_rate(data->clk);
  219. }
  220. uart.port.iotype = UPIO_MEM;
  221. uart.port.serial_in = dw8250_serial_in;
  222. uart.port.serial_out = dw8250_serial_out;
  223. uart.port.private_data = data;
  224. dw8250_setup_port(&uart);
  225. if (pdev->dev.of_node) {
  226. err = dw8250_probe_of(&uart.port);
  227. if (err)
  228. return err;
  229. } else if (ACPI_HANDLE(&pdev->dev)) {
  230. err = dw8250_probe_acpi(&uart);
  231. if (err)
  232. return err;
  233. } else {
  234. return -ENODEV;
  235. }
  236. data->line = serial8250_register_8250_port(&uart);
  237. if (data->line < 0)
  238. return data->line;
  239. platform_set_drvdata(pdev, data);
  240. pm_runtime_set_active(&pdev->dev);
  241. pm_runtime_enable(&pdev->dev);
  242. return 0;
  243. }
  244. static int dw8250_remove(struct platform_device *pdev)
  245. {
  246. struct dw8250_data *data = platform_get_drvdata(pdev);
  247. pm_runtime_get_sync(&pdev->dev);
  248. serial8250_unregister_port(data->line);
  249. if (!IS_ERR(data->clk))
  250. clk_disable_unprepare(data->clk);
  251. pm_runtime_disable(&pdev->dev);
  252. pm_runtime_put_noidle(&pdev->dev);
  253. return 0;
  254. }
  255. #ifdef CONFIG_PM
  256. static int dw8250_suspend(struct device *dev)
  257. {
  258. struct dw8250_data *data = dev_get_drvdata(dev);
  259. serial8250_suspend_port(data->line);
  260. return 0;
  261. }
  262. static int dw8250_resume(struct device *dev)
  263. {
  264. struct dw8250_data *data = dev_get_drvdata(dev);
  265. serial8250_resume_port(data->line);
  266. return 0;
  267. }
  268. #endif /* CONFIG_PM */
  269. #ifdef CONFIG_PM_RUNTIME
  270. static int dw8250_runtime_suspend(struct device *dev)
  271. {
  272. struct dw8250_data *data = dev_get_drvdata(dev);
  273. if (!IS_ERR(data->clk))
  274. clk_disable_unprepare(data->clk);
  275. return 0;
  276. }
  277. static int dw8250_runtime_resume(struct device *dev)
  278. {
  279. struct dw8250_data *data = dev_get_drvdata(dev);
  280. if (!IS_ERR(data->clk))
  281. clk_prepare_enable(data->clk);
  282. return 0;
  283. }
  284. #endif
  285. static const struct dev_pm_ops dw8250_pm_ops = {
  286. SET_SYSTEM_SLEEP_PM_OPS(dw8250_suspend, dw8250_resume)
  287. SET_RUNTIME_PM_OPS(dw8250_runtime_suspend, dw8250_runtime_resume, NULL)
  288. };
  289. static const struct of_device_id dw8250_of_match[] = {
  290. { .compatible = "snps,dw-apb-uart" },
  291. { /* Sentinel */ }
  292. };
  293. MODULE_DEVICE_TABLE(of, dw8250_of_match);
  294. static const struct acpi_device_id dw8250_acpi_match[] = {
  295. { "INT33C4", 0 },
  296. { "INT33C5", 0 },
  297. { "80860F0A", 0 },
  298. { },
  299. };
  300. MODULE_DEVICE_TABLE(acpi, dw8250_acpi_match);
  301. static struct platform_driver dw8250_platform_driver = {
  302. .driver = {
  303. .name = "dw-apb-uart",
  304. .owner = THIS_MODULE,
  305. .pm = &dw8250_pm_ops,
  306. .of_match_table = dw8250_of_match,
  307. .acpi_match_table = ACPI_PTR(dw8250_acpi_match),
  308. },
  309. .probe = dw8250_probe,
  310. .remove = dw8250_remove,
  311. };
  312. module_platform_driver(dw8250_platform_driver);
  313. MODULE_AUTHOR("Jamie Iles");
  314. MODULE_LICENSE("GPL");
  315. MODULE_DESCRIPTION("Synopsys DesignWare 8250 serial port driver");