blob: d2fce865b73180a70e9e3d3bf7435dc07b79fcc7 [file] [log] [blame]
Peter Korsgaard238b8722006-12-06 20:35:17 -08001/*
2 * uartlite.c: Serial driver for Xilinx uartlite serial controller
3 *
Grant Likely852e1ea2007-10-02 12:16:04 +10004 * Copyright (C) 2006 Peter Korsgaard <jacmet@sunsite.dk>
5 * Copyright (C) 2007 Secret Lab Technologies Ltd.
Peter Korsgaard238b8722006-12-06 20:35:17 -08006 *
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>
Stephen Neuendorffer0e349b02008-01-09 06:35:05 +110020#include <linux/init.h>
Peter Korsgaard238b8722006-12-06 20:35:17 -080021#include <asm/io.h>
David Miller07081fd2010-03-10 14:05:35 -070022#if defined(CONFIG_OF) && (defined(CONFIG_PPC32) || defined(CONFIG_MICROBLAZE))
Stephen Neuendorffer0e349b02008-01-09 06:35:05 +110023#include <linux/of.h>
Grant Likely22ae7822010-07-29 11:49:01 -060024#include <linux/of_address.h>
Grant Likely852e1ea2007-10-02 12:16:04 +100025#include <linux/of_device.h>
26#include <linux/of_platform.h>
Stephen Neuendorffer0e349b02008-01-09 06:35:05 +110027
28/* Match table for of_platform binding */
29static struct of_device_id ulite_of_match[] __devinitdata = {
30 { .compatible = "xlnx,opb-uartlite-1.00.b", },
31 { .compatible = "xlnx,xps-uartlite-1.00.a", },
32 {}
33};
34MODULE_DEVICE_TABLE(of, ulite_of_match);
35
Grant Likely852e1ea2007-10-02 12:16:04 +100036#endif
Peter Korsgaard238b8722006-12-06 20:35:17 -080037
Grant Likely00775822007-10-02 12:15:49 +100038#define ULITE_NAME "ttyUL"
Peter Korsgaard238b8722006-12-06 20:35:17 -080039#define ULITE_MAJOR 204
40#define ULITE_MINOR 187
41#define ULITE_NR_UARTS 4
42
Grant Likely435706b2007-10-02 12:15:59 +100043/* ---------------------------------------------------------------------
44 * Register definitions
45 *
46 * For register details see datasheet:
Justin P. Mattock631dd1a2010-10-18 11:03:14 +020047 * http://www.xilinx.com/support/documentation/ip_documentation/opb_uartlite.pdf
Grant Likely435706b2007-10-02 12:15:59 +100048 */
49
Peter Korsgaard238b8722006-12-06 20:35:17 -080050#define ULITE_RX 0x00
51#define ULITE_TX 0x04
52#define ULITE_STATUS 0x08
53#define ULITE_CONTROL 0x0c
54
55#define ULITE_REGION 16
56
57#define ULITE_STATUS_RXVALID 0x01
58#define ULITE_STATUS_RXFULL 0x02
59#define ULITE_STATUS_TXEMPTY 0x04
60#define ULITE_STATUS_TXFULL 0x08
61#define ULITE_STATUS_IE 0x10
62#define ULITE_STATUS_OVERRUN 0x20
63#define ULITE_STATUS_FRAME 0x40
64#define ULITE_STATUS_PARITY 0x80
65
66#define ULITE_CONTROL_RST_TX 0x01
67#define ULITE_CONTROL_RST_RX 0x02
68#define ULITE_CONTROL_IE 0x10
69
70
Grant Likely483c79d2007-10-02 12:15:44 +100071static struct uart_port ulite_ports[ULITE_NR_UARTS];
Peter Korsgaard238b8722006-12-06 20:35:17 -080072
Grant Likely435706b2007-10-02 12:15:59 +100073/* ---------------------------------------------------------------------
74 * Core UART driver operations
75 */
76
Peter Korsgaard238b8722006-12-06 20:35:17 -080077static int ulite_receive(struct uart_port *port, int stat)
78{
Alan Coxebd2c8f2009-09-19 13:13:28 -070079 struct tty_struct *tty = port->state->port.tty;
Peter Korsgaard238b8722006-12-06 20:35:17 -080080 unsigned char ch = 0;
81 char flag = TTY_NORMAL;
82
83 if ((stat & (ULITE_STATUS_RXVALID | ULITE_STATUS_OVERRUN
84 | ULITE_STATUS_FRAME)) == 0)
85 return 0;
86
87 /* stats */
88 if (stat & ULITE_STATUS_RXVALID) {
89 port->icount.rx++;
John Linne5bbbb12010-04-07 09:32:55 -060090 ch = ioread32be(port->membase + ULITE_RX);
Peter Korsgaard238b8722006-12-06 20:35:17 -080091
92 if (stat & ULITE_STATUS_PARITY)
93 port->icount.parity++;
94 }
95
96 if (stat & ULITE_STATUS_OVERRUN)
97 port->icount.overrun++;
98
99 if (stat & ULITE_STATUS_FRAME)
100 port->icount.frame++;
101
102
103 /* drop byte with parity error if IGNPAR specificed */
104 if (stat & port->ignore_status_mask & ULITE_STATUS_PARITY)
105 stat &= ~ULITE_STATUS_RXVALID;
106
107 stat &= port->read_status_mask;
108
109 if (stat & ULITE_STATUS_PARITY)
110 flag = TTY_PARITY;
111
112
113 stat &= ~port->ignore_status_mask;
114
115 if (stat & ULITE_STATUS_RXVALID)
116 tty_insert_flip_char(tty, ch, flag);
117
118 if (stat & ULITE_STATUS_FRAME)
119 tty_insert_flip_char(tty, 0, TTY_FRAME);
120
121 if (stat & ULITE_STATUS_OVERRUN)
122 tty_insert_flip_char(tty, 0, TTY_OVERRUN);
123
124 return 1;
125}
126
127static int ulite_transmit(struct uart_port *port, int stat)
128{
Alan Coxebd2c8f2009-09-19 13:13:28 -0700129 struct circ_buf *xmit = &port->state->xmit;
Peter Korsgaard238b8722006-12-06 20:35:17 -0800130
131 if (stat & ULITE_STATUS_TXFULL)
132 return 0;
133
134 if (port->x_char) {
John Linne5bbbb12010-04-07 09:32:55 -0600135 iowrite32be(port->x_char, port->membase + ULITE_TX);
Peter Korsgaard238b8722006-12-06 20:35:17 -0800136 port->x_char = 0;
137 port->icount.tx++;
138 return 1;
139 }
140
141 if (uart_circ_empty(xmit) || uart_tx_stopped(port))
142 return 0;
143
John Linne5bbbb12010-04-07 09:32:55 -0600144 iowrite32be(xmit->buf[xmit->tail], port->membase + ULITE_TX);
Peter Korsgaard238b8722006-12-06 20:35:17 -0800145 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE-1);
146 port->icount.tx++;
147
148 /* wake up */
149 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
150 uart_write_wakeup(port);
151
152 return 1;
153}
154
155static irqreturn_t ulite_isr(int irq, void *dev_id)
156{
Jeff Garzik15aafa22008-02-06 01:36:20 -0800157 struct uart_port *port = dev_id;
Peter Korsgaardd2cfe962009-09-09 16:54:04 +0200158 int busy, n = 0;
Peter Korsgaard238b8722006-12-06 20:35:17 -0800159
160 do {
John Linne5bbbb12010-04-07 09:32:55 -0600161 int stat = ioread32be(port->membase + ULITE_STATUS);
Peter Korsgaard238b8722006-12-06 20:35:17 -0800162 busy = ulite_receive(port, stat);
163 busy |= ulite_transmit(port, stat);
Peter Korsgaardd2cfe962009-09-09 16:54:04 +0200164 n++;
Peter Korsgaard238b8722006-12-06 20:35:17 -0800165 } while (busy);
166
Peter Korsgaardd2cfe962009-09-09 16:54:04 +0200167 /* work done? */
168 if (n > 1) {
169 tty_flip_buffer_push(port->state->port.tty);
170 return IRQ_HANDLED;
171 } else {
172 return IRQ_NONE;
173 }
Peter Korsgaard238b8722006-12-06 20:35:17 -0800174}
175
176static unsigned int ulite_tx_empty(struct uart_port *port)
177{
178 unsigned long flags;
179 unsigned int ret;
180
181 spin_lock_irqsave(&port->lock, flags);
John Linne5bbbb12010-04-07 09:32:55 -0600182 ret = ioread32be(port->membase + ULITE_STATUS);
Peter Korsgaard238b8722006-12-06 20:35:17 -0800183 spin_unlock_irqrestore(&port->lock, flags);
184
185 return ret & ULITE_STATUS_TXEMPTY ? TIOCSER_TEMT : 0;
186}
187
188static unsigned int ulite_get_mctrl(struct uart_port *port)
189{
190 return TIOCM_CTS | TIOCM_DSR | TIOCM_CAR;
191}
192
193static void ulite_set_mctrl(struct uart_port *port, unsigned int mctrl)
194{
195 /* N/A */
196}
197
198static void ulite_stop_tx(struct uart_port *port)
199{
200 /* N/A */
201}
202
203static void ulite_start_tx(struct uart_port *port)
204{
John Linne5bbbb12010-04-07 09:32:55 -0600205 ulite_transmit(port, ioread32be(port->membase + ULITE_STATUS));
Peter Korsgaard238b8722006-12-06 20:35:17 -0800206}
207
208static void ulite_stop_rx(struct uart_port *port)
209{
210 /* don't forward any more data (like !CREAD) */
211 port->ignore_status_mask = ULITE_STATUS_RXVALID | ULITE_STATUS_PARITY
212 | ULITE_STATUS_FRAME | ULITE_STATUS_OVERRUN;
213}
214
215static void ulite_enable_ms(struct uart_port *port)
216{
217 /* N/A */
218}
219
220static void ulite_break_ctl(struct uart_port *port, int ctl)
221{
222 /* N/A */
223}
224
225static int ulite_startup(struct uart_port *port)
226{
227 int ret;
228
229 ret = request_irq(port->irq, ulite_isr,
Peter Korsgaardd2cfe962009-09-09 16:54:04 +0200230 IRQF_SHARED | IRQF_SAMPLE_RANDOM, "uartlite", port);
Peter Korsgaard238b8722006-12-06 20:35:17 -0800231 if (ret)
232 return ret;
233
John Linne5bbbb12010-04-07 09:32:55 -0600234 iowrite32be(ULITE_CONTROL_RST_RX | ULITE_CONTROL_RST_TX,
Grant Likelye077b502007-10-03 02:47:02 +1000235 port->membase + ULITE_CONTROL);
John Linne5bbbb12010-04-07 09:32:55 -0600236 iowrite32be(ULITE_CONTROL_IE, port->membase + ULITE_CONTROL);
Peter Korsgaard238b8722006-12-06 20:35:17 -0800237
238 return 0;
239}
240
241static void ulite_shutdown(struct uart_port *port)
242{
John Linne5bbbb12010-04-07 09:32:55 -0600243 iowrite32be(0, port->membase + ULITE_CONTROL);
244 ioread32be(port->membase + ULITE_CONTROL); /* dummy */
Peter Korsgaard238b8722006-12-06 20:35:17 -0800245 free_irq(port->irq, port);
246}
247
Alan Cox606d0992006-12-08 02:38:45 -0800248static void ulite_set_termios(struct uart_port *port, struct ktermios *termios,
249 struct ktermios *old)
Peter Korsgaard238b8722006-12-06 20:35:17 -0800250{
251 unsigned long flags;
252 unsigned int baud;
253
254 spin_lock_irqsave(&port->lock, flags);
255
256 port->read_status_mask = ULITE_STATUS_RXVALID | ULITE_STATUS_OVERRUN
257 | ULITE_STATUS_TXFULL;
258
259 if (termios->c_iflag & INPCK)
260 port->read_status_mask |=
261 ULITE_STATUS_PARITY | ULITE_STATUS_FRAME;
262
263 port->ignore_status_mask = 0;
264 if (termios->c_iflag & IGNPAR)
265 port->ignore_status_mask |= ULITE_STATUS_PARITY
266 | ULITE_STATUS_FRAME | ULITE_STATUS_OVERRUN;
267
268 /* ignore all characters if CREAD is not set */
269 if ((termios->c_cflag & CREAD) == 0)
270 port->ignore_status_mask |=
271 ULITE_STATUS_RXVALID | ULITE_STATUS_PARITY
272 | ULITE_STATUS_FRAME | ULITE_STATUS_OVERRUN;
273
274 /* update timeout */
275 baud = uart_get_baud_rate(port, termios, old, 0, 460800);
276 uart_update_timeout(port, termios->c_cflag, baud);
277
278 spin_unlock_irqrestore(&port->lock, flags);
279}
280
281static const char *ulite_type(struct uart_port *port)
282{
283 return port->type == PORT_UARTLITE ? "uartlite" : NULL;
284}
285
286static void ulite_release_port(struct uart_port *port)
287{
288 release_mem_region(port->mapbase, ULITE_REGION);
289 iounmap(port->membase);
Al Virob81831c2007-02-09 16:38:25 +0000290 port->membase = NULL;
Peter Korsgaard238b8722006-12-06 20:35:17 -0800291}
292
293static int ulite_request_port(struct uart_port *port)
294{
Grant Likelya1080962008-11-14 09:59:48 -0700295 pr_debug("ulite console: port=%p; port->mapbase=%llx\n",
296 port, (unsigned long long) port->mapbase);
Stephen Neuendorffer0e349b02008-01-09 06:35:05 +1100297
Peter Korsgaard238b8722006-12-06 20:35:17 -0800298 if (!request_mem_region(port->mapbase, ULITE_REGION, "uartlite")) {
299 dev_err(port->dev, "Memory region busy\n");
300 return -EBUSY;
301 }
302
303 port->membase = ioremap(port->mapbase, ULITE_REGION);
304 if (!port->membase) {
305 dev_err(port->dev, "Unable to map registers\n");
306 release_mem_region(port->mapbase, ULITE_REGION);
307 return -EBUSY;
308 }
309
310 return 0;
311}
312
313static void ulite_config_port(struct uart_port *port, int flags)
314{
Peter Korsgaarde21654a2006-12-22 16:38:40 +0100315 if (!ulite_request_port(port))
316 port->type = PORT_UARTLITE;
Peter Korsgaard238b8722006-12-06 20:35:17 -0800317}
318
319static int ulite_verify_port(struct uart_port *port, struct serial_struct *ser)
320{
321 /* we don't want the core code to modify any port params */
322 return -EINVAL;
323}
324
Michal Simek8a28af72010-08-17 10:42:05 +0200325#ifdef CONFIG_CONSOLE_POLL
326static int ulite_get_poll_char(struct uart_port *port)
327{
328 if (!(ioread32be(port->membase + ULITE_STATUS)
329 & ULITE_STATUS_RXVALID))
330 return NO_POLL_CHAR;
331
332 return ioread32be(port->membase + ULITE_RX);
333}
334
335static void ulite_put_poll_char(struct uart_port *port, unsigned char ch)
336{
337 while (ioread32be(port->membase + ULITE_STATUS) & ULITE_STATUS_TXFULL)
338 cpu_relax();
339
340 /* write char to device */
341 iowrite32be(ch, port->membase + ULITE_TX);
342}
343#endif
344
Peter Korsgaard238b8722006-12-06 20:35:17 -0800345static struct uart_ops ulite_ops = {
346 .tx_empty = ulite_tx_empty,
347 .set_mctrl = ulite_set_mctrl,
348 .get_mctrl = ulite_get_mctrl,
349 .stop_tx = ulite_stop_tx,
350 .start_tx = ulite_start_tx,
351 .stop_rx = ulite_stop_rx,
352 .enable_ms = ulite_enable_ms,
353 .break_ctl = ulite_break_ctl,
354 .startup = ulite_startup,
355 .shutdown = ulite_shutdown,
356 .set_termios = ulite_set_termios,
357 .type = ulite_type,
358 .release_port = ulite_release_port,
359 .request_port = ulite_request_port,
360 .config_port = ulite_config_port,
Michal Simek8a28af72010-08-17 10:42:05 +0200361 .verify_port = ulite_verify_port,
362#ifdef CONFIG_CONSOLE_POLL
363 .poll_get_char = ulite_get_poll_char,
364 .poll_put_char = ulite_put_poll_char,
365#endif
Peter Korsgaard238b8722006-12-06 20:35:17 -0800366};
367
Grant Likely435706b2007-10-02 12:15:59 +1000368/* ---------------------------------------------------------------------
369 * Console driver operations
370 */
371
Peter Korsgaard238b8722006-12-06 20:35:17 -0800372#ifdef CONFIG_SERIAL_UARTLITE_CONSOLE
373static void ulite_console_wait_tx(struct uart_port *port)
374{
375 int i;
Grant Likely1d6b6982007-10-23 14:27:46 +1000376 u8 val;
Peter Korsgaard238b8722006-12-06 20:35:17 -0800377
Grant Likely1d6b6982007-10-23 14:27:46 +1000378 /* Spin waiting for TX fifo to have space available */
379 for (i = 0; i < 100000; i++) {
John Linne5bbbb12010-04-07 09:32:55 -0600380 val = ioread32be(port->membase + ULITE_STATUS);
Grant Likely1d6b6982007-10-23 14:27:46 +1000381 if ((val & ULITE_STATUS_TXFULL) == 0)
Peter Korsgaard238b8722006-12-06 20:35:17 -0800382 break;
Grant Likely1d6b6982007-10-23 14:27:46 +1000383 cpu_relax();
Peter Korsgaard238b8722006-12-06 20:35:17 -0800384 }
385}
386
387static void ulite_console_putchar(struct uart_port *port, int ch)
388{
389 ulite_console_wait_tx(port);
John Linne5bbbb12010-04-07 09:32:55 -0600390 iowrite32be(ch, port->membase + ULITE_TX);
Peter Korsgaard238b8722006-12-06 20:35:17 -0800391}
392
393static void ulite_console_write(struct console *co, const char *s,
394 unsigned int count)
395{
Grant Likely483c79d2007-10-02 12:15:44 +1000396 struct uart_port *port = &ulite_ports[co->index];
Peter Korsgaard238b8722006-12-06 20:35:17 -0800397 unsigned long flags;
398 unsigned int ier;
399 int locked = 1;
400
401 if (oops_in_progress) {
402 locked = spin_trylock_irqsave(&port->lock, flags);
403 } else
404 spin_lock_irqsave(&port->lock, flags);
405
406 /* save and disable interrupt */
John Linne5bbbb12010-04-07 09:32:55 -0600407 ier = ioread32be(port->membase + ULITE_STATUS) & ULITE_STATUS_IE;
408 iowrite32be(0, port->membase + ULITE_CONTROL);
Peter Korsgaard238b8722006-12-06 20:35:17 -0800409
410 uart_console_write(port, s, count, ulite_console_putchar);
411
412 ulite_console_wait_tx(port);
413
414 /* restore interrupt state */
415 if (ier)
John Linne5bbbb12010-04-07 09:32:55 -0600416 iowrite32be(ULITE_CONTROL_IE, port->membase + ULITE_CONTROL);
Peter Korsgaard238b8722006-12-06 20:35:17 -0800417
418 if (locked)
419 spin_unlock_irqrestore(&port->lock, flags);
420}
421
Richard Röjfors03eac7b2010-02-02 13:44:12 -0800422static int __devinit ulite_console_setup(struct console *co, char *options)
Peter Korsgaard238b8722006-12-06 20:35:17 -0800423{
424 struct uart_port *port;
425 int baud = 9600;
426 int bits = 8;
427 int parity = 'n';
428 int flow = 'n';
429
430 if (co->index < 0 || co->index >= ULITE_NR_UARTS)
431 return -EINVAL;
432
Grant Likely483c79d2007-10-02 12:15:44 +1000433 port = &ulite_ports[co->index];
Peter Korsgaard238b8722006-12-06 20:35:17 -0800434
Grant Likely3de66a12008-02-06 10:23:41 -0700435 /* Has the device been initialized yet? */
Grant Likelyfb4e6e62007-10-02 12:16:09 +1000436 if (!port->mapbase) {
437 pr_debug("console on ttyUL%i not present\n", co->index);
438 return -ENODEV;
439 }
440
Peter Korsgaard238b8722006-12-06 20:35:17 -0800441 /* not initialized yet? */
Grant Likely852e1ea2007-10-02 12:16:04 +1000442 if (!port->membase) {
Grant Likelyfb4e6e62007-10-02 12:16:09 +1000443 if (ulite_request_port(port))
444 return -ENODEV;
Grant Likely852e1ea2007-10-02 12:16:04 +1000445 }
Peter Korsgaard238b8722006-12-06 20:35:17 -0800446
447 if (options)
448 uart_parse_options(options, &baud, &parity, &bits, &flow);
449
450 return uart_set_options(port, co, baud, parity, bits, flow);
451}
452
453static struct uart_driver ulite_uart_driver;
454
455static struct console ulite_console = {
Grant Likely00775822007-10-02 12:15:49 +1000456 .name = ULITE_NAME,
Peter Korsgaard238b8722006-12-06 20:35:17 -0800457 .write = ulite_console_write,
458 .device = uart_console_device,
459 .setup = ulite_console_setup,
460 .flags = CON_PRINTBUFFER,
461 .index = -1, /* Specified on the cmdline (e.g. console=ttyUL0 ) */
462 .data = &ulite_uart_driver,
463};
464
465static int __init ulite_console_init(void)
466{
467 register_console(&ulite_console);
468 return 0;
469}
470
471console_initcall(ulite_console_init);
472
473#endif /* CONFIG_SERIAL_UARTLITE_CONSOLE */
474
475static struct uart_driver ulite_uart_driver = {
476 .owner = THIS_MODULE,
477 .driver_name = "uartlite",
Grant Likely00775822007-10-02 12:15:49 +1000478 .dev_name = ULITE_NAME,
Peter Korsgaard238b8722006-12-06 20:35:17 -0800479 .major = ULITE_MAJOR,
480 .minor = ULITE_MINOR,
481 .nr = ULITE_NR_UARTS,
482#ifdef CONFIG_SERIAL_UARTLITE_CONSOLE
483 .cons = &ulite_console,
484#endif
485};
486
Grant Likely435706b2007-10-02 12:15:59 +1000487/* ---------------------------------------------------------------------
488 * Port assignment functions (mapping devices to uart_port structures)
489 */
490
491/** ulite_assign: register a uartlite device with the driver
492 *
493 * @dev: pointer to device structure
494 * @id: requested id number. Pass -1 for automatic port assignment
495 * @base: base address of uartlite registers
496 * @irq: irq number for uartlite
497 *
498 * Returns: 0 on success, <0 otherwise
499 */
Grant Likely8fa7b612007-10-02 12:15:54 +1000500static int __devinit ulite_assign(struct device *dev, int id, u32 base, int irq)
501{
502 struct uart_port *port;
503 int rc;
504
505 /* if id = -1; then scan for a free id and use that */
506 if (id < 0) {
507 for (id = 0; id < ULITE_NR_UARTS; id++)
508 if (ulite_ports[id].mapbase == 0)
509 break;
510 }
511 if (id < 0 || id >= ULITE_NR_UARTS) {
512 dev_err(dev, "%s%i too large\n", ULITE_NAME, id);
513 return -EINVAL;
514 }
515
Grant Likelyfb4e6e62007-10-02 12:16:09 +1000516 if ((ulite_ports[id].mapbase) && (ulite_ports[id].mapbase != base)) {
Grant Likely8fa7b612007-10-02 12:15:54 +1000517 dev_err(dev, "cannot assign to %s%i; it is already in use\n",
518 ULITE_NAME, id);
519 return -EBUSY;
520 }
521
522 port = &ulite_ports[id];
523
524 spin_lock_init(&port->lock);
525 port->fifosize = 16;
526 port->regshift = 2;
527 port->iotype = UPIO_MEM;
528 port->iobase = 1; /* mark port in use */
529 port->mapbase = base;
530 port->membase = NULL;
531 port->ops = &ulite_ops;
532 port->irq = irq;
533 port->flags = UPF_BOOT_AUTOCONF;
534 port->dev = dev;
535 port->type = PORT_UNKNOWN;
536 port->line = id;
537
538 dev_set_drvdata(dev, port);
539
540 /* Register the port */
541 rc = uart_add_one_port(&ulite_uart_driver, port);
542 if (rc) {
543 dev_err(dev, "uart_add_one_port() failed; err=%i\n", rc);
544 port->mapbase = 0;
545 dev_set_drvdata(dev, NULL);
546 return rc;
547 }
548
549 return 0;
550}
551
Grant Likely435706b2007-10-02 12:15:59 +1000552/** ulite_release: register a uartlite device with the driver
553 *
554 * @dev: pointer to device structure
555 */
Stephen Neuendorfferf67702a2008-01-09 06:35:09 +1100556static int __devexit ulite_release(struct device *dev)
Grant Likely8fa7b612007-10-02 12:15:54 +1000557{
558 struct uart_port *port = dev_get_drvdata(dev);
559 int rc = 0;
560
561 if (port) {
562 rc = uart_remove_one_port(&ulite_uart_driver, port);
563 dev_set_drvdata(dev, NULL);
564 port->mapbase = 0;
565 }
566
567 return rc;
568}
569
Grant Likely435706b2007-10-02 12:15:59 +1000570/* ---------------------------------------------------------------------
571 * Platform bus binding
572 */
573
Peter Korsgaard238b8722006-12-06 20:35:17 -0800574static int __devinit ulite_probe(struct platform_device *pdev)
575{
576 struct resource *res, *res2;
Peter Korsgaard238b8722006-12-06 20:35:17 -0800577
578 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
579 if (!res)
580 return -ENODEV;
581
582 res2 = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
583 if (!res2)
584 return -ENODEV;
585
Grant Likely8fa7b612007-10-02 12:15:54 +1000586 return ulite_assign(&pdev->dev, pdev->id, res->start, res2->start);
Peter Korsgaard238b8722006-12-06 20:35:17 -0800587}
588
Stephen Neuendorfferf67702a2008-01-09 06:35:09 +1100589static int __devexit ulite_remove(struct platform_device *pdev)
Peter Korsgaard238b8722006-12-06 20:35:17 -0800590{
Grant Likely8fa7b612007-10-02 12:15:54 +1000591 return ulite_release(&pdev->dev);
Peter Korsgaard238b8722006-12-06 20:35:17 -0800592}
593
Kay Sieverse169c132008-04-15 14:34:35 -0700594/* work with hotplug and coldplug */
595MODULE_ALIAS("platform:uartlite");
596
Peter Korsgaard238b8722006-12-06 20:35:17 -0800597static struct platform_driver ulite_platform_driver = {
598 .probe = ulite_probe,
Stephen Neuendorfferf67702a2008-01-09 06:35:09 +1100599 .remove = __devexit_p(ulite_remove),
Peter Korsgaard238b8722006-12-06 20:35:17 -0800600 .driver = {
601 .owner = THIS_MODULE,
602 .name = "uartlite",
603 },
604};
605
Grant Likely435706b2007-10-02 12:15:59 +1000606/* ---------------------------------------------------------------------
Grant Likely852e1ea2007-10-02 12:16:04 +1000607 * OF bus bindings
608 */
David Miller07081fd2010-03-10 14:05:35 -0700609#if defined(CONFIG_OF) && (defined(CONFIG_PPC32) || defined(CONFIG_MICROBLAZE))
Grant Likely852e1ea2007-10-02 12:16:04 +1000610static int __devinit
Grant Likely2dc11582010-08-06 09:25:50 -0600611ulite_of_probe(struct platform_device *op, const struct of_device_id *match)
Grant Likely852e1ea2007-10-02 12:16:04 +1000612{
613 struct resource res;
614 const unsigned int *id;
615 int irq, rc;
616
Harvey Harrison71cc2c22008-04-30 00:55:10 -0700617 dev_dbg(&op->dev, "%s(%p, %p)\n", __func__, op, match);
Grant Likely852e1ea2007-10-02 12:16:04 +1000618
Grant Likely61c7a082010-04-13 16:12:29 -0700619 rc = of_address_to_resource(op->dev.of_node, 0, &res);
Grant Likely852e1ea2007-10-02 12:16:04 +1000620 if (rc) {
Grant Likelye077b502007-10-03 02:47:02 +1000621 dev_err(&op->dev, "invalid address\n");
Grant Likely852e1ea2007-10-02 12:16:04 +1000622 return rc;
623 }
624
Grant Likely61c7a082010-04-13 16:12:29 -0700625 irq = irq_of_parse_and_map(op->dev.of_node, 0);
Grant Likely852e1ea2007-10-02 12:16:04 +1000626
Grant Likely61c7a082010-04-13 16:12:29 -0700627 id = of_get_property(op->dev.of_node, "port-number", NULL);
Grant Likely852e1ea2007-10-02 12:16:04 +1000628
John Linne5bbbb12010-04-07 09:32:55 -0600629 return ulite_assign(&op->dev, id ? *id : -1, res.start, irq);
Grant Likely852e1ea2007-10-02 12:16:04 +1000630}
631
Grant Likely2dc11582010-08-06 09:25:50 -0600632static int __devexit ulite_of_remove(struct platform_device *op)
Grant Likely852e1ea2007-10-02 12:16:04 +1000633{
634 return ulite_release(&op->dev);
635}
636
Grant Likely852e1ea2007-10-02 12:16:04 +1000637static struct of_platform_driver ulite_of_driver = {
Grant Likely852e1ea2007-10-02 12:16:04 +1000638 .probe = ulite_of_probe,
639 .remove = __devexit_p(ulite_of_remove),
640 .driver = {
641 .name = "uartlite",
Grant Likely40182942010-04-13 16:13:02 -0700642 .owner = THIS_MODULE,
643 .of_match_table = ulite_of_match,
Grant Likely852e1ea2007-10-02 12:16:04 +1000644 },
645};
646
647/* Registration helpers to keep the number of #ifdefs to a minimum */
648static inline int __init ulite_of_register(void)
649{
650 pr_debug("uartlite: calling of_register_platform_driver()\n");
651 return of_register_platform_driver(&ulite_of_driver);
652}
653
654static inline void __exit ulite_of_unregister(void)
655{
656 of_unregister_platform_driver(&ulite_of_driver);
657}
David Miller07081fd2010-03-10 14:05:35 -0700658#else /* CONFIG_OF && (CONFIG_PPC32 || CONFIG_MICROBLAZE) */
659/* Appropriate config not enabled; do nothing helpers */
Grant Likely852e1ea2007-10-02 12:16:04 +1000660static inline int __init ulite_of_register(void) { return 0; }
661static inline void __exit ulite_of_unregister(void) { }
David Miller07081fd2010-03-10 14:05:35 -0700662#endif /* CONFIG_OF && (CONFIG_PPC32 || CONFIG_MICROBLAZE) */
Grant Likely852e1ea2007-10-02 12:16:04 +1000663
664/* ---------------------------------------------------------------------
Grant Likely435706b2007-10-02 12:15:59 +1000665 * Module setup/teardown
666 */
667
Peter Korsgaard238b8722006-12-06 20:35:17 -0800668int __init ulite_init(void)
669{
670 int ret;
671
Grant Likely852e1ea2007-10-02 12:16:04 +1000672 pr_debug("uartlite: calling uart_register_driver()\n");
Peter Korsgaard238b8722006-12-06 20:35:17 -0800673 ret = uart_register_driver(&ulite_uart_driver);
674 if (ret)
Grant Likely852e1ea2007-10-02 12:16:04 +1000675 goto err_uart;
Peter Korsgaard238b8722006-12-06 20:35:17 -0800676
Grant Likely852e1ea2007-10-02 12:16:04 +1000677 ret = ulite_of_register();
678 if (ret)
679 goto err_of;
680
681 pr_debug("uartlite: calling platform_driver_register()\n");
Peter Korsgaard238b8722006-12-06 20:35:17 -0800682 ret = platform_driver_register(&ulite_platform_driver);
683 if (ret)
Grant Likely852e1ea2007-10-02 12:16:04 +1000684 goto err_plat;
Peter Korsgaard238b8722006-12-06 20:35:17 -0800685
Grant Likely852e1ea2007-10-02 12:16:04 +1000686 return 0;
687
688err_plat:
689 ulite_of_unregister();
690err_of:
691 uart_unregister_driver(&ulite_uart_driver);
692err_uart:
693 printk(KERN_ERR "registering uartlite driver failed: err=%i", ret);
Peter Korsgaard238b8722006-12-06 20:35:17 -0800694 return ret;
695}
696
697void __exit ulite_exit(void)
698{
699 platform_driver_unregister(&ulite_platform_driver);
Grant Likely852e1ea2007-10-02 12:16:04 +1000700 ulite_of_unregister();
Peter Korsgaard238b8722006-12-06 20:35:17 -0800701 uart_unregister_driver(&ulite_uart_driver);
702}
703
704module_init(ulite_init);
705module_exit(ulite_exit);
706
707MODULE_AUTHOR("Peter Korsgaard <jacmet@sunsite.dk>");
708MODULE_DESCRIPTION("Xilinx uartlite serial driver");
709MODULE_LICENSE("GPL");