serial.c 21 KB

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