serial.c 22 KB

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