serial.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650
  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_8250.h>
  23. #include <linux/serial_reg.h>
  24. #include <linux/clk.h>
  25. #include <linux/io.h>
  26. #include <mach/common.h>
  27. #include <mach/board.h>
  28. #include <mach/clock.h>
  29. #include <mach/control.h>
  30. #include "prm.h"
  31. #include "pm.h"
  32. #include "prm-regbits-34xx.h"
  33. #define UART_OMAP_WER 0x17 /* Wake-up enable register */
  34. #define DEFAULT_TIMEOUT (5 * HZ)
  35. struct omap_uart_state {
  36. int num;
  37. int can_sleep;
  38. struct timer_list timer;
  39. u32 timeout;
  40. void __iomem *wk_st;
  41. void __iomem *wk_en;
  42. u32 wk_mask;
  43. u32 padconf;
  44. struct clk *ick;
  45. struct clk *fck;
  46. int clocked;
  47. struct plat_serial8250_port *p;
  48. struct list_head node;
  49. struct platform_device pdev;
  50. #if defined(CONFIG_ARCH_OMAP3) && defined(CONFIG_PM)
  51. int context_valid;
  52. /* Registers to be saved/restored for OFF-mode */
  53. u16 dll;
  54. u16 dlh;
  55. u16 ier;
  56. u16 sysc;
  57. u16 scr;
  58. u16 wer;
  59. #endif
  60. };
  61. static LIST_HEAD(uart_list);
  62. static struct plat_serial8250_port serial_platform_data0[] = {
  63. {
  64. .membase = OMAP2_IO_ADDRESS(OMAP_UART1_BASE),
  65. .mapbase = OMAP_UART1_BASE,
  66. .irq = 72,
  67. .flags = UPF_BOOT_AUTOCONF,
  68. .iotype = UPIO_MEM,
  69. .regshift = 2,
  70. .uartclk = OMAP24XX_BASE_BAUD * 16,
  71. }, {
  72. .flags = 0
  73. }
  74. };
  75. static struct plat_serial8250_port serial_platform_data1[] = {
  76. {
  77. .membase = OMAP2_IO_ADDRESS(OMAP_UART2_BASE),
  78. .mapbase = OMAP_UART2_BASE,
  79. .irq = 73,
  80. .flags = UPF_BOOT_AUTOCONF,
  81. .iotype = UPIO_MEM,
  82. .regshift = 2,
  83. .uartclk = OMAP24XX_BASE_BAUD * 16,
  84. }, {
  85. .flags = 0
  86. }
  87. };
  88. static struct plat_serial8250_port serial_platform_data2[] = {
  89. {
  90. .membase = OMAP2_IO_ADDRESS(OMAP_UART3_BASE),
  91. .mapbase = OMAP_UART3_BASE,
  92. .irq = 74,
  93. .flags = UPF_BOOT_AUTOCONF,
  94. .iotype = UPIO_MEM,
  95. .regshift = 2,
  96. .uartclk = OMAP24XX_BASE_BAUD * 16,
  97. }, {
  98. .flags = 0
  99. }
  100. };
  101. #ifdef CONFIG_ARCH_OMAP4
  102. static struct plat_serial8250_port serial_platform_data3[] = {
  103. {
  104. .membase = OMAP2_IO_ADDRESS(OMAP_UART4_BASE),
  105. .mapbase = OMAP_UART4_BASE,
  106. .irq = 70,
  107. .flags = UPF_BOOT_AUTOCONF,
  108. .iotype = UPIO_MEM,
  109. .regshift = 2,
  110. .uartclk = OMAP24XX_BASE_BAUD * 16,
  111. }, {
  112. .flags = 0
  113. }
  114. };
  115. #endif
  116. static inline unsigned int serial_read_reg(struct plat_serial8250_port *up,
  117. int offset)
  118. {
  119. offset <<= up->regshift;
  120. return (unsigned int)__raw_readb(up->membase + offset);
  121. }
  122. static inline void serial_write_reg(struct plat_serial8250_port *p, int offset,
  123. int value)
  124. {
  125. offset <<= p->regshift;
  126. __raw_writeb(value, p->membase + offset);
  127. }
  128. /*
  129. * Internal UARTs need to be initialized for the 8250 autoconfig to work
  130. * properly. Note that the TX watermark initialization may not be needed
  131. * once the 8250.c watermark handling code is merged.
  132. */
  133. static inline void __init omap_uart_reset(struct omap_uart_state *uart)
  134. {
  135. struct plat_serial8250_port *p = uart->p;
  136. serial_write_reg(p, UART_OMAP_MDR1, 0x07);
  137. serial_write_reg(p, UART_OMAP_SCR, 0x08);
  138. serial_write_reg(p, UART_OMAP_MDR1, 0x00);
  139. serial_write_reg(p, UART_OMAP_SYSC, (0x02 << 3) | (1 << 2) | (1 << 0));
  140. }
  141. #if defined(CONFIG_PM) && defined(CONFIG_ARCH_OMAP3)
  142. static int enable_off_mode; /* to be removed by full off-mode patches */
  143. static void omap_uart_save_context(struct omap_uart_state *uart)
  144. {
  145. u16 lcr = 0;
  146. struct plat_serial8250_port *p = uart->p;
  147. if (!enable_off_mode)
  148. return;
  149. lcr = serial_read_reg(p, UART_LCR);
  150. serial_write_reg(p, UART_LCR, 0xBF);
  151. uart->dll = serial_read_reg(p, UART_DLL);
  152. uart->dlh = serial_read_reg(p, UART_DLM);
  153. serial_write_reg(p, UART_LCR, lcr);
  154. uart->ier = serial_read_reg(p, UART_IER);
  155. uart->sysc = serial_read_reg(p, UART_OMAP_SYSC);
  156. uart->scr = serial_read_reg(p, UART_OMAP_SCR);
  157. uart->wer = serial_read_reg(p, UART_OMAP_WER);
  158. uart->context_valid = 1;
  159. }
  160. static void omap_uart_restore_context(struct omap_uart_state *uart)
  161. {
  162. u16 efr = 0;
  163. struct plat_serial8250_port *p = uart->p;
  164. if (!enable_off_mode)
  165. return;
  166. if (!uart->context_valid)
  167. return;
  168. uart->context_valid = 0;
  169. serial_write_reg(p, UART_OMAP_MDR1, 0x7);
  170. serial_write_reg(p, UART_LCR, 0xBF); /* Config B mode */
  171. efr = serial_read_reg(p, UART_EFR);
  172. serial_write_reg(p, UART_EFR, UART_EFR_ECB);
  173. serial_write_reg(p, UART_LCR, 0x0); /* Operational mode */
  174. serial_write_reg(p, UART_IER, 0x0);
  175. serial_write_reg(p, UART_LCR, 0xBF); /* Config B mode */
  176. serial_write_reg(p, UART_DLL, uart->dll);
  177. serial_write_reg(p, UART_DLM, uart->dlh);
  178. serial_write_reg(p, UART_LCR, 0x0); /* Operational mode */
  179. serial_write_reg(p, UART_IER, uart->ier);
  180. serial_write_reg(p, UART_FCR, 0xA1);
  181. serial_write_reg(p, UART_LCR, 0xBF); /* Config B mode */
  182. serial_write_reg(p, UART_EFR, efr);
  183. serial_write_reg(p, UART_LCR, UART_LCR_WLEN8);
  184. serial_write_reg(p, UART_OMAP_SCR, uart->scr);
  185. serial_write_reg(p, UART_OMAP_WER, uart->wer);
  186. serial_write_reg(p, UART_OMAP_SYSC, uart->sysc);
  187. serial_write_reg(p, UART_OMAP_MDR1, 0x00); /* UART 16x mode */
  188. }
  189. #else
  190. static inline void omap_uart_save_context(struct omap_uart_state *uart) {}
  191. static inline void omap_uart_restore_context(struct omap_uart_state *uart) {}
  192. #endif /* CONFIG_PM && CONFIG_ARCH_OMAP3 */
  193. static inline void omap_uart_enable_clocks(struct omap_uart_state *uart)
  194. {
  195. if (uart->clocked)
  196. return;
  197. clk_enable(uart->ick);
  198. clk_enable(uart->fck);
  199. uart->clocked = 1;
  200. omap_uart_restore_context(uart);
  201. }
  202. #ifdef CONFIG_PM
  203. static inline void omap_uart_disable_clocks(struct omap_uart_state *uart)
  204. {
  205. if (!uart->clocked)
  206. return;
  207. omap_uart_save_context(uart);
  208. uart->clocked = 0;
  209. clk_disable(uart->ick);
  210. clk_disable(uart->fck);
  211. }
  212. static void omap_uart_enable_wakeup(struct omap_uart_state *uart)
  213. {
  214. /* Set wake-enable bit */
  215. if (uart->wk_en && uart->wk_mask) {
  216. u32 v = __raw_readl(uart->wk_en);
  217. v |= uart->wk_mask;
  218. __raw_writel(v, uart->wk_en);
  219. }
  220. /* Ensure IOPAD wake-enables are set */
  221. if (cpu_is_omap34xx() && uart->padconf) {
  222. u16 v = omap_ctrl_readw(uart->padconf);
  223. v |= OMAP3_PADCONF_WAKEUPENABLE0;
  224. omap_ctrl_writew(v, uart->padconf);
  225. }
  226. }
  227. static void omap_uart_disable_wakeup(struct omap_uart_state *uart)
  228. {
  229. /* Clear wake-enable bit */
  230. if (uart->wk_en && uart->wk_mask) {
  231. u32 v = __raw_readl(uart->wk_en);
  232. v &= ~uart->wk_mask;
  233. __raw_writel(v, uart->wk_en);
  234. }
  235. /* Ensure IOPAD wake-enables are cleared */
  236. if (cpu_is_omap34xx() && uart->padconf) {
  237. u16 v = omap_ctrl_readw(uart->padconf);
  238. v &= ~OMAP3_PADCONF_WAKEUPENABLE0;
  239. omap_ctrl_writew(v, uart->padconf);
  240. }
  241. }
  242. static void omap_uart_smart_idle_enable(struct omap_uart_state *uart,
  243. int enable)
  244. {
  245. struct plat_serial8250_port *p = uart->p;
  246. u16 sysc;
  247. sysc = serial_read_reg(p, UART_OMAP_SYSC) & 0x7;
  248. if (enable)
  249. sysc |= 0x2 << 3;
  250. else
  251. sysc |= 0x1 << 3;
  252. serial_write_reg(p, UART_OMAP_SYSC, sysc);
  253. }
  254. static void omap_uart_block_sleep(struct omap_uart_state *uart)
  255. {
  256. omap_uart_enable_clocks(uart);
  257. omap_uart_smart_idle_enable(uart, 0);
  258. uart->can_sleep = 0;
  259. if (uart->timeout)
  260. mod_timer(&uart->timer, jiffies + uart->timeout);
  261. else
  262. del_timer(&uart->timer);
  263. }
  264. static void omap_uart_allow_sleep(struct omap_uart_state *uart)
  265. {
  266. if (device_may_wakeup(&uart->pdev.dev))
  267. omap_uart_enable_wakeup(uart);
  268. else
  269. omap_uart_disable_wakeup(uart);
  270. if (!uart->clocked)
  271. return;
  272. omap_uart_smart_idle_enable(uart, 1);
  273. uart->can_sleep = 1;
  274. del_timer(&uart->timer);
  275. }
  276. static void omap_uart_idle_timer(unsigned long data)
  277. {
  278. struct omap_uart_state *uart = (struct omap_uart_state *)data;
  279. omap_uart_allow_sleep(uart);
  280. }
  281. void omap_uart_prepare_idle(int num)
  282. {
  283. struct omap_uart_state *uart;
  284. list_for_each_entry(uart, &uart_list, node) {
  285. if (num == uart->num && uart->can_sleep) {
  286. omap_uart_disable_clocks(uart);
  287. return;
  288. }
  289. }
  290. }
  291. void omap_uart_resume_idle(int num)
  292. {
  293. struct omap_uart_state *uart;
  294. list_for_each_entry(uart, &uart_list, node) {
  295. if (num == uart->num) {
  296. omap_uart_enable_clocks(uart);
  297. /* Check for IO pad wakeup */
  298. if (cpu_is_omap34xx() && uart->padconf) {
  299. u16 p = omap_ctrl_readw(uart->padconf);
  300. if (p & OMAP3_PADCONF_WAKEUPEVENT0)
  301. omap_uart_block_sleep(uart);
  302. }
  303. /* Check for normal UART wakeup */
  304. if (__raw_readl(uart->wk_st) & uart->wk_mask)
  305. omap_uart_block_sleep(uart);
  306. return;
  307. }
  308. }
  309. }
  310. void omap_uart_prepare_suspend(void)
  311. {
  312. struct omap_uart_state *uart;
  313. list_for_each_entry(uart, &uart_list, node) {
  314. omap_uart_allow_sleep(uart);
  315. }
  316. }
  317. int omap_uart_can_sleep(void)
  318. {
  319. struct omap_uart_state *uart;
  320. int can_sleep = 1;
  321. list_for_each_entry(uart, &uart_list, node) {
  322. if (!uart->clocked)
  323. continue;
  324. if (!uart->can_sleep) {
  325. can_sleep = 0;
  326. continue;
  327. }
  328. /* This UART can now safely sleep. */
  329. omap_uart_allow_sleep(uart);
  330. }
  331. return can_sleep;
  332. }
  333. /**
  334. * omap_uart_interrupt()
  335. *
  336. * This handler is used only to detect that *any* UART interrupt has
  337. * occurred. It does _nothing_ to handle the interrupt. Rather,
  338. * any UART interrupt will trigger the inactivity timer so the
  339. * UART will not idle or sleep for its timeout period.
  340. *
  341. **/
  342. static irqreturn_t omap_uart_interrupt(int irq, void *dev_id)
  343. {
  344. struct omap_uart_state *uart = dev_id;
  345. omap_uart_block_sleep(uart);
  346. return IRQ_NONE;
  347. }
  348. static void omap_uart_idle_init(struct omap_uart_state *uart)
  349. {
  350. struct plat_serial8250_port *p = uart->p;
  351. int ret;
  352. uart->can_sleep = 0;
  353. uart->timeout = DEFAULT_TIMEOUT;
  354. setup_timer(&uart->timer, omap_uart_idle_timer,
  355. (unsigned long) uart);
  356. mod_timer(&uart->timer, jiffies + uart->timeout);
  357. omap_uart_smart_idle_enable(uart, 0);
  358. if (cpu_is_omap34xx()) {
  359. u32 mod = (uart->num == 2) ? OMAP3430_PER_MOD : CORE_MOD;
  360. u32 wk_mask = 0;
  361. u32 padconf = 0;
  362. uart->wk_en = OMAP34XX_PRM_REGADDR(mod, PM_WKEN1);
  363. uart->wk_st = OMAP34XX_PRM_REGADDR(mod, PM_WKST1);
  364. switch (uart->num) {
  365. case 0:
  366. wk_mask = OMAP3430_ST_UART1_MASK;
  367. padconf = 0x182;
  368. break;
  369. case 1:
  370. wk_mask = OMAP3430_ST_UART2_MASK;
  371. padconf = 0x17a;
  372. break;
  373. case 2:
  374. wk_mask = OMAP3430_ST_UART3_MASK;
  375. padconf = 0x19e;
  376. break;
  377. }
  378. uart->wk_mask = wk_mask;
  379. uart->padconf = padconf;
  380. } else if (cpu_is_omap24xx()) {
  381. u32 wk_mask = 0;
  382. if (cpu_is_omap2430()) {
  383. uart->wk_en = OMAP2430_PRM_REGADDR(CORE_MOD, PM_WKEN1);
  384. uart->wk_st = OMAP2430_PRM_REGADDR(CORE_MOD, PM_WKST1);
  385. } else if (cpu_is_omap2420()) {
  386. uart->wk_en = OMAP2420_PRM_REGADDR(CORE_MOD, PM_WKEN1);
  387. uart->wk_st = OMAP2420_PRM_REGADDR(CORE_MOD, PM_WKST1);
  388. }
  389. switch (uart->num) {
  390. case 0:
  391. wk_mask = OMAP24XX_ST_UART1_MASK;
  392. break;
  393. case 1:
  394. wk_mask = OMAP24XX_ST_UART2_MASK;
  395. break;
  396. case 2:
  397. wk_mask = OMAP24XX_ST_UART3_MASK;
  398. break;
  399. }
  400. uart->wk_mask = wk_mask;
  401. } else {
  402. uart->wk_en = 0;
  403. uart->wk_st = 0;
  404. uart->wk_mask = 0;
  405. uart->padconf = 0;
  406. }
  407. p->irqflags |= IRQF_SHARED;
  408. ret = request_irq(p->irq, omap_uart_interrupt, IRQF_SHARED,
  409. "serial idle", (void *)uart);
  410. WARN_ON(ret);
  411. }
  412. void omap_uart_enable_irqs(int enable)
  413. {
  414. int ret;
  415. struct omap_uart_state *uart;
  416. list_for_each_entry(uart, &uart_list, node) {
  417. if (enable)
  418. ret = request_irq(uart->p->irq, omap_uart_interrupt,
  419. IRQF_SHARED, "serial idle", (void *)uart);
  420. else
  421. free_irq(uart->p->irq, (void *)uart);
  422. }
  423. }
  424. static ssize_t sleep_timeout_show(struct device *dev,
  425. struct device_attribute *attr,
  426. char *buf)
  427. {
  428. struct platform_device *pdev = container_of(dev,
  429. struct platform_device, dev);
  430. struct omap_uart_state *uart = container_of(pdev,
  431. struct omap_uart_state, pdev);
  432. return sprintf(buf, "%u\n", uart->timeout / HZ);
  433. }
  434. static ssize_t sleep_timeout_store(struct device *dev,
  435. struct device_attribute *attr,
  436. const char *buf, size_t n)
  437. {
  438. struct platform_device *pdev = container_of(dev,
  439. struct platform_device, dev);
  440. struct omap_uart_state *uart = container_of(pdev,
  441. struct omap_uart_state, pdev);
  442. unsigned int value;
  443. if (sscanf(buf, "%u", &value) != 1) {
  444. printk(KERN_ERR "sleep_timeout_store: Invalid value\n");
  445. return -EINVAL;
  446. }
  447. uart->timeout = value * HZ;
  448. if (uart->timeout)
  449. mod_timer(&uart->timer, jiffies + uart->timeout);
  450. else
  451. /* A zero value means disable timeout feature */
  452. omap_uart_block_sleep(uart);
  453. return n;
  454. }
  455. DEVICE_ATTR(sleep_timeout, 0644, sleep_timeout_show, sleep_timeout_store);
  456. #define DEV_CREATE_FILE(dev, attr) WARN_ON(device_create_file(dev, attr))
  457. #else
  458. static inline void omap_uart_idle_init(struct omap_uart_state *uart) {}
  459. #define DEV_CREATE_FILE(dev, attr)
  460. #endif /* CONFIG_PM */
  461. static struct omap_uart_state omap_uart[OMAP_MAX_NR_PORTS] = {
  462. {
  463. .pdev = {
  464. .name = "serial8250",
  465. .id = PLAT8250_DEV_PLATFORM,
  466. .dev = {
  467. .platform_data = serial_platform_data0,
  468. },
  469. },
  470. }, {
  471. .pdev = {
  472. .name = "serial8250",
  473. .id = PLAT8250_DEV_PLATFORM1,
  474. .dev = {
  475. .platform_data = serial_platform_data1,
  476. },
  477. },
  478. }, {
  479. .pdev = {
  480. .name = "serial8250",
  481. .id = PLAT8250_DEV_PLATFORM2,
  482. .dev = {
  483. .platform_data = serial_platform_data2,
  484. },
  485. },
  486. },
  487. #ifdef CONFIG_ARCH_OMAP4
  488. {
  489. .pdev = {
  490. .name = "serial8250",
  491. .id = 3,
  492. .dev = {
  493. .platform_data = serial_platform_data3,
  494. },
  495. },
  496. },
  497. #endif
  498. };
  499. void __init omap_serial_early_init(void)
  500. {
  501. int i;
  502. char name[16];
  503. /*
  504. * Make sure the serial ports are muxed on at this point.
  505. * You have to mux them off in device drivers later on
  506. * if not needed.
  507. */
  508. for (i = 0; i < OMAP_MAX_NR_PORTS; i++) {
  509. struct omap_uart_state *uart = &omap_uart[i];
  510. struct platform_device *pdev = &uart->pdev;
  511. struct device *dev = &pdev->dev;
  512. struct plat_serial8250_port *p = dev->platform_data;
  513. sprintf(name, "uart%d_ick", i+1);
  514. uart->ick = clk_get(NULL, name);
  515. if (IS_ERR(uart->ick)) {
  516. printk(KERN_ERR "Could not get uart%d_ick\n", i+1);
  517. uart->ick = NULL;
  518. }
  519. sprintf(name, "uart%d_fck", i+1);
  520. uart->fck = clk_get(NULL, name);
  521. if (IS_ERR(uart->fck)) {
  522. printk(KERN_ERR "Could not get uart%d_fck\n", i+1);
  523. uart->fck = NULL;
  524. }
  525. /* FIXME: Remove this once the clkdev is ready */
  526. if (!cpu_is_omap44xx()) {
  527. if (!uart->ick || !uart->fck)
  528. continue;
  529. }
  530. uart->num = i;
  531. p->private_data = uart;
  532. uart->p = p;
  533. list_add_tail(&uart->node, &uart_list);
  534. if (cpu_is_omap44xx())
  535. p->irq += 32;
  536. omap_uart_enable_clocks(uart);
  537. }
  538. }
  539. void __init omap_serial_init(void)
  540. {
  541. int i;
  542. for (i = 0; i < OMAP_MAX_NR_PORTS; i++) {
  543. struct omap_uart_state *uart = &omap_uart[i];
  544. struct platform_device *pdev = &uart->pdev;
  545. struct device *dev = &pdev->dev;
  546. omap_uart_reset(uart);
  547. omap_uart_idle_init(uart);
  548. if (WARN_ON(platform_device_register(pdev)))
  549. continue;
  550. if ((cpu_is_omap34xx() && uart->padconf) ||
  551. (uart->wk_en && uart->wk_mask)) {
  552. device_init_wakeup(dev, true);
  553. DEV_CREATE_FILE(dev, &dev_attr_sleep_timeout);
  554. }
  555. }
  556. }