blob: b6116413ca0dafcf5d5205087d7dfe13600859e6 [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);
Rong Wang161e7732011-11-17 23:17:04 +080039static const struct sirfsoc_baudrate_to_regv baudrate_to_regv[] = {
40 {4000000, 2359296},
41 {3500000, 1310721},
42 {3000000, 1572865},
43 {2500000, 1245186},
44 {2000000, 1572866},
45 {1500000, 1245188},
46 {1152000, 1638404},
47 {1000000, 1572869},
48 {921600, 1114120},
49 {576000, 1245196},
50 {500000, 1245198},
51 {460800, 1572876},
52 {230400, 1310750},
53 {115200, 1310781},
54 {57600, 1310843},
55 {38400, 1114328},
56 {19200, 1114545},
57 {9600, 1114979},
58};
59
Qipan Lia6ffe892015-04-29 06:45:08 +000060static struct sirfsoc_uart_port *sirf_ports[SIRFSOC_UART_NR];
Rong Wang161e7732011-11-17 23:17:04 +080061
62static inline struct sirfsoc_uart_port *to_sirfport(struct uart_port *port)
63{
64 return container_of(port, struct sirfsoc_uart_port, port);
65}
66
67static inline unsigned int sirfsoc_uart_tx_empty(struct uart_port *port)
68{
69 unsigned long reg;
Qipan Li5df83112013-08-12 18:15:35 +080070 struct sirfsoc_uart_port *sirfport = to_sirfport(port);
71 struct sirfsoc_register *ureg = &sirfport->uart_reg->uart_reg;
72 struct sirfsoc_fifo_status *ufifo_st = &sirfport->uart_reg->fifo_status;
73 reg = rd_regl(port, ureg->sirfsoc_tx_fifo_status);
Qipan Licb4595a2015-04-29 06:45:09 +000074 return (reg & ufifo_st->ff_empty(port)) ? TIOCSER_TEMT : 0;
Rong Wang161e7732011-11-17 23:17:04 +080075}
76
77static unsigned int sirfsoc_uart_get_mctrl(struct uart_port *port)
78{
79 struct sirfsoc_uart_port *sirfport = to_sirfport(port);
Qipan Li5df83112013-08-12 18:15:35 +080080 struct sirfsoc_register *ureg = &sirfport->uart_reg->uart_reg;
Qipan Li2eb56182013-08-15 06:52:15 +080081 if (!sirfport->hw_flow_ctrl || !sirfport->ms_enabled)
Rong Wang161e7732011-11-17 23:17:04 +080082 goto cts_asserted;
Qipan Li2eb56182013-08-15 06:52:15 +080083 if (sirfport->uart_reg->uart_type == SIRF_REAL_UART) {
Qipan Li5df83112013-08-12 18:15:35 +080084 if (!(rd_regl(port, ureg->sirfsoc_afc_ctrl) &
85 SIRFUART_AFC_CTS_STATUS))
Rong Wang161e7732011-11-17 23:17:04 +080086 goto cts_asserted;
87 else
88 goto cts_deasserted;
Qipan Li2eb56182013-08-15 06:52:15 +080089 } else {
90 if (!gpio_get_value(sirfport->cts_gpio))
91 goto cts_asserted;
92 else
93 goto cts_deasserted;
Rong Wang161e7732011-11-17 23:17:04 +080094 }
95cts_deasserted:
96 return TIOCM_CAR | TIOCM_DSR;
97cts_asserted:
98 return TIOCM_CAR | TIOCM_DSR | TIOCM_CTS;
99}
100
101static void sirfsoc_uart_set_mctrl(struct uart_port *port, unsigned int mctrl)
102{
103 struct sirfsoc_uart_port *sirfport = to_sirfport(port);
Qipan Li5df83112013-08-12 18:15:35 +0800104 struct sirfsoc_register *ureg = &sirfport->uart_reg->uart_reg;
Rong Wang161e7732011-11-17 23:17:04 +0800105 unsigned int assert = mctrl & TIOCM_RTS;
106 unsigned int val = assert ? SIRFUART_AFC_CTRL_RX_THD : 0x0;
107 unsigned int current_val;
Qipan Li2eb56182013-08-15 06:52:15 +0800108
Qipan Li7f60f2f2015-05-14 06:45:25 +0000109 if (mctrl & TIOCM_LOOP) {
110 if (sirfport->uart_reg->uart_type == SIRF_REAL_UART)
111 wr_regl(port, ureg->sirfsoc_line_ctrl,
112 rd_regl(port, ureg->sirfsoc_line_ctrl) |
113 SIRFUART_LOOP_BACK);
114 else
115 wr_regl(port, ureg->sirfsoc_mode1,
116 rd_regl(port, ureg->sirfsoc_mode1) |
117 SIRFSOC_USP_LOOP_BACK_CTRL);
118 } else {
119 if (sirfport->uart_reg->uart_type == SIRF_REAL_UART)
120 wr_regl(port, ureg->sirfsoc_line_ctrl,
121 rd_regl(port, ureg->sirfsoc_line_ctrl) &
122 ~SIRFUART_LOOP_BACK);
123 else
124 wr_regl(port, ureg->sirfsoc_mode1,
125 rd_regl(port, ureg->sirfsoc_mode1) &
126 ~SIRFSOC_USP_LOOP_BACK_CTRL);
127 }
128
Qipan Li2eb56182013-08-15 06:52:15 +0800129 if (!sirfport->hw_flow_ctrl || !sirfport->ms_enabled)
130 return;
131 if (sirfport->uart_reg->uart_type == SIRF_REAL_UART) {
Qipan Li5df83112013-08-12 18:15:35 +0800132 current_val = rd_regl(port, ureg->sirfsoc_afc_ctrl) & ~0xFF;
Rong Wang161e7732011-11-17 23:17:04 +0800133 val |= current_val;
Qipan Li5df83112013-08-12 18:15:35 +0800134 wr_regl(port, ureg->sirfsoc_afc_ctrl, val);
Qipan Li2eb56182013-08-15 06:52:15 +0800135 } else {
136 if (!val)
137 gpio_set_value(sirfport->rts_gpio, 1);
138 else
139 gpio_set_value(sirfport->rts_gpio, 0);
Rong Wang161e7732011-11-17 23:17:04 +0800140 }
141}
142
143static void sirfsoc_uart_stop_tx(struct uart_port *port)
144{
Barry Song909102d2013-08-07 13:35:38 +0800145 struct sirfsoc_uart_port *sirfport = to_sirfport(port);
Qipan Li5df83112013-08-12 18:15:35 +0800146 struct sirfsoc_register *ureg = &sirfport->uart_reg->uart_reg;
147 struct sirfsoc_int_en *uint_en = &sirfport->uart_reg->uart_int_en;
Barry Song909102d2013-08-07 13:35:38 +0800148
Qipan Li9be16b32014-01-30 13:57:29 +0800149 if (sirfport->tx_dma_chan) {
Qipan Li8316d042013-08-19 11:47:53 +0800150 if (sirfport->tx_dma_state == TX_DMA_RUNNING) {
151 dmaengine_pause(sirfport->tx_dma_chan);
152 sirfport->tx_dma_state = TX_DMA_PAUSE;
153 } else {
Barry Song057badd2015-01-03 17:02:57 +0800154 if (!sirfport->is_atlas7)
Qipan Li8316d042013-08-19 11:47:53 +0800155 wr_regl(port, ureg->sirfsoc_int_en_reg,
156 rd_regl(port, ureg->sirfsoc_int_en_reg) &
157 ~uint_en->sirfsoc_txfifo_empty_en);
158 else
Qipan Lic1b7ac62015-05-14 06:45:21 +0000159 wr_regl(port, ureg->sirfsoc_int_en_clr_reg,
Qipan Li8316d042013-08-19 11:47:53 +0800160 uint_en->sirfsoc_txfifo_empty_en);
161 }
162 } else {
Qipan Lic1b7ac62015-05-14 06:45:21 +0000163 if (sirfport->uart_reg->uart_type == SIRF_USP_UART)
164 wr_regl(port, ureg->sirfsoc_tx_rx_en, rd_regl(port,
165 ureg->sirfsoc_tx_rx_en) & ~SIRFUART_TX_EN);
Barry Song057badd2015-01-03 17:02:57 +0800166 if (!sirfport->is_atlas7)
Qipan Li8316d042013-08-19 11:47:53 +0800167 wr_regl(port, ureg->sirfsoc_int_en_reg,
168 rd_regl(port, ureg->sirfsoc_int_en_reg) &
169 ~uint_en->sirfsoc_txfifo_empty_en);
170 else
Qipan Lic1b7ac62015-05-14 06:45:21 +0000171 wr_regl(port, ureg->sirfsoc_int_en_clr_reg,
Qipan Li8316d042013-08-19 11:47:53 +0800172 uint_en->sirfsoc_txfifo_empty_en);
173 }
174}
175
176static void sirfsoc_uart_tx_with_dma(struct sirfsoc_uart_port *sirfport)
177{
178 struct uart_port *port = &sirfport->port;
179 struct sirfsoc_register *ureg = &sirfport->uart_reg->uart_reg;
180 struct sirfsoc_int_en *uint_en = &sirfport->uart_reg->uart_int_en;
181 struct circ_buf *xmit = &port->state->xmit;
182 unsigned long tran_size;
183 unsigned long tran_start;
184 unsigned long pio_tx_size;
185
186 tran_size = CIRC_CNT_TO_END(xmit->head, xmit->tail, UART_XMIT_SIZE);
187 tran_start = (unsigned long)(xmit->buf + xmit->tail);
188 if (uart_circ_empty(xmit) || uart_tx_stopped(port) ||
189 !tran_size)
190 return;
191 if (sirfport->tx_dma_state == TX_DMA_PAUSE) {
192 dmaengine_resume(sirfport->tx_dma_chan);
193 return;
194 }
195 if (sirfport->tx_dma_state == TX_DMA_RUNNING)
196 return;
Barry Song057badd2015-01-03 17:02:57 +0800197 if (!sirfport->is_atlas7)
Qipan Li5df83112013-08-12 18:15:35 +0800198 wr_regl(port, ureg->sirfsoc_int_en_reg,
Qipan Li8316d042013-08-19 11:47:53 +0800199 rd_regl(port, ureg->sirfsoc_int_en_reg)&
200 ~(uint_en->sirfsoc_txfifo_empty_en));
201 else
Qipan Lic1b7ac62015-05-14 06:45:21 +0000202 wr_regl(port, ureg->sirfsoc_int_en_clr_reg,
Qipan Li5df83112013-08-12 18:15:35 +0800203 uint_en->sirfsoc_txfifo_empty_en);
Qipan Li8316d042013-08-19 11:47:53 +0800204 /*
205 * DMA requires buffer address and buffer length are both aligned with
206 * 4 bytes, so we use PIO for
207 * 1. if address is not aligned with 4bytes, use PIO for the first 1~3
208 * bytes, and move to DMA for the left part aligned with 4bytes
209 * 2. if buffer length is not aligned with 4bytes, use DMA for aligned
210 * part first, move to PIO for the left 1~3 bytes
211 */
212 if (tran_size < 4 || BYTES_TO_ALIGN(tran_start)) {
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 if (BYTES_TO_ALIGN(tran_start)) {
218 pio_tx_size = sirfsoc_uart_pio_tx_chars(sirfport,
219 BYTES_TO_ALIGN(tran_start));
220 tran_size -= pio_tx_size;
221 }
222 if (tran_size < 4)
223 sirfsoc_uart_pio_tx_chars(sirfport, tran_size);
Barry Song057badd2015-01-03 17:02:57 +0800224 if (!sirfport->is_atlas7)
Qipan Li8316d042013-08-19 11:47:53 +0800225 wr_regl(port, ureg->sirfsoc_int_en_reg,
226 rd_regl(port, ureg->sirfsoc_int_en_reg)|
227 uint_en->sirfsoc_txfifo_empty_en);
228 else
229 wr_regl(port, ureg->sirfsoc_int_en_reg,
230 uint_en->sirfsoc_txfifo_empty_en);
231 wr_regl(port, ureg->sirfsoc_tx_fifo_op, SIRFUART_FIFO_START);
232 } else {
233 /* tx transfer mode switch into dma mode */
234 wr_regl(port, ureg->sirfsoc_tx_fifo_op, SIRFUART_FIFO_STOP);
235 wr_regl(port, ureg->sirfsoc_tx_dma_io_ctrl,
236 rd_regl(port, ureg->sirfsoc_tx_dma_io_ctrl)&
237 ~SIRFUART_IO_MODE);
238 wr_regl(port, ureg->sirfsoc_tx_fifo_op, SIRFUART_FIFO_START);
239 tran_size &= ~(0x3);
Qipan Li5df83112013-08-12 18:15:35 +0800240
Qipan Li8316d042013-08-19 11:47:53 +0800241 sirfport->tx_dma_addr = dma_map_single(port->dev,
242 xmit->buf + xmit->tail,
243 tran_size, DMA_TO_DEVICE);
244 sirfport->tx_dma_desc = dmaengine_prep_slave_single(
245 sirfport->tx_dma_chan, sirfport->tx_dma_addr,
246 tran_size, DMA_MEM_TO_DEV, DMA_PREP_INTERRUPT);
247 if (!sirfport->tx_dma_desc) {
248 dev_err(port->dev, "DMA prep slave single fail\n");
249 return;
250 }
251 sirfport->tx_dma_desc->callback =
252 sirfsoc_uart_tx_dma_complete_callback;
253 sirfport->tx_dma_desc->callback_param = (void *)sirfport;
254 sirfport->transfer_size = tran_size;
255
256 dmaengine_submit(sirfport->tx_dma_desc);
257 dma_async_issue_pending(sirfport->tx_dma_chan);
258 sirfport->tx_dma_state = TX_DMA_RUNNING;
259 }
Rong Wang161e7732011-11-17 23:17:04 +0800260}
261
Jingoo Hanada1f442013-08-08 17:41:43 +0900262static void sirfsoc_uart_start_tx(struct uart_port *port)
Rong Wang161e7732011-11-17 23:17:04 +0800263{
264 struct sirfsoc_uart_port *sirfport = to_sirfport(port);
Qipan Li5df83112013-08-12 18:15:35 +0800265 struct sirfsoc_register *ureg = &sirfport->uart_reg->uart_reg;
266 struct sirfsoc_int_en *uint_en = &sirfport->uart_reg->uart_int_en;
Qipan Li9be16b32014-01-30 13:57:29 +0800267 if (sirfport->tx_dma_chan)
Qipan Li8316d042013-08-19 11:47:53 +0800268 sirfsoc_uart_tx_with_dma(sirfport);
269 else {
Qipan Lic1b7ac62015-05-14 06:45:21 +0000270 if (sirfport->uart_reg->uart_type == SIRF_USP_UART)
271 wr_regl(port, ureg->sirfsoc_tx_rx_en, rd_regl(port,
272 ureg->sirfsoc_tx_rx_en) | SIRFUART_TX_EN);
Qipan Li326707e2015-05-26 09:35:58 +0000273 wr_regl(port, ureg->sirfsoc_tx_fifo_op, SIRFUART_FIFO_STOP);
Qipan Licb4595a2015-04-29 06:45:09 +0000274 sirfsoc_uart_pio_tx_chars(sirfport, port->fifosize);
Qipan Li8316d042013-08-19 11:47:53 +0800275 wr_regl(port, ureg->sirfsoc_tx_fifo_op, SIRFUART_FIFO_START);
Barry Song057badd2015-01-03 17:02:57 +0800276 if (!sirfport->is_atlas7)
Qipan Li8316d042013-08-19 11:47:53 +0800277 wr_regl(port, ureg->sirfsoc_int_en_reg,
278 rd_regl(port, ureg->sirfsoc_int_en_reg)|
279 uint_en->sirfsoc_txfifo_empty_en);
280 else
281 wr_regl(port, ureg->sirfsoc_int_en_reg,
282 uint_en->sirfsoc_txfifo_empty_en);
283 }
Rong Wang161e7732011-11-17 23:17:04 +0800284}
285
286static void sirfsoc_uart_stop_rx(struct uart_port *port)
287{
Barry Song909102d2013-08-07 13:35:38 +0800288 struct sirfsoc_uart_port *sirfport = to_sirfport(port);
Qipan Li5df83112013-08-12 18:15:35 +0800289 struct sirfsoc_register *ureg = &sirfport->uart_reg->uart_reg;
290 struct sirfsoc_int_en *uint_en = &sirfport->uart_reg->uart_int_en;
Qipan Li8316d042013-08-19 11:47:53 +0800291
Qipan Li5df83112013-08-12 18:15:35 +0800292 wr_regl(port, ureg->sirfsoc_rx_fifo_op, 0);
Qipan Li9be16b32014-01-30 13:57:29 +0800293 if (sirfport->rx_dma_chan) {
Barry Song057badd2015-01-03 17:02:57 +0800294 if (!sirfport->is_atlas7)
Qipan Li8316d042013-08-19 11:47:53 +0800295 wr_regl(port, ureg->sirfsoc_int_en_reg,
296 rd_regl(port, ureg->sirfsoc_int_en_reg) &
Qipan Lic1b7ac62015-05-14 06:45:21 +0000297 ~(SIRFUART_RX_DMA_INT_EN(uint_en,
298 sirfport->uart_reg->uart_type) |
Qipan Li8316d042013-08-19 11:47:53 +0800299 uint_en->sirfsoc_rx_done_en));
300 else
Qipan Lic1b7ac62015-05-14 06:45:21 +0000301 wr_regl(port, ureg->sirfsoc_int_en_clr_reg,
302 SIRFUART_RX_DMA_INT_EN(uint_en,
303 sirfport->uart_reg->uart_type)|
304 uint_en->sirfsoc_rx_done_en);
Qipan Li8316d042013-08-19 11:47:53 +0800305 dmaengine_terminate_all(sirfport->rx_dma_chan);
306 } else {
Barry Song057badd2015-01-03 17:02:57 +0800307 if (!sirfport->is_atlas7)
Qipan Li8316d042013-08-19 11:47:53 +0800308 wr_regl(port, ureg->sirfsoc_int_en_reg,
309 rd_regl(port, ureg->sirfsoc_int_en_reg)&
Qipan Lic1b7ac62015-05-14 06:45:21 +0000310 ~(SIRFUART_RX_IO_INT_EN(uint_en,
311 sirfport->uart_reg->uart_type)));
Qipan Li8316d042013-08-19 11:47:53 +0800312 else
Qipan Lic1b7ac62015-05-14 06:45:21 +0000313 wr_regl(port, ureg->sirfsoc_int_en_clr_reg,
314 SIRFUART_RX_IO_INT_EN(uint_en,
315 sirfport->uart_reg->uart_type));
Qipan Li8316d042013-08-19 11:47:53 +0800316 }
Rong Wang161e7732011-11-17 23:17:04 +0800317}
318
319static void sirfsoc_uart_disable_ms(struct uart_port *port)
320{
321 struct sirfsoc_uart_port *sirfport = to_sirfport(port);
Qipan Li5df83112013-08-12 18:15:35 +0800322 struct sirfsoc_register *ureg = &sirfport->uart_reg->uart_reg;
323 struct sirfsoc_int_en *uint_en = &sirfport->uart_reg->uart_int_en;
Barry Song909102d2013-08-07 13:35:38 +0800324
Rong Wang161e7732011-11-17 23:17:04 +0800325 if (!sirfport->hw_flow_ctrl)
326 return;
Qipan Li2eb56182013-08-15 06:52:15 +0800327 sirfport->ms_enabled = false;
328 if (sirfport->uart_reg->uart_type == SIRF_REAL_UART) {
329 wr_regl(port, ureg->sirfsoc_afc_ctrl,
330 rd_regl(port, ureg->sirfsoc_afc_ctrl) & ~0x3FF);
Barry Song057badd2015-01-03 17:02:57 +0800331 if (!sirfport->is_atlas7)
Qipan Li2eb56182013-08-15 06:52:15 +0800332 wr_regl(port, ureg->sirfsoc_int_en_reg,
333 rd_regl(port, ureg->sirfsoc_int_en_reg)&
334 ~uint_en->sirfsoc_cts_en);
335 else
Qipan Lic1b7ac62015-05-14 06:45:21 +0000336 wr_regl(port, ureg->sirfsoc_int_en_clr_reg,
Qipan Li2eb56182013-08-15 06:52:15 +0800337 uint_en->sirfsoc_cts_en);
Qipan Li5df83112013-08-12 18:15:35 +0800338 } else
Qipan Li2eb56182013-08-15 06:52:15 +0800339 disable_irq(gpio_to_irq(sirfport->cts_gpio));
340}
341
342static irqreturn_t sirfsoc_uart_usp_cts_handler(int irq, void *dev_id)
343{
344 struct sirfsoc_uart_port *sirfport = (struct sirfsoc_uart_port *)dev_id;
345 struct uart_port *port = &sirfport->port;
Qipan Li07d410e2014-05-26 19:02:07 +0800346 spin_lock(&port->lock);
Qipan Li2eb56182013-08-15 06:52:15 +0800347 if (gpio_is_valid(sirfport->cts_gpio) && sirfport->ms_enabled)
348 uart_handle_cts_change(port,
349 !gpio_get_value(sirfport->cts_gpio));
Qipan Li07d410e2014-05-26 19:02:07 +0800350 spin_unlock(&port->lock);
Qipan Li2eb56182013-08-15 06:52:15 +0800351 return IRQ_HANDLED;
Rong Wang161e7732011-11-17 23:17:04 +0800352}
353
354static void sirfsoc_uart_enable_ms(struct uart_port *port)
355{
356 struct sirfsoc_uart_port *sirfport = to_sirfport(port);
Qipan Li5df83112013-08-12 18:15:35 +0800357 struct sirfsoc_register *ureg = &sirfport->uart_reg->uart_reg;
358 struct sirfsoc_int_en *uint_en = &sirfport->uart_reg->uart_int_en;
Barry Song909102d2013-08-07 13:35:38 +0800359
Rong Wang161e7732011-11-17 23:17:04 +0800360 if (!sirfport->hw_flow_ctrl)
361 return;
Qipan Li2eb56182013-08-15 06:52:15 +0800362 sirfport->ms_enabled = true;
363 if (sirfport->uart_reg->uart_type == SIRF_REAL_UART) {
364 wr_regl(port, ureg->sirfsoc_afc_ctrl,
365 rd_regl(port, ureg->sirfsoc_afc_ctrl) |
Qipan Lieab192a2015-05-14 06:45:22 +0000366 SIRFUART_AFC_TX_EN | SIRFUART_AFC_RX_EN |
367 SIRFUART_AFC_CTRL_RX_THD);
Barry Song057badd2015-01-03 17:02:57 +0800368 if (!sirfport->is_atlas7)
Qipan Li2eb56182013-08-15 06:52:15 +0800369 wr_regl(port, ureg->sirfsoc_int_en_reg,
370 rd_regl(port, ureg->sirfsoc_int_en_reg)
371 | uint_en->sirfsoc_cts_en);
372 else
373 wr_regl(port, ureg->sirfsoc_int_en_reg,
374 uint_en->sirfsoc_cts_en);
Qipan Li5df83112013-08-12 18:15:35 +0800375 } else
Qipan Li2eb56182013-08-15 06:52:15 +0800376 enable_irq(gpio_to_irq(sirfport->cts_gpio));
Rong Wang161e7732011-11-17 23:17:04 +0800377}
378
379static void sirfsoc_uart_break_ctl(struct uart_port *port, int break_state)
380{
Qipan Li5df83112013-08-12 18:15:35 +0800381 struct sirfsoc_uart_port *sirfport = to_sirfport(port);
382 struct sirfsoc_register *ureg = &sirfport->uart_reg->uart_reg;
383 if (sirfport->uart_reg->uart_type == SIRF_REAL_UART) {
384 unsigned long ulcon = rd_regl(port, ureg->sirfsoc_line_ctrl);
385 if (break_state)
386 ulcon |= SIRFUART_SET_BREAK;
387 else
388 ulcon &= ~SIRFUART_SET_BREAK;
389 wr_regl(port, ureg->sirfsoc_line_ctrl, ulcon);
390 }
Rong Wang161e7732011-11-17 23:17:04 +0800391}
392
393static unsigned int
394sirfsoc_uart_pio_rx_chars(struct uart_port *port, unsigned int max_rx_count)
395{
Qipan Li5df83112013-08-12 18:15:35 +0800396 struct sirfsoc_uart_port *sirfport = to_sirfport(port);
397 struct sirfsoc_register *ureg = &sirfport->uart_reg->uart_reg;
398 struct sirfsoc_fifo_status *ufifo_st = &sirfport->uart_reg->fifo_status;
Rong Wang161e7732011-11-17 23:17:04 +0800399 unsigned int ch, rx_count = 0;
Qipan Li5df83112013-08-12 18:15:35 +0800400 struct tty_struct *tty;
401 tty = tty_port_tty_get(&port->state->port);
402 if (!tty)
403 return -ENODEV;
404 while (!(rd_regl(port, ureg->sirfsoc_rx_fifo_status) &
Qipan Licb4595a2015-04-29 06:45:09 +0000405 ufifo_st->ff_empty(port))) {
Qipan Li5df83112013-08-12 18:15:35 +0800406 ch = rd_regl(port, ureg->sirfsoc_rx_fifo_data) |
407 SIRFUART_DUMMY_READ;
Rong Wang161e7732011-11-17 23:17:04 +0800408 if (unlikely(uart_handle_sysrq_char(port, ch)))
409 continue;
410 uart_insert_char(port, 0, 0, ch, TTY_NORMAL);
411 rx_count++;
412 if (rx_count >= max_rx_count)
413 break;
414 }
415
Qipan Li8316d042013-08-19 11:47:53 +0800416 sirfport->rx_io_count += rx_count;
Rong Wang161e7732011-11-17 23:17:04 +0800417 port->icount.rx += rx_count;
Viresh Kumar8b9ade92013-08-19 20:14:28 +0530418
Rong Wang161e7732011-11-17 23:17:04 +0800419 return rx_count;
420}
421
422static unsigned int
423sirfsoc_uart_pio_tx_chars(struct sirfsoc_uart_port *sirfport, int count)
424{
425 struct uart_port *port = &sirfport->port;
Qipan Li5df83112013-08-12 18:15:35 +0800426 struct sirfsoc_register *ureg = &sirfport->uart_reg->uart_reg;
427 struct sirfsoc_fifo_status *ufifo_st = &sirfport->uart_reg->fifo_status;
Rong Wang161e7732011-11-17 23:17:04 +0800428 struct circ_buf *xmit = &port->state->xmit;
429 unsigned int num_tx = 0;
430 while (!uart_circ_empty(xmit) &&
Qipan Li5df83112013-08-12 18:15:35 +0800431 !(rd_regl(port, ureg->sirfsoc_tx_fifo_status) &
Qipan Licb4595a2015-04-29 06:45:09 +0000432 ufifo_st->ff_full(port)) &&
Rong Wang161e7732011-11-17 23:17:04 +0800433 count--) {
Qipan Li5df83112013-08-12 18:15:35 +0800434 wr_regl(port, ureg->sirfsoc_tx_fifo_data,
435 xmit->buf[xmit->tail]);
Rong Wang161e7732011-11-17 23:17:04 +0800436 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
437 port->icount.tx++;
438 num_tx++;
439 }
440 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
441 uart_write_wakeup(port);
442 return num_tx;
443}
444
Qipan Li8316d042013-08-19 11:47:53 +0800445static void sirfsoc_uart_tx_dma_complete_callback(void *param)
446{
447 struct sirfsoc_uart_port *sirfport = (struct sirfsoc_uart_port *)param;
448 struct uart_port *port = &sirfport->port;
449 struct circ_buf *xmit = &port->state->xmit;
450 unsigned long flags;
451
Qipan Li07d410e2014-05-26 19:02:07 +0800452 spin_lock_irqsave(&port->lock, flags);
Qipan Li8316d042013-08-19 11:47:53 +0800453 xmit->tail = (xmit->tail + sirfport->transfer_size) &
454 (UART_XMIT_SIZE - 1);
455 port->icount.tx += sirfport->transfer_size;
456 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
457 uart_write_wakeup(port);
458 if (sirfport->tx_dma_addr)
459 dma_unmap_single(port->dev, sirfport->tx_dma_addr,
460 sirfport->transfer_size, DMA_TO_DEVICE);
Qipan Li8316d042013-08-19 11:47:53 +0800461 sirfport->tx_dma_state = TX_DMA_IDLE;
462 sirfsoc_uart_tx_with_dma(sirfport);
Qipan Li07d410e2014-05-26 19:02:07 +0800463 spin_unlock_irqrestore(&port->lock, flags);
Qipan Li8316d042013-08-19 11:47:53 +0800464}
465
Rong Wang161e7732011-11-17 23:17:04 +0800466static irqreturn_t sirfsoc_uart_isr(int irq, void *dev_id)
467{
468 unsigned long intr_status;
469 unsigned long cts_status;
470 unsigned long flag = TTY_NORMAL;
471 struct sirfsoc_uart_port *sirfport = (struct sirfsoc_uart_port *)dev_id;
472 struct uart_port *port = &sirfport->port;
Qipan Li5df83112013-08-12 18:15:35 +0800473 struct sirfsoc_register *ureg = &sirfport->uart_reg->uart_reg;
474 struct sirfsoc_fifo_status *ufifo_st = &sirfport->uart_reg->fifo_status;
475 struct sirfsoc_int_status *uint_st = &sirfport->uart_reg->uart_int_st;
476 struct sirfsoc_int_en *uint_en = &sirfport->uart_reg->uart_int_en;
Rong Wang161e7732011-11-17 23:17:04 +0800477 struct uart_state *state = port->state;
478 struct circ_buf *xmit = &port->state->xmit;
Barry Song5425e032012-12-25 17:32:04 +0800479 spin_lock(&port->lock);
Qipan Li5df83112013-08-12 18:15:35 +0800480 intr_status = rd_regl(port, ureg->sirfsoc_int_st_reg);
481 wr_regl(port, ureg->sirfsoc_int_st_reg, intr_status);
Qipan Li8316d042013-08-19 11:47:53 +0800482 intr_status &= rd_regl(port, ureg->sirfsoc_int_en_reg);
Qipan Lic1b7ac62015-05-14 06:45:21 +0000483 if (unlikely(intr_status & (SIRFUART_ERR_INT_STAT(uint_st,
484 sirfport->uart_reg->uart_type)))) {
Qipan Li5df83112013-08-12 18:15:35 +0800485 if (intr_status & uint_st->sirfsoc_rxd_brk) {
486 port->icount.brk++;
Rong Wang161e7732011-11-17 23:17:04 +0800487 if (uart_handle_break(port))
488 goto recv_char;
Rong Wang161e7732011-11-17 23:17:04 +0800489 }
Qipan Lid9e8e972015-05-14 06:45:24 +0000490 if (intr_status & uint_st->sirfsoc_rx_oflow) {
Rong Wang161e7732011-11-17 23:17:04 +0800491 port->icount.overrun++;
Qipan Lid9e8e972015-05-14 06:45:24 +0000492 flag = TTY_OVERRUN;
493 }
Qipan Li5df83112013-08-12 18:15:35 +0800494 if (intr_status & uint_st->sirfsoc_frm_err) {
Rong Wang161e7732011-11-17 23:17:04 +0800495 port->icount.frame++;
496 flag = TTY_FRAME;
497 }
Qipan Lid9e8e972015-05-14 06:45:24 +0000498 if (intr_status & uint_st->sirfsoc_parity_err) {
499 port->icount.parity++;
Rong Wang161e7732011-11-17 23:17:04 +0800500 flag = TTY_PARITY;
Qipan Lid9e8e972015-05-14 06:45:24 +0000501 }
Qipan Li5df83112013-08-12 18:15:35 +0800502 wr_regl(port, ureg->sirfsoc_rx_fifo_op, SIRFUART_FIFO_RESET);
503 wr_regl(port, ureg->sirfsoc_rx_fifo_op, 0);
504 wr_regl(port, ureg->sirfsoc_rx_fifo_op, SIRFUART_FIFO_START);
Rong Wang161e7732011-11-17 23:17:04 +0800505 intr_status &= port->read_status_mask;
506 uart_insert_char(port, intr_status,
Qipan Li5df83112013-08-12 18:15:35 +0800507 uint_en->sirfsoc_rx_oflow_en, 0, flag);
Rong Wang161e7732011-11-17 23:17:04 +0800508 }
509recv_char:
Qipan Li5df83112013-08-12 18:15:35 +0800510 if ((sirfport->uart_reg->uart_type == SIRF_REAL_UART) &&
Qipan Li8316d042013-08-19 11:47:53 +0800511 (intr_status & SIRFUART_CTS_INT_ST(uint_st)) &&
512 !sirfport->tx_dma_state) {
Qipan Li5df83112013-08-12 18:15:35 +0800513 cts_status = rd_regl(port, ureg->sirfsoc_afc_ctrl) &
514 SIRFUART_AFC_CTS_STATUS;
515 if (cts_status != 0)
516 cts_status = 0;
517 else
518 cts_status = 1;
519 uart_handle_cts_change(port, cts_status);
520 wake_up_interruptible(&state->port.delta_msr_wait);
Rong Wang161e7732011-11-17 23:17:04 +0800521 }
Qipan Li0f17e3b2015-05-26 09:36:00 +0000522 if (!sirfport->rx_dma_chan &&
523 (intr_status & SIRFUART_RX_IO_INT_ST(uint_st))) {
Qipan Lic1b7ac62015-05-14 06:45:21 +0000524 /*
525 * chip will trigger continuous RX_TIMEOUT interrupt
526 * in RXFIFO empty and not trigger if RXFIFO recevice
527 * data in limit time, original method use RX_TIMEOUT
528 * will trigger lots of useless interrupt in RXFIFO
529 * empty.RXFIFO received one byte will trigger RX_DONE
530 * interrupt.use RX_DONE to wait for data received
531 * into RXFIFO, use RX_THD/RX_FULL for lots data receive
532 * and use RX_TIMEOUT for the last left data.
533 */
534 if (intr_status & uint_st->sirfsoc_rx_done) {
535 if (!sirfport->is_atlas7) {
536 wr_regl(port, ureg->sirfsoc_int_en_reg,
537 rd_regl(port, ureg->sirfsoc_int_en_reg)
538 & ~(uint_en->sirfsoc_rx_done_en));
539 wr_regl(port, ureg->sirfsoc_int_en_reg,
540 rd_regl(port, ureg->sirfsoc_int_en_reg)
541 | (uint_en->sirfsoc_rx_timeout_en));
542 } else {
543 wr_regl(port, ureg->sirfsoc_int_en_clr_reg,
544 uint_en->sirfsoc_rx_done_en);
545 wr_regl(port, ureg->sirfsoc_int_en_reg,
546 uint_en->sirfsoc_rx_timeout_en);
547 }
548 } else {
549 if (intr_status & uint_st->sirfsoc_rx_timeout) {
550 if (!sirfport->is_atlas7) {
551 wr_regl(port, ureg->sirfsoc_int_en_reg,
552 rd_regl(port, ureg->sirfsoc_int_en_reg)
553 & ~(uint_en->sirfsoc_rx_timeout_en));
554 wr_regl(port, ureg->sirfsoc_int_en_reg,
555 rd_regl(port, ureg->sirfsoc_int_en_reg)
556 | (uint_en->sirfsoc_rx_done_en));
557 } else {
558 wr_regl(port,
559 ureg->sirfsoc_int_en_clr_reg,
560 uint_en->sirfsoc_rx_timeout_en);
561 wr_regl(port, ureg->sirfsoc_int_en_reg,
562 uint_en->sirfsoc_rx_done_en);
563 }
564 }
Qipan Licb4595a2015-04-29 06:45:09 +0000565 sirfsoc_uart_pio_rx_chars(port, port->fifosize);
Qipan Lic1b7ac62015-05-14 06:45:21 +0000566 }
Qipan Li8316d042013-08-19 11:47:53 +0800567 }
Qipan Li07d410e2014-05-26 19:02:07 +0800568 spin_unlock(&port->lock);
569 tty_flip_buffer_push(&state->port);
570 spin_lock(&port->lock);
Qipan Li5df83112013-08-12 18:15:35 +0800571 if (intr_status & uint_st->sirfsoc_txfifo_empty) {
Qipan Li9be16b32014-01-30 13:57:29 +0800572 if (sirfport->tx_dma_chan)
Qipan Li8316d042013-08-19 11:47:53 +0800573 sirfsoc_uart_tx_with_dma(sirfport);
574 else {
575 if (uart_circ_empty(xmit) || uart_tx_stopped(port)) {
576 spin_unlock(&port->lock);
577 return IRQ_HANDLED;
578 } else {
579 sirfsoc_uart_pio_tx_chars(sirfport,
Qipan Licb4595a2015-04-29 06:45:09 +0000580 port->fifosize);
Qipan Li8316d042013-08-19 11:47:53 +0800581 if ((uart_circ_empty(xmit)) &&
Qipan Li5df83112013-08-12 18:15:35 +0800582 (rd_regl(port, ureg->sirfsoc_tx_fifo_status) &
Qipan Licb4595a2015-04-29 06:45:09 +0000583 ufifo_st->ff_empty(port)))
Qipan Li8316d042013-08-19 11:47:53 +0800584 sirfsoc_uart_stop_tx(port);
585 }
Rong Wang161e7732011-11-17 23:17:04 +0800586 }
587 }
Barry Song5425e032012-12-25 17:32:04 +0800588 spin_unlock(&port->lock);
Qipan Li07d410e2014-05-26 19:02:07 +0800589
Rong Wang161e7732011-11-17 23:17:04 +0800590 return IRQ_HANDLED;
591}
592
Qipan Li8316d042013-08-19 11:47:53 +0800593static void sirfsoc_uart_rx_dma_complete_callback(void *param)
594{
Qipan Li8316d042013-08-19 11:47:53 +0800595}
596
597/* submit rx dma task into dmaengine */
598static void sirfsoc_uart_start_next_rx_dma(struct uart_port *port)
599{
600 struct sirfsoc_uart_port *sirfport = to_sirfport(port);
601 struct sirfsoc_register *ureg = &sirfport->uart_reg->uart_reg;
602 struct sirfsoc_int_en *uint_en = &sirfport->uart_reg->uart_int_en;
Qipan Li8316d042013-08-19 11:47:53 +0800603 sirfport->rx_io_count = 0;
604 wr_regl(port, ureg->sirfsoc_rx_dma_io_ctrl,
605 rd_regl(port, ureg->sirfsoc_rx_dma_io_ctrl) &
606 ~SIRFUART_IO_MODE);
Qipan Li0f17e3b2015-05-26 09:36:00 +0000607 sirfport->rx_dma_items.xmit.tail =
608 sirfport->rx_dma_items.xmit.head = 0;
609 sirfport->rx_dma_items.desc =
610 dmaengine_prep_dma_cyclic(sirfport->rx_dma_chan,
611 sirfport->rx_dma_items.dma_addr, SIRFSOC_RX_DMA_BUF_SIZE,
612 SIRFSOC_RX_DMA_BUF_SIZE / 2,
613 DMA_DEV_TO_MEM, DMA_PREP_INTERRUPT);
614 if (IS_ERR_OR_NULL(sirfport->rx_dma_items.desc)) {
615 dev_err(port->dev, "DMA slave single fail\n");
616 return;
617 }
618 sirfport->rx_dma_items.desc->callback =
619 sirfsoc_uart_rx_dma_complete_callback;
620 sirfport->rx_dma_items.desc->callback_param = sirfport;
621 sirfport->rx_dma_items.cookie =
622 dmaengine_submit(sirfport->rx_dma_items.desc);
623 dma_async_issue_pending(sirfport->rx_dma_chan);
Barry Song057badd2015-01-03 17:02:57 +0800624 if (!sirfport->is_atlas7)
Qipan Li8316d042013-08-19 11:47:53 +0800625 wr_regl(port, ureg->sirfsoc_int_en_reg,
626 rd_regl(port, ureg->sirfsoc_int_en_reg) |
Qipan Lic1b7ac62015-05-14 06:45:21 +0000627 SIRFUART_RX_DMA_INT_EN(uint_en,
628 sirfport->uart_reg->uart_type));
Qipan Li8316d042013-08-19 11:47:53 +0800629 else
630 wr_regl(port, ureg->sirfsoc_int_en_reg,
Qipan Lic1b7ac62015-05-14 06:45:21 +0000631 SIRFUART_RX_DMA_INT_EN(uint_en,
632 sirfport->uart_reg->uart_type));
Qipan Li8316d042013-08-19 11:47:53 +0800633}
634
Rong Wang161e7732011-11-17 23:17:04 +0800635static void sirfsoc_uart_start_rx(struct uart_port *port)
636{
Barry Song909102d2013-08-07 13:35:38 +0800637 struct sirfsoc_uart_port *sirfport = to_sirfport(port);
Qipan Li5df83112013-08-12 18:15:35 +0800638 struct sirfsoc_register *ureg = &sirfport->uart_reg->uart_reg;
639 struct sirfsoc_int_en *uint_en = &sirfport->uart_reg->uart_int_en;
Qipan Li8316d042013-08-19 11:47:53 +0800640
641 sirfport->rx_io_count = 0;
Qipan Li5df83112013-08-12 18:15:35 +0800642 wr_regl(port, ureg->sirfsoc_rx_fifo_op, SIRFUART_FIFO_RESET);
643 wr_regl(port, ureg->sirfsoc_rx_fifo_op, 0);
644 wr_regl(port, ureg->sirfsoc_rx_fifo_op, SIRFUART_FIFO_START);
Qipan Li9be16b32014-01-30 13:57:29 +0800645 if (sirfport->rx_dma_chan)
Qipan Li8316d042013-08-19 11:47:53 +0800646 sirfsoc_uart_start_next_rx_dma(port);
647 else {
Barry Song057badd2015-01-03 17:02:57 +0800648 if (!sirfport->is_atlas7)
Qipan Li8316d042013-08-19 11:47:53 +0800649 wr_regl(port, ureg->sirfsoc_int_en_reg,
650 rd_regl(port, ureg->sirfsoc_int_en_reg) |
Qipan Lic1b7ac62015-05-14 06:45:21 +0000651 SIRFUART_RX_IO_INT_EN(uint_en,
652 sirfport->uart_reg->uart_type));
Qipan Li8316d042013-08-19 11:47:53 +0800653 else
654 wr_regl(port, ureg->sirfsoc_int_en_reg,
Qipan Lic1b7ac62015-05-14 06:45:21 +0000655 SIRFUART_RX_IO_INT_EN(uint_en,
656 sirfport->uart_reg->uart_type));
Qipan Li8316d042013-08-19 11:47:53 +0800657 }
Rong Wang161e7732011-11-17 23:17:04 +0800658}
659
660static unsigned int
Qipan Li5df83112013-08-12 18:15:35 +0800661sirfsoc_usp_calc_sample_div(unsigned long set_rate,
662 unsigned long ioclk_rate, unsigned long *sample_reg)
663{
664 unsigned long min_delta = ~0UL;
665 unsigned short sample_div;
666 unsigned long ioclk_div = 0;
667 unsigned long temp_delta;
668
Qipan Licb4595a2015-04-29 06:45:09 +0000669 for (sample_div = SIRF_USP_MIN_SAMPLE_DIV;
Qipan Li5df83112013-08-12 18:15:35 +0800670 sample_div <= SIRF_MAX_SAMPLE_DIV; sample_div++) {
671 temp_delta = ioclk_rate -
672 (ioclk_rate + (set_rate * sample_div) / 2)
673 / (set_rate * sample_div) * set_rate * sample_div;
674
675 temp_delta = (temp_delta > 0) ? temp_delta : -temp_delta;
676 if (temp_delta < min_delta) {
677 ioclk_div = (2 * ioclk_rate /
678 (set_rate * sample_div) + 1) / 2 - 1;
679 if (ioclk_div > SIRF_IOCLK_DIV_MAX)
680 continue;
681 min_delta = temp_delta;
682 *sample_reg = sample_div;
683 if (!temp_delta)
684 break;
685 }
686 }
687 return ioclk_div;
688}
689
690static unsigned int
691sirfsoc_uart_calc_sample_div(unsigned long baud_rate,
692 unsigned long ioclk_rate, unsigned long *set_baud)
Rong Wang161e7732011-11-17 23:17:04 +0800693{
694 unsigned long min_delta = ~0UL;
695 unsigned short sample_div;
696 unsigned int regv = 0;
697 unsigned long ioclk_div;
698 unsigned long baud_tmp;
699 int temp_delta;
700
701 for (sample_div = SIRF_MIN_SAMPLE_DIV;
702 sample_div <= SIRF_MAX_SAMPLE_DIV; sample_div++) {
703 ioclk_div = (ioclk_rate / (baud_rate * (sample_div + 1))) - 1;
704 if (ioclk_div > SIRF_IOCLK_DIV_MAX)
705 continue;
706 baud_tmp = ioclk_rate / ((ioclk_div + 1) * (sample_div + 1));
707 temp_delta = baud_tmp - baud_rate;
708 temp_delta = (temp_delta > 0) ? temp_delta : -temp_delta;
709 if (temp_delta < min_delta) {
710 regv = regv & (~SIRF_IOCLK_DIV_MASK);
711 regv = regv | ioclk_div;
712 regv = regv & (~SIRF_SAMPLE_DIV_MASK);
713 regv = regv | (sample_div << SIRF_SAMPLE_DIV_SHIFT);
714 min_delta = temp_delta;
Qipan Li5df83112013-08-12 18:15:35 +0800715 *set_baud = baud_tmp;
Rong Wang161e7732011-11-17 23:17:04 +0800716 }
717 }
718 return regv;
719}
720
721static void sirfsoc_uart_set_termios(struct uart_port *port,
722 struct ktermios *termios,
723 struct ktermios *old)
724{
725 struct sirfsoc_uart_port *sirfport = to_sirfport(port);
Qipan Li5df83112013-08-12 18:15:35 +0800726 struct sirfsoc_register *ureg = &sirfport->uart_reg->uart_reg;
727 struct sirfsoc_int_en *uint_en = &sirfport->uart_reg->uart_int_en;
Rong Wang161e7732011-11-17 23:17:04 +0800728 unsigned long config_reg = 0;
729 unsigned long baud_rate;
Qipan Li5df83112013-08-12 18:15:35 +0800730 unsigned long set_baud;
Rong Wang161e7732011-11-17 23:17:04 +0800731 unsigned long flags;
732 unsigned long ic;
733 unsigned int clk_div_reg = 0;
Qipan Li8316d042013-08-19 11:47:53 +0800734 unsigned long txfifo_op_reg, ioclk_rate;
Rong Wang161e7732011-11-17 23:17:04 +0800735 unsigned long rx_time_out;
736 int threshold_div;
Qipan Li5df83112013-08-12 18:15:35 +0800737 u32 data_bit_len, stop_bit_len, len_val;
738 unsigned long sample_div_reg = 0xf;
739 ioclk_rate = port->uartclk;
Rong Wang161e7732011-11-17 23:17:04 +0800740
Rong Wang161e7732011-11-17 23:17:04 +0800741 switch (termios->c_cflag & CSIZE) {
742 default:
743 case CS8:
Qipan Li5df83112013-08-12 18:15:35 +0800744 data_bit_len = 8;
Rong Wang161e7732011-11-17 23:17:04 +0800745 config_reg |= SIRFUART_DATA_BIT_LEN_8;
746 break;
747 case CS7:
Qipan Li5df83112013-08-12 18:15:35 +0800748 data_bit_len = 7;
Rong Wang161e7732011-11-17 23:17:04 +0800749 config_reg |= SIRFUART_DATA_BIT_LEN_7;
750 break;
751 case CS6:
Qipan Li5df83112013-08-12 18:15:35 +0800752 data_bit_len = 6;
Rong Wang161e7732011-11-17 23:17:04 +0800753 config_reg |= SIRFUART_DATA_BIT_LEN_6;
754 break;
755 case CS5:
Qipan Li5df83112013-08-12 18:15:35 +0800756 data_bit_len = 5;
Rong Wang161e7732011-11-17 23:17:04 +0800757 config_reg |= SIRFUART_DATA_BIT_LEN_5;
758 break;
759 }
Qipan Li5df83112013-08-12 18:15:35 +0800760 if (termios->c_cflag & CSTOPB) {
Rong Wang161e7732011-11-17 23:17:04 +0800761 config_reg |= SIRFUART_STOP_BIT_LEN_2;
Qipan Li5df83112013-08-12 18:15:35 +0800762 stop_bit_len = 2;
763 } else
764 stop_bit_len = 1;
765
Rong Wang161e7732011-11-17 23:17:04 +0800766 spin_lock_irqsave(&port->lock, flags);
Qipan Li5df83112013-08-12 18:15:35 +0800767 port->read_status_mask = uint_en->sirfsoc_rx_oflow_en;
Rong Wang161e7732011-11-17 23:17:04 +0800768 port->ignore_status_mask = 0;
Qipan Li5df83112013-08-12 18:15:35 +0800769 if (sirfport->uart_reg->uart_type == SIRF_REAL_UART) {
770 if (termios->c_iflag & INPCK)
771 port->read_status_mask |= uint_en->sirfsoc_frm_err_en |
772 uint_en->sirfsoc_parity_err_en;
Qipan Li2eb56182013-08-15 06:52:15 +0800773 } else {
Qipan Li5df83112013-08-12 18:15:35 +0800774 if (termios->c_iflag & INPCK)
775 port->read_status_mask |= uint_en->sirfsoc_frm_err_en;
776 }
Peter Hurleyef8b9dd2014-06-16 08:10:41 -0400777 if (termios->c_iflag & (IGNBRK | BRKINT | PARMRK))
Qipan Li5df83112013-08-12 18:15:35 +0800778 port->read_status_mask |= uint_en->sirfsoc_rxd_brk_en;
779 if (sirfport->uart_reg->uart_type == SIRF_REAL_UART) {
780 if (termios->c_iflag & IGNPAR)
781 port->ignore_status_mask |=
782 uint_en->sirfsoc_frm_err_en |
783 uint_en->sirfsoc_parity_err_en;
784 if (termios->c_cflag & PARENB) {
785 if (termios->c_cflag & CMSPAR) {
786 if (termios->c_cflag & PARODD)
787 config_reg |= SIRFUART_STICK_BIT_MARK;
788 else
789 config_reg |= SIRFUART_STICK_BIT_SPACE;
Qipan Li5df83112013-08-12 18:15:35 +0800790 } else {
Qipan Lid9e8e972015-05-14 06:45:24 +0000791 if (termios->c_cflag & PARODD)
792 config_reg |= SIRFUART_STICK_BIT_ODD;
793 else
794 config_reg |= SIRFUART_STICK_BIT_EVEN;
Qipan Li5df83112013-08-12 18:15:35 +0800795 }
Rong Wang161e7732011-11-17 23:17:04 +0800796 }
Qipan Li2eb56182013-08-15 06:52:15 +0800797 } else {
Qipan Li5df83112013-08-12 18:15:35 +0800798 if (termios->c_iflag & IGNPAR)
799 port->ignore_status_mask |=
800 uint_en->sirfsoc_frm_err_en;
801 if (termios->c_cflag & PARENB)
802 dev_warn(port->dev,
803 "USP-UART not support parity err\n");
804 }
805 if (termios->c_iflag & IGNBRK) {
806 port->ignore_status_mask |=
807 uint_en->sirfsoc_rxd_brk_en;
808 if (termios->c_iflag & IGNPAR)
809 port->ignore_status_mask |=
810 uint_en->sirfsoc_rx_oflow_en;
811 }
812 if ((termios->c_cflag & CREAD) == 0)
813 port->ignore_status_mask |= SIRFUART_DUMMY_READ;
Rong Wang161e7732011-11-17 23:17:04 +0800814 /* Hardware Flow Control Settings */
815 if (UART_ENABLE_MS(port, termios->c_cflag)) {
816 if (!sirfport->ms_enabled)
817 sirfsoc_uart_enable_ms(port);
818 } else {
819 if (sirfport->ms_enabled)
820 sirfsoc_uart_disable_ms(port);
821 }
Qipan Li5df83112013-08-12 18:15:35 +0800822 baud_rate = uart_get_baud_rate(port, termios, old, 0, 4000000);
823 if (ioclk_rate == 150000000) {
Barry Songac4ce712013-01-16 14:49:27 +0800824 for (ic = 0; ic < SIRF_BAUD_RATE_SUPPORT_NR; ic++)
825 if (baud_rate == baudrate_to_regv[ic].baud_rate)
826 clk_div_reg = baudrate_to_regv[ic].reg_val;
827 }
Qipan Li5df83112013-08-12 18:15:35 +0800828 set_baud = baud_rate;
829 if (sirfport->uart_reg->uart_type == SIRF_REAL_UART) {
830 if (unlikely(clk_div_reg == 0))
831 clk_div_reg = sirfsoc_uart_calc_sample_div(baud_rate,
832 ioclk_rate, &set_baud);
833 wr_regl(port, ureg->sirfsoc_divisor, clk_div_reg);
Qipan Li2eb56182013-08-15 06:52:15 +0800834 } else {
Qipan Li5df83112013-08-12 18:15:35 +0800835 clk_div_reg = sirfsoc_usp_calc_sample_div(baud_rate,
836 ioclk_rate, &sample_div_reg);
837 sample_div_reg--;
838 set_baud = ((ioclk_rate / (clk_div_reg+1) - 1) /
839 (sample_div_reg + 1));
840 /* setting usp mode 2 */
Qipan Li459f15c2013-08-25 20:18:40 +0800841 len_val = ((1 << SIRFSOC_USP_MODE2_RXD_DELAY_OFFSET) |
842 (1 << SIRFSOC_USP_MODE2_TXD_DELAY_OFFSET));
843 len_val |= ((clk_div_reg & SIRFSOC_USP_MODE2_CLK_DIVISOR_MASK)
844 << SIRFSOC_USP_MODE2_CLK_DIVISOR_OFFSET);
845 wr_regl(port, ureg->sirfsoc_mode2, len_val);
Qipan Li5df83112013-08-12 18:15:35 +0800846 }
Rong Wang161e7732011-11-17 23:17:04 +0800847 if (tty_termios_baud_rate(termios))
Qipan Li5df83112013-08-12 18:15:35 +0800848 tty_termios_encode_baud_rate(termios, set_baud, set_baud);
849 /* set receive timeout && data bits len */
850 rx_time_out = SIRFSOC_UART_RX_TIMEOUT(set_baud, 20000);
851 rx_time_out = SIRFUART_RECV_TIMEOUT_VALUE(rx_time_out);
Qipan Li8316d042013-08-19 11:47:53 +0800852 txfifo_op_reg = rd_regl(port, ureg->sirfsoc_tx_fifo_op);
Qipan Li459f15c2013-08-25 20:18:40 +0800853 wr_regl(port, ureg->sirfsoc_rx_fifo_op, SIRFUART_FIFO_STOP);
Qipan Li5df83112013-08-12 18:15:35 +0800854 wr_regl(port, ureg->sirfsoc_tx_fifo_op,
Qipan Li8316d042013-08-19 11:47:53 +0800855 (txfifo_op_reg & ~SIRFUART_FIFO_START));
Qipan Li5df83112013-08-12 18:15:35 +0800856 if (sirfport->uart_reg->uart_type == SIRF_REAL_UART) {
Qipan Lic1b7ac62015-05-14 06:45:21 +0000857 config_reg |= SIRFUART_UART_RECV_TIMEOUT(rx_time_out);
Qipan Li5df83112013-08-12 18:15:35 +0800858 wr_regl(port, ureg->sirfsoc_line_ctrl, config_reg);
Qipan Li2eb56182013-08-15 06:52:15 +0800859 } else {
Qipan Li5df83112013-08-12 18:15:35 +0800860 /*tx frame ctrl*/
Qipan Li459f15c2013-08-25 20:18:40 +0800861 len_val = (data_bit_len - 1) << SIRFSOC_USP_TX_DATA_LEN_OFFSET;
862 len_val |= (data_bit_len + 1 + stop_bit_len - 1) <<
863 SIRFSOC_USP_TX_FRAME_LEN_OFFSET;
864 len_val |= ((data_bit_len - 1) <<
865 SIRFSOC_USP_TX_SHIFTER_LEN_OFFSET);
866 len_val |= (((clk_div_reg & 0xc00) >> 10) <<
867 SIRFSOC_USP_TX_CLK_DIVISOR_OFFSET);
Qipan Li5df83112013-08-12 18:15:35 +0800868 wr_regl(port, ureg->sirfsoc_tx_frame_ctrl, len_val);
869 /*rx frame ctrl*/
Qipan Li459f15c2013-08-25 20:18:40 +0800870 len_val = (data_bit_len - 1) << SIRFSOC_USP_RX_DATA_LEN_OFFSET;
871 len_val |= (data_bit_len + 1 + stop_bit_len - 1) <<
872 SIRFSOC_USP_RX_FRAME_LEN_OFFSET;
873 len_val |= (data_bit_len - 1) <<
874 SIRFSOC_USP_RX_SHIFTER_LEN_OFFSET;
875 len_val |= (((clk_div_reg & 0xf000) >> 12) <<
876 SIRFSOC_USP_RX_CLK_DIVISOR_OFFSET);
Qipan Li5df83112013-08-12 18:15:35 +0800877 wr_regl(port, ureg->sirfsoc_rx_frame_ctrl, len_val);
878 /*async param*/
879 wr_regl(port, ureg->sirfsoc_async_param_reg,
Qipan Lic1b7ac62015-05-14 06:45:21 +0000880 (SIRFUART_USP_RECV_TIMEOUT(rx_time_out)) |
Qipan Li459f15c2013-08-25 20:18:40 +0800881 (sample_div_reg & SIRFSOC_USP_ASYNC_DIV2_MASK) <<
882 SIRFSOC_USP_ASYNC_DIV2_OFFSET);
Qipan Li5df83112013-08-12 18:15:35 +0800883 }
Qipan Li9be16b32014-01-30 13:57:29 +0800884 if (sirfport->tx_dma_chan)
Qipan Li8316d042013-08-19 11:47:53 +0800885 wr_regl(port, ureg->sirfsoc_tx_dma_io_ctrl, SIRFUART_DMA_MODE);
886 else
887 wr_regl(port, ureg->sirfsoc_tx_dma_io_ctrl, SIRFUART_IO_MODE);
Qipan Li9be16b32014-01-30 13:57:29 +0800888 if (sirfport->rx_dma_chan)
Qipan Li8316d042013-08-19 11:47:53 +0800889 wr_regl(port, ureg->sirfsoc_rx_dma_io_ctrl, SIRFUART_DMA_MODE);
890 else
891 wr_regl(port, ureg->sirfsoc_rx_dma_io_ctrl, SIRFUART_IO_MODE);
Qipan Li0f17e3b2015-05-26 09:36:00 +0000892 sirfport->rx_period_time = 20000000;
Rong Wang161e7732011-11-17 23:17:04 +0800893 /* Reset Rx/Tx FIFO Threshold level for proper baudrate */
Qipan Li5df83112013-08-12 18:15:35 +0800894 if (set_baud < 1000000)
Rong Wang161e7732011-11-17 23:17:04 +0800895 threshold_div = 1;
896 else
897 threshold_div = 2;
Qipan Li8316d042013-08-19 11:47:53 +0800898 wr_regl(port, ureg->sirfsoc_tx_fifo_ctrl,
899 SIRFUART_FIFO_THD(port) / threshold_div);
900 wr_regl(port, ureg->sirfsoc_rx_fifo_ctrl,
901 SIRFUART_FIFO_THD(port) / threshold_div);
902 txfifo_op_reg |= SIRFUART_FIFO_START;
903 wr_regl(port, ureg->sirfsoc_tx_fifo_op, txfifo_op_reg);
Qipan Li5df83112013-08-12 18:15:35 +0800904 uart_update_timeout(port, termios->c_cflag, set_baud);
Rong Wang161e7732011-11-17 23:17:04 +0800905 sirfsoc_uart_start_rx(port);
Qipan Li5df83112013-08-12 18:15:35 +0800906 wr_regl(port, ureg->sirfsoc_tx_rx_en, SIRFUART_TX_EN | SIRFUART_RX_EN);
Rong Wang161e7732011-11-17 23:17:04 +0800907 spin_unlock_irqrestore(&port->lock, flags);
908}
909
Qipan Li388faf92014-01-03 15:44:07 +0800910static void sirfsoc_uart_pm(struct uart_port *port, unsigned int state,
911 unsigned int oldstate)
912{
913 struct sirfsoc_uart_port *sirfport = to_sirfport(port);
Qipan Li4b8038d2015-04-20 08:10:22 +0000914 if (!state)
Qipan Li388faf92014-01-03 15:44:07 +0800915 clk_prepare_enable(sirfport->clk);
Qipan Li4b8038d2015-04-20 08:10:22 +0000916 else
Qipan Li388faf92014-01-03 15:44:07 +0800917 clk_disable_unprepare(sirfport->clk);
918}
919
Rong Wang161e7732011-11-17 23:17:04 +0800920static int sirfsoc_uart_startup(struct uart_port *port)
921{
922 struct sirfsoc_uart_port *sirfport = to_sirfport(port);
Qipan Li15cdcb12013-08-19 11:47:52 +0800923 struct sirfsoc_register *ureg = &sirfport->uart_reg->uart_reg;
Rong Wang161e7732011-11-17 23:17:04 +0800924 unsigned int index = port->line;
925 int ret;
926 set_irq_flags(port->irq, IRQF_VALID | IRQF_NOAUTOEN);
927 ret = request_irq(port->irq,
928 sirfsoc_uart_isr,
929 0,
930 SIRFUART_PORT_NAME,
931 sirfport);
932 if (ret != 0) {
933 dev_err(port->dev, "UART%d request IRQ line (%d) failed.\n",
934 index, port->irq);
935 goto irq_err;
936 }
Qipan Li15cdcb12013-08-19 11:47:52 +0800937 /* initial hardware settings */
938 wr_regl(port, ureg->sirfsoc_tx_dma_io_ctrl,
939 rd_regl(port, ureg->sirfsoc_tx_dma_io_ctrl) |
940 SIRFUART_IO_MODE);
941 wr_regl(port, ureg->sirfsoc_rx_dma_io_ctrl,
942 rd_regl(port, ureg->sirfsoc_rx_dma_io_ctrl) |
943 SIRFUART_IO_MODE);
Qipan Li0f17e3b2015-05-26 09:36:00 +0000944 wr_regl(port, ureg->sirfsoc_rx_dma_io_ctrl,
945 rd_regl(port, ureg->sirfsoc_rx_dma_io_ctrl) &
946 ~SIRFUART_RX_DMA_FLUSH);
Qipan Li15cdcb12013-08-19 11:47:52 +0800947 wr_regl(port, ureg->sirfsoc_tx_dma_io_len, 0);
948 wr_regl(port, ureg->sirfsoc_rx_dma_io_len, 0);
949 wr_regl(port, ureg->sirfsoc_tx_rx_en, SIRFUART_RX_EN | SIRFUART_TX_EN);
950 if (sirfport->uart_reg->uart_type == SIRF_USP_UART)
951 wr_regl(port, ureg->sirfsoc_mode1,
952 SIRFSOC_USP_ENDIAN_CTRL_LSBF |
953 SIRFSOC_USP_EN);
954 wr_regl(port, ureg->sirfsoc_tx_fifo_op, SIRFUART_FIFO_RESET);
Qipan Li15cdcb12013-08-19 11:47:52 +0800955 wr_regl(port, ureg->sirfsoc_rx_fifo_op, SIRFUART_FIFO_RESET);
956 wr_regl(port, ureg->sirfsoc_rx_fifo_op, 0);
957 wr_regl(port, ureg->sirfsoc_tx_fifo_ctrl, SIRFUART_FIFO_THD(port));
958 wr_regl(port, ureg->sirfsoc_rx_fifo_ctrl, SIRFUART_FIFO_THD(port));
Qipan Li9be16b32014-01-30 13:57:29 +0800959 if (sirfport->rx_dma_chan)
Qipan Li8316d042013-08-19 11:47:53 +0800960 wr_regl(port, ureg->sirfsoc_rx_fifo_level_chk,
Qipan Li9be16b32014-01-30 13:57:29 +0800961 SIRFUART_RX_FIFO_CHK_SC(port->line, 0x4) |
962 SIRFUART_RX_FIFO_CHK_LC(port->line, 0xe) |
963 SIRFUART_RX_FIFO_CHK_HC(port->line, 0x1b));
964 if (sirfport->tx_dma_chan) {
Qipan Li8316d042013-08-19 11:47:53 +0800965 sirfport->tx_dma_state = TX_DMA_IDLE;
966 wr_regl(port, ureg->sirfsoc_tx_fifo_level_chk,
967 SIRFUART_TX_FIFO_CHK_SC(port->line, 0x1b) |
968 SIRFUART_TX_FIFO_CHK_LC(port->line, 0xe) |
969 SIRFUART_TX_FIFO_CHK_HC(port->line, 0x4));
970 }
Qipan Li2eb56182013-08-15 06:52:15 +0800971 sirfport->ms_enabled = false;
972 if (sirfport->uart_reg->uart_type == SIRF_USP_UART &&
973 sirfport->hw_flow_ctrl) {
974 set_irq_flags(gpio_to_irq(sirfport->cts_gpio),
975 IRQF_VALID | IRQF_NOAUTOEN);
976 ret = request_irq(gpio_to_irq(sirfport->cts_gpio),
977 sirfsoc_uart_usp_cts_handler, IRQF_TRIGGER_FALLING |
978 IRQF_TRIGGER_RISING, "usp_cts_irq", sirfport);
979 if (ret != 0) {
980 dev_err(port->dev, "UART-USP:request gpio irq fail\n");
981 goto init_rx_err;
982 }
983 }
Rong Wang161e7732011-11-17 23:17:04 +0800984 enable_irq(port->irq);
Qipan Li0f17e3b2015-05-26 09:36:00 +0000985 if (sirfport->rx_dma_chan && !sirfport->is_hrt_enabled) {
986 sirfport->is_hrt_enabled = true;
987 sirfport->rx_period_time = 20000000;
988 sirfport->rx_dma_items.xmit.tail =
989 sirfport->rx_dma_items.xmit.head = 0;
990 hrtimer_start(&sirfport->hrt,
991 ns_to_ktime(sirfport->rx_period_time),
992 HRTIMER_MODE_REL);
993 }
Qipan Li2eb56182013-08-15 06:52:15 +0800994
Qipan Li15cdcb12013-08-19 11:47:52 +0800995 return 0;
Qipan Li2eb56182013-08-15 06:52:15 +0800996init_rx_err:
997 free_irq(port->irq, sirfport);
Rong Wang161e7732011-11-17 23:17:04 +0800998irq_err:
999 return ret;
1000}
1001
1002static void sirfsoc_uart_shutdown(struct uart_port *port)
1003{
1004 struct sirfsoc_uart_port *sirfport = to_sirfport(port);
Qipan Li5df83112013-08-12 18:15:35 +08001005 struct sirfsoc_register *ureg = &sirfport->uart_reg->uart_reg;
Barry Song057badd2015-01-03 17:02:57 +08001006 if (!sirfport->is_atlas7)
Qipan Li5df83112013-08-12 18:15:35 +08001007 wr_regl(port, ureg->sirfsoc_int_en_reg, 0);
Barry Song909102d2013-08-07 13:35:38 +08001008 else
Qipan Lic1b7ac62015-05-14 06:45:21 +00001009 wr_regl(port, ureg->sirfsoc_int_en_clr_reg, ~0UL);
Barry Song909102d2013-08-07 13:35:38 +08001010
Rong Wang161e7732011-11-17 23:17:04 +08001011 free_irq(port->irq, sirfport);
Qipan Li2eb56182013-08-15 06:52:15 +08001012 if (sirfport->ms_enabled)
Rong Wang161e7732011-11-17 23:17:04 +08001013 sirfsoc_uart_disable_ms(port);
Qipan Li2eb56182013-08-15 06:52:15 +08001014 if (sirfport->uart_reg->uart_type == SIRF_USP_UART &&
1015 sirfport->hw_flow_ctrl) {
1016 gpio_set_value(sirfport->rts_gpio, 1);
1017 free_irq(gpio_to_irq(sirfport->cts_gpio), sirfport);
Rong Wang161e7732011-11-17 23:17:04 +08001018 }
Qipan Li9be16b32014-01-30 13:57:29 +08001019 if (sirfport->tx_dma_chan)
Qipan Li8316d042013-08-19 11:47:53 +08001020 sirfport->tx_dma_state = TX_DMA_IDLE;
Qipan Li0f17e3b2015-05-26 09:36:00 +00001021 if (sirfport->rx_dma_chan && sirfport->is_hrt_enabled) {
1022 while ((rd_regl(port, ureg->sirfsoc_rx_fifo_status) &
1023 SIRFUART_RX_FIFO_MASK) > 0)
1024 ;
1025 sirfport->is_hrt_enabled = false;
1026 hrtimer_cancel(&sirfport->hrt);
1027 }
Rong Wang161e7732011-11-17 23:17:04 +08001028}
1029
1030static const char *sirfsoc_uart_type(struct uart_port *port)
1031{
1032 return port->type == SIRFSOC_PORT_TYPE ? SIRFUART_PORT_NAME : NULL;
1033}
1034
1035static int sirfsoc_uart_request_port(struct uart_port *port)
1036{
Qipan Li5df83112013-08-12 18:15:35 +08001037 struct sirfsoc_uart_port *sirfport = to_sirfport(port);
1038 struct sirfsoc_uart_param *uart_param = &sirfport->uart_reg->uart_param;
Rong Wang161e7732011-11-17 23:17:04 +08001039 void *ret;
1040 ret = request_mem_region(port->mapbase,
Qipan Li5df83112013-08-12 18:15:35 +08001041 SIRFUART_MAP_SIZE, uart_param->port_name);
Rong Wang161e7732011-11-17 23:17:04 +08001042 return ret ? 0 : -EBUSY;
1043}
1044
1045static void sirfsoc_uart_release_port(struct uart_port *port)
1046{
1047 release_mem_region(port->mapbase, SIRFUART_MAP_SIZE);
1048}
1049
1050static void sirfsoc_uart_config_port(struct uart_port *port, int flags)
1051{
1052 if (flags & UART_CONFIG_TYPE) {
1053 port->type = SIRFSOC_PORT_TYPE;
1054 sirfsoc_uart_request_port(port);
1055 }
1056}
1057
1058static struct uart_ops sirfsoc_uart_ops = {
1059 .tx_empty = sirfsoc_uart_tx_empty,
1060 .get_mctrl = sirfsoc_uart_get_mctrl,
1061 .set_mctrl = sirfsoc_uart_set_mctrl,
1062 .stop_tx = sirfsoc_uart_stop_tx,
1063 .start_tx = sirfsoc_uart_start_tx,
1064 .stop_rx = sirfsoc_uart_stop_rx,
1065 .enable_ms = sirfsoc_uart_enable_ms,
1066 .break_ctl = sirfsoc_uart_break_ctl,
1067 .startup = sirfsoc_uart_startup,
1068 .shutdown = sirfsoc_uart_shutdown,
1069 .set_termios = sirfsoc_uart_set_termios,
Qipan Li388faf92014-01-03 15:44:07 +08001070 .pm = sirfsoc_uart_pm,
Rong Wang161e7732011-11-17 23:17:04 +08001071 .type = sirfsoc_uart_type,
1072 .release_port = sirfsoc_uart_release_port,
1073 .request_port = sirfsoc_uart_request_port,
1074 .config_port = sirfsoc_uart_config_port,
1075};
1076
1077#ifdef CONFIG_SERIAL_SIRFSOC_CONSOLE
Qipan Li5df83112013-08-12 18:15:35 +08001078static int __init
1079sirfsoc_uart_console_setup(struct console *co, char *options)
Rong Wang161e7732011-11-17 23:17:04 +08001080{
1081 unsigned int baud = 115200;
1082 unsigned int bits = 8;
1083 unsigned int parity = 'n';
1084 unsigned int flow = 'n';
Qipan Lia6ffe892015-04-29 06:45:08 +00001085 struct sirfsoc_uart_port *sirfport;
1086 struct sirfsoc_register *ureg;
Rong Wang161e7732011-11-17 23:17:04 +08001087 if (co->index < 0 || co->index >= SIRFSOC_UART_NR)
Qipan Lic35b49b2015-05-14 06:45:26 +00001088 co->index = 1;
Qipan Lia6ffe892015-04-29 06:45:08 +00001089 sirfport = sirf_ports[co->index];
1090 if (!sirfport)
1091 return -ENODEV;
1092 ureg = &sirfport->uart_reg->uart_reg;
1093 if (!sirfport->port.mapbase)
Rong Wang161e7732011-11-17 23:17:04 +08001094 return -ENODEV;
1095
Qipan Li5df83112013-08-12 18:15:35 +08001096 /* enable usp in mode1 register */
1097 if (sirfport->uart_reg->uart_type == SIRF_USP_UART)
Qipan Lia6ffe892015-04-29 06:45:08 +00001098 wr_regl(&sirfport->port, ureg->sirfsoc_mode1, SIRFSOC_USP_EN |
Qipan Li5df83112013-08-12 18:15:35 +08001099 SIRFSOC_USP_ENDIAN_CTRL_LSBF);
Rong Wang161e7732011-11-17 23:17:04 +08001100 if (options)
1101 uart_parse_options(options, &baud, &parity, &bits, &flow);
Qipan Lia6ffe892015-04-29 06:45:08 +00001102 sirfport->port.cons = co;
Qipan Li5df83112013-08-12 18:15:35 +08001103
Qipan Li8316d042013-08-19 11:47:53 +08001104 /* default console tx/rx transfer using io mode */
Qipan Li9be16b32014-01-30 13:57:29 +08001105 sirfport->rx_dma_chan = NULL;
1106 sirfport->tx_dma_chan = NULL;
Qipan Lia6ffe892015-04-29 06:45:08 +00001107 return uart_set_options(&sirfport->port, co, baud, parity, bits, flow);
Rong Wang161e7732011-11-17 23:17:04 +08001108}
1109
1110static void sirfsoc_uart_console_putchar(struct uart_port *port, int ch)
1111{
Qipan Li5df83112013-08-12 18:15:35 +08001112 struct sirfsoc_uart_port *sirfport = to_sirfport(port);
1113 struct sirfsoc_register *ureg = &sirfport->uart_reg->uart_reg;
1114 struct sirfsoc_fifo_status *ufifo_st = &sirfport->uart_reg->fifo_status;
Qipan Licb4595a2015-04-29 06:45:09 +00001115 while (rd_regl(port, ureg->sirfsoc_tx_fifo_status) &
1116 ufifo_st->ff_full(port))
Rong Wang161e7732011-11-17 23:17:04 +08001117 cpu_relax();
Barry Song205c3842014-05-05 08:05:51 +08001118 wr_regl(port, ureg->sirfsoc_tx_fifo_data, ch);
Rong Wang161e7732011-11-17 23:17:04 +08001119}
1120
1121static void sirfsoc_uart_console_write(struct console *co, const char *s,
1122 unsigned int count)
1123{
Qipan Lia6ffe892015-04-29 06:45:08 +00001124 struct sirfsoc_uart_port *sirfport = sirf_ports[co->index];
1125
1126 uart_console_write(&sirfport->port, s, count,
1127 sirfsoc_uart_console_putchar);
Rong Wang161e7732011-11-17 23:17:04 +08001128}
1129
1130static struct console sirfsoc_uart_console = {
1131 .name = SIRFSOC_UART_NAME,
1132 .device = uart_console_device,
1133 .flags = CON_PRINTBUFFER,
1134 .index = -1,
1135 .write = sirfsoc_uart_console_write,
1136 .setup = sirfsoc_uart_console_setup,
1137 .data = &sirfsoc_uart_drv,
1138};
1139
1140static int __init sirfsoc_uart_console_init(void)
1141{
1142 register_console(&sirfsoc_uart_console);
1143 return 0;
1144}
1145console_initcall(sirfsoc_uart_console_init);
1146#endif
1147
1148static struct uart_driver sirfsoc_uart_drv = {
1149 .owner = THIS_MODULE,
1150 .driver_name = SIRFUART_PORT_NAME,
1151 .nr = SIRFSOC_UART_NR,
1152 .dev_name = SIRFSOC_UART_NAME,
1153 .major = SIRFSOC_UART_MAJOR,
1154 .minor = SIRFSOC_UART_MINOR,
1155#ifdef CONFIG_SERIAL_SIRFSOC_CONSOLE
1156 .cons = &sirfsoc_uart_console,
1157#else
1158 .cons = NULL,
1159#endif
1160};
1161
Qipan Li0f17e3b2015-05-26 09:36:00 +00001162static enum hrtimer_restart
1163 sirfsoc_uart_rx_dma_hrtimer_callback(struct hrtimer *hrt)
1164{
1165 struct sirfsoc_uart_port *sirfport;
1166 struct uart_port *port;
1167 int count, inserted;
1168 struct dma_tx_state tx_state;
1169 struct tty_struct *tty;
1170 struct sirfsoc_register *ureg;
1171 struct circ_buf *xmit;
1172
1173 sirfport = container_of(hrt, struct sirfsoc_uart_port, hrt);
1174 port = &sirfport->port;
1175 inserted = 0;
1176 tty = port->state->port.tty;
1177 ureg = &sirfport->uart_reg->uart_reg;
1178 xmit = &sirfport->rx_dma_items.xmit;
1179 dmaengine_tx_status(sirfport->rx_dma_chan,
1180 sirfport->rx_dma_items.cookie, &tx_state);
1181 xmit->head = SIRFSOC_RX_DMA_BUF_SIZE - tx_state.residue;
1182 count = CIRC_CNT_TO_END(xmit->head, xmit->tail,
1183 SIRFSOC_RX_DMA_BUF_SIZE);
1184 while (count > 0) {
1185 inserted = tty_insert_flip_string(tty->port,
1186 (const unsigned char *)&xmit->buf[xmit->tail], count);
1187 if (!inserted)
1188 goto next_hrt;
1189 port->icount.rx += inserted;
1190 xmit->tail = (xmit->tail + inserted) &
1191 (SIRFSOC_RX_DMA_BUF_SIZE - 1);
1192 count = CIRC_CNT_TO_END(xmit->head, xmit->tail,
1193 SIRFSOC_RX_DMA_BUF_SIZE);
1194 tty_flip_buffer_push(tty->port);
1195 }
1196 /*
1197 * if RX DMA buffer data have all push into tty buffer, and there is
1198 * only little data(less than a dma transfer unit) left in rxfifo,
1199 * fetch it out in pio mode and switch back to dma immediately
1200 */
1201 if (!inserted && !count &&
1202 ((rd_regl(port, ureg->sirfsoc_rx_fifo_status) &
1203 SIRFUART_RX_FIFO_MASK) > 0)) {
1204 /* switch to pio mode */
1205 wr_regl(port, ureg->sirfsoc_rx_dma_io_ctrl,
1206 rd_regl(port, ureg->sirfsoc_rx_dma_io_ctrl) |
1207 SIRFUART_IO_MODE);
1208 while ((rd_regl(port, ureg->sirfsoc_rx_fifo_status) &
1209 SIRFUART_RX_FIFO_MASK) > 0) {
1210 if (sirfsoc_uart_pio_rx_chars(port, 16) > 0)
1211 tty_flip_buffer_push(tty->port);
1212 }
1213 wr_regl(port, ureg->sirfsoc_rx_fifo_op, SIRFUART_FIFO_RESET);
1214 wr_regl(port, ureg->sirfsoc_rx_fifo_op, 0);
1215 wr_regl(port, ureg->sirfsoc_rx_fifo_op, SIRFUART_FIFO_START);
1216 /* switch back to dma mode */
1217 wr_regl(port, ureg->sirfsoc_rx_dma_io_ctrl,
1218 rd_regl(port, ureg->sirfsoc_rx_dma_io_ctrl) &
1219 ~SIRFUART_IO_MODE);
1220 }
1221next_hrt:
1222 hrtimer_forward_now(hrt, ns_to_ktime(sirfport->rx_period_time));
1223 return HRTIMER_RESTART;
1224}
1225
Qipan Lic1b7ac62015-05-14 06:45:21 +00001226static struct of_device_id sirfsoc_uart_ids[] = {
Qipan Li5df83112013-08-12 18:15:35 +08001227 { .compatible = "sirf,prima2-uart", .data = &sirfsoc_uart,},
Barry Song057badd2015-01-03 17:02:57 +08001228 { .compatible = "sirf,atlas7-uart", .data = &sirfsoc_uart},
Qipan Li5df83112013-08-12 18:15:35 +08001229 { .compatible = "sirf,prima2-usp-uart", .data = &sirfsoc_usp},
Qipan Lic1b7ac62015-05-14 06:45:21 +00001230 { .compatible = "sirf,atlas7-usp-uart", .data = &sirfsoc_usp},
Qipan Li5df83112013-08-12 18:15:35 +08001231 {}
1232};
1233MODULE_DEVICE_TABLE(of, sirfsoc_uart_ids);
1234
Jingoo Hanada1f442013-08-08 17:41:43 +09001235static int sirfsoc_uart_probe(struct platform_device *pdev)
Rong Wang161e7732011-11-17 23:17:04 +08001236{
1237 struct sirfsoc_uart_port *sirfport;
1238 struct uart_port *port;
1239 struct resource *res;
1240 int ret;
Qipan Li9be16b32014-01-30 13:57:29 +08001241 struct dma_slave_config slv_cfg = {
1242 .src_maxburst = 2,
1243 };
1244 struct dma_slave_config tx_slv_cfg = {
1245 .dst_maxburst = 2,
1246 };
Qipan Li5df83112013-08-12 18:15:35 +08001247 const struct of_device_id *match;
Rong Wang161e7732011-11-17 23:17:04 +08001248
Qipan Li5df83112013-08-12 18:15:35 +08001249 match = of_match_node(sirfsoc_uart_ids, pdev->dev.of_node);
Qipan Lia6ffe892015-04-29 06:45:08 +00001250 sirfport = devm_kzalloc(&pdev->dev, sizeof(*sirfport), GFP_KERNEL);
1251 if (!sirfport) {
1252 ret = -ENOMEM;
Rong Wang161e7732011-11-17 23:17:04 +08001253 goto err;
1254 }
Qipan Lia6ffe892015-04-29 06:45:08 +00001255 sirfport->port.line = of_alias_get_id(pdev->dev.of_node, "serial");
1256 sirf_ports[sirfport->port.line] = sirfport;
1257 sirfport->port.iotype = UPIO_MEM;
1258 sirfport->port.flags = UPF_BOOT_AUTOCONF;
Rong Wang161e7732011-11-17 23:17:04 +08001259 port = &sirfport->port;
1260 port->dev = &pdev->dev;
1261 port->private_data = sirfport;
Qipan Li5df83112013-08-12 18:15:35 +08001262 sirfport->uart_reg = (struct sirfsoc_uart_register *)match->data;
Rong Wang161e7732011-11-17 23:17:04 +08001263
Qipan Li2eb56182013-08-15 06:52:15 +08001264 sirfport->hw_flow_ctrl = of_property_read_bool(pdev->dev.of_node,
1265 "sirf,uart-has-rtscts");
Qipan Lic1b7ac62015-05-14 06:45:21 +00001266 if (of_device_is_compatible(pdev->dev.of_node, "sirf,prima2-uart") ||
1267 of_device_is_compatible(pdev->dev.of_node, "sirf,atlas7-uart"))
Qipan Li5df83112013-08-12 18:15:35 +08001268 sirfport->uart_reg->uart_type = SIRF_REAL_UART;
Qipan Lic1b7ac62015-05-14 06:45:21 +00001269 if (of_device_is_compatible(pdev->dev.of_node,
1270 "sirf,prima2-usp-uart") || of_device_is_compatible(
1271 pdev->dev.of_node, "sirf,atlas7-usp-uart")) {
Qipan Li5df83112013-08-12 18:15:35 +08001272 sirfport->uart_reg->uart_type = SIRF_USP_UART;
Qipan Li2eb56182013-08-15 06:52:15 +08001273 if (!sirfport->hw_flow_ctrl)
1274 goto usp_no_flow_control;
1275 if (of_find_property(pdev->dev.of_node, "cts-gpios", NULL))
1276 sirfport->cts_gpio = of_get_named_gpio(
1277 pdev->dev.of_node, "cts-gpios", 0);
1278 else
1279 sirfport->cts_gpio = -1;
1280 if (of_find_property(pdev->dev.of_node, "rts-gpios", NULL))
1281 sirfport->rts_gpio = of_get_named_gpio(
1282 pdev->dev.of_node, "rts-gpios", 0);
1283 else
1284 sirfport->rts_gpio = -1;
1285
1286 if ((!gpio_is_valid(sirfport->cts_gpio) ||
1287 !gpio_is_valid(sirfport->rts_gpio))) {
1288 ret = -EINVAL;
1289 dev_err(&pdev->dev,
Qipan Li67bc3062013-08-19 11:47:51 +08001290 "Usp flow control must have cts and rts gpio");
Qipan Li2eb56182013-08-15 06:52:15 +08001291 goto err;
1292 }
1293 ret = devm_gpio_request(&pdev->dev, sirfport->cts_gpio,
Qipan Li67bc3062013-08-19 11:47:51 +08001294 "usp-cts-gpio");
Qipan Li2eb56182013-08-15 06:52:15 +08001295 if (ret) {
Qipan Li67bc3062013-08-19 11:47:51 +08001296 dev_err(&pdev->dev, "Unable request cts gpio");
Qipan Li2eb56182013-08-15 06:52:15 +08001297 goto err;
1298 }
1299 gpio_direction_input(sirfport->cts_gpio);
1300 ret = devm_gpio_request(&pdev->dev, sirfport->rts_gpio,
Qipan Li67bc3062013-08-19 11:47:51 +08001301 "usp-rts-gpio");
Qipan Li2eb56182013-08-15 06:52:15 +08001302 if (ret) {
Qipan Li67bc3062013-08-19 11:47:51 +08001303 dev_err(&pdev->dev, "Unable request rts gpio");
Qipan Li2eb56182013-08-15 06:52:15 +08001304 goto err;
1305 }
1306 gpio_direction_output(sirfport->rts_gpio, 1);
1307 }
1308usp_no_flow_control:
Qipan Lic1b7ac62015-05-14 06:45:21 +00001309 if (of_device_is_compatible(pdev->dev.of_node, "sirf,atlas7-uart") ||
1310 of_device_is_compatible(pdev->dev.of_node, "sirf,atlas7-usp-uart"))
Barry Song057badd2015-01-03 17:02:57 +08001311 sirfport->is_atlas7 = true;
Barry Song909102d2013-08-07 13:35:38 +08001312
Rong Wang161e7732011-11-17 23:17:04 +08001313 if (of_property_read_u32(pdev->dev.of_node,
1314 "fifosize",
1315 &port->fifosize)) {
1316 dev_err(&pdev->dev,
1317 "Unable to find fifosize in uart node.\n");
1318 ret = -EFAULT;
1319 goto err;
1320 }
1321
1322 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1323 if (res == NULL) {
1324 dev_err(&pdev->dev, "Insufficient resources.\n");
1325 ret = -EFAULT;
1326 goto err;
1327 }
1328 port->mapbase = res->start;
Qipan Li0f17e3b2015-05-26 09:36:00 +00001329 port->membase = devm_ioremap(&pdev->dev,
1330 res->start, resource_size(res));
Rong Wang161e7732011-11-17 23:17:04 +08001331 if (!port->membase) {
1332 dev_err(&pdev->dev, "Cannot remap resource.\n");
1333 ret = -ENOMEM;
1334 goto err;
1335 }
1336 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
1337 if (res == NULL) {
1338 dev_err(&pdev->dev, "Insufficient resources.\n");
1339 ret = -EFAULT;
Julia Lawall9250dd52012-09-01 18:33:09 +02001340 goto err;
Rong Wang161e7732011-11-17 23:17:04 +08001341 }
1342 port->irq = res->start;
1343
Qipan Liadeede72015-04-20 08:10:23 +00001344 sirfport->clk = devm_clk_get(&pdev->dev, NULL);
Barry Songac4ce712013-01-16 14:49:27 +08001345 if (IS_ERR(sirfport->clk)) {
1346 ret = PTR_ERR(sirfport->clk);
Barry Songa3437562013-08-15 06:52:14 +08001347 goto err;
Barry Songac4ce712013-01-16 14:49:27 +08001348 }
Barry Songac4ce712013-01-16 14:49:27 +08001349 port->uartclk = clk_get_rate(sirfport->clk);
1350
Rong Wang161e7732011-11-17 23:17:04 +08001351 port->ops = &sirfsoc_uart_ops;
1352 spin_lock_init(&port->lock);
1353
1354 platform_set_drvdata(pdev, sirfport);
1355 ret = uart_add_one_port(&sirfsoc_uart_drv, port);
1356 if (ret != 0) {
1357 dev_err(&pdev->dev, "Cannot add UART port(%d).\n", pdev->id);
Qipan Liadeede72015-04-20 08:10:23 +00001358 goto err;
Rong Wang161e7732011-11-17 23:17:04 +08001359 }
1360
Qipan Li9be16b32014-01-30 13:57:29 +08001361 sirfport->rx_dma_chan = dma_request_slave_channel(port->dev, "rx");
Qipan Li0f17e3b2015-05-26 09:36:00 +00001362 sirfport->rx_dma_items.xmit.buf =
1363 dma_alloc_coherent(port->dev, SIRFSOC_RX_DMA_BUF_SIZE,
1364 &sirfport->rx_dma_items.dma_addr, GFP_KERNEL);
1365 if (!sirfport->rx_dma_items.xmit.buf) {
1366 dev_err(port->dev, "Uart alloc bufa failed\n");
1367 ret = -ENOMEM;
1368 goto alloc_coherent_err;
Qipan Li9be16b32014-01-30 13:57:29 +08001369 }
Qipan Li0f17e3b2015-05-26 09:36:00 +00001370 sirfport->rx_dma_items.xmit.head =
1371 sirfport->rx_dma_items.xmit.tail = 0;
Qipan Li9be16b32014-01-30 13:57:29 +08001372 if (sirfport->rx_dma_chan)
1373 dmaengine_slave_config(sirfport->rx_dma_chan, &slv_cfg);
1374 sirfport->tx_dma_chan = dma_request_slave_channel(port->dev, "tx");
1375 if (sirfport->tx_dma_chan)
1376 dmaengine_slave_config(sirfport->tx_dma_chan, &tx_slv_cfg);
Qipan Li0f17e3b2015-05-26 09:36:00 +00001377 if (sirfport->rx_dma_chan) {
1378 hrtimer_init(&sirfport->hrt, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1379 sirfport->hrt.function = sirfsoc_uart_rx_dma_hrtimer_callback;
1380 sirfport->is_hrt_enabled = false;
1381 }
Rong Wang161e7732011-11-17 23:17:04 +08001382
Qipan Li9be16b32014-01-30 13:57:29 +08001383 return 0;
1384alloc_coherent_err:
Qipan Li0f17e3b2015-05-26 09:36:00 +00001385 dma_free_coherent(port->dev, SIRFSOC_RX_DMA_BUF_SIZE,
1386 sirfport->rx_dma_items.xmit.buf,
1387 sirfport->rx_dma_items.dma_addr);
Qipan Li9be16b32014-01-30 13:57:29 +08001388 dma_release_channel(sirfport->rx_dma_chan);
Rong Wang161e7732011-11-17 23:17:04 +08001389err:
1390 return ret;
1391}
1392
1393static int sirfsoc_uart_remove(struct platform_device *pdev)
1394{
1395 struct sirfsoc_uart_port *sirfport = platform_get_drvdata(pdev);
1396 struct uart_port *port = &sirfport->port;
Rong Wang161e7732011-11-17 23:17:04 +08001397 uart_remove_one_port(&sirfsoc_uart_drv, port);
Qipan Li9be16b32014-01-30 13:57:29 +08001398 if (sirfport->rx_dma_chan) {
Qipan Li9be16b32014-01-30 13:57:29 +08001399 dmaengine_terminate_all(sirfport->rx_dma_chan);
1400 dma_release_channel(sirfport->rx_dma_chan);
Qipan Li0f17e3b2015-05-26 09:36:00 +00001401 dma_free_coherent(port->dev, SIRFSOC_RX_DMA_BUF_SIZE,
1402 sirfport->rx_dma_items.xmit.buf,
1403 sirfport->rx_dma_items.dma_addr);
Qipan Li9be16b32014-01-30 13:57:29 +08001404 }
1405 if (sirfport->tx_dma_chan) {
1406 dmaengine_terminate_all(sirfport->tx_dma_chan);
1407 dma_release_channel(sirfport->tx_dma_chan);
1408 }
Rong Wang161e7732011-11-17 23:17:04 +08001409 return 0;
1410}
1411
Qipan Li99e626f2014-01-03 15:44:06 +08001412#ifdef CONFIG_PM_SLEEP
Rong Wang161e7732011-11-17 23:17:04 +08001413static int
Qipan Li99e626f2014-01-03 15:44:06 +08001414sirfsoc_uart_suspend(struct device *pdev)
Rong Wang161e7732011-11-17 23:17:04 +08001415{
Qipan Li99e626f2014-01-03 15:44:06 +08001416 struct sirfsoc_uart_port *sirfport = dev_get_drvdata(pdev);
Rong Wang161e7732011-11-17 23:17:04 +08001417 struct uart_port *port = &sirfport->port;
1418 uart_suspend_port(&sirfsoc_uart_drv, port);
1419 return 0;
1420}
1421
Qipan Li99e626f2014-01-03 15:44:06 +08001422static int sirfsoc_uart_resume(struct device *pdev)
Rong Wang161e7732011-11-17 23:17:04 +08001423{
Qipan Li99e626f2014-01-03 15:44:06 +08001424 struct sirfsoc_uart_port *sirfport = dev_get_drvdata(pdev);
Rong Wang161e7732011-11-17 23:17:04 +08001425 struct uart_port *port = &sirfport->port;
1426 uart_resume_port(&sirfsoc_uart_drv, port);
1427 return 0;
1428}
Qipan Li99e626f2014-01-03 15:44:06 +08001429#endif
1430
1431static const struct dev_pm_ops sirfsoc_uart_pm_ops = {
1432 SET_SYSTEM_SLEEP_PM_OPS(sirfsoc_uart_suspend, sirfsoc_uart_resume)
1433};
Rong Wang161e7732011-11-17 23:17:04 +08001434
Rong Wang161e7732011-11-17 23:17:04 +08001435static struct platform_driver sirfsoc_uart_driver = {
1436 .probe = sirfsoc_uart_probe,
Bill Pemberton2d47b712012-11-19 13:21:34 -05001437 .remove = sirfsoc_uart_remove,
Rong Wang161e7732011-11-17 23:17:04 +08001438 .driver = {
1439 .name = SIRFUART_PORT_NAME,
Rong Wang161e7732011-11-17 23:17:04 +08001440 .of_match_table = sirfsoc_uart_ids,
Qipan Li99e626f2014-01-03 15:44:06 +08001441 .pm = &sirfsoc_uart_pm_ops,
Rong Wang161e7732011-11-17 23:17:04 +08001442 },
1443};
1444
1445static int __init sirfsoc_uart_init(void)
1446{
1447 int ret = 0;
1448
1449 ret = uart_register_driver(&sirfsoc_uart_drv);
1450 if (ret)
1451 goto out;
1452
1453 ret = platform_driver_register(&sirfsoc_uart_driver);
1454 if (ret)
1455 uart_unregister_driver(&sirfsoc_uart_drv);
1456out:
1457 return ret;
1458}
1459module_init(sirfsoc_uart_init);
1460
1461static void __exit sirfsoc_uart_exit(void)
1462{
1463 platform_driver_unregister(&sirfsoc_uart_driver);
1464 uart_unregister_driver(&sirfsoc_uart_drv);
1465}
1466module_exit(sirfsoc_uart_exit);
1467
1468MODULE_LICENSE("GPL v2");
1469MODULE_AUTHOR("Bin Shi <Bin.Shi@csr.com>, Rong Wang<Rong.Wang@csr.com>");
1470MODULE_DESCRIPTION("CSR SiRFprimaII Uart Driver");