blob: 5f3035ec0d6f00c99be817ad2bff00400150a87b [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 *
Santosh Shilimkar44169072009-05-28 14:16:04 -070013 * Copyright (C) 2009 Texas Instruments
14 * Added OMAP4 support - Santosh Shilimkar <santosh.shilimkar@ti.com
15 *
Tony Lindgren1dbae812005-11-10 14:26:51 +000016 * 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>
Russell Kingf8ce2542006-01-07 16:15:52 +000024#include <linux/clk.h>
Russell Kingfced80c2008-09-06 12:10:45 +010025#include <linux/io.h>
Tony Lindgren1dbae812005-11-10 14:26:51 +000026
Tony Lindgrence491cf2009-10-20 09:40:47 -070027#include <plat/common.h>
28#include <plat/board.h>
29#include <plat/clock.h>
30#include <plat/control.h>
Tony Lindgren1dbae812005-11-10 14:26:51 +000031
Kevin Hilman4af40162009-02-04 10:51:40 -080032#include "prm.h"
33#include "pm.h"
34#include "prm-regbits-34xx.h"
35
vikram panditace13d472009-12-11 16:16:37 -080036#define UART_OMAP_NO_EMPTY_FIFO_READ_IP_REV 0x52
Kevin Hilman4af40162009-02-04 10:51:40 -080037#define UART_OMAP_WER 0x17 /* Wake-up enable register */
38
Tony Lindgren301fe8e2010-02-01 12:34:31 -080039/*
40 * NOTE: By default the serial timeout is disabled as it causes lost characters
41 * over the serial ports. This means that the UART clocks will stay on until
42 * disabled via sysfs. This also causes that any deeper omap sleep states are
43 * blocked.
44 */
45#define DEFAULT_TIMEOUT 0
Kevin Hilman4af40162009-02-04 10:51:40 -080046
47struct omap_uart_state {
48 int num;
49 int can_sleep;
50 struct timer_list timer;
51 u32 timeout;
52
53 void __iomem *wk_st;
54 void __iomem *wk_en;
55 u32 wk_mask;
56 u32 padconf;
57
58 struct clk *ick;
59 struct clk *fck;
60 int clocked;
61
62 struct plat_serial8250_port *p;
63 struct list_head node;
Kevin Hilmanfd455ea2009-04-27 12:27:36 -070064 struct platform_device pdev;
Kevin Hilman4af40162009-02-04 10:51:40 -080065
66#if defined(CONFIG_ARCH_OMAP3) && defined(CONFIG_PM)
67 int context_valid;
68
69 /* Registers to be saved/restored for OFF-mode */
70 u16 dll;
71 u16 dlh;
72 u16 ier;
73 u16 sysc;
74 u16 scr;
75 u16 wer;
76#endif
77};
78
Kevin Hilman4af40162009-02-04 10:51:40 -080079static LIST_HEAD(uart_list);
Tony Lindgren1dbae812005-11-10 14:26:51 +000080
Kevin Hilmanfd455ea2009-04-27 12:27:36 -070081static struct plat_serial8250_port serial_platform_data0[] = {
Tony Lindgren1dbae812005-11-10 14:26:51 +000082 {
Tony Lindgren1dbae812005-11-10 14:26:51 +000083 .irq = 72,
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 }, {
Kevin Hilmanfd455ea2009-04-27 12:27:36 -070089 .flags = 0
90 }
91};
92
93static struct plat_serial8250_port serial_platform_data1[] = {
94 {
Tony Lindgren1dbae812005-11-10 14:26:51 +000095 .irq = 73,
96 .flags = UPF_BOOT_AUTOCONF,
97 .iotype = UPIO_MEM,
98 .regshift = 2,
Jouni Hogander6e811762008-10-06 15:49:15 +030099 .uartclk = OMAP24XX_BASE_BAUD * 16,
Tony Lindgren1dbae812005-11-10 14:26:51 +0000100 }, {
Kevin Hilmanfd455ea2009-04-27 12:27:36 -0700101 .flags = 0
102 }
103};
104
105static struct plat_serial8250_port serial_platform_data2[] = {
106 {
Tony Lindgren1dbae812005-11-10 14:26:51 +0000107 .irq = 74,
108 .flags = UPF_BOOT_AUTOCONF,
109 .iotype = UPIO_MEM,
110 .regshift = 2,
Jouni Hogander6e811762008-10-06 15:49:15 +0300111 .uartclk = OMAP24XX_BASE_BAUD * 16,
Tony Lindgren1dbae812005-11-10 14:26:51 +0000112 }, {
113 .flags = 0
114 }
115};
116
Tony Lindgrena3a9b362010-02-15 09:27:25 -0800117#if defined(CONFIG_ARCH_OMAP3) || defined(CONFIG_ARCH_OMAP4)
Santosh Shilimkar0e3eaad2009-08-22 13:30:11 +0530118static struct plat_serial8250_port serial_platform_data3[] = {
119 {
Santosh Shilimkar0e3eaad2009-08-22 13:30:11 +0530120 .irq = 70,
121 .flags = UPF_BOOT_AUTOCONF,
122 .iotype = UPIO_MEM,
123 .regshift = 2,
124 .uartclk = OMAP24XX_BASE_BAUD * 16,
125 }, {
126 .flags = 0
127 }
128};
Tony Lindgrena3a9b362010-02-15 09:27:25 -0800129
130static inline void omap2_set_globals_uart4(struct omap_globals *omap2_globals)
131{
132 serial_platform_data3[0].mapbase = omap2_globals->uart4_phys;
133}
134#else
135static inline void omap2_set_globals_uart4(struct omap_globals *omap2_globals)
136{
137}
Santosh Shilimkar0e3eaad2009-08-22 13:30:11 +0530138#endif
Tony Lindgren4f2c49f2010-02-15 08:48:53 -0800139
140void __init omap2_set_globals_uart(struct omap_globals *omap2_globals)
141{
142 serial_platform_data0[0].mapbase = omap2_globals->uart1_phys;
143 serial_platform_data1[0].mapbase = omap2_globals->uart2_phys;
144 serial_platform_data2[0].mapbase = omap2_globals->uart3_phys;
Tony Lindgrena3a9b362010-02-15 09:27:25 -0800145 if (cpu_is_omap3630() || cpu_is_omap44xx())
146 omap2_set_globals_uart4(omap2_globals);
Tony Lindgren4f2c49f2010-02-15 08:48:53 -0800147}
148
Alexander Shishkin92303722010-01-08 10:29:06 -0800149static inline unsigned int __serial_read_reg(struct uart_port *up,
150 int offset)
151{
152 offset <<= up->regshift;
153 return (unsigned int)__raw_readb(up->membase + offset);
154}
155
Tony Lindgren1dbae812005-11-10 14:26:51 +0000156static inline unsigned int serial_read_reg(struct plat_serial8250_port *up,
157 int offset)
158{
159 offset <<= up->regshift;
160 return (unsigned int)__raw_readb(up->membase + offset);
161}
162
163static inline void serial_write_reg(struct plat_serial8250_port *p, int offset,
164 int value)
165{
166 offset <<= p->regshift;
Russell Kinge8a91c92008-09-01 22:07:37 +0100167 __raw_writeb(value, p->membase + offset);
Tony Lindgren1dbae812005-11-10 14:26:51 +0000168}
169
170/*
171 * Internal UARTs need to be initialized for the 8250 autoconfig to work
172 * properly. Note that the TX watermark initialization may not be needed
173 * once the 8250.c watermark handling code is merged.
174 */
Kevin Hilman4af40162009-02-04 10:51:40 -0800175static inline void __init omap_uart_reset(struct omap_uart_state *uart)
Tony Lindgren1dbae812005-11-10 14:26:51 +0000176{
Kevin Hilman4af40162009-02-04 10:51:40 -0800177 struct plat_serial8250_port *p = uart->p;
178
Tony Lindgren1dbae812005-11-10 14:26:51 +0000179 serial_write_reg(p, UART_OMAP_MDR1, 0x07);
180 serial_write_reg(p, UART_OMAP_SCR, 0x08);
181 serial_write_reg(p, UART_OMAP_MDR1, 0x00);
Juha Yrjola671c7232006-12-06 17:13:49 -0800182 serial_write_reg(p, UART_OMAP_SYSC, (0x02 << 3) | (1 << 2) | (1 << 0));
Tony Lindgren1dbae812005-11-10 14:26:51 +0000183}
184
Kevin Hilman4af40162009-02-04 10:51:40 -0800185#if defined(CONFIG_PM) && defined(CONFIG_ARCH_OMAP3)
186
Kevin Hilman4af40162009-02-04 10:51:40 -0800187static void omap_uart_save_context(struct omap_uart_state *uart)
Jouni Hogander6e811762008-10-06 15:49:15 +0300188{
Kevin Hilman4af40162009-02-04 10:51:40 -0800189 u16 lcr = 0;
190 struct plat_serial8250_port *p = uart->p;
191
192 if (!enable_off_mode)
193 return;
194
195 lcr = serial_read_reg(p, UART_LCR);
196 serial_write_reg(p, UART_LCR, 0xBF);
197 uart->dll = serial_read_reg(p, UART_DLL);
198 uart->dlh = serial_read_reg(p, UART_DLM);
199 serial_write_reg(p, UART_LCR, lcr);
200 uart->ier = serial_read_reg(p, UART_IER);
201 uart->sysc = serial_read_reg(p, UART_OMAP_SYSC);
202 uart->scr = serial_read_reg(p, UART_OMAP_SCR);
203 uart->wer = serial_read_reg(p, UART_OMAP_WER);
204
205 uart->context_valid = 1;
206}
207
208static void omap_uart_restore_context(struct omap_uart_state *uart)
209{
210 u16 efr = 0;
211 struct plat_serial8250_port *p = uart->p;
212
213 if (!enable_off_mode)
214 return;
215
216 if (!uart->context_valid)
217 return;
218
219 uart->context_valid = 0;
220
221 serial_write_reg(p, UART_OMAP_MDR1, 0x7);
222 serial_write_reg(p, UART_LCR, 0xBF); /* Config B mode */
223 efr = serial_read_reg(p, UART_EFR);
224 serial_write_reg(p, UART_EFR, UART_EFR_ECB);
225 serial_write_reg(p, UART_LCR, 0x0); /* Operational mode */
226 serial_write_reg(p, UART_IER, 0x0);
227 serial_write_reg(p, UART_LCR, 0xBF); /* Config B mode */
228 serial_write_reg(p, UART_DLL, uart->dll);
229 serial_write_reg(p, UART_DLM, uart->dlh);
230 serial_write_reg(p, UART_LCR, 0x0); /* Operational mode */
231 serial_write_reg(p, UART_IER, uart->ier);
232 serial_write_reg(p, UART_FCR, 0xA1);
233 serial_write_reg(p, UART_LCR, 0xBF); /* Config B mode */
234 serial_write_reg(p, UART_EFR, efr);
235 serial_write_reg(p, UART_LCR, UART_LCR_WLEN8);
236 serial_write_reg(p, UART_OMAP_SCR, uart->scr);
237 serial_write_reg(p, UART_OMAP_WER, uart->wer);
238 serial_write_reg(p, UART_OMAP_SYSC, uart->sysc);
239 serial_write_reg(p, UART_OMAP_MDR1, 0x00); /* UART 16x mode */
240}
241#else
242static inline void omap_uart_save_context(struct omap_uart_state *uart) {}
243static inline void omap_uart_restore_context(struct omap_uart_state *uart) {}
244#endif /* CONFIG_PM && CONFIG_ARCH_OMAP3 */
245
246static inline void omap_uart_enable_clocks(struct omap_uart_state *uart)
247{
248 if (uart->clocked)
249 return;
250
251 clk_enable(uart->ick);
252 clk_enable(uart->fck);
253 uart->clocked = 1;
254 omap_uart_restore_context(uart);
255}
256
257#ifdef CONFIG_PM
258
259static inline void omap_uart_disable_clocks(struct omap_uart_state *uart)
260{
261 if (!uart->clocked)
262 return;
263
264 omap_uart_save_context(uart);
265 uart->clocked = 0;
266 clk_disable(uart->ick);
267 clk_disable(uart->fck);
268}
269
Kevin Hilmanfd455ea2009-04-27 12:27:36 -0700270static void omap_uart_enable_wakeup(struct omap_uart_state *uart)
271{
272 /* Set wake-enable bit */
273 if (uart->wk_en && uart->wk_mask) {
274 u32 v = __raw_readl(uart->wk_en);
275 v |= uart->wk_mask;
276 __raw_writel(v, uart->wk_en);
277 }
278
279 /* Ensure IOPAD wake-enables are set */
280 if (cpu_is_omap34xx() && uart->padconf) {
281 u16 v = omap_ctrl_readw(uart->padconf);
282 v |= OMAP3_PADCONF_WAKEUPENABLE0;
283 omap_ctrl_writew(v, uart->padconf);
284 }
285}
286
287static void omap_uart_disable_wakeup(struct omap_uart_state *uart)
288{
289 /* Clear wake-enable bit */
290 if (uart->wk_en && uart->wk_mask) {
291 u32 v = __raw_readl(uart->wk_en);
292 v &= ~uart->wk_mask;
293 __raw_writel(v, uart->wk_en);
294 }
295
296 /* Ensure IOPAD wake-enables are cleared */
297 if (cpu_is_omap34xx() && uart->padconf) {
298 u16 v = omap_ctrl_readw(uart->padconf);
299 v &= ~OMAP3_PADCONF_WAKEUPENABLE0;
300 omap_ctrl_writew(v, uart->padconf);
301 }
302}
303
Kevin Hilman4af40162009-02-04 10:51:40 -0800304static void omap_uart_smart_idle_enable(struct omap_uart_state *uart,
305 int enable)
306{
307 struct plat_serial8250_port *p = uart->p;
308 u16 sysc;
309
310 sysc = serial_read_reg(p, UART_OMAP_SYSC) & 0x7;
311 if (enable)
312 sysc |= 0x2 << 3;
313 else
314 sysc |= 0x1 << 3;
315
316 serial_write_reg(p, UART_OMAP_SYSC, sysc);
317}
318
319static void omap_uart_block_sleep(struct omap_uart_state *uart)
320{
321 omap_uart_enable_clocks(uart);
322
323 omap_uart_smart_idle_enable(uart, 0);
324 uart->can_sleep = 0;
Jouni Hoganderba87a9b2008-12-09 13:36:50 +0200325 if (uart->timeout)
326 mod_timer(&uart->timer, jiffies + uart->timeout);
327 else
328 del_timer(&uart->timer);
Kevin Hilman4af40162009-02-04 10:51:40 -0800329}
330
331static void omap_uart_allow_sleep(struct omap_uart_state *uart)
332{
Kevin Hilmanfd455ea2009-04-27 12:27:36 -0700333 if (device_may_wakeup(&uart->pdev.dev))
334 omap_uart_enable_wakeup(uart);
335 else
336 omap_uart_disable_wakeup(uart);
337
Kevin Hilman4af40162009-02-04 10:51:40 -0800338 if (!uart->clocked)
339 return;
340
341 omap_uart_smart_idle_enable(uart, 1);
342 uart->can_sleep = 1;
343 del_timer(&uart->timer);
344}
345
346static void omap_uart_idle_timer(unsigned long data)
347{
348 struct omap_uart_state *uart = (struct omap_uart_state *)data;
349
350 omap_uart_allow_sleep(uart);
351}
352
353void omap_uart_prepare_idle(int num)
354{
355 struct omap_uart_state *uart;
356
357 list_for_each_entry(uart, &uart_list, node) {
358 if (num == uart->num && uart->can_sleep) {
359 omap_uart_disable_clocks(uart);
360 return;
Jouni Hogander6e811762008-10-06 15:49:15 +0300361 }
362 }
363}
364
Kevin Hilman4af40162009-02-04 10:51:40 -0800365void omap_uart_resume_idle(int num)
366{
367 struct omap_uart_state *uart;
368
369 list_for_each_entry(uart, &uart_list, node) {
370 if (num == uart->num) {
371 omap_uart_enable_clocks(uart);
372
373 /* Check for IO pad wakeup */
374 if (cpu_is_omap34xx() && uart->padconf) {
375 u16 p = omap_ctrl_readw(uart->padconf);
376
377 if (p & OMAP3_PADCONF_WAKEUPEVENT0)
378 omap_uart_block_sleep(uart);
379 }
380
381 /* Check for normal UART wakeup */
382 if (__raw_readl(uart->wk_st) & uart->wk_mask)
383 omap_uart_block_sleep(uart);
Kevin Hilman4af40162009-02-04 10:51:40 -0800384 return;
385 }
386 }
387}
388
389void omap_uart_prepare_suspend(void)
390{
391 struct omap_uart_state *uart;
392
393 list_for_each_entry(uart, &uart_list, node) {
394 omap_uart_allow_sleep(uart);
395 }
396}
397
398int omap_uart_can_sleep(void)
399{
400 struct omap_uart_state *uart;
401 int can_sleep = 1;
402
403 list_for_each_entry(uart, &uart_list, node) {
404 if (!uart->clocked)
405 continue;
406
407 if (!uart->can_sleep) {
408 can_sleep = 0;
409 continue;
410 }
411
412 /* This UART can now safely sleep. */
413 omap_uart_allow_sleep(uart);
414 }
415
416 return can_sleep;
417}
418
419/**
420 * omap_uart_interrupt()
421 *
422 * This handler is used only to detect that *any* UART interrupt has
423 * occurred. It does _nothing_ to handle the interrupt. Rather,
424 * any UART interrupt will trigger the inactivity timer so the
425 * UART will not idle or sleep for its timeout period.
426 *
427 **/
428static irqreturn_t omap_uart_interrupt(int irq, void *dev_id)
429{
430 struct omap_uart_state *uart = dev_id;
431
432 omap_uart_block_sleep(uart);
433
434 return IRQ_NONE;
435}
436
437static void omap_uart_idle_init(struct omap_uart_state *uart)
438{
Kevin Hilman4af40162009-02-04 10:51:40 -0800439 struct plat_serial8250_port *p = uart->p;
440 int ret;
441
442 uart->can_sleep = 0;
Kevin Hilmanfd455ea2009-04-27 12:27:36 -0700443 uart->timeout = DEFAULT_TIMEOUT;
Kevin Hilman4af40162009-02-04 10:51:40 -0800444 setup_timer(&uart->timer, omap_uart_idle_timer,
445 (unsigned long) uart);
Tony Lindgren301fe8e2010-02-01 12:34:31 -0800446 if (uart->timeout)
447 mod_timer(&uart->timer, jiffies + uart->timeout);
Kevin Hilman4af40162009-02-04 10:51:40 -0800448 omap_uart_smart_idle_enable(uart, 0);
449
450 if (cpu_is_omap34xx()) {
451 u32 mod = (uart->num == 2) ? OMAP3430_PER_MOD : CORE_MOD;
452 u32 wk_mask = 0;
453 u32 padconf = 0;
454
455 uart->wk_en = OMAP34XX_PRM_REGADDR(mod, PM_WKEN1);
456 uart->wk_st = OMAP34XX_PRM_REGADDR(mod, PM_WKST1);
457 switch (uart->num) {
458 case 0:
459 wk_mask = OMAP3430_ST_UART1_MASK;
460 padconf = 0x182;
461 break;
462 case 1:
463 wk_mask = OMAP3430_ST_UART2_MASK;
464 padconf = 0x17a;
465 break;
466 case 2:
467 wk_mask = OMAP3430_ST_UART3_MASK;
468 padconf = 0x19e;
469 break;
470 }
471 uart->wk_mask = wk_mask;
472 uart->padconf = padconf;
473 } else if (cpu_is_omap24xx()) {
474 u32 wk_mask = 0;
475
476 if (cpu_is_omap2430()) {
477 uart->wk_en = OMAP2430_PRM_REGADDR(CORE_MOD, PM_WKEN1);
478 uart->wk_st = OMAP2430_PRM_REGADDR(CORE_MOD, PM_WKST1);
479 } else if (cpu_is_omap2420()) {
480 uart->wk_en = OMAP2420_PRM_REGADDR(CORE_MOD, PM_WKEN1);
481 uart->wk_st = OMAP2420_PRM_REGADDR(CORE_MOD, PM_WKST1);
482 }
483 switch (uart->num) {
484 case 0:
485 wk_mask = OMAP24XX_ST_UART1_MASK;
486 break;
487 case 1:
488 wk_mask = OMAP24XX_ST_UART2_MASK;
489 break;
490 case 2:
491 wk_mask = OMAP24XX_ST_UART3_MASK;
492 break;
493 }
494 uart->wk_mask = wk_mask;
495 } else {
496 uart->wk_en = 0;
497 uart->wk_st = 0;
498 uart->wk_mask = 0;
499 uart->padconf = 0;
500 }
501
Vikram Panditac426df82009-08-28 11:24:08 -0700502 p->irqflags |= IRQF_SHARED;
Kevin Hilman4af40162009-02-04 10:51:40 -0800503 ret = request_irq(p->irq, omap_uart_interrupt, IRQF_SHARED,
504 "serial idle", (void *)uart);
505 WARN_ON(ret);
506}
507
Tero Kristo24662112009-03-05 16:32:23 +0200508void omap_uart_enable_irqs(int enable)
Jouni Hoganderba87a9b2008-12-09 13:36:50 +0200509{
Tero Kristo24662112009-03-05 16:32:23 +0200510 int ret;
511 struct omap_uart_state *uart;
512
513 list_for_each_entry(uart, &uart_list, node) {
514 if (enable)
515 ret = request_irq(uart->p->irq, omap_uart_interrupt,
516 IRQF_SHARED, "serial idle", (void *)uart);
517 else
518 free_irq(uart->p->irq, (void *)uart);
519 }
Jouni Hoganderba87a9b2008-12-09 13:36:50 +0200520}
521
Kevin Hilmanfd455ea2009-04-27 12:27:36 -0700522static ssize_t sleep_timeout_show(struct device *dev,
523 struct device_attribute *attr,
Jouni Hoganderba87a9b2008-12-09 13:36:50 +0200524 char *buf)
525{
Kevin Hilmanfd455ea2009-04-27 12:27:36 -0700526 struct platform_device *pdev = container_of(dev,
527 struct platform_device, dev);
528 struct omap_uart_state *uart = container_of(pdev,
529 struct omap_uart_state, pdev);
530
531 return sprintf(buf, "%u\n", uart->timeout / HZ);
Jouni Hoganderba87a9b2008-12-09 13:36:50 +0200532}
533
Kevin Hilmanfd455ea2009-04-27 12:27:36 -0700534static ssize_t sleep_timeout_store(struct device *dev,
535 struct device_attribute *attr,
Jouni Hoganderba87a9b2008-12-09 13:36:50 +0200536 const char *buf, size_t n)
537{
Kevin Hilmanfd455ea2009-04-27 12:27:36 -0700538 struct platform_device *pdev = container_of(dev,
539 struct platform_device, dev);
540 struct omap_uart_state *uart = container_of(pdev,
541 struct omap_uart_state, pdev);
Jouni Hoganderba87a9b2008-12-09 13:36:50 +0200542 unsigned int value;
543
544 if (sscanf(buf, "%u", &value) != 1) {
545 printk(KERN_ERR "sleep_timeout_store: Invalid value\n");
546 return -EINVAL;
547 }
Kevin Hilmanfd455ea2009-04-27 12:27:36 -0700548
549 uart->timeout = value * HZ;
550 if (uart->timeout)
551 mod_timer(&uart->timer, jiffies + uart->timeout);
552 else
553 /* A zero value means disable timeout feature */
554 omap_uart_block_sleep(uart);
555
Jouni Hoganderba87a9b2008-12-09 13:36:50 +0200556 return n;
557}
558
Kevin Hilmanfd455ea2009-04-27 12:27:36 -0700559DEVICE_ATTR(sleep_timeout, 0644, sleep_timeout_show, sleep_timeout_store);
560#define DEV_CREATE_FILE(dev, attr) WARN_ON(device_create_file(dev, attr))
Kevin Hilman4af40162009-02-04 10:51:40 -0800561#else
562static inline void omap_uart_idle_init(struct omap_uart_state *uart) {}
Kevin Hilmanfd455ea2009-04-27 12:27:36 -0700563#define DEV_CREATE_FILE(dev, attr)
Kevin Hilman4af40162009-02-04 10:51:40 -0800564#endif /* CONFIG_PM */
565
Alexander Shishkin9d30b992009-11-22 10:10:47 -0800566static struct omap_uart_state omap_uart[] = {
Kevin Hilmanfd455ea2009-04-27 12:27:36 -0700567 {
568 .pdev = {
569 .name = "serial8250",
570 .id = PLAT8250_DEV_PLATFORM,
571 .dev = {
572 .platform_data = serial_platform_data0,
573 },
574 },
575 }, {
576 .pdev = {
577 .name = "serial8250",
578 .id = PLAT8250_DEV_PLATFORM1,
579 .dev = {
580 .platform_data = serial_platform_data1,
581 },
582 },
583 }, {
584 .pdev = {
585 .name = "serial8250",
586 .id = PLAT8250_DEV_PLATFORM2,
587 .dev = {
588 .platform_data = serial_platform_data2,
589 },
590 },
Vikram Pandita2aa57be2009-05-28 14:03:59 -0700591 },
Tony Lindgrena3a9b362010-02-15 09:27:25 -0800592#if defined(CONFIG_ARCH_OMAP3) || defined(CONFIG_ARCH_OMAP4)
Santosh Shilimkar0e3eaad2009-08-22 13:30:11 +0530593 {
594 .pdev = {
595 .name = "serial8250",
Tony Lindgren61f04ee2009-09-24 16:23:07 -0700596 .id = 3,
Santosh Shilimkar0e3eaad2009-08-22 13:30:11 +0530597 .dev = {
598 .platform_data = serial_platform_data3,
599 },
600 },
601 },
602#endif
Vikram Pandita2aa57be2009-05-28 14:03:59 -0700603};
604
vikram panditace13d472009-12-11 16:16:37 -0800605/*
606 * Override the default 8250 read handler: mem_serial_in()
607 * Empty RX fifo read causes an abort on omap3630 and omap4
608 * This function makes sure that an empty rx fifo is not read on these silicons
609 * (OMAP1/2/3430 are not affected)
610 */
611static unsigned int serial_in_override(struct uart_port *up, int offset)
612{
613 if (UART_RX == offset) {
614 unsigned int lsr;
Alexander Shishkin92303722010-01-08 10:29:06 -0800615 lsr = __serial_read_reg(up, UART_LSR);
vikram panditace13d472009-12-11 16:16:37 -0800616 if (!(lsr & UART_LSR_DR))
617 return -EPERM;
618 }
Alexander Shishkin92303722010-01-08 10:29:06 -0800619
620 return __serial_read_reg(up, offset);
vikram panditace13d472009-12-11 16:16:37 -0800621}
622
Paul Walmsleyb3c6df32009-09-03 20:14:02 +0300623void __init omap_serial_early_init(void)
Tony Lindgren1dbae812005-11-10 14:26:51 +0000624{
Kevin Hilmanfd455ea2009-04-27 12:27:36 -0700625 int i;
Jouni Hogander6e811762008-10-06 15:49:15 +0300626 char name[16];
Tony Lindgren1dbae812005-11-10 14:26:51 +0000627
628 /*
629 * Make sure the serial ports are muxed on at this point.
630 * You have to mux them off in device drivers later on
631 * if not needed.
632 */
633
Alexander Shishkin9d30b992009-11-22 10:10:47 -0800634 for (i = 0; i < ARRAY_SIZE(omap_uart); i++) {
Kevin Hilman4af40162009-02-04 10:51:40 -0800635 struct omap_uart_state *uart = &omap_uart[i];
Kevin Hilmanfd455ea2009-04-27 12:27:36 -0700636 struct platform_device *pdev = &uart->pdev;
637 struct device *dev = &pdev->dev;
638 struct plat_serial8250_port *p = dev->platform_data;
Tony Lindgren1dbae812005-11-10 14:26:51 +0000639
Tony Lindgren84f90c92009-10-16 09:53:00 -0700640 /*
641 * Module 4KB + L4 interconnect 4KB
642 * Static mapping, never released
643 */
644 p->membase = ioremap(p->mapbase, SZ_8K);
645 if (!p->membase) {
646 printk(KERN_ERR "ioremap failed for uart%i\n", i + 1);
647 continue;
648 }
649
Jouni Hogander6e811762008-10-06 15:49:15 +0300650 sprintf(name, "uart%d_ick", i+1);
Kevin Hilman4af40162009-02-04 10:51:40 -0800651 uart->ick = clk_get(NULL, name);
652 if (IS_ERR(uart->ick)) {
Jouni Hogander6e811762008-10-06 15:49:15 +0300653 printk(KERN_ERR "Could not get uart%d_ick\n", i+1);
Kevin Hilman4af40162009-02-04 10:51:40 -0800654 uart->ick = NULL;
655 }
Tony Lindgren1dbae812005-11-10 14:26:51 +0000656
Jouni Hogander6e811762008-10-06 15:49:15 +0300657 sprintf(name, "uart%d_fck", i+1);
Kevin Hilman4af40162009-02-04 10:51:40 -0800658 uart->fck = clk_get(NULL, name);
659 if (IS_ERR(uart->fck)) {
Jouni Hogander6e811762008-10-06 15:49:15 +0300660 printk(KERN_ERR "Could not get uart%d_fck\n", i+1);
Kevin Hilman4af40162009-02-04 10:51:40 -0800661 uart->fck = NULL;
662 }
Tony Lindgren1dbae812005-11-10 14:26:51 +0000663
Santosh Shilimkaraae290f2009-08-22 13:30:12 +0530664 /* FIXME: Remove this once the clkdev is ready */
665 if (!cpu_is_omap44xx()) {
666 if (!uart->ick || !uart->fck)
667 continue;
668 }
Kevin Hilman4af40162009-02-04 10:51:40 -0800669
670 uart->num = i;
671 p->private_data = uart;
672 uart->p = p;
Kevin Hilman4af40162009-02-04 10:51:40 -0800673
Kevin Hilman47899982009-06-24 10:32:03 -0700674 if (cpu_is_omap44xx())
675 p->irq += 32;
Paul Walmsleyb3c6df32009-09-03 20:14:02 +0300676 }
677}
678
Mika Westerbergf62349e2009-12-11 16:16:35 -0800679/**
680 * omap_serial_init_port() - initialize single serial port
681 * @port: serial port number (0-3)
682 *
683 * This function initialies serial driver for given @port only.
684 * Platforms can call this function instead of omap_serial_init()
685 * if they don't plan to use all available UARTs as serial ports.
686 *
687 * Don't mix calls to omap_serial_init_port() and omap_serial_init(),
688 * use only one of the two.
689 */
690void __init omap_serial_init_port(int port)
691{
692 struct omap_uart_state *uart;
693 struct platform_device *pdev;
694 struct device *dev;
695
696 BUG_ON(port < 0);
697 BUG_ON(port >= ARRAY_SIZE(omap_uart));
698
699 uart = &omap_uart[port];
700 pdev = &uart->pdev;
701 dev = &pdev->dev;
702
Mika Westerbergf2eeeae2009-12-14 13:59:18 +0000703 omap_uart_enable_clocks(uart);
704
Mika Westerbergf62349e2009-12-11 16:16:35 -0800705 omap_uart_reset(uart);
706 omap_uart_idle_init(uart);
707
Mika Westerbergf2eeeae2009-12-14 13:59:18 +0000708 list_add_tail(&uart->node, &uart_list);
709
Mika Westerbergf62349e2009-12-11 16:16:35 -0800710 if (WARN_ON(platform_device_register(pdev)))
711 return;
712
713 if ((cpu_is_omap34xx() && uart->padconf) ||
714 (uart->wk_en && uart->wk_mask)) {
715 device_init_wakeup(dev, true);
716 DEV_CREATE_FILE(dev, &dev_attr_sleep_timeout);
717 }
718
vikram pandita30e53bc2010-02-15 10:03:33 -0800719 /*
720 * omap44xx: Never read empty UART fifo
721 * omap3xxx: Never read empty UART fifo on UARTs
722 * with IP rev >=0x52
723 */
724 if (cpu_is_omap44xx())
725 uart->p->serial_in = serial_in_override;
726 else if ((serial_read_reg(uart->p, UART_OMAP_MVER) & 0xFF)
727 >= UART_OMAP_NO_EMPTY_FIFO_READ_IP_REV)
728 uart->p->serial_in = serial_in_override;
Mika Westerbergf62349e2009-12-11 16:16:35 -0800729}
730
731/**
732 * omap_serial_init() - intialize all supported serial ports
733 *
734 * Initializes all available UARTs as serial ports. Platforms
735 * can call this function when they want to have default behaviour
736 * for serial ports (e.g initialize them all as serial ports).
737 */
Paul Walmsleyb3c6df32009-09-03 20:14:02 +0300738void __init omap_serial_init(void)
739{
Tony Lindgrena3a9b362010-02-15 09:27:25 -0800740 int i, nr_ports;
Paul Walmsleyb3c6df32009-09-03 20:14:02 +0300741
Tony Lindgrena3a9b362010-02-15 09:27:25 -0800742 if (!(cpu_is_omap3630() || cpu_is_omap4430()))
743 nr_ports = 3;
744 else
745 nr_ports = ARRAY_SIZE(omap_uart);
746
747 for (i = 0; i < nr_ports; i++)
Mika Westerbergf62349e2009-12-11 16:16:35 -0800748 omap_serial_init_port(i);
Tony Lindgren1dbae812005-11-10 14:26:51 +0000749}