8250_ingenic.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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. EARLYCON_DECLARE(jz4740_uart, ingenic_early_console_setup);
  104. OF_EARLYCON_DECLARE(jz4740_uart, "ingenic,jz4740-uart",
  105. ingenic_early_console_setup);
  106. EARLYCON_DECLARE(jz4770_uart, ingenic_early_console_setup);
  107. OF_EARLYCON_DECLARE(jz4770_uart, "ingenic,jz4770-uart",
  108. ingenic_early_console_setup);
  109. EARLYCON_DECLARE(jz4775_uart, ingenic_early_console_setup);
  110. OF_EARLYCON_DECLARE(jz4775_uart, "ingenic,jz4775-uart",
  111. ingenic_early_console_setup);
  112. EARLYCON_DECLARE(jz4780_uart, ingenic_early_console_setup);
  113. OF_EARLYCON_DECLARE(jz4780_uart, "ingenic,jz4780-uart",
  114. ingenic_early_console_setup);
  115. static void ingenic_uart_serial_out(struct uart_port *p, int offset, int value)
  116. {
  117. int ier;
  118. switch (offset) {
  119. case UART_FCR:
  120. /* UART module enable */
  121. value |= UART_FCR_UME;
  122. break;
  123. case UART_IER:
  124. /*
  125. * Enable receive timeout interrupt with the receive line
  126. * status interrupt.
  127. */
  128. value |= (value & 0x4) << 2;
  129. break;
  130. case UART_MCR:
  131. /*
  132. * If we have enabled modem status IRQs we should enable
  133. * modem mode.
  134. */
  135. ier = p->serial_in(p, UART_IER);
  136. if (ier & UART_IER_MSI)
  137. value |= UART_MCR_MDCE | UART_MCR_FCM;
  138. else
  139. value &= ~(UART_MCR_MDCE | UART_MCR_FCM);
  140. break;
  141. default:
  142. break;
  143. }
  144. writeb(value, p->membase + (offset << p->regshift));
  145. }
  146. static unsigned int ingenic_uart_serial_in(struct uart_port *p, int offset)
  147. {
  148. unsigned int value;
  149. value = readb(p->membase + (offset << p->regshift));
  150. /* Hide non-16550 compliant bits from higher levels */
  151. switch (offset) {
  152. case UART_FCR:
  153. value &= ~UART_FCR_UME;
  154. break;
  155. case UART_MCR:
  156. value &= ~(UART_MCR_MDCE | UART_MCR_FCM);
  157. break;
  158. default:
  159. break;
  160. }
  161. return value;
  162. }
  163. static int ingenic_uart_probe(struct platform_device *pdev)
  164. {
  165. struct uart_8250_port uart = {};
  166. struct resource *regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  167. struct resource *irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  168. struct ingenic_uart_data *data;
  169. const struct ingenic_uart_config *cdata;
  170. const struct of_device_id *match;
  171. int err, line;
  172. match = of_match_device(of_match, &pdev->dev);
  173. if (!match) {
  174. dev_err(&pdev->dev, "Error: No device match found\n");
  175. return -ENODEV;
  176. }
  177. cdata = match->data;
  178. if (!regs || !irq) {
  179. dev_err(&pdev->dev, "no registers/irq defined\n");
  180. return -EINVAL;
  181. }
  182. data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
  183. if (!data)
  184. return -ENOMEM;
  185. spin_lock_init(&uart.port.lock);
  186. uart.port.type = PORT_16550A;
  187. uart.port.flags = UPF_SKIP_TEST | UPF_IOREMAP | UPF_FIXED_TYPE;
  188. uart.port.iotype = UPIO_MEM;
  189. uart.port.mapbase = regs->start;
  190. uart.port.regshift = 2;
  191. uart.port.serial_out = ingenic_uart_serial_out;
  192. uart.port.serial_in = ingenic_uart_serial_in;
  193. uart.port.irq = irq->start;
  194. uart.port.dev = &pdev->dev;
  195. uart.port.fifosize = cdata->fifosize;
  196. uart.tx_loadsz = cdata->tx_loadsz;
  197. uart.capabilities = UART_CAP_FIFO | UART_CAP_RTOIE;
  198. /* Check for a fixed line number */
  199. line = of_alias_get_id(pdev->dev.of_node, "serial");
  200. if (line >= 0)
  201. uart.port.line = line;
  202. uart.port.membase = devm_ioremap(&pdev->dev, regs->start,
  203. resource_size(regs));
  204. if (!uart.port.membase)
  205. return -ENOMEM;
  206. data->clk_module = devm_clk_get(&pdev->dev, "module");
  207. if (IS_ERR(data->clk_module)) {
  208. err = PTR_ERR(data->clk_module);
  209. if (err != -EPROBE_DEFER)
  210. dev_err(&pdev->dev,
  211. "unable to get module clock: %d\n", err);
  212. return err;
  213. }
  214. data->clk_baud = devm_clk_get(&pdev->dev, "baud");
  215. if (IS_ERR(data->clk_baud)) {
  216. err = PTR_ERR(data->clk_baud);
  217. if (err != -EPROBE_DEFER)
  218. dev_err(&pdev->dev,
  219. "unable to get baud clock: %d\n", err);
  220. return err;
  221. }
  222. err = clk_prepare_enable(data->clk_module);
  223. if (err) {
  224. dev_err(&pdev->dev, "could not enable module clock: %d\n", err);
  225. goto out;
  226. }
  227. err = clk_prepare_enable(data->clk_baud);
  228. if (err) {
  229. dev_err(&pdev->dev, "could not enable baud clock: %d\n", err);
  230. goto out_disable_moduleclk;
  231. }
  232. uart.port.uartclk = clk_get_rate(data->clk_baud);
  233. data->line = serial8250_register_8250_port(&uart);
  234. if (data->line < 0) {
  235. err = data->line;
  236. goto out_disable_baudclk;
  237. }
  238. platform_set_drvdata(pdev, data);
  239. return 0;
  240. out_disable_baudclk:
  241. clk_disable_unprepare(data->clk_baud);
  242. out_disable_moduleclk:
  243. clk_disable_unprepare(data->clk_module);
  244. out:
  245. return err;
  246. }
  247. static int ingenic_uart_remove(struct platform_device *pdev)
  248. {
  249. struct ingenic_uart_data *data = platform_get_drvdata(pdev);
  250. serial8250_unregister_port(data->line);
  251. clk_disable_unprepare(data->clk_module);
  252. clk_disable_unprepare(data->clk_baud);
  253. return 0;
  254. }
  255. static const struct ingenic_uart_config jz4740_uart_config = {
  256. .tx_loadsz = 8,
  257. .fifosize = 16,
  258. };
  259. static const struct ingenic_uart_config jz4760_uart_config = {
  260. .tx_loadsz = 16,
  261. .fifosize = 32,
  262. };
  263. static const struct ingenic_uart_config jz4780_uart_config = {
  264. .tx_loadsz = 32,
  265. .fifosize = 64,
  266. };
  267. static const struct of_device_id of_match[] = {
  268. { .compatible = "ingenic,jz4740-uart", .data = &jz4740_uart_config },
  269. { .compatible = "ingenic,jz4760-uart", .data = &jz4760_uart_config },
  270. { .compatible = "ingenic,jz4770-uart", .data = &jz4760_uart_config },
  271. { .compatible = "ingenic,jz4775-uart", .data = &jz4760_uart_config },
  272. { .compatible = "ingenic,jz4780-uart", .data = &jz4780_uart_config },
  273. { /* sentinel */ }
  274. };
  275. MODULE_DEVICE_TABLE(of, of_match);
  276. static struct platform_driver ingenic_uart_platform_driver = {
  277. .driver = {
  278. .name = "ingenic-uart",
  279. .of_match_table = of_match,
  280. },
  281. .probe = ingenic_uart_probe,
  282. .remove = ingenic_uart_remove,
  283. };
  284. module_platform_driver(ingenic_uart_platform_driver);
  285. MODULE_AUTHOR("Paul Burton");
  286. MODULE_LICENSE("GPL");
  287. MODULE_DESCRIPTION("Ingenic SoC UART driver");