[POWERPC] Uartlite: Add of-platform-bus binding
[linux-2.6.git] / drivers / 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/delay.h>
19 #include <linux/interrupt.h>
20 #include <asm/io.h>
21 #if defined(CONFIG_OF)
22 #include <linux/of_device.h>
23 #include <linux/of_platform.h>
24 #endif
25
26 #define ULITE_NAME              "ttyUL"
27 #define ULITE_MAJOR             204
28 #define ULITE_MINOR             187
29 #define ULITE_NR_UARTS          4
30
31 /* ---------------------------------------------------------------------
32  * Register definitions
33  *
34  * For register details see datasheet:
35  * http://www.xilinx.com/bvdocs/ipcenter/data_sheet/opb_uartlite.pdf
36  */
37
38 #define ULITE_RX                0x00
39 #define ULITE_TX                0x04
40 #define ULITE_STATUS            0x08
41 #define ULITE_CONTROL           0x0c
42
43 #define ULITE_REGION            16
44
45 #define ULITE_STATUS_RXVALID    0x01
46 #define ULITE_STATUS_RXFULL     0x02
47 #define ULITE_STATUS_TXEMPTY    0x04
48 #define ULITE_STATUS_TXFULL     0x08
49 #define ULITE_STATUS_IE         0x10
50 #define ULITE_STATUS_OVERRUN    0x20
51 #define ULITE_STATUS_FRAME      0x40
52 #define ULITE_STATUS_PARITY     0x80
53
54 #define ULITE_CONTROL_RST_TX    0x01
55 #define ULITE_CONTROL_RST_RX    0x02
56 #define ULITE_CONTROL_IE        0x10
57
58
59 static struct uart_port ulite_ports[ULITE_NR_UARTS];
60
61 /* ---------------------------------------------------------------------
62  * Core UART driver operations
63  */
64
65 static int ulite_receive(struct uart_port *port, int stat)
66 {
67         struct tty_struct *tty = port->info->tty;
68         unsigned char ch = 0;
69         char flag = TTY_NORMAL;
70
71         if ((stat & (ULITE_STATUS_RXVALID | ULITE_STATUS_OVERRUN
72                      | ULITE_STATUS_FRAME)) == 0)
73                 return 0;
74
75         /* stats */
76         if (stat & ULITE_STATUS_RXVALID) {
77                 port->icount.rx++;
78                 ch = in_be32((void*)port->membase + ULITE_RX);
79
80                 if (stat & ULITE_STATUS_PARITY)
81                         port->icount.parity++;
82         }
83
84         if (stat & ULITE_STATUS_OVERRUN)
85                 port->icount.overrun++;
86
87         if (stat & ULITE_STATUS_FRAME)
88                 port->icount.frame++;
89
90
91         /* drop byte with parity error if IGNPAR specificed */
92         if (stat & port->ignore_status_mask & ULITE_STATUS_PARITY)
93                 stat &= ~ULITE_STATUS_RXVALID;
94
95         stat &= port->read_status_mask;
96
97         if (stat & ULITE_STATUS_PARITY)
98                 flag = TTY_PARITY;
99
100
101         stat &= ~port->ignore_status_mask;
102
103         if (stat & ULITE_STATUS_RXVALID)
104                 tty_insert_flip_char(tty, ch, flag);
105
106         if (stat & ULITE_STATUS_FRAME)
107                 tty_insert_flip_char(tty, 0, TTY_FRAME);
108
109         if (stat & ULITE_STATUS_OVERRUN)
110                 tty_insert_flip_char(tty, 0, TTY_OVERRUN);
111
112         return 1;
113 }
114
115 static int ulite_transmit(struct uart_port *port, int stat)
116 {
117         struct circ_buf *xmit  = &port->info->xmit;
118
119         if (stat & ULITE_STATUS_TXFULL)
120                 return 0;
121
122         if (port->x_char) {
123                 out_be32((void*)port->membase + ULITE_TX, port->x_char);
124                 port->x_char = 0;
125                 port->icount.tx++;
126                 return 1;
127         }
128
129         if (uart_circ_empty(xmit) || uart_tx_stopped(port))
130                 return 0;
131
132         out_be32((void*)port->membase + ULITE_TX, xmit->buf[xmit->tail]);
133         xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE-1);
134         port->icount.tx++;
135
136         /* wake up */
137         if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
138                 uart_write_wakeup(port);
139
140         return 1;
141 }
142
143 static irqreturn_t ulite_isr(int irq, void *dev_id)
144 {
145         struct uart_port *port = (struct uart_port *)dev_id;
146         int busy;
147
148         do {
149                 int stat = in_be32((void*)port->membase + ULITE_STATUS);
150                 busy  = ulite_receive(port, stat);
151                 busy |= ulite_transmit(port, stat);
152         } while (busy);
153
154         tty_flip_buffer_push(port->info->tty);
155
156         return IRQ_HANDLED;
157 }
158
159 static unsigned int ulite_tx_empty(struct uart_port *port)
160 {
161         unsigned long flags;
162         unsigned int ret;
163
164         spin_lock_irqsave(&port->lock, flags);
165         ret = in_be32((void*)port->membase + ULITE_STATUS);
166         spin_unlock_irqrestore(&port->lock, flags);
167
168         return ret & ULITE_STATUS_TXEMPTY ? TIOCSER_TEMT : 0;
169 }
170
171 static unsigned int ulite_get_mctrl(struct uart_port *port)
172 {
173         return TIOCM_CTS | TIOCM_DSR | TIOCM_CAR;
174 }
175
176 static void ulite_set_mctrl(struct uart_port *port, unsigned int mctrl)
177 {
178         /* N/A */
179 }
180
181 static void ulite_stop_tx(struct uart_port *port)
182 {
183         /* N/A */
184 }
185
186 static void ulite_start_tx(struct uart_port *port)
187 {
188         ulite_transmit(port, in_be32((void*)port->membase + ULITE_STATUS));
189 }
190
191 static void ulite_stop_rx(struct uart_port *port)
192 {
193         /* don't forward any more data (like !CREAD) */
194         port->ignore_status_mask = ULITE_STATUS_RXVALID | ULITE_STATUS_PARITY
195                 | ULITE_STATUS_FRAME | ULITE_STATUS_OVERRUN;
196 }
197
198 static void ulite_enable_ms(struct uart_port *port)
199 {
200         /* N/A */
201 }
202
203 static void ulite_break_ctl(struct uart_port *port, int ctl)
204 {
205         /* N/A */
206 }
207
208 static int ulite_startup(struct uart_port *port)
209 {
210         int ret;
211
212         ret = request_irq(port->irq, ulite_isr,
213                           IRQF_DISABLED | IRQF_SAMPLE_RANDOM, "uartlite", port);
214         if (ret)
215                 return ret;
216
217         out_be32((void*)port->membase + ULITE_CONTROL,
218                  ULITE_CONTROL_RST_RX | ULITE_CONTROL_RST_TX);
219         out_be32((void*)port->membase + ULITE_CONTROL, ULITE_CONTROL_IE);
220
221         return 0;
222 }
223
224 static void ulite_shutdown(struct uart_port *port)
225 {
226         out_be32((void*)port->membase + ULITE_CONTROL, 0);
227         in_be32((void*)port->membase + ULITE_CONTROL); /* dummy */
228         free_irq(port->irq, port);
229 }
230
231 static void ulite_set_termios(struct uart_port *port, struct ktermios *termios,
232                               struct ktermios *old)
233 {
234         unsigned long flags;
235         unsigned int baud;
236
237         spin_lock_irqsave(&port->lock, flags);
238
239         port->read_status_mask = ULITE_STATUS_RXVALID | ULITE_STATUS_OVERRUN
240                 | ULITE_STATUS_TXFULL;
241
242         if (termios->c_iflag & INPCK)
243                 port->read_status_mask |=
244                         ULITE_STATUS_PARITY | ULITE_STATUS_FRAME;
245
246         port->ignore_status_mask = 0;
247         if (termios->c_iflag & IGNPAR)
248                 port->ignore_status_mask |= ULITE_STATUS_PARITY
249                         | ULITE_STATUS_FRAME | ULITE_STATUS_OVERRUN;
250
251         /* ignore all characters if CREAD is not set */
252         if ((termios->c_cflag & CREAD) == 0)
253                 port->ignore_status_mask |=
254                         ULITE_STATUS_RXVALID | ULITE_STATUS_PARITY
255                         | ULITE_STATUS_FRAME | ULITE_STATUS_OVERRUN;
256
257         /* update timeout */
258         baud = uart_get_baud_rate(port, termios, old, 0, 460800);
259         uart_update_timeout(port, termios->c_cflag, baud);
260
261         spin_unlock_irqrestore(&port->lock, flags);
262 }
263
264 static const char *ulite_type(struct uart_port *port)
265 {
266         return port->type == PORT_UARTLITE ? "uartlite" : NULL;
267 }
268
269 static void ulite_release_port(struct uart_port *port)
270 {
271         release_mem_region(port->mapbase, ULITE_REGION);
272         iounmap(port->membase);
273         port->membase = NULL;
274 }
275
276 static int ulite_request_port(struct uart_port *port)
277 {
278         if (!request_mem_region(port->mapbase, ULITE_REGION, "uartlite")) {
279                 dev_err(port->dev, "Memory region busy\n");
280                 return -EBUSY;
281         }
282
283         port->membase = ioremap(port->mapbase, ULITE_REGION);
284         if (!port->membase) {
285                 dev_err(port->dev, "Unable to map registers\n");
286                 release_mem_region(port->mapbase, ULITE_REGION);
287                 return -EBUSY;
288         }
289
290         return 0;
291 }
292
293 static void ulite_config_port(struct uart_port *port, int flags)
294 {
295         if (!ulite_request_port(port))
296                 port->type = PORT_UARTLITE;
297 }
298
299 static int ulite_verify_port(struct uart_port *port, struct serial_struct *ser)
300 {
301         /* we don't want the core code to modify any port params */
302         return -EINVAL;
303 }
304
305 static struct uart_ops ulite_ops = {
306         .tx_empty       = ulite_tx_empty,
307         .set_mctrl      = ulite_set_mctrl,
308         .get_mctrl      = ulite_get_mctrl,
309         .stop_tx        = ulite_stop_tx,
310         .start_tx       = ulite_start_tx,
311         .stop_rx        = ulite_stop_rx,
312         .enable_ms      = ulite_enable_ms,
313         .break_ctl      = ulite_break_ctl,
314         .startup        = ulite_startup,
315         .shutdown       = ulite_shutdown,
316         .set_termios    = ulite_set_termios,
317         .type           = ulite_type,
318         .release_port   = ulite_release_port,
319         .request_port   = ulite_request_port,
320         .config_port    = ulite_config_port,
321         .verify_port    = ulite_verify_port
322 };
323
324 /* ---------------------------------------------------------------------
325  * Console driver operations
326  */
327
328 #ifdef CONFIG_SERIAL_UARTLITE_CONSOLE
329 static void ulite_console_wait_tx(struct uart_port *port)
330 {
331         int i;
332
333         /* wait up to 10ms for the character(s) to be sent */
334         for (i = 0; i < 10000; i++) {
335                 if (in_be32((void*)port->membase + ULITE_STATUS) & ULITE_STATUS_TXEMPTY)
336                         break;
337                 udelay(1);
338         }
339 }
340
341 static void ulite_console_putchar(struct uart_port *port, int ch)
342 {
343         ulite_console_wait_tx(port);
344         out_be32((void*)port->membase + ULITE_TX, ch);
345 }
346
347 static void ulite_console_write(struct console *co, const char *s,
348                                 unsigned int count)
349 {
350         struct uart_port *port = &ulite_ports[co->index];
351         unsigned long flags;
352         unsigned int ier;
353         int locked = 1;
354
355         if (oops_in_progress) {
356                 locked = spin_trylock_irqsave(&port->lock, flags);
357         } else
358                 spin_lock_irqsave(&port->lock, flags);
359
360         /* save and disable interrupt */
361         ier = in_be32((void*)port->membase + ULITE_STATUS) & ULITE_STATUS_IE;
362         out_be32((void*)port->membase + ULITE_CONTROL, 0);
363
364         uart_console_write(port, s, count, ulite_console_putchar);
365
366         ulite_console_wait_tx(port);
367
368         /* restore interrupt state */
369         if (ier)
370                 out_be32((void*)port->membase + ULITE_CONTROL, ULITE_CONTROL_IE);
371
372         if (locked)
373                 spin_unlock_irqrestore(&port->lock, flags);
374 }
375
376 static int __init ulite_console_setup(struct console *co, char *options)
377 {
378         struct uart_port *port;
379         int baud = 9600;
380         int bits = 8;
381         int parity = 'n';
382         int flow = 'n';
383
384         if (co->index < 0 || co->index >= ULITE_NR_UARTS)
385                 return -EINVAL;
386
387         port = &ulite_ports[co->index];
388
389         /* not initialized yet? */
390         if (!port->membase) {
391                 pr_debug("console on ttyUL%i not initialized\n", co->index);
392                 return -ENODEV;
393         }
394
395         if (options)
396                 uart_parse_options(options, &baud, &parity, &bits, &flow);
397
398         return uart_set_options(port, co, baud, parity, bits, flow);
399 }
400
401 static struct uart_driver ulite_uart_driver;
402
403 static struct console ulite_console = {
404         .name   = ULITE_NAME,
405         .write  = ulite_console_write,
406         .device = uart_console_device,
407         .setup  = ulite_console_setup,
408         .flags  = CON_PRINTBUFFER,
409         .index  = -1, /* Specified on the cmdline (e.g. console=ttyUL0 ) */
410         .data   = &ulite_uart_driver,
411 };
412
413 static int __init ulite_console_init(void)
414 {
415         register_console(&ulite_console);
416         return 0;
417 }
418
419 console_initcall(ulite_console_init);
420
421 #endif /* CONFIG_SERIAL_UARTLITE_CONSOLE */
422
423 static struct uart_driver ulite_uart_driver = {
424         .owner          = THIS_MODULE,
425         .driver_name    = "uartlite",
426         .dev_name       = ULITE_NAME,
427         .major          = ULITE_MAJOR,
428         .minor          = ULITE_MINOR,
429         .nr             = ULITE_NR_UARTS,
430 #ifdef CONFIG_SERIAL_UARTLITE_CONSOLE
431         .cons           = &ulite_console,
432 #endif
433 };
434
435 /* ---------------------------------------------------------------------
436  * Port assignment functions (mapping devices to uart_port structures)
437  */
438
439 /** ulite_assign: register a uartlite device with the driver
440  *
441  * @dev: pointer to device structure
442  * @id: requested id number.  Pass -1 for automatic port assignment
443  * @base: base address of uartlite registers
444  * @irq: irq number for uartlite
445  *
446  * Returns: 0 on success, <0 otherwise
447  */
448 static int __devinit ulite_assign(struct device *dev, int id, u32 base, int irq)
449 {
450         struct uart_port *port;
451         int rc;
452
453         /* if id = -1; then scan for a free id and use that */
454         if (id < 0) {
455                 for (id = 0; id < ULITE_NR_UARTS; id++)
456                         if (ulite_ports[id].mapbase == 0)
457                                 break;
458         }
459         if (id < 0 || id >= ULITE_NR_UARTS) {
460                 dev_err(dev, "%s%i too large\n", ULITE_NAME, id);
461                 return -EINVAL;
462         }
463
464         if (ulite_ports[id].mapbase) {
465                 dev_err(dev, "cannot assign to %s%i; it is already in use\n",
466                         ULITE_NAME, id);
467                 return -EBUSY;
468         }
469
470         port = &ulite_ports[id];
471
472         spin_lock_init(&port->lock);
473         port->fifosize = 16;
474         port->regshift = 2;
475         port->iotype = UPIO_MEM;
476         port->iobase = 1; /* mark port in use */
477         port->mapbase = base;
478         port->membase = NULL;
479         port->ops = &ulite_ops;
480         port->irq = irq;
481         port->flags = UPF_BOOT_AUTOCONF;
482         port->dev = dev;
483         port->type = PORT_UNKNOWN;
484         port->line = id;
485
486         dev_set_drvdata(dev, port);
487
488         /* Register the port */
489         rc = uart_add_one_port(&ulite_uart_driver, port);
490         if (rc) {
491                 dev_err(dev, "uart_add_one_port() failed; err=%i\n", rc);
492                 port->mapbase = 0;
493                 dev_set_drvdata(dev, NULL);
494                 return rc;
495         }
496
497         return 0;
498 }
499
500 /** ulite_release: register a uartlite device with the driver
501  *
502  * @dev: pointer to device structure
503  */
504 static int __devinit ulite_release(struct device *dev)
505 {
506         struct uart_port *port = dev_get_drvdata(dev);
507         int rc = 0;
508
509         if (port) {
510                 rc = uart_remove_one_port(&ulite_uart_driver, port);
511                 dev_set_drvdata(dev, NULL);
512                 port->mapbase = 0;
513         }
514
515         return rc;
516 }
517
518 /* ---------------------------------------------------------------------
519  * Platform bus binding
520  */
521
522 static int __devinit ulite_probe(struct platform_device *pdev)
523 {
524         struct resource *res, *res2;
525
526         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
527         if (!res)
528                 return -ENODEV;
529
530         res2 = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
531         if (!res2)
532                 return -ENODEV;
533
534         return ulite_assign(&pdev->dev, pdev->id, res->start, res2->start);
535 }
536
537 static int ulite_remove(struct platform_device *pdev)
538 {
539         return ulite_release(&pdev->dev);
540 }
541
542 static struct platform_driver ulite_platform_driver = {
543         .probe  = ulite_probe,
544         .remove = ulite_remove,
545         .driver = {
546                    .owner = THIS_MODULE,
547                    .name  = "uartlite",
548                    },
549 };
550
551 /* ---------------------------------------------------------------------
552  * OF bus bindings
553  */
554 #if defined(CONFIG_OF)
555 static int __devinit
556 ulite_of_probe(struct of_device *op, const struct of_device_id *match)
557 {
558         struct resource res;
559         const unsigned int *id;
560         int irq, rc;
561
562         dev_dbg(&op->dev, "%s(%p, %p)\n", __FUNCTION__, op, match);
563
564         rc = of_address_to_resource(op->node, 0, &res);
565         if (rc) {
566                 dev_err(&op->dev, "invalide address\n");
567                 return rc;
568         }
569
570         irq = irq_of_parse_and_map(op->node, 0);
571
572         id = of_get_property(op->node, "port-number", NULL);
573
574         return ulite_assign(&op->dev, id ? *id : -1, res.start, irq);
575 }
576
577 static int __devexit ulite_of_remove(struct of_device *op)
578 {
579         return ulite_release(&op->dev);
580 }
581
582 /* Match table for of_platform binding */
583 static struct of_device_id __devinit ulite_of_match[] = {
584         { .type = "serial", .compatible = "xilinx,uartlite", },
585         {},
586 };
587 MODULE_DEVICE_TABLE(of, ulite_of_match);
588
589 static struct of_platform_driver ulite_of_driver = {
590         .owner = THIS_MODULE,
591         .name = "uartlite",
592         .match_table = ulite_of_match,
593         .probe = ulite_of_probe,
594         .remove = __devexit_p(ulite_of_remove),
595         .driver = {
596                 .name = "uartlite",
597         },
598 };
599
600 /* Registration helpers to keep the number of #ifdefs to a minimum */
601 static inline int __init ulite_of_register(void)
602 {
603         pr_debug("uartlite: calling of_register_platform_driver()\n");
604         return of_register_platform_driver(&ulite_of_driver);
605 }
606
607 static inline void __exit ulite_of_unregister(void)
608 {
609         of_unregister_platform_driver(&ulite_of_driver);
610 }
611 #else /* CONFIG_OF */
612 /* CONFIG_OF not enabled; do nothing helpers */
613 static inline int __init ulite_of_register(void) { return 0; }
614 static inline void __exit ulite_of_unregister(void) { }
615 #endif /* CONFIG_OF */
616
617 /* ---------------------------------------------------------------------
618  * Module setup/teardown
619  */
620
621 int __init ulite_init(void)
622 {
623         int ret;
624
625         pr_debug("uartlite: calling uart_register_driver()\n");
626         ret = uart_register_driver(&ulite_uart_driver);
627         if (ret)
628                 goto err_uart;
629
630         ret = ulite_of_register();
631         if (ret)
632                 goto err_of;
633
634         pr_debug("uartlite: calling platform_driver_register()\n");
635         ret = platform_driver_register(&ulite_platform_driver);
636         if (ret)
637                 goto err_plat;
638
639         return 0;
640
641 err_plat:
642         ulite_of_unregister();
643 err_of:
644         uart_unregister_driver(&ulite_uart_driver);
645 err_uart:
646         printk(KERN_ERR "registering uartlite driver failed: err=%i", ret);
647         return ret;
648 }
649
650 void __exit ulite_exit(void)
651 {
652         platform_driver_unregister(&ulite_platform_driver);
653         ulite_of_unregister();
654         uart_unregister_driver(&ulite_uart_driver);
655 }
656
657 module_init(ulite_init);
658 module_exit(ulite_exit);
659
660 MODULE_AUTHOR("Peter Korsgaard <jacmet@sunsite.dk>");
661 MODULE_DESCRIPTION("Xilinx uartlite serial driver");
662 MODULE_LICENSE("GPL");