blob: fddbc4e1b2310b5fa35f5bc2709b51f4b54cfbf8 [file] [log] [blame]
Tony Lindgren1dbae812005-11-10 14:26:51 +00001/*
Uwe Zeisbergerf30c2262006-10-03 23:01:26 +02002 * arch/arm/mach-omap2/serial.c
Tony Lindgren1dbae812005-11-10 14:26:51 +00003 *
4 * OMAP2 serial support.
5 *
Jouni Hogander6e811762008-10-06 15:49:15 +03006 * Copyright (C) 2005-2008 Nokia Corporation
Tony Lindgren1dbae812005-11-10 14:26:51 +00007 * Author: Paul Mundt <paul.mundt@nokia.com>
8 *
Kevin Hilman4af40162009-02-04 10:51:40 -08009 * Major rework for PM support by Kevin Hilman
10 *
Tony Lindgren1dbae812005-11-10 14:26:51 +000011 * 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>
Russell Kingf8ce2542006-01-07 16:15:52 +000021#include <linux/clk.h>
Russell Kingfced80c2008-09-06 12:10:45 +010022#include <linux/io.h>
Tony Lindgren1dbae812005-11-10 14:26:51 +000023
Russell Kinga09e64f2008-08-05 16:14:15 +010024#include <mach/common.h>
25#include <mach/board.h>
Kevin Hilman4af40162009-02-04 10:51:40 -080026#include <mach/clock.h>
27#include <mach/control.h>
Tony Lindgren1dbae812005-11-10 14:26:51 +000028
Kevin Hilman4af40162009-02-04 10:51:40 -080029#include "prm.h"
30#include "pm.h"
31#include "prm-regbits-34xx.h"
32
33#define UART_OMAP_WER 0x17 /* Wake-up enable register */
34
Jouni Hoganderba87a9b2008-12-09 13:36:50 +020035#define DEFAULT_TIMEOUT (5 * HZ)
Kevin Hilman4af40162009-02-04 10:51:40 -080036
37struct omap_uart_state {
38 int num;
39 int can_sleep;
40 struct timer_list timer;
41 u32 timeout;
42
43 void __iomem *wk_st;
44 void __iomem *wk_en;
45 u32 wk_mask;
46 u32 padconf;
47
48 struct clk *ick;
49 struct clk *fck;
50 int clocked;
51
52 struct plat_serial8250_port *p;
53 struct list_head node;
54
55#if defined(CONFIG_ARCH_OMAP3) && defined(CONFIG_PM)
56 int context_valid;
57
58 /* Registers to be saved/restored for OFF-mode */
59 u16 dll;
60 u16 dlh;
61 u16 ier;
62 u16 sysc;
63 u16 scr;
64 u16 wer;
65#endif
66};
67
68static struct omap_uart_state omap_uart[OMAP_MAX_NR_PORTS];
69static LIST_HEAD(uart_list);
Tony Lindgren1dbae812005-11-10 14:26:51 +000070
71static struct plat_serial8250_port serial_platform_data[] = {
72 {
Russell Kinge8a91c92008-09-01 22:07:37 +010073 .membase = IO_ADDRESS(OMAP_UART1_BASE),
74 .mapbase = OMAP_UART1_BASE,
Tony Lindgren1dbae812005-11-10 14:26:51 +000075 .irq = 72,
76 .flags = UPF_BOOT_AUTOCONF,
77 .iotype = UPIO_MEM,
78 .regshift = 2,
Jouni Hogander6e811762008-10-06 15:49:15 +030079 .uartclk = OMAP24XX_BASE_BAUD * 16,
Tony Lindgren1dbae812005-11-10 14:26:51 +000080 }, {
Russell Kinge8a91c92008-09-01 22:07:37 +010081 .membase = IO_ADDRESS(OMAP_UART2_BASE),
82 .mapbase = OMAP_UART2_BASE,
Tony Lindgren1dbae812005-11-10 14:26:51 +000083 .irq = 73,
84 .flags = UPF_BOOT_AUTOCONF,
85 .iotype = UPIO_MEM,
86 .regshift = 2,
Jouni Hogander6e811762008-10-06 15:49:15 +030087 .uartclk = OMAP24XX_BASE_BAUD * 16,
Tony Lindgren1dbae812005-11-10 14:26:51 +000088 }, {
Russell Kinge8a91c92008-09-01 22:07:37 +010089 .membase = IO_ADDRESS(OMAP_UART3_BASE),
90 .mapbase = OMAP_UART3_BASE,
Tony Lindgren1dbae812005-11-10 14:26:51 +000091 .irq = 74,
92 .flags = UPF_BOOT_AUTOCONF,
93 .iotype = UPIO_MEM,
94 .regshift = 2,
Jouni Hogander6e811762008-10-06 15:49:15 +030095 .uartclk = OMAP24XX_BASE_BAUD * 16,
Tony Lindgren1dbae812005-11-10 14:26:51 +000096 }, {
97 .flags = 0
98 }
99};
100
101static 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
108static inline void serial_write_reg(struct plat_serial8250_port *p, int offset,
109 int value)
110{
111 offset <<= p->regshift;
Russell Kinge8a91c92008-09-01 22:07:37 +0100112 __raw_writeb(value, p->membase + offset);
Tony Lindgren1dbae812005-11-10 14:26:51 +0000113}
114
115/*
116 * Internal UARTs need to be initialized for the 8250 autoconfig to work
117 * properly. Note that the TX watermark initialization may not be needed
118 * once the 8250.c watermark handling code is merged.
119 */
Kevin Hilman4af40162009-02-04 10:51:40 -0800120static inline void __init omap_uart_reset(struct omap_uart_state *uart)
Tony Lindgren1dbae812005-11-10 14:26:51 +0000121{
Kevin Hilman4af40162009-02-04 10:51:40 -0800122 struct plat_serial8250_port *p = uart->p;
123
Tony Lindgren1dbae812005-11-10 14:26:51 +0000124 serial_write_reg(p, UART_OMAP_MDR1, 0x07);
125 serial_write_reg(p, UART_OMAP_SCR, 0x08);
126 serial_write_reg(p, UART_OMAP_MDR1, 0x00);
Juha Yrjola671c7232006-12-06 17:13:49 -0800127 serial_write_reg(p, UART_OMAP_SYSC, (0x02 << 3) | (1 << 2) | (1 << 0));
Tony Lindgren1dbae812005-11-10 14:26:51 +0000128}
129
Kevin Hilman4af40162009-02-04 10:51:40 -0800130#if defined(CONFIG_PM) && defined(CONFIG_ARCH_OMAP3)
131
132static int enable_off_mode; /* to be removed by full off-mode patches */
133
134static void omap_uart_save_context(struct omap_uart_state *uart)
Jouni Hogander6e811762008-10-06 15:49:15 +0300135{
Kevin Hilman4af40162009-02-04 10:51:40 -0800136 u16 lcr = 0;
137 struct plat_serial8250_port *p = uart->p;
138
139 if (!enable_off_mode)
140 return;
141
142 lcr = serial_read_reg(p, UART_LCR);
143 serial_write_reg(p, UART_LCR, 0xBF);
144 uart->dll = serial_read_reg(p, UART_DLL);
145 uart->dlh = serial_read_reg(p, UART_DLM);
146 serial_write_reg(p, UART_LCR, lcr);
147 uart->ier = serial_read_reg(p, UART_IER);
148 uart->sysc = serial_read_reg(p, UART_OMAP_SYSC);
149 uart->scr = serial_read_reg(p, UART_OMAP_SCR);
150 uart->wer = serial_read_reg(p, UART_OMAP_WER);
151
152 uart->context_valid = 1;
153}
154
155static void omap_uart_restore_context(struct omap_uart_state *uart)
156{
157 u16 efr = 0;
158 struct plat_serial8250_port *p = uart->p;
159
160 if (!enable_off_mode)
161 return;
162
163 if (!uart->context_valid)
164 return;
165
166 uart->context_valid = 0;
167
168 serial_write_reg(p, UART_OMAP_MDR1, 0x7);
169 serial_write_reg(p, UART_LCR, 0xBF); /* Config B mode */
170 efr = serial_read_reg(p, UART_EFR);
171 serial_write_reg(p, UART_EFR, UART_EFR_ECB);
172 serial_write_reg(p, UART_LCR, 0x0); /* Operational mode */
173 serial_write_reg(p, UART_IER, 0x0);
174 serial_write_reg(p, UART_LCR, 0xBF); /* Config B mode */
175 serial_write_reg(p, UART_DLL, uart->dll);
176 serial_write_reg(p, UART_DLM, uart->dlh);
177 serial_write_reg(p, UART_LCR, 0x0); /* Operational mode */
178 serial_write_reg(p, UART_IER, uart->ier);
179 serial_write_reg(p, UART_FCR, 0xA1);
180 serial_write_reg(p, UART_LCR, 0xBF); /* Config B mode */
181 serial_write_reg(p, UART_EFR, efr);
182 serial_write_reg(p, UART_LCR, UART_LCR_WLEN8);
183 serial_write_reg(p, UART_OMAP_SCR, uart->scr);
184 serial_write_reg(p, UART_OMAP_WER, uart->wer);
185 serial_write_reg(p, UART_OMAP_SYSC, uart->sysc);
186 serial_write_reg(p, UART_OMAP_MDR1, 0x00); /* UART 16x mode */
187}
188#else
189static inline void omap_uart_save_context(struct omap_uart_state *uart) {}
190static inline void omap_uart_restore_context(struct omap_uart_state *uart) {}
191#endif /* CONFIG_PM && CONFIG_ARCH_OMAP3 */
192
193static inline void omap_uart_enable_clocks(struct omap_uart_state *uart)
194{
195 if (uart->clocked)
196 return;
197
198 clk_enable(uart->ick);
199 clk_enable(uart->fck);
200 uart->clocked = 1;
201 omap_uart_restore_context(uart);
202}
203
204#ifdef CONFIG_PM
205
206static inline void omap_uart_disable_clocks(struct omap_uart_state *uart)
207{
208 if (!uart->clocked)
209 return;
210
211 omap_uart_save_context(uart);
212 uart->clocked = 0;
213 clk_disable(uart->ick);
214 clk_disable(uart->fck);
215}
216
217static void omap_uart_smart_idle_enable(struct omap_uart_state *uart,
218 int enable)
219{
220 struct plat_serial8250_port *p = uart->p;
221 u16 sysc;
222
223 sysc = serial_read_reg(p, UART_OMAP_SYSC) & 0x7;
224 if (enable)
225 sysc |= 0x2 << 3;
226 else
227 sysc |= 0x1 << 3;
228
229 serial_write_reg(p, UART_OMAP_SYSC, sysc);
230}
231
232static void omap_uart_block_sleep(struct omap_uart_state *uart)
233{
234 omap_uart_enable_clocks(uart);
235
236 omap_uart_smart_idle_enable(uart, 0);
237 uart->can_sleep = 0;
Jouni Hoganderba87a9b2008-12-09 13:36:50 +0200238 if (uart->timeout)
239 mod_timer(&uart->timer, jiffies + uart->timeout);
240 else
241 del_timer(&uart->timer);
Kevin Hilman4af40162009-02-04 10:51:40 -0800242}
243
244static void omap_uart_allow_sleep(struct omap_uart_state *uart)
245{
246 if (!uart->clocked)
247 return;
248
249 omap_uart_smart_idle_enable(uart, 1);
250 uart->can_sleep = 1;
251 del_timer(&uart->timer);
252}
253
254static void omap_uart_idle_timer(unsigned long data)
255{
256 struct omap_uart_state *uart = (struct omap_uart_state *)data;
257
258 omap_uart_allow_sleep(uart);
259}
260
261void omap_uart_prepare_idle(int num)
262{
263 struct omap_uart_state *uart;
264
265 list_for_each_entry(uart, &uart_list, node) {
266 if (num == uart->num && uart->can_sleep) {
267 omap_uart_disable_clocks(uart);
268 return;
Jouni Hogander6e811762008-10-06 15:49:15 +0300269 }
270 }
271}
272
Kevin Hilman4af40162009-02-04 10:51:40 -0800273void omap_uart_resume_idle(int num)
274{
275 struct omap_uart_state *uart;
276
277 list_for_each_entry(uart, &uart_list, node) {
278 if (num == uart->num) {
279 omap_uart_enable_clocks(uart);
280
281 /* Check for IO pad wakeup */
282 if (cpu_is_omap34xx() && uart->padconf) {
283 u16 p = omap_ctrl_readw(uart->padconf);
284
285 if (p & OMAP3_PADCONF_WAKEUPEVENT0)
286 omap_uart_block_sleep(uart);
287 }
288
289 /* Check for normal UART wakeup */
290 if (__raw_readl(uart->wk_st) & uart->wk_mask)
291 omap_uart_block_sleep(uart);
292
293 return;
294 }
295 }
296}
297
298void omap_uart_prepare_suspend(void)
299{
300 struct omap_uart_state *uart;
301
302 list_for_each_entry(uart, &uart_list, node) {
303 omap_uart_allow_sleep(uart);
304 }
305}
306
307int omap_uart_can_sleep(void)
308{
309 struct omap_uart_state *uart;
310 int can_sleep = 1;
311
312 list_for_each_entry(uart, &uart_list, node) {
313 if (!uart->clocked)
314 continue;
315
316 if (!uart->can_sleep) {
317 can_sleep = 0;
318 continue;
319 }
320
321 /* This UART can now safely sleep. */
322 omap_uart_allow_sleep(uart);
323 }
324
325 return can_sleep;
326}
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 **/
337static irqreturn_t omap_uart_interrupt(int irq, void *dev_id)
338{
339 struct omap_uart_state *uart = dev_id;
340
341 omap_uart_block_sleep(uart);
342
343 return IRQ_NONE;
344}
345
Jouni Hoganderba87a9b2008-12-09 13:36:50 +0200346static u32 sleep_timeout = DEFAULT_TIMEOUT;
347
Kevin Hilman4af40162009-02-04 10:51:40 -0800348static void omap_uart_idle_init(struct omap_uart_state *uart)
349{
350 u32 v;
351 struct plat_serial8250_port *p = uart->p;
352 int ret;
353
354 uart->can_sleep = 0;
Jouni Hoganderba87a9b2008-12-09 13:36:50 +0200355 uart->timeout = sleep_timeout;
Kevin Hilman4af40162009-02-04 10:51:40 -0800356 setup_timer(&uart->timer, omap_uart_idle_timer,
357 (unsigned long) uart);
358 mod_timer(&uart->timer, jiffies + uart->timeout);
359 omap_uart_smart_idle_enable(uart, 0);
360
361 if (cpu_is_omap34xx()) {
362 u32 mod = (uart->num == 2) ? OMAP3430_PER_MOD : CORE_MOD;
363 u32 wk_mask = 0;
364 u32 padconf = 0;
365
366 uart->wk_en = OMAP34XX_PRM_REGADDR(mod, PM_WKEN1);
367 uart->wk_st = OMAP34XX_PRM_REGADDR(mod, PM_WKST1);
368 switch (uart->num) {
369 case 0:
370 wk_mask = OMAP3430_ST_UART1_MASK;
371 padconf = 0x182;
372 break;
373 case 1:
374 wk_mask = OMAP3430_ST_UART2_MASK;
375 padconf = 0x17a;
376 break;
377 case 2:
378 wk_mask = OMAP3430_ST_UART3_MASK;
379 padconf = 0x19e;
380 break;
381 }
382 uart->wk_mask = wk_mask;
383 uart->padconf = padconf;
384 } else if (cpu_is_omap24xx()) {
385 u32 wk_mask = 0;
386
387 if (cpu_is_omap2430()) {
388 uart->wk_en = OMAP2430_PRM_REGADDR(CORE_MOD, PM_WKEN1);
389 uart->wk_st = OMAP2430_PRM_REGADDR(CORE_MOD, PM_WKST1);
390 } else if (cpu_is_omap2420()) {
391 uart->wk_en = OMAP2420_PRM_REGADDR(CORE_MOD, PM_WKEN1);
392 uart->wk_st = OMAP2420_PRM_REGADDR(CORE_MOD, PM_WKST1);
393 }
394 switch (uart->num) {
395 case 0:
396 wk_mask = OMAP24XX_ST_UART1_MASK;
397 break;
398 case 1:
399 wk_mask = OMAP24XX_ST_UART2_MASK;
400 break;
401 case 2:
402 wk_mask = OMAP24XX_ST_UART3_MASK;
403 break;
404 }
405 uart->wk_mask = wk_mask;
406 } else {
407 uart->wk_en = 0;
408 uart->wk_st = 0;
409 uart->wk_mask = 0;
410 uart->padconf = 0;
411 }
412
413 /* Set wake-enable bit */
414 if (uart->wk_en && uart->wk_mask) {
415 v = __raw_readl(uart->wk_en);
416 v |= uart->wk_mask;
417 __raw_writel(v, uart->wk_en);
418 }
419
420 /* Ensure IOPAD wake-enables are set */
421 if (cpu_is_omap34xx() && uart->padconf) {
422 u16 v;
423
424 v = omap_ctrl_readw(uart->padconf);
425 v |= OMAP3_PADCONF_WAKEUPENABLE0;
426 omap_ctrl_writew(v, uart->padconf);
427 }
428
429 p->flags |= UPF_SHARE_IRQ;
430 ret = request_irq(p->irq, omap_uart_interrupt, IRQF_SHARED,
431 "serial idle", (void *)uart);
432 WARN_ON(ret);
433}
434
Jouni Hoganderba87a9b2008-12-09 13:36:50 +0200435static ssize_t sleep_timeout_show(struct kobject *kobj,
436 struct kobj_attribute *attr,
437 char *buf)
438{
439 return sprintf(buf, "%u\n", sleep_timeout / HZ);
440}
441
442static ssize_t sleep_timeout_store(struct kobject *kobj,
443 struct kobj_attribute *attr,
444 const char *buf, size_t n)
445{
446 struct omap_uart_state *uart;
447 unsigned int value;
448
449 if (sscanf(buf, "%u", &value) != 1) {
450 printk(KERN_ERR "sleep_timeout_store: Invalid value\n");
451 return -EINVAL;
452 }
453 sleep_timeout = value * HZ;
454 list_for_each_entry(uart, &uart_list, node) {
455 uart->timeout = sleep_timeout;
456 if (uart->timeout)
457 mod_timer(&uart->timer, jiffies + uart->timeout);
458 else
459 /* A zero value means disable timeout feature */
460 omap_uart_block_sleep(uart);
461 }
462 return n;
463}
464
465static struct kobj_attribute sleep_timeout_attr =
466 __ATTR(sleep_timeout, 0644, sleep_timeout_show, sleep_timeout_store);
467
Kevin Hilman4af40162009-02-04 10:51:40 -0800468#else
469static inline void omap_uart_idle_init(struct omap_uart_state *uart) {}
470#endif /* CONFIG_PM */
471
Vikram Pandita2aa57be2009-05-28 14:03:59 -0700472static struct platform_device serial_device = {
473 .name = "serial8250",
474 .id = PLAT8250_DEV_PLATFORM,
475 .dev = {
476 .platform_data = serial_platform_data,
477 },
478};
479
Jouni Hogander6e811762008-10-06 15:49:15 +0300480void __init omap_serial_init(void)
Tony Lindgren1dbae812005-11-10 14:26:51 +0000481{
Tony Lindgren970a7242009-05-28 15:44:54 -0700482 int i, err;
Tony Lindgren1dbae812005-11-10 14:26:51 +0000483 const struct omap_uart_config *info;
Jouni Hogander6e811762008-10-06 15:49:15 +0300484 char name[16];
Tony Lindgren1dbae812005-11-10 14:26:51 +0000485
486 /*
487 * Make sure the serial ports are muxed on at this point.
488 * You have to mux them off in device drivers later on
489 * if not needed.
490 */
491
Jouni Hogander6e811762008-10-06 15:49:15 +0300492 info = omap_get_config(OMAP_TAG_UART, struct omap_uart_config);
Tony Lindgren1dbae812005-11-10 14:26:51 +0000493
494 if (info == NULL)
495 return;
496
497 for (i = 0; i < OMAP_MAX_NR_PORTS; i++) {
498 struct plat_serial8250_port *p = serial_platform_data + i;
Kevin Hilman4af40162009-02-04 10:51:40 -0800499 struct omap_uart_state *uart = &omap_uart[i];
Tony Lindgren1dbae812005-11-10 14:26:51 +0000500
501 if (!(info->enabled_uarts & (1 << i))) {
Russell Kingc0fc18c2008-09-05 15:10:27 +0100502 p->membase = NULL;
Tony Lindgren1dbae812005-11-10 14:26:51 +0000503 p->mapbase = 0;
504 continue;
505 }
506
Jouni Hogander6e811762008-10-06 15:49:15 +0300507 sprintf(name, "uart%d_ick", i+1);
Kevin Hilman4af40162009-02-04 10:51:40 -0800508 uart->ick = clk_get(NULL, name);
509 if (IS_ERR(uart->ick)) {
Jouni Hogander6e811762008-10-06 15:49:15 +0300510 printk(KERN_ERR "Could not get uart%d_ick\n", i+1);
Kevin Hilman4af40162009-02-04 10:51:40 -0800511 uart->ick = NULL;
512 }
Tony Lindgren1dbae812005-11-10 14:26:51 +0000513
Jouni Hogander6e811762008-10-06 15:49:15 +0300514 sprintf(name, "uart%d_fck", i+1);
Kevin Hilman4af40162009-02-04 10:51:40 -0800515 uart->fck = clk_get(NULL, name);
516 if (IS_ERR(uart->fck)) {
Jouni Hogander6e811762008-10-06 15:49:15 +0300517 printk(KERN_ERR "Could not get uart%d_fck\n", i+1);
Kevin Hilman4af40162009-02-04 10:51:40 -0800518 uart->fck = NULL;
519 }
Tony Lindgren1dbae812005-11-10 14:26:51 +0000520
Kevin Hilman4af40162009-02-04 10:51:40 -0800521 if (!uart->ick || !uart->fck)
522 continue;
523
524 uart->num = i;
525 p->private_data = uart;
526 uart->p = p;
527 list_add(&uart->node, &uart_list);
528
529 omap_uart_enable_clocks(uart);
530 omap_uart_reset(uart);
531 omap_uart_idle_init(uart);
Tony Lindgren1dbae812005-11-10 14:26:51 +0000532 }
Tony Lindgren1dbae812005-11-10 14:26:51 +0000533
Tony Lindgren970a7242009-05-28 15:44:54 -0700534 err = platform_device_register(&serial_device);
Jouni Hoganderba87a9b2008-12-09 13:36:50 +0200535
536#ifdef CONFIG_PM
Tony Lindgren970a7242009-05-28 15:44:54 -0700537 if (!err)
538 err = sysfs_create_file(&serial_device.dev.kobj,
Jouni Hoganderba87a9b2008-12-09 13:36:50 +0200539 &sleep_timeout_attr.attr);
540#endif
Tony Lindgren970a7242009-05-28 15:44:54 -0700541
Tony Lindgren1dbae812005-11-10 14:26:51 +0000542}
Tony Lindgren970a7242009-05-28 15:44:54 -0700543