1e3b3d0f5SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0+ 2ab4382d2SGreg Kroah-Hartman /* 372ce5732SAndy Shevchenko * Driver for Atmel AT91 Serial ports 4ab4382d2SGreg Kroah-Hartman * Copyright (C) 2003 Rick Bronson 5ab4382d2SGreg Kroah-Hartman * 6ab4382d2SGreg Kroah-Hartman * Based on drivers/char/serial_sa1100.c, by Deep Blue Solutions Ltd. 7ab4382d2SGreg Kroah-Hartman * Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o. 8ab4382d2SGreg Kroah-Hartman * 9ab4382d2SGreg Kroah-Hartman * DMA support added by Chip Coldwell. 10ab4382d2SGreg Kroah-Hartman */ 11702d10a0SJiri Slaby #include <linux/circ_buf.h> 12ab4382d2SGreg Kroah-Hartman #include <linux/tty.h> 13ab4382d2SGreg Kroah-Hartman #include <linux/ioport.h> 14ab4382d2SGreg Kroah-Hartman #include <linux/slab.h> 15ab4382d2SGreg Kroah-Hartman #include <linux/init.h> 16ab4382d2SGreg Kroah-Hartman #include <linux/serial.h> 17ab4382d2SGreg Kroah-Hartman #include <linux/clk.h> 18ab4382d2SGreg Kroah-Hartman #include <linux/console.h> 19ab4382d2SGreg Kroah-Hartman #include <linux/sysrq.h> 20ab4382d2SGreg Kroah-Hartman #include <linux/tty_flip.h> 21ab4382d2SGreg Kroah-Hartman #include <linux/platform_device.h> 225fbe46b6SNicolas Ferre #include <linux/of.h> 235fbe46b6SNicolas Ferre #include <linux/of_device.h> 24ab4382d2SGreg Kroah-Hartman #include <linux/dma-mapping.h> 256b997babSVinod Koul #include <linux/dmaengine.h> 26ab4382d2SGreg Kroah-Hartman #include <linux/atmel_pdc.h> 27ab4382d2SGreg Kroah-Hartman #include <linux/uaccess.h> 28bcd2360cSJean-Christophe PLAGNIOL-VILLARD #include <linux/platform_data/atmel.h> 292e68c22fSElen Song #include <linux/timer.h> 30e0b0baadSRichard Genoud #include <linux/err.h> 31ab5e4e41SRichard Genoud #include <linux/irq.h> 322c7af5baSBoris BREZILLON #include <linux/suspend.h> 332b5cf14bSGeliang Tang #include <linux/mm.h> 34635b2589SZihao Tang #include <linux/io.h> 35ab4382d2SGreg Kroah-Hartman 36377fedd1SNicolas Ferre #include <asm/div64.h> 37ab4382d2SGreg Kroah-Hartman #include <asm/ioctls.h> 38ab4382d2SGreg Kroah-Hartman 39ab4382d2SGreg Kroah-Hartman #define PDC_BUFFER_SIZE 512 40ab4382d2SGreg Kroah-Hartman /* Revisit: We should calculate this based on the actual port settings */ 41ab4382d2SGreg Kroah-Hartman #define PDC_RX_TIMEOUT (3 * 10) /* 3 bytes */ 42ab4382d2SGreg Kroah-Hartman 43b5199d46SCyrille Pitchen /* The minium number of data FIFOs should be able to contain */ 44b5199d46SCyrille Pitchen #define ATMEL_MIN_FIFO_SIZE 8 45b5199d46SCyrille Pitchen /* 46b5199d46SCyrille Pitchen * These two offsets are substracted from the RX FIFO size to define the RTS 47b5199d46SCyrille Pitchen * high and low thresholds 48b5199d46SCyrille Pitchen */ 49b5199d46SCyrille Pitchen #define ATMEL_RTS_HIGH_OFFSET 16 50b5199d46SCyrille Pitchen #define ATMEL_RTS_LOW_OFFSET 20 51b5199d46SCyrille Pitchen 52ab4382d2SGreg Kroah-Hartman #include <linux/serial_core.h> 53ab4382d2SGreg Kroah-Hartman 54e0b0baadSRichard Genoud #include "serial_mctrl_gpio.h" 558961df89SRichard Genoud #include "atmel_serial.h" 56e0b0baadSRichard Genoud 57ab4382d2SGreg Kroah-Hartman static void atmel_start_rx(struct uart_port *port); 58ab4382d2SGreg Kroah-Hartman static void atmel_stop_rx(struct uart_port *port); 59ab4382d2SGreg Kroah-Hartman 60ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_SERIAL_ATMEL_TTYAT 61ab4382d2SGreg Kroah-Hartman 62ab4382d2SGreg Kroah-Hartman /* Use device name ttyAT, major 204 and minor 154-169. This is necessary if we 63ab4382d2SGreg Kroah-Hartman * should coexist with the 8250 driver, such as if we have an external 16C550 64ab4382d2SGreg Kroah-Hartman * UART. */ 65ab4382d2SGreg Kroah-Hartman #define SERIAL_ATMEL_MAJOR 204 66ab4382d2SGreg Kroah-Hartman #define MINOR_START 154 67ab4382d2SGreg Kroah-Hartman #define ATMEL_DEVICENAME "ttyAT" 68ab4382d2SGreg Kroah-Hartman 69ab4382d2SGreg Kroah-Hartman #else 70ab4382d2SGreg Kroah-Hartman 71ab4382d2SGreg Kroah-Hartman /* Use device name ttyS, major 4, minor 64-68. This is the usual serial port 72ab4382d2SGreg Kroah-Hartman * name, but it is legally reserved for the 8250 driver. */ 73ab4382d2SGreg Kroah-Hartman #define SERIAL_ATMEL_MAJOR TTY_MAJOR 74ab4382d2SGreg Kroah-Hartman #define MINOR_START 64 75ab4382d2SGreg Kroah-Hartman #define ATMEL_DEVICENAME "ttyS" 76ab4382d2SGreg Kroah-Hartman 77ab4382d2SGreg Kroah-Hartman #endif 78ab4382d2SGreg Kroah-Hartman 79ab4382d2SGreg Kroah-Hartman #define ATMEL_ISR_PASS_LIMIT 256 80ab4382d2SGreg Kroah-Hartman 81ab4382d2SGreg Kroah-Hartman struct atmel_dma_buffer { 82ab4382d2SGreg Kroah-Hartman unsigned char *buf; 83ab4382d2SGreg Kroah-Hartman dma_addr_t dma_addr; 84ab4382d2SGreg Kroah-Hartman unsigned int dma_size; 85ab4382d2SGreg Kroah-Hartman unsigned int ofs; 86ab4382d2SGreg Kroah-Hartman }; 87ab4382d2SGreg Kroah-Hartman 88ab4382d2SGreg Kroah-Hartman struct atmel_uart_char { 89ab4382d2SGreg Kroah-Hartman u16 status; 90ab4382d2SGreg Kroah-Hartman u16 ch; 91ab4382d2SGreg Kroah-Hartman }; 92ab4382d2SGreg Kroah-Hartman 93637ba54fSLudovic Desroches /* 94637ba54fSLudovic Desroches * Be careful, the real size of the ring buffer is 95637ba54fSLudovic Desroches * sizeof(atmel_uart_char) * ATMEL_SERIAL_RINGSIZE. It means that ring buffer 96637ba54fSLudovic Desroches * can contain up to 1024 characters in PIO mode and up to 4096 characters in 97637ba54fSLudovic Desroches * DMA mode. 98637ba54fSLudovic Desroches */ 99ab4382d2SGreg Kroah-Hartman #define ATMEL_SERIAL_RINGSIZE 1024 100ab4382d2SGreg Kroah-Hartman 101ab4382d2SGreg Kroah-Hartman /* 1029af92fbfSAlexandre Belloni * at91: 6 USARTs and one DBGU port (SAM9260) 103432f9748SAlexandre Belloni * samx7: 3 USARTs and 5 UARTs 1049af92fbfSAlexandre Belloni */ 105432f9748SAlexandre Belloni #define ATMEL_MAX_UART 8 1069af92fbfSAlexandre Belloni 1079af92fbfSAlexandre Belloni /* 108ab4382d2SGreg Kroah-Hartman * We wrap our port structure around the generic uart_port. 109ab4382d2SGreg Kroah-Hartman */ 110ab4382d2SGreg Kroah-Hartman struct atmel_uart_port { 111ab4382d2SGreg Kroah-Hartman struct uart_port uart; /* uart */ 112ab4382d2SGreg Kroah-Hartman struct clk *clk; /* uart clock */ 113ab4382d2SGreg Kroah-Hartman int may_wakeup; /* cached value of device_may_wakeup for times we need to disable it */ 114ab4382d2SGreg Kroah-Hartman u32 backup_imr; /* IMR saved during suspend */ 115ab4382d2SGreg Kroah-Hartman int break_active; /* break being received */ 116ab4382d2SGreg Kroah-Hartman 11734df42f5SElen Song bool use_dma_rx; /* enable DMA receiver */ 11864e22ebeSElen Song bool use_pdc_rx; /* enable PDC receiver */ 119ab4382d2SGreg Kroah-Hartman short pdc_rx_idx; /* current PDC RX buffer */ 120ab4382d2SGreg Kroah-Hartman struct atmel_dma_buffer pdc_rx[2]; /* PDC receier */ 121ab4382d2SGreg Kroah-Hartman 12208f738beSElen Song bool use_dma_tx; /* enable DMA transmitter */ 12364e22ebeSElen Song bool use_pdc_tx; /* enable PDC transmitter */ 124ab4382d2SGreg Kroah-Hartman struct atmel_dma_buffer pdc_tx; /* PDC transmitter */ 125ab4382d2SGreg Kroah-Hartman 12608f738beSElen Song spinlock_t lock_tx; /* port lock */ 12734df42f5SElen Song spinlock_t lock_rx; /* port lock */ 12808f738beSElen Song struct dma_chan *chan_tx; 12934df42f5SElen Song struct dma_chan *chan_rx; 13008f738beSElen Song struct dma_async_tx_descriptor *desc_tx; 13134df42f5SElen Song struct dma_async_tx_descriptor *desc_rx; 13208f738beSElen Song dma_cookie_t cookie_tx; 13334df42f5SElen Song dma_cookie_t cookie_rx; 13408f738beSElen Song struct scatterlist sg_tx; 13534df42f5SElen Song struct scatterlist sg_rx; 13600e8e658SNicolas Ferre struct tasklet_struct tasklet_rx; 13700e8e658SNicolas Ferre struct tasklet_struct tasklet_tx; 13898f2082cSNicolas Ferre atomic_t tasklet_shutdown; 139ab4382d2SGreg Kroah-Hartman unsigned int irq_status_prev; 1405f258b3eSCyrille Pitchen unsigned int tx_len; 141ab4382d2SGreg Kroah-Hartman 142ab4382d2SGreg Kroah-Hartman struct circ_buf rx_ring; 143ab4382d2SGreg Kroah-Hartman 144e0b0baadSRichard Genoud struct mctrl_gpios *gpios; 145377fedd1SNicolas Ferre u32 backup_mode; /* MR saved during iso7816 operations */ 146377fedd1SNicolas Ferre u32 backup_brgr; /* BRGR saved during iso7816 operations */ 147ab4382d2SGreg Kroah-Hartman unsigned int tx_done_mask; 148b5199d46SCyrille Pitchen u32 fifo_size; 149b5199d46SCyrille Pitchen u32 rts_high; 150b5199d46SCyrille Pitchen u32 rts_low; 151ab5e4e41SRichard Genoud bool ms_irq_enabled; 1522958cceeSLudovic Desroches u32 rtor; /* address of receiver timeout register if it exists */ 153*5644bf18SSergiu Moga bool is_usart; 1545bf5635aSLudovic Desroches bool has_frac_baudrate; 1554b769371SNicolas Ferre bool has_hw_timer; 1564b769371SNicolas Ferre struct timer_list uart_timer; 1572c7af5baSBoris BREZILLON 158ea04f82aSRomain Izard bool tx_stopped; 1592c7af5baSBoris BREZILLON bool suspended; 1602c7af5baSBoris BREZILLON unsigned int pending; 1612c7af5baSBoris BREZILLON unsigned int pending_status; 1622c7af5baSBoris BREZILLON spinlock_t lock_suspended; 1632c7af5baSBoris BREZILLON 16469646d7aSRazvan Stefanescu bool hd_start_rx; /* can start RX during half-duplex operation */ 16569646d7aSRazvan Stefanescu 166377fedd1SNicolas Ferre /* ISO7816 */ 167377fedd1SNicolas Ferre unsigned int fidi_min; 168377fedd1SNicolas Ferre unsigned int fidi_max; 169377fedd1SNicolas Ferre 1706a5f0e2fSAlexandre Belloni struct { 1716a5f0e2fSAlexandre Belloni u32 cr; 1726a5f0e2fSAlexandre Belloni u32 mr; 1736a5f0e2fSAlexandre Belloni u32 imr; 1746a5f0e2fSAlexandre Belloni u32 brgr; 1756a5f0e2fSAlexandre Belloni u32 rtor; 1766a5f0e2fSAlexandre Belloni u32 ttgr; 1776a5f0e2fSAlexandre Belloni u32 fmr; 1786a5f0e2fSAlexandre Belloni u32 fimr; 1796a5f0e2fSAlexandre Belloni } cache; 1806a5f0e2fSAlexandre Belloni 181a930e528SElen Song int (*prepare_rx)(struct uart_port *port); 182a930e528SElen Song int (*prepare_tx)(struct uart_port *port); 183a930e528SElen Song void (*schedule_rx)(struct uart_port *port); 184a930e528SElen Song void (*schedule_tx)(struct uart_port *port); 185a930e528SElen Song void (*release_rx)(struct uart_port *port); 186a930e528SElen Song void (*release_tx)(struct uart_port *port); 187ab4382d2SGreg Kroah-Hartman }; 188ab4382d2SGreg Kroah-Hartman 189ab4382d2SGreg Kroah-Hartman static struct atmel_uart_port atmel_ports[ATMEL_MAX_UART]; 190503bded9SPawel Wieczorkiewicz static DECLARE_BITMAP(atmel_ports_in_use, ATMEL_MAX_UART); 191ab4382d2SGreg Kroah-Hartman 1925fbe46b6SNicolas Ferre #if defined(CONFIG_OF) 1935fbe46b6SNicolas Ferre static const struct of_device_id atmel_serial_dt_ids[] = { 194c24d2531SRadu Pirea { .compatible = "atmel,at91rm9200-usart-serial" }, 1955fbe46b6SNicolas Ferre { /* sentinel */ } 1965fbe46b6SNicolas Ferre }; 1975fbe46b6SNicolas Ferre #endif 1985fbe46b6SNicolas Ferre 199ab4382d2SGreg Kroah-Hartman static inline struct atmel_uart_port * 200ab4382d2SGreg Kroah-Hartman to_atmel_uart_port(struct uart_port *uart) 201ab4382d2SGreg Kroah-Hartman { 202ab4382d2SGreg Kroah-Hartman return container_of(uart, struct atmel_uart_port, uart); 203ab4382d2SGreg Kroah-Hartman } 204ab4382d2SGreg Kroah-Hartman 2054e7decdaSCyrille Pitchen static inline u32 atmel_uart_readl(struct uart_port *port, u32 reg) 2064e7decdaSCyrille Pitchen { 2074e7decdaSCyrille Pitchen return __raw_readl(port->membase + reg); 2084e7decdaSCyrille Pitchen } 2094e7decdaSCyrille Pitchen 2104e7decdaSCyrille Pitchen static inline void atmel_uart_writel(struct uart_port *port, u32 reg, u32 value) 2114e7decdaSCyrille Pitchen { 2124e7decdaSCyrille Pitchen __raw_writel(value, port->membase + reg); 2134e7decdaSCyrille Pitchen } 2144e7decdaSCyrille Pitchen 215a6499435SCyrille Pitchen static inline u8 atmel_uart_read_char(struct uart_port *port) 216a6499435SCyrille Pitchen { 217a6499435SCyrille Pitchen return __raw_readb(port->membase + ATMEL_US_RHR); 218a6499435SCyrille Pitchen } 219a6499435SCyrille Pitchen 220a6499435SCyrille Pitchen static inline void atmel_uart_write_char(struct uart_port *port, u8 value) 221a6499435SCyrille Pitchen { 222a6499435SCyrille Pitchen __raw_writeb(value, port->membase + ATMEL_US_THR); 223a6499435SCyrille Pitchen } 224a6499435SCyrille Pitchen 225f3040983SRazvan Stefanescu static inline int atmel_uart_is_half_duplex(struct uart_port *port) 226f3040983SRazvan Stefanescu { 227f3040983SRazvan Stefanescu return ((port->rs485.flags & SER_RS485_ENABLED) && 228f3040983SRazvan Stefanescu !(port->rs485.flags & SER_RS485_RX_DURING_TX)) || 229f3040983SRazvan Stefanescu (port->iso7816.flags & SER_ISO7816_ENABLED); 230f3040983SRazvan Stefanescu } 231f3040983SRazvan Stefanescu 232ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_SERIAL_ATMEL_PDC 23364e22ebeSElen Song static bool atmel_use_pdc_rx(struct uart_port *port) 234ab4382d2SGreg Kroah-Hartman { 235ab4382d2SGreg Kroah-Hartman struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 236ab4382d2SGreg Kroah-Hartman 23764e22ebeSElen Song return atmel_port->use_pdc_rx; 238ab4382d2SGreg Kroah-Hartman } 239ab4382d2SGreg Kroah-Hartman 24064e22ebeSElen Song static bool atmel_use_pdc_tx(struct uart_port *port) 241ab4382d2SGreg Kroah-Hartman { 242ab4382d2SGreg Kroah-Hartman struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 243ab4382d2SGreg Kroah-Hartman 24464e22ebeSElen Song return atmel_port->use_pdc_tx; 245ab4382d2SGreg Kroah-Hartman } 246ab4382d2SGreg Kroah-Hartman #else 24764e22ebeSElen Song static bool atmel_use_pdc_rx(struct uart_port *port) 248ab4382d2SGreg Kroah-Hartman { 249ab4382d2SGreg Kroah-Hartman return false; 250ab4382d2SGreg Kroah-Hartman } 251ab4382d2SGreg Kroah-Hartman 25264e22ebeSElen Song static bool atmel_use_pdc_tx(struct uart_port *port) 253ab4382d2SGreg Kroah-Hartman { 254ab4382d2SGreg Kroah-Hartman return false; 255ab4382d2SGreg Kroah-Hartman } 256ab4382d2SGreg Kroah-Hartman #endif 257ab4382d2SGreg Kroah-Hartman 25808f738beSElen Song static bool atmel_use_dma_tx(struct uart_port *port) 25908f738beSElen Song { 26008f738beSElen Song struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 26108f738beSElen Song 26208f738beSElen Song return atmel_port->use_dma_tx; 26308f738beSElen Song } 26408f738beSElen Song 26534df42f5SElen Song static bool atmel_use_dma_rx(struct uart_port *port) 26634df42f5SElen Song { 26734df42f5SElen Song struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 26834df42f5SElen Song 26934df42f5SElen Song return atmel_port->use_dma_rx; 27034df42f5SElen Song } 27134df42f5SElen Song 2725be605acSAlexandre Belloni static bool atmel_use_fifo(struct uart_port *port) 2735be605acSAlexandre Belloni { 2745be605acSAlexandre Belloni struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 2755be605acSAlexandre Belloni 2765be605acSAlexandre Belloni return atmel_port->fifo_size; 2775be605acSAlexandre Belloni } 2785be605acSAlexandre Belloni 27998f2082cSNicolas Ferre static void atmel_tasklet_schedule(struct atmel_uart_port *atmel_port, 28098f2082cSNicolas Ferre struct tasklet_struct *t) 28198f2082cSNicolas Ferre { 28298f2082cSNicolas Ferre if (!atomic_read(&atmel_port->tasklet_shutdown)) 28398f2082cSNicolas Ferre tasklet_schedule(t); 28498f2082cSNicolas Ferre } 28598f2082cSNicolas Ferre 286ab4382d2SGreg Kroah-Hartman /* Enable or disable the rs485 support */ 287ae50bb27SIlpo Järvinen static int atmel_config_rs485(struct uart_port *port, struct ktermios *termios, 28813bd3e6fSRicardo Ribalda Delgado struct serial_rs485 *rs485conf) 289ab4382d2SGreg Kroah-Hartman { 290ab4382d2SGreg Kroah-Hartman struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 291ab4382d2SGreg Kroah-Hartman unsigned int mode; 292ab4382d2SGreg Kroah-Hartman 293ab4382d2SGreg Kroah-Hartman /* Disable interrupts */ 2944e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_IDR, atmel_port->tx_done_mask); 295ab4382d2SGreg Kroah-Hartman 2964e7decdaSCyrille Pitchen mode = atmel_uart_readl(port, ATMEL_US_MR); 297ab4382d2SGreg Kroah-Hartman 298ab4382d2SGreg Kroah-Hartman if (rs485conf->flags & SER_RS485_ENABLED) { 299ab4382d2SGreg Kroah-Hartman dev_dbg(port->dev, "Setting UART to RS485\n"); 30060efd051SLino Sanfilippo if (rs485conf->flags & SER_RS485_RX_DURING_TX) 301477b8383SCodrin.Ciubotariu@microchip.com atmel_port->tx_done_mask = ATMEL_US_TXRDY; 302477b8383SCodrin.Ciubotariu@microchip.com else 303ab4382d2SGreg Kroah-Hartman atmel_port->tx_done_mask = ATMEL_US_TXEMPTY; 304477b8383SCodrin.Ciubotariu@microchip.com 3054e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_TTGR, 3064e7decdaSCyrille Pitchen rs485conf->delay_rts_after_send); 307692a8ebcSSergiu Moga mode &= ~ATMEL_US_USMODE; 308ab4382d2SGreg Kroah-Hartman mode |= ATMEL_US_USMODE_RS485; 309ab4382d2SGreg Kroah-Hartman } else { 310ab4382d2SGreg Kroah-Hartman dev_dbg(port->dev, "Setting UART to RS232\n"); 31164e22ebeSElen Song if (atmel_use_pdc_tx(port)) 312ab4382d2SGreg Kroah-Hartman atmel_port->tx_done_mask = ATMEL_US_ENDTX | 313ab4382d2SGreg Kroah-Hartman ATMEL_US_TXBUFE; 314ab4382d2SGreg Kroah-Hartman else 315ab4382d2SGreg Kroah-Hartman atmel_port->tx_done_mask = ATMEL_US_TXRDY; 316ab4382d2SGreg Kroah-Hartman } 3174e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_MR, mode); 318ab4382d2SGreg Kroah-Hartman 319ab4382d2SGreg Kroah-Hartman /* Enable interrupts */ 3204e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_IER, atmel_port->tx_done_mask); 321ab4382d2SGreg Kroah-Hartman 32213bd3e6fSRicardo Ribalda Delgado return 0; 323ab4382d2SGreg Kroah-Hartman } 324ab4382d2SGreg Kroah-Hartman 325377fedd1SNicolas Ferre static unsigned int atmel_calc_cd(struct uart_port *port, 326377fedd1SNicolas Ferre struct serial_iso7816 *iso7816conf) 327377fedd1SNicolas Ferre { 328377fedd1SNicolas Ferre struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 329377fedd1SNicolas Ferre unsigned int cd; 330377fedd1SNicolas Ferre u64 mck_rate; 331377fedd1SNicolas Ferre 332377fedd1SNicolas Ferre mck_rate = (u64)clk_get_rate(atmel_port->clk); 333377fedd1SNicolas Ferre do_div(mck_rate, iso7816conf->clk); 334377fedd1SNicolas Ferre cd = mck_rate; 335377fedd1SNicolas Ferre return cd; 336377fedd1SNicolas Ferre } 337377fedd1SNicolas Ferre 338377fedd1SNicolas Ferre static unsigned int atmel_calc_fidi(struct uart_port *port, 339377fedd1SNicolas Ferre struct serial_iso7816 *iso7816conf) 340377fedd1SNicolas Ferre { 341377fedd1SNicolas Ferre u64 fidi = 0; 342377fedd1SNicolas Ferre 343377fedd1SNicolas Ferre if (iso7816conf->sc_fi && iso7816conf->sc_di) { 344377fedd1SNicolas Ferre fidi = (u64)iso7816conf->sc_fi; 345377fedd1SNicolas Ferre do_div(fidi, iso7816conf->sc_di); 346377fedd1SNicolas Ferre } 347377fedd1SNicolas Ferre return (u32)fidi; 348377fedd1SNicolas Ferre } 349377fedd1SNicolas Ferre 350377fedd1SNicolas Ferre /* Enable or disable the iso7816 support */ 351377fedd1SNicolas Ferre /* Called with interrupts disabled */ 352377fedd1SNicolas Ferre static int atmel_config_iso7816(struct uart_port *port, 353377fedd1SNicolas Ferre struct serial_iso7816 *iso7816conf) 354377fedd1SNicolas Ferre { 355377fedd1SNicolas Ferre struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 356377fedd1SNicolas Ferre unsigned int mode; 357377fedd1SNicolas Ferre unsigned int cd, fidi; 358377fedd1SNicolas Ferre int ret = 0; 359377fedd1SNicolas Ferre 360377fedd1SNicolas Ferre /* Disable interrupts */ 361377fedd1SNicolas Ferre atmel_uart_writel(port, ATMEL_US_IDR, atmel_port->tx_done_mask); 362377fedd1SNicolas Ferre 363377fedd1SNicolas Ferre mode = atmel_uart_readl(port, ATMEL_US_MR); 364377fedd1SNicolas Ferre 365377fedd1SNicolas Ferre if (iso7816conf->flags & SER_ISO7816_ENABLED) { 366377fedd1SNicolas Ferre mode &= ~ATMEL_US_USMODE; 367377fedd1SNicolas Ferre 368377fedd1SNicolas Ferre if (iso7816conf->tg > 255) { 369377fedd1SNicolas Ferre dev_err(port->dev, "ISO7816: Timeguard exceeding 255\n"); 370377fedd1SNicolas Ferre memset(iso7816conf, 0, sizeof(struct serial_iso7816)); 371377fedd1SNicolas Ferre ret = -EINVAL; 372377fedd1SNicolas Ferre goto err_out; 373377fedd1SNicolas Ferre } 374377fedd1SNicolas Ferre 375377fedd1SNicolas Ferre if ((iso7816conf->flags & SER_ISO7816_T_PARAM) 376377fedd1SNicolas Ferre == SER_ISO7816_T(0)) { 377377fedd1SNicolas Ferre mode |= ATMEL_US_USMODE_ISO7816_T0 | ATMEL_US_DSNACK; 378377fedd1SNicolas Ferre } else if ((iso7816conf->flags & SER_ISO7816_T_PARAM) 379377fedd1SNicolas Ferre == SER_ISO7816_T(1)) { 380377fedd1SNicolas Ferre mode |= ATMEL_US_USMODE_ISO7816_T1 | ATMEL_US_INACK; 381377fedd1SNicolas Ferre } else { 382377fedd1SNicolas Ferre dev_err(port->dev, "ISO7816: Type not supported\n"); 383377fedd1SNicolas Ferre memset(iso7816conf, 0, sizeof(struct serial_iso7816)); 384377fedd1SNicolas Ferre ret = -EINVAL; 385377fedd1SNicolas Ferre goto err_out; 386377fedd1SNicolas Ferre } 387377fedd1SNicolas Ferre 388377fedd1SNicolas Ferre mode &= ~(ATMEL_US_USCLKS | ATMEL_US_NBSTOP | ATMEL_US_PAR); 389377fedd1SNicolas Ferre 390377fedd1SNicolas Ferre /* select mck clock, and output */ 391377fedd1SNicolas Ferre mode |= ATMEL_US_USCLKS_MCK | ATMEL_US_CLKO; 392377fedd1SNicolas Ferre /* set parity for normal/inverse mode + max iterations */ 393377fedd1SNicolas Ferre mode |= ATMEL_US_PAR_EVEN | ATMEL_US_NBSTOP_1 | ATMEL_US_MAX_ITER(3); 394377fedd1SNicolas Ferre 395377fedd1SNicolas Ferre cd = atmel_calc_cd(port, iso7816conf); 396377fedd1SNicolas Ferre fidi = atmel_calc_fidi(port, iso7816conf); 397377fedd1SNicolas Ferre if (fidi == 0) { 398377fedd1SNicolas Ferre dev_warn(port->dev, "ISO7816 fidi = 0, Generator generates no signal\n"); 399377fedd1SNicolas Ferre } else if (fidi < atmel_port->fidi_min 400377fedd1SNicolas Ferre || fidi > atmel_port->fidi_max) { 401377fedd1SNicolas Ferre dev_err(port->dev, "ISO7816 fidi = %u, value not supported\n", fidi); 402377fedd1SNicolas Ferre memset(iso7816conf, 0, sizeof(struct serial_iso7816)); 403377fedd1SNicolas Ferre ret = -EINVAL; 404377fedd1SNicolas Ferre goto err_out; 405377fedd1SNicolas Ferre } 406377fedd1SNicolas Ferre 407377fedd1SNicolas Ferre if (!(port->iso7816.flags & SER_ISO7816_ENABLED)) { 408377fedd1SNicolas Ferre /* port not yet in iso7816 mode: store configuration */ 409377fedd1SNicolas Ferre atmel_port->backup_mode = atmel_uart_readl(port, ATMEL_US_MR); 410377fedd1SNicolas Ferre atmel_port->backup_brgr = atmel_uart_readl(port, ATMEL_US_BRGR); 411377fedd1SNicolas Ferre } 412377fedd1SNicolas Ferre 413377fedd1SNicolas Ferre atmel_uart_writel(port, ATMEL_US_TTGR, iso7816conf->tg); 414377fedd1SNicolas Ferre atmel_uart_writel(port, ATMEL_US_BRGR, cd); 415377fedd1SNicolas Ferre atmel_uart_writel(port, ATMEL_US_FIDI, fidi); 416377fedd1SNicolas Ferre 417377fedd1SNicolas Ferre atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_TXDIS | ATMEL_US_RXEN); 418377fedd1SNicolas Ferre atmel_port->tx_done_mask = ATMEL_US_TXEMPTY | ATMEL_US_NACK | ATMEL_US_ITERATION; 419377fedd1SNicolas Ferre } else { 420377fedd1SNicolas Ferre dev_dbg(port->dev, "Setting UART back to RS232\n"); 421377fedd1SNicolas Ferre /* back to last RS232 settings */ 422377fedd1SNicolas Ferre mode = atmel_port->backup_mode; 423377fedd1SNicolas Ferre memset(iso7816conf, 0, sizeof(struct serial_iso7816)); 424377fedd1SNicolas Ferre atmel_uart_writel(port, ATMEL_US_TTGR, 0); 425377fedd1SNicolas Ferre atmel_uart_writel(port, ATMEL_US_BRGR, atmel_port->backup_brgr); 426377fedd1SNicolas Ferre atmel_uart_writel(port, ATMEL_US_FIDI, 0x174); 427377fedd1SNicolas Ferre 428377fedd1SNicolas Ferre if (atmel_use_pdc_tx(port)) 429377fedd1SNicolas Ferre atmel_port->tx_done_mask = ATMEL_US_ENDTX | 430377fedd1SNicolas Ferre ATMEL_US_TXBUFE; 431377fedd1SNicolas Ferre else 432377fedd1SNicolas Ferre atmel_port->tx_done_mask = ATMEL_US_TXRDY; 433377fedd1SNicolas Ferre } 434377fedd1SNicolas Ferre 435377fedd1SNicolas Ferre port->iso7816 = *iso7816conf; 436377fedd1SNicolas Ferre 437377fedd1SNicolas Ferre atmel_uart_writel(port, ATMEL_US_MR, mode); 438377fedd1SNicolas Ferre 439377fedd1SNicolas Ferre err_out: 440377fedd1SNicolas Ferre /* Enable interrupts */ 441377fedd1SNicolas Ferre atmel_uart_writel(port, ATMEL_US_IER, atmel_port->tx_done_mask); 442377fedd1SNicolas Ferre 443377fedd1SNicolas Ferre return ret; 444377fedd1SNicolas Ferre } 445377fedd1SNicolas Ferre 446ab4382d2SGreg Kroah-Hartman /* 447ab4382d2SGreg Kroah-Hartman * Return TIOCSER_TEMT when transmitter FIFO and Shift register is empty. 448ab4382d2SGreg Kroah-Hartman */ 449ab4382d2SGreg Kroah-Hartman static u_int atmel_tx_empty(struct uart_port *port) 450ab4382d2SGreg Kroah-Hartman { 451ea04f82aSRomain Izard struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 452ea04f82aSRomain Izard 453ea04f82aSRomain Izard if (atmel_port->tx_stopped) 454ea04f82aSRomain Izard return TIOCSER_TEMT; 4554e7decdaSCyrille Pitchen return (atmel_uart_readl(port, ATMEL_US_CSR) & ATMEL_US_TXEMPTY) ? 4564e7decdaSCyrille Pitchen TIOCSER_TEMT : 4574e7decdaSCyrille Pitchen 0; 458ab4382d2SGreg Kroah-Hartman } 459ab4382d2SGreg Kroah-Hartman 460ab4382d2SGreg Kroah-Hartman /* 461ab4382d2SGreg Kroah-Hartman * Set state of the modem control output lines 462ab4382d2SGreg Kroah-Hartman */ 463ab4382d2SGreg Kroah-Hartman static void atmel_set_mctrl(struct uart_port *port, u_int mctrl) 464ab4382d2SGreg Kroah-Hartman { 465ab4382d2SGreg Kroah-Hartman unsigned int control = 0; 4664e7decdaSCyrille Pitchen unsigned int mode = atmel_uart_readl(port, ATMEL_US_MR); 4671cf6e8fcSCyrille Pitchen unsigned int rts_paused, rts_ready; 468ab4382d2SGreg Kroah-Hartman struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 469ab4382d2SGreg Kroah-Hartman 4701cf6e8fcSCyrille Pitchen /* override mode to RS485 if needed, otherwise keep the current mode */ 4711cf6e8fcSCyrille Pitchen if (port->rs485.flags & SER_RS485_ENABLED) { 4724e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_TTGR, 4734e7decdaSCyrille Pitchen port->rs485.delay_rts_after_send); 4741cf6e8fcSCyrille Pitchen mode &= ~ATMEL_US_USMODE; 4751cf6e8fcSCyrille Pitchen mode |= ATMEL_US_USMODE_RS485; 4761cf6e8fcSCyrille Pitchen } 4771cf6e8fcSCyrille Pitchen 4781cf6e8fcSCyrille Pitchen /* set the RTS line state according to the mode */ 4791cf6e8fcSCyrille Pitchen if ((mode & ATMEL_US_USMODE) == ATMEL_US_USMODE_HWHS) { 4801cf6e8fcSCyrille Pitchen /* force RTS line to high level */ 4811cf6e8fcSCyrille Pitchen rts_paused = ATMEL_US_RTSEN; 4821cf6e8fcSCyrille Pitchen 4831cf6e8fcSCyrille Pitchen /* give the control of the RTS line back to the hardware */ 4841cf6e8fcSCyrille Pitchen rts_ready = ATMEL_US_RTSDIS; 4851cf6e8fcSCyrille Pitchen } else { 4861cf6e8fcSCyrille Pitchen /* force RTS line to high level */ 4871cf6e8fcSCyrille Pitchen rts_paused = ATMEL_US_RTSDIS; 4881cf6e8fcSCyrille Pitchen 4891cf6e8fcSCyrille Pitchen /* force RTS line to low level */ 4901cf6e8fcSCyrille Pitchen rts_ready = ATMEL_US_RTSEN; 4911cf6e8fcSCyrille Pitchen } 4921cf6e8fcSCyrille Pitchen 493ab4382d2SGreg Kroah-Hartman if (mctrl & TIOCM_RTS) 4941cf6e8fcSCyrille Pitchen control |= rts_ready; 495ab4382d2SGreg Kroah-Hartman else 4961cf6e8fcSCyrille Pitchen control |= rts_paused; 497ab4382d2SGreg Kroah-Hartman 498ab4382d2SGreg Kroah-Hartman if (mctrl & TIOCM_DTR) 499ab4382d2SGreg Kroah-Hartman control |= ATMEL_US_DTREN; 500ab4382d2SGreg Kroah-Hartman else 501ab4382d2SGreg Kroah-Hartman control |= ATMEL_US_DTRDIS; 502ab4382d2SGreg Kroah-Hartman 5034e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_CR, control); 504ab4382d2SGreg Kroah-Hartman 505e0b0baadSRichard Genoud mctrl_gpio_set(atmel_port->gpios, mctrl); 506e0b0baadSRichard Genoud 507ab4382d2SGreg Kroah-Hartman /* Local loopback mode? */ 5081cf6e8fcSCyrille Pitchen mode &= ~ATMEL_US_CHMODE; 509ab4382d2SGreg Kroah-Hartman if (mctrl & TIOCM_LOOP) 510ab4382d2SGreg Kroah-Hartman mode |= ATMEL_US_CHMODE_LOC_LOOP; 511ab4382d2SGreg Kroah-Hartman else 512ab4382d2SGreg Kroah-Hartman mode |= ATMEL_US_CHMODE_NORMAL; 513ab4382d2SGreg Kroah-Hartman 5144e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_MR, mode); 515ab4382d2SGreg Kroah-Hartman } 516ab4382d2SGreg Kroah-Hartman 517ab4382d2SGreg Kroah-Hartman /* 518ab4382d2SGreg Kroah-Hartman * Get state of the modem control input lines 519ab4382d2SGreg Kroah-Hartman */ 520ab4382d2SGreg Kroah-Hartman static u_int atmel_get_mctrl(struct uart_port *port) 521ab4382d2SGreg Kroah-Hartman { 522e0b0baadSRichard Genoud struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 523e0b0baadSRichard Genoud unsigned int ret = 0, status; 524ab4382d2SGreg Kroah-Hartman 5254e7decdaSCyrille Pitchen status = atmel_uart_readl(port, ATMEL_US_CSR); 526ab4382d2SGreg Kroah-Hartman 527ab4382d2SGreg Kroah-Hartman /* 528ab4382d2SGreg Kroah-Hartman * The control signals are active low. 529ab4382d2SGreg Kroah-Hartman */ 530ab4382d2SGreg Kroah-Hartman if (!(status & ATMEL_US_DCD)) 531ab4382d2SGreg Kroah-Hartman ret |= TIOCM_CD; 532ab4382d2SGreg Kroah-Hartman if (!(status & ATMEL_US_CTS)) 533ab4382d2SGreg Kroah-Hartman ret |= TIOCM_CTS; 534ab4382d2SGreg Kroah-Hartman if (!(status & ATMEL_US_DSR)) 535ab4382d2SGreg Kroah-Hartman ret |= TIOCM_DSR; 536ab4382d2SGreg Kroah-Hartman if (!(status & ATMEL_US_RI)) 537ab4382d2SGreg Kroah-Hartman ret |= TIOCM_RI; 538ab4382d2SGreg Kroah-Hartman 539e0b0baadSRichard Genoud return mctrl_gpio_get(atmel_port->gpios, &ret); 540ab4382d2SGreg Kroah-Hartman } 541ab4382d2SGreg Kroah-Hartman 542ab4382d2SGreg Kroah-Hartman /* 543ab4382d2SGreg Kroah-Hartman * Stop transmitting. 544ab4382d2SGreg Kroah-Hartman */ 545ab4382d2SGreg Kroah-Hartman static void atmel_stop_tx(struct uart_port *port) 546ab4382d2SGreg Kroah-Hartman { 547ab4382d2SGreg Kroah-Hartman struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 548ab4382d2SGreg Kroah-Hartman 54964e22ebeSElen Song if (atmel_use_pdc_tx(port)) { 550ab4382d2SGreg Kroah-Hartman /* disable PDC transmit */ 5514e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_TXTDIS); 552ab4382d2SGreg Kroah-Hartman } 55389d82324SRichard Genoud 55489d82324SRichard Genoud /* 55589d82324SRichard Genoud * Disable the transmitter. 55689d82324SRichard Genoud * This is mandatory when DMA is used, otherwise the DMA buffer 55789d82324SRichard Genoud * is fully transmitted. 55889d82324SRichard Genoud */ 55989d82324SRichard Genoud atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_TXDIS); 560ea04f82aSRomain Izard atmel_port->tx_stopped = true; 56189d82324SRichard Genoud 562ab4382d2SGreg Kroah-Hartman /* Disable interrupts */ 5634e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_IDR, atmel_port->tx_done_mask); 564ab4382d2SGreg Kroah-Hartman 565f3040983SRazvan Stefanescu if (atmel_uart_is_half_duplex(port)) 56604b5bfe3SNicolas Ferre if (!atomic_read(&atmel_port->tasklet_shutdown)) 567ab4382d2SGreg Kroah-Hartman atmel_start_rx(port); 568f3040983SRazvan Stefanescu 569ab4382d2SGreg Kroah-Hartman } 570ab4382d2SGreg Kroah-Hartman 571ab4382d2SGreg Kroah-Hartman /* 572ab4382d2SGreg Kroah-Hartman * Start transmitting. 573ab4382d2SGreg Kroah-Hartman */ 574ab4382d2SGreg Kroah-Hartman static void atmel_start_tx(struct uart_port *port) 575ab4382d2SGreg Kroah-Hartman { 576ab4382d2SGreg Kroah-Hartman struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 577ab4382d2SGreg Kroah-Hartman 5780058f087SAlexandre Belloni if (atmel_use_pdc_tx(port) && (atmel_uart_readl(port, ATMEL_PDC_PTSR) 5790058f087SAlexandre Belloni & ATMEL_PDC_TXTEN)) 580ab4382d2SGreg Kroah-Hartman /* The transmitter is already running. Yes, we 581ab4382d2SGreg Kroah-Hartman really need this.*/ 582ab4382d2SGreg Kroah-Hartman return; 583ab4382d2SGreg Kroah-Hartman 5840058f087SAlexandre Belloni if (atmel_use_pdc_tx(port) || atmel_use_dma_tx(port)) 585f3040983SRazvan Stefanescu if (atmel_uart_is_half_duplex(port)) 586ab4382d2SGreg Kroah-Hartman atmel_stop_rx(port); 587ab4382d2SGreg Kroah-Hartman 5880058f087SAlexandre Belloni if (atmel_use_pdc_tx(port)) 589ab4382d2SGreg Kroah-Hartman /* re-enable PDC transmit */ 5904e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_TXTEN); 5910058f087SAlexandre Belloni 592ab4382d2SGreg Kroah-Hartman /* Enable interrupts */ 5934e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_IER, atmel_port->tx_done_mask); 59489d82324SRichard Genoud 59589d82324SRichard Genoud /* re-enable the transmitter */ 59689d82324SRichard Genoud atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_TXEN); 597ea04f82aSRomain Izard atmel_port->tx_stopped = false; 598ab4382d2SGreg Kroah-Hartman } 599ab4382d2SGreg Kroah-Hartman 600ab4382d2SGreg Kroah-Hartman /* 601ab4382d2SGreg Kroah-Hartman * start receiving - port is in process of being opened. 602ab4382d2SGreg Kroah-Hartman */ 603ab4382d2SGreg Kroah-Hartman static void atmel_start_rx(struct uart_port *port) 604ab4382d2SGreg Kroah-Hartman { 6054e7decdaSCyrille Pitchen /* reset status and receiver */ 6064e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA); 607ab4382d2SGreg Kroah-Hartman 6084e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RXEN); 60957c36868SSiftar, Gabe 61064e22ebeSElen Song if (atmel_use_pdc_rx(port)) { 611ab4382d2SGreg Kroah-Hartman /* enable PDC controller */ 6124e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_IER, 6134e7decdaSCyrille Pitchen ATMEL_US_ENDRX | ATMEL_US_TIMEOUT | 614ab4382d2SGreg Kroah-Hartman port->read_status_mask); 6154e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_RXTEN); 616ab4382d2SGreg Kroah-Hartman } else { 6174e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_IER, ATMEL_US_RXRDY); 618ab4382d2SGreg Kroah-Hartman } 619ab4382d2SGreg Kroah-Hartman } 620ab4382d2SGreg Kroah-Hartman 621ab4382d2SGreg Kroah-Hartman /* 622ab4382d2SGreg Kroah-Hartman * Stop receiving - port is in process of being closed. 623ab4382d2SGreg Kroah-Hartman */ 624ab4382d2SGreg Kroah-Hartman static void atmel_stop_rx(struct uart_port *port) 625ab4382d2SGreg Kroah-Hartman { 6264e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RXDIS); 62757c36868SSiftar, Gabe 62864e22ebeSElen Song if (atmel_use_pdc_rx(port)) { 629ab4382d2SGreg Kroah-Hartman /* disable PDC receive */ 6304e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_RXTDIS); 6314e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_IDR, 6324e7decdaSCyrille Pitchen ATMEL_US_ENDRX | ATMEL_US_TIMEOUT | 633ab4382d2SGreg Kroah-Hartman port->read_status_mask); 634ab4382d2SGreg Kroah-Hartman } else { 6354e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_IDR, ATMEL_US_RXRDY); 636ab4382d2SGreg Kroah-Hartman } 637ab4382d2SGreg Kroah-Hartman } 638ab4382d2SGreg Kroah-Hartman 639ab4382d2SGreg Kroah-Hartman /* 640ab4382d2SGreg Kroah-Hartman * Enable modem status interrupts 641ab4382d2SGreg Kroah-Hartman */ 642ab4382d2SGreg Kroah-Hartman static void atmel_enable_ms(struct uart_port *port) 643ab4382d2SGreg Kroah-Hartman { 644ab5e4e41SRichard Genoud struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 645ab5e4e41SRichard Genoud uint32_t ier = 0; 646ab5e4e41SRichard Genoud 647ab5e4e41SRichard Genoud /* 648ab5e4e41SRichard Genoud * Interrupt should not be enabled twice 649ab5e4e41SRichard Genoud */ 650ab5e4e41SRichard Genoud if (atmel_port->ms_irq_enabled) 651ab5e4e41SRichard Genoud return; 652ab5e4e41SRichard Genoud 653ab5e4e41SRichard Genoud atmel_port->ms_irq_enabled = true; 654ab5e4e41SRichard Genoud 65518dfef9cSUwe Kleine-König if (!mctrl_gpio_to_gpiod(atmel_port->gpios, UART_GPIO_CTS)) 656ab5e4e41SRichard Genoud ier |= ATMEL_US_CTSIC; 657ab5e4e41SRichard Genoud 65818dfef9cSUwe Kleine-König if (!mctrl_gpio_to_gpiod(atmel_port->gpios, UART_GPIO_DSR)) 659ab5e4e41SRichard Genoud ier |= ATMEL_US_DSRIC; 660ab5e4e41SRichard Genoud 66118dfef9cSUwe Kleine-König if (!mctrl_gpio_to_gpiod(atmel_port->gpios, UART_GPIO_RI)) 662ab5e4e41SRichard Genoud ier |= ATMEL_US_RIIC; 663ab5e4e41SRichard Genoud 66418dfef9cSUwe Kleine-König if (!mctrl_gpio_to_gpiod(atmel_port->gpios, UART_GPIO_DCD)) 665ab5e4e41SRichard Genoud ier |= ATMEL_US_DCDIC; 666ab5e4e41SRichard Genoud 6674e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_IER, ier); 66818dfef9cSUwe Kleine-König 66918dfef9cSUwe Kleine-König mctrl_gpio_enable_ms(atmel_port->gpios); 670ab4382d2SGreg Kroah-Hartman } 671ab4382d2SGreg Kroah-Hartman 672ab4382d2SGreg Kroah-Hartman /* 67335b675b9SRichard Genoud * Disable modem status interrupts 67435b675b9SRichard Genoud */ 67535b675b9SRichard Genoud static void atmel_disable_ms(struct uart_port *port) 67635b675b9SRichard Genoud { 67735b675b9SRichard Genoud struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 67835b675b9SRichard Genoud uint32_t idr = 0; 67935b675b9SRichard Genoud 68035b675b9SRichard Genoud /* 68135b675b9SRichard Genoud * Interrupt should not be disabled twice 68235b675b9SRichard Genoud */ 68335b675b9SRichard Genoud if (!atmel_port->ms_irq_enabled) 68435b675b9SRichard Genoud return; 68535b675b9SRichard Genoud 68635b675b9SRichard Genoud atmel_port->ms_irq_enabled = false; 68735b675b9SRichard Genoud 68818dfef9cSUwe Kleine-König mctrl_gpio_disable_ms(atmel_port->gpios); 68918dfef9cSUwe Kleine-König 69018dfef9cSUwe Kleine-König if (!mctrl_gpio_to_gpiod(atmel_port->gpios, UART_GPIO_CTS)) 69135b675b9SRichard Genoud idr |= ATMEL_US_CTSIC; 69235b675b9SRichard Genoud 69318dfef9cSUwe Kleine-König if (!mctrl_gpio_to_gpiod(atmel_port->gpios, UART_GPIO_DSR)) 69435b675b9SRichard Genoud idr |= ATMEL_US_DSRIC; 69535b675b9SRichard Genoud 69618dfef9cSUwe Kleine-König if (!mctrl_gpio_to_gpiod(atmel_port->gpios, UART_GPIO_RI)) 69735b675b9SRichard Genoud idr |= ATMEL_US_RIIC; 69835b675b9SRichard Genoud 69918dfef9cSUwe Kleine-König if (!mctrl_gpio_to_gpiod(atmel_port->gpios, UART_GPIO_DCD)) 70035b675b9SRichard Genoud idr |= ATMEL_US_DCDIC; 70135b675b9SRichard Genoud 7024e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_IDR, idr); 70335b675b9SRichard Genoud } 70435b675b9SRichard Genoud 70535b675b9SRichard Genoud /* 706ab4382d2SGreg Kroah-Hartman * Control the transmission of a break signal 707ab4382d2SGreg Kroah-Hartman */ 708ab4382d2SGreg Kroah-Hartman static void atmel_break_ctl(struct uart_port *port, int break_state) 709ab4382d2SGreg Kroah-Hartman { 710ab4382d2SGreg Kroah-Hartman if (break_state != 0) 7114e7decdaSCyrille Pitchen /* start break */ 7124e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_STTBRK); 713ab4382d2SGreg Kroah-Hartman else 7144e7decdaSCyrille Pitchen /* stop break */ 7154e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_STPBRK); 716ab4382d2SGreg Kroah-Hartman } 717ab4382d2SGreg Kroah-Hartman 718ab4382d2SGreg Kroah-Hartman /* 719ab4382d2SGreg Kroah-Hartman * Stores the incoming character in the ring buffer 720ab4382d2SGreg Kroah-Hartman */ 721ab4382d2SGreg Kroah-Hartman static void 722ab4382d2SGreg Kroah-Hartman atmel_buffer_rx_char(struct uart_port *port, unsigned int status, 723ab4382d2SGreg Kroah-Hartman unsigned int ch) 724ab4382d2SGreg Kroah-Hartman { 725ab4382d2SGreg Kroah-Hartman struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 726ab4382d2SGreg Kroah-Hartman struct circ_buf *ring = &atmel_port->rx_ring; 727ab4382d2SGreg Kroah-Hartman struct atmel_uart_char *c; 728ab4382d2SGreg Kroah-Hartman 729ab4382d2SGreg Kroah-Hartman if (!CIRC_SPACE(ring->head, ring->tail, ATMEL_SERIAL_RINGSIZE)) 730ab4382d2SGreg Kroah-Hartman /* Buffer overflow, ignore char */ 731ab4382d2SGreg Kroah-Hartman return; 732ab4382d2SGreg Kroah-Hartman 733ab4382d2SGreg Kroah-Hartman c = &((struct atmel_uart_char *)ring->buf)[ring->head]; 734ab4382d2SGreg Kroah-Hartman c->status = status; 735ab4382d2SGreg Kroah-Hartman c->ch = ch; 736ab4382d2SGreg Kroah-Hartman 737ab4382d2SGreg Kroah-Hartman /* Make sure the character is stored before we update head. */ 738ab4382d2SGreg Kroah-Hartman smp_wmb(); 739ab4382d2SGreg Kroah-Hartman 740ab4382d2SGreg Kroah-Hartman ring->head = (ring->head + 1) & (ATMEL_SERIAL_RINGSIZE - 1); 741ab4382d2SGreg Kroah-Hartman } 742ab4382d2SGreg Kroah-Hartman 743ab4382d2SGreg Kroah-Hartman /* 744ab4382d2SGreg Kroah-Hartman * Deal with parity, framing and overrun errors. 745ab4382d2SGreg Kroah-Hartman */ 746ab4382d2SGreg Kroah-Hartman static void atmel_pdc_rxerr(struct uart_port *port, unsigned int status) 747ab4382d2SGreg Kroah-Hartman { 748ab4382d2SGreg Kroah-Hartman /* clear error */ 7494e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA); 750ab4382d2SGreg Kroah-Hartman 751ab4382d2SGreg Kroah-Hartman if (status & ATMEL_US_RXBRK) { 752ab4382d2SGreg Kroah-Hartman /* ignore side-effect */ 753ab4382d2SGreg Kroah-Hartman status &= ~(ATMEL_US_PARE | ATMEL_US_FRAME); 754ab4382d2SGreg Kroah-Hartman port->icount.brk++; 755ab4382d2SGreg Kroah-Hartman } 756ab4382d2SGreg Kroah-Hartman if (status & ATMEL_US_PARE) 757ab4382d2SGreg Kroah-Hartman port->icount.parity++; 758ab4382d2SGreg Kroah-Hartman if (status & ATMEL_US_FRAME) 759ab4382d2SGreg Kroah-Hartman port->icount.frame++; 760ab4382d2SGreg Kroah-Hartman if (status & ATMEL_US_OVRE) 761ab4382d2SGreg Kroah-Hartman port->icount.overrun++; 762ab4382d2SGreg Kroah-Hartman } 763ab4382d2SGreg Kroah-Hartman 764ab4382d2SGreg Kroah-Hartman /* 765ab4382d2SGreg Kroah-Hartman * Characters received (called from interrupt handler) 766ab4382d2SGreg Kroah-Hartman */ 767ab4382d2SGreg Kroah-Hartman static void atmel_rx_chars(struct uart_port *port) 768ab4382d2SGreg Kroah-Hartman { 769ab4382d2SGreg Kroah-Hartman struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 770ab4382d2SGreg Kroah-Hartman unsigned int status, ch; 771ab4382d2SGreg Kroah-Hartman 7724e7decdaSCyrille Pitchen status = atmel_uart_readl(port, ATMEL_US_CSR); 773ab4382d2SGreg Kroah-Hartman while (status & ATMEL_US_RXRDY) { 774a6499435SCyrille Pitchen ch = atmel_uart_read_char(port); 775ab4382d2SGreg Kroah-Hartman 776ab4382d2SGreg Kroah-Hartman /* 777ab4382d2SGreg Kroah-Hartman * note that the error handling code is 778ab4382d2SGreg Kroah-Hartman * out of the main execution path 779ab4382d2SGreg Kroah-Hartman */ 780ab4382d2SGreg Kroah-Hartman if (unlikely(status & (ATMEL_US_PARE | ATMEL_US_FRAME 781ab4382d2SGreg Kroah-Hartman | ATMEL_US_OVRE | ATMEL_US_RXBRK) 782ab4382d2SGreg Kroah-Hartman || atmel_port->break_active)) { 783ab4382d2SGreg Kroah-Hartman 784ab4382d2SGreg Kroah-Hartman /* clear error */ 7854e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA); 786ab4382d2SGreg Kroah-Hartman 787ab4382d2SGreg Kroah-Hartman if (status & ATMEL_US_RXBRK 788ab4382d2SGreg Kroah-Hartman && !atmel_port->break_active) { 789ab4382d2SGreg Kroah-Hartman atmel_port->break_active = 1; 7904e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_IER, 7914e7decdaSCyrille Pitchen ATMEL_US_RXBRK); 792ab4382d2SGreg Kroah-Hartman } else { 793ab4382d2SGreg Kroah-Hartman /* 794ab4382d2SGreg Kroah-Hartman * This is either the end-of-break 795ab4382d2SGreg Kroah-Hartman * condition or we've received at 796ab4382d2SGreg Kroah-Hartman * least one character without RXBRK 797ab4382d2SGreg Kroah-Hartman * being set. In both cases, the next 798ab4382d2SGreg Kroah-Hartman * RXBRK will indicate start-of-break. 799ab4382d2SGreg Kroah-Hartman */ 8004e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_IDR, 8014e7decdaSCyrille Pitchen ATMEL_US_RXBRK); 802ab4382d2SGreg Kroah-Hartman status &= ~ATMEL_US_RXBRK; 803ab4382d2SGreg Kroah-Hartman atmel_port->break_active = 0; 804ab4382d2SGreg Kroah-Hartman } 805ab4382d2SGreg Kroah-Hartman } 806ab4382d2SGreg Kroah-Hartman 807ab4382d2SGreg Kroah-Hartman atmel_buffer_rx_char(port, status, ch); 8084e7decdaSCyrille Pitchen status = atmel_uart_readl(port, ATMEL_US_CSR); 809ab4382d2SGreg Kroah-Hartman } 810ab4382d2SGreg Kroah-Hartman 81198f2082cSNicolas Ferre atmel_tasklet_schedule(atmel_port, &atmel_port->tasklet_rx); 812ab4382d2SGreg Kroah-Hartman } 813ab4382d2SGreg Kroah-Hartman 814ab4382d2SGreg Kroah-Hartman /* 815ab4382d2SGreg Kroah-Hartman * Transmit characters (called from tasklet with TXRDY interrupt 816ab4382d2SGreg Kroah-Hartman * disabled) 817ab4382d2SGreg Kroah-Hartman */ 818ab4382d2SGreg Kroah-Hartman static void atmel_tx_chars(struct uart_port *port) 819ab4382d2SGreg Kroah-Hartman { 820ab4382d2SGreg Kroah-Hartman struct circ_buf *xmit = &port->state->xmit; 821ab4382d2SGreg Kroah-Hartman struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 822ab4382d2SGreg Kroah-Hartman 8234e7decdaSCyrille Pitchen if (port->x_char && 824477b8383SCodrin.Ciubotariu@microchip.com (atmel_uart_readl(port, ATMEL_US_CSR) & ATMEL_US_TXRDY)) { 825a6499435SCyrille Pitchen atmel_uart_write_char(port, port->x_char); 826ab4382d2SGreg Kroah-Hartman port->icount.tx++; 827ab4382d2SGreg Kroah-Hartman port->x_char = 0; 828ab4382d2SGreg Kroah-Hartman } 829ab4382d2SGreg Kroah-Hartman if (uart_circ_empty(xmit) || uart_tx_stopped(port)) 830ab4382d2SGreg Kroah-Hartman return; 831ab4382d2SGreg Kroah-Hartman 832477b8383SCodrin.Ciubotariu@microchip.com while (atmel_uart_readl(port, ATMEL_US_CSR) & ATMEL_US_TXRDY) { 833a6499435SCyrille Pitchen atmel_uart_write_char(port, xmit->buf[xmit->tail]); 834ab4382d2SGreg Kroah-Hartman xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1); 835ab4382d2SGreg Kroah-Hartman port->icount.tx++; 836ab4382d2SGreg Kroah-Hartman if (uart_circ_empty(xmit)) 837ab4382d2SGreg Kroah-Hartman break; 838ab4382d2SGreg Kroah-Hartman } 839ab4382d2SGreg Kroah-Hartman 840ab4382d2SGreg Kroah-Hartman if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS) 841ab4382d2SGreg Kroah-Hartman uart_write_wakeup(port); 842ab4382d2SGreg Kroah-Hartman 843477b8383SCodrin.Ciubotariu@microchip.com if (!uart_circ_empty(xmit)) { 844477b8383SCodrin.Ciubotariu@microchip.com /* we still have characters to transmit, so we should continue 845477b8383SCodrin.Ciubotariu@microchip.com * transmitting them when TX is ready, regardless of 846477b8383SCodrin.Ciubotariu@microchip.com * mode or duplexity 847477b8383SCodrin.Ciubotariu@microchip.com */ 848477b8383SCodrin.Ciubotariu@microchip.com atmel_port->tx_done_mask |= ATMEL_US_TXRDY; 849477b8383SCodrin.Ciubotariu@microchip.com 850ab4382d2SGreg Kroah-Hartman /* Enable interrupts */ 8514e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_IER, 8524e7decdaSCyrille Pitchen atmel_port->tx_done_mask); 853477b8383SCodrin.Ciubotariu@microchip.com } else { 854477b8383SCodrin.Ciubotariu@microchip.com if (atmel_uart_is_half_duplex(port)) 855477b8383SCodrin.Ciubotariu@microchip.com atmel_port->tx_done_mask &= ~ATMEL_US_TXRDY; 856477b8383SCodrin.Ciubotariu@microchip.com } 857ab4382d2SGreg Kroah-Hartman } 858ab4382d2SGreg Kroah-Hartman 85908f738beSElen Song static void atmel_complete_tx_dma(void *arg) 86008f738beSElen Song { 86108f738beSElen Song struct atmel_uart_port *atmel_port = arg; 86208f738beSElen Song struct uart_port *port = &atmel_port->uart; 86308f738beSElen Song struct circ_buf *xmit = &port->state->xmit; 86408f738beSElen Song struct dma_chan *chan = atmel_port->chan_tx; 86508f738beSElen Song unsigned long flags; 86608f738beSElen Song 86708f738beSElen Song spin_lock_irqsave(&port->lock, flags); 86808f738beSElen Song 86908f738beSElen Song if (chan) 87008f738beSElen Song dmaengine_terminate_all(chan); 8715f258b3eSCyrille Pitchen xmit->tail += atmel_port->tx_len; 87208f738beSElen Song xmit->tail &= UART_XMIT_SIZE - 1; 87308f738beSElen Song 8745f258b3eSCyrille Pitchen port->icount.tx += atmel_port->tx_len; 87508f738beSElen Song 87608f738beSElen Song spin_lock_irq(&atmel_port->lock_tx); 87708f738beSElen Song async_tx_ack(atmel_port->desc_tx); 87808f738beSElen Song atmel_port->cookie_tx = -EINVAL; 87908f738beSElen Song atmel_port->desc_tx = NULL; 88008f738beSElen Song spin_unlock_irq(&atmel_port->lock_tx); 88108f738beSElen Song 88208f738beSElen Song if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS) 88308f738beSElen Song uart_write_wakeup(port); 88408f738beSElen Song 8851842dc2eSCyrille Pitchen /* 8861842dc2eSCyrille Pitchen * xmit is a circular buffer so, if we have just send data from 8871842dc2eSCyrille Pitchen * xmit->tail to the end of xmit->buf, now we have to transmit the 8881842dc2eSCyrille Pitchen * remaining data from the beginning of xmit->buf to xmit->head. 8891842dc2eSCyrille Pitchen */ 89008f738beSElen Song if (!uart_circ_empty(xmit)) 89198f2082cSNicolas Ferre atmel_tasklet_schedule(atmel_port, &atmel_port->tasklet_tx); 892f3040983SRazvan Stefanescu else if (atmel_uart_is_half_duplex(port)) { 89369646d7aSRazvan Stefanescu /* 89469646d7aSRazvan Stefanescu * DMA done, re-enable TXEMPTY and signal that we can stop 89569646d7aSRazvan Stefanescu * TX and start RX for RS485 89669646d7aSRazvan Stefanescu */ 89769646d7aSRazvan Stefanescu atmel_port->hd_start_rx = true; 89869646d7aSRazvan Stefanescu atmel_uart_writel(port, ATMEL_US_IER, 89969646d7aSRazvan Stefanescu atmel_port->tx_done_mask); 900b389f173SRichard Genoud } 90108f738beSElen Song 90208f738beSElen Song spin_unlock_irqrestore(&port->lock, flags); 90308f738beSElen Song } 90408f738beSElen Song 90508f738beSElen Song static void atmel_release_tx_dma(struct uart_port *port) 90608f738beSElen Song { 90708f738beSElen Song struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 90808f738beSElen Song struct dma_chan *chan = atmel_port->chan_tx; 90908f738beSElen Song 91008f738beSElen Song if (chan) { 91108f738beSElen Song dmaengine_terminate_all(chan); 91208f738beSElen Song dma_release_channel(chan); 91308f738beSElen Song dma_unmap_sg(port->dev, &atmel_port->sg_tx, 1, 91448479148SWolfram Sang DMA_TO_DEVICE); 91508f738beSElen Song } 91608f738beSElen Song 91708f738beSElen Song atmel_port->desc_tx = NULL; 91808f738beSElen Song atmel_port->chan_tx = NULL; 91908f738beSElen Song atmel_port->cookie_tx = -EINVAL; 92008f738beSElen Song } 92108f738beSElen Song 92208f738beSElen Song /* 92308f738beSElen Song * Called from tasklet with TXRDY interrupt is disabled. 92408f738beSElen Song */ 92508f738beSElen Song static void atmel_tx_dma(struct uart_port *port) 92608f738beSElen Song { 92708f738beSElen Song struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 92808f738beSElen Song struct circ_buf *xmit = &port->state->xmit; 92908f738beSElen Song struct dma_chan *chan = atmel_port->chan_tx; 93008f738beSElen Song struct dma_async_tx_descriptor *desc; 9315f258b3eSCyrille Pitchen struct scatterlist sgl[2], *sg, *sg_tx = &atmel_port->sg_tx; 9325f258b3eSCyrille Pitchen unsigned int tx_len, part1_len, part2_len, sg_len; 9335f258b3eSCyrille Pitchen dma_addr_t phys_addr; 93408f738beSElen Song 93508f738beSElen Song /* Make sure we have an idle channel */ 93608f738beSElen Song if (atmel_port->desc_tx != NULL) 93708f738beSElen Song return; 93808f738beSElen Song 93908f738beSElen Song if (!uart_circ_empty(xmit) && !uart_tx_stopped(port)) { 94008f738beSElen Song /* 94108f738beSElen Song * DMA is idle now. 94208f738beSElen Song * Port xmit buffer is already mapped, 94308f738beSElen Song * and it is one page... Just adjust 94408f738beSElen Song * offsets and lengths. Since it is a circular buffer, 94508f738beSElen Song * we have to transmit till the end, and then the rest. 94608f738beSElen Song * Take the port lock to get a 94708f738beSElen Song * consistent xmit buffer state. 94808f738beSElen Song */ 9495f258b3eSCyrille Pitchen tx_len = CIRC_CNT_TO_END(xmit->head, 95008f738beSElen Song xmit->tail, 95108f738beSElen Song UART_XMIT_SIZE); 9525f258b3eSCyrille Pitchen 9535f258b3eSCyrille Pitchen if (atmel_port->fifo_size) { 9545f258b3eSCyrille Pitchen /* multi data mode */ 9555f258b3eSCyrille Pitchen part1_len = (tx_len & ~0x3); /* DWORD access */ 9565f258b3eSCyrille Pitchen part2_len = (tx_len & 0x3); /* BYTE access */ 9575f258b3eSCyrille Pitchen } else { 9585f258b3eSCyrille Pitchen /* single data (legacy) mode */ 9595f258b3eSCyrille Pitchen part1_len = 0; 9605f258b3eSCyrille Pitchen part2_len = tx_len; /* BYTE access only */ 9615f258b3eSCyrille Pitchen } 9625f258b3eSCyrille Pitchen 9635f258b3eSCyrille Pitchen sg_init_table(sgl, 2); 9645f258b3eSCyrille Pitchen sg_len = 0; 9655f258b3eSCyrille Pitchen phys_addr = sg_dma_address(sg_tx) + xmit->tail; 9665f258b3eSCyrille Pitchen if (part1_len) { 9675f258b3eSCyrille Pitchen sg = &sgl[sg_len++]; 9685f258b3eSCyrille Pitchen sg_dma_address(sg) = phys_addr; 9695f258b3eSCyrille Pitchen sg_dma_len(sg) = part1_len; 9705f258b3eSCyrille Pitchen 9715f258b3eSCyrille Pitchen phys_addr += part1_len; 9725f258b3eSCyrille Pitchen } 9735f258b3eSCyrille Pitchen 9745f258b3eSCyrille Pitchen if (part2_len) { 9755f258b3eSCyrille Pitchen sg = &sgl[sg_len++]; 9765f258b3eSCyrille Pitchen sg_dma_address(sg) = phys_addr; 9775f258b3eSCyrille Pitchen sg_dma_len(sg) = part2_len; 9785f258b3eSCyrille Pitchen } 9795f258b3eSCyrille Pitchen 9805f258b3eSCyrille Pitchen /* 9815f258b3eSCyrille Pitchen * save tx_len so atmel_complete_tx_dma() will increase 9825f258b3eSCyrille Pitchen * xmit->tail correctly 9835f258b3eSCyrille Pitchen */ 9845f258b3eSCyrille Pitchen atmel_port->tx_len = tx_len; 98508f738beSElen Song 98608f738beSElen Song desc = dmaengine_prep_slave_sg(chan, 9875f258b3eSCyrille Pitchen sgl, 9885f258b3eSCyrille Pitchen sg_len, 98908f738beSElen Song DMA_MEM_TO_DEV, 99008f738beSElen Song DMA_PREP_INTERRUPT | 99108f738beSElen Song DMA_CTRL_ACK); 99208f738beSElen Song if (!desc) { 99308f738beSElen Song dev_err(port->dev, "Failed to send via dma!\n"); 99408f738beSElen Song return; 99508f738beSElen Song } 99608f738beSElen Song 9975f258b3eSCyrille Pitchen dma_sync_sg_for_device(port->dev, sg_tx, 1, DMA_TO_DEVICE); 99808f738beSElen Song 99908f738beSElen Song atmel_port->desc_tx = desc; 100008f738beSElen Song desc->callback = atmel_complete_tx_dma; 100108f738beSElen Song desc->callback_param = atmel_port; 100208f738beSElen Song atmel_port->cookie_tx = dmaengine_submit(desc); 10031e67bd2bSTudor Ambarus if (dma_submit_error(atmel_port->cookie_tx)) { 10041e67bd2bSTudor Ambarus dev_err(port->dev, "dma_submit_error %d\n", 10051e67bd2bSTudor Ambarus atmel_port->cookie_tx); 10061e67bd2bSTudor Ambarus return; 10071e67bd2bSTudor Ambarus } 10084f4b9b58STudor Ambarus 10094f4b9b58STudor Ambarus dma_async_issue_pending(chan); 101008f738beSElen Song } 101108f738beSElen Song 101208f738beSElen Song if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS) 101308f738beSElen Song uart_write_wakeup(port); 101408f738beSElen Song } 101508f738beSElen Song 101608f738beSElen Song static int atmel_prepare_tx_dma(struct uart_port *port) 101708f738beSElen Song { 101808f738beSElen Song struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 1019c24d2531SRadu Pirea struct device *mfd_dev = port->dev->parent; 102008f738beSElen Song dma_cap_mask_t mask; 102108f738beSElen Song struct dma_slave_config config; 102208f738beSElen Song int ret, nent; 102308f738beSElen Song 102408f738beSElen Song dma_cap_zero(mask); 102508f738beSElen Song dma_cap_set(DMA_SLAVE, mask); 102608f738beSElen Song 1027c24d2531SRadu Pirea atmel_port->chan_tx = dma_request_slave_channel(mfd_dev, "tx"); 102808f738beSElen Song if (atmel_port->chan_tx == NULL) 102908f738beSElen Song goto chan_err; 103008f738beSElen Song dev_info(port->dev, "using %s for tx DMA transfers\n", 103108f738beSElen Song dma_chan_name(atmel_port->chan_tx)); 103208f738beSElen Song 103308f738beSElen Song spin_lock_init(&atmel_port->lock_tx); 103408f738beSElen Song sg_init_table(&atmel_port->sg_tx, 1); 103508f738beSElen Song /* UART circular tx buffer is an aligned page. */ 10362c277054SLeilei Zhao BUG_ON(!PAGE_ALIGNED(port->state->xmit.buf)); 103708f738beSElen Song sg_set_page(&atmel_port->sg_tx, 103808f738beSElen Song virt_to_page(port->state->xmit.buf), 103908f738beSElen Song UART_XMIT_SIZE, 10402b5cf14bSGeliang Tang offset_in_page(port->state->xmit.buf)); 104108f738beSElen Song nent = dma_map_sg(port->dev, 104208f738beSElen Song &atmel_port->sg_tx, 104308f738beSElen Song 1, 104448479148SWolfram Sang DMA_TO_DEVICE); 104508f738beSElen Song 104608f738beSElen Song if (!nent) { 104708f738beSElen Song dev_dbg(port->dev, "need to release resource of dma\n"); 104808f738beSElen Song goto chan_err; 104908f738beSElen Song } else { 1050c8d1f022SUwe Kleine-König dev_dbg(port->dev, "%s: mapped %d@%p to %pad\n", __func__, 105108f738beSElen Song sg_dma_len(&atmel_port->sg_tx), 105208f738beSElen Song port->state->xmit.buf, 1053c8d1f022SUwe Kleine-König &sg_dma_address(&atmel_port->sg_tx)); 105408f738beSElen Song } 105508f738beSElen Song 105608f738beSElen Song /* Configure the slave DMA */ 105708f738beSElen Song memset(&config, 0, sizeof(config)); 105808f738beSElen Song config.direction = DMA_MEM_TO_DEV; 10595f258b3eSCyrille Pitchen config.dst_addr_width = (atmel_port->fifo_size) ? 10605f258b3eSCyrille Pitchen DMA_SLAVE_BUSWIDTH_4_BYTES : 10615f258b3eSCyrille Pitchen DMA_SLAVE_BUSWIDTH_1_BYTE; 106208f738beSElen Song config.dst_addr = port->mapbase + ATMEL_US_THR; 1063a8d4e016SLudovic Desroches config.dst_maxburst = 1; 106408f738beSElen Song 10655483c10eSMaxime Ripard ret = dmaengine_slave_config(atmel_port->chan_tx, 10665483c10eSMaxime Ripard &config); 106708f738beSElen Song if (ret) { 106808f738beSElen Song dev_err(port->dev, "DMA tx slave configuration failed\n"); 106908f738beSElen Song goto chan_err; 107008f738beSElen Song } 107108f738beSElen Song 107208f738beSElen Song return 0; 107308f738beSElen Song 107408f738beSElen Song chan_err: 107508f738beSElen Song dev_err(port->dev, "TX channel not available, switch to pio\n"); 107636ce7cffSZheng Bin atmel_port->use_dma_tx = false; 107708f738beSElen Song if (atmel_port->chan_tx) 107808f738beSElen Song atmel_release_tx_dma(port); 107908f738beSElen Song return -EINVAL; 108008f738beSElen Song } 108108f738beSElen Song 108234df42f5SElen Song static void atmel_complete_rx_dma(void *arg) 108334df42f5SElen Song { 108434df42f5SElen Song struct uart_port *port = arg; 108534df42f5SElen Song struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 108634df42f5SElen Song 108798f2082cSNicolas Ferre atmel_tasklet_schedule(atmel_port, &atmel_port->tasklet_rx); 108834df42f5SElen Song } 108934df42f5SElen Song 109034df42f5SElen Song static void atmel_release_rx_dma(struct uart_port *port) 109134df42f5SElen Song { 109234df42f5SElen Song struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 109334df42f5SElen Song struct dma_chan *chan = atmel_port->chan_rx; 109434df42f5SElen Song 109534df42f5SElen Song if (chan) { 109634df42f5SElen Song dmaengine_terminate_all(chan); 109734df42f5SElen Song dma_release_channel(chan); 109834df42f5SElen Song dma_unmap_sg(port->dev, &atmel_port->sg_rx, 1, 109948479148SWolfram Sang DMA_FROM_DEVICE); 110034df42f5SElen Song } 110134df42f5SElen Song 110234df42f5SElen Song atmel_port->desc_rx = NULL; 110334df42f5SElen Song atmel_port->chan_rx = NULL; 110434df42f5SElen Song atmel_port->cookie_rx = -EINVAL; 110534df42f5SElen Song } 110634df42f5SElen Song 110734df42f5SElen Song static void atmel_rx_from_dma(struct uart_port *port) 110834df42f5SElen Song { 110934df42f5SElen Song struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 111066f37aafSCyrille Pitchen struct tty_port *tport = &port->state->port; 111134df42f5SElen Song struct circ_buf *ring = &atmel_port->rx_ring; 111234df42f5SElen Song struct dma_chan *chan = atmel_port->chan_rx; 111334df42f5SElen Song struct dma_tx_state state; 111434df42f5SElen Song enum dma_status dmastat; 111566f37aafSCyrille Pitchen size_t count; 111634df42f5SElen Song 111734df42f5SElen Song 111834df42f5SElen Song /* Reset the UART timeout early so that we don't miss one */ 11194e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_STTTO); 112034df42f5SElen Song dmastat = dmaengine_tx_status(chan, 112134df42f5SElen Song atmel_port->cookie_rx, 112234df42f5SElen Song &state); 112334df42f5SElen Song /* Restart a new tasklet if DMA status is error */ 112434df42f5SElen Song if (dmastat == DMA_ERROR) { 112534df42f5SElen Song dev_dbg(port->dev, "Get residue error, restart tasklet\n"); 11264e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_IER, ATMEL_US_TIMEOUT); 112798f2082cSNicolas Ferre atmel_tasklet_schedule(atmel_port, &atmel_port->tasklet_rx); 112834df42f5SElen Song return; 112934df42f5SElen Song } 113066f37aafSCyrille Pitchen 113166f37aafSCyrille Pitchen /* CPU claims ownership of RX DMA buffer */ 113266f37aafSCyrille Pitchen dma_sync_sg_for_cpu(port->dev, 113366f37aafSCyrille Pitchen &atmel_port->sg_rx, 113466f37aafSCyrille Pitchen 1, 1135485819b5SCyrille Pitchen DMA_FROM_DEVICE); 113634df42f5SElen Song 113734df42f5SElen Song /* 113866f37aafSCyrille Pitchen * ring->head points to the end of data already written by the DMA. 113966f37aafSCyrille Pitchen * ring->tail points to the beginning of data to be read by the 114066f37aafSCyrille Pitchen * framework. 114166f37aafSCyrille Pitchen * The current transfer size should not be larger than the dma buffer 114266f37aafSCyrille Pitchen * length. 114334df42f5SElen Song */ 114466f37aafSCyrille Pitchen ring->head = sg_dma_len(&atmel_port->sg_rx) - state.residue; 114566f37aafSCyrille Pitchen BUG_ON(ring->head > sg_dma_len(&atmel_port->sg_rx)); 114666f37aafSCyrille Pitchen /* 114766f37aafSCyrille Pitchen * At this point ring->head may point to the first byte right after the 114866f37aafSCyrille Pitchen * last byte of the dma buffer: 114966f37aafSCyrille Pitchen * 0 <= ring->head <= sg_dma_len(&atmel_port->sg_rx) 115066f37aafSCyrille Pitchen * 115166f37aafSCyrille Pitchen * However ring->tail must always points inside the dma buffer: 115266f37aafSCyrille Pitchen * 0 <= ring->tail <= sg_dma_len(&atmel_port->sg_rx) - 1 115366f37aafSCyrille Pitchen * 115466f37aafSCyrille Pitchen * Since we use a ring buffer, we have to handle the case 115566f37aafSCyrille Pitchen * where head is lower than tail. In such a case, we first read from 115666f37aafSCyrille Pitchen * tail to the end of the buffer then reset tail. 115766f37aafSCyrille Pitchen */ 115866f37aafSCyrille Pitchen if (ring->head < ring->tail) { 115966f37aafSCyrille Pitchen count = sg_dma_len(&atmel_port->sg_rx) - ring->tail; 116034df42f5SElen Song 116166f37aafSCyrille Pitchen tty_insert_flip_string(tport, ring->buf + ring->tail, count); 116266f37aafSCyrille Pitchen ring->tail = 0; 116334df42f5SElen Song port->icount.rx += count; 116434df42f5SElen Song } 116534df42f5SElen Song 116666f37aafSCyrille Pitchen /* Finally we read data from tail to head */ 116766f37aafSCyrille Pitchen if (ring->tail < ring->head) { 116866f37aafSCyrille Pitchen count = ring->head - ring->tail; 116966f37aafSCyrille Pitchen 117066f37aafSCyrille Pitchen tty_insert_flip_string(tport, ring->buf + ring->tail, count); 117166f37aafSCyrille Pitchen /* Wrap ring->head if needed */ 117266f37aafSCyrille Pitchen if (ring->head >= sg_dma_len(&atmel_port->sg_rx)) 117366f37aafSCyrille Pitchen ring->head = 0; 117466f37aafSCyrille Pitchen ring->tail = ring->head; 117566f37aafSCyrille Pitchen port->icount.rx += count; 117666f37aafSCyrille Pitchen } 117766f37aafSCyrille Pitchen 117866f37aafSCyrille Pitchen /* USART retreives ownership of RX DMA buffer */ 117966f37aafSCyrille Pitchen dma_sync_sg_for_device(port->dev, 118066f37aafSCyrille Pitchen &atmel_port->sg_rx, 118166f37aafSCyrille Pitchen 1, 1182485819b5SCyrille Pitchen DMA_FROM_DEVICE); 118366f37aafSCyrille Pitchen 118466f37aafSCyrille Pitchen tty_flip_buffer_push(tport); 118566f37aafSCyrille Pitchen 11864e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_IER, ATMEL_US_TIMEOUT); 118734df42f5SElen Song } 118834df42f5SElen Song 118934df42f5SElen Song static int atmel_prepare_rx_dma(struct uart_port *port) 119034df42f5SElen Song { 119134df42f5SElen Song struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 1192c24d2531SRadu Pirea struct device *mfd_dev = port->dev->parent; 119334df42f5SElen Song struct dma_async_tx_descriptor *desc; 119434df42f5SElen Song dma_cap_mask_t mask; 119534df42f5SElen Song struct dma_slave_config config; 119634df42f5SElen Song struct circ_buf *ring; 119734df42f5SElen Song int ret, nent; 119834df42f5SElen Song 119934df42f5SElen Song ring = &atmel_port->rx_ring; 120034df42f5SElen Song 120134df42f5SElen Song dma_cap_zero(mask); 120234df42f5SElen Song dma_cap_set(DMA_CYCLIC, mask); 120334df42f5SElen Song 1204c24d2531SRadu Pirea atmel_port->chan_rx = dma_request_slave_channel(mfd_dev, "rx"); 120534df42f5SElen Song if (atmel_port->chan_rx == NULL) 120634df42f5SElen Song goto chan_err; 120734df42f5SElen Song dev_info(port->dev, "using %s for rx DMA transfers\n", 120834df42f5SElen Song dma_chan_name(atmel_port->chan_rx)); 120934df42f5SElen Song 121034df42f5SElen Song spin_lock_init(&atmel_port->lock_rx); 121134df42f5SElen Song sg_init_table(&atmel_port->sg_rx, 1); 121234df42f5SElen Song /* UART circular rx buffer is an aligned page. */ 12132c277054SLeilei Zhao BUG_ON(!PAGE_ALIGNED(ring->buf)); 121434df42f5SElen Song sg_set_page(&atmel_port->sg_rx, 121534df42f5SElen Song virt_to_page(ring->buf), 1216a510880fSLeilei Zhao sizeof(struct atmel_uart_char) * ATMEL_SERIAL_RINGSIZE, 12172b5cf14bSGeliang Tang offset_in_page(ring->buf)); 121834df42f5SElen Song nent = dma_map_sg(port->dev, 121934df42f5SElen Song &atmel_port->sg_rx, 122034df42f5SElen Song 1, 122148479148SWolfram Sang DMA_FROM_DEVICE); 122234df42f5SElen Song 122334df42f5SElen Song if (!nent) { 122434df42f5SElen Song dev_dbg(port->dev, "need to release resource of dma\n"); 122534df42f5SElen Song goto chan_err; 122634df42f5SElen Song } else { 1227c8d1f022SUwe Kleine-König dev_dbg(port->dev, "%s: mapped %d@%p to %pad\n", __func__, 122834df42f5SElen Song sg_dma_len(&atmel_port->sg_rx), 122934df42f5SElen Song ring->buf, 1230c8d1f022SUwe Kleine-König &sg_dma_address(&atmel_port->sg_rx)); 123134df42f5SElen Song } 123234df42f5SElen Song 123334df42f5SElen Song /* Configure the slave DMA */ 123434df42f5SElen Song memset(&config, 0, sizeof(config)); 123534df42f5SElen Song config.direction = DMA_DEV_TO_MEM; 123634df42f5SElen Song config.src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE; 123734df42f5SElen Song config.src_addr = port->mapbase + ATMEL_US_RHR; 1238a8d4e016SLudovic Desroches config.src_maxburst = 1; 123934df42f5SElen Song 12405483c10eSMaxime Ripard ret = dmaengine_slave_config(atmel_port->chan_rx, 12415483c10eSMaxime Ripard &config); 124234df42f5SElen Song if (ret) { 124334df42f5SElen Song dev_err(port->dev, "DMA rx slave configuration failed\n"); 124434df42f5SElen Song goto chan_err; 124534df42f5SElen Song } 124634df42f5SElen Song /* 124734df42f5SElen Song * Prepare a cyclic dma transfer, assign 2 descriptors, 124834df42f5SElen Song * each one is half ring buffer size 124934df42f5SElen Song */ 125034df42f5SElen Song desc = dmaengine_prep_dma_cyclic(atmel_port->chan_rx, 125134df42f5SElen Song sg_dma_address(&atmel_port->sg_rx), 125234df42f5SElen Song sg_dma_len(&atmel_port->sg_rx), 125334df42f5SElen Song sg_dma_len(&atmel_port->sg_rx)/2, 125434df42f5SElen Song DMA_DEV_TO_MEM, 125534df42f5SElen Song DMA_PREP_INTERRUPT); 1256c85be041SKangjie Lu if (!desc) { 1257c85be041SKangjie Lu dev_err(port->dev, "Preparing DMA cyclic failed\n"); 1258c85be041SKangjie Lu goto chan_err; 1259c85be041SKangjie Lu } 126034df42f5SElen Song desc->callback = atmel_complete_rx_dma; 126134df42f5SElen Song desc->callback_param = port; 126234df42f5SElen Song atmel_port->desc_rx = desc; 126334df42f5SElen Song atmel_port->cookie_rx = dmaengine_submit(desc); 12641e67bd2bSTudor Ambarus if (dma_submit_error(atmel_port->cookie_rx)) { 12651e67bd2bSTudor Ambarus dev_err(port->dev, "dma_submit_error %d\n", 12661e67bd2bSTudor Ambarus atmel_port->cookie_rx); 12671e67bd2bSTudor Ambarus goto chan_err; 12681e67bd2bSTudor Ambarus } 126934df42f5SElen Song 12704f4b9b58STudor Ambarus dma_async_issue_pending(atmel_port->chan_rx); 12714f4b9b58STudor Ambarus 127234df42f5SElen Song return 0; 127334df42f5SElen Song 127434df42f5SElen Song chan_err: 127534df42f5SElen Song dev_err(port->dev, "RX channel not available, switch to pio\n"); 127636ce7cffSZheng Bin atmel_port->use_dma_rx = false; 127734df42f5SElen Song if (atmel_port->chan_rx) 127834df42f5SElen Song atmel_release_rx_dma(port); 127934df42f5SElen Song return -EINVAL; 128034df42f5SElen Song } 128134df42f5SElen Song 1282026cb432SKees Cook static void atmel_uart_timer_callback(struct timer_list *t) 12832e68c22fSElen Song { 1284026cb432SKees Cook struct atmel_uart_port *atmel_port = from_timer(atmel_port, t, 1285026cb432SKees Cook uart_timer); 1286026cb432SKees Cook struct uart_port *port = &atmel_port->uart; 12872e68c22fSElen Song 128898f2082cSNicolas Ferre if (!atomic_read(&atmel_port->tasklet_shutdown)) { 128900e8e658SNicolas Ferre tasklet_schedule(&atmel_port->tasklet_rx); 129098f2082cSNicolas Ferre mod_timer(&atmel_port->uart_timer, 129198f2082cSNicolas Ferre jiffies + uart_poll_timeout(port)); 129298f2082cSNicolas Ferre } 12932e68c22fSElen Song } 12942e68c22fSElen Song 1295ab4382d2SGreg Kroah-Hartman /* 1296ab4382d2SGreg Kroah-Hartman * receive interrupt handler. 1297ab4382d2SGreg Kroah-Hartman */ 1298ab4382d2SGreg Kroah-Hartman static void 1299ab4382d2SGreg Kroah-Hartman atmel_handle_receive(struct uart_port *port, unsigned int pending) 1300ab4382d2SGreg Kroah-Hartman { 1301ab4382d2SGreg Kroah-Hartman struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 1302ab4382d2SGreg Kroah-Hartman 130364e22ebeSElen Song if (atmel_use_pdc_rx(port)) { 1304ab4382d2SGreg Kroah-Hartman /* 1305ab4382d2SGreg Kroah-Hartman * PDC receive. Just schedule the tasklet and let it 1306ab4382d2SGreg Kroah-Hartman * figure out the details. 1307ab4382d2SGreg Kroah-Hartman * 1308ab4382d2SGreg Kroah-Hartman * TODO: We're not handling error flags correctly at 1309ab4382d2SGreg Kroah-Hartman * the moment. 1310ab4382d2SGreg Kroah-Hartman */ 1311ab4382d2SGreg Kroah-Hartman if (pending & (ATMEL_US_ENDRX | ATMEL_US_TIMEOUT)) { 13124e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_IDR, 13134e7decdaSCyrille Pitchen (ATMEL_US_ENDRX | ATMEL_US_TIMEOUT)); 131498f2082cSNicolas Ferre atmel_tasklet_schedule(atmel_port, 131598f2082cSNicolas Ferre &atmel_port->tasklet_rx); 1316ab4382d2SGreg Kroah-Hartman } 1317ab4382d2SGreg Kroah-Hartman 1318ab4382d2SGreg Kroah-Hartman if (pending & (ATMEL_US_RXBRK | ATMEL_US_OVRE | 1319ab4382d2SGreg Kroah-Hartman ATMEL_US_FRAME | ATMEL_US_PARE)) 1320ab4382d2SGreg Kroah-Hartman atmel_pdc_rxerr(port, pending); 1321ab4382d2SGreg Kroah-Hartman } 1322ab4382d2SGreg Kroah-Hartman 132334df42f5SElen Song if (atmel_use_dma_rx(port)) { 132434df42f5SElen Song if (pending & ATMEL_US_TIMEOUT) { 13254e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_IDR, 13264e7decdaSCyrille Pitchen ATMEL_US_TIMEOUT); 132798f2082cSNicolas Ferre atmel_tasklet_schedule(atmel_port, 132898f2082cSNicolas Ferre &atmel_port->tasklet_rx); 132934df42f5SElen Song } 133034df42f5SElen Song } 133134df42f5SElen Song 1332ab4382d2SGreg Kroah-Hartman /* Interrupt receive */ 1333ab4382d2SGreg Kroah-Hartman if (pending & ATMEL_US_RXRDY) 1334ab4382d2SGreg Kroah-Hartman atmel_rx_chars(port); 1335ab4382d2SGreg Kroah-Hartman else if (pending & ATMEL_US_RXBRK) { 1336ab4382d2SGreg Kroah-Hartman /* 1337ab4382d2SGreg Kroah-Hartman * End of break detected. If it came along with a 1338ab4382d2SGreg Kroah-Hartman * character, atmel_rx_chars will handle it. 1339ab4382d2SGreg Kroah-Hartman */ 13404e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA); 13414e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_IDR, ATMEL_US_RXBRK); 1342ab4382d2SGreg Kroah-Hartman atmel_port->break_active = 0; 1343ab4382d2SGreg Kroah-Hartman } 1344ab4382d2SGreg Kroah-Hartman } 1345ab4382d2SGreg Kroah-Hartman 1346ab4382d2SGreg Kroah-Hartman /* 1347ab4382d2SGreg Kroah-Hartman * transmit interrupt handler. (Transmit is IRQF_NODELAY safe) 1348ab4382d2SGreg Kroah-Hartman */ 1349ab4382d2SGreg Kroah-Hartman static void 1350ab4382d2SGreg Kroah-Hartman atmel_handle_transmit(struct uart_port *port, unsigned int pending) 1351ab4382d2SGreg Kroah-Hartman { 1352ab4382d2SGreg Kroah-Hartman struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 1353ab4382d2SGreg Kroah-Hartman 1354ab4382d2SGreg Kroah-Hartman if (pending & atmel_port->tx_done_mask) { 13554e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_IDR, 13564e7decdaSCyrille Pitchen atmel_port->tx_done_mask); 135769646d7aSRazvan Stefanescu 135869646d7aSRazvan Stefanescu /* Start RX if flag was set and FIFO is empty */ 135969646d7aSRazvan Stefanescu if (atmel_port->hd_start_rx) { 136069646d7aSRazvan Stefanescu if (!(atmel_uart_readl(port, ATMEL_US_CSR) 136169646d7aSRazvan Stefanescu & ATMEL_US_TXEMPTY)) 136269646d7aSRazvan Stefanescu dev_warn(port->dev, "Should start RX, but TX fifo is not empty\n"); 136369646d7aSRazvan Stefanescu 136469646d7aSRazvan Stefanescu atmel_port->hd_start_rx = false; 136569646d7aSRazvan Stefanescu atmel_start_rx(port); 136669646d7aSRazvan Stefanescu } 136769646d7aSRazvan Stefanescu 136898f2082cSNicolas Ferre atmel_tasklet_schedule(atmel_port, &atmel_port->tasklet_tx); 1369ab4382d2SGreg Kroah-Hartman } 1370ab4382d2SGreg Kroah-Hartman } 1371ab4382d2SGreg Kroah-Hartman 1372ab4382d2SGreg Kroah-Hartman /* 1373ab4382d2SGreg Kroah-Hartman * status flags interrupt handler. 1374ab4382d2SGreg Kroah-Hartman */ 1375ab4382d2SGreg Kroah-Hartman static void 1376ab4382d2SGreg Kroah-Hartman atmel_handle_status(struct uart_port *port, unsigned int pending, 1377ab4382d2SGreg Kroah-Hartman unsigned int status) 1378ab4382d2SGreg Kroah-Hartman { 1379ab4382d2SGreg Kroah-Hartman struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 13809205218eSNicolas Ferre unsigned int status_change; 1381ab4382d2SGreg Kroah-Hartman 1382ab4382d2SGreg Kroah-Hartman if (pending & (ATMEL_US_RIIC | ATMEL_US_DSRIC | ATMEL_US_DCDIC 1383ab4382d2SGreg Kroah-Hartman | ATMEL_US_CTSIC)) { 13849205218eSNicolas Ferre status_change = status ^ atmel_port->irq_status_prev; 1385d033e82dSLeilei Zhao atmel_port->irq_status_prev = status; 13869205218eSNicolas Ferre 13879205218eSNicolas Ferre if (status_change & (ATMEL_US_RI | ATMEL_US_DSR 13889205218eSNicolas Ferre | ATMEL_US_DCD | ATMEL_US_CTS)) { 13899205218eSNicolas Ferre /* TODO: All reads to CSR will clear these interrupts! */ 13909205218eSNicolas Ferre if (status_change & ATMEL_US_RI) 13919205218eSNicolas Ferre port->icount.rng++; 13929205218eSNicolas Ferre if (status_change & ATMEL_US_DSR) 13939205218eSNicolas Ferre port->icount.dsr++; 13949205218eSNicolas Ferre if (status_change & ATMEL_US_DCD) 13959205218eSNicolas Ferre uart_handle_dcd_change(port, !(status & ATMEL_US_DCD)); 13969205218eSNicolas Ferre if (status_change & ATMEL_US_CTS) 13979205218eSNicolas Ferre uart_handle_cts_change(port, !(status & ATMEL_US_CTS)); 13989205218eSNicolas Ferre 13999205218eSNicolas Ferre wake_up_interruptible(&port->state->port.delta_msr_wait); 14009205218eSNicolas Ferre } 1401ab4382d2SGreg Kroah-Hartman } 1402377fedd1SNicolas Ferre 1403377fedd1SNicolas Ferre if (pending & (ATMEL_US_NACK | ATMEL_US_ITERATION)) 1404377fedd1SNicolas Ferre dev_dbg(port->dev, "ISO7816 ERROR (0x%08x)\n", pending); 1405ab4382d2SGreg Kroah-Hartman } 1406ab4382d2SGreg Kroah-Hartman 1407ab4382d2SGreg Kroah-Hartman /* 1408ab4382d2SGreg Kroah-Hartman * Interrupt handler 1409ab4382d2SGreg Kroah-Hartman */ 1410ab4382d2SGreg Kroah-Hartman static irqreturn_t atmel_interrupt(int irq, void *dev_id) 1411ab4382d2SGreg Kroah-Hartman { 1412ab4382d2SGreg Kroah-Hartman struct uart_port *port = dev_id; 1413ab5e4e41SRichard Genoud struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 14142c7af5baSBoris BREZILLON unsigned int status, pending, mask, pass_counter = 0; 1415ab4382d2SGreg Kroah-Hartman 14162c7af5baSBoris BREZILLON spin_lock(&atmel_port->lock_suspended); 14172c7af5baSBoris BREZILLON 1418ab4382d2SGreg Kroah-Hartman do { 1419d2d8d4c0SRichard Genoud status = atmel_uart_readl(port, ATMEL_US_CSR); 14204e7decdaSCyrille Pitchen mask = atmel_uart_readl(port, ATMEL_US_IMR); 14212c7af5baSBoris BREZILLON pending = status & mask; 1422ab4382d2SGreg Kroah-Hartman if (!pending) 1423ab4382d2SGreg Kroah-Hartman break; 1424ab4382d2SGreg Kroah-Hartman 14252c7af5baSBoris BREZILLON if (atmel_port->suspended) { 14262c7af5baSBoris BREZILLON atmel_port->pending |= pending; 14272c7af5baSBoris BREZILLON atmel_port->pending_status = status; 14284e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_IDR, mask); 14292c7af5baSBoris BREZILLON pm_system_wakeup(); 14302c7af5baSBoris BREZILLON break; 14312c7af5baSBoris BREZILLON } 14322c7af5baSBoris BREZILLON 1433ab4382d2SGreg Kroah-Hartman atmel_handle_receive(port, pending); 1434ab4382d2SGreg Kroah-Hartman atmel_handle_status(port, pending, status); 1435ab4382d2SGreg Kroah-Hartman atmel_handle_transmit(port, pending); 1436ab4382d2SGreg Kroah-Hartman } while (pass_counter++ < ATMEL_ISR_PASS_LIMIT); 1437ab4382d2SGreg Kroah-Hartman 14382c7af5baSBoris BREZILLON spin_unlock(&atmel_port->lock_suspended); 14392c7af5baSBoris BREZILLON 1440ab4382d2SGreg Kroah-Hartman return pass_counter ? IRQ_HANDLED : IRQ_NONE; 1441ab4382d2SGreg Kroah-Hartman } 1442ab4382d2SGreg Kroah-Hartman 1443a930e528SElen Song static void atmel_release_tx_pdc(struct uart_port *port) 1444a930e528SElen Song { 1445a930e528SElen Song struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 1446a930e528SElen Song struct atmel_dma_buffer *pdc = &atmel_port->pdc_tx; 1447a930e528SElen Song 1448a930e528SElen Song dma_unmap_single(port->dev, 1449a930e528SElen Song pdc->dma_addr, 1450a930e528SElen Song pdc->dma_size, 1451a930e528SElen Song DMA_TO_DEVICE); 1452a930e528SElen Song } 1453a930e528SElen Song 1454ab4382d2SGreg Kroah-Hartman /* 1455ab4382d2SGreg Kroah-Hartman * Called from tasklet with ENDTX and TXBUFE interrupts disabled. 1456ab4382d2SGreg Kroah-Hartman */ 145764e22ebeSElen Song static void atmel_tx_pdc(struct uart_port *port) 1458ab4382d2SGreg Kroah-Hartman { 1459ab4382d2SGreg Kroah-Hartman struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 1460ab4382d2SGreg Kroah-Hartman struct circ_buf *xmit = &port->state->xmit; 1461ab4382d2SGreg Kroah-Hartman struct atmel_dma_buffer *pdc = &atmel_port->pdc_tx; 1462ab4382d2SGreg Kroah-Hartman int count; 1463ab4382d2SGreg Kroah-Hartman 1464ab4382d2SGreg Kroah-Hartman /* nothing left to transmit? */ 14654e7decdaSCyrille Pitchen if (atmel_uart_readl(port, ATMEL_PDC_TCR)) 1466ab4382d2SGreg Kroah-Hartman return; 1467ab4382d2SGreg Kroah-Hartman 1468ab4382d2SGreg Kroah-Hartman xmit->tail += pdc->ofs; 1469ab4382d2SGreg Kroah-Hartman xmit->tail &= UART_XMIT_SIZE - 1; 1470ab4382d2SGreg Kroah-Hartman 1471ab4382d2SGreg Kroah-Hartman port->icount.tx += pdc->ofs; 1472ab4382d2SGreg Kroah-Hartman pdc->ofs = 0; 1473ab4382d2SGreg Kroah-Hartman 1474ab4382d2SGreg Kroah-Hartman /* more to transmit - setup next transfer */ 1475ab4382d2SGreg Kroah-Hartman 1476ab4382d2SGreg Kroah-Hartman /* disable PDC transmit */ 14774e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_TXTDIS); 1478ab4382d2SGreg Kroah-Hartman 1479ab4382d2SGreg Kroah-Hartman if (!uart_circ_empty(xmit) && !uart_tx_stopped(port)) { 1480ab4382d2SGreg Kroah-Hartman dma_sync_single_for_device(port->dev, 1481ab4382d2SGreg Kroah-Hartman pdc->dma_addr, 1482ab4382d2SGreg Kroah-Hartman pdc->dma_size, 1483ab4382d2SGreg Kroah-Hartman DMA_TO_DEVICE); 1484ab4382d2SGreg Kroah-Hartman 1485ab4382d2SGreg Kroah-Hartman count = CIRC_CNT_TO_END(xmit->head, xmit->tail, UART_XMIT_SIZE); 1486ab4382d2SGreg Kroah-Hartman pdc->ofs = count; 1487ab4382d2SGreg Kroah-Hartman 14884e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_PDC_TPR, 14894e7decdaSCyrille Pitchen pdc->dma_addr + xmit->tail); 14904e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_PDC_TCR, count); 1491ab4382d2SGreg Kroah-Hartman /* re-enable PDC transmit */ 14924e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_TXTEN); 1493ab4382d2SGreg Kroah-Hartman /* Enable interrupts */ 14944e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_IER, 14954e7decdaSCyrille Pitchen atmel_port->tx_done_mask); 1496ab4382d2SGreg Kroah-Hartman } else { 1497f3040983SRazvan Stefanescu if (atmel_uart_is_half_duplex(port)) { 1498ab4382d2SGreg Kroah-Hartman /* DMA done, stop TX, start RX for RS485 */ 1499ab4382d2SGreg Kroah-Hartman atmel_start_rx(port); 1500ab4382d2SGreg Kroah-Hartman } 1501ab4382d2SGreg Kroah-Hartman } 1502ab4382d2SGreg Kroah-Hartman 1503ab4382d2SGreg Kroah-Hartman if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS) 1504ab4382d2SGreg Kroah-Hartman uart_write_wakeup(port); 1505ab4382d2SGreg Kroah-Hartman } 1506ab4382d2SGreg Kroah-Hartman 1507a930e528SElen Song static int atmel_prepare_tx_pdc(struct uart_port *port) 1508a930e528SElen Song { 1509a930e528SElen Song struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 1510a930e528SElen Song struct atmel_dma_buffer *pdc = &atmel_port->pdc_tx; 1511a930e528SElen Song struct circ_buf *xmit = &port->state->xmit; 1512a930e528SElen Song 1513a930e528SElen Song pdc->buf = xmit->buf; 1514a930e528SElen Song pdc->dma_addr = dma_map_single(port->dev, 1515a930e528SElen Song pdc->buf, 1516a930e528SElen Song UART_XMIT_SIZE, 1517a930e528SElen Song DMA_TO_DEVICE); 1518a930e528SElen Song pdc->dma_size = UART_XMIT_SIZE; 1519a930e528SElen Song pdc->ofs = 0; 1520a930e528SElen Song 1521a930e528SElen Song return 0; 1522a930e528SElen Song } 1523a930e528SElen Song 1524ab4382d2SGreg Kroah-Hartman static void atmel_rx_from_ring(struct uart_port *port) 1525ab4382d2SGreg Kroah-Hartman { 1526ab4382d2SGreg Kroah-Hartman struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 1527ab4382d2SGreg Kroah-Hartman struct circ_buf *ring = &atmel_port->rx_ring; 1528ab4382d2SGreg Kroah-Hartman unsigned int flg; 1529ab4382d2SGreg Kroah-Hartman unsigned int status; 1530ab4382d2SGreg Kroah-Hartman 1531ab4382d2SGreg Kroah-Hartman while (ring->head != ring->tail) { 1532ab4382d2SGreg Kroah-Hartman struct atmel_uart_char c; 1533ab4382d2SGreg Kroah-Hartman 1534ab4382d2SGreg Kroah-Hartman /* Make sure c is loaded after head. */ 1535ab4382d2SGreg Kroah-Hartman smp_rmb(); 1536ab4382d2SGreg Kroah-Hartman 1537ab4382d2SGreg Kroah-Hartman c = ((struct atmel_uart_char *)ring->buf)[ring->tail]; 1538ab4382d2SGreg Kroah-Hartman 1539ab4382d2SGreg Kroah-Hartman ring->tail = (ring->tail + 1) & (ATMEL_SERIAL_RINGSIZE - 1); 1540ab4382d2SGreg Kroah-Hartman 1541ab4382d2SGreg Kroah-Hartman port->icount.rx++; 1542ab4382d2SGreg Kroah-Hartman status = c.status; 1543ab4382d2SGreg Kroah-Hartman flg = TTY_NORMAL; 1544ab4382d2SGreg Kroah-Hartman 1545ab4382d2SGreg Kroah-Hartman /* 1546ab4382d2SGreg Kroah-Hartman * note that the error handling code is 1547ab4382d2SGreg Kroah-Hartman * out of the main execution path 1548ab4382d2SGreg Kroah-Hartman */ 1549ab4382d2SGreg Kroah-Hartman if (unlikely(status & (ATMEL_US_PARE | ATMEL_US_FRAME 1550ab4382d2SGreg Kroah-Hartman | ATMEL_US_OVRE | ATMEL_US_RXBRK))) { 1551ab4382d2SGreg Kroah-Hartman if (status & ATMEL_US_RXBRK) { 1552ab4382d2SGreg Kroah-Hartman /* ignore side-effect */ 1553ab4382d2SGreg Kroah-Hartman status &= ~(ATMEL_US_PARE | ATMEL_US_FRAME); 1554ab4382d2SGreg Kroah-Hartman 1555ab4382d2SGreg Kroah-Hartman port->icount.brk++; 1556ab4382d2SGreg Kroah-Hartman if (uart_handle_break(port)) 1557ab4382d2SGreg Kroah-Hartman continue; 1558ab4382d2SGreg Kroah-Hartman } 1559ab4382d2SGreg Kroah-Hartman if (status & ATMEL_US_PARE) 1560ab4382d2SGreg Kroah-Hartman port->icount.parity++; 1561ab4382d2SGreg Kroah-Hartman if (status & ATMEL_US_FRAME) 1562ab4382d2SGreg Kroah-Hartman port->icount.frame++; 1563ab4382d2SGreg Kroah-Hartman if (status & ATMEL_US_OVRE) 1564ab4382d2SGreg Kroah-Hartman port->icount.overrun++; 1565ab4382d2SGreg Kroah-Hartman 1566ab4382d2SGreg Kroah-Hartman status &= port->read_status_mask; 1567ab4382d2SGreg Kroah-Hartman 1568ab4382d2SGreg Kroah-Hartman if (status & ATMEL_US_RXBRK) 1569ab4382d2SGreg Kroah-Hartman flg = TTY_BREAK; 1570ab4382d2SGreg Kroah-Hartman else if (status & ATMEL_US_PARE) 1571ab4382d2SGreg Kroah-Hartman flg = TTY_PARITY; 1572ab4382d2SGreg Kroah-Hartman else if (status & ATMEL_US_FRAME) 1573ab4382d2SGreg Kroah-Hartman flg = TTY_FRAME; 1574ab4382d2SGreg Kroah-Hartman } 1575ab4382d2SGreg Kroah-Hartman 1576ab4382d2SGreg Kroah-Hartman 1577ab4382d2SGreg Kroah-Hartman if (uart_handle_sysrq_char(port, c.ch)) 1578ab4382d2SGreg Kroah-Hartman continue; 1579ab4382d2SGreg Kroah-Hartman 1580ab4382d2SGreg Kroah-Hartman uart_insert_char(port, status, ATMEL_US_OVRE, c.ch, flg); 1581ab4382d2SGreg Kroah-Hartman } 1582ab4382d2SGreg Kroah-Hartman 15832e124b4aSJiri Slaby tty_flip_buffer_push(&port->state->port); 1584ab4382d2SGreg Kroah-Hartman } 1585ab4382d2SGreg Kroah-Hartman 1586a930e528SElen Song static void atmel_release_rx_pdc(struct uart_port *port) 1587a930e528SElen Song { 1588a930e528SElen Song struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 1589a930e528SElen Song int i; 1590a930e528SElen Song 1591a930e528SElen Song for (i = 0; i < 2; i++) { 1592a930e528SElen Song struct atmel_dma_buffer *pdc = &atmel_port->pdc_rx[i]; 1593a930e528SElen Song 1594a930e528SElen Song dma_unmap_single(port->dev, 1595a930e528SElen Song pdc->dma_addr, 1596a930e528SElen Song pdc->dma_size, 1597a930e528SElen Song DMA_FROM_DEVICE); 1598a930e528SElen Song kfree(pdc->buf); 1599a930e528SElen Song } 1600a930e528SElen Song } 1601a930e528SElen Song 160264e22ebeSElen Song static void atmel_rx_from_pdc(struct uart_port *port) 1603ab4382d2SGreg Kroah-Hartman { 1604ab4382d2SGreg Kroah-Hartman struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 160505c7cd39SJiri Slaby struct tty_port *tport = &port->state->port; 1606ab4382d2SGreg Kroah-Hartman struct atmel_dma_buffer *pdc; 1607ab4382d2SGreg Kroah-Hartman int rx_idx = atmel_port->pdc_rx_idx; 1608ab4382d2SGreg Kroah-Hartman unsigned int head; 1609ab4382d2SGreg Kroah-Hartman unsigned int tail; 1610ab4382d2SGreg Kroah-Hartman unsigned int count; 1611ab4382d2SGreg Kroah-Hartman 1612ab4382d2SGreg Kroah-Hartman do { 1613ab4382d2SGreg Kroah-Hartman /* Reset the UART timeout early so that we don't miss one */ 16144e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_STTTO); 1615ab4382d2SGreg Kroah-Hartman 1616ab4382d2SGreg Kroah-Hartman pdc = &atmel_port->pdc_rx[rx_idx]; 16174e7decdaSCyrille Pitchen head = atmel_uart_readl(port, ATMEL_PDC_RPR) - pdc->dma_addr; 1618ab4382d2SGreg Kroah-Hartman tail = pdc->ofs; 1619ab4382d2SGreg Kroah-Hartman 1620ab4382d2SGreg Kroah-Hartman /* If the PDC has switched buffers, RPR won't contain 1621ab4382d2SGreg Kroah-Hartman * any address within the current buffer. Since head 1622ab4382d2SGreg Kroah-Hartman * is unsigned, we just need a one-way comparison to 1623ab4382d2SGreg Kroah-Hartman * find out. 1624ab4382d2SGreg Kroah-Hartman * 1625ab4382d2SGreg Kroah-Hartman * In this case, we just need to consume the entire 1626ab4382d2SGreg Kroah-Hartman * buffer and resubmit it for DMA. This will clear the 1627ab4382d2SGreg Kroah-Hartman * ENDRX bit as well, so that we can safely re-enable 1628ab4382d2SGreg Kroah-Hartman * all interrupts below. 1629ab4382d2SGreg Kroah-Hartman */ 1630ab4382d2SGreg Kroah-Hartman head = min(head, pdc->dma_size); 1631ab4382d2SGreg Kroah-Hartman 1632ab4382d2SGreg Kroah-Hartman if (likely(head != tail)) { 1633ab4382d2SGreg Kroah-Hartman dma_sync_single_for_cpu(port->dev, pdc->dma_addr, 1634ab4382d2SGreg Kroah-Hartman pdc->dma_size, DMA_FROM_DEVICE); 1635ab4382d2SGreg Kroah-Hartman 1636ab4382d2SGreg Kroah-Hartman /* 1637ab4382d2SGreg Kroah-Hartman * head will only wrap around when we recycle 1638ab4382d2SGreg Kroah-Hartman * the DMA buffer, and when that happens, we 1639ab4382d2SGreg Kroah-Hartman * explicitly set tail to 0. So head will 1640ab4382d2SGreg Kroah-Hartman * always be greater than tail. 1641ab4382d2SGreg Kroah-Hartman */ 1642ab4382d2SGreg Kroah-Hartman count = head - tail; 1643ab4382d2SGreg Kroah-Hartman 164405c7cd39SJiri Slaby tty_insert_flip_string(tport, pdc->buf + pdc->ofs, 164505c7cd39SJiri Slaby count); 1646ab4382d2SGreg Kroah-Hartman 1647ab4382d2SGreg Kroah-Hartman dma_sync_single_for_device(port->dev, pdc->dma_addr, 1648ab4382d2SGreg Kroah-Hartman pdc->dma_size, DMA_FROM_DEVICE); 1649ab4382d2SGreg Kroah-Hartman 1650ab4382d2SGreg Kroah-Hartman port->icount.rx += count; 1651ab4382d2SGreg Kroah-Hartman pdc->ofs = head; 1652ab4382d2SGreg Kroah-Hartman } 1653ab4382d2SGreg Kroah-Hartman 1654ab4382d2SGreg Kroah-Hartman /* 1655ab4382d2SGreg Kroah-Hartman * If the current buffer is full, we need to check if 1656ab4382d2SGreg Kroah-Hartman * the next one contains any additional data. 1657ab4382d2SGreg Kroah-Hartman */ 1658ab4382d2SGreg Kroah-Hartman if (head >= pdc->dma_size) { 1659ab4382d2SGreg Kroah-Hartman pdc->ofs = 0; 16604e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_PDC_RNPR, pdc->dma_addr); 16614e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_PDC_RNCR, pdc->dma_size); 1662ab4382d2SGreg Kroah-Hartman 1663ab4382d2SGreg Kroah-Hartman rx_idx = !rx_idx; 1664ab4382d2SGreg Kroah-Hartman atmel_port->pdc_rx_idx = rx_idx; 1665ab4382d2SGreg Kroah-Hartman } 1666ab4382d2SGreg Kroah-Hartman } while (head >= pdc->dma_size); 1667ab4382d2SGreg Kroah-Hartman 16682e124b4aSJiri Slaby tty_flip_buffer_push(tport); 1669ab4382d2SGreg Kroah-Hartman 16704e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_IER, 16714e7decdaSCyrille Pitchen ATMEL_US_ENDRX | ATMEL_US_TIMEOUT); 1672ab4382d2SGreg Kroah-Hartman } 1673ab4382d2SGreg Kroah-Hartman 1674a930e528SElen Song static int atmel_prepare_rx_pdc(struct uart_port *port) 1675a930e528SElen Song { 1676a930e528SElen Song struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 1677a930e528SElen Song int i; 1678a930e528SElen Song 1679a930e528SElen Song for (i = 0; i < 2; i++) { 1680a930e528SElen Song struct atmel_dma_buffer *pdc = &atmel_port->pdc_rx[i]; 1681a930e528SElen Song 1682a930e528SElen Song pdc->buf = kmalloc(PDC_BUFFER_SIZE, GFP_KERNEL); 1683a930e528SElen Song if (pdc->buf == NULL) { 1684a930e528SElen Song if (i != 0) { 1685a930e528SElen Song dma_unmap_single(port->dev, 1686a930e528SElen Song atmel_port->pdc_rx[0].dma_addr, 1687a930e528SElen Song PDC_BUFFER_SIZE, 1688a930e528SElen Song DMA_FROM_DEVICE); 1689a930e528SElen Song kfree(atmel_port->pdc_rx[0].buf); 1690a930e528SElen Song } 169136ce7cffSZheng Bin atmel_port->use_pdc_rx = false; 1692a930e528SElen Song return -ENOMEM; 1693a930e528SElen Song } 1694a930e528SElen Song pdc->dma_addr = dma_map_single(port->dev, 1695a930e528SElen Song pdc->buf, 1696a930e528SElen Song PDC_BUFFER_SIZE, 1697a930e528SElen Song DMA_FROM_DEVICE); 1698a930e528SElen Song pdc->dma_size = PDC_BUFFER_SIZE; 1699a930e528SElen Song pdc->ofs = 0; 1700a930e528SElen Song } 1701a930e528SElen Song 1702a930e528SElen Song atmel_port->pdc_rx_idx = 0; 1703a930e528SElen Song 17044e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_PDC_RPR, atmel_port->pdc_rx[0].dma_addr); 17054e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_PDC_RCR, PDC_BUFFER_SIZE); 1706a930e528SElen Song 17074e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_PDC_RNPR, 17084e7decdaSCyrille Pitchen atmel_port->pdc_rx[1].dma_addr); 17094e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_PDC_RNCR, PDC_BUFFER_SIZE); 1710a930e528SElen Song 1711a930e528SElen Song return 0; 1712a930e528SElen Song } 1713a930e528SElen Song 1714ab4382d2SGreg Kroah-Hartman /* 1715ab4382d2SGreg Kroah-Hartman * tasklet handling tty stuff outside the interrupt handler. 1716ab4382d2SGreg Kroah-Hartman */ 171741e85e44SAllen Pais static void atmel_tasklet_rx_func(struct tasklet_struct *t) 1718ab4382d2SGreg Kroah-Hartman { 171941e85e44SAllen Pais struct atmel_uart_port *atmel_port = from_tasklet(atmel_port, t, 172041e85e44SAllen Pais tasklet_rx); 172141e85e44SAllen Pais struct uart_port *port = &atmel_port->uart; 1722ab4382d2SGreg Kroah-Hartman 1723ab4382d2SGreg Kroah-Hartman /* The interrupt handler does not take the lock */ 1724ab4382d2SGreg Kroah-Hartman spin_lock(&port->lock); 1725a930e528SElen Song atmel_port->schedule_rx(port); 172600e8e658SNicolas Ferre spin_unlock(&port->lock); 172700e8e658SNicolas Ferre } 1728ab4382d2SGreg Kroah-Hartman 172941e85e44SAllen Pais static void atmel_tasklet_tx_func(struct tasklet_struct *t) 173000e8e658SNicolas Ferre { 173141e85e44SAllen Pais struct atmel_uart_port *atmel_port = from_tasklet(atmel_port, t, 173241e85e44SAllen Pais tasklet_tx); 173341e85e44SAllen Pais struct uart_port *port = &atmel_port->uart; 173400e8e658SNicolas Ferre 173500e8e658SNicolas Ferre /* The interrupt handler does not take the lock */ 173600e8e658SNicolas Ferre spin_lock(&port->lock); 173700e8e658SNicolas Ferre atmel_port->schedule_tx(port); 1738ab4382d2SGreg Kroah-Hartman spin_unlock(&port->lock); 1739ab4382d2SGreg Kroah-Hartman } 1740ab4382d2SGreg Kroah-Hartman 17414a1e8888SLeilei Zhao static void atmel_init_property(struct atmel_uart_port *atmel_port, 174233d64c4fSElen Song struct platform_device *pdev) 174333d64c4fSElen Song { 174433d64c4fSElen Song struct device_node *np = pdev->dev.of_node; 174533d64c4fSElen Song 174633d64c4fSElen Song /* DMA/PDC usage specification */ 1747490d5ce2SJulia Lawall if (of_property_read_bool(np, "atmel,use-dma-rx")) { 1748490d5ce2SJulia Lawall if (of_property_read_bool(np, "dmas")) { 174933d64c4fSElen Song atmel_port->use_dma_rx = true; 175033d64c4fSElen Song atmel_port->use_pdc_rx = false; 175133d64c4fSElen Song } else { 175233d64c4fSElen Song atmel_port->use_dma_rx = false; 175333d64c4fSElen Song atmel_port->use_pdc_rx = true; 175433d64c4fSElen Song } 175533d64c4fSElen Song } else { 175633d64c4fSElen Song atmel_port->use_dma_rx = false; 175733d64c4fSElen Song atmel_port->use_pdc_rx = false; 175833d64c4fSElen Song } 175933d64c4fSElen Song 1760490d5ce2SJulia Lawall if (of_property_read_bool(np, "atmel,use-dma-tx")) { 1761490d5ce2SJulia Lawall if (of_property_read_bool(np, "dmas")) { 176233d64c4fSElen Song atmel_port->use_dma_tx = true; 176333d64c4fSElen Song atmel_port->use_pdc_tx = false; 176433d64c4fSElen Song } else { 176533d64c4fSElen Song atmel_port->use_dma_tx = false; 176633d64c4fSElen Song atmel_port->use_pdc_tx = true; 176733d64c4fSElen Song } 176833d64c4fSElen Song } else { 176933d64c4fSElen Song atmel_port->use_dma_tx = false; 177033d64c4fSElen Song atmel_port->use_pdc_tx = false; 177133d64c4fSElen Song } 177233d64c4fSElen Song } 177333d64c4fSElen Song 1774a930e528SElen Song static void atmel_set_ops(struct uart_port *port) 1775a930e528SElen Song { 1776a930e528SElen Song struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 1777a930e528SElen Song 177834df42f5SElen Song if (atmel_use_dma_rx(port)) { 177934df42f5SElen Song atmel_port->prepare_rx = &atmel_prepare_rx_dma; 178034df42f5SElen Song atmel_port->schedule_rx = &atmel_rx_from_dma; 178134df42f5SElen Song atmel_port->release_rx = &atmel_release_rx_dma; 178234df42f5SElen Song } else if (atmel_use_pdc_rx(port)) { 1783a930e528SElen Song atmel_port->prepare_rx = &atmel_prepare_rx_pdc; 1784a930e528SElen Song atmel_port->schedule_rx = &atmel_rx_from_pdc; 1785a930e528SElen Song atmel_port->release_rx = &atmel_release_rx_pdc; 1786a930e528SElen Song } else { 1787a930e528SElen Song atmel_port->prepare_rx = NULL; 1788a930e528SElen Song atmel_port->schedule_rx = &atmel_rx_from_ring; 1789a930e528SElen Song atmel_port->release_rx = NULL; 1790a930e528SElen Song } 1791a930e528SElen Song 179208f738beSElen Song if (atmel_use_dma_tx(port)) { 179308f738beSElen Song atmel_port->prepare_tx = &atmel_prepare_tx_dma; 179408f738beSElen Song atmel_port->schedule_tx = &atmel_tx_dma; 179508f738beSElen Song atmel_port->release_tx = &atmel_release_tx_dma; 179608f738beSElen Song } else if (atmel_use_pdc_tx(port)) { 1797a930e528SElen Song atmel_port->prepare_tx = &atmel_prepare_tx_pdc; 1798a930e528SElen Song atmel_port->schedule_tx = &atmel_tx_pdc; 1799a930e528SElen Song atmel_port->release_tx = &atmel_release_tx_pdc; 1800a930e528SElen Song } else { 1801a930e528SElen Song atmel_port->prepare_tx = NULL; 1802a930e528SElen Song atmel_port->schedule_tx = &atmel_tx_chars; 1803a930e528SElen Song atmel_port->release_tx = NULL; 1804a930e528SElen Song } 1805a930e528SElen Song } 1806a930e528SElen Song 1807ab4382d2SGreg Kroah-Hartman /* 1808055560b0SElen Song * Get ip name usart or uart 1809055560b0SElen Song */ 1810892db58bSNicolas Ferre static void atmel_get_ip_name(struct uart_port *port) 1811055560b0SElen Song { 1812055560b0SElen Song struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 18134e7decdaSCyrille Pitchen int name = atmel_uart_readl(port, ATMEL_US_NAME); 1814731d9caeSNicolas Ferre u32 version; 18151d673fb9SNicolas Ferre u32 usart, dbgu_uart, new_uart; 18164b769371SNicolas Ferre /* ASCII decoding for IP version */ 18174b769371SNicolas Ferre usart = 0x55534152; /* USAR(T) */ 18184b769371SNicolas Ferre dbgu_uart = 0x44424755; /* DBGU */ 18191d673fb9SNicolas Ferre new_uart = 0x55415254; /* UART */ 1820055560b0SElen Song 18215bf5635aSLudovic Desroches /* 18225bf5635aSLudovic Desroches * Only USART devices from at91sam9260 SOC implement fractional 18232867af2dSRomain Izard * baudrate. It is available for all asynchronous modes, with the 18242867af2dSRomain Izard * following restriction: the sampling clock's duty cycle is not 18252867af2dSRomain Izard * constant. 18265bf5635aSLudovic Desroches */ 18275bf5635aSLudovic Desroches atmel_port->has_frac_baudrate = false; 18284b769371SNicolas Ferre atmel_port->has_hw_timer = false; 1829*5644bf18SSergiu Moga atmel_port->is_usart = false; 1830055560b0SElen Song 18312958cceeSLudovic Desroches if (name == new_uart) { 18322958cceeSLudovic Desroches dev_dbg(port->dev, "Uart with hw timer"); 18334b769371SNicolas Ferre atmel_port->has_hw_timer = true; 18342958cceeSLudovic Desroches atmel_port->rtor = ATMEL_UA_RTOR; 18352958cceeSLudovic Desroches } else if (name == usart) { 18362958cceeSLudovic Desroches dev_dbg(port->dev, "Usart\n"); 18375bf5635aSLudovic Desroches atmel_port->has_frac_baudrate = true; 18382958cceeSLudovic Desroches atmel_port->has_hw_timer = true; 1839*5644bf18SSergiu Moga atmel_port->is_usart = true; 18402958cceeSLudovic Desroches atmel_port->rtor = ATMEL_US_RTOR; 1841377fedd1SNicolas Ferre version = atmel_uart_readl(port, ATMEL_US_VERSION); 1842377fedd1SNicolas Ferre switch (version) { 1843377fedd1SNicolas Ferre case 0x814: /* sama5d2 */ 1844df561f66SGustavo A. R. Silva fallthrough; 1845377fedd1SNicolas Ferre case 0x701: /* sama5d4 */ 1846377fedd1SNicolas Ferre atmel_port->fidi_min = 3; 1847377fedd1SNicolas Ferre atmel_port->fidi_max = 65535; 1848377fedd1SNicolas Ferre break; 1849377fedd1SNicolas Ferre case 0x502: /* sam9x5, sama5d3 */ 1850377fedd1SNicolas Ferre atmel_port->fidi_min = 3; 1851377fedd1SNicolas Ferre atmel_port->fidi_max = 2047; 1852377fedd1SNicolas Ferre break; 1853377fedd1SNicolas Ferre default: 1854377fedd1SNicolas Ferre atmel_port->fidi_min = 1; 1855377fedd1SNicolas Ferre atmel_port->fidi_max = 2047; 1856377fedd1SNicolas Ferre } 18574b769371SNicolas Ferre } else if (name == dbgu_uart) { 18584b769371SNicolas Ferre dev_dbg(port->dev, "Dbgu or uart without hw timer\n"); 1859055560b0SElen Song } else { 1860731d9caeSNicolas Ferre /* fallback for older SoCs: use version field */ 18614e7decdaSCyrille Pitchen version = atmel_uart_readl(port, ATMEL_US_VERSION); 1862731d9caeSNicolas Ferre switch (version) { 1863731d9caeSNicolas Ferre case 0x302: 1864731d9caeSNicolas Ferre case 0x10213: 1865fd63a890SJonas Danielsson case 0x10302: 1866731d9caeSNicolas Ferre dev_dbg(port->dev, "This version is usart\n"); 18675bf5635aSLudovic Desroches atmel_port->has_frac_baudrate = true; 18684b769371SNicolas Ferre atmel_port->has_hw_timer = true; 1869*5644bf18SSergiu Moga atmel_port->is_usart = true; 18702958cceeSLudovic Desroches atmel_port->rtor = ATMEL_US_RTOR; 1871731d9caeSNicolas Ferre break; 1872731d9caeSNicolas Ferre case 0x203: 1873731d9caeSNicolas Ferre case 0x10202: 1874731d9caeSNicolas Ferre dev_dbg(port->dev, "This version is uart\n"); 1875731d9caeSNicolas Ferre break; 1876731d9caeSNicolas Ferre default: 1877731d9caeSNicolas Ferre dev_err(port->dev, "Not supported ip name nor version, set to uart\n"); 1878731d9caeSNicolas Ferre } 1879055560b0SElen Song } 1880055560b0SElen Song } 1881055560b0SElen Song 1882055560b0SElen Song /* 1883ab4382d2SGreg Kroah-Hartman * Perform initialization and enable port for reception 1884ab4382d2SGreg Kroah-Hartman */ 1885ab4382d2SGreg Kroah-Hartman static int atmel_startup(struct uart_port *port) 1886ab4382d2SGreg Kroah-Hartman { 188733d64c4fSElen Song struct platform_device *pdev = to_platform_device(port->dev); 1888ab4382d2SGreg Kroah-Hartman struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 1889ab4382d2SGreg Kroah-Hartman int retval; 1890ab4382d2SGreg Kroah-Hartman 1891ab4382d2SGreg Kroah-Hartman /* 1892ab4382d2SGreg Kroah-Hartman * Ensure that no interrupts are enabled otherwise when 1893ab4382d2SGreg Kroah-Hartman * request_irq() is called we could get stuck trying to 1894ab4382d2SGreg Kroah-Hartman * handle an unexpected interrupt 1895ab4382d2SGreg Kroah-Hartman */ 18964e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_IDR, -1); 1897ab5e4e41SRichard Genoud atmel_port->ms_irq_enabled = false; 1898ab4382d2SGreg Kroah-Hartman 1899ab4382d2SGreg Kroah-Hartman /* 1900ab4382d2SGreg Kroah-Hartman * Allocate the IRQ 1901ab4382d2SGreg Kroah-Hartman */ 19022c7af5baSBoris BREZILLON retval = request_irq(port->irq, atmel_interrupt, 19032c7af5baSBoris BREZILLON IRQF_SHARED | IRQF_COND_SUSPEND, 19049594b5beSSebastian Andrzej Siewior dev_name(&pdev->dev), port); 1905ab4382d2SGreg Kroah-Hartman if (retval) { 1906ddaa6037SRichard Genoud dev_err(port->dev, "atmel_startup - Can't get irq\n"); 1907ab4382d2SGreg Kroah-Hartman return retval; 1908ab4382d2SGreg Kroah-Hartman } 1909ab4382d2SGreg Kroah-Hartman 191098f2082cSNicolas Ferre atomic_set(&atmel_port->tasklet_shutdown, 0); 191141e85e44SAllen Pais tasklet_setup(&atmel_port->tasklet_rx, atmel_tasklet_rx_func); 191241e85e44SAllen Pais tasklet_setup(&atmel_port->tasklet_tx, atmel_tasklet_tx_func); 19131e125786SLeilei Zhao 1914ab5e4e41SRichard Genoud /* 1915ab4382d2SGreg Kroah-Hartman * Initialize DMA (if necessary) 1916ab4382d2SGreg Kroah-Hartman */ 191733d64c4fSElen Song atmel_init_property(atmel_port, pdev); 19184d9628a1SLeilei Zhao atmel_set_ops(port); 191933d64c4fSElen Song 1920a930e528SElen Song if (atmel_port->prepare_rx) { 1921a930e528SElen Song retval = atmel_port->prepare_rx(port); 1922a930e528SElen Song if (retval < 0) 1923a930e528SElen Song atmel_set_ops(port); 1924ab4382d2SGreg Kroah-Hartman } 1925ab4382d2SGreg Kroah-Hartman 1926a930e528SElen Song if (atmel_port->prepare_tx) { 1927a930e528SElen Song retval = atmel_port->prepare_tx(port); 1928a930e528SElen Song if (retval < 0) 1929a930e528SElen Song atmel_set_ops(port); 1930ab4382d2SGreg Kroah-Hartman } 1931ab4382d2SGreg Kroah-Hartman 1932b5199d46SCyrille Pitchen /* 1933b5199d46SCyrille Pitchen * Enable FIFO when available 1934b5199d46SCyrille Pitchen */ 1935b5199d46SCyrille Pitchen if (atmel_port->fifo_size) { 1936b5199d46SCyrille Pitchen unsigned int txrdym = ATMEL_US_ONE_DATA; 1937b5199d46SCyrille Pitchen unsigned int rxrdym = ATMEL_US_ONE_DATA; 1938b5199d46SCyrille Pitchen unsigned int fmr; 1939b5199d46SCyrille Pitchen 1940b5199d46SCyrille Pitchen atmel_uart_writel(port, ATMEL_US_CR, 1941b5199d46SCyrille Pitchen ATMEL_US_FIFOEN | 1942b5199d46SCyrille Pitchen ATMEL_US_RXFCLR | 1943b5199d46SCyrille Pitchen ATMEL_US_TXFLCLR); 1944b5199d46SCyrille Pitchen 19455f258b3eSCyrille Pitchen if (atmel_use_dma_tx(port)) 19465f258b3eSCyrille Pitchen txrdym = ATMEL_US_FOUR_DATA; 19475f258b3eSCyrille Pitchen 1948b5199d46SCyrille Pitchen fmr = ATMEL_US_TXRDYM(txrdym) | ATMEL_US_RXRDYM(rxrdym); 1949b5199d46SCyrille Pitchen if (atmel_port->rts_high && 1950b5199d46SCyrille Pitchen atmel_port->rts_low) 1951b5199d46SCyrille Pitchen fmr |= ATMEL_US_FRTSC | 1952b5199d46SCyrille Pitchen ATMEL_US_RXFTHRES(atmel_port->rts_high) | 1953b5199d46SCyrille Pitchen ATMEL_US_RXFTHRES2(atmel_port->rts_low); 1954b5199d46SCyrille Pitchen 1955b5199d46SCyrille Pitchen atmel_uart_writel(port, ATMEL_US_FMR, fmr); 1956b5199d46SCyrille Pitchen } 1957b5199d46SCyrille Pitchen 1958ab4382d2SGreg Kroah-Hartman /* Save current CSR for comparison in atmel_tasklet_func() */ 1959d2d8d4c0SRichard Genoud atmel_port->irq_status_prev = atmel_uart_readl(port, ATMEL_US_CSR); 1960ab4382d2SGreg Kroah-Hartman 1961ab4382d2SGreg Kroah-Hartman /* 1962ab4382d2SGreg Kroah-Hartman * Finally, enable the serial port 1963ab4382d2SGreg Kroah-Hartman */ 19644e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA | ATMEL_US_RSTRX); 1965ab4382d2SGreg Kroah-Hartman /* enable xmit & rcvr */ 19664e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_TXEN | ATMEL_US_RXEN); 1967ea04f82aSRomain Izard atmel_port->tx_stopped = false; 1968ab4382d2SGreg Kroah-Hartman 1969026cb432SKees Cook timer_setup(&atmel_port->uart_timer, atmel_uart_timer_callback, 0); 19708bc661bfSMarek Roszko 19718bc661bfSMarek Roszko if (atmel_use_pdc_rx(port)) { 19728bc661bfSMarek Roszko /* set UART timeout */ 19734b769371SNicolas Ferre if (!atmel_port->has_hw_timer) { 19742e68c22fSElen Song mod_timer(&atmel_port->uart_timer, 19752e68c22fSElen Song jiffies + uart_poll_timeout(port)); 19762e68c22fSElen Song /* set USART timeout */ 19772e68c22fSElen Song } else { 19782958cceeSLudovic Desroches atmel_uart_writel(port, atmel_port->rtor, 19792958cceeSLudovic Desroches PDC_RX_TIMEOUT); 19804e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_STTTO); 1981ab4382d2SGreg Kroah-Hartman 19824e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_IER, 19834e7decdaSCyrille Pitchen ATMEL_US_ENDRX | ATMEL_US_TIMEOUT); 19842e68c22fSElen Song } 1985ab4382d2SGreg Kroah-Hartman /* enable PDC controller */ 19864e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_RXTEN); 198734df42f5SElen Song } else if (atmel_use_dma_rx(port)) { 19882e68c22fSElen Song /* set UART timeout */ 19894b769371SNicolas Ferre if (!atmel_port->has_hw_timer) { 19902e68c22fSElen Song mod_timer(&atmel_port->uart_timer, 19912e68c22fSElen Song jiffies + uart_poll_timeout(port)); 19922e68c22fSElen Song /* set USART timeout */ 19932e68c22fSElen Song } else { 19942958cceeSLudovic Desroches atmel_uart_writel(port, atmel_port->rtor, 19952958cceeSLudovic Desroches PDC_RX_TIMEOUT); 19964e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_STTTO); 199734df42f5SElen Song 19984e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_IER, 19994e7decdaSCyrille Pitchen ATMEL_US_TIMEOUT); 20002e68c22fSElen Song } 2001ab4382d2SGreg Kroah-Hartman } else { 2002ab4382d2SGreg Kroah-Hartman /* enable receive only */ 20034e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_IER, ATMEL_US_RXRDY); 2004ab4382d2SGreg Kroah-Hartman } 2005ab4382d2SGreg Kroah-Hartman 2006ab4382d2SGreg Kroah-Hartman return 0; 2007ab4382d2SGreg Kroah-Hartman } 2008ab4382d2SGreg Kroah-Hartman 2009ab4382d2SGreg Kroah-Hartman /* 2010479e9b94SPeter Hurley * Flush any TX data submitted for DMA. Called when the TX circular 2011479e9b94SPeter Hurley * buffer is reset. 2012479e9b94SPeter Hurley */ 2013479e9b94SPeter Hurley static void atmel_flush_buffer(struct uart_port *port) 2014479e9b94SPeter Hurley { 2015479e9b94SPeter Hurley struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 2016479e9b94SPeter Hurley 2017479e9b94SPeter Hurley if (atmel_use_pdc_tx(port)) { 20184e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_PDC_TCR, 0); 2019479e9b94SPeter Hurley atmel_port->pdc_tx.ofs = 0; 2020479e9b94SPeter Hurley } 202131ca2c63SRichard Genoud /* 202231ca2c63SRichard Genoud * in uart_flush_buffer(), the xmit circular buffer has just 202331ca2c63SRichard Genoud * been cleared, so we have to reset tx_len accordingly. 202431ca2c63SRichard Genoud */ 202531ca2c63SRichard Genoud atmel_port->tx_len = 0; 2026479e9b94SPeter Hurley } 2027479e9b94SPeter Hurley 2028479e9b94SPeter Hurley /* 2029ab4382d2SGreg Kroah-Hartman * Disable the port 2030ab4382d2SGreg Kroah-Hartman */ 2031ab4382d2SGreg Kroah-Hartman static void atmel_shutdown(struct uart_port *port) 2032ab4382d2SGreg Kroah-Hartman { 2033ab4382d2SGreg Kroah-Hartman struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 20340cc7c6c7SMarek Roszko 20350ae9fdefSRichard Genoud /* Disable modem control lines interrupts */ 20360ae9fdefSRichard Genoud atmel_disable_ms(port); 20370ae9fdefSRichard Genoud 203898f2082cSNicolas Ferre /* Disable interrupts at device level */ 203998f2082cSNicolas Ferre atmel_uart_writel(port, ATMEL_US_IDR, -1); 204098f2082cSNicolas Ferre 204198f2082cSNicolas Ferre /* Prevent spurious interrupts from scheduling the tasklet */ 204298f2082cSNicolas Ferre atomic_inc(&atmel_port->tasklet_shutdown); 204398f2082cSNicolas Ferre 2044ab4382d2SGreg Kroah-Hartman /* 20458bc661bfSMarek Roszko * Prevent any tasklets being scheduled during 20468bc661bfSMarek Roszko * cleanup 20478bc661bfSMarek Roszko */ 20488bc661bfSMarek Roszko del_timer_sync(&atmel_port->uart_timer); 20498bc661bfSMarek Roszko 205098f2082cSNicolas Ferre /* Make sure that no interrupt is on the fly */ 205198f2082cSNicolas Ferre synchronize_irq(port->irq); 205298f2082cSNicolas Ferre 20538bc661bfSMarek Roszko /* 20540cc7c6c7SMarek Roszko * Clear out any scheduled tasklets before 20550cc7c6c7SMarek Roszko * we destroy the buffers 20560cc7c6c7SMarek Roszko */ 205700e8e658SNicolas Ferre tasklet_kill(&atmel_port->tasklet_rx); 205800e8e658SNicolas Ferre tasklet_kill(&atmel_port->tasklet_tx); 20590cc7c6c7SMarek Roszko 20600cc7c6c7SMarek Roszko /* 20610cc7c6c7SMarek Roszko * Ensure everything is stopped and 206298f2082cSNicolas Ferre * disable port and break condition. 2063ab4382d2SGreg Kroah-Hartman */ 2064ab4382d2SGreg Kroah-Hartman atmel_stop_rx(port); 2065ab4382d2SGreg Kroah-Hartman atmel_stop_tx(port); 2066ab4382d2SGreg Kroah-Hartman 20674e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA); 20680cc7c6c7SMarek Roszko 2069ab4382d2SGreg Kroah-Hartman /* 2070ab4382d2SGreg Kroah-Hartman * Shut-down the DMA. 2071ab4382d2SGreg Kroah-Hartman */ 2072a930e528SElen Song if (atmel_port->release_rx) 2073a930e528SElen Song atmel_port->release_rx(port); 2074a930e528SElen Song if (atmel_port->release_tx) 2075a930e528SElen Song atmel_port->release_tx(port); 2076ab4382d2SGreg Kroah-Hartman 2077ab4382d2SGreg Kroah-Hartman /* 2078bb7e73c5SMark Deneen * Reset ring buffer pointers 2079bb7e73c5SMark Deneen */ 2080bb7e73c5SMark Deneen atmel_port->rx_ring.head = 0; 2081bb7e73c5SMark Deneen atmel_port->rx_ring.tail = 0; 2082bb7e73c5SMark Deneen 2083bb7e73c5SMark Deneen /* 2084ab5e4e41SRichard Genoud * Free the interrupts 2085ab4382d2SGreg Kroah-Hartman */ 2086ab4382d2SGreg Kroah-Hartman free_irq(port->irq, port); 2087ab5e4e41SRichard Genoud 2088479e9b94SPeter Hurley atmel_flush_buffer(port); 2089ab4382d2SGreg Kroah-Hartman } 2090ab4382d2SGreg Kroah-Hartman 2091ab4382d2SGreg Kroah-Hartman /* 2092ab4382d2SGreg Kroah-Hartman * Power / Clock management. 2093ab4382d2SGreg Kroah-Hartman */ 2094ab4382d2SGreg Kroah-Hartman static void atmel_serial_pm(struct uart_port *port, unsigned int state, 2095ab4382d2SGreg Kroah-Hartman unsigned int oldstate) 2096ab4382d2SGreg Kroah-Hartman { 2097ab4382d2SGreg Kroah-Hartman struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 2098ab4382d2SGreg Kroah-Hartman 2099ab4382d2SGreg Kroah-Hartman switch (state) { 2100aec079f8SClaudiu Beznea case UART_PM_STATE_ON: 2101ab4382d2SGreg Kroah-Hartman /* 2102ab4382d2SGreg Kroah-Hartman * Enable the peripheral clock for this serial port. 2103ab4382d2SGreg Kroah-Hartman * This is called on uart_open() or a resume event. 2104ab4382d2SGreg Kroah-Hartman */ 210591f8c2d8SBoris BREZILLON clk_prepare_enable(atmel_port->clk); 2106ab4382d2SGreg Kroah-Hartman 2107ab4382d2SGreg Kroah-Hartman /* re-enable interrupts if we disabled some on suspend */ 21084e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_IER, atmel_port->backup_imr); 2109ab4382d2SGreg Kroah-Hartman break; 2110aec079f8SClaudiu Beznea case UART_PM_STATE_OFF: 2111ab4382d2SGreg Kroah-Hartman /* Back up the interrupt mask and disable all interrupts */ 21124e7decdaSCyrille Pitchen atmel_port->backup_imr = atmel_uart_readl(port, ATMEL_US_IMR); 21134e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_IDR, -1); 2114ab4382d2SGreg Kroah-Hartman 2115ab4382d2SGreg Kroah-Hartman /* 2116ab4382d2SGreg Kroah-Hartman * Disable the peripheral clock for this serial port. 2117ab4382d2SGreg Kroah-Hartman * This is called on uart_close() or a suspend event. 2118ab4382d2SGreg Kroah-Hartman */ 211991f8c2d8SBoris BREZILLON clk_disable_unprepare(atmel_port->clk); 2120ab4382d2SGreg Kroah-Hartman break; 2121ab4382d2SGreg Kroah-Hartman default: 2122ddaa6037SRichard Genoud dev_err(port->dev, "atmel_serial: unknown pm %d\n", state); 2123ab4382d2SGreg Kroah-Hartman } 2124ab4382d2SGreg Kroah-Hartman } 2125ab4382d2SGreg Kroah-Hartman 2126ab4382d2SGreg Kroah-Hartman /* 2127ab4382d2SGreg Kroah-Hartman * Change the port parameters 2128ab4382d2SGreg Kroah-Hartman */ 2129bec5b814SIlpo Järvinen static void atmel_set_termios(struct uart_port *port, 2130bec5b814SIlpo Järvinen struct ktermios *termios, 2131bec5b814SIlpo Järvinen const struct ktermios *old) 2132ab4382d2SGreg Kroah-Hartman { 21335bf5635aSLudovic Desroches struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 2134ab4382d2SGreg Kroah-Hartman unsigned long flags; 21355bf5635aSLudovic Desroches unsigned int old_mode, mode, imr, quot, baud, div, cd, fp = 0; 2136ab4382d2SGreg Kroah-Hartman 21371cf6e8fcSCyrille Pitchen /* save the current mode register */ 21384e7decdaSCyrille Pitchen mode = old_mode = atmel_uart_readl(port, ATMEL_US_MR); 21391cf6e8fcSCyrille Pitchen 21401cf6e8fcSCyrille Pitchen /* reset the mode, clock divisor, parity, stop bits and data size */ 21411a5a01a1SSergiu Moga if (atmel_port->is_usart) 21421a5a01a1SSergiu Moga mode &= ~(ATMEL_US_NBSTOP | ATMEL_US_PAR | ATMEL_US_CHRL | 21431a5a01a1SSergiu Moga ATMEL_US_USCLKS | ATMEL_US_USMODE); 21441a5a01a1SSergiu Moga else 21451a5a01a1SSergiu Moga mode &= ~(ATMEL_UA_BRSRCCK | ATMEL_US_PAR | ATMEL_UA_FILTER); 2146ab4382d2SGreg Kroah-Hartman 2147ab4382d2SGreg Kroah-Hartman baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk / 16); 2148ab4382d2SGreg Kroah-Hartman 2149ab4382d2SGreg Kroah-Hartman /* byte size */ 2150ab4382d2SGreg Kroah-Hartman switch (termios->c_cflag & CSIZE) { 2151ab4382d2SGreg Kroah-Hartman case CS5: 2152ab4382d2SGreg Kroah-Hartman mode |= ATMEL_US_CHRL_5; 2153ab4382d2SGreg Kroah-Hartman break; 2154ab4382d2SGreg Kroah-Hartman case CS6: 2155ab4382d2SGreg Kroah-Hartman mode |= ATMEL_US_CHRL_6; 2156ab4382d2SGreg Kroah-Hartman break; 2157ab4382d2SGreg Kroah-Hartman case CS7: 2158ab4382d2SGreg Kroah-Hartman mode |= ATMEL_US_CHRL_7; 2159ab4382d2SGreg Kroah-Hartman break; 2160ab4382d2SGreg Kroah-Hartman default: 2161ab4382d2SGreg Kroah-Hartman mode |= ATMEL_US_CHRL_8; 2162ab4382d2SGreg Kroah-Hartman break; 2163ab4382d2SGreg Kroah-Hartman } 2164ab4382d2SGreg Kroah-Hartman 2165ab4382d2SGreg Kroah-Hartman /* stop bits */ 2166ab4382d2SGreg Kroah-Hartman if (termios->c_cflag & CSTOPB) 2167ab4382d2SGreg Kroah-Hartman mode |= ATMEL_US_NBSTOP_2; 2168ab4382d2SGreg Kroah-Hartman 2169ab4382d2SGreg Kroah-Hartman /* parity */ 2170ab4382d2SGreg Kroah-Hartman if (termios->c_cflag & PARENB) { 2171ab4382d2SGreg Kroah-Hartman /* Mark or Space parity */ 2172ab4382d2SGreg Kroah-Hartman if (termios->c_cflag & CMSPAR) { 2173ab4382d2SGreg Kroah-Hartman if (termios->c_cflag & PARODD) 2174ab4382d2SGreg Kroah-Hartman mode |= ATMEL_US_PAR_MARK; 2175ab4382d2SGreg Kroah-Hartman else 2176ab4382d2SGreg Kroah-Hartman mode |= ATMEL_US_PAR_SPACE; 2177ab4382d2SGreg Kroah-Hartman } else if (termios->c_cflag & PARODD) 2178ab4382d2SGreg Kroah-Hartman mode |= ATMEL_US_PAR_ODD; 2179ab4382d2SGreg Kroah-Hartman else 2180ab4382d2SGreg Kroah-Hartman mode |= ATMEL_US_PAR_EVEN; 2181ab4382d2SGreg Kroah-Hartman } else 2182ab4382d2SGreg Kroah-Hartman mode |= ATMEL_US_PAR_NONE; 2183ab4382d2SGreg Kroah-Hartman 2184ab4382d2SGreg Kroah-Hartman spin_lock_irqsave(&port->lock, flags); 2185ab4382d2SGreg Kroah-Hartman 2186ab4382d2SGreg Kroah-Hartman port->read_status_mask = ATMEL_US_OVRE; 2187ab4382d2SGreg Kroah-Hartman if (termios->c_iflag & INPCK) 2188ab4382d2SGreg Kroah-Hartman port->read_status_mask |= (ATMEL_US_FRAME | ATMEL_US_PARE); 2189ef8b9ddcSPeter Hurley if (termios->c_iflag & (IGNBRK | BRKINT | PARMRK)) 2190ab4382d2SGreg Kroah-Hartman port->read_status_mask |= ATMEL_US_RXBRK; 2191ab4382d2SGreg Kroah-Hartman 219264e22ebeSElen Song if (atmel_use_pdc_rx(port)) 2193ab4382d2SGreg Kroah-Hartman /* need to enable error interrupts */ 21944e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_IER, port->read_status_mask); 2195ab4382d2SGreg Kroah-Hartman 2196ab4382d2SGreg Kroah-Hartman /* 2197ab4382d2SGreg Kroah-Hartman * Characters to ignore 2198ab4382d2SGreg Kroah-Hartman */ 2199ab4382d2SGreg Kroah-Hartman port->ignore_status_mask = 0; 2200ab4382d2SGreg Kroah-Hartman if (termios->c_iflag & IGNPAR) 2201ab4382d2SGreg Kroah-Hartman port->ignore_status_mask |= (ATMEL_US_FRAME | ATMEL_US_PARE); 2202ab4382d2SGreg Kroah-Hartman if (termios->c_iflag & IGNBRK) { 2203ab4382d2SGreg Kroah-Hartman port->ignore_status_mask |= ATMEL_US_RXBRK; 2204ab4382d2SGreg Kroah-Hartman /* 2205ab4382d2SGreg Kroah-Hartman * If we're ignoring parity and break indicators, 2206ab4382d2SGreg Kroah-Hartman * ignore overruns too (for real raw support). 2207ab4382d2SGreg Kroah-Hartman */ 2208ab4382d2SGreg Kroah-Hartman if (termios->c_iflag & IGNPAR) 2209ab4382d2SGreg Kroah-Hartman port->ignore_status_mask |= ATMEL_US_OVRE; 2210ab4382d2SGreg Kroah-Hartman } 2211ab4382d2SGreg Kroah-Hartman /* TODO: Ignore all characters if CREAD is set.*/ 2212ab4382d2SGreg Kroah-Hartman 2213ab4382d2SGreg Kroah-Hartman /* update the per-port timeout */ 2214ab4382d2SGreg Kroah-Hartman uart_update_timeout(port, termios->c_cflag, baud); 2215ab4382d2SGreg Kroah-Hartman 2216ab4382d2SGreg Kroah-Hartman /* 2217ab4382d2SGreg Kroah-Hartman * save/disable interrupts. The tty layer will ensure that the 2218ab4382d2SGreg Kroah-Hartman * transmitter is empty if requested by the caller, so there's 2219ab4382d2SGreg Kroah-Hartman * no need to wait for it here. 2220ab4382d2SGreg Kroah-Hartman */ 22214e7decdaSCyrille Pitchen imr = atmel_uart_readl(port, ATMEL_US_IMR); 22224e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_IDR, -1); 2223ab4382d2SGreg Kroah-Hartman 2224ab4382d2SGreg Kroah-Hartman /* disable receiver and transmitter */ 22254e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_TXDIS | ATMEL_US_RXDIS); 2226ea04f82aSRomain Izard atmel_port->tx_stopped = true; 2227ab4382d2SGreg Kroah-Hartman 22281cf6e8fcSCyrille Pitchen /* mode */ 222913bd3e6fSRicardo Ribalda Delgado if (port->rs485.flags & SER_RS485_ENABLED) { 22304e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_TTGR, 22314e7decdaSCyrille Pitchen port->rs485.delay_rts_after_send); 2232ab4382d2SGreg Kroah-Hartman mode |= ATMEL_US_USMODE_RS485; 2233377fedd1SNicolas Ferre } else if (port->iso7816.flags & SER_ISO7816_ENABLED) { 2234377fedd1SNicolas Ferre atmel_uart_writel(port, ATMEL_US_TTGR, port->iso7816.tg); 2235377fedd1SNicolas Ferre /* select mck clock, and output */ 2236377fedd1SNicolas Ferre mode |= ATMEL_US_USCLKS_MCK | ATMEL_US_CLKO; 2237377fedd1SNicolas Ferre /* set max iterations */ 2238377fedd1SNicolas Ferre mode |= ATMEL_US_MAX_ITER(3); 2239377fedd1SNicolas Ferre if ((port->iso7816.flags & SER_ISO7816_T_PARAM) 2240377fedd1SNicolas Ferre == SER_ISO7816_T(0)) 2241377fedd1SNicolas Ferre mode |= ATMEL_US_USMODE_ISO7816_T0; 2242377fedd1SNicolas Ferre else 2243377fedd1SNicolas Ferre mode |= ATMEL_US_USMODE_ISO7816_T1; 22441cf6e8fcSCyrille Pitchen } else if (termios->c_cflag & CRTSCTS) { 22451cf6e8fcSCyrille Pitchen /* RS232 with hardware handshake (RTS/CTS) */ 22469bcffe75SRichard Genoud if (atmel_use_fifo(port) && 22479bcffe75SRichard Genoud !mctrl_gpio_to_gpiod(atmel_port->gpios, UART_GPIO_CTS)) { 22489bcffe75SRichard Genoud /* 22499bcffe75SRichard Genoud * with ATMEL_US_USMODE_HWHS set, the controller will 22509bcffe75SRichard Genoud * be able to drive the RTS pin high/low when the RX 22519bcffe75SRichard Genoud * FIFO is above RXFTHRES/below RXFTHRES2. 22529bcffe75SRichard Genoud * It will also disable the transmitter when the CTS 22539bcffe75SRichard Genoud * pin is high. 22549bcffe75SRichard Genoud * This mode is not activated if CTS pin is a GPIO 22559bcffe75SRichard Genoud * because in this case, the transmitter is always 22569bcffe75SRichard Genoud * disabled (there must be an internal pull-up 22579bcffe75SRichard Genoud * responsible for this behaviour). 22589bcffe75SRichard Genoud * If the RTS pin is a GPIO, the controller won't be 22599bcffe75SRichard Genoud * able to drive it according to the FIFO thresholds, 22609bcffe75SRichard Genoud * but it will be handled by the driver. 22619bcffe75SRichard Genoud */ 22621cf6e8fcSCyrille Pitchen mode |= ATMEL_US_USMODE_HWHS; 22639bcffe75SRichard Genoud } else { 22649bcffe75SRichard Genoud /* 22659bcffe75SRichard Genoud * For platforms without FIFO, the flow control is 22669bcffe75SRichard Genoud * handled by the driver. 22679bcffe75SRichard Genoud */ 22689bcffe75SRichard Genoud mode |= ATMEL_US_USMODE_NORMAL; 22695be605acSAlexandre Belloni } 22701cf6e8fcSCyrille Pitchen } else { 22711cf6e8fcSCyrille Pitchen /* RS232 without hadware handshake */ 22721cf6e8fcSCyrille Pitchen mode |= ATMEL_US_USMODE_NORMAL; 2273ab4382d2SGreg Kroah-Hartman } 2274ab4382d2SGreg Kroah-Hartman 22755bf5635aSLudovic Desroches /* 22765bf5635aSLudovic Desroches * Set the baud rate: 22775bf5635aSLudovic Desroches * Fractional baudrate allows to setup output frequency more 22785bf5635aSLudovic Desroches * accurately. This feature is enabled only when using normal mode. 22795bf5635aSLudovic Desroches * baudrate = selected clock / (8 * (2 - OVER) * (CD + FP / 8)) 22805bf5635aSLudovic Desroches * Currently, OVER is always set to 0 so we get 228136131cdfSAlexey Starikovskiy * baudrate = selected clock / (16 * (CD + FP / 8)) 228236131cdfSAlexey Starikovskiy * then 228336131cdfSAlexey Starikovskiy * 8 CD + FP = selected clock / (2 * baudrate) 22845bf5635aSLudovic Desroches */ 22852867af2dSRomain Izard if (atmel_port->has_frac_baudrate) { 228636131cdfSAlexey Starikovskiy div = DIV_ROUND_CLOSEST(port->uartclk, baud * 2); 228736131cdfSAlexey Starikovskiy cd = div >> 3; 228836131cdfSAlexey Starikovskiy fp = div & ATMEL_US_FP_MASK; 22895bf5635aSLudovic Desroches } else { 22905bf5635aSLudovic Desroches cd = uart_get_divisor(port, baud); 22915bf5635aSLudovic Desroches } 22925bf5635aSLudovic Desroches 2293*5644bf18SSergiu Moga /* 2294*5644bf18SSergiu Moga * If the current value of the Clock Divisor surpasses the 16 bit 2295*5644bf18SSergiu Moga * ATMEL_US_CD mask and the IP is USART, switch to the Peripheral 2296*5644bf18SSergiu Moga * Clock implicitly divided by 8. 2297*5644bf18SSergiu Moga * If the IP is UART however, keep the highest possible value for 2298*5644bf18SSergiu Moga * the CD and avoid needless division of CD, since UART IP's do not 2299*5644bf18SSergiu Moga * support implicit division of the Peripheral Clock. 2300*5644bf18SSergiu Moga */ 2301*5644bf18SSergiu Moga if (atmel_port->is_usart && cd > ATMEL_US_CD) { 23025bf5635aSLudovic Desroches cd /= 8; 23035bf5635aSLudovic Desroches mode |= ATMEL_US_USCLKS_MCK_DIV8; 2304*5644bf18SSergiu Moga } else { 2305*5644bf18SSergiu Moga cd = min_t(unsigned int, cd, ATMEL_US_CD); 23065bf5635aSLudovic Desroches } 2307*5644bf18SSergiu Moga 23085bf5635aSLudovic Desroches quot = cd | fp << ATMEL_US_FP_OFFSET; 23095bf5635aSLudovic Desroches 2310377fedd1SNicolas Ferre if (!(port->iso7816.flags & SER_ISO7816_ENABLED)) 23114e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_BRGR, quot); 2312cb47b9f8SDavid Engraf 2313cb47b9f8SDavid Engraf /* set the mode, clock divisor, parity, stop bits and data size */ 2314cb47b9f8SDavid Engraf atmel_uart_writel(port, ATMEL_US_MR, mode); 2315cb47b9f8SDavid Engraf 2316cb47b9f8SDavid Engraf /* 2317cb47b9f8SDavid Engraf * when switching the mode, set the RTS line state according to the 2318cb47b9f8SDavid Engraf * new mode, otherwise keep the former state 2319cb47b9f8SDavid Engraf */ 2320cb47b9f8SDavid Engraf if ((old_mode & ATMEL_US_USMODE) != (mode & ATMEL_US_USMODE)) { 2321cb47b9f8SDavid Engraf unsigned int rts_state; 2322cb47b9f8SDavid Engraf 2323cb47b9f8SDavid Engraf if ((mode & ATMEL_US_USMODE) == ATMEL_US_USMODE_HWHS) { 2324cb47b9f8SDavid Engraf /* let the hardware control the RTS line */ 2325cb47b9f8SDavid Engraf rts_state = ATMEL_US_RTSDIS; 2326cb47b9f8SDavid Engraf } else { 2327cb47b9f8SDavid Engraf /* force RTS line to low level */ 2328cb47b9f8SDavid Engraf rts_state = ATMEL_US_RTSEN; 2329cb47b9f8SDavid Engraf } 2330cb47b9f8SDavid Engraf 2331cb47b9f8SDavid Engraf atmel_uart_writel(port, ATMEL_US_CR, rts_state); 2332cb47b9f8SDavid Engraf } 2333cb47b9f8SDavid Engraf 23344e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA | ATMEL_US_RSTRX); 23354e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_TXEN | ATMEL_US_RXEN); 2336ea04f82aSRomain Izard atmel_port->tx_stopped = false; 2337ab4382d2SGreg Kroah-Hartman 2338ab4382d2SGreg Kroah-Hartman /* restore interrupts */ 23394e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_IER, imr); 2340ab4382d2SGreg Kroah-Hartman 2341ab4382d2SGreg Kroah-Hartman /* CTS flow-control and modem-status interrupts */ 2342ab4382d2SGreg Kroah-Hartman if (UART_ENABLE_MS(port, termios->c_cflag)) 234335b675b9SRichard Genoud atmel_enable_ms(port); 234435b675b9SRichard Genoud else 234535b675b9SRichard Genoud atmel_disable_ms(port); 2346ab4382d2SGreg Kroah-Hartman 2347ab4382d2SGreg Kroah-Hartman spin_unlock_irqrestore(&port->lock, flags); 2348ab4382d2SGreg Kroah-Hartman } 2349ab4382d2SGreg Kroah-Hartman 2350732a84a0SPeter Hurley static void atmel_set_ldisc(struct uart_port *port, struct ktermios *termios) 235142bd7a4fSViktar Palstsiuk { 2352732a84a0SPeter Hurley if (termios->c_line == N_PPS) { 235342bd7a4fSViktar Palstsiuk port->flags |= UPF_HARDPPS_CD; 2354d41510ceSPeter Hurley spin_lock_irq(&port->lock); 235542bd7a4fSViktar Palstsiuk atmel_enable_ms(port); 2356d41510ceSPeter Hurley spin_unlock_irq(&port->lock); 235742bd7a4fSViktar Palstsiuk } else { 235842bd7a4fSViktar Palstsiuk port->flags &= ~UPF_HARDPPS_CD; 2359cab68f89SPeter Hurley if (!UART_ENABLE_MS(port, termios->c_cflag)) { 2360cab68f89SPeter Hurley spin_lock_irq(&port->lock); 2361cab68f89SPeter Hurley atmel_disable_ms(port); 2362cab68f89SPeter Hurley spin_unlock_irq(&port->lock); 2363cab68f89SPeter Hurley } 236442bd7a4fSViktar Palstsiuk } 236542bd7a4fSViktar Palstsiuk } 236642bd7a4fSViktar Palstsiuk 2367ab4382d2SGreg Kroah-Hartman /* 2368ab4382d2SGreg Kroah-Hartman * Return string describing the specified port 2369ab4382d2SGreg Kroah-Hartman */ 2370ab4382d2SGreg Kroah-Hartman static const char *atmel_type(struct uart_port *port) 2371ab4382d2SGreg Kroah-Hartman { 2372ab4382d2SGreg Kroah-Hartman return (port->type == PORT_ATMEL) ? "ATMEL_SERIAL" : NULL; 2373ab4382d2SGreg Kroah-Hartman } 2374ab4382d2SGreg Kroah-Hartman 2375ab4382d2SGreg Kroah-Hartman /* 2376ab4382d2SGreg Kroah-Hartman * Release the memory region(s) being used by 'port'. 2377ab4382d2SGreg Kroah-Hartman */ 2378ab4382d2SGreg Kroah-Hartman static void atmel_release_port(struct uart_port *port) 2379ab4382d2SGreg Kroah-Hartman { 2380c24d2531SRadu Pirea struct platform_device *mpdev = to_platform_device(port->dev->parent); 2381c24d2531SRadu Pirea int size = resource_size(mpdev->resource); 2382ab4382d2SGreg Kroah-Hartman 2383ab4382d2SGreg Kroah-Hartman release_mem_region(port->mapbase, size); 2384ab4382d2SGreg Kroah-Hartman 2385ab4382d2SGreg Kroah-Hartman if (port->flags & UPF_IOREMAP) { 2386ab4382d2SGreg Kroah-Hartman iounmap(port->membase); 2387ab4382d2SGreg Kroah-Hartman port->membase = NULL; 2388ab4382d2SGreg Kroah-Hartman } 2389ab4382d2SGreg Kroah-Hartman } 2390ab4382d2SGreg Kroah-Hartman 2391ab4382d2SGreg Kroah-Hartman /* 2392ab4382d2SGreg Kroah-Hartman * Request the memory region(s) being used by 'port'. 2393ab4382d2SGreg Kroah-Hartman */ 2394ab4382d2SGreg Kroah-Hartman static int atmel_request_port(struct uart_port *port) 2395ab4382d2SGreg Kroah-Hartman { 2396c24d2531SRadu Pirea struct platform_device *mpdev = to_platform_device(port->dev->parent); 2397c24d2531SRadu Pirea int size = resource_size(mpdev->resource); 2398ab4382d2SGreg Kroah-Hartman 2399ab4382d2SGreg Kroah-Hartman if (!request_mem_region(port->mapbase, size, "atmel_serial")) 2400ab4382d2SGreg Kroah-Hartman return -EBUSY; 2401ab4382d2SGreg Kroah-Hartman 2402ab4382d2SGreg Kroah-Hartman if (port->flags & UPF_IOREMAP) { 2403ab4382d2SGreg Kroah-Hartman port->membase = ioremap(port->mapbase, size); 2404ab4382d2SGreg Kroah-Hartman if (port->membase == NULL) { 2405ab4382d2SGreg Kroah-Hartman release_mem_region(port->mapbase, size); 2406ab4382d2SGreg Kroah-Hartman return -ENOMEM; 2407ab4382d2SGreg Kroah-Hartman } 2408ab4382d2SGreg Kroah-Hartman } 2409ab4382d2SGreg Kroah-Hartman 2410ab4382d2SGreg Kroah-Hartman return 0; 2411ab4382d2SGreg Kroah-Hartman } 2412ab4382d2SGreg Kroah-Hartman 2413ab4382d2SGreg Kroah-Hartman /* 2414ab4382d2SGreg Kroah-Hartman * Configure/autoconfigure the port. 2415ab4382d2SGreg Kroah-Hartman */ 2416ab4382d2SGreg Kroah-Hartman static void atmel_config_port(struct uart_port *port, int flags) 2417ab4382d2SGreg Kroah-Hartman { 2418ab4382d2SGreg Kroah-Hartman if (flags & UART_CONFIG_TYPE) { 2419ab4382d2SGreg Kroah-Hartman port->type = PORT_ATMEL; 2420ab4382d2SGreg Kroah-Hartman atmel_request_port(port); 2421ab4382d2SGreg Kroah-Hartman } 2422ab4382d2SGreg Kroah-Hartman } 2423ab4382d2SGreg Kroah-Hartman 2424ab4382d2SGreg Kroah-Hartman /* 2425ab4382d2SGreg Kroah-Hartman * Verify the new serial_struct (for TIOCSSERIAL). 2426ab4382d2SGreg Kroah-Hartman */ 2427ab4382d2SGreg Kroah-Hartman static int atmel_verify_port(struct uart_port *port, struct serial_struct *ser) 2428ab4382d2SGreg Kroah-Hartman { 2429ab4382d2SGreg Kroah-Hartman int ret = 0; 2430ab4382d2SGreg Kroah-Hartman if (ser->type != PORT_UNKNOWN && ser->type != PORT_ATMEL) 2431ab4382d2SGreg Kroah-Hartman ret = -EINVAL; 2432ab4382d2SGreg Kroah-Hartman if (port->irq != ser->irq) 2433ab4382d2SGreg Kroah-Hartman ret = -EINVAL; 2434ab4382d2SGreg Kroah-Hartman if (ser->io_type != SERIAL_IO_MEM) 2435ab4382d2SGreg Kroah-Hartman ret = -EINVAL; 2436ab4382d2SGreg Kroah-Hartman if (port->uartclk / 16 != ser->baud_base) 2437ab4382d2SGreg Kroah-Hartman ret = -EINVAL; 2438270c2adeSAndre Przywara if (port->mapbase != (unsigned long)ser->iomem_base) 2439ab4382d2SGreg Kroah-Hartman ret = -EINVAL; 2440ab4382d2SGreg Kroah-Hartman if (port->iobase != ser->port) 2441ab4382d2SGreg Kroah-Hartman ret = -EINVAL; 2442ab4382d2SGreg Kroah-Hartman if (ser->hub6 != 0) 2443ab4382d2SGreg Kroah-Hartman ret = -EINVAL; 2444ab4382d2SGreg Kroah-Hartman return ret; 2445ab4382d2SGreg Kroah-Hartman } 2446ab4382d2SGreg Kroah-Hartman 2447ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_CONSOLE_POLL 2448ab4382d2SGreg Kroah-Hartman static int atmel_poll_get_char(struct uart_port *port) 2449ab4382d2SGreg Kroah-Hartman { 24504e7decdaSCyrille Pitchen while (!(atmel_uart_readl(port, ATMEL_US_CSR) & ATMEL_US_RXRDY)) 2451ab4382d2SGreg Kroah-Hartman cpu_relax(); 2452ab4382d2SGreg Kroah-Hartman 2453a6499435SCyrille Pitchen return atmel_uart_read_char(port); 2454ab4382d2SGreg Kroah-Hartman } 2455ab4382d2SGreg Kroah-Hartman 2456ab4382d2SGreg Kroah-Hartman static void atmel_poll_put_char(struct uart_port *port, unsigned char ch) 2457ab4382d2SGreg Kroah-Hartman { 24584e7decdaSCyrille Pitchen while (!(atmel_uart_readl(port, ATMEL_US_CSR) & ATMEL_US_TXRDY)) 2459ab4382d2SGreg Kroah-Hartman cpu_relax(); 2460ab4382d2SGreg Kroah-Hartman 2461a6499435SCyrille Pitchen atmel_uart_write_char(port, ch); 2462ab4382d2SGreg Kroah-Hartman } 2463ab4382d2SGreg Kroah-Hartman #endif 2464ab4382d2SGreg Kroah-Hartman 24655c7dcdb6SJulia Lawall static const struct uart_ops atmel_pops = { 2466ab4382d2SGreg Kroah-Hartman .tx_empty = atmel_tx_empty, 2467ab4382d2SGreg Kroah-Hartman .set_mctrl = atmel_set_mctrl, 2468ab4382d2SGreg Kroah-Hartman .get_mctrl = atmel_get_mctrl, 2469ab4382d2SGreg Kroah-Hartman .stop_tx = atmel_stop_tx, 2470ab4382d2SGreg Kroah-Hartman .start_tx = atmel_start_tx, 2471ab4382d2SGreg Kroah-Hartman .stop_rx = atmel_stop_rx, 2472ab4382d2SGreg Kroah-Hartman .enable_ms = atmel_enable_ms, 2473ab4382d2SGreg Kroah-Hartman .break_ctl = atmel_break_ctl, 2474ab4382d2SGreg Kroah-Hartman .startup = atmel_startup, 2475ab4382d2SGreg Kroah-Hartman .shutdown = atmel_shutdown, 2476ab4382d2SGreg Kroah-Hartman .flush_buffer = atmel_flush_buffer, 2477ab4382d2SGreg Kroah-Hartman .set_termios = atmel_set_termios, 247842bd7a4fSViktar Palstsiuk .set_ldisc = atmel_set_ldisc, 2479ab4382d2SGreg Kroah-Hartman .type = atmel_type, 2480ab4382d2SGreg Kroah-Hartman .release_port = atmel_release_port, 2481ab4382d2SGreg Kroah-Hartman .request_port = atmel_request_port, 2482ab4382d2SGreg Kroah-Hartman .config_port = atmel_config_port, 2483ab4382d2SGreg Kroah-Hartman .verify_port = atmel_verify_port, 2484ab4382d2SGreg Kroah-Hartman .pm = atmel_serial_pm, 2485ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_CONSOLE_POLL 2486ab4382d2SGreg Kroah-Hartman .poll_get_char = atmel_poll_get_char, 2487ab4382d2SGreg Kroah-Hartman .poll_put_char = atmel_poll_put_char, 2488ab4382d2SGreg Kroah-Hartman #endif 2489ab4382d2SGreg Kroah-Hartman }; 2490ab4382d2SGreg Kroah-Hartman 2491af47c491SIlpo Järvinen static const struct serial_rs485 atmel_rs485_supported = { 2492af47c491SIlpo Järvinen .flags = SER_RS485_ENABLED | SER_RS485_RTS_AFTER_SEND | SER_RS485_RX_DURING_TX, 2493af47c491SIlpo Järvinen .delay_rts_before_send = 1, 2494af47c491SIlpo Järvinen .delay_rts_after_send = 1, 2495af47c491SIlpo Järvinen }; 2496af47c491SIlpo Järvinen 2497ab4382d2SGreg Kroah-Hartman /* 2498ab4382d2SGreg Kroah-Hartman * Configure the port from the platform device resource info. 2499ab4382d2SGreg Kroah-Hartman */ 250091f8c2d8SBoris BREZILLON static int atmel_init_port(struct atmel_uart_port *atmel_port, 2501ab4382d2SGreg Kroah-Hartman struct platform_device *pdev) 2502ab4382d2SGreg Kroah-Hartman { 250391f8c2d8SBoris BREZILLON int ret; 2504ab4382d2SGreg Kroah-Hartman struct uart_port *port = &atmel_port->uart; 2505c24d2531SRadu Pirea struct platform_device *mpdev = to_platform_device(pdev->dev.parent); 2506ab4382d2SGreg Kroah-Hartman 25074a1e8888SLeilei Zhao atmel_init_property(atmel_port, pdev); 2508a930e528SElen Song atmel_set_ops(port); 2509a930e528SElen Song 2510ab4382d2SGreg Kroah-Hartman port->iotype = UPIO_MEM; 251192c8f7c0SAlexandre Belloni port->flags = UPF_BOOT_AUTOCONF | UPF_IOREMAP; 2512ab4382d2SGreg Kroah-Hartman port->ops = &atmel_pops; 2513ab4382d2SGreg Kroah-Hartman port->fifosize = 1; 2514ab4382d2SGreg Kroah-Hartman port->dev = &pdev->dev; 2515c24d2531SRadu Pirea port->mapbase = mpdev->resource[0].start; 25165bb221b0SRob Herring port->irq = platform_get_irq(mpdev, 0); 251713bd3e6fSRicardo Ribalda Delgado port->rs485_config = atmel_config_rs485; 25180139da50SIlpo Järvinen port->rs485_supported = atmel_rs485_supported; 2519377fedd1SNicolas Ferre port->iso7816_config = atmel_config_iso7816; 252092c8f7c0SAlexandre Belloni port->membase = NULL; 2521ab4382d2SGreg Kroah-Hartman 2522ab4382d2SGreg Kroah-Hartman memset(&atmel_port->rx_ring, 0, sizeof(atmel_port->rx_ring)); 2523ab4382d2SGreg Kroah-Hartman 2524c150c0f3SLukas Wunner ret = uart_get_rs485_mode(port); 2525c150c0f3SLukas Wunner if (ret) 2526c150c0f3SLukas Wunner return ret; 2527c150c0f3SLukas Wunner 2528ab4382d2SGreg Kroah-Hartman port->uartclk = clk_get_rate(atmel_port->clk); 2529ab4382d2SGreg Kroah-Hartman 2530377fedd1SNicolas Ferre /* 2531377fedd1SNicolas Ferre * Use TXEMPTY for interrupt when rs485 or ISO7816 else TXRDY or 2532377fedd1SNicolas Ferre * ENDTX|TXBUFE 2533377fedd1SNicolas Ferre */ 2534477b8383SCodrin.Ciubotariu@microchip.com if (atmel_uart_is_half_duplex(port)) 2535ab4382d2SGreg Kroah-Hartman atmel_port->tx_done_mask = ATMEL_US_TXEMPTY; 253664e22ebeSElen Song else if (atmel_use_pdc_tx(port)) { 2537ab4382d2SGreg Kroah-Hartman port->fifosize = PDC_BUFFER_SIZE; 2538ab4382d2SGreg Kroah-Hartman atmel_port->tx_done_mask = ATMEL_US_ENDTX | ATMEL_US_TXBUFE; 2539ab4382d2SGreg Kroah-Hartman } else { 2540ab4382d2SGreg Kroah-Hartman atmel_port->tx_done_mask = ATMEL_US_TXRDY; 2541ab4382d2SGreg Kroah-Hartman } 254291f8c2d8SBoris BREZILLON 254391f8c2d8SBoris BREZILLON return 0; 2544ab4382d2SGreg Kroah-Hartman } 2545ab4382d2SGreg Kroah-Hartman 2546ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_SERIAL_ATMEL_CONSOLE 25473f8bab17SJiri Slaby static void atmel_console_putchar(struct uart_port *port, unsigned char ch) 2548ab4382d2SGreg Kroah-Hartman { 25494e7decdaSCyrille Pitchen while (!(atmel_uart_readl(port, ATMEL_US_CSR) & ATMEL_US_TXRDY)) 2550ab4382d2SGreg Kroah-Hartman cpu_relax(); 2551a6499435SCyrille Pitchen atmel_uart_write_char(port, ch); 2552ab4382d2SGreg Kroah-Hartman } 2553ab4382d2SGreg Kroah-Hartman 2554ab4382d2SGreg Kroah-Hartman /* 2555ab4382d2SGreg Kroah-Hartman * Interrupts are disabled on entering 2556ab4382d2SGreg Kroah-Hartman */ 2557ab4382d2SGreg Kroah-Hartman static void atmel_console_write(struct console *co, const char *s, u_int count) 2558ab4382d2SGreg Kroah-Hartman { 2559ab4382d2SGreg Kroah-Hartman struct uart_port *port = &atmel_ports[co->index].uart; 2560ab4382d2SGreg Kroah-Hartman struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 2561ab4382d2SGreg Kroah-Hartman unsigned int status, imr; 2562ab4382d2SGreg Kroah-Hartman unsigned int pdc_tx; 2563ab4382d2SGreg Kroah-Hartman 2564ab4382d2SGreg Kroah-Hartman /* 2565ab4382d2SGreg Kroah-Hartman * First, save IMR and then disable interrupts 2566ab4382d2SGreg Kroah-Hartman */ 25674e7decdaSCyrille Pitchen imr = atmel_uart_readl(port, ATMEL_US_IMR); 25684e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_IDR, 25694e7decdaSCyrille Pitchen ATMEL_US_RXRDY | atmel_port->tx_done_mask); 2570ab4382d2SGreg Kroah-Hartman 2571ab4382d2SGreg Kroah-Hartman /* Store PDC transmit status and disable it */ 25724e7decdaSCyrille Pitchen pdc_tx = atmel_uart_readl(port, ATMEL_PDC_PTSR) & ATMEL_PDC_TXTEN; 25734e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_TXTDIS); 2574ab4382d2SGreg Kroah-Hartman 2575497e1e16SNicolas Ferre /* Make sure that tx path is actually able to send characters */ 2576497e1e16SNicolas Ferre atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_TXEN); 2577ea04f82aSRomain Izard atmel_port->tx_stopped = false; 2578497e1e16SNicolas Ferre 2579ab4382d2SGreg Kroah-Hartman uart_console_write(port, s, count, atmel_console_putchar); 2580ab4382d2SGreg Kroah-Hartman 2581ab4382d2SGreg Kroah-Hartman /* 2582ab4382d2SGreg Kroah-Hartman * Finally, wait for transmitter to become empty 2583ab4382d2SGreg Kroah-Hartman * and restore IMR 2584ab4382d2SGreg Kroah-Hartman */ 2585ab4382d2SGreg Kroah-Hartman do { 25864e7decdaSCyrille Pitchen status = atmel_uart_readl(port, ATMEL_US_CSR); 2587ab4382d2SGreg Kroah-Hartman } while (!(status & ATMEL_US_TXRDY)); 2588ab4382d2SGreg Kroah-Hartman 2589ab4382d2SGreg Kroah-Hartman /* Restore PDC transmit status */ 2590ab4382d2SGreg Kroah-Hartman if (pdc_tx) 25914e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_TXTEN); 2592ab4382d2SGreg Kroah-Hartman 2593ab4382d2SGreg Kroah-Hartman /* set interrupts back the way they were */ 25944e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_IER, imr); 2595ab4382d2SGreg Kroah-Hartman } 2596ab4382d2SGreg Kroah-Hartman 2597ab4382d2SGreg Kroah-Hartman /* 2598ab4382d2SGreg Kroah-Hartman * If the port was already initialised (eg, by a boot loader), 2599ab4382d2SGreg Kroah-Hartman * try to determine the current setup. 2600ab4382d2SGreg Kroah-Hartman */ 2601ab4382d2SGreg Kroah-Hartman static void __init atmel_console_get_options(struct uart_port *port, int *baud, 2602ab4382d2SGreg Kroah-Hartman int *parity, int *bits) 2603ab4382d2SGreg Kroah-Hartman { 2604ab4382d2SGreg Kroah-Hartman unsigned int mr, quot; 2605ab4382d2SGreg Kroah-Hartman 2606ab4382d2SGreg Kroah-Hartman /* 2607ab4382d2SGreg Kroah-Hartman * If the baud rate generator isn't running, the port wasn't 2608ab4382d2SGreg Kroah-Hartman * initialized by the boot loader. 2609ab4382d2SGreg Kroah-Hartman */ 26104e7decdaSCyrille Pitchen quot = atmel_uart_readl(port, ATMEL_US_BRGR) & ATMEL_US_CD; 2611ab4382d2SGreg Kroah-Hartman if (!quot) 2612ab4382d2SGreg Kroah-Hartman return; 2613ab4382d2SGreg Kroah-Hartman 26144e7decdaSCyrille Pitchen mr = atmel_uart_readl(port, ATMEL_US_MR) & ATMEL_US_CHRL; 2615ab4382d2SGreg Kroah-Hartman if (mr == ATMEL_US_CHRL_8) 2616ab4382d2SGreg Kroah-Hartman *bits = 8; 2617ab4382d2SGreg Kroah-Hartman else 2618ab4382d2SGreg Kroah-Hartman *bits = 7; 2619ab4382d2SGreg Kroah-Hartman 26204e7decdaSCyrille Pitchen mr = atmel_uart_readl(port, ATMEL_US_MR) & ATMEL_US_PAR; 2621ab4382d2SGreg Kroah-Hartman if (mr == ATMEL_US_PAR_EVEN) 2622ab4382d2SGreg Kroah-Hartman *parity = 'e'; 2623ab4382d2SGreg Kroah-Hartman else if (mr == ATMEL_US_PAR_ODD) 2624ab4382d2SGreg Kroah-Hartman *parity = 'o'; 2625ab4382d2SGreg Kroah-Hartman 2626ab4382d2SGreg Kroah-Hartman /* 2627ab4382d2SGreg Kroah-Hartman * The serial core only rounds down when matching this to a 2628ab4382d2SGreg Kroah-Hartman * supported baud rate. Make sure we don't end up slightly 2629ab4382d2SGreg Kroah-Hartman * lower than one of those, as it would make us fall through 2630ab4382d2SGreg Kroah-Hartman * to a much lower baud rate than we really want. 2631ab4382d2SGreg Kroah-Hartman */ 2632ab4382d2SGreg Kroah-Hartman *baud = port->uartclk / (16 * (quot - 1)); 2633ab4382d2SGreg Kroah-Hartman } 2634ab4382d2SGreg Kroah-Hartman 2635ab4382d2SGreg Kroah-Hartman static int __init atmel_console_setup(struct console *co, char *options) 2636ab4382d2SGreg Kroah-Hartman { 2637ab4382d2SGreg Kroah-Hartman struct uart_port *port = &atmel_ports[co->index].uart; 2638ea04f82aSRomain Izard struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 2639ab4382d2SGreg Kroah-Hartman int baud = 115200; 2640ab4382d2SGreg Kroah-Hartman int bits = 8; 2641ab4382d2SGreg Kroah-Hartman int parity = 'n'; 2642ab4382d2SGreg Kroah-Hartman int flow = 'n'; 2643ab4382d2SGreg Kroah-Hartman 2644ab4382d2SGreg Kroah-Hartman if (port->membase == NULL) { 2645ab4382d2SGreg Kroah-Hartman /* Port not initialized yet - delay setup */ 2646ab4382d2SGreg Kroah-Hartman return -ENODEV; 2647ab4382d2SGreg Kroah-Hartman } 2648ab4382d2SGreg Kroah-Hartman 26494e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_IDR, -1); 26504e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA | ATMEL_US_RSTRX); 26514e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_TXEN | ATMEL_US_RXEN); 2652ea04f82aSRomain Izard atmel_port->tx_stopped = false; 2653ab4382d2SGreg Kroah-Hartman 2654ab4382d2SGreg Kroah-Hartman if (options) 2655ab4382d2SGreg Kroah-Hartman uart_parse_options(options, &baud, &parity, &bits, &flow); 2656ab4382d2SGreg Kroah-Hartman else 2657ab4382d2SGreg Kroah-Hartman atmel_console_get_options(port, &baud, &parity, &bits); 2658ab4382d2SGreg Kroah-Hartman 2659ab4382d2SGreg Kroah-Hartman return uart_set_options(port, co, baud, parity, bits, flow); 2660ab4382d2SGreg Kroah-Hartman } 2661ab4382d2SGreg Kroah-Hartman 2662ab4382d2SGreg Kroah-Hartman static struct uart_driver atmel_uart; 2663ab4382d2SGreg Kroah-Hartman 2664ab4382d2SGreg Kroah-Hartman static struct console atmel_console = { 2665ab4382d2SGreg Kroah-Hartman .name = ATMEL_DEVICENAME, 2666ab4382d2SGreg Kroah-Hartman .write = atmel_console_write, 2667ab4382d2SGreg Kroah-Hartman .device = uart_console_device, 2668ab4382d2SGreg Kroah-Hartman .setup = atmel_console_setup, 2669ab4382d2SGreg Kroah-Hartman .flags = CON_PRINTBUFFER, 2670ab4382d2SGreg Kroah-Hartman .index = -1, 2671ab4382d2SGreg Kroah-Hartman .data = &atmel_uart, 2672ab4382d2SGreg Kroah-Hartman }; 2673ab4382d2SGreg Kroah-Hartman 2674aab68e95SMichael Walle static void atmel_serial_early_write(struct console *con, const char *s, 2675aab68e95SMichael Walle unsigned int n) 2676aab68e95SMichael Walle { 2677aab68e95SMichael Walle struct earlycon_device *dev = con->data; 2678aab68e95SMichael Walle 2679aab68e95SMichael Walle uart_console_write(&dev->port, s, n, atmel_console_putchar); 2680aab68e95SMichael Walle } 2681aab68e95SMichael Walle 2682aab68e95SMichael Walle static int __init atmel_early_console_setup(struct earlycon_device *device, 2683aab68e95SMichael Walle const char *options) 2684aab68e95SMichael Walle { 2685aab68e95SMichael Walle if (!device->port.membase) 2686aab68e95SMichael Walle return -ENODEV; 2687aab68e95SMichael Walle 2688aab68e95SMichael Walle device->con->write = atmel_serial_early_write; 2689aab68e95SMichael Walle 2690aab68e95SMichael Walle return 0; 2691aab68e95SMichael Walle } 2692aab68e95SMichael Walle 2693aab68e95SMichael Walle OF_EARLYCON_DECLARE(atmel_serial, "atmel,at91rm9200-usart", 2694aab68e95SMichael Walle atmel_early_console_setup); 2695aab68e95SMichael Walle OF_EARLYCON_DECLARE(atmel_serial, "atmel,at91sam9260-usart", 2696aab68e95SMichael Walle atmel_early_console_setup); 2697aab68e95SMichael Walle 2698ab4382d2SGreg Kroah-Hartman #define ATMEL_CONSOLE_DEVICE (&atmel_console) 2699ab4382d2SGreg Kroah-Hartman 2700ab4382d2SGreg Kroah-Hartman #else 2701ab4382d2SGreg Kroah-Hartman #define ATMEL_CONSOLE_DEVICE NULL 2702ab4382d2SGreg Kroah-Hartman #endif 2703ab4382d2SGreg Kroah-Hartman 2704ab4382d2SGreg Kroah-Hartman static struct uart_driver atmel_uart = { 2705ab4382d2SGreg Kroah-Hartman .owner = THIS_MODULE, 2706ab4382d2SGreg Kroah-Hartman .driver_name = "atmel_serial", 2707ab4382d2SGreg Kroah-Hartman .dev_name = ATMEL_DEVICENAME, 2708ab4382d2SGreg Kroah-Hartman .major = SERIAL_ATMEL_MAJOR, 2709ab4382d2SGreg Kroah-Hartman .minor = MINOR_START, 2710ab4382d2SGreg Kroah-Hartman .nr = ATMEL_MAX_UART, 2711ab4382d2SGreg Kroah-Hartman .cons = ATMEL_CONSOLE_DEVICE, 2712ab4382d2SGreg Kroah-Hartman }; 2713ab4382d2SGreg Kroah-Hartman 2714ab4382d2SGreg Kroah-Hartman static bool atmel_serial_clk_will_stop(void) 2715ab4382d2SGreg Kroah-Hartman { 2716ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_ARCH_AT91 2717ab4382d2SGreg Kroah-Hartman return at91_suspend_entering_slow_clock(); 2718ab4382d2SGreg Kroah-Hartman #else 2719ab4382d2SGreg Kroah-Hartman return false; 2720ab4382d2SGreg Kroah-Hartman #endif 2721ab4382d2SGreg Kroah-Hartman } 2722ab4382d2SGreg Kroah-Hartman 2723b50058b8SClaudiu Beznea static int __maybe_unused atmel_serial_suspend(struct device *dev) 2724ab4382d2SGreg Kroah-Hartman { 2725b50058b8SClaudiu Beznea struct uart_port *port = dev_get_drvdata(dev); 2726ab4382d2SGreg Kroah-Hartman struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 2727ab4382d2SGreg Kroah-Hartman 2728207f6f34SAndy Shevchenko if (uart_console(port) && console_suspend_enabled) { 2729ab4382d2SGreg Kroah-Hartman /* Drain the TX shifter */ 27304e7decdaSCyrille Pitchen while (!(atmel_uart_readl(port, ATMEL_US_CSR) & 27314e7decdaSCyrille Pitchen ATMEL_US_TXEMPTY)) 2732ab4382d2SGreg Kroah-Hartman cpu_relax(); 2733ab4382d2SGreg Kroah-Hartman } 2734ab4382d2SGreg Kroah-Hartman 2735207f6f34SAndy Shevchenko if (uart_console(port) && !console_suspend_enabled) { 27366a5f0e2fSAlexandre Belloni /* Cache register values as we won't get a full shutdown/startup 27376a5f0e2fSAlexandre Belloni * cycle 27386a5f0e2fSAlexandre Belloni */ 27396a5f0e2fSAlexandre Belloni atmel_port->cache.mr = atmel_uart_readl(port, ATMEL_US_MR); 27406a5f0e2fSAlexandre Belloni atmel_port->cache.imr = atmel_uart_readl(port, ATMEL_US_IMR); 27416a5f0e2fSAlexandre Belloni atmel_port->cache.brgr = atmel_uart_readl(port, ATMEL_US_BRGR); 27426a5f0e2fSAlexandre Belloni atmel_port->cache.rtor = atmel_uart_readl(port, 27436a5f0e2fSAlexandre Belloni atmel_port->rtor); 27446a5f0e2fSAlexandre Belloni atmel_port->cache.ttgr = atmel_uart_readl(port, ATMEL_US_TTGR); 27456a5f0e2fSAlexandre Belloni atmel_port->cache.fmr = atmel_uart_readl(port, ATMEL_US_FMR); 27466a5f0e2fSAlexandre Belloni atmel_port->cache.fimr = atmel_uart_readl(port, ATMEL_US_FIMR); 27476a5f0e2fSAlexandre Belloni } 27486a5f0e2fSAlexandre Belloni 2749ab4382d2SGreg Kroah-Hartman /* we can not wake up if we're running on slow clock */ 2750b50058b8SClaudiu Beznea atmel_port->may_wakeup = device_may_wakeup(dev); 27512c7af5baSBoris BREZILLON if (atmel_serial_clk_will_stop()) { 27522c7af5baSBoris BREZILLON unsigned long flags; 27532c7af5baSBoris BREZILLON 27542c7af5baSBoris BREZILLON spin_lock_irqsave(&atmel_port->lock_suspended, flags); 27552c7af5baSBoris BREZILLON atmel_port->suspended = true; 27562c7af5baSBoris BREZILLON spin_unlock_irqrestore(&atmel_port->lock_suspended, flags); 2757b50058b8SClaudiu Beznea device_set_wakeup_enable(dev, 0); 27582c7af5baSBoris BREZILLON } 2759ab4382d2SGreg Kroah-Hartman 2760ab4382d2SGreg Kroah-Hartman uart_suspend_port(&atmel_uart, port); 2761ab4382d2SGreg Kroah-Hartman 2762ab4382d2SGreg Kroah-Hartman return 0; 2763ab4382d2SGreg Kroah-Hartman } 2764ab4382d2SGreg Kroah-Hartman 2765b50058b8SClaudiu Beznea static int __maybe_unused atmel_serial_resume(struct device *dev) 2766ab4382d2SGreg Kroah-Hartman { 2767b50058b8SClaudiu Beznea struct uart_port *port = dev_get_drvdata(dev); 2768ab4382d2SGreg Kroah-Hartman struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 27692c7af5baSBoris BREZILLON unsigned long flags; 27702c7af5baSBoris BREZILLON 2771207f6f34SAndy Shevchenko if (uart_console(port) && !console_suspend_enabled) { 27726a5f0e2fSAlexandre Belloni atmel_uart_writel(port, ATMEL_US_MR, atmel_port->cache.mr); 27736a5f0e2fSAlexandre Belloni atmel_uart_writel(port, ATMEL_US_IER, atmel_port->cache.imr); 27746a5f0e2fSAlexandre Belloni atmel_uart_writel(port, ATMEL_US_BRGR, atmel_port->cache.brgr); 27756a5f0e2fSAlexandre Belloni atmel_uart_writel(port, atmel_port->rtor, 27766a5f0e2fSAlexandre Belloni atmel_port->cache.rtor); 27776a5f0e2fSAlexandre Belloni atmel_uart_writel(port, ATMEL_US_TTGR, atmel_port->cache.ttgr); 27786a5f0e2fSAlexandre Belloni 27796a5f0e2fSAlexandre Belloni if (atmel_port->fifo_size) { 27806a5f0e2fSAlexandre Belloni atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_FIFOEN | 27816a5f0e2fSAlexandre Belloni ATMEL_US_RXFCLR | ATMEL_US_TXFLCLR); 27826a5f0e2fSAlexandre Belloni atmel_uart_writel(port, ATMEL_US_FMR, 27836a5f0e2fSAlexandre Belloni atmel_port->cache.fmr); 27846a5f0e2fSAlexandre Belloni atmel_uart_writel(port, ATMEL_US_FIER, 27856a5f0e2fSAlexandre Belloni atmel_port->cache.fimr); 27866a5f0e2fSAlexandre Belloni } 27876a5f0e2fSAlexandre Belloni atmel_start_rx(port); 27886a5f0e2fSAlexandre Belloni } 27896a5f0e2fSAlexandre Belloni 27902c7af5baSBoris BREZILLON spin_lock_irqsave(&atmel_port->lock_suspended, flags); 27912c7af5baSBoris BREZILLON if (atmel_port->pending) { 27922c7af5baSBoris BREZILLON atmel_handle_receive(port, atmel_port->pending); 27932c7af5baSBoris BREZILLON atmel_handle_status(port, atmel_port->pending, 27942c7af5baSBoris BREZILLON atmel_port->pending_status); 27952c7af5baSBoris BREZILLON atmel_handle_transmit(port, atmel_port->pending); 27962c7af5baSBoris BREZILLON atmel_port->pending = 0; 27972c7af5baSBoris BREZILLON } 27982c7af5baSBoris BREZILLON atmel_port->suspended = false; 27992c7af5baSBoris BREZILLON spin_unlock_irqrestore(&atmel_port->lock_suspended, flags); 2800ab4382d2SGreg Kroah-Hartman 2801ab4382d2SGreg Kroah-Hartman uart_resume_port(&atmel_uart, port); 2802b50058b8SClaudiu Beznea device_set_wakeup_enable(dev, atmel_port->may_wakeup); 2803ab4382d2SGreg Kroah-Hartman 2804ab4382d2SGreg Kroah-Hartman return 0; 2805ab4382d2SGreg Kroah-Hartman } 2806ab4382d2SGreg Kroah-Hartman 2807b78cd169SJaeden Amero static void atmel_serial_probe_fifos(struct atmel_uart_port *atmel_port, 2808b5199d46SCyrille Pitchen struct platform_device *pdev) 2809b5199d46SCyrille Pitchen { 2810b78cd169SJaeden Amero atmel_port->fifo_size = 0; 2811b78cd169SJaeden Amero atmel_port->rts_low = 0; 2812b78cd169SJaeden Amero atmel_port->rts_high = 0; 2813b5199d46SCyrille Pitchen 2814b5199d46SCyrille Pitchen if (of_property_read_u32(pdev->dev.of_node, 2815b5199d46SCyrille Pitchen "atmel,fifo-size", 2816b78cd169SJaeden Amero &atmel_port->fifo_size)) 2817b5199d46SCyrille Pitchen return; 2818b5199d46SCyrille Pitchen 2819b78cd169SJaeden Amero if (!atmel_port->fifo_size) 2820b5199d46SCyrille Pitchen return; 2821b5199d46SCyrille Pitchen 2822b78cd169SJaeden Amero if (atmel_port->fifo_size < ATMEL_MIN_FIFO_SIZE) { 2823b78cd169SJaeden Amero atmel_port->fifo_size = 0; 2824b5199d46SCyrille Pitchen dev_err(&pdev->dev, "Invalid FIFO size\n"); 2825b5199d46SCyrille Pitchen return; 2826b5199d46SCyrille Pitchen } 2827b5199d46SCyrille Pitchen 2828b5199d46SCyrille Pitchen /* 2829b5199d46SCyrille Pitchen * 0 <= rts_low <= rts_high <= fifo_size 2830b5199d46SCyrille Pitchen * Once their CTS line asserted by the remote peer, some x86 UARTs tend 2831b5199d46SCyrille Pitchen * to flush their internal TX FIFO, commonly up to 16 data, before 2832b5199d46SCyrille Pitchen * actually stopping to send new data. So we try to set the RTS High 2833b5199d46SCyrille Pitchen * Threshold to a reasonably high value respecting this 16 data 2834b5199d46SCyrille Pitchen * empirical rule when possible. 2835b5199d46SCyrille Pitchen */ 2836b78cd169SJaeden Amero atmel_port->rts_high = max_t(int, atmel_port->fifo_size >> 1, 2837b78cd169SJaeden Amero atmel_port->fifo_size - ATMEL_RTS_HIGH_OFFSET); 2838b78cd169SJaeden Amero atmel_port->rts_low = max_t(int, atmel_port->fifo_size >> 2, 2839b78cd169SJaeden Amero atmel_port->fifo_size - ATMEL_RTS_LOW_OFFSET); 2840b5199d46SCyrille Pitchen 2841b5199d46SCyrille Pitchen dev_info(&pdev->dev, "Using FIFO (%u data)\n", 2842b78cd169SJaeden Amero atmel_port->fifo_size); 2843b5199d46SCyrille Pitchen dev_dbg(&pdev->dev, "RTS High Threshold : %2u data\n", 2844b78cd169SJaeden Amero atmel_port->rts_high); 2845b5199d46SCyrille Pitchen dev_dbg(&pdev->dev, "RTS Low Threshold : %2u data\n", 2846b78cd169SJaeden Amero atmel_port->rts_low); 2847b5199d46SCyrille Pitchen } 2848b5199d46SCyrille Pitchen 28499671f099SBill Pemberton static int atmel_serial_probe(struct platform_device *pdev) 2850ab4382d2SGreg Kroah-Hartman { 2851b78cd169SJaeden Amero struct atmel_uart_port *atmel_port; 2852c24d2531SRadu Pirea struct device_node *np = pdev->dev.parent->of_node; 2853ab4382d2SGreg Kroah-Hartman void *data; 28548d41ab87SColin Ian King int ret; 2855bd737f87SRicardo Ribalda Delgado bool rs485_enabled; 2856ab4382d2SGreg Kroah-Hartman 2857ab4382d2SGreg Kroah-Hartman BUILD_BUG_ON(ATMEL_SERIAL_RINGSIZE & (ATMEL_SERIAL_RINGSIZE - 1)); 2858ab4382d2SGreg Kroah-Hartman 2859c24d2531SRadu Pirea /* 2860c24d2531SRadu Pirea * In device tree there is no node with "atmel,at91rm9200-usart-serial" 2861c24d2531SRadu Pirea * as compatible string. This driver is probed by at91-usart mfd driver 2862c24d2531SRadu Pirea * which is just a wrapper over the atmel_serial driver and 2863c24d2531SRadu Pirea * spi-at91-usart driver. All attributes needed by this driver are 2864c24d2531SRadu Pirea * found in of_node of parent. 2865c24d2531SRadu Pirea */ 2866c24d2531SRadu Pirea pdev->dev.of_node = np; 2867c24d2531SRadu Pirea 28685fbe46b6SNicolas Ferre ret = of_alias_get_id(np, "serial"); 28694cbf9f48SNicolas Ferre if (ret < 0) 28705fbe46b6SNicolas Ferre /* port id not found in platform data nor device-tree aliases: 28714cbf9f48SNicolas Ferre * auto-enumerate it */ 2872503bded9SPawel Wieczorkiewicz ret = find_first_zero_bit(atmel_ports_in_use, ATMEL_MAX_UART); 28734cbf9f48SNicolas Ferre 2874503bded9SPawel Wieczorkiewicz if (ret >= ATMEL_MAX_UART) { 28754cbf9f48SNicolas Ferre ret = -ENODEV; 28764cbf9f48SNicolas Ferre goto err; 28774cbf9f48SNicolas Ferre } 28784cbf9f48SNicolas Ferre 2879503bded9SPawel Wieczorkiewicz if (test_and_set_bit(ret, atmel_ports_in_use)) { 28804cbf9f48SNicolas Ferre /* port already in use */ 28814cbf9f48SNicolas Ferre ret = -EBUSY; 28824cbf9f48SNicolas Ferre goto err; 28834cbf9f48SNicolas Ferre } 28844cbf9f48SNicolas Ferre 2885b78cd169SJaeden Amero atmel_port = &atmel_ports[ret]; 2886b78cd169SJaeden Amero atmel_port->backup_imr = 0; 2887b78cd169SJaeden Amero atmel_port->uart.line = ret; 2888078abd98SDmitry Safonov atmel_port->uart.has_sysrq = IS_ENABLED(CONFIG_SERIAL_ATMEL_CONSOLE); 2889b78cd169SJaeden Amero atmel_serial_probe_fifos(atmel_port, pdev); 2890354e57f3SLinus Walleij 289198f2082cSNicolas Ferre atomic_set(&atmel_port->tasklet_shutdown, 0); 2892b78cd169SJaeden Amero spin_lock_init(&atmel_port->lock_suspended); 28932c7af5baSBoris BREZILLON 289484b476b1SClaudiu Beznea atmel_port->clk = devm_clk_get(&pdev->dev, "usart"); 289584b476b1SClaudiu Beznea if (IS_ERR(atmel_port->clk)) { 289684b476b1SClaudiu Beznea ret = PTR_ERR(atmel_port->clk); 289784b476b1SClaudiu Beznea goto err; 289884b476b1SClaudiu Beznea } 289984b476b1SClaudiu Beznea ret = clk_prepare_enable(atmel_port->clk); 290084b476b1SClaudiu Beznea if (ret) 290184b476b1SClaudiu Beznea goto err; 290284b476b1SClaudiu Beznea 2903b78cd169SJaeden Amero ret = atmel_init_port(atmel_port, pdev); 290491f8c2d8SBoris BREZILLON if (ret) 290584b476b1SClaudiu Beznea goto err_clk_disable_unprepare; 2906ab4382d2SGreg Kroah-Hartman 2907b78cd169SJaeden Amero atmel_port->gpios = mctrl_gpio_init(&atmel_port->uart, 0); 2908b78cd169SJaeden Amero if (IS_ERR(atmel_port->gpios)) { 2909b78cd169SJaeden Amero ret = PTR_ERR(atmel_port->gpios); 291084b476b1SClaudiu Beznea goto err_clk_disable_unprepare; 291118dfef9cSUwe Kleine-König } 291218dfef9cSUwe Kleine-König 2913b78cd169SJaeden Amero if (!atmel_use_pdc_rx(&atmel_port->uart)) { 2914ab4382d2SGreg Kroah-Hartman ret = -ENOMEM; 29156da2ec56SKees Cook data = kmalloc_array(ATMEL_SERIAL_RINGSIZE, 29166da2ec56SKees Cook sizeof(struct atmel_uart_char), 29176da2ec56SKees Cook GFP_KERNEL); 2918ab4382d2SGreg Kroah-Hartman if (!data) 291984b476b1SClaudiu Beznea goto err_clk_disable_unprepare; 2920b78cd169SJaeden Amero atmel_port->rx_ring.buf = data; 2921ab4382d2SGreg Kroah-Hartman } 2922ab4382d2SGreg Kroah-Hartman 2923b78cd169SJaeden Amero rs485_enabled = atmel_port->uart.rs485.flags & SER_RS485_ENABLED; 2924bd737f87SRicardo Ribalda Delgado 2925b78cd169SJaeden Amero ret = uart_add_one_port(&atmel_uart, &atmel_port->uart); 2926ab4382d2SGreg Kroah-Hartman if (ret) 2927ab4382d2SGreg Kroah-Hartman goto err_add_port; 2928ab4382d2SGreg Kroah-Hartman 2929ab4382d2SGreg Kroah-Hartman device_init_wakeup(&pdev->dev, 1); 2930b78cd169SJaeden Amero platform_set_drvdata(pdev, atmel_port); 2931ab4382d2SGreg Kroah-Hartman 2932bd737f87SRicardo Ribalda Delgado if (rs485_enabled) { 2933b78cd169SJaeden Amero atmel_uart_writel(&atmel_port->uart, ATMEL_US_MR, 29344e7decdaSCyrille Pitchen ATMEL_US_USMODE_NORMAL); 2935b78cd169SJaeden Amero atmel_uart_writel(&atmel_port->uart, ATMEL_US_CR, 2936b78cd169SJaeden Amero ATMEL_US_RTSEN); 2937fc887b15SLinus Torvalds } 2938fc887b15SLinus Torvalds 2939055560b0SElen Song /* 2940055560b0SElen Song * Get port name of usart or uart 2941055560b0SElen Song */ 2942b78cd169SJaeden Amero atmel_get_ip_name(&atmel_port->uart); 2943055560b0SElen Song 2944d4f64187SCyrille Pitchen /* 2945d4f64187SCyrille Pitchen * The peripheral clock can now safely be disabled till the port 2946d4f64187SCyrille Pitchen * is used 2947d4f64187SCyrille Pitchen */ 2948b78cd169SJaeden Amero clk_disable_unprepare(atmel_port->clk); 2949d4f64187SCyrille Pitchen 2950ab4382d2SGreg Kroah-Hartman return 0; 2951ab4382d2SGreg Kroah-Hartman 2952ab4382d2SGreg Kroah-Hartman err_add_port: 2953b78cd169SJaeden Amero kfree(atmel_port->rx_ring.buf); 2954b78cd169SJaeden Amero atmel_port->rx_ring.buf = NULL; 295584b476b1SClaudiu Beznea err_clk_disable_unprepare: 295684b476b1SClaudiu Beznea clk_disable_unprepare(atmel_port->clk); 2957b78cd169SJaeden Amero clear_bit(atmel_port->uart.line, atmel_ports_in_use); 29584cbf9f48SNicolas Ferre err: 2959ab4382d2SGreg Kroah-Hartman return ret; 2960ab4382d2SGreg Kroah-Hartman } 2961ab4382d2SGreg Kroah-Hartman 2962f4a8ab04SRomain Izard /* 2963f4a8ab04SRomain Izard * Even if the driver is not modular, it makes sense to be able to 2964f4a8ab04SRomain Izard * unbind a device: there can be many bound devices, and there are 2965f4a8ab04SRomain Izard * situations where dynamic binding and unbinding can be useful. 2966f4a8ab04SRomain Izard * 2967f4a8ab04SRomain Izard * For example, a connected device can require a specific firmware update 2968f4a8ab04SRomain Izard * protocol that needs bitbanging on IO lines, but use the regular serial 2969f4a8ab04SRomain Izard * port in the normal case. 2970f4a8ab04SRomain Izard */ 2971f4a8ab04SRomain Izard static int atmel_serial_remove(struct platform_device *pdev) 2972f4a8ab04SRomain Izard { 2973f4a8ab04SRomain Izard struct uart_port *port = platform_get_drvdata(pdev); 2974f4a8ab04SRomain Izard struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 2975f4a8ab04SRomain Izard int ret = 0; 2976f4a8ab04SRomain Izard 297700e8e658SNicolas Ferre tasklet_kill(&atmel_port->tasklet_rx); 297800e8e658SNicolas Ferre tasklet_kill(&atmel_port->tasklet_tx); 2979f4a8ab04SRomain Izard 2980f4a8ab04SRomain Izard device_init_wakeup(&pdev->dev, 0); 2981f4a8ab04SRomain Izard 2982f4a8ab04SRomain Izard ret = uart_remove_one_port(&atmel_uart, port); 2983f4a8ab04SRomain Izard 2984f4a8ab04SRomain Izard kfree(atmel_port->rx_ring.buf); 2985f4a8ab04SRomain Izard 2986f4a8ab04SRomain Izard /* "port" is allocated statically, so we shouldn't free it */ 2987f4a8ab04SRomain Izard 2988f4a8ab04SRomain Izard clear_bit(port->line, atmel_ports_in_use); 2989f4a8ab04SRomain Izard 2990c24d2531SRadu Pirea pdev->dev.of_node = NULL; 2991f4a8ab04SRomain Izard 2992f4a8ab04SRomain Izard return ret; 2993f4a8ab04SRomain Izard } 2994f4a8ab04SRomain Izard 2995b50058b8SClaudiu Beznea static SIMPLE_DEV_PM_OPS(atmel_serial_pm_ops, atmel_serial_suspend, 2996b50058b8SClaudiu Beznea atmel_serial_resume); 2997b50058b8SClaudiu Beznea 2998ab4382d2SGreg Kroah-Hartman static struct platform_driver atmel_serial_driver = { 2999ab4382d2SGreg Kroah-Hartman .probe = atmel_serial_probe, 3000f4a8ab04SRomain Izard .remove = atmel_serial_remove, 3001ab4382d2SGreg Kroah-Hartman .driver = { 3002c24d2531SRadu Pirea .name = "atmel_usart_serial", 30035fbe46b6SNicolas Ferre .of_match_table = of_match_ptr(atmel_serial_dt_ids), 3004b50058b8SClaudiu Beznea .pm = pm_ptr(&atmel_serial_pm_ops), 3005ab4382d2SGreg Kroah-Hartman }, 3006ab4382d2SGreg Kroah-Hartman }; 3007ab4382d2SGreg Kroah-Hartman 3008ab4382d2SGreg Kroah-Hartman static int __init atmel_serial_init(void) 3009ab4382d2SGreg Kroah-Hartman { 3010ab4382d2SGreg Kroah-Hartman int ret; 3011ab4382d2SGreg Kroah-Hartman 3012ab4382d2SGreg Kroah-Hartman ret = uart_register_driver(&atmel_uart); 3013ab4382d2SGreg Kroah-Hartman if (ret) 3014ab4382d2SGreg Kroah-Hartman return ret; 3015ab4382d2SGreg Kroah-Hartman 3016ab4382d2SGreg Kroah-Hartman ret = platform_driver_register(&atmel_serial_driver); 3017ab4382d2SGreg Kroah-Hartman if (ret) 3018ab4382d2SGreg Kroah-Hartman uart_unregister_driver(&atmel_uart); 3019ab4382d2SGreg Kroah-Hartman 3020ab4382d2SGreg Kroah-Hartman return ret; 3021ab4382d2SGreg Kroah-Hartman } 3022c39dfebcSPaul Gortmaker device_initcall(atmel_serial_init); 3023