1c4b05856SSrinivas Kandagatla /* 2c4b05856SSrinivas Kandagatla * st-asc.c: ST Asynchronous serial controller (ASC) driver 3c4b05856SSrinivas Kandagatla * 4c4b05856SSrinivas Kandagatla * Copyright (C) 2003-2013 STMicroelectronics (R&D) Limited 5c4b05856SSrinivas Kandagatla * 6c4b05856SSrinivas Kandagatla * This program is free software; you can redistribute it and/or modify 7c4b05856SSrinivas Kandagatla * it under the terms of the GNU General Public License as published by 8c4b05856SSrinivas Kandagatla * the Free Software Foundation; either version 2 of the License, or 9c4b05856SSrinivas Kandagatla * (at your option) any later version. 10c4b05856SSrinivas Kandagatla * 11c4b05856SSrinivas Kandagatla */ 12c4b05856SSrinivas Kandagatla 13c4b05856SSrinivas Kandagatla #if defined(CONFIG_SERIAL_ST_ASC_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ) 14c4b05856SSrinivas Kandagatla #define SUPPORT_SYSRQ 15c4b05856SSrinivas Kandagatla #endif 16c4b05856SSrinivas Kandagatla 17c4b05856SSrinivas Kandagatla #include <linux/module.h> 18c4b05856SSrinivas Kandagatla #include <linux/serial.h> 19c4b05856SSrinivas Kandagatla #include <linux/console.h> 20c4b05856SSrinivas Kandagatla #include <linux/sysrq.h> 21c4b05856SSrinivas Kandagatla #include <linux/platform_device.h> 22c4b05856SSrinivas Kandagatla #include <linux/io.h> 23c4b05856SSrinivas Kandagatla #include <linux/irq.h> 24c4b05856SSrinivas Kandagatla #include <linux/tty.h> 25c4b05856SSrinivas Kandagatla #include <linux/tty_flip.h> 26c4b05856SSrinivas Kandagatla #include <linux/delay.h> 27c4b05856SSrinivas Kandagatla #include <linux/spinlock.h> 28c4b05856SSrinivas Kandagatla #include <linux/pm_runtime.h> 29c4b05856SSrinivas Kandagatla #include <linux/of.h> 30c4b05856SSrinivas Kandagatla #include <linux/of_platform.h> 31c4b05856SSrinivas Kandagatla #include <linux/serial_core.h> 32c4b05856SSrinivas Kandagatla #include <linux/clk.h> 33c4b05856SSrinivas Kandagatla 34c4b05856SSrinivas Kandagatla #define DRIVER_NAME "st-asc" 35c4b05856SSrinivas Kandagatla #define ASC_SERIAL_NAME "ttyAS" 36c4b05856SSrinivas Kandagatla #define ASC_FIFO_SIZE 16 37c4b05856SSrinivas Kandagatla #define ASC_MAX_PORTS 8 38c4b05856SSrinivas Kandagatla 39c4b05856SSrinivas Kandagatla struct asc_port { 40c4b05856SSrinivas Kandagatla struct uart_port port; 41c4b05856SSrinivas Kandagatla struct clk *clk; 42c4b05856SSrinivas Kandagatla unsigned int hw_flow_control:1; 43c4b05856SSrinivas Kandagatla unsigned int force_m1:1; 44c4b05856SSrinivas Kandagatla }; 45c4b05856SSrinivas Kandagatla 46c4b05856SSrinivas Kandagatla static struct asc_port asc_ports[ASC_MAX_PORTS]; 47c4b05856SSrinivas Kandagatla static struct uart_driver asc_uart_driver; 48c4b05856SSrinivas Kandagatla 49c4b05856SSrinivas Kandagatla /*---- UART Register definitions ------------------------------*/ 50c4b05856SSrinivas Kandagatla 51c4b05856SSrinivas Kandagatla /* Register offsets */ 52c4b05856SSrinivas Kandagatla 53c4b05856SSrinivas Kandagatla #define ASC_BAUDRATE 0x00 54c4b05856SSrinivas Kandagatla #define ASC_TXBUF 0x04 55c4b05856SSrinivas Kandagatla #define ASC_RXBUF 0x08 56c4b05856SSrinivas Kandagatla #define ASC_CTL 0x0C 57c4b05856SSrinivas Kandagatla #define ASC_INTEN 0x10 58c4b05856SSrinivas Kandagatla #define ASC_STA 0x14 59c4b05856SSrinivas Kandagatla #define ASC_GUARDTIME 0x18 60c4b05856SSrinivas Kandagatla #define ASC_TIMEOUT 0x1C 61c4b05856SSrinivas Kandagatla #define ASC_TXRESET 0x20 62c4b05856SSrinivas Kandagatla #define ASC_RXRESET 0x24 63c4b05856SSrinivas Kandagatla #define ASC_RETRIES 0x28 64c4b05856SSrinivas Kandagatla 65c4b05856SSrinivas Kandagatla /* ASC_RXBUF */ 66c4b05856SSrinivas Kandagatla #define ASC_RXBUF_PE 0x100 67c4b05856SSrinivas Kandagatla #define ASC_RXBUF_FE 0x200 68c4b05856SSrinivas Kandagatla /** 69c4b05856SSrinivas Kandagatla * Some of status comes from higher bits of the character and some come from 70c4b05856SSrinivas Kandagatla * the status register. Combining both of them in to single status using dummy 71c4b05856SSrinivas Kandagatla * bits. 72c4b05856SSrinivas Kandagatla */ 73c4b05856SSrinivas Kandagatla #define ASC_RXBUF_DUMMY_RX 0x10000 74c4b05856SSrinivas Kandagatla #define ASC_RXBUF_DUMMY_BE 0x20000 75c4b05856SSrinivas Kandagatla #define ASC_RXBUF_DUMMY_OE 0x40000 76c4b05856SSrinivas Kandagatla 77c4b05856SSrinivas Kandagatla /* ASC_CTL */ 78c4b05856SSrinivas Kandagatla 79c4b05856SSrinivas Kandagatla #define ASC_CTL_MODE_MSK 0x0007 80c4b05856SSrinivas Kandagatla #define ASC_CTL_MODE_8BIT 0x0001 81c4b05856SSrinivas Kandagatla #define ASC_CTL_MODE_7BIT_PAR 0x0003 82c4b05856SSrinivas Kandagatla #define ASC_CTL_MODE_9BIT 0x0004 83c4b05856SSrinivas Kandagatla #define ASC_CTL_MODE_8BIT_WKUP 0x0005 84c4b05856SSrinivas Kandagatla #define ASC_CTL_MODE_8BIT_PAR 0x0007 85c4b05856SSrinivas Kandagatla #define ASC_CTL_STOP_MSK 0x0018 86c4b05856SSrinivas Kandagatla #define ASC_CTL_STOP_HALFBIT 0x0000 87c4b05856SSrinivas Kandagatla #define ASC_CTL_STOP_1BIT 0x0008 88c4b05856SSrinivas Kandagatla #define ASC_CTL_STOP_1_HALFBIT 0x0010 89c4b05856SSrinivas Kandagatla #define ASC_CTL_STOP_2BIT 0x0018 90c4b05856SSrinivas Kandagatla #define ASC_CTL_PARITYODD 0x0020 91c4b05856SSrinivas Kandagatla #define ASC_CTL_LOOPBACK 0x0040 92c4b05856SSrinivas Kandagatla #define ASC_CTL_RUN 0x0080 93c4b05856SSrinivas Kandagatla #define ASC_CTL_RXENABLE 0x0100 94c4b05856SSrinivas Kandagatla #define ASC_CTL_SCENABLE 0x0200 95c4b05856SSrinivas Kandagatla #define ASC_CTL_FIFOENABLE 0x0400 96c4b05856SSrinivas Kandagatla #define ASC_CTL_CTSENABLE 0x0800 97c4b05856SSrinivas Kandagatla #define ASC_CTL_BAUDMODE 0x1000 98c4b05856SSrinivas Kandagatla 99c4b05856SSrinivas Kandagatla /* ASC_GUARDTIME */ 100c4b05856SSrinivas Kandagatla 101c4b05856SSrinivas Kandagatla #define ASC_GUARDTIME_MSK 0x00FF 102c4b05856SSrinivas Kandagatla 103c4b05856SSrinivas Kandagatla /* ASC_INTEN */ 104c4b05856SSrinivas Kandagatla 105c4b05856SSrinivas Kandagatla #define ASC_INTEN_RBE 0x0001 106c4b05856SSrinivas Kandagatla #define ASC_INTEN_TE 0x0002 107c4b05856SSrinivas Kandagatla #define ASC_INTEN_THE 0x0004 108c4b05856SSrinivas Kandagatla #define ASC_INTEN_PE 0x0008 109c4b05856SSrinivas Kandagatla #define ASC_INTEN_FE 0x0010 110c4b05856SSrinivas Kandagatla #define ASC_INTEN_OE 0x0020 111c4b05856SSrinivas Kandagatla #define ASC_INTEN_TNE 0x0040 112c4b05856SSrinivas Kandagatla #define ASC_INTEN_TOI 0x0080 113c4b05856SSrinivas Kandagatla #define ASC_INTEN_RHF 0x0100 114c4b05856SSrinivas Kandagatla 115c4b05856SSrinivas Kandagatla /* ASC_RETRIES */ 116c4b05856SSrinivas Kandagatla 117c4b05856SSrinivas Kandagatla #define ASC_RETRIES_MSK 0x00FF 118c4b05856SSrinivas Kandagatla 119c4b05856SSrinivas Kandagatla /* ASC_RXBUF */ 120c4b05856SSrinivas Kandagatla 121c4b05856SSrinivas Kandagatla #define ASC_RXBUF_MSK 0x03FF 122c4b05856SSrinivas Kandagatla 123c4b05856SSrinivas Kandagatla /* ASC_STA */ 124c4b05856SSrinivas Kandagatla 125c4b05856SSrinivas Kandagatla #define ASC_STA_RBF 0x0001 126c4b05856SSrinivas Kandagatla #define ASC_STA_TE 0x0002 127c4b05856SSrinivas Kandagatla #define ASC_STA_THE 0x0004 128c4b05856SSrinivas Kandagatla #define ASC_STA_PE 0x0008 129c4b05856SSrinivas Kandagatla #define ASC_STA_FE 0x0010 130c4b05856SSrinivas Kandagatla #define ASC_STA_OE 0x0020 131c4b05856SSrinivas Kandagatla #define ASC_STA_TNE 0x0040 132c4b05856SSrinivas Kandagatla #define ASC_STA_TOI 0x0080 133c4b05856SSrinivas Kandagatla #define ASC_STA_RHF 0x0100 134c4b05856SSrinivas Kandagatla #define ASC_STA_TF 0x0200 135c4b05856SSrinivas Kandagatla #define ASC_STA_NKD 0x0400 136c4b05856SSrinivas Kandagatla 137c4b05856SSrinivas Kandagatla /* ASC_TIMEOUT */ 138c4b05856SSrinivas Kandagatla 139c4b05856SSrinivas Kandagatla #define ASC_TIMEOUT_MSK 0x00FF 140c4b05856SSrinivas Kandagatla 141c4b05856SSrinivas Kandagatla /* ASC_TXBUF */ 142c4b05856SSrinivas Kandagatla 143c4b05856SSrinivas Kandagatla #define ASC_TXBUF_MSK 0x01FF 144c4b05856SSrinivas Kandagatla 145c4b05856SSrinivas Kandagatla /*---- Inline function definitions ---------------------------*/ 146c4b05856SSrinivas Kandagatla 147c4b05856SSrinivas Kandagatla static inline struct asc_port *to_asc_port(struct uart_port *port) 148c4b05856SSrinivas Kandagatla { 149c4b05856SSrinivas Kandagatla return container_of(port, struct asc_port, port); 150c4b05856SSrinivas Kandagatla } 151c4b05856SSrinivas Kandagatla 152c4b05856SSrinivas Kandagatla static inline u32 asc_in(struct uart_port *port, u32 offset) 153c4b05856SSrinivas Kandagatla { 154*daea65a7SDaniel Thompson #ifdef readl_relaxed 15508177eceSDaniel Thompson return readl_relaxed(port->membase + offset); 156*daea65a7SDaniel Thompson #else 157*daea65a7SDaniel Thompson return readl(port->membase + offset); 158*daea65a7SDaniel Thompson #endif 159c4b05856SSrinivas Kandagatla } 160c4b05856SSrinivas Kandagatla 161c4b05856SSrinivas Kandagatla static inline void asc_out(struct uart_port *port, u32 offset, u32 value) 162c4b05856SSrinivas Kandagatla { 16308177eceSDaniel Thompson #ifdef writel_relaxed 16408177eceSDaniel Thompson writel_relaxed(value, port->membase + offset); 16508177eceSDaniel Thompson #else 166c4b05856SSrinivas Kandagatla writel(value, port->membase + offset); 16708177eceSDaniel Thompson #endif 168c4b05856SSrinivas Kandagatla } 169c4b05856SSrinivas Kandagatla 170c4b05856SSrinivas Kandagatla /* 171c4b05856SSrinivas Kandagatla * Some simple utility functions to enable and disable interrupts. 172c4b05856SSrinivas Kandagatla * Note that these need to be called with interrupts disabled. 173c4b05856SSrinivas Kandagatla */ 174c4b05856SSrinivas Kandagatla static inline void asc_disable_tx_interrupts(struct uart_port *port) 175c4b05856SSrinivas Kandagatla { 176c4b05856SSrinivas Kandagatla u32 intenable = asc_in(port, ASC_INTEN) & ~ASC_INTEN_THE; 177c4b05856SSrinivas Kandagatla asc_out(port, ASC_INTEN, intenable); 178c4b05856SSrinivas Kandagatla (void)asc_in(port, ASC_INTEN); /* Defeat bus write posting */ 179c4b05856SSrinivas Kandagatla } 180c4b05856SSrinivas Kandagatla 181c4b05856SSrinivas Kandagatla static inline void asc_enable_tx_interrupts(struct uart_port *port) 182c4b05856SSrinivas Kandagatla { 183c4b05856SSrinivas Kandagatla u32 intenable = asc_in(port, ASC_INTEN) | ASC_INTEN_THE; 184c4b05856SSrinivas Kandagatla asc_out(port, ASC_INTEN, intenable); 185c4b05856SSrinivas Kandagatla } 186c4b05856SSrinivas Kandagatla 187c4b05856SSrinivas Kandagatla static inline void asc_disable_rx_interrupts(struct uart_port *port) 188c4b05856SSrinivas Kandagatla { 189c4b05856SSrinivas Kandagatla u32 intenable = asc_in(port, ASC_INTEN) & ~ASC_INTEN_RBE; 190c4b05856SSrinivas Kandagatla asc_out(port, ASC_INTEN, intenable); 191c4b05856SSrinivas Kandagatla (void)asc_in(port, ASC_INTEN); /* Defeat bus write posting */ 192c4b05856SSrinivas Kandagatla } 193c4b05856SSrinivas Kandagatla 194c4b05856SSrinivas Kandagatla static inline void asc_enable_rx_interrupts(struct uart_port *port) 195c4b05856SSrinivas Kandagatla { 196c4b05856SSrinivas Kandagatla u32 intenable = asc_in(port, ASC_INTEN) | ASC_INTEN_RBE; 197c4b05856SSrinivas Kandagatla asc_out(port, ASC_INTEN, intenable); 198c4b05856SSrinivas Kandagatla } 199c4b05856SSrinivas Kandagatla 200c4b05856SSrinivas Kandagatla static inline u32 asc_txfifo_is_empty(struct uart_port *port) 201c4b05856SSrinivas Kandagatla { 202c4b05856SSrinivas Kandagatla return asc_in(port, ASC_STA) & ASC_STA_TE; 203c4b05856SSrinivas Kandagatla } 204c4b05856SSrinivas Kandagatla 2051ffcd67dSDaniel Thompson static inline u32 asc_txfifo_is_half_empty(struct uart_port *port) 206c4b05856SSrinivas Kandagatla { 2071ffcd67dSDaniel Thompson return asc_in(port, ASC_STA) & ASC_STA_THE; 208c4b05856SSrinivas Kandagatla } 209c4b05856SSrinivas Kandagatla 210c4b05856SSrinivas Kandagatla static inline const char *asc_port_name(struct uart_port *port) 211c4b05856SSrinivas Kandagatla { 212c4b05856SSrinivas Kandagatla return to_platform_device(port->dev)->name; 213c4b05856SSrinivas Kandagatla } 214c4b05856SSrinivas Kandagatla 215c4b05856SSrinivas Kandagatla /*----------------------------------------------------------------------*/ 216c4b05856SSrinivas Kandagatla 217c4b05856SSrinivas Kandagatla /* 218c4b05856SSrinivas Kandagatla * This section contains code to support the use of the ASC as a 219c4b05856SSrinivas Kandagatla * generic serial port. 220c4b05856SSrinivas Kandagatla */ 221c4b05856SSrinivas Kandagatla 222c4b05856SSrinivas Kandagatla static inline unsigned asc_hw_txroom(struct uart_port *port) 223c4b05856SSrinivas Kandagatla { 224c4b05856SSrinivas Kandagatla u32 status = asc_in(port, ASC_STA); 225c4b05856SSrinivas Kandagatla 226c4b05856SSrinivas Kandagatla if (status & ASC_STA_THE) 227c4b05856SSrinivas Kandagatla return port->fifosize / 2; 228c4b05856SSrinivas Kandagatla else if (!(status & ASC_STA_TF)) 229c4b05856SSrinivas Kandagatla return 1; 230c4b05856SSrinivas Kandagatla 231c4b05856SSrinivas Kandagatla return 0; 232c4b05856SSrinivas Kandagatla } 233c4b05856SSrinivas Kandagatla 234c4b05856SSrinivas Kandagatla /* 235c4b05856SSrinivas Kandagatla * Start transmitting chars. 236c4b05856SSrinivas Kandagatla * This is called from both interrupt and task level. 237c4b05856SSrinivas Kandagatla * Either way interrupts are disabled. 238c4b05856SSrinivas Kandagatla */ 239c4b05856SSrinivas Kandagatla static void asc_transmit_chars(struct uart_port *port) 240c4b05856SSrinivas Kandagatla { 241c4b05856SSrinivas Kandagatla struct circ_buf *xmit = &port->state->xmit; 242c4b05856SSrinivas Kandagatla int txroom; 243c4b05856SSrinivas Kandagatla unsigned char c; 244c4b05856SSrinivas Kandagatla 245c4b05856SSrinivas Kandagatla txroom = asc_hw_txroom(port); 246c4b05856SSrinivas Kandagatla 247c4b05856SSrinivas Kandagatla if ((txroom != 0) && port->x_char) { 248c4b05856SSrinivas Kandagatla c = port->x_char; 249c4b05856SSrinivas Kandagatla port->x_char = 0; 250c4b05856SSrinivas Kandagatla asc_out(port, ASC_TXBUF, c); 251c4b05856SSrinivas Kandagatla port->icount.tx++; 252c4b05856SSrinivas Kandagatla txroom = asc_hw_txroom(port); 253c4b05856SSrinivas Kandagatla } 254c4b05856SSrinivas Kandagatla 255c4b05856SSrinivas Kandagatla if (uart_tx_stopped(port)) { 256c4b05856SSrinivas Kandagatla /* 257c4b05856SSrinivas Kandagatla * We should try and stop the hardware here, but I 258c4b05856SSrinivas Kandagatla * don't think the ASC has any way to do that. 259c4b05856SSrinivas Kandagatla */ 260c4b05856SSrinivas Kandagatla asc_disable_tx_interrupts(port); 261c4b05856SSrinivas Kandagatla return; 262c4b05856SSrinivas Kandagatla } 263c4b05856SSrinivas Kandagatla 264c4b05856SSrinivas Kandagatla if (uart_circ_empty(xmit)) { 265c4b05856SSrinivas Kandagatla asc_disable_tx_interrupts(port); 266c4b05856SSrinivas Kandagatla return; 267c4b05856SSrinivas Kandagatla } 268c4b05856SSrinivas Kandagatla 269c4b05856SSrinivas Kandagatla if (txroom == 0) 270c4b05856SSrinivas Kandagatla return; 271c4b05856SSrinivas Kandagatla 272c4b05856SSrinivas Kandagatla do { 273c4b05856SSrinivas Kandagatla c = xmit->buf[xmit->tail]; 274c4b05856SSrinivas Kandagatla xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1); 275c4b05856SSrinivas Kandagatla asc_out(port, ASC_TXBUF, c); 276c4b05856SSrinivas Kandagatla port->icount.tx++; 277c4b05856SSrinivas Kandagatla txroom--; 278c4b05856SSrinivas Kandagatla } while ((txroom > 0) && (!uart_circ_empty(xmit))); 279c4b05856SSrinivas Kandagatla 280c4b05856SSrinivas Kandagatla if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS) 281c4b05856SSrinivas Kandagatla uart_write_wakeup(port); 282c4b05856SSrinivas Kandagatla 283c4b05856SSrinivas Kandagatla if (uart_circ_empty(xmit)) 284c4b05856SSrinivas Kandagatla asc_disable_tx_interrupts(port); 285c4b05856SSrinivas Kandagatla } 286c4b05856SSrinivas Kandagatla 287c4b05856SSrinivas Kandagatla static void asc_receive_chars(struct uart_port *port) 288c4b05856SSrinivas Kandagatla { 289c4b05856SSrinivas Kandagatla struct tty_port *tport = &port->state->port; 290c4b05856SSrinivas Kandagatla unsigned long status; 291c4b05856SSrinivas Kandagatla unsigned long c = 0; 292c4b05856SSrinivas Kandagatla char flag; 293c4b05856SSrinivas Kandagatla 294c4b05856SSrinivas Kandagatla if (port->irq_wake) 295c4b05856SSrinivas Kandagatla pm_wakeup_event(tport->tty->dev, 0); 296c4b05856SSrinivas Kandagatla 297c4b05856SSrinivas Kandagatla while ((status = asc_in(port, ASC_STA)) & ASC_STA_RBF) { 298c4b05856SSrinivas Kandagatla c = asc_in(port, ASC_RXBUF) | ASC_RXBUF_DUMMY_RX; 299c4b05856SSrinivas Kandagatla flag = TTY_NORMAL; 300c4b05856SSrinivas Kandagatla port->icount.rx++; 301c4b05856SSrinivas Kandagatla 302c4b05856SSrinivas Kandagatla if ((c & (ASC_RXBUF_FE | ASC_RXBUF_PE)) || 303c4b05856SSrinivas Kandagatla status & ASC_STA_OE) { 304c4b05856SSrinivas Kandagatla 305c4b05856SSrinivas Kandagatla if (c & ASC_RXBUF_FE) { 306c3c00b6fSDaniel Thompson if (c == (ASC_RXBUF_FE | ASC_RXBUF_DUMMY_RX)) { 307c4b05856SSrinivas Kandagatla port->icount.brk++; 308c4b05856SSrinivas Kandagatla if (uart_handle_break(port)) 309c4b05856SSrinivas Kandagatla continue; 310c4b05856SSrinivas Kandagatla c |= ASC_RXBUF_DUMMY_BE; 311c4b05856SSrinivas Kandagatla } else { 312c4b05856SSrinivas Kandagatla port->icount.frame++; 313c4b05856SSrinivas Kandagatla } 314c4b05856SSrinivas Kandagatla } else if (c & ASC_RXBUF_PE) { 315c4b05856SSrinivas Kandagatla port->icount.parity++; 316c4b05856SSrinivas Kandagatla } 317c4b05856SSrinivas Kandagatla /* 318c4b05856SSrinivas Kandagatla * Reading any data from the RX FIFO clears the 319c4b05856SSrinivas Kandagatla * overflow error condition. 320c4b05856SSrinivas Kandagatla */ 321c4b05856SSrinivas Kandagatla if (status & ASC_STA_OE) { 322c4b05856SSrinivas Kandagatla port->icount.overrun++; 323c4b05856SSrinivas Kandagatla c |= ASC_RXBUF_DUMMY_OE; 324c4b05856SSrinivas Kandagatla } 325c4b05856SSrinivas Kandagatla 326c4b05856SSrinivas Kandagatla c &= port->read_status_mask; 327c4b05856SSrinivas Kandagatla 328c4b05856SSrinivas Kandagatla if (c & ASC_RXBUF_DUMMY_BE) 329c4b05856SSrinivas Kandagatla flag = TTY_BREAK; 330c4b05856SSrinivas Kandagatla else if (c & ASC_RXBUF_PE) 331c4b05856SSrinivas Kandagatla flag = TTY_PARITY; 332c4b05856SSrinivas Kandagatla else if (c & ASC_RXBUF_FE) 333c4b05856SSrinivas Kandagatla flag = TTY_FRAME; 334c4b05856SSrinivas Kandagatla } 335c4b05856SSrinivas Kandagatla 336c3c00b6fSDaniel Thompson if (uart_handle_sysrq_char(port, c & 0xff)) 337c4b05856SSrinivas Kandagatla continue; 338c4b05856SSrinivas Kandagatla 339c4b05856SSrinivas Kandagatla uart_insert_char(port, c, ASC_RXBUF_DUMMY_OE, c & 0xff, flag); 340c4b05856SSrinivas Kandagatla } 341c4b05856SSrinivas Kandagatla 342c4b05856SSrinivas Kandagatla /* Tell the rest of the system the news. New characters! */ 343c4b05856SSrinivas Kandagatla tty_flip_buffer_push(tport); 344c4b05856SSrinivas Kandagatla } 345c4b05856SSrinivas Kandagatla 346c4b05856SSrinivas Kandagatla static irqreturn_t asc_interrupt(int irq, void *ptr) 347c4b05856SSrinivas Kandagatla { 348c4b05856SSrinivas Kandagatla struct uart_port *port = ptr; 349c4b05856SSrinivas Kandagatla u32 status; 350c4b05856SSrinivas Kandagatla 351c4b05856SSrinivas Kandagatla spin_lock(&port->lock); 352c4b05856SSrinivas Kandagatla 353c4b05856SSrinivas Kandagatla status = asc_in(port, ASC_STA); 354c4b05856SSrinivas Kandagatla 355c4b05856SSrinivas Kandagatla if (status & ASC_STA_RBF) { 356c4b05856SSrinivas Kandagatla /* Receive FIFO not empty */ 357c4b05856SSrinivas Kandagatla asc_receive_chars(port); 358c4b05856SSrinivas Kandagatla } 359c4b05856SSrinivas Kandagatla 360c4b05856SSrinivas Kandagatla if ((status & ASC_STA_THE) && 361c4b05856SSrinivas Kandagatla (asc_in(port, ASC_INTEN) & ASC_INTEN_THE)) { 362c4b05856SSrinivas Kandagatla /* Transmitter FIFO at least half empty */ 363c4b05856SSrinivas Kandagatla asc_transmit_chars(port); 364c4b05856SSrinivas Kandagatla } 365c4b05856SSrinivas Kandagatla 366c4b05856SSrinivas Kandagatla spin_unlock(&port->lock); 367c4b05856SSrinivas Kandagatla 368c4b05856SSrinivas Kandagatla return IRQ_HANDLED; 369c4b05856SSrinivas Kandagatla } 370c4b05856SSrinivas Kandagatla 371c4b05856SSrinivas Kandagatla /*----------------------------------------------------------------------*/ 372c4b05856SSrinivas Kandagatla 373c4b05856SSrinivas Kandagatla /* 374c4b05856SSrinivas Kandagatla * UART Functions 375c4b05856SSrinivas Kandagatla */ 376c4b05856SSrinivas Kandagatla 377c4b05856SSrinivas Kandagatla static unsigned int asc_tx_empty(struct uart_port *port) 378c4b05856SSrinivas Kandagatla { 379c4b05856SSrinivas Kandagatla return asc_txfifo_is_empty(port) ? TIOCSER_TEMT : 0; 380c4b05856SSrinivas Kandagatla } 381c4b05856SSrinivas Kandagatla 382c4b05856SSrinivas Kandagatla static void asc_set_mctrl(struct uart_port *port, unsigned int mctrl) 383c4b05856SSrinivas Kandagatla { 384c4b05856SSrinivas Kandagatla /* 385c4b05856SSrinivas Kandagatla * This routine is used for seting signals of: DTR, DCD, CTS/RTS 386c4b05856SSrinivas Kandagatla * We use ASC's hardware for CTS/RTS, so don't need any for that. 387c4b05856SSrinivas Kandagatla * Some boards have DTR and DCD implemented using PIO pins, 388c4b05856SSrinivas Kandagatla * code to do this should be hooked in here. 389c4b05856SSrinivas Kandagatla */ 390c4b05856SSrinivas Kandagatla } 391c4b05856SSrinivas Kandagatla 392c4b05856SSrinivas Kandagatla static unsigned int asc_get_mctrl(struct uart_port *port) 393c4b05856SSrinivas Kandagatla { 394c4b05856SSrinivas Kandagatla /* 395c4b05856SSrinivas Kandagatla * This routine is used for geting signals of: DTR, DCD, DSR, RI, 396c4b05856SSrinivas Kandagatla * and CTS/RTS 397c4b05856SSrinivas Kandagatla */ 398c4b05856SSrinivas Kandagatla return TIOCM_CAR | TIOCM_DSR | TIOCM_CTS; 399c4b05856SSrinivas Kandagatla } 400c4b05856SSrinivas Kandagatla 401c4b05856SSrinivas Kandagatla /* There are probably characters waiting to be transmitted. */ 402c4b05856SSrinivas Kandagatla static void asc_start_tx(struct uart_port *port) 403c4b05856SSrinivas Kandagatla { 404c4b05856SSrinivas Kandagatla struct circ_buf *xmit = &port->state->xmit; 405c4b05856SSrinivas Kandagatla 406c4b05856SSrinivas Kandagatla if (!uart_circ_empty(xmit)) 407c4b05856SSrinivas Kandagatla asc_enable_tx_interrupts(port); 408c4b05856SSrinivas Kandagatla } 409c4b05856SSrinivas Kandagatla 410c4b05856SSrinivas Kandagatla /* Transmit stop */ 411c4b05856SSrinivas Kandagatla static void asc_stop_tx(struct uart_port *port) 412c4b05856SSrinivas Kandagatla { 413c4b05856SSrinivas Kandagatla asc_disable_tx_interrupts(port); 414c4b05856SSrinivas Kandagatla } 415c4b05856SSrinivas Kandagatla 416c4b05856SSrinivas Kandagatla /* Receive stop */ 417c4b05856SSrinivas Kandagatla static void asc_stop_rx(struct uart_port *port) 418c4b05856SSrinivas Kandagatla { 419c4b05856SSrinivas Kandagatla asc_disable_rx_interrupts(port); 420c4b05856SSrinivas Kandagatla } 421c4b05856SSrinivas Kandagatla 422c4b05856SSrinivas Kandagatla /* Handle breaks - ignored by us */ 423c4b05856SSrinivas Kandagatla static void asc_break_ctl(struct uart_port *port, int break_state) 424c4b05856SSrinivas Kandagatla { 425c4b05856SSrinivas Kandagatla /* Nothing here yet .. */ 426c4b05856SSrinivas Kandagatla } 427c4b05856SSrinivas Kandagatla 428c4b05856SSrinivas Kandagatla /* 429c4b05856SSrinivas Kandagatla * Enable port for reception. 430c4b05856SSrinivas Kandagatla */ 431c4b05856SSrinivas Kandagatla static int asc_startup(struct uart_port *port) 432c4b05856SSrinivas Kandagatla { 433c4b05856SSrinivas Kandagatla if (request_irq(port->irq, asc_interrupt, IRQF_NO_SUSPEND, 434c4b05856SSrinivas Kandagatla asc_port_name(port), port)) { 435c4b05856SSrinivas Kandagatla dev_err(port->dev, "cannot allocate irq.\n"); 436c4b05856SSrinivas Kandagatla return -ENODEV; 437c4b05856SSrinivas Kandagatla } 438c4b05856SSrinivas Kandagatla 439c4b05856SSrinivas Kandagatla asc_transmit_chars(port); 440c4b05856SSrinivas Kandagatla asc_enable_rx_interrupts(port); 441c4b05856SSrinivas Kandagatla 442c4b05856SSrinivas Kandagatla return 0; 443c4b05856SSrinivas Kandagatla } 444c4b05856SSrinivas Kandagatla 445c4b05856SSrinivas Kandagatla static void asc_shutdown(struct uart_port *port) 446c4b05856SSrinivas Kandagatla { 447c4b05856SSrinivas Kandagatla asc_disable_tx_interrupts(port); 448c4b05856SSrinivas Kandagatla asc_disable_rx_interrupts(port); 449c4b05856SSrinivas Kandagatla free_irq(port->irq, port); 450c4b05856SSrinivas Kandagatla } 451c4b05856SSrinivas Kandagatla 452c4b05856SSrinivas Kandagatla static void asc_pm(struct uart_port *port, unsigned int state, 453c4b05856SSrinivas Kandagatla unsigned int oldstate) 454c4b05856SSrinivas Kandagatla { 455c4b05856SSrinivas Kandagatla struct asc_port *ascport = to_asc_port(port); 456c4b05856SSrinivas Kandagatla unsigned long flags = 0; 457c4b05856SSrinivas Kandagatla u32 ctl; 458c4b05856SSrinivas Kandagatla 459c4b05856SSrinivas Kandagatla switch (state) { 460c4b05856SSrinivas Kandagatla case UART_PM_STATE_ON: 461c4b05856SSrinivas Kandagatla clk_prepare_enable(ascport->clk); 462c4b05856SSrinivas Kandagatla break; 463c4b05856SSrinivas Kandagatla case UART_PM_STATE_OFF: 464c4b05856SSrinivas Kandagatla /* 465c4b05856SSrinivas Kandagatla * Disable the ASC baud rate generator, which is as close as 466c4b05856SSrinivas Kandagatla * we can come to turning it off. Note this is not called with 467c4b05856SSrinivas Kandagatla * the port spinlock held. 468c4b05856SSrinivas Kandagatla */ 469c4b05856SSrinivas Kandagatla spin_lock_irqsave(&port->lock, flags); 470c4b05856SSrinivas Kandagatla ctl = asc_in(port, ASC_CTL) & ~ASC_CTL_RUN; 471c4b05856SSrinivas Kandagatla asc_out(port, ASC_CTL, ctl); 472c4b05856SSrinivas Kandagatla spin_unlock_irqrestore(&port->lock, flags); 473c4b05856SSrinivas Kandagatla clk_disable_unprepare(ascport->clk); 474c4b05856SSrinivas Kandagatla break; 475c4b05856SSrinivas Kandagatla } 476c4b05856SSrinivas Kandagatla } 477c4b05856SSrinivas Kandagatla 478c4b05856SSrinivas Kandagatla static void asc_set_termios(struct uart_port *port, struct ktermios *termios, 479c4b05856SSrinivas Kandagatla struct ktermios *old) 480c4b05856SSrinivas Kandagatla { 481c4b05856SSrinivas Kandagatla struct asc_port *ascport = to_asc_port(port); 482c4b05856SSrinivas Kandagatla unsigned int baud; 483c4b05856SSrinivas Kandagatla u32 ctrl_val; 484c4b05856SSrinivas Kandagatla tcflag_t cflag; 485c4b05856SSrinivas Kandagatla unsigned long flags; 486c4b05856SSrinivas Kandagatla 487c4b05856SSrinivas Kandagatla /* Update termios to reflect hardware capabilities */ 488c4b05856SSrinivas Kandagatla termios->c_cflag &= ~(CMSPAR | 489c4b05856SSrinivas Kandagatla (ascport->hw_flow_control ? 0 : CRTSCTS)); 490c4b05856SSrinivas Kandagatla 491c4b05856SSrinivas Kandagatla port->uartclk = clk_get_rate(ascport->clk); 492c4b05856SSrinivas Kandagatla 493c4b05856SSrinivas Kandagatla baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/16); 494c4b05856SSrinivas Kandagatla cflag = termios->c_cflag; 495c4b05856SSrinivas Kandagatla 496c4b05856SSrinivas Kandagatla spin_lock_irqsave(&port->lock, flags); 497c4b05856SSrinivas Kandagatla 498c4b05856SSrinivas Kandagatla /* read control register */ 499c4b05856SSrinivas Kandagatla ctrl_val = asc_in(port, ASC_CTL); 500c4b05856SSrinivas Kandagatla 501c4b05856SSrinivas Kandagatla /* stop serial port and reset value */ 502c4b05856SSrinivas Kandagatla asc_out(port, ASC_CTL, (ctrl_val & ~ASC_CTL_RUN)); 503c4b05856SSrinivas Kandagatla ctrl_val = ASC_CTL_RXENABLE | ASC_CTL_FIFOENABLE; 504c4b05856SSrinivas Kandagatla 505c4b05856SSrinivas Kandagatla /* reset fifo rx & tx */ 506c4b05856SSrinivas Kandagatla asc_out(port, ASC_TXRESET, 1); 507c4b05856SSrinivas Kandagatla asc_out(port, ASC_RXRESET, 1); 508c4b05856SSrinivas Kandagatla 509c4b05856SSrinivas Kandagatla /* set character length */ 510c4b05856SSrinivas Kandagatla if ((cflag & CSIZE) == CS7) { 511c4b05856SSrinivas Kandagatla ctrl_val |= ASC_CTL_MODE_7BIT_PAR; 512c4b05856SSrinivas Kandagatla } else { 513c4b05856SSrinivas Kandagatla ctrl_val |= (cflag & PARENB) ? ASC_CTL_MODE_8BIT_PAR : 514c4b05856SSrinivas Kandagatla ASC_CTL_MODE_8BIT; 515c4b05856SSrinivas Kandagatla } 516c4b05856SSrinivas Kandagatla 517c4b05856SSrinivas Kandagatla /* set stop bit */ 518c4b05856SSrinivas Kandagatla ctrl_val |= (cflag & CSTOPB) ? ASC_CTL_STOP_2BIT : ASC_CTL_STOP_1BIT; 519c4b05856SSrinivas Kandagatla 520c4b05856SSrinivas Kandagatla /* odd parity */ 521c4b05856SSrinivas Kandagatla if (cflag & PARODD) 522c4b05856SSrinivas Kandagatla ctrl_val |= ASC_CTL_PARITYODD; 523c4b05856SSrinivas Kandagatla 524c4b05856SSrinivas Kandagatla /* hardware flow control */ 525c4b05856SSrinivas Kandagatla if ((cflag & CRTSCTS)) 526c4b05856SSrinivas Kandagatla ctrl_val |= ASC_CTL_CTSENABLE; 527c4b05856SSrinivas Kandagatla 528c4b05856SSrinivas Kandagatla if ((baud < 19200) && !ascport->force_m1) { 529c4b05856SSrinivas Kandagatla asc_out(port, ASC_BAUDRATE, (port->uartclk / (16 * baud))); 530c4b05856SSrinivas Kandagatla } else { 531c4b05856SSrinivas Kandagatla /* 532c4b05856SSrinivas Kandagatla * MODE 1: recommended for high bit rates (above 19.2K) 533c4b05856SSrinivas Kandagatla * 534c4b05856SSrinivas Kandagatla * baudrate * 16 * 2^16 535c4b05856SSrinivas Kandagatla * ASCBaudRate = ------------------------ 536c4b05856SSrinivas Kandagatla * inputclock 537c4b05856SSrinivas Kandagatla * 5381d6ba284SMaxime COQUELIN * To keep maths inside 64bits, we divide inputclock by 16. 539c4b05856SSrinivas Kandagatla */ 5401d6ba284SMaxime COQUELIN u64 dividend = (u64)baud * (1 << 16); 5411d6ba284SMaxime COQUELIN 5421d6ba284SMaxime COQUELIN do_div(dividend, port->uartclk / 16); 5431d6ba284SMaxime COQUELIN asc_out(port, ASC_BAUDRATE, dividend); 544c4b05856SSrinivas Kandagatla ctrl_val |= ASC_CTL_BAUDMODE; 545c4b05856SSrinivas Kandagatla } 546c4b05856SSrinivas Kandagatla 547c4b05856SSrinivas Kandagatla uart_update_timeout(port, cflag, baud); 548c4b05856SSrinivas Kandagatla 549c4b05856SSrinivas Kandagatla ascport->port.read_status_mask = ASC_RXBUF_DUMMY_OE; 550c4b05856SSrinivas Kandagatla if (termios->c_iflag & INPCK) 551c4b05856SSrinivas Kandagatla ascport->port.read_status_mask |= ASC_RXBUF_FE | ASC_RXBUF_PE; 552ef8b9ddcSPeter Hurley if (termios->c_iflag & (IGNBRK | BRKINT | PARMRK)) 553c4b05856SSrinivas Kandagatla ascport->port.read_status_mask |= ASC_RXBUF_DUMMY_BE; 554c4b05856SSrinivas Kandagatla 555c4b05856SSrinivas Kandagatla /* 556c4b05856SSrinivas Kandagatla * Characters to ignore 557c4b05856SSrinivas Kandagatla */ 558c4b05856SSrinivas Kandagatla ascport->port.ignore_status_mask = 0; 559c4b05856SSrinivas Kandagatla if (termios->c_iflag & IGNPAR) 560c4b05856SSrinivas Kandagatla ascport->port.ignore_status_mask |= ASC_RXBUF_FE | ASC_RXBUF_PE; 561c4b05856SSrinivas Kandagatla if (termios->c_iflag & IGNBRK) { 562c4b05856SSrinivas Kandagatla ascport->port.ignore_status_mask |= ASC_RXBUF_DUMMY_BE; 563c4b05856SSrinivas Kandagatla /* 564c4b05856SSrinivas Kandagatla * If we're ignoring parity and break indicators, 565c4b05856SSrinivas Kandagatla * ignore overruns too (for real raw support). 566c4b05856SSrinivas Kandagatla */ 567c4b05856SSrinivas Kandagatla if (termios->c_iflag & IGNPAR) 568c4b05856SSrinivas Kandagatla ascport->port.ignore_status_mask |= ASC_RXBUF_DUMMY_OE; 569c4b05856SSrinivas Kandagatla } 570c4b05856SSrinivas Kandagatla 571c4b05856SSrinivas Kandagatla /* 572c4b05856SSrinivas Kandagatla * Ignore all characters if CREAD is not set. 573c4b05856SSrinivas Kandagatla */ 574c4b05856SSrinivas Kandagatla if (!(termios->c_cflag & CREAD)) 575c4b05856SSrinivas Kandagatla ascport->port.ignore_status_mask |= ASC_RXBUF_DUMMY_RX; 576c4b05856SSrinivas Kandagatla 577c4b05856SSrinivas Kandagatla /* Set the timeout */ 578c4b05856SSrinivas Kandagatla asc_out(port, ASC_TIMEOUT, 20); 579c4b05856SSrinivas Kandagatla 580c4b05856SSrinivas Kandagatla /* write final value and enable port */ 581c4b05856SSrinivas Kandagatla asc_out(port, ASC_CTL, (ctrl_val | ASC_CTL_RUN)); 582c4b05856SSrinivas Kandagatla 583c4b05856SSrinivas Kandagatla spin_unlock_irqrestore(&port->lock, flags); 584c4b05856SSrinivas Kandagatla } 585c4b05856SSrinivas Kandagatla 586c4b05856SSrinivas Kandagatla static const char *asc_type(struct uart_port *port) 587c4b05856SSrinivas Kandagatla { 588c4b05856SSrinivas Kandagatla return (port->type == PORT_ASC) ? DRIVER_NAME : NULL; 589c4b05856SSrinivas Kandagatla } 590c4b05856SSrinivas Kandagatla 591c4b05856SSrinivas Kandagatla static void asc_release_port(struct uart_port *port) 592c4b05856SSrinivas Kandagatla { 593c4b05856SSrinivas Kandagatla } 594c4b05856SSrinivas Kandagatla 595c4b05856SSrinivas Kandagatla static int asc_request_port(struct uart_port *port) 596c4b05856SSrinivas Kandagatla { 597c4b05856SSrinivas Kandagatla return 0; 598c4b05856SSrinivas Kandagatla } 599c4b05856SSrinivas Kandagatla 600c4b05856SSrinivas Kandagatla /* 601c4b05856SSrinivas Kandagatla * Called when the port is opened, and UPF_BOOT_AUTOCONF flag is set 602c4b05856SSrinivas Kandagatla * Set type field if successful 603c4b05856SSrinivas Kandagatla */ 604c4b05856SSrinivas Kandagatla static void asc_config_port(struct uart_port *port, int flags) 605c4b05856SSrinivas Kandagatla { 606c4b05856SSrinivas Kandagatla if ((flags & UART_CONFIG_TYPE)) 607c4b05856SSrinivas Kandagatla port->type = PORT_ASC; 608c4b05856SSrinivas Kandagatla } 609c4b05856SSrinivas Kandagatla 610c4b05856SSrinivas Kandagatla static int 611c4b05856SSrinivas Kandagatla asc_verify_port(struct uart_port *port, struct serial_struct *ser) 612c4b05856SSrinivas Kandagatla { 613c4b05856SSrinivas Kandagatla /* No user changeable parameters */ 614c4b05856SSrinivas Kandagatla return -EINVAL; 615c4b05856SSrinivas Kandagatla } 616c4b05856SSrinivas Kandagatla 617c4b05856SSrinivas Kandagatla #ifdef CONFIG_CONSOLE_POLL 618c4b05856SSrinivas Kandagatla /* 619c4b05856SSrinivas Kandagatla * Console polling routines for writing and reading from the uart while 620c4b05856SSrinivas Kandagatla * in an interrupt or debug context (i.e. kgdb). 621c4b05856SSrinivas Kandagatla */ 622c4b05856SSrinivas Kandagatla 623c4b05856SSrinivas Kandagatla static int asc_get_poll_char(struct uart_port *port) 624c4b05856SSrinivas Kandagatla { 625c4b05856SSrinivas Kandagatla if (!(asc_in(port, ASC_STA) & ASC_STA_RBF)) 626c4b05856SSrinivas Kandagatla return NO_POLL_CHAR; 627c4b05856SSrinivas Kandagatla 628c4b05856SSrinivas Kandagatla return asc_in(port, ASC_RXBUF); 629c4b05856SSrinivas Kandagatla } 630c4b05856SSrinivas Kandagatla 631c4b05856SSrinivas Kandagatla static void asc_put_poll_char(struct uart_port *port, unsigned char c) 632c4b05856SSrinivas Kandagatla { 6331ffcd67dSDaniel Thompson while (!asc_txfifo_is_half_empty(port)) 634c4b05856SSrinivas Kandagatla cpu_relax(); 635c4b05856SSrinivas Kandagatla asc_out(port, ASC_TXBUF, c); 636c4b05856SSrinivas Kandagatla } 637c4b05856SSrinivas Kandagatla 638c4b05856SSrinivas Kandagatla #endif /* CONFIG_CONSOLE_POLL */ 639c4b05856SSrinivas Kandagatla 640c4b05856SSrinivas Kandagatla /*---------------------------------------------------------------------*/ 641c4b05856SSrinivas Kandagatla 642c4b05856SSrinivas Kandagatla static struct uart_ops asc_uart_ops = { 643c4b05856SSrinivas Kandagatla .tx_empty = asc_tx_empty, 644c4b05856SSrinivas Kandagatla .set_mctrl = asc_set_mctrl, 645c4b05856SSrinivas Kandagatla .get_mctrl = asc_get_mctrl, 646c4b05856SSrinivas Kandagatla .start_tx = asc_start_tx, 647c4b05856SSrinivas Kandagatla .stop_tx = asc_stop_tx, 648c4b05856SSrinivas Kandagatla .stop_rx = asc_stop_rx, 649c4b05856SSrinivas Kandagatla .break_ctl = asc_break_ctl, 650c4b05856SSrinivas Kandagatla .startup = asc_startup, 651c4b05856SSrinivas Kandagatla .shutdown = asc_shutdown, 652c4b05856SSrinivas Kandagatla .set_termios = asc_set_termios, 653c4b05856SSrinivas Kandagatla .type = asc_type, 654c4b05856SSrinivas Kandagatla .release_port = asc_release_port, 655c4b05856SSrinivas Kandagatla .request_port = asc_request_port, 656c4b05856SSrinivas Kandagatla .config_port = asc_config_port, 657c4b05856SSrinivas Kandagatla .verify_port = asc_verify_port, 658c4b05856SSrinivas Kandagatla .pm = asc_pm, 659c4b05856SSrinivas Kandagatla #ifdef CONFIG_CONSOLE_POLL 660c4b05856SSrinivas Kandagatla .poll_get_char = asc_get_poll_char, 661c4b05856SSrinivas Kandagatla .poll_put_char = asc_put_poll_char, 662c4b05856SSrinivas Kandagatla #endif /* CONFIG_CONSOLE_POLL */ 663c4b05856SSrinivas Kandagatla }; 664c4b05856SSrinivas Kandagatla 665c4b05856SSrinivas Kandagatla static int asc_init_port(struct asc_port *ascport, 666c4b05856SSrinivas Kandagatla struct platform_device *pdev) 667c4b05856SSrinivas Kandagatla { 668c4b05856SSrinivas Kandagatla struct uart_port *port = &ascport->port; 6693abeff77SJulia Lawall struct resource *res; 670c4b05856SSrinivas Kandagatla 671c4b05856SSrinivas Kandagatla port->iotype = UPIO_MEM; 672c4b05856SSrinivas Kandagatla port->flags = UPF_BOOT_AUTOCONF; 673c4b05856SSrinivas Kandagatla port->ops = &asc_uart_ops; 674c4b05856SSrinivas Kandagatla port->fifosize = ASC_FIFO_SIZE; 675c4b05856SSrinivas Kandagatla port->dev = &pdev->dev; 676c4b05856SSrinivas Kandagatla port->irq = platform_get_irq(pdev, 0); 677c4b05856SSrinivas Kandagatla 6783abeff77SJulia Lawall res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 6793abeff77SJulia Lawall port->membase = devm_ioremap_resource(&pdev->dev, res); 6803abeff77SJulia Lawall if (IS_ERR(port->membase)) 6813abeff77SJulia Lawall return PTR_ERR(port->membase); 6823abeff77SJulia Lawall port->mapbase = res->start; 683c4b05856SSrinivas Kandagatla 684c4b05856SSrinivas Kandagatla spin_lock_init(&port->lock); 685c4b05856SSrinivas Kandagatla 686c4b05856SSrinivas Kandagatla ascport->clk = devm_clk_get(&pdev->dev, NULL); 687c4b05856SSrinivas Kandagatla 688c4b05856SSrinivas Kandagatla if (WARN_ON(IS_ERR(ascport->clk))) 689c4b05856SSrinivas Kandagatla return -EINVAL; 690c4b05856SSrinivas Kandagatla /* ensure that clk rate is correct by enabling the clk */ 691c4b05856SSrinivas Kandagatla clk_prepare_enable(ascport->clk); 692c4b05856SSrinivas Kandagatla ascport->port.uartclk = clk_get_rate(ascport->clk); 693c4b05856SSrinivas Kandagatla WARN_ON(ascport->port.uartclk == 0); 694c4b05856SSrinivas Kandagatla clk_disable_unprepare(ascport->clk); 695c4b05856SSrinivas Kandagatla 696c4b05856SSrinivas Kandagatla return 0; 697c4b05856SSrinivas Kandagatla } 698c4b05856SSrinivas Kandagatla 699c4b05856SSrinivas Kandagatla static struct asc_port *asc_of_get_asc_port(struct platform_device *pdev) 700c4b05856SSrinivas Kandagatla { 701c4b05856SSrinivas Kandagatla struct device_node *np = pdev->dev.of_node; 702c4b05856SSrinivas Kandagatla int id; 703c4b05856SSrinivas Kandagatla 704c4b05856SSrinivas Kandagatla if (!np) 705c4b05856SSrinivas Kandagatla return NULL; 706c4b05856SSrinivas Kandagatla 707c4b05856SSrinivas Kandagatla id = of_alias_get_id(np, ASC_SERIAL_NAME); 708c4b05856SSrinivas Kandagatla 709c4b05856SSrinivas Kandagatla if (id < 0) 710c4b05856SSrinivas Kandagatla id = 0; 711c4b05856SSrinivas Kandagatla 712c4b05856SSrinivas Kandagatla if (WARN_ON(id >= ASC_MAX_PORTS)) 713c4b05856SSrinivas Kandagatla return NULL; 714c4b05856SSrinivas Kandagatla 715c4b05856SSrinivas Kandagatla asc_ports[id].hw_flow_control = of_property_read_bool(np, 716c4b05856SSrinivas Kandagatla "st,hw-flow-control"); 717c4b05856SSrinivas Kandagatla asc_ports[id].force_m1 = of_property_read_bool(np, "st,force_m1"); 718c4b05856SSrinivas Kandagatla asc_ports[id].port.line = id; 719c4b05856SSrinivas Kandagatla return &asc_ports[id]; 720c4b05856SSrinivas Kandagatla } 721c4b05856SSrinivas Kandagatla 722a4f2dc9eSSrinivas Kandagatla #ifdef CONFIG_OF 723c4b05856SSrinivas Kandagatla static struct of_device_id asc_match[] = { 724c4b05856SSrinivas Kandagatla { .compatible = "st,asc", }, 725c4b05856SSrinivas Kandagatla {}, 726c4b05856SSrinivas Kandagatla }; 727c4b05856SSrinivas Kandagatla 728c4b05856SSrinivas Kandagatla MODULE_DEVICE_TABLE(of, asc_match); 729a4f2dc9eSSrinivas Kandagatla #endif 730c4b05856SSrinivas Kandagatla 731c4b05856SSrinivas Kandagatla static int asc_serial_probe(struct platform_device *pdev) 732c4b05856SSrinivas Kandagatla { 733c4b05856SSrinivas Kandagatla int ret; 734c4b05856SSrinivas Kandagatla struct asc_port *ascport; 735c4b05856SSrinivas Kandagatla 736c4b05856SSrinivas Kandagatla ascport = asc_of_get_asc_port(pdev); 737c4b05856SSrinivas Kandagatla if (!ascport) 738c4b05856SSrinivas Kandagatla return -ENODEV; 739c4b05856SSrinivas Kandagatla 740c4b05856SSrinivas Kandagatla ret = asc_init_port(ascport, pdev); 741c4b05856SSrinivas Kandagatla if (ret) 742c4b05856SSrinivas Kandagatla return ret; 743c4b05856SSrinivas Kandagatla 744c4b05856SSrinivas Kandagatla ret = uart_add_one_port(&asc_uart_driver, &ascport->port); 745c4b05856SSrinivas Kandagatla if (ret) 746c4b05856SSrinivas Kandagatla return ret; 747c4b05856SSrinivas Kandagatla 748c4b05856SSrinivas Kandagatla platform_set_drvdata(pdev, &ascport->port); 749c4b05856SSrinivas Kandagatla 750c4b05856SSrinivas Kandagatla return 0; 751c4b05856SSrinivas Kandagatla } 752c4b05856SSrinivas Kandagatla 753c4b05856SSrinivas Kandagatla static int asc_serial_remove(struct platform_device *pdev) 754c4b05856SSrinivas Kandagatla { 755c4b05856SSrinivas Kandagatla struct uart_port *port = platform_get_drvdata(pdev); 756c4b05856SSrinivas Kandagatla 757c4b05856SSrinivas Kandagatla return uart_remove_one_port(&asc_uart_driver, port); 758c4b05856SSrinivas Kandagatla } 759c4b05856SSrinivas Kandagatla 760c4b05856SSrinivas Kandagatla #ifdef CONFIG_PM_SLEEP 761c4b05856SSrinivas Kandagatla static int asc_serial_suspend(struct device *dev) 762c4b05856SSrinivas Kandagatla { 763c4b05856SSrinivas Kandagatla struct platform_device *pdev = to_platform_device(dev); 764c4b05856SSrinivas Kandagatla struct uart_port *port = platform_get_drvdata(pdev); 765c4b05856SSrinivas Kandagatla 766c4b05856SSrinivas Kandagatla return uart_suspend_port(&asc_uart_driver, port); 767c4b05856SSrinivas Kandagatla } 768c4b05856SSrinivas Kandagatla 769c4b05856SSrinivas Kandagatla static int asc_serial_resume(struct device *dev) 770c4b05856SSrinivas Kandagatla { 771c4b05856SSrinivas Kandagatla struct platform_device *pdev = to_platform_device(dev); 772c4b05856SSrinivas Kandagatla struct uart_port *port = platform_get_drvdata(pdev); 773c4b05856SSrinivas Kandagatla 774c4b05856SSrinivas Kandagatla return uart_resume_port(&asc_uart_driver, port); 775c4b05856SSrinivas Kandagatla } 776c4b05856SSrinivas Kandagatla 777c4b05856SSrinivas Kandagatla #endif /* CONFIG_PM_SLEEP */ 778c4b05856SSrinivas Kandagatla 779c4b05856SSrinivas Kandagatla /*----------------------------------------------------------------------*/ 780c4b05856SSrinivas Kandagatla 781c4b05856SSrinivas Kandagatla #ifdef CONFIG_SERIAL_ST_ASC_CONSOLE 782c4b05856SSrinivas Kandagatla static void asc_console_putchar(struct uart_port *port, int ch) 783c4b05856SSrinivas Kandagatla { 784c4b05856SSrinivas Kandagatla unsigned int timeout = 1000000; 785c4b05856SSrinivas Kandagatla 786c4b05856SSrinivas Kandagatla /* Wait for upto 1 second in case flow control is stopping us. */ 7871ffcd67dSDaniel Thompson while (--timeout && !asc_txfifo_is_half_empty(port)) 788c4b05856SSrinivas Kandagatla udelay(1); 789c4b05856SSrinivas Kandagatla 790c4b05856SSrinivas Kandagatla asc_out(port, ASC_TXBUF, ch); 791c4b05856SSrinivas Kandagatla } 792c4b05856SSrinivas Kandagatla 793c4b05856SSrinivas Kandagatla /* 794c4b05856SSrinivas Kandagatla * Print a string to the serial port trying not to disturb 795c4b05856SSrinivas Kandagatla * any possible real use of the port... 796c4b05856SSrinivas Kandagatla */ 797c4b05856SSrinivas Kandagatla 798c4b05856SSrinivas Kandagatla static void asc_console_write(struct console *co, const char *s, unsigned count) 799c4b05856SSrinivas Kandagatla { 800c4b05856SSrinivas Kandagatla struct uart_port *port = &asc_ports[co->index].port; 801c4b05856SSrinivas Kandagatla unsigned long flags; 802c4b05856SSrinivas Kandagatla unsigned long timeout = 1000000; 803c4b05856SSrinivas Kandagatla int locked = 1; 804c4b05856SSrinivas Kandagatla u32 intenable; 805c4b05856SSrinivas Kandagatla 806c4b05856SSrinivas Kandagatla local_irq_save(flags); 807c4b05856SSrinivas Kandagatla if (port->sysrq) 808c4b05856SSrinivas Kandagatla locked = 0; /* asc_interrupt has already claimed the lock */ 809c4b05856SSrinivas Kandagatla else if (oops_in_progress) 810c4b05856SSrinivas Kandagatla locked = spin_trylock(&port->lock); 811c4b05856SSrinivas Kandagatla else 812c4b05856SSrinivas Kandagatla spin_lock(&port->lock); 813c4b05856SSrinivas Kandagatla 814c4b05856SSrinivas Kandagatla /* 815c4b05856SSrinivas Kandagatla * Disable interrupts so we don't get the IRQ line bouncing 816c4b05856SSrinivas Kandagatla * up and down while interrupts are disabled. 817c4b05856SSrinivas Kandagatla */ 818c4b05856SSrinivas Kandagatla intenable = asc_in(port, ASC_INTEN); 819c4b05856SSrinivas Kandagatla asc_out(port, ASC_INTEN, 0); 820c4b05856SSrinivas Kandagatla (void)asc_in(port, ASC_INTEN); /* Defeat bus write posting */ 821c4b05856SSrinivas Kandagatla 822c4b05856SSrinivas Kandagatla uart_console_write(port, s, count, asc_console_putchar); 823c4b05856SSrinivas Kandagatla 824c4b05856SSrinivas Kandagatla while (--timeout && !asc_txfifo_is_empty(port)) 825c4b05856SSrinivas Kandagatla udelay(1); 826c4b05856SSrinivas Kandagatla 827c4b05856SSrinivas Kandagatla asc_out(port, ASC_INTEN, intenable); 828c4b05856SSrinivas Kandagatla 829c4b05856SSrinivas Kandagatla if (locked) 830c4b05856SSrinivas Kandagatla spin_unlock(&port->lock); 831c4b05856SSrinivas Kandagatla local_irq_restore(flags); 832c4b05856SSrinivas Kandagatla } 833c4b05856SSrinivas Kandagatla 834c4b05856SSrinivas Kandagatla static int asc_console_setup(struct console *co, char *options) 835c4b05856SSrinivas Kandagatla { 836c4b05856SSrinivas Kandagatla struct asc_port *ascport; 837c4b05856SSrinivas Kandagatla int baud = 9600; 838c4b05856SSrinivas Kandagatla int bits = 8; 839c4b05856SSrinivas Kandagatla int parity = 'n'; 840c4b05856SSrinivas Kandagatla int flow = 'n'; 841c4b05856SSrinivas Kandagatla 842c4b05856SSrinivas Kandagatla if (co->index >= ASC_MAX_PORTS) 843c4b05856SSrinivas Kandagatla return -ENODEV; 844c4b05856SSrinivas Kandagatla 845c4b05856SSrinivas Kandagatla ascport = &asc_ports[co->index]; 846c4b05856SSrinivas Kandagatla 847c4b05856SSrinivas Kandagatla /* 848c4b05856SSrinivas Kandagatla * This driver does not support early console initialization 849c4b05856SSrinivas Kandagatla * (use ARM early printk support instead), so we only expect 850c4b05856SSrinivas Kandagatla * this to be called during the uart port registration when the 851c4b05856SSrinivas Kandagatla * driver gets probed and the port should be mapped at that point. 852c4b05856SSrinivas Kandagatla */ 8535b4e79aeSMaxime COQUELIN if (ascport->port.mapbase == 0 || ascport->port.membase == NULL) 8545b4e79aeSMaxime COQUELIN return -ENXIO; 855c4b05856SSrinivas Kandagatla 856c4b05856SSrinivas Kandagatla if (options) 857c4b05856SSrinivas Kandagatla uart_parse_options(options, &baud, &parity, &bits, &flow); 858c4b05856SSrinivas Kandagatla 859c4b05856SSrinivas Kandagatla return uart_set_options(&ascport->port, co, baud, parity, bits, flow); 860c4b05856SSrinivas Kandagatla } 861c4b05856SSrinivas Kandagatla 862c4b05856SSrinivas Kandagatla static struct console asc_console = { 863c4b05856SSrinivas Kandagatla .name = ASC_SERIAL_NAME, 864c4b05856SSrinivas Kandagatla .device = uart_console_device, 865c4b05856SSrinivas Kandagatla .write = asc_console_write, 866c4b05856SSrinivas Kandagatla .setup = asc_console_setup, 867c4b05856SSrinivas Kandagatla .flags = CON_PRINTBUFFER, 868c4b05856SSrinivas Kandagatla .index = -1, 869c4b05856SSrinivas Kandagatla .data = &asc_uart_driver, 870c4b05856SSrinivas Kandagatla }; 871c4b05856SSrinivas Kandagatla 872c4b05856SSrinivas Kandagatla #define ASC_SERIAL_CONSOLE (&asc_console) 873c4b05856SSrinivas Kandagatla 874c4b05856SSrinivas Kandagatla #else 875c4b05856SSrinivas Kandagatla #define ASC_SERIAL_CONSOLE NULL 876c4b05856SSrinivas Kandagatla #endif /* CONFIG_SERIAL_ST_ASC_CONSOLE */ 877c4b05856SSrinivas Kandagatla 878c4b05856SSrinivas Kandagatla static struct uart_driver asc_uart_driver = { 879c4b05856SSrinivas Kandagatla .owner = THIS_MODULE, 880c4b05856SSrinivas Kandagatla .driver_name = DRIVER_NAME, 881c4b05856SSrinivas Kandagatla .dev_name = ASC_SERIAL_NAME, 882c4b05856SSrinivas Kandagatla .major = 0, 883c4b05856SSrinivas Kandagatla .minor = 0, 884c4b05856SSrinivas Kandagatla .nr = ASC_MAX_PORTS, 885c4b05856SSrinivas Kandagatla .cons = ASC_SERIAL_CONSOLE, 886c4b05856SSrinivas Kandagatla }; 887c4b05856SSrinivas Kandagatla 888c4b05856SSrinivas Kandagatla static const struct dev_pm_ops asc_serial_pm_ops = { 889c4b05856SSrinivas Kandagatla SET_SYSTEM_SLEEP_PM_OPS(asc_serial_suspend, asc_serial_resume) 890c4b05856SSrinivas Kandagatla }; 891c4b05856SSrinivas Kandagatla 892c4b05856SSrinivas Kandagatla static struct platform_driver asc_serial_driver = { 893c4b05856SSrinivas Kandagatla .probe = asc_serial_probe, 894c4b05856SSrinivas Kandagatla .remove = asc_serial_remove, 895c4b05856SSrinivas Kandagatla .driver = { 896c4b05856SSrinivas Kandagatla .name = DRIVER_NAME, 897c4b05856SSrinivas Kandagatla .pm = &asc_serial_pm_ops, 898c4b05856SSrinivas Kandagatla .owner = THIS_MODULE, 899c4b05856SSrinivas Kandagatla .of_match_table = of_match_ptr(asc_match), 900c4b05856SSrinivas Kandagatla }, 901c4b05856SSrinivas Kandagatla }; 902c4b05856SSrinivas Kandagatla 903c4b05856SSrinivas Kandagatla static int __init asc_init(void) 904c4b05856SSrinivas Kandagatla { 905c4b05856SSrinivas Kandagatla int ret; 906c4b05856SSrinivas Kandagatla static char banner[] __initdata = 907c4b05856SSrinivas Kandagatla KERN_INFO "STMicroelectronics ASC driver initialized\n"; 908c4b05856SSrinivas Kandagatla 909c4b05856SSrinivas Kandagatla printk(banner); 910c4b05856SSrinivas Kandagatla 911c4b05856SSrinivas Kandagatla ret = uart_register_driver(&asc_uart_driver); 912c4b05856SSrinivas Kandagatla if (ret) 913c4b05856SSrinivas Kandagatla return ret; 914c4b05856SSrinivas Kandagatla 915c4b05856SSrinivas Kandagatla ret = platform_driver_register(&asc_serial_driver); 916c4b05856SSrinivas Kandagatla if (ret) 917c4b05856SSrinivas Kandagatla uart_unregister_driver(&asc_uart_driver); 918c4b05856SSrinivas Kandagatla 919c4b05856SSrinivas Kandagatla return ret; 920c4b05856SSrinivas Kandagatla } 921c4b05856SSrinivas Kandagatla 922c4b05856SSrinivas Kandagatla static void __exit asc_exit(void) 923c4b05856SSrinivas Kandagatla { 924c4b05856SSrinivas Kandagatla platform_driver_unregister(&asc_serial_driver); 925c4b05856SSrinivas Kandagatla uart_unregister_driver(&asc_uart_driver); 926c4b05856SSrinivas Kandagatla } 927c4b05856SSrinivas Kandagatla 928c4b05856SSrinivas Kandagatla module_init(asc_init); 929c4b05856SSrinivas Kandagatla module_exit(asc_exit); 930c4b05856SSrinivas Kandagatla 931c4b05856SSrinivas Kandagatla MODULE_ALIAS("platform:" DRIVER_NAME); 932c4b05856SSrinivas Kandagatla MODULE_AUTHOR("STMicroelectronics (R&D) Limited"); 933c4b05856SSrinivas Kandagatla MODULE_DESCRIPTION("STMicroelectronics ASC serial port driver"); 934c4b05856SSrinivas Kandagatla MODULE_LICENSE("GPL"); 935