2 * Copyright (c) 2005 M. Warner Losh
3 * Copyright (c) 2005 Olivier Houchard
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
31 #include "opt_comconsole.h"
33 #include <sys/param.h>
34 #include <sys/systm.h>
39 #include <machine/bus.h>
41 #include <dev/uart/uart.h>
42 #include <dev/uart/uart_cpu.h>
43 #include <dev/uart/uart_bus.h>
44 #include <arm/at91/at91rm92reg.h>
45 #include <arm/at91/at91_usartreg.h>
46 #include <arm/at91/at91_pdcreg.h>
50 #define DEFAULT_RCLK AT91C_MASTER_CLOCK
51 #define USART_BUFFER_SIZE 128
54 * High-level UART interface.
56 struct at91_usart_rx {
58 uint8_t buffer[USART_BUFFER_SIZE];
62 struct at91_usart_softc {
63 struct uart_softc base;
64 bus_dma_tag_t dmatag; /* bus dma tag for mbufs */
68 struct at91_usart_rx ping_pong[2];
69 struct at91_usart_rx *ping;
70 struct at91_usart_rx *pong;
73 #define RD4(bas, reg) \
74 bus_space_read_4((bas)->bst, (bas)->bsh, uart_regofs(bas, reg))
75 #define WR4(bas, reg, value) \
76 bus_space_write_4((bas)->bst, (bas)->bsh, uart_regofs(bas, reg), value)
78 #define SIGCHG(c, i, s, d) \
81 i |= (i & s) ? s : s | d; \
83 i = (i & s) ? (i & ~s) | d : i; \
87 #define BAUD2DIVISOR(b) \
88 ((((DEFAULT_RCLK * 10) / ((b) * 16)) + 5) / 10)
91 * Low-level UART interface.
93 static int at91_usart_probe(struct uart_bas *bas);
94 static void at91_usart_init(struct uart_bas *bas, int, int, int, int);
95 static void at91_usart_term(struct uart_bas *bas);
96 static void at91_usart_putc(struct uart_bas *bas, int);
97 static int at91_usart_poll(struct uart_bas *bas);
98 static int at91_usart_getc(struct uart_bas *bas, struct mtx *mtx);
100 extern SLIST_HEAD(uart_devinfo_list, uart_devinfo) uart_sysdevs;
103 at91_usart_param(struct uart_bas *bas, int baudrate, int databits,
104 int stopbits, int parity)
109 * Assume 3-write RS-232 configuration.
110 * XXX Not sure how uart will present the other modes to us, so
111 * XXX they are unimplemented. maybe ioctl?
113 mr = USART_MR_MODE_NORMAL;
114 mr |= USART_MR_USCLKS_MCK; /* Assume MCK */
117 * Or in the databits requested
120 mr &= ~USART_MR_MODE9;
123 mr |= USART_MR_CHRL_5BITS;
126 mr |= USART_MR_CHRL_6BITS;
129 mr |= USART_MR_CHRL_7BITS;
132 mr |= USART_MR_CHRL_8BITS;
135 mr |= USART_MR_CHRL_8BITS | USART_MR_MODE9;
145 case UART_PARITY_NONE:
146 mr |= USART_MR_PAR_NONE;
148 case UART_PARITY_ODD:
149 mr |= USART_MR_PAR_ODD;
151 case UART_PARITY_EVEN:
152 mr |= USART_MR_PAR_EVEN;
154 case UART_PARITY_MARK:
155 mr |= USART_MR_PAR_MARK;
157 case UART_PARITY_SPACE:
158 mr |= USART_MR_PAR_SPACE;
165 * Or in the stop bits. Note: The hardware supports 1.5 stop
166 * bits in async mode, but there's no way to specify that
167 * AFAICT. Instead, rely on the convention documented at
168 * http://www.lammertbies.nl/comm/info/RS-232_specs.html which
169 * states that 1.5 stop bits are used for 5 bit bytes and
170 * 2 stop bits only for longer bytes.
173 mr |= USART_MR_NBSTOP_1;
174 else if (databits > 5)
175 mr |= USART_MR_NBSTOP_2;
177 mr |= USART_MR_NBSTOP_1_5;
180 * We want normal plumbing mode too, none of this fancy
181 * loopback or echo mode.
183 mr |= USART_MR_CHMODE_NORMAL;
185 mr &= ~USART_MR_MSBF; /* lsb first */
186 mr &= ~USART_MR_CKLO_SCK; /* Don't drive SCK */
188 WR4(bas, USART_MR, mr);
193 WR4(bas, USART_BRGR, BAUD2DIVISOR(baudrate));
195 /* XXX Need to take possible synchronous mode into account */
199 struct uart_ops at91_usart_ops = {
200 .probe = at91_usart_probe,
201 .init = at91_usart_init,
202 .term = at91_usart_term,
203 .putc = at91_usart_putc,
204 .poll = at91_usart_poll,
205 .getc = at91_usart_getc,
209 at91_usart_probe(struct uart_bas *bas)
211 /* We know that this is always here */
216 * Initialize this device for use as a console.
219 at91_usart_init(struct uart_bas *bas, int baudrate, int databits, int stopbits,
223 at91_usart_param(bas, baudrate, databits, stopbits, parity);
225 /* Reset the rx and tx buffers and turn on rx and tx */
226 WR4(bas, USART_CR, USART_CR_RSTSTA | USART_CR_RSTRX | USART_CR_RSTTX);
227 WR4(bas, USART_CR, USART_CR_RXEN | USART_CR_TXEN);
228 WR4(bas, USART_IDR, 0xffffffff);
232 * Free resources now that we're no longer the console. This appears to
233 * be never called, and I'm unsure quite what to do if I am called.
236 at91_usart_term(struct uart_bas *bas)
242 * Put a character of console output (so we do it here polling rather than
246 at91_usart_putc(struct uart_bas *bas, int c)
249 while (!(RD4(bas, USART_CSR) & USART_CSR_TXRDY))
251 WR4(bas, USART_THR, c);
255 * Poll for a character available
258 at91_usart_poll(struct uart_bas *bas)
261 if (!(RD4(bas, USART_CSR) & USART_CSR_RXRDY))
263 return (RD4(bas, USART_RHR) & 0xff);
267 * Block waiting for a character.
270 at91_usart_getc(struct uart_bas *bas, struct mtx *mtx)
274 while (!(RD4(bas, USART_CSR) & USART_CSR_RXRDY))
276 c = RD4(bas, USART_RHR);
281 static int at91_usart_bus_probe(struct uart_softc *sc);
282 static int at91_usart_bus_attach(struct uart_softc *sc);
283 static int at91_usart_bus_flush(struct uart_softc *, int);
284 static int at91_usart_bus_getsig(struct uart_softc *);
285 static int at91_usart_bus_ioctl(struct uart_softc *, int, intptr_t);
286 static int at91_usart_bus_ipend(struct uart_softc *);
287 static int at91_usart_bus_param(struct uart_softc *, int, int, int, int);
288 static int at91_usart_bus_receive(struct uart_softc *);
289 static int at91_usart_bus_setsig(struct uart_softc *, int);
290 static int at91_usart_bus_transmit(struct uart_softc *);
292 static kobj_method_t at91_usart_methods[] = {
293 KOBJMETHOD(uart_probe, at91_usart_bus_probe),
294 KOBJMETHOD(uart_attach, at91_usart_bus_attach),
295 KOBJMETHOD(uart_flush, at91_usart_bus_flush),
296 KOBJMETHOD(uart_getsig, at91_usart_bus_getsig),
297 KOBJMETHOD(uart_ioctl, at91_usart_bus_ioctl),
298 KOBJMETHOD(uart_ipend, at91_usart_bus_ipend),
299 KOBJMETHOD(uart_param, at91_usart_bus_param),
300 KOBJMETHOD(uart_receive, at91_usart_bus_receive),
301 KOBJMETHOD(uart_setsig, at91_usart_bus_setsig),
302 KOBJMETHOD(uart_transmit, at91_usart_bus_transmit),
308 at91_usart_bus_probe(struct uart_softc *sc)
313 #ifndef SKYEYE_WORKAROUNDS
315 at91_getaddr(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
319 *(bus_addr_t *)arg = segs[0].ds_addr;
324 at91_usart_bus_attach(struct uart_softc *sc)
328 struct at91_usart_softc *atsc;
330 atsc = (struct at91_usart_softc *)sc;
333 * See if we have a TIMEOUT bit. We disable all interrupts as
334 * a side effect. Boot loaders may have enabled them. Since
335 * a TIMEOUT interrupt can't happen without other setup, the
336 * apparent race here can't actually happen.
338 WR4(&sc->sc_bas, USART_IDR, 0xffffffff);
339 WR4(&sc->sc_bas, USART_IER, USART_CSR_TIMEOUT);
340 if (RD4(&sc->sc_bas, USART_IMR) & USART_CSR_TIMEOUT)
341 atsc->flags |= HAS_TIMEOUT;
342 WR4(&sc->sc_bas, USART_IDR, 0xffffffff);
344 sc->sc_txfifosz = USART_BUFFER_SIZE;
345 sc->sc_rxfifosz = USART_BUFFER_SIZE;
349 * Allocate DMA tags and maps
351 err = bus_dma_tag_create(NULL, 1, 0, BUS_SPACE_MAXADDR_32BIT,
352 BUS_SPACE_MAXADDR, NULL, NULL, USART_BUFFER_SIZE, 1,
353 USART_BUFFER_SIZE, BUS_DMA_ALLOCNOW, NULL, NULL, &atsc->dmatag);
356 err = bus_dmamap_create(atsc->dmatag, 0, &atsc->tx_map);
359 if (atsc->flags & HAS_TIMEOUT) {
360 for (i = 0; i < 2; i++) {
361 err = bus_dmamap_create(atsc->dmatag, 0,
362 &atsc->ping_pong[i].map);
365 err = bus_dmamap_load(atsc->dmatag,
366 atsc->ping_pong[i].map,
367 atsc->ping_pong[i].buffer, sc->sc_rxfifosz,
368 at91_getaddr, &atsc->ping_pong[i].pa, 0);
371 bus_dmamap_sync(atsc->dmatag, atsc->ping_pong[i].map,
372 BUS_DMASYNC_PREREAD);
374 atsc->ping = &atsc->ping_pong[0];
375 atsc->pong = &atsc->ping_pong[1];
379 * Prime the pump with the RX buffer. We use two 64 byte bounce
380 * buffers here to avoid data overflow.
383 /* Turn on rx and tx */
384 cr = USART_CR_RSTSTA | USART_CR_RSTRX | USART_CR_RSTTX;
385 WR4(&sc->sc_bas, USART_CR, cr);
386 WR4(&sc->sc_bas, USART_CR, USART_CR_RXEN | USART_CR_TXEN);
389 * Setup the PDC to receive data. We use the ping-pong buffers
390 * so that we can more easily bounce between the two and so that
391 * we get an interrupt 1/2 way through the software 'fifo' we have
394 if (atsc->flags & HAS_TIMEOUT) {
395 WR4(&sc->sc_bas, PDC_RPR, atsc->ping->pa);
396 WR4(&sc->sc_bas, PDC_RCR, sc->sc_rxfifosz);
397 WR4(&sc->sc_bas, PDC_RNPR, atsc->pong->pa);
398 WR4(&sc->sc_bas, PDC_RNCR, sc->sc_rxfifosz);
399 WR4(&sc->sc_bas, PDC_PTCR, PDC_PTCR_RXTEN);
401 /* Set the receive timeout to be 1.5 character times. */
402 WR4(&sc->sc_bas, USART_RTOR, 12);
403 WR4(&sc->sc_bas, USART_CR, USART_CR_STTTO);
404 WR4(&sc->sc_bas, USART_IER, USART_CSR_TIMEOUT |
405 USART_CSR_RXBUFF | USART_CSR_ENDRX);
407 WR4(&sc->sc_bas, USART_IER, USART_CSR_RXRDY);
409 WR4(&sc->sc_bas, USART_IER, USART_CSR_RXBRK);
416 at91_usart_bus_transmit(struct uart_softc *sc)
418 #ifndef SKYEYE_WORKAROUNDS
421 struct at91_usart_softc *atsc;
423 atsc = (struct at91_usart_softc *)sc;
424 #ifndef SKYEYE_WORKAROUNDS
425 if (bus_dmamap_load(atsc->dmatag, atsc->tx_map, sc->sc_txbuf,
426 sc->sc_txdatasz, at91_getaddr, &addr, 0) != 0)
428 bus_dmamap_sync(atsc->dmatag, atsc->tx_map, BUS_DMASYNC_PREWRITE);
431 uart_lock(sc->sc_hwmtx);
433 #ifndef SKYEYE_WORKAROUNDS
435 * Setup the PDC to transfer the data and interrupt us when it
436 * is done. We've already requested the interrupt.
438 WR4(&sc->sc_bas, PDC_TPR, addr);
439 WR4(&sc->sc_bas, PDC_TCR, sc->sc_txdatasz);
440 WR4(&sc->sc_bas, PDC_PTCR, PDC_PTCR_TXTEN);
441 WR4(&sc->sc_bas, USART_IER, USART_CSR_ENDTX);
442 uart_unlock(sc->sc_hwmtx);
444 for (int i = 0; i < sc->sc_txdatasz; i++)
445 at91_usart_putc(&sc->sc_bas, sc->sc_txbuf[i]);
447 * XXX: Gross hack : Skyeye doesn't raise an interrupt once the
448 * transfer is done, so simulate it.
450 WR4(&sc->sc_bas, USART_IER, USART_CSR_TXRDY);
455 at91_usart_bus_setsig(struct uart_softc *sc, int sig)
457 uint32_t new, old, cr;
458 struct uart_bas *bas;
464 SIGCHG(sig & SER_DTR, new, SER_DTR, SER_DDTR);
466 SIGCHG(sig & SER_RTS, new, SER_RTS, SER_DRTS);
467 } while (!atomic_cmpset_32(&sc->sc_hwsig, old, new));
469 uart_lock(sc->sc_hwmtx);
472 cr |= USART_CR_DTREN;
474 cr |= USART_CR_DTRDIS;
476 cr |= USART_CR_RTSEN;
478 cr |= USART_CR_RTSDIS;
479 WR4(bas, USART_CR, cr);
480 uart_unlock(sc->sc_hwmtx);
484 at91_usart_bus_receive(struct uart_softc *sc)
490 at91_usart_bus_param(struct uart_softc *sc, int baudrate, int databits,
491 int stopbits, int parity)
494 return (at91_usart_param(&sc->sc_bas, baudrate, databits, stopbits,
499 at91_rx_put(struct uart_softc *sc, int key)
501 #if defined(KDB) && defined(ALT_BREAK_TO_DEBUGGER)
502 if (sc->sc_sysdev != NULL && sc->sc_sysdev->type == UART_DEV_CONSOLE) {
503 if (kdb_alt_break(key, &sc->sc_altbrk))
504 kdb_enter("Break sequence to console");
507 uart_rx_put(sc, key);
511 at91_usart_bus_ipend(struct uart_softc *sc)
513 int csr = RD4(&sc->sc_bas, USART_CSR);
514 int ipend = 0, i, len;
515 struct at91_usart_softc *atsc;
516 struct at91_usart_rx *p;
518 atsc = (struct at91_usart_softc *)sc;
519 if (csr & USART_CSR_ENDTX) {
520 bus_dmamap_sync(atsc->dmatag, atsc->tx_map,
521 BUS_DMASYNC_POSTWRITE);
522 bus_dmamap_unload(atsc->dmatag, atsc->tx_map);
524 uart_lock(sc->sc_hwmtx);
525 if (csr & USART_CSR_TXRDY) {
527 ipend |= SER_INT_TXIDLE;
528 WR4(&sc->sc_bas, USART_IDR, USART_CSR_TXRDY);
530 if (csr & USART_CSR_ENDTX) {
532 ipend |= SER_INT_TXIDLE;
533 WR4(&sc->sc_bas, USART_IDR, USART_CSR_ENDTX);
537 * Due to the contraints of the DMA engine present in the
538 * atmel chip, I can't just say I have a rx interrupt pending
539 * and do all the work elsewhere. I need to look at the CSR
540 * bits right now and do things based on them to avoid races.
542 if ((atsc->flags & HAS_TIMEOUT) && (csr & USART_CSR_RXBUFF)) {
543 // Have a buffer overflow. Copy all data from both
544 // ping and pong. Insert overflow character. Reset
545 // ping and pong and re-enable the PDC to receive
547 bus_dmamap_sync(atsc->dmatag, atsc->ping->map,
548 BUS_DMASYNC_POSTREAD);
549 bus_dmamap_sync(atsc->dmatag, atsc->pong->map,
550 BUS_DMASYNC_POSTREAD);
551 for (i = 0; i < sc->sc_rxfifosz; i++)
552 at91_rx_put(sc, atsc->ping->buffer[i]);
553 for (i = 0; i < sc->sc_rxfifosz; i++)
554 at91_rx_put(sc, atsc->pong->buffer[i]);
555 uart_rx_put(sc, UART_STAT_OVERRUN);
556 csr &= ~(USART_CSR_ENDRX | USART_CSR_TIMEOUT);
557 WR4(&sc->sc_bas, PDC_RPR, atsc->ping->pa);
558 WR4(&sc->sc_bas, PDC_RCR, sc->sc_rxfifosz);
559 WR4(&sc->sc_bas, PDC_RNPR, atsc->pong->pa);
560 WR4(&sc->sc_bas, PDC_RNCR, sc->sc_rxfifosz);
561 WR4(&sc->sc_bas, PDC_PTCR, PDC_PTCR_RXTEN);
562 ipend |= SER_INT_RXREADY;
564 if ((atsc->flags & HAS_TIMEOUT) && (csr & USART_CSR_ENDRX)) {
565 // Shuffle data from 'ping' of ping pong buffer, but
566 // leave current 'pong' in place, as it has become the
567 // new 'ping'. We need to copy data and setup the old
568 // 'ping' as the new 'pong' when we're done.
569 bus_dmamap_sync(atsc->dmatag, atsc->ping->map,
570 BUS_DMASYNC_POSTREAD);
571 for (i = 0; i < sc->sc_rxfifosz; i++)
572 at91_rx_put(sc, atsc->ping->buffer[i]);
574 atsc->ping = atsc->pong;
576 WR4(&sc->sc_bas, PDC_RNPR, atsc->pong->pa);
577 WR4(&sc->sc_bas, PDC_RNCR, sc->sc_rxfifosz);
578 ipend |= SER_INT_RXREADY;
580 if ((atsc->flags & HAS_TIMEOUT) && (csr & USART_CSR_TIMEOUT)) {
581 // We have one partial buffer. We need to stop the
582 // PDC, get the number of characters left and from
583 // that compute number of valid characters. We then
584 // need to reset ping and pong and reenable the PDC.
585 // Not sure if there's a race here at fast baud rates
586 // we need to worry about.
587 WR4(&sc->sc_bas, PDC_PTCR, PDC_PTCR_RXTDIS);
588 bus_dmamap_sync(atsc->dmatag, atsc->ping->map,
589 BUS_DMASYNC_POSTREAD);
590 len = sc->sc_rxfifosz - RD4(&sc->sc_bas, PDC_RCR);
591 for (i = 0; i < len; i++)
592 at91_rx_put(sc, atsc->ping->buffer[i]);
593 WR4(&sc->sc_bas, PDC_RPR, atsc->ping->pa);
594 WR4(&sc->sc_bas, PDC_RCR, sc->sc_rxfifosz);
595 WR4(&sc->sc_bas, USART_CR, USART_CR_STTTO);
596 WR4(&sc->sc_bas, PDC_PTCR, PDC_PTCR_RXTEN);
597 ipend |= SER_INT_RXREADY;
599 if (!(atsc->flags & HAS_TIMEOUT) && (csr & USART_CSR_RXRDY)) {
600 // We have another charater in a device that doesn't support
601 // timeouts, so we do it one character at a time.
602 at91_rx_put(sc, RD4(&sc->sc_bas, USART_RHR) & 0xff);
603 ipend |= SER_INT_RXREADY;
606 if (csr & USART_CSR_RXBRK) {
607 unsigned int cr = USART_CR_RSTSTA;
609 ipend |= SER_INT_BREAK;
610 WR4(&sc->sc_bas, USART_CR, cr);
612 uart_unlock(sc->sc_hwmtx);
616 at91_usart_bus_flush(struct uart_softc *sc, int what)
622 at91_usart_bus_getsig(struct uart_softc *sc)
627 uart_lock(sc->sc_hwmtx);
628 csr = RD4(&sc->sc_bas, USART_CSR);
630 if (csr & USART_CSR_CTS)
632 if (csr & USART_CSR_DCD)
634 if (csr & USART_CSR_DSR)
636 if (csr & USART_CSR_RI)
638 new = sig & ~SER_MASK_DELTA;
640 uart_unlock(sc->sc_hwmtx);
645 at91_usart_bus_ioctl(struct uart_softc *sc, int request, intptr_t data)
648 case UART_IOCTL_BREAK:
649 case UART_IOCTL_IFLOW:
650 case UART_IOCTL_OFLOW:
652 case UART_IOCTL_BAUD:
653 WR4(&sc->sc_bas, USART_BRGR, BAUD2DIVISOR(*(int *)data));
658 struct uart_class at91_usart_class = {
661 sizeof(struct at91_usart_softc),
663 .uc_rclk = DEFAULT_RCLK