blob: d87003d49c05f604e7f7520ea6ea58ab29468eee [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>
23#include <asm/irq.h>
24#include <asm/mach/irq.h>
Rong Wang161e7732011-11-17 23:17:04 +080025
26#include "sirfsoc_uart.h"
27
28static unsigned int
29sirfsoc_uart_pio_tx_chars(struct sirfsoc_uart_port *sirfport, int count);
30static unsigned int
31sirfsoc_uart_pio_rx_chars(struct uart_port *port, unsigned int max_rx_count);
32static struct uart_driver sirfsoc_uart_drv;
33
34static const struct sirfsoc_baudrate_to_regv baudrate_to_regv[] = {
35 {4000000, 2359296},
36 {3500000, 1310721},
37 {3000000, 1572865},
38 {2500000, 1245186},
39 {2000000, 1572866},
40 {1500000, 1245188},
41 {1152000, 1638404},
42 {1000000, 1572869},
43 {921600, 1114120},
44 {576000, 1245196},
45 {500000, 1245198},
46 {460800, 1572876},
47 {230400, 1310750},
48 {115200, 1310781},
49 {57600, 1310843},
50 {38400, 1114328},
51 {19200, 1114545},
52 {9600, 1114979},
53};
54
55static struct sirfsoc_uart_port sirfsoc_uart_ports[SIRFSOC_UART_NR] = {
56 [0] = {
57 .port = {
58 .iotype = UPIO_MEM,
59 .flags = UPF_BOOT_AUTOCONF,
60 .line = 0,
61 },
62 },
63 [1] = {
64 .port = {
65 .iotype = UPIO_MEM,
66 .flags = UPF_BOOT_AUTOCONF,
67 .line = 1,
68 },
69 },
70 [2] = {
71 .port = {
72 .iotype = UPIO_MEM,
73 .flags = UPF_BOOT_AUTOCONF,
74 .line = 2,
75 },
76 },
Barry Song5425e032012-12-25 17:32:04 +080077 [3] = {
78 .port = {
79 .iotype = UPIO_MEM,
80 .flags = UPF_BOOT_AUTOCONF,
81 .line = 3,
82 },
83 },
84 [4] = {
85 .port = {
86 .iotype = UPIO_MEM,
87 .flags = UPF_BOOT_AUTOCONF,
88 .line = 4,
89 },
90 },
Rong Wang161e7732011-11-17 23:17:04 +080091};
92
93static inline struct sirfsoc_uart_port *to_sirfport(struct uart_port *port)
94{
95 return container_of(port, struct sirfsoc_uart_port, port);
96}
97
98static inline unsigned int sirfsoc_uart_tx_empty(struct uart_port *port)
99{
100 unsigned long reg;
Qipan Li5df83112013-08-12 18:15:35 +0800101 struct sirfsoc_uart_port *sirfport = to_sirfport(port);
102 struct sirfsoc_register *ureg = &sirfport->uart_reg->uart_reg;
103 struct sirfsoc_fifo_status *ufifo_st = &sirfport->uart_reg->fifo_status;
104 reg = rd_regl(port, ureg->sirfsoc_tx_fifo_status);
105
106 return (reg & ufifo_st->ff_empty(port->line)) ? TIOCSER_TEMT : 0;
Rong Wang161e7732011-11-17 23:17:04 +0800107}
108
109static unsigned int sirfsoc_uart_get_mctrl(struct uart_port *port)
110{
111 struct sirfsoc_uart_port *sirfport = to_sirfport(port);
Qipan Li5df83112013-08-12 18:15:35 +0800112 struct sirfsoc_register *ureg = &sirfport->uart_reg->uart_reg;
Rong Wang161e7732011-11-17 23:17:04 +0800113 if (!(sirfport->ms_enabled)) {
114 goto cts_asserted;
115 } else if (sirfport->hw_flow_ctrl) {
Qipan Li5df83112013-08-12 18:15:35 +0800116 if (!(rd_regl(port, ureg->sirfsoc_afc_ctrl) &
117 SIRFUART_AFC_CTS_STATUS))
Rong Wang161e7732011-11-17 23:17:04 +0800118 goto cts_asserted;
119 else
120 goto cts_deasserted;
121 }
122cts_deasserted:
123 return TIOCM_CAR | TIOCM_DSR;
124cts_asserted:
125 return TIOCM_CAR | TIOCM_DSR | TIOCM_CTS;
126}
127
128static void sirfsoc_uart_set_mctrl(struct uart_port *port, unsigned int mctrl)
129{
130 struct sirfsoc_uart_port *sirfport = to_sirfport(port);
Qipan Li5df83112013-08-12 18:15:35 +0800131 struct sirfsoc_register *ureg = &sirfport->uart_reg->uart_reg;
Rong Wang161e7732011-11-17 23:17:04 +0800132 unsigned int assert = mctrl & TIOCM_RTS;
133 unsigned int val = assert ? SIRFUART_AFC_CTRL_RX_THD : 0x0;
134 unsigned int current_val;
135 if (sirfport->hw_flow_ctrl) {
Qipan Li5df83112013-08-12 18:15:35 +0800136 current_val = rd_regl(port, ureg->sirfsoc_afc_ctrl) & ~0xFF;
Rong Wang161e7732011-11-17 23:17:04 +0800137 val |= current_val;
Qipan Li5df83112013-08-12 18:15:35 +0800138 wr_regl(port, ureg->sirfsoc_afc_ctrl, val);
Rong Wang161e7732011-11-17 23:17:04 +0800139 }
140}
141
142static void sirfsoc_uart_stop_tx(struct uart_port *port)
143{
Barry Song909102d2013-08-07 13:35:38 +0800144 struct sirfsoc_uart_port *sirfport = to_sirfport(port);
Qipan Li5df83112013-08-12 18:15:35 +0800145 struct sirfsoc_register *ureg = &sirfport->uart_reg->uart_reg;
146 struct sirfsoc_int_en *uint_en = &sirfport->uart_reg->uart_int_en;
Rong Wang161e7732011-11-17 23:17:04 +0800147 unsigned int regv;
Barry Song909102d2013-08-07 13:35:38 +0800148
149 if (!sirfport->is_marco) {
Qipan Li5df83112013-08-12 18:15:35 +0800150 regv = rd_regl(port, ureg->sirfsoc_int_en_reg);
151 wr_regl(port, ureg->sirfsoc_int_en_reg,
152 regv & ~uint_en->sirfsoc_txfifo_empty_en);
153 } else
154 wr_regl(port, SIRFUART_INT_EN_CLR,
155 uint_en->sirfsoc_txfifo_empty_en);
156
Rong Wang161e7732011-11-17 23:17:04 +0800157}
158
Jingoo Hanada1f443d2013-08-08 17:41:43 +0900159static void sirfsoc_uart_start_tx(struct uart_port *port)
Rong Wang161e7732011-11-17 23:17:04 +0800160{
161 struct sirfsoc_uart_port *sirfport = to_sirfport(port);
Qipan Li5df83112013-08-12 18:15:35 +0800162 struct sirfsoc_register *ureg = &sirfport->uart_reg->uart_reg;
163 struct sirfsoc_int_en *uint_en = &sirfport->uart_reg->uart_int_en;
Rong Wang161e7732011-11-17 23:17:04 +0800164 unsigned long regv;
Barry Song909102d2013-08-07 13:35:38 +0800165
Rong Wang161e7732011-11-17 23:17:04 +0800166 sirfsoc_uart_pio_tx_chars(sirfport, 1);
Qipan Li5df83112013-08-12 18:15:35 +0800167 wr_regl(port, ureg->sirfsoc_tx_fifo_op, SIRFUART_FIFO_START);
Barry Song909102d2013-08-07 13:35:38 +0800168 if (!sirfport->is_marco) {
Qipan Li5df83112013-08-12 18:15:35 +0800169 regv = rd_regl(port, ureg->sirfsoc_int_en_reg);
170 wr_regl(port, ureg->sirfsoc_int_en_reg, regv |
171 uint_en->sirfsoc_txfifo_empty_en);
172 } else
173 wr_regl(port, ureg->sirfsoc_int_en_reg,
174 uint_en->sirfsoc_txfifo_empty_en);
Rong Wang161e7732011-11-17 23:17:04 +0800175}
176
177static void sirfsoc_uart_stop_rx(struct uart_port *port)
178{
Barry Song909102d2013-08-07 13:35:38 +0800179 struct sirfsoc_uart_port *sirfport = to_sirfport(port);
Qipan Li5df83112013-08-12 18:15:35 +0800180 struct sirfsoc_register *ureg = &sirfport->uart_reg->uart_reg;
181 struct sirfsoc_int_en *uint_en = &sirfport->uart_reg->uart_int_en;
182 unsigned long reg;
183 wr_regl(port, ureg->sirfsoc_rx_fifo_op, 0);
Barry Song909102d2013-08-07 13:35:38 +0800184 if (!sirfport->is_marco) {
Qipan Li5df83112013-08-12 18:15:35 +0800185 reg = rd_regl(port, ureg->sirfsoc_int_en_reg);
186 wr_regl(port, ureg->sirfsoc_int_en_reg,
187 reg & ~(SIRFUART_RX_IO_INT_EN(port, uint_en)));
188 } else
189 wr_regl(port, SIRFUART_INT_EN_CLR,
190 SIRFUART_RX_IO_INT_EN(port, uint_en));
Rong Wang161e7732011-11-17 23:17:04 +0800191}
192
193static void sirfsoc_uart_disable_ms(struct uart_port *port)
194{
195 struct sirfsoc_uart_port *sirfport = to_sirfport(port);
Qipan Li5df83112013-08-12 18:15:35 +0800196 struct sirfsoc_register *ureg = &sirfport->uart_reg->uart_reg;
197 struct sirfsoc_int_en *uint_en = &sirfport->uart_reg->uart_int_en;
Rong Wang161e7732011-11-17 23:17:04 +0800198 unsigned long reg;
Barry Song909102d2013-08-07 13:35:38 +0800199
Rong Wang161e7732011-11-17 23:17:04 +0800200 sirfport->ms_enabled = 0;
201 if (!sirfport->hw_flow_ctrl)
202 return;
Barry Song909102d2013-08-07 13:35:38 +0800203
Qipan Li5df83112013-08-12 18:15:35 +0800204 reg = rd_regl(port, ureg->sirfsoc_afc_ctrl);
205 wr_regl(port, ureg->sirfsoc_afc_ctrl, reg & ~0x3FF);
Barry Song909102d2013-08-07 13:35:38 +0800206 if (!sirfport->is_marco) {
Qipan Li5df83112013-08-12 18:15:35 +0800207 reg = rd_regl(port, ureg->sirfsoc_int_en_reg);
208 wr_regl(port, ureg->sirfsoc_int_en_reg,
209 reg & ~uint_en->sirfsoc_cts_en);
210 } else
211 wr_regl(port, SIRFUART_INT_EN_CLR,
212 uint_en->sirfsoc_cts_en);
Rong Wang161e7732011-11-17 23:17:04 +0800213}
214
215static void sirfsoc_uart_enable_ms(struct uart_port *port)
216{
217 struct sirfsoc_uart_port *sirfport = to_sirfport(port);
Qipan Li5df83112013-08-12 18:15:35 +0800218 struct sirfsoc_register *ureg = &sirfport->uart_reg->uart_reg;
219 struct sirfsoc_int_en *uint_en = &sirfport->uart_reg->uart_int_en;
Rong Wang161e7732011-11-17 23:17:04 +0800220 unsigned long reg;
221 unsigned long flg;
Barry Song909102d2013-08-07 13:35:38 +0800222
Rong Wang161e7732011-11-17 23:17:04 +0800223 if (!sirfport->hw_flow_ctrl)
224 return;
Qipan Li5df83112013-08-12 18:15:35 +0800225 flg = SIRFUART_AFC_TX_EN | SIRFUART_AFC_RX_EN;
226 reg = rd_regl(port, ureg->sirfsoc_afc_ctrl);
227 wr_regl(port, ureg->sirfsoc_afc_ctrl, reg | flg);
Barry Song909102d2013-08-07 13:35:38 +0800228 if (!sirfport->is_marco) {
Qipan Li5df83112013-08-12 18:15:35 +0800229 reg = rd_regl(port, ureg->sirfsoc_int_en_reg);
230 wr_regl(port, ureg->sirfsoc_int_en_reg,
231 reg | uint_en->sirfsoc_cts_en);
232 } else
233 wr_regl(port, ureg->sirfsoc_int_en_reg,
234 uint_en->sirfsoc_cts_en);
Rong Wang161e7732011-11-17 23:17:04 +0800235 uart_handle_cts_change(port,
Qipan Li5df83112013-08-12 18:15:35 +0800236 !(rd_regl(port, ureg->sirfsoc_afc_ctrl) &
237 SIRFUART_AFC_CTS_STATUS));
Rong Wang161e7732011-11-17 23:17:04 +0800238 sirfport->ms_enabled = 1;
239}
240
241static void sirfsoc_uart_break_ctl(struct uart_port *port, int break_state)
242{
Qipan Li5df83112013-08-12 18:15:35 +0800243 struct sirfsoc_uart_port *sirfport = to_sirfport(port);
244 struct sirfsoc_register *ureg = &sirfport->uart_reg->uart_reg;
245 if (sirfport->uart_reg->uart_type == SIRF_REAL_UART) {
246 unsigned long ulcon = rd_regl(port, ureg->sirfsoc_line_ctrl);
247 if (break_state)
248 ulcon |= SIRFUART_SET_BREAK;
249 else
250 ulcon &= ~SIRFUART_SET_BREAK;
251 wr_regl(port, ureg->sirfsoc_line_ctrl, ulcon);
252 }
Rong Wang161e7732011-11-17 23:17:04 +0800253}
254
255static unsigned int
256sirfsoc_uart_pio_rx_chars(struct uart_port *port, unsigned int max_rx_count)
257{
Qipan Li5df83112013-08-12 18:15:35 +0800258 struct sirfsoc_uart_port *sirfport = to_sirfport(port);
259 struct sirfsoc_register *ureg = &sirfport->uart_reg->uart_reg;
260 struct sirfsoc_fifo_status *ufifo_st = &sirfport->uart_reg->fifo_status;
Rong Wang161e7732011-11-17 23:17:04 +0800261 unsigned int ch, rx_count = 0;
Qipan Li5df83112013-08-12 18:15:35 +0800262 struct tty_struct *tty;
263 tty = tty_port_tty_get(&port->state->port);
264 if (!tty)
265 return -ENODEV;
266 while (!(rd_regl(port, ureg->sirfsoc_rx_fifo_status) &
267 ufifo_st->ff_empty(port->line))) {
268 ch = rd_regl(port, ureg->sirfsoc_rx_fifo_data) |
269 SIRFUART_DUMMY_READ;
Rong Wang161e7732011-11-17 23:17:04 +0800270 if (unlikely(uart_handle_sysrq_char(port, ch)))
271 continue;
272 uart_insert_char(port, 0, 0, ch, TTY_NORMAL);
273 rx_count++;
274 if (rx_count >= max_rx_count)
275 break;
276 }
277
278 port->icount.rx += rx_count;
Jiri Slaby2e124b42013-01-03 15:53:06 +0100279 tty_flip_buffer_push(&port->state->port);
Rong Wang161e7732011-11-17 23:17:04 +0800280
281 return rx_count;
282}
283
284static unsigned int
285sirfsoc_uart_pio_tx_chars(struct sirfsoc_uart_port *sirfport, int count)
286{
287 struct uart_port *port = &sirfport->port;
Qipan Li5df83112013-08-12 18:15:35 +0800288 struct sirfsoc_register *ureg = &sirfport->uart_reg->uart_reg;
289 struct sirfsoc_fifo_status *ufifo_st = &sirfport->uart_reg->fifo_status;
Rong Wang161e7732011-11-17 23:17:04 +0800290 struct circ_buf *xmit = &port->state->xmit;
291 unsigned int num_tx = 0;
292 while (!uart_circ_empty(xmit) &&
Qipan Li5df83112013-08-12 18:15:35 +0800293 !(rd_regl(port, ureg->sirfsoc_tx_fifo_status) &
294 ufifo_st->ff_full(port->line)) &&
Rong Wang161e7732011-11-17 23:17:04 +0800295 count--) {
Qipan Li5df83112013-08-12 18:15:35 +0800296 wr_regl(port, ureg->sirfsoc_tx_fifo_data,
297 xmit->buf[xmit->tail]);
Rong Wang161e7732011-11-17 23:17:04 +0800298 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
299 port->icount.tx++;
300 num_tx++;
301 }
302 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
303 uart_write_wakeup(port);
304 return num_tx;
305}
306
307static irqreturn_t sirfsoc_uart_isr(int irq, void *dev_id)
308{
309 unsigned long intr_status;
310 unsigned long cts_status;
311 unsigned long flag = TTY_NORMAL;
312 struct sirfsoc_uart_port *sirfport = (struct sirfsoc_uart_port *)dev_id;
313 struct uart_port *port = &sirfport->port;
Qipan Li5df83112013-08-12 18:15:35 +0800314 struct sirfsoc_register *ureg = &sirfport->uart_reg->uart_reg;
315 struct sirfsoc_fifo_status *ufifo_st = &sirfport->uart_reg->fifo_status;
316 struct sirfsoc_int_status *uint_st = &sirfport->uart_reg->uart_int_st;
317 struct sirfsoc_int_en *uint_en = &sirfport->uart_reg->uart_int_en;
Rong Wang161e7732011-11-17 23:17:04 +0800318 struct uart_state *state = port->state;
319 struct circ_buf *xmit = &port->state->xmit;
Barry Song5425e032012-12-25 17:32:04 +0800320 spin_lock(&port->lock);
Qipan Li5df83112013-08-12 18:15:35 +0800321 intr_status = rd_regl(port, ureg->sirfsoc_int_st_reg);
322 wr_regl(port, ureg->sirfsoc_int_st_reg, intr_status);
323 if (unlikely(intr_status & (SIRFUART_ERR_INT_STAT(port, uint_st)))) {
324 if (intr_status & uint_st->sirfsoc_rxd_brk) {
325 port->icount.brk++;
Rong Wang161e7732011-11-17 23:17:04 +0800326 if (uart_handle_break(port))
327 goto recv_char;
Rong Wang161e7732011-11-17 23:17:04 +0800328 }
Qipan Li5df83112013-08-12 18:15:35 +0800329 if (intr_status & uint_st->sirfsoc_rx_oflow)
Rong Wang161e7732011-11-17 23:17:04 +0800330 port->icount.overrun++;
Qipan Li5df83112013-08-12 18:15:35 +0800331 if (intr_status & uint_st->sirfsoc_frm_err) {
Rong Wang161e7732011-11-17 23:17:04 +0800332 port->icount.frame++;
333 flag = TTY_FRAME;
334 }
Qipan Li5df83112013-08-12 18:15:35 +0800335 if (intr_status & uint_st->sirfsoc_parity_err)
Rong Wang161e7732011-11-17 23:17:04 +0800336 flag = TTY_PARITY;
Qipan Li5df83112013-08-12 18:15:35 +0800337 wr_regl(port, ureg->sirfsoc_rx_fifo_op, SIRFUART_FIFO_RESET);
338 wr_regl(port, ureg->sirfsoc_rx_fifo_op, 0);
339 wr_regl(port, ureg->sirfsoc_rx_fifo_op, SIRFUART_FIFO_START);
Rong Wang161e7732011-11-17 23:17:04 +0800340 intr_status &= port->read_status_mask;
341 uart_insert_char(port, intr_status,
Qipan Li5df83112013-08-12 18:15:35 +0800342 uint_en->sirfsoc_rx_oflow_en, 0, flag);
343 tty_flip_buffer_push(&state->port);
Rong Wang161e7732011-11-17 23:17:04 +0800344 }
345recv_char:
Qipan Li5df83112013-08-12 18:15:35 +0800346 if ((sirfport->uart_reg->uart_type == SIRF_REAL_UART) &&
347 (intr_status & SIRFUART_CTS_INT_ST(uint_st))) {
348 cts_status = rd_regl(port, ureg->sirfsoc_afc_ctrl) &
349 SIRFUART_AFC_CTS_STATUS;
350 if (cts_status != 0)
351 cts_status = 0;
352 else
353 cts_status = 1;
354 uart_handle_cts_change(port, cts_status);
355 wake_up_interruptible(&state->port.delta_msr_wait);
Rong Wang161e7732011-11-17 23:17:04 +0800356 }
Qipan Li5df83112013-08-12 18:15:35 +0800357 if (intr_status & SIRFUART_RX_IO_INT_ST(uint_st))
Rong Wang161e7732011-11-17 23:17:04 +0800358 sirfsoc_uart_pio_rx_chars(port, SIRFSOC_UART_IO_RX_MAX_CNT);
Qipan Li5df83112013-08-12 18:15:35 +0800359 if (intr_status & uint_st->sirfsoc_txfifo_empty) {
Rong Wang161e7732011-11-17 23:17:04 +0800360 if (uart_circ_empty(xmit) || uart_tx_stopped(port)) {
Barry Song5425e032012-12-25 17:32:04 +0800361 spin_unlock(&port->lock);
Rong Wang161e7732011-11-17 23:17:04 +0800362 return IRQ_HANDLED;
363 } else {
364 sirfsoc_uart_pio_tx_chars(sirfport,
365 SIRFSOC_UART_IO_TX_REASONABLE_CNT);
366 if ((uart_circ_empty(xmit)) &&
Qipan Li5df83112013-08-12 18:15:35 +0800367 (rd_regl(port, ureg->sirfsoc_tx_fifo_status) &
368 ufifo_st->ff_empty(port->line)))
Rong Wang161e7732011-11-17 23:17:04 +0800369 sirfsoc_uart_stop_tx(port);
370 }
371 }
Barry Song5425e032012-12-25 17:32:04 +0800372 spin_unlock(&port->lock);
Rong Wang161e7732011-11-17 23:17:04 +0800373 return IRQ_HANDLED;
374}
375
376static void sirfsoc_uart_start_rx(struct uart_port *port)
377{
Barry Song909102d2013-08-07 13:35:38 +0800378 struct sirfsoc_uart_port *sirfport = to_sirfport(port);
Qipan Li5df83112013-08-12 18:15:35 +0800379 struct sirfsoc_register *ureg = &sirfport->uart_reg->uart_reg;
380 struct sirfsoc_int_en *uint_en = &sirfport->uart_reg->uart_int_en;
381 unsigned long regv;
Barry Song909102d2013-08-07 13:35:38 +0800382 if (!sirfport->is_marco) {
Qipan Li5df83112013-08-12 18:15:35 +0800383 regv = rd_regl(port, ureg->sirfsoc_int_en_reg);
384 wr_regl(port, ureg->sirfsoc_int_en_reg, regv |
385 SIRFUART_RX_IO_INT_EN(port, uint_en));
386 } else
387 wr_regl(port, ureg->sirfsoc_int_en_reg,
388 SIRFUART_RX_IO_INT_EN(port, uint_en));
389 wr_regl(port, ureg->sirfsoc_rx_fifo_op, SIRFUART_FIFO_RESET);
390 wr_regl(port, ureg->sirfsoc_rx_fifo_op, 0);
391 wr_regl(port, ureg->sirfsoc_rx_fifo_op, SIRFUART_FIFO_START);
Rong Wang161e7732011-11-17 23:17:04 +0800392}
393
394static unsigned int
Qipan Li5df83112013-08-12 18:15:35 +0800395sirfsoc_usp_calc_sample_div(unsigned long set_rate,
396 unsigned long ioclk_rate, unsigned long *sample_reg)
397{
398 unsigned long min_delta = ~0UL;
399 unsigned short sample_div;
400 unsigned long ioclk_div = 0;
401 unsigned long temp_delta;
402
403 for (sample_div = SIRF_MIN_SAMPLE_DIV;
404 sample_div <= SIRF_MAX_SAMPLE_DIV; sample_div++) {
405 temp_delta = ioclk_rate -
406 (ioclk_rate + (set_rate * sample_div) / 2)
407 / (set_rate * sample_div) * set_rate * sample_div;
408
409 temp_delta = (temp_delta > 0) ? temp_delta : -temp_delta;
410 if (temp_delta < min_delta) {
411 ioclk_div = (2 * ioclk_rate /
412 (set_rate * sample_div) + 1) / 2 - 1;
413 if (ioclk_div > SIRF_IOCLK_DIV_MAX)
414 continue;
415 min_delta = temp_delta;
416 *sample_reg = sample_div;
417 if (!temp_delta)
418 break;
419 }
420 }
421 return ioclk_div;
422}
423
424static unsigned int
425sirfsoc_uart_calc_sample_div(unsigned long baud_rate,
426 unsigned long ioclk_rate, unsigned long *set_baud)
Rong Wang161e7732011-11-17 23:17:04 +0800427{
428 unsigned long min_delta = ~0UL;
429 unsigned short sample_div;
430 unsigned int regv = 0;
431 unsigned long ioclk_div;
432 unsigned long baud_tmp;
433 int temp_delta;
434
435 for (sample_div = SIRF_MIN_SAMPLE_DIV;
436 sample_div <= SIRF_MAX_SAMPLE_DIV; sample_div++) {
437 ioclk_div = (ioclk_rate / (baud_rate * (sample_div + 1))) - 1;
438 if (ioclk_div > SIRF_IOCLK_DIV_MAX)
439 continue;
440 baud_tmp = ioclk_rate / ((ioclk_div + 1) * (sample_div + 1));
441 temp_delta = baud_tmp - baud_rate;
442 temp_delta = (temp_delta > 0) ? temp_delta : -temp_delta;
443 if (temp_delta < min_delta) {
444 regv = regv & (~SIRF_IOCLK_DIV_MASK);
445 regv = regv | ioclk_div;
446 regv = regv & (~SIRF_SAMPLE_DIV_MASK);
447 regv = regv | (sample_div << SIRF_SAMPLE_DIV_SHIFT);
448 min_delta = temp_delta;
Qipan Li5df83112013-08-12 18:15:35 +0800449 *set_baud = baud_tmp;
Rong Wang161e7732011-11-17 23:17:04 +0800450 }
451 }
452 return regv;
453}
454
455static void sirfsoc_uart_set_termios(struct uart_port *port,
456 struct ktermios *termios,
457 struct ktermios *old)
458{
459 struct sirfsoc_uart_port *sirfport = to_sirfport(port);
Qipan Li5df83112013-08-12 18:15:35 +0800460 struct sirfsoc_register *ureg = &sirfport->uart_reg->uart_reg;
461 struct sirfsoc_int_en *uint_en = &sirfport->uart_reg->uart_int_en;
Rong Wang161e7732011-11-17 23:17:04 +0800462 unsigned long config_reg = 0;
463 unsigned long baud_rate;
Qipan Li5df83112013-08-12 18:15:35 +0800464 unsigned long set_baud;
Rong Wang161e7732011-11-17 23:17:04 +0800465 unsigned long flags;
466 unsigned long ic;
467 unsigned int clk_div_reg = 0;
Qipan Li5df83112013-08-12 18:15:35 +0800468 unsigned long temp_reg_val, ioclk_rate;
Rong Wang161e7732011-11-17 23:17:04 +0800469 unsigned long rx_time_out;
470 int threshold_div;
471 int temp;
Qipan Li5df83112013-08-12 18:15:35 +0800472 u32 data_bit_len, stop_bit_len, len_val;
473 unsigned long sample_div_reg = 0xf;
474 ioclk_rate = port->uartclk;
Rong Wang161e7732011-11-17 23:17:04 +0800475
Rong Wang161e7732011-11-17 23:17:04 +0800476 switch (termios->c_cflag & CSIZE) {
477 default:
478 case CS8:
Qipan Li5df83112013-08-12 18:15:35 +0800479 data_bit_len = 8;
Rong Wang161e7732011-11-17 23:17:04 +0800480 config_reg |= SIRFUART_DATA_BIT_LEN_8;
481 break;
482 case CS7:
Qipan Li5df83112013-08-12 18:15:35 +0800483 data_bit_len = 7;
Rong Wang161e7732011-11-17 23:17:04 +0800484 config_reg |= SIRFUART_DATA_BIT_LEN_7;
485 break;
486 case CS6:
Qipan Li5df83112013-08-12 18:15:35 +0800487 data_bit_len = 6;
Rong Wang161e7732011-11-17 23:17:04 +0800488 config_reg |= SIRFUART_DATA_BIT_LEN_6;
489 break;
490 case CS5:
Qipan Li5df83112013-08-12 18:15:35 +0800491 data_bit_len = 5;
Rong Wang161e7732011-11-17 23:17:04 +0800492 config_reg |= SIRFUART_DATA_BIT_LEN_5;
493 break;
494 }
Qipan Li5df83112013-08-12 18:15:35 +0800495 if (termios->c_cflag & CSTOPB) {
Rong Wang161e7732011-11-17 23:17:04 +0800496 config_reg |= SIRFUART_STOP_BIT_LEN_2;
Qipan Li5df83112013-08-12 18:15:35 +0800497 stop_bit_len = 2;
498 } else
499 stop_bit_len = 1;
500
Rong Wang161e7732011-11-17 23:17:04 +0800501 spin_lock_irqsave(&port->lock, flags);
Qipan Li5df83112013-08-12 18:15:35 +0800502 port->read_status_mask = uint_en->sirfsoc_rx_oflow_en;
Rong Wang161e7732011-11-17 23:17:04 +0800503 port->ignore_status_mask = 0;
Qipan Li5df83112013-08-12 18:15:35 +0800504 if (sirfport->uart_reg->uart_type == SIRF_REAL_UART) {
505 if (termios->c_iflag & INPCK)
506 port->read_status_mask |= uint_en->sirfsoc_frm_err_en |
507 uint_en->sirfsoc_parity_err_en;
508 }
509 if (sirfport->uart_reg->uart_type == SIRF_USP_UART) {
510 if (termios->c_iflag & INPCK)
511 port->read_status_mask |= uint_en->sirfsoc_frm_err_en;
512 }
Rong Wang161e7732011-11-17 23:17:04 +0800513 if (termios->c_iflag & (BRKINT | PARMRK))
Qipan Li5df83112013-08-12 18:15:35 +0800514 port->read_status_mask |= uint_en->sirfsoc_rxd_brk_en;
515 if (sirfport->uart_reg->uart_type == SIRF_REAL_UART) {
516 if (termios->c_iflag & IGNPAR)
517 port->ignore_status_mask |=
518 uint_en->sirfsoc_frm_err_en |
519 uint_en->sirfsoc_parity_err_en;
520 if (termios->c_cflag & PARENB) {
521 if (termios->c_cflag & CMSPAR) {
522 if (termios->c_cflag & PARODD)
523 config_reg |= SIRFUART_STICK_BIT_MARK;
524 else
525 config_reg |= SIRFUART_STICK_BIT_SPACE;
526 } else if (termios->c_cflag & PARODD) {
527 config_reg |= SIRFUART_STICK_BIT_ODD;
528 } else {
529 config_reg |= SIRFUART_STICK_BIT_EVEN;
530 }
Rong Wang161e7732011-11-17 23:17:04 +0800531 }
532 }
Qipan Li5df83112013-08-12 18:15:35 +0800533 if (sirfport->uart_reg->uart_type == SIRF_USP_UART) {
534 if (termios->c_iflag & IGNPAR)
535 port->ignore_status_mask |=
536 uint_en->sirfsoc_frm_err_en;
537 if (termios->c_cflag & PARENB)
538 dev_warn(port->dev,
539 "USP-UART not support parity err\n");
540 }
541 if (termios->c_iflag & IGNBRK) {
542 port->ignore_status_mask |=
543 uint_en->sirfsoc_rxd_brk_en;
544 if (termios->c_iflag & IGNPAR)
545 port->ignore_status_mask |=
546 uint_en->sirfsoc_rx_oflow_en;
547 }
548 if ((termios->c_cflag & CREAD) == 0)
549 port->ignore_status_mask |= SIRFUART_DUMMY_READ;
Rong Wang161e7732011-11-17 23:17:04 +0800550 /* Hardware Flow Control Settings */
551 if (UART_ENABLE_MS(port, termios->c_cflag)) {
552 if (!sirfport->ms_enabled)
553 sirfsoc_uart_enable_ms(port);
554 } else {
555 if (sirfport->ms_enabled)
556 sirfsoc_uart_disable_ms(port);
557 }
Qipan Li5df83112013-08-12 18:15:35 +0800558 baud_rate = uart_get_baud_rate(port, termios, old, 0, 4000000);
559 if (ioclk_rate == 150000000) {
Barry Songac4ce712013-01-16 14:49:27 +0800560 for (ic = 0; ic < SIRF_BAUD_RATE_SUPPORT_NR; ic++)
561 if (baud_rate == baudrate_to_regv[ic].baud_rate)
562 clk_div_reg = baudrate_to_regv[ic].reg_val;
563 }
Qipan Li5df83112013-08-12 18:15:35 +0800564 set_baud = baud_rate;
565 if (sirfport->uart_reg->uart_type == SIRF_REAL_UART) {
566 if (unlikely(clk_div_reg == 0))
567 clk_div_reg = sirfsoc_uart_calc_sample_div(baud_rate,
568 ioclk_rate, &set_baud);
569 wr_regl(port, ureg->sirfsoc_divisor, clk_div_reg);
570 }
571 if (sirfport->uart_reg->uart_type == SIRF_USP_UART) {
572 clk_div_reg = sirfsoc_usp_calc_sample_div(baud_rate,
573 ioclk_rate, &sample_div_reg);
574 sample_div_reg--;
575 set_baud = ((ioclk_rate / (clk_div_reg+1) - 1) /
576 (sample_div_reg + 1));
577 /* setting usp mode 2 */
578 len_val = ((1 << 0) | (1 << 8));
579 len_val |= ((clk_div_reg & 0x3ff) << 21);
580 wr_regl(port, ureg->sirfsoc_mode2,
581 len_val);
Barry Songac4ce712013-01-16 14:49:27 +0800582
Qipan Li5df83112013-08-12 18:15:35 +0800583 }
Rong Wang161e7732011-11-17 23:17:04 +0800584 if (tty_termios_baud_rate(termios))
Qipan Li5df83112013-08-12 18:15:35 +0800585 tty_termios_encode_baud_rate(termios, set_baud, set_baud);
586 /* set receive timeout && data bits len */
587 rx_time_out = SIRFSOC_UART_RX_TIMEOUT(set_baud, 20000);
588 rx_time_out = SIRFUART_RECV_TIMEOUT_VALUE(rx_time_out);
589 temp_reg_val = rd_regl(port, ureg->sirfsoc_tx_fifo_op);
590 wr_regl(port, ureg->sirfsoc_rx_fifo_op, 0);
591 wr_regl(port, ureg->sirfsoc_tx_fifo_op,
592 (temp_reg_val & ~SIRFUART_FIFO_START));
593 if (sirfport->uart_reg->uart_type == SIRF_REAL_UART) {
594 config_reg |= SIRFUART_RECV_TIMEOUT(port, rx_time_out);
595 wr_regl(port, ureg->sirfsoc_line_ctrl, config_reg);
596 }
597 if (sirfport->uart_reg->uart_type == SIRF_USP_UART) {
598 /*tx frame ctrl*/
599 len_val = (data_bit_len - 1) << 0;
600 len_val |= (data_bit_len + 1 + stop_bit_len - 1) << 16;
601 len_val |= ((data_bit_len - 1) << 24);
602 len_val |= (((clk_div_reg & 0xc00) >> 10) << 30);
603 wr_regl(port, ureg->sirfsoc_tx_frame_ctrl, len_val);
604 /*rx frame ctrl*/
605 len_val = (data_bit_len - 1) << 0;
606 len_val |= (data_bit_len + 1 + stop_bit_len - 1) << 8;
607 len_val |= (data_bit_len - 1) << 16;
608 len_val |= (((clk_div_reg & 0xf000) >> 12) << 24);
609 wr_regl(port, ureg->sirfsoc_rx_frame_ctrl, len_val);
610 /*async param*/
611 wr_regl(port, ureg->sirfsoc_async_param_reg,
612 (SIRFUART_RECV_TIMEOUT(port, rx_time_out)) |
613 (sample_div_reg & 0x3f) << 16);
614 }
615 wr_regl(port, ureg->sirfsoc_tx_dma_io_ctrl, SIRFUART_IO_MODE);
616 wr_regl(port, ureg->sirfsoc_rx_dma_io_ctrl, SIRFUART_IO_MODE);
Rong Wang161e7732011-11-17 23:17:04 +0800617 /* Reset Rx/Tx FIFO Threshold level for proper baudrate */
Qipan Li5df83112013-08-12 18:15:35 +0800618 if (set_baud < 1000000)
Rong Wang161e7732011-11-17 23:17:04 +0800619 threshold_div = 1;
620 else
621 threshold_div = 2;
Qipan Li5df83112013-08-12 18:15:35 +0800622 temp = SIRFUART_FIFO_THD(port);
623 wr_regl(port, ureg->sirfsoc_tx_fifo_ctrl, temp / threshold_div);
624 wr_regl(port, ureg->sirfsoc_rx_fifo_ctrl, temp / threshold_div);
625 temp_reg_val |= SIRFUART_FIFO_START;
626 wr_regl(port, ureg->sirfsoc_tx_fifo_op, temp_reg_val);
627 uart_update_timeout(port, termios->c_cflag, set_baud);
Rong Wang161e7732011-11-17 23:17:04 +0800628 sirfsoc_uart_start_rx(port);
Qipan Li5df83112013-08-12 18:15:35 +0800629 wr_regl(port, ureg->sirfsoc_tx_rx_en, SIRFUART_TX_EN | SIRFUART_RX_EN);
Rong Wang161e7732011-11-17 23:17:04 +0800630 spin_unlock_irqrestore(&port->lock, flags);
631}
632
633static void startup_uart_controller(struct uart_port *port)
634{
Qipan Li5df83112013-08-12 18:15:35 +0800635 struct sirfsoc_uart_port *sirfport = to_sirfport(port);
636 struct sirfsoc_register *ureg = &sirfport->uart_reg->uart_reg;
Rong Wang161e7732011-11-17 23:17:04 +0800637 unsigned long temp_regv;
638 int temp;
Qipan Li5df83112013-08-12 18:15:35 +0800639 temp_regv = rd_regl(port, ureg->sirfsoc_tx_dma_io_ctrl);
640 wr_regl(port, ureg->sirfsoc_tx_dma_io_ctrl, temp_regv |
641 SIRFUART_IO_MODE);
642 temp_regv = rd_regl(port, ureg->sirfsoc_rx_dma_io_ctrl);
643 wr_regl(port, ureg->sirfsoc_rx_dma_io_ctrl, temp_regv |
644 SIRFUART_IO_MODE);
645 wr_regl(port, ureg->sirfsoc_tx_dma_io_len, 0);
646 wr_regl(port, ureg->sirfsoc_rx_dma_io_len, 0);
647 wr_regl(port, ureg->sirfsoc_tx_rx_en, SIRFUART_RX_EN | SIRFUART_TX_EN);
648 if (sirfport->uart_reg->uart_type == SIRF_USP_UART)
649 wr_regl(port, ureg->sirfsoc_mode1,
650 SIRFSOC_USP_ENDIAN_CTRL_LSBF |
651 SIRFSOC_USP_EN);
652 wr_regl(port, ureg->sirfsoc_tx_fifo_op, SIRFUART_FIFO_RESET);
653 wr_regl(port, ureg->sirfsoc_tx_fifo_op, 0);
654 wr_regl(port, ureg->sirfsoc_rx_fifo_op, SIRFUART_FIFO_RESET);
655 wr_regl(port, ureg->sirfsoc_rx_fifo_op, 0);
656 temp = SIRFUART_FIFO_THD(port);
657 wr_regl(port, ureg->sirfsoc_tx_fifo_ctrl, temp);
658 wr_regl(port, ureg->sirfsoc_rx_fifo_ctrl, temp);
Rong Wang161e7732011-11-17 23:17:04 +0800659}
660
661static int sirfsoc_uart_startup(struct uart_port *port)
662{
663 struct sirfsoc_uart_port *sirfport = to_sirfport(port);
664 unsigned int index = port->line;
665 int ret;
666 set_irq_flags(port->irq, IRQF_VALID | IRQF_NOAUTOEN);
667 ret = request_irq(port->irq,
668 sirfsoc_uart_isr,
669 0,
670 SIRFUART_PORT_NAME,
671 sirfport);
672 if (ret != 0) {
673 dev_err(port->dev, "UART%d request IRQ line (%d) failed.\n",
674 index, port->irq);
675 goto irq_err;
676 }
677 startup_uart_controller(port);
678 enable_irq(port->irq);
679irq_err:
680 return ret;
681}
682
683static void sirfsoc_uart_shutdown(struct uart_port *port)
684{
685 struct sirfsoc_uart_port *sirfport = to_sirfport(port);
Qipan Li5df83112013-08-12 18:15:35 +0800686 struct sirfsoc_register *ureg = &sirfport->uart_reg->uart_reg;
Barry Song909102d2013-08-07 13:35:38 +0800687 if (!sirfport->is_marco)
Qipan Li5df83112013-08-12 18:15:35 +0800688 wr_regl(port, ureg->sirfsoc_int_en_reg, 0);
Barry Song909102d2013-08-07 13:35:38 +0800689 else
690 wr_regl(port, SIRFUART_INT_EN_CLR, ~0UL);
691
Rong Wang161e7732011-11-17 23:17:04 +0800692 free_irq(port->irq, sirfport);
693 if (sirfport->ms_enabled) {
694 sirfsoc_uart_disable_ms(port);
695 sirfport->ms_enabled = 0;
696 }
697}
698
699static const char *sirfsoc_uart_type(struct uart_port *port)
700{
701 return port->type == SIRFSOC_PORT_TYPE ? SIRFUART_PORT_NAME : NULL;
702}
703
704static int sirfsoc_uart_request_port(struct uart_port *port)
705{
Qipan Li5df83112013-08-12 18:15:35 +0800706 struct sirfsoc_uart_port *sirfport = to_sirfport(port);
707 struct sirfsoc_uart_param *uart_param = &sirfport->uart_reg->uart_param;
Rong Wang161e7732011-11-17 23:17:04 +0800708 void *ret;
709 ret = request_mem_region(port->mapbase,
Qipan Li5df83112013-08-12 18:15:35 +0800710 SIRFUART_MAP_SIZE, uart_param->port_name);
Rong Wang161e7732011-11-17 23:17:04 +0800711 return ret ? 0 : -EBUSY;
712}
713
714static void sirfsoc_uart_release_port(struct uart_port *port)
715{
716 release_mem_region(port->mapbase, SIRFUART_MAP_SIZE);
717}
718
719static void sirfsoc_uart_config_port(struct uart_port *port, int flags)
720{
721 if (flags & UART_CONFIG_TYPE) {
722 port->type = SIRFSOC_PORT_TYPE;
723 sirfsoc_uart_request_port(port);
724 }
725}
726
727static struct uart_ops sirfsoc_uart_ops = {
728 .tx_empty = sirfsoc_uart_tx_empty,
729 .get_mctrl = sirfsoc_uart_get_mctrl,
730 .set_mctrl = sirfsoc_uart_set_mctrl,
731 .stop_tx = sirfsoc_uart_stop_tx,
732 .start_tx = sirfsoc_uart_start_tx,
733 .stop_rx = sirfsoc_uart_stop_rx,
734 .enable_ms = sirfsoc_uart_enable_ms,
735 .break_ctl = sirfsoc_uart_break_ctl,
736 .startup = sirfsoc_uart_startup,
737 .shutdown = sirfsoc_uart_shutdown,
738 .set_termios = sirfsoc_uart_set_termios,
739 .type = sirfsoc_uart_type,
740 .release_port = sirfsoc_uart_release_port,
741 .request_port = sirfsoc_uart_request_port,
742 .config_port = sirfsoc_uart_config_port,
743};
744
745#ifdef CONFIG_SERIAL_SIRFSOC_CONSOLE
Qipan Li5df83112013-08-12 18:15:35 +0800746static int __init
747sirfsoc_uart_console_setup(struct console *co, char *options)
Rong Wang161e7732011-11-17 23:17:04 +0800748{
749 unsigned int baud = 115200;
750 unsigned int bits = 8;
751 unsigned int parity = 'n';
752 unsigned int flow = 'n';
753 struct uart_port *port = &sirfsoc_uart_ports[co->index].port;
Qipan Li5df83112013-08-12 18:15:35 +0800754 struct sirfsoc_uart_port *sirfport = to_sirfport(port);
755 struct sirfsoc_register *ureg = &sirfport->uart_reg->uart_reg;
Rong Wang161e7732011-11-17 23:17:04 +0800756 if (co->index < 0 || co->index >= SIRFSOC_UART_NR)
757 return -EINVAL;
758
759 if (!port->mapbase)
760 return -ENODEV;
761
Qipan Li5df83112013-08-12 18:15:35 +0800762 /* enable usp in mode1 register */
763 if (sirfport->uart_reg->uart_type == SIRF_USP_UART)
764 wr_regl(port, ureg->sirfsoc_mode1, SIRFSOC_USP_EN |
765 SIRFSOC_USP_ENDIAN_CTRL_LSBF);
Rong Wang161e7732011-11-17 23:17:04 +0800766 if (options)
767 uart_parse_options(options, &baud, &parity, &bits, &flow);
768 port->cons = co;
Qipan Li5df83112013-08-12 18:15:35 +0800769
Rong Wang161e7732011-11-17 23:17:04 +0800770 return uart_set_options(port, co, baud, parity, bits, flow);
771}
772
773static void sirfsoc_uart_console_putchar(struct uart_port *port, int ch)
774{
Qipan Li5df83112013-08-12 18:15:35 +0800775 struct sirfsoc_uart_port *sirfport = to_sirfport(port);
776 struct sirfsoc_register *ureg = &sirfport->uart_reg->uart_reg;
777 struct sirfsoc_fifo_status *ufifo_st = &sirfport->uart_reg->fifo_status;
Rong Wang161e7732011-11-17 23:17:04 +0800778 while (rd_regl(port,
Qipan Li5df83112013-08-12 18:15:35 +0800779 ureg->sirfsoc_tx_fifo_status) & ufifo_st->ff_full(port->line))
Rong Wang161e7732011-11-17 23:17:04 +0800780 cpu_relax();
Qipan Li5df83112013-08-12 18:15:35 +0800781 wr_regb(port, ureg->sirfsoc_tx_fifo_data, ch);
Rong Wang161e7732011-11-17 23:17:04 +0800782}
783
784static void sirfsoc_uart_console_write(struct console *co, const char *s,
785 unsigned int count)
786{
787 struct uart_port *port = &sirfsoc_uart_ports[co->index].port;
788 uart_console_write(port, s, count, sirfsoc_uart_console_putchar);
789}
790
791static struct console sirfsoc_uart_console = {
792 .name = SIRFSOC_UART_NAME,
793 .device = uart_console_device,
794 .flags = CON_PRINTBUFFER,
795 .index = -1,
796 .write = sirfsoc_uart_console_write,
797 .setup = sirfsoc_uart_console_setup,
798 .data = &sirfsoc_uart_drv,
799};
800
801static int __init sirfsoc_uart_console_init(void)
802{
803 register_console(&sirfsoc_uart_console);
804 return 0;
805}
806console_initcall(sirfsoc_uart_console_init);
807#endif
808
809static struct uart_driver sirfsoc_uart_drv = {
810 .owner = THIS_MODULE,
811 .driver_name = SIRFUART_PORT_NAME,
812 .nr = SIRFSOC_UART_NR,
813 .dev_name = SIRFSOC_UART_NAME,
814 .major = SIRFSOC_UART_MAJOR,
815 .minor = SIRFSOC_UART_MINOR,
816#ifdef CONFIG_SERIAL_SIRFSOC_CONSOLE
817 .cons = &sirfsoc_uart_console,
818#else
819 .cons = NULL,
820#endif
821};
822
Qipan Li5df83112013-08-12 18:15:35 +0800823static struct of_device_id sirfsoc_uart_ids[] = {
824 { .compatible = "sirf,prima2-uart", .data = &sirfsoc_uart,},
825 { .compatible = "sirf,marco-uart", .data = &sirfsoc_uart},
826 { .compatible = "sirf,prima2-usp-uart", .data = &sirfsoc_usp},
827 {}
828};
829MODULE_DEVICE_TABLE(of, sirfsoc_uart_ids);
830
Jingoo Hanada1f443d2013-08-08 17:41:43 +0900831static int sirfsoc_uart_probe(struct platform_device *pdev)
Rong Wang161e7732011-11-17 23:17:04 +0800832{
833 struct sirfsoc_uart_port *sirfport;
834 struct uart_port *port;
835 struct resource *res;
836 int ret;
Qipan Li5df83112013-08-12 18:15:35 +0800837 const struct of_device_id *match;
Rong Wang161e7732011-11-17 23:17:04 +0800838
Qipan Li5df83112013-08-12 18:15:35 +0800839 match = of_match_node(sirfsoc_uart_ids, pdev->dev.of_node);
Rong Wang161e7732011-11-17 23:17:04 +0800840 if (of_property_read_u32(pdev->dev.of_node, "cell-index", &pdev->id)) {
841 dev_err(&pdev->dev,
842 "Unable to find cell-index in uart node.\n");
843 ret = -EFAULT;
844 goto err;
845 }
Qipan Li5df83112013-08-12 18:15:35 +0800846 if (of_device_is_compatible(pdev->dev.of_node, "sirf,prima2-usp-uart"))
847 pdev->id += ((struct sirfsoc_uart_register *)
848 match->data)->uart_param.register_uart_nr;
Rong Wang161e7732011-11-17 23:17:04 +0800849 sirfport = &sirfsoc_uart_ports[pdev->id];
850 port = &sirfport->port;
851 port->dev = &pdev->dev;
852 port->private_data = sirfport;
Qipan Li5df83112013-08-12 18:15:35 +0800853 sirfport->uart_reg = (struct sirfsoc_uart_register *)match->data;
Rong Wang161e7732011-11-17 23:17:04 +0800854
Qipan Li5df83112013-08-12 18:15:35 +0800855 if (of_device_is_compatible(pdev->dev.of_node, "sirf,prima2-uart"))
856 sirfport->uart_reg->uart_type = SIRF_REAL_UART;
857 if (of_device_is_compatible(pdev->dev.of_node, "sirf,prima2-usp-uart"))
858 sirfport->uart_reg->uart_type = SIRF_USP_UART;
Barry Song909102d2013-08-07 13:35:38 +0800859 if (of_device_is_compatible(pdev->dev.of_node, "sirf,marco-uart"))
860 sirfport->is_marco = true;
861
Rong Wang161e7732011-11-17 23:17:04 +0800862 if (of_find_property(pdev->dev.of_node, "hw_flow_ctrl", NULL))
863 sirfport->hw_flow_ctrl = 1;
864
865 if (of_property_read_u32(pdev->dev.of_node,
866 "fifosize",
867 &port->fifosize)) {
868 dev_err(&pdev->dev,
869 "Unable to find fifosize in uart node.\n");
870 ret = -EFAULT;
871 goto err;
872 }
873
874 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
875 if (res == NULL) {
876 dev_err(&pdev->dev, "Insufficient resources.\n");
877 ret = -EFAULT;
878 goto err;
879 }
880 port->mapbase = res->start;
881 port->membase = devm_ioremap(&pdev->dev, res->start, resource_size(res));
882 if (!port->membase) {
883 dev_err(&pdev->dev, "Cannot remap resource.\n");
884 ret = -ENOMEM;
885 goto err;
886 }
887 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
888 if (res == NULL) {
889 dev_err(&pdev->dev, "Insufficient resources.\n");
890 ret = -EFAULT;
Julia Lawall9250dd52012-09-01 18:33:09 +0200891 goto err;
Rong Wang161e7732011-11-17 23:17:04 +0800892 }
893 port->irq = res->start;
894
Barry Songac4ce712013-01-16 14:49:27 +0800895 sirfport->clk = clk_get(&pdev->dev, NULL);
896 if (IS_ERR(sirfport->clk)) {
897 ret = PTR_ERR(sirfport->clk);
Barry Songa3437562013-08-15 06:52:14 +0800898 goto err;
Barry Songac4ce712013-01-16 14:49:27 +0800899 }
900 clk_prepare_enable(sirfport->clk);
901 port->uartclk = clk_get_rate(sirfport->clk);
902
Rong Wang161e7732011-11-17 23:17:04 +0800903 port->ops = &sirfsoc_uart_ops;
904 spin_lock_init(&port->lock);
905
906 platform_set_drvdata(pdev, sirfport);
907 ret = uart_add_one_port(&sirfsoc_uart_drv, port);
908 if (ret != 0) {
909 dev_err(&pdev->dev, "Cannot add UART port(%d).\n", pdev->id);
910 goto port_err;
911 }
912
913 return 0;
914
915port_err:
Barry Songac4ce712013-01-16 14:49:27 +0800916 clk_disable_unprepare(sirfport->clk);
917 clk_put(sirfport->clk);
Rong Wang161e7732011-11-17 23:17:04 +0800918err:
919 return ret;
920}
921
922static int sirfsoc_uart_remove(struct platform_device *pdev)
923{
924 struct sirfsoc_uart_port *sirfport = platform_get_drvdata(pdev);
925 struct uart_port *port = &sirfport->port;
Barry Songac4ce712013-01-16 14:49:27 +0800926 clk_disable_unprepare(sirfport->clk);
927 clk_put(sirfport->clk);
Rong Wang161e7732011-11-17 23:17:04 +0800928 uart_remove_one_port(&sirfsoc_uart_drv, port);
929 return 0;
930}
931
932static int
933sirfsoc_uart_suspend(struct platform_device *pdev, pm_message_t state)
934{
935 struct sirfsoc_uart_port *sirfport = platform_get_drvdata(pdev);
936 struct uart_port *port = &sirfport->port;
937 uart_suspend_port(&sirfsoc_uart_drv, port);
938 return 0;
939}
940
941static int sirfsoc_uart_resume(struct platform_device *pdev)
942{
943 struct sirfsoc_uart_port *sirfport = platform_get_drvdata(pdev);
944 struct uart_port *port = &sirfport->port;
945 uart_resume_port(&sirfsoc_uart_drv, port);
946 return 0;
947}
948
Rong Wang161e7732011-11-17 23:17:04 +0800949static struct platform_driver sirfsoc_uart_driver = {
950 .probe = sirfsoc_uart_probe,
Bill Pemberton2d47b712012-11-19 13:21:34 -0500951 .remove = sirfsoc_uart_remove,
Rong Wang161e7732011-11-17 23:17:04 +0800952 .suspend = sirfsoc_uart_suspend,
953 .resume = sirfsoc_uart_resume,
954 .driver = {
955 .name = SIRFUART_PORT_NAME,
956 .owner = THIS_MODULE,
957 .of_match_table = sirfsoc_uart_ids,
958 },
959};
960
961static int __init sirfsoc_uart_init(void)
962{
963 int ret = 0;
964
965 ret = uart_register_driver(&sirfsoc_uart_drv);
966 if (ret)
967 goto out;
968
969 ret = platform_driver_register(&sirfsoc_uart_driver);
970 if (ret)
971 uart_unregister_driver(&sirfsoc_uart_drv);
972out:
973 return ret;
974}
975module_init(sirfsoc_uart_init);
976
977static void __exit sirfsoc_uart_exit(void)
978{
979 platform_driver_unregister(&sirfsoc_uart_driver);
980 uart_unregister_driver(&sirfsoc_uart_drv);
981}
982module_exit(sirfsoc_uart_exit);
983
984MODULE_LICENSE("GPL v2");
985MODULE_AUTHOR("Bin Shi <Bin.Shi@csr.com>, Rong Wang<Rong.Wang@csr.com>");
986MODULE_DESCRIPTION("CSR SiRFprimaII Uart Driver");