8250_ingenic.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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/of_device.h>
  24. #include <linux/platform_device.h>
  25. #include <linux/serial_8250.h>
  26. #include <linux/serial_core.h>
  27. #include <linux/serial_reg.h>
  28. #include "8250.h"
  29. /** ingenic_uart_config: SOC specific config data. */
  30. struct ingenic_uart_config {
  31. int tx_loadsz;
  32. int fifosize;
  33. };
  34. struct ingenic_uart_data {
  35. struct clk *clk_module;
  36. struct clk *clk_baud;
  37. int line;
  38. };
  39. static const struct of_device_id of_match[];
  40. #define UART_FCR_UME BIT(4)
  41. #define UART_MCR_MDCE BIT(7)
  42. #define UART_MCR_FCM BIT(6)
  43. static struct earlycon_device *early_device;
  44. static uint8_t __init early_in(struct uart_port *port, int offset)
  45. {
  46. return readl(port->membase + (offset << 2));
  47. }
  48. static void __init early_out(struct uart_port *port, int offset, uint8_t value)
  49. {
  50. writel(value, port->membase + (offset << 2));
  51. }
  52. static void __init ingenic_early_console_putc(struct uart_port *port, int c)
  53. {
  54. uint8_t lsr;
  55. do {
  56. lsr = early_in(port, UART_LSR);
  57. } while ((lsr & UART_LSR_TEMT) == 0);
  58. early_out(port, UART_TX, c);
  59. }
  60. static void __init ingenic_early_console_write(struct console *console,
  61. const char *s, unsigned int count)
  62. {
  63. uart_console_write(&early_device->port, s, count,
  64. ingenic_early_console_putc);
  65. }
  66. static void __init ingenic_early_console_setup_clock(struct earlycon_device *dev)
  67. {
  68. void *fdt = initial_boot_params;
  69. const __be32 *prop;
  70. int offset;
  71. offset = fdt_path_offset(fdt, "/ext");
  72. if (offset < 0)
  73. return;
  74. prop = fdt_getprop(fdt, offset, "clock-frequency", NULL);
  75. if (!prop)
  76. return;
  77. dev->port.uartclk = be32_to_cpup(prop);
  78. }
  79. static int __init ingenic_early_console_setup(struct earlycon_device *dev,
  80. const char *opt)
  81. {
  82. struct uart_port *port = &dev->port;
  83. unsigned int baud, divisor;
  84. if (!dev->port.membase)
  85. return -ENODEV;
  86. ingenic_early_console_setup_clock(dev);
  87. baud = dev->baud ?: 115200;
  88. divisor = DIV_ROUND_CLOSEST(port->uartclk, 16 * baud);
  89. early_out(port, UART_IER, 0);
  90. early_out(port, UART_LCR, UART_LCR_DLAB | UART_LCR_WLEN8);
  91. early_out(port, UART_DLL, 0);
  92. early_out(port, UART_DLM, 0);
  93. early_out(port, UART_LCR, UART_LCR_WLEN8);
  94. early_out(port, UART_FCR, UART_FCR_UME | UART_FCR_CLEAR_XMIT |
  95. UART_FCR_CLEAR_RCVR | UART_FCR_ENABLE_FIFO);
  96. early_out(port, UART_MCR, UART_MCR_RTS | UART_MCR_DTR);
  97. early_out(port, UART_LCR, UART_LCR_DLAB | UART_LCR_WLEN8);
  98. early_out(port, UART_DLL, divisor & 0xff);
  99. early_out(port, UART_DLM, (divisor >> 8) & 0xff);
  100. early_out(port, UART_LCR, UART_LCR_WLEN8);
  101. early_device = dev;
  102. dev->con->write = ingenic_early_console_write;
  103. return 0;
  104. }
  105. EARLYCON_DECLARE(jz4740_uart, ingenic_early_console_setup);
  106. OF_EARLYCON_DECLARE(jz4740_uart, "ingenic,jz4740-uart",
  107. ingenic_early_console_setup);
  108. EARLYCON_DECLARE(jz4775_uart, ingenic_early_console_setup);
  109. OF_EARLYCON_DECLARE(jz4775_uart, "ingenic,jz4775-uart",
  110. ingenic_early_console_setup);
  111. EARLYCON_DECLARE(jz4780_uart, ingenic_early_console_setup);
  112. OF_EARLYCON_DECLARE(jz4780_uart, "ingenic,jz4780-uart",
  113. ingenic_early_console_setup);
  114. static void ingenic_uart_serial_out(struct uart_port *p, int offset, int value)
  115. {
  116. int ier;
  117. switch (offset) {
  118. case UART_FCR:
  119. /* UART module enable */
  120. value |= UART_FCR_UME;
  121. break;
  122. case UART_IER:
  123. /*
  124. * Enable receive timeout interrupt with the receive line
  125. * status interrupt.
  126. */
  127. value |= (value & 0x4) << 2;
  128. break;
  129. case UART_MCR:
  130. /*
  131. * If we have enabled modem status IRQs we should enable
  132. * modem mode.
  133. */
  134. ier = p->serial_in(p, UART_IER);
  135. if (ier & UART_IER_MSI)
  136. value |= UART_MCR_MDCE | UART_MCR_FCM;
  137. else
  138. value &= ~(UART_MCR_MDCE | UART_MCR_FCM);
  139. break;
  140. default:
  141. break;
  142. }
  143. writeb(value, p->membase + (offset << p->regshift));
  144. }
  145. static unsigned int ingenic_uart_serial_in(struct uart_port *p, int offset)
  146. {
  147. unsigned int value;
  148. value = readb(p->membase + (offset << p->regshift));
  149. /* Hide non-16550 compliant bits from higher levels */
  150. switch (offset) {
  151. case UART_FCR:
  152. value &= ~UART_FCR_UME;
  153. break;
  154. case UART_MCR:
  155. value &= ~(UART_MCR_MDCE | UART_MCR_FCM);
  156. break;
  157. default:
  158. break;
  159. }
  160. return value;
  161. }
  162. static int ingenic_uart_probe(struct platform_device *pdev)
  163. {
  164. struct uart_8250_port uart = {};
  165. struct resource *regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  166. struct resource *irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  167. struct ingenic_uart_data *data;
  168. const struct ingenic_uart_config *cdata;
  169. const struct of_device_id *match;
  170. int err, line;
  171. match = of_match_device(of_match, &pdev->dev);
  172. if (!match) {
  173. dev_err(&pdev->dev, "Error: No device match found\n");
  174. return -ENODEV;
  175. }
  176. cdata = match->data;
  177. if (!regs || !irq) {
  178. dev_err(&pdev->dev, "no registers/irq defined\n");
  179. return -EINVAL;
  180. }
  181. data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
  182. if (!data)
  183. return -ENOMEM;
  184. spin_lock_init(&uart.port.lock);
  185. uart.port.type = PORT_16550A;
  186. uart.port.flags = UPF_SKIP_TEST | UPF_IOREMAP | UPF_FIXED_TYPE;
  187. uart.port.iotype = UPIO_MEM;
  188. uart.port.mapbase = regs->start;
  189. uart.port.regshift = 2;
  190. uart.port.serial_out = ingenic_uart_serial_out;
  191. uart.port.serial_in = ingenic_uart_serial_in;
  192. uart.port.irq = irq->start;
  193. uart.port.dev = &pdev->dev;
  194. uart.port.fifosize = cdata->fifosize;
  195. uart.tx_loadsz = cdata->tx_loadsz;
  196. uart.capabilities = UART_CAP_FIFO | UART_CAP_RTOIE;
  197. /* Check for a fixed line number */
  198. line = of_alias_get_id(pdev->dev.of_node, "serial");
  199. if (line >= 0)
  200. uart.port.line = line;
  201. uart.port.membase = devm_ioremap(&pdev->dev, regs->start,
  202. resource_size(regs));
  203. if (!uart.port.membase)
  204. return -ENOMEM;
  205. data->clk_module = devm_clk_get(&pdev->dev, "module");
  206. if (IS_ERR(data->clk_module)) {
  207. err = PTR_ERR(data->clk_module);
  208. if (err != -EPROBE_DEFER)
  209. dev_err(&pdev->dev,
  210. "unable to get module clock: %d\n", err);
  211. return err;
  212. }
  213. data->clk_baud = devm_clk_get(&pdev->dev, "baud");
  214. if (IS_ERR(data->clk_baud)) {
  215. err = PTR_ERR(data->clk_baud);
  216. if (err != -EPROBE_DEFER)
  217. dev_err(&pdev->dev,
  218. "unable to get baud clock: %d\n", err);
  219. return err;
  220. }
  221. err = clk_prepare_enable(data->clk_module);
  222. if (err) {
  223. dev_err(&pdev->dev, "could not enable module clock: %d\n", err);
  224. goto out;
  225. }
  226. err = clk_prepare_enable(data->clk_baud);
  227. if (err) {
  228. dev_err(&pdev->dev, "could not enable baud clock: %d\n", err);
  229. goto out_disable_moduleclk;
  230. }
  231. uart.port.uartclk = clk_get_rate(data->clk_baud);
  232. data->line = serial8250_register_8250_port(&uart);
  233. if (data->line < 0) {
  234. err = data->line;
  235. goto out_disable_baudclk;
  236. }
  237. platform_set_drvdata(pdev, data);
  238. return 0;
  239. out_disable_baudclk:
  240. clk_disable_unprepare(data->clk_baud);
  241. out_disable_moduleclk:
  242. clk_disable_unprepare(data->clk_module);
  243. out:
  244. return err;
  245. }
  246. static int ingenic_uart_remove(struct platform_device *pdev)
  247. {
  248. struct ingenic_uart_data *data = platform_get_drvdata(pdev);
  249. serial8250_unregister_port(data->line);
  250. clk_disable_unprepare(data->clk_module);
  251. clk_disable_unprepare(data->clk_baud);
  252. return 0;
  253. }
  254. static const struct ingenic_uart_config jz4740_uart_config = {
  255. .tx_loadsz = 8,
  256. .fifosize = 16,
  257. };
  258. static const struct ingenic_uart_config jz4760_uart_config = {
  259. .tx_loadsz = 16,
  260. .fifosize = 32,
  261. };
  262. static const struct ingenic_uart_config jz4780_uart_config = {
  263. .tx_loadsz = 32,
  264. .fifosize = 64,
  265. };
  266. static const struct of_device_id of_match[] = {
  267. { .compatible = "ingenic,jz4740-uart", .data = &jz4740_uart_config },
  268. { .compatible = "ingenic,jz4760-uart", .data = &jz4760_uart_config },
  269. { .compatible = "ingenic,jz4775-uart", .data = &jz4760_uart_config },
  270. { .compatible = "ingenic,jz4780-uart", .data = &jz4780_uart_config },
  271. { /* sentinel */ }
  272. };
  273. MODULE_DEVICE_TABLE(of, of_match);
  274. static struct platform_driver ingenic_uart_platform_driver = {
  275. .driver = {
  276. .name = "ingenic-uart",
  277. .of_match_table = of_match,
  278. },
  279. .probe = ingenic_uart_probe,
  280. .remove = ingenic_uart_remove,
  281. };
  282. module_platform_driver(ingenic_uart_platform_driver);
  283. MODULE_AUTHOR("Paul Burton");
  284. MODULE_LICENSE("GPL");
  285. MODULE_DESCRIPTION("Ingenic SoC UART driver");