blob: 39b2eb165bdca7ba9b5437d40ec9d6f60c1a95f6 [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/
Greg Kroah-Hartman7c175252019-12-10 15:37:05 +01007 */
Ben Dooksb4975492008-07-03 12:32:51 +01008
Tamseel Shamsc89511f2020-06-17 16:29:07 +05309/* Note on 2410 error handling
Ben Dooksb4975492008-07-03 12:32:51 +010010 *
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
Greg Kroah-Hartman7c175252019-12-10 15:37:05 +010022 */
Ben Dooksb4975492008-07-03 12:32:51 +010023
Robert Baldyga62c37ee2014-12-10 12:49:25 +010024#include <linux/dmaengine.h>
25#include <linux/dma-mapping.h>
26#include <linux/slab.h>
Ben Dooksb4975492008-07-03 12:32:51 +010027#include <linux/module.h>
28#include <linux/ioport.h>
29#include <linux/io.h>
30#include <linux/platform_device.h>
31#include <linux/init.h>
32#include <linux/sysrq.h>
33#include <linux/console.h>
34#include <linux/tty.h>
35#include <linux/tty_flip.h>
36#include <linux/serial_core.h>
37#include <linux/serial.h>
Arnd Bergmann9ee51f02013-04-11 02:04:48 +020038#include <linux/serial_s3c.h>
Ben Dooksb4975492008-07-03 12:32:51 +010039#include <linux/delay.h>
40#include <linux/clk.h>
Ben Dooks30555472008-10-21 14:06:36 +010041#include <linux/cpufreq.h>
Thomas Abraham26c919e2011-11-06 22:10:44 +053042#include <linux/of.h>
Ben Dooksb4975492008-07-03 12:32:51 +010043#include <asm/irq.h>
44
Ben Dooksb4975492008-07-03 12:32:51 +010045/* UART name and device definitions */
46
47#define S3C24XX_SERIAL_NAME "ttySAC"
48#define S3C24XX_SERIAL_MAJOR 204
49#define S3C24XX_SERIAL_MINOR 64
50
Robert Baldyga29bef792014-12-10 12:49:26 +010051#define S3C24XX_TX_PIO 1
52#define S3C24XX_TX_DMA 2
Robert Baldygab543c302014-12-10 12:49:27 +010053#define S3C24XX_RX_PIO 1
54#define S3C24XX_RX_DMA 2
Ben Dooksb4975492008-07-03 12:32:51 +010055
Lucas De Marchi25985ed2011-03-30 22:57:33 -030056/* flag to ignore all characters coming in */
Ben Dooksb4975492008-07-03 12:32:51 +010057#define RXSTAT_DUMMY_READ (0x10000000)
58
Hector Martinaaf14402021-03-05 06:38:55 +090059enum s3c24xx_port_type {
60 TYPE_S3C24XX,
61 TYPE_S3C6400,
62};
63
Greg Kroah-Hartman43df1702019-12-10 15:37:01 +010064struct s3c24xx_uart_info {
65 char *name;
Hector Martinaaf14402021-03-05 06:38:55 +090066 enum s3c24xx_port_type type;
67 unsigned int port_type;
Greg Kroah-Hartman43df1702019-12-10 15:37:01 +010068 unsigned int fifosize;
69 unsigned long rx_fifomask;
70 unsigned long rx_fifoshift;
71 unsigned long rx_fifofull;
72 unsigned long tx_fifomask;
73 unsigned long tx_fifoshift;
74 unsigned long tx_fifofull;
75 unsigned int def_clk_sel;
76 unsigned long num_clks;
77 unsigned long clksel_mask;
78 unsigned long clksel_shift;
Hector Martin19d48782021-03-05 06:38:54 +090079 unsigned long ucon_mask;
Greg Kroah-Hartman43df1702019-12-10 15:37:01 +010080
81 /* uart port features */
82
83 unsigned int has_divslot:1;
84};
85
86struct s3c24xx_serial_drv_data {
87 struct s3c24xx_uart_info *info;
88 struct s3c2410_uartcfg *def_cfg;
89 unsigned int fifosize[CONFIG_SERIAL_SAMSUNG_UARTS];
90};
91
92struct s3c24xx_uart_dma {
93 unsigned int rx_chan_id;
94 unsigned int tx_chan_id;
95
96 struct dma_slave_config rx_conf;
97 struct dma_slave_config tx_conf;
98
99 struct dma_chan *rx_chan;
100 struct dma_chan *tx_chan;
101
102 dma_addr_t rx_addr;
103 dma_addr_t tx_addr;
104
105 dma_cookie_t rx_cookie;
106 dma_cookie_t tx_cookie;
107
108 char *rx_buf;
109
110 dma_addr_t tx_transfer_addr;
111
112 size_t rx_size;
113 size_t tx_size;
114
115 struct dma_async_tx_descriptor *tx_desc;
116 struct dma_async_tx_descriptor *rx_desc;
117
118 int tx_bytes_requested;
119 int rx_bytes_requested;
120};
121
122struct s3c24xx_uart_port {
123 unsigned char rx_claimed;
124 unsigned char tx_claimed;
Greg Kroah-Hartman83362402019-12-17 15:02:32 +0100125 unsigned char rx_enabled;
126 unsigned char tx_enabled;
Greg Kroah-Hartman43df1702019-12-10 15:37:01 +0100127 unsigned int pm_level;
128 unsigned long baudclk_rate;
129 unsigned int min_dma_size;
130
131 unsigned int rx_irq;
132 unsigned int tx_irq;
133
134 unsigned int tx_in_progress;
135 unsigned int tx_mode;
136 unsigned int rx_mode;
137
138 struct s3c24xx_uart_info *info;
139 struct clk *clk;
140 struct clk *baudclk;
141 struct uart_port port;
142 struct s3c24xx_serial_drv_data *drv_data;
143
144 /* reference to platform data */
145 struct s3c2410_uartcfg *cfg;
146
147 struct s3c24xx_uart_dma *dma;
148
149#ifdef CONFIG_ARM_S3C24XX_CPUFREQ
150 struct notifier_block freq_transition;
151#endif
152};
153
154/* conversion functions */
155
156#define s3c24xx_dev_to_port(__dev) dev_get_drvdata(__dev)
157
158/* register access controls */
159
160#define portaddr(port, reg) ((port)->membase + (reg))
161#define portaddrl(port, reg) \
162 ((unsigned long *)(unsigned long)((port)->membase + (reg)))
163
Hyunki Koo57253cc2020-05-06 17:02:40 +0900164static u32 rd_reg(struct uart_port *port, u32 reg)
165{
166 switch (port->iotype) {
167 case UPIO_MEM:
168 return readb_relaxed(portaddr(port, reg));
169 case UPIO_MEM32:
170 return readl_relaxed(portaddr(port, reg));
171 default:
172 return 0;
173 }
174 return 0;
175}
176
Greg Kroah-Hartman43df1702019-12-10 15:37:01 +0100177#define rd_regl(port, reg) (readl_relaxed(portaddr(port, reg)))
178
Hyunki Koo57253cc2020-05-06 17:02:40 +0900179static void wr_reg(struct uart_port *port, u32 reg, u32 val)
180{
181 switch (port->iotype) {
182 case UPIO_MEM:
183 writeb_relaxed(val, portaddr(port, reg));
184 break;
185 case UPIO_MEM32:
186 writel_relaxed(val, portaddr(port, reg));
187 break;
188 }
189}
190
Greg Kroah-Hartman43df1702019-12-10 15:37:01 +0100191#define wr_regl(port, reg, val) writel_relaxed(val, portaddr(port, reg))
192
193/* Byte-order aware bit setting/clearing functions. */
194
195static inline void s3c24xx_set_bit(struct uart_port *port, int idx,
196 unsigned int reg)
197{
198 unsigned long flags;
199 u32 val;
200
201 local_irq_save(flags);
202 val = rd_regl(port, reg);
203 val |= (1 << idx);
204 wr_regl(port, reg, val);
205 local_irq_restore(flags);
206}
207
208static inline void s3c24xx_clear_bit(struct uart_port *port, int idx,
209 unsigned int reg)
210{
211 unsigned long flags;
212 u32 val;
213
214 local_irq_save(flags);
215 val = rd_regl(port, reg);
216 val &= ~(1 << idx);
217 wr_regl(port, reg, val);
218 local_irq_restore(flags);
219}
220
Ben Dooksb4975492008-07-03 12:32:51 +0100221static inline struct s3c24xx_uart_port *to_ourport(struct uart_port *port)
222{
223 return container_of(port, struct s3c24xx_uart_port, port);
224}
225
226/* translate a port to the device name */
227
228static inline const char *s3c24xx_serial_portname(struct uart_port *port)
229{
230 return to_platform_device(port->dev)->name;
231}
232
233static int s3c24xx_serial_txempty_nofifo(struct uart_port *port)
234{
Sachin Kamat9303ac12012-09-05 10:30:11 +0530235 return rd_regl(port, S3C2410_UTRSTAT) & S3C2410_UTRSTAT_TXE;
Ben Dooksb4975492008-07-03 12:32:51 +0100236}
237
238static void s3c24xx_serial_rx_enable(struct uart_port *port)
239{
Greg Kroah-Hartman83362402019-12-17 15:02:32 +0100240 struct s3c24xx_uart_port *ourport = to_ourport(port);
Ben Dooksb4975492008-07-03 12:32:51 +0100241 unsigned long flags;
242 unsigned int ucon, ufcon;
243 int count = 10000;
244
245 spin_lock_irqsave(&port->lock, flags);
246
247 while (--count && !s3c24xx_serial_txempty_nofifo(port))
248 udelay(100);
249
250 ufcon = rd_regl(port, S3C2410_UFCON);
251 ufcon |= S3C2410_UFCON_RESETRX;
252 wr_regl(port, S3C2410_UFCON, ufcon);
253
254 ucon = rd_regl(port, S3C2410_UCON);
255 ucon |= S3C2410_UCON_RXIRQMODE;
256 wr_regl(port, S3C2410_UCON, ucon);
257
Greg Kroah-Hartman83362402019-12-17 15:02:32 +0100258 ourport->rx_enabled = 1;
Ben Dooksb4975492008-07-03 12:32:51 +0100259 spin_unlock_irqrestore(&port->lock, flags);
260}
261
262static void s3c24xx_serial_rx_disable(struct uart_port *port)
263{
Greg Kroah-Hartman83362402019-12-17 15:02:32 +0100264 struct s3c24xx_uart_port *ourport = to_ourport(port);
Ben Dooksb4975492008-07-03 12:32:51 +0100265 unsigned long flags;
266 unsigned int ucon;
267
268 spin_lock_irqsave(&port->lock, flags);
269
270 ucon = rd_regl(port, S3C2410_UCON);
271 ucon &= ~S3C2410_UCON_RXIRQMODE;
272 wr_regl(port, S3C2410_UCON, ucon);
273
Greg Kroah-Hartman83362402019-12-17 15:02:32 +0100274 ourport->rx_enabled = 0;
Ben Dooksb4975492008-07-03 12:32:51 +0100275 spin_unlock_irqrestore(&port->lock, flags);
276}
277
278static void s3c24xx_serial_stop_tx(struct uart_port *port)
279{
Ben Dooksb73c289c2008-10-21 14:07:04 +0100280 struct s3c24xx_uart_port *ourport = to_ourport(port);
Robert Baldyga29bef792014-12-10 12:49:26 +0100281 struct s3c24xx_uart_dma *dma = ourport->dma;
282 struct circ_buf *xmit = &port->state->xmit;
283 struct dma_tx_state state;
284 int count;
Ben Dooksb73c289c2008-10-21 14:07:04 +0100285
Greg Kroah-Hartman83362402019-12-17 15:02:32 +0100286 if (!ourport->tx_enabled)
Robert Baldyga29bef792014-12-10 12:49:26 +0100287 return;
288
Hector Martinaaf14402021-03-05 06:38:55 +0900289 switch (ourport->info->type) {
290 case TYPE_S3C6400:
Matthew Leachbbb5ff92016-06-22 17:57:03 +0100291 s3c24xx_set_bit(port, S3C64XX_UINTM_TXD, S3C64XX_UINTM);
Hector Martinaaf14402021-03-05 06:38:55 +0900292 break;
293 default:
Robert Baldyga29bef792014-12-10 12:49:26 +0100294 disable_irq_nosync(ourport->tx_irq);
Hector Martinaaf14402021-03-05 06:38:55 +0900295 break;
296 }
Robert Baldyga29bef792014-12-10 12:49:26 +0100297
298 if (dma && dma->tx_chan && ourport->tx_in_progress == S3C24XX_TX_DMA) {
299 dmaengine_pause(dma->tx_chan);
300 dmaengine_tx_status(dma->tx_chan, dma->tx_cookie, &state);
301 dmaengine_terminate_all(dma->tx_chan);
302 dma_sync_single_for_cpu(ourport->port.dev,
303 dma->tx_transfer_addr, dma->tx_size, DMA_TO_DEVICE);
304 async_tx_ack(dma->tx_desc);
305 count = dma->tx_bytes_requested - state.residue;
306 xmit->tail = (xmit->tail + count) & (UART_XMIT_SIZE - 1);
307 port->icount.tx += count;
Ben Dooksb4975492008-07-03 12:32:51 +0100308 }
Robert Baldyga29bef792014-12-10 12:49:26 +0100309
Greg Kroah-Hartman83362402019-12-17 15:02:32 +0100310 ourport->tx_enabled = 0;
Robert Baldyga29bef792014-12-10 12:49:26 +0100311 ourport->tx_in_progress = 0;
312
313 if (port->flags & UPF_CONS_FLOW)
314 s3c24xx_serial_rx_enable(port);
315
316 ourport->tx_mode = 0;
Ben Dooksb4975492008-07-03 12:32:51 +0100317}
318
Robert Baldyga29bef792014-12-10 12:49:26 +0100319static void s3c24xx_serial_start_next_tx(struct s3c24xx_uart_port *ourport);
320
321static void s3c24xx_serial_tx_dma_complete(void *args)
322{
323 struct s3c24xx_uart_port *ourport = args;
324 struct uart_port *port = &ourport->port;
325 struct circ_buf *xmit = &port->state->xmit;
326 struct s3c24xx_uart_dma *dma = ourport->dma;
327 struct dma_tx_state state;
328 unsigned long flags;
329 int count;
330
Robert Baldyga29bef792014-12-10 12:49:26 +0100331 dmaengine_tx_status(dma->tx_chan, dma->tx_cookie, &state);
332 count = dma->tx_bytes_requested - state.residue;
333 async_tx_ack(dma->tx_desc);
334
335 dma_sync_single_for_cpu(ourport->port.dev, dma->tx_transfer_addr,
336 dma->tx_size, DMA_TO_DEVICE);
337
338 spin_lock_irqsave(&port->lock, flags);
339
340 xmit->tail = (xmit->tail + count) & (UART_XMIT_SIZE - 1);
341 port->icount.tx += count;
342 ourport->tx_in_progress = 0;
343
344 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
345 uart_write_wakeup(port);
346
347 s3c24xx_serial_start_next_tx(ourport);
348 spin_unlock_irqrestore(&port->lock, flags);
349}
350
351static void enable_tx_dma(struct s3c24xx_uart_port *ourport)
352{
353 struct uart_port *port = &ourport->port;
354 u32 ucon;
355
356 /* Mask Tx interrupt */
Hector Martinaaf14402021-03-05 06:38:55 +0900357 switch (ourport->info->type) {
358 case TYPE_S3C6400:
Matthew Leachbbb5ff92016-06-22 17:57:03 +0100359 s3c24xx_set_bit(port, S3C64XX_UINTM_TXD, S3C64XX_UINTM);
Hector Martinaaf14402021-03-05 06:38:55 +0900360 break;
361 default:
Robert Baldyga29bef792014-12-10 12:49:26 +0100362 disable_irq_nosync(ourport->tx_irq);
Hector Martinaaf14402021-03-05 06:38:55 +0900363 break;
364 }
Robert Baldyga29bef792014-12-10 12:49:26 +0100365
366 /* Enable tx dma mode */
367 ucon = rd_regl(port, S3C2410_UCON);
368 ucon &= ~(S3C64XX_UCON_TXBURST_MASK | S3C64XX_UCON_TXMODE_MASK);
369 ucon |= (dma_get_cache_alignment() >= 16) ?
370 S3C64XX_UCON_TXBURST_16 : S3C64XX_UCON_TXBURST_1;
371 ucon |= S3C64XX_UCON_TXMODE_DMA;
372 wr_regl(port, S3C2410_UCON, ucon);
373
374 ourport->tx_mode = S3C24XX_TX_DMA;
375}
376
377static void enable_tx_pio(struct s3c24xx_uart_port *ourport)
378{
379 struct uart_port *port = &ourport->port;
380 u32 ucon, ufcon;
381
382 /* Set ufcon txtrig */
383 ourport->tx_in_progress = S3C24XX_TX_PIO;
384 ufcon = rd_regl(port, S3C2410_UFCON);
385 wr_regl(port, S3C2410_UFCON, ufcon);
386
387 /* Enable tx pio mode */
388 ucon = rd_regl(port, S3C2410_UCON);
389 ucon &= ~(S3C64XX_UCON_TXMODE_MASK);
390 ucon |= S3C64XX_UCON_TXMODE_CPU;
391 wr_regl(port, S3C2410_UCON, ucon);
392
393 /* Unmask Tx interrupt */
Hector Martinaaf14402021-03-05 06:38:55 +0900394 switch (ourport->info->type) {
395 case TYPE_S3C6400:
Matthew Leachbbb5ff92016-06-22 17:57:03 +0100396 s3c24xx_clear_bit(port, S3C64XX_UINTM_TXD,
397 S3C64XX_UINTM);
Hector Martinaaf14402021-03-05 06:38:55 +0900398 break;
399 default:
Robert Baldyga29bef792014-12-10 12:49:26 +0100400 enable_irq(ourport->tx_irq);
Hector Martinaaf14402021-03-05 06:38:55 +0900401 break;
402 }
Robert Baldyga29bef792014-12-10 12:49:26 +0100403
404 ourport->tx_mode = S3C24XX_TX_PIO;
405}
406
407static void s3c24xx_serial_start_tx_pio(struct s3c24xx_uart_port *ourport)
408{
409 if (ourport->tx_mode != S3C24XX_TX_PIO)
410 enable_tx_pio(ourport);
411}
412
413static int s3c24xx_serial_start_tx_dma(struct s3c24xx_uart_port *ourport,
414 unsigned int count)
415{
416 struct uart_port *port = &ourport->port;
417 struct circ_buf *xmit = &port->state->xmit;
418 struct s3c24xx_uart_dma *dma = ourport->dma;
419
Robert Baldyga29bef792014-12-10 12:49:26 +0100420 if (ourport->tx_mode != S3C24XX_TX_DMA)
421 enable_tx_dma(ourport);
422
Robert Baldyga29bef792014-12-10 12:49:26 +0100423 dma->tx_size = count & ~(dma_get_cache_alignment() - 1);
424 dma->tx_transfer_addr = dma->tx_addr + xmit->tail;
425
426 dma_sync_single_for_device(ourport->port.dev, dma->tx_transfer_addr,
427 dma->tx_size, DMA_TO_DEVICE);
428
429 dma->tx_desc = dmaengine_prep_slave_single(dma->tx_chan,
430 dma->tx_transfer_addr, dma->tx_size,
431 DMA_MEM_TO_DEV, DMA_PREP_INTERRUPT);
432 if (!dma->tx_desc) {
433 dev_err(ourport->port.dev, "Unable to get desc for Tx\n");
434 return -EIO;
435 }
436
437 dma->tx_desc->callback = s3c24xx_serial_tx_dma_complete;
438 dma->tx_desc->callback_param = ourport;
439 dma->tx_bytes_requested = dma->tx_size;
440
441 ourport->tx_in_progress = S3C24XX_TX_DMA;
442 dma->tx_cookie = dmaengine_submit(dma->tx_desc);
443 dma_async_issue_pending(dma->tx_chan);
444 return 0;
445}
446
447static void s3c24xx_serial_start_next_tx(struct s3c24xx_uart_port *ourport)
448{
449 struct uart_port *port = &ourport->port;
450 struct circ_buf *xmit = &port->state->xmit;
451 unsigned long count;
452
453 /* Get data size up to the end of buffer */
454 count = CIRC_CNT_TO_END(xmit->head, xmit->tail, UART_XMIT_SIZE);
455
456 if (!count) {
457 s3c24xx_serial_stop_tx(port);
458 return;
459 }
460
Marek Szyprowski81ccb2a2015-07-31 10:58:27 +0200461 if (!ourport->dma || !ourport->dma->tx_chan ||
Robert Baldyga736cd792015-07-31 10:58:28 +0200462 count < ourport->min_dma_size ||
463 xmit->tail & (dma_get_cache_alignment() - 1))
Robert Baldyga29bef792014-12-10 12:49:26 +0100464 s3c24xx_serial_start_tx_pio(ourport);
465 else
466 s3c24xx_serial_start_tx_dma(ourport, count);
467}
468
Krzysztof Kozlowski75781972015-05-02 00:40:04 +0900469static void s3c24xx_serial_start_tx(struct uart_port *port)
Ben Dooksb4975492008-07-03 12:32:51 +0100470{
Ben Dooksb73c289c2008-10-21 14:07:04 +0100471 struct s3c24xx_uart_port *ourport = to_ourport(port);
Robert Baldyga29bef792014-12-10 12:49:26 +0100472 struct circ_buf *xmit = &port->state->xmit;
Ben Dooksb73c289c2008-10-21 14:07:04 +0100473
Greg Kroah-Hartman83362402019-12-17 15:02:32 +0100474 if (!ourport->tx_enabled) {
Ben Dooksb4975492008-07-03 12:32:51 +0100475 if (port->flags & UPF_CONS_FLOW)
476 s3c24xx_serial_rx_disable(port);
477
Greg Kroah-Hartman83362402019-12-17 15:02:32 +0100478 ourport->tx_enabled = 1;
Robert Baldygaba019a32015-01-28 14:44:23 +0100479 if (!ourport->dma || !ourport->dma->tx_chan)
Robert Baldyga29bef792014-12-10 12:49:26 +0100480 s3c24xx_serial_start_tx_pio(ourport);
Robert Baldyga29bef792014-12-10 12:49:26 +0100481 }
482
483 if (ourport->dma && ourport->dma->tx_chan) {
484 if (!uart_circ_empty(xmit) && !ourport->tx_in_progress)
485 s3c24xx_serial_start_next_tx(ourport);
Ben Dooksb4975492008-07-03 12:32:51 +0100486 }
487}
488
Robert Baldygab543c302014-12-10 12:49:27 +0100489static void s3c24xx_uart_copy_rx_to_tty(struct s3c24xx_uart_port *ourport,
490 struct tty_port *tty, int count)
491{
492 struct s3c24xx_uart_dma *dma = ourport->dma;
493 int copied;
494
495 if (!count)
496 return;
497
498 dma_sync_single_for_cpu(ourport->port.dev, dma->rx_addr,
499 dma->rx_size, DMA_FROM_DEVICE);
500
501 ourport->port.icount.rx += count;
502 if (!tty) {
503 dev_err(ourport->port.dev, "No tty port\n");
504 return;
505 }
506 copied = tty_insert_flip_string(tty,
507 ((unsigned char *)(ourport->dma->rx_buf)), count);
508 if (copied != count) {
509 WARN_ON(1);
510 dev_err(ourport->port.dev, "RxData copy to tty layer failed\n");
511 }
512}
513
Ben Dooksb4975492008-07-03 12:32:51 +0100514static void s3c24xx_serial_stop_rx(struct uart_port *port)
515{
Ben Dooksb73c289c2008-10-21 14:07:04 +0100516 struct s3c24xx_uart_port *ourport = to_ourport(port);
Robert Baldygab543c302014-12-10 12:49:27 +0100517 struct s3c24xx_uart_dma *dma = ourport->dma;
518 struct tty_port *t = &port->state->port;
519 struct dma_tx_state state;
520 enum dma_status dma_status;
521 unsigned int received;
Ben Dooksb73c289c2008-10-21 14:07:04 +0100522
Greg Kroah-Hartman83362402019-12-17 15:02:32 +0100523 if (ourport->rx_enabled) {
Greg Kroah-Hartmana05025d2019-12-10 15:37:03 +0100524 dev_dbg(port->dev, "stopping rx\n");
Hector Martinaaf14402021-03-05 06:38:55 +0900525 switch (ourport->info->type) {
526 case TYPE_S3C6400:
Matthew Leachbbb5ff92016-06-22 17:57:03 +0100527 s3c24xx_set_bit(port, S3C64XX_UINTM_RXD,
528 S3C64XX_UINTM);
Hector Martinaaf14402021-03-05 06:38:55 +0900529 break;
530 default:
Thomas Abraham88bb4ea2011-08-10 15:51:19 +0530531 disable_irq_nosync(ourport->rx_irq);
Hector Martinaaf14402021-03-05 06:38:55 +0900532 break;
533 }
Greg Kroah-Hartman83362402019-12-17 15:02:32 +0100534 ourport->rx_enabled = 0;
Ben Dooksb4975492008-07-03 12:32:51 +0100535 }
Robert Baldygab543c302014-12-10 12:49:27 +0100536 if (dma && dma->rx_chan) {
537 dmaengine_pause(dma->tx_chan);
538 dma_status = dmaengine_tx_status(dma->rx_chan,
539 dma->rx_cookie, &state);
540 if (dma_status == DMA_IN_PROGRESS ||
541 dma_status == DMA_PAUSED) {
542 received = dma->rx_bytes_requested - state.residue;
543 dmaengine_terminate_all(dma->rx_chan);
544 s3c24xx_uart_copy_rx_to_tty(ourport, t, received);
545 }
546 }
Ben Dooksb4975492008-07-03 12:32:51 +0100547}
548
Robert Baldygaef4aca72014-11-24 07:56:22 +0100549static inline struct s3c24xx_uart_info
550 *s3c24xx_port_to_info(struct uart_port *port)
Ben Dooksb4975492008-07-03 12:32:51 +0100551{
552 return to_ourport(port)->info;
553}
554
Robert Baldygaef4aca72014-11-24 07:56:22 +0100555static inline struct s3c2410_uartcfg
556 *s3c24xx_port_to_cfg(struct uart_port *port)
Ben Dooksb4975492008-07-03 12:32:51 +0100557{
Thomas Abraham4d84e972011-10-24 11:47:25 +0200558 struct s3c24xx_uart_port *ourport;
559
Ben Dooksb4975492008-07-03 12:32:51 +0100560 if (port->dev == NULL)
561 return NULL;
562
Thomas Abraham4d84e972011-10-24 11:47:25 +0200563 ourport = container_of(port, struct s3c24xx_uart_port, port);
564 return ourport->cfg;
Ben Dooksb4975492008-07-03 12:32:51 +0100565}
566
567static int s3c24xx_serial_rx_fifocnt(struct s3c24xx_uart_port *ourport,
568 unsigned long ufstat)
569{
570 struct s3c24xx_uart_info *info = ourport->info;
571
572 if (ufstat & info->rx_fifofull)
Thomas Abrahamda121502011-11-02 19:23:25 +0900573 return ourport->port.fifosize;
Ben Dooksb4975492008-07-03 12:32:51 +0100574
575 return (ufstat & info->rx_fifomask) >> info->rx_fifoshift;
576}
577
Robert Baldygab543c302014-12-10 12:49:27 +0100578static void s3c64xx_start_rx_dma(struct s3c24xx_uart_port *ourport);
579static void s3c24xx_serial_rx_dma_complete(void *args)
580{
581 struct s3c24xx_uart_port *ourport = args;
582 struct uart_port *port = &ourport->port;
583
584 struct s3c24xx_uart_dma *dma = ourport->dma;
585 struct tty_port *t = &port->state->port;
586 struct tty_struct *tty = tty_port_tty_get(&ourport->port.state->port);
587
588 struct dma_tx_state state;
589 unsigned long flags;
590 int received;
591
592 dmaengine_tx_status(dma->rx_chan, dma->rx_cookie, &state);
593 received = dma->rx_bytes_requested - state.residue;
594 async_tx_ack(dma->rx_desc);
595
596 spin_lock_irqsave(&port->lock, flags);
597
598 if (received)
599 s3c24xx_uart_copy_rx_to_tty(ourport, t, received);
600
601 if (tty) {
602 tty_flip_buffer_push(t);
603 tty_kref_put(tty);
604 }
605
606 s3c64xx_start_rx_dma(ourport);
607
608 spin_unlock_irqrestore(&port->lock, flags);
609}
610
611static void s3c64xx_start_rx_dma(struct s3c24xx_uart_port *ourport)
612{
613 struct s3c24xx_uart_dma *dma = ourport->dma;
614
615 dma_sync_single_for_device(ourport->port.dev, dma->rx_addr,
616 dma->rx_size, DMA_FROM_DEVICE);
617
618 dma->rx_desc = dmaengine_prep_slave_single(dma->rx_chan,
619 dma->rx_addr, dma->rx_size, DMA_DEV_TO_MEM,
620 DMA_PREP_INTERRUPT);
621 if (!dma->rx_desc) {
622 dev_err(ourport->port.dev, "Unable to get desc for Rx\n");
623 return;
624 }
625
626 dma->rx_desc->callback = s3c24xx_serial_rx_dma_complete;
627 dma->rx_desc->callback_param = ourport;
628 dma->rx_bytes_requested = dma->rx_size;
629
630 dma->rx_cookie = dmaengine_submit(dma->rx_desc);
631 dma_async_issue_pending(dma->rx_chan);
632}
Ben Dooksb4975492008-07-03 12:32:51 +0100633
634/* ? - where has parity gone?? */
635#define S3C2410_UERSTAT_PARITY (0x1000)
636
Robert Baldygab543c302014-12-10 12:49:27 +0100637static void enable_rx_dma(struct s3c24xx_uart_port *ourport)
638{
639 struct uart_port *port = &ourport->port;
640 unsigned int ucon;
641
642 /* set Rx mode to DMA mode */
643 ucon = rd_regl(port, S3C2410_UCON);
644 ucon &= ~(S3C64XX_UCON_RXBURST_MASK |
645 S3C64XX_UCON_TIMEOUT_MASK |
646 S3C64XX_UCON_EMPTYINT_EN |
647 S3C64XX_UCON_DMASUS_EN |
648 S3C64XX_UCON_TIMEOUT_EN |
649 S3C64XX_UCON_RXMODE_MASK);
650 ucon |= S3C64XX_UCON_RXBURST_16 |
651 0xf << S3C64XX_UCON_TIMEOUT_SHIFT |
652 S3C64XX_UCON_EMPTYINT_EN |
653 S3C64XX_UCON_TIMEOUT_EN |
654 S3C64XX_UCON_RXMODE_DMA;
655 wr_regl(port, S3C2410_UCON, ucon);
656
657 ourport->rx_mode = S3C24XX_RX_DMA;
658}
659
660static void enable_rx_pio(struct s3c24xx_uart_port *ourport)
661{
662 struct uart_port *port = &ourport->port;
663 unsigned int ucon;
664
665 /* set Rx mode to DMA mode */
666 ucon = rd_regl(port, S3C2410_UCON);
667 ucon &= ~(S3C64XX_UCON_TIMEOUT_MASK |
668 S3C64XX_UCON_EMPTYINT_EN |
669 S3C64XX_UCON_DMASUS_EN |
670 S3C64XX_UCON_TIMEOUT_EN |
671 S3C64XX_UCON_RXMODE_MASK);
672 ucon |= 0xf << S3C64XX_UCON_TIMEOUT_SHIFT |
673 S3C64XX_UCON_TIMEOUT_EN |
674 S3C64XX_UCON_RXMODE_CPU;
675 wr_regl(port, S3C2410_UCON, ucon);
676
677 ourport->rx_mode = S3C24XX_RX_PIO;
678}
679
Robert Baldyga09557c02015-09-15 14:49:00 +0200680static void s3c24xx_serial_rx_drain_fifo(struct s3c24xx_uart_port *ourport);
681
Robert Baldygae4678af2015-09-15 14:48:57 +0200682static irqreturn_t s3c24xx_serial_rx_chars_dma(void *dev_id)
Robert Baldygab543c302014-12-10 12:49:27 +0100683{
Chen Wandun98aee0c2019-11-22 20:04:18 +0800684 unsigned int utrstat, received;
Robert Baldygab543c302014-12-10 12:49:27 +0100685 struct s3c24xx_uart_port *ourport = dev_id;
686 struct uart_port *port = &ourport->port;
687 struct s3c24xx_uart_dma *dma = ourport->dma;
688 struct tty_struct *tty = tty_port_tty_get(&ourport->port.state->port);
689 struct tty_port *t = &port->state->port;
690 unsigned long flags;
691 struct dma_tx_state state;
692
693 utrstat = rd_regl(port, S3C2410_UTRSTAT);
Chen Wandun98aee0c2019-11-22 20:04:18 +0800694 rd_regl(port, S3C2410_UFSTAT);
Robert Baldygab543c302014-12-10 12:49:27 +0100695
696 spin_lock_irqsave(&port->lock, flags);
697
698 if (!(utrstat & S3C2410_UTRSTAT_TIMEOUT)) {
699 s3c64xx_start_rx_dma(ourport);
700 if (ourport->rx_mode == S3C24XX_RX_PIO)
701 enable_rx_dma(ourport);
702 goto finish;
703 }
704
705 if (ourport->rx_mode == S3C24XX_RX_DMA) {
706 dmaengine_pause(dma->rx_chan);
707 dmaengine_tx_status(dma->rx_chan, dma->rx_cookie, &state);
708 dmaengine_terminate_all(dma->rx_chan);
709 received = dma->rx_bytes_requested - state.residue;
710 s3c24xx_uart_copy_rx_to_tty(ourport, t, received);
711
712 enable_rx_pio(ourport);
713 }
714
Robert Baldyga09557c02015-09-15 14:49:00 +0200715 s3c24xx_serial_rx_drain_fifo(ourport);
Robert Baldygab543c302014-12-10 12:49:27 +0100716
717 if (tty) {
718 tty_flip_buffer_push(t);
719 tty_kref_put(tty);
720 }
721
722 wr_regl(port, S3C2410_UTRSTAT, S3C2410_UTRSTAT_TIMEOUT);
723
724finish:
725 spin_unlock_irqrestore(&port->lock, flags);
726
727 return IRQ_HANDLED;
728}
729
Robert Baldyga01732dd2015-09-15 14:48:59 +0200730static void s3c24xx_serial_rx_drain_fifo(struct s3c24xx_uart_port *ourport)
Ben Dooksb4975492008-07-03 12:32:51 +0100731{
Ben Dooksb4975492008-07-03 12:32:51 +0100732 struct uart_port *port = &ourport->port;
Ben Dooksb4975492008-07-03 12:32:51 +0100733 unsigned int ufcon, ch, flag, ufstat, uerstat;
Youngmin Namaba06e92016-03-05 19:36:32 +0900734 unsigned int fifocnt = 0;
Robert Baldyga57850a52014-11-24 07:56:24 +0100735 int max_count = port->fifosize;
Ben Dooksb4975492008-07-03 12:32:51 +0100736
737 while (max_count-- > 0) {
Youngmin Namaba06e92016-03-05 19:36:32 +0900738 /*
739 * Receive all characters known to be in FIFO
740 * before reading FIFO level again
741 */
742 if (fifocnt == 0) {
743 ufstat = rd_regl(port, S3C2410_UFSTAT);
744 fifocnt = s3c24xx_serial_rx_fifocnt(ourport, ufstat);
745 if (fifocnt == 0)
746 break;
747 }
748 fifocnt--;
Ben Dooksb4975492008-07-03 12:32:51 +0100749
750 uerstat = rd_regl(port, S3C2410_UERSTAT);
Hyunki Koo8fba6c02020-05-06 17:02:38 +0900751 ch = rd_reg(port, S3C2410_URXH);
Ben Dooksb4975492008-07-03 12:32:51 +0100752
753 if (port->flags & UPF_CONS_FLOW) {
754 int txe = s3c24xx_serial_txempty_nofifo(port);
755
Greg Kroah-Hartman83362402019-12-17 15:02:32 +0100756 if (ourport->rx_enabled) {
Ben Dooksb4975492008-07-03 12:32:51 +0100757 if (!txe) {
Greg Kroah-Hartman83362402019-12-17 15:02:32 +0100758 ourport->rx_enabled = 0;
Ben Dooksb4975492008-07-03 12:32:51 +0100759 continue;
760 }
761 } else {
762 if (txe) {
Youngmin Namaba06e92016-03-05 19:36:32 +0900763 ufcon = rd_regl(port, S3C2410_UFCON);
Ben Dooksb4975492008-07-03 12:32:51 +0100764 ufcon |= S3C2410_UFCON_RESETRX;
765 wr_regl(port, S3C2410_UFCON, ufcon);
Greg Kroah-Hartman83362402019-12-17 15:02:32 +0100766 ourport->rx_enabled = 1;
Robert Baldyga01732dd2015-09-15 14:48:59 +0200767 return;
Ben Dooksb4975492008-07-03 12:32:51 +0100768 }
769 continue;
770 }
771 }
772
773 /* insert the character into the buffer */
774
775 flag = TTY_NORMAL;
776 port->icount.rx++;
777
778 if (unlikely(uerstat & S3C2410_UERSTAT_ANY)) {
Greg Kroah-Hartmana05025d2019-12-10 15:37:03 +0100779 dev_dbg(port->dev,
780 "rxerr: port ch=0x%02x, rxs=0x%08x\n",
781 ch, uerstat);
Ben Dooksb4975492008-07-03 12:32:51 +0100782
783 /* check for break */
784 if (uerstat & S3C2410_UERSTAT_BREAK) {
Greg Kroah-Hartmana05025d2019-12-10 15:37:03 +0100785 dev_dbg(port->dev, "break!\n");
Ben Dooksb4975492008-07-03 12:32:51 +0100786 port->icount.brk++;
787 if (uart_handle_break(port))
Robert Baldyga620bb212015-09-15 14:48:58 +0200788 continue; /* Ignore character */
Ben Dooksb4975492008-07-03 12:32:51 +0100789 }
790
791 if (uerstat & S3C2410_UERSTAT_FRAME)
792 port->icount.frame++;
793 if (uerstat & S3C2410_UERSTAT_OVERRUN)
794 port->icount.overrun++;
795
796 uerstat &= port->read_status_mask;
797
798 if (uerstat & S3C2410_UERSTAT_BREAK)
799 flag = TTY_BREAK;
800 else if (uerstat & S3C2410_UERSTAT_PARITY)
801 flag = TTY_PARITY;
802 else if (uerstat & (S3C2410_UERSTAT_FRAME |
803 S3C2410_UERSTAT_OVERRUN))
804 flag = TTY_FRAME;
805 }
806
807 if (uart_handle_sysrq_char(port, ch))
Robert Baldyga620bb212015-09-15 14:48:58 +0200808 continue; /* Ignore character */
Ben Dooksb4975492008-07-03 12:32:51 +0100809
810 uart_insert_char(port, uerstat, S3C2410_UERSTAT_OVERRUN,
811 ch, flag);
Ben Dooksb4975492008-07-03 12:32:51 +0100812 }
Viresh Kumarf5693ea2013-08-19 20:14:26 +0530813
Jiri Slaby2e124b42013-01-03 15:53:06 +0100814 tty_flip_buffer_push(&port->state->port);
Robert Baldyga01732dd2015-09-15 14:48:59 +0200815}
Ben Dooksb4975492008-07-03 12:32:51 +0100816
Robert Baldyga01732dd2015-09-15 14:48:59 +0200817static irqreturn_t s3c24xx_serial_rx_chars_pio(void *dev_id)
818{
819 struct s3c24xx_uart_port *ourport = dev_id;
820 struct uart_port *port = &ourport->port;
821 unsigned long flags;
822
823 spin_lock_irqsave(&port->lock, flags);
824 s3c24xx_serial_rx_drain_fifo(ourport);
825 spin_unlock_irqrestore(&port->lock, flags);
826
Ben Dooksb4975492008-07-03 12:32:51 +0100827 return IRQ_HANDLED;
828}
829
Robert Baldygab543c302014-12-10 12:49:27 +0100830static irqreturn_t s3c24xx_serial_rx_chars(int irq, void *dev_id)
831{
832 struct s3c24xx_uart_port *ourport = dev_id;
833
834 if (ourport->dma && ourport->dma->rx_chan)
Robert Baldygae4678af2015-09-15 14:48:57 +0200835 return s3c24xx_serial_rx_chars_dma(dev_id);
836 return s3c24xx_serial_rx_chars_pio(dev_id);
Robert Baldygab543c302014-12-10 12:49:27 +0100837}
838
Ben Dooksb4975492008-07-03 12:32:51 +0100839static irqreturn_t s3c24xx_serial_tx_chars(int irq, void *id)
840{
841 struct s3c24xx_uart_port *ourport = id;
842 struct uart_port *port = &ourport->port;
Alan Coxebd2c8f2009-09-19 13:13:28 -0700843 struct circ_buf *xmit = &port->state->xmit;
Thomas Abrahamc15c3742012-11-22 18:06:28 +0530844 unsigned long flags;
Robert Baldyga736cd792015-07-31 10:58:28 +0200845 int count, dma_count = 0;
Ben Dooksb4975492008-07-03 12:32:51 +0100846
Thomas Abrahamc15c3742012-11-22 18:06:28 +0530847 spin_lock_irqsave(&port->lock, flags);
848
Robert Baldyga29bef792014-12-10 12:49:26 +0100849 count = CIRC_CNT_TO_END(xmit->head, xmit->tail, UART_XMIT_SIZE);
850
Marek Szyprowski81ccb2a2015-07-31 10:58:27 +0200851 if (ourport->dma && ourport->dma->tx_chan &&
852 count >= ourport->min_dma_size) {
Robert Baldyga736cd792015-07-31 10:58:28 +0200853 int align = dma_get_cache_alignment() -
854 (xmit->tail & (dma_get_cache_alignment() - 1));
Krzysztof Kozlowski695b8472020-06-17 17:28:56 +0200855 if (count - align >= ourport->min_dma_size) {
856 dma_count = count - align;
Robert Baldyga736cd792015-07-31 10:58:28 +0200857 count = align;
858 }
Robert Baldyga29bef792014-12-10 12:49:26 +0100859 }
860
Ben Dooksb4975492008-07-03 12:32:51 +0100861 if (port->x_char) {
Hyunki Koo8fba6c02020-05-06 17:02:38 +0900862 wr_reg(port, S3C2410_UTXH, port->x_char);
Ben Dooksb4975492008-07-03 12:32:51 +0100863 port->icount.tx++;
864 port->x_char = 0;
865 goto out;
866 }
867
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300868 /* if there isn't anything more to transmit, or the uart is now
Ben Dooksb4975492008-07-03 12:32:51 +0100869 * stopped, disable the uart and exit
Greg Kroah-Hartman7c175252019-12-10 15:37:05 +0100870 */
Ben Dooksb4975492008-07-03 12:32:51 +0100871
872 if (uart_circ_empty(xmit) || uart_tx_stopped(port)) {
873 s3c24xx_serial_stop_tx(port);
874 goto out;
875 }
876
877 /* try and drain the buffer... */
878
Robert Baldyga736cd792015-07-31 10:58:28 +0200879 if (count > port->fifosize) {
880 count = port->fifosize;
881 dma_count = 0;
882 }
883
884 while (!uart_circ_empty(xmit) && count > 0) {
Ben Dooksb4975492008-07-03 12:32:51 +0100885 if (rd_regl(port, S3C2410_UFSTAT) & ourport->info->tx_fifofull)
886 break;
887
Hyunki Koo8fba6c02020-05-06 17:02:38 +0900888 wr_reg(port, S3C2410_UTXH, xmit->buf[xmit->tail]);
Ben Dooksb4975492008-07-03 12:32:51 +0100889 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
890 port->icount.tx++;
Robert Baldyga736cd792015-07-31 10:58:28 +0200891 count--;
892 }
893
894 if (!count && dma_count) {
895 s3c24xx_serial_start_tx_dma(ourport, dma_count);
896 goto out;
Ben Dooksb4975492008-07-03 12:32:51 +0100897 }
898
Thomas Abrahamc15c3742012-11-22 18:06:28 +0530899 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS) {
900 spin_unlock(&port->lock);
Ben Dooksb4975492008-07-03 12:32:51 +0100901 uart_write_wakeup(port);
Thomas Abrahamc15c3742012-11-22 18:06:28 +0530902 spin_lock(&port->lock);
903 }
Ben Dooksb4975492008-07-03 12:32:51 +0100904
905 if (uart_circ_empty(xmit))
906 s3c24xx_serial_stop_tx(port);
907
Robert Baldygaef4aca72014-11-24 07:56:22 +0100908out:
Thomas Abrahamc15c3742012-11-22 18:06:28 +0530909 spin_unlock_irqrestore(&port->lock, flags);
Ben Dooksb4975492008-07-03 12:32:51 +0100910 return IRQ_HANDLED;
911}
912
Thomas Abraham88bb4ea2011-08-10 15:51:19 +0530913/* interrupt handler for s3c64xx and later SoC's.*/
914static irqreturn_t s3c64xx_serial_handle_irq(int irq, void *id)
915{
916 struct s3c24xx_uart_port *ourport = id;
917 struct uart_port *port = &ourport->port;
918 unsigned int pend = rd_regl(port, S3C64XX_UINTP);
Thomas Abraham88bb4ea2011-08-10 15:51:19 +0530919 irqreturn_t ret = IRQ_HANDLED;
920
Thomas Abraham88bb4ea2011-08-10 15:51:19 +0530921 if (pend & S3C64XX_UINTM_RXD_MSK) {
922 ret = s3c24xx_serial_rx_chars(irq, id);
923 wr_regl(port, S3C64XX_UINTP, S3C64XX_UINTM_RXD_MSK);
924 }
925 if (pend & S3C64XX_UINTM_TXD_MSK) {
926 ret = s3c24xx_serial_tx_chars(irq, id);
927 wr_regl(port, S3C64XX_UINTP, S3C64XX_UINTM_TXD_MSK);
928 }
Thomas Abraham88bb4ea2011-08-10 15:51:19 +0530929 return ret;
930}
931
Ben Dooksb4975492008-07-03 12:32:51 +0100932static unsigned int s3c24xx_serial_tx_empty(struct uart_port *port)
933{
934 struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
935 unsigned long ufstat = rd_regl(port, S3C2410_UFSTAT);
936 unsigned long ufcon = rd_regl(port, S3C2410_UFCON);
937
938 if (ufcon & S3C2410_UFCON_FIFOMODE) {
939 if ((ufstat & info->tx_fifomask) != 0 ||
940 (ufstat & info->tx_fifofull))
941 return 0;
942
943 return 1;
944 }
945
946 return s3c24xx_serial_txempty_nofifo(port);
947}
948
949/* no modem control lines */
950static unsigned int s3c24xx_serial_get_mctrl(struct uart_port *port)
951{
Hyunki Koo8fba6c02020-05-06 17:02:38 +0900952 unsigned int umstat = rd_reg(port, S3C2410_UMSTAT);
Ben Dooksb4975492008-07-03 12:32:51 +0100953
954 if (umstat & S3C2410_UMSTAT_CTS)
955 return TIOCM_CAR | TIOCM_DSR | TIOCM_CTS;
956 else
957 return TIOCM_CAR | TIOCM_DSR;
958}
959
960static void s3c24xx_serial_set_mctrl(struct uart_port *port, unsigned int mctrl)
961{
José Miguel Gonçalves2d1e5a42013-09-18 16:52:49 +0100962 unsigned int umcon = rd_regl(port, S3C2410_UMCON);
963
964 if (mctrl & TIOCM_RTS)
965 umcon |= S3C2410_UMCOM_RTS_LOW;
966 else
967 umcon &= ~S3C2410_UMCOM_RTS_LOW;
968
969 wr_regl(port, S3C2410_UMCON, umcon);
Ben Dooksb4975492008-07-03 12:32:51 +0100970}
971
972static void s3c24xx_serial_break_ctl(struct uart_port *port, int break_state)
973{
974 unsigned long flags;
975 unsigned int ucon;
976
977 spin_lock_irqsave(&port->lock, flags);
978
979 ucon = rd_regl(port, S3C2410_UCON);
980
981 if (break_state)
982 ucon |= S3C2410_UCON_SBREAK;
983 else
984 ucon &= ~S3C2410_UCON_SBREAK;
985
986 wr_regl(port, S3C2410_UCON, ucon);
987
988 spin_unlock_irqrestore(&port->lock, flags);
989}
990
Robert Baldyga62c37ee2014-12-10 12:49:25 +0100991static int s3c24xx_serial_request_dma(struct s3c24xx_uart_port *p)
992{
993 struct s3c24xx_uart_dma *dma = p->dma;
Marek Szyprowskid8db8402018-05-17 13:37:14 +0200994 struct dma_slave_caps dma_caps;
995 const char *reason = NULL;
Marek Szyprowski500fcc02017-04-03 08:21:00 +0200996 int ret;
Robert Baldyga62c37ee2014-12-10 12:49:25 +0100997
998 /* Default slave configuration parameters */
999 dma->rx_conf.direction = DMA_DEV_TO_MEM;
1000 dma->rx_conf.src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
1001 dma->rx_conf.src_addr = p->port.mapbase + S3C2410_URXH;
Marek Szyprowskiaa2f80e2018-05-10 08:41:13 +02001002 dma->rx_conf.src_maxburst = 1;
Robert Baldyga62c37ee2014-12-10 12:49:25 +01001003
1004 dma->tx_conf.direction = DMA_MEM_TO_DEV;
1005 dma->tx_conf.dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
1006 dma->tx_conf.dst_addr = p->port.mapbase + S3C2410_UTXH;
Marek Szyprowskiaa2f80e2018-05-10 08:41:13 +02001007 dma->tx_conf.dst_maxburst = 1;
Robert Baldyga62c37ee2014-12-10 12:49:25 +01001008
Marek Szyprowskiba3d6f82016-12-16 10:56:53 +01001009 dma->rx_chan = dma_request_chan(p->port.dev, "rx");
Robert Baldyga62c37ee2014-12-10 12:49:25 +01001010
Marek Szyprowskid8db8402018-05-17 13:37:14 +02001011 if (IS_ERR(dma->rx_chan)) {
1012 reason = "DMA RX channel request failed";
1013 ret = PTR_ERR(dma->rx_chan);
1014 goto err_warn;
1015 }
1016
1017 ret = dma_get_slave_caps(dma->rx_chan, &dma_caps);
1018 if (ret < 0 ||
1019 dma_caps.residue_granularity < DMA_RESIDUE_GRANULARITY_BURST) {
1020 reason = "insufficient DMA RX engine capabilities";
1021 ret = -EOPNOTSUPP;
1022 goto err_release_rx;
1023 }
Robert Baldyga62c37ee2014-12-10 12:49:25 +01001024
1025 dmaengine_slave_config(dma->rx_chan, &dma->rx_conf);
1026
Marek Szyprowskiba3d6f82016-12-16 10:56:53 +01001027 dma->tx_chan = dma_request_chan(p->port.dev, "tx");
1028 if (IS_ERR(dma->tx_chan)) {
Marek Szyprowskid8db8402018-05-17 13:37:14 +02001029 reason = "DMA TX channel request failed";
Marek Szyprowski500fcc02017-04-03 08:21:00 +02001030 ret = PTR_ERR(dma->tx_chan);
1031 goto err_release_rx;
Robert Baldyga62c37ee2014-12-10 12:49:25 +01001032 }
1033
Marek Szyprowskid8db8402018-05-17 13:37:14 +02001034 ret = dma_get_slave_caps(dma->tx_chan, &dma_caps);
1035 if (ret < 0 ||
1036 dma_caps.residue_granularity < DMA_RESIDUE_GRANULARITY_BURST) {
1037 reason = "insufficient DMA TX engine capabilities";
1038 ret = -EOPNOTSUPP;
1039 goto err_release_tx;
1040 }
1041
Robert Baldyga62c37ee2014-12-10 12:49:25 +01001042 dmaengine_slave_config(dma->tx_chan, &dma->tx_conf);
1043
1044 /* RX buffer */
1045 dma->rx_size = PAGE_SIZE;
1046
1047 dma->rx_buf = kmalloc(dma->rx_size, GFP_KERNEL);
Robert Baldyga62c37ee2014-12-10 12:49:25 +01001048 if (!dma->rx_buf) {
Marek Szyprowski500fcc02017-04-03 08:21:00 +02001049 ret = -ENOMEM;
1050 goto err_release_tx;
Robert Baldyga62c37ee2014-12-10 12:49:25 +01001051 }
1052
Marek Szyprowski768d64f2017-04-03 08:20:59 +02001053 dma->rx_addr = dma_map_single(p->port.dev, dma->rx_buf,
Robert Baldyga62c37ee2014-12-10 12:49:25 +01001054 dma->rx_size, DMA_FROM_DEVICE);
Marek Szyprowski500fcc02017-04-03 08:21:00 +02001055 if (dma_mapping_error(p->port.dev, dma->rx_addr)) {
Marek Szyprowskid8db8402018-05-17 13:37:14 +02001056 reason = "DMA mapping error for RX buffer";
Marek Szyprowski500fcc02017-04-03 08:21:00 +02001057 ret = -EIO;
1058 goto err_free_rx;
1059 }
Robert Baldyga62c37ee2014-12-10 12:49:25 +01001060
Robert Baldyga62c37ee2014-12-10 12:49:25 +01001061 /* TX buffer */
Marek Szyprowski768d64f2017-04-03 08:20:59 +02001062 dma->tx_addr = dma_map_single(p->port.dev, p->port.state->xmit.buf,
Robert Baldyga62c37ee2014-12-10 12:49:25 +01001063 UART_XMIT_SIZE, DMA_TO_DEVICE);
Marek Szyprowski500fcc02017-04-03 08:21:00 +02001064 if (dma_mapping_error(p->port.dev, dma->tx_addr)) {
Marek Szyprowskid8db8402018-05-17 13:37:14 +02001065 reason = "DMA mapping error for TX buffer";
Marek Szyprowski500fcc02017-04-03 08:21:00 +02001066 ret = -EIO;
1067 goto err_unmap_rx;
1068 }
Robert Baldyga62c37ee2014-12-10 12:49:25 +01001069
1070 return 0;
Marek Szyprowski500fcc02017-04-03 08:21:00 +02001071
1072err_unmap_rx:
1073 dma_unmap_single(p->port.dev, dma->rx_addr, dma->rx_size,
1074 DMA_FROM_DEVICE);
1075err_free_rx:
1076 kfree(dma->rx_buf);
1077err_release_tx:
1078 dma_release_channel(dma->tx_chan);
1079err_release_rx:
1080 dma_release_channel(dma->rx_chan);
Marek Szyprowskid8db8402018-05-17 13:37:14 +02001081err_warn:
1082 if (reason)
1083 dev_warn(p->port.dev, "%s, DMA will not be used\n", reason);
Marek Szyprowski500fcc02017-04-03 08:21:00 +02001084 return ret;
Robert Baldyga62c37ee2014-12-10 12:49:25 +01001085}
1086
1087static void s3c24xx_serial_release_dma(struct s3c24xx_uart_port *p)
1088{
1089 struct s3c24xx_uart_dma *dma = p->dma;
1090
1091 if (dma->rx_chan) {
1092 dmaengine_terminate_all(dma->rx_chan);
Marek Szyprowski768d64f2017-04-03 08:20:59 +02001093 dma_unmap_single(p->port.dev, dma->rx_addr,
Robert Baldyga62c37ee2014-12-10 12:49:25 +01001094 dma->rx_size, DMA_FROM_DEVICE);
1095 kfree(dma->rx_buf);
1096 dma_release_channel(dma->rx_chan);
1097 dma->rx_chan = NULL;
1098 }
1099
1100 if (dma->tx_chan) {
1101 dmaengine_terminate_all(dma->tx_chan);
Marek Szyprowski768d64f2017-04-03 08:20:59 +02001102 dma_unmap_single(p->port.dev, dma->tx_addr,
Robert Baldyga62c37ee2014-12-10 12:49:25 +01001103 UART_XMIT_SIZE, DMA_TO_DEVICE);
1104 dma_release_channel(dma->tx_chan);
1105 dma->tx_chan = NULL;
1106 }
1107}
1108
Ben Dooksb4975492008-07-03 12:32:51 +01001109static void s3c24xx_serial_shutdown(struct uart_port *port)
1110{
1111 struct s3c24xx_uart_port *ourport = to_ourport(port);
1112
1113 if (ourport->tx_claimed) {
Hector Martin64689162021-03-05 06:38:53 +09001114 free_irq(ourport->tx_irq, ourport);
Greg Kroah-Hartman83362402019-12-17 15:02:32 +01001115 ourport->tx_enabled = 0;
Ben Dooksb4975492008-07-03 12:32:51 +01001116 ourport->tx_claimed = 0;
Javier Martinez Canillase91d8632015-03-13 12:38:51 +01001117 ourport->tx_mode = 0;
Ben Dooksb4975492008-07-03 12:32:51 +01001118 }
1119
1120 if (ourport->rx_claimed) {
Hector Martin64689162021-03-05 06:38:53 +09001121 free_irq(ourport->rx_irq, ourport);
Ben Dooksb4975492008-07-03 12:32:51 +01001122 ourport->rx_claimed = 0;
Greg Kroah-Hartman83362402019-12-17 15:02:32 +01001123 ourport->rx_enabled = 0;
Ben Dooksb4975492008-07-03 12:32:51 +01001124 }
Ben Dooksb4975492008-07-03 12:32:51 +01001125
Hector Martin64689162021-03-05 06:38:53 +09001126 if (ourport->dma)
1127 s3c24xx_serial_release_dma(ourport);
Tomasz Figab6ad2932013-03-26 15:57:35 +01001128
Hector Martin64689162021-03-05 06:38:53 +09001129 ourport->tx_in_progress = 0;
1130}
1131
1132static void s3c64xx_serial_shutdown(struct uart_port *port)
1133{
1134 struct s3c24xx_uart_port *ourport = to_ourport(port);
1135
1136 ourport->tx_enabled = 0;
1137 ourport->tx_mode = 0;
1138 ourport->rx_enabled = 0;
1139
1140 free_irq(port->irq, ourport);
1141
1142 wr_regl(port, S3C64XX_UINTP, 0xf);
1143 wr_regl(port, S3C64XX_UINTM, 0xf);
Robert Baldyga62c37ee2014-12-10 12:49:25 +01001144
1145 if (ourport->dma)
1146 s3c24xx_serial_release_dma(ourport);
1147
Robert Baldyga29bef792014-12-10 12:49:26 +01001148 ourport->tx_in_progress = 0;
Thomas Abraham88bb4ea2011-08-10 15:51:19 +05301149}
Ben Dooksb4975492008-07-03 12:32:51 +01001150
1151static int s3c24xx_serial_startup(struct uart_port *port)
1152{
1153 struct s3c24xx_uart_port *ourport = to_ourport(port);
1154 int ret;
1155
Greg Kroah-Hartman83362402019-12-17 15:02:32 +01001156 ourport->rx_enabled = 1;
Ben Dooksb4975492008-07-03 12:32:51 +01001157
Ben Dooksb73c289c2008-10-21 14:07:04 +01001158 ret = request_irq(ourport->rx_irq, s3c24xx_serial_rx_chars, 0,
Ben Dooksb4975492008-07-03 12:32:51 +01001159 s3c24xx_serial_portname(port), ourport);
1160
1161 if (ret != 0) {
Sachin Kamatd20925e2012-09-05 10:30:10 +05301162 dev_err(port->dev, "cannot get irq %d\n", ourport->rx_irq);
Ben Dooksb4975492008-07-03 12:32:51 +01001163 return ret;
1164 }
1165
1166 ourport->rx_claimed = 1;
1167
Greg Kroah-Hartmana05025d2019-12-10 15:37:03 +01001168 dev_dbg(port->dev, "requesting tx irq...\n");
Ben Dooksb4975492008-07-03 12:32:51 +01001169
Greg Kroah-Hartman83362402019-12-17 15:02:32 +01001170 ourport->tx_enabled = 1;
Ben Dooksb4975492008-07-03 12:32:51 +01001171
Ben Dooksb73c289c2008-10-21 14:07:04 +01001172 ret = request_irq(ourport->tx_irq, s3c24xx_serial_tx_chars, 0,
Ben Dooksb4975492008-07-03 12:32:51 +01001173 s3c24xx_serial_portname(port), ourport);
1174
1175 if (ret) {
Sachin Kamatd20925e2012-09-05 10:30:10 +05301176 dev_err(port->dev, "cannot get irq %d\n", ourport->tx_irq);
Ben Dooksb4975492008-07-03 12:32:51 +01001177 goto err;
1178 }
1179
1180 ourport->tx_claimed = 1;
1181
Ben Dooksb4975492008-07-03 12:32:51 +01001182 /* the port reset code should have done the correct
Greg Kroah-Hartman7c175252019-12-10 15:37:05 +01001183 * register setup for the port controls
1184 */
Ben Dooksb4975492008-07-03 12:32:51 +01001185
1186 return ret;
1187
Robert Baldygaef4aca72014-11-24 07:56:22 +01001188err:
Ben Dooksb4975492008-07-03 12:32:51 +01001189 s3c24xx_serial_shutdown(port);
1190 return ret;
1191}
1192
Thomas Abraham88bb4ea2011-08-10 15:51:19 +05301193static int s3c64xx_serial_startup(struct uart_port *port)
1194{
1195 struct s3c24xx_uart_port *ourport = to_ourport(port);
Robert Baldygab543c302014-12-10 12:49:27 +01001196 unsigned long flags;
1197 unsigned int ufcon;
Thomas Abraham88bb4ea2011-08-10 15:51:19 +05301198 int ret;
1199
Tomasz Figab6ad2932013-03-26 15:57:35 +01001200 wr_regl(port, S3C64XX_UINTM, 0xf);
Robert Baldyga62c37ee2014-12-10 12:49:25 +01001201 if (ourport->dma) {
1202 ret = s3c24xx_serial_request_dma(ourport);
1203 if (ret < 0) {
Krzysztof Kozlowskif98c7bc2017-02-25 18:36:44 +02001204 devm_kfree(port->dev, ourport->dma);
1205 ourport->dma = NULL;
Robert Baldyga62c37ee2014-12-10 12:49:25 +01001206 }
1207 }
Tomasz Figab6ad2932013-03-26 15:57:35 +01001208
Thomas Abraham88bb4ea2011-08-10 15:51:19 +05301209 ret = request_irq(port->irq, s3c64xx_serial_handle_irq, IRQF_SHARED,
1210 s3c24xx_serial_portname(port), ourport);
1211 if (ret) {
Sachin Kamatd20925e2012-09-05 10:30:10 +05301212 dev_err(port->dev, "cannot get irq %d\n", port->irq);
Thomas Abraham88bb4ea2011-08-10 15:51:19 +05301213 return ret;
1214 }
1215
1216 /* For compatibility with s3c24xx Soc's */
Greg Kroah-Hartman83362402019-12-17 15:02:32 +01001217 ourport->rx_enabled = 1;
Greg Kroah-Hartman83362402019-12-17 15:02:32 +01001218 ourport->tx_enabled = 0;
Thomas Abraham88bb4ea2011-08-10 15:51:19 +05301219
Robert Baldyga29bef792014-12-10 12:49:26 +01001220 spin_lock_irqsave(&port->lock, flags);
1221
1222 ufcon = rd_regl(port, S3C2410_UFCON);
Robert Baldyga31c6ba92015-04-17 08:43:09 +02001223 ufcon |= S3C2410_UFCON_RESETRX | S5PV210_UFCON_RXTRIG8;
1224 if (!uart_console(port))
1225 ufcon |= S3C2410_UFCON_RESETTX;
Robert Baldyga29bef792014-12-10 12:49:26 +01001226 wr_regl(port, S3C2410_UFCON, ufcon);
1227
1228 enable_rx_pio(ourport);
1229
1230 spin_unlock_irqrestore(&port->lock, flags);
1231
Thomas Abraham88bb4ea2011-08-10 15:51:19 +05301232 /* Enable Rx Interrupt */
Matthew Leachbbb5ff92016-06-22 17:57:03 +01001233 s3c24xx_clear_bit(port, S3C64XX_UINTM_RXD, S3C64XX_UINTM);
Robert Baldyga29bef792014-12-10 12:49:26 +01001234
Thomas Abraham88bb4ea2011-08-10 15:51:19 +05301235 return ret;
1236}
1237
Ben Dooksb4975492008-07-03 12:32:51 +01001238/* power power management control */
1239
1240static void s3c24xx_serial_pm(struct uart_port *port, unsigned int level,
1241 unsigned int old)
1242{
1243 struct s3c24xx_uart_port *ourport = to_ourport(port);
Robert Baldyga1ff383a2014-11-24 07:56:21 +01001244 int timeout = 10000;
Ben Dooksb4975492008-07-03 12:32:51 +01001245
Ben Dooks30555472008-10-21 14:06:36 +01001246 ourport->pm_level = level;
1247
Ben Dooksb4975492008-07-03 12:32:51 +01001248 switch (level) {
1249 case 3:
Robert Baldyga1ff383a2014-11-24 07:56:21 +01001250 while (--timeout && !s3c24xx_serial_txempty_nofifo(port))
1251 udelay(100);
1252
Kyoungil Kim7cd88832012-05-20 17:45:54 +09001253 if (!IS_ERR(ourport->baudclk))
Thomas Abraham9484b002012-10-03 07:40:04 +09001254 clk_disable_unprepare(ourport->baudclk);
Ben Dooksb4975492008-07-03 12:32:51 +01001255
Thomas Abraham9484b002012-10-03 07:40:04 +09001256 clk_disable_unprepare(ourport->clk);
Ben Dooksb4975492008-07-03 12:32:51 +01001257 break;
1258
1259 case 0:
Thomas Abraham9484b002012-10-03 07:40:04 +09001260 clk_prepare_enable(ourport->clk);
Ben Dooksb4975492008-07-03 12:32:51 +01001261
Kyoungil Kim7cd88832012-05-20 17:45:54 +09001262 if (!IS_ERR(ourport->baudclk))
Thomas Abraham9484b002012-10-03 07:40:04 +09001263 clk_prepare_enable(ourport->baudclk);
Ben Dooksb4975492008-07-03 12:32:51 +01001264
1265 break;
1266 default:
Sachin Kamatd20925e2012-09-05 10:30:10 +05301267 dev_err(port->dev, "s3c24xx_serial: unknown pm %d\n", level);
Ben Dooksb4975492008-07-03 12:32:51 +01001268 }
1269}
1270
1271/* baud rate calculation
1272 *
1273 * The UARTs on the S3C2410/S3C2440 can take their clocks from a number
1274 * of different sources, including the peripheral clock ("pclk") and an
1275 * external clock ("uclk"). The S3C2440 also adds the core clock ("fclk")
1276 * with a programmable extra divisor.
1277 *
1278 * The following code goes through the clock sources, and calculates the
1279 * baud clocks (and the resultant actual baud rates) and then tries to
1280 * pick the closest one and select that.
1281 *
Greg Kroah-Hartman7c175252019-12-10 15:37:05 +01001282 */
Ben Dooksb4975492008-07-03 12:32:51 +01001283
Thomas Abraham5f5a7a52011-10-24 11:47:46 +02001284#define MAX_CLK_NAME_LENGTH 15
Ben Dooksb4975492008-07-03 12:32:51 +01001285
Thomas Abraham5f5a7a52011-10-24 11:47:46 +02001286static inline int s3c24xx_serial_getsource(struct uart_port *port)
Ben Dooksb4975492008-07-03 12:32:51 +01001287{
1288 struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
Thomas Abraham5f5a7a52011-10-24 11:47:46 +02001289 unsigned int ucon;
Ben Dooksb4975492008-07-03 12:32:51 +01001290
Thomas Abraham5f5a7a52011-10-24 11:47:46 +02001291 if (info->num_clks == 1)
Ben Dooksb4975492008-07-03 12:32:51 +01001292 return 0;
1293
Thomas Abraham5f5a7a52011-10-24 11:47:46 +02001294 ucon = rd_regl(port, S3C2410_UCON);
1295 ucon &= info->clksel_mask;
1296 return ucon >> info->clksel_shift;
Ben Dooksb4975492008-07-03 12:32:51 +01001297}
1298
Thomas Abraham5f5a7a52011-10-24 11:47:46 +02001299static void s3c24xx_serial_setsource(struct uart_port *port,
1300 unsigned int clk_sel)
Ben Dooksb4975492008-07-03 12:32:51 +01001301{
Thomas Abraham5f5a7a52011-10-24 11:47:46 +02001302 struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
1303 unsigned int ucon;
Ben Dooksb4975492008-07-03 12:32:51 +01001304
Thomas Abraham5f5a7a52011-10-24 11:47:46 +02001305 if (info->num_clks == 1)
1306 return;
Ben Dooksb4975492008-07-03 12:32:51 +01001307
Thomas Abraham5f5a7a52011-10-24 11:47:46 +02001308 ucon = rd_regl(port, S3C2410_UCON);
1309 if ((ucon & info->clksel_mask) >> info->clksel_shift == clk_sel)
1310 return;
Ben Dooksb4975492008-07-03 12:32:51 +01001311
Thomas Abraham5f5a7a52011-10-24 11:47:46 +02001312 ucon &= ~info->clksel_mask;
1313 ucon |= clk_sel << info->clksel_shift;
1314 wr_regl(port, S3C2410_UCON, ucon);
1315}
Ben Dooksb4975492008-07-03 12:32:51 +01001316
Thomas Abraham5f5a7a52011-10-24 11:47:46 +02001317static unsigned int s3c24xx_serial_getclk(struct s3c24xx_uart_port *ourport,
1318 unsigned int req_baud, struct clk **best_clk,
1319 unsigned int *clk_num)
1320{
1321 struct s3c24xx_uart_info *info = ourport->info;
1322 struct clk *clk;
1323 unsigned long rate;
Jonathan Bakker7d316762020-05-08 18:34:33 -07001324 unsigned int cnt, baud, quot, best_quot = 0;
Thomas Abraham5f5a7a52011-10-24 11:47:46 +02001325 char clkname[MAX_CLK_NAME_LENGTH];
1326 int calc_deviation, deviation = (1 << 30) - 1;
1327
Thomas Abraham5f5a7a52011-10-24 11:47:46 +02001328 for (cnt = 0; cnt < info->num_clks; cnt++) {
Jonathan Bakker7d316762020-05-08 18:34:33 -07001329 /* Keep selected clock if provided */
1330 if (ourport->cfg->clk_sel &&
1331 !(ourport->cfg->clk_sel & (1 << cnt)))
Thomas Abraham5f5a7a52011-10-24 11:47:46 +02001332 continue;
1333
1334 sprintf(clkname, "clk_uart_baud%d", cnt);
1335 clk = clk_get(ourport->port.dev, clkname);
Kyoungil Kim7cd88832012-05-20 17:45:54 +09001336 if (IS_ERR(clk))
Thomas Abraham5f5a7a52011-10-24 11:47:46 +02001337 continue;
1338
1339 rate = clk_get_rate(clk);
1340 if (!rate)
1341 continue;
1342
1343 if (ourport->info->has_divslot) {
1344 unsigned long div = rate / req_baud;
1345
1346 /* The UDIVSLOT register on the newer UARTs allows us to
1347 * get a divisor adjustment of 1/16th on the baud clock.
1348 *
1349 * We don't keep the UDIVSLOT value (the 16ths we
1350 * calculated by not multiplying the baud by 16) as it
1351 * is easy enough to recalculate.
1352 */
1353
1354 quot = div / 16;
1355 baud = rate / div;
1356 } else {
1357 quot = (rate + (8 * req_baud)) / (16 * req_baud);
1358 baud = rate / (quot * 16);
1359 }
1360 quot--;
1361
1362 calc_deviation = req_baud - baud;
1363 if (calc_deviation < 0)
1364 calc_deviation = -calc_deviation;
1365
1366 if (calc_deviation < deviation) {
1367 *best_clk = clk;
1368 best_quot = quot;
1369 *clk_num = cnt;
1370 deviation = calc_deviation;
Ben Dooksb4975492008-07-03 12:32:51 +01001371 }
1372 }
1373
Thomas Abraham5f5a7a52011-10-24 11:47:46 +02001374 return best_quot;
Ben Dooksb4975492008-07-03 12:32:51 +01001375}
1376
Ben Dooks090f848d2008-12-12 00:24:21 +00001377/* udivslot_table[]
1378 *
1379 * This table takes the fractional value of the baud divisor and gives
1380 * the recommended setting for the UDIVSLOT register.
1381 */
1382static u16 udivslot_table[16] = {
1383 [0] = 0x0000,
1384 [1] = 0x0080,
1385 [2] = 0x0808,
1386 [3] = 0x0888,
1387 [4] = 0x2222,
1388 [5] = 0x4924,
1389 [6] = 0x4A52,
1390 [7] = 0x54AA,
1391 [8] = 0x5555,
1392 [9] = 0xD555,
1393 [10] = 0xD5D5,
1394 [11] = 0xDDD5,
1395 [12] = 0xDDDD,
1396 [13] = 0xDFDD,
1397 [14] = 0xDFDF,
1398 [15] = 0xFFDF,
1399};
1400
Ben Dooksb4975492008-07-03 12:32:51 +01001401static void s3c24xx_serial_set_termios(struct uart_port *port,
1402 struct ktermios *termios,
1403 struct ktermios *old)
1404{
1405 struct s3c2410_uartcfg *cfg = s3c24xx_port_to_cfg(port);
1406 struct s3c24xx_uart_port *ourport = to_ourport(port);
Kyoungil Kim7cd88832012-05-20 17:45:54 +09001407 struct clk *clk = ERR_PTR(-EINVAL);
Ben Dooksb4975492008-07-03 12:32:51 +01001408 unsigned long flags;
Thomas Abraham5f5a7a52011-10-24 11:47:46 +02001409 unsigned int baud, quot, clk_sel = 0;
Ben Dooksb4975492008-07-03 12:32:51 +01001410 unsigned int ulcon;
1411 unsigned int umcon;
Ben Dooks090f848d2008-12-12 00:24:21 +00001412 unsigned int udivslot = 0;
Ben Dooksb4975492008-07-03 12:32:51 +01001413
1414 /*
1415 * We don't support modem control lines.
1416 */
1417 termios->c_cflag &= ~(HUPCL | CMSPAR);
1418 termios->c_cflag |= CLOCAL;
1419
1420 /*
1421 * Ask the core to calculate the divisor for us.
1422 */
1423
Seung-Woo Kimec18f482018-12-14 12:34:09 +01001424 baud = uart_get_baud_rate(port, termios, old, 0, 3000000);
Thomas Abraham5f5a7a52011-10-24 11:47:46 +02001425 quot = s3c24xx_serial_getclk(ourport, baud, &clk, &clk_sel);
Ben Dooksb4975492008-07-03 12:32:51 +01001426 if (baud == 38400 && (port->flags & UPF_SPD_MASK) == UPF_SPD_CUST)
1427 quot = port->custom_divisor;
Kyoungil Kim7cd88832012-05-20 17:45:54 +09001428 if (IS_ERR(clk))
Thomas Abraham5f5a7a52011-10-24 11:47:46 +02001429 return;
Ben Dooksb4975492008-07-03 12:32:51 +01001430
1431 /* check to see if we need to change clock source */
1432
Thomas Abraham5f5a7a52011-10-24 11:47:46 +02001433 if (ourport->baudclk != clk) {
Chanwoo Choib8995f52016-04-21 18:58:31 +09001434 clk_prepare_enable(clk);
1435
Thomas Abraham5f5a7a52011-10-24 11:47:46 +02001436 s3c24xx_serial_setsource(port, clk_sel);
Ben Dooksb4975492008-07-03 12:32:51 +01001437
Kyoungil Kim7cd88832012-05-20 17:45:54 +09001438 if (!IS_ERR(ourport->baudclk)) {
Thomas Abraham9484b002012-10-03 07:40:04 +09001439 clk_disable_unprepare(ourport->baudclk);
Kyoungil Kim7cd88832012-05-20 17:45:54 +09001440 ourport->baudclk = ERR_PTR(-EINVAL);
Ben Dooksb4975492008-07-03 12:32:51 +01001441 }
1442
Ben Dooksb4975492008-07-03 12:32:51 +01001443 ourport->baudclk = clk;
Ben Dooks30555472008-10-21 14:06:36 +01001444 ourport->baudclk_rate = clk ? clk_get_rate(clk) : 0;
Ben Dooksb4975492008-07-03 12:32:51 +01001445 }
1446
Ben Dooks090f848d2008-12-12 00:24:21 +00001447 if (ourport->info->has_divslot) {
1448 unsigned int div = ourport->baudclk_rate / baud;
1449
Jongpill Lee8b526ae2010-07-16 10:19:41 +09001450 if (cfg->has_fracval) {
1451 udivslot = (div & 15);
Greg Kroah-Hartmana05025d2019-12-10 15:37:03 +01001452 dev_dbg(port->dev, "fracval = %04x\n", udivslot);
Jongpill Lee8b526ae2010-07-16 10:19:41 +09001453 } else {
1454 udivslot = udivslot_table[div & 15];
Greg Kroah-Hartmana05025d2019-12-10 15:37:03 +01001455 dev_dbg(port->dev, "udivslot = %04x (div %d)\n",
1456 udivslot, div & 15);
Jongpill Lee8b526ae2010-07-16 10:19:41 +09001457 }
Ben Dooks090f848d2008-12-12 00:24:21 +00001458 }
1459
Ben Dooksb4975492008-07-03 12:32:51 +01001460 switch (termios->c_cflag & CSIZE) {
1461 case CS5:
Greg Kroah-Hartmana05025d2019-12-10 15:37:03 +01001462 dev_dbg(port->dev, "config: 5bits/char\n");
Ben Dooksb4975492008-07-03 12:32:51 +01001463 ulcon = S3C2410_LCON_CS5;
1464 break;
1465 case CS6:
Greg Kroah-Hartmana05025d2019-12-10 15:37:03 +01001466 dev_dbg(port->dev, "config: 6bits/char\n");
Ben Dooksb4975492008-07-03 12:32:51 +01001467 ulcon = S3C2410_LCON_CS6;
1468 break;
1469 case CS7:
Greg Kroah-Hartmana05025d2019-12-10 15:37:03 +01001470 dev_dbg(port->dev, "config: 7bits/char\n");
Ben Dooksb4975492008-07-03 12:32:51 +01001471 ulcon = S3C2410_LCON_CS7;
1472 break;
1473 case CS8:
1474 default:
Greg Kroah-Hartmana05025d2019-12-10 15:37:03 +01001475 dev_dbg(port->dev, "config: 8bits/char\n");
Ben Dooksb4975492008-07-03 12:32:51 +01001476 ulcon = S3C2410_LCON_CS8;
1477 break;
1478 }
1479
1480 /* preserve original lcon IR settings */
1481 ulcon |= (cfg->ulcon & S3C2410_LCON_IRM);
1482
1483 if (termios->c_cflag & CSTOPB)
1484 ulcon |= S3C2410_LCON_STOPB;
1485
Ben Dooksb4975492008-07-03 12:32:51 +01001486 if (termios->c_cflag & PARENB) {
1487 if (termios->c_cflag & PARODD)
1488 ulcon |= S3C2410_LCON_PODD;
1489 else
1490 ulcon |= S3C2410_LCON_PEVEN;
1491 } else {
1492 ulcon |= S3C2410_LCON_PNONE;
1493 }
1494
1495 spin_lock_irqsave(&port->lock, flags);
1496
Greg Kroah-Hartmana05025d2019-12-10 15:37:03 +01001497 dev_dbg(port->dev,
1498 "setting ulcon to %08x, brddiv to %d, udivslot %08x\n",
1499 ulcon, quot, udivslot);
Ben Dooksb4975492008-07-03 12:32:51 +01001500
1501 wr_regl(port, S3C2410_ULCON, ulcon);
1502 wr_regl(port, S3C2410_UBRDIV, quot);
José Miguel Gonçalves2d1e5a42013-09-18 16:52:49 +01001503
Beomho Seo31e93362018-12-14 12:34:08 +01001504 port->status &= ~UPSTAT_AUTOCTS;
1505
José Miguel Gonçalves2d1e5a42013-09-18 16:52:49 +01001506 umcon = rd_regl(port, S3C2410_UMCON);
1507 if (termios->c_cflag & CRTSCTS) {
1508 umcon |= S3C2410_UMCOM_AFC;
1509 /* Disable RTS when RX FIFO contains 63 bytes */
1510 umcon &= ~S3C2412_UMCON_AFC_8;
Beomho Seo31e93362018-12-14 12:34:08 +01001511 port->status = UPSTAT_AUTOCTS;
José Miguel Gonçalves2d1e5a42013-09-18 16:52:49 +01001512 } else {
1513 umcon &= ~S3C2410_UMCOM_AFC;
1514 }
Ben Dooksb4975492008-07-03 12:32:51 +01001515 wr_regl(port, S3C2410_UMCON, umcon);
1516
Ben Dooks090f848d2008-12-12 00:24:21 +00001517 if (ourport->info->has_divslot)
1518 wr_regl(port, S3C2443_DIVSLOT, udivslot);
1519
Greg Kroah-Hartmana05025d2019-12-10 15:37:03 +01001520 dev_dbg(port->dev,
1521 "uart: ulcon = 0x%08x, ucon = 0x%08x, ufcon = 0x%08x\n",
1522 rd_regl(port, S3C2410_ULCON),
1523 rd_regl(port, S3C2410_UCON),
1524 rd_regl(port, S3C2410_UFCON));
Ben Dooksb4975492008-07-03 12:32:51 +01001525
1526 /*
1527 * Update the per-port timeout.
1528 */
1529 uart_update_timeout(port, termios->c_cflag, baud);
1530
1531 /*
1532 * Which character status flags are we interested in?
1533 */
1534 port->read_status_mask = S3C2410_UERSTAT_OVERRUN;
1535 if (termios->c_iflag & INPCK)
Robert Baldygaef4aca72014-11-24 07:56:22 +01001536 port->read_status_mask |= S3C2410_UERSTAT_FRAME |
1537 S3C2410_UERSTAT_PARITY;
Ben Dooksb4975492008-07-03 12:32:51 +01001538 /*
1539 * Which character status flags should we ignore?
1540 */
1541 port->ignore_status_mask = 0;
1542 if (termios->c_iflag & IGNPAR)
1543 port->ignore_status_mask |= S3C2410_UERSTAT_OVERRUN;
1544 if (termios->c_iflag & IGNBRK && termios->c_iflag & IGNPAR)
1545 port->ignore_status_mask |= S3C2410_UERSTAT_FRAME;
1546
1547 /*
1548 * Ignore all characters if CREAD is not set.
1549 */
1550 if ((termios->c_cflag & CREAD) == 0)
1551 port->ignore_status_mask |= RXSTAT_DUMMY_READ;
1552
1553 spin_unlock_irqrestore(&port->lock, flags);
1554}
1555
1556static const char *s3c24xx_serial_type(struct uart_port *port)
1557{
Hector Martinaaf14402021-03-05 06:38:55 +09001558 struct s3c24xx_uart_port *ourport = to_ourport(port);
1559
1560 switch (ourport->info->type) {
1561 case TYPE_S3C24XX:
1562 return "S3C24XX";
1563 case TYPE_S3C6400:
Ben Dooksb690ace2008-10-21 14:07:03 +01001564 return "S3C6400/10";
Ben Dooksb4975492008-07-03 12:32:51 +01001565 default:
1566 return NULL;
1567 }
1568}
1569
1570#define MAP_SIZE (0x100)
1571
1572static void s3c24xx_serial_release_port(struct uart_port *port)
1573{
1574 release_mem_region(port->mapbase, MAP_SIZE);
1575}
1576
1577static int s3c24xx_serial_request_port(struct uart_port *port)
1578{
1579 const char *name = s3c24xx_serial_portname(port);
Greg Kroah-Hartman9fe0d412019-12-10 15:37:06 +01001580
Ben Dooksb4975492008-07-03 12:32:51 +01001581 return request_mem_region(port->mapbase, MAP_SIZE, name) ? 0 : -EBUSY;
1582}
1583
1584static void s3c24xx_serial_config_port(struct uart_port *port, int flags)
1585{
1586 struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
1587
1588 if (flags & UART_CONFIG_TYPE &&
1589 s3c24xx_serial_request_port(port) == 0)
Hector Martinaaf14402021-03-05 06:38:55 +09001590 port->type = info->port_type;
Ben Dooksb4975492008-07-03 12:32:51 +01001591}
1592
1593/*
1594 * verify the new serial_struct (for TIOCSSERIAL).
1595 */
1596static int
1597s3c24xx_serial_verify_port(struct uart_port *port, struct serial_struct *ser)
1598{
1599 struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
1600
Hector Martinaaf14402021-03-05 06:38:55 +09001601 if (ser->type != PORT_UNKNOWN && ser->type != info->port_type)
Ben Dooksb4975492008-07-03 12:32:51 +01001602 return -EINVAL;
1603
1604 return 0;
1605}
1606
Ben Dooksb4975492008-07-03 12:32:51 +01001607#ifdef CONFIG_SERIAL_SAMSUNG_CONSOLE
1608
1609static struct console s3c24xx_serial_console;
1610
Julien Pichon93b5c032012-09-21 23:22:31 -07001611static int __init s3c24xx_serial_console_init(void)
1612{
1613 register_console(&s3c24xx_serial_console);
1614 return 0;
1615}
1616console_initcall(s3c24xx_serial_console_init);
1617
Ben Dooksb4975492008-07-03 12:32:51 +01001618#define S3C24XX_SERIAL_CONSOLE &s3c24xx_serial_console
1619#else
1620#define S3C24XX_SERIAL_CONSOLE NULL
1621#endif
1622
Arnd Bergmann84f57d92013-04-11 02:04:49 +02001623#if defined(CONFIG_SERIAL_SAMSUNG_CONSOLE) && defined(CONFIG_CONSOLE_POLL)
Julien Pichon93b5c032012-09-21 23:22:31 -07001624static int s3c24xx_serial_get_poll_char(struct uart_port *port);
1625static void s3c24xx_serial_put_poll_char(struct uart_port *port,
1626 unsigned char c);
1627#endif
1628
Hector Martin64689162021-03-05 06:38:53 +09001629static const struct uart_ops s3c24xx_serial_ops = {
Ben Dooksb4975492008-07-03 12:32:51 +01001630 .pm = s3c24xx_serial_pm,
1631 .tx_empty = s3c24xx_serial_tx_empty,
1632 .get_mctrl = s3c24xx_serial_get_mctrl,
1633 .set_mctrl = s3c24xx_serial_set_mctrl,
1634 .stop_tx = s3c24xx_serial_stop_tx,
1635 .start_tx = s3c24xx_serial_start_tx,
1636 .stop_rx = s3c24xx_serial_stop_rx,
Ben Dooksb4975492008-07-03 12:32:51 +01001637 .break_ctl = s3c24xx_serial_break_ctl,
1638 .startup = s3c24xx_serial_startup,
1639 .shutdown = s3c24xx_serial_shutdown,
1640 .set_termios = s3c24xx_serial_set_termios,
1641 .type = s3c24xx_serial_type,
1642 .release_port = s3c24xx_serial_release_port,
1643 .request_port = s3c24xx_serial_request_port,
1644 .config_port = s3c24xx_serial_config_port,
1645 .verify_port = s3c24xx_serial_verify_port,
Arnd Bergmann84f57d92013-04-11 02:04:49 +02001646#if defined(CONFIG_SERIAL_SAMSUNG_CONSOLE) && defined(CONFIG_CONSOLE_POLL)
Julien Pichon93b5c032012-09-21 23:22:31 -07001647 .poll_get_char = s3c24xx_serial_get_poll_char,
1648 .poll_put_char = s3c24xx_serial_put_poll_char,
1649#endif
Ben Dooksb4975492008-07-03 12:32:51 +01001650};
1651
Hector Martin64689162021-03-05 06:38:53 +09001652static const struct uart_ops s3c64xx_serial_ops = {
1653 .pm = s3c24xx_serial_pm,
1654 .tx_empty = s3c24xx_serial_tx_empty,
1655 .get_mctrl = s3c24xx_serial_get_mctrl,
1656 .set_mctrl = s3c24xx_serial_set_mctrl,
1657 .stop_tx = s3c24xx_serial_stop_tx,
1658 .start_tx = s3c24xx_serial_start_tx,
1659 .stop_rx = s3c24xx_serial_stop_rx,
1660 .break_ctl = s3c24xx_serial_break_ctl,
1661 .startup = s3c64xx_serial_startup,
1662 .shutdown = s3c64xx_serial_shutdown,
1663 .set_termios = s3c24xx_serial_set_termios,
1664 .type = s3c24xx_serial_type,
1665 .release_port = s3c24xx_serial_release_port,
1666 .request_port = s3c24xx_serial_request_port,
1667 .config_port = s3c24xx_serial_config_port,
1668 .verify_port = s3c24xx_serial_verify_port,
1669#if defined(CONFIG_SERIAL_SAMSUNG_CONSOLE) && defined(CONFIG_CONSOLE_POLL)
1670 .poll_get_char = s3c24xx_serial_get_poll_char,
1671 .poll_put_char = s3c24xx_serial_put_poll_char,
1672#endif
1673};
1674
Ben Dooksb4975492008-07-03 12:32:51 +01001675static struct uart_driver s3c24xx_uart_drv = {
1676 .owner = THIS_MODULE,
Darius Augulis2cf0c582011-01-12 14:50:51 +09001677 .driver_name = "s3c2410_serial",
Ben Dooksbdd49152008-11-03 19:51:42 +00001678 .nr = CONFIG_SERIAL_SAMSUNG_UARTS,
Ben Dooksb4975492008-07-03 12:32:51 +01001679 .cons = S3C24XX_SERIAL_CONSOLE,
Darius Augulis2cf0c582011-01-12 14:50:51 +09001680 .dev_name = S3C24XX_SERIAL_NAME,
Ben Dooksb4975492008-07-03 12:32:51 +01001681 .major = S3C24XX_SERIAL_MAJOR,
1682 .minor = S3C24XX_SERIAL_MINOR,
1683};
1684
Robert Baldygaef4aca72014-11-24 07:56:22 +01001685#define __PORT_LOCK_UNLOCKED(i) \
1686 __SPIN_LOCK_UNLOCKED(s3c24xx_serial_ports[i].port.lock)
1687static struct s3c24xx_uart_port
1688s3c24xx_serial_ports[CONFIG_SERIAL_SAMSUNG_UARTS] = {
Ben Dooksb4975492008-07-03 12:32:51 +01001689 [0] = {
1690 .port = {
Robert Baldygaef4aca72014-11-24 07:56:22 +01001691 .lock = __PORT_LOCK_UNLOCKED(0),
Ben Dooksb4975492008-07-03 12:32:51 +01001692 .iotype = UPIO_MEM,
Ben Dooksb4975492008-07-03 12:32:51 +01001693 .uartclk = 0,
1694 .fifosize = 16,
1695 .ops = &s3c24xx_serial_ops,
1696 .flags = UPF_BOOT_AUTOCONF,
1697 .line = 0,
1698 }
1699 },
1700 [1] = {
1701 .port = {
Robert Baldygaef4aca72014-11-24 07:56:22 +01001702 .lock = __PORT_LOCK_UNLOCKED(1),
Ben Dooksb4975492008-07-03 12:32:51 +01001703 .iotype = UPIO_MEM,
Ben Dooksb4975492008-07-03 12:32:51 +01001704 .uartclk = 0,
1705 .fifosize = 16,
1706 .ops = &s3c24xx_serial_ops,
1707 .flags = UPF_BOOT_AUTOCONF,
1708 .line = 1,
1709 }
1710 },
Ben Dooks03d5e772008-11-03 09:21:23 +00001711#if CONFIG_SERIAL_SAMSUNG_UARTS > 2
Ben Dooksb4975492008-07-03 12:32:51 +01001712 [2] = {
1713 .port = {
Robert Baldygaef4aca72014-11-24 07:56:22 +01001714 .lock = __PORT_LOCK_UNLOCKED(2),
Ben Dooksb4975492008-07-03 12:32:51 +01001715 .iotype = UPIO_MEM,
Ben Dooksb4975492008-07-03 12:32:51 +01001716 .uartclk = 0,
1717 .fifosize = 16,
1718 .ops = &s3c24xx_serial_ops,
1719 .flags = UPF_BOOT_AUTOCONF,
1720 .line = 2,
1721 }
Ben Dooks03d5e772008-11-03 09:21:23 +00001722 },
1723#endif
1724#if CONFIG_SERIAL_SAMSUNG_UARTS > 3
1725 [3] = {
1726 .port = {
Robert Baldygaef4aca72014-11-24 07:56:22 +01001727 .lock = __PORT_LOCK_UNLOCKED(3),
Ben Dooks03d5e772008-11-03 09:21:23 +00001728 .iotype = UPIO_MEM,
Ben Dooks03d5e772008-11-03 09:21:23 +00001729 .uartclk = 0,
1730 .fifosize = 16,
1731 .ops = &s3c24xx_serial_ops,
1732 .flags = UPF_BOOT_AUTOCONF,
1733 .line = 3,
1734 }
Ben Dooksb4975492008-07-03 12:32:51 +01001735 }
1736#endif
1737};
Robert Baldygaef4aca72014-11-24 07:56:22 +01001738#undef __PORT_LOCK_UNLOCKED
Ben Dooksb4975492008-07-03 12:32:51 +01001739
1740/* s3c24xx_serial_resetport
1741 *
Thomas Abraham0dfb3b42011-10-24 11:48:21 +02001742 * reset the fifos and other the settings.
Greg Kroah-Hartman7c175252019-12-10 15:37:05 +01001743 */
Ben Dooksb4975492008-07-03 12:32:51 +01001744
Thomas Abraham0dfb3b42011-10-24 11:48:21 +02001745static void s3c24xx_serial_resetport(struct uart_port *port,
1746 struct s3c2410_uartcfg *cfg)
Ben Dooksb4975492008-07-03 12:32:51 +01001747{
1748 struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
Thomas Abraham0dfb3b42011-10-24 11:48:21 +02001749 unsigned long ucon = rd_regl(port, S3C2410_UCON);
Ben Dooksb4975492008-07-03 12:32:51 +01001750
Hector Martin19d48782021-03-05 06:38:54 +09001751 ucon &= (info->clksel_mask | info->ucon_mask);
1752 wr_regl(port, S3C2410_UCON, ucon | cfg->ucon);
Thomas Abraham0dfb3b42011-10-24 11:48:21 +02001753
1754 /* reset both fifos */
1755 wr_regl(port, S3C2410_UFCON, cfg->ufcon | S3C2410_UFCON_RESETBOTH);
1756 wr_regl(port, S3C2410_UFCON, cfg->ufcon);
1757
1758 /* some delay is required after fifo reset */
1759 udelay(1);
Ben Dooksb4975492008-07-03 12:32:51 +01001760}
1761
Krzysztof Kozlowskiebaa81c2016-06-27 13:59:08 +02001762#ifdef CONFIG_ARM_S3C24XX_CPUFREQ
Ben Dooks30555472008-10-21 14:06:36 +01001763
1764static int s3c24xx_serial_cpufreq_transition(struct notifier_block *nb,
1765 unsigned long val, void *data)
1766{
1767 struct s3c24xx_uart_port *port;
1768 struct uart_port *uport;
1769
1770 port = container_of(nb, struct s3c24xx_uart_port, freq_transition);
1771 uport = &port->port;
1772
1773 /* check to see if port is enabled */
1774
1775 if (port->pm_level != 0)
1776 return 0;
1777
1778 /* try and work out if the baudrate is changing, we can detect
1779 * a change in rate, but we do not have support for detecting
1780 * a disturbance in the clock-rate over the change.
1781 */
1782
Kyoungil Kim25f04ad2012-05-20 17:49:31 +09001783 if (IS_ERR(port->baudclk))
Ben Dooks30555472008-10-21 14:06:36 +01001784 goto exit;
1785
Kyoungil Kim25f04ad2012-05-20 17:49:31 +09001786 if (port->baudclk_rate == clk_get_rate(port->baudclk))
Ben Dooks30555472008-10-21 14:06:36 +01001787 goto exit;
1788
1789 if (val == CPUFREQ_PRECHANGE) {
1790 /* we should really shut the port down whilst the
Greg Kroah-Hartman7c175252019-12-10 15:37:05 +01001791 * frequency change is in progress.
1792 */
Ben Dooks30555472008-10-21 14:06:36 +01001793
1794 } else if (val == CPUFREQ_POSTCHANGE) {
1795 struct ktermios *termios;
1796 struct tty_struct *tty;
1797
Alan Coxebd2c8f2009-09-19 13:13:28 -07001798 if (uport->state == NULL)
Ben Dooks30555472008-10-21 14:06:36 +01001799 goto exit;
Ben Dooks30555472008-10-21 14:06:36 +01001800
Alan Coxebd2c8f2009-09-19 13:13:28 -07001801 tty = uport->state->port.tty;
Ben Dooks30555472008-10-21 14:06:36 +01001802
Ben Dooks7de40c22008-12-14 23:11:02 +00001803 if (tty == NULL)
Ben Dooks30555472008-10-21 14:06:36 +01001804 goto exit;
Ben Dooks30555472008-10-21 14:06:36 +01001805
Alan Coxadc8d742012-07-14 15:31:47 +01001806 termios = &tty->termios;
Ben Dooks30555472008-10-21 14:06:36 +01001807
1808 if (termios == NULL) {
Sachin Kamatd20925e2012-09-05 10:30:10 +05301809 dev_warn(uport->dev, "%s: no termios?\n", __func__);
Ben Dooks30555472008-10-21 14:06:36 +01001810 goto exit;
1811 }
1812
1813 s3c24xx_serial_set_termios(uport, termios, NULL);
1814 }
1815
Robert Baldygaef4aca72014-11-24 07:56:22 +01001816exit:
Ben Dooks30555472008-10-21 14:06:36 +01001817 return 0;
1818}
1819
Robert Baldygaef4aca72014-11-24 07:56:22 +01001820static inline int
1821s3c24xx_serial_cpufreq_register(struct s3c24xx_uart_port *port)
Ben Dooks30555472008-10-21 14:06:36 +01001822{
1823 port->freq_transition.notifier_call = s3c24xx_serial_cpufreq_transition;
1824
1825 return cpufreq_register_notifier(&port->freq_transition,
1826 CPUFREQ_TRANSITION_NOTIFIER);
1827}
1828
Robert Baldygaef4aca72014-11-24 07:56:22 +01001829static inline void
1830s3c24xx_serial_cpufreq_deregister(struct s3c24xx_uart_port *port)
Ben Dooks30555472008-10-21 14:06:36 +01001831{
1832 cpufreq_unregister_notifier(&port->freq_transition,
1833 CPUFREQ_TRANSITION_NOTIFIER);
1834}
1835
1836#else
Robert Baldygaef4aca72014-11-24 07:56:22 +01001837static inline int
1838s3c24xx_serial_cpufreq_register(struct s3c24xx_uart_port *port)
Ben Dooks30555472008-10-21 14:06:36 +01001839{
1840 return 0;
1841}
1842
Robert Baldygaef4aca72014-11-24 07:56:22 +01001843static inline void
1844s3c24xx_serial_cpufreq_deregister(struct s3c24xx_uart_port *port)
Ben Dooks30555472008-10-21 14:06:36 +01001845{
1846}
1847#endif
1848
Stuart Menefy5086e0a2019-02-12 21:40:22 +00001849static int s3c24xx_serial_enable_baudclk(struct s3c24xx_uart_port *ourport)
1850{
1851 struct device *dev = ourport->port.dev;
1852 struct s3c24xx_uart_info *info = ourport->info;
1853 char clk_name[MAX_CLK_NAME_LENGTH];
1854 unsigned int clk_sel;
1855 struct clk *clk;
1856 int clk_num;
1857 int ret;
1858
1859 clk_sel = ourport->cfg->clk_sel ? : info->def_clk_sel;
1860 for (clk_num = 0; clk_num < info->num_clks; clk_num++) {
1861 if (!(clk_sel & (1 << clk_num)))
1862 continue;
1863
1864 sprintf(clk_name, "clk_uart_baud%d", clk_num);
1865 clk = clk_get(dev, clk_name);
1866 if (IS_ERR(clk))
1867 continue;
1868
1869 ret = clk_prepare_enable(clk);
1870 if (ret) {
1871 clk_put(clk);
1872 continue;
1873 }
1874
1875 ourport->baudclk = clk;
1876 ourport->baudclk_rate = clk_get_rate(clk);
1877 s3c24xx_serial_setsource(&ourport->port, clk_num);
1878
1879 return 0;
1880 }
1881
1882 return -EINVAL;
1883}
1884
Ben Dooksb4975492008-07-03 12:32:51 +01001885/* s3c24xx_serial_init_port
1886 *
1887 * initialise a single serial port from the platform device given
1888 */
1889
1890static int s3c24xx_serial_init_port(struct s3c24xx_uart_port *ourport,
Ben Dooksb4975492008-07-03 12:32:51 +01001891 struct platform_device *platdev)
1892{
1893 struct uart_port *port = &ourport->port;
Thomas Abrahamda121502011-11-02 19:23:25 +09001894 struct s3c2410_uartcfg *cfg = ourport->cfg;
Ben Dooksb4975492008-07-03 12:32:51 +01001895 struct resource *res;
1896 int ret;
1897
Ben Dooksb4975492008-07-03 12:32:51 +01001898 if (platdev == NULL)
1899 return -ENODEV;
1900
Ben Dooksb4975492008-07-03 12:32:51 +01001901 if (port->mapbase != 0)
Krzysztof Kozlowskie51e4d82016-06-16 08:27:35 +02001902 return -EINVAL;
Ben Dooksb4975492008-07-03 12:32:51 +01001903
Ben Dooksb4975492008-07-03 12:32:51 +01001904 /* setup info for port */
1905 port->dev = &platdev->dev;
Ben Dooksb4975492008-07-03 12:32:51 +01001906
Ben Dooksb4975492008-07-03 12:32:51 +01001907 port->uartclk = 1;
1908
1909 if (cfg->uart_flags & UPF_CONS_FLOW) {
Greg Kroah-Hartmana05025d2019-12-10 15:37:03 +01001910 dev_dbg(port->dev, "enabling flow control\n");
Ben Dooksb4975492008-07-03 12:32:51 +01001911 port->flags |= UPF_CONS_FLOW;
1912 }
1913
1914 /* sort our the physical and virtual addresses for each UART */
1915
1916 res = platform_get_resource(platdev, IORESOURCE_MEM, 0);
1917 if (res == NULL) {
Sachin Kamatd20925e2012-09-05 10:30:10 +05301918 dev_err(port->dev, "failed to find memory resource for uart\n");
Ben Dooksb4975492008-07-03 12:32:51 +01001919 return -EINVAL;
1920 }
1921
Greg Kroah-Hartmana05025d2019-12-10 15:37:03 +01001922 dev_dbg(port->dev, "resource %pR)\n", res);
Ben Dooksb4975492008-07-03 12:32:51 +01001923
Thomas Abraham41147bf2013-01-01 00:21:55 -08001924 port->membase = devm_ioremap(port->dev, res->start, resource_size(res));
1925 if (!port->membase) {
1926 dev_err(port->dev, "failed to remap controller address\n");
1927 return -EBUSY;
1928 }
1929
Ben Dooksb690ace2008-10-21 14:07:03 +01001930 port->mapbase = res->start;
Ben Dooksb4975492008-07-03 12:32:51 +01001931 ret = platform_get_irq(platdev, 0);
Krzysztof Kozlowski695b8472020-06-17 17:28:56 +02001932 if (ret < 0) {
Ben Dooksb4975492008-07-03 12:32:51 +01001933 port->irq = 0;
Krzysztof Kozlowski695b8472020-06-17 17:28:56 +02001934 } else {
Ben Dooksb4975492008-07-03 12:32:51 +01001935 port->irq = ret;
Ben Dooksb73c289c2008-10-21 14:07:04 +01001936 ourport->rx_irq = ret;
1937 ourport->tx_irq = ret + 1;
1938 }
Sachin Kamat9303ac12012-09-05 10:30:11 +05301939
Hector Martinaaf14402021-03-05 06:38:55 +09001940 switch (ourport->info->type) {
1941 case TYPE_S3C24XX:
Tamseel Shams8c6c3782020-08-10 08:30:21 +05301942 ret = platform_get_irq(platdev, 1);
1943 if (ret > 0)
1944 ourport->tx_irq = ret;
Hector Martinaaf14402021-03-05 06:38:55 +09001945 break;
1946 default:
1947 break;
Tamseel Shams8c6c3782020-08-10 08:30:21 +05301948 }
Hector Martinaaf14402021-03-05 06:38:55 +09001949
Robert Baldyga658c9d2b72014-12-10 12:49:23 +01001950 /*
1951 * DMA is currently supported only on DT platforms, if DMA properties
1952 * are specified.
1953 */
1954 if (platdev->dev.of_node && of_find_property(platdev->dev.of_node,
1955 "dmas", NULL)) {
1956 ourport->dma = devm_kzalloc(port->dev,
1957 sizeof(*ourport->dma),
1958 GFP_KERNEL);
Krzysztof Kozlowskie51e4d82016-06-16 08:27:35 +02001959 if (!ourport->dma) {
1960 ret = -ENOMEM;
1961 goto err;
1962 }
Robert Baldyga658c9d2b72014-12-10 12:49:23 +01001963 }
Ben Dooksb4975492008-07-03 12:32:51 +01001964
1965 ourport->clk = clk_get(&platdev->dev, "uart");
Chander Kashyap60e93572013-05-28 18:32:07 +05301966 if (IS_ERR(ourport->clk)) {
1967 pr_err("%s: Controller clock not found\n",
1968 dev_name(&platdev->dev));
Krzysztof Kozlowskie51e4d82016-06-16 08:27:35 +02001969 ret = PTR_ERR(ourport->clk);
1970 goto err;
Chander Kashyap60e93572013-05-28 18:32:07 +05301971 }
1972
1973 ret = clk_prepare_enable(ourport->clk);
1974 if (ret) {
1975 pr_err("uart: clock failed to prepare+enable: %d\n", ret);
1976 clk_put(ourport->clk);
Krzysztof Kozlowskie51e4d82016-06-16 08:27:35 +02001977 goto err;
Chander Kashyap60e93572013-05-28 18:32:07 +05301978 }
Ben Dooksb4975492008-07-03 12:32:51 +01001979
Stuart Menefy5086e0a2019-02-12 21:40:22 +00001980 ret = s3c24xx_serial_enable_baudclk(ourport);
1981 if (ret)
1982 pr_warn("uart: failed to enable baudclk\n");
1983
Thomas Abraham88bb4ea2011-08-10 15:51:19 +05301984 /* Keep all interrupts masked and cleared */
Hector Martinaaf14402021-03-05 06:38:55 +09001985 switch (ourport->info->type) {
1986 case TYPE_S3C6400:
Thomas Abraham88bb4ea2011-08-10 15:51:19 +05301987 wr_regl(port, S3C64XX_UINTM, 0xf);
1988 wr_regl(port, S3C64XX_UINTP, 0xf);
1989 wr_regl(port, S3C64XX_UINTSP, 0xf);
Hector Martinaaf14402021-03-05 06:38:55 +09001990 break;
1991 default:
1992 break;
Thomas Abraham88bb4ea2011-08-10 15:51:19 +05301993 }
1994
Greg Kroah-Hartmana05025d2019-12-10 15:37:03 +01001995 dev_dbg(port->dev, "port: map=%pa, mem=%p, irq=%d (%d,%d), clock=%u\n",
1996 &port->mapbase, port->membase, port->irq,
1997 ourport->rx_irq, ourport->tx_irq, port->uartclk);
Ben Dooksb4975492008-07-03 12:32:51 +01001998
1999 /* reset the fifos (and setup the uart) */
2000 s3c24xx_serial_resetport(port, cfg);
Krzysztof Kozlowskie51e4d82016-06-16 08:27:35 +02002001
Ben Dooksb4975492008-07-03 12:32:51 +01002002 return 0;
Krzysztof Kozlowskie51e4d82016-06-16 08:27:35 +02002003
2004err:
2005 port->mapbase = 0;
2006 return ret;
Ben Dooksb4975492008-07-03 12:32:51 +01002007}
2008
Ben Dooksb4975492008-07-03 12:32:51 +01002009/* Device driver serial port probe */
2010
Greg Kroah-Hartman06674e52019-12-10 15:36:58 +01002011#ifdef CONFIG_OF
Thomas Abraham26c919e2011-11-06 22:10:44 +05302012static const struct of_device_id s3c24xx_uart_dt_match[];
Greg Kroah-Hartman06674e52019-12-10 15:36:58 +01002013#endif
2014
Ben Dooksb4975492008-07-03 12:32:51 +01002015static int probe_index;
2016
Krzysztof Kozlowski695b8472020-06-17 17:28:56 +02002017static inline struct s3c24xx_serial_drv_data *
2018s3c24xx_get_driver_data(struct platform_device *pdev)
Thomas Abraham26c919e2011-11-06 22:10:44 +05302019{
2020#ifdef CONFIG_OF
2021 if (pdev->dev.of_node) {
2022 const struct of_device_id *match;
Greg Kroah-Hartman9fe0d412019-12-10 15:37:06 +01002023
Thomas Abraham26c919e2011-11-06 22:10:44 +05302024 match = of_match_node(s3c24xx_uart_dt_match, pdev->dev.of_node);
2025 return (struct s3c24xx_serial_drv_data *)match->data;
2026 }
2027#endif
2028 return (struct s3c24xx_serial_drv_data *)
2029 platform_get_device_id(pdev)->driver_data;
2030}
2031
Thomas Abrahamda121502011-11-02 19:23:25 +09002032static int s3c24xx_serial_probe(struct platform_device *pdev)
Ben Dooksb4975492008-07-03 12:32:51 +01002033{
Naveen Krishna Chatradhi4622eb62014-07-14 17:07:18 +05302034 struct device_node *np = pdev->dev.of_node;
Ben Dooksb4975492008-07-03 12:32:51 +01002035 struct s3c24xx_uart_port *ourport;
Tomasz Figa13a9f6c62014-06-26 13:24:34 +02002036 int index = probe_index;
Hyunki Koo57253cc2020-05-06 17:02:40 +09002037 int ret, prop = 0;
Ben Dooksb4975492008-07-03 12:32:51 +01002038
Naveen Krishna Chatradhi4622eb62014-07-14 17:07:18 +05302039 if (np) {
2040 ret = of_alias_get_id(np, "serial");
Tomasz Figa13a9f6c62014-06-26 13:24:34 +02002041 if (ret >= 0)
2042 index = ret;
2043 }
Ben Dooksb4975492008-07-03 12:32:51 +01002044
Geert Uytterhoeven49ee23b2018-02-23 14:38:34 +01002045 if (index >= ARRAY_SIZE(s3c24xx_serial_ports)) {
2046 dev_err(&pdev->dev, "serial%d out of range\n", index);
2047 return -EINVAL;
2048 }
Tomasz Figa13a9f6c62014-06-26 13:24:34 +02002049 ourport = &s3c24xx_serial_ports[index];
Thomas Abrahamda121502011-11-02 19:23:25 +09002050
Thomas Abraham26c919e2011-11-06 22:10:44 +05302051 ourport->drv_data = s3c24xx_get_driver_data(pdev);
2052 if (!ourport->drv_data) {
2053 dev_err(&pdev->dev, "could not find driver data\n");
2054 return -ENODEV;
2055 }
Thomas Abrahamda121502011-11-02 19:23:25 +09002056
Kyoungil Kim7cd88832012-05-20 17:45:54 +09002057 ourport->baudclk = ERR_PTR(-EINVAL);
Thomas Abrahamda121502011-11-02 19:23:25 +09002058 ourport->info = ourport->drv_data->info;
Jingoo Han574de552013-07-30 17:06:57 +09002059 ourport->cfg = (dev_get_platdata(&pdev->dev)) ?
Jingoo Hand4aab202013-09-09 14:10:30 +09002060 dev_get_platdata(&pdev->dev) :
Thomas Abrahamda121502011-11-02 19:23:25 +09002061 ourport->drv_data->def_cfg;
2062
Hector Martin64689162021-03-05 06:38:53 +09002063 switch (ourport->info->type) {
Hector Martinaaf14402021-03-05 06:38:55 +09002064 case TYPE_S3C24XX:
Hector Martin64689162021-03-05 06:38:53 +09002065 ourport->port.ops = &s3c24xx_serial_ops;
2066 break;
Hector Martinaaf14402021-03-05 06:38:55 +09002067 case TYPE_S3C6400:
Hector Martin64689162021-03-05 06:38:53 +09002068 ourport->port.ops = &s3c64xx_serial_ops;
2069 break;
2070 }
2071
Hyunki Koo57253cc2020-05-06 17:02:40 +09002072 if (np) {
Naveen Krishna Chatradhi4622eb62014-07-14 17:07:18 +05302073 of_property_read_u32(np,
Naveen Krishna Chatradhi135f07c2014-07-14 17:07:16 +05302074 "samsung,uart-fifosize", &ourport->port.fifosize);
2075
Hyunki Koo57253cc2020-05-06 17:02:40 +09002076 if (of_property_read_u32(np, "reg-io-width", &prop) == 0) {
2077 switch (prop) {
2078 case 1:
2079 ourport->port.iotype = UPIO_MEM;
2080 break;
2081 case 4:
2082 ourport->port.iotype = UPIO_MEM32;
2083 break;
2084 default:
2085 dev_warn(&pdev->dev, "unsupported reg-io-width (%d)\n",
2086 prop);
2087 ret = -EINVAL;
2088 break;
2089 }
2090 }
2091 }
2092
Robert Baldyga2f1ba722014-11-24 07:56:23 +01002093 if (ourport->drv_data->fifosize[index])
2094 ourport->port.fifosize = ourport->drv_data->fifosize[index];
2095 else if (ourport->info->fifosize)
2096 ourport->port.fifosize = ourport->info->fifosize;
Dmitry Safonov831cb962019-12-13 00:06:36 +00002097 ourport->port.has_sysrq = IS_ENABLED(CONFIG_SERIAL_SAMSUNG_CONSOLE);
Thomas Abrahamda121502011-11-02 19:23:25 +09002098
Marek Szyprowski81ccb2a2015-07-31 10:58:27 +02002099 /*
2100 * DMA transfers must be aligned at least to cache line size,
2101 * so find minimal transfer size suitable for DMA mode
2102 */
2103 ourport->min_dma_size = max_t(int, ourport->port.fifosize,
2104 dma_get_cache_alignment());
2105
Greg Kroah-Hartmana05025d2019-12-10 15:37:03 +01002106 dev_dbg(&pdev->dev, "%s: initialising port %p...\n", __func__, ourport);
Ben Dooksb4975492008-07-03 12:32:51 +01002107
Thomas Abrahamda121502011-11-02 19:23:25 +09002108 ret = s3c24xx_serial_init_port(ourport, pdev);
Ben Dooksb4975492008-07-03 12:32:51 +01002109 if (ret < 0)
Tushar Behera8ad711a2014-06-23 11:32:14 +05302110 return ret;
Ben Dooksb4975492008-07-03 12:32:51 +01002111
Tushar Behera6f134c3c2014-01-20 14:32:34 +05302112 if (!s3c24xx_uart_drv.state) {
2113 ret = uart_register_driver(&s3c24xx_uart_drv);
2114 if (ret < 0) {
2115 pr_err("Failed to register Samsung UART driver\n");
2116 return ret;
2117 }
2118 }
2119
Greg Kroah-Hartmana05025d2019-12-10 15:37:03 +01002120 dev_dbg(&pdev->dev, "%s: adding port\n", __func__);
Ben Dooksb4975492008-07-03 12:32:51 +01002121 uart_add_one_port(&s3c24xx_uart_drv, &ourport->port);
Thomas Abrahamda121502011-11-02 19:23:25 +09002122 platform_set_drvdata(pdev, &ourport->port);
Ben Dooksb4975492008-07-03 12:32:51 +01002123
Heiko Stübner0da33362013-12-05 00:54:38 +01002124 /*
2125 * Deactivate the clock enabled in s3c24xx_serial_init_port here,
2126 * so that a potential re-enablement through the pm-callback overlaps
2127 * and keeps the clock enabled in this case.
2128 */
2129 clk_disable_unprepare(ourport->clk);
Stuart Menefy5086e0a2019-02-12 21:40:22 +00002130 if (!IS_ERR(ourport->baudclk))
2131 clk_disable_unprepare(ourport->baudclk);
Heiko Stübner0da33362013-12-05 00:54:38 +01002132
Ben Dooks30555472008-10-21 14:06:36 +01002133 ret = s3c24xx_serial_cpufreq_register(ourport);
2134 if (ret < 0)
Thomas Abrahamda121502011-11-02 19:23:25 +09002135 dev_err(&pdev->dev, "failed to add cpufreq notifier\n");
Ben Dooks30555472008-10-21 14:06:36 +01002136
Krzysztof Kozlowski926b7b52016-06-16 08:27:36 +02002137 probe_index++;
2138
Ben Dooksb4975492008-07-03 12:32:51 +01002139 return 0;
Ben Dooksb4975492008-07-03 12:32:51 +01002140}
2141
Bill Pembertonae8d8a12012-11-19 13:26:18 -05002142static int s3c24xx_serial_remove(struct platform_device *dev)
Ben Dooksb4975492008-07-03 12:32:51 +01002143{
2144 struct uart_port *port = s3c24xx_dev_to_port(&dev->dev);
2145
2146 if (port) {
Ben Dooks30555472008-10-21 14:06:36 +01002147 s3c24xx_serial_cpufreq_deregister(to_ourport(port));
Ben Dooksb4975492008-07-03 12:32:51 +01002148 uart_remove_one_port(&s3c24xx_uart_drv, port);
2149 }
2150
Tushar Behera6f134c3c2014-01-20 14:32:34 +05302151 uart_unregister_driver(&s3c24xx_uart_drv);
2152
Ben Dooksb4975492008-07-03 12:32:51 +01002153 return 0;
2154}
2155
Ben Dooksb4975492008-07-03 12:32:51 +01002156/* UART power management code */
MyungJoo Hamaef7fe52011-06-29 15:28:24 +09002157#ifdef CONFIG_PM_SLEEP
2158static int s3c24xx_serial_suspend(struct device *dev)
Ben Dooksb4975492008-07-03 12:32:51 +01002159{
MyungJoo Hamaef7fe52011-06-29 15:28:24 +09002160 struct uart_port *port = s3c24xx_dev_to_port(dev);
Ben Dooksb4975492008-07-03 12:32:51 +01002161
2162 if (port)
2163 uart_suspend_port(&s3c24xx_uart_drv, port);
2164
2165 return 0;
2166}
2167
MyungJoo Hamaef7fe52011-06-29 15:28:24 +09002168static int s3c24xx_serial_resume(struct device *dev)
Ben Dooksb4975492008-07-03 12:32:51 +01002169{
MyungJoo Hamaef7fe52011-06-29 15:28:24 +09002170 struct uart_port *port = s3c24xx_dev_to_port(dev);
Ben Dooksb4975492008-07-03 12:32:51 +01002171 struct s3c24xx_uart_port *ourport = to_ourport(port);
2172
2173 if (port) {
Thomas Abraham9484b002012-10-03 07:40:04 +09002174 clk_prepare_enable(ourport->clk);
Marek Szyprowski1ff36522018-09-13 10:21:25 +02002175 if (!IS_ERR(ourport->baudclk))
2176 clk_prepare_enable(ourport->baudclk);
Ben Dooksb4975492008-07-03 12:32:51 +01002177 s3c24xx_serial_resetport(port, s3c24xx_port_to_cfg(port));
Marek Szyprowski1ff36522018-09-13 10:21:25 +02002178 if (!IS_ERR(ourport->baudclk))
2179 clk_disable_unprepare(ourport->baudclk);
Thomas Abraham9484b002012-10-03 07:40:04 +09002180 clk_disable_unprepare(ourport->clk);
Ben Dooksb4975492008-07-03 12:32:51 +01002181
2182 uart_resume_port(&s3c24xx_uart_drv, port);
2183 }
2184
2185 return 0;
2186}
MyungJoo Hamaef7fe52011-06-29 15:28:24 +09002187
Michael Spangd09a7302013-03-27 19:34:24 -04002188static int s3c24xx_serial_resume_noirq(struct device *dev)
2189{
2190 struct uart_port *port = s3c24xx_dev_to_port(dev);
남영민a8a17812017-02-01 19:25:46 +09002191 struct s3c24xx_uart_port *ourport = to_ourport(port);
Michael Spangd09a7302013-03-27 19:34:24 -04002192
2193 if (port) {
2194 /* restore IRQ mask */
Hector Martinaaf14402021-03-05 06:38:55 +09002195 switch (ourport->info->type) {
2196 case TYPE_S3C6400: {
Michael Spangd09a7302013-03-27 19:34:24 -04002197 unsigned int uintm = 0xf;
Greg Kroah-Hartman9fe0d412019-12-10 15:37:06 +01002198
Greg Kroah-Hartman83362402019-12-17 15:02:32 +01002199 if (ourport->tx_enabled)
Michael Spangd09a7302013-03-27 19:34:24 -04002200 uintm &= ~S3C64XX_UINTM_TXD_MSK;
Greg Kroah-Hartman83362402019-12-17 15:02:32 +01002201 if (ourport->rx_enabled)
Michael Spangd09a7302013-03-27 19:34:24 -04002202 uintm &= ~S3C64XX_UINTM_RXD_MSK;
남영민a8a17812017-02-01 19:25:46 +09002203 clk_prepare_enable(ourport->clk);
Marek Szyprowski1ff36522018-09-13 10:21:25 +02002204 if (!IS_ERR(ourport->baudclk))
2205 clk_prepare_enable(ourport->baudclk);
Michael Spangd09a7302013-03-27 19:34:24 -04002206 wr_regl(port, S3C64XX_UINTM, uintm);
Marek Szyprowski1ff36522018-09-13 10:21:25 +02002207 if (!IS_ERR(ourport->baudclk))
2208 clk_disable_unprepare(ourport->baudclk);
남영민a8a17812017-02-01 19:25:46 +09002209 clk_disable_unprepare(ourport->clk);
Hector Martinaaf14402021-03-05 06:38:55 +09002210 break;
2211 }
2212 default:
2213 break;
Michael Spangd09a7302013-03-27 19:34:24 -04002214 }
2215 }
2216
2217 return 0;
2218}
2219
MyungJoo Hamaef7fe52011-06-29 15:28:24 +09002220static const struct dev_pm_ops s3c24xx_serial_pm_ops = {
2221 .suspend = s3c24xx_serial_suspend,
2222 .resume = s3c24xx_serial_resume,
Michael Spangd09a7302013-03-27 19:34:24 -04002223 .resume_noirq = s3c24xx_serial_resume_noirq,
MyungJoo Hamaef7fe52011-06-29 15:28:24 +09002224};
Kukjin Kimb882fc12011-07-28 08:50:38 +09002225#define SERIAL_SAMSUNG_PM_OPS (&s3c24xx_serial_pm_ops)
2226
MyungJoo Hamaef7fe52011-06-29 15:28:24 +09002227#else /* !CONFIG_PM_SLEEP */
Kukjin Kimb882fc12011-07-28 08:50:38 +09002228
2229#define SERIAL_SAMSUNG_PM_OPS NULL
MyungJoo Hamaef7fe52011-06-29 15:28:24 +09002230#endif /* CONFIG_PM_SLEEP */
Ben Dooksb4975492008-07-03 12:32:51 +01002231
Ben Dooksb4975492008-07-03 12:32:51 +01002232/* Console code */
2233
2234#ifdef CONFIG_SERIAL_SAMSUNG_CONSOLE
2235
2236static struct uart_port *cons_uart;
2237
2238static int
2239s3c24xx_serial_console_txrdy(struct uart_port *port, unsigned int ufcon)
2240{
2241 struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
2242 unsigned long ufstat, utrstat;
2243
2244 if (ufcon & S3C2410_UFCON_FIFOMODE) {
Uwe Kleine-König9ddc5b62010-01-20 17:02:24 +01002245 /* fifo mode - check amount of data in fifo registers... */
Ben Dooksb4975492008-07-03 12:32:51 +01002246
2247 ufstat = rd_regl(port, S3C2410_UFSTAT);
2248 return (ufstat & info->tx_fifofull) ? 0 : 1;
2249 }
2250
2251 /* in non-fifo mode, we go and use the tx buffer empty */
2252
2253 utrstat = rd_regl(port, S3C2410_UTRSTAT);
2254 return (utrstat & S3C2410_UTRSTAT_TXE) ? 1 : 0;
2255}
2256
Michael Spang38adbc52013-03-27 19:34:25 -04002257static bool
2258s3c24xx_port_configured(unsigned int ucon)
2259{
2260 /* consider the serial port configured if the tx/rx mode set */
2261 return (ucon & 0xf) != 0;
2262}
2263
Julien Pichon93b5c032012-09-21 23:22:31 -07002264#ifdef CONFIG_CONSOLE_POLL
2265/*
2266 * Console polling routines for writing and reading from the uart while
2267 * in an interrupt or debug context.
2268 */
2269
2270static int s3c24xx_serial_get_poll_char(struct uart_port *port)
2271{
2272 struct s3c24xx_uart_port *ourport = to_ourport(port);
2273 unsigned int ufstat;
2274
2275 ufstat = rd_regl(port, S3C2410_UFSTAT);
2276 if (s3c24xx_serial_rx_fifocnt(ourport, ufstat) == 0)
2277 return NO_POLL_CHAR;
2278
Hyunki Koo8fba6c02020-05-06 17:02:38 +09002279 return rd_reg(port, S3C2410_URXH);
Julien Pichon93b5c032012-09-21 23:22:31 -07002280}
2281
2282static void s3c24xx_serial_put_poll_char(struct uart_port *port,
2283 unsigned char c)
2284{
Doug Andersonbb7f09b2014-04-21 09:40:34 -07002285 unsigned int ufcon = rd_regl(port, S3C2410_UFCON);
2286 unsigned int ucon = rd_regl(port, S3C2410_UCON);
Michael Spang38adbc52013-03-27 19:34:25 -04002287
2288 /* not possible to xmit on unconfigured port */
2289 if (!s3c24xx_port_configured(ucon))
2290 return;
Julien Pichon93b5c032012-09-21 23:22:31 -07002291
2292 while (!s3c24xx_serial_console_txrdy(port, ufcon))
2293 cpu_relax();
Hyunki Koo8fba6c02020-05-06 17:02:38 +09002294 wr_reg(port, S3C2410_UTXH, c);
Julien Pichon93b5c032012-09-21 23:22:31 -07002295}
2296
2297#endif /* CONFIG_CONSOLE_POLL */
2298
Ben Dooksb4975492008-07-03 12:32:51 +01002299static void
2300s3c24xx_serial_console_putchar(struct uart_port *port, int ch)
2301{
Doug Andersonbb7f09b2014-04-21 09:40:34 -07002302 unsigned int ufcon = rd_regl(port, S3C2410_UFCON);
Michael Spang38adbc52013-03-27 19:34:25 -04002303
Ben Dooksb4975492008-07-03 12:32:51 +01002304 while (!s3c24xx_serial_console_txrdy(port, ufcon))
Doug Andersonf94b0572014-04-21 09:40:36 -07002305 cpu_relax();
Hyunki Koo8fba6c02020-05-06 17:02:38 +09002306 wr_reg(port, S3C2410_UTXH, ch);
Ben Dooksb4975492008-07-03 12:32:51 +01002307}
2308
2309static void
2310s3c24xx_serial_console_write(struct console *co, const char *s,
2311 unsigned int count)
2312{
Doug Andersonab88c8d2014-04-21 09:40:35 -07002313 unsigned int ucon = rd_regl(cons_uart, S3C2410_UCON);
2314
2315 /* not possible to xmit on unconfigured port */
2316 if (!s3c24xx_port_configured(ucon))
2317 return;
2318
Ben Dooksb4975492008-07-03 12:32:51 +01002319 uart_console_write(cons_uart, s, count, s3c24xx_serial_console_putchar);
2320}
2321
2322static void __init
2323s3c24xx_serial_get_options(struct uart_port *port, int *baud,
2324 int *parity, int *bits)
2325{
Ben Dooksb4975492008-07-03 12:32:51 +01002326 struct clk *clk;
2327 unsigned int ulcon;
2328 unsigned int ucon;
2329 unsigned int ubrdiv;
2330 unsigned long rate;
Thomas Abraham5f5a7a52011-10-24 11:47:46 +02002331 unsigned int clk_sel;
2332 char clk_name[MAX_CLK_NAME_LENGTH];
Ben Dooksb4975492008-07-03 12:32:51 +01002333
2334 ulcon = rd_regl(port, S3C2410_ULCON);
2335 ucon = rd_regl(port, S3C2410_UCON);
2336 ubrdiv = rd_regl(port, S3C2410_UBRDIV);
2337
Michael Spang38adbc52013-03-27 19:34:25 -04002338 if (s3c24xx_port_configured(ucon)) {
Ben Dooksb4975492008-07-03 12:32:51 +01002339 switch (ulcon & S3C2410_LCON_CSMASK) {
2340 case S3C2410_LCON_CS5:
2341 *bits = 5;
2342 break;
2343 case S3C2410_LCON_CS6:
2344 *bits = 6;
2345 break;
2346 case S3C2410_LCON_CS7:
2347 *bits = 7;
2348 break;
Ben Dooksb4975492008-07-03 12:32:51 +01002349 case S3C2410_LCON_CS8:
Naveen Krishna Chatradhi3bcce592014-07-14 17:07:17 +05302350 default:
Ben Dooksb4975492008-07-03 12:32:51 +01002351 *bits = 8;
2352 break;
2353 }
2354
2355 switch (ulcon & S3C2410_LCON_PMASK) {
2356 case S3C2410_LCON_PEVEN:
2357 *parity = 'e';
2358 break;
2359
2360 case S3C2410_LCON_PODD:
2361 *parity = 'o';
2362 break;
2363
2364 case S3C2410_LCON_PNONE:
2365 default:
2366 *parity = 'n';
2367 }
2368
2369 /* now calculate the baud rate */
2370
Thomas Abraham5f5a7a52011-10-24 11:47:46 +02002371 clk_sel = s3c24xx_serial_getsource(port);
2372 sprintf(clk_name, "clk_uart_baud%d", clk_sel);
Ben Dooksb4975492008-07-03 12:32:51 +01002373
Thomas Abraham5f5a7a52011-10-24 11:47:46 +02002374 clk = clk_get(port->dev, clk_name);
Kyoungil Kim7cd88832012-05-20 17:45:54 +09002375 if (!IS_ERR(clk))
Thomas Abraham5f5a7a52011-10-24 11:47:46 +02002376 rate = clk_get_rate(clk);
Ben Dooksb4975492008-07-03 12:32:51 +01002377 else
2378 rate = 1;
2379
Ben Dooksb4975492008-07-03 12:32:51 +01002380 *baud = rate / (16 * (ubrdiv + 1));
Greg Kroah-Hartmana05025d2019-12-10 15:37:03 +01002381 dev_dbg(port->dev, "calculated baud %d\n", *baud);
Ben Dooksb4975492008-07-03 12:32:51 +01002382 }
Ben Dooksb4975492008-07-03 12:32:51 +01002383}
2384
Ben Dooksb4975492008-07-03 12:32:51 +01002385static int __init
2386s3c24xx_serial_console_setup(struct console *co, char *options)
2387{
2388 struct uart_port *port;
2389 int baud = 9600;
2390 int bits = 8;
2391 int parity = 'n';
2392 int flow = 'n';
2393
Ben Dooksb4975492008-07-03 12:32:51 +01002394 /* is this a valid port */
2395
Ben Dooks03d5e772008-11-03 09:21:23 +00002396 if (co->index == -1 || co->index >= CONFIG_SERIAL_SAMSUNG_UARTS)
Ben Dooksb4975492008-07-03 12:32:51 +01002397 co->index = 0;
2398
2399 port = &s3c24xx_serial_ports[co->index].port;
2400
2401 /* is the port configured? */
2402
Thomas Abrahamee430f12011-06-14 19:12:26 +09002403 if (port->mapbase == 0x0)
2404 return -ENODEV;
Ben Dooksb4975492008-07-03 12:32:51 +01002405
2406 cons_uart = port;
2407
Ben Dooksb4975492008-07-03 12:32:51 +01002408 /*
2409 * Check whether an invalid uart number has been specified, and
2410 * if so, search for the first available port that does have
2411 * console support.
2412 */
2413 if (options)
2414 uart_parse_options(options, &baud, &parity, &bits, &flow);
2415 else
2416 s3c24xx_serial_get_options(port, &baud, &parity, &bits);
2417
Greg Kroah-Hartmana05025d2019-12-10 15:37:03 +01002418 dev_dbg(port->dev, "baud %d\n", baud);
Ben Dooksb4975492008-07-03 12:32:51 +01002419
2420 return uart_set_options(port, co, baud, parity, bits, flow);
2421}
2422
Ben Dooksb4975492008-07-03 12:32:51 +01002423static struct console s3c24xx_serial_console = {
2424 .name = S3C24XX_SERIAL_NAME,
2425 .device = uart_console_device,
2426 .flags = CON_PRINTBUFFER,
2427 .index = -1,
2428 .write = s3c24xx_serial_console_write,
Thomas Abraham5822a5d2011-06-14 19:12:26 +09002429 .setup = s3c24xx_serial_console_setup,
2430 .data = &s3c24xx_uart_drv,
Ben Dooksb4975492008-07-03 12:32:51 +01002431};
Ben Dooksb4975492008-07-03 12:32:51 +01002432#endif /* CONFIG_SERIAL_SAMSUNG_CONSOLE */
2433
Thomas Abrahamda121502011-11-02 19:23:25 +09002434#ifdef CONFIG_CPU_S3C2410
2435static struct s3c24xx_serial_drv_data s3c2410_serial_drv_data = {
2436 .info = &(struct s3c24xx_uart_info) {
2437 .name = "Samsung S3C2410 UART",
Hector Martinaaf14402021-03-05 06:38:55 +09002438 .type = TYPE_S3C24XX,
2439 .port_type = PORT_S3C2410,
Thomas Abrahamda121502011-11-02 19:23:25 +09002440 .fifosize = 16,
2441 .rx_fifomask = S3C2410_UFSTAT_RXMASK,
2442 .rx_fifoshift = S3C2410_UFSTAT_RXSHIFT,
2443 .rx_fifofull = S3C2410_UFSTAT_RXFULL,
2444 .tx_fifofull = S3C2410_UFSTAT_TXFULL,
2445 .tx_fifomask = S3C2410_UFSTAT_TXMASK,
2446 .tx_fifoshift = S3C2410_UFSTAT_TXSHIFT,
2447 .def_clk_sel = S3C2410_UCON_CLKSEL0,
2448 .num_clks = 2,
2449 .clksel_mask = S3C2410_UCON_CLKMASK,
2450 .clksel_shift = S3C2410_UCON_CLKSHIFT,
2451 },
2452 .def_cfg = &(struct s3c2410_uartcfg) {
2453 .ucon = S3C2410_UCON_DEFAULT,
2454 .ufcon = S3C2410_UFCON_DEFAULT,
2455 },
2456};
2457#define S3C2410_SERIAL_DRV_DATA ((kernel_ulong_t)&s3c2410_serial_drv_data)
2458#else
2459#define S3C2410_SERIAL_DRV_DATA (kernel_ulong_t)NULL
2460#endif
2461
2462#ifdef CONFIG_CPU_S3C2412
2463static struct s3c24xx_serial_drv_data s3c2412_serial_drv_data = {
2464 .info = &(struct s3c24xx_uart_info) {
2465 .name = "Samsung S3C2412 UART",
Hector Martinaaf14402021-03-05 06:38:55 +09002466 .type = TYPE_S3C24XX,
2467 .port_type = PORT_S3C2412,
Thomas Abrahamda121502011-11-02 19:23:25 +09002468 .fifosize = 64,
2469 .has_divslot = 1,
2470 .rx_fifomask = S3C2440_UFSTAT_RXMASK,
2471 .rx_fifoshift = S3C2440_UFSTAT_RXSHIFT,
2472 .rx_fifofull = S3C2440_UFSTAT_RXFULL,
2473 .tx_fifofull = S3C2440_UFSTAT_TXFULL,
2474 .tx_fifomask = S3C2440_UFSTAT_TXMASK,
2475 .tx_fifoshift = S3C2440_UFSTAT_TXSHIFT,
2476 .def_clk_sel = S3C2410_UCON_CLKSEL2,
2477 .num_clks = 4,
2478 .clksel_mask = S3C2412_UCON_CLKMASK,
2479 .clksel_shift = S3C2412_UCON_CLKSHIFT,
2480 },
2481 .def_cfg = &(struct s3c2410_uartcfg) {
2482 .ucon = S3C2410_UCON_DEFAULT,
2483 .ufcon = S3C2410_UFCON_DEFAULT,
2484 },
2485};
2486#define S3C2412_SERIAL_DRV_DATA ((kernel_ulong_t)&s3c2412_serial_drv_data)
2487#else
2488#define S3C2412_SERIAL_DRV_DATA (kernel_ulong_t)NULL
2489#endif
2490
2491#if defined(CONFIG_CPU_S3C2440) || defined(CONFIG_CPU_S3C2416) || \
Denis 'GNUtoo' Cariklib26469a2012-02-23 08:23:52 +01002492 defined(CONFIG_CPU_S3C2443) || defined(CONFIG_CPU_S3C2442)
Thomas Abrahamda121502011-11-02 19:23:25 +09002493static struct s3c24xx_serial_drv_data s3c2440_serial_drv_data = {
2494 .info = &(struct s3c24xx_uart_info) {
2495 .name = "Samsung S3C2440 UART",
Hector Martinaaf14402021-03-05 06:38:55 +09002496 .type = TYPE_S3C24XX,
2497 .port_type = PORT_S3C2440,
Thomas Abrahamda121502011-11-02 19:23:25 +09002498 .fifosize = 64,
2499 .has_divslot = 1,
2500 .rx_fifomask = S3C2440_UFSTAT_RXMASK,
2501 .rx_fifoshift = S3C2440_UFSTAT_RXSHIFT,
2502 .rx_fifofull = S3C2440_UFSTAT_RXFULL,
2503 .tx_fifofull = S3C2440_UFSTAT_TXFULL,
2504 .tx_fifomask = S3C2440_UFSTAT_TXMASK,
2505 .tx_fifoshift = S3C2440_UFSTAT_TXSHIFT,
2506 .def_clk_sel = S3C2410_UCON_CLKSEL2,
2507 .num_clks = 4,
2508 .clksel_mask = S3C2412_UCON_CLKMASK,
2509 .clksel_shift = S3C2412_UCON_CLKSHIFT,
Hector Martin19d48782021-03-05 06:38:54 +09002510 .ucon_mask = S3C2440_UCON0_DIVMASK,
Thomas Abrahamda121502011-11-02 19:23:25 +09002511 },
2512 .def_cfg = &(struct s3c2410_uartcfg) {
2513 .ucon = S3C2410_UCON_DEFAULT,
2514 .ufcon = S3C2410_UFCON_DEFAULT,
2515 },
2516};
2517#define S3C2440_SERIAL_DRV_DATA ((kernel_ulong_t)&s3c2440_serial_drv_data)
2518#else
2519#define S3C2440_SERIAL_DRV_DATA (kernel_ulong_t)NULL
2520#endif
2521
Kukjin Kim953b53a2014-07-01 06:32:22 +09002522#if defined(CONFIG_CPU_S3C6400) || defined(CONFIG_CPU_S3C6410)
Thomas Abrahamda121502011-11-02 19:23:25 +09002523static struct s3c24xx_serial_drv_data s3c6400_serial_drv_data = {
2524 .info = &(struct s3c24xx_uart_info) {
2525 .name = "Samsung S3C6400 UART",
Hector Martinaaf14402021-03-05 06:38:55 +09002526 .type = TYPE_S3C6400,
2527 .port_type = PORT_S3C6400,
Thomas Abrahamda121502011-11-02 19:23:25 +09002528 .fifosize = 64,
2529 .has_divslot = 1,
2530 .rx_fifomask = S3C2440_UFSTAT_RXMASK,
2531 .rx_fifoshift = S3C2440_UFSTAT_RXSHIFT,
2532 .rx_fifofull = S3C2440_UFSTAT_RXFULL,
2533 .tx_fifofull = S3C2440_UFSTAT_TXFULL,
2534 .tx_fifomask = S3C2440_UFSTAT_TXMASK,
2535 .tx_fifoshift = S3C2440_UFSTAT_TXSHIFT,
2536 .def_clk_sel = S3C2410_UCON_CLKSEL2,
2537 .num_clks = 4,
2538 .clksel_mask = S3C6400_UCON_CLKMASK,
2539 .clksel_shift = S3C6400_UCON_CLKSHIFT,
2540 },
2541 .def_cfg = &(struct s3c2410_uartcfg) {
2542 .ucon = S3C2410_UCON_DEFAULT,
2543 .ufcon = S3C2410_UFCON_DEFAULT,
2544 },
2545};
2546#define S3C6400_SERIAL_DRV_DATA ((kernel_ulong_t)&s3c6400_serial_drv_data)
2547#else
2548#define S3C6400_SERIAL_DRV_DATA (kernel_ulong_t)NULL
2549#endif
2550
2551#ifdef CONFIG_CPU_S5PV210
2552static struct s3c24xx_serial_drv_data s5pv210_serial_drv_data = {
2553 .info = &(struct s3c24xx_uart_info) {
2554 .name = "Samsung S5PV210 UART",
Hector Martinaaf14402021-03-05 06:38:55 +09002555 .type = TYPE_S3C6400,
2556 .port_type = PORT_S3C6400,
Thomas Abrahamda121502011-11-02 19:23:25 +09002557 .has_divslot = 1,
2558 .rx_fifomask = S5PV210_UFSTAT_RXMASK,
2559 .rx_fifoshift = S5PV210_UFSTAT_RXSHIFT,
2560 .rx_fifofull = S5PV210_UFSTAT_RXFULL,
2561 .tx_fifofull = S5PV210_UFSTAT_TXFULL,
2562 .tx_fifomask = S5PV210_UFSTAT_TXMASK,
2563 .tx_fifoshift = S5PV210_UFSTAT_TXSHIFT,
2564 .def_clk_sel = S3C2410_UCON_CLKSEL0,
2565 .num_clks = 2,
2566 .clksel_mask = S5PV210_UCON_CLKMASK,
2567 .clksel_shift = S5PV210_UCON_CLKSHIFT,
2568 },
2569 .def_cfg = &(struct s3c2410_uartcfg) {
2570 .ucon = S5PV210_UCON_DEFAULT,
2571 .ufcon = S5PV210_UFCON_DEFAULT,
2572 },
2573 .fifosize = { 256, 64, 16, 16 },
2574};
2575#define S5PV210_SERIAL_DRV_DATA ((kernel_ulong_t)&s5pv210_serial_drv_data)
2576#else
2577#define S5PV210_SERIAL_DRV_DATA (kernel_ulong_t)NULL
2578#endif
2579
Chander Kashyap33f88132013-06-19 00:29:34 +09002580#if defined(CONFIG_ARCH_EXYNOS)
Chanwoo Choi31ec77a2014-12-02 17:49:54 +09002581#define EXYNOS_COMMON_SERIAL_DRV_DATA \
2582 .info = &(struct s3c24xx_uart_info) { \
2583 .name = "Samsung Exynos UART", \
Hector Martinaaf14402021-03-05 06:38:55 +09002584 .type = TYPE_S3C6400, \
2585 .port_type = PORT_S3C6400, \
Chanwoo Choi31ec77a2014-12-02 17:49:54 +09002586 .has_divslot = 1, \
2587 .rx_fifomask = S5PV210_UFSTAT_RXMASK, \
2588 .rx_fifoshift = S5PV210_UFSTAT_RXSHIFT, \
2589 .rx_fifofull = S5PV210_UFSTAT_RXFULL, \
2590 .tx_fifofull = S5PV210_UFSTAT_TXFULL, \
2591 .tx_fifomask = S5PV210_UFSTAT_TXMASK, \
2592 .tx_fifoshift = S5PV210_UFSTAT_TXSHIFT, \
2593 .def_clk_sel = S3C2410_UCON_CLKSEL0, \
2594 .num_clks = 1, \
2595 .clksel_mask = 0, \
2596 .clksel_shift = 0, \
2597 }, \
2598 .def_cfg = &(struct s3c2410_uartcfg) { \
2599 .ucon = S5PV210_UCON_DEFAULT, \
2600 .ufcon = S5PV210_UFCON_DEFAULT, \
2601 .has_fracval = 1, \
2602 } \
2603
Thomas Abrahamda121502011-11-02 19:23:25 +09002604static struct s3c24xx_serial_drv_data exynos4210_serial_drv_data = {
Chanwoo Choi31ec77a2014-12-02 17:49:54 +09002605 EXYNOS_COMMON_SERIAL_DRV_DATA,
Thomas Abrahamda121502011-11-02 19:23:25 +09002606 .fifosize = { 256, 64, 16, 16 },
2607};
Chanwoo Choi31ec77a2014-12-02 17:49:54 +09002608
2609static struct s3c24xx_serial_drv_data exynos5433_serial_drv_data = {
2610 EXYNOS_COMMON_SERIAL_DRV_DATA,
2611 .fifosize = { 64, 256, 16, 256 },
2612};
2613
Thomas Abrahamda121502011-11-02 19:23:25 +09002614#define EXYNOS4210_SERIAL_DRV_DATA ((kernel_ulong_t)&exynos4210_serial_drv_data)
Chanwoo Choi31ec77a2014-12-02 17:49:54 +09002615#define EXYNOS5433_SERIAL_DRV_DATA ((kernel_ulong_t)&exynos5433_serial_drv_data)
Thomas Abrahamda121502011-11-02 19:23:25 +09002616#else
2617#define EXYNOS4210_SERIAL_DRV_DATA (kernel_ulong_t)NULL
Chanwoo Choi31ec77a2014-12-02 17:49:54 +09002618#define EXYNOS5433_SERIAL_DRV_DATA (kernel_ulong_t)NULL
Thomas Abrahamda121502011-11-02 19:23:25 +09002619#endif
2620
Krzysztof Kozlowski24ee4df2015-05-02 00:40:05 +09002621static const struct platform_device_id s3c24xx_serial_driver_ids[] = {
Thomas Abrahamda121502011-11-02 19:23:25 +09002622 {
2623 .name = "s3c2410-uart",
2624 .driver_data = S3C2410_SERIAL_DRV_DATA,
2625 }, {
2626 .name = "s3c2412-uart",
2627 .driver_data = S3C2412_SERIAL_DRV_DATA,
2628 }, {
2629 .name = "s3c2440-uart",
2630 .driver_data = S3C2440_SERIAL_DRV_DATA,
2631 }, {
2632 .name = "s3c6400-uart",
2633 .driver_data = S3C6400_SERIAL_DRV_DATA,
2634 }, {
2635 .name = "s5pv210-uart",
2636 .driver_data = S5PV210_SERIAL_DRV_DATA,
2637 }, {
2638 .name = "exynos4210-uart",
2639 .driver_data = EXYNOS4210_SERIAL_DRV_DATA,
Chanwoo Choi31ec77a2014-12-02 17:49:54 +09002640 }, {
2641 .name = "exynos5433-uart",
2642 .driver_data = EXYNOS5433_SERIAL_DRV_DATA,
Thomas Abrahamda121502011-11-02 19:23:25 +09002643 },
2644 { },
2645};
2646MODULE_DEVICE_TABLE(platform, s3c24xx_serial_driver_ids);
2647
Thomas Abraham26c919e2011-11-06 22:10:44 +05302648#ifdef CONFIG_OF
2649static const struct of_device_id s3c24xx_uart_dt_match[] = {
Heiko Stübner666ca0b2012-11-22 11:37:44 +01002650 { .compatible = "samsung,s3c2410-uart",
2651 .data = (void *)S3C2410_SERIAL_DRV_DATA },
2652 { .compatible = "samsung,s3c2412-uart",
2653 .data = (void *)S3C2412_SERIAL_DRV_DATA },
2654 { .compatible = "samsung,s3c2440-uart",
2655 .data = (void *)S3C2440_SERIAL_DRV_DATA },
2656 { .compatible = "samsung,s3c6400-uart",
2657 .data = (void *)S3C6400_SERIAL_DRV_DATA },
2658 { .compatible = "samsung,s5pv210-uart",
2659 .data = (void *)S5PV210_SERIAL_DRV_DATA },
Thomas Abraham26c919e2011-11-06 22:10:44 +05302660 { .compatible = "samsung,exynos4210-uart",
Mark Browna169a882011-11-08 17:00:14 +09002661 .data = (void *)EXYNOS4210_SERIAL_DRV_DATA },
Chanwoo Choi31ec77a2014-12-02 17:49:54 +09002662 { .compatible = "samsung,exynos5433-uart",
2663 .data = (void *)EXYNOS5433_SERIAL_DRV_DATA },
Thomas Abraham26c919e2011-11-06 22:10:44 +05302664 {},
2665};
2666MODULE_DEVICE_TABLE(of, s3c24xx_uart_dt_match);
Thomas Abraham26c919e2011-11-06 22:10:44 +05302667#endif
2668
Thomas Abrahamda121502011-11-02 19:23:25 +09002669static struct platform_driver samsung_serial_driver = {
2670 .probe = s3c24xx_serial_probe,
Bill Pemberton2d47b712012-11-19 13:21:34 -05002671 .remove = s3c24xx_serial_remove,
Thomas Abrahamda121502011-11-02 19:23:25 +09002672 .id_table = s3c24xx_serial_driver_ids,
2673 .driver = {
2674 .name = "samsung-uart",
Thomas Abrahamda121502011-11-02 19:23:25 +09002675 .pm = SERIAL_SAMSUNG_PM_OPS,
Sachin Kamat905f4ba2013-01-07 09:50:42 +05302676 .of_match_table = of_match_ptr(s3c24xx_uart_dt_match),
Thomas Abrahamda121502011-11-02 19:23:25 +09002677 },
2678};
2679
Tushar Behera6f134c3c2014-01-20 14:32:34 +05302680module_platform_driver(samsung_serial_driver);
Thomas Abrahamda121502011-11-02 19:23:25 +09002681
Marek Szyprowskic3bda292015-02-02 10:47:35 +01002682#ifdef CONFIG_SERIAL_SAMSUNG_CONSOLE
Tomasz Figab94ba032015-01-23 14:47:41 +01002683/*
2684 * Early console.
2685 */
2686
Hyunki Koo57253cc2020-05-06 17:02:40 +09002687static void wr_reg_barrier(struct uart_port *port, u32 reg, u32 val)
2688{
2689 switch (port->iotype) {
2690 case UPIO_MEM:
2691 writeb(val, portaddr(port, reg));
2692 break;
2693 case UPIO_MEM32:
2694 writel(val, portaddr(port, reg));
2695 break;
2696 }
2697}
2698
Tomasz Figab94ba032015-01-23 14:47:41 +01002699struct samsung_early_console_data {
2700 u32 txfull_mask;
2701};
2702
2703static void samsung_early_busyuart(struct uart_port *port)
2704{
2705 while (!(readl(port->membase + S3C2410_UTRSTAT) & S3C2410_UTRSTAT_TXFE))
2706 ;
2707}
2708
2709static void samsung_early_busyuart_fifo(struct uart_port *port)
2710{
2711 struct samsung_early_console_data *data = port->private_data;
2712
2713 while (readl(port->membase + S3C2410_UFSTAT) & data->txfull_mask)
2714 ;
2715}
2716
2717static void samsung_early_putc(struct uart_port *port, int c)
2718{
2719 if (readl(port->membase + S3C2410_UFCON) & S3C2410_UFCON_FIFOMODE)
2720 samsung_early_busyuart_fifo(port);
2721 else
2722 samsung_early_busyuart(port);
2723
Hyunki Koo57253cc2020-05-06 17:02:40 +09002724 wr_reg_barrier(port, S3C2410_UTXH, c);
Tomasz Figab94ba032015-01-23 14:47:41 +01002725}
2726
Greg Kroah-Hartman90ece852019-12-10 15:37:04 +01002727static void samsung_early_write(struct console *con, const char *s,
2728 unsigned int n)
Tomasz Figab94ba032015-01-23 14:47:41 +01002729{
2730 struct earlycon_device *dev = con->data;
2731
2732 uart_console_write(&dev->port, s, n, samsung_early_putc);
2733}
2734
2735static int __init samsung_early_console_setup(struct earlycon_device *device,
2736 const char *opt)
2737{
2738 if (!device->port.membase)
2739 return -ENODEV;
2740
2741 device->con->write = samsung_early_write;
2742 return 0;
2743}
2744
2745/* S3C2410 */
2746static struct samsung_early_console_data s3c2410_early_console_data = {
2747 .txfull_mask = S3C2410_UFSTAT_TXFULL,
2748};
2749
2750static int __init s3c2410_early_console_setup(struct earlycon_device *device,
2751 const char *opt)
2752{
2753 device->port.private_data = &s3c2410_early_console_data;
2754 return samsung_early_console_setup(device, opt);
2755}
Krzysztof Kozlowski695b8472020-06-17 17:28:56 +02002756
Tomasz Figab94ba032015-01-23 14:47:41 +01002757OF_EARLYCON_DECLARE(s3c2410, "samsung,s3c2410-uart",
2758 s3c2410_early_console_setup);
Tomasz Figab94ba032015-01-23 14:47:41 +01002759
2760/* S3C2412, S3C2440, S3C64xx */
2761static struct samsung_early_console_data s3c2440_early_console_data = {
2762 .txfull_mask = S3C2440_UFSTAT_TXFULL,
2763};
2764
2765static int __init s3c2440_early_console_setup(struct earlycon_device *device,
2766 const char *opt)
2767{
2768 device->port.private_data = &s3c2440_early_console_data;
2769 return samsung_early_console_setup(device, opt);
2770}
Krzysztof Kozlowski695b8472020-06-17 17:28:56 +02002771
Tomasz Figab94ba032015-01-23 14:47:41 +01002772OF_EARLYCON_DECLARE(s3c2412, "samsung,s3c2412-uart",
2773 s3c2440_early_console_setup);
2774OF_EARLYCON_DECLARE(s3c2440, "samsung,s3c2440-uart",
2775 s3c2440_early_console_setup);
2776OF_EARLYCON_DECLARE(s3c6400, "samsung,s3c6400-uart",
2777 s3c2440_early_console_setup);
Tomasz Figab94ba032015-01-23 14:47:41 +01002778
Krzysztof Kozlowskib2097132020-01-04 16:21:04 +01002779/* S5PV210, Exynos */
Tomasz Figab94ba032015-01-23 14:47:41 +01002780static struct samsung_early_console_data s5pv210_early_console_data = {
2781 .txfull_mask = S5PV210_UFSTAT_TXFULL,
2782};
2783
2784static int __init s5pv210_early_console_setup(struct earlycon_device *device,
2785 const char *opt)
2786{
2787 device->port.private_data = &s5pv210_early_console_data;
2788 return samsung_early_console_setup(device, opt);
2789}
Krzysztof Kozlowski695b8472020-06-17 17:28:56 +02002790
Tomasz Figab94ba032015-01-23 14:47:41 +01002791OF_EARLYCON_DECLARE(s5pv210, "samsung,s5pv210-uart",
2792 s5pv210_early_console_setup);
2793OF_EARLYCON_DECLARE(exynos4210, "samsung,exynos4210-uart",
2794 s5pv210_early_console_setup);
Marek Szyprowskic3bda292015-02-02 10:47:35 +01002795#endif
Tomasz Figab94ba032015-01-23 14:47:41 +01002796
Thomas Abrahamda121502011-11-02 19:23:25 +09002797MODULE_ALIAS("platform:samsung-uart");
Ben Dooksb4975492008-07-03 12:32:51 +01002798MODULE_DESCRIPTION("Samsung SoC Serial port driver");
2799MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
2800MODULE_LICENSE("GPL v2");