Linux Audio

Check our new training course

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