serial.c 20 KB

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