8250_ingenic.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. /*
  2. * Copyright (C) 2010 Lars-Peter Clausen <lars@metafoo.de>
  3. * Copyright (C) 2015 Imagination Technologies
  4. *
  5. * Ingenic SoC UART support
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License as published by the
  9. * Free Software Foundation; either version 2 of the License, or (at your
  10. * option) any later version.
  11. *
  12. * You should have received a copy of the GNU General Public License along
  13. * with this program; if not, write to the Free Software Foundation, Inc.,
  14. * 675 Mass Ave, Cambridge, MA 02139, USA.
  15. */
  16. #include <linux/clk.h>
  17. #include <linux/console.h>
  18. #include <linux/io.h>
  19. #include <linux/libfdt.h>
  20. #include <linux/module.h>
  21. #include <linux/of.h>
  22. #include <linux/of_fdt.h>
  23. #include <linux/platform_device.h>
  24. #include <linux/serial_8250.h>
  25. #include <linux/serial_core.h>
  26. #include <linux/serial_reg.h>
  27. struct ingenic_uart_data {
  28. struct clk *clk_module;
  29. struct clk *clk_baud;
  30. int line;
  31. };
  32. #define UART_FCR_UME BIT(4)
  33. static struct earlycon_device *early_device;
  34. static uint8_t __init early_in(struct uart_port *port, int offset)
  35. {
  36. return readl(port->membase + (offset << 2));
  37. }
  38. static void __init early_out(struct uart_port *port, int offset, uint8_t value)
  39. {
  40. writel(value, port->membase + (offset << 2));
  41. }
  42. static void __init ingenic_early_console_putc(struct uart_port *port, int c)
  43. {
  44. uint8_t lsr;
  45. do {
  46. lsr = early_in(port, UART_LSR);
  47. } while ((lsr & UART_LSR_TEMT) == 0);
  48. early_out(port, UART_TX, c);
  49. }
  50. static void __init ingenic_early_console_write(struct console *console,
  51. const char *s, unsigned int count)
  52. {
  53. uart_console_write(&early_device->port, s, count,
  54. ingenic_early_console_putc);
  55. }
  56. static void __init ingenic_early_console_setup_clock(struct earlycon_device *dev)
  57. {
  58. void *fdt = initial_boot_params;
  59. const __be32 *prop;
  60. int offset;
  61. offset = fdt_path_offset(fdt, "/ext");
  62. if (offset < 0)
  63. return;
  64. prop = fdt_getprop(fdt, offset, "clock-frequency", NULL);
  65. if (!prop)
  66. return;
  67. dev->port.uartclk = be32_to_cpup(prop);
  68. }
  69. static int __init ingenic_early_console_setup(struct earlycon_device *dev,
  70. const char *opt)
  71. {
  72. struct uart_port *port = &dev->port;
  73. unsigned int baud, divisor;
  74. if (!dev->port.membase)
  75. return -ENODEV;
  76. ingenic_early_console_setup_clock(dev);
  77. baud = dev->baud ?: 115200;
  78. divisor = DIV_ROUND_CLOSEST(port->uartclk, 16 * baud);
  79. early_out(port, UART_IER, 0);
  80. early_out(port, UART_LCR, UART_LCR_DLAB | UART_LCR_WLEN8);
  81. early_out(port, UART_DLL, 0);
  82. early_out(port, UART_DLM, 0);
  83. early_out(port, UART_LCR, UART_LCR_WLEN8);
  84. early_out(port, UART_FCR, UART_FCR_UME | UART_FCR_CLEAR_XMIT |
  85. UART_FCR_CLEAR_RCVR | UART_FCR_ENABLE_FIFO);
  86. early_out(port, UART_MCR, UART_MCR_RTS | UART_MCR_DTR);
  87. early_out(port, UART_LCR, UART_LCR_DLAB | UART_LCR_WLEN8);
  88. early_out(port, UART_DLL, divisor & 0xff);
  89. early_out(port, UART_DLM, (divisor >> 8) & 0xff);
  90. early_out(port, UART_LCR, UART_LCR_WLEN8);
  91. early_device = dev;
  92. dev->con->write = ingenic_early_console_write;
  93. return 0;
  94. }
  95. EARLYCON_DECLARE(jz4740_uart, ingenic_early_console_setup);
  96. OF_EARLYCON_DECLARE(jz4740_uart, "ingenic,jz4740-uart",
  97. ingenic_early_console_setup);
  98. EARLYCON_DECLARE(jz4775_uart, ingenic_early_console_setup);
  99. OF_EARLYCON_DECLARE(jz4775_uart, "ingenic,jz4775-uart",
  100. ingenic_early_console_setup);
  101. EARLYCON_DECLARE(jz4780_uart, ingenic_early_console_setup);
  102. OF_EARLYCON_DECLARE(jz4780_uart, "ingenic,jz4780-uart",
  103. ingenic_early_console_setup);
  104. static void ingenic_uart_serial_out(struct uart_port *p, int offset, int value)
  105. {
  106. switch (offset) {
  107. case UART_FCR:
  108. /* UART module enable */
  109. value |= UART_FCR_UME;
  110. break;
  111. case UART_IER:
  112. value |= (value & 0x4) << 2;
  113. break;
  114. default:
  115. break;
  116. }
  117. writeb(value, p->membase + (offset << p->regshift));
  118. }
  119. static int ingenic_uart_probe(struct platform_device *pdev)
  120. {
  121. struct uart_8250_port uart = {};
  122. struct resource *regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  123. struct resource *irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  124. struct ingenic_uart_data *data;
  125. int err, line;
  126. if (!regs || !irq) {
  127. dev_err(&pdev->dev, "no registers/irq defined\n");
  128. return -EINVAL;
  129. }
  130. data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
  131. if (!data)
  132. return -ENOMEM;
  133. spin_lock_init(&uart.port.lock);
  134. uart.port.type = PORT_16550;
  135. uart.port.flags = UPF_SKIP_TEST | UPF_IOREMAP | UPF_FIXED_TYPE;
  136. uart.port.iotype = UPIO_MEM;
  137. uart.port.mapbase = regs->start;
  138. uart.port.regshift = 2;
  139. uart.port.serial_out = ingenic_uart_serial_out;
  140. uart.port.irq = irq->start;
  141. uart.port.dev = &pdev->dev;
  142. /* Check for a fixed line number */
  143. line = of_alias_get_id(pdev->dev.of_node, "serial");
  144. if (line >= 0)
  145. uart.port.line = line;
  146. uart.port.membase = devm_ioremap(&pdev->dev, regs->start,
  147. resource_size(regs));
  148. if (!uart.port.membase)
  149. return -ENOMEM;
  150. data->clk_module = devm_clk_get(&pdev->dev, "module");
  151. if (IS_ERR(data->clk_module)) {
  152. err = PTR_ERR(data->clk_module);
  153. if (err != -EPROBE_DEFER)
  154. dev_err(&pdev->dev,
  155. "unable to get module clock: %d\n", err);
  156. return err;
  157. }
  158. data->clk_baud = devm_clk_get(&pdev->dev, "baud");
  159. if (IS_ERR(data->clk_baud)) {
  160. err = PTR_ERR(data->clk_baud);
  161. if (err != -EPROBE_DEFER)
  162. dev_err(&pdev->dev,
  163. "unable to get baud clock: %d\n", err);
  164. return err;
  165. }
  166. err = clk_prepare_enable(data->clk_module);
  167. if (err) {
  168. dev_err(&pdev->dev, "could not enable module clock: %d\n", err);
  169. goto out;
  170. }
  171. err = clk_prepare_enable(data->clk_baud);
  172. if (err) {
  173. dev_err(&pdev->dev, "could not enable baud clock: %d\n", err);
  174. goto out_disable_moduleclk;
  175. }
  176. uart.port.uartclk = clk_get_rate(data->clk_baud);
  177. data->line = serial8250_register_8250_port(&uart);
  178. if (data->line < 0) {
  179. err = data->line;
  180. goto out_disable_baudclk;
  181. }
  182. platform_set_drvdata(pdev, data);
  183. return 0;
  184. out_disable_baudclk:
  185. clk_disable_unprepare(data->clk_baud);
  186. out_disable_moduleclk:
  187. clk_disable_unprepare(data->clk_module);
  188. out:
  189. return err;
  190. }
  191. static int ingenic_uart_remove(struct platform_device *pdev)
  192. {
  193. struct ingenic_uart_data *data = platform_get_drvdata(pdev);
  194. serial8250_unregister_port(data->line);
  195. clk_disable_unprepare(data->clk_module);
  196. clk_disable_unprepare(data->clk_baud);
  197. return 0;
  198. }
  199. static const struct of_device_id of_match[] = {
  200. { .compatible = "ingenic,jz4740-uart" },
  201. { .compatible = "ingenic,jz4775-uart" },
  202. { .compatible = "ingenic,jz4780-uart" },
  203. { /* sentinel */ }
  204. };
  205. MODULE_DEVICE_TABLE(of, of_match);
  206. static struct platform_driver ingenic_uart_platform_driver = {
  207. .driver = {
  208. .name = "ingenic-uart",
  209. .of_match_table = of_match,
  210. },
  211. .probe = ingenic_uart_probe,
  212. .remove = ingenic_uart_remove,
  213. };
  214. module_platform_driver(ingenic_uart_platform_driver);
  215. MODULE_AUTHOR("Paul Burton");
  216. MODULE_LICENSE("GPL");
  217. MODULE_DESCRIPTION("Ingenic SoC UART driver");