1
2
3
4
5
6
7
8
9
10
11
12#include <linux/platform_device.h>
13#include <linux/module.h>
14#include <linux/console.h>
15#include <linux/serial.h>
16#include <linux/serial_core.h>
17#include <linux/tty.h>
18#include <linux/tty_flip.h>
19#include <linux/delay.h>
20#include <linux/interrupt.h>
21#include <linux/init.h>
22#include <linux/io.h>
23#include <linux/of.h>
24#include <linux/of_address.h>
25#include <linux/of_device.h>
26#include <linux/of_platform.h>
27#include <linux/clk.h>
28
29#define ULITE_NAME "ttyUL"
30#define ULITE_MAJOR 204
31#define ULITE_MINOR 187
32#define ULITE_NR_UARTS 16
33
34
35
36
37
38
39
40
41#define ULITE_RX 0x00
42#define ULITE_TX 0x04
43#define ULITE_STATUS 0x08
44#define ULITE_CONTROL 0x0c
45
46#define ULITE_REGION 16
47
48#define ULITE_STATUS_RXVALID 0x01
49#define ULITE_STATUS_RXFULL 0x02
50#define ULITE_STATUS_TXEMPTY 0x04
51#define ULITE_STATUS_TXFULL 0x08
52#define ULITE_STATUS_IE 0x10
53#define ULITE_STATUS_OVERRUN 0x20
54#define ULITE_STATUS_FRAME 0x40
55#define ULITE_STATUS_PARITY 0x80
56
57#define ULITE_CONTROL_RST_TX 0x01
58#define ULITE_CONTROL_RST_RX 0x02
59#define ULITE_CONTROL_IE 0x10
60
61struct uartlite_data {
62 const struct uartlite_reg_ops *reg_ops;
63 struct clk *clk;
64};
65
66struct uartlite_reg_ops {
67 u32 (*in)(void __iomem *addr);
68 void (*out)(u32 val, void __iomem *addr);
69};
70
71static u32 uartlite_inbe32(void __iomem *addr)
72{
73 return ioread32be(addr);
74}
75
76static void uartlite_outbe32(u32 val, void __iomem *addr)
77{
78 iowrite32be(val, addr);
79}
80
81static const struct uartlite_reg_ops uartlite_be = {
82 .in = uartlite_inbe32,
83 .out = uartlite_outbe32,
84};
85
86static u32 uartlite_inle32(void __iomem *addr)
87{
88 return ioread32(addr);
89}
90
91static void uartlite_outle32(u32 val, void __iomem *addr)
92{
93 iowrite32(val, addr);
94}
95
96static const struct uartlite_reg_ops uartlite_le = {
97 .in = uartlite_inle32,
98 .out = uartlite_outle32,
99};
100
101static inline u32 uart_in32(u32 offset, struct uart_port *port)
102{
103 struct uartlite_data *pdata = port->private_data;
104
105 return pdata->reg_ops->in(port->membase + offset);
106}
107
108static inline void uart_out32(u32 val, u32 offset, struct uart_port *port)
109{
110 struct uartlite_data *pdata = port->private_data;
111
112 pdata->reg_ops->out(val, port->membase + offset);
113}
114
115static struct uart_port ulite_ports[ULITE_NR_UARTS];
116
117
118
119
120
121static int ulite_receive(struct uart_port *port, int stat)
122{
123 struct tty_port *tport = &port->state->port;
124 unsigned char ch = 0;
125 char flag = TTY_NORMAL;
126
127 if ((stat & (ULITE_STATUS_RXVALID | ULITE_STATUS_OVERRUN
128 | ULITE_STATUS_FRAME)) == 0)
129 return 0;
130
131
132 if (stat & ULITE_STATUS_RXVALID) {
133 port->icount.rx++;
134 ch = uart_in32(ULITE_RX, port);
135
136 if (stat & ULITE_STATUS_PARITY)
137 port->icount.parity++;
138 }
139
140 if (stat & ULITE_STATUS_OVERRUN)
141 port->icount.overrun++;
142
143 if (stat & ULITE_STATUS_FRAME)
144 port->icount.frame++;
145
146
147
148 if (stat & port->ignore_status_mask & ULITE_STATUS_PARITY)
149 stat &= ~ULITE_STATUS_RXVALID;
150
151 stat &= port->read_status_mask;
152
153 if (stat & ULITE_STATUS_PARITY)
154 flag = TTY_PARITY;
155
156
157 stat &= ~port->ignore_status_mask;
158
159 if (stat & ULITE_STATUS_RXVALID)
160 tty_insert_flip_char(tport, ch, flag);
161
162 if (stat & ULITE_STATUS_FRAME)
163 tty_insert_flip_char(tport, 0, TTY_FRAME);
164
165 if (stat & ULITE_STATUS_OVERRUN)
166 tty_insert_flip_char(tport, 0, TTY_OVERRUN);
167
168 return 1;
169}
170
171static int ulite_transmit(struct uart_port *port, int stat)
172{
173 struct circ_buf *xmit = &port->state->xmit;
174
175 if (stat & ULITE_STATUS_TXFULL)
176 return 0;
177
178 if (port->x_char) {
179 uart_out32(port->x_char, ULITE_TX, port);
180 port->x_char = 0;
181 port->icount.tx++;
182 return 1;
183 }
184
185 if (uart_circ_empty(xmit) || uart_tx_stopped(port))
186 return 0;
187
188 uart_out32(xmit->buf[xmit->tail], ULITE_TX, port);
189 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE-1);
190 port->icount.tx++;
191
192
193 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
194 uart_write_wakeup(port);
195
196 return 1;
197}
198
199static irqreturn_t ulite_isr(int irq, void *dev_id)
200{
201 struct uart_port *port = dev_id;
202 int stat, busy, n = 0;
203 unsigned long flags;
204
205 do {
206 spin_lock_irqsave(&port->lock, flags);
207 stat = uart_in32(ULITE_STATUS, port);
208 busy = ulite_receive(port, stat);
209 busy |= ulite_transmit(port, stat);
210 spin_unlock_irqrestore(&port->lock, flags);
211 n++;
212 } while (busy);
213
214
215 if (n > 1) {
216 tty_flip_buffer_push(&port->state->port);
217 return IRQ_HANDLED;
218 } else {
219 return IRQ_NONE;
220 }
221}
222
223static unsigned int ulite_tx_empty(struct uart_port *port)
224{
225 unsigned long flags;
226 unsigned int ret;
227
228 spin_lock_irqsave(&port->lock, flags);
229 ret = uart_in32(ULITE_STATUS, port);
230 spin_unlock_irqrestore(&port->lock, flags);
231
232 return ret & ULITE_STATUS_TXEMPTY ? TIOCSER_TEMT : 0;
233}
234
235static unsigned int ulite_get_mctrl(struct uart_port *port)
236{
237 return TIOCM_CTS | TIOCM_DSR | TIOCM_CAR;
238}
239
240static void ulite_set_mctrl(struct uart_port *port, unsigned int mctrl)
241{
242
243}
244
245static void ulite_stop_tx(struct uart_port *port)
246{
247
248}
249
250static void ulite_start_tx(struct uart_port *port)
251{
252 ulite_transmit(port, uart_in32(ULITE_STATUS, port));
253}
254
255static void ulite_stop_rx(struct uart_port *port)
256{
257
258 port->ignore_status_mask = ULITE_STATUS_RXVALID | ULITE_STATUS_PARITY
259 | ULITE_STATUS_FRAME | ULITE_STATUS_OVERRUN;
260}
261
262static void ulite_break_ctl(struct uart_port *port, int ctl)
263{
264
265}
266
267static int ulite_startup(struct uart_port *port)
268{
269 struct uartlite_data *pdata = port->private_data;
270 int ret;
271
272 ret = clk_enable(pdata->clk);
273 if (ret) {
274 dev_err(port->dev, "Failed to enable clock\n");
275 return ret;
276 }
277
278 ret = request_irq(port->irq, ulite_isr, IRQF_SHARED | IRQF_TRIGGER_RISING,
279 "uartlite", port);
280 if (ret)
281 return ret;
282
283 uart_out32(ULITE_CONTROL_RST_RX | ULITE_CONTROL_RST_TX,
284 ULITE_CONTROL, port);
285 uart_out32(ULITE_CONTROL_IE, ULITE_CONTROL, port);
286
287 return 0;
288}
289
290static void ulite_shutdown(struct uart_port *port)
291{
292 struct uartlite_data *pdata = port->private_data;
293
294 uart_out32(0, ULITE_CONTROL, port);
295 uart_in32(ULITE_CONTROL, port);
296 free_irq(port->irq, port);
297 clk_disable(pdata->clk);
298}
299
300static void ulite_set_termios(struct uart_port *port, struct ktermios *termios,
301 struct ktermios *old)
302{
303 unsigned long flags;
304 unsigned int baud;
305
306 spin_lock_irqsave(&port->lock, flags);
307
308 port->read_status_mask = ULITE_STATUS_RXVALID | ULITE_STATUS_OVERRUN
309 | ULITE_STATUS_TXFULL;
310
311 if (termios->c_iflag & INPCK)
312 port->read_status_mask |=
313 ULITE_STATUS_PARITY | ULITE_STATUS_FRAME;
314
315 port->ignore_status_mask = 0;
316 if (termios->c_iflag & IGNPAR)
317 port->ignore_status_mask |= ULITE_STATUS_PARITY
318 | ULITE_STATUS_FRAME | ULITE_STATUS_OVERRUN;
319
320
321 if ((termios->c_cflag & CREAD) == 0)
322 port->ignore_status_mask |=
323 ULITE_STATUS_RXVALID | ULITE_STATUS_PARITY
324 | ULITE_STATUS_FRAME | ULITE_STATUS_OVERRUN;
325
326
327 baud = uart_get_baud_rate(port, termios, old, 0, 460800);
328 uart_update_timeout(port, termios->c_cflag, baud);
329
330 spin_unlock_irqrestore(&port->lock, flags);
331}
332
333static const char *ulite_type(struct uart_port *port)
334{
335 return port->type == PORT_UARTLITE ? "uartlite" : NULL;
336}
337
338static void ulite_release_port(struct uart_port *port)
339{
340 release_mem_region(port->mapbase, ULITE_REGION);
341 iounmap(port->membase);
342 port->membase = NULL;
343}
344
345static int ulite_request_port(struct uart_port *port)
346{
347 struct uartlite_data *pdata = port->private_data;
348 int ret;
349
350 pr_debug("ulite console: port=%p; port->mapbase=%llx\n",
351 port, (unsigned long long) port->mapbase);
352
353 if (!request_mem_region(port->mapbase, ULITE_REGION, "uartlite")) {
354 dev_err(port->dev, "Memory region busy\n");
355 return -EBUSY;
356 }
357
358 port->membase = ioremap(port->mapbase, ULITE_REGION);
359 if (!port->membase) {
360 dev_err(port->dev, "Unable to map registers\n");
361 release_mem_region(port->mapbase, ULITE_REGION);
362 return -EBUSY;
363 }
364
365 pdata->reg_ops = &uartlite_be;
366 ret = uart_in32(ULITE_CONTROL, port);
367 uart_out32(ULITE_CONTROL_RST_TX, ULITE_CONTROL, port);
368 ret = uart_in32(ULITE_STATUS, port);
369
370 if ((ret & ULITE_STATUS_TXEMPTY) != ULITE_STATUS_TXEMPTY)
371 pdata->reg_ops = &uartlite_le;
372
373 return 0;
374}
375
376static void ulite_config_port(struct uart_port *port, int flags)
377{
378 if (!ulite_request_port(port))
379 port->type = PORT_UARTLITE;
380}
381
382static int ulite_verify_port(struct uart_port *port, struct serial_struct *ser)
383{
384
385 return -EINVAL;
386}
387
388static void ulite_pm(struct uart_port *port, unsigned int state,
389 unsigned int oldstate)
390{
391 struct uartlite_data *pdata = port->private_data;
392
393 if (!state)
394 clk_enable(pdata->clk);
395 else
396 clk_disable(pdata->clk);
397}
398
399#ifdef CONFIG_CONSOLE_POLL
400static int ulite_get_poll_char(struct uart_port *port)
401{
402 if (!(uart_in32(ULITE_STATUS, port) & ULITE_STATUS_RXVALID))
403 return NO_POLL_CHAR;
404
405 return uart_in32(ULITE_RX, port);
406}
407
408static void ulite_put_poll_char(struct uart_port *port, unsigned char ch)
409{
410 while (uart_in32(ULITE_STATUS, port) & ULITE_STATUS_TXFULL)
411 cpu_relax();
412
413
414 uart_out32(ch, ULITE_TX, port);
415}
416#endif
417
418static const struct uart_ops ulite_ops = {
419 .tx_empty = ulite_tx_empty,
420 .set_mctrl = ulite_set_mctrl,
421 .get_mctrl = ulite_get_mctrl,
422 .stop_tx = ulite_stop_tx,
423 .start_tx = ulite_start_tx,
424 .stop_rx = ulite_stop_rx,
425 .break_ctl = ulite_break_ctl,
426 .startup = ulite_startup,
427 .shutdown = ulite_shutdown,
428 .set_termios = ulite_set_termios,
429 .type = ulite_type,
430 .release_port = ulite_release_port,
431 .request_port = ulite_request_port,
432 .config_port = ulite_config_port,
433 .verify_port = ulite_verify_port,
434 .pm = ulite_pm,
435#ifdef CONFIG_CONSOLE_POLL
436 .poll_get_char = ulite_get_poll_char,
437 .poll_put_char = ulite_put_poll_char,
438#endif
439};
440
441
442
443
444
445#ifdef CONFIG_SERIAL_UARTLITE_CONSOLE
446static void ulite_console_wait_tx(struct uart_port *port)
447{
448 u8 val;
449 unsigned long timeout;
450
451
452
453
454
455 timeout = jiffies + msecs_to_jiffies(1000);
456 while (1) {
457 val = uart_in32(ULITE_STATUS, port);
458 if ((val & ULITE_STATUS_TXFULL) == 0)
459 break;
460 if (time_after(jiffies, timeout)) {
461 dev_warn(port->dev,
462 "timeout waiting for TX buffer empty\n");
463 break;
464 }
465 cpu_relax();
466 }
467}
468
469static void ulite_console_putchar(struct uart_port *port, int ch)
470{
471 ulite_console_wait_tx(port);
472 uart_out32(ch, ULITE_TX, port);
473}
474
475static void ulite_console_write(struct console *co, const char *s,
476 unsigned int count)
477{
478 struct uart_port *port = &ulite_ports[co->index];
479 unsigned long flags;
480 unsigned int ier;
481 int locked = 1;
482
483 if (oops_in_progress) {
484 locked = spin_trylock_irqsave(&port->lock, flags);
485 } else
486 spin_lock_irqsave(&port->lock, flags);
487
488
489 ier = uart_in32(ULITE_STATUS, port) & ULITE_STATUS_IE;
490 uart_out32(0, ULITE_CONTROL, port);
491
492 uart_console_write(port, s, count, ulite_console_putchar);
493
494 ulite_console_wait_tx(port);
495
496
497 if (ier)
498 uart_out32(ULITE_CONTROL_IE, ULITE_CONTROL, port);
499
500 if (locked)
501 spin_unlock_irqrestore(&port->lock, flags);
502}
503
504static int ulite_console_setup(struct console *co, char *options)
505{
506 struct uart_port *port;
507 int baud = 9600;
508 int bits = 8;
509 int parity = 'n';
510 int flow = 'n';
511
512 if (co->index < 0 || co->index >= ULITE_NR_UARTS)
513 return -EINVAL;
514
515 port = &ulite_ports[co->index];
516
517
518 if (!port->mapbase) {
519 pr_debug("console on ttyUL%i not present\n", co->index);
520 return -ENODEV;
521 }
522
523
524 if (!port->membase) {
525 if (ulite_request_port(port))
526 return -ENODEV;
527 }
528
529 if (options)
530 uart_parse_options(options, &baud, &parity, &bits, &flow);
531
532 return uart_set_options(port, co, baud, parity, bits, flow);
533}
534
535static struct uart_driver ulite_uart_driver;
536
537static struct console ulite_console = {
538 .name = ULITE_NAME,
539 .write = ulite_console_write,
540 .device = uart_console_device,
541 .setup = ulite_console_setup,
542 .flags = CON_PRINTBUFFER,
543 .index = -1,
544 .data = &ulite_uart_driver,
545};
546
547static int __init ulite_console_init(void)
548{
549 register_console(&ulite_console);
550 return 0;
551}
552
553console_initcall(ulite_console_init);
554
555static void early_uartlite_putc(struct uart_port *port, int c)
556{
557
558
559
560
561
562
563
564
565 unsigned retries = 1000000;
566
567 while (--retries && (readl(port->membase + 8) & (1 << 3)))
568 ;
569
570
571
572 if (retries)
573 writel(c & 0xff, port->membase + 4);
574}
575
576static void early_uartlite_write(struct console *console,
577 const char *s, unsigned n)
578{
579 struct earlycon_device *device = console->data;
580 uart_console_write(&device->port, s, n, early_uartlite_putc);
581}
582
583static int __init early_uartlite_setup(struct earlycon_device *device,
584 const char *options)
585{
586 if (!device->port.membase)
587 return -ENODEV;
588
589 device->con->write = early_uartlite_write;
590 return 0;
591}
592EARLYCON_DECLARE(uartlite, early_uartlite_setup);
593OF_EARLYCON_DECLARE(uartlite_b, "xlnx,opb-uartlite-1.00.b", early_uartlite_setup);
594OF_EARLYCON_DECLARE(uartlite_a, "xlnx,xps-uartlite-1.00.a", early_uartlite_setup);
595
596#endif
597
598static struct uart_driver ulite_uart_driver = {
599 .owner = THIS_MODULE,
600 .driver_name = "uartlite",
601 .dev_name = ULITE_NAME,
602 .major = ULITE_MAJOR,
603 .minor = ULITE_MINOR,
604 .nr = ULITE_NR_UARTS,
605#ifdef CONFIG_SERIAL_UARTLITE_CONSOLE
606 .cons = &ulite_console,
607#endif
608};
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624static int ulite_assign(struct device *dev, int id, u32 base, int irq,
625 struct uartlite_data *pdata)
626{
627 struct uart_port *port;
628 int rc;
629
630
631 if (id < 0) {
632 for (id = 0; id < ULITE_NR_UARTS; id++)
633 if (ulite_ports[id].mapbase == 0)
634 break;
635 }
636 if (id < 0 || id >= ULITE_NR_UARTS) {
637 dev_err(dev, "%s%i too large\n", ULITE_NAME, id);
638 return -EINVAL;
639 }
640
641 if ((ulite_ports[id].mapbase) && (ulite_ports[id].mapbase != base)) {
642 dev_err(dev, "cannot assign to %s%i; it is already in use\n",
643 ULITE_NAME, id);
644 return -EBUSY;
645 }
646
647 port = &ulite_ports[id];
648
649 spin_lock_init(&port->lock);
650 port->fifosize = 16;
651 port->regshift = 2;
652 port->iotype = UPIO_MEM;
653 port->iobase = 1;
654 port->mapbase = base;
655 port->membase = NULL;
656 port->ops = &ulite_ops;
657 port->irq = irq;
658 port->flags = UPF_BOOT_AUTOCONF;
659 port->dev = dev;
660 port->type = PORT_UNKNOWN;
661 port->line = id;
662 port->private_data = pdata;
663
664 dev_set_drvdata(dev, port);
665
666
667 rc = uart_add_one_port(&ulite_uart_driver, port);
668 if (rc) {
669 dev_err(dev, "uart_add_one_port() failed; err=%i\n", rc);
670 port->mapbase = 0;
671 dev_set_drvdata(dev, NULL);
672 return rc;
673 }
674
675 return 0;
676}
677
678
679
680
681
682static int ulite_release(struct device *dev)
683{
684 struct uart_port *port = dev_get_drvdata(dev);
685 int rc = 0;
686
687 if (port) {
688 rc = uart_remove_one_port(&ulite_uart_driver, port);
689 dev_set_drvdata(dev, NULL);
690 port->mapbase = 0;
691 }
692
693 return rc;
694}
695
696
697
698
699
700
701
702static int __maybe_unused ulite_suspend(struct device *dev)
703{
704 struct uart_port *port = dev_get_drvdata(dev);
705
706 if (port)
707 uart_suspend_port(&ulite_uart_driver, port);
708
709 return 0;
710}
711
712
713
714
715
716
717
718static int __maybe_unused ulite_resume(struct device *dev)
719{
720 struct uart_port *port = dev_get_drvdata(dev);
721
722 if (port)
723 uart_resume_port(&ulite_uart_driver, port);
724
725 return 0;
726}
727
728
729
730
731
732static SIMPLE_DEV_PM_OPS(ulite_pm_ops, ulite_suspend, ulite_resume);
733
734#if defined(CONFIG_OF)
735
736static const struct of_device_id ulite_of_match[] = {
737 { .compatible = "xlnx,opb-uartlite-1.00.b", },
738 { .compatible = "xlnx,xps-uartlite-1.00.a", },
739 {}
740};
741MODULE_DEVICE_TABLE(of, ulite_of_match);
742#endif
743
744static int ulite_probe(struct platform_device *pdev)
745{
746 struct resource *res;
747 struct uartlite_data *pdata;
748 int irq, ret;
749 int id = pdev->id;
750#ifdef CONFIG_OF
751 const __be32 *prop;
752
753 prop = of_get_property(pdev->dev.of_node, "port-number", NULL);
754 if (prop)
755 id = be32_to_cpup(prop);
756#endif
757 pdata = devm_kzalloc(&pdev->dev, sizeof(struct uartlite_data),
758 GFP_KERNEL);
759 if (!pdata)
760 return -ENOMEM;
761
762 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
763 if (!res)
764 return -ENODEV;
765
766 irq = platform_get_irq(pdev, 0);
767 if (irq <= 0)
768 return -ENXIO;
769
770 pdata->clk = devm_clk_get(&pdev->dev, "ulite_clk");
771 if (IS_ERR(pdata->clk)) {
772 if (PTR_ERR(pdata->clk) != -ENOENT)
773 return PTR_ERR(pdata->clk);
774
775
776
777
778
779 pdata->clk = NULL;
780 }
781
782 ret = clk_prepare(pdata->clk);
783 if (ret) {
784 dev_err(&pdev->dev, "Failed to prepare clock\n");
785 return ret;
786 }
787
788 return ulite_assign(&pdev->dev, id, res->start, irq, pdata);
789}
790
791static int ulite_remove(struct platform_device *pdev)
792{
793 struct uart_port *port = dev_get_drvdata(&pdev->dev);
794 struct uartlite_data *pdata = port->private_data;
795
796 clk_disable_unprepare(pdata->clk);
797 return ulite_release(&pdev->dev);
798}
799
800
801MODULE_ALIAS("platform:uartlite");
802
803static struct platform_driver ulite_platform_driver = {
804 .probe = ulite_probe,
805 .remove = ulite_remove,
806 .driver = {
807 .name = "uartlite",
808 .of_match_table = of_match_ptr(ulite_of_match),
809 .pm = &ulite_pm_ops,
810 },
811};
812
813
814
815
816
817static int __init ulite_init(void)
818{
819 int ret;
820
821 pr_debug("uartlite: calling uart_register_driver()\n");
822 ret = uart_register_driver(&ulite_uart_driver);
823 if (ret)
824 goto err_uart;
825
826 pr_debug("uartlite: calling platform_driver_register()\n");
827 ret = platform_driver_register(&ulite_platform_driver);
828 if (ret)
829 goto err_plat;
830
831 return 0;
832
833err_plat:
834 uart_unregister_driver(&ulite_uart_driver);
835err_uart:
836 pr_err("registering uartlite driver failed: err=%i", ret);
837 return ret;
838}
839
840static void __exit ulite_exit(void)
841{
842 platform_driver_unregister(&ulite_platform_driver);
843 uart_unregister_driver(&ulite_uart_driver);
844}
845
846module_init(ulite_init);
847module_exit(ulite_exit);
848
849MODULE_AUTHOR("Peter Korsgaard <jacmet@sunsite.dk>");
850MODULE_DESCRIPTION("Xilinx uartlite serial driver");
851MODULE_LICENSE("GPL");
852