blob: 9dc077e2d8afe5918f97cc228be6985887e104c2 [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>
Kevin Hilman3244fcd2010-09-27 20:19:53 +053029#include <linux/pm_runtime.h>
Paul Walmsley0d8e2d02010-11-24 16:49:05 -070030#include <linux/console.h>
Kevin Hilman6f251e92010-09-27 20:19:38 +053031
32#ifdef CONFIG_SERIAL_OMAP
33#include <plat/omap-serial.h>
34#endif
Tony Lindgren1dbae812005-11-10 14:26:51 +000035
Tony Lindgrence491cf2009-10-20 09:40:47 -070036#include <plat/common.h>
37#include <plat/board.h>
38#include <plat/clock.h>
Kevin Hilman6f251e92010-09-27 20:19:38 +053039#include <plat/dma.h>
40#include <plat/omap_hwmod.h>
41#include <plat/omap_device.h>
Tony Lindgren1dbae812005-11-10 14:26:51 +000042
Kevin Hilman4af40162009-02-04 10:51:40 -080043#include "prm.h"
44#include "pm.h"
Kevin Hilman6f251e92010-09-27 20:19:38 +053045#include "cm.h"
Kevin Hilman4af40162009-02-04 10:51:40 -080046#include "prm-regbits-34xx.h"
Paul Walmsley4814ced2010-10-08 11:40:20 -060047#include "control.h"
Kevin Hilman4af40162009-02-04 10:51:40 -080048
vikram panditace13d472009-12-11 16:16:37 -080049#define UART_OMAP_NO_EMPTY_FIFO_READ_IP_REV 0x52
Kevin Hilman4af40162009-02-04 10:51:40 -080050#define UART_OMAP_WER 0x17 /* Wake-up enable register */
51
Nishanth Menon5a927b32010-08-02 13:18:12 +030052#define UART_ERRATA_FIFO_FULL_ABORT (0x1 << 0)
Deepak K00034502010-08-02 13:18:12 +030053#define UART_ERRATA_i202_MDR1_ACCESS (0x1 << 1)
Nishanth Menon5a927b32010-08-02 13:18:12 +030054
Tony Lindgren301fe8e2010-02-01 12:34:31 -080055/*
56 * NOTE: By default the serial timeout is disabled as it causes lost characters
57 * over the serial ports. This means that the UART clocks will stay on until
58 * disabled via sysfs. This also causes that any deeper omap sleep states are
59 * blocked.
60 */
61#define DEFAULT_TIMEOUT 0
Kevin Hilman4af40162009-02-04 10:51:40 -080062
Kevin Hilman6f251e92010-09-27 20:19:38 +053063#define MAX_UART_HWMOD_NAME_LEN 16
64
Kevin Hilman4af40162009-02-04 10:51:40 -080065struct omap_uart_state {
66 int num;
67 int can_sleep;
68 struct timer_list timer;
69 u32 timeout;
70
71 void __iomem *wk_st;
72 void __iomem *wk_en;
73 u32 wk_mask;
74 u32 padconf;
Kevin Hilman6f251e92010-09-27 20:19:38 +053075 u32 dma_enabled;
Kevin Hilman4af40162009-02-04 10:51:40 -080076
77 struct clk *ick;
78 struct clk *fck;
79 int clocked;
80
Kevin Hilman6f251e92010-09-27 20:19:38 +053081 int irq;
82 int regshift;
83 int irqflags;
84 void __iomem *membase;
85 resource_size_t mapbase;
86
Kevin Hilman4af40162009-02-04 10:51:40 -080087 struct list_head node;
Kevin Hilman6f251e92010-09-27 20:19:38 +053088 struct omap_hwmod *oh;
89 struct platform_device *pdev;
Kevin Hilman4af40162009-02-04 10:51:40 -080090
Nishanth Menon5a927b32010-08-02 13:18:12 +030091 u32 errata;
Kevin Hilman4af40162009-02-04 10:51:40 -080092#if defined(CONFIG_ARCH_OMAP3) && defined(CONFIG_PM)
93 int context_valid;
94
95 /* Registers to be saved/restored for OFF-mode */
96 u16 dll;
97 u16 dlh;
98 u16 ier;
99 u16 sysc;
100 u16 scr;
101 u16 wer;
Govindraj R5ade4ff2010-08-02 13:18:11 +0300102 u16 mcr;
Kevin Hilman4af40162009-02-04 10:51:40 -0800103#endif
104};
105
Kevin Hilman4af40162009-02-04 10:51:40 -0800106static LIST_HEAD(uart_list);
Kevin Hilman6f251e92010-09-27 20:19:38 +0530107static u8 num_uarts;
Tony Lindgren1dbae812005-11-10 14:26:51 +0000108
Kevin Hilman8da37d92010-09-27 20:20:06 +0530109/*
110 * Since these idle/enable hooks are used in the idle path itself
111 * which has interrupts disabled, use the non-locking versions of
112 * the hwmod enable/disable functions.
113 */
114static int uart_idle_hwmod(struct omap_device *od)
115{
116 _omap_hwmod_idle(od->hwmods[0]);
117
118 return 0;
119}
120
121static int uart_enable_hwmod(struct omap_device *od)
122{
123 _omap_hwmod_enable(od->hwmods[0]);
124
125 return 0;
126}
127
Kevin Hilman6f251e92010-09-27 20:19:38 +0530128static struct omap_device_pm_latency omap_uart_latency[] = {
129 {
Kevin Hilman8da37d92010-09-27 20:20:06 +0530130 .deactivate_func = uart_idle_hwmod,
131 .activate_func = uart_enable_hwmod,
Kevin Hilman6f251e92010-09-27 20:19:38 +0530132 .flags = OMAP_DEVICE_LATENCY_AUTO_ADJUST,
133 },
134};
135
Alexander Shishkin92303722010-01-08 10:29:06 -0800136static inline unsigned int __serial_read_reg(struct uart_port *up,
Kevin Hilman6f251e92010-09-27 20:19:38 +0530137 int offset)
Alexander Shishkin92303722010-01-08 10:29:06 -0800138{
139 offset <<= up->regshift;
140 return (unsigned int)__raw_readb(up->membase + offset);
141}
142
Kevin Hilman6f251e92010-09-27 20:19:38 +0530143static inline unsigned int serial_read_reg(struct omap_uart_state *uart,
Tony Lindgren1dbae812005-11-10 14:26:51 +0000144 int offset)
145{
Kevin Hilman6f251e92010-09-27 20:19:38 +0530146 offset <<= uart->regshift;
147 return (unsigned int)__raw_readb(uart->membase + offset);
Tony Lindgren1dbae812005-11-10 14:26:51 +0000148}
149
Santosh Shilimkare03d37d2010-02-18 08:59:06 +0000150static inline void __serial_write_reg(struct uart_port *up, int offset,
151 int value)
152{
153 offset <<= up->regshift;
154 __raw_writeb(value, up->membase + offset);
155}
156
Kevin Hilman6f251e92010-09-27 20:19:38 +0530157static inline void serial_write_reg(struct omap_uart_state *uart, int offset,
Tony Lindgren1dbae812005-11-10 14:26:51 +0000158 int value)
159{
Kevin Hilman6f251e92010-09-27 20:19:38 +0530160 offset <<= uart->regshift;
161 __raw_writeb(value, uart->membase + offset);
Tony Lindgren1dbae812005-11-10 14:26:51 +0000162}
163
164/*
165 * Internal UARTs need to be initialized for the 8250 autoconfig to work
166 * properly. Note that the TX watermark initialization may not be needed
167 * once the 8250.c watermark handling code is merged.
168 */
Kevin Hilman6f251e92010-09-27 20:19:38 +0530169
Kevin Hilman4af40162009-02-04 10:51:40 -0800170static inline void __init omap_uart_reset(struct omap_uart_state *uart)
Tony Lindgren1dbae812005-11-10 14:26:51 +0000171{
Andrei Emeltchenko498cb952010-11-30 14:11:49 -0800172 serial_write_reg(uart, UART_OMAP_MDR1, UART_OMAP_MDR1_DISABLE);
Kevin Hilman6f251e92010-09-27 20:19:38 +0530173 serial_write_reg(uart, UART_OMAP_SCR, 0x08);
Andrei Emeltchenko498cb952010-11-30 14:11:49 -0800174 serial_write_reg(uart, UART_OMAP_MDR1, UART_OMAP_MDR1_16X_MODE);
Tony Lindgren1dbae812005-11-10 14:26:51 +0000175}
176
Kevin Hilman4af40162009-02-04 10:51:40 -0800177#if defined(CONFIG_PM) && defined(CONFIG_ARCH_OMAP3)
178
Deepak K00034502010-08-02 13:18:12 +0300179/*
180 * Work Around for Errata i202 (3430 - 1.12, 3630 - 1.6)
181 * The access to uart register after MDR1 Access
182 * causes UART to corrupt data.
183 *
184 * Need a delay =
185 * 5 L4 clock cycles + 5 UART functional clock cycle (@48MHz = ~0.2uS)
186 * give 10 times as much
187 */
188static void omap_uart_mdr1_errataset(struct omap_uart_state *uart, u8 mdr1_val,
189 u8 fcr_val)
190{
Deepak K00034502010-08-02 13:18:12 +0300191 u8 timeout = 255;
192
Kevin Hilman6f251e92010-09-27 20:19:38 +0530193 serial_write_reg(uart, UART_OMAP_MDR1, mdr1_val);
Deepak K00034502010-08-02 13:18:12 +0300194 udelay(2);
Kevin Hilman6f251e92010-09-27 20:19:38 +0530195 serial_write_reg(uart, UART_FCR, fcr_val | UART_FCR_CLEAR_XMIT |
Deepak K00034502010-08-02 13:18:12 +0300196 UART_FCR_CLEAR_RCVR);
197 /*
198 * Wait for FIFO to empty: when empty, RX_FIFO_E bit is 0 and
199 * TX_FIFO_E bit is 1.
200 */
Kevin Hilman6f251e92010-09-27 20:19:38 +0530201 while (UART_LSR_THRE != (serial_read_reg(uart, UART_LSR) &
Deepak K00034502010-08-02 13:18:12 +0300202 (UART_LSR_THRE | UART_LSR_DR))) {
203 timeout--;
204 if (!timeout) {
205 /* Should *never* happen. we warn and carry on */
Kevin Hilman6f251e92010-09-27 20:19:38 +0530206 dev_crit(&uart->pdev->dev, "Errata i202: timedout %x\n",
207 serial_read_reg(uart, UART_LSR));
Deepak K00034502010-08-02 13:18:12 +0300208 break;
209 }
210 udelay(1);
211 }
212}
213
Kevin Hilman4af40162009-02-04 10:51:40 -0800214static void omap_uart_save_context(struct omap_uart_state *uart)
Jouni Hogander6e811762008-10-06 15:49:15 +0300215{
Kevin Hilman4af40162009-02-04 10:51:40 -0800216 u16 lcr = 0;
Kevin Hilman4af40162009-02-04 10:51:40 -0800217
218 if (!enable_off_mode)
219 return;
220
Kevin Hilman6f251e92010-09-27 20:19:38 +0530221 lcr = serial_read_reg(uart, UART_LCR);
Andrei Emeltchenko662b083a2010-11-30 14:11:49 -0800222 serial_write_reg(uart, UART_LCR, UART_LCR_CONF_MODE_B);
Kevin Hilman6f251e92010-09-27 20:19:38 +0530223 uart->dll = serial_read_reg(uart, UART_DLL);
224 uart->dlh = serial_read_reg(uart, UART_DLM);
225 serial_write_reg(uart, UART_LCR, lcr);
226 uart->ier = serial_read_reg(uart, UART_IER);
227 uart->sysc = serial_read_reg(uart, UART_OMAP_SYSC);
228 uart->scr = serial_read_reg(uart, UART_OMAP_SCR);
229 uart->wer = serial_read_reg(uart, UART_OMAP_WER);
Andrei Emeltchenko662b083a2010-11-30 14:11:49 -0800230 serial_write_reg(uart, UART_LCR, UART_LCR_CONF_MODE_A);
Kevin Hilman6f251e92010-09-27 20:19:38 +0530231 uart->mcr = serial_read_reg(uart, UART_MCR);
232 serial_write_reg(uart, UART_LCR, lcr);
Kevin Hilman4af40162009-02-04 10:51:40 -0800233
234 uart->context_valid = 1;
235}
236
237static void omap_uart_restore_context(struct omap_uart_state *uart)
238{
239 u16 efr = 0;
Kevin Hilman4af40162009-02-04 10:51:40 -0800240
241 if (!enable_off_mode)
242 return;
243
244 if (!uart->context_valid)
245 return;
246
247 uart->context_valid = 0;
248
Deepak K00034502010-08-02 13:18:12 +0300249 if (uart->errata & UART_ERRATA_i202_MDR1_ACCESS)
Andrei Emeltchenko498cb952010-11-30 14:11:49 -0800250 omap_uart_mdr1_errataset(uart, UART_OMAP_MDR1_DISABLE, 0xA0);
Deepak K00034502010-08-02 13:18:12 +0300251 else
Andrei Emeltchenko498cb952010-11-30 14:11:49 -0800252 serial_write_reg(uart, UART_OMAP_MDR1, UART_OMAP_MDR1_DISABLE);
253
Andrei Emeltchenko662b083a2010-11-30 14:11:49 -0800254 serial_write_reg(uart, UART_LCR, UART_LCR_CONF_MODE_B);
Kevin Hilman6f251e92010-09-27 20:19:38 +0530255 efr = serial_read_reg(uart, UART_EFR);
256 serial_write_reg(uart, UART_EFR, UART_EFR_ECB);
257 serial_write_reg(uart, UART_LCR, 0x0); /* Operational mode */
258 serial_write_reg(uart, UART_IER, 0x0);
Andrei Emeltchenko662b083a2010-11-30 14:11:49 -0800259 serial_write_reg(uart, UART_LCR, UART_LCR_CONF_MODE_B);
Kevin Hilman6f251e92010-09-27 20:19:38 +0530260 serial_write_reg(uart, UART_DLL, uart->dll);
261 serial_write_reg(uart, UART_DLM, uart->dlh);
262 serial_write_reg(uart, UART_LCR, 0x0); /* Operational mode */
263 serial_write_reg(uart, UART_IER, uart->ier);
Andrei Emeltchenko662b083a2010-11-30 14:11:49 -0800264 serial_write_reg(uart, UART_LCR, UART_LCR_CONF_MODE_A);
Kevin Hilman6f251e92010-09-27 20:19:38 +0530265 serial_write_reg(uart, UART_MCR, uart->mcr);
Andrei Emeltchenko662b083a2010-11-30 14:11:49 -0800266 serial_write_reg(uart, UART_LCR, UART_LCR_CONF_MODE_B);
Kevin Hilman6f251e92010-09-27 20:19:38 +0530267 serial_write_reg(uart, UART_EFR, efr);
268 serial_write_reg(uart, UART_LCR, UART_LCR_WLEN8);
269 serial_write_reg(uart, UART_OMAP_SCR, uart->scr);
270 serial_write_reg(uart, UART_OMAP_WER, uart->wer);
271 serial_write_reg(uart, UART_OMAP_SYSC, uart->sysc);
Andrei Emeltchenko498cb952010-11-30 14:11:49 -0800272
Deepak K00034502010-08-02 13:18:12 +0300273 if (uart->errata & UART_ERRATA_i202_MDR1_ACCESS)
Andrei Emeltchenko498cb952010-11-30 14:11:49 -0800274 omap_uart_mdr1_errataset(uart, UART_OMAP_MDR1_16X_MODE, 0xA1);
Deepak K00034502010-08-02 13:18:12 +0300275 else
Kevin Hilman6f251e92010-09-27 20:19:38 +0530276 /* UART 16x mode */
Andrei Emeltchenko498cb952010-11-30 14:11:49 -0800277 serial_write_reg(uart, UART_OMAP_MDR1,
278 UART_OMAP_MDR1_16X_MODE);
Kevin Hilman4af40162009-02-04 10:51:40 -0800279}
280#else
281static inline void omap_uart_save_context(struct omap_uart_state *uart) {}
282static inline void omap_uart_restore_context(struct omap_uart_state *uart) {}
283#endif /* CONFIG_PM && CONFIG_ARCH_OMAP3 */
284
285static inline void omap_uart_enable_clocks(struct omap_uart_state *uart)
286{
287 if (uart->clocked)
288 return;
289
Kevin Hilman6f251e92010-09-27 20:19:38 +0530290 omap_device_enable(uart->pdev);
Kevin Hilman4af40162009-02-04 10:51:40 -0800291 uart->clocked = 1;
292 omap_uart_restore_context(uart);
293}
294
295#ifdef CONFIG_PM
296
297static inline void omap_uart_disable_clocks(struct omap_uart_state *uart)
298{
299 if (!uart->clocked)
300 return;
301
302 omap_uart_save_context(uart);
303 uart->clocked = 0;
Kevin Hilman6f251e92010-09-27 20:19:38 +0530304 omap_device_idle(uart->pdev);
Kevin Hilman4af40162009-02-04 10:51:40 -0800305}
306
Kevin Hilmanfd455ea2009-04-27 12:27:36 -0700307static void omap_uart_enable_wakeup(struct omap_uart_state *uart)
308{
309 /* Set wake-enable bit */
310 if (uart->wk_en && uart->wk_mask) {
311 u32 v = __raw_readl(uart->wk_en);
312 v |= uart->wk_mask;
313 __raw_writel(v, uart->wk_en);
314 }
315
316 /* Ensure IOPAD wake-enables are set */
317 if (cpu_is_omap34xx() && uart->padconf) {
318 u16 v = omap_ctrl_readw(uart->padconf);
319 v |= OMAP3_PADCONF_WAKEUPENABLE0;
320 omap_ctrl_writew(v, uart->padconf);
321 }
322}
323
324static void omap_uart_disable_wakeup(struct omap_uart_state *uart)
325{
326 /* Clear wake-enable bit */
327 if (uart->wk_en && uart->wk_mask) {
328 u32 v = __raw_readl(uart->wk_en);
329 v &= ~uart->wk_mask;
330 __raw_writel(v, uart->wk_en);
331 }
332
333 /* Ensure IOPAD wake-enables are cleared */
334 if (cpu_is_omap34xx() && uart->padconf) {
335 u16 v = omap_ctrl_readw(uart->padconf);
336 v &= ~OMAP3_PADCONF_WAKEUPENABLE0;
337 omap_ctrl_writew(v, uart->padconf);
338 }
339}
340
Kevin Hilman4af40162009-02-04 10:51:40 -0800341static void omap_uart_smart_idle_enable(struct omap_uart_state *uart,
Kevin Hilman6f251e92010-09-27 20:19:38 +0530342 int enable)
Kevin Hilman4af40162009-02-04 10:51:40 -0800343{
Kevin Hilman6f251e92010-09-27 20:19:38 +0530344 u8 idlemode;
Kevin Hilman4af40162009-02-04 10:51:40 -0800345
Kevin Hilman6f251e92010-09-27 20:19:38 +0530346 if (enable) {
347 /**
348 * Errata 2.15: [UART]:Cannot Acknowledge Idle Requests
349 * in Smartidle Mode When Configured for DMA Operations.
350 */
351 if (uart->dma_enabled)
352 idlemode = HWMOD_IDLEMODE_FORCE;
353 else
354 idlemode = HWMOD_IDLEMODE_SMART;
355 } else {
356 idlemode = HWMOD_IDLEMODE_NO;
357 }
Kevin Hilman4af40162009-02-04 10:51:40 -0800358
Kevin Hilman6f251e92010-09-27 20:19:38 +0530359 omap_hwmod_set_slave_idlemode(uart->oh, idlemode);
Kevin Hilman4af40162009-02-04 10:51:40 -0800360}
361
362static void omap_uart_block_sleep(struct omap_uart_state *uart)
363{
364 omap_uart_enable_clocks(uart);
365
366 omap_uart_smart_idle_enable(uart, 0);
367 uart->can_sleep = 0;
Jouni Hoganderba87a9b2008-12-09 13:36:50 +0200368 if (uart->timeout)
369 mod_timer(&uart->timer, jiffies + uart->timeout);
370 else
371 del_timer(&uart->timer);
Kevin Hilman4af40162009-02-04 10:51:40 -0800372}
373
374static void omap_uart_allow_sleep(struct omap_uart_state *uart)
375{
Kevin Hilman6f251e92010-09-27 20:19:38 +0530376 if (device_may_wakeup(&uart->pdev->dev))
Kevin Hilmanfd455ea2009-04-27 12:27:36 -0700377 omap_uart_enable_wakeup(uart);
378 else
379 omap_uart_disable_wakeup(uart);
380
Kevin Hilman4af40162009-02-04 10:51:40 -0800381 if (!uart->clocked)
382 return;
383
384 omap_uart_smart_idle_enable(uart, 1);
385 uart->can_sleep = 1;
386 del_timer(&uart->timer);
387}
388
389static void omap_uart_idle_timer(unsigned long data)
390{
391 struct omap_uart_state *uart = (struct omap_uart_state *)data;
392
393 omap_uart_allow_sleep(uart);
394}
395
396void omap_uart_prepare_idle(int num)
397{
398 struct omap_uart_state *uart;
399
400 list_for_each_entry(uart, &uart_list, node) {
401 if (num == uart->num && uart->can_sleep) {
402 omap_uart_disable_clocks(uart);
403 return;
Jouni Hogander6e811762008-10-06 15:49:15 +0300404 }
405 }
406}
407
Kevin Hilman4af40162009-02-04 10:51:40 -0800408void omap_uart_resume_idle(int num)
409{
410 struct omap_uart_state *uart;
411
412 list_for_each_entry(uart, &uart_list, node) {
Kevin Hilmanf9100432010-11-24 11:09:03 -0800413 if (num == uart->num && uart->can_sleep) {
Kevin Hilman4af40162009-02-04 10:51:40 -0800414 omap_uart_enable_clocks(uart);
415
416 /* Check for IO pad wakeup */
417 if (cpu_is_omap34xx() && uart->padconf) {
418 u16 p = omap_ctrl_readw(uart->padconf);
419
420 if (p & OMAP3_PADCONF_WAKEUPEVENT0)
421 omap_uart_block_sleep(uart);
422 }
423
424 /* Check for normal UART wakeup */
425 if (__raw_readl(uart->wk_st) & uart->wk_mask)
426 omap_uart_block_sleep(uart);
Kevin Hilman4af40162009-02-04 10:51:40 -0800427 return;
428 }
429 }
430}
431
432void omap_uart_prepare_suspend(void)
433{
434 struct omap_uart_state *uart;
435
436 list_for_each_entry(uart, &uart_list, node) {
437 omap_uart_allow_sleep(uart);
438 }
439}
440
441int omap_uart_can_sleep(void)
442{
443 struct omap_uart_state *uart;
444 int can_sleep = 1;
445
446 list_for_each_entry(uart, &uart_list, node) {
447 if (!uart->clocked)
448 continue;
449
450 if (!uart->can_sleep) {
451 can_sleep = 0;
452 continue;
453 }
454
455 /* This UART can now safely sleep. */
456 omap_uart_allow_sleep(uart);
457 }
458
459 return can_sleep;
460}
461
462/**
463 * omap_uart_interrupt()
464 *
465 * This handler is used only to detect that *any* UART interrupt has
466 * occurred. It does _nothing_ to handle the interrupt. Rather,
467 * any UART interrupt will trigger the inactivity timer so the
468 * UART will not idle or sleep for its timeout period.
469 *
470 **/
Kevin Hilman6f251e92010-09-27 20:19:38 +0530471/* static int first_interrupt; */
Kevin Hilman4af40162009-02-04 10:51:40 -0800472static irqreturn_t omap_uart_interrupt(int irq, void *dev_id)
473{
474 struct omap_uart_state *uart = dev_id;
475
476 omap_uart_block_sleep(uart);
477
478 return IRQ_NONE;
479}
480
481static void omap_uart_idle_init(struct omap_uart_state *uart)
482{
Kevin Hilman4af40162009-02-04 10:51:40 -0800483 int ret;
484
485 uart->can_sleep = 0;
Kevin Hilmanfd455ea2009-04-27 12:27:36 -0700486 uart->timeout = DEFAULT_TIMEOUT;
Kevin Hilman4af40162009-02-04 10:51:40 -0800487 setup_timer(&uart->timer, omap_uart_idle_timer,
488 (unsigned long) uart);
Tony Lindgren301fe8e2010-02-01 12:34:31 -0800489 if (uart->timeout)
490 mod_timer(&uart->timer, jiffies + uart->timeout);
Kevin Hilman4af40162009-02-04 10:51:40 -0800491 omap_uart_smart_idle_enable(uart, 0);
492
493 if (cpu_is_omap34xx()) {
Govindraj.R52663ae2010-09-27 20:20:41 +0530494 u32 mod = (uart->num > 1) ? OMAP3430_PER_MOD : CORE_MOD;
Kevin Hilman4af40162009-02-04 10:51:40 -0800495 u32 wk_mask = 0;
496 u32 padconf = 0;
497
498 uart->wk_en = OMAP34XX_PRM_REGADDR(mod, PM_WKEN1);
499 uart->wk_st = OMAP34XX_PRM_REGADDR(mod, PM_WKST1);
500 switch (uart->num) {
501 case 0:
502 wk_mask = OMAP3430_ST_UART1_MASK;
503 padconf = 0x182;
504 break;
505 case 1:
506 wk_mask = OMAP3430_ST_UART2_MASK;
507 padconf = 0x17a;
508 break;
509 case 2:
510 wk_mask = OMAP3430_ST_UART3_MASK;
511 padconf = 0x19e;
512 break;
Govindraj.R52663ae2010-09-27 20:20:41 +0530513 case 3:
514 wk_mask = OMAP3630_ST_UART4_MASK;
515 padconf = 0x0d2;
516 break;
Kevin Hilman4af40162009-02-04 10:51:40 -0800517 }
518 uart->wk_mask = wk_mask;
519 uart->padconf = padconf;
520 } else if (cpu_is_omap24xx()) {
521 u32 wk_mask = 0;
Kevin Hilmancb74f022010-10-20 23:19:03 +0000522 u32 wk_en = PM_WKEN1, wk_st = PM_WKST1;
Kevin Hilman4af40162009-02-04 10:51:40 -0800523
Kevin Hilman4af40162009-02-04 10:51:40 -0800524 switch (uart->num) {
525 case 0:
526 wk_mask = OMAP24XX_ST_UART1_MASK;
527 break;
528 case 1:
529 wk_mask = OMAP24XX_ST_UART2_MASK;
530 break;
531 case 2:
Kevin Hilmancb74f022010-10-20 23:19:03 +0000532 wk_en = OMAP24XX_PM_WKEN2;
533 wk_st = OMAP24XX_PM_WKST2;
Kevin Hilman4af40162009-02-04 10:51:40 -0800534 wk_mask = OMAP24XX_ST_UART3_MASK;
535 break;
536 }
537 uart->wk_mask = wk_mask;
Kevin Hilmancb74f022010-10-20 23:19:03 +0000538 if (cpu_is_omap2430()) {
539 uart->wk_en = OMAP2430_PRM_REGADDR(CORE_MOD, wk_en);
540 uart->wk_st = OMAP2430_PRM_REGADDR(CORE_MOD, wk_st);
541 } else if (cpu_is_omap2420()) {
542 uart->wk_en = OMAP2420_PRM_REGADDR(CORE_MOD, wk_en);
543 uart->wk_st = OMAP2420_PRM_REGADDR(CORE_MOD, wk_st);
544 }
Kevin Hilman4af40162009-02-04 10:51:40 -0800545 } else {
Nishanth Menonc54bae12010-08-02 13:18:11 +0300546 uart->wk_en = NULL;
547 uart->wk_st = NULL;
Kevin Hilman4af40162009-02-04 10:51:40 -0800548 uart->wk_mask = 0;
549 uart->padconf = 0;
550 }
551
Kevin Hilman6f251e92010-09-27 20:19:38 +0530552 uart->irqflags |= IRQF_SHARED;
553 ret = request_threaded_irq(uart->irq, NULL, omap_uart_interrupt,
554 IRQF_SHARED, "serial idle", (void *)uart);
Kevin Hilman4af40162009-02-04 10:51:40 -0800555 WARN_ON(ret);
556}
557
Tero Kristo24662112009-03-05 16:32:23 +0200558void omap_uart_enable_irqs(int enable)
Jouni Hoganderba87a9b2008-12-09 13:36:50 +0200559{
Tero Kristo24662112009-03-05 16:32:23 +0200560 int ret;
561 struct omap_uart_state *uart;
562
563 list_for_each_entry(uart, &uart_list, node) {
Kevin Hilman3244fcd2010-09-27 20:19:53 +0530564 if (enable) {
565 pm_runtime_put_sync(&uart->pdev->dev);
Kevin Hilman6f251e92010-09-27 20:19:38 +0530566 ret = request_threaded_irq(uart->irq, NULL,
567 omap_uart_interrupt,
568 IRQF_SHARED,
569 "serial idle",
570 (void *)uart);
Kevin Hilman3244fcd2010-09-27 20:19:53 +0530571 } else {
572 pm_runtime_get_noresume(&uart->pdev->dev);
Kevin Hilman6f251e92010-09-27 20:19:38 +0530573 free_irq(uart->irq, (void *)uart);
Kevin Hilman3244fcd2010-09-27 20:19:53 +0530574 }
Tero Kristo24662112009-03-05 16:32:23 +0200575 }
Jouni Hoganderba87a9b2008-12-09 13:36:50 +0200576}
577
Kevin Hilmanfd455ea2009-04-27 12:27:36 -0700578static ssize_t sleep_timeout_show(struct device *dev,
579 struct device_attribute *attr,
Jouni Hoganderba87a9b2008-12-09 13:36:50 +0200580 char *buf)
581{
Kevin Hilman6f251e92010-09-27 20:19:38 +0530582 struct platform_device *pdev = to_platform_device(dev);
583 struct omap_device *odev = to_omap_device(pdev);
584 struct omap_uart_state *uart = odev->hwmods[0]->dev_attr;
Kevin Hilmanfd455ea2009-04-27 12:27:36 -0700585
586 return sprintf(buf, "%u\n", uart->timeout / HZ);
Jouni Hoganderba87a9b2008-12-09 13:36:50 +0200587}
588
Kevin Hilmanfd455ea2009-04-27 12:27:36 -0700589static ssize_t sleep_timeout_store(struct device *dev,
590 struct device_attribute *attr,
Jouni Hoganderba87a9b2008-12-09 13:36:50 +0200591 const char *buf, size_t n)
592{
Kevin Hilman6f251e92010-09-27 20:19:38 +0530593 struct platform_device *pdev = to_platform_device(dev);
594 struct omap_device *odev = to_omap_device(pdev);
595 struct omap_uart_state *uart = odev->hwmods[0]->dev_attr;
Jouni Hoganderba87a9b2008-12-09 13:36:50 +0200596 unsigned int value;
597
598 if (sscanf(buf, "%u", &value) != 1) {
Sergio Aguirre10c805e2010-03-09 13:22:14 -0600599 dev_err(dev, "sleep_timeout_store: Invalid value\n");
Jouni Hoganderba87a9b2008-12-09 13:36:50 +0200600 return -EINVAL;
601 }
Kevin Hilmanfd455ea2009-04-27 12:27:36 -0700602
603 uart->timeout = value * HZ;
604 if (uart->timeout)
605 mod_timer(&uart->timer, jiffies + uart->timeout);
606 else
607 /* A zero value means disable timeout feature */
608 omap_uart_block_sleep(uart);
609
Jouni Hoganderba87a9b2008-12-09 13:36:50 +0200610 return n;
611}
612
Nishanth Menonbfe69772010-08-02 13:18:12 +0300613static DEVICE_ATTR(sleep_timeout, 0644, sleep_timeout_show,
614 sleep_timeout_store);
Kevin Hilmanfd455ea2009-04-27 12:27:36 -0700615#define DEV_CREATE_FILE(dev, attr) WARN_ON(device_create_file(dev, attr))
Kevin Hilman4af40162009-02-04 10:51:40 -0800616#else
617static inline void omap_uart_idle_init(struct omap_uart_state *uart) {}
Santosh Shilimkara1b04cc2010-10-11 11:05:18 +0000618static void omap_uart_block_sleep(struct omap_uart_state *uart)
619{
620 /* Needed to enable UART clocks when built without CONFIG_PM */
621 omap_uart_enable_clocks(uart);
622}
Kevin Hilmanfd455ea2009-04-27 12:27:36 -0700623#define DEV_CREATE_FILE(dev, attr)
Kevin Hilman4af40162009-02-04 10:51:40 -0800624#endif /* CONFIG_PM */
625
Kevin Hilman6f251e92010-09-27 20:19:38 +0530626#ifndef CONFIG_SERIAL_OMAP
vikram panditace13d472009-12-11 16:16:37 -0800627/*
628 * Override the default 8250 read handler: mem_serial_in()
629 * Empty RX fifo read causes an abort on omap3630 and omap4
630 * This function makes sure that an empty rx fifo is not read on these silicons
631 * (OMAP1/2/3430 are not affected)
632 */
633static unsigned int serial_in_override(struct uart_port *up, int offset)
634{
635 if (UART_RX == offset) {
636 unsigned int lsr;
Alexander Shishkin92303722010-01-08 10:29:06 -0800637 lsr = __serial_read_reg(up, UART_LSR);
vikram panditace13d472009-12-11 16:16:37 -0800638 if (!(lsr & UART_LSR_DR))
639 return -EPERM;
640 }
Alexander Shishkin92303722010-01-08 10:29:06 -0800641
642 return __serial_read_reg(up, offset);
vikram panditace13d472009-12-11 16:16:37 -0800643}
644
Santosh Shilimkare03d37d2010-02-18 08:59:06 +0000645static void serial_out_override(struct uart_port *up, int offset, int value)
646{
647 unsigned int status, tmout = 10000;
648
649 status = __serial_read_reg(up, UART_LSR);
650 while (!(status & UART_LSR_THRE)) {
651 /* Wait up to 10ms for the character(s) to be sent. */
652 if (--tmout == 0)
653 break;
654 udelay(1);
655 status = __serial_read_reg(up, UART_LSR);
656 }
657 __serial_write_reg(up, offset, value);
658}
Kevin Hilman6f251e92010-09-27 20:19:38 +0530659#endif
660
Paul Walmsleyb3c6df32009-09-03 20:14:02 +0300661void __init omap_serial_early_init(void)
Tony Lindgren1dbae812005-11-10 14:26:51 +0000662{
Kevin Hilman6f251e92010-09-27 20:19:38 +0530663 int i = 0;
Tony Lindgren1dbae812005-11-10 14:26:51 +0000664
Kevin Hilman6f251e92010-09-27 20:19:38 +0530665 do {
666 char oh_name[MAX_UART_HWMOD_NAME_LEN];
667 struct omap_hwmod *oh;
668 struct omap_uart_state *uart;
Thomas Weber21b90342010-02-25 09:40:19 +0000669
Kevin Hilman6f251e92010-09-27 20:19:38 +0530670 snprintf(oh_name, MAX_UART_HWMOD_NAME_LEN,
671 "uart%d", i + 1);
672 oh = omap_hwmod_lookup(oh_name);
673 if (!oh)
674 break;
Tony Lindgren1dbae812005-11-10 14:26:51 +0000675
Kevin Hilman6f251e92010-09-27 20:19:38 +0530676 uart = kzalloc(sizeof(struct omap_uart_state), GFP_KERNEL);
677 if (WARN_ON(!uart))
678 return;
Tony Lindgren1dbae812005-11-10 14:26:51 +0000679
Kevin Hilman6f251e92010-09-27 20:19:38 +0530680 uart->oh = oh;
681 uart->num = i++;
682 list_add_tail(&uart->node, &uart_list);
683 num_uarts++;
684
Tony Lindgren84f90c92009-10-16 09:53:00 -0700685 /*
Kevin Hilman6f251e92010-09-27 20:19:38 +0530686 * NOTE: omap_hwmod_init() has not yet been called,
687 * so no hwmod functions will work yet.
Tony Lindgren84f90c92009-10-16 09:53:00 -0700688 */
Tony Lindgren84f90c92009-10-16 09:53:00 -0700689
Kevin Hilman6f251e92010-09-27 20:19:38 +0530690 /*
691 * During UART early init, device need to be probed
692 * to determine SoC specific init before omap_device
693 * is ready. Therefore, don't allow idle here
694 */
695 uart->oh->flags |= HWMOD_INIT_NO_IDLE | HWMOD_INIT_NO_RESET;
696 } while (1);
Paul Walmsleyb3c6df32009-09-03 20:14:02 +0300697}
698
Mika Westerbergf62349e2009-12-11 16:16:35 -0800699/**
700 * omap_serial_init_port() - initialize single serial port
701 * @port: serial port number (0-3)
702 *
703 * This function initialies serial driver for given @port only.
704 * Platforms can call this function instead of omap_serial_init()
705 * if they don't plan to use all available UARTs as serial ports.
706 *
707 * Don't mix calls to omap_serial_init_port() and omap_serial_init(),
708 * use only one of the two.
709 */
710void __init omap_serial_init_port(int port)
711{
712 struct omap_uart_state *uart;
Kevin Hilman6f251e92010-09-27 20:19:38 +0530713 struct omap_hwmod *oh;
714 struct omap_device *od;
715 void *pdata = NULL;
716 u32 pdata_size = 0;
717 char *name;
718#ifndef CONFIG_SERIAL_OMAP
719 struct plat_serial8250_port ports[2] = {
720 {},
721 {.flags = 0},
722 };
723 struct plat_serial8250_port *p = &ports[0];
724#else
725 struct omap_uart_port_info omap_up;
726#endif
Mika Westerbergf62349e2009-12-11 16:16:35 -0800727
Kevin Hilman6f251e92010-09-27 20:19:38 +0530728 if (WARN_ON(port < 0))
Sergio Aguirree88d5562010-02-27 14:13:43 -0600729 return;
Kevin Hilman6f251e92010-09-27 20:19:38 +0530730 if (WARN_ON(port >= num_uarts))
Mika Westerbergf62349e2009-12-11 16:16:35 -0800731 return;
732
Kevin Hilman6f251e92010-09-27 20:19:38 +0530733 list_for_each_entry(uart, &uart_list, node)
734 if (port == uart->num)
735 break;
736
737 oh = uart->oh;
738 uart->dma_enabled = 0;
739#ifndef CONFIG_SERIAL_OMAP
740 name = "serial8250";
741
742 /*
743 * !! 8250 driver does not use standard IORESOURCE* It
744 * has it's own custom pdata that can be taken from
745 * the hwmod resource data. But, this needs to be
746 * done after the build.
747 *
748 * ?? does it have to be done before the register ??
749 * YES, because platform_device_data_add() copies
750 * pdata, it does not use a pointer.
751 */
752 p->flags = UPF_BOOT_AUTOCONF;
753 p->iotype = UPIO_MEM;
754 p->regshift = 2;
755 p->uartclk = OMAP24XX_BASE_BAUD * 16;
756 p->irq = oh->mpu_irqs[0].irq;
757 p->mapbase = oh->slaves[0]->addr->pa_start;
758 p->membase = omap_hwmod_get_mpu_rt_va(oh);
759 p->irqflags = IRQF_SHARED;
760 p->private_data = uart;
Mika Westerbergf62349e2009-12-11 16:16:35 -0800761
vikram pandita30e53bc2010-02-15 10:03:33 -0800762 /*
763 * omap44xx: Never read empty UART fifo
764 * omap3xxx: Never read empty UART fifo on UARTs
765 * with IP rev >=0x52
766 */
Kevin Hilman6f251e92010-09-27 20:19:38 +0530767 uart->regshift = p->regshift;
768 uart->membase = p->membase;
Nishanth Menon5a927b32010-08-02 13:18:12 +0300769 if (cpu_is_omap44xx())
770 uart->errata |= UART_ERRATA_FIFO_FULL_ABORT;
Kevin Hilman6f251e92010-09-27 20:19:38 +0530771 else if ((serial_read_reg(uart, UART_OMAP_MVER) & 0xFF)
Nishanth Menon5a927b32010-08-02 13:18:12 +0300772 >= UART_OMAP_NO_EMPTY_FIFO_READ_IP_REV)
773 uart->errata |= UART_ERRATA_FIFO_FULL_ABORT;
774
775 if (uart->errata & UART_ERRATA_FIFO_FULL_ABORT) {
Kevin Hilman6f251e92010-09-27 20:19:38 +0530776 p->serial_in = serial_in_override;
777 p->serial_out = serial_out_override;
778 }
779
780 pdata = &ports[0];
781 pdata_size = 2 * sizeof(struct plat_serial8250_port);
782#else
783
784 name = DRIVER_NAME;
785
786 omap_up.dma_enabled = uart->dma_enabled;
787 omap_up.uartclk = OMAP24XX_BASE_BAUD * 16;
788 omap_up.mapbase = oh->slaves[0]->addr->pa_start;
789 omap_up.membase = omap_hwmod_get_mpu_rt_va(oh);
790 omap_up.irqflags = IRQF_SHARED;
791 omap_up.flags = UPF_BOOT_AUTOCONF | UPF_SHARE_IRQ;
792
793 pdata = &omap_up;
794 pdata_size = sizeof(struct omap_uart_port_info);
795#endif
796
797 if (WARN_ON(!oh))
798 return;
799
800 od = omap_device_build(name, uart->num, oh, pdata, pdata_size,
801 omap_uart_latency,
802 ARRAY_SIZE(omap_uart_latency), false);
803 WARN(IS_ERR(od), "Could not build omap_device for %s: %s.\n",
804 name, oh->name);
805
806 uart->irq = oh->mpu_irqs[0].irq;
807 uart->regshift = 2;
808 uart->mapbase = oh->slaves[0]->addr->pa_start;
809 uart->membase = omap_hwmod_get_mpu_rt_va(oh);
810 uart->pdev = &od->pdev;
811
812 oh->dev_attr = uart;
813
Paul Walmsley0d8e2d02010-11-24 16:49:05 -0700814 acquire_console_sem(); /* in case the earlycon is on the UART */
815
Kevin Hilman6f251e92010-09-27 20:19:38 +0530816 /*
817 * Because of early UART probing, UART did not get idled
818 * on init. Now that omap_device is ready, ensure full idle
819 * before doing omap_device_enable().
820 */
821 omap_hwmod_idle(uart->oh);
822
823 omap_device_enable(uart->pdev);
824 omap_uart_idle_init(uart);
825 omap_uart_reset(uart);
826 omap_hwmod_enable_wakeup(uart->oh);
827 omap_device_idle(uart->pdev);
828
829 /*
830 * Need to block sleep long enough for interrupt driven
831 * driver to start. Console driver is in polling mode
832 * so device needs to be kept enabled while polling driver
833 * is in use.
834 */
835 if (uart->timeout)
836 uart->timeout = (30 * HZ);
837 omap_uart_block_sleep(uart);
838 uart->timeout = DEFAULT_TIMEOUT;
839
Paul Walmsley0d8e2d02010-11-24 16:49:05 -0700840 release_console_sem();
841
Kevin Hilman6f251e92010-09-27 20:19:38 +0530842 if ((cpu_is_omap34xx() && uart->padconf) ||
843 (uart->wk_en && uart->wk_mask)) {
844 device_init_wakeup(&od->pdev.dev, true);
845 DEV_CREATE_FILE(&od->pdev.dev, &dev_attr_sleep_timeout);
Santosh Shilimkare03d37d2010-02-18 08:59:06 +0000846 }
Deepak K00034502010-08-02 13:18:12 +0300847
848 /* Enable the MDR1 errata for OMAP3 */
849 if (cpu_is_omap34xx())
850 uart->errata |= UART_ERRATA_i202_MDR1_ACCESS;
Mika Westerbergf62349e2009-12-11 16:16:35 -0800851}
852
853/**
854 * omap_serial_init() - intialize all supported serial ports
855 *
856 * Initializes all available UARTs as serial ports. Platforms
857 * can call this function when they want to have default behaviour
858 * for serial ports (e.g initialize them all as serial ports).
859 */
Paul Walmsleyb3c6df32009-09-03 20:14:02 +0300860void __init omap_serial_init(void)
861{
Kevin Hilman6f251e92010-09-27 20:19:38 +0530862 struct omap_uart_state *uart;
Paul Walmsleyb3c6df32009-09-03 20:14:02 +0300863
Kevin Hilman6f251e92010-09-27 20:19:38 +0530864 list_for_each_entry(uart, &uart_list, node)
865 omap_serial_init_port(uart->num);
Tony Lindgren1dbae812005-11-10 14:26:51 +0000866}