8250_dw.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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. /* Offsets for the DesignWare specific registers */
  30. #define DW_UART_USR 0x1f /* UART Status Register */
  31. #define DW_UART_CPR 0xf4 /* Component Parameter Register */
  32. #define DW_UART_UCV 0xf8 /* UART Component Version */
  33. /* Intel Low Power Subsystem specific */
  34. #define LPSS_PRV_CLOCK_PARAMS 0x800
  35. /* Component Parameter Register bits */
  36. #define DW_UART_CPR_ABP_DATA_WIDTH (3 << 0)
  37. #define DW_UART_CPR_AFCE_MODE (1 << 4)
  38. #define DW_UART_CPR_THRE_MODE (1 << 5)
  39. #define DW_UART_CPR_SIR_MODE (1 << 6)
  40. #define DW_UART_CPR_SIR_LP_MODE (1 << 7)
  41. #define DW_UART_CPR_ADDITIONAL_FEATURES (1 << 8)
  42. #define DW_UART_CPR_FIFO_ACCESS (1 << 9)
  43. #define DW_UART_CPR_FIFO_STAT (1 << 10)
  44. #define DW_UART_CPR_SHADOW (1 << 11)
  45. #define DW_UART_CPR_ENCODED_PARMS (1 << 12)
  46. #define DW_UART_CPR_DMA_EXTRA (1 << 13)
  47. #define DW_UART_CPR_FIFO_MODE (0xff << 16)
  48. /* Helper for fifo size calculation */
  49. #define DW_UART_CPR_FIFO_SIZE(a) (((a >> 16) & 0xff) * 16)
  50. struct dw8250_data {
  51. int last_lcr;
  52. int line;
  53. };
  54. static void dw8250_serial_out(struct uart_port *p, int offset, int value)
  55. {
  56. struct dw8250_data *d = p->private_data;
  57. if (offset == UART_LCR)
  58. d->last_lcr = value;
  59. offset <<= p->regshift;
  60. writeb(value, p->membase + offset);
  61. }
  62. static unsigned int dw8250_serial_in(struct uart_port *p, int offset)
  63. {
  64. offset <<= p->regshift;
  65. return readb(p->membase + offset);
  66. }
  67. static void dw8250_serial_out32(struct uart_port *p, int offset, int value)
  68. {
  69. struct dw8250_data *d = p->private_data;
  70. if (offset == UART_LCR)
  71. d->last_lcr = value;
  72. offset <<= p->regshift;
  73. writel(value, p->membase + offset);
  74. }
  75. static unsigned int dw8250_serial_in32(struct uart_port *p, int offset)
  76. {
  77. offset <<= p->regshift;
  78. return readl(p->membase + offset);
  79. }
  80. static int dw8250_handle_irq(struct uart_port *p)
  81. {
  82. struct dw8250_data *d = p->private_data;
  83. unsigned int iir = p->serial_in(p, UART_IIR);
  84. if (serial8250_handle_irq(p, iir)) {
  85. return 1;
  86. } else if ((iir & UART_IIR_BUSY) == UART_IIR_BUSY) {
  87. /* Clear the USR and write the LCR again. */
  88. (void)p->serial_in(p, DW_UART_USR);
  89. p->serial_out(p, d->last_lcr, UART_LCR);
  90. return 1;
  91. }
  92. return 0;
  93. }
  94. static int dw8250_probe_of(struct uart_port *p)
  95. {
  96. struct device_node *np = p->dev->of_node;
  97. u32 val;
  98. if (!of_property_read_u32(np, "reg-io-width", &val)) {
  99. switch (val) {
  100. case 1:
  101. break;
  102. case 4:
  103. p->iotype = UPIO_MEM32;
  104. p->serial_in = dw8250_serial_in32;
  105. p->serial_out = dw8250_serial_out32;
  106. break;
  107. default:
  108. dev_err(p->dev, "unsupported reg-io-width (%u)\n", val);
  109. return -EINVAL;
  110. }
  111. }
  112. if (!of_property_read_u32(np, "reg-shift", &val))
  113. p->regshift = val;
  114. if (of_property_read_u32(np, "clock-frequency", &val)) {
  115. dev_err(p->dev, "no clock-frequency property set\n");
  116. return -EINVAL;
  117. }
  118. p->uartclk = val;
  119. return 0;
  120. }
  121. static int dw8250_probe_acpi(struct uart_port *p)
  122. {
  123. const struct acpi_device_id *id;
  124. u32 reg;
  125. id = acpi_match_device(p->dev->driver->acpi_match_table, p->dev);
  126. if (!id)
  127. return -ENODEV;
  128. p->iotype = UPIO_MEM32;
  129. p->serial_in = dw8250_serial_in32;
  130. p->serial_out = dw8250_serial_out32;
  131. p->regshift = 2;
  132. p->uartclk = (unsigned int)id->driver_data;
  133. /* Fix Haswell issue where the clocks do not get enabled */
  134. if (!strcmp(id->id, "INT33C4") || !strcmp(id->id, "INT33C5")) {
  135. reg = readl(p->membase + LPSS_PRV_CLOCK_PARAMS);
  136. writel(reg | 1, p->membase + LPSS_PRV_CLOCK_PARAMS);
  137. }
  138. return 0;
  139. }
  140. static void dw8250_setup_port(struct uart_8250_port *up)
  141. {
  142. struct uart_port *p = &up->port;
  143. u32 reg = readl(p->membase + DW_UART_UCV);
  144. /*
  145. * If the Component Version Register returns zero, we know that
  146. * ADDITIONAL_FEATURES are not enabled. No need to go any further.
  147. */
  148. if (!reg)
  149. return;
  150. dev_dbg_ratelimited(p->dev, "Designware UART version %c.%c%c\n",
  151. (reg >> 24) & 0xff, (reg >> 16) & 0xff, (reg >> 8) & 0xff);
  152. reg = readl(p->membase + DW_UART_CPR);
  153. if (!reg)
  154. return;
  155. /* Select the type based on fifo */
  156. if (reg & DW_UART_CPR_FIFO_MODE) {
  157. p->type = PORT_16550A;
  158. p->flags |= UPF_FIXED_TYPE;
  159. p->fifosize = DW_UART_CPR_FIFO_SIZE(reg);
  160. up->tx_loadsz = p->fifosize;
  161. }
  162. }
  163. static int dw8250_probe(struct platform_device *pdev)
  164. {
  165. struct uart_8250_port uart = {};
  166. struct resource *regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  167. struct resource *irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  168. struct dw8250_data *data;
  169. int err;
  170. if (!regs || !irq) {
  171. dev_err(&pdev->dev, "no registers/irq defined\n");
  172. return -EINVAL;
  173. }
  174. spin_lock_init(&uart.port.lock);
  175. uart.port.mapbase = regs->start;
  176. uart.port.irq = irq->start;
  177. uart.port.handle_irq = dw8250_handle_irq;
  178. uart.port.type = PORT_8250;
  179. uart.port.flags = UPF_SHARE_IRQ | UPF_BOOT_AUTOCONF | UPF_FIXED_PORT;
  180. uart.port.dev = &pdev->dev;
  181. uart.port.membase = ioremap(regs->start, resource_size(regs));
  182. if (!uart.port.membase)
  183. return -ENOMEM;
  184. uart.port.iotype = UPIO_MEM;
  185. uart.port.serial_in = dw8250_serial_in;
  186. uart.port.serial_out = dw8250_serial_out;
  187. if (pdev->dev.of_node) {
  188. err = dw8250_probe_of(&uart.port);
  189. if (err)
  190. return err;
  191. } else if (ACPI_HANDLE(&pdev->dev)) {
  192. err = dw8250_probe_acpi(&uart.port);
  193. if (err)
  194. return err;
  195. } else {
  196. return -ENODEV;
  197. }
  198. dw8250_setup_port(&uart);
  199. data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
  200. if (!data)
  201. return -ENOMEM;
  202. uart.port.private_data = data;
  203. data->line = serial8250_register_8250_port(&uart);
  204. if (data->line < 0)
  205. return data->line;
  206. platform_set_drvdata(pdev, data);
  207. return 0;
  208. }
  209. static int dw8250_remove(struct platform_device *pdev)
  210. {
  211. struct dw8250_data *data = platform_get_drvdata(pdev);
  212. serial8250_unregister_port(data->line);
  213. return 0;
  214. }
  215. #ifdef CONFIG_PM
  216. static int dw8250_suspend(struct platform_device *pdev, pm_message_t state)
  217. {
  218. struct dw8250_data *data = platform_get_drvdata(pdev);
  219. serial8250_suspend_port(data->line);
  220. return 0;
  221. }
  222. static int dw8250_resume(struct platform_device *pdev)
  223. {
  224. struct dw8250_data *data = platform_get_drvdata(pdev);
  225. serial8250_resume_port(data->line);
  226. return 0;
  227. }
  228. #else
  229. #define dw8250_suspend NULL
  230. #define dw8250_resume NULL
  231. #endif /* CONFIG_PM */
  232. static const struct of_device_id dw8250_of_match[] = {
  233. { .compatible = "snps,dw-apb-uart" },
  234. { /* Sentinel */ }
  235. };
  236. MODULE_DEVICE_TABLE(of, dw8250_of_match);
  237. static const struct acpi_device_id dw8250_acpi_match[] = {
  238. { "INT33C4", 100000000 },
  239. { "INT33C5", 100000000 },
  240. { },
  241. };
  242. MODULE_DEVICE_TABLE(acpi, dw8250_acpi_match);
  243. static struct platform_driver dw8250_platform_driver = {
  244. .driver = {
  245. .name = "dw-apb-uart",
  246. .owner = THIS_MODULE,
  247. .of_match_table = dw8250_of_match,
  248. .acpi_match_table = ACPI_PTR(dw8250_acpi_match),
  249. },
  250. .probe = dw8250_probe,
  251. .remove = dw8250_remove,
  252. .suspend = dw8250_suspend,
  253. .resume = dw8250_resume,
  254. };
  255. module_platform_driver(dw8250_platform_driver);
  256. MODULE_AUTHOR("Jamie Iles");
  257. MODULE_LICENSE("GPL");
  258. MODULE_DESCRIPTION("Synopsys DesignWare 8250 serial port driver");