blob: 6ca5dd615f9e38b2f380d23fe6006d4deb3d3bb4 [file] [log] [blame]
Gabor Juhosd57f3412011-06-20 19:26:11 +02001/*
2 * Atheros AR933X SoC built-in UART driver
3 *
4 * Copyright (C) 2011 Gabor Juhos <juhosg@openwrt.org>
5 *
6 * Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o.
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License version 2 as published
10 * by the Free Software Foundation.
11 */
12
13#include <linux/module.h>
14#include <linux/ioport.h>
15#include <linux/init.h>
16#include <linux/console.h>
17#include <linux/sysrq.h>
18#include <linux/delay.h>
19#include <linux/platform_device.h>
20#include <linux/tty.h>
21#include <linux/tty_flip.h>
22#include <linux/serial_core.h>
23#include <linux/serial.h>
24#include <linux/slab.h>
25#include <linux/io.h>
26#include <linux/irq.h>
27
Gabor Juhos2dff8ad2012-11-14 10:38:13 +010028#include <asm/div64.h>
29
Gabor Juhosd57f3412011-06-20 19:26:11 +020030#include <asm/mach-ath79/ar933x_uart.h>
31#include <asm/mach-ath79/ar933x_uart_platform.h>
32
33#define DRIVER_NAME "ar933x-uart"
34
Gabor Juhos2dff8ad2012-11-14 10:38:13 +010035#define AR933X_UART_MAX_SCALE 0xff
36#define AR933X_UART_MAX_STEP 0xffff
37
38#define AR933X_UART_MIN_BAUD 300
39#define AR933X_UART_MAX_BAUD 3000000
40
Gabor Juhosd57f3412011-06-20 19:26:11 +020041#define AR933X_DUMMY_STATUS_RD 0x01
42
43static struct uart_driver ar933x_uart_driver;
44
45struct ar933x_uart_port {
46 struct uart_port port;
47 unsigned int ier; /* shadow Interrupt Enable Register */
Gabor Juhos2dff8ad2012-11-14 10:38:13 +010048 unsigned int min_baud;
49 unsigned int max_baud;
Gabor Juhosd57f3412011-06-20 19:26:11 +020050};
51
52static inline unsigned int ar933x_uart_read(struct ar933x_uart_port *up,
53 int offset)
54{
55 return readl(up->port.membase + offset);
56}
57
58static inline void ar933x_uart_write(struct ar933x_uart_port *up,
59 int offset, unsigned int value)
60{
61 writel(value, up->port.membase + offset);
62}
63
64static inline void ar933x_uart_rmw(struct ar933x_uart_port *up,
65 unsigned int offset,
66 unsigned int mask,
67 unsigned int val)
68{
69 unsigned int t;
70
71 t = ar933x_uart_read(up, offset);
72 t &= ~mask;
73 t |= val;
74 ar933x_uart_write(up, offset, t);
75}
76
77static inline void ar933x_uart_rmw_set(struct ar933x_uart_port *up,
78 unsigned int offset,
79 unsigned int val)
80{
81 ar933x_uart_rmw(up, offset, 0, val);
82}
83
84static inline void ar933x_uart_rmw_clear(struct ar933x_uart_port *up,
85 unsigned int offset,
86 unsigned int val)
87{
88 ar933x_uart_rmw(up, offset, val, 0);
89}
90
91static inline void ar933x_uart_start_tx_interrupt(struct ar933x_uart_port *up)
92{
93 up->ier |= AR933X_UART_INT_TX_EMPTY;
94 ar933x_uart_write(up, AR933X_UART_INT_EN_REG, up->ier);
95}
96
97static inline void ar933x_uart_stop_tx_interrupt(struct ar933x_uart_port *up)
98{
99 up->ier &= ~AR933X_UART_INT_TX_EMPTY;
100 ar933x_uart_write(up, AR933X_UART_INT_EN_REG, up->ier);
101}
102
103static inline void ar933x_uart_putc(struct ar933x_uart_port *up, int ch)
104{
105 unsigned int rdata;
106
107 rdata = ch & AR933X_UART_DATA_TX_RX_MASK;
108 rdata |= AR933X_UART_DATA_TX_CSR;
109 ar933x_uart_write(up, AR933X_UART_DATA_REG, rdata);
110}
111
112static unsigned int ar933x_uart_tx_empty(struct uart_port *port)
113{
114 struct ar933x_uart_port *up = (struct ar933x_uart_port *) port;
115 unsigned long flags;
116 unsigned int rdata;
117
118 spin_lock_irqsave(&up->port.lock, flags);
119 rdata = ar933x_uart_read(up, AR933X_UART_DATA_REG);
120 spin_unlock_irqrestore(&up->port.lock, flags);
121
122 return (rdata & AR933X_UART_DATA_TX_CSR) ? 0 : TIOCSER_TEMT;
123}
124
125static unsigned int ar933x_uart_get_mctrl(struct uart_port *port)
126{
127 return TIOCM_CAR;
128}
129
130static void ar933x_uart_set_mctrl(struct uart_port *port, unsigned int mctrl)
131{
132}
133
134static void ar933x_uart_start_tx(struct uart_port *port)
135{
136 struct ar933x_uart_port *up = (struct ar933x_uart_port *) port;
137
138 ar933x_uart_start_tx_interrupt(up);
139}
140
141static void ar933x_uart_stop_tx(struct uart_port *port)
142{
143 struct ar933x_uart_port *up = (struct ar933x_uart_port *) port;
144
145 ar933x_uart_stop_tx_interrupt(up);
146}
147
148static void ar933x_uart_stop_rx(struct uart_port *port)
149{
150 struct ar933x_uart_port *up = (struct ar933x_uart_port *) port;
151
152 up->ier &= ~AR933X_UART_INT_RX_VALID;
153 ar933x_uart_write(up, AR933X_UART_INT_EN_REG, up->ier);
154}
155
156static void ar933x_uart_break_ctl(struct uart_port *port, int break_state)
157{
158 struct ar933x_uart_port *up = (struct ar933x_uart_port *) port;
159 unsigned long flags;
160
161 spin_lock_irqsave(&up->port.lock, flags);
162 if (break_state == -1)
163 ar933x_uart_rmw_set(up, AR933X_UART_CS_REG,
164 AR933X_UART_CS_TX_BREAK);
165 else
166 ar933x_uart_rmw_clear(up, AR933X_UART_CS_REG,
167 AR933X_UART_CS_TX_BREAK);
168 spin_unlock_irqrestore(&up->port.lock, flags);
169}
170
171static void ar933x_uart_enable_ms(struct uart_port *port)
172{
173}
174
Gabor Juhos2dff8ad2012-11-14 10:38:13 +0100175/*
176 * baudrate = (clk / (scale + 1)) * (step * (1 / 2^17))
177 */
178static unsigned long ar933x_uart_get_baud(unsigned int clk,
179 unsigned int scale,
180 unsigned int step)
181{
182 u64 t;
183 u32 div;
184
185 div = (2 << 16) * (scale + 1);
186 t = clk;
187 t *= step;
188 t += (div / 2);
189 do_div(t, div);
190
191 return t;
192}
193
194static void ar933x_uart_get_scale_step(unsigned int clk,
195 unsigned int baud,
196 unsigned int *scale,
197 unsigned int *step)
198{
199 unsigned int tscale;
200 long min_diff;
201
202 *scale = 0;
203 *step = 0;
204
205 min_diff = baud;
206 for (tscale = 0; tscale < AR933X_UART_MAX_SCALE; tscale++) {
207 u64 tstep;
208 int diff;
209
210 tstep = baud * (tscale + 1);
211 tstep *= (2 << 16);
212 do_div(tstep, clk);
213
214 if (tstep > AR933X_UART_MAX_STEP)
215 break;
216
217 diff = abs(ar933x_uart_get_baud(clk, tscale, tstep) - baud);
218 if (diff < min_diff) {
219 min_diff = diff;
220 *scale = tscale;
221 *step = tstep;
222 }
223 }
224}
225
Gabor Juhosd57f3412011-06-20 19:26:11 +0200226static void ar933x_uart_set_termios(struct uart_port *port,
227 struct ktermios *new,
228 struct ktermios *old)
229{
230 struct ar933x_uart_port *up = (struct ar933x_uart_port *) port;
231 unsigned int cs;
232 unsigned long flags;
Gabor Juhos2dff8ad2012-11-14 10:38:13 +0100233 unsigned int baud, scale, step;
Gabor Juhosd57f3412011-06-20 19:26:11 +0200234
235 /* Only CS8 is supported */
236 new->c_cflag &= ~CSIZE;
237 new->c_cflag |= CS8;
238
239 /* Only one stop bit is supported */
240 new->c_cflag &= ~CSTOPB;
241
242 cs = 0;
243 if (new->c_cflag & PARENB) {
244 if (!(new->c_cflag & PARODD))
245 cs |= AR933X_UART_CS_PARITY_EVEN;
246 else
247 cs |= AR933X_UART_CS_PARITY_ODD;
248 } else {
249 cs |= AR933X_UART_CS_PARITY_NONE;
250 }
251
252 /* Mark/space parity is not supported */
253 new->c_cflag &= ~CMSPAR;
254
Gabor Juhos2dff8ad2012-11-14 10:38:13 +0100255 baud = uart_get_baud_rate(port, new, old, up->min_baud, up->max_baud);
256 ar933x_uart_get_scale_step(port->uartclk, baud, &scale, &step);
Gabor Juhosd57f3412011-06-20 19:26:11 +0200257
258 /*
259 * Ok, we're now changing the port state. Do it with
260 * interrupts disabled.
261 */
262 spin_lock_irqsave(&up->port.lock, flags);
263
Gabor Juhos2dff8ad2012-11-14 10:38:13 +0100264 /* disable the UART */
265 ar933x_uart_rmw_clear(up, AR933X_UART_CS_REG,
266 AR933X_UART_CS_IF_MODE_M << AR933X_UART_CS_IF_MODE_S);
267
Gabor Juhosd57f3412011-06-20 19:26:11 +0200268 /* Update the per-port timeout. */
269 uart_update_timeout(port, new->c_cflag, baud);
270
271 up->port.ignore_status_mask = 0;
272
273 /* ignore all characters if CREAD is not set */
274 if ((new->c_cflag & CREAD) == 0)
275 up->port.ignore_status_mask |= AR933X_DUMMY_STATUS_RD;
276
277 ar933x_uart_write(up, AR933X_UART_CLOCK_REG,
Gabor Juhos2dff8ad2012-11-14 10:38:13 +0100278 scale << AR933X_UART_CLOCK_SCALE_S | step);
Gabor Juhosd57f3412011-06-20 19:26:11 +0200279
280 /* setup configuration register */
281 ar933x_uart_rmw(up, AR933X_UART_CS_REG, AR933X_UART_CS_PARITY_M, cs);
282
283 /* enable host interrupt */
284 ar933x_uart_rmw_set(up, AR933X_UART_CS_REG,
285 AR933X_UART_CS_HOST_INT_EN);
286
Gabor Juhos2dff8ad2012-11-14 10:38:13 +0100287 /* reenable the UART */
288 ar933x_uart_rmw(up, AR933X_UART_CS_REG,
289 AR933X_UART_CS_IF_MODE_M << AR933X_UART_CS_IF_MODE_S,
290 AR933X_UART_CS_IF_MODE_DCE << AR933X_UART_CS_IF_MODE_S);
291
Gabor Juhosd57f3412011-06-20 19:26:11 +0200292 spin_unlock_irqrestore(&up->port.lock, flags);
293
294 if (tty_termios_baud_rate(new))
295 tty_termios_encode_baud_rate(new, baud, baud);
296}
297
298static void ar933x_uart_rx_chars(struct ar933x_uart_port *up)
299{
Jiri Slaby92a19f92013-01-03 15:53:03 +0100300 struct tty_port *port = &up->port.state->port;
Gabor Juhosd57f3412011-06-20 19:26:11 +0200301 struct tty_struct *tty;
302 int max_count = 256;
303
Jiri Slaby92a19f92013-01-03 15:53:03 +0100304 tty = tty_port_tty_get(port);
Gabor Juhosd57f3412011-06-20 19:26:11 +0200305 do {
306 unsigned int rdata;
307 unsigned char ch;
308
309 rdata = ar933x_uart_read(up, AR933X_UART_DATA_REG);
310 if ((rdata & AR933X_UART_DATA_RX_CSR) == 0)
311 break;
312
313 /* remove the character from the FIFO */
314 ar933x_uart_write(up, AR933X_UART_DATA_REG,
315 AR933X_UART_DATA_RX_CSR);
316
Gabor Juhosd57f3412011-06-20 19:26:11 +0200317 up->port.icount.rx++;
318 ch = rdata & AR933X_UART_DATA_TX_RX_MASK;
319
320 if (uart_handle_sysrq_char(&up->port, ch))
321 continue;
322
323 if ((up->port.ignore_status_mask & AR933X_DUMMY_STATUS_RD) == 0)
Jiri Slaby92a19f92013-01-03 15:53:03 +0100324 tty_insert_flip_char(port, ch, TTY_NORMAL);
Gabor Juhosd57f3412011-06-20 19:26:11 +0200325 } while (max_count-- > 0);
326
327 if (tty) {
328 tty_flip_buffer_push(tty);
329 tty_kref_put(tty);
330 }
331}
332
333static void ar933x_uart_tx_chars(struct ar933x_uart_port *up)
334{
335 struct circ_buf *xmit = &up->port.state->xmit;
336 int count;
337
338 if (uart_tx_stopped(&up->port))
339 return;
340
341 count = up->port.fifosize;
342 do {
343 unsigned int rdata;
344
345 rdata = ar933x_uart_read(up, AR933X_UART_DATA_REG);
346 if ((rdata & AR933X_UART_DATA_TX_CSR) == 0)
347 break;
348
349 if (up->port.x_char) {
350 ar933x_uart_putc(up, up->port.x_char);
351 up->port.icount.tx++;
352 up->port.x_char = 0;
353 continue;
354 }
355
356 if (uart_circ_empty(xmit))
357 break;
358
359 ar933x_uart_putc(up, xmit->buf[xmit->tail]);
360
361 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
362 up->port.icount.tx++;
363 } while (--count > 0);
364
365 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
366 uart_write_wakeup(&up->port);
367
368 if (!uart_circ_empty(xmit))
369 ar933x_uart_start_tx_interrupt(up);
370}
371
372static irqreturn_t ar933x_uart_interrupt(int irq, void *dev_id)
373{
374 struct ar933x_uart_port *up = dev_id;
375 unsigned int status;
376
377 status = ar933x_uart_read(up, AR933X_UART_CS_REG);
378 if ((status & AR933X_UART_CS_HOST_INT) == 0)
379 return IRQ_NONE;
380
381 spin_lock(&up->port.lock);
382
383 status = ar933x_uart_read(up, AR933X_UART_INT_REG);
384 status &= ar933x_uart_read(up, AR933X_UART_INT_EN_REG);
385
386 if (status & AR933X_UART_INT_RX_VALID) {
387 ar933x_uart_write(up, AR933X_UART_INT_REG,
388 AR933X_UART_INT_RX_VALID);
389 ar933x_uart_rx_chars(up);
390 }
391
392 if (status & AR933X_UART_INT_TX_EMPTY) {
393 ar933x_uart_write(up, AR933X_UART_INT_REG,
394 AR933X_UART_INT_TX_EMPTY);
395 ar933x_uart_stop_tx_interrupt(up);
396 ar933x_uart_tx_chars(up);
397 }
398
399 spin_unlock(&up->port.lock);
400
401 return IRQ_HANDLED;
402}
403
404static int ar933x_uart_startup(struct uart_port *port)
405{
406 struct ar933x_uart_port *up = (struct ar933x_uart_port *) port;
407 unsigned long flags;
408 int ret;
409
410 ret = request_irq(up->port.irq, ar933x_uart_interrupt,
411 up->port.irqflags, dev_name(up->port.dev), up);
412 if (ret)
413 return ret;
414
415 spin_lock_irqsave(&up->port.lock, flags);
416
417 /* Enable HOST interrupts */
418 ar933x_uart_rmw_set(up, AR933X_UART_CS_REG,
419 AR933X_UART_CS_HOST_INT_EN);
420
421 /* Enable RX interrupts */
422 up->ier = AR933X_UART_INT_RX_VALID;
423 ar933x_uart_write(up, AR933X_UART_INT_EN_REG, up->ier);
424
425 spin_unlock_irqrestore(&up->port.lock, flags);
426
427 return 0;
428}
429
430static void ar933x_uart_shutdown(struct uart_port *port)
431{
432 struct ar933x_uart_port *up = (struct ar933x_uart_port *) port;
433
434 /* Disable all interrupts */
435 up->ier = 0;
436 ar933x_uart_write(up, AR933X_UART_INT_EN_REG, up->ier);
437
438 /* Disable break condition */
439 ar933x_uart_rmw_clear(up, AR933X_UART_CS_REG,
440 AR933X_UART_CS_TX_BREAK);
441
442 free_irq(up->port.irq, up);
443}
444
445static const char *ar933x_uart_type(struct uart_port *port)
446{
447 return (port->type == PORT_AR933X) ? "AR933X UART" : NULL;
448}
449
450static void ar933x_uart_release_port(struct uart_port *port)
451{
452 /* Nothing to release ... */
453}
454
455static int ar933x_uart_request_port(struct uart_port *port)
456{
457 /* UARTs always present */
458 return 0;
459}
460
461static void ar933x_uart_config_port(struct uart_port *port, int flags)
462{
463 if (flags & UART_CONFIG_TYPE)
464 port->type = PORT_AR933X;
465}
466
467static int ar933x_uart_verify_port(struct uart_port *port,
468 struct serial_struct *ser)
469{
Gabor Juhos2dff8ad2012-11-14 10:38:13 +0100470 struct ar933x_uart_port *up = (struct ar933x_uart_port *) port;
471
Gabor Juhosd57f3412011-06-20 19:26:11 +0200472 if (ser->type != PORT_UNKNOWN &&
473 ser->type != PORT_AR933X)
474 return -EINVAL;
475
476 if (ser->irq < 0 || ser->irq >= NR_IRQS)
477 return -EINVAL;
478
Gabor Juhos2dff8ad2012-11-14 10:38:13 +0100479 if (ser->baud_base < up->min_baud ||
480 ser->baud_base > up->max_baud)
Gabor Juhosd57f3412011-06-20 19:26:11 +0200481 return -EINVAL;
482
483 return 0;
484}
485
486static struct uart_ops ar933x_uart_ops = {
487 .tx_empty = ar933x_uart_tx_empty,
488 .set_mctrl = ar933x_uart_set_mctrl,
489 .get_mctrl = ar933x_uart_get_mctrl,
490 .stop_tx = ar933x_uart_stop_tx,
491 .start_tx = ar933x_uart_start_tx,
492 .stop_rx = ar933x_uart_stop_rx,
493 .enable_ms = ar933x_uart_enable_ms,
494 .break_ctl = ar933x_uart_break_ctl,
495 .startup = ar933x_uart_startup,
496 .shutdown = ar933x_uart_shutdown,
497 .set_termios = ar933x_uart_set_termios,
498 .type = ar933x_uart_type,
499 .release_port = ar933x_uart_release_port,
500 .request_port = ar933x_uart_request_port,
501 .config_port = ar933x_uart_config_port,
502 .verify_port = ar933x_uart_verify_port,
503};
504
505#ifdef CONFIG_SERIAL_AR933X_CONSOLE
506
507static struct ar933x_uart_port *
508ar933x_console_ports[CONFIG_SERIAL_AR933X_NR_UARTS];
509
510static void ar933x_uart_wait_xmitr(struct ar933x_uart_port *up)
511{
512 unsigned int status;
513 unsigned int timeout = 60000;
514
515 /* Wait up to 60ms for the character(s) to be sent. */
516 do {
517 status = ar933x_uart_read(up, AR933X_UART_DATA_REG);
518 if (--timeout == 0)
519 break;
520 udelay(1);
521 } while ((status & AR933X_UART_DATA_TX_CSR) == 0);
522}
523
524static void ar933x_uart_console_putchar(struct uart_port *port, int ch)
525{
526 struct ar933x_uart_port *up = (struct ar933x_uart_port *) port;
527
528 ar933x_uart_wait_xmitr(up);
529 ar933x_uart_putc(up, ch);
530}
531
532static void ar933x_uart_console_write(struct console *co, const char *s,
533 unsigned int count)
534{
535 struct ar933x_uart_port *up = ar933x_console_ports[co->index];
536 unsigned long flags;
537 unsigned int int_en;
538 int locked = 1;
539
540 local_irq_save(flags);
541
542 if (up->port.sysrq)
543 locked = 0;
544 else if (oops_in_progress)
545 locked = spin_trylock(&up->port.lock);
546 else
547 spin_lock(&up->port.lock);
548
549 /*
550 * First save the IER then disable the interrupts
551 */
552 int_en = ar933x_uart_read(up, AR933X_UART_INT_EN_REG);
553 ar933x_uart_write(up, AR933X_UART_INT_EN_REG, 0);
554
555 uart_console_write(&up->port, s, count, ar933x_uart_console_putchar);
556
557 /*
558 * Finally, wait for transmitter to become empty
559 * and restore the IER
560 */
561 ar933x_uart_wait_xmitr(up);
562 ar933x_uart_write(up, AR933X_UART_INT_EN_REG, int_en);
563
564 ar933x_uart_write(up, AR933X_UART_INT_REG, AR933X_UART_INT_ALLINTS);
565
566 if (locked)
567 spin_unlock(&up->port.lock);
568
569 local_irq_restore(flags);
570}
571
572static int ar933x_uart_console_setup(struct console *co, char *options)
573{
574 struct ar933x_uart_port *up;
575 int baud = 115200;
576 int bits = 8;
577 int parity = 'n';
578 int flow = 'n';
579
580 if (co->index < 0 || co->index >= CONFIG_SERIAL_AR933X_NR_UARTS)
581 return -EINVAL;
582
583 up = ar933x_console_ports[co->index];
584 if (!up)
585 return -ENODEV;
586
587 if (options)
588 uart_parse_options(options, &baud, &parity, &bits, &flow);
589
590 return uart_set_options(&up->port, co, baud, parity, bits, flow);
591}
592
593static struct console ar933x_uart_console = {
594 .name = "ttyATH",
595 .write = ar933x_uart_console_write,
596 .device = uart_console_device,
597 .setup = ar933x_uart_console_setup,
598 .flags = CON_PRINTBUFFER,
599 .index = -1,
600 .data = &ar933x_uart_driver,
601};
602
603static void ar933x_uart_add_console_port(struct ar933x_uart_port *up)
604{
605 ar933x_console_ports[up->port.line] = up;
606}
607
608#define AR933X_SERIAL_CONSOLE (&ar933x_uart_console)
609
610#else
611
612static inline void ar933x_uart_add_console_port(struct ar933x_uart_port *up) {}
613
614#define AR933X_SERIAL_CONSOLE NULL
615
616#endif /* CONFIG_SERIAL_AR933X_CONSOLE */
617
618static struct uart_driver ar933x_uart_driver = {
619 .owner = THIS_MODULE,
620 .driver_name = DRIVER_NAME,
621 .dev_name = "ttyATH",
622 .nr = CONFIG_SERIAL_AR933X_NR_UARTS,
623 .cons = AR933X_SERIAL_CONSOLE,
624};
625
Bill Pemberton9671f092012-11-19 13:21:50 -0500626static int ar933x_uart_probe(struct platform_device *pdev)
Gabor Juhosd57f3412011-06-20 19:26:11 +0200627{
628 struct ar933x_uart_platform_data *pdata;
629 struct ar933x_uart_port *up;
630 struct uart_port *port;
631 struct resource *mem_res;
632 struct resource *irq_res;
Gabor Juhos2dff8ad2012-11-14 10:38:13 +0100633 unsigned int baud;
Gabor Juhosd57f3412011-06-20 19:26:11 +0200634 int id;
635 int ret;
636
637 pdata = pdev->dev.platform_data;
638 if (!pdata)
639 return -EINVAL;
640
641 id = pdev->id;
642 if (id == -1)
643 id = 0;
644
645 if (id > CONFIG_SERIAL_AR933X_NR_UARTS)
646 return -EINVAL;
647
648 mem_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
649 if (!mem_res) {
650 dev_err(&pdev->dev, "no MEM resource\n");
651 return -EINVAL;
652 }
653
654 irq_res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
655 if (!irq_res) {
656 dev_err(&pdev->dev, "no IRQ resource\n");
657 return -EINVAL;
658 }
659
660 up = kzalloc(sizeof(struct ar933x_uart_port), GFP_KERNEL);
661 if (!up)
662 return -ENOMEM;
663
664 port = &up->port;
665 port->mapbase = mem_res->start;
666
667 port->membase = ioremap(mem_res->start, AR933X_UART_REGS_SIZE);
668 if (!port->membase) {
669 ret = -ENOMEM;
670 goto err_free_up;
671 }
672
673 port->line = id;
674 port->irq = irq_res->start;
675 port->dev = &pdev->dev;
676 port->type = PORT_AR933X;
677 port->iotype = UPIO_MEM32;
678 port->uartclk = pdata->uartclk;
679
680 port->regshift = 2;
681 port->fifosize = AR933X_UART_FIFO_SIZE;
682 port->ops = &ar933x_uart_ops;
683
Gabor Juhos2dff8ad2012-11-14 10:38:13 +0100684 baud = ar933x_uart_get_baud(port->uartclk, AR933X_UART_MAX_SCALE, 1);
685 up->min_baud = max_t(unsigned int, baud, AR933X_UART_MIN_BAUD);
686
687 baud = ar933x_uart_get_baud(port->uartclk, 0, AR933X_UART_MAX_STEP);
688 up->max_baud = min_t(unsigned int, baud, AR933X_UART_MAX_BAUD);
689
Gabor Juhosd57f3412011-06-20 19:26:11 +0200690 ar933x_uart_add_console_port(up);
691
692 ret = uart_add_one_port(&ar933x_uart_driver, &up->port);
693 if (ret)
694 goto err_unmap;
695
696 platform_set_drvdata(pdev, up);
697 return 0;
698
699err_unmap:
700 iounmap(up->port.membase);
701err_free_up:
702 kfree(up);
703 return ret;
704}
705
Bill Pembertonae8d8a12012-11-19 13:26:18 -0500706static int ar933x_uart_remove(struct platform_device *pdev)
Gabor Juhosd57f3412011-06-20 19:26:11 +0200707{
708 struct ar933x_uart_port *up;
709
710 up = platform_get_drvdata(pdev);
711 platform_set_drvdata(pdev, NULL);
712
713 if (up) {
714 uart_remove_one_port(&ar933x_uart_driver, &up->port);
715 iounmap(up->port.membase);
716 kfree(up);
717 }
718
719 return 0;
720}
721
722static struct platform_driver ar933x_uart_platform_driver = {
723 .probe = ar933x_uart_probe,
Bill Pemberton2d47b712012-11-19 13:21:34 -0500724 .remove = ar933x_uart_remove,
Gabor Juhosd57f3412011-06-20 19:26:11 +0200725 .driver = {
726 .name = DRIVER_NAME,
727 .owner = THIS_MODULE,
728 },
729};
730
731static int __init ar933x_uart_init(void)
732{
733 int ret;
734
735 ar933x_uart_driver.nr = CONFIG_SERIAL_AR933X_NR_UARTS;
736 ret = uart_register_driver(&ar933x_uart_driver);
737 if (ret)
738 goto err_out;
739
740 ret = platform_driver_register(&ar933x_uart_platform_driver);
741 if (ret)
742 goto err_unregister_uart_driver;
743
744 return 0;
745
746err_unregister_uart_driver:
747 uart_unregister_driver(&ar933x_uart_driver);
748err_out:
749 return ret;
750}
751
752static void __exit ar933x_uart_exit(void)
753{
754 platform_driver_unregister(&ar933x_uart_platform_driver);
755 uart_unregister_driver(&ar933x_uart_driver);
756}
757
758module_init(ar933x_uart_init);
759module_exit(ar933x_uart_exit);
760
761MODULE_DESCRIPTION("Atheros AR933X UART driver");
762MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org>");
763MODULE_LICENSE("GPL v2");
764MODULE_ALIAS("platform:" DRIVER_NAME);