8250_ingenic.c 9.0 KB

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