8250_aspeed_vuart.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Serial Port driver for Aspeed VUART device
  4. *
  5. * Copyright (C) 2016 Jeremy Kerr <jk@ozlabs.org>, IBM Corp.
  6. * Copyright (C) 2006 Arnd Bergmann <arnd@arndb.de>, IBM Corp.
  7. */
  8. #include <linux/device.h>
  9. #include <linux/module.h>
  10. #include <linux/of_address.h>
  11. #include <linux/of_irq.h>
  12. #include <linux/of_platform.h>
  13. #include <linux/tty.h>
  14. #include <linux/tty_flip.h>
  15. #include <linux/clk.h>
  16. #include "8250.h"
  17. #define ASPEED_VUART_GCRA 0x20
  18. #define ASPEED_VUART_GCRA_VUART_EN BIT(0)
  19. #define ASPEED_VUART_GCRA_DISABLE_HOST_TX_DISCARD BIT(5)
  20. #define ASPEED_VUART_GCRB 0x24
  21. #define ASPEED_VUART_GCRB_HOST_SIRQ_MASK GENMASK(7, 4)
  22. #define ASPEED_VUART_GCRB_HOST_SIRQ_SHIFT 4
  23. #define ASPEED_VUART_ADDRL 0x28
  24. #define ASPEED_VUART_ADDRH 0x2c
  25. struct aspeed_vuart {
  26. struct device *dev;
  27. void __iomem *regs;
  28. struct clk *clk;
  29. int line;
  30. struct timer_list unthrottle_timer;
  31. struct uart_8250_port *port;
  32. };
  33. /*
  34. * If we fill the tty flip buffers, we throttle the data ready interrupt
  35. * to prevent dropped characters. This timeout defines how long we wait
  36. * to (conditionally, depending on buffer state) unthrottle.
  37. */
  38. static const int unthrottle_timeout = HZ/10;
  39. /*
  40. * The VUART is basically two UART 'front ends' connected by their FIFO
  41. * (no actual serial line in between). One is on the BMC side (management
  42. * controller) and one is on the host CPU side.
  43. *
  44. * It allows the BMC to provide to the host a "UART" that pipes into
  45. * the BMC itself and can then be turned by the BMC into a network console
  46. * of some sort for example.
  47. *
  48. * This driver is for the BMC side. The sysfs files allow the BMC
  49. * userspace which owns the system configuration policy, to specify
  50. * at what IO port and interrupt number the host side will appear
  51. * to the host on the Host <-> BMC LPC bus. It could be different on a
  52. * different system (though most of them use 3f8/4).
  53. */
  54. static ssize_t lpc_address_show(struct device *dev,
  55. struct device_attribute *attr, char *buf)
  56. {
  57. struct aspeed_vuart *vuart = dev_get_drvdata(dev);
  58. u16 addr;
  59. addr = (readb(vuart->regs + ASPEED_VUART_ADDRH) << 8) |
  60. (readb(vuart->regs + ASPEED_VUART_ADDRL));
  61. return snprintf(buf, PAGE_SIZE - 1, "0x%x\n", addr);
  62. }
  63. static ssize_t lpc_address_store(struct device *dev,
  64. struct device_attribute *attr,
  65. const char *buf, size_t count)
  66. {
  67. struct aspeed_vuart *vuart = dev_get_drvdata(dev);
  68. unsigned long val;
  69. int err;
  70. err = kstrtoul(buf, 0, &val);
  71. if (err)
  72. return err;
  73. writeb(val >> 8, vuart->regs + ASPEED_VUART_ADDRH);
  74. writeb(val >> 0, vuart->regs + ASPEED_VUART_ADDRL);
  75. return count;
  76. }
  77. static DEVICE_ATTR_RW(lpc_address);
  78. static ssize_t sirq_show(struct device *dev,
  79. struct device_attribute *attr, char *buf)
  80. {
  81. struct aspeed_vuart *vuart = dev_get_drvdata(dev);
  82. u8 reg;
  83. reg = readb(vuart->regs + ASPEED_VUART_GCRB);
  84. reg &= ASPEED_VUART_GCRB_HOST_SIRQ_MASK;
  85. reg >>= ASPEED_VUART_GCRB_HOST_SIRQ_SHIFT;
  86. return snprintf(buf, PAGE_SIZE - 1, "%u\n", reg);
  87. }
  88. static ssize_t sirq_store(struct device *dev, struct device_attribute *attr,
  89. const char *buf, size_t count)
  90. {
  91. struct aspeed_vuart *vuart = dev_get_drvdata(dev);
  92. unsigned long val;
  93. int err;
  94. u8 reg;
  95. err = kstrtoul(buf, 0, &val);
  96. if (err)
  97. return err;
  98. val <<= ASPEED_VUART_GCRB_HOST_SIRQ_SHIFT;
  99. val &= ASPEED_VUART_GCRB_HOST_SIRQ_MASK;
  100. reg = readb(vuart->regs + ASPEED_VUART_GCRB);
  101. reg &= ~ASPEED_VUART_GCRB_HOST_SIRQ_MASK;
  102. reg |= val;
  103. writeb(reg, vuart->regs + ASPEED_VUART_GCRB);
  104. return count;
  105. }
  106. static DEVICE_ATTR_RW(sirq);
  107. static struct attribute *aspeed_vuart_attrs[] = {
  108. &dev_attr_sirq.attr,
  109. &dev_attr_lpc_address.attr,
  110. NULL,
  111. };
  112. static const struct attribute_group aspeed_vuart_attr_group = {
  113. .attrs = aspeed_vuart_attrs,
  114. };
  115. static void aspeed_vuart_set_enabled(struct aspeed_vuart *vuart, bool enabled)
  116. {
  117. u8 reg = readb(vuart->regs + ASPEED_VUART_GCRA);
  118. if (enabled)
  119. reg |= ASPEED_VUART_GCRA_VUART_EN;
  120. else
  121. reg &= ~ASPEED_VUART_GCRA_VUART_EN;
  122. writeb(reg, vuart->regs + ASPEED_VUART_GCRA);
  123. }
  124. static void aspeed_vuart_set_host_tx_discard(struct aspeed_vuart *vuart,
  125. bool discard)
  126. {
  127. u8 reg;
  128. reg = readb(vuart->regs + ASPEED_VUART_GCRA);
  129. /* If the DISABLE_HOST_TX_DISCARD bit is set, discard is disabled */
  130. if (!discard)
  131. reg |= ASPEED_VUART_GCRA_DISABLE_HOST_TX_DISCARD;
  132. else
  133. reg &= ~ASPEED_VUART_GCRA_DISABLE_HOST_TX_DISCARD;
  134. writeb(reg, vuart->regs + ASPEED_VUART_GCRA);
  135. }
  136. static int aspeed_vuart_startup(struct uart_port *uart_port)
  137. {
  138. struct uart_8250_port *uart_8250_port = up_to_u8250p(uart_port);
  139. struct aspeed_vuart *vuart = uart_8250_port->port.private_data;
  140. int rc;
  141. rc = serial8250_do_startup(uart_port);
  142. if (rc)
  143. return rc;
  144. aspeed_vuart_set_host_tx_discard(vuart, false);
  145. return 0;
  146. }
  147. static void aspeed_vuart_shutdown(struct uart_port *uart_port)
  148. {
  149. struct uart_8250_port *uart_8250_port = up_to_u8250p(uart_port);
  150. struct aspeed_vuart *vuart = uart_8250_port->port.private_data;
  151. aspeed_vuart_set_host_tx_discard(vuart, true);
  152. serial8250_do_shutdown(uart_port);
  153. }
  154. static void __aspeed_vuart_set_throttle(struct uart_8250_port *up,
  155. bool throttle)
  156. {
  157. unsigned char irqs = UART_IER_RLSI | UART_IER_RDI;
  158. up->ier &= ~irqs;
  159. if (!throttle)
  160. up->ier |= irqs;
  161. serial_out(up, UART_IER, up->ier);
  162. }
  163. static void aspeed_vuart_set_throttle(struct uart_port *port, bool throttle)
  164. {
  165. struct uart_8250_port *up = up_to_u8250p(port);
  166. unsigned long flags;
  167. spin_lock_irqsave(&port->lock, flags);
  168. __aspeed_vuart_set_throttle(up, throttle);
  169. spin_unlock_irqrestore(&port->lock, flags);
  170. }
  171. static void aspeed_vuart_throttle(struct uart_port *port)
  172. {
  173. aspeed_vuart_set_throttle(port, true);
  174. }
  175. static void aspeed_vuart_unthrottle(struct uart_port *port)
  176. {
  177. aspeed_vuart_set_throttle(port, false);
  178. }
  179. static void aspeed_vuart_unthrottle_exp(struct timer_list *timer)
  180. {
  181. struct aspeed_vuart *vuart = from_timer(vuart, timer, unthrottle_timer);
  182. struct uart_8250_port *up = vuart->port;
  183. if (!tty_buffer_space_avail(&up->port.state->port)) {
  184. mod_timer(&vuart->unthrottle_timer,
  185. jiffies + unthrottle_timeout);
  186. return;
  187. }
  188. aspeed_vuart_unthrottle(&up->port);
  189. }
  190. /*
  191. * Custom interrupt handler to manage finer-grained flow control. Although we
  192. * have throttle/unthrottle callbacks, we've seen that the VUART device can
  193. * deliver characters faster than the ldisc has a chance to check buffer space
  194. * against the throttle threshold. This results in dropped characters before
  195. * the throttle.
  196. *
  197. * We do this by checking for flip buffer space before RX. If we have no space,
  198. * throttle now and schedule an unthrottle for later, once the ldisc has had
  199. * a chance to drain the buffers.
  200. */
  201. static int aspeed_vuart_handle_irq(struct uart_port *port)
  202. {
  203. struct uart_8250_port *up = up_to_u8250p(port);
  204. unsigned int iir, lsr;
  205. unsigned long flags;
  206. int space, count;
  207. iir = serial_port_in(port, UART_IIR);
  208. if (iir & UART_IIR_NO_INT)
  209. return 0;
  210. spin_lock_irqsave(&port->lock, flags);
  211. lsr = serial_port_in(port, UART_LSR);
  212. if (lsr & (UART_LSR_DR | UART_LSR_BI)) {
  213. space = tty_buffer_space_avail(&port->state->port);
  214. if (!space) {
  215. /* throttle and schedule an unthrottle later */
  216. struct aspeed_vuart *vuart = port->private_data;
  217. __aspeed_vuart_set_throttle(up, true);
  218. if (!timer_pending(&vuart->unthrottle_timer)) {
  219. vuart->port = up;
  220. mod_timer(&vuart->unthrottle_timer,
  221. jiffies + unthrottle_timeout);
  222. }
  223. } else {
  224. count = min(space, 256);
  225. do {
  226. serial8250_read_char(up, lsr);
  227. lsr = serial_in(up, UART_LSR);
  228. if (--count == 0)
  229. break;
  230. } while (lsr & (UART_LSR_DR | UART_LSR_BI));
  231. tty_flip_buffer_push(&port->state->port);
  232. }
  233. }
  234. serial8250_modem_status(up);
  235. if (lsr & UART_LSR_THRE)
  236. serial8250_tx_chars(up);
  237. spin_unlock_irqrestore(&port->lock, flags);
  238. return 1;
  239. }
  240. static int aspeed_vuart_probe(struct platform_device *pdev)
  241. {
  242. struct uart_8250_port port;
  243. struct aspeed_vuart *vuart;
  244. struct device_node *np;
  245. struct resource *res;
  246. u32 clk, prop;
  247. int rc;
  248. np = pdev->dev.of_node;
  249. vuart = devm_kzalloc(&pdev->dev, sizeof(*vuart), GFP_KERNEL);
  250. if (!vuart)
  251. return -ENOMEM;
  252. vuart->dev = &pdev->dev;
  253. timer_setup(&vuart->unthrottle_timer, aspeed_vuart_unthrottle_exp, 0);
  254. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  255. vuart->regs = devm_ioremap_resource(&pdev->dev, res);
  256. if (IS_ERR(vuart->regs))
  257. return PTR_ERR(vuart->regs);
  258. memset(&port, 0, sizeof(port));
  259. port.port.private_data = vuart;
  260. port.port.membase = vuart->regs;
  261. port.port.mapbase = res->start;
  262. port.port.mapsize = resource_size(res);
  263. port.port.startup = aspeed_vuart_startup;
  264. port.port.shutdown = aspeed_vuart_shutdown;
  265. port.port.throttle = aspeed_vuart_throttle;
  266. port.port.unthrottle = aspeed_vuart_unthrottle;
  267. port.port.status = UPSTAT_SYNC_FIFO;
  268. port.port.dev = &pdev->dev;
  269. rc = sysfs_create_group(&vuart->dev->kobj, &aspeed_vuart_attr_group);
  270. if (rc < 0)
  271. return rc;
  272. if (of_property_read_u32(np, "clock-frequency", &clk)) {
  273. vuart->clk = devm_clk_get(&pdev->dev, NULL);
  274. if (IS_ERR(vuart->clk)) {
  275. dev_warn(&pdev->dev,
  276. "clk or clock-frequency not defined\n");
  277. rc = PTR_ERR(vuart->clk);
  278. goto err_sysfs_remove;
  279. }
  280. rc = clk_prepare_enable(vuart->clk);
  281. if (rc < 0)
  282. goto err_sysfs_remove;
  283. clk = clk_get_rate(vuart->clk);
  284. }
  285. /* If current-speed was set, then try not to change it. */
  286. if (of_property_read_u32(np, "current-speed", &prop) == 0)
  287. port.port.custom_divisor = clk / (16 * prop);
  288. /* Check for shifted address mapping */
  289. if (of_property_read_u32(np, "reg-offset", &prop) == 0)
  290. port.port.mapbase += prop;
  291. /* Check for registers offset within the devices address range */
  292. if (of_property_read_u32(np, "reg-shift", &prop) == 0)
  293. port.port.regshift = prop;
  294. /* Check for fifo size */
  295. if (of_property_read_u32(np, "fifo-size", &prop) == 0)
  296. port.port.fifosize = prop;
  297. /* Check for a fixed line number */
  298. rc = of_alias_get_id(np, "serial");
  299. if (rc >= 0)
  300. port.port.line = rc;
  301. port.port.irq = irq_of_parse_and_map(np, 0);
  302. port.port.handle_irq = aspeed_vuart_handle_irq;
  303. port.port.iotype = UPIO_MEM;
  304. port.port.type = PORT_16550A;
  305. port.port.uartclk = clk;
  306. port.port.flags = UPF_SHARE_IRQ | UPF_BOOT_AUTOCONF
  307. | UPF_FIXED_PORT | UPF_FIXED_TYPE | UPF_NO_THRE_TEST;
  308. if (of_property_read_bool(np, "no-loopback-test"))
  309. port.port.flags |= UPF_SKIP_TEST;
  310. if (port.port.fifosize)
  311. port.capabilities = UART_CAP_FIFO;
  312. if (of_property_read_bool(np, "auto-flow-control"))
  313. port.capabilities |= UART_CAP_AFE;
  314. rc = serial8250_register_8250_port(&port);
  315. if (rc < 0)
  316. goto err_clk_disable;
  317. vuart->line = rc;
  318. aspeed_vuart_set_enabled(vuart, true);
  319. aspeed_vuart_set_host_tx_discard(vuart, true);
  320. platform_set_drvdata(pdev, vuart);
  321. return 0;
  322. err_clk_disable:
  323. clk_disable_unprepare(vuart->clk);
  324. irq_dispose_mapping(port.port.irq);
  325. err_sysfs_remove:
  326. sysfs_remove_group(&vuart->dev->kobj, &aspeed_vuart_attr_group);
  327. return rc;
  328. }
  329. static int aspeed_vuart_remove(struct platform_device *pdev)
  330. {
  331. struct aspeed_vuart *vuart = platform_get_drvdata(pdev);
  332. del_timer_sync(&vuart->unthrottle_timer);
  333. aspeed_vuart_set_enabled(vuart, false);
  334. serial8250_unregister_port(vuart->line);
  335. sysfs_remove_group(&vuart->dev->kobj, &aspeed_vuart_attr_group);
  336. clk_disable_unprepare(vuart->clk);
  337. return 0;
  338. }
  339. static const struct of_device_id aspeed_vuart_table[] = {
  340. { .compatible = "aspeed,ast2400-vuart" },
  341. { .compatible = "aspeed,ast2500-vuart" },
  342. { },
  343. };
  344. static struct platform_driver aspeed_vuart_driver = {
  345. .driver = {
  346. .name = "aspeed-vuart",
  347. .of_match_table = aspeed_vuart_table,
  348. },
  349. .probe = aspeed_vuart_probe,
  350. .remove = aspeed_vuart_remove,
  351. };
  352. module_platform_driver(aspeed_vuart_driver);
  353. MODULE_AUTHOR("Jeremy Kerr <jk@ozlabs.org>");
  354. MODULE_LICENSE("GPL");
  355. MODULE_DESCRIPTION("Driver for Aspeed VUART device");