blob: 96c694931832991827f40eefab645b81e096fac1 [file] [log] [blame]
Greg Kroah-Hartmane3b3d0f2017-11-06 18:11:51 +01001// SPDX-License-Identifier: GPL-2.0
Jovi Zhang99edb3d2011-03-30 05:30:41 -04002/*
Ben Dooksb4975492008-07-03 12:32:51 +01003 * Driver core for Samsung SoC onboard UARTs.
4 *
Ben Dooksccae9412009-11-13 22:54:14 +00005 * Ben Dooks, Copyright (c) 2003-2008 Simtec Electronics
Ben Dooksb4975492008-07-03 12:32:51 +01006 * http://armlinux.simtec.co.uk/
Ben Dooksb4975492008-07-03 12:32:51 +01007*/
8
9/* Hote 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#if defined(CONFIG_SERIAL_SAMSUNG_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
25#define SUPPORT_SYSRQ
26#endif
27
Robert Baldyga62c37ee2014-12-10 12:49:25 +010028#include <linux/dmaengine.h>
29#include <linux/dma-mapping.h>
30#include <linux/slab.h>
Ben Dooksb4975492008-07-03 12:32:51 +010031#include <linux/module.h>
32#include <linux/ioport.h>
33#include <linux/io.h>
34#include <linux/platform_device.h>
35#include <linux/init.h>
36#include <linux/sysrq.h>
37#include <linux/console.h>
38#include <linux/tty.h>
39#include <linux/tty_flip.h>
40#include <linux/serial_core.h>
41#include <linux/serial.h>
Arnd Bergmann9ee51f02013-04-11 02:04:48 +020042#include <linux/serial_s3c.h>
Ben Dooksb4975492008-07-03 12:32:51 +010043#include <linux/delay.h>
44#include <linux/clk.h>
Ben Dooks30555472008-10-21 14:06:36 +010045#include <linux/cpufreq.h>
Thomas Abraham26c919e2011-11-06 22:10:44 +053046#include <linux/of.h>
Ben Dooksb4975492008-07-03 12:32:51 +010047#include <asm/irq.h>
48
Ben Dooksb4975492008-07-03 12:32:51 +010049/* UART name and device definitions */
50
51#define S3C24XX_SERIAL_NAME "ttySAC"
52#define S3C24XX_SERIAL_MAJOR 204
53#define S3C24XX_SERIAL_MINOR 64
54
Robert Baldyga29bef792014-12-10 12:49:26 +010055#define S3C24XX_TX_PIO 1
56#define S3C24XX_TX_DMA 2
Robert Baldygab543c302014-12-10 12:49:27 +010057#define S3C24XX_RX_PIO 1
58#define S3C24XX_RX_DMA 2
Ben Dooksb4975492008-07-03 12:32:51 +010059/* macros to change one thing to another */
60
61#define tx_enabled(port) ((port)->unused[0])
62#define rx_enabled(port) ((port)->unused[1])
63
Lucas De Marchi25985ed2011-03-30 22:57:33 -030064/* flag to ignore all characters coming in */
Ben Dooksb4975492008-07-03 12:32:51 +010065#define RXSTAT_DUMMY_READ (0x10000000)
66
Greg Kroah-Hartman43df1702019-12-10 15:37:01 +010067struct s3c24xx_uart_info {
68 char *name;
69 unsigned int type;
70 unsigned int fifosize;
71 unsigned long rx_fifomask;
72 unsigned long rx_fifoshift;
73 unsigned long rx_fifofull;
74 unsigned long tx_fifomask;
75 unsigned long tx_fifoshift;
76 unsigned long tx_fifofull;
77 unsigned int def_clk_sel;
78 unsigned long num_clks;
79 unsigned long clksel_mask;
80 unsigned long clksel_shift;
81
82 /* uart port features */
83
84 unsigned int has_divslot:1;
85};
86
87struct s3c24xx_serial_drv_data {
88 struct s3c24xx_uart_info *info;
89 struct s3c2410_uartcfg *def_cfg;
90 unsigned int fifosize[CONFIG_SERIAL_SAMSUNG_UARTS];
91};
92
93struct 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
123struct s3c24xx_uart_port {
124 unsigned char rx_claimed;
125 unsigned char tx_claimed;
126 unsigned int pm_level;
127 unsigned long baudclk_rate;
128 unsigned int min_dma_size;
129
130 unsigned int rx_irq;
131 unsigned int tx_irq;
132
133 unsigned int tx_in_progress;
134 unsigned int tx_mode;
135 unsigned int rx_mode;
136
137 struct s3c24xx_uart_info *info;
138 struct clk *clk;
139 struct clk *baudclk;
140 struct uart_port port;
141 struct s3c24xx_serial_drv_data *drv_data;
142
143 /* reference to platform data */
144 struct s3c2410_uartcfg *cfg;
145
146 struct s3c24xx_uart_dma *dma;
147
148#ifdef CONFIG_ARM_S3C24XX_CPUFREQ
149 struct notifier_block freq_transition;
150#endif
151};
152
153/* conversion functions */
154
155#define s3c24xx_dev_to_port(__dev) dev_get_drvdata(__dev)
156
157/* register access controls */
158
159#define portaddr(port, reg) ((port)->membase + (reg))
160#define portaddrl(port, reg) \
161 ((unsigned long *)(unsigned long)((port)->membase + (reg)))
162
163#define rd_regb(port, reg) (readb_relaxed(portaddr(port, reg)))
164#define rd_regl(port, reg) (readl_relaxed(portaddr(port, reg)))
165
166#define wr_regb(port, reg, val) writeb_relaxed(val, portaddr(port, reg))
167#define wr_regl(port, reg, val) writel_relaxed(val, portaddr(port, reg))
168
169/* Byte-order aware bit setting/clearing functions. */
170
171static inline void s3c24xx_set_bit(struct uart_port *port, int idx,
172 unsigned int reg)
173{
174 unsigned long flags;
175 u32 val;
176
177 local_irq_save(flags);
178 val = rd_regl(port, reg);
179 val |= (1 << idx);
180 wr_regl(port, reg, val);
181 local_irq_restore(flags);
182}
183
184static inline void s3c24xx_clear_bit(struct uart_port *port, int idx,
185 unsigned int reg)
186{
187 unsigned long flags;
188 u32 val;
189
190 local_irq_save(flags);
191 val = rd_regl(port, reg);
192 val &= ~(1 << idx);
193 wr_regl(port, reg, val);
194 local_irq_restore(flags);
195}
196
Ben Dooksb4975492008-07-03 12:32:51 +0100197static inline struct s3c24xx_uart_port *to_ourport(struct uart_port *port)
198{
199 return container_of(port, struct s3c24xx_uart_port, port);
200}
201
202/* translate a port to the device name */
203
204static inline const char *s3c24xx_serial_portname(struct uart_port *port)
205{
206 return to_platform_device(port->dev)->name;
207}
208
209static int s3c24xx_serial_txempty_nofifo(struct uart_port *port)
210{
Sachin Kamat9303ac12012-09-05 10:30:11 +0530211 return rd_regl(port, S3C2410_UTRSTAT) & S3C2410_UTRSTAT_TXE;
Ben Dooksb4975492008-07-03 12:32:51 +0100212}
213
Thomas Abraham88bb4ea2011-08-10 15:51:19 +0530214/*
215 * s3c64xx and later SoC's include the interrupt mask and status registers in
216 * the controller itself, unlike the s3c24xx SoC's which have these registers
217 * in the interrupt controller. Check if the port type is s3c64xx or higher.
218 */
219static int s3c24xx_serial_has_interrupt_mask(struct uart_port *port)
220{
221 return to_ourport(port)->info->type == PORT_S3C6400;
222}
223
Ben Dooksb4975492008-07-03 12:32:51 +0100224static void s3c24xx_serial_rx_enable(struct uart_port *port)
225{
226 unsigned long flags;
227 unsigned int ucon, ufcon;
228 int count = 10000;
229
230 spin_lock_irqsave(&port->lock, flags);
231
232 while (--count && !s3c24xx_serial_txempty_nofifo(port))
233 udelay(100);
234
235 ufcon = rd_regl(port, S3C2410_UFCON);
236 ufcon |= S3C2410_UFCON_RESETRX;
237 wr_regl(port, S3C2410_UFCON, ufcon);
238
239 ucon = rd_regl(port, S3C2410_UCON);
240 ucon |= S3C2410_UCON_RXIRQMODE;
241 wr_regl(port, S3C2410_UCON, ucon);
242
243 rx_enabled(port) = 1;
244 spin_unlock_irqrestore(&port->lock, flags);
245}
246
247static void s3c24xx_serial_rx_disable(struct uart_port *port)
248{
249 unsigned long flags;
250 unsigned int ucon;
251
252 spin_lock_irqsave(&port->lock, flags);
253
254 ucon = rd_regl(port, S3C2410_UCON);
255 ucon &= ~S3C2410_UCON_RXIRQMODE;
256 wr_regl(port, S3C2410_UCON, ucon);
257
258 rx_enabled(port) = 0;
259 spin_unlock_irqrestore(&port->lock, flags);
260}
261
262static void s3c24xx_serial_stop_tx(struct uart_port *port)
263{
Ben Dooksb73c289c2008-10-21 14:07:04 +0100264 struct s3c24xx_uart_port *ourport = to_ourport(port);
Robert Baldyga29bef792014-12-10 12:49:26 +0100265 struct s3c24xx_uart_dma *dma = ourport->dma;
266 struct circ_buf *xmit = &port->state->xmit;
267 struct dma_tx_state state;
268 int count;
Ben Dooksb73c289c2008-10-21 14:07:04 +0100269
Robert Baldyga29bef792014-12-10 12:49:26 +0100270 if (!tx_enabled(port))
271 return;
272
273 if (s3c24xx_serial_has_interrupt_mask(port))
Matthew Leachbbb5ff92016-06-22 17:57:03 +0100274 s3c24xx_set_bit(port, S3C64XX_UINTM_TXD, S3C64XX_UINTM);
Robert Baldyga29bef792014-12-10 12:49:26 +0100275 else
276 disable_irq_nosync(ourport->tx_irq);
277
278 if (dma && dma->tx_chan && ourport->tx_in_progress == S3C24XX_TX_DMA) {
279 dmaengine_pause(dma->tx_chan);
280 dmaengine_tx_status(dma->tx_chan, dma->tx_cookie, &state);
281 dmaengine_terminate_all(dma->tx_chan);
282 dma_sync_single_for_cpu(ourport->port.dev,
283 dma->tx_transfer_addr, dma->tx_size, DMA_TO_DEVICE);
284 async_tx_ack(dma->tx_desc);
285 count = dma->tx_bytes_requested - state.residue;
286 xmit->tail = (xmit->tail + count) & (UART_XMIT_SIZE - 1);
287 port->icount.tx += count;
Ben Dooksb4975492008-07-03 12:32:51 +0100288 }
Robert Baldyga29bef792014-12-10 12:49:26 +0100289
290 tx_enabled(port) = 0;
291 ourport->tx_in_progress = 0;
292
293 if (port->flags & UPF_CONS_FLOW)
294 s3c24xx_serial_rx_enable(port);
295
296 ourport->tx_mode = 0;
Ben Dooksb4975492008-07-03 12:32:51 +0100297}
298
Robert Baldyga29bef792014-12-10 12:49:26 +0100299static void s3c24xx_serial_start_next_tx(struct s3c24xx_uart_port *ourport);
300
301static void s3c24xx_serial_tx_dma_complete(void *args)
302{
303 struct s3c24xx_uart_port *ourport = args;
304 struct uart_port *port = &ourport->port;
305 struct circ_buf *xmit = &port->state->xmit;
306 struct s3c24xx_uart_dma *dma = ourport->dma;
307 struct dma_tx_state state;
308 unsigned long flags;
309 int count;
310
311
312 dmaengine_tx_status(dma->tx_chan, dma->tx_cookie, &state);
313 count = dma->tx_bytes_requested - state.residue;
314 async_tx_ack(dma->tx_desc);
315
316 dma_sync_single_for_cpu(ourport->port.dev, dma->tx_transfer_addr,
317 dma->tx_size, DMA_TO_DEVICE);
318
319 spin_lock_irqsave(&port->lock, flags);
320
321 xmit->tail = (xmit->tail + count) & (UART_XMIT_SIZE - 1);
322 port->icount.tx += count;
323 ourport->tx_in_progress = 0;
324
325 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
326 uart_write_wakeup(port);
327
328 s3c24xx_serial_start_next_tx(ourport);
329 spin_unlock_irqrestore(&port->lock, flags);
330}
331
332static void enable_tx_dma(struct s3c24xx_uart_port *ourport)
333{
334 struct uart_port *port = &ourport->port;
335 u32 ucon;
336
337 /* Mask Tx interrupt */
338 if (s3c24xx_serial_has_interrupt_mask(port))
Matthew Leachbbb5ff92016-06-22 17:57:03 +0100339 s3c24xx_set_bit(port, S3C64XX_UINTM_TXD, S3C64XX_UINTM);
Robert Baldyga29bef792014-12-10 12:49:26 +0100340 else
341 disable_irq_nosync(ourport->tx_irq);
342
343 /* Enable tx dma mode */
344 ucon = rd_regl(port, S3C2410_UCON);
345 ucon &= ~(S3C64XX_UCON_TXBURST_MASK | S3C64XX_UCON_TXMODE_MASK);
346 ucon |= (dma_get_cache_alignment() >= 16) ?
347 S3C64XX_UCON_TXBURST_16 : S3C64XX_UCON_TXBURST_1;
348 ucon |= S3C64XX_UCON_TXMODE_DMA;
349 wr_regl(port, S3C2410_UCON, ucon);
350
351 ourport->tx_mode = S3C24XX_TX_DMA;
352}
353
354static void enable_tx_pio(struct s3c24xx_uart_port *ourport)
355{
356 struct uart_port *port = &ourport->port;
357 u32 ucon, ufcon;
358
359 /* Set ufcon txtrig */
360 ourport->tx_in_progress = S3C24XX_TX_PIO;
361 ufcon = rd_regl(port, S3C2410_UFCON);
362 wr_regl(port, S3C2410_UFCON, ufcon);
363
364 /* Enable tx pio mode */
365 ucon = rd_regl(port, S3C2410_UCON);
366 ucon &= ~(S3C64XX_UCON_TXMODE_MASK);
367 ucon |= S3C64XX_UCON_TXMODE_CPU;
368 wr_regl(port, S3C2410_UCON, ucon);
369
370 /* Unmask Tx interrupt */
371 if (s3c24xx_serial_has_interrupt_mask(port))
Matthew Leachbbb5ff92016-06-22 17:57:03 +0100372 s3c24xx_clear_bit(port, S3C64XX_UINTM_TXD,
373 S3C64XX_UINTM);
Robert Baldyga29bef792014-12-10 12:49:26 +0100374 else
375 enable_irq(ourport->tx_irq);
376
377 ourport->tx_mode = S3C24XX_TX_PIO;
378}
379
380static void s3c24xx_serial_start_tx_pio(struct s3c24xx_uart_port *ourport)
381{
382 if (ourport->tx_mode != S3C24XX_TX_PIO)
383 enable_tx_pio(ourport);
384}
385
386static int s3c24xx_serial_start_tx_dma(struct s3c24xx_uart_port *ourport,
387 unsigned int count)
388{
389 struct uart_port *port = &ourport->port;
390 struct circ_buf *xmit = &port->state->xmit;
391 struct s3c24xx_uart_dma *dma = ourport->dma;
392
393
394 if (ourport->tx_mode != S3C24XX_TX_DMA)
395 enable_tx_dma(ourport);
396
Robert Baldyga29bef792014-12-10 12:49:26 +0100397 dma->tx_size = count & ~(dma_get_cache_alignment() - 1);
398 dma->tx_transfer_addr = dma->tx_addr + xmit->tail;
399
400 dma_sync_single_for_device(ourport->port.dev, dma->tx_transfer_addr,
401 dma->tx_size, DMA_TO_DEVICE);
402
403 dma->tx_desc = dmaengine_prep_slave_single(dma->tx_chan,
404 dma->tx_transfer_addr, dma->tx_size,
405 DMA_MEM_TO_DEV, DMA_PREP_INTERRUPT);
406 if (!dma->tx_desc) {
407 dev_err(ourport->port.dev, "Unable to get desc for Tx\n");
408 return -EIO;
409 }
410
411 dma->tx_desc->callback = s3c24xx_serial_tx_dma_complete;
412 dma->tx_desc->callback_param = ourport;
413 dma->tx_bytes_requested = dma->tx_size;
414
415 ourport->tx_in_progress = S3C24XX_TX_DMA;
416 dma->tx_cookie = dmaengine_submit(dma->tx_desc);
417 dma_async_issue_pending(dma->tx_chan);
418 return 0;
419}
420
421static void s3c24xx_serial_start_next_tx(struct s3c24xx_uart_port *ourport)
422{
423 struct uart_port *port = &ourport->port;
424 struct circ_buf *xmit = &port->state->xmit;
425 unsigned long count;
426
427 /* Get data size up to the end of buffer */
428 count = CIRC_CNT_TO_END(xmit->head, xmit->tail, UART_XMIT_SIZE);
429
430 if (!count) {
431 s3c24xx_serial_stop_tx(port);
432 return;
433 }
434
Marek Szyprowski81ccb2a2015-07-31 10:58:27 +0200435 if (!ourport->dma || !ourport->dma->tx_chan ||
Robert Baldyga736cd792015-07-31 10:58:28 +0200436 count < ourport->min_dma_size ||
437 xmit->tail & (dma_get_cache_alignment() - 1))
Robert Baldyga29bef792014-12-10 12:49:26 +0100438 s3c24xx_serial_start_tx_pio(ourport);
439 else
440 s3c24xx_serial_start_tx_dma(ourport, count);
441}
442
Krzysztof Kozlowski75781972015-05-02 00:40:04 +0900443static void s3c24xx_serial_start_tx(struct uart_port *port)
Ben Dooksb4975492008-07-03 12:32:51 +0100444{
Ben Dooksb73c289c2008-10-21 14:07:04 +0100445 struct s3c24xx_uart_port *ourport = to_ourport(port);
Robert Baldyga29bef792014-12-10 12:49:26 +0100446 struct circ_buf *xmit = &port->state->xmit;
Ben Dooksb73c289c2008-10-21 14:07:04 +0100447
Ben Dooksb4975492008-07-03 12:32:51 +0100448 if (!tx_enabled(port)) {
449 if (port->flags & UPF_CONS_FLOW)
450 s3c24xx_serial_rx_disable(port);
451
Ben Dooksb4975492008-07-03 12:32:51 +0100452 tx_enabled(port) = 1;
Robert Baldygaba019a32015-01-28 14:44:23 +0100453 if (!ourport->dma || !ourport->dma->tx_chan)
Robert Baldyga29bef792014-12-10 12:49:26 +0100454 s3c24xx_serial_start_tx_pio(ourport);
Robert Baldyga29bef792014-12-10 12:49:26 +0100455 }
456
457 if (ourport->dma && ourport->dma->tx_chan) {
458 if (!uart_circ_empty(xmit) && !ourport->tx_in_progress)
459 s3c24xx_serial_start_next_tx(ourport);
Ben Dooksb4975492008-07-03 12:32:51 +0100460 }
461}
462
Robert Baldygab543c302014-12-10 12:49:27 +0100463static void s3c24xx_uart_copy_rx_to_tty(struct s3c24xx_uart_port *ourport,
464 struct tty_port *tty, int count)
465{
466 struct s3c24xx_uart_dma *dma = ourport->dma;
467 int copied;
468
469 if (!count)
470 return;
471
472 dma_sync_single_for_cpu(ourport->port.dev, dma->rx_addr,
473 dma->rx_size, DMA_FROM_DEVICE);
474
475 ourport->port.icount.rx += count;
476 if (!tty) {
477 dev_err(ourport->port.dev, "No tty port\n");
478 return;
479 }
480 copied = tty_insert_flip_string(tty,
481 ((unsigned char *)(ourport->dma->rx_buf)), count);
482 if (copied != count) {
483 WARN_ON(1);
484 dev_err(ourport->port.dev, "RxData copy to tty layer failed\n");
485 }
486}
487
Ben Dooksb4975492008-07-03 12:32:51 +0100488static void s3c24xx_serial_stop_rx(struct uart_port *port)
489{
Ben Dooksb73c289c2008-10-21 14:07:04 +0100490 struct s3c24xx_uart_port *ourport = to_ourport(port);
Robert Baldygab543c302014-12-10 12:49:27 +0100491 struct s3c24xx_uart_dma *dma = ourport->dma;
492 struct tty_port *t = &port->state->port;
493 struct dma_tx_state state;
494 enum dma_status dma_status;
495 unsigned int received;
Ben Dooksb73c289c2008-10-21 14:07:04 +0100496
Ben Dooksb4975492008-07-03 12:32:51 +0100497 if (rx_enabled(port)) {
Greg Kroah-Hartmana05025d2019-12-10 15:37:03 +0100498 dev_dbg(port->dev, "stopping rx\n");
Thomas Abraham88bb4ea2011-08-10 15:51:19 +0530499 if (s3c24xx_serial_has_interrupt_mask(port))
Matthew Leachbbb5ff92016-06-22 17:57:03 +0100500 s3c24xx_set_bit(port, S3C64XX_UINTM_RXD,
501 S3C64XX_UINTM);
Thomas Abraham88bb4ea2011-08-10 15:51:19 +0530502 else
503 disable_irq_nosync(ourport->rx_irq);
Ben Dooksb4975492008-07-03 12:32:51 +0100504 rx_enabled(port) = 0;
505 }
Robert Baldygab543c302014-12-10 12:49:27 +0100506 if (dma && dma->rx_chan) {
507 dmaengine_pause(dma->tx_chan);
508 dma_status = dmaengine_tx_status(dma->rx_chan,
509 dma->rx_cookie, &state);
510 if (dma_status == DMA_IN_PROGRESS ||
511 dma_status == DMA_PAUSED) {
512 received = dma->rx_bytes_requested - state.residue;
513 dmaengine_terminate_all(dma->rx_chan);
514 s3c24xx_uart_copy_rx_to_tty(ourport, t, received);
515 }
516 }
Ben Dooksb4975492008-07-03 12:32:51 +0100517}
518
Robert Baldygaef4aca72014-11-24 07:56:22 +0100519static inline struct s3c24xx_uart_info
520 *s3c24xx_port_to_info(struct uart_port *port)
Ben Dooksb4975492008-07-03 12:32:51 +0100521{
522 return to_ourport(port)->info;
523}
524
Robert Baldygaef4aca72014-11-24 07:56:22 +0100525static inline struct s3c2410_uartcfg
526 *s3c24xx_port_to_cfg(struct uart_port *port)
Ben Dooksb4975492008-07-03 12:32:51 +0100527{
Thomas Abraham4d84e972011-10-24 11:47:25 +0200528 struct s3c24xx_uart_port *ourport;
529
Ben Dooksb4975492008-07-03 12:32:51 +0100530 if (port->dev == NULL)
531 return NULL;
532
Thomas Abraham4d84e972011-10-24 11:47:25 +0200533 ourport = container_of(port, struct s3c24xx_uart_port, port);
534 return ourport->cfg;
Ben Dooksb4975492008-07-03 12:32:51 +0100535}
536
537static int s3c24xx_serial_rx_fifocnt(struct s3c24xx_uart_port *ourport,
538 unsigned long ufstat)
539{
540 struct s3c24xx_uart_info *info = ourport->info;
541
542 if (ufstat & info->rx_fifofull)
Thomas Abrahamda121502011-11-02 19:23:25 +0900543 return ourport->port.fifosize;
Ben Dooksb4975492008-07-03 12:32:51 +0100544
545 return (ufstat & info->rx_fifomask) >> info->rx_fifoshift;
546}
547
Robert Baldygab543c302014-12-10 12:49:27 +0100548static void s3c64xx_start_rx_dma(struct s3c24xx_uart_port *ourport);
549static void s3c24xx_serial_rx_dma_complete(void *args)
550{
551 struct s3c24xx_uart_port *ourport = args;
552 struct uart_port *port = &ourport->port;
553
554 struct s3c24xx_uart_dma *dma = ourport->dma;
555 struct tty_port *t = &port->state->port;
556 struct tty_struct *tty = tty_port_tty_get(&ourport->port.state->port);
557
558 struct dma_tx_state state;
559 unsigned long flags;
560 int received;
561
562 dmaengine_tx_status(dma->rx_chan, dma->rx_cookie, &state);
563 received = dma->rx_bytes_requested - state.residue;
564 async_tx_ack(dma->rx_desc);
565
566 spin_lock_irqsave(&port->lock, flags);
567
568 if (received)
569 s3c24xx_uart_copy_rx_to_tty(ourport, t, received);
570
571 if (tty) {
572 tty_flip_buffer_push(t);
573 tty_kref_put(tty);
574 }
575
576 s3c64xx_start_rx_dma(ourport);
577
578 spin_unlock_irqrestore(&port->lock, flags);
579}
580
581static void s3c64xx_start_rx_dma(struct s3c24xx_uart_port *ourport)
582{
583 struct s3c24xx_uart_dma *dma = ourport->dma;
584
585 dma_sync_single_for_device(ourport->port.dev, dma->rx_addr,
586 dma->rx_size, DMA_FROM_DEVICE);
587
588 dma->rx_desc = dmaengine_prep_slave_single(dma->rx_chan,
589 dma->rx_addr, dma->rx_size, DMA_DEV_TO_MEM,
590 DMA_PREP_INTERRUPT);
591 if (!dma->rx_desc) {
592 dev_err(ourport->port.dev, "Unable to get desc for Rx\n");
593 return;
594 }
595
596 dma->rx_desc->callback = s3c24xx_serial_rx_dma_complete;
597 dma->rx_desc->callback_param = ourport;
598 dma->rx_bytes_requested = dma->rx_size;
599
600 dma->rx_cookie = dmaengine_submit(dma->rx_desc);
601 dma_async_issue_pending(dma->rx_chan);
602}
Ben Dooksb4975492008-07-03 12:32:51 +0100603
604/* ? - where has parity gone?? */
605#define S3C2410_UERSTAT_PARITY (0x1000)
606
Robert Baldygab543c302014-12-10 12:49:27 +0100607static void enable_rx_dma(struct s3c24xx_uart_port *ourport)
608{
609 struct uart_port *port = &ourport->port;
610 unsigned int ucon;
611
612 /* set Rx mode to DMA mode */
613 ucon = rd_regl(port, S3C2410_UCON);
614 ucon &= ~(S3C64XX_UCON_RXBURST_MASK |
615 S3C64XX_UCON_TIMEOUT_MASK |
616 S3C64XX_UCON_EMPTYINT_EN |
617 S3C64XX_UCON_DMASUS_EN |
618 S3C64XX_UCON_TIMEOUT_EN |
619 S3C64XX_UCON_RXMODE_MASK);
620 ucon |= S3C64XX_UCON_RXBURST_16 |
621 0xf << S3C64XX_UCON_TIMEOUT_SHIFT |
622 S3C64XX_UCON_EMPTYINT_EN |
623 S3C64XX_UCON_TIMEOUT_EN |
624 S3C64XX_UCON_RXMODE_DMA;
625 wr_regl(port, S3C2410_UCON, ucon);
626
627 ourport->rx_mode = S3C24XX_RX_DMA;
628}
629
630static void enable_rx_pio(struct s3c24xx_uart_port *ourport)
631{
632 struct uart_port *port = &ourport->port;
633 unsigned int ucon;
634
635 /* set Rx mode to DMA mode */
636 ucon = rd_regl(port, S3C2410_UCON);
637 ucon &= ~(S3C64XX_UCON_TIMEOUT_MASK |
638 S3C64XX_UCON_EMPTYINT_EN |
639 S3C64XX_UCON_DMASUS_EN |
640 S3C64XX_UCON_TIMEOUT_EN |
641 S3C64XX_UCON_RXMODE_MASK);
642 ucon |= 0xf << S3C64XX_UCON_TIMEOUT_SHIFT |
643 S3C64XX_UCON_TIMEOUT_EN |
644 S3C64XX_UCON_RXMODE_CPU;
645 wr_regl(port, S3C2410_UCON, ucon);
646
647 ourport->rx_mode = S3C24XX_RX_PIO;
648}
649
Robert Baldyga09557c02015-09-15 14:49:00 +0200650static void s3c24xx_serial_rx_drain_fifo(struct s3c24xx_uart_port *ourport);
651
Robert Baldygae4678af2015-09-15 14:48:57 +0200652static irqreturn_t s3c24xx_serial_rx_chars_dma(void *dev_id)
Robert Baldygab543c302014-12-10 12:49:27 +0100653{
Chen Wandun98aee0c2019-11-22 20:04:18 +0800654 unsigned int utrstat, received;
Robert Baldygab543c302014-12-10 12:49:27 +0100655 struct s3c24xx_uart_port *ourport = dev_id;
656 struct uart_port *port = &ourport->port;
657 struct s3c24xx_uart_dma *dma = ourport->dma;
658 struct tty_struct *tty = tty_port_tty_get(&ourport->port.state->port);
659 struct tty_port *t = &port->state->port;
660 unsigned long flags;
661 struct dma_tx_state state;
662
663 utrstat = rd_regl(port, S3C2410_UTRSTAT);
Chen Wandun98aee0c2019-11-22 20:04:18 +0800664 rd_regl(port, S3C2410_UFSTAT);
Robert Baldygab543c302014-12-10 12:49:27 +0100665
666 spin_lock_irqsave(&port->lock, flags);
667
668 if (!(utrstat & S3C2410_UTRSTAT_TIMEOUT)) {
669 s3c64xx_start_rx_dma(ourport);
670 if (ourport->rx_mode == S3C24XX_RX_PIO)
671 enable_rx_dma(ourport);
672 goto finish;
673 }
674
675 if (ourport->rx_mode == S3C24XX_RX_DMA) {
676 dmaengine_pause(dma->rx_chan);
677 dmaengine_tx_status(dma->rx_chan, dma->rx_cookie, &state);
678 dmaengine_terminate_all(dma->rx_chan);
679 received = dma->rx_bytes_requested - state.residue;
680 s3c24xx_uart_copy_rx_to_tty(ourport, t, received);
681
682 enable_rx_pio(ourport);
683 }
684
Robert Baldyga09557c02015-09-15 14:49:00 +0200685 s3c24xx_serial_rx_drain_fifo(ourport);
Robert Baldygab543c302014-12-10 12:49:27 +0100686
687 if (tty) {
688 tty_flip_buffer_push(t);
689 tty_kref_put(tty);
690 }
691
692 wr_regl(port, S3C2410_UTRSTAT, S3C2410_UTRSTAT_TIMEOUT);
693
694finish:
695 spin_unlock_irqrestore(&port->lock, flags);
696
697 return IRQ_HANDLED;
698}
699
Robert Baldyga01732dd2015-09-15 14:48:59 +0200700static void s3c24xx_serial_rx_drain_fifo(struct s3c24xx_uart_port *ourport)
Ben Dooksb4975492008-07-03 12:32:51 +0100701{
Ben Dooksb4975492008-07-03 12:32:51 +0100702 struct uart_port *port = &ourport->port;
Ben Dooksb4975492008-07-03 12:32:51 +0100703 unsigned int ufcon, ch, flag, ufstat, uerstat;
Youngmin Namaba06e92016-03-05 19:36:32 +0900704 unsigned int fifocnt = 0;
Robert Baldyga57850a52014-11-24 07:56:24 +0100705 int max_count = port->fifosize;
Ben Dooksb4975492008-07-03 12:32:51 +0100706
707 while (max_count-- > 0) {
Youngmin Namaba06e92016-03-05 19:36:32 +0900708 /*
709 * Receive all characters known to be in FIFO
710 * before reading FIFO level again
711 */
712 if (fifocnt == 0) {
713 ufstat = rd_regl(port, S3C2410_UFSTAT);
714 fifocnt = s3c24xx_serial_rx_fifocnt(ourport, ufstat);
715 if (fifocnt == 0)
716 break;
717 }
718 fifocnt--;
Ben Dooksb4975492008-07-03 12:32:51 +0100719
720 uerstat = rd_regl(port, S3C2410_UERSTAT);
721 ch = rd_regb(port, S3C2410_URXH);
722
723 if (port->flags & UPF_CONS_FLOW) {
724 int txe = s3c24xx_serial_txempty_nofifo(port);
725
726 if (rx_enabled(port)) {
727 if (!txe) {
728 rx_enabled(port) = 0;
729 continue;
730 }
731 } else {
732 if (txe) {
Youngmin Namaba06e92016-03-05 19:36:32 +0900733 ufcon = rd_regl(port, S3C2410_UFCON);
Ben Dooksb4975492008-07-03 12:32:51 +0100734 ufcon |= S3C2410_UFCON_RESETRX;
735 wr_regl(port, S3C2410_UFCON, ufcon);
736 rx_enabled(port) = 1;
Robert Baldyga01732dd2015-09-15 14:48:59 +0200737 return;
Ben Dooksb4975492008-07-03 12:32:51 +0100738 }
739 continue;
740 }
741 }
742
743 /* insert the character into the buffer */
744
745 flag = TTY_NORMAL;
746 port->icount.rx++;
747
748 if (unlikely(uerstat & S3C2410_UERSTAT_ANY)) {
Greg Kroah-Hartmana05025d2019-12-10 15:37:03 +0100749 dev_dbg(port->dev,
750 "rxerr: port ch=0x%02x, rxs=0x%08x\n",
751 ch, uerstat);
Ben Dooksb4975492008-07-03 12:32:51 +0100752
753 /* check for break */
754 if (uerstat & S3C2410_UERSTAT_BREAK) {
Greg Kroah-Hartmana05025d2019-12-10 15:37:03 +0100755 dev_dbg(port->dev, "break!\n");
Ben Dooksb4975492008-07-03 12:32:51 +0100756 port->icount.brk++;
757 if (uart_handle_break(port))
Robert Baldyga620bb212015-09-15 14:48:58 +0200758 continue; /* Ignore character */
Ben Dooksb4975492008-07-03 12:32:51 +0100759 }
760
761 if (uerstat & S3C2410_UERSTAT_FRAME)
762 port->icount.frame++;
763 if (uerstat & S3C2410_UERSTAT_OVERRUN)
764 port->icount.overrun++;
765
766 uerstat &= port->read_status_mask;
767
768 if (uerstat & S3C2410_UERSTAT_BREAK)
769 flag = TTY_BREAK;
770 else if (uerstat & S3C2410_UERSTAT_PARITY)
771 flag = TTY_PARITY;
772 else if (uerstat & (S3C2410_UERSTAT_FRAME |
773 S3C2410_UERSTAT_OVERRUN))
774 flag = TTY_FRAME;
775 }
776
777 if (uart_handle_sysrq_char(port, ch))
Robert Baldyga620bb212015-09-15 14:48:58 +0200778 continue; /* Ignore character */
Ben Dooksb4975492008-07-03 12:32:51 +0100779
780 uart_insert_char(port, uerstat, S3C2410_UERSTAT_OVERRUN,
781 ch, flag);
Ben Dooksb4975492008-07-03 12:32:51 +0100782 }
Viresh Kumarf5693ea2013-08-19 20:14:26 +0530783
Jiri Slaby2e124b42013-01-03 15:53:06 +0100784 tty_flip_buffer_push(&port->state->port);
Robert Baldyga01732dd2015-09-15 14:48:59 +0200785}
Ben Dooksb4975492008-07-03 12:32:51 +0100786
Robert Baldyga01732dd2015-09-15 14:48:59 +0200787static irqreturn_t s3c24xx_serial_rx_chars_pio(void *dev_id)
788{
789 struct s3c24xx_uart_port *ourport = dev_id;
790 struct uart_port *port = &ourport->port;
791 unsigned long flags;
792
793 spin_lock_irqsave(&port->lock, flags);
794 s3c24xx_serial_rx_drain_fifo(ourport);
795 spin_unlock_irqrestore(&port->lock, flags);
796
Ben Dooksb4975492008-07-03 12:32:51 +0100797 return IRQ_HANDLED;
798}
799
Robert Baldygab543c302014-12-10 12:49:27 +0100800
801static irqreturn_t s3c24xx_serial_rx_chars(int irq, void *dev_id)
802{
803 struct s3c24xx_uart_port *ourport = dev_id;
804
805 if (ourport->dma && ourport->dma->rx_chan)
Robert Baldygae4678af2015-09-15 14:48:57 +0200806 return s3c24xx_serial_rx_chars_dma(dev_id);
807 return s3c24xx_serial_rx_chars_pio(dev_id);
Robert Baldygab543c302014-12-10 12:49:27 +0100808}
809
Ben Dooksb4975492008-07-03 12:32:51 +0100810static irqreturn_t s3c24xx_serial_tx_chars(int irq, void *id)
811{
812 struct s3c24xx_uart_port *ourport = id;
813 struct uart_port *port = &ourport->port;
Alan Coxebd2c8f2009-09-19 13:13:28 -0700814 struct circ_buf *xmit = &port->state->xmit;
Thomas Abrahamc15c3742012-11-22 18:06:28 +0530815 unsigned long flags;
Robert Baldyga736cd792015-07-31 10:58:28 +0200816 int count, dma_count = 0;
Ben Dooksb4975492008-07-03 12:32:51 +0100817
Thomas Abrahamc15c3742012-11-22 18:06:28 +0530818 spin_lock_irqsave(&port->lock, flags);
819
Robert Baldyga29bef792014-12-10 12:49:26 +0100820 count = CIRC_CNT_TO_END(xmit->head, xmit->tail, UART_XMIT_SIZE);
821
Marek Szyprowski81ccb2a2015-07-31 10:58:27 +0200822 if (ourport->dma && ourport->dma->tx_chan &&
823 count >= ourport->min_dma_size) {
Robert Baldyga736cd792015-07-31 10:58:28 +0200824 int align = dma_get_cache_alignment() -
825 (xmit->tail & (dma_get_cache_alignment() - 1));
826 if (count-align >= ourport->min_dma_size) {
827 dma_count = count-align;
828 count = align;
829 }
Robert Baldyga29bef792014-12-10 12:49:26 +0100830 }
831
Ben Dooksb4975492008-07-03 12:32:51 +0100832 if (port->x_char) {
833 wr_regb(port, S3C2410_UTXH, port->x_char);
834 port->icount.tx++;
835 port->x_char = 0;
836 goto out;
837 }
838
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300839 /* if there isn't anything more to transmit, or the uart is now
Ben Dooksb4975492008-07-03 12:32:51 +0100840 * stopped, disable the uart and exit
841 */
842
843 if (uart_circ_empty(xmit) || uart_tx_stopped(port)) {
844 s3c24xx_serial_stop_tx(port);
845 goto out;
846 }
847
848 /* try and drain the buffer... */
849
Robert Baldyga736cd792015-07-31 10:58:28 +0200850 if (count > port->fifosize) {
851 count = port->fifosize;
852 dma_count = 0;
853 }
854
855 while (!uart_circ_empty(xmit) && count > 0) {
Ben Dooksb4975492008-07-03 12:32:51 +0100856 if (rd_regl(port, S3C2410_UFSTAT) & ourport->info->tx_fifofull)
857 break;
858
859 wr_regb(port, S3C2410_UTXH, xmit->buf[xmit->tail]);
860 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
861 port->icount.tx++;
Robert Baldyga736cd792015-07-31 10:58:28 +0200862 count--;
863 }
864
865 if (!count && dma_count) {
866 s3c24xx_serial_start_tx_dma(ourport, dma_count);
867 goto out;
Ben Dooksb4975492008-07-03 12:32:51 +0100868 }
869
Thomas Abrahamc15c3742012-11-22 18:06:28 +0530870 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS) {
871 spin_unlock(&port->lock);
Ben Dooksb4975492008-07-03 12:32:51 +0100872 uart_write_wakeup(port);
Thomas Abrahamc15c3742012-11-22 18:06:28 +0530873 spin_lock(&port->lock);
874 }
Ben Dooksb4975492008-07-03 12:32:51 +0100875
876 if (uart_circ_empty(xmit))
877 s3c24xx_serial_stop_tx(port);
878
Robert Baldygaef4aca72014-11-24 07:56:22 +0100879out:
Thomas Abrahamc15c3742012-11-22 18:06:28 +0530880 spin_unlock_irqrestore(&port->lock, flags);
Ben Dooksb4975492008-07-03 12:32:51 +0100881 return IRQ_HANDLED;
882}
883
Thomas Abraham88bb4ea2011-08-10 15:51:19 +0530884/* interrupt handler for s3c64xx and later SoC's.*/
885static irqreturn_t s3c64xx_serial_handle_irq(int irq, void *id)
886{
887 struct s3c24xx_uart_port *ourport = id;
888 struct uart_port *port = &ourport->port;
889 unsigned int pend = rd_regl(port, S3C64XX_UINTP);
Thomas Abraham88bb4ea2011-08-10 15:51:19 +0530890 irqreturn_t ret = IRQ_HANDLED;
891
Thomas Abraham88bb4ea2011-08-10 15:51:19 +0530892 if (pend & S3C64XX_UINTM_RXD_MSK) {
893 ret = s3c24xx_serial_rx_chars(irq, id);
894 wr_regl(port, S3C64XX_UINTP, S3C64XX_UINTM_RXD_MSK);
895 }
896 if (pend & S3C64XX_UINTM_TXD_MSK) {
897 ret = s3c24xx_serial_tx_chars(irq, id);
898 wr_regl(port, S3C64XX_UINTP, S3C64XX_UINTM_TXD_MSK);
899 }
Thomas Abraham88bb4ea2011-08-10 15:51:19 +0530900 return ret;
901}
902
Ben Dooksb4975492008-07-03 12:32:51 +0100903static unsigned int s3c24xx_serial_tx_empty(struct uart_port *port)
904{
905 struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
906 unsigned long ufstat = rd_regl(port, S3C2410_UFSTAT);
907 unsigned long ufcon = rd_regl(port, S3C2410_UFCON);
908
909 if (ufcon & S3C2410_UFCON_FIFOMODE) {
910 if ((ufstat & info->tx_fifomask) != 0 ||
911 (ufstat & info->tx_fifofull))
912 return 0;
913
914 return 1;
915 }
916
917 return s3c24xx_serial_txempty_nofifo(port);
918}
919
920/* no modem control lines */
921static unsigned int s3c24xx_serial_get_mctrl(struct uart_port *port)
922{
923 unsigned int umstat = rd_regb(port, S3C2410_UMSTAT);
924
925 if (umstat & S3C2410_UMSTAT_CTS)
926 return TIOCM_CAR | TIOCM_DSR | TIOCM_CTS;
927 else
928 return TIOCM_CAR | TIOCM_DSR;
929}
930
931static void s3c24xx_serial_set_mctrl(struct uart_port *port, unsigned int mctrl)
932{
José Miguel Gonçalves2d1e5a42013-09-18 16:52:49 +0100933 unsigned int umcon = rd_regl(port, S3C2410_UMCON);
934
935 if (mctrl & TIOCM_RTS)
936 umcon |= S3C2410_UMCOM_RTS_LOW;
937 else
938 umcon &= ~S3C2410_UMCOM_RTS_LOW;
939
940 wr_regl(port, S3C2410_UMCON, umcon);
Ben Dooksb4975492008-07-03 12:32:51 +0100941}
942
943static void s3c24xx_serial_break_ctl(struct uart_port *port, int break_state)
944{
945 unsigned long flags;
946 unsigned int ucon;
947
948 spin_lock_irqsave(&port->lock, flags);
949
950 ucon = rd_regl(port, S3C2410_UCON);
951
952 if (break_state)
953 ucon |= S3C2410_UCON_SBREAK;
954 else
955 ucon &= ~S3C2410_UCON_SBREAK;
956
957 wr_regl(port, S3C2410_UCON, ucon);
958
959 spin_unlock_irqrestore(&port->lock, flags);
960}
961
Robert Baldyga62c37ee2014-12-10 12:49:25 +0100962static int s3c24xx_serial_request_dma(struct s3c24xx_uart_port *p)
963{
964 struct s3c24xx_uart_dma *dma = p->dma;
Marek Szyprowskid8db8402018-05-17 13:37:14 +0200965 struct dma_slave_caps dma_caps;
966 const char *reason = NULL;
Marek Szyprowski500fcc02017-04-03 08:21:00 +0200967 int ret;
Robert Baldyga62c37ee2014-12-10 12:49:25 +0100968
969 /* Default slave configuration parameters */
970 dma->rx_conf.direction = DMA_DEV_TO_MEM;
971 dma->rx_conf.src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
972 dma->rx_conf.src_addr = p->port.mapbase + S3C2410_URXH;
Marek Szyprowskiaa2f80e2018-05-10 08:41:13 +0200973 dma->rx_conf.src_maxburst = 1;
Robert Baldyga62c37ee2014-12-10 12:49:25 +0100974
975 dma->tx_conf.direction = DMA_MEM_TO_DEV;
976 dma->tx_conf.dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
977 dma->tx_conf.dst_addr = p->port.mapbase + S3C2410_UTXH;
Marek Szyprowskiaa2f80e2018-05-10 08:41:13 +0200978 dma->tx_conf.dst_maxburst = 1;
Robert Baldyga62c37ee2014-12-10 12:49:25 +0100979
Marek Szyprowskiba3d6f82016-12-16 10:56:53 +0100980 dma->rx_chan = dma_request_chan(p->port.dev, "rx");
Robert Baldyga62c37ee2014-12-10 12:49:25 +0100981
Marek Szyprowskid8db8402018-05-17 13:37:14 +0200982 if (IS_ERR(dma->rx_chan)) {
983 reason = "DMA RX channel request failed";
984 ret = PTR_ERR(dma->rx_chan);
985 goto err_warn;
986 }
987
988 ret = dma_get_slave_caps(dma->rx_chan, &dma_caps);
989 if (ret < 0 ||
990 dma_caps.residue_granularity < DMA_RESIDUE_GRANULARITY_BURST) {
991 reason = "insufficient DMA RX engine capabilities";
992 ret = -EOPNOTSUPP;
993 goto err_release_rx;
994 }
Robert Baldyga62c37ee2014-12-10 12:49:25 +0100995
996 dmaengine_slave_config(dma->rx_chan, &dma->rx_conf);
997
Marek Szyprowskiba3d6f82016-12-16 10:56:53 +0100998 dma->tx_chan = dma_request_chan(p->port.dev, "tx");
999 if (IS_ERR(dma->tx_chan)) {
Marek Szyprowskid8db8402018-05-17 13:37:14 +02001000 reason = "DMA TX channel request failed";
Marek Szyprowski500fcc02017-04-03 08:21:00 +02001001 ret = PTR_ERR(dma->tx_chan);
1002 goto err_release_rx;
Robert Baldyga62c37ee2014-12-10 12:49:25 +01001003 }
1004
Marek Szyprowskid8db8402018-05-17 13:37:14 +02001005 ret = dma_get_slave_caps(dma->tx_chan, &dma_caps);
1006 if (ret < 0 ||
1007 dma_caps.residue_granularity < DMA_RESIDUE_GRANULARITY_BURST) {
1008 reason = "insufficient DMA TX engine capabilities";
1009 ret = -EOPNOTSUPP;
1010 goto err_release_tx;
1011 }
1012
Robert Baldyga62c37ee2014-12-10 12:49:25 +01001013 dmaengine_slave_config(dma->tx_chan, &dma->tx_conf);
1014
1015 /* RX buffer */
1016 dma->rx_size = PAGE_SIZE;
1017
1018 dma->rx_buf = kmalloc(dma->rx_size, GFP_KERNEL);
Robert Baldyga62c37ee2014-12-10 12:49:25 +01001019 if (!dma->rx_buf) {
Marek Szyprowski500fcc02017-04-03 08:21:00 +02001020 ret = -ENOMEM;
1021 goto err_release_tx;
Robert Baldyga62c37ee2014-12-10 12:49:25 +01001022 }
1023
Marek Szyprowski768d64f2017-04-03 08:20:59 +02001024 dma->rx_addr = dma_map_single(p->port.dev, dma->rx_buf,
Robert Baldyga62c37ee2014-12-10 12:49:25 +01001025 dma->rx_size, DMA_FROM_DEVICE);
Marek Szyprowski500fcc02017-04-03 08:21:00 +02001026 if (dma_mapping_error(p->port.dev, dma->rx_addr)) {
Marek Szyprowskid8db8402018-05-17 13:37:14 +02001027 reason = "DMA mapping error for RX buffer";
Marek Szyprowski500fcc02017-04-03 08:21:00 +02001028 ret = -EIO;
1029 goto err_free_rx;
1030 }
Robert Baldyga62c37ee2014-12-10 12:49:25 +01001031
Robert Baldyga62c37ee2014-12-10 12:49:25 +01001032 /* TX buffer */
Marek Szyprowski768d64f2017-04-03 08:20:59 +02001033 dma->tx_addr = dma_map_single(p->port.dev, p->port.state->xmit.buf,
Robert Baldyga62c37ee2014-12-10 12:49:25 +01001034 UART_XMIT_SIZE, DMA_TO_DEVICE);
Marek Szyprowski500fcc02017-04-03 08:21:00 +02001035 if (dma_mapping_error(p->port.dev, dma->tx_addr)) {
Marek Szyprowskid8db8402018-05-17 13:37:14 +02001036 reason = "DMA mapping error for TX buffer";
Marek Szyprowski500fcc02017-04-03 08:21:00 +02001037 ret = -EIO;
1038 goto err_unmap_rx;
1039 }
Robert Baldyga62c37ee2014-12-10 12:49:25 +01001040
1041 return 0;
Marek Szyprowski500fcc02017-04-03 08:21:00 +02001042
1043err_unmap_rx:
1044 dma_unmap_single(p->port.dev, dma->rx_addr, dma->rx_size,
1045 DMA_FROM_DEVICE);
1046err_free_rx:
1047 kfree(dma->rx_buf);
1048err_release_tx:
1049 dma_release_channel(dma->tx_chan);
1050err_release_rx:
1051 dma_release_channel(dma->rx_chan);
Marek Szyprowskid8db8402018-05-17 13:37:14 +02001052err_warn:
1053 if (reason)
1054 dev_warn(p->port.dev, "%s, DMA will not be used\n", reason);
Marek Szyprowski500fcc02017-04-03 08:21:00 +02001055 return ret;
Robert Baldyga62c37ee2014-12-10 12:49:25 +01001056}
1057
1058static void s3c24xx_serial_release_dma(struct s3c24xx_uart_port *p)
1059{
1060 struct s3c24xx_uart_dma *dma = p->dma;
1061
1062 if (dma->rx_chan) {
1063 dmaengine_terminate_all(dma->rx_chan);
Marek Szyprowski768d64f2017-04-03 08:20:59 +02001064 dma_unmap_single(p->port.dev, dma->rx_addr,
Robert Baldyga62c37ee2014-12-10 12:49:25 +01001065 dma->rx_size, DMA_FROM_DEVICE);
1066 kfree(dma->rx_buf);
1067 dma_release_channel(dma->rx_chan);
1068 dma->rx_chan = NULL;
1069 }
1070
1071 if (dma->tx_chan) {
1072 dmaengine_terminate_all(dma->tx_chan);
Marek Szyprowski768d64f2017-04-03 08:20:59 +02001073 dma_unmap_single(p->port.dev, dma->tx_addr,
Robert Baldyga62c37ee2014-12-10 12:49:25 +01001074 UART_XMIT_SIZE, DMA_TO_DEVICE);
1075 dma_release_channel(dma->tx_chan);
1076 dma->tx_chan = NULL;
1077 }
1078}
1079
Ben Dooksb4975492008-07-03 12:32:51 +01001080static void s3c24xx_serial_shutdown(struct uart_port *port)
1081{
1082 struct s3c24xx_uart_port *ourport = to_ourport(port);
1083
1084 if (ourport->tx_claimed) {
Thomas Abraham88bb4ea2011-08-10 15:51:19 +05301085 if (!s3c24xx_serial_has_interrupt_mask(port))
1086 free_irq(ourport->tx_irq, ourport);
Ben Dooksb4975492008-07-03 12:32:51 +01001087 tx_enabled(port) = 0;
1088 ourport->tx_claimed = 0;
Javier Martinez Canillase91d8632015-03-13 12:38:51 +01001089 ourport->tx_mode = 0;
Ben Dooksb4975492008-07-03 12:32:51 +01001090 }
1091
1092 if (ourport->rx_claimed) {
Thomas Abraham88bb4ea2011-08-10 15:51:19 +05301093 if (!s3c24xx_serial_has_interrupt_mask(port))
1094 free_irq(ourport->rx_irq, ourport);
Ben Dooksb4975492008-07-03 12:32:51 +01001095 ourport->rx_claimed = 0;
1096 rx_enabled(port) = 0;
1097 }
Ben Dooksb4975492008-07-03 12:32:51 +01001098
Thomas Abraham88bb4ea2011-08-10 15:51:19 +05301099 /* Clear pending interrupts and mask all interrupts */
1100 if (s3c24xx_serial_has_interrupt_mask(port)) {
Tomasz Figab6ad2932013-03-26 15:57:35 +01001101 free_irq(port->irq, ourport);
1102
Thomas Abraham88bb4ea2011-08-10 15:51:19 +05301103 wr_regl(port, S3C64XX_UINTP, 0xf);
1104 wr_regl(port, S3C64XX_UINTM, 0xf);
1105 }
Robert Baldyga62c37ee2014-12-10 12:49:25 +01001106
1107 if (ourport->dma)
1108 s3c24xx_serial_release_dma(ourport);
1109
Robert Baldyga29bef792014-12-10 12:49:26 +01001110 ourport->tx_in_progress = 0;
Thomas Abraham88bb4ea2011-08-10 15:51:19 +05301111}
Ben Dooksb4975492008-07-03 12:32:51 +01001112
1113static int s3c24xx_serial_startup(struct uart_port *port)
1114{
1115 struct s3c24xx_uart_port *ourport = to_ourport(port);
1116 int ret;
1117
Ben Dooksb4975492008-07-03 12:32:51 +01001118 rx_enabled(port) = 1;
1119
Ben Dooksb73c289c2008-10-21 14:07:04 +01001120 ret = request_irq(ourport->rx_irq, s3c24xx_serial_rx_chars, 0,
Ben Dooksb4975492008-07-03 12:32:51 +01001121 s3c24xx_serial_portname(port), ourport);
1122
1123 if (ret != 0) {
Sachin Kamatd20925e2012-09-05 10:30:10 +05301124 dev_err(port->dev, "cannot get irq %d\n", ourport->rx_irq);
Ben Dooksb4975492008-07-03 12:32:51 +01001125 return ret;
1126 }
1127
1128 ourport->rx_claimed = 1;
1129
Greg Kroah-Hartmana05025d2019-12-10 15:37:03 +01001130 dev_dbg(port->dev, "requesting tx irq...\n");
Ben Dooksb4975492008-07-03 12:32:51 +01001131
1132 tx_enabled(port) = 1;
1133
Ben Dooksb73c289c2008-10-21 14:07:04 +01001134 ret = request_irq(ourport->tx_irq, s3c24xx_serial_tx_chars, 0,
Ben Dooksb4975492008-07-03 12:32:51 +01001135 s3c24xx_serial_portname(port), ourport);
1136
1137 if (ret) {
Sachin Kamatd20925e2012-09-05 10:30:10 +05301138 dev_err(port->dev, "cannot get irq %d\n", ourport->tx_irq);
Ben Dooksb4975492008-07-03 12:32:51 +01001139 goto err;
1140 }
1141
1142 ourport->tx_claimed = 1;
1143
Ben Dooksb4975492008-07-03 12:32:51 +01001144 /* the port reset code should have done the correct
1145 * register setup for the port controls */
1146
1147 return ret;
1148
Robert Baldygaef4aca72014-11-24 07:56:22 +01001149err:
Ben Dooksb4975492008-07-03 12:32:51 +01001150 s3c24xx_serial_shutdown(port);
1151 return ret;
1152}
1153
Thomas Abraham88bb4ea2011-08-10 15:51:19 +05301154static int s3c64xx_serial_startup(struct uart_port *port)
1155{
1156 struct s3c24xx_uart_port *ourport = to_ourport(port);
Robert Baldygab543c302014-12-10 12:49:27 +01001157 unsigned long flags;
1158 unsigned int ufcon;
Thomas Abraham88bb4ea2011-08-10 15:51:19 +05301159 int ret;
1160
Tomasz Figab6ad2932013-03-26 15:57:35 +01001161 wr_regl(port, S3C64XX_UINTM, 0xf);
Robert Baldyga62c37ee2014-12-10 12:49:25 +01001162 if (ourport->dma) {
1163 ret = s3c24xx_serial_request_dma(ourport);
1164 if (ret < 0) {
Krzysztof Kozlowskif98c7bc2017-02-25 18:36:44 +02001165 devm_kfree(port->dev, ourport->dma);
1166 ourport->dma = NULL;
Robert Baldyga62c37ee2014-12-10 12:49:25 +01001167 }
1168 }
Tomasz Figab6ad2932013-03-26 15:57:35 +01001169
Thomas Abraham88bb4ea2011-08-10 15:51:19 +05301170 ret = request_irq(port->irq, s3c64xx_serial_handle_irq, IRQF_SHARED,
1171 s3c24xx_serial_portname(port), ourport);
1172 if (ret) {
Sachin Kamatd20925e2012-09-05 10:30:10 +05301173 dev_err(port->dev, "cannot get irq %d\n", port->irq);
Thomas Abraham88bb4ea2011-08-10 15:51:19 +05301174 return ret;
1175 }
1176
1177 /* For compatibility with s3c24xx Soc's */
1178 rx_enabled(port) = 1;
1179 ourport->rx_claimed = 1;
1180 tx_enabled(port) = 0;
1181 ourport->tx_claimed = 1;
1182
Robert Baldyga29bef792014-12-10 12:49:26 +01001183 spin_lock_irqsave(&port->lock, flags);
1184
1185 ufcon = rd_regl(port, S3C2410_UFCON);
Robert Baldyga31c6ba92015-04-17 08:43:09 +02001186 ufcon |= S3C2410_UFCON_RESETRX | S5PV210_UFCON_RXTRIG8;
1187 if (!uart_console(port))
1188 ufcon |= S3C2410_UFCON_RESETTX;
Robert Baldyga29bef792014-12-10 12:49:26 +01001189 wr_regl(port, S3C2410_UFCON, ufcon);
1190
1191 enable_rx_pio(ourport);
1192
1193 spin_unlock_irqrestore(&port->lock, flags);
1194
Thomas Abraham88bb4ea2011-08-10 15:51:19 +05301195 /* Enable Rx Interrupt */
Matthew Leachbbb5ff92016-06-22 17:57:03 +01001196 s3c24xx_clear_bit(port, S3C64XX_UINTM_RXD, S3C64XX_UINTM);
Robert Baldyga29bef792014-12-10 12:49:26 +01001197
Thomas Abraham88bb4ea2011-08-10 15:51:19 +05301198 return ret;
1199}
1200
Ben Dooksb4975492008-07-03 12:32:51 +01001201/* power power management control */
1202
1203static void s3c24xx_serial_pm(struct uart_port *port, unsigned int level,
1204 unsigned int old)
1205{
1206 struct s3c24xx_uart_port *ourport = to_ourport(port);
Robert Baldyga1ff383a2014-11-24 07:56:21 +01001207 int timeout = 10000;
Ben Dooksb4975492008-07-03 12:32:51 +01001208
Ben Dooks30555472008-10-21 14:06:36 +01001209 ourport->pm_level = level;
1210
Ben Dooksb4975492008-07-03 12:32:51 +01001211 switch (level) {
1212 case 3:
Robert Baldyga1ff383a2014-11-24 07:56:21 +01001213 while (--timeout && !s3c24xx_serial_txempty_nofifo(port))
1214 udelay(100);
1215
Kyoungil Kim7cd88832012-05-20 17:45:54 +09001216 if (!IS_ERR(ourport->baudclk))
Thomas Abraham9484b002012-10-03 07:40:04 +09001217 clk_disable_unprepare(ourport->baudclk);
Ben Dooksb4975492008-07-03 12:32:51 +01001218
Thomas Abraham9484b002012-10-03 07:40:04 +09001219 clk_disable_unprepare(ourport->clk);
Ben Dooksb4975492008-07-03 12:32:51 +01001220 break;
1221
1222 case 0:
Thomas Abraham9484b002012-10-03 07:40:04 +09001223 clk_prepare_enable(ourport->clk);
Ben Dooksb4975492008-07-03 12:32:51 +01001224
Kyoungil Kim7cd88832012-05-20 17:45:54 +09001225 if (!IS_ERR(ourport->baudclk))
Thomas Abraham9484b002012-10-03 07:40:04 +09001226 clk_prepare_enable(ourport->baudclk);
Ben Dooksb4975492008-07-03 12:32:51 +01001227
1228 break;
1229 default:
Sachin Kamatd20925e2012-09-05 10:30:10 +05301230 dev_err(port->dev, "s3c24xx_serial: unknown pm %d\n", level);
Ben Dooksb4975492008-07-03 12:32:51 +01001231 }
1232}
1233
1234/* baud rate calculation
1235 *
1236 * The UARTs on the S3C2410/S3C2440 can take their clocks from a number
1237 * of different sources, including the peripheral clock ("pclk") and an
1238 * external clock ("uclk"). The S3C2440 also adds the core clock ("fclk")
1239 * with a programmable extra divisor.
1240 *
1241 * The following code goes through the clock sources, and calculates the
1242 * baud clocks (and the resultant actual baud rates) and then tries to
1243 * pick the closest one and select that.
1244 *
1245*/
1246
Thomas Abraham5f5a7a52011-10-24 11:47:46 +02001247#define MAX_CLK_NAME_LENGTH 15
Ben Dooksb4975492008-07-03 12:32:51 +01001248
Thomas Abraham5f5a7a52011-10-24 11:47:46 +02001249static inline int s3c24xx_serial_getsource(struct uart_port *port)
Ben Dooksb4975492008-07-03 12:32:51 +01001250{
1251 struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
Thomas Abraham5f5a7a52011-10-24 11:47:46 +02001252 unsigned int ucon;
Ben Dooksb4975492008-07-03 12:32:51 +01001253
Thomas Abraham5f5a7a52011-10-24 11:47:46 +02001254 if (info->num_clks == 1)
Ben Dooksb4975492008-07-03 12:32:51 +01001255 return 0;
1256
Thomas Abraham5f5a7a52011-10-24 11:47:46 +02001257 ucon = rd_regl(port, S3C2410_UCON);
1258 ucon &= info->clksel_mask;
1259 return ucon >> info->clksel_shift;
Ben Dooksb4975492008-07-03 12:32:51 +01001260}
1261
Thomas Abraham5f5a7a52011-10-24 11:47:46 +02001262static void s3c24xx_serial_setsource(struct uart_port *port,
1263 unsigned int clk_sel)
Ben Dooksb4975492008-07-03 12:32:51 +01001264{
Thomas Abraham5f5a7a52011-10-24 11:47:46 +02001265 struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
1266 unsigned int ucon;
Ben Dooksb4975492008-07-03 12:32:51 +01001267
Thomas Abraham5f5a7a52011-10-24 11:47:46 +02001268 if (info->num_clks == 1)
1269 return;
Ben Dooksb4975492008-07-03 12:32:51 +01001270
Thomas Abraham5f5a7a52011-10-24 11:47:46 +02001271 ucon = rd_regl(port, S3C2410_UCON);
1272 if ((ucon & info->clksel_mask) >> info->clksel_shift == clk_sel)
1273 return;
Ben Dooksb4975492008-07-03 12:32:51 +01001274
Thomas Abraham5f5a7a52011-10-24 11:47:46 +02001275 ucon &= ~info->clksel_mask;
1276 ucon |= clk_sel << info->clksel_shift;
1277 wr_regl(port, S3C2410_UCON, ucon);
1278}
Ben Dooksb4975492008-07-03 12:32:51 +01001279
Thomas Abraham5f5a7a52011-10-24 11:47:46 +02001280static unsigned int s3c24xx_serial_getclk(struct s3c24xx_uart_port *ourport,
1281 unsigned int req_baud, struct clk **best_clk,
1282 unsigned int *clk_num)
1283{
1284 struct s3c24xx_uart_info *info = ourport->info;
1285 struct clk *clk;
1286 unsigned long rate;
1287 unsigned int cnt, baud, quot, clk_sel, best_quot = 0;
1288 char clkname[MAX_CLK_NAME_LENGTH];
1289 int calc_deviation, deviation = (1 << 30) - 1;
1290
Thomas Abraham5f5a7a52011-10-24 11:47:46 +02001291 clk_sel = (ourport->cfg->clk_sel) ? ourport->cfg->clk_sel :
1292 ourport->info->def_clk_sel;
1293 for (cnt = 0; cnt < info->num_clks; cnt++) {
1294 if (!(clk_sel & (1 << cnt)))
1295 continue;
1296
1297 sprintf(clkname, "clk_uart_baud%d", cnt);
1298 clk = clk_get(ourport->port.dev, clkname);
Kyoungil Kim7cd88832012-05-20 17:45:54 +09001299 if (IS_ERR(clk))
Thomas Abraham5f5a7a52011-10-24 11:47:46 +02001300 continue;
1301
1302 rate = clk_get_rate(clk);
1303 if (!rate)
1304 continue;
1305
1306 if (ourport->info->has_divslot) {
1307 unsigned long div = rate / req_baud;
1308
1309 /* The UDIVSLOT register on the newer UARTs allows us to
1310 * get a divisor adjustment of 1/16th on the baud clock.
1311 *
1312 * We don't keep the UDIVSLOT value (the 16ths we
1313 * calculated by not multiplying the baud by 16) as it
1314 * is easy enough to recalculate.
1315 */
1316
1317 quot = div / 16;
1318 baud = rate / div;
1319 } else {
1320 quot = (rate + (8 * req_baud)) / (16 * req_baud);
1321 baud = rate / (quot * 16);
1322 }
1323 quot--;
1324
1325 calc_deviation = req_baud - baud;
1326 if (calc_deviation < 0)
1327 calc_deviation = -calc_deviation;
1328
1329 if (calc_deviation < deviation) {
1330 *best_clk = clk;
1331 best_quot = quot;
1332 *clk_num = cnt;
1333 deviation = calc_deviation;
Ben Dooksb4975492008-07-03 12:32:51 +01001334 }
1335 }
1336
Thomas Abraham5f5a7a52011-10-24 11:47:46 +02001337 return best_quot;
Ben Dooksb4975492008-07-03 12:32:51 +01001338}
1339
Ben Dooks090f848d2008-12-12 00:24:21 +00001340/* udivslot_table[]
1341 *
1342 * This table takes the fractional value of the baud divisor and gives
1343 * the recommended setting for the UDIVSLOT register.
1344 */
1345static u16 udivslot_table[16] = {
1346 [0] = 0x0000,
1347 [1] = 0x0080,
1348 [2] = 0x0808,
1349 [3] = 0x0888,
1350 [4] = 0x2222,
1351 [5] = 0x4924,
1352 [6] = 0x4A52,
1353 [7] = 0x54AA,
1354 [8] = 0x5555,
1355 [9] = 0xD555,
1356 [10] = 0xD5D5,
1357 [11] = 0xDDD5,
1358 [12] = 0xDDDD,
1359 [13] = 0xDFDD,
1360 [14] = 0xDFDF,
1361 [15] = 0xFFDF,
1362};
1363
Ben Dooksb4975492008-07-03 12:32:51 +01001364static void s3c24xx_serial_set_termios(struct uart_port *port,
1365 struct ktermios *termios,
1366 struct ktermios *old)
1367{
1368 struct s3c2410_uartcfg *cfg = s3c24xx_port_to_cfg(port);
1369 struct s3c24xx_uart_port *ourport = to_ourport(port);
Kyoungil Kim7cd88832012-05-20 17:45:54 +09001370 struct clk *clk = ERR_PTR(-EINVAL);
Ben Dooksb4975492008-07-03 12:32:51 +01001371 unsigned long flags;
Thomas Abraham5f5a7a52011-10-24 11:47:46 +02001372 unsigned int baud, quot, clk_sel = 0;
Ben Dooksb4975492008-07-03 12:32:51 +01001373 unsigned int ulcon;
1374 unsigned int umcon;
Ben Dooks090f848d2008-12-12 00:24:21 +00001375 unsigned int udivslot = 0;
Ben Dooksb4975492008-07-03 12:32:51 +01001376
1377 /*
1378 * We don't support modem control lines.
1379 */
1380 termios->c_cflag &= ~(HUPCL | CMSPAR);
1381 termios->c_cflag |= CLOCAL;
1382
1383 /*
1384 * Ask the core to calculate the divisor for us.
1385 */
1386
Seung-Woo Kimec18f482018-12-14 12:34:09 +01001387 baud = uart_get_baud_rate(port, termios, old, 0, 3000000);
Thomas Abraham5f5a7a52011-10-24 11:47:46 +02001388 quot = s3c24xx_serial_getclk(ourport, baud, &clk, &clk_sel);
Ben Dooksb4975492008-07-03 12:32:51 +01001389 if (baud == 38400 && (port->flags & UPF_SPD_MASK) == UPF_SPD_CUST)
1390 quot = port->custom_divisor;
Kyoungil Kim7cd88832012-05-20 17:45:54 +09001391 if (IS_ERR(clk))
Thomas Abraham5f5a7a52011-10-24 11:47:46 +02001392 return;
Ben Dooksb4975492008-07-03 12:32:51 +01001393
1394 /* check to see if we need to change clock source */
1395
Thomas Abraham5f5a7a52011-10-24 11:47:46 +02001396 if (ourport->baudclk != clk) {
Chanwoo Choib8995f52016-04-21 18:58:31 +09001397 clk_prepare_enable(clk);
1398
Thomas Abraham5f5a7a52011-10-24 11:47:46 +02001399 s3c24xx_serial_setsource(port, clk_sel);
Ben Dooksb4975492008-07-03 12:32:51 +01001400
Kyoungil Kim7cd88832012-05-20 17:45:54 +09001401 if (!IS_ERR(ourport->baudclk)) {
Thomas Abraham9484b002012-10-03 07:40:04 +09001402 clk_disable_unprepare(ourport->baudclk);
Kyoungil Kim7cd88832012-05-20 17:45:54 +09001403 ourport->baudclk = ERR_PTR(-EINVAL);
Ben Dooksb4975492008-07-03 12:32:51 +01001404 }
1405
Ben Dooksb4975492008-07-03 12:32:51 +01001406 ourport->baudclk = clk;
Ben Dooks30555472008-10-21 14:06:36 +01001407 ourport->baudclk_rate = clk ? clk_get_rate(clk) : 0;
Ben Dooksb4975492008-07-03 12:32:51 +01001408 }
1409
Ben Dooks090f848d2008-12-12 00:24:21 +00001410 if (ourport->info->has_divslot) {
1411 unsigned int div = ourport->baudclk_rate / baud;
1412
Jongpill Lee8b526ae2010-07-16 10:19:41 +09001413 if (cfg->has_fracval) {
1414 udivslot = (div & 15);
Greg Kroah-Hartmana05025d2019-12-10 15:37:03 +01001415 dev_dbg(port->dev, "fracval = %04x\n", udivslot);
Jongpill Lee8b526ae2010-07-16 10:19:41 +09001416 } else {
1417 udivslot = udivslot_table[div & 15];
Greg Kroah-Hartmana05025d2019-12-10 15:37:03 +01001418 dev_dbg(port->dev, "udivslot = %04x (div %d)\n",
1419 udivslot, div & 15);
Jongpill Lee8b526ae2010-07-16 10:19:41 +09001420 }
Ben Dooks090f848d2008-12-12 00:24:21 +00001421 }
1422
Ben Dooksb4975492008-07-03 12:32:51 +01001423 switch (termios->c_cflag & CSIZE) {
1424 case CS5:
Greg Kroah-Hartmana05025d2019-12-10 15:37:03 +01001425 dev_dbg(port->dev, "config: 5bits/char\n");
Ben Dooksb4975492008-07-03 12:32:51 +01001426 ulcon = S3C2410_LCON_CS5;
1427 break;
1428 case CS6:
Greg Kroah-Hartmana05025d2019-12-10 15:37:03 +01001429 dev_dbg(port->dev, "config: 6bits/char\n");
Ben Dooksb4975492008-07-03 12:32:51 +01001430 ulcon = S3C2410_LCON_CS6;
1431 break;
1432 case CS7:
Greg Kroah-Hartmana05025d2019-12-10 15:37:03 +01001433 dev_dbg(port->dev, "config: 7bits/char\n");
Ben Dooksb4975492008-07-03 12:32:51 +01001434 ulcon = S3C2410_LCON_CS7;
1435 break;
1436 case CS8:
1437 default:
Greg Kroah-Hartmana05025d2019-12-10 15:37:03 +01001438 dev_dbg(port->dev, "config: 8bits/char\n");
Ben Dooksb4975492008-07-03 12:32:51 +01001439 ulcon = S3C2410_LCON_CS8;
1440 break;
1441 }
1442
1443 /* preserve original lcon IR settings */
1444 ulcon |= (cfg->ulcon & S3C2410_LCON_IRM);
1445
1446 if (termios->c_cflag & CSTOPB)
1447 ulcon |= S3C2410_LCON_STOPB;
1448
Ben Dooksb4975492008-07-03 12:32:51 +01001449 if (termios->c_cflag & PARENB) {
1450 if (termios->c_cflag & PARODD)
1451 ulcon |= S3C2410_LCON_PODD;
1452 else
1453 ulcon |= S3C2410_LCON_PEVEN;
1454 } else {
1455 ulcon |= S3C2410_LCON_PNONE;
1456 }
1457
1458 spin_lock_irqsave(&port->lock, flags);
1459
Greg Kroah-Hartmana05025d2019-12-10 15:37:03 +01001460 dev_dbg(port->dev,
1461 "setting ulcon to %08x, brddiv to %d, udivslot %08x\n",
1462 ulcon, quot, udivslot);
Ben Dooksb4975492008-07-03 12:32:51 +01001463
1464 wr_regl(port, S3C2410_ULCON, ulcon);
1465 wr_regl(port, S3C2410_UBRDIV, quot);
José Miguel Gonçalves2d1e5a42013-09-18 16:52:49 +01001466
Beomho Seo31e93362018-12-14 12:34:08 +01001467 port->status &= ~UPSTAT_AUTOCTS;
1468
José Miguel Gonçalves2d1e5a42013-09-18 16:52:49 +01001469 umcon = rd_regl(port, S3C2410_UMCON);
1470 if (termios->c_cflag & CRTSCTS) {
1471 umcon |= S3C2410_UMCOM_AFC;
1472 /* Disable RTS when RX FIFO contains 63 bytes */
1473 umcon &= ~S3C2412_UMCON_AFC_8;
Beomho Seo31e93362018-12-14 12:34:08 +01001474 port->status = UPSTAT_AUTOCTS;
José Miguel Gonçalves2d1e5a42013-09-18 16:52:49 +01001475 } else {
1476 umcon &= ~S3C2410_UMCOM_AFC;
1477 }
Ben Dooksb4975492008-07-03 12:32:51 +01001478 wr_regl(port, S3C2410_UMCON, umcon);
1479
Ben Dooks090f848d2008-12-12 00:24:21 +00001480 if (ourport->info->has_divslot)
1481 wr_regl(port, S3C2443_DIVSLOT, udivslot);
1482
Greg Kroah-Hartmana05025d2019-12-10 15:37:03 +01001483 dev_dbg(port->dev,
1484 "uart: ulcon = 0x%08x, ucon = 0x%08x, ufcon = 0x%08x\n",
1485 rd_regl(port, S3C2410_ULCON),
1486 rd_regl(port, S3C2410_UCON),
1487 rd_regl(port, S3C2410_UFCON));
Ben Dooksb4975492008-07-03 12:32:51 +01001488
1489 /*
1490 * Update the per-port timeout.
1491 */
1492 uart_update_timeout(port, termios->c_cflag, baud);
1493
1494 /*
1495 * Which character status flags are we interested in?
1496 */
1497 port->read_status_mask = S3C2410_UERSTAT_OVERRUN;
1498 if (termios->c_iflag & INPCK)
Robert Baldygaef4aca72014-11-24 07:56:22 +01001499 port->read_status_mask |= S3C2410_UERSTAT_FRAME |
1500 S3C2410_UERSTAT_PARITY;
Ben Dooksb4975492008-07-03 12:32:51 +01001501 /*
1502 * Which character status flags should we ignore?
1503 */
1504 port->ignore_status_mask = 0;
1505 if (termios->c_iflag & IGNPAR)
1506 port->ignore_status_mask |= S3C2410_UERSTAT_OVERRUN;
1507 if (termios->c_iflag & IGNBRK && termios->c_iflag & IGNPAR)
1508 port->ignore_status_mask |= S3C2410_UERSTAT_FRAME;
1509
1510 /*
1511 * Ignore all characters if CREAD is not set.
1512 */
1513 if ((termios->c_cflag & CREAD) == 0)
1514 port->ignore_status_mask |= RXSTAT_DUMMY_READ;
1515
1516 spin_unlock_irqrestore(&port->lock, flags);
1517}
1518
1519static const char *s3c24xx_serial_type(struct uart_port *port)
1520{
1521 switch (port->type) {
1522 case PORT_S3C2410:
1523 return "S3C2410";
1524 case PORT_S3C2440:
1525 return "S3C2440";
1526 case PORT_S3C2412:
1527 return "S3C2412";
Ben Dooksb690ace2008-10-21 14:07:03 +01001528 case PORT_S3C6400:
1529 return "S3C6400/10";
Ben Dooksb4975492008-07-03 12:32:51 +01001530 default:
1531 return NULL;
1532 }
1533}
1534
1535#define MAP_SIZE (0x100)
1536
1537static void s3c24xx_serial_release_port(struct uart_port *port)
1538{
1539 release_mem_region(port->mapbase, MAP_SIZE);
1540}
1541
1542static int s3c24xx_serial_request_port(struct uart_port *port)
1543{
1544 const char *name = s3c24xx_serial_portname(port);
1545 return request_mem_region(port->mapbase, MAP_SIZE, name) ? 0 : -EBUSY;
1546}
1547
1548static void s3c24xx_serial_config_port(struct uart_port *port, int flags)
1549{
1550 struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
1551
1552 if (flags & UART_CONFIG_TYPE &&
1553 s3c24xx_serial_request_port(port) == 0)
1554 port->type = info->type;
1555}
1556
1557/*
1558 * verify the new serial_struct (for TIOCSSERIAL).
1559 */
1560static int
1561s3c24xx_serial_verify_port(struct uart_port *port, struct serial_struct *ser)
1562{
1563 struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
1564
1565 if (ser->type != PORT_UNKNOWN && ser->type != info->type)
1566 return -EINVAL;
1567
1568 return 0;
1569}
1570
1571
1572#ifdef CONFIG_SERIAL_SAMSUNG_CONSOLE
1573
1574static struct console s3c24xx_serial_console;
1575
Julien Pichon93b5c032012-09-21 23:22:31 -07001576static int __init s3c24xx_serial_console_init(void)
1577{
1578 register_console(&s3c24xx_serial_console);
1579 return 0;
1580}
1581console_initcall(s3c24xx_serial_console_init);
1582
Ben Dooksb4975492008-07-03 12:32:51 +01001583#define S3C24XX_SERIAL_CONSOLE &s3c24xx_serial_console
1584#else
1585#define S3C24XX_SERIAL_CONSOLE NULL
1586#endif
1587
Arnd Bergmann84f57d92013-04-11 02:04:49 +02001588#if defined(CONFIG_SERIAL_SAMSUNG_CONSOLE) && defined(CONFIG_CONSOLE_POLL)
Julien Pichon93b5c032012-09-21 23:22:31 -07001589static int s3c24xx_serial_get_poll_char(struct uart_port *port);
1590static void s3c24xx_serial_put_poll_char(struct uart_port *port,
1591 unsigned char c);
1592#endif
1593
Ben Dooksb4975492008-07-03 12:32:51 +01001594static struct uart_ops s3c24xx_serial_ops = {
1595 .pm = s3c24xx_serial_pm,
1596 .tx_empty = s3c24xx_serial_tx_empty,
1597 .get_mctrl = s3c24xx_serial_get_mctrl,
1598 .set_mctrl = s3c24xx_serial_set_mctrl,
1599 .stop_tx = s3c24xx_serial_stop_tx,
1600 .start_tx = s3c24xx_serial_start_tx,
1601 .stop_rx = s3c24xx_serial_stop_rx,
Ben Dooksb4975492008-07-03 12:32:51 +01001602 .break_ctl = s3c24xx_serial_break_ctl,
1603 .startup = s3c24xx_serial_startup,
1604 .shutdown = s3c24xx_serial_shutdown,
1605 .set_termios = s3c24xx_serial_set_termios,
1606 .type = s3c24xx_serial_type,
1607 .release_port = s3c24xx_serial_release_port,
1608 .request_port = s3c24xx_serial_request_port,
1609 .config_port = s3c24xx_serial_config_port,
1610 .verify_port = s3c24xx_serial_verify_port,
Arnd Bergmann84f57d92013-04-11 02:04:49 +02001611#if defined(CONFIG_SERIAL_SAMSUNG_CONSOLE) && defined(CONFIG_CONSOLE_POLL)
Julien Pichon93b5c032012-09-21 23:22:31 -07001612 .poll_get_char = s3c24xx_serial_get_poll_char,
1613 .poll_put_char = s3c24xx_serial_put_poll_char,
1614#endif
Ben Dooksb4975492008-07-03 12:32:51 +01001615};
1616
Ben Dooksb4975492008-07-03 12:32:51 +01001617static struct uart_driver s3c24xx_uart_drv = {
1618 .owner = THIS_MODULE,
Darius Augulis2cf0c582011-01-12 14:50:51 +09001619 .driver_name = "s3c2410_serial",
Ben Dooksbdd49152008-11-03 19:51:42 +00001620 .nr = CONFIG_SERIAL_SAMSUNG_UARTS,
Ben Dooksb4975492008-07-03 12:32:51 +01001621 .cons = S3C24XX_SERIAL_CONSOLE,
Darius Augulis2cf0c582011-01-12 14:50:51 +09001622 .dev_name = S3C24XX_SERIAL_NAME,
Ben Dooksb4975492008-07-03 12:32:51 +01001623 .major = S3C24XX_SERIAL_MAJOR,
1624 .minor = S3C24XX_SERIAL_MINOR,
1625};
1626
Robert Baldygaef4aca72014-11-24 07:56:22 +01001627#define __PORT_LOCK_UNLOCKED(i) \
1628 __SPIN_LOCK_UNLOCKED(s3c24xx_serial_ports[i].port.lock)
1629static struct s3c24xx_uart_port
1630s3c24xx_serial_ports[CONFIG_SERIAL_SAMSUNG_UARTS] = {
Ben Dooksb4975492008-07-03 12:32:51 +01001631 [0] = {
1632 .port = {
Robert Baldygaef4aca72014-11-24 07:56:22 +01001633 .lock = __PORT_LOCK_UNLOCKED(0),
Ben Dooksb4975492008-07-03 12:32:51 +01001634 .iotype = UPIO_MEM,
Ben Dooksb4975492008-07-03 12:32:51 +01001635 .uartclk = 0,
1636 .fifosize = 16,
1637 .ops = &s3c24xx_serial_ops,
1638 .flags = UPF_BOOT_AUTOCONF,
1639 .line = 0,
1640 }
1641 },
1642 [1] = {
1643 .port = {
Robert Baldygaef4aca72014-11-24 07:56:22 +01001644 .lock = __PORT_LOCK_UNLOCKED(1),
Ben Dooksb4975492008-07-03 12:32:51 +01001645 .iotype = UPIO_MEM,
Ben Dooksb4975492008-07-03 12:32:51 +01001646 .uartclk = 0,
1647 .fifosize = 16,
1648 .ops = &s3c24xx_serial_ops,
1649 .flags = UPF_BOOT_AUTOCONF,
1650 .line = 1,
1651 }
1652 },
Ben Dooks03d5e772008-11-03 09:21:23 +00001653#if CONFIG_SERIAL_SAMSUNG_UARTS > 2
Ben Dooksb4975492008-07-03 12:32:51 +01001654
1655 [2] = {
1656 .port = {
Robert Baldygaef4aca72014-11-24 07:56:22 +01001657 .lock = __PORT_LOCK_UNLOCKED(2),
Ben Dooksb4975492008-07-03 12:32:51 +01001658 .iotype = UPIO_MEM,
Ben Dooksb4975492008-07-03 12:32:51 +01001659 .uartclk = 0,
1660 .fifosize = 16,
1661 .ops = &s3c24xx_serial_ops,
1662 .flags = UPF_BOOT_AUTOCONF,
1663 .line = 2,
1664 }
Ben Dooks03d5e772008-11-03 09:21:23 +00001665 },
1666#endif
1667#if CONFIG_SERIAL_SAMSUNG_UARTS > 3
1668 [3] = {
1669 .port = {
Robert Baldygaef4aca72014-11-24 07:56:22 +01001670 .lock = __PORT_LOCK_UNLOCKED(3),
Ben Dooks03d5e772008-11-03 09:21:23 +00001671 .iotype = UPIO_MEM,
Ben Dooks03d5e772008-11-03 09:21:23 +00001672 .uartclk = 0,
1673 .fifosize = 16,
1674 .ops = &s3c24xx_serial_ops,
1675 .flags = UPF_BOOT_AUTOCONF,
1676 .line = 3,
1677 }
Ben Dooksb4975492008-07-03 12:32:51 +01001678 }
1679#endif
1680};
Robert Baldygaef4aca72014-11-24 07:56:22 +01001681#undef __PORT_LOCK_UNLOCKED
Ben Dooksb4975492008-07-03 12:32:51 +01001682
1683/* s3c24xx_serial_resetport
1684 *
Thomas Abraham0dfb3b42011-10-24 11:48:21 +02001685 * reset the fifos and other the settings.
Ben Dooksb4975492008-07-03 12:32:51 +01001686*/
1687
Thomas Abraham0dfb3b42011-10-24 11:48:21 +02001688static void s3c24xx_serial_resetport(struct uart_port *port,
1689 struct s3c2410_uartcfg *cfg)
Ben Dooksb4975492008-07-03 12:32:51 +01001690{
1691 struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
Thomas Abraham0dfb3b42011-10-24 11:48:21 +02001692 unsigned long ucon = rd_regl(port, S3C2410_UCON);
1693 unsigned int ucon_mask;
Ben Dooksb4975492008-07-03 12:32:51 +01001694
Thomas Abraham0dfb3b42011-10-24 11:48:21 +02001695 ucon_mask = info->clksel_mask;
1696 if (info->type == PORT_S3C2440)
1697 ucon_mask |= S3C2440_UCON0_DIVMASK;
1698
1699 ucon &= ucon_mask;
1700 wr_regl(port, S3C2410_UCON, ucon | cfg->ucon);
1701
1702 /* reset both fifos */
1703 wr_regl(port, S3C2410_UFCON, cfg->ufcon | S3C2410_UFCON_RESETBOTH);
1704 wr_regl(port, S3C2410_UFCON, cfg->ufcon);
1705
1706 /* some delay is required after fifo reset */
1707 udelay(1);
Ben Dooksb4975492008-07-03 12:32:51 +01001708}
1709
Ben Dooks30555472008-10-21 14:06:36 +01001710
Krzysztof Kozlowskiebaa81c2016-06-27 13:59:08 +02001711#ifdef CONFIG_ARM_S3C24XX_CPUFREQ
Ben Dooks30555472008-10-21 14:06:36 +01001712
1713static int s3c24xx_serial_cpufreq_transition(struct notifier_block *nb,
1714 unsigned long val, void *data)
1715{
1716 struct s3c24xx_uart_port *port;
1717 struct uart_port *uport;
1718
1719 port = container_of(nb, struct s3c24xx_uart_port, freq_transition);
1720 uport = &port->port;
1721
1722 /* check to see if port is enabled */
1723
1724 if (port->pm_level != 0)
1725 return 0;
1726
1727 /* try and work out if the baudrate is changing, we can detect
1728 * a change in rate, but we do not have support for detecting
1729 * a disturbance in the clock-rate over the change.
1730 */
1731
Kyoungil Kim25f04ad2012-05-20 17:49:31 +09001732 if (IS_ERR(port->baudclk))
Ben Dooks30555472008-10-21 14:06:36 +01001733 goto exit;
1734
Kyoungil Kim25f04ad2012-05-20 17:49:31 +09001735 if (port->baudclk_rate == clk_get_rate(port->baudclk))
Ben Dooks30555472008-10-21 14:06:36 +01001736 goto exit;
1737
1738 if (val == CPUFREQ_PRECHANGE) {
1739 /* we should really shut the port down whilst the
1740 * frequency change is in progress. */
1741
1742 } else if (val == CPUFREQ_POSTCHANGE) {
1743 struct ktermios *termios;
1744 struct tty_struct *tty;
1745
Alan Coxebd2c8f2009-09-19 13:13:28 -07001746 if (uport->state == NULL)
Ben Dooks30555472008-10-21 14:06:36 +01001747 goto exit;
Ben Dooks30555472008-10-21 14:06:36 +01001748
Alan Coxebd2c8f2009-09-19 13:13:28 -07001749 tty = uport->state->port.tty;
Ben Dooks30555472008-10-21 14:06:36 +01001750
Ben Dooks7de40c22008-12-14 23:11:02 +00001751 if (tty == NULL)
Ben Dooks30555472008-10-21 14:06:36 +01001752 goto exit;
Ben Dooks30555472008-10-21 14:06:36 +01001753
Alan Coxadc8d742012-07-14 15:31:47 +01001754 termios = &tty->termios;
Ben Dooks30555472008-10-21 14:06:36 +01001755
1756 if (termios == NULL) {
Sachin Kamatd20925e2012-09-05 10:30:10 +05301757 dev_warn(uport->dev, "%s: no termios?\n", __func__);
Ben Dooks30555472008-10-21 14:06:36 +01001758 goto exit;
1759 }
1760
1761 s3c24xx_serial_set_termios(uport, termios, NULL);
1762 }
1763
Robert Baldygaef4aca72014-11-24 07:56:22 +01001764exit:
Ben Dooks30555472008-10-21 14:06:36 +01001765 return 0;
1766}
1767
Robert Baldygaef4aca72014-11-24 07:56:22 +01001768static inline int
1769s3c24xx_serial_cpufreq_register(struct s3c24xx_uart_port *port)
Ben Dooks30555472008-10-21 14:06:36 +01001770{
1771 port->freq_transition.notifier_call = s3c24xx_serial_cpufreq_transition;
1772
1773 return cpufreq_register_notifier(&port->freq_transition,
1774 CPUFREQ_TRANSITION_NOTIFIER);
1775}
1776
Robert Baldygaef4aca72014-11-24 07:56:22 +01001777static inline void
1778s3c24xx_serial_cpufreq_deregister(struct s3c24xx_uart_port *port)
Ben Dooks30555472008-10-21 14:06:36 +01001779{
1780 cpufreq_unregister_notifier(&port->freq_transition,
1781 CPUFREQ_TRANSITION_NOTIFIER);
1782}
1783
1784#else
Robert Baldygaef4aca72014-11-24 07:56:22 +01001785static inline int
1786s3c24xx_serial_cpufreq_register(struct s3c24xx_uart_port *port)
Ben Dooks30555472008-10-21 14:06:36 +01001787{
1788 return 0;
1789}
1790
Robert Baldygaef4aca72014-11-24 07:56:22 +01001791static inline void
1792s3c24xx_serial_cpufreq_deregister(struct s3c24xx_uart_port *port)
Ben Dooks30555472008-10-21 14:06:36 +01001793{
1794}
1795#endif
1796
Stuart Menefy5086e0a2019-02-12 21:40:22 +00001797static int s3c24xx_serial_enable_baudclk(struct s3c24xx_uart_port *ourport)
1798{
1799 struct device *dev = ourport->port.dev;
1800 struct s3c24xx_uart_info *info = ourport->info;
1801 char clk_name[MAX_CLK_NAME_LENGTH];
1802 unsigned int clk_sel;
1803 struct clk *clk;
1804 int clk_num;
1805 int ret;
1806
1807 clk_sel = ourport->cfg->clk_sel ? : info->def_clk_sel;
1808 for (clk_num = 0; clk_num < info->num_clks; clk_num++) {
1809 if (!(clk_sel & (1 << clk_num)))
1810 continue;
1811
1812 sprintf(clk_name, "clk_uart_baud%d", clk_num);
1813 clk = clk_get(dev, clk_name);
1814 if (IS_ERR(clk))
1815 continue;
1816
1817 ret = clk_prepare_enable(clk);
1818 if (ret) {
1819 clk_put(clk);
1820 continue;
1821 }
1822
1823 ourport->baudclk = clk;
1824 ourport->baudclk_rate = clk_get_rate(clk);
1825 s3c24xx_serial_setsource(&ourport->port, clk_num);
1826
1827 return 0;
1828 }
1829
1830 return -EINVAL;
1831}
1832
Ben Dooksb4975492008-07-03 12:32:51 +01001833/* s3c24xx_serial_init_port
1834 *
1835 * initialise a single serial port from the platform device given
1836 */
1837
1838static int s3c24xx_serial_init_port(struct s3c24xx_uart_port *ourport,
Ben Dooksb4975492008-07-03 12:32:51 +01001839 struct platform_device *platdev)
1840{
1841 struct uart_port *port = &ourport->port;
Thomas Abrahamda121502011-11-02 19:23:25 +09001842 struct s3c2410_uartcfg *cfg = ourport->cfg;
Ben Dooksb4975492008-07-03 12:32:51 +01001843 struct resource *res;
1844 int ret;
1845
Ben Dooksb4975492008-07-03 12:32:51 +01001846 if (platdev == NULL)
1847 return -ENODEV;
1848
Ben Dooksb4975492008-07-03 12:32:51 +01001849 if (port->mapbase != 0)
Krzysztof Kozlowskie51e4d82016-06-16 08:27:35 +02001850 return -EINVAL;
Ben Dooksb4975492008-07-03 12:32:51 +01001851
Ben Dooksb4975492008-07-03 12:32:51 +01001852 /* setup info for port */
1853 port->dev = &platdev->dev;
Ben Dooksb4975492008-07-03 12:32:51 +01001854
Thomas Abraham88bb4ea2011-08-10 15:51:19 +05301855 /* Startup sequence is different for s3c64xx and higher SoC's */
1856 if (s3c24xx_serial_has_interrupt_mask(port))
1857 s3c24xx_serial_ops.startup = s3c64xx_serial_startup;
1858
Ben Dooksb4975492008-07-03 12:32:51 +01001859 port->uartclk = 1;
1860
1861 if (cfg->uart_flags & UPF_CONS_FLOW) {
Greg Kroah-Hartmana05025d2019-12-10 15:37:03 +01001862 dev_dbg(port->dev, "enabling flow control\n");
Ben Dooksb4975492008-07-03 12:32:51 +01001863 port->flags |= UPF_CONS_FLOW;
1864 }
1865
1866 /* sort our the physical and virtual addresses for each UART */
1867
1868 res = platform_get_resource(platdev, IORESOURCE_MEM, 0);
1869 if (res == NULL) {
Sachin Kamatd20925e2012-09-05 10:30:10 +05301870 dev_err(port->dev, "failed to find memory resource for uart\n");
Ben Dooksb4975492008-07-03 12:32:51 +01001871 return -EINVAL;
1872 }
1873
Greg Kroah-Hartmana05025d2019-12-10 15:37:03 +01001874 dev_dbg(port->dev, "resource %pR)\n", res);
Ben Dooksb4975492008-07-03 12:32:51 +01001875
Thomas Abraham41147bf2013-01-01 00:21:55 -08001876 port->membase = devm_ioremap(port->dev, res->start, resource_size(res));
1877 if (!port->membase) {
1878 dev_err(port->dev, "failed to remap controller address\n");
1879 return -EBUSY;
1880 }
1881
Ben Dooksb690ace2008-10-21 14:07:03 +01001882 port->mapbase = res->start;
Ben Dooksb4975492008-07-03 12:32:51 +01001883 ret = platform_get_irq(platdev, 0);
1884 if (ret < 0)
1885 port->irq = 0;
Ben Dooksb73c289c2008-10-21 14:07:04 +01001886 else {
Ben Dooksb4975492008-07-03 12:32:51 +01001887 port->irq = ret;
Ben Dooksb73c289c2008-10-21 14:07:04 +01001888 ourport->rx_irq = ret;
1889 ourport->tx_irq = ret + 1;
1890 }
Sachin Kamat9303ac12012-09-05 10:30:11 +05301891
Ben Dooksb73c289c2008-10-21 14:07:04 +01001892 ret = platform_get_irq(platdev, 1);
1893 if (ret > 0)
1894 ourport->tx_irq = ret;
Robert Baldyga658c9d2b72014-12-10 12:49:23 +01001895 /*
1896 * DMA is currently supported only on DT platforms, if DMA properties
1897 * are specified.
1898 */
1899 if (platdev->dev.of_node && of_find_property(platdev->dev.of_node,
1900 "dmas", NULL)) {
1901 ourport->dma = devm_kzalloc(port->dev,
1902 sizeof(*ourport->dma),
1903 GFP_KERNEL);
Krzysztof Kozlowskie51e4d82016-06-16 08:27:35 +02001904 if (!ourport->dma) {
1905 ret = -ENOMEM;
1906 goto err;
1907 }
Robert Baldyga658c9d2b72014-12-10 12:49:23 +01001908 }
Ben Dooksb4975492008-07-03 12:32:51 +01001909
1910 ourport->clk = clk_get(&platdev->dev, "uart");
Chander Kashyap60e93572013-05-28 18:32:07 +05301911 if (IS_ERR(ourport->clk)) {
1912 pr_err("%s: Controller clock not found\n",
1913 dev_name(&platdev->dev));
Krzysztof Kozlowskie51e4d82016-06-16 08:27:35 +02001914 ret = PTR_ERR(ourport->clk);
1915 goto err;
Chander Kashyap60e93572013-05-28 18:32:07 +05301916 }
1917
1918 ret = clk_prepare_enable(ourport->clk);
1919 if (ret) {
1920 pr_err("uart: clock failed to prepare+enable: %d\n", ret);
1921 clk_put(ourport->clk);
Krzysztof Kozlowskie51e4d82016-06-16 08:27:35 +02001922 goto err;
Chander Kashyap60e93572013-05-28 18:32:07 +05301923 }
Ben Dooksb4975492008-07-03 12:32:51 +01001924
Stuart Menefy5086e0a2019-02-12 21:40:22 +00001925 ret = s3c24xx_serial_enable_baudclk(ourport);
1926 if (ret)
1927 pr_warn("uart: failed to enable baudclk\n");
1928
Thomas Abraham88bb4ea2011-08-10 15:51:19 +05301929 /* Keep all interrupts masked and cleared */
1930 if (s3c24xx_serial_has_interrupt_mask(port)) {
1931 wr_regl(port, S3C64XX_UINTM, 0xf);
1932 wr_regl(port, S3C64XX_UINTP, 0xf);
1933 wr_regl(port, S3C64XX_UINTSP, 0xf);
1934 }
1935
Greg Kroah-Hartmana05025d2019-12-10 15:37:03 +01001936 dev_dbg(port->dev, "port: map=%pa, mem=%p, irq=%d (%d,%d), clock=%u\n",
1937 &port->mapbase, port->membase, port->irq,
1938 ourport->rx_irq, ourport->tx_irq, port->uartclk);
Ben Dooksb4975492008-07-03 12:32:51 +01001939
1940 /* reset the fifos (and setup the uart) */
1941 s3c24xx_serial_resetport(port, cfg);
Krzysztof Kozlowskie51e4d82016-06-16 08:27:35 +02001942
Ben Dooksb4975492008-07-03 12:32:51 +01001943 return 0;
Krzysztof Kozlowskie51e4d82016-06-16 08:27:35 +02001944
1945err:
1946 port->mapbase = 0;
1947 return ret;
Ben Dooksb4975492008-07-03 12:32:51 +01001948}
1949
Ben Dooksb4975492008-07-03 12:32:51 +01001950/* Device driver serial port probe */
1951
Greg Kroah-Hartman06674e52019-12-10 15:36:58 +01001952#ifdef CONFIG_OF
Thomas Abraham26c919e2011-11-06 22:10:44 +05301953static const struct of_device_id s3c24xx_uart_dt_match[];
Greg Kroah-Hartman06674e52019-12-10 15:36:58 +01001954#endif
1955
Ben Dooksb4975492008-07-03 12:32:51 +01001956static int probe_index;
1957
Thomas Abraham26c919e2011-11-06 22:10:44 +05301958static inline struct s3c24xx_serial_drv_data *s3c24xx_get_driver_data(
1959 struct platform_device *pdev)
1960{
1961#ifdef CONFIG_OF
1962 if (pdev->dev.of_node) {
1963 const struct of_device_id *match;
1964 match = of_match_node(s3c24xx_uart_dt_match, pdev->dev.of_node);
1965 return (struct s3c24xx_serial_drv_data *)match->data;
1966 }
1967#endif
1968 return (struct s3c24xx_serial_drv_data *)
1969 platform_get_device_id(pdev)->driver_data;
1970}
1971
Thomas Abrahamda121502011-11-02 19:23:25 +09001972static int s3c24xx_serial_probe(struct platform_device *pdev)
Ben Dooksb4975492008-07-03 12:32:51 +01001973{
Naveen Krishna Chatradhi4622eb62014-07-14 17:07:18 +05301974 struct device_node *np = pdev->dev.of_node;
Ben Dooksb4975492008-07-03 12:32:51 +01001975 struct s3c24xx_uart_port *ourport;
Tomasz Figa13a9f6c62014-06-26 13:24:34 +02001976 int index = probe_index;
Ben Dooksb4975492008-07-03 12:32:51 +01001977 int ret;
1978
Naveen Krishna Chatradhi4622eb62014-07-14 17:07:18 +05301979 if (np) {
1980 ret = of_alias_get_id(np, "serial");
Tomasz Figa13a9f6c62014-06-26 13:24:34 +02001981 if (ret >= 0)
1982 index = ret;
1983 }
Ben Dooksb4975492008-07-03 12:32:51 +01001984
Geert Uytterhoeven49ee23b2018-02-23 14:38:34 +01001985 if (index >= ARRAY_SIZE(s3c24xx_serial_ports)) {
1986 dev_err(&pdev->dev, "serial%d out of range\n", index);
1987 return -EINVAL;
1988 }
Tomasz Figa13a9f6c62014-06-26 13:24:34 +02001989 ourport = &s3c24xx_serial_ports[index];
Thomas Abrahamda121502011-11-02 19:23:25 +09001990
Thomas Abraham26c919e2011-11-06 22:10:44 +05301991 ourport->drv_data = s3c24xx_get_driver_data(pdev);
1992 if (!ourport->drv_data) {
1993 dev_err(&pdev->dev, "could not find driver data\n");
1994 return -ENODEV;
1995 }
Thomas Abrahamda121502011-11-02 19:23:25 +09001996
Kyoungil Kim7cd88832012-05-20 17:45:54 +09001997 ourport->baudclk = ERR_PTR(-EINVAL);
Thomas Abrahamda121502011-11-02 19:23:25 +09001998 ourport->info = ourport->drv_data->info;
Jingoo Han574de552013-07-30 17:06:57 +09001999 ourport->cfg = (dev_get_platdata(&pdev->dev)) ?
Jingoo Hand4aab202013-09-09 14:10:30 +09002000 dev_get_platdata(&pdev->dev) :
Thomas Abrahamda121502011-11-02 19:23:25 +09002001 ourport->drv_data->def_cfg;
2002
Naveen Krishna Chatradhi4622eb62014-07-14 17:07:18 +05302003 if (np)
2004 of_property_read_u32(np,
Naveen Krishna Chatradhi135f07c2014-07-14 17:07:16 +05302005 "samsung,uart-fifosize", &ourport->port.fifosize);
2006
Robert Baldyga2f1ba722014-11-24 07:56:23 +01002007 if (ourport->drv_data->fifosize[index])
2008 ourport->port.fifosize = ourport->drv_data->fifosize[index];
2009 else if (ourport->info->fifosize)
2010 ourport->port.fifosize = ourport->info->fifosize;
Thomas Abrahamda121502011-11-02 19:23:25 +09002011
Marek Szyprowski81ccb2a2015-07-31 10:58:27 +02002012 /*
2013 * DMA transfers must be aligned at least to cache line size,
2014 * so find minimal transfer size suitable for DMA mode
2015 */
2016 ourport->min_dma_size = max_t(int, ourport->port.fifosize,
2017 dma_get_cache_alignment());
2018
Greg Kroah-Hartmana05025d2019-12-10 15:37:03 +01002019 dev_dbg(&pdev->dev, "%s: initialising port %p...\n", __func__, ourport);
Ben Dooksb4975492008-07-03 12:32:51 +01002020
Thomas Abrahamda121502011-11-02 19:23:25 +09002021 ret = s3c24xx_serial_init_port(ourport, pdev);
Ben Dooksb4975492008-07-03 12:32:51 +01002022 if (ret < 0)
Tushar Behera8ad711a2014-06-23 11:32:14 +05302023 return ret;
Ben Dooksb4975492008-07-03 12:32:51 +01002024
Tushar Behera6f134c3c2014-01-20 14:32:34 +05302025 if (!s3c24xx_uart_drv.state) {
2026 ret = uart_register_driver(&s3c24xx_uart_drv);
2027 if (ret < 0) {
2028 pr_err("Failed to register Samsung UART driver\n");
2029 return ret;
2030 }
2031 }
2032
Greg Kroah-Hartmana05025d2019-12-10 15:37:03 +01002033 dev_dbg(&pdev->dev, "%s: adding port\n", __func__);
Ben Dooksb4975492008-07-03 12:32:51 +01002034 uart_add_one_port(&s3c24xx_uart_drv, &ourport->port);
Thomas Abrahamda121502011-11-02 19:23:25 +09002035 platform_set_drvdata(pdev, &ourport->port);
Ben Dooksb4975492008-07-03 12:32:51 +01002036
Heiko Stübner0da33362013-12-05 00:54:38 +01002037 /*
2038 * Deactivate the clock enabled in s3c24xx_serial_init_port here,
2039 * so that a potential re-enablement through the pm-callback overlaps
2040 * and keeps the clock enabled in this case.
2041 */
2042 clk_disable_unprepare(ourport->clk);
Stuart Menefy5086e0a2019-02-12 21:40:22 +00002043 if (!IS_ERR(ourport->baudclk))
2044 clk_disable_unprepare(ourport->baudclk);
Heiko Stübner0da33362013-12-05 00:54:38 +01002045
Ben Dooks30555472008-10-21 14:06:36 +01002046 ret = s3c24xx_serial_cpufreq_register(ourport);
2047 if (ret < 0)
Thomas Abrahamda121502011-11-02 19:23:25 +09002048 dev_err(&pdev->dev, "failed to add cpufreq notifier\n");
Ben Dooks30555472008-10-21 14:06:36 +01002049
Krzysztof Kozlowski926b7b52016-06-16 08:27:36 +02002050 probe_index++;
2051
Ben Dooksb4975492008-07-03 12:32:51 +01002052 return 0;
Ben Dooksb4975492008-07-03 12:32:51 +01002053}
2054
Bill Pembertonae8d8a12012-11-19 13:26:18 -05002055static int s3c24xx_serial_remove(struct platform_device *dev)
Ben Dooksb4975492008-07-03 12:32:51 +01002056{
2057 struct uart_port *port = s3c24xx_dev_to_port(&dev->dev);
2058
2059 if (port) {
Ben Dooks30555472008-10-21 14:06:36 +01002060 s3c24xx_serial_cpufreq_deregister(to_ourport(port));
Ben Dooksb4975492008-07-03 12:32:51 +01002061 uart_remove_one_port(&s3c24xx_uart_drv, port);
2062 }
2063
Tushar Behera6f134c3c2014-01-20 14:32:34 +05302064 uart_unregister_driver(&s3c24xx_uart_drv);
2065
Ben Dooksb4975492008-07-03 12:32:51 +01002066 return 0;
2067}
2068
Ben Dooksb4975492008-07-03 12:32:51 +01002069/* UART power management code */
MyungJoo Hamaef7fe52011-06-29 15:28:24 +09002070#ifdef CONFIG_PM_SLEEP
2071static int s3c24xx_serial_suspend(struct device *dev)
Ben Dooksb4975492008-07-03 12:32:51 +01002072{
MyungJoo Hamaef7fe52011-06-29 15:28:24 +09002073 struct uart_port *port = s3c24xx_dev_to_port(dev);
Ben Dooksb4975492008-07-03 12:32:51 +01002074
2075 if (port)
2076 uart_suspend_port(&s3c24xx_uart_drv, port);
2077
2078 return 0;
2079}
2080
MyungJoo Hamaef7fe52011-06-29 15:28:24 +09002081static int s3c24xx_serial_resume(struct device *dev)
Ben Dooksb4975492008-07-03 12:32:51 +01002082{
MyungJoo Hamaef7fe52011-06-29 15:28:24 +09002083 struct uart_port *port = s3c24xx_dev_to_port(dev);
Ben Dooksb4975492008-07-03 12:32:51 +01002084 struct s3c24xx_uart_port *ourport = to_ourport(port);
2085
2086 if (port) {
Thomas Abraham9484b002012-10-03 07:40:04 +09002087 clk_prepare_enable(ourport->clk);
Marek Szyprowski1ff36522018-09-13 10:21:25 +02002088 if (!IS_ERR(ourport->baudclk))
2089 clk_prepare_enable(ourport->baudclk);
Ben Dooksb4975492008-07-03 12:32:51 +01002090 s3c24xx_serial_resetport(port, s3c24xx_port_to_cfg(port));
Marek Szyprowski1ff36522018-09-13 10:21:25 +02002091 if (!IS_ERR(ourport->baudclk))
2092 clk_disable_unprepare(ourport->baudclk);
Thomas Abraham9484b002012-10-03 07:40:04 +09002093 clk_disable_unprepare(ourport->clk);
Ben Dooksb4975492008-07-03 12:32:51 +01002094
2095 uart_resume_port(&s3c24xx_uart_drv, port);
2096 }
2097
2098 return 0;
2099}
MyungJoo Hamaef7fe52011-06-29 15:28:24 +09002100
Michael Spangd09a7302013-03-27 19:34:24 -04002101static int s3c24xx_serial_resume_noirq(struct device *dev)
2102{
2103 struct uart_port *port = s3c24xx_dev_to_port(dev);
남영민a8a17812017-02-01 19:25:46 +09002104 struct s3c24xx_uart_port *ourport = to_ourport(port);
Michael Spangd09a7302013-03-27 19:34:24 -04002105
2106 if (port) {
2107 /* restore IRQ mask */
2108 if (s3c24xx_serial_has_interrupt_mask(port)) {
2109 unsigned int uintm = 0xf;
2110 if (tx_enabled(port))
2111 uintm &= ~S3C64XX_UINTM_TXD_MSK;
2112 if (rx_enabled(port))
2113 uintm &= ~S3C64XX_UINTM_RXD_MSK;
남영민a8a17812017-02-01 19:25:46 +09002114 clk_prepare_enable(ourport->clk);
Marek Szyprowski1ff36522018-09-13 10:21:25 +02002115 if (!IS_ERR(ourport->baudclk))
2116 clk_prepare_enable(ourport->baudclk);
Michael Spangd09a7302013-03-27 19:34:24 -04002117 wr_regl(port, S3C64XX_UINTM, uintm);
Marek Szyprowski1ff36522018-09-13 10:21:25 +02002118 if (!IS_ERR(ourport->baudclk))
2119 clk_disable_unprepare(ourport->baudclk);
남영민a8a17812017-02-01 19:25:46 +09002120 clk_disable_unprepare(ourport->clk);
Michael Spangd09a7302013-03-27 19:34:24 -04002121 }
2122 }
2123
2124 return 0;
2125}
2126
MyungJoo Hamaef7fe52011-06-29 15:28:24 +09002127static const struct dev_pm_ops s3c24xx_serial_pm_ops = {
2128 .suspend = s3c24xx_serial_suspend,
2129 .resume = s3c24xx_serial_resume,
Michael Spangd09a7302013-03-27 19:34:24 -04002130 .resume_noirq = s3c24xx_serial_resume_noirq,
MyungJoo Hamaef7fe52011-06-29 15:28:24 +09002131};
Kukjin Kimb882fc12011-07-28 08:50:38 +09002132#define SERIAL_SAMSUNG_PM_OPS (&s3c24xx_serial_pm_ops)
2133
MyungJoo Hamaef7fe52011-06-29 15:28:24 +09002134#else /* !CONFIG_PM_SLEEP */
Kukjin Kimb882fc12011-07-28 08:50:38 +09002135
2136#define SERIAL_SAMSUNG_PM_OPS NULL
MyungJoo Hamaef7fe52011-06-29 15:28:24 +09002137#endif /* CONFIG_PM_SLEEP */
Ben Dooksb4975492008-07-03 12:32:51 +01002138
Ben Dooksb4975492008-07-03 12:32:51 +01002139/* Console code */
2140
2141#ifdef CONFIG_SERIAL_SAMSUNG_CONSOLE
2142
2143static struct uart_port *cons_uart;
2144
2145static int
2146s3c24xx_serial_console_txrdy(struct uart_port *port, unsigned int ufcon)
2147{
2148 struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
2149 unsigned long ufstat, utrstat;
2150
2151 if (ufcon & S3C2410_UFCON_FIFOMODE) {
Uwe Kleine-König9ddc5b62010-01-20 17:02:24 +01002152 /* fifo mode - check amount of data in fifo registers... */
Ben Dooksb4975492008-07-03 12:32:51 +01002153
2154 ufstat = rd_regl(port, S3C2410_UFSTAT);
2155 return (ufstat & info->tx_fifofull) ? 0 : 1;
2156 }
2157
2158 /* in non-fifo mode, we go and use the tx buffer empty */
2159
2160 utrstat = rd_regl(port, S3C2410_UTRSTAT);
2161 return (utrstat & S3C2410_UTRSTAT_TXE) ? 1 : 0;
2162}
2163
Michael Spang38adbc52013-03-27 19:34:25 -04002164static bool
2165s3c24xx_port_configured(unsigned int ucon)
2166{
2167 /* consider the serial port configured if the tx/rx mode set */
2168 return (ucon & 0xf) != 0;
2169}
2170
Julien Pichon93b5c032012-09-21 23:22:31 -07002171#ifdef CONFIG_CONSOLE_POLL
2172/*
2173 * Console polling routines for writing and reading from the uart while
2174 * in an interrupt or debug context.
2175 */
2176
2177static int s3c24xx_serial_get_poll_char(struct uart_port *port)
2178{
2179 struct s3c24xx_uart_port *ourport = to_ourport(port);
2180 unsigned int ufstat;
2181
2182 ufstat = rd_regl(port, S3C2410_UFSTAT);
2183 if (s3c24xx_serial_rx_fifocnt(ourport, ufstat) == 0)
2184 return NO_POLL_CHAR;
2185
2186 return rd_regb(port, S3C2410_URXH);
2187}
2188
2189static void s3c24xx_serial_put_poll_char(struct uart_port *port,
2190 unsigned char c)
2191{
Doug Andersonbb7f09b2014-04-21 09:40:34 -07002192 unsigned int ufcon = rd_regl(port, S3C2410_UFCON);
2193 unsigned int ucon = rd_regl(port, S3C2410_UCON);
Michael Spang38adbc52013-03-27 19:34:25 -04002194
2195 /* not possible to xmit on unconfigured port */
2196 if (!s3c24xx_port_configured(ucon))
2197 return;
Julien Pichon93b5c032012-09-21 23:22:31 -07002198
2199 while (!s3c24xx_serial_console_txrdy(port, ufcon))
2200 cpu_relax();
Doug Andersonbb7f09b2014-04-21 09:40:34 -07002201 wr_regb(port, S3C2410_UTXH, c);
Julien Pichon93b5c032012-09-21 23:22:31 -07002202}
2203
2204#endif /* CONFIG_CONSOLE_POLL */
2205
Ben Dooksb4975492008-07-03 12:32:51 +01002206static void
2207s3c24xx_serial_console_putchar(struct uart_port *port, int ch)
2208{
Doug Andersonbb7f09b2014-04-21 09:40:34 -07002209 unsigned int ufcon = rd_regl(port, S3C2410_UFCON);
Michael Spang38adbc52013-03-27 19:34:25 -04002210
Ben Dooksb4975492008-07-03 12:32:51 +01002211 while (!s3c24xx_serial_console_txrdy(port, ufcon))
Doug Andersonf94b0572014-04-21 09:40:36 -07002212 cpu_relax();
Doug Andersonbb7f09b2014-04-21 09:40:34 -07002213 wr_regb(port, S3C2410_UTXH, ch);
Ben Dooksb4975492008-07-03 12:32:51 +01002214}
2215
2216static void
2217s3c24xx_serial_console_write(struct console *co, const char *s,
2218 unsigned int count)
2219{
Doug Andersonab88c8d2014-04-21 09:40:35 -07002220 unsigned int ucon = rd_regl(cons_uart, S3C2410_UCON);
2221
2222 /* not possible to xmit on unconfigured port */
2223 if (!s3c24xx_port_configured(ucon))
2224 return;
2225
Ben Dooksb4975492008-07-03 12:32:51 +01002226 uart_console_write(cons_uart, s, count, s3c24xx_serial_console_putchar);
2227}
2228
2229static void __init
2230s3c24xx_serial_get_options(struct uart_port *port, int *baud,
2231 int *parity, int *bits)
2232{
Ben Dooksb4975492008-07-03 12:32:51 +01002233 struct clk *clk;
2234 unsigned int ulcon;
2235 unsigned int ucon;
2236 unsigned int ubrdiv;
2237 unsigned long rate;
Thomas Abraham5f5a7a52011-10-24 11:47:46 +02002238 unsigned int clk_sel;
2239 char clk_name[MAX_CLK_NAME_LENGTH];
Ben Dooksb4975492008-07-03 12:32:51 +01002240
2241 ulcon = rd_regl(port, S3C2410_ULCON);
2242 ucon = rd_regl(port, S3C2410_UCON);
2243 ubrdiv = rd_regl(port, S3C2410_UBRDIV);
2244
Michael Spang38adbc52013-03-27 19:34:25 -04002245 if (s3c24xx_port_configured(ucon)) {
Ben Dooksb4975492008-07-03 12:32:51 +01002246 switch (ulcon & S3C2410_LCON_CSMASK) {
2247 case S3C2410_LCON_CS5:
2248 *bits = 5;
2249 break;
2250 case S3C2410_LCON_CS6:
2251 *bits = 6;
2252 break;
2253 case S3C2410_LCON_CS7:
2254 *bits = 7;
2255 break;
Ben Dooksb4975492008-07-03 12:32:51 +01002256 case S3C2410_LCON_CS8:
Naveen Krishna Chatradhi3bcce592014-07-14 17:07:17 +05302257 default:
Ben Dooksb4975492008-07-03 12:32:51 +01002258 *bits = 8;
2259 break;
2260 }
2261
2262 switch (ulcon & S3C2410_LCON_PMASK) {
2263 case S3C2410_LCON_PEVEN:
2264 *parity = 'e';
2265 break;
2266
2267 case S3C2410_LCON_PODD:
2268 *parity = 'o';
2269 break;
2270
2271 case S3C2410_LCON_PNONE:
2272 default:
2273 *parity = 'n';
2274 }
2275
2276 /* now calculate the baud rate */
2277
Thomas Abraham5f5a7a52011-10-24 11:47:46 +02002278 clk_sel = s3c24xx_serial_getsource(port);
2279 sprintf(clk_name, "clk_uart_baud%d", clk_sel);
Ben Dooksb4975492008-07-03 12:32:51 +01002280
Thomas Abraham5f5a7a52011-10-24 11:47:46 +02002281 clk = clk_get(port->dev, clk_name);
Kyoungil Kim7cd88832012-05-20 17:45:54 +09002282 if (!IS_ERR(clk))
Thomas Abraham5f5a7a52011-10-24 11:47:46 +02002283 rate = clk_get_rate(clk);
Ben Dooksb4975492008-07-03 12:32:51 +01002284 else
2285 rate = 1;
2286
Ben Dooksb4975492008-07-03 12:32:51 +01002287 *baud = rate / (16 * (ubrdiv + 1));
Greg Kroah-Hartmana05025d2019-12-10 15:37:03 +01002288 dev_dbg(port->dev, "calculated baud %d\n", *baud);
Ben Dooksb4975492008-07-03 12:32:51 +01002289 }
2290
2291}
2292
Ben Dooksb4975492008-07-03 12:32:51 +01002293static int __init
2294s3c24xx_serial_console_setup(struct console *co, char *options)
2295{
2296 struct uart_port *port;
2297 int baud = 9600;
2298 int bits = 8;
2299 int parity = 'n';
2300 int flow = 'n';
2301
Ben Dooksb4975492008-07-03 12:32:51 +01002302 /* is this a valid port */
2303
Ben Dooks03d5e772008-11-03 09:21:23 +00002304 if (co->index == -1 || co->index >= CONFIG_SERIAL_SAMSUNG_UARTS)
Ben Dooksb4975492008-07-03 12:32:51 +01002305 co->index = 0;
2306
2307 port = &s3c24xx_serial_ports[co->index].port;
2308
2309 /* is the port configured? */
2310
Thomas Abrahamee430f12011-06-14 19:12:26 +09002311 if (port->mapbase == 0x0)
2312 return -ENODEV;
Ben Dooksb4975492008-07-03 12:32:51 +01002313
2314 cons_uart = port;
2315
Ben Dooksb4975492008-07-03 12:32:51 +01002316 /*
2317 * Check whether an invalid uart number has been specified, and
2318 * if so, search for the first available port that does have
2319 * console support.
2320 */
2321 if (options)
2322 uart_parse_options(options, &baud, &parity, &bits, &flow);
2323 else
2324 s3c24xx_serial_get_options(port, &baud, &parity, &bits);
2325
Greg Kroah-Hartmana05025d2019-12-10 15:37:03 +01002326 dev_dbg(port->dev, "baud %d\n", baud);
Ben Dooksb4975492008-07-03 12:32:51 +01002327
2328 return uart_set_options(port, co, baud, parity, bits, flow);
2329}
2330
Ben Dooksb4975492008-07-03 12:32:51 +01002331static struct console s3c24xx_serial_console = {
2332 .name = S3C24XX_SERIAL_NAME,
2333 .device = uart_console_device,
2334 .flags = CON_PRINTBUFFER,
2335 .index = -1,
2336 .write = s3c24xx_serial_console_write,
Thomas Abraham5822a5d2011-06-14 19:12:26 +09002337 .setup = s3c24xx_serial_console_setup,
2338 .data = &s3c24xx_uart_drv,
Ben Dooksb4975492008-07-03 12:32:51 +01002339};
Ben Dooksb4975492008-07-03 12:32:51 +01002340#endif /* CONFIG_SERIAL_SAMSUNG_CONSOLE */
2341
Thomas Abrahamda121502011-11-02 19:23:25 +09002342#ifdef CONFIG_CPU_S3C2410
2343static struct s3c24xx_serial_drv_data s3c2410_serial_drv_data = {
2344 .info = &(struct s3c24xx_uart_info) {
2345 .name = "Samsung S3C2410 UART",
2346 .type = PORT_S3C2410,
2347 .fifosize = 16,
2348 .rx_fifomask = S3C2410_UFSTAT_RXMASK,
2349 .rx_fifoshift = S3C2410_UFSTAT_RXSHIFT,
2350 .rx_fifofull = S3C2410_UFSTAT_RXFULL,
2351 .tx_fifofull = S3C2410_UFSTAT_TXFULL,
2352 .tx_fifomask = S3C2410_UFSTAT_TXMASK,
2353 .tx_fifoshift = S3C2410_UFSTAT_TXSHIFT,
2354 .def_clk_sel = S3C2410_UCON_CLKSEL0,
2355 .num_clks = 2,
2356 .clksel_mask = S3C2410_UCON_CLKMASK,
2357 .clksel_shift = S3C2410_UCON_CLKSHIFT,
2358 },
2359 .def_cfg = &(struct s3c2410_uartcfg) {
2360 .ucon = S3C2410_UCON_DEFAULT,
2361 .ufcon = S3C2410_UFCON_DEFAULT,
2362 },
2363};
2364#define S3C2410_SERIAL_DRV_DATA ((kernel_ulong_t)&s3c2410_serial_drv_data)
2365#else
2366#define S3C2410_SERIAL_DRV_DATA (kernel_ulong_t)NULL
2367#endif
2368
2369#ifdef CONFIG_CPU_S3C2412
2370static struct s3c24xx_serial_drv_data s3c2412_serial_drv_data = {
2371 .info = &(struct s3c24xx_uart_info) {
2372 .name = "Samsung S3C2412 UART",
2373 .type = PORT_S3C2412,
2374 .fifosize = 64,
2375 .has_divslot = 1,
2376 .rx_fifomask = S3C2440_UFSTAT_RXMASK,
2377 .rx_fifoshift = S3C2440_UFSTAT_RXSHIFT,
2378 .rx_fifofull = S3C2440_UFSTAT_RXFULL,
2379 .tx_fifofull = S3C2440_UFSTAT_TXFULL,
2380 .tx_fifomask = S3C2440_UFSTAT_TXMASK,
2381 .tx_fifoshift = S3C2440_UFSTAT_TXSHIFT,
2382 .def_clk_sel = S3C2410_UCON_CLKSEL2,
2383 .num_clks = 4,
2384 .clksel_mask = S3C2412_UCON_CLKMASK,
2385 .clksel_shift = S3C2412_UCON_CLKSHIFT,
2386 },
2387 .def_cfg = &(struct s3c2410_uartcfg) {
2388 .ucon = S3C2410_UCON_DEFAULT,
2389 .ufcon = S3C2410_UFCON_DEFAULT,
2390 },
2391};
2392#define S3C2412_SERIAL_DRV_DATA ((kernel_ulong_t)&s3c2412_serial_drv_data)
2393#else
2394#define S3C2412_SERIAL_DRV_DATA (kernel_ulong_t)NULL
2395#endif
2396
2397#if defined(CONFIG_CPU_S3C2440) || defined(CONFIG_CPU_S3C2416) || \
Denis 'GNUtoo' Cariklib26469a2012-02-23 08:23:52 +01002398 defined(CONFIG_CPU_S3C2443) || defined(CONFIG_CPU_S3C2442)
Thomas Abrahamda121502011-11-02 19:23:25 +09002399static struct s3c24xx_serial_drv_data s3c2440_serial_drv_data = {
2400 .info = &(struct s3c24xx_uart_info) {
2401 .name = "Samsung S3C2440 UART",
2402 .type = PORT_S3C2440,
2403 .fifosize = 64,
2404 .has_divslot = 1,
2405 .rx_fifomask = S3C2440_UFSTAT_RXMASK,
2406 .rx_fifoshift = S3C2440_UFSTAT_RXSHIFT,
2407 .rx_fifofull = S3C2440_UFSTAT_RXFULL,
2408 .tx_fifofull = S3C2440_UFSTAT_TXFULL,
2409 .tx_fifomask = S3C2440_UFSTAT_TXMASK,
2410 .tx_fifoshift = S3C2440_UFSTAT_TXSHIFT,
2411 .def_clk_sel = S3C2410_UCON_CLKSEL2,
2412 .num_clks = 4,
2413 .clksel_mask = S3C2412_UCON_CLKMASK,
2414 .clksel_shift = S3C2412_UCON_CLKSHIFT,
2415 },
2416 .def_cfg = &(struct s3c2410_uartcfg) {
2417 .ucon = S3C2410_UCON_DEFAULT,
2418 .ufcon = S3C2410_UFCON_DEFAULT,
2419 },
2420};
2421#define S3C2440_SERIAL_DRV_DATA ((kernel_ulong_t)&s3c2440_serial_drv_data)
2422#else
2423#define S3C2440_SERIAL_DRV_DATA (kernel_ulong_t)NULL
2424#endif
2425
Kukjin Kim953b53a2014-07-01 06:32:22 +09002426#if defined(CONFIG_CPU_S3C6400) || defined(CONFIG_CPU_S3C6410)
Thomas Abrahamda121502011-11-02 19:23:25 +09002427static struct s3c24xx_serial_drv_data s3c6400_serial_drv_data = {
2428 .info = &(struct s3c24xx_uart_info) {
2429 .name = "Samsung S3C6400 UART",
2430 .type = PORT_S3C6400,
2431 .fifosize = 64,
2432 .has_divslot = 1,
2433 .rx_fifomask = S3C2440_UFSTAT_RXMASK,
2434 .rx_fifoshift = S3C2440_UFSTAT_RXSHIFT,
2435 .rx_fifofull = S3C2440_UFSTAT_RXFULL,
2436 .tx_fifofull = S3C2440_UFSTAT_TXFULL,
2437 .tx_fifomask = S3C2440_UFSTAT_TXMASK,
2438 .tx_fifoshift = S3C2440_UFSTAT_TXSHIFT,
2439 .def_clk_sel = S3C2410_UCON_CLKSEL2,
2440 .num_clks = 4,
2441 .clksel_mask = S3C6400_UCON_CLKMASK,
2442 .clksel_shift = S3C6400_UCON_CLKSHIFT,
2443 },
2444 .def_cfg = &(struct s3c2410_uartcfg) {
2445 .ucon = S3C2410_UCON_DEFAULT,
2446 .ufcon = S3C2410_UFCON_DEFAULT,
2447 },
2448};
2449#define S3C6400_SERIAL_DRV_DATA ((kernel_ulong_t)&s3c6400_serial_drv_data)
2450#else
2451#define S3C6400_SERIAL_DRV_DATA (kernel_ulong_t)NULL
2452#endif
2453
2454#ifdef CONFIG_CPU_S5PV210
2455static struct s3c24xx_serial_drv_data s5pv210_serial_drv_data = {
2456 .info = &(struct s3c24xx_uart_info) {
2457 .name = "Samsung S5PV210 UART",
2458 .type = PORT_S3C6400,
2459 .has_divslot = 1,
2460 .rx_fifomask = S5PV210_UFSTAT_RXMASK,
2461 .rx_fifoshift = S5PV210_UFSTAT_RXSHIFT,
2462 .rx_fifofull = S5PV210_UFSTAT_RXFULL,
2463 .tx_fifofull = S5PV210_UFSTAT_TXFULL,
2464 .tx_fifomask = S5PV210_UFSTAT_TXMASK,
2465 .tx_fifoshift = S5PV210_UFSTAT_TXSHIFT,
2466 .def_clk_sel = S3C2410_UCON_CLKSEL0,
2467 .num_clks = 2,
2468 .clksel_mask = S5PV210_UCON_CLKMASK,
2469 .clksel_shift = S5PV210_UCON_CLKSHIFT,
2470 },
2471 .def_cfg = &(struct s3c2410_uartcfg) {
2472 .ucon = S5PV210_UCON_DEFAULT,
2473 .ufcon = S5PV210_UFCON_DEFAULT,
2474 },
2475 .fifosize = { 256, 64, 16, 16 },
2476};
2477#define S5PV210_SERIAL_DRV_DATA ((kernel_ulong_t)&s5pv210_serial_drv_data)
2478#else
2479#define S5PV210_SERIAL_DRV_DATA (kernel_ulong_t)NULL
2480#endif
2481
Chander Kashyap33f88132013-06-19 00:29:34 +09002482#if defined(CONFIG_ARCH_EXYNOS)
Chanwoo Choi31ec77a2014-12-02 17:49:54 +09002483#define EXYNOS_COMMON_SERIAL_DRV_DATA \
2484 .info = &(struct s3c24xx_uart_info) { \
2485 .name = "Samsung Exynos UART", \
2486 .type = PORT_S3C6400, \
2487 .has_divslot = 1, \
2488 .rx_fifomask = S5PV210_UFSTAT_RXMASK, \
2489 .rx_fifoshift = S5PV210_UFSTAT_RXSHIFT, \
2490 .rx_fifofull = S5PV210_UFSTAT_RXFULL, \
2491 .tx_fifofull = S5PV210_UFSTAT_TXFULL, \
2492 .tx_fifomask = S5PV210_UFSTAT_TXMASK, \
2493 .tx_fifoshift = S5PV210_UFSTAT_TXSHIFT, \
2494 .def_clk_sel = S3C2410_UCON_CLKSEL0, \
2495 .num_clks = 1, \
2496 .clksel_mask = 0, \
2497 .clksel_shift = 0, \
2498 }, \
2499 .def_cfg = &(struct s3c2410_uartcfg) { \
2500 .ucon = S5PV210_UCON_DEFAULT, \
2501 .ufcon = S5PV210_UFCON_DEFAULT, \
2502 .has_fracval = 1, \
2503 } \
2504
Thomas Abrahamda121502011-11-02 19:23:25 +09002505static struct s3c24xx_serial_drv_data exynos4210_serial_drv_data = {
Chanwoo Choi31ec77a2014-12-02 17:49:54 +09002506 EXYNOS_COMMON_SERIAL_DRV_DATA,
Thomas Abrahamda121502011-11-02 19:23:25 +09002507 .fifosize = { 256, 64, 16, 16 },
2508};
Chanwoo Choi31ec77a2014-12-02 17:49:54 +09002509
2510static struct s3c24xx_serial_drv_data exynos5433_serial_drv_data = {
2511 EXYNOS_COMMON_SERIAL_DRV_DATA,
2512 .fifosize = { 64, 256, 16, 256 },
2513};
2514
Thomas Abrahamda121502011-11-02 19:23:25 +09002515#define EXYNOS4210_SERIAL_DRV_DATA ((kernel_ulong_t)&exynos4210_serial_drv_data)
Chanwoo Choi31ec77a2014-12-02 17:49:54 +09002516#define EXYNOS5433_SERIAL_DRV_DATA ((kernel_ulong_t)&exynos5433_serial_drv_data)
Thomas Abrahamda121502011-11-02 19:23:25 +09002517#else
2518#define EXYNOS4210_SERIAL_DRV_DATA (kernel_ulong_t)NULL
Chanwoo Choi31ec77a2014-12-02 17:49:54 +09002519#define EXYNOS5433_SERIAL_DRV_DATA (kernel_ulong_t)NULL
Thomas Abrahamda121502011-11-02 19:23:25 +09002520#endif
2521
Krzysztof Kozlowski24ee4df2015-05-02 00:40:05 +09002522static const struct platform_device_id s3c24xx_serial_driver_ids[] = {
Thomas Abrahamda121502011-11-02 19:23:25 +09002523 {
2524 .name = "s3c2410-uart",
2525 .driver_data = S3C2410_SERIAL_DRV_DATA,
2526 }, {
2527 .name = "s3c2412-uart",
2528 .driver_data = S3C2412_SERIAL_DRV_DATA,
2529 }, {
2530 .name = "s3c2440-uart",
2531 .driver_data = S3C2440_SERIAL_DRV_DATA,
2532 }, {
2533 .name = "s3c6400-uart",
2534 .driver_data = S3C6400_SERIAL_DRV_DATA,
2535 }, {
2536 .name = "s5pv210-uart",
2537 .driver_data = S5PV210_SERIAL_DRV_DATA,
2538 }, {
2539 .name = "exynos4210-uart",
2540 .driver_data = EXYNOS4210_SERIAL_DRV_DATA,
Chanwoo Choi31ec77a2014-12-02 17:49:54 +09002541 }, {
2542 .name = "exynos5433-uart",
2543 .driver_data = EXYNOS5433_SERIAL_DRV_DATA,
Thomas Abrahamda121502011-11-02 19:23:25 +09002544 },
2545 { },
2546};
2547MODULE_DEVICE_TABLE(platform, s3c24xx_serial_driver_ids);
2548
Thomas Abraham26c919e2011-11-06 22:10:44 +05302549#ifdef CONFIG_OF
2550static const struct of_device_id s3c24xx_uart_dt_match[] = {
Heiko Stübner666ca0b2012-11-22 11:37:44 +01002551 { .compatible = "samsung,s3c2410-uart",
2552 .data = (void *)S3C2410_SERIAL_DRV_DATA },
2553 { .compatible = "samsung,s3c2412-uart",
2554 .data = (void *)S3C2412_SERIAL_DRV_DATA },
2555 { .compatible = "samsung,s3c2440-uart",
2556 .data = (void *)S3C2440_SERIAL_DRV_DATA },
2557 { .compatible = "samsung,s3c6400-uart",
2558 .data = (void *)S3C6400_SERIAL_DRV_DATA },
2559 { .compatible = "samsung,s5pv210-uart",
2560 .data = (void *)S5PV210_SERIAL_DRV_DATA },
Thomas Abraham26c919e2011-11-06 22:10:44 +05302561 { .compatible = "samsung,exynos4210-uart",
Mark Browna169a882011-11-08 17:00:14 +09002562 .data = (void *)EXYNOS4210_SERIAL_DRV_DATA },
Chanwoo Choi31ec77a2014-12-02 17:49:54 +09002563 { .compatible = "samsung,exynos5433-uart",
2564 .data = (void *)EXYNOS5433_SERIAL_DRV_DATA },
Thomas Abraham26c919e2011-11-06 22:10:44 +05302565 {},
2566};
2567MODULE_DEVICE_TABLE(of, s3c24xx_uart_dt_match);
Thomas Abraham26c919e2011-11-06 22:10:44 +05302568#endif
2569
Thomas Abrahamda121502011-11-02 19:23:25 +09002570static struct platform_driver samsung_serial_driver = {
2571 .probe = s3c24xx_serial_probe,
Bill Pemberton2d47b712012-11-19 13:21:34 -05002572 .remove = s3c24xx_serial_remove,
Thomas Abrahamda121502011-11-02 19:23:25 +09002573 .id_table = s3c24xx_serial_driver_ids,
2574 .driver = {
2575 .name = "samsung-uart",
Thomas Abrahamda121502011-11-02 19:23:25 +09002576 .pm = SERIAL_SAMSUNG_PM_OPS,
Sachin Kamat905f4ba2013-01-07 09:50:42 +05302577 .of_match_table = of_match_ptr(s3c24xx_uart_dt_match),
Thomas Abrahamda121502011-11-02 19:23:25 +09002578 },
2579};
2580
Tushar Behera6f134c3c2014-01-20 14:32:34 +05302581module_platform_driver(samsung_serial_driver);
Thomas Abrahamda121502011-11-02 19:23:25 +09002582
Marek Szyprowskic3bda292015-02-02 10:47:35 +01002583#ifdef CONFIG_SERIAL_SAMSUNG_CONSOLE
Tomasz Figab94ba032015-01-23 14:47:41 +01002584/*
2585 * Early console.
2586 */
2587
2588struct samsung_early_console_data {
2589 u32 txfull_mask;
2590};
2591
2592static void samsung_early_busyuart(struct uart_port *port)
2593{
2594 while (!(readl(port->membase + S3C2410_UTRSTAT) & S3C2410_UTRSTAT_TXFE))
2595 ;
2596}
2597
2598static void samsung_early_busyuart_fifo(struct uart_port *port)
2599{
2600 struct samsung_early_console_data *data = port->private_data;
2601
2602 while (readl(port->membase + S3C2410_UFSTAT) & data->txfull_mask)
2603 ;
2604}
2605
2606static void samsung_early_putc(struct uart_port *port, int c)
2607{
2608 if (readl(port->membase + S3C2410_UFCON) & S3C2410_UFCON_FIFOMODE)
2609 samsung_early_busyuart_fifo(port);
2610 else
2611 samsung_early_busyuart(port);
2612
2613 writeb(c, port->membase + S3C2410_UTXH);
2614}
2615
2616static void samsung_early_write(struct console *con, const char *s, unsigned n)
2617{
2618 struct earlycon_device *dev = con->data;
2619
2620 uart_console_write(&dev->port, s, n, samsung_early_putc);
2621}
2622
2623static int __init samsung_early_console_setup(struct earlycon_device *device,
2624 const char *opt)
2625{
2626 if (!device->port.membase)
2627 return -ENODEV;
2628
2629 device->con->write = samsung_early_write;
2630 return 0;
2631}
2632
2633/* S3C2410 */
2634static struct samsung_early_console_data s3c2410_early_console_data = {
2635 .txfull_mask = S3C2410_UFSTAT_TXFULL,
2636};
2637
2638static int __init s3c2410_early_console_setup(struct earlycon_device *device,
2639 const char *opt)
2640{
2641 device->port.private_data = &s3c2410_early_console_data;
2642 return samsung_early_console_setup(device, opt);
2643}
2644OF_EARLYCON_DECLARE(s3c2410, "samsung,s3c2410-uart",
2645 s3c2410_early_console_setup);
Tomasz Figab94ba032015-01-23 14:47:41 +01002646
2647/* S3C2412, S3C2440, S3C64xx */
2648static struct samsung_early_console_data s3c2440_early_console_data = {
2649 .txfull_mask = S3C2440_UFSTAT_TXFULL,
2650};
2651
2652static int __init s3c2440_early_console_setup(struct earlycon_device *device,
2653 const char *opt)
2654{
2655 device->port.private_data = &s3c2440_early_console_data;
2656 return samsung_early_console_setup(device, opt);
2657}
2658OF_EARLYCON_DECLARE(s3c2412, "samsung,s3c2412-uart",
2659 s3c2440_early_console_setup);
2660OF_EARLYCON_DECLARE(s3c2440, "samsung,s3c2440-uart",
2661 s3c2440_early_console_setup);
2662OF_EARLYCON_DECLARE(s3c6400, "samsung,s3c6400-uart",
2663 s3c2440_early_console_setup);
Tomasz Figab94ba032015-01-23 14:47:41 +01002664
2665/* S5PV210, EXYNOS */
2666static struct samsung_early_console_data s5pv210_early_console_data = {
2667 .txfull_mask = S5PV210_UFSTAT_TXFULL,
2668};
2669
2670static int __init s5pv210_early_console_setup(struct earlycon_device *device,
2671 const char *opt)
2672{
2673 device->port.private_data = &s5pv210_early_console_data;
2674 return samsung_early_console_setup(device, opt);
2675}
2676OF_EARLYCON_DECLARE(s5pv210, "samsung,s5pv210-uart",
2677 s5pv210_early_console_setup);
2678OF_EARLYCON_DECLARE(exynos4210, "samsung,exynos4210-uart",
2679 s5pv210_early_console_setup);
Marek Szyprowskic3bda292015-02-02 10:47:35 +01002680#endif
Tomasz Figab94ba032015-01-23 14:47:41 +01002681
Thomas Abrahamda121502011-11-02 19:23:25 +09002682MODULE_ALIAS("platform:samsung-uart");
Ben Dooksb4975492008-07-03 12:32:51 +01002683MODULE_DESCRIPTION("Samsung SoC Serial port driver");
2684MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
2685MODULE_LICENSE("GPL v2");