8250_aspeed_vuart.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579
  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 = from_timer(vuart, timer, unthrottle_timer);
  244. struct uart_8250_port *up = vuart->port;
  245. if (!tty_buffer_space_avail(&up->port.state->port)) {
  246. mod_timer(&vuart->unthrottle_timer,
  247. jiffies + unthrottle_timeout);
  248. return;
  249. }
  250. aspeed_vuart_unthrottle(&up->port);
  251. }
  252. /*
  253. * Custom interrupt handler to manage finer-grained flow control. Although we
  254. * have throttle/unthrottle callbacks, we've seen that the VUART device can
  255. * deliver characters faster than the ldisc has a chance to check buffer space
  256. * against the throttle threshold. This results in dropped characters before
  257. * the throttle.
  258. *
  259. * We do this by checking for flip buffer space before RX. If we have no space,
  260. * throttle now and schedule an unthrottle for later, once the ldisc has had
  261. * a chance to drain the buffers.
  262. */
  263. static int aspeed_vuart_handle_irq(struct uart_port *port)
  264. {
  265. struct uart_8250_port *up = up_to_u8250p(port);
  266. unsigned int iir, lsr;
  267. unsigned long flags;
  268. unsigned int space, count;
  269. iir = serial_port_in(port, UART_IIR);
  270. if (iir & UART_IIR_NO_INT)
  271. return 0;
  272. uart_port_lock_irqsave(port, &flags);
  273. lsr = serial_port_in(port, UART_LSR);
  274. if (lsr & (UART_LSR_DR | UART_LSR_BI)) {
  275. space = tty_buffer_space_avail(&port->state->port);
  276. if (!space) {
  277. /* throttle and schedule an unthrottle later */
  278. struct aspeed_vuart *vuart = port->private_data;
  279. __aspeed_vuart_set_throttle(up, true);
  280. if (!timer_pending(&vuart->unthrottle_timer))
  281. mod_timer(&vuart->unthrottle_timer,
  282. jiffies + unthrottle_timeout);
  283. } else {
  284. count = min(space, 256U);
  285. do {
  286. serial8250_read_char(up, lsr);
  287. lsr = serial_in(up, UART_LSR);
  288. if (--count == 0)
  289. break;
  290. } while (lsr & (UART_LSR_DR | UART_LSR_BI));
  291. tty_flip_buffer_push(&port->state->port);
  292. }
  293. }
  294. serial8250_modem_status(up);
  295. if (lsr & UART_LSR_THRE)
  296. serial8250_tx_chars(up);
  297. uart_unlock_and_check_sysrq_irqrestore(port, flags);
  298. return 1;
  299. }
  300. static void aspeed_vuart_auto_configure_sirq_polarity(
  301. struct aspeed_vuart *vuart, struct device_node *syscon_np,
  302. u32 reg_offset, u32 reg_mask)
  303. {
  304. struct regmap *regmap;
  305. u32 value;
  306. regmap = syscon_node_to_regmap(syscon_np);
  307. if (IS_ERR(regmap)) {
  308. dev_warn(vuart->dev,
  309. "could not get regmap for aspeed,sirq-polarity-sense\n");
  310. return;
  311. }
  312. if (regmap_read(regmap, reg_offset, &value)) {
  313. dev_warn(vuart->dev, "could not read hw strap table\n");
  314. return;
  315. }
  316. aspeed_vuart_set_sirq_polarity(vuart, (value & reg_mask) == 0);
  317. }
  318. static int aspeed_vuart_map_irq_polarity(u32 dt)
  319. {
  320. switch (dt) {
  321. case IRQ_TYPE_LEVEL_LOW:
  322. return 0;
  323. case IRQ_TYPE_LEVEL_HIGH:
  324. return 1;
  325. default:
  326. return -EINVAL;
  327. }
  328. }
  329. static int aspeed_vuart_probe(struct platform_device *pdev)
  330. {
  331. struct of_phandle_args sirq_polarity_sense_args;
  332. struct device *dev = &pdev->dev;
  333. struct uart_8250_port port;
  334. struct aspeed_vuart *vuart;
  335. struct device_node *np;
  336. struct resource *res;
  337. int rc, sirq_polarity;
  338. u32 prop, sirq[2];
  339. struct clk *vclk;
  340. np = pdev->dev.of_node;
  341. vuart = devm_kzalloc(&pdev->dev, sizeof(*vuart), GFP_KERNEL);
  342. if (!vuart)
  343. return -ENOMEM;
  344. vuart->dev = &pdev->dev;
  345. timer_setup(&vuart->unthrottle_timer, aspeed_vuart_unthrottle_exp, 0);
  346. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  347. if (!res)
  348. return -EINVAL;
  349. memset(&port, 0, sizeof(port));
  350. port.port.private_data = vuart;
  351. port.port.mapbase = res->start;
  352. port.port.mapsize = resource_size(res);
  353. port.port.startup = aspeed_vuart_startup;
  354. port.port.shutdown = aspeed_vuart_shutdown;
  355. port.port.throttle = aspeed_vuart_throttle;
  356. port.port.unthrottle = aspeed_vuart_unthrottle;
  357. port.port.status = UPSTAT_SYNC_FIFO;
  358. port.port.dev = &pdev->dev;
  359. port.port.has_sysrq = IS_ENABLED(CONFIG_SERIAL_8250_CONSOLE);
  360. port.port.flags = UPF_BOOT_AUTOCONF | UPF_IOREMAP | UPF_FIXED_PORT | UPF_FIXED_TYPE |
  361. UPF_NO_THRE_TEST;
  362. port.bugs |= UART_BUG_TXRACE;
  363. rc = sysfs_create_group(&vuart->dev->kobj, &aspeed_vuart_attr_group);
  364. if (rc < 0)
  365. return rc;
  366. rc = uart_read_port_properties(&port.port);
  367. if (rc)
  368. goto err_sysfs_remove;
  369. /* Get clk rate through clk driver if present */
  370. if (!port.port.uartclk) {
  371. vclk = devm_clk_get_enabled(dev, NULL);
  372. if (IS_ERR(vclk)) {
  373. rc = dev_err_probe(dev, PTR_ERR(vclk), "clk or clock-frequency not defined\n");
  374. goto err_sysfs_remove;
  375. }
  376. port.port.uartclk = clk_get_rate(vclk);
  377. }
  378. /* If current-speed was set, then try not to change it. */
  379. if (of_property_read_u32(np, "current-speed", &prop) == 0)
  380. port.port.custom_divisor = port.port.uartclk / (16 * prop);
  381. port.port.handle_irq = aspeed_vuart_handle_irq;
  382. port.port.type = PORT_ASPEED_VUART;
  383. if (port.port.fifosize)
  384. port.capabilities = UART_CAP_FIFO;
  385. if (of_property_read_bool(np, "auto-flow-control"))
  386. port.capabilities |= UART_CAP_AFE;
  387. rc = serial8250_register_8250_port(&port);
  388. if (rc < 0)
  389. goto err_sysfs_remove;
  390. vuart->line = rc;
  391. vuart->port = serial8250_get_port(vuart->line);
  392. rc = of_parse_phandle_with_fixed_args(
  393. np, "aspeed,sirq-polarity-sense", 2, 0,
  394. &sirq_polarity_sense_args);
  395. if (rc < 0) {
  396. dev_dbg(&pdev->dev,
  397. "aspeed,sirq-polarity-sense property not found\n");
  398. } else {
  399. aspeed_vuart_auto_configure_sirq_polarity(
  400. vuart, sirq_polarity_sense_args.np,
  401. sirq_polarity_sense_args.args[0],
  402. BIT(sirq_polarity_sense_args.args[1]));
  403. of_node_put(sirq_polarity_sense_args.np);
  404. }
  405. rc = of_property_read_u32(np, "aspeed,lpc-io-reg", &prop);
  406. if (rc < 0)
  407. prop = ASPEED_VUART_DEFAULT_LPC_ADDR;
  408. rc = aspeed_vuart_set_lpc_address(vuart, prop);
  409. if (rc < 0) {
  410. dev_err_probe(dev, rc, "invalid value in aspeed,lpc-io-reg property\n");
  411. goto err_sysfs_remove;
  412. }
  413. rc = of_property_read_u32_array(np, "aspeed,lpc-interrupts", sirq, 2);
  414. if (rc < 0) {
  415. sirq[0] = ASPEED_VUART_DEFAULT_SIRQ;
  416. sirq[1] = ASPEED_VUART_DEFAULT_SIRQ_POLARITY;
  417. }
  418. rc = aspeed_vuart_set_sirq(vuart, sirq[0]);
  419. if (rc < 0) {
  420. dev_err_probe(dev, rc, "invalid sirq number in aspeed,lpc-interrupts property\n");
  421. goto err_sysfs_remove;
  422. }
  423. sirq_polarity = aspeed_vuart_map_irq_polarity(sirq[1]);
  424. if (sirq_polarity < 0) {
  425. rc = dev_err_probe(dev, sirq_polarity,
  426. "invalid sirq polarity in aspeed,lpc-interrupts property\n");
  427. goto err_sysfs_remove;
  428. }
  429. aspeed_vuart_set_sirq_polarity(vuart, sirq_polarity);
  430. aspeed_vuart_set_enabled(vuart, true);
  431. aspeed_vuart_set_host_tx_discard(vuart, true);
  432. platform_set_drvdata(pdev, vuart);
  433. return 0;
  434. err_sysfs_remove:
  435. sysfs_remove_group(&vuart->dev->kobj, &aspeed_vuart_attr_group);
  436. return rc;
  437. }
  438. static void aspeed_vuart_remove(struct platform_device *pdev)
  439. {
  440. struct aspeed_vuart *vuart = platform_get_drvdata(pdev);
  441. del_timer_sync(&vuart->unthrottle_timer);
  442. aspeed_vuart_set_enabled(vuart, false);
  443. serial8250_unregister_port(vuart->line);
  444. sysfs_remove_group(&vuart->dev->kobj, &aspeed_vuart_attr_group);
  445. }
  446. static const struct of_device_id aspeed_vuart_table[] = {
  447. { .compatible = "aspeed,ast2400-vuart" },
  448. { .compatible = "aspeed,ast2500-vuart" },
  449. { },
  450. };
  451. MODULE_DEVICE_TABLE(of, aspeed_vuart_table);
  452. static struct platform_driver aspeed_vuart_driver = {
  453. .driver = {
  454. .name = "aspeed-vuart",
  455. .of_match_table = aspeed_vuart_table,
  456. },
  457. .probe = aspeed_vuart_probe,
  458. .remove_new = aspeed_vuart_remove,
  459. };
  460. module_platform_driver(aspeed_vuart_driver);
  461. MODULE_AUTHOR("Jeremy Kerr <jk@ozlabs.org>");
  462. MODULE_LICENSE("GPL");
  463. MODULE_DESCRIPTION("Driver for Aspeed VUART device");