1 // SPDX-License-Identifier: GPL-2.0+
3 * PIC32 Integrated Serial Driver.
5 * Copyright (C) 2015 Microchip Technology, Inc.
11 #include <linux/gpio/consumer.h>
12 #include <linux/kernel.h>
13 #include <linux/platform_device.h>
15 #include <linux/of_irq.h>
16 #include <linux/init.h>
17 #include <linux/module.h>
18 #include <linux/slab.h>
19 #include <linux/console.h>
20 #include <linux/clk.h>
21 #include <linux/tty.h>
22 #include <linux/tty_flip.h>
23 #include <linux/serial_core.h>
24 #include <linux/delay.h>
26 #include <asm/mach-pic32/pic32.h>
28 /* UART name and device definitions */
29 #define PIC32_DEV_NAME "pic32-uart"
30 #define PIC32_MAX_UARTS 6
31 #define PIC32_SDEV_NAME "ttyPIC"
33 #define PIC32_UART_DFLT_BRATE 9600
34 #define PIC32_UART_TX_FIFO_DEPTH 8
35 #define PIC32_UART_RX_FIFO_DEPTH 8
37 #define PIC32_UART_MODE 0x00
38 #define PIC32_UART_STA 0x10
39 #define PIC32_UART_TX 0x20
40 #define PIC32_UART_RX 0x30
41 #define PIC32_UART_BRG 0x40
43 /* struct pic32_sport - pic32 serial port descriptor
44 * @port: uart port descriptor
46 * @irq_fault: virtual fault interrupt number
47 * @irq_fault_name: irq fault name
48 * @irq_rx: virtual rx interrupt number
49 * @irq_rx_name: irq rx name
50 * @irq_tx: virtual tx interrupt number
51 * @irq_tx_name: irq tx name
52 * @cts_gpiod: clear to send GPIO
53 * @dev: device descriptor
56 struct uart_port port;
60 const char *irq_fault_name;
62 const char *irq_rx_name;
64 const char *irq_tx_name;
67 struct gpio_desc *cts_gpiod;
74 static inline struct pic32_sport *to_pic32_sport(struct uart_port *port)
76 return container_of(port, struct pic32_sport, port);
79 static inline void pic32_uart_writel(struct pic32_sport *sport,
82 __raw_writel(val, sport->port.membase + reg);
85 static inline u32 pic32_uart_readl(struct pic32_sport *sport, u32 reg)
87 return __raw_readl(sport->port.membase + reg);
90 /* pic32 uart mode register bits */
91 #define PIC32_UART_MODE_ON BIT(15)
92 #define PIC32_UART_MODE_FRZ BIT(14)
93 #define PIC32_UART_MODE_SIDL BIT(13)
94 #define PIC32_UART_MODE_IREN BIT(12)
95 #define PIC32_UART_MODE_RTSMD BIT(11)
96 #define PIC32_UART_MODE_RESV1 BIT(10)
97 #define PIC32_UART_MODE_UEN1 BIT(9)
98 #define PIC32_UART_MODE_UEN0 BIT(8)
99 #define PIC32_UART_MODE_WAKE BIT(7)
100 #define PIC32_UART_MODE_LPBK BIT(6)
101 #define PIC32_UART_MODE_ABAUD BIT(5)
102 #define PIC32_UART_MODE_RXINV BIT(4)
103 #define PIC32_UART_MODE_BRGH BIT(3)
104 #define PIC32_UART_MODE_PDSEL1 BIT(2)
105 #define PIC32_UART_MODE_PDSEL0 BIT(1)
106 #define PIC32_UART_MODE_STSEL BIT(0)
108 /* pic32 uart status register bits */
109 #define PIC32_UART_STA_UTXISEL1 BIT(15)
110 #define PIC32_UART_STA_UTXISEL0 BIT(14)
111 #define PIC32_UART_STA_UTXINV BIT(13)
112 #define PIC32_UART_STA_URXEN BIT(12)
113 #define PIC32_UART_STA_UTXBRK BIT(11)
114 #define PIC32_UART_STA_UTXEN BIT(10)
115 #define PIC32_UART_STA_UTXBF BIT(9)
116 #define PIC32_UART_STA_TRMT BIT(8)
117 #define PIC32_UART_STA_URXISEL1 BIT(7)
118 #define PIC32_UART_STA_URXISEL0 BIT(6)
119 #define PIC32_UART_STA_ADDEN BIT(5)
120 #define PIC32_UART_STA_RIDLE BIT(4)
121 #define PIC32_UART_STA_PERR BIT(3)
122 #define PIC32_UART_STA_FERR BIT(2)
123 #define PIC32_UART_STA_OERR BIT(1)
124 #define PIC32_UART_STA_URXDA BIT(0)
126 /* pic32_sport pointer for console use */
127 static struct pic32_sport *pic32_sports[PIC32_MAX_UARTS];
129 static inline void pic32_wait_deplete_txbuf(struct pic32_sport *sport)
131 /* wait for tx empty, otherwise chars will be lost or corrupted */
132 while (!(pic32_uart_readl(sport, PIC32_UART_STA) & PIC32_UART_STA_TRMT))
136 /* serial core request to check if uart tx buffer is empty */
137 static unsigned int pic32_uart_tx_empty(struct uart_port *port)
139 struct pic32_sport *sport = to_pic32_sport(port);
140 u32 val = pic32_uart_readl(sport, PIC32_UART_STA);
142 return (val & PIC32_UART_STA_TRMT) ? 1 : 0;
145 /* serial core request to set UART outputs */
146 static void pic32_uart_set_mctrl(struct uart_port *port, unsigned int mctrl)
148 struct pic32_sport *sport = to_pic32_sport(port);
150 /* set loopback mode */
151 if (mctrl & TIOCM_LOOP)
152 pic32_uart_writel(sport, PIC32_SET(PIC32_UART_MODE),
153 PIC32_UART_MODE_LPBK);
155 pic32_uart_writel(sport, PIC32_CLR(PIC32_UART_MODE),
156 PIC32_UART_MODE_LPBK);
159 /* serial core request to return the state of misc UART input pins */
160 static unsigned int pic32_uart_get_mctrl(struct uart_port *port)
162 struct pic32_sport *sport = to_pic32_sport(port);
163 unsigned int mctrl = 0;
165 /* get the state of CTS input pin for this port */
166 if (!sport->cts_gpiod)
168 else if (gpiod_get_value(sport->cts_gpiod))
171 /* DSR and CD are not supported in PIC32, so return 1
172 * RI is not supported in PIC32, so return 0
180 /* stop tx and start tx are not called in pairs, therefore a flag indicates
181 * the status of irq to control the irq-depth.
183 static inline void pic32_uart_irqtxen(struct pic32_sport *sport, u8 en)
185 if (en && !sport->enable_tx_irq) {
186 enable_irq(sport->irq_tx);
187 sport->enable_tx_irq = true;
188 } else if (!en && sport->enable_tx_irq) {
189 /* use disable_irq_nosync() and not disable_irq() to avoid self
190 * imposed deadlock by not waiting for irq handler to end,
191 * since this callback is called from interrupt context.
193 disable_irq_nosync(sport->irq_tx);
194 sport->enable_tx_irq = false;
198 /* serial core request to disable tx ASAP (used for flow control) */
199 static void pic32_uart_stop_tx(struct uart_port *port)
201 struct pic32_sport *sport = to_pic32_sport(port);
203 if (!(pic32_uart_readl(sport, PIC32_UART_MODE) & PIC32_UART_MODE_ON))
206 if (!(pic32_uart_readl(sport, PIC32_UART_STA) & PIC32_UART_STA_UTXEN))
209 /* wait for tx empty */
210 pic32_wait_deplete_txbuf(sport);
212 pic32_uart_writel(sport, PIC32_CLR(PIC32_UART_STA),
213 PIC32_UART_STA_UTXEN);
214 pic32_uart_irqtxen(sport, 0);
217 /* serial core request to (re)enable tx */
218 static void pic32_uart_start_tx(struct uart_port *port)
220 struct pic32_sport *sport = to_pic32_sport(port);
222 pic32_uart_irqtxen(sport, 1);
223 pic32_uart_writel(sport, PIC32_SET(PIC32_UART_STA),
224 PIC32_UART_STA_UTXEN);
227 /* serial core request to stop rx, called before port shutdown */
228 static void pic32_uart_stop_rx(struct uart_port *port)
230 struct pic32_sport *sport = to_pic32_sport(port);
232 /* disable rx interrupts */
233 disable_irq(sport->irq_rx);
235 /* receiver Enable bit OFF */
236 pic32_uart_writel(sport, PIC32_CLR(PIC32_UART_STA),
237 PIC32_UART_STA_URXEN);
240 /* serial core request to start/stop emitting break char */
241 static void pic32_uart_break_ctl(struct uart_port *port, int ctl)
243 struct pic32_sport *sport = to_pic32_sport(port);
246 uart_port_lock_irqsave(port, &flags);
249 pic32_uart_writel(sport, PIC32_SET(PIC32_UART_STA),
250 PIC32_UART_STA_UTXBRK);
252 pic32_uart_writel(sport, PIC32_CLR(PIC32_UART_STA),
253 PIC32_UART_STA_UTXBRK);
255 uart_port_unlock_irqrestore(port, flags);
258 /* get port type in string format */
259 static const char *pic32_uart_type(struct uart_port *port)
261 return (port->type == PORT_PIC32) ? PIC32_DEV_NAME : NULL;
264 /* read all chars in rx fifo and send them to core */
265 static void pic32_uart_do_rx(struct uart_port *port)
267 struct pic32_sport *sport = to_pic32_sport(port);
268 struct tty_port *tty;
269 unsigned int max_count;
271 /* limit number of char read in interrupt, should not be
272 * higher than fifo size anyway since we're much faster than
275 max_count = PIC32_UART_RX_FIFO_DEPTH;
277 uart_port_lock(port);
279 tty = &port->state->port;
285 /* get overrun/fifo empty information from status register */
286 sta_reg = pic32_uart_readl(sport, PIC32_UART_STA);
287 if (unlikely(sta_reg & PIC32_UART_STA_OERR)) {
289 /* fifo reset is required to clear interrupt */
290 pic32_uart_writel(sport, PIC32_CLR(PIC32_UART_STA),
291 PIC32_UART_STA_OERR);
293 port->icount.overrun++;
294 tty_insert_flip_char(tty, 0, TTY_OVERRUN);
297 /* Can at least one more character can be read? */
298 if (!(sta_reg & PIC32_UART_STA_URXDA))
301 /* read the character and increment the rx counter */
302 c = pic32_uart_readl(sport, PIC32_UART_RX);
308 if (unlikely((sta_reg & PIC32_UART_STA_PERR) ||
309 (sta_reg & PIC32_UART_STA_FERR))) {
312 if (sta_reg & PIC32_UART_STA_PERR)
313 port->icount.parity++;
314 if (sta_reg & PIC32_UART_STA_FERR)
315 port->icount.frame++;
317 /* update flag wrt read_status_mask */
318 sta_reg &= port->read_status_mask;
320 if (sta_reg & PIC32_UART_STA_FERR)
322 if (sta_reg & PIC32_UART_STA_PERR)
326 if (uart_handle_sysrq_char(port, c))
329 if ((sta_reg & port->ignore_status_mask) == 0)
330 tty_insert_flip_char(tty, c, flag);
332 } while (--max_count);
334 uart_port_unlock(port);
336 tty_flip_buffer_push(tty);
339 /* fill tx fifo with chars to send, stop when fifo is about to be full
340 * or when all chars have been sent.
342 static void pic32_uart_do_tx(struct uart_port *port)
344 struct pic32_sport *sport = to_pic32_sport(port);
345 struct tty_port *tport = &port->state->port;
346 unsigned int max_count = PIC32_UART_TX_FIFO_DEPTH;
349 pic32_uart_writel(sport, PIC32_UART_TX, port->x_char);
355 if (uart_tx_stopped(port)) {
356 pic32_uart_stop_tx(port);
360 if (kfifo_is_empty(&tport->xmit_fifo))
363 /* keep stuffing chars into uart tx buffer
364 * 1) until uart fifo is full
366 * 2) until the circ buffer is empty
367 * (all chars have been sent)
369 * 3) until the max count is reached
370 * (prevents lingering here for too long in certain cases)
372 while (!(PIC32_UART_STA_UTXBF &
373 pic32_uart_readl(sport, PIC32_UART_STA))) {
376 if (!uart_fifo_get(port, &c))
378 pic32_uart_writel(sport, PIC32_UART_TX, c);
380 if (--max_count == 0)
384 if (kfifo_len(&tport->xmit_fifo) < WAKEUP_CHARS)
385 uart_write_wakeup(port);
387 if (kfifo_is_empty(&tport->xmit_fifo))
393 pic32_uart_irqtxen(sport, 0);
396 /* RX interrupt handler */
397 static irqreturn_t pic32_uart_rx_interrupt(int irq, void *dev_id)
399 struct uart_port *port = dev_id;
401 pic32_uart_do_rx(port);
406 /* TX interrupt handler */
407 static irqreturn_t pic32_uart_tx_interrupt(int irq, void *dev_id)
409 struct uart_port *port = dev_id;
412 uart_port_lock_irqsave(port, &flags);
413 pic32_uart_do_tx(port);
414 uart_port_unlock_irqrestore(port, flags);
419 /* FAULT interrupt handler */
420 static irqreturn_t pic32_uart_fault_interrupt(int irq, void *dev_id)
422 /* do nothing: pic32_uart_do_rx() handles faults. */
426 /* enable rx & tx operation on uart */
427 static void pic32_uart_en_and_unmask(struct uart_port *port)
429 struct pic32_sport *sport = to_pic32_sport(port);
431 pic32_uart_writel(sport, PIC32_SET(PIC32_UART_STA),
432 PIC32_UART_STA_UTXEN | PIC32_UART_STA_URXEN);
433 pic32_uart_writel(sport, PIC32_SET(PIC32_UART_MODE),
437 /* disable rx & tx operation on uart */
438 static void pic32_uart_dsbl_and_mask(struct uart_port *port)
440 struct pic32_sport *sport = to_pic32_sport(port);
442 /* wait for tx empty, otherwise chars will be lost or corrupted */
443 pic32_wait_deplete_txbuf(sport);
445 pic32_uart_writel(sport, PIC32_CLR(PIC32_UART_STA),
446 PIC32_UART_STA_UTXEN | PIC32_UART_STA_URXEN);
447 pic32_uart_writel(sport, PIC32_CLR(PIC32_UART_MODE),
451 /* serial core request to initialize uart and start rx operation */
452 static int pic32_uart_startup(struct uart_port *port)
454 struct pic32_sport *sport = to_pic32_sport(port);
455 u32 dflt_baud = (port->uartclk / PIC32_UART_DFLT_BRATE / 16) - 1;
459 local_irq_save(flags);
461 ret = clk_prepare_enable(sport->clk);
463 local_irq_restore(flags);
467 /* clear status and mode registers */
468 pic32_uart_writel(sport, PIC32_UART_MODE, 0);
469 pic32_uart_writel(sport, PIC32_UART_STA, 0);
471 /* disable uart and mask all interrupts */
472 pic32_uart_dsbl_and_mask(port);
474 /* set default baud */
475 pic32_uart_writel(sport, PIC32_UART_BRG, dflt_baud);
477 local_irq_restore(flags);
479 /* Each UART of a PIC32 has three interrupts therefore,
480 * we setup driver to register the 3 irqs for the device.
482 * For each irq request_irq() is called with interrupt disabled.
483 * And the irq is enabled as soon as we are ready to handle them.
485 sport->enable_tx_irq = false;
487 sport->irq_fault_name = kasprintf(GFP_KERNEL, "%s%d-fault",
488 pic32_uart_type(port),
490 if (!sport->irq_fault_name) {
491 dev_err(port->dev, "%s: kasprintf err!", __func__);
493 goto out_disable_clk;
495 irq_set_status_flags(sport->irq_fault, IRQ_NOAUTOEN);
496 ret = request_irq(sport->irq_fault, pic32_uart_fault_interrupt,
497 IRQF_NO_THREAD, sport->irq_fault_name, port);
499 dev_err(port->dev, "%s: request irq(%d) err! ret:%d name:%s\n",
500 __func__, sport->irq_fault, ret,
501 pic32_uart_type(port));
505 sport->irq_rx_name = kasprintf(GFP_KERNEL, "%s%d-rx",
506 pic32_uart_type(port),
508 if (!sport->irq_rx_name) {
509 dev_err(port->dev, "%s: kasprintf err!", __func__);
513 irq_set_status_flags(sport->irq_rx, IRQ_NOAUTOEN);
514 ret = request_irq(sport->irq_rx, pic32_uart_rx_interrupt,
515 IRQF_NO_THREAD, sport->irq_rx_name, port);
517 dev_err(port->dev, "%s: request irq(%d) err! ret:%d name:%s\n",
518 __func__, sport->irq_rx, ret,
519 pic32_uart_type(port));
523 sport->irq_tx_name = kasprintf(GFP_KERNEL, "%s%d-tx",
524 pic32_uart_type(port),
526 if (!sport->irq_tx_name) {
527 dev_err(port->dev, "%s: kasprintf err!", __func__);
531 irq_set_status_flags(sport->irq_tx, IRQ_NOAUTOEN);
532 ret = request_irq(sport->irq_tx, pic32_uart_tx_interrupt,
533 IRQF_NO_THREAD, sport->irq_tx_name, port);
535 dev_err(port->dev, "%s: request irq(%d) err! ret:%d name:%s\n",
536 __func__, sport->irq_tx, ret,
537 pic32_uart_type(port));
541 local_irq_save(flags);
543 /* set rx interrupt on first receive */
544 pic32_uart_writel(sport, PIC32_CLR(PIC32_UART_STA),
545 PIC32_UART_STA_URXISEL1 | PIC32_UART_STA_URXISEL0);
547 /* set interrupt on empty */
548 pic32_uart_writel(sport, PIC32_CLR(PIC32_UART_STA),
549 PIC32_UART_STA_UTXISEL1);
551 /* enable all interrupts and eanable uart */
552 pic32_uart_en_and_unmask(port);
554 local_irq_restore(flags);
556 enable_irq(sport->irq_rx);
561 free_irq(sport->irq_tx, port);
562 kfree(sport->irq_tx_name);
564 free_irq(sport->irq_rx, port);
565 kfree(sport->irq_rx_name);
567 free_irq(sport->irq_fault, port);
568 kfree(sport->irq_fault_name);
570 clk_disable_unprepare(sport->clk);
575 /* serial core request to flush & disable uart */
576 static void pic32_uart_shutdown(struct uart_port *port)
578 struct pic32_sport *sport = to_pic32_sport(port);
582 uart_port_lock_irqsave(port, &flags);
583 pic32_uart_dsbl_and_mask(port);
584 uart_port_unlock_irqrestore(port, flags);
585 clk_disable_unprepare(sport->clk);
587 /* free all 3 interrupts for this UART */
588 free_irq(sport->irq_fault, port);
589 kfree(sport->irq_fault_name);
590 free_irq(sport->irq_tx, port);
591 kfree(sport->irq_tx_name);
592 free_irq(sport->irq_rx, port);
593 kfree(sport->irq_rx_name);
596 /* serial core request to change current uart setting */
597 static void pic32_uart_set_termios(struct uart_port *port,
598 struct ktermios *new,
599 const struct ktermios *old)
601 struct pic32_sport *sport = to_pic32_sport(port);
606 uart_port_lock_irqsave(port, &flags);
608 /* disable uart and mask all interrupts while changing speed */
609 pic32_uart_dsbl_and_mask(port);
611 /* stop bit options */
612 if (new->c_cflag & CSTOPB)
613 pic32_uart_writel(sport, PIC32_SET(PIC32_UART_MODE),
614 PIC32_UART_MODE_STSEL);
616 pic32_uart_writel(sport, PIC32_CLR(PIC32_UART_MODE),
617 PIC32_UART_MODE_STSEL);
620 if (new->c_cflag & PARENB) {
621 if (new->c_cflag & PARODD) {
622 pic32_uart_writel(sport, PIC32_SET(PIC32_UART_MODE),
623 PIC32_UART_MODE_PDSEL1);
624 pic32_uart_writel(sport, PIC32_CLR(PIC32_UART_MODE),
625 PIC32_UART_MODE_PDSEL0);
627 pic32_uart_writel(sport, PIC32_SET(PIC32_UART_MODE),
628 PIC32_UART_MODE_PDSEL0);
629 pic32_uart_writel(sport, PIC32_CLR(PIC32_UART_MODE),
630 PIC32_UART_MODE_PDSEL1);
633 pic32_uart_writel(sport, PIC32_CLR(PIC32_UART_MODE),
634 PIC32_UART_MODE_PDSEL1 |
635 PIC32_UART_MODE_PDSEL0);
637 /* if hw flow ctrl, then the pins must be specified in device tree */
638 if ((new->c_cflag & CRTSCTS) && sport->cts_gpiod) {
639 /* enable hardware flow control */
640 pic32_uart_writel(sport, PIC32_SET(PIC32_UART_MODE),
641 PIC32_UART_MODE_UEN1);
642 pic32_uart_writel(sport, PIC32_CLR(PIC32_UART_MODE),
643 PIC32_UART_MODE_UEN0);
644 pic32_uart_writel(sport, PIC32_CLR(PIC32_UART_MODE),
645 PIC32_UART_MODE_RTSMD);
647 /* disable hardware flow control */
648 pic32_uart_writel(sport, PIC32_CLR(PIC32_UART_MODE),
649 PIC32_UART_MODE_UEN1);
650 pic32_uart_writel(sport, PIC32_CLR(PIC32_UART_MODE),
651 PIC32_UART_MODE_UEN0);
652 pic32_uart_writel(sport, PIC32_CLR(PIC32_UART_MODE),
653 PIC32_UART_MODE_RTSMD);
659 /* Mark/Space parity is not supported */
660 new->c_cflag &= ~CMSPAR;
663 baud = uart_get_baud_rate(port, new, old, 0, port->uartclk / 16);
664 quot = uart_get_divisor(port, baud) - 1;
665 pic32_uart_writel(sport, PIC32_UART_BRG, quot);
666 uart_update_timeout(port, new->c_cflag, baud);
668 if (tty_termios_baud_rate(new))
669 tty_termios_encode_baud_rate(new, baud, baud);
672 pic32_uart_en_and_unmask(port);
674 uart_port_unlock_irqrestore(port, flags);
677 /* serial core request to claim uart iomem */
678 static int pic32_uart_request_port(struct uart_port *port)
680 struct platform_device *pdev = to_platform_device(port->dev);
681 struct resource *res_mem;
683 res_mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
684 if (unlikely(!res_mem))
687 if (!request_mem_region(port->mapbase, resource_size(res_mem),
691 port->membase = devm_ioremap(port->dev, port->mapbase,
692 resource_size(res_mem));
693 if (!port->membase) {
694 dev_err(port->dev, "Unable to map registers\n");
695 release_mem_region(port->mapbase, resource_size(res_mem));
702 /* serial core request to release uart iomem */
703 static void pic32_uart_release_port(struct uart_port *port)
705 struct platform_device *pdev = to_platform_device(port->dev);
706 struct resource *res_mem;
707 unsigned int res_size;
709 res_mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
710 if (unlikely(!res_mem))
712 res_size = resource_size(res_mem);
714 release_mem_region(port->mapbase, res_size);
717 /* serial core request to do any port required auto-configuration */
718 static void pic32_uart_config_port(struct uart_port *port, int flags)
720 if (flags & UART_CONFIG_TYPE) {
721 if (pic32_uart_request_port(port))
723 port->type = PORT_PIC32;
727 /* serial core request to check that port information in serinfo are suitable */
728 static int pic32_uart_verify_port(struct uart_port *port,
729 struct serial_struct *serinfo)
731 if (port->type != PORT_PIC32)
733 if (port->irq != serinfo->irq)
735 if (port->iotype != serinfo->io_type)
737 if (port->mapbase != (unsigned long)serinfo->iomem_base)
743 /* serial core callbacks */
744 static const struct uart_ops pic32_uart_ops = {
745 .tx_empty = pic32_uart_tx_empty,
746 .get_mctrl = pic32_uart_get_mctrl,
747 .set_mctrl = pic32_uart_set_mctrl,
748 .start_tx = pic32_uart_start_tx,
749 .stop_tx = pic32_uart_stop_tx,
750 .stop_rx = pic32_uart_stop_rx,
751 .break_ctl = pic32_uart_break_ctl,
752 .startup = pic32_uart_startup,
753 .shutdown = pic32_uart_shutdown,
754 .set_termios = pic32_uart_set_termios,
755 .type = pic32_uart_type,
756 .release_port = pic32_uart_release_port,
757 .request_port = pic32_uart_request_port,
758 .config_port = pic32_uart_config_port,
759 .verify_port = pic32_uart_verify_port,
762 #ifdef CONFIG_SERIAL_PIC32_CONSOLE
763 /* output given char */
764 static void pic32_console_putchar(struct uart_port *port, unsigned char ch)
766 struct pic32_sport *sport = to_pic32_sport(port);
768 if (!(pic32_uart_readl(sport, PIC32_UART_MODE) & PIC32_UART_MODE_ON))
771 if (!(pic32_uart_readl(sport, PIC32_UART_STA) & PIC32_UART_STA_UTXEN))
774 /* wait for tx empty */
775 pic32_wait_deplete_txbuf(sport);
777 pic32_uart_writel(sport, PIC32_UART_TX, ch & 0xff);
780 /* console core request to output given string */
781 static void pic32_console_write(struct console *co, const char *s,
784 struct pic32_sport *sport = pic32_sports[co->index];
786 /* call uart helper to deal with \r\n */
787 uart_console_write(&sport->port, s, count, pic32_console_putchar);
790 /* console core request to setup given console, find matching uart
793 static int pic32_console_setup(struct console *co, char *options)
795 struct pic32_sport *sport;
802 if (unlikely(co->index < 0 || co->index >= PIC32_MAX_UARTS))
805 sport = pic32_sports[co->index];
809 ret = clk_prepare_enable(sport->clk);
814 uart_parse_options(options, &baud, &parity, &bits, &flow);
816 return uart_set_options(&sport->port, co, baud, parity, bits, flow);
819 static struct uart_driver pic32_uart_driver;
820 static struct console pic32_console = {
821 .name = PIC32_SDEV_NAME,
822 .write = pic32_console_write,
823 .device = uart_console_device,
824 .setup = pic32_console_setup,
825 .flags = CON_PRINTBUFFER,
827 .data = &pic32_uart_driver,
829 #define PIC32_SCONSOLE (&pic32_console)
831 static int __init pic32_console_init(void)
833 register_console(&pic32_console);
836 console_initcall(pic32_console_init);
839 * Late console initialization.
841 static int __init pic32_late_console_init(void)
843 if (!console_is_registered(&pic32_console))
844 register_console(&pic32_console);
849 core_initcall(pic32_late_console_init);
852 #define PIC32_SCONSOLE NULL
855 static struct uart_driver pic32_uart_driver = {
856 .owner = THIS_MODULE,
857 .driver_name = PIC32_DEV_NAME,
858 .dev_name = PIC32_SDEV_NAME,
859 .nr = PIC32_MAX_UARTS,
860 .cons = PIC32_SCONSOLE,
863 static int pic32_uart_probe(struct platform_device *pdev)
865 struct device *dev = &pdev->dev;
866 struct device_node *np = dev->of_node;
867 struct pic32_sport *sport;
869 struct resource *res_mem;
870 struct uart_port *port;
873 uart_idx = of_alias_get_id(np, "serial");
874 if (uart_idx < 0 || uart_idx >= PIC32_MAX_UARTS)
877 res_mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
881 sport = devm_kzalloc(&pdev->dev, sizeof(*sport), GFP_KERNEL);
885 sport->idx = uart_idx;
886 sport->irq_fault = irq_of_parse_and_map(np, 0);
887 sport->irq_rx = irq_of_parse_and_map(np, 1);
888 sport->irq_tx = irq_of_parse_and_map(np, 2);
889 sport->clk = devm_clk_get(&pdev->dev, NULL);
890 if (IS_ERR(sport->clk))
891 return PTR_ERR(sport->clk);
892 sport->dev = &pdev->dev;
894 /* Hardware flow control: gpios
895 * !Note: Basically, CTS is needed for reading the status.
897 sport->cts_gpiod = devm_gpiod_get_optional(dev, "cts", GPIOD_IN);
898 if (IS_ERR(sport->cts_gpiod))
899 return dev_err_probe(dev, PTR_ERR(sport->cts_gpiod), "error requesting CTS GPIO\n");
900 gpiod_set_consumer_name(sport->cts_gpiod, "CTS");
902 pic32_sports[uart_idx] = sport;
904 port->iotype = UPIO_MEM;
905 port->mapbase = res_mem->start;
906 port->ops = &pic32_uart_ops;
907 port->flags = UPF_BOOT_AUTOCONF;
908 port->dev = &pdev->dev;
909 port->fifosize = PIC32_UART_TX_FIFO_DEPTH;
910 port->uartclk = clk_get_rate(sport->clk);
911 port->line = uart_idx;
913 ret = uart_add_one_port(&pic32_uart_driver, port);
915 port->membase = NULL;
916 dev_err(port->dev, "%s: uart add port error!\n", __func__);
920 #ifdef CONFIG_SERIAL_PIC32_CONSOLE
921 if (uart_console_registered(port)) {
922 /* The peripheral clock has been enabled by console_setup,
923 * so disable it till the port is used.
925 clk_disable_unprepare(sport->clk);
929 platform_set_drvdata(pdev, port);
931 dev_info(&pdev->dev, "%s: uart(%d) driver initialized.\n",
936 /* automatic unroll of sport and gpios */
940 static void pic32_uart_remove(struct platform_device *pdev)
942 struct uart_port *port = platform_get_drvdata(pdev);
943 struct pic32_sport *sport = to_pic32_sport(port);
945 uart_remove_one_port(&pic32_uart_driver, port);
946 clk_disable_unprepare(sport->clk);
947 platform_set_drvdata(pdev, NULL);
948 pic32_sports[sport->idx] = NULL;
951 static const struct of_device_id pic32_serial_dt_ids[] = {
952 { .compatible = "microchip,pic32mzda-uart" },
955 MODULE_DEVICE_TABLE(of, pic32_serial_dt_ids);
957 static struct platform_driver pic32_uart_platform_driver = {
958 .probe = pic32_uart_probe,
959 .remove = pic32_uart_remove,
961 .name = PIC32_DEV_NAME,
962 .of_match_table = of_match_ptr(pic32_serial_dt_ids),
963 .suppress_bind_attrs = IS_BUILTIN(CONFIG_SERIAL_PIC32),
967 static int __init pic32_uart_init(void)
971 ret = uart_register_driver(&pic32_uart_driver);
973 pr_err("failed to register %s:%d\n",
974 pic32_uart_driver.driver_name, ret);
978 ret = platform_driver_register(&pic32_uart_platform_driver);
980 pr_err("fail to register pic32 uart\n");
981 uart_unregister_driver(&pic32_uart_driver);
986 arch_initcall(pic32_uart_init);
988 static void __exit pic32_uart_exit(void)
990 #ifdef CONFIG_SERIAL_PIC32_CONSOLE
991 unregister_console(&pic32_console);
993 platform_driver_unregister(&pic32_uart_platform_driver);
994 uart_unregister_driver(&pic32_uart_driver);
996 module_exit(pic32_uart_exit);
999 MODULE_DESCRIPTION("Microchip PIC32 integrated serial port driver");
1000 MODULE_LICENSE("GPL v2");