blob: 78be13ac1a957d673905efe995884b5dc5ad926c [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 int max_count = 256;
302
Gabor Juhosd57f3412011-06-20 19:26:11 +0200303 do {
304 unsigned int rdata;
305 unsigned char ch;
306
307 rdata = ar933x_uart_read(up, AR933X_UART_DATA_REG);
308 if ((rdata & AR933X_UART_DATA_RX_CSR) == 0)
309 break;
310
311 /* remove the character from the FIFO */
312 ar933x_uart_write(up, AR933X_UART_DATA_REG,
313 AR933X_UART_DATA_RX_CSR);
314
Gabor Juhosd57f3412011-06-20 19:26:11 +0200315 up->port.icount.rx++;
316 ch = rdata & AR933X_UART_DATA_TX_RX_MASK;
317
318 if (uart_handle_sysrq_char(&up->port, ch))
319 continue;
320
321 if ((up->port.ignore_status_mask & AR933X_DUMMY_STATUS_RD) == 0)
Jiri Slaby92a19f92013-01-03 15:53:03 +0100322 tty_insert_flip_char(port, ch, TTY_NORMAL);
Gabor Juhosd57f3412011-06-20 19:26:11 +0200323 } while (max_count-- > 0);
324
Viresh Kumarb16c8e32013-08-19 20:14:08 +0530325 spin_unlock(&up->port.lock);
Jiri Slaby2e124b42013-01-03 15:53:06 +0100326 tty_flip_buffer_push(port);
Viresh Kumarb16c8e32013-08-19 20:14:08 +0530327 spin_lock(&up->port.lock);
Gabor Juhosd57f3412011-06-20 19:26:11 +0200328}
329
330static void ar933x_uart_tx_chars(struct ar933x_uart_port *up)
331{
332 struct circ_buf *xmit = &up->port.state->xmit;
333 int count;
334
335 if (uart_tx_stopped(&up->port))
336 return;
337
338 count = up->port.fifosize;
339 do {
340 unsigned int rdata;
341
342 rdata = ar933x_uart_read(up, AR933X_UART_DATA_REG);
343 if ((rdata & AR933X_UART_DATA_TX_CSR) == 0)
344 break;
345
346 if (up->port.x_char) {
347 ar933x_uart_putc(up, up->port.x_char);
348 up->port.icount.tx++;
349 up->port.x_char = 0;
350 continue;
351 }
352
353 if (uart_circ_empty(xmit))
354 break;
355
356 ar933x_uart_putc(up, xmit->buf[xmit->tail]);
357
358 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
359 up->port.icount.tx++;
360 } while (--count > 0);
361
362 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
363 uart_write_wakeup(&up->port);
364
365 if (!uart_circ_empty(xmit))
366 ar933x_uart_start_tx_interrupt(up);
367}
368
369static irqreturn_t ar933x_uart_interrupt(int irq, void *dev_id)
370{
371 struct ar933x_uart_port *up = dev_id;
372 unsigned int status;
373
374 status = ar933x_uart_read(up, AR933X_UART_CS_REG);
375 if ((status & AR933X_UART_CS_HOST_INT) == 0)
376 return IRQ_NONE;
377
378 spin_lock(&up->port.lock);
379
380 status = ar933x_uart_read(up, AR933X_UART_INT_REG);
381 status &= ar933x_uart_read(up, AR933X_UART_INT_EN_REG);
382
383 if (status & AR933X_UART_INT_RX_VALID) {
384 ar933x_uart_write(up, AR933X_UART_INT_REG,
385 AR933X_UART_INT_RX_VALID);
386 ar933x_uart_rx_chars(up);
387 }
388
389 if (status & AR933X_UART_INT_TX_EMPTY) {
390 ar933x_uart_write(up, AR933X_UART_INT_REG,
391 AR933X_UART_INT_TX_EMPTY);
392 ar933x_uart_stop_tx_interrupt(up);
393 ar933x_uart_tx_chars(up);
394 }
395
396 spin_unlock(&up->port.lock);
397
398 return IRQ_HANDLED;
399}
400
401static int ar933x_uart_startup(struct uart_port *port)
402{
403 struct ar933x_uart_port *up = (struct ar933x_uart_port *) port;
404 unsigned long flags;
405 int ret;
406
407 ret = request_irq(up->port.irq, ar933x_uart_interrupt,
408 up->port.irqflags, dev_name(up->port.dev), up);
409 if (ret)
410 return ret;
411
412 spin_lock_irqsave(&up->port.lock, flags);
413
414 /* Enable HOST interrupts */
415 ar933x_uart_rmw_set(up, AR933X_UART_CS_REG,
416 AR933X_UART_CS_HOST_INT_EN);
417
418 /* Enable RX interrupts */
419 up->ier = AR933X_UART_INT_RX_VALID;
420 ar933x_uart_write(up, AR933X_UART_INT_EN_REG, up->ier);
421
422 spin_unlock_irqrestore(&up->port.lock, flags);
423
424 return 0;
425}
426
427static void ar933x_uart_shutdown(struct uart_port *port)
428{
429 struct ar933x_uart_port *up = (struct ar933x_uart_port *) port;
430
431 /* Disable all interrupts */
432 up->ier = 0;
433 ar933x_uart_write(up, AR933X_UART_INT_EN_REG, up->ier);
434
435 /* Disable break condition */
436 ar933x_uart_rmw_clear(up, AR933X_UART_CS_REG,
437 AR933X_UART_CS_TX_BREAK);
438
439 free_irq(up->port.irq, up);
440}
441
442static const char *ar933x_uart_type(struct uart_port *port)
443{
444 return (port->type == PORT_AR933X) ? "AR933X UART" : NULL;
445}
446
447static void ar933x_uart_release_port(struct uart_port *port)
448{
449 /* Nothing to release ... */
450}
451
452static int ar933x_uart_request_port(struct uart_port *port)
453{
454 /* UARTs always present */
455 return 0;
456}
457
458static void ar933x_uart_config_port(struct uart_port *port, int flags)
459{
460 if (flags & UART_CONFIG_TYPE)
461 port->type = PORT_AR933X;
462}
463
464static int ar933x_uart_verify_port(struct uart_port *port,
465 struct serial_struct *ser)
466{
Gabor Juhos2dff8ad2012-11-14 10:38:13 +0100467 struct ar933x_uart_port *up = (struct ar933x_uart_port *) port;
468
Gabor Juhosd57f3412011-06-20 19:26:11 +0200469 if (ser->type != PORT_UNKNOWN &&
470 ser->type != PORT_AR933X)
471 return -EINVAL;
472
473 if (ser->irq < 0 || ser->irq >= NR_IRQS)
474 return -EINVAL;
475
Gabor Juhos2dff8ad2012-11-14 10:38:13 +0100476 if (ser->baud_base < up->min_baud ||
477 ser->baud_base > up->max_baud)
Gabor Juhosd57f3412011-06-20 19:26:11 +0200478 return -EINVAL;
479
480 return 0;
481}
482
483static struct uart_ops ar933x_uart_ops = {
484 .tx_empty = ar933x_uart_tx_empty,
485 .set_mctrl = ar933x_uart_set_mctrl,
486 .get_mctrl = ar933x_uart_get_mctrl,
487 .stop_tx = ar933x_uart_stop_tx,
488 .start_tx = ar933x_uart_start_tx,
489 .stop_rx = ar933x_uart_stop_rx,
490 .enable_ms = ar933x_uart_enable_ms,
491 .break_ctl = ar933x_uart_break_ctl,
492 .startup = ar933x_uart_startup,
493 .shutdown = ar933x_uart_shutdown,
494 .set_termios = ar933x_uart_set_termios,
495 .type = ar933x_uart_type,
496 .release_port = ar933x_uart_release_port,
497 .request_port = ar933x_uart_request_port,
498 .config_port = ar933x_uart_config_port,
499 .verify_port = ar933x_uart_verify_port,
500};
501
502#ifdef CONFIG_SERIAL_AR933X_CONSOLE
503
504static struct ar933x_uart_port *
505ar933x_console_ports[CONFIG_SERIAL_AR933X_NR_UARTS];
506
507static void ar933x_uart_wait_xmitr(struct ar933x_uart_port *up)
508{
509 unsigned int status;
510 unsigned int timeout = 60000;
511
512 /* Wait up to 60ms for the character(s) to be sent. */
513 do {
514 status = ar933x_uart_read(up, AR933X_UART_DATA_REG);
515 if (--timeout == 0)
516 break;
517 udelay(1);
518 } while ((status & AR933X_UART_DATA_TX_CSR) == 0);
519}
520
521static void ar933x_uart_console_putchar(struct uart_port *port, int ch)
522{
523 struct ar933x_uart_port *up = (struct ar933x_uart_port *) port;
524
525 ar933x_uart_wait_xmitr(up);
526 ar933x_uart_putc(up, ch);
527}
528
529static void ar933x_uart_console_write(struct console *co, const char *s,
530 unsigned int count)
531{
532 struct ar933x_uart_port *up = ar933x_console_ports[co->index];
533 unsigned long flags;
534 unsigned int int_en;
535 int locked = 1;
536
537 local_irq_save(flags);
538
539 if (up->port.sysrq)
540 locked = 0;
541 else if (oops_in_progress)
542 locked = spin_trylock(&up->port.lock);
543 else
544 spin_lock(&up->port.lock);
545
546 /*
547 * First save the IER then disable the interrupts
548 */
549 int_en = ar933x_uart_read(up, AR933X_UART_INT_EN_REG);
550 ar933x_uart_write(up, AR933X_UART_INT_EN_REG, 0);
551
552 uart_console_write(&up->port, s, count, ar933x_uart_console_putchar);
553
554 /*
555 * Finally, wait for transmitter to become empty
556 * and restore the IER
557 */
558 ar933x_uart_wait_xmitr(up);
559 ar933x_uart_write(up, AR933X_UART_INT_EN_REG, int_en);
560
561 ar933x_uart_write(up, AR933X_UART_INT_REG, AR933X_UART_INT_ALLINTS);
562
563 if (locked)
564 spin_unlock(&up->port.lock);
565
566 local_irq_restore(flags);
567}
568
569static int ar933x_uart_console_setup(struct console *co, char *options)
570{
571 struct ar933x_uart_port *up;
572 int baud = 115200;
573 int bits = 8;
574 int parity = 'n';
575 int flow = 'n';
576
577 if (co->index < 0 || co->index >= CONFIG_SERIAL_AR933X_NR_UARTS)
578 return -EINVAL;
579
580 up = ar933x_console_ports[co->index];
581 if (!up)
582 return -ENODEV;
583
584 if (options)
585 uart_parse_options(options, &baud, &parity, &bits, &flow);
586
587 return uart_set_options(&up->port, co, baud, parity, bits, flow);
588}
589
590static struct console ar933x_uart_console = {
591 .name = "ttyATH",
592 .write = ar933x_uart_console_write,
593 .device = uart_console_device,
594 .setup = ar933x_uart_console_setup,
595 .flags = CON_PRINTBUFFER,
596 .index = -1,
597 .data = &ar933x_uart_driver,
598};
599
600static void ar933x_uart_add_console_port(struct ar933x_uart_port *up)
601{
602 ar933x_console_ports[up->port.line] = up;
603}
604
605#define AR933X_SERIAL_CONSOLE (&ar933x_uart_console)
606
607#else
608
609static inline void ar933x_uart_add_console_port(struct ar933x_uart_port *up) {}
610
611#define AR933X_SERIAL_CONSOLE NULL
612
613#endif /* CONFIG_SERIAL_AR933X_CONSOLE */
614
615static struct uart_driver ar933x_uart_driver = {
616 .owner = THIS_MODULE,
617 .driver_name = DRIVER_NAME,
618 .dev_name = "ttyATH",
619 .nr = CONFIG_SERIAL_AR933X_NR_UARTS,
620 .cons = AR933X_SERIAL_CONSOLE,
621};
622
Bill Pemberton9671f092012-11-19 13:21:50 -0500623static int ar933x_uart_probe(struct platform_device *pdev)
Gabor Juhosd57f3412011-06-20 19:26:11 +0200624{
625 struct ar933x_uart_platform_data *pdata;
626 struct ar933x_uart_port *up;
627 struct uart_port *port;
628 struct resource *mem_res;
629 struct resource *irq_res;
Gabor Juhos2dff8ad2012-11-14 10:38:13 +0100630 unsigned int baud;
Gabor Juhosd57f3412011-06-20 19:26:11 +0200631 int id;
632 int ret;
633
Jingoo Han574de552013-07-30 17:06:57 +0900634 pdata = dev_get_platdata(&pdev->dev);
Gabor Juhosd57f3412011-06-20 19:26:11 +0200635 if (!pdata)
636 return -EINVAL;
637
638 id = pdev->id;
639 if (id == -1)
640 id = 0;
641
642 if (id > CONFIG_SERIAL_AR933X_NR_UARTS)
643 return -EINVAL;
644
Gabor Juhosd57f3412011-06-20 19:26:11 +0200645 irq_res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
646 if (!irq_res) {
647 dev_err(&pdev->dev, "no IRQ resource\n");
648 return -EINVAL;
649 }
650
Gabor Juhosa324e4d2013-08-10 09:32:15 +0200651 up = devm_kzalloc(&pdev->dev, sizeof(struct ar933x_uart_port),
652 GFP_KERNEL);
Gabor Juhosd57f3412011-06-20 19:26:11 +0200653 if (!up)
654 return -ENOMEM;
655
656 port = &up->port;
Gabor Juhosa324e4d2013-08-10 09:32:15 +0200657
Julia Lawall071eb0f2013-08-14 11:11:22 +0200658 mem_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Gabor Juhosa324e4d2013-08-10 09:32:15 +0200659 port->membase = devm_ioremap_resource(&pdev->dev, mem_res);
660 if (IS_ERR(port->membase))
661 return PTR_ERR(port->membase);
662
Gabor Juhosd57f3412011-06-20 19:26:11 +0200663 port->mapbase = mem_res->start;
Gabor Juhosd57f3412011-06-20 19:26:11 +0200664 port->line = id;
665 port->irq = irq_res->start;
666 port->dev = &pdev->dev;
667 port->type = PORT_AR933X;
668 port->iotype = UPIO_MEM32;
669 port->uartclk = pdata->uartclk;
670
671 port->regshift = 2;
672 port->fifosize = AR933X_UART_FIFO_SIZE;
673 port->ops = &ar933x_uart_ops;
674
Gabor Juhos2dff8ad2012-11-14 10:38:13 +0100675 baud = ar933x_uart_get_baud(port->uartclk, AR933X_UART_MAX_SCALE, 1);
676 up->min_baud = max_t(unsigned int, baud, AR933X_UART_MIN_BAUD);
677
678 baud = ar933x_uart_get_baud(port->uartclk, 0, AR933X_UART_MAX_STEP);
679 up->max_baud = min_t(unsigned int, baud, AR933X_UART_MAX_BAUD);
680
Gabor Juhosd57f3412011-06-20 19:26:11 +0200681 ar933x_uart_add_console_port(up);
682
683 ret = uart_add_one_port(&ar933x_uart_driver, &up->port);
684 if (ret)
Gabor Juhosa324e4d2013-08-10 09:32:15 +0200685 return ret;
Gabor Juhosd57f3412011-06-20 19:26:11 +0200686
687 platform_set_drvdata(pdev, up);
688 return 0;
Gabor Juhosd57f3412011-06-20 19:26:11 +0200689}
690
Bill Pembertonae8d8a12012-11-19 13:26:18 -0500691static int ar933x_uart_remove(struct platform_device *pdev)
Gabor Juhosd57f3412011-06-20 19:26:11 +0200692{
693 struct ar933x_uart_port *up;
694
695 up = platform_get_drvdata(pdev);
Gabor Juhosd57f3412011-06-20 19:26:11 +0200696
Gabor Juhosa324e4d2013-08-10 09:32:15 +0200697 if (up)
Gabor Juhosd57f3412011-06-20 19:26:11 +0200698 uart_remove_one_port(&ar933x_uart_driver, &up->port);
Gabor Juhosd57f3412011-06-20 19:26:11 +0200699
700 return 0;
701}
702
703static struct platform_driver ar933x_uart_platform_driver = {
704 .probe = ar933x_uart_probe,
Bill Pemberton2d47b712012-11-19 13:21:34 -0500705 .remove = ar933x_uart_remove,
Gabor Juhosd57f3412011-06-20 19:26:11 +0200706 .driver = {
707 .name = DRIVER_NAME,
708 .owner = THIS_MODULE,
709 },
710};
711
712static int __init ar933x_uart_init(void)
713{
714 int ret;
715
716 ar933x_uart_driver.nr = CONFIG_SERIAL_AR933X_NR_UARTS;
717 ret = uart_register_driver(&ar933x_uart_driver);
718 if (ret)
719 goto err_out;
720
721 ret = platform_driver_register(&ar933x_uart_platform_driver);
722 if (ret)
723 goto err_unregister_uart_driver;
724
725 return 0;
726
727err_unregister_uart_driver:
728 uart_unregister_driver(&ar933x_uart_driver);
729err_out:
730 return ret;
731}
732
733static void __exit ar933x_uart_exit(void)
734{
735 platform_driver_unregister(&ar933x_uart_platform_driver);
736 uart_unregister_driver(&ar933x_uart_driver);
737}
738
739module_init(ar933x_uart_init);
740module_exit(ar933x_uart_exit);
741
742MODULE_DESCRIPTION("Atheros AR933X UART driver");
743MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org>");
744MODULE_LICENSE("GPL v2");
745MODULE_ALIAS("platform:" DRIVER_NAME);