Rong Wang | 161e773 | 2011-11-17 23:17:04 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Driver for CSR SiRFprimaII onboard UARTs. |
| 3 | * |
| 4 | * Copyright (c) 2011 Cambridge Silicon Radio Limited, a CSR plc group company. |
| 5 | * |
| 6 | * Licensed under GPLv2 or later. |
| 7 | */ |
| 8 | |
| 9 | #include <linux/module.h> |
| 10 | #include <linux/ioport.h> |
| 11 | #include <linux/platform_device.h> |
| 12 | #include <linux/init.h> |
| 13 | #include <linux/sysrq.h> |
| 14 | #include <linux/console.h> |
| 15 | #include <linux/tty.h> |
| 16 | #include <linux/tty_flip.h> |
| 17 | #include <linux/serial_core.h> |
| 18 | #include <linux/serial.h> |
| 19 | #include <linux/clk.h> |
| 20 | #include <linux/of.h> |
| 21 | #include <linux/slab.h> |
| 22 | #include <linux/io.h> |
Qipan Li | 2eb5618 | 2013-08-15 06:52:15 +0800 | [diff] [blame] | 23 | #include <linux/of_gpio.h> |
Qipan Li | 8316d04 | 2013-08-19 11:47:53 +0800 | [diff] [blame] | 24 | #include <linux/dmaengine.h> |
| 25 | #include <linux/dma-direction.h> |
| 26 | #include <linux/dma-mapping.h> |
Rong Wang | 161e773 | 2011-11-17 23:17:04 +0800 | [diff] [blame] | 27 | #include <asm/irq.h> |
| 28 | #include <asm/mach/irq.h> |
Rong Wang | 161e773 | 2011-11-17 23:17:04 +0800 | [diff] [blame] | 29 | |
| 30 | #include "sirfsoc_uart.h" |
| 31 | |
| 32 | static unsigned int |
| 33 | sirfsoc_uart_pio_tx_chars(struct sirfsoc_uart_port *sirfport, int count); |
| 34 | static unsigned int |
| 35 | sirfsoc_uart_pio_rx_chars(struct uart_port *port, unsigned int max_rx_count); |
| 36 | static struct uart_driver sirfsoc_uart_drv; |
| 37 | |
Qipan Li | 8316d04 | 2013-08-19 11:47:53 +0800 | [diff] [blame] | 38 | static void sirfsoc_uart_tx_dma_complete_callback(void *param); |
| 39 | static void sirfsoc_uart_start_next_rx_dma(struct uart_port *port); |
| 40 | static void sirfsoc_uart_rx_dma_complete_callback(void *param); |
Rong Wang | 161e773 | 2011-11-17 23:17:04 +0800 | [diff] [blame] | 41 | static const struct sirfsoc_baudrate_to_regv baudrate_to_regv[] = { |
| 42 | {4000000, 2359296}, |
| 43 | {3500000, 1310721}, |
| 44 | {3000000, 1572865}, |
| 45 | {2500000, 1245186}, |
| 46 | {2000000, 1572866}, |
| 47 | {1500000, 1245188}, |
| 48 | {1152000, 1638404}, |
| 49 | {1000000, 1572869}, |
| 50 | {921600, 1114120}, |
| 51 | {576000, 1245196}, |
| 52 | {500000, 1245198}, |
| 53 | {460800, 1572876}, |
| 54 | {230400, 1310750}, |
| 55 | {115200, 1310781}, |
| 56 | {57600, 1310843}, |
| 57 | {38400, 1114328}, |
| 58 | {19200, 1114545}, |
| 59 | {9600, 1114979}, |
| 60 | }; |
| 61 | |
Qipan Li | a6ffe89 | 2015-04-29 06:45:08 +0000 | [diff] [blame] | 62 | static struct sirfsoc_uart_port *sirf_ports[SIRFSOC_UART_NR]; |
Rong Wang | 161e773 | 2011-11-17 23:17:04 +0800 | [diff] [blame] | 63 | |
| 64 | static inline struct sirfsoc_uart_port *to_sirfport(struct uart_port *port) |
| 65 | { |
| 66 | return container_of(port, struct sirfsoc_uart_port, port); |
| 67 | } |
| 68 | |
| 69 | static inline unsigned int sirfsoc_uart_tx_empty(struct uart_port *port) |
| 70 | { |
| 71 | unsigned long reg; |
Qipan Li | 5df8311 | 2013-08-12 18:15:35 +0800 | [diff] [blame] | 72 | struct sirfsoc_uart_port *sirfport = to_sirfport(port); |
| 73 | struct sirfsoc_register *ureg = &sirfport->uart_reg->uart_reg; |
| 74 | struct sirfsoc_fifo_status *ufifo_st = &sirfport->uart_reg->fifo_status; |
| 75 | reg = rd_regl(port, ureg->sirfsoc_tx_fifo_status); |
Qipan Li | cb4595a | 2015-04-29 06:45:09 +0000 | [diff] [blame] | 76 | return (reg & ufifo_st->ff_empty(port)) ? TIOCSER_TEMT : 0; |
Rong Wang | 161e773 | 2011-11-17 23:17:04 +0800 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | static unsigned int sirfsoc_uart_get_mctrl(struct uart_port *port) |
| 80 | { |
| 81 | struct sirfsoc_uart_port *sirfport = to_sirfport(port); |
Qipan Li | 5df8311 | 2013-08-12 18:15:35 +0800 | [diff] [blame] | 82 | struct sirfsoc_register *ureg = &sirfport->uart_reg->uart_reg; |
Qipan Li | 2eb5618 | 2013-08-15 06:52:15 +0800 | [diff] [blame] | 83 | if (!sirfport->hw_flow_ctrl || !sirfport->ms_enabled) |
Rong Wang | 161e773 | 2011-11-17 23:17:04 +0800 | [diff] [blame] | 84 | goto cts_asserted; |
Qipan Li | 2eb5618 | 2013-08-15 06:52:15 +0800 | [diff] [blame] | 85 | if (sirfport->uart_reg->uart_type == SIRF_REAL_UART) { |
Qipan Li | 5df8311 | 2013-08-12 18:15:35 +0800 | [diff] [blame] | 86 | if (!(rd_regl(port, ureg->sirfsoc_afc_ctrl) & |
| 87 | SIRFUART_AFC_CTS_STATUS)) |
Rong Wang | 161e773 | 2011-11-17 23:17:04 +0800 | [diff] [blame] | 88 | goto cts_asserted; |
| 89 | else |
| 90 | goto cts_deasserted; |
Qipan Li | 2eb5618 | 2013-08-15 06:52:15 +0800 | [diff] [blame] | 91 | } else { |
| 92 | if (!gpio_get_value(sirfport->cts_gpio)) |
| 93 | goto cts_asserted; |
| 94 | else |
| 95 | goto cts_deasserted; |
Rong Wang | 161e773 | 2011-11-17 23:17:04 +0800 | [diff] [blame] | 96 | } |
| 97 | cts_deasserted: |
| 98 | return TIOCM_CAR | TIOCM_DSR; |
| 99 | cts_asserted: |
| 100 | return TIOCM_CAR | TIOCM_DSR | TIOCM_CTS; |
| 101 | } |
| 102 | |
| 103 | static void sirfsoc_uart_set_mctrl(struct uart_port *port, unsigned int mctrl) |
| 104 | { |
| 105 | struct sirfsoc_uart_port *sirfport = to_sirfport(port); |
Qipan Li | 5df8311 | 2013-08-12 18:15:35 +0800 | [diff] [blame] | 106 | struct sirfsoc_register *ureg = &sirfport->uart_reg->uart_reg; |
Rong Wang | 161e773 | 2011-11-17 23:17:04 +0800 | [diff] [blame] | 107 | unsigned int assert = mctrl & TIOCM_RTS; |
| 108 | unsigned int val = assert ? SIRFUART_AFC_CTRL_RX_THD : 0x0; |
| 109 | unsigned int current_val; |
Qipan Li | 2eb5618 | 2013-08-15 06:52:15 +0800 | [diff] [blame] | 110 | |
| 111 | if (!sirfport->hw_flow_ctrl || !sirfport->ms_enabled) |
| 112 | return; |
| 113 | if (sirfport->uart_reg->uart_type == SIRF_REAL_UART) { |
Qipan Li | 5df8311 | 2013-08-12 18:15:35 +0800 | [diff] [blame] | 114 | current_val = rd_regl(port, ureg->sirfsoc_afc_ctrl) & ~0xFF; |
Rong Wang | 161e773 | 2011-11-17 23:17:04 +0800 | [diff] [blame] | 115 | val |= current_val; |
Qipan Li | 5df8311 | 2013-08-12 18:15:35 +0800 | [diff] [blame] | 116 | wr_regl(port, ureg->sirfsoc_afc_ctrl, val); |
Qipan Li | 2eb5618 | 2013-08-15 06:52:15 +0800 | [diff] [blame] | 117 | } else { |
| 118 | if (!val) |
| 119 | gpio_set_value(sirfport->rts_gpio, 1); |
| 120 | else |
| 121 | gpio_set_value(sirfport->rts_gpio, 0); |
Rong Wang | 161e773 | 2011-11-17 23:17:04 +0800 | [diff] [blame] | 122 | } |
| 123 | } |
| 124 | |
| 125 | static void sirfsoc_uart_stop_tx(struct uart_port *port) |
| 126 | { |
Barry Song | 909102d | 2013-08-07 13:35:38 +0800 | [diff] [blame] | 127 | struct sirfsoc_uart_port *sirfport = to_sirfport(port); |
Qipan Li | 5df8311 | 2013-08-12 18:15:35 +0800 | [diff] [blame] | 128 | struct sirfsoc_register *ureg = &sirfport->uart_reg->uart_reg; |
| 129 | struct sirfsoc_int_en *uint_en = &sirfport->uart_reg->uart_int_en; |
Barry Song | 909102d | 2013-08-07 13:35:38 +0800 | [diff] [blame] | 130 | |
Qipan Li | 9be16b3 | 2014-01-30 13:57:29 +0800 | [diff] [blame] | 131 | if (sirfport->tx_dma_chan) { |
Qipan Li | 8316d04 | 2013-08-19 11:47:53 +0800 | [diff] [blame] | 132 | if (sirfport->tx_dma_state == TX_DMA_RUNNING) { |
| 133 | dmaengine_pause(sirfport->tx_dma_chan); |
| 134 | sirfport->tx_dma_state = TX_DMA_PAUSE; |
| 135 | } else { |
Barry Song | 057badd | 2015-01-03 17:02:57 +0800 | [diff] [blame] | 136 | if (!sirfport->is_atlas7) |
Qipan Li | 8316d04 | 2013-08-19 11:47:53 +0800 | [diff] [blame] | 137 | wr_regl(port, ureg->sirfsoc_int_en_reg, |
| 138 | rd_regl(port, ureg->sirfsoc_int_en_reg) & |
| 139 | ~uint_en->sirfsoc_txfifo_empty_en); |
| 140 | else |
Qipan Li | c1b7ac6 | 2015-05-14 06:45:21 +0000 | [diff] [blame^] | 141 | wr_regl(port, ureg->sirfsoc_int_en_clr_reg, |
Qipan Li | 8316d04 | 2013-08-19 11:47:53 +0800 | [diff] [blame] | 142 | uint_en->sirfsoc_txfifo_empty_en); |
| 143 | } |
| 144 | } else { |
Qipan Li | c1b7ac6 | 2015-05-14 06:45:21 +0000 | [diff] [blame^] | 145 | if (sirfport->uart_reg->uart_type == SIRF_USP_UART) |
| 146 | wr_regl(port, ureg->sirfsoc_tx_rx_en, rd_regl(port, |
| 147 | ureg->sirfsoc_tx_rx_en) & ~SIRFUART_TX_EN); |
Barry Song | 057badd | 2015-01-03 17:02:57 +0800 | [diff] [blame] | 148 | if (!sirfport->is_atlas7) |
Qipan Li | 8316d04 | 2013-08-19 11:47:53 +0800 | [diff] [blame] | 149 | wr_regl(port, ureg->sirfsoc_int_en_reg, |
| 150 | rd_regl(port, ureg->sirfsoc_int_en_reg) & |
| 151 | ~uint_en->sirfsoc_txfifo_empty_en); |
| 152 | else |
Qipan Li | c1b7ac6 | 2015-05-14 06:45:21 +0000 | [diff] [blame^] | 153 | wr_regl(port, ureg->sirfsoc_int_en_clr_reg, |
Qipan Li | 8316d04 | 2013-08-19 11:47:53 +0800 | [diff] [blame] | 154 | uint_en->sirfsoc_txfifo_empty_en); |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | static void sirfsoc_uart_tx_with_dma(struct sirfsoc_uart_port *sirfport) |
| 159 | { |
| 160 | struct uart_port *port = &sirfport->port; |
| 161 | struct sirfsoc_register *ureg = &sirfport->uart_reg->uart_reg; |
| 162 | struct sirfsoc_int_en *uint_en = &sirfport->uart_reg->uart_int_en; |
| 163 | struct circ_buf *xmit = &port->state->xmit; |
| 164 | unsigned long tran_size; |
| 165 | unsigned long tran_start; |
| 166 | unsigned long pio_tx_size; |
| 167 | |
| 168 | tran_size = CIRC_CNT_TO_END(xmit->head, xmit->tail, UART_XMIT_SIZE); |
| 169 | tran_start = (unsigned long)(xmit->buf + xmit->tail); |
| 170 | if (uart_circ_empty(xmit) || uart_tx_stopped(port) || |
| 171 | !tran_size) |
| 172 | return; |
| 173 | if (sirfport->tx_dma_state == TX_DMA_PAUSE) { |
| 174 | dmaengine_resume(sirfport->tx_dma_chan); |
| 175 | return; |
| 176 | } |
| 177 | if (sirfport->tx_dma_state == TX_DMA_RUNNING) |
| 178 | return; |
Barry Song | 057badd | 2015-01-03 17:02:57 +0800 | [diff] [blame] | 179 | if (!sirfport->is_atlas7) |
Qipan Li | 5df8311 | 2013-08-12 18:15:35 +0800 | [diff] [blame] | 180 | wr_regl(port, ureg->sirfsoc_int_en_reg, |
Qipan Li | 8316d04 | 2013-08-19 11:47:53 +0800 | [diff] [blame] | 181 | rd_regl(port, ureg->sirfsoc_int_en_reg)& |
| 182 | ~(uint_en->sirfsoc_txfifo_empty_en)); |
| 183 | else |
Qipan Li | c1b7ac6 | 2015-05-14 06:45:21 +0000 | [diff] [blame^] | 184 | wr_regl(port, ureg->sirfsoc_int_en_clr_reg, |
Qipan Li | 5df8311 | 2013-08-12 18:15:35 +0800 | [diff] [blame] | 185 | uint_en->sirfsoc_txfifo_empty_en); |
Qipan Li | 8316d04 | 2013-08-19 11:47:53 +0800 | [diff] [blame] | 186 | /* |
| 187 | * DMA requires buffer address and buffer length are both aligned with |
| 188 | * 4 bytes, so we use PIO for |
| 189 | * 1. if address is not aligned with 4bytes, use PIO for the first 1~3 |
| 190 | * bytes, and move to DMA for the left part aligned with 4bytes |
| 191 | * 2. if buffer length is not aligned with 4bytes, use DMA for aligned |
| 192 | * part first, move to PIO for the left 1~3 bytes |
| 193 | */ |
| 194 | if (tran_size < 4 || BYTES_TO_ALIGN(tran_start)) { |
| 195 | wr_regl(port, ureg->sirfsoc_tx_fifo_op, SIRFUART_FIFO_STOP); |
| 196 | wr_regl(port, ureg->sirfsoc_tx_dma_io_ctrl, |
| 197 | rd_regl(port, ureg->sirfsoc_tx_dma_io_ctrl)| |
| 198 | SIRFUART_IO_MODE); |
| 199 | if (BYTES_TO_ALIGN(tran_start)) { |
| 200 | pio_tx_size = sirfsoc_uart_pio_tx_chars(sirfport, |
| 201 | BYTES_TO_ALIGN(tran_start)); |
| 202 | tran_size -= pio_tx_size; |
| 203 | } |
| 204 | if (tran_size < 4) |
| 205 | sirfsoc_uart_pio_tx_chars(sirfport, tran_size); |
Barry Song | 057badd | 2015-01-03 17:02:57 +0800 | [diff] [blame] | 206 | if (!sirfport->is_atlas7) |
Qipan Li | 8316d04 | 2013-08-19 11:47:53 +0800 | [diff] [blame] | 207 | wr_regl(port, ureg->sirfsoc_int_en_reg, |
| 208 | rd_regl(port, ureg->sirfsoc_int_en_reg)| |
| 209 | uint_en->sirfsoc_txfifo_empty_en); |
| 210 | else |
| 211 | wr_regl(port, ureg->sirfsoc_int_en_reg, |
| 212 | uint_en->sirfsoc_txfifo_empty_en); |
| 213 | wr_regl(port, ureg->sirfsoc_tx_fifo_op, SIRFUART_FIFO_START); |
| 214 | } else { |
| 215 | /* tx transfer mode switch into dma mode */ |
| 216 | wr_regl(port, ureg->sirfsoc_tx_fifo_op, SIRFUART_FIFO_STOP); |
| 217 | wr_regl(port, ureg->sirfsoc_tx_dma_io_ctrl, |
| 218 | rd_regl(port, ureg->sirfsoc_tx_dma_io_ctrl)& |
| 219 | ~SIRFUART_IO_MODE); |
| 220 | wr_regl(port, ureg->sirfsoc_tx_fifo_op, SIRFUART_FIFO_START); |
| 221 | tran_size &= ~(0x3); |
Qipan Li | 5df8311 | 2013-08-12 18:15:35 +0800 | [diff] [blame] | 222 | |
Qipan Li | 8316d04 | 2013-08-19 11:47:53 +0800 | [diff] [blame] | 223 | sirfport->tx_dma_addr = dma_map_single(port->dev, |
| 224 | xmit->buf + xmit->tail, |
| 225 | tran_size, DMA_TO_DEVICE); |
| 226 | sirfport->tx_dma_desc = dmaengine_prep_slave_single( |
| 227 | sirfport->tx_dma_chan, sirfport->tx_dma_addr, |
| 228 | tran_size, DMA_MEM_TO_DEV, DMA_PREP_INTERRUPT); |
| 229 | if (!sirfport->tx_dma_desc) { |
| 230 | dev_err(port->dev, "DMA prep slave single fail\n"); |
| 231 | return; |
| 232 | } |
| 233 | sirfport->tx_dma_desc->callback = |
| 234 | sirfsoc_uart_tx_dma_complete_callback; |
| 235 | sirfport->tx_dma_desc->callback_param = (void *)sirfport; |
| 236 | sirfport->transfer_size = tran_size; |
| 237 | |
| 238 | dmaengine_submit(sirfport->tx_dma_desc); |
| 239 | dma_async_issue_pending(sirfport->tx_dma_chan); |
| 240 | sirfport->tx_dma_state = TX_DMA_RUNNING; |
| 241 | } |
Rong Wang | 161e773 | 2011-11-17 23:17:04 +0800 | [diff] [blame] | 242 | } |
| 243 | |
Jingoo Han | ada1f443d | 2013-08-08 17:41:43 +0900 | [diff] [blame] | 244 | static void sirfsoc_uart_start_tx(struct uart_port *port) |
Rong Wang | 161e773 | 2011-11-17 23:17:04 +0800 | [diff] [blame] | 245 | { |
| 246 | struct sirfsoc_uart_port *sirfport = to_sirfport(port); |
Qipan Li | 5df8311 | 2013-08-12 18:15:35 +0800 | [diff] [blame] | 247 | struct sirfsoc_register *ureg = &sirfport->uart_reg->uart_reg; |
| 248 | struct sirfsoc_int_en *uint_en = &sirfport->uart_reg->uart_int_en; |
Qipan Li | 9be16b3 | 2014-01-30 13:57:29 +0800 | [diff] [blame] | 249 | if (sirfport->tx_dma_chan) |
Qipan Li | 8316d04 | 2013-08-19 11:47:53 +0800 | [diff] [blame] | 250 | sirfsoc_uart_tx_with_dma(sirfport); |
| 251 | else { |
Qipan Li | c1b7ac6 | 2015-05-14 06:45:21 +0000 | [diff] [blame^] | 252 | if (sirfport->uart_reg->uart_type == SIRF_USP_UART) |
| 253 | wr_regl(port, ureg->sirfsoc_tx_rx_en, rd_regl(port, |
| 254 | ureg->sirfsoc_tx_rx_en) | SIRFUART_TX_EN); |
Qipan Li | cb4595a | 2015-04-29 06:45:09 +0000 | [diff] [blame] | 255 | sirfsoc_uart_pio_tx_chars(sirfport, port->fifosize); |
Qipan Li | 8316d04 | 2013-08-19 11:47:53 +0800 | [diff] [blame] | 256 | wr_regl(port, ureg->sirfsoc_tx_fifo_op, SIRFUART_FIFO_START); |
Barry Song | 057badd | 2015-01-03 17:02:57 +0800 | [diff] [blame] | 257 | if (!sirfport->is_atlas7) |
Qipan Li | 8316d04 | 2013-08-19 11:47:53 +0800 | [diff] [blame] | 258 | wr_regl(port, ureg->sirfsoc_int_en_reg, |
| 259 | rd_regl(port, ureg->sirfsoc_int_en_reg)| |
| 260 | uint_en->sirfsoc_txfifo_empty_en); |
| 261 | else |
| 262 | wr_regl(port, ureg->sirfsoc_int_en_reg, |
| 263 | uint_en->sirfsoc_txfifo_empty_en); |
| 264 | } |
Rong Wang | 161e773 | 2011-11-17 23:17:04 +0800 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | static void sirfsoc_uart_stop_rx(struct uart_port *port) |
| 268 | { |
Barry Song | 909102d | 2013-08-07 13:35:38 +0800 | [diff] [blame] | 269 | struct sirfsoc_uart_port *sirfport = to_sirfport(port); |
Qipan Li | 5df8311 | 2013-08-12 18:15:35 +0800 | [diff] [blame] | 270 | struct sirfsoc_register *ureg = &sirfport->uart_reg->uart_reg; |
| 271 | struct sirfsoc_int_en *uint_en = &sirfport->uart_reg->uart_int_en; |
Qipan Li | 8316d04 | 2013-08-19 11:47:53 +0800 | [diff] [blame] | 272 | |
Qipan Li | 5df8311 | 2013-08-12 18:15:35 +0800 | [diff] [blame] | 273 | wr_regl(port, ureg->sirfsoc_rx_fifo_op, 0); |
Qipan Li | 9be16b3 | 2014-01-30 13:57:29 +0800 | [diff] [blame] | 274 | if (sirfport->rx_dma_chan) { |
Barry Song | 057badd | 2015-01-03 17:02:57 +0800 | [diff] [blame] | 275 | if (!sirfport->is_atlas7) |
Qipan Li | 8316d04 | 2013-08-19 11:47:53 +0800 | [diff] [blame] | 276 | wr_regl(port, ureg->sirfsoc_int_en_reg, |
| 277 | rd_regl(port, ureg->sirfsoc_int_en_reg) & |
Qipan Li | c1b7ac6 | 2015-05-14 06:45:21 +0000 | [diff] [blame^] | 278 | ~(SIRFUART_RX_DMA_INT_EN(uint_en, |
| 279 | sirfport->uart_reg->uart_type) | |
Qipan Li | 8316d04 | 2013-08-19 11:47:53 +0800 | [diff] [blame] | 280 | uint_en->sirfsoc_rx_done_en)); |
| 281 | else |
Qipan Li | c1b7ac6 | 2015-05-14 06:45:21 +0000 | [diff] [blame^] | 282 | wr_regl(port, ureg->sirfsoc_int_en_clr_reg, |
| 283 | SIRFUART_RX_DMA_INT_EN(uint_en, |
| 284 | sirfport->uart_reg->uart_type)| |
| 285 | uint_en->sirfsoc_rx_done_en); |
Qipan Li | 8316d04 | 2013-08-19 11:47:53 +0800 | [diff] [blame] | 286 | dmaengine_terminate_all(sirfport->rx_dma_chan); |
| 287 | } else { |
Barry Song | 057badd | 2015-01-03 17:02:57 +0800 | [diff] [blame] | 288 | if (!sirfport->is_atlas7) |
Qipan Li | 8316d04 | 2013-08-19 11:47:53 +0800 | [diff] [blame] | 289 | wr_regl(port, ureg->sirfsoc_int_en_reg, |
| 290 | rd_regl(port, ureg->sirfsoc_int_en_reg)& |
Qipan Li | c1b7ac6 | 2015-05-14 06:45:21 +0000 | [diff] [blame^] | 291 | ~(SIRFUART_RX_IO_INT_EN(uint_en, |
| 292 | sirfport->uart_reg->uart_type))); |
Qipan Li | 8316d04 | 2013-08-19 11:47:53 +0800 | [diff] [blame] | 293 | else |
Qipan Li | c1b7ac6 | 2015-05-14 06:45:21 +0000 | [diff] [blame^] | 294 | wr_regl(port, ureg->sirfsoc_int_en_clr_reg, |
| 295 | SIRFUART_RX_IO_INT_EN(uint_en, |
| 296 | sirfport->uart_reg->uart_type)); |
Qipan Li | 8316d04 | 2013-08-19 11:47:53 +0800 | [diff] [blame] | 297 | } |
Rong Wang | 161e773 | 2011-11-17 23:17:04 +0800 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | static void sirfsoc_uart_disable_ms(struct uart_port *port) |
| 301 | { |
| 302 | struct sirfsoc_uart_port *sirfport = to_sirfport(port); |
Qipan Li | 5df8311 | 2013-08-12 18:15:35 +0800 | [diff] [blame] | 303 | struct sirfsoc_register *ureg = &sirfport->uart_reg->uart_reg; |
| 304 | struct sirfsoc_int_en *uint_en = &sirfport->uart_reg->uart_int_en; |
Barry Song | 909102d | 2013-08-07 13:35:38 +0800 | [diff] [blame] | 305 | |
Rong Wang | 161e773 | 2011-11-17 23:17:04 +0800 | [diff] [blame] | 306 | if (!sirfport->hw_flow_ctrl) |
| 307 | return; |
Qipan Li | 2eb5618 | 2013-08-15 06:52:15 +0800 | [diff] [blame] | 308 | sirfport->ms_enabled = false; |
| 309 | if (sirfport->uart_reg->uart_type == SIRF_REAL_UART) { |
| 310 | wr_regl(port, ureg->sirfsoc_afc_ctrl, |
| 311 | rd_regl(port, ureg->sirfsoc_afc_ctrl) & ~0x3FF); |
Barry Song | 057badd | 2015-01-03 17:02:57 +0800 | [diff] [blame] | 312 | if (!sirfport->is_atlas7) |
Qipan Li | 2eb5618 | 2013-08-15 06:52:15 +0800 | [diff] [blame] | 313 | wr_regl(port, ureg->sirfsoc_int_en_reg, |
| 314 | rd_regl(port, ureg->sirfsoc_int_en_reg)& |
| 315 | ~uint_en->sirfsoc_cts_en); |
| 316 | else |
Qipan Li | c1b7ac6 | 2015-05-14 06:45:21 +0000 | [diff] [blame^] | 317 | wr_regl(port, ureg->sirfsoc_int_en_clr_reg, |
Qipan Li | 2eb5618 | 2013-08-15 06:52:15 +0800 | [diff] [blame] | 318 | uint_en->sirfsoc_cts_en); |
Qipan Li | 5df8311 | 2013-08-12 18:15:35 +0800 | [diff] [blame] | 319 | } else |
Qipan Li | 2eb5618 | 2013-08-15 06:52:15 +0800 | [diff] [blame] | 320 | disable_irq(gpio_to_irq(sirfport->cts_gpio)); |
| 321 | } |
| 322 | |
| 323 | static irqreturn_t sirfsoc_uart_usp_cts_handler(int irq, void *dev_id) |
| 324 | { |
| 325 | struct sirfsoc_uart_port *sirfport = (struct sirfsoc_uart_port *)dev_id; |
| 326 | struct uart_port *port = &sirfport->port; |
Qipan Li | 07d410e | 2014-05-26 19:02:07 +0800 | [diff] [blame] | 327 | spin_lock(&port->lock); |
Qipan Li | 2eb5618 | 2013-08-15 06:52:15 +0800 | [diff] [blame] | 328 | if (gpio_is_valid(sirfport->cts_gpio) && sirfport->ms_enabled) |
| 329 | uart_handle_cts_change(port, |
| 330 | !gpio_get_value(sirfport->cts_gpio)); |
Qipan Li | 07d410e | 2014-05-26 19:02:07 +0800 | [diff] [blame] | 331 | spin_unlock(&port->lock); |
Qipan Li | 2eb5618 | 2013-08-15 06:52:15 +0800 | [diff] [blame] | 332 | return IRQ_HANDLED; |
Rong Wang | 161e773 | 2011-11-17 23:17:04 +0800 | [diff] [blame] | 333 | } |
| 334 | |
| 335 | static void sirfsoc_uart_enable_ms(struct uart_port *port) |
| 336 | { |
| 337 | struct sirfsoc_uart_port *sirfport = to_sirfport(port); |
Qipan Li | 5df8311 | 2013-08-12 18:15:35 +0800 | [diff] [blame] | 338 | struct sirfsoc_register *ureg = &sirfport->uart_reg->uart_reg; |
| 339 | struct sirfsoc_int_en *uint_en = &sirfport->uart_reg->uart_int_en; |
Barry Song | 909102d | 2013-08-07 13:35:38 +0800 | [diff] [blame] | 340 | |
Rong Wang | 161e773 | 2011-11-17 23:17:04 +0800 | [diff] [blame] | 341 | if (!sirfport->hw_flow_ctrl) |
| 342 | return; |
Qipan Li | 2eb5618 | 2013-08-15 06:52:15 +0800 | [diff] [blame] | 343 | sirfport->ms_enabled = true; |
| 344 | if (sirfport->uart_reg->uart_type == SIRF_REAL_UART) { |
| 345 | wr_regl(port, ureg->sirfsoc_afc_ctrl, |
| 346 | rd_regl(port, ureg->sirfsoc_afc_ctrl) | |
| 347 | SIRFUART_AFC_TX_EN | SIRFUART_AFC_RX_EN); |
Barry Song | 057badd | 2015-01-03 17:02:57 +0800 | [diff] [blame] | 348 | if (!sirfport->is_atlas7) |
Qipan Li | 2eb5618 | 2013-08-15 06:52:15 +0800 | [diff] [blame] | 349 | wr_regl(port, ureg->sirfsoc_int_en_reg, |
| 350 | rd_regl(port, ureg->sirfsoc_int_en_reg) |
| 351 | | uint_en->sirfsoc_cts_en); |
| 352 | else |
| 353 | wr_regl(port, ureg->sirfsoc_int_en_reg, |
| 354 | uint_en->sirfsoc_cts_en); |
Qipan Li | 5df8311 | 2013-08-12 18:15:35 +0800 | [diff] [blame] | 355 | } else |
Qipan Li | 2eb5618 | 2013-08-15 06:52:15 +0800 | [diff] [blame] | 356 | enable_irq(gpio_to_irq(sirfport->cts_gpio)); |
Rong Wang | 161e773 | 2011-11-17 23:17:04 +0800 | [diff] [blame] | 357 | } |
| 358 | |
| 359 | static void sirfsoc_uart_break_ctl(struct uart_port *port, int break_state) |
| 360 | { |
Qipan Li | 5df8311 | 2013-08-12 18:15:35 +0800 | [diff] [blame] | 361 | struct sirfsoc_uart_port *sirfport = to_sirfport(port); |
| 362 | struct sirfsoc_register *ureg = &sirfport->uart_reg->uart_reg; |
| 363 | if (sirfport->uart_reg->uart_type == SIRF_REAL_UART) { |
| 364 | unsigned long ulcon = rd_regl(port, ureg->sirfsoc_line_ctrl); |
| 365 | if (break_state) |
| 366 | ulcon |= SIRFUART_SET_BREAK; |
| 367 | else |
| 368 | ulcon &= ~SIRFUART_SET_BREAK; |
| 369 | wr_regl(port, ureg->sirfsoc_line_ctrl, ulcon); |
| 370 | } |
Rong Wang | 161e773 | 2011-11-17 23:17:04 +0800 | [diff] [blame] | 371 | } |
| 372 | |
| 373 | static unsigned int |
| 374 | sirfsoc_uart_pio_rx_chars(struct uart_port *port, unsigned int max_rx_count) |
| 375 | { |
Qipan Li | 5df8311 | 2013-08-12 18:15:35 +0800 | [diff] [blame] | 376 | struct sirfsoc_uart_port *sirfport = to_sirfport(port); |
| 377 | struct sirfsoc_register *ureg = &sirfport->uart_reg->uart_reg; |
| 378 | struct sirfsoc_fifo_status *ufifo_st = &sirfport->uart_reg->fifo_status; |
Rong Wang | 161e773 | 2011-11-17 23:17:04 +0800 | [diff] [blame] | 379 | unsigned int ch, rx_count = 0; |
Qipan Li | 5df8311 | 2013-08-12 18:15:35 +0800 | [diff] [blame] | 380 | struct tty_struct *tty; |
| 381 | tty = tty_port_tty_get(&port->state->port); |
| 382 | if (!tty) |
| 383 | return -ENODEV; |
| 384 | while (!(rd_regl(port, ureg->sirfsoc_rx_fifo_status) & |
Qipan Li | cb4595a | 2015-04-29 06:45:09 +0000 | [diff] [blame] | 385 | ufifo_st->ff_empty(port))) { |
Qipan Li | 5df8311 | 2013-08-12 18:15:35 +0800 | [diff] [blame] | 386 | ch = rd_regl(port, ureg->sirfsoc_rx_fifo_data) | |
| 387 | SIRFUART_DUMMY_READ; |
Rong Wang | 161e773 | 2011-11-17 23:17:04 +0800 | [diff] [blame] | 388 | if (unlikely(uart_handle_sysrq_char(port, ch))) |
| 389 | continue; |
| 390 | uart_insert_char(port, 0, 0, ch, TTY_NORMAL); |
| 391 | rx_count++; |
| 392 | if (rx_count >= max_rx_count) |
| 393 | break; |
| 394 | } |
| 395 | |
Qipan Li | 8316d04 | 2013-08-19 11:47:53 +0800 | [diff] [blame] | 396 | sirfport->rx_io_count += rx_count; |
Rong Wang | 161e773 | 2011-11-17 23:17:04 +0800 | [diff] [blame] | 397 | port->icount.rx += rx_count; |
Viresh Kumar | 8b9ade9 | 2013-08-19 20:14:28 +0530 | [diff] [blame] | 398 | |
Rong Wang | 161e773 | 2011-11-17 23:17:04 +0800 | [diff] [blame] | 399 | return rx_count; |
| 400 | } |
| 401 | |
| 402 | static unsigned int |
| 403 | sirfsoc_uart_pio_tx_chars(struct sirfsoc_uart_port *sirfport, int count) |
| 404 | { |
| 405 | struct uart_port *port = &sirfport->port; |
Qipan Li | 5df8311 | 2013-08-12 18:15:35 +0800 | [diff] [blame] | 406 | struct sirfsoc_register *ureg = &sirfport->uart_reg->uart_reg; |
| 407 | struct sirfsoc_fifo_status *ufifo_st = &sirfport->uart_reg->fifo_status; |
Rong Wang | 161e773 | 2011-11-17 23:17:04 +0800 | [diff] [blame] | 408 | struct circ_buf *xmit = &port->state->xmit; |
| 409 | unsigned int num_tx = 0; |
| 410 | while (!uart_circ_empty(xmit) && |
Qipan Li | 5df8311 | 2013-08-12 18:15:35 +0800 | [diff] [blame] | 411 | !(rd_regl(port, ureg->sirfsoc_tx_fifo_status) & |
Qipan Li | cb4595a | 2015-04-29 06:45:09 +0000 | [diff] [blame] | 412 | ufifo_st->ff_full(port)) && |
Rong Wang | 161e773 | 2011-11-17 23:17:04 +0800 | [diff] [blame] | 413 | count--) { |
Qipan Li | 5df8311 | 2013-08-12 18:15:35 +0800 | [diff] [blame] | 414 | wr_regl(port, ureg->sirfsoc_tx_fifo_data, |
| 415 | xmit->buf[xmit->tail]); |
Rong Wang | 161e773 | 2011-11-17 23:17:04 +0800 | [diff] [blame] | 416 | xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1); |
| 417 | port->icount.tx++; |
| 418 | num_tx++; |
| 419 | } |
| 420 | if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS) |
| 421 | uart_write_wakeup(port); |
| 422 | return num_tx; |
| 423 | } |
| 424 | |
Qipan Li | 8316d04 | 2013-08-19 11:47:53 +0800 | [diff] [blame] | 425 | static void sirfsoc_uart_tx_dma_complete_callback(void *param) |
| 426 | { |
| 427 | struct sirfsoc_uart_port *sirfport = (struct sirfsoc_uart_port *)param; |
| 428 | struct uart_port *port = &sirfport->port; |
| 429 | struct circ_buf *xmit = &port->state->xmit; |
| 430 | unsigned long flags; |
| 431 | |
Qipan Li | 07d410e | 2014-05-26 19:02:07 +0800 | [diff] [blame] | 432 | spin_lock_irqsave(&port->lock, flags); |
Qipan Li | 8316d04 | 2013-08-19 11:47:53 +0800 | [diff] [blame] | 433 | xmit->tail = (xmit->tail + sirfport->transfer_size) & |
| 434 | (UART_XMIT_SIZE - 1); |
| 435 | port->icount.tx += sirfport->transfer_size; |
| 436 | if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS) |
| 437 | uart_write_wakeup(port); |
| 438 | if (sirfport->tx_dma_addr) |
| 439 | dma_unmap_single(port->dev, sirfport->tx_dma_addr, |
| 440 | sirfport->transfer_size, DMA_TO_DEVICE); |
Qipan Li | 8316d04 | 2013-08-19 11:47:53 +0800 | [diff] [blame] | 441 | sirfport->tx_dma_state = TX_DMA_IDLE; |
| 442 | sirfsoc_uart_tx_with_dma(sirfport); |
Qipan Li | 07d410e | 2014-05-26 19:02:07 +0800 | [diff] [blame] | 443 | spin_unlock_irqrestore(&port->lock, flags); |
Qipan Li | 8316d04 | 2013-08-19 11:47:53 +0800 | [diff] [blame] | 444 | } |
| 445 | |
| 446 | static void sirfsoc_uart_insert_rx_buf_to_tty( |
| 447 | struct sirfsoc_uart_port *sirfport, int count) |
| 448 | { |
| 449 | struct uart_port *port = &sirfport->port; |
| 450 | struct tty_port *tport = &port->state->port; |
| 451 | int inserted; |
| 452 | |
| 453 | inserted = tty_insert_flip_string(tport, |
| 454 | sirfport->rx_dma_items[sirfport->rx_completed].xmit.buf, count); |
| 455 | port->icount.rx += inserted; |
Qipan Li | 8316d04 | 2013-08-19 11:47:53 +0800 | [diff] [blame] | 456 | } |
| 457 | |
| 458 | static void sirfsoc_rx_submit_one_dma_desc(struct uart_port *port, int index) |
| 459 | { |
| 460 | struct sirfsoc_uart_port *sirfport = to_sirfport(port); |
| 461 | |
| 462 | sirfport->rx_dma_items[index].xmit.tail = |
| 463 | sirfport->rx_dma_items[index].xmit.head = 0; |
| 464 | sirfport->rx_dma_items[index].desc = |
| 465 | dmaengine_prep_slave_single(sirfport->rx_dma_chan, |
| 466 | sirfport->rx_dma_items[index].dma_addr, SIRFSOC_RX_DMA_BUF_SIZE, |
| 467 | DMA_DEV_TO_MEM, DMA_PREP_INTERRUPT); |
Qipan Li | c1b7ac6 | 2015-05-14 06:45:21 +0000 | [diff] [blame^] | 468 | if (IS_ERR_OR_NULL(sirfport->rx_dma_items[index].desc)) { |
Qipan Li | 8316d04 | 2013-08-19 11:47:53 +0800 | [diff] [blame] | 469 | dev_err(port->dev, "DMA slave single fail\n"); |
| 470 | return; |
| 471 | } |
| 472 | sirfport->rx_dma_items[index].desc->callback = |
| 473 | sirfsoc_uart_rx_dma_complete_callback; |
| 474 | sirfport->rx_dma_items[index].desc->callback_param = sirfport; |
| 475 | sirfport->rx_dma_items[index].cookie = |
| 476 | dmaengine_submit(sirfport->rx_dma_items[index].desc); |
| 477 | dma_async_issue_pending(sirfport->rx_dma_chan); |
| 478 | } |
| 479 | |
| 480 | static void sirfsoc_rx_tmo_process_tl(unsigned long param) |
| 481 | { |
| 482 | struct sirfsoc_uart_port *sirfport = (struct sirfsoc_uart_port *)param; |
| 483 | struct uart_port *port = &sirfport->port; |
| 484 | struct sirfsoc_register *ureg = &sirfport->uart_reg->uart_reg; |
| 485 | struct sirfsoc_int_en *uint_en = &sirfport->uart_reg->uart_int_en; |
| 486 | struct sirfsoc_int_status *uint_st = &sirfport->uart_reg->uart_int_st; |
| 487 | unsigned int count; |
Qipan Li | df8d4aa | 2014-01-03 15:44:08 +0800 | [diff] [blame] | 488 | struct dma_tx_state tx_state; |
Qipan Li | c1b7ac6 | 2015-05-14 06:45:21 +0000 | [diff] [blame^] | 489 | unsigned long flags; |
Qipan Li | 8316d04 | 2013-08-19 11:47:53 +0800 | [diff] [blame] | 490 | |
Qipan Li | 07d410e | 2014-05-26 19:02:07 +0800 | [diff] [blame] | 491 | spin_lock_irqsave(&port->lock, flags); |
Qipan Li | df8d4aa | 2014-01-03 15:44:08 +0800 | [diff] [blame] | 492 | while (DMA_COMPLETE == dmaengine_tx_status(sirfport->rx_dma_chan, |
Qipan Li | c1b7ac6 | 2015-05-14 06:45:21 +0000 | [diff] [blame^] | 493 | sirfport->rx_dma_items[sirfport->rx_completed].cookie, |
| 494 | &tx_state)) { |
Qipan Li | 8316d04 | 2013-08-19 11:47:53 +0800 | [diff] [blame] | 495 | sirfsoc_uart_insert_rx_buf_to_tty(sirfport, |
| 496 | SIRFSOC_RX_DMA_BUF_SIZE); |
Qipan Li | 59f8a62 | 2013-09-21 09:02:10 +0800 | [diff] [blame] | 497 | sirfport->rx_completed++; |
Qipan Li | 8316d04 | 2013-08-19 11:47:53 +0800 | [diff] [blame] | 498 | sirfport->rx_completed %= SIRFSOC_RX_LOOP_BUF_CNT; |
| 499 | } |
| 500 | count = CIRC_CNT(sirfport->rx_dma_items[sirfport->rx_issued].xmit.head, |
| 501 | sirfport->rx_dma_items[sirfport->rx_issued].xmit.tail, |
| 502 | SIRFSOC_RX_DMA_BUF_SIZE); |
| 503 | if (count > 0) |
| 504 | sirfsoc_uart_insert_rx_buf_to_tty(sirfport, count); |
| 505 | wr_regl(port, ureg->sirfsoc_rx_dma_io_ctrl, |
| 506 | rd_regl(port, ureg->sirfsoc_rx_dma_io_ctrl) | |
| 507 | SIRFUART_IO_MODE); |
Qipan Li | fb78b81 | 2014-01-27 14:23:39 +0800 | [diff] [blame] | 508 | sirfsoc_uart_pio_rx_chars(port, 4 - sirfport->rx_io_count); |
Qipan Li | 8316d04 | 2013-08-19 11:47:53 +0800 | [diff] [blame] | 509 | if (sirfport->rx_io_count == 4) { |
Qipan Li | 8316d04 | 2013-08-19 11:47:53 +0800 | [diff] [blame] | 510 | sirfport->rx_io_count = 0; |
| 511 | wr_regl(port, ureg->sirfsoc_int_st_reg, |
| 512 | uint_st->sirfsoc_rx_done); |
Barry Song | 057badd | 2015-01-03 17:02:57 +0800 | [diff] [blame] | 513 | if (!sirfport->is_atlas7) |
Qipan Li | 8316d04 | 2013-08-19 11:47:53 +0800 | [diff] [blame] | 514 | wr_regl(port, ureg->sirfsoc_int_en_reg, |
| 515 | rd_regl(port, ureg->sirfsoc_int_en_reg) & |
| 516 | ~(uint_en->sirfsoc_rx_done_en)); |
| 517 | else |
Qipan Li | c1b7ac6 | 2015-05-14 06:45:21 +0000 | [diff] [blame^] | 518 | wr_regl(port, ureg->sirfsoc_int_en_clr_reg, |
Qipan Li | 8316d04 | 2013-08-19 11:47:53 +0800 | [diff] [blame] | 519 | uint_en->sirfsoc_rx_done_en); |
Qipan Li | 8316d04 | 2013-08-19 11:47:53 +0800 | [diff] [blame] | 520 | sirfsoc_uart_start_next_rx_dma(port); |
| 521 | } else { |
Qipan Li | 8316d04 | 2013-08-19 11:47:53 +0800 | [diff] [blame] | 522 | wr_regl(port, ureg->sirfsoc_int_st_reg, |
| 523 | uint_st->sirfsoc_rx_done); |
Barry Song | 057badd | 2015-01-03 17:02:57 +0800 | [diff] [blame] | 524 | if (!sirfport->is_atlas7) |
Qipan Li | 8316d04 | 2013-08-19 11:47:53 +0800 | [diff] [blame] | 525 | wr_regl(port, ureg->sirfsoc_int_en_reg, |
| 526 | rd_regl(port, ureg->sirfsoc_int_en_reg) | |
| 527 | (uint_en->sirfsoc_rx_done_en)); |
| 528 | else |
| 529 | wr_regl(port, ureg->sirfsoc_int_en_reg, |
| 530 | uint_en->sirfsoc_rx_done_en); |
Qipan Li | 8316d04 | 2013-08-19 11:47:53 +0800 | [diff] [blame] | 531 | } |
Qipan Li | 07d410e | 2014-05-26 19:02:07 +0800 | [diff] [blame] | 532 | spin_unlock_irqrestore(&port->lock, flags); |
| 533 | tty_flip_buffer_push(&port->state->port); |
Qipan Li | 8316d04 | 2013-08-19 11:47:53 +0800 | [diff] [blame] | 534 | } |
| 535 | |
| 536 | static void sirfsoc_uart_handle_rx_tmo(struct sirfsoc_uart_port *sirfport) |
| 537 | { |
| 538 | struct uart_port *port = &sirfport->port; |
| 539 | struct sirfsoc_register *ureg = &sirfport->uart_reg->uart_reg; |
| 540 | struct sirfsoc_int_en *uint_en = &sirfport->uart_reg->uart_int_en; |
| 541 | struct dma_tx_state tx_state; |
Qipan Li | 8316d04 | 2013-08-19 11:47:53 +0800 | [diff] [blame] | 542 | dmaengine_tx_status(sirfport->rx_dma_chan, |
| 543 | sirfport->rx_dma_items[sirfport->rx_issued].cookie, &tx_state); |
| 544 | dmaengine_terminate_all(sirfport->rx_dma_chan); |
| 545 | sirfport->rx_dma_items[sirfport->rx_issued].xmit.head = |
| 546 | SIRFSOC_RX_DMA_BUF_SIZE - tx_state.residue; |
Barry Song | 057badd | 2015-01-03 17:02:57 +0800 | [diff] [blame] | 547 | if (!sirfport->is_atlas7) |
Qipan Li | 8316d04 | 2013-08-19 11:47:53 +0800 | [diff] [blame] | 548 | wr_regl(port, ureg->sirfsoc_int_en_reg, |
| 549 | rd_regl(port, ureg->sirfsoc_int_en_reg) & |
| 550 | ~(uint_en->sirfsoc_rx_timeout_en)); |
| 551 | else |
Qipan Li | c1b7ac6 | 2015-05-14 06:45:21 +0000 | [diff] [blame^] | 552 | wr_regl(port, ureg->sirfsoc_int_en_clr_reg, |
Qipan Li | 8316d04 | 2013-08-19 11:47:53 +0800 | [diff] [blame] | 553 | uint_en->sirfsoc_rx_timeout_en); |
Qipan Li | 8316d04 | 2013-08-19 11:47:53 +0800 | [diff] [blame] | 554 | tasklet_schedule(&sirfport->rx_tmo_process_tasklet); |
| 555 | } |
| 556 | |
| 557 | static void sirfsoc_uart_handle_rx_done(struct sirfsoc_uart_port *sirfport) |
| 558 | { |
| 559 | struct uart_port *port = &sirfport->port; |
| 560 | struct sirfsoc_register *ureg = &sirfport->uart_reg->uart_reg; |
| 561 | struct sirfsoc_int_en *uint_en = &sirfport->uart_reg->uart_int_en; |
| 562 | struct sirfsoc_int_status *uint_st = &sirfport->uart_reg->uart_int_st; |
| 563 | |
| 564 | sirfsoc_uart_pio_rx_chars(port, 4 - sirfport->rx_io_count); |
| 565 | if (sirfport->rx_io_count == 4) { |
| 566 | sirfport->rx_io_count = 0; |
Barry Song | 057badd | 2015-01-03 17:02:57 +0800 | [diff] [blame] | 567 | if (!sirfport->is_atlas7) |
Qipan Li | 8316d04 | 2013-08-19 11:47:53 +0800 | [diff] [blame] | 568 | wr_regl(port, ureg->sirfsoc_int_en_reg, |
| 569 | rd_regl(port, ureg->sirfsoc_int_en_reg) & |
| 570 | ~(uint_en->sirfsoc_rx_done_en)); |
| 571 | else |
Qipan Li | c1b7ac6 | 2015-05-14 06:45:21 +0000 | [diff] [blame^] | 572 | wr_regl(port, ureg->sirfsoc_int_en_clr_reg, |
Qipan Li | 8316d04 | 2013-08-19 11:47:53 +0800 | [diff] [blame] | 573 | uint_en->sirfsoc_rx_done_en); |
| 574 | wr_regl(port, ureg->sirfsoc_int_st_reg, |
| 575 | uint_st->sirfsoc_rx_timeout); |
| 576 | sirfsoc_uart_start_next_rx_dma(port); |
| 577 | } |
| 578 | } |
| 579 | |
Rong Wang | 161e773 | 2011-11-17 23:17:04 +0800 | [diff] [blame] | 580 | static irqreturn_t sirfsoc_uart_isr(int irq, void *dev_id) |
| 581 | { |
| 582 | unsigned long intr_status; |
| 583 | unsigned long cts_status; |
| 584 | unsigned long flag = TTY_NORMAL; |
| 585 | struct sirfsoc_uart_port *sirfport = (struct sirfsoc_uart_port *)dev_id; |
| 586 | struct uart_port *port = &sirfport->port; |
Qipan Li | 5df8311 | 2013-08-12 18:15:35 +0800 | [diff] [blame] | 587 | struct sirfsoc_register *ureg = &sirfport->uart_reg->uart_reg; |
| 588 | struct sirfsoc_fifo_status *ufifo_st = &sirfport->uart_reg->fifo_status; |
| 589 | struct sirfsoc_int_status *uint_st = &sirfport->uart_reg->uart_int_st; |
| 590 | struct sirfsoc_int_en *uint_en = &sirfport->uart_reg->uart_int_en; |
Rong Wang | 161e773 | 2011-11-17 23:17:04 +0800 | [diff] [blame] | 591 | struct uart_state *state = port->state; |
| 592 | struct circ_buf *xmit = &port->state->xmit; |
Barry Song | 5425e03 | 2012-12-25 17:32:04 +0800 | [diff] [blame] | 593 | spin_lock(&port->lock); |
Qipan Li | 5df8311 | 2013-08-12 18:15:35 +0800 | [diff] [blame] | 594 | intr_status = rd_regl(port, ureg->sirfsoc_int_st_reg); |
| 595 | wr_regl(port, ureg->sirfsoc_int_st_reg, intr_status); |
Qipan Li | 8316d04 | 2013-08-19 11:47:53 +0800 | [diff] [blame] | 596 | intr_status &= rd_regl(port, ureg->sirfsoc_int_en_reg); |
Qipan Li | c1b7ac6 | 2015-05-14 06:45:21 +0000 | [diff] [blame^] | 597 | if (unlikely(intr_status & (SIRFUART_ERR_INT_STAT(uint_st, |
| 598 | sirfport->uart_reg->uart_type)))) { |
Qipan Li | 5df8311 | 2013-08-12 18:15:35 +0800 | [diff] [blame] | 599 | if (intr_status & uint_st->sirfsoc_rxd_brk) { |
| 600 | port->icount.brk++; |
Rong Wang | 161e773 | 2011-11-17 23:17:04 +0800 | [diff] [blame] | 601 | if (uart_handle_break(port)) |
| 602 | goto recv_char; |
Rong Wang | 161e773 | 2011-11-17 23:17:04 +0800 | [diff] [blame] | 603 | } |
Qipan Li | 5df8311 | 2013-08-12 18:15:35 +0800 | [diff] [blame] | 604 | if (intr_status & uint_st->sirfsoc_rx_oflow) |
Rong Wang | 161e773 | 2011-11-17 23:17:04 +0800 | [diff] [blame] | 605 | port->icount.overrun++; |
Qipan Li | 5df8311 | 2013-08-12 18:15:35 +0800 | [diff] [blame] | 606 | if (intr_status & uint_st->sirfsoc_frm_err) { |
Rong Wang | 161e773 | 2011-11-17 23:17:04 +0800 | [diff] [blame] | 607 | port->icount.frame++; |
| 608 | flag = TTY_FRAME; |
| 609 | } |
Qipan Li | 5df8311 | 2013-08-12 18:15:35 +0800 | [diff] [blame] | 610 | if (intr_status & uint_st->sirfsoc_parity_err) |
Rong Wang | 161e773 | 2011-11-17 23:17:04 +0800 | [diff] [blame] | 611 | flag = TTY_PARITY; |
Qipan Li | 5df8311 | 2013-08-12 18:15:35 +0800 | [diff] [blame] | 612 | wr_regl(port, ureg->sirfsoc_rx_fifo_op, SIRFUART_FIFO_RESET); |
| 613 | wr_regl(port, ureg->sirfsoc_rx_fifo_op, 0); |
| 614 | wr_regl(port, ureg->sirfsoc_rx_fifo_op, SIRFUART_FIFO_START); |
Rong Wang | 161e773 | 2011-11-17 23:17:04 +0800 | [diff] [blame] | 615 | intr_status &= port->read_status_mask; |
| 616 | uart_insert_char(port, intr_status, |
Qipan Li | 5df8311 | 2013-08-12 18:15:35 +0800 | [diff] [blame] | 617 | uint_en->sirfsoc_rx_oflow_en, 0, flag); |
Rong Wang | 161e773 | 2011-11-17 23:17:04 +0800 | [diff] [blame] | 618 | } |
| 619 | recv_char: |
Qipan Li | 5df8311 | 2013-08-12 18:15:35 +0800 | [diff] [blame] | 620 | if ((sirfport->uart_reg->uart_type == SIRF_REAL_UART) && |
Qipan Li | 8316d04 | 2013-08-19 11:47:53 +0800 | [diff] [blame] | 621 | (intr_status & SIRFUART_CTS_INT_ST(uint_st)) && |
| 622 | !sirfport->tx_dma_state) { |
Qipan Li | 5df8311 | 2013-08-12 18:15:35 +0800 | [diff] [blame] | 623 | cts_status = rd_regl(port, ureg->sirfsoc_afc_ctrl) & |
| 624 | SIRFUART_AFC_CTS_STATUS; |
| 625 | if (cts_status != 0) |
| 626 | cts_status = 0; |
| 627 | else |
| 628 | cts_status = 1; |
| 629 | uart_handle_cts_change(port, cts_status); |
| 630 | wake_up_interruptible(&state->port.delta_msr_wait); |
Rong Wang | 161e773 | 2011-11-17 23:17:04 +0800 | [diff] [blame] | 631 | } |
Qipan Li | 9be16b3 | 2014-01-30 13:57:29 +0800 | [diff] [blame] | 632 | if (sirfport->rx_dma_chan) { |
Qipan Li | 8316d04 | 2013-08-19 11:47:53 +0800 | [diff] [blame] | 633 | if (intr_status & uint_st->sirfsoc_rx_timeout) |
| 634 | sirfsoc_uart_handle_rx_tmo(sirfport); |
| 635 | if (intr_status & uint_st->sirfsoc_rx_done) |
| 636 | sirfsoc_uart_handle_rx_done(sirfport); |
Qipan Li | c1b7ac6 | 2015-05-14 06:45:21 +0000 | [diff] [blame^] | 637 | } else if (intr_status & SIRFUART_RX_IO_INT_ST(uint_st)) { |
| 638 | /* |
| 639 | * chip will trigger continuous RX_TIMEOUT interrupt |
| 640 | * in RXFIFO empty and not trigger if RXFIFO recevice |
| 641 | * data in limit time, original method use RX_TIMEOUT |
| 642 | * will trigger lots of useless interrupt in RXFIFO |
| 643 | * empty.RXFIFO received one byte will trigger RX_DONE |
| 644 | * interrupt.use RX_DONE to wait for data received |
| 645 | * into RXFIFO, use RX_THD/RX_FULL for lots data receive |
| 646 | * and use RX_TIMEOUT for the last left data. |
| 647 | */ |
| 648 | if (intr_status & uint_st->sirfsoc_rx_done) { |
| 649 | if (!sirfport->is_atlas7) { |
| 650 | wr_regl(port, ureg->sirfsoc_int_en_reg, |
| 651 | rd_regl(port, ureg->sirfsoc_int_en_reg) |
| 652 | & ~(uint_en->sirfsoc_rx_done_en)); |
| 653 | wr_regl(port, ureg->sirfsoc_int_en_reg, |
| 654 | rd_regl(port, ureg->sirfsoc_int_en_reg) |
| 655 | | (uint_en->sirfsoc_rx_timeout_en)); |
| 656 | } else { |
| 657 | wr_regl(port, ureg->sirfsoc_int_en_clr_reg, |
| 658 | uint_en->sirfsoc_rx_done_en); |
| 659 | wr_regl(port, ureg->sirfsoc_int_en_reg, |
| 660 | uint_en->sirfsoc_rx_timeout_en); |
| 661 | } |
| 662 | } else { |
| 663 | if (intr_status & uint_st->sirfsoc_rx_timeout) { |
| 664 | if (!sirfport->is_atlas7) { |
| 665 | wr_regl(port, ureg->sirfsoc_int_en_reg, |
| 666 | rd_regl(port, ureg->sirfsoc_int_en_reg) |
| 667 | & ~(uint_en->sirfsoc_rx_timeout_en)); |
| 668 | wr_regl(port, ureg->sirfsoc_int_en_reg, |
| 669 | rd_regl(port, ureg->sirfsoc_int_en_reg) |
| 670 | | (uint_en->sirfsoc_rx_done_en)); |
| 671 | } else { |
| 672 | wr_regl(port, |
| 673 | ureg->sirfsoc_int_en_clr_reg, |
| 674 | uint_en->sirfsoc_rx_timeout_en); |
| 675 | wr_regl(port, ureg->sirfsoc_int_en_reg, |
| 676 | uint_en->sirfsoc_rx_done_en); |
| 677 | } |
| 678 | } |
Qipan Li | cb4595a | 2015-04-29 06:45:09 +0000 | [diff] [blame] | 679 | sirfsoc_uart_pio_rx_chars(port, port->fifosize); |
Qipan Li | c1b7ac6 | 2015-05-14 06:45:21 +0000 | [diff] [blame^] | 680 | } |
Qipan Li | 8316d04 | 2013-08-19 11:47:53 +0800 | [diff] [blame] | 681 | } |
Qipan Li | 07d410e | 2014-05-26 19:02:07 +0800 | [diff] [blame] | 682 | spin_unlock(&port->lock); |
| 683 | tty_flip_buffer_push(&state->port); |
| 684 | spin_lock(&port->lock); |
Qipan Li | 5df8311 | 2013-08-12 18:15:35 +0800 | [diff] [blame] | 685 | if (intr_status & uint_st->sirfsoc_txfifo_empty) { |
Qipan Li | 9be16b3 | 2014-01-30 13:57:29 +0800 | [diff] [blame] | 686 | if (sirfport->tx_dma_chan) |
Qipan Li | 8316d04 | 2013-08-19 11:47:53 +0800 | [diff] [blame] | 687 | sirfsoc_uart_tx_with_dma(sirfport); |
| 688 | else { |
| 689 | if (uart_circ_empty(xmit) || uart_tx_stopped(port)) { |
| 690 | spin_unlock(&port->lock); |
| 691 | return IRQ_HANDLED; |
| 692 | } else { |
| 693 | sirfsoc_uart_pio_tx_chars(sirfport, |
Qipan Li | cb4595a | 2015-04-29 06:45:09 +0000 | [diff] [blame] | 694 | port->fifosize); |
Qipan Li | 8316d04 | 2013-08-19 11:47:53 +0800 | [diff] [blame] | 695 | if ((uart_circ_empty(xmit)) && |
Qipan Li | 5df8311 | 2013-08-12 18:15:35 +0800 | [diff] [blame] | 696 | (rd_regl(port, ureg->sirfsoc_tx_fifo_status) & |
Qipan Li | cb4595a | 2015-04-29 06:45:09 +0000 | [diff] [blame] | 697 | ufifo_st->ff_empty(port))) |
Qipan Li | 8316d04 | 2013-08-19 11:47:53 +0800 | [diff] [blame] | 698 | sirfsoc_uart_stop_tx(port); |
| 699 | } |
Rong Wang | 161e773 | 2011-11-17 23:17:04 +0800 | [diff] [blame] | 700 | } |
| 701 | } |
Barry Song | 5425e03 | 2012-12-25 17:32:04 +0800 | [diff] [blame] | 702 | spin_unlock(&port->lock); |
Qipan Li | 07d410e | 2014-05-26 19:02:07 +0800 | [diff] [blame] | 703 | |
Rong Wang | 161e773 | 2011-11-17 23:17:04 +0800 | [diff] [blame] | 704 | return IRQ_HANDLED; |
| 705 | } |
| 706 | |
Qipan Li | 8316d04 | 2013-08-19 11:47:53 +0800 | [diff] [blame] | 707 | static void sirfsoc_uart_rx_dma_complete_tl(unsigned long param) |
| 708 | { |
| 709 | struct sirfsoc_uart_port *sirfport = (struct sirfsoc_uart_port *)param; |
| 710 | struct uart_port *port = &sirfport->port; |
Qipan Li | 59f8a62 | 2013-09-21 09:02:10 +0800 | [diff] [blame] | 711 | struct sirfsoc_register *ureg = &sirfport->uart_reg->uart_reg; |
| 712 | struct sirfsoc_int_en *uint_en = &sirfport->uart_reg->uart_int_en; |
Qipan Li | df8d4aa | 2014-01-03 15:44:08 +0800 | [diff] [blame] | 713 | struct dma_tx_state tx_state; |
Qipan Li | c1b7ac6 | 2015-05-14 06:45:21 +0000 | [diff] [blame^] | 714 | unsigned long flags; |
Daniel Thompson | 58eb97c | 2014-05-29 11:13:43 +0100 | [diff] [blame] | 715 | spin_lock_irqsave(&port->lock, flags); |
Qipan Li | df8d4aa | 2014-01-03 15:44:08 +0800 | [diff] [blame] | 716 | while (DMA_COMPLETE == dmaengine_tx_status(sirfport->rx_dma_chan, |
Qipan Li | c1b7ac6 | 2015-05-14 06:45:21 +0000 | [diff] [blame^] | 717 | sirfport->rx_dma_items[sirfport->rx_completed].cookie, |
| 718 | &tx_state)) { |
Qipan Li | 8316d04 | 2013-08-19 11:47:53 +0800 | [diff] [blame] | 719 | sirfsoc_uart_insert_rx_buf_to_tty(sirfport, |
| 720 | SIRFSOC_RX_DMA_BUF_SIZE); |
Qipan Li | 59f8a62 | 2013-09-21 09:02:10 +0800 | [diff] [blame] | 721 | if (rd_regl(port, ureg->sirfsoc_int_en_reg) & |
| 722 | uint_en->sirfsoc_rx_timeout_en) |
| 723 | sirfsoc_rx_submit_one_dma_desc(port, |
| 724 | sirfport->rx_completed++); |
| 725 | else |
| 726 | sirfport->rx_completed++; |
Qipan Li | 8316d04 | 2013-08-19 11:47:53 +0800 | [diff] [blame] | 727 | sirfport->rx_completed %= SIRFSOC_RX_LOOP_BUF_CNT; |
| 728 | } |
Qipan Li | 07d410e | 2014-05-26 19:02:07 +0800 | [diff] [blame] | 729 | spin_unlock_irqrestore(&port->lock, flags); |
| 730 | tty_flip_buffer_push(&port->state->port); |
Qipan Li | 8316d04 | 2013-08-19 11:47:53 +0800 | [diff] [blame] | 731 | } |
| 732 | |
| 733 | static void sirfsoc_uart_rx_dma_complete_callback(void *param) |
| 734 | { |
| 735 | struct sirfsoc_uart_port *sirfport = (struct sirfsoc_uart_port *)param; |
Qipan Li | 07d410e | 2014-05-26 19:02:07 +0800 | [diff] [blame] | 736 | unsigned long flags; |
| 737 | |
| 738 | spin_lock_irqsave(&sirfport->port.lock, flags); |
Qipan Li | 8316d04 | 2013-08-19 11:47:53 +0800 | [diff] [blame] | 739 | sirfport->rx_issued++; |
| 740 | sirfport->rx_issued %= SIRFSOC_RX_LOOP_BUF_CNT; |
Qipan Li | 8316d04 | 2013-08-19 11:47:53 +0800 | [diff] [blame] | 741 | tasklet_schedule(&sirfport->rx_dma_complete_tasklet); |
Qipan Li | 07d410e | 2014-05-26 19:02:07 +0800 | [diff] [blame] | 742 | spin_unlock_irqrestore(&sirfport->port.lock, flags); |
Qipan Li | 8316d04 | 2013-08-19 11:47:53 +0800 | [diff] [blame] | 743 | } |
| 744 | |
| 745 | /* submit rx dma task into dmaengine */ |
| 746 | static void sirfsoc_uart_start_next_rx_dma(struct uart_port *port) |
| 747 | { |
| 748 | struct sirfsoc_uart_port *sirfport = to_sirfport(port); |
| 749 | struct sirfsoc_register *ureg = &sirfport->uart_reg->uart_reg; |
| 750 | struct sirfsoc_int_en *uint_en = &sirfport->uart_reg->uart_int_en; |
Qipan Li | 8316d04 | 2013-08-19 11:47:53 +0800 | [diff] [blame] | 751 | int i; |
Qipan Li | 8316d04 | 2013-08-19 11:47:53 +0800 | [diff] [blame] | 752 | sirfport->rx_io_count = 0; |
| 753 | wr_regl(port, ureg->sirfsoc_rx_dma_io_ctrl, |
| 754 | rd_regl(port, ureg->sirfsoc_rx_dma_io_ctrl) & |
| 755 | ~SIRFUART_IO_MODE); |
Qipan Li | 8316d04 | 2013-08-19 11:47:53 +0800 | [diff] [blame] | 756 | for (i = 0; i < SIRFSOC_RX_LOOP_BUF_CNT; i++) |
| 757 | sirfsoc_rx_submit_one_dma_desc(port, i); |
| 758 | sirfport->rx_completed = sirfport->rx_issued = 0; |
Barry Song | 057badd | 2015-01-03 17:02:57 +0800 | [diff] [blame] | 759 | if (!sirfport->is_atlas7) |
Qipan Li | 8316d04 | 2013-08-19 11:47:53 +0800 | [diff] [blame] | 760 | wr_regl(port, ureg->sirfsoc_int_en_reg, |
| 761 | rd_regl(port, ureg->sirfsoc_int_en_reg) | |
Qipan Li | c1b7ac6 | 2015-05-14 06:45:21 +0000 | [diff] [blame^] | 762 | SIRFUART_RX_DMA_INT_EN(uint_en, |
| 763 | sirfport->uart_reg->uart_type)); |
Qipan Li | 8316d04 | 2013-08-19 11:47:53 +0800 | [diff] [blame] | 764 | else |
| 765 | wr_regl(port, ureg->sirfsoc_int_en_reg, |
Qipan Li | c1b7ac6 | 2015-05-14 06:45:21 +0000 | [diff] [blame^] | 766 | SIRFUART_RX_DMA_INT_EN(uint_en, |
| 767 | sirfport->uart_reg->uart_type)); |
Qipan Li | 8316d04 | 2013-08-19 11:47:53 +0800 | [diff] [blame] | 768 | } |
| 769 | |
Rong Wang | 161e773 | 2011-11-17 23:17:04 +0800 | [diff] [blame] | 770 | static void sirfsoc_uart_start_rx(struct uart_port *port) |
| 771 | { |
Barry Song | 909102d | 2013-08-07 13:35:38 +0800 | [diff] [blame] | 772 | struct sirfsoc_uart_port *sirfport = to_sirfport(port); |
Qipan Li | 5df8311 | 2013-08-12 18:15:35 +0800 | [diff] [blame] | 773 | struct sirfsoc_register *ureg = &sirfport->uart_reg->uart_reg; |
| 774 | struct sirfsoc_int_en *uint_en = &sirfport->uart_reg->uart_int_en; |
Qipan Li | 8316d04 | 2013-08-19 11:47:53 +0800 | [diff] [blame] | 775 | |
| 776 | sirfport->rx_io_count = 0; |
Qipan Li | 5df8311 | 2013-08-12 18:15:35 +0800 | [diff] [blame] | 777 | wr_regl(port, ureg->sirfsoc_rx_fifo_op, SIRFUART_FIFO_RESET); |
| 778 | wr_regl(port, ureg->sirfsoc_rx_fifo_op, 0); |
| 779 | wr_regl(port, ureg->sirfsoc_rx_fifo_op, SIRFUART_FIFO_START); |
Qipan Li | 9be16b3 | 2014-01-30 13:57:29 +0800 | [diff] [blame] | 780 | if (sirfport->rx_dma_chan) |
Qipan Li | 8316d04 | 2013-08-19 11:47:53 +0800 | [diff] [blame] | 781 | sirfsoc_uart_start_next_rx_dma(port); |
| 782 | else { |
Barry Song | 057badd | 2015-01-03 17:02:57 +0800 | [diff] [blame] | 783 | if (!sirfport->is_atlas7) |
Qipan Li | 8316d04 | 2013-08-19 11:47:53 +0800 | [diff] [blame] | 784 | wr_regl(port, ureg->sirfsoc_int_en_reg, |
| 785 | rd_regl(port, ureg->sirfsoc_int_en_reg) | |
Qipan Li | c1b7ac6 | 2015-05-14 06:45:21 +0000 | [diff] [blame^] | 786 | SIRFUART_RX_IO_INT_EN(uint_en, |
| 787 | sirfport->uart_reg->uart_type)); |
Qipan Li | 8316d04 | 2013-08-19 11:47:53 +0800 | [diff] [blame] | 788 | else |
| 789 | wr_regl(port, ureg->sirfsoc_int_en_reg, |
Qipan Li | c1b7ac6 | 2015-05-14 06:45:21 +0000 | [diff] [blame^] | 790 | SIRFUART_RX_IO_INT_EN(uint_en, |
| 791 | sirfport->uart_reg->uart_type)); |
Qipan Li | 8316d04 | 2013-08-19 11:47:53 +0800 | [diff] [blame] | 792 | } |
Rong Wang | 161e773 | 2011-11-17 23:17:04 +0800 | [diff] [blame] | 793 | } |
| 794 | |
| 795 | static unsigned int |
Qipan Li | 5df8311 | 2013-08-12 18:15:35 +0800 | [diff] [blame] | 796 | sirfsoc_usp_calc_sample_div(unsigned long set_rate, |
| 797 | unsigned long ioclk_rate, unsigned long *sample_reg) |
| 798 | { |
| 799 | unsigned long min_delta = ~0UL; |
| 800 | unsigned short sample_div; |
| 801 | unsigned long ioclk_div = 0; |
| 802 | unsigned long temp_delta; |
| 803 | |
Qipan Li | cb4595a | 2015-04-29 06:45:09 +0000 | [diff] [blame] | 804 | for (sample_div = SIRF_USP_MIN_SAMPLE_DIV; |
Qipan Li | 5df8311 | 2013-08-12 18:15:35 +0800 | [diff] [blame] | 805 | sample_div <= SIRF_MAX_SAMPLE_DIV; sample_div++) { |
| 806 | temp_delta = ioclk_rate - |
| 807 | (ioclk_rate + (set_rate * sample_div) / 2) |
| 808 | / (set_rate * sample_div) * set_rate * sample_div; |
| 809 | |
| 810 | temp_delta = (temp_delta > 0) ? temp_delta : -temp_delta; |
| 811 | if (temp_delta < min_delta) { |
| 812 | ioclk_div = (2 * ioclk_rate / |
| 813 | (set_rate * sample_div) + 1) / 2 - 1; |
| 814 | if (ioclk_div > SIRF_IOCLK_DIV_MAX) |
| 815 | continue; |
| 816 | min_delta = temp_delta; |
| 817 | *sample_reg = sample_div; |
| 818 | if (!temp_delta) |
| 819 | break; |
| 820 | } |
| 821 | } |
| 822 | return ioclk_div; |
| 823 | } |
| 824 | |
| 825 | static unsigned int |
| 826 | sirfsoc_uart_calc_sample_div(unsigned long baud_rate, |
| 827 | unsigned long ioclk_rate, unsigned long *set_baud) |
Rong Wang | 161e773 | 2011-11-17 23:17:04 +0800 | [diff] [blame] | 828 | { |
| 829 | unsigned long min_delta = ~0UL; |
| 830 | unsigned short sample_div; |
| 831 | unsigned int regv = 0; |
| 832 | unsigned long ioclk_div; |
| 833 | unsigned long baud_tmp; |
| 834 | int temp_delta; |
| 835 | |
| 836 | for (sample_div = SIRF_MIN_SAMPLE_DIV; |
| 837 | sample_div <= SIRF_MAX_SAMPLE_DIV; sample_div++) { |
| 838 | ioclk_div = (ioclk_rate / (baud_rate * (sample_div + 1))) - 1; |
| 839 | if (ioclk_div > SIRF_IOCLK_DIV_MAX) |
| 840 | continue; |
| 841 | baud_tmp = ioclk_rate / ((ioclk_div + 1) * (sample_div + 1)); |
| 842 | temp_delta = baud_tmp - baud_rate; |
| 843 | temp_delta = (temp_delta > 0) ? temp_delta : -temp_delta; |
| 844 | if (temp_delta < min_delta) { |
| 845 | regv = regv & (~SIRF_IOCLK_DIV_MASK); |
| 846 | regv = regv | ioclk_div; |
| 847 | regv = regv & (~SIRF_SAMPLE_DIV_MASK); |
| 848 | regv = regv | (sample_div << SIRF_SAMPLE_DIV_SHIFT); |
| 849 | min_delta = temp_delta; |
Qipan Li | 5df8311 | 2013-08-12 18:15:35 +0800 | [diff] [blame] | 850 | *set_baud = baud_tmp; |
Rong Wang | 161e773 | 2011-11-17 23:17:04 +0800 | [diff] [blame] | 851 | } |
| 852 | } |
| 853 | return regv; |
| 854 | } |
| 855 | |
| 856 | static void sirfsoc_uart_set_termios(struct uart_port *port, |
| 857 | struct ktermios *termios, |
| 858 | struct ktermios *old) |
| 859 | { |
| 860 | struct sirfsoc_uart_port *sirfport = to_sirfport(port); |
Qipan Li | 5df8311 | 2013-08-12 18:15:35 +0800 | [diff] [blame] | 861 | struct sirfsoc_register *ureg = &sirfport->uart_reg->uart_reg; |
| 862 | struct sirfsoc_int_en *uint_en = &sirfport->uart_reg->uart_int_en; |
Rong Wang | 161e773 | 2011-11-17 23:17:04 +0800 | [diff] [blame] | 863 | unsigned long config_reg = 0; |
| 864 | unsigned long baud_rate; |
Qipan Li | 5df8311 | 2013-08-12 18:15:35 +0800 | [diff] [blame] | 865 | unsigned long set_baud; |
Rong Wang | 161e773 | 2011-11-17 23:17:04 +0800 | [diff] [blame] | 866 | unsigned long flags; |
| 867 | unsigned long ic; |
| 868 | unsigned int clk_div_reg = 0; |
Qipan Li | 8316d04 | 2013-08-19 11:47:53 +0800 | [diff] [blame] | 869 | unsigned long txfifo_op_reg, ioclk_rate; |
Rong Wang | 161e773 | 2011-11-17 23:17:04 +0800 | [diff] [blame] | 870 | unsigned long rx_time_out; |
| 871 | int threshold_div; |
Qipan Li | 5df8311 | 2013-08-12 18:15:35 +0800 | [diff] [blame] | 872 | u32 data_bit_len, stop_bit_len, len_val; |
| 873 | unsigned long sample_div_reg = 0xf; |
| 874 | ioclk_rate = port->uartclk; |
Rong Wang | 161e773 | 2011-11-17 23:17:04 +0800 | [diff] [blame] | 875 | |
Rong Wang | 161e773 | 2011-11-17 23:17:04 +0800 | [diff] [blame] | 876 | switch (termios->c_cflag & CSIZE) { |
| 877 | default: |
| 878 | case CS8: |
Qipan Li | 5df8311 | 2013-08-12 18:15:35 +0800 | [diff] [blame] | 879 | data_bit_len = 8; |
Rong Wang | 161e773 | 2011-11-17 23:17:04 +0800 | [diff] [blame] | 880 | config_reg |= SIRFUART_DATA_BIT_LEN_8; |
| 881 | break; |
| 882 | case CS7: |
Qipan Li | 5df8311 | 2013-08-12 18:15:35 +0800 | [diff] [blame] | 883 | data_bit_len = 7; |
Rong Wang | 161e773 | 2011-11-17 23:17:04 +0800 | [diff] [blame] | 884 | config_reg |= SIRFUART_DATA_BIT_LEN_7; |
| 885 | break; |
| 886 | case CS6: |
Qipan Li | 5df8311 | 2013-08-12 18:15:35 +0800 | [diff] [blame] | 887 | data_bit_len = 6; |
Rong Wang | 161e773 | 2011-11-17 23:17:04 +0800 | [diff] [blame] | 888 | config_reg |= SIRFUART_DATA_BIT_LEN_6; |
| 889 | break; |
| 890 | case CS5: |
Qipan Li | 5df8311 | 2013-08-12 18:15:35 +0800 | [diff] [blame] | 891 | data_bit_len = 5; |
Rong Wang | 161e773 | 2011-11-17 23:17:04 +0800 | [diff] [blame] | 892 | config_reg |= SIRFUART_DATA_BIT_LEN_5; |
| 893 | break; |
| 894 | } |
Qipan Li | 5df8311 | 2013-08-12 18:15:35 +0800 | [diff] [blame] | 895 | if (termios->c_cflag & CSTOPB) { |
Rong Wang | 161e773 | 2011-11-17 23:17:04 +0800 | [diff] [blame] | 896 | config_reg |= SIRFUART_STOP_BIT_LEN_2; |
Qipan Li | 5df8311 | 2013-08-12 18:15:35 +0800 | [diff] [blame] | 897 | stop_bit_len = 2; |
| 898 | } else |
| 899 | stop_bit_len = 1; |
| 900 | |
Rong Wang | 161e773 | 2011-11-17 23:17:04 +0800 | [diff] [blame] | 901 | spin_lock_irqsave(&port->lock, flags); |
Qipan Li | 5df8311 | 2013-08-12 18:15:35 +0800 | [diff] [blame] | 902 | port->read_status_mask = uint_en->sirfsoc_rx_oflow_en; |
Rong Wang | 161e773 | 2011-11-17 23:17:04 +0800 | [diff] [blame] | 903 | port->ignore_status_mask = 0; |
Qipan Li | 5df8311 | 2013-08-12 18:15:35 +0800 | [diff] [blame] | 904 | if (sirfport->uart_reg->uart_type == SIRF_REAL_UART) { |
| 905 | if (termios->c_iflag & INPCK) |
| 906 | port->read_status_mask |= uint_en->sirfsoc_frm_err_en | |
| 907 | uint_en->sirfsoc_parity_err_en; |
Qipan Li | 2eb5618 | 2013-08-15 06:52:15 +0800 | [diff] [blame] | 908 | } else { |
Qipan Li | 5df8311 | 2013-08-12 18:15:35 +0800 | [diff] [blame] | 909 | if (termios->c_iflag & INPCK) |
| 910 | port->read_status_mask |= uint_en->sirfsoc_frm_err_en; |
| 911 | } |
Peter Hurley | ef8b9dd | 2014-06-16 08:10:41 -0400 | [diff] [blame] | 912 | if (termios->c_iflag & (IGNBRK | BRKINT | PARMRK)) |
Qipan Li | 5df8311 | 2013-08-12 18:15:35 +0800 | [diff] [blame] | 913 | port->read_status_mask |= uint_en->sirfsoc_rxd_brk_en; |
| 914 | if (sirfport->uart_reg->uart_type == SIRF_REAL_UART) { |
| 915 | if (termios->c_iflag & IGNPAR) |
| 916 | port->ignore_status_mask |= |
| 917 | uint_en->sirfsoc_frm_err_en | |
| 918 | uint_en->sirfsoc_parity_err_en; |
| 919 | if (termios->c_cflag & PARENB) { |
| 920 | if (termios->c_cflag & CMSPAR) { |
| 921 | if (termios->c_cflag & PARODD) |
| 922 | config_reg |= SIRFUART_STICK_BIT_MARK; |
| 923 | else |
| 924 | config_reg |= SIRFUART_STICK_BIT_SPACE; |
| 925 | } else if (termios->c_cflag & PARODD) { |
| 926 | config_reg |= SIRFUART_STICK_BIT_ODD; |
| 927 | } else { |
| 928 | config_reg |= SIRFUART_STICK_BIT_EVEN; |
| 929 | } |
Rong Wang | 161e773 | 2011-11-17 23:17:04 +0800 | [diff] [blame] | 930 | } |
Qipan Li | 2eb5618 | 2013-08-15 06:52:15 +0800 | [diff] [blame] | 931 | } else { |
Qipan Li | 5df8311 | 2013-08-12 18:15:35 +0800 | [diff] [blame] | 932 | if (termios->c_iflag & IGNPAR) |
| 933 | port->ignore_status_mask |= |
| 934 | uint_en->sirfsoc_frm_err_en; |
| 935 | if (termios->c_cflag & PARENB) |
| 936 | dev_warn(port->dev, |
| 937 | "USP-UART not support parity err\n"); |
| 938 | } |
| 939 | if (termios->c_iflag & IGNBRK) { |
| 940 | port->ignore_status_mask |= |
| 941 | uint_en->sirfsoc_rxd_brk_en; |
| 942 | if (termios->c_iflag & IGNPAR) |
| 943 | port->ignore_status_mask |= |
| 944 | uint_en->sirfsoc_rx_oflow_en; |
| 945 | } |
| 946 | if ((termios->c_cflag & CREAD) == 0) |
| 947 | port->ignore_status_mask |= SIRFUART_DUMMY_READ; |
Rong Wang | 161e773 | 2011-11-17 23:17:04 +0800 | [diff] [blame] | 948 | /* Hardware Flow Control Settings */ |
| 949 | if (UART_ENABLE_MS(port, termios->c_cflag)) { |
| 950 | if (!sirfport->ms_enabled) |
| 951 | sirfsoc_uart_enable_ms(port); |
| 952 | } else { |
| 953 | if (sirfport->ms_enabled) |
| 954 | sirfsoc_uart_disable_ms(port); |
| 955 | } |
Qipan Li | 5df8311 | 2013-08-12 18:15:35 +0800 | [diff] [blame] | 956 | baud_rate = uart_get_baud_rate(port, termios, old, 0, 4000000); |
| 957 | if (ioclk_rate == 150000000) { |
Barry Song | ac4ce71 | 2013-01-16 14:49:27 +0800 | [diff] [blame] | 958 | for (ic = 0; ic < SIRF_BAUD_RATE_SUPPORT_NR; ic++) |
| 959 | if (baud_rate == baudrate_to_regv[ic].baud_rate) |
| 960 | clk_div_reg = baudrate_to_regv[ic].reg_val; |
| 961 | } |
Qipan Li | 5df8311 | 2013-08-12 18:15:35 +0800 | [diff] [blame] | 962 | set_baud = baud_rate; |
| 963 | if (sirfport->uart_reg->uart_type == SIRF_REAL_UART) { |
| 964 | if (unlikely(clk_div_reg == 0)) |
| 965 | clk_div_reg = sirfsoc_uart_calc_sample_div(baud_rate, |
| 966 | ioclk_rate, &set_baud); |
| 967 | wr_regl(port, ureg->sirfsoc_divisor, clk_div_reg); |
Qipan Li | 2eb5618 | 2013-08-15 06:52:15 +0800 | [diff] [blame] | 968 | } else { |
Qipan Li | 5df8311 | 2013-08-12 18:15:35 +0800 | [diff] [blame] | 969 | clk_div_reg = sirfsoc_usp_calc_sample_div(baud_rate, |
| 970 | ioclk_rate, &sample_div_reg); |
| 971 | sample_div_reg--; |
| 972 | set_baud = ((ioclk_rate / (clk_div_reg+1) - 1) / |
| 973 | (sample_div_reg + 1)); |
| 974 | /* setting usp mode 2 */ |
Qipan Li | 459f15c | 2013-08-25 20:18:40 +0800 | [diff] [blame] | 975 | len_val = ((1 << SIRFSOC_USP_MODE2_RXD_DELAY_OFFSET) | |
| 976 | (1 << SIRFSOC_USP_MODE2_TXD_DELAY_OFFSET)); |
| 977 | len_val |= ((clk_div_reg & SIRFSOC_USP_MODE2_CLK_DIVISOR_MASK) |
| 978 | << SIRFSOC_USP_MODE2_CLK_DIVISOR_OFFSET); |
| 979 | wr_regl(port, ureg->sirfsoc_mode2, len_val); |
Qipan Li | 5df8311 | 2013-08-12 18:15:35 +0800 | [diff] [blame] | 980 | } |
Rong Wang | 161e773 | 2011-11-17 23:17:04 +0800 | [diff] [blame] | 981 | if (tty_termios_baud_rate(termios)) |
Qipan Li | 5df8311 | 2013-08-12 18:15:35 +0800 | [diff] [blame] | 982 | tty_termios_encode_baud_rate(termios, set_baud, set_baud); |
| 983 | /* set receive timeout && data bits len */ |
| 984 | rx_time_out = SIRFSOC_UART_RX_TIMEOUT(set_baud, 20000); |
| 985 | rx_time_out = SIRFUART_RECV_TIMEOUT_VALUE(rx_time_out); |
Qipan Li | 8316d04 | 2013-08-19 11:47:53 +0800 | [diff] [blame] | 986 | txfifo_op_reg = rd_regl(port, ureg->sirfsoc_tx_fifo_op); |
Qipan Li | 459f15c | 2013-08-25 20:18:40 +0800 | [diff] [blame] | 987 | wr_regl(port, ureg->sirfsoc_rx_fifo_op, SIRFUART_FIFO_STOP); |
Qipan Li | 5df8311 | 2013-08-12 18:15:35 +0800 | [diff] [blame] | 988 | wr_regl(port, ureg->sirfsoc_tx_fifo_op, |
Qipan Li | 8316d04 | 2013-08-19 11:47:53 +0800 | [diff] [blame] | 989 | (txfifo_op_reg & ~SIRFUART_FIFO_START)); |
Qipan Li | 5df8311 | 2013-08-12 18:15:35 +0800 | [diff] [blame] | 990 | if (sirfport->uart_reg->uart_type == SIRF_REAL_UART) { |
Qipan Li | c1b7ac6 | 2015-05-14 06:45:21 +0000 | [diff] [blame^] | 991 | config_reg |= SIRFUART_UART_RECV_TIMEOUT(rx_time_out); |
Qipan Li | 5df8311 | 2013-08-12 18:15:35 +0800 | [diff] [blame] | 992 | wr_regl(port, ureg->sirfsoc_line_ctrl, config_reg); |
Qipan Li | 2eb5618 | 2013-08-15 06:52:15 +0800 | [diff] [blame] | 993 | } else { |
Qipan Li | 5df8311 | 2013-08-12 18:15:35 +0800 | [diff] [blame] | 994 | /*tx frame ctrl*/ |
Qipan Li | 459f15c | 2013-08-25 20:18:40 +0800 | [diff] [blame] | 995 | len_val = (data_bit_len - 1) << SIRFSOC_USP_TX_DATA_LEN_OFFSET; |
| 996 | len_val |= (data_bit_len + 1 + stop_bit_len - 1) << |
| 997 | SIRFSOC_USP_TX_FRAME_LEN_OFFSET; |
| 998 | len_val |= ((data_bit_len - 1) << |
| 999 | SIRFSOC_USP_TX_SHIFTER_LEN_OFFSET); |
| 1000 | len_val |= (((clk_div_reg & 0xc00) >> 10) << |
| 1001 | SIRFSOC_USP_TX_CLK_DIVISOR_OFFSET); |
Qipan Li | 5df8311 | 2013-08-12 18:15:35 +0800 | [diff] [blame] | 1002 | wr_regl(port, ureg->sirfsoc_tx_frame_ctrl, len_val); |
| 1003 | /*rx frame ctrl*/ |
Qipan Li | 459f15c | 2013-08-25 20:18:40 +0800 | [diff] [blame] | 1004 | len_val = (data_bit_len - 1) << SIRFSOC_USP_RX_DATA_LEN_OFFSET; |
| 1005 | len_val |= (data_bit_len + 1 + stop_bit_len - 1) << |
| 1006 | SIRFSOC_USP_RX_FRAME_LEN_OFFSET; |
| 1007 | len_val |= (data_bit_len - 1) << |
| 1008 | SIRFSOC_USP_RX_SHIFTER_LEN_OFFSET; |
| 1009 | len_val |= (((clk_div_reg & 0xf000) >> 12) << |
| 1010 | SIRFSOC_USP_RX_CLK_DIVISOR_OFFSET); |
Qipan Li | 5df8311 | 2013-08-12 18:15:35 +0800 | [diff] [blame] | 1011 | wr_regl(port, ureg->sirfsoc_rx_frame_ctrl, len_val); |
| 1012 | /*async param*/ |
| 1013 | wr_regl(port, ureg->sirfsoc_async_param_reg, |
Qipan Li | c1b7ac6 | 2015-05-14 06:45:21 +0000 | [diff] [blame^] | 1014 | (SIRFUART_USP_RECV_TIMEOUT(rx_time_out)) | |
Qipan Li | 459f15c | 2013-08-25 20:18:40 +0800 | [diff] [blame] | 1015 | (sample_div_reg & SIRFSOC_USP_ASYNC_DIV2_MASK) << |
| 1016 | SIRFSOC_USP_ASYNC_DIV2_OFFSET); |
Qipan Li | 5df8311 | 2013-08-12 18:15:35 +0800 | [diff] [blame] | 1017 | } |
Qipan Li | 9be16b3 | 2014-01-30 13:57:29 +0800 | [diff] [blame] | 1018 | if (sirfport->tx_dma_chan) |
Qipan Li | 8316d04 | 2013-08-19 11:47:53 +0800 | [diff] [blame] | 1019 | wr_regl(port, ureg->sirfsoc_tx_dma_io_ctrl, SIRFUART_DMA_MODE); |
| 1020 | else |
| 1021 | wr_regl(port, ureg->sirfsoc_tx_dma_io_ctrl, SIRFUART_IO_MODE); |
Qipan Li | 9be16b3 | 2014-01-30 13:57:29 +0800 | [diff] [blame] | 1022 | if (sirfport->rx_dma_chan) |
Qipan Li | 8316d04 | 2013-08-19 11:47:53 +0800 | [diff] [blame] | 1023 | wr_regl(port, ureg->sirfsoc_rx_dma_io_ctrl, SIRFUART_DMA_MODE); |
| 1024 | else |
| 1025 | wr_regl(port, ureg->sirfsoc_rx_dma_io_ctrl, SIRFUART_IO_MODE); |
Rong Wang | 161e773 | 2011-11-17 23:17:04 +0800 | [diff] [blame] | 1026 | /* Reset Rx/Tx FIFO Threshold level for proper baudrate */ |
Qipan Li | 5df8311 | 2013-08-12 18:15:35 +0800 | [diff] [blame] | 1027 | if (set_baud < 1000000) |
Rong Wang | 161e773 | 2011-11-17 23:17:04 +0800 | [diff] [blame] | 1028 | threshold_div = 1; |
| 1029 | else |
| 1030 | threshold_div = 2; |
Qipan Li | 8316d04 | 2013-08-19 11:47:53 +0800 | [diff] [blame] | 1031 | wr_regl(port, ureg->sirfsoc_tx_fifo_ctrl, |
| 1032 | SIRFUART_FIFO_THD(port) / threshold_div); |
| 1033 | wr_regl(port, ureg->sirfsoc_rx_fifo_ctrl, |
| 1034 | SIRFUART_FIFO_THD(port) / threshold_div); |
| 1035 | txfifo_op_reg |= SIRFUART_FIFO_START; |
| 1036 | wr_regl(port, ureg->sirfsoc_tx_fifo_op, txfifo_op_reg); |
Qipan Li | 5df8311 | 2013-08-12 18:15:35 +0800 | [diff] [blame] | 1037 | uart_update_timeout(port, termios->c_cflag, set_baud); |
Rong Wang | 161e773 | 2011-11-17 23:17:04 +0800 | [diff] [blame] | 1038 | sirfsoc_uart_start_rx(port); |
Qipan Li | 5df8311 | 2013-08-12 18:15:35 +0800 | [diff] [blame] | 1039 | wr_regl(port, ureg->sirfsoc_tx_rx_en, SIRFUART_TX_EN | SIRFUART_RX_EN); |
Rong Wang | 161e773 | 2011-11-17 23:17:04 +0800 | [diff] [blame] | 1040 | spin_unlock_irqrestore(&port->lock, flags); |
| 1041 | } |
| 1042 | |
Qipan Li | 388faf9 | 2014-01-03 15:44:07 +0800 | [diff] [blame] | 1043 | static void sirfsoc_uart_pm(struct uart_port *port, unsigned int state, |
| 1044 | unsigned int oldstate) |
| 1045 | { |
| 1046 | struct sirfsoc_uart_port *sirfport = to_sirfport(port); |
Qipan Li | 4b8038d | 2015-04-20 08:10:22 +0000 | [diff] [blame] | 1047 | if (!state) |
Qipan Li | 388faf9 | 2014-01-03 15:44:07 +0800 | [diff] [blame] | 1048 | clk_prepare_enable(sirfport->clk); |
Qipan Li | 4b8038d | 2015-04-20 08:10:22 +0000 | [diff] [blame] | 1049 | else |
Qipan Li | 388faf9 | 2014-01-03 15:44:07 +0800 | [diff] [blame] | 1050 | clk_disable_unprepare(sirfport->clk); |
| 1051 | } |
| 1052 | |
Rong Wang | 161e773 | 2011-11-17 23:17:04 +0800 | [diff] [blame] | 1053 | static int sirfsoc_uart_startup(struct uart_port *port) |
| 1054 | { |
| 1055 | struct sirfsoc_uart_port *sirfport = to_sirfport(port); |
Qipan Li | 15cdcb1 | 2013-08-19 11:47:52 +0800 | [diff] [blame] | 1056 | struct sirfsoc_register *ureg = &sirfport->uart_reg->uart_reg; |
Rong Wang | 161e773 | 2011-11-17 23:17:04 +0800 | [diff] [blame] | 1057 | unsigned int index = port->line; |
| 1058 | int ret; |
| 1059 | set_irq_flags(port->irq, IRQF_VALID | IRQF_NOAUTOEN); |
| 1060 | ret = request_irq(port->irq, |
| 1061 | sirfsoc_uart_isr, |
| 1062 | 0, |
| 1063 | SIRFUART_PORT_NAME, |
| 1064 | sirfport); |
| 1065 | if (ret != 0) { |
| 1066 | dev_err(port->dev, "UART%d request IRQ line (%d) failed.\n", |
| 1067 | index, port->irq); |
| 1068 | goto irq_err; |
| 1069 | } |
Qipan Li | 15cdcb1 | 2013-08-19 11:47:52 +0800 | [diff] [blame] | 1070 | /* initial hardware settings */ |
| 1071 | wr_regl(port, ureg->sirfsoc_tx_dma_io_ctrl, |
| 1072 | rd_regl(port, ureg->sirfsoc_tx_dma_io_ctrl) | |
| 1073 | SIRFUART_IO_MODE); |
| 1074 | wr_regl(port, ureg->sirfsoc_rx_dma_io_ctrl, |
| 1075 | rd_regl(port, ureg->sirfsoc_rx_dma_io_ctrl) | |
| 1076 | SIRFUART_IO_MODE); |
| 1077 | wr_regl(port, ureg->sirfsoc_tx_dma_io_len, 0); |
| 1078 | wr_regl(port, ureg->sirfsoc_rx_dma_io_len, 0); |
| 1079 | wr_regl(port, ureg->sirfsoc_tx_rx_en, SIRFUART_RX_EN | SIRFUART_TX_EN); |
| 1080 | if (sirfport->uart_reg->uart_type == SIRF_USP_UART) |
| 1081 | wr_regl(port, ureg->sirfsoc_mode1, |
| 1082 | SIRFSOC_USP_ENDIAN_CTRL_LSBF | |
| 1083 | SIRFSOC_USP_EN); |
| 1084 | wr_regl(port, ureg->sirfsoc_tx_fifo_op, SIRFUART_FIFO_RESET); |
| 1085 | wr_regl(port, ureg->sirfsoc_tx_fifo_op, 0); |
| 1086 | wr_regl(port, ureg->sirfsoc_rx_fifo_op, SIRFUART_FIFO_RESET); |
| 1087 | wr_regl(port, ureg->sirfsoc_rx_fifo_op, 0); |
| 1088 | wr_regl(port, ureg->sirfsoc_tx_fifo_ctrl, SIRFUART_FIFO_THD(port)); |
| 1089 | wr_regl(port, ureg->sirfsoc_rx_fifo_ctrl, SIRFUART_FIFO_THD(port)); |
Qipan Li | 9be16b3 | 2014-01-30 13:57:29 +0800 | [diff] [blame] | 1090 | if (sirfport->rx_dma_chan) |
Qipan Li | 8316d04 | 2013-08-19 11:47:53 +0800 | [diff] [blame] | 1091 | wr_regl(port, ureg->sirfsoc_rx_fifo_level_chk, |
Qipan Li | 9be16b3 | 2014-01-30 13:57:29 +0800 | [diff] [blame] | 1092 | SIRFUART_RX_FIFO_CHK_SC(port->line, 0x4) | |
| 1093 | SIRFUART_RX_FIFO_CHK_LC(port->line, 0xe) | |
| 1094 | SIRFUART_RX_FIFO_CHK_HC(port->line, 0x1b)); |
| 1095 | if (sirfport->tx_dma_chan) { |
Qipan Li | 8316d04 | 2013-08-19 11:47:53 +0800 | [diff] [blame] | 1096 | sirfport->tx_dma_state = TX_DMA_IDLE; |
| 1097 | wr_regl(port, ureg->sirfsoc_tx_fifo_level_chk, |
| 1098 | SIRFUART_TX_FIFO_CHK_SC(port->line, 0x1b) | |
| 1099 | SIRFUART_TX_FIFO_CHK_LC(port->line, 0xe) | |
| 1100 | SIRFUART_TX_FIFO_CHK_HC(port->line, 0x4)); |
| 1101 | } |
Qipan Li | 2eb5618 | 2013-08-15 06:52:15 +0800 | [diff] [blame] | 1102 | sirfport->ms_enabled = false; |
| 1103 | if (sirfport->uart_reg->uart_type == SIRF_USP_UART && |
| 1104 | sirfport->hw_flow_ctrl) { |
| 1105 | set_irq_flags(gpio_to_irq(sirfport->cts_gpio), |
| 1106 | IRQF_VALID | IRQF_NOAUTOEN); |
| 1107 | ret = request_irq(gpio_to_irq(sirfport->cts_gpio), |
| 1108 | sirfsoc_uart_usp_cts_handler, IRQF_TRIGGER_FALLING | |
| 1109 | IRQF_TRIGGER_RISING, "usp_cts_irq", sirfport); |
| 1110 | if (ret != 0) { |
| 1111 | dev_err(port->dev, "UART-USP:request gpio irq fail\n"); |
| 1112 | goto init_rx_err; |
| 1113 | } |
| 1114 | } |
| 1115 | |
Rong Wang | 161e773 | 2011-11-17 23:17:04 +0800 | [diff] [blame] | 1116 | enable_irq(port->irq); |
Qipan Li | 2eb5618 | 2013-08-15 06:52:15 +0800 | [diff] [blame] | 1117 | |
Qipan Li | 15cdcb1 | 2013-08-19 11:47:52 +0800 | [diff] [blame] | 1118 | return 0; |
Qipan Li | 2eb5618 | 2013-08-15 06:52:15 +0800 | [diff] [blame] | 1119 | init_rx_err: |
| 1120 | free_irq(port->irq, sirfport); |
Rong Wang | 161e773 | 2011-11-17 23:17:04 +0800 | [diff] [blame] | 1121 | irq_err: |
| 1122 | return ret; |
| 1123 | } |
| 1124 | |
| 1125 | static void sirfsoc_uart_shutdown(struct uart_port *port) |
| 1126 | { |
| 1127 | struct sirfsoc_uart_port *sirfport = to_sirfport(port); |
Qipan Li | 5df8311 | 2013-08-12 18:15:35 +0800 | [diff] [blame] | 1128 | struct sirfsoc_register *ureg = &sirfport->uart_reg->uart_reg; |
Barry Song | 057badd | 2015-01-03 17:02:57 +0800 | [diff] [blame] | 1129 | if (!sirfport->is_atlas7) |
Qipan Li | 5df8311 | 2013-08-12 18:15:35 +0800 | [diff] [blame] | 1130 | wr_regl(port, ureg->sirfsoc_int_en_reg, 0); |
Barry Song | 909102d | 2013-08-07 13:35:38 +0800 | [diff] [blame] | 1131 | else |
Qipan Li | c1b7ac6 | 2015-05-14 06:45:21 +0000 | [diff] [blame^] | 1132 | wr_regl(port, ureg->sirfsoc_int_en_clr_reg, ~0UL); |
Barry Song | 909102d | 2013-08-07 13:35:38 +0800 | [diff] [blame] | 1133 | |
Rong Wang | 161e773 | 2011-11-17 23:17:04 +0800 | [diff] [blame] | 1134 | free_irq(port->irq, sirfport); |
Qipan Li | 2eb5618 | 2013-08-15 06:52:15 +0800 | [diff] [blame] | 1135 | if (sirfport->ms_enabled) |
Rong Wang | 161e773 | 2011-11-17 23:17:04 +0800 | [diff] [blame] | 1136 | sirfsoc_uart_disable_ms(port); |
Qipan Li | 2eb5618 | 2013-08-15 06:52:15 +0800 | [diff] [blame] | 1137 | if (sirfport->uart_reg->uart_type == SIRF_USP_UART && |
| 1138 | sirfport->hw_flow_ctrl) { |
| 1139 | gpio_set_value(sirfport->rts_gpio, 1); |
| 1140 | free_irq(gpio_to_irq(sirfport->cts_gpio), sirfport); |
Rong Wang | 161e773 | 2011-11-17 23:17:04 +0800 | [diff] [blame] | 1141 | } |
Qipan Li | 9be16b3 | 2014-01-30 13:57:29 +0800 | [diff] [blame] | 1142 | if (sirfport->tx_dma_chan) |
Qipan Li | 8316d04 | 2013-08-19 11:47:53 +0800 | [diff] [blame] | 1143 | sirfport->tx_dma_state = TX_DMA_IDLE; |
Rong Wang | 161e773 | 2011-11-17 23:17:04 +0800 | [diff] [blame] | 1144 | } |
| 1145 | |
| 1146 | static const char *sirfsoc_uart_type(struct uart_port *port) |
| 1147 | { |
| 1148 | return port->type == SIRFSOC_PORT_TYPE ? SIRFUART_PORT_NAME : NULL; |
| 1149 | } |
| 1150 | |
| 1151 | static int sirfsoc_uart_request_port(struct uart_port *port) |
| 1152 | { |
Qipan Li | 5df8311 | 2013-08-12 18:15:35 +0800 | [diff] [blame] | 1153 | struct sirfsoc_uart_port *sirfport = to_sirfport(port); |
| 1154 | struct sirfsoc_uart_param *uart_param = &sirfport->uart_reg->uart_param; |
Rong Wang | 161e773 | 2011-11-17 23:17:04 +0800 | [diff] [blame] | 1155 | void *ret; |
| 1156 | ret = request_mem_region(port->mapbase, |
Qipan Li | 5df8311 | 2013-08-12 18:15:35 +0800 | [diff] [blame] | 1157 | SIRFUART_MAP_SIZE, uart_param->port_name); |
Rong Wang | 161e773 | 2011-11-17 23:17:04 +0800 | [diff] [blame] | 1158 | return ret ? 0 : -EBUSY; |
| 1159 | } |
| 1160 | |
| 1161 | static void sirfsoc_uart_release_port(struct uart_port *port) |
| 1162 | { |
| 1163 | release_mem_region(port->mapbase, SIRFUART_MAP_SIZE); |
| 1164 | } |
| 1165 | |
| 1166 | static void sirfsoc_uart_config_port(struct uart_port *port, int flags) |
| 1167 | { |
| 1168 | if (flags & UART_CONFIG_TYPE) { |
| 1169 | port->type = SIRFSOC_PORT_TYPE; |
| 1170 | sirfsoc_uart_request_port(port); |
| 1171 | } |
| 1172 | } |
| 1173 | |
| 1174 | static struct uart_ops sirfsoc_uart_ops = { |
| 1175 | .tx_empty = sirfsoc_uart_tx_empty, |
| 1176 | .get_mctrl = sirfsoc_uart_get_mctrl, |
| 1177 | .set_mctrl = sirfsoc_uart_set_mctrl, |
| 1178 | .stop_tx = sirfsoc_uart_stop_tx, |
| 1179 | .start_tx = sirfsoc_uart_start_tx, |
| 1180 | .stop_rx = sirfsoc_uart_stop_rx, |
| 1181 | .enable_ms = sirfsoc_uart_enable_ms, |
| 1182 | .break_ctl = sirfsoc_uart_break_ctl, |
| 1183 | .startup = sirfsoc_uart_startup, |
| 1184 | .shutdown = sirfsoc_uart_shutdown, |
| 1185 | .set_termios = sirfsoc_uart_set_termios, |
Qipan Li | 388faf9 | 2014-01-03 15:44:07 +0800 | [diff] [blame] | 1186 | .pm = sirfsoc_uart_pm, |
Rong Wang | 161e773 | 2011-11-17 23:17:04 +0800 | [diff] [blame] | 1187 | .type = sirfsoc_uart_type, |
| 1188 | .release_port = sirfsoc_uart_release_port, |
| 1189 | .request_port = sirfsoc_uart_request_port, |
| 1190 | .config_port = sirfsoc_uart_config_port, |
| 1191 | }; |
| 1192 | |
| 1193 | #ifdef CONFIG_SERIAL_SIRFSOC_CONSOLE |
Qipan Li | 5df8311 | 2013-08-12 18:15:35 +0800 | [diff] [blame] | 1194 | static int __init |
| 1195 | sirfsoc_uart_console_setup(struct console *co, char *options) |
Rong Wang | 161e773 | 2011-11-17 23:17:04 +0800 | [diff] [blame] | 1196 | { |
| 1197 | unsigned int baud = 115200; |
| 1198 | unsigned int bits = 8; |
| 1199 | unsigned int parity = 'n'; |
| 1200 | unsigned int flow = 'n'; |
Qipan Li | a6ffe89 | 2015-04-29 06:45:08 +0000 | [diff] [blame] | 1201 | struct sirfsoc_uart_port *sirfport; |
| 1202 | struct sirfsoc_register *ureg; |
Rong Wang | 161e773 | 2011-11-17 23:17:04 +0800 | [diff] [blame] | 1203 | if (co->index < 0 || co->index >= SIRFSOC_UART_NR) |
| 1204 | return -EINVAL; |
Qipan Li | a6ffe89 | 2015-04-29 06:45:08 +0000 | [diff] [blame] | 1205 | sirfport = sirf_ports[co->index]; |
| 1206 | if (!sirfport) |
| 1207 | return -ENODEV; |
| 1208 | ureg = &sirfport->uart_reg->uart_reg; |
| 1209 | if (!sirfport->port.mapbase) |
Rong Wang | 161e773 | 2011-11-17 23:17:04 +0800 | [diff] [blame] | 1210 | return -ENODEV; |
| 1211 | |
Qipan Li | 5df8311 | 2013-08-12 18:15:35 +0800 | [diff] [blame] | 1212 | /* enable usp in mode1 register */ |
| 1213 | if (sirfport->uart_reg->uart_type == SIRF_USP_UART) |
Qipan Li | a6ffe89 | 2015-04-29 06:45:08 +0000 | [diff] [blame] | 1214 | wr_regl(&sirfport->port, ureg->sirfsoc_mode1, SIRFSOC_USP_EN | |
Qipan Li | 5df8311 | 2013-08-12 18:15:35 +0800 | [diff] [blame] | 1215 | SIRFSOC_USP_ENDIAN_CTRL_LSBF); |
Rong Wang | 161e773 | 2011-11-17 23:17:04 +0800 | [diff] [blame] | 1216 | if (options) |
| 1217 | uart_parse_options(options, &baud, &parity, &bits, &flow); |
Qipan Li | a6ffe89 | 2015-04-29 06:45:08 +0000 | [diff] [blame] | 1218 | sirfport->port.cons = co; |
Qipan Li | 5df8311 | 2013-08-12 18:15:35 +0800 | [diff] [blame] | 1219 | |
Qipan Li | 8316d04 | 2013-08-19 11:47:53 +0800 | [diff] [blame] | 1220 | /* default console tx/rx transfer using io mode */ |
Qipan Li | 9be16b3 | 2014-01-30 13:57:29 +0800 | [diff] [blame] | 1221 | sirfport->rx_dma_chan = NULL; |
| 1222 | sirfport->tx_dma_chan = NULL; |
Qipan Li | a6ffe89 | 2015-04-29 06:45:08 +0000 | [diff] [blame] | 1223 | return uart_set_options(&sirfport->port, co, baud, parity, bits, flow); |
Rong Wang | 161e773 | 2011-11-17 23:17:04 +0800 | [diff] [blame] | 1224 | } |
| 1225 | |
| 1226 | static void sirfsoc_uart_console_putchar(struct uart_port *port, int ch) |
| 1227 | { |
Qipan Li | 5df8311 | 2013-08-12 18:15:35 +0800 | [diff] [blame] | 1228 | struct sirfsoc_uart_port *sirfport = to_sirfport(port); |
| 1229 | struct sirfsoc_register *ureg = &sirfport->uart_reg->uart_reg; |
| 1230 | struct sirfsoc_fifo_status *ufifo_st = &sirfport->uart_reg->fifo_status; |
Qipan Li | cb4595a | 2015-04-29 06:45:09 +0000 | [diff] [blame] | 1231 | while (rd_regl(port, ureg->sirfsoc_tx_fifo_status) & |
| 1232 | ufifo_st->ff_full(port)) |
Rong Wang | 161e773 | 2011-11-17 23:17:04 +0800 | [diff] [blame] | 1233 | cpu_relax(); |
Barry Song | 205c384 | 2014-05-05 08:05:51 +0800 | [diff] [blame] | 1234 | wr_regl(port, ureg->sirfsoc_tx_fifo_data, ch); |
Rong Wang | 161e773 | 2011-11-17 23:17:04 +0800 | [diff] [blame] | 1235 | } |
| 1236 | |
| 1237 | static void sirfsoc_uart_console_write(struct console *co, const char *s, |
| 1238 | unsigned int count) |
| 1239 | { |
Qipan Li | a6ffe89 | 2015-04-29 06:45:08 +0000 | [diff] [blame] | 1240 | struct sirfsoc_uart_port *sirfport = sirf_ports[co->index]; |
| 1241 | |
| 1242 | uart_console_write(&sirfport->port, s, count, |
| 1243 | sirfsoc_uart_console_putchar); |
Rong Wang | 161e773 | 2011-11-17 23:17:04 +0800 | [diff] [blame] | 1244 | } |
| 1245 | |
| 1246 | static struct console sirfsoc_uart_console = { |
| 1247 | .name = SIRFSOC_UART_NAME, |
| 1248 | .device = uart_console_device, |
| 1249 | .flags = CON_PRINTBUFFER, |
| 1250 | .index = -1, |
| 1251 | .write = sirfsoc_uart_console_write, |
| 1252 | .setup = sirfsoc_uart_console_setup, |
| 1253 | .data = &sirfsoc_uart_drv, |
| 1254 | }; |
| 1255 | |
| 1256 | static int __init sirfsoc_uart_console_init(void) |
| 1257 | { |
| 1258 | register_console(&sirfsoc_uart_console); |
| 1259 | return 0; |
| 1260 | } |
| 1261 | console_initcall(sirfsoc_uart_console_init); |
| 1262 | #endif |
| 1263 | |
| 1264 | static struct uart_driver sirfsoc_uart_drv = { |
| 1265 | .owner = THIS_MODULE, |
| 1266 | .driver_name = SIRFUART_PORT_NAME, |
| 1267 | .nr = SIRFSOC_UART_NR, |
| 1268 | .dev_name = SIRFSOC_UART_NAME, |
| 1269 | .major = SIRFSOC_UART_MAJOR, |
| 1270 | .minor = SIRFSOC_UART_MINOR, |
| 1271 | #ifdef CONFIG_SERIAL_SIRFSOC_CONSOLE |
| 1272 | .cons = &sirfsoc_uart_console, |
| 1273 | #else |
| 1274 | .cons = NULL, |
| 1275 | #endif |
| 1276 | }; |
| 1277 | |
Qipan Li | c1b7ac6 | 2015-05-14 06:45:21 +0000 | [diff] [blame^] | 1278 | static struct of_device_id sirfsoc_uart_ids[] = { |
Qipan Li | 5df8311 | 2013-08-12 18:15:35 +0800 | [diff] [blame] | 1279 | { .compatible = "sirf,prima2-uart", .data = &sirfsoc_uart,}, |
Barry Song | 057badd | 2015-01-03 17:02:57 +0800 | [diff] [blame] | 1280 | { .compatible = "sirf,atlas7-uart", .data = &sirfsoc_uart}, |
Qipan Li | 5df8311 | 2013-08-12 18:15:35 +0800 | [diff] [blame] | 1281 | { .compatible = "sirf,prima2-usp-uart", .data = &sirfsoc_usp}, |
Qipan Li | c1b7ac6 | 2015-05-14 06:45:21 +0000 | [diff] [blame^] | 1282 | { .compatible = "sirf,atlas7-usp-uart", .data = &sirfsoc_usp}, |
Qipan Li | 5df8311 | 2013-08-12 18:15:35 +0800 | [diff] [blame] | 1283 | {} |
| 1284 | }; |
| 1285 | MODULE_DEVICE_TABLE(of, sirfsoc_uart_ids); |
| 1286 | |
Jingoo Han | ada1f443d | 2013-08-08 17:41:43 +0900 | [diff] [blame] | 1287 | static int sirfsoc_uart_probe(struct platform_device *pdev) |
Rong Wang | 161e773 | 2011-11-17 23:17:04 +0800 | [diff] [blame] | 1288 | { |
| 1289 | struct sirfsoc_uart_port *sirfport; |
| 1290 | struct uart_port *port; |
| 1291 | struct resource *res; |
| 1292 | int ret; |
Qipan Li | 9be16b3 | 2014-01-30 13:57:29 +0800 | [diff] [blame] | 1293 | int i, j; |
| 1294 | struct dma_slave_config slv_cfg = { |
| 1295 | .src_maxburst = 2, |
| 1296 | }; |
| 1297 | struct dma_slave_config tx_slv_cfg = { |
| 1298 | .dst_maxburst = 2, |
| 1299 | }; |
Qipan Li | 5df8311 | 2013-08-12 18:15:35 +0800 | [diff] [blame] | 1300 | const struct of_device_id *match; |
Rong Wang | 161e773 | 2011-11-17 23:17:04 +0800 | [diff] [blame] | 1301 | |
Qipan Li | 5df8311 | 2013-08-12 18:15:35 +0800 | [diff] [blame] | 1302 | match = of_match_node(sirfsoc_uart_ids, pdev->dev.of_node); |
Qipan Li | a6ffe89 | 2015-04-29 06:45:08 +0000 | [diff] [blame] | 1303 | sirfport = devm_kzalloc(&pdev->dev, sizeof(*sirfport), GFP_KERNEL); |
| 1304 | if (!sirfport) { |
| 1305 | ret = -ENOMEM; |
Rong Wang | 161e773 | 2011-11-17 23:17:04 +0800 | [diff] [blame] | 1306 | goto err; |
| 1307 | } |
Qipan Li | a6ffe89 | 2015-04-29 06:45:08 +0000 | [diff] [blame] | 1308 | sirfport->port.line = of_alias_get_id(pdev->dev.of_node, "serial"); |
| 1309 | sirf_ports[sirfport->port.line] = sirfport; |
| 1310 | sirfport->port.iotype = UPIO_MEM; |
| 1311 | sirfport->port.flags = UPF_BOOT_AUTOCONF; |
Rong Wang | 161e773 | 2011-11-17 23:17:04 +0800 | [diff] [blame] | 1312 | port = &sirfport->port; |
| 1313 | port->dev = &pdev->dev; |
| 1314 | port->private_data = sirfport; |
Qipan Li | 5df8311 | 2013-08-12 18:15:35 +0800 | [diff] [blame] | 1315 | sirfport->uart_reg = (struct sirfsoc_uart_register *)match->data; |
Rong Wang | 161e773 | 2011-11-17 23:17:04 +0800 | [diff] [blame] | 1316 | |
Qipan Li | 2eb5618 | 2013-08-15 06:52:15 +0800 | [diff] [blame] | 1317 | sirfport->hw_flow_ctrl = of_property_read_bool(pdev->dev.of_node, |
| 1318 | "sirf,uart-has-rtscts"); |
Qipan Li | c1b7ac6 | 2015-05-14 06:45:21 +0000 | [diff] [blame^] | 1319 | if (of_device_is_compatible(pdev->dev.of_node, "sirf,prima2-uart") || |
| 1320 | of_device_is_compatible(pdev->dev.of_node, "sirf,atlas7-uart")) |
Qipan Li | 5df8311 | 2013-08-12 18:15:35 +0800 | [diff] [blame] | 1321 | sirfport->uart_reg->uart_type = SIRF_REAL_UART; |
Qipan Li | c1b7ac6 | 2015-05-14 06:45:21 +0000 | [diff] [blame^] | 1322 | if (of_device_is_compatible(pdev->dev.of_node, |
| 1323 | "sirf,prima2-usp-uart") || of_device_is_compatible( |
| 1324 | pdev->dev.of_node, "sirf,atlas7-usp-uart")) { |
Qipan Li | 5df8311 | 2013-08-12 18:15:35 +0800 | [diff] [blame] | 1325 | sirfport->uart_reg->uart_type = SIRF_USP_UART; |
Qipan Li | 2eb5618 | 2013-08-15 06:52:15 +0800 | [diff] [blame] | 1326 | if (!sirfport->hw_flow_ctrl) |
| 1327 | goto usp_no_flow_control; |
| 1328 | if (of_find_property(pdev->dev.of_node, "cts-gpios", NULL)) |
| 1329 | sirfport->cts_gpio = of_get_named_gpio( |
| 1330 | pdev->dev.of_node, "cts-gpios", 0); |
| 1331 | else |
| 1332 | sirfport->cts_gpio = -1; |
| 1333 | if (of_find_property(pdev->dev.of_node, "rts-gpios", NULL)) |
| 1334 | sirfport->rts_gpio = of_get_named_gpio( |
| 1335 | pdev->dev.of_node, "rts-gpios", 0); |
| 1336 | else |
| 1337 | sirfport->rts_gpio = -1; |
| 1338 | |
| 1339 | if ((!gpio_is_valid(sirfport->cts_gpio) || |
| 1340 | !gpio_is_valid(sirfport->rts_gpio))) { |
| 1341 | ret = -EINVAL; |
| 1342 | dev_err(&pdev->dev, |
Qipan Li | 67bc306 | 2013-08-19 11:47:51 +0800 | [diff] [blame] | 1343 | "Usp flow control must have cts and rts gpio"); |
Qipan Li | 2eb5618 | 2013-08-15 06:52:15 +0800 | [diff] [blame] | 1344 | goto err; |
| 1345 | } |
| 1346 | ret = devm_gpio_request(&pdev->dev, sirfport->cts_gpio, |
Qipan Li | 67bc306 | 2013-08-19 11:47:51 +0800 | [diff] [blame] | 1347 | "usp-cts-gpio"); |
Qipan Li | 2eb5618 | 2013-08-15 06:52:15 +0800 | [diff] [blame] | 1348 | if (ret) { |
Qipan Li | 67bc306 | 2013-08-19 11:47:51 +0800 | [diff] [blame] | 1349 | dev_err(&pdev->dev, "Unable request cts gpio"); |
Qipan Li | 2eb5618 | 2013-08-15 06:52:15 +0800 | [diff] [blame] | 1350 | goto err; |
| 1351 | } |
| 1352 | gpio_direction_input(sirfport->cts_gpio); |
| 1353 | ret = devm_gpio_request(&pdev->dev, sirfport->rts_gpio, |
Qipan Li | 67bc306 | 2013-08-19 11:47:51 +0800 | [diff] [blame] | 1354 | "usp-rts-gpio"); |
Qipan Li | 2eb5618 | 2013-08-15 06:52:15 +0800 | [diff] [blame] | 1355 | if (ret) { |
Qipan Li | 67bc306 | 2013-08-19 11:47:51 +0800 | [diff] [blame] | 1356 | dev_err(&pdev->dev, "Unable request rts gpio"); |
Qipan Li | 2eb5618 | 2013-08-15 06:52:15 +0800 | [diff] [blame] | 1357 | goto err; |
| 1358 | } |
| 1359 | gpio_direction_output(sirfport->rts_gpio, 1); |
| 1360 | } |
| 1361 | usp_no_flow_control: |
Qipan Li | c1b7ac6 | 2015-05-14 06:45:21 +0000 | [diff] [blame^] | 1362 | if (of_device_is_compatible(pdev->dev.of_node, "sirf,atlas7-uart") || |
| 1363 | of_device_is_compatible(pdev->dev.of_node, "sirf,atlas7-usp-uart")) |
Barry Song | 057badd | 2015-01-03 17:02:57 +0800 | [diff] [blame] | 1364 | sirfport->is_atlas7 = true; |
Barry Song | 909102d | 2013-08-07 13:35:38 +0800 | [diff] [blame] | 1365 | |
Rong Wang | 161e773 | 2011-11-17 23:17:04 +0800 | [diff] [blame] | 1366 | if (of_property_read_u32(pdev->dev.of_node, |
| 1367 | "fifosize", |
| 1368 | &port->fifosize)) { |
| 1369 | dev_err(&pdev->dev, |
| 1370 | "Unable to find fifosize in uart node.\n"); |
| 1371 | ret = -EFAULT; |
| 1372 | goto err; |
| 1373 | } |
| 1374 | |
| 1375 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 1376 | if (res == NULL) { |
| 1377 | dev_err(&pdev->dev, "Insufficient resources.\n"); |
| 1378 | ret = -EFAULT; |
| 1379 | goto err; |
| 1380 | } |
Qipan Li | 8316d04 | 2013-08-19 11:47:53 +0800 | [diff] [blame] | 1381 | tasklet_init(&sirfport->rx_dma_complete_tasklet, |
| 1382 | sirfsoc_uart_rx_dma_complete_tl, (unsigned long)sirfport); |
| 1383 | tasklet_init(&sirfport->rx_tmo_process_tasklet, |
| 1384 | sirfsoc_rx_tmo_process_tl, (unsigned long)sirfport); |
Rong Wang | 161e773 | 2011-11-17 23:17:04 +0800 | [diff] [blame] | 1385 | port->mapbase = res->start; |
| 1386 | port->membase = devm_ioremap(&pdev->dev, res->start, resource_size(res)); |
| 1387 | if (!port->membase) { |
| 1388 | dev_err(&pdev->dev, "Cannot remap resource.\n"); |
| 1389 | ret = -ENOMEM; |
| 1390 | goto err; |
| 1391 | } |
| 1392 | res = platform_get_resource(pdev, IORESOURCE_IRQ, 0); |
| 1393 | if (res == NULL) { |
| 1394 | dev_err(&pdev->dev, "Insufficient resources.\n"); |
| 1395 | ret = -EFAULT; |
Julia Lawall | 9250dd5 | 2012-09-01 18:33:09 +0200 | [diff] [blame] | 1396 | goto err; |
Rong Wang | 161e773 | 2011-11-17 23:17:04 +0800 | [diff] [blame] | 1397 | } |
| 1398 | port->irq = res->start; |
| 1399 | |
Qipan Li | adeede7 | 2015-04-20 08:10:23 +0000 | [diff] [blame] | 1400 | sirfport->clk = devm_clk_get(&pdev->dev, NULL); |
Barry Song | ac4ce71 | 2013-01-16 14:49:27 +0800 | [diff] [blame] | 1401 | if (IS_ERR(sirfport->clk)) { |
| 1402 | ret = PTR_ERR(sirfport->clk); |
Barry Song | a343756 | 2013-08-15 06:52:14 +0800 | [diff] [blame] | 1403 | goto err; |
Barry Song | ac4ce71 | 2013-01-16 14:49:27 +0800 | [diff] [blame] | 1404 | } |
Barry Song | ac4ce71 | 2013-01-16 14:49:27 +0800 | [diff] [blame] | 1405 | port->uartclk = clk_get_rate(sirfport->clk); |
| 1406 | |
Rong Wang | 161e773 | 2011-11-17 23:17:04 +0800 | [diff] [blame] | 1407 | port->ops = &sirfsoc_uart_ops; |
| 1408 | spin_lock_init(&port->lock); |
| 1409 | |
| 1410 | platform_set_drvdata(pdev, sirfport); |
| 1411 | ret = uart_add_one_port(&sirfsoc_uart_drv, port); |
| 1412 | if (ret != 0) { |
| 1413 | dev_err(&pdev->dev, "Cannot add UART port(%d).\n", pdev->id); |
Qipan Li | adeede7 | 2015-04-20 08:10:23 +0000 | [diff] [blame] | 1414 | goto err; |
Rong Wang | 161e773 | 2011-11-17 23:17:04 +0800 | [diff] [blame] | 1415 | } |
| 1416 | |
Qipan Li | 9be16b3 | 2014-01-30 13:57:29 +0800 | [diff] [blame] | 1417 | sirfport->rx_dma_chan = dma_request_slave_channel(port->dev, "rx"); |
| 1418 | for (i = 0; sirfport->rx_dma_chan && i < SIRFSOC_RX_LOOP_BUF_CNT; i++) { |
| 1419 | sirfport->rx_dma_items[i].xmit.buf = |
| 1420 | dma_alloc_coherent(port->dev, SIRFSOC_RX_DMA_BUF_SIZE, |
| 1421 | &sirfport->rx_dma_items[i].dma_addr, GFP_KERNEL); |
| 1422 | if (!sirfport->rx_dma_items[i].xmit.buf) { |
| 1423 | dev_err(port->dev, "Uart alloc bufa failed\n"); |
| 1424 | ret = -ENOMEM; |
| 1425 | goto alloc_coherent_err; |
| 1426 | } |
| 1427 | sirfport->rx_dma_items[i].xmit.head = |
| 1428 | sirfport->rx_dma_items[i].xmit.tail = 0; |
| 1429 | } |
| 1430 | if (sirfport->rx_dma_chan) |
| 1431 | dmaengine_slave_config(sirfport->rx_dma_chan, &slv_cfg); |
| 1432 | sirfport->tx_dma_chan = dma_request_slave_channel(port->dev, "tx"); |
| 1433 | if (sirfport->tx_dma_chan) |
| 1434 | dmaengine_slave_config(sirfport->tx_dma_chan, &tx_slv_cfg); |
Rong Wang | 161e773 | 2011-11-17 23:17:04 +0800 | [diff] [blame] | 1435 | |
Qipan Li | 9be16b3 | 2014-01-30 13:57:29 +0800 | [diff] [blame] | 1436 | return 0; |
| 1437 | alloc_coherent_err: |
| 1438 | for (j = 0; j < i; j++) |
| 1439 | dma_free_coherent(port->dev, SIRFSOC_RX_DMA_BUF_SIZE, |
| 1440 | sirfport->rx_dma_items[j].xmit.buf, |
| 1441 | sirfport->rx_dma_items[j].dma_addr); |
| 1442 | dma_release_channel(sirfport->rx_dma_chan); |
Rong Wang | 161e773 | 2011-11-17 23:17:04 +0800 | [diff] [blame] | 1443 | err: |
| 1444 | return ret; |
| 1445 | } |
| 1446 | |
| 1447 | static int sirfsoc_uart_remove(struct platform_device *pdev) |
| 1448 | { |
| 1449 | struct sirfsoc_uart_port *sirfport = platform_get_drvdata(pdev); |
| 1450 | struct uart_port *port = &sirfport->port; |
Rong Wang | 161e773 | 2011-11-17 23:17:04 +0800 | [diff] [blame] | 1451 | uart_remove_one_port(&sirfsoc_uart_drv, port); |
Qipan Li | 9be16b3 | 2014-01-30 13:57:29 +0800 | [diff] [blame] | 1452 | if (sirfport->rx_dma_chan) { |
| 1453 | int i; |
| 1454 | dmaengine_terminate_all(sirfport->rx_dma_chan); |
| 1455 | dma_release_channel(sirfport->rx_dma_chan); |
| 1456 | for (i = 0; i < SIRFSOC_RX_LOOP_BUF_CNT; i++) |
| 1457 | dma_free_coherent(port->dev, SIRFSOC_RX_DMA_BUF_SIZE, |
| 1458 | sirfport->rx_dma_items[i].xmit.buf, |
| 1459 | sirfport->rx_dma_items[i].dma_addr); |
| 1460 | } |
| 1461 | if (sirfport->tx_dma_chan) { |
| 1462 | dmaengine_terminate_all(sirfport->tx_dma_chan); |
| 1463 | dma_release_channel(sirfport->tx_dma_chan); |
| 1464 | } |
Rong Wang | 161e773 | 2011-11-17 23:17:04 +0800 | [diff] [blame] | 1465 | return 0; |
| 1466 | } |
| 1467 | |
Qipan Li | 99e626f | 2014-01-03 15:44:06 +0800 | [diff] [blame] | 1468 | #ifdef CONFIG_PM_SLEEP |
Rong Wang | 161e773 | 2011-11-17 23:17:04 +0800 | [diff] [blame] | 1469 | static int |
Qipan Li | 99e626f | 2014-01-03 15:44:06 +0800 | [diff] [blame] | 1470 | sirfsoc_uart_suspend(struct device *pdev) |
Rong Wang | 161e773 | 2011-11-17 23:17:04 +0800 | [diff] [blame] | 1471 | { |
Qipan Li | 99e626f | 2014-01-03 15:44:06 +0800 | [diff] [blame] | 1472 | struct sirfsoc_uart_port *sirfport = dev_get_drvdata(pdev); |
Rong Wang | 161e773 | 2011-11-17 23:17:04 +0800 | [diff] [blame] | 1473 | struct uart_port *port = &sirfport->port; |
| 1474 | uart_suspend_port(&sirfsoc_uart_drv, port); |
| 1475 | return 0; |
| 1476 | } |
| 1477 | |
Qipan Li | 99e626f | 2014-01-03 15:44:06 +0800 | [diff] [blame] | 1478 | static int sirfsoc_uart_resume(struct device *pdev) |
Rong Wang | 161e773 | 2011-11-17 23:17:04 +0800 | [diff] [blame] | 1479 | { |
Qipan Li | 99e626f | 2014-01-03 15:44:06 +0800 | [diff] [blame] | 1480 | struct sirfsoc_uart_port *sirfport = dev_get_drvdata(pdev); |
Rong Wang | 161e773 | 2011-11-17 23:17:04 +0800 | [diff] [blame] | 1481 | struct uart_port *port = &sirfport->port; |
| 1482 | uart_resume_port(&sirfsoc_uart_drv, port); |
| 1483 | return 0; |
| 1484 | } |
Qipan Li | 99e626f | 2014-01-03 15:44:06 +0800 | [diff] [blame] | 1485 | #endif |
| 1486 | |
| 1487 | static const struct dev_pm_ops sirfsoc_uart_pm_ops = { |
| 1488 | SET_SYSTEM_SLEEP_PM_OPS(sirfsoc_uart_suspend, sirfsoc_uart_resume) |
| 1489 | }; |
Rong Wang | 161e773 | 2011-11-17 23:17:04 +0800 | [diff] [blame] | 1490 | |
Rong Wang | 161e773 | 2011-11-17 23:17:04 +0800 | [diff] [blame] | 1491 | static struct platform_driver sirfsoc_uart_driver = { |
| 1492 | .probe = sirfsoc_uart_probe, |
Bill Pemberton | 2d47b71 | 2012-11-19 13:21:34 -0500 | [diff] [blame] | 1493 | .remove = sirfsoc_uart_remove, |
Rong Wang | 161e773 | 2011-11-17 23:17:04 +0800 | [diff] [blame] | 1494 | .driver = { |
| 1495 | .name = SIRFUART_PORT_NAME, |
Rong Wang | 161e773 | 2011-11-17 23:17:04 +0800 | [diff] [blame] | 1496 | .of_match_table = sirfsoc_uart_ids, |
Qipan Li | 99e626f | 2014-01-03 15:44:06 +0800 | [diff] [blame] | 1497 | .pm = &sirfsoc_uart_pm_ops, |
Rong Wang | 161e773 | 2011-11-17 23:17:04 +0800 | [diff] [blame] | 1498 | }, |
| 1499 | }; |
| 1500 | |
| 1501 | static int __init sirfsoc_uart_init(void) |
| 1502 | { |
| 1503 | int ret = 0; |
| 1504 | |
| 1505 | ret = uart_register_driver(&sirfsoc_uart_drv); |
| 1506 | if (ret) |
| 1507 | goto out; |
| 1508 | |
| 1509 | ret = platform_driver_register(&sirfsoc_uart_driver); |
| 1510 | if (ret) |
| 1511 | uart_unregister_driver(&sirfsoc_uart_drv); |
| 1512 | out: |
| 1513 | return ret; |
| 1514 | } |
| 1515 | module_init(sirfsoc_uart_init); |
| 1516 | |
| 1517 | static void __exit sirfsoc_uart_exit(void) |
| 1518 | { |
| 1519 | platform_driver_unregister(&sirfsoc_uart_driver); |
| 1520 | uart_unregister_driver(&sirfsoc_uart_drv); |
| 1521 | } |
| 1522 | module_exit(sirfsoc_uart_exit); |
| 1523 | |
| 1524 | MODULE_LICENSE("GPL v2"); |
| 1525 | MODULE_AUTHOR("Bin Shi <Bin.Shi@csr.com>, Rong Wang<Rong.Wang@csr.com>"); |
| 1526 | MODULE_DESCRIPTION("CSR SiRFprimaII Uart Driver"); |