blob: 6ffbc924c4d41b09a912502beca981fc83900825 [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>
Tony Lindgren1dbae812005-11-10 14:26:51 +000022#include <linux/serial_reg.h>
Russell Kingf8ce2542006-01-07 16:15:52 +000023#include <linux/clk.h>
Russell Kingfced80c2008-09-06 12:10:45 +010024#include <linux/io.h>
Santosh Shilimkare03d37d2010-02-18 08:59:06 +000025#include <linux/delay.h>
Kevin Hilman6f251e92010-09-27 20:19:38 +053026#include <linux/platform_device.h>
27#include <linux/slab.h>
28#include <linux/serial_8250.h>
29
30#ifdef CONFIG_SERIAL_OMAP
31#include <plat/omap-serial.h>
32#endif
Tony Lindgren1dbae812005-11-10 14:26:51 +000033
Tony Lindgrence491cf2009-10-20 09:40:47 -070034#include <plat/common.h>
35#include <plat/board.h>
36#include <plat/clock.h>
37#include <plat/control.h>
Kevin Hilman6f251e92010-09-27 20:19:38 +053038#include <plat/dma.h>
39#include <plat/omap_hwmod.h>
40#include <plat/omap_device.h>
Tony Lindgren1dbae812005-11-10 14:26:51 +000041
Kevin Hilman4af40162009-02-04 10:51:40 -080042#include "prm.h"
43#include "pm.h"
Kevin Hilman6f251e92010-09-27 20:19:38 +053044#include "cm.h"
Kevin Hilman4af40162009-02-04 10:51:40 -080045#include "prm-regbits-34xx.h"
46
vikram panditace13d472009-12-11 16:16:37 -080047#define UART_OMAP_NO_EMPTY_FIFO_READ_IP_REV 0x52
Kevin Hilman4af40162009-02-04 10:51:40 -080048#define UART_OMAP_WER 0x17 /* Wake-up enable register */
49
Nishanth Menon5a927b32010-08-02 13:18:12 +030050#define UART_ERRATA_FIFO_FULL_ABORT (0x1 << 0)
Deepak K00034502010-08-02 13:18:12 +030051#define UART_ERRATA_i202_MDR1_ACCESS (0x1 << 1)
Nishanth Menon5a927b32010-08-02 13:18:12 +030052
Tony Lindgren301fe8e2010-02-01 12:34:31 -080053/*
54 * NOTE: By default the serial timeout is disabled as it causes lost characters
55 * over the serial ports. This means that the UART clocks will stay on until
56 * disabled via sysfs. This also causes that any deeper omap sleep states are
57 * blocked.
58 */
59#define DEFAULT_TIMEOUT 0
Kevin Hilman4af40162009-02-04 10:51:40 -080060
Kevin Hilman6f251e92010-09-27 20:19:38 +053061#define MAX_UART_HWMOD_NAME_LEN 16
62
Kevin Hilman4af40162009-02-04 10:51:40 -080063struct omap_uart_state {
64 int num;
65 int can_sleep;
66 struct timer_list timer;
67 u32 timeout;
68
69 void __iomem *wk_st;
70 void __iomem *wk_en;
71 u32 wk_mask;
72 u32 padconf;
Kevin Hilman6f251e92010-09-27 20:19:38 +053073 u32 dma_enabled;
Kevin Hilman4af40162009-02-04 10:51:40 -080074
75 struct clk *ick;
76 struct clk *fck;
77 int clocked;
78
Kevin Hilman6f251e92010-09-27 20:19:38 +053079 int irq;
80 int regshift;
81 int irqflags;
82 void __iomem *membase;
83 resource_size_t mapbase;
84
Kevin Hilman4af40162009-02-04 10:51:40 -080085 struct list_head node;
Kevin Hilman6f251e92010-09-27 20:19:38 +053086 struct omap_hwmod *oh;
87 struct platform_device *pdev;
Kevin Hilman4af40162009-02-04 10:51:40 -080088
Nishanth Menon5a927b32010-08-02 13:18:12 +030089 u32 errata;
Kevin Hilman4af40162009-02-04 10:51:40 -080090#if defined(CONFIG_ARCH_OMAP3) && defined(CONFIG_PM)
91 int context_valid;
92
93 /* Registers to be saved/restored for OFF-mode */
94 u16 dll;
95 u16 dlh;
96 u16 ier;
97 u16 sysc;
98 u16 scr;
99 u16 wer;
Govindraj R5ade4ff2010-08-02 13:18:11 +0300100 u16 mcr;
Kevin Hilman4af40162009-02-04 10:51:40 -0800101#endif
102};
103
Kevin Hilman4af40162009-02-04 10:51:40 -0800104static LIST_HEAD(uart_list);
Kevin Hilman6f251e92010-09-27 20:19:38 +0530105static u8 num_uarts;
Tony Lindgren1dbae812005-11-10 14:26:51 +0000106
Kevin Hilman6f251e92010-09-27 20:19:38 +0530107static struct omap_device_pm_latency omap_uart_latency[] = {
108 {
109 .deactivate_func = omap_device_idle_hwmods,
110 .activate_func = omap_device_enable_hwmods,
111 .flags = OMAP_DEVICE_LATENCY_AUTO_ADJUST,
112 },
113};
114
Alexander Shishkin92303722010-01-08 10:29:06 -0800115static inline unsigned int __serial_read_reg(struct uart_port *up,
Kevin Hilman6f251e92010-09-27 20:19:38 +0530116 int offset)
Alexander Shishkin92303722010-01-08 10:29:06 -0800117{
118 offset <<= up->regshift;
119 return (unsigned int)__raw_readb(up->membase + offset);
120}
121
Kevin Hilman6f251e92010-09-27 20:19:38 +0530122static inline unsigned int serial_read_reg(struct omap_uart_state *uart,
Tony Lindgren1dbae812005-11-10 14:26:51 +0000123 int offset)
124{
Kevin Hilman6f251e92010-09-27 20:19:38 +0530125 offset <<= uart->regshift;
126 return (unsigned int)__raw_readb(uart->membase + offset);
Tony Lindgren1dbae812005-11-10 14:26:51 +0000127}
128
Santosh Shilimkare03d37d2010-02-18 08:59:06 +0000129static inline void __serial_write_reg(struct uart_port *up, int offset,
130 int value)
131{
132 offset <<= up->regshift;
133 __raw_writeb(value, up->membase + offset);
134}
135
Kevin Hilman6f251e92010-09-27 20:19:38 +0530136static inline void serial_write_reg(struct omap_uart_state *uart, int offset,
Tony Lindgren1dbae812005-11-10 14:26:51 +0000137 int value)
138{
Kevin Hilman6f251e92010-09-27 20:19:38 +0530139 offset <<= uart->regshift;
140 __raw_writeb(value, uart->membase + offset);
Tony Lindgren1dbae812005-11-10 14:26:51 +0000141}
142
143/*
144 * Internal UARTs need to be initialized for the 8250 autoconfig to work
145 * properly. Note that the TX watermark initialization may not be needed
146 * once the 8250.c watermark handling code is merged.
147 */
Kevin Hilman6f251e92010-09-27 20:19:38 +0530148
Kevin Hilman4af40162009-02-04 10:51:40 -0800149static inline void __init omap_uart_reset(struct omap_uart_state *uart)
Tony Lindgren1dbae812005-11-10 14:26:51 +0000150{
Kevin Hilman6f251e92010-09-27 20:19:38 +0530151 serial_write_reg(uart, UART_OMAP_MDR1, 0x07);
152 serial_write_reg(uart, UART_OMAP_SCR, 0x08);
153 serial_write_reg(uart, UART_OMAP_MDR1, 0x00);
Tony Lindgren1dbae812005-11-10 14:26:51 +0000154}
155
Kevin Hilman4af40162009-02-04 10:51:40 -0800156#if defined(CONFIG_PM) && defined(CONFIG_ARCH_OMAP3)
157
Deepak K00034502010-08-02 13:18:12 +0300158/*
159 * Work Around for Errata i202 (3430 - 1.12, 3630 - 1.6)
160 * The access to uart register after MDR1 Access
161 * causes UART to corrupt data.
162 *
163 * Need a delay =
164 * 5 L4 clock cycles + 5 UART functional clock cycle (@48MHz = ~0.2uS)
165 * give 10 times as much
166 */
167static void omap_uart_mdr1_errataset(struct omap_uart_state *uart, u8 mdr1_val,
168 u8 fcr_val)
169{
Deepak K00034502010-08-02 13:18:12 +0300170 u8 timeout = 255;
171
Kevin Hilman6f251e92010-09-27 20:19:38 +0530172 serial_write_reg(uart, UART_OMAP_MDR1, mdr1_val);
Deepak K00034502010-08-02 13:18:12 +0300173 udelay(2);
Kevin Hilman6f251e92010-09-27 20:19:38 +0530174 serial_write_reg(uart, UART_FCR, fcr_val | UART_FCR_CLEAR_XMIT |
Deepak K00034502010-08-02 13:18:12 +0300175 UART_FCR_CLEAR_RCVR);
176 /*
177 * Wait for FIFO to empty: when empty, RX_FIFO_E bit is 0 and
178 * TX_FIFO_E bit is 1.
179 */
Kevin Hilman6f251e92010-09-27 20:19:38 +0530180 while (UART_LSR_THRE != (serial_read_reg(uart, UART_LSR) &
Deepak K00034502010-08-02 13:18:12 +0300181 (UART_LSR_THRE | UART_LSR_DR))) {
182 timeout--;
183 if (!timeout) {
184 /* Should *never* happen. we warn and carry on */
Kevin Hilman6f251e92010-09-27 20:19:38 +0530185 dev_crit(&uart->pdev->dev, "Errata i202: timedout %x\n",
186 serial_read_reg(uart, UART_LSR));
Deepak K00034502010-08-02 13:18:12 +0300187 break;
188 }
189 udelay(1);
190 }
191}
192
Kevin Hilman4af40162009-02-04 10:51:40 -0800193static void omap_uart_save_context(struct omap_uart_state *uart)
Jouni Hogander6e811762008-10-06 15:49:15 +0300194{
Kevin Hilman4af40162009-02-04 10:51:40 -0800195 u16 lcr = 0;
Kevin Hilman4af40162009-02-04 10:51:40 -0800196
197 if (!enable_off_mode)
198 return;
199
Kevin Hilman6f251e92010-09-27 20:19:38 +0530200 lcr = serial_read_reg(uart, UART_LCR);
201 serial_write_reg(uart, UART_LCR, 0xBF);
202 uart->dll = serial_read_reg(uart, UART_DLL);
203 uart->dlh = serial_read_reg(uart, UART_DLM);
204 serial_write_reg(uart, UART_LCR, lcr);
205 uart->ier = serial_read_reg(uart, UART_IER);
206 uart->sysc = serial_read_reg(uart, UART_OMAP_SYSC);
207 uart->scr = serial_read_reg(uart, UART_OMAP_SCR);
208 uart->wer = serial_read_reg(uart, UART_OMAP_WER);
209 serial_write_reg(uart, UART_LCR, 0x80);
210 uart->mcr = serial_read_reg(uart, UART_MCR);
211 serial_write_reg(uart, UART_LCR, lcr);
Kevin Hilman4af40162009-02-04 10:51:40 -0800212
213 uart->context_valid = 1;
214}
215
216static void omap_uart_restore_context(struct omap_uart_state *uart)
217{
218 u16 efr = 0;
Kevin Hilman4af40162009-02-04 10:51:40 -0800219
220 if (!enable_off_mode)
221 return;
222
223 if (!uart->context_valid)
224 return;
225
226 uart->context_valid = 0;
227
Deepak K00034502010-08-02 13:18:12 +0300228 if (uart->errata & UART_ERRATA_i202_MDR1_ACCESS)
229 omap_uart_mdr1_errataset(uart, 0x07, 0xA0);
230 else
Kevin Hilman6f251e92010-09-27 20:19:38 +0530231 serial_write_reg(uart, UART_OMAP_MDR1, 0x7);
232 serial_write_reg(uart, UART_LCR, 0xBF); /* Config B mode */
233 efr = serial_read_reg(uart, UART_EFR);
234 serial_write_reg(uart, UART_EFR, UART_EFR_ECB);
235 serial_write_reg(uart, UART_LCR, 0x0); /* Operational mode */
236 serial_write_reg(uart, UART_IER, 0x0);
237 serial_write_reg(uart, UART_LCR, 0xBF); /* Config B mode */
238 serial_write_reg(uart, UART_DLL, uart->dll);
239 serial_write_reg(uart, UART_DLM, uart->dlh);
240 serial_write_reg(uart, UART_LCR, 0x0); /* Operational mode */
241 serial_write_reg(uart, UART_IER, uart->ier);
242 serial_write_reg(uart, UART_LCR, 0x80);
243 serial_write_reg(uart, UART_MCR, uart->mcr);
244 serial_write_reg(uart, UART_LCR, 0xBF); /* Config B mode */
245 serial_write_reg(uart, UART_EFR, efr);
246 serial_write_reg(uart, UART_LCR, UART_LCR_WLEN8);
247 serial_write_reg(uart, UART_OMAP_SCR, uart->scr);
248 serial_write_reg(uart, UART_OMAP_WER, uart->wer);
249 serial_write_reg(uart, UART_OMAP_SYSC, uart->sysc);
Deepak K00034502010-08-02 13:18:12 +0300250 if (uart->errata & UART_ERRATA_i202_MDR1_ACCESS)
251 omap_uart_mdr1_errataset(uart, 0x00, 0xA1);
252 else
Kevin Hilman6f251e92010-09-27 20:19:38 +0530253 /* UART 16x mode */
254 serial_write_reg(uart, UART_OMAP_MDR1, 0x00);
Kevin Hilman4af40162009-02-04 10:51:40 -0800255}
256#else
257static inline void omap_uart_save_context(struct omap_uart_state *uart) {}
258static inline void omap_uart_restore_context(struct omap_uart_state *uart) {}
259#endif /* CONFIG_PM && CONFIG_ARCH_OMAP3 */
260
261static inline void omap_uart_enable_clocks(struct omap_uart_state *uart)
262{
263 if (uart->clocked)
264 return;
265
Kevin Hilman6f251e92010-09-27 20:19:38 +0530266 omap_device_enable(uart->pdev);
Kevin Hilman4af40162009-02-04 10:51:40 -0800267 uart->clocked = 1;
268 omap_uart_restore_context(uart);
269}
270
271#ifdef CONFIG_PM
272
273static inline void omap_uart_disable_clocks(struct omap_uart_state *uart)
274{
275 if (!uart->clocked)
276 return;
277
278 omap_uart_save_context(uart);
279 uart->clocked = 0;
Kevin Hilman6f251e92010-09-27 20:19:38 +0530280 omap_device_idle(uart->pdev);
Kevin Hilman4af40162009-02-04 10:51:40 -0800281}
282
Kevin Hilmanfd455ea2009-04-27 12:27:36 -0700283static void omap_uart_enable_wakeup(struct omap_uart_state *uart)
284{
285 /* Set 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 set */
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
300static void omap_uart_disable_wakeup(struct omap_uart_state *uart)
301{
302 /* Clear wake-enable bit */
303 if (uart->wk_en && uart->wk_mask) {
304 u32 v = __raw_readl(uart->wk_en);
305 v &= ~uart->wk_mask;
306 __raw_writel(v, uart->wk_en);
307 }
308
309 /* Ensure IOPAD wake-enables are cleared */
310 if (cpu_is_omap34xx() && uart->padconf) {
311 u16 v = omap_ctrl_readw(uart->padconf);
312 v &= ~OMAP3_PADCONF_WAKEUPENABLE0;
313 omap_ctrl_writew(v, uart->padconf);
314 }
315}
316
Kevin Hilman4af40162009-02-04 10:51:40 -0800317static void omap_uart_smart_idle_enable(struct omap_uart_state *uart,
Kevin Hilman6f251e92010-09-27 20:19:38 +0530318 int enable)
Kevin Hilman4af40162009-02-04 10:51:40 -0800319{
Kevin Hilman6f251e92010-09-27 20:19:38 +0530320 u8 idlemode;
Kevin Hilman4af40162009-02-04 10:51:40 -0800321
Kevin Hilman6f251e92010-09-27 20:19:38 +0530322 if (enable) {
323 /**
324 * Errata 2.15: [UART]:Cannot Acknowledge Idle Requests
325 * in Smartidle Mode When Configured for DMA Operations.
326 */
327 if (uart->dma_enabled)
328 idlemode = HWMOD_IDLEMODE_FORCE;
329 else
330 idlemode = HWMOD_IDLEMODE_SMART;
331 } else {
332 idlemode = HWMOD_IDLEMODE_NO;
333 }
Kevin Hilman4af40162009-02-04 10:51:40 -0800334
Kevin Hilman6f251e92010-09-27 20:19:38 +0530335 omap_hwmod_set_slave_idlemode(uart->oh, idlemode);
Kevin Hilman4af40162009-02-04 10:51:40 -0800336}
337
338static void omap_uart_block_sleep(struct omap_uart_state *uart)
339{
340 omap_uart_enable_clocks(uart);
341
342 omap_uart_smart_idle_enable(uart, 0);
343 uart->can_sleep = 0;
Jouni Hoganderba87a9b2008-12-09 13:36:50 +0200344 if (uart->timeout)
345 mod_timer(&uart->timer, jiffies + uart->timeout);
346 else
347 del_timer(&uart->timer);
Kevin Hilman4af40162009-02-04 10:51:40 -0800348}
349
350static void omap_uart_allow_sleep(struct omap_uart_state *uart)
351{
Kevin Hilman6f251e92010-09-27 20:19:38 +0530352 if (device_may_wakeup(&uart->pdev->dev))
Kevin Hilmanfd455ea2009-04-27 12:27:36 -0700353 omap_uart_enable_wakeup(uart);
354 else
355 omap_uart_disable_wakeup(uart);
356
Kevin Hilman4af40162009-02-04 10:51:40 -0800357 if (!uart->clocked)
358 return;
359
360 omap_uart_smart_idle_enable(uart, 1);
361 uart->can_sleep = 1;
362 del_timer(&uart->timer);
363}
364
365static void omap_uart_idle_timer(unsigned long data)
366{
367 struct omap_uart_state *uart = (struct omap_uart_state *)data;
368
369 omap_uart_allow_sleep(uart);
370}
371
372void omap_uart_prepare_idle(int num)
373{
374 struct omap_uart_state *uart;
375
376 list_for_each_entry(uart, &uart_list, node) {
377 if (num == uart->num && uart->can_sleep) {
378 omap_uart_disable_clocks(uart);
379 return;
Jouni Hogander6e811762008-10-06 15:49:15 +0300380 }
381 }
382}
383
Kevin Hilman4af40162009-02-04 10:51:40 -0800384void omap_uart_resume_idle(int num)
385{
386 struct omap_uart_state *uart;
387
388 list_for_each_entry(uart, &uart_list, node) {
389 if (num == uart->num) {
390 omap_uart_enable_clocks(uart);
391
392 /* Check for IO pad wakeup */
393 if (cpu_is_omap34xx() && uart->padconf) {
394 u16 p = omap_ctrl_readw(uart->padconf);
395
396 if (p & OMAP3_PADCONF_WAKEUPEVENT0)
397 omap_uart_block_sleep(uart);
398 }
399
400 /* Check for normal UART wakeup */
401 if (__raw_readl(uart->wk_st) & uart->wk_mask)
402 omap_uart_block_sleep(uart);
Kevin Hilman4af40162009-02-04 10:51:40 -0800403 return;
404 }
405 }
406}
407
408void omap_uart_prepare_suspend(void)
409{
410 struct omap_uart_state *uart;
411
412 list_for_each_entry(uart, &uart_list, node) {
413 omap_uart_allow_sleep(uart);
414 }
415}
416
417int omap_uart_can_sleep(void)
418{
419 struct omap_uart_state *uart;
420 int can_sleep = 1;
421
422 list_for_each_entry(uart, &uart_list, node) {
423 if (!uart->clocked)
424 continue;
425
426 if (!uart->can_sleep) {
427 can_sleep = 0;
428 continue;
429 }
430
431 /* This UART can now safely sleep. */
432 omap_uart_allow_sleep(uart);
433 }
434
435 return can_sleep;
436}
437
438/**
439 * omap_uart_interrupt()
440 *
441 * This handler is used only to detect that *any* UART interrupt has
442 * occurred. It does _nothing_ to handle the interrupt. Rather,
443 * any UART interrupt will trigger the inactivity timer so the
444 * UART will not idle or sleep for its timeout period.
445 *
446 **/
Kevin Hilman6f251e92010-09-27 20:19:38 +0530447/* static int first_interrupt; */
Kevin Hilman4af40162009-02-04 10:51:40 -0800448static irqreturn_t omap_uart_interrupt(int irq, void *dev_id)
449{
450 struct omap_uart_state *uart = dev_id;
451
452 omap_uart_block_sleep(uart);
453
454 return IRQ_NONE;
455}
456
457static void omap_uart_idle_init(struct omap_uart_state *uart)
458{
Kevin Hilman4af40162009-02-04 10:51:40 -0800459 int ret;
460
461 uart->can_sleep = 0;
Kevin Hilmanfd455ea2009-04-27 12:27:36 -0700462 uart->timeout = DEFAULT_TIMEOUT;
Kevin Hilman4af40162009-02-04 10:51:40 -0800463 setup_timer(&uart->timer, omap_uart_idle_timer,
464 (unsigned long) uart);
Tony Lindgren301fe8e2010-02-01 12:34:31 -0800465 if (uart->timeout)
466 mod_timer(&uart->timer, jiffies + uart->timeout);
Kevin Hilman4af40162009-02-04 10:51:40 -0800467 omap_uart_smart_idle_enable(uart, 0);
468
469 if (cpu_is_omap34xx()) {
470 u32 mod = (uart->num == 2) ? OMAP3430_PER_MOD : CORE_MOD;
471 u32 wk_mask = 0;
472 u32 padconf = 0;
473
474 uart->wk_en = OMAP34XX_PRM_REGADDR(mod, PM_WKEN1);
475 uart->wk_st = OMAP34XX_PRM_REGADDR(mod, PM_WKST1);
476 switch (uart->num) {
477 case 0:
478 wk_mask = OMAP3430_ST_UART1_MASK;
479 padconf = 0x182;
480 break;
481 case 1:
482 wk_mask = OMAP3430_ST_UART2_MASK;
483 padconf = 0x17a;
484 break;
485 case 2:
486 wk_mask = OMAP3430_ST_UART3_MASK;
487 padconf = 0x19e;
488 break;
489 }
490 uart->wk_mask = wk_mask;
491 uart->padconf = padconf;
492 } else if (cpu_is_omap24xx()) {
493 u32 wk_mask = 0;
494
495 if (cpu_is_omap2430()) {
496 uart->wk_en = OMAP2430_PRM_REGADDR(CORE_MOD, PM_WKEN1);
497 uart->wk_st = OMAP2430_PRM_REGADDR(CORE_MOD, PM_WKST1);
498 } else if (cpu_is_omap2420()) {
499 uart->wk_en = OMAP2420_PRM_REGADDR(CORE_MOD, PM_WKEN1);
500 uart->wk_st = OMAP2420_PRM_REGADDR(CORE_MOD, PM_WKST1);
501 }
502 switch (uart->num) {
503 case 0:
504 wk_mask = OMAP24XX_ST_UART1_MASK;
505 break;
506 case 1:
507 wk_mask = OMAP24XX_ST_UART2_MASK;
508 break;
509 case 2:
510 wk_mask = OMAP24XX_ST_UART3_MASK;
511 break;
512 }
513 uart->wk_mask = wk_mask;
514 } else {
Nishanth Menonc54bae12010-08-02 13:18:11 +0300515 uart->wk_en = NULL;
516 uart->wk_st = NULL;
Kevin Hilman4af40162009-02-04 10:51:40 -0800517 uart->wk_mask = 0;
518 uart->padconf = 0;
519 }
520
Kevin Hilman6f251e92010-09-27 20:19:38 +0530521 uart->irqflags |= IRQF_SHARED;
522 ret = request_threaded_irq(uart->irq, NULL, omap_uart_interrupt,
523 IRQF_SHARED, "serial idle", (void *)uart);
Kevin Hilman4af40162009-02-04 10:51:40 -0800524 WARN_ON(ret);
525}
526
Tero Kristo24662112009-03-05 16:32:23 +0200527void omap_uart_enable_irqs(int enable)
Jouni Hoganderba87a9b2008-12-09 13:36:50 +0200528{
Tero Kristo24662112009-03-05 16:32:23 +0200529 int ret;
530 struct omap_uart_state *uart;
531
532 list_for_each_entry(uart, &uart_list, node) {
533 if (enable)
Kevin Hilman6f251e92010-09-27 20:19:38 +0530534 ret = request_threaded_irq(uart->irq, NULL,
535 omap_uart_interrupt,
536 IRQF_SHARED,
537 "serial idle",
538 (void *)uart);
Tero Kristo24662112009-03-05 16:32:23 +0200539 else
Kevin Hilman6f251e92010-09-27 20:19:38 +0530540 free_irq(uart->irq, (void *)uart);
Tero Kristo24662112009-03-05 16:32:23 +0200541 }
Jouni Hoganderba87a9b2008-12-09 13:36:50 +0200542}
543
Kevin Hilmanfd455ea2009-04-27 12:27:36 -0700544static ssize_t sleep_timeout_show(struct device *dev,
545 struct device_attribute *attr,
Jouni Hoganderba87a9b2008-12-09 13:36:50 +0200546 char *buf)
547{
Kevin Hilman6f251e92010-09-27 20:19:38 +0530548 struct platform_device *pdev = to_platform_device(dev);
549 struct omap_device *odev = to_omap_device(pdev);
550 struct omap_uart_state *uart = odev->hwmods[0]->dev_attr;
Kevin Hilmanfd455ea2009-04-27 12:27:36 -0700551
552 return sprintf(buf, "%u\n", uart->timeout / HZ);
Jouni Hoganderba87a9b2008-12-09 13:36:50 +0200553}
554
Kevin Hilmanfd455ea2009-04-27 12:27:36 -0700555static ssize_t sleep_timeout_store(struct device *dev,
556 struct device_attribute *attr,
Jouni Hoganderba87a9b2008-12-09 13:36:50 +0200557 const char *buf, size_t n)
558{
Kevin Hilman6f251e92010-09-27 20:19:38 +0530559 struct platform_device *pdev = to_platform_device(dev);
560 struct omap_device *odev = to_omap_device(pdev);
561 struct omap_uart_state *uart = odev->hwmods[0]->dev_attr;
Jouni Hoganderba87a9b2008-12-09 13:36:50 +0200562 unsigned int value;
563
564 if (sscanf(buf, "%u", &value) != 1) {
Sergio Aguirre10c805e2010-03-09 13:22:14 -0600565 dev_err(dev, "sleep_timeout_store: Invalid value\n");
Jouni Hoganderba87a9b2008-12-09 13:36:50 +0200566 return -EINVAL;
567 }
Kevin Hilmanfd455ea2009-04-27 12:27:36 -0700568
569 uart->timeout = value * HZ;
570 if (uart->timeout)
571 mod_timer(&uart->timer, jiffies + uart->timeout);
572 else
573 /* A zero value means disable timeout feature */
574 omap_uart_block_sleep(uart);
575
Jouni Hoganderba87a9b2008-12-09 13:36:50 +0200576 return n;
577}
578
Nishanth Menonbfe69772010-08-02 13:18:12 +0300579static DEVICE_ATTR(sleep_timeout, 0644, sleep_timeout_show,
580 sleep_timeout_store);
Kevin Hilmanfd455ea2009-04-27 12:27:36 -0700581#define DEV_CREATE_FILE(dev, attr) WARN_ON(device_create_file(dev, attr))
Kevin Hilman4af40162009-02-04 10:51:40 -0800582#else
583static inline void omap_uart_idle_init(struct omap_uart_state *uart) {}
Kevin Hilman6f251e92010-09-27 20:19:38 +0530584static void omap_uart_block_sleep(struct omap_uart_state *uart) {}
Kevin Hilmanfd455ea2009-04-27 12:27:36 -0700585#define DEV_CREATE_FILE(dev, attr)
Kevin Hilman4af40162009-02-04 10:51:40 -0800586#endif /* CONFIG_PM */
587
Kevin Hilman6f251e92010-09-27 20:19:38 +0530588#ifndef CONFIG_SERIAL_OMAP
vikram panditace13d472009-12-11 16:16:37 -0800589/*
590 * Override the default 8250 read handler: mem_serial_in()
591 * Empty RX fifo read causes an abort on omap3630 and omap4
592 * This function makes sure that an empty rx fifo is not read on these silicons
593 * (OMAP1/2/3430 are not affected)
594 */
595static unsigned int serial_in_override(struct uart_port *up, int offset)
596{
597 if (UART_RX == offset) {
598 unsigned int lsr;
Alexander Shishkin92303722010-01-08 10:29:06 -0800599 lsr = __serial_read_reg(up, UART_LSR);
vikram panditace13d472009-12-11 16:16:37 -0800600 if (!(lsr & UART_LSR_DR))
601 return -EPERM;
602 }
Alexander Shishkin92303722010-01-08 10:29:06 -0800603
604 return __serial_read_reg(up, offset);
vikram panditace13d472009-12-11 16:16:37 -0800605}
606
Santosh Shilimkare03d37d2010-02-18 08:59:06 +0000607static void serial_out_override(struct uart_port *up, int offset, int value)
608{
609 unsigned int status, tmout = 10000;
610
611 status = __serial_read_reg(up, UART_LSR);
612 while (!(status & UART_LSR_THRE)) {
613 /* Wait up to 10ms for the character(s) to be sent. */
614 if (--tmout == 0)
615 break;
616 udelay(1);
617 status = __serial_read_reg(up, UART_LSR);
618 }
619 __serial_write_reg(up, offset, value);
620}
Kevin Hilman6f251e92010-09-27 20:19:38 +0530621#endif
622
Paul Walmsleyb3c6df32009-09-03 20:14:02 +0300623void __init omap_serial_early_init(void)
Tony Lindgren1dbae812005-11-10 14:26:51 +0000624{
Kevin Hilman6f251e92010-09-27 20:19:38 +0530625 int i = 0;
Tony Lindgren1dbae812005-11-10 14:26:51 +0000626
Kevin Hilman6f251e92010-09-27 20:19:38 +0530627 do {
628 char oh_name[MAX_UART_HWMOD_NAME_LEN];
629 struct omap_hwmod *oh;
630 struct omap_uart_state *uart;
Thomas Weber21b90342010-02-25 09:40:19 +0000631
Kevin Hilman6f251e92010-09-27 20:19:38 +0530632 snprintf(oh_name, MAX_UART_HWMOD_NAME_LEN,
633 "uart%d", i + 1);
634 oh = omap_hwmod_lookup(oh_name);
635 if (!oh)
636 break;
Tony Lindgren1dbae812005-11-10 14:26:51 +0000637
Kevin Hilman6f251e92010-09-27 20:19:38 +0530638 uart = kzalloc(sizeof(struct omap_uart_state), GFP_KERNEL);
639 if (WARN_ON(!uart))
640 return;
Tony Lindgren1dbae812005-11-10 14:26:51 +0000641
Kevin Hilman6f251e92010-09-27 20:19:38 +0530642 uart->oh = oh;
643 uart->num = i++;
644 list_add_tail(&uart->node, &uart_list);
645 num_uarts++;
646
Tony Lindgren84f90c92009-10-16 09:53:00 -0700647 /*
Kevin Hilman6f251e92010-09-27 20:19:38 +0530648 * NOTE: omap_hwmod_init() has not yet been called,
649 * so no hwmod functions will work yet.
Tony Lindgren84f90c92009-10-16 09:53:00 -0700650 */
Tony Lindgren84f90c92009-10-16 09:53:00 -0700651
Kevin Hilman6f251e92010-09-27 20:19:38 +0530652 /*
653 * During UART early init, device need to be probed
654 * to determine SoC specific init before omap_device
655 * is ready. Therefore, don't allow idle here
656 */
657 uart->oh->flags |= HWMOD_INIT_NO_IDLE | HWMOD_INIT_NO_RESET;
658 } while (1);
Paul Walmsleyb3c6df32009-09-03 20:14:02 +0300659}
660
Mika Westerbergf62349e2009-12-11 16:16:35 -0800661/**
662 * omap_serial_init_port() - initialize single serial port
663 * @port: serial port number (0-3)
664 *
665 * This function initialies serial driver for given @port only.
666 * Platforms can call this function instead of omap_serial_init()
667 * if they don't plan to use all available UARTs as serial ports.
668 *
669 * Don't mix calls to omap_serial_init_port() and omap_serial_init(),
670 * use only one of the two.
671 */
672void __init omap_serial_init_port(int port)
673{
674 struct omap_uart_state *uart;
Kevin Hilman6f251e92010-09-27 20:19:38 +0530675 struct omap_hwmod *oh;
676 struct omap_device *od;
677 void *pdata = NULL;
678 u32 pdata_size = 0;
679 char *name;
680#ifndef CONFIG_SERIAL_OMAP
681 struct plat_serial8250_port ports[2] = {
682 {},
683 {.flags = 0},
684 };
685 struct plat_serial8250_port *p = &ports[0];
686#else
687 struct omap_uart_port_info omap_up;
688#endif
Mika Westerbergf62349e2009-12-11 16:16:35 -0800689
Kevin Hilman6f251e92010-09-27 20:19:38 +0530690 if (WARN_ON(port < 0))
Sergio Aguirree88d5562010-02-27 14:13:43 -0600691 return;
Kevin Hilman6f251e92010-09-27 20:19:38 +0530692 if (WARN_ON(port >= num_uarts))
Mika Westerbergf62349e2009-12-11 16:16:35 -0800693 return;
694
Kevin Hilman6f251e92010-09-27 20:19:38 +0530695 list_for_each_entry(uart, &uart_list, node)
696 if (port == uart->num)
697 break;
698
699 oh = uart->oh;
700 uart->dma_enabled = 0;
701#ifndef CONFIG_SERIAL_OMAP
702 name = "serial8250";
703
704 /*
705 * !! 8250 driver does not use standard IORESOURCE* It
706 * has it's own custom pdata that can be taken from
707 * the hwmod resource data. But, this needs to be
708 * done after the build.
709 *
710 * ?? does it have to be done before the register ??
711 * YES, because platform_device_data_add() copies
712 * pdata, it does not use a pointer.
713 */
714 p->flags = UPF_BOOT_AUTOCONF;
715 p->iotype = UPIO_MEM;
716 p->regshift = 2;
717 p->uartclk = OMAP24XX_BASE_BAUD * 16;
718 p->irq = oh->mpu_irqs[0].irq;
719 p->mapbase = oh->slaves[0]->addr->pa_start;
720 p->membase = omap_hwmod_get_mpu_rt_va(oh);
721 p->irqflags = IRQF_SHARED;
722 p->private_data = uart;
Mika Westerbergf62349e2009-12-11 16:16:35 -0800723
vikram pandita30e53bc2010-02-15 10:03:33 -0800724 /*
725 * omap44xx: Never read empty UART fifo
726 * omap3xxx: Never read empty UART fifo on UARTs
727 * with IP rev >=0x52
728 */
Kevin Hilman6f251e92010-09-27 20:19:38 +0530729 uart->regshift = p->regshift;
730 uart->membase = p->membase;
Nishanth Menon5a927b32010-08-02 13:18:12 +0300731 if (cpu_is_omap44xx())
732 uart->errata |= UART_ERRATA_FIFO_FULL_ABORT;
Kevin Hilman6f251e92010-09-27 20:19:38 +0530733 else if ((serial_read_reg(uart, UART_OMAP_MVER) & 0xFF)
Nishanth Menon5a927b32010-08-02 13:18:12 +0300734 >= UART_OMAP_NO_EMPTY_FIFO_READ_IP_REV)
735 uart->errata |= UART_ERRATA_FIFO_FULL_ABORT;
736
737 if (uart->errata & UART_ERRATA_FIFO_FULL_ABORT) {
Kevin Hilman6f251e92010-09-27 20:19:38 +0530738 p->serial_in = serial_in_override;
739 p->serial_out = serial_out_override;
740 }
741
742 pdata = &ports[0];
743 pdata_size = 2 * sizeof(struct plat_serial8250_port);
744#else
745
746 name = DRIVER_NAME;
747
748 omap_up.dma_enabled = uart->dma_enabled;
749 omap_up.uartclk = OMAP24XX_BASE_BAUD * 16;
750 omap_up.mapbase = oh->slaves[0]->addr->pa_start;
751 omap_up.membase = omap_hwmod_get_mpu_rt_va(oh);
752 omap_up.irqflags = IRQF_SHARED;
753 omap_up.flags = UPF_BOOT_AUTOCONF | UPF_SHARE_IRQ;
754
755 pdata = &omap_up;
756 pdata_size = sizeof(struct omap_uart_port_info);
757#endif
758
759 if (WARN_ON(!oh))
760 return;
761
762 od = omap_device_build(name, uart->num, oh, pdata, pdata_size,
763 omap_uart_latency,
764 ARRAY_SIZE(omap_uart_latency), false);
765 WARN(IS_ERR(od), "Could not build omap_device for %s: %s.\n",
766 name, oh->name);
767
768 uart->irq = oh->mpu_irqs[0].irq;
769 uart->regshift = 2;
770 uart->mapbase = oh->slaves[0]->addr->pa_start;
771 uart->membase = omap_hwmod_get_mpu_rt_va(oh);
772 uart->pdev = &od->pdev;
773
774 oh->dev_attr = uart;
775
776 /*
777 * Because of early UART probing, UART did not get idled
778 * on init. Now that omap_device is ready, ensure full idle
779 * before doing omap_device_enable().
780 */
781 omap_hwmod_idle(uart->oh);
782
783 omap_device_enable(uart->pdev);
784 omap_uart_idle_init(uart);
785 omap_uart_reset(uart);
786 omap_hwmod_enable_wakeup(uart->oh);
787 omap_device_idle(uart->pdev);
788
789 /*
790 * Need to block sleep long enough for interrupt driven
791 * driver to start. Console driver is in polling mode
792 * so device needs to be kept enabled while polling driver
793 * is in use.
794 */
795 if (uart->timeout)
796 uart->timeout = (30 * HZ);
797 omap_uart_block_sleep(uart);
798 uart->timeout = DEFAULT_TIMEOUT;
799
800 if ((cpu_is_omap34xx() && uart->padconf) ||
801 (uart->wk_en && uart->wk_mask)) {
802 device_init_wakeup(&od->pdev.dev, true);
803 DEV_CREATE_FILE(&od->pdev.dev, &dev_attr_sleep_timeout);
Santosh Shilimkare03d37d2010-02-18 08:59:06 +0000804 }
Deepak K00034502010-08-02 13:18:12 +0300805
806 /* Enable the MDR1 errata for OMAP3 */
807 if (cpu_is_omap34xx())
808 uart->errata |= UART_ERRATA_i202_MDR1_ACCESS;
Mika Westerbergf62349e2009-12-11 16:16:35 -0800809}
810
811/**
812 * omap_serial_init() - intialize all supported serial ports
813 *
814 * Initializes all available UARTs as serial ports. Platforms
815 * can call this function when they want to have default behaviour
816 * for serial ports (e.g initialize them all as serial ports).
817 */
Paul Walmsleyb3c6df32009-09-03 20:14:02 +0300818void __init omap_serial_init(void)
819{
Kevin Hilman6f251e92010-09-27 20:19:38 +0530820 struct omap_uart_state *uart;
Paul Walmsleyb3c6df32009-09-03 20:14:02 +0300821
Kevin Hilman6f251e92010-09-27 20:19:38 +0530822 list_for_each_entry(uart, &uart_list, node)
823 omap_serial_init_port(uart->num);
Tony Lindgren1dbae812005-11-10 14:26:51 +0000824}