serial.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855
  1. /*
  2. * arch/arm/mach-omap2/serial.c
  3. *
  4. * OMAP2 serial support.
  5. *
  6. * Copyright (C) 2005-2008 Nokia Corporation
  7. * Author: Paul Mundt <paul.mundt@nokia.com>
  8. *
  9. * Major rework for PM support by Kevin Hilman
  10. *
  11. * Based off of arch/arm/mach-omap/omap1/serial.c
  12. *
  13. * Copyright (C) 2009 Texas Instruments
  14. * Added OMAP4 support - Santosh Shilimkar <santosh.shilimkar@ti.com
  15. *
  16. * This file is subject to the terms and conditions of the GNU General Public
  17. * License. See the file "COPYING" in the main directory of this archive
  18. * for more details.
  19. */
  20. #include <linux/kernel.h>
  21. #include <linux/init.h>
  22. #include <linux/serial_reg.h>
  23. #include <linux/clk.h>
  24. #include <linux/io.h>
  25. #include <linux/delay.h>
  26. #include <linux/platform_device.h>
  27. #include <linux/slab.h>
  28. #include <linux/serial_8250.h>
  29. #include <linux/pm_runtime.h>
  30. #include <linux/console.h>
  31. #ifdef CONFIG_SERIAL_OMAP
  32. #include <plat/omap-serial.h>
  33. #endif
  34. #include "common.h"
  35. #include <plat/board.h>
  36. #include <plat/clock.h>
  37. #include <plat/dma.h>
  38. #include <plat/omap_hwmod.h>
  39. #include <plat/omap_device.h>
  40. #include "prm2xxx_3xxx.h"
  41. #include "pm.h"
  42. #include "cm2xxx_3xxx.h"
  43. #include "prm-regbits-34xx.h"
  44. #include "control.h"
  45. #include "mux.h"
  46. #define UART_OMAP_NO_EMPTY_FIFO_READ_IP_REV 0x52
  47. #define UART_OMAP_WER 0x17 /* Wake-up enable register */
  48. #define UART_ERRATA_FIFO_FULL_ABORT (0x1 << 0)
  49. #define UART_ERRATA_i202_MDR1_ACCESS (0x1 << 1)
  50. /*
  51. * NOTE: By default the serial timeout is disabled as it causes lost characters
  52. * over the serial ports. This means that the UART clocks will stay on until
  53. * disabled via sysfs. This also causes that any deeper omap sleep states are
  54. * blocked.
  55. */
  56. #define DEFAULT_TIMEOUT 0
  57. #define MAX_UART_HWMOD_NAME_LEN 16
  58. struct omap_uart_state {
  59. int num;
  60. int can_sleep;
  61. struct timer_list timer;
  62. u32 timeout;
  63. void __iomem *wk_st;
  64. void __iomem *wk_en;
  65. u32 wk_mask;
  66. u32 padconf;
  67. u32 dma_enabled;
  68. struct clk *ick;
  69. struct clk *fck;
  70. int clocked;
  71. int irq;
  72. int regshift;
  73. int irqflags;
  74. void __iomem *membase;
  75. resource_size_t mapbase;
  76. struct list_head node;
  77. struct omap_hwmod *oh;
  78. struct platform_device *pdev;
  79. u32 errata;
  80. #if defined(CONFIG_ARCH_OMAP3) && defined(CONFIG_PM)
  81. int context_valid;
  82. /* Registers to be saved/restored for OFF-mode */
  83. u16 dll;
  84. u16 dlh;
  85. u16 ier;
  86. u16 sysc;
  87. u16 scr;
  88. u16 wer;
  89. u16 mcr;
  90. #endif
  91. };
  92. static LIST_HEAD(uart_list);
  93. static u8 num_uarts;
  94. static inline unsigned int __serial_read_reg(struct uart_port *up,
  95. int offset)
  96. {
  97. offset <<= up->regshift;
  98. return (unsigned int)__raw_readb(up->membase + offset);
  99. }
  100. static inline unsigned int serial_read_reg(struct omap_uart_state *uart,
  101. int offset)
  102. {
  103. offset <<= uart->regshift;
  104. return (unsigned int)__raw_readb(uart->membase + offset);
  105. }
  106. static inline void __serial_write_reg(struct uart_port *up, int offset,
  107. int value)
  108. {
  109. offset <<= up->regshift;
  110. __raw_writeb(value, up->membase + offset);
  111. }
  112. static inline void serial_write_reg(struct omap_uart_state *uart, int offset,
  113. int value)
  114. {
  115. offset <<= uart->regshift;
  116. __raw_writeb(value, uart->membase + offset);
  117. }
  118. /*
  119. * Internal UARTs need to be initialized for the 8250 autoconfig to work
  120. * properly. Note that the TX watermark initialization may not be needed
  121. * once the 8250.c watermark handling code is merged.
  122. */
  123. static inline void __init omap_uart_reset(struct omap_uart_state *uart)
  124. {
  125. serial_write_reg(uart, UART_OMAP_MDR1, UART_OMAP_MDR1_DISABLE);
  126. serial_write_reg(uart, UART_OMAP_SCR, 0x08);
  127. serial_write_reg(uart, UART_OMAP_MDR1, UART_OMAP_MDR1_16X_MODE);
  128. }
  129. #if defined(CONFIG_PM) && defined(CONFIG_ARCH_OMAP3)
  130. /*
  131. * Work Around for Errata i202 (3430 - 1.12, 3630 - 1.6)
  132. * The access to uart register after MDR1 Access
  133. * causes UART to corrupt data.
  134. *
  135. * Need a delay =
  136. * 5 L4 clock cycles + 5 UART functional clock cycle (@48MHz = ~0.2uS)
  137. * give 10 times as much
  138. */
  139. static void omap_uart_mdr1_errataset(struct omap_uart_state *uart, u8 mdr1_val,
  140. u8 fcr_val)
  141. {
  142. u8 timeout = 255;
  143. serial_write_reg(uart, UART_OMAP_MDR1, mdr1_val);
  144. udelay(2);
  145. serial_write_reg(uart, UART_FCR, fcr_val | UART_FCR_CLEAR_XMIT |
  146. UART_FCR_CLEAR_RCVR);
  147. /*
  148. * Wait for FIFO to empty: when empty, RX_FIFO_E bit is 0 and
  149. * TX_FIFO_E bit is 1.
  150. */
  151. while (UART_LSR_THRE != (serial_read_reg(uart, UART_LSR) &
  152. (UART_LSR_THRE | UART_LSR_DR))) {
  153. timeout--;
  154. if (!timeout) {
  155. /* Should *never* happen. we warn and carry on */
  156. dev_crit(&uart->pdev->dev, "Errata i202: timedout %x\n",
  157. serial_read_reg(uart, UART_LSR));
  158. break;
  159. }
  160. udelay(1);
  161. }
  162. }
  163. static void omap_uart_save_context(struct omap_uart_state *uart)
  164. {
  165. u16 lcr = 0;
  166. if (!enable_off_mode)
  167. return;
  168. lcr = serial_read_reg(uart, UART_LCR);
  169. serial_write_reg(uart, UART_LCR, UART_LCR_CONF_MODE_B);
  170. uart->dll = serial_read_reg(uart, UART_DLL);
  171. uart->dlh = serial_read_reg(uart, UART_DLM);
  172. serial_write_reg(uart, UART_LCR, lcr);
  173. uart->ier = serial_read_reg(uart, UART_IER);
  174. uart->sysc = serial_read_reg(uart, UART_OMAP_SYSC);
  175. uart->scr = serial_read_reg(uart, UART_OMAP_SCR);
  176. uart->wer = serial_read_reg(uart, UART_OMAP_WER);
  177. serial_write_reg(uart, UART_LCR, UART_LCR_CONF_MODE_A);
  178. uart->mcr = serial_read_reg(uart, UART_MCR);
  179. serial_write_reg(uart, UART_LCR, lcr);
  180. uart->context_valid = 1;
  181. }
  182. static void omap_uart_restore_context(struct omap_uart_state *uart)
  183. {
  184. u16 efr = 0;
  185. if (!enable_off_mode)
  186. return;
  187. if (!uart->context_valid)
  188. return;
  189. uart->context_valid = 0;
  190. if (uart->errata & UART_ERRATA_i202_MDR1_ACCESS)
  191. omap_uart_mdr1_errataset(uart, UART_OMAP_MDR1_DISABLE, 0xA0);
  192. else
  193. serial_write_reg(uart, UART_OMAP_MDR1, UART_OMAP_MDR1_DISABLE);
  194. serial_write_reg(uart, UART_LCR, UART_LCR_CONF_MODE_B);
  195. efr = serial_read_reg(uart, UART_EFR);
  196. serial_write_reg(uart, UART_EFR, UART_EFR_ECB);
  197. serial_write_reg(uart, UART_LCR, 0x0); /* Operational mode */
  198. serial_write_reg(uart, UART_IER, 0x0);
  199. serial_write_reg(uart, UART_LCR, UART_LCR_CONF_MODE_B);
  200. serial_write_reg(uart, UART_DLL, uart->dll);
  201. serial_write_reg(uart, UART_DLM, uart->dlh);
  202. serial_write_reg(uart, UART_LCR, 0x0); /* Operational mode */
  203. serial_write_reg(uart, UART_IER, uart->ier);
  204. serial_write_reg(uart, UART_LCR, UART_LCR_CONF_MODE_A);
  205. serial_write_reg(uart, UART_MCR, uart->mcr);
  206. serial_write_reg(uart, UART_LCR, UART_LCR_CONF_MODE_B);
  207. serial_write_reg(uart, UART_EFR, efr);
  208. serial_write_reg(uart, UART_LCR, UART_LCR_WLEN8);
  209. serial_write_reg(uart, UART_OMAP_SCR, uart->scr);
  210. serial_write_reg(uart, UART_OMAP_WER, uart->wer);
  211. serial_write_reg(uart, UART_OMAP_SYSC, uart->sysc);
  212. if (uart->errata & UART_ERRATA_i202_MDR1_ACCESS)
  213. omap_uart_mdr1_errataset(uart, UART_OMAP_MDR1_16X_MODE, 0xA1);
  214. else
  215. /* UART 16x mode */
  216. serial_write_reg(uart, UART_OMAP_MDR1,
  217. UART_OMAP_MDR1_16X_MODE);
  218. }
  219. #else
  220. static inline void omap_uart_save_context(struct omap_uart_state *uart) {}
  221. static inline void omap_uart_restore_context(struct omap_uart_state *uart) {}
  222. #endif /* CONFIG_PM && CONFIG_ARCH_OMAP3 */
  223. static inline void omap_uart_enable_clocks(struct omap_uart_state *uart)
  224. {
  225. if (uart->clocked)
  226. return;
  227. omap_device_enable(uart->pdev);
  228. uart->clocked = 1;
  229. omap_uart_restore_context(uart);
  230. }
  231. #ifdef CONFIG_PM
  232. static inline void omap_uart_disable_clocks(struct omap_uart_state *uart)
  233. {
  234. if (!uart->clocked)
  235. return;
  236. omap_uart_save_context(uart);
  237. uart->clocked = 0;
  238. omap_device_idle(uart->pdev);
  239. }
  240. static void omap_uart_enable_wakeup(struct omap_uart_state *uart)
  241. {
  242. /* Set wake-enable bit */
  243. if (uart->wk_en && uart->wk_mask) {
  244. u32 v = __raw_readl(uart->wk_en);
  245. v |= uart->wk_mask;
  246. __raw_writel(v, uart->wk_en);
  247. }
  248. /* Ensure IOPAD wake-enables are set */
  249. if (cpu_is_omap34xx() && uart->padconf) {
  250. u16 v = omap_ctrl_readw(uart->padconf);
  251. v |= OMAP3_PADCONF_WAKEUPENABLE0;
  252. omap_ctrl_writew(v, uart->padconf);
  253. }
  254. }
  255. static void omap_uart_disable_wakeup(struct omap_uart_state *uart)
  256. {
  257. /* Clear wake-enable bit */
  258. if (uart->wk_en && uart->wk_mask) {
  259. u32 v = __raw_readl(uart->wk_en);
  260. v &= ~uart->wk_mask;
  261. __raw_writel(v, uart->wk_en);
  262. }
  263. /* Ensure IOPAD wake-enables are cleared */
  264. if (cpu_is_omap34xx() && uart->padconf) {
  265. u16 v = omap_ctrl_readw(uart->padconf);
  266. v &= ~OMAP3_PADCONF_WAKEUPENABLE0;
  267. omap_ctrl_writew(v, uart->padconf);
  268. }
  269. }
  270. static void omap_uart_smart_idle_enable(struct omap_uart_state *uart,
  271. int enable)
  272. {
  273. u8 idlemode;
  274. if (enable) {
  275. /**
  276. * Errata 2.15: [UART]:Cannot Acknowledge Idle Requests
  277. * in Smartidle Mode When Configured for DMA Operations.
  278. */
  279. if (uart->dma_enabled)
  280. idlemode = HWMOD_IDLEMODE_FORCE;
  281. else
  282. idlemode = HWMOD_IDLEMODE_SMART;
  283. } else {
  284. idlemode = HWMOD_IDLEMODE_NO;
  285. }
  286. omap_hwmod_set_slave_idlemode(uart->oh, idlemode);
  287. }
  288. static void omap_uart_block_sleep(struct omap_uart_state *uart)
  289. {
  290. omap_uart_enable_clocks(uart);
  291. omap_uart_smart_idle_enable(uart, 0);
  292. uart->can_sleep = 0;
  293. if (uart->timeout)
  294. mod_timer(&uart->timer, jiffies + uart->timeout);
  295. else
  296. del_timer(&uart->timer);
  297. }
  298. static void omap_uart_allow_sleep(struct omap_uart_state *uart)
  299. {
  300. if (device_may_wakeup(&uart->pdev->dev))
  301. omap_uart_enable_wakeup(uart);
  302. else
  303. omap_uart_disable_wakeup(uart);
  304. if (!uart->clocked)
  305. return;
  306. omap_uart_smart_idle_enable(uart, 1);
  307. uart->can_sleep = 1;
  308. del_timer(&uart->timer);
  309. }
  310. static void omap_uart_idle_timer(unsigned long data)
  311. {
  312. struct omap_uart_state *uart = (struct omap_uart_state *)data;
  313. omap_uart_allow_sleep(uart);
  314. }
  315. void omap_uart_prepare_idle(int num)
  316. {
  317. struct omap_uart_state *uart;
  318. list_for_each_entry(uart, &uart_list, node) {
  319. if (num == uart->num && uart->can_sleep) {
  320. omap_uart_disable_clocks(uart);
  321. return;
  322. }
  323. }
  324. }
  325. void omap_uart_resume_idle(int num)
  326. {
  327. struct omap_uart_state *uart;
  328. list_for_each_entry(uart, &uart_list, node) {
  329. if (num == uart->num && uart->can_sleep) {
  330. omap_uart_enable_clocks(uart);
  331. /* Check for IO pad wakeup */
  332. if (cpu_is_omap34xx() && uart->padconf) {
  333. u16 p = omap_ctrl_readw(uart->padconf);
  334. if (p & OMAP3_PADCONF_WAKEUPEVENT0)
  335. omap_uart_block_sleep(uart);
  336. }
  337. /* Check for normal UART wakeup */
  338. if (__raw_readl(uart->wk_st) & uart->wk_mask)
  339. omap_uart_block_sleep(uart);
  340. return;
  341. }
  342. }
  343. }
  344. void omap_uart_prepare_suspend(void)
  345. {
  346. struct omap_uart_state *uart;
  347. list_for_each_entry(uart, &uart_list, node) {
  348. omap_uart_allow_sleep(uart);
  349. }
  350. }
  351. int omap_uart_can_sleep(void)
  352. {
  353. struct omap_uart_state *uart;
  354. int can_sleep = 1;
  355. list_for_each_entry(uart, &uart_list, node) {
  356. if (!uart->clocked)
  357. continue;
  358. if (!uart->can_sleep) {
  359. can_sleep = 0;
  360. continue;
  361. }
  362. /* This UART can now safely sleep. */
  363. omap_uart_allow_sleep(uart);
  364. }
  365. return can_sleep;
  366. }
  367. /**
  368. * omap_uart_interrupt()
  369. *
  370. * This handler is used only to detect that *any* UART interrupt has
  371. * occurred. It does _nothing_ to handle the interrupt. Rather,
  372. * any UART interrupt will trigger the inactivity timer so the
  373. * UART will not idle or sleep for its timeout period.
  374. *
  375. **/
  376. /* static int first_interrupt; */
  377. static irqreturn_t omap_uart_interrupt(int irq, void *dev_id)
  378. {
  379. struct omap_uart_state *uart = dev_id;
  380. omap_uart_block_sleep(uart);
  381. return IRQ_NONE;
  382. }
  383. static void omap_uart_idle_init(struct omap_uart_state *uart)
  384. {
  385. int ret;
  386. uart->can_sleep = 0;
  387. uart->timeout = DEFAULT_TIMEOUT;
  388. setup_timer(&uart->timer, omap_uart_idle_timer,
  389. (unsigned long) uart);
  390. if (uart->timeout)
  391. mod_timer(&uart->timer, jiffies + uart->timeout);
  392. omap_uart_smart_idle_enable(uart, 0);
  393. if (cpu_is_omap34xx() && !cpu_is_ti816x()) {
  394. u32 mod = (uart->num > 1) ? OMAP3430_PER_MOD : CORE_MOD;
  395. u32 wk_mask = 0;
  396. u32 padconf = 0;
  397. /* XXX These PRM accesses do not belong here */
  398. uart->wk_en = OMAP34XX_PRM_REGADDR(mod, PM_WKEN1);
  399. uart->wk_st = OMAP34XX_PRM_REGADDR(mod, PM_WKST1);
  400. switch (uart->num) {
  401. case 0:
  402. wk_mask = OMAP3430_ST_UART1_MASK;
  403. padconf = 0x182;
  404. break;
  405. case 1:
  406. wk_mask = OMAP3430_ST_UART2_MASK;
  407. padconf = 0x17a;
  408. break;
  409. case 2:
  410. wk_mask = OMAP3430_ST_UART3_MASK;
  411. padconf = 0x19e;
  412. break;
  413. case 3:
  414. wk_mask = OMAP3630_ST_UART4_MASK;
  415. padconf = 0x0d2;
  416. break;
  417. }
  418. uart->wk_mask = wk_mask;
  419. uart->padconf = padconf;
  420. } else if (cpu_is_omap24xx()) {
  421. u32 wk_mask = 0;
  422. u32 wk_en = PM_WKEN1, wk_st = PM_WKST1;
  423. switch (uart->num) {
  424. case 0:
  425. wk_mask = OMAP24XX_ST_UART1_MASK;
  426. break;
  427. case 1:
  428. wk_mask = OMAP24XX_ST_UART2_MASK;
  429. break;
  430. case 2:
  431. wk_en = OMAP24XX_PM_WKEN2;
  432. wk_st = OMAP24XX_PM_WKST2;
  433. wk_mask = OMAP24XX_ST_UART3_MASK;
  434. break;
  435. }
  436. uart->wk_mask = wk_mask;
  437. if (cpu_is_omap2430()) {
  438. uart->wk_en = OMAP2430_PRM_REGADDR(CORE_MOD, wk_en);
  439. uart->wk_st = OMAP2430_PRM_REGADDR(CORE_MOD, wk_st);
  440. } else if (cpu_is_omap2420()) {
  441. uart->wk_en = OMAP2420_PRM_REGADDR(CORE_MOD, wk_en);
  442. uart->wk_st = OMAP2420_PRM_REGADDR(CORE_MOD, wk_st);
  443. }
  444. } else {
  445. uart->wk_en = NULL;
  446. uart->wk_st = NULL;
  447. uart->wk_mask = 0;
  448. uart->padconf = 0;
  449. }
  450. uart->irqflags |= IRQF_SHARED;
  451. ret = request_threaded_irq(uart->irq, NULL, omap_uart_interrupt,
  452. IRQF_SHARED, "serial idle", (void *)uart);
  453. WARN_ON(ret);
  454. }
  455. void omap_uart_enable_irqs(int enable)
  456. {
  457. int ret;
  458. struct omap_uart_state *uart;
  459. list_for_each_entry(uart, &uart_list, node) {
  460. if (enable) {
  461. pm_runtime_put_sync(&uart->pdev->dev);
  462. ret = request_threaded_irq(uart->irq, NULL,
  463. omap_uart_interrupt,
  464. IRQF_SHARED,
  465. "serial idle",
  466. (void *)uart);
  467. } else {
  468. pm_runtime_get_noresume(&uart->pdev->dev);
  469. free_irq(uart->irq, (void *)uart);
  470. }
  471. }
  472. }
  473. static ssize_t sleep_timeout_show(struct device *dev,
  474. struct device_attribute *attr,
  475. char *buf)
  476. {
  477. struct platform_device *pdev = to_platform_device(dev);
  478. struct omap_device *odev = to_omap_device(pdev);
  479. struct omap_uart_state *uart = odev->hwmods[0]->dev_attr;
  480. return sprintf(buf, "%u\n", uart->timeout / HZ);
  481. }
  482. static ssize_t sleep_timeout_store(struct device *dev,
  483. struct device_attribute *attr,
  484. const char *buf, size_t n)
  485. {
  486. struct platform_device *pdev = to_platform_device(dev);
  487. struct omap_device *odev = to_omap_device(pdev);
  488. struct omap_uart_state *uart = odev->hwmods[0]->dev_attr;
  489. unsigned int value;
  490. if (sscanf(buf, "%u", &value) != 1) {
  491. dev_err(dev, "sleep_timeout_store: Invalid value\n");
  492. return -EINVAL;
  493. }
  494. uart->timeout = value * HZ;
  495. if (uart->timeout)
  496. mod_timer(&uart->timer, jiffies + uart->timeout);
  497. else
  498. /* A zero value means disable timeout feature */
  499. omap_uart_block_sleep(uart);
  500. return n;
  501. }
  502. static DEVICE_ATTR(sleep_timeout, 0644, sleep_timeout_show,
  503. sleep_timeout_store);
  504. #define DEV_CREATE_FILE(dev, attr) WARN_ON(device_create_file(dev, attr))
  505. #else
  506. static inline void omap_uart_idle_init(struct omap_uart_state *uart) {}
  507. static void omap_uart_block_sleep(struct omap_uart_state *uart)
  508. {
  509. /* Needed to enable UART clocks when built without CONFIG_PM */
  510. omap_uart_enable_clocks(uart);
  511. }
  512. #define DEV_CREATE_FILE(dev, attr)
  513. #endif /* CONFIG_PM */
  514. #ifndef CONFIG_SERIAL_OMAP
  515. /*
  516. * Override the default 8250 read handler: mem_serial_in()
  517. * Empty RX fifo read causes an abort on omap3630 and omap4
  518. * This function makes sure that an empty rx fifo is not read on these silicons
  519. * (OMAP1/2/3430 are not affected)
  520. */
  521. static unsigned int serial_in_override(struct uart_port *up, int offset)
  522. {
  523. if (UART_RX == offset) {
  524. unsigned int lsr;
  525. lsr = __serial_read_reg(up, UART_LSR);
  526. if (!(lsr & UART_LSR_DR))
  527. return -EPERM;
  528. }
  529. return __serial_read_reg(up, offset);
  530. }
  531. static void serial_out_override(struct uart_port *up, int offset, int value)
  532. {
  533. unsigned int status, tmout = 10000;
  534. status = __serial_read_reg(up, UART_LSR);
  535. while (!(status & UART_LSR_THRE)) {
  536. /* Wait up to 10ms for the character(s) to be sent. */
  537. if (--tmout == 0)
  538. break;
  539. udelay(1);
  540. status = __serial_read_reg(up, UART_LSR);
  541. }
  542. __serial_write_reg(up, offset, value);
  543. }
  544. #endif
  545. static int __init omap_serial_early_init(void)
  546. {
  547. int i = 0;
  548. do {
  549. char oh_name[MAX_UART_HWMOD_NAME_LEN];
  550. struct omap_hwmod *oh;
  551. struct omap_uart_state *uart;
  552. snprintf(oh_name, MAX_UART_HWMOD_NAME_LEN,
  553. "uart%d", i + 1);
  554. oh = omap_hwmod_lookup(oh_name);
  555. if (!oh)
  556. break;
  557. uart = kzalloc(sizeof(struct omap_uart_state), GFP_KERNEL);
  558. if (WARN_ON(!uart))
  559. return -ENODEV;
  560. uart->oh = oh;
  561. uart->num = i++;
  562. list_add_tail(&uart->node, &uart_list);
  563. num_uarts++;
  564. /*
  565. * NOTE: omap_hwmod_setup*() has not yet been called,
  566. * so no hwmod functions will work yet.
  567. */
  568. /*
  569. * During UART early init, device need to be probed
  570. * to determine SoC specific init before omap_device
  571. * is ready. Therefore, don't allow idle here
  572. */
  573. uart->oh->flags |= HWMOD_INIT_NO_IDLE | HWMOD_INIT_NO_RESET;
  574. } while (1);
  575. return 0;
  576. }
  577. core_initcall(omap_serial_early_init);
  578. /**
  579. * omap_serial_init_port() - initialize single serial port
  580. * @bdata: port specific board data pointer
  581. *
  582. * This function initialies serial driver for given port only.
  583. * Platforms can call this function instead of omap_serial_init()
  584. * if they don't plan to use all available UARTs as serial ports.
  585. *
  586. * Don't mix calls to omap_serial_init_port() and omap_serial_init(),
  587. * use only one of the two.
  588. */
  589. void __init omap_serial_init_port(struct omap_board_data *bdata)
  590. {
  591. struct omap_uart_state *uart;
  592. struct omap_hwmod *oh;
  593. struct platform_device *pdev;
  594. void *pdata = NULL;
  595. u32 pdata_size = 0;
  596. char *name;
  597. #ifndef CONFIG_SERIAL_OMAP
  598. struct plat_serial8250_port ports[2] = {
  599. {},
  600. {.flags = 0},
  601. };
  602. struct plat_serial8250_port *p = &ports[0];
  603. #else
  604. struct omap_uart_port_info omap_up;
  605. #endif
  606. if (WARN_ON(!bdata))
  607. return;
  608. if (WARN_ON(bdata->id < 0))
  609. return;
  610. if (WARN_ON(bdata->id >= num_uarts))
  611. return;
  612. list_for_each_entry(uart, &uart_list, node)
  613. if (bdata->id == uart->num)
  614. break;
  615. oh = uart->oh;
  616. uart->dma_enabled = 0;
  617. #ifndef CONFIG_SERIAL_OMAP
  618. name = "serial8250";
  619. /*
  620. * !! 8250 driver does not use standard IORESOURCE* It
  621. * has it's own custom pdata that can be taken from
  622. * the hwmod resource data. But, this needs to be
  623. * done after the build.
  624. *
  625. * ?? does it have to be done before the register ??
  626. * YES, because platform_device_data_add() copies
  627. * pdata, it does not use a pointer.
  628. */
  629. p->flags = UPF_BOOT_AUTOCONF;
  630. p->iotype = UPIO_MEM;
  631. p->regshift = 2;
  632. p->uartclk = OMAP24XX_BASE_BAUD * 16;
  633. p->irq = oh->mpu_irqs[0].irq;
  634. p->mapbase = oh->slaves[0]->addr->pa_start;
  635. p->membase = omap_hwmod_get_mpu_rt_va(oh);
  636. p->irqflags = IRQF_SHARED;
  637. p->private_data = uart;
  638. /*
  639. * omap44xx, ti816x: Never read empty UART fifo
  640. * omap3xxx: Never read empty UART fifo on UARTs
  641. * with IP rev >=0x52
  642. */
  643. uart->regshift = p->regshift;
  644. uart->membase = p->membase;
  645. if (cpu_is_omap44xx() || cpu_is_ti816x())
  646. uart->errata |= UART_ERRATA_FIFO_FULL_ABORT;
  647. else if ((serial_read_reg(uart, UART_OMAP_MVER) & 0xFF)
  648. >= UART_OMAP_NO_EMPTY_FIFO_READ_IP_REV)
  649. uart->errata |= UART_ERRATA_FIFO_FULL_ABORT;
  650. if (uart->errata & UART_ERRATA_FIFO_FULL_ABORT) {
  651. p->serial_in = serial_in_override;
  652. p->serial_out = serial_out_override;
  653. }
  654. pdata = &ports[0];
  655. pdata_size = 2 * sizeof(struct plat_serial8250_port);
  656. #else
  657. name = DRIVER_NAME;
  658. omap_up.dma_enabled = uart->dma_enabled;
  659. omap_up.uartclk = OMAP24XX_BASE_BAUD * 16;
  660. omap_up.mapbase = oh->slaves[0]->addr->pa_start;
  661. omap_up.membase = omap_hwmod_get_mpu_rt_va(oh);
  662. omap_up.irqflags = IRQF_SHARED;
  663. omap_up.flags = UPF_BOOT_AUTOCONF | UPF_SHARE_IRQ;
  664. pdata = &omap_up;
  665. pdata_size = sizeof(struct omap_uart_port_info);
  666. #endif
  667. if (WARN_ON(!oh))
  668. return;
  669. pdev = omap_device_build(name, uart->num, oh, pdata, pdata_size,
  670. NULL, 0, false);
  671. WARN(IS_ERR(pdev), "Could not build omap_device for %s: %s.\n",
  672. name, oh->name);
  673. omap_device_disable_idle_on_suspend(pdev);
  674. oh->mux = omap_hwmod_mux_init(bdata->pads, bdata->pads_cnt);
  675. uart->irq = oh->mpu_irqs[0].irq;
  676. uart->regshift = 2;
  677. uart->mapbase = oh->slaves[0]->addr->pa_start;
  678. uart->membase = omap_hwmod_get_mpu_rt_va(oh);
  679. uart->pdev = pdev;
  680. oh->dev_attr = uart;
  681. console_lock(); /* in case the earlycon is on the UART */
  682. /*
  683. * Because of early UART probing, UART did not get idled
  684. * on init. Now that omap_device is ready, ensure full idle
  685. * before doing omap_device_enable().
  686. */
  687. omap_hwmod_idle(uart->oh);
  688. omap_device_enable(uart->pdev);
  689. omap_uart_idle_init(uart);
  690. omap_uart_reset(uart);
  691. omap_hwmod_enable_wakeup(uart->oh);
  692. omap_device_idle(uart->pdev);
  693. /*
  694. * Need to block sleep long enough for interrupt driven
  695. * driver to start. Console driver is in polling mode
  696. * so device needs to be kept enabled while polling driver
  697. * is in use.
  698. */
  699. if (uart->timeout)
  700. uart->timeout = (30 * HZ);
  701. omap_uart_block_sleep(uart);
  702. uart->timeout = DEFAULT_TIMEOUT;
  703. console_unlock();
  704. if ((cpu_is_omap34xx() && uart->padconf) ||
  705. (uart->wk_en && uart->wk_mask)) {
  706. device_init_wakeup(&pdev->dev, true);
  707. DEV_CREATE_FILE(&pdev->dev, &dev_attr_sleep_timeout);
  708. }
  709. /* Enable the MDR1 errata for OMAP3 */
  710. if (cpu_is_omap34xx() && !cpu_is_ti816x())
  711. uart->errata |= UART_ERRATA_i202_MDR1_ACCESS;
  712. }
  713. /**
  714. * omap_serial_init() - initialize all supported serial ports
  715. *
  716. * Initializes all available UARTs as serial ports. Platforms
  717. * can call this function when they want to have default behaviour
  718. * for serial ports (e.g initialize them all as serial ports).
  719. */
  720. void __init omap_serial_init(void)
  721. {
  722. struct omap_uart_state *uart;
  723. struct omap_board_data bdata;
  724. list_for_each_entry(uart, &uart_list, node) {
  725. bdata.id = uart->num;
  726. bdata.flags = 0;
  727. bdata.pads = NULL;
  728. bdata.pads_cnt = 0;
  729. omap_serial_init_port(&bdata);
  730. }
  731. }