serial.c 15 KB

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