serial.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  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/clk.h>
  23. #include <linux/io.h>
  24. #include <linux/delay.h>
  25. #include <linux/platform_device.h>
  26. #include <linux/slab.h>
  27. #include <linux/pm_runtime.h>
  28. #include <linux/console.h>
  29. #include <plat/omap-serial.h>
  30. #include "common.h"
  31. #include <plat/board.h>
  32. #include <plat/dma.h>
  33. #include <plat/omap_hwmod.h>
  34. #include <plat/omap_device.h>
  35. #include <plat/omap-pm.h>
  36. #include "prm2xxx_3xxx.h"
  37. #include "pm.h"
  38. #include "cm2xxx_3xxx.h"
  39. #include "prm-regbits-34xx.h"
  40. #include "control.h"
  41. #include "mux.h"
  42. #define UART_ERRATA_i202_MDR1_ACCESS (0x1 << 1)
  43. /*
  44. * NOTE: By default the serial timeout is disabled as it causes lost characters
  45. * over the serial ports. This means that the UART clocks will stay on until
  46. * disabled via sysfs. This also causes that any deeper omap sleep states are
  47. * blocked.
  48. */
  49. #define DEFAULT_TIMEOUT 0
  50. #define MAX_UART_HWMOD_NAME_LEN 16
  51. struct omap_uart_state {
  52. int num;
  53. int can_sleep;
  54. void __iomem *wk_st;
  55. void __iomem *wk_en;
  56. u32 wk_mask;
  57. u32 dma_enabled;
  58. int clocked;
  59. struct list_head node;
  60. struct omap_hwmod *oh;
  61. struct platform_device *pdev;
  62. u32 errata;
  63. };
  64. static LIST_HEAD(uart_list);
  65. static u8 num_uarts;
  66. #if defined(CONFIG_PM) && defined(CONFIG_ARCH_OMAP3)
  67. /*
  68. * Work Around for Errata i202 (3430 - 1.12, 3630 - 1.6)
  69. * The access to uart register after MDR1 Access
  70. * causes UART to corrupt data.
  71. *
  72. * Need a delay =
  73. * 5 L4 clock cycles + 5 UART functional clock cycle (@48MHz = ~0.2uS)
  74. * give 10 times as much
  75. */
  76. static void omap_uart_mdr1_errataset(struct omap_uart_state *uart, u8 mdr1_val,
  77. u8 fcr_val)
  78. {
  79. u8 timeout = 255;
  80. serial_write_reg(uart, UART_OMAP_MDR1, mdr1_val);
  81. udelay(2);
  82. serial_write_reg(uart, UART_FCR, fcr_val | UART_FCR_CLEAR_XMIT |
  83. UART_FCR_CLEAR_RCVR);
  84. /*
  85. * Wait for FIFO to empty: when empty, RX_FIFO_E bit is 0 and
  86. * TX_FIFO_E bit is 1.
  87. */
  88. while (UART_LSR_THRE != (serial_read_reg(uart, UART_LSR) &
  89. (UART_LSR_THRE | UART_LSR_DR))) {
  90. timeout--;
  91. if (!timeout) {
  92. /* Should *never* happen. we warn and carry on */
  93. dev_crit(&uart->pdev->dev, "Errata i202: timedout %x\n",
  94. serial_read_reg(uart, UART_LSR));
  95. break;
  96. }
  97. udelay(1);
  98. }
  99. }
  100. #endif /* CONFIG_PM && CONFIG_ARCH_OMAP3 */
  101. static inline void omap_uart_enable_clocks(struct omap_uart_state *uart)
  102. {
  103. if (uart->clocked)
  104. return;
  105. omap_device_enable(uart->pdev);
  106. uart->clocked = 1;
  107. omap_uart_restore_context(uart);
  108. }
  109. #ifdef CONFIG_PM
  110. static inline void omap_uart_disable_clocks(struct omap_uart_state *uart)
  111. {
  112. if (!uart->clocked)
  113. return;
  114. omap_uart_save_context(uart);
  115. uart->clocked = 0;
  116. omap_device_idle(uart->pdev);
  117. }
  118. static void omap_uart_enable_wakeup(struct omap_uart_state *uart)
  119. {
  120. /* Set wake-enable bit */
  121. if (uart->wk_en && uart->wk_mask) {
  122. u32 v = __raw_readl(uart->wk_en);
  123. v |= uart->wk_mask;
  124. __raw_writel(v, uart->wk_en);
  125. }
  126. }
  127. static void omap_uart_disable_wakeup(struct omap_uart_state *uart)
  128. {
  129. /* Clear wake-enable bit */
  130. if (uart->wk_en && uart->wk_mask) {
  131. u32 v = __raw_readl(uart->wk_en);
  132. v &= ~uart->wk_mask;
  133. __raw_writel(v, uart->wk_en);
  134. }
  135. }
  136. static void omap_uart_smart_idle_enable(struct omap_uart_state *uart,
  137. int enable)
  138. {
  139. u8 idlemode;
  140. if (enable) {
  141. /**
  142. * Errata 2.15: [UART]:Cannot Acknowledge Idle Requests
  143. * in Smartidle Mode When Configured for DMA Operations.
  144. */
  145. if (uart->dma_enabled)
  146. idlemode = HWMOD_IDLEMODE_FORCE;
  147. else
  148. idlemode = HWMOD_IDLEMODE_SMART;
  149. } else {
  150. idlemode = HWMOD_IDLEMODE_NO;
  151. }
  152. omap_hwmod_set_slave_idlemode(uart->oh, idlemode);
  153. }
  154. static void omap_uart_block_sleep(struct omap_uart_state *uart)
  155. {
  156. omap_uart_enable_clocks(uart);
  157. omap_uart_smart_idle_enable(uart, 0);
  158. uart->can_sleep = 0;
  159. }
  160. int omap_uart_can_sleep(void)
  161. {
  162. struct omap_uart_state *uart;
  163. int can_sleep = 1;
  164. list_for_each_entry(uart, &uart_list, node) {
  165. if (!uart->clocked)
  166. continue;
  167. if (!uart->can_sleep) {
  168. can_sleep = 0;
  169. continue;
  170. }
  171. /* This UART can now safely sleep. */
  172. omap_uart_allow_sleep(uart);
  173. }
  174. return can_sleep;
  175. }
  176. static void omap_uart_idle_init(struct omap_uart_state *uart)
  177. {
  178. int ret;
  179. uart->can_sleep = 0;
  180. omap_uart_smart_idle_enable(uart, 0);
  181. if (cpu_is_omap34xx() && !(cpu_is_ti81xx() || cpu_is_am33xx())) {
  182. u32 mod = (uart->num > 1) ? OMAP3430_PER_MOD : CORE_MOD;
  183. u32 wk_mask = 0;
  184. /* XXX These PRM accesses do not belong here */
  185. uart->wk_en = OMAP34XX_PRM_REGADDR(mod, PM_WKEN1);
  186. uart->wk_st = OMAP34XX_PRM_REGADDR(mod, PM_WKST1);
  187. switch (uart->num) {
  188. case 0:
  189. wk_mask = OMAP3430_ST_UART1_MASK;
  190. break;
  191. case 1:
  192. wk_mask = OMAP3430_ST_UART2_MASK;
  193. break;
  194. case 2:
  195. wk_mask = OMAP3430_ST_UART3_MASK;
  196. break;
  197. case 3:
  198. wk_mask = OMAP3630_ST_UART4_MASK;
  199. break;
  200. }
  201. uart->wk_mask = wk_mask;
  202. } else if (cpu_is_omap24xx()) {
  203. u32 wk_mask = 0;
  204. u32 wk_en = PM_WKEN1, wk_st = PM_WKST1;
  205. switch (uart->num) {
  206. case 0:
  207. wk_mask = OMAP24XX_ST_UART1_MASK;
  208. break;
  209. case 1:
  210. wk_mask = OMAP24XX_ST_UART2_MASK;
  211. break;
  212. case 2:
  213. wk_en = OMAP24XX_PM_WKEN2;
  214. wk_st = OMAP24XX_PM_WKST2;
  215. wk_mask = OMAP24XX_ST_UART3_MASK;
  216. break;
  217. }
  218. uart->wk_mask = wk_mask;
  219. if (cpu_is_omap2430()) {
  220. uart->wk_en = OMAP2430_PRM_REGADDR(CORE_MOD, wk_en);
  221. uart->wk_st = OMAP2430_PRM_REGADDR(CORE_MOD, wk_st);
  222. } else if (cpu_is_omap2420()) {
  223. uart->wk_en = OMAP2420_PRM_REGADDR(CORE_MOD, wk_en);
  224. uart->wk_st = OMAP2420_PRM_REGADDR(CORE_MOD, wk_st);
  225. }
  226. } else {
  227. uart->wk_en = NULL;
  228. uart->wk_st = NULL;
  229. uart->wk_mask = 0;
  230. }
  231. }
  232. #else
  233. static void omap_uart_block_sleep(struct omap_uart_state *uart)
  234. {
  235. /* Needed to enable UART clocks when built without CONFIG_PM */
  236. omap_uart_enable_clocks(uart);
  237. }
  238. #endif /* CONFIG_PM */
  239. #ifdef CONFIG_OMAP_MUX
  240. static struct omap_device_pad default_uart1_pads[] __initdata = {
  241. {
  242. .name = "uart1_cts.uart1_cts",
  243. .enable = OMAP_PIN_INPUT_PULLUP | OMAP_MUX_MODE0,
  244. },
  245. {
  246. .name = "uart1_rts.uart1_rts",
  247. .enable = OMAP_PIN_OUTPUT | OMAP_MUX_MODE0,
  248. },
  249. {
  250. .name = "uart1_tx.uart1_tx",
  251. .enable = OMAP_PIN_OUTPUT | OMAP_MUX_MODE0,
  252. },
  253. {
  254. .name = "uart1_rx.uart1_rx",
  255. .flags = OMAP_DEVICE_PAD_REMUX | OMAP_DEVICE_PAD_WAKEUP,
  256. .enable = OMAP_PIN_INPUT_PULLUP | OMAP_MUX_MODE0,
  257. .idle = OMAP_PIN_INPUT_PULLUP | OMAP_MUX_MODE0,
  258. },
  259. };
  260. static struct omap_device_pad default_uart2_pads[] __initdata = {
  261. {
  262. .name = "uart2_cts.uart2_cts",
  263. .enable = OMAP_PIN_INPUT_PULLUP | OMAP_MUX_MODE0,
  264. },
  265. {
  266. .name = "uart2_rts.uart2_rts",
  267. .enable = OMAP_PIN_OUTPUT | OMAP_MUX_MODE0,
  268. },
  269. {
  270. .name = "uart2_tx.uart2_tx",
  271. .enable = OMAP_PIN_OUTPUT | OMAP_MUX_MODE0,
  272. },
  273. {
  274. .name = "uart2_rx.uart2_rx",
  275. .flags = OMAP_DEVICE_PAD_REMUX | OMAP_DEVICE_PAD_WAKEUP,
  276. .enable = OMAP_PIN_INPUT_PULLUP | OMAP_MUX_MODE0,
  277. .idle = OMAP_PIN_INPUT_PULLUP | OMAP_MUX_MODE0,
  278. },
  279. };
  280. static struct omap_device_pad default_uart3_pads[] __initdata = {
  281. {
  282. .name = "uart3_cts_rctx.uart3_cts_rctx",
  283. .enable = OMAP_PIN_INPUT_PULLUP | OMAP_MUX_MODE0,
  284. },
  285. {
  286. .name = "uart3_rts_sd.uart3_rts_sd",
  287. .enable = OMAP_PIN_OUTPUT | OMAP_MUX_MODE0,
  288. },
  289. {
  290. .name = "uart3_tx_irtx.uart3_tx_irtx",
  291. .enable = OMAP_PIN_OUTPUT | OMAP_MUX_MODE0,
  292. },
  293. {
  294. .name = "uart3_rx_irrx.uart3_rx_irrx",
  295. .flags = OMAP_DEVICE_PAD_REMUX | OMAP_DEVICE_PAD_WAKEUP,
  296. .enable = OMAP_PIN_INPUT | OMAP_MUX_MODE0,
  297. .idle = OMAP_PIN_INPUT | OMAP_MUX_MODE0,
  298. },
  299. };
  300. static struct omap_device_pad default_omap36xx_uart4_pads[] __initdata = {
  301. {
  302. .name = "gpmc_wait2.uart4_tx",
  303. .enable = OMAP_PIN_OUTPUT | OMAP_MUX_MODE0,
  304. },
  305. {
  306. .name = "gpmc_wait3.uart4_rx",
  307. .flags = OMAP_DEVICE_PAD_REMUX | OMAP_DEVICE_PAD_WAKEUP,
  308. .enable = OMAP_PIN_INPUT | OMAP_MUX_MODE2,
  309. .idle = OMAP_PIN_INPUT | OMAP_MUX_MODE2,
  310. },
  311. };
  312. static struct omap_device_pad default_omap4_uart4_pads[] __initdata = {
  313. {
  314. .name = "uart4_tx.uart4_tx",
  315. .enable = OMAP_PIN_OUTPUT | OMAP_MUX_MODE0,
  316. },
  317. {
  318. .name = "uart4_rx.uart4_rx",
  319. .flags = OMAP_DEVICE_PAD_REMUX | OMAP_DEVICE_PAD_WAKEUP,
  320. .enable = OMAP_PIN_INPUT | OMAP_MUX_MODE0,
  321. .idle = OMAP_PIN_INPUT | OMAP_MUX_MODE0,
  322. },
  323. };
  324. static void omap_serial_fill_default_pads(struct omap_board_data *bdata)
  325. {
  326. switch (bdata->id) {
  327. case 0:
  328. bdata->pads = default_uart1_pads;
  329. bdata->pads_cnt = ARRAY_SIZE(default_uart1_pads);
  330. break;
  331. case 1:
  332. bdata->pads = default_uart2_pads;
  333. bdata->pads_cnt = ARRAY_SIZE(default_uart2_pads);
  334. break;
  335. case 2:
  336. bdata->pads = default_uart3_pads;
  337. bdata->pads_cnt = ARRAY_SIZE(default_uart3_pads);
  338. break;
  339. case 3:
  340. if (cpu_is_omap44xx()) {
  341. bdata->pads = default_omap4_uart4_pads;
  342. bdata->pads_cnt =
  343. ARRAY_SIZE(default_omap4_uart4_pads);
  344. } else if (cpu_is_omap3630()) {
  345. bdata->pads = default_omap36xx_uart4_pads;
  346. bdata->pads_cnt =
  347. ARRAY_SIZE(default_omap36xx_uart4_pads);
  348. }
  349. break;
  350. default:
  351. break;
  352. }
  353. }
  354. #else
  355. static void omap_serial_fill_default_pads(struct omap_board_data *bdata) {}
  356. #endif
  357. static int __init omap_serial_early_init(void)
  358. {
  359. int i = 0;
  360. do {
  361. char oh_name[MAX_UART_HWMOD_NAME_LEN];
  362. struct omap_hwmod *oh;
  363. struct omap_uart_state *uart;
  364. snprintf(oh_name, MAX_UART_HWMOD_NAME_LEN,
  365. "uart%d", i + 1);
  366. oh = omap_hwmod_lookup(oh_name);
  367. if (!oh)
  368. break;
  369. uart = kzalloc(sizeof(struct omap_uart_state), GFP_KERNEL);
  370. if (WARN_ON(!uart))
  371. return -ENODEV;
  372. uart->oh = oh;
  373. uart->num = i++;
  374. list_add_tail(&uart->node, &uart_list);
  375. num_uarts++;
  376. /*
  377. * NOTE: omap_hwmod_setup*() has not yet been called,
  378. * so no hwmod functions will work yet.
  379. */
  380. /*
  381. * During UART early init, device need to be probed
  382. * to determine SoC specific init before omap_device
  383. * is ready. Therefore, don't allow idle here
  384. */
  385. uart->oh->flags |= HWMOD_INIT_NO_IDLE | HWMOD_INIT_NO_RESET;
  386. } while (1);
  387. return 0;
  388. }
  389. core_initcall(omap_serial_early_init);
  390. /**
  391. * omap_serial_init_port() - initialize single serial port
  392. * @bdata: port specific board data pointer
  393. *
  394. * This function initialies serial driver for given port only.
  395. * Platforms can call this function instead of omap_serial_init()
  396. * if they don't plan to use all available UARTs as serial ports.
  397. *
  398. * Don't mix calls to omap_serial_init_port() and omap_serial_init(),
  399. * use only one of the two.
  400. */
  401. void __init omap_serial_init_port(struct omap_board_data *bdata)
  402. {
  403. struct omap_uart_state *uart;
  404. struct omap_hwmod *oh;
  405. struct platform_device *pdev;
  406. void *pdata = NULL;
  407. u32 pdata_size = 0;
  408. char *name;
  409. struct omap_uart_port_info omap_up;
  410. if (WARN_ON(!bdata))
  411. return;
  412. if (WARN_ON(bdata->id < 0))
  413. return;
  414. if (WARN_ON(bdata->id >= num_uarts))
  415. return;
  416. list_for_each_entry(uart, &uart_list, node)
  417. if (bdata->id == uart->num)
  418. break;
  419. oh = uart->oh;
  420. uart->dma_enabled = 0;
  421. name = DRIVER_NAME;
  422. omap_up.dma_enabled = uart->dma_enabled;
  423. omap_up.uartclk = OMAP24XX_BASE_BAUD * 16;
  424. omap_up.flags = UPF_BOOT_AUTOCONF;
  425. omap_up.get_context_loss_count = omap_pm_get_dev_context_loss_count;
  426. pdata = &omap_up;
  427. pdata_size = sizeof(struct omap_uart_port_info);
  428. if (WARN_ON(!oh))
  429. return;
  430. pdev = omap_device_build(name, uart->num, oh, pdata, pdata_size,
  431. NULL, 0, false);
  432. WARN(IS_ERR(pdev), "Could not build omap_device for %s: %s.\n",
  433. name, oh->name);
  434. omap_device_disable_idle_on_suspend(pdev);
  435. oh->mux = omap_hwmod_mux_init(bdata->pads, bdata->pads_cnt);
  436. uart->pdev = pdev;
  437. oh->dev_attr = uart;
  438. console_lock(); /* in case the earlycon is on the UART */
  439. /*
  440. * Because of early UART probing, UART did not get idled
  441. * on init. Now that omap_device is ready, ensure full idle
  442. * before doing omap_device_enable().
  443. */
  444. omap_hwmod_idle(uart->oh);
  445. omap_device_enable(uart->pdev);
  446. omap_uart_idle_init(uart);
  447. omap_hwmod_enable_wakeup(uart->oh);
  448. omap_device_idle(uart->pdev);
  449. omap_uart_block_sleep(uart);
  450. console_unlock();
  451. if (((cpu_is_omap34xx() || cpu_is_omap44xx()) && bdata->pads) ||
  452. (pdata->wk_en && pdata->wk_mask))
  453. device_init_wakeup(&pdev->dev, true);
  454. /* Enable the MDR1 errata for OMAP3 */
  455. if (cpu_is_omap34xx() && !(cpu_is_ti81xx() || cpu_is_am33xx()))
  456. uart->errata |= UART_ERRATA_i202_MDR1_ACCESS;
  457. }
  458. /**
  459. * omap_serial_init() - initialize all supported serial ports
  460. *
  461. * Initializes all available UARTs as serial ports. Platforms
  462. * can call this function when they want to have default behaviour
  463. * for serial ports (e.g initialize them all as serial ports).
  464. */
  465. void __init omap_serial_init(void)
  466. {
  467. struct omap_uart_state *uart;
  468. struct omap_board_data bdata;
  469. list_for_each_entry(uart, &uart_list, node) {
  470. bdata.id = uart->num;
  471. bdata.flags = 0;
  472. bdata.pads = NULL;
  473. bdata.pads_cnt = 0;
  474. if (cpu_is_omap44xx() || cpu_is_omap34xx())
  475. omap_serial_fill_default_pads(&bdata);
  476. omap_serial_init_port(&bdata);
  477. }
  478. }