2 * PIC32 Integrated Serial Driver.
4 * Copyright (C) 2015 Microchip Technology, Inc.
7 * Sorin-Andrei Pistirica <andrei.pistirica@microchip.com>
9 * Licensed under GPLv2 or later.
12 #include <linux/kernel.h>
13 #include <linux/platform_device.h>
15 #include <linux/of_device.h>
16 #include <linux/of_irq.h>
17 #include <linux/of_gpio.h>
18 #include <linux/init.h>
19 #include <linux/module.h>
20 #include <linux/slab.h>
21 #include <linux/console.h>
22 #include <linux/clk.h>
23 #include <linux/tty.h>
24 #include <linux/tty_flip.h>
25 #include <linux/serial_core.h>
26 #include <linux/delay.h>
28 #include <asm/mach-pic32/pic32.h>
29 #include "pic32_uart.h"
31 /* UART name and device definitions */
32 #define PIC32_DEV_NAME "pic32-uart"
33 #define PIC32_MAX_UARTS 6
34 #define PIC32_SDEV_NAME "ttyPIC"
36 /* pic32_sport pointer for console use */
37 static struct pic32_sport
*pic32_sports
[PIC32_MAX_UARTS
];
39 static inline void pic32_wait_deplete_txbuf(struct pic32_sport
*sport
)
41 /* wait for tx empty, otherwise chars will be lost or corrupted */
42 while (!(pic32_uart_readl(sport
, PIC32_UART_STA
) & PIC32_UART_STA_TRMT
))
46 static inline int pic32_enable_clock(struct pic32_sport
*sport
)
48 int ret
= clk_prepare_enable(sport
->clk
);
57 static inline void pic32_disable_clock(struct pic32_sport
*sport
)
60 clk_disable_unprepare(sport
->clk
);
63 /* serial core request to check if uart tx buffer is empty */
64 static unsigned int pic32_uart_tx_empty(struct uart_port
*port
)
66 struct pic32_sport
*sport
= to_pic32_sport(port
);
67 u32 val
= pic32_uart_readl(sport
, PIC32_UART_STA
);
69 return (val
& PIC32_UART_STA_TRMT
) ? 1 : 0;
72 /* serial core request to set UART outputs */
73 static void pic32_uart_set_mctrl(struct uart_port
*port
, unsigned int mctrl
)
75 struct pic32_sport
*sport
= to_pic32_sport(port
);
77 /* set loopback mode */
78 if (mctrl
& TIOCM_LOOP
)
79 pic32_uart_writel(sport
, PIC32_SET(PIC32_UART_MODE
),
80 PIC32_UART_MODE_LPBK
);
82 pic32_uart_writel(sport
, PIC32_CLR(PIC32_UART_MODE
),
83 PIC32_UART_MODE_LPBK
);
86 /* get the state of CTS input pin for this port */
87 static unsigned int get_cts_state(struct pic32_sport
*sport
)
89 /* read and invert UxCTS */
90 if (gpio_is_valid(sport
->cts_gpio
))
91 return !gpio_get_value(sport
->cts_gpio
);
96 /* serial core request to return the state of misc UART input pins */
97 static unsigned int pic32_uart_get_mctrl(struct uart_port
*port
)
99 struct pic32_sport
*sport
= to_pic32_sport(port
);
100 unsigned int mctrl
= 0;
102 if (!sport
->hw_flow_ctrl
)
104 else if (get_cts_state(sport
))
107 /* DSR and CD are not supported in PIC32, so return 1
108 * RI is not supported in PIC32, so return 0
116 /* stop tx and start tx are not called in pairs, therefore a flag indicates
117 * the status of irq to control the irq-depth.
119 static inline void pic32_uart_irqtxen(struct pic32_sport
*sport
, u8 en
)
121 if (en
&& !tx_irq_enabled(sport
)) {
122 enable_irq(sport
->irq_tx
);
123 tx_irq_enabled(sport
) = 1;
124 } else if (!en
&& tx_irq_enabled(sport
)) {
125 /* use disable_irq_nosync() and not disable_irq() to avoid self
126 * imposed deadlock by not waiting for irq handler to end,
127 * since this callback is called from interrupt context.
129 disable_irq_nosync(sport
->irq_tx
);
130 tx_irq_enabled(sport
) = 0;
134 /* serial core request to disable tx ASAP (used for flow control) */
135 static void pic32_uart_stop_tx(struct uart_port
*port
)
137 struct pic32_sport
*sport
= to_pic32_sport(port
);
139 if (!(pic32_uart_readl(sport
, PIC32_UART_MODE
) & PIC32_UART_MODE_ON
))
142 if (!(pic32_uart_readl(sport
, PIC32_UART_STA
) & PIC32_UART_STA_UTXEN
))
145 /* wait for tx empty */
146 pic32_wait_deplete_txbuf(sport
);
148 pic32_uart_writel(sport
, PIC32_CLR(PIC32_UART_STA
),
149 PIC32_UART_STA_UTXEN
);
150 pic32_uart_irqtxen(sport
, 0);
153 /* serial core request to (re)enable tx */
154 static void pic32_uart_start_tx(struct uart_port
*port
)
156 struct pic32_sport
*sport
= to_pic32_sport(port
);
158 pic32_uart_irqtxen(sport
, 1);
159 pic32_uart_writel(sport
, PIC32_SET(PIC32_UART_STA
),
160 PIC32_UART_STA_UTXEN
);
163 /* serial core request to stop rx, called before port shutdown */
164 static void pic32_uart_stop_rx(struct uart_port
*port
)
166 struct pic32_sport
*sport
= to_pic32_sport(port
);
168 /* disable rx interrupts */
169 disable_irq(sport
->irq_rx
);
171 /* receiver Enable bit OFF */
172 pic32_uart_writel(sport
, PIC32_CLR(PIC32_UART_STA
),
173 PIC32_UART_STA_URXEN
);
176 /* serial core request to start/stop emitting break char */
177 static void pic32_uart_break_ctl(struct uart_port
*port
, int ctl
)
179 struct pic32_sport
*sport
= to_pic32_sport(port
);
182 spin_lock_irqsave(&port
->lock
, flags
);
185 pic32_uart_writel(sport
, PIC32_SET(PIC32_UART_STA
),
186 PIC32_UART_STA_UTXBRK
);
188 pic32_uart_writel(sport
, PIC32_CLR(PIC32_UART_STA
),
189 PIC32_UART_STA_UTXBRK
);
191 spin_unlock_irqrestore(&port
->lock
, flags
);
194 /* get port type in string format */
195 static const char *pic32_uart_type(struct uart_port
*port
)
197 return (port
->type
== PORT_PIC32
) ? PIC32_DEV_NAME
: NULL
;
200 /* read all chars in rx fifo and send them to core */
201 static void pic32_uart_do_rx(struct uart_port
*port
)
203 struct pic32_sport
*sport
= to_pic32_sport(port
);
204 struct tty_port
*tty
;
205 unsigned int max_count
;
207 /* limit number of char read in interrupt, should not be
208 * higher than fifo size anyway since we're much faster than
211 max_count
= PIC32_UART_RX_FIFO_DEPTH
;
213 spin_lock(&port
->lock
);
215 tty
= &port
->state
->port
;
221 /* get overrun/fifo empty information from status register */
222 sta_reg
= pic32_uart_readl(sport
, PIC32_UART_STA
);
223 if (unlikely(sta_reg
& PIC32_UART_STA_OERR
)) {
225 /* fifo reset is required to clear interrupt */
226 pic32_uart_writel(sport
, PIC32_CLR(PIC32_UART_STA
),
227 PIC32_UART_STA_OERR
);
229 port
->icount
.overrun
++;
230 tty_insert_flip_char(tty
, 0, TTY_OVERRUN
);
233 /* Can at least one more character can be read? */
234 if (!(sta_reg
& PIC32_UART_STA_URXDA
))
237 /* read the character and increment the rx counter */
238 c
= pic32_uart_readl(sport
, PIC32_UART_RX
);
244 if (unlikely((sta_reg
& PIC32_UART_STA_PERR
) ||
245 (sta_reg
& PIC32_UART_STA_FERR
))) {
248 if (sta_reg
& PIC32_UART_STA_PERR
)
249 port
->icount
.parity
++;
250 if (sta_reg
& PIC32_UART_STA_FERR
)
251 port
->icount
.frame
++;
253 /* update flag wrt read_status_mask */
254 sta_reg
&= port
->read_status_mask
;
256 if (sta_reg
& PIC32_UART_STA_FERR
)
258 if (sta_reg
& PIC32_UART_STA_PERR
)
262 if (uart_handle_sysrq_char(port
, c
))
265 if ((sta_reg
& port
->ignore_status_mask
) == 0)
266 tty_insert_flip_char(tty
, c
, flag
);
268 } while (--max_count
);
270 spin_unlock(&port
->lock
);
272 tty_flip_buffer_push(tty
);
275 /* fill tx fifo with chars to send, stop when fifo is about to be full
276 * or when all chars have been sent.
278 static void pic32_uart_do_tx(struct uart_port
*port
)
280 struct pic32_sport
*sport
= to_pic32_sport(port
);
281 struct circ_buf
*xmit
= &port
->state
->xmit
;
282 unsigned int max_count
= PIC32_UART_TX_FIFO_DEPTH
;
285 pic32_uart_writel(sport
, PIC32_UART_TX
, port
->x_char
);
291 if (uart_tx_stopped(port
)) {
292 pic32_uart_stop_tx(port
);
296 if (uart_circ_empty(xmit
))
299 /* keep stuffing chars into uart tx buffer
300 * 1) until uart fifo is full
302 * 2) until the circ buffer is empty
303 * (all chars have been sent)
305 * 3) until the max count is reached
306 * (prevents lingering here for too long in certain cases)
308 while (!(PIC32_UART_STA_UTXBF
&
309 pic32_uart_readl(sport
, PIC32_UART_STA
))) {
310 unsigned int c
= xmit
->buf
[xmit
->tail
];
312 pic32_uart_writel(sport
, PIC32_UART_TX
, c
);
314 xmit
->tail
= (xmit
->tail
+ 1) & (UART_XMIT_SIZE
- 1);
316 if (uart_circ_empty(xmit
))
318 if (--max_count
== 0)
322 if (uart_circ_chars_pending(xmit
) < WAKEUP_CHARS
)
323 uart_write_wakeup(port
);
325 if (uart_circ_empty(xmit
))
331 pic32_uart_irqtxen(sport
, 0);
334 /* RX interrupt handler */
335 static irqreturn_t
pic32_uart_rx_interrupt(int irq
, void *dev_id
)
337 struct uart_port
*port
= dev_id
;
339 pic32_uart_do_rx(port
);
344 /* TX interrupt handler */
345 static irqreturn_t
pic32_uart_tx_interrupt(int irq
, void *dev_id
)
347 struct uart_port
*port
= dev_id
;
350 spin_lock_irqsave(&port
->lock
, flags
);
351 pic32_uart_do_tx(port
);
352 spin_unlock_irqrestore(&port
->lock
, flags
);
357 /* FAULT interrupt handler */
358 static irqreturn_t
pic32_uart_fault_interrupt(int irq
, void *dev_id
)
360 /* do nothing: pic32_uart_do_rx() handles faults. */
364 /* enable rx & tx operation on uart */
365 static void pic32_uart_en_and_unmask(struct uart_port
*port
)
367 struct pic32_sport
*sport
= to_pic32_sport(port
);
369 pic32_uart_writel(sport
, PIC32_SET(PIC32_UART_STA
),
370 PIC32_UART_STA_UTXEN
| PIC32_UART_STA_URXEN
);
371 pic32_uart_writel(sport
, PIC32_SET(PIC32_UART_MODE
),
375 /* disable rx & tx operation on uart */
376 static void pic32_uart_dsbl_and_mask(struct uart_port
*port
)
378 struct pic32_sport
*sport
= to_pic32_sport(port
);
380 /* wait for tx empty, otherwise chars will be lost or corrupted */
381 pic32_wait_deplete_txbuf(sport
);
383 pic32_uart_writel(sport
, PIC32_CLR(PIC32_UART_STA
),
384 PIC32_UART_STA_UTXEN
| PIC32_UART_STA_URXEN
);
385 pic32_uart_writel(sport
, PIC32_CLR(PIC32_UART_MODE
),
389 /* serial core request to initialize uart and start rx operation */
390 static int pic32_uart_startup(struct uart_port
*port
)
392 struct pic32_sport
*sport
= to_pic32_sport(port
);
393 u32 dflt_baud
= (port
->uartclk
/ PIC32_UART_DFLT_BRATE
/ 16) - 1;
397 local_irq_save(flags
);
399 ret
= pic32_enable_clock(sport
);
401 local_irq_restore(flags
);
405 /* clear status and mode registers */
406 pic32_uart_writel(sport
, PIC32_UART_MODE
, 0);
407 pic32_uart_writel(sport
, PIC32_UART_STA
, 0);
409 /* disable uart and mask all interrupts */
410 pic32_uart_dsbl_and_mask(port
);
412 /* set default baud */
413 pic32_uart_writel(sport
, PIC32_UART_BRG
, dflt_baud
);
415 local_irq_restore(flags
);
417 /* Each UART of a PIC32 has three interrupts therefore,
418 * we setup driver to register the 3 irqs for the device.
420 * For each irq request_irq() is called with interrupt disabled.
421 * And the irq is enabled as soon as we are ready to handle them.
423 tx_irq_enabled(sport
) = 0;
425 sport
->irq_fault_name
= kasprintf(GFP_KERNEL
, "%s%d-fault",
426 pic32_uart_type(port
),
428 if (!sport
->irq_fault_name
) {
429 dev_err(port
->dev
, "%s: kasprintf err!", __func__
);
433 irq_set_status_flags(sport
->irq_fault
, IRQ_NOAUTOEN
);
434 ret
= request_irq(sport
->irq_fault
, pic32_uart_fault_interrupt
,
435 sport
->irqflags_fault
, sport
->irq_fault_name
, port
);
437 dev_err(port
->dev
, "%s: request irq(%d) err! ret:%d name:%s\n",
438 __func__
, sport
->irq_fault
, ret
,
439 pic32_uart_type(port
));
443 sport
->irq_rx_name
= kasprintf(GFP_KERNEL
, "%s%d-rx",
444 pic32_uart_type(port
),
446 if (!sport
->irq_rx_name
) {
447 dev_err(port
->dev
, "%s: kasprintf err!", __func__
);
451 irq_set_status_flags(sport
->irq_rx
, IRQ_NOAUTOEN
);
452 ret
= request_irq(sport
->irq_rx
, pic32_uart_rx_interrupt
,
453 sport
->irqflags_rx
, sport
->irq_rx_name
, port
);
455 dev_err(port
->dev
, "%s: request irq(%d) err! ret:%d name:%s\n",
456 __func__
, sport
->irq_rx
, ret
,
457 pic32_uart_type(port
));
461 sport
->irq_tx_name
= kasprintf(GFP_KERNEL
, "%s%d-tx",
462 pic32_uart_type(port
),
464 if (!sport
->irq_tx_name
) {
465 dev_err(port
->dev
, "%s: kasprintf err!", __func__
);
469 irq_set_status_flags(sport
->irq_tx
, IRQ_NOAUTOEN
);
470 ret
= request_irq(sport
->irq_tx
, pic32_uart_tx_interrupt
,
471 sport
->irqflags_tx
, sport
->irq_tx_name
, port
);
473 dev_err(port
->dev
, "%s: request irq(%d) err! ret:%d name:%s\n",
474 __func__
, sport
->irq_tx
, ret
,
475 pic32_uart_type(port
));
479 local_irq_save(flags
);
481 /* set rx interrupt on first receive */
482 pic32_uart_writel(sport
, PIC32_CLR(PIC32_UART_STA
),
483 PIC32_UART_STA_URXISEL1
| PIC32_UART_STA_URXISEL0
);
485 /* set interrupt on empty */
486 pic32_uart_writel(sport
, PIC32_CLR(PIC32_UART_STA
),
487 PIC32_UART_STA_UTXISEL1
);
489 /* enable all interrupts and eanable uart */
490 pic32_uart_en_and_unmask(port
);
492 enable_irq(sport
->irq_rx
);
497 kfree(sport
->irq_tx_name
);
498 free_irq(sport
->irq_tx
, sport
);
500 kfree(sport
->irq_rx_name
);
501 free_irq(sport
->irq_rx
, sport
);
503 kfree(sport
->irq_fault_name
);
504 free_irq(sport
->irq_fault
, sport
);
509 /* serial core request to flush & disable uart */
510 static void pic32_uart_shutdown(struct uart_port
*port
)
512 struct pic32_sport
*sport
= to_pic32_sport(port
);
516 spin_lock_irqsave(&port
->lock
, flags
);
517 pic32_uart_dsbl_and_mask(port
);
518 spin_unlock_irqrestore(&port
->lock
, flags
);
519 pic32_disable_clock(sport
);
521 /* free all 3 interrupts for this UART */
522 free_irq(sport
->irq_fault
, port
);
523 free_irq(sport
->irq_tx
, port
);
524 free_irq(sport
->irq_rx
, port
);
527 /* serial core request to change current uart setting */
528 static void pic32_uart_set_termios(struct uart_port
*port
,
529 struct ktermios
*new,
530 struct ktermios
*old
)
532 struct pic32_sport
*sport
= to_pic32_sport(port
);
537 spin_lock_irqsave(&port
->lock
, flags
);
539 /* disable uart and mask all interrupts while changing speed */
540 pic32_uart_dsbl_and_mask(port
);
542 /* stop bit options */
543 if (new->c_cflag
& CSTOPB
)
544 pic32_uart_writel(sport
, PIC32_SET(PIC32_UART_MODE
),
545 PIC32_UART_MODE_STSEL
);
547 pic32_uart_writel(sport
, PIC32_CLR(PIC32_UART_MODE
),
548 PIC32_UART_MODE_STSEL
);
551 if (new->c_cflag
& PARENB
) {
552 if (new->c_cflag
& PARODD
) {
553 pic32_uart_writel(sport
, PIC32_SET(PIC32_UART_MODE
),
554 PIC32_UART_MODE_PDSEL1
);
555 pic32_uart_writel(sport
, PIC32_CLR(PIC32_UART_MODE
),
556 PIC32_UART_MODE_PDSEL0
);
558 pic32_uart_writel(sport
, PIC32_SET(PIC32_UART_MODE
),
559 PIC32_UART_MODE_PDSEL0
);
560 pic32_uart_writel(sport
, PIC32_CLR(PIC32_UART_MODE
),
561 PIC32_UART_MODE_PDSEL1
);
564 pic32_uart_writel(sport
, PIC32_CLR(PIC32_UART_MODE
),
565 PIC32_UART_MODE_PDSEL1
|
566 PIC32_UART_MODE_PDSEL0
);
568 /* if hw flow ctrl, then the pins must be specified in device tree */
569 if ((new->c_cflag
& CRTSCTS
) && sport
->hw_flow_ctrl
) {
570 /* enable hardware flow control */
571 pic32_uart_writel(sport
, PIC32_SET(PIC32_UART_MODE
),
572 PIC32_UART_MODE_UEN1
);
573 pic32_uart_writel(sport
, PIC32_CLR(PIC32_UART_MODE
),
574 PIC32_UART_MODE_UEN0
);
575 pic32_uart_writel(sport
, PIC32_CLR(PIC32_UART_MODE
),
576 PIC32_UART_MODE_RTSMD
);
578 /* disable hardware flow control */
579 pic32_uart_writel(sport
, PIC32_CLR(PIC32_UART_MODE
),
580 PIC32_UART_MODE_UEN1
);
581 pic32_uart_writel(sport
, PIC32_CLR(PIC32_UART_MODE
),
582 PIC32_UART_MODE_UEN0
);
583 pic32_uart_writel(sport
, PIC32_CLR(PIC32_UART_MODE
),
584 PIC32_UART_MODE_RTSMD
);
590 /* Mark/Space parity is not supported */
591 new->c_cflag
&= ~CMSPAR
;
594 baud
= uart_get_baud_rate(port
, new, old
, 0, port
->uartclk
/ 16);
595 quot
= uart_get_divisor(port
, baud
) - 1;
596 pic32_uart_writel(sport
, PIC32_UART_BRG
, quot
);
597 uart_update_timeout(port
, new->c_cflag
, baud
);
599 if (tty_termios_baud_rate(new))
600 tty_termios_encode_baud_rate(new, baud
, baud
);
603 pic32_uart_en_and_unmask(port
);
605 spin_unlock_irqrestore(&port
->lock
, flags
);
608 /* serial core request to claim uart iomem */
609 static int pic32_uart_request_port(struct uart_port
*port
)
611 struct platform_device
*pdev
= to_platform_device(port
->dev
);
612 struct resource
*res_mem
;
614 res_mem
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
615 if (unlikely(!res_mem
))
618 if (!request_mem_region(port
->mapbase
, resource_size(res_mem
),
622 port
->membase
= devm_ioremap_nocache(port
->dev
, port
->mapbase
,
623 resource_size(res_mem
));
624 if (!port
->membase
) {
625 dev_err(port
->dev
, "Unable to map registers\n");
626 release_mem_region(port
->mapbase
, resource_size(res_mem
));
633 /* serial core request to release uart iomem */
634 static void pic32_uart_release_port(struct uart_port
*port
)
636 struct platform_device
*pdev
= to_platform_device(port
->dev
);
637 struct resource
*res_mem
;
638 unsigned int res_size
;
640 res_mem
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
641 if (unlikely(!res_mem
))
643 res_size
= resource_size(res_mem
);
645 release_mem_region(port
->mapbase
, res_size
);
648 /* serial core request to do any port required auto-configuration */
649 static void pic32_uart_config_port(struct uart_port
*port
, int flags
)
651 if (flags
& UART_CONFIG_TYPE
) {
652 if (pic32_uart_request_port(port
))
654 port
->type
= PORT_PIC32
;
658 /* serial core request to check that port information in serinfo are suitable */
659 static int pic32_uart_verify_port(struct uart_port
*port
,
660 struct serial_struct
*serinfo
)
662 if (port
->type
!= PORT_PIC32
)
664 if (port
->irq
!= serinfo
->irq
)
666 if (port
->iotype
!= serinfo
->io_type
)
668 if (port
->mapbase
!= (unsigned long)serinfo
->iomem_base
)
674 /* serial core callbacks */
675 static const struct uart_ops pic32_uart_ops
= {
676 .tx_empty
= pic32_uart_tx_empty
,
677 .get_mctrl
= pic32_uart_get_mctrl
,
678 .set_mctrl
= pic32_uart_set_mctrl
,
679 .start_tx
= pic32_uart_start_tx
,
680 .stop_tx
= pic32_uart_stop_tx
,
681 .stop_rx
= pic32_uart_stop_rx
,
682 .break_ctl
= pic32_uart_break_ctl
,
683 .startup
= pic32_uart_startup
,
684 .shutdown
= pic32_uart_shutdown
,
685 .set_termios
= pic32_uart_set_termios
,
686 .type
= pic32_uart_type
,
687 .release_port
= pic32_uart_release_port
,
688 .request_port
= pic32_uart_request_port
,
689 .config_port
= pic32_uart_config_port
,
690 .verify_port
= pic32_uart_verify_port
,
693 #ifdef CONFIG_SERIAL_PIC32_CONSOLE
694 /* output given char */
695 static void pic32_console_putchar(struct uart_port
*port
, int ch
)
697 struct pic32_sport
*sport
= to_pic32_sport(port
);
699 if (!(pic32_uart_readl(sport
, PIC32_UART_MODE
) & PIC32_UART_MODE_ON
))
702 if (!(pic32_uart_readl(sport
, PIC32_UART_STA
) & PIC32_UART_STA_UTXEN
))
705 /* wait for tx empty */
706 pic32_wait_deplete_txbuf(sport
);
708 pic32_uart_writel(sport
, PIC32_UART_TX
, ch
& 0xff);
711 /* console core request to output given string */
712 static void pic32_console_write(struct console
*co
, const char *s
,
715 struct pic32_sport
*sport
= pic32_sports
[co
->index
];
716 struct uart_port
*port
= pic32_get_port(sport
);
718 /* call uart helper to deal with \r\n */
719 uart_console_write(port
, s
, count
, pic32_console_putchar
);
722 /* console core request to setup given console, find matching uart
725 static int pic32_console_setup(struct console
*co
, char *options
)
727 struct pic32_sport
*sport
;
728 struct uart_port
*port
= NULL
;
735 if (unlikely(co
->index
< 0 || co
->index
>= PIC32_MAX_UARTS
))
738 sport
= pic32_sports
[co
->index
];
741 port
= pic32_get_port(sport
);
743 ret
= pic32_enable_clock(sport
);
748 uart_parse_options(options
, &baud
, &parity
, &bits
, &flow
);
750 return uart_set_options(port
, co
, baud
, parity
, bits
, flow
);
753 static struct uart_driver pic32_uart_driver
;
754 static struct console pic32_console
= {
755 .name
= PIC32_SDEV_NAME
,
756 .write
= pic32_console_write
,
757 .device
= uart_console_device
,
758 .setup
= pic32_console_setup
,
759 .flags
= CON_PRINTBUFFER
,
761 .data
= &pic32_uart_driver
,
763 #define PIC32_SCONSOLE (&pic32_console)
765 static int __init
pic32_console_init(void)
767 register_console(&pic32_console
);
770 console_initcall(pic32_console_init
);
772 static inline bool is_pic32_console_port(struct uart_port
*port
)
774 return port
->cons
&& port
->cons
->index
== port
->line
;
778 * Late console initialization.
780 static int __init
pic32_late_console_init(void)
782 if (!(pic32_console
.flags
& CON_ENABLED
))
783 register_console(&pic32_console
);
788 core_initcall(pic32_late_console_init
);
791 #define PIC32_SCONSOLE NULL
794 static struct uart_driver pic32_uart_driver
= {
795 .owner
= THIS_MODULE
,
796 .driver_name
= PIC32_DEV_NAME
,
797 .dev_name
= PIC32_SDEV_NAME
,
798 .nr
= PIC32_MAX_UARTS
,
799 .cons
= PIC32_SCONSOLE
,
802 static int pic32_uart_probe(struct platform_device
*pdev
)
804 struct device_node
*np
= pdev
->dev
.of_node
;
805 struct pic32_sport
*sport
;
807 struct resource
*res_mem
;
808 struct uart_port
*port
;
811 uart_idx
= of_alias_get_id(np
, "serial");
812 if (uart_idx
< 0 || uart_idx
>= PIC32_MAX_UARTS
)
815 res_mem
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
819 sport
= devm_kzalloc(&pdev
->dev
, sizeof(*sport
), GFP_KERNEL
);
823 sport
->idx
= uart_idx
;
824 sport
->irq_fault
= irq_of_parse_and_map(np
, 0);
825 sport
->irqflags_fault
= IRQF_NO_THREAD
;
826 sport
->irq_rx
= irq_of_parse_and_map(np
, 1);
827 sport
->irqflags_rx
= IRQF_NO_THREAD
;
828 sport
->irq_tx
= irq_of_parse_and_map(np
, 2);
829 sport
->irqflags_tx
= IRQF_NO_THREAD
;
830 sport
->clk
= devm_clk_get(&pdev
->dev
, NULL
);
831 sport
->cts_gpio
= -EINVAL
;
832 sport
->dev
= &pdev
->dev
;
834 /* Hardware flow control: gpios
835 * !Note: Basically, CTS is needed for reading the status.
837 sport
->hw_flow_ctrl
= false;
838 sport
->cts_gpio
= of_get_named_gpio(np
, "cts-gpios", 0);
839 if (gpio_is_valid(sport
->cts_gpio
)) {
840 sport
->hw_flow_ctrl
= true;
842 ret
= devm_gpio_request(sport
->dev
,
843 sport
->cts_gpio
, "CTS");
846 "error requesting CTS GPIO\n");
850 ret
= gpio_direction_input(sport
->cts_gpio
);
852 dev_err(&pdev
->dev
, "error setting CTS GPIO\n");
857 pic32_sports
[uart_idx
] = sport
;
859 memset(port
, 0, sizeof(*port
));
860 port
->iotype
= UPIO_MEM
;
861 port
->mapbase
= res_mem
->start
;
862 port
->ops
= &pic32_uart_ops
;
863 port
->flags
= UPF_BOOT_AUTOCONF
;
864 port
->dev
= &pdev
->dev
;
865 port
->fifosize
= PIC32_UART_TX_FIFO_DEPTH
;
866 port
->uartclk
= clk_get_rate(sport
->clk
);
867 port
->line
= uart_idx
;
869 ret
= uart_add_one_port(&pic32_uart_driver
, port
);
871 port
->membase
= NULL
;
872 dev_err(port
->dev
, "%s: uart add port error!\n", __func__
);
876 #ifdef CONFIG_SERIAL_PIC32_CONSOLE
877 if (is_pic32_console_port(port
) &&
878 (pic32_console
.flags
& CON_ENABLED
)) {
879 /* The peripheral clock has been enabled by console_setup,
880 * so disable it till the port is used.
882 pic32_disable_clock(sport
);
886 platform_set_drvdata(pdev
, port
);
888 dev_info(&pdev
->dev
, "%s: uart(%d) driver initialized.\n",
893 /* automatic unroll of sport and gpios */
897 static int pic32_uart_remove(struct platform_device
*pdev
)
899 struct uart_port
*port
= platform_get_drvdata(pdev
);
900 struct pic32_sport
*sport
= to_pic32_sport(port
);
902 uart_remove_one_port(&pic32_uart_driver
, port
);
903 pic32_disable_clock(sport
);
904 platform_set_drvdata(pdev
, NULL
);
905 pic32_sports
[sport
->idx
] = NULL
;
907 /* automatic unroll of sport and gpios */
911 static const struct of_device_id pic32_serial_dt_ids
[] = {
912 { .compatible
= "microchip,pic32mzda-uart" },
915 MODULE_DEVICE_TABLE(of
, pic32_serial_dt_ids
);
917 static struct platform_driver pic32_uart_platform_driver
= {
918 .probe
= pic32_uart_probe
,
919 .remove
= pic32_uart_remove
,
921 .name
= PIC32_DEV_NAME
,
922 .of_match_table
= of_match_ptr(pic32_serial_dt_ids
),
926 static int __init
pic32_uart_init(void)
930 ret
= uart_register_driver(&pic32_uart_driver
);
932 pr_err("failed to register %s:%d\n",
933 pic32_uart_driver
.driver_name
, ret
);
937 ret
= platform_driver_register(&pic32_uart_platform_driver
);
939 pr_err("fail to register pic32 uart\n");
940 uart_unregister_driver(&pic32_uart_driver
);
945 arch_initcall(pic32_uart_init
);
947 static void __exit
pic32_uart_exit(void)
949 #ifdef CONFIG_SERIAL_PIC32_CONSOLE
950 unregister_console(&pic32_console
);
952 platform_driver_unregister(&pic32_uart_platform_driver
);
953 uart_unregister_driver(&pic32_uart_driver
);
955 module_exit(pic32_uart_exit
);
957 MODULE_AUTHOR("Sorin-Andrei Pistirica <andrei.pistirica@microchip.com>");
958 MODULE_DESCRIPTION("Microchip PIC32 integrated serial port driver");
959 MODULE_LICENSE("GPL v2");