serial.c 20 KB

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