serial.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  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. * This file is subject to the terms and conditions of the GNU General Public
  14. * License. See the file "COPYING" in the main directory of this archive
  15. * for more details.
  16. */
  17. #include <linux/kernel.h>
  18. #include <linux/init.h>
  19. #include <linux/serial_8250.h>
  20. #include <linux/serial_reg.h>
  21. #include <linux/clk.h>
  22. #include <linux/io.h>
  23. #include <mach/common.h>
  24. #include <mach/board.h>
  25. #include <mach/clock.h>
  26. #include <mach/control.h>
  27. #include "prm.h"
  28. #include "pm.h"
  29. #include "prm-regbits-34xx.h"
  30. #define UART_OMAP_WER 0x17 /* Wake-up enable register */
  31. #define DEFAULT_TIMEOUT (2 * HZ)
  32. struct omap_uart_state {
  33. int num;
  34. int can_sleep;
  35. struct timer_list timer;
  36. u32 timeout;
  37. void __iomem *wk_st;
  38. void __iomem *wk_en;
  39. u32 wk_mask;
  40. u32 padconf;
  41. struct clk *ick;
  42. struct clk *fck;
  43. int clocked;
  44. struct plat_serial8250_port *p;
  45. struct list_head node;
  46. #if defined(CONFIG_ARCH_OMAP3) && defined(CONFIG_PM)
  47. int context_valid;
  48. /* Registers to be saved/restored for OFF-mode */
  49. u16 dll;
  50. u16 dlh;
  51. u16 ier;
  52. u16 sysc;
  53. u16 scr;
  54. u16 wer;
  55. #endif
  56. };
  57. static struct omap_uart_state omap_uart[OMAP_MAX_NR_PORTS];
  58. static LIST_HEAD(uart_list);
  59. static struct plat_serial8250_port serial_platform_data[] = {
  60. {
  61. .membase = IO_ADDRESS(OMAP_UART1_BASE),
  62. .mapbase = OMAP_UART1_BASE,
  63. .irq = 72,
  64. .flags = UPF_BOOT_AUTOCONF,
  65. .iotype = UPIO_MEM,
  66. .regshift = 2,
  67. .uartclk = OMAP24XX_BASE_BAUD * 16,
  68. }, {
  69. .membase = IO_ADDRESS(OMAP_UART2_BASE),
  70. .mapbase = OMAP_UART2_BASE,
  71. .irq = 73,
  72. .flags = UPF_BOOT_AUTOCONF,
  73. .iotype = UPIO_MEM,
  74. .regshift = 2,
  75. .uartclk = OMAP24XX_BASE_BAUD * 16,
  76. }, {
  77. .membase = IO_ADDRESS(OMAP_UART3_BASE),
  78. .mapbase = OMAP_UART3_BASE,
  79. .irq = 74,
  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 inline unsigned int serial_read_reg(struct plat_serial8250_port *up,
  89. int offset)
  90. {
  91. offset <<= up->regshift;
  92. return (unsigned int)__raw_readb(up->membase + offset);
  93. }
  94. static inline void serial_write_reg(struct plat_serial8250_port *p, int offset,
  95. int value)
  96. {
  97. offset <<= p->regshift;
  98. __raw_writeb(value, p->membase + offset);
  99. }
  100. /*
  101. * Internal UARTs need to be initialized for the 8250 autoconfig to work
  102. * properly. Note that the TX watermark initialization may not be needed
  103. * once the 8250.c watermark handling code is merged.
  104. */
  105. static inline void __init omap_uart_reset(struct omap_uart_state *uart)
  106. {
  107. struct plat_serial8250_port *p = uart->p;
  108. serial_write_reg(p, UART_OMAP_MDR1, 0x07);
  109. serial_write_reg(p, UART_OMAP_SCR, 0x08);
  110. serial_write_reg(p, UART_OMAP_MDR1, 0x00);
  111. serial_write_reg(p, UART_OMAP_SYSC, (0x02 << 3) | (1 << 2) | (1 << 0));
  112. }
  113. #if defined(CONFIG_PM) && defined(CONFIG_ARCH_OMAP3)
  114. static int enable_off_mode; /* to be removed by full off-mode patches */
  115. static void omap_uart_save_context(struct omap_uart_state *uart)
  116. {
  117. u16 lcr = 0;
  118. struct plat_serial8250_port *p = uart->p;
  119. if (!enable_off_mode)
  120. return;
  121. lcr = serial_read_reg(p, UART_LCR);
  122. serial_write_reg(p, UART_LCR, 0xBF);
  123. uart->dll = serial_read_reg(p, UART_DLL);
  124. uart->dlh = serial_read_reg(p, UART_DLM);
  125. serial_write_reg(p, UART_LCR, lcr);
  126. uart->ier = serial_read_reg(p, UART_IER);
  127. uart->sysc = serial_read_reg(p, UART_OMAP_SYSC);
  128. uart->scr = serial_read_reg(p, UART_OMAP_SCR);
  129. uart->wer = serial_read_reg(p, UART_OMAP_WER);
  130. uart->context_valid = 1;
  131. }
  132. static void omap_uart_restore_context(struct omap_uart_state *uart)
  133. {
  134. u16 efr = 0;
  135. struct plat_serial8250_port *p = uart->p;
  136. if (!enable_off_mode)
  137. return;
  138. if (!uart->context_valid)
  139. return;
  140. uart->context_valid = 0;
  141. serial_write_reg(p, UART_OMAP_MDR1, 0x7);
  142. serial_write_reg(p, UART_LCR, 0xBF); /* Config B mode */
  143. efr = serial_read_reg(p, UART_EFR);
  144. serial_write_reg(p, UART_EFR, UART_EFR_ECB);
  145. serial_write_reg(p, UART_LCR, 0x0); /* Operational mode */
  146. serial_write_reg(p, UART_IER, 0x0);
  147. serial_write_reg(p, UART_LCR, 0xBF); /* Config B mode */
  148. serial_write_reg(p, UART_DLL, uart->dll);
  149. serial_write_reg(p, UART_DLM, uart->dlh);
  150. serial_write_reg(p, UART_LCR, 0x0); /* Operational mode */
  151. serial_write_reg(p, UART_IER, uart->ier);
  152. serial_write_reg(p, UART_FCR, 0xA1);
  153. serial_write_reg(p, UART_LCR, 0xBF); /* Config B mode */
  154. serial_write_reg(p, UART_EFR, efr);
  155. serial_write_reg(p, UART_LCR, UART_LCR_WLEN8);
  156. serial_write_reg(p, UART_OMAP_SCR, uart->scr);
  157. serial_write_reg(p, UART_OMAP_WER, uart->wer);
  158. serial_write_reg(p, UART_OMAP_SYSC, uart->sysc);
  159. serial_write_reg(p, UART_OMAP_MDR1, 0x00); /* UART 16x mode */
  160. }
  161. #else
  162. static inline void omap_uart_save_context(struct omap_uart_state *uart) {}
  163. static inline void omap_uart_restore_context(struct omap_uart_state *uart) {}
  164. #endif /* CONFIG_PM && CONFIG_ARCH_OMAP3 */
  165. static inline void omap_uart_enable_clocks(struct omap_uart_state *uart)
  166. {
  167. if (uart->clocked)
  168. return;
  169. clk_enable(uart->ick);
  170. clk_enable(uart->fck);
  171. uart->clocked = 1;
  172. omap_uart_restore_context(uart);
  173. }
  174. #ifdef CONFIG_PM
  175. static inline void omap_uart_disable_clocks(struct omap_uart_state *uart)
  176. {
  177. if (!uart->clocked)
  178. return;
  179. omap_uart_save_context(uart);
  180. uart->clocked = 0;
  181. clk_disable(uart->ick);
  182. clk_disable(uart->fck);
  183. }
  184. static void omap_uart_smart_idle_enable(struct omap_uart_state *uart,
  185. int enable)
  186. {
  187. struct plat_serial8250_port *p = uart->p;
  188. u16 sysc;
  189. sysc = serial_read_reg(p, UART_OMAP_SYSC) & 0x7;
  190. if (enable)
  191. sysc |= 0x2 << 3;
  192. else
  193. sysc |= 0x1 << 3;
  194. serial_write_reg(p, UART_OMAP_SYSC, sysc);
  195. }
  196. static void omap_uart_block_sleep(struct omap_uart_state *uart)
  197. {
  198. omap_uart_enable_clocks(uart);
  199. omap_uart_smart_idle_enable(uart, 0);
  200. uart->can_sleep = 0;
  201. mod_timer(&uart->timer, jiffies + uart->timeout);
  202. }
  203. static void omap_uart_allow_sleep(struct omap_uart_state *uart)
  204. {
  205. if (!uart->clocked)
  206. return;
  207. omap_uart_smart_idle_enable(uart, 1);
  208. uart->can_sleep = 1;
  209. del_timer(&uart->timer);
  210. }
  211. static void omap_uart_idle_timer(unsigned long data)
  212. {
  213. struct omap_uart_state *uart = (struct omap_uart_state *)data;
  214. omap_uart_allow_sleep(uart);
  215. }
  216. void omap_uart_prepare_idle(int num)
  217. {
  218. struct omap_uart_state *uart;
  219. list_for_each_entry(uart, &uart_list, node) {
  220. if (num == uart->num && uart->can_sleep) {
  221. omap_uart_disable_clocks(uart);
  222. return;
  223. }
  224. }
  225. }
  226. void omap_uart_resume_idle(int num)
  227. {
  228. struct omap_uart_state *uart;
  229. list_for_each_entry(uart, &uart_list, node) {
  230. if (num == uart->num) {
  231. omap_uart_enable_clocks(uart);
  232. /* Check for IO pad wakeup */
  233. if (cpu_is_omap34xx() && uart->padconf) {
  234. u16 p = omap_ctrl_readw(uart->padconf);
  235. if (p & OMAP3_PADCONF_WAKEUPEVENT0)
  236. omap_uart_block_sleep(uart);
  237. }
  238. /* Check for normal UART wakeup */
  239. if (__raw_readl(uart->wk_st) & uart->wk_mask)
  240. omap_uart_block_sleep(uart);
  241. return;
  242. }
  243. }
  244. }
  245. void omap_uart_prepare_suspend(void)
  246. {
  247. struct omap_uart_state *uart;
  248. list_for_each_entry(uart, &uart_list, node) {
  249. omap_uart_allow_sleep(uart);
  250. }
  251. }
  252. int omap_uart_can_sleep(void)
  253. {
  254. struct omap_uart_state *uart;
  255. int can_sleep = 1;
  256. list_for_each_entry(uart, &uart_list, node) {
  257. if (!uart->clocked)
  258. continue;
  259. if (!uart->can_sleep) {
  260. can_sleep = 0;
  261. continue;
  262. }
  263. /* This UART can now safely sleep. */
  264. omap_uart_allow_sleep(uart);
  265. }
  266. return can_sleep;
  267. }
  268. /**
  269. * omap_uart_interrupt()
  270. *
  271. * This handler is used only to detect that *any* UART interrupt has
  272. * occurred. It does _nothing_ to handle the interrupt. Rather,
  273. * any UART interrupt will trigger the inactivity timer so the
  274. * UART will not idle or sleep for its timeout period.
  275. *
  276. **/
  277. static irqreturn_t omap_uart_interrupt(int irq, void *dev_id)
  278. {
  279. struct omap_uart_state *uart = dev_id;
  280. omap_uart_block_sleep(uart);
  281. return IRQ_NONE;
  282. }
  283. static void omap_uart_idle_init(struct omap_uart_state *uart)
  284. {
  285. u32 v;
  286. struct plat_serial8250_port *p = uart->p;
  287. int ret;
  288. uart->can_sleep = 0;
  289. uart->timeout = DEFAULT_TIMEOUT;
  290. setup_timer(&uart->timer, omap_uart_idle_timer,
  291. (unsigned long) uart);
  292. mod_timer(&uart->timer, jiffies + uart->timeout);
  293. omap_uart_smart_idle_enable(uart, 0);
  294. if (cpu_is_omap34xx()) {
  295. u32 mod = (uart->num == 2) ? OMAP3430_PER_MOD : CORE_MOD;
  296. u32 wk_mask = 0;
  297. u32 padconf = 0;
  298. uart->wk_en = OMAP34XX_PRM_REGADDR(mod, PM_WKEN1);
  299. uart->wk_st = OMAP34XX_PRM_REGADDR(mod, PM_WKST1);
  300. switch (uart->num) {
  301. case 0:
  302. wk_mask = OMAP3430_ST_UART1_MASK;
  303. padconf = 0x182;
  304. break;
  305. case 1:
  306. wk_mask = OMAP3430_ST_UART2_MASK;
  307. padconf = 0x17a;
  308. break;
  309. case 2:
  310. wk_mask = OMAP3430_ST_UART3_MASK;
  311. padconf = 0x19e;
  312. break;
  313. }
  314. uart->wk_mask = wk_mask;
  315. uart->padconf = padconf;
  316. } else if (cpu_is_omap24xx()) {
  317. u32 wk_mask = 0;
  318. if (cpu_is_omap2430()) {
  319. uart->wk_en = OMAP2430_PRM_REGADDR(CORE_MOD, PM_WKEN1);
  320. uart->wk_st = OMAP2430_PRM_REGADDR(CORE_MOD, PM_WKST1);
  321. } else if (cpu_is_omap2420()) {
  322. uart->wk_en = OMAP2420_PRM_REGADDR(CORE_MOD, PM_WKEN1);
  323. uart->wk_st = OMAP2420_PRM_REGADDR(CORE_MOD, PM_WKST1);
  324. }
  325. switch (uart->num) {
  326. case 0:
  327. wk_mask = OMAP24XX_ST_UART1_MASK;
  328. break;
  329. case 1:
  330. wk_mask = OMAP24XX_ST_UART2_MASK;
  331. break;
  332. case 2:
  333. wk_mask = OMAP24XX_ST_UART3_MASK;
  334. break;
  335. }
  336. uart->wk_mask = wk_mask;
  337. } else {
  338. uart->wk_en = 0;
  339. uart->wk_st = 0;
  340. uart->wk_mask = 0;
  341. uart->padconf = 0;
  342. }
  343. /* Set wake-enable bit */
  344. if (uart->wk_en && uart->wk_mask) {
  345. v = __raw_readl(uart->wk_en);
  346. v |= uart->wk_mask;
  347. __raw_writel(v, uart->wk_en);
  348. }
  349. /* Ensure IOPAD wake-enables are set */
  350. if (cpu_is_omap34xx() && uart->padconf) {
  351. u16 v;
  352. v = omap_ctrl_readw(uart->padconf);
  353. v |= OMAP3_PADCONF_WAKEUPENABLE0;
  354. omap_ctrl_writew(v, uart->padconf);
  355. }
  356. p->flags |= UPF_SHARE_IRQ;
  357. ret = request_irq(p->irq, omap_uart_interrupt, IRQF_SHARED,
  358. "serial idle", (void *)uart);
  359. WARN_ON(ret);
  360. }
  361. #else
  362. static inline void omap_uart_idle_init(struct omap_uart_state *uart) {}
  363. #endif /* CONFIG_PM */
  364. void __init omap_serial_init(void)
  365. {
  366. int i;
  367. const struct omap_uart_config *info;
  368. char name[16];
  369. /*
  370. * Make sure the serial ports are muxed on at this point.
  371. * You have to mux them off in device drivers later on
  372. * if not needed.
  373. */
  374. info = omap_get_config(OMAP_TAG_UART, struct omap_uart_config);
  375. if (info == NULL)
  376. return;
  377. for (i = 0; i < OMAP_MAX_NR_PORTS; i++) {
  378. struct plat_serial8250_port *p = serial_platform_data + i;
  379. struct omap_uart_state *uart = &omap_uart[i];
  380. if (!(info->enabled_uarts & (1 << i))) {
  381. p->membase = NULL;
  382. p->mapbase = 0;
  383. continue;
  384. }
  385. sprintf(name, "uart%d_ick", i+1);
  386. uart->ick = clk_get(NULL, name);
  387. if (IS_ERR(uart->ick)) {
  388. printk(KERN_ERR "Could not get uart%d_ick\n", i+1);
  389. uart->ick = NULL;
  390. }
  391. sprintf(name, "uart%d_fck", i+1);
  392. uart->fck = clk_get(NULL, name);
  393. if (IS_ERR(uart->fck)) {
  394. printk(KERN_ERR "Could not get uart%d_fck\n", i+1);
  395. uart->fck = NULL;
  396. }
  397. if (!uart->ick || !uart->fck)
  398. continue;
  399. uart->num = i;
  400. p->private_data = uart;
  401. uart->p = p;
  402. list_add(&uart->node, &uart_list);
  403. omap_uart_enable_clocks(uart);
  404. omap_uart_reset(uart);
  405. omap_uart_idle_init(uart);
  406. }
  407. }
  408. static struct platform_device serial_device = {
  409. .name = "serial8250",
  410. .id = PLAT8250_DEV_PLATFORM,
  411. .dev = {
  412. .platform_data = serial_platform_data,
  413. },
  414. };
  415. static int __init omap_init(void)
  416. {
  417. return platform_device_register(&serial_device);
  418. }
  419. arch_initcall(omap_init);