8250_dw.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /*
  2. * Synopsys DesignWare 8250 driver.
  3. *
  4. * Copyright 2011 Picochip, Jamie Iles.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * The Synopsys DesignWare 8250 has an extra feature whereby it detects if the
  12. * LCR is written whilst busy. If it is, then a busy detect interrupt is
  13. * raised, the LCR needs to be rewritten and the uart status register read.
  14. */
  15. #include <linux/device.h>
  16. #include <linux/init.h>
  17. #include <linux/io.h>
  18. #include <linux/module.h>
  19. #include <linux/serial_8250.h>
  20. #include <linux/serial_core.h>
  21. #include <linux/serial_reg.h>
  22. #include <linux/of.h>
  23. #include <linux/of_irq.h>
  24. #include <linux/of_platform.h>
  25. #include <linux/platform_device.h>
  26. #include <linux/slab.h>
  27. struct dw8250_data {
  28. int last_lcr;
  29. int line;
  30. };
  31. static void dw8250_serial_out(struct uart_port *p, int offset, int value)
  32. {
  33. struct dw8250_data *d = p->private_data;
  34. if (offset == UART_LCR)
  35. d->last_lcr = value;
  36. offset <<= p->regshift;
  37. writeb(value, p->membase + offset);
  38. }
  39. static unsigned int dw8250_serial_in(struct uart_port *p, int offset)
  40. {
  41. offset <<= p->regshift;
  42. return readb(p->membase + offset);
  43. }
  44. static void dw8250_serial_out32(struct uart_port *p, int offset, int value)
  45. {
  46. struct dw8250_data *d = p->private_data;
  47. if (offset == UART_LCR)
  48. d->last_lcr = value;
  49. offset <<= p->regshift;
  50. writel(value, p->membase + offset);
  51. }
  52. static unsigned int dw8250_serial_in32(struct uart_port *p, int offset)
  53. {
  54. offset <<= p->regshift;
  55. return readl(p->membase + offset);
  56. }
  57. /* Offset for the DesignWare's UART Status Register. */
  58. #define UART_USR 0x1f
  59. static int dw8250_handle_irq(struct uart_port *p)
  60. {
  61. struct dw8250_data *d = p->private_data;
  62. unsigned int iir = p->serial_in(p, UART_IIR);
  63. if (serial8250_handle_irq(p, iir)) {
  64. return 1;
  65. } else if ((iir & UART_IIR_BUSY) == UART_IIR_BUSY) {
  66. /* Clear the USR and write the LCR again. */
  67. (void)p->serial_in(p, UART_USR);
  68. p->serial_out(p, d->last_lcr, UART_LCR);
  69. return 1;
  70. }
  71. return 0;
  72. }
  73. static int __devinit dw8250_probe(struct platform_device *pdev)
  74. {
  75. struct uart_port port = {};
  76. struct resource *regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  77. struct resource *irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  78. struct device_node *np = pdev->dev.of_node;
  79. u32 val;
  80. struct dw8250_data *data;
  81. if (!regs || !irq) {
  82. dev_err(&pdev->dev, "no registers/irq defined\n");
  83. return -EINVAL;
  84. }
  85. data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
  86. if (!data)
  87. return -ENOMEM;
  88. port.private_data = data;
  89. spin_lock_init(&port.lock);
  90. port.mapbase = regs->start;
  91. port.irq = irq->start;
  92. port.handle_irq = dw8250_handle_irq;
  93. port.type = PORT_8250;
  94. port.flags = UPF_SHARE_IRQ | UPF_BOOT_AUTOCONF | UPF_IOREMAP |
  95. UPF_FIXED_PORT | UPF_FIXED_TYPE;
  96. port.dev = &pdev->dev;
  97. port.iotype = UPIO_MEM;
  98. port.serial_in = dw8250_serial_in;
  99. port.serial_out = dw8250_serial_out;
  100. if (!of_property_read_u32(np, "reg-io-width", &val)) {
  101. switch (val) {
  102. case 1:
  103. break;
  104. case 4:
  105. port.iotype = UPIO_MEM32;
  106. port.serial_in = dw8250_serial_in32;
  107. port.serial_out = dw8250_serial_out32;
  108. break;
  109. default:
  110. dev_err(&pdev->dev, "unsupported reg-io-width (%u)\n",
  111. val);
  112. return -EINVAL;
  113. }
  114. }
  115. if (!of_property_read_u32(np, "reg-shift", &val))
  116. port.regshift = val;
  117. if (of_property_read_u32(np, "clock-frequency", &val)) {
  118. dev_err(&pdev->dev, "no clock-frequency property set\n");
  119. return -EINVAL;
  120. }
  121. port.uartclk = val;
  122. data->line = serial8250_register_port(&port);
  123. if (data->line < 0)
  124. return data->line;
  125. platform_set_drvdata(pdev, data);
  126. return 0;
  127. }
  128. static int __devexit dw8250_remove(struct platform_device *pdev)
  129. {
  130. struct dw8250_data *data = platform_get_drvdata(pdev);
  131. serial8250_unregister_port(data->line);
  132. return 0;
  133. }
  134. static const struct of_device_id dw8250_match[] = {
  135. { .compatible = "snps,dw-apb-uart" },
  136. { /* Sentinel */ }
  137. };
  138. MODULE_DEVICE_TABLE(of, dw8250_match);
  139. static struct platform_driver dw8250_platform_driver = {
  140. .driver = {
  141. .name = "dw-apb-uart",
  142. .owner = THIS_MODULE,
  143. .of_match_table = dw8250_match,
  144. },
  145. .probe = dw8250_probe,
  146. .remove = __devexit_p(dw8250_remove),
  147. };
  148. static int __init dw8250_init(void)
  149. {
  150. return platform_driver_register(&dw8250_platform_driver);
  151. }
  152. module_init(dw8250_init);
  153. static void __exit dw8250_exit(void)
  154. {
  155. platform_driver_unregister(&dw8250_platform_driver);
  156. }
  157. module_exit(dw8250_exit);
  158. MODULE_AUTHOR("Jamie Iles");
  159. MODULE_LICENSE("GPL");
  160. MODULE_DESCRIPTION("Synopsys DesignWare 8250 serial port driver");