1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17#if defined(CONFIG_SERIAL_XILINX_PS_UART_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
18#define SUPPORT_SYSRQ
19#endif
20
21#include <linux/platform_device.h>
22#include <linux/serial.h>
23#include <linux/console.h>
24#include <linux/serial_core.h>
25#include <linux/slab.h>
26#include <linux/tty.h>
27#include <linux/tty_flip.h>
28#include <linux/clk.h>
29#include <linux/irq.h>
30#include <linux/io.h>
31#include <linux/of.h>
32#include <linux/module.h>
33
34#define CDNS_UART_TTY_NAME "ttyPS"
35#define CDNS_UART_NAME "xuartps"
36#define CDNS_UART_MAJOR 0
37#define CDNS_UART_MINOR 0
38#define CDNS_UART_NR_PORTS 2
39#define CDNS_UART_FIFO_SIZE 64
40#define CDNS_UART_REGISTER_SPACE 0x1000
41
42#define cdns_uart_readl(offset) ioread32(port->membase + offset)
43#define cdns_uart_writel(val, offset) iowrite32(val, port->membase + offset)
44
45
46static int rx_trigger_level = 56;
47module_param(rx_trigger_level, uint, S_IRUGO);
48MODULE_PARM_DESC(rx_trigger_level, "Rx trigger level, 1-63 bytes");
49
50
51static int rx_timeout = 10;
52module_param(rx_timeout, uint, S_IRUGO);
53MODULE_PARM_DESC(rx_timeout, "Rx timeout, 1-255");
54
55
56#define CDNS_UART_CR_OFFSET 0x00
57#define CDNS_UART_MR_OFFSET 0x04
58#define CDNS_UART_IER_OFFSET 0x08
59#define CDNS_UART_IDR_OFFSET 0x0C
60#define CDNS_UART_IMR_OFFSET 0x10
61#define CDNS_UART_ISR_OFFSET 0x14
62#define CDNS_UART_BAUDGEN_OFFSET 0x18
63#define CDNS_UART_RXTOUT_OFFSET 0x1C
64#define CDNS_UART_RXWM_OFFSET 0x20
65#define CDNS_UART_MODEMCR_OFFSET 0x24
66#define CDNS_UART_MODEMSR_OFFSET 0x28
67#define CDNS_UART_SR_OFFSET 0x2C
68#define CDNS_UART_FIFO_OFFSET 0x30
69#define CDNS_UART_BAUDDIV_OFFSET 0x34
70#define CDNS_UART_FLOWDEL_OFFSET 0x38
71#define CDNS_UART_IRRX_PWIDTH_OFFSET 0x3C
72#define CDNS_UART_IRTX_PWIDTH_OFFSET 0x40
73#define CDNS_UART_TXWM_OFFSET 0x44
74#define CDNS_UART_RXBS_OFFSET 0x48
75
76
77#define CDNS_UART_CR_STOPBRK 0x00000100
78#define CDNS_UART_CR_STARTBRK 0x00000080
79#define CDNS_UART_CR_TX_DIS 0x00000020
80#define CDNS_UART_CR_TX_EN 0x00000010
81#define CDNS_UART_CR_RX_DIS 0x00000008
82#define CDNS_UART_CR_RX_EN 0x00000004
83#define CDNS_UART_CR_TXRST 0x00000002
84#define CDNS_UART_CR_RXRST 0x00000001
85#define CDNS_UART_CR_RST_TO 0x00000040
86
87
88#define CDNS_UART_RXBS_PARITY 0x00000001
89#define CDNS_UART_RXBS_FRAMING 0x00000002
90#define CDNS_UART_RXBS_BRK 0x00000004
91
92
93
94
95
96
97
98#define CDNS_UART_MR_CLKSEL 0x00000001
99#define CDNS_UART_MR_CHMODE_L_LOOP 0x00000200
100#define CDNS_UART_MR_CHMODE_NORM 0x00000000
101
102#define CDNS_UART_MR_STOPMODE_2_BIT 0x00000080
103#define CDNS_UART_MR_STOPMODE_1_BIT 0x00000000
104
105#define CDNS_UART_MR_PARITY_NONE 0x00000020
106#define CDNS_UART_MR_PARITY_MARK 0x00000018
107#define CDNS_UART_MR_PARITY_SPACE 0x00000010
108#define CDNS_UART_MR_PARITY_ODD 0x00000008
109#define CDNS_UART_MR_PARITY_EVEN 0x00000000
110
111#define CDNS_UART_MR_CHARLEN_6_BIT 0x00000006
112#define CDNS_UART_MR_CHARLEN_7_BIT 0x00000004
113#define CDNS_UART_MR_CHARLEN_8_BIT 0x00000000
114
115
116
117
118
119
120
121
122
123
124
125
126#define CDNS_UART_IXR_TOUT 0x00000100
127#define CDNS_UART_IXR_PARITY 0x00000080
128#define CDNS_UART_IXR_FRAMING 0x00000040
129#define CDNS_UART_IXR_OVERRUN 0x00000020
130#define CDNS_UART_IXR_TXFULL 0x00000010
131#define CDNS_UART_IXR_TXEMPTY 0x00000008
132#define CDNS_UART_ISR_RXEMPTY 0x00000002
133#define CDNS_UART_IXR_RXTRIG 0x00000001
134#define CDNS_UART_IXR_RXFULL 0x00000004
135#define CDNS_UART_IXR_RXEMPTY 0x00000002
136#define CDNS_UART_IXR_MASK 0x00003FFF
137
138
139#define CDNS_UART_IXR_BRK 0x00002000
140
141#define CDNS_UART_RXBS_SUPPORT BIT(1)
142
143
144
145
146
147#define CDNS_UART_MODEMCR_FCM 0x00000020
148#define CDNS_UART_MODEMCR_RTS 0x00000002
149#define CDNS_UART_MODEMCR_DTR 0x00000001
150
151
152
153
154
155
156
157#define CDNS_UART_SR_RXEMPTY 0x00000002
158#define CDNS_UART_SR_TXEMPTY 0x00000008
159#define CDNS_UART_SR_TXFULL 0x00000010
160#define CDNS_UART_SR_RXTRIG 0x00000001
161
162
163#define CDNS_UART_BDIV_MIN 4
164#define CDNS_UART_BDIV_MAX 255
165#define CDNS_UART_CD_MAX 65535
166
167
168
169
170
171
172
173
174
175struct cdns_uart {
176 struct uart_port *port;
177 struct clk *uartclk;
178 struct clk *pclk;
179 unsigned int baud;
180 struct notifier_block clk_rate_change_nb;
181 u32 quirks;
182};
183struct cdns_platform_data {
184 u32 quirks;
185};
186
187#define to_cdns_uart(_nb) container_of(_nb, struct cdns_uart, \
188 clk_rate_change_nb);
189
190
191
192
193
194
195static void cdns_uart_handle_tx(void *dev_id)
196{
197 struct uart_port *port = (struct uart_port *)dev_id;
198 unsigned int numbytes;
199
200 if (uart_circ_empty(&port->state->xmit)) {
201 cdns_uart_writel(CDNS_UART_IXR_TXEMPTY, CDNS_UART_IDR_OFFSET);
202 } else {
203 numbytes = port->fifosize;
204
205 while (numbytes--) {
206 if (uart_circ_empty(&port->state->xmit))
207 break;
208
209
210
211
212
213 cdns_uart_writel(
214 port->state->xmit.buf[port->state->xmit.
215 tail], CDNS_UART_FIFO_OFFSET);
216
217 port->icount.tx++;
218
219
220
221
222
223 port->state->xmit.tail =
224 (port->state->xmit.tail + 1) &
225 (UART_XMIT_SIZE - 1);
226 }
227
228 if (uart_circ_chars_pending(
229 &port->state->xmit) < WAKEUP_CHARS)
230 uart_write_wakeup(port);
231 }
232}
233
234
235
236
237
238
239
240static void cdns_uart_handle_rx(void *dev_id, unsigned int isrstatus)
241{
242 struct uart_port *port = (struct uart_port *)dev_id;
243 struct cdns_uart *cdns_uart = port->private_data;
244 unsigned int data;
245 unsigned int rxbs_status = 0;
246 unsigned int status_mask;
247 unsigned int framerrprocessed = 0;
248 char status = TTY_NORMAL;
249 bool is_rxbs_support;
250
251 is_rxbs_support = cdns_uart->quirks & CDNS_UART_RXBS_SUPPORT;
252
253 while ((cdns_uart_readl(CDNS_UART_SR_OFFSET) &
254 CDNS_UART_SR_RXEMPTY) != CDNS_UART_SR_RXEMPTY) {
255 if (is_rxbs_support)
256 rxbs_status = cdns_uart_readl(CDNS_UART_RXBS_OFFSET);
257 data = cdns_uart_readl(CDNS_UART_FIFO_OFFSET);
258 port->icount.rx++;
259
260
261
262
263
264
265 if (!is_rxbs_support && (isrstatus & CDNS_UART_IXR_FRAMING)) {
266 if (!data) {
267 port->read_status_mask |= CDNS_UART_IXR_BRK;
268 framerrprocessed = 1;
269 continue;
270 }
271 }
272 if (is_rxbs_support && (rxbs_status & CDNS_UART_RXBS_BRK)) {
273 port->icount.brk++;
274 status = TTY_BREAK;
275 if (uart_handle_break(port))
276 continue;
277 }
278
279 isrstatus &= port->read_status_mask;
280 isrstatus &= ~port->ignore_status_mask;
281 status_mask = port->read_status_mask;
282 status_mask &= ~port->ignore_status_mask;
283
284 if (data &&
285 (port->read_status_mask & CDNS_UART_IXR_BRK)) {
286 port->read_status_mask &= ~CDNS_UART_IXR_BRK;
287 port->icount.brk++;
288 if (uart_handle_break(port))
289 continue;
290 }
291
292#ifdef SUPPORT_SYSRQ
293
294
295
296
297 if (port->sysrq) {
298 spin_unlock(&port->lock);
299 if (uart_handle_sysrq_char(port,
300 (unsigned char)data)) {
301 spin_lock(&port->lock);
302 continue;
303 }
304 spin_lock(&port->lock);
305 }
306#endif
307 if (is_rxbs_support) {
308 if ((rxbs_status & CDNS_UART_RXBS_PARITY)
309 && (status_mask & CDNS_UART_IXR_PARITY)) {
310 port->icount.parity++;
311 status = TTY_PARITY;
312 }
313 if ((rxbs_status & CDNS_UART_RXBS_FRAMING)
314 && (status_mask & CDNS_UART_IXR_PARITY)) {
315 port->icount.frame++;
316 status = TTY_FRAME;
317 }
318 } else {
319 if (isrstatus & CDNS_UART_IXR_PARITY) {
320 port->icount.parity++;
321 status = TTY_PARITY;
322 }
323 if ((isrstatus & CDNS_UART_IXR_FRAMING) &&
324 !framerrprocessed) {
325 port->icount.frame++;
326 status = TTY_FRAME;
327 }
328 }
329 if (isrstatus & CDNS_UART_IXR_OVERRUN) {
330 port->icount.overrun++;
331 tty_insert_flip_char(&port->state->port, 0,
332 TTY_OVERRUN);
333 }
334 tty_insert_flip_char(&port->state->port, data, status);
335 isrstatus = 0;
336 }
337 spin_unlock(&port->lock);
338 tty_flip_buffer_push(&port->state->port);
339 spin_lock(&port->lock);
340}
341
342
343
344
345
346
347
348
349static irqreturn_t cdns_uart_isr(int irq, void *dev_id)
350{
351 struct uart_port *port = (struct uart_port *)dev_id;
352 unsigned int isrstatus;
353
354 spin_lock(&port->lock);
355
356
357
358
359 isrstatus = cdns_uart_readl(CDNS_UART_ISR_OFFSET);
360 cdns_uart_writel(isrstatus, CDNS_UART_ISR_OFFSET);
361
362 if (isrstatus & CDNS_UART_IXR_TXEMPTY) {
363 cdns_uart_handle_tx(dev_id);
364 isrstatus &= ~CDNS_UART_IXR_TXEMPTY;
365 }
366 if (isrstatus & CDNS_UART_IXR_MASK)
367 cdns_uart_handle_rx(dev_id, isrstatus);
368
369 spin_unlock(&port->lock);
370 return IRQ_HANDLED;
371}
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393static unsigned int cdns_uart_calc_baud_divs(unsigned int clk,
394 unsigned int baud, u32 *rbdiv, u32 *rcd, int *div8)
395{
396 u32 cd, bdiv;
397 unsigned int calc_baud;
398 unsigned int bestbaud = 0;
399 unsigned int bauderror;
400 unsigned int besterror = ~0;
401
402 if (baud < clk / ((CDNS_UART_BDIV_MAX + 1) * CDNS_UART_CD_MAX)) {
403 *div8 = 1;
404 clk /= 8;
405 } else {
406 *div8 = 0;
407 }
408
409 for (bdiv = CDNS_UART_BDIV_MIN; bdiv <= CDNS_UART_BDIV_MAX; bdiv++) {
410 cd = DIV_ROUND_CLOSEST(clk, baud * (bdiv + 1));
411 if (cd < 1 || cd > CDNS_UART_CD_MAX)
412 continue;
413
414 calc_baud = clk / (cd * (bdiv + 1));
415
416 if (baud > calc_baud)
417 bauderror = baud - calc_baud;
418 else
419 bauderror = calc_baud - baud;
420
421 if (besterror > bauderror) {
422 *rbdiv = bdiv;
423 *rcd = cd;
424 bestbaud = calc_baud;
425 besterror = bauderror;
426 }
427 }
428
429 if (((besterror * 100) / baud) < 3)
430 bestbaud = baud;
431
432 return bestbaud;
433}
434
435
436
437
438
439
440
441
442static unsigned int cdns_uart_set_baud_rate(struct uart_port *port,
443 unsigned int baud)
444{
445 unsigned int calc_baud;
446 u32 cd = 0, bdiv = 0;
447 u32 mreg;
448 int div8;
449 struct cdns_uart *cdns_uart = port->private_data;
450
451 calc_baud = cdns_uart_calc_baud_divs(port->uartclk, baud, &bdiv, &cd,
452 &div8);
453
454
455 mreg = cdns_uart_readl(CDNS_UART_MR_OFFSET);
456 if (div8)
457 mreg |= CDNS_UART_MR_CLKSEL;
458 else
459 mreg &= ~CDNS_UART_MR_CLKSEL;
460 cdns_uart_writel(mreg, CDNS_UART_MR_OFFSET);
461 cdns_uart_writel(cd, CDNS_UART_BAUDGEN_OFFSET);
462 cdns_uart_writel(bdiv, CDNS_UART_BAUDDIV_OFFSET);
463 cdns_uart->baud = baud;
464
465 return calc_baud;
466}
467
468#ifdef CONFIG_COMMON_CLK
469
470
471
472
473
474
475
476static int cdns_uart_clk_notifier_cb(struct notifier_block *nb,
477 unsigned long event, void *data)
478{
479 u32 ctrl_reg;
480 struct uart_port *port;
481 int locked = 0;
482 struct clk_notifier_data *ndata = data;
483 unsigned long flags = 0;
484 struct cdns_uart *cdns_uart = to_cdns_uart(nb);
485
486 port = cdns_uart->port;
487 if (port->suspended)
488 return NOTIFY_OK;
489
490 switch (event) {
491 case PRE_RATE_CHANGE:
492 {
493 u32 bdiv, cd;
494 int div8;
495
496
497
498
499
500 if (!cdns_uart_calc_baud_divs(ndata->new_rate, cdns_uart->baud,
501 &bdiv, &cd, &div8)) {
502 dev_warn(port->dev, "clock rate change rejected\n");
503 return NOTIFY_BAD;
504 }
505
506 spin_lock_irqsave(&cdns_uart->port->lock, flags);
507
508
509 ctrl_reg = cdns_uart_readl(CDNS_UART_CR_OFFSET);
510 ctrl_reg |= CDNS_UART_CR_TX_DIS | CDNS_UART_CR_RX_DIS;
511 cdns_uart_writel(ctrl_reg, CDNS_UART_CR_OFFSET);
512
513 spin_unlock_irqrestore(&cdns_uart->port->lock, flags);
514
515 return NOTIFY_OK;
516 }
517 case POST_RATE_CHANGE:
518
519
520
521
522
523 spin_lock_irqsave(&cdns_uart->port->lock, flags);
524
525 locked = 1;
526 port->uartclk = ndata->new_rate;
527
528 cdns_uart->baud = cdns_uart_set_baud_rate(cdns_uart->port,
529 cdns_uart->baud);
530
531 case ABORT_RATE_CHANGE:
532 if (!locked)
533 spin_lock_irqsave(&cdns_uart->port->lock, flags);
534
535
536 ctrl_reg = cdns_uart_readl(CDNS_UART_CR_OFFSET);
537 ctrl_reg |= CDNS_UART_CR_TXRST | CDNS_UART_CR_RXRST;
538 cdns_uart_writel(ctrl_reg, CDNS_UART_CR_OFFSET);
539
540 while (cdns_uart_readl(CDNS_UART_CR_OFFSET) &
541 (CDNS_UART_CR_TXRST | CDNS_UART_CR_RXRST))
542 cpu_relax();
543
544
545
546
547
548
549 cdns_uart_writel(rx_timeout, CDNS_UART_RXTOUT_OFFSET);
550 ctrl_reg = cdns_uart_readl(CDNS_UART_CR_OFFSET);
551 ctrl_reg &= ~(CDNS_UART_CR_TX_DIS | CDNS_UART_CR_RX_DIS);
552 ctrl_reg |= CDNS_UART_CR_TX_EN | CDNS_UART_CR_RX_EN;
553 cdns_uart_writel(ctrl_reg, CDNS_UART_CR_OFFSET);
554
555 spin_unlock_irqrestore(&cdns_uart->port->lock, flags);
556
557 return NOTIFY_OK;
558 default:
559 return NOTIFY_DONE;
560 }
561}
562#endif
563
564
565
566
567
568static void cdns_uart_start_tx(struct uart_port *port)
569{
570 unsigned int status, numbytes = port->fifosize;
571
572 if (uart_circ_empty(&port->state->xmit) || uart_tx_stopped(port))
573 return;
574
575 status = cdns_uart_readl(CDNS_UART_CR_OFFSET);
576
577
578
579 cdns_uart_writel((status & ~CDNS_UART_CR_TX_DIS) | CDNS_UART_CR_TX_EN,
580 CDNS_UART_CR_OFFSET);
581
582 while (numbytes-- && ((cdns_uart_readl(CDNS_UART_SR_OFFSET) &
583 CDNS_UART_SR_TXFULL)) != CDNS_UART_SR_TXFULL) {
584
585 if (uart_circ_empty(&port->state->xmit))
586 break;
587
588
589
590
591 cdns_uart_writel(
592 port->state->xmit.buf[port->state->xmit.tail],
593 CDNS_UART_FIFO_OFFSET);
594 port->icount.tx++;
595
596
597
598
599 port->state->xmit.tail = (port->state->xmit.tail + 1) &
600 (UART_XMIT_SIZE - 1);
601 }
602 cdns_uart_writel(CDNS_UART_IXR_TXEMPTY, CDNS_UART_ISR_OFFSET);
603
604 cdns_uart_writel(CDNS_UART_IXR_TXEMPTY, CDNS_UART_IER_OFFSET);
605
606 if (uart_circ_chars_pending(&port->state->xmit) < WAKEUP_CHARS)
607 uart_write_wakeup(port);
608}
609
610
611
612
613
614static void cdns_uart_stop_tx(struct uart_port *port)
615{
616 unsigned int regval;
617
618 regval = cdns_uart_readl(CDNS_UART_CR_OFFSET);
619 regval |= CDNS_UART_CR_TX_DIS;
620
621 cdns_uart_writel(regval, CDNS_UART_CR_OFFSET);
622}
623
624
625
626
627
628static void cdns_uart_stop_rx(struct uart_port *port)
629{
630 unsigned int regval;
631
632 regval = cdns_uart_readl(CDNS_UART_CR_OFFSET);
633 regval |= CDNS_UART_CR_RX_DIS;
634
635 cdns_uart_writel(regval, CDNS_UART_CR_OFFSET);
636}
637
638
639
640
641
642
643
644static unsigned int cdns_uart_tx_empty(struct uart_port *port)
645{
646 unsigned int status;
647
648 status = cdns_uart_readl(CDNS_UART_SR_OFFSET) & CDNS_UART_SR_TXEMPTY;
649 return status ? TIOCSER_TEMT : 0;
650}
651
652
653
654
655
656
657
658static void cdns_uart_break_ctl(struct uart_port *port, int ctl)
659{
660 unsigned int status;
661 unsigned long flags;
662
663 spin_lock_irqsave(&port->lock, flags);
664
665 status = cdns_uart_readl(CDNS_UART_CR_OFFSET);
666
667 if (ctl == -1)
668 cdns_uart_writel(CDNS_UART_CR_STARTBRK | status,
669 CDNS_UART_CR_OFFSET);
670 else {
671 if ((status & CDNS_UART_CR_STOPBRK) == 0)
672 cdns_uart_writel(CDNS_UART_CR_STOPBRK | status,
673 CDNS_UART_CR_OFFSET);
674 }
675 spin_unlock_irqrestore(&port->lock, flags);
676}
677
678
679
680
681
682
683
684
685static void cdns_uart_set_termios(struct uart_port *port,
686 struct ktermios *termios, struct ktermios *old)
687{
688 unsigned int cval = 0;
689 unsigned int baud, minbaud, maxbaud;
690 unsigned long flags;
691 unsigned int ctrl_reg, mode_reg;
692
693 spin_lock_irqsave(&port->lock, flags);
694
695
696 if (!(cdns_uart_readl(CDNS_UART_CR_OFFSET) & CDNS_UART_CR_TX_DIS)) {
697 while (!(cdns_uart_readl(CDNS_UART_SR_OFFSET) &
698 CDNS_UART_SR_TXEMPTY)) {
699 cpu_relax();
700 }
701 }
702
703
704 ctrl_reg = cdns_uart_readl(CDNS_UART_CR_OFFSET);
705 ctrl_reg |= CDNS_UART_CR_TX_DIS | CDNS_UART_CR_RX_DIS;
706 cdns_uart_writel(ctrl_reg, CDNS_UART_CR_OFFSET);
707
708
709
710
711
712
713 minbaud = port->uartclk /
714 ((CDNS_UART_BDIV_MAX + 1) * CDNS_UART_CD_MAX * 8);
715 maxbaud = port->uartclk / (CDNS_UART_BDIV_MIN + 1);
716 baud = uart_get_baud_rate(port, termios, old, minbaud, maxbaud);
717 baud = cdns_uart_set_baud_rate(port, baud);
718 if (tty_termios_baud_rate(termios))
719 tty_termios_encode_baud_rate(termios, baud, baud);
720
721
722 uart_update_timeout(port, termios->c_cflag, baud);
723
724
725 ctrl_reg = cdns_uart_readl(CDNS_UART_CR_OFFSET);
726 ctrl_reg |= CDNS_UART_CR_TXRST | CDNS_UART_CR_RXRST;
727 cdns_uart_writel(ctrl_reg, CDNS_UART_CR_OFFSET);
728
729 while (readl(port->membase + CDNS_UART_CR_OFFSET) &
730 (CDNS_UART_CR_TXRST | CDNS_UART_CR_RXRST))
731 cpu_relax();
732
733
734
735
736 ctrl_reg = cdns_uart_readl(CDNS_UART_CR_OFFSET);
737 ctrl_reg &= ~(CDNS_UART_CR_TX_DIS | CDNS_UART_CR_RX_DIS);
738 ctrl_reg |= CDNS_UART_CR_TX_EN | CDNS_UART_CR_RX_EN;
739 cdns_uart_writel(ctrl_reg, CDNS_UART_CR_OFFSET);
740
741 cdns_uart_writel(rx_timeout, CDNS_UART_RXTOUT_OFFSET);
742
743 port->read_status_mask = CDNS_UART_IXR_TXEMPTY | CDNS_UART_IXR_RXTRIG |
744 CDNS_UART_IXR_OVERRUN | CDNS_UART_IXR_TOUT;
745 port->ignore_status_mask = 0;
746
747 if (termios->c_iflag & INPCK)
748 port->read_status_mask |= CDNS_UART_IXR_PARITY |
749 CDNS_UART_IXR_FRAMING;
750
751 if (termios->c_iflag & IGNPAR)
752 port->ignore_status_mask |= CDNS_UART_IXR_PARITY |
753 CDNS_UART_IXR_FRAMING;
754
755
756 if ((termios->c_cflag & CREAD) == 0)
757 port->ignore_status_mask |= CDNS_UART_IXR_RXTRIG |
758 CDNS_UART_IXR_TOUT | CDNS_UART_IXR_PARITY |
759 CDNS_UART_IXR_FRAMING | CDNS_UART_IXR_OVERRUN;
760
761 mode_reg = cdns_uart_readl(CDNS_UART_MR_OFFSET);
762
763
764 switch (termios->c_cflag & CSIZE) {
765 case CS6:
766 cval |= CDNS_UART_MR_CHARLEN_6_BIT;
767 break;
768 case CS7:
769 cval |= CDNS_UART_MR_CHARLEN_7_BIT;
770 break;
771 default:
772 case CS8:
773 cval |= CDNS_UART_MR_CHARLEN_8_BIT;
774 termios->c_cflag &= ~CSIZE;
775 termios->c_cflag |= CS8;
776 break;
777 }
778
779
780 if (termios->c_cflag & CSTOPB)
781 cval |= CDNS_UART_MR_STOPMODE_2_BIT;
782 else
783 cval |= CDNS_UART_MR_STOPMODE_1_BIT;
784
785 if ((termios->c_cflag & PARENB) || (termios->c_cflag & PARODD)) {
786
787 if (termios->c_cflag & CMSPAR) {
788 if (termios->c_cflag & PARODD)
789 cval |= CDNS_UART_MR_PARITY_MARK;
790 else
791 cval |= CDNS_UART_MR_PARITY_SPACE;
792 } else {
793 if (termios->c_cflag & PARODD)
794 cval |= CDNS_UART_MR_PARITY_ODD;
795 else
796 cval |= CDNS_UART_MR_PARITY_EVEN;
797 }
798 } else {
799 cval |= CDNS_UART_MR_PARITY_NONE;
800 }
801 cval |= mode_reg & 1;
802 cdns_uart_writel(cval, CDNS_UART_MR_OFFSET);
803
804 spin_unlock_irqrestore(&port->lock, flags);
805}
806
807
808
809
810
811
812
813static int cdns_uart_startup(struct uart_port *port)
814{
815 struct cdns_uart *cdns_uart = port->private_data;
816 bool is_brk_support;
817 unsigned int retval = 0, status = 0;
818
819 is_brk_support = cdns_uart->quirks & CDNS_UART_RXBS_SUPPORT;
820 retval = request_irq(port->irq, cdns_uart_isr, 0, CDNS_UART_NAME,
821 (void *)port);
822 if (retval)
823 return retval;
824
825
826 cdns_uart_writel(CDNS_UART_CR_TX_DIS | CDNS_UART_CR_RX_DIS,
827 CDNS_UART_CR_OFFSET);
828
829
830
831
832 cdns_uart_writel(CDNS_UART_CR_TXRST | CDNS_UART_CR_RXRST,
833 CDNS_UART_CR_OFFSET);
834
835 while (readl(port->membase + CDNS_UART_CR_OFFSET) &
836 (CDNS_UART_CR_TXRST | CDNS_UART_CR_RXRST))
837 cpu_relax();
838
839 status = cdns_uart_readl(CDNS_UART_CR_OFFSET);
840
841
842
843
844 cdns_uart_writel((status & ~(CDNS_UART_CR_TX_DIS | CDNS_UART_CR_RX_DIS))
845 | (CDNS_UART_CR_TX_EN | CDNS_UART_CR_RX_EN |
846 CDNS_UART_CR_STOPBRK), CDNS_UART_CR_OFFSET);
847
848
849
850
851 cdns_uart_writel(CDNS_UART_MR_CHMODE_NORM | CDNS_UART_MR_STOPMODE_1_BIT
852 | CDNS_UART_MR_PARITY_NONE | CDNS_UART_MR_CHARLEN_8_BIT,
853 CDNS_UART_MR_OFFSET);
854
855
856
857
858
859 cdns_uart_writel(rx_trigger_level, CDNS_UART_RXWM_OFFSET);
860
861
862
863
864
865 cdns_uart_writel(rx_timeout, CDNS_UART_RXTOUT_OFFSET);
866
867
868 cdns_uart_writel(cdns_uart_readl(CDNS_UART_ISR_OFFSET),
869 CDNS_UART_ISR_OFFSET);
870
871
872
873
874
875
876
877
878
879
880
881
882 cdns_uart_writel(CDNS_UART_IXR_TXEMPTY |
883 CDNS_UART_IXR_FRAMING | CDNS_UART_IXR_OVERRUN |
884 CDNS_UART_IXR_RXTRIG | CDNS_UART_IXR_TOUT,
885 CDNS_UART_IER_OFFSET);
886 if (is_brk_support) {
887 status = cdns_uart_readl(CDNS_UART_IMR_OFFSET);
888 cdns_uart_writel((status | CDNS_UART_IXR_BRK),
889 CDNS_UART_IER_OFFSET);
890 }
891
892 return retval;
893}
894
895
896
897
898
899static void cdns_uart_shutdown(struct uart_port *port)
900{
901 int status;
902
903
904 status = cdns_uart_readl(CDNS_UART_IMR_OFFSET);
905 cdns_uart_writel(status, CDNS_UART_IDR_OFFSET);
906
907
908 cdns_uart_writel(CDNS_UART_CR_TX_DIS | CDNS_UART_CR_RX_DIS,
909 CDNS_UART_CR_OFFSET);
910 free_irq(port->irq, port);
911}
912
913
914
915
916
917
918
919static const char *cdns_uart_type(struct uart_port *port)
920{
921 return port->type == PORT_XUARTPS ? CDNS_UART_NAME : NULL;
922}
923
924
925
926
927
928
929
930
931static int cdns_uart_verify_port(struct uart_port *port,
932 struct serial_struct *ser)
933{
934 if (ser->type != PORT_UNKNOWN && ser->type != PORT_XUARTPS)
935 return -EINVAL;
936 if (port->irq != ser->irq)
937 return -EINVAL;
938 if (ser->io_type != UPIO_MEM)
939 return -EINVAL;
940 if (port->iobase != ser->port)
941 return -EINVAL;
942 if (ser->hub6 != 0)
943 return -EINVAL;
944 return 0;
945}
946
947
948
949
950
951
952
953
954
955static int cdns_uart_request_port(struct uart_port *port)
956{
957 if (!request_mem_region(port->mapbase, CDNS_UART_REGISTER_SPACE,
958 CDNS_UART_NAME)) {
959 return -ENOMEM;
960 }
961
962 port->membase = ioremap(port->mapbase, CDNS_UART_REGISTER_SPACE);
963 if (!port->membase) {
964 dev_err(port->dev, "Unable to map registers\n");
965 release_mem_region(port->mapbase, CDNS_UART_REGISTER_SPACE);
966 return -ENOMEM;
967 }
968 return 0;
969}
970
971
972
973
974
975
976
977
978static void cdns_uart_release_port(struct uart_port *port)
979{
980 release_mem_region(port->mapbase, CDNS_UART_REGISTER_SPACE);
981 iounmap(port->membase);
982 port->membase = NULL;
983}
984
985
986
987
988
989
990static void cdns_uart_config_port(struct uart_port *port, int flags)
991{
992 if (flags & UART_CONFIG_TYPE && cdns_uart_request_port(port) == 0)
993 port->type = PORT_XUARTPS;
994}
995
996
997
998
999
1000
1001
1002static unsigned int cdns_uart_get_mctrl(struct uart_port *port)
1003{
1004 return TIOCM_CTS | TIOCM_DSR | TIOCM_CAR;
1005}
1006
1007static void cdns_uart_set_mctrl(struct uart_port *port, unsigned int mctrl)
1008{
1009 u32 val;
1010
1011 val = cdns_uart_readl(CDNS_UART_MODEMCR_OFFSET);
1012
1013 val &= ~(CDNS_UART_MODEMCR_RTS | CDNS_UART_MODEMCR_DTR);
1014
1015 if (mctrl & TIOCM_RTS)
1016 val |= CDNS_UART_MODEMCR_RTS;
1017 if (mctrl & TIOCM_DTR)
1018 val |= CDNS_UART_MODEMCR_DTR;
1019
1020 cdns_uart_writel(val, CDNS_UART_MODEMCR_OFFSET);
1021}
1022
1023#ifdef CONFIG_CONSOLE_POLL
1024static int cdns_uart_poll_get_char(struct uart_port *port)
1025{
1026 u32 imr;
1027 int c;
1028
1029
1030 imr = cdns_uart_readl(CDNS_UART_IMR_OFFSET);
1031 cdns_uart_writel(imr, CDNS_UART_IDR_OFFSET);
1032
1033
1034 if (cdns_uart_readl(CDNS_UART_SR_OFFSET) & CDNS_UART_SR_RXEMPTY)
1035 c = NO_POLL_CHAR;
1036 else
1037 c = (unsigned char) cdns_uart_readl(CDNS_UART_FIFO_OFFSET);
1038
1039
1040 cdns_uart_writel(imr, CDNS_UART_IER_OFFSET);
1041
1042 return c;
1043}
1044
1045static void cdns_uart_poll_put_char(struct uart_port *port, unsigned char c)
1046{
1047 u32 imr;
1048
1049
1050 imr = cdns_uart_readl(CDNS_UART_IMR_OFFSET);
1051 cdns_uart_writel(imr, CDNS_UART_IDR_OFFSET);
1052
1053
1054 while (!(cdns_uart_readl(CDNS_UART_SR_OFFSET) & CDNS_UART_SR_TXEMPTY))
1055 cpu_relax();
1056
1057
1058 cdns_uart_writel(c, CDNS_UART_FIFO_OFFSET);
1059
1060
1061 while (!(cdns_uart_readl(CDNS_UART_SR_OFFSET) & CDNS_UART_SR_TXEMPTY))
1062 cpu_relax();
1063
1064
1065 cdns_uart_writel(imr, CDNS_UART_IER_OFFSET);
1066
1067 return;
1068}
1069#endif
1070
1071static void cdns_uart_pm(struct uart_port *port, unsigned int state,
1072 unsigned int oldstate)
1073{
1074 struct cdns_uart *cdns_uart = port->private_data;
1075
1076 switch (state) {
1077 case UART_PM_STATE_OFF:
1078 clk_disable(cdns_uart->uartclk);
1079 clk_disable(cdns_uart->pclk);
1080 break;
1081 default:
1082 clk_enable(cdns_uart->pclk);
1083 clk_enable(cdns_uart->uartclk);
1084 break;
1085 }
1086}
1087
1088static struct uart_ops cdns_uart_ops = {
1089 .set_mctrl = cdns_uart_set_mctrl,
1090 .get_mctrl = cdns_uart_get_mctrl,
1091 .start_tx = cdns_uart_start_tx,
1092 .stop_tx = cdns_uart_stop_tx,
1093 .stop_rx = cdns_uart_stop_rx,
1094 .tx_empty = cdns_uart_tx_empty,
1095 .break_ctl = cdns_uart_break_ctl,
1096 .set_termios = cdns_uart_set_termios,
1097 .startup = cdns_uart_startup,
1098 .shutdown = cdns_uart_shutdown,
1099 .pm = cdns_uart_pm,
1100 .type = cdns_uart_type,
1101 .verify_port = cdns_uart_verify_port,
1102 .request_port = cdns_uart_request_port,
1103 .release_port = cdns_uart_release_port,
1104 .config_port = cdns_uart_config_port,
1105#ifdef CONFIG_CONSOLE_POLL
1106 .poll_get_char = cdns_uart_poll_get_char,
1107 .poll_put_char = cdns_uart_poll_put_char,
1108#endif
1109};
1110
1111static struct uart_port cdns_uart_port[CDNS_UART_NR_PORTS];
1112
1113
1114
1115
1116
1117
1118
1119static struct uart_port *cdns_uart_get_port(int id)
1120{
1121 struct uart_port *port;
1122
1123
1124 if (cdns_uart_port[id].mapbase != 0) {
1125
1126 for (id = 0; id < CDNS_UART_NR_PORTS; id++)
1127 if (cdns_uart_port[id].mapbase == 0)
1128 break;
1129 }
1130
1131 if (id >= CDNS_UART_NR_PORTS)
1132 return NULL;
1133
1134 port = &cdns_uart_port[id];
1135
1136
1137 spin_lock_init(&port->lock);
1138 port->membase = NULL;
1139 port->irq = 0;
1140 port->type = PORT_UNKNOWN;
1141 port->iotype = UPIO_MEM32;
1142 port->flags = UPF_BOOT_AUTOCONF;
1143 port->ops = &cdns_uart_ops;
1144 port->fifosize = CDNS_UART_FIFO_SIZE;
1145 port->line = id;
1146 port->dev = NULL;
1147 return port;
1148}
1149
1150#ifdef CONFIG_SERIAL_XILINX_PS_UART_CONSOLE
1151
1152
1153
1154
1155static void cdns_uart_console_wait_tx(struct uart_port *port)
1156{
1157 while ((cdns_uart_readl(CDNS_UART_SR_OFFSET) & CDNS_UART_SR_TXEMPTY)
1158 != CDNS_UART_SR_TXEMPTY)
1159 barrier();
1160}
1161
1162
1163
1164
1165
1166
1167static void cdns_uart_console_putchar(struct uart_port *port, int ch)
1168{
1169 cdns_uart_console_wait_tx(port);
1170 cdns_uart_writel(ch, CDNS_UART_FIFO_OFFSET);
1171}
1172
1173static void __init cdns_early_write(struct console *con, const char *s,
1174 unsigned n)
1175{
1176 struct earlycon_device *dev = con->data;
1177
1178 uart_console_write(&dev->port, s, n, cdns_uart_console_putchar);
1179}
1180
1181static int __init cdns_early_console_setup(struct earlycon_device *device,
1182 const char *opt)
1183{
1184 if (!device->port.membase)
1185 return -ENODEV;
1186
1187 device->con->write = cdns_early_write;
1188
1189 return 0;
1190}
1191EARLYCON_DECLARE(cdns, cdns_early_console_setup);
1192OF_EARLYCON_DECLARE(cdns_a, "xlnx,xuartps", cdns_early_console_setup);
1193OF_EARLYCON_DECLARE(cdns_b, "cdns,uart-r1p8", cdns_early_console_setup);
1194OF_EARLYCON_DECLARE(cdns_c, "cdns,uart-r1p12", cdns_early_console_setup);
1195
1196
1197
1198
1199
1200
1201
1202static void cdns_uart_console_write(struct console *co, const char *s,
1203 unsigned int count)
1204{
1205 struct uart_port *port = &cdns_uart_port[co->index];
1206 unsigned long flags;
1207 unsigned int imr, ctrl;
1208 int locked = 1;
1209
1210 if (oops_in_progress)
1211 locked = spin_trylock_irqsave(&port->lock, flags);
1212 else
1213 spin_lock_irqsave(&port->lock, flags);
1214
1215
1216 imr = cdns_uart_readl(CDNS_UART_IMR_OFFSET);
1217 cdns_uart_writel(imr, CDNS_UART_IDR_OFFSET);
1218
1219
1220
1221
1222
1223 ctrl = cdns_uart_readl(CDNS_UART_CR_OFFSET);
1224 cdns_uart_writel((ctrl & ~CDNS_UART_CR_TX_DIS) | CDNS_UART_CR_TX_EN,
1225 CDNS_UART_CR_OFFSET);
1226
1227 uart_console_write(port, s, count, cdns_uart_console_putchar);
1228 cdns_uart_console_wait_tx(port);
1229
1230 cdns_uart_writel(ctrl, CDNS_UART_CR_OFFSET);
1231
1232
1233 cdns_uart_writel(imr, CDNS_UART_IER_OFFSET);
1234
1235 if (locked)
1236 spin_unlock_irqrestore(&port->lock, flags);
1237}
1238
1239
1240
1241
1242
1243
1244
1245
1246static int __init cdns_uart_console_setup(struct console *co, char *options)
1247{
1248 struct uart_port *port = &cdns_uart_port[co->index];
1249 int baud = 9600;
1250 int bits = 8;
1251 int parity = 'n';
1252 int flow = 'n';
1253
1254 if (co->index < 0 || co->index >= CDNS_UART_NR_PORTS)
1255 return -EINVAL;
1256
1257 if (!port->membase) {
1258 pr_debug("console on ttyPS%i not present\n", co->index);
1259 return -ENODEV;
1260 }
1261
1262 if (options)
1263 uart_parse_options(options, &baud, &parity, &bits, &flow);
1264
1265 return uart_set_options(port, co, baud, parity, bits, flow);
1266}
1267
1268static struct uart_driver cdns_uart_uart_driver;
1269
1270static struct console cdns_uart_console = {
1271 .name = CDNS_UART_TTY_NAME,
1272 .write = cdns_uart_console_write,
1273 .device = uart_console_device,
1274 .setup = cdns_uart_console_setup,
1275 .flags = CON_PRINTBUFFER,
1276 .index = -1,
1277 .data = &cdns_uart_uart_driver,
1278};
1279
1280
1281
1282
1283
1284
1285static int __init cdns_uart_console_init(void)
1286{
1287 register_console(&cdns_uart_console);
1288 return 0;
1289}
1290
1291console_initcall(cdns_uart_console_init);
1292
1293#endif
1294
1295static struct uart_driver cdns_uart_uart_driver = {
1296 .owner = THIS_MODULE,
1297 .driver_name = CDNS_UART_NAME,
1298 .dev_name = CDNS_UART_TTY_NAME,
1299 .major = CDNS_UART_MAJOR,
1300 .minor = CDNS_UART_MINOR,
1301 .nr = CDNS_UART_NR_PORTS,
1302#ifdef CONFIG_SERIAL_XILINX_PS_UART_CONSOLE
1303 .cons = &cdns_uart_console,
1304#endif
1305};
1306
1307#ifdef CONFIG_PM_SLEEP
1308
1309
1310
1311
1312
1313
1314static int cdns_uart_suspend(struct device *device)
1315{
1316 struct uart_port *port = dev_get_drvdata(device);
1317 struct tty_struct *tty;
1318 struct device *tty_dev;
1319 int may_wake = 0;
1320
1321
1322 tty = tty_port_tty_get(&port->state->port);
1323 if (tty) {
1324 tty_dev = tty->dev;
1325 may_wake = device_may_wakeup(tty_dev);
1326 tty_kref_put(tty);
1327 }
1328
1329
1330
1331
1332
1333 uart_suspend_port(&cdns_uart_uart_driver, port);
1334 if (console_suspend_enabled && !may_wake) {
1335 struct cdns_uart *cdns_uart = port->private_data;
1336
1337 clk_disable(cdns_uart->uartclk);
1338 clk_disable(cdns_uart->pclk);
1339 } else {
1340 unsigned long flags = 0;
1341
1342 spin_lock_irqsave(&port->lock, flags);
1343
1344 while (!(cdns_uart_readl(CDNS_UART_SR_OFFSET) &
1345 CDNS_UART_SR_RXEMPTY))
1346 cdns_uart_readl(CDNS_UART_FIFO_OFFSET);
1347
1348 cdns_uart_writel(1, CDNS_UART_RXWM_OFFSET);
1349
1350 cdns_uart_writel(CDNS_UART_IXR_TOUT, CDNS_UART_IDR_OFFSET);
1351 spin_unlock_irqrestore(&port->lock, flags);
1352 }
1353
1354 return 0;
1355}
1356
1357
1358
1359
1360
1361
1362
1363static int cdns_uart_resume(struct device *device)
1364{
1365 struct uart_port *port = dev_get_drvdata(device);
1366 unsigned long flags = 0;
1367 u32 ctrl_reg;
1368 struct tty_struct *tty;
1369 struct device *tty_dev;
1370 int may_wake = 0;
1371
1372
1373 tty = tty_port_tty_get(&port->state->port);
1374 if (tty) {
1375 tty_dev = tty->dev;
1376 may_wake = device_may_wakeup(tty_dev);
1377 tty_kref_put(tty);
1378 }
1379
1380 if (console_suspend_enabled && !may_wake) {
1381 struct cdns_uart *cdns_uart = port->private_data;
1382
1383 clk_enable(cdns_uart->pclk);
1384 clk_enable(cdns_uart->uartclk);
1385
1386 spin_lock_irqsave(&port->lock, flags);
1387
1388
1389 ctrl_reg = cdns_uart_readl(CDNS_UART_CR_OFFSET);
1390 ctrl_reg |= CDNS_UART_CR_TXRST | CDNS_UART_CR_RXRST;
1391 cdns_uart_writel(ctrl_reg, CDNS_UART_CR_OFFSET);
1392 while (cdns_uart_readl(CDNS_UART_CR_OFFSET) &
1393 (CDNS_UART_CR_TXRST | CDNS_UART_CR_RXRST))
1394 cpu_relax();
1395
1396
1397 cdns_uart_writel(rx_timeout, CDNS_UART_RXTOUT_OFFSET);
1398
1399 ctrl_reg = cdns_uart_readl(CDNS_UART_CR_OFFSET);
1400 ctrl_reg &= ~(CDNS_UART_CR_TX_DIS | CDNS_UART_CR_RX_DIS);
1401 ctrl_reg |= CDNS_UART_CR_TX_EN | CDNS_UART_CR_RX_EN;
1402 cdns_uart_writel(ctrl_reg, CDNS_UART_CR_OFFSET);
1403
1404 spin_unlock_irqrestore(&port->lock, flags);
1405 } else {
1406 spin_lock_irqsave(&port->lock, flags);
1407
1408 cdns_uart_writel(rx_trigger_level, CDNS_UART_RXWM_OFFSET);
1409
1410 cdns_uart_writel(CDNS_UART_IXR_TOUT, CDNS_UART_IER_OFFSET);
1411 spin_unlock_irqrestore(&port->lock, flags);
1412 }
1413
1414 return uart_resume_port(&cdns_uart_uart_driver, port);
1415}
1416#endif
1417
1418static SIMPLE_DEV_PM_OPS(cdns_uart_dev_pm_ops, cdns_uart_suspend,
1419 cdns_uart_resume);
1420static const struct cdns_platform_data zynqmp_uart_def = {
1421 .quirks = CDNS_UART_RXBS_SUPPORT, };
1422
1423static const struct of_device_id cdns_uart_of_match[] = {
1424 { .compatible = "xlnx,xuartps", },
1425 { .compatible = "cdns,uart-r1p8", },
1426 { .compatible = "cdns,uart-r1p12", .data = &zynqmp_uart_def },
1427 {}
1428};
1429MODULE_DEVICE_TABLE(of, cdns_uart_of_match);
1430
1431
1432
1433
1434
1435
1436
1437static int cdns_uart_probe(struct platform_device *pdev)
1438{
1439 int rc, id, irq;
1440 struct uart_port *port;
1441 struct resource *res;
1442 struct cdns_uart *cdns_uart_data;
1443 const struct of_device_id *match;
1444
1445 cdns_uart_data = devm_kzalloc(&pdev->dev, sizeof(*cdns_uart_data),
1446 GFP_KERNEL);
1447 if (!cdns_uart_data)
1448 return -ENOMEM;
1449
1450 match = of_match_node(cdns_uart_of_match, pdev->dev.of_node);
1451 if (match && match->data) {
1452 const struct cdns_platform_data *data = match->data;
1453
1454 cdns_uart_data->quirks = data->quirks;
1455 }
1456
1457 cdns_uart_data->pclk = devm_clk_get(&pdev->dev, "pclk");
1458 if (IS_ERR(cdns_uart_data->pclk)) {
1459 cdns_uart_data->pclk = devm_clk_get(&pdev->dev, "aper_clk");
1460 if (!IS_ERR(cdns_uart_data->pclk))
1461 dev_err(&pdev->dev, "clock name 'aper_clk' is deprecated.\n");
1462 }
1463 if (IS_ERR(cdns_uart_data->pclk)) {
1464 dev_err(&pdev->dev, "pclk clock not found.\n");
1465 return PTR_ERR(cdns_uart_data->pclk);
1466 }
1467
1468 cdns_uart_data->uartclk = devm_clk_get(&pdev->dev, "uart_clk");
1469 if (IS_ERR(cdns_uart_data->uartclk)) {
1470 cdns_uart_data->uartclk = devm_clk_get(&pdev->dev, "ref_clk");
1471 if (!IS_ERR(cdns_uart_data->uartclk))
1472 dev_err(&pdev->dev, "clock name 'ref_clk' is deprecated.\n");
1473 }
1474 if (IS_ERR(cdns_uart_data->uartclk)) {
1475 dev_err(&pdev->dev, "uart_clk clock not found.\n");
1476 return PTR_ERR(cdns_uart_data->uartclk);
1477 }
1478
1479 rc = clk_prepare(cdns_uart_data->pclk);
1480 if (rc) {
1481 dev_err(&pdev->dev, "Unable to enable pclk clock.\n");
1482 return rc;
1483 }
1484 rc = clk_prepare(cdns_uart_data->uartclk);
1485 if (rc) {
1486 dev_err(&pdev->dev, "Unable to enable device clock.\n");
1487 goto err_out_clk_dis_pclk;
1488 }
1489
1490 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1491 if (!res) {
1492 rc = -ENODEV;
1493 goto err_out_clk_disable;
1494 }
1495
1496 irq = platform_get_irq(pdev, 0);
1497 if (irq <= 0) {
1498 rc = -ENXIO;
1499 goto err_out_clk_disable;
1500 }
1501
1502#ifdef CONFIG_COMMON_CLK
1503 cdns_uart_data->clk_rate_change_nb.notifier_call =
1504 cdns_uart_clk_notifier_cb;
1505 if (clk_notifier_register(cdns_uart_data->uartclk,
1506 &cdns_uart_data->clk_rate_change_nb))
1507 dev_warn(&pdev->dev, "Unable to register clock notifier.\n");
1508#endif
1509
1510 id = of_alias_get_id(pdev->dev.of_node, "serial");
1511 if (id < 0)
1512 id = 0;
1513
1514
1515 port = cdns_uart_get_port(id);
1516
1517 if (!port) {
1518 dev_err(&pdev->dev, "Cannot get uart_port structure\n");
1519 rc = -ENODEV;
1520 goto err_out_notif_unreg;
1521 } else {
1522
1523
1524
1525
1526 port->mapbase = res->start;
1527 port->irq = irq;
1528 port->dev = &pdev->dev;
1529 port->uartclk = clk_get_rate(cdns_uart_data->uartclk);
1530 port->private_data = cdns_uart_data;
1531 cdns_uart_data->port = port;
1532 platform_set_drvdata(pdev, port);
1533 rc = uart_add_one_port(&cdns_uart_uart_driver, port);
1534 if (rc) {
1535 dev_err(&pdev->dev,
1536 "uart_add_one_port() failed; err=%i\n", rc);
1537 goto err_out_notif_unreg;
1538 }
1539 return 0;
1540 }
1541
1542err_out_notif_unreg:
1543#ifdef CONFIG_COMMON_CLK
1544 clk_notifier_unregister(cdns_uart_data->uartclk,
1545 &cdns_uart_data->clk_rate_change_nb);
1546#endif
1547err_out_clk_disable:
1548 clk_unprepare(cdns_uart_data->uartclk);
1549err_out_clk_dis_pclk:
1550 clk_unprepare(cdns_uart_data->pclk);
1551
1552 return rc;
1553}
1554
1555
1556
1557
1558
1559
1560
1561static int cdns_uart_remove(struct platform_device *pdev)
1562{
1563 struct uart_port *port = platform_get_drvdata(pdev);
1564 struct cdns_uart *cdns_uart_data = port->private_data;
1565 int rc;
1566
1567
1568#ifdef CONFIG_COMMON_CLK
1569 clk_notifier_unregister(cdns_uart_data->uartclk,
1570 &cdns_uart_data->clk_rate_change_nb);
1571#endif
1572 rc = uart_remove_one_port(&cdns_uart_uart_driver, port);
1573 port->mapbase = 0;
1574 clk_unprepare(cdns_uart_data->uartclk);
1575 clk_unprepare(cdns_uart_data->pclk);
1576 return rc;
1577}
1578
1579static struct platform_driver cdns_uart_platform_driver = {
1580 .probe = cdns_uart_probe,
1581 .remove = cdns_uart_remove,
1582 .driver = {
1583 .name = CDNS_UART_NAME,
1584 .of_match_table = cdns_uart_of_match,
1585 .pm = &cdns_uart_dev_pm_ops,
1586 },
1587};
1588
1589static int __init cdns_uart_init(void)
1590{
1591 int retval = 0;
1592
1593
1594 retval = uart_register_driver(&cdns_uart_uart_driver);
1595 if (retval)
1596 return retval;
1597
1598
1599 retval = platform_driver_register(&cdns_uart_platform_driver);
1600 if (retval)
1601 uart_unregister_driver(&cdns_uart_uart_driver);
1602
1603 return retval;
1604}
1605
1606static void __exit cdns_uart_exit(void)
1607{
1608
1609 platform_driver_unregister(&cdns_uart_platform_driver);
1610
1611
1612 uart_unregister_driver(&cdns_uart_uart_driver);
1613}
1614
1615module_init(cdns_uart_init);
1616module_exit(cdns_uart_exit);
1617
1618MODULE_DESCRIPTION("Driver for Cadence UART");
1619MODULE_AUTHOR("Xilinx Inc.");
1620MODULE_LICENSE("GPL");
1621