Greg Kroah-Hartman | e3b3d0f | 2017-11-06 18:11:51 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Jovi Zhang | 99edb3d | 2011-03-30 05:30:41 -0400 | [diff] [blame] | 2 | /* |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 3 | * Driver core for Samsung SoC onboard UARTs. |
| 4 | * |
Ben Dooks | ccae941 | 2009-11-13 22:54:14 +0000 | [diff] [blame] | 5 | * Ben Dooks, Copyright (c) 2003-2008 Simtec Electronics |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 6 | * http://armlinux.simtec.co.uk/ |
Greg Kroah-Hartman | 7c17525 | 2019-12-10 15:37:05 +0100 | [diff] [blame] | 7 | */ |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 8 | |
Tamseel Shams | c89511f | 2020-06-17 16:29:07 +0530 | [diff] [blame^] | 9 | /* Note on 2410 error handling |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 10 | * |
| 11 | * The s3c2410 manual has a love/hate affair with the contents of the |
| 12 | * UERSTAT register in the UART blocks, and keeps marking some of the |
| 13 | * error bits as reserved. Having checked with the s3c2410x01, |
| 14 | * it copes with BREAKs properly, so I am happy to ignore the RESERVED |
| 15 | * feature from the latter versions of the manual. |
| 16 | * |
| 17 | * If it becomes aparrent that latter versions of the 2410 remove these |
| 18 | * bits, then action will have to be taken to differentiate the versions |
| 19 | * and change the policy on BREAK |
| 20 | * |
| 21 | * BJD, 04-Nov-2004 |
Greg Kroah-Hartman | 7c17525 | 2019-12-10 15:37:05 +0100 | [diff] [blame] | 22 | */ |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 23 | |
Robert Baldyga | 62c37ee | 2014-12-10 12:49:25 +0100 | [diff] [blame] | 24 | #include <linux/dmaengine.h> |
| 25 | #include <linux/dma-mapping.h> |
| 26 | #include <linux/slab.h> |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 27 | #include <linux/module.h> |
| 28 | #include <linux/ioport.h> |
| 29 | #include <linux/io.h> |
| 30 | #include <linux/platform_device.h> |
| 31 | #include <linux/init.h> |
| 32 | #include <linux/sysrq.h> |
| 33 | #include <linux/console.h> |
| 34 | #include <linux/tty.h> |
| 35 | #include <linux/tty_flip.h> |
| 36 | #include <linux/serial_core.h> |
| 37 | #include <linux/serial.h> |
Arnd Bergmann | 9ee51f0 | 2013-04-11 02:04:48 +0200 | [diff] [blame] | 38 | #include <linux/serial_s3c.h> |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 39 | #include <linux/delay.h> |
| 40 | #include <linux/clk.h> |
Ben Dooks | 3055547 | 2008-10-21 14:06:36 +0100 | [diff] [blame] | 41 | #include <linux/cpufreq.h> |
Thomas Abraham | 26c919e | 2011-11-06 22:10:44 +0530 | [diff] [blame] | 42 | #include <linux/of.h> |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 43 | #include <asm/irq.h> |
| 44 | |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 45 | /* UART name and device definitions */ |
| 46 | |
| 47 | #define S3C24XX_SERIAL_NAME "ttySAC" |
| 48 | #define S3C24XX_SERIAL_MAJOR 204 |
| 49 | #define S3C24XX_SERIAL_MINOR 64 |
| 50 | |
Robert Baldyga | 29bef79 | 2014-12-10 12:49:26 +0100 | [diff] [blame] | 51 | #define S3C24XX_TX_PIO 1 |
| 52 | #define S3C24XX_TX_DMA 2 |
Robert Baldyga | b543c30 | 2014-12-10 12:49:27 +0100 | [diff] [blame] | 53 | #define S3C24XX_RX_PIO 1 |
| 54 | #define S3C24XX_RX_DMA 2 |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 55 | |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 56 | /* flag to ignore all characters coming in */ |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 57 | #define RXSTAT_DUMMY_READ (0x10000000) |
| 58 | |
Greg Kroah-Hartman | 43df170 | 2019-12-10 15:37:01 +0100 | [diff] [blame] | 59 | struct s3c24xx_uart_info { |
| 60 | char *name; |
| 61 | unsigned int type; |
| 62 | unsigned int fifosize; |
| 63 | unsigned long rx_fifomask; |
| 64 | unsigned long rx_fifoshift; |
| 65 | unsigned long rx_fifofull; |
| 66 | unsigned long tx_fifomask; |
| 67 | unsigned long tx_fifoshift; |
| 68 | unsigned long tx_fifofull; |
| 69 | unsigned int def_clk_sel; |
| 70 | unsigned long num_clks; |
| 71 | unsigned long clksel_mask; |
| 72 | unsigned long clksel_shift; |
| 73 | |
| 74 | /* uart port features */ |
| 75 | |
| 76 | unsigned int has_divslot:1; |
| 77 | }; |
| 78 | |
| 79 | struct s3c24xx_serial_drv_data { |
| 80 | struct s3c24xx_uart_info *info; |
| 81 | struct s3c2410_uartcfg *def_cfg; |
| 82 | unsigned int fifosize[CONFIG_SERIAL_SAMSUNG_UARTS]; |
| 83 | }; |
| 84 | |
| 85 | struct s3c24xx_uart_dma { |
| 86 | unsigned int rx_chan_id; |
| 87 | unsigned int tx_chan_id; |
| 88 | |
| 89 | struct dma_slave_config rx_conf; |
| 90 | struct dma_slave_config tx_conf; |
| 91 | |
| 92 | struct dma_chan *rx_chan; |
| 93 | struct dma_chan *tx_chan; |
| 94 | |
| 95 | dma_addr_t rx_addr; |
| 96 | dma_addr_t tx_addr; |
| 97 | |
| 98 | dma_cookie_t rx_cookie; |
| 99 | dma_cookie_t tx_cookie; |
| 100 | |
| 101 | char *rx_buf; |
| 102 | |
| 103 | dma_addr_t tx_transfer_addr; |
| 104 | |
| 105 | size_t rx_size; |
| 106 | size_t tx_size; |
| 107 | |
| 108 | struct dma_async_tx_descriptor *tx_desc; |
| 109 | struct dma_async_tx_descriptor *rx_desc; |
| 110 | |
| 111 | int tx_bytes_requested; |
| 112 | int rx_bytes_requested; |
| 113 | }; |
| 114 | |
| 115 | struct s3c24xx_uart_port { |
| 116 | unsigned char rx_claimed; |
| 117 | unsigned char tx_claimed; |
Greg Kroah-Hartman | 8336240 | 2019-12-17 15:02:32 +0100 | [diff] [blame] | 118 | unsigned char rx_enabled; |
| 119 | unsigned char tx_enabled; |
Greg Kroah-Hartman | 43df170 | 2019-12-10 15:37:01 +0100 | [diff] [blame] | 120 | unsigned int pm_level; |
| 121 | unsigned long baudclk_rate; |
| 122 | unsigned int min_dma_size; |
| 123 | |
| 124 | unsigned int rx_irq; |
| 125 | unsigned int tx_irq; |
| 126 | |
| 127 | unsigned int tx_in_progress; |
| 128 | unsigned int tx_mode; |
| 129 | unsigned int rx_mode; |
| 130 | |
| 131 | struct s3c24xx_uart_info *info; |
| 132 | struct clk *clk; |
| 133 | struct clk *baudclk; |
| 134 | struct uart_port port; |
| 135 | struct s3c24xx_serial_drv_data *drv_data; |
| 136 | |
| 137 | /* reference to platform data */ |
| 138 | struct s3c2410_uartcfg *cfg; |
| 139 | |
| 140 | struct s3c24xx_uart_dma *dma; |
| 141 | |
| 142 | #ifdef CONFIG_ARM_S3C24XX_CPUFREQ |
| 143 | struct notifier_block freq_transition; |
| 144 | #endif |
| 145 | }; |
| 146 | |
| 147 | /* conversion functions */ |
| 148 | |
| 149 | #define s3c24xx_dev_to_port(__dev) dev_get_drvdata(__dev) |
| 150 | |
| 151 | /* register access controls */ |
| 152 | |
| 153 | #define portaddr(port, reg) ((port)->membase + (reg)) |
| 154 | #define portaddrl(port, reg) \ |
| 155 | ((unsigned long *)(unsigned long)((port)->membase + (reg))) |
| 156 | |
Hyunki Koo | 57253cc | 2020-05-06 17:02:40 +0900 | [diff] [blame] | 157 | static u32 rd_reg(struct uart_port *port, u32 reg) |
| 158 | { |
| 159 | switch (port->iotype) { |
| 160 | case UPIO_MEM: |
| 161 | return readb_relaxed(portaddr(port, reg)); |
| 162 | case UPIO_MEM32: |
| 163 | return readl_relaxed(portaddr(port, reg)); |
| 164 | default: |
| 165 | return 0; |
| 166 | } |
| 167 | return 0; |
| 168 | } |
| 169 | |
Greg Kroah-Hartman | 43df170 | 2019-12-10 15:37:01 +0100 | [diff] [blame] | 170 | #define rd_regl(port, reg) (readl_relaxed(portaddr(port, reg))) |
| 171 | |
Hyunki Koo | 57253cc | 2020-05-06 17:02:40 +0900 | [diff] [blame] | 172 | static void wr_reg(struct uart_port *port, u32 reg, u32 val) |
| 173 | { |
| 174 | switch (port->iotype) { |
| 175 | case UPIO_MEM: |
| 176 | writeb_relaxed(val, portaddr(port, reg)); |
| 177 | break; |
| 178 | case UPIO_MEM32: |
| 179 | writel_relaxed(val, portaddr(port, reg)); |
| 180 | break; |
| 181 | } |
| 182 | } |
| 183 | |
Greg Kroah-Hartman | 43df170 | 2019-12-10 15:37:01 +0100 | [diff] [blame] | 184 | #define wr_regl(port, reg, val) writel_relaxed(val, portaddr(port, reg)) |
| 185 | |
| 186 | /* Byte-order aware bit setting/clearing functions. */ |
| 187 | |
| 188 | static inline void s3c24xx_set_bit(struct uart_port *port, int idx, |
| 189 | unsigned int reg) |
| 190 | { |
| 191 | unsigned long flags; |
| 192 | u32 val; |
| 193 | |
| 194 | local_irq_save(flags); |
| 195 | val = rd_regl(port, reg); |
| 196 | val |= (1 << idx); |
| 197 | wr_regl(port, reg, val); |
| 198 | local_irq_restore(flags); |
| 199 | } |
| 200 | |
| 201 | static inline void s3c24xx_clear_bit(struct uart_port *port, int idx, |
| 202 | unsigned int reg) |
| 203 | { |
| 204 | unsigned long flags; |
| 205 | u32 val; |
| 206 | |
| 207 | local_irq_save(flags); |
| 208 | val = rd_regl(port, reg); |
| 209 | val &= ~(1 << idx); |
| 210 | wr_regl(port, reg, val); |
| 211 | local_irq_restore(flags); |
| 212 | } |
| 213 | |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 214 | static inline struct s3c24xx_uart_port *to_ourport(struct uart_port *port) |
| 215 | { |
| 216 | return container_of(port, struct s3c24xx_uart_port, port); |
| 217 | } |
| 218 | |
| 219 | /* translate a port to the device name */ |
| 220 | |
| 221 | static inline const char *s3c24xx_serial_portname(struct uart_port *port) |
| 222 | { |
| 223 | return to_platform_device(port->dev)->name; |
| 224 | } |
| 225 | |
| 226 | static int s3c24xx_serial_txempty_nofifo(struct uart_port *port) |
| 227 | { |
Sachin Kamat | 9303ac1 | 2012-09-05 10:30:11 +0530 | [diff] [blame] | 228 | return rd_regl(port, S3C2410_UTRSTAT) & S3C2410_UTRSTAT_TXE; |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 229 | } |
| 230 | |
Thomas Abraham | 88bb4ea | 2011-08-10 15:51:19 +0530 | [diff] [blame] | 231 | /* |
| 232 | * s3c64xx and later SoC's include the interrupt mask and status registers in |
| 233 | * the controller itself, unlike the s3c24xx SoC's which have these registers |
| 234 | * in the interrupt controller. Check if the port type is s3c64xx or higher. |
| 235 | */ |
| 236 | static int s3c24xx_serial_has_interrupt_mask(struct uart_port *port) |
| 237 | { |
| 238 | return to_ourport(port)->info->type == PORT_S3C6400; |
| 239 | } |
| 240 | |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 241 | static void s3c24xx_serial_rx_enable(struct uart_port *port) |
| 242 | { |
Greg Kroah-Hartman | 8336240 | 2019-12-17 15:02:32 +0100 | [diff] [blame] | 243 | struct s3c24xx_uart_port *ourport = to_ourport(port); |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 244 | unsigned long flags; |
| 245 | unsigned int ucon, ufcon; |
| 246 | int count = 10000; |
| 247 | |
| 248 | spin_lock_irqsave(&port->lock, flags); |
| 249 | |
| 250 | while (--count && !s3c24xx_serial_txempty_nofifo(port)) |
| 251 | udelay(100); |
| 252 | |
| 253 | ufcon = rd_regl(port, S3C2410_UFCON); |
| 254 | ufcon |= S3C2410_UFCON_RESETRX; |
| 255 | wr_regl(port, S3C2410_UFCON, ufcon); |
| 256 | |
| 257 | ucon = rd_regl(port, S3C2410_UCON); |
| 258 | ucon |= S3C2410_UCON_RXIRQMODE; |
| 259 | wr_regl(port, S3C2410_UCON, ucon); |
| 260 | |
Greg Kroah-Hartman | 8336240 | 2019-12-17 15:02:32 +0100 | [diff] [blame] | 261 | ourport->rx_enabled = 1; |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 262 | spin_unlock_irqrestore(&port->lock, flags); |
| 263 | } |
| 264 | |
| 265 | static void s3c24xx_serial_rx_disable(struct uart_port *port) |
| 266 | { |
Greg Kroah-Hartman | 8336240 | 2019-12-17 15:02:32 +0100 | [diff] [blame] | 267 | struct s3c24xx_uart_port *ourport = to_ourport(port); |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 268 | unsigned long flags; |
| 269 | unsigned int ucon; |
| 270 | |
| 271 | spin_lock_irqsave(&port->lock, flags); |
| 272 | |
| 273 | ucon = rd_regl(port, S3C2410_UCON); |
| 274 | ucon &= ~S3C2410_UCON_RXIRQMODE; |
| 275 | wr_regl(port, S3C2410_UCON, ucon); |
| 276 | |
Greg Kroah-Hartman | 8336240 | 2019-12-17 15:02:32 +0100 | [diff] [blame] | 277 | ourport->rx_enabled = 0; |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 278 | spin_unlock_irqrestore(&port->lock, flags); |
| 279 | } |
| 280 | |
| 281 | static void s3c24xx_serial_stop_tx(struct uart_port *port) |
| 282 | { |
Ben Dooks | b73c289c | 2008-10-21 14:07:04 +0100 | [diff] [blame] | 283 | struct s3c24xx_uart_port *ourport = to_ourport(port); |
Robert Baldyga | 29bef79 | 2014-12-10 12:49:26 +0100 | [diff] [blame] | 284 | struct s3c24xx_uart_dma *dma = ourport->dma; |
| 285 | struct circ_buf *xmit = &port->state->xmit; |
| 286 | struct dma_tx_state state; |
| 287 | int count; |
Ben Dooks | b73c289c | 2008-10-21 14:07:04 +0100 | [diff] [blame] | 288 | |
Greg Kroah-Hartman | 8336240 | 2019-12-17 15:02:32 +0100 | [diff] [blame] | 289 | if (!ourport->tx_enabled) |
Robert Baldyga | 29bef79 | 2014-12-10 12:49:26 +0100 | [diff] [blame] | 290 | return; |
| 291 | |
| 292 | if (s3c24xx_serial_has_interrupt_mask(port)) |
Matthew Leach | bbb5ff9 | 2016-06-22 17:57:03 +0100 | [diff] [blame] | 293 | s3c24xx_set_bit(port, S3C64XX_UINTM_TXD, S3C64XX_UINTM); |
Robert Baldyga | 29bef79 | 2014-12-10 12:49:26 +0100 | [diff] [blame] | 294 | else |
| 295 | disable_irq_nosync(ourport->tx_irq); |
| 296 | |
| 297 | if (dma && dma->tx_chan && ourport->tx_in_progress == S3C24XX_TX_DMA) { |
| 298 | dmaengine_pause(dma->tx_chan); |
| 299 | dmaengine_tx_status(dma->tx_chan, dma->tx_cookie, &state); |
| 300 | dmaengine_terminate_all(dma->tx_chan); |
| 301 | dma_sync_single_for_cpu(ourport->port.dev, |
| 302 | dma->tx_transfer_addr, dma->tx_size, DMA_TO_DEVICE); |
| 303 | async_tx_ack(dma->tx_desc); |
| 304 | count = dma->tx_bytes_requested - state.residue; |
| 305 | xmit->tail = (xmit->tail + count) & (UART_XMIT_SIZE - 1); |
| 306 | port->icount.tx += count; |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 307 | } |
Robert Baldyga | 29bef79 | 2014-12-10 12:49:26 +0100 | [diff] [blame] | 308 | |
Greg Kroah-Hartman | 8336240 | 2019-12-17 15:02:32 +0100 | [diff] [blame] | 309 | ourport->tx_enabled = 0; |
Robert Baldyga | 29bef79 | 2014-12-10 12:49:26 +0100 | [diff] [blame] | 310 | ourport->tx_in_progress = 0; |
| 311 | |
| 312 | if (port->flags & UPF_CONS_FLOW) |
| 313 | s3c24xx_serial_rx_enable(port); |
| 314 | |
| 315 | ourport->tx_mode = 0; |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 316 | } |
| 317 | |
Robert Baldyga | 29bef79 | 2014-12-10 12:49:26 +0100 | [diff] [blame] | 318 | static void s3c24xx_serial_start_next_tx(struct s3c24xx_uart_port *ourport); |
| 319 | |
| 320 | static void s3c24xx_serial_tx_dma_complete(void *args) |
| 321 | { |
| 322 | struct s3c24xx_uart_port *ourport = args; |
| 323 | struct uart_port *port = &ourport->port; |
| 324 | struct circ_buf *xmit = &port->state->xmit; |
| 325 | struct s3c24xx_uart_dma *dma = ourport->dma; |
| 326 | struct dma_tx_state state; |
| 327 | unsigned long flags; |
| 328 | int count; |
| 329 | |
Robert Baldyga | 29bef79 | 2014-12-10 12:49:26 +0100 | [diff] [blame] | 330 | dmaengine_tx_status(dma->tx_chan, dma->tx_cookie, &state); |
| 331 | count = dma->tx_bytes_requested - state.residue; |
| 332 | async_tx_ack(dma->tx_desc); |
| 333 | |
| 334 | dma_sync_single_for_cpu(ourport->port.dev, dma->tx_transfer_addr, |
| 335 | dma->tx_size, DMA_TO_DEVICE); |
| 336 | |
| 337 | spin_lock_irqsave(&port->lock, flags); |
| 338 | |
| 339 | xmit->tail = (xmit->tail + count) & (UART_XMIT_SIZE - 1); |
| 340 | port->icount.tx += count; |
| 341 | ourport->tx_in_progress = 0; |
| 342 | |
| 343 | if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS) |
| 344 | uart_write_wakeup(port); |
| 345 | |
| 346 | s3c24xx_serial_start_next_tx(ourport); |
| 347 | spin_unlock_irqrestore(&port->lock, flags); |
| 348 | } |
| 349 | |
| 350 | static void enable_tx_dma(struct s3c24xx_uart_port *ourport) |
| 351 | { |
| 352 | struct uart_port *port = &ourport->port; |
| 353 | u32 ucon; |
| 354 | |
| 355 | /* Mask Tx interrupt */ |
| 356 | if (s3c24xx_serial_has_interrupt_mask(port)) |
Matthew Leach | bbb5ff9 | 2016-06-22 17:57:03 +0100 | [diff] [blame] | 357 | s3c24xx_set_bit(port, S3C64XX_UINTM_TXD, S3C64XX_UINTM); |
Robert Baldyga | 29bef79 | 2014-12-10 12:49:26 +0100 | [diff] [blame] | 358 | else |
| 359 | disable_irq_nosync(ourport->tx_irq); |
| 360 | |
| 361 | /* Enable tx dma mode */ |
| 362 | ucon = rd_regl(port, S3C2410_UCON); |
| 363 | ucon &= ~(S3C64XX_UCON_TXBURST_MASK | S3C64XX_UCON_TXMODE_MASK); |
| 364 | ucon |= (dma_get_cache_alignment() >= 16) ? |
| 365 | S3C64XX_UCON_TXBURST_16 : S3C64XX_UCON_TXBURST_1; |
| 366 | ucon |= S3C64XX_UCON_TXMODE_DMA; |
| 367 | wr_regl(port, S3C2410_UCON, ucon); |
| 368 | |
| 369 | ourport->tx_mode = S3C24XX_TX_DMA; |
| 370 | } |
| 371 | |
| 372 | static void enable_tx_pio(struct s3c24xx_uart_port *ourport) |
| 373 | { |
| 374 | struct uart_port *port = &ourport->port; |
| 375 | u32 ucon, ufcon; |
| 376 | |
| 377 | /* Set ufcon txtrig */ |
| 378 | ourport->tx_in_progress = S3C24XX_TX_PIO; |
| 379 | ufcon = rd_regl(port, S3C2410_UFCON); |
| 380 | wr_regl(port, S3C2410_UFCON, ufcon); |
| 381 | |
| 382 | /* Enable tx pio mode */ |
| 383 | ucon = rd_regl(port, S3C2410_UCON); |
| 384 | ucon &= ~(S3C64XX_UCON_TXMODE_MASK); |
| 385 | ucon |= S3C64XX_UCON_TXMODE_CPU; |
| 386 | wr_regl(port, S3C2410_UCON, ucon); |
| 387 | |
| 388 | /* Unmask Tx interrupt */ |
| 389 | if (s3c24xx_serial_has_interrupt_mask(port)) |
Matthew Leach | bbb5ff9 | 2016-06-22 17:57:03 +0100 | [diff] [blame] | 390 | s3c24xx_clear_bit(port, S3C64XX_UINTM_TXD, |
| 391 | S3C64XX_UINTM); |
Robert Baldyga | 29bef79 | 2014-12-10 12:49:26 +0100 | [diff] [blame] | 392 | else |
| 393 | enable_irq(ourport->tx_irq); |
| 394 | |
| 395 | ourport->tx_mode = S3C24XX_TX_PIO; |
| 396 | } |
| 397 | |
| 398 | static void s3c24xx_serial_start_tx_pio(struct s3c24xx_uart_port *ourport) |
| 399 | { |
| 400 | if (ourport->tx_mode != S3C24XX_TX_PIO) |
| 401 | enable_tx_pio(ourport); |
| 402 | } |
| 403 | |
| 404 | static int s3c24xx_serial_start_tx_dma(struct s3c24xx_uart_port *ourport, |
| 405 | unsigned int count) |
| 406 | { |
| 407 | struct uart_port *port = &ourport->port; |
| 408 | struct circ_buf *xmit = &port->state->xmit; |
| 409 | struct s3c24xx_uart_dma *dma = ourport->dma; |
| 410 | |
Robert Baldyga | 29bef79 | 2014-12-10 12:49:26 +0100 | [diff] [blame] | 411 | if (ourport->tx_mode != S3C24XX_TX_DMA) |
| 412 | enable_tx_dma(ourport); |
| 413 | |
Robert Baldyga | 29bef79 | 2014-12-10 12:49:26 +0100 | [diff] [blame] | 414 | dma->tx_size = count & ~(dma_get_cache_alignment() - 1); |
| 415 | dma->tx_transfer_addr = dma->tx_addr + xmit->tail; |
| 416 | |
| 417 | dma_sync_single_for_device(ourport->port.dev, dma->tx_transfer_addr, |
| 418 | dma->tx_size, DMA_TO_DEVICE); |
| 419 | |
| 420 | dma->tx_desc = dmaengine_prep_slave_single(dma->tx_chan, |
| 421 | dma->tx_transfer_addr, dma->tx_size, |
| 422 | DMA_MEM_TO_DEV, DMA_PREP_INTERRUPT); |
| 423 | if (!dma->tx_desc) { |
| 424 | dev_err(ourport->port.dev, "Unable to get desc for Tx\n"); |
| 425 | return -EIO; |
| 426 | } |
| 427 | |
| 428 | dma->tx_desc->callback = s3c24xx_serial_tx_dma_complete; |
| 429 | dma->tx_desc->callback_param = ourport; |
| 430 | dma->tx_bytes_requested = dma->tx_size; |
| 431 | |
| 432 | ourport->tx_in_progress = S3C24XX_TX_DMA; |
| 433 | dma->tx_cookie = dmaengine_submit(dma->tx_desc); |
| 434 | dma_async_issue_pending(dma->tx_chan); |
| 435 | return 0; |
| 436 | } |
| 437 | |
| 438 | static void s3c24xx_serial_start_next_tx(struct s3c24xx_uart_port *ourport) |
| 439 | { |
| 440 | struct uart_port *port = &ourport->port; |
| 441 | struct circ_buf *xmit = &port->state->xmit; |
| 442 | unsigned long count; |
| 443 | |
| 444 | /* Get data size up to the end of buffer */ |
| 445 | count = CIRC_CNT_TO_END(xmit->head, xmit->tail, UART_XMIT_SIZE); |
| 446 | |
| 447 | if (!count) { |
| 448 | s3c24xx_serial_stop_tx(port); |
| 449 | return; |
| 450 | } |
| 451 | |
Marek Szyprowski | 81ccb2a | 2015-07-31 10:58:27 +0200 | [diff] [blame] | 452 | if (!ourport->dma || !ourport->dma->tx_chan || |
Robert Baldyga | 736cd79 | 2015-07-31 10:58:28 +0200 | [diff] [blame] | 453 | count < ourport->min_dma_size || |
| 454 | xmit->tail & (dma_get_cache_alignment() - 1)) |
Robert Baldyga | 29bef79 | 2014-12-10 12:49:26 +0100 | [diff] [blame] | 455 | s3c24xx_serial_start_tx_pio(ourport); |
| 456 | else |
| 457 | s3c24xx_serial_start_tx_dma(ourport, count); |
| 458 | } |
| 459 | |
Krzysztof Kozlowski | 7578197 | 2015-05-02 00:40:04 +0900 | [diff] [blame] | 460 | static void s3c24xx_serial_start_tx(struct uart_port *port) |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 461 | { |
Ben Dooks | b73c289c | 2008-10-21 14:07:04 +0100 | [diff] [blame] | 462 | struct s3c24xx_uart_port *ourport = to_ourport(port); |
Robert Baldyga | 29bef79 | 2014-12-10 12:49:26 +0100 | [diff] [blame] | 463 | struct circ_buf *xmit = &port->state->xmit; |
Ben Dooks | b73c289c | 2008-10-21 14:07:04 +0100 | [diff] [blame] | 464 | |
Greg Kroah-Hartman | 8336240 | 2019-12-17 15:02:32 +0100 | [diff] [blame] | 465 | if (!ourport->tx_enabled) { |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 466 | if (port->flags & UPF_CONS_FLOW) |
| 467 | s3c24xx_serial_rx_disable(port); |
| 468 | |
Greg Kroah-Hartman | 8336240 | 2019-12-17 15:02:32 +0100 | [diff] [blame] | 469 | ourport->tx_enabled = 1; |
Robert Baldyga | ba019a3 | 2015-01-28 14:44:23 +0100 | [diff] [blame] | 470 | if (!ourport->dma || !ourport->dma->tx_chan) |
Robert Baldyga | 29bef79 | 2014-12-10 12:49:26 +0100 | [diff] [blame] | 471 | s3c24xx_serial_start_tx_pio(ourport); |
Robert Baldyga | 29bef79 | 2014-12-10 12:49:26 +0100 | [diff] [blame] | 472 | } |
| 473 | |
| 474 | if (ourport->dma && ourport->dma->tx_chan) { |
| 475 | if (!uart_circ_empty(xmit) && !ourport->tx_in_progress) |
| 476 | s3c24xx_serial_start_next_tx(ourport); |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 477 | } |
| 478 | } |
| 479 | |
Robert Baldyga | b543c30 | 2014-12-10 12:49:27 +0100 | [diff] [blame] | 480 | static void s3c24xx_uart_copy_rx_to_tty(struct s3c24xx_uart_port *ourport, |
| 481 | struct tty_port *tty, int count) |
| 482 | { |
| 483 | struct s3c24xx_uart_dma *dma = ourport->dma; |
| 484 | int copied; |
| 485 | |
| 486 | if (!count) |
| 487 | return; |
| 488 | |
| 489 | dma_sync_single_for_cpu(ourport->port.dev, dma->rx_addr, |
| 490 | dma->rx_size, DMA_FROM_DEVICE); |
| 491 | |
| 492 | ourport->port.icount.rx += count; |
| 493 | if (!tty) { |
| 494 | dev_err(ourport->port.dev, "No tty port\n"); |
| 495 | return; |
| 496 | } |
| 497 | copied = tty_insert_flip_string(tty, |
| 498 | ((unsigned char *)(ourport->dma->rx_buf)), count); |
| 499 | if (copied != count) { |
| 500 | WARN_ON(1); |
| 501 | dev_err(ourport->port.dev, "RxData copy to tty layer failed\n"); |
| 502 | } |
| 503 | } |
| 504 | |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 505 | static void s3c24xx_serial_stop_rx(struct uart_port *port) |
| 506 | { |
Ben Dooks | b73c289c | 2008-10-21 14:07:04 +0100 | [diff] [blame] | 507 | struct s3c24xx_uart_port *ourport = to_ourport(port); |
Robert Baldyga | b543c30 | 2014-12-10 12:49:27 +0100 | [diff] [blame] | 508 | struct s3c24xx_uart_dma *dma = ourport->dma; |
| 509 | struct tty_port *t = &port->state->port; |
| 510 | struct dma_tx_state state; |
| 511 | enum dma_status dma_status; |
| 512 | unsigned int received; |
Ben Dooks | b73c289c | 2008-10-21 14:07:04 +0100 | [diff] [blame] | 513 | |
Greg Kroah-Hartman | 8336240 | 2019-12-17 15:02:32 +0100 | [diff] [blame] | 514 | if (ourport->rx_enabled) { |
Greg Kroah-Hartman | a05025d | 2019-12-10 15:37:03 +0100 | [diff] [blame] | 515 | dev_dbg(port->dev, "stopping rx\n"); |
Thomas Abraham | 88bb4ea | 2011-08-10 15:51:19 +0530 | [diff] [blame] | 516 | if (s3c24xx_serial_has_interrupt_mask(port)) |
Matthew Leach | bbb5ff9 | 2016-06-22 17:57:03 +0100 | [diff] [blame] | 517 | s3c24xx_set_bit(port, S3C64XX_UINTM_RXD, |
| 518 | S3C64XX_UINTM); |
Thomas Abraham | 88bb4ea | 2011-08-10 15:51:19 +0530 | [diff] [blame] | 519 | else |
| 520 | disable_irq_nosync(ourport->rx_irq); |
Greg Kroah-Hartman | 8336240 | 2019-12-17 15:02:32 +0100 | [diff] [blame] | 521 | ourport->rx_enabled = 0; |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 522 | } |
Robert Baldyga | b543c30 | 2014-12-10 12:49:27 +0100 | [diff] [blame] | 523 | if (dma && dma->rx_chan) { |
| 524 | dmaengine_pause(dma->tx_chan); |
| 525 | dma_status = dmaengine_tx_status(dma->rx_chan, |
| 526 | dma->rx_cookie, &state); |
| 527 | if (dma_status == DMA_IN_PROGRESS || |
| 528 | dma_status == DMA_PAUSED) { |
| 529 | received = dma->rx_bytes_requested - state.residue; |
| 530 | dmaengine_terminate_all(dma->rx_chan); |
| 531 | s3c24xx_uart_copy_rx_to_tty(ourport, t, received); |
| 532 | } |
| 533 | } |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 534 | } |
| 535 | |
Robert Baldyga | ef4aca7 | 2014-11-24 07:56:22 +0100 | [diff] [blame] | 536 | static inline struct s3c24xx_uart_info |
| 537 | *s3c24xx_port_to_info(struct uart_port *port) |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 538 | { |
| 539 | return to_ourport(port)->info; |
| 540 | } |
| 541 | |
Robert Baldyga | ef4aca7 | 2014-11-24 07:56:22 +0100 | [diff] [blame] | 542 | static inline struct s3c2410_uartcfg |
| 543 | *s3c24xx_port_to_cfg(struct uart_port *port) |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 544 | { |
Thomas Abraham | 4d84e97 | 2011-10-24 11:47:25 +0200 | [diff] [blame] | 545 | struct s3c24xx_uart_port *ourport; |
| 546 | |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 547 | if (port->dev == NULL) |
| 548 | return NULL; |
| 549 | |
Thomas Abraham | 4d84e97 | 2011-10-24 11:47:25 +0200 | [diff] [blame] | 550 | ourport = container_of(port, struct s3c24xx_uart_port, port); |
| 551 | return ourport->cfg; |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 552 | } |
| 553 | |
| 554 | static int s3c24xx_serial_rx_fifocnt(struct s3c24xx_uart_port *ourport, |
| 555 | unsigned long ufstat) |
| 556 | { |
| 557 | struct s3c24xx_uart_info *info = ourport->info; |
| 558 | |
| 559 | if (ufstat & info->rx_fifofull) |
Thomas Abraham | da12150 | 2011-11-02 19:23:25 +0900 | [diff] [blame] | 560 | return ourport->port.fifosize; |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 561 | |
| 562 | return (ufstat & info->rx_fifomask) >> info->rx_fifoshift; |
| 563 | } |
| 564 | |
Robert Baldyga | b543c30 | 2014-12-10 12:49:27 +0100 | [diff] [blame] | 565 | static void s3c64xx_start_rx_dma(struct s3c24xx_uart_port *ourport); |
| 566 | static void s3c24xx_serial_rx_dma_complete(void *args) |
| 567 | { |
| 568 | struct s3c24xx_uart_port *ourport = args; |
| 569 | struct uart_port *port = &ourport->port; |
| 570 | |
| 571 | struct s3c24xx_uart_dma *dma = ourport->dma; |
| 572 | struct tty_port *t = &port->state->port; |
| 573 | struct tty_struct *tty = tty_port_tty_get(&ourport->port.state->port); |
| 574 | |
| 575 | struct dma_tx_state state; |
| 576 | unsigned long flags; |
| 577 | int received; |
| 578 | |
| 579 | dmaengine_tx_status(dma->rx_chan, dma->rx_cookie, &state); |
| 580 | received = dma->rx_bytes_requested - state.residue; |
| 581 | async_tx_ack(dma->rx_desc); |
| 582 | |
| 583 | spin_lock_irqsave(&port->lock, flags); |
| 584 | |
| 585 | if (received) |
| 586 | s3c24xx_uart_copy_rx_to_tty(ourport, t, received); |
| 587 | |
| 588 | if (tty) { |
| 589 | tty_flip_buffer_push(t); |
| 590 | tty_kref_put(tty); |
| 591 | } |
| 592 | |
| 593 | s3c64xx_start_rx_dma(ourport); |
| 594 | |
| 595 | spin_unlock_irqrestore(&port->lock, flags); |
| 596 | } |
| 597 | |
| 598 | static void s3c64xx_start_rx_dma(struct s3c24xx_uart_port *ourport) |
| 599 | { |
| 600 | struct s3c24xx_uart_dma *dma = ourport->dma; |
| 601 | |
| 602 | dma_sync_single_for_device(ourport->port.dev, dma->rx_addr, |
| 603 | dma->rx_size, DMA_FROM_DEVICE); |
| 604 | |
| 605 | dma->rx_desc = dmaengine_prep_slave_single(dma->rx_chan, |
| 606 | dma->rx_addr, dma->rx_size, DMA_DEV_TO_MEM, |
| 607 | DMA_PREP_INTERRUPT); |
| 608 | if (!dma->rx_desc) { |
| 609 | dev_err(ourport->port.dev, "Unable to get desc for Rx\n"); |
| 610 | return; |
| 611 | } |
| 612 | |
| 613 | dma->rx_desc->callback = s3c24xx_serial_rx_dma_complete; |
| 614 | dma->rx_desc->callback_param = ourport; |
| 615 | dma->rx_bytes_requested = dma->rx_size; |
| 616 | |
| 617 | dma->rx_cookie = dmaengine_submit(dma->rx_desc); |
| 618 | dma_async_issue_pending(dma->rx_chan); |
| 619 | } |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 620 | |
| 621 | /* ? - where has parity gone?? */ |
| 622 | #define S3C2410_UERSTAT_PARITY (0x1000) |
| 623 | |
Robert Baldyga | b543c30 | 2014-12-10 12:49:27 +0100 | [diff] [blame] | 624 | static void enable_rx_dma(struct s3c24xx_uart_port *ourport) |
| 625 | { |
| 626 | struct uart_port *port = &ourport->port; |
| 627 | unsigned int ucon; |
| 628 | |
| 629 | /* set Rx mode to DMA mode */ |
| 630 | ucon = rd_regl(port, S3C2410_UCON); |
| 631 | ucon &= ~(S3C64XX_UCON_RXBURST_MASK | |
| 632 | S3C64XX_UCON_TIMEOUT_MASK | |
| 633 | S3C64XX_UCON_EMPTYINT_EN | |
| 634 | S3C64XX_UCON_DMASUS_EN | |
| 635 | S3C64XX_UCON_TIMEOUT_EN | |
| 636 | S3C64XX_UCON_RXMODE_MASK); |
| 637 | ucon |= S3C64XX_UCON_RXBURST_16 | |
| 638 | 0xf << S3C64XX_UCON_TIMEOUT_SHIFT | |
| 639 | S3C64XX_UCON_EMPTYINT_EN | |
| 640 | S3C64XX_UCON_TIMEOUT_EN | |
| 641 | S3C64XX_UCON_RXMODE_DMA; |
| 642 | wr_regl(port, S3C2410_UCON, ucon); |
| 643 | |
| 644 | ourport->rx_mode = S3C24XX_RX_DMA; |
| 645 | } |
| 646 | |
| 647 | static void enable_rx_pio(struct s3c24xx_uart_port *ourport) |
| 648 | { |
| 649 | struct uart_port *port = &ourport->port; |
| 650 | unsigned int ucon; |
| 651 | |
| 652 | /* set Rx mode to DMA mode */ |
| 653 | ucon = rd_regl(port, S3C2410_UCON); |
| 654 | ucon &= ~(S3C64XX_UCON_TIMEOUT_MASK | |
| 655 | S3C64XX_UCON_EMPTYINT_EN | |
| 656 | S3C64XX_UCON_DMASUS_EN | |
| 657 | S3C64XX_UCON_TIMEOUT_EN | |
| 658 | S3C64XX_UCON_RXMODE_MASK); |
| 659 | ucon |= 0xf << S3C64XX_UCON_TIMEOUT_SHIFT | |
| 660 | S3C64XX_UCON_TIMEOUT_EN | |
| 661 | S3C64XX_UCON_RXMODE_CPU; |
| 662 | wr_regl(port, S3C2410_UCON, ucon); |
| 663 | |
| 664 | ourport->rx_mode = S3C24XX_RX_PIO; |
| 665 | } |
| 666 | |
Robert Baldyga | 09557c0 | 2015-09-15 14:49:00 +0200 | [diff] [blame] | 667 | static void s3c24xx_serial_rx_drain_fifo(struct s3c24xx_uart_port *ourport); |
| 668 | |
Robert Baldyga | e4678af | 2015-09-15 14:48:57 +0200 | [diff] [blame] | 669 | static irqreturn_t s3c24xx_serial_rx_chars_dma(void *dev_id) |
Robert Baldyga | b543c30 | 2014-12-10 12:49:27 +0100 | [diff] [blame] | 670 | { |
Chen Wandun | 98aee0c | 2019-11-22 20:04:18 +0800 | [diff] [blame] | 671 | unsigned int utrstat, received; |
Robert Baldyga | b543c30 | 2014-12-10 12:49:27 +0100 | [diff] [blame] | 672 | struct s3c24xx_uart_port *ourport = dev_id; |
| 673 | struct uart_port *port = &ourport->port; |
| 674 | struct s3c24xx_uart_dma *dma = ourport->dma; |
| 675 | struct tty_struct *tty = tty_port_tty_get(&ourport->port.state->port); |
| 676 | struct tty_port *t = &port->state->port; |
| 677 | unsigned long flags; |
| 678 | struct dma_tx_state state; |
| 679 | |
| 680 | utrstat = rd_regl(port, S3C2410_UTRSTAT); |
Chen Wandun | 98aee0c | 2019-11-22 20:04:18 +0800 | [diff] [blame] | 681 | rd_regl(port, S3C2410_UFSTAT); |
Robert Baldyga | b543c30 | 2014-12-10 12:49:27 +0100 | [diff] [blame] | 682 | |
| 683 | spin_lock_irqsave(&port->lock, flags); |
| 684 | |
| 685 | if (!(utrstat & S3C2410_UTRSTAT_TIMEOUT)) { |
| 686 | s3c64xx_start_rx_dma(ourport); |
| 687 | if (ourport->rx_mode == S3C24XX_RX_PIO) |
| 688 | enable_rx_dma(ourport); |
| 689 | goto finish; |
| 690 | } |
| 691 | |
| 692 | if (ourport->rx_mode == S3C24XX_RX_DMA) { |
| 693 | dmaengine_pause(dma->rx_chan); |
| 694 | dmaengine_tx_status(dma->rx_chan, dma->rx_cookie, &state); |
| 695 | dmaengine_terminate_all(dma->rx_chan); |
| 696 | received = dma->rx_bytes_requested - state.residue; |
| 697 | s3c24xx_uart_copy_rx_to_tty(ourport, t, received); |
| 698 | |
| 699 | enable_rx_pio(ourport); |
| 700 | } |
| 701 | |
Robert Baldyga | 09557c0 | 2015-09-15 14:49:00 +0200 | [diff] [blame] | 702 | s3c24xx_serial_rx_drain_fifo(ourport); |
Robert Baldyga | b543c30 | 2014-12-10 12:49:27 +0100 | [diff] [blame] | 703 | |
| 704 | if (tty) { |
| 705 | tty_flip_buffer_push(t); |
| 706 | tty_kref_put(tty); |
| 707 | } |
| 708 | |
| 709 | wr_regl(port, S3C2410_UTRSTAT, S3C2410_UTRSTAT_TIMEOUT); |
| 710 | |
| 711 | finish: |
| 712 | spin_unlock_irqrestore(&port->lock, flags); |
| 713 | |
| 714 | return IRQ_HANDLED; |
| 715 | } |
| 716 | |
Robert Baldyga | 01732dd | 2015-09-15 14:48:59 +0200 | [diff] [blame] | 717 | static void s3c24xx_serial_rx_drain_fifo(struct s3c24xx_uart_port *ourport) |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 718 | { |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 719 | struct uart_port *port = &ourport->port; |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 720 | unsigned int ufcon, ch, flag, ufstat, uerstat; |
Youngmin Nam | aba06e9 | 2016-03-05 19:36:32 +0900 | [diff] [blame] | 721 | unsigned int fifocnt = 0; |
Robert Baldyga | 57850a5 | 2014-11-24 07:56:24 +0100 | [diff] [blame] | 722 | int max_count = port->fifosize; |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 723 | |
| 724 | while (max_count-- > 0) { |
Youngmin Nam | aba06e9 | 2016-03-05 19:36:32 +0900 | [diff] [blame] | 725 | /* |
| 726 | * Receive all characters known to be in FIFO |
| 727 | * before reading FIFO level again |
| 728 | */ |
| 729 | if (fifocnt == 0) { |
| 730 | ufstat = rd_regl(port, S3C2410_UFSTAT); |
| 731 | fifocnt = s3c24xx_serial_rx_fifocnt(ourport, ufstat); |
| 732 | if (fifocnt == 0) |
| 733 | break; |
| 734 | } |
| 735 | fifocnt--; |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 736 | |
| 737 | uerstat = rd_regl(port, S3C2410_UERSTAT); |
Hyunki Koo | 8fba6c0 | 2020-05-06 17:02:38 +0900 | [diff] [blame] | 738 | ch = rd_reg(port, S3C2410_URXH); |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 739 | |
| 740 | if (port->flags & UPF_CONS_FLOW) { |
| 741 | int txe = s3c24xx_serial_txempty_nofifo(port); |
| 742 | |
Greg Kroah-Hartman | 8336240 | 2019-12-17 15:02:32 +0100 | [diff] [blame] | 743 | if (ourport->rx_enabled) { |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 744 | if (!txe) { |
Greg Kroah-Hartman | 8336240 | 2019-12-17 15:02:32 +0100 | [diff] [blame] | 745 | ourport->rx_enabled = 0; |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 746 | continue; |
| 747 | } |
| 748 | } else { |
| 749 | if (txe) { |
Youngmin Nam | aba06e9 | 2016-03-05 19:36:32 +0900 | [diff] [blame] | 750 | ufcon = rd_regl(port, S3C2410_UFCON); |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 751 | ufcon |= S3C2410_UFCON_RESETRX; |
| 752 | wr_regl(port, S3C2410_UFCON, ufcon); |
Greg Kroah-Hartman | 8336240 | 2019-12-17 15:02:32 +0100 | [diff] [blame] | 753 | ourport->rx_enabled = 1; |
Robert Baldyga | 01732dd | 2015-09-15 14:48:59 +0200 | [diff] [blame] | 754 | return; |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 755 | } |
| 756 | continue; |
| 757 | } |
| 758 | } |
| 759 | |
| 760 | /* insert the character into the buffer */ |
| 761 | |
| 762 | flag = TTY_NORMAL; |
| 763 | port->icount.rx++; |
| 764 | |
| 765 | if (unlikely(uerstat & S3C2410_UERSTAT_ANY)) { |
Greg Kroah-Hartman | a05025d | 2019-12-10 15:37:03 +0100 | [diff] [blame] | 766 | dev_dbg(port->dev, |
| 767 | "rxerr: port ch=0x%02x, rxs=0x%08x\n", |
| 768 | ch, uerstat); |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 769 | |
| 770 | /* check for break */ |
| 771 | if (uerstat & S3C2410_UERSTAT_BREAK) { |
Greg Kroah-Hartman | a05025d | 2019-12-10 15:37:03 +0100 | [diff] [blame] | 772 | dev_dbg(port->dev, "break!\n"); |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 773 | port->icount.brk++; |
| 774 | if (uart_handle_break(port)) |
Robert Baldyga | 620bb21 | 2015-09-15 14:48:58 +0200 | [diff] [blame] | 775 | continue; /* Ignore character */ |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 776 | } |
| 777 | |
| 778 | if (uerstat & S3C2410_UERSTAT_FRAME) |
| 779 | port->icount.frame++; |
| 780 | if (uerstat & S3C2410_UERSTAT_OVERRUN) |
| 781 | port->icount.overrun++; |
| 782 | |
| 783 | uerstat &= port->read_status_mask; |
| 784 | |
| 785 | if (uerstat & S3C2410_UERSTAT_BREAK) |
| 786 | flag = TTY_BREAK; |
| 787 | else if (uerstat & S3C2410_UERSTAT_PARITY) |
| 788 | flag = TTY_PARITY; |
| 789 | else if (uerstat & (S3C2410_UERSTAT_FRAME | |
| 790 | S3C2410_UERSTAT_OVERRUN)) |
| 791 | flag = TTY_FRAME; |
| 792 | } |
| 793 | |
| 794 | if (uart_handle_sysrq_char(port, ch)) |
Robert Baldyga | 620bb21 | 2015-09-15 14:48:58 +0200 | [diff] [blame] | 795 | continue; /* Ignore character */ |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 796 | |
| 797 | uart_insert_char(port, uerstat, S3C2410_UERSTAT_OVERRUN, |
| 798 | ch, flag); |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 799 | } |
Viresh Kumar | f5693ea | 2013-08-19 20:14:26 +0530 | [diff] [blame] | 800 | |
Jiri Slaby | 2e124b4 | 2013-01-03 15:53:06 +0100 | [diff] [blame] | 801 | tty_flip_buffer_push(&port->state->port); |
Robert Baldyga | 01732dd | 2015-09-15 14:48:59 +0200 | [diff] [blame] | 802 | } |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 803 | |
Robert Baldyga | 01732dd | 2015-09-15 14:48:59 +0200 | [diff] [blame] | 804 | static irqreturn_t s3c24xx_serial_rx_chars_pio(void *dev_id) |
| 805 | { |
| 806 | struct s3c24xx_uart_port *ourport = dev_id; |
| 807 | struct uart_port *port = &ourport->port; |
| 808 | unsigned long flags; |
| 809 | |
| 810 | spin_lock_irqsave(&port->lock, flags); |
| 811 | s3c24xx_serial_rx_drain_fifo(ourport); |
| 812 | spin_unlock_irqrestore(&port->lock, flags); |
| 813 | |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 814 | return IRQ_HANDLED; |
| 815 | } |
| 816 | |
Robert Baldyga | b543c30 | 2014-12-10 12:49:27 +0100 | [diff] [blame] | 817 | static irqreturn_t s3c24xx_serial_rx_chars(int irq, void *dev_id) |
| 818 | { |
| 819 | struct s3c24xx_uart_port *ourport = dev_id; |
| 820 | |
| 821 | if (ourport->dma && ourport->dma->rx_chan) |
Robert Baldyga | e4678af | 2015-09-15 14:48:57 +0200 | [diff] [blame] | 822 | return s3c24xx_serial_rx_chars_dma(dev_id); |
| 823 | return s3c24xx_serial_rx_chars_pio(dev_id); |
Robert Baldyga | b543c30 | 2014-12-10 12:49:27 +0100 | [diff] [blame] | 824 | } |
| 825 | |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 826 | static irqreturn_t s3c24xx_serial_tx_chars(int irq, void *id) |
| 827 | { |
| 828 | struct s3c24xx_uart_port *ourport = id; |
| 829 | struct uart_port *port = &ourport->port; |
Alan Cox | ebd2c8f | 2009-09-19 13:13:28 -0700 | [diff] [blame] | 830 | struct circ_buf *xmit = &port->state->xmit; |
Thomas Abraham | c15c374 | 2012-11-22 18:06:28 +0530 | [diff] [blame] | 831 | unsigned long flags; |
Robert Baldyga | 736cd79 | 2015-07-31 10:58:28 +0200 | [diff] [blame] | 832 | int count, dma_count = 0; |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 833 | |
Thomas Abraham | c15c374 | 2012-11-22 18:06:28 +0530 | [diff] [blame] | 834 | spin_lock_irqsave(&port->lock, flags); |
| 835 | |
Robert Baldyga | 29bef79 | 2014-12-10 12:49:26 +0100 | [diff] [blame] | 836 | count = CIRC_CNT_TO_END(xmit->head, xmit->tail, UART_XMIT_SIZE); |
| 837 | |
Marek Szyprowski | 81ccb2a | 2015-07-31 10:58:27 +0200 | [diff] [blame] | 838 | if (ourport->dma && ourport->dma->tx_chan && |
| 839 | count >= ourport->min_dma_size) { |
Robert Baldyga | 736cd79 | 2015-07-31 10:58:28 +0200 | [diff] [blame] | 840 | int align = dma_get_cache_alignment() - |
| 841 | (xmit->tail & (dma_get_cache_alignment() - 1)); |
Krzysztof Kozlowski | 695b847 | 2020-06-17 17:28:56 +0200 | [diff] [blame] | 842 | if (count - align >= ourport->min_dma_size) { |
| 843 | dma_count = count - align; |
Robert Baldyga | 736cd79 | 2015-07-31 10:58:28 +0200 | [diff] [blame] | 844 | count = align; |
| 845 | } |
Robert Baldyga | 29bef79 | 2014-12-10 12:49:26 +0100 | [diff] [blame] | 846 | } |
| 847 | |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 848 | if (port->x_char) { |
Hyunki Koo | 8fba6c0 | 2020-05-06 17:02:38 +0900 | [diff] [blame] | 849 | wr_reg(port, S3C2410_UTXH, port->x_char); |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 850 | port->icount.tx++; |
| 851 | port->x_char = 0; |
| 852 | goto out; |
| 853 | } |
| 854 | |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 855 | /* if there isn't anything more to transmit, or the uart is now |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 856 | * stopped, disable the uart and exit |
Greg Kroah-Hartman | 7c17525 | 2019-12-10 15:37:05 +0100 | [diff] [blame] | 857 | */ |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 858 | |
| 859 | if (uart_circ_empty(xmit) || uart_tx_stopped(port)) { |
| 860 | s3c24xx_serial_stop_tx(port); |
| 861 | goto out; |
| 862 | } |
| 863 | |
| 864 | /* try and drain the buffer... */ |
| 865 | |
Robert Baldyga | 736cd79 | 2015-07-31 10:58:28 +0200 | [diff] [blame] | 866 | if (count > port->fifosize) { |
| 867 | count = port->fifosize; |
| 868 | dma_count = 0; |
| 869 | } |
| 870 | |
| 871 | while (!uart_circ_empty(xmit) && count > 0) { |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 872 | if (rd_regl(port, S3C2410_UFSTAT) & ourport->info->tx_fifofull) |
| 873 | break; |
| 874 | |
Hyunki Koo | 8fba6c0 | 2020-05-06 17:02:38 +0900 | [diff] [blame] | 875 | wr_reg(port, S3C2410_UTXH, xmit->buf[xmit->tail]); |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 876 | xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1); |
| 877 | port->icount.tx++; |
Robert Baldyga | 736cd79 | 2015-07-31 10:58:28 +0200 | [diff] [blame] | 878 | count--; |
| 879 | } |
| 880 | |
| 881 | if (!count && dma_count) { |
| 882 | s3c24xx_serial_start_tx_dma(ourport, dma_count); |
| 883 | goto out; |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 884 | } |
| 885 | |
Thomas Abraham | c15c374 | 2012-11-22 18:06:28 +0530 | [diff] [blame] | 886 | if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS) { |
| 887 | spin_unlock(&port->lock); |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 888 | uart_write_wakeup(port); |
Thomas Abraham | c15c374 | 2012-11-22 18:06:28 +0530 | [diff] [blame] | 889 | spin_lock(&port->lock); |
| 890 | } |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 891 | |
| 892 | if (uart_circ_empty(xmit)) |
| 893 | s3c24xx_serial_stop_tx(port); |
| 894 | |
Robert Baldyga | ef4aca7 | 2014-11-24 07:56:22 +0100 | [diff] [blame] | 895 | out: |
Thomas Abraham | c15c374 | 2012-11-22 18:06:28 +0530 | [diff] [blame] | 896 | spin_unlock_irqrestore(&port->lock, flags); |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 897 | return IRQ_HANDLED; |
| 898 | } |
| 899 | |
Thomas Abraham | 88bb4ea | 2011-08-10 15:51:19 +0530 | [diff] [blame] | 900 | /* interrupt handler for s3c64xx and later SoC's.*/ |
| 901 | static irqreturn_t s3c64xx_serial_handle_irq(int irq, void *id) |
| 902 | { |
| 903 | struct s3c24xx_uart_port *ourport = id; |
| 904 | struct uart_port *port = &ourport->port; |
| 905 | unsigned int pend = rd_regl(port, S3C64XX_UINTP); |
Thomas Abraham | 88bb4ea | 2011-08-10 15:51:19 +0530 | [diff] [blame] | 906 | irqreturn_t ret = IRQ_HANDLED; |
| 907 | |
Thomas Abraham | 88bb4ea | 2011-08-10 15:51:19 +0530 | [diff] [blame] | 908 | if (pend & S3C64XX_UINTM_RXD_MSK) { |
| 909 | ret = s3c24xx_serial_rx_chars(irq, id); |
| 910 | wr_regl(port, S3C64XX_UINTP, S3C64XX_UINTM_RXD_MSK); |
| 911 | } |
| 912 | if (pend & S3C64XX_UINTM_TXD_MSK) { |
| 913 | ret = s3c24xx_serial_tx_chars(irq, id); |
| 914 | wr_regl(port, S3C64XX_UINTP, S3C64XX_UINTM_TXD_MSK); |
| 915 | } |
Thomas Abraham | 88bb4ea | 2011-08-10 15:51:19 +0530 | [diff] [blame] | 916 | return ret; |
| 917 | } |
| 918 | |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 919 | static unsigned int s3c24xx_serial_tx_empty(struct uart_port *port) |
| 920 | { |
| 921 | struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port); |
| 922 | unsigned long ufstat = rd_regl(port, S3C2410_UFSTAT); |
| 923 | unsigned long ufcon = rd_regl(port, S3C2410_UFCON); |
| 924 | |
| 925 | if (ufcon & S3C2410_UFCON_FIFOMODE) { |
| 926 | if ((ufstat & info->tx_fifomask) != 0 || |
| 927 | (ufstat & info->tx_fifofull)) |
| 928 | return 0; |
| 929 | |
| 930 | return 1; |
| 931 | } |
| 932 | |
| 933 | return s3c24xx_serial_txempty_nofifo(port); |
| 934 | } |
| 935 | |
| 936 | /* no modem control lines */ |
| 937 | static unsigned int s3c24xx_serial_get_mctrl(struct uart_port *port) |
| 938 | { |
Hyunki Koo | 8fba6c0 | 2020-05-06 17:02:38 +0900 | [diff] [blame] | 939 | unsigned int umstat = rd_reg(port, S3C2410_UMSTAT); |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 940 | |
| 941 | if (umstat & S3C2410_UMSTAT_CTS) |
| 942 | return TIOCM_CAR | TIOCM_DSR | TIOCM_CTS; |
| 943 | else |
| 944 | return TIOCM_CAR | TIOCM_DSR; |
| 945 | } |
| 946 | |
| 947 | static void s3c24xx_serial_set_mctrl(struct uart_port *port, unsigned int mctrl) |
| 948 | { |
José Miguel Gonçalves | 2d1e5a4 | 2013-09-18 16:52:49 +0100 | [diff] [blame] | 949 | unsigned int umcon = rd_regl(port, S3C2410_UMCON); |
| 950 | |
| 951 | if (mctrl & TIOCM_RTS) |
| 952 | umcon |= S3C2410_UMCOM_RTS_LOW; |
| 953 | else |
| 954 | umcon &= ~S3C2410_UMCOM_RTS_LOW; |
| 955 | |
| 956 | wr_regl(port, S3C2410_UMCON, umcon); |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 957 | } |
| 958 | |
| 959 | static void s3c24xx_serial_break_ctl(struct uart_port *port, int break_state) |
| 960 | { |
| 961 | unsigned long flags; |
| 962 | unsigned int ucon; |
| 963 | |
| 964 | spin_lock_irqsave(&port->lock, flags); |
| 965 | |
| 966 | ucon = rd_regl(port, S3C2410_UCON); |
| 967 | |
| 968 | if (break_state) |
| 969 | ucon |= S3C2410_UCON_SBREAK; |
| 970 | else |
| 971 | ucon &= ~S3C2410_UCON_SBREAK; |
| 972 | |
| 973 | wr_regl(port, S3C2410_UCON, ucon); |
| 974 | |
| 975 | spin_unlock_irqrestore(&port->lock, flags); |
| 976 | } |
| 977 | |
Robert Baldyga | 62c37ee | 2014-12-10 12:49:25 +0100 | [diff] [blame] | 978 | static int s3c24xx_serial_request_dma(struct s3c24xx_uart_port *p) |
| 979 | { |
| 980 | struct s3c24xx_uart_dma *dma = p->dma; |
Marek Szyprowski | d8db840 | 2018-05-17 13:37:14 +0200 | [diff] [blame] | 981 | struct dma_slave_caps dma_caps; |
| 982 | const char *reason = NULL; |
Marek Szyprowski | 500fcc0 | 2017-04-03 08:21:00 +0200 | [diff] [blame] | 983 | int ret; |
Robert Baldyga | 62c37ee | 2014-12-10 12:49:25 +0100 | [diff] [blame] | 984 | |
| 985 | /* Default slave configuration parameters */ |
| 986 | dma->rx_conf.direction = DMA_DEV_TO_MEM; |
| 987 | dma->rx_conf.src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE; |
| 988 | dma->rx_conf.src_addr = p->port.mapbase + S3C2410_URXH; |
Marek Szyprowski | aa2f80e | 2018-05-10 08:41:13 +0200 | [diff] [blame] | 989 | dma->rx_conf.src_maxburst = 1; |
Robert Baldyga | 62c37ee | 2014-12-10 12:49:25 +0100 | [diff] [blame] | 990 | |
| 991 | dma->tx_conf.direction = DMA_MEM_TO_DEV; |
| 992 | dma->tx_conf.dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE; |
| 993 | dma->tx_conf.dst_addr = p->port.mapbase + S3C2410_UTXH; |
Marek Szyprowski | aa2f80e | 2018-05-10 08:41:13 +0200 | [diff] [blame] | 994 | dma->tx_conf.dst_maxburst = 1; |
Robert Baldyga | 62c37ee | 2014-12-10 12:49:25 +0100 | [diff] [blame] | 995 | |
Marek Szyprowski | ba3d6f8 | 2016-12-16 10:56:53 +0100 | [diff] [blame] | 996 | dma->rx_chan = dma_request_chan(p->port.dev, "rx"); |
Robert Baldyga | 62c37ee | 2014-12-10 12:49:25 +0100 | [diff] [blame] | 997 | |
Marek Szyprowski | d8db840 | 2018-05-17 13:37:14 +0200 | [diff] [blame] | 998 | if (IS_ERR(dma->rx_chan)) { |
| 999 | reason = "DMA RX channel request failed"; |
| 1000 | ret = PTR_ERR(dma->rx_chan); |
| 1001 | goto err_warn; |
| 1002 | } |
| 1003 | |
| 1004 | ret = dma_get_slave_caps(dma->rx_chan, &dma_caps); |
| 1005 | if (ret < 0 || |
| 1006 | dma_caps.residue_granularity < DMA_RESIDUE_GRANULARITY_BURST) { |
| 1007 | reason = "insufficient DMA RX engine capabilities"; |
| 1008 | ret = -EOPNOTSUPP; |
| 1009 | goto err_release_rx; |
| 1010 | } |
Robert Baldyga | 62c37ee | 2014-12-10 12:49:25 +0100 | [diff] [blame] | 1011 | |
| 1012 | dmaengine_slave_config(dma->rx_chan, &dma->rx_conf); |
| 1013 | |
Marek Szyprowski | ba3d6f8 | 2016-12-16 10:56:53 +0100 | [diff] [blame] | 1014 | dma->tx_chan = dma_request_chan(p->port.dev, "tx"); |
| 1015 | if (IS_ERR(dma->tx_chan)) { |
Marek Szyprowski | d8db840 | 2018-05-17 13:37:14 +0200 | [diff] [blame] | 1016 | reason = "DMA TX channel request failed"; |
Marek Szyprowski | 500fcc0 | 2017-04-03 08:21:00 +0200 | [diff] [blame] | 1017 | ret = PTR_ERR(dma->tx_chan); |
| 1018 | goto err_release_rx; |
Robert Baldyga | 62c37ee | 2014-12-10 12:49:25 +0100 | [diff] [blame] | 1019 | } |
| 1020 | |
Marek Szyprowski | d8db840 | 2018-05-17 13:37:14 +0200 | [diff] [blame] | 1021 | ret = dma_get_slave_caps(dma->tx_chan, &dma_caps); |
| 1022 | if (ret < 0 || |
| 1023 | dma_caps.residue_granularity < DMA_RESIDUE_GRANULARITY_BURST) { |
| 1024 | reason = "insufficient DMA TX engine capabilities"; |
| 1025 | ret = -EOPNOTSUPP; |
| 1026 | goto err_release_tx; |
| 1027 | } |
| 1028 | |
Robert Baldyga | 62c37ee | 2014-12-10 12:49:25 +0100 | [diff] [blame] | 1029 | dmaengine_slave_config(dma->tx_chan, &dma->tx_conf); |
| 1030 | |
| 1031 | /* RX buffer */ |
| 1032 | dma->rx_size = PAGE_SIZE; |
| 1033 | |
| 1034 | dma->rx_buf = kmalloc(dma->rx_size, GFP_KERNEL); |
Robert Baldyga | 62c37ee | 2014-12-10 12:49:25 +0100 | [diff] [blame] | 1035 | if (!dma->rx_buf) { |
Marek Szyprowski | 500fcc0 | 2017-04-03 08:21:00 +0200 | [diff] [blame] | 1036 | ret = -ENOMEM; |
| 1037 | goto err_release_tx; |
Robert Baldyga | 62c37ee | 2014-12-10 12:49:25 +0100 | [diff] [blame] | 1038 | } |
| 1039 | |
Marek Szyprowski | 768d64f | 2017-04-03 08:20:59 +0200 | [diff] [blame] | 1040 | dma->rx_addr = dma_map_single(p->port.dev, dma->rx_buf, |
Robert Baldyga | 62c37ee | 2014-12-10 12:49:25 +0100 | [diff] [blame] | 1041 | dma->rx_size, DMA_FROM_DEVICE); |
Marek Szyprowski | 500fcc0 | 2017-04-03 08:21:00 +0200 | [diff] [blame] | 1042 | if (dma_mapping_error(p->port.dev, dma->rx_addr)) { |
Marek Szyprowski | d8db840 | 2018-05-17 13:37:14 +0200 | [diff] [blame] | 1043 | reason = "DMA mapping error for RX buffer"; |
Marek Szyprowski | 500fcc0 | 2017-04-03 08:21:00 +0200 | [diff] [blame] | 1044 | ret = -EIO; |
| 1045 | goto err_free_rx; |
| 1046 | } |
Robert Baldyga | 62c37ee | 2014-12-10 12:49:25 +0100 | [diff] [blame] | 1047 | |
Robert Baldyga | 62c37ee | 2014-12-10 12:49:25 +0100 | [diff] [blame] | 1048 | /* TX buffer */ |
Marek Szyprowski | 768d64f | 2017-04-03 08:20:59 +0200 | [diff] [blame] | 1049 | dma->tx_addr = dma_map_single(p->port.dev, p->port.state->xmit.buf, |
Robert Baldyga | 62c37ee | 2014-12-10 12:49:25 +0100 | [diff] [blame] | 1050 | UART_XMIT_SIZE, DMA_TO_DEVICE); |
Marek Szyprowski | 500fcc0 | 2017-04-03 08:21:00 +0200 | [diff] [blame] | 1051 | if (dma_mapping_error(p->port.dev, dma->tx_addr)) { |
Marek Szyprowski | d8db840 | 2018-05-17 13:37:14 +0200 | [diff] [blame] | 1052 | reason = "DMA mapping error for TX buffer"; |
Marek Szyprowski | 500fcc0 | 2017-04-03 08:21:00 +0200 | [diff] [blame] | 1053 | ret = -EIO; |
| 1054 | goto err_unmap_rx; |
| 1055 | } |
Robert Baldyga | 62c37ee | 2014-12-10 12:49:25 +0100 | [diff] [blame] | 1056 | |
| 1057 | return 0; |
Marek Szyprowski | 500fcc0 | 2017-04-03 08:21:00 +0200 | [diff] [blame] | 1058 | |
| 1059 | err_unmap_rx: |
| 1060 | dma_unmap_single(p->port.dev, dma->rx_addr, dma->rx_size, |
| 1061 | DMA_FROM_DEVICE); |
| 1062 | err_free_rx: |
| 1063 | kfree(dma->rx_buf); |
| 1064 | err_release_tx: |
| 1065 | dma_release_channel(dma->tx_chan); |
| 1066 | err_release_rx: |
| 1067 | dma_release_channel(dma->rx_chan); |
Marek Szyprowski | d8db840 | 2018-05-17 13:37:14 +0200 | [diff] [blame] | 1068 | err_warn: |
| 1069 | if (reason) |
| 1070 | dev_warn(p->port.dev, "%s, DMA will not be used\n", reason); |
Marek Szyprowski | 500fcc0 | 2017-04-03 08:21:00 +0200 | [diff] [blame] | 1071 | return ret; |
Robert Baldyga | 62c37ee | 2014-12-10 12:49:25 +0100 | [diff] [blame] | 1072 | } |
| 1073 | |
| 1074 | static void s3c24xx_serial_release_dma(struct s3c24xx_uart_port *p) |
| 1075 | { |
| 1076 | struct s3c24xx_uart_dma *dma = p->dma; |
| 1077 | |
| 1078 | if (dma->rx_chan) { |
| 1079 | dmaengine_terminate_all(dma->rx_chan); |
Marek Szyprowski | 768d64f | 2017-04-03 08:20:59 +0200 | [diff] [blame] | 1080 | dma_unmap_single(p->port.dev, dma->rx_addr, |
Robert Baldyga | 62c37ee | 2014-12-10 12:49:25 +0100 | [diff] [blame] | 1081 | dma->rx_size, DMA_FROM_DEVICE); |
| 1082 | kfree(dma->rx_buf); |
| 1083 | dma_release_channel(dma->rx_chan); |
| 1084 | dma->rx_chan = NULL; |
| 1085 | } |
| 1086 | |
| 1087 | if (dma->tx_chan) { |
| 1088 | dmaengine_terminate_all(dma->tx_chan); |
Marek Szyprowski | 768d64f | 2017-04-03 08:20:59 +0200 | [diff] [blame] | 1089 | dma_unmap_single(p->port.dev, dma->tx_addr, |
Robert Baldyga | 62c37ee | 2014-12-10 12:49:25 +0100 | [diff] [blame] | 1090 | UART_XMIT_SIZE, DMA_TO_DEVICE); |
| 1091 | dma_release_channel(dma->tx_chan); |
| 1092 | dma->tx_chan = NULL; |
| 1093 | } |
| 1094 | } |
| 1095 | |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 1096 | static void s3c24xx_serial_shutdown(struct uart_port *port) |
| 1097 | { |
| 1098 | struct s3c24xx_uart_port *ourport = to_ourport(port); |
| 1099 | |
| 1100 | if (ourport->tx_claimed) { |
Thomas Abraham | 88bb4ea | 2011-08-10 15:51:19 +0530 | [diff] [blame] | 1101 | if (!s3c24xx_serial_has_interrupt_mask(port)) |
| 1102 | free_irq(ourport->tx_irq, ourport); |
Greg Kroah-Hartman | 8336240 | 2019-12-17 15:02:32 +0100 | [diff] [blame] | 1103 | ourport->tx_enabled = 0; |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 1104 | ourport->tx_claimed = 0; |
Javier Martinez Canillas | e91d863 | 2015-03-13 12:38:51 +0100 | [diff] [blame] | 1105 | ourport->tx_mode = 0; |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 1106 | } |
| 1107 | |
| 1108 | if (ourport->rx_claimed) { |
Thomas Abraham | 88bb4ea | 2011-08-10 15:51:19 +0530 | [diff] [blame] | 1109 | if (!s3c24xx_serial_has_interrupt_mask(port)) |
| 1110 | free_irq(ourport->rx_irq, ourport); |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 1111 | ourport->rx_claimed = 0; |
Greg Kroah-Hartman | 8336240 | 2019-12-17 15:02:32 +0100 | [diff] [blame] | 1112 | ourport->rx_enabled = 0; |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 1113 | } |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 1114 | |
Thomas Abraham | 88bb4ea | 2011-08-10 15:51:19 +0530 | [diff] [blame] | 1115 | /* Clear pending interrupts and mask all interrupts */ |
| 1116 | if (s3c24xx_serial_has_interrupt_mask(port)) { |
Tomasz Figa | b6ad293 | 2013-03-26 15:57:35 +0100 | [diff] [blame] | 1117 | free_irq(port->irq, ourport); |
| 1118 | |
Thomas Abraham | 88bb4ea | 2011-08-10 15:51:19 +0530 | [diff] [blame] | 1119 | wr_regl(port, S3C64XX_UINTP, 0xf); |
| 1120 | wr_regl(port, S3C64XX_UINTM, 0xf); |
| 1121 | } |
Robert Baldyga | 62c37ee | 2014-12-10 12:49:25 +0100 | [diff] [blame] | 1122 | |
| 1123 | if (ourport->dma) |
| 1124 | s3c24xx_serial_release_dma(ourport); |
| 1125 | |
Robert Baldyga | 29bef79 | 2014-12-10 12:49:26 +0100 | [diff] [blame] | 1126 | ourport->tx_in_progress = 0; |
Thomas Abraham | 88bb4ea | 2011-08-10 15:51:19 +0530 | [diff] [blame] | 1127 | } |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 1128 | |
| 1129 | static int s3c24xx_serial_startup(struct uart_port *port) |
| 1130 | { |
| 1131 | struct s3c24xx_uart_port *ourport = to_ourport(port); |
| 1132 | int ret; |
| 1133 | |
Greg Kroah-Hartman | 8336240 | 2019-12-17 15:02:32 +0100 | [diff] [blame] | 1134 | ourport->rx_enabled = 1; |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 1135 | |
Ben Dooks | b73c289c | 2008-10-21 14:07:04 +0100 | [diff] [blame] | 1136 | ret = request_irq(ourport->rx_irq, s3c24xx_serial_rx_chars, 0, |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 1137 | s3c24xx_serial_portname(port), ourport); |
| 1138 | |
| 1139 | if (ret != 0) { |
Sachin Kamat | d20925e | 2012-09-05 10:30:10 +0530 | [diff] [blame] | 1140 | dev_err(port->dev, "cannot get irq %d\n", ourport->rx_irq); |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 1141 | return ret; |
| 1142 | } |
| 1143 | |
| 1144 | ourport->rx_claimed = 1; |
| 1145 | |
Greg Kroah-Hartman | a05025d | 2019-12-10 15:37:03 +0100 | [diff] [blame] | 1146 | dev_dbg(port->dev, "requesting tx irq...\n"); |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 1147 | |
Greg Kroah-Hartman | 8336240 | 2019-12-17 15:02:32 +0100 | [diff] [blame] | 1148 | ourport->tx_enabled = 1; |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 1149 | |
Ben Dooks | b73c289c | 2008-10-21 14:07:04 +0100 | [diff] [blame] | 1150 | ret = request_irq(ourport->tx_irq, s3c24xx_serial_tx_chars, 0, |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 1151 | s3c24xx_serial_portname(port), ourport); |
| 1152 | |
| 1153 | if (ret) { |
Sachin Kamat | d20925e | 2012-09-05 10:30:10 +0530 | [diff] [blame] | 1154 | dev_err(port->dev, "cannot get irq %d\n", ourport->tx_irq); |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 1155 | goto err; |
| 1156 | } |
| 1157 | |
| 1158 | ourport->tx_claimed = 1; |
| 1159 | |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 1160 | /* the port reset code should have done the correct |
Greg Kroah-Hartman | 7c17525 | 2019-12-10 15:37:05 +0100 | [diff] [blame] | 1161 | * register setup for the port controls |
| 1162 | */ |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 1163 | |
| 1164 | return ret; |
| 1165 | |
Robert Baldyga | ef4aca7 | 2014-11-24 07:56:22 +0100 | [diff] [blame] | 1166 | err: |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 1167 | s3c24xx_serial_shutdown(port); |
| 1168 | return ret; |
| 1169 | } |
| 1170 | |
Thomas Abraham | 88bb4ea | 2011-08-10 15:51:19 +0530 | [diff] [blame] | 1171 | static int s3c64xx_serial_startup(struct uart_port *port) |
| 1172 | { |
| 1173 | struct s3c24xx_uart_port *ourport = to_ourport(port); |
Robert Baldyga | b543c30 | 2014-12-10 12:49:27 +0100 | [diff] [blame] | 1174 | unsigned long flags; |
| 1175 | unsigned int ufcon; |
Thomas Abraham | 88bb4ea | 2011-08-10 15:51:19 +0530 | [diff] [blame] | 1176 | int ret; |
| 1177 | |
Tomasz Figa | b6ad293 | 2013-03-26 15:57:35 +0100 | [diff] [blame] | 1178 | wr_regl(port, S3C64XX_UINTM, 0xf); |
Robert Baldyga | 62c37ee | 2014-12-10 12:49:25 +0100 | [diff] [blame] | 1179 | if (ourport->dma) { |
| 1180 | ret = s3c24xx_serial_request_dma(ourport); |
| 1181 | if (ret < 0) { |
Krzysztof Kozlowski | f98c7bc | 2017-02-25 18:36:44 +0200 | [diff] [blame] | 1182 | devm_kfree(port->dev, ourport->dma); |
| 1183 | ourport->dma = NULL; |
Robert Baldyga | 62c37ee | 2014-12-10 12:49:25 +0100 | [diff] [blame] | 1184 | } |
| 1185 | } |
Tomasz Figa | b6ad293 | 2013-03-26 15:57:35 +0100 | [diff] [blame] | 1186 | |
Thomas Abraham | 88bb4ea | 2011-08-10 15:51:19 +0530 | [diff] [blame] | 1187 | ret = request_irq(port->irq, s3c64xx_serial_handle_irq, IRQF_SHARED, |
| 1188 | s3c24xx_serial_portname(port), ourport); |
| 1189 | if (ret) { |
Sachin Kamat | d20925e | 2012-09-05 10:30:10 +0530 | [diff] [blame] | 1190 | dev_err(port->dev, "cannot get irq %d\n", port->irq); |
Thomas Abraham | 88bb4ea | 2011-08-10 15:51:19 +0530 | [diff] [blame] | 1191 | return ret; |
| 1192 | } |
| 1193 | |
| 1194 | /* For compatibility with s3c24xx Soc's */ |
Greg Kroah-Hartman | 8336240 | 2019-12-17 15:02:32 +0100 | [diff] [blame] | 1195 | ourport->rx_enabled = 1; |
Thomas Abraham | 88bb4ea | 2011-08-10 15:51:19 +0530 | [diff] [blame] | 1196 | ourport->rx_claimed = 1; |
Greg Kroah-Hartman | 8336240 | 2019-12-17 15:02:32 +0100 | [diff] [blame] | 1197 | ourport->tx_enabled = 0; |
Thomas Abraham | 88bb4ea | 2011-08-10 15:51:19 +0530 | [diff] [blame] | 1198 | ourport->tx_claimed = 1; |
| 1199 | |
Robert Baldyga | 29bef79 | 2014-12-10 12:49:26 +0100 | [diff] [blame] | 1200 | spin_lock_irqsave(&port->lock, flags); |
| 1201 | |
| 1202 | ufcon = rd_regl(port, S3C2410_UFCON); |
Robert Baldyga | 31c6ba9 | 2015-04-17 08:43:09 +0200 | [diff] [blame] | 1203 | ufcon |= S3C2410_UFCON_RESETRX | S5PV210_UFCON_RXTRIG8; |
| 1204 | if (!uart_console(port)) |
| 1205 | ufcon |= S3C2410_UFCON_RESETTX; |
Robert Baldyga | 29bef79 | 2014-12-10 12:49:26 +0100 | [diff] [blame] | 1206 | wr_regl(port, S3C2410_UFCON, ufcon); |
| 1207 | |
| 1208 | enable_rx_pio(ourport); |
| 1209 | |
| 1210 | spin_unlock_irqrestore(&port->lock, flags); |
| 1211 | |
Thomas Abraham | 88bb4ea | 2011-08-10 15:51:19 +0530 | [diff] [blame] | 1212 | /* Enable Rx Interrupt */ |
Matthew Leach | bbb5ff9 | 2016-06-22 17:57:03 +0100 | [diff] [blame] | 1213 | s3c24xx_clear_bit(port, S3C64XX_UINTM_RXD, S3C64XX_UINTM); |
Robert Baldyga | 29bef79 | 2014-12-10 12:49:26 +0100 | [diff] [blame] | 1214 | |
Thomas Abraham | 88bb4ea | 2011-08-10 15:51:19 +0530 | [diff] [blame] | 1215 | return ret; |
| 1216 | } |
| 1217 | |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 1218 | /* power power management control */ |
| 1219 | |
| 1220 | static void s3c24xx_serial_pm(struct uart_port *port, unsigned int level, |
| 1221 | unsigned int old) |
| 1222 | { |
| 1223 | struct s3c24xx_uart_port *ourport = to_ourport(port); |
Robert Baldyga | 1ff383a | 2014-11-24 07:56:21 +0100 | [diff] [blame] | 1224 | int timeout = 10000; |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 1225 | |
Ben Dooks | 3055547 | 2008-10-21 14:06:36 +0100 | [diff] [blame] | 1226 | ourport->pm_level = level; |
| 1227 | |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 1228 | switch (level) { |
| 1229 | case 3: |
Robert Baldyga | 1ff383a | 2014-11-24 07:56:21 +0100 | [diff] [blame] | 1230 | while (--timeout && !s3c24xx_serial_txempty_nofifo(port)) |
| 1231 | udelay(100); |
| 1232 | |
Kyoungil Kim | 7cd8883 | 2012-05-20 17:45:54 +0900 | [diff] [blame] | 1233 | if (!IS_ERR(ourport->baudclk)) |
Thomas Abraham | 9484b00 | 2012-10-03 07:40:04 +0900 | [diff] [blame] | 1234 | clk_disable_unprepare(ourport->baudclk); |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 1235 | |
Thomas Abraham | 9484b00 | 2012-10-03 07:40:04 +0900 | [diff] [blame] | 1236 | clk_disable_unprepare(ourport->clk); |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 1237 | break; |
| 1238 | |
| 1239 | case 0: |
Thomas Abraham | 9484b00 | 2012-10-03 07:40:04 +0900 | [diff] [blame] | 1240 | clk_prepare_enable(ourport->clk); |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 1241 | |
Kyoungil Kim | 7cd8883 | 2012-05-20 17:45:54 +0900 | [diff] [blame] | 1242 | if (!IS_ERR(ourport->baudclk)) |
Thomas Abraham | 9484b00 | 2012-10-03 07:40:04 +0900 | [diff] [blame] | 1243 | clk_prepare_enable(ourport->baudclk); |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 1244 | |
| 1245 | break; |
| 1246 | default: |
Sachin Kamat | d20925e | 2012-09-05 10:30:10 +0530 | [diff] [blame] | 1247 | dev_err(port->dev, "s3c24xx_serial: unknown pm %d\n", level); |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 1248 | } |
| 1249 | } |
| 1250 | |
| 1251 | /* baud rate calculation |
| 1252 | * |
| 1253 | * The UARTs on the S3C2410/S3C2440 can take their clocks from a number |
| 1254 | * of different sources, including the peripheral clock ("pclk") and an |
| 1255 | * external clock ("uclk"). The S3C2440 also adds the core clock ("fclk") |
| 1256 | * with a programmable extra divisor. |
| 1257 | * |
| 1258 | * The following code goes through the clock sources, and calculates the |
| 1259 | * baud clocks (and the resultant actual baud rates) and then tries to |
| 1260 | * pick the closest one and select that. |
| 1261 | * |
Greg Kroah-Hartman | 7c17525 | 2019-12-10 15:37:05 +0100 | [diff] [blame] | 1262 | */ |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 1263 | |
Thomas Abraham | 5f5a7a5 | 2011-10-24 11:47:46 +0200 | [diff] [blame] | 1264 | #define MAX_CLK_NAME_LENGTH 15 |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 1265 | |
Thomas Abraham | 5f5a7a5 | 2011-10-24 11:47:46 +0200 | [diff] [blame] | 1266 | static inline int s3c24xx_serial_getsource(struct uart_port *port) |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 1267 | { |
| 1268 | struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port); |
Thomas Abraham | 5f5a7a5 | 2011-10-24 11:47:46 +0200 | [diff] [blame] | 1269 | unsigned int ucon; |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 1270 | |
Thomas Abraham | 5f5a7a5 | 2011-10-24 11:47:46 +0200 | [diff] [blame] | 1271 | if (info->num_clks == 1) |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 1272 | return 0; |
| 1273 | |
Thomas Abraham | 5f5a7a5 | 2011-10-24 11:47:46 +0200 | [diff] [blame] | 1274 | ucon = rd_regl(port, S3C2410_UCON); |
| 1275 | ucon &= info->clksel_mask; |
| 1276 | return ucon >> info->clksel_shift; |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 1277 | } |
| 1278 | |
Thomas Abraham | 5f5a7a5 | 2011-10-24 11:47:46 +0200 | [diff] [blame] | 1279 | static void s3c24xx_serial_setsource(struct uart_port *port, |
| 1280 | unsigned int clk_sel) |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 1281 | { |
Thomas Abraham | 5f5a7a5 | 2011-10-24 11:47:46 +0200 | [diff] [blame] | 1282 | struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port); |
| 1283 | unsigned int ucon; |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 1284 | |
Thomas Abraham | 5f5a7a5 | 2011-10-24 11:47:46 +0200 | [diff] [blame] | 1285 | if (info->num_clks == 1) |
| 1286 | return; |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 1287 | |
Thomas Abraham | 5f5a7a5 | 2011-10-24 11:47:46 +0200 | [diff] [blame] | 1288 | ucon = rd_regl(port, S3C2410_UCON); |
| 1289 | if ((ucon & info->clksel_mask) >> info->clksel_shift == clk_sel) |
| 1290 | return; |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 1291 | |
Thomas Abraham | 5f5a7a5 | 2011-10-24 11:47:46 +0200 | [diff] [blame] | 1292 | ucon &= ~info->clksel_mask; |
| 1293 | ucon |= clk_sel << info->clksel_shift; |
| 1294 | wr_regl(port, S3C2410_UCON, ucon); |
| 1295 | } |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 1296 | |
Thomas Abraham | 5f5a7a5 | 2011-10-24 11:47:46 +0200 | [diff] [blame] | 1297 | static unsigned int s3c24xx_serial_getclk(struct s3c24xx_uart_port *ourport, |
| 1298 | unsigned int req_baud, struct clk **best_clk, |
| 1299 | unsigned int *clk_num) |
| 1300 | { |
| 1301 | struct s3c24xx_uart_info *info = ourport->info; |
| 1302 | struct clk *clk; |
| 1303 | unsigned long rate; |
Jonathan Bakker | 7d31676 | 2020-05-08 18:34:33 -0700 | [diff] [blame] | 1304 | unsigned int cnt, baud, quot, best_quot = 0; |
Thomas Abraham | 5f5a7a5 | 2011-10-24 11:47:46 +0200 | [diff] [blame] | 1305 | char clkname[MAX_CLK_NAME_LENGTH]; |
| 1306 | int calc_deviation, deviation = (1 << 30) - 1; |
| 1307 | |
Thomas Abraham | 5f5a7a5 | 2011-10-24 11:47:46 +0200 | [diff] [blame] | 1308 | for (cnt = 0; cnt < info->num_clks; cnt++) { |
Jonathan Bakker | 7d31676 | 2020-05-08 18:34:33 -0700 | [diff] [blame] | 1309 | /* Keep selected clock if provided */ |
| 1310 | if (ourport->cfg->clk_sel && |
| 1311 | !(ourport->cfg->clk_sel & (1 << cnt))) |
Thomas Abraham | 5f5a7a5 | 2011-10-24 11:47:46 +0200 | [diff] [blame] | 1312 | continue; |
| 1313 | |
| 1314 | sprintf(clkname, "clk_uart_baud%d", cnt); |
| 1315 | clk = clk_get(ourport->port.dev, clkname); |
Kyoungil Kim | 7cd8883 | 2012-05-20 17:45:54 +0900 | [diff] [blame] | 1316 | if (IS_ERR(clk)) |
Thomas Abraham | 5f5a7a5 | 2011-10-24 11:47:46 +0200 | [diff] [blame] | 1317 | continue; |
| 1318 | |
| 1319 | rate = clk_get_rate(clk); |
| 1320 | if (!rate) |
| 1321 | continue; |
| 1322 | |
| 1323 | if (ourport->info->has_divslot) { |
| 1324 | unsigned long div = rate / req_baud; |
| 1325 | |
| 1326 | /* The UDIVSLOT register on the newer UARTs allows us to |
| 1327 | * get a divisor adjustment of 1/16th on the baud clock. |
| 1328 | * |
| 1329 | * We don't keep the UDIVSLOT value (the 16ths we |
| 1330 | * calculated by not multiplying the baud by 16) as it |
| 1331 | * is easy enough to recalculate. |
| 1332 | */ |
| 1333 | |
| 1334 | quot = div / 16; |
| 1335 | baud = rate / div; |
| 1336 | } else { |
| 1337 | quot = (rate + (8 * req_baud)) / (16 * req_baud); |
| 1338 | baud = rate / (quot * 16); |
| 1339 | } |
| 1340 | quot--; |
| 1341 | |
| 1342 | calc_deviation = req_baud - baud; |
| 1343 | if (calc_deviation < 0) |
| 1344 | calc_deviation = -calc_deviation; |
| 1345 | |
| 1346 | if (calc_deviation < deviation) { |
| 1347 | *best_clk = clk; |
| 1348 | best_quot = quot; |
| 1349 | *clk_num = cnt; |
| 1350 | deviation = calc_deviation; |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 1351 | } |
| 1352 | } |
| 1353 | |
Thomas Abraham | 5f5a7a5 | 2011-10-24 11:47:46 +0200 | [diff] [blame] | 1354 | return best_quot; |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 1355 | } |
| 1356 | |
Ben Dooks | 090f848d | 2008-12-12 00:24:21 +0000 | [diff] [blame] | 1357 | /* udivslot_table[] |
| 1358 | * |
| 1359 | * This table takes the fractional value of the baud divisor and gives |
| 1360 | * the recommended setting for the UDIVSLOT register. |
| 1361 | */ |
| 1362 | static u16 udivslot_table[16] = { |
| 1363 | [0] = 0x0000, |
| 1364 | [1] = 0x0080, |
| 1365 | [2] = 0x0808, |
| 1366 | [3] = 0x0888, |
| 1367 | [4] = 0x2222, |
| 1368 | [5] = 0x4924, |
| 1369 | [6] = 0x4A52, |
| 1370 | [7] = 0x54AA, |
| 1371 | [8] = 0x5555, |
| 1372 | [9] = 0xD555, |
| 1373 | [10] = 0xD5D5, |
| 1374 | [11] = 0xDDD5, |
| 1375 | [12] = 0xDDDD, |
| 1376 | [13] = 0xDFDD, |
| 1377 | [14] = 0xDFDF, |
| 1378 | [15] = 0xFFDF, |
| 1379 | }; |
| 1380 | |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 1381 | static void s3c24xx_serial_set_termios(struct uart_port *port, |
| 1382 | struct ktermios *termios, |
| 1383 | struct ktermios *old) |
| 1384 | { |
| 1385 | struct s3c2410_uartcfg *cfg = s3c24xx_port_to_cfg(port); |
| 1386 | struct s3c24xx_uart_port *ourport = to_ourport(port); |
Kyoungil Kim | 7cd8883 | 2012-05-20 17:45:54 +0900 | [diff] [blame] | 1387 | struct clk *clk = ERR_PTR(-EINVAL); |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 1388 | unsigned long flags; |
Thomas Abraham | 5f5a7a5 | 2011-10-24 11:47:46 +0200 | [diff] [blame] | 1389 | unsigned int baud, quot, clk_sel = 0; |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 1390 | unsigned int ulcon; |
| 1391 | unsigned int umcon; |
Ben Dooks | 090f848d | 2008-12-12 00:24:21 +0000 | [diff] [blame] | 1392 | unsigned int udivslot = 0; |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 1393 | |
| 1394 | /* |
| 1395 | * We don't support modem control lines. |
| 1396 | */ |
| 1397 | termios->c_cflag &= ~(HUPCL | CMSPAR); |
| 1398 | termios->c_cflag |= CLOCAL; |
| 1399 | |
| 1400 | /* |
| 1401 | * Ask the core to calculate the divisor for us. |
| 1402 | */ |
| 1403 | |
Seung-Woo Kim | ec18f48 | 2018-12-14 12:34:09 +0100 | [diff] [blame] | 1404 | baud = uart_get_baud_rate(port, termios, old, 0, 3000000); |
Thomas Abraham | 5f5a7a5 | 2011-10-24 11:47:46 +0200 | [diff] [blame] | 1405 | quot = s3c24xx_serial_getclk(ourport, baud, &clk, &clk_sel); |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 1406 | if (baud == 38400 && (port->flags & UPF_SPD_MASK) == UPF_SPD_CUST) |
| 1407 | quot = port->custom_divisor; |
Kyoungil Kim | 7cd8883 | 2012-05-20 17:45:54 +0900 | [diff] [blame] | 1408 | if (IS_ERR(clk)) |
Thomas Abraham | 5f5a7a5 | 2011-10-24 11:47:46 +0200 | [diff] [blame] | 1409 | return; |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 1410 | |
| 1411 | /* check to see if we need to change clock source */ |
| 1412 | |
Thomas Abraham | 5f5a7a5 | 2011-10-24 11:47:46 +0200 | [diff] [blame] | 1413 | if (ourport->baudclk != clk) { |
Chanwoo Choi | b8995f5 | 2016-04-21 18:58:31 +0900 | [diff] [blame] | 1414 | clk_prepare_enable(clk); |
| 1415 | |
Thomas Abraham | 5f5a7a5 | 2011-10-24 11:47:46 +0200 | [diff] [blame] | 1416 | s3c24xx_serial_setsource(port, clk_sel); |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 1417 | |
Kyoungil Kim | 7cd8883 | 2012-05-20 17:45:54 +0900 | [diff] [blame] | 1418 | if (!IS_ERR(ourport->baudclk)) { |
Thomas Abraham | 9484b00 | 2012-10-03 07:40:04 +0900 | [diff] [blame] | 1419 | clk_disable_unprepare(ourport->baudclk); |
Kyoungil Kim | 7cd8883 | 2012-05-20 17:45:54 +0900 | [diff] [blame] | 1420 | ourport->baudclk = ERR_PTR(-EINVAL); |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 1421 | } |
| 1422 | |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 1423 | ourport->baudclk = clk; |
Ben Dooks | 3055547 | 2008-10-21 14:06:36 +0100 | [diff] [blame] | 1424 | ourport->baudclk_rate = clk ? clk_get_rate(clk) : 0; |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 1425 | } |
| 1426 | |
Ben Dooks | 090f848d | 2008-12-12 00:24:21 +0000 | [diff] [blame] | 1427 | if (ourport->info->has_divslot) { |
| 1428 | unsigned int div = ourport->baudclk_rate / baud; |
| 1429 | |
Jongpill Lee | 8b526ae | 2010-07-16 10:19:41 +0900 | [diff] [blame] | 1430 | if (cfg->has_fracval) { |
| 1431 | udivslot = (div & 15); |
Greg Kroah-Hartman | a05025d | 2019-12-10 15:37:03 +0100 | [diff] [blame] | 1432 | dev_dbg(port->dev, "fracval = %04x\n", udivslot); |
Jongpill Lee | 8b526ae | 2010-07-16 10:19:41 +0900 | [diff] [blame] | 1433 | } else { |
| 1434 | udivslot = udivslot_table[div & 15]; |
Greg Kroah-Hartman | a05025d | 2019-12-10 15:37:03 +0100 | [diff] [blame] | 1435 | dev_dbg(port->dev, "udivslot = %04x (div %d)\n", |
| 1436 | udivslot, div & 15); |
Jongpill Lee | 8b526ae | 2010-07-16 10:19:41 +0900 | [diff] [blame] | 1437 | } |
Ben Dooks | 090f848d | 2008-12-12 00:24:21 +0000 | [diff] [blame] | 1438 | } |
| 1439 | |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 1440 | switch (termios->c_cflag & CSIZE) { |
| 1441 | case CS5: |
Greg Kroah-Hartman | a05025d | 2019-12-10 15:37:03 +0100 | [diff] [blame] | 1442 | dev_dbg(port->dev, "config: 5bits/char\n"); |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 1443 | ulcon = S3C2410_LCON_CS5; |
| 1444 | break; |
| 1445 | case CS6: |
Greg Kroah-Hartman | a05025d | 2019-12-10 15:37:03 +0100 | [diff] [blame] | 1446 | dev_dbg(port->dev, "config: 6bits/char\n"); |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 1447 | ulcon = S3C2410_LCON_CS6; |
| 1448 | break; |
| 1449 | case CS7: |
Greg Kroah-Hartman | a05025d | 2019-12-10 15:37:03 +0100 | [diff] [blame] | 1450 | dev_dbg(port->dev, "config: 7bits/char\n"); |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 1451 | ulcon = S3C2410_LCON_CS7; |
| 1452 | break; |
| 1453 | case CS8: |
| 1454 | default: |
Greg Kroah-Hartman | a05025d | 2019-12-10 15:37:03 +0100 | [diff] [blame] | 1455 | dev_dbg(port->dev, "config: 8bits/char\n"); |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 1456 | ulcon = S3C2410_LCON_CS8; |
| 1457 | break; |
| 1458 | } |
| 1459 | |
| 1460 | /* preserve original lcon IR settings */ |
| 1461 | ulcon |= (cfg->ulcon & S3C2410_LCON_IRM); |
| 1462 | |
| 1463 | if (termios->c_cflag & CSTOPB) |
| 1464 | ulcon |= S3C2410_LCON_STOPB; |
| 1465 | |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 1466 | if (termios->c_cflag & PARENB) { |
| 1467 | if (termios->c_cflag & PARODD) |
| 1468 | ulcon |= S3C2410_LCON_PODD; |
| 1469 | else |
| 1470 | ulcon |= S3C2410_LCON_PEVEN; |
| 1471 | } else { |
| 1472 | ulcon |= S3C2410_LCON_PNONE; |
| 1473 | } |
| 1474 | |
| 1475 | spin_lock_irqsave(&port->lock, flags); |
| 1476 | |
Greg Kroah-Hartman | a05025d | 2019-12-10 15:37:03 +0100 | [diff] [blame] | 1477 | dev_dbg(port->dev, |
| 1478 | "setting ulcon to %08x, brddiv to %d, udivslot %08x\n", |
| 1479 | ulcon, quot, udivslot); |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 1480 | |
| 1481 | wr_regl(port, S3C2410_ULCON, ulcon); |
| 1482 | wr_regl(port, S3C2410_UBRDIV, quot); |
José Miguel Gonçalves | 2d1e5a4 | 2013-09-18 16:52:49 +0100 | [diff] [blame] | 1483 | |
Beomho Seo | 31e9336 | 2018-12-14 12:34:08 +0100 | [diff] [blame] | 1484 | port->status &= ~UPSTAT_AUTOCTS; |
| 1485 | |
José Miguel Gonçalves | 2d1e5a4 | 2013-09-18 16:52:49 +0100 | [diff] [blame] | 1486 | umcon = rd_regl(port, S3C2410_UMCON); |
| 1487 | if (termios->c_cflag & CRTSCTS) { |
| 1488 | umcon |= S3C2410_UMCOM_AFC; |
| 1489 | /* Disable RTS when RX FIFO contains 63 bytes */ |
| 1490 | umcon &= ~S3C2412_UMCON_AFC_8; |
Beomho Seo | 31e9336 | 2018-12-14 12:34:08 +0100 | [diff] [blame] | 1491 | port->status = UPSTAT_AUTOCTS; |
José Miguel Gonçalves | 2d1e5a4 | 2013-09-18 16:52:49 +0100 | [diff] [blame] | 1492 | } else { |
| 1493 | umcon &= ~S3C2410_UMCOM_AFC; |
| 1494 | } |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 1495 | wr_regl(port, S3C2410_UMCON, umcon); |
| 1496 | |
Ben Dooks | 090f848d | 2008-12-12 00:24:21 +0000 | [diff] [blame] | 1497 | if (ourport->info->has_divslot) |
| 1498 | wr_regl(port, S3C2443_DIVSLOT, udivslot); |
| 1499 | |
Greg Kroah-Hartman | a05025d | 2019-12-10 15:37:03 +0100 | [diff] [blame] | 1500 | dev_dbg(port->dev, |
| 1501 | "uart: ulcon = 0x%08x, ucon = 0x%08x, ufcon = 0x%08x\n", |
| 1502 | rd_regl(port, S3C2410_ULCON), |
| 1503 | rd_regl(port, S3C2410_UCON), |
| 1504 | rd_regl(port, S3C2410_UFCON)); |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 1505 | |
| 1506 | /* |
| 1507 | * Update the per-port timeout. |
| 1508 | */ |
| 1509 | uart_update_timeout(port, termios->c_cflag, baud); |
| 1510 | |
| 1511 | /* |
| 1512 | * Which character status flags are we interested in? |
| 1513 | */ |
| 1514 | port->read_status_mask = S3C2410_UERSTAT_OVERRUN; |
| 1515 | if (termios->c_iflag & INPCK) |
Robert Baldyga | ef4aca7 | 2014-11-24 07:56:22 +0100 | [diff] [blame] | 1516 | port->read_status_mask |= S3C2410_UERSTAT_FRAME | |
| 1517 | S3C2410_UERSTAT_PARITY; |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 1518 | /* |
| 1519 | * Which character status flags should we ignore? |
| 1520 | */ |
| 1521 | port->ignore_status_mask = 0; |
| 1522 | if (termios->c_iflag & IGNPAR) |
| 1523 | port->ignore_status_mask |= S3C2410_UERSTAT_OVERRUN; |
| 1524 | if (termios->c_iflag & IGNBRK && termios->c_iflag & IGNPAR) |
| 1525 | port->ignore_status_mask |= S3C2410_UERSTAT_FRAME; |
| 1526 | |
| 1527 | /* |
| 1528 | * Ignore all characters if CREAD is not set. |
| 1529 | */ |
| 1530 | if ((termios->c_cflag & CREAD) == 0) |
| 1531 | port->ignore_status_mask |= RXSTAT_DUMMY_READ; |
| 1532 | |
| 1533 | spin_unlock_irqrestore(&port->lock, flags); |
| 1534 | } |
| 1535 | |
| 1536 | static const char *s3c24xx_serial_type(struct uart_port *port) |
| 1537 | { |
| 1538 | switch (port->type) { |
| 1539 | case PORT_S3C2410: |
| 1540 | return "S3C2410"; |
| 1541 | case PORT_S3C2440: |
| 1542 | return "S3C2440"; |
| 1543 | case PORT_S3C2412: |
| 1544 | return "S3C2412"; |
Ben Dooks | b690ace | 2008-10-21 14:07:03 +0100 | [diff] [blame] | 1545 | case PORT_S3C6400: |
| 1546 | return "S3C6400/10"; |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 1547 | default: |
| 1548 | return NULL; |
| 1549 | } |
| 1550 | } |
| 1551 | |
| 1552 | #define MAP_SIZE (0x100) |
| 1553 | |
| 1554 | static void s3c24xx_serial_release_port(struct uart_port *port) |
| 1555 | { |
| 1556 | release_mem_region(port->mapbase, MAP_SIZE); |
| 1557 | } |
| 1558 | |
| 1559 | static int s3c24xx_serial_request_port(struct uart_port *port) |
| 1560 | { |
| 1561 | const char *name = s3c24xx_serial_portname(port); |
Greg Kroah-Hartman | 9fe0d41 | 2019-12-10 15:37:06 +0100 | [diff] [blame] | 1562 | |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 1563 | return request_mem_region(port->mapbase, MAP_SIZE, name) ? 0 : -EBUSY; |
| 1564 | } |
| 1565 | |
| 1566 | static void s3c24xx_serial_config_port(struct uart_port *port, int flags) |
| 1567 | { |
| 1568 | struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port); |
| 1569 | |
| 1570 | if (flags & UART_CONFIG_TYPE && |
| 1571 | s3c24xx_serial_request_port(port) == 0) |
| 1572 | port->type = info->type; |
| 1573 | } |
| 1574 | |
| 1575 | /* |
| 1576 | * verify the new serial_struct (for TIOCSSERIAL). |
| 1577 | */ |
| 1578 | static int |
| 1579 | s3c24xx_serial_verify_port(struct uart_port *port, struct serial_struct *ser) |
| 1580 | { |
| 1581 | struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port); |
| 1582 | |
| 1583 | if (ser->type != PORT_UNKNOWN && ser->type != info->type) |
| 1584 | return -EINVAL; |
| 1585 | |
| 1586 | return 0; |
| 1587 | } |
| 1588 | |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 1589 | #ifdef CONFIG_SERIAL_SAMSUNG_CONSOLE |
| 1590 | |
| 1591 | static struct console s3c24xx_serial_console; |
| 1592 | |
Julien Pichon | 93b5c03 | 2012-09-21 23:22:31 -0700 | [diff] [blame] | 1593 | static int __init s3c24xx_serial_console_init(void) |
| 1594 | { |
| 1595 | register_console(&s3c24xx_serial_console); |
| 1596 | return 0; |
| 1597 | } |
| 1598 | console_initcall(s3c24xx_serial_console_init); |
| 1599 | |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 1600 | #define S3C24XX_SERIAL_CONSOLE &s3c24xx_serial_console |
| 1601 | #else |
| 1602 | #define S3C24XX_SERIAL_CONSOLE NULL |
| 1603 | #endif |
| 1604 | |
Arnd Bergmann | 84f57d9 | 2013-04-11 02:04:49 +0200 | [diff] [blame] | 1605 | #if defined(CONFIG_SERIAL_SAMSUNG_CONSOLE) && defined(CONFIG_CONSOLE_POLL) |
Julien Pichon | 93b5c03 | 2012-09-21 23:22:31 -0700 | [diff] [blame] | 1606 | static int s3c24xx_serial_get_poll_char(struct uart_port *port); |
| 1607 | static void s3c24xx_serial_put_poll_char(struct uart_port *port, |
| 1608 | unsigned char c); |
| 1609 | #endif |
| 1610 | |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 1611 | static struct uart_ops s3c24xx_serial_ops = { |
| 1612 | .pm = s3c24xx_serial_pm, |
| 1613 | .tx_empty = s3c24xx_serial_tx_empty, |
| 1614 | .get_mctrl = s3c24xx_serial_get_mctrl, |
| 1615 | .set_mctrl = s3c24xx_serial_set_mctrl, |
| 1616 | .stop_tx = s3c24xx_serial_stop_tx, |
| 1617 | .start_tx = s3c24xx_serial_start_tx, |
| 1618 | .stop_rx = s3c24xx_serial_stop_rx, |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 1619 | .break_ctl = s3c24xx_serial_break_ctl, |
| 1620 | .startup = s3c24xx_serial_startup, |
| 1621 | .shutdown = s3c24xx_serial_shutdown, |
| 1622 | .set_termios = s3c24xx_serial_set_termios, |
| 1623 | .type = s3c24xx_serial_type, |
| 1624 | .release_port = s3c24xx_serial_release_port, |
| 1625 | .request_port = s3c24xx_serial_request_port, |
| 1626 | .config_port = s3c24xx_serial_config_port, |
| 1627 | .verify_port = s3c24xx_serial_verify_port, |
Arnd Bergmann | 84f57d9 | 2013-04-11 02:04:49 +0200 | [diff] [blame] | 1628 | #if defined(CONFIG_SERIAL_SAMSUNG_CONSOLE) && defined(CONFIG_CONSOLE_POLL) |
Julien Pichon | 93b5c03 | 2012-09-21 23:22:31 -0700 | [diff] [blame] | 1629 | .poll_get_char = s3c24xx_serial_get_poll_char, |
| 1630 | .poll_put_char = s3c24xx_serial_put_poll_char, |
| 1631 | #endif |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 1632 | }; |
| 1633 | |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 1634 | static struct uart_driver s3c24xx_uart_drv = { |
| 1635 | .owner = THIS_MODULE, |
Darius Augulis | 2cf0c58 | 2011-01-12 14:50:51 +0900 | [diff] [blame] | 1636 | .driver_name = "s3c2410_serial", |
Ben Dooks | bdd4915 | 2008-11-03 19:51:42 +0000 | [diff] [blame] | 1637 | .nr = CONFIG_SERIAL_SAMSUNG_UARTS, |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 1638 | .cons = S3C24XX_SERIAL_CONSOLE, |
Darius Augulis | 2cf0c58 | 2011-01-12 14:50:51 +0900 | [diff] [blame] | 1639 | .dev_name = S3C24XX_SERIAL_NAME, |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 1640 | .major = S3C24XX_SERIAL_MAJOR, |
| 1641 | .minor = S3C24XX_SERIAL_MINOR, |
| 1642 | }; |
| 1643 | |
Robert Baldyga | ef4aca7 | 2014-11-24 07:56:22 +0100 | [diff] [blame] | 1644 | #define __PORT_LOCK_UNLOCKED(i) \ |
| 1645 | __SPIN_LOCK_UNLOCKED(s3c24xx_serial_ports[i].port.lock) |
| 1646 | static struct s3c24xx_uart_port |
| 1647 | s3c24xx_serial_ports[CONFIG_SERIAL_SAMSUNG_UARTS] = { |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 1648 | [0] = { |
| 1649 | .port = { |
Robert Baldyga | ef4aca7 | 2014-11-24 07:56:22 +0100 | [diff] [blame] | 1650 | .lock = __PORT_LOCK_UNLOCKED(0), |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 1651 | .iotype = UPIO_MEM, |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 1652 | .uartclk = 0, |
| 1653 | .fifosize = 16, |
| 1654 | .ops = &s3c24xx_serial_ops, |
| 1655 | .flags = UPF_BOOT_AUTOCONF, |
| 1656 | .line = 0, |
| 1657 | } |
| 1658 | }, |
| 1659 | [1] = { |
| 1660 | .port = { |
Robert Baldyga | ef4aca7 | 2014-11-24 07:56:22 +0100 | [diff] [blame] | 1661 | .lock = __PORT_LOCK_UNLOCKED(1), |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 1662 | .iotype = UPIO_MEM, |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 1663 | .uartclk = 0, |
| 1664 | .fifosize = 16, |
| 1665 | .ops = &s3c24xx_serial_ops, |
| 1666 | .flags = UPF_BOOT_AUTOCONF, |
| 1667 | .line = 1, |
| 1668 | } |
| 1669 | }, |
Ben Dooks | 03d5e77 | 2008-11-03 09:21:23 +0000 | [diff] [blame] | 1670 | #if CONFIG_SERIAL_SAMSUNG_UARTS > 2 |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 1671 | [2] = { |
| 1672 | .port = { |
Robert Baldyga | ef4aca7 | 2014-11-24 07:56:22 +0100 | [diff] [blame] | 1673 | .lock = __PORT_LOCK_UNLOCKED(2), |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 1674 | .iotype = UPIO_MEM, |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 1675 | .uartclk = 0, |
| 1676 | .fifosize = 16, |
| 1677 | .ops = &s3c24xx_serial_ops, |
| 1678 | .flags = UPF_BOOT_AUTOCONF, |
| 1679 | .line = 2, |
| 1680 | } |
Ben Dooks | 03d5e77 | 2008-11-03 09:21:23 +0000 | [diff] [blame] | 1681 | }, |
| 1682 | #endif |
| 1683 | #if CONFIG_SERIAL_SAMSUNG_UARTS > 3 |
| 1684 | [3] = { |
| 1685 | .port = { |
Robert Baldyga | ef4aca7 | 2014-11-24 07:56:22 +0100 | [diff] [blame] | 1686 | .lock = __PORT_LOCK_UNLOCKED(3), |
Ben Dooks | 03d5e77 | 2008-11-03 09:21:23 +0000 | [diff] [blame] | 1687 | .iotype = UPIO_MEM, |
Ben Dooks | 03d5e77 | 2008-11-03 09:21:23 +0000 | [diff] [blame] | 1688 | .uartclk = 0, |
| 1689 | .fifosize = 16, |
| 1690 | .ops = &s3c24xx_serial_ops, |
| 1691 | .flags = UPF_BOOT_AUTOCONF, |
| 1692 | .line = 3, |
| 1693 | } |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 1694 | } |
| 1695 | #endif |
| 1696 | }; |
Robert Baldyga | ef4aca7 | 2014-11-24 07:56:22 +0100 | [diff] [blame] | 1697 | #undef __PORT_LOCK_UNLOCKED |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 1698 | |
| 1699 | /* s3c24xx_serial_resetport |
| 1700 | * |
Thomas Abraham | 0dfb3b4 | 2011-10-24 11:48:21 +0200 | [diff] [blame] | 1701 | * reset the fifos and other the settings. |
Greg Kroah-Hartman | 7c17525 | 2019-12-10 15:37:05 +0100 | [diff] [blame] | 1702 | */ |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 1703 | |
Thomas Abraham | 0dfb3b4 | 2011-10-24 11:48:21 +0200 | [diff] [blame] | 1704 | static void s3c24xx_serial_resetport(struct uart_port *port, |
| 1705 | struct s3c2410_uartcfg *cfg) |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 1706 | { |
| 1707 | struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port); |
Thomas Abraham | 0dfb3b4 | 2011-10-24 11:48:21 +0200 | [diff] [blame] | 1708 | unsigned long ucon = rd_regl(port, S3C2410_UCON); |
| 1709 | unsigned int ucon_mask; |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 1710 | |
Thomas Abraham | 0dfb3b4 | 2011-10-24 11:48:21 +0200 | [diff] [blame] | 1711 | ucon_mask = info->clksel_mask; |
| 1712 | if (info->type == PORT_S3C2440) |
| 1713 | ucon_mask |= S3C2440_UCON0_DIVMASK; |
| 1714 | |
| 1715 | ucon &= ucon_mask; |
| 1716 | wr_regl(port, S3C2410_UCON, ucon | cfg->ucon); |
| 1717 | |
| 1718 | /* reset both fifos */ |
| 1719 | wr_regl(port, S3C2410_UFCON, cfg->ufcon | S3C2410_UFCON_RESETBOTH); |
| 1720 | wr_regl(port, S3C2410_UFCON, cfg->ufcon); |
| 1721 | |
| 1722 | /* some delay is required after fifo reset */ |
| 1723 | udelay(1); |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 1724 | } |
| 1725 | |
Krzysztof Kozlowski | ebaa81c | 2016-06-27 13:59:08 +0200 | [diff] [blame] | 1726 | #ifdef CONFIG_ARM_S3C24XX_CPUFREQ |
Ben Dooks | 3055547 | 2008-10-21 14:06:36 +0100 | [diff] [blame] | 1727 | |
| 1728 | static int s3c24xx_serial_cpufreq_transition(struct notifier_block *nb, |
| 1729 | unsigned long val, void *data) |
| 1730 | { |
| 1731 | struct s3c24xx_uart_port *port; |
| 1732 | struct uart_port *uport; |
| 1733 | |
| 1734 | port = container_of(nb, struct s3c24xx_uart_port, freq_transition); |
| 1735 | uport = &port->port; |
| 1736 | |
| 1737 | /* check to see if port is enabled */ |
| 1738 | |
| 1739 | if (port->pm_level != 0) |
| 1740 | return 0; |
| 1741 | |
| 1742 | /* try and work out if the baudrate is changing, we can detect |
| 1743 | * a change in rate, but we do not have support for detecting |
| 1744 | * a disturbance in the clock-rate over the change. |
| 1745 | */ |
| 1746 | |
Kyoungil Kim | 25f04ad | 2012-05-20 17:49:31 +0900 | [diff] [blame] | 1747 | if (IS_ERR(port->baudclk)) |
Ben Dooks | 3055547 | 2008-10-21 14:06:36 +0100 | [diff] [blame] | 1748 | goto exit; |
| 1749 | |
Kyoungil Kim | 25f04ad | 2012-05-20 17:49:31 +0900 | [diff] [blame] | 1750 | if (port->baudclk_rate == clk_get_rate(port->baudclk)) |
Ben Dooks | 3055547 | 2008-10-21 14:06:36 +0100 | [diff] [blame] | 1751 | goto exit; |
| 1752 | |
| 1753 | if (val == CPUFREQ_PRECHANGE) { |
| 1754 | /* we should really shut the port down whilst the |
Greg Kroah-Hartman | 7c17525 | 2019-12-10 15:37:05 +0100 | [diff] [blame] | 1755 | * frequency change is in progress. |
| 1756 | */ |
Ben Dooks | 3055547 | 2008-10-21 14:06:36 +0100 | [diff] [blame] | 1757 | |
| 1758 | } else if (val == CPUFREQ_POSTCHANGE) { |
| 1759 | struct ktermios *termios; |
| 1760 | struct tty_struct *tty; |
| 1761 | |
Alan Cox | ebd2c8f | 2009-09-19 13:13:28 -0700 | [diff] [blame] | 1762 | if (uport->state == NULL) |
Ben Dooks | 3055547 | 2008-10-21 14:06:36 +0100 | [diff] [blame] | 1763 | goto exit; |
Ben Dooks | 3055547 | 2008-10-21 14:06:36 +0100 | [diff] [blame] | 1764 | |
Alan Cox | ebd2c8f | 2009-09-19 13:13:28 -0700 | [diff] [blame] | 1765 | tty = uport->state->port.tty; |
Ben Dooks | 3055547 | 2008-10-21 14:06:36 +0100 | [diff] [blame] | 1766 | |
Ben Dooks | 7de40c2 | 2008-12-14 23:11:02 +0000 | [diff] [blame] | 1767 | if (tty == NULL) |
Ben Dooks | 3055547 | 2008-10-21 14:06:36 +0100 | [diff] [blame] | 1768 | goto exit; |
Ben Dooks | 3055547 | 2008-10-21 14:06:36 +0100 | [diff] [blame] | 1769 | |
Alan Cox | adc8d74 | 2012-07-14 15:31:47 +0100 | [diff] [blame] | 1770 | termios = &tty->termios; |
Ben Dooks | 3055547 | 2008-10-21 14:06:36 +0100 | [diff] [blame] | 1771 | |
| 1772 | if (termios == NULL) { |
Sachin Kamat | d20925e | 2012-09-05 10:30:10 +0530 | [diff] [blame] | 1773 | dev_warn(uport->dev, "%s: no termios?\n", __func__); |
Ben Dooks | 3055547 | 2008-10-21 14:06:36 +0100 | [diff] [blame] | 1774 | goto exit; |
| 1775 | } |
| 1776 | |
| 1777 | s3c24xx_serial_set_termios(uport, termios, NULL); |
| 1778 | } |
| 1779 | |
Robert Baldyga | ef4aca7 | 2014-11-24 07:56:22 +0100 | [diff] [blame] | 1780 | exit: |
Ben Dooks | 3055547 | 2008-10-21 14:06:36 +0100 | [diff] [blame] | 1781 | return 0; |
| 1782 | } |
| 1783 | |
Robert Baldyga | ef4aca7 | 2014-11-24 07:56:22 +0100 | [diff] [blame] | 1784 | static inline int |
| 1785 | s3c24xx_serial_cpufreq_register(struct s3c24xx_uart_port *port) |
Ben Dooks | 3055547 | 2008-10-21 14:06:36 +0100 | [diff] [blame] | 1786 | { |
| 1787 | port->freq_transition.notifier_call = s3c24xx_serial_cpufreq_transition; |
| 1788 | |
| 1789 | return cpufreq_register_notifier(&port->freq_transition, |
| 1790 | CPUFREQ_TRANSITION_NOTIFIER); |
| 1791 | } |
| 1792 | |
Robert Baldyga | ef4aca7 | 2014-11-24 07:56:22 +0100 | [diff] [blame] | 1793 | static inline void |
| 1794 | s3c24xx_serial_cpufreq_deregister(struct s3c24xx_uart_port *port) |
Ben Dooks | 3055547 | 2008-10-21 14:06:36 +0100 | [diff] [blame] | 1795 | { |
| 1796 | cpufreq_unregister_notifier(&port->freq_transition, |
| 1797 | CPUFREQ_TRANSITION_NOTIFIER); |
| 1798 | } |
| 1799 | |
| 1800 | #else |
Robert Baldyga | ef4aca7 | 2014-11-24 07:56:22 +0100 | [diff] [blame] | 1801 | static inline int |
| 1802 | s3c24xx_serial_cpufreq_register(struct s3c24xx_uart_port *port) |
Ben Dooks | 3055547 | 2008-10-21 14:06:36 +0100 | [diff] [blame] | 1803 | { |
| 1804 | return 0; |
| 1805 | } |
| 1806 | |
Robert Baldyga | ef4aca7 | 2014-11-24 07:56:22 +0100 | [diff] [blame] | 1807 | static inline void |
| 1808 | s3c24xx_serial_cpufreq_deregister(struct s3c24xx_uart_port *port) |
Ben Dooks | 3055547 | 2008-10-21 14:06:36 +0100 | [diff] [blame] | 1809 | { |
| 1810 | } |
| 1811 | #endif |
| 1812 | |
Stuart Menefy | 5086e0a | 2019-02-12 21:40:22 +0000 | [diff] [blame] | 1813 | static int s3c24xx_serial_enable_baudclk(struct s3c24xx_uart_port *ourport) |
| 1814 | { |
| 1815 | struct device *dev = ourport->port.dev; |
| 1816 | struct s3c24xx_uart_info *info = ourport->info; |
| 1817 | char clk_name[MAX_CLK_NAME_LENGTH]; |
| 1818 | unsigned int clk_sel; |
| 1819 | struct clk *clk; |
| 1820 | int clk_num; |
| 1821 | int ret; |
| 1822 | |
| 1823 | clk_sel = ourport->cfg->clk_sel ? : info->def_clk_sel; |
| 1824 | for (clk_num = 0; clk_num < info->num_clks; clk_num++) { |
| 1825 | if (!(clk_sel & (1 << clk_num))) |
| 1826 | continue; |
| 1827 | |
| 1828 | sprintf(clk_name, "clk_uart_baud%d", clk_num); |
| 1829 | clk = clk_get(dev, clk_name); |
| 1830 | if (IS_ERR(clk)) |
| 1831 | continue; |
| 1832 | |
| 1833 | ret = clk_prepare_enable(clk); |
| 1834 | if (ret) { |
| 1835 | clk_put(clk); |
| 1836 | continue; |
| 1837 | } |
| 1838 | |
| 1839 | ourport->baudclk = clk; |
| 1840 | ourport->baudclk_rate = clk_get_rate(clk); |
| 1841 | s3c24xx_serial_setsource(&ourport->port, clk_num); |
| 1842 | |
| 1843 | return 0; |
| 1844 | } |
| 1845 | |
| 1846 | return -EINVAL; |
| 1847 | } |
| 1848 | |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 1849 | /* s3c24xx_serial_init_port |
| 1850 | * |
| 1851 | * initialise a single serial port from the platform device given |
| 1852 | */ |
| 1853 | |
| 1854 | static int s3c24xx_serial_init_port(struct s3c24xx_uart_port *ourport, |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 1855 | struct platform_device *platdev) |
| 1856 | { |
| 1857 | struct uart_port *port = &ourport->port; |
Thomas Abraham | da12150 | 2011-11-02 19:23:25 +0900 | [diff] [blame] | 1858 | struct s3c2410_uartcfg *cfg = ourport->cfg; |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 1859 | struct resource *res; |
| 1860 | int ret; |
| 1861 | |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 1862 | if (platdev == NULL) |
| 1863 | return -ENODEV; |
| 1864 | |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 1865 | if (port->mapbase != 0) |
Krzysztof Kozlowski | e51e4d8 | 2016-06-16 08:27:35 +0200 | [diff] [blame] | 1866 | return -EINVAL; |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 1867 | |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 1868 | /* setup info for port */ |
| 1869 | port->dev = &platdev->dev; |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 1870 | |
Thomas Abraham | 88bb4ea | 2011-08-10 15:51:19 +0530 | [diff] [blame] | 1871 | /* Startup sequence is different for s3c64xx and higher SoC's */ |
| 1872 | if (s3c24xx_serial_has_interrupt_mask(port)) |
| 1873 | s3c24xx_serial_ops.startup = s3c64xx_serial_startup; |
| 1874 | |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 1875 | port->uartclk = 1; |
| 1876 | |
| 1877 | if (cfg->uart_flags & UPF_CONS_FLOW) { |
Greg Kroah-Hartman | a05025d | 2019-12-10 15:37:03 +0100 | [diff] [blame] | 1878 | dev_dbg(port->dev, "enabling flow control\n"); |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 1879 | port->flags |= UPF_CONS_FLOW; |
| 1880 | } |
| 1881 | |
| 1882 | /* sort our the physical and virtual addresses for each UART */ |
| 1883 | |
| 1884 | res = platform_get_resource(platdev, IORESOURCE_MEM, 0); |
| 1885 | if (res == NULL) { |
Sachin Kamat | d20925e | 2012-09-05 10:30:10 +0530 | [diff] [blame] | 1886 | dev_err(port->dev, "failed to find memory resource for uart\n"); |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 1887 | return -EINVAL; |
| 1888 | } |
| 1889 | |
Greg Kroah-Hartman | a05025d | 2019-12-10 15:37:03 +0100 | [diff] [blame] | 1890 | dev_dbg(port->dev, "resource %pR)\n", res); |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 1891 | |
Thomas Abraham | 41147bf | 2013-01-01 00:21:55 -0800 | [diff] [blame] | 1892 | port->membase = devm_ioremap(port->dev, res->start, resource_size(res)); |
| 1893 | if (!port->membase) { |
| 1894 | dev_err(port->dev, "failed to remap controller address\n"); |
| 1895 | return -EBUSY; |
| 1896 | } |
| 1897 | |
Ben Dooks | b690ace | 2008-10-21 14:07:03 +0100 | [diff] [blame] | 1898 | port->mapbase = res->start; |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 1899 | ret = platform_get_irq(platdev, 0); |
Krzysztof Kozlowski | 695b847 | 2020-06-17 17:28:56 +0200 | [diff] [blame] | 1900 | if (ret < 0) { |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 1901 | port->irq = 0; |
Krzysztof Kozlowski | 695b847 | 2020-06-17 17:28:56 +0200 | [diff] [blame] | 1902 | } else { |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 1903 | port->irq = ret; |
Ben Dooks | b73c289c | 2008-10-21 14:07:04 +0100 | [diff] [blame] | 1904 | ourport->rx_irq = ret; |
| 1905 | ourport->tx_irq = ret + 1; |
| 1906 | } |
Sachin Kamat | 9303ac1 | 2012-09-05 10:30:11 +0530 | [diff] [blame] | 1907 | |
Ben Dooks | b73c289c | 2008-10-21 14:07:04 +0100 | [diff] [blame] | 1908 | ret = platform_get_irq(platdev, 1); |
| 1909 | if (ret > 0) |
| 1910 | ourport->tx_irq = ret; |
Robert Baldyga | 658c9d2b7 | 2014-12-10 12:49:23 +0100 | [diff] [blame] | 1911 | /* |
| 1912 | * DMA is currently supported only on DT platforms, if DMA properties |
| 1913 | * are specified. |
| 1914 | */ |
| 1915 | if (platdev->dev.of_node && of_find_property(platdev->dev.of_node, |
| 1916 | "dmas", NULL)) { |
| 1917 | ourport->dma = devm_kzalloc(port->dev, |
| 1918 | sizeof(*ourport->dma), |
| 1919 | GFP_KERNEL); |
Krzysztof Kozlowski | e51e4d8 | 2016-06-16 08:27:35 +0200 | [diff] [blame] | 1920 | if (!ourport->dma) { |
| 1921 | ret = -ENOMEM; |
| 1922 | goto err; |
| 1923 | } |
Robert Baldyga | 658c9d2b7 | 2014-12-10 12:49:23 +0100 | [diff] [blame] | 1924 | } |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 1925 | |
| 1926 | ourport->clk = clk_get(&platdev->dev, "uart"); |
Chander Kashyap | 60e9357 | 2013-05-28 18:32:07 +0530 | [diff] [blame] | 1927 | if (IS_ERR(ourport->clk)) { |
| 1928 | pr_err("%s: Controller clock not found\n", |
| 1929 | dev_name(&platdev->dev)); |
Krzysztof Kozlowski | e51e4d8 | 2016-06-16 08:27:35 +0200 | [diff] [blame] | 1930 | ret = PTR_ERR(ourport->clk); |
| 1931 | goto err; |
Chander Kashyap | 60e9357 | 2013-05-28 18:32:07 +0530 | [diff] [blame] | 1932 | } |
| 1933 | |
| 1934 | ret = clk_prepare_enable(ourport->clk); |
| 1935 | if (ret) { |
| 1936 | pr_err("uart: clock failed to prepare+enable: %d\n", ret); |
| 1937 | clk_put(ourport->clk); |
Krzysztof Kozlowski | e51e4d8 | 2016-06-16 08:27:35 +0200 | [diff] [blame] | 1938 | goto err; |
Chander Kashyap | 60e9357 | 2013-05-28 18:32:07 +0530 | [diff] [blame] | 1939 | } |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 1940 | |
Stuart Menefy | 5086e0a | 2019-02-12 21:40:22 +0000 | [diff] [blame] | 1941 | ret = s3c24xx_serial_enable_baudclk(ourport); |
| 1942 | if (ret) |
| 1943 | pr_warn("uart: failed to enable baudclk\n"); |
| 1944 | |
Thomas Abraham | 88bb4ea | 2011-08-10 15:51:19 +0530 | [diff] [blame] | 1945 | /* Keep all interrupts masked and cleared */ |
| 1946 | if (s3c24xx_serial_has_interrupt_mask(port)) { |
| 1947 | wr_regl(port, S3C64XX_UINTM, 0xf); |
| 1948 | wr_regl(port, S3C64XX_UINTP, 0xf); |
| 1949 | wr_regl(port, S3C64XX_UINTSP, 0xf); |
| 1950 | } |
| 1951 | |
Greg Kroah-Hartman | a05025d | 2019-12-10 15:37:03 +0100 | [diff] [blame] | 1952 | dev_dbg(port->dev, "port: map=%pa, mem=%p, irq=%d (%d,%d), clock=%u\n", |
| 1953 | &port->mapbase, port->membase, port->irq, |
| 1954 | ourport->rx_irq, ourport->tx_irq, port->uartclk); |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 1955 | |
| 1956 | /* reset the fifos (and setup the uart) */ |
| 1957 | s3c24xx_serial_resetport(port, cfg); |
Krzysztof Kozlowski | e51e4d8 | 2016-06-16 08:27:35 +0200 | [diff] [blame] | 1958 | |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 1959 | return 0; |
Krzysztof Kozlowski | e51e4d8 | 2016-06-16 08:27:35 +0200 | [diff] [blame] | 1960 | |
| 1961 | err: |
| 1962 | port->mapbase = 0; |
| 1963 | return ret; |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 1964 | } |
| 1965 | |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 1966 | /* Device driver serial port probe */ |
| 1967 | |
Greg Kroah-Hartman | 06674e5 | 2019-12-10 15:36:58 +0100 | [diff] [blame] | 1968 | #ifdef CONFIG_OF |
Thomas Abraham | 26c919e | 2011-11-06 22:10:44 +0530 | [diff] [blame] | 1969 | static const struct of_device_id s3c24xx_uart_dt_match[]; |
Greg Kroah-Hartman | 06674e5 | 2019-12-10 15:36:58 +0100 | [diff] [blame] | 1970 | #endif |
| 1971 | |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 1972 | static int probe_index; |
| 1973 | |
Krzysztof Kozlowski | 695b847 | 2020-06-17 17:28:56 +0200 | [diff] [blame] | 1974 | static inline struct s3c24xx_serial_drv_data * |
| 1975 | s3c24xx_get_driver_data(struct platform_device *pdev) |
Thomas Abraham | 26c919e | 2011-11-06 22:10:44 +0530 | [diff] [blame] | 1976 | { |
| 1977 | #ifdef CONFIG_OF |
| 1978 | if (pdev->dev.of_node) { |
| 1979 | const struct of_device_id *match; |
Greg Kroah-Hartman | 9fe0d41 | 2019-12-10 15:37:06 +0100 | [diff] [blame] | 1980 | |
Thomas Abraham | 26c919e | 2011-11-06 22:10:44 +0530 | [diff] [blame] | 1981 | match = of_match_node(s3c24xx_uart_dt_match, pdev->dev.of_node); |
| 1982 | return (struct s3c24xx_serial_drv_data *)match->data; |
| 1983 | } |
| 1984 | #endif |
| 1985 | return (struct s3c24xx_serial_drv_data *) |
| 1986 | platform_get_device_id(pdev)->driver_data; |
| 1987 | } |
| 1988 | |
Thomas Abraham | da12150 | 2011-11-02 19:23:25 +0900 | [diff] [blame] | 1989 | static int s3c24xx_serial_probe(struct platform_device *pdev) |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 1990 | { |
Naveen Krishna Chatradhi | 4622eb6 | 2014-07-14 17:07:18 +0530 | [diff] [blame] | 1991 | struct device_node *np = pdev->dev.of_node; |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 1992 | struct s3c24xx_uart_port *ourport; |
Tomasz Figa | 13a9f6c6 | 2014-06-26 13:24:34 +0200 | [diff] [blame] | 1993 | int index = probe_index; |
Hyunki Koo | 57253cc | 2020-05-06 17:02:40 +0900 | [diff] [blame] | 1994 | int ret, prop = 0; |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 1995 | |
Naveen Krishna Chatradhi | 4622eb6 | 2014-07-14 17:07:18 +0530 | [diff] [blame] | 1996 | if (np) { |
| 1997 | ret = of_alias_get_id(np, "serial"); |
Tomasz Figa | 13a9f6c6 | 2014-06-26 13:24:34 +0200 | [diff] [blame] | 1998 | if (ret >= 0) |
| 1999 | index = ret; |
| 2000 | } |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 2001 | |
Geert Uytterhoeven | 49ee23b | 2018-02-23 14:38:34 +0100 | [diff] [blame] | 2002 | if (index >= ARRAY_SIZE(s3c24xx_serial_ports)) { |
| 2003 | dev_err(&pdev->dev, "serial%d out of range\n", index); |
| 2004 | return -EINVAL; |
| 2005 | } |
Tomasz Figa | 13a9f6c6 | 2014-06-26 13:24:34 +0200 | [diff] [blame] | 2006 | ourport = &s3c24xx_serial_ports[index]; |
Thomas Abraham | da12150 | 2011-11-02 19:23:25 +0900 | [diff] [blame] | 2007 | |
Thomas Abraham | 26c919e | 2011-11-06 22:10:44 +0530 | [diff] [blame] | 2008 | ourport->drv_data = s3c24xx_get_driver_data(pdev); |
| 2009 | if (!ourport->drv_data) { |
| 2010 | dev_err(&pdev->dev, "could not find driver data\n"); |
| 2011 | return -ENODEV; |
| 2012 | } |
Thomas Abraham | da12150 | 2011-11-02 19:23:25 +0900 | [diff] [blame] | 2013 | |
Kyoungil Kim | 7cd8883 | 2012-05-20 17:45:54 +0900 | [diff] [blame] | 2014 | ourport->baudclk = ERR_PTR(-EINVAL); |
Thomas Abraham | da12150 | 2011-11-02 19:23:25 +0900 | [diff] [blame] | 2015 | ourport->info = ourport->drv_data->info; |
Jingoo Han | 574de55 | 2013-07-30 17:06:57 +0900 | [diff] [blame] | 2016 | ourport->cfg = (dev_get_platdata(&pdev->dev)) ? |
Jingoo Han | d4aab20 | 2013-09-09 14:10:30 +0900 | [diff] [blame] | 2017 | dev_get_platdata(&pdev->dev) : |
Thomas Abraham | da12150 | 2011-11-02 19:23:25 +0900 | [diff] [blame] | 2018 | ourport->drv_data->def_cfg; |
| 2019 | |
Hyunki Koo | 57253cc | 2020-05-06 17:02:40 +0900 | [diff] [blame] | 2020 | if (np) { |
Naveen Krishna Chatradhi | 4622eb6 | 2014-07-14 17:07:18 +0530 | [diff] [blame] | 2021 | of_property_read_u32(np, |
Naveen Krishna Chatradhi | 135f07c | 2014-07-14 17:07:16 +0530 | [diff] [blame] | 2022 | "samsung,uart-fifosize", &ourport->port.fifosize); |
| 2023 | |
Hyunki Koo | 57253cc | 2020-05-06 17:02:40 +0900 | [diff] [blame] | 2024 | if (of_property_read_u32(np, "reg-io-width", &prop) == 0) { |
| 2025 | switch (prop) { |
| 2026 | case 1: |
| 2027 | ourport->port.iotype = UPIO_MEM; |
| 2028 | break; |
| 2029 | case 4: |
| 2030 | ourport->port.iotype = UPIO_MEM32; |
| 2031 | break; |
| 2032 | default: |
| 2033 | dev_warn(&pdev->dev, "unsupported reg-io-width (%d)\n", |
| 2034 | prop); |
| 2035 | ret = -EINVAL; |
| 2036 | break; |
| 2037 | } |
| 2038 | } |
| 2039 | } |
| 2040 | |
Robert Baldyga | 2f1ba72 | 2014-11-24 07:56:23 +0100 | [diff] [blame] | 2041 | if (ourport->drv_data->fifosize[index]) |
| 2042 | ourport->port.fifosize = ourport->drv_data->fifosize[index]; |
| 2043 | else if (ourport->info->fifosize) |
| 2044 | ourport->port.fifosize = ourport->info->fifosize; |
Dmitry Safonov | 831cb96 | 2019-12-13 00:06:36 +0000 | [diff] [blame] | 2045 | ourport->port.has_sysrq = IS_ENABLED(CONFIG_SERIAL_SAMSUNG_CONSOLE); |
Thomas Abraham | da12150 | 2011-11-02 19:23:25 +0900 | [diff] [blame] | 2046 | |
Marek Szyprowski | 81ccb2a | 2015-07-31 10:58:27 +0200 | [diff] [blame] | 2047 | /* |
| 2048 | * DMA transfers must be aligned at least to cache line size, |
| 2049 | * so find minimal transfer size suitable for DMA mode |
| 2050 | */ |
| 2051 | ourport->min_dma_size = max_t(int, ourport->port.fifosize, |
| 2052 | dma_get_cache_alignment()); |
| 2053 | |
Greg Kroah-Hartman | a05025d | 2019-12-10 15:37:03 +0100 | [diff] [blame] | 2054 | dev_dbg(&pdev->dev, "%s: initialising port %p...\n", __func__, ourport); |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 2055 | |
Thomas Abraham | da12150 | 2011-11-02 19:23:25 +0900 | [diff] [blame] | 2056 | ret = s3c24xx_serial_init_port(ourport, pdev); |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 2057 | if (ret < 0) |
Tushar Behera | 8ad711a | 2014-06-23 11:32:14 +0530 | [diff] [blame] | 2058 | return ret; |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 2059 | |
Tushar Behera | 6f134c3c | 2014-01-20 14:32:34 +0530 | [diff] [blame] | 2060 | if (!s3c24xx_uart_drv.state) { |
| 2061 | ret = uart_register_driver(&s3c24xx_uart_drv); |
| 2062 | if (ret < 0) { |
| 2063 | pr_err("Failed to register Samsung UART driver\n"); |
| 2064 | return ret; |
| 2065 | } |
| 2066 | } |
| 2067 | |
Greg Kroah-Hartman | a05025d | 2019-12-10 15:37:03 +0100 | [diff] [blame] | 2068 | dev_dbg(&pdev->dev, "%s: adding port\n", __func__); |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 2069 | uart_add_one_port(&s3c24xx_uart_drv, &ourport->port); |
Thomas Abraham | da12150 | 2011-11-02 19:23:25 +0900 | [diff] [blame] | 2070 | platform_set_drvdata(pdev, &ourport->port); |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 2071 | |
Heiko Stübner | 0da3336 | 2013-12-05 00:54:38 +0100 | [diff] [blame] | 2072 | /* |
| 2073 | * Deactivate the clock enabled in s3c24xx_serial_init_port here, |
| 2074 | * so that a potential re-enablement through the pm-callback overlaps |
| 2075 | * and keeps the clock enabled in this case. |
| 2076 | */ |
| 2077 | clk_disable_unprepare(ourport->clk); |
Stuart Menefy | 5086e0a | 2019-02-12 21:40:22 +0000 | [diff] [blame] | 2078 | if (!IS_ERR(ourport->baudclk)) |
| 2079 | clk_disable_unprepare(ourport->baudclk); |
Heiko Stübner | 0da3336 | 2013-12-05 00:54:38 +0100 | [diff] [blame] | 2080 | |
Ben Dooks | 3055547 | 2008-10-21 14:06:36 +0100 | [diff] [blame] | 2081 | ret = s3c24xx_serial_cpufreq_register(ourport); |
| 2082 | if (ret < 0) |
Thomas Abraham | da12150 | 2011-11-02 19:23:25 +0900 | [diff] [blame] | 2083 | dev_err(&pdev->dev, "failed to add cpufreq notifier\n"); |
Ben Dooks | 3055547 | 2008-10-21 14:06:36 +0100 | [diff] [blame] | 2084 | |
Krzysztof Kozlowski | 926b7b5 | 2016-06-16 08:27:36 +0200 | [diff] [blame] | 2085 | probe_index++; |
| 2086 | |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 2087 | return 0; |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 2088 | } |
| 2089 | |
Bill Pemberton | ae8d8a1 | 2012-11-19 13:26:18 -0500 | [diff] [blame] | 2090 | static int s3c24xx_serial_remove(struct platform_device *dev) |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 2091 | { |
| 2092 | struct uart_port *port = s3c24xx_dev_to_port(&dev->dev); |
| 2093 | |
| 2094 | if (port) { |
Ben Dooks | 3055547 | 2008-10-21 14:06:36 +0100 | [diff] [blame] | 2095 | s3c24xx_serial_cpufreq_deregister(to_ourport(port)); |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 2096 | uart_remove_one_port(&s3c24xx_uart_drv, port); |
| 2097 | } |
| 2098 | |
Tushar Behera | 6f134c3c | 2014-01-20 14:32:34 +0530 | [diff] [blame] | 2099 | uart_unregister_driver(&s3c24xx_uart_drv); |
| 2100 | |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 2101 | return 0; |
| 2102 | } |
| 2103 | |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 2104 | /* UART power management code */ |
MyungJoo Ham | aef7fe5 | 2011-06-29 15:28:24 +0900 | [diff] [blame] | 2105 | #ifdef CONFIG_PM_SLEEP |
| 2106 | static int s3c24xx_serial_suspend(struct device *dev) |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 2107 | { |
MyungJoo Ham | aef7fe5 | 2011-06-29 15:28:24 +0900 | [diff] [blame] | 2108 | struct uart_port *port = s3c24xx_dev_to_port(dev); |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 2109 | |
| 2110 | if (port) |
| 2111 | uart_suspend_port(&s3c24xx_uart_drv, port); |
| 2112 | |
| 2113 | return 0; |
| 2114 | } |
| 2115 | |
MyungJoo Ham | aef7fe5 | 2011-06-29 15:28:24 +0900 | [diff] [blame] | 2116 | static int s3c24xx_serial_resume(struct device *dev) |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 2117 | { |
MyungJoo Ham | aef7fe5 | 2011-06-29 15:28:24 +0900 | [diff] [blame] | 2118 | struct uart_port *port = s3c24xx_dev_to_port(dev); |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 2119 | struct s3c24xx_uart_port *ourport = to_ourport(port); |
| 2120 | |
| 2121 | if (port) { |
Thomas Abraham | 9484b00 | 2012-10-03 07:40:04 +0900 | [diff] [blame] | 2122 | clk_prepare_enable(ourport->clk); |
Marek Szyprowski | 1ff3652 | 2018-09-13 10:21:25 +0200 | [diff] [blame] | 2123 | if (!IS_ERR(ourport->baudclk)) |
| 2124 | clk_prepare_enable(ourport->baudclk); |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 2125 | s3c24xx_serial_resetport(port, s3c24xx_port_to_cfg(port)); |
Marek Szyprowski | 1ff3652 | 2018-09-13 10:21:25 +0200 | [diff] [blame] | 2126 | if (!IS_ERR(ourport->baudclk)) |
| 2127 | clk_disable_unprepare(ourport->baudclk); |
Thomas Abraham | 9484b00 | 2012-10-03 07:40:04 +0900 | [diff] [blame] | 2128 | clk_disable_unprepare(ourport->clk); |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 2129 | |
| 2130 | uart_resume_port(&s3c24xx_uart_drv, port); |
| 2131 | } |
| 2132 | |
| 2133 | return 0; |
| 2134 | } |
MyungJoo Ham | aef7fe5 | 2011-06-29 15:28:24 +0900 | [diff] [blame] | 2135 | |
Michael Spang | d09a730 | 2013-03-27 19:34:24 -0400 | [diff] [blame] | 2136 | static int s3c24xx_serial_resume_noirq(struct device *dev) |
| 2137 | { |
| 2138 | struct uart_port *port = s3c24xx_dev_to_port(dev); |
남영민 | a8a1781 | 2017-02-01 19:25:46 +0900 | [diff] [blame] | 2139 | struct s3c24xx_uart_port *ourport = to_ourport(port); |
Michael Spang | d09a730 | 2013-03-27 19:34:24 -0400 | [diff] [blame] | 2140 | |
| 2141 | if (port) { |
| 2142 | /* restore IRQ mask */ |
| 2143 | if (s3c24xx_serial_has_interrupt_mask(port)) { |
| 2144 | unsigned int uintm = 0xf; |
Greg Kroah-Hartman | 9fe0d41 | 2019-12-10 15:37:06 +0100 | [diff] [blame] | 2145 | |
Greg Kroah-Hartman | 8336240 | 2019-12-17 15:02:32 +0100 | [diff] [blame] | 2146 | if (ourport->tx_enabled) |
Michael Spang | d09a730 | 2013-03-27 19:34:24 -0400 | [diff] [blame] | 2147 | uintm &= ~S3C64XX_UINTM_TXD_MSK; |
Greg Kroah-Hartman | 8336240 | 2019-12-17 15:02:32 +0100 | [diff] [blame] | 2148 | if (ourport->rx_enabled) |
Michael Spang | d09a730 | 2013-03-27 19:34:24 -0400 | [diff] [blame] | 2149 | uintm &= ~S3C64XX_UINTM_RXD_MSK; |
남영민 | a8a1781 | 2017-02-01 19:25:46 +0900 | [diff] [blame] | 2150 | clk_prepare_enable(ourport->clk); |
Marek Szyprowski | 1ff3652 | 2018-09-13 10:21:25 +0200 | [diff] [blame] | 2151 | if (!IS_ERR(ourport->baudclk)) |
| 2152 | clk_prepare_enable(ourport->baudclk); |
Michael Spang | d09a730 | 2013-03-27 19:34:24 -0400 | [diff] [blame] | 2153 | wr_regl(port, S3C64XX_UINTM, uintm); |
Marek Szyprowski | 1ff3652 | 2018-09-13 10:21:25 +0200 | [diff] [blame] | 2154 | if (!IS_ERR(ourport->baudclk)) |
| 2155 | clk_disable_unprepare(ourport->baudclk); |
남영민 | a8a1781 | 2017-02-01 19:25:46 +0900 | [diff] [blame] | 2156 | clk_disable_unprepare(ourport->clk); |
Michael Spang | d09a730 | 2013-03-27 19:34:24 -0400 | [diff] [blame] | 2157 | } |
| 2158 | } |
| 2159 | |
| 2160 | return 0; |
| 2161 | } |
| 2162 | |
MyungJoo Ham | aef7fe5 | 2011-06-29 15:28:24 +0900 | [diff] [blame] | 2163 | static const struct dev_pm_ops s3c24xx_serial_pm_ops = { |
| 2164 | .suspend = s3c24xx_serial_suspend, |
| 2165 | .resume = s3c24xx_serial_resume, |
Michael Spang | d09a730 | 2013-03-27 19:34:24 -0400 | [diff] [blame] | 2166 | .resume_noirq = s3c24xx_serial_resume_noirq, |
MyungJoo Ham | aef7fe5 | 2011-06-29 15:28:24 +0900 | [diff] [blame] | 2167 | }; |
Kukjin Kim | b882fc1 | 2011-07-28 08:50:38 +0900 | [diff] [blame] | 2168 | #define SERIAL_SAMSUNG_PM_OPS (&s3c24xx_serial_pm_ops) |
| 2169 | |
MyungJoo Ham | aef7fe5 | 2011-06-29 15:28:24 +0900 | [diff] [blame] | 2170 | #else /* !CONFIG_PM_SLEEP */ |
Kukjin Kim | b882fc1 | 2011-07-28 08:50:38 +0900 | [diff] [blame] | 2171 | |
| 2172 | #define SERIAL_SAMSUNG_PM_OPS NULL |
MyungJoo Ham | aef7fe5 | 2011-06-29 15:28:24 +0900 | [diff] [blame] | 2173 | #endif /* CONFIG_PM_SLEEP */ |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 2174 | |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 2175 | /* Console code */ |
| 2176 | |
| 2177 | #ifdef CONFIG_SERIAL_SAMSUNG_CONSOLE |
| 2178 | |
| 2179 | static struct uart_port *cons_uart; |
| 2180 | |
| 2181 | static int |
| 2182 | s3c24xx_serial_console_txrdy(struct uart_port *port, unsigned int ufcon) |
| 2183 | { |
| 2184 | struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port); |
| 2185 | unsigned long ufstat, utrstat; |
| 2186 | |
| 2187 | if (ufcon & S3C2410_UFCON_FIFOMODE) { |
Uwe Kleine-König | 9ddc5b6 | 2010-01-20 17:02:24 +0100 | [diff] [blame] | 2188 | /* fifo mode - check amount of data in fifo registers... */ |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 2189 | |
| 2190 | ufstat = rd_regl(port, S3C2410_UFSTAT); |
| 2191 | return (ufstat & info->tx_fifofull) ? 0 : 1; |
| 2192 | } |
| 2193 | |
| 2194 | /* in non-fifo mode, we go and use the tx buffer empty */ |
| 2195 | |
| 2196 | utrstat = rd_regl(port, S3C2410_UTRSTAT); |
| 2197 | return (utrstat & S3C2410_UTRSTAT_TXE) ? 1 : 0; |
| 2198 | } |
| 2199 | |
Michael Spang | 38adbc5 | 2013-03-27 19:34:25 -0400 | [diff] [blame] | 2200 | static bool |
| 2201 | s3c24xx_port_configured(unsigned int ucon) |
| 2202 | { |
| 2203 | /* consider the serial port configured if the tx/rx mode set */ |
| 2204 | return (ucon & 0xf) != 0; |
| 2205 | } |
| 2206 | |
Julien Pichon | 93b5c03 | 2012-09-21 23:22:31 -0700 | [diff] [blame] | 2207 | #ifdef CONFIG_CONSOLE_POLL |
| 2208 | /* |
| 2209 | * Console polling routines for writing and reading from the uart while |
| 2210 | * in an interrupt or debug context. |
| 2211 | */ |
| 2212 | |
| 2213 | static int s3c24xx_serial_get_poll_char(struct uart_port *port) |
| 2214 | { |
| 2215 | struct s3c24xx_uart_port *ourport = to_ourport(port); |
| 2216 | unsigned int ufstat; |
| 2217 | |
| 2218 | ufstat = rd_regl(port, S3C2410_UFSTAT); |
| 2219 | if (s3c24xx_serial_rx_fifocnt(ourport, ufstat) == 0) |
| 2220 | return NO_POLL_CHAR; |
| 2221 | |
Hyunki Koo | 8fba6c0 | 2020-05-06 17:02:38 +0900 | [diff] [blame] | 2222 | return rd_reg(port, S3C2410_URXH); |
Julien Pichon | 93b5c03 | 2012-09-21 23:22:31 -0700 | [diff] [blame] | 2223 | } |
| 2224 | |
| 2225 | static void s3c24xx_serial_put_poll_char(struct uart_port *port, |
| 2226 | unsigned char c) |
| 2227 | { |
Doug Anderson | bb7f09b | 2014-04-21 09:40:34 -0700 | [diff] [blame] | 2228 | unsigned int ufcon = rd_regl(port, S3C2410_UFCON); |
| 2229 | unsigned int ucon = rd_regl(port, S3C2410_UCON); |
Michael Spang | 38adbc5 | 2013-03-27 19:34:25 -0400 | [diff] [blame] | 2230 | |
| 2231 | /* not possible to xmit on unconfigured port */ |
| 2232 | if (!s3c24xx_port_configured(ucon)) |
| 2233 | return; |
Julien Pichon | 93b5c03 | 2012-09-21 23:22:31 -0700 | [diff] [blame] | 2234 | |
| 2235 | while (!s3c24xx_serial_console_txrdy(port, ufcon)) |
| 2236 | cpu_relax(); |
Hyunki Koo | 8fba6c0 | 2020-05-06 17:02:38 +0900 | [diff] [blame] | 2237 | wr_reg(port, S3C2410_UTXH, c); |
Julien Pichon | 93b5c03 | 2012-09-21 23:22:31 -0700 | [diff] [blame] | 2238 | } |
| 2239 | |
| 2240 | #endif /* CONFIG_CONSOLE_POLL */ |
| 2241 | |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 2242 | static void |
| 2243 | s3c24xx_serial_console_putchar(struct uart_port *port, int ch) |
| 2244 | { |
Doug Anderson | bb7f09b | 2014-04-21 09:40:34 -0700 | [diff] [blame] | 2245 | unsigned int ufcon = rd_regl(port, S3C2410_UFCON); |
Michael Spang | 38adbc5 | 2013-03-27 19:34:25 -0400 | [diff] [blame] | 2246 | |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 2247 | while (!s3c24xx_serial_console_txrdy(port, ufcon)) |
Doug Anderson | f94b057 | 2014-04-21 09:40:36 -0700 | [diff] [blame] | 2248 | cpu_relax(); |
Hyunki Koo | 8fba6c0 | 2020-05-06 17:02:38 +0900 | [diff] [blame] | 2249 | wr_reg(port, S3C2410_UTXH, ch); |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 2250 | } |
| 2251 | |
| 2252 | static void |
| 2253 | s3c24xx_serial_console_write(struct console *co, const char *s, |
| 2254 | unsigned int count) |
| 2255 | { |
Doug Anderson | ab88c8d | 2014-04-21 09:40:35 -0700 | [diff] [blame] | 2256 | unsigned int ucon = rd_regl(cons_uart, S3C2410_UCON); |
| 2257 | |
| 2258 | /* not possible to xmit on unconfigured port */ |
| 2259 | if (!s3c24xx_port_configured(ucon)) |
| 2260 | return; |
| 2261 | |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 2262 | uart_console_write(cons_uart, s, count, s3c24xx_serial_console_putchar); |
| 2263 | } |
| 2264 | |
| 2265 | static void __init |
| 2266 | s3c24xx_serial_get_options(struct uart_port *port, int *baud, |
| 2267 | int *parity, int *bits) |
| 2268 | { |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 2269 | struct clk *clk; |
| 2270 | unsigned int ulcon; |
| 2271 | unsigned int ucon; |
| 2272 | unsigned int ubrdiv; |
| 2273 | unsigned long rate; |
Thomas Abraham | 5f5a7a5 | 2011-10-24 11:47:46 +0200 | [diff] [blame] | 2274 | unsigned int clk_sel; |
| 2275 | char clk_name[MAX_CLK_NAME_LENGTH]; |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 2276 | |
| 2277 | ulcon = rd_regl(port, S3C2410_ULCON); |
| 2278 | ucon = rd_regl(port, S3C2410_UCON); |
| 2279 | ubrdiv = rd_regl(port, S3C2410_UBRDIV); |
| 2280 | |
Michael Spang | 38adbc5 | 2013-03-27 19:34:25 -0400 | [diff] [blame] | 2281 | if (s3c24xx_port_configured(ucon)) { |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 2282 | switch (ulcon & S3C2410_LCON_CSMASK) { |
| 2283 | case S3C2410_LCON_CS5: |
| 2284 | *bits = 5; |
| 2285 | break; |
| 2286 | case S3C2410_LCON_CS6: |
| 2287 | *bits = 6; |
| 2288 | break; |
| 2289 | case S3C2410_LCON_CS7: |
| 2290 | *bits = 7; |
| 2291 | break; |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 2292 | case S3C2410_LCON_CS8: |
Naveen Krishna Chatradhi | 3bcce59 | 2014-07-14 17:07:17 +0530 | [diff] [blame] | 2293 | default: |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 2294 | *bits = 8; |
| 2295 | break; |
| 2296 | } |
| 2297 | |
| 2298 | switch (ulcon & S3C2410_LCON_PMASK) { |
| 2299 | case S3C2410_LCON_PEVEN: |
| 2300 | *parity = 'e'; |
| 2301 | break; |
| 2302 | |
| 2303 | case S3C2410_LCON_PODD: |
| 2304 | *parity = 'o'; |
| 2305 | break; |
| 2306 | |
| 2307 | case S3C2410_LCON_PNONE: |
| 2308 | default: |
| 2309 | *parity = 'n'; |
| 2310 | } |
| 2311 | |
| 2312 | /* now calculate the baud rate */ |
| 2313 | |
Thomas Abraham | 5f5a7a5 | 2011-10-24 11:47:46 +0200 | [diff] [blame] | 2314 | clk_sel = s3c24xx_serial_getsource(port); |
| 2315 | sprintf(clk_name, "clk_uart_baud%d", clk_sel); |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 2316 | |
Thomas Abraham | 5f5a7a5 | 2011-10-24 11:47:46 +0200 | [diff] [blame] | 2317 | clk = clk_get(port->dev, clk_name); |
Kyoungil Kim | 7cd8883 | 2012-05-20 17:45:54 +0900 | [diff] [blame] | 2318 | if (!IS_ERR(clk)) |
Thomas Abraham | 5f5a7a5 | 2011-10-24 11:47:46 +0200 | [diff] [blame] | 2319 | rate = clk_get_rate(clk); |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 2320 | else |
| 2321 | rate = 1; |
| 2322 | |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 2323 | *baud = rate / (16 * (ubrdiv + 1)); |
Greg Kroah-Hartman | a05025d | 2019-12-10 15:37:03 +0100 | [diff] [blame] | 2324 | dev_dbg(port->dev, "calculated baud %d\n", *baud); |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 2325 | } |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 2326 | } |
| 2327 | |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 2328 | static int __init |
| 2329 | s3c24xx_serial_console_setup(struct console *co, char *options) |
| 2330 | { |
| 2331 | struct uart_port *port; |
| 2332 | int baud = 9600; |
| 2333 | int bits = 8; |
| 2334 | int parity = 'n'; |
| 2335 | int flow = 'n'; |
| 2336 | |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 2337 | /* is this a valid port */ |
| 2338 | |
Ben Dooks | 03d5e77 | 2008-11-03 09:21:23 +0000 | [diff] [blame] | 2339 | if (co->index == -1 || co->index >= CONFIG_SERIAL_SAMSUNG_UARTS) |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 2340 | co->index = 0; |
| 2341 | |
| 2342 | port = &s3c24xx_serial_ports[co->index].port; |
| 2343 | |
| 2344 | /* is the port configured? */ |
| 2345 | |
Thomas Abraham | ee430f1 | 2011-06-14 19:12:26 +0900 | [diff] [blame] | 2346 | if (port->mapbase == 0x0) |
| 2347 | return -ENODEV; |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 2348 | |
| 2349 | cons_uart = port; |
| 2350 | |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 2351 | /* |
| 2352 | * Check whether an invalid uart number has been specified, and |
| 2353 | * if so, search for the first available port that does have |
| 2354 | * console support. |
| 2355 | */ |
| 2356 | if (options) |
| 2357 | uart_parse_options(options, &baud, &parity, &bits, &flow); |
| 2358 | else |
| 2359 | s3c24xx_serial_get_options(port, &baud, &parity, &bits); |
| 2360 | |
Greg Kroah-Hartman | a05025d | 2019-12-10 15:37:03 +0100 | [diff] [blame] | 2361 | dev_dbg(port->dev, "baud %d\n", baud); |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 2362 | |
| 2363 | return uart_set_options(port, co, baud, parity, bits, flow); |
| 2364 | } |
| 2365 | |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 2366 | static struct console s3c24xx_serial_console = { |
| 2367 | .name = S3C24XX_SERIAL_NAME, |
| 2368 | .device = uart_console_device, |
| 2369 | .flags = CON_PRINTBUFFER, |
| 2370 | .index = -1, |
| 2371 | .write = s3c24xx_serial_console_write, |
Thomas Abraham | 5822a5d | 2011-06-14 19:12:26 +0900 | [diff] [blame] | 2372 | .setup = s3c24xx_serial_console_setup, |
| 2373 | .data = &s3c24xx_uart_drv, |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 2374 | }; |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 2375 | #endif /* CONFIG_SERIAL_SAMSUNG_CONSOLE */ |
| 2376 | |
Thomas Abraham | da12150 | 2011-11-02 19:23:25 +0900 | [diff] [blame] | 2377 | #ifdef CONFIG_CPU_S3C2410 |
| 2378 | static struct s3c24xx_serial_drv_data s3c2410_serial_drv_data = { |
| 2379 | .info = &(struct s3c24xx_uart_info) { |
| 2380 | .name = "Samsung S3C2410 UART", |
| 2381 | .type = PORT_S3C2410, |
| 2382 | .fifosize = 16, |
| 2383 | .rx_fifomask = S3C2410_UFSTAT_RXMASK, |
| 2384 | .rx_fifoshift = S3C2410_UFSTAT_RXSHIFT, |
| 2385 | .rx_fifofull = S3C2410_UFSTAT_RXFULL, |
| 2386 | .tx_fifofull = S3C2410_UFSTAT_TXFULL, |
| 2387 | .tx_fifomask = S3C2410_UFSTAT_TXMASK, |
| 2388 | .tx_fifoshift = S3C2410_UFSTAT_TXSHIFT, |
| 2389 | .def_clk_sel = S3C2410_UCON_CLKSEL0, |
| 2390 | .num_clks = 2, |
| 2391 | .clksel_mask = S3C2410_UCON_CLKMASK, |
| 2392 | .clksel_shift = S3C2410_UCON_CLKSHIFT, |
| 2393 | }, |
| 2394 | .def_cfg = &(struct s3c2410_uartcfg) { |
| 2395 | .ucon = S3C2410_UCON_DEFAULT, |
| 2396 | .ufcon = S3C2410_UFCON_DEFAULT, |
| 2397 | }, |
| 2398 | }; |
| 2399 | #define S3C2410_SERIAL_DRV_DATA ((kernel_ulong_t)&s3c2410_serial_drv_data) |
| 2400 | #else |
| 2401 | #define S3C2410_SERIAL_DRV_DATA (kernel_ulong_t)NULL |
| 2402 | #endif |
| 2403 | |
| 2404 | #ifdef CONFIG_CPU_S3C2412 |
| 2405 | static struct s3c24xx_serial_drv_data s3c2412_serial_drv_data = { |
| 2406 | .info = &(struct s3c24xx_uart_info) { |
| 2407 | .name = "Samsung S3C2412 UART", |
| 2408 | .type = PORT_S3C2412, |
| 2409 | .fifosize = 64, |
| 2410 | .has_divslot = 1, |
| 2411 | .rx_fifomask = S3C2440_UFSTAT_RXMASK, |
| 2412 | .rx_fifoshift = S3C2440_UFSTAT_RXSHIFT, |
| 2413 | .rx_fifofull = S3C2440_UFSTAT_RXFULL, |
| 2414 | .tx_fifofull = S3C2440_UFSTAT_TXFULL, |
| 2415 | .tx_fifomask = S3C2440_UFSTAT_TXMASK, |
| 2416 | .tx_fifoshift = S3C2440_UFSTAT_TXSHIFT, |
| 2417 | .def_clk_sel = S3C2410_UCON_CLKSEL2, |
| 2418 | .num_clks = 4, |
| 2419 | .clksel_mask = S3C2412_UCON_CLKMASK, |
| 2420 | .clksel_shift = S3C2412_UCON_CLKSHIFT, |
| 2421 | }, |
| 2422 | .def_cfg = &(struct s3c2410_uartcfg) { |
| 2423 | .ucon = S3C2410_UCON_DEFAULT, |
| 2424 | .ufcon = S3C2410_UFCON_DEFAULT, |
| 2425 | }, |
| 2426 | }; |
| 2427 | #define S3C2412_SERIAL_DRV_DATA ((kernel_ulong_t)&s3c2412_serial_drv_data) |
| 2428 | #else |
| 2429 | #define S3C2412_SERIAL_DRV_DATA (kernel_ulong_t)NULL |
| 2430 | #endif |
| 2431 | |
| 2432 | #if defined(CONFIG_CPU_S3C2440) || defined(CONFIG_CPU_S3C2416) || \ |
Denis 'GNUtoo' Carikli | b26469a | 2012-02-23 08:23:52 +0100 | [diff] [blame] | 2433 | defined(CONFIG_CPU_S3C2443) || defined(CONFIG_CPU_S3C2442) |
Thomas Abraham | da12150 | 2011-11-02 19:23:25 +0900 | [diff] [blame] | 2434 | static struct s3c24xx_serial_drv_data s3c2440_serial_drv_data = { |
| 2435 | .info = &(struct s3c24xx_uart_info) { |
| 2436 | .name = "Samsung S3C2440 UART", |
| 2437 | .type = PORT_S3C2440, |
| 2438 | .fifosize = 64, |
| 2439 | .has_divslot = 1, |
| 2440 | .rx_fifomask = S3C2440_UFSTAT_RXMASK, |
| 2441 | .rx_fifoshift = S3C2440_UFSTAT_RXSHIFT, |
| 2442 | .rx_fifofull = S3C2440_UFSTAT_RXFULL, |
| 2443 | .tx_fifofull = S3C2440_UFSTAT_TXFULL, |
| 2444 | .tx_fifomask = S3C2440_UFSTAT_TXMASK, |
| 2445 | .tx_fifoshift = S3C2440_UFSTAT_TXSHIFT, |
| 2446 | .def_clk_sel = S3C2410_UCON_CLKSEL2, |
| 2447 | .num_clks = 4, |
| 2448 | .clksel_mask = S3C2412_UCON_CLKMASK, |
| 2449 | .clksel_shift = S3C2412_UCON_CLKSHIFT, |
| 2450 | }, |
| 2451 | .def_cfg = &(struct s3c2410_uartcfg) { |
| 2452 | .ucon = S3C2410_UCON_DEFAULT, |
| 2453 | .ufcon = S3C2410_UFCON_DEFAULT, |
| 2454 | }, |
| 2455 | }; |
| 2456 | #define S3C2440_SERIAL_DRV_DATA ((kernel_ulong_t)&s3c2440_serial_drv_data) |
| 2457 | #else |
| 2458 | #define S3C2440_SERIAL_DRV_DATA (kernel_ulong_t)NULL |
| 2459 | #endif |
| 2460 | |
Kukjin Kim | 953b53a | 2014-07-01 06:32:22 +0900 | [diff] [blame] | 2461 | #if defined(CONFIG_CPU_S3C6400) || defined(CONFIG_CPU_S3C6410) |
Thomas Abraham | da12150 | 2011-11-02 19:23:25 +0900 | [diff] [blame] | 2462 | static struct s3c24xx_serial_drv_data s3c6400_serial_drv_data = { |
| 2463 | .info = &(struct s3c24xx_uart_info) { |
| 2464 | .name = "Samsung S3C6400 UART", |
| 2465 | .type = PORT_S3C6400, |
| 2466 | .fifosize = 64, |
| 2467 | .has_divslot = 1, |
| 2468 | .rx_fifomask = S3C2440_UFSTAT_RXMASK, |
| 2469 | .rx_fifoshift = S3C2440_UFSTAT_RXSHIFT, |
| 2470 | .rx_fifofull = S3C2440_UFSTAT_RXFULL, |
| 2471 | .tx_fifofull = S3C2440_UFSTAT_TXFULL, |
| 2472 | .tx_fifomask = S3C2440_UFSTAT_TXMASK, |
| 2473 | .tx_fifoshift = S3C2440_UFSTAT_TXSHIFT, |
| 2474 | .def_clk_sel = S3C2410_UCON_CLKSEL2, |
| 2475 | .num_clks = 4, |
| 2476 | .clksel_mask = S3C6400_UCON_CLKMASK, |
| 2477 | .clksel_shift = S3C6400_UCON_CLKSHIFT, |
| 2478 | }, |
| 2479 | .def_cfg = &(struct s3c2410_uartcfg) { |
| 2480 | .ucon = S3C2410_UCON_DEFAULT, |
| 2481 | .ufcon = S3C2410_UFCON_DEFAULT, |
| 2482 | }, |
| 2483 | }; |
| 2484 | #define S3C6400_SERIAL_DRV_DATA ((kernel_ulong_t)&s3c6400_serial_drv_data) |
| 2485 | #else |
| 2486 | #define S3C6400_SERIAL_DRV_DATA (kernel_ulong_t)NULL |
| 2487 | #endif |
| 2488 | |
| 2489 | #ifdef CONFIG_CPU_S5PV210 |
| 2490 | static struct s3c24xx_serial_drv_data s5pv210_serial_drv_data = { |
| 2491 | .info = &(struct s3c24xx_uart_info) { |
| 2492 | .name = "Samsung S5PV210 UART", |
| 2493 | .type = PORT_S3C6400, |
| 2494 | .has_divslot = 1, |
| 2495 | .rx_fifomask = S5PV210_UFSTAT_RXMASK, |
| 2496 | .rx_fifoshift = S5PV210_UFSTAT_RXSHIFT, |
| 2497 | .rx_fifofull = S5PV210_UFSTAT_RXFULL, |
| 2498 | .tx_fifofull = S5PV210_UFSTAT_TXFULL, |
| 2499 | .tx_fifomask = S5PV210_UFSTAT_TXMASK, |
| 2500 | .tx_fifoshift = S5PV210_UFSTAT_TXSHIFT, |
| 2501 | .def_clk_sel = S3C2410_UCON_CLKSEL0, |
| 2502 | .num_clks = 2, |
| 2503 | .clksel_mask = S5PV210_UCON_CLKMASK, |
| 2504 | .clksel_shift = S5PV210_UCON_CLKSHIFT, |
| 2505 | }, |
| 2506 | .def_cfg = &(struct s3c2410_uartcfg) { |
| 2507 | .ucon = S5PV210_UCON_DEFAULT, |
| 2508 | .ufcon = S5PV210_UFCON_DEFAULT, |
| 2509 | }, |
| 2510 | .fifosize = { 256, 64, 16, 16 }, |
| 2511 | }; |
| 2512 | #define S5PV210_SERIAL_DRV_DATA ((kernel_ulong_t)&s5pv210_serial_drv_data) |
| 2513 | #else |
| 2514 | #define S5PV210_SERIAL_DRV_DATA (kernel_ulong_t)NULL |
| 2515 | #endif |
| 2516 | |
Chander Kashyap | 33f8813 | 2013-06-19 00:29:34 +0900 | [diff] [blame] | 2517 | #if defined(CONFIG_ARCH_EXYNOS) |
Chanwoo Choi | 31ec77a | 2014-12-02 17:49:54 +0900 | [diff] [blame] | 2518 | #define EXYNOS_COMMON_SERIAL_DRV_DATA \ |
| 2519 | .info = &(struct s3c24xx_uart_info) { \ |
| 2520 | .name = "Samsung Exynos UART", \ |
| 2521 | .type = PORT_S3C6400, \ |
| 2522 | .has_divslot = 1, \ |
| 2523 | .rx_fifomask = S5PV210_UFSTAT_RXMASK, \ |
| 2524 | .rx_fifoshift = S5PV210_UFSTAT_RXSHIFT, \ |
| 2525 | .rx_fifofull = S5PV210_UFSTAT_RXFULL, \ |
| 2526 | .tx_fifofull = S5PV210_UFSTAT_TXFULL, \ |
| 2527 | .tx_fifomask = S5PV210_UFSTAT_TXMASK, \ |
| 2528 | .tx_fifoshift = S5PV210_UFSTAT_TXSHIFT, \ |
| 2529 | .def_clk_sel = S3C2410_UCON_CLKSEL0, \ |
| 2530 | .num_clks = 1, \ |
| 2531 | .clksel_mask = 0, \ |
| 2532 | .clksel_shift = 0, \ |
| 2533 | }, \ |
| 2534 | .def_cfg = &(struct s3c2410_uartcfg) { \ |
| 2535 | .ucon = S5PV210_UCON_DEFAULT, \ |
| 2536 | .ufcon = S5PV210_UFCON_DEFAULT, \ |
| 2537 | .has_fracval = 1, \ |
| 2538 | } \ |
| 2539 | |
Thomas Abraham | da12150 | 2011-11-02 19:23:25 +0900 | [diff] [blame] | 2540 | static struct s3c24xx_serial_drv_data exynos4210_serial_drv_data = { |
Chanwoo Choi | 31ec77a | 2014-12-02 17:49:54 +0900 | [diff] [blame] | 2541 | EXYNOS_COMMON_SERIAL_DRV_DATA, |
Thomas Abraham | da12150 | 2011-11-02 19:23:25 +0900 | [diff] [blame] | 2542 | .fifosize = { 256, 64, 16, 16 }, |
| 2543 | }; |
Chanwoo Choi | 31ec77a | 2014-12-02 17:49:54 +0900 | [diff] [blame] | 2544 | |
| 2545 | static struct s3c24xx_serial_drv_data exynos5433_serial_drv_data = { |
| 2546 | EXYNOS_COMMON_SERIAL_DRV_DATA, |
| 2547 | .fifosize = { 64, 256, 16, 256 }, |
| 2548 | }; |
| 2549 | |
Thomas Abraham | da12150 | 2011-11-02 19:23:25 +0900 | [diff] [blame] | 2550 | #define EXYNOS4210_SERIAL_DRV_DATA ((kernel_ulong_t)&exynos4210_serial_drv_data) |
Chanwoo Choi | 31ec77a | 2014-12-02 17:49:54 +0900 | [diff] [blame] | 2551 | #define EXYNOS5433_SERIAL_DRV_DATA ((kernel_ulong_t)&exynos5433_serial_drv_data) |
Thomas Abraham | da12150 | 2011-11-02 19:23:25 +0900 | [diff] [blame] | 2552 | #else |
| 2553 | #define EXYNOS4210_SERIAL_DRV_DATA (kernel_ulong_t)NULL |
Chanwoo Choi | 31ec77a | 2014-12-02 17:49:54 +0900 | [diff] [blame] | 2554 | #define EXYNOS5433_SERIAL_DRV_DATA (kernel_ulong_t)NULL |
Thomas Abraham | da12150 | 2011-11-02 19:23:25 +0900 | [diff] [blame] | 2555 | #endif |
| 2556 | |
Krzysztof Kozlowski | 24ee4df | 2015-05-02 00:40:05 +0900 | [diff] [blame] | 2557 | static const struct platform_device_id s3c24xx_serial_driver_ids[] = { |
Thomas Abraham | da12150 | 2011-11-02 19:23:25 +0900 | [diff] [blame] | 2558 | { |
| 2559 | .name = "s3c2410-uart", |
| 2560 | .driver_data = S3C2410_SERIAL_DRV_DATA, |
| 2561 | }, { |
| 2562 | .name = "s3c2412-uart", |
| 2563 | .driver_data = S3C2412_SERIAL_DRV_DATA, |
| 2564 | }, { |
| 2565 | .name = "s3c2440-uart", |
| 2566 | .driver_data = S3C2440_SERIAL_DRV_DATA, |
| 2567 | }, { |
| 2568 | .name = "s3c6400-uart", |
| 2569 | .driver_data = S3C6400_SERIAL_DRV_DATA, |
| 2570 | }, { |
| 2571 | .name = "s5pv210-uart", |
| 2572 | .driver_data = S5PV210_SERIAL_DRV_DATA, |
| 2573 | }, { |
| 2574 | .name = "exynos4210-uart", |
| 2575 | .driver_data = EXYNOS4210_SERIAL_DRV_DATA, |
Chanwoo Choi | 31ec77a | 2014-12-02 17:49:54 +0900 | [diff] [blame] | 2576 | }, { |
| 2577 | .name = "exynos5433-uart", |
| 2578 | .driver_data = EXYNOS5433_SERIAL_DRV_DATA, |
Thomas Abraham | da12150 | 2011-11-02 19:23:25 +0900 | [diff] [blame] | 2579 | }, |
| 2580 | { }, |
| 2581 | }; |
| 2582 | MODULE_DEVICE_TABLE(platform, s3c24xx_serial_driver_ids); |
| 2583 | |
Thomas Abraham | 26c919e | 2011-11-06 22:10:44 +0530 | [diff] [blame] | 2584 | #ifdef CONFIG_OF |
| 2585 | static const struct of_device_id s3c24xx_uart_dt_match[] = { |
Heiko Stübner | 666ca0b | 2012-11-22 11:37:44 +0100 | [diff] [blame] | 2586 | { .compatible = "samsung,s3c2410-uart", |
| 2587 | .data = (void *)S3C2410_SERIAL_DRV_DATA }, |
| 2588 | { .compatible = "samsung,s3c2412-uart", |
| 2589 | .data = (void *)S3C2412_SERIAL_DRV_DATA }, |
| 2590 | { .compatible = "samsung,s3c2440-uart", |
| 2591 | .data = (void *)S3C2440_SERIAL_DRV_DATA }, |
| 2592 | { .compatible = "samsung,s3c6400-uart", |
| 2593 | .data = (void *)S3C6400_SERIAL_DRV_DATA }, |
| 2594 | { .compatible = "samsung,s5pv210-uart", |
| 2595 | .data = (void *)S5PV210_SERIAL_DRV_DATA }, |
Thomas Abraham | 26c919e | 2011-11-06 22:10:44 +0530 | [diff] [blame] | 2596 | { .compatible = "samsung,exynos4210-uart", |
Mark Brown | a169a88 | 2011-11-08 17:00:14 +0900 | [diff] [blame] | 2597 | .data = (void *)EXYNOS4210_SERIAL_DRV_DATA }, |
Chanwoo Choi | 31ec77a | 2014-12-02 17:49:54 +0900 | [diff] [blame] | 2598 | { .compatible = "samsung,exynos5433-uart", |
| 2599 | .data = (void *)EXYNOS5433_SERIAL_DRV_DATA }, |
Thomas Abraham | 26c919e | 2011-11-06 22:10:44 +0530 | [diff] [blame] | 2600 | {}, |
| 2601 | }; |
| 2602 | MODULE_DEVICE_TABLE(of, s3c24xx_uart_dt_match); |
Thomas Abraham | 26c919e | 2011-11-06 22:10:44 +0530 | [diff] [blame] | 2603 | #endif |
| 2604 | |
Thomas Abraham | da12150 | 2011-11-02 19:23:25 +0900 | [diff] [blame] | 2605 | static struct platform_driver samsung_serial_driver = { |
| 2606 | .probe = s3c24xx_serial_probe, |
Bill Pemberton | 2d47b71 | 2012-11-19 13:21:34 -0500 | [diff] [blame] | 2607 | .remove = s3c24xx_serial_remove, |
Thomas Abraham | da12150 | 2011-11-02 19:23:25 +0900 | [diff] [blame] | 2608 | .id_table = s3c24xx_serial_driver_ids, |
| 2609 | .driver = { |
| 2610 | .name = "samsung-uart", |
Thomas Abraham | da12150 | 2011-11-02 19:23:25 +0900 | [diff] [blame] | 2611 | .pm = SERIAL_SAMSUNG_PM_OPS, |
Sachin Kamat | 905f4ba | 2013-01-07 09:50:42 +0530 | [diff] [blame] | 2612 | .of_match_table = of_match_ptr(s3c24xx_uart_dt_match), |
Thomas Abraham | da12150 | 2011-11-02 19:23:25 +0900 | [diff] [blame] | 2613 | }, |
| 2614 | }; |
| 2615 | |
Tushar Behera | 6f134c3c | 2014-01-20 14:32:34 +0530 | [diff] [blame] | 2616 | module_platform_driver(samsung_serial_driver); |
Thomas Abraham | da12150 | 2011-11-02 19:23:25 +0900 | [diff] [blame] | 2617 | |
Marek Szyprowski | c3bda29 | 2015-02-02 10:47:35 +0100 | [diff] [blame] | 2618 | #ifdef CONFIG_SERIAL_SAMSUNG_CONSOLE |
Tomasz Figa | b94ba03 | 2015-01-23 14:47:41 +0100 | [diff] [blame] | 2619 | /* |
| 2620 | * Early console. |
| 2621 | */ |
| 2622 | |
Hyunki Koo | 57253cc | 2020-05-06 17:02:40 +0900 | [diff] [blame] | 2623 | static void wr_reg_barrier(struct uart_port *port, u32 reg, u32 val) |
| 2624 | { |
| 2625 | switch (port->iotype) { |
| 2626 | case UPIO_MEM: |
| 2627 | writeb(val, portaddr(port, reg)); |
| 2628 | break; |
| 2629 | case UPIO_MEM32: |
| 2630 | writel(val, portaddr(port, reg)); |
| 2631 | break; |
| 2632 | } |
| 2633 | } |
| 2634 | |
Tomasz Figa | b94ba03 | 2015-01-23 14:47:41 +0100 | [diff] [blame] | 2635 | struct samsung_early_console_data { |
| 2636 | u32 txfull_mask; |
| 2637 | }; |
| 2638 | |
| 2639 | static void samsung_early_busyuart(struct uart_port *port) |
| 2640 | { |
| 2641 | while (!(readl(port->membase + S3C2410_UTRSTAT) & S3C2410_UTRSTAT_TXFE)) |
| 2642 | ; |
| 2643 | } |
| 2644 | |
| 2645 | static void samsung_early_busyuart_fifo(struct uart_port *port) |
| 2646 | { |
| 2647 | struct samsung_early_console_data *data = port->private_data; |
| 2648 | |
| 2649 | while (readl(port->membase + S3C2410_UFSTAT) & data->txfull_mask) |
| 2650 | ; |
| 2651 | } |
| 2652 | |
| 2653 | static void samsung_early_putc(struct uart_port *port, int c) |
| 2654 | { |
| 2655 | if (readl(port->membase + S3C2410_UFCON) & S3C2410_UFCON_FIFOMODE) |
| 2656 | samsung_early_busyuart_fifo(port); |
| 2657 | else |
| 2658 | samsung_early_busyuart(port); |
| 2659 | |
Hyunki Koo | 57253cc | 2020-05-06 17:02:40 +0900 | [diff] [blame] | 2660 | wr_reg_barrier(port, S3C2410_UTXH, c); |
Tomasz Figa | b94ba03 | 2015-01-23 14:47:41 +0100 | [diff] [blame] | 2661 | } |
| 2662 | |
Greg Kroah-Hartman | 90ece85 | 2019-12-10 15:37:04 +0100 | [diff] [blame] | 2663 | static void samsung_early_write(struct console *con, const char *s, |
| 2664 | unsigned int n) |
Tomasz Figa | b94ba03 | 2015-01-23 14:47:41 +0100 | [diff] [blame] | 2665 | { |
| 2666 | struct earlycon_device *dev = con->data; |
| 2667 | |
| 2668 | uart_console_write(&dev->port, s, n, samsung_early_putc); |
| 2669 | } |
| 2670 | |
| 2671 | static int __init samsung_early_console_setup(struct earlycon_device *device, |
| 2672 | const char *opt) |
| 2673 | { |
| 2674 | if (!device->port.membase) |
| 2675 | return -ENODEV; |
| 2676 | |
| 2677 | device->con->write = samsung_early_write; |
| 2678 | return 0; |
| 2679 | } |
| 2680 | |
| 2681 | /* S3C2410 */ |
| 2682 | static struct samsung_early_console_data s3c2410_early_console_data = { |
| 2683 | .txfull_mask = S3C2410_UFSTAT_TXFULL, |
| 2684 | }; |
| 2685 | |
| 2686 | static int __init s3c2410_early_console_setup(struct earlycon_device *device, |
| 2687 | const char *opt) |
| 2688 | { |
| 2689 | device->port.private_data = &s3c2410_early_console_data; |
| 2690 | return samsung_early_console_setup(device, opt); |
| 2691 | } |
Krzysztof Kozlowski | 695b847 | 2020-06-17 17:28:56 +0200 | [diff] [blame] | 2692 | |
Tomasz Figa | b94ba03 | 2015-01-23 14:47:41 +0100 | [diff] [blame] | 2693 | OF_EARLYCON_DECLARE(s3c2410, "samsung,s3c2410-uart", |
| 2694 | s3c2410_early_console_setup); |
Tomasz Figa | b94ba03 | 2015-01-23 14:47:41 +0100 | [diff] [blame] | 2695 | |
| 2696 | /* S3C2412, S3C2440, S3C64xx */ |
| 2697 | static struct samsung_early_console_data s3c2440_early_console_data = { |
| 2698 | .txfull_mask = S3C2440_UFSTAT_TXFULL, |
| 2699 | }; |
| 2700 | |
| 2701 | static int __init s3c2440_early_console_setup(struct earlycon_device *device, |
| 2702 | const char *opt) |
| 2703 | { |
| 2704 | device->port.private_data = &s3c2440_early_console_data; |
| 2705 | return samsung_early_console_setup(device, opt); |
| 2706 | } |
Krzysztof Kozlowski | 695b847 | 2020-06-17 17:28:56 +0200 | [diff] [blame] | 2707 | |
Tomasz Figa | b94ba03 | 2015-01-23 14:47:41 +0100 | [diff] [blame] | 2708 | OF_EARLYCON_DECLARE(s3c2412, "samsung,s3c2412-uart", |
| 2709 | s3c2440_early_console_setup); |
| 2710 | OF_EARLYCON_DECLARE(s3c2440, "samsung,s3c2440-uart", |
| 2711 | s3c2440_early_console_setup); |
| 2712 | OF_EARLYCON_DECLARE(s3c6400, "samsung,s3c6400-uart", |
| 2713 | s3c2440_early_console_setup); |
Tomasz Figa | b94ba03 | 2015-01-23 14:47:41 +0100 | [diff] [blame] | 2714 | |
Krzysztof Kozlowski | b209713 | 2020-01-04 16:21:04 +0100 | [diff] [blame] | 2715 | /* S5PV210, Exynos */ |
Tomasz Figa | b94ba03 | 2015-01-23 14:47:41 +0100 | [diff] [blame] | 2716 | static struct samsung_early_console_data s5pv210_early_console_data = { |
| 2717 | .txfull_mask = S5PV210_UFSTAT_TXFULL, |
| 2718 | }; |
| 2719 | |
| 2720 | static int __init s5pv210_early_console_setup(struct earlycon_device *device, |
| 2721 | const char *opt) |
| 2722 | { |
| 2723 | device->port.private_data = &s5pv210_early_console_data; |
| 2724 | return samsung_early_console_setup(device, opt); |
| 2725 | } |
Krzysztof Kozlowski | 695b847 | 2020-06-17 17:28:56 +0200 | [diff] [blame] | 2726 | |
Tomasz Figa | b94ba03 | 2015-01-23 14:47:41 +0100 | [diff] [blame] | 2727 | OF_EARLYCON_DECLARE(s5pv210, "samsung,s5pv210-uart", |
| 2728 | s5pv210_early_console_setup); |
| 2729 | OF_EARLYCON_DECLARE(exynos4210, "samsung,exynos4210-uart", |
| 2730 | s5pv210_early_console_setup); |
Marek Szyprowski | c3bda29 | 2015-02-02 10:47:35 +0100 | [diff] [blame] | 2731 | #endif |
Tomasz Figa | b94ba03 | 2015-01-23 14:47:41 +0100 | [diff] [blame] | 2732 | |
Thomas Abraham | da12150 | 2011-11-02 19:23:25 +0900 | [diff] [blame] | 2733 | MODULE_ALIAS("platform:samsung-uart"); |
Ben Dooks | b497549 | 2008-07-03 12:32:51 +0100 | [diff] [blame] | 2734 | MODULE_DESCRIPTION("Samsung SoC Serial port driver"); |
| 2735 | MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>"); |
| 2736 | MODULE_LICENSE("GPL v2"); |