]> CyberLeo.Net >> Repos - FreeBSD/releng/10.0.git/blob - sys/arm/sa11x0/uart_dev_sa1110.c
- Copy stable/10 (r259064) to releng/10.0 as part of the
[FreeBSD/releng/10.0.git] / sys / arm / sa11x0 / uart_dev_sa1110.c
1 /*-
2  * Copyright (c) 2003 Marcel Moolenaar
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
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.
14  *
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.
25  */
26
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/bus.h>
33 #include <sys/conf.h>
34 #include <sys/cons.h>
35 #include <sys/tty.h>
36 #include <machine/bus.h>
37
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>
43
44 #include "uart_if.h"
45
46 #define      DEFAULT_RCLK    3686400
47
48 /*
49  * Low-level UART interface.
50  */
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_rxready(struct uart_bas *bas);
56 static int sa1110_getc(struct uart_bas *bas, struct mtx *mtx);
57
58 extern SLIST_HEAD(uart_devinfo_list, uart_devinfo) uart_sysdevs;
59
60 static struct uart_ops uart_sa1110_ops = {
61         .probe = sa1110_probe,
62         .init = sa1110_init,
63         .term = sa1110_term,
64         .putc = sa1110_putc,
65         .rxready = sa1110_rxready,
66         .getc = sa1110_getc,
67 };
68
69 static int
70 sa1110_probe(struct uart_bas *bas)
71 {
72         return (0);
73 }
74
75 static void
76 sa1110_init(struct uart_bas *bas, int baudrate, int databits, int stopbits,
77     int parity)
78 {
79         int brd;
80         
81         if (bas->rclk == 0)
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);
89 }
90
91 static void
92 sa1110_term(struct uart_bas *bas)
93 {
94         /* XXX */
95 }
96
97 static void
98 sa1110_putc(struct uart_bas *bas, int c)
99 {
100         while (!(uart_getreg(bas, SACOM_SR1) & SR1_TNF));
101         uart_setreg(bas, SACOM_DR, c);
102 }
103
104 static int
105 sa1110_rxready(struct uart_bas *bas)
106 {
107
108         return ((uart_getreg(bas, SACOM_SR1) & SR1_RNE) != 0 ? 1 : 0);
109 }
110
111 static int
112 sa1110_getc(struct uart_bas *bas, struct mtx *mtx)
113 {
114         int c;
115
116         while (!(uart_getreg(bas, SACOM_SR1) & SR1_RNE)) {
117                 u_int32_t sr0;
118
119                 sr0 = uart_getreg(bas, SACOM_SR0);
120                 if (ISSET(sr0, SR0_RBB))
121                         uart_setreg(bas, SACOM_SR0, SR0_RBB);
122                 if (ISSET(sr0, SR0_REB))
123                         uart_setreg(bas, SACOM_SR0, SR0_REB);
124         }
125         c = uart_getreg(bas, SACOM_DR);
126         c &= 0xff;
127         return (c);
128 }
129
130 static int sa1110_bus_probe(struct uart_softc *sc);
131 static int sa1110_bus_attach(struct uart_softc *sc);
132 static int sa1110_bus_flush(struct uart_softc *, int);
133 static int sa1110_bus_getsig(struct uart_softc *);
134 static int sa1110_bus_ioctl(struct uart_softc *, int, intptr_t);
135 static int sa1110_bus_ipend(struct uart_softc *);
136 static int sa1110_bus_param(struct uart_softc *, int, int, int, int);
137 static int sa1110_bus_receive(struct uart_softc *);
138 static int sa1110_bus_setsig(struct uart_softc *, int);
139 static int sa1110_bus_transmit(struct uart_softc *);
140
141 static kobj_method_t sa1110_methods[] = {
142         KOBJMETHOD(uart_probe,          sa1110_bus_probe),
143         KOBJMETHOD(uart_attach,         sa1110_bus_attach),
144         KOBJMETHOD(uart_flush,          sa1110_bus_flush),
145         KOBJMETHOD(uart_getsig,         sa1110_bus_getsig),
146         KOBJMETHOD(uart_ioctl,          sa1110_bus_ioctl),
147         KOBJMETHOD(uart_ipend,          sa1110_bus_ipend),
148         KOBJMETHOD(uart_param,          sa1110_bus_param),
149         KOBJMETHOD(uart_receive,        sa1110_bus_receive),
150         KOBJMETHOD(uart_setsig,         sa1110_bus_setsig),
151         KOBJMETHOD(uart_transmit,       sa1110_bus_transmit),
152         
153         {0, 0 }
154 };
155
156 int
157 sa1110_bus_probe(struct uart_softc *sc)
158 {
159         sc->sc_txfifosz = 3;
160         sc->sc_rxfifosz = 1;
161         return (0);
162 }
163
164 static int
165 sa1110_bus_attach(struct uart_softc *sc)
166 {
167          bcopy(&sc->sc_sysdev->bas, &sc->sc_bas, sizeof(sc->sc_bas));
168
169          sc->sc_hwiflow = 0;
170          uart_setreg(&sc->sc_bas, SACOM_CR3, CR3_RXE | CR3_TXE | CR3_RIE | CR3_TIE);
171         return (0);
172 }
173 static int
174 sa1110_bus_transmit(struct uart_softc *sc)
175 {
176         int i;
177 #if 0
178         int sr = uart_getreg(&sc->sc_bas, SACOM_SR0);
179
180         while (!(uart_getreg(&sc->sc_bas, SACOM_CR3) & CR3_TIE))
181                 uart_setreg(&sc->sc_bas, SACOM_CR3,
182                     uart_getreg(&sc->sc_bas, SACOM_CR3) | CR3_TIE);
183 #endif
184
185         sc->sc_txbusy = 1;
186         uart_setreg(&sc->sc_bas, SACOM_CR3, uart_getreg(&sc->sc_bas, SACOM_CR3)
187             | CR3_TIE);
188         for (i = 0; i < sc->sc_txdatasz; i++) {
189                 while (!(uart_getreg(&sc->sc_bas, SACOM_SR1) & SR1_TNF));
190
191                 uart_setreg(&sc->sc_bas, SACOM_DR, sc->sc_txbuf[i]);
192                 uart_barrier(&sc->sc_bas);
193         }
194 #if 0
195         sr = uart_getreg(&sc->sc_bas, SACOM_SR0);
196 #endif
197
198         return (0);
199 }
200 static int
201 sa1110_bus_setsig(struct uart_softc *sc, int sig)
202 {
203         return (0);
204 }
205 static int
206 sa1110_bus_receive(struct uart_softc *sc)
207 {
208         
209 #if 0
210         while (!(uart_getreg(&sc->sc_bas, SACOM_SR1) & SR1_RNE)) {
211                 u_int32_t sr0;
212
213                 sr0 = uart_getreg(&sc->sc_bas, SACOM_SR0);
214                 if (ISSET(sr0, SR0_RBB))
215                         uart_setreg(&sc->sc_bas, SACOM_SR0, SR0_RBB);
216                 if (ISSET(sr0, SR0_REB))
217                         uart_setreg(&sc->sc_bas, SACOM_SR0, SR0_REB);
218         }
219 #endif
220         
221         uart_setreg(&sc->sc_bas, SACOM_CR3, uart_getreg(&sc->sc_bas, SACOM_CR3)
222             | CR3_RIE);
223         uart_rx_put(sc, uart_getreg(&sc->sc_bas, SACOM_DR));
224         return (0);
225 }
226 static int
227 sa1110_bus_param(struct uart_softc *sc, int baudrate, int databits,
228     int stopbits, int parity)
229 {
230         int brd;
231         
232         if (baudrate > 0) {
233                 brd = SACOMSPEED(baudrate);
234                 uart_setreg(&sc->sc_bas, SACOM_CR1, brd >> 8);
235                 uart_setreg(&sc->sc_bas, SACOM_CR2, brd & 0xff);
236         }
237         return (0);
238 }
239 static int
240 sa1110_bus_ipend(struct uart_softc *sc)
241 {
242         int sr = uart_getreg(&sc->sc_bas, SACOM_SR0);
243         int ipend = 0;
244         int mask = CR3_RIE | CR3_TIE;
245         if (sr & 1) {
246                 if (uart_getreg(&sc->sc_bas, SACOM_CR3) & CR3_TIE)
247                         ipend |= SER_INT_TXIDLE;
248                 mask &= ~CR3_TIE;
249         }
250         if (sr & 4) {
251                 if (uart_getreg(&sc->sc_bas, SACOM_CR3) & CR3_RIE)
252                         ipend |= SER_INT_RXREADY;
253                 mask &= ~CR3_RIE;
254         }
255         uart_setreg(&sc->sc_bas, SACOM_CR3, CR3_RXE | mask);
256         return (ipend);
257 }
258 static int
259 sa1110_bus_flush(struct uart_softc *sc, int what)
260 {
261         return (0);
262 }
263
264 static int
265 sa1110_bus_getsig(struct uart_softc *sc)
266 {
267         return (0);
268 }
269
270 static int
271 sa1110_bus_ioctl(struct uart_softc *sc, int request, intptr_t data)
272 {
273         return (EINVAL);
274 }
275
276 struct uart_class uart_sa1110_class = {
277         "sa1110",
278         sa1110_methods,
279         1,
280         .uc_ops = &uart_sa1110_ops,
281         .uc_range = 8,
282         .uc_rclk = 3686400
283 };