8250_aspeed_vuart.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580
  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/regmap.h>
  14. #include <linux/mfd/syscon.h>
  15. #include <linux/tty.h>
  16. #include <linux/tty_flip.h>
  17. #include <linux/clk.h>
  18. #include "8250.h"
  19. #define ASPEED_VUART_GCRA 0x20
  20. #define ASPEED_VUART_GCRA_VUART_EN BIT(0)
  21. #define ASPEED_VUART_GCRA_HOST_SIRQ_POLARITY BIT(1)
  22. #define ASPEED_VUART_GCRA_DISABLE_HOST_TX_DISCARD BIT(5)
  23. #define ASPEED_VUART_GCRB 0x24
  24. #define ASPEED_VUART_GCRB_HOST_SIRQ_MASK GENMASK(7, 4)
  25. #define ASPEED_VUART_GCRB_HOST_SIRQ_SHIFT 4
  26. #define ASPEED_VUART_ADDRL 0x28
  27. #define ASPEED_VUART_ADDRH 0x2c
  28. #define ASPEED_VUART_DEFAULT_LPC_ADDR 0x3f8
  29. #define ASPEED_VUART_DEFAULT_SIRQ 4
  30. #define ASPEED_VUART_DEFAULT_SIRQ_POLARITY IRQ_TYPE_LEVEL_LOW
  31. struct aspeed_vuart {
  32. struct device *dev;
  33. int line;
  34. struct timer_list unthrottle_timer;
  35. struct uart_8250_port *port;
  36. };
  37. /*
  38. * If we fill the tty flip buffers, we throttle the data ready interrupt
  39. * to prevent dropped characters. This timeout defines how long we wait
  40. * to (conditionally, depending on buffer state) unthrottle.
  41. */
  42. static const int unthrottle_timeout = HZ/10;
  43. /*
  44. * The VUART is basically two UART 'front ends' connected by their FIFO
  45. * (no actual serial line in between). One is on the BMC side (management
  46. * controller) and one is on the host CPU side.
  47. *
  48. * It allows the BMC to provide to the host a "UART" that pipes into
  49. * the BMC itself and can then be turned by the BMC into a network console
  50. * of some sort for example.
  51. *
  52. * This driver is for the BMC side. The sysfs files allow the BMC
  53. * userspace which owns the system configuration policy, to specify
  54. * at what IO port and interrupt number the host side will appear
  55. * to the host on the Host <-> BMC LPC bus. It could be different on a
  56. * different system (though most of them use 3f8/4).
  57. */
  58. static inline u8 aspeed_vuart_readb(struct aspeed_vuart *vuart, u8 reg)
  59. {
  60. return readb(vuart->port->port.membase + reg);
  61. }
  62. static inline void aspeed_vuart_writeb(struct aspeed_vuart *vuart, u8 val, u8 reg)
  63. {
  64. writeb(val, vuart->port->port.membase + reg);
  65. }
  66. static ssize_t lpc_address_show(struct device *dev,
  67. struct device_attribute *attr, char *buf)
  68. {
  69. struct aspeed_vuart *vuart = dev_get_drvdata(dev);
  70. u16 addr;
  71. addr = (aspeed_vuart_readb(vuart, ASPEED_VUART_ADDRH) << 8) |
  72. (aspeed_vuart_readb(vuart, ASPEED_VUART_ADDRL));
  73. return sysfs_emit(buf, "0x%x\n", addr);
  74. }
  75. static int aspeed_vuart_set_lpc_address(struct aspeed_vuart *vuart, u32 addr)
  76. {
  77. if (addr > U16_MAX)
  78. return -EINVAL;
  79. aspeed_vuart_writeb(vuart, addr >> 8, ASPEED_VUART_ADDRH);
  80. aspeed_vuart_writeb(vuart, addr >> 0, ASPEED_VUART_ADDRL);
  81. return 0;
  82. }
  83. static ssize_t lpc_address_store(struct device *dev,
  84. struct device_attribute *attr,
  85. const char *buf, size_t count)
  86. {
  87. struct aspeed_vuart *vuart = dev_get_drvdata(dev);
  88. u32 val;
  89. int err;
  90. err = kstrtou32(buf, 0, &val);
  91. if (err)
  92. return err;
  93. err = aspeed_vuart_set_lpc_address(vuart, val);
  94. return err ? : count;
  95. }
  96. static DEVICE_ATTR_RW(lpc_address);
  97. static ssize_t sirq_show(struct device *dev,
  98. struct device_attribute *attr, char *buf)
  99. {
  100. struct aspeed_vuart *vuart = dev_get_drvdata(dev);
  101. u8 reg;
  102. reg = aspeed_vuart_readb(vuart, ASPEED_VUART_GCRB);
  103. reg &= ASPEED_VUART_GCRB_HOST_SIRQ_MASK;
  104. reg >>= ASPEED_VUART_GCRB_HOST_SIRQ_SHIFT;
  105. return sysfs_emit(buf, "%u\n", reg);
  106. }
  107. static int aspeed_vuart_set_sirq(struct aspeed_vuart *vuart, u32 sirq)
  108. {
  109. u8 reg;
  110. if (sirq > (ASPEED_VUART_GCRB_HOST_SIRQ_MASK >> ASPEED_VUART_GCRB_HOST_SIRQ_SHIFT))
  111. return -EINVAL;
  112. sirq <<= ASPEED_VUART_GCRB_HOST_SIRQ_SHIFT;
  113. sirq &= ASPEED_VUART_GCRB_HOST_SIRQ_MASK;
  114. reg = aspeed_vuart_readb(vuart, ASPEED_VUART_GCRB);
  115. reg &= ~ASPEED_VUART_GCRB_HOST_SIRQ_MASK;
  116. reg |= sirq;
  117. aspeed_vuart_writeb(vuart, reg, ASPEED_VUART_GCRB);
  118. return 0;
  119. }
  120. static ssize_t sirq_store(struct device *dev, struct device_attribute *attr,
  121. const char *buf, size_t count)
  122. {
  123. struct aspeed_vuart *vuart = dev_get_drvdata(dev);
  124. unsigned long val;
  125. int err;
  126. err = kstrtoul(buf, 0, &val);
  127. if (err)
  128. return err;
  129. err = aspeed_vuart_set_sirq(vuart, val);
  130. return err ? : count;
  131. }
  132. static DEVICE_ATTR_RW(sirq);
  133. static ssize_t sirq_polarity_show(struct device *dev,
  134. struct device_attribute *attr, char *buf)
  135. {
  136. struct aspeed_vuart *vuart = dev_get_drvdata(dev);
  137. u8 reg;
  138. reg = aspeed_vuart_readb(vuart, ASPEED_VUART_GCRA);
  139. reg &= ASPEED_VUART_GCRA_HOST_SIRQ_POLARITY;
  140. return sysfs_emit(buf, "%u\n", reg ? 1 : 0);
  141. }
  142. static void aspeed_vuart_set_sirq_polarity(struct aspeed_vuart *vuart,
  143. bool polarity)
  144. {
  145. u8 reg = aspeed_vuart_readb(vuart, ASPEED_VUART_GCRA);
  146. if (polarity)
  147. reg |= ASPEED_VUART_GCRA_HOST_SIRQ_POLARITY;
  148. else
  149. reg &= ~ASPEED_VUART_GCRA_HOST_SIRQ_POLARITY;
  150. aspeed_vuart_writeb(vuart, reg, ASPEED_VUART_GCRA);
  151. }
  152. static ssize_t sirq_polarity_store(struct device *dev,
  153. struct device_attribute *attr,
  154. const char *buf, size_t count)
  155. {
  156. struct aspeed_vuart *vuart = dev_get_drvdata(dev);
  157. unsigned long val;
  158. int err;
  159. err = kstrtoul(buf, 0, &val);
  160. if (err)
  161. return err;
  162. aspeed_vuart_set_sirq_polarity(vuart, val != 0);
  163. return count;
  164. }
  165. static DEVICE_ATTR_RW(sirq_polarity);
  166. static struct attribute *aspeed_vuart_attrs[] = {
  167. &dev_attr_sirq.attr,
  168. &dev_attr_sirq_polarity.attr,
  169. &dev_attr_lpc_address.attr,
  170. NULL,
  171. };
  172. static const struct attribute_group aspeed_vuart_attr_group = {
  173. .attrs = aspeed_vuart_attrs,
  174. };
  175. static void aspeed_vuart_set_enabled(struct aspeed_vuart *vuart, bool enabled)
  176. {
  177. u8 reg = aspeed_vuart_readb(vuart, ASPEED_VUART_GCRA);
  178. if (enabled)
  179. reg |= ASPEED_VUART_GCRA_VUART_EN;
  180. else
  181. reg &= ~ASPEED_VUART_GCRA_VUART_EN;
  182. aspeed_vuart_writeb(vuart, reg, ASPEED_VUART_GCRA);
  183. }
  184. static void aspeed_vuart_set_host_tx_discard(struct aspeed_vuart *vuart,
  185. bool discard)
  186. {
  187. u8 reg;
  188. reg = aspeed_vuart_readb(vuart, ASPEED_VUART_GCRA);
  189. /* If the DISABLE_HOST_TX_DISCARD bit is set, discard is disabled */
  190. if (!discard)
  191. reg |= ASPEED_VUART_GCRA_DISABLE_HOST_TX_DISCARD;
  192. else
  193. reg &= ~ASPEED_VUART_GCRA_DISABLE_HOST_TX_DISCARD;
  194. aspeed_vuart_writeb(vuart, reg, ASPEED_VUART_GCRA);
  195. }
  196. static int aspeed_vuart_startup(struct uart_port *uart_port)
  197. {
  198. struct uart_8250_port *uart_8250_port = up_to_u8250p(uart_port);
  199. struct aspeed_vuart *vuart = uart_8250_port->port.private_data;
  200. int rc;
  201. rc = serial8250_do_startup(uart_port);
  202. if (rc)
  203. return rc;
  204. aspeed_vuart_set_host_tx_discard(vuart, false);
  205. return 0;
  206. }
  207. static void aspeed_vuart_shutdown(struct uart_port *uart_port)
  208. {
  209. struct uart_8250_port *uart_8250_port = up_to_u8250p(uart_port);
  210. struct aspeed_vuart *vuart = uart_8250_port->port.private_data;
  211. aspeed_vuart_set_host_tx_discard(vuart, true);
  212. serial8250_do_shutdown(uart_port);
  213. }
  214. static void __aspeed_vuart_set_throttle(struct uart_8250_port *up,
  215. bool throttle)
  216. {
  217. unsigned char irqs = UART_IER_RLSI | UART_IER_RDI;
  218. /* Port locked to synchronize UART_IER access against the console. */
  219. lockdep_assert_held_once(&up->port.lock);
  220. up->ier &= ~irqs;
  221. if (!throttle)
  222. up->ier |= irqs;
  223. serial_out(up, UART_IER, up->ier);
  224. }
  225. static void aspeed_vuart_set_throttle(struct uart_port *port, bool throttle)
  226. {
  227. struct uart_8250_port *up = up_to_u8250p(port);
  228. unsigned long flags;
  229. uart_port_lock_irqsave(port, &flags);
  230. __aspeed_vuart_set_throttle(up, throttle);
  231. uart_port_unlock_irqrestore(port, flags);
  232. }
  233. static void aspeed_vuart_throttle(struct uart_port *port)
  234. {
  235. aspeed_vuart_set_throttle(port, true);
  236. }
  237. static void aspeed_vuart_unthrottle(struct uart_port *port)
  238. {
  239. aspeed_vuart_set_throttle(port, false);
  240. }
  241. static void aspeed_vuart_unthrottle_exp(struct timer_list *timer)
  242. {
  243. struct aspeed_vuart *vuart = timer_container_of(vuart, timer,
  244. unthrottle_timer);
  245. struct uart_8250_port *up = vuart->port;
  246. if (!tty_buffer_space_avail(&up->port.state->port)) {
  247. mod_timer(&vuart->unthrottle_timer,
  248. jiffies + unthrottle_timeout);
  249. return;
  250. }
  251. aspeed_vuart_unthrottle(&up->port);
  252. }
  253. /*
  254. * Custom interrupt handler to manage finer-grained flow control. Although we
  255. * have throttle/unthrottle callbacks, we've seen that the VUART device can
  256. * deliver characters faster than the ldisc has a chance to check buffer space
  257. * against the throttle threshold. This results in dropped characters before
  258. * the throttle.
  259. *
  260. * We do this by checking for flip buffer space before RX. If we have no space,
  261. * throttle now and schedule an unthrottle for later, once the ldisc has had
  262. * a chance to drain the buffers.
  263. */
  264. static int aspeed_vuart_handle_irq(struct uart_port *port)
  265. {
  266. struct uart_8250_port *up = up_to_u8250p(port);
  267. unsigned int iir, lsr;
  268. unsigned long flags;
  269. unsigned int space, count;
  270. iir = serial_port_in(port, UART_IIR);
  271. if (iir & UART_IIR_NO_INT)
  272. return 0;
  273. uart_port_lock_irqsave(port, &flags);
  274. lsr = serial_port_in(port, UART_LSR);
  275. if (lsr & (UART_LSR_DR | UART_LSR_BI)) {
  276. space = tty_buffer_space_avail(&port->state->port);
  277. if (!space) {
  278. /* throttle and schedule an unthrottle later */
  279. struct aspeed_vuart *vuart = port->private_data;
  280. __aspeed_vuart_set_throttle(up, true);
  281. if (!timer_pending(&vuart->unthrottle_timer))
  282. mod_timer(&vuart->unthrottle_timer,
  283. jiffies + unthrottle_timeout);
  284. } else {
  285. count = min(space, 256U);
  286. do {
  287. serial8250_read_char(up, lsr);
  288. lsr = serial_in(up, UART_LSR);
  289. if (--count == 0)
  290. break;
  291. } while (lsr & (UART_LSR_DR | UART_LSR_BI));
  292. tty_flip_buffer_push(&port->state->port);
  293. }
  294. }
  295. serial8250_modem_status(up);
  296. if (lsr & UART_LSR_THRE)
  297. serial8250_tx_chars(up);
  298. uart_unlock_and_check_sysrq_irqrestore(port, flags);
  299. return 1;
  300. }
  301. static void aspeed_vuart_auto_configure_sirq_polarity(
  302. struct aspeed_vuart *vuart, struct device_node *syscon_np,
  303. u32 reg_offset, u32 reg_mask)
  304. {
  305. struct regmap *regmap;
  306. u32 value;
  307. regmap = syscon_node_to_regmap(syscon_np);
  308. if (IS_ERR(regmap)) {
  309. dev_warn(vuart->dev,
  310. "could not get regmap for aspeed,sirq-polarity-sense\n");
  311. return;
  312. }
  313. if (regmap_read(regmap, reg_offset, &value)) {
  314. dev_warn(vuart->dev, "could not read hw strap table\n");
  315. return;
  316. }
  317. aspeed_vuart_set_sirq_polarity(vuart, (value & reg_mask) == 0);
  318. }
  319. static int aspeed_vuart_map_irq_polarity(u32 dt)
  320. {
  321. switch (dt) {
  322. case IRQ_TYPE_LEVEL_LOW:
  323. return 0;
  324. case IRQ_TYPE_LEVEL_HIGH:
  325. return 1;
  326. default:
  327. return -EINVAL;
  328. }
  329. }
  330. static int aspeed_vuart_probe(struct platform_device *pdev)
  331. {
  332. struct of_phandle_args sirq_polarity_sense_args;
  333. struct device *dev = &pdev->dev;
  334. struct uart_8250_port port;
  335. struct aspeed_vuart *vuart;
  336. struct device_node *np;
  337. struct resource *res;
  338. int rc, sirq_polarity;
  339. u32 prop, sirq[2];
  340. struct clk *vclk;
  341. np = pdev->dev.of_node;
  342. vuart = devm_kzalloc(&pdev->dev, sizeof(*vuart), GFP_KERNEL);
  343. if (!vuart)
  344. return -ENOMEM;
  345. vuart->dev = &pdev->dev;
  346. timer_setup(&vuart->unthrottle_timer, aspeed_vuart_unthrottle_exp, 0);
  347. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  348. if (!res)
  349. return -EINVAL;
  350. memset(&port, 0, sizeof(port));
  351. port.port.private_data = vuart;
  352. port.port.mapbase = res->start;
  353. port.port.mapsize = resource_size(res);
  354. port.port.startup = aspeed_vuart_startup;
  355. port.port.shutdown = aspeed_vuart_shutdown;
  356. port.port.throttle = aspeed_vuart_throttle;
  357. port.port.unthrottle = aspeed_vuart_unthrottle;
  358. port.port.status = UPSTAT_SYNC_FIFO;
  359. port.port.dev = &pdev->dev;
  360. port.port.has_sysrq = IS_ENABLED(CONFIG_SERIAL_8250_CONSOLE);
  361. port.port.flags = UPF_BOOT_AUTOCONF | UPF_IOREMAP | UPF_FIXED_PORT | UPF_FIXED_TYPE |
  362. UPF_NO_THRE_TEST;
  363. port.bugs |= UART_BUG_TXRACE;
  364. rc = sysfs_create_group(&vuart->dev->kobj, &aspeed_vuart_attr_group);
  365. if (rc < 0)
  366. return rc;
  367. rc = uart_read_port_properties(&port.port);
  368. if (rc)
  369. goto err_sysfs_remove;
  370. /* Get clk rate through clk driver if present */
  371. if (!port.port.uartclk) {
  372. vclk = devm_clk_get_enabled(dev, NULL);
  373. if (IS_ERR(vclk)) {
  374. rc = dev_err_probe(dev, PTR_ERR(vclk), "clk or clock-frequency not defined\n");
  375. goto err_sysfs_remove;
  376. }
  377. port.port.uartclk = clk_get_rate(vclk);
  378. }
  379. /* If current-speed was set, then try not to change it. */
  380. if (of_property_read_u32(np, "current-speed", &prop) == 0)
  381. port.port.custom_divisor = port.port.uartclk / (16 * prop);
  382. port.port.handle_irq = aspeed_vuart_handle_irq;
  383. port.port.type = PORT_ASPEED_VUART;
  384. if (port.port.fifosize)
  385. port.capabilities = UART_CAP_FIFO;
  386. if (of_property_read_bool(np, "auto-flow-control"))
  387. port.capabilities |= UART_CAP_AFE;
  388. rc = serial8250_register_8250_port(&port);
  389. if (rc < 0)
  390. goto err_sysfs_remove;
  391. vuart->line = rc;
  392. vuart->port = serial8250_get_port(vuart->line);
  393. rc = of_parse_phandle_with_fixed_args(
  394. np, "aspeed,sirq-polarity-sense", 2, 0,
  395. &sirq_polarity_sense_args);
  396. if (rc < 0) {
  397. dev_dbg(&pdev->dev,
  398. "aspeed,sirq-polarity-sense property not found\n");
  399. } else {
  400. aspeed_vuart_auto_configure_sirq_polarity(
  401. vuart, sirq_polarity_sense_args.np,
  402. sirq_polarity_sense_args.args[0],
  403. BIT(sirq_polarity_sense_args.args[1]));
  404. of_node_put(sirq_polarity_sense_args.np);
  405. }
  406. rc = of_property_read_u32(np, "aspeed,lpc-io-reg", &prop);
  407. if (rc < 0)
  408. prop = ASPEED_VUART_DEFAULT_LPC_ADDR;
  409. rc = aspeed_vuart_set_lpc_address(vuart, prop);
  410. if (rc < 0) {
  411. dev_err_probe(dev, rc, "invalid value in aspeed,lpc-io-reg property\n");
  412. goto err_sysfs_remove;
  413. }
  414. rc = of_property_read_u32_array(np, "aspeed,lpc-interrupts", sirq, 2);
  415. if (rc < 0) {
  416. sirq[0] = ASPEED_VUART_DEFAULT_SIRQ;
  417. sirq[1] = ASPEED_VUART_DEFAULT_SIRQ_POLARITY;
  418. }
  419. rc = aspeed_vuart_set_sirq(vuart, sirq[0]);
  420. if (rc < 0) {
  421. dev_err_probe(dev, rc, "invalid sirq number in aspeed,lpc-interrupts property\n");
  422. goto err_sysfs_remove;
  423. }
  424. sirq_polarity = aspeed_vuart_map_irq_polarity(sirq[1]);
  425. if (sirq_polarity < 0) {
  426. rc = dev_err_probe(dev, sirq_polarity,
  427. "invalid sirq polarity in aspeed,lpc-interrupts property\n");
  428. goto err_sysfs_remove;
  429. }
  430. aspeed_vuart_set_sirq_polarity(vuart, sirq_polarity);
  431. aspeed_vuart_set_enabled(vuart, true);
  432. aspeed_vuart_set_host_tx_discard(vuart, true);
  433. platform_set_drvdata(pdev, vuart);
  434. return 0;
  435. err_sysfs_remove:
  436. sysfs_remove_group(&vuart->dev->kobj, &aspeed_vuart_attr_group);
  437. return rc;
  438. }
  439. static void aspeed_vuart_remove(struct platform_device *pdev)
  440. {
  441. struct aspeed_vuart *vuart = platform_get_drvdata(pdev);
  442. timer_delete_sync(&vuart->unthrottle_timer);
  443. aspeed_vuart_set_enabled(vuart, false);
  444. serial8250_unregister_port(vuart->line);
  445. sysfs_remove_group(&vuart->dev->kobj, &aspeed_vuart_attr_group);
  446. }
  447. static const struct of_device_id aspeed_vuart_table[] = {
  448. { .compatible = "aspeed,ast2400-vuart" },
  449. { .compatible = "aspeed,ast2500-vuart" },
  450. { },
  451. };
  452. MODULE_DEVICE_TABLE(of, aspeed_vuart_table);
  453. static struct platform_driver aspeed_vuart_driver = {
  454. .driver = {
  455. .name = "aspeed-vuart",
  456. .of_match_table = aspeed_vuart_table,
  457. },
  458. .probe = aspeed_vuart_probe,
  459. .remove = aspeed_vuart_remove,
  460. };
  461. module_platform_driver(aspeed_vuart_driver);
  462. MODULE_AUTHOR("Jeremy Kerr <jk@ozlabs.org>");
  463. MODULE_LICENSE("GPL");
  464. MODULE_DESCRIPTION("Driver for Aspeed VUART device");