blob: 7214abe0d91344fe20049948ad478dd36c9c7117 [file] [log] [blame]
Rong Wang161e7732011-11-17 23:17:04 +08001/*
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 Li2eb56182013-08-15 06:52:15 +080023#include <linux/of_gpio.h>
Qipan Li8316d042013-08-19 11:47:53 +080024#include <linux/dmaengine.h>
25#include <linux/dma-direction.h>
26#include <linux/dma-mapping.h>
Rong Wang161e7732011-11-17 23:17:04 +080027#include <asm/irq.h>
28#include <asm/mach/irq.h>
Rong Wang161e7732011-11-17 23:17:04 +080029
30#include "sirfsoc_uart.h"
31
32static unsigned int
33sirfsoc_uart_pio_tx_chars(struct sirfsoc_uart_port *sirfport, int count);
34static unsigned int
35sirfsoc_uart_pio_rx_chars(struct uart_port *port, unsigned int max_rx_count);
36static struct uart_driver sirfsoc_uart_drv;
37
Qipan Li8316d042013-08-19 11:47:53 +080038static void sirfsoc_uart_tx_dma_complete_callback(void *param);
39static void sirfsoc_uart_start_next_rx_dma(struct uart_port *port);
40static void sirfsoc_uart_rx_dma_complete_callback(void *param);
Rong Wang161e7732011-11-17 23:17:04 +080041static 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 Lia6ffe892015-04-29 06:45:08 +000062static struct sirfsoc_uart_port *sirf_ports[SIRFSOC_UART_NR];
Rong Wang161e7732011-11-17 23:17:04 +080063
64static inline struct sirfsoc_uart_port *to_sirfport(struct uart_port *port)
65{
66 return container_of(port, struct sirfsoc_uart_port, port);
67}
68
69static inline unsigned int sirfsoc_uart_tx_empty(struct uart_port *port)
70{
71 unsigned long reg;
Qipan Li5df83112013-08-12 18:15:35 +080072 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 Licb4595a2015-04-29 06:45:09 +000076 return (reg & ufifo_st->ff_empty(port)) ? TIOCSER_TEMT : 0;
Rong Wang161e7732011-11-17 23:17:04 +080077}
78
79static unsigned int sirfsoc_uart_get_mctrl(struct uart_port *port)
80{
81 struct sirfsoc_uart_port *sirfport = to_sirfport(port);
Qipan Li5df83112013-08-12 18:15:35 +080082 struct sirfsoc_register *ureg = &sirfport->uart_reg->uart_reg;
Qipan Li2eb56182013-08-15 06:52:15 +080083 if (!sirfport->hw_flow_ctrl || !sirfport->ms_enabled)
Rong Wang161e7732011-11-17 23:17:04 +080084 goto cts_asserted;
Qipan Li2eb56182013-08-15 06:52:15 +080085 if (sirfport->uart_reg->uart_type == SIRF_REAL_UART) {
Qipan Li5df83112013-08-12 18:15:35 +080086 if (!(rd_regl(port, ureg->sirfsoc_afc_ctrl) &
87 SIRFUART_AFC_CTS_STATUS))
Rong Wang161e7732011-11-17 23:17:04 +080088 goto cts_asserted;
89 else
90 goto cts_deasserted;
Qipan Li2eb56182013-08-15 06:52:15 +080091 } else {
92 if (!gpio_get_value(sirfport->cts_gpio))
93 goto cts_asserted;
94 else
95 goto cts_deasserted;
Rong Wang161e7732011-11-17 23:17:04 +080096 }
97cts_deasserted:
98 return TIOCM_CAR | TIOCM_DSR;
99cts_asserted:
100 return TIOCM_CAR | TIOCM_DSR | TIOCM_CTS;
101}
102
103static void sirfsoc_uart_set_mctrl(struct uart_port *port, unsigned int mctrl)
104{
105 struct sirfsoc_uart_port *sirfport = to_sirfport(port);
Qipan Li5df83112013-08-12 18:15:35 +0800106 struct sirfsoc_register *ureg = &sirfport->uart_reg->uart_reg;
Rong Wang161e7732011-11-17 23:17:04 +0800107 unsigned int assert = mctrl & TIOCM_RTS;
108 unsigned int val = assert ? SIRFUART_AFC_CTRL_RX_THD : 0x0;
109 unsigned int current_val;
Qipan Li2eb56182013-08-15 06:52:15 +0800110
111 if (!sirfport->hw_flow_ctrl || !sirfport->ms_enabled)
112 return;
113 if (sirfport->uart_reg->uart_type == SIRF_REAL_UART) {
Qipan Li5df83112013-08-12 18:15:35 +0800114 current_val = rd_regl(port, ureg->sirfsoc_afc_ctrl) & ~0xFF;
Rong Wang161e7732011-11-17 23:17:04 +0800115 val |= current_val;
Qipan Li5df83112013-08-12 18:15:35 +0800116 wr_regl(port, ureg->sirfsoc_afc_ctrl, val);
Qipan Li2eb56182013-08-15 06:52:15 +0800117 } else {
118 if (!val)
119 gpio_set_value(sirfport->rts_gpio, 1);
120 else
121 gpio_set_value(sirfport->rts_gpio, 0);
Rong Wang161e7732011-11-17 23:17:04 +0800122 }
123}
124
125static void sirfsoc_uart_stop_tx(struct uart_port *port)
126{
Barry Song909102d2013-08-07 13:35:38 +0800127 struct sirfsoc_uart_port *sirfport = to_sirfport(port);
Qipan Li5df83112013-08-12 18:15:35 +0800128 struct sirfsoc_register *ureg = &sirfport->uart_reg->uart_reg;
129 struct sirfsoc_int_en *uint_en = &sirfport->uart_reg->uart_int_en;
Barry Song909102d2013-08-07 13:35:38 +0800130
Qipan Li9be16b32014-01-30 13:57:29 +0800131 if (sirfport->tx_dma_chan) {
Qipan Li8316d042013-08-19 11:47:53 +0800132 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 Song057badd2015-01-03 17:02:57 +0800136 if (!sirfport->is_atlas7)
Qipan Li8316d042013-08-19 11:47:53 +0800137 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
141 wr_regl(port, SIRFUART_INT_EN_CLR,
142 uint_en->sirfsoc_txfifo_empty_en);
143 }
144 } else {
Barry Song057badd2015-01-03 17:02:57 +0800145 if (!sirfport->is_atlas7)
Qipan Li8316d042013-08-19 11:47:53 +0800146 wr_regl(port, ureg->sirfsoc_int_en_reg,
147 rd_regl(port, ureg->sirfsoc_int_en_reg) &
148 ~uint_en->sirfsoc_txfifo_empty_en);
149 else
150 wr_regl(port, SIRFUART_INT_EN_CLR,
151 uint_en->sirfsoc_txfifo_empty_en);
152 }
153}
154
155static void sirfsoc_uart_tx_with_dma(struct sirfsoc_uart_port *sirfport)
156{
157 struct uart_port *port = &sirfport->port;
158 struct sirfsoc_register *ureg = &sirfport->uart_reg->uart_reg;
159 struct sirfsoc_int_en *uint_en = &sirfport->uart_reg->uart_int_en;
160 struct circ_buf *xmit = &port->state->xmit;
161 unsigned long tran_size;
162 unsigned long tran_start;
163 unsigned long pio_tx_size;
164
165 tran_size = CIRC_CNT_TO_END(xmit->head, xmit->tail, UART_XMIT_SIZE);
166 tran_start = (unsigned long)(xmit->buf + xmit->tail);
167 if (uart_circ_empty(xmit) || uart_tx_stopped(port) ||
168 !tran_size)
169 return;
170 if (sirfport->tx_dma_state == TX_DMA_PAUSE) {
171 dmaengine_resume(sirfport->tx_dma_chan);
172 return;
173 }
174 if (sirfport->tx_dma_state == TX_DMA_RUNNING)
175 return;
Barry Song057badd2015-01-03 17:02:57 +0800176 if (!sirfport->is_atlas7)
Qipan Li5df83112013-08-12 18:15:35 +0800177 wr_regl(port, ureg->sirfsoc_int_en_reg,
Qipan Li8316d042013-08-19 11:47:53 +0800178 rd_regl(port, ureg->sirfsoc_int_en_reg)&
179 ~(uint_en->sirfsoc_txfifo_empty_en));
180 else
Qipan Li5df83112013-08-12 18:15:35 +0800181 wr_regl(port, SIRFUART_INT_EN_CLR,
182 uint_en->sirfsoc_txfifo_empty_en);
Qipan Li8316d042013-08-19 11:47:53 +0800183 /*
184 * DMA requires buffer address and buffer length are both aligned with
185 * 4 bytes, so we use PIO for
186 * 1. if address is not aligned with 4bytes, use PIO for the first 1~3
187 * bytes, and move to DMA for the left part aligned with 4bytes
188 * 2. if buffer length is not aligned with 4bytes, use DMA for aligned
189 * part first, move to PIO for the left 1~3 bytes
190 */
191 if (tran_size < 4 || BYTES_TO_ALIGN(tran_start)) {
192 wr_regl(port, ureg->sirfsoc_tx_fifo_op, SIRFUART_FIFO_STOP);
193 wr_regl(port, ureg->sirfsoc_tx_dma_io_ctrl,
194 rd_regl(port, ureg->sirfsoc_tx_dma_io_ctrl)|
195 SIRFUART_IO_MODE);
196 if (BYTES_TO_ALIGN(tran_start)) {
197 pio_tx_size = sirfsoc_uart_pio_tx_chars(sirfport,
198 BYTES_TO_ALIGN(tran_start));
199 tran_size -= pio_tx_size;
200 }
201 if (tran_size < 4)
202 sirfsoc_uart_pio_tx_chars(sirfport, tran_size);
Barry Song057badd2015-01-03 17:02:57 +0800203 if (!sirfport->is_atlas7)
Qipan Li8316d042013-08-19 11:47:53 +0800204 wr_regl(port, ureg->sirfsoc_int_en_reg,
205 rd_regl(port, ureg->sirfsoc_int_en_reg)|
206 uint_en->sirfsoc_txfifo_empty_en);
207 else
208 wr_regl(port, ureg->sirfsoc_int_en_reg,
209 uint_en->sirfsoc_txfifo_empty_en);
210 wr_regl(port, ureg->sirfsoc_tx_fifo_op, SIRFUART_FIFO_START);
211 } else {
212 /* tx transfer mode switch into dma mode */
213 wr_regl(port, ureg->sirfsoc_tx_fifo_op, SIRFUART_FIFO_STOP);
214 wr_regl(port, ureg->sirfsoc_tx_dma_io_ctrl,
215 rd_regl(port, ureg->sirfsoc_tx_dma_io_ctrl)&
216 ~SIRFUART_IO_MODE);
217 wr_regl(port, ureg->sirfsoc_tx_fifo_op, SIRFUART_FIFO_START);
218 tran_size &= ~(0x3);
Qipan Li5df83112013-08-12 18:15:35 +0800219
Qipan Li8316d042013-08-19 11:47:53 +0800220 sirfport->tx_dma_addr = dma_map_single(port->dev,
221 xmit->buf + xmit->tail,
222 tran_size, DMA_TO_DEVICE);
223 sirfport->tx_dma_desc = dmaengine_prep_slave_single(
224 sirfport->tx_dma_chan, sirfport->tx_dma_addr,
225 tran_size, DMA_MEM_TO_DEV, DMA_PREP_INTERRUPT);
226 if (!sirfport->tx_dma_desc) {
227 dev_err(port->dev, "DMA prep slave single fail\n");
228 return;
229 }
230 sirfport->tx_dma_desc->callback =
231 sirfsoc_uart_tx_dma_complete_callback;
232 sirfport->tx_dma_desc->callback_param = (void *)sirfport;
233 sirfport->transfer_size = tran_size;
234
235 dmaengine_submit(sirfport->tx_dma_desc);
236 dma_async_issue_pending(sirfport->tx_dma_chan);
237 sirfport->tx_dma_state = TX_DMA_RUNNING;
238 }
Rong Wang161e7732011-11-17 23:17:04 +0800239}
240
Jingoo Hanada1f443d2013-08-08 17:41:43 +0900241static void sirfsoc_uart_start_tx(struct uart_port *port)
Rong Wang161e7732011-11-17 23:17:04 +0800242{
243 struct sirfsoc_uart_port *sirfport = to_sirfport(port);
Qipan Li5df83112013-08-12 18:15:35 +0800244 struct sirfsoc_register *ureg = &sirfport->uart_reg->uart_reg;
245 struct sirfsoc_int_en *uint_en = &sirfport->uart_reg->uart_int_en;
Qipan Li9be16b32014-01-30 13:57:29 +0800246 if (sirfport->tx_dma_chan)
Qipan Li8316d042013-08-19 11:47:53 +0800247 sirfsoc_uart_tx_with_dma(sirfport);
248 else {
Qipan Licb4595a2015-04-29 06:45:09 +0000249 sirfsoc_uart_pio_tx_chars(sirfport, port->fifosize);
Qipan Li8316d042013-08-19 11:47:53 +0800250 wr_regl(port, ureg->sirfsoc_tx_fifo_op, SIRFUART_FIFO_START);
Barry Song057badd2015-01-03 17:02:57 +0800251 if (!sirfport->is_atlas7)
Qipan Li8316d042013-08-19 11:47:53 +0800252 wr_regl(port, ureg->sirfsoc_int_en_reg,
253 rd_regl(port, ureg->sirfsoc_int_en_reg)|
254 uint_en->sirfsoc_txfifo_empty_en);
255 else
256 wr_regl(port, ureg->sirfsoc_int_en_reg,
257 uint_en->sirfsoc_txfifo_empty_en);
258 }
Rong Wang161e7732011-11-17 23:17:04 +0800259}
260
261static void sirfsoc_uart_stop_rx(struct uart_port *port)
262{
Barry Song909102d2013-08-07 13:35:38 +0800263 struct sirfsoc_uart_port *sirfport = to_sirfport(port);
Qipan Li5df83112013-08-12 18:15:35 +0800264 struct sirfsoc_register *ureg = &sirfport->uart_reg->uart_reg;
265 struct sirfsoc_int_en *uint_en = &sirfport->uart_reg->uart_int_en;
Qipan Li8316d042013-08-19 11:47:53 +0800266
Qipan Li5df83112013-08-12 18:15:35 +0800267 wr_regl(port, ureg->sirfsoc_rx_fifo_op, 0);
Qipan Li9be16b32014-01-30 13:57:29 +0800268 if (sirfport->rx_dma_chan) {
Barry Song057badd2015-01-03 17:02:57 +0800269 if (!sirfport->is_atlas7)
Qipan Li8316d042013-08-19 11:47:53 +0800270 wr_regl(port, ureg->sirfsoc_int_en_reg,
271 rd_regl(port, ureg->sirfsoc_int_en_reg) &
272 ~(SIRFUART_RX_DMA_INT_EN(port, uint_en) |
273 uint_en->sirfsoc_rx_done_en));
274 else
275 wr_regl(port, SIRFUART_INT_EN_CLR,
276 SIRFUART_RX_DMA_INT_EN(port, uint_en)|
277 uint_en->sirfsoc_rx_done_en);
278 dmaengine_terminate_all(sirfport->rx_dma_chan);
279 } else {
Barry Song057badd2015-01-03 17:02:57 +0800280 if (!sirfport->is_atlas7)
Qipan Li8316d042013-08-19 11:47:53 +0800281 wr_regl(port, ureg->sirfsoc_int_en_reg,
282 rd_regl(port, ureg->sirfsoc_int_en_reg)&
283 ~(SIRFUART_RX_IO_INT_EN(port, uint_en)));
284 else
285 wr_regl(port, SIRFUART_INT_EN_CLR,
286 SIRFUART_RX_IO_INT_EN(port, uint_en));
287 }
Rong Wang161e7732011-11-17 23:17:04 +0800288}
289
290static void sirfsoc_uart_disable_ms(struct uart_port *port)
291{
292 struct sirfsoc_uart_port *sirfport = to_sirfport(port);
Qipan Li5df83112013-08-12 18:15:35 +0800293 struct sirfsoc_register *ureg = &sirfport->uart_reg->uart_reg;
294 struct sirfsoc_int_en *uint_en = &sirfport->uart_reg->uart_int_en;
Barry Song909102d2013-08-07 13:35:38 +0800295
Rong Wang161e7732011-11-17 23:17:04 +0800296 if (!sirfport->hw_flow_ctrl)
297 return;
Qipan Li2eb56182013-08-15 06:52:15 +0800298 sirfport->ms_enabled = false;
299 if (sirfport->uart_reg->uart_type == SIRF_REAL_UART) {
300 wr_regl(port, ureg->sirfsoc_afc_ctrl,
301 rd_regl(port, ureg->sirfsoc_afc_ctrl) & ~0x3FF);
Barry Song057badd2015-01-03 17:02:57 +0800302 if (!sirfport->is_atlas7)
Qipan Li2eb56182013-08-15 06:52:15 +0800303 wr_regl(port, ureg->sirfsoc_int_en_reg,
304 rd_regl(port, ureg->sirfsoc_int_en_reg)&
305 ~uint_en->sirfsoc_cts_en);
306 else
307 wr_regl(port, SIRFUART_INT_EN_CLR,
308 uint_en->sirfsoc_cts_en);
Qipan Li5df83112013-08-12 18:15:35 +0800309 } else
Qipan Li2eb56182013-08-15 06:52:15 +0800310 disable_irq(gpio_to_irq(sirfport->cts_gpio));
311}
312
313static irqreturn_t sirfsoc_uart_usp_cts_handler(int irq, void *dev_id)
314{
315 struct sirfsoc_uart_port *sirfport = (struct sirfsoc_uart_port *)dev_id;
316 struct uart_port *port = &sirfport->port;
Qipan Li07d410e2014-05-26 19:02:07 +0800317 spin_lock(&port->lock);
Qipan Li2eb56182013-08-15 06:52:15 +0800318 if (gpio_is_valid(sirfport->cts_gpio) && sirfport->ms_enabled)
319 uart_handle_cts_change(port,
320 !gpio_get_value(sirfport->cts_gpio));
Qipan Li07d410e2014-05-26 19:02:07 +0800321 spin_unlock(&port->lock);
Qipan Li2eb56182013-08-15 06:52:15 +0800322 return IRQ_HANDLED;
Rong Wang161e7732011-11-17 23:17:04 +0800323}
324
325static void sirfsoc_uart_enable_ms(struct uart_port *port)
326{
327 struct sirfsoc_uart_port *sirfport = to_sirfport(port);
Qipan Li5df83112013-08-12 18:15:35 +0800328 struct sirfsoc_register *ureg = &sirfport->uart_reg->uart_reg;
329 struct sirfsoc_int_en *uint_en = &sirfport->uart_reg->uart_int_en;
Barry Song909102d2013-08-07 13:35:38 +0800330
Rong Wang161e7732011-11-17 23:17:04 +0800331 if (!sirfport->hw_flow_ctrl)
332 return;
Qipan Li2eb56182013-08-15 06:52:15 +0800333 sirfport->ms_enabled = true;
334 if (sirfport->uart_reg->uart_type == SIRF_REAL_UART) {
335 wr_regl(port, ureg->sirfsoc_afc_ctrl,
336 rd_regl(port, ureg->sirfsoc_afc_ctrl) |
337 SIRFUART_AFC_TX_EN | SIRFUART_AFC_RX_EN);
Barry Song057badd2015-01-03 17:02:57 +0800338 if (!sirfport->is_atlas7)
Qipan Li2eb56182013-08-15 06:52:15 +0800339 wr_regl(port, ureg->sirfsoc_int_en_reg,
340 rd_regl(port, ureg->sirfsoc_int_en_reg)
341 | uint_en->sirfsoc_cts_en);
342 else
343 wr_regl(port, ureg->sirfsoc_int_en_reg,
344 uint_en->sirfsoc_cts_en);
Qipan Li5df83112013-08-12 18:15:35 +0800345 } else
Qipan Li2eb56182013-08-15 06:52:15 +0800346 enable_irq(gpio_to_irq(sirfport->cts_gpio));
Rong Wang161e7732011-11-17 23:17:04 +0800347}
348
349static void sirfsoc_uart_break_ctl(struct uart_port *port, int break_state)
350{
Qipan Li5df83112013-08-12 18:15:35 +0800351 struct sirfsoc_uart_port *sirfport = to_sirfport(port);
352 struct sirfsoc_register *ureg = &sirfport->uart_reg->uart_reg;
353 if (sirfport->uart_reg->uart_type == SIRF_REAL_UART) {
354 unsigned long ulcon = rd_regl(port, ureg->sirfsoc_line_ctrl);
355 if (break_state)
356 ulcon |= SIRFUART_SET_BREAK;
357 else
358 ulcon &= ~SIRFUART_SET_BREAK;
359 wr_regl(port, ureg->sirfsoc_line_ctrl, ulcon);
360 }
Rong Wang161e7732011-11-17 23:17:04 +0800361}
362
363static unsigned int
364sirfsoc_uart_pio_rx_chars(struct uart_port *port, unsigned int max_rx_count)
365{
Qipan Li5df83112013-08-12 18:15:35 +0800366 struct sirfsoc_uart_port *sirfport = to_sirfport(port);
367 struct sirfsoc_register *ureg = &sirfport->uart_reg->uart_reg;
368 struct sirfsoc_fifo_status *ufifo_st = &sirfport->uart_reg->fifo_status;
Rong Wang161e7732011-11-17 23:17:04 +0800369 unsigned int ch, rx_count = 0;
Qipan Li5df83112013-08-12 18:15:35 +0800370 struct tty_struct *tty;
371 tty = tty_port_tty_get(&port->state->port);
372 if (!tty)
373 return -ENODEV;
374 while (!(rd_regl(port, ureg->sirfsoc_rx_fifo_status) &
Qipan Licb4595a2015-04-29 06:45:09 +0000375 ufifo_st->ff_empty(port))) {
Qipan Li5df83112013-08-12 18:15:35 +0800376 ch = rd_regl(port, ureg->sirfsoc_rx_fifo_data) |
377 SIRFUART_DUMMY_READ;
Rong Wang161e7732011-11-17 23:17:04 +0800378 if (unlikely(uart_handle_sysrq_char(port, ch)))
379 continue;
380 uart_insert_char(port, 0, 0, ch, TTY_NORMAL);
381 rx_count++;
382 if (rx_count >= max_rx_count)
383 break;
384 }
385
Qipan Li8316d042013-08-19 11:47:53 +0800386 sirfport->rx_io_count += rx_count;
Rong Wang161e7732011-11-17 23:17:04 +0800387 port->icount.rx += rx_count;
Viresh Kumar8b9ade92013-08-19 20:14:28 +0530388
Rong Wang161e7732011-11-17 23:17:04 +0800389 return rx_count;
390}
391
392static unsigned int
393sirfsoc_uart_pio_tx_chars(struct sirfsoc_uart_port *sirfport, int count)
394{
395 struct uart_port *port = &sirfport->port;
Qipan Li5df83112013-08-12 18:15:35 +0800396 struct sirfsoc_register *ureg = &sirfport->uart_reg->uart_reg;
397 struct sirfsoc_fifo_status *ufifo_st = &sirfport->uart_reg->fifo_status;
Rong Wang161e7732011-11-17 23:17:04 +0800398 struct circ_buf *xmit = &port->state->xmit;
399 unsigned int num_tx = 0;
400 while (!uart_circ_empty(xmit) &&
Qipan Li5df83112013-08-12 18:15:35 +0800401 !(rd_regl(port, ureg->sirfsoc_tx_fifo_status) &
Qipan Licb4595a2015-04-29 06:45:09 +0000402 ufifo_st->ff_full(port)) &&
Rong Wang161e7732011-11-17 23:17:04 +0800403 count--) {
Qipan Li5df83112013-08-12 18:15:35 +0800404 wr_regl(port, ureg->sirfsoc_tx_fifo_data,
405 xmit->buf[xmit->tail]);
Rong Wang161e7732011-11-17 23:17:04 +0800406 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
407 port->icount.tx++;
408 num_tx++;
409 }
410 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
411 uart_write_wakeup(port);
412 return num_tx;
413}
414
Qipan Li8316d042013-08-19 11:47:53 +0800415static void sirfsoc_uart_tx_dma_complete_callback(void *param)
416{
417 struct sirfsoc_uart_port *sirfport = (struct sirfsoc_uart_port *)param;
418 struct uart_port *port = &sirfport->port;
419 struct circ_buf *xmit = &port->state->xmit;
420 unsigned long flags;
421
Qipan Li07d410e2014-05-26 19:02:07 +0800422 spin_lock_irqsave(&port->lock, flags);
Qipan Li8316d042013-08-19 11:47:53 +0800423 xmit->tail = (xmit->tail + sirfport->transfer_size) &
424 (UART_XMIT_SIZE - 1);
425 port->icount.tx += sirfport->transfer_size;
426 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
427 uart_write_wakeup(port);
428 if (sirfport->tx_dma_addr)
429 dma_unmap_single(port->dev, sirfport->tx_dma_addr,
430 sirfport->transfer_size, DMA_TO_DEVICE);
Qipan Li8316d042013-08-19 11:47:53 +0800431 sirfport->tx_dma_state = TX_DMA_IDLE;
432 sirfsoc_uart_tx_with_dma(sirfport);
Qipan Li07d410e2014-05-26 19:02:07 +0800433 spin_unlock_irqrestore(&port->lock, flags);
Qipan Li8316d042013-08-19 11:47:53 +0800434}
435
436static void sirfsoc_uart_insert_rx_buf_to_tty(
437 struct sirfsoc_uart_port *sirfport, int count)
438{
439 struct uart_port *port = &sirfport->port;
440 struct tty_port *tport = &port->state->port;
441 int inserted;
442
443 inserted = tty_insert_flip_string(tport,
444 sirfport->rx_dma_items[sirfport->rx_completed].xmit.buf, count);
445 port->icount.rx += inserted;
Qipan Li8316d042013-08-19 11:47:53 +0800446}
447
448static void sirfsoc_rx_submit_one_dma_desc(struct uart_port *port, int index)
449{
450 struct sirfsoc_uart_port *sirfport = to_sirfport(port);
451
452 sirfport->rx_dma_items[index].xmit.tail =
453 sirfport->rx_dma_items[index].xmit.head = 0;
454 sirfport->rx_dma_items[index].desc =
455 dmaengine_prep_slave_single(sirfport->rx_dma_chan,
456 sirfport->rx_dma_items[index].dma_addr, SIRFSOC_RX_DMA_BUF_SIZE,
457 DMA_DEV_TO_MEM, DMA_PREP_INTERRUPT);
458 if (!sirfport->rx_dma_items[index].desc) {
459 dev_err(port->dev, "DMA slave single fail\n");
460 return;
461 }
462 sirfport->rx_dma_items[index].desc->callback =
463 sirfsoc_uart_rx_dma_complete_callback;
464 sirfport->rx_dma_items[index].desc->callback_param = sirfport;
465 sirfport->rx_dma_items[index].cookie =
466 dmaengine_submit(sirfport->rx_dma_items[index].desc);
467 dma_async_issue_pending(sirfport->rx_dma_chan);
468}
469
470static void sirfsoc_rx_tmo_process_tl(unsigned long param)
471{
472 struct sirfsoc_uart_port *sirfport = (struct sirfsoc_uart_port *)param;
473 struct uart_port *port = &sirfport->port;
474 struct sirfsoc_register *ureg = &sirfport->uart_reg->uart_reg;
475 struct sirfsoc_int_en *uint_en = &sirfport->uart_reg->uart_int_en;
476 struct sirfsoc_int_status *uint_st = &sirfport->uart_reg->uart_int_st;
477 unsigned int count;
478 unsigned long flags;
Qipan Lidf8d4aa2014-01-03 15:44:08 +0800479 struct dma_tx_state tx_state;
Qipan Li8316d042013-08-19 11:47:53 +0800480
Qipan Li07d410e2014-05-26 19:02:07 +0800481 spin_lock_irqsave(&port->lock, flags);
Qipan Lidf8d4aa2014-01-03 15:44:08 +0800482 while (DMA_COMPLETE == dmaengine_tx_status(sirfport->rx_dma_chan,
483 sirfport->rx_dma_items[sirfport->rx_completed].cookie, &tx_state)) {
Qipan Li8316d042013-08-19 11:47:53 +0800484 sirfsoc_uart_insert_rx_buf_to_tty(sirfport,
485 SIRFSOC_RX_DMA_BUF_SIZE);
Qipan Li59f8a622013-09-21 09:02:10 +0800486 sirfport->rx_completed++;
Qipan Li8316d042013-08-19 11:47:53 +0800487 sirfport->rx_completed %= SIRFSOC_RX_LOOP_BUF_CNT;
488 }
489 count = CIRC_CNT(sirfport->rx_dma_items[sirfport->rx_issued].xmit.head,
490 sirfport->rx_dma_items[sirfport->rx_issued].xmit.tail,
491 SIRFSOC_RX_DMA_BUF_SIZE);
492 if (count > 0)
493 sirfsoc_uart_insert_rx_buf_to_tty(sirfport, count);
494 wr_regl(port, ureg->sirfsoc_rx_dma_io_ctrl,
495 rd_regl(port, ureg->sirfsoc_rx_dma_io_ctrl) |
496 SIRFUART_IO_MODE);
Qipan Lifb78b812014-01-27 14:23:39 +0800497 sirfsoc_uart_pio_rx_chars(port, 4 - sirfport->rx_io_count);
Qipan Li8316d042013-08-19 11:47:53 +0800498 if (sirfport->rx_io_count == 4) {
Qipan Li8316d042013-08-19 11:47:53 +0800499 sirfport->rx_io_count = 0;
500 wr_regl(port, ureg->sirfsoc_int_st_reg,
501 uint_st->sirfsoc_rx_done);
Barry Song057badd2015-01-03 17:02:57 +0800502 if (!sirfport->is_atlas7)
Qipan Li8316d042013-08-19 11:47:53 +0800503 wr_regl(port, ureg->sirfsoc_int_en_reg,
504 rd_regl(port, ureg->sirfsoc_int_en_reg) &
505 ~(uint_en->sirfsoc_rx_done_en));
506 else
507 wr_regl(port, SIRFUART_INT_EN_CLR,
508 uint_en->sirfsoc_rx_done_en);
Qipan Li8316d042013-08-19 11:47:53 +0800509 sirfsoc_uart_start_next_rx_dma(port);
510 } else {
Qipan Li8316d042013-08-19 11:47:53 +0800511 wr_regl(port, ureg->sirfsoc_int_st_reg,
512 uint_st->sirfsoc_rx_done);
Barry Song057badd2015-01-03 17:02:57 +0800513 if (!sirfport->is_atlas7)
Qipan Li8316d042013-08-19 11:47:53 +0800514 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
518 wr_regl(port, ureg->sirfsoc_int_en_reg,
519 uint_en->sirfsoc_rx_done_en);
Qipan Li8316d042013-08-19 11:47:53 +0800520 }
Qipan Li07d410e2014-05-26 19:02:07 +0800521 spin_unlock_irqrestore(&port->lock, flags);
522 tty_flip_buffer_push(&port->state->port);
Qipan Li8316d042013-08-19 11:47:53 +0800523}
524
525static void sirfsoc_uart_handle_rx_tmo(struct sirfsoc_uart_port *sirfport)
526{
527 struct uart_port *port = &sirfport->port;
528 struct sirfsoc_register *ureg = &sirfport->uart_reg->uart_reg;
529 struct sirfsoc_int_en *uint_en = &sirfport->uart_reg->uart_int_en;
530 struct dma_tx_state tx_state;
Qipan Li8316d042013-08-19 11:47:53 +0800531 dmaengine_tx_status(sirfport->rx_dma_chan,
532 sirfport->rx_dma_items[sirfport->rx_issued].cookie, &tx_state);
533 dmaengine_terminate_all(sirfport->rx_dma_chan);
534 sirfport->rx_dma_items[sirfport->rx_issued].xmit.head =
535 SIRFSOC_RX_DMA_BUF_SIZE - tx_state.residue;
Barry Song057badd2015-01-03 17:02:57 +0800536 if (!sirfport->is_atlas7)
Qipan Li8316d042013-08-19 11:47:53 +0800537 wr_regl(port, ureg->sirfsoc_int_en_reg,
538 rd_regl(port, ureg->sirfsoc_int_en_reg) &
539 ~(uint_en->sirfsoc_rx_timeout_en));
540 else
541 wr_regl(port, SIRFUART_INT_EN_CLR,
542 uint_en->sirfsoc_rx_timeout_en);
Qipan Li8316d042013-08-19 11:47:53 +0800543 tasklet_schedule(&sirfport->rx_tmo_process_tasklet);
544}
545
546static void sirfsoc_uart_handle_rx_done(struct sirfsoc_uart_port *sirfport)
547{
548 struct uart_port *port = &sirfport->port;
549 struct sirfsoc_register *ureg = &sirfport->uart_reg->uart_reg;
550 struct sirfsoc_int_en *uint_en = &sirfport->uart_reg->uart_int_en;
551 struct sirfsoc_int_status *uint_st = &sirfport->uart_reg->uart_int_st;
552
553 sirfsoc_uart_pio_rx_chars(port, 4 - sirfport->rx_io_count);
554 if (sirfport->rx_io_count == 4) {
555 sirfport->rx_io_count = 0;
Barry Song057badd2015-01-03 17:02:57 +0800556 if (!sirfport->is_atlas7)
Qipan Li8316d042013-08-19 11:47:53 +0800557 wr_regl(port, ureg->sirfsoc_int_en_reg,
558 rd_regl(port, ureg->sirfsoc_int_en_reg) &
559 ~(uint_en->sirfsoc_rx_done_en));
560 else
561 wr_regl(port, SIRFUART_INT_EN_CLR,
562 uint_en->sirfsoc_rx_done_en);
563 wr_regl(port, ureg->sirfsoc_int_st_reg,
564 uint_st->sirfsoc_rx_timeout);
565 sirfsoc_uart_start_next_rx_dma(port);
566 }
567}
568
Rong Wang161e7732011-11-17 23:17:04 +0800569static irqreturn_t sirfsoc_uart_isr(int irq, void *dev_id)
570{
571 unsigned long intr_status;
572 unsigned long cts_status;
573 unsigned long flag = TTY_NORMAL;
574 struct sirfsoc_uart_port *sirfport = (struct sirfsoc_uart_port *)dev_id;
575 struct uart_port *port = &sirfport->port;
Qipan Li5df83112013-08-12 18:15:35 +0800576 struct sirfsoc_register *ureg = &sirfport->uart_reg->uart_reg;
577 struct sirfsoc_fifo_status *ufifo_st = &sirfport->uart_reg->fifo_status;
578 struct sirfsoc_int_status *uint_st = &sirfport->uart_reg->uart_int_st;
579 struct sirfsoc_int_en *uint_en = &sirfport->uart_reg->uart_int_en;
Rong Wang161e7732011-11-17 23:17:04 +0800580 struct uart_state *state = port->state;
581 struct circ_buf *xmit = &port->state->xmit;
Barry Song5425e032012-12-25 17:32:04 +0800582 spin_lock(&port->lock);
Qipan Li5df83112013-08-12 18:15:35 +0800583 intr_status = rd_regl(port, ureg->sirfsoc_int_st_reg);
584 wr_regl(port, ureg->sirfsoc_int_st_reg, intr_status);
Qipan Li8316d042013-08-19 11:47:53 +0800585 intr_status &= rd_regl(port, ureg->sirfsoc_int_en_reg);
Qipan Li5df83112013-08-12 18:15:35 +0800586 if (unlikely(intr_status & (SIRFUART_ERR_INT_STAT(port, uint_st)))) {
587 if (intr_status & uint_st->sirfsoc_rxd_brk) {
588 port->icount.brk++;
Rong Wang161e7732011-11-17 23:17:04 +0800589 if (uart_handle_break(port))
590 goto recv_char;
Rong Wang161e7732011-11-17 23:17:04 +0800591 }
Qipan Li5df83112013-08-12 18:15:35 +0800592 if (intr_status & uint_st->sirfsoc_rx_oflow)
Rong Wang161e7732011-11-17 23:17:04 +0800593 port->icount.overrun++;
Qipan Li5df83112013-08-12 18:15:35 +0800594 if (intr_status & uint_st->sirfsoc_frm_err) {
Rong Wang161e7732011-11-17 23:17:04 +0800595 port->icount.frame++;
596 flag = TTY_FRAME;
597 }
Qipan Li5df83112013-08-12 18:15:35 +0800598 if (intr_status & uint_st->sirfsoc_parity_err)
Rong Wang161e7732011-11-17 23:17:04 +0800599 flag = TTY_PARITY;
Qipan Li5df83112013-08-12 18:15:35 +0800600 wr_regl(port, ureg->sirfsoc_rx_fifo_op, SIRFUART_FIFO_RESET);
601 wr_regl(port, ureg->sirfsoc_rx_fifo_op, 0);
602 wr_regl(port, ureg->sirfsoc_rx_fifo_op, SIRFUART_FIFO_START);
Rong Wang161e7732011-11-17 23:17:04 +0800603 intr_status &= port->read_status_mask;
604 uart_insert_char(port, intr_status,
Qipan Li5df83112013-08-12 18:15:35 +0800605 uint_en->sirfsoc_rx_oflow_en, 0, flag);
Rong Wang161e7732011-11-17 23:17:04 +0800606 }
607recv_char:
Qipan Li5df83112013-08-12 18:15:35 +0800608 if ((sirfport->uart_reg->uart_type == SIRF_REAL_UART) &&
Qipan Li8316d042013-08-19 11:47:53 +0800609 (intr_status & SIRFUART_CTS_INT_ST(uint_st)) &&
610 !sirfport->tx_dma_state) {
Qipan Li5df83112013-08-12 18:15:35 +0800611 cts_status = rd_regl(port, ureg->sirfsoc_afc_ctrl) &
612 SIRFUART_AFC_CTS_STATUS;
613 if (cts_status != 0)
614 cts_status = 0;
615 else
616 cts_status = 1;
617 uart_handle_cts_change(port, cts_status);
618 wake_up_interruptible(&state->port.delta_msr_wait);
Rong Wang161e7732011-11-17 23:17:04 +0800619 }
Qipan Li9be16b32014-01-30 13:57:29 +0800620 if (sirfport->rx_dma_chan) {
Qipan Li8316d042013-08-19 11:47:53 +0800621 if (intr_status & uint_st->sirfsoc_rx_timeout)
622 sirfsoc_uart_handle_rx_tmo(sirfport);
623 if (intr_status & uint_st->sirfsoc_rx_done)
624 sirfsoc_uart_handle_rx_done(sirfport);
625 } else {
626 if (intr_status & SIRFUART_RX_IO_INT_ST(uint_st))
Qipan Licb4595a2015-04-29 06:45:09 +0000627 sirfsoc_uart_pio_rx_chars(port, port->fifosize);
Qipan Li8316d042013-08-19 11:47:53 +0800628 }
Qipan Li07d410e2014-05-26 19:02:07 +0800629 spin_unlock(&port->lock);
630 tty_flip_buffer_push(&state->port);
631 spin_lock(&port->lock);
Qipan Li5df83112013-08-12 18:15:35 +0800632 if (intr_status & uint_st->sirfsoc_txfifo_empty) {
Qipan Li9be16b32014-01-30 13:57:29 +0800633 if (sirfport->tx_dma_chan)
Qipan Li8316d042013-08-19 11:47:53 +0800634 sirfsoc_uart_tx_with_dma(sirfport);
635 else {
636 if (uart_circ_empty(xmit) || uart_tx_stopped(port)) {
637 spin_unlock(&port->lock);
638 return IRQ_HANDLED;
639 } else {
640 sirfsoc_uart_pio_tx_chars(sirfport,
Qipan Licb4595a2015-04-29 06:45:09 +0000641 port->fifosize);
Qipan Li8316d042013-08-19 11:47:53 +0800642 if ((uart_circ_empty(xmit)) &&
Qipan Li5df83112013-08-12 18:15:35 +0800643 (rd_regl(port, ureg->sirfsoc_tx_fifo_status) &
Qipan Licb4595a2015-04-29 06:45:09 +0000644 ufifo_st->ff_empty(port)))
Qipan Li8316d042013-08-19 11:47:53 +0800645 sirfsoc_uart_stop_tx(port);
646 }
Rong Wang161e7732011-11-17 23:17:04 +0800647 }
648 }
Barry Song5425e032012-12-25 17:32:04 +0800649 spin_unlock(&port->lock);
Qipan Li07d410e2014-05-26 19:02:07 +0800650
Rong Wang161e7732011-11-17 23:17:04 +0800651 return IRQ_HANDLED;
652}
653
Qipan Li8316d042013-08-19 11:47:53 +0800654static void sirfsoc_uart_rx_dma_complete_tl(unsigned long param)
655{
656 struct sirfsoc_uart_port *sirfport = (struct sirfsoc_uart_port *)param;
657 struct uart_port *port = &sirfport->port;
Qipan Li59f8a622013-09-21 09:02:10 +0800658 struct sirfsoc_register *ureg = &sirfport->uart_reg->uart_reg;
659 struct sirfsoc_int_en *uint_en = &sirfport->uart_reg->uart_int_en;
Qipan Li8316d042013-08-19 11:47:53 +0800660 unsigned long flags;
Qipan Lidf8d4aa2014-01-03 15:44:08 +0800661 struct dma_tx_state tx_state;
Daniel Thompson58eb97c2014-05-29 11:13:43 +0100662 spin_lock_irqsave(&port->lock, flags);
Qipan Lidf8d4aa2014-01-03 15:44:08 +0800663 while (DMA_COMPLETE == dmaengine_tx_status(sirfport->rx_dma_chan,
664 sirfport->rx_dma_items[sirfport->rx_completed].cookie, &tx_state)) {
Qipan Li8316d042013-08-19 11:47:53 +0800665 sirfsoc_uart_insert_rx_buf_to_tty(sirfport,
666 SIRFSOC_RX_DMA_BUF_SIZE);
Qipan Li59f8a622013-09-21 09:02:10 +0800667 if (rd_regl(port, ureg->sirfsoc_int_en_reg) &
668 uint_en->sirfsoc_rx_timeout_en)
669 sirfsoc_rx_submit_one_dma_desc(port,
670 sirfport->rx_completed++);
671 else
672 sirfport->rx_completed++;
Qipan Li8316d042013-08-19 11:47:53 +0800673 sirfport->rx_completed %= SIRFSOC_RX_LOOP_BUF_CNT;
674 }
Qipan Li07d410e2014-05-26 19:02:07 +0800675 spin_unlock_irqrestore(&port->lock, flags);
676 tty_flip_buffer_push(&port->state->port);
Qipan Li8316d042013-08-19 11:47:53 +0800677}
678
679static void sirfsoc_uart_rx_dma_complete_callback(void *param)
680{
681 struct sirfsoc_uart_port *sirfport = (struct sirfsoc_uart_port *)param;
Qipan Li07d410e2014-05-26 19:02:07 +0800682 unsigned long flags;
683
684 spin_lock_irqsave(&sirfport->port.lock, flags);
Qipan Li8316d042013-08-19 11:47:53 +0800685 sirfport->rx_issued++;
686 sirfport->rx_issued %= SIRFSOC_RX_LOOP_BUF_CNT;
Qipan Li8316d042013-08-19 11:47:53 +0800687 tasklet_schedule(&sirfport->rx_dma_complete_tasklet);
Qipan Li07d410e2014-05-26 19:02:07 +0800688 spin_unlock_irqrestore(&sirfport->port.lock, flags);
Qipan Li8316d042013-08-19 11:47:53 +0800689}
690
691/* submit rx dma task into dmaengine */
692static void sirfsoc_uart_start_next_rx_dma(struct uart_port *port)
693{
694 struct sirfsoc_uart_port *sirfport = to_sirfport(port);
695 struct sirfsoc_register *ureg = &sirfport->uart_reg->uart_reg;
696 struct sirfsoc_int_en *uint_en = &sirfport->uart_reg->uart_int_en;
Qipan Li8316d042013-08-19 11:47:53 +0800697 int i;
Qipan Li8316d042013-08-19 11:47:53 +0800698 sirfport->rx_io_count = 0;
699 wr_regl(port, ureg->sirfsoc_rx_dma_io_ctrl,
700 rd_regl(port, ureg->sirfsoc_rx_dma_io_ctrl) &
701 ~SIRFUART_IO_MODE);
Qipan Li8316d042013-08-19 11:47:53 +0800702 for (i = 0; i < SIRFSOC_RX_LOOP_BUF_CNT; i++)
703 sirfsoc_rx_submit_one_dma_desc(port, i);
704 sirfport->rx_completed = sirfport->rx_issued = 0;
Barry Song057badd2015-01-03 17:02:57 +0800705 if (!sirfport->is_atlas7)
Qipan Li8316d042013-08-19 11:47:53 +0800706 wr_regl(port, ureg->sirfsoc_int_en_reg,
707 rd_regl(port, ureg->sirfsoc_int_en_reg) |
708 SIRFUART_RX_DMA_INT_EN(port, uint_en));
709 else
710 wr_regl(port, ureg->sirfsoc_int_en_reg,
711 SIRFUART_RX_DMA_INT_EN(port, uint_en));
Qipan Li8316d042013-08-19 11:47:53 +0800712}
713
Rong Wang161e7732011-11-17 23:17:04 +0800714static void sirfsoc_uart_start_rx(struct uart_port *port)
715{
Barry Song909102d2013-08-07 13:35:38 +0800716 struct sirfsoc_uart_port *sirfport = to_sirfport(port);
Qipan Li5df83112013-08-12 18:15:35 +0800717 struct sirfsoc_register *ureg = &sirfport->uart_reg->uart_reg;
718 struct sirfsoc_int_en *uint_en = &sirfport->uart_reg->uart_int_en;
Qipan Li8316d042013-08-19 11:47:53 +0800719
720 sirfport->rx_io_count = 0;
Qipan Li5df83112013-08-12 18:15:35 +0800721 wr_regl(port, ureg->sirfsoc_rx_fifo_op, SIRFUART_FIFO_RESET);
722 wr_regl(port, ureg->sirfsoc_rx_fifo_op, 0);
723 wr_regl(port, ureg->sirfsoc_rx_fifo_op, SIRFUART_FIFO_START);
Qipan Li9be16b32014-01-30 13:57:29 +0800724 if (sirfport->rx_dma_chan)
Qipan Li8316d042013-08-19 11:47:53 +0800725 sirfsoc_uart_start_next_rx_dma(port);
726 else {
Barry Song057badd2015-01-03 17:02:57 +0800727 if (!sirfport->is_atlas7)
Qipan Li8316d042013-08-19 11:47:53 +0800728 wr_regl(port, ureg->sirfsoc_int_en_reg,
729 rd_regl(port, ureg->sirfsoc_int_en_reg) |
730 SIRFUART_RX_IO_INT_EN(port, uint_en));
731 else
732 wr_regl(port, ureg->sirfsoc_int_en_reg,
733 SIRFUART_RX_IO_INT_EN(port, uint_en));
734 }
Rong Wang161e7732011-11-17 23:17:04 +0800735}
736
737static unsigned int
Qipan Li5df83112013-08-12 18:15:35 +0800738sirfsoc_usp_calc_sample_div(unsigned long set_rate,
739 unsigned long ioclk_rate, unsigned long *sample_reg)
740{
741 unsigned long min_delta = ~0UL;
742 unsigned short sample_div;
743 unsigned long ioclk_div = 0;
744 unsigned long temp_delta;
745
Qipan Licb4595a2015-04-29 06:45:09 +0000746 for (sample_div = SIRF_USP_MIN_SAMPLE_DIV;
Qipan Li5df83112013-08-12 18:15:35 +0800747 sample_div <= SIRF_MAX_SAMPLE_DIV; sample_div++) {
748 temp_delta = ioclk_rate -
749 (ioclk_rate + (set_rate * sample_div) / 2)
750 / (set_rate * sample_div) * set_rate * sample_div;
751
752 temp_delta = (temp_delta > 0) ? temp_delta : -temp_delta;
753 if (temp_delta < min_delta) {
754 ioclk_div = (2 * ioclk_rate /
755 (set_rate * sample_div) + 1) / 2 - 1;
756 if (ioclk_div > SIRF_IOCLK_DIV_MAX)
757 continue;
758 min_delta = temp_delta;
759 *sample_reg = sample_div;
760 if (!temp_delta)
761 break;
762 }
763 }
764 return ioclk_div;
765}
766
767static unsigned int
768sirfsoc_uart_calc_sample_div(unsigned long baud_rate,
769 unsigned long ioclk_rate, unsigned long *set_baud)
Rong Wang161e7732011-11-17 23:17:04 +0800770{
771 unsigned long min_delta = ~0UL;
772 unsigned short sample_div;
773 unsigned int regv = 0;
774 unsigned long ioclk_div;
775 unsigned long baud_tmp;
776 int temp_delta;
777
778 for (sample_div = SIRF_MIN_SAMPLE_DIV;
779 sample_div <= SIRF_MAX_SAMPLE_DIV; sample_div++) {
780 ioclk_div = (ioclk_rate / (baud_rate * (sample_div + 1))) - 1;
781 if (ioclk_div > SIRF_IOCLK_DIV_MAX)
782 continue;
783 baud_tmp = ioclk_rate / ((ioclk_div + 1) * (sample_div + 1));
784 temp_delta = baud_tmp - baud_rate;
785 temp_delta = (temp_delta > 0) ? temp_delta : -temp_delta;
786 if (temp_delta < min_delta) {
787 regv = regv & (~SIRF_IOCLK_DIV_MASK);
788 regv = regv | ioclk_div;
789 regv = regv & (~SIRF_SAMPLE_DIV_MASK);
790 regv = regv | (sample_div << SIRF_SAMPLE_DIV_SHIFT);
791 min_delta = temp_delta;
Qipan Li5df83112013-08-12 18:15:35 +0800792 *set_baud = baud_tmp;
Rong Wang161e7732011-11-17 23:17:04 +0800793 }
794 }
795 return regv;
796}
797
798static void sirfsoc_uart_set_termios(struct uart_port *port,
799 struct ktermios *termios,
800 struct ktermios *old)
801{
802 struct sirfsoc_uart_port *sirfport = to_sirfport(port);
Qipan Li5df83112013-08-12 18:15:35 +0800803 struct sirfsoc_register *ureg = &sirfport->uart_reg->uart_reg;
804 struct sirfsoc_int_en *uint_en = &sirfport->uart_reg->uart_int_en;
Rong Wang161e7732011-11-17 23:17:04 +0800805 unsigned long config_reg = 0;
806 unsigned long baud_rate;
Qipan Li5df83112013-08-12 18:15:35 +0800807 unsigned long set_baud;
Rong Wang161e7732011-11-17 23:17:04 +0800808 unsigned long flags;
809 unsigned long ic;
810 unsigned int clk_div_reg = 0;
Qipan Li8316d042013-08-19 11:47:53 +0800811 unsigned long txfifo_op_reg, ioclk_rate;
Rong Wang161e7732011-11-17 23:17:04 +0800812 unsigned long rx_time_out;
813 int threshold_div;
Qipan Li5df83112013-08-12 18:15:35 +0800814 u32 data_bit_len, stop_bit_len, len_val;
815 unsigned long sample_div_reg = 0xf;
816 ioclk_rate = port->uartclk;
Rong Wang161e7732011-11-17 23:17:04 +0800817
Rong Wang161e7732011-11-17 23:17:04 +0800818 switch (termios->c_cflag & CSIZE) {
819 default:
820 case CS8:
Qipan Li5df83112013-08-12 18:15:35 +0800821 data_bit_len = 8;
Rong Wang161e7732011-11-17 23:17:04 +0800822 config_reg |= SIRFUART_DATA_BIT_LEN_8;
823 break;
824 case CS7:
Qipan Li5df83112013-08-12 18:15:35 +0800825 data_bit_len = 7;
Rong Wang161e7732011-11-17 23:17:04 +0800826 config_reg |= SIRFUART_DATA_BIT_LEN_7;
827 break;
828 case CS6:
Qipan Li5df83112013-08-12 18:15:35 +0800829 data_bit_len = 6;
Rong Wang161e7732011-11-17 23:17:04 +0800830 config_reg |= SIRFUART_DATA_BIT_LEN_6;
831 break;
832 case CS5:
Qipan Li5df83112013-08-12 18:15:35 +0800833 data_bit_len = 5;
Rong Wang161e7732011-11-17 23:17:04 +0800834 config_reg |= SIRFUART_DATA_BIT_LEN_5;
835 break;
836 }
Qipan Li5df83112013-08-12 18:15:35 +0800837 if (termios->c_cflag & CSTOPB) {
Rong Wang161e7732011-11-17 23:17:04 +0800838 config_reg |= SIRFUART_STOP_BIT_LEN_2;
Qipan Li5df83112013-08-12 18:15:35 +0800839 stop_bit_len = 2;
840 } else
841 stop_bit_len = 1;
842
Rong Wang161e7732011-11-17 23:17:04 +0800843 spin_lock_irqsave(&port->lock, flags);
Qipan Li5df83112013-08-12 18:15:35 +0800844 port->read_status_mask = uint_en->sirfsoc_rx_oflow_en;
Rong Wang161e7732011-11-17 23:17:04 +0800845 port->ignore_status_mask = 0;
Qipan Li5df83112013-08-12 18:15:35 +0800846 if (sirfport->uart_reg->uart_type == SIRF_REAL_UART) {
847 if (termios->c_iflag & INPCK)
848 port->read_status_mask |= uint_en->sirfsoc_frm_err_en |
849 uint_en->sirfsoc_parity_err_en;
Qipan Li2eb56182013-08-15 06:52:15 +0800850 } else {
Qipan Li5df83112013-08-12 18:15:35 +0800851 if (termios->c_iflag & INPCK)
852 port->read_status_mask |= uint_en->sirfsoc_frm_err_en;
853 }
Peter Hurleyef8b9dd2014-06-16 08:10:41 -0400854 if (termios->c_iflag & (IGNBRK | BRKINT | PARMRK))
Qipan Li5df83112013-08-12 18:15:35 +0800855 port->read_status_mask |= uint_en->sirfsoc_rxd_brk_en;
856 if (sirfport->uart_reg->uart_type == SIRF_REAL_UART) {
857 if (termios->c_iflag & IGNPAR)
858 port->ignore_status_mask |=
859 uint_en->sirfsoc_frm_err_en |
860 uint_en->sirfsoc_parity_err_en;
861 if (termios->c_cflag & PARENB) {
862 if (termios->c_cflag & CMSPAR) {
863 if (termios->c_cflag & PARODD)
864 config_reg |= SIRFUART_STICK_BIT_MARK;
865 else
866 config_reg |= SIRFUART_STICK_BIT_SPACE;
867 } else if (termios->c_cflag & PARODD) {
868 config_reg |= SIRFUART_STICK_BIT_ODD;
869 } else {
870 config_reg |= SIRFUART_STICK_BIT_EVEN;
871 }
Rong Wang161e7732011-11-17 23:17:04 +0800872 }
Qipan Li2eb56182013-08-15 06:52:15 +0800873 } else {
Qipan Li5df83112013-08-12 18:15:35 +0800874 if (termios->c_iflag & IGNPAR)
875 port->ignore_status_mask |=
876 uint_en->sirfsoc_frm_err_en;
877 if (termios->c_cflag & PARENB)
878 dev_warn(port->dev,
879 "USP-UART not support parity err\n");
880 }
881 if (termios->c_iflag & IGNBRK) {
882 port->ignore_status_mask |=
883 uint_en->sirfsoc_rxd_brk_en;
884 if (termios->c_iflag & IGNPAR)
885 port->ignore_status_mask |=
886 uint_en->sirfsoc_rx_oflow_en;
887 }
888 if ((termios->c_cflag & CREAD) == 0)
889 port->ignore_status_mask |= SIRFUART_DUMMY_READ;
Rong Wang161e7732011-11-17 23:17:04 +0800890 /* Hardware Flow Control Settings */
891 if (UART_ENABLE_MS(port, termios->c_cflag)) {
892 if (!sirfport->ms_enabled)
893 sirfsoc_uart_enable_ms(port);
894 } else {
895 if (sirfport->ms_enabled)
896 sirfsoc_uart_disable_ms(port);
897 }
Qipan Li5df83112013-08-12 18:15:35 +0800898 baud_rate = uart_get_baud_rate(port, termios, old, 0, 4000000);
899 if (ioclk_rate == 150000000) {
Barry Songac4ce712013-01-16 14:49:27 +0800900 for (ic = 0; ic < SIRF_BAUD_RATE_SUPPORT_NR; ic++)
901 if (baud_rate == baudrate_to_regv[ic].baud_rate)
902 clk_div_reg = baudrate_to_regv[ic].reg_val;
903 }
Qipan Li5df83112013-08-12 18:15:35 +0800904 set_baud = baud_rate;
905 if (sirfport->uart_reg->uart_type == SIRF_REAL_UART) {
906 if (unlikely(clk_div_reg == 0))
907 clk_div_reg = sirfsoc_uart_calc_sample_div(baud_rate,
908 ioclk_rate, &set_baud);
909 wr_regl(port, ureg->sirfsoc_divisor, clk_div_reg);
Qipan Li2eb56182013-08-15 06:52:15 +0800910 } else {
Qipan Li5df83112013-08-12 18:15:35 +0800911 clk_div_reg = sirfsoc_usp_calc_sample_div(baud_rate,
912 ioclk_rate, &sample_div_reg);
913 sample_div_reg--;
914 set_baud = ((ioclk_rate / (clk_div_reg+1) - 1) /
915 (sample_div_reg + 1));
916 /* setting usp mode 2 */
Qipan Li459f15c2013-08-25 20:18:40 +0800917 len_val = ((1 << SIRFSOC_USP_MODE2_RXD_DELAY_OFFSET) |
918 (1 << SIRFSOC_USP_MODE2_TXD_DELAY_OFFSET));
919 len_val |= ((clk_div_reg & SIRFSOC_USP_MODE2_CLK_DIVISOR_MASK)
920 << SIRFSOC_USP_MODE2_CLK_DIVISOR_OFFSET);
921 wr_regl(port, ureg->sirfsoc_mode2, len_val);
Qipan Li5df83112013-08-12 18:15:35 +0800922 }
Rong Wang161e7732011-11-17 23:17:04 +0800923 if (tty_termios_baud_rate(termios))
Qipan Li5df83112013-08-12 18:15:35 +0800924 tty_termios_encode_baud_rate(termios, set_baud, set_baud);
925 /* set receive timeout && data bits len */
926 rx_time_out = SIRFSOC_UART_RX_TIMEOUT(set_baud, 20000);
927 rx_time_out = SIRFUART_RECV_TIMEOUT_VALUE(rx_time_out);
Qipan Li8316d042013-08-19 11:47:53 +0800928 txfifo_op_reg = rd_regl(port, ureg->sirfsoc_tx_fifo_op);
Qipan Li459f15c2013-08-25 20:18:40 +0800929 wr_regl(port, ureg->sirfsoc_rx_fifo_op, SIRFUART_FIFO_STOP);
Qipan Li5df83112013-08-12 18:15:35 +0800930 wr_regl(port, ureg->sirfsoc_tx_fifo_op,
Qipan Li8316d042013-08-19 11:47:53 +0800931 (txfifo_op_reg & ~SIRFUART_FIFO_START));
Qipan Li5df83112013-08-12 18:15:35 +0800932 if (sirfport->uart_reg->uart_type == SIRF_REAL_UART) {
933 config_reg |= SIRFUART_RECV_TIMEOUT(port, rx_time_out);
934 wr_regl(port, ureg->sirfsoc_line_ctrl, config_reg);
Qipan Li2eb56182013-08-15 06:52:15 +0800935 } else {
Qipan Li5df83112013-08-12 18:15:35 +0800936 /*tx frame ctrl*/
Qipan Li459f15c2013-08-25 20:18:40 +0800937 len_val = (data_bit_len - 1) << SIRFSOC_USP_TX_DATA_LEN_OFFSET;
938 len_val |= (data_bit_len + 1 + stop_bit_len - 1) <<
939 SIRFSOC_USP_TX_FRAME_LEN_OFFSET;
940 len_val |= ((data_bit_len - 1) <<
941 SIRFSOC_USP_TX_SHIFTER_LEN_OFFSET);
942 len_val |= (((clk_div_reg & 0xc00) >> 10) <<
943 SIRFSOC_USP_TX_CLK_DIVISOR_OFFSET);
Qipan Li5df83112013-08-12 18:15:35 +0800944 wr_regl(port, ureg->sirfsoc_tx_frame_ctrl, len_val);
945 /*rx frame ctrl*/
Qipan Li459f15c2013-08-25 20:18:40 +0800946 len_val = (data_bit_len - 1) << SIRFSOC_USP_RX_DATA_LEN_OFFSET;
947 len_val |= (data_bit_len + 1 + stop_bit_len - 1) <<
948 SIRFSOC_USP_RX_FRAME_LEN_OFFSET;
949 len_val |= (data_bit_len - 1) <<
950 SIRFSOC_USP_RX_SHIFTER_LEN_OFFSET;
951 len_val |= (((clk_div_reg & 0xf000) >> 12) <<
952 SIRFSOC_USP_RX_CLK_DIVISOR_OFFSET);
Qipan Li5df83112013-08-12 18:15:35 +0800953 wr_regl(port, ureg->sirfsoc_rx_frame_ctrl, len_val);
954 /*async param*/
955 wr_regl(port, ureg->sirfsoc_async_param_reg,
956 (SIRFUART_RECV_TIMEOUT(port, rx_time_out)) |
Qipan Li459f15c2013-08-25 20:18:40 +0800957 (sample_div_reg & SIRFSOC_USP_ASYNC_DIV2_MASK) <<
958 SIRFSOC_USP_ASYNC_DIV2_OFFSET);
Qipan Li5df83112013-08-12 18:15:35 +0800959 }
Qipan Li9be16b32014-01-30 13:57:29 +0800960 if (sirfport->tx_dma_chan)
Qipan Li8316d042013-08-19 11:47:53 +0800961 wr_regl(port, ureg->sirfsoc_tx_dma_io_ctrl, SIRFUART_DMA_MODE);
962 else
963 wr_regl(port, ureg->sirfsoc_tx_dma_io_ctrl, SIRFUART_IO_MODE);
Qipan Li9be16b32014-01-30 13:57:29 +0800964 if (sirfport->rx_dma_chan)
Qipan Li8316d042013-08-19 11:47:53 +0800965 wr_regl(port, ureg->sirfsoc_rx_dma_io_ctrl, SIRFUART_DMA_MODE);
966 else
967 wr_regl(port, ureg->sirfsoc_rx_dma_io_ctrl, SIRFUART_IO_MODE);
Rong Wang161e7732011-11-17 23:17:04 +0800968 /* Reset Rx/Tx FIFO Threshold level for proper baudrate */
Qipan Li5df83112013-08-12 18:15:35 +0800969 if (set_baud < 1000000)
Rong Wang161e7732011-11-17 23:17:04 +0800970 threshold_div = 1;
971 else
972 threshold_div = 2;
Qipan Li8316d042013-08-19 11:47:53 +0800973 wr_regl(port, ureg->sirfsoc_tx_fifo_ctrl,
974 SIRFUART_FIFO_THD(port) / threshold_div);
975 wr_regl(port, ureg->sirfsoc_rx_fifo_ctrl,
976 SIRFUART_FIFO_THD(port) / threshold_div);
977 txfifo_op_reg |= SIRFUART_FIFO_START;
978 wr_regl(port, ureg->sirfsoc_tx_fifo_op, txfifo_op_reg);
Qipan Li5df83112013-08-12 18:15:35 +0800979 uart_update_timeout(port, termios->c_cflag, set_baud);
Rong Wang161e7732011-11-17 23:17:04 +0800980 sirfsoc_uart_start_rx(port);
Qipan Li5df83112013-08-12 18:15:35 +0800981 wr_regl(port, ureg->sirfsoc_tx_rx_en, SIRFUART_TX_EN | SIRFUART_RX_EN);
Rong Wang161e7732011-11-17 23:17:04 +0800982 spin_unlock_irqrestore(&port->lock, flags);
983}
984
Qipan Li388faf92014-01-03 15:44:07 +0800985static void sirfsoc_uart_pm(struct uart_port *port, unsigned int state,
986 unsigned int oldstate)
987{
988 struct sirfsoc_uart_port *sirfport = to_sirfport(port);
Qipan Li4b8038d2015-04-20 08:10:22 +0000989 if (!state)
Qipan Li388faf92014-01-03 15:44:07 +0800990 clk_prepare_enable(sirfport->clk);
Qipan Li4b8038d2015-04-20 08:10:22 +0000991 else
Qipan Li388faf92014-01-03 15:44:07 +0800992 clk_disable_unprepare(sirfport->clk);
993}
994
Rong Wang161e7732011-11-17 23:17:04 +0800995static int sirfsoc_uart_startup(struct uart_port *port)
996{
997 struct sirfsoc_uart_port *sirfport = to_sirfport(port);
Qipan Li15cdcb12013-08-19 11:47:52 +0800998 struct sirfsoc_register *ureg = &sirfport->uart_reg->uart_reg;
Rong Wang161e7732011-11-17 23:17:04 +0800999 unsigned int index = port->line;
1000 int ret;
1001 set_irq_flags(port->irq, IRQF_VALID | IRQF_NOAUTOEN);
1002 ret = request_irq(port->irq,
1003 sirfsoc_uart_isr,
1004 0,
1005 SIRFUART_PORT_NAME,
1006 sirfport);
1007 if (ret != 0) {
1008 dev_err(port->dev, "UART%d request IRQ line (%d) failed.\n",
1009 index, port->irq);
1010 goto irq_err;
1011 }
Qipan Li15cdcb12013-08-19 11:47:52 +08001012 /* initial hardware settings */
1013 wr_regl(port, ureg->sirfsoc_tx_dma_io_ctrl,
1014 rd_regl(port, ureg->sirfsoc_tx_dma_io_ctrl) |
1015 SIRFUART_IO_MODE);
1016 wr_regl(port, ureg->sirfsoc_rx_dma_io_ctrl,
1017 rd_regl(port, ureg->sirfsoc_rx_dma_io_ctrl) |
1018 SIRFUART_IO_MODE);
1019 wr_regl(port, ureg->sirfsoc_tx_dma_io_len, 0);
1020 wr_regl(port, ureg->sirfsoc_rx_dma_io_len, 0);
1021 wr_regl(port, ureg->sirfsoc_tx_rx_en, SIRFUART_RX_EN | SIRFUART_TX_EN);
1022 if (sirfport->uart_reg->uart_type == SIRF_USP_UART)
1023 wr_regl(port, ureg->sirfsoc_mode1,
1024 SIRFSOC_USP_ENDIAN_CTRL_LSBF |
1025 SIRFSOC_USP_EN);
1026 wr_regl(port, ureg->sirfsoc_tx_fifo_op, SIRFUART_FIFO_RESET);
1027 wr_regl(port, ureg->sirfsoc_tx_fifo_op, 0);
1028 wr_regl(port, ureg->sirfsoc_rx_fifo_op, SIRFUART_FIFO_RESET);
1029 wr_regl(port, ureg->sirfsoc_rx_fifo_op, 0);
1030 wr_regl(port, ureg->sirfsoc_tx_fifo_ctrl, SIRFUART_FIFO_THD(port));
1031 wr_regl(port, ureg->sirfsoc_rx_fifo_ctrl, SIRFUART_FIFO_THD(port));
Qipan Li9be16b32014-01-30 13:57:29 +08001032 if (sirfport->rx_dma_chan)
Qipan Li8316d042013-08-19 11:47:53 +08001033 wr_regl(port, ureg->sirfsoc_rx_fifo_level_chk,
Qipan Li9be16b32014-01-30 13:57:29 +08001034 SIRFUART_RX_FIFO_CHK_SC(port->line, 0x4) |
1035 SIRFUART_RX_FIFO_CHK_LC(port->line, 0xe) |
1036 SIRFUART_RX_FIFO_CHK_HC(port->line, 0x1b));
1037 if (sirfport->tx_dma_chan) {
Qipan Li8316d042013-08-19 11:47:53 +08001038 sirfport->tx_dma_state = TX_DMA_IDLE;
1039 wr_regl(port, ureg->sirfsoc_tx_fifo_level_chk,
1040 SIRFUART_TX_FIFO_CHK_SC(port->line, 0x1b) |
1041 SIRFUART_TX_FIFO_CHK_LC(port->line, 0xe) |
1042 SIRFUART_TX_FIFO_CHK_HC(port->line, 0x4));
1043 }
Qipan Li2eb56182013-08-15 06:52:15 +08001044 sirfport->ms_enabled = false;
1045 if (sirfport->uart_reg->uart_type == SIRF_USP_UART &&
1046 sirfport->hw_flow_ctrl) {
1047 set_irq_flags(gpio_to_irq(sirfport->cts_gpio),
1048 IRQF_VALID | IRQF_NOAUTOEN);
1049 ret = request_irq(gpio_to_irq(sirfport->cts_gpio),
1050 sirfsoc_uart_usp_cts_handler, IRQF_TRIGGER_FALLING |
1051 IRQF_TRIGGER_RISING, "usp_cts_irq", sirfport);
1052 if (ret != 0) {
1053 dev_err(port->dev, "UART-USP:request gpio irq fail\n");
1054 goto init_rx_err;
1055 }
1056 }
1057
Rong Wang161e7732011-11-17 23:17:04 +08001058 enable_irq(port->irq);
Qipan Li2eb56182013-08-15 06:52:15 +08001059
Qipan Li15cdcb12013-08-19 11:47:52 +08001060 return 0;
Qipan Li2eb56182013-08-15 06:52:15 +08001061init_rx_err:
1062 free_irq(port->irq, sirfport);
Rong Wang161e7732011-11-17 23:17:04 +08001063irq_err:
1064 return ret;
1065}
1066
1067static void sirfsoc_uart_shutdown(struct uart_port *port)
1068{
1069 struct sirfsoc_uart_port *sirfport = to_sirfport(port);
Qipan Li5df83112013-08-12 18:15:35 +08001070 struct sirfsoc_register *ureg = &sirfport->uart_reg->uart_reg;
Barry Song057badd2015-01-03 17:02:57 +08001071 if (!sirfport->is_atlas7)
Qipan Li5df83112013-08-12 18:15:35 +08001072 wr_regl(port, ureg->sirfsoc_int_en_reg, 0);
Barry Song909102d2013-08-07 13:35:38 +08001073 else
1074 wr_regl(port, SIRFUART_INT_EN_CLR, ~0UL);
1075
Rong Wang161e7732011-11-17 23:17:04 +08001076 free_irq(port->irq, sirfport);
Qipan Li2eb56182013-08-15 06:52:15 +08001077 if (sirfport->ms_enabled)
Rong Wang161e7732011-11-17 23:17:04 +08001078 sirfsoc_uart_disable_ms(port);
Qipan Li2eb56182013-08-15 06:52:15 +08001079 if (sirfport->uart_reg->uart_type == SIRF_USP_UART &&
1080 sirfport->hw_flow_ctrl) {
1081 gpio_set_value(sirfport->rts_gpio, 1);
1082 free_irq(gpio_to_irq(sirfport->cts_gpio), sirfport);
Rong Wang161e7732011-11-17 23:17:04 +08001083 }
Qipan Li9be16b32014-01-30 13:57:29 +08001084 if (sirfport->tx_dma_chan)
Qipan Li8316d042013-08-19 11:47:53 +08001085 sirfport->tx_dma_state = TX_DMA_IDLE;
Rong Wang161e7732011-11-17 23:17:04 +08001086}
1087
1088static const char *sirfsoc_uart_type(struct uart_port *port)
1089{
1090 return port->type == SIRFSOC_PORT_TYPE ? SIRFUART_PORT_NAME : NULL;
1091}
1092
1093static int sirfsoc_uart_request_port(struct uart_port *port)
1094{
Qipan Li5df83112013-08-12 18:15:35 +08001095 struct sirfsoc_uart_port *sirfport = to_sirfport(port);
1096 struct sirfsoc_uart_param *uart_param = &sirfport->uart_reg->uart_param;
Rong Wang161e7732011-11-17 23:17:04 +08001097 void *ret;
1098 ret = request_mem_region(port->mapbase,
Qipan Li5df83112013-08-12 18:15:35 +08001099 SIRFUART_MAP_SIZE, uart_param->port_name);
Rong Wang161e7732011-11-17 23:17:04 +08001100 return ret ? 0 : -EBUSY;
1101}
1102
1103static void sirfsoc_uart_release_port(struct uart_port *port)
1104{
1105 release_mem_region(port->mapbase, SIRFUART_MAP_SIZE);
1106}
1107
1108static void sirfsoc_uart_config_port(struct uart_port *port, int flags)
1109{
1110 if (flags & UART_CONFIG_TYPE) {
1111 port->type = SIRFSOC_PORT_TYPE;
1112 sirfsoc_uart_request_port(port);
1113 }
1114}
1115
1116static struct uart_ops sirfsoc_uart_ops = {
1117 .tx_empty = sirfsoc_uart_tx_empty,
1118 .get_mctrl = sirfsoc_uart_get_mctrl,
1119 .set_mctrl = sirfsoc_uart_set_mctrl,
1120 .stop_tx = sirfsoc_uart_stop_tx,
1121 .start_tx = sirfsoc_uart_start_tx,
1122 .stop_rx = sirfsoc_uart_stop_rx,
1123 .enable_ms = sirfsoc_uart_enable_ms,
1124 .break_ctl = sirfsoc_uart_break_ctl,
1125 .startup = sirfsoc_uart_startup,
1126 .shutdown = sirfsoc_uart_shutdown,
1127 .set_termios = sirfsoc_uart_set_termios,
Qipan Li388faf92014-01-03 15:44:07 +08001128 .pm = sirfsoc_uart_pm,
Rong Wang161e7732011-11-17 23:17:04 +08001129 .type = sirfsoc_uart_type,
1130 .release_port = sirfsoc_uart_release_port,
1131 .request_port = sirfsoc_uart_request_port,
1132 .config_port = sirfsoc_uart_config_port,
1133};
1134
1135#ifdef CONFIG_SERIAL_SIRFSOC_CONSOLE
Qipan Li5df83112013-08-12 18:15:35 +08001136static int __init
1137sirfsoc_uart_console_setup(struct console *co, char *options)
Rong Wang161e7732011-11-17 23:17:04 +08001138{
1139 unsigned int baud = 115200;
1140 unsigned int bits = 8;
1141 unsigned int parity = 'n';
1142 unsigned int flow = 'n';
Qipan Lia6ffe892015-04-29 06:45:08 +00001143 struct sirfsoc_uart_port *sirfport;
1144 struct sirfsoc_register *ureg;
Rong Wang161e7732011-11-17 23:17:04 +08001145 if (co->index < 0 || co->index >= SIRFSOC_UART_NR)
1146 return -EINVAL;
Qipan Lia6ffe892015-04-29 06:45:08 +00001147 sirfport = sirf_ports[co->index];
1148 if (!sirfport)
1149 return -ENODEV;
1150 ureg = &sirfport->uart_reg->uart_reg;
1151 if (!sirfport->port.mapbase)
Rong Wang161e7732011-11-17 23:17:04 +08001152 return -ENODEV;
1153
Qipan Li5df83112013-08-12 18:15:35 +08001154 /* enable usp in mode1 register */
1155 if (sirfport->uart_reg->uart_type == SIRF_USP_UART)
Qipan Lia6ffe892015-04-29 06:45:08 +00001156 wr_regl(&sirfport->port, ureg->sirfsoc_mode1, SIRFSOC_USP_EN |
Qipan Li5df83112013-08-12 18:15:35 +08001157 SIRFSOC_USP_ENDIAN_CTRL_LSBF);
Rong Wang161e7732011-11-17 23:17:04 +08001158 if (options)
1159 uart_parse_options(options, &baud, &parity, &bits, &flow);
Qipan Lia6ffe892015-04-29 06:45:08 +00001160 sirfport->port.cons = co;
Qipan Li5df83112013-08-12 18:15:35 +08001161
Qipan Li8316d042013-08-19 11:47:53 +08001162 /* default console tx/rx transfer using io mode */
Qipan Li9be16b32014-01-30 13:57:29 +08001163 sirfport->rx_dma_chan = NULL;
1164 sirfport->tx_dma_chan = NULL;
Qipan Lia6ffe892015-04-29 06:45:08 +00001165 return uart_set_options(&sirfport->port, co, baud, parity, bits, flow);
Rong Wang161e7732011-11-17 23:17:04 +08001166}
1167
1168static void sirfsoc_uart_console_putchar(struct uart_port *port, int ch)
1169{
Qipan Li5df83112013-08-12 18:15:35 +08001170 struct sirfsoc_uart_port *sirfport = to_sirfport(port);
1171 struct sirfsoc_register *ureg = &sirfport->uart_reg->uart_reg;
1172 struct sirfsoc_fifo_status *ufifo_st = &sirfport->uart_reg->fifo_status;
Qipan Licb4595a2015-04-29 06:45:09 +00001173 while (rd_regl(port, ureg->sirfsoc_tx_fifo_status) &
1174 ufifo_st->ff_full(port))
Rong Wang161e7732011-11-17 23:17:04 +08001175 cpu_relax();
Barry Song205c3842014-05-05 08:05:51 +08001176 wr_regl(port, ureg->sirfsoc_tx_fifo_data, ch);
Rong Wang161e7732011-11-17 23:17:04 +08001177}
1178
1179static void sirfsoc_uart_console_write(struct console *co, const char *s,
1180 unsigned int count)
1181{
Qipan Lia6ffe892015-04-29 06:45:08 +00001182 struct sirfsoc_uart_port *sirfport = sirf_ports[co->index];
1183
1184 uart_console_write(&sirfport->port, s, count,
1185 sirfsoc_uart_console_putchar);
Rong Wang161e7732011-11-17 23:17:04 +08001186}
1187
1188static struct console sirfsoc_uart_console = {
1189 .name = SIRFSOC_UART_NAME,
1190 .device = uart_console_device,
1191 .flags = CON_PRINTBUFFER,
1192 .index = -1,
1193 .write = sirfsoc_uart_console_write,
1194 .setup = sirfsoc_uart_console_setup,
1195 .data = &sirfsoc_uart_drv,
1196};
1197
1198static int __init sirfsoc_uart_console_init(void)
1199{
1200 register_console(&sirfsoc_uart_console);
1201 return 0;
1202}
1203console_initcall(sirfsoc_uart_console_init);
1204#endif
1205
1206static struct uart_driver sirfsoc_uart_drv = {
1207 .owner = THIS_MODULE,
1208 .driver_name = SIRFUART_PORT_NAME,
1209 .nr = SIRFSOC_UART_NR,
1210 .dev_name = SIRFSOC_UART_NAME,
1211 .major = SIRFSOC_UART_MAJOR,
1212 .minor = SIRFSOC_UART_MINOR,
1213#ifdef CONFIG_SERIAL_SIRFSOC_CONSOLE
1214 .cons = &sirfsoc_uart_console,
1215#else
1216 .cons = NULL,
1217#endif
1218};
1219
Fabian Fredericked0bb232015-03-16 20:17:11 +01001220static const struct of_device_id sirfsoc_uart_ids[] = {
Qipan Li5df83112013-08-12 18:15:35 +08001221 { .compatible = "sirf,prima2-uart", .data = &sirfsoc_uart,},
Barry Song057badd2015-01-03 17:02:57 +08001222 { .compatible = "sirf,atlas7-uart", .data = &sirfsoc_uart},
Qipan Li5df83112013-08-12 18:15:35 +08001223 { .compatible = "sirf,prima2-usp-uart", .data = &sirfsoc_usp},
1224 {}
1225};
1226MODULE_DEVICE_TABLE(of, sirfsoc_uart_ids);
1227
Jingoo Hanada1f443d2013-08-08 17:41:43 +09001228static int sirfsoc_uart_probe(struct platform_device *pdev)
Rong Wang161e7732011-11-17 23:17:04 +08001229{
1230 struct sirfsoc_uart_port *sirfport;
1231 struct uart_port *port;
1232 struct resource *res;
1233 int ret;
Qipan Li9be16b32014-01-30 13:57:29 +08001234 int i, j;
1235 struct dma_slave_config slv_cfg = {
1236 .src_maxburst = 2,
1237 };
1238 struct dma_slave_config tx_slv_cfg = {
1239 .dst_maxburst = 2,
1240 };
Qipan Li5df83112013-08-12 18:15:35 +08001241 const struct of_device_id *match;
Rong Wang161e7732011-11-17 23:17:04 +08001242
Qipan Li5df83112013-08-12 18:15:35 +08001243 match = of_match_node(sirfsoc_uart_ids, pdev->dev.of_node);
Qipan Lia6ffe892015-04-29 06:45:08 +00001244 sirfport = devm_kzalloc(&pdev->dev, sizeof(*sirfport), GFP_KERNEL);
1245 if (!sirfport) {
1246 ret = -ENOMEM;
Rong Wang161e7732011-11-17 23:17:04 +08001247 goto err;
1248 }
Qipan Lia6ffe892015-04-29 06:45:08 +00001249 sirfport->port.line = of_alias_get_id(pdev->dev.of_node, "serial");
1250 sirf_ports[sirfport->port.line] = sirfport;
1251 sirfport->port.iotype = UPIO_MEM;
1252 sirfport->port.flags = UPF_BOOT_AUTOCONF;
Rong Wang161e7732011-11-17 23:17:04 +08001253 port = &sirfport->port;
1254 port->dev = &pdev->dev;
1255 port->private_data = sirfport;
Qipan Li5df83112013-08-12 18:15:35 +08001256 sirfport->uart_reg = (struct sirfsoc_uart_register *)match->data;
Rong Wang161e7732011-11-17 23:17:04 +08001257
Qipan Li2eb56182013-08-15 06:52:15 +08001258 sirfport->hw_flow_ctrl = of_property_read_bool(pdev->dev.of_node,
1259 "sirf,uart-has-rtscts");
Qipan Li9be16b32014-01-30 13:57:29 +08001260 if (of_device_is_compatible(pdev->dev.of_node, "sirf,prima2-uart"))
Qipan Li5df83112013-08-12 18:15:35 +08001261 sirfport->uart_reg->uart_type = SIRF_REAL_UART;
Qipan Li2eb56182013-08-15 06:52:15 +08001262 if (of_device_is_compatible(pdev->dev.of_node, "sirf,prima2-usp-uart")) {
Qipan Li5df83112013-08-12 18:15:35 +08001263 sirfport->uart_reg->uart_type = SIRF_USP_UART;
Qipan Li2eb56182013-08-15 06:52:15 +08001264 if (!sirfport->hw_flow_ctrl)
1265 goto usp_no_flow_control;
1266 if (of_find_property(pdev->dev.of_node, "cts-gpios", NULL))
1267 sirfport->cts_gpio = of_get_named_gpio(
1268 pdev->dev.of_node, "cts-gpios", 0);
1269 else
1270 sirfport->cts_gpio = -1;
1271 if (of_find_property(pdev->dev.of_node, "rts-gpios", NULL))
1272 sirfport->rts_gpio = of_get_named_gpio(
1273 pdev->dev.of_node, "rts-gpios", 0);
1274 else
1275 sirfport->rts_gpio = -1;
1276
1277 if ((!gpio_is_valid(sirfport->cts_gpio) ||
1278 !gpio_is_valid(sirfport->rts_gpio))) {
1279 ret = -EINVAL;
1280 dev_err(&pdev->dev,
Qipan Li67bc3062013-08-19 11:47:51 +08001281 "Usp flow control must have cts and rts gpio");
Qipan Li2eb56182013-08-15 06:52:15 +08001282 goto err;
1283 }
1284 ret = devm_gpio_request(&pdev->dev, sirfport->cts_gpio,
Qipan Li67bc3062013-08-19 11:47:51 +08001285 "usp-cts-gpio");
Qipan Li2eb56182013-08-15 06:52:15 +08001286 if (ret) {
Qipan Li67bc3062013-08-19 11:47:51 +08001287 dev_err(&pdev->dev, "Unable request cts gpio");
Qipan Li2eb56182013-08-15 06:52:15 +08001288 goto err;
1289 }
1290 gpio_direction_input(sirfport->cts_gpio);
1291 ret = devm_gpio_request(&pdev->dev, sirfport->rts_gpio,
Qipan Li67bc3062013-08-19 11:47:51 +08001292 "usp-rts-gpio");
Qipan Li2eb56182013-08-15 06:52:15 +08001293 if (ret) {
Qipan Li67bc3062013-08-19 11:47:51 +08001294 dev_err(&pdev->dev, "Unable request rts gpio");
Qipan Li2eb56182013-08-15 06:52:15 +08001295 goto err;
1296 }
1297 gpio_direction_output(sirfport->rts_gpio, 1);
1298 }
1299usp_no_flow_control:
Barry Song057badd2015-01-03 17:02:57 +08001300 if (of_device_is_compatible(pdev->dev.of_node, "sirf,atlas7-uart"))
1301 sirfport->is_atlas7 = true;
Barry Song909102d2013-08-07 13:35:38 +08001302
Rong Wang161e7732011-11-17 23:17:04 +08001303 if (of_property_read_u32(pdev->dev.of_node,
1304 "fifosize",
1305 &port->fifosize)) {
1306 dev_err(&pdev->dev,
1307 "Unable to find fifosize in uart node.\n");
1308 ret = -EFAULT;
1309 goto err;
1310 }
1311
1312 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1313 if (res == NULL) {
1314 dev_err(&pdev->dev, "Insufficient resources.\n");
1315 ret = -EFAULT;
1316 goto err;
1317 }
Qipan Li8316d042013-08-19 11:47:53 +08001318 tasklet_init(&sirfport->rx_dma_complete_tasklet,
1319 sirfsoc_uart_rx_dma_complete_tl, (unsigned long)sirfport);
1320 tasklet_init(&sirfport->rx_tmo_process_tasklet,
1321 sirfsoc_rx_tmo_process_tl, (unsigned long)sirfport);
Rong Wang161e7732011-11-17 23:17:04 +08001322 port->mapbase = res->start;
1323 port->membase = devm_ioremap(&pdev->dev, res->start, resource_size(res));
1324 if (!port->membase) {
1325 dev_err(&pdev->dev, "Cannot remap resource.\n");
1326 ret = -ENOMEM;
1327 goto err;
1328 }
1329 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
1330 if (res == NULL) {
1331 dev_err(&pdev->dev, "Insufficient resources.\n");
1332 ret = -EFAULT;
Julia Lawall9250dd52012-09-01 18:33:09 +02001333 goto err;
Rong Wang161e7732011-11-17 23:17:04 +08001334 }
1335 port->irq = res->start;
1336
Qipan Liadeede72015-04-20 08:10:23 +00001337 sirfport->clk = devm_clk_get(&pdev->dev, NULL);
Barry Songac4ce712013-01-16 14:49:27 +08001338 if (IS_ERR(sirfport->clk)) {
1339 ret = PTR_ERR(sirfport->clk);
Barry Songa3437562013-08-15 06:52:14 +08001340 goto err;
Barry Songac4ce712013-01-16 14:49:27 +08001341 }
Barry Songac4ce712013-01-16 14:49:27 +08001342 port->uartclk = clk_get_rate(sirfport->clk);
1343
Rong Wang161e7732011-11-17 23:17:04 +08001344 port->ops = &sirfsoc_uart_ops;
1345 spin_lock_init(&port->lock);
1346
1347 platform_set_drvdata(pdev, sirfport);
1348 ret = uart_add_one_port(&sirfsoc_uart_drv, port);
1349 if (ret != 0) {
1350 dev_err(&pdev->dev, "Cannot add UART port(%d).\n", pdev->id);
Qipan Liadeede72015-04-20 08:10:23 +00001351 goto err;
Rong Wang161e7732011-11-17 23:17:04 +08001352 }
1353
Qipan Li9be16b32014-01-30 13:57:29 +08001354 sirfport->rx_dma_chan = dma_request_slave_channel(port->dev, "rx");
1355 for (i = 0; sirfport->rx_dma_chan && i < SIRFSOC_RX_LOOP_BUF_CNT; i++) {
1356 sirfport->rx_dma_items[i].xmit.buf =
1357 dma_alloc_coherent(port->dev, SIRFSOC_RX_DMA_BUF_SIZE,
1358 &sirfport->rx_dma_items[i].dma_addr, GFP_KERNEL);
1359 if (!sirfport->rx_dma_items[i].xmit.buf) {
1360 dev_err(port->dev, "Uart alloc bufa failed\n");
1361 ret = -ENOMEM;
1362 goto alloc_coherent_err;
1363 }
1364 sirfport->rx_dma_items[i].xmit.head =
1365 sirfport->rx_dma_items[i].xmit.tail = 0;
1366 }
1367 if (sirfport->rx_dma_chan)
1368 dmaengine_slave_config(sirfport->rx_dma_chan, &slv_cfg);
1369 sirfport->tx_dma_chan = dma_request_slave_channel(port->dev, "tx");
1370 if (sirfport->tx_dma_chan)
1371 dmaengine_slave_config(sirfport->tx_dma_chan, &tx_slv_cfg);
Rong Wang161e7732011-11-17 23:17:04 +08001372
Qipan Li9be16b32014-01-30 13:57:29 +08001373 return 0;
1374alloc_coherent_err:
1375 for (j = 0; j < i; j++)
1376 dma_free_coherent(port->dev, SIRFSOC_RX_DMA_BUF_SIZE,
1377 sirfport->rx_dma_items[j].xmit.buf,
1378 sirfport->rx_dma_items[j].dma_addr);
1379 dma_release_channel(sirfport->rx_dma_chan);
Rong Wang161e7732011-11-17 23:17:04 +08001380err:
1381 return ret;
1382}
1383
1384static int sirfsoc_uart_remove(struct platform_device *pdev)
1385{
1386 struct sirfsoc_uart_port *sirfport = platform_get_drvdata(pdev);
1387 struct uart_port *port = &sirfport->port;
Rong Wang161e7732011-11-17 23:17:04 +08001388 uart_remove_one_port(&sirfsoc_uart_drv, port);
Qipan Li9be16b32014-01-30 13:57:29 +08001389 if (sirfport->rx_dma_chan) {
1390 int i;
1391 dmaengine_terminate_all(sirfport->rx_dma_chan);
1392 dma_release_channel(sirfport->rx_dma_chan);
1393 for (i = 0; i < SIRFSOC_RX_LOOP_BUF_CNT; i++)
1394 dma_free_coherent(port->dev, SIRFSOC_RX_DMA_BUF_SIZE,
1395 sirfport->rx_dma_items[i].xmit.buf,
1396 sirfport->rx_dma_items[i].dma_addr);
1397 }
1398 if (sirfport->tx_dma_chan) {
1399 dmaengine_terminate_all(sirfport->tx_dma_chan);
1400 dma_release_channel(sirfport->tx_dma_chan);
1401 }
Rong Wang161e7732011-11-17 23:17:04 +08001402 return 0;
1403}
1404
Qipan Li99e626f2014-01-03 15:44:06 +08001405#ifdef CONFIG_PM_SLEEP
Rong Wang161e7732011-11-17 23:17:04 +08001406static int
Qipan Li99e626f2014-01-03 15:44:06 +08001407sirfsoc_uart_suspend(struct device *pdev)
Rong Wang161e7732011-11-17 23:17:04 +08001408{
Qipan Li99e626f2014-01-03 15:44:06 +08001409 struct sirfsoc_uart_port *sirfport = dev_get_drvdata(pdev);
Rong Wang161e7732011-11-17 23:17:04 +08001410 struct uart_port *port = &sirfport->port;
1411 uart_suspend_port(&sirfsoc_uart_drv, port);
1412 return 0;
1413}
1414
Qipan Li99e626f2014-01-03 15:44:06 +08001415static int sirfsoc_uart_resume(struct device *pdev)
Rong Wang161e7732011-11-17 23:17:04 +08001416{
Qipan Li99e626f2014-01-03 15:44:06 +08001417 struct sirfsoc_uart_port *sirfport = dev_get_drvdata(pdev);
Rong Wang161e7732011-11-17 23:17:04 +08001418 struct uart_port *port = &sirfport->port;
1419 uart_resume_port(&sirfsoc_uart_drv, port);
1420 return 0;
1421}
Qipan Li99e626f2014-01-03 15:44:06 +08001422#endif
1423
1424static const struct dev_pm_ops sirfsoc_uart_pm_ops = {
1425 SET_SYSTEM_SLEEP_PM_OPS(sirfsoc_uart_suspend, sirfsoc_uart_resume)
1426};
Rong Wang161e7732011-11-17 23:17:04 +08001427
Rong Wang161e7732011-11-17 23:17:04 +08001428static struct platform_driver sirfsoc_uart_driver = {
1429 .probe = sirfsoc_uart_probe,
Bill Pemberton2d47b712012-11-19 13:21:34 -05001430 .remove = sirfsoc_uart_remove,
Rong Wang161e7732011-11-17 23:17:04 +08001431 .driver = {
1432 .name = SIRFUART_PORT_NAME,
Rong Wang161e7732011-11-17 23:17:04 +08001433 .of_match_table = sirfsoc_uart_ids,
Qipan Li99e626f2014-01-03 15:44:06 +08001434 .pm = &sirfsoc_uart_pm_ops,
Rong Wang161e7732011-11-17 23:17:04 +08001435 },
1436};
1437
1438static int __init sirfsoc_uart_init(void)
1439{
1440 int ret = 0;
1441
1442 ret = uart_register_driver(&sirfsoc_uart_drv);
1443 if (ret)
1444 goto out;
1445
1446 ret = platform_driver_register(&sirfsoc_uart_driver);
1447 if (ret)
1448 uart_unregister_driver(&sirfsoc_uart_drv);
1449out:
1450 return ret;
1451}
1452module_init(sirfsoc_uart_init);
1453
1454static void __exit sirfsoc_uart_exit(void)
1455{
1456 platform_driver_unregister(&sirfsoc_uart_driver);
1457 uart_unregister_driver(&sirfsoc_uart_drv);
1458}
1459module_exit(sirfsoc_uart_exit);
1460
1461MODULE_LICENSE("GPL v2");
1462MODULE_AUTHOR("Bin Shi <Bin.Shi@csr.com>, Rong Wang<Rong.Wang@csr.com>");
1463MODULE_DESCRIPTION("CSR SiRFprimaII Uart Driver");