0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014 #include <linux/module.h>
0015 #include <linux/tty.h>
0016 #include <linux/tty_flip.h>
0017 #include <linux/ioport.h>
0018 #include <linux/init.h>
0019 #include <linux/serial.h>
0020 #include <linux/console.h>
0021 #include <linux/sysrq.h>
0022 #include <linux/kthread.h>
0023 #include <linux/device.h>
0024 #include <linux/of.h>
0025 #include <linux/of_device.h>
0026 #include <linux/of_platform.h>
0027 #include <linux/of_irq.h>
0028 #include <linux/platform_device.h>
0029 #include <linux/io.h>
0030 #include <linux/serial_core.h>
0031 #include <asm/irq.h>
0032
0033 #include "apbuart.h"
0034
0035 #define SERIAL_APBUART_MAJOR TTY_MAJOR
0036 #define SERIAL_APBUART_MINOR 64
0037 #define UART_DUMMY_RSR_RX 0x8000
0038
0039 static void apbuart_tx_chars(struct uart_port *port);
0040
0041 static void apbuart_stop_tx(struct uart_port *port)
0042 {
0043 unsigned int cr;
0044
0045 cr = UART_GET_CTRL(port);
0046 cr &= ~UART_CTRL_TI;
0047 UART_PUT_CTRL(port, cr);
0048 }
0049
0050 static void apbuart_start_tx(struct uart_port *port)
0051 {
0052 unsigned int cr;
0053
0054 cr = UART_GET_CTRL(port);
0055 cr |= UART_CTRL_TI;
0056 UART_PUT_CTRL(port, cr);
0057
0058 if (UART_GET_STATUS(port) & UART_STATUS_THE)
0059 apbuart_tx_chars(port);
0060 }
0061
0062 static void apbuart_stop_rx(struct uart_port *port)
0063 {
0064 unsigned int cr;
0065
0066 cr = UART_GET_CTRL(port);
0067 cr &= ~(UART_CTRL_RI);
0068 UART_PUT_CTRL(port, cr);
0069 }
0070
0071 static void apbuart_rx_chars(struct uart_port *port)
0072 {
0073 unsigned int status, ch, rsr, flag;
0074 unsigned int max_chars = port->fifosize;
0075
0076 status = UART_GET_STATUS(port);
0077
0078 while (UART_RX_DATA(status) && (max_chars--)) {
0079
0080 ch = UART_GET_CHAR(port);
0081 flag = TTY_NORMAL;
0082
0083 port->icount.rx++;
0084
0085 rsr = UART_GET_STATUS(port) | UART_DUMMY_RSR_RX;
0086 UART_PUT_STATUS(port, 0);
0087 if (rsr & UART_STATUS_ERR) {
0088
0089 if (rsr & UART_STATUS_BR) {
0090 rsr &= ~(UART_STATUS_FE | UART_STATUS_PE);
0091 port->icount.brk++;
0092 if (uart_handle_break(port))
0093 goto ignore_char;
0094 } else if (rsr & UART_STATUS_PE) {
0095 port->icount.parity++;
0096 } else if (rsr & UART_STATUS_FE) {
0097 port->icount.frame++;
0098 }
0099 if (rsr & UART_STATUS_OE)
0100 port->icount.overrun++;
0101
0102 rsr &= port->read_status_mask;
0103
0104 if (rsr & UART_STATUS_PE)
0105 flag = TTY_PARITY;
0106 else if (rsr & UART_STATUS_FE)
0107 flag = TTY_FRAME;
0108 }
0109
0110 if (uart_handle_sysrq_char(port, ch))
0111 goto ignore_char;
0112
0113 uart_insert_char(port, rsr, UART_STATUS_OE, ch, flag);
0114
0115
0116 ignore_char:
0117 status = UART_GET_STATUS(port);
0118 }
0119
0120 tty_flip_buffer_push(&port->state->port);
0121 }
0122
0123 static void apbuart_tx_chars(struct uart_port *port)
0124 {
0125 struct circ_buf *xmit = &port->state->xmit;
0126 int count;
0127
0128 if (port->x_char) {
0129 UART_PUT_CHAR(port, port->x_char);
0130 port->icount.tx++;
0131 port->x_char = 0;
0132 return;
0133 }
0134
0135 if (uart_circ_empty(xmit) || uart_tx_stopped(port)) {
0136 apbuart_stop_tx(port);
0137 return;
0138 }
0139
0140
0141 count = port->fifosize >> 1;
0142 do {
0143 UART_PUT_CHAR(port, xmit->buf[xmit->tail]);
0144 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
0145 port->icount.tx++;
0146 if (uart_circ_empty(xmit))
0147 break;
0148 } while (--count > 0);
0149
0150 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
0151 uart_write_wakeup(port);
0152
0153 if (uart_circ_empty(xmit))
0154 apbuart_stop_tx(port);
0155 }
0156
0157 static irqreturn_t apbuart_int(int irq, void *dev_id)
0158 {
0159 struct uart_port *port = dev_id;
0160 unsigned int status;
0161
0162 spin_lock(&port->lock);
0163
0164 status = UART_GET_STATUS(port);
0165 if (status & UART_STATUS_DR)
0166 apbuart_rx_chars(port);
0167 if (status & UART_STATUS_THE)
0168 apbuart_tx_chars(port);
0169
0170 spin_unlock(&port->lock);
0171
0172 return IRQ_HANDLED;
0173 }
0174
0175 static unsigned int apbuart_tx_empty(struct uart_port *port)
0176 {
0177 unsigned int status = UART_GET_STATUS(port);
0178 return status & UART_STATUS_THE ? TIOCSER_TEMT : 0;
0179 }
0180
0181 static unsigned int apbuart_get_mctrl(struct uart_port *port)
0182 {
0183
0184 return TIOCM_CAR | TIOCM_DSR | TIOCM_CTS;
0185 }
0186
0187 static void apbuart_set_mctrl(struct uart_port *port, unsigned int mctrl)
0188 {
0189
0190 }
0191
0192 static void apbuart_break_ctl(struct uart_port *port, int break_state)
0193 {
0194
0195 }
0196
0197 static int apbuart_startup(struct uart_port *port)
0198 {
0199 int retval;
0200 unsigned int cr;
0201
0202
0203 retval = request_irq(port->irq, apbuart_int, 0, "apbuart", port);
0204 if (retval)
0205 return retval;
0206
0207
0208 cr = UART_GET_CTRL(port);
0209 UART_PUT_CTRL(port,
0210 cr | UART_CTRL_RE | UART_CTRL_TE |
0211 UART_CTRL_RI | UART_CTRL_TI);
0212
0213 return 0;
0214 }
0215
0216 static void apbuart_shutdown(struct uart_port *port)
0217 {
0218 unsigned int cr;
0219
0220
0221 cr = UART_GET_CTRL(port);
0222 UART_PUT_CTRL(port,
0223 cr & ~(UART_CTRL_RE | UART_CTRL_TE |
0224 UART_CTRL_RI | UART_CTRL_TI));
0225
0226
0227 free_irq(port->irq, port);
0228 }
0229
0230 static void apbuart_set_termios(struct uart_port *port,
0231 struct ktermios *termios, struct ktermios *old)
0232 {
0233 unsigned int cr;
0234 unsigned long flags;
0235 unsigned int baud, quot;
0236
0237
0238 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk / 16);
0239 if (baud == 0)
0240 panic("invalid baudrate %i\n", port->uartclk / 16);
0241
0242
0243 quot = (uart_get_divisor(port, baud)) * 2;
0244 cr = UART_GET_CTRL(port);
0245 cr &= ~(UART_CTRL_PE | UART_CTRL_PS);
0246
0247 if (termios->c_cflag & PARENB) {
0248 cr |= UART_CTRL_PE;
0249 if ((termios->c_cflag & PARODD))
0250 cr |= UART_CTRL_PS;
0251 }
0252
0253
0254 if (termios->c_cflag & CRTSCTS)
0255 cr |= UART_CTRL_FL;
0256
0257 spin_lock_irqsave(&port->lock, flags);
0258
0259
0260 uart_update_timeout(port, termios->c_cflag, baud);
0261
0262 port->read_status_mask = UART_STATUS_OE;
0263 if (termios->c_iflag & INPCK)
0264 port->read_status_mask |= UART_STATUS_FE | UART_STATUS_PE;
0265
0266
0267 port->ignore_status_mask = 0;
0268 if (termios->c_iflag & IGNPAR)
0269 port->ignore_status_mask |= UART_STATUS_FE | UART_STATUS_PE;
0270
0271
0272 if ((termios->c_cflag & CREAD) == 0)
0273 port->ignore_status_mask |= UART_DUMMY_RSR_RX;
0274
0275
0276 quot -= 1;
0277 UART_PUT_SCAL(port, quot);
0278 UART_PUT_CTRL(port, cr);
0279
0280 spin_unlock_irqrestore(&port->lock, flags);
0281 }
0282
0283 static const char *apbuart_type(struct uart_port *port)
0284 {
0285 return port->type == PORT_APBUART ? "GRLIB/APBUART" : NULL;
0286 }
0287
0288 static void apbuart_release_port(struct uart_port *port)
0289 {
0290 release_mem_region(port->mapbase, 0x100);
0291 }
0292
0293 static int apbuart_request_port(struct uart_port *port)
0294 {
0295 return request_mem_region(port->mapbase, 0x100, "grlib-apbuart")
0296 != NULL ? 0 : -EBUSY;
0297 return 0;
0298 }
0299
0300
0301 static void apbuart_config_port(struct uart_port *port, int flags)
0302 {
0303 if (flags & UART_CONFIG_TYPE) {
0304 port->type = PORT_APBUART;
0305 apbuart_request_port(port);
0306 }
0307 }
0308
0309
0310 static int apbuart_verify_port(struct uart_port *port,
0311 struct serial_struct *ser)
0312 {
0313 int ret = 0;
0314 if (ser->type != PORT_UNKNOWN && ser->type != PORT_APBUART)
0315 ret = -EINVAL;
0316 if (ser->irq < 0 || ser->irq >= NR_IRQS)
0317 ret = -EINVAL;
0318 if (ser->baud_base < 9600)
0319 ret = -EINVAL;
0320 return ret;
0321 }
0322
0323 static const struct uart_ops grlib_apbuart_ops = {
0324 .tx_empty = apbuart_tx_empty,
0325 .set_mctrl = apbuart_set_mctrl,
0326 .get_mctrl = apbuart_get_mctrl,
0327 .stop_tx = apbuart_stop_tx,
0328 .start_tx = apbuart_start_tx,
0329 .stop_rx = apbuart_stop_rx,
0330 .break_ctl = apbuart_break_ctl,
0331 .startup = apbuart_startup,
0332 .shutdown = apbuart_shutdown,
0333 .set_termios = apbuart_set_termios,
0334 .type = apbuart_type,
0335 .release_port = apbuart_release_port,
0336 .request_port = apbuart_request_port,
0337 .config_port = apbuart_config_port,
0338 .verify_port = apbuart_verify_port,
0339 };
0340
0341 static struct uart_port grlib_apbuart_ports[UART_NR];
0342 static struct device_node *grlib_apbuart_nodes[UART_NR];
0343
0344 static int apbuart_scan_fifo_size(struct uart_port *port, int portnumber)
0345 {
0346 int ctrl, loop = 0;
0347 int status;
0348 int fifosize;
0349 unsigned long flags;
0350
0351 ctrl = UART_GET_CTRL(port);
0352
0353
0354
0355
0356
0357
0358
0359
0360 local_irq_save(flags);
0361
0362 UART_PUT_CTRL(port, ctrl | UART_CTRL_TE);
0363
0364 while (!UART_TX_READY(UART_GET_STATUS(port)))
0365 loop++;
0366
0367
0368
0369
0370
0371
0372 UART_PUT_CTRL(port, ctrl & ~(UART_CTRL_TE));
0373
0374 fifosize = 1;
0375 UART_PUT_CHAR(port, 0);
0376
0377
0378
0379
0380
0381
0382
0383 status = UART_GET_STATUS(port);
0384 while (((status >> 20) & 0x3F) == fifosize) {
0385 fifosize++;
0386 UART_PUT_CHAR(port, 0);
0387 status = UART_GET_STATUS(port);
0388 }
0389
0390 fifosize--;
0391
0392 UART_PUT_CTRL(port, ctrl);
0393 local_irq_restore(flags);
0394
0395 if (fifosize == 0)
0396 fifosize = 1;
0397
0398 return fifosize;
0399 }
0400
0401 static void apbuart_flush_fifo(struct uart_port *port)
0402 {
0403 int i;
0404
0405 for (i = 0; i < port->fifosize; i++)
0406 UART_GET_CHAR(port);
0407 }
0408
0409
0410
0411
0412
0413
0414 #ifdef CONFIG_SERIAL_GRLIB_GAISLER_APBUART_CONSOLE
0415
0416 static void apbuart_console_putchar(struct uart_port *port, unsigned char ch)
0417 {
0418 unsigned int status;
0419 do {
0420 status = UART_GET_STATUS(port);
0421 } while (!UART_TX_READY(status));
0422 UART_PUT_CHAR(port, ch);
0423 }
0424
0425 static void
0426 apbuart_console_write(struct console *co, const char *s, unsigned int count)
0427 {
0428 struct uart_port *port = &grlib_apbuart_ports[co->index];
0429 unsigned int status, old_cr, new_cr;
0430
0431
0432 old_cr = UART_GET_CTRL(port);
0433 new_cr = old_cr & ~(UART_CTRL_RI | UART_CTRL_TI);
0434 UART_PUT_CTRL(port, new_cr);
0435
0436 uart_console_write(port, s, count, apbuart_console_putchar);
0437
0438
0439
0440
0441
0442 do {
0443 status = UART_GET_STATUS(port);
0444 } while (!UART_TX_READY(status));
0445 UART_PUT_CTRL(port, old_cr);
0446 }
0447
0448 static void __init
0449 apbuart_console_get_options(struct uart_port *port, int *baud,
0450 int *parity, int *bits)
0451 {
0452 if (UART_GET_CTRL(port) & (UART_CTRL_RE | UART_CTRL_TE)) {
0453
0454 unsigned int quot, status;
0455 status = UART_GET_STATUS(port);
0456
0457 *parity = 'n';
0458 if (status & UART_CTRL_PE) {
0459 if ((status & UART_CTRL_PS) == 0)
0460 *parity = 'e';
0461 else
0462 *parity = 'o';
0463 }
0464
0465 *bits = 8;
0466 quot = UART_GET_SCAL(port) / 8;
0467 *baud = port->uartclk / (16 * (quot + 1));
0468 }
0469 }
0470
0471 static int __init apbuart_console_setup(struct console *co, char *options)
0472 {
0473 struct uart_port *port;
0474 int baud = 38400;
0475 int bits = 8;
0476 int parity = 'n';
0477 int flow = 'n';
0478
0479 pr_debug("apbuart_console_setup co=%p, co->index=%i, options=%s\n",
0480 co, co->index, options);
0481
0482
0483
0484
0485
0486
0487 if (co->index >= grlib_apbuart_port_nr)
0488 co->index = 0;
0489
0490 port = &grlib_apbuart_ports[co->index];
0491
0492 spin_lock_init(&port->lock);
0493
0494 if (options)
0495 uart_parse_options(options, &baud, &parity, &bits, &flow);
0496 else
0497 apbuart_console_get_options(port, &baud, &parity, &bits);
0498
0499 return uart_set_options(port, co, baud, parity, bits, flow);
0500 }
0501
0502 static struct uart_driver grlib_apbuart_driver;
0503
0504 static struct console grlib_apbuart_console = {
0505 .name = "ttyS",
0506 .write = apbuart_console_write,
0507 .device = uart_console_device,
0508 .setup = apbuart_console_setup,
0509 .flags = CON_PRINTBUFFER,
0510 .index = -1,
0511 .data = &grlib_apbuart_driver,
0512 };
0513
0514
0515 static int grlib_apbuart_configure(void);
0516
0517 static int __init apbuart_console_init(void)
0518 {
0519 if (grlib_apbuart_configure())
0520 return -ENODEV;
0521 register_console(&grlib_apbuart_console);
0522 return 0;
0523 }
0524
0525 console_initcall(apbuart_console_init);
0526
0527 #define APBUART_CONSOLE (&grlib_apbuart_console)
0528 #else
0529 #define APBUART_CONSOLE NULL
0530 #endif
0531
0532 static struct uart_driver grlib_apbuart_driver = {
0533 .owner = THIS_MODULE,
0534 .driver_name = "serial",
0535 .dev_name = "ttyS",
0536 .major = SERIAL_APBUART_MAJOR,
0537 .minor = SERIAL_APBUART_MINOR,
0538 .nr = UART_NR,
0539 .cons = APBUART_CONSOLE,
0540 };
0541
0542
0543
0544
0545
0546
0547 static int apbuart_probe(struct platform_device *op)
0548 {
0549 int i;
0550 struct uart_port *port = NULL;
0551
0552 for (i = 0; i < grlib_apbuart_port_nr; i++) {
0553 if (op->dev.of_node == grlib_apbuart_nodes[i])
0554 break;
0555 }
0556
0557 port = &grlib_apbuart_ports[i];
0558 port->dev = &op->dev;
0559 port->irq = op->archdata.irqs[0];
0560
0561 uart_add_one_port(&grlib_apbuart_driver, (struct uart_port *) port);
0562
0563 apbuart_flush_fifo((struct uart_port *) port);
0564
0565 printk(KERN_INFO "grlib-apbuart at 0x%llx, irq %d\n",
0566 (unsigned long long) port->mapbase, port->irq);
0567 return 0;
0568 }
0569
0570 static const struct of_device_id apbuart_match[] = {
0571 {
0572 .name = "GAISLER_APBUART",
0573 },
0574 {
0575 .name = "01_00c",
0576 },
0577 {},
0578 };
0579 MODULE_DEVICE_TABLE(of, apbuart_match);
0580
0581 static struct platform_driver grlib_apbuart_of_driver = {
0582 .probe = apbuart_probe,
0583 .driver = {
0584 .name = "grlib-apbuart",
0585 .of_match_table = apbuart_match,
0586 },
0587 };
0588
0589
0590 static int __init grlib_apbuart_configure(void)
0591 {
0592 struct device_node *np;
0593 int line = 0;
0594
0595 for_each_matching_node(np, apbuart_match) {
0596 const int *ampopts;
0597 const u32 *freq_hz;
0598 const struct amba_prom_registers *regs;
0599 struct uart_port *port;
0600 unsigned long addr;
0601
0602 ampopts = of_get_property(np, "ampopts", NULL);
0603 if (ampopts && (*ampopts == 0))
0604 continue;
0605 regs = of_get_property(np, "reg", NULL);
0606
0607 freq_hz = of_get_property(np, "freq", NULL);
0608
0609 if (!regs || !freq_hz || (*freq_hz == 0))
0610 continue;
0611
0612 grlib_apbuart_nodes[line] = np;
0613
0614 addr = regs->phys_addr;
0615
0616 port = &grlib_apbuart_ports[line];
0617
0618 port->mapbase = addr;
0619 port->membase = ioremap(addr, sizeof(struct grlib_apbuart_regs_map));
0620 port->irq = 0;
0621 port->iotype = UPIO_MEM;
0622 port->ops = &grlib_apbuart_ops;
0623 port->has_sysrq = IS_ENABLED(CONFIG_SERIAL_GRLIB_GAISLER_APBUART_CONSOLE);
0624 port->flags = UPF_BOOT_AUTOCONF;
0625 port->line = line;
0626 port->uartclk = *freq_hz;
0627 port->fifosize = apbuart_scan_fifo_size((struct uart_port *) port, line);
0628 line++;
0629
0630
0631 if (line == UART_NR)
0632 break;
0633 }
0634
0635 grlib_apbuart_driver.nr = grlib_apbuart_port_nr = line;
0636 return line ? 0 : -ENODEV;
0637 }
0638
0639 static int __init grlib_apbuart_init(void)
0640 {
0641 int ret;
0642
0643
0644 ret = grlib_apbuart_configure();
0645 if (ret)
0646 return ret;
0647
0648 printk(KERN_INFO "Serial: GRLIB APBUART driver\n");
0649
0650 ret = uart_register_driver(&grlib_apbuart_driver);
0651
0652 if (ret) {
0653 printk(KERN_ERR "%s: uart_register_driver failed (%i)\n",
0654 __FILE__, ret);
0655 return ret;
0656 }
0657
0658 ret = platform_driver_register(&grlib_apbuart_of_driver);
0659 if (ret) {
0660 printk(KERN_ERR
0661 "%s: platform_driver_register failed (%i)\n",
0662 __FILE__, ret);
0663 uart_unregister_driver(&grlib_apbuart_driver);
0664 return ret;
0665 }
0666
0667 return ret;
0668 }
0669
0670 static void __exit grlib_apbuart_exit(void)
0671 {
0672 int i;
0673
0674 for (i = 0; i < grlib_apbuart_port_nr; i++)
0675 uart_remove_one_port(&grlib_apbuart_driver,
0676 &grlib_apbuart_ports[i]);
0677
0678 uart_unregister_driver(&grlib_apbuart_driver);
0679 platform_driver_unregister(&grlib_apbuart_of_driver);
0680 }
0681
0682 module_init(grlib_apbuart_init);
0683 module_exit(grlib_apbuart_exit);
0684
0685 MODULE_AUTHOR("Aeroflex Gaisler AB");
0686 MODULE_DESCRIPTION("GRLIB APBUART serial driver");
0687 MODULE_VERSION("2.1");
0688 MODULE_LICENSE("GPL");