blob: e027bc99251c00e15ad192a31a40cc19ca2b64e3 [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>
Russell Kingf8ce2542006-01-07 16:15:52 +000022#include <linux/clk.h>
Russell Kingfced80c2008-09-06 12:10:45 +010023#include <linux/io.h>
Santosh Shilimkare03d37d2010-02-18 08:59:06 +000024#include <linux/delay.h>
Kevin Hilman6f251e92010-09-27 20:19:38 +053025#include <linux/platform_device.h>
26#include <linux/slab.h>
Kevin Hilman3244fcd2010-09-27 20:19:53 +053027#include <linux/pm_runtime.h>
Paul Walmsley0d8e2d02010-11-24 16:49:05 -070028#include <linux/console.h>
Kevin Hilman6f251e92010-09-27 20:19:38 +053029
Kevin Hilman6f251e92010-09-27 20:19:38 +053030#include <plat/omap-serial.h>
Tony Lindgren4e653312011-11-10 22:45:17 +010031#include "common.h"
Tony Lindgrence491cf2009-10-20 09:40:47 -070032#include <plat/board.h>
Kevin Hilman6f251e92010-09-27 20:19:38 +053033#include <plat/dma.h>
34#include <plat/omap_hwmod.h>
35#include <plat/omap_device.h>
Tony Lindgren1dbae812005-11-10 14:26:51 +000036
Paul Walmsley59fb6592010-12-21 15:30:55 -070037#include "prm2xxx_3xxx.h"
Kevin Hilman4af40162009-02-04 10:51:40 -080038#include "pm.h"
Paul Walmsley59fb6592010-12-21 15:30:55 -070039#include "cm2xxx_3xxx.h"
Kevin Hilman4af40162009-02-04 10:51:40 -080040#include "prm-regbits-34xx.h"
Paul Walmsley4814ced2010-10-08 11:40:20 -060041#include "control.h"
Tony Lindgren40e44392010-12-22 18:42:35 -080042#include "mux.h"
Kevin Hilman4af40162009-02-04 10:51:40 -080043
44#define UART_OMAP_WER 0x17 /* Wake-up enable register */
45
Deepak K00034502010-08-02 13:18:12 +030046#define UART_ERRATA_i202_MDR1_ACCESS (0x1 << 1)
Nishanth Menon5a927b32010-08-02 13:18:12 +030047
Tony Lindgren301fe8e2010-02-01 12:34:31 -080048/*
49 * NOTE: By default the serial timeout is disabled as it causes lost characters
50 * over the serial ports. This means that the UART clocks will stay on until
51 * disabled via sysfs. This also causes that any deeper omap sleep states are
52 * blocked.
53 */
54#define DEFAULT_TIMEOUT 0
Kevin Hilman4af40162009-02-04 10:51:40 -080055
Kevin Hilman6f251e92010-09-27 20:19:38 +053056#define MAX_UART_HWMOD_NAME_LEN 16
57
Kevin Hilman4af40162009-02-04 10:51:40 -080058struct omap_uart_state {
59 int num;
60 int can_sleep;
61 struct timer_list timer;
62 u32 timeout;
63
64 void __iomem *wk_st;
65 void __iomem *wk_en;
66 u32 wk_mask;
67 u32 padconf;
Kevin Hilman6f251e92010-09-27 20:19:38 +053068 u32 dma_enabled;
Kevin Hilman4af40162009-02-04 10:51:40 -080069
70 struct clk *ick;
71 struct clk *fck;
72 int clocked;
73
Kevin Hilman6f251e92010-09-27 20:19:38 +053074 int irq;
75 int regshift;
76 int irqflags;
77 void __iomem *membase;
78 resource_size_t mapbase;
79
Kevin Hilman4af40162009-02-04 10:51:40 -080080 struct list_head node;
Kevin Hilman6f251e92010-09-27 20:19:38 +053081 struct omap_hwmod *oh;
82 struct platform_device *pdev;
Kevin Hilman4af40162009-02-04 10:51:40 -080083
Nishanth Menon5a927b32010-08-02 13:18:12 +030084 u32 errata;
Kevin Hilman4af40162009-02-04 10:51:40 -080085#if defined(CONFIG_ARCH_OMAP3) && defined(CONFIG_PM)
86 int context_valid;
87
88 /* Registers to be saved/restored for OFF-mode */
89 u16 dll;
90 u16 dlh;
91 u16 ier;
92 u16 sysc;
93 u16 scr;
94 u16 wer;
Govindraj R5ade4ff2010-08-02 13:18:11 +030095 u16 mcr;
Kevin Hilman4af40162009-02-04 10:51:40 -080096#endif
97};
98
Kevin Hilman4af40162009-02-04 10:51:40 -080099static LIST_HEAD(uart_list);
Kevin Hilman6f251e92010-09-27 20:19:38 +0530100static u8 num_uarts;
Tony Lindgren1dbae812005-11-10 14:26:51 +0000101
Alexander Shishkin92303722010-01-08 10:29:06 -0800102static inline unsigned int __serial_read_reg(struct uart_port *up,
Kevin Hilman6f251e92010-09-27 20:19:38 +0530103 int offset)
Alexander Shishkin92303722010-01-08 10:29:06 -0800104{
105 offset <<= up->regshift;
106 return (unsigned int)__raw_readb(up->membase + offset);
107}
108
Kevin Hilman6f251e92010-09-27 20:19:38 +0530109static inline unsigned int serial_read_reg(struct omap_uart_state *uart,
Tony Lindgren1dbae812005-11-10 14:26:51 +0000110 int offset)
111{
Kevin Hilman6f251e92010-09-27 20:19:38 +0530112 offset <<= uart->regshift;
113 return (unsigned int)__raw_readb(uart->membase + offset);
Tony Lindgren1dbae812005-11-10 14:26:51 +0000114}
115
Santosh Shilimkare03d37d2010-02-18 08:59:06 +0000116static inline void __serial_write_reg(struct uart_port *up, int offset,
117 int value)
118{
119 offset <<= up->regshift;
120 __raw_writeb(value, up->membase + offset);
121}
122
Kevin Hilman6f251e92010-09-27 20:19:38 +0530123static inline void serial_write_reg(struct omap_uart_state *uart, int offset,
Tony Lindgren1dbae812005-11-10 14:26:51 +0000124 int value)
125{
Kevin Hilman6f251e92010-09-27 20:19:38 +0530126 offset <<= uart->regshift;
127 __raw_writeb(value, uart->membase + offset);
Tony Lindgren1dbae812005-11-10 14:26:51 +0000128}
129
130/*
131 * Internal UARTs need to be initialized for the 8250 autoconfig to work
132 * properly. Note that the TX watermark initialization may not be needed
133 * once the 8250.c watermark handling code is merged.
134 */
Kevin Hilman6f251e92010-09-27 20:19:38 +0530135
Kevin Hilman4af40162009-02-04 10:51:40 -0800136static inline void __init omap_uart_reset(struct omap_uart_state *uart)
Tony Lindgren1dbae812005-11-10 14:26:51 +0000137{
Andrei Emeltchenko498cb952010-11-30 14:11:49 -0800138 serial_write_reg(uart, UART_OMAP_MDR1, UART_OMAP_MDR1_DISABLE);
Kevin Hilman6f251e92010-09-27 20:19:38 +0530139 serial_write_reg(uart, UART_OMAP_SCR, 0x08);
Andrei Emeltchenko498cb952010-11-30 14:11:49 -0800140 serial_write_reg(uart, UART_OMAP_MDR1, UART_OMAP_MDR1_16X_MODE);
Tony Lindgren1dbae812005-11-10 14:26:51 +0000141}
142
Kevin Hilman4af40162009-02-04 10:51:40 -0800143#if defined(CONFIG_PM) && defined(CONFIG_ARCH_OMAP3)
144
Deepak K00034502010-08-02 13:18:12 +0300145/*
146 * Work Around for Errata i202 (3430 - 1.12, 3630 - 1.6)
147 * The access to uart register after MDR1 Access
148 * causes UART to corrupt data.
149 *
150 * Need a delay =
151 * 5 L4 clock cycles + 5 UART functional clock cycle (@48MHz = ~0.2uS)
152 * give 10 times as much
153 */
154static void omap_uart_mdr1_errataset(struct omap_uart_state *uart, u8 mdr1_val,
155 u8 fcr_val)
156{
Deepak K00034502010-08-02 13:18:12 +0300157 u8 timeout = 255;
158
Kevin Hilman6f251e92010-09-27 20:19:38 +0530159 serial_write_reg(uart, UART_OMAP_MDR1, mdr1_val);
Deepak K00034502010-08-02 13:18:12 +0300160 udelay(2);
Kevin Hilman6f251e92010-09-27 20:19:38 +0530161 serial_write_reg(uart, UART_FCR, fcr_val | UART_FCR_CLEAR_XMIT |
Deepak K00034502010-08-02 13:18:12 +0300162 UART_FCR_CLEAR_RCVR);
163 /*
164 * Wait for FIFO to empty: when empty, RX_FIFO_E bit is 0 and
165 * TX_FIFO_E bit is 1.
166 */
Kevin Hilman6f251e92010-09-27 20:19:38 +0530167 while (UART_LSR_THRE != (serial_read_reg(uart, UART_LSR) &
Deepak K00034502010-08-02 13:18:12 +0300168 (UART_LSR_THRE | UART_LSR_DR))) {
169 timeout--;
170 if (!timeout) {
171 /* Should *never* happen. we warn and carry on */
Kevin Hilman6f251e92010-09-27 20:19:38 +0530172 dev_crit(&uart->pdev->dev, "Errata i202: timedout %x\n",
173 serial_read_reg(uart, UART_LSR));
Deepak K00034502010-08-02 13:18:12 +0300174 break;
175 }
176 udelay(1);
177 }
178}
179
Kevin Hilman4af40162009-02-04 10:51:40 -0800180static void omap_uart_save_context(struct omap_uart_state *uart)
Jouni Hogander6e811762008-10-06 15:49:15 +0300181{
Kevin Hilman4af40162009-02-04 10:51:40 -0800182 u16 lcr = 0;
Kevin Hilman4af40162009-02-04 10:51:40 -0800183
184 if (!enable_off_mode)
185 return;
186
Kevin Hilman6f251e92010-09-27 20:19:38 +0530187 lcr = serial_read_reg(uart, UART_LCR);
Andrei Emeltchenko662b083a2010-11-30 14:11:49 -0800188 serial_write_reg(uart, UART_LCR, UART_LCR_CONF_MODE_B);
Kevin Hilman6f251e92010-09-27 20:19:38 +0530189 uart->dll = serial_read_reg(uart, UART_DLL);
190 uart->dlh = serial_read_reg(uart, UART_DLM);
191 serial_write_reg(uart, UART_LCR, lcr);
192 uart->ier = serial_read_reg(uart, UART_IER);
193 uart->sysc = serial_read_reg(uart, UART_OMAP_SYSC);
194 uart->scr = serial_read_reg(uart, UART_OMAP_SCR);
195 uart->wer = serial_read_reg(uart, UART_OMAP_WER);
Andrei Emeltchenko662b083a2010-11-30 14:11:49 -0800196 serial_write_reg(uart, UART_LCR, UART_LCR_CONF_MODE_A);
Kevin Hilman6f251e92010-09-27 20:19:38 +0530197 uart->mcr = serial_read_reg(uart, UART_MCR);
198 serial_write_reg(uart, UART_LCR, lcr);
Kevin Hilman4af40162009-02-04 10:51:40 -0800199
200 uart->context_valid = 1;
201}
202
203static void omap_uart_restore_context(struct omap_uart_state *uart)
204{
205 u16 efr = 0;
Kevin Hilman4af40162009-02-04 10:51:40 -0800206
207 if (!enable_off_mode)
208 return;
209
210 if (!uart->context_valid)
211 return;
212
213 uart->context_valid = 0;
214
Deepak K00034502010-08-02 13:18:12 +0300215 if (uart->errata & UART_ERRATA_i202_MDR1_ACCESS)
Andrei Emeltchenko498cb952010-11-30 14:11:49 -0800216 omap_uart_mdr1_errataset(uart, UART_OMAP_MDR1_DISABLE, 0xA0);
Deepak K00034502010-08-02 13:18:12 +0300217 else
Andrei Emeltchenko498cb952010-11-30 14:11:49 -0800218 serial_write_reg(uart, UART_OMAP_MDR1, UART_OMAP_MDR1_DISABLE);
219
Andrei Emeltchenko662b083a2010-11-30 14:11:49 -0800220 serial_write_reg(uart, UART_LCR, UART_LCR_CONF_MODE_B);
Kevin Hilman6f251e92010-09-27 20:19:38 +0530221 efr = serial_read_reg(uart, UART_EFR);
222 serial_write_reg(uart, UART_EFR, UART_EFR_ECB);
223 serial_write_reg(uart, UART_LCR, 0x0); /* Operational mode */
224 serial_write_reg(uart, UART_IER, 0x0);
Andrei Emeltchenko662b083a2010-11-30 14:11:49 -0800225 serial_write_reg(uart, UART_LCR, UART_LCR_CONF_MODE_B);
Kevin Hilman6f251e92010-09-27 20:19:38 +0530226 serial_write_reg(uart, UART_DLL, uart->dll);
227 serial_write_reg(uart, UART_DLM, uart->dlh);
228 serial_write_reg(uart, UART_LCR, 0x0); /* Operational mode */
229 serial_write_reg(uart, UART_IER, uart->ier);
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 serial_write_reg(uart, UART_MCR, uart->mcr);
Andrei Emeltchenko662b083a2010-11-30 14:11:49 -0800232 serial_write_reg(uart, UART_LCR, UART_LCR_CONF_MODE_B);
Kevin Hilman6f251e92010-09-27 20:19:38 +0530233 serial_write_reg(uart, UART_EFR, efr);
234 serial_write_reg(uart, UART_LCR, UART_LCR_WLEN8);
235 serial_write_reg(uart, UART_OMAP_SCR, uart->scr);
236 serial_write_reg(uart, UART_OMAP_WER, uart->wer);
237 serial_write_reg(uart, UART_OMAP_SYSC, uart->sysc);
Andrei Emeltchenko498cb952010-11-30 14:11:49 -0800238
Deepak K00034502010-08-02 13:18:12 +0300239 if (uart->errata & UART_ERRATA_i202_MDR1_ACCESS)
Andrei Emeltchenko498cb952010-11-30 14:11:49 -0800240 omap_uart_mdr1_errataset(uart, UART_OMAP_MDR1_16X_MODE, 0xA1);
Deepak K00034502010-08-02 13:18:12 +0300241 else
Kevin Hilman6f251e92010-09-27 20:19:38 +0530242 /* UART 16x mode */
Andrei Emeltchenko498cb952010-11-30 14:11:49 -0800243 serial_write_reg(uart, UART_OMAP_MDR1,
244 UART_OMAP_MDR1_16X_MODE);
Kevin Hilman4af40162009-02-04 10:51:40 -0800245}
246#else
247static inline void omap_uart_save_context(struct omap_uart_state *uart) {}
248static inline void omap_uart_restore_context(struct omap_uart_state *uart) {}
249#endif /* CONFIG_PM && CONFIG_ARCH_OMAP3 */
250
251static inline void omap_uart_enable_clocks(struct omap_uart_state *uart)
252{
253 if (uart->clocked)
254 return;
255
Kevin Hilman6f251e92010-09-27 20:19:38 +0530256 omap_device_enable(uart->pdev);
Kevin Hilman4af40162009-02-04 10:51:40 -0800257 uart->clocked = 1;
258 omap_uart_restore_context(uart);
259}
260
261#ifdef CONFIG_PM
262
263static inline void omap_uart_disable_clocks(struct omap_uart_state *uart)
264{
265 if (!uart->clocked)
266 return;
267
268 omap_uart_save_context(uart);
269 uart->clocked = 0;
Kevin Hilman6f251e92010-09-27 20:19:38 +0530270 omap_device_idle(uart->pdev);
Kevin Hilman4af40162009-02-04 10:51:40 -0800271}
272
Kevin Hilmanfd455ea2009-04-27 12:27:36 -0700273static void omap_uart_enable_wakeup(struct omap_uart_state *uart)
274{
275 /* Set wake-enable bit */
276 if (uart->wk_en && uart->wk_mask) {
277 u32 v = __raw_readl(uart->wk_en);
278 v |= uart->wk_mask;
279 __raw_writel(v, uart->wk_en);
280 }
281
282 /* Ensure IOPAD wake-enables are set */
283 if (cpu_is_omap34xx() && uart->padconf) {
284 u16 v = omap_ctrl_readw(uart->padconf);
285 v |= OMAP3_PADCONF_WAKEUPENABLE0;
286 omap_ctrl_writew(v, uart->padconf);
287 }
288}
289
290static void omap_uart_disable_wakeup(struct omap_uart_state *uart)
291{
292 /* Clear wake-enable bit */
293 if (uart->wk_en && uart->wk_mask) {
294 u32 v = __raw_readl(uart->wk_en);
295 v &= ~uart->wk_mask;
296 __raw_writel(v, uart->wk_en);
297 }
298
299 /* Ensure IOPAD wake-enables are cleared */
300 if (cpu_is_omap34xx() && uart->padconf) {
301 u16 v = omap_ctrl_readw(uart->padconf);
302 v &= ~OMAP3_PADCONF_WAKEUPENABLE0;
303 omap_ctrl_writew(v, uart->padconf);
304 }
305}
306
Kevin Hilman4af40162009-02-04 10:51:40 -0800307static void omap_uart_smart_idle_enable(struct omap_uart_state *uart,
Kevin Hilman6f251e92010-09-27 20:19:38 +0530308 int enable)
Kevin Hilman4af40162009-02-04 10:51:40 -0800309{
Kevin Hilman6f251e92010-09-27 20:19:38 +0530310 u8 idlemode;
Kevin Hilman4af40162009-02-04 10:51:40 -0800311
Kevin Hilman6f251e92010-09-27 20:19:38 +0530312 if (enable) {
313 /**
314 * Errata 2.15: [UART]:Cannot Acknowledge Idle Requests
315 * in Smartidle Mode When Configured for DMA Operations.
316 */
317 if (uart->dma_enabled)
318 idlemode = HWMOD_IDLEMODE_FORCE;
319 else
320 idlemode = HWMOD_IDLEMODE_SMART;
321 } else {
322 idlemode = HWMOD_IDLEMODE_NO;
323 }
Kevin Hilman4af40162009-02-04 10:51:40 -0800324
Kevin Hilman6f251e92010-09-27 20:19:38 +0530325 omap_hwmod_set_slave_idlemode(uart->oh, idlemode);
Kevin Hilman4af40162009-02-04 10:51:40 -0800326}
327
328static void omap_uart_block_sleep(struct omap_uart_state *uart)
329{
330 omap_uart_enable_clocks(uart);
331
332 omap_uart_smart_idle_enable(uart, 0);
333 uart->can_sleep = 0;
Jouni Hoganderba87a9b2008-12-09 13:36:50 +0200334 if (uart->timeout)
335 mod_timer(&uart->timer, jiffies + uart->timeout);
336 else
337 del_timer(&uart->timer);
Kevin Hilman4af40162009-02-04 10:51:40 -0800338}
339
340static void omap_uart_allow_sleep(struct omap_uart_state *uart)
341{
Kevin Hilman6f251e92010-09-27 20:19:38 +0530342 if (device_may_wakeup(&uart->pdev->dev))
Kevin Hilmanfd455ea2009-04-27 12:27:36 -0700343 omap_uart_enable_wakeup(uart);
344 else
345 omap_uart_disable_wakeup(uart);
346
Kevin Hilman4af40162009-02-04 10:51:40 -0800347 if (!uart->clocked)
348 return;
349
350 omap_uart_smart_idle_enable(uart, 1);
351 uart->can_sleep = 1;
352 del_timer(&uart->timer);
353}
354
355static void omap_uart_idle_timer(unsigned long data)
356{
357 struct omap_uart_state *uart = (struct omap_uart_state *)data;
358
359 omap_uart_allow_sleep(uart);
360}
361
Kevin Hilman4af40162009-02-04 10:51:40 -0800362int omap_uart_can_sleep(void)
363{
364 struct omap_uart_state *uart;
365 int can_sleep = 1;
366
367 list_for_each_entry(uart, &uart_list, node) {
368 if (!uart->clocked)
369 continue;
370
371 if (!uart->can_sleep) {
372 can_sleep = 0;
373 continue;
374 }
375
376 /* This UART can now safely sleep. */
377 omap_uart_allow_sleep(uart);
378 }
379
380 return can_sleep;
381}
382
383/**
384 * omap_uart_interrupt()
385 *
386 * This handler is used only to detect that *any* UART interrupt has
387 * occurred. It does _nothing_ to handle the interrupt. Rather,
388 * any UART interrupt will trigger the inactivity timer so the
389 * UART will not idle or sleep for its timeout period.
390 *
391 **/
Kevin Hilman6f251e92010-09-27 20:19:38 +0530392/* static int first_interrupt; */
Kevin Hilman4af40162009-02-04 10:51:40 -0800393static irqreturn_t omap_uart_interrupt(int irq, void *dev_id)
394{
395 struct omap_uart_state *uart = dev_id;
396
397 omap_uart_block_sleep(uart);
398
399 return IRQ_NONE;
400}
401
402static void omap_uart_idle_init(struct omap_uart_state *uart)
403{
Kevin Hilman4af40162009-02-04 10:51:40 -0800404 int ret;
405
406 uart->can_sleep = 0;
Kevin Hilmanfd455ea2009-04-27 12:27:36 -0700407 uart->timeout = DEFAULT_TIMEOUT;
Kevin Hilman4af40162009-02-04 10:51:40 -0800408 setup_timer(&uart->timer, omap_uart_idle_timer,
409 (unsigned long) uart);
Tony Lindgren301fe8e2010-02-01 12:34:31 -0800410 if (uart->timeout)
411 mod_timer(&uart->timer, jiffies + uart->timeout);
Kevin Hilman4af40162009-02-04 10:51:40 -0800412 omap_uart_smart_idle_enable(uart, 0);
413
Hemant Pedanekara9203602011-12-13 10:46:44 -0800414 if (cpu_is_omap34xx() && !(cpu_is_ti81xx() || cpu_is_am33xx())) {
Govindraj.R52663ae2010-09-27 20:20:41 +0530415 u32 mod = (uart->num > 1) ? OMAP3430_PER_MOD : CORE_MOD;
Kevin Hilman4af40162009-02-04 10:51:40 -0800416 u32 wk_mask = 0;
417 u32 padconf = 0;
418
Paul Walmsleyc4d7e582010-12-21 21:05:14 -0700419 /* XXX These PRM accesses do not belong here */
Kevin Hilman4af40162009-02-04 10:51:40 -0800420 uart->wk_en = OMAP34XX_PRM_REGADDR(mod, PM_WKEN1);
421 uart->wk_st = OMAP34XX_PRM_REGADDR(mod, PM_WKST1);
422 switch (uart->num) {
423 case 0:
424 wk_mask = OMAP3430_ST_UART1_MASK;
425 padconf = 0x182;
426 break;
427 case 1:
428 wk_mask = OMAP3430_ST_UART2_MASK;
429 padconf = 0x17a;
430 break;
431 case 2:
432 wk_mask = OMAP3430_ST_UART3_MASK;
433 padconf = 0x19e;
434 break;
Govindraj.R52663ae2010-09-27 20:20:41 +0530435 case 3:
436 wk_mask = OMAP3630_ST_UART4_MASK;
437 padconf = 0x0d2;
438 break;
Kevin Hilman4af40162009-02-04 10:51:40 -0800439 }
440 uart->wk_mask = wk_mask;
441 uart->padconf = padconf;
442 } else if (cpu_is_omap24xx()) {
443 u32 wk_mask = 0;
Kevin Hilmancb74f022010-10-20 23:19:03 +0000444 u32 wk_en = PM_WKEN1, wk_st = PM_WKST1;
Kevin Hilman4af40162009-02-04 10:51:40 -0800445
Kevin Hilman4af40162009-02-04 10:51:40 -0800446 switch (uart->num) {
447 case 0:
448 wk_mask = OMAP24XX_ST_UART1_MASK;
449 break;
450 case 1:
451 wk_mask = OMAP24XX_ST_UART2_MASK;
452 break;
453 case 2:
Kevin Hilmancb74f022010-10-20 23:19:03 +0000454 wk_en = OMAP24XX_PM_WKEN2;
455 wk_st = OMAP24XX_PM_WKST2;
Kevin Hilman4af40162009-02-04 10:51:40 -0800456 wk_mask = OMAP24XX_ST_UART3_MASK;
457 break;
458 }
459 uart->wk_mask = wk_mask;
Kevin Hilmancb74f022010-10-20 23:19:03 +0000460 if (cpu_is_omap2430()) {
461 uart->wk_en = OMAP2430_PRM_REGADDR(CORE_MOD, wk_en);
462 uart->wk_st = OMAP2430_PRM_REGADDR(CORE_MOD, wk_st);
463 } else if (cpu_is_omap2420()) {
464 uart->wk_en = OMAP2420_PRM_REGADDR(CORE_MOD, wk_en);
465 uart->wk_st = OMAP2420_PRM_REGADDR(CORE_MOD, wk_st);
466 }
Kevin Hilman4af40162009-02-04 10:51:40 -0800467 } else {
Nishanth Menonc54bae12010-08-02 13:18:11 +0300468 uart->wk_en = NULL;
469 uart->wk_st = NULL;
Kevin Hilman4af40162009-02-04 10:51:40 -0800470 uart->wk_mask = 0;
471 uart->padconf = 0;
472 }
473
Kevin Hilman6f251e92010-09-27 20:19:38 +0530474 uart->irqflags |= IRQF_SHARED;
475 ret = request_threaded_irq(uart->irq, NULL, omap_uart_interrupt,
476 IRQF_SHARED, "serial idle", (void *)uart);
Kevin Hilman4af40162009-02-04 10:51:40 -0800477 WARN_ON(ret);
478}
479
Kevin Hilmanfd455ea2009-04-27 12:27:36 -0700480static ssize_t sleep_timeout_show(struct device *dev,
481 struct device_attribute *attr,
Jouni Hoganderba87a9b2008-12-09 13:36:50 +0200482 char *buf)
483{
Kevin Hilman6f251e92010-09-27 20:19:38 +0530484 struct platform_device *pdev = to_platform_device(dev);
485 struct omap_device *odev = to_omap_device(pdev);
486 struct omap_uart_state *uart = odev->hwmods[0]->dev_attr;
Kevin Hilmanfd455ea2009-04-27 12:27:36 -0700487
488 return sprintf(buf, "%u\n", uart->timeout / HZ);
Jouni Hoganderba87a9b2008-12-09 13:36:50 +0200489}
490
Kevin Hilmanfd455ea2009-04-27 12:27:36 -0700491static ssize_t sleep_timeout_store(struct device *dev,
492 struct device_attribute *attr,
Jouni Hoganderba87a9b2008-12-09 13:36:50 +0200493 const char *buf, size_t n)
494{
Kevin Hilman6f251e92010-09-27 20:19:38 +0530495 struct platform_device *pdev = to_platform_device(dev);
496 struct omap_device *odev = to_omap_device(pdev);
497 struct omap_uart_state *uart = odev->hwmods[0]->dev_attr;
Jouni Hoganderba87a9b2008-12-09 13:36:50 +0200498 unsigned int value;
499
500 if (sscanf(buf, "%u", &value) != 1) {
Sergio Aguirre10c805e2010-03-09 13:22:14 -0600501 dev_err(dev, "sleep_timeout_store: Invalid value\n");
Jouni Hoganderba87a9b2008-12-09 13:36:50 +0200502 return -EINVAL;
503 }
Kevin Hilmanfd455ea2009-04-27 12:27:36 -0700504
505 uart->timeout = value * HZ;
506 if (uart->timeout)
507 mod_timer(&uart->timer, jiffies + uart->timeout);
508 else
509 /* A zero value means disable timeout feature */
510 omap_uart_block_sleep(uart);
511
Jouni Hoganderba87a9b2008-12-09 13:36:50 +0200512 return n;
513}
514
Nishanth Menonbfe69772010-08-02 13:18:12 +0300515static DEVICE_ATTR(sleep_timeout, 0644, sleep_timeout_show,
516 sleep_timeout_store);
Kevin Hilmanfd455ea2009-04-27 12:27:36 -0700517#define DEV_CREATE_FILE(dev, attr) WARN_ON(device_create_file(dev, attr))
Kevin Hilman4af40162009-02-04 10:51:40 -0800518#else
519static inline void omap_uart_idle_init(struct omap_uart_state *uart) {}
Santosh Shilimkara1b04cc2010-10-11 11:05:18 +0000520static void omap_uart_block_sleep(struct omap_uart_state *uart)
521{
522 /* Needed to enable UART clocks when built without CONFIG_PM */
523 omap_uart_enable_clocks(uart);
524}
Kevin Hilmanfd455ea2009-04-27 12:27:36 -0700525#define DEV_CREATE_FILE(dev, attr)
Kevin Hilman4af40162009-02-04 10:51:40 -0800526#endif /* CONFIG_PM */
527
Tony Lindgren3e16f922011-02-14 15:40:20 -0800528static int __init omap_serial_early_init(void)
Tony Lindgren1dbae812005-11-10 14:26:51 +0000529{
Kevin Hilman6f251e92010-09-27 20:19:38 +0530530 int i = 0;
Tony Lindgren1dbae812005-11-10 14:26:51 +0000531
Kevin Hilman6f251e92010-09-27 20:19:38 +0530532 do {
533 char oh_name[MAX_UART_HWMOD_NAME_LEN];
534 struct omap_hwmod *oh;
535 struct omap_uart_state *uart;
Thomas Weber21b90342010-02-25 09:40:19 +0000536
Kevin Hilman6f251e92010-09-27 20:19:38 +0530537 snprintf(oh_name, MAX_UART_HWMOD_NAME_LEN,
538 "uart%d", i + 1);
539 oh = omap_hwmod_lookup(oh_name);
540 if (!oh)
541 break;
Tony Lindgren1dbae812005-11-10 14:26:51 +0000542
Kevin Hilman6f251e92010-09-27 20:19:38 +0530543 uart = kzalloc(sizeof(struct omap_uart_state), GFP_KERNEL);
544 if (WARN_ON(!uart))
Tony Lindgren3e16f922011-02-14 15:40:20 -0800545 return -ENODEV;
Tony Lindgren1dbae812005-11-10 14:26:51 +0000546
Kevin Hilman6f251e92010-09-27 20:19:38 +0530547 uart->oh = oh;
548 uart->num = i++;
549 list_add_tail(&uart->node, &uart_list);
550 num_uarts++;
551
Tony Lindgren84f90c92009-10-16 09:53:00 -0700552 /*
Paul Walmsley550c8092011-02-28 11:58:14 -0700553 * NOTE: omap_hwmod_setup*() has not yet been called,
Kevin Hilman6f251e92010-09-27 20:19:38 +0530554 * so no hwmod functions will work yet.
Tony Lindgren84f90c92009-10-16 09:53:00 -0700555 */
Tony Lindgren84f90c92009-10-16 09:53:00 -0700556
Kevin Hilman6f251e92010-09-27 20:19:38 +0530557 /*
558 * During UART early init, device need to be probed
559 * to determine SoC specific init before omap_device
560 * is ready. Therefore, don't allow idle here
561 */
562 uart->oh->flags |= HWMOD_INIT_NO_IDLE | HWMOD_INIT_NO_RESET;
563 } while (1);
Tony Lindgren3e16f922011-02-14 15:40:20 -0800564
565 return 0;
Paul Walmsleyb3c6df32009-09-03 20:14:02 +0300566}
Tony Lindgren3e16f922011-02-14 15:40:20 -0800567core_initcall(omap_serial_early_init);
Paul Walmsleyb3c6df32009-09-03 20:14:02 +0300568
Mika Westerbergf62349e2009-12-11 16:16:35 -0800569/**
570 * omap_serial_init_port() - initialize single serial port
Tony Lindgren40e44392010-12-22 18:42:35 -0800571 * @bdata: port specific board data pointer
Mika Westerbergf62349e2009-12-11 16:16:35 -0800572 *
Tony Lindgren40e44392010-12-22 18:42:35 -0800573 * This function initialies serial driver for given port only.
Mika Westerbergf62349e2009-12-11 16:16:35 -0800574 * Platforms can call this function instead of omap_serial_init()
575 * if they don't plan to use all available UARTs as serial ports.
576 *
577 * Don't mix calls to omap_serial_init_port() and omap_serial_init(),
578 * use only one of the two.
579 */
Tony Lindgren40e44392010-12-22 18:42:35 -0800580void __init omap_serial_init_port(struct omap_board_data *bdata)
Mika Westerbergf62349e2009-12-11 16:16:35 -0800581{
582 struct omap_uart_state *uart;
Kevin Hilman6f251e92010-09-27 20:19:38 +0530583 struct omap_hwmod *oh;
Kevin Hilman3528c582011-07-21 13:48:45 -0700584 struct platform_device *pdev;
Kevin Hilman6f251e92010-09-27 20:19:38 +0530585 void *pdata = NULL;
586 u32 pdata_size = 0;
587 char *name;
Kevin Hilman6f251e92010-09-27 20:19:38 +0530588 struct omap_uart_port_info omap_up;
Mika Westerbergf62349e2009-12-11 16:16:35 -0800589
Tony Lindgren40e44392010-12-22 18:42:35 -0800590 if (WARN_ON(!bdata))
Sergio Aguirree88d5562010-02-27 14:13:43 -0600591 return;
Tony Lindgren40e44392010-12-22 18:42:35 -0800592 if (WARN_ON(bdata->id < 0))
593 return;
594 if (WARN_ON(bdata->id >= num_uarts))
Mika Westerbergf62349e2009-12-11 16:16:35 -0800595 return;
596
Kevin Hilman6f251e92010-09-27 20:19:38 +0530597 list_for_each_entry(uart, &uart_list, node)
Tony Lindgren40e44392010-12-22 18:42:35 -0800598 if (bdata->id == uart->num)
Kevin Hilman6f251e92010-09-27 20:19:38 +0530599 break;
600
601 oh = uart->oh;
602 uart->dma_enabled = 0;
Kevin Hilman6f251e92010-09-27 20:19:38 +0530603 name = DRIVER_NAME;
604
605 omap_up.dma_enabled = uart->dma_enabled;
606 omap_up.uartclk = OMAP24XX_BASE_BAUD * 16;
607 omap_up.mapbase = oh->slaves[0]->addr->pa_start;
608 omap_up.membase = omap_hwmod_get_mpu_rt_va(oh);
609 omap_up.irqflags = IRQF_SHARED;
610 omap_up.flags = UPF_BOOT_AUTOCONF | UPF_SHARE_IRQ;
611
612 pdata = &omap_up;
613 pdata_size = sizeof(struct omap_uart_port_info);
Kevin Hilman6f251e92010-09-27 20:19:38 +0530614
615 if (WARN_ON(!oh))
616 return;
617
Kevin Hilman3528c582011-07-21 13:48:45 -0700618 pdev = omap_device_build(name, uart->num, oh, pdata, pdata_size,
Benoit Coussonf718e2c2011-08-10 15:30:09 +0200619 NULL, 0, false);
Kevin Hilman3528c582011-07-21 13:48:45 -0700620 WARN(IS_ERR(pdev), "Could not build omap_device for %s: %s.\n",
Kevin Hilman6f251e92010-09-27 20:19:38 +0530621 name, oh->name);
622
Kevin Hilman9f8b6942011-08-01 09:33:13 -0700623 omap_device_disable_idle_on_suspend(pdev);
Tony Lindgren40e44392010-12-22 18:42:35 -0800624 oh->mux = omap_hwmod_mux_init(bdata->pads, bdata->pads_cnt);
625
Kevin Hilman6f251e92010-09-27 20:19:38 +0530626 uart->irq = oh->mpu_irqs[0].irq;
627 uart->regshift = 2;
628 uart->mapbase = oh->slaves[0]->addr->pa_start;
629 uart->membase = omap_hwmod_get_mpu_rt_va(oh);
Kevin Hilman3528c582011-07-21 13:48:45 -0700630 uart->pdev = pdev;
Kevin Hilman6f251e92010-09-27 20:19:38 +0530631
632 oh->dev_attr = uart;
633
Torben Hohnac751ef2011-01-25 15:07:35 -0800634 console_lock(); /* in case the earlycon is on the UART */
Paul Walmsley0d8e2d02010-11-24 16:49:05 -0700635
Kevin Hilman6f251e92010-09-27 20:19:38 +0530636 /*
637 * Because of early UART probing, UART did not get idled
638 * on init. Now that omap_device is ready, ensure full idle
639 * before doing omap_device_enable().
640 */
641 omap_hwmod_idle(uart->oh);
642
643 omap_device_enable(uart->pdev);
644 omap_uart_idle_init(uart);
645 omap_uart_reset(uart);
646 omap_hwmod_enable_wakeup(uart->oh);
647 omap_device_idle(uart->pdev);
648
649 /*
650 * Need to block sleep long enough for interrupt driven
651 * driver to start. Console driver is in polling mode
652 * so device needs to be kept enabled while polling driver
653 * is in use.
654 */
655 if (uart->timeout)
656 uart->timeout = (30 * HZ);
657 omap_uart_block_sleep(uart);
658 uart->timeout = DEFAULT_TIMEOUT;
659
Torben Hohnac751ef2011-01-25 15:07:35 -0800660 console_unlock();
Paul Walmsley0d8e2d02010-11-24 16:49:05 -0700661
Kevin Hilman6f251e92010-09-27 20:19:38 +0530662 if ((cpu_is_omap34xx() && uart->padconf) ||
663 (uart->wk_en && uart->wk_mask)) {
Kevin Hilman3528c582011-07-21 13:48:45 -0700664 device_init_wakeup(&pdev->dev, true);
665 DEV_CREATE_FILE(&pdev->dev, &dev_attr_sleep_timeout);
Santosh Shilimkare03d37d2010-02-18 08:59:06 +0000666 }
Deepak K00034502010-08-02 13:18:12 +0300667
668 /* Enable the MDR1 errata for OMAP3 */
Hemant Pedanekara9203602011-12-13 10:46:44 -0800669 if (cpu_is_omap34xx() && !(cpu_is_ti81xx() || cpu_is_am33xx()))
Deepak K00034502010-08-02 13:18:12 +0300670 uart->errata |= UART_ERRATA_i202_MDR1_ACCESS;
Mika Westerbergf62349e2009-12-11 16:16:35 -0800671}
672
673/**
Uwe Kleine-Königb5950762010-11-01 15:38:34 -0400674 * omap_serial_init() - initialize all supported serial ports
Mika Westerbergf62349e2009-12-11 16:16:35 -0800675 *
676 * Initializes all available UARTs as serial ports. Platforms
677 * can call this function when they want to have default behaviour
678 * for serial ports (e.g initialize them all as serial ports).
679 */
Paul Walmsleyb3c6df32009-09-03 20:14:02 +0300680void __init omap_serial_init(void)
681{
Kevin Hilman6f251e92010-09-27 20:19:38 +0530682 struct omap_uart_state *uart;
Tony Lindgren40e44392010-12-22 18:42:35 -0800683 struct omap_board_data bdata;
Paul Walmsleyb3c6df32009-09-03 20:14:02 +0300684
Tony Lindgren40e44392010-12-22 18:42:35 -0800685 list_for_each_entry(uart, &uart_list, node) {
686 bdata.id = uart->num;
687 bdata.flags = 0;
688 bdata.pads = NULL;
689 bdata.pads_cnt = 0;
690 omap_serial_init_port(&bdata);
691
692 }
Tony Lindgren1dbae812005-11-10 14:26:51 +0000693}