xref: /openbmc/linux/drivers/tty/serial/samsung_tty.c (revision 08b7cf13)
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 	const 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 	const struct s3c24xx_uart_info	info;
89 	const struct s3c2410_uartcfg	def_cfg;
90 	const 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 	const struct s3c24xx_uart_info	*info;
140 	struct clk			*clk;
141 	struct clk			*baudclk;
142 	struct uart_port		port;
143 	const struct s3c24xx_serial_drv_data	*drv_data;
144 
145 	/* reference to platform data */
146 	const 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(const 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(const 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(const 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(const 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(const struct uart_port *port)
232 {
233 	return to_platform_device(port->dev)->name;
234 }
235 
236 static int s3c24xx_serial_txempty_nofifo(const 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 	const 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 	const 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 const 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 const struct s3c2410_uartcfg
583 	*s3c24xx_port_to_cfg(const struct uart_port *port)
584 {
585 	const 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(const struct s3c24xx_uart_port *ourport,
595 				     unsigned long ufstat)
596 {
597 	const 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 		uart_write_wakeup(port);
926 
927 	if (uart_circ_empty(xmit))
928 		s3c24xx_serial_stop_tx(port);
929 }
930 
931 static irqreturn_t s3c24xx_serial_tx_irq(int irq, void *id)
932 {
933 	struct s3c24xx_uart_port *ourport = id;
934 	struct uart_port *port = &ourport->port;
935 
936 	spin_lock(&port->lock);
937 
938 	s3c24xx_serial_tx_chars(ourport);
939 
940 	spin_unlock(&port->lock);
941 	return IRQ_HANDLED;
942 }
943 
944 /* interrupt handler for s3c64xx and later SoC's.*/
945 static irqreturn_t s3c64xx_serial_handle_irq(int irq, void *id)
946 {
947 	const struct s3c24xx_uart_port *ourport = id;
948 	const struct uart_port *port = &ourport->port;
949 	unsigned int pend = rd_regl(port, S3C64XX_UINTP);
950 	irqreturn_t ret = IRQ_HANDLED;
951 
952 	if (pend & S3C64XX_UINTM_RXD_MSK) {
953 		ret = s3c24xx_serial_rx_irq(irq, id);
954 		wr_regl(port, S3C64XX_UINTP, S3C64XX_UINTM_RXD_MSK);
955 	}
956 	if (pend & S3C64XX_UINTM_TXD_MSK) {
957 		ret = s3c24xx_serial_tx_irq(irq, id);
958 		wr_regl(port, S3C64XX_UINTP, S3C64XX_UINTM_TXD_MSK);
959 	}
960 	return ret;
961 }
962 
963 /* interrupt handler for Apple SoC's.*/
964 static irqreturn_t apple_serial_handle_irq(int irq, void *id)
965 {
966 	const struct s3c24xx_uart_port *ourport = id;
967 	const struct uart_port *port = &ourport->port;
968 	unsigned int pend = rd_regl(port, S3C2410_UTRSTAT);
969 	irqreturn_t ret = IRQ_NONE;
970 
971 	if (pend & (APPLE_S5L_UTRSTAT_RXTHRESH | APPLE_S5L_UTRSTAT_RXTO)) {
972 		wr_regl(port, S3C2410_UTRSTAT,
973 			APPLE_S5L_UTRSTAT_RXTHRESH | APPLE_S5L_UTRSTAT_RXTO);
974 		ret = s3c24xx_serial_rx_irq(irq, id);
975 	}
976 	if (pend & APPLE_S5L_UTRSTAT_TXTHRESH) {
977 		wr_regl(port, S3C2410_UTRSTAT, APPLE_S5L_UTRSTAT_TXTHRESH);
978 		ret = s3c24xx_serial_tx_irq(irq, id);
979 	}
980 
981 	return ret;
982 }
983 
984 static unsigned int s3c24xx_serial_tx_empty(struct uart_port *port)
985 {
986 	const struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
987 	unsigned long ufstat = rd_regl(port, S3C2410_UFSTAT);
988 	unsigned long ufcon = rd_regl(port, S3C2410_UFCON);
989 
990 	if (ufcon & S3C2410_UFCON_FIFOMODE) {
991 		if ((ufstat & info->tx_fifomask) != 0 ||
992 		    (ufstat & info->tx_fifofull))
993 			return 0;
994 
995 		return 1;
996 	}
997 
998 	return s3c24xx_serial_txempty_nofifo(port);
999 }
1000 
1001 /* no modem control lines */
1002 static unsigned int s3c24xx_serial_get_mctrl(struct uart_port *port)
1003 {
1004 	unsigned int umstat = rd_reg(port, S3C2410_UMSTAT);
1005 
1006 	if (umstat & S3C2410_UMSTAT_CTS)
1007 		return TIOCM_CAR | TIOCM_DSR | TIOCM_CTS;
1008 	else
1009 		return TIOCM_CAR | TIOCM_DSR;
1010 }
1011 
1012 static void s3c24xx_serial_set_mctrl(struct uart_port *port, unsigned int mctrl)
1013 {
1014 	unsigned int umcon = rd_regl(port, S3C2410_UMCON);
1015 
1016 	if (mctrl & TIOCM_RTS)
1017 		umcon |= S3C2410_UMCOM_RTS_LOW;
1018 	else
1019 		umcon &= ~S3C2410_UMCOM_RTS_LOW;
1020 
1021 	wr_regl(port, S3C2410_UMCON, umcon);
1022 }
1023 
1024 static void s3c24xx_serial_break_ctl(struct uart_port *port, int break_state)
1025 {
1026 	unsigned long flags;
1027 	unsigned int ucon;
1028 
1029 	spin_lock_irqsave(&port->lock, flags);
1030 
1031 	ucon = rd_regl(port, S3C2410_UCON);
1032 
1033 	if (break_state)
1034 		ucon |= S3C2410_UCON_SBREAK;
1035 	else
1036 		ucon &= ~S3C2410_UCON_SBREAK;
1037 
1038 	wr_regl(port, S3C2410_UCON, ucon);
1039 
1040 	spin_unlock_irqrestore(&port->lock, flags);
1041 }
1042 
1043 static int s3c24xx_serial_request_dma(struct s3c24xx_uart_port *p)
1044 {
1045 	struct s3c24xx_uart_dma	*dma = p->dma;
1046 	struct dma_slave_caps dma_caps;
1047 	const char *reason = NULL;
1048 	int ret;
1049 
1050 	/* Default slave configuration parameters */
1051 	dma->rx_conf.direction		= DMA_DEV_TO_MEM;
1052 	dma->rx_conf.src_addr_width	= DMA_SLAVE_BUSWIDTH_1_BYTE;
1053 	dma->rx_conf.src_addr		= p->port.mapbase + S3C2410_URXH;
1054 	dma->rx_conf.src_maxburst	= 1;
1055 
1056 	dma->tx_conf.direction		= DMA_MEM_TO_DEV;
1057 	dma->tx_conf.dst_addr_width	= DMA_SLAVE_BUSWIDTH_1_BYTE;
1058 	dma->tx_conf.dst_addr		= p->port.mapbase + S3C2410_UTXH;
1059 	dma->tx_conf.dst_maxburst	= 1;
1060 
1061 	dma->rx_chan = dma_request_chan(p->port.dev, "rx");
1062 
1063 	if (IS_ERR(dma->rx_chan)) {
1064 		reason = "DMA RX channel request failed";
1065 		ret = PTR_ERR(dma->rx_chan);
1066 		goto err_warn;
1067 	}
1068 
1069 	ret = dma_get_slave_caps(dma->rx_chan, &dma_caps);
1070 	if (ret < 0 ||
1071 	    dma_caps.residue_granularity < DMA_RESIDUE_GRANULARITY_BURST) {
1072 		reason = "insufficient DMA RX engine capabilities";
1073 		ret = -EOPNOTSUPP;
1074 		goto err_release_rx;
1075 	}
1076 
1077 	dmaengine_slave_config(dma->rx_chan, &dma->rx_conf);
1078 
1079 	dma->tx_chan = dma_request_chan(p->port.dev, "tx");
1080 	if (IS_ERR(dma->tx_chan)) {
1081 		reason = "DMA TX channel request failed";
1082 		ret = PTR_ERR(dma->tx_chan);
1083 		goto err_release_rx;
1084 	}
1085 
1086 	ret = dma_get_slave_caps(dma->tx_chan, &dma_caps);
1087 	if (ret < 0 ||
1088 	    dma_caps.residue_granularity < DMA_RESIDUE_GRANULARITY_BURST) {
1089 		reason = "insufficient DMA TX engine capabilities";
1090 		ret = -EOPNOTSUPP;
1091 		goto err_release_tx;
1092 	}
1093 
1094 	dmaengine_slave_config(dma->tx_chan, &dma->tx_conf);
1095 
1096 	/* RX buffer */
1097 	dma->rx_size = PAGE_SIZE;
1098 
1099 	dma->rx_buf = kmalloc(dma->rx_size, GFP_KERNEL);
1100 	if (!dma->rx_buf) {
1101 		ret = -ENOMEM;
1102 		goto err_release_tx;
1103 	}
1104 
1105 	dma->rx_addr = dma_map_single(dma->rx_chan->device->dev, dma->rx_buf,
1106 				      dma->rx_size, DMA_FROM_DEVICE);
1107 	if (dma_mapping_error(dma->rx_chan->device->dev, dma->rx_addr)) {
1108 		reason = "DMA mapping error for RX buffer";
1109 		ret = -EIO;
1110 		goto err_free_rx;
1111 	}
1112 
1113 	/* TX buffer */
1114 	dma->tx_addr = dma_map_single(dma->tx_chan->device->dev,
1115 				      p->port.state->xmit.buf, UART_XMIT_SIZE,
1116 				      DMA_TO_DEVICE);
1117 	if (dma_mapping_error(dma->tx_chan->device->dev, dma->tx_addr)) {
1118 		reason = "DMA mapping error for TX buffer";
1119 		ret = -EIO;
1120 		goto err_unmap_rx;
1121 	}
1122 
1123 	return 0;
1124 
1125 err_unmap_rx:
1126 	dma_unmap_single(dma->rx_chan->device->dev, dma->rx_addr,
1127 			 dma->rx_size, DMA_FROM_DEVICE);
1128 err_free_rx:
1129 	kfree(dma->rx_buf);
1130 err_release_tx:
1131 	dma_release_channel(dma->tx_chan);
1132 err_release_rx:
1133 	dma_release_channel(dma->rx_chan);
1134 err_warn:
1135 	if (reason)
1136 		dev_warn(p->port.dev, "%s, DMA will not be used\n", reason);
1137 	return ret;
1138 }
1139 
1140 static void s3c24xx_serial_release_dma(struct s3c24xx_uart_port *p)
1141 {
1142 	struct s3c24xx_uart_dma	*dma = p->dma;
1143 
1144 	if (dma->rx_chan) {
1145 		dmaengine_terminate_all(dma->rx_chan);
1146 		dma_unmap_single(dma->rx_chan->device->dev, dma->rx_addr,
1147 				 dma->rx_size, DMA_FROM_DEVICE);
1148 		kfree(dma->rx_buf);
1149 		dma_release_channel(dma->rx_chan);
1150 		dma->rx_chan = NULL;
1151 	}
1152 
1153 	if (dma->tx_chan) {
1154 		dmaengine_terminate_all(dma->tx_chan);
1155 		dma_unmap_single(dma->tx_chan->device->dev, dma->tx_addr,
1156 				 UART_XMIT_SIZE, DMA_TO_DEVICE);
1157 		dma_release_channel(dma->tx_chan);
1158 		dma->tx_chan = NULL;
1159 	}
1160 }
1161 
1162 static void s3c24xx_serial_shutdown(struct uart_port *port)
1163 {
1164 	struct s3c24xx_uart_port *ourport = to_ourport(port);
1165 
1166 	if (ourport->tx_claimed) {
1167 		free_irq(ourport->tx_irq, ourport);
1168 		ourport->tx_enabled = 0;
1169 		ourport->tx_claimed = 0;
1170 		ourport->tx_mode = 0;
1171 	}
1172 
1173 	if (ourport->rx_claimed) {
1174 		free_irq(ourport->rx_irq, ourport);
1175 		ourport->rx_claimed = 0;
1176 		ourport->rx_enabled = 0;
1177 	}
1178 
1179 	if (ourport->dma)
1180 		s3c24xx_serial_release_dma(ourport);
1181 
1182 	ourport->tx_in_progress = 0;
1183 }
1184 
1185 static void s3c64xx_serial_shutdown(struct uart_port *port)
1186 {
1187 	struct s3c24xx_uart_port *ourport = to_ourport(port);
1188 
1189 	ourport->tx_enabled = 0;
1190 	ourport->tx_mode = 0;
1191 	ourport->rx_enabled = 0;
1192 
1193 	free_irq(port->irq, ourport);
1194 
1195 	wr_regl(port, S3C64XX_UINTP, 0xf);
1196 	wr_regl(port, S3C64XX_UINTM, 0xf);
1197 
1198 	if (ourport->dma)
1199 		s3c24xx_serial_release_dma(ourport);
1200 
1201 	ourport->tx_in_progress = 0;
1202 }
1203 
1204 static void apple_s5l_serial_shutdown(struct uart_port *port)
1205 {
1206 	struct s3c24xx_uart_port *ourport = to_ourport(port);
1207 
1208 	unsigned int ucon;
1209 
1210 	ucon = rd_regl(port, S3C2410_UCON);
1211 	ucon &= ~(APPLE_S5L_UCON_TXTHRESH_ENA_MSK |
1212 		  APPLE_S5L_UCON_RXTHRESH_ENA_MSK |
1213 		  APPLE_S5L_UCON_RXTO_ENA_MSK);
1214 	wr_regl(port, S3C2410_UCON, ucon);
1215 
1216 	wr_regl(port, S3C2410_UTRSTAT, APPLE_S5L_UTRSTAT_ALL_FLAGS);
1217 
1218 	free_irq(port->irq, ourport);
1219 
1220 	ourport->tx_enabled = 0;
1221 	ourport->tx_mode = 0;
1222 	ourport->rx_enabled = 0;
1223 
1224 	if (ourport->dma)
1225 		s3c24xx_serial_release_dma(ourport);
1226 
1227 	ourport->tx_in_progress = 0;
1228 }
1229 
1230 static int s3c24xx_serial_startup(struct uart_port *port)
1231 {
1232 	struct s3c24xx_uart_port *ourport = to_ourport(port);
1233 	int ret;
1234 
1235 	ourport->rx_enabled = 1;
1236 
1237 	ret = request_irq(ourport->rx_irq, s3c24xx_serial_rx_irq, 0,
1238 			  s3c24xx_serial_portname(port), ourport);
1239 
1240 	if (ret != 0) {
1241 		dev_err(port->dev, "cannot get irq %d\n", ourport->rx_irq);
1242 		return ret;
1243 	}
1244 
1245 	ourport->rx_claimed = 1;
1246 
1247 	dev_dbg(port->dev, "requesting tx irq...\n");
1248 
1249 	ourport->tx_enabled = 1;
1250 
1251 	ret = request_irq(ourport->tx_irq, s3c24xx_serial_tx_irq, 0,
1252 			  s3c24xx_serial_portname(port), ourport);
1253 
1254 	if (ret) {
1255 		dev_err(port->dev, "cannot get irq %d\n", ourport->tx_irq);
1256 		goto err;
1257 	}
1258 
1259 	ourport->tx_claimed = 1;
1260 
1261 	/* the port reset code should have done the correct
1262 	 * register setup for the port controls
1263 	 */
1264 
1265 	return ret;
1266 
1267 err:
1268 	s3c24xx_serial_shutdown(port);
1269 	return ret;
1270 }
1271 
1272 static int s3c64xx_serial_startup(struct uart_port *port)
1273 {
1274 	struct s3c24xx_uart_port *ourport = to_ourport(port);
1275 	unsigned long flags;
1276 	unsigned int ufcon;
1277 	int ret;
1278 
1279 	wr_regl(port, S3C64XX_UINTM, 0xf);
1280 	if (ourport->dma) {
1281 		ret = s3c24xx_serial_request_dma(ourport);
1282 		if (ret < 0) {
1283 			devm_kfree(port->dev, ourport->dma);
1284 			ourport->dma = NULL;
1285 		}
1286 	}
1287 
1288 	ret = request_irq(port->irq, s3c64xx_serial_handle_irq, IRQF_SHARED,
1289 			  s3c24xx_serial_portname(port), ourport);
1290 	if (ret) {
1291 		dev_err(port->dev, "cannot get irq %d\n", port->irq);
1292 		return ret;
1293 	}
1294 
1295 	/* For compatibility with s3c24xx Soc's */
1296 	ourport->rx_enabled = 1;
1297 	ourport->tx_enabled = 0;
1298 
1299 	spin_lock_irqsave(&port->lock, flags);
1300 
1301 	ufcon = rd_regl(port, S3C2410_UFCON);
1302 	ufcon |= S3C2410_UFCON_RESETRX | S5PV210_UFCON_RXTRIG8;
1303 	if (!uart_console(port))
1304 		ufcon |= S3C2410_UFCON_RESETTX;
1305 	wr_regl(port, S3C2410_UFCON, ufcon);
1306 
1307 	enable_rx_pio(ourport);
1308 
1309 	spin_unlock_irqrestore(&port->lock, flags);
1310 
1311 	/* Enable Rx Interrupt */
1312 	s3c24xx_clear_bit(port, S3C64XX_UINTM_RXD, S3C64XX_UINTM);
1313 
1314 	return ret;
1315 }
1316 
1317 static int apple_s5l_serial_startup(struct uart_port *port)
1318 {
1319 	struct s3c24xx_uart_port *ourport = to_ourport(port);
1320 	unsigned long flags;
1321 	unsigned int ufcon;
1322 	int ret;
1323 
1324 	wr_regl(port, S3C2410_UTRSTAT, APPLE_S5L_UTRSTAT_ALL_FLAGS);
1325 
1326 	ret = request_irq(port->irq, apple_serial_handle_irq, 0,
1327 			  s3c24xx_serial_portname(port), ourport);
1328 	if (ret) {
1329 		dev_err(port->dev, "cannot get irq %d\n", port->irq);
1330 		return ret;
1331 	}
1332 
1333 	/* For compatibility with s3c24xx Soc's */
1334 	ourport->rx_enabled = 1;
1335 	ourport->tx_enabled = 0;
1336 
1337 	spin_lock_irqsave(&port->lock, flags);
1338 
1339 	ufcon = rd_regl(port, S3C2410_UFCON);
1340 	ufcon |= S3C2410_UFCON_RESETRX | S5PV210_UFCON_RXTRIG8;
1341 	if (!uart_console(port))
1342 		ufcon |= S3C2410_UFCON_RESETTX;
1343 	wr_regl(port, S3C2410_UFCON, ufcon);
1344 
1345 	enable_rx_pio(ourport);
1346 
1347 	spin_unlock_irqrestore(&port->lock, flags);
1348 
1349 	/* Enable Rx Interrupt */
1350 	s3c24xx_set_bit(port, APPLE_S5L_UCON_RXTHRESH_ENA, S3C2410_UCON);
1351 	s3c24xx_set_bit(port, APPLE_S5L_UCON_RXTO_ENA, S3C2410_UCON);
1352 
1353 	return ret;
1354 }
1355 
1356 /* power power management control */
1357 
1358 static void s3c24xx_serial_pm(struct uart_port *port, unsigned int level,
1359 			      unsigned int old)
1360 {
1361 	struct s3c24xx_uart_port *ourport = to_ourport(port);
1362 	int timeout = 10000;
1363 
1364 	ourport->pm_level = level;
1365 
1366 	switch (level) {
1367 	case 3:
1368 		while (--timeout && !s3c24xx_serial_txempty_nofifo(port))
1369 			udelay(100);
1370 
1371 		if (!IS_ERR(ourport->baudclk))
1372 			clk_disable_unprepare(ourport->baudclk);
1373 
1374 		clk_disable_unprepare(ourport->clk);
1375 		break;
1376 
1377 	case 0:
1378 		clk_prepare_enable(ourport->clk);
1379 
1380 		if (!IS_ERR(ourport->baudclk))
1381 			clk_prepare_enable(ourport->baudclk);
1382 		break;
1383 	default:
1384 		dev_err(port->dev, "s3c24xx_serial: unknown pm %d\n", level);
1385 	}
1386 }
1387 
1388 /* baud rate calculation
1389  *
1390  * The UARTs on the S3C2410/S3C2440 can take their clocks from a number
1391  * of different sources, including the peripheral clock ("pclk") and an
1392  * external clock ("uclk"). The S3C2440 also adds the core clock ("fclk")
1393  * with a programmable extra divisor.
1394  *
1395  * The following code goes through the clock sources, and calculates the
1396  * baud clocks (and the resultant actual baud rates) and then tries to
1397  * pick the closest one and select that.
1398  *
1399  */
1400 
1401 #define MAX_CLK_NAME_LENGTH 15
1402 
1403 static inline int s3c24xx_serial_getsource(struct uart_port *port)
1404 {
1405 	const struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
1406 	unsigned int ucon;
1407 
1408 	if (info->num_clks == 1)
1409 		return 0;
1410 
1411 	ucon = rd_regl(port, S3C2410_UCON);
1412 	ucon &= info->clksel_mask;
1413 	return ucon >> info->clksel_shift;
1414 }
1415 
1416 static void s3c24xx_serial_setsource(struct uart_port *port,
1417 			unsigned int clk_sel)
1418 {
1419 	const struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
1420 	unsigned int ucon;
1421 
1422 	if (info->num_clks == 1)
1423 		return;
1424 
1425 	ucon = rd_regl(port, S3C2410_UCON);
1426 	if ((ucon & info->clksel_mask) >> info->clksel_shift == clk_sel)
1427 		return;
1428 
1429 	ucon &= ~info->clksel_mask;
1430 	ucon |= clk_sel << info->clksel_shift;
1431 	wr_regl(port, S3C2410_UCON, ucon);
1432 }
1433 
1434 static unsigned int s3c24xx_serial_getclk(struct s3c24xx_uart_port *ourport,
1435 			unsigned int req_baud, struct clk **best_clk,
1436 			unsigned int *clk_num)
1437 {
1438 	const struct s3c24xx_uart_info *info = ourport->info;
1439 	struct clk *clk;
1440 	unsigned long rate;
1441 	unsigned int cnt, baud, quot, best_quot = 0;
1442 	char clkname[MAX_CLK_NAME_LENGTH];
1443 	int calc_deviation, deviation = (1 << 30) - 1;
1444 
1445 	for (cnt = 0; cnt < info->num_clks; cnt++) {
1446 		/* Keep selected clock if provided */
1447 		if (ourport->cfg->clk_sel &&
1448 			!(ourport->cfg->clk_sel & (1 << cnt)))
1449 			continue;
1450 
1451 		sprintf(clkname, "clk_uart_baud%d", cnt);
1452 		clk = clk_get(ourport->port.dev, clkname);
1453 		if (IS_ERR(clk))
1454 			continue;
1455 
1456 		rate = clk_get_rate(clk);
1457 		if (!rate)
1458 			continue;
1459 
1460 		if (ourport->info->has_divslot) {
1461 			unsigned long div = rate / req_baud;
1462 
1463 			/* The UDIVSLOT register on the newer UARTs allows us to
1464 			 * get a divisor adjustment of 1/16th on the baud clock.
1465 			 *
1466 			 * We don't keep the UDIVSLOT value (the 16ths we
1467 			 * calculated by not multiplying the baud by 16) as it
1468 			 * is easy enough to recalculate.
1469 			 */
1470 
1471 			quot = div / 16;
1472 			baud = rate / div;
1473 		} else {
1474 			quot = (rate + (8 * req_baud)) / (16 * req_baud);
1475 			baud = rate / (quot * 16);
1476 		}
1477 		quot--;
1478 
1479 		calc_deviation = req_baud - baud;
1480 		if (calc_deviation < 0)
1481 			calc_deviation = -calc_deviation;
1482 
1483 		if (calc_deviation < deviation) {
1484 			*best_clk = clk;
1485 			best_quot = quot;
1486 			*clk_num = cnt;
1487 			deviation = calc_deviation;
1488 		}
1489 	}
1490 
1491 	return best_quot;
1492 }
1493 
1494 /* udivslot_table[]
1495  *
1496  * This table takes the fractional value of the baud divisor and gives
1497  * the recommended setting for the UDIVSLOT register.
1498  */
1499 static const u16 udivslot_table[16] = {
1500 	[0] = 0x0000,
1501 	[1] = 0x0080,
1502 	[2] = 0x0808,
1503 	[3] = 0x0888,
1504 	[4] = 0x2222,
1505 	[5] = 0x4924,
1506 	[6] = 0x4A52,
1507 	[7] = 0x54AA,
1508 	[8] = 0x5555,
1509 	[9] = 0xD555,
1510 	[10] = 0xD5D5,
1511 	[11] = 0xDDD5,
1512 	[12] = 0xDDDD,
1513 	[13] = 0xDFDD,
1514 	[14] = 0xDFDF,
1515 	[15] = 0xFFDF,
1516 };
1517 
1518 static void s3c24xx_serial_set_termios(struct uart_port *port,
1519 				       struct ktermios *termios,
1520 				       struct ktermios *old)
1521 {
1522 	const struct s3c2410_uartcfg *cfg = s3c24xx_port_to_cfg(port);
1523 	struct s3c24xx_uart_port *ourport = to_ourport(port);
1524 	struct clk *clk = ERR_PTR(-EINVAL);
1525 	unsigned long flags;
1526 	unsigned int baud, quot, clk_sel = 0;
1527 	unsigned int ulcon;
1528 	unsigned int umcon;
1529 	unsigned int udivslot = 0;
1530 
1531 	/*
1532 	 * We don't support modem control lines.
1533 	 */
1534 	termios->c_cflag &= ~(HUPCL | CMSPAR);
1535 	termios->c_cflag |= CLOCAL;
1536 
1537 	/*
1538 	 * Ask the core to calculate the divisor for us.
1539 	 */
1540 
1541 	baud = uart_get_baud_rate(port, termios, old, 0, 3000000);
1542 	quot = s3c24xx_serial_getclk(ourport, baud, &clk, &clk_sel);
1543 	if (baud == 38400 && (port->flags & UPF_SPD_MASK) == UPF_SPD_CUST)
1544 		quot = port->custom_divisor;
1545 	if (IS_ERR(clk))
1546 		return;
1547 
1548 	/* check to see if we need  to change clock source */
1549 
1550 	if (ourport->baudclk != clk) {
1551 		clk_prepare_enable(clk);
1552 
1553 		s3c24xx_serial_setsource(port, clk_sel);
1554 
1555 		if (!IS_ERR(ourport->baudclk)) {
1556 			clk_disable_unprepare(ourport->baudclk);
1557 			ourport->baudclk = ERR_PTR(-EINVAL);
1558 		}
1559 
1560 		ourport->baudclk = clk;
1561 		ourport->baudclk_rate = clk ? clk_get_rate(clk) : 0;
1562 	}
1563 
1564 	if (ourport->info->has_divslot) {
1565 		unsigned int div = ourport->baudclk_rate / baud;
1566 
1567 		if (cfg->has_fracval) {
1568 			udivslot = (div & 15);
1569 			dev_dbg(port->dev, "fracval = %04x\n", udivslot);
1570 		} else {
1571 			udivslot = udivslot_table[div & 15];
1572 			dev_dbg(port->dev, "udivslot = %04x (div %d)\n",
1573 				udivslot, div & 15);
1574 		}
1575 	}
1576 
1577 	switch (termios->c_cflag & CSIZE) {
1578 	case CS5:
1579 		dev_dbg(port->dev, "config: 5bits/char\n");
1580 		ulcon = S3C2410_LCON_CS5;
1581 		break;
1582 	case CS6:
1583 		dev_dbg(port->dev, "config: 6bits/char\n");
1584 		ulcon = S3C2410_LCON_CS6;
1585 		break;
1586 	case CS7:
1587 		dev_dbg(port->dev, "config: 7bits/char\n");
1588 		ulcon = S3C2410_LCON_CS7;
1589 		break;
1590 	case CS8:
1591 	default:
1592 		dev_dbg(port->dev, "config: 8bits/char\n");
1593 		ulcon = S3C2410_LCON_CS8;
1594 		break;
1595 	}
1596 
1597 	/* preserve original lcon IR settings */
1598 	ulcon |= (cfg->ulcon & S3C2410_LCON_IRM);
1599 
1600 	if (termios->c_cflag & CSTOPB)
1601 		ulcon |= S3C2410_LCON_STOPB;
1602 
1603 	if (termios->c_cflag & PARENB) {
1604 		if (termios->c_cflag & PARODD)
1605 			ulcon |= S3C2410_LCON_PODD;
1606 		else
1607 			ulcon |= S3C2410_LCON_PEVEN;
1608 	} else {
1609 		ulcon |= S3C2410_LCON_PNONE;
1610 	}
1611 
1612 	spin_lock_irqsave(&port->lock, flags);
1613 
1614 	dev_dbg(port->dev,
1615 		"setting ulcon to %08x, brddiv to %d, udivslot %08x\n",
1616 		ulcon, quot, udivslot);
1617 
1618 	wr_regl(port, S3C2410_ULCON, ulcon);
1619 	wr_regl(port, S3C2410_UBRDIV, quot);
1620 
1621 	port->status &= ~UPSTAT_AUTOCTS;
1622 
1623 	umcon = rd_regl(port, S3C2410_UMCON);
1624 	if (termios->c_cflag & CRTSCTS) {
1625 		umcon |= S3C2410_UMCOM_AFC;
1626 		/* Disable RTS when RX FIFO contains 63 bytes */
1627 		umcon &= ~S3C2412_UMCON_AFC_8;
1628 		port->status = UPSTAT_AUTOCTS;
1629 	} else {
1630 		umcon &= ~S3C2410_UMCOM_AFC;
1631 	}
1632 	wr_regl(port, S3C2410_UMCON, umcon);
1633 
1634 	if (ourport->info->has_divslot)
1635 		wr_regl(port, S3C2443_DIVSLOT, udivslot);
1636 
1637 	dev_dbg(port->dev,
1638 		"uart: ulcon = 0x%08x, ucon = 0x%08x, ufcon = 0x%08x\n",
1639 		rd_regl(port, S3C2410_ULCON),
1640 		rd_regl(port, S3C2410_UCON),
1641 		rd_regl(port, S3C2410_UFCON));
1642 
1643 	/*
1644 	 * Update the per-port timeout.
1645 	 */
1646 	uart_update_timeout(port, termios->c_cflag, baud);
1647 
1648 	/*
1649 	 * Which character status flags are we interested in?
1650 	 */
1651 	port->read_status_mask = S3C2410_UERSTAT_OVERRUN;
1652 	if (termios->c_iflag & INPCK)
1653 		port->read_status_mask |= S3C2410_UERSTAT_FRAME |
1654 			S3C2410_UERSTAT_PARITY;
1655 	/*
1656 	 * Which character status flags should we ignore?
1657 	 */
1658 	port->ignore_status_mask = 0;
1659 	if (termios->c_iflag & IGNPAR)
1660 		port->ignore_status_mask |= S3C2410_UERSTAT_OVERRUN;
1661 	if (termios->c_iflag & IGNBRK && termios->c_iflag & IGNPAR)
1662 		port->ignore_status_mask |= S3C2410_UERSTAT_FRAME;
1663 
1664 	/*
1665 	 * Ignore all characters if CREAD is not set.
1666 	 */
1667 	if ((termios->c_cflag & CREAD) == 0)
1668 		port->ignore_status_mask |= RXSTAT_DUMMY_READ;
1669 
1670 	spin_unlock_irqrestore(&port->lock, flags);
1671 }
1672 
1673 static const char *s3c24xx_serial_type(struct uart_port *port)
1674 {
1675 	const struct s3c24xx_uart_port *ourport = to_ourport(port);
1676 
1677 	switch (ourport->info->type) {
1678 	case TYPE_S3C24XX:
1679 		return "S3C24XX";
1680 	case TYPE_S3C6400:
1681 		return "S3C6400/10";
1682 	case TYPE_APPLE_S5L:
1683 		return "APPLE S5L";
1684 	default:
1685 		return NULL;
1686 	}
1687 }
1688 
1689 static void s3c24xx_serial_config_port(struct uart_port *port, int flags)
1690 {
1691 	const struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
1692 
1693 	if (flags & UART_CONFIG_TYPE)
1694 		port->type = info->port_type;
1695 }
1696 
1697 /*
1698  * verify the new serial_struct (for TIOCSSERIAL).
1699  */
1700 static int
1701 s3c24xx_serial_verify_port(struct uart_port *port, struct serial_struct *ser)
1702 {
1703 	const struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
1704 
1705 	if (ser->type != PORT_UNKNOWN && ser->type != info->port_type)
1706 		return -EINVAL;
1707 
1708 	return 0;
1709 }
1710 
1711 #ifdef CONFIG_SERIAL_SAMSUNG_CONSOLE
1712 
1713 static struct console s3c24xx_serial_console;
1714 
1715 static void __init s3c24xx_serial_register_console(void)
1716 {
1717 	register_console(&s3c24xx_serial_console);
1718 }
1719 
1720 static void s3c24xx_serial_unregister_console(void)
1721 {
1722 	if (s3c24xx_serial_console.flags & CON_ENABLED)
1723 		unregister_console(&s3c24xx_serial_console);
1724 }
1725 
1726 #define S3C24XX_SERIAL_CONSOLE &s3c24xx_serial_console
1727 #else
1728 static inline void s3c24xx_serial_register_console(void) { }
1729 static inline void s3c24xx_serial_unregister_console(void) { }
1730 #define S3C24XX_SERIAL_CONSOLE NULL
1731 #endif
1732 
1733 #if defined(CONFIG_SERIAL_SAMSUNG_CONSOLE) && defined(CONFIG_CONSOLE_POLL)
1734 static int s3c24xx_serial_get_poll_char(struct uart_port *port);
1735 static void s3c24xx_serial_put_poll_char(struct uart_port *port,
1736 			 unsigned char c);
1737 #endif
1738 
1739 static const struct uart_ops s3c24xx_serial_ops = {
1740 	.pm		= s3c24xx_serial_pm,
1741 	.tx_empty	= s3c24xx_serial_tx_empty,
1742 	.get_mctrl	= s3c24xx_serial_get_mctrl,
1743 	.set_mctrl	= s3c24xx_serial_set_mctrl,
1744 	.stop_tx	= s3c24xx_serial_stop_tx,
1745 	.start_tx	= s3c24xx_serial_start_tx,
1746 	.stop_rx	= s3c24xx_serial_stop_rx,
1747 	.break_ctl	= s3c24xx_serial_break_ctl,
1748 	.startup	= s3c24xx_serial_startup,
1749 	.shutdown	= s3c24xx_serial_shutdown,
1750 	.set_termios	= s3c24xx_serial_set_termios,
1751 	.type		= s3c24xx_serial_type,
1752 	.config_port	= s3c24xx_serial_config_port,
1753 	.verify_port	= s3c24xx_serial_verify_port,
1754 #if defined(CONFIG_SERIAL_SAMSUNG_CONSOLE) && defined(CONFIG_CONSOLE_POLL)
1755 	.poll_get_char = s3c24xx_serial_get_poll_char,
1756 	.poll_put_char = s3c24xx_serial_put_poll_char,
1757 #endif
1758 };
1759 
1760 static const struct uart_ops s3c64xx_serial_ops = {
1761 	.pm		= s3c24xx_serial_pm,
1762 	.tx_empty	= s3c24xx_serial_tx_empty,
1763 	.get_mctrl	= s3c24xx_serial_get_mctrl,
1764 	.set_mctrl	= s3c24xx_serial_set_mctrl,
1765 	.stop_tx	= s3c24xx_serial_stop_tx,
1766 	.start_tx	= s3c24xx_serial_start_tx,
1767 	.stop_rx	= s3c24xx_serial_stop_rx,
1768 	.break_ctl	= s3c24xx_serial_break_ctl,
1769 	.startup	= s3c64xx_serial_startup,
1770 	.shutdown	= s3c64xx_serial_shutdown,
1771 	.set_termios	= s3c24xx_serial_set_termios,
1772 	.type		= s3c24xx_serial_type,
1773 	.config_port	= s3c24xx_serial_config_port,
1774 	.verify_port	= s3c24xx_serial_verify_port,
1775 #if defined(CONFIG_SERIAL_SAMSUNG_CONSOLE) && defined(CONFIG_CONSOLE_POLL)
1776 	.poll_get_char = s3c24xx_serial_get_poll_char,
1777 	.poll_put_char = s3c24xx_serial_put_poll_char,
1778 #endif
1779 };
1780 
1781 static const struct uart_ops apple_s5l_serial_ops = {
1782 	.pm		= s3c24xx_serial_pm,
1783 	.tx_empty	= s3c24xx_serial_tx_empty,
1784 	.get_mctrl	= s3c24xx_serial_get_mctrl,
1785 	.set_mctrl	= s3c24xx_serial_set_mctrl,
1786 	.stop_tx	= s3c24xx_serial_stop_tx,
1787 	.start_tx	= s3c24xx_serial_start_tx,
1788 	.stop_rx	= s3c24xx_serial_stop_rx,
1789 	.break_ctl	= s3c24xx_serial_break_ctl,
1790 	.startup	= apple_s5l_serial_startup,
1791 	.shutdown	= apple_s5l_serial_shutdown,
1792 	.set_termios	= s3c24xx_serial_set_termios,
1793 	.type		= s3c24xx_serial_type,
1794 	.config_port	= s3c24xx_serial_config_port,
1795 	.verify_port	= s3c24xx_serial_verify_port,
1796 #if defined(CONFIG_SERIAL_SAMSUNG_CONSOLE) && defined(CONFIG_CONSOLE_POLL)
1797 	.poll_get_char = s3c24xx_serial_get_poll_char,
1798 	.poll_put_char = s3c24xx_serial_put_poll_char,
1799 #endif
1800 };
1801 
1802 static struct uart_driver s3c24xx_uart_drv = {
1803 	.owner		= THIS_MODULE,
1804 	.driver_name	= "s3c2410_serial",
1805 	.nr		= CONFIG_SERIAL_SAMSUNG_UARTS,
1806 	.cons		= S3C24XX_SERIAL_CONSOLE,
1807 	.dev_name	= S3C24XX_SERIAL_NAME,
1808 	.major		= S3C24XX_SERIAL_MAJOR,
1809 	.minor		= S3C24XX_SERIAL_MINOR,
1810 };
1811 
1812 #define __PORT_LOCK_UNLOCKED(i) \
1813 	__SPIN_LOCK_UNLOCKED(s3c24xx_serial_ports[i].port.lock)
1814 static struct s3c24xx_uart_port
1815 s3c24xx_serial_ports[CONFIG_SERIAL_SAMSUNG_UARTS] = {
1816 	[0] = {
1817 		.port = {
1818 			.lock		= __PORT_LOCK_UNLOCKED(0),
1819 			.iotype		= UPIO_MEM,
1820 			.uartclk	= 0,
1821 			.fifosize	= 16,
1822 			.ops		= &s3c24xx_serial_ops,
1823 			.flags		= UPF_BOOT_AUTOCONF,
1824 			.line		= 0,
1825 		}
1826 	},
1827 	[1] = {
1828 		.port = {
1829 			.lock		= __PORT_LOCK_UNLOCKED(1),
1830 			.iotype		= UPIO_MEM,
1831 			.uartclk	= 0,
1832 			.fifosize	= 16,
1833 			.ops		= &s3c24xx_serial_ops,
1834 			.flags		= UPF_BOOT_AUTOCONF,
1835 			.line		= 1,
1836 		}
1837 	},
1838 #if CONFIG_SERIAL_SAMSUNG_UARTS > 2
1839 	[2] = {
1840 		.port = {
1841 			.lock		= __PORT_LOCK_UNLOCKED(2),
1842 			.iotype		= UPIO_MEM,
1843 			.uartclk	= 0,
1844 			.fifosize	= 16,
1845 			.ops		= &s3c24xx_serial_ops,
1846 			.flags		= UPF_BOOT_AUTOCONF,
1847 			.line		= 2,
1848 		}
1849 	},
1850 #endif
1851 #if CONFIG_SERIAL_SAMSUNG_UARTS > 3
1852 	[3] = {
1853 		.port = {
1854 			.lock		= __PORT_LOCK_UNLOCKED(3),
1855 			.iotype		= UPIO_MEM,
1856 			.uartclk	= 0,
1857 			.fifosize	= 16,
1858 			.ops		= &s3c24xx_serial_ops,
1859 			.flags		= UPF_BOOT_AUTOCONF,
1860 			.line		= 3,
1861 		}
1862 	}
1863 #endif
1864 };
1865 #undef __PORT_LOCK_UNLOCKED
1866 
1867 /* s3c24xx_serial_resetport
1868  *
1869  * reset the fifos and other the settings.
1870  */
1871 
1872 static void s3c24xx_serial_resetport(struct uart_port *port,
1873 				     const struct s3c2410_uartcfg *cfg)
1874 {
1875 	const struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
1876 	unsigned long ucon = rd_regl(port, S3C2410_UCON);
1877 
1878 	ucon &= (info->clksel_mask | info->ucon_mask);
1879 	wr_regl(port, S3C2410_UCON, ucon | cfg->ucon);
1880 
1881 	/* reset both fifos */
1882 	wr_regl(port, S3C2410_UFCON, cfg->ufcon | S3C2410_UFCON_RESETBOTH);
1883 	wr_regl(port, S3C2410_UFCON, cfg->ufcon);
1884 
1885 	/* some delay is required after fifo reset */
1886 	udelay(1);
1887 }
1888 
1889 #ifdef CONFIG_ARM_S3C24XX_CPUFREQ
1890 
1891 static int s3c24xx_serial_cpufreq_transition(struct notifier_block *nb,
1892 					     unsigned long val, void *data)
1893 {
1894 	struct s3c24xx_uart_port *port;
1895 	struct uart_port *uport;
1896 
1897 	port = container_of(nb, struct s3c24xx_uart_port, freq_transition);
1898 	uport = &port->port;
1899 
1900 	/* check to see if port is enabled */
1901 
1902 	if (port->pm_level != 0)
1903 		return 0;
1904 
1905 	/* try and work out if the baudrate is changing, we can detect
1906 	 * a change in rate, but we do not have support for detecting
1907 	 * a disturbance in the clock-rate over the change.
1908 	 */
1909 
1910 	if (IS_ERR(port->baudclk))
1911 		goto exit;
1912 
1913 	if (port->baudclk_rate == clk_get_rate(port->baudclk))
1914 		goto exit;
1915 
1916 	if (val == CPUFREQ_PRECHANGE) {
1917 		/* we should really shut the port down whilst the
1918 		 * frequency change is in progress.
1919 		 */
1920 
1921 	} else if (val == CPUFREQ_POSTCHANGE) {
1922 		struct ktermios *termios;
1923 		struct tty_struct *tty;
1924 
1925 		if (uport->state == NULL)
1926 			goto exit;
1927 
1928 		tty = uport->state->port.tty;
1929 
1930 		if (tty == NULL)
1931 			goto exit;
1932 
1933 		termios = &tty->termios;
1934 
1935 		if (termios == NULL) {
1936 			dev_warn(uport->dev, "%s: no termios?\n", __func__);
1937 			goto exit;
1938 		}
1939 
1940 		s3c24xx_serial_set_termios(uport, termios, NULL);
1941 	}
1942 
1943 exit:
1944 	return 0;
1945 }
1946 
1947 static inline int
1948 s3c24xx_serial_cpufreq_register(struct s3c24xx_uart_port *port)
1949 {
1950 	port->freq_transition.notifier_call = s3c24xx_serial_cpufreq_transition;
1951 
1952 	return cpufreq_register_notifier(&port->freq_transition,
1953 					 CPUFREQ_TRANSITION_NOTIFIER);
1954 }
1955 
1956 static inline void
1957 s3c24xx_serial_cpufreq_deregister(struct s3c24xx_uart_port *port)
1958 {
1959 	cpufreq_unregister_notifier(&port->freq_transition,
1960 				    CPUFREQ_TRANSITION_NOTIFIER);
1961 }
1962 
1963 #else
1964 static inline int
1965 s3c24xx_serial_cpufreq_register(struct s3c24xx_uart_port *port)
1966 {
1967 	return 0;
1968 }
1969 
1970 static inline void
1971 s3c24xx_serial_cpufreq_deregister(struct s3c24xx_uart_port *port)
1972 {
1973 }
1974 #endif
1975 
1976 static int s3c24xx_serial_enable_baudclk(struct s3c24xx_uart_port *ourport)
1977 {
1978 	struct device *dev = ourport->port.dev;
1979 	const struct s3c24xx_uart_info *info = ourport->info;
1980 	char clk_name[MAX_CLK_NAME_LENGTH];
1981 	unsigned int clk_sel;
1982 	struct clk *clk;
1983 	int clk_num;
1984 	int ret;
1985 
1986 	clk_sel = ourport->cfg->clk_sel ? : info->def_clk_sel;
1987 	for (clk_num = 0; clk_num < info->num_clks; clk_num++) {
1988 		if (!(clk_sel & (1 << clk_num)))
1989 			continue;
1990 
1991 		sprintf(clk_name, "clk_uart_baud%d", clk_num);
1992 		clk = clk_get(dev, clk_name);
1993 		if (IS_ERR(clk))
1994 			continue;
1995 
1996 		ret = clk_prepare_enable(clk);
1997 		if (ret) {
1998 			clk_put(clk);
1999 			continue;
2000 		}
2001 
2002 		ourport->baudclk = clk;
2003 		ourport->baudclk_rate = clk_get_rate(clk);
2004 		s3c24xx_serial_setsource(&ourport->port, clk_num);
2005 
2006 		return 0;
2007 	}
2008 
2009 	return -EINVAL;
2010 }
2011 
2012 /* s3c24xx_serial_init_port
2013  *
2014  * initialise a single serial port from the platform device given
2015  */
2016 
2017 static int s3c24xx_serial_init_port(struct s3c24xx_uart_port *ourport,
2018 				    struct platform_device *platdev)
2019 {
2020 	struct uart_port *port = &ourport->port;
2021 	const struct s3c2410_uartcfg *cfg = ourport->cfg;
2022 	struct resource *res;
2023 	int ret;
2024 
2025 	if (platdev == NULL)
2026 		return -ENODEV;
2027 
2028 	if (port->mapbase != 0)
2029 		return -EINVAL;
2030 
2031 	/* setup info for port */
2032 	port->dev	= &platdev->dev;
2033 
2034 	port->uartclk = 1;
2035 
2036 	if (cfg->uart_flags & UPF_CONS_FLOW) {
2037 		dev_dbg(port->dev, "enabling flow control\n");
2038 		port->flags |= UPF_CONS_FLOW;
2039 	}
2040 
2041 	/* sort our the physical and virtual addresses for each UART */
2042 
2043 	res = platform_get_resource(platdev, IORESOURCE_MEM, 0);
2044 	if (res == NULL) {
2045 		dev_err(port->dev, "failed to find memory resource for uart\n");
2046 		return -EINVAL;
2047 	}
2048 
2049 	dev_dbg(port->dev, "resource %pR)\n", res);
2050 
2051 	port->membase = devm_ioremap_resource(port->dev, res);
2052 	if (IS_ERR(port->membase)) {
2053 		dev_err(port->dev, "failed to remap controller address\n");
2054 		return -EBUSY;
2055 	}
2056 
2057 	port->mapbase = res->start;
2058 	ret = platform_get_irq(platdev, 0);
2059 	if (ret < 0) {
2060 		port->irq = 0;
2061 	} else {
2062 		port->irq = ret;
2063 		ourport->rx_irq = ret;
2064 		ourport->tx_irq = ret + 1;
2065 	}
2066 
2067 	switch (ourport->info->type) {
2068 	case TYPE_S3C24XX:
2069 		ret = platform_get_irq(platdev, 1);
2070 		if (ret > 0)
2071 			ourport->tx_irq = ret;
2072 		break;
2073 	default:
2074 		break;
2075 	}
2076 
2077 	/*
2078 	 * DMA is currently supported only on DT platforms, if DMA properties
2079 	 * are specified.
2080 	 */
2081 	if (platdev->dev.of_node && of_find_property(platdev->dev.of_node,
2082 						     "dmas", NULL)) {
2083 		ourport->dma = devm_kzalloc(port->dev,
2084 					    sizeof(*ourport->dma),
2085 					    GFP_KERNEL);
2086 		if (!ourport->dma) {
2087 			ret = -ENOMEM;
2088 			goto err;
2089 		}
2090 	}
2091 
2092 	ourport->clk	= clk_get(&platdev->dev, "uart");
2093 	if (IS_ERR(ourport->clk)) {
2094 		pr_err("%s: Controller clock not found\n",
2095 				dev_name(&platdev->dev));
2096 		ret = PTR_ERR(ourport->clk);
2097 		goto err;
2098 	}
2099 
2100 	ret = clk_prepare_enable(ourport->clk);
2101 	if (ret) {
2102 		pr_err("uart: clock failed to prepare+enable: %d\n", ret);
2103 		clk_put(ourport->clk);
2104 		goto err;
2105 	}
2106 
2107 	ret = s3c24xx_serial_enable_baudclk(ourport);
2108 	if (ret)
2109 		pr_warn("uart: failed to enable baudclk\n");
2110 
2111 	/* Keep all interrupts masked and cleared */
2112 	switch (ourport->info->type) {
2113 	case TYPE_S3C6400:
2114 		wr_regl(port, S3C64XX_UINTM, 0xf);
2115 		wr_regl(port, S3C64XX_UINTP, 0xf);
2116 		wr_regl(port, S3C64XX_UINTSP, 0xf);
2117 		break;
2118 	case TYPE_APPLE_S5L: {
2119 		unsigned int ucon;
2120 
2121 		ucon = rd_regl(port, S3C2410_UCON);
2122 		ucon &= ~(APPLE_S5L_UCON_TXTHRESH_ENA_MSK |
2123 			APPLE_S5L_UCON_RXTHRESH_ENA_MSK |
2124 			APPLE_S5L_UCON_RXTO_ENA_MSK);
2125 		wr_regl(port, S3C2410_UCON, ucon);
2126 
2127 		wr_regl(port, S3C2410_UTRSTAT, APPLE_S5L_UTRSTAT_ALL_FLAGS);
2128 		break;
2129 	}
2130 	default:
2131 		break;
2132 	}
2133 
2134 	dev_dbg(port->dev, "port: map=%pa, mem=%p, irq=%d (%d,%d), clock=%u\n",
2135 		&port->mapbase, port->membase, port->irq,
2136 		ourport->rx_irq, ourport->tx_irq, port->uartclk);
2137 
2138 	/* reset the fifos (and setup the uart) */
2139 	s3c24xx_serial_resetport(port, cfg);
2140 
2141 	return 0;
2142 
2143 err:
2144 	port->mapbase = 0;
2145 	return ret;
2146 }
2147 
2148 /* Device driver serial port probe */
2149 
2150 static int probe_index;
2151 
2152 static inline const struct s3c24xx_serial_drv_data *
2153 s3c24xx_get_driver_data(struct platform_device *pdev)
2154 {
2155 	if (dev_of_node(&pdev->dev))
2156 		return of_device_get_match_data(&pdev->dev);
2157 
2158 	return (struct s3c24xx_serial_drv_data *)
2159 			platform_get_device_id(pdev)->driver_data;
2160 }
2161 
2162 static int s3c24xx_serial_probe(struct platform_device *pdev)
2163 {
2164 	struct device_node *np = pdev->dev.of_node;
2165 	struct s3c24xx_uart_port *ourport;
2166 	int index = probe_index;
2167 	int ret, prop = 0;
2168 
2169 	if (np) {
2170 		ret = of_alias_get_id(np, "serial");
2171 		if (ret >= 0)
2172 			index = ret;
2173 	}
2174 
2175 	if (index >= ARRAY_SIZE(s3c24xx_serial_ports)) {
2176 		dev_err(&pdev->dev, "serial%d out of range\n", index);
2177 		return -EINVAL;
2178 	}
2179 	ourport = &s3c24xx_serial_ports[index];
2180 
2181 	ourport->drv_data = s3c24xx_get_driver_data(pdev);
2182 	if (!ourport->drv_data) {
2183 		dev_err(&pdev->dev, "could not find driver data\n");
2184 		return -ENODEV;
2185 	}
2186 
2187 	ourport->baudclk = ERR_PTR(-EINVAL);
2188 	ourport->info = &ourport->drv_data->info;
2189 	ourport->cfg = (dev_get_platdata(&pdev->dev)) ?
2190 			dev_get_platdata(&pdev->dev) :
2191 			&ourport->drv_data->def_cfg;
2192 
2193 	switch (ourport->info->type) {
2194 	case TYPE_S3C24XX:
2195 		ourport->port.ops = &s3c24xx_serial_ops;
2196 		break;
2197 	case TYPE_S3C6400:
2198 		ourport->port.ops = &s3c64xx_serial_ops;
2199 		break;
2200 	case TYPE_APPLE_S5L:
2201 		ourport->port.ops = &apple_s5l_serial_ops;
2202 		break;
2203 	}
2204 
2205 	if (np) {
2206 		of_property_read_u32(np,
2207 			"samsung,uart-fifosize", &ourport->port.fifosize);
2208 
2209 		if (of_property_read_u32(np, "reg-io-width", &prop) == 0) {
2210 			switch (prop) {
2211 			case 1:
2212 				ourport->port.iotype = UPIO_MEM;
2213 				break;
2214 			case 4:
2215 				ourport->port.iotype = UPIO_MEM32;
2216 				break;
2217 			default:
2218 				dev_warn(&pdev->dev, "unsupported reg-io-width (%d)\n",
2219 						prop);
2220 				return -EINVAL;
2221 			}
2222 		}
2223 	}
2224 
2225 	if (ourport->drv_data->fifosize[index])
2226 		ourport->port.fifosize = ourport->drv_data->fifosize[index];
2227 	else if (ourport->info->fifosize)
2228 		ourport->port.fifosize = ourport->info->fifosize;
2229 	ourport->port.has_sysrq = IS_ENABLED(CONFIG_SERIAL_SAMSUNG_CONSOLE);
2230 
2231 	/*
2232 	 * DMA transfers must be aligned at least to cache line size,
2233 	 * so find minimal transfer size suitable for DMA mode
2234 	 */
2235 	ourport->min_dma_size = max_t(int, ourport->port.fifosize,
2236 				    dma_get_cache_alignment());
2237 
2238 	dev_dbg(&pdev->dev, "%s: initialising port %p...\n", __func__, ourport);
2239 
2240 	ret = s3c24xx_serial_init_port(ourport, pdev);
2241 	if (ret < 0)
2242 		return ret;
2243 
2244 	if (!s3c24xx_uart_drv.state) {
2245 		ret = uart_register_driver(&s3c24xx_uart_drv);
2246 		if (ret < 0) {
2247 			pr_err("Failed to register Samsung UART driver\n");
2248 			return ret;
2249 		}
2250 	}
2251 
2252 	dev_dbg(&pdev->dev, "%s: adding port\n", __func__);
2253 	uart_add_one_port(&s3c24xx_uart_drv, &ourport->port);
2254 	platform_set_drvdata(pdev, &ourport->port);
2255 
2256 	/*
2257 	 * Deactivate the clock enabled in s3c24xx_serial_init_port here,
2258 	 * so that a potential re-enablement through the pm-callback overlaps
2259 	 * and keeps the clock enabled in this case.
2260 	 */
2261 	clk_disable_unprepare(ourport->clk);
2262 	if (!IS_ERR(ourport->baudclk))
2263 		clk_disable_unprepare(ourport->baudclk);
2264 
2265 	ret = s3c24xx_serial_cpufreq_register(ourport);
2266 	if (ret < 0)
2267 		dev_err(&pdev->dev, "failed to add cpufreq notifier\n");
2268 
2269 	probe_index++;
2270 
2271 	return 0;
2272 }
2273 
2274 static int s3c24xx_serial_remove(struct platform_device *dev)
2275 {
2276 	struct uart_port *port = s3c24xx_dev_to_port(&dev->dev);
2277 
2278 	if (port) {
2279 		s3c24xx_serial_cpufreq_deregister(to_ourport(port));
2280 		uart_remove_one_port(&s3c24xx_uart_drv, port);
2281 	}
2282 
2283 	uart_unregister_driver(&s3c24xx_uart_drv);
2284 
2285 	return 0;
2286 }
2287 
2288 /* UART power management code */
2289 #ifdef CONFIG_PM_SLEEP
2290 static int s3c24xx_serial_suspend(struct device *dev)
2291 {
2292 	struct uart_port *port = s3c24xx_dev_to_port(dev);
2293 
2294 	if (port)
2295 		uart_suspend_port(&s3c24xx_uart_drv, port);
2296 
2297 	return 0;
2298 }
2299 
2300 static int s3c24xx_serial_resume(struct device *dev)
2301 {
2302 	struct uart_port *port = s3c24xx_dev_to_port(dev);
2303 	struct s3c24xx_uart_port *ourport = to_ourport(port);
2304 
2305 	if (port) {
2306 		clk_prepare_enable(ourport->clk);
2307 		if (!IS_ERR(ourport->baudclk))
2308 			clk_prepare_enable(ourport->baudclk);
2309 		s3c24xx_serial_resetport(port, s3c24xx_port_to_cfg(port));
2310 		if (!IS_ERR(ourport->baudclk))
2311 			clk_disable_unprepare(ourport->baudclk);
2312 		clk_disable_unprepare(ourport->clk);
2313 
2314 		uart_resume_port(&s3c24xx_uart_drv, port);
2315 	}
2316 
2317 	return 0;
2318 }
2319 
2320 static int s3c24xx_serial_resume_noirq(struct device *dev)
2321 {
2322 	struct uart_port *port = s3c24xx_dev_to_port(dev);
2323 	struct s3c24xx_uart_port *ourport = to_ourport(port);
2324 
2325 	if (port) {
2326 		/* restore IRQ mask */
2327 		switch (ourport->info->type) {
2328 		case TYPE_S3C6400: {
2329 			unsigned int uintm = 0xf;
2330 
2331 			if (ourport->tx_enabled)
2332 				uintm &= ~S3C64XX_UINTM_TXD_MSK;
2333 			if (ourport->rx_enabled)
2334 				uintm &= ~S3C64XX_UINTM_RXD_MSK;
2335 			clk_prepare_enable(ourport->clk);
2336 			if (!IS_ERR(ourport->baudclk))
2337 				clk_prepare_enable(ourport->baudclk);
2338 			wr_regl(port, S3C64XX_UINTM, uintm);
2339 			if (!IS_ERR(ourport->baudclk))
2340 				clk_disable_unprepare(ourport->baudclk);
2341 			clk_disable_unprepare(ourport->clk);
2342 			break;
2343 		}
2344 		case TYPE_APPLE_S5L: {
2345 			unsigned int ucon;
2346 			int ret;
2347 
2348 			ret = clk_prepare_enable(ourport->clk);
2349 			if (ret) {
2350 				dev_err(dev, "clk_enable clk failed: %d\n", ret);
2351 				return ret;
2352 			}
2353 			if (!IS_ERR(ourport->baudclk)) {
2354 				ret = clk_prepare_enable(ourport->baudclk);
2355 				if (ret) {
2356 					dev_err(dev, "clk_enable baudclk failed: %d\n", ret);
2357 					clk_disable_unprepare(ourport->clk);
2358 					return ret;
2359 				}
2360 			}
2361 
2362 			ucon = rd_regl(port, S3C2410_UCON);
2363 
2364 			ucon &= ~(APPLE_S5L_UCON_TXTHRESH_ENA_MSK |
2365 				  APPLE_S5L_UCON_RXTHRESH_ENA_MSK |
2366 				  APPLE_S5L_UCON_RXTO_ENA_MSK);
2367 
2368 			if (ourport->tx_enabled)
2369 				ucon |= APPLE_S5L_UCON_TXTHRESH_ENA_MSK;
2370 			if (ourport->rx_enabled)
2371 				ucon |= APPLE_S5L_UCON_RXTHRESH_ENA_MSK |
2372 					APPLE_S5L_UCON_RXTO_ENA_MSK;
2373 
2374 			wr_regl(port, S3C2410_UCON, ucon);
2375 
2376 			if (!IS_ERR(ourport->baudclk))
2377 				clk_disable_unprepare(ourport->baudclk);
2378 			clk_disable_unprepare(ourport->clk);
2379 			break;
2380 		}
2381 		default:
2382 			break;
2383 		}
2384 	}
2385 
2386 	return 0;
2387 }
2388 
2389 static const struct dev_pm_ops s3c24xx_serial_pm_ops = {
2390 	.suspend = s3c24xx_serial_suspend,
2391 	.resume = s3c24xx_serial_resume,
2392 	.resume_noirq = s3c24xx_serial_resume_noirq,
2393 };
2394 #define SERIAL_SAMSUNG_PM_OPS	(&s3c24xx_serial_pm_ops)
2395 
2396 #else /* !CONFIG_PM_SLEEP */
2397 
2398 #define SERIAL_SAMSUNG_PM_OPS	NULL
2399 #endif /* CONFIG_PM_SLEEP */
2400 
2401 /* Console code */
2402 
2403 #ifdef CONFIG_SERIAL_SAMSUNG_CONSOLE
2404 
2405 static struct uart_port *cons_uart;
2406 
2407 static int
2408 s3c24xx_serial_console_txrdy(struct uart_port *port, unsigned int ufcon)
2409 {
2410 	const struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
2411 	unsigned long ufstat, utrstat;
2412 
2413 	if (ufcon & S3C2410_UFCON_FIFOMODE) {
2414 		/* fifo mode - check amount of data in fifo registers... */
2415 
2416 		ufstat = rd_regl(port, S3C2410_UFSTAT);
2417 		return (ufstat & info->tx_fifofull) ? 0 : 1;
2418 	}
2419 
2420 	/* in non-fifo mode, we go and use the tx buffer empty */
2421 
2422 	utrstat = rd_regl(port, S3C2410_UTRSTAT);
2423 	return (utrstat & S3C2410_UTRSTAT_TXE) ? 1 : 0;
2424 }
2425 
2426 static bool
2427 s3c24xx_port_configured(unsigned int ucon)
2428 {
2429 	/* consider the serial port configured if the tx/rx mode set */
2430 	return (ucon & 0xf) != 0;
2431 }
2432 
2433 #ifdef CONFIG_CONSOLE_POLL
2434 /*
2435  * Console polling routines for writing and reading from the uart while
2436  * in an interrupt or debug context.
2437  */
2438 
2439 static int s3c24xx_serial_get_poll_char(struct uart_port *port)
2440 {
2441 	const struct s3c24xx_uart_port *ourport = to_ourport(port);
2442 	unsigned int ufstat;
2443 
2444 	ufstat = rd_regl(port, S3C2410_UFSTAT);
2445 	if (s3c24xx_serial_rx_fifocnt(ourport, ufstat) == 0)
2446 		return NO_POLL_CHAR;
2447 
2448 	return rd_reg(port, S3C2410_URXH);
2449 }
2450 
2451 static void s3c24xx_serial_put_poll_char(struct uart_port *port,
2452 		unsigned char c)
2453 {
2454 	unsigned int ufcon = rd_regl(port, S3C2410_UFCON);
2455 	unsigned int ucon = rd_regl(port, S3C2410_UCON);
2456 
2457 	/* not possible to xmit on unconfigured port */
2458 	if (!s3c24xx_port_configured(ucon))
2459 		return;
2460 
2461 	while (!s3c24xx_serial_console_txrdy(port, ufcon))
2462 		cpu_relax();
2463 	wr_reg(port, S3C2410_UTXH, c);
2464 }
2465 
2466 #endif /* CONFIG_CONSOLE_POLL */
2467 
2468 static void
2469 s3c24xx_serial_console_putchar(struct uart_port *port, unsigned char ch)
2470 {
2471 	unsigned int ufcon = rd_regl(port, S3C2410_UFCON);
2472 
2473 	while (!s3c24xx_serial_console_txrdy(port, ufcon))
2474 		cpu_relax();
2475 	wr_reg(port, S3C2410_UTXH, ch);
2476 }
2477 
2478 static void
2479 s3c24xx_serial_console_write(struct console *co, const char *s,
2480 			     unsigned int count)
2481 {
2482 	unsigned int ucon = rd_regl(cons_uart, S3C2410_UCON);
2483 
2484 	/* not possible to xmit on unconfigured port */
2485 	if (!s3c24xx_port_configured(ucon))
2486 		return;
2487 
2488 	uart_console_write(cons_uart, s, count, s3c24xx_serial_console_putchar);
2489 }
2490 
2491 /* Shouldn't be __init, as it can be instantiated from other module */
2492 static void
2493 s3c24xx_serial_get_options(struct uart_port *port, int *baud,
2494 			   int *parity, int *bits)
2495 {
2496 	struct clk *clk;
2497 	unsigned int ulcon;
2498 	unsigned int ucon;
2499 	unsigned int ubrdiv;
2500 	unsigned long rate;
2501 	unsigned int clk_sel;
2502 	char clk_name[MAX_CLK_NAME_LENGTH];
2503 
2504 	ulcon  = rd_regl(port, S3C2410_ULCON);
2505 	ucon   = rd_regl(port, S3C2410_UCON);
2506 	ubrdiv = rd_regl(port, S3C2410_UBRDIV);
2507 
2508 	if (s3c24xx_port_configured(ucon)) {
2509 		switch (ulcon & S3C2410_LCON_CSMASK) {
2510 		case S3C2410_LCON_CS5:
2511 			*bits = 5;
2512 			break;
2513 		case S3C2410_LCON_CS6:
2514 			*bits = 6;
2515 			break;
2516 		case S3C2410_LCON_CS7:
2517 			*bits = 7;
2518 			break;
2519 		case S3C2410_LCON_CS8:
2520 		default:
2521 			*bits = 8;
2522 			break;
2523 		}
2524 
2525 		switch (ulcon & S3C2410_LCON_PMASK) {
2526 		case S3C2410_LCON_PEVEN:
2527 			*parity = 'e';
2528 			break;
2529 
2530 		case S3C2410_LCON_PODD:
2531 			*parity = 'o';
2532 			break;
2533 
2534 		case S3C2410_LCON_PNONE:
2535 		default:
2536 			*parity = 'n';
2537 		}
2538 
2539 		/* now calculate the baud rate */
2540 
2541 		clk_sel = s3c24xx_serial_getsource(port);
2542 		sprintf(clk_name, "clk_uart_baud%d", clk_sel);
2543 
2544 		clk = clk_get(port->dev, clk_name);
2545 		if (!IS_ERR(clk))
2546 			rate = clk_get_rate(clk);
2547 		else
2548 			rate = 1;
2549 
2550 		*baud = rate / (16 * (ubrdiv + 1));
2551 		dev_dbg(port->dev, "calculated baud %d\n", *baud);
2552 	}
2553 }
2554 
2555 /* Shouldn't be __init, as it can be instantiated from other module */
2556 static int
2557 s3c24xx_serial_console_setup(struct console *co, char *options)
2558 {
2559 	struct uart_port *port;
2560 	int baud = 9600;
2561 	int bits = 8;
2562 	int parity = 'n';
2563 	int flow = 'n';
2564 
2565 	/* is this a valid port */
2566 
2567 	if (co->index == -1 || co->index >= CONFIG_SERIAL_SAMSUNG_UARTS)
2568 		co->index = 0;
2569 
2570 	port = &s3c24xx_serial_ports[co->index].port;
2571 
2572 	/* is the port configured? */
2573 
2574 	if (port->mapbase == 0x0)
2575 		return -ENODEV;
2576 
2577 	cons_uart = port;
2578 
2579 	/*
2580 	 * Check whether an invalid uart number has been specified, and
2581 	 * if so, search for the first available port that does have
2582 	 * console support.
2583 	 */
2584 	if (options)
2585 		uart_parse_options(options, &baud, &parity, &bits, &flow);
2586 	else
2587 		s3c24xx_serial_get_options(port, &baud, &parity, &bits);
2588 
2589 	dev_dbg(port->dev, "baud %d\n", baud);
2590 
2591 	return uart_set_options(port, co, baud, parity, bits, flow);
2592 }
2593 
2594 static struct console s3c24xx_serial_console = {
2595 	.name		= S3C24XX_SERIAL_NAME,
2596 	.device		= uart_console_device,
2597 	.flags		= CON_PRINTBUFFER,
2598 	.index		= -1,
2599 	.write		= s3c24xx_serial_console_write,
2600 	.setup		= s3c24xx_serial_console_setup,
2601 	.data		= &s3c24xx_uart_drv,
2602 };
2603 #endif /* CONFIG_SERIAL_SAMSUNG_CONSOLE */
2604 
2605 #ifdef CONFIG_CPU_S3C2410
2606 static const struct s3c24xx_serial_drv_data s3c2410_serial_drv_data = {
2607 	.info = {
2608 		.name		= "Samsung S3C2410 UART",
2609 		.type		= TYPE_S3C24XX,
2610 		.port_type	= PORT_S3C2410,
2611 		.fifosize	= 16,
2612 		.rx_fifomask	= S3C2410_UFSTAT_RXMASK,
2613 		.rx_fifoshift	= S3C2410_UFSTAT_RXSHIFT,
2614 		.rx_fifofull	= S3C2410_UFSTAT_RXFULL,
2615 		.tx_fifofull	= S3C2410_UFSTAT_TXFULL,
2616 		.tx_fifomask	= S3C2410_UFSTAT_TXMASK,
2617 		.tx_fifoshift	= S3C2410_UFSTAT_TXSHIFT,
2618 		.def_clk_sel	= S3C2410_UCON_CLKSEL0,
2619 		.num_clks	= 2,
2620 		.clksel_mask	= S3C2410_UCON_CLKMASK,
2621 		.clksel_shift	= S3C2410_UCON_CLKSHIFT,
2622 	},
2623 	.def_cfg = {
2624 		.ucon		= S3C2410_UCON_DEFAULT,
2625 		.ufcon		= S3C2410_UFCON_DEFAULT,
2626 	},
2627 };
2628 #define S3C2410_SERIAL_DRV_DATA (&s3c2410_serial_drv_data)
2629 #else
2630 #define S3C2410_SERIAL_DRV_DATA NULL
2631 #endif
2632 
2633 #ifdef CONFIG_CPU_S3C2412
2634 static const struct s3c24xx_serial_drv_data s3c2412_serial_drv_data = {
2635 	.info = {
2636 		.name		= "Samsung S3C2412 UART",
2637 		.type		= TYPE_S3C24XX,
2638 		.port_type	= PORT_S3C2412,
2639 		.fifosize	= 64,
2640 		.has_divslot	= 1,
2641 		.rx_fifomask	= S3C2440_UFSTAT_RXMASK,
2642 		.rx_fifoshift	= S3C2440_UFSTAT_RXSHIFT,
2643 		.rx_fifofull	= S3C2440_UFSTAT_RXFULL,
2644 		.tx_fifofull	= S3C2440_UFSTAT_TXFULL,
2645 		.tx_fifomask	= S3C2440_UFSTAT_TXMASK,
2646 		.tx_fifoshift	= S3C2440_UFSTAT_TXSHIFT,
2647 		.def_clk_sel	= S3C2410_UCON_CLKSEL2,
2648 		.num_clks	= 4,
2649 		.clksel_mask	= S3C2412_UCON_CLKMASK,
2650 		.clksel_shift	= S3C2412_UCON_CLKSHIFT,
2651 	},
2652 	.def_cfg = {
2653 		.ucon		= S3C2410_UCON_DEFAULT,
2654 		.ufcon		= S3C2410_UFCON_DEFAULT,
2655 	},
2656 };
2657 #define S3C2412_SERIAL_DRV_DATA (&s3c2412_serial_drv_data)
2658 #else
2659 #define S3C2412_SERIAL_DRV_DATA NULL
2660 #endif
2661 
2662 #if defined(CONFIG_CPU_S3C2440) || defined(CONFIG_CPU_S3C2416) || \
2663 	defined(CONFIG_CPU_S3C2443) || defined(CONFIG_CPU_S3C2442)
2664 static const struct s3c24xx_serial_drv_data s3c2440_serial_drv_data = {
2665 	.info = {
2666 		.name		= "Samsung S3C2440 UART",
2667 		.type		= TYPE_S3C24XX,
2668 		.port_type	= PORT_S3C2440,
2669 		.fifosize	= 64,
2670 		.has_divslot	= 1,
2671 		.rx_fifomask	= S3C2440_UFSTAT_RXMASK,
2672 		.rx_fifoshift	= S3C2440_UFSTAT_RXSHIFT,
2673 		.rx_fifofull	= S3C2440_UFSTAT_RXFULL,
2674 		.tx_fifofull	= S3C2440_UFSTAT_TXFULL,
2675 		.tx_fifomask	= S3C2440_UFSTAT_TXMASK,
2676 		.tx_fifoshift	= S3C2440_UFSTAT_TXSHIFT,
2677 		.def_clk_sel	= S3C2410_UCON_CLKSEL2,
2678 		.num_clks	= 4,
2679 		.clksel_mask	= S3C2412_UCON_CLKMASK,
2680 		.clksel_shift	= S3C2412_UCON_CLKSHIFT,
2681 		.ucon_mask	= S3C2440_UCON0_DIVMASK,
2682 	},
2683 	.def_cfg = {
2684 		.ucon		= S3C2410_UCON_DEFAULT,
2685 		.ufcon		= S3C2410_UFCON_DEFAULT,
2686 	},
2687 };
2688 #define S3C2440_SERIAL_DRV_DATA (&s3c2440_serial_drv_data)
2689 #else
2690 #define S3C2440_SERIAL_DRV_DATA NULL
2691 #endif
2692 
2693 #if defined(CONFIG_CPU_S3C6400) || defined(CONFIG_CPU_S3C6410)
2694 static const struct s3c24xx_serial_drv_data s3c6400_serial_drv_data = {
2695 	.info = {
2696 		.name		= "Samsung S3C6400 UART",
2697 		.type		= TYPE_S3C6400,
2698 		.port_type	= PORT_S3C6400,
2699 		.fifosize	= 64,
2700 		.has_divslot	= 1,
2701 		.rx_fifomask	= S3C2440_UFSTAT_RXMASK,
2702 		.rx_fifoshift	= S3C2440_UFSTAT_RXSHIFT,
2703 		.rx_fifofull	= S3C2440_UFSTAT_RXFULL,
2704 		.tx_fifofull	= S3C2440_UFSTAT_TXFULL,
2705 		.tx_fifomask	= S3C2440_UFSTAT_TXMASK,
2706 		.tx_fifoshift	= S3C2440_UFSTAT_TXSHIFT,
2707 		.def_clk_sel	= S3C2410_UCON_CLKSEL2,
2708 		.num_clks	= 4,
2709 		.clksel_mask	= S3C6400_UCON_CLKMASK,
2710 		.clksel_shift	= S3C6400_UCON_CLKSHIFT,
2711 	},
2712 	.def_cfg = {
2713 		.ucon		= S3C2410_UCON_DEFAULT,
2714 		.ufcon		= S3C2410_UFCON_DEFAULT,
2715 	},
2716 };
2717 #define S3C6400_SERIAL_DRV_DATA (&s3c6400_serial_drv_data)
2718 #else
2719 #define S3C6400_SERIAL_DRV_DATA NULL
2720 #endif
2721 
2722 #ifdef CONFIG_CPU_S5PV210
2723 static const struct s3c24xx_serial_drv_data s5pv210_serial_drv_data = {
2724 	.info = {
2725 		.name		= "Samsung S5PV210 UART",
2726 		.type		= TYPE_S3C6400,
2727 		.port_type	= PORT_S3C6400,
2728 		.has_divslot	= 1,
2729 		.rx_fifomask	= S5PV210_UFSTAT_RXMASK,
2730 		.rx_fifoshift	= S5PV210_UFSTAT_RXSHIFT,
2731 		.rx_fifofull	= S5PV210_UFSTAT_RXFULL,
2732 		.tx_fifofull	= S5PV210_UFSTAT_TXFULL,
2733 		.tx_fifomask	= S5PV210_UFSTAT_TXMASK,
2734 		.tx_fifoshift	= S5PV210_UFSTAT_TXSHIFT,
2735 		.def_clk_sel	= S3C2410_UCON_CLKSEL0,
2736 		.num_clks	= 2,
2737 		.clksel_mask	= S5PV210_UCON_CLKMASK,
2738 		.clksel_shift	= S5PV210_UCON_CLKSHIFT,
2739 	},
2740 	.def_cfg = {
2741 		.ucon		= S5PV210_UCON_DEFAULT,
2742 		.ufcon		= S5PV210_UFCON_DEFAULT,
2743 	},
2744 	.fifosize = { 256, 64, 16, 16 },
2745 };
2746 #define S5PV210_SERIAL_DRV_DATA (&s5pv210_serial_drv_data)
2747 #else
2748 #define S5PV210_SERIAL_DRV_DATA	NULL
2749 #endif
2750 
2751 #if defined(CONFIG_ARCH_EXYNOS)
2752 #define EXYNOS_COMMON_SERIAL_DRV_DATA()				\
2753 	.info = {						\
2754 		.name		= "Samsung Exynos UART",	\
2755 		.type		= TYPE_S3C6400,			\
2756 		.port_type	= PORT_S3C6400,			\
2757 		.has_divslot	= 1,				\
2758 		.rx_fifomask	= S5PV210_UFSTAT_RXMASK,	\
2759 		.rx_fifoshift	= S5PV210_UFSTAT_RXSHIFT,	\
2760 		.rx_fifofull	= S5PV210_UFSTAT_RXFULL,	\
2761 		.tx_fifofull	= S5PV210_UFSTAT_TXFULL,	\
2762 		.tx_fifomask	= S5PV210_UFSTAT_TXMASK,	\
2763 		.tx_fifoshift	= S5PV210_UFSTAT_TXSHIFT,	\
2764 		.def_clk_sel	= S3C2410_UCON_CLKSEL0,		\
2765 		.num_clks	= 1,				\
2766 		.clksel_mask	= 0,				\
2767 		.clksel_shift	= 0,				\
2768 	},							\
2769 	.def_cfg = {						\
2770 		.ucon		= S5PV210_UCON_DEFAULT,		\
2771 		.ufcon		= S5PV210_UFCON_DEFAULT,	\
2772 		.has_fracval	= 1,				\
2773 	}							\
2774 
2775 static const struct s3c24xx_serial_drv_data exynos4210_serial_drv_data = {
2776 	EXYNOS_COMMON_SERIAL_DRV_DATA(),
2777 	.fifosize = { 256, 64, 16, 16 },
2778 };
2779 
2780 static const struct s3c24xx_serial_drv_data exynos5433_serial_drv_data = {
2781 	EXYNOS_COMMON_SERIAL_DRV_DATA(),
2782 	.fifosize = { 64, 256, 16, 256 },
2783 };
2784 
2785 static const struct s3c24xx_serial_drv_data exynos850_serial_drv_data = {
2786 	EXYNOS_COMMON_SERIAL_DRV_DATA(),
2787 	.fifosize = { 256, 64, 64, 64 },
2788 };
2789 
2790 #define EXYNOS4210_SERIAL_DRV_DATA (&exynos4210_serial_drv_data)
2791 #define EXYNOS5433_SERIAL_DRV_DATA (&exynos5433_serial_drv_data)
2792 #define EXYNOS850_SERIAL_DRV_DATA (&exynos850_serial_drv_data)
2793 
2794 #else
2795 #define EXYNOS4210_SERIAL_DRV_DATA NULL
2796 #define EXYNOS5433_SERIAL_DRV_DATA NULL
2797 #define EXYNOS850_SERIAL_DRV_DATA NULL
2798 #endif
2799 
2800 #ifdef CONFIG_ARCH_APPLE
2801 static const struct s3c24xx_serial_drv_data s5l_serial_drv_data = {
2802 	.info = {
2803 		.name		= "Apple S5L UART",
2804 		.type		= TYPE_APPLE_S5L,
2805 		.port_type	= PORT_8250,
2806 		.fifosize	= 16,
2807 		.rx_fifomask	= S3C2410_UFSTAT_RXMASK,
2808 		.rx_fifoshift	= S3C2410_UFSTAT_RXSHIFT,
2809 		.rx_fifofull	= S3C2410_UFSTAT_RXFULL,
2810 		.tx_fifofull	= S3C2410_UFSTAT_TXFULL,
2811 		.tx_fifomask	= S3C2410_UFSTAT_TXMASK,
2812 		.tx_fifoshift	= S3C2410_UFSTAT_TXSHIFT,
2813 		.def_clk_sel	= S3C2410_UCON_CLKSEL0,
2814 		.num_clks	= 1,
2815 		.clksel_mask	= 0,
2816 		.clksel_shift	= 0,
2817 	},
2818 	.def_cfg = {
2819 		.ucon		= APPLE_S5L_UCON_DEFAULT,
2820 		.ufcon		= S3C2410_UFCON_DEFAULT,
2821 	},
2822 };
2823 #define S5L_SERIAL_DRV_DATA (&s5l_serial_drv_data)
2824 #else
2825 #define S5L_SERIAL_DRV_DATA NULL
2826 #endif
2827 
2828 #if defined(CONFIG_ARCH_ARTPEC)
2829 static const struct s3c24xx_serial_drv_data artpec8_serial_drv_data = {
2830 	.info = {
2831 		.name		= "Axis ARTPEC-8 UART",
2832 		.type		= TYPE_S3C6400,
2833 		.port_type	= PORT_S3C6400,
2834 		.fifosize	= 64,
2835 		.has_divslot	= 1,
2836 		.rx_fifomask	= S5PV210_UFSTAT_RXMASK,
2837 		.rx_fifoshift	= S5PV210_UFSTAT_RXSHIFT,
2838 		.rx_fifofull	= S5PV210_UFSTAT_RXFULL,
2839 		.tx_fifofull	= S5PV210_UFSTAT_TXFULL,
2840 		.tx_fifomask	= S5PV210_UFSTAT_TXMASK,
2841 		.tx_fifoshift	= S5PV210_UFSTAT_TXSHIFT,
2842 		.def_clk_sel	= S3C2410_UCON_CLKSEL0,
2843 		.num_clks	= 1,
2844 		.clksel_mask	= 0,
2845 		.clksel_shift	= 0,
2846 	},
2847 	.def_cfg = {
2848 		.ucon		= S5PV210_UCON_DEFAULT,
2849 		.ufcon		= S5PV210_UFCON_DEFAULT,
2850 		.has_fracval	= 1,
2851 	}
2852 };
2853 #define ARTPEC8_SERIAL_DRV_DATA (&artpec8_serial_drv_data)
2854 #else
2855 #define ARTPEC8_SERIAL_DRV_DATA (NULL)
2856 #endif
2857 
2858 static const struct platform_device_id s3c24xx_serial_driver_ids[] = {
2859 	{
2860 		.name		= "s3c2410-uart",
2861 		.driver_data	= (kernel_ulong_t)S3C2410_SERIAL_DRV_DATA,
2862 	}, {
2863 		.name		= "s3c2412-uart",
2864 		.driver_data	= (kernel_ulong_t)S3C2412_SERIAL_DRV_DATA,
2865 	}, {
2866 		.name		= "s3c2440-uart",
2867 		.driver_data	= (kernel_ulong_t)S3C2440_SERIAL_DRV_DATA,
2868 	}, {
2869 		.name		= "s3c6400-uart",
2870 		.driver_data	= (kernel_ulong_t)S3C6400_SERIAL_DRV_DATA,
2871 	}, {
2872 		.name		= "s5pv210-uart",
2873 		.driver_data	= (kernel_ulong_t)S5PV210_SERIAL_DRV_DATA,
2874 	}, {
2875 		.name		= "exynos4210-uart",
2876 		.driver_data	= (kernel_ulong_t)EXYNOS4210_SERIAL_DRV_DATA,
2877 	}, {
2878 		.name		= "exynos5433-uart",
2879 		.driver_data	= (kernel_ulong_t)EXYNOS5433_SERIAL_DRV_DATA,
2880 	}, {
2881 		.name		= "s5l-uart",
2882 		.driver_data	= (kernel_ulong_t)S5L_SERIAL_DRV_DATA,
2883 	}, {
2884 		.name		= "exynos850-uart",
2885 		.driver_data	= (kernel_ulong_t)EXYNOS850_SERIAL_DRV_DATA,
2886 	}, {
2887 		.name		= "artpec8-uart",
2888 		.driver_data	= (kernel_ulong_t)ARTPEC8_SERIAL_DRV_DATA,
2889 	},
2890 	{ },
2891 };
2892 MODULE_DEVICE_TABLE(platform, s3c24xx_serial_driver_ids);
2893 
2894 #ifdef CONFIG_OF
2895 static const struct of_device_id s3c24xx_uart_dt_match[] = {
2896 	{ .compatible = "samsung,s3c2410-uart",
2897 		.data = S3C2410_SERIAL_DRV_DATA },
2898 	{ .compatible = "samsung,s3c2412-uart",
2899 		.data = S3C2412_SERIAL_DRV_DATA },
2900 	{ .compatible = "samsung,s3c2440-uart",
2901 		.data = S3C2440_SERIAL_DRV_DATA },
2902 	{ .compatible = "samsung,s3c6400-uart",
2903 		.data = S3C6400_SERIAL_DRV_DATA },
2904 	{ .compatible = "samsung,s5pv210-uart",
2905 		.data = S5PV210_SERIAL_DRV_DATA },
2906 	{ .compatible = "samsung,exynos4210-uart",
2907 		.data = EXYNOS4210_SERIAL_DRV_DATA },
2908 	{ .compatible = "samsung,exynos5433-uart",
2909 		.data = EXYNOS5433_SERIAL_DRV_DATA },
2910 	{ .compatible = "apple,s5l-uart",
2911 		.data = S5L_SERIAL_DRV_DATA },
2912 	{ .compatible = "samsung,exynos850-uart",
2913 		.data = EXYNOS850_SERIAL_DRV_DATA },
2914 	{ .compatible = "axis,artpec8-uart",
2915 		.data = ARTPEC8_SERIAL_DRV_DATA },
2916 	{},
2917 };
2918 MODULE_DEVICE_TABLE(of, s3c24xx_uart_dt_match);
2919 #endif
2920 
2921 static struct platform_driver samsung_serial_driver = {
2922 	.probe		= s3c24xx_serial_probe,
2923 	.remove		= s3c24xx_serial_remove,
2924 	.id_table	= s3c24xx_serial_driver_ids,
2925 	.driver		= {
2926 		.name	= "samsung-uart",
2927 		.pm	= SERIAL_SAMSUNG_PM_OPS,
2928 		.of_match_table	= of_match_ptr(s3c24xx_uart_dt_match),
2929 	},
2930 };
2931 
2932 static int __init samsung_serial_init(void)
2933 {
2934 	int ret;
2935 
2936 	s3c24xx_serial_register_console();
2937 
2938 	ret = platform_driver_register(&samsung_serial_driver);
2939 	if (ret) {
2940 		s3c24xx_serial_unregister_console();
2941 		return ret;
2942 	}
2943 
2944 	return 0;
2945 }
2946 
2947 static void __exit samsung_serial_exit(void)
2948 {
2949 	platform_driver_unregister(&samsung_serial_driver);
2950 	s3c24xx_serial_unregister_console();
2951 }
2952 
2953 module_init(samsung_serial_init);
2954 module_exit(samsung_serial_exit);
2955 
2956 #ifdef CONFIG_SERIAL_SAMSUNG_CONSOLE
2957 /*
2958  * Early console.
2959  */
2960 
2961 static void wr_reg_barrier(const struct uart_port *port, u32 reg, u32 val)
2962 {
2963 	switch (port->iotype) {
2964 	case UPIO_MEM:
2965 		writeb(val, portaddr(port, reg));
2966 		break;
2967 	case UPIO_MEM32:
2968 		writel(val, portaddr(port, reg));
2969 		break;
2970 	}
2971 }
2972 
2973 struct samsung_early_console_data {
2974 	u32 txfull_mask;
2975 	u32 rxfifo_mask;
2976 };
2977 
2978 static void samsung_early_busyuart(const struct uart_port *port)
2979 {
2980 	while (!(readl(port->membase + S3C2410_UTRSTAT) & S3C2410_UTRSTAT_TXFE))
2981 		;
2982 }
2983 
2984 static void samsung_early_busyuart_fifo(const struct uart_port *port)
2985 {
2986 	const struct samsung_early_console_data *data = port->private_data;
2987 
2988 	while (readl(port->membase + S3C2410_UFSTAT) & data->txfull_mask)
2989 		;
2990 }
2991 
2992 static void samsung_early_putc(struct uart_port *port, unsigned char c)
2993 {
2994 	if (readl(port->membase + S3C2410_UFCON) & S3C2410_UFCON_FIFOMODE)
2995 		samsung_early_busyuart_fifo(port);
2996 	else
2997 		samsung_early_busyuart(port);
2998 
2999 	wr_reg_barrier(port, S3C2410_UTXH, c);
3000 }
3001 
3002 static void samsung_early_write(struct console *con, const char *s,
3003 				unsigned int n)
3004 {
3005 	struct earlycon_device *dev = con->data;
3006 
3007 	uart_console_write(&dev->port, s, n, samsung_early_putc);
3008 }
3009 
3010 static int samsung_early_read(struct console *con, char *s, unsigned int n)
3011 {
3012 	struct earlycon_device *dev = con->data;
3013 	const struct samsung_early_console_data *data = dev->port.private_data;
3014 	int ch, ufstat, num_read = 0;
3015 
3016 	while (num_read < n) {
3017 		ufstat = rd_regl(&dev->port, S3C2410_UFSTAT);
3018 		if (!(ufstat & data->rxfifo_mask))
3019 			break;
3020 		ch = rd_reg(&dev->port, S3C2410_URXH);
3021 		if (ch == NO_POLL_CHAR)
3022 			break;
3023 
3024 		s[num_read++] = ch;
3025 	}
3026 
3027 	return num_read;
3028 }
3029 
3030 static int __init samsung_early_console_setup(struct earlycon_device *device,
3031 					      const char *opt)
3032 {
3033 	if (!device->port.membase)
3034 		return -ENODEV;
3035 
3036 	device->con->write = samsung_early_write;
3037 	device->con->read = samsung_early_read;
3038 	return 0;
3039 }
3040 
3041 /* S3C2410 */
3042 static struct samsung_early_console_data s3c2410_early_console_data = {
3043 	.txfull_mask = S3C2410_UFSTAT_TXFULL,
3044 	.rxfifo_mask = S3C2410_UFSTAT_RXFULL | S3C2410_UFSTAT_RXMASK,
3045 };
3046 
3047 static int __init s3c2410_early_console_setup(struct earlycon_device *device,
3048 					      const char *opt)
3049 {
3050 	device->port.private_data = &s3c2410_early_console_data;
3051 	return samsung_early_console_setup(device, opt);
3052 }
3053 
3054 OF_EARLYCON_DECLARE(s3c2410, "samsung,s3c2410-uart",
3055 			s3c2410_early_console_setup);
3056 
3057 /* S3C2412, S3C2440, S3C64xx */
3058 static struct samsung_early_console_data s3c2440_early_console_data = {
3059 	.txfull_mask = S3C2440_UFSTAT_TXFULL,
3060 	.rxfifo_mask = S3C2440_UFSTAT_RXFULL | S3C2440_UFSTAT_RXMASK,
3061 };
3062 
3063 static int __init s3c2440_early_console_setup(struct earlycon_device *device,
3064 					      const char *opt)
3065 {
3066 	device->port.private_data = &s3c2440_early_console_data;
3067 	return samsung_early_console_setup(device, opt);
3068 }
3069 
3070 OF_EARLYCON_DECLARE(s3c2412, "samsung,s3c2412-uart",
3071 			s3c2440_early_console_setup);
3072 OF_EARLYCON_DECLARE(s3c2440, "samsung,s3c2440-uart",
3073 			s3c2440_early_console_setup);
3074 OF_EARLYCON_DECLARE(s3c6400, "samsung,s3c6400-uart",
3075 			s3c2440_early_console_setup);
3076 
3077 /* S5PV210, Exynos */
3078 static struct samsung_early_console_data s5pv210_early_console_data = {
3079 	.txfull_mask = S5PV210_UFSTAT_TXFULL,
3080 	.rxfifo_mask = S5PV210_UFSTAT_RXFULL | S5PV210_UFSTAT_RXMASK,
3081 };
3082 
3083 static int __init s5pv210_early_console_setup(struct earlycon_device *device,
3084 					      const char *opt)
3085 {
3086 	device->port.private_data = &s5pv210_early_console_data;
3087 	return samsung_early_console_setup(device, opt);
3088 }
3089 
3090 OF_EARLYCON_DECLARE(s5pv210, "samsung,s5pv210-uart",
3091 			s5pv210_early_console_setup);
3092 OF_EARLYCON_DECLARE(exynos4210, "samsung,exynos4210-uart",
3093 			s5pv210_early_console_setup);
3094 OF_EARLYCON_DECLARE(artpec8, "axis,artpec8-uart",
3095 			s5pv210_early_console_setup);
3096 
3097 /* Apple S5L */
3098 static int __init apple_s5l_early_console_setup(struct earlycon_device *device,
3099 						const char *opt)
3100 {
3101 	/* Close enough to S3C2410 for earlycon... */
3102 	device->port.private_data = &s3c2410_early_console_data;
3103 
3104 #ifdef CONFIG_ARM64
3105 	/* ... but we need to override the existing fixmap entry as nGnRnE */
3106 	__set_fixmap(FIX_EARLYCON_MEM_BASE, device->port.mapbase,
3107 		     __pgprot(PROT_DEVICE_nGnRnE));
3108 #endif
3109 	return samsung_early_console_setup(device, opt);
3110 }
3111 
3112 OF_EARLYCON_DECLARE(s5l, "apple,s5l-uart", apple_s5l_early_console_setup);
3113 #endif
3114 
3115 MODULE_ALIAS("platform:samsung-uart");
3116 MODULE_DESCRIPTION("Samsung SoC Serial port driver");
3117 MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
3118 MODULE_LICENSE("GPL v2");
3119