serial.c 21 KB

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