Lines Matching +full:serial +full:- +full:clk +full:- +full:low

1 // SPDX-License-Identifier: GPL-2.0+
3 * Driver for Atmel AT91 Serial ports
7 * Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o.
16 #include <linux/serial.h>
17 #include <linux/clk.h>
18 #include <linux/clk-provider.h>
25 #include <linux/dma-mapping.h>
48 * high and low thresholds
63 /* Use device name ttyAT, major 204 and minor 154-169. This is necessary if we
72 /* Use device name ttyS, major 4, minor 64-68. This is the usual serial port
113 struct clk *clk; /* uart clock */ member
114 struct clk *gclk; /* uart generic clock */
166 bool hd_start_rx; /* can start RX during half-duplex operation */
196 { .compatible = "atmel,at91rm9200-usart-serial" },
209 return __raw_readl(port->membase + reg); in atmel_uart_readl()
214 __raw_writel(value, port->membase + reg); in atmel_uart_writel()
219 return __raw_readb(port->membase + ATMEL_US_RHR); in atmel_uart_read_char()
224 __raw_writeb(value, port->membase + ATMEL_US_THR); in atmel_uart_write_char()
229 return ((port->rs485.flags & SER_RS485_ENABLED) && in atmel_uart_is_half_duplex()
230 !(port->rs485.flags & SER_RS485_RX_DURING_TX)) || in atmel_uart_is_half_duplex()
231 (port->iso7816.flags & SER_ISO7816_ENABLED); in atmel_uart_is_half_duplex()
236 return 100 - (desired_value * 100) / actual_value; in atmel_error_rate()
244 return atmel_port->use_pdc_rx; in atmel_use_pdc_rx()
251 return atmel_port->use_pdc_tx; in atmel_use_pdc_tx()
269 return atmel_port->use_dma_tx; in atmel_use_dma_tx()
276 return atmel_port->use_dma_rx; in atmel_use_dma_rx()
283 return atmel_port->fifo_size; in atmel_use_fifo()
289 if (!atomic_read(&atmel_port->tasklet_shutdown)) in atmel_tasklet_schedule()
301 atmel_uart_writel(port, ATMEL_US_IDR, atmel_port->tx_done_mask); in atmel_config_rs485()
305 if (rs485conf->flags & SER_RS485_ENABLED) { in atmel_config_rs485()
306 dev_dbg(port->dev, "Setting UART to RS485\n"); in atmel_config_rs485()
307 if (rs485conf->flags & SER_RS485_RX_DURING_TX) in atmel_config_rs485()
308 atmel_port->tx_done_mask = ATMEL_US_TXRDY; in atmel_config_rs485()
310 atmel_port->tx_done_mask = ATMEL_US_TXEMPTY; in atmel_config_rs485()
313 rs485conf->delay_rts_after_send); in atmel_config_rs485()
317 dev_dbg(port->dev, "Setting UART to RS232\n"); in atmel_config_rs485()
319 atmel_port->tx_done_mask = ATMEL_US_ENDTX | in atmel_config_rs485()
322 atmel_port->tx_done_mask = ATMEL_US_TXRDY; in atmel_config_rs485()
327 atmel_uart_writel(port, ATMEL_US_IER, atmel_port->tx_done_mask); in atmel_config_rs485()
339 mck_rate = (u64)clk_get_rate(atmel_port->clk); in atmel_calc_cd()
340 do_div(mck_rate, iso7816conf->clk); in atmel_calc_cd()
350 if (iso7816conf->sc_fi && iso7816conf->sc_di) { in atmel_calc_fidi()
351 fidi = (u64)iso7816conf->sc_fi; in atmel_calc_fidi()
352 do_div(fidi, iso7816conf->sc_di); in atmel_calc_fidi()
368 atmel_uart_writel(port, ATMEL_US_IDR, atmel_port->tx_done_mask); in atmel_config_iso7816()
372 if (iso7816conf->flags & SER_ISO7816_ENABLED) { in atmel_config_iso7816()
375 if (iso7816conf->tg > 255) { in atmel_config_iso7816()
376 dev_err(port->dev, "ISO7816: Timeguard exceeding 255\n"); in atmel_config_iso7816()
378 ret = -EINVAL; in atmel_config_iso7816()
382 if ((iso7816conf->flags & SER_ISO7816_T_PARAM) in atmel_config_iso7816()
385 } else if ((iso7816conf->flags & SER_ISO7816_T_PARAM) in atmel_config_iso7816()
389 dev_err(port->dev, "ISO7816: Type not supported\n"); in atmel_config_iso7816()
391 ret = -EINVAL; in atmel_config_iso7816()
405 dev_warn(port->dev, "ISO7816 fidi = 0, Generator generates no signal\n"); in atmel_config_iso7816()
406 } else if (fidi < atmel_port->fidi_min in atmel_config_iso7816()
407 || fidi > atmel_port->fidi_max) { in atmel_config_iso7816()
408 dev_err(port->dev, "ISO7816 fidi = %u, value not supported\n", fidi); in atmel_config_iso7816()
410 ret = -EINVAL; in atmel_config_iso7816()
414 if (!(port->iso7816.flags & SER_ISO7816_ENABLED)) { in atmel_config_iso7816()
416 atmel_port->backup_mode = atmel_uart_readl(port, ATMEL_US_MR); in atmel_config_iso7816()
417 atmel_port->backup_brgr = atmel_uart_readl(port, ATMEL_US_BRGR); in atmel_config_iso7816()
420 atmel_uart_writel(port, ATMEL_US_TTGR, iso7816conf->tg); in atmel_config_iso7816()
425 atmel_port->tx_done_mask = ATMEL_US_TXEMPTY | ATMEL_US_NACK | ATMEL_US_ITERATION; in atmel_config_iso7816()
427 dev_dbg(port->dev, "Setting UART back to RS232\n"); in atmel_config_iso7816()
429 mode = atmel_port->backup_mode; in atmel_config_iso7816()
432 atmel_uart_writel(port, ATMEL_US_BRGR, atmel_port->backup_brgr); in atmel_config_iso7816()
436 atmel_port->tx_done_mask = ATMEL_US_ENDTX | in atmel_config_iso7816()
439 atmel_port->tx_done_mask = ATMEL_US_TXRDY; in atmel_config_iso7816()
442 port->iso7816 = *iso7816conf; in atmel_config_iso7816()
448 atmel_uart_writel(port, ATMEL_US_IER, atmel_port->tx_done_mask); in atmel_config_iso7816()
460 if (atmel_port->tx_stopped) in atmel_tx_empty()
478 if (port->rs485.flags & SER_RS485_ENABLED) { in atmel_set_mctrl()
480 port->rs485.delay_rts_after_send); in atmel_set_mctrl()
496 /* force RTS line to low level */ in atmel_set_mctrl()
512 mctrl_gpio_set(atmel_port->gpios, mctrl); in atmel_set_mctrl()
535 * The control signals are active low. in atmel_get_mctrl()
546 return mctrl_gpio_get(atmel_port->gpios, &ret); in atmel_get_mctrl()
567 atmel_port->tx_stopped = true; in atmel_stop_tx()
570 atmel_uart_writel(port, ATMEL_US_IDR, atmel_port->tx_done_mask); in atmel_stop_tx()
573 if (!atomic_read(&atmel_port->tasklet_shutdown)) in atmel_stop_tx()
596 /* re-enable PDC transmit */ in atmel_start_tx()
600 atmel_uart_writel(port, ATMEL_US_IER, atmel_port->tx_done_mask); in atmel_start_tx()
602 /* re-enable the transmitter */ in atmel_start_tx()
604 atmel_port->tx_stopped = false; in atmel_start_tx()
608 * start receiving - port is in process of being opened.
621 port->read_status_mask); in atmel_start_rx()
629 * Stop receiving - port is in process of being closed.
640 port->read_status_mask); in atmel_stop_rx()
657 if (atmel_port->ms_irq_enabled) in atmel_enable_ms()
660 atmel_port->ms_irq_enabled = true; in atmel_enable_ms()
662 if (!mctrl_gpio_to_gpiod(atmel_port->gpios, UART_GPIO_CTS)) in atmel_enable_ms()
665 if (!mctrl_gpio_to_gpiod(atmel_port->gpios, UART_GPIO_DSR)) in atmel_enable_ms()
668 if (!mctrl_gpio_to_gpiod(atmel_port->gpios, UART_GPIO_RI)) in atmel_enable_ms()
671 if (!mctrl_gpio_to_gpiod(atmel_port->gpios, UART_GPIO_DCD)) in atmel_enable_ms()
676 mctrl_gpio_enable_ms(atmel_port->gpios); in atmel_enable_ms()
690 if (!atmel_port->ms_irq_enabled) in atmel_disable_ms()
693 atmel_port->ms_irq_enabled = false; in atmel_disable_ms()
695 mctrl_gpio_disable_ms(atmel_port->gpios); in atmel_disable_ms()
697 if (!mctrl_gpio_to_gpiod(atmel_port->gpios, UART_GPIO_CTS)) in atmel_disable_ms()
700 if (!mctrl_gpio_to_gpiod(atmel_port->gpios, UART_GPIO_DSR)) in atmel_disable_ms()
703 if (!mctrl_gpio_to_gpiod(atmel_port->gpios, UART_GPIO_RI)) in atmel_disable_ms()
706 if (!mctrl_gpio_to_gpiod(atmel_port->gpios, UART_GPIO_DCD)) in atmel_disable_ms()
733 struct circ_buf *ring = &atmel_port->rx_ring; in atmel_buffer_rx_char()
736 if (!CIRC_SPACE(ring->head, ring->tail, ATMEL_SERIAL_RINGSIZE)) in atmel_buffer_rx_char()
740 c = &((struct atmel_uart_char *)ring->buf)[ring->head]; in atmel_buffer_rx_char()
741 c->status = status; in atmel_buffer_rx_char()
742 c->ch = ch; in atmel_buffer_rx_char()
747 ring->head = (ring->head + 1) & (ATMEL_SERIAL_RINGSIZE - 1); in atmel_buffer_rx_char()
759 /* ignore side-effect */ in atmel_pdc_rxerr()
761 port->icount.brk++; in atmel_pdc_rxerr()
764 port->icount.parity++; in atmel_pdc_rxerr()
766 port->icount.frame++; in atmel_pdc_rxerr()
768 port->icount.overrun++; in atmel_pdc_rxerr()
789 || atmel_port->break_active)) { in atmel_rx_chars()
795 && !atmel_port->break_active) { in atmel_rx_chars()
796 atmel_port->break_active = 1; in atmel_rx_chars()
801 * This is either the end-of-break in atmel_rx_chars()
805 * RXBRK will indicate start-of-break. in atmel_rx_chars()
810 atmel_port->break_active = 0; in atmel_rx_chars()
818 atmel_tasklet_schedule(atmel_port, &atmel_port->tasklet_rx); in atmel_rx_chars()
827 struct circ_buf *xmit = &port->state->xmit; in atmel_tx_chars()
830 if (port->x_char && in atmel_tx_chars()
832 atmel_uart_write_char(port, port->x_char); in atmel_tx_chars()
833 port->icount.tx++; in atmel_tx_chars()
834 port->x_char = 0; in atmel_tx_chars()
840 atmel_uart_write_char(port, xmit->buf[xmit->tail]); in atmel_tx_chars()
841 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1); in atmel_tx_chars()
842 port->icount.tx++; in atmel_tx_chars()
855 atmel_port->tx_done_mask |= ATMEL_US_TXRDY; in atmel_tx_chars()
859 atmel_port->tx_done_mask); in atmel_tx_chars()
862 atmel_port->tx_done_mask &= ~ATMEL_US_TXRDY; in atmel_tx_chars()
869 struct uart_port *port = &atmel_port->uart; in atmel_complete_tx_dma()
870 struct circ_buf *xmit = &port->state->xmit; in atmel_complete_tx_dma()
871 struct dma_chan *chan = atmel_port->chan_tx; in atmel_complete_tx_dma()
874 spin_lock_irqsave(&port->lock, flags); in atmel_complete_tx_dma()
878 xmit->tail += atmel_port->tx_len; in atmel_complete_tx_dma()
879 xmit->tail &= UART_XMIT_SIZE - 1; in atmel_complete_tx_dma()
881 port->icount.tx += atmel_port->tx_len; in atmel_complete_tx_dma()
883 spin_lock_irq(&atmel_port->lock_tx); in atmel_complete_tx_dma()
884 async_tx_ack(atmel_port->desc_tx); in atmel_complete_tx_dma()
885 atmel_port->cookie_tx = -EINVAL; in atmel_complete_tx_dma()
886 atmel_port->desc_tx = NULL; in atmel_complete_tx_dma()
887 spin_unlock_irq(&atmel_port->lock_tx); in atmel_complete_tx_dma()
894 * xmit->tail to the end of xmit->buf, now we have to transmit the in atmel_complete_tx_dma()
895 * remaining data from the beginning of xmit->buf to xmit->head. in atmel_complete_tx_dma()
898 atmel_tasklet_schedule(atmel_port, &atmel_port->tasklet_tx); in atmel_complete_tx_dma()
901 * DMA done, re-enable TXEMPTY and signal that we can stop in atmel_complete_tx_dma()
904 atmel_port->hd_start_rx = true; in atmel_complete_tx_dma()
906 atmel_port->tx_done_mask); in atmel_complete_tx_dma()
909 spin_unlock_irqrestore(&port->lock, flags); in atmel_complete_tx_dma()
915 struct dma_chan *chan = atmel_port->chan_tx; in atmel_release_tx_dma()
920 dma_unmap_sg(port->dev, &atmel_port->sg_tx, 1, in atmel_release_tx_dma()
924 atmel_port->desc_tx = NULL; in atmel_release_tx_dma()
925 atmel_port->chan_tx = NULL; in atmel_release_tx_dma()
926 atmel_port->cookie_tx = -EINVAL; in atmel_release_tx_dma()
935 struct circ_buf *xmit = &port->state->xmit; in atmel_tx_dma()
936 struct dma_chan *chan = atmel_port->chan_tx; in atmel_tx_dma()
938 struct scatterlist sgl[2], *sg, *sg_tx = &atmel_port->sg_tx; in atmel_tx_dma()
943 if (atmel_port->desc_tx != NULL) in atmel_tx_dma()
956 tx_len = CIRC_CNT_TO_END(xmit->head, in atmel_tx_dma()
957 xmit->tail, in atmel_tx_dma()
960 if (atmel_port->fifo_size) { in atmel_tx_dma()
972 phys_addr = sg_dma_address(sg_tx) + xmit->tail; in atmel_tx_dma()
989 * xmit->tail correctly in atmel_tx_dma()
991 atmel_port->tx_len = tx_len; in atmel_tx_dma()
1000 dev_err(port->dev, "Failed to send via dma!\n"); in atmel_tx_dma()
1004 dma_sync_sg_for_device(port->dev, sg_tx, 1, DMA_TO_DEVICE); in atmel_tx_dma()
1006 atmel_port->desc_tx = desc; in atmel_tx_dma()
1007 desc->callback = atmel_complete_tx_dma; in atmel_tx_dma()
1008 desc->callback_param = atmel_port; in atmel_tx_dma()
1009 atmel_port->cookie_tx = dmaengine_submit(desc); in atmel_tx_dma()
1010 if (dma_submit_error(atmel_port->cookie_tx)) { in atmel_tx_dma()
1011 dev_err(port->dev, "dma_submit_error %d\n", in atmel_tx_dma()
1012 atmel_port->cookie_tx); in atmel_tx_dma()
1026 struct device *mfd_dev = port->dev->parent; in atmel_prepare_tx_dma()
1034 atmel_port->chan_tx = dma_request_slave_channel(mfd_dev, "tx"); in atmel_prepare_tx_dma()
1035 if (atmel_port->chan_tx == NULL) in atmel_prepare_tx_dma()
1037 dev_info(port->dev, "using %s for tx DMA transfers\n", in atmel_prepare_tx_dma()
1038 dma_chan_name(atmel_port->chan_tx)); in atmel_prepare_tx_dma()
1040 spin_lock_init(&atmel_port->lock_tx); in atmel_prepare_tx_dma()
1041 sg_init_table(&atmel_port->sg_tx, 1); in atmel_prepare_tx_dma()
1043 BUG_ON(!PAGE_ALIGNED(port->state->xmit.buf)); in atmel_prepare_tx_dma()
1044 sg_set_page(&atmel_port->sg_tx, in atmel_prepare_tx_dma()
1045 virt_to_page(port->state->xmit.buf), in atmel_prepare_tx_dma()
1047 offset_in_page(port->state->xmit.buf)); in atmel_prepare_tx_dma()
1048 nent = dma_map_sg(port->dev, in atmel_prepare_tx_dma()
1049 &atmel_port->sg_tx, in atmel_prepare_tx_dma()
1054 dev_dbg(port->dev, "need to release resource of dma\n"); in atmel_prepare_tx_dma()
1057 dev_dbg(port->dev, "%s: mapped %d@%p to %pad\n", __func__, in atmel_prepare_tx_dma()
1058 sg_dma_len(&atmel_port->sg_tx), in atmel_prepare_tx_dma()
1059 port->state->xmit.buf, in atmel_prepare_tx_dma()
1060 &sg_dma_address(&atmel_port->sg_tx)); in atmel_prepare_tx_dma()
1066 config.dst_addr_width = (atmel_port->fifo_size) ? in atmel_prepare_tx_dma()
1069 config.dst_addr = port->mapbase + ATMEL_US_THR; in atmel_prepare_tx_dma()
1072 ret = dmaengine_slave_config(atmel_port->chan_tx, in atmel_prepare_tx_dma()
1075 dev_err(port->dev, "DMA tx slave configuration failed\n"); in atmel_prepare_tx_dma()
1082 dev_err(port->dev, "TX channel not available, switch to pio\n"); in atmel_prepare_tx_dma()
1083 atmel_port->use_dma_tx = false; in atmel_prepare_tx_dma()
1084 if (atmel_port->chan_tx) in atmel_prepare_tx_dma()
1086 return -EINVAL; in atmel_prepare_tx_dma()
1094 atmel_tasklet_schedule(atmel_port, &atmel_port->tasklet_rx); in atmel_complete_rx_dma()
1100 struct dma_chan *chan = atmel_port->chan_rx; in atmel_release_rx_dma()
1105 dma_unmap_sg(port->dev, &atmel_port->sg_rx, 1, in atmel_release_rx_dma()
1109 atmel_port->desc_rx = NULL; in atmel_release_rx_dma()
1110 atmel_port->chan_rx = NULL; in atmel_release_rx_dma()
1111 atmel_port->cookie_rx = -EINVAL; in atmel_release_rx_dma()
1117 struct tty_port *tport = &port->state->port; in atmel_rx_from_dma()
1118 struct circ_buf *ring = &atmel_port->rx_ring; in atmel_rx_from_dma()
1119 struct dma_chan *chan = atmel_port->chan_rx; in atmel_rx_from_dma()
1128 atmel_port->cookie_rx, in atmel_rx_from_dma()
1132 dev_dbg(port->dev, "Get residue error, restart tasklet\n"); in atmel_rx_from_dma()
1134 atmel_tasklet_schedule(atmel_port, &atmel_port->tasklet_rx); in atmel_rx_from_dma()
1139 dma_sync_sg_for_cpu(port->dev, in atmel_rx_from_dma()
1140 &atmel_port->sg_rx, in atmel_rx_from_dma()
1145 * ring->head points to the end of data already written by the DMA. in atmel_rx_from_dma()
1146 * ring->tail points to the beginning of data to be read by the in atmel_rx_from_dma()
1151 ring->head = sg_dma_len(&atmel_port->sg_rx) - state.residue; in atmel_rx_from_dma()
1152 BUG_ON(ring->head > sg_dma_len(&atmel_port->sg_rx)); in atmel_rx_from_dma()
1154 * At this point ring->head may point to the first byte right after the in atmel_rx_from_dma()
1156 * 0 <= ring->head <= sg_dma_len(&atmel_port->sg_rx) in atmel_rx_from_dma()
1158 * However ring->tail must always points inside the dma buffer: in atmel_rx_from_dma()
1159 * 0 <= ring->tail <= sg_dma_len(&atmel_port->sg_rx) - 1 in atmel_rx_from_dma()
1165 if (ring->head < ring->tail) { in atmel_rx_from_dma()
1166 count = sg_dma_len(&atmel_port->sg_rx) - ring->tail; in atmel_rx_from_dma()
1168 tty_insert_flip_string(tport, ring->buf + ring->tail, count); in atmel_rx_from_dma()
1169 ring->tail = 0; in atmel_rx_from_dma()
1170 port->icount.rx += count; in atmel_rx_from_dma()
1174 if (ring->tail < ring->head) { in atmel_rx_from_dma()
1175 count = ring->head - ring->tail; in atmel_rx_from_dma()
1177 tty_insert_flip_string(tport, ring->buf + ring->tail, count); in atmel_rx_from_dma()
1178 /* Wrap ring->head if needed */ in atmel_rx_from_dma()
1179 if (ring->head >= sg_dma_len(&atmel_port->sg_rx)) in atmel_rx_from_dma()
1180 ring->head = 0; in atmel_rx_from_dma()
1181 ring->tail = ring->head; in atmel_rx_from_dma()
1182 port->icount.rx += count; in atmel_rx_from_dma()
1186 dma_sync_sg_for_device(port->dev, in atmel_rx_from_dma()
1187 &atmel_port->sg_rx, in atmel_rx_from_dma()
1199 struct device *mfd_dev = port->dev->parent; in atmel_prepare_rx_dma()
1206 ring = &atmel_port->rx_ring; in atmel_prepare_rx_dma()
1211 atmel_port->chan_rx = dma_request_slave_channel(mfd_dev, "rx"); in atmel_prepare_rx_dma()
1212 if (atmel_port->chan_rx == NULL) in atmel_prepare_rx_dma()
1214 dev_info(port->dev, "using %s for rx DMA transfers\n", in atmel_prepare_rx_dma()
1215 dma_chan_name(atmel_port->chan_rx)); in atmel_prepare_rx_dma()
1217 spin_lock_init(&atmel_port->lock_rx); in atmel_prepare_rx_dma()
1218 sg_init_table(&atmel_port->sg_rx, 1); in atmel_prepare_rx_dma()
1220 BUG_ON(!PAGE_ALIGNED(ring->buf)); in atmel_prepare_rx_dma()
1221 sg_set_page(&atmel_port->sg_rx, in atmel_prepare_rx_dma()
1222 virt_to_page(ring->buf), in atmel_prepare_rx_dma()
1224 offset_in_page(ring->buf)); in atmel_prepare_rx_dma()
1225 nent = dma_map_sg(port->dev, in atmel_prepare_rx_dma()
1226 &atmel_port->sg_rx, in atmel_prepare_rx_dma()
1231 dev_dbg(port->dev, "need to release resource of dma\n"); in atmel_prepare_rx_dma()
1234 dev_dbg(port->dev, "%s: mapped %d@%p to %pad\n", __func__, in atmel_prepare_rx_dma()
1235 sg_dma_len(&atmel_port->sg_rx), in atmel_prepare_rx_dma()
1236 ring->buf, in atmel_prepare_rx_dma()
1237 &sg_dma_address(&atmel_port->sg_rx)); in atmel_prepare_rx_dma()
1244 config.src_addr = port->mapbase + ATMEL_US_RHR; in atmel_prepare_rx_dma()
1247 ret = dmaengine_slave_config(atmel_port->chan_rx, in atmel_prepare_rx_dma()
1250 dev_err(port->dev, "DMA rx slave configuration failed\n"); in atmel_prepare_rx_dma()
1257 desc = dmaengine_prep_dma_cyclic(atmel_port->chan_rx, in atmel_prepare_rx_dma()
1258 sg_dma_address(&atmel_port->sg_rx), in atmel_prepare_rx_dma()
1259 sg_dma_len(&atmel_port->sg_rx), in atmel_prepare_rx_dma()
1260 sg_dma_len(&atmel_port->sg_rx)/2, in atmel_prepare_rx_dma()
1264 dev_err(port->dev, "Preparing DMA cyclic failed\n"); in atmel_prepare_rx_dma()
1267 desc->callback = atmel_complete_rx_dma; in atmel_prepare_rx_dma()
1268 desc->callback_param = port; in atmel_prepare_rx_dma()
1269 atmel_port->desc_rx = desc; in atmel_prepare_rx_dma()
1270 atmel_port->cookie_rx = dmaengine_submit(desc); in atmel_prepare_rx_dma()
1271 if (dma_submit_error(atmel_port->cookie_rx)) { in atmel_prepare_rx_dma()
1272 dev_err(port->dev, "dma_submit_error %d\n", in atmel_prepare_rx_dma()
1273 atmel_port->cookie_rx); in atmel_prepare_rx_dma()
1277 dma_async_issue_pending(atmel_port->chan_rx); in atmel_prepare_rx_dma()
1282 dev_err(port->dev, "RX channel not available, switch to pio\n"); in atmel_prepare_rx_dma()
1283 atmel_port->use_dma_rx = false; in atmel_prepare_rx_dma()
1284 if (atmel_port->chan_rx) in atmel_prepare_rx_dma()
1286 return -EINVAL; in atmel_prepare_rx_dma()
1293 struct uart_port *port = &atmel_port->uart; in atmel_uart_timer_callback()
1295 if (!atomic_read(&atmel_port->tasklet_shutdown)) { in atmel_uart_timer_callback()
1296 tasklet_schedule(&atmel_port->tasklet_rx); in atmel_uart_timer_callback()
1297 mod_timer(&atmel_port->uart_timer, in atmel_uart_timer_callback()
1322 &atmel_port->tasklet_rx); in atmel_handle_receive()
1335 &atmel_port->tasklet_rx); in atmel_handle_receive()
1349 atmel_port->break_active = 0; in atmel_handle_receive()
1361 if (pending & atmel_port->tx_done_mask) { in atmel_handle_transmit()
1363 atmel_port->tx_done_mask); in atmel_handle_transmit()
1366 if (atmel_port->hd_start_rx) { in atmel_handle_transmit()
1369 dev_warn(port->dev, "Should start RX, but TX fifo is not empty\n"); in atmel_handle_transmit()
1371 atmel_port->hd_start_rx = false; in atmel_handle_transmit()
1375 atmel_tasklet_schedule(atmel_port, &atmel_port->tasklet_tx); in atmel_handle_transmit()
1391 status_change = status ^ atmel_port->irq_status_prev; in atmel_handle_status()
1392 atmel_port->irq_status_prev = status; in atmel_handle_status()
1398 port->icount.rng++; in atmel_handle_status()
1400 port->icount.dsr++; in atmel_handle_status()
1406 wake_up_interruptible(&port->state->port.delta_msr_wait); in atmel_handle_status()
1411 dev_dbg(port->dev, "ISO7816 ERROR (0x%08x)\n", pending); in atmel_handle_status()
1423 spin_lock(&atmel_port->lock_suspended); in atmel_interrupt()
1432 if (atmel_port->suspended) { in atmel_interrupt()
1433 atmel_port->pending |= pending; in atmel_interrupt()
1434 atmel_port->pending_status = status; in atmel_interrupt()
1445 spin_unlock(&atmel_port->lock_suspended); in atmel_interrupt()
1453 struct atmel_dma_buffer *pdc = &atmel_port->pdc_tx; in atmel_release_tx_pdc()
1455 dma_unmap_single(port->dev, in atmel_release_tx_pdc()
1456 pdc->dma_addr, in atmel_release_tx_pdc()
1457 pdc->dma_size, in atmel_release_tx_pdc()
1467 struct circ_buf *xmit = &port->state->xmit; in atmel_tx_pdc()
1468 struct atmel_dma_buffer *pdc = &atmel_port->pdc_tx; in atmel_tx_pdc()
1475 xmit->tail += pdc->ofs; in atmel_tx_pdc()
1476 xmit->tail &= UART_XMIT_SIZE - 1; in atmel_tx_pdc()
1478 port->icount.tx += pdc->ofs; in atmel_tx_pdc()
1479 pdc->ofs = 0; in atmel_tx_pdc()
1481 /* more to transmit - setup next transfer */ in atmel_tx_pdc()
1487 dma_sync_single_for_device(port->dev, in atmel_tx_pdc()
1488 pdc->dma_addr, in atmel_tx_pdc()
1489 pdc->dma_size, in atmel_tx_pdc()
1492 count = CIRC_CNT_TO_END(xmit->head, xmit->tail, UART_XMIT_SIZE); in atmel_tx_pdc()
1493 pdc->ofs = count; in atmel_tx_pdc()
1496 pdc->dma_addr + xmit->tail); in atmel_tx_pdc()
1498 /* re-enable PDC transmit */ in atmel_tx_pdc()
1502 atmel_port->tx_done_mask); in atmel_tx_pdc()
1517 struct atmel_dma_buffer *pdc = &atmel_port->pdc_tx; in atmel_prepare_tx_pdc()
1518 struct circ_buf *xmit = &port->state->xmit; in atmel_prepare_tx_pdc()
1520 pdc->buf = xmit->buf; in atmel_prepare_tx_pdc()
1521 pdc->dma_addr = dma_map_single(port->dev, in atmel_prepare_tx_pdc()
1522 pdc->buf, in atmel_prepare_tx_pdc()
1525 pdc->dma_size = UART_XMIT_SIZE; in atmel_prepare_tx_pdc()
1526 pdc->ofs = 0; in atmel_prepare_tx_pdc()
1534 struct circ_buf *ring = &atmel_port->rx_ring; in atmel_rx_from_ring()
1538 while (ring->head != ring->tail) { in atmel_rx_from_ring()
1544 c = ((struct atmel_uart_char *)ring->buf)[ring->tail]; in atmel_rx_from_ring()
1546 ring->tail = (ring->tail + 1) & (ATMEL_SERIAL_RINGSIZE - 1); in atmel_rx_from_ring()
1548 port->icount.rx++; in atmel_rx_from_ring()
1559 /* ignore side-effect */ in atmel_rx_from_ring()
1562 port->icount.brk++; in atmel_rx_from_ring()
1567 port->icount.parity++; in atmel_rx_from_ring()
1569 port->icount.frame++; in atmel_rx_from_ring()
1571 port->icount.overrun++; in atmel_rx_from_ring()
1573 status &= port->read_status_mask; in atmel_rx_from_ring()
1590 tty_flip_buffer_push(&port->state->port); in atmel_rx_from_ring()
1599 struct atmel_dma_buffer *pdc = &atmel_port->pdc_rx[i]; in atmel_release_rx_pdc()
1601 dma_unmap_single(port->dev, in atmel_release_rx_pdc()
1602 pdc->dma_addr, in atmel_release_rx_pdc()
1603 pdc->dma_size, in atmel_release_rx_pdc()
1605 kfree(pdc->buf); in atmel_release_rx_pdc()
1612 struct tty_port *tport = &port->state->port; in atmel_rx_from_pdc()
1614 int rx_idx = atmel_port->pdc_rx_idx; in atmel_rx_from_pdc()
1623 pdc = &atmel_port->pdc_rx[rx_idx]; in atmel_rx_from_pdc()
1624 head = atmel_uart_readl(port, ATMEL_PDC_RPR) - pdc->dma_addr; in atmel_rx_from_pdc()
1625 tail = pdc->ofs; in atmel_rx_from_pdc()
1629 * is unsigned, we just need a one-way comparison to in atmel_rx_from_pdc()
1634 * ENDRX bit as well, so that we can safely re-enable in atmel_rx_from_pdc()
1637 head = min(head, pdc->dma_size); in atmel_rx_from_pdc()
1640 dma_sync_single_for_cpu(port->dev, pdc->dma_addr, in atmel_rx_from_pdc()
1641 pdc->dma_size, DMA_FROM_DEVICE); in atmel_rx_from_pdc()
1649 count = head - tail; in atmel_rx_from_pdc()
1651 tty_insert_flip_string(tport, pdc->buf + pdc->ofs, in atmel_rx_from_pdc()
1654 dma_sync_single_for_device(port->dev, pdc->dma_addr, in atmel_rx_from_pdc()
1655 pdc->dma_size, DMA_FROM_DEVICE); in atmel_rx_from_pdc()
1657 port->icount.rx += count; in atmel_rx_from_pdc()
1658 pdc->ofs = head; in atmel_rx_from_pdc()
1665 if (head >= pdc->dma_size) { in atmel_rx_from_pdc()
1666 pdc->ofs = 0; in atmel_rx_from_pdc()
1667 atmel_uart_writel(port, ATMEL_PDC_RNPR, pdc->dma_addr); in atmel_rx_from_pdc()
1668 atmel_uart_writel(port, ATMEL_PDC_RNCR, pdc->dma_size); in atmel_rx_from_pdc()
1671 atmel_port->pdc_rx_idx = rx_idx; in atmel_rx_from_pdc()
1673 } while (head >= pdc->dma_size); in atmel_rx_from_pdc()
1687 struct atmel_dma_buffer *pdc = &atmel_port->pdc_rx[i]; in atmel_prepare_rx_pdc()
1689 pdc->buf = kmalloc(PDC_BUFFER_SIZE, GFP_KERNEL); in atmel_prepare_rx_pdc()
1690 if (pdc->buf == NULL) { in atmel_prepare_rx_pdc()
1692 dma_unmap_single(port->dev, in atmel_prepare_rx_pdc()
1693 atmel_port->pdc_rx[0].dma_addr, in atmel_prepare_rx_pdc()
1696 kfree(atmel_port->pdc_rx[0].buf); in atmel_prepare_rx_pdc()
1698 atmel_port->use_pdc_rx = false; in atmel_prepare_rx_pdc()
1699 return -ENOMEM; in atmel_prepare_rx_pdc()
1701 pdc->dma_addr = dma_map_single(port->dev, in atmel_prepare_rx_pdc()
1702 pdc->buf, in atmel_prepare_rx_pdc()
1705 pdc->dma_size = PDC_BUFFER_SIZE; in atmel_prepare_rx_pdc()
1706 pdc->ofs = 0; in atmel_prepare_rx_pdc()
1709 atmel_port->pdc_rx_idx = 0; in atmel_prepare_rx_pdc()
1711 atmel_uart_writel(port, ATMEL_PDC_RPR, atmel_port->pdc_rx[0].dma_addr); in atmel_prepare_rx_pdc()
1715 atmel_port->pdc_rx[1].dma_addr); in atmel_prepare_rx_pdc()
1728 struct uart_port *port = &atmel_port->uart; in atmel_tasklet_rx_func()
1731 spin_lock(&port->lock); in atmel_tasklet_rx_func()
1732 atmel_port->schedule_rx(port); in atmel_tasklet_rx_func()
1733 spin_unlock(&port->lock); in atmel_tasklet_rx_func()
1740 struct uart_port *port = &atmel_port->uart; in atmel_tasklet_tx_func()
1743 spin_lock(&port->lock); in atmel_tasklet_tx_func()
1744 atmel_port->schedule_tx(port); in atmel_tasklet_tx_func()
1745 spin_unlock(&port->lock); in atmel_tasklet_tx_func()
1751 struct device_node *np = pdev->dev.of_node; in atmel_init_property()
1754 if (of_property_read_bool(np, "atmel,use-dma-rx")) { in atmel_init_property()
1756 atmel_port->use_dma_rx = true; in atmel_init_property()
1757 atmel_port->use_pdc_rx = false; in atmel_init_property()
1759 atmel_port->use_dma_rx = false; in atmel_init_property()
1760 atmel_port->use_pdc_rx = true; in atmel_init_property()
1763 atmel_port->use_dma_rx = false; in atmel_init_property()
1764 atmel_port->use_pdc_rx = false; in atmel_init_property()
1767 if (of_property_read_bool(np, "atmel,use-dma-tx")) { in atmel_init_property()
1769 atmel_port->use_dma_tx = true; in atmel_init_property()
1770 atmel_port->use_pdc_tx = false; in atmel_init_property()
1772 atmel_port->use_dma_tx = false; in atmel_init_property()
1773 atmel_port->use_pdc_tx = true; in atmel_init_property()
1776 atmel_port->use_dma_tx = false; in atmel_init_property()
1777 atmel_port->use_pdc_tx = false; in atmel_init_property()
1786 atmel_port->prepare_rx = &atmel_prepare_rx_dma; in atmel_set_ops()
1787 atmel_port->schedule_rx = &atmel_rx_from_dma; in atmel_set_ops()
1788 atmel_port->release_rx = &atmel_release_rx_dma; in atmel_set_ops()
1790 atmel_port->prepare_rx = &atmel_prepare_rx_pdc; in atmel_set_ops()
1791 atmel_port->schedule_rx = &atmel_rx_from_pdc; in atmel_set_ops()
1792 atmel_port->release_rx = &atmel_release_rx_pdc; in atmel_set_ops()
1794 atmel_port->prepare_rx = NULL; in atmel_set_ops()
1795 atmel_port->schedule_rx = &atmel_rx_from_ring; in atmel_set_ops()
1796 atmel_port->release_rx = NULL; in atmel_set_ops()
1800 atmel_port->prepare_tx = &atmel_prepare_tx_dma; in atmel_set_ops()
1801 atmel_port->schedule_tx = &atmel_tx_dma; in atmel_set_ops()
1802 atmel_port->release_tx = &atmel_release_tx_dma; in atmel_set_ops()
1804 atmel_port->prepare_tx = &atmel_prepare_tx_pdc; in atmel_set_ops()
1805 atmel_port->schedule_tx = &atmel_tx_pdc; in atmel_set_ops()
1806 atmel_port->release_tx = &atmel_release_tx_pdc; in atmel_set_ops()
1808 atmel_port->prepare_tx = NULL; in atmel_set_ops()
1809 atmel_port->schedule_tx = &atmel_tx_chars; in atmel_set_ops()
1810 atmel_port->release_tx = NULL; in atmel_set_ops()
1834 atmel_port->has_frac_baudrate = false; in atmel_get_ip_name()
1835 atmel_port->has_hw_timer = false; in atmel_get_ip_name()
1836 atmel_port->is_usart = false; in atmel_get_ip_name()
1839 dev_dbg(port->dev, "Uart with hw timer"); in atmel_get_ip_name()
1840 atmel_port->has_hw_timer = true; in atmel_get_ip_name()
1841 atmel_port->rtor = ATMEL_UA_RTOR; in atmel_get_ip_name()
1843 dev_dbg(port->dev, "Usart\n"); in atmel_get_ip_name()
1844 atmel_port->has_frac_baudrate = true; in atmel_get_ip_name()
1845 atmel_port->has_hw_timer = true; in atmel_get_ip_name()
1846 atmel_port->is_usart = true; in atmel_get_ip_name()
1847 atmel_port->rtor = ATMEL_US_RTOR; in atmel_get_ip_name()
1853 atmel_port->fidi_min = 3; in atmel_get_ip_name()
1854 atmel_port->fidi_max = 65535; in atmel_get_ip_name()
1857 atmel_port->fidi_min = 3; in atmel_get_ip_name()
1858 atmel_port->fidi_max = 2047; in atmel_get_ip_name()
1861 atmel_port->fidi_min = 1; in atmel_get_ip_name()
1862 atmel_port->fidi_max = 2047; in atmel_get_ip_name()
1865 dev_dbg(port->dev, "Dbgu or uart without hw timer\n"); in atmel_get_ip_name()
1873 dev_dbg(port->dev, "This version is usart\n"); in atmel_get_ip_name()
1874 atmel_port->has_frac_baudrate = true; in atmel_get_ip_name()
1875 atmel_port->has_hw_timer = true; in atmel_get_ip_name()
1876 atmel_port->is_usart = true; in atmel_get_ip_name()
1877 atmel_port->rtor = ATMEL_US_RTOR; in atmel_get_ip_name()
1881 dev_dbg(port->dev, "This version is uart\n"); in atmel_get_ip_name()
1884 dev_err(port->dev, "Not supported ip name nor version, set to uart\n"); in atmel_get_ip_name()
1894 struct platform_device *pdev = to_platform_device(port->dev); in atmel_startup()
1903 atmel_uart_writel(port, ATMEL_US_IDR, -1); in atmel_startup()
1904 atmel_port->ms_irq_enabled = false; in atmel_startup()
1909 retval = request_irq(port->irq, atmel_interrupt, in atmel_startup()
1911 dev_name(&pdev->dev), port); in atmel_startup()
1913 dev_err(port->dev, "atmel_startup - Can't get irq\n"); in atmel_startup()
1917 atomic_set(&atmel_port->tasklet_shutdown, 0); in atmel_startup()
1918 tasklet_setup(&atmel_port->tasklet_rx, atmel_tasklet_rx_func); in atmel_startup()
1919 tasklet_setup(&atmel_port->tasklet_tx, atmel_tasklet_tx_func); in atmel_startup()
1927 if (atmel_port->prepare_rx) { in atmel_startup()
1928 retval = atmel_port->prepare_rx(port); in atmel_startup()
1933 if (atmel_port->prepare_tx) { in atmel_startup()
1934 retval = atmel_port->prepare_tx(port); in atmel_startup()
1942 if (atmel_port->fifo_size) { in atmel_startup()
1956 if (atmel_port->rts_high && in atmel_startup()
1957 atmel_port->rts_low) in atmel_startup()
1959 ATMEL_US_RXFTHRES(atmel_port->rts_high) | in atmel_startup()
1960 ATMEL_US_RXFTHRES2(atmel_port->rts_low); in atmel_startup()
1966 atmel_port->irq_status_prev = atmel_uart_readl(port, ATMEL_US_CSR); in atmel_startup()
1969 * Finally, enable the serial port in atmel_startup()
1974 atmel_port->tx_stopped = false; in atmel_startup()
1976 timer_setup(&atmel_port->uart_timer, atmel_uart_timer_callback, 0); in atmel_startup()
1980 if (!atmel_port->has_hw_timer) { in atmel_startup()
1981 mod_timer(&atmel_port->uart_timer, in atmel_startup()
1985 atmel_uart_writel(port, atmel_port->rtor, in atmel_startup()
1996 if (!atmel_port->has_hw_timer) { in atmel_startup()
1997 mod_timer(&atmel_port->uart_timer, in atmel_startup()
2001 atmel_uart_writel(port, atmel_port->rtor, in atmel_startup()
2026 atmel_port->pdc_tx.ofs = 0; in atmel_flush_buffer()
2032 atmel_port->tx_len = 0; in atmel_flush_buffer()
2046 atmel_uart_writel(port, ATMEL_US_IDR, -1); in atmel_shutdown()
2049 atomic_inc(&atmel_port->tasklet_shutdown); in atmel_shutdown()
2055 del_timer_sync(&atmel_port->uart_timer); in atmel_shutdown()
2058 synchronize_irq(port->irq); in atmel_shutdown()
2064 tasklet_kill(&atmel_port->tasklet_rx); in atmel_shutdown()
2065 tasklet_kill(&atmel_port->tasklet_tx); in atmel_shutdown()
2077 * Shut-down the DMA. in atmel_shutdown()
2079 if (atmel_port->release_rx) in atmel_shutdown()
2080 atmel_port->release_rx(port); in atmel_shutdown()
2081 if (atmel_port->release_tx) in atmel_shutdown()
2082 atmel_port->release_tx(port); in atmel_shutdown()
2087 atmel_port->rx_ring.head = 0; in atmel_shutdown()
2088 atmel_port->rx_ring.tail = 0; in atmel_shutdown()
2093 free_irq(port->irq, port); in atmel_shutdown()
2109 * Enable the peripheral clock for this serial port. in atmel_serial_pm()
2112 clk_prepare_enable(atmel_port->clk); in atmel_serial_pm()
2114 /* re-enable interrupts if we disabled some on suspend */ in atmel_serial_pm()
2115 atmel_uart_writel(port, ATMEL_US_IER, atmel_port->backup_imr); in atmel_serial_pm()
2119 atmel_port->backup_imr = atmel_uart_readl(port, ATMEL_US_IMR); in atmel_serial_pm()
2120 atmel_uart_writel(port, ATMEL_US_IDR, -1); in atmel_serial_pm()
2123 * Disable the peripheral clock for this serial port. in atmel_serial_pm()
2126 clk_disable_unprepare(atmel_port->clk); in atmel_serial_pm()
2127 if (__clk_is_enabled(atmel_port->gclk)) in atmel_serial_pm()
2128 clk_disable_unprepare(atmel_port->gclk); in atmel_serial_pm()
2131 dev_err(port->dev, "atmel_serial: unknown pm %d\n", state); in atmel_serial_pm()
2152 if (atmel_port->is_usart) in atmel_set_termios()
2158 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk / 16); in atmel_set_termios()
2161 switch (termios->c_cflag & CSIZE) { in atmel_set_termios()
2177 if (termios->c_cflag & CSTOPB) in atmel_set_termios()
2181 if (termios->c_cflag & PARENB) { in atmel_set_termios()
2183 if (termios->c_cflag & CMSPAR) { in atmel_set_termios()
2184 if (termios->c_cflag & PARODD) in atmel_set_termios()
2188 } else if (termios->c_cflag & PARODD) in atmel_set_termios()
2195 spin_lock_irqsave(&port->lock, flags); in atmel_set_termios()
2197 port->read_status_mask = ATMEL_US_OVRE; in atmel_set_termios()
2198 if (termios->c_iflag & INPCK) in atmel_set_termios()
2199 port->read_status_mask |= (ATMEL_US_FRAME | ATMEL_US_PARE); in atmel_set_termios()
2200 if (termios->c_iflag & (IGNBRK | BRKINT | PARMRK)) in atmel_set_termios()
2201 port->read_status_mask |= ATMEL_US_RXBRK; in atmel_set_termios()
2205 atmel_uart_writel(port, ATMEL_US_IER, port->read_status_mask); in atmel_set_termios()
2210 port->ignore_status_mask = 0; in atmel_set_termios()
2211 if (termios->c_iflag & IGNPAR) in atmel_set_termios()
2212 port->ignore_status_mask |= (ATMEL_US_FRAME | ATMEL_US_PARE); in atmel_set_termios()
2213 if (termios->c_iflag & IGNBRK) { in atmel_set_termios()
2214 port->ignore_status_mask |= ATMEL_US_RXBRK; in atmel_set_termios()
2219 if (termios->c_iflag & IGNPAR) in atmel_set_termios()
2220 port->ignore_status_mask |= ATMEL_US_OVRE; in atmel_set_termios()
2224 /* update the per-port timeout */ in atmel_set_termios()
2225 uart_update_timeout(port, termios->c_cflag, baud); in atmel_set_termios()
2233 atmel_uart_writel(port, ATMEL_US_IDR, -1); in atmel_set_termios()
2237 atmel_port->tx_stopped = true; in atmel_set_termios()
2240 if (port->rs485.flags & SER_RS485_ENABLED) { in atmel_set_termios()
2242 port->rs485.delay_rts_after_send); in atmel_set_termios()
2244 } else if (port->iso7816.flags & SER_ISO7816_ENABLED) { in atmel_set_termios()
2245 atmel_uart_writel(port, ATMEL_US_TTGR, port->iso7816.tg); in atmel_set_termios()
2250 if ((port->iso7816.flags & SER_ISO7816_T_PARAM) in atmel_set_termios()
2255 } else if (termios->c_cflag & CRTSCTS) { in atmel_set_termios()
2258 !mctrl_gpio_to_gpiod(atmel_port->gpios, UART_GPIO_CTS)) { in atmel_set_termios()
2261 * be able to drive the RTS pin high/low when the RX in atmel_set_termios()
2267 * disabled (there must be an internal pull-up in atmel_set_termios()
2290 * baudrate = selected clock / (8 * (2 - OVER) * (CD + FP / 8)) in atmel_set_termios()
2296 if (atmel_port->has_frac_baudrate) { in atmel_set_termios()
2297 div = DIV_ROUND_CLOSEST(port->uartclk, baud * 2); in atmel_set_termios()
2312 if (atmel_port->is_usart && cd > ATMEL_US_CD) { in atmel_set_termios()
2325 if (!atmel_port->has_frac_baudrate) { in atmel_set_termios()
2326 if (__clk_is_enabled(atmel_port->gclk)) in atmel_set_termios()
2327 clk_disable_unprepare(atmel_port->gclk); in atmel_set_termios()
2328 gclk_rate = clk_round_rate(atmel_port->gclk, 16 * baud); in atmel_set_termios()
2329 actual_baud = clk_get_rate(atmel_port->clk) / (16 * cd); in atmel_set_termios()
2332 clk_set_rate(atmel_port->gclk, 16 * baud); in atmel_set_termios()
2333 ret = clk_prepare_enable(atmel_port->gclk); in atmel_set_termios()
2337 if (atmel_port->is_usart) { in atmel_set_termios()
2360 if (!(port->iso7816.flags & SER_ISO7816_ENABLED)) in atmel_set_termios()
2377 /* force RTS line to low level */ in atmel_set_termios()
2386 atmel_port->tx_stopped = false; in atmel_set_termios()
2391 /* CTS flow-control and modem-status interrupts */ in atmel_set_termios()
2392 if (UART_ENABLE_MS(port, termios->c_cflag)) in atmel_set_termios()
2397 spin_unlock_irqrestore(&port->lock, flags); in atmel_set_termios()
2402 if (termios->c_line == N_PPS) { in atmel_set_ldisc()
2403 port->flags |= UPF_HARDPPS_CD; in atmel_set_ldisc()
2404 spin_lock_irq(&port->lock); in atmel_set_ldisc()
2406 spin_unlock_irq(&port->lock); in atmel_set_ldisc()
2408 port->flags &= ~UPF_HARDPPS_CD; in atmel_set_ldisc()
2409 if (!UART_ENABLE_MS(port, termios->c_cflag)) { in atmel_set_ldisc()
2410 spin_lock_irq(&port->lock); in atmel_set_ldisc()
2412 spin_unlock_irq(&port->lock); in atmel_set_ldisc()
2422 return (port->type == PORT_ATMEL) ? "ATMEL_SERIAL" : NULL; in atmel_type()
2430 struct platform_device *mpdev = to_platform_device(port->dev->parent); in atmel_release_port()
2431 int size = resource_size(mpdev->resource); in atmel_release_port()
2433 release_mem_region(port->mapbase, size); in atmel_release_port()
2435 if (port->flags & UPF_IOREMAP) { in atmel_release_port()
2436 iounmap(port->membase); in atmel_release_port()
2437 port->membase = NULL; in atmel_release_port()
2446 struct platform_device *mpdev = to_platform_device(port->dev->parent); in atmel_request_port()
2447 int size = resource_size(mpdev->resource); in atmel_request_port()
2449 if (!request_mem_region(port->mapbase, size, "atmel_serial")) in atmel_request_port()
2450 return -EBUSY; in atmel_request_port()
2452 if (port->flags & UPF_IOREMAP) { in atmel_request_port()
2453 port->membase = ioremap(port->mapbase, size); in atmel_request_port()
2454 if (port->membase == NULL) { in atmel_request_port()
2455 release_mem_region(port->mapbase, size); in atmel_request_port()
2456 return -ENOMEM; in atmel_request_port()
2469 port->type = PORT_ATMEL; in atmel_config_port()
2480 if (ser->type != PORT_UNKNOWN && ser->type != PORT_ATMEL) in atmel_verify_port()
2481 ret = -EINVAL; in atmel_verify_port()
2482 if (port->irq != ser->irq) in atmel_verify_port()
2483 ret = -EINVAL; in atmel_verify_port()
2484 if (ser->io_type != SERIAL_IO_MEM) in atmel_verify_port()
2485 ret = -EINVAL; in atmel_verify_port()
2486 if (port->uartclk / 16 != ser->baud_base) in atmel_verify_port()
2487 ret = -EINVAL; in atmel_verify_port()
2488 if (port->mapbase != (unsigned long)ser->iomem_base) in atmel_verify_port()
2489 ret = -EINVAL; in atmel_verify_port()
2490 if (port->iobase != ser->port) in atmel_verify_port()
2491 ret = -EINVAL; in atmel_verify_port()
2492 if (ser->hub6 != 0) in atmel_verify_port()
2493 ret = -EINVAL; in atmel_verify_port()
2554 struct uart_port *port = &atmel_port->uart; in atmel_init_port()
2555 struct platform_device *mpdev = to_platform_device(pdev->dev.parent); in atmel_init_port()
2560 port->iotype = UPIO_MEM; in atmel_init_port()
2561 port->flags = UPF_BOOT_AUTOCONF | UPF_IOREMAP; in atmel_init_port()
2562 port->ops = &atmel_pops; in atmel_init_port()
2563 port->fifosize = 1; in atmel_init_port()
2564 port->dev = &pdev->dev; in atmel_init_port()
2565 port->mapbase = mpdev->resource[0].start; in atmel_init_port()
2566 port->irq = platform_get_irq(mpdev, 0); in atmel_init_port()
2567 port->rs485_config = atmel_config_rs485; in atmel_init_port()
2568 port->rs485_supported = atmel_rs485_supported; in atmel_init_port()
2569 port->iso7816_config = atmel_config_iso7816; in atmel_init_port()
2570 port->membase = NULL; in atmel_init_port()
2572 memset(&atmel_port->rx_ring, 0, sizeof(atmel_port->rx_ring)); in atmel_init_port()
2578 port->uartclk = clk_get_rate(atmel_port->clk); in atmel_init_port()
2585 atmel_port->tx_done_mask = ATMEL_US_TXEMPTY; in atmel_init_port()
2587 port->fifosize = PDC_BUFFER_SIZE; in atmel_init_port()
2588 atmel_port->tx_done_mask = ATMEL_US_ENDTX | ATMEL_US_TXBUFE; in atmel_init_port()
2590 atmel_port->tx_done_mask = ATMEL_US_TXRDY; in atmel_init_port()
2609 struct uart_port *port = &atmel_ports[co->index].uart; in atmel_console_write()
2619 ATMEL_US_RXRDY | atmel_port->tx_done_mask); in atmel_console_write()
2627 atmel_port->tx_stopped = false; in atmel_console_write()
2677 * The serial core only rounds down when matching this to a in atmel_console_get_options()
2682 *baud = port->uartclk / (16 * (quot - 1)); in atmel_console_get_options()
2687 struct uart_port *port = &atmel_ports[co->index].uart; in atmel_console_setup()
2694 if (port->membase == NULL) { in atmel_console_setup()
2695 /* Port not initialized yet - delay setup */ in atmel_console_setup()
2696 return -ENODEV; in atmel_console_setup()
2699 atmel_uart_writel(port, ATMEL_US_IDR, -1); in atmel_console_setup()
2702 atmel_port->tx_stopped = false; in atmel_console_setup()
2720 .index = -1,
2727 struct earlycon_device *dev = con->data; in atmel_serial_early_write()
2729 uart_console_write(&dev->port, s, n, atmel_console_putchar); in atmel_serial_early_write()
2735 if (!device->port.membase) in atmel_early_console_setup()
2736 return -ENODEV; in atmel_early_console_setup()
2738 device->con->write = atmel_serial_early_write; in atmel_early_console_setup()
2743 OF_EARLYCON_DECLARE(atmel_serial, "atmel,at91rm9200-usart",
2745 OF_EARLYCON_DECLARE(atmel_serial, "atmel,at91sam9260-usart",
2789 atmel_port->cache.mr = atmel_uart_readl(port, ATMEL_US_MR); in atmel_serial_suspend()
2790 atmel_port->cache.imr = atmel_uart_readl(port, ATMEL_US_IMR); in atmel_serial_suspend()
2791 atmel_port->cache.brgr = atmel_uart_readl(port, ATMEL_US_BRGR); in atmel_serial_suspend()
2792 atmel_port->cache.rtor = atmel_uart_readl(port, in atmel_serial_suspend()
2793 atmel_port->rtor); in atmel_serial_suspend()
2794 atmel_port->cache.ttgr = atmel_uart_readl(port, ATMEL_US_TTGR); in atmel_serial_suspend()
2795 atmel_port->cache.fmr = atmel_uart_readl(port, ATMEL_US_FMR); in atmel_serial_suspend()
2796 atmel_port->cache.fimr = atmel_uart_readl(port, ATMEL_US_FIMR); in atmel_serial_suspend()
2800 atmel_port->may_wakeup = device_may_wakeup(dev); in atmel_serial_suspend()
2804 spin_lock_irqsave(&atmel_port->lock_suspended, flags); in atmel_serial_suspend()
2805 atmel_port->suspended = true; in atmel_serial_suspend()
2806 spin_unlock_irqrestore(&atmel_port->lock_suspended, flags); in atmel_serial_suspend()
2822 atmel_uart_writel(port, ATMEL_US_MR, atmel_port->cache.mr); in atmel_serial_resume()
2823 atmel_uart_writel(port, ATMEL_US_IER, atmel_port->cache.imr); in atmel_serial_resume()
2824 atmel_uart_writel(port, ATMEL_US_BRGR, atmel_port->cache.brgr); in atmel_serial_resume()
2825 atmel_uart_writel(port, atmel_port->rtor, in atmel_serial_resume()
2826 atmel_port->cache.rtor); in atmel_serial_resume()
2827 atmel_uart_writel(port, ATMEL_US_TTGR, atmel_port->cache.ttgr); in atmel_serial_resume()
2829 if (atmel_port->fifo_size) { in atmel_serial_resume()
2833 atmel_port->cache.fmr); in atmel_serial_resume()
2835 atmel_port->cache.fimr); in atmel_serial_resume()
2840 spin_lock_irqsave(&atmel_port->lock_suspended, flags); in atmel_serial_resume()
2841 if (atmel_port->pending) { in atmel_serial_resume()
2842 atmel_handle_receive(port, atmel_port->pending); in atmel_serial_resume()
2843 atmel_handle_status(port, atmel_port->pending, in atmel_serial_resume()
2844 atmel_port->pending_status); in atmel_serial_resume()
2845 atmel_handle_transmit(port, atmel_port->pending); in atmel_serial_resume()
2846 atmel_port->pending = 0; in atmel_serial_resume()
2848 atmel_port->suspended = false; in atmel_serial_resume()
2849 spin_unlock_irqrestore(&atmel_port->lock_suspended, flags); in atmel_serial_resume()
2852 device_set_wakeup_enable(dev, atmel_port->may_wakeup); in atmel_serial_resume()
2860 atmel_port->fifo_size = 0; in atmel_serial_probe_fifos()
2861 atmel_port->rts_low = 0; in atmel_serial_probe_fifos()
2862 atmel_port->rts_high = 0; in atmel_serial_probe_fifos()
2864 if (of_property_read_u32(pdev->dev.of_node, in atmel_serial_probe_fifos()
2865 "atmel,fifo-size", in atmel_serial_probe_fifos()
2866 &atmel_port->fifo_size)) in atmel_serial_probe_fifos()
2869 if (!atmel_port->fifo_size) in atmel_serial_probe_fifos()
2872 if (atmel_port->fifo_size < ATMEL_MIN_FIFO_SIZE) { in atmel_serial_probe_fifos()
2873 atmel_port->fifo_size = 0; in atmel_serial_probe_fifos()
2874 dev_err(&pdev->dev, "Invalid FIFO size\n"); in atmel_serial_probe_fifos()
2886 atmel_port->rts_high = max_t(int, atmel_port->fifo_size >> 1, in atmel_serial_probe_fifos()
2887 atmel_port->fifo_size - ATMEL_RTS_HIGH_OFFSET); in atmel_serial_probe_fifos()
2888 atmel_port->rts_low = max_t(int, atmel_port->fifo_size >> 2, in atmel_serial_probe_fifos()
2889 atmel_port->fifo_size - ATMEL_RTS_LOW_OFFSET); in atmel_serial_probe_fifos()
2891 dev_info(&pdev->dev, "Using FIFO (%u data)\n", in atmel_serial_probe_fifos()
2892 atmel_port->fifo_size); in atmel_serial_probe_fifos()
2893 dev_dbg(&pdev->dev, "RTS High Threshold : %2u data\n", in atmel_serial_probe_fifos()
2894 atmel_port->rts_high); in atmel_serial_probe_fifos()
2895 dev_dbg(&pdev->dev, "RTS Low Threshold : %2u data\n", in atmel_serial_probe_fifos()
2896 atmel_port->rts_low); in atmel_serial_probe_fifos()
2902 struct device_node *np = pdev->dev.parent->of_node; in atmel_serial_probe()
2907 BUILD_BUG_ON(ATMEL_SERIAL_RINGSIZE & (ATMEL_SERIAL_RINGSIZE - 1)); in atmel_serial_probe()
2910 * In device tree there is no node with "atmel,at91rm9200-usart-serial" in atmel_serial_probe()
2911 * as compatible string. This driver is probed by at91-usart mfd driver in atmel_serial_probe()
2913 * spi-at91-usart driver. All attributes needed by this driver are in atmel_serial_probe()
2916 pdev->dev.of_node = np; in atmel_serial_probe()
2918 ret = of_alias_get_id(np, "serial"); in atmel_serial_probe()
2920 /* port id not found in platform data nor device-tree aliases: in atmel_serial_probe()
2921 * auto-enumerate it */ in atmel_serial_probe()
2925 ret = -ENODEV; in atmel_serial_probe()
2931 ret = -EBUSY; in atmel_serial_probe()
2936 atmel_port->backup_imr = 0; in atmel_serial_probe()
2937 atmel_port->uart.line = ret; in atmel_serial_probe()
2938 atmel_port->uart.has_sysrq = IS_ENABLED(CONFIG_SERIAL_ATMEL_CONSOLE); in atmel_serial_probe()
2941 atomic_set(&atmel_port->tasklet_shutdown, 0); in atmel_serial_probe()
2942 spin_lock_init(&atmel_port->lock_suspended); in atmel_serial_probe()
2944 atmel_port->clk = devm_clk_get(&pdev->dev, "usart"); in atmel_serial_probe()
2945 if (IS_ERR(atmel_port->clk)) { in atmel_serial_probe()
2946 ret = PTR_ERR(atmel_port->clk); in atmel_serial_probe()
2949 ret = clk_prepare_enable(atmel_port->clk); in atmel_serial_probe()
2953 atmel_port->gclk = devm_clk_get_optional(&pdev->dev, "gclk"); in atmel_serial_probe()
2954 if (IS_ERR(atmel_port->gclk)) { in atmel_serial_probe()
2955 ret = PTR_ERR(atmel_port->gclk); in atmel_serial_probe()
2963 atmel_port->gpios = mctrl_gpio_init(&atmel_port->uart, 0); in atmel_serial_probe()
2964 if (IS_ERR(atmel_port->gpios)) { in atmel_serial_probe()
2965 ret = PTR_ERR(atmel_port->gpios); in atmel_serial_probe()
2969 if (!atmel_use_pdc_rx(&atmel_port->uart)) { in atmel_serial_probe()
2970 ret = -ENOMEM; in atmel_serial_probe()
2976 atmel_port->rx_ring.buf = data; in atmel_serial_probe()
2979 rs485_enabled = atmel_port->uart.rs485.flags & SER_RS485_ENABLED; in atmel_serial_probe()
2981 ret = uart_add_one_port(&atmel_uart, &atmel_port->uart); in atmel_serial_probe()
2985 device_init_wakeup(&pdev->dev, 1); in atmel_serial_probe()
2989 atmel_uart_writel(&atmel_port->uart, ATMEL_US_MR, in atmel_serial_probe()
2991 atmel_uart_writel(&atmel_port->uart, ATMEL_US_CR, in atmel_serial_probe()
2998 atmel_get_ip_name(&atmel_port->uart); in atmel_serial_probe()
3004 clk_disable_unprepare(atmel_port->clk); in atmel_serial_probe()
3009 kfree(atmel_port->rx_ring.buf); in atmel_serial_probe()
3010 atmel_port->rx_ring.buf = NULL; in atmel_serial_probe()
3012 clk_disable_unprepare(atmel_port->clk); in atmel_serial_probe()
3013 clear_bit(atmel_port->uart.line, atmel_ports_in_use); in atmel_serial_probe()
3024 * protocol that needs bitbanging on IO lines, but use the regular serial
3033 tasklet_kill(&atmel_port->tasklet_rx); in atmel_serial_remove()
3034 tasklet_kill(&atmel_port->tasklet_tx); in atmel_serial_remove()
3036 device_init_wakeup(&pdev->dev, 0); in atmel_serial_remove()
3040 kfree(atmel_port->rx_ring.buf); in atmel_serial_remove()
3044 clear_bit(port->line, atmel_ports_in_use); in atmel_serial_remove()
3046 pdev->dev.of_node = NULL; in atmel_serial_remove()