]> rtime.felk.cvut.cz Git - zynq/linux.git/blob - drivers/tty/serial/uartlite.c
serial-uartlite: Remove ULITE_NR_PORTS macro
[zynq/linux.git] / drivers / tty / serial / uartlite.c
1 /*
2  * uartlite.c: Serial driver for Xilinx uartlite serial controller
3  *
4  * Copyright (C) 2006 Peter Korsgaard <jacmet@sunsite.dk>
5  * Copyright (C) 2007 Secret Lab Technologies Ltd.
6  *
7  * This file is licensed under the terms of the GNU General Public License
8  * version 2.  This program is licensed "as is" without any warranty of any
9  * kind, whether express or implied.
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 #include <linux/pm_runtime.h>
29
30 #define ULITE_NAME              "ttyUL"
31 #define ULITE_MAJOR             204
32 #define ULITE_MINOR             187
33
34 /* ---------------------------------------------------------------------
35  * Register definitions
36  *
37  * For register details see datasheet:
38  * http://www.xilinx.com/support/documentation/ip_documentation/opb_uartlite.pdf
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 #define UART_AUTOSUSPEND_TIMEOUT        3000
61
62 /* Static pointer to console port */
63 #ifdef CONFIG_SERIAL_UARTLITE_CONSOLE
64 static struct uart_port *console_port;
65 #endif
66
67 struct uartlite_data {
68         const struct uartlite_reg_ops *reg_ops;
69         struct clk *clk;
70         int id;
71         struct uart_driver *ulite_uart_driver;
72 };
73
74 struct uartlite_reg_ops {
75         u32 (*in)(void __iomem *addr);
76         void (*out)(u32 val, void __iomem *addr);
77 };
78
79 static u32 uartlite_inbe32(void __iomem *addr)
80 {
81         return ioread32be(addr);
82 }
83
84 static void uartlite_outbe32(u32 val, void __iomem *addr)
85 {
86         iowrite32be(val, addr);
87 }
88
89 static const struct uartlite_reg_ops uartlite_be = {
90         .in = uartlite_inbe32,
91         .out = uartlite_outbe32,
92 };
93
94 static u32 uartlite_inle32(void __iomem *addr)
95 {
96         return ioread32(addr);
97 }
98
99 static void uartlite_outle32(u32 val, void __iomem *addr)
100 {
101         iowrite32(val, addr);
102 }
103
104 static const struct uartlite_reg_ops uartlite_le = {
105         .in = uartlite_inle32,
106         .out = uartlite_outle32,
107 };
108
109 static inline u32 uart_in32(u32 offset, struct uart_port *port)
110 {
111         struct uartlite_data *pdata = port->private_data;
112
113         return pdata->reg_ops->in(port->membase + offset);
114 }
115
116 static inline void uart_out32(u32 val, u32 offset, struct uart_port *port)
117 {
118         struct uartlite_data *pdata = port->private_data;
119
120         pdata->reg_ops->out(val, port->membase + offset);
121 }
122
123
124 /* ---------------------------------------------------------------------
125  * Core UART driver operations
126  */
127
128 static int ulite_receive(struct uart_port *port, int stat)
129 {
130         struct tty_port *tport = &port->state->port;
131         unsigned char ch = 0;
132         char flag = TTY_NORMAL;
133
134         if ((stat & (ULITE_STATUS_RXVALID | ULITE_STATUS_OVERRUN
135                      | ULITE_STATUS_FRAME)) == 0)
136                 return 0;
137
138         /* stats */
139         if (stat & ULITE_STATUS_RXVALID) {
140                 port->icount.rx++;
141                 ch = uart_in32(ULITE_RX, port);
142
143                 if (stat & ULITE_STATUS_PARITY)
144                         port->icount.parity++;
145         }
146
147         if (stat & ULITE_STATUS_OVERRUN)
148                 port->icount.overrun++;
149
150         if (stat & ULITE_STATUS_FRAME)
151                 port->icount.frame++;
152
153
154         /* drop byte with parity error if IGNPAR specificed */
155         if (stat & port->ignore_status_mask & ULITE_STATUS_PARITY)
156                 stat &= ~ULITE_STATUS_RXVALID;
157
158         stat &= port->read_status_mask;
159
160         if (stat & ULITE_STATUS_PARITY)
161                 flag = TTY_PARITY;
162
163
164         stat &= ~port->ignore_status_mask;
165
166         if (stat & ULITE_STATUS_RXVALID)
167                 tty_insert_flip_char(tport, ch, flag);
168
169         if (stat & ULITE_STATUS_FRAME)
170                 tty_insert_flip_char(tport, 0, TTY_FRAME);
171
172         if (stat & ULITE_STATUS_OVERRUN)
173                 tty_insert_flip_char(tport, 0, TTY_OVERRUN);
174
175         return 1;
176 }
177
178 static int ulite_transmit(struct uart_port *port, int stat)
179 {
180         struct circ_buf *xmit  = &port->state->xmit;
181
182         if (stat & ULITE_STATUS_TXFULL)
183                 return 0;
184
185         if (port->x_char) {
186                 uart_out32(port->x_char, ULITE_TX, port);
187                 port->x_char = 0;
188                 port->icount.tx++;
189                 return 1;
190         }
191
192         if (uart_circ_empty(xmit) || uart_tx_stopped(port))
193                 return 0;
194
195         uart_out32(xmit->buf[xmit->tail], ULITE_TX, port);
196         xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE-1);
197         port->icount.tx++;
198
199         /* wake up */
200         if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
201                 uart_write_wakeup(port);
202
203         return 1;
204 }
205
206 static irqreturn_t ulite_isr(int irq, void *dev_id)
207 {
208         struct uart_port *port = dev_id;
209         int stat, busy, n = 0;
210         unsigned long flags;
211
212         do {
213                 spin_lock_irqsave(&port->lock, flags);
214                 stat = uart_in32(ULITE_STATUS, port);
215                 busy  = ulite_receive(port, stat);
216                 busy |= ulite_transmit(port, stat);
217                 spin_unlock_irqrestore(&port->lock, flags);
218                 n++;
219         } while (busy);
220
221         /* work done? */
222         if (n > 1) {
223                 tty_flip_buffer_push(&port->state->port);
224                 return IRQ_HANDLED;
225         } else {
226                 return IRQ_NONE;
227         }
228 }
229
230 static unsigned int ulite_tx_empty(struct uart_port *port)
231 {
232         unsigned long flags;
233         unsigned int ret;
234
235         spin_lock_irqsave(&port->lock, flags);
236         ret = uart_in32(ULITE_STATUS, port);
237         spin_unlock_irqrestore(&port->lock, flags);
238
239         return ret & ULITE_STATUS_TXEMPTY ? TIOCSER_TEMT : 0;
240 }
241
242 static unsigned int ulite_get_mctrl(struct uart_port *port)
243 {
244         return TIOCM_CTS | TIOCM_DSR | TIOCM_CAR;
245 }
246
247 static void ulite_set_mctrl(struct uart_port *port, unsigned int mctrl)
248 {
249         /* N/A */
250 }
251
252 static void ulite_stop_tx(struct uart_port *port)
253 {
254         /* N/A */
255 }
256
257 static void ulite_start_tx(struct uart_port *port)
258 {
259         ulite_transmit(port, uart_in32(ULITE_STATUS, port));
260 }
261
262 static void ulite_stop_rx(struct uart_port *port)
263 {
264         /* don't forward any more data (like !CREAD) */
265         port->ignore_status_mask = ULITE_STATUS_RXVALID | ULITE_STATUS_PARITY
266                 | ULITE_STATUS_FRAME | ULITE_STATUS_OVERRUN;
267 }
268
269 static void ulite_break_ctl(struct uart_port *port, int ctl)
270 {
271         /* N/A */
272 }
273
274 static int ulite_startup(struct uart_port *port)
275 {
276         struct uartlite_data *pdata = port->private_data;
277         int ret;
278
279         ret = clk_enable(pdata->clk);
280         if (ret) {
281                 dev_err(port->dev, "Failed to enable clock\n");
282                 return ret;
283         }
284
285         ret = request_irq(port->irq, ulite_isr, IRQF_SHARED | IRQF_TRIGGER_RISING,
286                           "uartlite", port);
287         if (ret)
288                 return ret;
289
290         uart_out32(ULITE_CONTROL_RST_RX | ULITE_CONTROL_RST_TX,
291                 ULITE_CONTROL, port);
292         uart_out32(ULITE_CONTROL_IE, ULITE_CONTROL, port);
293
294         return 0;
295 }
296
297 static void ulite_shutdown(struct uart_port *port)
298 {
299         struct uartlite_data *pdata = port->private_data;
300
301         uart_out32(0, ULITE_CONTROL, port);
302         uart_in32(ULITE_CONTROL, port); /* dummy */
303         free_irq(port->irq, port);
304         clk_disable(pdata->clk);
305 }
306
307 static void ulite_set_termios(struct uart_port *port, struct ktermios *termios,
308                               struct ktermios *old)
309 {
310         unsigned long flags;
311         unsigned int baud;
312
313         spin_lock_irqsave(&port->lock, flags);
314
315         port->read_status_mask = ULITE_STATUS_RXVALID | ULITE_STATUS_OVERRUN
316                 | ULITE_STATUS_TXFULL;
317
318         if (termios->c_iflag & INPCK)
319                 port->read_status_mask |=
320                         ULITE_STATUS_PARITY | ULITE_STATUS_FRAME;
321
322         port->ignore_status_mask = 0;
323         if (termios->c_iflag & IGNPAR)
324                 port->ignore_status_mask |= ULITE_STATUS_PARITY
325                         | ULITE_STATUS_FRAME | ULITE_STATUS_OVERRUN;
326
327         /* ignore all characters if CREAD is not set */
328         if ((termios->c_cflag & CREAD) == 0)
329                 port->ignore_status_mask |=
330                         ULITE_STATUS_RXVALID | ULITE_STATUS_PARITY
331                         | ULITE_STATUS_FRAME | ULITE_STATUS_OVERRUN;
332
333         /* update timeout */
334         baud = uart_get_baud_rate(port, termios, old, 0, 460800);
335         uart_update_timeout(port, termios->c_cflag, baud);
336
337         spin_unlock_irqrestore(&port->lock, flags);
338 }
339
340 static const char *ulite_type(struct uart_port *port)
341 {
342         return port->type == PORT_UARTLITE ? "uartlite" : NULL;
343 }
344
345 static void ulite_release_port(struct uart_port *port)
346 {
347         release_mem_region(port->mapbase, ULITE_REGION);
348         iounmap(port->membase);
349         port->membase = NULL;
350 }
351
352 static int ulite_request_port(struct uart_port *port)
353 {
354         struct uartlite_data *pdata = port->private_data;
355         int ret;
356
357         pr_debug("ulite console: port=%p; port->mapbase=%llx\n",
358                  port, (unsigned long long) port->mapbase);
359
360         if (!request_mem_region(port->mapbase, ULITE_REGION, "uartlite")) {
361                 dev_err(port->dev, "Memory region busy\n");
362                 return -EBUSY;
363         }
364
365         port->membase = ioremap(port->mapbase, ULITE_REGION);
366         if (!port->membase) {
367                 dev_err(port->dev, "Unable to map registers\n");
368                 release_mem_region(port->mapbase, ULITE_REGION);
369                 return -EBUSY;
370         }
371
372         pdata->reg_ops = &uartlite_be;
373         ret = uart_in32(ULITE_CONTROL, port);
374         uart_out32(ULITE_CONTROL_RST_TX, ULITE_CONTROL, port);
375         ret = uart_in32(ULITE_STATUS, port);
376         /* Endianess detection */
377         if ((ret & ULITE_STATUS_TXEMPTY) != ULITE_STATUS_TXEMPTY)
378                 pdata->reg_ops = &uartlite_le;
379
380         return 0;
381 }
382
383 static void ulite_config_port(struct uart_port *port, int flags)
384 {
385         if (!ulite_request_port(port))
386                 port->type = PORT_UARTLITE;
387 }
388
389 static int ulite_verify_port(struct uart_port *port, struct serial_struct *ser)
390 {
391         /* we don't want the core code to modify any port params */
392         return -EINVAL;
393 }
394
395 static void ulite_pm(struct uart_port *port, unsigned int state,
396               unsigned int oldstate)
397 {
398         if (!state) {
399                 pm_runtime_get_sync(port->dev);
400         } else {
401                 pm_runtime_mark_last_busy(port->dev);
402                 pm_runtime_put_autosuspend(port->dev);
403         }
404 }
405
406 #ifdef CONFIG_CONSOLE_POLL
407 static int ulite_get_poll_char(struct uart_port *port)
408 {
409         if (!(uart_in32(ULITE_STATUS, port) & ULITE_STATUS_RXVALID))
410                 return NO_POLL_CHAR;
411
412         return uart_in32(ULITE_RX, port);
413 }
414
415 static void ulite_put_poll_char(struct uart_port *port, unsigned char ch)
416 {
417         while (uart_in32(ULITE_STATUS, port) & ULITE_STATUS_TXFULL)
418                 cpu_relax();
419
420         /* write char to device */
421         uart_out32(ch, ULITE_TX, port);
422 }
423 #endif
424
425 static const struct uart_ops ulite_ops = {
426         .tx_empty       = ulite_tx_empty,
427         .set_mctrl      = ulite_set_mctrl,
428         .get_mctrl      = ulite_get_mctrl,
429         .stop_tx        = ulite_stop_tx,
430         .start_tx       = ulite_start_tx,
431         .stop_rx        = ulite_stop_rx,
432         .break_ctl      = ulite_break_ctl,
433         .startup        = ulite_startup,
434         .shutdown       = ulite_shutdown,
435         .set_termios    = ulite_set_termios,
436         .type           = ulite_type,
437         .release_port   = ulite_release_port,
438         .request_port   = ulite_request_port,
439         .config_port    = ulite_config_port,
440         .verify_port    = ulite_verify_port,
441         .pm             = ulite_pm,
442 #ifdef CONFIG_CONSOLE_POLL
443         .poll_get_char  = ulite_get_poll_char,
444         .poll_put_char  = ulite_put_poll_char,
445 #endif
446 };
447
448 /* ---------------------------------------------------------------------
449  * Console driver operations
450  */
451
452 #ifdef CONFIG_SERIAL_UARTLITE_CONSOLE
453 static void ulite_console_wait_tx(struct uart_port *port)
454 {
455         u8 val;
456         unsigned long timeout;
457
458         /*
459          * Spin waiting for TX fifo to have space available.
460          * When using the Microblaze Debug Module this can take up to 1s
461          */
462         timeout = jiffies + msecs_to_jiffies(1000);
463         while (1) {
464                 val = uart_in32(ULITE_STATUS, port);
465                 if ((val & ULITE_STATUS_TXFULL) == 0)
466                         break;
467                 if (time_after(jiffies, timeout)) {
468                         dev_warn(port->dev,
469                                  "timeout waiting for TX buffer empty\n");
470                         break;
471                 }
472                 cpu_relax();
473         }
474 }
475
476 static void ulite_console_putchar(struct uart_port *port, int ch)
477 {
478         ulite_console_wait_tx(port);
479         uart_out32(ch, ULITE_TX, port);
480 }
481
482 static void ulite_console_write(struct console *co, const char *s,
483                                 unsigned int count)
484 {
485         struct uart_port *port = console_port;
486         unsigned long flags;
487         unsigned int ier;
488         int locked = 1;
489
490         if (oops_in_progress) {
491                 locked = spin_trylock_irqsave(&port->lock, flags);
492         } else
493                 spin_lock_irqsave(&port->lock, flags);
494
495         /* save and disable interrupt */
496         ier = uart_in32(ULITE_STATUS, port) & ULITE_STATUS_IE;
497         uart_out32(0, ULITE_CONTROL, port);
498
499         uart_console_write(port, s, count, ulite_console_putchar);
500
501         ulite_console_wait_tx(port);
502
503         /* restore interrupt state */
504         if (ier)
505                 uart_out32(ULITE_CONTROL_IE, ULITE_CONTROL, port);
506
507         if (locked)
508                 spin_unlock_irqrestore(&port->lock, flags);
509 }
510
511 static int ulite_console_setup(struct console *co, char *options)
512 {
513         struct uart_port *port;
514         int baud = 9600;
515         int bits = 8;
516         int parity = 'n';
517         int flow = 'n';
518
519
520         port = console_port;
521
522         /* Has the device been initialized yet? */
523         if (!port->mapbase) {
524                 pr_debug("console on ttyUL%i not present\n", co->index);
525                 return -ENODEV;
526         }
527
528         /* not initialized yet? */
529         if (!port->membase) {
530                 if (ulite_request_port(port))
531                         return -ENODEV;
532         }
533
534         if (options)
535                 uart_parse_options(options, &baud, &parity, &bits, &flow);
536
537         return uart_set_options(port, co, baud, parity, bits, flow);
538 }
539
540 static void early_uartlite_putc(struct uart_port *port, int c)
541 {
542         /*
543          * Limit how many times we'll spin waiting for TX FIFO status.
544          * This will prevent lockups if the base address is incorrectly
545          * set, or any other issue on the UARTLITE.
546          * This limit is pretty arbitrary, unless we are at about 10 baud
547          * we'll never timeout on a working UART.
548          */
549
550         unsigned retries = 1000000;
551         /* read status bit - 0x8 offset */
552         while (--retries && (readl(port->membase + 8) & (1 << 3)))
553                 ;
554
555         /* Only attempt the iowrite if we didn't timeout */
556         /* write to TX_FIFO - 0x4 offset */
557         if (retries)
558                 writel(c & 0xff, port->membase + 4);
559 }
560
561 static void early_uartlite_write(struct console *console,
562                                  const char *s, unsigned n)
563 {
564         struct earlycon_device *device = console->data;
565         uart_console_write(&device->port, s, n, early_uartlite_putc);
566 }
567
568 static int __init early_uartlite_setup(struct earlycon_device *device,
569                                        const char *options)
570 {
571         if (!device->port.membase)
572                 return -ENODEV;
573
574         device->con->write = early_uartlite_write;
575         return 0;
576 }
577 EARLYCON_DECLARE(uartlite, early_uartlite_setup);
578 OF_EARLYCON_DECLARE(uartlite_b, "xlnx,opb-uartlite-1.00.b", early_uartlite_setup);
579 OF_EARLYCON_DECLARE(uartlite_a, "xlnx,xps-uartlite-1.00.a", early_uartlite_setup);
580
581 #endif /* CONFIG_SERIAL_UARTLITE_CONSOLE */
582
583 /* ---------------------------------------------------------------------
584  * Port assignment functions (mapping devices to uart_port structures)
585  */
586
587 /** ulite_assign: register a uartlite device with the driver
588  *
589  * @dev: pointer to device structure
590  * @id: requested id number.  Pass -1 for automatic port assignment
591  * @base: base address of uartlite registers
592  * @irq: irq number for uartlite
593  * @pdata: private data for uartlite
594  *
595  * Returns: 0 on success, <0 otherwise
596  */
597 static int ulite_assign(struct device *dev, int id, u32 base, int irq,
598                 struct uartlite_data *pdata)
599 {
600         struct uart_port *port;
601         int rc;
602
603         port = devm_kzalloc(dev, sizeof(*port), GFP_KERNEL);
604         if (!port)
605                 return -ENOMEM;
606
607         spin_lock_init(&port->lock);
608         port->fifosize = 16;
609         port->regshift = 2;
610         port->iotype = UPIO_MEM;
611         port->iobase = 1; /* mark port in use */
612         port->mapbase = base;
613         port->membase = NULL;
614         port->ops = &ulite_ops;
615         port->irq = irq;
616         port->flags = UPF_BOOT_AUTOCONF;
617         port->dev = dev;
618         port->type = PORT_UNKNOWN;
619         port->private_data = pdata;
620
621         dev_set_drvdata(dev, port);
622
623 #ifdef CONFIG_SERIAL_UARTLITE_CONSOLE
624         /*
625          * If console hasn't been found yet try to assign this port
626          * because it is required to be assigned for console setup function.
627          * If register_console() don't assign value, then console_port pointer
628          * is cleanup.
629          */
630         if (!console_port)
631                 console_port = port;
632 #endif
633
634         /* Register the port */
635         rc = uart_add_one_port(pdata->ulite_uart_driver, port);
636         if (rc) {
637                 dev_err(dev, "uart_add_one_port() failed; err=%i\n", rc);
638                 port->mapbase = 0;
639                 dev_set_drvdata(dev, NULL);
640                 return rc;
641         }
642
643 #ifdef CONFIG_SERIAL_UARTLITE_CONSOLE
644         /* This is not port which is used for console that's why clean it up */
645         if (console_port == port &&
646             !(pdata->ulite_uart_driver->cons->flags & CON_ENABLED))
647                 console_port = NULL;
648 #endif
649
650         return 0;
651 }
652
653 /** ulite_release: register a uartlite device with the driver
654  *
655  * @dev: pointer to device structure
656  */
657 static int ulite_release(struct device *dev)
658 {
659         struct uart_port *port = dev_get_drvdata(dev);
660         struct uartlite_data *pdata = port->private_data;
661         int rc = 0;
662
663         if (port) {
664                 rc = uart_remove_one_port(pdata->ulite_uart_driver, port);
665                 dev_set_drvdata(dev, NULL);
666                 port->mapbase = 0;
667         }
668
669         return rc;
670 }
671
672 /**
673  * ulite_suspend - Stop the device.
674  *
675  * @dev: handle to the device structure.
676  * Return: 0 always.
677  */
678 static int __maybe_unused ulite_suspend(struct device *dev)
679 {
680         struct uart_port *port = dev_get_drvdata(dev);
681         struct uartlite_data *pdata = port->private_data;
682
683         if (port)
684                 uart_suspend_port(pdata->ulite_uart_driver, port);
685
686         return 0;
687 }
688
689 /**
690  * ulite_resume - Resume the device.
691  *
692  * @dev: handle to the device structure.
693  * Return: 0 on success, errno otherwise.
694  */
695 static int __maybe_unused ulite_resume(struct device *dev)
696 {
697         struct uart_port *port = dev_get_drvdata(dev);
698         struct uartlite_data *pdata = port->private_data;
699
700         if (port)
701                 uart_resume_port(pdata->ulite_uart_driver, port);
702
703         return 0;
704 }
705
706 static int __maybe_unused ulite_runtime_suspend(struct device *dev)
707 {
708         struct uart_port *port = dev_get_drvdata(dev);
709         struct uartlite_data *pdata = port->private_data;
710
711         clk_disable(pdata->clk);
712         return 0;
713 };
714
715 static int __maybe_unused ulite_runtime_resume(struct device *dev)
716 {
717         struct uart_port *port = dev_get_drvdata(dev);
718         struct uartlite_data *pdata = port->private_data;
719
720         clk_enable(pdata->clk);
721         return 0;
722 }
723 /* ---------------------------------------------------------------------
724  * Platform bus binding
725  */
726
727 static const struct dev_pm_ops ulite_pm_ops = {
728         SET_SYSTEM_SLEEP_PM_OPS(ulite_suspend, ulite_resume)
729         SET_RUNTIME_PM_OPS(ulite_runtime_suspend,
730                            ulite_runtime_resume, NULL)
731 };
732
733 #if defined(CONFIG_OF)
734 /* Match table for of_platform binding */
735 static const struct of_device_id ulite_of_match[] = {
736         { .compatible = "xlnx,opb-uartlite-1.00.b", },
737         { .compatible = "xlnx,xps-uartlite-1.00.a", },
738         {}
739 };
740 MODULE_DEVICE_TABLE(of, ulite_of_match);
741 #endif /* CONFIG_OF */
742
743 /*
744  * Maximum number of instances without alias IDs but if there is alias
745  * which target "< MAX_UART_INSTANCES" range this ID can't be used.
746  */
747 #define MAX_UART_INSTANCES      256
748
749 /* Stores static aliases list */
750 static DECLARE_BITMAP(alias_bitmap, MAX_UART_INSTANCES);
751 static int alias_bitmap_initialized;
752
753 /* Stores actual bitmap of allocated IDs with alias IDs together */
754 static DECLARE_BITMAP(bitmap, MAX_UART_INSTANCES);
755 /* Protect bitmap operations to have unique IDs */
756 static DEFINE_MUTEX(bitmap_lock);
757
758 static int ulite_get_id(struct platform_device *pdev)
759 {
760         int ret;
761         int id = pdev->id;
762 #ifdef CONFIG_OF
763         const __be32 *prop;
764
765         prop = of_get_property(pdev->dev.of_node, "port-number", NULL);
766         if (prop)
767                 id = be32_to_cpup(prop);
768 #endif
769
770         mutex_lock(&bitmap_lock);
771
772         /* Alias list is stable that's why get alias bitmap only once */
773         if (!alias_bitmap_initialized) {
774                 ret = of_alias_get_alias_list(ulite_of_match, "serial",
775                                               alias_bitmap, MAX_UART_INSTANCES);
776                 if (ret) {
777                         mutex_unlock(&bitmap_lock);
778                         return ret;
779                 }
780
781                 alias_bitmap_initialized++;
782         }
783
784         /* Make sure that alias ID is not taken by instance without alias */
785         bitmap_or(bitmap, bitmap, alias_bitmap, MAX_UART_INSTANCES);
786
787         dev_dbg(&pdev->dev, "Alias bitmap: %*pb\n",
788                 MAX_UART_INSTANCES, bitmap);
789
790         /* Look for a serialN alias */
791         if (id < 0)
792                 id = of_alias_get_id(pdev->dev.of_node, "serial");
793
794         if (id < 0) {
795                 dev_warn(&pdev->dev,
796                          "No serial alias passed. Using the first free id\n");
797
798                 /*
799                  * Start with id 0 and check if there is no serial0 alias
800                  * which points to device which is compatible with this driver.
801                  * If alias exists then try next free position.
802                  */
803                 id = 0;
804
805                 for (;;) {
806                         dev_info(&pdev->dev, "Checking id %d\n", id);
807                         id = find_next_zero_bit(bitmap, MAX_UART_INSTANCES, id);
808
809                         /* No free empty instance */
810                         if (id == MAX_UART_INSTANCES) {
811                                 dev_err(&pdev->dev, "No free ID\n");
812                                 mutex_unlock(&bitmap_lock);
813                                 return -EINVAL;
814                         }
815
816                         dev_dbg(&pdev->dev, "The empty id is %d\n", id);
817                         /* Check if ID is empty */
818                         if (!test_and_set_bit(id, bitmap)) {
819                                 /* Break the loop if bit is taken */
820                                 dev_dbg(&pdev->dev,
821                                         "Selected ID %d allocation passed\n",
822                                         id);
823                                 break;
824                         }
825                         dev_dbg(&pdev->dev,
826                                 "Selected ID %d allocation failed\n", id);
827                         /* if taking bit fails then try next one */
828                         id++;
829                 }
830         }
831
832         mutex_unlock(&bitmap_lock);
833
834         return id;
835 }
836
837 static int ulite_probe(struct platform_device *pdev)
838 {
839         struct resource *res;
840         struct uartlite_data *pdata;
841         int irq, ret;
842         struct uart_driver *ulite_uart_driver;
843         char *driver_name;
844 #ifdef CONFIG_SERIAL_UARTLITE_CONSOLE
845         struct console *ulite_console;
846 #endif
847
848         pdata = devm_kzalloc(&pdev->dev, sizeof(struct uartlite_data),
849                         GFP_KERNEL);
850         if (!pdata)
851                 return -ENOMEM;
852
853         ulite_uart_driver = devm_kzalloc(&pdev->dev,
854                                          sizeof(*ulite_uart_driver),
855                                          GFP_KERNEL);
856         if (!ulite_uart_driver)
857                 return -ENOMEM;
858
859         pdata->id = ulite_get_id(pdev);
860         if (pdata->id < 0)
861                 return pdata->id;
862
863         /* There is a need to use unique driver name */
864         driver_name = devm_kasprintf(&pdev->dev, GFP_KERNEL, "%s%d",
865                                      ULITE_NAME, pdata->id);
866         if (!driver_name) {
867                 ret = -ENOMEM;
868                 goto err_out_id;
869         }
870
871         ulite_uart_driver->owner = THIS_MODULE;
872         ulite_uart_driver->driver_name = driver_name;
873         ulite_uart_driver->dev_name = ULITE_NAME;
874         ulite_uart_driver->major = ULITE_MAJOR;
875         ulite_uart_driver->minor = pdata->id;
876         ulite_uart_driver->nr = 1;
877 #ifdef CONFIG_SERIAL_UARTLITE_CONSOLE
878         ulite_console = devm_kzalloc(&pdev->dev, sizeof(*ulite_console),
879                                      GFP_KERNEL);
880         if (!ulite_console) {
881                 ret = -ENOMEM;
882                 goto err_out_id;
883         }
884
885         strncpy(ulite_console->name, ULITE_NAME,
886                 sizeof(ulite_console->name));
887         ulite_console->index = pdata->id;
888         ulite_console->write = ulite_console_write;
889         ulite_console->device = uart_console_device;
890         ulite_console->setup = ulite_console_setup;
891         ulite_console->flags = CON_PRINTBUFFER;
892         ulite_uart_driver->cons = ulite_console;
893         ulite_console->data = ulite_uart_driver;
894 #endif
895
896         dev_dbg(&pdev->dev, "uartlite: calling uart_register_driver()\n");
897         ret = uart_register_driver(ulite_uart_driver);
898         if (ret < 0) {
899                 dev_err(&pdev->dev, "Failed to register driver\n");
900                 goto err_out_id;
901         }
902
903         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
904         if (!res) {
905                 ret = -ENODEV;
906                 goto err_out_unregister_driver;
907         }
908
909         irq = platform_get_irq(pdev, 0);
910         if (irq <= 0) {
911                 ret = -ENXIO;
912                 goto err_out_unregister_driver;
913         }
914
915         pdata->clk = devm_clk_get(&pdev->dev, "s_axi_aclk");
916         if (IS_ERR(pdata->clk)) {
917                 if (PTR_ERR(pdata->clk) != -ENOENT) {
918                         ret = PTR_ERR(pdata->clk);
919                         goto err_out_unregister_driver;
920                 }
921
922                 /*
923                  * Clock framework support is optional, continue on
924                  * anyways if we don't find a matching clock.
925                  */
926                 pdata->clk = NULL;
927         }
928
929         ret = clk_prepare_enable(pdata->clk);
930         if (ret) {
931                 dev_err(&pdev->dev, "Failed to prepare clock\n");
932                 goto err_out_unregister_driver;
933         }
934
935         pm_runtime_use_autosuspend(&pdev->dev);
936         pm_runtime_set_autosuspend_delay(&pdev->dev, UART_AUTOSUSPEND_TIMEOUT);
937         pm_runtime_set_active(&pdev->dev);
938         pm_runtime_enable(&pdev->dev);
939
940         ulite_uart_driver->tty_driver->name_base = pdata->id;
941         pdata->ulite_uart_driver = ulite_uart_driver;
942         ret = ulite_assign(&pdev->dev, pdata->id, res->start, irq, pdata);
943         if (ret < 0)
944                 goto err_out_clk_disable;
945
946         pm_runtime_mark_last_busy(&pdev->dev);
947         pm_runtime_put_autosuspend(&pdev->dev);
948         return 0;
949
950 err_out_clk_disable:
951         clk_disable_unprepare(pdata->clk);
952         pm_runtime_disable(&pdev->dev);
953         pm_runtime_set_suspended(&pdev->dev);
954         pm_runtime_dont_use_autosuspend(&pdev->dev);
955 err_out_unregister_driver:
956         uart_unregister_driver(pdata->ulite_uart_driver);
957 err_out_id:
958         mutex_lock(&bitmap_lock);
959         clear_bit(pdata->id, bitmap);
960         mutex_unlock(&bitmap_lock);
961         return ret;
962 }
963
964 static int ulite_remove(struct platform_device *pdev)
965 {
966         struct uart_port *port = dev_get_drvdata(&pdev->dev);
967         struct uartlite_data *pdata = port->private_data;
968         int rc;
969
970         clk_unprepare(pdata->clk);
971         rc = ulite_release(&pdev->dev);
972         mutex_lock(&bitmap_lock);
973         clear_bit(pdata->id, bitmap);
974         mutex_unlock(&bitmap_lock);
975
976 #ifdef CONFIG_SERIAL_UARTLITE_CONSOLE
977         if (console_port == port)
978                 console_port = NULL;
979 #endif
980
981         uart_unregister_driver(pdata->ulite_uart_driver);
982         pm_runtime_disable(&pdev->dev);
983         pm_runtime_set_suspended(&pdev->dev);
984         pm_runtime_dont_use_autosuspend(&pdev->dev);
985         return rc;
986 }
987
988 /* work with hotplug and coldplug */
989 MODULE_ALIAS("platform:uartlite");
990
991 static struct platform_driver ulite_platform_driver = {
992         .probe = ulite_probe,
993         .remove = ulite_remove,
994         .driver = {
995                 .name  = "uartlite",
996                 .of_match_table = of_match_ptr(ulite_of_match),
997                 .pm = &ulite_pm_ops,
998         },
999 };
1000
1001 /* ---------------------------------------------------------------------
1002  * Module setup/teardown
1003  */
1004
1005 static int __init ulite_init(void)
1006 {
1007         pr_debug("uartlite: calling platform_driver_register()\n");
1008         return platform_driver_register(&ulite_platform_driver);
1009 }
1010
1011 static void __exit ulite_exit(void)
1012 {
1013         platform_driver_unregister(&ulite_platform_driver);
1014 }
1015
1016 module_init(ulite_init);
1017 module_exit(ulite_exit);
1018
1019 MODULE_AUTHOR("Peter Korsgaard <jacmet@sunsite.dk>");
1020 MODULE_DESCRIPTION("Xilinx uartlite serial driver");
1021 MODULE_LICENSE("GPL");