1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/module.h>
3 #include <linux/init.h>
4 #include <linux/console.h>
5 #include <linux/platform_device.h>
6 #include <linux/serial_core.h>
7 #include <linux/tty_flip.h>
9 #include <linux/gpio.h>
10 #include <linux/of_irq.h>
11 #include <linux/of_address.h>
12 #include <hwregs/ser_defs.h>
14 #include "serial_mctrl_gpio.h"
16 #define DRV_NAME "etraxfs-uart"
17 #define UART_NR CONFIG_ETRAX_SERIAL_PORTS
19 #define MODIFY_REG(instance, reg, var) \
21 if (REG_RD_INT(ser, instance, reg) != \
22 REG_TYPE_CONV(int, reg_ser_##reg, var)) \
23 REG_WR(ser, instance, reg, var); \
26 struct uart_cris_port
{
27 struct uart_port port
;
32 void __iomem
*regi_ser
;
34 struct mctrl_gpios
*gpios
;
39 static struct uart_driver etraxfs_uart_driver
;
40 static struct uart_port
*console_port
;
41 static int console_baud
= 115200;
42 static struct uart_cris_port
*etraxfs_uart_ports
[UART_NR
];
44 static void cris_serial_port_init(struct uart_port
*port
, int line
);
45 static void etraxfs_uart_stop_rx(struct uart_port
*port
);
46 static inline void etraxfs_uart_start_tx_bottom(struct uart_port
*port
);
48 #ifdef CONFIG_SERIAL_ETRAXFS_CONSOLE
50 cris_console_write(struct console
*co
, const char *s
, unsigned int count
)
52 struct uart_cris_port
*up
;
54 reg_ser_r_stat_din stat
;
55 reg_ser_rw_tr_dma_en tr_dma_en
, old
;
57 up
= etraxfs_uart_ports
[co
->index
];
62 /* Switch to manual mode. */
63 tr_dma_en
= old
= REG_RD(ser
, up
->regi_ser
, rw_tr_dma_en
);
64 if (tr_dma_en
.en
== regk_ser_yes
) {
65 tr_dma_en
.en
= regk_ser_no
;
66 REG_WR(ser
, up
->regi_ser
, rw_tr_dma_en
, tr_dma_en
);
70 for (i
= 0; i
< count
; i
++) {
74 stat
= REG_RD(ser
, up
->regi_ser
, r_stat_din
);
75 } while (!stat
.tr_rdy
);
76 REG_WR_INT(ser
, up
->regi_ser
, rw_dout
, '\r');
78 /* Wait until transmitter is ready and send. */
80 stat
= REG_RD(ser
, up
->regi_ser
, r_stat_din
);
81 } while (!stat
.tr_rdy
);
82 REG_WR_INT(ser
, up
->regi_ser
, rw_dout
, s
[i
]);
86 if (tr_dma_en
.en
!= old
.en
)
87 REG_WR(ser
, up
->regi_ser
, rw_tr_dma_en
, old
);
91 cris_console_setup(struct console
*co
, char *options
)
93 struct uart_port
*port
;
99 if (co
->index
< 0 || co
->index
>= UART_NR
)
101 port
= &etraxfs_uart_ports
[co
->index
]->port
;
104 co
->flags
|= CON_CONSDEV
;
107 uart_parse_options(options
, &baud
, &parity
, &bits
, &flow
);
109 cris_serial_port_init(port
, co
->index
);
110 uart_set_options(port
, co
, baud
, parity
, bits
, flow
);
115 static struct console cris_console
= {
117 .write
= cris_console_write
,
118 .device
= uart_console_device
,
119 .setup
= cris_console_setup
,
120 .flags
= CON_PRINTBUFFER
,
122 .data
= &etraxfs_uart_driver
,
124 #endif /* CONFIG_SERIAL_ETRAXFS_CONSOLE */
126 static struct uart_driver etraxfs_uart_driver
= {
127 .owner
= THIS_MODULE
,
128 .driver_name
= "serial",
133 #ifdef CONFIG_SERIAL_ETRAXFS_CONSOLE
134 .cons
= &cris_console
,
135 #endif /* CONFIG_SERIAL_ETRAXFS_CONSOLE */
138 static inline int crisv32_serial_get_rts(struct uart_cris_port
*up
)
140 void __iomem
*regi_ser
= up
->regi_ser
;
142 * Return what the user has controlled rts to or
143 * what the pin is? (if auto_rts is used it differs during tx)
145 reg_ser_r_stat_din rstat
= REG_RD(ser
, regi_ser
, r_stat_din
);
147 return !(rstat
.rts_n
== regk_ser_active
);
151 * A set = 0 means 3.3V on the pin, bitvalue: 0=active, 1=inactive
154 static inline void crisv32_serial_set_rts(struct uart_cris_port
*up
,
157 void __iomem
*regi_ser
= up
->regi_ser
;
160 reg_ser_rw_rec_ctrl rec_ctrl
;
162 local_irq_save(flags
);
163 rec_ctrl
= REG_RD(ser
, regi_ser
, rw_rec_ctrl
);
166 rec_ctrl
.rts_n
= regk_ser_active
;
168 rec_ctrl
.rts_n
= regk_ser_inactive
;
169 REG_WR(ser
, regi_ser
, rw_rec_ctrl
, rec_ctrl
);
170 local_irq_restore(flags
);
173 static inline int crisv32_serial_get_cts(struct uart_cris_port
*up
)
175 void __iomem
*regi_ser
= up
->regi_ser
;
176 reg_ser_r_stat_din rstat
= REG_RD(ser
, regi_ser
, r_stat_din
);
178 return (rstat
.cts_n
== regk_ser_active
);
182 * Send a single character for XON/XOFF purposes. We do it in this separate
183 * function instead of the alternative support port.x_char, in the ...start_tx
184 * function, so we don't mix up this case with possibly enabling transmission
185 * of queued-up data (in case that's disabled after *receiving* an XOFF or
186 * negative CTS). This function is used for both DMA and non-DMA case; see HW
187 * docs specifically blessing sending characters manually when DMA for
188 * transmission is enabled and running. We may be asked to transmit despite
189 * the transmitter being disabled by a ..._stop_tx call so we need to enable
190 * it temporarily but restore the state afterwards.
192 static void etraxfs_uart_send_xchar(struct uart_port
*port
, char ch
)
194 struct uart_cris_port
*up
= (struct uart_cris_port
*)port
;
195 reg_ser_rw_dout dout
= { .data
= ch
};
196 reg_ser_rw_ack_intr ack_intr
= { .tr_rdy
= regk_ser_yes
};
197 reg_ser_r_stat_din rstat
;
198 reg_ser_rw_tr_ctrl prev_tr_ctrl
, tr_ctrl
;
199 void __iomem
*regi_ser
= up
->regi_ser
;
203 * Wait for tr_rdy in case a character is already being output. Make
204 * sure we have integrity between the register reads and the writes
205 * below, but don't busy-wait with interrupts off and the port lock
208 spin_lock_irqsave(&port
->lock
, flags
);
210 spin_unlock_irqrestore(&port
->lock
, flags
);
211 spin_lock_irqsave(&port
->lock
, flags
);
212 prev_tr_ctrl
= tr_ctrl
= REG_RD(ser
, regi_ser
, rw_tr_ctrl
);
213 rstat
= REG_RD(ser
, regi_ser
, r_stat_din
);
214 } while (!rstat
.tr_rdy
);
217 * Ack an interrupt if one was just issued for the previous character
218 * that was output. This is required for non-DMA as the interrupt is
219 * used as the only indicator that the transmitter is ready and it
220 * isn't while this x_char is being transmitted.
222 REG_WR(ser
, regi_ser
, rw_ack_intr
, ack_intr
);
224 /* Enable the transmitter in case it was disabled. */
226 REG_WR(ser
, regi_ser
, rw_tr_ctrl
, tr_ctrl
);
229 * Finally, send the blessed character; nothing should stop it now,
230 * except for an xoff-detected state, which we'll handle below.
232 REG_WR(ser
, regi_ser
, rw_dout
, dout
);
233 up
->port
.icount
.tx
++;
235 /* There might be an xoff state to clear. */
236 rstat
= REG_RD(ser
, up
->regi_ser
, r_stat_din
);
239 * Clear any xoff state that *may* have been there to
240 * inhibit transmission of the character.
242 if (rstat
.xoff_detect
) {
243 reg_ser_rw_xoff_clr xoff_clr
= { .clr
= 1 };
244 reg_ser_rw_tr_dma_en tr_dma_en
;
246 REG_WR(ser
, regi_ser
, rw_xoff_clr
, xoff_clr
);
247 tr_dma_en
= REG_RD(ser
, regi_ser
, rw_tr_dma_en
);
250 * If we had an xoff state but cleared it, instead sneak in a
251 * disabled state for the transmitter, after the character we
252 * sent. Thus we keep the port disabled, just as if the xoff
253 * state was still in effect (or actually, as if stop_tx had
254 * been called, as we stop DMA too).
256 prev_tr_ctrl
.stop
= 1;
259 REG_WR(ser
, regi_ser
, rw_tr_dma_en
, tr_dma_en
);
262 /* Restore "previous" enabled/disabled state of the transmitter. */
263 REG_WR(ser
, regi_ser
, rw_tr_ctrl
, prev_tr_ctrl
);
265 spin_unlock_irqrestore(&port
->lock
, flags
);
269 * Do not spin_lock_irqsave or disable interrupts by other means here; it's
270 * already done by the caller.
272 static void etraxfs_uart_start_tx(struct uart_port
*port
)
274 struct uart_cris_port
*up
= (struct uart_cris_port
*)port
;
276 /* we have already done below if a write is ongoing */
277 if (up
->write_ongoing
)
280 /* Signal that write is ongoing */
281 up
->write_ongoing
= 1;
283 etraxfs_uart_start_tx_bottom(port
);
286 static inline void etraxfs_uart_start_tx_bottom(struct uart_port
*port
)
288 struct uart_cris_port
*up
= (struct uart_cris_port
*)port
;
289 void __iomem
*regi_ser
= up
->regi_ser
;
290 reg_ser_rw_tr_ctrl tr_ctrl
;
291 reg_ser_rw_intr_mask intr_mask
;
293 tr_ctrl
= REG_RD(ser
, regi_ser
, rw_tr_ctrl
);
294 tr_ctrl
.stop
= regk_ser_no
;
295 REG_WR(ser
, regi_ser
, rw_tr_ctrl
, tr_ctrl
);
296 intr_mask
= REG_RD(ser
, regi_ser
, rw_intr_mask
);
297 intr_mask
.tr_rdy
= regk_ser_yes
;
298 REG_WR(ser
, regi_ser
, rw_intr_mask
, intr_mask
);
302 * This function handles both the DMA and non-DMA case by ordering the
303 * transmitter to stop of after the current character. We don't need to wait
304 * for any such character to be completely transmitted; we do that where it
305 * matters, like in etraxfs_uart_set_termios. Don't busy-wait here; see
306 * Documentation/serial/driver: this function is called within
307 * spin_lock_irq{,save} and thus separate ones would be disastrous (when SMP).
308 * There's no documented need to set the txd pin to any particular value;
309 * break setting is controlled solely by etraxfs_uart_break_ctl.
311 static void etraxfs_uart_stop_tx(struct uart_port
*port
)
313 struct uart_cris_port
*up
= (struct uart_cris_port
*)port
;
314 void __iomem
*regi_ser
= up
->regi_ser
;
315 reg_ser_rw_tr_ctrl tr_ctrl
;
316 reg_ser_rw_intr_mask intr_mask
;
317 reg_ser_rw_tr_dma_en tr_dma_en
= {0};
318 reg_ser_rw_xoff_clr xoff_clr
= {0};
321 * For the non-DMA case, we'd get a tr_rdy interrupt that we're not
322 * interested in as we're not transmitting any characters. For the
323 * DMA case, that interrupt is already turned off, but no reason to
324 * waste code on conditionals here.
326 intr_mask
= REG_RD(ser
, regi_ser
, rw_intr_mask
);
327 intr_mask
.tr_rdy
= regk_ser_no
;
328 REG_WR(ser
, regi_ser
, rw_intr_mask
, intr_mask
);
330 tr_ctrl
= REG_RD(ser
, regi_ser
, rw_tr_ctrl
);
332 REG_WR(ser
, regi_ser
, rw_tr_ctrl
, tr_ctrl
);
335 * Always clear possible hardware xoff-detected state here, no need to
336 * unnecessary consider mctrl settings and when they change. We clear
337 * it here rather than in start_tx: both functions are called as the
338 * effect of XOFF processing, but start_tx is also called when upper
339 * levels tell the driver that there are more characters to send, so
340 * avoid adding code there.
343 REG_WR(ser
, regi_ser
, rw_xoff_clr
, xoff_clr
);
346 * Disable transmitter DMA, so that if we're in XON/XOFF, we can send
347 * those single characters without also giving go-ahead for queued up
351 REG_WR(ser
, regi_ser
, rw_tr_dma_en
, tr_dma_en
);
354 * Make sure that write_ongoing is reset when stopping tx.
356 up
->write_ongoing
= 0;
359 static void etraxfs_uart_stop_rx(struct uart_port
*port
)
361 struct uart_cris_port
*up
= (struct uart_cris_port
*)port
;
362 void __iomem
*regi_ser
= up
->regi_ser
;
363 reg_ser_rw_rec_ctrl rec_ctrl
= REG_RD(ser
, regi_ser
, rw_rec_ctrl
);
365 rec_ctrl
.en
= regk_ser_no
;
366 REG_WR(ser
, regi_ser
, rw_rec_ctrl
, rec_ctrl
);
369 static unsigned int etraxfs_uart_tx_empty(struct uart_port
*port
)
371 struct uart_cris_port
*up
= (struct uart_cris_port
*)port
;
374 reg_ser_r_stat_din rstat
= {0};
376 spin_lock_irqsave(&up
->port
.lock
, flags
);
378 rstat
= REG_RD(ser
, up
->regi_ser
, r_stat_din
);
379 ret
= rstat
.tr_empty
? TIOCSER_TEMT
: 0;
381 spin_unlock_irqrestore(&up
->port
.lock
, flags
);
384 static unsigned int etraxfs_uart_get_mctrl(struct uart_port
*port
)
386 struct uart_cris_port
*up
= (struct uart_cris_port
*)port
;
390 if (crisv32_serial_get_rts(up
))
392 if (crisv32_serial_get_cts(up
))
394 return mctrl_gpio_get(up
->gpios
, &ret
);
397 static void etraxfs_uart_set_mctrl(struct uart_port
*port
, unsigned int mctrl
)
399 struct uart_cris_port
*up
= (struct uart_cris_port
*)port
;
401 crisv32_serial_set_rts(up
, mctrl
& TIOCM_RTS
? 1 : 0, 0);
402 mctrl_gpio_set(up
->gpios
, mctrl
);
405 static void etraxfs_uart_break_ctl(struct uart_port
*port
, int break_state
)
407 struct uart_cris_port
*up
= (struct uart_cris_port
*)port
;
409 reg_ser_rw_tr_ctrl tr_ctrl
;
410 reg_ser_rw_tr_dma_en tr_dma_en
;
411 reg_ser_rw_intr_mask intr_mask
;
413 spin_lock_irqsave(&up
->port
.lock
, flags
);
414 tr_ctrl
= REG_RD(ser
, up
->regi_ser
, rw_tr_ctrl
);
415 tr_dma_en
= REG_RD(ser
, up
->regi_ser
, rw_tr_dma_en
);
416 intr_mask
= REG_RD(ser
, up
->regi_ser
, rw_intr_mask
);
418 if (break_state
!= 0) { /* Send break */
420 * We need to disable DMA (if used) or tr_rdy interrupts if no
421 * DMA. No need to make this conditional on use of DMA;
422 * disabling will be a no-op for the other mode.
424 intr_mask
.tr_rdy
= regk_ser_no
;
428 * Stop transmission and set the txd pin to 0 after the
429 * current character. The txd setting will take effect after
430 * any current transmission has completed.
435 /* Re-enable the serial interrupt. */
436 intr_mask
.tr_rdy
= regk_ser_yes
;
441 REG_WR(ser
, up
->regi_ser
, rw_tr_ctrl
, tr_ctrl
);
442 REG_WR(ser
, up
->regi_ser
, rw_tr_dma_en
, tr_dma_en
);
443 REG_WR(ser
, up
->regi_ser
, rw_intr_mask
, intr_mask
);
445 spin_unlock_irqrestore(&up
->port
.lock
, flags
);
449 transmit_chars_no_dma(struct uart_cris_port
*up
)
452 struct circ_buf
*xmit
= &up
->port
.state
->xmit
;
454 void __iomem
*regi_ser
= up
->regi_ser
;
455 reg_ser_r_stat_din rstat
;
456 reg_ser_rw_ack_intr ack_intr
= { .tr_rdy
= regk_ser_yes
};
458 if (uart_circ_empty(xmit
) || uart_tx_stopped(&up
->port
)) {
459 /* No more to send, so disable the interrupt. */
460 reg_ser_rw_intr_mask intr_mask
;
462 intr_mask
= REG_RD(ser
, regi_ser
, rw_intr_mask
);
463 intr_mask
.tr_rdy
= 0;
464 intr_mask
.tr_empty
= 0;
465 REG_WR(ser
, regi_ser
, rw_intr_mask
, intr_mask
);
466 up
->write_ongoing
= 0;
470 /* If the serport is fast, we send up to max_count bytes before
474 reg_ser_rw_dout dout
= { .data
= xmit
->buf
[xmit
->tail
] };
476 REG_WR(ser
, regi_ser
, rw_dout
, dout
);
477 REG_WR(ser
, regi_ser
, rw_ack_intr
, ack_intr
);
478 xmit
->tail
= (xmit
->tail
+ 1) & (UART_XMIT_SIZE
-1);
479 up
->port
.icount
.tx
++;
480 if (xmit
->head
== xmit
->tail
)
482 rstat
= REG_RD(ser
, regi_ser
, r_stat_din
);
483 } while ((--max_count
> 0) && rstat
.tr_rdy
);
485 if (uart_circ_chars_pending(xmit
) < WAKEUP_CHARS
)
486 uart_write_wakeup(&up
->port
);
489 static void receive_chars_no_dma(struct uart_cris_port
*up
)
491 reg_ser_rs_stat_din stat_din
;
492 reg_ser_r_stat_din rstat
;
493 struct tty_port
*port
;
494 struct uart_icount
*icount
;
497 reg_ser_rw_ack_intr ack_intr
= { 0 };
499 rstat
= REG_RD(ser
, up
->regi_ser
, r_stat_din
);
500 icount
= &up
->port
.icount
;
501 port
= &up
->port
.state
->port
;
504 stat_din
= REG_RD(ser
, up
->regi_ser
, rs_stat_din
);
508 REG_WR(ser
, up
->regi_ser
, rw_ack_intr
, ack_intr
);
511 if (stat_din
.framing_err
| stat_din
.par_err
| stat_din
.orun
) {
512 if (stat_din
.data
== 0x00 &&
513 stat_din
.framing_err
) {
514 /* Most likely a break. */
517 } else if (stat_din
.par_err
) {
520 } else if (stat_din
.orun
) {
523 } else if (stat_din
.framing_err
) {
530 * If this becomes important, we probably *could* handle this
531 * gracefully by keeping track of the unhandled character.
533 if (!tty_insert_flip_char(port
, stat_din
.data
, flag
))
534 panic("%s: No tty buffer space", __func__
);
535 rstat
= REG_RD(ser
, up
->regi_ser
, r_stat_din
);
536 } while (rstat
.dav
&& (max_count
-- > 0));
537 spin_unlock(&up
->port
.lock
);
538 tty_flip_buffer_push(port
);
539 spin_lock(&up
->port
.lock
);
543 ser_interrupt(int irq
, void *dev_id
)
545 struct uart_cris_port
*up
= (struct uart_cris_port
*)dev_id
;
546 void __iomem
*regi_ser
;
549 spin_lock(&up
->port
.lock
);
551 regi_ser
= up
->regi_ser
;
554 reg_ser_r_masked_intr masked_intr
;
556 masked_intr
= REG_RD(ser
, regi_ser
, r_masked_intr
);
558 * Check what interrupts are active before taking
559 * actions. If DMA is used the interrupt shouldn't
562 if (masked_intr
.dav
) {
563 receive_chars_no_dma(up
);
567 if (masked_intr
.tr_rdy
) {
568 transmit_chars_no_dma(up
);
572 spin_unlock(&up
->port
.lock
);
573 return IRQ_RETVAL(handled
);
576 #ifdef CONFIG_CONSOLE_POLL
577 static int etraxfs_uart_get_poll_char(struct uart_port
*port
)
579 reg_ser_rs_stat_din stat
;
580 reg_ser_rw_ack_intr ack_intr
= { 0 };
581 struct uart_cris_port
*up
= (struct uart_cris_port
*)port
;
584 stat
= REG_RD(ser
, up
->regi_ser
, rs_stat_din
);
587 /* Ack the data_avail interrupt. */
589 REG_WR(ser
, up
->regi_ser
, rw_ack_intr
, ack_intr
);
594 static void etraxfs_uart_put_poll_char(struct uart_port
*port
,
597 reg_ser_r_stat_din stat
;
598 struct uart_cris_port
*up
= (struct uart_cris_port
*)port
;
601 stat
= REG_RD(ser
, up
->regi_ser
, r_stat_din
);
602 } while (!stat
.tr_rdy
);
603 REG_WR_INT(ser
, up
->regi_ser
, rw_dout
, c
);
605 #endif /* CONFIG_CONSOLE_POLL */
607 static int etraxfs_uart_startup(struct uart_port
*port
)
609 struct uart_cris_port
*up
= (struct uart_cris_port
*)port
;
611 reg_ser_rw_intr_mask ser_intr_mask
= {0};
613 ser_intr_mask
.dav
= regk_ser_yes
;
615 if (request_irq(etraxfs_uart_ports
[port
->line
]->irq
, ser_interrupt
,
616 0, DRV_NAME
, etraxfs_uart_ports
[port
->line
]))
617 panic("irq ser%d", port
->line
);
619 spin_lock_irqsave(&up
->port
.lock
, flags
);
621 REG_WR(ser
, up
->regi_ser
, rw_intr_mask
, ser_intr_mask
);
623 etraxfs_uart_set_mctrl(&up
->port
, up
->port
.mctrl
);
625 spin_unlock_irqrestore(&up
->port
.lock
, flags
);
630 static void etraxfs_uart_shutdown(struct uart_port
*port
)
632 struct uart_cris_port
*up
= (struct uart_cris_port
*)port
;
635 spin_lock_irqsave(&up
->port
.lock
, flags
);
637 etraxfs_uart_stop_tx(port
);
638 etraxfs_uart_stop_rx(port
);
640 free_irq(etraxfs_uart_ports
[port
->line
]->irq
,
641 etraxfs_uart_ports
[port
->line
]);
643 etraxfs_uart_set_mctrl(&up
->port
, up
->port
.mctrl
);
645 spin_unlock_irqrestore(&up
->port
.lock
, flags
);
650 etraxfs_uart_set_termios(struct uart_port
*port
, struct ktermios
*termios
,
651 struct ktermios
*old
)
653 struct uart_cris_port
*up
= (struct uart_cris_port
*)port
;
655 reg_ser_rw_xoff xoff
;
656 reg_ser_rw_xoff_clr xoff_clr
= {0};
657 reg_ser_rw_tr_ctrl tx_ctrl
= {0};
658 reg_ser_rw_tr_dma_en tx_dma_en
= {0};
659 reg_ser_rw_rec_ctrl rx_ctrl
= {0};
660 reg_ser_rw_tr_baud_div tx_baud_div
= {0};
661 reg_ser_rw_rec_baud_div rx_baud_div
= {0};
665 termios
->c_cflag
== old
->c_cflag
&&
666 termios
->c_iflag
== old
->c_iflag
)
669 /* Tx: 8 bit, no/even parity, 1 stop bit, no cts. */
670 tx_ctrl
.base_freq
= regk_ser_f29_493
;
673 tx_ctrl
.auto_rts
= regk_ser_no
;
675 tx_ctrl
.auto_cts
= 0;
676 /* Rx: 8 bit, no/even parity. */
677 rx_ctrl
.dma_err
= regk_ser_stop
;
678 rx_ctrl
.sampling
= regk_ser_majority
;
681 rx_ctrl
.rts_n
= regk_ser_inactive
;
683 /* Common for tx and rx: 8N1. */
684 tx_ctrl
.data_bits
= regk_ser_bits8
;
685 rx_ctrl
.data_bits
= regk_ser_bits8
;
686 tx_ctrl
.par
= regk_ser_even
;
687 rx_ctrl
.par
= regk_ser_even
;
688 tx_ctrl
.par_en
= regk_ser_no
;
689 rx_ctrl
.par_en
= regk_ser_no
;
691 tx_ctrl
.stop_bits
= regk_ser_bits1
;
694 * Change baud-rate and write it to the hardware.
696 * baud_clock = base_freq / (divisor*8)
697 * divisor = base_freq / (baud_clock * 8)
698 * base_freq is either:
699 * off, ext, 29.493MHz, 32.000 MHz, 32.768 MHz or 100 MHz
700 * 20.493MHz is used for standard baudrates
704 * For the console port we keep the original baudrate here. Not very
707 if ((port
!= console_port
) || old
)
708 baud
= uart_get_baud_rate(port
, termios
, old
, 0,
713 tx_baud_div
.div
= 29493000 / (8 * baud
);
714 /* Rx uses same as tx. */
715 rx_baud_div
.div
= tx_baud_div
.div
;
716 rx_ctrl
.base_freq
= tx_ctrl
.base_freq
;
718 if ((termios
->c_cflag
& CSIZE
) == CS7
) {
719 /* Set 7 bit mode. */
720 tx_ctrl
.data_bits
= regk_ser_bits7
;
721 rx_ctrl
.data_bits
= regk_ser_bits7
;
724 if (termios
->c_cflag
& CSTOPB
) {
725 /* Set 2 stop bit mode. */
726 tx_ctrl
.stop_bits
= regk_ser_bits2
;
729 if (termios
->c_cflag
& PARENB
) {
731 tx_ctrl
.par_en
= regk_ser_yes
;
732 rx_ctrl
.par_en
= regk_ser_yes
;
735 if (termios
->c_cflag
& CMSPAR
) {
736 if (termios
->c_cflag
& PARODD
) {
737 /* Set mark parity if PARODD and CMSPAR. */
738 tx_ctrl
.par
= regk_ser_mark
;
739 rx_ctrl
.par
= regk_ser_mark
;
741 tx_ctrl
.par
= regk_ser_space
;
742 rx_ctrl
.par
= regk_ser_space
;
745 if (termios
->c_cflag
& PARODD
) {
746 /* Set odd parity. */
747 tx_ctrl
.par
= regk_ser_odd
;
748 rx_ctrl
.par
= regk_ser_odd
;
752 if (termios
->c_cflag
& CRTSCTS
) {
753 /* Enable automatic CTS handling. */
754 tx_ctrl
.auto_cts
= regk_ser_yes
;
757 /* Make sure the tx and rx are enabled. */
758 tx_ctrl
.en
= regk_ser_yes
;
759 rx_ctrl
.en
= regk_ser_yes
;
761 spin_lock_irqsave(&port
->lock
, flags
);
764 REG_WR(ser
, up
->regi_ser
, rw_tr_dma_en
, tx_dma_en
);
766 /* Actually write the control regs (if modified) to the hardware. */
767 uart_update_timeout(port
, termios
->c_cflag
, port
->uartclk
/8);
768 MODIFY_REG(up
->regi_ser
, rw_rec_baud_div
, rx_baud_div
);
769 MODIFY_REG(up
->regi_ser
, rw_rec_ctrl
, rx_ctrl
);
771 MODIFY_REG(up
->regi_ser
, rw_tr_baud_div
, tx_baud_div
);
772 MODIFY_REG(up
->regi_ser
, rw_tr_ctrl
, tx_ctrl
);
775 REG_WR(ser
, up
->regi_ser
, rw_tr_dma_en
, tx_dma_en
);
777 xoff
= REG_RD(ser
, up
->regi_ser
, rw_xoff
);
779 if (up
->port
.state
&& up
->port
.state
->port
.tty
&&
780 (up
->port
.state
->port
.tty
->termios
.c_iflag
& IXON
)) {
781 xoff
.chr
= STOP_CHAR(up
->port
.state
->port
.tty
);
782 xoff
.automatic
= regk_ser_yes
;
784 xoff
.automatic
= regk_ser_no
;
786 MODIFY_REG(up
->regi_ser
, rw_xoff
, xoff
);
789 * Make sure we don't start in an automatically shut-off state due to
790 * a previous early exit.
793 REG_WR(ser
, up
->regi_ser
, rw_xoff_clr
, xoff_clr
);
795 etraxfs_uart_set_mctrl(&up
->port
, up
->port
.mctrl
);
796 spin_unlock_irqrestore(&up
->port
.lock
, flags
);
800 etraxfs_uart_type(struct uart_port
*port
)
805 static void etraxfs_uart_release_port(struct uart_port
*port
)
809 static int etraxfs_uart_request_port(struct uart_port
*port
)
814 static void etraxfs_uart_config_port(struct uart_port
*port
, int flags
)
816 struct uart_cris_port
*up
= (struct uart_cris_port
*)port
;
818 up
->port
.type
= PORT_CRIS
;
821 static const struct uart_ops etraxfs_uart_pops
= {
822 .tx_empty
= etraxfs_uart_tx_empty
,
823 .set_mctrl
= etraxfs_uart_set_mctrl
,
824 .get_mctrl
= etraxfs_uart_get_mctrl
,
825 .stop_tx
= etraxfs_uart_stop_tx
,
826 .start_tx
= etraxfs_uart_start_tx
,
827 .send_xchar
= etraxfs_uart_send_xchar
,
828 .stop_rx
= etraxfs_uart_stop_rx
,
829 .break_ctl
= etraxfs_uart_break_ctl
,
830 .startup
= etraxfs_uart_startup
,
831 .shutdown
= etraxfs_uart_shutdown
,
832 .set_termios
= etraxfs_uart_set_termios
,
833 .type
= etraxfs_uart_type
,
834 .release_port
= etraxfs_uart_release_port
,
835 .request_port
= etraxfs_uart_request_port
,
836 .config_port
= etraxfs_uart_config_port
,
837 #ifdef CONFIG_CONSOLE_POLL
838 .poll_get_char
= etraxfs_uart_get_poll_char
,
839 .poll_put_char
= etraxfs_uart_put_poll_char
,
843 static void cris_serial_port_init(struct uart_port
*port
, int line
)
845 struct uart_cris_port
*up
= (struct uart_cris_port
*)port
;
851 spin_lock_init(&port
->lock
);
852 port
->ops
= &etraxfs_uart_pops
;
854 port
->iobase
= (unsigned long) up
->regi_ser
;
855 port
->uartclk
= 29493000;
858 * We can't fit any more than 255 here (unsigned char), though
859 * actually UART_XMIT_SIZE characters could be pending output.
860 * At time of this writing, the definition of "fifosize" is here the
861 * amount of characters that can be pending output after a start_tx call
862 * until tx_empty returns 1: see serial_core.c:uart_wait_until_sent.
863 * This matters for timeout calculations unfortunately, but keeping
864 * larger amounts at the DMA wouldn't win much so let's just play nice.
866 port
->fifosize
= 255;
867 port
->flags
= UPF_BOOT_AUTOCONF
;
870 static int etraxfs_uart_probe(struct platform_device
*pdev
)
872 struct device_node
*np
= pdev
->dev
.of_node
;
873 struct uart_cris_port
*up
;
879 dev_id
= of_alias_get_id(np
, "serial");
883 if (dev_id
>= UART_NR
)
886 if (etraxfs_uart_ports
[dev_id
])
889 up
= devm_kzalloc(&pdev
->dev
, sizeof(struct uart_cris_port
),
894 up
->irq
= irq_of_parse_and_map(np
, 0);
895 up
->regi_ser
= of_iomap(np
, 0);
896 up
->port
.dev
= &pdev
->dev
;
898 up
->gpios
= mctrl_gpio_init_noauto(&pdev
->dev
, 0);
899 if (IS_ERR(up
->gpios
))
900 return PTR_ERR(up
->gpios
);
902 cris_serial_port_init(&up
->port
, dev_id
);
904 etraxfs_uart_ports
[dev_id
] = up
;
905 platform_set_drvdata(pdev
, &up
->port
);
906 uart_add_one_port(&etraxfs_uart_driver
, &up
->port
);
911 static int etraxfs_uart_remove(struct platform_device
*pdev
)
913 struct uart_port
*port
;
915 port
= platform_get_drvdata(pdev
);
916 uart_remove_one_port(&etraxfs_uart_driver
, port
);
917 etraxfs_uart_ports
[port
->line
] = NULL
;
922 static const struct of_device_id etraxfs_uart_dt_ids
[] = {
923 { .compatible
= "axis,etraxfs-uart" },
927 MODULE_DEVICE_TABLE(of
, etraxfs_uart_dt_ids
);
929 static struct platform_driver etraxfs_uart_platform_driver
= {
932 .of_match_table
= of_match_ptr(etraxfs_uart_dt_ids
),
934 .probe
= etraxfs_uart_probe
,
935 .remove
= etraxfs_uart_remove
,
938 static int __init
etraxfs_uart_init(void)
942 ret
= uart_register_driver(&etraxfs_uart_driver
);
946 ret
= platform_driver_register(&etraxfs_uart_platform_driver
);
948 uart_unregister_driver(&etraxfs_uart_driver
);
953 static void __exit
etraxfs_uart_exit(void)
955 platform_driver_unregister(&etraxfs_uart_platform_driver
);
956 uart_unregister_driver(&etraxfs_uart_driver
);
959 module_init(etraxfs_uart_init
);
960 module_exit(etraxfs_uart_exit
);