]> CyberLeo.Net >> Repos - FreeBSD/releng/10.0.git/blob - sys/mips/rt305x/uart_dev_rt305x.c
- Copy stable/10 (r259064) to releng/10.0 as part of the
[FreeBSD/releng/10.0.git] / sys / mips / rt305x / uart_dev_rt305x.c
1 /* $NetBSD: uart.c,v 1.2 2007/03/23 20:05:47 dogcow Exp $ */
2
3 /*-
4  * Copyright (c) 2010 Aleksandr Rybalko.
5  * Copyright (c) 2007 Ruslan Ermilov and Vsevolod Lobko.
6  * Copyright (c) 2007 Oleksandr Tymoshenko.
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or
10  * without modification, are permitted provided that the following
11  * conditions are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above
15  *    copyright notice, this list of conditions and the following
16  *    disclaimer in the documentation and/or other materials provided
17  *    with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY
20  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
22  * PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
24  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
26  * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
28  * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
29  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
30  * OF SUCH DAMAGE.
31  */
32
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
35
36 #include "opt_ddb.h"
37
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/bus.h>
41 #include <sys/conf.h>
42 #include <sys/kdb.h>
43 #include <sys/reboot.h>
44 #include <sys/sysctl.h>
45 #include <sys/kernel.h>
46 #include <machine/bus.h>
47
48 #include <dev/uart/uart.h>
49 #include <dev/uart/uart_cpu.h>
50 #include <dev/uart/uart_bus.h>
51
52 #include <mips/rt305x/uart_dev_rt305x.h>
53 #include <mips/rt305x/rt305xreg.h>
54
55 #include "uart_if.h"
56 /*
57  * Low-level UART interface.
58  */
59 static int rt305x_uart_probe(struct uart_bas *bas);
60 static void rt305x_uart_init(struct uart_bas *bas, int, int, int, int);
61 static void rt305x_uart_term(struct uart_bas *bas);
62 static void rt305x_uart_putc(struct uart_bas *bas, int);
63 static int rt305x_uart_rxready(struct uart_bas *bas);
64 static int rt305x_uart_getc(struct uart_bas *bas, struct mtx *);
65
66 static struct uart_ops uart_rt305x_uart_ops = {
67         .probe = rt305x_uart_probe,
68         .init = rt305x_uart_init,
69         .term = rt305x_uart_term,
70         .putc = rt305x_uart_putc,
71         .rxready = rt305x_uart_rxready,
72         .getc = rt305x_uart_getc,
73 };
74
75 static int      uart_output = 1;
76 TUNABLE_INT("kern.uart_output", &uart_output);
77 SYSCTL_INT(_kern, OID_AUTO, uart_output, CTLFLAG_RW,
78     &uart_output, 0, "UART output enabled.");
79
80
81
82
83 static int
84 rt305x_uart_probe(struct uart_bas *bas)
85 {
86
87         return (0);
88 }
89
90 static void
91 rt305x_uart_init(struct uart_bas *bas, int baudrate, int databits, 
92     int stopbits, int parity)
93 {
94 #ifdef notyet
95         /* CLKDIV  = 384000000/ 3/ 16/ br */
96         /* for 384MHz CLKDIV = 8000000 / baudrate; */
97         switch (databits) {
98         case 5:
99                 databits = UART_LCR_5B;
100                 break;
101         case 6:
102                 databits = UART_LCR_6B;
103                 break;
104         case 7:
105                 databits = UART_LCR_7B;
106                 break;
107         case 8:
108                 databits = UART_LCR_8B;
109                 break;
110         default:
111                 /* Unsupported */
112                 return;
113         }
114         switch (parity) {
115         case UART_PARITY_EVEN:  parity = (UART_LCR_PEN|UART_LCR_EVEN); break;
116         case UART_PARITY_NONE:  parity = (UART_LCR_PEN); break;
117         case UART_PARITY_ODD:   parity = 0; break;
118         /* Unsupported */
119         default:                return;
120         }
121         uart_setreg(bas, UART_CDDL_REG, 8000000/baudrate);
122         uart_barrier(bas);
123         uart_setreg(bas, UART_LCR_REG, databits | (stopbits==1?0:4) | parity);
124         uart_barrier(bas);
125 #endif
126 }
127
128 static void
129 rt305x_uart_term(struct uart_bas *bas)
130 {
131         uart_setreg(bas, UART_MCR_REG, 0);
132         uart_barrier(bas);
133 }
134
135 static void
136 rt305x_uart_putc(struct uart_bas *bas, int c)
137 {
138         char chr;
139         if (!uart_output) return;
140         chr = c;
141         while (!(uart_getreg(bas, UART_LSR_REG) & UART_LSR_THRE));
142         uart_setreg(bas, UART_TX_REG, c);
143         uart_barrier(bas);
144         while (!(uart_getreg(bas, UART_LSR_REG) & UART_LSR_THRE));
145 }
146
147 static int
148 rt305x_uart_rxready(struct uart_bas *bas)
149 {
150 #ifdef notyet
151         if (uart_getreg(bas, UART_LSR_REG) & UART_LSR_DR)
152                 return (1);
153
154         return (0);
155 #else
156         return (1);
157 #endif
158 }
159
160 static int
161 rt305x_uart_getc(struct uart_bas *bas, struct mtx *hwmtx)
162 {
163         int c;
164
165         uart_lock(hwmtx);
166
167         while (!(uart_getreg(bas, UART_LSR_REG) & UART_LSR_DR)) {
168                 uart_unlock(hwmtx);
169                 DELAY(10);
170                 uart_lock(hwmtx);
171         }
172
173         c = uart_getreg(bas, UART_RX_REG);
174
175         uart_unlock(hwmtx);
176
177         return (c);
178 }
179
180 /*
181  * High-level UART interface.
182  */
183 struct rt305x_uart_softc {
184         struct uart_softc base;
185 };
186
187 static int rt305x_uart_bus_attach(struct uart_softc *);
188 static int rt305x_uart_bus_detach(struct uart_softc *);
189 static int rt305x_uart_bus_flush(struct uart_softc *, int);
190 static int rt305x_uart_bus_getsig(struct uart_softc *);
191 static int rt305x_uart_bus_ioctl(struct uart_softc *, int, intptr_t);
192 static int rt305x_uart_bus_ipend(struct uart_softc *);
193 static int rt305x_uart_bus_param(struct uart_softc *, int, int, int, int);
194 static int rt305x_uart_bus_probe(struct uart_softc *);
195 static int rt305x_uart_bus_receive(struct uart_softc *);
196 static int rt305x_uart_bus_setsig(struct uart_softc *, int);
197 static int rt305x_uart_bus_transmit(struct uart_softc *);
198
199 static kobj_method_t rt305x_uart_methods[] = {
200         KOBJMETHOD(uart_attach,         rt305x_uart_bus_attach),
201         KOBJMETHOD(uart_detach,         rt305x_uart_bus_detach),
202         KOBJMETHOD(uart_flush,          rt305x_uart_bus_flush),
203         KOBJMETHOD(uart_getsig,         rt305x_uart_bus_getsig),
204         KOBJMETHOD(uart_ioctl,          rt305x_uart_bus_ioctl),
205         KOBJMETHOD(uart_ipend,          rt305x_uart_bus_ipend),
206         KOBJMETHOD(uart_param,          rt305x_uart_bus_param),
207         KOBJMETHOD(uart_probe,          rt305x_uart_bus_probe),
208         KOBJMETHOD(uart_receive,        rt305x_uart_bus_receive),
209         KOBJMETHOD(uart_setsig,         rt305x_uart_bus_setsig),
210         KOBJMETHOD(uart_transmit,       rt305x_uart_bus_transmit),
211         { 0, 0 }
212 };
213
214 struct uart_class uart_rt305x_uart_class = {
215         "rt305x",
216         rt305x_uart_methods,
217         sizeof(struct rt305x_uart_softc),
218         .uc_ops = &uart_rt305x_uart_ops,
219         .uc_range = 1, /* use hinted range */
220         .uc_rclk = SYSTEM_CLOCK
221 };
222
223 #define SIGCHG(c, i, s, d)                              \
224         if (c) {                                        \
225                 i |= (i & s) ? s : s | d;               \
226         } else {                                        \
227                 i = (i & s) ? (i & ~s) | d : i;         \
228         }
229
230 /*
231  * Disable TX interrupt. uart should be locked 
232  */ 
233 static __inline void
234 rt305x_uart_disable_txintr(struct uart_softc *sc)
235 {
236         struct uart_bas *bas = &sc->sc_bas;
237         uint8_t cr;
238
239         cr = uart_getreg(bas, UART_IER_REG);
240         cr &= ~UART_IER_ETBEI;
241         uart_setreg(bas, UART_IER_REG, cr);
242         uart_barrier(bas);
243 }
244
245 /*
246  * Enable TX interrupt. uart should be locked 
247  */ 
248 static __inline void
249 rt305x_uart_enable_txintr(struct uart_softc *sc)
250 {
251         struct uart_bas *bas = &sc->sc_bas;
252         uint8_t cr;
253
254         cr = uart_getreg(bas, UART_IER_REG);
255         cr |= UART_IER_ETBEI;
256         uart_setreg(bas, UART_IER_REG, cr);
257         uart_barrier(bas);
258 }
259
260 static int
261 rt305x_uart_bus_attach(struct uart_softc *sc)
262 {
263         struct uart_bas *bas;
264         struct uart_devinfo *di;
265
266         bas = &sc->sc_bas;
267         if (sc->sc_sysdev != NULL) {
268                 di = sc->sc_sysdev;
269                 rt305x_uart_init(bas, di->baudrate, di->databits, di->stopbits,
270                     di->parity);
271         } else {
272                 rt305x_uart_init(bas, 115200, 8, 1, 0);
273         }
274
275         (void)rt305x_uart_bus_getsig(sc);
276
277         /* Enable FIFO */
278         uart_setreg(bas, UART_FCR_REG, 
279             uart_getreg(bas, UART_FCR_REG) | 
280             UART_FCR_FIFOEN | UART_FCR_TXTGR_1 | UART_FCR_RXTGR_1);
281         uart_barrier(bas);
282         /* Enable interrupts */
283         uart_setreg(bas, UART_IER_REG,
284             UART_IER_EDSSI | UART_IER_ELSI | UART_IER_ERBFI);
285         uart_barrier(bas);
286
287         return (0);
288 }
289
290 static int
291 rt305x_uart_bus_detach(struct uart_softc *sc)
292 {
293
294         return (0);
295 }
296
297 static int
298 rt305x_uart_bus_flush(struct uart_softc *sc, int what)
299 {
300         struct uart_bas *bas = &sc->sc_bas;
301         uint32_t fcr = uart_getreg(bas, UART_FCR_REG);
302         if (what & UART_FLUSH_TRANSMITTER) {
303                 uart_setreg(bas, UART_FCR_REG, fcr|UART_FCR_TXRST);
304                 uart_barrier(bas);
305         }
306         if (what & UART_FLUSH_RECEIVER) {
307                 uart_setreg(bas, UART_FCR_REG, fcr|UART_FCR_RXRST);
308                 uart_barrier(bas);
309         }
310         uart_setreg(bas, UART_FCR_REG, fcr);
311         uart_barrier(bas);
312         return (0);
313 }
314
315 static int
316 rt305x_uart_bus_getsig(struct uart_softc *sc)
317 {
318         uint32_t new, old, sig;
319         uint8_t bes;
320
321         do {
322                 old = sc->sc_hwsig;
323                 sig = old;
324                 uart_lock(sc->sc_hwmtx);
325                 bes = uart_getreg(&sc->sc_bas, UART_MSR_REG);
326                 uart_unlock(sc->sc_hwmtx);
327                 /* XXX: chip can show delta */
328                 SIGCHG(bes & UART_MSR_CTS, sig, SER_CTS, SER_DCTS);
329                 SIGCHG(bes & UART_MSR_DCD, sig, SER_DCD, SER_DDCD);
330                 SIGCHG(bes & UART_MSR_DSR, sig, SER_DSR, SER_DDSR);
331                 new = sig & ~SER_MASK_DELTA;
332         } while (!atomic_cmpset_32(&sc->sc_hwsig, old, new));
333
334         return (sig);
335 }
336
337 static int
338 rt305x_uart_bus_ioctl(struct uart_softc *sc, int request, intptr_t data)
339 {
340         struct uart_bas *bas;
341         int baudrate, divisor, error;
342
343         bas = &sc->sc_bas;
344         error = 0;
345         uart_lock(sc->sc_hwmtx);
346         switch (request) {
347         case UART_IOCTL_BREAK:
348                 /* TODO: Send BREAK */
349                 break;
350         case UART_IOCTL_BAUD:
351                 divisor = uart_getreg(bas, UART_CDDL_REG);
352                 baudrate = bas->rclk / (divisor * 16);
353                 *(int*)data = baudrate;
354                 break;
355         default:
356                 error = EINVAL;
357                 break;
358         }
359         uart_unlock(sc->sc_hwmtx);
360         return (error);
361 }
362
363 static int
364 rt305x_uart_bus_ipend(struct uart_softc *sc)
365 {
366         struct uart_bas *bas;
367         int ipend;
368         uint8_t iir, lsr, msr;
369
370         bas = &sc->sc_bas;
371         ipend = 0;
372
373         uart_lock(sc->sc_hwmtx);
374         iir = uart_getreg(&sc->sc_bas, UART_IIR_REG);
375         lsr = uart_getreg(&sc->sc_bas, UART_LSR_REG);
376         uart_setreg(&sc->sc_bas, UART_LSR_REG, lsr);
377         msr = uart_getreg(&sc->sc_bas, UART_MSR_REG);
378         uart_setreg(&sc->sc_bas, UART_MSR_REG, msr);
379         if (iir & UART_IIR_INTP) {
380                 uart_unlock(sc->sc_hwmtx);
381                 return (0);
382         }
383
384
385         switch ((iir >> 1) & 0x07) {
386         case UART_IIR_ID_THRE:
387                 ipend |= SER_INT_TXIDLE;
388                 break;
389         case UART_IIR_ID_DR2:
390                 rt305x_uart_bus_flush(sc, UART_FLUSH_RECEIVER);
391                 /* passthrough */
392         case UART_IIR_ID_DR:
393                 ipend |= SER_INT_RXREADY;
394                 break;
395         case UART_IIR_ID_MST:
396         case UART_IIR_ID_LINESTATUS:
397                 ipend |= SER_INT_SIGCHG;
398                 if (lsr & UART_LSR_BI)
399                 {
400                         ipend |= SER_INT_BREAK;
401 #ifdef KDB
402                         breakpoint();
403 #endif
404                 }
405                 if (lsr & UART_LSR_OE)
406                         ipend |= SER_INT_OVERRUN;
407                 break;
408         default:
409                 /* XXX: maybe return error here */
410                 break;
411         }
412
413         uart_unlock(sc->sc_hwmtx);
414
415         return (ipend);
416 }
417
418 static int
419 rt305x_uart_bus_param(struct uart_softc *sc, int baudrate, int databits,
420     int stopbits, int parity)
421 {
422         uart_lock(sc->sc_hwmtx);
423         rt305x_uart_init(&sc->sc_bas, baudrate, databits, stopbits, parity);
424         uart_unlock(sc->sc_hwmtx);
425         return (0);
426 }
427
428 static int
429 rt305x_uart_bus_probe(struct uart_softc *sc)
430 {
431         char buf[80];
432         int error;
433
434         error = rt305x_uart_probe(&sc->sc_bas);
435         if (error)
436                 return (error);
437
438         sc->sc_rxfifosz = 16;
439         sc->sc_txfifosz = 16;
440
441         snprintf(buf, sizeof(buf), "rt305x_uart");
442         device_set_desc_copy(sc->sc_dev, buf);
443
444         return (0);
445 }
446
447 static int
448 rt305x_uart_bus_receive(struct uart_softc *sc)
449 {
450         struct uart_bas *bas;
451         int xc;
452         uint8_t lsr;
453
454         bas = &sc->sc_bas;
455         uart_lock(sc->sc_hwmtx);
456         lsr = uart_getreg(bas, UART_LSR_REG);
457         while ((lsr & UART_LSR_DR)) {
458                 if (uart_rx_full(sc)) {
459                         sc->sc_rxbuf[sc->sc_rxput] = UART_STAT_OVERRUN;
460                         break;
461                 }
462                 xc = 0;
463                 xc = uart_getreg(bas, UART_RX_REG);
464                 if (lsr & UART_LSR_FE)
465                         xc |= UART_STAT_FRAMERR;
466                 if (lsr & UART_LSR_PE)
467                         xc |= UART_STAT_PARERR;
468                 if (lsr & UART_LSR_OE)
469                         xc |= UART_STAT_OVERRUN;
470                 uart_barrier(bas);
471                 uart_rx_put(sc, xc);
472                 lsr = uart_getreg(bas, UART_LSR_REG);
473         }
474
475         uart_unlock(sc->sc_hwmtx);
476         return (0);
477 }
478
479 static int
480 rt305x_uart_bus_setsig(struct uart_softc *sc, int sig)
481 {
482
483         /* TODO: implement (?) */
484         return (0);
485 }
486
487 static int
488 rt305x_uart_bus_transmit(struct uart_softc *sc)
489 {
490         struct uart_bas *bas = &sc->sc_bas;
491         int i;
492
493         if (!uart_output) return (0);
494
495         bas = &sc->sc_bas;
496         uart_lock(sc->sc_hwmtx);
497         while ((uart_getreg(bas, UART_LSR_REG) & UART_LSR_THRE) == 0)
498                 ;
499         rt305x_uart_enable_txintr(sc);
500         for (i = 0; i < sc->sc_txdatasz; i++) {
501                 uart_setreg(bas, UART_TX_REG, sc->sc_txbuf[i]);
502                 uart_barrier(bas);
503         }
504         sc->sc_txbusy = 1;
505         uart_unlock(sc->sc_hwmtx);
506         return (0);
507 }