blob: 0548bbd434075225752b0a20d46de96dbc09aec3 [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 +0530109static int uart_idle_hwmod(struct omap_device *od)
110{
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -0700111 omap_hwmod_idle(od->hwmods[0]);
Kevin Hilman8da37d92010-09-27 20:20:06 +0530112
113 return 0;
114}
115
116static int uart_enable_hwmod(struct omap_device *od)
117{
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -0700118 omap_hwmod_enable(od->hwmods[0]);
Kevin Hilman8da37d92010-09-27 20:20:06 +0530119
120 return 0;
121}
122
Kevin Hilman6f251e92010-09-27 20:19:38 +0530123static struct omap_device_pm_latency omap_uart_latency[] = {
124 {
Kevin Hilman8da37d92010-09-27 20:20:06 +0530125 .deactivate_func = uart_idle_hwmod,
126 .activate_func = uart_enable_hwmod,
Kevin Hilman6f251e92010-09-27 20:19:38 +0530127 .flags = OMAP_DEVICE_LATENCY_AUTO_ADJUST,
128 },
129};
130
Alexander Shishkin92303722010-01-08 10:29:06 -0800131static inline unsigned int __serial_read_reg(struct uart_port *up,
Kevin Hilman6f251e92010-09-27 20:19:38 +0530132 int offset)
Alexander Shishkin92303722010-01-08 10:29:06 -0800133{
134 offset <<= up->regshift;
135 return (unsigned int)__raw_readb(up->membase + offset);
136}
137
Kevin Hilman6f251e92010-09-27 20:19:38 +0530138static inline unsigned int serial_read_reg(struct omap_uart_state *uart,
Tony Lindgren1dbae812005-11-10 14:26:51 +0000139 int offset)
140{
Kevin Hilman6f251e92010-09-27 20:19:38 +0530141 offset <<= uart->regshift;
142 return (unsigned int)__raw_readb(uart->membase + offset);
Tony Lindgren1dbae812005-11-10 14:26:51 +0000143}
144
Santosh Shilimkare03d37d2010-02-18 08:59:06 +0000145static inline void __serial_write_reg(struct uart_port *up, int offset,
146 int value)
147{
148 offset <<= up->regshift;
149 __raw_writeb(value, up->membase + offset);
150}
151
Kevin Hilman6f251e92010-09-27 20:19:38 +0530152static inline void serial_write_reg(struct omap_uart_state *uart, int offset,
Tony Lindgren1dbae812005-11-10 14:26:51 +0000153 int value)
154{
Kevin Hilman6f251e92010-09-27 20:19:38 +0530155 offset <<= uart->regshift;
156 __raw_writeb(value, uart->membase + offset);
Tony Lindgren1dbae812005-11-10 14:26:51 +0000157}
158
159/*
160 * Internal UARTs need to be initialized for the 8250 autoconfig to work
161 * properly. Note that the TX watermark initialization may not be needed
162 * once the 8250.c watermark handling code is merged.
163 */
Kevin Hilman6f251e92010-09-27 20:19:38 +0530164
Kevin Hilman4af40162009-02-04 10:51:40 -0800165static inline void __init omap_uart_reset(struct omap_uart_state *uart)
Tony Lindgren1dbae812005-11-10 14:26:51 +0000166{
Andrei Emeltchenko498cb952010-11-30 14:11:49 -0800167 serial_write_reg(uart, UART_OMAP_MDR1, UART_OMAP_MDR1_DISABLE);
Kevin Hilman6f251e92010-09-27 20:19:38 +0530168 serial_write_reg(uart, UART_OMAP_SCR, 0x08);
Andrei Emeltchenko498cb952010-11-30 14:11:49 -0800169 serial_write_reg(uart, UART_OMAP_MDR1, UART_OMAP_MDR1_16X_MODE);
Tony Lindgren1dbae812005-11-10 14:26:51 +0000170}
171
Kevin Hilman4af40162009-02-04 10:51:40 -0800172#if defined(CONFIG_PM) && defined(CONFIG_ARCH_OMAP3)
173
Deepak K00034502010-08-02 13:18:12 +0300174/*
175 * Work Around for Errata i202 (3430 - 1.12, 3630 - 1.6)
176 * The access to uart register after MDR1 Access
177 * causes UART to corrupt data.
178 *
179 * Need a delay =
180 * 5 L4 clock cycles + 5 UART functional clock cycle (@48MHz = ~0.2uS)
181 * give 10 times as much
182 */
183static void omap_uart_mdr1_errataset(struct omap_uart_state *uart, u8 mdr1_val,
184 u8 fcr_val)
185{
Deepak K00034502010-08-02 13:18:12 +0300186 u8 timeout = 255;
187
Kevin Hilman6f251e92010-09-27 20:19:38 +0530188 serial_write_reg(uart, UART_OMAP_MDR1, mdr1_val);
Deepak K00034502010-08-02 13:18:12 +0300189 udelay(2);
Kevin Hilman6f251e92010-09-27 20:19:38 +0530190 serial_write_reg(uart, UART_FCR, fcr_val | UART_FCR_CLEAR_XMIT |
Deepak K00034502010-08-02 13:18:12 +0300191 UART_FCR_CLEAR_RCVR);
192 /*
193 * Wait for FIFO to empty: when empty, RX_FIFO_E bit is 0 and
194 * TX_FIFO_E bit is 1.
195 */
Kevin Hilman6f251e92010-09-27 20:19:38 +0530196 while (UART_LSR_THRE != (serial_read_reg(uart, UART_LSR) &
Deepak K00034502010-08-02 13:18:12 +0300197 (UART_LSR_THRE | UART_LSR_DR))) {
198 timeout--;
199 if (!timeout) {
200 /* Should *never* happen. we warn and carry on */
Kevin Hilman6f251e92010-09-27 20:19:38 +0530201 dev_crit(&uart->pdev->dev, "Errata i202: timedout %x\n",
202 serial_read_reg(uart, UART_LSR));
Deepak K00034502010-08-02 13:18:12 +0300203 break;
204 }
205 udelay(1);
206 }
207}
208
Kevin Hilman4af40162009-02-04 10:51:40 -0800209static void omap_uart_save_context(struct omap_uart_state *uart)
Jouni Hogander6e811762008-10-06 15:49:15 +0300210{
Kevin Hilman4af40162009-02-04 10:51:40 -0800211 u16 lcr = 0;
Kevin Hilman4af40162009-02-04 10:51:40 -0800212
213 if (!enable_off_mode)
214 return;
215
Kevin Hilman6f251e92010-09-27 20:19:38 +0530216 lcr = serial_read_reg(uart, UART_LCR);
Andrei Emeltchenko662b083a2010-11-30 14:11:49 -0800217 serial_write_reg(uart, UART_LCR, UART_LCR_CONF_MODE_B);
Kevin Hilman6f251e92010-09-27 20:19:38 +0530218 uart->dll = serial_read_reg(uart, UART_DLL);
219 uart->dlh = serial_read_reg(uart, UART_DLM);
220 serial_write_reg(uart, UART_LCR, lcr);
221 uart->ier = serial_read_reg(uart, UART_IER);
222 uart->sysc = serial_read_reg(uart, UART_OMAP_SYSC);
223 uart->scr = serial_read_reg(uart, UART_OMAP_SCR);
224 uart->wer = serial_read_reg(uart, UART_OMAP_WER);
Andrei Emeltchenko662b083a2010-11-30 14:11:49 -0800225 serial_write_reg(uart, UART_LCR, UART_LCR_CONF_MODE_A);
Kevin Hilman6f251e92010-09-27 20:19:38 +0530226 uart->mcr = serial_read_reg(uart, UART_MCR);
227 serial_write_reg(uart, UART_LCR, lcr);
Kevin Hilman4af40162009-02-04 10:51:40 -0800228
229 uart->context_valid = 1;
230}
231
232static void omap_uart_restore_context(struct omap_uart_state *uart)
233{
234 u16 efr = 0;
Kevin Hilman4af40162009-02-04 10:51:40 -0800235
236 if (!enable_off_mode)
237 return;
238
239 if (!uart->context_valid)
240 return;
241
242 uart->context_valid = 0;
243
Deepak K00034502010-08-02 13:18:12 +0300244 if (uart->errata & UART_ERRATA_i202_MDR1_ACCESS)
Andrei Emeltchenko498cb952010-11-30 14:11:49 -0800245 omap_uart_mdr1_errataset(uart, UART_OMAP_MDR1_DISABLE, 0xA0);
Deepak K00034502010-08-02 13:18:12 +0300246 else
Andrei Emeltchenko498cb952010-11-30 14:11:49 -0800247 serial_write_reg(uart, UART_OMAP_MDR1, UART_OMAP_MDR1_DISABLE);
248
Andrei Emeltchenko662b083a2010-11-30 14:11:49 -0800249 serial_write_reg(uart, UART_LCR, UART_LCR_CONF_MODE_B);
Kevin Hilman6f251e92010-09-27 20:19:38 +0530250 efr = serial_read_reg(uart, UART_EFR);
251 serial_write_reg(uart, UART_EFR, UART_EFR_ECB);
252 serial_write_reg(uart, UART_LCR, 0x0); /* Operational mode */
253 serial_write_reg(uart, UART_IER, 0x0);
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 serial_write_reg(uart, UART_DLL, uart->dll);
256 serial_write_reg(uart, UART_DLM, uart->dlh);
257 serial_write_reg(uart, UART_LCR, 0x0); /* Operational mode */
258 serial_write_reg(uart, UART_IER, uart->ier);
Andrei Emeltchenko662b083a2010-11-30 14:11:49 -0800259 serial_write_reg(uart, UART_LCR, UART_LCR_CONF_MODE_A);
Kevin Hilman6f251e92010-09-27 20:19:38 +0530260 serial_write_reg(uart, UART_MCR, uart->mcr);
Andrei Emeltchenko662b083a2010-11-30 14:11:49 -0800261 serial_write_reg(uart, UART_LCR, UART_LCR_CONF_MODE_B);
Kevin Hilman6f251e92010-09-27 20:19:38 +0530262 serial_write_reg(uart, UART_EFR, efr);
263 serial_write_reg(uart, UART_LCR, UART_LCR_WLEN8);
264 serial_write_reg(uart, UART_OMAP_SCR, uart->scr);
265 serial_write_reg(uart, UART_OMAP_WER, uart->wer);
266 serial_write_reg(uart, UART_OMAP_SYSC, uart->sysc);
Andrei Emeltchenko498cb952010-11-30 14:11:49 -0800267
Deepak K00034502010-08-02 13:18:12 +0300268 if (uart->errata & UART_ERRATA_i202_MDR1_ACCESS)
Andrei Emeltchenko498cb952010-11-30 14:11:49 -0800269 omap_uart_mdr1_errataset(uart, UART_OMAP_MDR1_16X_MODE, 0xA1);
Deepak K00034502010-08-02 13:18:12 +0300270 else
Kevin Hilman6f251e92010-09-27 20:19:38 +0530271 /* UART 16x mode */
Andrei Emeltchenko498cb952010-11-30 14:11:49 -0800272 serial_write_reg(uart, UART_OMAP_MDR1,
273 UART_OMAP_MDR1_16X_MODE);
Kevin Hilman4af40162009-02-04 10:51:40 -0800274}
275#else
276static inline void omap_uart_save_context(struct omap_uart_state *uart) {}
277static inline void omap_uart_restore_context(struct omap_uart_state *uart) {}
278#endif /* CONFIG_PM && CONFIG_ARCH_OMAP3 */
279
280static inline void omap_uart_enable_clocks(struct omap_uart_state *uart)
281{
282 if (uart->clocked)
283 return;
284
Kevin Hilman6f251e92010-09-27 20:19:38 +0530285 omap_device_enable(uart->pdev);
Kevin Hilman4af40162009-02-04 10:51:40 -0800286 uart->clocked = 1;
287 omap_uart_restore_context(uart);
288}
289
290#ifdef CONFIG_PM
291
292static inline void omap_uart_disable_clocks(struct omap_uart_state *uart)
293{
294 if (!uart->clocked)
295 return;
296
297 omap_uart_save_context(uart);
298 uart->clocked = 0;
Kevin Hilman6f251e92010-09-27 20:19:38 +0530299 omap_device_idle(uart->pdev);
Kevin Hilman4af40162009-02-04 10:51:40 -0800300}
301
Kevin Hilmanfd455ea2009-04-27 12:27:36 -0700302static void omap_uart_enable_wakeup(struct omap_uart_state *uart)
303{
304 /* Set wake-enable bit */
305 if (uart->wk_en && uart->wk_mask) {
306 u32 v = __raw_readl(uart->wk_en);
307 v |= uart->wk_mask;
308 __raw_writel(v, uart->wk_en);
309 }
310
311 /* Ensure IOPAD wake-enables are set */
312 if (cpu_is_omap34xx() && uart->padconf) {
313 u16 v = omap_ctrl_readw(uart->padconf);
314 v |= OMAP3_PADCONF_WAKEUPENABLE0;
315 omap_ctrl_writew(v, uart->padconf);
316 }
317}
318
319static void omap_uart_disable_wakeup(struct omap_uart_state *uart)
320{
321 /* Clear wake-enable bit */
322 if (uart->wk_en && uart->wk_mask) {
323 u32 v = __raw_readl(uart->wk_en);
324 v &= ~uart->wk_mask;
325 __raw_writel(v, uart->wk_en);
326 }
327
328 /* Ensure IOPAD wake-enables are cleared */
329 if (cpu_is_omap34xx() && uart->padconf) {
330 u16 v = omap_ctrl_readw(uart->padconf);
331 v &= ~OMAP3_PADCONF_WAKEUPENABLE0;
332 omap_ctrl_writew(v, uart->padconf);
333 }
334}
335
Kevin Hilman4af40162009-02-04 10:51:40 -0800336static void omap_uart_smart_idle_enable(struct omap_uart_state *uart,
Kevin Hilman6f251e92010-09-27 20:19:38 +0530337 int enable)
Kevin Hilman4af40162009-02-04 10:51:40 -0800338{
Kevin Hilman6f251e92010-09-27 20:19:38 +0530339 u8 idlemode;
Kevin Hilman4af40162009-02-04 10:51:40 -0800340
Kevin Hilman6f251e92010-09-27 20:19:38 +0530341 if (enable) {
342 /**
343 * Errata 2.15: [UART]:Cannot Acknowledge Idle Requests
344 * in Smartidle Mode When Configured for DMA Operations.
345 */
346 if (uart->dma_enabled)
347 idlemode = HWMOD_IDLEMODE_FORCE;
348 else
349 idlemode = HWMOD_IDLEMODE_SMART;
350 } else {
351 idlemode = HWMOD_IDLEMODE_NO;
352 }
Kevin Hilman4af40162009-02-04 10:51:40 -0800353
Kevin Hilman6f251e92010-09-27 20:19:38 +0530354 omap_hwmod_set_slave_idlemode(uart->oh, idlemode);
Kevin Hilman4af40162009-02-04 10:51:40 -0800355}
356
357static void omap_uart_block_sleep(struct omap_uart_state *uart)
358{
359 omap_uart_enable_clocks(uart);
360
361 omap_uart_smart_idle_enable(uart, 0);
362 uart->can_sleep = 0;
Jouni Hoganderba87a9b2008-12-09 13:36:50 +0200363 if (uart->timeout)
364 mod_timer(&uart->timer, jiffies + uart->timeout);
365 else
366 del_timer(&uart->timer);
Kevin Hilman4af40162009-02-04 10:51:40 -0800367}
368
369static void omap_uart_allow_sleep(struct omap_uart_state *uart)
370{
Kevin Hilman6f251e92010-09-27 20:19:38 +0530371 if (device_may_wakeup(&uart->pdev->dev))
Kevin Hilmanfd455ea2009-04-27 12:27:36 -0700372 omap_uart_enable_wakeup(uart);
373 else
374 omap_uart_disable_wakeup(uart);
375
Kevin Hilman4af40162009-02-04 10:51:40 -0800376 if (!uart->clocked)
377 return;
378
379 omap_uart_smart_idle_enable(uart, 1);
380 uart->can_sleep = 1;
381 del_timer(&uart->timer);
382}
383
384static void omap_uart_idle_timer(unsigned long data)
385{
386 struct omap_uart_state *uart = (struct omap_uart_state *)data;
387
388 omap_uart_allow_sleep(uart);
389}
390
391void omap_uart_prepare_idle(int num)
392{
393 struct omap_uart_state *uart;
394
395 list_for_each_entry(uart, &uart_list, node) {
396 if (num == uart->num && uart->can_sleep) {
397 omap_uart_disable_clocks(uart);
398 return;
Jouni Hogander6e811762008-10-06 15:49:15 +0300399 }
400 }
401}
402
Kevin Hilman4af40162009-02-04 10:51:40 -0800403void omap_uart_resume_idle(int num)
404{
405 struct omap_uart_state *uart;
406
407 list_for_each_entry(uart, &uart_list, node) {
Kevin Hilmanf9100432010-11-24 11:09:03 -0800408 if (num == uart->num && uart->can_sleep) {
Kevin Hilman4af40162009-02-04 10:51:40 -0800409 omap_uart_enable_clocks(uart);
410
411 /* Check for IO pad wakeup */
412 if (cpu_is_omap34xx() && uart->padconf) {
413 u16 p = omap_ctrl_readw(uart->padconf);
414
415 if (p & OMAP3_PADCONF_WAKEUPEVENT0)
416 omap_uart_block_sleep(uart);
417 }
418
419 /* Check for normal UART wakeup */
420 if (__raw_readl(uart->wk_st) & uart->wk_mask)
421 omap_uart_block_sleep(uart);
Kevin Hilman4af40162009-02-04 10:51:40 -0800422 return;
423 }
424 }
425}
426
427void omap_uart_prepare_suspend(void)
428{
429 struct omap_uart_state *uart;
430
431 list_for_each_entry(uart, &uart_list, node) {
432 omap_uart_allow_sleep(uart);
433 }
434}
435
436int omap_uart_can_sleep(void)
437{
438 struct omap_uart_state *uart;
439 int can_sleep = 1;
440
441 list_for_each_entry(uart, &uart_list, node) {
442 if (!uart->clocked)
443 continue;
444
445 if (!uart->can_sleep) {
446 can_sleep = 0;
447 continue;
448 }
449
450 /* This UART can now safely sleep. */
451 omap_uart_allow_sleep(uart);
452 }
453
454 return can_sleep;
455}
456
457/**
458 * omap_uart_interrupt()
459 *
460 * This handler is used only to detect that *any* UART interrupt has
461 * occurred. It does _nothing_ to handle the interrupt. Rather,
462 * any UART interrupt will trigger the inactivity timer so the
463 * UART will not idle or sleep for its timeout period.
464 *
465 **/
Kevin Hilman6f251e92010-09-27 20:19:38 +0530466/* static int first_interrupt; */
Kevin Hilman4af40162009-02-04 10:51:40 -0800467static irqreturn_t omap_uart_interrupt(int irq, void *dev_id)
468{
469 struct omap_uart_state *uart = dev_id;
470
471 omap_uart_block_sleep(uart);
472
473 return IRQ_NONE;
474}
475
476static void omap_uart_idle_init(struct omap_uart_state *uart)
477{
Kevin Hilman4af40162009-02-04 10:51:40 -0800478 int ret;
479
480 uart->can_sleep = 0;
Kevin Hilmanfd455ea2009-04-27 12:27:36 -0700481 uart->timeout = DEFAULT_TIMEOUT;
Kevin Hilman4af40162009-02-04 10:51:40 -0800482 setup_timer(&uart->timer, omap_uart_idle_timer,
483 (unsigned long) uart);
Tony Lindgren301fe8e2010-02-01 12:34:31 -0800484 if (uart->timeout)
485 mod_timer(&uart->timer, jiffies + uart->timeout);
Kevin Hilman4af40162009-02-04 10:51:40 -0800486 omap_uart_smart_idle_enable(uart, 0);
487
488 if (cpu_is_omap34xx()) {
Govindraj.R52663ae2010-09-27 20:20:41 +0530489 u32 mod = (uart->num > 1) ? OMAP3430_PER_MOD : CORE_MOD;
Kevin Hilman4af40162009-02-04 10:51:40 -0800490 u32 wk_mask = 0;
491 u32 padconf = 0;
492
493 uart->wk_en = OMAP34XX_PRM_REGADDR(mod, PM_WKEN1);
494 uart->wk_st = OMAP34XX_PRM_REGADDR(mod, PM_WKST1);
495 switch (uart->num) {
496 case 0:
497 wk_mask = OMAP3430_ST_UART1_MASK;
498 padconf = 0x182;
499 break;
500 case 1:
501 wk_mask = OMAP3430_ST_UART2_MASK;
502 padconf = 0x17a;
503 break;
504 case 2:
505 wk_mask = OMAP3430_ST_UART3_MASK;
506 padconf = 0x19e;
507 break;
Govindraj.R52663ae2010-09-27 20:20:41 +0530508 case 3:
509 wk_mask = OMAP3630_ST_UART4_MASK;
510 padconf = 0x0d2;
511 break;
Kevin Hilman4af40162009-02-04 10:51:40 -0800512 }
513 uart->wk_mask = wk_mask;
514 uart->padconf = padconf;
515 } else if (cpu_is_omap24xx()) {
516 u32 wk_mask = 0;
Kevin Hilmancb74f022010-10-20 23:19:03 +0000517 u32 wk_en = PM_WKEN1, wk_st = PM_WKST1;
Kevin Hilman4af40162009-02-04 10:51:40 -0800518
Kevin Hilman4af40162009-02-04 10:51:40 -0800519 switch (uart->num) {
520 case 0:
521 wk_mask = OMAP24XX_ST_UART1_MASK;
522 break;
523 case 1:
524 wk_mask = OMAP24XX_ST_UART2_MASK;
525 break;
526 case 2:
Kevin Hilmancb74f022010-10-20 23:19:03 +0000527 wk_en = OMAP24XX_PM_WKEN2;
528 wk_st = OMAP24XX_PM_WKST2;
Kevin Hilman4af40162009-02-04 10:51:40 -0800529 wk_mask = OMAP24XX_ST_UART3_MASK;
530 break;
531 }
532 uart->wk_mask = wk_mask;
Kevin Hilmancb74f022010-10-20 23:19:03 +0000533 if (cpu_is_omap2430()) {
534 uart->wk_en = OMAP2430_PRM_REGADDR(CORE_MOD, wk_en);
535 uart->wk_st = OMAP2430_PRM_REGADDR(CORE_MOD, wk_st);
536 } else if (cpu_is_omap2420()) {
537 uart->wk_en = OMAP2420_PRM_REGADDR(CORE_MOD, wk_en);
538 uart->wk_st = OMAP2420_PRM_REGADDR(CORE_MOD, wk_st);
539 }
Kevin Hilman4af40162009-02-04 10:51:40 -0800540 } else {
Nishanth Menonc54bae12010-08-02 13:18:11 +0300541 uart->wk_en = NULL;
542 uart->wk_st = NULL;
Kevin Hilman4af40162009-02-04 10:51:40 -0800543 uart->wk_mask = 0;
544 uart->padconf = 0;
545 }
546
Kevin Hilman6f251e92010-09-27 20:19:38 +0530547 uart->irqflags |= IRQF_SHARED;
548 ret = request_threaded_irq(uart->irq, NULL, omap_uart_interrupt,
549 IRQF_SHARED, "serial idle", (void *)uart);
Kevin Hilman4af40162009-02-04 10:51:40 -0800550 WARN_ON(ret);
551}
552
Tero Kristo24662112009-03-05 16:32:23 +0200553void omap_uart_enable_irqs(int enable)
Jouni Hoganderba87a9b2008-12-09 13:36:50 +0200554{
Tero Kristo24662112009-03-05 16:32:23 +0200555 int ret;
556 struct omap_uart_state *uart;
557
558 list_for_each_entry(uart, &uart_list, node) {
Kevin Hilman3244fcd2010-09-27 20:19:53 +0530559 if (enable) {
560 pm_runtime_put_sync(&uart->pdev->dev);
Kevin Hilman6f251e92010-09-27 20:19:38 +0530561 ret = request_threaded_irq(uart->irq, NULL,
562 omap_uart_interrupt,
563 IRQF_SHARED,
564 "serial idle",
565 (void *)uart);
Kevin Hilman3244fcd2010-09-27 20:19:53 +0530566 } else {
567 pm_runtime_get_noresume(&uart->pdev->dev);
Kevin Hilman6f251e92010-09-27 20:19:38 +0530568 free_irq(uart->irq, (void *)uart);
Kevin Hilman3244fcd2010-09-27 20:19:53 +0530569 }
Tero Kristo24662112009-03-05 16:32:23 +0200570 }
Jouni Hoganderba87a9b2008-12-09 13:36:50 +0200571}
572
Kevin Hilmanfd455ea2009-04-27 12:27:36 -0700573static ssize_t sleep_timeout_show(struct device *dev,
574 struct device_attribute *attr,
Jouni Hoganderba87a9b2008-12-09 13:36:50 +0200575 char *buf)
576{
Kevin Hilman6f251e92010-09-27 20:19:38 +0530577 struct platform_device *pdev = to_platform_device(dev);
578 struct omap_device *odev = to_omap_device(pdev);
579 struct omap_uart_state *uart = odev->hwmods[0]->dev_attr;
Kevin Hilmanfd455ea2009-04-27 12:27:36 -0700580
581 return sprintf(buf, "%u\n", uart->timeout / HZ);
Jouni Hoganderba87a9b2008-12-09 13:36:50 +0200582}
583
Kevin Hilmanfd455ea2009-04-27 12:27:36 -0700584static ssize_t sleep_timeout_store(struct device *dev,
585 struct device_attribute *attr,
Jouni Hoganderba87a9b2008-12-09 13:36:50 +0200586 const char *buf, size_t n)
587{
Kevin Hilman6f251e92010-09-27 20:19:38 +0530588 struct platform_device *pdev = to_platform_device(dev);
589 struct omap_device *odev = to_omap_device(pdev);
590 struct omap_uart_state *uart = odev->hwmods[0]->dev_attr;
Jouni Hoganderba87a9b2008-12-09 13:36:50 +0200591 unsigned int value;
592
593 if (sscanf(buf, "%u", &value) != 1) {
Sergio Aguirre10c805e2010-03-09 13:22:14 -0600594 dev_err(dev, "sleep_timeout_store: Invalid value\n");
Jouni Hoganderba87a9b2008-12-09 13:36:50 +0200595 return -EINVAL;
596 }
Kevin Hilmanfd455ea2009-04-27 12:27:36 -0700597
598 uart->timeout = value * HZ;
599 if (uart->timeout)
600 mod_timer(&uart->timer, jiffies + uart->timeout);
601 else
602 /* A zero value means disable timeout feature */
603 omap_uart_block_sleep(uart);
604
Jouni Hoganderba87a9b2008-12-09 13:36:50 +0200605 return n;
606}
607
Nishanth Menonbfe69772010-08-02 13:18:12 +0300608static DEVICE_ATTR(sleep_timeout, 0644, sleep_timeout_show,
609 sleep_timeout_store);
Kevin Hilmanfd455ea2009-04-27 12:27:36 -0700610#define DEV_CREATE_FILE(dev, attr) WARN_ON(device_create_file(dev, attr))
Kevin Hilman4af40162009-02-04 10:51:40 -0800611#else
612static inline void omap_uart_idle_init(struct omap_uart_state *uart) {}
Santosh Shilimkara1b04cc2010-10-11 11:05:18 +0000613static void omap_uart_block_sleep(struct omap_uart_state *uart)
614{
615 /* Needed to enable UART clocks when built without CONFIG_PM */
616 omap_uart_enable_clocks(uart);
617}
Kevin Hilmanfd455ea2009-04-27 12:27:36 -0700618#define DEV_CREATE_FILE(dev, attr)
Kevin Hilman4af40162009-02-04 10:51:40 -0800619#endif /* CONFIG_PM */
620
Kevin Hilman6f251e92010-09-27 20:19:38 +0530621#ifndef CONFIG_SERIAL_OMAP
vikram panditace13d472009-12-11 16:16:37 -0800622/*
623 * Override the default 8250 read handler: mem_serial_in()
624 * Empty RX fifo read causes an abort on omap3630 and omap4
625 * This function makes sure that an empty rx fifo is not read on these silicons
626 * (OMAP1/2/3430 are not affected)
627 */
628static unsigned int serial_in_override(struct uart_port *up, int offset)
629{
630 if (UART_RX == offset) {
631 unsigned int lsr;
Alexander Shishkin92303722010-01-08 10:29:06 -0800632 lsr = __serial_read_reg(up, UART_LSR);
vikram panditace13d472009-12-11 16:16:37 -0800633 if (!(lsr & UART_LSR_DR))
634 return -EPERM;
635 }
Alexander Shishkin92303722010-01-08 10:29:06 -0800636
637 return __serial_read_reg(up, offset);
vikram panditace13d472009-12-11 16:16:37 -0800638}
639
Santosh Shilimkare03d37d2010-02-18 08:59:06 +0000640static void serial_out_override(struct uart_port *up, int offset, int value)
641{
642 unsigned int status, tmout = 10000;
643
644 status = __serial_read_reg(up, UART_LSR);
645 while (!(status & UART_LSR_THRE)) {
646 /* Wait up to 10ms for the character(s) to be sent. */
647 if (--tmout == 0)
648 break;
649 udelay(1);
650 status = __serial_read_reg(up, UART_LSR);
651 }
652 __serial_write_reg(up, offset, value);
653}
Kevin Hilman6f251e92010-09-27 20:19:38 +0530654#endif
655
Paul Walmsleyb3c6df32009-09-03 20:14:02 +0300656void __init omap_serial_early_init(void)
Tony Lindgren1dbae812005-11-10 14:26:51 +0000657{
Kevin Hilman6f251e92010-09-27 20:19:38 +0530658 int i = 0;
Tony Lindgren1dbae812005-11-10 14:26:51 +0000659
Kevin Hilman6f251e92010-09-27 20:19:38 +0530660 do {
661 char oh_name[MAX_UART_HWMOD_NAME_LEN];
662 struct omap_hwmod *oh;
663 struct omap_uart_state *uart;
Thomas Weber21b90342010-02-25 09:40:19 +0000664
Kevin Hilman6f251e92010-09-27 20:19:38 +0530665 snprintf(oh_name, MAX_UART_HWMOD_NAME_LEN,
666 "uart%d", i + 1);
667 oh = omap_hwmod_lookup(oh_name);
668 if (!oh)
669 break;
Tony Lindgren1dbae812005-11-10 14:26:51 +0000670
Kevin Hilman6f251e92010-09-27 20:19:38 +0530671 uart = kzalloc(sizeof(struct omap_uart_state), GFP_KERNEL);
672 if (WARN_ON(!uart))
673 return;
Tony Lindgren1dbae812005-11-10 14:26:51 +0000674
Kevin Hilman6f251e92010-09-27 20:19:38 +0530675 uart->oh = oh;
676 uart->num = i++;
677 list_add_tail(&uart->node, &uart_list);
678 num_uarts++;
679
Tony Lindgren84f90c92009-10-16 09:53:00 -0700680 /*
Kevin Hilman6f251e92010-09-27 20:19:38 +0530681 * NOTE: omap_hwmod_init() has not yet been called,
682 * so no hwmod functions will work yet.
Tony Lindgren84f90c92009-10-16 09:53:00 -0700683 */
Tony Lindgren84f90c92009-10-16 09:53:00 -0700684
Kevin Hilman6f251e92010-09-27 20:19:38 +0530685 /*
686 * During UART early init, device need to be probed
687 * to determine SoC specific init before omap_device
688 * is ready. Therefore, don't allow idle here
689 */
690 uart->oh->flags |= HWMOD_INIT_NO_IDLE | HWMOD_INIT_NO_RESET;
691 } while (1);
Paul Walmsleyb3c6df32009-09-03 20:14:02 +0300692}
693
Mika Westerbergf62349e2009-12-11 16:16:35 -0800694/**
695 * omap_serial_init_port() - initialize single serial port
696 * @port: serial port number (0-3)
697 *
698 * This function initialies serial driver for given @port only.
699 * Platforms can call this function instead of omap_serial_init()
700 * if they don't plan to use all available UARTs as serial ports.
701 *
702 * Don't mix calls to omap_serial_init_port() and omap_serial_init(),
703 * use only one of the two.
704 */
705void __init omap_serial_init_port(int port)
706{
707 struct omap_uart_state *uart;
Kevin Hilman6f251e92010-09-27 20:19:38 +0530708 struct omap_hwmod *oh;
709 struct omap_device *od;
710 void *pdata = NULL;
711 u32 pdata_size = 0;
712 char *name;
713#ifndef CONFIG_SERIAL_OMAP
714 struct plat_serial8250_port ports[2] = {
715 {},
716 {.flags = 0},
717 };
718 struct plat_serial8250_port *p = &ports[0];
719#else
720 struct omap_uart_port_info omap_up;
721#endif
Mika Westerbergf62349e2009-12-11 16:16:35 -0800722
Kevin Hilman6f251e92010-09-27 20:19:38 +0530723 if (WARN_ON(port < 0))
Sergio Aguirree88d5562010-02-27 14:13:43 -0600724 return;
Kevin Hilman6f251e92010-09-27 20:19:38 +0530725 if (WARN_ON(port >= num_uarts))
Mika Westerbergf62349e2009-12-11 16:16:35 -0800726 return;
727
Kevin Hilman6f251e92010-09-27 20:19:38 +0530728 list_for_each_entry(uart, &uart_list, node)
729 if (port == uart->num)
730 break;
731
732 oh = uart->oh;
733 uart->dma_enabled = 0;
734#ifndef CONFIG_SERIAL_OMAP
735 name = "serial8250";
736
737 /*
738 * !! 8250 driver does not use standard IORESOURCE* It
739 * has it's own custom pdata that can be taken from
740 * the hwmod resource data. But, this needs to be
741 * done after the build.
742 *
743 * ?? does it have to be done before the register ??
744 * YES, because platform_device_data_add() copies
745 * pdata, it does not use a pointer.
746 */
747 p->flags = UPF_BOOT_AUTOCONF;
748 p->iotype = UPIO_MEM;
749 p->regshift = 2;
750 p->uartclk = OMAP24XX_BASE_BAUD * 16;
751 p->irq = oh->mpu_irqs[0].irq;
752 p->mapbase = oh->slaves[0]->addr->pa_start;
753 p->membase = omap_hwmod_get_mpu_rt_va(oh);
754 p->irqflags = IRQF_SHARED;
755 p->private_data = uart;
Mika Westerbergf62349e2009-12-11 16:16:35 -0800756
vikram pandita30e53bc2010-02-15 10:03:33 -0800757 /*
758 * omap44xx: Never read empty UART fifo
759 * omap3xxx: Never read empty UART fifo on UARTs
760 * with IP rev >=0x52
761 */
Kevin Hilman6f251e92010-09-27 20:19:38 +0530762 uart->regshift = p->regshift;
763 uart->membase = p->membase;
Nishanth Menon5a927b32010-08-02 13:18:12 +0300764 if (cpu_is_omap44xx())
765 uart->errata |= UART_ERRATA_FIFO_FULL_ABORT;
Kevin Hilman6f251e92010-09-27 20:19:38 +0530766 else if ((serial_read_reg(uart, UART_OMAP_MVER) & 0xFF)
Nishanth Menon5a927b32010-08-02 13:18:12 +0300767 >= UART_OMAP_NO_EMPTY_FIFO_READ_IP_REV)
768 uart->errata |= UART_ERRATA_FIFO_FULL_ABORT;
769
770 if (uart->errata & UART_ERRATA_FIFO_FULL_ABORT) {
Kevin Hilman6f251e92010-09-27 20:19:38 +0530771 p->serial_in = serial_in_override;
772 p->serial_out = serial_out_override;
773 }
774
775 pdata = &ports[0];
776 pdata_size = 2 * sizeof(struct plat_serial8250_port);
777#else
778
779 name = DRIVER_NAME;
780
781 omap_up.dma_enabled = uart->dma_enabled;
782 omap_up.uartclk = OMAP24XX_BASE_BAUD * 16;
783 omap_up.mapbase = oh->slaves[0]->addr->pa_start;
784 omap_up.membase = omap_hwmod_get_mpu_rt_va(oh);
785 omap_up.irqflags = IRQF_SHARED;
786 omap_up.flags = UPF_BOOT_AUTOCONF | UPF_SHARE_IRQ;
787
788 pdata = &omap_up;
789 pdata_size = sizeof(struct omap_uart_port_info);
790#endif
791
792 if (WARN_ON(!oh))
793 return;
794
795 od = omap_device_build(name, uart->num, oh, pdata, pdata_size,
796 omap_uart_latency,
797 ARRAY_SIZE(omap_uart_latency), false);
798 WARN(IS_ERR(od), "Could not build omap_device for %s: %s.\n",
799 name, oh->name);
800
801 uart->irq = oh->mpu_irqs[0].irq;
802 uart->regshift = 2;
803 uart->mapbase = oh->slaves[0]->addr->pa_start;
804 uart->membase = omap_hwmod_get_mpu_rt_va(oh);
805 uart->pdev = &od->pdev;
806
807 oh->dev_attr = uart;
808
Paul Walmsley0d8e2d02010-11-24 16:49:05 -0700809 acquire_console_sem(); /* in case the earlycon is on the UART */
810
Kevin Hilman6f251e92010-09-27 20:19:38 +0530811 /*
812 * Because of early UART probing, UART did not get idled
813 * on init. Now that omap_device is ready, ensure full idle
814 * before doing omap_device_enable().
815 */
816 omap_hwmod_idle(uart->oh);
817
818 omap_device_enable(uart->pdev);
819 omap_uart_idle_init(uart);
820 omap_uart_reset(uart);
821 omap_hwmod_enable_wakeup(uart->oh);
822 omap_device_idle(uart->pdev);
823
824 /*
825 * Need to block sleep long enough for interrupt driven
826 * driver to start. Console driver is in polling mode
827 * so device needs to be kept enabled while polling driver
828 * is in use.
829 */
830 if (uart->timeout)
831 uart->timeout = (30 * HZ);
832 omap_uart_block_sleep(uart);
833 uart->timeout = DEFAULT_TIMEOUT;
834
Paul Walmsley0d8e2d02010-11-24 16:49:05 -0700835 release_console_sem();
836
Kevin Hilman6f251e92010-09-27 20:19:38 +0530837 if ((cpu_is_omap34xx() && uart->padconf) ||
838 (uart->wk_en && uart->wk_mask)) {
839 device_init_wakeup(&od->pdev.dev, true);
840 DEV_CREATE_FILE(&od->pdev.dev, &dev_attr_sleep_timeout);
Santosh Shilimkare03d37d2010-02-18 08:59:06 +0000841 }
Deepak K00034502010-08-02 13:18:12 +0300842
843 /* Enable the MDR1 errata for OMAP3 */
844 if (cpu_is_omap34xx())
845 uart->errata |= UART_ERRATA_i202_MDR1_ACCESS;
Mika Westerbergf62349e2009-12-11 16:16:35 -0800846}
847
848/**
849 * omap_serial_init() - intialize all supported serial ports
850 *
851 * Initializes all available UARTs as serial ports. Platforms
852 * can call this function when they want to have default behaviour
853 * for serial ports (e.g initialize them all as serial ports).
854 */
Paul Walmsleyb3c6df32009-09-03 20:14:02 +0300855void __init omap_serial_init(void)
856{
Kevin Hilman6f251e92010-09-27 20:19:38 +0530857 struct omap_uart_state *uart;
Paul Walmsleyb3c6df32009-09-03 20:14:02 +0300858
Kevin Hilman6f251e92010-09-27 20:19:38 +0530859 list_for_each_entry(uart, &uart_list, node)
860 omap_serial_init_port(uart->num);
Tony Lindgren1dbae812005-11-10 14:26:51 +0000861}