2 * Copyright (c) 2003 Marcel Moolenaar
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
31 #include <sys/systm.h>
36 #include <machine/bus.h>
38 #include <dev/uart/uart.h>
39 #include <dev/uart/uart_cpu.h>
40 #include <dev/uart/uart_bus.h>
41 #include <arm/sa11x0/sa11x0_reg.h>
42 #include <arm/sa11x0/uart_dev_sa1110.h>
46 #define DEFAULT_RCLK 3686400
49 * Low-level UART interface.
51 static int sa1110_probe(struct uart_bas *bas);
52 static void sa1110_init(struct uart_bas *bas, int, int, int, int);
53 static void sa1110_term(struct uart_bas *bas);
54 static void sa1110_putc(struct uart_bas *bas, int);
55 static int sa1110_poll(struct uart_bas *bas);
56 static int sa1110_getc(struct uart_bas *bas, struct mtx *mtx);
58 extern SLIST_HEAD(uart_devinfo_list, uart_devinfo) uart_sysdevs;
60 struct uart_ops uart_sa1110_ops = {
61 .probe = sa1110_probe,
70 sa1110_probe(struct uart_bas *bas)
76 sa1110_init(struct uart_bas *bas, int baudrate, int databits, int stopbits,
82 bas->rclk = DEFAULT_RCLK;
83 while (uart_getreg(bas, SACOM_SR1) & SR1_TBY);
84 uart_setreg(bas, SACOM_CR3, 0);
85 brd = SACOMSPEED(baudrate);
86 uart_setreg(bas, SACOM_CR1, brd >> 8);
87 uart_setreg(bas, SACOM_CR2, brd & 0xff);
88 uart_setreg(bas, SACOM_CR3, CR3_RXE | CR3_TXE);
92 sa1110_term(struct uart_bas *bas)
98 sa1110_putc(struct uart_bas *bas, int c)
100 while (!uart_getreg(bas, SACOM_SR1) & SR1_TNF);
101 uart_setreg(bas, SACOM_DR, c);
105 sa1110_poll(struct uart_bas *bas)
107 if (!(uart_getreg(bas, SACOM_SR1) & SR1_RNE))
109 return (uart_getreg(bas, SACOM_DR) & 0xff);
113 sa1110_getc(struct uart_bas *bas, struct mtx *mtx)
117 while (!(uart_getreg(bas, SACOM_SR1) & SR1_RNE)) {
120 sr0 = uart_getreg(bas, SACOM_SR0);
121 if (ISSET(sr0, SR0_RBB))
122 uart_setreg(bas, SACOM_SR0, SR0_RBB);
123 if (ISSET(sr0, SR0_REB))
124 uart_setreg(bas, SACOM_SR0, SR0_REB);
126 c = uart_getreg(bas, SACOM_DR);
131 static int sa1110_bus_probe(struct uart_softc *sc);
132 static int sa1110_bus_attach(struct uart_softc *sc);
133 static int sa1110_bus_flush(struct uart_softc *, int);
134 static int sa1110_bus_getsig(struct uart_softc *);
135 static int sa1110_bus_ioctl(struct uart_softc *, int, intptr_t);
136 static int sa1110_bus_ipend(struct uart_softc *);
137 static int sa1110_bus_param(struct uart_softc *, int, int, int, int);
138 static int sa1110_bus_receive(struct uart_softc *);
139 static int sa1110_bus_setsig(struct uart_softc *, int);
140 static int sa1110_bus_transmit(struct uart_softc *);
142 static kobj_method_t sa1110_methods[] = {
143 KOBJMETHOD(uart_probe, sa1110_bus_probe),
144 KOBJMETHOD(uart_attach, sa1110_bus_attach),
145 KOBJMETHOD(uart_flush, sa1110_bus_flush),
146 KOBJMETHOD(uart_getsig, sa1110_bus_getsig),
147 KOBJMETHOD(uart_ioctl, sa1110_bus_ioctl),
148 KOBJMETHOD(uart_ipend, sa1110_bus_ipend),
149 KOBJMETHOD(uart_param, sa1110_bus_param),
150 KOBJMETHOD(uart_receive, sa1110_bus_receive),
151 KOBJMETHOD(uart_setsig, sa1110_bus_setsig),
152 KOBJMETHOD(uart_transmit, sa1110_bus_transmit),
158 sa1110_bus_probe(struct uart_softc *sc)
164 sa1110_bus_attach(struct uart_softc *sc)
166 bcopy(&sc->sc_sysdev->bas, &sc->sc_bas, sizeof(sc->sc_bas));
171 uart_setreg(&sc->sc_bas, SACOM_CR3, CR3_RXE | CR3_TXE | CR3_RIE | CR3_TIE);
175 sa1110_bus_transmit(struct uart_softc *sc)
179 int sr = uart_getreg(&sc->sc_bas, SACOM_SR0);
181 while (!(uart_getreg(&sc->sc_bas, SACOM_CR3) & CR3_TIE))
182 uart_setreg(&sc->sc_bas, SACOM_CR3,
183 uart_getreg(&sc->sc_bas, SACOM_CR3) | CR3_TIE);
187 uart_setreg(&sc->sc_bas, SACOM_CR3, uart_getreg(&sc->sc_bas, SACOM_CR3)
189 for (i = 0; i < sc->sc_txdatasz; i++) {
190 while (!uart_getreg(&sc->sc_bas, SACOM_SR1) & SR1_TNF);
192 uart_setreg(&sc->sc_bas, SACOM_DR, sc->sc_txbuf[i]);
193 uart_barrier(&sc->sc_bas);
196 sr = uart_getreg(&sc->sc_bas, SACOM_SR0);
202 sa1110_bus_setsig(struct uart_softc *sc, int sig)
207 sa1110_bus_receive(struct uart_softc *sc)
211 while (!(uart_getreg(&sc->sc_bas, SACOM_SR1) & SR1_RNE)) {
214 sr0 = uart_getreg(&sc->sc_bas, SACOM_SR0);
215 if (ISSET(sr0, SR0_RBB))
216 uart_setreg(&sc->sc_bas, SACOM_SR0, SR0_RBB);
217 if (ISSET(sr0, SR0_REB))
218 uart_setreg(&sc->sc_bas, SACOM_SR0, SR0_REB);
222 uart_setreg(&sc->sc_bas, SACOM_CR3, uart_getreg(&sc->sc_bas, SACOM_CR3)
224 uart_rx_put(sc, uart_getreg(&sc->sc_bas, SACOM_DR));
228 sa1110_bus_param(struct uart_softc *sc, int baudrate, int databits,
229 int stopbits, int parity)
234 brd = SACOMSPEED(baudrate);
235 uart_setreg(&sc->sc_bas, SACOM_CR1, brd >> 8);
236 uart_setreg(&sc->sc_bas, SACOM_CR2, brd & 0xff);
241 sa1110_bus_ipend(struct uart_softc *sc)
243 int sr = uart_getreg(&sc->sc_bas, SACOM_SR0);
245 int mask = CR3_RIE | CR3_TIE;
247 if (uart_getreg(&sc->sc_bas, SACOM_CR3) & CR3_TIE)
248 ipend |= SER_INT_TXIDLE;
252 if (uart_getreg(&sc->sc_bas, SACOM_CR3) & CR3_RIE)
253 ipend |= SER_INT_RXREADY;
256 uart_setreg(&sc->sc_bas, SACOM_CR3, CR3_RXE | mask);
260 sa1110_bus_flush(struct uart_softc *sc, int what)
266 sa1110_bus_getsig(struct uart_softc *sc)
272 sa1110_bus_ioctl(struct uart_softc *sc, int request, intptr_t data)
276 struct uart_class uart_sa1110_class = {