8250_em.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /*
  2. * Renesas Emma Mobile 8250 driver
  3. *
  4. * Copyright (C) 2012 Magnus Damm
  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
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. #include <linux/device.h>
  20. #include <linux/init.h>
  21. #include <linux/io.h>
  22. #include <linux/module.h>
  23. #include <linux/serial_8250.h>
  24. #include <linux/serial_core.h>
  25. #include <linux/serial_reg.h>
  26. #include <linux/platform_device.h>
  27. #include <linux/clk.h>
  28. #include <linux/slab.h>
  29. #include "8250.h"
  30. #define UART_DLL_EM 9
  31. #define UART_DLM_EM 10
  32. struct serial8250_em_priv {
  33. struct clk *sclk;
  34. int line;
  35. };
  36. static void serial8250_em_serial_out(struct uart_port *p, int offset, int value)
  37. {
  38. switch (offset) {
  39. case UART_TX: /* TX @ 0x00 */
  40. writeb(value, p->membase);
  41. break;
  42. case UART_FCR: /* FCR @ 0x0c (+1) */
  43. case UART_LCR: /* LCR @ 0x10 (+1) */
  44. case UART_MCR: /* MCR @ 0x14 (+1) */
  45. case UART_SCR: /* SCR @ 0x20 (+1) */
  46. writel(value, p->membase + ((offset + 1) << 2));
  47. break;
  48. case UART_IER: /* IER @ 0x04 */
  49. value &= 0x0f; /* only 4 valid bits - not Xscale */
  50. /* fall-through */
  51. case UART_DLL_EM: /* DLL @ 0x24 (+9) */
  52. case UART_DLM_EM: /* DLM @ 0x28 (+9) */
  53. writel(value, p->membase + (offset << 2));
  54. }
  55. }
  56. static unsigned int serial8250_em_serial_in(struct uart_port *p, int offset)
  57. {
  58. switch (offset) {
  59. case UART_RX: /* RX @ 0x00 */
  60. return readb(p->membase);
  61. case UART_MCR: /* MCR @ 0x14 (+1) */
  62. case UART_LSR: /* LSR @ 0x18 (+1) */
  63. case UART_MSR: /* MSR @ 0x1c (+1) */
  64. case UART_SCR: /* SCR @ 0x20 (+1) */
  65. return readl(p->membase + ((offset + 1) << 2));
  66. case UART_IER: /* IER @ 0x04 */
  67. case UART_IIR: /* IIR @ 0x08 */
  68. case UART_DLL_EM: /* DLL @ 0x24 (+9) */
  69. case UART_DLM_EM: /* DLM @ 0x28 (+9) */
  70. return readl(p->membase + (offset << 2));
  71. }
  72. return 0;
  73. }
  74. static int serial8250_em_serial_dl_read(struct uart_8250_port *up)
  75. {
  76. return serial_in(up, UART_DLL_EM) | serial_in(up, UART_DLM_EM) << 8;
  77. }
  78. static void serial8250_em_serial_dl_write(struct uart_8250_port *up, int value)
  79. {
  80. serial_out(up, UART_DLL_EM, value & 0xff);
  81. serial_out(up, UART_DLM_EM, value >> 8 & 0xff);
  82. }
  83. static int serial8250_em_probe(struct platform_device *pdev)
  84. {
  85. struct resource *regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  86. struct resource *irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  87. struct serial8250_em_priv *priv;
  88. struct uart_8250_port up;
  89. int ret = -EINVAL;
  90. if (!regs || !irq) {
  91. dev_err(&pdev->dev, "missing registers or irq\n");
  92. goto err0;
  93. }
  94. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  95. if (!priv) {
  96. dev_err(&pdev->dev, "unable to allocate private data\n");
  97. ret = -ENOMEM;
  98. goto err0;
  99. }
  100. priv->sclk = clk_get(&pdev->dev, "sclk");
  101. if (IS_ERR(priv->sclk)) {
  102. dev_err(&pdev->dev, "unable to get clock\n");
  103. ret = PTR_ERR(priv->sclk);
  104. goto err1;
  105. }
  106. memset(&up, 0, sizeof(up));
  107. up.port.mapbase = regs->start;
  108. up.port.irq = irq->start;
  109. up.port.type = PORT_UNKNOWN;
  110. up.port.flags = UPF_BOOT_AUTOCONF | UPF_FIXED_PORT | UPF_IOREMAP;
  111. up.port.dev = &pdev->dev;
  112. up.port.private_data = priv;
  113. clk_enable(priv->sclk);
  114. up.port.uartclk = clk_get_rate(priv->sclk);
  115. up.port.iotype = UPIO_MEM32;
  116. up.port.serial_in = serial8250_em_serial_in;
  117. up.port.serial_out = serial8250_em_serial_out;
  118. up.dl_read = serial8250_em_serial_dl_read;
  119. up.dl_write = serial8250_em_serial_dl_write;
  120. ret = serial8250_register_8250_port(&up);
  121. if (ret < 0) {
  122. dev_err(&pdev->dev, "unable to register 8250 port\n");
  123. goto err2;
  124. }
  125. priv->line = ret;
  126. platform_set_drvdata(pdev, priv);
  127. return 0;
  128. err2:
  129. clk_disable(priv->sclk);
  130. clk_put(priv->sclk);
  131. err1:
  132. kfree(priv);
  133. err0:
  134. return ret;
  135. }
  136. static int serial8250_em_remove(struct platform_device *pdev)
  137. {
  138. struct serial8250_em_priv *priv = platform_get_drvdata(pdev);
  139. serial8250_unregister_port(priv->line);
  140. clk_disable(priv->sclk);
  141. clk_put(priv->sclk);
  142. kfree(priv);
  143. return 0;
  144. }
  145. static const struct of_device_id serial8250_em_dt_ids[] = {
  146. { .compatible = "renesas,em-uart", },
  147. {},
  148. };
  149. MODULE_DEVICE_TABLE(of, serial8250_em_dt_ids);
  150. static struct platform_driver serial8250_em_platform_driver = {
  151. .driver = {
  152. .name = "serial8250-em",
  153. .of_match_table = serial8250_em_dt_ids,
  154. .owner = THIS_MODULE,
  155. },
  156. .probe = serial8250_em_probe,
  157. .remove = serial8250_em_remove,
  158. };
  159. module_platform_driver(serial8250_em_platform_driver);
  160. MODULE_AUTHOR("Magnus Damm");
  161. MODULE_DESCRIPTION("Renesas Emma Mobile 8250 Driver");
  162. MODULE_LICENSE("GPL v2");