serial.c 21 KB

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