blob: 1093a88a1fe3d9d9dd62410f8975c85ed8a3cde4 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * Driver for the PSC of the Freescale MPC52xx PSCs configured as UARTs.
3 *
4 * FIXME According to the usermanual the status bits in the status register
5 * are only updated when the peripherals access the FIFO and not when the
6 * CPU access them. So since we use this bits to know when we stop writing
7 * and reading, they may not be updated in-time and a race condition may
8 * exists. But I haven't be able to prove this and I don't care. But if
9 * any problem arises, it might worth checking. The TX/RX FIFO Stats
10 * registers should be used in addition.
11 * Update: Actually, they seem updated ... At least the bits we use.
12 *
13 *
14 * Maintainer : Sylvain Munaut <tnt@246tNt.com>
Grant Likely9b9129e2006-11-27 14:21:01 -070015 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070016 * Some of the code has been inspired/copied from the 2.4 code written
17 * by Dale Farnsworth <dfarnsworth@mvista.com>.
Grant Likely9b9129e2006-11-27 14:21:01 -070018 *
John Rigby25ae3a02008-01-29 04:28:56 +110019 * Copyright (C) 2008 Freescale Semiconductor Inc.
20 * John Rigby <jrigby@gmail.com>
21 * Added support for MPC5121
Grant Likelyb9272df2006-11-27 14:21:02 -070022 * Copyright (C) 2006 Secret Lab Technologies Ltd.
23 * Grant Likely <grant.likely@secretlab.ca>
24 * Copyright (C) 2004-2006 Sylvain Munaut <tnt@246tNt.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025 * Copyright (C) 2003 MontaVista, Software, Inc.
Grant Likely9b9129e2006-11-27 14:21:01 -070026 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070027 * This file is licensed under the terms of the GNU General Public License
28 * version 2. This program is licensed "as is" without any warranty of any
29 * kind, whether express or implied.
30 */
Grant Likely9b9129e2006-11-27 14:21:01 -070031
Grant Likelyb9272df2006-11-27 14:21:02 -070032#undef DEBUG
33
34#include <linux/device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <linux/module.h>
36#include <linux/tty.h>
Jiri Slabyee160a32011-09-01 16:20:57 +020037#include <linux/tty_flip.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <linux/serial.h>
39#include <linux/sysrq.h>
40#include <linux/console.h>
John Rigby406b7d42008-01-17 08:37:25 +110041#include <linux/delay.h>
42#include <linux/io.h>
Grant Likely283029d2008-01-09 06:20:40 +110043#include <linux/of.h>
44#include <linux/of_platform.h>
Anatolij Gustschin6acc6832010-02-16 22:30:03 -070045#include <linux/clk.h>
Grant Likelyb9272df2006-11-27 14:21:02 -070046
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include <asm/mpc52xx.h>
48#include <asm/mpc52xx_psc.h>
49
50#if defined(CONFIG_SERIAL_MPC52xx_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
51#define SUPPORT_SYSRQ
52#endif
53
54#include <linux/serial_core.h>
55
56
Sylvain Munautd62de3a2006-01-06 00:11:32 -080057/* We've been assigned a range on the "Low-density serial ports" major */
58#define SERIAL_PSC_MAJOR 204
59#define SERIAL_PSC_MINOR 148
60
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
62#define ISR_PASS_LIMIT 256 /* Max number of iteration in the interrupt */
63
64
65static struct uart_port mpc52xx_uart_ports[MPC52xx_PSC_MAXNUM];
66 /* Rem: - We use the read_status_mask as a shadow of
67 * psc->mpc52xx_psc_imr
68 * - It's important that is array is all zero on start as we
69 * use it to know if it's initialized or not ! If it's not sure
70 * it's cleared, then a memset(...,0,...) should be added to
71 * the console_init
72 */
Kumar Gala8d1fb8c2008-08-01 11:09:34 -050073
Grant Likelyb9272df2006-11-27 14:21:02 -070074/* lookup table for matching device nodes to index numbers */
75static struct device_node *mpc52xx_uart_nodes[MPC52xx_PSC_MAXNUM];
76
77static void mpc52xx_uart_of_enumerate(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
John Rigby599f0302008-01-29 04:28:55 +110079
Linus Torvalds1da177e2005-04-16 15:20:36 -070080#define PSC(port) ((struct mpc52xx_psc __iomem *)((port)->membase))
81
82
83/* Forward declaration of the interruption handling routine */
John Rigby406b7d42008-01-17 08:37:25 +110084static irqreturn_t mpc52xx_uart_int(int irq, void *dev_id);
Anatolij Gustschin6acc6832010-02-16 22:30:03 -070085static irqreturn_t mpc5xxx_uart_process_int(struct uart_port *port);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
87
88/* Simple macro to test if a port is console or not. This one is taken
89 * for serial_core.c and maybe should be moved to serial_core.h ? */
90#ifdef CONFIG_SERIAL_CORE_CONSOLE
John Rigby406b7d42008-01-17 08:37:25 +110091#define uart_console(port) \
92 ((port)->cons && (port)->cons->index == (port)->line)
Linus Torvalds1da177e2005-04-16 15:20:36 -070093#else
94#define uart_console(port) (0)
95#endif
96
John Rigby599f0302008-01-29 04:28:55 +110097/* ======================================================================== */
98/* PSC fifo operations for isolating differences between 52xx and 512x */
99/* ======================================================================== */
100
101struct psc_ops {
102 void (*fifo_init)(struct uart_port *port);
103 int (*raw_rx_rdy)(struct uart_port *port);
104 int (*raw_tx_rdy)(struct uart_port *port);
105 int (*rx_rdy)(struct uart_port *port);
106 int (*tx_rdy)(struct uart_port *port);
107 int (*tx_empty)(struct uart_port *port);
108 void (*stop_rx)(struct uart_port *port);
109 void (*start_tx)(struct uart_port *port);
110 void (*stop_tx)(struct uart_port *port);
111 void (*rx_clr_irq)(struct uart_port *port);
112 void (*tx_clr_irq)(struct uart_port *port);
113 void (*write_char)(struct uart_port *port, unsigned char c);
114 unsigned char (*read_char)(struct uart_port *port);
115 void (*cw_disable_ints)(struct uart_port *port);
116 void (*cw_restore_ints)(struct uart_port *port);
Albrecht Dreß0d1f22e2010-04-26 11:18:12 +0000117 unsigned int (*set_baudrate)(struct uart_port *port,
118 struct ktermios *new,
119 struct ktermios *old);
Anatolij Gustschin6acc6832010-02-16 22:30:03 -0700120 int (*clock)(struct uart_port *port, int enable);
121 int (*fifoc_init)(void);
122 void (*fifoc_uninit)(void);
123 void (*get_irq)(struct uart_port *, struct device_node *);
124 irqreturn_t (*handle_irq)(struct uart_port *port);
John Rigby599f0302008-01-29 04:28:55 +1100125};
126
Albrecht Dreß0d1f22e2010-04-26 11:18:12 +0000127/* setting the prescaler and divisor reg is common for all chips */
128static inline void mpc52xx_set_divisor(struct mpc52xx_psc __iomem *psc,
129 u16 prescaler, unsigned int divisor)
130{
131 /* select prescaler */
132 out_be16(&psc->mpc52xx_psc_clock_select, prescaler);
133 out_8(&psc->ctur, divisor >> 8);
134 out_8(&psc->ctlr, divisor & 0xff);
135}
136
John Rigby25ae3a02008-01-29 04:28:56 +1100137#ifdef CONFIG_PPC_MPC52xx
John Rigby599f0302008-01-29 04:28:55 +1100138#define FIFO_52xx(port) ((struct mpc52xx_psc_fifo __iomem *)(PSC(port)+1))
139static void mpc52xx_psc_fifo_init(struct uart_port *port)
140{
141 struct mpc52xx_psc __iomem *psc = PSC(port);
142 struct mpc52xx_psc_fifo __iomem *fifo = FIFO_52xx(port);
143
John Rigby599f0302008-01-29 04:28:55 +1100144 out_8(&fifo->rfcntl, 0x00);
145 out_be16(&fifo->rfalarm, 0x1ff);
146 out_8(&fifo->tfcntl, 0x07);
147 out_be16(&fifo->tfalarm, 0x80);
148
149 port->read_status_mask |= MPC52xx_PSC_IMR_RXRDY | MPC52xx_PSC_IMR_TXRDY;
150 out_be16(&psc->mpc52xx_psc_imr, port->read_status_mask);
151}
152
153static int mpc52xx_psc_raw_rx_rdy(struct uart_port *port)
154{
155 return in_be16(&PSC(port)->mpc52xx_psc_status)
156 & MPC52xx_PSC_SR_RXRDY;
157}
158
159static int mpc52xx_psc_raw_tx_rdy(struct uart_port *port)
160{
161 return in_be16(&PSC(port)->mpc52xx_psc_status)
162 & MPC52xx_PSC_SR_TXRDY;
163}
164
165
166static int mpc52xx_psc_rx_rdy(struct uart_port *port)
167{
168 return in_be16(&PSC(port)->mpc52xx_psc_isr)
169 & port->read_status_mask
170 & MPC52xx_PSC_IMR_RXRDY;
171}
172
173static int mpc52xx_psc_tx_rdy(struct uart_port *port)
174{
175 return in_be16(&PSC(port)->mpc52xx_psc_isr)
176 & port->read_status_mask
177 & MPC52xx_PSC_IMR_TXRDY;
178}
179
180static int mpc52xx_psc_tx_empty(struct uart_port *port)
181{
182 return in_be16(&PSC(port)->mpc52xx_psc_status)
183 & MPC52xx_PSC_SR_TXEMP;
184}
185
186static void mpc52xx_psc_start_tx(struct uart_port *port)
187{
188 port->read_status_mask |= MPC52xx_PSC_IMR_TXRDY;
189 out_be16(&PSC(port)->mpc52xx_psc_imr, port->read_status_mask);
190}
191
192static void mpc52xx_psc_stop_tx(struct uart_port *port)
193{
194 port->read_status_mask &= ~MPC52xx_PSC_IMR_TXRDY;
195 out_be16(&PSC(port)->mpc52xx_psc_imr, port->read_status_mask);
196}
197
198static void mpc52xx_psc_stop_rx(struct uart_port *port)
199{
200 port->read_status_mask &= ~MPC52xx_PSC_IMR_RXRDY;
201 out_be16(&PSC(port)->mpc52xx_psc_imr, port->read_status_mask);
202}
203
204static void mpc52xx_psc_rx_clr_irq(struct uart_port *port)
205{
206}
207
208static void mpc52xx_psc_tx_clr_irq(struct uart_port *port)
209{
210}
211
212static void mpc52xx_psc_write_char(struct uart_port *port, unsigned char c)
213{
214 out_8(&PSC(port)->mpc52xx_psc_buffer_8, c);
215}
216
217static unsigned char mpc52xx_psc_read_char(struct uart_port *port)
218{
219 return in_8(&PSC(port)->mpc52xx_psc_buffer_8);
220}
221
222static void mpc52xx_psc_cw_disable_ints(struct uart_port *port)
223{
224 out_be16(&PSC(port)->mpc52xx_psc_imr, 0);
225}
226
227static void mpc52xx_psc_cw_restore_ints(struct uart_port *port)
228{
229 out_be16(&PSC(port)->mpc52xx_psc_imr, port->read_status_mask);
230}
231
Albrecht Dreß0d1f22e2010-04-26 11:18:12 +0000232static unsigned int mpc5200_psc_set_baudrate(struct uart_port *port,
233 struct ktermios *new,
234 struct ktermios *old)
John Rigby599f0302008-01-29 04:28:55 +1100235{
Albrecht Dreß0d1f22e2010-04-26 11:18:12 +0000236 unsigned int baud;
237 unsigned int divisor;
238
239 /* The 5200 has a fixed /32 prescaler, uartclk contains the ipb freq */
240 baud = uart_get_baud_rate(port, new, old,
241 port->uartclk / (32 * 0xffff) + 1,
242 port->uartclk / 32);
243 divisor = (port->uartclk + 16 * baud) / (32 * baud);
244
245 /* enable the /32 prescaler and set the divisor */
246 mpc52xx_set_divisor(PSC(port), 0xdd00, divisor);
247 return baud;
248}
249
250static unsigned int mpc5200b_psc_set_baudrate(struct uart_port *port,
251 struct ktermios *new,
252 struct ktermios *old)
253{
254 unsigned int baud;
255 unsigned int divisor;
256 u16 prescaler;
257
258 /* The 5200B has a selectable /4 or /32 prescaler, uartclk contains the
259 * ipb freq */
260 baud = uart_get_baud_rate(port, new, old,
261 port->uartclk / (32 * 0xffff) + 1,
262 port->uartclk / 4);
263 divisor = (port->uartclk + 2 * baud) / (4 * baud);
264
265 /* select the proper prescaler and set the divisor */
266 if (divisor > 0xffff) {
267 divisor = (divisor + 4) / 8;
268 prescaler = 0xdd00; /* /32 */
269 } else
270 prescaler = 0xff00; /* /4 */
271 mpc52xx_set_divisor(PSC(port), prescaler, divisor);
272 return baud;
John Rigby599f0302008-01-29 04:28:55 +1100273}
274
Anatolij Gustschin6acc6832010-02-16 22:30:03 -0700275static void mpc52xx_psc_get_irq(struct uart_port *port, struct device_node *np)
276{
Yong Zhang9cfb5c02011-09-22 16:59:15 +0800277 port->irqflags = 0;
Anatolij Gustschin6acc6832010-02-16 22:30:03 -0700278 port->irq = irq_of_parse_and_map(np, 0);
279}
280
281/* 52xx specific interrupt handler. The caller holds the port lock */
282static irqreturn_t mpc52xx_psc_handle_irq(struct uart_port *port)
283{
284 return mpc5xxx_uart_process_int(port);
285}
286
John Rigby599f0302008-01-29 04:28:55 +1100287static struct psc_ops mpc52xx_psc_ops = {
288 .fifo_init = mpc52xx_psc_fifo_init,
289 .raw_rx_rdy = mpc52xx_psc_raw_rx_rdy,
290 .raw_tx_rdy = mpc52xx_psc_raw_tx_rdy,
291 .rx_rdy = mpc52xx_psc_rx_rdy,
292 .tx_rdy = mpc52xx_psc_tx_rdy,
293 .tx_empty = mpc52xx_psc_tx_empty,
294 .stop_rx = mpc52xx_psc_stop_rx,
295 .start_tx = mpc52xx_psc_start_tx,
296 .stop_tx = mpc52xx_psc_stop_tx,
297 .rx_clr_irq = mpc52xx_psc_rx_clr_irq,
298 .tx_clr_irq = mpc52xx_psc_tx_clr_irq,
299 .write_char = mpc52xx_psc_write_char,
300 .read_char = mpc52xx_psc_read_char,
301 .cw_disable_ints = mpc52xx_psc_cw_disable_ints,
302 .cw_restore_ints = mpc52xx_psc_cw_restore_ints,
Albrecht Dreß0d1f22e2010-04-26 11:18:12 +0000303 .set_baudrate = mpc5200_psc_set_baudrate,
304 .get_irq = mpc52xx_psc_get_irq,
305 .handle_irq = mpc52xx_psc_handle_irq,
306};
307
308static struct psc_ops mpc5200b_psc_ops = {
309 .fifo_init = mpc52xx_psc_fifo_init,
310 .raw_rx_rdy = mpc52xx_psc_raw_rx_rdy,
311 .raw_tx_rdy = mpc52xx_psc_raw_tx_rdy,
312 .rx_rdy = mpc52xx_psc_rx_rdy,
313 .tx_rdy = mpc52xx_psc_tx_rdy,
314 .tx_empty = mpc52xx_psc_tx_empty,
315 .stop_rx = mpc52xx_psc_stop_rx,
316 .start_tx = mpc52xx_psc_start_tx,
317 .stop_tx = mpc52xx_psc_stop_tx,
318 .rx_clr_irq = mpc52xx_psc_rx_clr_irq,
319 .tx_clr_irq = mpc52xx_psc_tx_clr_irq,
320 .write_char = mpc52xx_psc_write_char,
321 .read_char = mpc52xx_psc_read_char,
322 .cw_disable_ints = mpc52xx_psc_cw_disable_ints,
323 .cw_restore_ints = mpc52xx_psc_cw_restore_ints,
324 .set_baudrate = mpc5200b_psc_set_baudrate,
Anatolij Gustschin6acc6832010-02-16 22:30:03 -0700325 .get_irq = mpc52xx_psc_get_irq,
326 .handle_irq = mpc52xx_psc_handle_irq,
John Rigby599f0302008-01-29 04:28:55 +1100327};
328
John Rigby25ae3a02008-01-29 04:28:56 +1100329#endif /* CONFIG_MPC52xx */
330
331#ifdef CONFIG_PPC_MPC512x
332#define FIFO_512x(port) ((struct mpc512x_psc_fifo __iomem *)(PSC(port)+1))
Anatolij Gustschin6acc6832010-02-16 22:30:03 -0700333
334/* PSC FIFO Controller for mpc512x */
335struct psc_fifoc {
336 u32 fifoc_cmd;
337 u32 fifoc_int;
338 u32 fifoc_dma;
339 u32 fifoc_axe;
340 u32 fifoc_debug;
341};
342
343static struct psc_fifoc __iomem *psc_fifoc;
344static unsigned int psc_fifoc_irq;
345
John Rigby25ae3a02008-01-29 04:28:56 +1100346static void mpc512x_psc_fifo_init(struct uart_port *port)
347{
Anatolij Gustschin6acc6832010-02-16 22:30:03 -0700348 /* /32 prescaler */
349 out_be16(&PSC(port)->mpc52xx_psc_clock_select, 0xdd00);
350
John Rigby25ae3a02008-01-29 04:28:56 +1100351 out_be32(&FIFO_512x(port)->txcmd, MPC512x_PSC_FIFO_RESET_SLICE);
352 out_be32(&FIFO_512x(port)->txcmd, MPC512x_PSC_FIFO_ENABLE_SLICE);
353 out_be32(&FIFO_512x(port)->txalarm, 1);
354 out_be32(&FIFO_512x(port)->tximr, 0);
355
356 out_be32(&FIFO_512x(port)->rxcmd, MPC512x_PSC_FIFO_RESET_SLICE);
357 out_be32(&FIFO_512x(port)->rxcmd, MPC512x_PSC_FIFO_ENABLE_SLICE);
358 out_be32(&FIFO_512x(port)->rxalarm, 1);
359 out_be32(&FIFO_512x(port)->rximr, 0);
360
361 out_be32(&FIFO_512x(port)->tximr, MPC512x_PSC_FIFO_ALARM);
362 out_be32(&FIFO_512x(port)->rximr, MPC512x_PSC_FIFO_ALARM);
363}
364
365static int mpc512x_psc_raw_rx_rdy(struct uart_port *port)
366{
367 return !(in_be32(&FIFO_512x(port)->rxsr) & MPC512x_PSC_FIFO_EMPTY);
368}
369
370static int mpc512x_psc_raw_tx_rdy(struct uart_port *port)
371{
372 return !(in_be32(&FIFO_512x(port)->txsr) & MPC512x_PSC_FIFO_FULL);
373}
374
375static int mpc512x_psc_rx_rdy(struct uart_port *port)
376{
377 return in_be32(&FIFO_512x(port)->rxsr)
378 & in_be32(&FIFO_512x(port)->rximr)
379 & MPC512x_PSC_FIFO_ALARM;
380}
381
382static int mpc512x_psc_tx_rdy(struct uart_port *port)
383{
384 return in_be32(&FIFO_512x(port)->txsr)
385 & in_be32(&FIFO_512x(port)->tximr)
386 & MPC512x_PSC_FIFO_ALARM;
387}
388
389static int mpc512x_psc_tx_empty(struct uart_port *port)
390{
391 return in_be32(&FIFO_512x(port)->txsr)
392 & MPC512x_PSC_FIFO_EMPTY;
393}
394
395static void mpc512x_psc_stop_rx(struct uart_port *port)
396{
397 unsigned long rx_fifo_imr;
398
399 rx_fifo_imr = in_be32(&FIFO_512x(port)->rximr);
400 rx_fifo_imr &= ~MPC512x_PSC_FIFO_ALARM;
401 out_be32(&FIFO_512x(port)->rximr, rx_fifo_imr);
402}
403
404static void mpc512x_psc_start_tx(struct uart_port *port)
405{
406 unsigned long tx_fifo_imr;
407
408 tx_fifo_imr = in_be32(&FIFO_512x(port)->tximr);
409 tx_fifo_imr |= MPC512x_PSC_FIFO_ALARM;
410 out_be32(&FIFO_512x(port)->tximr, tx_fifo_imr);
411}
412
413static void mpc512x_psc_stop_tx(struct uart_port *port)
414{
415 unsigned long tx_fifo_imr;
416
417 tx_fifo_imr = in_be32(&FIFO_512x(port)->tximr);
418 tx_fifo_imr &= ~MPC512x_PSC_FIFO_ALARM;
419 out_be32(&FIFO_512x(port)->tximr, tx_fifo_imr);
420}
421
422static void mpc512x_psc_rx_clr_irq(struct uart_port *port)
423{
424 out_be32(&FIFO_512x(port)->rxisr, in_be32(&FIFO_512x(port)->rxisr));
425}
426
427static void mpc512x_psc_tx_clr_irq(struct uart_port *port)
428{
429 out_be32(&FIFO_512x(port)->txisr, in_be32(&FIFO_512x(port)->txisr));
430}
431
432static void mpc512x_psc_write_char(struct uart_port *port, unsigned char c)
433{
434 out_8(&FIFO_512x(port)->txdata_8, c);
435}
436
437static unsigned char mpc512x_psc_read_char(struct uart_port *port)
438{
439 return in_8(&FIFO_512x(port)->rxdata_8);
440}
441
442static void mpc512x_psc_cw_disable_ints(struct uart_port *port)
443{
444 port->read_status_mask =
445 in_be32(&FIFO_512x(port)->tximr) << 16 |
446 in_be32(&FIFO_512x(port)->rximr);
447 out_be32(&FIFO_512x(port)->tximr, 0);
448 out_be32(&FIFO_512x(port)->rximr, 0);
449}
450
451static void mpc512x_psc_cw_restore_ints(struct uart_port *port)
452{
453 out_be32(&FIFO_512x(port)->tximr,
454 (port->read_status_mask >> 16) & 0x7f);
455 out_be32(&FIFO_512x(port)->rximr, port->read_status_mask & 0x7f);
456}
457
Albrecht Dreß0d1f22e2010-04-26 11:18:12 +0000458static unsigned int mpc512x_psc_set_baudrate(struct uart_port *port,
459 struct ktermios *new,
460 struct ktermios *old)
John Rigby25ae3a02008-01-29 04:28:56 +1100461{
Albrecht Dreß0d1f22e2010-04-26 11:18:12 +0000462 unsigned int baud;
463 unsigned int divisor;
464
465 /*
466 * The "MPC5121e Microcontroller Reference Manual, Rev. 3" says on
467 * pg. 30-10 that the chip supports a /32 and a /10 prescaler.
468 * Furthermore, it states that "After reset, the prescaler by 10
469 * for the UART mode is selected", but the reset register value is
470 * 0x0000 which means a /32 prescaler. This is wrong.
471 *
472 * In reality using /32 prescaler doesn't work, as it is not supported!
473 * Use /16 or /10 prescaler, see "MPC5121e Hardware Design Guide",
474 * Chapter 4.1 PSC in UART Mode.
475 * Calculate with a /16 prescaler here.
476 */
477
478 /* uartclk contains the ips freq */
479 baud = uart_get_baud_rate(port, new, old,
480 port->uartclk / (16 * 0xffff) + 1,
481 port->uartclk / 16);
482 divisor = (port->uartclk + 8 * baud) / (16 * baud);
483
484 /* enable the /16 prescaler and set the divisor */
485 mpc52xx_set_divisor(PSC(port), 0xdd00, divisor);
486 return baud;
John Rigby25ae3a02008-01-29 04:28:56 +1100487}
488
Anatolij Gustschin6acc6832010-02-16 22:30:03 -0700489/* Init PSC FIFO Controller */
490static int __init mpc512x_psc_fifoc_init(void)
491{
492 struct device_node *np;
Anatolij Gustschin6acc6832010-02-16 22:30:03 -0700493
494 np = of_find_compatible_node(NULL, NULL,
495 "fsl,mpc5121-psc-fifo");
496 if (!np) {
497 pr_err("%s: Can't find FIFOC node\n", __func__);
498 return -ENODEV;
499 }
500
501 psc_fifoc = of_iomap(np, 0);
502 if (!psc_fifoc) {
503 pr_err("%s: Can't map FIFOC\n", __func__);
Julia Lawall05f25ab2010-08-29 11:52:41 +0200504 of_node_put(np);
Anatolij Gustschin6acc6832010-02-16 22:30:03 -0700505 return -ENODEV;
506 }
507
508 psc_fifoc_irq = irq_of_parse_and_map(np, 0);
509 of_node_put(np);
510 if (psc_fifoc_irq == NO_IRQ) {
511 pr_err("%s: Can't get FIFOC irq\n", __func__);
512 iounmap(psc_fifoc);
513 return -ENODEV;
514 }
515
Anatolij Gustschin6acc6832010-02-16 22:30:03 -0700516 return 0;
517}
518
519static void __exit mpc512x_psc_fifoc_uninit(void)
520{
521 iounmap(psc_fifoc);
522}
523
524/* 512x specific interrupt handler. The caller holds the port lock */
525static irqreturn_t mpc512x_psc_handle_irq(struct uart_port *port)
526{
527 unsigned long fifoc_int;
528 int psc_num;
529
530 /* Read pending PSC FIFOC interrupts */
531 fifoc_int = in_be32(&psc_fifoc->fifoc_int);
532
533 /* Check if it is an interrupt for this port */
534 psc_num = (port->mapbase & 0xf00) >> 8;
535 if (test_bit(psc_num, &fifoc_int) ||
536 test_bit(psc_num + 16, &fifoc_int))
537 return mpc5xxx_uart_process_int(port);
538
539 return IRQ_NONE;
540}
541
542static int mpc512x_psc_clock(struct uart_port *port, int enable)
543{
544 struct clk *psc_clk;
545 int psc_num;
546 char clk_name[10];
547
548 if (uart_console(port))
549 return 0;
550
551 psc_num = (port->mapbase & 0xf00) >> 8;
552 snprintf(clk_name, sizeof(clk_name), "psc%d_clk", psc_num);
553 psc_clk = clk_get(port->dev, clk_name);
554 if (IS_ERR(psc_clk)) {
555 dev_err(port->dev, "Failed to get PSC clock entry!\n");
556 return -ENODEV;
557 }
558
559 dev_dbg(port->dev, "%s %sable\n", clk_name, enable ? "en" : "dis");
560
561 if (enable)
562 clk_enable(psc_clk);
563 else
564 clk_disable(psc_clk);
565
566 return 0;
567}
568
569static void mpc512x_psc_get_irq(struct uart_port *port, struct device_node *np)
570{
571 port->irqflags = IRQF_SHARED;
572 port->irq = psc_fifoc_irq;
573}
574
John Rigby25ae3a02008-01-29 04:28:56 +1100575static struct psc_ops mpc512x_psc_ops = {
576 .fifo_init = mpc512x_psc_fifo_init,
577 .raw_rx_rdy = mpc512x_psc_raw_rx_rdy,
578 .raw_tx_rdy = mpc512x_psc_raw_tx_rdy,
579 .rx_rdy = mpc512x_psc_rx_rdy,
580 .tx_rdy = mpc512x_psc_tx_rdy,
581 .tx_empty = mpc512x_psc_tx_empty,
582 .stop_rx = mpc512x_psc_stop_rx,
583 .start_tx = mpc512x_psc_start_tx,
584 .stop_tx = mpc512x_psc_stop_tx,
585 .rx_clr_irq = mpc512x_psc_rx_clr_irq,
586 .tx_clr_irq = mpc512x_psc_tx_clr_irq,
587 .write_char = mpc512x_psc_write_char,
588 .read_char = mpc512x_psc_read_char,
589 .cw_disable_ints = mpc512x_psc_cw_disable_ints,
590 .cw_restore_ints = mpc512x_psc_cw_restore_ints,
Albrecht Dreß0d1f22e2010-04-26 11:18:12 +0000591 .set_baudrate = mpc512x_psc_set_baudrate,
Anatolij Gustschin6acc6832010-02-16 22:30:03 -0700592 .clock = mpc512x_psc_clock,
593 .fifoc_init = mpc512x_psc_fifoc_init,
594 .fifoc_uninit = mpc512x_psc_fifoc_uninit,
595 .get_irq = mpc512x_psc_get_irq,
596 .handle_irq = mpc512x_psc_handle_irq,
John Rigby25ae3a02008-01-29 04:28:56 +1100597};
598#endif
599
600static struct psc_ops *psc_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601
602/* ======================================================================== */
603/* UART operations */
604/* ======================================================================== */
605
Grant Likely9b9129e2006-11-27 14:21:01 -0700606static unsigned int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607mpc52xx_uart_tx_empty(struct uart_port *port)
608{
John Rigby599f0302008-01-29 04:28:55 +1100609 return psc_ops->tx_empty(port) ? TIOCSER_TEMT : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610}
611
Grant Likely9b9129e2006-11-27 14:21:01 -0700612static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613mpc52xx_uart_set_mctrl(struct uart_port *port, unsigned int mctrl)
614{
Wolfram Sangaec739e2008-12-21 02:54:32 -0700615 if (mctrl & TIOCM_RTS)
616 out_8(&PSC(port)->op1, MPC52xx_PSC_OP_RTS);
617 else
618 out_8(&PSC(port)->op0, MPC52xx_PSC_OP_RTS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619}
620
Grant Likely9b9129e2006-11-27 14:21:01 -0700621static unsigned int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622mpc52xx_uart_get_mctrl(struct uart_port *port)
623{
Wolfram Sangaec739e2008-12-21 02:54:32 -0700624 unsigned int ret = TIOCM_DSR;
625 u8 status = in_8(&PSC(port)->mpc52xx_psc_ipcr);
626
627 if (!(status & MPC52xx_PSC_CTS))
628 ret |= TIOCM_CTS;
629 if (!(status & MPC52xx_PSC_DCD))
630 ret |= TIOCM_CAR;
631
632 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633}
634
Grant Likely9b9129e2006-11-27 14:21:01 -0700635static void
Russell Kingb129a8c2005-08-31 10:12:14 +0100636mpc52xx_uart_stop_tx(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637{
638 /* port->lock taken by caller */
John Rigby599f0302008-01-29 04:28:55 +1100639 psc_ops->stop_tx(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640}
641
Grant Likely9b9129e2006-11-27 14:21:01 -0700642static void
Russell Kingb129a8c2005-08-31 10:12:14 +0100643mpc52xx_uart_start_tx(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644{
645 /* port->lock taken by caller */
John Rigby599f0302008-01-29 04:28:55 +1100646 psc_ops->start_tx(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647}
648
Grant Likely9b9129e2006-11-27 14:21:01 -0700649static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650mpc52xx_uart_send_xchar(struct uart_port *port, char ch)
651{
652 unsigned long flags;
653 spin_lock_irqsave(&port->lock, flags);
Grant Likely9b9129e2006-11-27 14:21:01 -0700654
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 port->x_char = ch;
656 if (ch) {
657 /* Make sure tx interrupts are on */
658 /* Truly necessary ??? They should be anyway */
John Rigby599f0302008-01-29 04:28:55 +1100659 psc_ops->start_tx(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 }
Grant Likely9b9129e2006-11-27 14:21:01 -0700661
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 spin_unlock_irqrestore(&port->lock, flags);
663}
664
665static void
666mpc52xx_uart_stop_rx(struct uart_port *port)
667{
668 /* port->lock taken by caller */
John Rigby599f0302008-01-29 04:28:55 +1100669 psc_ops->stop_rx(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670}
671
672static void
673mpc52xx_uart_enable_ms(struct uart_port *port)
674{
Wolfram Sangaec739e2008-12-21 02:54:32 -0700675 struct mpc52xx_psc __iomem *psc = PSC(port);
676
677 /* clear D_*-bits by reading them */
678 in_8(&psc->mpc52xx_psc_ipcr);
679 /* enable CTS and DCD as IPC interrupts */
680 out_8(&psc->mpc52xx_psc_acr, MPC52xx_PSC_IEC_CTS | MPC52xx_PSC_IEC_DCD);
681
682 port->read_status_mask |= MPC52xx_PSC_IMR_IPC;
683 out_be16(&psc->mpc52xx_psc_imr, port->read_status_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684}
685
686static void
687mpc52xx_uart_break_ctl(struct uart_port *port, int ctl)
688{
689 unsigned long flags;
690 spin_lock_irqsave(&port->lock, flags);
691
John Rigby406b7d42008-01-17 08:37:25 +1100692 if (ctl == -1)
693 out_8(&PSC(port)->command, MPC52xx_PSC_START_BRK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 else
John Rigby406b7d42008-01-17 08:37:25 +1100695 out_8(&PSC(port)->command, MPC52xx_PSC_STOP_BRK);
Grant Likely9b9129e2006-11-27 14:21:01 -0700696
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 spin_unlock_irqrestore(&port->lock, flags);
698}
699
700static int
701mpc52xx_uart_startup(struct uart_port *port)
702{
703 struct mpc52xx_psc __iomem *psc = PSC(port);
704 int ret;
705
Anatolij Gustschin6acc6832010-02-16 22:30:03 -0700706 if (psc_ops->clock) {
707 ret = psc_ops->clock(port, 1);
708 if (ret)
709 return ret;
710 }
711
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 /* Request IRQ */
713 ret = request_irq(port->irq, mpc52xx_uart_int,
Anatolij Gustschin6acc6832010-02-16 22:30:03 -0700714 port->irqflags, "mpc52xx_psc_uart", port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 if (ret)
716 return ret;
717
718 /* Reset/activate the port, clear and enable interrupts */
John Rigby406b7d42008-01-17 08:37:25 +1100719 out_8(&psc->command, MPC52xx_PSC_RST_RX);
720 out_8(&psc->command, MPC52xx_PSC_RST_TX);
Grant Likely9b9129e2006-11-27 14:21:01 -0700721
John Rigby406b7d42008-01-17 08:37:25 +1100722 out_be32(&psc->sicr, 0); /* UART mode DCD ignored */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723
John Rigby599f0302008-01-29 04:28:55 +1100724 psc_ops->fifo_init(port);
Grant Likely9b9129e2006-11-27 14:21:01 -0700725
John Rigby406b7d42008-01-17 08:37:25 +1100726 out_8(&psc->command, MPC52xx_PSC_TX_ENABLE);
727 out_8(&psc->command, MPC52xx_PSC_RX_ENABLE);
Grant Likely9b9129e2006-11-27 14:21:01 -0700728
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 return 0;
730}
731
732static void
733mpc52xx_uart_shutdown(struct uart_port *port)
734{
735 struct mpc52xx_psc __iomem *psc = PSC(port);
Grant Likely9b9129e2006-11-27 14:21:01 -0700736
Grant Likelya3481192007-05-07 01:38:51 +1000737 /* Shut down the port. Leave TX active if on a console port */
John Rigby406b7d42008-01-17 08:37:25 +1100738 out_8(&psc->command, MPC52xx_PSC_RST_RX);
Grant Likelya3481192007-05-07 01:38:51 +1000739 if (!uart_console(port))
John Rigby406b7d42008-01-17 08:37:25 +1100740 out_8(&psc->command, MPC52xx_PSC_RST_TX);
Grant Likely9b9129e2006-11-27 14:21:01 -0700741
742 port->read_status_mask = 0;
John Rigby406b7d42008-01-17 08:37:25 +1100743 out_be16(&psc->mpc52xx_psc_imr, port->read_status_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744
Anatolij Gustschin6acc6832010-02-16 22:30:03 -0700745 if (psc_ops->clock)
746 psc_ops->clock(port, 0);
747
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 /* Release interrupt */
749 free_irq(port->irq, port);
750}
751
Grant Likely9b9129e2006-11-27 14:21:01 -0700752static void
Alan Cox606d0992006-12-08 02:38:45 -0800753mpc52xx_uart_set_termios(struct uart_port *port, struct ktermios *new,
John Rigby406b7d42008-01-17 08:37:25 +1100754 struct ktermios *old)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755{
756 struct mpc52xx_psc __iomem *psc = PSC(port);
757 unsigned long flags;
758 unsigned char mr1, mr2;
Albrecht Dreß0d1f22e2010-04-26 11:18:12 +0000759 unsigned int j;
760 unsigned int baud;
Grant Likely9b9129e2006-11-27 14:21:01 -0700761
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 /* Prepare what we're gonna write */
763 mr1 = 0;
Grant Likely9b9129e2006-11-27 14:21:01 -0700764
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 switch (new->c_cflag & CSIZE) {
John Rigby406b7d42008-01-17 08:37:25 +1100766 case CS5: mr1 |= MPC52xx_PSC_MODE_5_BITS;
767 break;
768 case CS6: mr1 |= MPC52xx_PSC_MODE_6_BITS;
769 break;
770 case CS7: mr1 |= MPC52xx_PSC_MODE_7_BITS;
771 break;
772 case CS8:
773 default: mr1 |= MPC52xx_PSC_MODE_8_BITS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 }
775
776 if (new->c_cflag & PARENB) {
777 mr1 |= (new->c_cflag & PARODD) ?
778 MPC52xx_PSC_MODE_PARODD : MPC52xx_PSC_MODE_PAREVEN;
779 } else
780 mr1 |= MPC52xx_PSC_MODE_PARNONE;
Grant Likely9b9129e2006-11-27 14:21:01 -0700781
782
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 mr2 = 0;
784
785 if (new->c_cflag & CSTOPB)
786 mr2 |= MPC52xx_PSC_MODE_TWO_STOP;
787 else
788 mr2 |= ((new->c_cflag & CSIZE) == CS5) ?
789 MPC52xx_PSC_MODE_ONE_STOP_5_BITS :
790 MPC52xx_PSC_MODE_ONE_STOP;
791
Wolfram Sangaec739e2008-12-21 02:54:32 -0700792 if (new->c_cflag & CRTSCTS) {
793 mr1 |= MPC52xx_PSC_MODE_RXRTS;
794 mr2 |= MPC52xx_PSC_MODE_TXCTS;
795 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 /* Get the lock */
798 spin_lock_irqsave(&port->lock, flags);
799
Nick Andrewc4f01242008-12-05 16:34:46 +0000800 /* Do our best to flush TX & RX, so we don't lose anything */
801 /* But we don't wait indefinitely ! */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 j = 5000000; /* Maximum wait */
803 /* FIXME Can't receive chars since set_termios might be called at early
804 * boot for the console, all stuff is not yet ready to receive at that
805 * time and that just makes the kernel oops */
806 /* while (j-- && mpc52xx_uart_int_rx_chars(port)); */
John Rigby599f0302008-01-29 04:28:55 +1100807 while (!mpc52xx_uart_tx_empty(port) && --j)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 udelay(1);
809
810 if (!j)
John Rigby406b7d42008-01-17 08:37:25 +1100811 printk(KERN_ERR "mpc52xx_uart.c: "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 "Unable to flush RX & TX fifos in-time in set_termios."
John Rigby406b7d42008-01-17 08:37:25 +1100813 "Some chars may have been lost.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814
815 /* Reset the TX & RX */
John Rigby406b7d42008-01-17 08:37:25 +1100816 out_8(&psc->command, MPC52xx_PSC_RST_RX);
817 out_8(&psc->command, MPC52xx_PSC_RST_TX);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818
819 /* Send new mode settings */
John Rigby406b7d42008-01-17 08:37:25 +1100820 out_8(&psc->command, MPC52xx_PSC_SEL_MODE_REG_1);
821 out_8(&psc->mode, mr1);
822 out_8(&psc->mode, mr2);
Albrecht Dreß0d1f22e2010-04-26 11:18:12 +0000823 baud = psc_ops->set_baudrate(port, new, old);
824
825 /* Update the per-port timeout */
826 uart_update_timeout(port, new->c_cflag, baud);
Grant Likely9b9129e2006-11-27 14:21:01 -0700827
Wolfram Sangaec739e2008-12-21 02:54:32 -0700828 if (UART_ENABLE_MS(port, new->c_cflag))
829 mpc52xx_uart_enable_ms(port);
830
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 /* Reenable TX & RX */
John Rigby406b7d42008-01-17 08:37:25 +1100832 out_8(&psc->command, MPC52xx_PSC_TX_ENABLE);
833 out_8(&psc->command, MPC52xx_PSC_RX_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834
835 /* We're all set, release the lock */
836 spin_unlock_irqrestore(&port->lock, flags);
837}
838
839static const char *
840mpc52xx_uart_type(struct uart_port *port)
841{
Wolfram Sange44dcb62010-11-12 19:47:47 +0100842 /*
843 * We keep using PORT_MPC52xx for historic reasons although it applies
844 * for MPC512x, too, but print "MPC5xxx" to not irritate users
845 */
846 return port->type == PORT_MPC52xx ? "MPC5xxx PSC" : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847}
848
849static void
850mpc52xx_uart_release_port(struct uart_port *port)
851{
John Rigby406b7d42008-01-17 08:37:25 +1100852 /* remapped by us ? */
853 if (port->flags & UPF_IOREMAP) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 iounmap(port->membase);
855 port->membase = NULL;
856 }
857
Grant Likelyb9272df2006-11-27 14:21:02 -0700858 release_mem_region(port->mapbase, sizeof(struct mpc52xx_psc));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859}
860
861static int
862mpc52xx_uart_request_port(struct uart_port *port)
863{
Amol Ladbe618f52006-09-30 23:29:23 -0700864 int err;
865
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 if (port->flags & UPF_IOREMAP) /* Need to remap ? */
Grant Likelyb9272df2006-11-27 14:21:02 -0700867 port->membase = ioremap(port->mapbase,
John Rigby406b7d42008-01-17 08:37:25 +1100868 sizeof(struct mpc52xx_psc));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869
870 if (!port->membase)
871 return -EINVAL;
872
Grant Likelyb9272df2006-11-27 14:21:02 -0700873 err = request_mem_region(port->mapbase, sizeof(struct mpc52xx_psc),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 "mpc52xx_psc_uart") != NULL ? 0 : -EBUSY;
Amol Ladbe618f52006-09-30 23:29:23 -0700875
876 if (err && (port->flags & UPF_IOREMAP)) {
877 iounmap(port->membase);
878 port->membase = NULL;
879 }
880
881 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882}
883
884static void
885mpc52xx_uart_config_port(struct uart_port *port, int flags)
886{
John Rigby406b7d42008-01-17 08:37:25 +1100887 if ((flags & UART_CONFIG_TYPE)
888 && (mpc52xx_uart_request_port(port) == 0))
889 port->type = PORT_MPC52xx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890}
891
892static int
893mpc52xx_uart_verify_port(struct uart_port *port, struct serial_struct *ser)
894{
John Rigby406b7d42008-01-17 08:37:25 +1100895 if (ser->type != PORT_UNKNOWN && ser->type != PORT_MPC52xx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 return -EINVAL;
897
John Rigby406b7d42008-01-17 08:37:25 +1100898 if ((ser->irq != port->irq) ||
Julia Lawallb7a82122009-10-15 09:58:28 -0600899 (ser->io_type != UPIO_MEM) ||
John Rigby406b7d42008-01-17 08:37:25 +1100900 (ser->baud_base != port->uartclk) ||
901 (ser->iomem_base != (void *)port->mapbase) ||
902 (ser->hub6 != 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 return -EINVAL;
904
905 return 0;
906}
907
908
909static struct uart_ops mpc52xx_uart_ops = {
910 .tx_empty = mpc52xx_uart_tx_empty,
911 .set_mctrl = mpc52xx_uart_set_mctrl,
912 .get_mctrl = mpc52xx_uart_get_mctrl,
913 .stop_tx = mpc52xx_uart_stop_tx,
914 .start_tx = mpc52xx_uart_start_tx,
915 .send_xchar = mpc52xx_uart_send_xchar,
916 .stop_rx = mpc52xx_uart_stop_rx,
917 .enable_ms = mpc52xx_uart_enable_ms,
918 .break_ctl = mpc52xx_uart_break_ctl,
919 .startup = mpc52xx_uart_startup,
920 .shutdown = mpc52xx_uart_shutdown,
921 .set_termios = mpc52xx_uart_set_termios,
922/* .pm = mpc52xx_uart_pm, Not supported yet */
923/* .set_wake = mpc52xx_uart_set_wake, Not supported yet */
924 .type = mpc52xx_uart_type,
925 .release_port = mpc52xx_uart_release_port,
926 .request_port = mpc52xx_uart_request_port,
927 .config_port = mpc52xx_uart_config_port,
928 .verify_port = mpc52xx_uart_verify_port
929};
930
Grant Likely9b9129e2006-11-27 14:21:01 -0700931
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932/* ======================================================================== */
933/* Interrupt handling */
934/* ======================================================================== */
Grant Likely9b9129e2006-11-27 14:21:01 -0700935
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936static inline int
David Howells7d12e782006-10-05 14:55:46 +0100937mpc52xx_uart_int_rx_chars(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938{
Alan Coxebd2c8f2009-09-19 13:13:28 -0700939 struct tty_struct *tty = port->state->port.tty;
Alan Cox33f0f882006-01-09 20:54:13 -0800940 unsigned char ch, flag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 unsigned short status;
942
943 /* While we can read, do so ! */
John Rigby599f0302008-01-29 04:28:55 +1100944 while (psc_ops->raw_rx_rdy(port)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 /* Get the char */
John Rigby599f0302008-01-29 04:28:55 +1100946 ch = psc_ops->read_char(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947
948 /* Handle sysreq char */
949#ifdef SUPPORT_SYSRQ
David Howells7d12e782006-10-05 14:55:46 +0100950 if (uart_handle_sysrq_char(port, ch)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 port->sysrq = 0;
952 continue;
953 }
954#endif
955
956 /* Store it */
Alan Cox33f0f882006-01-09 20:54:13 -0800957
958 flag = TTY_NORMAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 port->icount.rx++;
Grant Likely9b9129e2006-11-27 14:21:01 -0700960
John Rigby599f0302008-01-29 04:28:55 +1100961 status = in_be16(&PSC(port)->mpc52xx_psc_status);
962
John Rigby406b7d42008-01-17 08:37:25 +1100963 if (status & (MPC52xx_PSC_SR_PE |
964 MPC52xx_PSC_SR_FE |
965 MPC52xx_PSC_SR_RB)) {
Grant Likely9b9129e2006-11-27 14:21:01 -0700966
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 if (status & MPC52xx_PSC_SR_RB) {
Alan Cox33f0f882006-01-09 20:54:13 -0800968 flag = TTY_BREAK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 uart_handle_break(port);
René Bürgelb6514982008-12-21 02:54:31 -0700970 port->icount.brk++;
971 } else if (status & MPC52xx_PSC_SR_PE) {
Alan Cox33f0f882006-01-09 20:54:13 -0800972 flag = TTY_PARITY;
René Bürgelb6514982008-12-21 02:54:31 -0700973 port->icount.parity++;
974 }
975 else if (status & MPC52xx_PSC_SR_FE) {
Alan Cox33f0f882006-01-09 20:54:13 -0800976 flag = TTY_FRAME;
René Bürgelb6514982008-12-21 02:54:31 -0700977 port->icount.frame++;
978 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979
980 /* Clear error condition */
John Rigby406b7d42008-01-17 08:37:25 +1100981 out_8(&PSC(port)->command, MPC52xx_PSC_RST_ERR_STAT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982
983 }
Alan Cox33f0f882006-01-09 20:54:13 -0800984 tty_insert_flip_char(tty, ch, flag);
985 if (status & MPC52xx_PSC_SR_OE) {
986 /*
987 * Overrun is special, since it's
988 * reported immediately, and doesn't
989 * affect the current character
990 */
991 tty_insert_flip_char(tty, 0, TTY_OVERRUN);
René Bürgelb6514982008-12-21 02:54:31 -0700992 port->icount.overrun++;
Alan Cox33f0f882006-01-09 20:54:13 -0800993 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 }
995
Andrew Liufbe543b2008-04-29 17:36:25 +1000996 spin_unlock(&port->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 tty_flip_buffer_push(tty);
Andrew Liufbe543b2008-04-29 17:36:25 +1000998 spin_lock(&port->lock);
Grant Likely9b9129e2006-11-27 14:21:01 -0700999
John Rigby599f0302008-01-29 04:28:55 +11001000 return psc_ops->raw_rx_rdy(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001}
1002
1003static inline int
1004mpc52xx_uart_int_tx_chars(struct uart_port *port)
1005{
Alan Coxebd2c8f2009-09-19 13:13:28 -07001006 struct circ_buf *xmit = &port->state->xmit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007
1008 /* Process out of band chars */
1009 if (port->x_char) {
John Rigby599f0302008-01-29 04:28:55 +11001010 psc_ops->write_char(port, port->x_char);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 port->icount.tx++;
1012 port->x_char = 0;
1013 return 1;
1014 }
1015
1016 /* Nothing to do ? */
1017 if (uart_circ_empty(xmit) || uart_tx_stopped(port)) {
Russell Kingb129a8c2005-08-31 10:12:14 +01001018 mpc52xx_uart_stop_tx(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 return 0;
1020 }
1021
1022 /* Send chars */
John Rigby599f0302008-01-29 04:28:55 +11001023 while (psc_ops->raw_tx_rdy(port)) {
1024 psc_ops->write_char(port, xmit->buf[xmit->tail]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
1026 port->icount.tx++;
1027 if (uart_circ_empty(xmit))
1028 break;
1029 }
1030
1031 /* Wake up */
1032 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
1033 uart_write_wakeup(port);
1034
1035 /* Maybe we're done after all */
1036 if (uart_circ_empty(xmit)) {
Russell Kingb129a8c2005-08-31 10:12:14 +01001037 mpc52xx_uart_stop_tx(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 return 0;
1039 }
1040
1041 return 1;
1042}
1043
Grant Likely9b9129e2006-11-27 14:21:01 -07001044static irqreturn_t
Anatolij Gustschin6acc6832010-02-16 22:30:03 -07001045mpc5xxx_uart_process_int(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 unsigned long pass = ISR_PASS_LIMIT;
1048 unsigned int keepgoing;
Wolfram Sangaec739e2008-12-21 02:54:32 -07001049 u8 status;
Grant Likely9b9129e2006-11-27 14:21:01 -07001050
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 /* While we have stuff to do, we continue */
1052 do {
1053 /* If we don't find anything to do, we stop */
Grant Likely9b9129e2006-11-27 14:21:01 -07001054 keepgoing = 0;
1055
John Rigby599f0302008-01-29 04:28:55 +11001056 psc_ops->rx_clr_irq(port);
1057 if (psc_ops->rx_rdy(port))
David Howells7d12e782006-10-05 14:55:46 +01001058 keepgoing |= mpc52xx_uart_int_rx_chars(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059
John Rigby599f0302008-01-29 04:28:55 +11001060 psc_ops->tx_clr_irq(port);
1061 if (psc_ops->tx_rdy(port))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 keepgoing |= mpc52xx_uart_int_tx_chars(port);
Grant Likely9b9129e2006-11-27 14:21:01 -07001063
Wolfram Sangaec739e2008-12-21 02:54:32 -07001064 status = in_8(&PSC(port)->mpc52xx_psc_ipcr);
1065 if (status & MPC52xx_PSC_D_DCD)
1066 uart_handle_dcd_change(port, !(status & MPC52xx_PSC_DCD));
1067
1068 if (status & MPC52xx_PSC_D_CTS)
1069 uart_handle_cts_change(port, !(status & MPC52xx_PSC_CTS));
1070
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 /* Limit number of iteration */
John Rigby406b7d42008-01-17 08:37:25 +11001072 if (!(--pass))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 keepgoing = 0;
1074
1075 } while (keepgoing);
Grant Likely9b9129e2006-11-27 14:21:01 -07001076
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 return IRQ_HANDLED;
1078}
1079
Anatolij Gustschin6acc6832010-02-16 22:30:03 -07001080static irqreturn_t
1081mpc52xx_uart_int(int irq, void *dev_id)
1082{
1083 struct uart_port *port = dev_id;
1084 irqreturn_t ret;
1085
1086 spin_lock(&port->lock);
1087
1088 ret = psc_ops->handle_irq(port);
1089
1090 spin_unlock(&port->lock);
1091
1092 return ret;
1093}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094
1095/* ======================================================================== */
1096/* Console ( if applicable ) */
1097/* ======================================================================== */
1098
1099#ifdef CONFIG_SERIAL_MPC52xx_CONSOLE
1100
1101static void __init
1102mpc52xx_console_get_options(struct uart_port *port,
John Rigby406b7d42008-01-17 08:37:25 +11001103 int *baud, int *parity, int *bits, int *flow)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104{
1105 struct mpc52xx_psc __iomem *psc = PSC(port);
1106 unsigned char mr1;
1107
Grant Likelyb9272df2006-11-27 14:21:02 -07001108 pr_debug("mpc52xx_console_get_options(port=%p)\n", port);
1109
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 /* Read the mode registers */
John Rigby406b7d42008-01-17 08:37:25 +11001111 out_8(&psc->command, MPC52xx_PSC_SEL_MODE_REG_1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 mr1 = in_8(&psc->mode);
Grant Likely9b9129e2006-11-27 14:21:01 -07001113
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 /* CT{U,L}R are write-only ! */
Grant Likelyb9272df2006-11-27 14:21:02 -07001115 *baud = CONFIG_SERIAL_MPC52xx_CONSOLE_BAUD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116
1117 /* Parse them */
1118 switch (mr1 & MPC52xx_PSC_MODE_BITS_MASK) {
John Rigby406b7d42008-01-17 08:37:25 +11001119 case MPC52xx_PSC_MODE_5_BITS:
1120 *bits = 5;
1121 break;
1122 case MPC52xx_PSC_MODE_6_BITS:
1123 *bits = 6;
1124 break;
1125 case MPC52xx_PSC_MODE_7_BITS:
1126 *bits = 7;
1127 break;
1128 case MPC52xx_PSC_MODE_8_BITS:
1129 default:
1130 *bits = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 }
Grant Likely9b9129e2006-11-27 14:21:01 -07001132
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 if (mr1 & MPC52xx_PSC_MODE_PARNONE)
1134 *parity = 'n';
1135 else
1136 *parity = mr1 & MPC52xx_PSC_MODE_PARODD ? 'o' : 'e';
1137}
1138
Grant Likely9b9129e2006-11-27 14:21:01 -07001139static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140mpc52xx_console_write(struct console *co, const char *s, unsigned int count)
1141{
1142 struct uart_port *port = &mpc52xx_uart_ports[co->index];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 unsigned int i, j;
Grant Likely9b9129e2006-11-27 14:21:01 -07001144
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 /* Disable interrupts */
John Rigby599f0302008-01-29 04:28:55 +11001146 psc_ops->cw_disable_ints(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147
1148 /* Wait the TX buffer to be empty */
Grant Likely9b9129e2006-11-27 14:21:01 -07001149 j = 5000000; /* Maximum wait */
John Rigby599f0302008-01-29 04:28:55 +11001150 while (!mpc52xx_uart_tx_empty(port) && --j)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 udelay(1);
1152
1153 /* Write all the chars */
Russell Kingd3587882006-03-20 20:00:09 +00001154 for (i = 0; i < count; i++, s++) {
1155 /* Line return handling */
1156 if (*s == '\n')
John Rigby599f0302008-01-29 04:28:55 +11001157 psc_ops->write_char(port, '\r');
Grant Likely9b9129e2006-11-27 14:21:01 -07001158
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 /* Send the char */
John Rigby599f0302008-01-29 04:28:55 +11001160 psc_ops->write_char(port, *s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 /* Wait the TX buffer to be empty */
Grant Likely9b9129e2006-11-27 14:21:01 -07001163 j = 20000; /* Maximum wait */
John Rigby599f0302008-01-29 04:28:55 +11001164 while (!mpc52xx_uart_tx_empty(port) && --j)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 udelay(1);
1166 }
1167
1168 /* Restore interrupt state */
John Rigby599f0302008-01-29 04:28:55 +11001169 psc_ops->cw_restore_ints(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170}
1171
Grant Likelyb9272df2006-11-27 14:21:02 -07001172
1173static int __init
1174mpc52xx_console_setup(struct console *co, char *options)
1175{
1176 struct uart_port *port = &mpc52xx_uart_ports[co->index];
1177 struct device_node *np = mpc52xx_uart_nodes[co->index];
John Rigby599f0302008-01-29 04:28:55 +11001178 unsigned int uartclk;
Grant Likelyb9272df2006-11-27 14:21:02 -07001179 struct resource res;
1180 int ret;
1181
1182 int baud = CONFIG_SERIAL_MPC52xx_CONSOLE_BAUD;
1183 int bits = 8;
1184 int parity = 'n';
1185 int flow = 'n';
1186
1187 pr_debug("mpc52xx_console_setup co=%p, co->index=%i, options=%s\n",
1188 co, co->index, options);
1189
Roel Kluinb898f4f2009-05-28 14:34:29 -07001190 if ((co->index < 0) || (co->index >= MPC52xx_PSC_MAXNUM)) {
Grant Likelyb9272df2006-11-27 14:21:02 -07001191 pr_debug("PSC%x out of range\n", co->index);
1192 return -EINVAL;
1193 }
1194
1195 if (!np) {
1196 pr_debug("PSC%x not found in device tree\n", co->index);
1197 return -EINVAL;
1198 }
1199
1200 pr_debug("Console on ttyPSC%x is %s\n",
John Rigby406b7d42008-01-17 08:37:25 +11001201 co->index, mpc52xx_uart_nodes[co->index]->full_name);
Grant Likelyb9272df2006-11-27 14:21:02 -07001202
1203 /* Fetch register locations */
John Rigby406b7d42008-01-17 08:37:25 +11001204 ret = of_address_to_resource(np, 0, &res);
1205 if (ret) {
Grant Likelyb9272df2006-11-27 14:21:02 -07001206 pr_debug("Could not get resources for PSC%x\n", co->index);
1207 return ret;
1208 }
1209
Albrecht Dreß0d1f22e2010-04-26 11:18:12 +00001210 uartclk = mpc5xxx_get_bus_frequency(np);
John Rigby599f0302008-01-29 04:28:55 +11001211 if (uartclk == 0) {
1212 pr_debug("Could not find uart clock frequency!\n");
Grant Likelyb9272df2006-11-27 14:21:02 -07001213 return -EINVAL;
1214 }
1215
1216 /* Basic port init. Needed since we use some uart_??? func before
1217 * real init for early access */
1218 spin_lock_init(&port->lock);
John Rigby599f0302008-01-29 04:28:55 +11001219 port->uartclk = uartclk;
Grant Likelyb9272df2006-11-27 14:21:02 -07001220 port->ops = &mpc52xx_uart_ops;
1221 port->mapbase = res.start;
1222 port->membase = ioremap(res.start, sizeof(struct mpc52xx_psc));
1223 port->irq = irq_of_parse_and_map(np, 0);
1224
1225 if (port->membase == NULL)
1226 return -EINVAL;
1227
Grant Likely5dd80d52007-10-13 22:37:02 -06001228 pr_debug("mpc52xx-psc uart at %p, mapped to %p, irq=%x, freq=%i\n",
John Rigby406b7d42008-01-17 08:37:25 +11001229 (void *)port->mapbase, port->membase,
1230 port->irq, port->uartclk);
Grant Likelyb9272df2006-11-27 14:21:02 -07001231
1232 /* Setup the port parameters accoding to options */
1233 if (options)
1234 uart_parse_options(options, &baud, &parity, &bits, &flow);
1235 else
1236 mpc52xx_console_get_options(port, &baud, &parity, &bits, &flow);
1237
1238 pr_debug("Setting console parameters: %i %i%c1 flow=%c\n",
John Rigby406b7d42008-01-17 08:37:25 +11001239 baud, bits, parity, flow);
Grant Likelyb9272df2006-11-27 14:21:02 -07001240
1241 return uart_set_options(port, co, baud, parity, bits, flow);
1242}
Grant Likelyb9272df2006-11-27 14:21:02 -07001243
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244
Sylvain Munaut2d8179c2006-01-06 00:11:31 -08001245static struct uart_driver mpc52xx_uart_driver;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246
1247static struct console mpc52xx_console = {
Sylvain Munautd62de3a2006-01-06 00:11:32 -08001248 .name = "ttyPSC",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 .write = mpc52xx_console_write,
1250 .device = uart_console_device,
1251 .setup = mpc52xx_console_setup,
1252 .flags = CON_PRINTBUFFER,
John Rigby406b7d42008-01-17 08:37:25 +11001253 .index = -1, /* Specified on the cmdline (e.g. console=ttyPSC0) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 .data = &mpc52xx_uart_driver,
1255};
1256
Grant Likely9b9129e2006-11-27 14:21:01 -07001257
1258static int __init
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259mpc52xx_console_init(void)
1260{
Grant Likelyb9272df2006-11-27 14:21:02 -07001261 mpc52xx_uart_of_enumerate();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 register_console(&mpc52xx_console);
1263 return 0;
1264}
1265
1266console_initcall(mpc52xx_console_init);
1267
1268#define MPC52xx_PSC_CONSOLE &mpc52xx_console
1269#else
1270#define MPC52xx_PSC_CONSOLE NULL
1271#endif
1272
1273
1274/* ======================================================================== */
1275/* UART Driver */
1276/* ======================================================================== */
1277
1278static struct uart_driver mpc52xx_uart_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 .driver_name = "mpc52xx_psc_uart",
Sylvain Munautd62de3a2006-01-06 00:11:32 -08001280 .dev_name = "ttyPSC",
Sylvain Munautd62de3a2006-01-06 00:11:32 -08001281 .major = SERIAL_PSC_MAJOR,
1282 .minor = SERIAL_PSC_MINOR,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 .nr = MPC52xx_PSC_MAXNUM,
1284 .cons = MPC52xx_PSC_CONSOLE,
1285};
1286
Grant Likelyb9272df2006-11-27 14:21:02 -07001287/* ======================================================================== */
1288/* OF Platform Driver */
1289/* ======================================================================== */
1290
Grant Likely52b80482008-02-06 22:29:25 -07001291static struct of_device_id mpc52xx_uart_of_match[] = {
1292#ifdef CONFIG_PPC_MPC52xx
Albrecht Dreß0d1f22e2010-04-26 11:18:12 +00001293 { .compatible = "fsl,mpc5200b-psc-uart", .data = &mpc5200b_psc_ops, },
Grant Likely52b80482008-02-06 22:29:25 -07001294 { .compatible = "fsl,mpc5200-psc-uart", .data = &mpc52xx_psc_ops, },
1295 /* binding used by old lite5200 device trees: */
1296 { .compatible = "mpc5200-psc-uart", .data = &mpc52xx_psc_ops, },
1297 /* binding used by efika: */
1298 { .compatible = "mpc5200-serial", .data = &mpc52xx_psc_ops, },
1299#endif
1300#ifdef CONFIG_PPC_MPC512x
1301 { .compatible = "fsl,mpc5121-psc-uart", .data = &mpc512x_psc_ops, },
Grant Likely52b80482008-02-06 22:29:25 -07001302#endif
Grant Likelybc775ea2008-04-29 06:40:37 -06001303 {},
Grant Likely52b80482008-02-06 22:29:25 -07001304};
1305
Grant Likely793218d2011-02-22 21:10:26 -07001306static int __devinit mpc52xx_uart_of_probe(struct platform_device *op)
Grant Likelyb9272df2006-11-27 14:21:02 -07001307{
1308 int idx = -1;
John Rigby599f0302008-01-29 04:28:55 +11001309 unsigned int uartclk;
Grant Likelyb9272df2006-11-27 14:21:02 -07001310 struct uart_port *port = NULL;
1311 struct resource res;
1312 int ret;
1313
Grant Likelyb9272df2006-11-27 14:21:02 -07001314 /* Check validity & presence */
1315 for (idx = 0; idx < MPC52xx_PSC_MAXNUM; idx++)
Grant Likely61c7a082010-04-13 16:12:29 -07001316 if (mpc52xx_uart_nodes[idx] == op->dev.of_node)
Grant Likelyb9272df2006-11-27 14:21:02 -07001317 break;
1318 if (idx >= MPC52xx_PSC_MAXNUM)
1319 return -EINVAL;
1320 pr_debug("Found %s assigned to ttyPSC%x\n",
John Rigby406b7d42008-01-17 08:37:25 +11001321 mpc52xx_uart_nodes[idx]->full_name, idx);
Grant Likelyb9272df2006-11-27 14:21:02 -07001322
Albrecht Dreß0d1f22e2010-04-26 11:18:12 +00001323 /* set the uart clock to the input clock of the psc, the different
1324 * prescalers are taken into account in the set_baudrate() methods
1325 * of the respective chip */
1326 uartclk = mpc5xxx_get_bus_frequency(op->dev.of_node);
John Rigby599f0302008-01-29 04:28:55 +11001327 if (uartclk == 0) {
1328 dev_dbg(&op->dev, "Could not find uart clock frequency!\n");
Grant Likelyb9272df2006-11-27 14:21:02 -07001329 return -EINVAL;
1330 }
1331
1332 /* Init the port structure */
1333 port = &mpc52xx_uart_ports[idx];
1334
1335 spin_lock_init(&port->lock);
John Rigby599f0302008-01-29 04:28:55 +11001336 port->uartclk = uartclk;
Grant Likelyb9272df2006-11-27 14:21:02 -07001337 port->fifosize = 512;
1338 port->iotype = UPIO_MEM;
1339 port->flags = UPF_BOOT_AUTOCONF |
John Rigby406b7d42008-01-17 08:37:25 +11001340 (uart_console(port) ? 0 : UPF_IOREMAP);
Grant Likelyb9272df2006-11-27 14:21:02 -07001341 port->line = idx;
1342 port->ops = &mpc52xx_uart_ops;
1343 port->dev = &op->dev;
1344
1345 /* Search for IRQ and mapbase */
Grant Likely61c7a082010-04-13 16:12:29 -07001346 ret = of_address_to_resource(op->dev.of_node, 0, &res);
John Rigby406b7d42008-01-17 08:37:25 +11001347 if (ret)
Grant Likelyb9272df2006-11-27 14:21:02 -07001348 return ret;
1349
1350 port->mapbase = res.start;
Wolfram Sang418441d92008-12-21 02:54:32 -07001351 if (!port->mapbase) {
1352 dev_dbg(&op->dev, "Could not allocate resources for PSC\n");
1353 return -EINVAL;
1354 }
1355
Grant Likely61c7a082010-04-13 16:12:29 -07001356 psc_ops->get_irq(port, op->dev.of_node);
Wolfram Sang418441d92008-12-21 02:54:32 -07001357 if (port->irq == NO_IRQ) {
1358 dev_dbg(&op->dev, "Could not get irq\n");
1359 return -EINVAL;
1360 }
Grant Likelyb9272df2006-11-27 14:21:02 -07001361
Grant Likely5dd80d52007-10-13 22:37:02 -06001362 dev_dbg(&op->dev, "mpc52xx-psc uart at %p, irq=%x, freq=%i\n",
John Rigby406b7d42008-01-17 08:37:25 +11001363 (void *)port->mapbase, port->irq, port->uartclk);
Grant Likelyb9272df2006-11-27 14:21:02 -07001364
Grant Likelyb9272df2006-11-27 14:21:02 -07001365 /* Add the port to the uart sub-system */
1366 ret = uart_add_one_port(&mpc52xx_uart_driver, port);
Anatolij Gustschin6acc6832010-02-16 22:30:03 -07001367 if (ret)
Wolfram Sang418441d92008-12-21 02:54:32 -07001368 return ret;
Grant Likelyb9272df2006-11-27 14:21:02 -07001369
Wolfram Sang418441d92008-12-21 02:54:32 -07001370 dev_set_drvdata(&op->dev, (void *)port);
1371 return 0;
Grant Likelyb9272df2006-11-27 14:21:02 -07001372}
1373
1374static int
Grant Likely2dc11582010-08-06 09:25:50 -06001375mpc52xx_uart_of_remove(struct platform_device *op)
Grant Likelyb9272df2006-11-27 14:21:02 -07001376{
1377 struct uart_port *port = dev_get_drvdata(&op->dev);
1378 dev_set_drvdata(&op->dev, NULL);
1379
Anatolij Gustschin6acc6832010-02-16 22:30:03 -07001380 if (port)
Grant Likelyb9272df2006-11-27 14:21:02 -07001381 uart_remove_one_port(&mpc52xx_uart_driver, port);
1382
1383 return 0;
1384}
1385
1386#ifdef CONFIG_PM
1387static int
Grant Likely2dc11582010-08-06 09:25:50 -06001388mpc52xx_uart_of_suspend(struct platform_device *op, pm_message_t state)
Grant Likelyb9272df2006-11-27 14:21:02 -07001389{
1390 struct uart_port *port = (struct uart_port *) dev_get_drvdata(&op->dev);
1391
1392 if (port)
1393 uart_suspend_port(&mpc52xx_uart_driver, port);
1394
1395 return 0;
1396}
1397
1398static int
Grant Likely2dc11582010-08-06 09:25:50 -06001399mpc52xx_uart_of_resume(struct platform_device *op)
Grant Likelyb9272df2006-11-27 14:21:02 -07001400{
1401 struct uart_port *port = (struct uart_port *) dev_get_drvdata(&op->dev);
1402
1403 if (port)
1404 uart_resume_port(&mpc52xx_uart_driver, port);
1405
1406 return 0;
1407}
1408#endif
1409
1410static void
Grant Likely3b5ebf82009-02-03 12:30:25 -07001411mpc52xx_uart_of_assign(struct device_node *np)
Grant Likelyb9272df2006-11-27 14:21:02 -07001412{
Grant Likelyb9272df2006-11-27 14:21:02 -07001413 int i;
1414
Grant Likely3b5ebf82009-02-03 12:30:25 -07001415 /* Find the first free PSC number */
Grant Likelyb9272df2006-11-27 14:21:02 -07001416 for (i = 0; i < MPC52xx_PSC_MAXNUM; i++) {
1417 if (mpc52xx_uart_nodes[i] == NULL) {
Grant Likely3b5ebf82009-02-03 12:30:25 -07001418 of_node_get(np);
1419 mpc52xx_uart_nodes[i] = np;
1420 return;
Grant Likelyb9272df2006-11-27 14:21:02 -07001421 }
1422 }
Grant Likelyb9272df2006-11-27 14:21:02 -07001423}
1424
1425static void
1426mpc52xx_uart_of_enumerate(void)
1427{
John Rigby406b7d42008-01-17 08:37:25 +11001428 static int enum_done;
Grant Likelyb9272df2006-11-27 14:21:02 -07001429 struct device_node *np;
John Rigby25ae3a02008-01-29 04:28:56 +11001430 const struct of_device_id *match;
Grant Likelyb9272df2006-11-27 14:21:02 -07001431 int i;
1432
1433 if (enum_done)
1434 return;
1435
Grant Likely3b5ebf82009-02-03 12:30:25 -07001436 /* Assign index to each PSC in device tree */
1437 for_each_matching_node(np, mpc52xx_uart_of_match) {
John Rigby25ae3a02008-01-29 04:28:56 +11001438 match = of_match_node(mpc52xx_uart_of_match, np);
John Rigby25ae3a02008-01-29 04:28:56 +11001439 psc_ops = match->data;
Grant Likely3b5ebf82009-02-03 12:30:25 -07001440 mpc52xx_uart_of_assign(np);
Grant Likelyb9272df2006-11-27 14:21:02 -07001441 }
1442
1443 enum_done = 1;
1444
1445 for (i = 0; i < MPC52xx_PSC_MAXNUM; i++) {
1446 if (mpc52xx_uart_nodes[i])
1447 pr_debug("%s assigned to ttyPSC%x\n",
John Rigby406b7d42008-01-17 08:37:25 +11001448 mpc52xx_uart_nodes[i]->full_name, i);
Grant Likelyb9272df2006-11-27 14:21:02 -07001449 }
1450}
1451
1452MODULE_DEVICE_TABLE(of, mpc52xx_uart_of_match);
1453
Grant Likely793218d2011-02-22 21:10:26 -07001454static struct platform_driver mpc52xx_uart_of_driver = {
Grant Likelyb9272df2006-11-27 14:21:02 -07001455 .probe = mpc52xx_uart_of_probe,
1456 .remove = mpc52xx_uart_of_remove,
1457#ifdef CONFIG_PM
1458 .suspend = mpc52xx_uart_of_suspend,
1459 .resume = mpc52xx_uart_of_resume,
1460#endif
Grant Likely40182942010-04-13 16:13:02 -07001461 .driver = {
1462 .name = "mpc52xx-psc-uart",
1463 .owner = THIS_MODULE,
1464 .of_match_table = mpc52xx_uart_of_match,
Grant Likelyb9272df2006-11-27 14:21:02 -07001465 },
1466};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467
1468
1469/* ======================================================================== */
1470/* Module */
1471/* ======================================================================== */
1472
1473static int __init
1474mpc52xx_uart_init(void)
1475{
1476 int ret;
1477
Grant Likelyb9272df2006-11-27 14:21:02 -07001478 printk(KERN_INFO "Serial: MPC52xx PSC UART driver\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479
John Rigby406b7d42008-01-17 08:37:25 +11001480 ret = uart_register_driver(&mpc52xx_uart_driver);
1481 if (ret) {
Grant Likelyb9272df2006-11-27 14:21:02 -07001482 printk(KERN_ERR "%s: uart_register_driver failed (%i)\n",
1483 __FILE__, ret);
1484 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 }
1486
Grant Likelyb9272df2006-11-27 14:21:02 -07001487 mpc52xx_uart_of_enumerate();
1488
Anatolij Gustschin6acc6832010-02-16 22:30:03 -07001489 /*
1490 * Map the PSC FIFO Controller and init if on MPC512x.
1491 */
Anatolij Gustschine6114fa2010-05-05 00:18:59 +02001492 if (psc_ops && psc_ops->fifoc_init) {
Anatolij Gustschin6acc6832010-02-16 22:30:03 -07001493 ret = psc_ops->fifoc_init();
1494 if (ret)
1495 return ret;
1496 }
1497
Grant Likely793218d2011-02-22 21:10:26 -07001498 ret = platform_driver_register(&mpc52xx_uart_of_driver);
Grant Likelyb9272df2006-11-27 14:21:02 -07001499 if (ret) {
Grant Likely793218d2011-02-22 21:10:26 -07001500 printk(KERN_ERR "%s: platform_driver_register failed (%i)\n",
Grant Likelyb9272df2006-11-27 14:21:02 -07001501 __FILE__, ret);
1502 uart_unregister_driver(&mpc52xx_uart_driver);
1503 return ret;
1504 }
Grant Likelyb9272df2006-11-27 14:21:02 -07001505
1506 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507}
1508
1509static void __exit
1510mpc52xx_uart_exit(void)
1511{
Anatolij Gustschin6acc6832010-02-16 22:30:03 -07001512 if (psc_ops->fifoc_uninit)
1513 psc_ops->fifoc_uninit();
1514
Grant Likely793218d2011-02-22 21:10:26 -07001515 platform_driver_unregister(&mpc52xx_uart_of_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 uart_unregister_driver(&mpc52xx_uart_driver);
1517}
1518
1519
1520module_init(mpc52xx_uart_init);
1521module_exit(mpc52xx_uart_exit);
1522
1523MODULE_AUTHOR("Sylvain Munaut <tnt@246tNt.com>");
1524MODULE_DESCRIPTION("Freescale MPC52xx PSC UART");
1525MODULE_LICENSE("GPL");