serial.c 21 KB

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