xref: /linux/drivers/tty/serial/samsung_tty.c (revision 9f90a4ddef4e4d3aa4229f6b117d4e57231457b3)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Driver core for Samsung SoC onboard UARTs.
4  *
5  * Ben Dooks, Copyright (c) 2003-2008 Simtec Electronics
6  *	http://armlinux.simtec.co.uk/
7  */
8 
9 /* Note on 2410 error handling
10  *
11  * The s3c2410 manual has a love/hate affair with the contents of the
12  * UERSTAT register in the UART blocks, and keeps marking some of the
13  * error bits as reserved. Having checked with the s3c2410x01,
14  * it copes with BREAKs properly, so I am happy to ignore the RESERVED
15  * feature from the latter versions of the manual.
16  *
17  * If it becomes aparrent that latter versions of the 2410 remove these
18  * bits, then action will have to be taken to differentiate the versions
19  * and change the policy on BREAK
20  *
21  * BJD, 04-Nov-2004
22  */
23 
24 #include <linux/dmaengine.h>
25 #include <linux/dma-mapping.h>
26 #include <linux/slab.h>
27 #include <linux/module.h>
28 #include <linux/ioport.h>
29 #include <linux/io.h>
30 #include <linux/platform_device.h>
31 #include <linux/init.h>
32 #include <linux/sysrq.h>
33 #include <linux/console.h>
34 #include <linux/tty.h>
35 #include <linux/tty_flip.h>
36 #include <linux/serial_core.h>
37 #include <linux/serial.h>
38 #include <linux/serial_s3c.h>
39 #include <linux/delay.h>
40 #include <linux/clk.h>
41 #include <linux/cpufreq.h>
42 #include <linux/of.h>
43 #include <asm/irq.h>
44 
45 /* UART name and device definitions */
46 
47 #define S3C24XX_SERIAL_NAME	"ttySAC"
48 #define S3C24XX_SERIAL_MAJOR	204
49 #define S3C24XX_SERIAL_MINOR	64
50 
51 #define S3C24XX_TX_PIO			1
52 #define S3C24XX_TX_DMA			2
53 #define S3C24XX_RX_PIO			1
54 #define S3C24XX_RX_DMA			2
55 
56 /* flag to ignore all characters coming in */
57 #define RXSTAT_DUMMY_READ (0x10000000)
58 
59 enum s3c24xx_port_type {
60 	TYPE_S3C24XX,
61 	TYPE_S3C6400,
62 	TYPE_APPLE_S5L,
63 };
64 
65 struct s3c24xx_uart_info {
66 	char			*name;
67 	enum s3c24xx_port_type	type;
68 	unsigned int		port_type;
69 	unsigned int		fifosize;
70 	unsigned long		rx_fifomask;
71 	unsigned long		rx_fifoshift;
72 	unsigned long		rx_fifofull;
73 	unsigned long		tx_fifomask;
74 	unsigned long		tx_fifoshift;
75 	unsigned long		tx_fifofull;
76 	unsigned int		def_clk_sel;
77 	unsigned long		num_clks;
78 	unsigned long		clksel_mask;
79 	unsigned long		clksel_shift;
80 	unsigned long		ucon_mask;
81 
82 	/* uart port features */
83 
84 	unsigned int		has_divslot:1;
85 };
86 
87 struct s3c24xx_serial_drv_data {
88 	struct s3c24xx_uart_info	*info;
89 	struct s3c2410_uartcfg		*def_cfg;
90 	unsigned int			fifosize[CONFIG_SERIAL_SAMSUNG_UARTS];
91 };
92 
93 struct s3c24xx_uart_dma {
94 	unsigned int			rx_chan_id;
95 	unsigned int			tx_chan_id;
96 
97 	struct dma_slave_config		rx_conf;
98 	struct dma_slave_config		tx_conf;
99 
100 	struct dma_chan			*rx_chan;
101 	struct dma_chan			*tx_chan;
102 
103 	dma_addr_t			rx_addr;
104 	dma_addr_t			tx_addr;
105 
106 	dma_cookie_t			rx_cookie;
107 	dma_cookie_t			tx_cookie;
108 
109 	char				*rx_buf;
110 
111 	dma_addr_t			tx_transfer_addr;
112 
113 	size_t				rx_size;
114 	size_t				tx_size;
115 
116 	struct dma_async_tx_descriptor	*tx_desc;
117 	struct dma_async_tx_descriptor	*rx_desc;
118 
119 	int				tx_bytes_requested;
120 	int				rx_bytes_requested;
121 };
122 
123 struct s3c24xx_uart_port {
124 	unsigned char			rx_claimed;
125 	unsigned char			tx_claimed;
126 	unsigned char			rx_enabled;
127 	unsigned char			tx_enabled;
128 	unsigned int			pm_level;
129 	unsigned long			baudclk_rate;
130 	unsigned int			min_dma_size;
131 
132 	unsigned int			rx_irq;
133 	unsigned int			tx_irq;
134 
135 	unsigned int			tx_in_progress;
136 	unsigned int			tx_mode;
137 	unsigned int			rx_mode;
138 
139 	struct s3c24xx_uart_info	*info;
140 	struct clk			*clk;
141 	struct clk			*baudclk;
142 	struct uart_port		port;
143 	struct s3c24xx_serial_drv_data	*drv_data;
144 
145 	/* reference to platform data */
146 	struct s3c2410_uartcfg		*cfg;
147 
148 	struct s3c24xx_uart_dma		*dma;
149 
150 #ifdef CONFIG_ARM_S3C24XX_CPUFREQ
151 	struct notifier_block		freq_transition;
152 #endif
153 };
154 
155 static void s3c24xx_serial_tx_chars(struct s3c24xx_uart_port *ourport);
156 
157 /* conversion functions */
158 
159 #define s3c24xx_dev_to_port(__dev) dev_get_drvdata(__dev)
160 
161 /* register access controls */
162 
163 #define portaddr(port, reg) ((port)->membase + (reg))
164 #define portaddrl(port, reg) \
165 	((unsigned long *)(unsigned long)((port)->membase + (reg)))
166 
167 static u32 rd_reg(struct uart_port *port, u32 reg)
168 {
169 	switch (port->iotype) {
170 	case UPIO_MEM:
171 		return readb_relaxed(portaddr(port, reg));
172 	case UPIO_MEM32:
173 		return readl_relaxed(portaddr(port, reg));
174 	default:
175 		return 0;
176 	}
177 	return 0;
178 }
179 
180 #define rd_regl(port, reg) (readl_relaxed(portaddr(port, reg)))
181 
182 static void wr_reg(struct uart_port *port, u32 reg, u32 val)
183 {
184 	switch (port->iotype) {
185 	case UPIO_MEM:
186 		writeb_relaxed(val, portaddr(port, reg));
187 		break;
188 	case UPIO_MEM32:
189 		writel_relaxed(val, portaddr(port, reg));
190 		break;
191 	}
192 }
193 
194 #define wr_regl(port, reg, val) writel_relaxed(val, portaddr(port, reg))
195 
196 /* Byte-order aware bit setting/clearing functions. */
197 
198 static inline void s3c24xx_set_bit(struct uart_port *port, int idx,
199 				   unsigned int reg)
200 {
201 	unsigned long flags;
202 	u32 val;
203 
204 	local_irq_save(flags);
205 	val = rd_regl(port, reg);
206 	val |= (1 << idx);
207 	wr_regl(port, reg, val);
208 	local_irq_restore(flags);
209 }
210 
211 static inline void s3c24xx_clear_bit(struct uart_port *port, int idx,
212 				     unsigned int reg)
213 {
214 	unsigned long flags;
215 	u32 val;
216 
217 	local_irq_save(flags);
218 	val = rd_regl(port, reg);
219 	val &= ~(1 << idx);
220 	wr_regl(port, reg, val);
221 	local_irq_restore(flags);
222 }
223 
224 static inline struct s3c24xx_uart_port *to_ourport(struct uart_port *port)
225 {
226 	return container_of(port, struct s3c24xx_uart_port, port);
227 }
228 
229 /* translate a port to the device name */
230 
231 static inline const char *s3c24xx_serial_portname(struct uart_port *port)
232 {
233 	return to_platform_device(port->dev)->name;
234 }
235 
236 static int s3c24xx_serial_txempty_nofifo(struct uart_port *port)
237 {
238 	return rd_regl(port, S3C2410_UTRSTAT) & S3C2410_UTRSTAT_TXE;
239 }
240 
241 static void s3c24xx_serial_rx_enable(struct uart_port *port)
242 {
243 	struct s3c24xx_uart_port *ourport = to_ourport(port);
244 	unsigned long flags;
245 	unsigned int ucon, ufcon;
246 	int count = 10000;
247 
248 	spin_lock_irqsave(&port->lock, flags);
249 
250 	while (--count && !s3c24xx_serial_txempty_nofifo(port))
251 		udelay(100);
252 
253 	ufcon = rd_regl(port, S3C2410_UFCON);
254 	ufcon |= S3C2410_UFCON_RESETRX;
255 	wr_regl(port, S3C2410_UFCON, ufcon);
256 
257 	ucon = rd_regl(port, S3C2410_UCON);
258 	ucon |= S3C2410_UCON_RXIRQMODE;
259 	wr_regl(port, S3C2410_UCON, ucon);
260 
261 	ourport->rx_enabled = 1;
262 	spin_unlock_irqrestore(&port->lock, flags);
263 }
264 
265 static void s3c24xx_serial_rx_disable(struct uart_port *port)
266 {
267 	struct s3c24xx_uart_port *ourport = to_ourport(port);
268 	unsigned long flags;
269 	unsigned int ucon;
270 
271 	spin_lock_irqsave(&port->lock, flags);
272 
273 	ucon = rd_regl(port, S3C2410_UCON);
274 	ucon &= ~S3C2410_UCON_RXIRQMODE;
275 	wr_regl(port, S3C2410_UCON, ucon);
276 
277 	ourport->rx_enabled = 0;
278 	spin_unlock_irqrestore(&port->lock, flags);
279 }
280 
281 static void s3c24xx_serial_stop_tx(struct uart_port *port)
282 {
283 	struct s3c24xx_uart_port *ourport = to_ourport(port);
284 	struct s3c24xx_uart_dma *dma = ourport->dma;
285 	struct circ_buf *xmit = &port->state->xmit;
286 	struct dma_tx_state state;
287 	int count;
288 
289 	if (!ourport->tx_enabled)
290 		return;
291 
292 	switch (ourport->info->type) {
293 	case TYPE_S3C6400:
294 		s3c24xx_set_bit(port, S3C64XX_UINTM_TXD, S3C64XX_UINTM);
295 		break;
296 	case TYPE_APPLE_S5L:
297 		s3c24xx_clear_bit(port, APPLE_S5L_UCON_TXTHRESH_ENA, S3C2410_UCON);
298 		break;
299 	default:
300 		disable_irq_nosync(ourport->tx_irq);
301 		break;
302 	}
303 
304 	if (dma && dma->tx_chan && ourport->tx_in_progress == S3C24XX_TX_DMA) {
305 		dmaengine_pause(dma->tx_chan);
306 		dmaengine_tx_status(dma->tx_chan, dma->tx_cookie, &state);
307 		dmaengine_terminate_all(dma->tx_chan);
308 		dma_sync_single_for_cpu(dma->tx_chan->device->dev,
309 					dma->tx_transfer_addr, dma->tx_size,
310 					DMA_TO_DEVICE);
311 		async_tx_ack(dma->tx_desc);
312 		count = dma->tx_bytes_requested - state.residue;
313 		xmit->tail = (xmit->tail + count) & (UART_XMIT_SIZE - 1);
314 		port->icount.tx += count;
315 	}
316 
317 	ourport->tx_enabled = 0;
318 	ourport->tx_in_progress = 0;
319 
320 	if (port->flags & UPF_CONS_FLOW)
321 		s3c24xx_serial_rx_enable(port);
322 
323 	ourport->tx_mode = 0;
324 }
325 
326 static void s3c24xx_serial_start_next_tx(struct s3c24xx_uart_port *ourport);
327 
328 static void s3c24xx_serial_tx_dma_complete(void *args)
329 {
330 	struct s3c24xx_uart_port *ourport = args;
331 	struct uart_port *port = &ourport->port;
332 	struct circ_buf *xmit = &port->state->xmit;
333 	struct s3c24xx_uart_dma *dma = ourport->dma;
334 	struct dma_tx_state state;
335 	unsigned long flags;
336 	int count;
337 
338 	dmaengine_tx_status(dma->tx_chan, dma->tx_cookie, &state);
339 	count = dma->tx_bytes_requested - state.residue;
340 	async_tx_ack(dma->tx_desc);
341 
342 	dma_sync_single_for_cpu(dma->tx_chan->device->dev,
343 				dma->tx_transfer_addr, dma->tx_size,
344 				DMA_TO_DEVICE);
345 
346 	spin_lock_irqsave(&port->lock, flags);
347 
348 	xmit->tail = (xmit->tail + count) & (UART_XMIT_SIZE - 1);
349 	port->icount.tx += count;
350 	ourport->tx_in_progress = 0;
351 
352 	if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
353 		uart_write_wakeup(port);
354 
355 	s3c24xx_serial_start_next_tx(ourport);
356 	spin_unlock_irqrestore(&port->lock, flags);
357 }
358 
359 static void enable_tx_dma(struct s3c24xx_uart_port *ourport)
360 {
361 	struct uart_port *port = &ourport->port;
362 	u32 ucon;
363 
364 	/* Mask Tx interrupt */
365 	switch (ourport->info->type) {
366 	case TYPE_S3C6400:
367 		s3c24xx_set_bit(port, S3C64XX_UINTM_TXD, S3C64XX_UINTM);
368 		break;
369 	case TYPE_APPLE_S5L:
370 		WARN_ON(1); // No DMA
371 		break;
372 	default:
373 		disable_irq_nosync(ourport->tx_irq);
374 		break;
375 	}
376 
377 	/* Enable tx dma mode */
378 	ucon = rd_regl(port, S3C2410_UCON);
379 	ucon &= ~(S3C64XX_UCON_TXBURST_MASK | S3C64XX_UCON_TXMODE_MASK);
380 	ucon |= (dma_get_cache_alignment() >= 16) ?
381 		S3C64XX_UCON_TXBURST_16 : S3C64XX_UCON_TXBURST_1;
382 	ucon |= S3C64XX_UCON_TXMODE_DMA;
383 	wr_regl(port,  S3C2410_UCON, ucon);
384 
385 	ourport->tx_mode = S3C24XX_TX_DMA;
386 }
387 
388 static void enable_tx_pio(struct s3c24xx_uart_port *ourport)
389 {
390 	struct uart_port *port = &ourport->port;
391 	u32 ucon, ufcon;
392 
393 	/* Set ufcon txtrig */
394 	ourport->tx_in_progress = S3C24XX_TX_PIO;
395 	ufcon = rd_regl(port, S3C2410_UFCON);
396 	wr_regl(port,  S3C2410_UFCON, ufcon);
397 
398 	/* Enable tx pio mode */
399 	ucon = rd_regl(port, S3C2410_UCON);
400 	ucon &= ~(S3C64XX_UCON_TXMODE_MASK);
401 	ucon |= S3C64XX_UCON_TXMODE_CPU;
402 	wr_regl(port,  S3C2410_UCON, ucon);
403 
404 	/* Unmask Tx interrupt */
405 	switch (ourport->info->type) {
406 	case TYPE_S3C6400:
407 		s3c24xx_clear_bit(port, S3C64XX_UINTM_TXD,
408 				  S3C64XX_UINTM);
409 		break;
410 	case TYPE_APPLE_S5L:
411 		ucon |= APPLE_S5L_UCON_TXTHRESH_ENA_MSK;
412 		wr_regl(port, S3C2410_UCON, ucon);
413 		break;
414 	default:
415 		enable_irq(ourport->tx_irq);
416 		break;
417 	}
418 
419 	ourport->tx_mode = S3C24XX_TX_PIO;
420 
421 	/*
422 	 * The Apple version only has edge triggered TX IRQs, so we need
423 	 * to kick off the process by sending some characters here.
424 	 */
425 	if (ourport->info->type == TYPE_APPLE_S5L)
426 		s3c24xx_serial_tx_chars(ourport);
427 }
428 
429 static void s3c24xx_serial_start_tx_pio(struct s3c24xx_uart_port *ourport)
430 {
431 	if (ourport->tx_mode != S3C24XX_TX_PIO)
432 		enable_tx_pio(ourport);
433 }
434 
435 static int s3c24xx_serial_start_tx_dma(struct s3c24xx_uart_port *ourport,
436 				      unsigned int count)
437 {
438 	struct uart_port *port = &ourport->port;
439 	struct circ_buf *xmit = &port->state->xmit;
440 	struct s3c24xx_uart_dma *dma = ourport->dma;
441 
442 	if (ourport->tx_mode != S3C24XX_TX_DMA)
443 		enable_tx_dma(ourport);
444 
445 	dma->tx_size = count & ~(dma_get_cache_alignment() - 1);
446 	dma->tx_transfer_addr = dma->tx_addr + xmit->tail;
447 
448 	dma_sync_single_for_device(dma->tx_chan->device->dev,
449 				   dma->tx_transfer_addr, dma->tx_size,
450 				   DMA_TO_DEVICE);
451 
452 	dma->tx_desc = dmaengine_prep_slave_single(dma->tx_chan,
453 				dma->tx_transfer_addr, dma->tx_size,
454 				DMA_MEM_TO_DEV, DMA_PREP_INTERRUPT);
455 	if (!dma->tx_desc) {
456 		dev_err(ourport->port.dev, "Unable to get desc for Tx\n");
457 		return -EIO;
458 	}
459 
460 	dma->tx_desc->callback = s3c24xx_serial_tx_dma_complete;
461 	dma->tx_desc->callback_param = ourport;
462 	dma->tx_bytes_requested = dma->tx_size;
463 
464 	ourport->tx_in_progress = S3C24XX_TX_DMA;
465 	dma->tx_cookie = dmaengine_submit(dma->tx_desc);
466 	dma_async_issue_pending(dma->tx_chan);
467 	return 0;
468 }
469 
470 static void s3c24xx_serial_start_next_tx(struct s3c24xx_uart_port *ourport)
471 {
472 	struct uart_port *port = &ourport->port;
473 	struct circ_buf *xmit = &port->state->xmit;
474 	unsigned long count;
475 
476 	/* Get data size up to the end of buffer */
477 	count = CIRC_CNT_TO_END(xmit->head, xmit->tail, UART_XMIT_SIZE);
478 
479 	if (!count) {
480 		s3c24xx_serial_stop_tx(port);
481 		return;
482 	}
483 
484 	if (!ourport->dma || !ourport->dma->tx_chan ||
485 	    count < ourport->min_dma_size ||
486 	    xmit->tail & (dma_get_cache_alignment() - 1))
487 		s3c24xx_serial_start_tx_pio(ourport);
488 	else
489 		s3c24xx_serial_start_tx_dma(ourport, count);
490 }
491 
492 static void s3c24xx_serial_start_tx(struct uart_port *port)
493 {
494 	struct s3c24xx_uart_port *ourport = to_ourport(port);
495 	struct circ_buf *xmit = &port->state->xmit;
496 
497 	if (!ourport->tx_enabled) {
498 		if (port->flags & UPF_CONS_FLOW)
499 			s3c24xx_serial_rx_disable(port);
500 
501 		ourport->tx_enabled = 1;
502 		if (!ourport->dma || !ourport->dma->tx_chan)
503 			s3c24xx_serial_start_tx_pio(ourport);
504 	}
505 
506 	if (ourport->dma && ourport->dma->tx_chan) {
507 		if (!uart_circ_empty(xmit) && !ourport->tx_in_progress)
508 			s3c24xx_serial_start_next_tx(ourport);
509 	}
510 }
511 
512 static void s3c24xx_uart_copy_rx_to_tty(struct s3c24xx_uart_port *ourport,
513 		struct tty_port *tty, int count)
514 {
515 	struct s3c24xx_uart_dma *dma = ourport->dma;
516 	int copied;
517 
518 	if (!count)
519 		return;
520 
521 	dma_sync_single_for_cpu(dma->rx_chan->device->dev, dma->rx_addr,
522 				dma->rx_size, DMA_FROM_DEVICE);
523 
524 	ourport->port.icount.rx += count;
525 	if (!tty) {
526 		dev_err(ourport->port.dev, "No tty port\n");
527 		return;
528 	}
529 	copied = tty_insert_flip_string(tty,
530 			((unsigned char *)(ourport->dma->rx_buf)), count);
531 	if (copied != count) {
532 		WARN_ON(1);
533 		dev_err(ourport->port.dev, "RxData copy to tty layer failed\n");
534 	}
535 }
536 
537 static void s3c24xx_serial_stop_rx(struct uart_port *port)
538 {
539 	struct s3c24xx_uart_port *ourport = to_ourport(port);
540 	struct s3c24xx_uart_dma *dma = ourport->dma;
541 	struct tty_port *t = &port->state->port;
542 	struct dma_tx_state state;
543 	enum dma_status dma_status;
544 	unsigned int received;
545 
546 	if (ourport->rx_enabled) {
547 		dev_dbg(port->dev, "stopping rx\n");
548 		switch (ourport->info->type) {
549 		case TYPE_S3C6400:
550 			s3c24xx_set_bit(port, S3C64XX_UINTM_RXD,
551 					S3C64XX_UINTM);
552 			break;
553 		case TYPE_APPLE_S5L:
554 			s3c24xx_clear_bit(port, APPLE_S5L_UCON_RXTHRESH_ENA, S3C2410_UCON);
555 			s3c24xx_clear_bit(port, APPLE_S5L_UCON_RXTO_ENA, S3C2410_UCON);
556 			break;
557 		default:
558 			disable_irq_nosync(ourport->rx_irq);
559 			break;
560 		}
561 		ourport->rx_enabled = 0;
562 	}
563 	if (dma && dma->rx_chan) {
564 		dmaengine_pause(dma->tx_chan);
565 		dma_status = dmaengine_tx_status(dma->rx_chan,
566 				dma->rx_cookie, &state);
567 		if (dma_status == DMA_IN_PROGRESS ||
568 			dma_status == DMA_PAUSED) {
569 			received = dma->rx_bytes_requested - state.residue;
570 			dmaengine_terminate_all(dma->rx_chan);
571 			s3c24xx_uart_copy_rx_to_tty(ourport, t, received);
572 		}
573 	}
574 }
575 
576 static inline struct s3c24xx_uart_info
577 	*s3c24xx_port_to_info(struct uart_port *port)
578 {
579 	return to_ourport(port)->info;
580 }
581 
582 static inline struct s3c2410_uartcfg
583 	*s3c24xx_port_to_cfg(struct uart_port *port)
584 {
585 	struct s3c24xx_uart_port *ourport;
586 
587 	if (port->dev == NULL)
588 		return NULL;
589 
590 	ourport = container_of(port, struct s3c24xx_uart_port, port);
591 	return ourport->cfg;
592 }
593 
594 static int s3c24xx_serial_rx_fifocnt(struct s3c24xx_uart_port *ourport,
595 				     unsigned long ufstat)
596 {
597 	struct s3c24xx_uart_info *info = ourport->info;
598 
599 	if (ufstat & info->rx_fifofull)
600 		return ourport->port.fifosize;
601 
602 	return (ufstat & info->rx_fifomask) >> info->rx_fifoshift;
603 }
604 
605 static void s3c64xx_start_rx_dma(struct s3c24xx_uart_port *ourport);
606 static void s3c24xx_serial_rx_dma_complete(void *args)
607 {
608 	struct s3c24xx_uart_port *ourport = args;
609 	struct uart_port *port = &ourport->port;
610 
611 	struct s3c24xx_uart_dma *dma = ourport->dma;
612 	struct tty_port *t = &port->state->port;
613 	struct tty_struct *tty = tty_port_tty_get(&ourport->port.state->port);
614 
615 	struct dma_tx_state state;
616 	unsigned long flags;
617 	int received;
618 
619 	dmaengine_tx_status(dma->rx_chan,  dma->rx_cookie, &state);
620 	received  = dma->rx_bytes_requested - state.residue;
621 	async_tx_ack(dma->rx_desc);
622 
623 	spin_lock_irqsave(&port->lock, flags);
624 
625 	if (received)
626 		s3c24xx_uart_copy_rx_to_tty(ourport, t, received);
627 
628 	if (tty) {
629 		tty_flip_buffer_push(t);
630 		tty_kref_put(tty);
631 	}
632 
633 	s3c64xx_start_rx_dma(ourport);
634 
635 	spin_unlock_irqrestore(&port->lock, flags);
636 }
637 
638 static void s3c64xx_start_rx_dma(struct s3c24xx_uart_port *ourport)
639 {
640 	struct s3c24xx_uart_dma *dma = ourport->dma;
641 
642 	dma_sync_single_for_device(dma->rx_chan->device->dev, dma->rx_addr,
643 				   dma->rx_size, DMA_FROM_DEVICE);
644 
645 	dma->rx_desc = dmaengine_prep_slave_single(dma->rx_chan,
646 				dma->rx_addr, dma->rx_size, DMA_DEV_TO_MEM,
647 				DMA_PREP_INTERRUPT);
648 	if (!dma->rx_desc) {
649 		dev_err(ourport->port.dev, "Unable to get desc for Rx\n");
650 		return;
651 	}
652 
653 	dma->rx_desc->callback = s3c24xx_serial_rx_dma_complete;
654 	dma->rx_desc->callback_param = ourport;
655 	dma->rx_bytes_requested = dma->rx_size;
656 
657 	dma->rx_cookie = dmaengine_submit(dma->rx_desc);
658 	dma_async_issue_pending(dma->rx_chan);
659 }
660 
661 /* ? - where has parity gone?? */
662 #define S3C2410_UERSTAT_PARITY (0x1000)
663 
664 static void enable_rx_dma(struct s3c24xx_uart_port *ourport)
665 {
666 	struct uart_port *port = &ourport->port;
667 	unsigned int ucon;
668 
669 	/* set Rx mode to DMA mode */
670 	ucon = rd_regl(port, S3C2410_UCON);
671 	ucon &= ~(S3C64XX_UCON_RXBURST_MASK |
672 			S3C64XX_UCON_TIMEOUT_MASK |
673 			S3C64XX_UCON_EMPTYINT_EN |
674 			S3C64XX_UCON_DMASUS_EN |
675 			S3C64XX_UCON_TIMEOUT_EN |
676 			S3C64XX_UCON_RXMODE_MASK);
677 	ucon |= S3C64XX_UCON_RXBURST_16 |
678 			0xf << S3C64XX_UCON_TIMEOUT_SHIFT |
679 			S3C64XX_UCON_EMPTYINT_EN |
680 			S3C64XX_UCON_TIMEOUT_EN |
681 			S3C64XX_UCON_RXMODE_DMA;
682 	wr_regl(port, S3C2410_UCON, ucon);
683 
684 	ourport->rx_mode = S3C24XX_RX_DMA;
685 }
686 
687 static void enable_rx_pio(struct s3c24xx_uart_port *ourport)
688 {
689 	struct uart_port *port = &ourport->port;
690 	unsigned int ucon;
691 
692 	/* set Rx mode to DMA mode */
693 	ucon = rd_regl(port, S3C2410_UCON);
694 	ucon &= ~S3C64XX_UCON_RXMODE_MASK;
695 	ucon |= S3C64XX_UCON_RXMODE_CPU;
696 
697 	/* Apple types use these bits for IRQ masks */
698 	if (ourport->info->type != TYPE_APPLE_S5L) {
699 		ucon &= ~(S3C64XX_UCON_TIMEOUT_MASK |
700 				S3C64XX_UCON_EMPTYINT_EN |
701 				S3C64XX_UCON_DMASUS_EN |
702 				S3C64XX_UCON_TIMEOUT_EN);
703 		ucon |= 0xf << S3C64XX_UCON_TIMEOUT_SHIFT |
704 				S3C64XX_UCON_TIMEOUT_EN;
705 	}
706 	wr_regl(port, S3C2410_UCON, ucon);
707 
708 	ourport->rx_mode = S3C24XX_RX_PIO;
709 }
710 
711 static void s3c24xx_serial_rx_drain_fifo(struct s3c24xx_uart_port *ourport);
712 
713 static irqreturn_t s3c24xx_serial_rx_chars_dma(void *dev_id)
714 {
715 	unsigned int utrstat, received;
716 	struct s3c24xx_uart_port *ourport = dev_id;
717 	struct uart_port *port = &ourport->port;
718 	struct s3c24xx_uart_dma *dma = ourport->dma;
719 	struct tty_struct *tty = tty_port_tty_get(&ourport->port.state->port);
720 	struct tty_port *t = &port->state->port;
721 	struct dma_tx_state state;
722 
723 	utrstat = rd_regl(port, S3C2410_UTRSTAT);
724 	rd_regl(port, S3C2410_UFSTAT);
725 
726 	spin_lock(&port->lock);
727 
728 	if (!(utrstat & S3C2410_UTRSTAT_TIMEOUT)) {
729 		s3c64xx_start_rx_dma(ourport);
730 		if (ourport->rx_mode == S3C24XX_RX_PIO)
731 			enable_rx_dma(ourport);
732 		goto finish;
733 	}
734 
735 	if (ourport->rx_mode == S3C24XX_RX_DMA) {
736 		dmaengine_pause(dma->rx_chan);
737 		dmaengine_tx_status(dma->rx_chan, dma->rx_cookie, &state);
738 		dmaengine_terminate_all(dma->rx_chan);
739 		received = dma->rx_bytes_requested - state.residue;
740 		s3c24xx_uart_copy_rx_to_tty(ourport, t, received);
741 
742 		enable_rx_pio(ourport);
743 	}
744 
745 	s3c24xx_serial_rx_drain_fifo(ourport);
746 
747 	if (tty) {
748 		tty_flip_buffer_push(t);
749 		tty_kref_put(tty);
750 	}
751 
752 	wr_regl(port, S3C2410_UTRSTAT, S3C2410_UTRSTAT_TIMEOUT);
753 
754 finish:
755 	spin_unlock(&port->lock);
756 
757 	return IRQ_HANDLED;
758 }
759 
760 static void s3c24xx_serial_rx_drain_fifo(struct s3c24xx_uart_port *ourport)
761 {
762 	struct uart_port *port = &ourport->port;
763 	unsigned int ufcon, ch, flag, ufstat, uerstat;
764 	unsigned int fifocnt = 0;
765 	int max_count = port->fifosize;
766 
767 	while (max_count-- > 0) {
768 		/*
769 		 * Receive all characters known to be in FIFO
770 		 * before reading FIFO level again
771 		 */
772 		if (fifocnt == 0) {
773 			ufstat = rd_regl(port, S3C2410_UFSTAT);
774 			fifocnt = s3c24xx_serial_rx_fifocnt(ourport, ufstat);
775 			if (fifocnt == 0)
776 				break;
777 		}
778 		fifocnt--;
779 
780 		uerstat = rd_regl(port, S3C2410_UERSTAT);
781 		ch = rd_reg(port, S3C2410_URXH);
782 
783 		if (port->flags & UPF_CONS_FLOW) {
784 			int txe = s3c24xx_serial_txempty_nofifo(port);
785 
786 			if (ourport->rx_enabled) {
787 				if (!txe) {
788 					ourport->rx_enabled = 0;
789 					continue;
790 				}
791 			} else {
792 				if (txe) {
793 					ufcon = rd_regl(port, S3C2410_UFCON);
794 					ufcon |= S3C2410_UFCON_RESETRX;
795 					wr_regl(port, S3C2410_UFCON, ufcon);
796 					ourport->rx_enabled = 1;
797 					return;
798 				}
799 				continue;
800 			}
801 		}
802 
803 		/* insert the character into the buffer */
804 
805 		flag = TTY_NORMAL;
806 		port->icount.rx++;
807 
808 		if (unlikely(uerstat & S3C2410_UERSTAT_ANY)) {
809 			dev_dbg(port->dev,
810 				"rxerr: port ch=0x%02x, rxs=0x%08x\n",
811 				ch, uerstat);
812 
813 			/* check for break */
814 			if (uerstat & S3C2410_UERSTAT_BREAK) {
815 				dev_dbg(port->dev, "break!\n");
816 				port->icount.brk++;
817 				if (uart_handle_break(port))
818 					continue; /* Ignore character */
819 			}
820 
821 			if (uerstat & S3C2410_UERSTAT_FRAME)
822 				port->icount.frame++;
823 			if (uerstat & S3C2410_UERSTAT_OVERRUN)
824 				port->icount.overrun++;
825 
826 			uerstat &= port->read_status_mask;
827 
828 			if (uerstat & S3C2410_UERSTAT_BREAK)
829 				flag = TTY_BREAK;
830 			else if (uerstat & S3C2410_UERSTAT_PARITY)
831 				flag = TTY_PARITY;
832 			else if (uerstat & (S3C2410_UERSTAT_FRAME |
833 					    S3C2410_UERSTAT_OVERRUN))
834 				flag = TTY_FRAME;
835 		}
836 
837 		if (uart_handle_sysrq_char(port, ch))
838 			continue; /* Ignore character */
839 
840 		uart_insert_char(port, uerstat, S3C2410_UERSTAT_OVERRUN,
841 				 ch, flag);
842 	}
843 
844 	tty_flip_buffer_push(&port->state->port);
845 }
846 
847 static irqreturn_t s3c24xx_serial_rx_chars_pio(void *dev_id)
848 {
849 	struct s3c24xx_uart_port *ourport = dev_id;
850 	struct uart_port *port = &ourport->port;
851 
852 	spin_lock(&port->lock);
853 	s3c24xx_serial_rx_drain_fifo(ourport);
854 	spin_unlock(&port->lock);
855 
856 	return IRQ_HANDLED;
857 }
858 
859 static irqreturn_t s3c24xx_serial_rx_irq(int irq, void *dev_id)
860 {
861 	struct s3c24xx_uart_port *ourport = dev_id;
862 
863 	if (ourport->dma && ourport->dma->rx_chan)
864 		return s3c24xx_serial_rx_chars_dma(dev_id);
865 	return s3c24xx_serial_rx_chars_pio(dev_id);
866 }
867 
868 static void s3c24xx_serial_tx_chars(struct s3c24xx_uart_port *ourport)
869 {
870 	struct uart_port *port = &ourport->port;
871 	struct circ_buf *xmit = &port->state->xmit;
872 	int count, dma_count = 0;
873 
874 	count = CIRC_CNT_TO_END(xmit->head, xmit->tail, UART_XMIT_SIZE);
875 
876 	if (ourport->dma && ourport->dma->tx_chan &&
877 	    count >= ourport->min_dma_size) {
878 		int align = dma_get_cache_alignment() -
879 			(xmit->tail & (dma_get_cache_alignment() - 1));
880 		if (count - align >= ourport->min_dma_size) {
881 			dma_count = count - align;
882 			count = align;
883 		}
884 	}
885 
886 	if (port->x_char) {
887 		wr_reg(port, S3C2410_UTXH, port->x_char);
888 		port->icount.tx++;
889 		port->x_char = 0;
890 		return;
891 	}
892 
893 	/* if there isn't anything more to transmit, or the uart is now
894 	 * stopped, disable the uart and exit
895 	 */
896 
897 	if (uart_circ_empty(xmit) || uart_tx_stopped(port)) {
898 		s3c24xx_serial_stop_tx(port);
899 		return;
900 	}
901 
902 	/* try and drain the buffer... */
903 
904 	if (count > port->fifosize) {
905 		count = port->fifosize;
906 		dma_count = 0;
907 	}
908 
909 	while (!uart_circ_empty(xmit) && count > 0) {
910 		if (rd_regl(port, S3C2410_UFSTAT) & ourport->info->tx_fifofull)
911 			break;
912 
913 		wr_reg(port, S3C2410_UTXH, xmit->buf[xmit->tail]);
914 		xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
915 		port->icount.tx++;
916 		count--;
917 	}
918 
919 	if (!count && dma_count) {
920 		s3c24xx_serial_start_tx_dma(ourport, dma_count);
921 		return;
922 	}
923 
924 	if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS) {
925 		spin_unlock(&port->lock);
926 		uart_write_wakeup(port);
927 		spin_lock(&port->lock);
928 	}
929 
930 	if (uart_circ_empty(xmit))
931 		s3c24xx_serial_stop_tx(port);
932 }
933 
934 static irqreturn_t s3c24xx_serial_tx_irq(int irq, void *id)
935 {
936 	struct s3c24xx_uart_port *ourport = id;
937 	struct uart_port *port = &ourport->port;
938 
939 	spin_lock(&port->lock);
940 
941 	s3c24xx_serial_tx_chars(ourport);
942 
943 	spin_unlock(&port->lock);
944 	return IRQ_HANDLED;
945 }
946 
947 /* interrupt handler for s3c64xx and later SoC's.*/
948 static irqreturn_t s3c64xx_serial_handle_irq(int irq, void *id)
949 {
950 	struct s3c24xx_uart_port *ourport = id;
951 	struct uart_port *port = &ourport->port;
952 	unsigned int pend = rd_regl(port, S3C64XX_UINTP);
953 	irqreturn_t ret = IRQ_HANDLED;
954 
955 	if (pend & S3C64XX_UINTM_RXD_MSK) {
956 		ret = s3c24xx_serial_rx_irq(irq, id);
957 		wr_regl(port, S3C64XX_UINTP, S3C64XX_UINTM_RXD_MSK);
958 	}
959 	if (pend & S3C64XX_UINTM_TXD_MSK) {
960 		ret = s3c24xx_serial_tx_irq(irq, id);
961 		wr_regl(port, S3C64XX_UINTP, S3C64XX_UINTM_TXD_MSK);
962 	}
963 	return ret;
964 }
965 
966 /* interrupt handler for Apple SoC's.*/
967 static irqreturn_t apple_serial_handle_irq(int irq, void *id)
968 {
969 	struct s3c24xx_uart_port *ourport = id;
970 	struct uart_port *port = &ourport->port;
971 	unsigned int pend = rd_regl(port, S3C2410_UTRSTAT);
972 	irqreturn_t ret = IRQ_NONE;
973 
974 	if (pend & (APPLE_S5L_UTRSTAT_RXTHRESH | APPLE_S5L_UTRSTAT_RXTO)) {
975 		wr_regl(port, S3C2410_UTRSTAT,
976 			APPLE_S5L_UTRSTAT_RXTHRESH | APPLE_S5L_UTRSTAT_RXTO);
977 		ret = s3c24xx_serial_rx_irq(irq, id);
978 	}
979 	if (pend & APPLE_S5L_UTRSTAT_TXTHRESH) {
980 		wr_regl(port, S3C2410_UTRSTAT, APPLE_S5L_UTRSTAT_TXTHRESH);
981 		ret = s3c24xx_serial_tx_irq(irq, id);
982 	}
983 
984 	return ret;
985 }
986 
987 static unsigned int s3c24xx_serial_tx_empty(struct uart_port *port)
988 {
989 	struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
990 	unsigned long ufstat = rd_regl(port, S3C2410_UFSTAT);
991 	unsigned long ufcon = rd_regl(port, S3C2410_UFCON);
992 
993 	if (ufcon & S3C2410_UFCON_FIFOMODE) {
994 		if ((ufstat & info->tx_fifomask) != 0 ||
995 		    (ufstat & info->tx_fifofull))
996 			return 0;
997 
998 		return 1;
999 	}
1000 
1001 	return s3c24xx_serial_txempty_nofifo(port);
1002 }
1003 
1004 /* no modem control lines */
1005 static unsigned int s3c24xx_serial_get_mctrl(struct uart_port *port)
1006 {
1007 	unsigned int umstat = rd_reg(port, S3C2410_UMSTAT);
1008 
1009 	if (umstat & S3C2410_UMSTAT_CTS)
1010 		return TIOCM_CAR | TIOCM_DSR | TIOCM_CTS;
1011 	else
1012 		return TIOCM_CAR | TIOCM_DSR;
1013 }
1014 
1015 static void s3c24xx_serial_set_mctrl(struct uart_port *port, unsigned int mctrl)
1016 {
1017 	unsigned int umcon = rd_regl(port, S3C2410_UMCON);
1018 
1019 	if (mctrl & TIOCM_RTS)
1020 		umcon |= S3C2410_UMCOM_RTS_LOW;
1021 	else
1022 		umcon &= ~S3C2410_UMCOM_RTS_LOW;
1023 
1024 	wr_regl(port, S3C2410_UMCON, umcon);
1025 }
1026 
1027 static void s3c24xx_serial_break_ctl(struct uart_port *port, int break_state)
1028 {
1029 	unsigned long flags;
1030 	unsigned int ucon;
1031 
1032 	spin_lock_irqsave(&port->lock, flags);
1033 
1034 	ucon = rd_regl(port, S3C2410_UCON);
1035 
1036 	if (break_state)
1037 		ucon |= S3C2410_UCON_SBREAK;
1038 	else
1039 		ucon &= ~S3C2410_UCON_SBREAK;
1040 
1041 	wr_regl(port, S3C2410_UCON, ucon);
1042 
1043 	spin_unlock_irqrestore(&port->lock, flags);
1044 }
1045 
1046 static int s3c24xx_serial_request_dma(struct s3c24xx_uart_port *p)
1047 {
1048 	struct s3c24xx_uart_dma	*dma = p->dma;
1049 	struct dma_slave_caps dma_caps;
1050 	const char *reason = NULL;
1051 	int ret;
1052 
1053 	/* Default slave configuration parameters */
1054 	dma->rx_conf.direction		= DMA_DEV_TO_MEM;
1055 	dma->rx_conf.src_addr_width	= DMA_SLAVE_BUSWIDTH_1_BYTE;
1056 	dma->rx_conf.src_addr		= p->port.mapbase + S3C2410_URXH;
1057 	dma->rx_conf.src_maxburst	= 1;
1058 
1059 	dma->tx_conf.direction		= DMA_MEM_TO_DEV;
1060 	dma->tx_conf.dst_addr_width	= DMA_SLAVE_BUSWIDTH_1_BYTE;
1061 	dma->tx_conf.dst_addr		= p->port.mapbase + S3C2410_UTXH;
1062 	dma->tx_conf.dst_maxburst	= 1;
1063 
1064 	dma->rx_chan = dma_request_chan(p->port.dev, "rx");
1065 
1066 	if (IS_ERR(dma->rx_chan)) {
1067 		reason = "DMA RX channel request failed";
1068 		ret = PTR_ERR(dma->rx_chan);
1069 		goto err_warn;
1070 	}
1071 
1072 	ret = dma_get_slave_caps(dma->rx_chan, &dma_caps);
1073 	if (ret < 0 ||
1074 	    dma_caps.residue_granularity < DMA_RESIDUE_GRANULARITY_BURST) {
1075 		reason = "insufficient DMA RX engine capabilities";
1076 		ret = -EOPNOTSUPP;
1077 		goto err_release_rx;
1078 	}
1079 
1080 	dmaengine_slave_config(dma->rx_chan, &dma->rx_conf);
1081 
1082 	dma->tx_chan = dma_request_chan(p->port.dev, "tx");
1083 	if (IS_ERR(dma->tx_chan)) {
1084 		reason = "DMA TX channel request failed";
1085 		ret = PTR_ERR(dma->tx_chan);
1086 		goto err_release_rx;
1087 	}
1088 
1089 	ret = dma_get_slave_caps(dma->tx_chan, &dma_caps);
1090 	if (ret < 0 ||
1091 	    dma_caps.residue_granularity < DMA_RESIDUE_GRANULARITY_BURST) {
1092 		reason = "insufficient DMA TX engine capabilities";
1093 		ret = -EOPNOTSUPP;
1094 		goto err_release_tx;
1095 	}
1096 
1097 	dmaengine_slave_config(dma->tx_chan, &dma->tx_conf);
1098 
1099 	/* RX buffer */
1100 	dma->rx_size = PAGE_SIZE;
1101 
1102 	dma->rx_buf = kmalloc(dma->rx_size, GFP_KERNEL);
1103 	if (!dma->rx_buf) {
1104 		ret = -ENOMEM;
1105 		goto err_release_tx;
1106 	}
1107 
1108 	dma->rx_addr = dma_map_single(dma->rx_chan->device->dev, dma->rx_buf,
1109 				      dma->rx_size, DMA_FROM_DEVICE);
1110 	if (dma_mapping_error(dma->rx_chan->device->dev, dma->rx_addr)) {
1111 		reason = "DMA mapping error for RX buffer";
1112 		ret = -EIO;
1113 		goto err_free_rx;
1114 	}
1115 
1116 	/* TX buffer */
1117 	dma->tx_addr = dma_map_single(dma->tx_chan->device->dev,
1118 				      p->port.state->xmit.buf, UART_XMIT_SIZE,
1119 				      DMA_TO_DEVICE);
1120 	if (dma_mapping_error(dma->tx_chan->device->dev, dma->tx_addr)) {
1121 		reason = "DMA mapping error for TX buffer";
1122 		ret = -EIO;
1123 		goto err_unmap_rx;
1124 	}
1125 
1126 	return 0;
1127 
1128 err_unmap_rx:
1129 	dma_unmap_single(dma->rx_chan->device->dev, dma->rx_addr,
1130 			 dma->rx_size, DMA_FROM_DEVICE);
1131 err_free_rx:
1132 	kfree(dma->rx_buf);
1133 err_release_tx:
1134 	dma_release_channel(dma->tx_chan);
1135 err_release_rx:
1136 	dma_release_channel(dma->rx_chan);
1137 err_warn:
1138 	if (reason)
1139 		dev_warn(p->port.dev, "%s, DMA will not be used\n", reason);
1140 	return ret;
1141 }
1142 
1143 static void s3c24xx_serial_release_dma(struct s3c24xx_uart_port *p)
1144 {
1145 	struct s3c24xx_uart_dma	*dma = p->dma;
1146 
1147 	if (dma->rx_chan) {
1148 		dmaengine_terminate_all(dma->rx_chan);
1149 		dma_unmap_single(dma->rx_chan->device->dev, dma->rx_addr,
1150 				 dma->rx_size, DMA_FROM_DEVICE);
1151 		kfree(dma->rx_buf);
1152 		dma_release_channel(dma->rx_chan);
1153 		dma->rx_chan = NULL;
1154 	}
1155 
1156 	if (dma->tx_chan) {
1157 		dmaengine_terminate_all(dma->tx_chan);
1158 		dma_unmap_single(dma->tx_chan->device->dev, dma->tx_addr,
1159 				 UART_XMIT_SIZE, DMA_TO_DEVICE);
1160 		dma_release_channel(dma->tx_chan);
1161 		dma->tx_chan = NULL;
1162 	}
1163 }
1164 
1165 static void s3c24xx_serial_shutdown(struct uart_port *port)
1166 {
1167 	struct s3c24xx_uart_port *ourport = to_ourport(port);
1168 
1169 	if (ourport->tx_claimed) {
1170 		free_irq(ourport->tx_irq, ourport);
1171 		ourport->tx_enabled = 0;
1172 		ourport->tx_claimed = 0;
1173 		ourport->tx_mode = 0;
1174 	}
1175 
1176 	if (ourport->rx_claimed) {
1177 		free_irq(ourport->rx_irq, ourport);
1178 		ourport->rx_claimed = 0;
1179 		ourport->rx_enabled = 0;
1180 	}
1181 
1182 	if (ourport->dma)
1183 		s3c24xx_serial_release_dma(ourport);
1184 
1185 	ourport->tx_in_progress = 0;
1186 }
1187 
1188 static void s3c64xx_serial_shutdown(struct uart_port *port)
1189 {
1190 	struct s3c24xx_uart_port *ourport = to_ourport(port);
1191 
1192 	ourport->tx_enabled = 0;
1193 	ourport->tx_mode = 0;
1194 	ourport->rx_enabled = 0;
1195 
1196 	free_irq(port->irq, ourport);
1197 
1198 	wr_regl(port, S3C64XX_UINTP, 0xf);
1199 	wr_regl(port, S3C64XX_UINTM, 0xf);
1200 
1201 	if (ourport->dma)
1202 		s3c24xx_serial_release_dma(ourport);
1203 
1204 	ourport->tx_in_progress = 0;
1205 }
1206 
1207 static void apple_s5l_serial_shutdown(struct uart_port *port)
1208 {
1209 	struct s3c24xx_uart_port *ourport = to_ourport(port);
1210 
1211 	unsigned int ucon;
1212 
1213 	ucon = rd_regl(port, S3C2410_UCON);
1214 	ucon &= ~(APPLE_S5L_UCON_TXTHRESH_ENA_MSK |
1215 		  APPLE_S5L_UCON_RXTHRESH_ENA_MSK |
1216 		  APPLE_S5L_UCON_RXTO_ENA_MSK);
1217 	wr_regl(port, S3C2410_UCON, ucon);
1218 
1219 	wr_regl(port, S3C2410_UTRSTAT, APPLE_S5L_UTRSTAT_ALL_FLAGS);
1220 
1221 	free_irq(port->irq, ourport);
1222 
1223 	ourport->tx_enabled = 0;
1224 	ourport->tx_mode = 0;
1225 	ourport->rx_enabled = 0;
1226 
1227 	if (ourport->dma)
1228 		s3c24xx_serial_release_dma(ourport);
1229 
1230 	ourport->tx_in_progress = 0;
1231 }
1232 
1233 static int s3c24xx_serial_startup(struct uart_port *port)
1234 {
1235 	struct s3c24xx_uart_port *ourport = to_ourport(port);
1236 	int ret;
1237 
1238 	ourport->rx_enabled = 1;
1239 
1240 	ret = request_irq(ourport->rx_irq, s3c24xx_serial_rx_irq, 0,
1241 			  s3c24xx_serial_portname(port), ourport);
1242 
1243 	if (ret != 0) {
1244 		dev_err(port->dev, "cannot get irq %d\n", ourport->rx_irq);
1245 		return ret;
1246 	}
1247 
1248 	ourport->rx_claimed = 1;
1249 
1250 	dev_dbg(port->dev, "requesting tx irq...\n");
1251 
1252 	ourport->tx_enabled = 1;
1253 
1254 	ret = request_irq(ourport->tx_irq, s3c24xx_serial_tx_irq, 0,
1255 			  s3c24xx_serial_portname(port), ourport);
1256 
1257 	if (ret) {
1258 		dev_err(port->dev, "cannot get irq %d\n", ourport->tx_irq);
1259 		goto err;
1260 	}
1261 
1262 	ourport->tx_claimed = 1;
1263 
1264 	/* the port reset code should have done the correct
1265 	 * register setup for the port controls
1266 	 */
1267 
1268 	return ret;
1269 
1270 err:
1271 	s3c24xx_serial_shutdown(port);
1272 	return ret;
1273 }
1274 
1275 static int s3c64xx_serial_startup(struct uart_port *port)
1276 {
1277 	struct s3c24xx_uart_port *ourport = to_ourport(port);
1278 	unsigned long flags;
1279 	unsigned int ufcon;
1280 	int ret;
1281 
1282 	wr_regl(port, S3C64XX_UINTM, 0xf);
1283 	if (ourport->dma) {
1284 		ret = s3c24xx_serial_request_dma(ourport);
1285 		if (ret < 0) {
1286 			devm_kfree(port->dev, ourport->dma);
1287 			ourport->dma = NULL;
1288 		}
1289 	}
1290 
1291 	ret = request_irq(port->irq, s3c64xx_serial_handle_irq, IRQF_SHARED,
1292 			  s3c24xx_serial_portname(port), ourport);
1293 	if (ret) {
1294 		dev_err(port->dev, "cannot get irq %d\n", port->irq);
1295 		return ret;
1296 	}
1297 
1298 	/* For compatibility with s3c24xx Soc's */
1299 	ourport->rx_enabled = 1;
1300 	ourport->tx_enabled = 0;
1301 
1302 	spin_lock_irqsave(&port->lock, flags);
1303 
1304 	ufcon = rd_regl(port, S3C2410_UFCON);
1305 	ufcon |= S3C2410_UFCON_RESETRX | S5PV210_UFCON_RXTRIG8;
1306 	if (!uart_console(port))
1307 		ufcon |= S3C2410_UFCON_RESETTX;
1308 	wr_regl(port, S3C2410_UFCON, ufcon);
1309 
1310 	enable_rx_pio(ourport);
1311 
1312 	spin_unlock_irqrestore(&port->lock, flags);
1313 
1314 	/* Enable Rx Interrupt */
1315 	s3c24xx_clear_bit(port, S3C64XX_UINTM_RXD, S3C64XX_UINTM);
1316 
1317 	return ret;
1318 }
1319 
1320 static int apple_s5l_serial_startup(struct uart_port *port)
1321 {
1322 	struct s3c24xx_uart_port *ourport = to_ourport(port);
1323 	unsigned long flags;
1324 	unsigned int ufcon;
1325 	int ret;
1326 
1327 	wr_regl(port, S3C2410_UTRSTAT, APPLE_S5L_UTRSTAT_ALL_FLAGS);
1328 
1329 	ret = request_irq(port->irq, apple_serial_handle_irq, 0,
1330 			  s3c24xx_serial_portname(port), ourport);
1331 	if (ret) {
1332 		dev_err(port->dev, "cannot get irq %d\n", port->irq);
1333 		return ret;
1334 	}
1335 
1336 	/* For compatibility with s3c24xx Soc's */
1337 	ourport->rx_enabled = 1;
1338 	ourport->tx_enabled = 0;
1339 
1340 	spin_lock_irqsave(&port->lock, flags);
1341 
1342 	ufcon = rd_regl(port, S3C2410_UFCON);
1343 	ufcon |= S3C2410_UFCON_RESETRX | S5PV210_UFCON_RXTRIG8;
1344 	if (!uart_console(port))
1345 		ufcon |= S3C2410_UFCON_RESETTX;
1346 	wr_regl(port, S3C2410_UFCON, ufcon);
1347 
1348 	enable_rx_pio(ourport);
1349 
1350 	spin_unlock_irqrestore(&port->lock, flags);
1351 
1352 	/* Enable Rx Interrupt */
1353 	s3c24xx_set_bit(port, APPLE_S5L_UCON_RXTHRESH_ENA, S3C2410_UCON);
1354 	s3c24xx_set_bit(port, APPLE_S5L_UCON_RXTO_ENA, S3C2410_UCON);
1355 
1356 	return ret;
1357 }
1358 
1359 /* power power management control */
1360 
1361 static void s3c24xx_serial_pm(struct uart_port *port, unsigned int level,
1362 			      unsigned int old)
1363 {
1364 	struct s3c24xx_uart_port *ourport = to_ourport(port);
1365 	int timeout = 10000;
1366 
1367 	ourport->pm_level = level;
1368 
1369 	switch (level) {
1370 	case 3:
1371 		while (--timeout && !s3c24xx_serial_txempty_nofifo(port))
1372 			udelay(100);
1373 
1374 		if (!IS_ERR(ourport->baudclk))
1375 			clk_disable_unprepare(ourport->baudclk);
1376 
1377 		clk_disable_unprepare(ourport->clk);
1378 		break;
1379 
1380 	case 0:
1381 		clk_prepare_enable(ourport->clk);
1382 
1383 		if (!IS_ERR(ourport->baudclk))
1384 			clk_prepare_enable(ourport->baudclk);
1385 
1386 		break;
1387 	default:
1388 		dev_err(port->dev, "s3c24xx_serial: unknown pm %d\n", level);
1389 	}
1390 }
1391 
1392 /* baud rate calculation
1393  *
1394  * The UARTs on the S3C2410/S3C2440 can take their clocks from a number
1395  * of different sources, including the peripheral clock ("pclk") and an
1396  * external clock ("uclk"). The S3C2440 also adds the core clock ("fclk")
1397  * with a programmable extra divisor.
1398  *
1399  * The following code goes through the clock sources, and calculates the
1400  * baud clocks (and the resultant actual baud rates) and then tries to
1401  * pick the closest one and select that.
1402  *
1403  */
1404 
1405 #define MAX_CLK_NAME_LENGTH 15
1406 
1407 static inline int s3c24xx_serial_getsource(struct uart_port *port)
1408 {
1409 	struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
1410 	unsigned int ucon;
1411 
1412 	if (info->num_clks == 1)
1413 		return 0;
1414 
1415 	ucon = rd_regl(port, S3C2410_UCON);
1416 	ucon &= info->clksel_mask;
1417 	return ucon >> info->clksel_shift;
1418 }
1419 
1420 static void s3c24xx_serial_setsource(struct uart_port *port,
1421 			unsigned int clk_sel)
1422 {
1423 	struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
1424 	unsigned int ucon;
1425 
1426 	if (info->num_clks == 1)
1427 		return;
1428 
1429 	ucon = rd_regl(port, S3C2410_UCON);
1430 	if ((ucon & info->clksel_mask) >> info->clksel_shift == clk_sel)
1431 		return;
1432 
1433 	ucon &= ~info->clksel_mask;
1434 	ucon |= clk_sel << info->clksel_shift;
1435 	wr_regl(port, S3C2410_UCON, ucon);
1436 }
1437 
1438 static unsigned int s3c24xx_serial_getclk(struct s3c24xx_uart_port *ourport,
1439 			unsigned int req_baud, struct clk **best_clk,
1440 			unsigned int *clk_num)
1441 {
1442 	struct s3c24xx_uart_info *info = ourport->info;
1443 	struct clk *clk;
1444 	unsigned long rate;
1445 	unsigned int cnt, baud, quot, best_quot = 0;
1446 	char clkname[MAX_CLK_NAME_LENGTH];
1447 	int calc_deviation, deviation = (1 << 30) - 1;
1448 
1449 	for (cnt = 0; cnt < info->num_clks; cnt++) {
1450 		/* Keep selected clock if provided */
1451 		if (ourport->cfg->clk_sel &&
1452 			!(ourport->cfg->clk_sel & (1 << cnt)))
1453 			continue;
1454 
1455 		sprintf(clkname, "clk_uart_baud%d", cnt);
1456 		clk = clk_get(ourport->port.dev, clkname);
1457 		if (IS_ERR(clk))
1458 			continue;
1459 
1460 		rate = clk_get_rate(clk);
1461 		if (!rate)
1462 			continue;
1463 
1464 		if (ourport->info->has_divslot) {
1465 			unsigned long div = rate / req_baud;
1466 
1467 			/* The UDIVSLOT register on the newer UARTs allows us to
1468 			 * get a divisor adjustment of 1/16th on the baud clock.
1469 			 *
1470 			 * We don't keep the UDIVSLOT value (the 16ths we
1471 			 * calculated by not multiplying the baud by 16) as it
1472 			 * is easy enough to recalculate.
1473 			 */
1474 
1475 			quot = div / 16;
1476 			baud = rate / div;
1477 		} else {
1478 			quot = (rate + (8 * req_baud)) / (16 * req_baud);
1479 			baud = rate / (quot * 16);
1480 		}
1481 		quot--;
1482 
1483 		calc_deviation = req_baud - baud;
1484 		if (calc_deviation < 0)
1485 			calc_deviation = -calc_deviation;
1486 
1487 		if (calc_deviation < deviation) {
1488 			*best_clk = clk;
1489 			best_quot = quot;
1490 			*clk_num = cnt;
1491 			deviation = calc_deviation;
1492 		}
1493 	}
1494 
1495 	return best_quot;
1496 }
1497 
1498 /* udivslot_table[]
1499  *
1500  * This table takes the fractional value of the baud divisor and gives
1501  * the recommended setting for the UDIVSLOT register.
1502  */
1503 static u16 udivslot_table[16] = {
1504 	[0] = 0x0000,
1505 	[1] = 0x0080,
1506 	[2] = 0x0808,
1507 	[3] = 0x0888,
1508 	[4] = 0x2222,
1509 	[5] = 0x4924,
1510 	[6] = 0x4A52,
1511 	[7] = 0x54AA,
1512 	[8] = 0x5555,
1513 	[9] = 0xD555,
1514 	[10] = 0xD5D5,
1515 	[11] = 0xDDD5,
1516 	[12] = 0xDDDD,
1517 	[13] = 0xDFDD,
1518 	[14] = 0xDFDF,
1519 	[15] = 0xFFDF,
1520 };
1521 
1522 static void s3c24xx_serial_set_termios(struct uart_port *port,
1523 				       struct ktermios *termios,
1524 				       struct ktermios *old)
1525 {
1526 	struct s3c2410_uartcfg *cfg = s3c24xx_port_to_cfg(port);
1527 	struct s3c24xx_uart_port *ourport = to_ourport(port);
1528 	struct clk *clk = ERR_PTR(-EINVAL);
1529 	unsigned long flags;
1530 	unsigned int baud, quot, clk_sel = 0;
1531 	unsigned int ulcon;
1532 	unsigned int umcon;
1533 	unsigned int udivslot = 0;
1534 
1535 	/*
1536 	 * We don't support modem control lines.
1537 	 */
1538 	termios->c_cflag &= ~(HUPCL | CMSPAR);
1539 	termios->c_cflag |= CLOCAL;
1540 
1541 	/*
1542 	 * Ask the core to calculate the divisor for us.
1543 	 */
1544 
1545 	baud = uart_get_baud_rate(port, termios, old, 0, 3000000);
1546 	quot = s3c24xx_serial_getclk(ourport, baud, &clk, &clk_sel);
1547 	if (baud == 38400 && (port->flags & UPF_SPD_MASK) == UPF_SPD_CUST)
1548 		quot = port->custom_divisor;
1549 	if (IS_ERR(clk))
1550 		return;
1551 
1552 	/* check to see if we need  to change clock source */
1553 
1554 	if (ourport->baudclk != clk) {
1555 		clk_prepare_enable(clk);
1556 
1557 		s3c24xx_serial_setsource(port, clk_sel);
1558 
1559 		if (!IS_ERR(ourport->baudclk)) {
1560 			clk_disable_unprepare(ourport->baudclk);
1561 			ourport->baudclk = ERR_PTR(-EINVAL);
1562 		}
1563 
1564 		ourport->baudclk = clk;
1565 		ourport->baudclk_rate = clk ? clk_get_rate(clk) : 0;
1566 	}
1567 
1568 	if (ourport->info->has_divslot) {
1569 		unsigned int div = ourport->baudclk_rate / baud;
1570 
1571 		if (cfg->has_fracval) {
1572 			udivslot = (div & 15);
1573 			dev_dbg(port->dev, "fracval = %04x\n", udivslot);
1574 		} else {
1575 			udivslot = udivslot_table[div & 15];
1576 			dev_dbg(port->dev, "udivslot = %04x (div %d)\n",
1577 				udivslot, div & 15);
1578 		}
1579 	}
1580 
1581 	switch (termios->c_cflag & CSIZE) {
1582 	case CS5:
1583 		dev_dbg(port->dev, "config: 5bits/char\n");
1584 		ulcon = S3C2410_LCON_CS5;
1585 		break;
1586 	case CS6:
1587 		dev_dbg(port->dev, "config: 6bits/char\n");
1588 		ulcon = S3C2410_LCON_CS6;
1589 		break;
1590 	case CS7:
1591 		dev_dbg(port->dev, "config: 7bits/char\n");
1592 		ulcon = S3C2410_LCON_CS7;
1593 		break;
1594 	case CS8:
1595 	default:
1596 		dev_dbg(port->dev, "config: 8bits/char\n");
1597 		ulcon = S3C2410_LCON_CS8;
1598 		break;
1599 	}
1600 
1601 	/* preserve original lcon IR settings */
1602 	ulcon |= (cfg->ulcon & S3C2410_LCON_IRM);
1603 
1604 	if (termios->c_cflag & CSTOPB)
1605 		ulcon |= S3C2410_LCON_STOPB;
1606 
1607 	if (termios->c_cflag & PARENB) {
1608 		if (termios->c_cflag & PARODD)
1609 			ulcon |= S3C2410_LCON_PODD;
1610 		else
1611 			ulcon |= S3C2410_LCON_PEVEN;
1612 	} else {
1613 		ulcon |= S3C2410_LCON_PNONE;
1614 	}
1615 
1616 	spin_lock_irqsave(&port->lock, flags);
1617 
1618 	dev_dbg(port->dev,
1619 		"setting ulcon to %08x, brddiv to %d, udivslot %08x\n",
1620 		ulcon, quot, udivslot);
1621 
1622 	wr_regl(port, S3C2410_ULCON, ulcon);
1623 	wr_regl(port, S3C2410_UBRDIV, quot);
1624 
1625 	port->status &= ~UPSTAT_AUTOCTS;
1626 
1627 	umcon = rd_regl(port, S3C2410_UMCON);
1628 	if (termios->c_cflag & CRTSCTS) {
1629 		umcon |= S3C2410_UMCOM_AFC;
1630 		/* Disable RTS when RX FIFO contains 63 bytes */
1631 		umcon &= ~S3C2412_UMCON_AFC_8;
1632 		port->status = UPSTAT_AUTOCTS;
1633 	} else {
1634 		umcon &= ~S3C2410_UMCOM_AFC;
1635 	}
1636 	wr_regl(port, S3C2410_UMCON, umcon);
1637 
1638 	if (ourport->info->has_divslot)
1639 		wr_regl(port, S3C2443_DIVSLOT, udivslot);
1640 
1641 	dev_dbg(port->dev,
1642 		"uart: ulcon = 0x%08x, ucon = 0x%08x, ufcon = 0x%08x\n",
1643 		rd_regl(port, S3C2410_ULCON),
1644 		rd_regl(port, S3C2410_UCON),
1645 		rd_regl(port, S3C2410_UFCON));
1646 
1647 	/*
1648 	 * Update the per-port timeout.
1649 	 */
1650 	uart_update_timeout(port, termios->c_cflag, baud);
1651 
1652 	/*
1653 	 * Which character status flags are we interested in?
1654 	 */
1655 	port->read_status_mask = S3C2410_UERSTAT_OVERRUN;
1656 	if (termios->c_iflag & INPCK)
1657 		port->read_status_mask |= S3C2410_UERSTAT_FRAME |
1658 			S3C2410_UERSTAT_PARITY;
1659 	/*
1660 	 * Which character status flags should we ignore?
1661 	 */
1662 	port->ignore_status_mask = 0;
1663 	if (termios->c_iflag & IGNPAR)
1664 		port->ignore_status_mask |= S3C2410_UERSTAT_OVERRUN;
1665 	if (termios->c_iflag & IGNBRK && termios->c_iflag & IGNPAR)
1666 		port->ignore_status_mask |= S3C2410_UERSTAT_FRAME;
1667 
1668 	/*
1669 	 * Ignore all characters if CREAD is not set.
1670 	 */
1671 	if ((termios->c_cflag & CREAD) == 0)
1672 		port->ignore_status_mask |= RXSTAT_DUMMY_READ;
1673 
1674 	spin_unlock_irqrestore(&port->lock, flags);
1675 }
1676 
1677 static const char *s3c24xx_serial_type(struct uart_port *port)
1678 {
1679 	struct s3c24xx_uart_port *ourport = to_ourport(port);
1680 
1681 	switch (ourport->info->type) {
1682 	case TYPE_S3C24XX:
1683 		return "S3C24XX";
1684 	case TYPE_S3C6400:
1685 		return "S3C6400/10";
1686 	case TYPE_APPLE_S5L:
1687 		return "APPLE S5L";
1688 	default:
1689 		return NULL;
1690 	}
1691 }
1692 
1693 static void s3c24xx_serial_config_port(struct uart_port *port, int flags)
1694 {
1695 	struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
1696 
1697 	if (flags & UART_CONFIG_TYPE)
1698 		port->type = info->port_type;
1699 }
1700 
1701 /*
1702  * verify the new serial_struct (for TIOCSSERIAL).
1703  */
1704 static int
1705 s3c24xx_serial_verify_port(struct uart_port *port, struct serial_struct *ser)
1706 {
1707 	struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
1708 
1709 	if (ser->type != PORT_UNKNOWN && ser->type != info->port_type)
1710 		return -EINVAL;
1711 
1712 	return 0;
1713 }
1714 
1715 #ifdef CONFIG_SERIAL_SAMSUNG_CONSOLE
1716 
1717 static struct console s3c24xx_serial_console;
1718 
1719 static int __init s3c24xx_serial_console_init(void)
1720 {
1721 	register_console(&s3c24xx_serial_console);
1722 	return 0;
1723 }
1724 console_initcall(s3c24xx_serial_console_init);
1725 
1726 #define S3C24XX_SERIAL_CONSOLE &s3c24xx_serial_console
1727 #else
1728 #define S3C24XX_SERIAL_CONSOLE NULL
1729 #endif
1730 
1731 #if defined(CONFIG_SERIAL_SAMSUNG_CONSOLE) && defined(CONFIG_CONSOLE_POLL)
1732 static int s3c24xx_serial_get_poll_char(struct uart_port *port);
1733 static void s3c24xx_serial_put_poll_char(struct uart_port *port,
1734 			 unsigned char c);
1735 #endif
1736 
1737 static const struct uart_ops s3c24xx_serial_ops = {
1738 	.pm		= s3c24xx_serial_pm,
1739 	.tx_empty	= s3c24xx_serial_tx_empty,
1740 	.get_mctrl	= s3c24xx_serial_get_mctrl,
1741 	.set_mctrl	= s3c24xx_serial_set_mctrl,
1742 	.stop_tx	= s3c24xx_serial_stop_tx,
1743 	.start_tx	= s3c24xx_serial_start_tx,
1744 	.stop_rx	= s3c24xx_serial_stop_rx,
1745 	.break_ctl	= s3c24xx_serial_break_ctl,
1746 	.startup	= s3c24xx_serial_startup,
1747 	.shutdown	= s3c24xx_serial_shutdown,
1748 	.set_termios	= s3c24xx_serial_set_termios,
1749 	.type		= s3c24xx_serial_type,
1750 	.config_port	= s3c24xx_serial_config_port,
1751 	.verify_port	= s3c24xx_serial_verify_port,
1752 #if defined(CONFIG_SERIAL_SAMSUNG_CONSOLE) && defined(CONFIG_CONSOLE_POLL)
1753 	.poll_get_char = s3c24xx_serial_get_poll_char,
1754 	.poll_put_char = s3c24xx_serial_put_poll_char,
1755 #endif
1756 };
1757 
1758 static const struct uart_ops s3c64xx_serial_ops = {
1759 	.pm		= s3c24xx_serial_pm,
1760 	.tx_empty	= s3c24xx_serial_tx_empty,
1761 	.get_mctrl	= s3c24xx_serial_get_mctrl,
1762 	.set_mctrl	= s3c24xx_serial_set_mctrl,
1763 	.stop_tx	= s3c24xx_serial_stop_tx,
1764 	.start_tx	= s3c24xx_serial_start_tx,
1765 	.stop_rx	= s3c24xx_serial_stop_rx,
1766 	.break_ctl	= s3c24xx_serial_break_ctl,
1767 	.startup	= s3c64xx_serial_startup,
1768 	.shutdown	= s3c64xx_serial_shutdown,
1769 	.set_termios	= s3c24xx_serial_set_termios,
1770 	.type		= s3c24xx_serial_type,
1771 	.config_port	= s3c24xx_serial_config_port,
1772 	.verify_port	= s3c24xx_serial_verify_port,
1773 #if defined(CONFIG_SERIAL_SAMSUNG_CONSOLE) && defined(CONFIG_CONSOLE_POLL)
1774 	.poll_get_char = s3c24xx_serial_get_poll_char,
1775 	.poll_put_char = s3c24xx_serial_put_poll_char,
1776 #endif
1777 };
1778 
1779 static const struct uart_ops apple_s5l_serial_ops = {
1780 	.pm		= s3c24xx_serial_pm,
1781 	.tx_empty	= s3c24xx_serial_tx_empty,
1782 	.get_mctrl	= s3c24xx_serial_get_mctrl,
1783 	.set_mctrl	= s3c24xx_serial_set_mctrl,
1784 	.stop_tx	= s3c24xx_serial_stop_tx,
1785 	.start_tx	= s3c24xx_serial_start_tx,
1786 	.stop_rx	= s3c24xx_serial_stop_rx,
1787 	.break_ctl	= s3c24xx_serial_break_ctl,
1788 	.startup	= apple_s5l_serial_startup,
1789 	.shutdown	= apple_s5l_serial_shutdown,
1790 	.set_termios	= s3c24xx_serial_set_termios,
1791 	.type		= s3c24xx_serial_type,
1792 	.config_port	= s3c24xx_serial_config_port,
1793 	.verify_port	= s3c24xx_serial_verify_port,
1794 #if defined(CONFIG_SERIAL_SAMSUNG_CONSOLE) && defined(CONFIG_CONSOLE_POLL)
1795 	.poll_get_char = s3c24xx_serial_get_poll_char,
1796 	.poll_put_char = s3c24xx_serial_put_poll_char,
1797 #endif
1798 };
1799 
1800 static struct uart_driver s3c24xx_uart_drv = {
1801 	.owner		= THIS_MODULE,
1802 	.driver_name	= "s3c2410_serial",
1803 	.nr		= CONFIG_SERIAL_SAMSUNG_UARTS,
1804 	.cons		= S3C24XX_SERIAL_CONSOLE,
1805 	.dev_name	= S3C24XX_SERIAL_NAME,
1806 	.major		= S3C24XX_SERIAL_MAJOR,
1807 	.minor		= S3C24XX_SERIAL_MINOR,
1808 };
1809 
1810 #define __PORT_LOCK_UNLOCKED(i) \
1811 	__SPIN_LOCK_UNLOCKED(s3c24xx_serial_ports[i].port.lock)
1812 static struct s3c24xx_uart_port
1813 s3c24xx_serial_ports[CONFIG_SERIAL_SAMSUNG_UARTS] = {
1814 	[0] = {
1815 		.port = {
1816 			.lock		= __PORT_LOCK_UNLOCKED(0),
1817 			.iotype		= UPIO_MEM,
1818 			.uartclk	= 0,
1819 			.fifosize	= 16,
1820 			.ops		= &s3c24xx_serial_ops,
1821 			.flags		= UPF_BOOT_AUTOCONF,
1822 			.line		= 0,
1823 		}
1824 	},
1825 	[1] = {
1826 		.port = {
1827 			.lock		= __PORT_LOCK_UNLOCKED(1),
1828 			.iotype		= UPIO_MEM,
1829 			.uartclk	= 0,
1830 			.fifosize	= 16,
1831 			.ops		= &s3c24xx_serial_ops,
1832 			.flags		= UPF_BOOT_AUTOCONF,
1833 			.line		= 1,
1834 		}
1835 	},
1836 #if CONFIG_SERIAL_SAMSUNG_UARTS > 2
1837 	[2] = {
1838 		.port = {
1839 			.lock		= __PORT_LOCK_UNLOCKED(2),
1840 			.iotype		= UPIO_MEM,
1841 			.uartclk	= 0,
1842 			.fifosize	= 16,
1843 			.ops		= &s3c24xx_serial_ops,
1844 			.flags		= UPF_BOOT_AUTOCONF,
1845 			.line		= 2,
1846 		}
1847 	},
1848 #endif
1849 #if CONFIG_SERIAL_SAMSUNG_UARTS > 3
1850 	[3] = {
1851 		.port = {
1852 			.lock		= __PORT_LOCK_UNLOCKED(3),
1853 			.iotype		= UPIO_MEM,
1854 			.uartclk	= 0,
1855 			.fifosize	= 16,
1856 			.ops		= &s3c24xx_serial_ops,
1857 			.flags		= UPF_BOOT_AUTOCONF,
1858 			.line		= 3,
1859 		}
1860 	}
1861 #endif
1862 };
1863 #undef __PORT_LOCK_UNLOCKED
1864 
1865 /* s3c24xx_serial_resetport
1866  *
1867  * reset the fifos and other the settings.
1868  */
1869 
1870 static void s3c24xx_serial_resetport(struct uart_port *port,
1871 				   struct s3c2410_uartcfg *cfg)
1872 {
1873 	struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
1874 	unsigned long ucon = rd_regl(port, S3C2410_UCON);
1875 
1876 	ucon &= (info->clksel_mask | info->ucon_mask);
1877 	wr_regl(port, S3C2410_UCON, ucon | cfg->ucon);
1878 
1879 	/* reset both fifos */
1880 	wr_regl(port, S3C2410_UFCON, cfg->ufcon | S3C2410_UFCON_RESETBOTH);
1881 	wr_regl(port, S3C2410_UFCON, cfg->ufcon);
1882 
1883 	/* some delay is required after fifo reset */
1884 	udelay(1);
1885 }
1886 
1887 #ifdef CONFIG_ARM_S3C24XX_CPUFREQ
1888 
1889 static int s3c24xx_serial_cpufreq_transition(struct notifier_block *nb,
1890 					     unsigned long val, void *data)
1891 {
1892 	struct s3c24xx_uart_port *port;
1893 	struct uart_port *uport;
1894 
1895 	port = container_of(nb, struct s3c24xx_uart_port, freq_transition);
1896 	uport = &port->port;
1897 
1898 	/* check to see if port is enabled */
1899 
1900 	if (port->pm_level != 0)
1901 		return 0;
1902 
1903 	/* try and work out if the baudrate is changing, we can detect
1904 	 * a change in rate, but we do not have support for detecting
1905 	 * a disturbance in the clock-rate over the change.
1906 	 */
1907 
1908 	if (IS_ERR(port->baudclk))
1909 		goto exit;
1910 
1911 	if (port->baudclk_rate == clk_get_rate(port->baudclk))
1912 		goto exit;
1913 
1914 	if (val == CPUFREQ_PRECHANGE) {
1915 		/* we should really shut the port down whilst the
1916 		 * frequency change is in progress.
1917 		 */
1918 
1919 	} else if (val == CPUFREQ_POSTCHANGE) {
1920 		struct ktermios *termios;
1921 		struct tty_struct *tty;
1922 
1923 		if (uport->state == NULL)
1924 			goto exit;
1925 
1926 		tty = uport->state->port.tty;
1927 
1928 		if (tty == NULL)
1929 			goto exit;
1930 
1931 		termios = &tty->termios;
1932 
1933 		if (termios == NULL) {
1934 			dev_warn(uport->dev, "%s: no termios?\n", __func__);
1935 			goto exit;
1936 		}
1937 
1938 		s3c24xx_serial_set_termios(uport, termios, NULL);
1939 	}
1940 
1941 exit:
1942 	return 0;
1943 }
1944 
1945 static inline int
1946 s3c24xx_serial_cpufreq_register(struct s3c24xx_uart_port *port)
1947 {
1948 	port->freq_transition.notifier_call = s3c24xx_serial_cpufreq_transition;
1949 
1950 	return cpufreq_register_notifier(&port->freq_transition,
1951 					 CPUFREQ_TRANSITION_NOTIFIER);
1952 }
1953 
1954 static inline void
1955 s3c24xx_serial_cpufreq_deregister(struct s3c24xx_uart_port *port)
1956 {
1957 	cpufreq_unregister_notifier(&port->freq_transition,
1958 				    CPUFREQ_TRANSITION_NOTIFIER);
1959 }
1960 
1961 #else
1962 static inline int
1963 s3c24xx_serial_cpufreq_register(struct s3c24xx_uart_port *port)
1964 {
1965 	return 0;
1966 }
1967 
1968 static inline void
1969 s3c24xx_serial_cpufreq_deregister(struct s3c24xx_uart_port *port)
1970 {
1971 }
1972 #endif
1973 
1974 static int s3c24xx_serial_enable_baudclk(struct s3c24xx_uart_port *ourport)
1975 {
1976 	struct device *dev = ourport->port.dev;
1977 	struct s3c24xx_uart_info *info = ourport->info;
1978 	char clk_name[MAX_CLK_NAME_LENGTH];
1979 	unsigned int clk_sel;
1980 	struct clk *clk;
1981 	int clk_num;
1982 	int ret;
1983 
1984 	clk_sel = ourport->cfg->clk_sel ? : info->def_clk_sel;
1985 	for (clk_num = 0; clk_num < info->num_clks; clk_num++) {
1986 		if (!(clk_sel & (1 << clk_num)))
1987 			continue;
1988 
1989 		sprintf(clk_name, "clk_uart_baud%d", clk_num);
1990 		clk = clk_get(dev, clk_name);
1991 		if (IS_ERR(clk))
1992 			continue;
1993 
1994 		ret = clk_prepare_enable(clk);
1995 		if (ret) {
1996 			clk_put(clk);
1997 			continue;
1998 		}
1999 
2000 		ourport->baudclk = clk;
2001 		ourport->baudclk_rate = clk_get_rate(clk);
2002 		s3c24xx_serial_setsource(&ourport->port, clk_num);
2003 
2004 		return 0;
2005 	}
2006 
2007 	return -EINVAL;
2008 }
2009 
2010 /* s3c24xx_serial_init_port
2011  *
2012  * initialise a single serial port from the platform device given
2013  */
2014 
2015 static int s3c24xx_serial_init_port(struct s3c24xx_uart_port *ourport,
2016 				    struct platform_device *platdev)
2017 {
2018 	struct uart_port *port = &ourport->port;
2019 	struct s3c2410_uartcfg *cfg = ourport->cfg;
2020 	struct resource *res;
2021 	int ret;
2022 
2023 	if (platdev == NULL)
2024 		return -ENODEV;
2025 
2026 	if (port->mapbase != 0)
2027 		return -EINVAL;
2028 
2029 	/* setup info for port */
2030 	port->dev	= &platdev->dev;
2031 
2032 	port->uartclk = 1;
2033 
2034 	if (cfg->uart_flags & UPF_CONS_FLOW) {
2035 		dev_dbg(port->dev, "enabling flow control\n");
2036 		port->flags |= UPF_CONS_FLOW;
2037 	}
2038 
2039 	/* sort our the physical and virtual addresses for each UART */
2040 
2041 	res = platform_get_resource(platdev, IORESOURCE_MEM, 0);
2042 	if (res == NULL) {
2043 		dev_err(port->dev, "failed to find memory resource for uart\n");
2044 		return -EINVAL;
2045 	}
2046 
2047 	dev_dbg(port->dev, "resource %pR)\n", res);
2048 
2049 	port->membase = devm_ioremap_resource(port->dev, res);
2050 	if (IS_ERR(port->membase)) {
2051 		dev_err(port->dev, "failed to remap controller address\n");
2052 		return -EBUSY;
2053 	}
2054 
2055 	port->mapbase = res->start;
2056 	ret = platform_get_irq(platdev, 0);
2057 	if (ret < 0) {
2058 		port->irq = 0;
2059 	} else {
2060 		port->irq = ret;
2061 		ourport->rx_irq = ret;
2062 		ourport->tx_irq = ret + 1;
2063 	}
2064 
2065 	switch (ourport->info->type) {
2066 	case TYPE_S3C24XX:
2067 		ret = platform_get_irq(platdev, 1);
2068 		if (ret > 0)
2069 			ourport->tx_irq = ret;
2070 		break;
2071 	default:
2072 		break;
2073 	}
2074 
2075 	/*
2076 	 * DMA is currently supported only on DT platforms, if DMA properties
2077 	 * are specified.
2078 	 */
2079 	if (platdev->dev.of_node && of_find_property(platdev->dev.of_node,
2080 						     "dmas", NULL)) {
2081 		ourport->dma = devm_kzalloc(port->dev,
2082 					    sizeof(*ourport->dma),
2083 					    GFP_KERNEL);
2084 		if (!ourport->dma) {
2085 			ret = -ENOMEM;
2086 			goto err;
2087 		}
2088 	}
2089 
2090 	ourport->clk	= clk_get(&platdev->dev, "uart");
2091 	if (IS_ERR(ourport->clk)) {
2092 		pr_err("%s: Controller clock not found\n",
2093 				dev_name(&platdev->dev));
2094 		ret = PTR_ERR(ourport->clk);
2095 		goto err;
2096 	}
2097 
2098 	ret = clk_prepare_enable(ourport->clk);
2099 	if (ret) {
2100 		pr_err("uart: clock failed to prepare+enable: %d\n", ret);
2101 		clk_put(ourport->clk);
2102 		goto err;
2103 	}
2104 
2105 	ret = s3c24xx_serial_enable_baudclk(ourport);
2106 	if (ret)
2107 		pr_warn("uart: failed to enable baudclk\n");
2108 
2109 	/* Keep all interrupts masked and cleared */
2110 	switch (ourport->info->type) {
2111 	case TYPE_S3C6400:
2112 		wr_regl(port, S3C64XX_UINTM, 0xf);
2113 		wr_regl(port, S3C64XX_UINTP, 0xf);
2114 		wr_regl(port, S3C64XX_UINTSP, 0xf);
2115 		break;
2116 	case TYPE_APPLE_S5L: {
2117 		unsigned int ucon;
2118 
2119 		ucon = rd_regl(port, S3C2410_UCON);
2120 		ucon &= ~(APPLE_S5L_UCON_TXTHRESH_ENA_MSK |
2121 			APPLE_S5L_UCON_RXTHRESH_ENA_MSK |
2122 			APPLE_S5L_UCON_RXTO_ENA_MSK);
2123 		wr_regl(port, S3C2410_UCON, ucon);
2124 
2125 		wr_regl(port, S3C2410_UTRSTAT, APPLE_S5L_UTRSTAT_ALL_FLAGS);
2126 		break;
2127 	}
2128 	default:
2129 		break;
2130 	}
2131 
2132 	dev_dbg(port->dev, "port: map=%pa, mem=%p, irq=%d (%d,%d), clock=%u\n",
2133 		&port->mapbase, port->membase, port->irq,
2134 		ourport->rx_irq, ourport->tx_irq, port->uartclk);
2135 
2136 	/* reset the fifos (and setup the uart) */
2137 	s3c24xx_serial_resetport(port, cfg);
2138 
2139 	return 0;
2140 
2141 err:
2142 	port->mapbase = 0;
2143 	return ret;
2144 }
2145 
2146 /* Device driver serial port probe */
2147 
2148 #ifdef CONFIG_OF
2149 static const struct of_device_id s3c24xx_uart_dt_match[];
2150 #endif
2151 
2152 static int probe_index;
2153 
2154 static inline struct s3c24xx_serial_drv_data *
2155 s3c24xx_get_driver_data(struct platform_device *pdev)
2156 {
2157 #ifdef CONFIG_OF
2158 	if (pdev->dev.of_node) {
2159 		const struct of_device_id *match;
2160 
2161 		match = of_match_node(s3c24xx_uart_dt_match, pdev->dev.of_node);
2162 		return (struct s3c24xx_serial_drv_data *)match->data;
2163 	}
2164 #endif
2165 	return (struct s3c24xx_serial_drv_data *)
2166 			platform_get_device_id(pdev)->driver_data;
2167 }
2168 
2169 static int s3c24xx_serial_probe(struct platform_device *pdev)
2170 {
2171 	struct device_node *np = pdev->dev.of_node;
2172 	struct s3c24xx_uart_port *ourport;
2173 	int index = probe_index;
2174 	int ret, prop = 0;
2175 
2176 	if (np) {
2177 		ret = of_alias_get_id(np, "serial");
2178 		if (ret >= 0)
2179 			index = ret;
2180 	}
2181 
2182 	if (index >= ARRAY_SIZE(s3c24xx_serial_ports)) {
2183 		dev_err(&pdev->dev, "serial%d out of range\n", index);
2184 		return -EINVAL;
2185 	}
2186 	ourport = &s3c24xx_serial_ports[index];
2187 
2188 	ourport->drv_data = s3c24xx_get_driver_data(pdev);
2189 	if (!ourport->drv_data) {
2190 		dev_err(&pdev->dev, "could not find driver data\n");
2191 		return -ENODEV;
2192 	}
2193 
2194 	ourport->baudclk = ERR_PTR(-EINVAL);
2195 	ourport->info = ourport->drv_data->info;
2196 	ourport->cfg = (dev_get_platdata(&pdev->dev)) ?
2197 			dev_get_platdata(&pdev->dev) :
2198 			ourport->drv_data->def_cfg;
2199 
2200 	switch (ourport->info->type) {
2201 	case TYPE_S3C24XX:
2202 		ourport->port.ops = &s3c24xx_serial_ops;
2203 		break;
2204 	case TYPE_S3C6400:
2205 		ourport->port.ops = &s3c64xx_serial_ops;
2206 		break;
2207 	case TYPE_APPLE_S5L:
2208 		ourport->port.ops = &apple_s5l_serial_ops;
2209 		break;
2210 	}
2211 
2212 	if (np) {
2213 		of_property_read_u32(np,
2214 			"samsung,uart-fifosize", &ourport->port.fifosize);
2215 
2216 		if (of_property_read_u32(np, "reg-io-width", &prop) == 0) {
2217 			switch (prop) {
2218 			case 1:
2219 				ourport->port.iotype = UPIO_MEM;
2220 				break;
2221 			case 4:
2222 				ourport->port.iotype = UPIO_MEM32;
2223 				break;
2224 			default:
2225 				dev_warn(&pdev->dev, "unsupported reg-io-width (%d)\n",
2226 						prop);
2227 				return -EINVAL;
2228 			}
2229 		}
2230 	}
2231 
2232 	if (ourport->drv_data->fifosize[index])
2233 		ourport->port.fifosize = ourport->drv_data->fifosize[index];
2234 	else if (ourport->info->fifosize)
2235 		ourport->port.fifosize = ourport->info->fifosize;
2236 	ourport->port.has_sysrq = IS_ENABLED(CONFIG_SERIAL_SAMSUNG_CONSOLE);
2237 
2238 	/*
2239 	 * DMA transfers must be aligned at least to cache line size,
2240 	 * so find minimal transfer size suitable for DMA mode
2241 	 */
2242 	ourport->min_dma_size = max_t(int, ourport->port.fifosize,
2243 				    dma_get_cache_alignment());
2244 
2245 	dev_dbg(&pdev->dev, "%s: initialising port %p...\n", __func__, ourport);
2246 
2247 	ret = s3c24xx_serial_init_port(ourport, pdev);
2248 	if (ret < 0)
2249 		return ret;
2250 
2251 	if (!s3c24xx_uart_drv.state) {
2252 		ret = uart_register_driver(&s3c24xx_uart_drv);
2253 		if (ret < 0) {
2254 			pr_err("Failed to register Samsung UART driver\n");
2255 			return ret;
2256 		}
2257 	}
2258 
2259 	dev_dbg(&pdev->dev, "%s: adding port\n", __func__);
2260 	uart_add_one_port(&s3c24xx_uart_drv, &ourport->port);
2261 	platform_set_drvdata(pdev, &ourport->port);
2262 
2263 	/*
2264 	 * Deactivate the clock enabled in s3c24xx_serial_init_port here,
2265 	 * so that a potential re-enablement through the pm-callback overlaps
2266 	 * and keeps the clock enabled in this case.
2267 	 */
2268 	clk_disable_unprepare(ourport->clk);
2269 	if (!IS_ERR(ourport->baudclk))
2270 		clk_disable_unprepare(ourport->baudclk);
2271 
2272 	ret = s3c24xx_serial_cpufreq_register(ourport);
2273 	if (ret < 0)
2274 		dev_err(&pdev->dev, "failed to add cpufreq notifier\n");
2275 
2276 	probe_index++;
2277 
2278 	return 0;
2279 }
2280 
2281 static int s3c24xx_serial_remove(struct platform_device *dev)
2282 {
2283 	struct uart_port *port = s3c24xx_dev_to_port(&dev->dev);
2284 
2285 	if (port) {
2286 		s3c24xx_serial_cpufreq_deregister(to_ourport(port));
2287 		uart_remove_one_port(&s3c24xx_uart_drv, port);
2288 	}
2289 
2290 	uart_unregister_driver(&s3c24xx_uart_drv);
2291 
2292 	return 0;
2293 }
2294 
2295 /* UART power management code */
2296 #ifdef CONFIG_PM_SLEEP
2297 static int s3c24xx_serial_suspend(struct device *dev)
2298 {
2299 	struct uart_port *port = s3c24xx_dev_to_port(dev);
2300 
2301 	if (port)
2302 		uart_suspend_port(&s3c24xx_uart_drv, port);
2303 
2304 	return 0;
2305 }
2306 
2307 static int s3c24xx_serial_resume(struct device *dev)
2308 {
2309 	struct uart_port *port = s3c24xx_dev_to_port(dev);
2310 	struct s3c24xx_uart_port *ourport = to_ourport(port);
2311 
2312 	if (port) {
2313 		clk_prepare_enable(ourport->clk);
2314 		if (!IS_ERR(ourport->baudclk))
2315 			clk_prepare_enable(ourport->baudclk);
2316 		s3c24xx_serial_resetport(port, s3c24xx_port_to_cfg(port));
2317 		if (!IS_ERR(ourport->baudclk))
2318 			clk_disable_unprepare(ourport->baudclk);
2319 		clk_disable_unprepare(ourport->clk);
2320 
2321 		uart_resume_port(&s3c24xx_uart_drv, port);
2322 	}
2323 
2324 	return 0;
2325 }
2326 
2327 static int s3c24xx_serial_resume_noirq(struct device *dev)
2328 {
2329 	struct uart_port *port = s3c24xx_dev_to_port(dev);
2330 	struct s3c24xx_uart_port *ourport = to_ourport(port);
2331 
2332 	if (port) {
2333 		/* restore IRQ mask */
2334 		switch (ourport->info->type) {
2335 		case TYPE_S3C6400: {
2336 			unsigned int uintm = 0xf;
2337 
2338 			if (ourport->tx_enabled)
2339 				uintm &= ~S3C64XX_UINTM_TXD_MSK;
2340 			if (ourport->rx_enabled)
2341 				uintm &= ~S3C64XX_UINTM_RXD_MSK;
2342 			clk_prepare_enable(ourport->clk);
2343 			if (!IS_ERR(ourport->baudclk))
2344 				clk_prepare_enable(ourport->baudclk);
2345 			wr_regl(port, S3C64XX_UINTM, uintm);
2346 			if (!IS_ERR(ourport->baudclk))
2347 				clk_disable_unprepare(ourport->baudclk);
2348 			clk_disable_unprepare(ourport->clk);
2349 			break;
2350 		}
2351 		case TYPE_APPLE_S5L: {
2352 			unsigned int ucon;
2353 			int ret;
2354 
2355 			ret = clk_prepare_enable(ourport->clk);
2356 			if (ret) {
2357 				dev_err(dev, "clk_enable clk failed: %d\n", ret);
2358 				return ret;
2359 			}
2360 			if (!IS_ERR(ourport->baudclk)) {
2361 				ret = clk_prepare_enable(ourport->baudclk);
2362 				if (ret) {
2363 					dev_err(dev, "clk_enable baudclk failed: %d\n", ret);
2364 					clk_disable_unprepare(ourport->clk);
2365 					return ret;
2366 				}
2367 			}
2368 
2369 			ucon = rd_regl(port, S3C2410_UCON);
2370 
2371 			ucon &= ~(APPLE_S5L_UCON_TXTHRESH_ENA_MSK |
2372 				  APPLE_S5L_UCON_RXTHRESH_ENA_MSK |
2373 				  APPLE_S5L_UCON_RXTO_ENA_MSK);
2374 
2375 			if (ourport->tx_enabled)
2376 				ucon |= APPLE_S5L_UCON_TXTHRESH_ENA_MSK;
2377 			if (ourport->rx_enabled)
2378 				ucon |= APPLE_S5L_UCON_RXTHRESH_ENA_MSK |
2379 					APPLE_S5L_UCON_RXTO_ENA_MSK;
2380 
2381 			wr_regl(port, S3C2410_UCON, ucon);
2382 
2383 			if (!IS_ERR(ourport->baudclk))
2384 				clk_disable_unprepare(ourport->baudclk);
2385 			clk_disable_unprepare(ourport->clk);
2386 			break;
2387 		}
2388 		default:
2389 			break;
2390 		}
2391 	}
2392 
2393 	return 0;
2394 }
2395 
2396 static const struct dev_pm_ops s3c24xx_serial_pm_ops = {
2397 	.suspend = s3c24xx_serial_suspend,
2398 	.resume = s3c24xx_serial_resume,
2399 	.resume_noirq = s3c24xx_serial_resume_noirq,
2400 };
2401 #define SERIAL_SAMSUNG_PM_OPS	(&s3c24xx_serial_pm_ops)
2402 
2403 #else /* !CONFIG_PM_SLEEP */
2404 
2405 #define SERIAL_SAMSUNG_PM_OPS	NULL
2406 #endif /* CONFIG_PM_SLEEP */
2407 
2408 /* Console code */
2409 
2410 #ifdef CONFIG_SERIAL_SAMSUNG_CONSOLE
2411 
2412 static struct uart_port *cons_uart;
2413 
2414 static int
2415 s3c24xx_serial_console_txrdy(struct uart_port *port, unsigned int ufcon)
2416 {
2417 	struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
2418 	unsigned long ufstat, utrstat;
2419 
2420 	if (ufcon & S3C2410_UFCON_FIFOMODE) {
2421 		/* fifo mode - check amount of data in fifo registers... */
2422 
2423 		ufstat = rd_regl(port, S3C2410_UFSTAT);
2424 		return (ufstat & info->tx_fifofull) ? 0 : 1;
2425 	}
2426 
2427 	/* in non-fifo mode, we go and use the tx buffer empty */
2428 
2429 	utrstat = rd_regl(port, S3C2410_UTRSTAT);
2430 	return (utrstat & S3C2410_UTRSTAT_TXE) ? 1 : 0;
2431 }
2432 
2433 static bool
2434 s3c24xx_port_configured(unsigned int ucon)
2435 {
2436 	/* consider the serial port configured if the tx/rx mode set */
2437 	return (ucon & 0xf) != 0;
2438 }
2439 
2440 #ifdef CONFIG_CONSOLE_POLL
2441 /*
2442  * Console polling routines for writing and reading from the uart while
2443  * in an interrupt or debug context.
2444  */
2445 
2446 static int s3c24xx_serial_get_poll_char(struct uart_port *port)
2447 {
2448 	struct s3c24xx_uart_port *ourport = to_ourport(port);
2449 	unsigned int ufstat;
2450 
2451 	ufstat = rd_regl(port, S3C2410_UFSTAT);
2452 	if (s3c24xx_serial_rx_fifocnt(ourport, ufstat) == 0)
2453 		return NO_POLL_CHAR;
2454 
2455 	return rd_reg(port, S3C2410_URXH);
2456 }
2457 
2458 static void s3c24xx_serial_put_poll_char(struct uart_port *port,
2459 		unsigned char c)
2460 {
2461 	unsigned int ufcon = rd_regl(port, S3C2410_UFCON);
2462 	unsigned int ucon = rd_regl(port, S3C2410_UCON);
2463 
2464 	/* not possible to xmit on unconfigured port */
2465 	if (!s3c24xx_port_configured(ucon))
2466 		return;
2467 
2468 	while (!s3c24xx_serial_console_txrdy(port, ufcon))
2469 		cpu_relax();
2470 	wr_reg(port, S3C2410_UTXH, c);
2471 }
2472 
2473 #endif /* CONFIG_CONSOLE_POLL */
2474 
2475 static void
2476 s3c24xx_serial_console_putchar(struct uart_port *port, int ch)
2477 {
2478 	unsigned int ufcon = rd_regl(port, S3C2410_UFCON);
2479 
2480 	while (!s3c24xx_serial_console_txrdy(port, ufcon))
2481 		cpu_relax();
2482 	wr_reg(port, S3C2410_UTXH, ch);
2483 }
2484 
2485 static void
2486 s3c24xx_serial_console_write(struct console *co, const char *s,
2487 			     unsigned int count)
2488 {
2489 	unsigned int ucon = rd_regl(cons_uart, S3C2410_UCON);
2490 
2491 	/* not possible to xmit on unconfigured port */
2492 	if (!s3c24xx_port_configured(ucon))
2493 		return;
2494 
2495 	uart_console_write(cons_uart, s, count, s3c24xx_serial_console_putchar);
2496 }
2497 
2498 static void __init
2499 s3c24xx_serial_get_options(struct uart_port *port, int *baud,
2500 			   int *parity, int *bits)
2501 {
2502 	struct clk *clk;
2503 	unsigned int ulcon;
2504 	unsigned int ucon;
2505 	unsigned int ubrdiv;
2506 	unsigned long rate;
2507 	unsigned int clk_sel;
2508 	char clk_name[MAX_CLK_NAME_LENGTH];
2509 
2510 	ulcon  = rd_regl(port, S3C2410_ULCON);
2511 	ucon   = rd_regl(port, S3C2410_UCON);
2512 	ubrdiv = rd_regl(port, S3C2410_UBRDIV);
2513 
2514 	if (s3c24xx_port_configured(ucon)) {
2515 		switch (ulcon & S3C2410_LCON_CSMASK) {
2516 		case S3C2410_LCON_CS5:
2517 			*bits = 5;
2518 			break;
2519 		case S3C2410_LCON_CS6:
2520 			*bits = 6;
2521 			break;
2522 		case S3C2410_LCON_CS7:
2523 			*bits = 7;
2524 			break;
2525 		case S3C2410_LCON_CS8:
2526 		default:
2527 			*bits = 8;
2528 			break;
2529 		}
2530 
2531 		switch (ulcon & S3C2410_LCON_PMASK) {
2532 		case S3C2410_LCON_PEVEN:
2533 			*parity = 'e';
2534 			break;
2535 
2536 		case S3C2410_LCON_PODD:
2537 			*parity = 'o';
2538 			break;
2539 
2540 		case S3C2410_LCON_PNONE:
2541 		default:
2542 			*parity = 'n';
2543 		}
2544 
2545 		/* now calculate the baud rate */
2546 
2547 		clk_sel = s3c24xx_serial_getsource(port);
2548 		sprintf(clk_name, "clk_uart_baud%d", clk_sel);
2549 
2550 		clk = clk_get(port->dev, clk_name);
2551 		if (!IS_ERR(clk))
2552 			rate = clk_get_rate(clk);
2553 		else
2554 			rate = 1;
2555 
2556 		*baud = rate / (16 * (ubrdiv + 1));
2557 		dev_dbg(port->dev, "calculated baud %d\n", *baud);
2558 	}
2559 }
2560 
2561 static int __init
2562 s3c24xx_serial_console_setup(struct console *co, char *options)
2563 {
2564 	struct uart_port *port;
2565 	int baud = 9600;
2566 	int bits = 8;
2567 	int parity = 'n';
2568 	int flow = 'n';
2569 
2570 	/* is this a valid port */
2571 
2572 	if (co->index == -1 || co->index >= CONFIG_SERIAL_SAMSUNG_UARTS)
2573 		co->index = 0;
2574 
2575 	port = &s3c24xx_serial_ports[co->index].port;
2576 
2577 	/* is the port configured? */
2578 
2579 	if (port->mapbase == 0x0)
2580 		return -ENODEV;
2581 
2582 	cons_uart = port;
2583 
2584 	/*
2585 	 * Check whether an invalid uart number has been specified, and
2586 	 * if so, search for the first available port that does have
2587 	 * console support.
2588 	 */
2589 	if (options)
2590 		uart_parse_options(options, &baud, &parity, &bits, &flow);
2591 	else
2592 		s3c24xx_serial_get_options(port, &baud, &parity, &bits);
2593 
2594 	dev_dbg(port->dev, "baud %d\n", baud);
2595 
2596 	return uart_set_options(port, co, baud, parity, bits, flow);
2597 }
2598 
2599 static struct console s3c24xx_serial_console = {
2600 	.name		= S3C24XX_SERIAL_NAME,
2601 	.device		= uart_console_device,
2602 	.flags		= CON_PRINTBUFFER,
2603 	.index		= -1,
2604 	.write		= s3c24xx_serial_console_write,
2605 	.setup		= s3c24xx_serial_console_setup,
2606 	.data		= &s3c24xx_uart_drv,
2607 };
2608 #endif /* CONFIG_SERIAL_SAMSUNG_CONSOLE */
2609 
2610 #ifdef CONFIG_CPU_S3C2410
2611 static struct s3c24xx_serial_drv_data s3c2410_serial_drv_data = {
2612 	.info = &(struct s3c24xx_uart_info) {
2613 		.name		= "Samsung S3C2410 UART",
2614 		.type		= TYPE_S3C24XX,
2615 		.port_type	= PORT_S3C2410,
2616 		.fifosize	= 16,
2617 		.rx_fifomask	= S3C2410_UFSTAT_RXMASK,
2618 		.rx_fifoshift	= S3C2410_UFSTAT_RXSHIFT,
2619 		.rx_fifofull	= S3C2410_UFSTAT_RXFULL,
2620 		.tx_fifofull	= S3C2410_UFSTAT_TXFULL,
2621 		.tx_fifomask	= S3C2410_UFSTAT_TXMASK,
2622 		.tx_fifoshift	= S3C2410_UFSTAT_TXSHIFT,
2623 		.def_clk_sel	= S3C2410_UCON_CLKSEL0,
2624 		.num_clks	= 2,
2625 		.clksel_mask	= S3C2410_UCON_CLKMASK,
2626 		.clksel_shift	= S3C2410_UCON_CLKSHIFT,
2627 	},
2628 	.def_cfg = &(struct s3c2410_uartcfg) {
2629 		.ucon		= S3C2410_UCON_DEFAULT,
2630 		.ufcon		= S3C2410_UFCON_DEFAULT,
2631 	},
2632 };
2633 #define S3C2410_SERIAL_DRV_DATA ((kernel_ulong_t)&s3c2410_serial_drv_data)
2634 #else
2635 #define S3C2410_SERIAL_DRV_DATA (kernel_ulong_t)NULL
2636 #endif
2637 
2638 #ifdef CONFIG_CPU_S3C2412
2639 static struct s3c24xx_serial_drv_data s3c2412_serial_drv_data = {
2640 	.info = &(struct s3c24xx_uart_info) {
2641 		.name		= "Samsung S3C2412 UART",
2642 		.type		= TYPE_S3C24XX,
2643 		.port_type	= PORT_S3C2412,
2644 		.fifosize	= 64,
2645 		.has_divslot	= 1,
2646 		.rx_fifomask	= S3C2440_UFSTAT_RXMASK,
2647 		.rx_fifoshift	= S3C2440_UFSTAT_RXSHIFT,
2648 		.rx_fifofull	= S3C2440_UFSTAT_RXFULL,
2649 		.tx_fifofull	= S3C2440_UFSTAT_TXFULL,
2650 		.tx_fifomask	= S3C2440_UFSTAT_TXMASK,
2651 		.tx_fifoshift	= S3C2440_UFSTAT_TXSHIFT,
2652 		.def_clk_sel	= S3C2410_UCON_CLKSEL2,
2653 		.num_clks	= 4,
2654 		.clksel_mask	= S3C2412_UCON_CLKMASK,
2655 		.clksel_shift	= S3C2412_UCON_CLKSHIFT,
2656 	},
2657 	.def_cfg = &(struct s3c2410_uartcfg) {
2658 		.ucon		= S3C2410_UCON_DEFAULT,
2659 		.ufcon		= S3C2410_UFCON_DEFAULT,
2660 	},
2661 };
2662 #define S3C2412_SERIAL_DRV_DATA ((kernel_ulong_t)&s3c2412_serial_drv_data)
2663 #else
2664 #define S3C2412_SERIAL_DRV_DATA (kernel_ulong_t)NULL
2665 #endif
2666 
2667 #if defined(CONFIG_CPU_S3C2440) || defined(CONFIG_CPU_S3C2416) || \
2668 	defined(CONFIG_CPU_S3C2443) || defined(CONFIG_CPU_S3C2442)
2669 static struct s3c24xx_serial_drv_data s3c2440_serial_drv_data = {
2670 	.info = &(struct s3c24xx_uart_info) {
2671 		.name		= "Samsung S3C2440 UART",
2672 		.type		= TYPE_S3C24XX,
2673 		.port_type	= PORT_S3C2440,
2674 		.fifosize	= 64,
2675 		.has_divslot	= 1,
2676 		.rx_fifomask	= S3C2440_UFSTAT_RXMASK,
2677 		.rx_fifoshift	= S3C2440_UFSTAT_RXSHIFT,
2678 		.rx_fifofull	= S3C2440_UFSTAT_RXFULL,
2679 		.tx_fifofull	= S3C2440_UFSTAT_TXFULL,
2680 		.tx_fifomask	= S3C2440_UFSTAT_TXMASK,
2681 		.tx_fifoshift	= S3C2440_UFSTAT_TXSHIFT,
2682 		.def_clk_sel	= S3C2410_UCON_CLKSEL2,
2683 		.num_clks	= 4,
2684 		.clksel_mask	= S3C2412_UCON_CLKMASK,
2685 		.clksel_shift	= S3C2412_UCON_CLKSHIFT,
2686 		.ucon_mask	= S3C2440_UCON0_DIVMASK,
2687 	},
2688 	.def_cfg = &(struct s3c2410_uartcfg) {
2689 		.ucon		= S3C2410_UCON_DEFAULT,
2690 		.ufcon		= S3C2410_UFCON_DEFAULT,
2691 	},
2692 };
2693 #define S3C2440_SERIAL_DRV_DATA ((kernel_ulong_t)&s3c2440_serial_drv_data)
2694 #else
2695 #define S3C2440_SERIAL_DRV_DATA (kernel_ulong_t)NULL
2696 #endif
2697 
2698 #if defined(CONFIG_CPU_S3C6400) || defined(CONFIG_CPU_S3C6410)
2699 static struct s3c24xx_serial_drv_data s3c6400_serial_drv_data = {
2700 	.info = &(struct s3c24xx_uart_info) {
2701 		.name		= "Samsung S3C6400 UART",
2702 		.type		= TYPE_S3C6400,
2703 		.port_type	= PORT_S3C6400,
2704 		.fifosize	= 64,
2705 		.has_divslot	= 1,
2706 		.rx_fifomask	= S3C2440_UFSTAT_RXMASK,
2707 		.rx_fifoshift	= S3C2440_UFSTAT_RXSHIFT,
2708 		.rx_fifofull	= S3C2440_UFSTAT_RXFULL,
2709 		.tx_fifofull	= S3C2440_UFSTAT_TXFULL,
2710 		.tx_fifomask	= S3C2440_UFSTAT_TXMASK,
2711 		.tx_fifoshift	= S3C2440_UFSTAT_TXSHIFT,
2712 		.def_clk_sel	= S3C2410_UCON_CLKSEL2,
2713 		.num_clks	= 4,
2714 		.clksel_mask	= S3C6400_UCON_CLKMASK,
2715 		.clksel_shift	= S3C6400_UCON_CLKSHIFT,
2716 	},
2717 	.def_cfg = &(struct s3c2410_uartcfg) {
2718 		.ucon		= S3C2410_UCON_DEFAULT,
2719 		.ufcon		= S3C2410_UFCON_DEFAULT,
2720 	},
2721 };
2722 #define S3C6400_SERIAL_DRV_DATA ((kernel_ulong_t)&s3c6400_serial_drv_data)
2723 #else
2724 #define S3C6400_SERIAL_DRV_DATA (kernel_ulong_t)NULL
2725 #endif
2726 
2727 #ifdef CONFIG_CPU_S5PV210
2728 static struct s3c24xx_serial_drv_data s5pv210_serial_drv_data = {
2729 	.info = &(struct s3c24xx_uart_info) {
2730 		.name		= "Samsung S5PV210 UART",
2731 		.type		= TYPE_S3C6400,
2732 		.port_type	= PORT_S3C6400,
2733 		.has_divslot	= 1,
2734 		.rx_fifomask	= S5PV210_UFSTAT_RXMASK,
2735 		.rx_fifoshift	= S5PV210_UFSTAT_RXSHIFT,
2736 		.rx_fifofull	= S5PV210_UFSTAT_RXFULL,
2737 		.tx_fifofull	= S5PV210_UFSTAT_TXFULL,
2738 		.tx_fifomask	= S5PV210_UFSTAT_TXMASK,
2739 		.tx_fifoshift	= S5PV210_UFSTAT_TXSHIFT,
2740 		.def_clk_sel	= S3C2410_UCON_CLKSEL0,
2741 		.num_clks	= 2,
2742 		.clksel_mask	= S5PV210_UCON_CLKMASK,
2743 		.clksel_shift	= S5PV210_UCON_CLKSHIFT,
2744 	},
2745 	.def_cfg = &(struct s3c2410_uartcfg) {
2746 		.ucon		= S5PV210_UCON_DEFAULT,
2747 		.ufcon		= S5PV210_UFCON_DEFAULT,
2748 	},
2749 	.fifosize = { 256, 64, 16, 16 },
2750 };
2751 #define S5PV210_SERIAL_DRV_DATA ((kernel_ulong_t)&s5pv210_serial_drv_data)
2752 #else
2753 #define S5PV210_SERIAL_DRV_DATA	(kernel_ulong_t)NULL
2754 #endif
2755 
2756 #if defined(CONFIG_ARCH_EXYNOS)
2757 #define EXYNOS_COMMON_SERIAL_DRV_DATA				\
2758 	.info = &(struct s3c24xx_uart_info) {			\
2759 		.name		= "Samsung Exynos UART",	\
2760 		.type		= TYPE_S3C6400,			\
2761 		.port_type	= PORT_S3C6400,			\
2762 		.has_divslot	= 1,				\
2763 		.rx_fifomask	= S5PV210_UFSTAT_RXMASK,	\
2764 		.rx_fifoshift	= S5PV210_UFSTAT_RXSHIFT,	\
2765 		.rx_fifofull	= S5PV210_UFSTAT_RXFULL,	\
2766 		.tx_fifofull	= S5PV210_UFSTAT_TXFULL,	\
2767 		.tx_fifomask	= S5PV210_UFSTAT_TXMASK,	\
2768 		.tx_fifoshift	= S5PV210_UFSTAT_TXSHIFT,	\
2769 		.def_clk_sel	= S3C2410_UCON_CLKSEL0,		\
2770 		.num_clks	= 1,				\
2771 		.clksel_mask	= 0,				\
2772 		.clksel_shift	= 0,				\
2773 	},							\
2774 	.def_cfg = &(struct s3c2410_uartcfg) {			\
2775 		.ucon		= S5PV210_UCON_DEFAULT,		\
2776 		.ufcon		= S5PV210_UFCON_DEFAULT,	\
2777 		.has_fracval	= 1,				\
2778 	}							\
2779 
2780 static struct s3c24xx_serial_drv_data exynos4210_serial_drv_data = {
2781 	EXYNOS_COMMON_SERIAL_DRV_DATA,
2782 	.fifosize = { 256, 64, 16, 16 },
2783 };
2784 
2785 static struct s3c24xx_serial_drv_data exynos5433_serial_drv_data = {
2786 	EXYNOS_COMMON_SERIAL_DRV_DATA,
2787 	.fifosize = { 64, 256, 16, 256 },
2788 };
2789 
2790 #define EXYNOS4210_SERIAL_DRV_DATA ((kernel_ulong_t)&exynos4210_serial_drv_data)
2791 #define EXYNOS5433_SERIAL_DRV_DATA ((kernel_ulong_t)&exynos5433_serial_drv_data)
2792 #else
2793 #define EXYNOS4210_SERIAL_DRV_DATA (kernel_ulong_t)NULL
2794 #define EXYNOS5433_SERIAL_DRV_DATA (kernel_ulong_t)NULL
2795 #endif
2796 
2797 #ifdef CONFIG_ARCH_APPLE
2798 static struct s3c24xx_serial_drv_data s5l_serial_drv_data = {
2799 	.info = &(struct s3c24xx_uart_info) {
2800 		.name		= "Apple S5L UART",
2801 		.type		= TYPE_APPLE_S5L,
2802 		.port_type	= PORT_8250,
2803 		.fifosize	= 16,
2804 		.rx_fifomask	= S3C2410_UFSTAT_RXMASK,
2805 		.rx_fifoshift	= S3C2410_UFSTAT_RXSHIFT,
2806 		.rx_fifofull	= S3C2410_UFSTAT_RXFULL,
2807 		.tx_fifofull	= S3C2410_UFSTAT_TXFULL,
2808 		.tx_fifomask	= S3C2410_UFSTAT_TXMASK,
2809 		.tx_fifoshift	= S3C2410_UFSTAT_TXSHIFT,
2810 		.def_clk_sel	= S3C2410_UCON_CLKSEL0,
2811 		.num_clks	= 1,
2812 		.clksel_mask	= 0,
2813 		.clksel_shift	= 0,
2814 	},
2815 	.def_cfg = &(struct s3c2410_uartcfg) {
2816 		.ucon		= APPLE_S5L_UCON_DEFAULT,
2817 		.ufcon		= S3C2410_UFCON_DEFAULT,
2818 	},
2819 };
2820 #define S5L_SERIAL_DRV_DATA ((kernel_ulong_t)&s5l_serial_drv_data)
2821 #else
2822 #define S5L_SERIAL_DRV_DATA ((kernel_ulong_t)NULL)
2823 #endif
2824 
2825 static const struct platform_device_id s3c24xx_serial_driver_ids[] = {
2826 	{
2827 		.name		= "s3c2410-uart",
2828 		.driver_data	= S3C2410_SERIAL_DRV_DATA,
2829 	}, {
2830 		.name		= "s3c2412-uart",
2831 		.driver_data	= S3C2412_SERIAL_DRV_DATA,
2832 	}, {
2833 		.name		= "s3c2440-uart",
2834 		.driver_data	= S3C2440_SERIAL_DRV_DATA,
2835 	}, {
2836 		.name		= "s3c6400-uart",
2837 		.driver_data	= S3C6400_SERIAL_DRV_DATA,
2838 	}, {
2839 		.name		= "s5pv210-uart",
2840 		.driver_data	= S5PV210_SERIAL_DRV_DATA,
2841 	}, {
2842 		.name		= "exynos4210-uart",
2843 		.driver_data	= EXYNOS4210_SERIAL_DRV_DATA,
2844 	}, {
2845 		.name		= "exynos5433-uart",
2846 		.driver_data	= EXYNOS5433_SERIAL_DRV_DATA,
2847 	}, {
2848 		.name		= "s5l-uart",
2849 		.driver_data	= S5L_SERIAL_DRV_DATA,
2850 	},
2851 	{ },
2852 };
2853 MODULE_DEVICE_TABLE(platform, s3c24xx_serial_driver_ids);
2854 
2855 #ifdef CONFIG_OF
2856 static const struct of_device_id s3c24xx_uart_dt_match[] = {
2857 	{ .compatible = "samsung,s3c2410-uart",
2858 		.data = (void *)S3C2410_SERIAL_DRV_DATA },
2859 	{ .compatible = "samsung,s3c2412-uart",
2860 		.data = (void *)S3C2412_SERIAL_DRV_DATA },
2861 	{ .compatible = "samsung,s3c2440-uart",
2862 		.data = (void *)S3C2440_SERIAL_DRV_DATA },
2863 	{ .compatible = "samsung,s3c6400-uart",
2864 		.data = (void *)S3C6400_SERIAL_DRV_DATA },
2865 	{ .compatible = "samsung,s5pv210-uart",
2866 		.data = (void *)S5PV210_SERIAL_DRV_DATA },
2867 	{ .compatible = "samsung,exynos4210-uart",
2868 		.data = (void *)EXYNOS4210_SERIAL_DRV_DATA },
2869 	{ .compatible = "samsung,exynos5433-uart",
2870 		.data = (void *)EXYNOS5433_SERIAL_DRV_DATA },
2871 	{ .compatible = "apple,s5l-uart",
2872 		.data = (void *)S5L_SERIAL_DRV_DATA },
2873 	{},
2874 };
2875 MODULE_DEVICE_TABLE(of, s3c24xx_uart_dt_match);
2876 #endif
2877 
2878 static struct platform_driver samsung_serial_driver = {
2879 	.probe		= s3c24xx_serial_probe,
2880 	.remove		= s3c24xx_serial_remove,
2881 	.id_table	= s3c24xx_serial_driver_ids,
2882 	.driver		= {
2883 		.name	= "samsung-uart",
2884 		.pm	= SERIAL_SAMSUNG_PM_OPS,
2885 		.of_match_table	= of_match_ptr(s3c24xx_uart_dt_match),
2886 	},
2887 };
2888 
2889 module_platform_driver(samsung_serial_driver);
2890 
2891 #ifdef CONFIG_SERIAL_SAMSUNG_CONSOLE
2892 /*
2893  * Early console.
2894  */
2895 
2896 static void wr_reg_barrier(struct uart_port *port, u32 reg, u32 val)
2897 {
2898 	switch (port->iotype) {
2899 	case UPIO_MEM:
2900 		writeb(val, portaddr(port, reg));
2901 		break;
2902 	case UPIO_MEM32:
2903 		writel(val, portaddr(port, reg));
2904 		break;
2905 	}
2906 }
2907 
2908 struct samsung_early_console_data {
2909 	u32 txfull_mask;
2910 };
2911 
2912 static void samsung_early_busyuart(struct uart_port *port)
2913 {
2914 	while (!(readl(port->membase + S3C2410_UTRSTAT) & S3C2410_UTRSTAT_TXFE))
2915 		;
2916 }
2917 
2918 static void samsung_early_busyuart_fifo(struct uart_port *port)
2919 {
2920 	struct samsung_early_console_data *data = port->private_data;
2921 
2922 	while (readl(port->membase + S3C2410_UFSTAT) & data->txfull_mask)
2923 		;
2924 }
2925 
2926 static void samsung_early_putc(struct uart_port *port, int c)
2927 {
2928 	if (readl(port->membase + S3C2410_UFCON) & S3C2410_UFCON_FIFOMODE)
2929 		samsung_early_busyuart_fifo(port);
2930 	else
2931 		samsung_early_busyuart(port);
2932 
2933 	wr_reg_barrier(port, S3C2410_UTXH, c);
2934 }
2935 
2936 static void samsung_early_write(struct console *con, const char *s,
2937 				unsigned int n)
2938 {
2939 	struct earlycon_device *dev = con->data;
2940 
2941 	uart_console_write(&dev->port, s, n, samsung_early_putc);
2942 }
2943 
2944 static int __init samsung_early_console_setup(struct earlycon_device *device,
2945 					      const char *opt)
2946 {
2947 	if (!device->port.membase)
2948 		return -ENODEV;
2949 
2950 	device->con->write = samsung_early_write;
2951 	return 0;
2952 }
2953 
2954 /* S3C2410 */
2955 static struct samsung_early_console_data s3c2410_early_console_data = {
2956 	.txfull_mask = S3C2410_UFSTAT_TXFULL,
2957 };
2958 
2959 static int __init s3c2410_early_console_setup(struct earlycon_device *device,
2960 					      const char *opt)
2961 {
2962 	device->port.private_data = &s3c2410_early_console_data;
2963 	return samsung_early_console_setup(device, opt);
2964 }
2965 
2966 OF_EARLYCON_DECLARE(s3c2410, "samsung,s3c2410-uart",
2967 			s3c2410_early_console_setup);
2968 
2969 /* S3C2412, S3C2440, S3C64xx */
2970 static struct samsung_early_console_data s3c2440_early_console_data = {
2971 	.txfull_mask = S3C2440_UFSTAT_TXFULL,
2972 };
2973 
2974 static int __init s3c2440_early_console_setup(struct earlycon_device *device,
2975 					      const char *opt)
2976 {
2977 	device->port.private_data = &s3c2440_early_console_data;
2978 	return samsung_early_console_setup(device, opt);
2979 }
2980 
2981 OF_EARLYCON_DECLARE(s3c2412, "samsung,s3c2412-uart",
2982 			s3c2440_early_console_setup);
2983 OF_EARLYCON_DECLARE(s3c2440, "samsung,s3c2440-uart",
2984 			s3c2440_early_console_setup);
2985 OF_EARLYCON_DECLARE(s3c6400, "samsung,s3c6400-uart",
2986 			s3c2440_early_console_setup);
2987 
2988 /* S5PV210, Exynos */
2989 static struct samsung_early_console_data s5pv210_early_console_data = {
2990 	.txfull_mask = S5PV210_UFSTAT_TXFULL,
2991 };
2992 
2993 static int __init s5pv210_early_console_setup(struct earlycon_device *device,
2994 					      const char *opt)
2995 {
2996 	device->port.private_data = &s5pv210_early_console_data;
2997 	return samsung_early_console_setup(device, opt);
2998 }
2999 
3000 OF_EARLYCON_DECLARE(s5pv210, "samsung,s5pv210-uart",
3001 			s5pv210_early_console_setup);
3002 OF_EARLYCON_DECLARE(exynos4210, "samsung,exynos4210-uart",
3003 			s5pv210_early_console_setup);
3004 
3005 /* Apple S5L */
3006 static int __init apple_s5l_early_console_setup(struct earlycon_device *device,
3007 						const char *opt)
3008 {
3009 	/* Close enough to S3C2410 for earlycon... */
3010 	device->port.private_data = &s3c2410_early_console_data;
3011 
3012 #ifdef CONFIG_ARM64
3013 	/* ... but we need to override the existing fixmap entry as nGnRnE */
3014 	__set_fixmap(FIX_EARLYCON_MEM_BASE, device->port.mapbase,
3015 		     __pgprot(PROT_DEVICE_nGnRnE));
3016 #endif
3017 	return samsung_early_console_setup(device, opt);
3018 }
3019 
3020 OF_EARLYCON_DECLARE(s5l, "apple,s5l-uart", apple_s5l_early_console_setup);
3021 #endif
3022 
3023 MODULE_ALIAS("platform:samsung-uart");
3024 MODULE_DESCRIPTION("Samsung SoC Serial port driver");
3025 MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
3026 MODULE_LICENSE("GPL v2");
3027