serial.c 14 KB

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