1 /*
2 * Copyright (C) 2012-2015 Spreadtrum Communications Inc.
3 *
4 * This software is licensed under the terms of the GNU General Public
5 * License version 2, as published by the Free Software Foundation, and
6 * may be copied, distributed, and modified under those terms.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 */
13
14 #if defined(CONFIG_SERIAL_SPRD_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
15 #define SUPPORT_SYSRQ
16 #endif
17
18 #include <linux/clk.h>
19 #include <linux/console.h>
20 #include <linux/delay.h>
21 #include <linux/io.h>
22 #include <linux/ioport.h>
23 #include <linux/kernel.h>
24 #include <linux/module.h>
25 #include <linux/of.h>
26 #include <linux/platform_device.h>
27 #include <linux/serial_core.h>
28 #include <linux/serial.h>
29 #include <linux/slab.h>
30 #include <linux/tty.h>
31 #include <linux/tty_flip.h>
32
33 /* device name */
34 #define UART_NR_MAX 8
35 #define SPRD_TTY_NAME "ttyS"
36 #define SPRD_FIFO_SIZE 128
37 #define SPRD_DEF_RATE 26000000
38 #define SPRD_BAUD_IO_LIMIT 3000000
39 #define SPRD_TIMEOUT 256000
40
41 /* the offset of serial registers and BITs for them */
42 /* data registers */
43 #define SPRD_TXD 0x0000
44 #define SPRD_RXD 0x0004
45
46 /* line status register and its BITs */
47 #define SPRD_LSR 0x0008
48 #define SPRD_LSR_OE BIT(4)
49 #define SPRD_LSR_FE BIT(3)
50 #define SPRD_LSR_PE BIT(2)
51 #define SPRD_LSR_BI BIT(7)
52 #define SPRD_LSR_TX_OVER BIT(15)
53
54 /* data number in TX and RX fifo */
55 #define SPRD_STS1 0x000C
56
57 /* interrupt enable register and its BITs */
58 #define SPRD_IEN 0x0010
59 #define SPRD_IEN_RX_FULL BIT(0)
60 #define SPRD_IEN_TX_EMPTY BIT(1)
61 #define SPRD_IEN_BREAK_DETECT BIT(7)
62 #define SPRD_IEN_TIMEOUT BIT(13)
63
64 /* interrupt clear register */
65 #define SPRD_ICLR 0x0014
66 #define SPRD_ICLR_TIMEOUT BIT(13)
67
68 /* line control register */
69 #define SPRD_LCR 0x0018
70 #define SPRD_LCR_STOP_1BIT 0x10
71 #define SPRD_LCR_STOP_2BIT 0x30
72 #define SPRD_LCR_DATA_LEN (BIT(2) | BIT(3))
73 #define SPRD_LCR_DATA_LEN5 0x0
74 #define SPRD_LCR_DATA_LEN6 0x4
75 #define SPRD_LCR_DATA_LEN7 0x8
76 #define SPRD_LCR_DATA_LEN8 0xc
77 #define SPRD_LCR_PARITY (BIT(0) | BIT(1))
78 #define SPRD_LCR_PARITY_EN 0x2
79 #define SPRD_LCR_EVEN_PAR 0x0
80 #define SPRD_LCR_ODD_PAR 0x1
81
82 /* control register 1 */
83 #define SPRD_CTL1 0x001C
84 #define RX_HW_FLOW_CTL_THLD BIT(6)
85 #define RX_HW_FLOW_CTL_EN BIT(7)
86 #define TX_HW_FLOW_CTL_EN BIT(8)
87 #define RX_TOUT_THLD_DEF 0x3E00
88 #define RX_HFC_THLD_DEF 0x40
89
90 /* fifo threshold register */
91 #define SPRD_CTL2 0x0020
92 #define THLD_TX_EMPTY 0x40
93 #define THLD_RX_FULL 0x40
94
95 /* config baud rate register */
96 #define SPRD_CLKD0 0x0024
97 #define SPRD_CLKD1 0x0028
98
99 /* interrupt mask status register */
100 #define SPRD_IMSR 0x002C
101 #define SPRD_IMSR_RX_FIFO_FULL BIT(0)
102 #define SPRD_IMSR_TX_FIFO_EMPTY BIT(1)
103 #define SPRD_IMSR_BREAK_DETECT BIT(7)
104 #define SPRD_IMSR_TIMEOUT BIT(13)
105
106 struct reg_backup {
107 u32 ien;
108 u32 ctrl0;
109 u32 ctrl1;
110 u32 ctrl2;
111 u32 clkd0;
112 u32 clkd1;
113 u32 dspwait;
114 };
115
116 struct sprd_uart_port {
117 struct uart_port port;
118 struct reg_backup reg_bak;
119 char name[16];
120 };
121
122 static struct sprd_uart_port *sprd_port[UART_NR_MAX];
123 static int sprd_ports_num;
124
serial_in(struct uart_port * port,int offset)125 static inline unsigned int serial_in(struct uart_port *port, int offset)
126 {
127 return readl_relaxed(port->membase + offset);
128 }
129
serial_out(struct uart_port * port,int offset,int value)130 static inline void serial_out(struct uart_port *port, int offset, int value)
131 {
132 writel_relaxed(value, port->membase + offset);
133 }
134
sprd_tx_empty(struct uart_port * port)135 static unsigned int sprd_tx_empty(struct uart_port *port)
136 {
137 if (serial_in(port, SPRD_STS1) & 0xff00)
138 return 0;
139 else
140 return TIOCSER_TEMT;
141 }
142
sprd_get_mctrl(struct uart_port * port)143 static unsigned int sprd_get_mctrl(struct uart_port *port)
144 {
145 return TIOCM_DSR | TIOCM_CTS;
146 }
147
sprd_set_mctrl(struct uart_port * port,unsigned int mctrl)148 static void sprd_set_mctrl(struct uart_port *port, unsigned int mctrl)
149 {
150 /* nothing to do */
151 }
152
sprd_stop_tx(struct uart_port * port)153 static void sprd_stop_tx(struct uart_port *port)
154 {
155 unsigned int ien, iclr;
156
157 iclr = serial_in(port, SPRD_ICLR);
158 ien = serial_in(port, SPRD_IEN);
159
160 iclr |= SPRD_IEN_TX_EMPTY;
161 ien &= ~SPRD_IEN_TX_EMPTY;
162
163 serial_out(port, SPRD_ICLR, iclr);
164 serial_out(port, SPRD_IEN, ien);
165 }
166
sprd_start_tx(struct uart_port * port)167 static void sprd_start_tx(struct uart_port *port)
168 {
169 unsigned int ien;
170
171 ien = serial_in(port, SPRD_IEN);
172 if (!(ien & SPRD_IEN_TX_EMPTY)) {
173 ien |= SPRD_IEN_TX_EMPTY;
174 serial_out(port, SPRD_IEN, ien);
175 }
176 }
177
sprd_stop_rx(struct uart_port * port)178 static void sprd_stop_rx(struct uart_port *port)
179 {
180 unsigned int ien, iclr;
181
182 iclr = serial_in(port, SPRD_ICLR);
183 ien = serial_in(port, SPRD_IEN);
184
185 ien &= ~(SPRD_IEN_RX_FULL | SPRD_IEN_BREAK_DETECT);
186 iclr |= SPRD_IEN_RX_FULL | SPRD_IEN_BREAK_DETECT;
187
188 serial_out(port, SPRD_IEN, ien);
189 serial_out(port, SPRD_ICLR, iclr);
190 }
191
192 /* The Sprd serial does not support this function. */
sprd_break_ctl(struct uart_port * port,int break_state)193 static void sprd_break_ctl(struct uart_port *port, int break_state)
194 {
195 /* nothing to do */
196 }
197
handle_lsr_errors(struct uart_port * port,unsigned int * flag,unsigned int * lsr)198 static int handle_lsr_errors(struct uart_port *port,
199 unsigned int *flag,
200 unsigned int *lsr)
201 {
202 int ret = 0;
203
204 /* statistics */
205 if (*lsr & SPRD_LSR_BI) {
206 *lsr &= ~(SPRD_LSR_FE | SPRD_LSR_PE);
207 port->icount.brk++;
208 ret = uart_handle_break(port);
209 if (ret)
210 return ret;
211 } else if (*lsr & SPRD_LSR_PE)
212 port->icount.parity++;
213 else if (*lsr & SPRD_LSR_FE)
214 port->icount.frame++;
215 if (*lsr & SPRD_LSR_OE)
216 port->icount.overrun++;
217
218 /* mask off conditions which should be ignored */
219 *lsr &= port->read_status_mask;
220 if (*lsr & SPRD_LSR_BI)
221 *flag = TTY_BREAK;
222 else if (*lsr & SPRD_LSR_PE)
223 *flag = TTY_PARITY;
224 else if (*lsr & SPRD_LSR_FE)
225 *flag = TTY_FRAME;
226
227 return ret;
228 }
229
sprd_rx(struct uart_port * port)230 static inline void sprd_rx(struct uart_port *port)
231 {
232 struct tty_port *tty = &port->state->port;
233 unsigned int ch, flag, lsr, max_count = SPRD_TIMEOUT;
234
235 while ((serial_in(port, SPRD_STS1) & 0x00ff) && max_count--) {
236 lsr = serial_in(port, SPRD_LSR);
237 ch = serial_in(port, SPRD_RXD);
238 flag = TTY_NORMAL;
239 port->icount.rx++;
240
241 if (lsr & (SPRD_LSR_BI | SPRD_LSR_PE |
242 SPRD_LSR_FE | SPRD_LSR_OE))
243 if (handle_lsr_errors(port, &flag, &lsr))
244 continue;
245 if (uart_handle_sysrq_char(port, ch))
246 continue;
247
248 uart_insert_char(port, lsr, SPRD_LSR_OE, ch, flag);
249 }
250
251 tty_flip_buffer_push(tty);
252 }
253
sprd_tx(struct uart_port * port)254 static inline void sprd_tx(struct uart_port *port)
255 {
256 struct circ_buf *xmit = &port->state->xmit;
257 int count;
258
259 if (port->x_char) {
260 serial_out(port, SPRD_TXD, port->x_char);
261 port->icount.tx++;
262 port->x_char = 0;
263 return;
264 }
265
266 if (uart_circ_empty(xmit) || uart_tx_stopped(port)) {
267 sprd_stop_tx(port);
268 return;
269 }
270
271 count = THLD_TX_EMPTY;
272 do {
273 serial_out(port, SPRD_TXD, xmit->buf[xmit->tail]);
274 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
275 port->icount.tx++;
276 if (uart_circ_empty(xmit))
277 break;
278 } while (--count > 0);
279
280 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
281 uart_write_wakeup(port);
282
283 if (uart_circ_empty(xmit))
284 sprd_stop_tx(port);
285 }
286
287 /* this handles the interrupt from one port */
sprd_handle_irq(int irq,void * dev_id)288 static irqreturn_t sprd_handle_irq(int irq, void *dev_id)
289 {
290 struct uart_port *port = dev_id;
291 unsigned int ims;
292
293 spin_lock(&port->lock);
294
295 ims = serial_in(port, SPRD_IMSR);
296
297 if (!ims) {
298 spin_unlock(&port->lock);
299 return IRQ_NONE;
300 }
301
302 if (ims & SPRD_IMSR_TIMEOUT)
303 serial_out(port, SPRD_ICLR, SPRD_ICLR_TIMEOUT);
304
305 if (ims & (SPRD_IMSR_RX_FIFO_FULL |
306 SPRD_IMSR_BREAK_DETECT | SPRD_IMSR_TIMEOUT))
307 sprd_rx(port);
308
309 if (ims & SPRD_IMSR_TX_FIFO_EMPTY)
310 sprd_tx(port);
311
312 spin_unlock(&port->lock);
313
314 return IRQ_HANDLED;
315 }
316
sprd_startup(struct uart_port * port)317 static int sprd_startup(struct uart_port *port)
318 {
319 int ret = 0;
320 unsigned int ien, fc;
321 unsigned int timeout;
322 struct sprd_uart_port *sp;
323 unsigned long flags;
324
325 serial_out(port, SPRD_CTL2, ((THLD_TX_EMPTY << 8) | THLD_RX_FULL));
326
327 /* clear rx fifo */
328 timeout = SPRD_TIMEOUT;
329 while (timeout-- && serial_in(port, SPRD_STS1) & 0x00ff)
330 serial_in(port, SPRD_RXD);
331
332 /* clear tx fifo */
333 timeout = SPRD_TIMEOUT;
334 while (timeout-- && serial_in(port, SPRD_STS1) & 0xff00)
335 cpu_relax();
336
337 /* clear interrupt */
338 serial_out(port, SPRD_IEN, 0);
339 serial_out(port, SPRD_ICLR, ~0);
340
341 /* allocate irq */
342 sp = container_of(port, struct sprd_uart_port, port);
343 snprintf(sp->name, sizeof(sp->name), "sprd_serial%d", port->line);
344 ret = devm_request_irq(port->dev, port->irq, sprd_handle_irq,
345 IRQF_SHARED, sp->name, port);
346 if (ret) {
347 dev_err(port->dev, "fail to request serial irq %d, ret=%d\n",
348 port->irq, ret);
349 return ret;
350 }
351 fc = serial_in(port, SPRD_CTL1);
352 fc |= RX_TOUT_THLD_DEF | RX_HFC_THLD_DEF;
353 serial_out(port, SPRD_CTL1, fc);
354
355 /* enable interrupt */
356 spin_lock_irqsave(&port->lock, flags);
357 ien = serial_in(port, SPRD_IEN);
358 ien |= SPRD_IEN_RX_FULL | SPRD_IEN_BREAK_DETECT | SPRD_IEN_TIMEOUT;
359 serial_out(port, SPRD_IEN, ien);
360 spin_unlock_irqrestore(&port->lock, flags);
361
362 return 0;
363 }
364
sprd_shutdown(struct uart_port * port)365 static void sprd_shutdown(struct uart_port *port)
366 {
367 serial_out(port, SPRD_IEN, 0);
368 serial_out(port, SPRD_ICLR, ~0);
369 devm_free_irq(port->dev, port->irq, port);
370 }
371
sprd_set_termios(struct uart_port * port,struct ktermios * termios,struct ktermios * old)372 static void sprd_set_termios(struct uart_port *port,
373 struct ktermios *termios,
374 struct ktermios *old)
375 {
376 unsigned int baud, quot;
377 unsigned int lcr = 0, fc;
378 unsigned long flags;
379
380 /* ask the core to calculate the divisor for us */
381 baud = uart_get_baud_rate(port, termios, old, 0, SPRD_BAUD_IO_LIMIT);
382
383 quot = (unsigned int)((port->uartclk + baud / 2) / baud);
384
385 /* set data length */
386 switch (termios->c_cflag & CSIZE) {
387 case CS5:
388 lcr |= SPRD_LCR_DATA_LEN5;
389 break;
390 case CS6:
391 lcr |= SPRD_LCR_DATA_LEN6;
392 break;
393 case CS7:
394 lcr |= SPRD_LCR_DATA_LEN7;
395 break;
396 case CS8:
397 default:
398 lcr |= SPRD_LCR_DATA_LEN8;
399 break;
400 }
401
402 /* calculate stop bits */
403 lcr &= ~(SPRD_LCR_STOP_1BIT | SPRD_LCR_STOP_2BIT);
404 if (termios->c_cflag & CSTOPB)
405 lcr |= SPRD_LCR_STOP_2BIT;
406 else
407 lcr |= SPRD_LCR_STOP_1BIT;
408
409 /* calculate parity */
410 lcr &= ~SPRD_LCR_PARITY;
411 termios->c_cflag &= ~CMSPAR; /* no support mark/space */
412 if (termios->c_cflag & PARENB) {
413 lcr |= SPRD_LCR_PARITY_EN;
414 if (termios->c_cflag & PARODD)
415 lcr |= SPRD_LCR_ODD_PAR;
416 else
417 lcr |= SPRD_LCR_EVEN_PAR;
418 }
419
420 spin_lock_irqsave(&port->lock, flags);
421
422 /* update the per-port timeout */
423 uart_update_timeout(port, termios->c_cflag, baud);
424
425 port->read_status_mask = SPRD_LSR_OE;
426 if (termios->c_iflag & INPCK)
427 port->read_status_mask |= SPRD_LSR_FE | SPRD_LSR_PE;
428 if (termios->c_iflag & (IGNBRK | BRKINT | PARMRK))
429 port->read_status_mask |= SPRD_LSR_BI;
430
431 /* characters to ignore */
432 port->ignore_status_mask = 0;
433 if (termios->c_iflag & IGNPAR)
434 port->ignore_status_mask |= SPRD_LSR_PE | SPRD_LSR_FE;
435 if (termios->c_iflag & IGNBRK) {
436 port->ignore_status_mask |= SPRD_LSR_BI;
437 /*
438 * If we're ignoring parity and break indicators,
439 * ignore overruns too (for real raw support).
440 */
441 if (termios->c_iflag & IGNPAR)
442 port->ignore_status_mask |= SPRD_LSR_OE;
443 }
444
445 /* flow control */
446 fc = serial_in(port, SPRD_CTL1);
447 fc &= ~(RX_HW_FLOW_CTL_THLD | RX_HW_FLOW_CTL_EN | TX_HW_FLOW_CTL_EN);
448 if (termios->c_cflag & CRTSCTS) {
449 fc |= RX_HW_FLOW_CTL_THLD;
450 fc |= RX_HW_FLOW_CTL_EN;
451 fc |= TX_HW_FLOW_CTL_EN;
452 }
453
454 /* clock divider bit0~bit15 */
455 serial_out(port, SPRD_CLKD0, quot & 0xffff);
456
457 /* clock divider bit16~bit20 */
458 serial_out(port, SPRD_CLKD1, (quot & 0x1f0000) >> 16);
459 serial_out(port, SPRD_LCR, lcr);
460 fc |= RX_TOUT_THLD_DEF | RX_HFC_THLD_DEF;
461 serial_out(port, SPRD_CTL1, fc);
462
463 spin_unlock_irqrestore(&port->lock, flags);
464
465 /* Don't rewrite B0 */
466 if (tty_termios_baud_rate(termios))
467 tty_termios_encode_baud_rate(termios, baud, baud);
468 }
469
sprd_type(struct uart_port * port)470 static const char *sprd_type(struct uart_port *port)
471 {
472 return "SPX";
473 }
474
sprd_release_port(struct uart_port * port)475 static void sprd_release_port(struct uart_port *port)
476 {
477 /* nothing to do */
478 }
479
sprd_request_port(struct uart_port * port)480 static int sprd_request_port(struct uart_port *port)
481 {
482 return 0;
483 }
484
sprd_config_port(struct uart_port * port,int flags)485 static void sprd_config_port(struct uart_port *port, int flags)
486 {
487 if (flags & UART_CONFIG_TYPE)
488 port->type = PORT_SPRD;
489 }
490
sprd_verify_port(struct uart_port * port,struct serial_struct * ser)491 static int sprd_verify_port(struct uart_port *port,
492 struct serial_struct *ser)
493 {
494 if (ser->type != PORT_SPRD)
495 return -EINVAL;
496 if (port->irq != ser->irq)
497 return -EINVAL;
498 if (port->iotype != ser->io_type)
499 return -EINVAL;
500 return 0;
501 }
502
503 static const struct uart_ops serial_sprd_ops = {
504 .tx_empty = sprd_tx_empty,
505 .get_mctrl = sprd_get_mctrl,
506 .set_mctrl = sprd_set_mctrl,
507 .stop_tx = sprd_stop_tx,
508 .start_tx = sprd_start_tx,
509 .stop_rx = sprd_stop_rx,
510 .break_ctl = sprd_break_ctl,
511 .startup = sprd_startup,
512 .shutdown = sprd_shutdown,
513 .set_termios = sprd_set_termios,
514 .type = sprd_type,
515 .release_port = sprd_release_port,
516 .request_port = sprd_request_port,
517 .config_port = sprd_config_port,
518 .verify_port = sprd_verify_port,
519 };
520
521 #ifdef CONFIG_SERIAL_SPRD_CONSOLE
wait_for_xmitr(struct uart_port * port)522 static void wait_for_xmitr(struct uart_port *port)
523 {
524 unsigned int status, tmout = 10000;
525
526 /* wait up to 10ms for the character(s) to be sent */
527 do {
528 status = serial_in(port, SPRD_STS1);
529 if (--tmout == 0)
530 break;
531 udelay(1);
532 } while (status & 0xff00);
533 }
534
sprd_console_putchar(struct uart_port * port,int ch)535 static void sprd_console_putchar(struct uart_port *port, int ch)
536 {
537 wait_for_xmitr(port);
538 serial_out(port, SPRD_TXD, ch);
539 }
540
sprd_console_write(struct console * co,const char * s,unsigned int count)541 static void sprd_console_write(struct console *co, const char *s,
542 unsigned int count)
543 {
544 struct uart_port *port = &sprd_port[co->index]->port;
545 int locked = 1;
546 unsigned long flags;
547
548 if (port->sysrq)
549 locked = 0;
550 else if (oops_in_progress)
551 locked = spin_trylock_irqsave(&port->lock, flags);
552 else
553 spin_lock_irqsave(&port->lock, flags);
554
555 uart_console_write(port, s, count, sprd_console_putchar);
556
557 /* wait for transmitter to become empty */
558 wait_for_xmitr(port);
559
560 if (locked)
561 spin_unlock_irqrestore(&port->lock, flags);
562 }
563
sprd_console_setup(struct console * co,char * options)564 static int __init sprd_console_setup(struct console *co, char *options)
565 {
566 struct uart_port *port;
567 int baud = 115200;
568 int bits = 8;
569 int parity = 'n';
570 int flow = 'n';
571
572 if (co->index >= UART_NR_MAX || co->index < 0)
573 co->index = 0;
574
575 port = &sprd_port[co->index]->port;
576 if (port == NULL) {
577 pr_info("serial port %d not yet initialized\n", co->index);
578 return -ENODEV;
579 }
580 if (options)
581 uart_parse_options(options, &baud, &parity, &bits, &flow);
582
583 return uart_set_options(port, co, baud, parity, bits, flow);
584 }
585
586 static struct uart_driver sprd_uart_driver;
587 static struct console sprd_console = {
588 .name = SPRD_TTY_NAME,
589 .write = sprd_console_write,
590 .device = uart_console_device,
591 .setup = sprd_console_setup,
592 .flags = CON_PRINTBUFFER,
593 .index = -1,
594 .data = &sprd_uart_driver,
595 };
596
597 #define SPRD_CONSOLE (&sprd_console)
598
599 /* Support for earlycon */
sprd_putc(struct uart_port * port,int c)600 static void sprd_putc(struct uart_port *port, int c)
601 {
602 unsigned int timeout = SPRD_TIMEOUT;
603
604 while (timeout-- &&
605 !(readl(port->membase + SPRD_LSR) & SPRD_LSR_TX_OVER))
606 cpu_relax();
607
608 writeb(c, port->membase + SPRD_TXD);
609 }
610
sprd_early_write(struct console * con,const char * s,unsigned n)611 static void sprd_early_write(struct console *con, const char *s,
612 unsigned n)
613 {
614 struct earlycon_device *dev = con->data;
615
616 uart_console_write(&dev->port, s, n, sprd_putc);
617 }
618
sprd_early_console_setup(struct earlycon_device * device,const char * opt)619 static int __init sprd_early_console_setup(
620 struct earlycon_device *device,
621 const char *opt)
622 {
623 if (!device->port.membase)
624 return -ENODEV;
625
626 device->con->write = sprd_early_write;
627 return 0;
628 }
629 OF_EARLYCON_DECLARE(sprd_serial, "sprd,sc9836-uart",
630 sprd_early_console_setup);
631
632 #else /* !CONFIG_SERIAL_SPRD_CONSOLE */
633 #define SPRD_CONSOLE NULL
634 #endif
635
636 static struct uart_driver sprd_uart_driver = {
637 .owner = THIS_MODULE,
638 .driver_name = "sprd_serial",
639 .dev_name = SPRD_TTY_NAME,
640 .major = 0,
641 .minor = 0,
642 .nr = UART_NR_MAX,
643 .cons = SPRD_CONSOLE,
644 };
645
sprd_probe_dt_alias(int index,struct device * dev)646 static int sprd_probe_dt_alias(int index, struct device *dev)
647 {
648 struct device_node *np;
649 int ret = index;
650
651 if (!IS_ENABLED(CONFIG_OF))
652 return ret;
653
654 np = dev->of_node;
655 if (!np)
656 return ret;
657
658 ret = of_alias_get_id(np, "serial");
659 if (ret < 0)
660 ret = index;
661 else if (ret >= ARRAY_SIZE(sprd_port) || sprd_port[ret] != NULL) {
662 dev_warn(dev, "requested serial port %d not available.\n", ret);
663 ret = index;
664 }
665
666 return ret;
667 }
668
sprd_remove(struct platform_device * dev)669 static int sprd_remove(struct platform_device *dev)
670 {
671 struct sprd_uart_port *sup = platform_get_drvdata(dev);
672
673 if (sup) {
674 uart_remove_one_port(&sprd_uart_driver, &sup->port);
675 sprd_port[sup->port.line] = NULL;
676 sprd_ports_num--;
677 }
678
679 if (!sprd_ports_num)
680 uart_unregister_driver(&sprd_uart_driver);
681
682 return 0;
683 }
684
sprd_probe(struct platform_device * pdev)685 static int sprd_probe(struct platform_device *pdev)
686 {
687 struct resource *res;
688 struct uart_port *up;
689 struct clk *clk;
690 int irq;
691 int index;
692 int ret;
693
694 for (index = 0; index < ARRAY_SIZE(sprd_port); index++)
695 if (sprd_port[index] == NULL)
696 break;
697
698 if (index == ARRAY_SIZE(sprd_port))
699 return -EBUSY;
700
701 index = sprd_probe_dt_alias(index, &pdev->dev);
702
703 sprd_port[index] = devm_kzalloc(&pdev->dev,
704 sizeof(*sprd_port[index]), GFP_KERNEL);
705 if (!sprd_port[index])
706 return -ENOMEM;
707
708 up = &sprd_port[index]->port;
709 up->dev = &pdev->dev;
710 up->line = index;
711 up->type = PORT_SPRD;
712 up->iotype = UPIO_MEM;
713 up->uartclk = SPRD_DEF_RATE;
714 up->fifosize = SPRD_FIFO_SIZE;
715 up->ops = &serial_sprd_ops;
716 up->flags = UPF_BOOT_AUTOCONF;
717
718 clk = devm_clk_get(&pdev->dev, NULL);
719 if (!IS_ERR_OR_NULL(clk))
720 up->uartclk = clk_get_rate(clk);
721
722 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
723 if (!res) {
724 dev_err(&pdev->dev, "not provide mem resource\n");
725 return -ENODEV;
726 }
727 up->mapbase = res->start;
728 up->membase = devm_ioremap_resource(&pdev->dev, res);
729 if (IS_ERR(up->membase))
730 return PTR_ERR(up->membase);
731
732 irq = platform_get_irq(pdev, 0);
733 if (irq < 0) {
734 dev_err(&pdev->dev, "not provide irq resource: %d\n", irq);
735 return irq;
736 }
737 up->irq = irq;
738
739 if (!sprd_ports_num) {
740 ret = uart_register_driver(&sprd_uart_driver);
741 if (ret < 0) {
742 pr_err("Failed to register SPRD-UART driver\n");
743 return ret;
744 }
745 }
746 sprd_ports_num++;
747
748 ret = uart_add_one_port(&sprd_uart_driver, up);
749 if (ret) {
750 sprd_port[index] = NULL;
751 sprd_remove(pdev);
752 }
753
754 platform_set_drvdata(pdev, up);
755
756 return ret;
757 }
758
759 #ifdef CONFIG_PM_SLEEP
sprd_suspend(struct device * dev)760 static int sprd_suspend(struct device *dev)
761 {
762 struct sprd_uart_port *sup = dev_get_drvdata(dev);
763
764 uart_suspend_port(&sprd_uart_driver, &sup->port);
765
766 return 0;
767 }
768
sprd_resume(struct device * dev)769 static int sprd_resume(struct device *dev)
770 {
771 struct sprd_uart_port *sup = dev_get_drvdata(dev);
772
773 uart_resume_port(&sprd_uart_driver, &sup->port);
774
775 return 0;
776 }
777 #endif
778
779 static SIMPLE_DEV_PM_OPS(sprd_pm_ops, sprd_suspend, sprd_resume);
780
781 static const struct of_device_id serial_ids[] = {
782 {.compatible = "sprd,sc9836-uart",},
783 {}
784 };
785 MODULE_DEVICE_TABLE(of, serial_ids);
786
787 static struct platform_driver sprd_platform_driver = {
788 .probe = sprd_probe,
789 .remove = sprd_remove,
790 .driver = {
791 .name = "sprd_serial",
792 .of_match_table = of_match_ptr(serial_ids),
793 .pm = &sprd_pm_ops,
794 },
795 };
796
797 module_platform_driver(sprd_platform_driver);
798
799 MODULE_LICENSE("GPL v2");
800 MODULE_DESCRIPTION("Spreadtrum SoC serial driver series");
801