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 */ 11ab4382d2SGreg Kroah-Hartman #include <linux/tty.h> 12ab4382d2SGreg Kroah-Hartman #include <linux/ioport.h> 13ab4382d2SGreg Kroah-Hartman #include <linux/slab.h> 14ab4382d2SGreg Kroah-Hartman #include <linux/init.h> 15ab4382d2SGreg Kroah-Hartman #include <linux/serial.h> 16ab4382d2SGreg Kroah-Hartman #include <linux/clk.h> 17ab4382d2SGreg Kroah-Hartman #include <linux/console.h> 18ab4382d2SGreg Kroah-Hartman #include <linux/sysrq.h> 19ab4382d2SGreg Kroah-Hartman #include <linux/tty_flip.h> 20ab4382d2SGreg Kroah-Hartman #include <linux/platform_device.h> 215fbe46b6SNicolas Ferre #include <linux/of.h> 225fbe46b6SNicolas Ferre #include <linux/of_device.h> 23354e57f3SLinus Walleij #include <linux/of_gpio.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> 30354e57f3SLinus Walleij #include <linux/gpio.h> 31e0b0baadSRichard Genoud #include <linux/gpio/consumer.h> 32e0b0baadSRichard Genoud #include <linux/err.h> 33ab5e4e41SRichard Genoud #include <linux/irq.h> 342c7af5baSBoris BREZILLON #include <linux/suspend.h> 352b5cf14bSGeliang Tang #include <linux/mm.h> 36ab4382d2SGreg Kroah-Hartman 37*377fedd1SNicolas Ferre #include <asm/div64.h> 38ab4382d2SGreg Kroah-Hartman #include <asm/io.h> 39ab4382d2SGreg Kroah-Hartman #include <asm/ioctls.h> 40ab4382d2SGreg Kroah-Hartman 41ab4382d2SGreg Kroah-Hartman #define PDC_BUFFER_SIZE 512 42ab4382d2SGreg Kroah-Hartman /* Revisit: We should calculate this based on the actual port settings */ 43ab4382d2SGreg Kroah-Hartman #define PDC_RX_TIMEOUT (3 * 10) /* 3 bytes */ 44ab4382d2SGreg Kroah-Hartman 45b5199d46SCyrille Pitchen /* The minium number of data FIFOs should be able to contain */ 46b5199d46SCyrille Pitchen #define ATMEL_MIN_FIFO_SIZE 8 47b5199d46SCyrille Pitchen /* 48b5199d46SCyrille Pitchen * These two offsets are substracted from the RX FIFO size to define the RTS 49b5199d46SCyrille Pitchen * high and low thresholds 50b5199d46SCyrille Pitchen */ 51b5199d46SCyrille Pitchen #define ATMEL_RTS_HIGH_OFFSET 16 52b5199d46SCyrille Pitchen #define ATMEL_RTS_LOW_OFFSET 20 53b5199d46SCyrille Pitchen 54ab4382d2SGreg Kroah-Hartman #if defined(CONFIG_SERIAL_ATMEL_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ) 55ab4382d2SGreg Kroah-Hartman #define SUPPORT_SYSRQ 56ab4382d2SGreg Kroah-Hartman #endif 57ab4382d2SGreg Kroah-Hartman 58ab4382d2SGreg Kroah-Hartman #include <linux/serial_core.h> 59ab4382d2SGreg Kroah-Hartman 60e0b0baadSRichard Genoud #include "serial_mctrl_gpio.h" 618961df89SRichard Genoud #include "atmel_serial.h" 62e0b0baadSRichard Genoud 63ab4382d2SGreg Kroah-Hartman static void atmel_start_rx(struct uart_port *port); 64ab4382d2SGreg Kroah-Hartman static void atmel_stop_rx(struct uart_port *port); 65ab4382d2SGreg Kroah-Hartman 66ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_SERIAL_ATMEL_TTYAT 67ab4382d2SGreg Kroah-Hartman 68ab4382d2SGreg Kroah-Hartman /* Use device name ttyAT, major 204 and minor 154-169. This is necessary if we 69ab4382d2SGreg Kroah-Hartman * should coexist with the 8250 driver, such as if we have an external 16C550 70ab4382d2SGreg Kroah-Hartman * UART. */ 71ab4382d2SGreg Kroah-Hartman #define SERIAL_ATMEL_MAJOR 204 72ab4382d2SGreg Kroah-Hartman #define MINOR_START 154 73ab4382d2SGreg Kroah-Hartman #define ATMEL_DEVICENAME "ttyAT" 74ab4382d2SGreg Kroah-Hartman 75ab4382d2SGreg Kroah-Hartman #else 76ab4382d2SGreg Kroah-Hartman 77ab4382d2SGreg Kroah-Hartman /* Use device name ttyS, major 4, minor 64-68. This is the usual serial port 78ab4382d2SGreg Kroah-Hartman * name, but it is legally reserved for the 8250 driver. */ 79ab4382d2SGreg Kroah-Hartman #define SERIAL_ATMEL_MAJOR TTY_MAJOR 80ab4382d2SGreg Kroah-Hartman #define MINOR_START 64 81ab4382d2SGreg Kroah-Hartman #define ATMEL_DEVICENAME "ttyS" 82ab4382d2SGreg Kroah-Hartman 83ab4382d2SGreg Kroah-Hartman #endif 84ab4382d2SGreg Kroah-Hartman 85ab4382d2SGreg Kroah-Hartman #define ATMEL_ISR_PASS_LIMIT 256 86ab4382d2SGreg Kroah-Hartman 87ab4382d2SGreg Kroah-Hartman struct atmel_dma_buffer { 88ab4382d2SGreg Kroah-Hartman unsigned char *buf; 89ab4382d2SGreg Kroah-Hartman dma_addr_t dma_addr; 90ab4382d2SGreg Kroah-Hartman unsigned int dma_size; 91ab4382d2SGreg Kroah-Hartman unsigned int ofs; 92ab4382d2SGreg Kroah-Hartman }; 93ab4382d2SGreg Kroah-Hartman 94ab4382d2SGreg Kroah-Hartman struct atmel_uart_char { 95ab4382d2SGreg Kroah-Hartman u16 status; 96ab4382d2SGreg Kroah-Hartman u16 ch; 97ab4382d2SGreg Kroah-Hartman }; 98ab4382d2SGreg Kroah-Hartman 99637ba54fSLudovic Desroches /* 100637ba54fSLudovic Desroches * Be careful, the real size of the ring buffer is 101637ba54fSLudovic Desroches * sizeof(atmel_uart_char) * ATMEL_SERIAL_RINGSIZE. It means that ring buffer 102637ba54fSLudovic Desroches * can contain up to 1024 characters in PIO mode and up to 4096 characters in 103637ba54fSLudovic Desroches * DMA mode. 104637ba54fSLudovic Desroches */ 105ab4382d2SGreg Kroah-Hartman #define ATMEL_SERIAL_RINGSIZE 1024 106ab4382d2SGreg Kroah-Hartman 107ab4382d2SGreg Kroah-Hartman /* 1089af92fbfSAlexandre Belloni * at91: 6 USARTs and one DBGU port (SAM9260) 109432f9748SAlexandre Belloni * samx7: 3 USARTs and 5 UARTs 1109af92fbfSAlexandre Belloni */ 111432f9748SAlexandre Belloni #define ATMEL_MAX_UART 8 1129af92fbfSAlexandre Belloni 1139af92fbfSAlexandre Belloni /* 114ab4382d2SGreg Kroah-Hartman * We wrap our port structure around the generic uart_port. 115ab4382d2SGreg Kroah-Hartman */ 116ab4382d2SGreg Kroah-Hartman struct atmel_uart_port { 117ab4382d2SGreg Kroah-Hartman struct uart_port uart; /* uart */ 118ab4382d2SGreg Kroah-Hartman struct clk *clk; /* uart clock */ 119ab4382d2SGreg Kroah-Hartman int may_wakeup; /* cached value of device_may_wakeup for times we need to disable it */ 120ab4382d2SGreg Kroah-Hartman u32 backup_imr; /* IMR saved during suspend */ 121ab4382d2SGreg Kroah-Hartman int break_active; /* break being received */ 122ab4382d2SGreg Kroah-Hartman 12334df42f5SElen Song bool use_dma_rx; /* enable DMA receiver */ 12464e22ebeSElen Song bool use_pdc_rx; /* enable PDC receiver */ 125ab4382d2SGreg Kroah-Hartman short pdc_rx_idx; /* current PDC RX buffer */ 126ab4382d2SGreg Kroah-Hartman struct atmel_dma_buffer pdc_rx[2]; /* PDC receier */ 127ab4382d2SGreg Kroah-Hartman 12808f738beSElen Song bool use_dma_tx; /* enable DMA transmitter */ 12964e22ebeSElen Song bool use_pdc_tx; /* enable PDC transmitter */ 130ab4382d2SGreg Kroah-Hartman struct atmel_dma_buffer pdc_tx; /* PDC transmitter */ 131ab4382d2SGreg Kroah-Hartman 13208f738beSElen Song spinlock_t lock_tx; /* port lock */ 13334df42f5SElen Song spinlock_t lock_rx; /* port lock */ 13408f738beSElen Song struct dma_chan *chan_tx; 13534df42f5SElen Song struct dma_chan *chan_rx; 13608f738beSElen Song struct dma_async_tx_descriptor *desc_tx; 13734df42f5SElen Song struct dma_async_tx_descriptor *desc_rx; 13808f738beSElen Song dma_cookie_t cookie_tx; 13934df42f5SElen Song dma_cookie_t cookie_rx; 14008f738beSElen Song struct scatterlist sg_tx; 14134df42f5SElen Song struct scatterlist sg_rx; 14200e8e658SNicolas Ferre struct tasklet_struct tasklet_rx; 14300e8e658SNicolas Ferre struct tasklet_struct tasklet_tx; 14498f2082cSNicolas Ferre atomic_t tasklet_shutdown; 145ab4382d2SGreg Kroah-Hartman unsigned int irq_status_prev; 1465f258b3eSCyrille Pitchen unsigned int tx_len; 147ab4382d2SGreg Kroah-Hartman 148ab4382d2SGreg Kroah-Hartman struct circ_buf rx_ring; 149ab4382d2SGreg Kroah-Hartman 150e0b0baadSRichard Genoud struct mctrl_gpios *gpios; 151*377fedd1SNicolas Ferre u32 backup_mode; /* MR saved during iso7816 operations */ 152*377fedd1SNicolas Ferre u32 backup_brgr; /* BRGR saved during iso7816 operations */ 153ab4382d2SGreg Kroah-Hartman unsigned int tx_done_mask; 154b5199d46SCyrille Pitchen u32 fifo_size; 155b5199d46SCyrille Pitchen u32 rts_high; 156b5199d46SCyrille Pitchen u32 rts_low; 157ab5e4e41SRichard Genoud bool ms_irq_enabled; 1582958cceeSLudovic Desroches u32 rtor; /* address of receiver timeout register if it exists */ 1595bf5635aSLudovic Desroches bool has_frac_baudrate; 1604b769371SNicolas Ferre bool has_hw_timer; 1614b769371SNicolas Ferre struct timer_list uart_timer; 1622c7af5baSBoris BREZILLON 163ea04f82aSRomain Izard bool tx_stopped; 1642c7af5baSBoris BREZILLON bool suspended; 1652c7af5baSBoris BREZILLON unsigned int pending; 1662c7af5baSBoris BREZILLON unsigned int pending_status; 1672c7af5baSBoris BREZILLON spinlock_t lock_suspended; 1682c7af5baSBoris BREZILLON 169*377fedd1SNicolas Ferre /* ISO7816 */ 170*377fedd1SNicolas Ferre unsigned int fidi_min; 171*377fedd1SNicolas Ferre unsigned int fidi_max; 172*377fedd1SNicolas Ferre 173488ae82dSAlexandre Belloni #ifdef CONFIG_PM 1746a5f0e2fSAlexandre Belloni struct { 1756a5f0e2fSAlexandre Belloni u32 cr; 1766a5f0e2fSAlexandre Belloni u32 mr; 1776a5f0e2fSAlexandre Belloni u32 imr; 1786a5f0e2fSAlexandre Belloni u32 brgr; 1796a5f0e2fSAlexandre Belloni u32 rtor; 1806a5f0e2fSAlexandre Belloni u32 ttgr; 1816a5f0e2fSAlexandre Belloni u32 fmr; 1826a5f0e2fSAlexandre Belloni u32 fimr; 1836a5f0e2fSAlexandre Belloni } cache; 184488ae82dSAlexandre Belloni #endif 1856a5f0e2fSAlexandre Belloni 186a930e528SElen Song int (*prepare_rx)(struct uart_port *port); 187a930e528SElen Song int (*prepare_tx)(struct uart_port *port); 188a930e528SElen Song void (*schedule_rx)(struct uart_port *port); 189a930e528SElen Song void (*schedule_tx)(struct uart_port *port); 190a930e528SElen Song void (*release_rx)(struct uart_port *port); 191a930e528SElen Song void (*release_tx)(struct uart_port *port); 192ab4382d2SGreg Kroah-Hartman }; 193ab4382d2SGreg Kroah-Hartman 194ab4382d2SGreg Kroah-Hartman static struct atmel_uart_port atmel_ports[ATMEL_MAX_UART]; 195503bded9SPawel Wieczorkiewicz static DECLARE_BITMAP(atmel_ports_in_use, ATMEL_MAX_UART); 196ab4382d2SGreg Kroah-Hartman 197ab4382d2SGreg Kroah-Hartman #ifdef SUPPORT_SYSRQ 198ab4382d2SGreg Kroah-Hartman static struct console atmel_console; 199ab4382d2SGreg Kroah-Hartman #endif 200ab4382d2SGreg Kroah-Hartman 2015fbe46b6SNicolas Ferre #if defined(CONFIG_OF) 2025fbe46b6SNicolas Ferre static const struct of_device_id atmel_serial_dt_ids[] = { 203c24d2531SRadu Pirea { .compatible = "atmel,at91rm9200-usart-serial" }, 2045fbe46b6SNicolas Ferre { /* sentinel */ } 2055fbe46b6SNicolas Ferre }; 2065fbe46b6SNicolas Ferre #endif 2075fbe46b6SNicolas Ferre 208ab4382d2SGreg Kroah-Hartman static inline struct atmel_uart_port * 209ab4382d2SGreg Kroah-Hartman to_atmel_uart_port(struct uart_port *uart) 210ab4382d2SGreg Kroah-Hartman { 211ab4382d2SGreg Kroah-Hartman return container_of(uart, struct atmel_uart_port, uart); 212ab4382d2SGreg Kroah-Hartman } 213ab4382d2SGreg Kroah-Hartman 2144e7decdaSCyrille Pitchen static inline u32 atmel_uart_readl(struct uart_port *port, u32 reg) 2154e7decdaSCyrille Pitchen { 2164e7decdaSCyrille Pitchen return __raw_readl(port->membase + reg); 2174e7decdaSCyrille Pitchen } 2184e7decdaSCyrille Pitchen 2194e7decdaSCyrille Pitchen static inline void atmel_uart_writel(struct uart_port *port, u32 reg, u32 value) 2204e7decdaSCyrille Pitchen { 2214e7decdaSCyrille Pitchen __raw_writel(value, port->membase + reg); 2224e7decdaSCyrille Pitchen } 2234e7decdaSCyrille Pitchen 224a6499435SCyrille Pitchen static inline u8 atmel_uart_read_char(struct uart_port *port) 225a6499435SCyrille Pitchen { 226a6499435SCyrille Pitchen return __raw_readb(port->membase + ATMEL_US_RHR); 227a6499435SCyrille Pitchen } 228a6499435SCyrille Pitchen 229a6499435SCyrille Pitchen static inline void atmel_uart_write_char(struct uart_port *port, u8 value) 230a6499435SCyrille Pitchen { 231a6499435SCyrille Pitchen __raw_writeb(value, port->membase + ATMEL_US_THR); 232a6499435SCyrille Pitchen } 233a6499435SCyrille Pitchen 234ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_SERIAL_ATMEL_PDC 23564e22ebeSElen Song static bool atmel_use_pdc_rx(struct uart_port *port) 236ab4382d2SGreg Kroah-Hartman { 237ab4382d2SGreg Kroah-Hartman struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 238ab4382d2SGreg Kroah-Hartman 23964e22ebeSElen Song return atmel_port->use_pdc_rx; 240ab4382d2SGreg Kroah-Hartman } 241ab4382d2SGreg Kroah-Hartman 24264e22ebeSElen Song static bool atmel_use_pdc_tx(struct uart_port *port) 243ab4382d2SGreg Kroah-Hartman { 244ab4382d2SGreg Kroah-Hartman struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 245ab4382d2SGreg Kroah-Hartman 24664e22ebeSElen Song return atmel_port->use_pdc_tx; 247ab4382d2SGreg Kroah-Hartman } 248ab4382d2SGreg Kroah-Hartman #else 24964e22ebeSElen Song static bool atmel_use_pdc_rx(struct uart_port *port) 250ab4382d2SGreg Kroah-Hartman { 251ab4382d2SGreg Kroah-Hartman return false; 252ab4382d2SGreg Kroah-Hartman } 253ab4382d2SGreg Kroah-Hartman 25464e22ebeSElen Song static bool atmel_use_pdc_tx(struct uart_port *port) 255ab4382d2SGreg Kroah-Hartman { 256ab4382d2SGreg Kroah-Hartman return false; 257ab4382d2SGreg Kroah-Hartman } 258ab4382d2SGreg Kroah-Hartman #endif 259ab4382d2SGreg Kroah-Hartman 26008f738beSElen Song static bool atmel_use_dma_tx(struct uart_port *port) 26108f738beSElen Song { 26208f738beSElen Song struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 26308f738beSElen Song 26408f738beSElen Song return atmel_port->use_dma_tx; 26508f738beSElen Song } 26608f738beSElen Song 26734df42f5SElen Song static bool atmel_use_dma_rx(struct uart_port *port) 26834df42f5SElen Song { 26934df42f5SElen Song struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 27034df42f5SElen Song 27134df42f5SElen Song return atmel_port->use_dma_rx; 27234df42f5SElen Song } 27334df42f5SElen Song 2745be605acSAlexandre Belloni static bool atmel_use_fifo(struct uart_port *port) 2755be605acSAlexandre Belloni { 2765be605acSAlexandre Belloni struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 2775be605acSAlexandre Belloni 2785be605acSAlexandre Belloni return atmel_port->fifo_size; 2795be605acSAlexandre Belloni } 2805be605acSAlexandre Belloni 28198f2082cSNicolas Ferre static void atmel_tasklet_schedule(struct atmel_uart_port *atmel_port, 28298f2082cSNicolas Ferre struct tasklet_struct *t) 28398f2082cSNicolas Ferre { 28498f2082cSNicolas Ferre if (!atomic_read(&atmel_port->tasklet_shutdown)) 28598f2082cSNicolas Ferre tasklet_schedule(t); 28698f2082cSNicolas Ferre } 28798f2082cSNicolas Ferre 288e0b0baadSRichard Genoud static unsigned int atmel_get_lines_status(struct uart_port *port) 289e0b0baadSRichard Genoud { 290e0b0baadSRichard Genoud struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 291e0b0baadSRichard Genoud unsigned int status, ret = 0; 292e0b0baadSRichard Genoud 2934e7decdaSCyrille Pitchen status = atmel_uart_readl(port, ATMEL_US_CSR); 294e0b0baadSRichard Genoud 295e0b0baadSRichard Genoud mctrl_gpio_get(atmel_port->gpios, &ret); 296e0b0baadSRichard Genoud 297e0b0baadSRichard Genoud if (!IS_ERR_OR_NULL(mctrl_gpio_to_gpiod(atmel_port->gpios, 298e0b0baadSRichard Genoud UART_GPIO_CTS))) { 299e0b0baadSRichard Genoud if (ret & TIOCM_CTS) 300e0b0baadSRichard Genoud status &= ~ATMEL_US_CTS; 301e0b0baadSRichard Genoud else 302e0b0baadSRichard Genoud status |= ATMEL_US_CTS; 303e0b0baadSRichard Genoud } 304e0b0baadSRichard Genoud 305e0b0baadSRichard Genoud if (!IS_ERR_OR_NULL(mctrl_gpio_to_gpiod(atmel_port->gpios, 306e0b0baadSRichard Genoud UART_GPIO_DSR))) { 307e0b0baadSRichard Genoud if (ret & TIOCM_DSR) 308e0b0baadSRichard Genoud status &= ~ATMEL_US_DSR; 309e0b0baadSRichard Genoud else 310e0b0baadSRichard Genoud status |= ATMEL_US_DSR; 311e0b0baadSRichard Genoud } 312e0b0baadSRichard Genoud 313e0b0baadSRichard Genoud if (!IS_ERR_OR_NULL(mctrl_gpio_to_gpiod(atmel_port->gpios, 314e0b0baadSRichard Genoud UART_GPIO_RI))) { 315e0b0baadSRichard Genoud if (ret & TIOCM_RI) 316e0b0baadSRichard Genoud status &= ~ATMEL_US_RI; 317e0b0baadSRichard Genoud else 318e0b0baadSRichard Genoud status |= ATMEL_US_RI; 319e0b0baadSRichard Genoud } 320e0b0baadSRichard Genoud 321e0b0baadSRichard Genoud if (!IS_ERR_OR_NULL(mctrl_gpio_to_gpiod(atmel_port->gpios, 322e0b0baadSRichard Genoud UART_GPIO_DCD))) { 323e0b0baadSRichard Genoud if (ret & TIOCM_CD) 324e0b0baadSRichard Genoud status &= ~ATMEL_US_DCD; 325e0b0baadSRichard Genoud else 326e0b0baadSRichard Genoud status |= ATMEL_US_DCD; 327e0b0baadSRichard Genoud } 328e0b0baadSRichard Genoud 329e0b0baadSRichard Genoud return status; 330e0b0baadSRichard Genoud } 331e0b0baadSRichard Genoud 332ab4382d2SGreg Kroah-Hartman /* Enable or disable the rs485 support */ 33313bd3e6fSRicardo Ribalda Delgado static int atmel_config_rs485(struct uart_port *port, 33413bd3e6fSRicardo Ribalda Delgado struct serial_rs485 *rs485conf) 335ab4382d2SGreg Kroah-Hartman { 336ab4382d2SGreg Kroah-Hartman struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 337ab4382d2SGreg Kroah-Hartman unsigned int mode; 338ab4382d2SGreg Kroah-Hartman 339ab4382d2SGreg Kroah-Hartman /* Disable interrupts */ 3404e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_IDR, atmel_port->tx_done_mask); 341ab4382d2SGreg Kroah-Hartman 3424e7decdaSCyrille Pitchen mode = atmel_uart_readl(port, ATMEL_US_MR); 343ab4382d2SGreg Kroah-Hartman 344ab4382d2SGreg Kroah-Hartman /* Resetting serial mode to RS232 (0x0) */ 345ab4382d2SGreg Kroah-Hartman mode &= ~ATMEL_US_USMODE; 346ab4382d2SGreg Kroah-Hartman 34713bd3e6fSRicardo Ribalda Delgado port->rs485 = *rs485conf; 348ab4382d2SGreg Kroah-Hartman 349ab4382d2SGreg Kroah-Hartman if (rs485conf->flags & SER_RS485_ENABLED) { 350ab4382d2SGreg Kroah-Hartman dev_dbg(port->dev, "Setting UART to RS485\n"); 351ab4382d2SGreg Kroah-Hartman atmel_port->tx_done_mask = ATMEL_US_TXEMPTY; 3524e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_TTGR, 3534e7decdaSCyrille Pitchen rs485conf->delay_rts_after_send); 354ab4382d2SGreg Kroah-Hartman mode |= ATMEL_US_USMODE_RS485; 355ab4382d2SGreg Kroah-Hartman } else { 356ab4382d2SGreg Kroah-Hartman dev_dbg(port->dev, "Setting UART to RS232\n"); 35764e22ebeSElen Song if (atmel_use_pdc_tx(port)) 358ab4382d2SGreg Kroah-Hartman atmel_port->tx_done_mask = ATMEL_US_ENDTX | 359ab4382d2SGreg Kroah-Hartman ATMEL_US_TXBUFE; 360ab4382d2SGreg Kroah-Hartman else 361ab4382d2SGreg Kroah-Hartman atmel_port->tx_done_mask = ATMEL_US_TXRDY; 362ab4382d2SGreg Kroah-Hartman } 3634e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_MR, mode); 364ab4382d2SGreg Kroah-Hartman 365ab4382d2SGreg Kroah-Hartman /* Enable interrupts */ 3664e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_IER, atmel_port->tx_done_mask); 367ab4382d2SGreg Kroah-Hartman 36813bd3e6fSRicardo Ribalda Delgado return 0; 369ab4382d2SGreg Kroah-Hartman } 370ab4382d2SGreg Kroah-Hartman 371*377fedd1SNicolas Ferre static unsigned int atmel_calc_cd(struct uart_port *port, 372*377fedd1SNicolas Ferre struct serial_iso7816 *iso7816conf) 373*377fedd1SNicolas Ferre { 374*377fedd1SNicolas Ferre struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 375*377fedd1SNicolas Ferre unsigned int cd; 376*377fedd1SNicolas Ferre u64 mck_rate; 377*377fedd1SNicolas Ferre 378*377fedd1SNicolas Ferre mck_rate = (u64)clk_get_rate(atmel_port->clk); 379*377fedd1SNicolas Ferre do_div(mck_rate, iso7816conf->clk); 380*377fedd1SNicolas Ferre cd = mck_rate; 381*377fedd1SNicolas Ferre return cd; 382*377fedd1SNicolas Ferre } 383*377fedd1SNicolas Ferre 384*377fedd1SNicolas Ferre static unsigned int atmel_calc_fidi(struct uart_port *port, 385*377fedd1SNicolas Ferre struct serial_iso7816 *iso7816conf) 386*377fedd1SNicolas Ferre { 387*377fedd1SNicolas Ferre u64 fidi = 0; 388*377fedd1SNicolas Ferre 389*377fedd1SNicolas Ferre if (iso7816conf->sc_fi && iso7816conf->sc_di) { 390*377fedd1SNicolas Ferre fidi = (u64)iso7816conf->sc_fi; 391*377fedd1SNicolas Ferre do_div(fidi, iso7816conf->sc_di); 392*377fedd1SNicolas Ferre } 393*377fedd1SNicolas Ferre return (u32)fidi; 394*377fedd1SNicolas Ferre } 395*377fedd1SNicolas Ferre 396*377fedd1SNicolas Ferre /* Enable or disable the iso7816 support */ 397*377fedd1SNicolas Ferre /* Called with interrupts disabled */ 398*377fedd1SNicolas Ferre static int atmel_config_iso7816(struct uart_port *port, 399*377fedd1SNicolas Ferre struct serial_iso7816 *iso7816conf) 400*377fedd1SNicolas Ferre { 401*377fedd1SNicolas Ferre struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 402*377fedd1SNicolas Ferre unsigned int mode; 403*377fedd1SNicolas Ferre unsigned int cd, fidi; 404*377fedd1SNicolas Ferre int ret = 0; 405*377fedd1SNicolas Ferre 406*377fedd1SNicolas Ferre /* Disable interrupts */ 407*377fedd1SNicolas Ferre atmel_uart_writel(port, ATMEL_US_IDR, atmel_port->tx_done_mask); 408*377fedd1SNicolas Ferre 409*377fedd1SNicolas Ferre mode = atmel_uart_readl(port, ATMEL_US_MR); 410*377fedd1SNicolas Ferre 411*377fedd1SNicolas Ferre if (iso7816conf->flags & SER_ISO7816_ENABLED) { 412*377fedd1SNicolas Ferre mode &= ~ATMEL_US_USMODE; 413*377fedd1SNicolas Ferre 414*377fedd1SNicolas Ferre if (iso7816conf->tg > 255) { 415*377fedd1SNicolas Ferre dev_err(port->dev, "ISO7816: Timeguard exceeding 255\n"); 416*377fedd1SNicolas Ferre memset(iso7816conf, 0, sizeof(struct serial_iso7816)); 417*377fedd1SNicolas Ferre ret = -EINVAL; 418*377fedd1SNicolas Ferre goto err_out; 419*377fedd1SNicolas Ferre } 420*377fedd1SNicolas Ferre 421*377fedd1SNicolas Ferre if ((iso7816conf->flags & SER_ISO7816_T_PARAM) 422*377fedd1SNicolas Ferre == SER_ISO7816_T(0)) { 423*377fedd1SNicolas Ferre mode |= ATMEL_US_USMODE_ISO7816_T0 | ATMEL_US_DSNACK; 424*377fedd1SNicolas Ferre } else if ((iso7816conf->flags & SER_ISO7816_T_PARAM) 425*377fedd1SNicolas Ferre == SER_ISO7816_T(1)) { 426*377fedd1SNicolas Ferre mode |= ATMEL_US_USMODE_ISO7816_T1 | ATMEL_US_INACK; 427*377fedd1SNicolas Ferre } else { 428*377fedd1SNicolas Ferre dev_err(port->dev, "ISO7816: Type not supported\n"); 429*377fedd1SNicolas Ferre memset(iso7816conf, 0, sizeof(struct serial_iso7816)); 430*377fedd1SNicolas Ferre ret = -EINVAL; 431*377fedd1SNicolas Ferre goto err_out; 432*377fedd1SNicolas Ferre } 433*377fedd1SNicolas Ferre 434*377fedd1SNicolas Ferre mode &= ~(ATMEL_US_USCLKS | ATMEL_US_NBSTOP | ATMEL_US_PAR); 435*377fedd1SNicolas Ferre 436*377fedd1SNicolas Ferre /* select mck clock, and output */ 437*377fedd1SNicolas Ferre mode |= ATMEL_US_USCLKS_MCK | ATMEL_US_CLKO; 438*377fedd1SNicolas Ferre /* set parity for normal/inverse mode + max iterations */ 439*377fedd1SNicolas Ferre mode |= ATMEL_US_PAR_EVEN | ATMEL_US_NBSTOP_1 | ATMEL_US_MAX_ITER(3); 440*377fedd1SNicolas Ferre 441*377fedd1SNicolas Ferre cd = atmel_calc_cd(port, iso7816conf); 442*377fedd1SNicolas Ferre fidi = atmel_calc_fidi(port, iso7816conf); 443*377fedd1SNicolas Ferre if (fidi == 0) { 444*377fedd1SNicolas Ferre dev_warn(port->dev, "ISO7816 fidi = 0, Generator generates no signal\n"); 445*377fedd1SNicolas Ferre } else if (fidi < atmel_port->fidi_min 446*377fedd1SNicolas Ferre || fidi > atmel_port->fidi_max) { 447*377fedd1SNicolas Ferre dev_err(port->dev, "ISO7816 fidi = %u, value not supported\n", fidi); 448*377fedd1SNicolas Ferre memset(iso7816conf, 0, sizeof(struct serial_iso7816)); 449*377fedd1SNicolas Ferre ret = -EINVAL; 450*377fedd1SNicolas Ferre goto err_out; 451*377fedd1SNicolas Ferre } 452*377fedd1SNicolas Ferre 453*377fedd1SNicolas Ferre if (!(port->iso7816.flags & SER_ISO7816_ENABLED)) { 454*377fedd1SNicolas Ferre /* port not yet in iso7816 mode: store configuration */ 455*377fedd1SNicolas Ferre atmel_port->backup_mode = atmel_uart_readl(port, ATMEL_US_MR); 456*377fedd1SNicolas Ferre atmel_port->backup_brgr = atmel_uart_readl(port, ATMEL_US_BRGR); 457*377fedd1SNicolas Ferre } 458*377fedd1SNicolas Ferre 459*377fedd1SNicolas Ferre atmel_uart_writel(port, ATMEL_US_TTGR, iso7816conf->tg); 460*377fedd1SNicolas Ferre atmel_uart_writel(port, ATMEL_US_BRGR, cd); 461*377fedd1SNicolas Ferre atmel_uart_writel(port, ATMEL_US_FIDI, fidi); 462*377fedd1SNicolas Ferre 463*377fedd1SNicolas Ferre atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_TXDIS | ATMEL_US_RXEN); 464*377fedd1SNicolas Ferre atmel_port->tx_done_mask = ATMEL_US_TXEMPTY | ATMEL_US_NACK | ATMEL_US_ITERATION; 465*377fedd1SNicolas Ferre } else { 466*377fedd1SNicolas Ferre dev_dbg(port->dev, "Setting UART back to RS232\n"); 467*377fedd1SNicolas Ferre /* back to last RS232 settings */ 468*377fedd1SNicolas Ferre mode = atmel_port->backup_mode; 469*377fedd1SNicolas Ferre memset(iso7816conf, 0, sizeof(struct serial_iso7816)); 470*377fedd1SNicolas Ferre atmel_uart_writel(port, ATMEL_US_TTGR, 0); 471*377fedd1SNicolas Ferre atmel_uart_writel(port, ATMEL_US_BRGR, atmel_port->backup_brgr); 472*377fedd1SNicolas Ferre atmel_uart_writel(port, ATMEL_US_FIDI, 0x174); 473*377fedd1SNicolas Ferre 474*377fedd1SNicolas Ferre if (atmel_use_pdc_tx(port)) 475*377fedd1SNicolas Ferre atmel_port->tx_done_mask = ATMEL_US_ENDTX | 476*377fedd1SNicolas Ferre ATMEL_US_TXBUFE; 477*377fedd1SNicolas Ferre else 478*377fedd1SNicolas Ferre atmel_port->tx_done_mask = ATMEL_US_TXRDY; 479*377fedd1SNicolas Ferre } 480*377fedd1SNicolas Ferre 481*377fedd1SNicolas Ferre port->iso7816 = *iso7816conf; 482*377fedd1SNicolas Ferre 483*377fedd1SNicolas Ferre atmel_uart_writel(port, ATMEL_US_MR, mode); 484*377fedd1SNicolas Ferre 485*377fedd1SNicolas Ferre err_out: 486*377fedd1SNicolas Ferre /* Enable interrupts */ 487*377fedd1SNicolas Ferre atmel_uart_writel(port, ATMEL_US_IER, atmel_port->tx_done_mask); 488*377fedd1SNicolas Ferre 489*377fedd1SNicolas Ferre return ret; 490*377fedd1SNicolas Ferre } 491*377fedd1SNicolas Ferre 492ab4382d2SGreg Kroah-Hartman /* 493ab4382d2SGreg Kroah-Hartman * Return TIOCSER_TEMT when transmitter FIFO and Shift register is empty. 494ab4382d2SGreg Kroah-Hartman */ 495ab4382d2SGreg Kroah-Hartman static u_int atmel_tx_empty(struct uart_port *port) 496ab4382d2SGreg Kroah-Hartman { 497ea04f82aSRomain Izard struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 498ea04f82aSRomain Izard 499ea04f82aSRomain Izard if (atmel_port->tx_stopped) 500ea04f82aSRomain Izard return TIOCSER_TEMT; 5014e7decdaSCyrille Pitchen return (atmel_uart_readl(port, ATMEL_US_CSR) & ATMEL_US_TXEMPTY) ? 5024e7decdaSCyrille Pitchen TIOCSER_TEMT : 5034e7decdaSCyrille Pitchen 0; 504ab4382d2SGreg Kroah-Hartman } 505ab4382d2SGreg Kroah-Hartman 506ab4382d2SGreg Kroah-Hartman /* 507ab4382d2SGreg Kroah-Hartman * Set state of the modem control output lines 508ab4382d2SGreg Kroah-Hartman */ 509ab4382d2SGreg Kroah-Hartman static void atmel_set_mctrl(struct uart_port *port, u_int mctrl) 510ab4382d2SGreg Kroah-Hartman { 511ab4382d2SGreg Kroah-Hartman unsigned int control = 0; 5124e7decdaSCyrille Pitchen unsigned int mode = atmel_uart_readl(port, ATMEL_US_MR); 5131cf6e8fcSCyrille Pitchen unsigned int rts_paused, rts_ready; 514ab4382d2SGreg Kroah-Hartman struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 515ab4382d2SGreg Kroah-Hartman 5161cf6e8fcSCyrille Pitchen /* override mode to RS485 if needed, otherwise keep the current mode */ 5171cf6e8fcSCyrille Pitchen if (port->rs485.flags & SER_RS485_ENABLED) { 5184e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_TTGR, 5194e7decdaSCyrille Pitchen port->rs485.delay_rts_after_send); 5201cf6e8fcSCyrille Pitchen mode &= ~ATMEL_US_USMODE; 5211cf6e8fcSCyrille Pitchen mode |= ATMEL_US_USMODE_RS485; 5221cf6e8fcSCyrille Pitchen } 5231cf6e8fcSCyrille Pitchen 5241cf6e8fcSCyrille Pitchen /* set the RTS line state according to the mode */ 5251cf6e8fcSCyrille Pitchen if ((mode & ATMEL_US_USMODE) == ATMEL_US_USMODE_HWHS) { 5261cf6e8fcSCyrille Pitchen /* force RTS line to high level */ 5271cf6e8fcSCyrille Pitchen rts_paused = ATMEL_US_RTSEN; 5281cf6e8fcSCyrille Pitchen 5291cf6e8fcSCyrille Pitchen /* give the control of the RTS line back to the hardware */ 5301cf6e8fcSCyrille Pitchen rts_ready = ATMEL_US_RTSDIS; 5311cf6e8fcSCyrille Pitchen } else { 5321cf6e8fcSCyrille Pitchen /* force RTS line to high level */ 5331cf6e8fcSCyrille Pitchen rts_paused = ATMEL_US_RTSDIS; 5341cf6e8fcSCyrille Pitchen 5351cf6e8fcSCyrille Pitchen /* force RTS line to low level */ 5361cf6e8fcSCyrille Pitchen rts_ready = ATMEL_US_RTSEN; 5371cf6e8fcSCyrille Pitchen } 5381cf6e8fcSCyrille Pitchen 539ab4382d2SGreg Kroah-Hartman if (mctrl & TIOCM_RTS) 5401cf6e8fcSCyrille Pitchen control |= rts_ready; 541ab4382d2SGreg Kroah-Hartman else 5421cf6e8fcSCyrille Pitchen control |= rts_paused; 543ab4382d2SGreg Kroah-Hartman 544ab4382d2SGreg Kroah-Hartman if (mctrl & TIOCM_DTR) 545ab4382d2SGreg Kroah-Hartman control |= ATMEL_US_DTREN; 546ab4382d2SGreg Kroah-Hartman else 547ab4382d2SGreg Kroah-Hartman control |= ATMEL_US_DTRDIS; 548ab4382d2SGreg Kroah-Hartman 5494e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_CR, control); 550ab4382d2SGreg Kroah-Hartman 551e0b0baadSRichard Genoud mctrl_gpio_set(atmel_port->gpios, mctrl); 552e0b0baadSRichard Genoud 553ab4382d2SGreg Kroah-Hartman /* Local loopback mode? */ 5541cf6e8fcSCyrille Pitchen mode &= ~ATMEL_US_CHMODE; 555ab4382d2SGreg Kroah-Hartman if (mctrl & TIOCM_LOOP) 556ab4382d2SGreg Kroah-Hartman mode |= ATMEL_US_CHMODE_LOC_LOOP; 557ab4382d2SGreg Kroah-Hartman else 558ab4382d2SGreg Kroah-Hartman mode |= ATMEL_US_CHMODE_NORMAL; 559ab4382d2SGreg Kroah-Hartman 5604e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_MR, mode); 561ab4382d2SGreg Kroah-Hartman } 562ab4382d2SGreg Kroah-Hartman 563ab4382d2SGreg Kroah-Hartman /* 564ab4382d2SGreg Kroah-Hartman * Get state of the modem control input lines 565ab4382d2SGreg Kroah-Hartman */ 566ab4382d2SGreg Kroah-Hartman static u_int atmel_get_mctrl(struct uart_port *port) 567ab4382d2SGreg Kroah-Hartman { 568e0b0baadSRichard Genoud struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 569e0b0baadSRichard Genoud unsigned int ret = 0, status; 570ab4382d2SGreg Kroah-Hartman 5714e7decdaSCyrille Pitchen status = atmel_uart_readl(port, ATMEL_US_CSR); 572ab4382d2SGreg Kroah-Hartman 573ab4382d2SGreg Kroah-Hartman /* 574ab4382d2SGreg Kroah-Hartman * The control signals are active low. 575ab4382d2SGreg Kroah-Hartman */ 576ab4382d2SGreg Kroah-Hartman if (!(status & ATMEL_US_DCD)) 577ab4382d2SGreg Kroah-Hartman ret |= TIOCM_CD; 578ab4382d2SGreg Kroah-Hartman if (!(status & ATMEL_US_CTS)) 579ab4382d2SGreg Kroah-Hartman ret |= TIOCM_CTS; 580ab4382d2SGreg Kroah-Hartman if (!(status & ATMEL_US_DSR)) 581ab4382d2SGreg Kroah-Hartman ret |= TIOCM_DSR; 582ab4382d2SGreg Kroah-Hartman if (!(status & ATMEL_US_RI)) 583ab4382d2SGreg Kroah-Hartman ret |= TIOCM_RI; 584ab4382d2SGreg Kroah-Hartman 585e0b0baadSRichard Genoud return mctrl_gpio_get(atmel_port->gpios, &ret); 586ab4382d2SGreg Kroah-Hartman } 587ab4382d2SGreg Kroah-Hartman 588ab4382d2SGreg Kroah-Hartman /* 589ab4382d2SGreg Kroah-Hartman * Stop transmitting. 590ab4382d2SGreg Kroah-Hartman */ 591ab4382d2SGreg Kroah-Hartman static void atmel_stop_tx(struct uart_port *port) 592ab4382d2SGreg Kroah-Hartman { 593ab4382d2SGreg Kroah-Hartman struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 594ab4382d2SGreg Kroah-Hartman 59564e22ebeSElen Song if (atmel_use_pdc_tx(port)) { 596ab4382d2SGreg Kroah-Hartman /* disable PDC transmit */ 5974e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_TXTDIS); 598ab4382d2SGreg Kroah-Hartman } 59989d82324SRichard Genoud 60089d82324SRichard Genoud /* 60189d82324SRichard Genoud * Disable the transmitter. 60289d82324SRichard Genoud * This is mandatory when DMA is used, otherwise the DMA buffer 60389d82324SRichard Genoud * is fully transmitted. 60489d82324SRichard Genoud */ 60589d82324SRichard Genoud atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_TXDIS); 606ea04f82aSRomain Izard atmel_port->tx_stopped = true; 60789d82324SRichard Genoud 608ab4382d2SGreg Kroah-Hartman /* Disable interrupts */ 6094e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_IDR, atmel_port->tx_done_mask); 610ab4382d2SGreg Kroah-Hartman 611*377fedd1SNicolas Ferre if (((port->rs485.flags & SER_RS485_ENABLED) && 612*377fedd1SNicolas Ferre !(port->rs485.flags & SER_RS485_RX_DURING_TX)) || 613*377fedd1SNicolas Ferre port->iso7816.flags & SER_ISO7816_ENABLED) 614ab4382d2SGreg Kroah-Hartman atmel_start_rx(port); 615ab4382d2SGreg Kroah-Hartman } 616ab4382d2SGreg Kroah-Hartman 617ab4382d2SGreg Kroah-Hartman /* 618ab4382d2SGreg Kroah-Hartman * Start transmitting. 619ab4382d2SGreg Kroah-Hartman */ 620ab4382d2SGreg Kroah-Hartman static void atmel_start_tx(struct uart_port *port) 621ab4382d2SGreg Kroah-Hartman { 622ab4382d2SGreg Kroah-Hartman struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 623ab4382d2SGreg Kroah-Hartman 6240058f087SAlexandre Belloni if (atmel_use_pdc_tx(port) && (atmel_uart_readl(port, ATMEL_PDC_PTSR) 6250058f087SAlexandre Belloni & ATMEL_PDC_TXTEN)) 626ab4382d2SGreg Kroah-Hartman /* The transmitter is already running. Yes, we 627ab4382d2SGreg Kroah-Hartman really need this.*/ 628ab4382d2SGreg Kroah-Hartman return; 629ab4382d2SGreg Kroah-Hartman 6300058f087SAlexandre Belloni if (atmel_use_pdc_tx(port) || atmel_use_dma_tx(port)) 631*377fedd1SNicolas Ferre if (((port->rs485.flags & SER_RS485_ENABLED) && 632*377fedd1SNicolas Ferre !(port->rs485.flags & SER_RS485_RX_DURING_TX)) || 633*377fedd1SNicolas Ferre port->iso7816.flags & SER_ISO7816_ENABLED) 634ab4382d2SGreg Kroah-Hartman atmel_stop_rx(port); 635ab4382d2SGreg Kroah-Hartman 6360058f087SAlexandre Belloni if (atmel_use_pdc_tx(port)) 637ab4382d2SGreg Kroah-Hartman /* re-enable PDC transmit */ 6384e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_TXTEN); 6390058f087SAlexandre Belloni 640ab4382d2SGreg Kroah-Hartman /* Enable interrupts */ 6414e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_IER, atmel_port->tx_done_mask); 64289d82324SRichard Genoud 64389d82324SRichard Genoud /* re-enable the transmitter */ 64489d82324SRichard Genoud atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_TXEN); 645ea04f82aSRomain Izard atmel_port->tx_stopped = false; 646ab4382d2SGreg Kroah-Hartman } 647ab4382d2SGreg Kroah-Hartman 648ab4382d2SGreg Kroah-Hartman /* 649ab4382d2SGreg Kroah-Hartman * start receiving - port is in process of being opened. 650ab4382d2SGreg Kroah-Hartman */ 651ab4382d2SGreg Kroah-Hartman static void atmel_start_rx(struct uart_port *port) 652ab4382d2SGreg Kroah-Hartman { 6534e7decdaSCyrille Pitchen /* reset status and receiver */ 6544e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA); 655ab4382d2SGreg Kroah-Hartman 6564e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RXEN); 65757c36868SSiftar, Gabe 65864e22ebeSElen Song if (atmel_use_pdc_rx(port)) { 659ab4382d2SGreg Kroah-Hartman /* enable PDC controller */ 6604e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_IER, 6614e7decdaSCyrille Pitchen ATMEL_US_ENDRX | ATMEL_US_TIMEOUT | 662ab4382d2SGreg Kroah-Hartman port->read_status_mask); 6634e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_RXTEN); 664ab4382d2SGreg Kroah-Hartman } else { 6654e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_IER, ATMEL_US_RXRDY); 666ab4382d2SGreg Kroah-Hartman } 667ab4382d2SGreg Kroah-Hartman } 668ab4382d2SGreg Kroah-Hartman 669ab4382d2SGreg Kroah-Hartman /* 670ab4382d2SGreg Kroah-Hartman * Stop receiving - port is in process of being closed. 671ab4382d2SGreg Kroah-Hartman */ 672ab4382d2SGreg Kroah-Hartman static void atmel_stop_rx(struct uart_port *port) 673ab4382d2SGreg Kroah-Hartman { 6744e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RXDIS); 67557c36868SSiftar, Gabe 67664e22ebeSElen Song if (atmel_use_pdc_rx(port)) { 677ab4382d2SGreg Kroah-Hartman /* disable PDC receive */ 6784e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_RXTDIS); 6794e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_IDR, 6804e7decdaSCyrille Pitchen ATMEL_US_ENDRX | ATMEL_US_TIMEOUT | 681ab4382d2SGreg Kroah-Hartman port->read_status_mask); 682ab4382d2SGreg Kroah-Hartman } else { 6834e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_IDR, ATMEL_US_RXRDY); 684ab4382d2SGreg Kroah-Hartman } 685ab4382d2SGreg Kroah-Hartman } 686ab4382d2SGreg Kroah-Hartman 687ab4382d2SGreg Kroah-Hartman /* 688ab4382d2SGreg Kroah-Hartman * Enable modem status interrupts 689ab4382d2SGreg Kroah-Hartman */ 690ab4382d2SGreg Kroah-Hartman static void atmel_enable_ms(struct uart_port *port) 691ab4382d2SGreg Kroah-Hartman { 692ab5e4e41SRichard Genoud struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 693ab5e4e41SRichard Genoud uint32_t ier = 0; 694ab5e4e41SRichard Genoud 695ab5e4e41SRichard Genoud /* 696ab5e4e41SRichard Genoud * Interrupt should not be enabled twice 697ab5e4e41SRichard Genoud */ 698ab5e4e41SRichard Genoud if (atmel_port->ms_irq_enabled) 699ab5e4e41SRichard Genoud return; 700ab5e4e41SRichard Genoud 701ab5e4e41SRichard Genoud atmel_port->ms_irq_enabled = true; 702ab5e4e41SRichard Genoud 70318dfef9cSUwe Kleine-König if (!mctrl_gpio_to_gpiod(atmel_port->gpios, UART_GPIO_CTS)) 704ab5e4e41SRichard Genoud ier |= ATMEL_US_CTSIC; 705ab5e4e41SRichard Genoud 70618dfef9cSUwe Kleine-König if (!mctrl_gpio_to_gpiod(atmel_port->gpios, UART_GPIO_DSR)) 707ab5e4e41SRichard Genoud ier |= ATMEL_US_DSRIC; 708ab5e4e41SRichard Genoud 70918dfef9cSUwe Kleine-König if (!mctrl_gpio_to_gpiod(atmel_port->gpios, UART_GPIO_RI)) 710ab5e4e41SRichard Genoud ier |= ATMEL_US_RIIC; 711ab5e4e41SRichard Genoud 71218dfef9cSUwe Kleine-König if (!mctrl_gpio_to_gpiod(atmel_port->gpios, UART_GPIO_DCD)) 713ab5e4e41SRichard Genoud ier |= ATMEL_US_DCDIC; 714ab5e4e41SRichard Genoud 7154e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_IER, ier); 71618dfef9cSUwe Kleine-König 71718dfef9cSUwe Kleine-König mctrl_gpio_enable_ms(atmel_port->gpios); 718ab4382d2SGreg Kroah-Hartman } 719ab4382d2SGreg Kroah-Hartman 720ab4382d2SGreg Kroah-Hartman /* 72135b675b9SRichard Genoud * Disable modem status interrupts 72235b675b9SRichard Genoud */ 72335b675b9SRichard Genoud static void atmel_disable_ms(struct uart_port *port) 72435b675b9SRichard Genoud { 72535b675b9SRichard Genoud struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 72635b675b9SRichard Genoud uint32_t idr = 0; 72735b675b9SRichard Genoud 72835b675b9SRichard Genoud /* 72935b675b9SRichard Genoud * Interrupt should not be disabled twice 73035b675b9SRichard Genoud */ 73135b675b9SRichard Genoud if (!atmel_port->ms_irq_enabled) 73235b675b9SRichard Genoud return; 73335b675b9SRichard Genoud 73435b675b9SRichard Genoud atmel_port->ms_irq_enabled = false; 73535b675b9SRichard Genoud 73618dfef9cSUwe Kleine-König mctrl_gpio_disable_ms(atmel_port->gpios); 73718dfef9cSUwe Kleine-König 73818dfef9cSUwe Kleine-König if (!mctrl_gpio_to_gpiod(atmel_port->gpios, UART_GPIO_CTS)) 73935b675b9SRichard Genoud idr |= ATMEL_US_CTSIC; 74035b675b9SRichard Genoud 74118dfef9cSUwe Kleine-König if (!mctrl_gpio_to_gpiod(atmel_port->gpios, UART_GPIO_DSR)) 74235b675b9SRichard Genoud idr |= ATMEL_US_DSRIC; 74335b675b9SRichard Genoud 74418dfef9cSUwe Kleine-König if (!mctrl_gpio_to_gpiod(atmel_port->gpios, UART_GPIO_RI)) 74535b675b9SRichard Genoud idr |= ATMEL_US_RIIC; 74635b675b9SRichard Genoud 74718dfef9cSUwe Kleine-König if (!mctrl_gpio_to_gpiod(atmel_port->gpios, UART_GPIO_DCD)) 74835b675b9SRichard Genoud idr |= ATMEL_US_DCDIC; 74935b675b9SRichard Genoud 7504e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_IDR, idr); 75135b675b9SRichard Genoud } 75235b675b9SRichard Genoud 75335b675b9SRichard Genoud /* 754ab4382d2SGreg Kroah-Hartman * Control the transmission of a break signal 755ab4382d2SGreg Kroah-Hartman */ 756ab4382d2SGreg Kroah-Hartman static void atmel_break_ctl(struct uart_port *port, int break_state) 757ab4382d2SGreg Kroah-Hartman { 758ab4382d2SGreg Kroah-Hartman if (break_state != 0) 7594e7decdaSCyrille Pitchen /* start break */ 7604e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_STTBRK); 761ab4382d2SGreg Kroah-Hartman else 7624e7decdaSCyrille Pitchen /* stop break */ 7634e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_STPBRK); 764ab4382d2SGreg Kroah-Hartman } 765ab4382d2SGreg Kroah-Hartman 766ab4382d2SGreg Kroah-Hartman /* 767ab4382d2SGreg Kroah-Hartman * Stores the incoming character in the ring buffer 768ab4382d2SGreg Kroah-Hartman */ 769ab4382d2SGreg Kroah-Hartman static void 770ab4382d2SGreg Kroah-Hartman atmel_buffer_rx_char(struct uart_port *port, unsigned int status, 771ab4382d2SGreg Kroah-Hartman unsigned int ch) 772ab4382d2SGreg Kroah-Hartman { 773ab4382d2SGreg Kroah-Hartman struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 774ab4382d2SGreg Kroah-Hartman struct circ_buf *ring = &atmel_port->rx_ring; 775ab4382d2SGreg Kroah-Hartman struct atmel_uart_char *c; 776ab4382d2SGreg Kroah-Hartman 777ab4382d2SGreg Kroah-Hartman if (!CIRC_SPACE(ring->head, ring->tail, ATMEL_SERIAL_RINGSIZE)) 778ab4382d2SGreg Kroah-Hartman /* Buffer overflow, ignore char */ 779ab4382d2SGreg Kroah-Hartman return; 780ab4382d2SGreg Kroah-Hartman 781ab4382d2SGreg Kroah-Hartman c = &((struct atmel_uart_char *)ring->buf)[ring->head]; 782ab4382d2SGreg Kroah-Hartman c->status = status; 783ab4382d2SGreg Kroah-Hartman c->ch = ch; 784ab4382d2SGreg Kroah-Hartman 785ab4382d2SGreg Kroah-Hartman /* Make sure the character is stored before we update head. */ 786ab4382d2SGreg Kroah-Hartman smp_wmb(); 787ab4382d2SGreg Kroah-Hartman 788ab4382d2SGreg Kroah-Hartman ring->head = (ring->head + 1) & (ATMEL_SERIAL_RINGSIZE - 1); 789ab4382d2SGreg Kroah-Hartman } 790ab4382d2SGreg Kroah-Hartman 791ab4382d2SGreg Kroah-Hartman /* 792ab4382d2SGreg Kroah-Hartman * Deal with parity, framing and overrun errors. 793ab4382d2SGreg Kroah-Hartman */ 794ab4382d2SGreg Kroah-Hartman static void atmel_pdc_rxerr(struct uart_port *port, unsigned int status) 795ab4382d2SGreg Kroah-Hartman { 796ab4382d2SGreg Kroah-Hartman /* clear error */ 7974e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA); 798ab4382d2SGreg Kroah-Hartman 799ab4382d2SGreg Kroah-Hartman if (status & ATMEL_US_RXBRK) { 800ab4382d2SGreg Kroah-Hartman /* ignore side-effect */ 801ab4382d2SGreg Kroah-Hartman status &= ~(ATMEL_US_PARE | ATMEL_US_FRAME); 802ab4382d2SGreg Kroah-Hartman port->icount.brk++; 803ab4382d2SGreg Kroah-Hartman } 804ab4382d2SGreg Kroah-Hartman if (status & ATMEL_US_PARE) 805ab4382d2SGreg Kroah-Hartman port->icount.parity++; 806ab4382d2SGreg Kroah-Hartman if (status & ATMEL_US_FRAME) 807ab4382d2SGreg Kroah-Hartman port->icount.frame++; 808ab4382d2SGreg Kroah-Hartman if (status & ATMEL_US_OVRE) 809ab4382d2SGreg Kroah-Hartman port->icount.overrun++; 810ab4382d2SGreg Kroah-Hartman } 811ab4382d2SGreg Kroah-Hartman 812ab4382d2SGreg Kroah-Hartman /* 813ab4382d2SGreg Kroah-Hartman * Characters received (called from interrupt handler) 814ab4382d2SGreg Kroah-Hartman */ 815ab4382d2SGreg Kroah-Hartman static void atmel_rx_chars(struct uart_port *port) 816ab4382d2SGreg Kroah-Hartman { 817ab4382d2SGreg Kroah-Hartman struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 818ab4382d2SGreg Kroah-Hartman unsigned int status, ch; 819ab4382d2SGreg Kroah-Hartman 8204e7decdaSCyrille Pitchen status = atmel_uart_readl(port, ATMEL_US_CSR); 821ab4382d2SGreg Kroah-Hartman while (status & ATMEL_US_RXRDY) { 822a6499435SCyrille Pitchen ch = atmel_uart_read_char(port); 823ab4382d2SGreg Kroah-Hartman 824ab4382d2SGreg Kroah-Hartman /* 825ab4382d2SGreg Kroah-Hartman * note that the error handling code is 826ab4382d2SGreg Kroah-Hartman * out of the main execution path 827ab4382d2SGreg Kroah-Hartman */ 828ab4382d2SGreg Kroah-Hartman if (unlikely(status & (ATMEL_US_PARE | ATMEL_US_FRAME 829ab4382d2SGreg Kroah-Hartman | ATMEL_US_OVRE | ATMEL_US_RXBRK) 830ab4382d2SGreg Kroah-Hartman || atmel_port->break_active)) { 831ab4382d2SGreg Kroah-Hartman 832ab4382d2SGreg Kroah-Hartman /* clear error */ 8334e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA); 834ab4382d2SGreg Kroah-Hartman 835ab4382d2SGreg Kroah-Hartman if (status & ATMEL_US_RXBRK 836ab4382d2SGreg Kroah-Hartman && !atmel_port->break_active) { 837ab4382d2SGreg Kroah-Hartman atmel_port->break_active = 1; 8384e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_IER, 8394e7decdaSCyrille Pitchen ATMEL_US_RXBRK); 840ab4382d2SGreg Kroah-Hartman } else { 841ab4382d2SGreg Kroah-Hartman /* 842ab4382d2SGreg Kroah-Hartman * This is either the end-of-break 843ab4382d2SGreg Kroah-Hartman * condition or we've received at 844ab4382d2SGreg Kroah-Hartman * least one character without RXBRK 845ab4382d2SGreg Kroah-Hartman * being set. In both cases, the next 846ab4382d2SGreg Kroah-Hartman * RXBRK will indicate start-of-break. 847ab4382d2SGreg Kroah-Hartman */ 8484e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_IDR, 8494e7decdaSCyrille Pitchen ATMEL_US_RXBRK); 850ab4382d2SGreg Kroah-Hartman status &= ~ATMEL_US_RXBRK; 851ab4382d2SGreg Kroah-Hartman atmel_port->break_active = 0; 852ab4382d2SGreg Kroah-Hartman } 853ab4382d2SGreg Kroah-Hartman } 854ab4382d2SGreg Kroah-Hartman 855ab4382d2SGreg Kroah-Hartman atmel_buffer_rx_char(port, status, ch); 8564e7decdaSCyrille Pitchen status = atmel_uart_readl(port, ATMEL_US_CSR); 857ab4382d2SGreg Kroah-Hartman } 858ab4382d2SGreg Kroah-Hartman 85998f2082cSNicolas Ferre atmel_tasklet_schedule(atmel_port, &atmel_port->tasklet_rx); 860ab4382d2SGreg Kroah-Hartman } 861ab4382d2SGreg Kroah-Hartman 862ab4382d2SGreg Kroah-Hartman /* 863ab4382d2SGreg Kroah-Hartman * Transmit characters (called from tasklet with TXRDY interrupt 864ab4382d2SGreg Kroah-Hartman * disabled) 865ab4382d2SGreg Kroah-Hartman */ 866ab4382d2SGreg Kroah-Hartman static void atmel_tx_chars(struct uart_port *port) 867ab4382d2SGreg Kroah-Hartman { 868ab4382d2SGreg Kroah-Hartman struct circ_buf *xmit = &port->state->xmit; 869ab4382d2SGreg Kroah-Hartman struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 870ab4382d2SGreg Kroah-Hartman 8714e7decdaSCyrille Pitchen if (port->x_char && 8724e7decdaSCyrille Pitchen (atmel_uart_readl(port, ATMEL_US_CSR) & atmel_port->tx_done_mask)) { 873a6499435SCyrille Pitchen atmel_uart_write_char(port, port->x_char); 874ab4382d2SGreg Kroah-Hartman port->icount.tx++; 875ab4382d2SGreg Kroah-Hartman port->x_char = 0; 876ab4382d2SGreg Kroah-Hartman } 877ab4382d2SGreg Kroah-Hartman if (uart_circ_empty(xmit) || uart_tx_stopped(port)) 878ab4382d2SGreg Kroah-Hartman return; 879ab4382d2SGreg Kroah-Hartman 8804e7decdaSCyrille Pitchen while (atmel_uart_readl(port, ATMEL_US_CSR) & 8814e7decdaSCyrille Pitchen atmel_port->tx_done_mask) { 882a6499435SCyrille Pitchen atmel_uart_write_char(port, xmit->buf[xmit->tail]); 883ab4382d2SGreg Kroah-Hartman xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1); 884ab4382d2SGreg Kroah-Hartman port->icount.tx++; 885ab4382d2SGreg Kroah-Hartman if (uart_circ_empty(xmit)) 886ab4382d2SGreg Kroah-Hartman break; 887ab4382d2SGreg Kroah-Hartman } 888ab4382d2SGreg Kroah-Hartman 889ab4382d2SGreg Kroah-Hartman if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS) 890ab4382d2SGreg Kroah-Hartman uart_write_wakeup(port); 891ab4382d2SGreg Kroah-Hartman 892ab4382d2SGreg Kroah-Hartman if (!uart_circ_empty(xmit)) 893ab4382d2SGreg Kroah-Hartman /* Enable interrupts */ 8944e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_IER, 8954e7decdaSCyrille Pitchen atmel_port->tx_done_mask); 896ab4382d2SGreg Kroah-Hartman } 897ab4382d2SGreg Kroah-Hartman 89808f738beSElen Song static void atmel_complete_tx_dma(void *arg) 89908f738beSElen Song { 90008f738beSElen Song struct atmel_uart_port *atmel_port = arg; 90108f738beSElen Song struct uart_port *port = &atmel_port->uart; 90208f738beSElen Song struct circ_buf *xmit = &port->state->xmit; 90308f738beSElen Song struct dma_chan *chan = atmel_port->chan_tx; 90408f738beSElen Song unsigned long flags; 90508f738beSElen Song 90608f738beSElen Song spin_lock_irqsave(&port->lock, flags); 90708f738beSElen Song 90808f738beSElen Song if (chan) 90908f738beSElen Song dmaengine_terminate_all(chan); 9105f258b3eSCyrille Pitchen xmit->tail += atmel_port->tx_len; 91108f738beSElen Song xmit->tail &= UART_XMIT_SIZE - 1; 91208f738beSElen Song 9135f258b3eSCyrille Pitchen port->icount.tx += atmel_port->tx_len; 91408f738beSElen Song 91508f738beSElen Song spin_lock_irq(&atmel_port->lock_tx); 91608f738beSElen Song async_tx_ack(atmel_port->desc_tx); 91708f738beSElen Song atmel_port->cookie_tx = -EINVAL; 91808f738beSElen Song atmel_port->desc_tx = NULL; 91908f738beSElen Song spin_unlock_irq(&atmel_port->lock_tx); 92008f738beSElen Song 92108f738beSElen Song if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS) 92208f738beSElen Song uart_write_wakeup(port); 92308f738beSElen Song 9241842dc2eSCyrille Pitchen /* 9251842dc2eSCyrille Pitchen * xmit is a circular buffer so, if we have just send data from 9261842dc2eSCyrille Pitchen * xmit->tail to the end of xmit->buf, now we have to transmit the 9271842dc2eSCyrille Pitchen * remaining data from the beginning of xmit->buf to xmit->head. 9281842dc2eSCyrille Pitchen */ 92908f738beSElen Song if (!uart_circ_empty(xmit)) 93098f2082cSNicolas Ferre atmel_tasklet_schedule(atmel_port, &atmel_port->tasklet_tx); 931*377fedd1SNicolas Ferre else if (((port->rs485.flags & SER_RS485_ENABLED) && 932*377fedd1SNicolas Ferre !(port->rs485.flags & SER_RS485_RX_DURING_TX)) || 933*377fedd1SNicolas Ferre port->iso7816.flags & SER_ISO7816_ENABLED) { 934b389f173SRichard Genoud /* DMA done, stop TX, start RX for RS485 */ 935b389f173SRichard Genoud atmel_start_rx(port); 936b389f173SRichard Genoud } 93708f738beSElen Song 93808f738beSElen Song spin_unlock_irqrestore(&port->lock, flags); 93908f738beSElen Song } 94008f738beSElen Song 94108f738beSElen Song static void atmel_release_tx_dma(struct uart_port *port) 94208f738beSElen Song { 94308f738beSElen Song struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 94408f738beSElen Song struct dma_chan *chan = atmel_port->chan_tx; 94508f738beSElen Song 94608f738beSElen Song if (chan) { 94708f738beSElen Song dmaengine_terminate_all(chan); 94808f738beSElen Song dma_release_channel(chan); 94908f738beSElen Song dma_unmap_sg(port->dev, &atmel_port->sg_tx, 1, 95048479148SWolfram Sang DMA_TO_DEVICE); 95108f738beSElen Song } 95208f738beSElen Song 95308f738beSElen Song atmel_port->desc_tx = NULL; 95408f738beSElen Song atmel_port->chan_tx = NULL; 95508f738beSElen Song atmel_port->cookie_tx = -EINVAL; 95608f738beSElen Song } 95708f738beSElen Song 95808f738beSElen Song /* 95908f738beSElen Song * Called from tasklet with TXRDY interrupt is disabled. 96008f738beSElen Song */ 96108f738beSElen Song static void atmel_tx_dma(struct uart_port *port) 96208f738beSElen Song { 96308f738beSElen Song struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 96408f738beSElen Song struct circ_buf *xmit = &port->state->xmit; 96508f738beSElen Song struct dma_chan *chan = atmel_port->chan_tx; 96608f738beSElen Song struct dma_async_tx_descriptor *desc; 9675f258b3eSCyrille Pitchen struct scatterlist sgl[2], *sg, *sg_tx = &atmel_port->sg_tx; 9685f258b3eSCyrille Pitchen unsigned int tx_len, part1_len, part2_len, sg_len; 9695f258b3eSCyrille Pitchen dma_addr_t phys_addr; 97008f738beSElen Song 97108f738beSElen Song /* Make sure we have an idle channel */ 97208f738beSElen Song if (atmel_port->desc_tx != NULL) 97308f738beSElen Song return; 97408f738beSElen Song 97508f738beSElen Song if (!uart_circ_empty(xmit) && !uart_tx_stopped(port)) { 97608f738beSElen Song /* 97708f738beSElen Song * DMA is idle now. 97808f738beSElen Song * Port xmit buffer is already mapped, 97908f738beSElen Song * and it is one page... Just adjust 98008f738beSElen Song * offsets and lengths. Since it is a circular buffer, 98108f738beSElen Song * we have to transmit till the end, and then the rest. 98208f738beSElen Song * Take the port lock to get a 98308f738beSElen Song * consistent xmit buffer state. 98408f738beSElen Song */ 9855f258b3eSCyrille Pitchen tx_len = CIRC_CNT_TO_END(xmit->head, 98608f738beSElen Song xmit->tail, 98708f738beSElen Song UART_XMIT_SIZE); 9885f258b3eSCyrille Pitchen 9895f258b3eSCyrille Pitchen if (atmel_port->fifo_size) { 9905f258b3eSCyrille Pitchen /* multi data mode */ 9915f258b3eSCyrille Pitchen part1_len = (tx_len & ~0x3); /* DWORD access */ 9925f258b3eSCyrille Pitchen part2_len = (tx_len & 0x3); /* BYTE access */ 9935f258b3eSCyrille Pitchen } else { 9945f258b3eSCyrille Pitchen /* single data (legacy) mode */ 9955f258b3eSCyrille Pitchen part1_len = 0; 9965f258b3eSCyrille Pitchen part2_len = tx_len; /* BYTE access only */ 9975f258b3eSCyrille Pitchen } 9985f258b3eSCyrille Pitchen 9995f258b3eSCyrille Pitchen sg_init_table(sgl, 2); 10005f258b3eSCyrille Pitchen sg_len = 0; 10015f258b3eSCyrille Pitchen phys_addr = sg_dma_address(sg_tx) + xmit->tail; 10025f258b3eSCyrille Pitchen if (part1_len) { 10035f258b3eSCyrille Pitchen sg = &sgl[sg_len++]; 10045f258b3eSCyrille Pitchen sg_dma_address(sg) = phys_addr; 10055f258b3eSCyrille Pitchen sg_dma_len(sg) = part1_len; 10065f258b3eSCyrille Pitchen 10075f258b3eSCyrille Pitchen phys_addr += part1_len; 10085f258b3eSCyrille Pitchen } 10095f258b3eSCyrille Pitchen 10105f258b3eSCyrille Pitchen if (part2_len) { 10115f258b3eSCyrille Pitchen sg = &sgl[sg_len++]; 10125f258b3eSCyrille Pitchen sg_dma_address(sg) = phys_addr; 10135f258b3eSCyrille Pitchen sg_dma_len(sg) = part2_len; 10145f258b3eSCyrille Pitchen } 10155f258b3eSCyrille Pitchen 10165f258b3eSCyrille Pitchen /* 10175f258b3eSCyrille Pitchen * save tx_len so atmel_complete_tx_dma() will increase 10185f258b3eSCyrille Pitchen * xmit->tail correctly 10195f258b3eSCyrille Pitchen */ 10205f258b3eSCyrille Pitchen atmel_port->tx_len = tx_len; 102108f738beSElen Song 102208f738beSElen Song desc = dmaengine_prep_slave_sg(chan, 10235f258b3eSCyrille Pitchen sgl, 10245f258b3eSCyrille Pitchen sg_len, 102508f738beSElen Song DMA_MEM_TO_DEV, 102608f738beSElen Song DMA_PREP_INTERRUPT | 102708f738beSElen Song DMA_CTRL_ACK); 102808f738beSElen Song if (!desc) { 102908f738beSElen Song dev_err(port->dev, "Failed to send via dma!\n"); 103008f738beSElen Song return; 103108f738beSElen Song } 103208f738beSElen Song 10335f258b3eSCyrille Pitchen dma_sync_sg_for_device(port->dev, sg_tx, 1, DMA_TO_DEVICE); 103408f738beSElen Song 103508f738beSElen Song atmel_port->desc_tx = desc; 103608f738beSElen Song desc->callback = atmel_complete_tx_dma; 103708f738beSElen Song desc->callback_param = atmel_port; 103808f738beSElen Song atmel_port->cookie_tx = dmaengine_submit(desc); 103908f738beSElen Song } 104008f738beSElen Song 104108f738beSElen Song if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS) 104208f738beSElen Song uart_write_wakeup(port); 104308f738beSElen Song } 104408f738beSElen Song 104508f738beSElen Song static int atmel_prepare_tx_dma(struct uart_port *port) 104608f738beSElen Song { 104708f738beSElen Song struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 1048c24d2531SRadu Pirea struct device *mfd_dev = port->dev->parent; 104908f738beSElen Song dma_cap_mask_t mask; 105008f738beSElen Song struct dma_slave_config config; 105108f738beSElen Song int ret, nent; 105208f738beSElen Song 105308f738beSElen Song dma_cap_zero(mask); 105408f738beSElen Song dma_cap_set(DMA_SLAVE, mask); 105508f738beSElen Song 1056c24d2531SRadu Pirea atmel_port->chan_tx = dma_request_slave_channel(mfd_dev, "tx"); 105708f738beSElen Song if (atmel_port->chan_tx == NULL) 105808f738beSElen Song goto chan_err; 105908f738beSElen Song dev_info(port->dev, "using %s for tx DMA transfers\n", 106008f738beSElen Song dma_chan_name(atmel_port->chan_tx)); 106108f738beSElen Song 106208f738beSElen Song spin_lock_init(&atmel_port->lock_tx); 106308f738beSElen Song sg_init_table(&atmel_port->sg_tx, 1); 106408f738beSElen Song /* UART circular tx buffer is an aligned page. */ 10652c277054SLeilei Zhao BUG_ON(!PAGE_ALIGNED(port->state->xmit.buf)); 106608f738beSElen Song sg_set_page(&atmel_port->sg_tx, 106708f738beSElen Song virt_to_page(port->state->xmit.buf), 106808f738beSElen Song UART_XMIT_SIZE, 10692b5cf14bSGeliang Tang offset_in_page(port->state->xmit.buf)); 107008f738beSElen Song nent = dma_map_sg(port->dev, 107108f738beSElen Song &atmel_port->sg_tx, 107208f738beSElen Song 1, 107348479148SWolfram Sang DMA_TO_DEVICE); 107408f738beSElen Song 107508f738beSElen Song if (!nent) { 107608f738beSElen Song dev_dbg(port->dev, "need to release resource of dma\n"); 107708f738beSElen Song goto chan_err; 107808f738beSElen Song } else { 1079c8d1f022SUwe Kleine-König dev_dbg(port->dev, "%s: mapped %d@%p to %pad\n", __func__, 108008f738beSElen Song sg_dma_len(&atmel_port->sg_tx), 108108f738beSElen Song port->state->xmit.buf, 1082c8d1f022SUwe Kleine-König &sg_dma_address(&atmel_port->sg_tx)); 108308f738beSElen Song } 108408f738beSElen Song 108508f738beSElen Song /* Configure the slave DMA */ 108608f738beSElen Song memset(&config, 0, sizeof(config)); 108708f738beSElen Song config.direction = DMA_MEM_TO_DEV; 10885f258b3eSCyrille Pitchen config.dst_addr_width = (atmel_port->fifo_size) ? 10895f258b3eSCyrille Pitchen DMA_SLAVE_BUSWIDTH_4_BYTES : 10905f258b3eSCyrille Pitchen DMA_SLAVE_BUSWIDTH_1_BYTE; 109108f738beSElen Song config.dst_addr = port->mapbase + ATMEL_US_THR; 1092a8d4e016SLudovic Desroches config.dst_maxburst = 1; 109308f738beSElen Song 10945483c10eSMaxime Ripard ret = dmaengine_slave_config(atmel_port->chan_tx, 10955483c10eSMaxime Ripard &config); 109608f738beSElen Song if (ret) { 109708f738beSElen Song dev_err(port->dev, "DMA tx slave configuration failed\n"); 109808f738beSElen Song goto chan_err; 109908f738beSElen Song } 110008f738beSElen Song 110108f738beSElen Song return 0; 110208f738beSElen Song 110308f738beSElen Song chan_err: 110408f738beSElen Song dev_err(port->dev, "TX channel not available, switch to pio\n"); 110508f738beSElen Song atmel_port->use_dma_tx = 0; 110608f738beSElen Song if (atmel_port->chan_tx) 110708f738beSElen Song atmel_release_tx_dma(port); 110808f738beSElen Song return -EINVAL; 110908f738beSElen Song } 111008f738beSElen Song 111134df42f5SElen Song static void atmel_complete_rx_dma(void *arg) 111234df42f5SElen Song { 111334df42f5SElen Song struct uart_port *port = arg; 111434df42f5SElen Song struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 111534df42f5SElen Song 111698f2082cSNicolas Ferre atmel_tasklet_schedule(atmel_port, &atmel_port->tasklet_rx); 111734df42f5SElen Song } 111834df42f5SElen Song 111934df42f5SElen Song static void atmel_release_rx_dma(struct uart_port *port) 112034df42f5SElen Song { 112134df42f5SElen Song struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 112234df42f5SElen Song struct dma_chan *chan = atmel_port->chan_rx; 112334df42f5SElen Song 112434df42f5SElen Song if (chan) { 112534df42f5SElen Song dmaengine_terminate_all(chan); 112634df42f5SElen Song dma_release_channel(chan); 112734df42f5SElen Song dma_unmap_sg(port->dev, &atmel_port->sg_rx, 1, 112848479148SWolfram Sang DMA_FROM_DEVICE); 112934df42f5SElen Song } 113034df42f5SElen Song 113134df42f5SElen Song atmel_port->desc_rx = NULL; 113234df42f5SElen Song atmel_port->chan_rx = NULL; 113334df42f5SElen Song atmel_port->cookie_rx = -EINVAL; 113434df42f5SElen Song } 113534df42f5SElen Song 113634df42f5SElen Song static void atmel_rx_from_dma(struct uart_port *port) 113734df42f5SElen Song { 113834df42f5SElen Song struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 113966f37aafSCyrille Pitchen struct tty_port *tport = &port->state->port; 114034df42f5SElen Song struct circ_buf *ring = &atmel_port->rx_ring; 114134df42f5SElen Song struct dma_chan *chan = atmel_port->chan_rx; 114234df42f5SElen Song struct dma_tx_state state; 114334df42f5SElen Song enum dma_status dmastat; 114466f37aafSCyrille Pitchen size_t count; 114534df42f5SElen Song 114634df42f5SElen Song 114734df42f5SElen Song /* Reset the UART timeout early so that we don't miss one */ 11484e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_STTTO); 114934df42f5SElen Song dmastat = dmaengine_tx_status(chan, 115034df42f5SElen Song atmel_port->cookie_rx, 115134df42f5SElen Song &state); 115234df42f5SElen Song /* Restart a new tasklet if DMA status is error */ 115334df42f5SElen Song if (dmastat == DMA_ERROR) { 115434df42f5SElen Song dev_dbg(port->dev, "Get residue error, restart tasklet\n"); 11554e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_IER, ATMEL_US_TIMEOUT); 115698f2082cSNicolas Ferre atmel_tasklet_schedule(atmel_port, &atmel_port->tasklet_rx); 115734df42f5SElen Song return; 115834df42f5SElen Song } 115966f37aafSCyrille Pitchen 116066f37aafSCyrille Pitchen /* CPU claims ownership of RX DMA buffer */ 116166f37aafSCyrille Pitchen dma_sync_sg_for_cpu(port->dev, 116266f37aafSCyrille Pitchen &atmel_port->sg_rx, 116366f37aafSCyrille Pitchen 1, 1164485819b5SCyrille Pitchen DMA_FROM_DEVICE); 116534df42f5SElen Song 116634df42f5SElen Song /* 116766f37aafSCyrille Pitchen * ring->head points to the end of data already written by the DMA. 116866f37aafSCyrille Pitchen * ring->tail points to the beginning of data to be read by the 116966f37aafSCyrille Pitchen * framework. 117066f37aafSCyrille Pitchen * The current transfer size should not be larger than the dma buffer 117166f37aafSCyrille Pitchen * length. 117234df42f5SElen Song */ 117366f37aafSCyrille Pitchen ring->head = sg_dma_len(&atmel_port->sg_rx) - state.residue; 117466f37aafSCyrille Pitchen BUG_ON(ring->head > sg_dma_len(&atmel_port->sg_rx)); 117566f37aafSCyrille Pitchen /* 117666f37aafSCyrille Pitchen * At this point ring->head may point to the first byte right after the 117766f37aafSCyrille Pitchen * last byte of the dma buffer: 117866f37aafSCyrille Pitchen * 0 <= ring->head <= sg_dma_len(&atmel_port->sg_rx) 117966f37aafSCyrille Pitchen * 118066f37aafSCyrille Pitchen * However ring->tail must always points inside the dma buffer: 118166f37aafSCyrille Pitchen * 0 <= ring->tail <= sg_dma_len(&atmel_port->sg_rx) - 1 118266f37aafSCyrille Pitchen * 118366f37aafSCyrille Pitchen * Since we use a ring buffer, we have to handle the case 118466f37aafSCyrille Pitchen * where head is lower than tail. In such a case, we first read from 118566f37aafSCyrille Pitchen * tail to the end of the buffer then reset tail. 118666f37aafSCyrille Pitchen */ 118766f37aafSCyrille Pitchen if (ring->head < ring->tail) { 118866f37aafSCyrille Pitchen count = sg_dma_len(&atmel_port->sg_rx) - ring->tail; 118934df42f5SElen Song 119066f37aafSCyrille Pitchen tty_insert_flip_string(tport, ring->buf + ring->tail, count); 119166f37aafSCyrille Pitchen ring->tail = 0; 119234df42f5SElen Song port->icount.rx += count; 119334df42f5SElen Song } 119434df42f5SElen Song 119566f37aafSCyrille Pitchen /* Finally we read data from tail to head */ 119666f37aafSCyrille Pitchen if (ring->tail < ring->head) { 119766f37aafSCyrille Pitchen count = ring->head - ring->tail; 119866f37aafSCyrille Pitchen 119966f37aafSCyrille Pitchen tty_insert_flip_string(tport, ring->buf + ring->tail, count); 120066f37aafSCyrille Pitchen /* Wrap ring->head if needed */ 120166f37aafSCyrille Pitchen if (ring->head >= sg_dma_len(&atmel_port->sg_rx)) 120266f37aafSCyrille Pitchen ring->head = 0; 120366f37aafSCyrille Pitchen ring->tail = ring->head; 120466f37aafSCyrille Pitchen port->icount.rx += count; 120566f37aafSCyrille Pitchen } 120666f37aafSCyrille Pitchen 120766f37aafSCyrille Pitchen /* USART retreives ownership of RX DMA buffer */ 120866f37aafSCyrille Pitchen dma_sync_sg_for_device(port->dev, 120966f37aafSCyrille Pitchen &atmel_port->sg_rx, 121066f37aafSCyrille Pitchen 1, 1211485819b5SCyrille Pitchen DMA_FROM_DEVICE); 121266f37aafSCyrille Pitchen 121366f37aafSCyrille Pitchen /* 121466f37aafSCyrille Pitchen * Drop the lock here since it might end up calling 121566f37aafSCyrille Pitchen * uart_start(), which takes the lock. 121666f37aafSCyrille Pitchen */ 121766f37aafSCyrille Pitchen spin_unlock(&port->lock); 121866f37aafSCyrille Pitchen tty_flip_buffer_push(tport); 121966f37aafSCyrille Pitchen spin_lock(&port->lock); 122066f37aafSCyrille Pitchen 12214e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_IER, ATMEL_US_TIMEOUT); 122234df42f5SElen Song } 122334df42f5SElen Song 122434df42f5SElen Song static int atmel_prepare_rx_dma(struct uart_port *port) 122534df42f5SElen Song { 122634df42f5SElen Song struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 1227c24d2531SRadu Pirea struct device *mfd_dev = port->dev->parent; 122834df42f5SElen Song struct dma_async_tx_descriptor *desc; 122934df42f5SElen Song dma_cap_mask_t mask; 123034df42f5SElen Song struct dma_slave_config config; 123134df42f5SElen Song struct circ_buf *ring; 123234df42f5SElen Song int ret, nent; 123334df42f5SElen Song 123434df42f5SElen Song ring = &atmel_port->rx_ring; 123534df42f5SElen Song 123634df42f5SElen Song dma_cap_zero(mask); 123734df42f5SElen Song dma_cap_set(DMA_CYCLIC, mask); 123834df42f5SElen Song 1239c24d2531SRadu Pirea atmel_port->chan_rx = dma_request_slave_channel(mfd_dev, "rx"); 124034df42f5SElen Song if (atmel_port->chan_rx == NULL) 124134df42f5SElen Song goto chan_err; 124234df42f5SElen Song dev_info(port->dev, "using %s for rx DMA transfers\n", 124334df42f5SElen Song dma_chan_name(atmel_port->chan_rx)); 124434df42f5SElen Song 124534df42f5SElen Song spin_lock_init(&atmel_port->lock_rx); 124634df42f5SElen Song sg_init_table(&atmel_port->sg_rx, 1); 124734df42f5SElen Song /* UART circular rx buffer is an aligned page. */ 12482c277054SLeilei Zhao BUG_ON(!PAGE_ALIGNED(ring->buf)); 124934df42f5SElen Song sg_set_page(&atmel_port->sg_rx, 125034df42f5SElen Song virt_to_page(ring->buf), 1251a510880fSLeilei Zhao sizeof(struct atmel_uart_char) * ATMEL_SERIAL_RINGSIZE, 12522b5cf14bSGeliang Tang offset_in_page(ring->buf)); 125334df42f5SElen Song nent = dma_map_sg(port->dev, 125434df42f5SElen Song &atmel_port->sg_rx, 125534df42f5SElen Song 1, 125648479148SWolfram Sang DMA_FROM_DEVICE); 125734df42f5SElen Song 125834df42f5SElen Song if (!nent) { 125934df42f5SElen Song dev_dbg(port->dev, "need to release resource of dma\n"); 126034df42f5SElen Song goto chan_err; 126134df42f5SElen Song } else { 1262c8d1f022SUwe Kleine-König dev_dbg(port->dev, "%s: mapped %d@%p to %pad\n", __func__, 126334df42f5SElen Song sg_dma_len(&atmel_port->sg_rx), 126434df42f5SElen Song ring->buf, 1265c8d1f022SUwe Kleine-König &sg_dma_address(&atmel_port->sg_rx)); 126634df42f5SElen Song } 126734df42f5SElen Song 126834df42f5SElen Song /* Configure the slave DMA */ 126934df42f5SElen Song memset(&config, 0, sizeof(config)); 127034df42f5SElen Song config.direction = DMA_DEV_TO_MEM; 127134df42f5SElen Song config.src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE; 127234df42f5SElen Song config.src_addr = port->mapbase + ATMEL_US_RHR; 1273a8d4e016SLudovic Desroches config.src_maxburst = 1; 127434df42f5SElen Song 12755483c10eSMaxime Ripard ret = dmaengine_slave_config(atmel_port->chan_rx, 12765483c10eSMaxime Ripard &config); 127734df42f5SElen Song if (ret) { 127834df42f5SElen Song dev_err(port->dev, "DMA rx slave configuration failed\n"); 127934df42f5SElen Song goto chan_err; 128034df42f5SElen Song } 128134df42f5SElen Song /* 128234df42f5SElen Song * Prepare a cyclic dma transfer, assign 2 descriptors, 128334df42f5SElen Song * each one is half ring buffer size 128434df42f5SElen Song */ 128534df42f5SElen Song desc = dmaengine_prep_dma_cyclic(atmel_port->chan_rx, 128634df42f5SElen Song sg_dma_address(&atmel_port->sg_rx), 128734df42f5SElen Song sg_dma_len(&atmel_port->sg_rx), 128834df42f5SElen Song sg_dma_len(&atmel_port->sg_rx)/2, 128934df42f5SElen Song DMA_DEV_TO_MEM, 129034df42f5SElen Song DMA_PREP_INTERRUPT); 129134df42f5SElen Song desc->callback = atmel_complete_rx_dma; 129234df42f5SElen Song desc->callback_param = port; 129334df42f5SElen Song atmel_port->desc_rx = desc; 129434df42f5SElen Song atmel_port->cookie_rx = dmaengine_submit(desc); 129534df42f5SElen Song 129634df42f5SElen Song return 0; 129734df42f5SElen Song 129834df42f5SElen Song chan_err: 129934df42f5SElen Song dev_err(port->dev, "RX channel not available, switch to pio\n"); 130034df42f5SElen Song atmel_port->use_dma_rx = 0; 130134df42f5SElen Song if (atmel_port->chan_rx) 130234df42f5SElen Song atmel_release_rx_dma(port); 130334df42f5SElen Song return -EINVAL; 130434df42f5SElen Song } 130534df42f5SElen Song 1306026cb432SKees Cook static void atmel_uart_timer_callback(struct timer_list *t) 13072e68c22fSElen Song { 1308026cb432SKees Cook struct atmel_uart_port *atmel_port = from_timer(atmel_port, t, 1309026cb432SKees Cook uart_timer); 1310026cb432SKees Cook struct uart_port *port = &atmel_port->uart; 13112e68c22fSElen Song 131298f2082cSNicolas Ferre if (!atomic_read(&atmel_port->tasklet_shutdown)) { 131300e8e658SNicolas Ferre tasklet_schedule(&atmel_port->tasklet_rx); 131498f2082cSNicolas Ferre mod_timer(&atmel_port->uart_timer, 131598f2082cSNicolas Ferre jiffies + uart_poll_timeout(port)); 131698f2082cSNicolas Ferre } 13172e68c22fSElen Song } 13182e68c22fSElen Song 1319ab4382d2SGreg Kroah-Hartman /* 1320ab4382d2SGreg Kroah-Hartman * receive interrupt handler. 1321ab4382d2SGreg Kroah-Hartman */ 1322ab4382d2SGreg Kroah-Hartman static void 1323ab4382d2SGreg Kroah-Hartman atmel_handle_receive(struct uart_port *port, unsigned int pending) 1324ab4382d2SGreg Kroah-Hartman { 1325ab4382d2SGreg Kroah-Hartman struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 1326ab4382d2SGreg Kroah-Hartman 132764e22ebeSElen Song if (atmel_use_pdc_rx(port)) { 1328ab4382d2SGreg Kroah-Hartman /* 1329ab4382d2SGreg Kroah-Hartman * PDC receive. Just schedule the tasklet and let it 1330ab4382d2SGreg Kroah-Hartman * figure out the details. 1331ab4382d2SGreg Kroah-Hartman * 1332ab4382d2SGreg Kroah-Hartman * TODO: We're not handling error flags correctly at 1333ab4382d2SGreg Kroah-Hartman * the moment. 1334ab4382d2SGreg Kroah-Hartman */ 1335ab4382d2SGreg Kroah-Hartman if (pending & (ATMEL_US_ENDRX | ATMEL_US_TIMEOUT)) { 13364e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_IDR, 13374e7decdaSCyrille Pitchen (ATMEL_US_ENDRX | ATMEL_US_TIMEOUT)); 133898f2082cSNicolas Ferre atmel_tasklet_schedule(atmel_port, 133998f2082cSNicolas Ferre &atmel_port->tasklet_rx); 1340ab4382d2SGreg Kroah-Hartman } 1341ab4382d2SGreg Kroah-Hartman 1342ab4382d2SGreg Kroah-Hartman if (pending & (ATMEL_US_RXBRK | ATMEL_US_OVRE | 1343ab4382d2SGreg Kroah-Hartman ATMEL_US_FRAME | ATMEL_US_PARE)) 1344ab4382d2SGreg Kroah-Hartman atmel_pdc_rxerr(port, pending); 1345ab4382d2SGreg Kroah-Hartman } 1346ab4382d2SGreg Kroah-Hartman 134734df42f5SElen Song if (atmel_use_dma_rx(port)) { 134834df42f5SElen Song if (pending & ATMEL_US_TIMEOUT) { 13494e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_IDR, 13504e7decdaSCyrille Pitchen ATMEL_US_TIMEOUT); 135198f2082cSNicolas Ferre atmel_tasklet_schedule(atmel_port, 135298f2082cSNicolas Ferre &atmel_port->tasklet_rx); 135334df42f5SElen Song } 135434df42f5SElen Song } 135534df42f5SElen Song 1356ab4382d2SGreg Kroah-Hartman /* Interrupt receive */ 1357ab4382d2SGreg Kroah-Hartman if (pending & ATMEL_US_RXRDY) 1358ab4382d2SGreg Kroah-Hartman atmel_rx_chars(port); 1359ab4382d2SGreg Kroah-Hartman else if (pending & ATMEL_US_RXBRK) { 1360ab4382d2SGreg Kroah-Hartman /* 1361ab4382d2SGreg Kroah-Hartman * End of break detected. If it came along with a 1362ab4382d2SGreg Kroah-Hartman * character, atmel_rx_chars will handle it. 1363ab4382d2SGreg Kroah-Hartman */ 13644e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA); 13654e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_IDR, ATMEL_US_RXBRK); 1366ab4382d2SGreg Kroah-Hartman atmel_port->break_active = 0; 1367ab4382d2SGreg Kroah-Hartman } 1368ab4382d2SGreg Kroah-Hartman } 1369ab4382d2SGreg Kroah-Hartman 1370ab4382d2SGreg Kroah-Hartman /* 1371ab4382d2SGreg Kroah-Hartman * transmit interrupt handler. (Transmit is IRQF_NODELAY safe) 1372ab4382d2SGreg Kroah-Hartman */ 1373ab4382d2SGreg Kroah-Hartman static void 1374ab4382d2SGreg Kroah-Hartman atmel_handle_transmit(struct uart_port *port, unsigned int pending) 1375ab4382d2SGreg Kroah-Hartman { 1376ab4382d2SGreg Kroah-Hartman struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 1377ab4382d2SGreg Kroah-Hartman 1378ab4382d2SGreg Kroah-Hartman if (pending & atmel_port->tx_done_mask) { 1379ab4382d2SGreg Kroah-Hartman /* Either PDC or interrupt transmission */ 13804e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_IDR, 13814e7decdaSCyrille Pitchen atmel_port->tx_done_mask); 138298f2082cSNicolas Ferre atmel_tasklet_schedule(atmel_port, &atmel_port->tasklet_tx); 1383ab4382d2SGreg Kroah-Hartman } 1384ab4382d2SGreg Kroah-Hartman } 1385ab4382d2SGreg Kroah-Hartman 1386ab4382d2SGreg Kroah-Hartman /* 1387ab4382d2SGreg Kroah-Hartman * status flags interrupt handler. 1388ab4382d2SGreg Kroah-Hartman */ 1389ab4382d2SGreg Kroah-Hartman static void 1390ab4382d2SGreg Kroah-Hartman atmel_handle_status(struct uart_port *port, unsigned int pending, 1391ab4382d2SGreg Kroah-Hartman unsigned int status) 1392ab4382d2SGreg Kroah-Hartman { 1393ab4382d2SGreg Kroah-Hartman struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 13949205218eSNicolas Ferre unsigned int status_change; 1395ab4382d2SGreg Kroah-Hartman 1396ab4382d2SGreg Kroah-Hartman if (pending & (ATMEL_US_RIIC | ATMEL_US_DSRIC | ATMEL_US_DCDIC 1397ab4382d2SGreg Kroah-Hartman | ATMEL_US_CTSIC)) { 13989205218eSNicolas Ferre status_change = status ^ atmel_port->irq_status_prev; 1399d033e82dSLeilei Zhao atmel_port->irq_status_prev = status; 14009205218eSNicolas Ferre 14019205218eSNicolas Ferre if (status_change & (ATMEL_US_RI | ATMEL_US_DSR 14029205218eSNicolas Ferre | ATMEL_US_DCD | ATMEL_US_CTS)) { 14039205218eSNicolas Ferre /* TODO: All reads to CSR will clear these interrupts! */ 14049205218eSNicolas Ferre if (status_change & ATMEL_US_RI) 14059205218eSNicolas Ferre port->icount.rng++; 14069205218eSNicolas Ferre if (status_change & ATMEL_US_DSR) 14079205218eSNicolas Ferre port->icount.dsr++; 14089205218eSNicolas Ferre if (status_change & ATMEL_US_DCD) 14099205218eSNicolas Ferre uart_handle_dcd_change(port, !(status & ATMEL_US_DCD)); 14109205218eSNicolas Ferre if (status_change & ATMEL_US_CTS) 14119205218eSNicolas Ferre uart_handle_cts_change(port, !(status & ATMEL_US_CTS)); 14129205218eSNicolas Ferre 14139205218eSNicolas Ferre wake_up_interruptible(&port->state->port.delta_msr_wait); 14149205218eSNicolas Ferre } 1415ab4382d2SGreg Kroah-Hartman } 1416*377fedd1SNicolas Ferre 1417*377fedd1SNicolas Ferre if (pending & (ATMEL_US_NACK | ATMEL_US_ITERATION)) 1418*377fedd1SNicolas Ferre dev_dbg(port->dev, "ISO7816 ERROR (0x%08x)\n", pending); 1419ab4382d2SGreg Kroah-Hartman } 1420ab4382d2SGreg Kroah-Hartman 1421ab4382d2SGreg Kroah-Hartman /* 1422ab4382d2SGreg Kroah-Hartman * Interrupt handler 1423ab4382d2SGreg Kroah-Hartman */ 1424ab4382d2SGreg Kroah-Hartman static irqreturn_t atmel_interrupt(int irq, void *dev_id) 1425ab4382d2SGreg Kroah-Hartman { 1426ab4382d2SGreg Kroah-Hartman struct uart_port *port = dev_id; 1427ab5e4e41SRichard Genoud struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 14282c7af5baSBoris BREZILLON unsigned int status, pending, mask, pass_counter = 0; 1429ab4382d2SGreg Kroah-Hartman 14302c7af5baSBoris BREZILLON spin_lock(&atmel_port->lock_suspended); 14312c7af5baSBoris BREZILLON 1432ab4382d2SGreg Kroah-Hartman do { 1433e0b0baadSRichard Genoud status = atmel_get_lines_status(port); 14344e7decdaSCyrille Pitchen mask = atmel_uart_readl(port, ATMEL_US_IMR); 14352c7af5baSBoris BREZILLON pending = status & mask; 1436ab4382d2SGreg Kroah-Hartman if (!pending) 1437ab4382d2SGreg Kroah-Hartman break; 1438ab4382d2SGreg Kroah-Hartman 14392c7af5baSBoris BREZILLON if (atmel_port->suspended) { 14402c7af5baSBoris BREZILLON atmel_port->pending |= pending; 14412c7af5baSBoris BREZILLON atmel_port->pending_status = status; 14424e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_IDR, mask); 14432c7af5baSBoris BREZILLON pm_system_wakeup(); 14442c7af5baSBoris BREZILLON break; 14452c7af5baSBoris BREZILLON } 14462c7af5baSBoris BREZILLON 1447ab4382d2SGreg Kroah-Hartman atmel_handle_receive(port, pending); 1448ab4382d2SGreg Kroah-Hartman atmel_handle_status(port, pending, status); 1449ab4382d2SGreg Kroah-Hartman atmel_handle_transmit(port, pending); 1450ab4382d2SGreg Kroah-Hartman } while (pass_counter++ < ATMEL_ISR_PASS_LIMIT); 1451ab4382d2SGreg Kroah-Hartman 14522c7af5baSBoris BREZILLON spin_unlock(&atmel_port->lock_suspended); 14532c7af5baSBoris BREZILLON 1454ab4382d2SGreg Kroah-Hartman return pass_counter ? IRQ_HANDLED : IRQ_NONE; 1455ab4382d2SGreg Kroah-Hartman } 1456ab4382d2SGreg Kroah-Hartman 1457a930e528SElen Song static void atmel_release_tx_pdc(struct uart_port *port) 1458a930e528SElen Song { 1459a930e528SElen Song struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 1460a930e528SElen Song struct atmel_dma_buffer *pdc = &atmel_port->pdc_tx; 1461a930e528SElen Song 1462a930e528SElen Song dma_unmap_single(port->dev, 1463a930e528SElen Song pdc->dma_addr, 1464a930e528SElen Song pdc->dma_size, 1465a930e528SElen Song DMA_TO_DEVICE); 1466a930e528SElen Song } 1467a930e528SElen Song 1468ab4382d2SGreg Kroah-Hartman /* 1469ab4382d2SGreg Kroah-Hartman * Called from tasklet with ENDTX and TXBUFE interrupts disabled. 1470ab4382d2SGreg Kroah-Hartman */ 147164e22ebeSElen Song static void atmel_tx_pdc(struct uart_port *port) 1472ab4382d2SGreg Kroah-Hartman { 1473ab4382d2SGreg Kroah-Hartman struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 1474ab4382d2SGreg Kroah-Hartman struct circ_buf *xmit = &port->state->xmit; 1475ab4382d2SGreg Kroah-Hartman struct atmel_dma_buffer *pdc = &atmel_port->pdc_tx; 1476ab4382d2SGreg Kroah-Hartman int count; 1477ab4382d2SGreg Kroah-Hartman 1478ab4382d2SGreg Kroah-Hartman /* nothing left to transmit? */ 14794e7decdaSCyrille Pitchen if (atmel_uart_readl(port, ATMEL_PDC_TCR)) 1480ab4382d2SGreg Kroah-Hartman return; 1481ab4382d2SGreg Kroah-Hartman 1482ab4382d2SGreg Kroah-Hartman xmit->tail += pdc->ofs; 1483ab4382d2SGreg Kroah-Hartman xmit->tail &= UART_XMIT_SIZE - 1; 1484ab4382d2SGreg Kroah-Hartman 1485ab4382d2SGreg Kroah-Hartman port->icount.tx += pdc->ofs; 1486ab4382d2SGreg Kroah-Hartman pdc->ofs = 0; 1487ab4382d2SGreg Kroah-Hartman 1488ab4382d2SGreg Kroah-Hartman /* more to transmit - setup next transfer */ 1489ab4382d2SGreg Kroah-Hartman 1490ab4382d2SGreg Kroah-Hartman /* disable PDC transmit */ 14914e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_TXTDIS); 1492ab4382d2SGreg Kroah-Hartman 1493ab4382d2SGreg Kroah-Hartman if (!uart_circ_empty(xmit) && !uart_tx_stopped(port)) { 1494ab4382d2SGreg Kroah-Hartman dma_sync_single_for_device(port->dev, 1495ab4382d2SGreg Kroah-Hartman pdc->dma_addr, 1496ab4382d2SGreg Kroah-Hartman pdc->dma_size, 1497ab4382d2SGreg Kroah-Hartman DMA_TO_DEVICE); 1498ab4382d2SGreg Kroah-Hartman 1499ab4382d2SGreg Kroah-Hartman count = CIRC_CNT_TO_END(xmit->head, xmit->tail, UART_XMIT_SIZE); 1500ab4382d2SGreg Kroah-Hartman pdc->ofs = count; 1501ab4382d2SGreg Kroah-Hartman 15024e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_PDC_TPR, 15034e7decdaSCyrille Pitchen pdc->dma_addr + xmit->tail); 15044e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_PDC_TCR, count); 1505ab4382d2SGreg Kroah-Hartman /* re-enable PDC transmit */ 15064e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_TXTEN); 1507ab4382d2SGreg Kroah-Hartman /* Enable interrupts */ 15084e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_IER, 15094e7decdaSCyrille Pitchen atmel_port->tx_done_mask); 1510ab4382d2SGreg Kroah-Hartman } else { 1511*377fedd1SNicolas Ferre if (((port->rs485.flags & SER_RS485_ENABLED) && 1512*377fedd1SNicolas Ferre !(port->rs485.flags & SER_RS485_RX_DURING_TX)) || 1513*377fedd1SNicolas Ferre port->iso7816.flags & SER_ISO7816_ENABLED) { 1514ab4382d2SGreg Kroah-Hartman /* DMA done, stop TX, start RX for RS485 */ 1515ab4382d2SGreg Kroah-Hartman atmel_start_rx(port); 1516ab4382d2SGreg Kroah-Hartman } 1517ab4382d2SGreg Kroah-Hartman } 1518ab4382d2SGreg Kroah-Hartman 1519ab4382d2SGreg Kroah-Hartman if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS) 1520ab4382d2SGreg Kroah-Hartman uart_write_wakeup(port); 1521ab4382d2SGreg Kroah-Hartman } 1522ab4382d2SGreg Kroah-Hartman 1523a930e528SElen Song static int atmel_prepare_tx_pdc(struct uart_port *port) 1524a930e528SElen Song { 1525a930e528SElen Song struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 1526a930e528SElen Song struct atmel_dma_buffer *pdc = &atmel_port->pdc_tx; 1527a930e528SElen Song struct circ_buf *xmit = &port->state->xmit; 1528a930e528SElen Song 1529a930e528SElen Song pdc->buf = xmit->buf; 1530a930e528SElen Song pdc->dma_addr = dma_map_single(port->dev, 1531a930e528SElen Song pdc->buf, 1532a930e528SElen Song UART_XMIT_SIZE, 1533a930e528SElen Song DMA_TO_DEVICE); 1534a930e528SElen Song pdc->dma_size = UART_XMIT_SIZE; 1535a930e528SElen Song pdc->ofs = 0; 1536a930e528SElen Song 1537a930e528SElen Song return 0; 1538a930e528SElen Song } 1539a930e528SElen Song 1540ab4382d2SGreg Kroah-Hartman static void atmel_rx_from_ring(struct uart_port *port) 1541ab4382d2SGreg Kroah-Hartman { 1542ab4382d2SGreg Kroah-Hartman struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 1543ab4382d2SGreg Kroah-Hartman struct circ_buf *ring = &atmel_port->rx_ring; 1544ab4382d2SGreg Kroah-Hartman unsigned int flg; 1545ab4382d2SGreg Kroah-Hartman unsigned int status; 1546ab4382d2SGreg Kroah-Hartman 1547ab4382d2SGreg Kroah-Hartman while (ring->head != ring->tail) { 1548ab4382d2SGreg Kroah-Hartman struct atmel_uart_char c; 1549ab4382d2SGreg Kroah-Hartman 1550ab4382d2SGreg Kroah-Hartman /* Make sure c is loaded after head. */ 1551ab4382d2SGreg Kroah-Hartman smp_rmb(); 1552ab4382d2SGreg Kroah-Hartman 1553ab4382d2SGreg Kroah-Hartman c = ((struct atmel_uart_char *)ring->buf)[ring->tail]; 1554ab4382d2SGreg Kroah-Hartman 1555ab4382d2SGreg Kroah-Hartman ring->tail = (ring->tail + 1) & (ATMEL_SERIAL_RINGSIZE - 1); 1556ab4382d2SGreg Kroah-Hartman 1557ab4382d2SGreg Kroah-Hartman port->icount.rx++; 1558ab4382d2SGreg Kroah-Hartman status = c.status; 1559ab4382d2SGreg Kroah-Hartman flg = TTY_NORMAL; 1560ab4382d2SGreg Kroah-Hartman 1561ab4382d2SGreg Kroah-Hartman /* 1562ab4382d2SGreg Kroah-Hartman * note that the error handling code is 1563ab4382d2SGreg Kroah-Hartman * out of the main execution path 1564ab4382d2SGreg Kroah-Hartman */ 1565ab4382d2SGreg Kroah-Hartman if (unlikely(status & (ATMEL_US_PARE | ATMEL_US_FRAME 1566ab4382d2SGreg Kroah-Hartman | ATMEL_US_OVRE | ATMEL_US_RXBRK))) { 1567ab4382d2SGreg Kroah-Hartman if (status & ATMEL_US_RXBRK) { 1568ab4382d2SGreg Kroah-Hartman /* ignore side-effect */ 1569ab4382d2SGreg Kroah-Hartman status &= ~(ATMEL_US_PARE | ATMEL_US_FRAME); 1570ab4382d2SGreg Kroah-Hartman 1571ab4382d2SGreg Kroah-Hartman port->icount.brk++; 1572ab4382d2SGreg Kroah-Hartman if (uart_handle_break(port)) 1573ab4382d2SGreg Kroah-Hartman continue; 1574ab4382d2SGreg Kroah-Hartman } 1575ab4382d2SGreg Kroah-Hartman if (status & ATMEL_US_PARE) 1576ab4382d2SGreg Kroah-Hartman port->icount.parity++; 1577ab4382d2SGreg Kroah-Hartman if (status & ATMEL_US_FRAME) 1578ab4382d2SGreg Kroah-Hartman port->icount.frame++; 1579ab4382d2SGreg Kroah-Hartman if (status & ATMEL_US_OVRE) 1580ab4382d2SGreg Kroah-Hartman port->icount.overrun++; 1581ab4382d2SGreg Kroah-Hartman 1582ab4382d2SGreg Kroah-Hartman status &= port->read_status_mask; 1583ab4382d2SGreg Kroah-Hartman 1584ab4382d2SGreg Kroah-Hartman if (status & ATMEL_US_RXBRK) 1585ab4382d2SGreg Kroah-Hartman flg = TTY_BREAK; 1586ab4382d2SGreg Kroah-Hartman else if (status & ATMEL_US_PARE) 1587ab4382d2SGreg Kroah-Hartman flg = TTY_PARITY; 1588ab4382d2SGreg Kroah-Hartman else if (status & ATMEL_US_FRAME) 1589ab4382d2SGreg Kroah-Hartman flg = TTY_FRAME; 1590ab4382d2SGreg Kroah-Hartman } 1591ab4382d2SGreg Kroah-Hartman 1592ab4382d2SGreg Kroah-Hartman 1593ab4382d2SGreg Kroah-Hartman if (uart_handle_sysrq_char(port, c.ch)) 1594ab4382d2SGreg Kroah-Hartman continue; 1595ab4382d2SGreg Kroah-Hartman 1596ab4382d2SGreg Kroah-Hartman uart_insert_char(port, status, ATMEL_US_OVRE, c.ch, flg); 1597ab4382d2SGreg Kroah-Hartman } 1598ab4382d2SGreg Kroah-Hartman 1599ab4382d2SGreg Kroah-Hartman /* 1600ab4382d2SGreg Kroah-Hartman * Drop the lock here since it might end up calling 1601ab4382d2SGreg Kroah-Hartman * uart_start(), which takes the lock. 1602ab4382d2SGreg Kroah-Hartman */ 1603ab4382d2SGreg Kroah-Hartman spin_unlock(&port->lock); 16042e124b4aSJiri Slaby tty_flip_buffer_push(&port->state->port); 1605ab4382d2SGreg Kroah-Hartman spin_lock(&port->lock); 1606ab4382d2SGreg Kroah-Hartman } 1607ab4382d2SGreg Kroah-Hartman 1608a930e528SElen Song static void atmel_release_rx_pdc(struct uart_port *port) 1609a930e528SElen Song { 1610a930e528SElen Song struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 1611a930e528SElen Song int i; 1612a930e528SElen Song 1613a930e528SElen Song for (i = 0; i < 2; i++) { 1614a930e528SElen Song struct atmel_dma_buffer *pdc = &atmel_port->pdc_rx[i]; 1615a930e528SElen Song 1616a930e528SElen Song dma_unmap_single(port->dev, 1617a930e528SElen Song pdc->dma_addr, 1618a930e528SElen Song pdc->dma_size, 1619a930e528SElen Song DMA_FROM_DEVICE); 1620a930e528SElen Song kfree(pdc->buf); 1621a930e528SElen Song } 1622a930e528SElen Song } 1623a930e528SElen Song 162464e22ebeSElen Song static void atmel_rx_from_pdc(struct uart_port *port) 1625ab4382d2SGreg Kroah-Hartman { 1626ab4382d2SGreg Kroah-Hartman struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 162705c7cd39SJiri Slaby struct tty_port *tport = &port->state->port; 1628ab4382d2SGreg Kroah-Hartman struct atmel_dma_buffer *pdc; 1629ab4382d2SGreg Kroah-Hartman int rx_idx = atmel_port->pdc_rx_idx; 1630ab4382d2SGreg Kroah-Hartman unsigned int head; 1631ab4382d2SGreg Kroah-Hartman unsigned int tail; 1632ab4382d2SGreg Kroah-Hartman unsigned int count; 1633ab4382d2SGreg Kroah-Hartman 1634ab4382d2SGreg Kroah-Hartman do { 1635ab4382d2SGreg Kroah-Hartman /* Reset the UART timeout early so that we don't miss one */ 16364e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_STTTO); 1637ab4382d2SGreg Kroah-Hartman 1638ab4382d2SGreg Kroah-Hartman pdc = &atmel_port->pdc_rx[rx_idx]; 16394e7decdaSCyrille Pitchen head = atmel_uart_readl(port, ATMEL_PDC_RPR) - pdc->dma_addr; 1640ab4382d2SGreg Kroah-Hartman tail = pdc->ofs; 1641ab4382d2SGreg Kroah-Hartman 1642ab4382d2SGreg Kroah-Hartman /* If the PDC has switched buffers, RPR won't contain 1643ab4382d2SGreg Kroah-Hartman * any address within the current buffer. Since head 1644ab4382d2SGreg Kroah-Hartman * is unsigned, we just need a one-way comparison to 1645ab4382d2SGreg Kroah-Hartman * find out. 1646ab4382d2SGreg Kroah-Hartman * 1647ab4382d2SGreg Kroah-Hartman * In this case, we just need to consume the entire 1648ab4382d2SGreg Kroah-Hartman * buffer and resubmit it for DMA. This will clear the 1649ab4382d2SGreg Kroah-Hartman * ENDRX bit as well, so that we can safely re-enable 1650ab4382d2SGreg Kroah-Hartman * all interrupts below. 1651ab4382d2SGreg Kroah-Hartman */ 1652ab4382d2SGreg Kroah-Hartman head = min(head, pdc->dma_size); 1653ab4382d2SGreg Kroah-Hartman 1654ab4382d2SGreg Kroah-Hartman if (likely(head != tail)) { 1655ab4382d2SGreg Kroah-Hartman dma_sync_single_for_cpu(port->dev, pdc->dma_addr, 1656ab4382d2SGreg Kroah-Hartman pdc->dma_size, DMA_FROM_DEVICE); 1657ab4382d2SGreg Kroah-Hartman 1658ab4382d2SGreg Kroah-Hartman /* 1659ab4382d2SGreg Kroah-Hartman * head will only wrap around when we recycle 1660ab4382d2SGreg Kroah-Hartman * the DMA buffer, and when that happens, we 1661ab4382d2SGreg Kroah-Hartman * explicitly set tail to 0. So head will 1662ab4382d2SGreg Kroah-Hartman * always be greater than tail. 1663ab4382d2SGreg Kroah-Hartman */ 1664ab4382d2SGreg Kroah-Hartman count = head - tail; 1665ab4382d2SGreg Kroah-Hartman 166605c7cd39SJiri Slaby tty_insert_flip_string(tport, pdc->buf + pdc->ofs, 166705c7cd39SJiri Slaby count); 1668ab4382d2SGreg Kroah-Hartman 1669ab4382d2SGreg Kroah-Hartman dma_sync_single_for_device(port->dev, pdc->dma_addr, 1670ab4382d2SGreg Kroah-Hartman pdc->dma_size, DMA_FROM_DEVICE); 1671ab4382d2SGreg Kroah-Hartman 1672ab4382d2SGreg Kroah-Hartman port->icount.rx += count; 1673ab4382d2SGreg Kroah-Hartman pdc->ofs = head; 1674ab4382d2SGreg Kroah-Hartman } 1675ab4382d2SGreg Kroah-Hartman 1676ab4382d2SGreg Kroah-Hartman /* 1677ab4382d2SGreg Kroah-Hartman * If the current buffer is full, we need to check if 1678ab4382d2SGreg Kroah-Hartman * the next one contains any additional data. 1679ab4382d2SGreg Kroah-Hartman */ 1680ab4382d2SGreg Kroah-Hartman if (head >= pdc->dma_size) { 1681ab4382d2SGreg Kroah-Hartman pdc->ofs = 0; 16824e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_PDC_RNPR, pdc->dma_addr); 16834e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_PDC_RNCR, pdc->dma_size); 1684ab4382d2SGreg Kroah-Hartman 1685ab4382d2SGreg Kroah-Hartman rx_idx = !rx_idx; 1686ab4382d2SGreg Kroah-Hartman atmel_port->pdc_rx_idx = rx_idx; 1687ab4382d2SGreg Kroah-Hartman } 1688ab4382d2SGreg Kroah-Hartman } while (head >= pdc->dma_size); 1689ab4382d2SGreg Kroah-Hartman 1690ab4382d2SGreg Kroah-Hartman /* 1691ab4382d2SGreg Kroah-Hartman * Drop the lock here since it might end up calling 1692ab4382d2SGreg Kroah-Hartman * uart_start(), which takes the lock. 1693ab4382d2SGreg Kroah-Hartman */ 1694ab4382d2SGreg Kroah-Hartman spin_unlock(&port->lock); 16952e124b4aSJiri Slaby tty_flip_buffer_push(tport); 1696ab4382d2SGreg Kroah-Hartman spin_lock(&port->lock); 1697ab4382d2SGreg Kroah-Hartman 16984e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_IER, 16994e7decdaSCyrille Pitchen ATMEL_US_ENDRX | ATMEL_US_TIMEOUT); 1700ab4382d2SGreg Kroah-Hartman } 1701ab4382d2SGreg Kroah-Hartman 1702a930e528SElen Song static int atmel_prepare_rx_pdc(struct uart_port *port) 1703a930e528SElen Song { 1704a930e528SElen Song struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 1705a930e528SElen Song int i; 1706a930e528SElen Song 1707a930e528SElen Song for (i = 0; i < 2; i++) { 1708a930e528SElen Song struct atmel_dma_buffer *pdc = &atmel_port->pdc_rx[i]; 1709a930e528SElen Song 1710a930e528SElen Song pdc->buf = kmalloc(PDC_BUFFER_SIZE, GFP_KERNEL); 1711a930e528SElen Song if (pdc->buf == NULL) { 1712a930e528SElen Song if (i != 0) { 1713a930e528SElen Song dma_unmap_single(port->dev, 1714a930e528SElen Song atmel_port->pdc_rx[0].dma_addr, 1715a930e528SElen Song PDC_BUFFER_SIZE, 1716a930e528SElen Song DMA_FROM_DEVICE); 1717a930e528SElen Song kfree(atmel_port->pdc_rx[0].buf); 1718a930e528SElen Song } 1719a930e528SElen Song atmel_port->use_pdc_rx = 0; 1720a930e528SElen Song return -ENOMEM; 1721a930e528SElen Song } 1722a930e528SElen Song pdc->dma_addr = dma_map_single(port->dev, 1723a930e528SElen Song pdc->buf, 1724a930e528SElen Song PDC_BUFFER_SIZE, 1725a930e528SElen Song DMA_FROM_DEVICE); 1726a930e528SElen Song pdc->dma_size = PDC_BUFFER_SIZE; 1727a930e528SElen Song pdc->ofs = 0; 1728a930e528SElen Song } 1729a930e528SElen Song 1730a930e528SElen Song atmel_port->pdc_rx_idx = 0; 1731a930e528SElen Song 17324e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_PDC_RPR, atmel_port->pdc_rx[0].dma_addr); 17334e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_PDC_RCR, PDC_BUFFER_SIZE); 1734a930e528SElen Song 17354e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_PDC_RNPR, 17364e7decdaSCyrille Pitchen atmel_port->pdc_rx[1].dma_addr); 17374e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_PDC_RNCR, PDC_BUFFER_SIZE); 1738a930e528SElen Song 1739a930e528SElen Song return 0; 1740a930e528SElen Song } 1741a930e528SElen Song 1742ab4382d2SGreg Kroah-Hartman /* 1743ab4382d2SGreg Kroah-Hartman * tasklet handling tty stuff outside the interrupt handler. 1744ab4382d2SGreg Kroah-Hartman */ 174500e8e658SNicolas Ferre static void atmel_tasklet_rx_func(unsigned long data) 1746ab4382d2SGreg Kroah-Hartman { 1747ab4382d2SGreg Kroah-Hartman struct uart_port *port = (struct uart_port *)data; 1748ab4382d2SGreg Kroah-Hartman struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 1749ab4382d2SGreg Kroah-Hartman 1750ab4382d2SGreg Kroah-Hartman /* The interrupt handler does not take the lock */ 1751ab4382d2SGreg Kroah-Hartman spin_lock(&port->lock); 1752a930e528SElen Song atmel_port->schedule_rx(port); 175300e8e658SNicolas Ferre spin_unlock(&port->lock); 175400e8e658SNicolas Ferre } 1755ab4382d2SGreg Kroah-Hartman 175600e8e658SNicolas Ferre static void atmel_tasklet_tx_func(unsigned long data) 175700e8e658SNicolas Ferre { 175800e8e658SNicolas Ferre struct uart_port *port = (struct uart_port *)data; 175900e8e658SNicolas Ferre struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 176000e8e658SNicolas Ferre 176100e8e658SNicolas Ferre /* The interrupt handler does not take the lock */ 176200e8e658SNicolas Ferre spin_lock(&port->lock); 176300e8e658SNicolas Ferre atmel_port->schedule_tx(port); 1764ab4382d2SGreg Kroah-Hartman spin_unlock(&port->lock); 1765ab4382d2SGreg Kroah-Hartman } 1766ab4382d2SGreg Kroah-Hartman 17674a1e8888SLeilei Zhao static void atmel_init_property(struct atmel_uart_port *atmel_port, 176833d64c4fSElen Song struct platform_device *pdev) 176933d64c4fSElen Song { 177033d64c4fSElen Song struct device_node *np = pdev->dev.of_node; 177133d64c4fSElen Song 177233d64c4fSElen Song /* DMA/PDC usage specification */ 1773490d5ce2SJulia Lawall if (of_property_read_bool(np, "atmel,use-dma-rx")) { 1774490d5ce2SJulia Lawall if (of_property_read_bool(np, "dmas")) { 177533d64c4fSElen Song atmel_port->use_dma_rx = true; 177633d64c4fSElen Song atmel_port->use_pdc_rx = false; 177733d64c4fSElen Song } else { 177833d64c4fSElen Song atmel_port->use_dma_rx = false; 177933d64c4fSElen Song atmel_port->use_pdc_rx = true; 178033d64c4fSElen Song } 178133d64c4fSElen Song } else { 178233d64c4fSElen Song atmel_port->use_dma_rx = false; 178333d64c4fSElen Song atmel_port->use_pdc_rx = false; 178433d64c4fSElen Song } 178533d64c4fSElen Song 1786490d5ce2SJulia Lawall if (of_property_read_bool(np, "atmel,use-dma-tx")) { 1787490d5ce2SJulia Lawall if (of_property_read_bool(np, "dmas")) { 178833d64c4fSElen Song atmel_port->use_dma_tx = true; 178933d64c4fSElen Song atmel_port->use_pdc_tx = false; 179033d64c4fSElen Song } else { 179133d64c4fSElen Song atmel_port->use_dma_tx = false; 179233d64c4fSElen Song atmel_port->use_pdc_tx = true; 179333d64c4fSElen Song } 179433d64c4fSElen Song } else { 179533d64c4fSElen Song atmel_port->use_dma_tx = false; 179633d64c4fSElen Song atmel_port->use_pdc_tx = false; 179733d64c4fSElen Song } 179833d64c4fSElen Song } 179933d64c4fSElen Song 1800a930e528SElen Song static void atmel_set_ops(struct uart_port *port) 1801a930e528SElen Song { 1802a930e528SElen Song struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 1803a930e528SElen Song 180434df42f5SElen Song if (atmel_use_dma_rx(port)) { 180534df42f5SElen Song atmel_port->prepare_rx = &atmel_prepare_rx_dma; 180634df42f5SElen Song atmel_port->schedule_rx = &atmel_rx_from_dma; 180734df42f5SElen Song atmel_port->release_rx = &atmel_release_rx_dma; 180834df42f5SElen Song } else if (atmel_use_pdc_rx(port)) { 1809a930e528SElen Song atmel_port->prepare_rx = &atmel_prepare_rx_pdc; 1810a930e528SElen Song atmel_port->schedule_rx = &atmel_rx_from_pdc; 1811a930e528SElen Song atmel_port->release_rx = &atmel_release_rx_pdc; 1812a930e528SElen Song } else { 1813a930e528SElen Song atmel_port->prepare_rx = NULL; 1814a930e528SElen Song atmel_port->schedule_rx = &atmel_rx_from_ring; 1815a930e528SElen Song atmel_port->release_rx = NULL; 1816a930e528SElen Song } 1817a930e528SElen Song 181808f738beSElen Song if (atmel_use_dma_tx(port)) { 181908f738beSElen Song atmel_port->prepare_tx = &atmel_prepare_tx_dma; 182008f738beSElen Song atmel_port->schedule_tx = &atmel_tx_dma; 182108f738beSElen Song atmel_port->release_tx = &atmel_release_tx_dma; 182208f738beSElen Song } else if (atmel_use_pdc_tx(port)) { 1823a930e528SElen Song atmel_port->prepare_tx = &atmel_prepare_tx_pdc; 1824a930e528SElen Song atmel_port->schedule_tx = &atmel_tx_pdc; 1825a930e528SElen Song atmel_port->release_tx = &atmel_release_tx_pdc; 1826a930e528SElen Song } else { 1827a930e528SElen Song atmel_port->prepare_tx = NULL; 1828a930e528SElen Song atmel_port->schedule_tx = &atmel_tx_chars; 1829a930e528SElen Song atmel_port->release_tx = NULL; 1830a930e528SElen Song } 1831a930e528SElen Song } 1832a930e528SElen Song 1833ab4382d2SGreg Kroah-Hartman /* 1834055560b0SElen Song * Get ip name usart or uart 1835055560b0SElen Song */ 1836892db58bSNicolas Ferre static void atmel_get_ip_name(struct uart_port *port) 1837055560b0SElen Song { 1838055560b0SElen Song struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 18394e7decdaSCyrille Pitchen int name = atmel_uart_readl(port, ATMEL_US_NAME); 1840731d9caeSNicolas Ferre u32 version; 18411d673fb9SNicolas Ferre u32 usart, dbgu_uart, new_uart; 18424b769371SNicolas Ferre /* ASCII decoding for IP version */ 18434b769371SNicolas Ferre usart = 0x55534152; /* USAR(T) */ 18444b769371SNicolas Ferre dbgu_uart = 0x44424755; /* DBGU */ 18451d673fb9SNicolas Ferre new_uart = 0x55415254; /* UART */ 1846055560b0SElen Song 18475bf5635aSLudovic Desroches /* 18485bf5635aSLudovic Desroches * Only USART devices from at91sam9260 SOC implement fractional 18492867af2dSRomain Izard * baudrate. It is available for all asynchronous modes, with the 18502867af2dSRomain Izard * following restriction: the sampling clock's duty cycle is not 18512867af2dSRomain Izard * constant. 18525bf5635aSLudovic Desroches */ 18535bf5635aSLudovic Desroches atmel_port->has_frac_baudrate = false; 18544b769371SNicolas Ferre atmel_port->has_hw_timer = false; 1855055560b0SElen Song 18562958cceeSLudovic Desroches if (name == new_uart) { 18572958cceeSLudovic Desroches dev_dbg(port->dev, "Uart with hw timer"); 18584b769371SNicolas Ferre atmel_port->has_hw_timer = true; 18592958cceeSLudovic Desroches atmel_port->rtor = ATMEL_UA_RTOR; 18602958cceeSLudovic Desroches } else if (name == usart) { 18612958cceeSLudovic Desroches dev_dbg(port->dev, "Usart\n"); 18625bf5635aSLudovic Desroches atmel_port->has_frac_baudrate = true; 18632958cceeSLudovic Desroches atmel_port->has_hw_timer = true; 18642958cceeSLudovic Desroches atmel_port->rtor = ATMEL_US_RTOR; 1865*377fedd1SNicolas Ferre version = atmel_uart_readl(port, ATMEL_US_VERSION); 1866*377fedd1SNicolas Ferre switch (version) { 1867*377fedd1SNicolas Ferre case 0x814: /* sama5d2 */ 1868*377fedd1SNicolas Ferre /* fall through */ 1869*377fedd1SNicolas Ferre case 0x701: /* sama5d4 */ 1870*377fedd1SNicolas Ferre atmel_port->fidi_min = 3; 1871*377fedd1SNicolas Ferre atmel_port->fidi_max = 65535; 1872*377fedd1SNicolas Ferre break; 1873*377fedd1SNicolas Ferre case 0x502: /* sam9x5, sama5d3 */ 1874*377fedd1SNicolas Ferre atmel_port->fidi_min = 3; 1875*377fedd1SNicolas Ferre atmel_port->fidi_max = 2047; 1876*377fedd1SNicolas Ferre break; 1877*377fedd1SNicolas Ferre default: 1878*377fedd1SNicolas Ferre atmel_port->fidi_min = 1; 1879*377fedd1SNicolas Ferre atmel_port->fidi_max = 2047; 1880*377fedd1SNicolas Ferre } 18814b769371SNicolas Ferre } else if (name == dbgu_uart) { 18824b769371SNicolas Ferre dev_dbg(port->dev, "Dbgu or uart without hw timer\n"); 1883055560b0SElen Song } else { 1884731d9caeSNicolas Ferre /* fallback for older SoCs: use version field */ 18854e7decdaSCyrille Pitchen version = atmel_uart_readl(port, ATMEL_US_VERSION); 1886731d9caeSNicolas Ferre switch (version) { 1887731d9caeSNicolas Ferre case 0x302: 1888731d9caeSNicolas Ferre case 0x10213: 1889fd63a890SJonas Danielsson case 0x10302: 1890731d9caeSNicolas Ferre dev_dbg(port->dev, "This version is usart\n"); 18915bf5635aSLudovic Desroches atmel_port->has_frac_baudrate = true; 18924b769371SNicolas Ferre atmel_port->has_hw_timer = true; 18932958cceeSLudovic Desroches atmel_port->rtor = ATMEL_US_RTOR; 1894731d9caeSNicolas Ferre break; 1895731d9caeSNicolas Ferre case 0x203: 1896731d9caeSNicolas Ferre case 0x10202: 1897731d9caeSNicolas Ferre dev_dbg(port->dev, "This version is uart\n"); 1898731d9caeSNicolas Ferre break; 1899731d9caeSNicolas Ferre default: 1900731d9caeSNicolas Ferre dev_err(port->dev, "Not supported ip name nor version, set to uart\n"); 1901731d9caeSNicolas Ferre } 1902055560b0SElen Song } 1903055560b0SElen Song } 1904055560b0SElen Song 1905055560b0SElen Song /* 1906ab4382d2SGreg Kroah-Hartman * Perform initialization and enable port for reception 1907ab4382d2SGreg Kroah-Hartman */ 1908ab4382d2SGreg Kroah-Hartman static int atmel_startup(struct uart_port *port) 1909ab4382d2SGreg Kroah-Hartman { 191033d64c4fSElen Song struct platform_device *pdev = to_platform_device(port->dev); 1911ab4382d2SGreg Kroah-Hartman struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 1912ab4382d2SGreg Kroah-Hartman int retval; 1913ab4382d2SGreg Kroah-Hartman 1914ab4382d2SGreg Kroah-Hartman /* 1915ab4382d2SGreg Kroah-Hartman * Ensure that no interrupts are enabled otherwise when 1916ab4382d2SGreg Kroah-Hartman * request_irq() is called we could get stuck trying to 1917ab4382d2SGreg Kroah-Hartman * handle an unexpected interrupt 1918ab4382d2SGreg Kroah-Hartman */ 19194e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_IDR, -1); 1920ab5e4e41SRichard Genoud atmel_port->ms_irq_enabled = false; 1921ab4382d2SGreg Kroah-Hartman 1922ab4382d2SGreg Kroah-Hartman /* 1923ab4382d2SGreg Kroah-Hartman * Allocate the IRQ 1924ab4382d2SGreg Kroah-Hartman */ 19252c7af5baSBoris BREZILLON retval = request_irq(port->irq, atmel_interrupt, 19262c7af5baSBoris BREZILLON IRQF_SHARED | IRQF_COND_SUSPEND, 19279594b5beSSebastian Andrzej Siewior dev_name(&pdev->dev), port); 1928ab4382d2SGreg Kroah-Hartman if (retval) { 1929ddaa6037SRichard Genoud dev_err(port->dev, "atmel_startup - Can't get irq\n"); 1930ab4382d2SGreg Kroah-Hartman return retval; 1931ab4382d2SGreg Kroah-Hartman } 1932ab4382d2SGreg Kroah-Hartman 193398f2082cSNicolas Ferre atomic_set(&atmel_port->tasklet_shutdown, 0); 193498f2082cSNicolas Ferre tasklet_init(&atmel_port->tasklet_rx, atmel_tasklet_rx_func, 193598f2082cSNicolas Ferre (unsigned long)port); 193698f2082cSNicolas Ferre tasklet_init(&atmel_port->tasklet_tx, atmel_tasklet_tx_func, 193798f2082cSNicolas Ferre (unsigned long)port); 19381e125786SLeilei Zhao 1939ab5e4e41SRichard Genoud /* 1940ab4382d2SGreg Kroah-Hartman * Initialize DMA (if necessary) 1941ab4382d2SGreg Kroah-Hartman */ 194233d64c4fSElen Song atmel_init_property(atmel_port, pdev); 19434d9628a1SLeilei Zhao atmel_set_ops(port); 194433d64c4fSElen Song 1945a930e528SElen Song if (atmel_port->prepare_rx) { 1946a930e528SElen Song retval = atmel_port->prepare_rx(port); 1947a930e528SElen Song if (retval < 0) 1948a930e528SElen Song atmel_set_ops(port); 1949ab4382d2SGreg Kroah-Hartman } 1950ab4382d2SGreg Kroah-Hartman 1951a930e528SElen Song if (atmel_port->prepare_tx) { 1952a930e528SElen Song retval = atmel_port->prepare_tx(port); 1953a930e528SElen Song if (retval < 0) 1954a930e528SElen Song atmel_set_ops(port); 1955ab4382d2SGreg Kroah-Hartman } 1956ab4382d2SGreg Kroah-Hartman 1957b5199d46SCyrille Pitchen /* 1958b5199d46SCyrille Pitchen * Enable FIFO when available 1959b5199d46SCyrille Pitchen */ 1960b5199d46SCyrille Pitchen if (atmel_port->fifo_size) { 1961b5199d46SCyrille Pitchen unsigned int txrdym = ATMEL_US_ONE_DATA; 1962b5199d46SCyrille Pitchen unsigned int rxrdym = ATMEL_US_ONE_DATA; 1963b5199d46SCyrille Pitchen unsigned int fmr; 1964b5199d46SCyrille Pitchen 1965b5199d46SCyrille Pitchen atmel_uart_writel(port, ATMEL_US_CR, 1966b5199d46SCyrille Pitchen ATMEL_US_FIFOEN | 1967b5199d46SCyrille Pitchen ATMEL_US_RXFCLR | 1968b5199d46SCyrille Pitchen ATMEL_US_TXFLCLR); 1969b5199d46SCyrille Pitchen 19705f258b3eSCyrille Pitchen if (atmel_use_dma_tx(port)) 19715f258b3eSCyrille Pitchen txrdym = ATMEL_US_FOUR_DATA; 19725f258b3eSCyrille Pitchen 1973b5199d46SCyrille Pitchen fmr = ATMEL_US_TXRDYM(txrdym) | ATMEL_US_RXRDYM(rxrdym); 1974b5199d46SCyrille Pitchen if (atmel_port->rts_high && 1975b5199d46SCyrille Pitchen atmel_port->rts_low) 1976b5199d46SCyrille Pitchen fmr |= ATMEL_US_FRTSC | 1977b5199d46SCyrille Pitchen ATMEL_US_RXFTHRES(atmel_port->rts_high) | 1978b5199d46SCyrille Pitchen ATMEL_US_RXFTHRES2(atmel_port->rts_low); 1979b5199d46SCyrille Pitchen 1980b5199d46SCyrille Pitchen atmel_uart_writel(port, ATMEL_US_FMR, fmr); 1981b5199d46SCyrille Pitchen } 1982b5199d46SCyrille Pitchen 1983ab4382d2SGreg Kroah-Hartman /* Save current CSR for comparison in atmel_tasklet_func() */ 1984e0b0baadSRichard Genoud atmel_port->irq_status_prev = atmel_get_lines_status(port); 1985ab4382d2SGreg Kroah-Hartman 1986ab4382d2SGreg Kroah-Hartman /* 1987ab4382d2SGreg Kroah-Hartman * Finally, enable the serial port 1988ab4382d2SGreg Kroah-Hartman */ 19894e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA | ATMEL_US_RSTRX); 1990ab4382d2SGreg Kroah-Hartman /* enable xmit & rcvr */ 19914e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_TXEN | ATMEL_US_RXEN); 1992ea04f82aSRomain Izard atmel_port->tx_stopped = false; 1993ab4382d2SGreg Kroah-Hartman 1994026cb432SKees Cook timer_setup(&atmel_port->uart_timer, atmel_uart_timer_callback, 0); 19958bc661bfSMarek Roszko 19968bc661bfSMarek Roszko if (atmel_use_pdc_rx(port)) { 19978bc661bfSMarek Roszko /* set UART timeout */ 19984b769371SNicolas Ferre if (!atmel_port->has_hw_timer) { 19992e68c22fSElen Song mod_timer(&atmel_port->uart_timer, 20002e68c22fSElen Song jiffies + uart_poll_timeout(port)); 20012e68c22fSElen Song /* set USART timeout */ 20022e68c22fSElen Song } else { 20032958cceeSLudovic Desroches atmel_uart_writel(port, atmel_port->rtor, 20042958cceeSLudovic Desroches PDC_RX_TIMEOUT); 20054e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_STTTO); 2006ab4382d2SGreg Kroah-Hartman 20074e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_IER, 20084e7decdaSCyrille Pitchen ATMEL_US_ENDRX | ATMEL_US_TIMEOUT); 20092e68c22fSElen Song } 2010ab4382d2SGreg Kroah-Hartman /* enable PDC controller */ 20114e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_RXTEN); 201234df42f5SElen Song } else if (atmel_use_dma_rx(port)) { 20132e68c22fSElen Song /* set UART timeout */ 20144b769371SNicolas Ferre if (!atmel_port->has_hw_timer) { 20152e68c22fSElen Song mod_timer(&atmel_port->uart_timer, 20162e68c22fSElen Song jiffies + uart_poll_timeout(port)); 20172e68c22fSElen Song /* set USART timeout */ 20182e68c22fSElen Song } else { 20192958cceeSLudovic Desroches atmel_uart_writel(port, atmel_port->rtor, 20202958cceeSLudovic Desroches PDC_RX_TIMEOUT); 20214e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_STTTO); 202234df42f5SElen Song 20234e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_IER, 20244e7decdaSCyrille Pitchen ATMEL_US_TIMEOUT); 20252e68c22fSElen Song } 2026ab4382d2SGreg Kroah-Hartman } else { 2027ab4382d2SGreg Kroah-Hartman /* enable receive only */ 20284e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_IER, ATMEL_US_RXRDY); 2029ab4382d2SGreg Kroah-Hartman } 2030ab4382d2SGreg Kroah-Hartman 2031ab4382d2SGreg Kroah-Hartman return 0; 2032ab4382d2SGreg Kroah-Hartman } 2033ab4382d2SGreg Kroah-Hartman 2034ab4382d2SGreg Kroah-Hartman /* 2035479e9b94SPeter Hurley * Flush any TX data submitted for DMA. Called when the TX circular 2036479e9b94SPeter Hurley * buffer is reset. 2037479e9b94SPeter Hurley */ 2038479e9b94SPeter Hurley static void atmel_flush_buffer(struct uart_port *port) 2039479e9b94SPeter Hurley { 2040479e9b94SPeter Hurley struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 2041479e9b94SPeter Hurley 2042479e9b94SPeter Hurley if (atmel_use_pdc_tx(port)) { 20434e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_PDC_TCR, 0); 2044479e9b94SPeter Hurley atmel_port->pdc_tx.ofs = 0; 2045479e9b94SPeter Hurley } 204631ca2c63SRichard Genoud /* 204731ca2c63SRichard Genoud * in uart_flush_buffer(), the xmit circular buffer has just 204831ca2c63SRichard Genoud * been cleared, so we have to reset tx_len accordingly. 204931ca2c63SRichard Genoud */ 205031ca2c63SRichard Genoud atmel_port->tx_len = 0; 2051479e9b94SPeter Hurley } 2052479e9b94SPeter Hurley 2053479e9b94SPeter Hurley /* 2054ab4382d2SGreg Kroah-Hartman * Disable the port 2055ab4382d2SGreg Kroah-Hartman */ 2056ab4382d2SGreg Kroah-Hartman static void atmel_shutdown(struct uart_port *port) 2057ab4382d2SGreg Kroah-Hartman { 2058ab4382d2SGreg Kroah-Hartman struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 20590cc7c6c7SMarek Roszko 20600ae9fdefSRichard Genoud /* Disable modem control lines interrupts */ 20610ae9fdefSRichard Genoud atmel_disable_ms(port); 20620ae9fdefSRichard Genoud 206398f2082cSNicolas Ferre /* Disable interrupts at device level */ 206498f2082cSNicolas Ferre atmel_uart_writel(port, ATMEL_US_IDR, -1); 206598f2082cSNicolas Ferre 206698f2082cSNicolas Ferre /* Prevent spurious interrupts from scheduling the tasklet */ 206798f2082cSNicolas Ferre atomic_inc(&atmel_port->tasklet_shutdown); 206898f2082cSNicolas Ferre 2069ab4382d2SGreg Kroah-Hartman /* 20708bc661bfSMarek Roszko * Prevent any tasklets being scheduled during 20718bc661bfSMarek Roszko * cleanup 20728bc661bfSMarek Roszko */ 20738bc661bfSMarek Roszko del_timer_sync(&atmel_port->uart_timer); 20748bc661bfSMarek Roszko 207598f2082cSNicolas Ferre /* Make sure that no interrupt is on the fly */ 207698f2082cSNicolas Ferre synchronize_irq(port->irq); 207798f2082cSNicolas Ferre 20788bc661bfSMarek Roszko /* 20790cc7c6c7SMarek Roszko * Clear out any scheduled tasklets before 20800cc7c6c7SMarek Roszko * we destroy the buffers 20810cc7c6c7SMarek Roszko */ 208200e8e658SNicolas Ferre tasklet_kill(&atmel_port->tasklet_rx); 208300e8e658SNicolas Ferre tasklet_kill(&atmel_port->tasklet_tx); 20840cc7c6c7SMarek Roszko 20850cc7c6c7SMarek Roszko /* 20860cc7c6c7SMarek Roszko * Ensure everything is stopped and 208798f2082cSNicolas Ferre * disable port and break condition. 2088ab4382d2SGreg Kroah-Hartman */ 2089ab4382d2SGreg Kroah-Hartman atmel_stop_rx(port); 2090ab4382d2SGreg Kroah-Hartman atmel_stop_tx(port); 2091ab4382d2SGreg Kroah-Hartman 20924e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA); 20930cc7c6c7SMarek Roszko 2094ab4382d2SGreg Kroah-Hartman /* 2095ab4382d2SGreg Kroah-Hartman * Shut-down the DMA. 2096ab4382d2SGreg Kroah-Hartman */ 2097a930e528SElen Song if (atmel_port->release_rx) 2098a930e528SElen Song atmel_port->release_rx(port); 2099a930e528SElen Song if (atmel_port->release_tx) 2100a930e528SElen Song atmel_port->release_tx(port); 2101ab4382d2SGreg Kroah-Hartman 2102ab4382d2SGreg Kroah-Hartman /* 2103bb7e73c5SMark Deneen * Reset ring buffer pointers 2104bb7e73c5SMark Deneen */ 2105bb7e73c5SMark Deneen atmel_port->rx_ring.head = 0; 2106bb7e73c5SMark Deneen atmel_port->rx_ring.tail = 0; 2107bb7e73c5SMark Deneen 2108bb7e73c5SMark Deneen /* 2109ab5e4e41SRichard Genoud * Free the interrupts 2110ab4382d2SGreg Kroah-Hartman */ 2111ab4382d2SGreg Kroah-Hartman free_irq(port->irq, port); 2112ab5e4e41SRichard Genoud 2113479e9b94SPeter Hurley atmel_flush_buffer(port); 2114ab4382d2SGreg Kroah-Hartman } 2115ab4382d2SGreg Kroah-Hartman 2116ab4382d2SGreg Kroah-Hartman /* 2117ab4382d2SGreg Kroah-Hartman * Power / Clock management. 2118ab4382d2SGreg Kroah-Hartman */ 2119ab4382d2SGreg Kroah-Hartman static void atmel_serial_pm(struct uart_port *port, unsigned int state, 2120ab4382d2SGreg Kroah-Hartman unsigned int oldstate) 2121ab4382d2SGreg Kroah-Hartman { 2122ab4382d2SGreg Kroah-Hartman struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 2123ab4382d2SGreg Kroah-Hartman 2124ab4382d2SGreg Kroah-Hartman switch (state) { 2125ab4382d2SGreg Kroah-Hartman case 0: 2126ab4382d2SGreg Kroah-Hartman /* 2127ab4382d2SGreg Kroah-Hartman * Enable the peripheral clock for this serial port. 2128ab4382d2SGreg Kroah-Hartman * This is called on uart_open() or a resume event. 2129ab4382d2SGreg Kroah-Hartman */ 213091f8c2d8SBoris BREZILLON clk_prepare_enable(atmel_port->clk); 2131ab4382d2SGreg Kroah-Hartman 2132ab4382d2SGreg Kroah-Hartman /* re-enable interrupts if we disabled some on suspend */ 21334e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_IER, atmel_port->backup_imr); 2134ab4382d2SGreg Kroah-Hartman break; 2135ab4382d2SGreg Kroah-Hartman case 3: 2136ab4382d2SGreg Kroah-Hartman /* Back up the interrupt mask and disable all interrupts */ 21374e7decdaSCyrille Pitchen atmel_port->backup_imr = atmel_uart_readl(port, ATMEL_US_IMR); 21384e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_IDR, -1); 2139ab4382d2SGreg Kroah-Hartman 2140ab4382d2SGreg Kroah-Hartman /* 2141ab4382d2SGreg Kroah-Hartman * Disable the peripheral clock for this serial port. 2142ab4382d2SGreg Kroah-Hartman * This is called on uart_close() or a suspend event. 2143ab4382d2SGreg Kroah-Hartman */ 214491f8c2d8SBoris BREZILLON clk_disable_unprepare(atmel_port->clk); 2145ab4382d2SGreg Kroah-Hartman break; 2146ab4382d2SGreg Kroah-Hartman default: 2147ddaa6037SRichard Genoud dev_err(port->dev, "atmel_serial: unknown pm %d\n", state); 2148ab4382d2SGreg Kroah-Hartman } 2149ab4382d2SGreg Kroah-Hartman } 2150ab4382d2SGreg Kroah-Hartman 2151ab4382d2SGreg Kroah-Hartman /* 2152ab4382d2SGreg Kroah-Hartman * Change the port parameters 2153ab4382d2SGreg Kroah-Hartman */ 2154ab4382d2SGreg Kroah-Hartman static void atmel_set_termios(struct uart_port *port, struct ktermios *termios, 2155ab4382d2SGreg Kroah-Hartman struct ktermios *old) 2156ab4382d2SGreg Kroah-Hartman { 21575bf5635aSLudovic Desroches struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 2158ab4382d2SGreg Kroah-Hartman unsigned long flags; 21595bf5635aSLudovic Desroches unsigned int old_mode, mode, imr, quot, baud, div, cd, fp = 0; 2160ab4382d2SGreg Kroah-Hartman 21611cf6e8fcSCyrille Pitchen /* save the current mode register */ 21624e7decdaSCyrille Pitchen mode = old_mode = atmel_uart_readl(port, ATMEL_US_MR); 21631cf6e8fcSCyrille Pitchen 21641cf6e8fcSCyrille Pitchen /* reset the mode, clock divisor, parity, stop bits and data size */ 21651cf6e8fcSCyrille Pitchen mode &= ~(ATMEL_US_USCLKS | ATMEL_US_CHRL | ATMEL_US_NBSTOP | 21661cf6e8fcSCyrille Pitchen ATMEL_US_PAR | ATMEL_US_USMODE); 2167ab4382d2SGreg Kroah-Hartman 2168ab4382d2SGreg Kroah-Hartman baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk / 16); 2169ab4382d2SGreg Kroah-Hartman 2170ab4382d2SGreg Kroah-Hartman /* byte size */ 2171ab4382d2SGreg Kroah-Hartman switch (termios->c_cflag & CSIZE) { 2172ab4382d2SGreg Kroah-Hartman case CS5: 2173ab4382d2SGreg Kroah-Hartman mode |= ATMEL_US_CHRL_5; 2174ab4382d2SGreg Kroah-Hartman break; 2175ab4382d2SGreg Kroah-Hartman case CS6: 2176ab4382d2SGreg Kroah-Hartman mode |= ATMEL_US_CHRL_6; 2177ab4382d2SGreg Kroah-Hartman break; 2178ab4382d2SGreg Kroah-Hartman case CS7: 2179ab4382d2SGreg Kroah-Hartman mode |= ATMEL_US_CHRL_7; 2180ab4382d2SGreg Kroah-Hartman break; 2181ab4382d2SGreg Kroah-Hartman default: 2182ab4382d2SGreg Kroah-Hartman mode |= ATMEL_US_CHRL_8; 2183ab4382d2SGreg Kroah-Hartman break; 2184ab4382d2SGreg Kroah-Hartman } 2185ab4382d2SGreg Kroah-Hartman 2186ab4382d2SGreg Kroah-Hartman /* stop bits */ 2187ab4382d2SGreg Kroah-Hartman if (termios->c_cflag & CSTOPB) 2188ab4382d2SGreg Kroah-Hartman mode |= ATMEL_US_NBSTOP_2; 2189ab4382d2SGreg Kroah-Hartman 2190ab4382d2SGreg Kroah-Hartman /* parity */ 2191ab4382d2SGreg Kroah-Hartman if (termios->c_cflag & PARENB) { 2192ab4382d2SGreg Kroah-Hartman /* Mark or Space parity */ 2193ab4382d2SGreg Kroah-Hartman if (termios->c_cflag & CMSPAR) { 2194ab4382d2SGreg Kroah-Hartman if (termios->c_cflag & PARODD) 2195ab4382d2SGreg Kroah-Hartman mode |= ATMEL_US_PAR_MARK; 2196ab4382d2SGreg Kroah-Hartman else 2197ab4382d2SGreg Kroah-Hartman mode |= ATMEL_US_PAR_SPACE; 2198ab4382d2SGreg Kroah-Hartman } else if (termios->c_cflag & PARODD) 2199ab4382d2SGreg Kroah-Hartman mode |= ATMEL_US_PAR_ODD; 2200ab4382d2SGreg Kroah-Hartman else 2201ab4382d2SGreg Kroah-Hartman mode |= ATMEL_US_PAR_EVEN; 2202ab4382d2SGreg Kroah-Hartman } else 2203ab4382d2SGreg Kroah-Hartman mode |= ATMEL_US_PAR_NONE; 2204ab4382d2SGreg Kroah-Hartman 2205ab4382d2SGreg Kroah-Hartman spin_lock_irqsave(&port->lock, flags); 2206ab4382d2SGreg Kroah-Hartman 2207ab4382d2SGreg Kroah-Hartman port->read_status_mask = ATMEL_US_OVRE; 2208ab4382d2SGreg Kroah-Hartman if (termios->c_iflag & INPCK) 2209ab4382d2SGreg Kroah-Hartman port->read_status_mask |= (ATMEL_US_FRAME | ATMEL_US_PARE); 2210ef8b9ddcSPeter Hurley if (termios->c_iflag & (IGNBRK | BRKINT | PARMRK)) 2211ab4382d2SGreg Kroah-Hartman port->read_status_mask |= ATMEL_US_RXBRK; 2212ab4382d2SGreg Kroah-Hartman 221364e22ebeSElen Song if (atmel_use_pdc_rx(port)) 2214ab4382d2SGreg Kroah-Hartman /* need to enable error interrupts */ 22154e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_IER, port->read_status_mask); 2216ab4382d2SGreg Kroah-Hartman 2217ab4382d2SGreg Kroah-Hartman /* 2218ab4382d2SGreg Kroah-Hartman * Characters to ignore 2219ab4382d2SGreg Kroah-Hartman */ 2220ab4382d2SGreg Kroah-Hartman port->ignore_status_mask = 0; 2221ab4382d2SGreg Kroah-Hartman if (termios->c_iflag & IGNPAR) 2222ab4382d2SGreg Kroah-Hartman port->ignore_status_mask |= (ATMEL_US_FRAME | ATMEL_US_PARE); 2223ab4382d2SGreg Kroah-Hartman if (termios->c_iflag & IGNBRK) { 2224ab4382d2SGreg Kroah-Hartman port->ignore_status_mask |= ATMEL_US_RXBRK; 2225ab4382d2SGreg Kroah-Hartman /* 2226ab4382d2SGreg Kroah-Hartman * If we're ignoring parity and break indicators, 2227ab4382d2SGreg Kroah-Hartman * ignore overruns too (for real raw support). 2228ab4382d2SGreg Kroah-Hartman */ 2229ab4382d2SGreg Kroah-Hartman if (termios->c_iflag & IGNPAR) 2230ab4382d2SGreg Kroah-Hartman port->ignore_status_mask |= ATMEL_US_OVRE; 2231ab4382d2SGreg Kroah-Hartman } 2232ab4382d2SGreg Kroah-Hartman /* TODO: Ignore all characters if CREAD is set.*/ 2233ab4382d2SGreg Kroah-Hartman 2234ab4382d2SGreg Kroah-Hartman /* update the per-port timeout */ 2235ab4382d2SGreg Kroah-Hartman uart_update_timeout(port, termios->c_cflag, baud); 2236ab4382d2SGreg Kroah-Hartman 2237ab4382d2SGreg Kroah-Hartman /* 2238ab4382d2SGreg Kroah-Hartman * save/disable interrupts. The tty layer will ensure that the 2239ab4382d2SGreg Kroah-Hartman * transmitter is empty if requested by the caller, so there's 2240ab4382d2SGreg Kroah-Hartman * no need to wait for it here. 2241ab4382d2SGreg Kroah-Hartman */ 22424e7decdaSCyrille Pitchen imr = atmel_uart_readl(port, ATMEL_US_IMR); 22434e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_IDR, -1); 2244ab4382d2SGreg Kroah-Hartman 2245ab4382d2SGreg Kroah-Hartman /* disable receiver and transmitter */ 22464e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_TXDIS | ATMEL_US_RXDIS); 2247ea04f82aSRomain Izard atmel_port->tx_stopped = true; 2248ab4382d2SGreg Kroah-Hartman 22491cf6e8fcSCyrille Pitchen /* mode */ 225013bd3e6fSRicardo Ribalda Delgado if (port->rs485.flags & SER_RS485_ENABLED) { 22514e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_TTGR, 22524e7decdaSCyrille Pitchen port->rs485.delay_rts_after_send); 2253ab4382d2SGreg Kroah-Hartman mode |= ATMEL_US_USMODE_RS485; 2254*377fedd1SNicolas Ferre } else if (port->iso7816.flags & SER_ISO7816_ENABLED) { 2255*377fedd1SNicolas Ferre atmel_uart_writel(port, ATMEL_US_TTGR, port->iso7816.tg); 2256*377fedd1SNicolas Ferre /* select mck clock, and output */ 2257*377fedd1SNicolas Ferre mode |= ATMEL_US_USCLKS_MCK | ATMEL_US_CLKO; 2258*377fedd1SNicolas Ferre /* set max iterations */ 2259*377fedd1SNicolas Ferre mode |= ATMEL_US_MAX_ITER(3); 2260*377fedd1SNicolas Ferre if ((port->iso7816.flags & SER_ISO7816_T_PARAM) 2261*377fedd1SNicolas Ferre == SER_ISO7816_T(0)) 2262*377fedd1SNicolas Ferre mode |= ATMEL_US_USMODE_ISO7816_T0; 2263*377fedd1SNicolas Ferre else 2264*377fedd1SNicolas Ferre mode |= ATMEL_US_USMODE_ISO7816_T1; 22651cf6e8fcSCyrille Pitchen } else if (termios->c_cflag & CRTSCTS) { 22661cf6e8fcSCyrille Pitchen /* RS232 with hardware handshake (RTS/CTS) */ 22679bcffe75SRichard Genoud if (atmel_use_fifo(port) && 22689bcffe75SRichard Genoud !mctrl_gpio_to_gpiod(atmel_port->gpios, UART_GPIO_CTS)) { 22699bcffe75SRichard Genoud /* 22709bcffe75SRichard Genoud * with ATMEL_US_USMODE_HWHS set, the controller will 22719bcffe75SRichard Genoud * be able to drive the RTS pin high/low when the RX 22729bcffe75SRichard Genoud * FIFO is above RXFTHRES/below RXFTHRES2. 22739bcffe75SRichard Genoud * It will also disable the transmitter when the CTS 22749bcffe75SRichard Genoud * pin is high. 22759bcffe75SRichard Genoud * This mode is not activated if CTS pin is a GPIO 22769bcffe75SRichard Genoud * because in this case, the transmitter is always 22779bcffe75SRichard Genoud * disabled (there must be an internal pull-up 22789bcffe75SRichard Genoud * responsible for this behaviour). 22799bcffe75SRichard Genoud * If the RTS pin is a GPIO, the controller won't be 22809bcffe75SRichard Genoud * able to drive it according to the FIFO thresholds, 22819bcffe75SRichard Genoud * but it will be handled by the driver. 22829bcffe75SRichard Genoud */ 22831cf6e8fcSCyrille Pitchen mode |= ATMEL_US_USMODE_HWHS; 22849bcffe75SRichard Genoud } else { 22859bcffe75SRichard Genoud /* 22869bcffe75SRichard Genoud * For platforms without FIFO, the flow control is 22879bcffe75SRichard Genoud * handled by the driver. 22889bcffe75SRichard Genoud */ 22899bcffe75SRichard Genoud mode |= ATMEL_US_USMODE_NORMAL; 22905be605acSAlexandre Belloni } 22911cf6e8fcSCyrille Pitchen } else { 22921cf6e8fcSCyrille Pitchen /* RS232 without hadware handshake */ 22931cf6e8fcSCyrille Pitchen mode |= ATMEL_US_USMODE_NORMAL; 2294ab4382d2SGreg Kroah-Hartman } 2295ab4382d2SGreg Kroah-Hartman 22961cf6e8fcSCyrille Pitchen /* set the mode, clock divisor, parity, stop bits and data size */ 22974e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_MR, mode); 2298ab4382d2SGreg Kroah-Hartman 22991cf6e8fcSCyrille Pitchen /* 23001cf6e8fcSCyrille Pitchen * when switching the mode, set the RTS line state according to the 23011cf6e8fcSCyrille Pitchen * new mode, otherwise keep the former state 23021cf6e8fcSCyrille Pitchen */ 23031cf6e8fcSCyrille Pitchen if ((old_mode & ATMEL_US_USMODE) != (mode & ATMEL_US_USMODE)) { 23041cf6e8fcSCyrille Pitchen unsigned int rts_state; 23051cf6e8fcSCyrille Pitchen 23061cf6e8fcSCyrille Pitchen if ((mode & ATMEL_US_USMODE) == ATMEL_US_USMODE_HWHS) { 23071cf6e8fcSCyrille Pitchen /* let the hardware control the RTS line */ 23081cf6e8fcSCyrille Pitchen rts_state = ATMEL_US_RTSDIS; 23091cf6e8fcSCyrille Pitchen } else { 23101cf6e8fcSCyrille Pitchen /* force RTS line to low level */ 23111cf6e8fcSCyrille Pitchen rts_state = ATMEL_US_RTSEN; 23121cf6e8fcSCyrille Pitchen } 23131cf6e8fcSCyrille Pitchen 23144e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_CR, rts_state); 23151cf6e8fcSCyrille Pitchen } 23161cf6e8fcSCyrille Pitchen 23175bf5635aSLudovic Desroches /* 23185bf5635aSLudovic Desroches * Set the baud rate: 23195bf5635aSLudovic Desroches * Fractional baudrate allows to setup output frequency more 23205bf5635aSLudovic Desroches * accurately. This feature is enabled only when using normal mode. 23215bf5635aSLudovic Desroches * baudrate = selected clock / (8 * (2 - OVER) * (CD + FP / 8)) 23225bf5635aSLudovic Desroches * Currently, OVER is always set to 0 so we get 232336131cdfSAlexey Starikovskiy * baudrate = selected clock / (16 * (CD + FP / 8)) 232436131cdfSAlexey Starikovskiy * then 232536131cdfSAlexey Starikovskiy * 8 CD + FP = selected clock / (2 * baudrate) 23265bf5635aSLudovic Desroches */ 23272867af2dSRomain Izard if (atmel_port->has_frac_baudrate) { 232836131cdfSAlexey Starikovskiy div = DIV_ROUND_CLOSEST(port->uartclk, baud * 2); 232936131cdfSAlexey Starikovskiy cd = div >> 3; 233036131cdfSAlexey Starikovskiy fp = div & ATMEL_US_FP_MASK; 23315bf5635aSLudovic Desroches } else { 23325bf5635aSLudovic Desroches cd = uart_get_divisor(port, baud); 23335bf5635aSLudovic Desroches } 23345bf5635aSLudovic Desroches 23355bf5635aSLudovic Desroches if (cd > 65535) { /* BRGR is 16-bit, so switch to slower clock */ 23365bf5635aSLudovic Desroches cd /= 8; 23375bf5635aSLudovic Desroches mode |= ATMEL_US_USCLKS_MCK_DIV8; 23385bf5635aSLudovic Desroches } 23395bf5635aSLudovic Desroches quot = cd | fp << ATMEL_US_FP_OFFSET; 23405bf5635aSLudovic Desroches 2341*377fedd1SNicolas Ferre if (!(port->iso7816.flags & SER_ISO7816_ENABLED)) 23424e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_BRGR, quot); 23434e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA | ATMEL_US_RSTRX); 23444e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_TXEN | ATMEL_US_RXEN); 2345ea04f82aSRomain Izard atmel_port->tx_stopped = false; 2346ab4382d2SGreg Kroah-Hartman 2347ab4382d2SGreg Kroah-Hartman /* restore interrupts */ 23484e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_IER, imr); 2349ab4382d2SGreg Kroah-Hartman 2350ab4382d2SGreg Kroah-Hartman /* CTS flow-control and modem-status interrupts */ 2351ab4382d2SGreg Kroah-Hartman if (UART_ENABLE_MS(port, termios->c_cflag)) 235235b675b9SRichard Genoud atmel_enable_ms(port); 235335b675b9SRichard Genoud else 235435b675b9SRichard Genoud atmel_disable_ms(port); 2355ab4382d2SGreg Kroah-Hartman 2356ab4382d2SGreg Kroah-Hartman spin_unlock_irqrestore(&port->lock, flags); 2357ab4382d2SGreg Kroah-Hartman } 2358ab4382d2SGreg Kroah-Hartman 2359732a84a0SPeter Hurley static void atmel_set_ldisc(struct uart_port *port, struct ktermios *termios) 236042bd7a4fSViktar Palstsiuk { 2361732a84a0SPeter Hurley if (termios->c_line == N_PPS) { 236242bd7a4fSViktar Palstsiuk port->flags |= UPF_HARDPPS_CD; 2363d41510ceSPeter Hurley spin_lock_irq(&port->lock); 236442bd7a4fSViktar Palstsiuk atmel_enable_ms(port); 2365d41510ceSPeter Hurley spin_unlock_irq(&port->lock); 236642bd7a4fSViktar Palstsiuk } else { 236742bd7a4fSViktar Palstsiuk port->flags &= ~UPF_HARDPPS_CD; 2368cab68f89SPeter Hurley if (!UART_ENABLE_MS(port, termios->c_cflag)) { 2369cab68f89SPeter Hurley spin_lock_irq(&port->lock); 2370cab68f89SPeter Hurley atmel_disable_ms(port); 2371cab68f89SPeter Hurley spin_unlock_irq(&port->lock); 2372cab68f89SPeter Hurley } 237342bd7a4fSViktar Palstsiuk } 237442bd7a4fSViktar Palstsiuk } 237542bd7a4fSViktar Palstsiuk 2376ab4382d2SGreg Kroah-Hartman /* 2377ab4382d2SGreg Kroah-Hartman * Return string describing the specified port 2378ab4382d2SGreg Kroah-Hartman */ 2379ab4382d2SGreg Kroah-Hartman static const char *atmel_type(struct uart_port *port) 2380ab4382d2SGreg Kroah-Hartman { 2381ab4382d2SGreg Kroah-Hartman return (port->type == PORT_ATMEL) ? "ATMEL_SERIAL" : NULL; 2382ab4382d2SGreg Kroah-Hartman } 2383ab4382d2SGreg Kroah-Hartman 2384ab4382d2SGreg Kroah-Hartman /* 2385ab4382d2SGreg Kroah-Hartman * Release the memory region(s) being used by 'port'. 2386ab4382d2SGreg Kroah-Hartman */ 2387ab4382d2SGreg Kroah-Hartman static void atmel_release_port(struct uart_port *port) 2388ab4382d2SGreg Kroah-Hartman { 2389c24d2531SRadu Pirea struct platform_device *mpdev = to_platform_device(port->dev->parent); 2390c24d2531SRadu Pirea int size = resource_size(mpdev->resource); 2391ab4382d2SGreg Kroah-Hartman 2392ab4382d2SGreg Kroah-Hartman release_mem_region(port->mapbase, size); 2393ab4382d2SGreg Kroah-Hartman 2394ab4382d2SGreg Kroah-Hartman if (port->flags & UPF_IOREMAP) { 2395ab4382d2SGreg Kroah-Hartman iounmap(port->membase); 2396ab4382d2SGreg Kroah-Hartman port->membase = NULL; 2397ab4382d2SGreg Kroah-Hartman } 2398ab4382d2SGreg Kroah-Hartman } 2399ab4382d2SGreg Kroah-Hartman 2400ab4382d2SGreg Kroah-Hartman /* 2401ab4382d2SGreg Kroah-Hartman * Request the memory region(s) being used by 'port'. 2402ab4382d2SGreg Kroah-Hartman */ 2403ab4382d2SGreg Kroah-Hartman static int atmel_request_port(struct uart_port *port) 2404ab4382d2SGreg Kroah-Hartman { 2405c24d2531SRadu Pirea struct platform_device *mpdev = to_platform_device(port->dev->parent); 2406c24d2531SRadu Pirea int size = resource_size(mpdev->resource); 2407ab4382d2SGreg Kroah-Hartman 2408ab4382d2SGreg Kroah-Hartman if (!request_mem_region(port->mapbase, size, "atmel_serial")) 2409ab4382d2SGreg Kroah-Hartman return -EBUSY; 2410ab4382d2SGreg Kroah-Hartman 2411ab4382d2SGreg Kroah-Hartman if (port->flags & UPF_IOREMAP) { 2412ab4382d2SGreg Kroah-Hartman port->membase = ioremap(port->mapbase, size); 2413ab4382d2SGreg Kroah-Hartman if (port->membase == NULL) { 2414ab4382d2SGreg Kroah-Hartman release_mem_region(port->mapbase, size); 2415ab4382d2SGreg Kroah-Hartman return -ENOMEM; 2416ab4382d2SGreg Kroah-Hartman } 2417ab4382d2SGreg Kroah-Hartman } 2418ab4382d2SGreg Kroah-Hartman 2419ab4382d2SGreg Kroah-Hartman return 0; 2420ab4382d2SGreg Kroah-Hartman } 2421ab4382d2SGreg Kroah-Hartman 2422ab4382d2SGreg Kroah-Hartman /* 2423ab4382d2SGreg Kroah-Hartman * Configure/autoconfigure the port. 2424ab4382d2SGreg Kroah-Hartman */ 2425ab4382d2SGreg Kroah-Hartman static void atmel_config_port(struct uart_port *port, int flags) 2426ab4382d2SGreg Kroah-Hartman { 2427ab4382d2SGreg Kroah-Hartman if (flags & UART_CONFIG_TYPE) { 2428ab4382d2SGreg Kroah-Hartman port->type = PORT_ATMEL; 2429ab4382d2SGreg Kroah-Hartman atmel_request_port(port); 2430ab4382d2SGreg Kroah-Hartman } 2431ab4382d2SGreg Kroah-Hartman } 2432ab4382d2SGreg Kroah-Hartman 2433ab4382d2SGreg Kroah-Hartman /* 2434ab4382d2SGreg Kroah-Hartman * Verify the new serial_struct (for TIOCSSERIAL). 2435ab4382d2SGreg Kroah-Hartman */ 2436ab4382d2SGreg Kroah-Hartman static int atmel_verify_port(struct uart_port *port, struct serial_struct *ser) 2437ab4382d2SGreg Kroah-Hartman { 2438ab4382d2SGreg Kroah-Hartman int ret = 0; 2439ab4382d2SGreg Kroah-Hartman if (ser->type != PORT_UNKNOWN && ser->type != PORT_ATMEL) 2440ab4382d2SGreg Kroah-Hartman ret = -EINVAL; 2441ab4382d2SGreg Kroah-Hartman if (port->irq != ser->irq) 2442ab4382d2SGreg Kroah-Hartman ret = -EINVAL; 2443ab4382d2SGreg Kroah-Hartman if (ser->io_type != SERIAL_IO_MEM) 2444ab4382d2SGreg Kroah-Hartman ret = -EINVAL; 2445ab4382d2SGreg Kroah-Hartman if (port->uartclk / 16 != ser->baud_base) 2446ab4382d2SGreg Kroah-Hartman ret = -EINVAL; 2447270c2adeSAndre Przywara if (port->mapbase != (unsigned long)ser->iomem_base) 2448ab4382d2SGreg Kroah-Hartman ret = -EINVAL; 2449ab4382d2SGreg Kroah-Hartman if (port->iobase != ser->port) 2450ab4382d2SGreg Kroah-Hartman ret = -EINVAL; 2451ab4382d2SGreg Kroah-Hartman if (ser->hub6 != 0) 2452ab4382d2SGreg Kroah-Hartman ret = -EINVAL; 2453ab4382d2SGreg Kroah-Hartman return ret; 2454ab4382d2SGreg Kroah-Hartman } 2455ab4382d2SGreg Kroah-Hartman 2456ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_CONSOLE_POLL 2457ab4382d2SGreg Kroah-Hartman static int atmel_poll_get_char(struct uart_port *port) 2458ab4382d2SGreg Kroah-Hartman { 24594e7decdaSCyrille Pitchen while (!(atmel_uart_readl(port, ATMEL_US_CSR) & ATMEL_US_RXRDY)) 2460ab4382d2SGreg Kroah-Hartman cpu_relax(); 2461ab4382d2SGreg Kroah-Hartman 2462a6499435SCyrille Pitchen return atmel_uart_read_char(port); 2463ab4382d2SGreg Kroah-Hartman } 2464ab4382d2SGreg Kroah-Hartman 2465ab4382d2SGreg Kroah-Hartman static void atmel_poll_put_char(struct uart_port *port, unsigned char ch) 2466ab4382d2SGreg Kroah-Hartman { 24674e7decdaSCyrille Pitchen while (!(atmel_uart_readl(port, ATMEL_US_CSR) & ATMEL_US_TXRDY)) 2468ab4382d2SGreg Kroah-Hartman cpu_relax(); 2469ab4382d2SGreg Kroah-Hartman 2470a6499435SCyrille Pitchen atmel_uart_write_char(port, ch); 2471ab4382d2SGreg Kroah-Hartman } 2472ab4382d2SGreg Kroah-Hartman #endif 2473ab4382d2SGreg Kroah-Hartman 24745c7dcdb6SJulia Lawall static const struct uart_ops atmel_pops = { 2475ab4382d2SGreg Kroah-Hartman .tx_empty = atmel_tx_empty, 2476ab4382d2SGreg Kroah-Hartman .set_mctrl = atmel_set_mctrl, 2477ab4382d2SGreg Kroah-Hartman .get_mctrl = atmel_get_mctrl, 2478ab4382d2SGreg Kroah-Hartman .stop_tx = atmel_stop_tx, 2479ab4382d2SGreg Kroah-Hartman .start_tx = atmel_start_tx, 2480ab4382d2SGreg Kroah-Hartman .stop_rx = atmel_stop_rx, 2481ab4382d2SGreg Kroah-Hartman .enable_ms = atmel_enable_ms, 2482ab4382d2SGreg Kroah-Hartman .break_ctl = atmel_break_ctl, 2483ab4382d2SGreg Kroah-Hartman .startup = atmel_startup, 2484ab4382d2SGreg Kroah-Hartman .shutdown = atmel_shutdown, 2485ab4382d2SGreg Kroah-Hartman .flush_buffer = atmel_flush_buffer, 2486ab4382d2SGreg Kroah-Hartman .set_termios = atmel_set_termios, 248742bd7a4fSViktar Palstsiuk .set_ldisc = atmel_set_ldisc, 2488ab4382d2SGreg Kroah-Hartman .type = atmel_type, 2489ab4382d2SGreg Kroah-Hartman .release_port = atmel_release_port, 2490ab4382d2SGreg Kroah-Hartman .request_port = atmel_request_port, 2491ab4382d2SGreg Kroah-Hartman .config_port = atmel_config_port, 2492ab4382d2SGreg Kroah-Hartman .verify_port = atmel_verify_port, 2493ab4382d2SGreg Kroah-Hartman .pm = atmel_serial_pm, 2494ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_CONSOLE_POLL 2495ab4382d2SGreg Kroah-Hartman .poll_get_char = atmel_poll_get_char, 2496ab4382d2SGreg Kroah-Hartman .poll_put_char = atmel_poll_put_char, 2497ab4382d2SGreg Kroah-Hartman #endif 2498ab4382d2SGreg Kroah-Hartman }; 2499ab4382d2SGreg Kroah-Hartman 2500ab4382d2SGreg Kroah-Hartman /* 2501ab4382d2SGreg Kroah-Hartman * Configure the port from the platform device resource info. 2502ab4382d2SGreg Kroah-Hartman */ 250391f8c2d8SBoris BREZILLON static int atmel_init_port(struct atmel_uart_port *atmel_port, 2504ab4382d2SGreg Kroah-Hartman struct platform_device *pdev) 2505ab4382d2SGreg Kroah-Hartman { 250691f8c2d8SBoris BREZILLON int ret; 2507ab4382d2SGreg Kroah-Hartman struct uart_port *port = &atmel_port->uart; 2508c24d2531SRadu Pirea struct platform_device *mpdev = to_platform_device(pdev->dev.parent); 2509ab4382d2SGreg Kroah-Hartman 25104a1e8888SLeilei Zhao atmel_init_property(atmel_port, pdev); 2511a930e528SElen Song atmel_set_ops(port); 2512a930e528SElen Song 2513c24d2531SRadu Pirea uart_get_rs485_mode(&mpdev->dev, &port->rs485); 251433d64c4fSElen Song 2515ab4382d2SGreg Kroah-Hartman port->iotype = UPIO_MEM; 251692c8f7c0SAlexandre Belloni port->flags = UPF_BOOT_AUTOCONF | UPF_IOREMAP; 2517ab4382d2SGreg Kroah-Hartman port->ops = &atmel_pops; 2518ab4382d2SGreg Kroah-Hartman port->fifosize = 1; 2519ab4382d2SGreg Kroah-Hartman port->dev = &pdev->dev; 2520c24d2531SRadu Pirea port->mapbase = mpdev->resource[0].start; 2521c24d2531SRadu Pirea port->irq = mpdev->resource[1].start; 252213bd3e6fSRicardo Ribalda Delgado port->rs485_config = atmel_config_rs485; 2523*377fedd1SNicolas Ferre port->iso7816_config = atmel_config_iso7816; 252492c8f7c0SAlexandre Belloni port->membase = NULL; 2525ab4382d2SGreg Kroah-Hartman 2526ab4382d2SGreg Kroah-Hartman memset(&atmel_port->rx_ring, 0, sizeof(atmel_port->rx_ring)); 2527ab4382d2SGreg Kroah-Hartman 2528ab4382d2SGreg Kroah-Hartman /* for console, the clock could already be configured */ 2529ab4382d2SGreg Kroah-Hartman if (!atmel_port->clk) { 2530c24d2531SRadu Pirea atmel_port->clk = clk_get(&mpdev->dev, "usart"); 253191f8c2d8SBoris BREZILLON if (IS_ERR(atmel_port->clk)) { 253291f8c2d8SBoris BREZILLON ret = PTR_ERR(atmel_port->clk); 253391f8c2d8SBoris BREZILLON atmel_port->clk = NULL; 253491f8c2d8SBoris BREZILLON return ret; 253591f8c2d8SBoris BREZILLON } 253691f8c2d8SBoris BREZILLON ret = clk_prepare_enable(atmel_port->clk); 253791f8c2d8SBoris BREZILLON if (ret) { 253891f8c2d8SBoris BREZILLON clk_put(atmel_port->clk); 253991f8c2d8SBoris BREZILLON atmel_port->clk = NULL; 254091f8c2d8SBoris BREZILLON return ret; 254191f8c2d8SBoris BREZILLON } 2542ab4382d2SGreg Kroah-Hartman port->uartclk = clk_get_rate(atmel_port->clk); 254391f8c2d8SBoris BREZILLON clk_disable_unprepare(atmel_port->clk); 2544ab4382d2SGreg Kroah-Hartman /* only enable clock when USART is in use */ 2545ab4382d2SGreg Kroah-Hartman } 2546ab4382d2SGreg Kroah-Hartman 2547*377fedd1SNicolas Ferre /* 2548*377fedd1SNicolas Ferre * Use TXEMPTY for interrupt when rs485 or ISO7816 else TXRDY or 2549*377fedd1SNicolas Ferre * ENDTX|TXBUFE 2550*377fedd1SNicolas Ferre */ 2551*377fedd1SNicolas Ferre if (port->rs485.flags & SER_RS485_ENABLED || 2552*377fedd1SNicolas Ferre port->iso7816.flags & SER_ISO7816_ENABLED) 2553ab4382d2SGreg Kroah-Hartman atmel_port->tx_done_mask = ATMEL_US_TXEMPTY; 255464e22ebeSElen Song else if (atmel_use_pdc_tx(port)) { 2555ab4382d2SGreg Kroah-Hartman port->fifosize = PDC_BUFFER_SIZE; 2556ab4382d2SGreg Kroah-Hartman atmel_port->tx_done_mask = ATMEL_US_ENDTX | ATMEL_US_TXBUFE; 2557ab4382d2SGreg Kroah-Hartman } else { 2558ab4382d2SGreg Kroah-Hartman atmel_port->tx_done_mask = ATMEL_US_TXRDY; 2559ab4382d2SGreg Kroah-Hartman } 256091f8c2d8SBoris BREZILLON 256191f8c2d8SBoris BREZILLON return 0; 2562ab4382d2SGreg Kroah-Hartman } 2563ab4382d2SGreg Kroah-Hartman 2564ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_SERIAL_ATMEL_CONSOLE 2565ab4382d2SGreg Kroah-Hartman static void atmel_console_putchar(struct uart_port *port, int ch) 2566ab4382d2SGreg Kroah-Hartman { 25674e7decdaSCyrille Pitchen while (!(atmel_uart_readl(port, ATMEL_US_CSR) & ATMEL_US_TXRDY)) 2568ab4382d2SGreg Kroah-Hartman cpu_relax(); 2569a6499435SCyrille Pitchen atmel_uart_write_char(port, ch); 2570ab4382d2SGreg Kroah-Hartman } 2571ab4382d2SGreg Kroah-Hartman 2572ab4382d2SGreg Kroah-Hartman /* 2573ab4382d2SGreg Kroah-Hartman * Interrupts are disabled on entering 2574ab4382d2SGreg Kroah-Hartman */ 2575ab4382d2SGreg Kroah-Hartman static void atmel_console_write(struct console *co, const char *s, u_int count) 2576ab4382d2SGreg Kroah-Hartman { 2577ab4382d2SGreg Kroah-Hartman struct uart_port *port = &atmel_ports[co->index].uart; 2578ab4382d2SGreg Kroah-Hartman struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 2579ab4382d2SGreg Kroah-Hartman unsigned int status, imr; 2580ab4382d2SGreg Kroah-Hartman unsigned int pdc_tx; 2581ab4382d2SGreg Kroah-Hartman 2582ab4382d2SGreg Kroah-Hartman /* 2583ab4382d2SGreg Kroah-Hartman * First, save IMR and then disable interrupts 2584ab4382d2SGreg Kroah-Hartman */ 25854e7decdaSCyrille Pitchen imr = atmel_uart_readl(port, ATMEL_US_IMR); 25864e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_IDR, 25874e7decdaSCyrille Pitchen ATMEL_US_RXRDY | atmel_port->tx_done_mask); 2588ab4382d2SGreg Kroah-Hartman 2589ab4382d2SGreg Kroah-Hartman /* Store PDC transmit status and disable it */ 25904e7decdaSCyrille Pitchen pdc_tx = atmel_uart_readl(port, ATMEL_PDC_PTSR) & ATMEL_PDC_TXTEN; 25914e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_TXTDIS); 2592ab4382d2SGreg Kroah-Hartman 2593497e1e16SNicolas Ferre /* Make sure that tx path is actually able to send characters */ 2594497e1e16SNicolas Ferre atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_TXEN); 2595ea04f82aSRomain Izard atmel_port->tx_stopped = false; 2596497e1e16SNicolas Ferre 2597ab4382d2SGreg Kroah-Hartman uart_console_write(port, s, count, atmel_console_putchar); 2598ab4382d2SGreg Kroah-Hartman 2599ab4382d2SGreg Kroah-Hartman /* 2600ab4382d2SGreg Kroah-Hartman * Finally, wait for transmitter to become empty 2601ab4382d2SGreg Kroah-Hartman * and restore IMR 2602ab4382d2SGreg Kroah-Hartman */ 2603ab4382d2SGreg Kroah-Hartman do { 26044e7decdaSCyrille Pitchen status = atmel_uart_readl(port, ATMEL_US_CSR); 2605ab4382d2SGreg Kroah-Hartman } while (!(status & ATMEL_US_TXRDY)); 2606ab4382d2SGreg Kroah-Hartman 2607ab4382d2SGreg Kroah-Hartman /* Restore PDC transmit status */ 2608ab4382d2SGreg Kroah-Hartman if (pdc_tx) 26094e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_TXTEN); 2610ab4382d2SGreg Kroah-Hartman 2611ab4382d2SGreg Kroah-Hartman /* set interrupts back the way they were */ 26124e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_IER, imr); 2613ab4382d2SGreg Kroah-Hartman } 2614ab4382d2SGreg Kroah-Hartman 2615ab4382d2SGreg Kroah-Hartman /* 2616ab4382d2SGreg Kroah-Hartman * If the port was already initialised (eg, by a boot loader), 2617ab4382d2SGreg Kroah-Hartman * try to determine the current setup. 2618ab4382d2SGreg Kroah-Hartman */ 2619ab4382d2SGreg Kroah-Hartman static void __init atmel_console_get_options(struct uart_port *port, int *baud, 2620ab4382d2SGreg Kroah-Hartman int *parity, int *bits) 2621ab4382d2SGreg Kroah-Hartman { 2622ab4382d2SGreg Kroah-Hartman unsigned int mr, quot; 2623ab4382d2SGreg Kroah-Hartman 2624ab4382d2SGreg Kroah-Hartman /* 2625ab4382d2SGreg Kroah-Hartman * If the baud rate generator isn't running, the port wasn't 2626ab4382d2SGreg Kroah-Hartman * initialized by the boot loader. 2627ab4382d2SGreg Kroah-Hartman */ 26284e7decdaSCyrille Pitchen quot = atmel_uart_readl(port, ATMEL_US_BRGR) & ATMEL_US_CD; 2629ab4382d2SGreg Kroah-Hartman if (!quot) 2630ab4382d2SGreg Kroah-Hartman return; 2631ab4382d2SGreg Kroah-Hartman 26324e7decdaSCyrille Pitchen mr = atmel_uart_readl(port, ATMEL_US_MR) & ATMEL_US_CHRL; 2633ab4382d2SGreg Kroah-Hartman if (mr == ATMEL_US_CHRL_8) 2634ab4382d2SGreg Kroah-Hartman *bits = 8; 2635ab4382d2SGreg Kroah-Hartman else 2636ab4382d2SGreg Kroah-Hartman *bits = 7; 2637ab4382d2SGreg Kroah-Hartman 26384e7decdaSCyrille Pitchen mr = atmel_uart_readl(port, ATMEL_US_MR) & ATMEL_US_PAR; 2639ab4382d2SGreg Kroah-Hartman if (mr == ATMEL_US_PAR_EVEN) 2640ab4382d2SGreg Kroah-Hartman *parity = 'e'; 2641ab4382d2SGreg Kroah-Hartman else if (mr == ATMEL_US_PAR_ODD) 2642ab4382d2SGreg Kroah-Hartman *parity = 'o'; 2643ab4382d2SGreg Kroah-Hartman 2644ab4382d2SGreg Kroah-Hartman /* 2645ab4382d2SGreg Kroah-Hartman * The serial core only rounds down when matching this to a 2646ab4382d2SGreg Kroah-Hartman * supported baud rate. Make sure we don't end up slightly 2647ab4382d2SGreg Kroah-Hartman * lower than one of those, as it would make us fall through 2648ab4382d2SGreg Kroah-Hartman * to a much lower baud rate than we really want. 2649ab4382d2SGreg Kroah-Hartman */ 2650ab4382d2SGreg Kroah-Hartman *baud = port->uartclk / (16 * (quot - 1)); 2651ab4382d2SGreg Kroah-Hartman } 2652ab4382d2SGreg Kroah-Hartman 2653ab4382d2SGreg Kroah-Hartman static int __init atmel_console_setup(struct console *co, char *options) 2654ab4382d2SGreg Kroah-Hartman { 265591f8c2d8SBoris BREZILLON int ret; 2656ab4382d2SGreg Kroah-Hartman struct uart_port *port = &atmel_ports[co->index].uart; 2657ea04f82aSRomain Izard struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 2658ab4382d2SGreg Kroah-Hartman int baud = 115200; 2659ab4382d2SGreg Kroah-Hartman int bits = 8; 2660ab4382d2SGreg Kroah-Hartman int parity = 'n'; 2661ab4382d2SGreg Kroah-Hartman int flow = 'n'; 2662ab4382d2SGreg Kroah-Hartman 2663ab4382d2SGreg Kroah-Hartman if (port->membase == NULL) { 2664ab4382d2SGreg Kroah-Hartman /* Port not initialized yet - delay setup */ 2665ab4382d2SGreg Kroah-Hartman return -ENODEV; 2666ab4382d2SGreg Kroah-Hartman } 2667ab4382d2SGreg Kroah-Hartman 266891f8c2d8SBoris BREZILLON ret = clk_prepare_enable(atmel_ports[co->index].clk); 266991f8c2d8SBoris BREZILLON if (ret) 267091f8c2d8SBoris BREZILLON return ret; 2671ab4382d2SGreg Kroah-Hartman 26724e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_IDR, -1); 26734e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA | ATMEL_US_RSTRX); 26744e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_TXEN | ATMEL_US_RXEN); 2675ea04f82aSRomain Izard atmel_port->tx_stopped = false; 2676ab4382d2SGreg Kroah-Hartman 2677ab4382d2SGreg Kroah-Hartman if (options) 2678ab4382d2SGreg Kroah-Hartman uart_parse_options(options, &baud, &parity, &bits, &flow); 2679ab4382d2SGreg Kroah-Hartman else 2680ab4382d2SGreg Kroah-Hartman atmel_console_get_options(port, &baud, &parity, &bits); 2681ab4382d2SGreg Kroah-Hartman 2682ab4382d2SGreg Kroah-Hartman return uart_set_options(port, co, baud, parity, bits, flow); 2683ab4382d2SGreg Kroah-Hartman } 2684ab4382d2SGreg Kroah-Hartman 2685ab4382d2SGreg Kroah-Hartman static struct uart_driver atmel_uart; 2686ab4382d2SGreg Kroah-Hartman 2687ab4382d2SGreg Kroah-Hartman static struct console atmel_console = { 2688ab4382d2SGreg Kroah-Hartman .name = ATMEL_DEVICENAME, 2689ab4382d2SGreg Kroah-Hartman .write = atmel_console_write, 2690ab4382d2SGreg Kroah-Hartman .device = uart_console_device, 2691ab4382d2SGreg Kroah-Hartman .setup = atmel_console_setup, 2692ab4382d2SGreg Kroah-Hartman .flags = CON_PRINTBUFFER, 2693ab4382d2SGreg Kroah-Hartman .index = -1, 2694ab4382d2SGreg Kroah-Hartman .data = &atmel_uart, 2695ab4382d2SGreg Kroah-Hartman }; 2696ab4382d2SGreg Kroah-Hartman 2697ab4382d2SGreg Kroah-Hartman #define ATMEL_CONSOLE_DEVICE (&atmel_console) 2698ab4382d2SGreg Kroah-Hartman 2699ab4382d2SGreg Kroah-Hartman static inline bool atmel_is_console_port(struct uart_port *port) 2700ab4382d2SGreg Kroah-Hartman { 2701ab4382d2SGreg Kroah-Hartman return port->cons && port->cons->index == port->line; 2702ab4382d2SGreg Kroah-Hartman } 2703ab4382d2SGreg Kroah-Hartman 2704ab4382d2SGreg Kroah-Hartman #else 2705ab4382d2SGreg Kroah-Hartman #define ATMEL_CONSOLE_DEVICE NULL 2706ab4382d2SGreg Kroah-Hartman 2707ab4382d2SGreg Kroah-Hartman static inline bool atmel_is_console_port(struct uart_port *port) 2708ab4382d2SGreg Kroah-Hartman { 2709ab4382d2SGreg Kroah-Hartman return false; 2710ab4382d2SGreg Kroah-Hartman } 2711ab4382d2SGreg Kroah-Hartman #endif 2712ab4382d2SGreg Kroah-Hartman 2713ab4382d2SGreg Kroah-Hartman static struct uart_driver atmel_uart = { 2714ab4382d2SGreg Kroah-Hartman .owner = THIS_MODULE, 2715ab4382d2SGreg Kroah-Hartman .driver_name = "atmel_serial", 2716ab4382d2SGreg Kroah-Hartman .dev_name = ATMEL_DEVICENAME, 2717ab4382d2SGreg Kroah-Hartman .major = SERIAL_ATMEL_MAJOR, 2718ab4382d2SGreg Kroah-Hartman .minor = MINOR_START, 2719ab4382d2SGreg Kroah-Hartman .nr = ATMEL_MAX_UART, 2720ab4382d2SGreg Kroah-Hartman .cons = ATMEL_CONSOLE_DEVICE, 2721ab4382d2SGreg Kroah-Hartman }; 2722ab4382d2SGreg Kroah-Hartman 2723ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_PM 2724ab4382d2SGreg Kroah-Hartman static bool atmel_serial_clk_will_stop(void) 2725ab4382d2SGreg Kroah-Hartman { 2726ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_ARCH_AT91 2727ab4382d2SGreg Kroah-Hartman return at91_suspend_entering_slow_clock(); 2728ab4382d2SGreg Kroah-Hartman #else 2729ab4382d2SGreg Kroah-Hartman return false; 2730ab4382d2SGreg Kroah-Hartman #endif 2731ab4382d2SGreg Kroah-Hartman } 2732ab4382d2SGreg Kroah-Hartman 2733ab4382d2SGreg Kroah-Hartman static int atmel_serial_suspend(struct platform_device *pdev, 2734ab4382d2SGreg Kroah-Hartman pm_message_t state) 2735ab4382d2SGreg Kroah-Hartman { 2736ab4382d2SGreg Kroah-Hartman struct uart_port *port = platform_get_drvdata(pdev); 2737ab4382d2SGreg Kroah-Hartman struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 2738ab4382d2SGreg Kroah-Hartman 2739ab4382d2SGreg Kroah-Hartman if (atmel_is_console_port(port) && console_suspend_enabled) { 2740ab4382d2SGreg Kroah-Hartman /* Drain the TX shifter */ 27414e7decdaSCyrille Pitchen while (!(atmel_uart_readl(port, ATMEL_US_CSR) & 27424e7decdaSCyrille Pitchen ATMEL_US_TXEMPTY)) 2743ab4382d2SGreg Kroah-Hartman cpu_relax(); 2744ab4382d2SGreg Kroah-Hartman } 2745ab4382d2SGreg Kroah-Hartman 27466a5f0e2fSAlexandre Belloni if (atmel_is_console_port(port) && !console_suspend_enabled) { 27476a5f0e2fSAlexandre Belloni /* Cache register values as we won't get a full shutdown/startup 27486a5f0e2fSAlexandre Belloni * cycle 27496a5f0e2fSAlexandre Belloni */ 27506a5f0e2fSAlexandre Belloni atmel_port->cache.mr = atmel_uart_readl(port, ATMEL_US_MR); 27516a5f0e2fSAlexandre Belloni atmel_port->cache.imr = atmel_uart_readl(port, ATMEL_US_IMR); 27526a5f0e2fSAlexandre Belloni atmel_port->cache.brgr = atmel_uart_readl(port, ATMEL_US_BRGR); 27536a5f0e2fSAlexandre Belloni atmel_port->cache.rtor = atmel_uart_readl(port, 27546a5f0e2fSAlexandre Belloni atmel_port->rtor); 27556a5f0e2fSAlexandre Belloni atmel_port->cache.ttgr = atmel_uart_readl(port, ATMEL_US_TTGR); 27566a5f0e2fSAlexandre Belloni atmel_port->cache.fmr = atmel_uart_readl(port, ATMEL_US_FMR); 27576a5f0e2fSAlexandre Belloni atmel_port->cache.fimr = atmel_uart_readl(port, ATMEL_US_FIMR); 27586a5f0e2fSAlexandre Belloni } 27596a5f0e2fSAlexandre Belloni 2760ab4382d2SGreg Kroah-Hartman /* we can not wake up if we're running on slow clock */ 2761ab4382d2SGreg Kroah-Hartman atmel_port->may_wakeup = device_may_wakeup(&pdev->dev); 27622c7af5baSBoris BREZILLON if (atmel_serial_clk_will_stop()) { 27632c7af5baSBoris BREZILLON unsigned long flags; 27642c7af5baSBoris BREZILLON 27652c7af5baSBoris BREZILLON spin_lock_irqsave(&atmel_port->lock_suspended, flags); 27662c7af5baSBoris BREZILLON atmel_port->suspended = true; 27672c7af5baSBoris BREZILLON spin_unlock_irqrestore(&atmel_port->lock_suspended, flags); 2768ab4382d2SGreg Kroah-Hartman device_set_wakeup_enable(&pdev->dev, 0); 27692c7af5baSBoris BREZILLON } 2770ab4382d2SGreg Kroah-Hartman 2771ab4382d2SGreg Kroah-Hartman uart_suspend_port(&atmel_uart, port); 2772ab4382d2SGreg Kroah-Hartman 2773ab4382d2SGreg Kroah-Hartman return 0; 2774ab4382d2SGreg Kroah-Hartman } 2775ab4382d2SGreg Kroah-Hartman 2776ab4382d2SGreg Kroah-Hartman static int atmel_serial_resume(struct platform_device *pdev) 2777ab4382d2SGreg Kroah-Hartman { 2778ab4382d2SGreg Kroah-Hartman struct uart_port *port = platform_get_drvdata(pdev); 2779ab4382d2SGreg Kroah-Hartman struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 27802c7af5baSBoris BREZILLON unsigned long flags; 27812c7af5baSBoris BREZILLON 27826a5f0e2fSAlexandre Belloni if (atmel_is_console_port(port) && !console_suspend_enabled) { 27836a5f0e2fSAlexandre Belloni atmel_uart_writel(port, ATMEL_US_MR, atmel_port->cache.mr); 27846a5f0e2fSAlexandre Belloni atmel_uart_writel(port, ATMEL_US_IER, atmel_port->cache.imr); 27856a5f0e2fSAlexandre Belloni atmel_uart_writel(port, ATMEL_US_BRGR, atmel_port->cache.brgr); 27866a5f0e2fSAlexandre Belloni atmel_uart_writel(port, atmel_port->rtor, 27876a5f0e2fSAlexandre Belloni atmel_port->cache.rtor); 27886a5f0e2fSAlexandre Belloni atmel_uart_writel(port, ATMEL_US_TTGR, atmel_port->cache.ttgr); 27896a5f0e2fSAlexandre Belloni 27906a5f0e2fSAlexandre Belloni if (atmel_port->fifo_size) { 27916a5f0e2fSAlexandre Belloni atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_FIFOEN | 27926a5f0e2fSAlexandre Belloni ATMEL_US_RXFCLR | ATMEL_US_TXFLCLR); 27936a5f0e2fSAlexandre Belloni atmel_uart_writel(port, ATMEL_US_FMR, 27946a5f0e2fSAlexandre Belloni atmel_port->cache.fmr); 27956a5f0e2fSAlexandre Belloni atmel_uart_writel(port, ATMEL_US_FIER, 27966a5f0e2fSAlexandre Belloni atmel_port->cache.fimr); 27976a5f0e2fSAlexandre Belloni } 27986a5f0e2fSAlexandre Belloni atmel_start_rx(port); 27996a5f0e2fSAlexandre Belloni } 28006a5f0e2fSAlexandre Belloni 28012c7af5baSBoris BREZILLON spin_lock_irqsave(&atmel_port->lock_suspended, flags); 28022c7af5baSBoris BREZILLON if (atmel_port->pending) { 28032c7af5baSBoris BREZILLON atmel_handle_receive(port, atmel_port->pending); 28042c7af5baSBoris BREZILLON atmel_handle_status(port, atmel_port->pending, 28052c7af5baSBoris BREZILLON atmel_port->pending_status); 28062c7af5baSBoris BREZILLON atmel_handle_transmit(port, atmel_port->pending); 28072c7af5baSBoris BREZILLON atmel_port->pending = 0; 28082c7af5baSBoris BREZILLON } 28092c7af5baSBoris BREZILLON atmel_port->suspended = false; 28102c7af5baSBoris BREZILLON spin_unlock_irqrestore(&atmel_port->lock_suspended, flags); 2811ab4382d2SGreg Kroah-Hartman 2812ab4382d2SGreg Kroah-Hartman uart_resume_port(&atmel_uart, port); 2813ab4382d2SGreg Kroah-Hartman device_set_wakeup_enable(&pdev->dev, atmel_port->may_wakeup); 2814ab4382d2SGreg Kroah-Hartman 2815ab4382d2SGreg Kroah-Hartman return 0; 2816ab4382d2SGreg Kroah-Hartman } 2817ab4382d2SGreg Kroah-Hartman #else 2818ab4382d2SGreg Kroah-Hartman #define atmel_serial_suspend NULL 2819ab4382d2SGreg Kroah-Hartman #define atmel_serial_resume NULL 2820ab4382d2SGreg Kroah-Hartman #endif 2821ab4382d2SGreg Kroah-Hartman 2822b78cd169SJaeden Amero static void atmel_serial_probe_fifos(struct atmel_uart_port *atmel_port, 2823b5199d46SCyrille Pitchen struct platform_device *pdev) 2824b5199d46SCyrille Pitchen { 2825b78cd169SJaeden Amero atmel_port->fifo_size = 0; 2826b78cd169SJaeden Amero atmel_port->rts_low = 0; 2827b78cd169SJaeden Amero atmel_port->rts_high = 0; 2828b5199d46SCyrille Pitchen 2829b5199d46SCyrille Pitchen if (of_property_read_u32(pdev->dev.of_node, 2830b5199d46SCyrille Pitchen "atmel,fifo-size", 2831b78cd169SJaeden Amero &atmel_port->fifo_size)) 2832b5199d46SCyrille Pitchen return; 2833b5199d46SCyrille Pitchen 2834b78cd169SJaeden Amero if (!atmel_port->fifo_size) 2835b5199d46SCyrille Pitchen return; 2836b5199d46SCyrille Pitchen 2837b78cd169SJaeden Amero if (atmel_port->fifo_size < ATMEL_MIN_FIFO_SIZE) { 2838b78cd169SJaeden Amero atmel_port->fifo_size = 0; 2839b5199d46SCyrille Pitchen dev_err(&pdev->dev, "Invalid FIFO size\n"); 2840b5199d46SCyrille Pitchen return; 2841b5199d46SCyrille Pitchen } 2842b5199d46SCyrille Pitchen 2843b5199d46SCyrille Pitchen /* 2844b5199d46SCyrille Pitchen * 0 <= rts_low <= rts_high <= fifo_size 2845b5199d46SCyrille Pitchen * Once their CTS line asserted by the remote peer, some x86 UARTs tend 2846b5199d46SCyrille Pitchen * to flush their internal TX FIFO, commonly up to 16 data, before 2847b5199d46SCyrille Pitchen * actually stopping to send new data. So we try to set the RTS High 2848b5199d46SCyrille Pitchen * Threshold to a reasonably high value respecting this 16 data 2849b5199d46SCyrille Pitchen * empirical rule when possible. 2850b5199d46SCyrille Pitchen */ 2851b78cd169SJaeden Amero atmel_port->rts_high = max_t(int, atmel_port->fifo_size >> 1, 2852b78cd169SJaeden Amero atmel_port->fifo_size - ATMEL_RTS_HIGH_OFFSET); 2853b78cd169SJaeden Amero atmel_port->rts_low = max_t(int, atmel_port->fifo_size >> 2, 2854b78cd169SJaeden Amero atmel_port->fifo_size - ATMEL_RTS_LOW_OFFSET); 2855b5199d46SCyrille Pitchen 2856b5199d46SCyrille Pitchen dev_info(&pdev->dev, "Using FIFO (%u data)\n", 2857b78cd169SJaeden Amero atmel_port->fifo_size); 2858b5199d46SCyrille Pitchen dev_dbg(&pdev->dev, "RTS High Threshold : %2u data\n", 2859b78cd169SJaeden Amero atmel_port->rts_high); 2860b5199d46SCyrille Pitchen dev_dbg(&pdev->dev, "RTS Low Threshold : %2u data\n", 2861b78cd169SJaeden Amero atmel_port->rts_low); 2862b5199d46SCyrille Pitchen } 2863b5199d46SCyrille Pitchen 28649671f099SBill Pemberton static int atmel_serial_probe(struct platform_device *pdev) 2865ab4382d2SGreg Kroah-Hartman { 2866b78cd169SJaeden Amero struct atmel_uart_port *atmel_port; 2867c24d2531SRadu Pirea struct device_node *np = pdev->dev.parent->of_node; 2868ab4382d2SGreg Kroah-Hartman void *data; 28694cbf9f48SNicolas Ferre int ret = -ENODEV; 2870bd737f87SRicardo Ribalda Delgado bool rs485_enabled; 2871ab4382d2SGreg Kroah-Hartman 2872ab4382d2SGreg Kroah-Hartman BUILD_BUG_ON(ATMEL_SERIAL_RINGSIZE & (ATMEL_SERIAL_RINGSIZE - 1)); 2873ab4382d2SGreg Kroah-Hartman 2874c24d2531SRadu Pirea /* 2875c24d2531SRadu Pirea * In device tree there is no node with "atmel,at91rm9200-usart-serial" 2876c24d2531SRadu Pirea * as compatible string. This driver is probed by at91-usart mfd driver 2877c24d2531SRadu Pirea * which is just a wrapper over the atmel_serial driver and 2878c24d2531SRadu Pirea * spi-at91-usart driver. All attributes needed by this driver are 2879c24d2531SRadu Pirea * found in of_node of parent. 2880c24d2531SRadu Pirea */ 2881c24d2531SRadu Pirea pdev->dev.of_node = np; 2882c24d2531SRadu Pirea 28835fbe46b6SNicolas Ferre ret = of_alias_get_id(np, "serial"); 28844cbf9f48SNicolas Ferre if (ret < 0) 28855fbe46b6SNicolas Ferre /* port id not found in platform data nor device-tree aliases: 28864cbf9f48SNicolas Ferre * auto-enumerate it */ 2887503bded9SPawel Wieczorkiewicz ret = find_first_zero_bit(atmel_ports_in_use, ATMEL_MAX_UART); 28884cbf9f48SNicolas Ferre 2889503bded9SPawel Wieczorkiewicz if (ret >= ATMEL_MAX_UART) { 28904cbf9f48SNicolas Ferre ret = -ENODEV; 28914cbf9f48SNicolas Ferre goto err; 28924cbf9f48SNicolas Ferre } 28934cbf9f48SNicolas Ferre 2894503bded9SPawel Wieczorkiewicz if (test_and_set_bit(ret, atmel_ports_in_use)) { 28954cbf9f48SNicolas Ferre /* port already in use */ 28964cbf9f48SNicolas Ferre ret = -EBUSY; 28974cbf9f48SNicolas Ferre goto err; 28984cbf9f48SNicolas Ferre } 28994cbf9f48SNicolas Ferre 2900b78cd169SJaeden Amero atmel_port = &atmel_ports[ret]; 2901b78cd169SJaeden Amero atmel_port->backup_imr = 0; 2902b78cd169SJaeden Amero atmel_port->uart.line = ret; 2903b78cd169SJaeden Amero atmel_serial_probe_fifos(atmel_port, pdev); 2904354e57f3SLinus Walleij 290598f2082cSNicolas Ferre atomic_set(&atmel_port->tasklet_shutdown, 0); 2906b78cd169SJaeden Amero spin_lock_init(&atmel_port->lock_suspended); 29072c7af5baSBoris BREZILLON 2908b78cd169SJaeden Amero ret = atmel_init_port(atmel_port, pdev); 290991f8c2d8SBoris BREZILLON if (ret) 29106fbb9bdfSCyrille Pitchen goto err_clear_bit; 2911ab4382d2SGreg Kroah-Hartman 2912b78cd169SJaeden Amero atmel_port->gpios = mctrl_gpio_init(&atmel_port->uart, 0); 2913b78cd169SJaeden Amero if (IS_ERR(atmel_port->gpios)) { 2914b78cd169SJaeden Amero ret = PTR_ERR(atmel_port->gpios); 291518dfef9cSUwe Kleine-König goto err_clear_bit; 291618dfef9cSUwe Kleine-König } 291718dfef9cSUwe Kleine-König 2918b78cd169SJaeden Amero if (!atmel_use_pdc_rx(&atmel_port->uart)) { 2919ab4382d2SGreg Kroah-Hartman ret = -ENOMEM; 29206da2ec56SKees Cook data = kmalloc_array(ATMEL_SERIAL_RINGSIZE, 29216da2ec56SKees Cook sizeof(struct atmel_uart_char), 29226da2ec56SKees Cook GFP_KERNEL); 2923ab4382d2SGreg Kroah-Hartman if (!data) 2924ab4382d2SGreg Kroah-Hartman goto err_alloc_ring; 2925b78cd169SJaeden Amero atmel_port->rx_ring.buf = data; 2926ab4382d2SGreg Kroah-Hartman } 2927ab4382d2SGreg Kroah-Hartman 2928b78cd169SJaeden Amero rs485_enabled = atmel_port->uart.rs485.flags & SER_RS485_ENABLED; 2929bd737f87SRicardo Ribalda Delgado 2930b78cd169SJaeden Amero ret = uart_add_one_port(&atmel_uart, &atmel_port->uart); 2931ab4382d2SGreg Kroah-Hartman if (ret) 2932ab4382d2SGreg Kroah-Hartman goto err_add_port; 2933ab4382d2SGreg Kroah-Hartman 2934ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_SERIAL_ATMEL_CONSOLE 2935b78cd169SJaeden Amero if (atmel_is_console_port(&atmel_port->uart) 2936ab4382d2SGreg Kroah-Hartman && ATMEL_CONSOLE_DEVICE->flags & CON_ENABLED) { 2937ab4382d2SGreg Kroah-Hartman /* 2938ab4382d2SGreg Kroah-Hartman * The serial core enabled the clock for us, so undo 293991f8c2d8SBoris BREZILLON * the clk_prepare_enable() in atmel_console_setup() 2940ab4382d2SGreg Kroah-Hartman */ 2941b78cd169SJaeden Amero clk_disable_unprepare(atmel_port->clk); 2942ab4382d2SGreg Kroah-Hartman } 2943ab4382d2SGreg Kroah-Hartman #endif 2944ab4382d2SGreg Kroah-Hartman 2945ab4382d2SGreg Kroah-Hartman device_init_wakeup(&pdev->dev, 1); 2946b78cd169SJaeden Amero platform_set_drvdata(pdev, atmel_port); 2947ab4382d2SGreg Kroah-Hartman 2948d4f64187SCyrille Pitchen /* 2949d4f64187SCyrille Pitchen * The peripheral clock has been disabled by atmel_init_port(): 2950d4f64187SCyrille Pitchen * enable it before accessing I/O registers 2951d4f64187SCyrille Pitchen */ 2952b78cd169SJaeden Amero clk_prepare_enable(atmel_port->clk); 2953d4f64187SCyrille Pitchen 2954bd737f87SRicardo Ribalda Delgado if (rs485_enabled) { 2955b78cd169SJaeden Amero atmel_uart_writel(&atmel_port->uart, ATMEL_US_MR, 29564e7decdaSCyrille Pitchen ATMEL_US_USMODE_NORMAL); 2957b78cd169SJaeden Amero atmel_uart_writel(&atmel_port->uart, ATMEL_US_CR, 2958b78cd169SJaeden Amero ATMEL_US_RTSEN); 2959fc887b15SLinus Torvalds } 2960fc887b15SLinus Torvalds 2961055560b0SElen Song /* 2962055560b0SElen Song * Get port name of usart or uart 2963055560b0SElen Song */ 2964b78cd169SJaeden Amero atmel_get_ip_name(&atmel_port->uart); 2965055560b0SElen Song 2966d4f64187SCyrille Pitchen /* 2967d4f64187SCyrille Pitchen * The peripheral clock can now safely be disabled till the port 2968d4f64187SCyrille Pitchen * is used 2969d4f64187SCyrille Pitchen */ 2970b78cd169SJaeden Amero clk_disable_unprepare(atmel_port->clk); 2971d4f64187SCyrille Pitchen 2972ab4382d2SGreg Kroah-Hartman return 0; 2973ab4382d2SGreg Kroah-Hartman 2974ab4382d2SGreg Kroah-Hartman err_add_port: 2975b78cd169SJaeden Amero kfree(atmel_port->rx_ring.buf); 2976b78cd169SJaeden Amero atmel_port->rx_ring.buf = NULL; 2977ab4382d2SGreg Kroah-Hartman err_alloc_ring: 2978b78cd169SJaeden Amero if (!atmel_is_console_port(&atmel_port->uart)) { 2979b78cd169SJaeden Amero clk_put(atmel_port->clk); 2980b78cd169SJaeden Amero atmel_port->clk = NULL; 2981ab4382d2SGreg Kroah-Hartman } 29826fbb9bdfSCyrille Pitchen err_clear_bit: 2983b78cd169SJaeden Amero clear_bit(atmel_port->uart.line, atmel_ports_in_use); 29844cbf9f48SNicolas Ferre err: 2985ab4382d2SGreg Kroah-Hartman return ret; 2986ab4382d2SGreg Kroah-Hartman } 2987ab4382d2SGreg Kroah-Hartman 2988f4a8ab04SRomain Izard /* 2989f4a8ab04SRomain Izard * Even if the driver is not modular, it makes sense to be able to 2990f4a8ab04SRomain Izard * unbind a device: there can be many bound devices, and there are 2991f4a8ab04SRomain Izard * situations where dynamic binding and unbinding can be useful. 2992f4a8ab04SRomain Izard * 2993f4a8ab04SRomain Izard * For example, a connected device can require a specific firmware update 2994f4a8ab04SRomain Izard * protocol that needs bitbanging on IO lines, but use the regular serial 2995f4a8ab04SRomain Izard * port in the normal case. 2996f4a8ab04SRomain Izard */ 2997f4a8ab04SRomain Izard static int atmel_serial_remove(struct platform_device *pdev) 2998f4a8ab04SRomain Izard { 2999f4a8ab04SRomain Izard struct uart_port *port = platform_get_drvdata(pdev); 3000f4a8ab04SRomain Izard struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 3001f4a8ab04SRomain Izard int ret = 0; 3002f4a8ab04SRomain Izard 300300e8e658SNicolas Ferre tasklet_kill(&atmel_port->tasklet_rx); 300400e8e658SNicolas Ferre tasklet_kill(&atmel_port->tasklet_tx); 3005f4a8ab04SRomain Izard 3006f4a8ab04SRomain Izard device_init_wakeup(&pdev->dev, 0); 3007f4a8ab04SRomain Izard 3008f4a8ab04SRomain Izard ret = uart_remove_one_port(&atmel_uart, port); 3009f4a8ab04SRomain Izard 3010f4a8ab04SRomain Izard kfree(atmel_port->rx_ring.buf); 3011f4a8ab04SRomain Izard 3012f4a8ab04SRomain Izard /* "port" is allocated statically, so we shouldn't free it */ 3013f4a8ab04SRomain Izard 3014f4a8ab04SRomain Izard clear_bit(port->line, atmel_ports_in_use); 3015f4a8ab04SRomain Izard 3016f4a8ab04SRomain Izard clk_put(atmel_port->clk); 3017f4a8ab04SRomain Izard atmel_port->clk = NULL; 3018c24d2531SRadu Pirea pdev->dev.of_node = NULL; 3019f4a8ab04SRomain Izard 3020f4a8ab04SRomain Izard return ret; 3021f4a8ab04SRomain Izard } 3022f4a8ab04SRomain Izard 3023ab4382d2SGreg Kroah-Hartman static struct platform_driver atmel_serial_driver = { 3024ab4382d2SGreg Kroah-Hartman .probe = atmel_serial_probe, 3025f4a8ab04SRomain Izard .remove = atmel_serial_remove, 3026ab4382d2SGreg Kroah-Hartman .suspend = atmel_serial_suspend, 3027ab4382d2SGreg Kroah-Hartman .resume = atmel_serial_resume, 3028ab4382d2SGreg Kroah-Hartman .driver = { 3029c24d2531SRadu Pirea .name = "atmel_usart_serial", 30305fbe46b6SNicolas Ferre .of_match_table = of_match_ptr(atmel_serial_dt_ids), 3031ab4382d2SGreg Kroah-Hartman }, 3032ab4382d2SGreg Kroah-Hartman }; 3033ab4382d2SGreg Kroah-Hartman 3034ab4382d2SGreg Kroah-Hartman static int __init atmel_serial_init(void) 3035ab4382d2SGreg Kroah-Hartman { 3036ab4382d2SGreg Kroah-Hartman int ret; 3037ab4382d2SGreg Kroah-Hartman 3038ab4382d2SGreg Kroah-Hartman ret = uart_register_driver(&atmel_uart); 3039ab4382d2SGreg Kroah-Hartman if (ret) 3040ab4382d2SGreg Kroah-Hartman return ret; 3041ab4382d2SGreg Kroah-Hartman 3042ab4382d2SGreg Kroah-Hartman ret = platform_driver_register(&atmel_serial_driver); 3043ab4382d2SGreg Kroah-Hartman if (ret) 3044ab4382d2SGreg Kroah-Hartman uart_unregister_driver(&atmel_uart); 3045ab4382d2SGreg Kroah-Hartman 3046ab4382d2SGreg Kroah-Hartman return ret; 3047ab4382d2SGreg Kroah-Hartman } 3048c39dfebcSPaul Gortmaker device_initcall(atmel_serial_init); 3049