uart.c revision 1.1 1 1.1 dyoung /* $NetBSD: uart.c,v 1.1 2007/03/20 08:52:02 dyoung Exp $ */
2 1.1 dyoung
3 1.1 dyoung /*-
4 1.1 dyoung * Copyright (c) 2007 Ruslan Ermilov and Vsevolod Lobko.
5 1.1 dyoung * All rights reserved.
6 1.1 dyoung *
7 1.1 dyoung * Redistribution and use in source and binary forms, with or
8 1.1 dyoung * without modification, are permitted provided that the following
9 1.1 dyoung * conditions are met:
10 1.1 dyoung * 1. Redistributions of source code must retain the above copyright
11 1.1 dyoung * notice, this list of conditions and the following disclaimer.
12 1.1 dyoung * 2. Redistributions in binary form must reproduce the above
13 1.1 dyoung * copyright notice, this list of conditions and the following
14 1.1 dyoung * disclaimer in the documentation and/or other materials provided
15 1.1 dyoung * with the distribution.
16 1.1 dyoung * 3. The names of the authors may not be used to endorse or promote
17 1.1 dyoung * products derived from this software without specific prior
18 1.1 dyoung * written permission.
19 1.1 dyoung *
20 1.1 dyoung * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY
21 1.1 dyoung * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22 1.1 dyoung * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
23 1.1 dyoung * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS
24 1.1 dyoung * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
25 1.1 dyoung * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26 1.1 dyoung * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
27 1.1 dyoung * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 1.1 dyoung * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
29 1.1 dyoung * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
30 1.1 dyoung * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
31 1.1 dyoung * OF SUCH DAMAGE.
32 1.1 dyoung */
33 1.1 dyoung
34 1.1 dyoung #include <sys/cdefs.h>
35 1.1 dyoung __KERNEL_RCSID(0, "$NetBSD: uart.c,v 1.1 2007/03/20 08:52:02 dyoung Exp $");
36 1.1 dyoung
37 1.1 dyoung #include <sys/types.h>
38 1.1 dyoung #include <sys/param.h>
39 1.1 dyoung #include <sys/systm.h>
40 1.1 dyoung #include <sys/kernel.h>
41 1.1 dyoung #include <sys/time.h>
42 1.1 dyoung #include <sys/device.h>
43 1.1 dyoung
44 1.1 dyoung #include <sys/proc.h>
45 1.1 dyoung #include <sys/user.h>
46 1.1 dyoung #include <sys/buf.h>
47 1.1 dyoung #include <sys/ioctl.h>
48 1.1 dyoung #include <sys/kauth.h>
49 1.1 dyoung #include <sys/tty.h>
50 1.1 dyoung #include <sys/file.h>
51 1.1 dyoung #include <sys/conf.h>
52 1.1 dyoung #include <sys/vnode.h>
53 1.1 dyoung
54 1.1 dyoung #include <machine/intr.h>
55 1.1 dyoung #include <machine/bus.h>
56 1.1 dyoung
57 1.1 dyoung #include <mips/adm5120/include/adm5120var.h>
58 1.1 dyoung #include <mips/adm5120/include/adm5120_obiovar.h>
59 1.1 dyoung #include <dev/cons.h>
60 1.1 dyoung #include <mips/adm5120/dev/uart.h>
61 1.1 dyoung
62 1.1 dyoung #define REG_READ(o) bus_space_read_4(sc->sc_st, sc->sc_ioh, (o))
63 1.1 dyoung #define REG_WRITE(o,v) bus_space_write_4(sc->sc_st, sc->sc_ioh, (o),(v))
64 1.1 dyoung
65 1.1 dyoung cons_decl(uart_);
66 1.1 dyoung
67 1.1 dyoung extern struct consdev *cn_tab; /* physical console device info */
68 1.1 dyoung
69 1.1 dyoung dev_type_open(uart_open);
70 1.1 dyoung dev_type_open(uart_close);
71 1.1 dyoung dev_type_read(uart_read);
72 1.1 dyoung dev_type_write(uart_write);
73 1.1 dyoung dev_type_ioctl(uart_ioctl);
74 1.1 dyoung dev_type_tty(uart_tty);
75 1.1 dyoung dev_type_poll(uart_poll);
76 1.1 dyoung dev_type_stop(uart_stop);
77 1.1 dyoung
78 1.1 dyoung const struct cdevsw uart_cdevsw = {
79 1.1 dyoung uart_open, uart_close, uart_read, uart_write, uart_ioctl,
80 1.1 dyoung uart_stop, uart_tty, uart_poll, nommap, ttykqfilter, D_TTY
81 1.1 dyoung };
82 1.1 dyoung
83 1.1 dyoung
84 1.1 dyoung struct consdev uartcons = {
85 1.1 dyoung NULL, NULL, uart_cngetc, uart_cnputc, uart_cnpollc, NULL, NULL, NULL,
86 1.1 dyoung NODEV, CN_NORMAL
87 1.1 dyoung };
88 1.1 dyoung
89 1.1 dyoung struct uart_softc {
90 1.1 dyoung struct device sc_dev;
91 1.1 dyoung struct tty *sc_tty;
92 1.1 dyoung
93 1.1 dyoung bus_space_tag_t sc_st;
94 1.1 dyoung bus_space_handle_t sc_ioh;
95 1.1 dyoung void *sc_ih;
96 1.1 dyoung };
97 1.1 dyoung
98 1.1 dyoung extern struct cfdriver uart_cd;
99 1.1 dyoung static int uart_consattached;
100 1.1 dyoung
101 1.1 dyoung static int uart_probe (struct device *, struct cfdata *, void *);
102 1.1 dyoung static void uart_attach (struct device *, struct device *, void *);
103 1.1 dyoung
104 1.1 dyoung void uart_start(struct tty *);
105 1.1 dyoung int uart_param(struct tty *, struct termios *);
106 1.1 dyoung int uart_intr(void *);
107 1.1 dyoung
108 1.1 dyoung CFATTACH_DECL(uart, sizeof(struct uart_softc),
109 1.1 dyoung uart_probe, uart_attach, NULL, NULL);
110 1.1 dyoung
111 1.1 dyoung static int
112 1.1 dyoung uart_probe(struct device *parent, struct cfdata *cf, void *aux)
113 1.1 dyoung {
114 1.1 dyoung struct obio_attach_args *aa = aux;
115 1.1 dyoung
116 1.1 dyoung if (strcmp(aa->oba_name, cf->cf_name) == 0)
117 1.1 dyoung return (1);
118 1.1 dyoung
119 1.1 dyoung return (0);
120 1.1 dyoung }
121 1.1 dyoung
122 1.1 dyoung static void
123 1.1 dyoung uart_attach(struct device *parent, struct device *self, void *aux)
124 1.1 dyoung {
125 1.1 dyoung struct obio_attach_args *oba = aux;
126 1.1 dyoung struct uart_softc *sc = (struct uart_softc *)self;
127 1.1 dyoung struct tty *tp;
128 1.1 dyoung int maj, minor;
129 1.1 dyoung
130 1.1 dyoung sc->sc_st = oba->oba_st;
131 1.1 dyoung if (bus_space_map(oba->oba_st, oba->oba_addr, 256, 0,
132 1.1 dyoung &sc->sc_ioh)) {
133 1.1 dyoung printf("%s: unable to map device\n", sc->sc_dev.dv_xname);
134 1.1 dyoung return;
135 1.1 dyoung }
136 1.1 dyoung
137 1.1 dyoung /* Establish the interrupt. */
138 1.1 dyoung sc->sc_ih = adm5120_intr_establish(oba->oba_irq, INTR_FIQ, uart_intr, sc);
139 1.1 dyoung if (sc->sc_ih == NULL) {
140 1.1 dyoung printf("%s: unable to establish interrupt\n",
141 1.1 dyoung sc->sc_dev.dv_xname);
142 1.1 dyoung return;
143 1.1 dyoung }
144 1.1 dyoung REG_WRITE(UART_CR_REG,UART_CR_PORT_EN|UART_CR_RX_INT_EN|UART_CR_RX_TIMEOUT_INT_EN);
145 1.1 dyoung
146 1.1 dyoung maj = cdevsw_lookup_major(&uart_cdevsw);
147 1.1 dyoung minor = sc->sc_dev.dv_unit;
148 1.1 dyoung
149 1.1 dyoung tp = ttymalloc();
150 1.1 dyoung tp->t_oproc = uart_start;
151 1.1 dyoung tp->t_param = uart_param;
152 1.1 dyoung sc->sc_tty = tp;
153 1.1 dyoung tp->t_dev = makedev(maj, minor);
154 1.1 dyoung tty_attach(tp);
155 1.1 dyoung if (minor == 0 && uart_consattached) {
156 1.1 dyoung /* attach as console*/
157 1.1 dyoung cn_tab->cn_dev = tp->t_dev;
158 1.1 dyoung printf(" console");
159 1.1 dyoung }
160 1.1 dyoung printf("\n");
161 1.1 dyoung }
162 1.1 dyoung
163 1.1 dyoung int
164 1.1 dyoung uart_cnattach(void)
165 1.1 dyoung {
166 1.1 dyoung cn_tab = &uartcons;
167 1.1 dyoung uart_consattached = 1;
168 1.1 dyoung return (0);
169 1.1 dyoung }
170 1.1 dyoung
171 1.1 dyoung void
172 1.1 dyoung uart_cnputc(dev_t dev, int c)
173 1.1 dyoung {
174 1.1 dyoung char chr;
175 1.1 dyoung chr = c;
176 1.1 dyoung while ((*((volatile unsigned long *)0xb2600018)) & 0x20) ;
177 1.1 dyoung (*((volatile unsigned long *)0xb2600000)) = c;
178 1.1 dyoung }
179 1.1 dyoung
180 1.1 dyoung int
181 1.1 dyoung uart_cngetc(dev_t dev)
182 1.1 dyoung {
183 1.1 dyoung while ((*((volatile unsigned long *)0xb2600018)) & 0x10) ;
184 1.1 dyoung return (*((volatile unsigned long *)0xb2600000)) & 0xff;
185 1.1 dyoung }
186 1.1 dyoung
187 1.1 dyoung void
188 1.1 dyoung uart_cnpollc(dev_t dev, int on)
189 1.1 dyoung {
190 1.1 dyoung
191 1.1 dyoung }
192 1.1 dyoung
193 1.1 dyoung
194 1.1 dyoung /*
195 1.1 dyoung * TTY device
196 1.1 dyoung */
197 1.1 dyoung
198 1.1 dyoung int
199 1.1 dyoung uart_open(dev_t dev, int flag, int mode, struct lwp *l)
200 1.1 dyoung {
201 1.1 dyoung struct uart_softc *sc = device_lookup(&uart_cd, minor(dev));
202 1.1 dyoung struct tty *tp = sc->sc_tty;
203 1.1 dyoung int s, error = 0;
204 1.1 dyoung
205 1.1 dyoung s = spltty();
206 1.1 dyoung
207 1.1 dyoung tp->t_dev = dev;
208 1.1 dyoung if ((tp->t_state & TS_ISOPEN) == 0) {
209 1.1 dyoung tp->t_state |= TS_CARR_ON;
210 1.1 dyoung ttychars(tp);
211 1.1 dyoung tp->t_iflag = TTYDEF_IFLAG;
212 1.1 dyoung tp->t_oflag = TTYDEF_OFLAG;
213 1.1 dyoung tp->t_cflag = TTYDEF_CFLAG | CLOCAL;
214 1.1 dyoung tp->t_lflag = TTYDEF_LFLAG;
215 1.1 dyoung tp->t_ispeed = tp->t_ospeed = 115200;
216 1.1 dyoung ttsetwater(tp);
217 1.1 dyoung } else if (tp->t_state & TS_XCLUDE &&
218 1.1 dyoung kauth_authorize_generic(l->l_proc->p_cred, KAUTH_GENERIC_ISSUSER, &l->l_proc->p_acflag) != 0) {
219 1.1 dyoung splx(s);
220 1.1 dyoung return (EBUSY);
221 1.1 dyoung }
222 1.1 dyoung
223 1.1 dyoung splx(s);
224 1.1 dyoung
225 1.1 dyoung error = (*tp->t_linesw->l_open)(dev, tp);
226 1.1 dyoung
227 1.1 dyoung return (error);
228 1.1 dyoung }
229 1.1 dyoung
230 1.1 dyoung int
231 1.1 dyoung uart_close(dev_t dev, int flag, int mode, struct lwp *l)
232 1.1 dyoung {
233 1.1 dyoung struct uart_softc *sc = device_lookup(&uart_cd, minor(dev));
234 1.1 dyoung struct tty *tp = sc->sc_tty;
235 1.1 dyoung
236 1.1 dyoung (*tp->t_linesw->l_close)(tp, flag);
237 1.1 dyoung ttyclose(tp);
238 1.1 dyoung return (0);
239 1.1 dyoung }
240 1.1 dyoung
241 1.1 dyoung
242 1.1 dyoung int
243 1.1 dyoung uart_read(dev_t dev, struct uio *uio, int flag)
244 1.1 dyoung {
245 1.1 dyoung struct uart_softc *sc = device_lookup(&uart_cd, minor(dev));
246 1.1 dyoung struct tty *tp = sc->sc_tty;
247 1.1 dyoung
248 1.1 dyoung return ((*tp->t_linesw->l_read)(tp, uio, flag));
249 1.1 dyoung }
250 1.1 dyoung
251 1.1 dyoung int
252 1.1 dyoung uart_write(dev_t dev, struct uio *uio, int flag)
253 1.1 dyoung {
254 1.1 dyoung struct uart_softc *sc = device_lookup(&uart_cd, minor(dev));
255 1.1 dyoung struct tty *tp = sc->sc_tty;
256 1.1 dyoung
257 1.1 dyoung return ((*tp->t_linesw->l_write)(tp, uio, flag));
258 1.1 dyoung }
259 1.1 dyoung
260 1.1 dyoung int
261 1.1 dyoung uart_poll(dev_t dev, int events, struct lwp *l)
262 1.1 dyoung {
263 1.1 dyoung struct uart_softc *sc = device_lookup(&uart_cd, minor(dev));
264 1.1 dyoung struct tty *tp = sc->sc_tty;
265 1.1 dyoung
266 1.1 dyoung return ((*tp->t_linesw->l_poll)(tp, events, l));
267 1.1 dyoung }
268 1.1 dyoung
269 1.1 dyoung int
270 1.1 dyoung uart_ioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
271 1.1 dyoung {
272 1.1 dyoung struct uart_softc *sc = device_lookup(&uart_cd, minor(dev));
273 1.1 dyoung struct tty *tp = sc->sc_tty;
274 1.1 dyoung int error;
275 1.1 dyoung
276 1.1 dyoung error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, l);
277 1.1 dyoung if (error != EPASSTHROUGH)
278 1.1 dyoung return (error);
279 1.1 dyoung return (ttioctl(tp, cmd, data, flag, l));
280 1.1 dyoung }
281 1.1 dyoung
282 1.1 dyoung int
283 1.1 dyoung uart_param(struct tty *tp, struct termios *t)
284 1.1 dyoung {
285 1.1 dyoung
286 1.1 dyoung return (0);
287 1.1 dyoung }
288 1.1 dyoung
289 1.1 dyoung struct tty*
290 1.1 dyoung uart_tty(dev)
291 1.1 dyoung dev_t dev;
292 1.1 dyoung {
293 1.1 dyoung struct uart_softc *sc = device_lookup(&uart_cd, minor(dev));
294 1.1 dyoung
295 1.1 dyoung return sc->sc_tty;
296 1.1 dyoung }
297 1.1 dyoung
298 1.1 dyoung
299 1.1 dyoung void
300 1.1 dyoung uart_start(struct tty *tp)
301 1.1 dyoung {
302 1.1 dyoung int s,i,cnt;
303 1.1 dyoung
304 1.1 dyoung s = spltty();
305 1.1 dyoung if (tp->t_state & (TS_TTSTOP | TS_BUSY))
306 1.1 dyoung goto out;
307 1.1 dyoung if (tp->t_outq.c_cc <= tp->t_lowat) {
308 1.1 dyoung if (tp->t_state & TS_ASLEEP) {
309 1.1 dyoung tp->t_state &= ~TS_ASLEEP;
310 1.1 dyoung wakeup((caddr_t)&tp->t_outq);
311 1.1 dyoung }
312 1.1 dyoung selwakeup(&tp->t_wsel);
313 1.1 dyoung }
314 1.1 dyoung tp->t_state |= TS_BUSY;
315 1.1 dyoung while (tp->t_outq.c_cc != 0) {
316 1.1 dyoung cnt = ndqb(&tp->t_outq, 0);
317 1.1 dyoung for (i=0; i<cnt; i++)
318 1.1 dyoung uart_cnputc(0,tp->t_outq.c_cf[i]);
319 1.1 dyoung ndflush(&tp->t_outq, cnt);
320 1.1 dyoung }
321 1.1 dyoung tp->t_state &= ~TS_BUSY;
322 1.1 dyoung out:
323 1.1 dyoung splx(s);
324 1.1 dyoung }
325 1.1 dyoung
326 1.1 dyoung void
327 1.1 dyoung uart_stop(struct tty *tp, int flag)
328 1.1 dyoung {
329 1.1 dyoung int s;
330 1.1 dyoung
331 1.1 dyoung s = spltty();
332 1.1 dyoung if (tp->t_state & TS_BUSY)
333 1.1 dyoung if ((tp->t_state & TS_TTSTOP) == 0)
334 1.1 dyoung tp->t_state |= TS_FLUSH;
335 1.1 dyoung splx(s);
336 1.1 dyoung }
337 1.1 dyoung
338 1.1 dyoung int
339 1.1 dyoung uart_intr(void *v)
340 1.1 dyoung {
341 1.1 dyoung struct uart_softc *sc = v;
342 1.1 dyoung struct tty *tp = sc->sc_tty;
343 1.1 dyoung int c, l_r;
344 1.1 dyoung
345 1.1 dyoung if (REG_READ(UART_RSR_REG) & UART_RSR_BE) {
346 1.1 dyoung REG_WRITE(UART_ECR_REG, UART_ECR_RSR);
347 1.1 dyoung console_debugger();
348 1.1 dyoung }
349 1.1 dyoung
350 1.1 dyoung while ((REG_READ(UART_FR_REG) & UART_FR_RX_FIFO_EMPTY) == 0) {
351 1.1 dyoung c = REG_READ(UART_DR_REG) & 0xff;
352 1.1 dyoung if (tp->t_state & TS_ISOPEN)
353 1.1 dyoung l_r = (*tp->t_linesw->l_rint)(c, tp);
354 1.1 dyoung }
355 1.1 dyoung return 0;
356 1.1 dyoung }
357