8250_ingenic.c 8.6 KB

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