uart.c revision 1.7 1 1.7 rmind /* $NetBSD: uart.c,v 1.7 2011/04/24 16:26:56 rmind 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.7 rmind __KERNEL_RCSID(0, "$NetBSD: uart.c,v 1.7 2011/04/24 16:26:56 rmind 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/buf.h>
46 1.1 dyoung #include <sys/ioctl.h>
47 1.1 dyoung #include <sys/kauth.h>
48 1.1 dyoung #include <sys/tty.h>
49 1.1 dyoung #include <sys/file.h>
50 1.1 dyoung #include <sys/conf.h>
51 1.1 dyoung #include <sys/vnode.h>
52 1.1 dyoung
53 1.1 dyoung #include <machine/intr.h>
54 1.1 dyoung #include <machine/bus.h>
55 1.1 dyoung
56 1.1 dyoung #include <mips/adm5120/include/adm5120var.h>
57 1.1 dyoung #include <mips/adm5120/include/adm5120_obiovar.h>
58 1.1 dyoung #include <dev/cons.h>
59 1.1 dyoung #include <mips/adm5120/dev/uart.h>
60 1.1 dyoung
61 1.1 dyoung #define REG_READ(o) bus_space_read_4(sc->sc_st, sc->sc_ioh, (o))
62 1.1 dyoung #define REG_WRITE(o,v) bus_space_write_4(sc->sc_st, sc->sc_ioh, (o),(v))
63 1.1 dyoung
64 1.1 dyoung cons_decl(uart_);
65 1.1 dyoung
66 1.1 dyoung extern struct consdev *cn_tab; /* physical console device info */
67 1.1 dyoung
68 1.1 dyoung dev_type_open(uart_open);
69 1.1 dyoung dev_type_open(uart_close);
70 1.1 dyoung dev_type_read(uart_read);
71 1.1 dyoung dev_type_write(uart_write);
72 1.1 dyoung dev_type_ioctl(uart_ioctl);
73 1.1 dyoung dev_type_tty(uart_tty);
74 1.1 dyoung dev_type_poll(uart_poll);
75 1.1 dyoung dev_type_stop(uart_stop);
76 1.1 dyoung
77 1.1 dyoung const struct cdevsw uart_cdevsw = {
78 1.1 dyoung uart_open, uart_close, uart_read, uart_write, uart_ioctl,
79 1.1 dyoung uart_stop, uart_tty, uart_poll, nommap, ttykqfilter, D_TTY
80 1.1 dyoung };
81 1.1 dyoung
82 1.1 dyoung
83 1.1 dyoung struct consdev uartcons = {
84 1.1 dyoung NULL, NULL, uart_cngetc, uart_cnputc, uart_cnpollc, NULL, NULL, NULL,
85 1.1 dyoung NODEV, CN_NORMAL
86 1.1 dyoung };
87 1.1 dyoung
88 1.1 dyoung struct uart_softc {
89 1.1 dyoung struct device sc_dev;
90 1.1 dyoung struct tty *sc_tty;
91 1.1 dyoung
92 1.1 dyoung bus_space_tag_t sc_st;
93 1.1 dyoung bus_space_handle_t sc_ioh;
94 1.1 dyoung void *sc_ih;
95 1.1 dyoung };
96 1.1 dyoung
97 1.1 dyoung extern struct cfdriver uart_cd;
98 1.1 dyoung static int uart_consattached;
99 1.1 dyoung
100 1.1 dyoung static int uart_probe (struct device *, struct cfdata *, void *);
101 1.1 dyoung static void uart_attach (struct device *, struct device *, void *);
102 1.1 dyoung
103 1.1 dyoung void uart_start(struct tty *);
104 1.1 dyoung int uart_param(struct tty *, struct termios *);
105 1.1 dyoung int uart_intr(void *);
106 1.1 dyoung
107 1.1 dyoung CFATTACH_DECL(uart, sizeof(struct uart_softc),
108 1.1 dyoung uart_probe, uart_attach, NULL, NULL);
109 1.1 dyoung
110 1.1 dyoung static int
111 1.1 dyoung uart_probe(struct device *parent, struct cfdata *cf, void *aux)
112 1.1 dyoung {
113 1.1 dyoung struct obio_attach_args *aa = aux;
114 1.1 dyoung
115 1.1 dyoung if (strcmp(aa->oba_name, cf->cf_name) == 0)
116 1.1 dyoung return (1);
117 1.1 dyoung
118 1.1 dyoung return (0);
119 1.1 dyoung }
120 1.1 dyoung
121 1.1 dyoung static void
122 1.1 dyoung uart_attach(struct device *parent, struct device *self, void *aux)
123 1.1 dyoung {
124 1.1 dyoung struct obio_attach_args *oba = aux;
125 1.1 dyoung struct uart_softc *sc = (struct uart_softc *)self;
126 1.1 dyoung struct tty *tp;
127 1.1 dyoung int maj, minor;
128 1.1 dyoung
129 1.1 dyoung sc->sc_st = oba->oba_st;
130 1.1 dyoung if (bus_space_map(oba->oba_st, oba->oba_addr, 256, 0,
131 1.1 dyoung &sc->sc_ioh)) {
132 1.1 dyoung printf("%s: unable to map device\n", sc->sc_dev.dv_xname);
133 1.1 dyoung return;
134 1.1 dyoung }
135 1.1 dyoung
136 1.1 dyoung /* Establish the interrupt. */
137 1.1 dyoung sc->sc_ih = adm5120_intr_establish(oba->oba_irq, INTR_FIQ, uart_intr, sc);
138 1.1 dyoung if (sc->sc_ih == NULL) {
139 1.1 dyoung printf("%s: unable to establish interrupt\n",
140 1.1 dyoung sc->sc_dev.dv_xname);
141 1.1 dyoung return;
142 1.1 dyoung }
143 1.1 dyoung REG_WRITE(UART_CR_REG,UART_CR_PORT_EN|UART_CR_RX_INT_EN|UART_CR_RX_TIMEOUT_INT_EN);
144 1.1 dyoung
145 1.1 dyoung maj = cdevsw_lookup_major(&uart_cdevsw);
146 1.1 dyoung minor = sc->sc_dev.dv_unit;
147 1.1 dyoung
148 1.7 rmind tp = tty_alloc();
149 1.1 dyoung tp->t_oproc = uart_start;
150 1.1 dyoung tp->t_param = uart_param;
151 1.1 dyoung sc->sc_tty = tp;
152 1.1 dyoung tp->t_dev = makedev(maj, minor);
153 1.1 dyoung tty_attach(tp);
154 1.1 dyoung if (minor == 0 && uart_consattached) {
155 1.1 dyoung /* attach as console*/
156 1.1 dyoung cn_tab->cn_dev = tp->t_dev;
157 1.1 dyoung printf(" console");
158 1.1 dyoung }
159 1.1 dyoung printf("\n");
160 1.1 dyoung }
161 1.1 dyoung
162 1.1 dyoung int
163 1.1 dyoung uart_cnattach(void)
164 1.1 dyoung {
165 1.1 dyoung cn_tab = &uartcons;
166 1.1 dyoung uart_consattached = 1;
167 1.1 dyoung return (0);
168 1.1 dyoung }
169 1.1 dyoung
170 1.1 dyoung void
171 1.1 dyoung uart_cnputc(dev_t dev, int c)
172 1.1 dyoung {
173 1.1 dyoung char chr;
174 1.1 dyoung chr = c;
175 1.1 dyoung while ((*((volatile unsigned long *)0xb2600018)) & 0x20) ;
176 1.1 dyoung (*((volatile unsigned long *)0xb2600000)) = c;
177 1.1 dyoung }
178 1.1 dyoung
179 1.1 dyoung int
180 1.1 dyoung uart_cngetc(dev_t dev)
181 1.1 dyoung {
182 1.1 dyoung while ((*((volatile unsigned long *)0xb2600018)) & 0x10) ;
183 1.1 dyoung return (*((volatile unsigned long *)0xb2600000)) & 0xff;
184 1.1 dyoung }
185 1.1 dyoung
186 1.1 dyoung void
187 1.1 dyoung uart_cnpollc(dev_t dev, int on)
188 1.1 dyoung {
189 1.1 dyoung
190 1.1 dyoung }
191 1.1 dyoung
192 1.1 dyoung
193 1.1 dyoung /*
194 1.1 dyoung * TTY device
195 1.1 dyoung */
196 1.1 dyoung
197 1.1 dyoung int
198 1.1 dyoung uart_open(dev_t dev, int flag, int mode, struct lwp *l)
199 1.1 dyoung {
200 1.5 cegger struct uart_softc *sc = device_lookup_private(&uart_cd, minor(dev));
201 1.1 dyoung struct tty *tp = sc->sc_tty;
202 1.1 dyoung int s, error = 0;
203 1.1 dyoung
204 1.1 dyoung s = spltty();
205 1.1 dyoung
206 1.1 dyoung tp->t_dev = dev;
207 1.1 dyoung if ((tp->t_state & TS_ISOPEN) == 0) {
208 1.1 dyoung tp->t_state |= TS_CARR_ON;
209 1.1 dyoung ttychars(tp);
210 1.1 dyoung tp->t_iflag = TTYDEF_IFLAG;
211 1.1 dyoung tp->t_oflag = TTYDEF_OFLAG;
212 1.1 dyoung tp->t_cflag = TTYDEF_CFLAG | CLOCAL;
213 1.1 dyoung tp->t_lflag = TTYDEF_LFLAG;
214 1.1 dyoung tp->t_ispeed = tp->t_ospeed = 115200;
215 1.1 dyoung ttsetwater(tp);
216 1.4 elad } else if (kauth_authorize_device_tty(l->l_cred, KAUTH_DEVICE_TTY_OPEN,
217 1.4 elad tp) != 0) {
218 1.1 dyoung splx(s);
219 1.1 dyoung return (EBUSY);
220 1.1 dyoung }
221 1.1 dyoung
222 1.1 dyoung splx(s);
223 1.1 dyoung
224 1.1 dyoung error = (*tp->t_linesw->l_open)(dev, tp);
225 1.1 dyoung
226 1.1 dyoung return (error);
227 1.1 dyoung }
228 1.1 dyoung
229 1.1 dyoung int
230 1.1 dyoung uart_close(dev_t dev, int flag, int mode, struct lwp *l)
231 1.1 dyoung {
232 1.5 cegger struct uart_softc *sc = device_lookup_private(&uart_cd, minor(dev));
233 1.1 dyoung struct tty *tp = sc->sc_tty;
234 1.1 dyoung
235 1.1 dyoung (*tp->t_linesw->l_close)(tp, flag);
236 1.1 dyoung ttyclose(tp);
237 1.1 dyoung return (0);
238 1.1 dyoung }
239 1.1 dyoung
240 1.1 dyoung
241 1.1 dyoung int
242 1.1 dyoung uart_read(dev_t dev, struct uio *uio, int flag)
243 1.1 dyoung {
244 1.5 cegger struct uart_softc *sc = device_lookup_private(&uart_cd, minor(dev));
245 1.1 dyoung struct tty *tp = sc->sc_tty;
246 1.1 dyoung
247 1.1 dyoung return ((*tp->t_linesw->l_read)(tp, uio, flag));
248 1.1 dyoung }
249 1.1 dyoung
250 1.1 dyoung int
251 1.1 dyoung uart_write(dev_t dev, struct uio *uio, int flag)
252 1.1 dyoung {
253 1.5 cegger struct uart_softc *sc = device_lookup_private(&uart_cd, minor(dev));
254 1.1 dyoung struct tty *tp = sc->sc_tty;
255 1.1 dyoung
256 1.1 dyoung return ((*tp->t_linesw->l_write)(tp, uio, flag));
257 1.1 dyoung }
258 1.1 dyoung
259 1.1 dyoung int
260 1.1 dyoung uart_poll(dev_t dev, int events, struct lwp *l)
261 1.1 dyoung {
262 1.5 cegger struct uart_softc *sc = device_lookup_private(&uart_cd, minor(dev));
263 1.1 dyoung struct tty *tp = sc->sc_tty;
264 1.1 dyoung
265 1.1 dyoung return ((*tp->t_linesw->l_poll)(tp, events, l));
266 1.1 dyoung }
267 1.1 dyoung
268 1.1 dyoung int
269 1.1 dyoung uart_ioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
270 1.1 dyoung {
271 1.5 cegger struct uart_softc *sc = device_lookup_private(&uart_cd, minor(dev));
272 1.1 dyoung struct tty *tp = sc->sc_tty;
273 1.1 dyoung int error;
274 1.1 dyoung
275 1.1 dyoung error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, l);
276 1.1 dyoung if (error != EPASSTHROUGH)
277 1.1 dyoung return (error);
278 1.1 dyoung return (ttioctl(tp, cmd, data, flag, l));
279 1.1 dyoung }
280 1.1 dyoung
281 1.1 dyoung int
282 1.1 dyoung uart_param(struct tty *tp, struct termios *t)
283 1.1 dyoung {
284 1.1 dyoung
285 1.1 dyoung return (0);
286 1.1 dyoung }
287 1.1 dyoung
288 1.1 dyoung struct tty*
289 1.5 cegger uart_tty(dev_t dev)
290 1.1 dyoung {
291 1.5 cegger struct uart_softc *sc = device_lookup_private(&uart_cd, minor(dev));
292 1.1 dyoung
293 1.1 dyoung return sc->sc_tty;
294 1.1 dyoung }
295 1.1 dyoung
296 1.1 dyoung
297 1.1 dyoung void
298 1.1 dyoung uart_start(struct tty *tp)
299 1.1 dyoung {
300 1.1 dyoung int s,i,cnt;
301 1.1 dyoung
302 1.1 dyoung s = spltty();
303 1.1 dyoung if (tp->t_state & (TS_TTSTOP | TS_BUSY))
304 1.1 dyoung goto out;
305 1.3 ad ttypull(tp);
306 1.1 dyoung tp->t_state |= TS_BUSY;
307 1.1 dyoung while (tp->t_outq.c_cc != 0) {
308 1.1 dyoung cnt = ndqb(&tp->t_outq, 0);
309 1.1 dyoung for (i=0; i<cnt; i++)
310 1.1 dyoung uart_cnputc(0,tp->t_outq.c_cf[i]);
311 1.1 dyoung ndflush(&tp->t_outq, cnt);
312 1.1 dyoung }
313 1.1 dyoung tp->t_state &= ~TS_BUSY;
314 1.1 dyoung out:
315 1.1 dyoung splx(s);
316 1.1 dyoung }
317 1.1 dyoung
318 1.1 dyoung void
319 1.1 dyoung uart_stop(struct tty *tp, int flag)
320 1.1 dyoung {
321 1.1 dyoung int s;
322 1.1 dyoung
323 1.1 dyoung s = spltty();
324 1.1 dyoung if (tp->t_state & TS_BUSY)
325 1.1 dyoung if ((tp->t_state & TS_TTSTOP) == 0)
326 1.1 dyoung tp->t_state |= TS_FLUSH;
327 1.1 dyoung splx(s);
328 1.1 dyoung }
329 1.1 dyoung
330 1.1 dyoung int
331 1.1 dyoung uart_intr(void *v)
332 1.1 dyoung {
333 1.1 dyoung struct uart_softc *sc = v;
334 1.1 dyoung struct tty *tp = sc->sc_tty;
335 1.1 dyoung int c, l_r;
336 1.1 dyoung
337 1.1 dyoung if (REG_READ(UART_RSR_REG) & UART_RSR_BE) {
338 1.1 dyoung REG_WRITE(UART_ECR_REG, UART_ECR_RSR);
339 1.1 dyoung console_debugger();
340 1.1 dyoung }
341 1.1 dyoung
342 1.1 dyoung while ((REG_READ(UART_FR_REG) & UART_FR_RX_FIFO_EMPTY) == 0) {
343 1.1 dyoung c = REG_READ(UART_DR_REG) & 0xff;
344 1.1 dyoung if (tp->t_state & TS_ISOPEN)
345 1.1 dyoung l_r = (*tp->t_linesw->l_rint)(c, tp);
346 1.1 dyoung }
347 1.1 dyoung return 0;
348 1.1 dyoung }
349