blob: e6c40273dafece34d7dec2fd1c6c997488760658 [file] [log] [blame]
Tony Lindgren1dbae812005-11-10 14:26:51 +00001/*
Uwe Zeisbergerf30c2262006-10-03 23:01:26 +02002 * arch/arm/mach-omap2/serial.c
Tony Lindgren1dbae812005-11-10 14:26:51 +00003 *
4 * OMAP2 serial support.
5 *
Jouni Hogander6e811762008-10-06 15:49:15 +03006 * Copyright (C) 2005-2008 Nokia Corporation
Tony Lindgren1dbae812005-11-10 14:26:51 +00007 * Author: Paul Mundt <paul.mundt@nokia.com>
8 *
Kevin Hilman4af40162009-02-04 10:51:40 -08009 * Major rework for PM support by Kevin Hilman
10 *
Tony Lindgren1dbae812005-11-10 14:26:51 +000011 * Based off of arch/arm/mach-omap/omap1/serial.c
12 *
Santosh Shilimkar44169072009-05-28 14:16:04 -070013 * Copyright (C) 2009 Texas Instruments
14 * Added OMAP4 support - Santosh Shilimkar <santosh.shilimkar@ti.com
15 *
Tony Lindgren1dbae812005-11-10 14:26:51 +000016 * This file is subject to the terms and conditions of the GNU General Public
17 * License. See the file "COPYING" in the main directory of this archive
18 * for more details.
19 */
20#include <linux/kernel.h>
21#include <linux/init.h>
Tony Lindgren1dbae812005-11-10 14:26:51 +000022#include <linux/serial_reg.h>
Russell Kingf8ce2542006-01-07 16:15:52 +000023#include <linux/clk.h>
Russell Kingfced80c2008-09-06 12:10:45 +010024#include <linux/io.h>
Santosh Shilimkare03d37d2010-02-18 08:59:06 +000025#include <linux/delay.h>
Kevin Hilman6f251e92010-09-27 20:19:38 +053026#include <linux/platform_device.h>
27#include <linux/slab.h>
28#include <linux/serial_8250.h>
29
30#ifdef CONFIG_SERIAL_OMAP
31#include <plat/omap-serial.h>
32#endif
Tony Lindgren1dbae812005-11-10 14:26:51 +000033
Tony Lindgrence491cf2009-10-20 09:40:47 -070034#include <plat/common.h>
35#include <plat/board.h>
36#include <plat/clock.h>
37#include <plat/control.h>
Kevin Hilman6f251e92010-09-27 20:19:38 +053038#include <plat/dma.h>
39#include <plat/omap_hwmod.h>
40#include <plat/omap_device.h>
Tony Lindgren1dbae812005-11-10 14:26:51 +000041
Kevin Hilman4af40162009-02-04 10:51:40 -080042#include "prm.h"
43#include "pm.h"
Kevin Hilman6f251e92010-09-27 20:19:38 +053044#include "cm.h"
Kevin Hilman4af40162009-02-04 10:51:40 -080045#include "prm-regbits-34xx.h"
46
vikram panditace13d472009-12-11 16:16:37 -080047#define UART_OMAP_NO_EMPTY_FIFO_READ_IP_REV 0x52
Kevin Hilman4af40162009-02-04 10:51:40 -080048#define UART_OMAP_WER 0x17 /* Wake-up enable register */
49
Nishanth Menon5a927b32010-08-02 13:18:12 +030050#define UART_ERRATA_FIFO_FULL_ABORT (0x1 << 0)
Deepak K00034502010-08-02 13:18:12 +030051#define UART_ERRATA_i202_MDR1_ACCESS (0x1 << 1)
Nishanth Menon5a927b32010-08-02 13:18:12 +030052
Tony Lindgren301fe8e2010-02-01 12:34:31 -080053/*
54 * NOTE: By default the serial timeout is disabled as it causes lost characters
55 * over the serial ports. This means that the UART clocks will stay on until
56 * disabled via sysfs. This also causes that any deeper omap sleep states are
57 * blocked.
58 */
59#define DEFAULT_TIMEOUT 0
Kevin Hilman4af40162009-02-04 10:51:40 -080060
Kevin Hilman6f251e92010-09-27 20:19:38 +053061#define MAX_UART_HWMOD_NAME_LEN 16
62
Kevin Hilman4af40162009-02-04 10:51:40 -080063struct omap_uart_state {
64 int num;
65 int can_sleep;
66 struct timer_list timer;
67 u32 timeout;
68
69 void __iomem *wk_st;
70 void __iomem *wk_en;
71 u32 wk_mask;
72 u32 padconf;
Kevin Hilman6f251e92010-09-27 20:19:38 +053073 u32 dma_enabled;
Kevin Hilman4af40162009-02-04 10:51:40 -080074
75 struct clk *ick;
76 struct clk *fck;
77 int clocked;
78
Kevin Hilman6f251e92010-09-27 20:19:38 +053079 int irq;
80 int regshift;
81 int irqflags;
82 void __iomem *membase;
83 resource_size_t mapbase;
84
Kevin Hilman4af40162009-02-04 10:51:40 -080085 struct list_head node;
Kevin Hilman6f251e92010-09-27 20:19:38 +053086 struct omap_hwmod *oh;
87 struct platform_device *pdev;
Kevin Hilman4af40162009-02-04 10:51:40 -080088
Nishanth Menon5a927b32010-08-02 13:18:12 +030089 u32 errata;
Kevin Hilman4af40162009-02-04 10:51:40 -080090#if defined(CONFIG_ARCH_OMAP3) && defined(CONFIG_PM)
91 int context_valid;
92
93 /* Registers to be saved/restored for OFF-mode */
94 u16 dll;
95 u16 dlh;
96 u16 ier;
97 u16 sysc;
98 u16 scr;
99 u16 wer;
Govindraj R5ade4ff2010-08-02 13:18:11 +0300100 u16 mcr;
Kevin Hilman4af40162009-02-04 10:51:40 -0800101#endif
102};
103
Kevin Hilman4af40162009-02-04 10:51:40 -0800104static LIST_HEAD(uart_list);
Kevin Hilman6f251e92010-09-27 20:19:38 +0530105static u8 num_uarts;
Tony Lindgren1dbae812005-11-10 14:26:51 +0000106
Tony Lindgrena3a9b362010-02-15 09:27:25 -0800107
Tony Lindgren4f2c49f2010-02-15 08:48:53 -0800108void __init omap2_set_globals_uart(struct omap_globals *omap2_globals)
109{
Tony Lindgren4f2c49f2010-02-15 08:48:53 -0800110}
111
Kevin Hilman6f251e92010-09-27 20:19:38 +0530112static struct omap_device_pm_latency omap_uart_latency[] = {
113 {
114 .deactivate_func = omap_device_idle_hwmods,
115 .activate_func = omap_device_enable_hwmods,
116 .flags = OMAP_DEVICE_LATENCY_AUTO_ADJUST,
117 },
118};
119
Alexander Shishkin92303722010-01-08 10:29:06 -0800120static inline unsigned int __serial_read_reg(struct uart_port *up,
Kevin Hilman6f251e92010-09-27 20:19:38 +0530121 int offset)
Alexander Shishkin92303722010-01-08 10:29:06 -0800122{
123 offset <<= up->regshift;
124 return (unsigned int)__raw_readb(up->membase + offset);
125}
126
Kevin Hilman6f251e92010-09-27 20:19:38 +0530127static inline unsigned int serial_read_reg(struct omap_uart_state *uart,
Tony Lindgren1dbae812005-11-10 14:26:51 +0000128 int offset)
129{
Kevin Hilman6f251e92010-09-27 20:19:38 +0530130 offset <<= uart->regshift;
131 return (unsigned int)__raw_readb(uart->membase + offset);
Tony Lindgren1dbae812005-11-10 14:26:51 +0000132}
133
Santosh Shilimkare03d37d2010-02-18 08:59:06 +0000134static inline void __serial_write_reg(struct uart_port *up, int offset,
135 int value)
136{
137 offset <<= up->regshift;
138 __raw_writeb(value, up->membase + offset);
139}
140
Kevin Hilman6f251e92010-09-27 20:19:38 +0530141static inline void serial_write_reg(struct omap_uart_state *uart, int offset,
Tony Lindgren1dbae812005-11-10 14:26:51 +0000142 int value)
143{
Kevin Hilman6f251e92010-09-27 20:19:38 +0530144 offset <<= uart->regshift;
145 __raw_writeb(value, uart->membase + offset);
Tony Lindgren1dbae812005-11-10 14:26:51 +0000146}
147
148/*
149 * Internal UARTs need to be initialized for the 8250 autoconfig to work
150 * properly. Note that the TX watermark initialization may not be needed
151 * once the 8250.c watermark handling code is merged.
152 */
Kevin Hilman6f251e92010-09-27 20:19:38 +0530153
Kevin Hilman4af40162009-02-04 10:51:40 -0800154static inline void __init omap_uart_reset(struct omap_uart_state *uart)
Tony Lindgren1dbae812005-11-10 14:26:51 +0000155{
Kevin Hilman6f251e92010-09-27 20:19:38 +0530156 serial_write_reg(uart, UART_OMAP_MDR1, 0x07);
157 serial_write_reg(uart, UART_OMAP_SCR, 0x08);
158 serial_write_reg(uart, UART_OMAP_MDR1, 0x00);
Tony Lindgren1dbae812005-11-10 14:26:51 +0000159}
160
Kevin Hilman4af40162009-02-04 10:51:40 -0800161#if defined(CONFIG_PM) && defined(CONFIG_ARCH_OMAP3)
162
Deepak K00034502010-08-02 13:18:12 +0300163/*
164 * Work Around for Errata i202 (3430 - 1.12, 3630 - 1.6)
165 * The access to uart register after MDR1 Access
166 * causes UART to corrupt data.
167 *
168 * Need a delay =
169 * 5 L4 clock cycles + 5 UART functional clock cycle (@48MHz = ~0.2uS)
170 * give 10 times as much
171 */
172static void omap_uart_mdr1_errataset(struct omap_uart_state *uart, u8 mdr1_val,
173 u8 fcr_val)
174{
Deepak K00034502010-08-02 13:18:12 +0300175 u8 timeout = 255;
176
Kevin Hilman6f251e92010-09-27 20:19:38 +0530177 serial_write_reg(uart, UART_OMAP_MDR1, mdr1_val);
Deepak K00034502010-08-02 13:18:12 +0300178 udelay(2);
Kevin Hilman6f251e92010-09-27 20:19:38 +0530179 serial_write_reg(uart, UART_FCR, fcr_val | UART_FCR_CLEAR_XMIT |
Deepak K00034502010-08-02 13:18:12 +0300180 UART_FCR_CLEAR_RCVR);
181 /*
182 * Wait for FIFO to empty: when empty, RX_FIFO_E bit is 0 and
183 * TX_FIFO_E bit is 1.
184 */
Kevin Hilman6f251e92010-09-27 20:19:38 +0530185 while (UART_LSR_THRE != (serial_read_reg(uart, UART_LSR) &
Deepak K00034502010-08-02 13:18:12 +0300186 (UART_LSR_THRE | UART_LSR_DR))) {
187 timeout--;
188 if (!timeout) {
189 /* Should *never* happen. we warn and carry on */
Kevin Hilman6f251e92010-09-27 20:19:38 +0530190 dev_crit(&uart->pdev->dev, "Errata i202: timedout %x\n",
191 serial_read_reg(uart, UART_LSR));
Deepak K00034502010-08-02 13:18:12 +0300192 break;
193 }
194 udelay(1);
195 }
196}
197
Kevin Hilman4af40162009-02-04 10:51:40 -0800198static void omap_uart_save_context(struct omap_uart_state *uart)
Jouni Hogander6e811762008-10-06 15:49:15 +0300199{
Kevin Hilman4af40162009-02-04 10:51:40 -0800200 u16 lcr = 0;
Kevin Hilman4af40162009-02-04 10:51:40 -0800201
202 if (!enable_off_mode)
203 return;
204
Kevin Hilman6f251e92010-09-27 20:19:38 +0530205 lcr = serial_read_reg(uart, UART_LCR);
206 serial_write_reg(uart, UART_LCR, 0xBF);
207 uart->dll = serial_read_reg(uart, UART_DLL);
208 uart->dlh = serial_read_reg(uart, UART_DLM);
209 serial_write_reg(uart, UART_LCR, lcr);
210 uart->ier = serial_read_reg(uart, UART_IER);
211 uart->sysc = serial_read_reg(uart, UART_OMAP_SYSC);
212 uart->scr = serial_read_reg(uart, UART_OMAP_SCR);
213 uart->wer = serial_read_reg(uart, UART_OMAP_WER);
214 serial_write_reg(uart, UART_LCR, 0x80);
215 uart->mcr = serial_read_reg(uart, UART_MCR);
216 serial_write_reg(uart, UART_LCR, lcr);
Kevin Hilman4af40162009-02-04 10:51:40 -0800217
218 uart->context_valid = 1;
219}
220
221static void omap_uart_restore_context(struct omap_uart_state *uart)
222{
223 u16 efr = 0;
Kevin Hilman4af40162009-02-04 10:51:40 -0800224
225 if (!enable_off_mode)
226 return;
227
228 if (!uart->context_valid)
229 return;
230
231 uart->context_valid = 0;
232
Deepak K00034502010-08-02 13:18:12 +0300233 if (uart->errata & UART_ERRATA_i202_MDR1_ACCESS)
234 omap_uart_mdr1_errataset(uart, 0x07, 0xA0);
235 else
Kevin Hilman6f251e92010-09-27 20:19:38 +0530236 serial_write_reg(uart, UART_OMAP_MDR1, 0x7);
237 serial_write_reg(uart, UART_LCR, 0xBF); /* Config B mode */
238 efr = serial_read_reg(uart, UART_EFR);
239 serial_write_reg(uart, UART_EFR, UART_EFR_ECB);
240 serial_write_reg(uart, UART_LCR, 0x0); /* Operational mode */
241 serial_write_reg(uart, UART_IER, 0x0);
242 serial_write_reg(uart, UART_LCR, 0xBF); /* Config B mode */
243 serial_write_reg(uart, UART_DLL, uart->dll);
244 serial_write_reg(uart, UART_DLM, uart->dlh);
245 serial_write_reg(uart, UART_LCR, 0x0); /* Operational mode */
246 serial_write_reg(uart, UART_IER, uart->ier);
247 serial_write_reg(uart, UART_LCR, 0x80);
248 serial_write_reg(uart, UART_MCR, uart->mcr);
249 serial_write_reg(uart, UART_LCR, 0xBF); /* Config B mode */
250 serial_write_reg(uart, UART_EFR, efr);
251 serial_write_reg(uart, UART_LCR, UART_LCR_WLEN8);
252 serial_write_reg(uart, UART_OMAP_SCR, uart->scr);
253 serial_write_reg(uart, UART_OMAP_WER, uart->wer);
254 serial_write_reg(uart, UART_OMAP_SYSC, uart->sysc);
Deepak K00034502010-08-02 13:18:12 +0300255 if (uart->errata & UART_ERRATA_i202_MDR1_ACCESS)
256 omap_uart_mdr1_errataset(uart, 0x00, 0xA1);
257 else
Kevin Hilman6f251e92010-09-27 20:19:38 +0530258 /* UART 16x mode */
259 serial_write_reg(uart, UART_OMAP_MDR1, 0x00);
Kevin Hilman4af40162009-02-04 10:51:40 -0800260}
261#else
262static inline void omap_uart_save_context(struct omap_uart_state *uart) {}
263static inline void omap_uart_restore_context(struct omap_uart_state *uart) {}
264#endif /* CONFIG_PM && CONFIG_ARCH_OMAP3 */
265
266static inline void omap_uart_enable_clocks(struct omap_uart_state *uart)
267{
268 if (uart->clocked)
269 return;
270
Kevin Hilman6f251e92010-09-27 20:19:38 +0530271 omap_device_enable(uart->pdev);
Kevin Hilman4af40162009-02-04 10:51:40 -0800272 uart->clocked = 1;
273 omap_uart_restore_context(uart);
274}
275
276#ifdef CONFIG_PM
277
278static inline void omap_uart_disable_clocks(struct omap_uart_state *uart)
279{
280 if (!uart->clocked)
281 return;
282
283 omap_uart_save_context(uart);
284 uart->clocked = 0;
Kevin Hilman6f251e92010-09-27 20:19:38 +0530285 omap_device_idle(uart->pdev);
Kevin Hilman4af40162009-02-04 10:51:40 -0800286}
287
Kevin Hilmanfd455ea2009-04-27 12:27:36 -0700288static void omap_uart_enable_wakeup(struct omap_uart_state *uart)
289{
290 /* Set wake-enable bit */
291 if (uart->wk_en && uart->wk_mask) {
292 u32 v = __raw_readl(uart->wk_en);
293 v |= uart->wk_mask;
294 __raw_writel(v, uart->wk_en);
295 }
296
297 /* Ensure IOPAD wake-enables are set */
298 if (cpu_is_omap34xx() && uart->padconf) {
299 u16 v = omap_ctrl_readw(uart->padconf);
300 v |= OMAP3_PADCONF_WAKEUPENABLE0;
301 omap_ctrl_writew(v, uart->padconf);
302 }
303}
304
305static void omap_uart_disable_wakeup(struct omap_uart_state *uart)
306{
307 /* Clear wake-enable bit */
308 if (uart->wk_en && uart->wk_mask) {
309 u32 v = __raw_readl(uart->wk_en);
310 v &= ~uart->wk_mask;
311 __raw_writel(v, uart->wk_en);
312 }
313
314 /* Ensure IOPAD wake-enables are cleared */
315 if (cpu_is_omap34xx() && uart->padconf) {
316 u16 v = omap_ctrl_readw(uart->padconf);
317 v &= ~OMAP3_PADCONF_WAKEUPENABLE0;
318 omap_ctrl_writew(v, uart->padconf);
319 }
320}
321
Kevin Hilman4af40162009-02-04 10:51:40 -0800322static void omap_uart_smart_idle_enable(struct omap_uart_state *uart,
Kevin Hilman6f251e92010-09-27 20:19:38 +0530323 int enable)
Kevin Hilman4af40162009-02-04 10:51:40 -0800324{
Kevin Hilman6f251e92010-09-27 20:19:38 +0530325 u8 idlemode;
Kevin Hilman4af40162009-02-04 10:51:40 -0800326
Kevin Hilman6f251e92010-09-27 20:19:38 +0530327 if (enable) {
328 /**
329 * Errata 2.15: [UART]:Cannot Acknowledge Idle Requests
330 * in Smartidle Mode When Configured for DMA Operations.
331 */
332 if (uart->dma_enabled)
333 idlemode = HWMOD_IDLEMODE_FORCE;
334 else
335 idlemode = HWMOD_IDLEMODE_SMART;
336 } else {
337 idlemode = HWMOD_IDLEMODE_NO;
338 }
Kevin Hilman4af40162009-02-04 10:51:40 -0800339
Kevin Hilman6f251e92010-09-27 20:19:38 +0530340 omap_hwmod_set_slave_idlemode(uart->oh, idlemode);
Kevin Hilman4af40162009-02-04 10:51:40 -0800341}
342
343static void omap_uart_block_sleep(struct omap_uart_state *uart)
344{
345 omap_uart_enable_clocks(uart);
346
347 omap_uart_smart_idle_enable(uart, 0);
348 uart->can_sleep = 0;
Jouni Hoganderba87a9b2008-12-09 13:36:50 +0200349 if (uart->timeout)
350 mod_timer(&uart->timer, jiffies + uart->timeout);
351 else
352 del_timer(&uart->timer);
Kevin Hilman4af40162009-02-04 10:51:40 -0800353}
354
355static void omap_uart_allow_sleep(struct omap_uart_state *uart)
356{
Kevin Hilman6f251e92010-09-27 20:19:38 +0530357 if (device_may_wakeup(&uart->pdev->dev))
Kevin Hilmanfd455ea2009-04-27 12:27:36 -0700358 omap_uart_enable_wakeup(uart);
359 else
360 omap_uart_disable_wakeup(uart);
361
Kevin Hilman4af40162009-02-04 10:51:40 -0800362 if (!uart->clocked)
363 return;
364
365 omap_uart_smart_idle_enable(uart, 1);
366 uart->can_sleep = 1;
367 del_timer(&uart->timer);
368}
369
370static void omap_uart_idle_timer(unsigned long data)
371{
372 struct omap_uart_state *uart = (struct omap_uart_state *)data;
373
374 omap_uart_allow_sleep(uart);
375}
376
377void omap_uart_prepare_idle(int num)
378{
379 struct omap_uart_state *uart;
380
381 list_for_each_entry(uart, &uart_list, node) {
382 if (num == uart->num && uart->can_sleep) {
383 omap_uart_disable_clocks(uart);
384 return;
Jouni Hogander6e811762008-10-06 15:49:15 +0300385 }
386 }
387}
388
Kevin Hilman4af40162009-02-04 10:51:40 -0800389void omap_uart_resume_idle(int num)
390{
391 struct omap_uart_state *uart;
392
393 list_for_each_entry(uart, &uart_list, node) {
394 if (num == uart->num) {
395 omap_uart_enable_clocks(uart);
396
397 /* Check for IO pad wakeup */
398 if (cpu_is_omap34xx() && uart->padconf) {
399 u16 p = omap_ctrl_readw(uart->padconf);
400
401 if (p & OMAP3_PADCONF_WAKEUPEVENT0)
402 omap_uart_block_sleep(uart);
403 }
404
405 /* Check for normal UART wakeup */
406 if (__raw_readl(uart->wk_st) & uart->wk_mask)
407 omap_uart_block_sleep(uart);
Kevin Hilman4af40162009-02-04 10:51:40 -0800408 return;
409 }
410 }
411}
412
413void omap_uart_prepare_suspend(void)
414{
415 struct omap_uart_state *uart;
416
417 list_for_each_entry(uart, &uart_list, node) {
418 omap_uart_allow_sleep(uart);
419 }
420}
421
422int omap_uart_can_sleep(void)
423{
424 struct omap_uart_state *uart;
425 int can_sleep = 1;
426
427 list_for_each_entry(uart, &uart_list, node) {
428 if (!uart->clocked)
429 continue;
430
431 if (!uart->can_sleep) {
432 can_sleep = 0;
433 continue;
434 }
435
436 /* This UART can now safely sleep. */
437 omap_uart_allow_sleep(uart);
438 }
439
440 return can_sleep;
441}
442
443/**
444 * omap_uart_interrupt()
445 *
446 * This handler is used only to detect that *any* UART interrupt has
447 * occurred. It does _nothing_ to handle the interrupt. Rather,
448 * any UART interrupt will trigger the inactivity timer so the
449 * UART will not idle or sleep for its timeout period.
450 *
451 **/
Kevin Hilman6f251e92010-09-27 20:19:38 +0530452/* static int first_interrupt; */
Kevin Hilman4af40162009-02-04 10:51:40 -0800453static irqreturn_t omap_uart_interrupt(int irq, void *dev_id)
454{
455 struct omap_uart_state *uart = dev_id;
456
457 omap_uart_block_sleep(uart);
458
459 return IRQ_NONE;
460}
461
462static void omap_uart_idle_init(struct omap_uart_state *uart)
463{
Kevin Hilman4af40162009-02-04 10:51:40 -0800464 int ret;
465
466 uart->can_sleep = 0;
Kevin Hilmanfd455ea2009-04-27 12:27:36 -0700467 uart->timeout = DEFAULT_TIMEOUT;
Kevin Hilman4af40162009-02-04 10:51:40 -0800468 setup_timer(&uart->timer, omap_uart_idle_timer,
469 (unsigned long) uart);
Tony Lindgren301fe8e2010-02-01 12:34:31 -0800470 if (uart->timeout)
471 mod_timer(&uart->timer, jiffies + uart->timeout);
Kevin Hilman4af40162009-02-04 10:51:40 -0800472 omap_uart_smart_idle_enable(uart, 0);
473
474 if (cpu_is_omap34xx()) {
475 u32 mod = (uart->num == 2) ? OMAP3430_PER_MOD : CORE_MOD;
476 u32 wk_mask = 0;
477 u32 padconf = 0;
478
479 uart->wk_en = OMAP34XX_PRM_REGADDR(mod, PM_WKEN1);
480 uart->wk_st = OMAP34XX_PRM_REGADDR(mod, PM_WKST1);
481 switch (uart->num) {
482 case 0:
483 wk_mask = OMAP3430_ST_UART1_MASK;
484 padconf = 0x182;
485 break;
486 case 1:
487 wk_mask = OMAP3430_ST_UART2_MASK;
488 padconf = 0x17a;
489 break;
490 case 2:
491 wk_mask = OMAP3430_ST_UART3_MASK;
492 padconf = 0x19e;
493 break;
494 }
495 uart->wk_mask = wk_mask;
496 uart->padconf = padconf;
497 } else if (cpu_is_omap24xx()) {
498 u32 wk_mask = 0;
499
500 if (cpu_is_omap2430()) {
501 uart->wk_en = OMAP2430_PRM_REGADDR(CORE_MOD, PM_WKEN1);
502 uart->wk_st = OMAP2430_PRM_REGADDR(CORE_MOD, PM_WKST1);
503 } else if (cpu_is_omap2420()) {
504 uart->wk_en = OMAP2420_PRM_REGADDR(CORE_MOD, PM_WKEN1);
505 uart->wk_st = OMAP2420_PRM_REGADDR(CORE_MOD, PM_WKST1);
506 }
507 switch (uart->num) {
508 case 0:
509 wk_mask = OMAP24XX_ST_UART1_MASK;
510 break;
511 case 1:
512 wk_mask = OMAP24XX_ST_UART2_MASK;
513 break;
514 case 2:
515 wk_mask = OMAP24XX_ST_UART3_MASK;
516 break;
517 }
518 uart->wk_mask = wk_mask;
519 } else {
Nishanth Menonc54bae12010-08-02 13:18:11 +0300520 uart->wk_en = NULL;
521 uart->wk_st = NULL;
Kevin Hilman4af40162009-02-04 10:51:40 -0800522 uart->wk_mask = 0;
523 uart->padconf = 0;
524 }
525
Kevin Hilman6f251e92010-09-27 20:19:38 +0530526 uart->irqflags |= IRQF_SHARED;
527 ret = request_threaded_irq(uart->irq, NULL, omap_uart_interrupt,
528 IRQF_SHARED, "serial idle", (void *)uart);
Kevin Hilman4af40162009-02-04 10:51:40 -0800529 WARN_ON(ret);
530}
531
Tero Kristo24662112009-03-05 16:32:23 +0200532void omap_uart_enable_irqs(int enable)
Jouni Hoganderba87a9b2008-12-09 13:36:50 +0200533{
Tero Kristo24662112009-03-05 16:32:23 +0200534 int ret;
535 struct omap_uart_state *uart;
536
537 list_for_each_entry(uart, &uart_list, node) {
538 if (enable)
Kevin Hilman6f251e92010-09-27 20:19:38 +0530539 ret = request_threaded_irq(uart->irq, NULL,
540 omap_uart_interrupt,
541 IRQF_SHARED,
542 "serial idle",
543 (void *)uart);
Tero Kristo24662112009-03-05 16:32:23 +0200544 else
Kevin Hilman6f251e92010-09-27 20:19:38 +0530545 free_irq(uart->irq, (void *)uart);
Tero Kristo24662112009-03-05 16:32:23 +0200546 }
Jouni Hoganderba87a9b2008-12-09 13:36:50 +0200547}
548
Kevin Hilmanfd455ea2009-04-27 12:27:36 -0700549static ssize_t sleep_timeout_show(struct device *dev,
550 struct device_attribute *attr,
Jouni Hoganderba87a9b2008-12-09 13:36:50 +0200551 char *buf)
552{
Kevin Hilman6f251e92010-09-27 20:19:38 +0530553 struct platform_device *pdev = to_platform_device(dev);
554 struct omap_device *odev = to_omap_device(pdev);
555 struct omap_uart_state *uart = odev->hwmods[0]->dev_attr;
Kevin Hilmanfd455ea2009-04-27 12:27:36 -0700556
557 return sprintf(buf, "%u\n", uart->timeout / HZ);
Jouni Hoganderba87a9b2008-12-09 13:36:50 +0200558}
559
Kevin Hilmanfd455ea2009-04-27 12:27:36 -0700560static ssize_t sleep_timeout_store(struct device *dev,
561 struct device_attribute *attr,
Jouni Hoganderba87a9b2008-12-09 13:36:50 +0200562 const char *buf, size_t n)
563{
Kevin Hilman6f251e92010-09-27 20:19:38 +0530564 struct platform_device *pdev = to_platform_device(dev);
565 struct omap_device *odev = to_omap_device(pdev);
566 struct omap_uart_state *uart = odev->hwmods[0]->dev_attr;
Jouni Hoganderba87a9b2008-12-09 13:36:50 +0200567 unsigned int value;
568
569 if (sscanf(buf, "%u", &value) != 1) {
Sergio Aguirre10c805e2010-03-09 13:22:14 -0600570 dev_err(dev, "sleep_timeout_store: Invalid value\n");
Jouni Hoganderba87a9b2008-12-09 13:36:50 +0200571 return -EINVAL;
572 }
Kevin Hilmanfd455ea2009-04-27 12:27:36 -0700573
574 uart->timeout = value * HZ;
575 if (uart->timeout)
576 mod_timer(&uart->timer, jiffies + uart->timeout);
577 else
578 /* A zero value means disable timeout feature */
579 omap_uart_block_sleep(uart);
580
Jouni Hoganderba87a9b2008-12-09 13:36:50 +0200581 return n;
582}
583
Nishanth Menonbfe69772010-08-02 13:18:12 +0300584static DEVICE_ATTR(sleep_timeout, 0644, sleep_timeout_show,
585 sleep_timeout_store);
Kevin Hilmanfd455ea2009-04-27 12:27:36 -0700586#define DEV_CREATE_FILE(dev, attr) WARN_ON(device_create_file(dev, attr))
Kevin Hilman4af40162009-02-04 10:51:40 -0800587#else
588static inline void omap_uart_idle_init(struct omap_uart_state *uart) {}
Kevin Hilman6f251e92010-09-27 20:19:38 +0530589static void omap_uart_block_sleep(struct omap_uart_state *uart) {}
Kevin Hilmanfd455ea2009-04-27 12:27:36 -0700590#define DEV_CREATE_FILE(dev, attr)
Kevin Hilman4af40162009-02-04 10:51:40 -0800591#endif /* CONFIG_PM */
592
Kevin Hilman6f251e92010-09-27 20:19:38 +0530593#ifndef CONFIG_SERIAL_OMAP
vikram panditace13d472009-12-11 16:16:37 -0800594/*
595 * Override the default 8250 read handler: mem_serial_in()
596 * Empty RX fifo read causes an abort on omap3630 and omap4
597 * This function makes sure that an empty rx fifo is not read on these silicons
598 * (OMAP1/2/3430 are not affected)
599 */
600static unsigned int serial_in_override(struct uart_port *up, int offset)
601{
602 if (UART_RX == offset) {
603 unsigned int lsr;
Alexander Shishkin92303722010-01-08 10:29:06 -0800604 lsr = __serial_read_reg(up, UART_LSR);
vikram panditace13d472009-12-11 16:16:37 -0800605 if (!(lsr & UART_LSR_DR))
606 return -EPERM;
607 }
Alexander Shishkin92303722010-01-08 10:29:06 -0800608
609 return __serial_read_reg(up, offset);
vikram panditace13d472009-12-11 16:16:37 -0800610}
611
Santosh Shilimkare03d37d2010-02-18 08:59:06 +0000612static void serial_out_override(struct uart_port *up, int offset, int value)
613{
614 unsigned int status, tmout = 10000;
615
616 status = __serial_read_reg(up, UART_LSR);
617 while (!(status & UART_LSR_THRE)) {
618 /* Wait up to 10ms for the character(s) to be sent. */
619 if (--tmout == 0)
620 break;
621 udelay(1);
622 status = __serial_read_reg(up, UART_LSR);
623 }
624 __serial_write_reg(up, offset, value);
625}
Kevin Hilman6f251e92010-09-27 20:19:38 +0530626#endif
627
Paul Walmsleyb3c6df32009-09-03 20:14:02 +0300628void __init omap_serial_early_init(void)
Tony Lindgren1dbae812005-11-10 14:26:51 +0000629{
Kevin Hilman6f251e92010-09-27 20:19:38 +0530630 int i = 0;
Tony Lindgren1dbae812005-11-10 14:26:51 +0000631
Kevin Hilman6f251e92010-09-27 20:19:38 +0530632 do {
633 char oh_name[MAX_UART_HWMOD_NAME_LEN];
634 struct omap_hwmod *oh;
635 struct omap_uart_state *uart;
Thomas Weber21b90342010-02-25 09:40:19 +0000636
Kevin Hilman6f251e92010-09-27 20:19:38 +0530637 snprintf(oh_name, MAX_UART_HWMOD_NAME_LEN,
638 "uart%d", i + 1);
639 oh = omap_hwmod_lookup(oh_name);
640 if (!oh)
641 break;
Tony Lindgren1dbae812005-11-10 14:26:51 +0000642
Kevin Hilman6f251e92010-09-27 20:19:38 +0530643 uart = kzalloc(sizeof(struct omap_uart_state), GFP_KERNEL);
644 if (WARN_ON(!uart))
645 return;
Tony Lindgren1dbae812005-11-10 14:26:51 +0000646
Kevin Hilman6f251e92010-09-27 20:19:38 +0530647 uart->oh = oh;
648 uart->num = i++;
649 list_add_tail(&uart->node, &uart_list);
650 num_uarts++;
651
Tony Lindgren84f90c92009-10-16 09:53:00 -0700652 /*
Kevin Hilman6f251e92010-09-27 20:19:38 +0530653 * NOTE: omap_hwmod_init() has not yet been called,
654 * so no hwmod functions will work yet.
Tony Lindgren84f90c92009-10-16 09:53:00 -0700655 */
Tony Lindgren84f90c92009-10-16 09:53:00 -0700656
Kevin Hilman6f251e92010-09-27 20:19:38 +0530657 /*
658 * During UART early init, device need to be probed
659 * to determine SoC specific init before omap_device
660 * is ready. Therefore, don't allow idle here
661 */
662 uart->oh->flags |= HWMOD_INIT_NO_IDLE | HWMOD_INIT_NO_RESET;
663 } while (1);
Paul Walmsleyb3c6df32009-09-03 20:14:02 +0300664}
665
Mika Westerbergf62349e2009-12-11 16:16:35 -0800666/**
667 * omap_serial_init_port() - initialize single serial port
668 * @port: serial port number (0-3)
669 *
670 * This function initialies serial driver for given @port only.
671 * Platforms can call this function instead of omap_serial_init()
672 * if they don't plan to use all available UARTs as serial ports.
673 *
674 * Don't mix calls to omap_serial_init_port() and omap_serial_init(),
675 * use only one of the two.
676 */
677void __init omap_serial_init_port(int port)
678{
679 struct omap_uart_state *uart;
Kevin Hilman6f251e92010-09-27 20:19:38 +0530680 struct omap_hwmod *oh;
681 struct omap_device *od;
682 void *pdata = NULL;
683 u32 pdata_size = 0;
684 char *name;
685#ifndef CONFIG_SERIAL_OMAP
686 struct plat_serial8250_port ports[2] = {
687 {},
688 {.flags = 0},
689 };
690 struct plat_serial8250_port *p = &ports[0];
691#else
692 struct omap_uart_port_info omap_up;
693#endif
Mika Westerbergf62349e2009-12-11 16:16:35 -0800694
Kevin Hilman6f251e92010-09-27 20:19:38 +0530695 if (WARN_ON(port < 0))
Sergio Aguirree88d5562010-02-27 14:13:43 -0600696 return;
Kevin Hilman6f251e92010-09-27 20:19:38 +0530697 if (WARN_ON(port >= num_uarts))
Mika Westerbergf62349e2009-12-11 16:16:35 -0800698 return;
699
Kevin Hilman6f251e92010-09-27 20:19:38 +0530700 list_for_each_entry(uart, &uart_list, node)
701 if (port == uart->num)
702 break;
703
704 oh = uart->oh;
705 uart->dma_enabled = 0;
706#ifndef CONFIG_SERIAL_OMAP
707 name = "serial8250";
708
709 /*
710 * !! 8250 driver does not use standard IORESOURCE* It
711 * has it's own custom pdata that can be taken from
712 * the hwmod resource data. But, this needs to be
713 * done after the build.
714 *
715 * ?? does it have to be done before the register ??
716 * YES, because platform_device_data_add() copies
717 * pdata, it does not use a pointer.
718 */
719 p->flags = UPF_BOOT_AUTOCONF;
720 p->iotype = UPIO_MEM;
721 p->regshift = 2;
722 p->uartclk = OMAP24XX_BASE_BAUD * 16;
723 p->irq = oh->mpu_irqs[0].irq;
724 p->mapbase = oh->slaves[0]->addr->pa_start;
725 p->membase = omap_hwmod_get_mpu_rt_va(oh);
726 p->irqflags = IRQF_SHARED;
727 p->private_data = uart;
Mika Westerbergf62349e2009-12-11 16:16:35 -0800728
vikram pandita30e53bc2010-02-15 10:03:33 -0800729 /*
730 * omap44xx: Never read empty UART fifo
731 * omap3xxx: Never read empty UART fifo on UARTs
732 * with IP rev >=0x52
733 */
Kevin Hilman6f251e92010-09-27 20:19:38 +0530734 uart->regshift = p->regshift;
735 uart->membase = p->membase;
Nishanth Menon5a927b32010-08-02 13:18:12 +0300736 if (cpu_is_omap44xx())
737 uart->errata |= UART_ERRATA_FIFO_FULL_ABORT;
Kevin Hilman6f251e92010-09-27 20:19:38 +0530738 else if ((serial_read_reg(uart, UART_OMAP_MVER) & 0xFF)
Nishanth Menon5a927b32010-08-02 13:18:12 +0300739 >= UART_OMAP_NO_EMPTY_FIFO_READ_IP_REV)
740 uart->errata |= UART_ERRATA_FIFO_FULL_ABORT;
741
742 if (uart->errata & UART_ERRATA_FIFO_FULL_ABORT) {
Kevin Hilman6f251e92010-09-27 20:19:38 +0530743 p->serial_in = serial_in_override;
744 p->serial_out = serial_out_override;
745 }
746
747 pdata = &ports[0];
748 pdata_size = 2 * sizeof(struct plat_serial8250_port);
749#else
750
751 name = DRIVER_NAME;
752
753 omap_up.dma_enabled = uart->dma_enabled;
754 omap_up.uartclk = OMAP24XX_BASE_BAUD * 16;
755 omap_up.mapbase = oh->slaves[0]->addr->pa_start;
756 omap_up.membase = omap_hwmod_get_mpu_rt_va(oh);
757 omap_up.irqflags = IRQF_SHARED;
758 omap_up.flags = UPF_BOOT_AUTOCONF | UPF_SHARE_IRQ;
759
760 pdata = &omap_up;
761 pdata_size = sizeof(struct omap_uart_port_info);
762#endif
763
764 if (WARN_ON(!oh))
765 return;
766
767 od = omap_device_build(name, uart->num, oh, pdata, pdata_size,
768 omap_uart_latency,
769 ARRAY_SIZE(omap_uart_latency), false);
770 WARN(IS_ERR(od), "Could not build omap_device for %s: %s.\n",
771 name, oh->name);
772
773 uart->irq = oh->mpu_irqs[0].irq;
774 uart->regshift = 2;
775 uart->mapbase = oh->slaves[0]->addr->pa_start;
776 uart->membase = omap_hwmod_get_mpu_rt_va(oh);
777 uart->pdev = &od->pdev;
778
779 oh->dev_attr = uart;
780
781 /*
782 * Because of early UART probing, UART did not get idled
783 * on init. Now that omap_device is ready, ensure full idle
784 * before doing omap_device_enable().
785 */
786 omap_hwmod_idle(uart->oh);
787
788 omap_device_enable(uart->pdev);
789 omap_uart_idle_init(uart);
790 omap_uart_reset(uart);
791 omap_hwmod_enable_wakeup(uart->oh);
792 omap_device_idle(uart->pdev);
793
794 /*
795 * Need to block sleep long enough for interrupt driven
796 * driver to start. Console driver is in polling mode
797 * so device needs to be kept enabled while polling driver
798 * is in use.
799 */
800 if (uart->timeout)
801 uart->timeout = (30 * HZ);
802 omap_uart_block_sleep(uart);
803 uart->timeout = DEFAULT_TIMEOUT;
804
805 if ((cpu_is_omap34xx() && uart->padconf) ||
806 (uart->wk_en && uart->wk_mask)) {
807 device_init_wakeup(&od->pdev.dev, true);
808 DEV_CREATE_FILE(&od->pdev.dev, &dev_attr_sleep_timeout);
Santosh Shilimkare03d37d2010-02-18 08:59:06 +0000809 }
Deepak K00034502010-08-02 13:18:12 +0300810
811 /* Enable the MDR1 errata for OMAP3 */
812 if (cpu_is_omap34xx())
813 uart->errata |= UART_ERRATA_i202_MDR1_ACCESS;
Mika Westerbergf62349e2009-12-11 16:16:35 -0800814}
815
816/**
817 * omap_serial_init() - intialize all supported serial ports
818 *
819 * Initializes all available UARTs as serial ports. Platforms
820 * can call this function when they want to have default behaviour
821 * for serial ports (e.g initialize them all as serial ports).
822 */
Paul Walmsleyb3c6df32009-09-03 20:14:02 +0300823void __init omap_serial_init(void)
824{
Kevin Hilman6f251e92010-09-27 20:19:38 +0530825 struct omap_uart_state *uart;
Paul Walmsleyb3c6df32009-09-03 20:14:02 +0300826
Kevin Hilman6f251e92010-09-27 20:19:38 +0530827 list_for_each_entry(uart, &uart_list, node)
828 omap_serial_init_port(uart->num);
Tony Lindgren1dbae812005-11-10 14:26:51 +0000829}