blob: c64578853a8d5235f0b46f10ae89e556b31cf4d2 [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
Paul Walmsley59fb6592010-12-21 15:30:55 -070043#include "prm2xxx_3xxx.h"
Kevin Hilman4af40162009-02-04 10:51:40 -080044#include "pm.h"
Paul Walmsley59fb6592010-12-21 15:30:55 -070045#include "cm2xxx_3xxx.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"
Tony Lindgren40e44392010-12-22 18:42:35 -080048#include "mux.h"
Kevin Hilman4af40162009-02-04 10:51:40 -080049
vikram panditace13d472009-12-11 16:16:37 -080050#define UART_OMAP_NO_EMPTY_FIFO_READ_IP_REV 0x52
Kevin Hilman4af40162009-02-04 10:51:40 -080051#define UART_OMAP_WER 0x17 /* Wake-up enable register */
52
Nishanth Menon5a927b32010-08-02 13:18:12 +030053#define UART_ERRATA_FIFO_FULL_ABORT (0x1 << 0)
Deepak K00034502010-08-02 13:18:12 +030054#define UART_ERRATA_i202_MDR1_ACCESS (0x1 << 1)
Nishanth Menon5a927b32010-08-02 13:18:12 +030055
Tony Lindgren301fe8e2010-02-01 12:34:31 -080056/*
57 * NOTE: By default the serial timeout is disabled as it causes lost characters
58 * over the serial ports. This means that the UART clocks will stay on until
59 * disabled via sysfs. This also causes that any deeper omap sleep states are
60 * blocked.
61 */
62#define DEFAULT_TIMEOUT 0
Kevin Hilman4af40162009-02-04 10:51:40 -080063
Kevin Hilman6f251e92010-09-27 20:19:38 +053064#define MAX_UART_HWMOD_NAME_LEN 16
65
Kevin Hilman4af40162009-02-04 10:51:40 -080066struct omap_uart_state {
67 int num;
68 int can_sleep;
69 struct timer_list timer;
70 u32 timeout;
71
72 void __iomem *wk_st;
73 void __iomem *wk_en;
74 u32 wk_mask;
75 u32 padconf;
Kevin Hilman6f251e92010-09-27 20:19:38 +053076 u32 dma_enabled;
Kevin Hilman4af40162009-02-04 10:51:40 -080077
78 struct clk *ick;
79 struct clk *fck;
80 int clocked;
81
Kevin Hilman6f251e92010-09-27 20:19:38 +053082 int irq;
83 int regshift;
84 int irqflags;
85 void __iomem *membase;
86 resource_size_t mapbase;
87
Kevin Hilman4af40162009-02-04 10:51:40 -080088 struct list_head node;
Kevin Hilman6f251e92010-09-27 20:19:38 +053089 struct omap_hwmod *oh;
90 struct platform_device *pdev;
Kevin Hilman4af40162009-02-04 10:51:40 -080091
Nishanth Menon5a927b32010-08-02 13:18:12 +030092 u32 errata;
Kevin Hilman4af40162009-02-04 10:51:40 -080093#if defined(CONFIG_ARCH_OMAP3) && defined(CONFIG_PM)
94 int context_valid;
95
96 /* Registers to be saved/restored for OFF-mode */
97 u16 dll;
98 u16 dlh;
99 u16 ier;
100 u16 sysc;
101 u16 scr;
102 u16 wer;
Govindraj R5ade4ff2010-08-02 13:18:11 +0300103 u16 mcr;
Kevin Hilman4af40162009-02-04 10:51:40 -0800104#endif
105};
106
Kevin Hilman4af40162009-02-04 10:51:40 -0800107static LIST_HEAD(uart_list);
Kevin Hilman6f251e92010-09-27 20:19:38 +0530108static u8 num_uarts;
Tony Lindgren1dbae812005-11-10 14:26:51 +0000109
Kevin Hilman8da37d92010-09-27 20:20:06 +0530110static int uart_idle_hwmod(struct omap_device *od)
111{
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -0700112 omap_hwmod_idle(od->hwmods[0]);
Kevin Hilman8da37d92010-09-27 20:20:06 +0530113
114 return 0;
115}
116
117static int uart_enable_hwmod(struct omap_device *od)
118{
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -0700119 omap_hwmod_enable(od->hwmods[0]);
Kevin Hilman8da37d92010-09-27 20:20:06 +0530120
121 return 0;
122}
123
Kevin Hilman6f251e92010-09-27 20:19:38 +0530124static struct omap_device_pm_latency omap_uart_latency[] = {
125 {
Kevin Hilman8da37d92010-09-27 20:20:06 +0530126 .deactivate_func = uart_idle_hwmod,
127 .activate_func = uart_enable_hwmod,
Kevin Hilman6f251e92010-09-27 20:19:38 +0530128 .flags = OMAP_DEVICE_LATENCY_AUTO_ADJUST,
129 },
130};
131
Alexander Shishkin92303722010-01-08 10:29:06 -0800132static inline unsigned int __serial_read_reg(struct uart_port *up,
Kevin Hilman6f251e92010-09-27 20:19:38 +0530133 int offset)
Alexander Shishkin92303722010-01-08 10:29:06 -0800134{
135 offset <<= up->regshift;
136 return (unsigned int)__raw_readb(up->membase + offset);
137}
138
Kevin Hilman6f251e92010-09-27 20:19:38 +0530139static inline unsigned int serial_read_reg(struct omap_uart_state *uart,
Tony Lindgren1dbae812005-11-10 14:26:51 +0000140 int offset)
141{
Kevin Hilman6f251e92010-09-27 20:19:38 +0530142 offset <<= uart->regshift;
143 return (unsigned int)__raw_readb(uart->membase + offset);
Tony Lindgren1dbae812005-11-10 14:26:51 +0000144}
145
Santosh Shilimkare03d37d2010-02-18 08:59:06 +0000146static inline void __serial_write_reg(struct uart_port *up, int offset,
147 int value)
148{
149 offset <<= up->regshift;
150 __raw_writeb(value, up->membase + offset);
151}
152
Kevin Hilman6f251e92010-09-27 20:19:38 +0530153static inline void serial_write_reg(struct omap_uart_state *uart, int offset,
Tony Lindgren1dbae812005-11-10 14:26:51 +0000154 int value)
155{
Kevin Hilman6f251e92010-09-27 20:19:38 +0530156 offset <<= uart->regshift;
157 __raw_writeb(value, uart->membase + offset);
Tony Lindgren1dbae812005-11-10 14:26:51 +0000158}
159
160/*
161 * Internal UARTs need to be initialized for the 8250 autoconfig to work
162 * properly. Note that the TX watermark initialization may not be needed
163 * once the 8250.c watermark handling code is merged.
164 */
Kevin Hilman6f251e92010-09-27 20:19:38 +0530165
Kevin Hilman4af40162009-02-04 10:51:40 -0800166static inline void __init omap_uart_reset(struct omap_uart_state *uart)
Tony Lindgren1dbae812005-11-10 14:26:51 +0000167{
Andrei Emeltchenko498cb952010-11-30 14:11:49 -0800168 serial_write_reg(uart, UART_OMAP_MDR1, UART_OMAP_MDR1_DISABLE);
Kevin Hilman6f251e92010-09-27 20:19:38 +0530169 serial_write_reg(uart, UART_OMAP_SCR, 0x08);
Andrei Emeltchenko498cb952010-11-30 14:11:49 -0800170 serial_write_reg(uart, UART_OMAP_MDR1, UART_OMAP_MDR1_16X_MODE);
Tony Lindgren1dbae812005-11-10 14:26:51 +0000171}
172
Kevin Hilman4af40162009-02-04 10:51:40 -0800173#if defined(CONFIG_PM) && defined(CONFIG_ARCH_OMAP3)
174
Deepak K00034502010-08-02 13:18:12 +0300175/*
176 * Work Around for Errata i202 (3430 - 1.12, 3630 - 1.6)
177 * The access to uart register after MDR1 Access
178 * causes UART to corrupt data.
179 *
180 * Need a delay =
181 * 5 L4 clock cycles + 5 UART functional clock cycle (@48MHz = ~0.2uS)
182 * give 10 times as much
183 */
184static void omap_uart_mdr1_errataset(struct omap_uart_state *uart, u8 mdr1_val,
185 u8 fcr_val)
186{
Deepak K00034502010-08-02 13:18:12 +0300187 u8 timeout = 255;
188
Kevin Hilman6f251e92010-09-27 20:19:38 +0530189 serial_write_reg(uart, UART_OMAP_MDR1, mdr1_val);
Deepak K00034502010-08-02 13:18:12 +0300190 udelay(2);
Kevin Hilman6f251e92010-09-27 20:19:38 +0530191 serial_write_reg(uart, UART_FCR, fcr_val | UART_FCR_CLEAR_XMIT |
Deepak K00034502010-08-02 13:18:12 +0300192 UART_FCR_CLEAR_RCVR);
193 /*
194 * Wait for FIFO to empty: when empty, RX_FIFO_E bit is 0 and
195 * TX_FIFO_E bit is 1.
196 */
Kevin Hilman6f251e92010-09-27 20:19:38 +0530197 while (UART_LSR_THRE != (serial_read_reg(uart, UART_LSR) &
Deepak K00034502010-08-02 13:18:12 +0300198 (UART_LSR_THRE | UART_LSR_DR))) {
199 timeout--;
200 if (!timeout) {
201 /* Should *never* happen. we warn and carry on */
Kevin Hilman6f251e92010-09-27 20:19:38 +0530202 dev_crit(&uart->pdev->dev, "Errata i202: timedout %x\n",
203 serial_read_reg(uart, UART_LSR));
Deepak K00034502010-08-02 13:18:12 +0300204 break;
205 }
206 udelay(1);
207 }
208}
209
Kevin Hilman4af40162009-02-04 10:51:40 -0800210static void omap_uart_save_context(struct omap_uart_state *uart)
Jouni Hogander6e811762008-10-06 15:49:15 +0300211{
Kevin Hilman4af40162009-02-04 10:51:40 -0800212 u16 lcr = 0;
Kevin Hilman4af40162009-02-04 10:51:40 -0800213
214 if (!enable_off_mode)
215 return;
216
Kevin Hilman6f251e92010-09-27 20:19:38 +0530217 lcr = serial_read_reg(uart, UART_LCR);
Andrei Emeltchenko662b083a2010-11-30 14:11:49 -0800218 serial_write_reg(uart, UART_LCR, UART_LCR_CONF_MODE_B);
Kevin Hilman6f251e92010-09-27 20:19:38 +0530219 uart->dll = serial_read_reg(uart, UART_DLL);
220 uart->dlh = serial_read_reg(uart, UART_DLM);
221 serial_write_reg(uart, UART_LCR, lcr);
222 uart->ier = serial_read_reg(uart, UART_IER);
223 uart->sysc = serial_read_reg(uart, UART_OMAP_SYSC);
224 uart->scr = serial_read_reg(uart, UART_OMAP_SCR);
225 uart->wer = serial_read_reg(uart, UART_OMAP_WER);
Andrei Emeltchenko662b083a2010-11-30 14:11:49 -0800226 serial_write_reg(uart, UART_LCR, UART_LCR_CONF_MODE_A);
Kevin Hilman6f251e92010-09-27 20:19:38 +0530227 uart->mcr = serial_read_reg(uart, UART_MCR);
228 serial_write_reg(uart, UART_LCR, lcr);
Kevin Hilman4af40162009-02-04 10:51:40 -0800229
230 uart->context_valid = 1;
231}
232
233static void omap_uart_restore_context(struct omap_uart_state *uart)
234{
235 u16 efr = 0;
Kevin Hilman4af40162009-02-04 10:51:40 -0800236
237 if (!enable_off_mode)
238 return;
239
240 if (!uart->context_valid)
241 return;
242
243 uart->context_valid = 0;
244
Deepak K00034502010-08-02 13:18:12 +0300245 if (uart->errata & UART_ERRATA_i202_MDR1_ACCESS)
Andrei Emeltchenko498cb952010-11-30 14:11:49 -0800246 omap_uart_mdr1_errataset(uart, UART_OMAP_MDR1_DISABLE, 0xA0);
Deepak K00034502010-08-02 13:18:12 +0300247 else
Andrei Emeltchenko498cb952010-11-30 14:11:49 -0800248 serial_write_reg(uart, UART_OMAP_MDR1, UART_OMAP_MDR1_DISABLE);
249
Andrei Emeltchenko662b083a2010-11-30 14:11:49 -0800250 serial_write_reg(uart, UART_LCR, UART_LCR_CONF_MODE_B);
Kevin Hilman6f251e92010-09-27 20:19:38 +0530251 efr = serial_read_reg(uart, UART_EFR);
252 serial_write_reg(uart, UART_EFR, UART_EFR_ECB);
253 serial_write_reg(uart, UART_LCR, 0x0); /* Operational mode */
254 serial_write_reg(uart, UART_IER, 0x0);
Andrei Emeltchenko662b083a2010-11-30 14:11:49 -0800255 serial_write_reg(uart, UART_LCR, UART_LCR_CONF_MODE_B);
Kevin Hilman6f251e92010-09-27 20:19:38 +0530256 serial_write_reg(uart, UART_DLL, uart->dll);
257 serial_write_reg(uart, UART_DLM, uart->dlh);
258 serial_write_reg(uart, UART_LCR, 0x0); /* Operational mode */
259 serial_write_reg(uart, UART_IER, uart->ier);
Andrei Emeltchenko662b083a2010-11-30 14:11:49 -0800260 serial_write_reg(uart, UART_LCR, UART_LCR_CONF_MODE_A);
Kevin Hilman6f251e92010-09-27 20:19:38 +0530261 serial_write_reg(uart, UART_MCR, uart->mcr);
Andrei Emeltchenko662b083a2010-11-30 14:11:49 -0800262 serial_write_reg(uart, UART_LCR, UART_LCR_CONF_MODE_B);
Kevin Hilman6f251e92010-09-27 20:19:38 +0530263 serial_write_reg(uart, UART_EFR, efr);
264 serial_write_reg(uart, UART_LCR, UART_LCR_WLEN8);
265 serial_write_reg(uart, UART_OMAP_SCR, uart->scr);
266 serial_write_reg(uart, UART_OMAP_WER, uart->wer);
267 serial_write_reg(uart, UART_OMAP_SYSC, uart->sysc);
Andrei Emeltchenko498cb952010-11-30 14:11:49 -0800268
Deepak K00034502010-08-02 13:18:12 +0300269 if (uart->errata & UART_ERRATA_i202_MDR1_ACCESS)
Andrei Emeltchenko498cb952010-11-30 14:11:49 -0800270 omap_uart_mdr1_errataset(uart, UART_OMAP_MDR1_16X_MODE, 0xA1);
Deepak K00034502010-08-02 13:18:12 +0300271 else
Kevin Hilman6f251e92010-09-27 20:19:38 +0530272 /* UART 16x mode */
Andrei Emeltchenko498cb952010-11-30 14:11:49 -0800273 serial_write_reg(uart, UART_OMAP_MDR1,
274 UART_OMAP_MDR1_16X_MODE);
Kevin Hilman4af40162009-02-04 10:51:40 -0800275}
276#else
277static inline void omap_uart_save_context(struct omap_uart_state *uart) {}
278static inline void omap_uart_restore_context(struct omap_uart_state *uart) {}
279#endif /* CONFIG_PM && CONFIG_ARCH_OMAP3 */
280
281static inline void omap_uart_enable_clocks(struct omap_uart_state *uart)
282{
283 if (uart->clocked)
284 return;
285
Kevin Hilman6f251e92010-09-27 20:19:38 +0530286 omap_device_enable(uart->pdev);
Kevin Hilman4af40162009-02-04 10:51:40 -0800287 uart->clocked = 1;
288 omap_uart_restore_context(uart);
289}
290
291#ifdef CONFIG_PM
292
293static inline void omap_uart_disable_clocks(struct omap_uart_state *uart)
294{
295 if (!uart->clocked)
296 return;
297
298 omap_uart_save_context(uart);
299 uart->clocked = 0;
Kevin Hilman6f251e92010-09-27 20:19:38 +0530300 omap_device_idle(uart->pdev);
Kevin Hilman4af40162009-02-04 10:51:40 -0800301}
302
Kevin Hilmanfd455ea2009-04-27 12:27:36 -0700303static void omap_uart_enable_wakeup(struct omap_uart_state *uart)
304{
305 /* Set wake-enable bit */
306 if (uart->wk_en && uart->wk_mask) {
307 u32 v = __raw_readl(uart->wk_en);
308 v |= uart->wk_mask;
309 __raw_writel(v, uart->wk_en);
310 }
311
312 /* Ensure IOPAD wake-enables are set */
313 if (cpu_is_omap34xx() && uart->padconf) {
314 u16 v = omap_ctrl_readw(uart->padconf);
315 v |= OMAP3_PADCONF_WAKEUPENABLE0;
316 omap_ctrl_writew(v, uart->padconf);
317 }
318}
319
320static void omap_uart_disable_wakeup(struct omap_uart_state *uart)
321{
322 /* Clear wake-enable bit */
323 if (uart->wk_en && uart->wk_mask) {
324 u32 v = __raw_readl(uart->wk_en);
325 v &= ~uart->wk_mask;
326 __raw_writel(v, uart->wk_en);
327 }
328
329 /* Ensure IOPAD wake-enables are cleared */
330 if (cpu_is_omap34xx() && uart->padconf) {
331 u16 v = omap_ctrl_readw(uart->padconf);
332 v &= ~OMAP3_PADCONF_WAKEUPENABLE0;
333 omap_ctrl_writew(v, uart->padconf);
334 }
335}
336
Kevin Hilman4af40162009-02-04 10:51:40 -0800337static void omap_uart_smart_idle_enable(struct omap_uart_state *uart,
Kevin Hilman6f251e92010-09-27 20:19:38 +0530338 int enable)
Kevin Hilman4af40162009-02-04 10:51:40 -0800339{
Kevin Hilman6f251e92010-09-27 20:19:38 +0530340 u8 idlemode;
Kevin Hilman4af40162009-02-04 10:51:40 -0800341
Kevin Hilman6f251e92010-09-27 20:19:38 +0530342 if (enable) {
343 /**
344 * Errata 2.15: [UART]:Cannot Acknowledge Idle Requests
345 * in Smartidle Mode When Configured for DMA Operations.
346 */
347 if (uart->dma_enabled)
348 idlemode = HWMOD_IDLEMODE_FORCE;
349 else
350 idlemode = HWMOD_IDLEMODE_SMART;
351 } else {
352 idlemode = HWMOD_IDLEMODE_NO;
353 }
Kevin Hilman4af40162009-02-04 10:51:40 -0800354
Kevin Hilman6f251e92010-09-27 20:19:38 +0530355 omap_hwmod_set_slave_idlemode(uart->oh, idlemode);
Kevin Hilman4af40162009-02-04 10:51:40 -0800356}
357
358static void omap_uart_block_sleep(struct omap_uart_state *uart)
359{
360 omap_uart_enable_clocks(uart);
361
362 omap_uart_smart_idle_enable(uart, 0);
363 uart->can_sleep = 0;
Jouni Hoganderba87a9b2008-12-09 13:36:50 +0200364 if (uart->timeout)
365 mod_timer(&uart->timer, jiffies + uart->timeout);
366 else
367 del_timer(&uart->timer);
Kevin Hilman4af40162009-02-04 10:51:40 -0800368}
369
370static void omap_uart_allow_sleep(struct omap_uart_state *uart)
371{
Kevin Hilman6f251e92010-09-27 20:19:38 +0530372 if (device_may_wakeup(&uart->pdev->dev))
Kevin Hilmanfd455ea2009-04-27 12:27:36 -0700373 omap_uart_enable_wakeup(uart);
374 else
375 omap_uart_disable_wakeup(uart);
376
Kevin Hilman4af40162009-02-04 10:51:40 -0800377 if (!uart->clocked)
378 return;
379
380 omap_uart_smart_idle_enable(uart, 1);
381 uart->can_sleep = 1;
382 del_timer(&uart->timer);
383}
384
385static void omap_uart_idle_timer(unsigned long data)
386{
387 struct omap_uart_state *uart = (struct omap_uart_state *)data;
388
389 omap_uart_allow_sleep(uart);
390}
391
392void omap_uart_prepare_idle(int num)
393{
394 struct omap_uart_state *uart;
395
396 list_for_each_entry(uart, &uart_list, node) {
397 if (num == uart->num && uart->can_sleep) {
398 omap_uart_disable_clocks(uart);
399 return;
Jouni Hogander6e811762008-10-06 15:49:15 +0300400 }
401 }
402}
403
Kevin Hilman4af40162009-02-04 10:51:40 -0800404void omap_uart_resume_idle(int num)
405{
406 struct omap_uart_state *uart;
407
408 list_for_each_entry(uart, &uart_list, node) {
Kevin Hilmanf9100432010-11-24 11:09:03 -0800409 if (num == uart->num && uart->can_sleep) {
Kevin Hilman4af40162009-02-04 10:51:40 -0800410 omap_uart_enable_clocks(uart);
411
412 /* Check for IO pad wakeup */
413 if (cpu_is_omap34xx() && uart->padconf) {
414 u16 p = omap_ctrl_readw(uart->padconf);
415
416 if (p & OMAP3_PADCONF_WAKEUPEVENT0)
417 omap_uart_block_sleep(uart);
418 }
419
420 /* Check for normal UART wakeup */
421 if (__raw_readl(uart->wk_st) & uart->wk_mask)
422 omap_uart_block_sleep(uart);
Kevin Hilman4af40162009-02-04 10:51:40 -0800423 return;
424 }
425 }
426}
427
428void omap_uart_prepare_suspend(void)
429{
430 struct omap_uart_state *uart;
431
432 list_for_each_entry(uart, &uart_list, node) {
433 omap_uart_allow_sleep(uart);
434 }
435}
436
437int omap_uart_can_sleep(void)
438{
439 struct omap_uart_state *uart;
440 int can_sleep = 1;
441
442 list_for_each_entry(uart, &uart_list, node) {
443 if (!uart->clocked)
444 continue;
445
446 if (!uart->can_sleep) {
447 can_sleep = 0;
448 continue;
449 }
450
451 /* This UART can now safely sleep. */
452 omap_uart_allow_sleep(uart);
453 }
454
455 return can_sleep;
456}
457
458/**
459 * omap_uart_interrupt()
460 *
461 * This handler is used only to detect that *any* UART interrupt has
462 * occurred. It does _nothing_ to handle the interrupt. Rather,
463 * any UART interrupt will trigger the inactivity timer so the
464 * UART will not idle or sleep for its timeout period.
465 *
466 **/
Kevin Hilman6f251e92010-09-27 20:19:38 +0530467/* static int first_interrupt; */
Kevin Hilman4af40162009-02-04 10:51:40 -0800468static irqreturn_t omap_uart_interrupt(int irq, void *dev_id)
469{
470 struct omap_uart_state *uart = dev_id;
471
472 omap_uart_block_sleep(uart);
473
474 return IRQ_NONE;
475}
476
477static void omap_uart_idle_init(struct omap_uart_state *uart)
478{
Kevin Hilman4af40162009-02-04 10:51:40 -0800479 int ret;
480
481 uart->can_sleep = 0;
Kevin Hilmanfd455ea2009-04-27 12:27:36 -0700482 uart->timeout = DEFAULT_TIMEOUT;
Kevin Hilman4af40162009-02-04 10:51:40 -0800483 setup_timer(&uart->timer, omap_uart_idle_timer,
484 (unsigned long) uart);
Tony Lindgren301fe8e2010-02-01 12:34:31 -0800485 if (uart->timeout)
486 mod_timer(&uart->timer, jiffies + uart->timeout);
Kevin Hilman4af40162009-02-04 10:51:40 -0800487 omap_uart_smart_idle_enable(uart, 0);
488
489 if (cpu_is_omap34xx()) {
Govindraj.R52663ae2010-09-27 20:20:41 +0530490 u32 mod = (uart->num > 1) ? OMAP3430_PER_MOD : CORE_MOD;
Kevin Hilman4af40162009-02-04 10:51:40 -0800491 u32 wk_mask = 0;
492 u32 padconf = 0;
493
Paul Walmsleyc4d7e582010-12-21 21:05:14 -0700494 /* XXX These PRM accesses do not belong here */
Kevin Hilman4af40162009-02-04 10:51:40 -0800495 uart->wk_en = OMAP34XX_PRM_REGADDR(mod, PM_WKEN1);
496 uart->wk_st = OMAP34XX_PRM_REGADDR(mod, PM_WKST1);
497 switch (uart->num) {
498 case 0:
499 wk_mask = OMAP3430_ST_UART1_MASK;
500 padconf = 0x182;
501 break;
502 case 1:
503 wk_mask = OMAP3430_ST_UART2_MASK;
504 padconf = 0x17a;
505 break;
506 case 2:
507 wk_mask = OMAP3430_ST_UART3_MASK;
508 padconf = 0x19e;
509 break;
Govindraj.R52663ae2010-09-27 20:20:41 +0530510 case 3:
511 wk_mask = OMAP3630_ST_UART4_MASK;
512 padconf = 0x0d2;
513 break;
Kevin Hilman4af40162009-02-04 10:51:40 -0800514 }
515 uart->wk_mask = wk_mask;
516 uart->padconf = padconf;
517 } else if (cpu_is_omap24xx()) {
518 u32 wk_mask = 0;
Kevin Hilmancb74f022010-10-20 23:19:03 +0000519 u32 wk_en = PM_WKEN1, wk_st = PM_WKST1;
Kevin Hilman4af40162009-02-04 10:51:40 -0800520
Kevin Hilman4af40162009-02-04 10:51:40 -0800521 switch (uart->num) {
522 case 0:
523 wk_mask = OMAP24XX_ST_UART1_MASK;
524 break;
525 case 1:
526 wk_mask = OMAP24XX_ST_UART2_MASK;
527 break;
528 case 2:
Kevin Hilmancb74f022010-10-20 23:19:03 +0000529 wk_en = OMAP24XX_PM_WKEN2;
530 wk_st = OMAP24XX_PM_WKST2;
Kevin Hilman4af40162009-02-04 10:51:40 -0800531 wk_mask = OMAP24XX_ST_UART3_MASK;
532 break;
533 }
534 uart->wk_mask = wk_mask;
Kevin Hilmancb74f022010-10-20 23:19:03 +0000535 if (cpu_is_omap2430()) {
536 uart->wk_en = OMAP2430_PRM_REGADDR(CORE_MOD, wk_en);
537 uart->wk_st = OMAP2430_PRM_REGADDR(CORE_MOD, wk_st);
538 } else if (cpu_is_omap2420()) {
539 uart->wk_en = OMAP2420_PRM_REGADDR(CORE_MOD, wk_en);
540 uart->wk_st = OMAP2420_PRM_REGADDR(CORE_MOD, wk_st);
541 }
Kevin Hilman4af40162009-02-04 10:51:40 -0800542 } else {
Nishanth Menonc54bae12010-08-02 13:18:11 +0300543 uart->wk_en = NULL;
544 uart->wk_st = NULL;
Kevin Hilman4af40162009-02-04 10:51:40 -0800545 uart->wk_mask = 0;
546 uart->padconf = 0;
547 }
548
Kevin Hilman6f251e92010-09-27 20:19:38 +0530549 uart->irqflags |= IRQF_SHARED;
550 ret = request_threaded_irq(uart->irq, NULL, omap_uart_interrupt,
551 IRQF_SHARED, "serial idle", (void *)uart);
Kevin Hilman4af40162009-02-04 10:51:40 -0800552 WARN_ON(ret);
553}
554
Tero Kristo24662112009-03-05 16:32:23 +0200555void omap_uart_enable_irqs(int enable)
Jouni Hoganderba87a9b2008-12-09 13:36:50 +0200556{
Tero Kristo24662112009-03-05 16:32:23 +0200557 int ret;
558 struct omap_uart_state *uart;
559
560 list_for_each_entry(uart, &uart_list, node) {
Kevin Hilman3244fcd2010-09-27 20:19:53 +0530561 if (enable) {
562 pm_runtime_put_sync(&uart->pdev->dev);
Kevin Hilman6f251e92010-09-27 20:19:38 +0530563 ret = request_threaded_irq(uart->irq, NULL,
564 omap_uart_interrupt,
565 IRQF_SHARED,
566 "serial idle",
567 (void *)uart);
Kevin Hilman3244fcd2010-09-27 20:19:53 +0530568 } else {
569 pm_runtime_get_noresume(&uart->pdev->dev);
Kevin Hilman6f251e92010-09-27 20:19:38 +0530570 free_irq(uart->irq, (void *)uart);
Kevin Hilman3244fcd2010-09-27 20:19:53 +0530571 }
Tero Kristo24662112009-03-05 16:32:23 +0200572 }
Jouni Hoganderba87a9b2008-12-09 13:36:50 +0200573}
574
Kevin Hilmanfd455ea2009-04-27 12:27:36 -0700575static ssize_t sleep_timeout_show(struct device *dev,
576 struct device_attribute *attr,
Jouni Hoganderba87a9b2008-12-09 13:36:50 +0200577 char *buf)
578{
Kevin Hilman6f251e92010-09-27 20:19:38 +0530579 struct platform_device *pdev = to_platform_device(dev);
580 struct omap_device *odev = to_omap_device(pdev);
581 struct omap_uart_state *uart = odev->hwmods[0]->dev_attr;
Kevin Hilmanfd455ea2009-04-27 12:27:36 -0700582
583 return sprintf(buf, "%u\n", uart->timeout / HZ);
Jouni Hoganderba87a9b2008-12-09 13:36:50 +0200584}
585
Kevin Hilmanfd455ea2009-04-27 12:27:36 -0700586static ssize_t sleep_timeout_store(struct device *dev,
587 struct device_attribute *attr,
Jouni Hoganderba87a9b2008-12-09 13:36:50 +0200588 const char *buf, size_t n)
589{
Kevin Hilman6f251e92010-09-27 20:19:38 +0530590 struct platform_device *pdev = to_platform_device(dev);
591 struct omap_device *odev = to_omap_device(pdev);
592 struct omap_uart_state *uart = odev->hwmods[0]->dev_attr;
Jouni Hoganderba87a9b2008-12-09 13:36:50 +0200593 unsigned int value;
594
595 if (sscanf(buf, "%u", &value) != 1) {
Sergio Aguirre10c805e2010-03-09 13:22:14 -0600596 dev_err(dev, "sleep_timeout_store: Invalid value\n");
Jouni Hoganderba87a9b2008-12-09 13:36:50 +0200597 return -EINVAL;
598 }
Kevin Hilmanfd455ea2009-04-27 12:27:36 -0700599
600 uart->timeout = value * HZ;
601 if (uart->timeout)
602 mod_timer(&uart->timer, jiffies + uart->timeout);
603 else
604 /* A zero value means disable timeout feature */
605 omap_uart_block_sleep(uart);
606
Jouni Hoganderba87a9b2008-12-09 13:36:50 +0200607 return n;
608}
609
Nishanth Menonbfe69772010-08-02 13:18:12 +0300610static DEVICE_ATTR(sleep_timeout, 0644, sleep_timeout_show,
611 sleep_timeout_store);
Kevin Hilmanfd455ea2009-04-27 12:27:36 -0700612#define DEV_CREATE_FILE(dev, attr) WARN_ON(device_create_file(dev, attr))
Kevin Hilman4af40162009-02-04 10:51:40 -0800613#else
614static inline void omap_uart_idle_init(struct omap_uart_state *uart) {}
Santosh Shilimkara1b04cc2010-10-11 11:05:18 +0000615static void omap_uart_block_sleep(struct omap_uart_state *uart)
616{
617 /* Needed to enable UART clocks when built without CONFIG_PM */
618 omap_uart_enable_clocks(uart);
619}
Kevin Hilmanfd455ea2009-04-27 12:27:36 -0700620#define DEV_CREATE_FILE(dev, attr)
Kevin Hilman4af40162009-02-04 10:51:40 -0800621#endif /* CONFIG_PM */
622
Kevin Hilman6f251e92010-09-27 20:19:38 +0530623#ifndef CONFIG_SERIAL_OMAP
vikram panditace13d472009-12-11 16:16:37 -0800624/*
625 * Override the default 8250 read handler: mem_serial_in()
626 * Empty RX fifo read causes an abort on omap3630 and omap4
627 * This function makes sure that an empty rx fifo is not read on these silicons
628 * (OMAP1/2/3430 are not affected)
629 */
630static unsigned int serial_in_override(struct uart_port *up, int offset)
631{
632 if (UART_RX == offset) {
633 unsigned int lsr;
Alexander Shishkin92303722010-01-08 10:29:06 -0800634 lsr = __serial_read_reg(up, UART_LSR);
vikram panditace13d472009-12-11 16:16:37 -0800635 if (!(lsr & UART_LSR_DR))
636 return -EPERM;
637 }
Alexander Shishkin92303722010-01-08 10:29:06 -0800638
639 return __serial_read_reg(up, offset);
vikram panditace13d472009-12-11 16:16:37 -0800640}
641
Santosh Shilimkare03d37d2010-02-18 08:59:06 +0000642static void serial_out_override(struct uart_port *up, int offset, int value)
643{
644 unsigned int status, tmout = 10000;
645
646 status = __serial_read_reg(up, UART_LSR);
647 while (!(status & UART_LSR_THRE)) {
648 /* Wait up to 10ms for the character(s) to be sent. */
649 if (--tmout == 0)
650 break;
651 udelay(1);
652 status = __serial_read_reg(up, UART_LSR);
653 }
654 __serial_write_reg(up, offset, value);
655}
Kevin Hilman6f251e92010-09-27 20:19:38 +0530656#endif
657
Paul Walmsleyb3c6df32009-09-03 20:14:02 +0300658void __init omap_serial_early_init(void)
Tony Lindgren1dbae812005-11-10 14:26:51 +0000659{
Kevin Hilman6f251e92010-09-27 20:19:38 +0530660 int i = 0;
Tony Lindgren1dbae812005-11-10 14:26:51 +0000661
Kevin Hilman6f251e92010-09-27 20:19:38 +0530662 do {
663 char oh_name[MAX_UART_HWMOD_NAME_LEN];
664 struct omap_hwmod *oh;
665 struct omap_uart_state *uart;
Thomas Weber21b90342010-02-25 09:40:19 +0000666
Kevin Hilman6f251e92010-09-27 20:19:38 +0530667 snprintf(oh_name, MAX_UART_HWMOD_NAME_LEN,
668 "uart%d", i + 1);
669 oh = omap_hwmod_lookup(oh_name);
670 if (!oh)
671 break;
Tony Lindgren1dbae812005-11-10 14:26:51 +0000672
Kevin Hilman6f251e92010-09-27 20:19:38 +0530673 uart = kzalloc(sizeof(struct omap_uart_state), GFP_KERNEL);
674 if (WARN_ON(!uart))
675 return;
Tony Lindgren1dbae812005-11-10 14:26:51 +0000676
Kevin Hilman6f251e92010-09-27 20:19:38 +0530677 uart->oh = oh;
678 uart->num = i++;
679 list_add_tail(&uart->node, &uart_list);
680 num_uarts++;
681
Tony Lindgren84f90c92009-10-16 09:53:00 -0700682 /*
Kevin Hilman6f251e92010-09-27 20:19:38 +0530683 * NOTE: omap_hwmod_init() has not yet been called,
684 * so no hwmod functions will work yet.
Tony Lindgren84f90c92009-10-16 09:53:00 -0700685 */
Tony Lindgren84f90c92009-10-16 09:53:00 -0700686
Kevin Hilman6f251e92010-09-27 20:19:38 +0530687 /*
688 * During UART early init, device need to be probed
689 * to determine SoC specific init before omap_device
690 * is ready. Therefore, don't allow idle here
691 */
692 uart->oh->flags |= HWMOD_INIT_NO_IDLE | HWMOD_INIT_NO_RESET;
693 } while (1);
Paul Walmsleyb3c6df32009-09-03 20:14:02 +0300694}
695
Mika Westerbergf62349e2009-12-11 16:16:35 -0800696/**
697 * omap_serial_init_port() - initialize single serial port
Tony Lindgren40e44392010-12-22 18:42:35 -0800698 * @bdata: port specific board data pointer
Mika Westerbergf62349e2009-12-11 16:16:35 -0800699 *
Tony Lindgren40e44392010-12-22 18:42:35 -0800700 * This function initialies serial driver for given port only.
Mika Westerbergf62349e2009-12-11 16:16:35 -0800701 * Platforms can call this function instead of omap_serial_init()
702 * if they don't plan to use all available UARTs as serial ports.
703 *
704 * Don't mix calls to omap_serial_init_port() and omap_serial_init(),
705 * use only one of the two.
706 */
Tony Lindgren40e44392010-12-22 18:42:35 -0800707void __init omap_serial_init_port(struct omap_board_data *bdata)
Mika Westerbergf62349e2009-12-11 16:16:35 -0800708{
709 struct omap_uart_state *uart;
Kevin Hilman6f251e92010-09-27 20:19:38 +0530710 struct omap_hwmod *oh;
711 struct omap_device *od;
712 void *pdata = NULL;
713 u32 pdata_size = 0;
714 char *name;
715#ifndef CONFIG_SERIAL_OMAP
716 struct plat_serial8250_port ports[2] = {
717 {},
718 {.flags = 0},
719 };
720 struct plat_serial8250_port *p = &ports[0];
721#else
722 struct omap_uart_port_info omap_up;
723#endif
Mika Westerbergf62349e2009-12-11 16:16:35 -0800724
Tony Lindgren40e44392010-12-22 18:42:35 -0800725 if (WARN_ON(!bdata))
Sergio Aguirree88d5562010-02-27 14:13:43 -0600726 return;
Tony Lindgren40e44392010-12-22 18:42:35 -0800727 if (WARN_ON(bdata->id < 0))
728 return;
729 if (WARN_ON(bdata->id >= num_uarts))
Mika Westerbergf62349e2009-12-11 16:16:35 -0800730 return;
731
Kevin Hilman6f251e92010-09-27 20:19:38 +0530732 list_for_each_entry(uart, &uart_list, node)
Tony Lindgren40e44392010-12-22 18:42:35 -0800733 if (bdata->id == uart->num)
Kevin Hilman6f251e92010-09-27 20:19:38 +0530734 break;
735
736 oh = uart->oh;
737 uart->dma_enabled = 0;
738#ifndef CONFIG_SERIAL_OMAP
739 name = "serial8250";
740
741 /*
742 * !! 8250 driver does not use standard IORESOURCE* It
743 * has it's own custom pdata that can be taken from
744 * the hwmod resource data. But, this needs to be
745 * done after the build.
746 *
747 * ?? does it have to be done before the register ??
748 * YES, because platform_device_data_add() copies
749 * pdata, it does not use a pointer.
750 */
751 p->flags = UPF_BOOT_AUTOCONF;
752 p->iotype = UPIO_MEM;
753 p->regshift = 2;
754 p->uartclk = OMAP24XX_BASE_BAUD * 16;
755 p->irq = oh->mpu_irqs[0].irq;
756 p->mapbase = oh->slaves[0]->addr->pa_start;
757 p->membase = omap_hwmod_get_mpu_rt_va(oh);
758 p->irqflags = IRQF_SHARED;
759 p->private_data = uart;
Mika Westerbergf62349e2009-12-11 16:16:35 -0800760
vikram pandita30e53bc2010-02-15 10:03:33 -0800761 /*
762 * omap44xx: Never read empty UART fifo
763 * omap3xxx: Never read empty UART fifo on UARTs
764 * with IP rev >=0x52
765 */
Kevin Hilman6f251e92010-09-27 20:19:38 +0530766 uart->regshift = p->regshift;
767 uart->membase = p->membase;
Nishanth Menon5a927b32010-08-02 13:18:12 +0300768 if (cpu_is_omap44xx())
769 uart->errata |= UART_ERRATA_FIFO_FULL_ABORT;
Kevin Hilman6f251e92010-09-27 20:19:38 +0530770 else if ((serial_read_reg(uart, UART_OMAP_MVER) & 0xFF)
Nishanth Menon5a927b32010-08-02 13:18:12 +0300771 >= UART_OMAP_NO_EMPTY_FIFO_READ_IP_REV)
772 uart->errata |= UART_ERRATA_FIFO_FULL_ABORT;
773
774 if (uart->errata & UART_ERRATA_FIFO_FULL_ABORT) {
Kevin Hilman6f251e92010-09-27 20:19:38 +0530775 p->serial_in = serial_in_override;
776 p->serial_out = serial_out_override;
777 }
778
779 pdata = &ports[0];
780 pdata_size = 2 * sizeof(struct plat_serial8250_port);
781#else
782
783 name = DRIVER_NAME;
784
785 omap_up.dma_enabled = uart->dma_enabled;
786 omap_up.uartclk = OMAP24XX_BASE_BAUD * 16;
787 omap_up.mapbase = oh->slaves[0]->addr->pa_start;
788 omap_up.membase = omap_hwmod_get_mpu_rt_va(oh);
789 omap_up.irqflags = IRQF_SHARED;
790 omap_up.flags = UPF_BOOT_AUTOCONF | UPF_SHARE_IRQ;
791
792 pdata = &omap_up;
793 pdata_size = sizeof(struct omap_uart_port_info);
794#endif
795
796 if (WARN_ON(!oh))
797 return;
798
799 od = omap_device_build(name, uart->num, oh, pdata, pdata_size,
800 omap_uart_latency,
801 ARRAY_SIZE(omap_uart_latency), false);
802 WARN(IS_ERR(od), "Could not build omap_device for %s: %s.\n",
803 name, oh->name);
804
Tony Lindgren40e44392010-12-22 18:42:35 -0800805 oh->mux = omap_hwmod_mux_init(bdata->pads, bdata->pads_cnt);
806
Kevin Hilman6f251e92010-09-27 20:19:38 +0530807 uart->irq = oh->mpu_irqs[0].irq;
808 uart->regshift = 2;
809 uart->mapbase = oh->slaves[0]->addr->pa_start;
810 uart->membase = omap_hwmod_get_mpu_rt_va(oh);
811 uart->pdev = &od->pdev;
812
813 oh->dev_attr = uart;
814
Paul Walmsley0d8e2d02010-11-24 16:49:05 -0700815 acquire_console_sem(); /* in case the earlycon is on the UART */
816
Kevin Hilman6f251e92010-09-27 20:19:38 +0530817 /*
818 * Because of early UART probing, UART did not get idled
819 * on init. Now that omap_device is ready, ensure full idle
820 * before doing omap_device_enable().
821 */
822 omap_hwmod_idle(uart->oh);
823
824 omap_device_enable(uart->pdev);
825 omap_uart_idle_init(uart);
826 omap_uart_reset(uart);
827 omap_hwmod_enable_wakeup(uart->oh);
828 omap_device_idle(uart->pdev);
829
830 /*
831 * Need to block sleep long enough for interrupt driven
832 * driver to start. Console driver is in polling mode
833 * so device needs to be kept enabled while polling driver
834 * is in use.
835 */
836 if (uart->timeout)
837 uart->timeout = (30 * HZ);
838 omap_uart_block_sleep(uart);
839 uart->timeout = DEFAULT_TIMEOUT;
840
Paul Walmsley0d8e2d02010-11-24 16:49:05 -0700841 release_console_sem();
842
Kevin Hilman6f251e92010-09-27 20:19:38 +0530843 if ((cpu_is_omap34xx() && uart->padconf) ||
844 (uart->wk_en && uart->wk_mask)) {
845 device_init_wakeup(&od->pdev.dev, true);
846 DEV_CREATE_FILE(&od->pdev.dev, &dev_attr_sleep_timeout);
Santosh Shilimkare03d37d2010-02-18 08:59:06 +0000847 }
Deepak K00034502010-08-02 13:18:12 +0300848
849 /* Enable the MDR1 errata for OMAP3 */
850 if (cpu_is_omap34xx())
851 uart->errata |= UART_ERRATA_i202_MDR1_ACCESS;
Mika Westerbergf62349e2009-12-11 16:16:35 -0800852}
853
854/**
855 * omap_serial_init() - intialize all supported serial ports
856 *
857 * Initializes all available UARTs as serial ports. Platforms
858 * can call this function when they want to have default behaviour
859 * for serial ports (e.g initialize them all as serial ports).
860 */
Paul Walmsleyb3c6df32009-09-03 20:14:02 +0300861void __init omap_serial_init(void)
862{
Kevin Hilman6f251e92010-09-27 20:19:38 +0530863 struct omap_uart_state *uart;
Tony Lindgren40e44392010-12-22 18:42:35 -0800864 struct omap_board_data bdata;
Paul Walmsleyb3c6df32009-09-03 20:14:02 +0300865
Tony Lindgren40e44392010-12-22 18:42:35 -0800866 list_for_each_entry(uart, &uart_list, node) {
867 bdata.id = uart->num;
868 bdata.flags = 0;
869 bdata.pads = NULL;
870 bdata.pads_cnt = 0;
871 omap_serial_init_port(&bdata);
872
873 }
Tony Lindgren1dbae812005-11-10 14:26:51 +0000874}