0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031 #include <linux/clk.h>
0032 #include <linux/console.h>
0033 #include <linux/delay.h>
0034 #include <linux/init.h>
0035 #include <linux/io.h>
0036 #include <linux/irq.h>
0037 #include <linux/module.h>
0038 #include <linux/of.h>
0039 #include <linux/of_irq.h>
0040 #include <linux/platform_device.h>
0041 #include <linux/serial_core.h>
0042 #include <linux/serial_reg.h>
0043 #include <linux/slab.h>
0044 #include <linux/tty.h>
0045 #include <linux/tty_flip.h>
0046
0047
0048
0049
0050
0051
0052 #define SIFIVE_SERIAL_TXDATA_OFFS 0x0
0053 #define SIFIVE_SERIAL_TXDATA_FULL_SHIFT 31
0054 #define SIFIVE_SERIAL_TXDATA_FULL_MASK (1 << SIFIVE_SERIAL_TXDATA_FULL_SHIFT)
0055 #define SIFIVE_SERIAL_TXDATA_DATA_SHIFT 0
0056 #define SIFIVE_SERIAL_TXDATA_DATA_MASK (0xff << SIFIVE_SERIAL_TXDATA_DATA_SHIFT)
0057
0058
0059 #define SIFIVE_SERIAL_RXDATA_OFFS 0x4
0060 #define SIFIVE_SERIAL_RXDATA_EMPTY_SHIFT 31
0061 #define SIFIVE_SERIAL_RXDATA_EMPTY_MASK (1 << SIFIVE_SERIAL_RXDATA_EMPTY_SHIFT)
0062 #define SIFIVE_SERIAL_RXDATA_DATA_SHIFT 0
0063 #define SIFIVE_SERIAL_RXDATA_DATA_MASK (0xff << SIFIVE_SERIAL_RXDATA_DATA_SHIFT)
0064
0065
0066 #define SIFIVE_SERIAL_TXCTRL_OFFS 0x8
0067 #define SIFIVE_SERIAL_TXCTRL_TXCNT_SHIFT 16
0068 #define SIFIVE_SERIAL_TXCTRL_TXCNT_MASK (0x7 << SIFIVE_SERIAL_TXCTRL_TXCNT_SHIFT)
0069 #define SIFIVE_SERIAL_TXCTRL_NSTOP_SHIFT 1
0070 #define SIFIVE_SERIAL_TXCTRL_NSTOP_MASK (1 << SIFIVE_SERIAL_TXCTRL_NSTOP_SHIFT)
0071 #define SIFIVE_SERIAL_TXCTRL_TXEN_SHIFT 0
0072 #define SIFIVE_SERIAL_TXCTRL_TXEN_MASK (1 << SIFIVE_SERIAL_TXCTRL_TXEN_SHIFT)
0073
0074
0075 #define SIFIVE_SERIAL_RXCTRL_OFFS 0xC
0076 #define SIFIVE_SERIAL_RXCTRL_RXCNT_SHIFT 16
0077 #define SIFIVE_SERIAL_RXCTRL_RXCNT_MASK (0x7 << SIFIVE_SERIAL_TXCTRL_TXCNT_SHIFT)
0078 #define SIFIVE_SERIAL_RXCTRL_RXEN_SHIFT 0
0079 #define SIFIVE_SERIAL_RXCTRL_RXEN_MASK (1 << SIFIVE_SERIAL_RXCTRL_RXEN_SHIFT)
0080
0081
0082 #define SIFIVE_SERIAL_IE_OFFS 0x10
0083 #define SIFIVE_SERIAL_IE_RXWM_SHIFT 1
0084 #define SIFIVE_SERIAL_IE_RXWM_MASK (1 << SIFIVE_SERIAL_IE_RXWM_SHIFT)
0085 #define SIFIVE_SERIAL_IE_TXWM_SHIFT 0
0086 #define SIFIVE_SERIAL_IE_TXWM_MASK (1 << SIFIVE_SERIAL_IE_TXWM_SHIFT)
0087
0088
0089 #define SIFIVE_SERIAL_IP_OFFS 0x14
0090 #define SIFIVE_SERIAL_IP_RXWM_SHIFT 1
0091 #define SIFIVE_SERIAL_IP_RXWM_MASK (1 << SIFIVE_SERIAL_IP_RXWM_SHIFT)
0092 #define SIFIVE_SERIAL_IP_TXWM_SHIFT 0
0093 #define SIFIVE_SERIAL_IP_TXWM_MASK (1 << SIFIVE_SERIAL_IP_TXWM_SHIFT)
0094
0095
0096 #define SIFIVE_SERIAL_DIV_OFFS 0x18
0097 #define SIFIVE_SERIAL_DIV_DIV_SHIFT 0
0098 #define SIFIVE_SERIAL_DIV_DIV_MASK (0xffff << SIFIVE_SERIAL_IP_DIV_SHIFT)
0099
0100
0101
0102
0103
0104
0105
0106
0107
0108 #define SIFIVE_SERIAL_MAX_PORTS 8
0109
0110
0111
0112
0113
0114 #define SIFIVE_DEFAULT_BAUD_RATE 115200
0115
0116
0117 #define SIFIVE_SERIAL_NAME "sifive-serial"
0118
0119
0120 #define SIFIVE_TTY_PREFIX "ttySIF"
0121
0122
0123 #define SIFIVE_TX_FIFO_DEPTH 8
0124
0125
0126 #define SIFIVE_RX_FIFO_DEPTH 8
0127
0128 #if (SIFIVE_TX_FIFO_DEPTH != SIFIVE_RX_FIFO_DEPTH)
0129 #error Driver does not support configurations with different TX, RX FIFO sizes
0130 #endif
0131
0132
0133
0134
0135
0136
0137
0138
0139
0140
0141
0142
0143
0144
0145
0146
0147 struct sifive_serial_port {
0148 struct uart_port port;
0149 struct device *dev;
0150 unsigned char ier;
0151 unsigned long baud_rate;
0152 struct clk *clk;
0153 struct notifier_block clk_notifier;
0154 };
0155
0156
0157
0158
0159
0160 #define port_to_sifive_serial_port(p) (container_of((p), \
0161 struct sifive_serial_port, \
0162 port))
0163
0164 #define notifier_to_sifive_serial_port(nb) (container_of((nb), \
0165 struct sifive_serial_port, \
0166 clk_notifier))
0167
0168
0169
0170
0171 static void sifive_serial_stop_tx(struct uart_port *port);
0172
0173
0174
0175
0176
0177
0178
0179
0180
0181
0182
0183
0184
0185
0186
0187
0188
0189 static void __ssp_early_writel(u32 v, u16 offs, struct uart_port *port)
0190 {
0191 writel_relaxed(v, port->membase + offs);
0192 }
0193
0194
0195
0196
0197
0198
0199
0200
0201
0202
0203
0204
0205
0206
0207
0208
0209 static u32 __ssp_early_readl(struct uart_port *port, u16 offs)
0210 {
0211 return readl_relaxed(port->membase + offs);
0212 }
0213
0214
0215
0216
0217
0218
0219
0220
0221
0222
0223
0224
0225 static void __ssp_writel(u32 v, u16 offs, struct sifive_serial_port *ssp)
0226 {
0227 __ssp_early_writel(v, offs, &ssp->port);
0228 }
0229
0230
0231
0232
0233
0234
0235
0236
0237
0238
0239
0240
0241
0242 static u32 __ssp_readl(struct sifive_serial_port *ssp, u16 offs)
0243 {
0244 return __ssp_early_readl(&ssp->port, offs);
0245 }
0246
0247
0248
0249
0250
0251
0252
0253
0254
0255
0256
0257
0258 static int sifive_serial_is_txfifo_full(struct sifive_serial_port *ssp)
0259 {
0260 return __ssp_readl(ssp, SIFIVE_SERIAL_TXDATA_OFFS) &
0261 SIFIVE_SERIAL_TXDATA_FULL_MASK;
0262 }
0263
0264
0265
0266
0267
0268
0269
0270
0271
0272
0273
0274
0275 static void __ssp_transmit_char(struct sifive_serial_port *ssp, int ch)
0276 {
0277 __ssp_writel(ch, SIFIVE_SERIAL_TXDATA_OFFS, ssp);
0278 }
0279
0280
0281
0282
0283
0284
0285
0286
0287
0288
0289 static void __ssp_transmit_chars(struct sifive_serial_port *ssp)
0290 {
0291 struct circ_buf *xmit = &ssp->port.state->xmit;
0292 int count;
0293
0294 if (ssp->port.x_char) {
0295 __ssp_transmit_char(ssp, ssp->port.x_char);
0296 ssp->port.icount.tx++;
0297 ssp->port.x_char = 0;
0298 return;
0299 }
0300 if (uart_circ_empty(xmit) || uart_tx_stopped(&ssp->port)) {
0301 sifive_serial_stop_tx(&ssp->port);
0302 return;
0303 }
0304 count = SIFIVE_TX_FIFO_DEPTH;
0305 do {
0306 __ssp_transmit_char(ssp, xmit->buf[xmit->tail]);
0307 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
0308 ssp->port.icount.tx++;
0309 if (uart_circ_empty(xmit))
0310 break;
0311 } while (--count > 0);
0312
0313 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
0314 uart_write_wakeup(&ssp->port);
0315
0316 if (uart_circ_empty(xmit))
0317 sifive_serial_stop_tx(&ssp->port);
0318 }
0319
0320
0321
0322
0323
0324
0325
0326
0327 static void __ssp_enable_txwm(struct sifive_serial_port *ssp)
0328 {
0329 if (ssp->ier & SIFIVE_SERIAL_IE_TXWM_MASK)
0330 return;
0331
0332 ssp->ier |= SIFIVE_SERIAL_IE_TXWM_MASK;
0333 __ssp_writel(ssp->ier, SIFIVE_SERIAL_IE_OFFS, ssp);
0334 }
0335
0336
0337
0338
0339
0340
0341
0342
0343 static void __ssp_enable_rxwm(struct sifive_serial_port *ssp)
0344 {
0345 if (ssp->ier & SIFIVE_SERIAL_IE_RXWM_MASK)
0346 return;
0347
0348 ssp->ier |= SIFIVE_SERIAL_IE_RXWM_MASK;
0349 __ssp_writel(ssp->ier, SIFIVE_SERIAL_IE_OFFS, ssp);
0350 }
0351
0352
0353
0354
0355
0356
0357
0358
0359 static void __ssp_disable_txwm(struct sifive_serial_port *ssp)
0360 {
0361 if (!(ssp->ier & SIFIVE_SERIAL_IE_TXWM_MASK))
0362 return;
0363
0364 ssp->ier &= ~SIFIVE_SERIAL_IE_TXWM_MASK;
0365 __ssp_writel(ssp->ier, SIFIVE_SERIAL_IE_OFFS, ssp);
0366 }
0367
0368
0369
0370
0371
0372
0373
0374
0375 static void __ssp_disable_rxwm(struct sifive_serial_port *ssp)
0376 {
0377 if (!(ssp->ier & SIFIVE_SERIAL_IE_RXWM_MASK))
0378 return;
0379
0380 ssp->ier &= ~SIFIVE_SERIAL_IE_RXWM_MASK;
0381 __ssp_writel(ssp->ier, SIFIVE_SERIAL_IE_OFFS, ssp);
0382 }
0383
0384
0385
0386
0387
0388
0389
0390
0391
0392
0393
0394
0395
0396 static char __ssp_receive_char(struct sifive_serial_port *ssp, char *is_empty)
0397 {
0398 u32 v;
0399 u8 ch;
0400
0401 v = __ssp_readl(ssp, SIFIVE_SERIAL_RXDATA_OFFS);
0402
0403 if (!is_empty)
0404 WARN_ON(1);
0405 else
0406 *is_empty = (v & SIFIVE_SERIAL_RXDATA_EMPTY_MASK) >>
0407 SIFIVE_SERIAL_RXDATA_EMPTY_SHIFT;
0408
0409 ch = (v & SIFIVE_SERIAL_RXDATA_DATA_MASK) >>
0410 SIFIVE_SERIAL_RXDATA_DATA_SHIFT;
0411
0412 return ch;
0413 }
0414
0415
0416
0417
0418
0419
0420
0421
0422
0423
0424 static void __ssp_receive_chars(struct sifive_serial_port *ssp)
0425 {
0426 unsigned char ch;
0427 char is_empty;
0428 int c;
0429
0430 for (c = SIFIVE_RX_FIFO_DEPTH; c > 0; --c) {
0431 ch = __ssp_receive_char(ssp, &is_empty);
0432 if (is_empty)
0433 break;
0434
0435 ssp->port.icount.rx++;
0436 uart_insert_char(&ssp->port, 0, 0, ch, TTY_NORMAL);
0437 }
0438
0439 tty_flip_buffer_push(&ssp->port.state->port);
0440 }
0441
0442
0443
0444
0445
0446
0447
0448
0449
0450 static void __ssp_update_div(struct sifive_serial_port *ssp)
0451 {
0452 u16 div;
0453
0454 div = DIV_ROUND_UP(ssp->port.uartclk, ssp->baud_rate) - 1;
0455
0456 __ssp_writel(div, SIFIVE_SERIAL_DIV_OFFS, ssp);
0457 }
0458
0459
0460
0461
0462
0463
0464
0465
0466
0467
0468
0469 static void __ssp_update_baud_rate(struct sifive_serial_port *ssp,
0470 unsigned int rate)
0471 {
0472 if (ssp->baud_rate == rate)
0473 return;
0474
0475 ssp->baud_rate = rate;
0476 __ssp_update_div(ssp);
0477 }
0478
0479
0480
0481
0482
0483
0484
0485
0486 static void __ssp_set_stop_bits(struct sifive_serial_port *ssp, char nstop)
0487 {
0488 u32 v;
0489
0490 if (nstop < 1 || nstop > 2) {
0491 WARN_ON(1);
0492 return;
0493 }
0494
0495 v = __ssp_readl(ssp, SIFIVE_SERIAL_TXCTRL_OFFS);
0496 v &= ~SIFIVE_SERIAL_TXCTRL_NSTOP_MASK;
0497 v |= (nstop - 1) << SIFIVE_SERIAL_TXCTRL_NSTOP_SHIFT;
0498 __ssp_writel(v, SIFIVE_SERIAL_TXCTRL_OFFS, ssp);
0499 }
0500
0501
0502
0503
0504
0505
0506
0507
0508
0509 static void __maybe_unused __ssp_wait_for_xmitr(struct sifive_serial_port *ssp)
0510 {
0511 while (sifive_serial_is_txfifo_full(ssp))
0512 udelay(1);
0513 }
0514
0515
0516
0517
0518
0519 static void sifive_serial_stop_tx(struct uart_port *port)
0520 {
0521 struct sifive_serial_port *ssp = port_to_sifive_serial_port(port);
0522
0523 __ssp_disable_txwm(ssp);
0524 }
0525
0526 static void sifive_serial_stop_rx(struct uart_port *port)
0527 {
0528 struct sifive_serial_port *ssp = port_to_sifive_serial_port(port);
0529
0530 __ssp_disable_rxwm(ssp);
0531 }
0532
0533 static void sifive_serial_start_tx(struct uart_port *port)
0534 {
0535 struct sifive_serial_port *ssp = port_to_sifive_serial_port(port);
0536
0537 __ssp_enable_txwm(ssp);
0538 }
0539
0540 static irqreturn_t sifive_serial_irq(int irq, void *dev_id)
0541 {
0542 struct sifive_serial_port *ssp = dev_id;
0543 u32 ip;
0544
0545 spin_lock(&ssp->port.lock);
0546
0547 ip = __ssp_readl(ssp, SIFIVE_SERIAL_IP_OFFS);
0548 if (!ip) {
0549 spin_unlock(&ssp->port.lock);
0550 return IRQ_NONE;
0551 }
0552
0553 if (ip & SIFIVE_SERIAL_IP_RXWM_MASK)
0554 __ssp_receive_chars(ssp);
0555 if (ip & SIFIVE_SERIAL_IP_TXWM_MASK)
0556 __ssp_transmit_chars(ssp);
0557
0558 spin_unlock(&ssp->port.lock);
0559
0560 return IRQ_HANDLED;
0561 }
0562
0563 static unsigned int sifive_serial_tx_empty(struct uart_port *port)
0564 {
0565 return TIOCSER_TEMT;
0566 }
0567
0568 static unsigned int sifive_serial_get_mctrl(struct uart_port *port)
0569 {
0570 return TIOCM_CAR | TIOCM_CTS | TIOCM_DSR;
0571 }
0572
0573 static void sifive_serial_set_mctrl(struct uart_port *port, unsigned int mctrl)
0574 {
0575
0576 }
0577
0578 static void sifive_serial_break_ctl(struct uart_port *port, int break_state)
0579 {
0580
0581 }
0582
0583 static int sifive_serial_startup(struct uart_port *port)
0584 {
0585 struct sifive_serial_port *ssp = port_to_sifive_serial_port(port);
0586
0587 __ssp_enable_rxwm(ssp);
0588
0589 return 0;
0590 }
0591
0592 static void sifive_serial_shutdown(struct uart_port *port)
0593 {
0594 struct sifive_serial_port *ssp = port_to_sifive_serial_port(port);
0595
0596 __ssp_disable_rxwm(ssp);
0597 __ssp_disable_txwm(ssp);
0598 }
0599
0600
0601
0602
0603
0604
0605
0606
0607
0608
0609
0610
0611
0612
0613 static int sifive_serial_clk_notifier(struct notifier_block *nb,
0614 unsigned long event, void *data)
0615 {
0616 struct clk_notifier_data *cnd = data;
0617 struct sifive_serial_port *ssp = notifier_to_sifive_serial_port(nb);
0618
0619 if (event == PRE_RATE_CHANGE) {
0620
0621
0622
0623
0624
0625
0626 __ssp_wait_for_xmitr(ssp);
0627
0628
0629
0630
0631
0632
0633
0634
0635
0636 udelay(DIV_ROUND_UP(12 * 1000 * 1000, ssp->baud_rate));
0637 }
0638
0639 if (event == POST_RATE_CHANGE && ssp->port.uartclk != cnd->new_rate) {
0640 ssp->port.uartclk = cnd->new_rate;
0641 __ssp_update_div(ssp);
0642 }
0643
0644 return NOTIFY_OK;
0645 }
0646
0647 static void sifive_serial_set_termios(struct uart_port *port,
0648 struct ktermios *termios,
0649 struct ktermios *old)
0650 {
0651 struct sifive_serial_port *ssp = port_to_sifive_serial_port(port);
0652 unsigned long flags;
0653 u32 v, old_v;
0654 int rate;
0655 char nstop;
0656
0657 if ((termios->c_cflag & CSIZE) != CS8) {
0658 dev_err_once(ssp->port.dev, "only 8-bit words supported\n");
0659 termios->c_cflag &= ~CSIZE;
0660 termios->c_cflag |= CS8;
0661 }
0662 if (termios->c_iflag & (INPCK | PARMRK))
0663 dev_err_once(ssp->port.dev, "parity checking not supported\n");
0664 if (termios->c_iflag & BRKINT)
0665 dev_err_once(ssp->port.dev, "BREAK detection not supported\n");
0666 termios->c_iflag &= ~(INPCK|PARMRK|BRKINT);
0667
0668
0669 nstop = (termios->c_cflag & CSTOPB) ? 2 : 1;
0670 __ssp_set_stop_bits(ssp, nstop);
0671
0672
0673 rate = uart_get_baud_rate(port, termios, old, 0,
0674 ssp->port.uartclk / 16);
0675 __ssp_update_baud_rate(ssp, rate);
0676
0677 spin_lock_irqsave(&ssp->port.lock, flags);
0678
0679
0680 uart_update_timeout(port, termios->c_cflag, rate);
0681
0682 ssp->port.read_status_mask = 0;
0683
0684
0685 v = __ssp_readl(ssp, SIFIVE_SERIAL_RXCTRL_OFFS);
0686 old_v = v;
0687 if ((termios->c_cflag & CREAD) == 0)
0688 v &= SIFIVE_SERIAL_RXCTRL_RXEN_MASK;
0689 else
0690 v |= SIFIVE_SERIAL_RXCTRL_RXEN_MASK;
0691 if (v != old_v)
0692 __ssp_writel(v, SIFIVE_SERIAL_RXCTRL_OFFS, ssp);
0693
0694 spin_unlock_irqrestore(&ssp->port.lock, flags);
0695 }
0696
0697 static void sifive_serial_release_port(struct uart_port *port)
0698 {
0699 }
0700
0701 static int sifive_serial_request_port(struct uart_port *port)
0702 {
0703 return 0;
0704 }
0705
0706 static void sifive_serial_config_port(struct uart_port *port, int flags)
0707 {
0708 struct sifive_serial_port *ssp = port_to_sifive_serial_port(port);
0709
0710 ssp->port.type = PORT_SIFIVE_V0;
0711 }
0712
0713 static int sifive_serial_verify_port(struct uart_port *port,
0714 struct serial_struct *ser)
0715 {
0716 return -EINVAL;
0717 }
0718
0719 static const char *sifive_serial_type(struct uart_port *port)
0720 {
0721 return port->type == PORT_SIFIVE_V0 ? "SiFive UART v0" : NULL;
0722 }
0723
0724 #ifdef CONFIG_CONSOLE_POLL
0725 static int sifive_serial_poll_get_char(struct uart_port *port)
0726 {
0727 struct sifive_serial_port *ssp = port_to_sifive_serial_port(port);
0728 char is_empty, ch;
0729
0730 ch = __ssp_receive_char(ssp, &is_empty);
0731 if (is_empty)
0732 return NO_POLL_CHAR;
0733
0734 return ch;
0735 }
0736
0737 static void sifive_serial_poll_put_char(struct uart_port *port,
0738 unsigned char c)
0739 {
0740 struct sifive_serial_port *ssp = port_to_sifive_serial_port(port);
0741
0742 __ssp_wait_for_xmitr(ssp);
0743 __ssp_transmit_char(ssp, c);
0744 }
0745 #endif
0746
0747
0748
0749
0750
0751 #ifdef CONFIG_SERIAL_EARLYCON
0752 static void early_sifive_serial_putc(struct uart_port *port, unsigned char c)
0753 {
0754 while (__ssp_early_readl(port, SIFIVE_SERIAL_TXDATA_OFFS) &
0755 SIFIVE_SERIAL_TXDATA_FULL_MASK)
0756 cpu_relax();
0757
0758 __ssp_early_writel(c, SIFIVE_SERIAL_TXDATA_OFFS, port);
0759 }
0760
0761 static void early_sifive_serial_write(struct console *con, const char *s,
0762 unsigned int n)
0763 {
0764 struct earlycon_device *dev = con->data;
0765 struct uart_port *port = &dev->port;
0766
0767 uart_console_write(port, s, n, early_sifive_serial_putc);
0768 }
0769
0770 static int __init early_sifive_serial_setup(struct earlycon_device *dev,
0771 const char *options)
0772 {
0773 struct uart_port *port = &dev->port;
0774
0775 if (!port->membase)
0776 return -ENODEV;
0777
0778 dev->con->write = early_sifive_serial_write;
0779
0780 return 0;
0781 }
0782
0783 OF_EARLYCON_DECLARE(sifive, "sifive,uart0", early_sifive_serial_setup);
0784 OF_EARLYCON_DECLARE(sifive, "sifive,fu540-c000-uart0",
0785 early_sifive_serial_setup);
0786 #endif
0787
0788
0789
0790
0791
0792 #ifdef CONFIG_SERIAL_SIFIVE_CONSOLE
0793
0794 static struct sifive_serial_port *sifive_serial_console_ports[SIFIVE_SERIAL_MAX_PORTS];
0795
0796 static void sifive_serial_console_putchar(struct uart_port *port, unsigned char ch)
0797 {
0798 struct sifive_serial_port *ssp = port_to_sifive_serial_port(port);
0799
0800 __ssp_wait_for_xmitr(ssp);
0801 __ssp_transmit_char(ssp, ch);
0802 }
0803
0804 static void sifive_serial_console_write(struct console *co, const char *s,
0805 unsigned int count)
0806 {
0807 struct sifive_serial_port *ssp = sifive_serial_console_ports[co->index];
0808 unsigned long flags;
0809 unsigned int ier;
0810 int locked = 1;
0811
0812 if (!ssp)
0813 return;
0814
0815 local_irq_save(flags);
0816 if (ssp->port.sysrq)
0817 locked = 0;
0818 else if (oops_in_progress)
0819 locked = spin_trylock(&ssp->port.lock);
0820 else
0821 spin_lock(&ssp->port.lock);
0822
0823 ier = __ssp_readl(ssp, SIFIVE_SERIAL_IE_OFFS);
0824 __ssp_writel(0, SIFIVE_SERIAL_IE_OFFS, ssp);
0825
0826 uart_console_write(&ssp->port, s, count, sifive_serial_console_putchar);
0827
0828 __ssp_writel(ier, SIFIVE_SERIAL_IE_OFFS, ssp);
0829
0830 if (locked)
0831 spin_unlock(&ssp->port.lock);
0832 local_irq_restore(flags);
0833 }
0834
0835 static int __init sifive_serial_console_setup(struct console *co, char *options)
0836 {
0837 struct sifive_serial_port *ssp;
0838 int baud = SIFIVE_DEFAULT_BAUD_RATE;
0839 int bits = 8;
0840 int parity = 'n';
0841 int flow = 'n';
0842
0843 if (co->index < 0 || co->index >= SIFIVE_SERIAL_MAX_PORTS)
0844 return -ENODEV;
0845
0846 ssp = sifive_serial_console_ports[co->index];
0847 if (!ssp)
0848 return -ENODEV;
0849
0850 if (options)
0851 uart_parse_options(options, &baud, &parity, &bits, &flow);
0852
0853 return uart_set_options(&ssp->port, co, baud, parity, bits, flow);
0854 }
0855
0856 static struct uart_driver sifive_serial_uart_driver;
0857
0858 static struct console sifive_serial_console = {
0859 .name = SIFIVE_TTY_PREFIX,
0860 .write = sifive_serial_console_write,
0861 .device = uart_console_device,
0862 .setup = sifive_serial_console_setup,
0863 .flags = CON_PRINTBUFFER,
0864 .index = -1,
0865 .data = &sifive_serial_uart_driver,
0866 };
0867
0868 static int __init sifive_console_init(void)
0869 {
0870 register_console(&sifive_serial_console);
0871 return 0;
0872 }
0873
0874 console_initcall(sifive_console_init);
0875
0876 static void __ssp_add_console_port(struct sifive_serial_port *ssp)
0877 {
0878 sifive_serial_console_ports[ssp->port.line] = ssp;
0879 }
0880
0881 static void __ssp_remove_console_port(struct sifive_serial_port *ssp)
0882 {
0883 sifive_serial_console_ports[ssp->port.line] = NULL;
0884 }
0885
0886 #define SIFIVE_SERIAL_CONSOLE (&sifive_serial_console)
0887
0888 #else
0889
0890 #define SIFIVE_SERIAL_CONSOLE NULL
0891
0892 static void __ssp_add_console_port(struct sifive_serial_port *ssp)
0893 {}
0894 static void __ssp_remove_console_port(struct sifive_serial_port *ssp)
0895 {}
0896
0897 #endif
0898
0899 static const struct uart_ops sifive_serial_uops = {
0900 .tx_empty = sifive_serial_tx_empty,
0901 .set_mctrl = sifive_serial_set_mctrl,
0902 .get_mctrl = sifive_serial_get_mctrl,
0903 .stop_tx = sifive_serial_stop_tx,
0904 .start_tx = sifive_serial_start_tx,
0905 .stop_rx = sifive_serial_stop_rx,
0906 .break_ctl = sifive_serial_break_ctl,
0907 .startup = sifive_serial_startup,
0908 .shutdown = sifive_serial_shutdown,
0909 .set_termios = sifive_serial_set_termios,
0910 .type = sifive_serial_type,
0911 .release_port = sifive_serial_release_port,
0912 .request_port = sifive_serial_request_port,
0913 .config_port = sifive_serial_config_port,
0914 .verify_port = sifive_serial_verify_port,
0915 #ifdef CONFIG_CONSOLE_POLL
0916 .poll_get_char = sifive_serial_poll_get_char,
0917 .poll_put_char = sifive_serial_poll_put_char,
0918 #endif
0919 };
0920
0921 static struct uart_driver sifive_serial_uart_driver = {
0922 .owner = THIS_MODULE,
0923 .driver_name = SIFIVE_SERIAL_NAME,
0924 .dev_name = SIFIVE_TTY_PREFIX,
0925 .nr = SIFIVE_SERIAL_MAX_PORTS,
0926 .cons = SIFIVE_SERIAL_CONSOLE,
0927 };
0928
0929 static int sifive_serial_probe(struct platform_device *pdev)
0930 {
0931 struct sifive_serial_port *ssp;
0932 struct resource *mem;
0933 struct clk *clk;
0934 void __iomem *base;
0935 int irq, id, r;
0936
0937 irq = platform_get_irq(pdev, 0);
0938 if (irq < 0)
0939 return -EPROBE_DEFER;
0940
0941 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
0942 base = devm_ioremap_resource(&pdev->dev, mem);
0943 if (IS_ERR(base)) {
0944 dev_err(&pdev->dev, "could not acquire device memory\n");
0945 return PTR_ERR(base);
0946 }
0947
0948 clk = devm_clk_get_enabled(&pdev->dev, NULL);
0949 if (IS_ERR(clk)) {
0950 dev_err(&pdev->dev, "unable to find controller clock\n");
0951 return PTR_ERR(clk);
0952 }
0953
0954 id = of_alias_get_id(pdev->dev.of_node, "serial");
0955 if (id < 0) {
0956 dev_err(&pdev->dev, "missing aliases entry\n");
0957 return id;
0958 }
0959
0960 #ifdef CONFIG_SERIAL_SIFIVE_CONSOLE
0961 if (id > SIFIVE_SERIAL_MAX_PORTS) {
0962 dev_err(&pdev->dev, "too many UARTs (%d)\n", id);
0963 return -EINVAL;
0964 }
0965 #endif
0966
0967 ssp = devm_kzalloc(&pdev->dev, sizeof(*ssp), GFP_KERNEL);
0968 if (!ssp)
0969 return -ENOMEM;
0970
0971 ssp->port.dev = &pdev->dev;
0972 ssp->port.type = PORT_SIFIVE_V0;
0973 ssp->port.iotype = UPIO_MEM;
0974 ssp->port.irq = irq;
0975 ssp->port.fifosize = SIFIVE_TX_FIFO_DEPTH;
0976 ssp->port.ops = &sifive_serial_uops;
0977 ssp->port.line = id;
0978 ssp->port.mapbase = mem->start;
0979 ssp->port.membase = base;
0980 ssp->dev = &pdev->dev;
0981 ssp->clk = clk;
0982 ssp->clk_notifier.notifier_call = sifive_serial_clk_notifier;
0983
0984 r = clk_notifier_register(ssp->clk, &ssp->clk_notifier);
0985 if (r) {
0986 dev_err(&pdev->dev, "could not register clock notifier: %d\n",
0987 r);
0988 goto probe_out1;
0989 }
0990
0991
0992 ssp->port.uartclk = clk_get_rate(ssp->clk);
0993 ssp->baud_rate = SIFIVE_DEFAULT_BAUD_RATE;
0994 __ssp_update_div(ssp);
0995
0996 platform_set_drvdata(pdev, ssp);
0997
0998
0999 __ssp_writel((1 << SIFIVE_SERIAL_TXCTRL_TXCNT_SHIFT) |
1000 SIFIVE_SERIAL_TXCTRL_TXEN_MASK,
1001 SIFIVE_SERIAL_TXCTRL_OFFS, ssp);
1002
1003
1004 __ssp_writel((0 << SIFIVE_SERIAL_RXCTRL_RXCNT_SHIFT) |
1005 SIFIVE_SERIAL_RXCTRL_RXEN_MASK,
1006 SIFIVE_SERIAL_RXCTRL_OFFS, ssp);
1007
1008 r = request_irq(ssp->port.irq, sifive_serial_irq, ssp->port.irqflags,
1009 dev_name(&pdev->dev), ssp);
1010 if (r) {
1011 dev_err(&pdev->dev, "could not attach interrupt: %d\n", r);
1012 goto probe_out2;
1013 }
1014
1015 __ssp_add_console_port(ssp);
1016
1017 r = uart_add_one_port(&sifive_serial_uart_driver, &ssp->port);
1018 if (r != 0) {
1019 dev_err(&pdev->dev, "could not add uart: %d\n", r);
1020 goto probe_out3;
1021 }
1022
1023 return 0;
1024
1025 probe_out3:
1026 __ssp_remove_console_port(ssp);
1027 free_irq(ssp->port.irq, ssp);
1028 probe_out2:
1029 clk_notifier_unregister(ssp->clk, &ssp->clk_notifier);
1030 probe_out1:
1031 return r;
1032 }
1033
1034 static int sifive_serial_remove(struct platform_device *dev)
1035 {
1036 struct sifive_serial_port *ssp = platform_get_drvdata(dev);
1037
1038 __ssp_remove_console_port(ssp);
1039 uart_remove_one_port(&sifive_serial_uart_driver, &ssp->port);
1040 free_irq(ssp->port.irq, ssp);
1041 clk_notifier_unregister(ssp->clk, &ssp->clk_notifier);
1042
1043 return 0;
1044 }
1045
1046 static const struct of_device_id sifive_serial_of_match[] = {
1047 { .compatible = "sifive,fu540-c000-uart0" },
1048 { .compatible = "sifive,uart0" },
1049 {},
1050 };
1051 MODULE_DEVICE_TABLE(of, sifive_serial_of_match);
1052
1053 static struct platform_driver sifive_serial_platform_driver = {
1054 .probe = sifive_serial_probe,
1055 .remove = sifive_serial_remove,
1056 .driver = {
1057 .name = SIFIVE_SERIAL_NAME,
1058 .of_match_table = of_match_ptr(sifive_serial_of_match),
1059 },
1060 };
1061
1062 static int __init sifive_serial_init(void)
1063 {
1064 int r;
1065
1066 r = uart_register_driver(&sifive_serial_uart_driver);
1067 if (r)
1068 goto init_out1;
1069
1070 r = platform_driver_register(&sifive_serial_platform_driver);
1071 if (r)
1072 goto init_out2;
1073
1074 return 0;
1075
1076 init_out2:
1077 uart_unregister_driver(&sifive_serial_uart_driver);
1078 init_out1:
1079 return r;
1080 }
1081
1082 static void __exit sifive_serial_exit(void)
1083 {
1084 platform_driver_unregister(&sifive_serial_platform_driver);
1085 uart_unregister_driver(&sifive_serial_uart_driver);
1086 }
1087
1088 module_init(sifive_serial_init);
1089 module_exit(sifive_serial_exit);
1090
1091 MODULE_DESCRIPTION("SiFive UART serial driver");
1092 MODULE_LICENSE("GPL");
1093 MODULE_AUTHOR("Paul Walmsley <paul@pwsan.com>");