blob: 3771254dfa811a45efda31a7afc7a0cc48ce86d5 [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>
Santosh Shilimkare03d37d2010-02-18 08:59:06 +000026#include <linux/delay.h>
Tony Lindgren1dbae812005-11-10 14:26:51 +000027
Tony Lindgrence491cf2009-10-20 09:40:47 -070028#include <plat/common.h>
29#include <plat/board.h>
30#include <plat/clock.h>
31#include <plat/control.h>
Tony Lindgren1dbae812005-11-10 14:26:51 +000032
Kevin Hilman4af40162009-02-04 10:51:40 -080033#include "prm.h"
34#include "pm.h"
35#include "prm-regbits-34xx.h"
36
vikram panditace13d472009-12-11 16:16:37 -080037#define UART_OMAP_NO_EMPTY_FIFO_READ_IP_REV 0x52
Kevin Hilman4af40162009-02-04 10:51:40 -080038#define UART_OMAP_WER 0x17 /* Wake-up enable register */
39
Tony Lindgren301fe8e2010-02-01 12:34:31 -080040/*
41 * NOTE: By default the serial timeout is disabled as it causes lost characters
42 * over the serial ports. This means that the UART clocks will stay on until
43 * disabled via sysfs. This also causes that any deeper omap sleep states are
44 * blocked.
45 */
46#define DEFAULT_TIMEOUT 0
Kevin Hilman4af40162009-02-04 10:51:40 -080047
48struct omap_uart_state {
49 int num;
50 int can_sleep;
51 struct timer_list timer;
52 u32 timeout;
53
54 void __iomem *wk_st;
55 void __iomem *wk_en;
56 u32 wk_mask;
57 u32 padconf;
58
59 struct clk *ick;
60 struct clk *fck;
61 int clocked;
62
63 struct plat_serial8250_port *p;
64 struct list_head node;
Kevin Hilmanfd455ea2009-04-27 12:27:36 -070065 struct platform_device pdev;
Kevin Hilman4af40162009-02-04 10:51:40 -080066
67#if defined(CONFIG_ARCH_OMAP3) && defined(CONFIG_PM)
68 int context_valid;
69
70 /* Registers to be saved/restored for OFF-mode */
71 u16 dll;
72 u16 dlh;
73 u16 ier;
74 u16 sysc;
75 u16 scr;
76 u16 wer;
77#endif
78};
79
Kevin Hilman4af40162009-02-04 10:51:40 -080080static LIST_HEAD(uart_list);
Tony Lindgren1dbae812005-11-10 14:26:51 +000081
Kevin Hilmanfd455ea2009-04-27 12:27:36 -070082static struct plat_serial8250_port serial_platform_data0[] = {
Tony Lindgren1dbae812005-11-10 14:26:51 +000083 {
Tony Lindgren1dbae812005-11-10 14:26:51 +000084 .irq = 72,
85 .flags = UPF_BOOT_AUTOCONF,
86 .iotype = UPIO_MEM,
87 .regshift = 2,
Jouni Hogander6e811762008-10-06 15:49:15 +030088 .uartclk = OMAP24XX_BASE_BAUD * 16,
Tony Lindgren1dbae812005-11-10 14:26:51 +000089 }, {
Kevin Hilmanfd455ea2009-04-27 12:27:36 -070090 .flags = 0
91 }
92};
93
94static struct plat_serial8250_port serial_platform_data1[] = {
95 {
Tony Lindgren1dbae812005-11-10 14:26:51 +000096 .irq = 73,
97 .flags = UPF_BOOT_AUTOCONF,
98 .iotype = UPIO_MEM,
99 .regshift = 2,
Jouni Hogander6e811762008-10-06 15:49:15 +0300100 .uartclk = OMAP24XX_BASE_BAUD * 16,
Tony Lindgren1dbae812005-11-10 14:26:51 +0000101 }, {
Kevin Hilmanfd455ea2009-04-27 12:27:36 -0700102 .flags = 0
103 }
104};
105
106static struct plat_serial8250_port serial_platform_data2[] = {
107 {
Tony Lindgren1dbae812005-11-10 14:26:51 +0000108 .irq = 74,
109 .flags = UPF_BOOT_AUTOCONF,
110 .iotype = UPIO_MEM,
111 .regshift = 2,
Jouni Hogander6e811762008-10-06 15:49:15 +0300112 .uartclk = OMAP24XX_BASE_BAUD * 16,
Tony Lindgren1dbae812005-11-10 14:26:51 +0000113 }, {
114 .flags = 0
115 }
116};
117
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
Tony Lindgren4f2c49f2010-02-15 08:48:53 -0800130void __init omap2_set_globals_uart(struct omap_globals *omap2_globals)
131{
132 serial_platform_data0[0].mapbase = omap2_globals->uart1_phys;
133 serial_platform_data1[0].mapbase = omap2_globals->uart2_phys;
134 serial_platform_data2[0].mapbase = omap2_globals->uart3_phys;
Sergio Aguirre4b1bbd32010-02-26 19:58:47 -0600135 serial_platform_data3[0].mapbase = omap2_globals->uart4_phys;
Tony Lindgren4f2c49f2010-02-15 08:48:53 -0800136}
137
Alexander Shishkin92303722010-01-08 10:29:06 -0800138static inline unsigned int __serial_read_reg(struct uart_port *up,
139 int offset)
140{
141 offset <<= up->regshift;
142 return (unsigned int)__raw_readb(up->membase + offset);
143}
144
Tony Lindgren1dbae812005-11-10 14:26:51 +0000145static inline unsigned int serial_read_reg(struct plat_serial8250_port *up,
146 int offset)
147{
148 offset <<= up->regshift;
149 return (unsigned int)__raw_readb(up->membase + offset);
150}
151
Santosh Shilimkare03d37d2010-02-18 08:59:06 +0000152static inline void __serial_write_reg(struct uart_port *up, int offset,
153 int value)
154{
155 offset <<= up->regshift;
156 __raw_writeb(value, up->membase + offset);
157}
158
Tony Lindgren1dbae812005-11-10 14:26:51 +0000159static inline void serial_write_reg(struct plat_serial8250_port *p, int offset,
160 int value)
161{
162 offset <<= p->regshift;
Russell Kinge8a91c92008-09-01 22:07:37 +0100163 __raw_writeb(value, p->membase + offset);
Tony Lindgren1dbae812005-11-10 14:26:51 +0000164}
165
166/*
167 * Internal UARTs need to be initialized for the 8250 autoconfig to work
168 * properly. Note that the TX watermark initialization may not be needed
169 * once the 8250.c watermark handling code is merged.
170 */
Kevin Hilman4af40162009-02-04 10:51:40 -0800171static inline void __init omap_uart_reset(struct omap_uart_state *uart)
Tony Lindgren1dbae812005-11-10 14:26:51 +0000172{
Kevin Hilman4af40162009-02-04 10:51:40 -0800173 struct plat_serial8250_port *p = uart->p;
174
Tony Lindgren1dbae812005-11-10 14:26:51 +0000175 serial_write_reg(p, UART_OMAP_MDR1, 0x07);
176 serial_write_reg(p, UART_OMAP_SCR, 0x08);
177 serial_write_reg(p, UART_OMAP_MDR1, 0x00);
Juha Yrjola671c7232006-12-06 17:13:49 -0800178 serial_write_reg(p, UART_OMAP_SYSC, (0x02 << 3) | (1 << 2) | (1 << 0));
Tony Lindgren1dbae812005-11-10 14:26:51 +0000179}
180
Kevin Hilman4af40162009-02-04 10:51:40 -0800181#if defined(CONFIG_PM) && defined(CONFIG_ARCH_OMAP3)
182
Kevin Hilman4af40162009-02-04 10:51:40 -0800183static void omap_uart_save_context(struct omap_uart_state *uart)
Jouni Hogander6e811762008-10-06 15:49:15 +0300184{
Kevin Hilman4af40162009-02-04 10:51:40 -0800185 u16 lcr = 0;
186 struct plat_serial8250_port *p = uart->p;
187
188 if (!enable_off_mode)
189 return;
190
191 lcr = serial_read_reg(p, UART_LCR);
192 serial_write_reg(p, UART_LCR, 0xBF);
193 uart->dll = serial_read_reg(p, UART_DLL);
194 uart->dlh = serial_read_reg(p, UART_DLM);
195 serial_write_reg(p, UART_LCR, lcr);
196 uart->ier = serial_read_reg(p, UART_IER);
197 uart->sysc = serial_read_reg(p, UART_OMAP_SYSC);
198 uart->scr = serial_read_reg(p, UART_OMAP_SCR);
199 uart->wer = serial_read_reg(p, UART_OMAP_WER);
200
201 uart->context_valid = 1;
202}
203
204static void omap_uart_restore_context(struct omap_uart_state *uart)
205{
206 u16 efr = 0;
207 struct plat_serial8250_port *p = uart->p;
208
209 if (!enable_off_mode)
210 return;
211
212 if (!uart->context_valid)
213 return;
214
215 uart->context_valid = 0;
216
217 serial_write_reg(p, UART_OMAP_MDR1, 0x7);
218 serial_write_reg(p, UART_LCR, 0xBF); /* Config B mode */
219 efr = serial_read_reg(p, UART_EFR);
220 serial_write_reg(p, UART_EFR, UART_EFR_ECB);
221 serial_write_reg(p, UART_LCR, 0x0); /* Operational mode */
222 serial_write_reg(p, UART_IER, 0x0);
223 serial_write_reg(p, UART_LCR, 0xBF); /* Config B mode */
224 serial_write_reg(p, UART_DLL, uart->dll);
225 serial_write_reg(p, UART_DLM, uart->dlh);
226 serial_write_reg(p, UART_LCR, 0x0); /* Operational mode */
227 serial_write_reg(p, UART_IER, uart->ier);
228 serial_write_reg(p, UART_FCR, 0xA1);
229 serial_write_reg(p, UART_LCR, 0xBF); /* Config B mode */
230 serial_write_reg(p, UART_EFR, efr);
231 serial_write_reg(p, UART_LCR, UART_LCR_WLEN8);
232 serial_write_reg(p, UART_OMAP_SCR, uart->scr);
233 serial_write_reg(p, UART_OMAP_WER, uart->wer);
234 serial_write_reg(p, UART_OMAP_SYSC, uart->sysc);
235 serial_write_reg(p, UART_OMAP_MDR1, 0x00); /* UART 16x mode */
236}
237#else
238static inline void omap_uart_save_context(struct omap_uart_state *uart) {}
239static inline void omap_uart_restore_context(struct omap_uart_state *uart) {}
240#endif /* CONFIG_PM && CONFIG_ARCH_OMAP3 */
241
242static inline void omap_uart_enable_clocks(struct omap_uart_state *uart)
243{
244 if (uart->clocked)
245 return;
246
247 clk_enable(uart->ick);
248 clk_enable(uart->fck);
249 uart->clocked = 1;
250 omap_uart_restore_context(uart);
251}
252
253#ifdef CONFIG_PM
254
255static inline void omap_uart_disable_clocks(struct omap_uart_state *uart)
256{
257 if (!uart->clocked)
258 return;
259
260 omap_uart_save_context(uart);
261 uart->clocked = 0;
262 clk_disable(uart->ick);
263 clk_disable(uart->fck);
264}
265
Kevin Hilmanfd455ea2009-04-27 12:27:36 -0700266static void omap_uart_enable_wakeup(struct omap_uart_state *uart)
267{
268 /* Set wake-enable bit */
269 if (uart->wk_en && uart->wk_mask) {
270 u32 v = __raw_readl(uart->wk_en);
271 v |= uart->wk_mask;
272 __raw_writel(v, uart->wk_en);
273 }
274
275 /* Ensure IOPAD wake-enables are set */
276 if (cpu_is_omap34xx() && uart->padconf) {
277 u16 v = omap_ctrl_readw(uart->padconf);
278 v |= OMAP3_PADCONF_WAKEUPENABLE0;
279 omap_ctrl_writew(v, uart->padconf);
280 }
281}
282
283static void omap_uart_disable_wakeup(struct omap_uart_state *uart)
284{
285 /* Clear wake-enable bit */
286 if (uart->wk_en && uart->wk_mask) {
287 u32 v = __raw_readl(uart->wk_en);
288 v &= ~uart->wk_mask;
289 __raw_writel(v, uart->wk_en);
290 }
291
292 /* Ensure IOPAD wake-enables are cleared */
293 if (cpu_is_omap34xx() && uart->padconf) {
294 u16 v = omap_ctrl_readw(uart->padconf);
295 v &= ~OMAP3_PADCONF_WAKEUPENABLE0;
296 omap_ctrl_writew(v, uart->padconf);
297 }
298}
299
Kevin Hilman4af40162009-02-04 10:51:40 -0800300static void omap_uart_smart_idle_enable(struct omap_uart_state *uart,
301 int enable)
302{
303 struct plat_serial8250_port *p = uart->p;
304 u16 sysc;
305
306 sysc = serial_read_reg(p, UART_OMAP_SYSC) & 0x7;
307 if (enable)
308 sysc |= 0x2 << 3;
309 else
310 sysc |= 0x1 << 3;
311
312 serial_write_reg(p, UART_OMAP_SYSC, sysc);
313}
314
315static void omap_uart_block_sleep(struct omap_uart_state *uart)
316{
317 omap_uart_enable_clocks(uart);
318
319 omap_uart_smart_idle_enable(uart, 0);
320 uart->can_sleep = 0;
Jouni Hoganderba87a9b2008-12-09 13:36:50 +0200321 if (uart->timeout)
322 mod_timer(&uart->timer, jiffies + uart->timeout);
323 else
324 del_timer(&uart->timer);
Kevin Hilman4af40162009-02-04 10:51:40 -0800325}
326
327static void omap_uart_allow_sleep(struct omap_uart_state *uart)
328{
Kevin Hilmanfd455ea2009-04-27 12:27:36 -0700329 if (device_may_wakeup(&uart->pdev.dev))
330 omap_uart_enable_wakeup(uart);
331 else
332 omap_uart_disable_wakeup(uart);
333
Kevin Hilman4af40162009-02-04 10:51:40 -0800334 if (!uart->clocked)
335 return;
336
337 omap_uart_smart_idle_enable(uart, 1);
338 uart->can_sleep = 1;
339 del_timer(&uart->timer);
340}
341
342static void omap_uart_idle_timer(unsigned long data)
343{
344 struct omap_uart_state *uart = (struct omap_uart_state *)data;
345
346 omap_uart_allow_sleep(uart);
347}
348
349void omap_uart_prepare_idle(int num)
350{
351 struct omap_uart_state *uart;
352
353 list_for_each_entry(uart, &uart_list, node) {
354 if (num == uart->num && uart->can_sleep) {
355 omap_uart_disable_clocks(uart);
356 return;
Jouni Hogander6e811762008-10-06 15:49:15 +0300357 }
358 }
359}
360
Kevin Hilman4af40162009-02-04 10:51:40 -0800361void omap_uart_resume_idle(int num)
362{
363 struct omap_uart_state *uart;
364
365 list_for_each_entry(uart, &uart_list, node) {
366 if (num == uart->num) {
367 omap_uart_enable_clocks(uart);
368
369 /* Check for IO pad wakeup */
370 if (cpu_is_omap34xx() && uart->padconf) {
371 u16 p = omap_ctrl_readw(uart->padconf);
372
373 if (p & OMAP3_PADCONF_WAKEUPEVENT0)
374 omap_uart_block_sleep(uart);
375 }
376
377 /* Check for normal UART wakeup */
378 if (__raw_readl(uart->wk_st) & uart->wk_mask)
379 omap_uart_block_sleep(uart);
Kevin Hilman4af40162009-02-04 10:51:40 -0800380 return;
381 }
382 }
383}
384
385void omap_uart_prepare_suspend(void)
386{
387 struct omap_uart_state *uart;
388
389 list_for_each_entry(uart, &uart_list, node) {
390 omap_uart_allow_sleep(uart);
391 }
392}
393
394int omap_uart_can_sleep(void)
395{
396 struct omap_uart_state *uart;
397 int can_sleep = 1;
398
399 list_for_each_entry(uart, &uart_list, node) {
400 if (!uart->clocked)
401 continue;
402
403 if (!uart->can_sleep) {
404 can_sleep = 0;
405 continue;
406 }
407
408 /* This UART can now safely sleep. */
409 omap_uart_allow_sleep(uart);
410 }
411
412 return can_sleep;
413}
414
415/**
416 * omap_uart_interrupt()
417 *
418 * This handler is used only to detect that *any* UART interrupt has
419 * occurred. It does _nothing_ to handle the interrupt. Rather,
420 * any UART interrupt will trigger the inactivity timer so the
421 * UART will not idle or sleep for its timeout period.
422 *
423 **/
424static irqreturn_t omap_uart_interrupt(int irq, void *dev_id)
425{
426 struct omap_uart_state *uart = dev_id;
427
428 omap_uart_block_sleep(uart);
429
430 return IRQ_NONE;
431}
432
433static void omap_uart_idle_init(struct omap_uart_state *uart)
434{
Kevin Hilman4af40162009-02-04 10:51:40 -0800435 struct plat_serial8250_port *p = uart->p;
436 int ret;
437
438 uart->can_sleep = 0;
Kevin Hilmanfd455ea2009-04-27 12:27:36 -0700439 uart->timeout = DEFAULT_TIMEOUT;
Kevin Hilman4af40162009-02-04 10:51:40 -0800440 setup_timer(&uart->timer, omap_uart_idle_timer,
441 (unsigned long) uart);
Tony Lindgren301fe8e2010-02-01 12:34:31 -0800442 if (uart->timeout)
443 mod_timer(&uart->timer, jiffies + uart->timeout);
Kevin Hilman4af40162009-02-04 10:51:40 -0800444 omap_uart_smart_idle_enable(uart, 0);
445
446 if (cpu_is_omap34xx()) {
447 u32 mod = (uart->num == 2) ? OMAP3430_PER_MOD : CORE_MOD;
448 u32 wk_mask = 0;
449 u32 padconf = 0;
450
451 uart->wk_en = OMAP34XX_PRM_REGADDR(mod, PM_WKEN1);
452 uart->wk_st = OMAP34XX_PRM_REGADDR(mod, PM_WKST1);
453 switch (uart->num) {
454 case 0:
455 wk_mask = OMAP3430_ST_UART1_MASK;
456 padconf = 0x182;
457 break;
458 case 1:
459 wk_mask = OMAP3430_ST_UART2_MASK;
460 padconf = 0x17a;
461 break;
462 case 2:
463 wk_mask = OMAP3430_ST_UART3_MASK;
464 padconf = 0x19e;
465 break;
466 }
467 uart->wk_mask = wk_mask;
468 uart->padconf = padconf;
469 } else if (cpu_is_omap24xx()) {
470 u32 wk_mask = 0;
471
472 if (cpu_is_omap2430()) {
473 uart->wk_en = OMAP2430_PRM_REGADDR(CORE_MOD, PM_WKEN1);
474 uart->wk_st = OMAP2430_PRM_REGADDR(CORE_MOD, PM_WKST1);
475 } else if (cpu_is_omap2420()) {
476 uart->wk_en = OMAP2420_PRM_REGADDR(CORE_MOD, PM_WKEN1);
477 uart->wk_st = OMAP2420_PRM_REGADDR(CORE_MOD, PM_WKST1);
478 }
479 switch (uart->num) {
480 case 0:
481 wk_mask = OMAP24XX_ST_UART1_MASK;
482 break;
483 case 1:
484 wk_mask = OMAP24XX_ST_UART2_MASK;
485 break;
486 case 2:
487 wk_mask = OMAP24XX_ST_UART3_MASK;
488 break;
489 }
490 uart->wk_mask = wk_mask;
491 } else {
492 uart->wk_en = 0;
493 uart->wk_st = 0;
494 uart->wk_mask = 0;
495 uart->padconf = 0;
496 }
497
Vikram Panditac426df82009-08-28 11:24:08 -0700498 p->irqflags |= IRQF_SHARED;
Kevin Hilman4af40162009-02-04 10:51:40 -0800499 ret = request_irq(p->irq, omap_uart_interrupt, IRQF_SHARED,
500 "serial idle", (void *)uart);
501 WARN_ON(ret);
502}
503
Tero Kristo24662112009-03-05 16:32:23 +0200504void omap_uart_enable_irqs(int enable)
Jouni Hoganderba87a9b2008-12-09 13:36:50 +0200505{
Tero Kristo24662112009-03-05 16:32:23 +0200506 int ret;
507 struct omap_uart_state *uart;
508
509 list_for_each_entry(uart, &uart_list, node) {
510 if (enable)
511 ret = request_irq(uart->p->irq, omap_uart_interrupt,
512 IRQF_SHARED, "serial idle", (void *)uart);
513 else
514 free_irq(uart->p->irq, (void *)uart);
515 }
Jouni Hoganderba87a9b2008-12-09 13:36:50 +0200516}
517
Kevin Hilmanfd455ea2009-04-27 12:27:36 -0700518static ssize_t sleep_timeout_show(struct device *dev,
519 struct device_attribute *attr,
Jouni Hoganderba87a9b2008-12-09 13:36:50 +0200520 char *buf)
521{
Kevin Hilmanfd455ea2009-04-27 12:27:36 -0700522 struct platform_device *pdev = container_of(dev,
523 struct platform_device, dev);
524 struct omap_uart_state *uart = container_of(pdev,
525 struct omap_uart_state, pdev);
526
527 return sprintf(buf, "%u\n", uart->timeout / HZ);
Jouni Hoganderba87a9b2008-12-09 13:36:50 +0200528}
529
Kevin Hilmanfd455ea2009-04-27 12:27:36 -0700530static ssize_t sleep_timeout_store(struct device *dev,
531 struct device_attribute *attr,
Jouni Hoganderba87a9b2008-12-09 13:36:50 +0200532 const char *buf, size_t n)
533{
Kevin Hilmanfd455ea2009-04-27 12:27:36 -0700534 struct platform_device *pdev = container_of(dev,
535 struct platform_device, dev);
536 struct omap_uart_state *uart = container_of(pdev,
537 struct omap_uart_state, pdev);
Jouni Hoganderba87a9b2008-12-09 13:36:50 +0200538 unsigned int value;
539
540 if (sscanf(buf, "%u", &value) != 1) {
Sergio Aguirre10c805e2010-03-09 13:22:14 -0600541 dev_err(dev, "sleep_timeout_store: Invalid value\n");
Jouni Hoganderba87a9b2008-12-09 13:36:50 +0200542 return -EINVAL;
543 }
Kevin Hilmanfd455ea2009-04-27 12:27:36 -0700544
545 uart->timeout = value * HZ;
546 if (uart->timeout)
547 mod_timer(&uart->timer, jiffies + uart->timeout);
548 else
549 /* A zero value means disable timeout feature */
550 omap_uart_block_sleep(uart);
551
Jouni Hoganderba87a9b2008-12-09 13:36:50 +0200552 return n;
553}
554
Kevin Hilmanfd455ea2009-04-27 12:27:36 -0700555DEVICE_ATTR(sleep_timeout, 0644, sleep_timeout_show, sleep_timeout_store);
556#define DEV_CREATE_FILE(dev, attr) WARN_ON(device_create_file(dev, attr))
Kevin Hilman4af40162009-02-04 10:51:40 -0800557#else
558static inline void omap_uart_idle_init(struct omap_uart_state *uart) {}
Kevin Hilmanfd455ea2009-04-27 12:27:36 -0700559#define DEV_CREATE_FILE(dev, attr)
Kevin Hilman4af40162009-02-04 10:51:40 -0800560#endif /* CONFIG_PM */
561
Alexander Shishkin9d30b992009-11-22 10:10:47 -0800562static struct omap_uart_state omap_uart[] = {
Kevin Hilmanfd455ea2009-04-27 12:27:36 -0700563 {
564 .pdev = {
565 .name = "serial8250",
566 .id = PLAT8250_DEV_PLATFORM,
567 .dev = {
568 .platform_data = serial_platform_data0,
569 },
570 },
571 }, {
572 .pdev = {
573 .name = "serial8250",
574 .id = PLAT8250_DEV_PLATFORM1,
575 .dev = {
576 .platform_data = serial_platform_data1,
577 },
578 },
579 }, {
580 .pdev = {
581 .name = "serial8250",
582 .id = PLAT8250_DEV_PLATFORM2,
583 .dev = {
584 .platform_data = serial_platform_data2,
585 },
586 },
Vikram Pandita2aa57be2009-05-28 14:03:59 -0700587 },
Tony Lindgrena3a9b362010-02-15 09:27:25 -0800588#if defined(CONFIG_ARCH_OMAP3) || defined(CONFIG_ARCH_OMAP4)
Santosh Shilimkar0e3eaad2009-08-22 13:30:11 +0530589 {
590 .pdev = {
591 .name = "serial8250",
Tony Lindgren61f04ee2009-09-24 16:23:07 -0700592 .id = 3,
Santosh Shilimkar0e3eaad2009-08-22 13:30:11 +0530593 .dev = {
594 .platform_data = serial_platform_data3,
595 },
596 },
597 },
598#endif
Vikram Pandita2aa57be2009-05-28 14:03:59 -0700599};
600
vikram panditace13d472009-12-11 16:16:37 -0800601/*
602 * Override the default 8250 read handler: mem_serial_in()
603 * Empty RX fifo read causes an abort on omap3630 and omap4
604 * This function makes sure that an empty rx fifo is not read on these silicons
605 * (OMAP1/2/3430 are not affected)
606 */
607static unsigned int serial_in_override(struct uart_port *up, int offset)
608{
609 if (UART_RX == offset) {
610 unsigned int lsr;
Alexander Shishkin92303722010-01-08 10:29:06 -0800611 lsr = __serial_read_reg(up, UART_LSR);
vikram panditace13d472009-12-11 16:16:37 -0800612 if (!(lsr & UART_LSR_DR))
613 return -EPERM;
614 }
Alexander Shishkin92303722010-01-08 10:29:06 -0800615
616 return __serial_read_reg(up, offset);
vikram panditace13d472009-12-11 16:16:37 -0800617}
618
Santosh Shilimkare03d37d2010-02-18 08:59:06 +0000619static void serial_out_override(struct uart_port *up, int offset, int value)
620{
621 unsigned int status, tmout = 10000;
622
623 status = __serial_read_reg(up, UART_LSR);
624 while (!(status & UART_LSR_THRE)) {
625 /* Wait up to 10ms for the character(s) to be sent. */
626 if (--tmout == 0)
627 break;
628 udelay(1);
629 status = __serial_read_reg(up, UART_LSR);
630 }
631 __serial_write_reg(up, offset, value);
632}
Paul Walmsleyb3c6df32009-09-03 20:14:02 +0300633void __init omap_serial_early_init(void)
Tony Lindgren1dbae812005-11-10 14:26:51 +0000634{
Thomas Weber21b90342010-02-25 09:40:19 +0000635 int i, nr_ports;
Jouni Hogander6e811762008-10-06 15:49:15 +0300636 char name[16];
Tony Lindgren1dbae812005-11-10 14:26:51 +0000637
Thomas Weber21b90342010-02-25 09:40:19 +0000638 if (!(cpu_is_omap3630() || cpu_is_omap4430()))
639 nr_ports = 3;
640 else
641 nr_ports = ARRAY_SIZE(omap_uart);
642
Tony Lindgren1dbae812005-11-10 14:26:51 +0000643 /*
644 * Make sure the serial ports are muxed on at this point.
645 * You have to mux them off in device drivers later on
646 * if not needed.
647 */
648
Thomas Weber21b90342010-02-25 09:40:19 +0000649 for (i = 0; i < nr_ports; i++) {
Kevin Hilman4af40162009-02-04 10:51:40 -0800650 struct omap_uart_state *uart = &omap_uart[i];
Kevin Hilmanfd455ea2009-04-27 12:27:36 -0700651 struct platform_device *pdev = &uart->pdev;
652 struct device *dev = &pdev->dev;
653 struct plat_serial8250_port *p = dev->platform_data;
Tony Lindgren1dbae812005-11-10 14:26:51 +0000654
Sergio Aguirree88d5562010-02-27 14:13:43 -0600655 /* Don't map zero-based physical address */
656 if (p->mapbase == 0) {
Sergio Aguirre10c805e2010-03-09 13:22:14 -0600657 dev_warn(dev, "no physical address for uart#%d,"
658 " so skipping early_init...\n", i);
Sergio Aguirree88d5562010-02-27 14:13:43 -0600659 continue;
660 }
Tony Lindgren84f90c92009-10-16 09:53:00 -0700661 /*
662 * Module 4KB + L4 interconnect 4KB
663 * Static mapping, never released
664 */
665 p->membase = ioremap(p->mapbase, SZ_8K);
666 if (!p->membase) {
Sergio Aguirre10c805e2010-03-09 13:22:14 -0600667 dev_err(dev, "ioremap failed for uart%i\n", i + 1);
Tony Lindgren84f90c92009-10-16 09:53:00 -0700668 continue;
669 }
670
Thomas Weber21b90342010-02-25 09:40:19 +0000671 sprintf(name, "uart%d_ick", i + 1);
Kevin Hilman4af40162009-02-04 10:51:40 -0800672 uart->ick = clk_get(NULL, name);
673 if (IS_ERR(uart->ick)) {
Sergio Aguirre10c805e2010-03-09 13:22:14 -0600674 dev_err(dev, "Could not get uart%d_ick\n", i + 1);
Kevin Hilman4af40162009-02-04 10:51:40 -0800675 uart->ick = NULL;
676 }
Tony Lindgren1dbae812005-11-10 14:26:51 +0000677
Jouni Hogander6e811762008-10-06 15:49:15 +0300678 sprintf(name, "uart%d_fck", i+1);
Kevin Hilman4af40162009-02-04 10:51:40 -0800679 uart->fck = clk_get(NULL, name);
680 if (IS_ERR(uart->fck)) {
Sergio Aguirre10c805e2010-03-09 13:22:14 -0600681 dev_err(dev, "Could not get uart%d_fck\n", i + 1);
Kevin Hilman4af40162009-02-04 10:51:40 -0800682 uart->fck = NULL;
683 }
Tony Lindgren1dbae812005-11-10 14:26:51 +0000684
Santosh Shilimkaraae290f2009-08-22 13:30:12 +0530685 /* FIXME: Remove this once the clkdev is ready */
686 if (!cpu_is_omap44xx()) {
687 if (!uart->ick || !uart->fck)
688 continue;
689 }
Kevin Hilman4af40162009-02-04 10:51:40 -0800690
691 uart->num = i;
692 p->private_data = uart;
693 uart->p = p;
Kevin Hilman4af40162009-02-04 10:51:40 -0800694
Kevin Hilman47899982009-06-24 10:32:03 -0700695 if (cpu_is_omap44xx())
696 p->irq += 32;
Paul Walmsleyb3c6df32009-09-03 20:14:02 +0300697 }
698}
699
Mika Westerbergf62349e2009-12-11 16:16:35 -0800700/**
701 * omap_serial_init_port() - initialize single serial port
702 * @port: serial port number (0-3)
703 *
704 * This function initialies serial driver for given @port only.
705 * Platforms can call this function instead of omap_serial_init()
706 * if they don't plan to use all available UARTs as serial ports.
707 *
708 * Don't mix calls to omap_serial_init_port() and omap_serial_init(),
709 * use only one of the two.
710 */
711void __init omap_serial_init_port(int port)
712{
713 struct omap_uart_state *uart;
714 struct platform_device *pdev;
715 struct device *dev;
716
717 BUG_ON(port < 0);
718 BUG_ON(port >= ARRAY_SIZE(omap_uart));
719
720 uart = &omap_uart[port];
721 pdev = &uart->pdev;
722 dev = &pdev->dev;
723
Sergio Aguirree88d5562010-02-27 14:13:43 -0600724 /* Don't proceed if there's no clocks available */
725 if (unlikely(!uart->ick || !uart->fck)) {
726 WARN(1, "%s: can't init uart%d, no clocks available\n",
727 kobject_name(&dev->kobj), port);
728 return;
729 }
730
Mika Westerbergf2eeeae2009-12-14 13:59:18 +0000731 omap_uart_enable_clocks(uart);
732
Mika Westerbergf62349e2009-12-11 16:16:35 -0800733 omap_uart_reset(uart);
734 omap_uart_idle_init(uart);
735
Mika Westerbergf2eeeae2009-12-14 13:59:18 +0000736 list_add_tail(&uart->node, &uart_list);
737
Mika Westerbergf62349e2009-12-11 16:16:35 -0800738 if (WARN_ON(platform_device_register(pdev)))
739 return;
740
741 if ((cpu_is_omap34xx() && uart->padconf) ||
742 (uart->wk_en && uart->wk_mask)) {
743 device_init_wakeup(dev, true);
744 DEV_CREATE_FILE(dev, &dev_attr_sleep_timeout);
745 }
746
vikram pandita30e53bc2010-02-15 10:03:33 -0800747 /*
748 * omap44xx: Never read empty UART fifo
749 * omap3xxx: Never read empty UART fifo on UARTs
750 * with IP rev >=0x52
751 */
Santosh Shilimkare03d37d2010-02-18 08:59:06 +0000752 if (cpu_is_omap44xx()) {
vikram pandita30e53bc2010-02-15 10:03:33 -0800753 uart->p->serial_in = serial_in_override;
Santosh Shilimkare03d37d2010-02-18 08:59:06 +0000754 uart->p->serial_out = serial_out_override;
755 } else if ((serial_read_reg(uart->p, UART_OMAP_MVER) & 0xFF)
756 >= UART_OMAP_NO_EMPTY_FIFO_READ_IP_REV) {
vikram pandita30e53bc2010-02-15 10:03:33 -0800757 uart->p->serial_in = serial_in_override;
Santosh Shilimkare03d37d2010-02-18 08:59:06 +0000758 uart->p->serial_out = serial_out_override;
759 }
Mika Westerbergf62349e2009-12-11 16:16:35 -0800760}
761
762/**
763 * omap_serial_init() - intialize all supported serial ports
764 *
765 * Initializes all available UARTs as serial ports. Platforms
766 * can call this function when they want to have default behaviour
767 * for serial ports (e.g initialize them all as serial ports).
768 */
Paul Walmsleyb3c6df32009-09-03 20:14:02 +0300769void __init omap_serial_init(void)
770{
Tony Lindgrena3a9b362010-02-15 09:27:25 -0800771 int i, nr_ports;
Paul Walmsleyb3c6df32009-09-03 20:14:02 +0300772
Tony Lindgrena3a9b362010-02-15 09:27:25 -0800773 if (!(cpu_is_omap3630() || cpu_is_omap4430()))
774 nr_ports = 3;
775 else
776 nr_ports = ARRAY_SIZE(omap_uart);
777
778 for (i = 0; i < nr_ports; i++)
Mika Westerbergf62349e2009-12-11 16:16:35 -0800779 omap_serial_init_port(i);
Tony Lindgren1dbae812005-11-10 14:26:51 +0000780}