8250_dw.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621
  1. /*
  2. * Synopsys DesignWare 8250 driver.
  3. *
  4. * Copyright 2011 Picochip, Jamie Iles.
  5. * Copyright 2013 Intel Corporation
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * The Synopsys DesignWare 8250 has an extra feature whereby it detects if the
  13. * LCR is written whilst busy. If it is, then a busy detect interrupt is
  14. * raised, the LCR needs to be rewritten and the uart status register read.
  15. */
  16. #include <linux/device.h>
  17. #include <linux/io.h>
  18. #include <linux/module.h>
  19. #include <linux/serial_8250.h>
  20. #include <linux/serial_reg.h>
  21. #include <linux/of.h>
  22. #include <linux/of_irq.h>
  23. #include <linux/of_platform.h>
  24. #include <linux/platform_device.h>
  25. #include <linux/slab.h>
  26. #include <linux/acpi.h>
  27. #include <linux/clk.h>
  28. #include <linux/reset.h>
  29. #include <linux/pm_runtime.h>
  30. #include <asm/byteorder.h>
  31. #include "8250.h"
  32. /* Offsets for the DesignWare specific registers */
  33. #define DW_UART_USR 0x1f /* UART Status Register */
  34. #define DW_UART_CPR 0xf4 /* Component Parameter Register */
  35. #define DW_UART_UCV 0xf8 /* UART Component Version */
  36. /* Component Parameter Register bits */
  37. #define DW_UART_CPR_ABP_DATA_WIDTH (3 << 0)
  38. #define DW_UART_CPR_AFCE_MODE (1 << 4)
  39. #define DW_UART_CPR_THRE_MODE (1 << 5)
  40. #define DW_UART_CPR_SIR_MODE (1 << 6)
  41. #define DW_UART_CPR_SIR_LP_MODE (1 << 7)
  42. #define DW_UART_CPR_ADDITIONAL_FEATURES (1 << 8)
  43. #define DW_UART_CPR_FIFO_ACCESS (1 << 9)
  44. #define DW_UART_CPR_FIFO_STAT (1 << 10)
  45. #define DW_UART_CPR_SHADOW (1 << 11)
  46. #define DW_UART_CPR_ENCODED_PARMS (1 << 12)
  47. #define DW_UART_CPR_DMA_EXTRA (1 << 13)
  48. #define DW_UART_CPR_FIFO_MODE (0xff << 16)
  49. /* Helper for fifo size calculation */
  50. #define DW_UART_CPR_FIFO_SIZE(a) (((a >> 16) & 0xff) * 16)
  51. struct dw8250_data {
  52. u8 usr_reg;
  53. int line;
  54. int msr_mask_on;
  55. int msr_mask_off;
  56. struct clk *clk;
  57. struct clk *pclk;
  58. struct reset_control *rst;
  59. struct uart_8250_dma dma;
  60. unsigned int skip_autocfg:1;
  61. unsigned int uart_16550_compatible:1;
  62. };
  63. #define BYT_PRV_CLK 0x800
  64. #define BYT_PRV_CLK_EN (1 << 0)
  65. #define BYT_PRV_CLK_M_VAL_SHIFT 1
  66. #define BYT_PRV_CLK_N_VAL_SHIFT 16
  67. #define BYT_PRV_CLK_UPDATE (1 << 31)
  68. static inline int dw8250_modify_msr(struct uart_port *p, int offset, int value)
  69. {
  70. struct dw8250_data *d = p->private_data;
  71. /* Override any modem control signals if needed */
  72. if (offset == UART_MSR) {
  73. value |= d->msr_mask_on;
  74. value &= ~d->msr_mask_off;
  75. }
  76. return value;
  77. }
  78. static void dw8250_force_idle(struct uart_port *p)
  79. {
  80. struct uart_8250_port *up = up_to_u8250p(p);
  81. serial8250_clear_and_reinit_fifos(up);
  82. (void)p->serial_in(p, UART_RX);
  83. }
  84. static void dw8250_serial_out(struct uart_port *p, int offset, int value)
  85. {
  86. writeb(value, p->membase + (offset << p->regshift));
  87. /* Make sure LCR write wasn't ignored */
  88. if (offset == UART_LCR) {
  89. int tries = 1000;
  90. while (tries--) {
  91. unsigned int lcr = p->serial_in(p, UART_LCR);
  92. if ((value & ~UART_LCR_SPAR) == (lcr & ~UART_LCR_SPAR))
  93. return;
  94. dw8250_force_idle(p);
  95. writeb(value, p->membase + (UART_LCR << p->regshift));
  96. }
  97. /*
  98. * FIXME: this deadlocks if port->lock is already held
  99. * dev_err(p->dev, "Couldn't set LCR to %d\n", value);
  100. */
  101. }
  102. }
  103. static unsigned int dw8250_serial_in(struct uart_port *p, int offset)
  104. {
  105. unsigned int value = readb(p->membase + (offset << p->regshift));
  106. return dw8250_modify_msr(p, offset, value);
  107. }
  108. #ifdef CONFIG_64BIT
  109. static unsigned int dw8250_serial_inq(struct uart_port *p, int offset)
  110. {
  111. unsigned int value;
  112. value = (u8)__raw_readq(p->membase + (offset << p->regshift));
  113. return dw8250_modify_msr(p, offset, value);
  114. }
  115. static void dw8250_serial_outq(struct uart_port *p, int offset, int value)
  116. {
  117. value &= 0xff;
  118. __raw_writeq(value, p->membase + (offset << p->regshift));
  119. /* Read back to ensure register write ordering. */
  120. __raw_readq(p->membase + (UART_LCR << p->regshift));
  121. /* Make sure LCR write wasn't ignored */
  122. if (offset == UART_LCR) {
  123. int tries = 1000;
  124. while (tries--) {
  125. unsigned int lcr = p->serial_in(p, UART_LCR);
  126. if ((value & ~UART_LCR_SPAR) == (lcr & ~UART_LCR_SPAR))
  127. return;
  128. dw8250_force_idle(p);
  129. __raw_writeq(value & 0xff,
  130. p->membase + (UART_LCR << p->regshift));
  131. }
  132. /*
  133. * FIXME: this deadlocks if port->lock is already held
  134. * dev_err(p->dev, "Couldn't set LCR to %d\n", value);
  135. */
  136. }
  137. }
  138. #endif /* CONFIG_64BIT */
  139. static void dw8250_serial_out32(struct uart_port *p, int offset, int value)
  140. {
  141. writel(value, p->membase + (offset << p->regshift));
  142. /* Make sure LCR write wasn't ignored */
  143. if (offset == UART_LCR) {
  144. int tries = 1000;
  145. while (tries--) {
  146. unsigned int lcr = p->serial_in(p, UART_LCR);
  147. if ((value & ~UART_LCR_SPAR) == (lcr & ~UART_LCR_SPAR))
  148. return;
  149. dw8250_force_idle(p);
  150. writel(value, p->membase + (UART_LCR << p->regshift));
  151. }
  152. /*
  153. * FIXME: this deadlocks if port->lock is already held
  154. * dev_err(p->dev, "Couldn't set LCR to %d\n", value);
  155. */
  156. }
  157. }
  158. static unsigned int dw8250_serial_in32(struct uart_port *p, int offset)
  159. {
  160. unsigned int value = readl(p->membase + (offset << p->regshift));
  161. return dw8250_modify_msr(p, offset, value);
  162. }
  163. static int dw8250_handle_irq(struct uart_port *p)
  164. {
  165. struct dw8250_data *d = p->private_data;
  166. unsigned int iir = p->serial_in(p, UART_IIR);
  167. if (serial8250_handle_irq(p, iir)) {
  168. return 1;
  169. } else if ((iir & UART_IIR_BUSY) == UART_IIR_BUSY) {
  170. /* Clear the USR */
  171. (void)p->serial_in(p, d->usr_reg);
  172. return 1;
  173. }
  174. return 0;
  175. }
  176. static void
  177. dw8250_do_pm(struct uart_port *port, unsigned int state, unsigned int old)
  178. {
  179. if (!state)
  180. pm_runtime_get_sync(port->dev);
  181. serial8250_do_pm(port, state, old);
  182. if (state)
  183. pm_runtime_put_sync_suspend(port->dev);
  184. }
  185. static void dw8250_set_termios(struct uart_port *p, struct ktermios *termios,
  186. struct ktermios *old)
  187. {
  188. unsigned int baud = tty_termios_baud_rate(termios);
  189. struct dw8250_data *d = p->private_data;
  190. unsigned int rate;
  191. int ret;
  192. if (IS_ERR(d->clk))
  193. goto out;
  194. clk_disable_unprepare(d->clk);
  195. rate = clk_round_rate(d->clk, baud * 16);
  196. ret = clk_set_rate(d->clk, rate);
  197. clk_prepare_enable(d->clk);
  198. if (!ret)
  199. p->uartclk = rate;
  200. p->status &= ~UPSTAT_AUTOCTS;
  201. if (termios->c_cflag & CRTSCTS)
  202. p->status |= UPSTAT_AUTOCTS;
  203. out:
  204. serial8250_do_set_termios(p, termios, old);
  205. }
  206. /*
  207. * dw8250_fallback_dma_filter will prevent the UART from getting just any free
  208. * channel on platforms that have DMA engines, but don't have any channels
  209. * assigned to the UART.
  210. *
  211. * REVISIT: This is a work around for limitation in the DMA Engine API. Once the
  212. * core problem is fixed, this function is no longer needed.
  213. */
  214. static bool dw8250_fallback_dma_filter(struct dma_chan *chan, void *param)
  215. {
  216. return false;
  217. }
  218. static bool dw8250_idma_filter(struct dma_chan *chan, void *param)
  219. {
  220. return param == chan->device->dev->parent;
  221. }
  222. static void dw8250_quirks(struct uart_port *p, struct dw8250_data *data)
  223. {
  224. if (p->dev->of_node) {
  225. struct device_node *np = p->dev->of_node;
  226. int id;
  227. /* get index of serial line, if found in DT aliases */
  228. id = of_alias_get_id(np, "serial");
  229. if (id >= 0)
  230. p->line = id;
  231. #ifdef CONFIG_64BIT
  232. if (of_device_is_compatible(np, "cavium,octeon-3860-uart")) {
  233. p->serial_in = dw8250_serial_inq;
  234. p->serial_out = dw8250_serial_outq;
  235. p->flags = UPF_SKIP_TEST | UPF_SHARE_IRQ | UPF_FIXED_TYPE;
  236. p->type = PORT_OCTEON;
  237. data->usr_reg = 0x27;
  238. data->skip_autocfg = true;
  239. }
  240. #endif
  241. } else if (has_acpi_companion(p->dev)) {
  242. p->iotype = UPIO_MEM32;
  243. p->regshift = 2;
  244. p->serial_in = dw8250_serial_in32;
  245. p->set_termios = dw8250_set_termios;
  246. /* So far none of there implement the Busy Functionality */
  247. data->uart_16550_compatible = true;
  248. }
  249. /* Platforms with iDMA */
  250. if (platform_get_resource_byname(to_platform_device(p->dev),
  251. IORESOURCE_MEM, "lpss_priv")) {
  252. p->set_termios = dw8250_set_termios;
  253. data->dma.rx_param = p->dev->parent;
  254. data->dma.tx_param = p->dev->parent;
  255. data->dma.fn = dw8250_idma_filter;
  256. }
  257. }
  258. static void dw8250_setup_port(struct uart_port *p)
  259. {
  260. struct uart_8250_port *up = up_to_u8250p(p);
  261. u32 reg;
  262. /*
  263. * If the Component Version Register returns zero, we know that
  264. * ADDITIONAL_FEATURES are not enabled. No need to go any further.
  265. */
  266. reg = readl(p->membase + DW_UART_UCV);
  267. if (!reg)
  268. return;
  269. dev_dbg(p->dev, "Designware UART version %c.%c%c\n",
  270. (reg >> 24) & 0xff, (reg >> 16) & 0xff, (reg >> 8) & 0xff);
  271. reg = readl(p->membase + DW_UART_CPR);
  272. if (!reg)
  273. return;
  274. /* Select the type based on fifo */
  275. if (reg & DW_UART_CPR_FIFO_MODE) {
  276. p->type = PORT_16550A;
  277. p->flags |= UPF_FIXED_TYPE;
  278. p->fifosize = DW_UART_CPR_FIFO_SIZE(reg);
  279. up->capabilities = UART_CAP_FIFO;
  280. }
  281. if (reg & DW_UART_CPR_AFCE_MODE)
  282. up->capabilities |= UART_CAP_AFE;
  283. }
  284. static int dw8250_probe(struct platform_device *pdev)
  285. {
  286. struct uart_8250_port uart = {};
  287. struct resource *regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  288. int irq = platform_get_irq(pdev, 0);
  289. struct uart_port *p = &uart.port;
  290. struct dw8250_data *data;
  291. int err;
  292. u32 val;
  293. if (!regs) {
  294. dev_err(&pdev->dev, "no registers defined\n");
  295. return -EINVAL;
  296. }
  297. if (irq < 0) {
  298. if (irq != -EPROBE_DEFER)
  299. dev_err(&pdev->dev, "cannot get irq\n");
  300. return irq;
  301. }
  302. spin_lock_init(&p->lock);
  303. p->mapbase = regs->start;
  304. p->irq = irq;
  305. p->handle_irq = dw8250_handle_irq;
  306. p->pm = dw8250_do_pm;
  307. p->type = PORT_8250;
  308. p->flags = UPF_SHARE_IRQ | UPF_FIXED_PORT;
  309. p->dev = &pdev->dev;
  310. p->iotype = UPIO_MEM;
  311. p->serial_in = dw8250_serial_in;
  312. p->serial_out = dw8250_serial_out;
  313. p->membase = devm_ioremap(&pdev->dev, regs->start, resource_size(regs));
  314. if (!p->membase)
  315. return -ENOMEM;
  316. data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
  317. if (!data)
  318. return -ENOMEM;
  319. data->dma.fn = dw8250_fallback_dma_filter;
  320. data->usr_reg = DW_UART_USR;
  321. p->private_data = data;
  322. data->uart_16550_compatible = device_property_read_bool(p->dev,
  323. "snps,uart-16550-compatible");
  324. err = device_property_read_u32(p->dev, "reg-shift", &val);
  325. if (!err)
  326. p->regshift = val;
  327. err = device_property_read_u32(p->dev, "reg-io-width", &val);
  328. if (!err && val == 4) {
  329. p->iotype = UPIO_MEM32;
  330. p->serial_in = dw8250_serial_in32;
  331. p->serial_out = dw8250_serial_out32;
  332. }
  333. if (device_property_read_bool(p->dev, "dcd-override")) {
  334. /* Always report DCD as active */
  335. data->msr_mask_on |= UART_MSR_DCD;
  336. data->msr_mask_off |= UART_MSR_DDCD;
  337. }
  338. if (device_property_read_bool(p->dev, "dsr-override")) {
  339. /* Always report DSR as active */
  340. data->msr_mask_on |= UART_MSR_DSR;
  341. data->msr_mask_off |= UART_MSR_DDSR;
  342. }
  343. if (device_property_read_bool(p->dev, "cts-override")) {
  344. /* Always report CTS as active */
  345. data->msr_mask_on |= UART_MSR_CTS;
  346. data->msr_mask_off |= UART_MSR_DCTS;
  347. }
  348. if (device_property_read_bool(p->dev, "ri-override")) {
  349. /* Always report Ring indicator as inactive */
  350. data->msr_mask_off |= UART_MSR_RI;
  351. data->msr_mask_off |= UART_MSR_TERI;
  352. }
  353. /* Always ask for fixed clock rate from a property. */
  354. device_property_read_u32(p->dev, "clock-frequency", &p->uartclk);
  355. /* If there is separate baudclk, get the rate from it. */
  356. data->clk = devm_clk_get(&pdev->dev, "baudclk");
  357. if (IS_ERR(data->clk) && PTR_ERR(data->clk) != -EPROBE_DEFER)
  358. data->clk = devm_clk_get(&pdev->dev, NULL);
  359. if (IS_ERR(data->clk) && PTR_ERR(data->clk) == -EPROBE_DEFER)
  360. return -EPROBE_DEFER;
  361. if (!IS_ERR_OR_NULL(data->clk)) {
  362. err = clk_prepare_enable(data->clk);
  363. if (err)
  364. dev_warn(&pdev->dev, "could not enable optional baudclk: %d\n",
  365. err);
  366. else
  367. p->uartclk = clk_get_rate(data->clk);
  368. }
  369. /* If no clock rate is defined, fail. */
  370. if (!p->uartclk) {
  371. dev_err(&pdev->dev, "clock rate not defined\n");
  372. return -EINVAL;
  373. }
  374. data->pclk = devm_clk_get(&pdev->dev, "apb_pclk");
  375. if (IS_ERR(data->pclk) && PTR_ERR(data->pclk) == -EPROBE_DEFER) {
  376. err = -EPROBE_DEFER;
  377. goto err_clk;
  378. }
  379. if (!IS_ERR(data->pclk)) {
  380. err = clk_prepare_enable(data->pclk);
  381. if (err) {
  382. dev_err(&pdev->dev, "could not enable apb_pclk\n");
  383. goto err_clk;
  384. }
  385. }
  386. data->rst = devm_reset_control_get_optional(&pdev->dev, NULL);
  387. if (IS_ERR(data->rst) && PTR_ERR(data->rst) == -EPROBE_DEFER) {
  388. err = -EPROBE_DEFER;
  389. goto err_pclk;
  390. }
  391. if (!IS_ERR(data->rst))
  392. reset_control_deassert(data->rst);
  393. dw8250_quirks(p, data);
  394. /* If the Busy Functionality is not implemented, don't handle it */
  395. if (data->uart_16550_compatible) {
  396. p->serial_out = NULL;
  397. p->handle_irq = NULL;
  398. }
  399. if (!data->skip_autocfg)
  400. dw8250_setup_port(p);
  401. /* If we have a valid fifosize, try hooking up DMA */
  402. if (p->fifosize) {
  403. data->dma.rxconf.src_maxburst = p->fifosize / 4;
  404. data->dma.txconf.dst_maxburst = p->fifosize / 4;
  405. uart.dma = &data->dma;
  406. }
  407. data->line = serial8250_register_8250_port(&uart);
  408. if (data->line < 0) {
  409. err = data->line;
  410. goto err_reset;
  411. }
  412. platform_set_drvdata(pdev, data);
  413. pm_runtime_set_active(&pdev->dev);
  414. pm_runtime_enable(&pdev->dev);
  415. return 0;
  416. err_reset:
  417. if (!IS_ERR(data->rst))
  418. reset_control_assert(data->rst);
  419. err_pclk:
  420. if (!IS_ERR(data->pclk))
  421. clk_disable_unprepare(data->pclk);
  422. err_clk:
  423. if (!IS_ERR(data->clk))
  424. clk_disable_unprepare(data->clk);
  425. return err;
  426. }
  427. static int dw8250_remove(struct platform_device *pdev)
  428. {
  429. struct dw8250_data *data = platform_get_drvdata(pdev);
  430. pm_runtime_get_sync(&pdev->dev);
  431. serial8250_unregister_port(data->line);
  432. if (!IS_ERR(data->rst))
  433. reset_control_assert(data->rst);
  434. if (!IS_ERR(data->pclk))
  435. clk_disable_unprepare(data->pclk);
  436. if (!IS_ERR(data->clk))
  437. clk_disable_unprepare(data->clk);
  438. pm_runtime_disable(&pdev->dev);
  439. pm_runtime_put_noidle(&pdev->dev);
  440. return 0;
  441. }
  442. #ifdef CONFIG_PM_SLEEP
  443. static int dw8250_suspend(struct device *dev)
  444. {
  445. struct dw8250_data *data = dev_get_drvdata(dev);
  446. serial8250_suspend_port(data->line);
  447. return 0;
  448. }
  449. static int dw8250_resume(struct device *dev)
  450. {
  451. struct dw8250_data *data = dev_get_drvdata(dev);
  452. serial8250_resume_port(data->line);
  453. return 0;
  454. }
  455. #endif /* CONFIG_PM_SLEEP */
  456. #ifdef CONFIG_PM
  457. static int dw8250_runtime_suspend(struct device *dev)
  458. {
  459. struct dw8250_data *data = dev_get_drvdata(dev);
  460. if (!IS_ERR(data->clk))
  461. clk_disable_unprepare(data->clk);
  462. if (!IS_ERR(data->pclk))
  463. clk_disable_unprepare(data->pclk);
  464. return 0;
  465. }
  466. static int dw8250_runtime_resume(struct device *dev)
  467. {
  468. struct dw8250_data *data = dev_get_drvdata(dev);
  469. if (!IS_ERR(data->pclk))
  470. clk_prepare_enable(data->pclk);
  471. if (!IS_ERR(data->clk))
  472. clk_prepare_enable(data->clk);
  473. return 0;
  474. }
  475. #endif
  476. static const struct dev_pm_ops dw8250_pm_ops = {
  477. SET_SYSTEM_SLEEP_PM_OPS(dw8250_suspend, dw8250_resume)
  478. SET_RUNTIME_PM_OPS(dw8250_runtime_suspend, dw8250_runtime_resume, NULL)
  479. };
  480. static const struct of_device_id dw8250_of_match[] = {
  481. { .compatible = "snps,dw-apb-uart" },
  482. { .compatible = "cavium,octeon-3860-uart" },
  483. { /* Sentinel */ }
  484. };
  485. MODULE_DEVICE_TABLE(of, dw8250_of_match);
  486. static const struct acpi_device_id dw8250_acpi_match[] = {
  487. { "INT33C4", 0 },
  488. { "INT33C5", 0 },
  489. { "INT3434", 0 },
  490. { "INT3435", 0 },
  491. { "80860F0A", 0 },
  492. { "8086228A", 0 },
  493. { "APMC0D08", 0},
  494. { "AMD0020", 0 },
  495. { },
  496. };
  497. MODULE_DEVICE_TABLE(acpi, dw8250_acpi_match);
  498. static struct platform_driver dw8250_platform_driver = {
  499. .driver = {
  500. .name = "dw-apb-uart",
  501. .pm = &dw8250_pm_ops,
  502. .of_match_table = dw8250_of_match,
  503. .acpi_match_table = ACPI_PTR(dw8250_acpi_match),
  504. },
  505. .probe = dw8250_probe,
  506. .remove = dw8250_remove,
  507. };
  508. module_platform_driver(dw8250_platform_driver);
  509. MODULE_AUTHOR("Jamie Iles");
  510. MODULE_LICENSE("GPL");
  511. MODULE_DESCRIPTION("Synopsys DesignWare 8250 serial port driver");
  512. MODULE_ALIAS("platform:dw-apb-uart");