blob: 8f3d6c091accb32266c94573da0d0d8096efa47c [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>
Linus Walleij5c9bdc32012-02-16 19:36:21 +010025#include <linux/pinctrl/consumer.h>
Rong Wang161e7732011-11-17 23:17:04 +080026
27#include "sirfsoc_uart.h"
28
29static unsigned int
30sirfsoc_uart_pio_tx_chars(struct sirfsoc_uart_port *sirfport, int count);
31static unsigned int
32sirfsoc_uart_pio_rx_chars(struct uart_port *port, unsigned int max_rx_count);
33static struct uart_driver sirfsoc_uart_drv;
34
35static const struct sirfsoc_baudrate_to_regv baudrate_to_regv[] = {
36 {4000000, 2359296},
37 {3500000, 1310721},
38 {3000000, 1572865},
39 {2500000, 1245186},
40 {2000000, 1572866},
41 {1500000, 1245188},
42 {1152000, 1638404},
43 {1000000, 1572869},
44 {921600, 1114120},
45 {576000, 1245196},
46 {500000, 1245198},
47 {460800, 1572876},
48 {230400, 1310750},
49 {115200, 1310781},
50 {57600, 1310843},
51 {38400, 1114328},
52 {19200, 1114545},
53 {9600, 1114979},
54};
55
56static struct sirfsoc_uart_port sirfsoc_uart_ports[SIRFSOC_UART_NR] = {
57 [0] = {
58 .port = {
59 .iotype = UPIO_MEM,
60 .flags = UPF_BOOT_AUTOCONF,
61 .line = 0,
62 },
63 },
64 [1] = {
65 .port = {
66 .iotype = UPIO_MEM,
67 .flags = UPF_BOOT_AUTOCONF,
68 .line = 1,
69 },
70 },
71 [2] = {
72 .port = {
73 .iotype = UPIO_MEM,
74 .flags = UPF_BOOT_AUTOCONF,
75 .line = 2,
76 },
77 },
Barry Song5425e032012-12-25 17:32:04 +080078 [3] = {
79 .port = {
80 .iotype = UPIO_MEM,
81 .flags = UPF_BOOT_AUTOCONF,
82 .line = 3,
83 },
84 },
85 [4] = {
86 .port = {
87 .iotype = UPIO_MEM,
88 .flags = UPF_BOOT_AUTOCONF,
89 .line = 4,
90 },
91 },
Rong Wang161e7732011-11-17 23:17:04 +080092};
93
94static inline struct sirfsoc_uart_port *to_sirfport(struct uart_port *port)
95{
96 return container_of(port, struct sirfsoc_uart_port, port);
97}
98
99static inline unsigned int sirfsoc_uart_tx_empty(struct uart_port *port)
100{
101 unsigned long reg;
102 reg = rd_regl(port, SIRFUART_TX_FIFO_STATUS);
103 if (reg & SIRFUART_FIFOEMPTY_MASK(port))
104 return TIOCSER_TEMT;
105 else
106 return 0;
107}
108
109static unsigned int sirfsoc_uart_get_mctrl(struct uart_port *port)
110{
111 struct sirfsoc_uart_port *sirfport = to_sirfport(port);
112 if (!(sirfport->ms_enabled)) {
113 goto cts_asserted;
114 } else if (sirfport->hw_flow_ctrl) {
115 if (!(rd_regl(port, SIRFUART_AFC_CTRL) &
116 SIRFUART_CTS_IN_STATUS))
117 goto cts_asserted;
118 else
119 goto cts_deasserted;
120 }
121cts_deasserted:
122 return TIOCM_CAR | TIOCM_DSR;
123cts_asserted:
124 return TIOCM_CAR | TIOCM_DSR | TIOCM_CTS;
125}
126
127static void sirfsoc_uart_set_mctrl(struct uart_port *port, unsigned int mctrl)
128{
129 struct sirfsoc_uart_port *sirfport = to_sirfport(port);
130 unsigned int assert = mctrl & TIOCM_RTS;
131 unsigned int val = assert ? SIRFUART_AFC_CTRL_RX_THD : 0x0;
132 unsigned int current_val;
133 if (sirfport->hw_flow_ctrl) {
134 current_val = rd_regl(port, SIRFUART_AFC_CTRL) & ~0xFF;
135 val |= current_val;
136 wr_regl(port, SIRFUART_AFC_CTRL, val);
137 }
138}
139
140static void sirfsoc_uart_stop_tx(struct uart_port *port)
141{
142 unsigned int regv;
143 regv = rd_regl(port, SIRFUART_INT_EN);
144 wr_regl(port, SIRFUART_INT_EN, regv & ~SIRFUART_TX_INT_EN);
145}
146
147void sirfsoc_uart_start_tx(struct uart_port *port)
148{
149 struct sirfsoc_uart_port *sirfport = to_sirfport(port);
150 unsigned long regv;
151 sirfsoc_uart_pio_tx_chars(sirfport, 1);
152 wr_regl(port, SIRFUART_TX_FIFO_OP, SIRFUART_TX_FIFO_START);
153 regv = rd_regl(port, SIRFUART_INT_EN);
154 wr_regl(port, SIRFUART_INT_EN, regv | SIRFUART_TX_INT_EN);
155}
156
157static void sirfsoc_uart_stop_rx(struct uart_port *port)
158{
159 unsigned long regv;
160 wr_regl(port, SIRFUART_RX_FIFO_OP, 0);
161 regv = rd_regl(port, SIRFUART_INT_EN);
162 wr_regl(port, SIRFUART_INT_EN, regv & ~SIRFUART_RX_IO_INT_EN);
163}
164
165static void sirfsoc_uart_disable_ms(struct uart_port *port)
166{
167 struct sirfsoc_uart_port *sirfport = to_sirfport(port);
168 unsigned long reg;
169 sirfport->ms_enabled = 0;
170 if (!sirfport->hw_flow_ctrl)
171 return;
172 reg = rd_regl(port, SIRFUART_AFC_CTRL);
173 wr_regl(port, SIRFUART_AFC_CTRL, reg & ~0x3FF);
174 reg = rd_regl(port, SIRFUART_INT_EN);
175 wr_regl(port, SIRFUART_INT_EN, reg & ~SIRFUART_CTS_INT_EN);
176}
177
178static void sirfsoc_uart_enable_ms(struct uart_port *port)
179{
180 struct sirfsoc_uart_port *sirfport = to_sirfport(port);
181 unsigned long reg;
182 unsigned long flg;
183 if (!sirfport->hw_flow_ctrl)
184 return;
185 flg = SIRFUART_AFC_RX_EN | SIRFUART_AFC_TX_EN;
186 reg = rd_regl(port, SIRFUART_AFC_CTRL);
187 wr_regl(port, SIRFUART_AFC_CTRL, reg | flg);
188 reg = rd_regl(port, SIRFUART_INT_EN);
189 wr_regl(port, SIRFUART_INT_EN, reg | SIRFUART_CTS_INT_EN);
190 uart_handle_cts_change(port,
191 !(rd_regl(port, SIRFUART_AFC_CTRL) & SIRFUART_CTS_IN_STATUS));
192 sirfport->ms_enabled = 1;
193}
194
195static void sirfsoc_uart_break_ctl(struct uart_port *port, int break_state)
196{
197 unsigned long ulcon = rd_regl(port, SIRFUART_LINE_CTRL);
198 if (break_state)
199 ulcon |= SIRFUART_SET_BREAK;
200 else
201 ulcon &= ~SIRFUART_SET_BREAK;
202 wr_regl(port, SIRFUART_LINE_CTRL, ulcon);
203}
204
205static unsigned int
206sirfsoc_uart_pio_rx_chars(struct uart_port *port, unsigned int max_rx_count)
207{
208 unsigned int ch, rx_count = 0;
Rong Wang161e7732011-11-17 23:17:04 +0800209
210 while (!(rd_regl(port, SIRFUART_RX_FIFO_STATUS) &
211 SIRFUART_FIFOEMPTY_MASK(port))) {
212 ch = rd_regl(port, SIRFUART_RX_FIFO_DATA) | SIRFUART_DUMMY_READ;
213 if (unlikely(uart_handle_sysrq_char(port, ch)))
214 continue;
215 uart_insert_char(port, 0, 0, ch, TTY_NORMAL);
216 rx_count++;
217 if (rx_count >= max_rx_count)
218 break;
219 }
220
221 port->icount.rx += rx_count;
Jiri Slaby2e124b42013-01-03 15:53:06 +0100222 tty_flip_buffer_push(&port->state->port);
Rong Wang161e7732011-11-17 23:17:04 +0800223
224 return rx_count;
225}
226
227static unsigned int
228sirfsoc_uart_pio_tx_chars(struct sirfsoc_uart_port *sirfport, int count)
229{
230 struct uart_port *port = &sirfport->port;
231 struct circ_buf *xmit = &port->state->xmit;
232 unsigned int num_tx = 0;
233 while (!uart_circ_empty(xmit) &&
234 !(rd_regl(port, SIRFUART_TX_FIFO_STATUS) &
235 SIRFUART_FIFOFULL_MASK(port)) &&
236 count--) {
237 wr_regl(port, SIRFUART_TX_FIFO_DATA, xmit->buf[xmit->tail]);
238 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
239 port->icount.tx++;
240 num_tx++;
241 }
242 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
243 uart_write_wakeup(port);
244 return num_tx;
245}
246
247static irqreturn_t sirfsoc_uart_isr(int irq, void *dev_id)
248{
249 unsigned long intr_status;
250 unsigned long cts_status;
251 unsigned long flag = TTY_NORMAL;
252 struct sirfsoc_uart_port *sirfport = (struct sirfsoc_uart_port *)dev_id;
253 struct uart_port *port = &sirfport->port;
254 struct uart_state *state = port->state;
255 struct circ_buf *xmit = &port->state->xmit;
Barry Song5425e032012-12-25 17:32:04 +0800256 spin_lock(&port->lock);
Rong Wang161e7732011-11-17 23:17:04 +0800257 intr_status = rd_regl(port, SIRFUART_INT_STATUS);
258 wr_regl(port, SIRFUART_INT_STATUS, intr_status);
259 intr_status &= rd_regl(port, SIRFUART_INT_EN);
260 if (unlikely(intr_status & (SIRFUART_ERR_INT_STAT))) {
261 if (intr_status & SIRFUART_RXD_BREAK) {
262 if (uart_handle_break(port))
263 goto recv_char;
264 uart_insert_char(port, intr_status,
265 SIRFUART_RX_OFLOW, 0, TTY_BREAK);
Barry Song5425e032012-12-25 17:32:04 +0800266 spin_unlock(&port->lock);
Rong Wang161e7732011-11-17 23:17:04 +0800267 return IRQ_HANDLED;
268 }
269 if (intr_status & SIRFUART_RX_OFLOW)
270 port->icount.overrun++;
271 if (intr_status & SIRFUART_FRM_ERR) {
272 port->icount.frame++;
273 flag = TTY_FRAME;
274 }
275 if (intr_status & SIRFUART_PARITY_ERR)
276 flag = TTY_PARITY;
277 wr_regl(port, SIRFUART_RX_FIFO_OP, SIRFUART_RX_FIFO_RESET);
278 wr_regl(port, SIRFUART_RX_FIFO_OP, 0);
279 wr_regl(port, SIRFUART_RX_FIFO_OP, SIRFUART_RX_FIFO_START);
280 intr_status &= port->read_status_mask;
281 uart_insert_char(port, intr_status,
282 SIRFUART_RX_OFLOW_INT, 0, flag);
283 }
284recv_char:
285 if (intr_status & SIRFUART_CTS_INT_EN) {
286 cts_status = !(rd_regl(port, SIRFUART_AFC_CTRL) &
287 SIRFUART_CTS_IN_STATUS);
288 if (cts_status != 0) {
289 uart_handle_cts_change(port, 1);
290 } else {
291 uart_handle_cts_change(port, 0);
292 wake_up_interruptible(&state->port.delta_msr_wait);
293 }
294 }
295 if (intr_status & SIRFUART_RX_IO_INT_EN)
296 sirfsoc_uart_pio_rx_chars(port, SIRFSOC_UART_IO_RX_MAX_CNT);
297 if (intr_status & SIRFUART_TX_INT_EN) {
298 if (uart_circ_empty(xmit) || uart_tx_stopped(port)) {
Barry Song5425e032012-12-25 17:32:04 +0800299 spin_unlock(&port->lock);
Rong Wang161e7732011-11-17 23:17:04 +0800300 return IRQ_HANDLED;
301 } else {
302 sirfsoc_uart_pio_tx_chars(sirfport,
303 SIRFSOC_UART_IO_TX_REASONABLE_CNT);
304 if ((uart_circ_empty(xmit)) &&
305 (rd_regl(port, SIRFUART_TX_FIFO_STATUS) &
306 SIRFUART_FIFOEMPTY_MASK(port)))
307 sirfsoc_uart_stop_tx(port);
308 }
309 }
Barry Song5425e032012-12-25 17:32:04 +0800310 spin_unlock(&port->lock);
Rong Wang161e7732011-11-17 23:17:04 +0800311 return IRQ_HANDLED;
312}
313
314static void sirfsoc_uart_start_rx(struct uart_port *port)
315{
316 unsigned long regv;
317 regv = rd_regl(port, SIRFUART_INT_EN);
318 wr_regl(port, SIRFUART_INT_EN, regv | SIRFUART_RX_IO_INT_EN);
319 wr_regl(port, SIRFUART_RX_FIFO_OP, SIRFUART_RX_FIFO_RESET);
320 wr_regl(port, SIRFUART_RX_FIFO_OP, 0);
321 wr_regl(port, SIRFUART_RX_FIFO_OP, SIRFUART_RX_FIFO_START);
322}
323
324static unsigned int
325sirfsoc_calc_sample_div(unsigned long baud_rate,
326 unsigned long ioclk_rate, unsigned long *setted_baud)
327{
328 unsigned long min_delta = ~0UL;
329 unsigned short sample_div;
330 unsigned int regv = 0;
331 unsigned long ioclk_div;
332 unsigned long baud_tmp;
333 int temp_delta;
334
335 for (sample_div = SIRF_MIN_SAMPLE_DIV;
336 sample_div <= SIRF_MAX_SAMPLE_DIV; sample_div++) {
337 ioclk_div = (ioclk_rate / (baud_rate * (sample_div + 1))) - 1;
338 if (ioclk_div > SIRF_IOCLK_DIV_MAX)
339 continue;
340 baud_tmp = ioclk_rate / ((ioclk_div + 1) * (sample_div + 1));
341 temp_delta = baud_tmp - baud_rate;
342 temp_delta = (temp_delta > 0) ? temp_delta : -temp_delta;
343 if (temp_delta < min_delta) {
344 regv = regv & (~SIRF_IOCLK_DIV_MASK);
345 regv = regv | ioclk_div;
346 regv = regv & (~SIRF_SAMPLE_DIV_MASK);
347 regv = regv | (sample_div << SIRF_SAMPLE_DIV_SHIFT);
348 min_delta = temp_delta;
349 *setted_baud = baud_tmp;
350 }
351 }
352 return regv;
353}
354
355static void sirfsoc_uart_set_termios(struct uart_port *port,
356 struct ktermios *termios,
357 struct ktermios *old)
358{
359 struct sirfsoc_uart_port *sirfport = to_sirfport(port);
360 unsigned long ioclk_rate;
361 unsigned long config_reg = 0;
362 unsigned long baud_rate;
363 unsigned long setted_baud;
364 unsigned long flags;
365 unsigned long ic;
366 unsigned int clk_div_reg = 0;
367 unsigned long temp_reg_val;
368 unsigned long rx_time_out;
369 int threshold_div;
370 int temp;
371
372 ioclk_rate = 150000000;
373 switch (termios->c_cflag & CSIZE) {
374 default:
375 case CS8:
376 config_reg |= SIRFUART_DATA_BIT_LEN_8;
377 break;
378 case CS7:
379 config_reg |= SIRFUART_DATA_BIT_LEN_7;
380 break;
381 case CS6:
382 config_reg |= SIRFUART_DATA_BIT_LEN_6;
383 break;
384 case CS5:
385 config_reg |= SIRFUART_DATA_BIT_LEN_5;
386 break;
387 }
388 if (termios->c_cflag & CSTOPB)
389 config_reg |= SIRFUART_STOP_BIT_LEN_2;
390 baud_rate = uart_get_baud_rate(port, termios, old, 0, 4000000);
391 spin_lock_irqsave(&port->lock, flags);
392 port->read_status_mask = SIRFUART_RX_OFLOW_INT;
393 port->ignore_status_mask = 0;
394 /* read flags */
395 if (termios->c_iflag & INPCK)
396 port->read_status_mask |=
397 SIRFUART_FRM_ERR_INT | SIRFUART_PARITY_ERR_INT;
398 if (termios->c_iflag & (BRKINT | PARMRK))
399 port->read_status_mask |= SIRFUART_RXD_BREAK_INT;
400 /* ignore flags */
401 if (termios->c_iflag & IGNPAR)
402 port->ignore_status_mask |=
403 SIRFUART_FRM_ERR_INT | SIRFUART_PARITY_ERR_INT;
404 if ((termios->c_cflag & CREAD) == 0)
405 port->ignore_status_mask |= SIRFUART_DUMMY_READ;
406 /* enable parity if PARENB is set*/
407 if (termios->c_cflag & PARENB) {
408 if (termios->c_cflag & CMSPAR) {
409 if (termios->c_cflag & PARODD)
410 config_reg |= SIRFUART_STICK_BIT_MARK;
411 else
412 config_reg |= SIRFUART_STICK_BIT_SPACE;
413 } else if (termios->c_cflag & PARODD) {
414 config_reg |= SIRFUART_STICK_BIT_ODD;
415 } else {
416 config_reg |= SIRFUART_STICK_BIT_EVEN;
417 }
418 }
419 /* Hardware Flow Control Settings */
420 if (UART_ENABLE_MS(port, termios->c_cflag)) {
421 if (!sirfport->ms_enabled)
422 sirfsoc_uart_enable_ms(port);
423 } else {
424 if (sirfport->ms_enabled)
425 sirfsoc_uart_disable_ms(port);
426 }
427
428 /* common rate: fast calculation */
429 for (ic = 0; ic < SIRF_BAUD_RATE_SUPPORT_NR; ic++)
430 if (baud_rate == baudrate_to_regv[ic].baud_rate)
431 clk_div_reg = baudrate_to_regv[ic].reg_val;
432 setted_baud = baud_rate;
433 /* arbitary rate setting */
434 if (unlikely(clk_div_reg == 0))
435 clk_div_reg = sirfsoc_calc_sample_div(baud_rate, ioclk_rate,
436 &setted_baud);
437 wr_regl(port, SIRFUART_DIVISOR, clk_div_reg);
438
439 if (tty_termios_baud_rate(termios))
440 tty_termios_encode_baud_rate(termios, setted_baud, setted_baud);
441
442 /* set receive timeout */
443 rx_time_out = SIRFSOC_UART_RX_TIMEOUT(baud_rate, 20000);
444 rx_time_out = (rx_time_out > 0xFFFF) ? 0xFFFF : rx_time_out;
445 config_reg |= SIRFUART_RECV_TIMEOUT(rx_time_out);
446 temp_reg_val = rd_regl(port, SIRFUART_TX_FIFO_OP);
447 wr_regl(port, SIRFUART_RX_FIFO_OP, 0);
448 wr_regl(port, SIRFUART_TX_FIFO_OP,
449 temp_reg_val & ~SIRFUART_TX_FIFO_START);
450 wr_regl(port, SIRFUART_TX_DMA_IO_CTRL, SIRFUART_TX_MODE_IO);
451 wr_regl(port, SIRFUART_RX_DMA_IO_CTRL, SIRFUART_RX_MODE_IO);
452 wr_regl(port, SIRFUART_LINE_CTRL, config_reg);
453
454 /* Reset Rx/Tx FIFO Threshold level for proper baudrate */
455 if (baud_rate < 1000000)
456 threshold_div = 1;
457 else
458 threshold_div = 2;
459 temp = port->line == 1 ? 16 : 64;
460 wr_regl(port, SIRFUART_TX_FIFO_CTRL, temp / threshold_div);
461 wr_regl(port, SIRFUART_RX_FIFO_CTRL, temp / threshold_div);
462 temp_reg_val |= SIRFUART_TX_FIFO_START;
463 wr_regl(port, SIRFUART_TX_FIFO_OP, temp_reg_val);
464 uart_update_timeout(port, termios->c_cflag, baud_rate);
465 sirfsoc_uart_start_rx(port);
466 wr_regl(port, SIRFUART_TX_RX_EN, SIRFUART_TX_EN | SIRFUART_RX_EN);
467 spin_unlock_irqrestore(&port->lock, flags);
468}
469
470static void startup_uart_controller(struct uart_port *port)
471{
472 unsigned long temp_regv;
473 int temp;
474 temp_regv = rd_regl(port, SIRFUART_TX_DMA_IO_CTRL);
475 wr_regl(port, SIRFUART_TX_DMA_IO_CTRL, temp_regv | SIRFUART_TX_MODE_IO);
476 temp_regv = rd_regl(port, SIRFUART_RX_DMA_IO_CTRL);
477 wr_regl(port, SIRFUART_RX_DMA_IO_CTRL, temp_regv | SIRFUART_RX_MODE_IO);
478 wr_regl(port, SIRFUART_TX_DMA_IO_LEN, 0);
479 wr_regl(port, SIRFUART_RX_DMA_IO_LEN, 0);
480 wr_regl(port, SIRFUART_TX_RX_EN, SIRFUART_RX_EN | SIRFUART_TX_EN);
481 wr_regl(port, SIRFUART_TX_FIFO_OP, SIRFUART_TX_FIFO_RESET);
482 wr_regl(port, SIRFUART_TX_FIFO_OP, 0);
483 wr_regl(port, SIRFUART_RX_FIFO_OP, SIRFUART_RX_FIFO_RESET);
484 wr_regl(port, SIRFUART_RX_FIFO_OP, 0);
485 temp = port->line == 1 ? 16 : 64;
486 wr_regl(port, SIRFUART_TX_FIFO_CTRL, temp);
487 wr_regl(port, SIRFUART_RX_FIFO_CTRL, temp);
488}
489
490static int sirfsoc_uart_startup(struct uart_port *port)
491{
492 struct sirfsoc_uart_port *sirfport = to_sirfport(port);
493 unsigned int index = port->line;
494 int ret;
495 set_irq_flags(port->irq, IRQF_VALID | IRQF_NOAUTOEN);
496 ret = request_irq(port->irq,
497 sirfsoc_uart_isr,
498 0,
499 SIRFUART_PORT_NAME,
500 sirfport);
501 if (ret != 0) {
502 dev_err(port->dev, "UART%d request IRQ line (%d) failed.\n",
503 index, port->irq);
504 goto irq_err;
505 }
506 startup_uart_controller(port);
507 enable_irq(port->irq);
508irq_err:
509 return ret;
510}
511
512static void sirfsoc_uart_shutdown(struct uart_port *port)
513{
514 struct sirfsoc_uart_port *sirfport = to_sirfport(port);
515 wr_regl(port, SIRFUART_INT_EN, 0);
516 free_irq(port->irq, sirfport);
517 if (sirfport->ms_enabled) {
518 sirfsoc_uart_disable_ms(port);
519 sirfport->ms_enabled = 0;
520 }
521}
522
523static const char *sirfsoc_uart_type(struct uart_port *port)
524{
525 return port->type == SIRFSOC_PORT_TYPE ? SIRFUART_PORT_NAME : NULL;
526}
527
528static int sirfsoc_uart_request_port(struct uart_port *port)
529{
530 void *ret;
531 ret = request_mem_region(port->mapbase,
532 SIRFUART_MAP_SIZE, SIRFUART_PORT_NAME);
533 return ret ? 0 : -EBUSY;
534}
535
536static void sirfsoc_uart_release_port(struct uart_port *port)
537{
538 release_mem_region(port->mapbase, SIRFUART_MAP_SIZE);
539}
540
541static void sirfsoc_uart_config_port(struct uart_port *port, int flags)
542{
543 if (flags & UART_CONFIG_TYPE) {
544 port->type = SIRFSOC_PORT_TYPE;
545 sirfsoc_uart_request_port(port);
546 }
547}
548
549static struct uart_ops sirfsoc_uart_ops = {
550 .tx_empty = sirfsoc_uart_tx_empty,
551 .get_mctrl = sirfsoc_uart_get_mctrl,
552 .set_mctrl = sirfsoc_uart_set_mctrl,
553 .stop_tx = sirfsoc_uart_stop_tx,
554 .start_tx = sirfsoc_uart_start_tx,
555 .stop_rx = sirfsoc_uart_stop_rx,
556 .enable_ms = sirfsoc_uart_enable_ms,
557 .break_ctl = sirfsoc_uart_break_ctl,
558 .startup = sirfsoc_uart_startup,
559 .shutdown = sirfsoc_uart_shutdown,
560 .set_termios = sirfsoc_uart_set_termios,
561 .type = sirfsoc_uart_type,
562 .release_port = sirfsoc_uart_release_port,
563 .request_port = sirfsoc_uart_request_port,
564 .config_port = sirfsoc_uart_config_port,
565};
566
567#ifdef CONFIG_SERIAL_SIRFSOC_CONSOLE
568static int __init sirfsoc_uart_console_setup(struct console *co, char *options)
569{
570 unsigned int baud = 115200;
571 unsigned int bits = 8;
572 unsigned int parity = 'n';
573 unsigned int flow = 'n';
574 struct uart_port *port = &sirfsoc_uart_ports[co->index].port;
575
576 if (co->index < 0 || co->index >= SIRFSOC_UART_NR)
577 return -EINVAL;
578
579 if (!port->mapbase)
580 return -ENODEV;
581
582 if (options)
583 uart_parse_options(options, &baud, &parity, &bits, &flow);
584 port->cons = co;
585 return uart_set_options(port, co, baud, parity, bits, flow);
586}
587
588static void sirfsoc_uart_console_putchar(struct uart_port *port, int ch)
589{
590 while (rd_regl(port,
591 SIRFUART_TX_FIFO_STATUS) & SIRFUART_FIFOFULL_MASK(port))
592 cpu_relax();
593 wr_regb(port, SIRFUART_TX_FIFO_DATA, ch);
594}
595
596static void sirfsoc_uart_console_write(struct console *co, const char *s,
597 unsigned int count)
598{
599 struct uart_port *port = &sirfsoc_uart_ports[co->index].port;
600 uart_console_write(port, s, count, sirfsoc_uart_console_putchar);
601}
602
603static struct console sirfsoc_uart_console = {
604 .name = SIRFSOC_UART_NAME,
605 .device = uart_console_device,
606 .flags = CON_PRINTBUFFER,
607 .index = -1,
608 .write = sirfsoc_uart_console_write,
609 .setup = sirfsoc_uart_console_setup,
610 .data = &sirfsoc_uart_drv,
611};
612
613static int __init sirfsoc_uart_console_init(void)
614{
615 register_console(&sirfsoc_uart_console);
616 return 0;
617}
618console_initcall(sirfsoc_uart_console_init);
619#endif
620
621static struct uart_driver sirfsoc_uart_drv = {
622 .owner = THIS_MODULE,
623 .driver_name = SIRFUART_PORT_NAME,
624 .nr = SIRFSOC_UART_NR,
625 .dev_name = SIRFSOC_UART_NAME,
626 .major = SIRFSOC_UART_MAJOR,
627 .minor = SIRFSOC_UART_MINOR,
628#ifdef CONFIG_SERIAL_SIRFSOC_CONSOLE
629 .cons = &sirfsoc_uart_console,
630#else
631 .cons = NULL,
632#endif
633};
634
635int sirfsoc_uart_probe(struct platform_device *pdev)
636{
637 struct sirfsoc_uart_port *sirfport;
638 struct uart_port *port;
639 struct resource *res;
640 int ret;
641
642 if (of_property_read_u32(pdev->dev.of_node, "cell-index", &pdev->id)) {
643 dev_err(&pdev->dev,
644 "Unable to find cell-index in uart node.\n");
645 ret = -EFAULT;
646 goto err;
647 }
648
649 sirfport = &sirfsoc_uart_ports[pdev->id];
650 port = &sirfport->port;
651 port->dev = &pdev->dev;
652 port->private_data = sirfport;
653
654 if (of_find_property(pdev->dev.of_node, "hw_flow_ctrl", NULL))
655 sirfport->hw_flow_ctrl = 1;
656
657 if (of_property_read_u32(pdev->dev.of_node,
658 "fifosize",
659 &port->fifosize)) {
660 dev_err(&pdev->dev,
661 "Unable to find fifosize in uart node.\n");
662 ret = -EFAULT;
663 goto err;
664 }
665
666 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
667 if (res == NULL) {
668 dev_err(&pdev->dev, "Insufficient resources.\n");
669 ret = -EFAULT;
670 goto err;
671 }
672 port->mapbase = res->start;
673 port->membase = devm_ioremap(&pdev->dev, res->start, resource_size(res));
674 if (!port->membase) {
675 dev_err(&pdev->dev, "Cannot remap resource.\n");
676 ret = -ENOMEM;
677 goto err;
678 }
679 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
680 if (res == NULL) {
681 dev_err(&pdev->dev, "Insufficient resources.\n");
682 ret = -EFAULT;
Julia Lawall9250dd52012-09-01 18:33:09 +0200683 goto err;
Rong Wang161e7732011-11-17 23:17:04 +0800684 }
685 port->irq = res->start;
686
687 if (sirfport->hw_flow_ctrl) {
Stephen Warren6e5e9592012-03-02 13:05:47 -0700688 sirfport->p = pinctrl_get_select_default(&pdev->dev);
Linus Walleij5c9bdc32012-02-16 19:36:21 +0100689 ret = IS_ERR(sirfport->p);
Rong Wang161e7732011-11-17 23:17:04 +0800690 if (ret)
Julia Lawall9250dd52012-09-01 18:33:09 +0200691 goto err;
Rong Wang161e7732011-11-17 23:17:04 +0800692 }
693
694 port->ops = &sirfsoc_uart_ops;
695 spin_lock_init(&port->lock);
696
697 platform_set_drvdata(pdev, sirfport);
698 ret = uart_add_one_port(&sirfsoc_uart_drv, port);
699 if (ret != 0) {
700 dev_err(&pdev->dev, "Cannot add UART port(%d).\n", pdev->id);
701 goto port_err;
702 }
703
704 return 0;
705
706port_err:
707 platform_set_drvdata(pdev, NULL);
Stephen Warren6e5e9592012-03-02 13:05:47 -0700708 if (sirfport->hw_flow_ctrl)
Linus Walleij5c9bdc32012-02-16 19:36:21 +0100709 pinctrl_put(sirfport->p);
Rong Wang161e7732011-11-17 23:17:04 +0800710err:
711 return ret;
712}
713
714static int sirfsoc_uart_remove(struct platform_device *pdev)
715{
716 struct sirfsoc_uart_port *sirfport = platform_get_drvdata(pdev);
717 struct uart_port *port = &sirfport->port;
718 platform_set_drvdata(pdev, NULL);
Stephen Warren6e5e9592012-03-02 13:05:47 -0700719 if (sirfport->hw_flow_ctrl)
Linus Walleij5c9bdc32012-02-16 19:36:21 +0100720 pinctrl_put(sirfport->p);
Rong Wang161e7732011-11-17 23:17:04 +0800721 uart_remove_one_port(&sirfsoc_uart_drv, port);
722 return 0;
723}
724
725static int
726sirfsoc_uart_suspend(struct platform_device *pdev, pm_message_t state)
727{
728 struct sirfsoc_uart_port *sirfport = platform_get_drvdata(pdev);
729 struct uart_port *port = &sirfport->port;
730 uart_suspend_port(&sirfsoc_uart_drv, port);
731 return 0;
732}
733
734static int sirfsoc_uart_resume(struct platform_device *pdev)
735{
736 struct sirfsoc_uart_port *sirfport = platform_get_drvdata(pdev);
737 struct uart_port *port = &sirfport->port;
738 uart_resume_port(&sirfsoc_uart_drv, port);
739 return 0;
740}
741
Bill Pembertonde88b342012-11-19 13:24:32 -0500742static struct of_device_id sirfsoc_uart_ids[] = {
Rong Wang161e7732011-11-17 23:17:04 +0800743 { .compatible = "sirf,prima2-uart", },
Barry Song5425e032012-12-25 17:32:04 +0800744 { .compatible = "sirf,marco-uart", },
Rong Wang161e7732011-11-17 23:17:04 +0800745 {}
746};
747MODULE_DEVICE_TABLE(of, sirfsoc_serial_of_match);
748
749static struct platform_driver sirfsoc_uart_driver = {
750 .probe = sirfsoc_uart_probe,
Bill Pemberton2d47b712012-11-19 13:21:34 -0500751 .remove = sirfsoc_uart_remove,
Rong Wang161e7732011-11-17 23:17:04 +0800752 .suspend = sirfsoc_uart_suspend,
753 .resume = sirfsoc_uart_resume,
754 .driver = {
755 .name = SIRFUART_PORT_NAME,
756 .owner = THIS_MODULE,
757 .of_match_table = sirfsoc_uart_ids,
758 },
759};
760
761static int __init sirfsoc_uart_init(void)
762{
763 int ret = 0;
764
765 ret = uart_register_driver(&sirfsoc_uart_drv);
766 if (ret)
767 goto out;
768
769 ret = platform_driver_register(&sirfsoc_uart_driver);
770 if (ret)
771 uart_unregister_driver(&sirfsoc_uart_drv);
772out:
773 return ret;
774}
775module_init(sirfsoc_uart_init);
776
777static void __exit sirfsoc_uart_exit(void)
778{
779 platform_driver_unregister(&sirfsoc_uart_driver);
780 uart_unregister_driver(&sirfsoc_uart_drv);
781}
782module_exit(sirfsoc_uart_exit);
783
784MODULE_LICENSE("GPL v2");
785MODULE_AUTHOR("Bin Shi <Bin.Shi@csr.com>, Rong Wang<Rong.Wang@csr.com>");
786MODULE_DESCRIPTION("CSR SiRFprimaII Uart Driver");