1 // SPDX-License-Identifier: GPL-2.0
3 * Driver core for Samsung SoC onboard UARTs.
5 * Ben Dooks, Copyright (c) 2003-2008 Simtec Electronics
6 * http://armlinux.simtec.co.uk/
9 /* Note on 2410 error handling
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.
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
24 #include <linux/dmaengine.h>
25 #include <linux/dma-mapping.h>
26 #include <linux/slab.h>
27 #include <linux/math.h>
28 #include <linux/module.h>
29 #include <linux/ioport.h>
31 #include <linux/platform_device.h>
32 #include <linux/init.h>
33 #include <linux/sysrq.h>
34 #include <linux/console.h>
35 #include <linux/tty.h>
36 #include <linux/tty_flip.h>
37 #include <linux/serial_core.h>
38 #include <linux/serial.h>
39 #include <linux/serial_s3c.h>
40 #include <linux/delay.h>
41 #include <linux/clk.h>
42 #include <linux/cpufreq.h>
46 /* UART name and device definitions */
48 #define S3C24XX_SERIAL_NAME "ttySAC"
49 #define S3C24XX_SERIAL_MAJOR 204
50 #define S3C24XX_SERIAL_MINOR 64
55 #define UART_NR CONFIG_SERIAL_SAMSUNG_UARTS
58 #define S3C24XX_TX_PIO 1
59 #define S3C24XX_TX_DMA 2
60 #define S3C24XX_RX_PIO 1
61 #define S3C24XX_RX_DMA 2
63 /* flag to ignore all characters coming in */
64 #define RXSTAT_DUMMY_READ (0x10000000)
66 enum s3c24xx_port_type {
72 struct s3c24xx_uart_info {
74 enum s3c24xx_port_type type;
75 unsigned int port_type;
76 unsigned int fifosize;
77 unsigned long rx_fifomask;
78 unsigned long rx_fifoshift;
79 unsigned long rx_fifofull;
80 unsigned long tx_fifomask;
81 unsigned long tx_fifoshift;
82 unsigned long tx_fifofull;
83 unsigned int def_clk_sel;
84 unsigned long num_clks;
85 unsigned long clksel_mask;
86 unsigned long clksel_shift;
87 unsigned long ucon_mask;
89 /* uart port features */
91 unsigned int has_divslot:1;
94 struct s3c24xx_serial_drv_data {
95 const struct s3c24xx_uart_info info;
96 const struct s3c2410_uartcfg def_cfg;
97 const unsigned int fifosize[UART_NR];
100 struct s3c24xx_uart_dma {
101 unsigned int rx_chan_id;
102 unsigned int tx_chan_id;
104 struct dma_slave_config rx_conf;
105 struct dma_slave_config tx_conf;
107 struct dma_chan *rx_chan;
108 struct dma_chan *tx_chan;
113 dma_cookie_t rx_cookie;
114 dma_cookie_t tx_cookie;
118 dma_addr_t tx_transfer_addr;
123 struct dma_async_tx_descriptor *tx_desc;
124 struct dma_async_tx_descriptor *rx_desc;
126 int tx_bytes_requested;
127 int rx_bytes_requested;
130 struct s3c24xx_uart_port {
131 unsigned char rx_claimed;
132 unsigned char tx_claimed;
133 unsigned char rx_enabled;
134 unsigned char tx_enabled;
135 unsigned int pm_level;
136 unsigned long baudclk_rate;
137 unsigned int min_dma_size;
142 unsigned int tx_in_progress;
143 unsigned int tx_mode;
144 unsigned int rx_mode;
146 const struct s3c24xx_uart_info *info;
149 struct uart_port port;
150 const struct s3c24xx_serial_drv_data *drv_data;
152 /* reference to platform data */
153 const struct s3c2410_uartcfg *cfg;
155 struct s3c24xx_uart_dma *dma;
158 static void s3c24xx_serial_tx_chars(struct s3c24xx_uart_port *ourport);
160 /* conversion functions */
162 #define s3c24xx_dev_to_port(__dev) dev_get_drvdata(__dev)
164 /* register access controls */
166 #define portaddr(port, reg) ((port)->membase + (reg))
167 #define portaddrl(port, reg) \
168 ((unsigned long *)(unsigned long)((port)->membase + (reg)))
170 static u32 rd_reg(const struct uart_port *port, u32 reg)
172 switch (port->iotype) {
174 return readb_relaxed(portaddr(port, reg));
176 return readl_relaxed(portaddr(port, reg));
183 #define rd_regl(port, reg) (readl_relaxed(portaddr(port, reg)))
185 static void wr_reg(const struct uart_port *port, u32 reg, u32 val)
187 switch (port->iotype) {
189 writeb_relaxed(val, portaddr(port, reg));
192 writel_relaxed(val, portaddr(port, reg));
197 #define wr_regl(port, reg, val) writel_relaxed(val, portaddr(port, reg))
199 /* Byte-order aware bit setting/clearing functions. */
201 static inline void s3c24xx_set_bit(const struct uart_port *port, int idx,
207 local_irq_save(flags);
208 val = rd_regl(port, reg);
210 wr_regl(port, reg, val);
211 local_irq_restore(flags);
214 static inline void s3c24xx_clear_bit(const struct uart_port *port, int idx,
220 local_irq_save(flags);
221 val = rd_regl(port, reg);
223 wr_regl(port, reg, val);
224 local_irq_restore(flags);
227 static inline struct s3c24xx_uart_port *to_ourport(struct uart_port *port)
229 return container_of(port, struct s3c24xx_uart_port, port);
232 /* translate a port to the device name */
234 static inline const char *s3c24xx_serial_portname(const struct uart_port *port)
236 return to_platform_device(port->dev)->name;
239 static int s3c24xx_serial_txempty_nofifo(const struct uart_port *port)
241 return rd_regl(port, S3C2410_UTRSTAT) & S3C2410_UTRSTAT_TXE;
244 static void s3c24xx_serial_rx_enable(struct uart_port *port)
246 struct s3c24xx_uart_port *ourport = to_ourport(port);
248 unsigned int ucon, ufcon;
251 spin_lock_irqsave(&port->lock, flags);
253 while (--count && !s3c24xx_serial_txempty_nofifo(port))
256 ufcon = rd_regl(port, S3C2410_UFCON);
257 ufcon |= S3C2410_UFCON_RESETRX;
258 wr_regl(port, S3C2410_UFCON, ufcon);
260 ucon = rd_regl(port, S3C2410_UCON);
261 ucon |= S3C2410_UCON_RXIRQMODE;
262 wr_regl(port, S3C2410_UCON, ucon);
264 ourport->rx_enabled = 1;
265 spin_unlock_irqrestore(&port->lock, flags);
268 static void s3c24xx_serial_rx_disable(struct uart_port *port)
270 struct s3c24xx_uart_port *ourport = to_ourport(port);
274 spin_lock_irqsave(&port->lock, flags);
276 ucon = rd_regl(port, S3C2410_UCON);
277 ucon &= ~S3C2410_UCON_RXIRQMODE;
278 wr_regl(port, S3C2410_UCON, ucon);
280 ourport->rx_enabled = 0;
281 spin_unlock_irqrestore(&port->lock, flags);
284 static void s3c24xx_serial_stop_tx(struct uart_port *port)
286 struct s3c24xx_uart_port *ourport = to_ourport(port);
287 struct s3c24xx_uart_dma *dma = ourport->dma;
288 struct dma_tx_state state;
291 if (!ourport->tx_enabled)
294 switch (ourport->info->type) {
296 s3c24xx_set_bit(port, S3C64XX_UINTM_TXD, S3C64XX_UINTM);
299 s3c24xx_clear_bit(port, APPLE_S5L_UCON_TXTHRESH_ENA, S3C2410_UCON);
302 disable_irq_nosync(ourport->tx_irq);
306 if (dma && dma->tx_chan && ourport->tx_in_progress == S3C24XX_TX_DMA) {
307 dmaengine_pause(dma->tx_chan);
308 dmaengine_tx_status(dma->tx_chan, dma->tx_cookie, &state);
309 dmaengine_terminate_all(dma->tx_chan);
310 dma_sync_single_for_cpu(dma->tx_chan->device->dev,
311 dma->tx_transfer_addr, dma->tx_size,
313 async_tx_ack(dma->tx_desc);
314 count = dma->tx_bytes_requested - state.residue;
315 uart_xmit_advance(port, count);
318 ourport->tx_enabled = 0;
319 ourport->tx_in_progress = 0;
321 if (port->flags & UPF_CONS_FLOW)
322 s3c24xx_serial_rx_enable(port);
324 ourport->tx_mode = 0;
327 static void s3c24xx_serial_start_next_tx(struct s3c24xx_uart_port *ourport);
329 static void s3c24xx_serial_tx_dma_complete(void *args)
331 struct s3c24xx_uart_port *ourport = args;
332 struct uart_port *port = &ourport->port;
333 struct circ_buf *xmit = &port->state->xmit;
334 struct s3c24xx_uart_dma *dma = ourport->dma;
335 struct dma_tx_state state;
339 dmaengine_tx_status(dma->tx_chan, dma->tx_cookie, &state);
340 count = dma->tx_bytes_requested - state.residue;
341 async_tx_ack(dma->tx_desc);
343 dma_sync_single_for_cpu(dma->tx_chan->device->dev,
344 dma->tx_transfer_addr, dma->tx_size,
347 spin_lock_irqsave(&port->lock, flags);
349 uart_xmit_advance(port, count);
350 ourport->tx_in_progress = 0;
352 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
353 uart_write_wakeup(port);
355 s3c24xx_serial_start_next_tx(ourport);
356 spin_unlock_irqrestore(&port->lock, flags);
359 static void enable_tx_dma(struct s3c24xx_uart_port *ourport)
361 const struct uart_port *port = &ourport->port;
364 /* Mask Tx interrupt */
365 switch (ourport->info->type) {
367 s3c24xx_set_bit(port, S3C64XX_UINTM_TXD, S3C64XX_UINTM);
370 WARN_ON(1); // No DMA
373 disable_irq_nosync(ourport->tx_irq);
377 /* Enable tx dma mode */
378 ucon = rd_regl(port, S3C2410_UCON);
379 ucon &= ~(S3C64XX_UCON_TXBURST_MASK | S3C64XX_UCON_TXMODE_MASK);
380 ucon |= S3C64XX_UCON_TXBURST_1;
381 ucon |= S3C64XX_UCON_TXMODE_DMA;
382 wr_regl(port, S3C2410_UCON, ucon);
384 ourport->tx_mode = S3C24XX_TX_DMA;
387 static void enable_tx_pio(struct s3c24xx_uart_port *ourport)
389 const struct uart_port *port = &ourport->port;
392 /* Set ufcon txtrig */
393 ourport->tx_in_progress = S3C24XX_TX_PIO;
394 ufcon = rd_regl(port, S3C2410_UFCON);
395 wr_regl(port, S3C2410_UFCON, ufcon);
397 /* Enable tx pio mode */
398 ucon = rd_regl(port, S3C2410_UCON);
399 ucon &= ~(S3C64XX_UCON_TXMODE_MASK);
400 ucon |= S3C64XX_UCON_TXMODE_CPU;
401 wr_regl(port, S3C2410_UCON, ucon);
403 /* Unmask Tx interrupt */
404 switch (ourport->info->type) {
406 s3c24xx_clear_bit(port, S3C64XX_UINTM_TXD,
410 ucon |= APPLE_S5L_UCON_TXTHRESH_ENA_MSK;
411 wr_regl(port, S3C2410_UCON, ucon);
414 enable_irq(ourport->tx_irq);
418 ourport->tx_mode = S3C24XX_TX_PIO;
421 * The Apple version only has edge triggered TX IRQs, so we need
422 * to kick off the process by sending some characters here.
424 if (ourport->info->type == TYPE_APPLE_S5L)
425 s3c24xx_serial_tx_chars(ourport);
428 static void s3c24xx_serial_start_tx_pio(struct s3c24xx_uart_port *ourport)
430 if (ourport->tx_mode != S3C24XX_TX_PIO)
431 enable_tx_pio(ourport);
434 static int s3c24xx_serial_start_tx_dma(struct s3c24xx_uart_port *ourport,
437 struct uart_port *port = &ourport->port;
438 struct circ_buf *xmit = &port->state->xmit;
439 struct s3c24xx_uart_dma *dma = ourport->dma;
441 if (ourport->tx_mode != S3C24XX_TX_DMA)
442 enable_tx_dma(ourport);
444 dma->tx_size = count & ~(dma_get_cache_alignment() - 1);
445 dma->tx_transfer_addr = dma->tx_addr + xmit->tail;
447 dma_sync_single_for_device(dma->tx_chan->device->dev,
448 dma->tx_transfer_addr, dma->tx_size,
451 dma->tx_desc = dmaengine_prep_slave_single(dma->tx_chan,
452 dma->tx_transfer_addr, dma->tx_size,
453 DMA_MEM_TO_DEV, DMA_PREP_INTERRUPT);
455 dev_err(ourport->port.dev, "Unable to get desc for Tx\n");
459 dma->tx_desc->callback = s3c24xx_serial_tx_dma_complete;
460 dma->tx_desc->callback_param = ourport;
461 dma->tx_bytes_requested = dma->tx_size;
463 ourport->tx_in_progress = S3C24XX_TX_DMA;
464 dma->tx_cookie = dmaengine_submit(dma->tx_desc);
465 dma_async_issue_pending(dma->tx_chan);
469 static void s3c24xx_serial_start_next_tx(struct s3c24xx_uart_port *ourport)
471 struct uart_port *port = &ourport->port;
472 struct circ_buf *xmit = &port->state->xmit;
475 /* Get data size up to the end of buffer */
476 count = CIRC_CNT_TO_END(xmit->head, xmit->tail, UART_XMIT_SIZE);
479 s3c24xx_serial_stop_tx(port);
483 if (!ourport->dma || !ourport->dma->tx_chan ||
484 count < ourport->min_dma_size ||
485 xmit->tail & (dma_get_cache_alignment() - 1))
486 s3c24xx_serial_start_tx_pio(ourport);
488 s3c24xx_serial_start_tx_dma(ourport, count);
491 static void s3c24xx_serial_start_tx(struct uart_port *port)
493 struct s3c24xx_uart_port *ourport = to_ourport(port);
494 struct circ_buf *xmit = &port->state->xmit;
496 if (!ourport->tx_enabled) {
497 if (port->flags & UPF_CONS_FLOW)
498 s3c24xx_serial_rx_disable(port);
500 ourport->tx_enabled = 1;
501 if (!ourport->dma || !ourport->dma->tx_chan)
502 s3c24xx_serial_start_tx_pio(ourport);
505 if (ourport->dma && ourport->dma->tx_chan) {
506 if (!uart_circ_empty(xmit) && !ourport->tx_in_progress)
507 s3c24xx_serial_start_next_tx(ourport);
511 static void s3c24xx_uart_copy_rx_to_tty(struct s3c24xx_uart_port *ourport,
512 struct tty_port *tty, int count)
514 struct s3c24xx_uart_dma *dma = ourport->dma;
520 dma_sync_single_for_cpu(dma->rx_chan->device->dev, dma->rx_addr,
521 dma->rx_size, DMA_FROM_DEVICE);
523 ourport->port.icount.rx += count;
525 dev_err(ourport->port.dev, "No tty port\n");
528 copied = tty_insert_flip_string(tty,
529 ((unsigned char *)(ourport->dma->rx_buf)), count);
530 if (copied != count) {
532 dev_err(ourport->port.dev, "RxData copy to tty layer failed\n");
536 static void s3c24xx_serial_stop_rx(struct uart_port *port)
538 struct s3c24xx_uart_port *ourport = to_ourport(port);
539 struct s3c24xx_uart_dma *dma = ourport->dma;
540 struct tty_port *t = &port->state->port;
541 struct dma_tx_state state;
542 enum dma_status dma_status;
543 unsigned int received;
545 if (ourport->rx_enabled) {
546 dev_dbg(port->dev, "stopping rx\n");
547 switch (ourport->info->type) {
549 s3c24xx_set_bit(port, S3C64XX_UINTM_RXD,
553 s3c24xx_clear_bit(port, APPLE_S5L_UCON_RXTHRESH_ENA, S3C2410_UCON);
554 s3c24xx_clear_bit(port, APPLE_S5L_UCON_RXTO_ENA, S3C2410_UCON);
557 disable_irq_nosync(ourport->rx_irq);
560 ourport->rx_enabled = 0;
562 if (dma && dma->rx_chan) {
563 dmaengine_pause(dma->tx_chan);
564 dma_status = dmaengine_tx_status(dma->rx_chan,
565 dma->rx_cookie, &state);
566 if (dma_status == DMA_IN_PROGRESS ||
567 dma_status == DMA_PAUSED) {
568 received = dma->rx_bytes_requested - state.residue;
569 dmaengine_terminate_all(dma->rx_chan);
570 s3c24xx_uart_copy_rx_to_tty(ourport, t, received);
575 static inline const struct s3c24xx_uart_info
576 *s3c24xx_port_to_info(struct uart_port *port)
578 return to_ourport(port)->info;
581 static inline const struct s3c2410_uartcfg
582 *s3c24xx_port_to_cfg(const struct uart_port *port)
584 const struct s3c24xx_uart_port *ourport;
586 if (port->dev == NULL)
589 ourport = container_of(port, struct s3c24xx_uart_port, port);
593 static int s3c24xx_serial_rx_fifocnt(const struct s3c24xx_uart_port *ourport,
594 unsigned long ufstat)
596 const struct s3c24xx_uart_info *info = ourport->info;
598 if (ufstat & info->rx_fifofull)
599 return ourport->port.fifosize;
601 return (ufstat & info->rx_fifomask) >> info->rx_fifoshift;
604 static void s3c64xx_start_rx_dma(struct s3c24xx_uart_port *ourport);
605 static void s3c24xx_serial_rx_dma_complete(void *args)
607 struct s3c24xx_uart_port *ourport = args;
608 struct uart_port *port = &ourport->port;
610 struct s3c24xx_uart_dma *dma = ourport->dma;
611 struct tty_port *t = &port->state->port;
612 struct tty_struct *tty = tty_port_tty_get(&ourport->port.state->port);
614 struct dma_tx_state state;
618 dmaengine_tx_status(dma->rx_chan, dma->rx_cookie, &state);
619 received = dma->rx_bytes_requested - state.residue;
620 async_tx_ack(dma->rx_desc);
622 spin_lock_irqsave(&port->lock, flags);
625 s3c24xx_uart_copy_rx_to_tty(ourport, t, received);
628 tty_flip_buffer_push(t);
632 s3c64xx_start_rx_dma(ourport);
634 spin_unlock_irqrestore(&port->lock, flags);
637 static void s3c64xx_start_rx_dma(struct s3c24xx_uart_port *ourport)
639 struct s3c24xx_uart_dma *dma = ourport->dma;
641 dma_sync_single_for_device(dma->rx_chan->device->dev, dma->rx_addr,
642 dma->rx_size, DMA_FROM_DEVICE);
644 dma->rx_desc = dmaengine_prep_slave_single(dma->rx_chan,
645 dma->rx_addr, dma->rx_size, DMA_DEV_TO_MEM,
648 dev_err(ourport->port.dev, "Unable to get desc for Rx\n");
652 dma->rx_desc->callback = s3c24xx_serial_rx_dma_complete;
653 dma->rx_desc->callback_param = ourport;
654 dma->rx_bytes_requested = dma->rx_size;
656 dma->rx_cookie = dmaengine_submit(dma->rx_desc);
657 dma_async_issue_pending(dma->rx_chan);
660 /* ? - where has parity gone?? */
661 #define S3C2410_UERSTAT_PARITY (0x1000)
663 static void enable_rx_dma(struct s3c24xx_uart_port *ourport)
665 struct uart_port *port = &ourport->port;
668 /* set Rx mode to DMA mode */
669 ucon = rd_regl(port, S3C2410_UCON);
670 ucon &= ~(S3C64XX_UCON_RXBURST_MASK |
671 S3C64XX_UCON_TIMEOUT_MASK |
672 S3C64XX_UCON_EMPTYINT_EN |
673 S3C64XX_UCON_DMASUS_EN |
674 S3C64XX_UCON_TIMEOUT_EN |
675 S3C64XX_UCON_RXMODE_MASK);
676 ucon |= S3C64XX_UCON_RXBURST_1 |
677 0xf << S3C64XX_UCON_TIMEOUT_SHIFT |
678 S3C64XX_UCON_EMPTYINT_EN |
679 S3C64XX_UCON_TIMEOUT_EN |
680 S3C64XX_UCON_RXMODE_DMA;
681 wr_regl(port, S3C2410_UCON, ucon);
683 ourport->rx_mode = S3C24XX_RX_DMA;
686 static void enable_rx_pio(struct s3c24xx_uart_port *ourport)
688 struct uart_port *port = &ourport->port;
691 /* set Rx mode to DMA mode */
692 ucon = rd_regl(port, S3C2410_UCON);
693 ucon &= ~S3C64XX_UCON_RXMODE_MASK;
694 ucon |= S3C64XX_UCON_RXMODE_CPU;
696 /* Apple types use these bits for IRQ masks */
697 if (ourport->info->type != TYPE_APPLE_S5L) {
698 ucon &= ~(S3C64XX_UCON_TIMEOUT_MASK |
699 S3C64XX_UCON_EMPTYINT_EN |
700 S3C64XX_UCON_DMASUS_EN |
701 S3C64XX_UCON_TIMEOUT_EN);
702 ucon |= 0xf << S3C64XX_UCON_TIMEOUT_SHIFT |
703 S3C64XX_UCON_TIMEOUT_EN;
705 wr_regl(port, S3C2410_UCON, ucon);
707 ourport->rx_mode = S3C24XX_RX_PIO;
710 static void s3c24xx_serial_rx_drain_fifo(struct s3c24xx_uart_port *ourport);
712 static irqreturn_t s3c24xx_serial_rx_chars_dma(void *dev_id)
714 unsigned int utrstat, received;
715 struct s3c24xx_uart_port *ourport = dev_id;
716 struct uart_port *port = &ourport->port;
717 struct s3c24xx_uart_dma *dma = ourport->dma;
718 struct tty_struct *tty = tty_port_tty_get(&ourport->port.state->port);
719 struct tty_port *t = &port->state->port;
720 struct dma_tx_state state;
722 utrstat = rd_regl(port, S3C2410_UTRSTAT);
723 rd_regl(port, S3C2410_UFSTAT);
725 spin_lock(&port->lock);
727 if (!(utrstat & S3C2410_UTRSTAT_TIMEOUT)) {
728 s3c64xx_start_rx_dma(ourport);
729 if (ourport->rx_mode == S3C24XX_RX_PIO)
730 enable_rx_dma(ourport);
734 if (ourport->rx_mode == S3C24XX_RX_DMA) {
735 dmaengine_pause(dma->rx_chan);
736 dmaengine_tx_status(dma->rx_chan, dma->rx_cookie, &state);
737 dmaengine_terminate_all(dma->rx_chan);
738 received = dma->rx_bytes_requested - state.residue;
739 s3c24xx_uart_copy_rx_to_tty(ourport, t, received);
741 enable_rx_pio(ourport);
744 s3c24xx_serial_rx_drain_fifo(ourport);
747 tty_flip_buffer_push(t);
751 wr_regl(port, S3C2410_UTRSTAT, S3C2410_UTRSTAT_TIMEOUT);
754 spin_unlock(&port->lock);
759 static void s3c24xx_serial_rx_drain_fifo(struct s3c24xx_uart_port *ourport)
761 struct uart_port *port = &ourport->port;
762 unsigned int ufcon, ch, flag, ufstat, uerstat;
763 unsigned int fifocnt = 0;
764 int max_count = port->fifosize;
766 while (max_count-- > 0) {
768 * Receive all characters known to be in FIFO
769 * before reading FIFO level again
772 ufstat = rd_regl(port, S3C2410_UFSTAT);
773 fifocnt = s3c24xx_serial_rx_fifocnt(ourport, ufstat);
779 uerstat = rd_regl(port, S3C2410_UERSTAT);
780 ch = rd_reg(port, S3C2410_URXH);
782 if (port->flags & UPF_CONS_FLOW) {
783 int txe = s3c24xx_serial_txempty_nofifo(port);
785 if (ourport->rx_enabled) {
787 ourport->rx_enabled = 0;
792 ufcon = rd_regl(port, S3C2410_UFCON);
793 ufcon |= S3C2410_UFCON_RESETRX;
794 wr_regl(port, S3C2410_UFCON, ufcon);
795 ourport->rx_enabled = 1;
802 /* insert the character into the buffer */
807 if (unlikely(uerstat & S3C2410_UERSTAT_ANY)) {
809 "rxerr: port ch=0x%02x, rxs=0x%08x\n",
812 /* check for break */
813 if (uerstat & S3C2410_UERSTAT_BREAK) {
814 dev_dbg(port->dev, "break!\n");
816 if (uart_handle_break(port))
817 continue; /* Ignore character */
820 if (uerstat & S3C2410_UERSTAT_FRAME)
821 port->icount.frame++;
822 if (uerstat & S3C2410_UERSTAT_OVERRUN)
823 port->icount.overrun++;
825 uerstat &= port->read_status_mask;
827 if (uerstat & S3C2410_UERSTAT_BREAK)
829 else if (uerstat & S3C2410_UERSTAT_PARITY)
831 else if (uerstat & (S3C2410_UERSTAT_FRAME |
832 S3C2410_UERSTAT_OVERRUN))
836 if (uart_handle_sysrq_char(port, ch))
837 continue; /* Ignore character */
839 uart_insert_char(port, uerstat, S3C2410_UERSTAT_OVERRUN,
843 tty_flip_buffer_push(&port->state->port);
846 static irqreturn_t s3c24xx_serial_rx_chars_pio(void *dev_id)
848 struct s3c24xx_uart_port *ourport = dev_id;
849 struct uart_port *port = &ourport->port;
851 spin_lock(&port->lock);
852 s3c24xx_serial_rx_drain_fifo(ourport);
853 spin_unlock(&port->lock);
858 static irqreturn_t s3c24xx_serial_rx_irq(int irq, void *dev_id)
860 struct s3c24xx_uart_port *ourport = dev_id;
862 if (ourport->dma && ourport->dma->rx_chan)
863 return s3c24xx_serial_rx_chars_dma(dev_id);
864 return s3c24xx_serial_rx_chars_pio(dev_id);
867 static void s3c24xx_serial_tx_chars(struct s3c24xx_uart_port *ourport)
869 struct uart_port *port = &ourport->port;
870 struct circ_buf *xmit = &port->state->xmit;
871 int count, dma_count = 0;
873 count = CIRC_CNT_TO_END(xmit->head, xmit->tail, UART_XMIT_SIZE);
875 if (ourport->dma && ourport->dma->tx_chan &&
876 count >= ourport->min_dma_size) {
877 int align = dma_get_cache_alignment() -
878 (xmit->tail & (dma_get_cache_alignment() - 1));
879 if (count - align >= ourport->min_dma_size) {
880 dma_count = count - align;
886 wr_reg(port, S3C2410_UTXH, port->x_char);
892 /* if there isn't anything more to transmit, or the uart is now
893 * stopped, disable the uart and exit
896 if (uart_circ_empty(xmit) || uart_tx_stopped(port)) {
897 s3c24xx_serial_stop_tx(port);
901 /* try and drain the buffer... */
903 if (count > port->fifosize) {
904 count = port->fifosize;
908 while (!uart_circ_empty(xmit) && count > 0) {
909 if (rd_regl(port, S3C2410_UFSTAT) & ourport->info->tx_fifofull)
912 wr_reg(port, S3C2410_UTXH, xmit->buf[xmit->tail]);
913 uart_xmit_advance(port, 1);
917 if (!count && dma_count) {
918 s3c24xx_serial_start_tx_dma(ourport, dma_count);
922 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
923 uart_write_wakeup(port);
925 if (uart_circ_empty(xmit))
926 s3c24xx_serial_stop_tx(port);
929 static irqreturn_t s3c24xx_serial_tx_irq(int irq, void *id)
931 struct s3c24xx_uart_port *ourport = id;
932 struct uart_port *port = &ourport->port;
934 spin_lock(&port->lock);
936 s3c24xx_serial_tx_chars(ourport);
938 spin_unlock(&port->lock);
942 /* interrupt handler for s3c64xx and later SoC's.*/
943 static irqreturn_t s3c64xx_serial_handle_irq(int irq, void *id)
945 const struct s3c24xx_uart_port *ourport = id;
946 const struct uart_port *port = &ourport->port;
947 unsigned int pend = rd_regl(port, S3C64XX_UINTP);
948 irqreturn_t ret = IRQ_HANDLED;
950 if (pend & S3C64XX_UINTM_RXD_MSK) {
951 ret = s3c24xx_serial_rx_irq(irq, id);
952 wr_regl(port, S3C64XX_UINTP, S3C64XX_UINTM_RXD_MSK);
954 if (pend & S3C64XX_UINTM_TXD_MSK) {
955 ret = s3c24xx_serial_tx_irq(irq, id);
956 wr_regl(port, S3C64XX_UINTP, S3C64XX_UINTM_TXD_MSK);
961 /* interrupt handler for Apple SoC's.*/
962 static irqreturn_t apple_serial_handle_irq(int irq, void *id)
964 const struct s3c24xx_uart_port *ourport = id;
965 const struct uart_port *port = &ourport->port;
966 unsigned int pend = rd_regl(port, S3C2410_UTRSTAT);
967 irqreturn_t ret = IRQ_NONE;
969 if (pend & (APPLE_S5L_UTRSTAT_RXTHRESH | APPLE_S5L_UTRSTAT_RXTO)) {
970 wr_regl(port, S3C2410_UTRSTAT,
971 APPLE_S5L_UTRSTAT_RXTHRESH | APPLE_S5L_UTRSTAT_RXTO);
972 ret = s3c24xx_serial_rx_irq(irq, id);
974 if (pend & APPLE_S5L_UTRSTAT_TXTHRESH) {
975 wr_regl(port, S3C2410_UTRSTAT, APPLE_S5L_UTRSTAT_TXTHRESH);
976 ret = s3c24xx_serial_tx_irq(irq, id);
982 static unsigned int s3c24xx_serial_tx_empty(struct uart_port *port)
984 const struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
985 unsigned long ufstat = rd_regl(port, S3C2410_UFSTAT);
986 unsigned long ufcon = rd_regl(port, S3C2410_UFCON);
988 if (ufcon & S3C2410_UFCON_FIFOMODE) {
989 if ((ufstat & info->tx_fifomask) != 0 ||
990 (ufstat & info->tx_fifofull))
996 return s3c24xx_serial_txempty_nofifo(port);
999 /* no modem control lines */
1000 static unsigned int s3c24xx_serial_get_mctrl(struct uart_port *port)
1002 unsigned int umstat = rd_reg(port, S3C2410_UMSTAT);
1004 if (umstat & S3C2410_UMSTAT_CTS)
1005 return TIOCM_CAR | TIOCM_DSR | TIOCM_CTS;
1007 return TIOCM_CAR | TIOCM_DSR;
1010 static void s3c24xx_serial_set_mctrl(struct uart_port *port, unsigned int mctrl)
1012 unsigned int umcon = rd_regl(port, S3C2410_UMCON);
1013 unsigned int ucon = rd_regl(port, S3C2410_UCON);
1015 if (mctrl & TIOCM_RTS)
1016 umcon |= S3C2410_UMCOM_RTS_LOW;
1018 umcon &= ~S3C2410_UMCOM_RTS_LOW;
1020 wr_regl(port, S3C2410_UMCON, umcon);
1022 if (mctrl & TIOCM_LOOP)
1023 ucon |= S3C2410_UCON_LOOPBACK;
1025 ucon &= ~S3C2410_UCON_LOOPBACK;
1027 wr_regl(port, S3C2410_UCON, ucon);
1030 static void s3c24xx_serial_break_ctl(struct uart_port *port, int break_state)
1032 unsigned long flags;
1035 spin_lock_irqsave(&port->lock, flags);
1037 ucon = rd_regl(port, S3C2410_UCON);
1040 ucon |= S3C2410_UCON_SBREAK;
1042 ucon &= ~S3C2410_UCON_SBREAK;
1044 wr_regl(port, S3C2410_UCON, ucon);
1046 spin_unlock_irqrestore(&port->lock, flags);
1049 static int s3c24xx_serial_request_dma(struct s3c24xx_uart_port *p)
1051 struct s3c24xx_uart_dma *dma = p->dma;
1052 struct dma_slave_caps dma_caps;
1053 const char *reason = NULL;
1056 /* Default slave configuration parameters */
1057 dma->rx_conf.direction = DMA_DEV_TO_MEM;
1058 dma->rx_conf.src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
1059 dma->rx_conf.src_addr = p->port.mapbase + S3C2410_URXH;
1060 dma->rx_conf.src_maxburst = 1;
1062 dma->tx_conf.direction = DMA_MEM_TO_DEV;
1063 dma->tx_conf.dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
1064 dma->tx_conf.dst_addr = p->port.mapbase + S3C2410_UTXH;
1065 dma->tx_conf.dst_maxburst = 1;
1067 dma->rx_chan = dma_request_chan(p->port.dev, "rx");
1069 if (IS_ERR(dma->rx_chan)) {
1070 reason = "DMA RX channel request failed";
1071 ret = PTR_ERR(dma->rx_chan);
1075 ret = dma_get_slave_caps(dma->rx_chan, &dma_caps);
1077 dma_caps.residue_granularity < DMA_RESIDUE_GRANULARITY_BURST) {
1078 reason = "insufficient DMA RX engine capabilities";
1080 goto err_release_rx;
1083 dmaengine_slave_config(dma->rx_chan, &dma->rx_conf);
1085 dma->tx_chan = dma_request_chan(p->port.dev, "tx");
1086 if (IS_ERR(dma->tx_chan)) {
1087 reason = "DMA TX channel request failed";
1088 ret = PTR_ERR(dma->tx_chan);
1089 goto err_release_rx;
1092 ret = dma_get_slave_caps(dma->tx_chan, &dma_caps);
1094 dma_caps.residue_granularity < DMA_RESIDUE_GRANULARITY_BURST) {
1095 reason = "insufficient DMA TX engine capabilities";
1097 goto err_release_tx;
1100 dmaengine_slave_config(dma->tx_chan, &dma->tx_conf);
1103 dma->rx_size = PAGE_SIZE;
1105 dma->rx_buf = kmalloc(dma->rx_size, GFP_KERNEL);
1108 goto err_release_tx;
1111 dma->rx_addr = dma_map_single(dma->rx_chan->device->dev, dma->rx_buf,
1112 dma->rx_size, DMA_FROM_DEVICE);
1113 if (dma_mapping_error(dma->rx_chan->device->dev, dma->rx_addr)) {
1114 reason = "DMA mapping error for RX buffer";
1120 dma->tx_addr = dma_map_single(dma->tx_chan->device->dev,
1121 p->port.state->xmit.buf, UART_XMIT_SIZE,
1123 if (dma_mapping_error(dma->tx_chan->device->dev, dma->tx_addr)) {
1124 reason = "DMA mapping error for TX buffer";
1132 dma_unmap_single(dma->rx_chan->device->dev, dma->rx_addr,
1133 dma->rx_size, DMA_FROM_DEVICE);
1137 dma_release_channel(dma->tx_chan);
1139 dma_release_channel(dma->rx_chan);
1142 dev_warn(p->port.dev, "%s, DMA will not be used\n", reason);
1146 static void s3c24xx_serial_release_dma(struct s3c24xx_uart_port *p)
1148 struct s3c24xx_uart_dma *dma = p->dma;
1151 dmaengine_terminate_all(dma->rx_chan);
1152 dma_unmap_single(dma->rx_chan->device->dev, dma->rx_addr,
1153 dma->rx_size, DMA_FROM_DEVICE);
1155 dma_release_channel(dma->rx_chan);
1156 dma->rx_chan = NULL;
1160 dmaengine_terminate_all(dma->tx_chan);
1161 dma_unmap_single(dma->tx_chan->device->dev, dma->tx_addr,
1162 UART_XMIT_SIZE, DMA_TO_DEVICE);
1163 dma_release_channel(dma->tx_chan);
1164 dma->tx_chan = NULL;
1168 static void s3c24xx_serial_shutdown(struct uart_port *port)
1170 struct s3c24xx_uart_port *ourport = to_ourport(port);
1172 if (ourport->tx_claimed) {
1173 free_irq(ourport->tx_irq, ourport);
1174 ourport->tx_enabled = 0;
1175 ourport->tx_claimed = 0;
1176 ourport->tx_mode = 0;
1179 if (ourport->rx_claimed) {
1180 free_irq(ourport->rx_irq, ourport);
1181 ourport->rx_claimed = 0;
1182 ourport->rx_enabled = 0;
1186 s3c24xx_serial_release_dma(ourport);
1188 ourport->tx_in_progress = 0;
1191 static void s3c64xx_serial_shutdown(struct uart_port *port)
1193 struct s3c24xx_uart_port *ourport = to_ourport(port);
1195 ourport->tx_enabled = 0;
1196 ourport->tx_mode = 0;
1197 ourport->rx_enabled = 0;
1199 free_irq(port->irq, ourport);
1201 wr_regl(port, S3C64XX_UINTP, 0xf);
1202 wr_regl(port, S3C64XX_UINTM, 0xf);
1205 s3c24xx_serial_release_dma(ourport);
1207 ourport->tx_in_progress = 0;
1210 static void apple_s5l_serial_shutdown(struct uart_port *port)
1212 struct s3c24xx_uart_port *ourport = to_ourport(port);
1216 ucon = rd_regl(port, S3C2410_UCON);
1217 ucon &= ~(APPLE_S5L_UCON_TXTHRESH_ENA_MSK |
1218 APPLE_S5L_UCON_RXTHRESH_ENA_MSK |
1219 APPLE_S5L_UCON_RXTO_ENA_MSK);
1220 wr_regl(port, S3C2410_UCON, ucon);
1222 wr_regl(port, S3C2410_UTRSTAT, APPLE_S5L_UTRSTAT_ALL_FLAGS);
1224 free_irq(port->irq, ourport);
1226 ourport->tx_enabled = 0;
1227 ourport->tx_mode = 0;
1228 ourport->rx_enabled = 0;
1231 s3c24xx_serial_release_dma(ourport);
1233 ourport->tx_in_progress = 0;
1236 static int s3c24xx_serial_startup(struct uart_port *port)
1238 struct s3c24xx_uart_port *ourport = to_ourport(port);
1241 ourport->rx_enabled = 1;
1243 ret = request_irq(ourport->rx_irq, s3c24xx_serial_rx_irq, 0,
1244 s3c24xx_serial_portname(port), ourport);
1247 dev_err(port->dev, "cannot get irq %d\n", ourport->rx_irq);
1251 ourport->rx_claimed = 1;
1253 dev_dbg(port->dev, "requesting tx irq...\n");
1255 ourport->tx_enabled = 1;
1257 ret = request_irq(ourport->tx_irq, s3c24xx_serial_tx_irq, 0,
1258 s3c24xx_serial_portname(port), ourport);
1261 dev_err(port->dev, "cannot get irq %d\n", ourport->tx_irq);
1265 ourport->tx_claimed = 1;
1267 /* the port reset code should have done the correct
1268 * register setup for the port controls
1274 s3c24xx_serial_shutdown(port);
1278 static int s3c64xx_serial_startup(struct uart_port *port)
1280 struct s3c24xx_uart_port *ourport = to_ourport(port);
1281 unsigned long flags;
1285 wr_regl(port, S3C64XX_UINTM, 0xf);
1287 ret = s3c24xx_serial_request_dma(ourport);
1289 devm_kfree(port->dev, ourport->dma);
1290 ourport->dma = NULL;
1294 ret = request_irq(port->irq, s3c64xx_serial_handle_irq, IRQF_SHARED,
1295 s3c24xx_serial_portname(port), ourport);
1297 dev_err(port->dev, "cannot get irq %d\n", port->irq);
1301 /* For compatibility with s3c24xx Soc's */
1302 ourport->rx_enabled = 1;
1303 ourport->tx_enabled = 0;
1305 spin_lock_irqsave(&port->lock, flags);
1307 ufcon = rd_regl(port, S3C2410_UFCON);
1308 ufcon |= S3C2410_UFCON_RESETRX | S5PV210_UFCON_RXTRIG8;
1309 if (!uart_console(port))
1310 ufcon |= S3C2410_UFCON_RESETTX;
1311 wr_regl(port, S3C2410_UFCON, ufcon);
1313 enable_rx_pio(ourport);
1315 spin_unlock_irqrestore(&port->lock, flags);
1317 /* Enable Rx Interrupt */
1318 s3c24xx_clear_bit(port, S3C64XX_UINTM_RXD, S3C64XX_UINTM);
1323 static int apple_s5l_serial_startup(struct uart_port *port)
1325 struct s3c24xx_uart_port *ourport = to_ourport(port);
1326 unsigned long flags;
1330 wr_regl(port, S3C2410_UTRSTAT, APPLE_S5L_UTRSTAT_ALL_FLAGS);
1332 ret = request_irq(port->irq, apple_serial_handle_irq, 0,
1333 s3c24xx_serial_portname(port), ourport);
1335 dev_err(port->dev, "cannot get irq %d\n", port->irq);
1339 /* For compatibility with s3c24xx Soc's */
1340 ourport->rx_enabled = 1;
1341 ourport->tx_enabled = 0;
1343 spin_lock_irqsave(&port->lock, flags);
1345 ufcon = rd_regl(port, S3C2410_UFCON);
1346 ufcon |= S3C2410_UFCON_RESETRX | S5PV210_UFCON_RXTRIG8;
1347 if (!uart_console(port))
1348 ufcon |= S3C2410_UFCON_RESETTX;
1349 wr_regl(port, S3C2410_UFCON, ufcon);
1351 enable_rx_pio(ourport);
1353 spin_unlock_irqrestore(&port->lock, flags);
1355 /* Enable Rx Interrupt */
1356 s3c24xx_set_bit(port, APPLE_S5L_UCON_RXTHRESH_ENA, S3C2410_UCON);
1357 s3c24xx_set_bit(port, APPLE_S5L_UCON_RXTO_ENA, S3C2410_UCON);
1362 /* power power management control */
1364 static void s3c24xx_serial_pm(struct uart_port *port, unsigned int level,
1367 struct s3c24xx_uart_port *ourport = to_ourport(port);
1368 int timeout = 10000;
1370 ourport->pm_level = level;
1374 while (--timeout && !s3c24xx_serial_txempty_nofifo(port))
1377 if (!IS_ERR(ourport->baudclk))
1378 clk_disable_unprepare(ourport->baudclk);
1380 clk_disable_unprepare(ourport->clk);
1384 clk_prepare_enable(ourport->clk);
1386 if (!IS_ERR(ourport->baudclk))
1387 clk_prepare_enable(ourport->baudclk);
1390 dev_err(port->dev, "s3c24xx_serial: unknown pm %d\n", level);
1394 /* baud rate calculation
1396 * The UARTs on the S3C2410/S3C2440 can take their clocks from a number
1397 * of different sources, including the peripheral clock ("pclk") and an
1398 * external clock ("uclk"). The S3C2440 also adds the core clock ("fclk")
1399 * with a programmable extra divisor.
1401 * The following code goes through the clock sources, and calculates the
1402 * baud clocks (and the resultant actual baud rates) and then tries to
1403 * pick the closest one and select that.
1407 #define MAX_CLK_NAME_LENGTH 15
1409 static inline int s3c24xx_serial_getsource(struct uart_port *port)
1411 const struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
1414 if (info->num_clks == 1)
1417 ucon = rd_regl(port, S3C2410_UCON);
1418 ucon &= info->clksel_mask;
1419 return ucon >> info->clksel_shift;
1422 static void s3c24xx_serial_setsource(struct uart_port *port,
1423 unsigned int clk_sel)
1425 const struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
1428 if (info->num_clks == 1)
1431 ucon = rd_regl(port, S3C2410_UCON);
1432 if ((ucon & info->clksel_mask) >> info->clksel_shift == clk_sel)
1435 ucon &= ~info->clksel_mask;
1436 ucon |= clk_sel << info->clksel_shift;
1437 wr_regl(port, S3C2410_UCON, ucon);
1440 static unsigned int s3c24xx_serial_getclk(struct s3c24xx_uart_port *ourport,
1441 unsigned int req_baud, struct clk **best_clk,
1442 unsigned int *clk_num)
1444 const struct s3c24xx_uart_info *info = ourport->info;
1447 unsigned int cnt, baud, quot, best_quot = 0;
1448 char clkname[MAX_CLK_NAME_LENGTH];
1449 int calc_deviation, deviation = (1 << 30) - 1;
1451 for (cnt = 0; cnt < info->num_clks; cnt++) {
1452 /* Keep selected clock if provided */
1453 if (ourport->cfg->clk_sel &&
1454 !(ourport->cfg->clk_sel & (1 << cnt)))
1457 sprintf(clkname, "clk_uart_baud%d", cnt);
1458 clk = clk_get(ourport->port.dev, clkname);
1462 rate = clk_get_rate(clk);
1464 dev_err(ourport->port.dev,
1465 "Failed to get clock rate for %s.\n", clkname);
1470 if (ourport->info->has_divslot) {
1471 unsigned long div = rate / req_baud;
1473 /* The UDIVSLOT register on the newer UARTs allows us to
1474 * get a divisor adjustment of 1/16th on the baud clock.
1476 * We don't keep the UDIVSLOT value (the 16ths we
1477 * calculated by not multiplying the baud by 16) as it
1478 * is easy enough to recalculate.
1484 quot = (rate + (8 * req_baud)) / (16 * req_baud);
1485 baud = rate / (quot * 16);
1489 calc_deviation = abs(req_baud - baud);
1491 if (calc_deviation < deviation) {
1493 * If we find a better clk, release the previous one, if
1496 if (!IS_ERR(*best_clk))
1501 deviation = calc_deviation;
1512 * This table takes the fractional value of the baud divisor and gives
1513 * the recommended setting for the UDIVSLOT register.
1515 static const u16 udivslot_table[16] = {
1534 static void s3c24xx_serial_set_termios(struct uart_port *port,
1535 struct ktermios *termios,
1536 const struct ktermios *old)
1538 const struct s3c2410_uartcfg *cfg = s3c24xx_port_to_cfg(port);
1539 struct s3c24xx_uart_port *ourport = to_ourport(port);
1540 struct clk *clk = ERR_PTR(-EINVAL);
1541 unsigned long flags;
1542 unsigned int baud, quot, clk_sel = 0;
1545 unsigned int udivslot = 0;
1548 * We don't support modem control lines.
1550 termios->c_cflag &= ~(HUPCL | CMSPAR);
1551 termios->c_cflag |= CLOCAL;
1554 * Ask the core to calculate the divisor for us.
1557 baud = uart_get_baud_rate(port, termios, old, 0, 3000000);
1558 quot = s3c24xx_serial_getclk(ourport, baud, &clk, &clk_sel);
1559 if (baud == 38400 && (port->flags & UPF_SPD_MASK) == UPF_SPD_CUST)
1560 quot = port->custom_divisor;
1564 /* check to see if we need to change clock source */
1566 if (ourport->baudclk != clk) {
1567 clk_prepare_enable(clk);
1569 s3c24xx_serial_setsource(port, clk_sel);
1571 if (!IS_ERR(ourport->baudclk)) {
1572 clk_disable_unprepare(ourport->baudclk);
1573 ourport->baudclk = ERR_PTR(-EINVAL);
1576 ourport->baudclk = clk;
1577 ourport->baudclk_rate = clk ? clk_get_rate(clk) : 0;
1580 if (ourport->info->has_divslot) {
1581 unsigned int div = ourport->baudclk_rate / baud;
1583 if (cfg->has_fracval) {
1584 udivslot = (div & 15);
1585 dev_dbg(port->dev, "fracval = %04x\n", udivslot);
1587 udivslot = udivslot_table[div & 15];
1588 dev_dbg(port->dev, "udivslot = %04x (div %d)\n",
1589 udivslot, div & 15);
1593 switch (termios->c_cflag & CSIZE) {
1595 dev_dbg(port->dev, "config: 5bits/char\n");
1596 ulcon = S3C2410_LCON_CS5;
1599 dev_dbg(port->dev, "config: 6bits/char\n");
1600 ulcon = S3C2410_LCON_CS6;
1603 dev_dbg(port->dev, "config: 7bits/char\n");
1604 ulcon = S3C2410_LCON_CS7;
1608 dev_dbg(port->dev, "config: 8bits/char\n");
1609 ulcon = S3C2410_LCON_CS8;
1613 /* preserve original lcon IR settings */
1614 ulcon |= (cfg->ulcon & S3C2410_LCON_IRM);
1616 if (termios->c_cflag & CSTOPB)
1617 ulcon |= S3C2410_LCON_STOPB;
1619 if (termios->c_cflag & PARENB) {
1620 if (termios->c_cflag & PARODD)
1621 ulcon |= S3C2410_LCON_PODD;
1623 ulcon |= S3C2410_LCON_PEVEN;
1625 ulcon |= S3C2410_LCON_PNONE;
1628 spin_lock_irqsave(&port->lock, flags);
1631 "setting ulcon to %08x, brddiv to %d, udivslot %08x\n",
1632 ulcon, quot, udivslot);
1634 wr_regl(port, S3C2410_ULCON, ulcon);
1635 wr_regl(port, S3C2410_UBRDIV, quot);
1637 port->status &= ~UPSTAT_AUTOCTS;
1639 umcon = rd_regl(port, S3C2410_UMCON);
1640 if (termios->c_cflag & CRTSCTS) {
1641 umcon |= S3C2410_UMCOM_AFC;
1642 /* Disable RTS when RX FIFO contains 63 bytes */
1643 umcon &= ~S3C2412_UMCON_AFC_8;
1644 port->status = UPSTAT_AUTOCTS;
1646 umcon &= ~S3C2410_UMCOM_AFC;
1648 wr_regl(port, S3C2410_UMCON, umcon);
1650 if (ourport->info->has_divslot)
1651 wr_regl(port, S3C2443_DIVSLOT, udivslot);
1654 "uart: ulcon = 0x%08x, ucon = 0x%08x, ufcon = 0x%08x\n",
1655 rd_regl(port, S3C2410_ULCON),
1656 rd_regl(port, S3C2410_UCON),
1657 rd_regl(port, S3C2410_UFCON));
1660 * Update the per-port timeout.
1662 uart_update_timeout(port, termios->c_cflag, baud);
1665 * Which character status flags are we interested in?
1667 port->read_status_mask = S3C2410_UERSTAT_OVERRUN;
1668 if (termios->c_iflag & INPCK)
1669 port->read_status_mask |= S3C2410_UERSTAT_FRAME |
1670 S3C2410_UERSTAT_PARITY;
1672 * Which character status flags should we ignore?
1674 port->ignore_status_mask = 0;
1675 if (termios->c_iflag & IGNPAR)
1676 port->ignore_status_mask |= S3C2410_UERSTAT_OVERRUN;
1677 if (termios->c_iflag & IGNBRK && termios->c_iflag & IGNPAR)
1678 port->ignore_status_mask |= S3C2410_UERSTAT_FRAME;
1681 * Ignore all characters if CREAD is not set.
1683 if ((termios->c_cflag & CREAD) == 0)
1684 port->ignore_status_mask |= RXSTAT_DUMMY_READ;
1686 spin_unlock_irqrestore(&port->lock, flags);
1689 static const char *s3c24xx_serial_type(struct uart_port *port)
1691 const struct s3c24xx_uart_port *ourport = to_ourport(port);
1693 switch (ourport->info->type) {
1697 return "S3C6400/10";
1698 case TYPE_APPLE_S5L:
1705 static void s3c24xx_serial_config_port(struct uart_port *port, int flags)
1707 const struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
1709 if (flags & UART_CONFIG_TYPE)
1710 port->type = info->port_type;
1714 * verify the new serial_struct (for TIOCSSERIAL).
1717 s3c24xx_serial_verify_port(struct uart_port *port, struct serial_struct *ser)
1719 const struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
1721 if (ser->type != PORT_UNKNOWN && ser->type != info->port_type)
1727 #ifdef CONFIG_SERIAL_SAMSUNG_CONSOLE
1729 static struct console s3c24xx_serial_console;
1731 static void __init s3c24xx_serial_register_console(void)
1733 register_console(&s3c24xx_serial_console);
1736 static void s3c24xx_serial_unregister_console(void)
1738 if (console_is_registered(&s3c24xx_serial_console))
1739 unregister_console(&s3c24xx_serial_console);
1742 #define S3C24XX_SERIAL_CONSOLE &s3c24xx_serial_console
1744 static inline void s3c24xx_serial_register_console(void) { }
1745 static inline void s3c24xx_serial_unregister_console(void) { }
1746 #define S3C24XX_SERIAL_CONSOLE NULL
1749 #if defined(CONFIG_SERIAL_SAMSUNG_CONSOLE) && defined(CONFIG_CONSOLE_POLL)
1750 static int s3c24xx_serial_get_poll_char(struct uart_port *port);
1751 static void s3c24xx_serial_put_poll_char(struct uart_port *port,
1755 static const struct uart_ops s3c24xx_serial_ops = {
1756 .pm = s3c24xx_serial_pm,
1757 .tx_empty = s3c24xx_serial_tx_empty,
1758 .get_mctrl = s3c24xx_serial_get_mctrl,
1759 .set_mctrl = s3c24xx_serial_set_mctrl,
1760 .stop_tx = s3c24xx_serial_stop_tx,
1761 .start_tx = s3c24xx_serial_start_tx,
1762 .stop_rx = s3c24xx_serial_stop_rx,
1763 .break_ctl = s3c24xx_serial_break_ctl,
1764 .startup = s3c24xx_serial_startup,
1765 .shutdown = s3c24xx_serial_shutdown,
1766 .set_termios = s3c24xx_serial_set_termios,
1767 .type = s3c24xx_serial_type,
1768 .config_port = s3c24xx_serial_config_port,
1769 .verify_port = s3c24xx_serial_verify_port,
1770 #if defined(CONFIG_SERIAL_SAMSUNG_CONSOLE) && defined(CONFIG_CONSOLE_POLL)
1771 .poll_get_char = s3c24xx_serial_get_poll_char,
1772 .poll_put_char = s3c24xx_serial_put_poll_char,
1776 static const struct uart_ops s3c64xx_serial_ops = {
1777 .pm = s3c24xx_serial_pm,
1778 .tx_empty = s3c24xx_serial_tx_empty,
1779 .get_mctrl = s3c24xx_serial_get_mctrl,
1780 .set_mctrl = s3c24xx_serial_set_mctrl,
1781 .stop_tx = s3c24xx_serial_stop_tx,
1782 .start_tx = s3c24xx_serial_start_tx,
1783 .stop_rx = s3c24xx_serial_stop_rx,
1784 .break_ctl = s3c24xx_serial_break_ctl,
1785 .startup = s3c64xx_serial_startup,
1786 .shutdown = s3c64xx_serial_shutdown,
1787 .set_termios = s3c24xx_serial_set_termios,
1788 .type = s3c24xx_serial_type,
1789 .config_port = s3c24xx_serial_config_port,
1790 .verify_port = s3c24xx_serial_verify_port,
1791 #if defined(CONFIG_SERIAL_SAMSUNG_CONSOLE) && defined(CONFIG_CONSOLE_POLL)
1792 .poll_get_char = s3c24xx_serial_get_poll_char,
1793 .poll_put_char = s3c24xx_serial_put_poll_char,
1797 static const struct uart_ops apple_s5l_serial_ops = {
1798 .pm = s3c24xx_serial_pm,
1799 .tx_empty = s3c24xx_serial_tx_empty,
1800 .get_mctrl = s3c24xx_serial_get_mctrl,
1801 .set_mctrl = s3c24xx_serial_set_mctrl,
1802 .stop_tx = s3c24xx_serial_stop_tx,
1803 .start_tx = s3c24xx_serial_start_tx,
1804 .stop_rx = s3c24xx_serial_stop_rx,
1805 .break_ctl = s3c24xx_serial_break_ctl,
1806 .startup = apple_s5l_serial_startup,
1807 .shutdown = apple_s5l_serial_shutdown,
1808 .set_termios = s3c24xx_serial_set_termios,
1809 .type = s3c24xx_serial_type,
1810 .config_port = s3c24xx_serial_config_port,
1811 .verify_port = s3c24xx_serial_verify_port,
1812 #if defined(CONFIG_SERIAL_SAMSUNG_CONSOLE) && defined(CONFIG_CONSOLE_POLL)
1813 .poll_get_char = s3c24xx_serial_get_poll_char,
1814 .poll_put_char = s3c24xx_serial_put_poll_char,
1818 static struct uart_driver s3c24xx_uart_drv = {
1819 .owner = THIS_MODULE,
1820 .driver_name = "s3c2410_serial",
1822 .cons = S3C24XX_SERIAL_CONSOLE,
1823 .dev_name = S3C24XX_SERIAL_NAME,
1824 .major = S3C24XX_SERIAL_MAJOR,
1825 .minor = S3C24XX_SERIAL_MINOR,
1828 static struct s3c24xx_uart_port s3c24xx_serial_ports[UART_NR];
1830 static void s3c24xx_serial_init_port_default(int index) {
1831 struct uart_port *port = &s3c24xx_serial_ports[index].port;
1833 spin_lock_init(&port->lock);
1835 port->iotype = UPIO_MEM;
1837 port->fifosize = 16;
1838 port->ops = &s3c24xx_serial_ops;
1839 port->flags = UPF_BOOT_AUTOCONF;
1843 /* s3c24xx_serial_resetport
1845 * reset the fifos and other the settings.
1848 static void s3c24xx_serial_resetport(struct uart_port *port,
1849 const struct s3c2410_uartcfg *cfg)
1851 const struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
1852 unsigned long ucon = rd_regl(port, S3C2410_UCON);
1854 ucon &= (info->clksel_mask | info->ucon_mask);
1855 wr_regl(port, S3C2410_UCON, ucon | cfg->ucon);
1857 /* reset both fifos */
1858 wr_regl(port, S3C2410_UFCON, cfg->ufcon | S3C2410_UFCON_RESETBOTH);
1859 wr_regl(port, S3C2410_UFCON, cfg->ufcon);
1861 /* some delay is required after fifo reset */
1865 static int s3c24xx_serial_enable_baudclk(struct s3c24xx_uart_port *ourport)
1867 struct device *dev = ourport->port.dev;
1868 const struct s3c24xx_uart_info *info = ourport->info;
1869 char clk_name[MAX_CLK_NAME_LENGTH];
1870 unsigned int clk_sel;
1875 clk_sel = ourport->cfg->clk_sel ? : info->def_clk_sel;
1876 for (clk_num = 0; clk_num < info->num_clks; clk_num++) {
1877 if (!(clk_sel & (1 << clk_num)))
1880 sprintf(clk_name, "clk_uart_baud%d", clk_num);
1881 clk = clk_get(dev, clk_name);
1885 ret = clk_prepare_enable(clk);
1891 ourport->baudclk = clk;
1892 ourport->baudclk_rate = clk_get_rate(clk);
1893 s3c24xx_serial_setsource(&ourport->port, clk_num);
1901 /* s3c24xx_serial_init_port
1903 * initialise a single serial port from the platform device given
1906 static int s3c24xx_serial_init_port(struct s3c24xx_uart_port *ourport,
1907 struct platform_device *platdev)
1909 struct uart_port *port = &ourport->port;
1910 const struct s3c2410_uartcfg *cfg = ourport->cfg;
1911 struct resource *res;
1914 if (platdev == NULL)
1917 if (port->mapbase != 0)
1920 /* setup info for port */
1921 port->dev = &platdev->dev;
1925 if (cfg->uart_flags & UPF_CONS_FLOW) {
1926 dev_dbg(port->dev, "enabling flow control\n");
1927 port->flags |= UPF_CONS_FLOW;
1930 /* sort our the physical and virtual addresses for each UART */
1932 res = platform_get_resource(platdev, IORESOURCE_MEM, 0);
1934 dev_err(port->dev, "failed to find memory resource for uart\n");
1938 dev_dbg(port->dev, "resource %pR)\n", res);
1940 port->membase = devm_ioremap_resource(port->dev, res);
1941 if (IS_ERR(port->membase)) {
1942 dev_err(port->dev, "failed to remap controller address\n");
1946 port->mapbase = res->start;
1947 ret = platform_get_irq(platdev, 0);
1952 ourport->rx_irq = ret;
1953 ourport->tx_irq = ret + 1;
1956 switch (ourport->info->type) {
1958 ret = platform_get_irq(platdev, 1);
1960 ourport->tx_irq = ret;
1967 * DMA is currently supported only on DT platforms, if DMA properties
1970 if (platdev->dev.of_node && of_find_property(platdev->dev.of_node,
1972 ourport->dma = devm_kzalloc(port->dev,
1973 sizeof(*ourport->dma),
1975 if (!ourport->dma) {
1981 ourport->clk = clk_get(&platdev->dev, "uart");
1982 if (IS_ERR(ourport->clk)) {
1983 pr_err("%s: Controller clock not found\n",
1984 dev_name(&platdev->dev));
1985 ret = PTR_ERR(ourport->clk);
1989 ret = clk_prepare_enable(ourport->clk);
1991 pr_err("uart: clock failed to prepare+enable: %d\n", ret);
1992 clk_put(ourport->clk);
1996 ret = s3c24xx_serial_enable_baudclk(ourport);
1998 pr_warn("uart: failed to enable baudclk\n");
2000 /* Keep all interrupts masked and cleared */
2001 switch (ourport->info->type) {
2003 wr_regl(port, S3C64XX_UINTM, 0xf);
2004 wr_regl(port, S3C64XX_UINTP, 0xf);
2005 wr_regl(port, S3C64XX_UINTSP, 0xf);
2007 case TYPE_APPLE_S5L: {
2010 ucon = rd_regl(port, S3C2410_UCON);
2011 ucon &= ~(APPLE_S5L_UCON_TXTHRESH_ENA_MSK |
2012 APPLE_S5L_UCON_RXTHRESH_ENA_MSK |
2013 APPLE_S5L_UCON_RXTO_ENA_MSK);
2014 wr_regl(port, S3C2410_UCON, ucon);
2016 wr_regl(port, S3C2410_UTRSTAT, APPLE_S5L_UTRSTAT_ALL_FLAGS);
2023 dev_dbg(port->dev, "port: map=%pa, mem=%p, irq=%d (%d,%d), clock=%u\n",
2024 &port->mapbase, port->membase, port->irq,
2025 ourport->rx_irq, ourport->tx_irq, port->uartclk);
2027 /* reset the fifos (and setup the uart) */
2028 s3c24xx_serial_resetport(port, cfg);
2037 /* Device driver serial port probe */
2039 static int probe_index;
2041 static inline const struct s3c24xx_serial_drv_data *
2042 s3c24xx_get_driver_data(struct platform_device *pdev)
2044 if (dev_of_node(&pdev->dev))
2045 return of_device_get_match_data(&pdev->dev);
2047 return (struct s3c24xx_serial_drv_data *)
2048 platform_get_device_id(pdev)->driver_data;
2051 static int s3c24xx_serial_probe(struct platform_device *pdev)
2053 struct device_node *np = pdev->dev.of_node;
2054 struct s3c24xx_uart_port *ourport;
2055 int index = probe_index;
2059 ret = of_alias_get_id(np, "serial");
2064 if (index >= ARRAY_SIZE(s3c24xx_serial_ports)) {
2065 dev_err(&pdev->dev, "serial%d out of range\n", index);
2068 ourport = &s3c24xx_serial_ports[index];
2070 s3c24xx_serial_init_port_default(index);
2072 ourport->drv_data = s3c24xx_get_driver_data(pdev);
2073 if (!ourport->drv_data) {
2074 dev_err(&pdev->dev, "could not find driver data\n");
2078 ourport->baudclk = ERR_PTR(-EINVAL);
2079 ourport->info = &ourport->drv_data->info;
2080 ourport->cfg = (dev_get_platdata(&pdev->dev)) ?
2081 dev_get_platdata(&pdev->dev) :
2082 &ourport->drv_data->def_cfg;
2084 switch (ourport->info->type) {
2086 ourport->port.ops = &s3c24xx_serial_ops;
2089 ourport->port.ops = &s3c64xx_serial_ops;
2091 case TYPE_APPLE_S5L:
2092 ourport->port.ops = &apple_s5l_serial_ops;
2097 of_property_read_u32(np,
2098 "samsung,uart-fifosize", &ourport->port.fifosize);
2100 if (of_property_read_u32(np, "reg-io-width", &prop) == 0) {
2103 ourport->port.iotype = UPIO_MEM;
2106 ourport->port.iotype = UPIO_MEM32;
2109 dev_warn(&pdev->dev, "unsupported reg-io-width (%d)\n",
2116 if (ourport->drv_data->fifosize[index])
2117 ourport->port.fifosize = ourport->drv_data->fifosize[index];
2118 else if (ourport->info->fifosize)
2119 ourport->port.fifosize = ourport->info->fifosize;
2120 ourport->port.has_sysrq = IS_ENABLED(CONFIG_SERIAL_SAMSUNG_CONSOLE);
2123 * DMA transfers must be aligned at least to cache line size,
2124 * so find minimal transfer size suitable for DMA mode
2126 ourport->min_dma_size = max_t(int, ourport->port.fifosize,
2127 dma_get_cache_alignment());
2129 dev_dbg(&pdev->dev, "%s: initialising port %p...\n", __func__, ourport);
2131 ret = s3c24xx_serial_init_port(ourport, pdev);
2135 if (!s3c24xx_uart_drv.state) {
2136 ret = uart_register_driver(&s3c24xx_uart_drv);
2138 pr_err("Failed to register Samsung UART driver\n");
2143 dev_dbg(&pdev->dev, "%s: adding port\n", __func__);
2144 uart_add_one_port(&s3c24xx_uart_drv, &ourport->port);
2145 platform_set_drvdata(pdev, &ourport->port);
2148 * Deactivate the clock enabled in s3c24xx_serial_init_port here,
2149 * so that a potential re-enablement through the pm-callback overlaps
2150 * and keeps the clock enabled in this case.
2152 clk_disable_unprepare(ourport->clk);
2153 if (!IS_ERR(ourport->baudclk))
2154 clk_disable_unprepare(ourport->baudclk);
2161 static int s3c24xx_serial_remove(struct platform_device *dev)
2163 struct uart_port *port = s3c24xx_dev_to_port(&dev->dev);
2166 uart_remove_one_port(&s3c24xx_uart_drv, port);
2169 uart_unregister_driver(&s3c24xx_uart_drv);
2174 /* UART power management code */
2175 #ifdef CONFIG_PM_SLEEP
2176 static int s3c24xx_serial_suspend(struct device *dev)
2178 struct uart_port *port = s3c24xx_dev_to_port(dev);
2181 uart_suspend_port(&s3c24xx_uart_drv, port);
2186 static int s3c24xx_serial_resume(struct device *dev)
2188 struct uart_port *port = s3c24xx_dev_to_port(dev);
2189 struct s3c24xx_uart_port *ourport = to_ourport(port);
2192 clk_prepare_enable(ourport->clk);
2193 if (!IS_ERR(ourport->baudclk))
2194 clk_prepare_enable(ourport->baudclk);
2195 s3c24xx_serial_resetport(port, s3c24xx_port_to_cfg(port));
2196 if (!IS_ERR(ourport->baudclk))
2197 clk_disable_unprepare(ourport->baudclk);
2198 clk_disable_unprepare(ourport->clk);
2200 uart_resume_port(&s3c24xx_uart_drv, port);
2206 static int s3c24xx_serial_resume_noirq(struct device *dev)
2208 struct uart_port *port = s3c24xx_dev_to_port(dev);
2209 struct s3c24xx_uart_port *ourport = to_ourport(port);
2212 /* restore IRQ mask */
2213 switch (ourport->info->type) {
2214 case TYPE_S3C6400: {
2215 unsigned int uintm = 0xf;
2217 if (ourport->tx_enabled)
2218 uintm &= ~S3C64XX_UINTM_TXD_MSK;
2219 if (ourport->rx_enabled)
2220 uintm &= ~S3C64XX_UINTM_RXD_MSK;
2221 clk_prepare_enable(ourport->clk);
2222 if (!IS_ERR(ourport->baudclk))
2223 clk_prepare_enable(ourport->baudclk);
2224 wr_regl(port, S3C64XX_UINTM, uintm);
2225 if (!IS_ERR(ourport->baudclk))
2226 clk_disable_unprepare(ourport->baudclk);
2227 clk_disable_unprepare(ourport->clk);
2230 case TYPE_APPLE_S5L: {
2234 ret = clk_prepare_enable(ourport->clk);
2236 dev_err(dev, "clk_enable clk failed: %d\n", ret);
2239 if (!IS_ERR(ourport->baudclk)) {
2240 ret = clk_prepare_enable(ourport->baudclk);
2242 dev_err(dev, "clk_enable baudclk failed: %d\n", ret);
2243 clk_disable_unprepare(ourport->clk);
2248 ucon = rd_regl(port, S3C2410_UCON);
2250 ucon &= ~(APPLE_S5L_UCON_TXTHRESH_ENA_MSK |
2251 APPLE_S5L_UCON_RXTHRESH_ENA_MSK |
2252 APPLE_S5L_UCON_RXTO_ENA_MSK);
2254 if (ourport->tx_enabled)
2255 ucon |= APPLE_S5L_UCON_TXTHRESH_ENA_MSK;
2256 if (ourport->rx_enabled)
2257 ucon |= APPLE_S5L_UCON_RXTHRESH_ENA_MSK |
2258 APPLE_S5L_UCON_RXTO_ENA_MSK;
2260 wr_regl(port, S3C2410_UCON, ucon);
2262 if (!IS_ERR(ourport->baudclk))
2263 clk_disable_unprepare(ourport->baudclk);
2264 clk_disable_unprepare(ourport->clk);
2275 static const struct dev_pm_ops s3c24xx_serial_pm_ops = {
2276 .suspend = s3c24xx_serial_suspend,
2277 .resume = s3c24xx_serial_resume,
2278 .resume_noirq = s3c24xx_serial_resume_noirq,
2280 #define SERIAL_SAMSUNG_PM_OPS (&s3c24xx_serial_pm_ops)
2282 #else /* !CONFIG_PM_SLEEP */
2284 #define SERIAL_SAMSUNG_PM_OPS NULL
2285 #endif /* CONFIG_PM_SLEEP */
2289 #ifdef CONFIG_SERIAL_SAMSUNG_CONSOLE
2291 static struct uart_port *cons_uart;
2294 s3c24xx_serial_console_txrdy(struct uart_port *port, unsigned int ufcon)
2296 const struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
2297 unsigned long ufstat, utrstat;
2299 if (ufcon & S3C2410_UFCON_FIFOMODE) {
2300 /* fifo mode - check amount of data in fifo registers... */
2302 ufstat = rd_regl(port, S3C2410_UFSTAT);
2303 return (ufstat & info->tx_fifofull) ? 0 : 1;
2306 /* in non-fifo mode, we go and use the tx buffer empty */
2308 utrstat = rd_regl(port, S3C2410_UTRSTAT);
2309 return (utrstat & S3C2410_UTRSTAT_TXE) ? 1 : 0;
2313 s3c24xx_port_configured(unsigned int ucon)
2315 /* consider the serial port configured if the tx/rx mode set */
2316 return (ucon & 0xf) != 0;
2319 #ifdef CONFIG_CONSOLE_POLL
2321 * Console polling routines for writing and reading from the uart while
2322 * in an interrupt or debug context.
2325 static int s3c24xx_serial_get_poll_char(struct uart_port *port)
2327 const struct s3c24xx_uart_port *ourport = to_ourport(port);
2328 unsigned int ufstat;
2330 ufstat = rd_regl(port, S3C2410_UFSTAT);
2331 if (s3c24xx_serial_rx_fifocnt(ourport, ufstat) == 0)
2332 return NO_POLL_CHAR;
2334 return rd_reg(port, S3C2410_URXH);
2337 static void s3c24xx_serial_put_poll_char(struct uart_port *port,
2340 unsigned int ufcon = rd_regl(port, S3C2410_UFCON);
2341 unsigned int ucon = rd_regl(port, S3C2410_UCON);
2343 /* not possible to xmit on unconfigured port */
2344 if (!s3c24xx_port_configured(ucon))
2347 while (!s3c24xx_serial_console_txrdy(port, ufcon))
2349 wr_reg(port, S3C2410_UTXH, c);
2352 #endif /* CONFIG_CONSOLE_POLL */
2355 s3c24xx_serial_console_putchar(struct uart_port *port, unsigned char ch)
2357 unsigned int ufcon = rd_regl(port, S3C2410_UFCON);
2359 while (!s3c24xx_serial_console_txrdy(port, ufcon))
2361 wr_reg(port, S3C2410_UTXH, ch);
2365 s3c24xx_serial_console_write(struct console *co, const char *s,
2368 unsigned int ucon = rd_regl(cons_uart, S3C2410_UCON);
2369 unsigned long flags;
2372 /* not possible to xmit on unconfigured port */
2373 if (!s3c24xx_port_configured(ucon))
2376 if (cons_uart->sysrq)
2378 else if (oops_in_progress)
2379 locked = spin_trylock_irqsave(&cons_uart->lock, flags);
2381 spin_lock_irqsave(&cons_uart->lock, flags);
2383 uart_console_write(cons_uart, s, count, s3c24xx_serial_console_putchar);
2386 spin_unlock_irqrestore(&cons_uart->lock, flags);
2389 /* Shouldn't be __init, as it can be instantiated from other module */
2391 s3c24xx_serial_get_options(struct uart_port *port, int *baud,
2392 int *parity, int *bits)
2397 unsigned int ubrdiv;
2399 unsigned int clk_sel;
2400 char clk_name[MAX_CLK_NAME_LENGTH];
2402 ulcon = rd_regl(port, S3C2410_ULCON);
2403 ucon = rd_regl(port, S3C2410_UCON);
2404 ubrdiv = rd_regl(port, S3C2410_UBRDIV);
2406 if (s3c24xx_port_configured(ucon)) {
2407 switch (ulcon & S3C2410_LCON_CSMASK) {
2408 case S3C2410_LCON_CS5:
2411 case S3C2410_LCON_CS6:
2414 case S3C2410_LCON_CS7:
2417 case S3C2410_LCON_CS8:
2423 switch (ulcon & S3C2410_LCON_PMASK) {
2424 case S3C2410_LCON_PEVEN:
2428 case S3C2410_LCON_PODD:
2432 case S3C2410_LCON_PNONE:
2437 /* now calculate the baud rate */
2439 clk_sel = s3c24xx_serial_getsource(port);
2440 sprintf(clk_name, "clk_uart_baud%d", clk_sel);
2442 clk = clk_get(port->dev, clk_name);
2444 rate = clk_get_rate(clk);
2448 *baud = rate / (16 * (ubrdiv + 1));
2449 dev_dbg(port->dev, "calculated baud %d\n", *baud);
2453 /* Shouldn't be __init, as it can be instantiated from other module */
2455 s3c24xx_serial_console_setup(struct console *co, char *options)
2457 struct uart_port *port;
2463 /* is this a valid port */
2465 if (co->index == -1 || co->index >= UART_NR)
2468 port = &s3c24xx_serial_ports[co->index].port;
2470 /* is the port configured? */
2472 if (port->mapbase == 0x0)
2478 * Check whether an invalid uart number has been specified, and
2479 * if so, search for the first available port that does have
2483 uart_parse_options(options, &baud, &parity, &bits, &flow);
2485 s3c24xx_serial_get_options(port, &baud, &parity, &bits);
2487 dev_dbg(port->dev, "baud %d\n", baud);
2489 return uart_set_options(port, co, baud, parity, bits, flow);
2492 static struct console s3c24xx_serial_console = {
2493 .name = S3C24XX_SERIAL_NAME,
2494 .device = uart_console_device,
2495 .flags = CON_PRINTBUFFER,
2497 .write = s3c24xx_serial_console_write,
2498 .setup = s3c24xx_serial_console_setup,
2499 .data = &s3c24xx_uart_drv,
2501 #endif /* CONFIG_SERIAL_SAMSUNG_CONSOLE */
2503 #if defined(CONFIG_CPU_S3C6400) || defined(CONFIG_CPU_S3C6410)
2504 static const struct s3c24xx_serial_drv_data s3c6400_serial_drv_data = {
2506 .name = "Samsung S3C6400 UART",
2507 .type = TYPE_S3C6400,
2508 .port_type = PORT_S3C6400,
2511 .rx_fifomask = S3C2440_UFSTAT_RXMASK,
2512 .rx_fifoshift = S3C2440_UFSTAT_RXSHIFT,
2513 .rx_fifofull = S3C2440_UFSTAT_RXFULL,
2514 .tx_fifofull = S3C2440_UFSTAT_TXFULL,
2515 .tx_fifomask = S3C2440_UFSTAT_TXMASK,
2516 .tx_fifoshift = S3C2440_UFSTAT_TXSHIFT,
2517 .def_clk_sel = S3C2410_UCON_CLKSEL2,
2519 .clksel_mask = S3C6400_UCON_CLKMASK,
2520 .clksel_shift = S3C6400_UCON_CLKSHIFT,
2523 .ucon = S3C2410_UCON_DEFAULT,
2524 .ufcon = S3C2410_UFCON_DEFAULT,
2527 #define S3C6400_SERIAL_DRV_DATA (&s3c6400_serial_drv_data)
2529 #define S3C6400_SERIAL_DRV_DATA NULL
2532 #ifdef CONFIG_CPU_S5PV210
2533 static const struct s3c24xx_serial_drv_data s5pv210_serial_drv_data = {
2535 .name = "Samsung S5PV210 UART",
2536 .type = TYPE_S3C6400,
2537 .port_type = PORT_S3C6400,
2539 .rx_fifomask = S5PV210_UFSTAT_RXMASK,
2540 .rx_fifoshift = S5PV210_UFSTAT_RXSHIFT,
2541 .rx_fifofull = S5PV210_UFSTAT_RXFULL,
2542 .tx_fifofull = S5PV210_UFSTAT_TXFULL,
2543 .tx_fifomask = S5PV210_UFSTAT_TXMASK,
2544 .tx_fifoshift = S5PV210_UFSTAT_TXSHIFT,
2545 .def_clk_sel = S3C2410_UCON_CLKSEL0,
2547 .clksel_mask = S5PV210_UCON_CLKMASK,
2548 .clksel_shift = S5PV210_UCON_CLKSHIFT,
2551 .ucon = S5PV210_UCON_DEFAULT,
2552 .ufcon = S5PV210_UFCON_DEFAULT,
2554 .fifosize = { 256, 64, 16, 16 },
2556 #define S5PV210_SERIAL_DRV_DATA (&s5pv210_serial_drv_data)
2558 #define S5PV210_SERIAL_DRV_DATA NULL
2561 #if defined(CONFIG_ARCH_EXYNOS)
2562 #define EXYNOS_COMMON_SERIAL_DRV_DATA() \
2564 .name = "Samsung Exynos UART", \
2565 .type = TYPE_S3C6400, \
2566 .port_type = PORT_S3C6400, \
2568 .rx_fifomask = S5PV210_UFSTAT_RXMASK, \
2569 .rx_fifoshift = S5PV210_UFSTAT_RXSHIFT, \
2570 .rx_fifofull = S5PV210_UFSTAT_RXFULL, \
2571 .tx_fifofull = S5PV210_UFSTAT_TXFULL, \
2572 .tx_fifomask = S5PV210_UFSTAT_TXMASK, \
2573 .tx_fifoshift = S5PV210_UFSTAT_TXSHIFT, \
2574 .def_clk_sel = S3C2410_UCON_CLKSEL0, \
2577 .clksel_shift = 0, \
2580 .ucon = S5PV210_UCON_DEFAULT, \
2581 .ufcon = S5PV210_UFCON_DEFAULT, \
2585 static const struct s3c24xx_serial_drv_data exynos4210_serial_drv_data = {
2586 EXYNOS_COMMON_SERIAL_DRV_DATA(),
2587 .fifosize = { 256, 64, 16, 16 },
2590 static const struct s3c24xx_serial_drv_data exynos5433_serial_drv_data = {
2591 EXYNOS_COMMON_SERIAL_DRV_DATA(),
2592 .fifosize = { 64, 256, 16, 256 },
2595 static const struct s3c24xx_serial_drv_data exynos850_serial_drv_data = {
2596 EXYNOS_COMMON_SERIAL_DRV_DATA(),
2597 .fifosize = { 256, 64, 64, 64 },
2600 #define EXYNOS4210_SERIAL_DRV_DATA (&exynos4210_serial_drv_data)
2601 #define EXYNOS5433_SERIAL_DRV_DATA (&exynos5433_serial_drv_data)
2602 #define EXYNOS850_SERIAL_DRV_DATA (&exynos850_serial_drv_data)
2605 #define EXYNOS4210_SERIAL_DRV_DATA NULL
2606 #define EXYNOS5433_SERIAL_DRV_DATA NULL
2607 #define EXYNOS850_SERIAL_DRV_DATA NULL
2610 #ifdef CONFIG_ARCH_APPLE
2611 static const struct s3c24xx_serial_drv_data s5l_serial_drv_data = {
2613 .name = "Apple S5L UART",
2614 .type = TYPE_APPLE_S5L,
2615 .port_type = PORT_8250,
2617 .rx_fifomask = S3C2410_UFSTAT_RXMASK,
2618 .rx_fifoshift = S3C2410_UFSTAT_RXSHIFT,
2619 .rx_fifofull = S3C2410_UFSTAT_RXFULL,
2620 .tx_fifofull = S3C2410_UFSTAT_TXFULL,
2621 .tx_fifomask = S3C2410_UFSTAT_TXMASK,
2622 .tx_fifoshift = S3C2410_UFSTAT_TXSHIFT,
2623 .def_clk_sel = S3C2410_UCON_CLKSEL0,
2627 .ucon_mask = APPLE_S5L_UCON_MASK,
2630 .ucon = APPLE_S5L_UCON_DEFAULT,
2631 .ufcon = S3C2410_UFCON_DEFAULT,
2634 #define S5L_SERIAL_DRV_DATA (&s5l_serial_drv_data)
2636 #define S5L_SERIAL_DRV_DATA NULL
2639 #if defined(CONFIG_ARCH_ARTPEC)
2640 static const struct s3c24xx_serial_drv_data artpec8_serial_drv_data = {
2642 .name = "Axis ARTPEC-8 UART",
2643 .type = TYPE_S3C6400,
2644 .port_type = PORT_S3C6400,
2647 .rx_fifomask = S5PV210_UFSTAT_RXMASK,
2648 .rx_fifoshift = S5PV210_UFSTAT_RXSHIFT,
2649 .rx_fifofull = S5PV210_UFSTAT_RXFULL,
2650 .tx_fifofull = S5PV210_UFSTAT_TXFULL,
2651 .tx_fifomask = S5PV210_UFSTAT_TXMASK,
2652 .tx_fifoshift = S5PV210_UFSTAT_TXSHIFT,
2653 .def_clk_sel = S3C2410_UCON_CLKSEL0,
2659 .ucon = S5PV210_UCON_DEFAULT,
2660 .ufcon = S5PV210_UFCON_DEFAULT,
2664 #define ARTPEC8_SERIAL_DRV_DATA (&artpec8_serial_drv_data)
2666 #define ARTPEC8_SERIAL_DRV_DATA (NULL)
2669 static const struct platform_device_id s3c24xx_serial_driver_ids[] = {
2671 .name = "s3c6400-uart",
2672 .driver_data = (kernel_ulong_t)S3C6400_SERIAL_DRV_DATA,
2674 .name = "s5pv210-uart",
2675 .driver_data = (kernel_ulong_t)S5PV210_SERIAL_DRV_DATA,
2677 .name = "exynos4210-uart",
2678 .driver_data = (kernel_ulong_t)EXYNOS4210_SERIAL_DRV_DATA,
2680 .name = "exynos5433-uart",
2681 .driver_data = (kernel_ulong_t)EXYNOS5433_SERIAL_DRV_DATA,
2684 .driver_data = (kernel_ulong_t)S5L_SERIAL_DRV_DATA,
2686 .name = "exynos850-uart",
2687 .driver_data = (kernel_ulong_t)EXYNOS850_SERIAL_DRV_DATA,
2689 .name = "artpec8-uart",
2690 .driver_data = (kernel_ulong_t)ARTPEC8_SERIAL_DRV_DATA,
2694 MODULE_DEVICE_TABLE(platform, s3c24xx_serial_driver_ids);
2697 static const struct of_device_id s3c24xx_uart_dt_match[] = {
2698 { .compatible = "samsung,s3c6400-uart",
2699 .data = S3C6400_SERIAL_DRV_DATA },
2700 { .compatible = "samsung,s5pv210-uart",
2701 .data = S5PV210_SERIAL_DRV_DATA },
2702 { .compatible = "samsung,exynos4210-uart",
2703 .data = EXYNOS4210_SERIAL_DRV_DATA },
2704 { .compatible = "samsung,exynos5433-uart",
2705 .data = EXYNOS5433_SERIAL_DRV_DATA },
2706 { .compatible = "apple,s5l-uart",
2707 .data = S5L_SERIAL_DRV_DATA },
2708 { .compatible = "samsung,exynos850-uart",
2709 .data = EXYNOS850_SERIAL_DRV_DATA },
2710 { .compatible = "axis,artpec8-uart",
2711 .data = ARTPEC8_SERIAL_DRV_DATA },
2714 MODULE_DEVICE_TABLE(of, s3c24xx_uart_dt_match);
2717 static struct platform_driver samsung_serial_driver = {
2718 .probe = s3c24xx_serial_probe,
2719 .remove = s3c24xx_serial_remove,
2720 .id_table = s3c24xx_serial_driver_ids,
2722 .name = "samsung-uart",
2723 .pm = SERIAL_SAMSUNG_PM_OPS,
2724 .of_match_table = of_match_ptr(s3c24xx_uart_dt_match),
2728 static int __init samsung_serial_init(void)
2732 s3c24xx_serial_register_console();
2734 ret = platform_driver_register(&samsung_serial_driver);
2736 s3c24xx_serial_unregister_console();
2743 static void __exit samsung_serial_exit(void)
2745 platform_driver_unregister(&samsung_serial_driver);
2746 s3c24xx_serial_unregister_console();
2749 module_init(samsung_serial_init);
2750 module_exit(samsung_serial_exit);
2752 #ifdef CONFIG_SERIAL_SAMSUNG_CONSOLE
2757 static void wr_reg_barrier(const struct uart_port *port, u32 reg, u32 val)
2759 switch (port->iotype) {
2761 writeb(val, portaddr(port, reg));
2764 writel(val, portaddr(port, reg));
2769 struct samsung_early_console_data {
2774 static void samsung_early_busyuart(const struct uart_port *port)
2776 while (!(readl(port->membase + S3C2410_UTRSTAT) & S3C2410_UTRSTAT_TXFE))
2780 static void samsung_early_busyuart_fifo(const struct uart_port *port)
2782 const struct samsung_early_console_data *data = port->private_data;
2784 while (readl(port->membase + S3C2410_UFSTAT) & data->txfull_mask)
2788 static void samsung_early_putc(struct uart_port *port, unsigned char c)
2790 if (readl(port->membase + S3C2410_UFCON) & S3C2410_UFCON_FIFOMODE)
2791 samsung_early_busyuart_fifo(port);
2793 samsung_early_busyuart(port);
2795 wr_reg_barrier(port, S3C2410_UTXH, c);
2798 static void samsung_early_write(struct console *con, const char *s,
2801 struct earlycon_device *dev = con->data;
2803 uart_console_write(&dev->port, s, n, samsung_early_putc);
2806 static int samsung_early_read(struct console *con, char *s, unsigned int n)
2808 struct earlycon_device *dev = con->data;
2809 const struct samsung_early_console_data *data = dev->port.private_data;
2810 int ch, ufstat, num_read = 0;
2812 while (num_read < n) {
2813 ufstat = rd_regl(&dev->port, S3C2410_UFSTAT);
2814 if (!(ufstat & data->rxfifo_mask))
2816 ch = rd_reg(&dev->port, S3C2410_URXH);
2817 if (ch == NO_POLL_CHAR)
2826 static int __init samsung_early_console_setup(struct earlycon_device *device,
2829 if (!device->port.membase)
2832 device->con->write = samsung_early_write;
2833 device->con->read = samsung_early_read;
2838 static struct samsung_early_console_data s3c2410_early_console_data = {
2839 .txfull_mask = S3C2410_UFSTAT_TXFULL,
2840 .rxfifo_mask = S3C2410_UFSTAT_RXFULL | S3C2410_UFSTAT_RXMASK,
2843 static int __init s3c2410_early_console_setup(struct earlycon_device *device,
2846 device->port.private_data = &s3c2410_early_console_data;
2847 return samsung_early_console_setup(device, opt);
2850 OF_EARLYCON_DECLARE(s3c2410, "samsung,s3c2410-uart",
2851 s3c2410_early_console_setup);
2853 /* S3C2412, S3C2440, S3C64xx */
2854 static struct samsung_early_console_data s3c2440_early_console_data = {
2855 .txfull_mask = S3C2440_UFSTAT_TXFULL,
2856 .rxfifo_mask = S3C2440_UFSTAT_RXFULL | S3C2440_UFSTAT_RXMASK,
2859 static int __init s3c2440_early_console_setup(struct earlycon_device *device,
2862 device->port.private_data = &s3c2440_early_console_data;
2863 return samsung_early_console_setup(device, opt);
2866 OF_EARLYCON_DECLARE(s3c2412, "samsung,s3c2412-uart",
2867 s3c2440_early_console_setup);
2868 OF_EARLYCON_DECLARE(s3c2440, "samsung,s3c2440-uart",
2869 s3c2440_early_console_setup);
2870 OF_EARLYCON_DECLARE(s3c6400, "samsung,s3c6400-uart",
2871 s3c2440_early_console_setup);
2873 /* S5PV210, Exynos */
2874 static struct samsung_early_console_data s5pv210_early_console_data = {
2875 .txfull_mask = S5PV210_UFSTAT_TXFULL,
2876 .rxfifo_mask = S5PV210_UFSTAT_RXFULL | S5PV210_UFSTAT_RXMASK,
2879 static int __init s5pv210_early_console_setup(struct earlycon_device *device,
2882 device->port.private_data = &s5pv210_early_console_data;
2883 return samsung_early_console_setup(device, opt);
2886 OF_EARLYCON_DECLARE(s5pv210, "samsung,s5pv210-uart",
2887 s5pv210_early_console_setup);
2888 OF_EARLYCON_DECLARE(exynos4210, "samsung,exynos4210-uart",
2889 s5pv210_early_console_setup);
2890 OF_EARLYCON_DECLARE(artpec8, "axis,artpec8-uart",
2891 s5pv210_early_console_setup);
2894 static int __init apple_s5l_early_console_setup(struct earlycon_device *device,
2897 /* Close enough to S3C2410 for earlycon... */
2898 device->port.private_data = &s3c2410_early_console_data;
2901 /* ... but we need to override the existing fixmap entry as nGnRnE */
2902 __set_fixmap(FIX_EARLYCON_MEM_BASE, device->port.mapbase,
2903 __pgprot(PROT_DEVICE_nGnRnE));
2905 return samsung_early_console_setup(device, opt);
2908 OF_EARLYCON_DECLARE(s5l, "apple,s5l-uart", apple_s5l_early_console_setup);
2911 MODULE_ALIAS("platform:samsung-uart");
2912 MODULE_DESCRIPTION("Samsung SoC Serial port driver");
2914 MODULE_LICENSE("GPL v2");