footbridge_com.c revision 1.2.2.1 1 1.2.2.1 fvdl /* $NetBSD: footbridge_com.c,v 1.2.2.1 2001/10/10 11:55:54 fvdl Exp $ */
2 1.1 chris
3 1.1 chris /*-
4 1.1 chris * Copyright (c) 1997 Mark Brinicombe
5 1.1 chris * Copyright (c) 1997 Causality Limited
6 1.1 chris *
7 1.1 chris * Redistribution and use in source and binary forms, with or without
8 1.1 chris * modification, are permitted provided that the following conditions
9 1.1 chris * are met:
10 1.1 chris * 1. Redistributions of source code must retain the above copyright
11 1.1 chris * notice, this list of conditions and the following disclaimer.
12 1.1 chris * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 chris * notice, this list of conditions and the following disclaimer in the
14 1.1 chris * documentation and/or other materials provided with the distribution.
15 1.1 chris * 3. All advertising materials mentioning features or use of this software
16 1.1 chris * must display the following acknowledgement:
17 1.1 chris * This product includes software developed by Mark Brinicombe
18 1.1 chris * for the NetBSD Project.
19 1.1 chris * 4. The name of the author may not be used to endorse or promote products
20 1.1 chris * derived from this software without specific prior written permission.
21 1.1 chris *
22 1.1 chris * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 1.1 chris * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 1.1 chris * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 1.1 chris * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26 1.1 chris * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27 1.1 chris * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 1.1 chris * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 1.1 chris * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 1.1 chris * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31 1.1 chris * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 1.1 chris */
33 1.1 chris
34 1.1 chris /*
35 1.1 chris * COM driver, using the footbridge UART
36 1.1 chris */
37 1.1 chris
38 1.1 chris #include "opt_ddb.h"
39 1.1 chris
40 1.1 chris #include <sys/param.h>
41 1.1 chris #include <sys/systm.h>
42 1.1 chris #include <sys/ioctl.h>
43 1.1 chris #include <sys/select.h>
44 1.1 chris #include <sys/tty.h>
45 1.1 chris #include <sys/proc.h>
46 1.1 chris #include <sys/conf.h>
47 1.1 chris #include <sys/syslog.h>
48 1.1 chris #include <sys/device.h>
49 1.1 chris #include <sys/malloc.h>
50 1.1 chris #include <sys/termios.h>
51 1.2.2.1 fvdl #include <sys/vnode.h>
52 1.1 chris #include <machine/bus.h>
53 1.2 matt #include <machine/intr.h>
54 1.1 chris #include <arm/footbridge/dc21285mem.h>
55 1.1 chris #include <arm/footbridge/dc21285reg.h>
56 1.1 chris #include <arm/footbridge/footbridgevar.h>
57 1.1 chris
58 1.1 chris #include <dev/cons.h>
59 1.1 chris
60 1.1 chris #include "fcom.h"
61 1.1 chris
62 1.1 chris extern u_int dc21285_fclk;
63 1.1 chris
64 1.1 chris #ifdef DDB
65 1.1 chris /*
66 1.1 chris * Define the keycode recognised as a request to call the debugger
67 1.1 chris * A value of 0 disables the feature when DDB is built in
68 1.1 chris */
69 1.1 chris #ifndef DDB_KEYCODE
70 1.1 chris #define DDB_KEYCODE 0
71 1.1 chris #endif /* DDB_KEYCODE */
72 1.1 chris #endif /* DDB */
73 1.1 chris
74 1.1 chris struct fcom_softc {
75 1.1 chris struct device sc_dev;
76 1.1 chris bus_space_tag_t sc_iot;
77 1.1 chris bus_space_handle_t sc_ioh;
78 1.1 chris void *sc_ih;
79 1.1 chris struct callout sc_softintr_ch;
80 1.1 chris int sc_rx_irq;
81 1.1 chris int sc_tx_irq;
82 1.1 chris int sc_hwflags;
83 1.1 chris #define HW_FLAG_CONSOLE 0x01
84 1.1 chris int sc_swflags;
85 1.1 chris int sc_l_ubrlcr;
86 1.1 chris int sc_m_ubrlcr;
87 1.1 chris int sc_h_ubrlcr;
88 1.1 chris char *sc_rxbuffer[2];
89 1.1 chris char *sc_rxbuf;
90 1.1 chris int sc_rxpos;
91 1.1 chris int sc_rxcur;
92 1.1 chris struct tty *sc_tty;
93 1.1 chris };
94 1.1 chris
95 1.1 chris #define RX_BUFFER_SIZE 0x100
96 1.1 chris
97 1.1 chris /* Macros to clear/set/test flags. */
98 1.1 chris #define SET(t, f) (t) |= (f)
99 1.1 chris #define CLR(t, f) (t) &= ~(f)
100 1.1 chris #define ISSET(t, f) ((t) & (f))
101 1.1 chris
102 1.1 chris static int fcom_probe __P((struct device *, struct cfdata *, void *));
103 1.1 chris static void fcom_attach __P((struct device *, struct device *, void *));
104 1.1 chris
105 1.2.2.1 fvdl int fcomopen __P((struct vnode *devvp, int flag, int mode, struct proc *p));
106 1.1 chris static int fcom_rxintr __P((void *));
107 1.1 chris /*static int fcom_txintr __P((void *));*/
108 1.1 chris
109 1.1 chris /*struct consdev;*/
110 1.1 chris /*void fcomcnprobe __P((struct consdev *));
111 1.1 chris void fcomcninit __P((struct consdev *));*/
112 1.1 chris int fcomcngetc __P((dev_t));
113 1.1 chris void fcomcnputc __P((dev_t, int));
114 1.1 chris void fcomcnpollc __P((dev_t, int));
115 1.1 chris
116 1.1 chris struct cfattach fcom_ca = {
117 1.1 chris sizeof(struct fcom_softc), fcom_probe, fcom_attach
118 1.1 chris };
119 1.1 chris
120 1.1 chris extern struct cfdriver fcom_cd;
121 1.1 chris
122 1.1 chris void fcominit __P((bus_space_tag_t, bus_space_handle_t, int, int));
123 1.1 chris void fcominitcons __P((bus_space_tag_t, bus_space_handle_t));
124 1.1 chris
125 1.1 chris bus_space_tag_t fcomconstag;
126 1.1 chris bus_space_handle_t fcomconsioh;
127 1.1 chris extern int comcnmode;
128 1.1 chris extern int comcnspeed;
129 1.1 chris
130 1.1 chris #define COMUNIT(x) (minor(x))
131 1.1 chris #ifndef CONUNIT
132 1.1 chris #define CONUNIT 0
133 1.1 chris #endif
134 1.1 chris
135 1.1 chris /*
136 1.1 chris * The console is set up at init time, well in advance of the reset of the
137 1.1 chris * system and thus we have a private bus space tag for the console.
138 1.1 chris *
139 1.1 chris * The tag is provided by fcom_io.c and fcom_io_asm.S
140 1.1 chris */
141 1.1 chris extern struct bus_space fcomcons_bs_tag;
142 1.1 chris
143 1.1 chris /*
144 1.1 chris * int fcom_probe(struct device *parent, struct cfdata *cf, void *aux)
145 1.1 chris *
146 1.1 chris * Make sure we are trying to attach a com device and then
147 1.1 chris * probe for one.
148 1.1 chris */
149 1.1 chris
150 1.1 chris static int
151 1.1 chris fcom_probe(parent, cf, aux)
152 1.1 chris struct device *parent;
153 1.1 chris struct cfdata *cf;
154 1.1 chris void *aux;
155 1.1 chris {
156 1.1 chris union footbridge_attach_args *fba = aux;
157 1.1 chris
158 1.1 chris if (strcmp(fba->fba_name, "fcom") == 0)
159 1.1 chris return(1);
160 1.1 chris return(0);
161 1.1 chris }
162 1.1 chris
163 1.1 chris /*
164 1.1 chris * void fcom_attach(struct device *parent, struct device *self, void *aux)
165 1.1 chris *
166 1.1 chris * attach the com device
167 1.1 chris */
168 1.1 chris
169 1.1 chris static void
170 1.1 chris fcom_attach(parent, self, aux)
171 1.1 chris struct device *parent, *self;
172 1.1 chris void *aux;
173 1.1 chris {
174 1.1 chris union footbridge_attach_args *fba = aux;
175 1.1 chris struct fcom_softc *sc = (struct fcom_softc *)self;
176 1.1 chris
177 1.1 chris /* Set up the softc */
178 1.1 chris sc->sc_iot = fba->fba_fca.fca_iot;
179 1.1 chris sc->sc_ioh = fba->fba_fca.fca_ioh;
180 1.1 chris callout_init(&sc->sc_softintr_ch);
181 1.1 chris sc->sc_rx_irq = fba->fba_fca.fca_rx_irq;
182 1.1 chris sc->sc_tx_irq = fba->fba_fca.fca_tx_irq;
183 1.1 chris sc->sc_hwflags = 0;
184 1.1 chris sc->sc_swflags = 0;
185 1.1 chris
186 1.1 chris /* If we have a console tag then make a note of it */
187 1.1 chris if (fcomconstag)
188 1.1 chris sc->sc_hwflags |= HW_FLAG_CONSOLE;
189 1.1 chris
190 1.1 chris if (sc->sc_hwflags & HW_FLAG_CONSOLE) {
191 1.1 chris int major;
192 1.1 chris
193 1.1 chris /* locate the major number */
194 1.1 chris for (major = 0; major < nchrdev; ++major)
195 1.1 chris if (cdevsw[major].d_open == fcomopen)
196 1.1 chris break;
197 1.1 chris
198 1.1 chris cn_tab->cn_dev = makedev(major, sc->sc_dev.dv_unit);
199 1.1 chris printf(": console");
200 1.1 chris }
201 1.1 chris printf("\n");
202 1.1 chris
203 1.1 chris sc->sc_ih = intr_claim(sc->sc_rx_irq, IPL_SERIAL, "serial rx",
204 1.1 chris fcom_rxintr, sc);
205 1.1 chris if (sc->sc_ih == NULL)
206 1.1 chris panic("%s: Cannot install rx interrupt handler\n",
207 1.1 chris sc->sc_dev.dv_xname);
208 1.1 chris }
209 1.1 chris
210 1.1 chris static void fcomstart __P((struct tty *));
211 1.1 chris static int fcomparam __P((struct tty *, struct termios *));
212 1.1 chris
213 1.1 chris int
214 1.2.2.1 fvdl fcomopen(devvp, flag, mode, p)
215 1.2.2.1 fvdl struct vnode *devvp;
216 1.1 chris int flag, mode;
217 1.1 chris struct proc *p;
218 1.1 chris {
219 1.1 chris struct fcom_softc *sc;
220 1.2.2.1 fvdl int unit;
221 1.1 chris struct tty *tp;
222 1.2.2.1 fvdl dev_t dev;
223 1.2.2.1 fvdl
224 1.2.2.1 fvdl dev = vdev_rdev(devvp);
225 1.2.2.1 fvdl unit = minor(dev);
226 1.1 chris
227 1.1 chris if (unit >= fcom_cd.cd_ndevs)
228 1.1 chris return ENXIO;
229 1.1 chris sc = fcom_cd.cd_devs[unit];
230 1.1 chris if (!sc)
231 1.1 chris return ENXIO;
232 1.2.2.1 fvdl
233 1.2.2.1 fvdl vdev_setprivdata(devvp, sc);
234 1.2.2.1 fvdl
235 1.1 chris if (!(tp = sc->sc_tty))
236 1.1 chris sc->sc_tty = tp = ttymalloc();
237 1.1 chris if (!sc->sc_rxbuffer[0]) {
238 1.1 chris sc->sc_rxbuffer[0] = malloc(RX_BUFFER_SIZE, M_DEVBUF, M_WAITOK);
239 1.1 chris sc->sc_rxbuffer[1] = malloc(RX_BUFFER_SIZE, M_DEVBUF, M_WAITOK);
240 1.1 chris sc->sc_rxpos = 0;
241 1.1 chris sc->sc_rxcur = 0;
242 1.1 chris sc->sc_rxbuf = sc->sc_rxbuffer[sc->sc_rxcur];
243 1.1 chris if (!sc->sc_rxbuf)
244 1.1 chris panic("%s: Cannot allocate rx buffer memory",
245 1.1 chris sc->sc_dev.dv_xname);
246 1.1 chris }
247 1.1 chris tp->t_oproc = fcomstart;
248 1.1 chris tp->t_param = fcomparam;
249 1.2.2.1 fvdl tp->t_devvp = devvp;
250 1.1 chris if (!(tp->t_state & TS_ISOPEN && tp->t_wopen == 0)) {
251 1.1 chris ttychars(tp);
252 1.1 chris tp->t_cflag = TTYDEF_CFLAG;
253 1.1 chris tp->t_iflag = TTYDEF_IFLAG;
254 1.1 chris tp->t_oflag = TTYDEF_OFLAG;
255 1.1 chris tp->t_lflag = TTYDEF_LFLAG;
256 1.1 chris
257 1.1 chris /*
258 1.1 chris * Initialize the termios status to the defaults. Add in the
259 1.1 chris * sticky bits from TIOCSFLAGS.
260 1.1 chris */
261 1.1 chris tp->t_ispeed = 0;
262 1.1 chris if (ISSET(sc->sc_hwflags, HW_FLAG_CONSOLE))
263 1.1 chris tp->t_ospeed = comcnspeed;
264 1.1 chris else
265 1.1 chris tp->t_ospeed = TTYDEF_SPEED;
266 1.1 chris
267 1.1 chris fcomparam(tp, &tp->t_termios);
268 1.1 chris ttsetwater(tp);
269 1.1 chris } else if ((tp->t_state&TS_XCLUDE) && suser(p->p_ucred, &p->p_acflag))
270 1.1 chris return EBUSY;
271 1.1 chris tp->t_state |= TS_CARR_ON;
272 1.1 chris
273 1.2.2.1 fvdl return (*tp->t_linesw->l_open)(devvp, tp);
274 1.1 chris }
275 1.1 chris
276 1.1 chris int
277 1.2.2.1 fvdl fcomclose(devvp, flag, mode, p)
278 1.2.2.1 fvdl struct vnode *devvp;
279 1.1 chris int flag, mode;
280 1.1 chris struct proc *p;
281 1.1 chris {
282 1.2.2.1 fvdl struct fcom_softc *sc;
283 1.2.2.1 fvdl struct tty *tp;
284 1.2.2.1 fvdl
285 1.2.2.1 fvdl sc = vdev_privdata(devvp);
286 1.2.2.1 fvdl tp = sc->sc_tty;
287 1.2.2.1 fvdl
288 1.1 chris /* XXX This is for cons.c. */
289 1.1 chris if (!ISSET(tp->t_state, TS_ISOPEN))
290 1.1 chris return (0);
291 1.1 chris
292 1.1 chris (*tp->t_linesw->l_close)(tp, flag);
293 1.1 chris ttyclose(tp);
294 1.1 chris #ifdef DIAGNOSTIC
295 1.1 chris if (sc->sc_rxbuffer[0] == NULL)
296 1.1 chris panic("fcomclose: rx buffers not allocated\n");
297 1.1 chris #endif /* DIAGNOSTIC */
298 1.1 chris free(sc->sc_rxbuffer[0], M_DEVBUF);
299 1.1 chris free(sc->sc_rxbuffer[1], M_DEVBUF);
300 1.1 chris sc->sc_rxbuffer[0] = NULL;
301 1.1 chris sc->sc_rxbuffer[1] = NULL;
302 1.1 chris
303 1.1 chris return 0;
304 1.1 chris }
305 1.1 chris
306 1.1 chris int
307 1.2.2.1 fvdl fcomread(devvp, uio, flag)
308 1.2.2.1 fvdl struct vnode *devvp;
309 1.1 chris struct uio *uio;
310 1.1 chris int flag;
311 1.1 chris {
312 1.2.2.1 fvdl struct fcom_softc *sc;
313 1.2.2.1 fvdl struct tty *tp;
314 1.2.2.1 fvdl
315 1.2.2.1 fvdl sc = vdev_privdata(devvp);
316 1.2.2.1 fvdl tp = sc->sc_tty;
317 1.1 chris
318 1.1 chris return (*tp->t_linesw->l_read)(tp, uio, flag);
319 1.1 chris }
320 1.1 chris
321 1.1 chris int
322 1.2.2.1 fvdl fcomwrite(devvp, uio, flag)
323 1.2.2.1 fvdl struct vnode *devvp;
324 1.1 chris struct uio *uio;
325 1.1 chris int flag;
326 1.1 chris {
327 1.2.2.1 fvdl struct fcom_softc *sc;
328 1.2.2.1 fvdl struct tty *tp;
329 1.2.2.1 fvdl
330 1.2.2.1 fvdl sc = vdev_privdata(devvp);
331 1.2.2.1 fvdl tp = sc->sc_tty;
332 1.1 chris
333 1.1 chris return (*tp->t_linesw->l_write)(tp, uio, flag);
334 1.1 chris }
335 1.1 chris
336 1.1 chris int
337 1.2.2.1 fvdl fcompoll(devvp, events, p)
338 1.2.2.1 fvdl struct vnode *devvp;
339 1.1 chris int events;
340 1.1 chris struct proc *p;
341 1.1 chris {
342 1.2.2.1 fvdl struct fcom_softc *sc;
343 1.2.2.1 fvdl struct tty *tp;
344 1.2.2.1 fvdl
345 1.2.2.1 fvdl sc = vdev_privdata(devvp);
346 1.2.2.1 fvdl tp = sc->sc_tty;
347 1.1 chris
348 1.1 chris return ((*tp->t_linesw->l_poll)(tp, events, p));
349 1.1 chris }
350 1.1 chris
351 1.1 chris int
352 1.2.2.1 fvdl fcomioctl(devvp, cmd, data, flag, p)
353 1.2.2.1 fvdl struct vnode *devvp;
354 1.1 chris u_long cmd;
355 1.1 chris caddr_t data;
356 1.1 chris int flag;
357 1.1 chris struct proc *p;
358 1.1 chris {
359 1.2.2.1 fvdl struct fcom_softc *sc;
360 1.2.2.1 fvdl struct tty *tp;
361 1.1 chris int error;
362 1.2.2.1 fvdl
363 1.2.2.1 fvdl sc = vdev_privdata(devvp);
364 1.2.2.1 fvdl tp = sc->sc_tty;
365 1.1 chris
366 1.1 chris if ((error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, p)) >= 0)
367 1.1 chris return error;
368 1.1 chris if ((error = ttioctl(tp, cmd, data, flag, p)) >= 0)
369 1.1 chris return error;
370 1.1 chris
371 1.1 chris switch (cmd) {
372 1.1 chris case TIOCGFLAGS:
373 1.1 chris *(int *)data = sc->sc_swflags;
374 1.1 chris break;
375 1.1 chris
376 1.1 chris case TIOCSFLAGS:
377 1.1 chris error = suser(p->p_ucred, &p->p_acflag);
378 1.1 chris if (error)
379 1.1 chris return (error);
380 1.1 chris sc->sc_swflags = *(int *)data;
381 1.1 chris break;
382 1.1 chris }
383 1.1 chris
384 1.1 chris return ENOTTY;
385 1.1 chris }
386 1.1 chris
387 1.1 chris struct tty *
388 1.2.2.1 fvdl fcomtty(devvp)
389 1.2.2.1 fvdl struct vnode *devvp;
390 1.1 chris {
391 1.2.2.1 fvdl struct fcom_softc *sc;
392 1.2.2.1 fvdl
393 1.2.2.1 fvdl sc = vdev_privdata(devvp);
394 1.1 chris
395 1.1 chris return sc->sc_tty;
396 1.1 chris }
397 1.1 chris
398 1.1 chris void
399 1.1 chris fcomstop(tp, flag)
400 1.1 chris struct tty *tp;
401 1.1 chris int flag;
402 1.1 chris {
403 1.1 chris }
404 1.1 chris
405 1.1 chris static void
406 1.1 chris fcomstart(tp)
407 1.1 chris struct tty *tp;
408 1.1 chris {
409 1.1 chris struct clist *cl;
410 1.1 chris int s, len;
411 1.1 chris u_char buf[64];
412 1.1 chris int loop;
413 1.2.2.1 fvdl struct fcom_softc *sc = vdev_privdata(tp->t_devvp);
414 1.1 chris bus_space_tag_t iot = sc->sc_iot;
415 1.1 chris bus_space_handle_t ioh = sc->sc_ioh;
416 1.1 chris int timo;
417 1.1 chris
418 1.1 chris s = spltty();
419 1.1 chris if (tp->t_state & (TS_TIMEOUT | TS_BUSY | TS_TTSTOP)) {
420 1.1 chris (void)splx(s);
421 1.1 chris return;
422 1.1 chris }
423 1.1 chris tp->t_state |= TS_BUSY;
424 1.1 chris (void)splx(s);
425 1.1 chris
426 1.1 chris /* s = splserial();*/
427 1.1 chris /* wait for any pending transmission to finish */
428 1.1 chris timo = 100000;
429 1.1 chris while ((bus_space_read_4(iot, ioh, UART_FLAGS) & UART_TX_BUSY) && --timo)
430 1.1 chris ;
431 1.1 chris
432 1.1 chris s = splserial();
433 1.1 chris if (bus_space_read_4(iot, ioh, UART_FLAGS) & UART_TX_BUSY) {
434 1.1 chris tp->t_state |= TS_TIMEOUT;
435 1.1 chris callout_reset(&tp->t_rstrt_ch, 1, ttrstrt, tp);
436 1.1 chris (void)splx(s);
437 1.1 chris return;
438 1.1 chris }
439 1.1 chris
440 1.1 chris (void)splx(s);
441 1.1 chris
442 1.1 chris cl = &tp->t_outq;
443 1.1 chris len = q_to_b(cl, buf, 64);
444 1.1 chris for (loop = 0; loop < len; ++loop) {
445 1.1 chris /* s = splserial();*/
446 1.1 chris
447 1.1 chris bus_space_write_4(iot, ioh, UART_DATA, buf[loop]);
448 1.1 chris
449 1.1 chris /* wait for this transmission to complete */
450 1.1 chris timo = 100000;
451 1.1 chris while ((bus_space_read_4(iot, ioh, UART_FLAGS) & UART_TX_BUSY) && --timo)
452 1.1 chris ;
453 1.1 chris /* (void)splx(s);*/
454 1.1 chris }
455 1.1 chris s = spltty();
456 1.1 chris tp->t_state &= ~TS_BUSY;
457 1.1 chris if (cl->c_cc) {
458 1.1 chris tp->t_state |= TS_TIMEOUT;
459 1.1 chris callout_reset(&tp->t_rstrt_ch, 1, ttrstrt, tp);
460 1.1 chris }
461 1.1 chris if (cl->c_cc <= tp->t_lowat) {
462 1.1 chris if (tp->t_state & TS_ASLEEP) {
463 1.1 chris tp->t_state &= ~TS_ASLEEP;
464 1.1 chris wakeup(cl);
465 1.1 chris }
466 1.1 chris selwakeup(&tp->t_wsel);
467 1.1 chris }
468 1.1 chris (void)splx(s);
469 1.1 chris }
470 1.1 chris
471 1.1 chris static int
472 1.1 chris fcomparam(tp, t)
473 1.1 chris struct tty *tp;
474 1.1 chris struct termios *t;
475 1.1 chris {
476 1.2.2.1 fvdl struct fcom_softc *sc = vdev_privdata(tp->t_devvp);
477 1.1 chris bus_space_tag_t iot = sc->sc_iot;
478 1.1 chris bus_space_handle_t ioh = sc->sc_ioh;
479 1.1 chris int baudrate;
480 1.1 chris int h_ubrlcr;
481 1.1 chris int m_ubrlcr;
482 1.1 chris int l_ubrlcr;
483 1.1 chris int s;
484 1.1 chris
485 1.1 chris /* check requested parameters */
486 1.1 chris if (t->c_ospeed < 0)
487 1.1 chris return (EINVAL);
488 1.1 chris if (t->c_ispeed && t->c_ispeed != t->c_ospeed)
489 1.1 chris return (EINVAL);
490 1.1 chris
491 1.1 chris switch (t->c_ospeed) {
492 1.1 chris case B1200:
493 1.1 chris case B2400:
494 1.1 chris case B4800:
495 1.1 chris case B9600:
496 1.1 chris case B19200:
497 1.1 chris case B38400:
498 1.1 chris baudrate = UART_BRD(dc21285_fclk, t->c_ospeed);
499 1.1 chris break;
500 1.1 chris default:
501 1.1 chris baudrate = UART_BRD(dc21285_fclk, 9600);
502 1.1 chris break;
503 1.1 chris }
504 1.1 chris
505 1.1 chris l_ubrlcr = baudrate & 0xff;
506 1.1 chris m_ubrlcr = (baudrate >> 8) & 0xf;
507 1.1 chris h_ubrlcr = 0;
508 1.1 chris
509 1.1 chris switch (ISSET(t->c_cflag, CSIZE)) {
510 1.1 chris case CS5:
511 1.1 chris h_ubrlcr |= UART_DATA_BITS_5;
512 1.1 chris break;
513 1.1 chris case CS6:
514 1.1 chris h_ubrlcr |= UART_DATA_BITS_6;
515 1.1 chris break;
516 1.1 chris case CS7:
517 1.1 chris h_ubrlcr |= UART_DATA_BITS_7;
518 1.1 chris break;
519 1.1 chris case CS8:
520 1.1 chris h_ubrlcr |= UART_DATA_BITS_8;
521 1.1 chris break;
522 1.1 chris }
523 1.1 chris
524 1.1 chris if (ISSET(t->c_cflag, PARENB)) {
525 1.1 chris h_ubrlcr |= UART_PARITY_ENABLE;
526 1.1 chris if (ISSET(t->c_cflag, PARODD))
527 1.1 chris h_ubrlcr |= UART_ODD_PARITY;
528 1.1 chris else
529 1.1 chris h_ubrlcr |= UART_EVEN_PARITY;
530 1.1 chris }
531 1.1 chris
532 1.1 chris if (ISSET(t->c_cflag, CSTOPB))
533 1.1 chris h_ubrlcr |= UART_STOP_BITS_2;
534 1.1 chris
535 1.1 chris bus_space_write_4(iot, ioh, UART_L_UBRLCR, l_ubrlcr);
536 1.1 chris bus_space_write_4(iot, ioh, UART_M_UBRLCR, m_ubrlcr);
537 1.1 chris bus_space_write_4(iot, ioh, UART_H_UBRLCR, h_ubrlcr);
538 1.1 chris
539 1.1 chris s = splserial();
540 1.1 chris
541 1.1 chris sc->sc_l_ubrlcr = l_ubrlcr;
542 1.1 chris sc->sc_m_ubrlcr = m_ubrlcr;
543 1.1 chris sc->sc_h_ubrlcr = h_ubrlcr;
544 1.1 chris
545 1.1 chris /*
546 1.1 chris * For the console, always force CLOCAL and !HUPCL, so that the port
547 1.1 chris * is always active.
548 1.1 chris */
549 1.1 chris if (ISSET(sc->sc_swflags, TIOCFLAG_SOFTCAR) ||
550 1.1 chris ISSET(sc->sc_hwflags, HW_FLAG_CONSOLE)) {
551 1.1 chris SET(t->c_cflag, CLOCAL);
552 1.1 chris CLR(t->c_cflag, HUPCL);
553 1.1 chris }
554 1.1 chris
555 1.1 chris /* and copy to tty */
556 1.1 chris tp->t_ispeed = 0;
557 1.1 chris tp->t_ospeed = t->c_ospeed;
558 1.1 chris tp->t_cflag = t->c_cflag;
559 1.1 chris
560 1.1 chris bus_space_write_4(iot, ioh, UART_L_UBRLCR, l_ubrlcr);
561 1.1 chris bus_space_write_4(iot, ioh, UART_M_UBRLCR, m_ubrlcr);
562 1.1 chris bus_space_write_4(iot, ioh, UART_H_UBRLCR, h_ubrlcr);
563 1.1 chris
564 1.1 chris (void)splx(s);
565 1.1 chris
566 1.1 chris return (0);
567 1.1 chris }
568 1.1 chris
569 1.1 chris static int softint_scheduled = 0;
570 1.1 chris
571 1.1 chris static void
572 1.1 chris fcom_softintr(sc)
573 1.1 chris struct fcom_softc *sc;
574 1.1 chris {
575 1.1 chris struct tty *tp = sc->sc_tty;
576 1.1 chris int s;
577 1.1 chris int loop;
578 1.1 chris int len;
579 1.1 chris char *ptr;
580 1.1 chris
581 1.1 chris s = spltty();
582 1.1 chris ptr = sc->sc_rxbuf;
583 1.1 chris len = sc->sc_rxpos;
584 1.1 chris sc->sc_rxcur ^= 1;
585 1.1 chris sc->sc_rxbuf = sc->sc_rxbuffer[sc->sc_rxcur];
586 1.1 chris sc->sc_rxpos = 0;
587 1.1 chris (void)splx(s);
588 1.1 chris
589 1.1 chris for (loop = 0; loop < len; ++loop)
590 1.1 chris (*tp->t_linesw->l_rint)(ptr[loop], tp);
591 1.1 chris softint_scheduled = 0;
592 1.1 chris }
593 1.1 chris
594 1.1 chris #if 0
595 1.1 chris static int
596 1.1 chris fcom_txintr(arg)
597 1.1 chris void *arg;
598 1.1 chris {
599 1.1 chris /* struct fcom_softc *sc = arg;*/
600 1.1 chris
601 1.1 chris printf("fcom_txintr()\n");
602 1.1 chris return(0);
603 1.1 chris }
604 1.1 chris #endif
605 1.1 chris
606 1.1 chris static int
607 1.1 chris fcom_rxintr(arg)
608 1.1 chris void *arg;
609 1.1 chris {
610 1.1 chris struct fcom_softc *sc = arg;
611 1.1 chris bus_space_tag_t iot = sc->sc_iot;
612 1.1 chris bus_space_handle_t ioh = sc->sc_ioh;
613 1.1 chris struct tty *tp = sc->sc_tty;
614 1.1 chris int status;
615 1.1 chris int byte;
616 1.1 chris
617 1.1 chris do {
618 1.1 chris status = bus_space_read_4(iot, ioh, UART_FLAGS);
619 1.1 chris if ((status & UART_RX_FULL))
620 1.1 chris break;
621 1.1 chris byte = bus_space_read_4(iot, ioh, UART_DATA);
622 1.1 chris status = bus_space_read_4(iot, ioh, UART_RX_STAT);
623 1.1 chris #if DDB_KEYCODE > 0
624 1.1 chris /*
625 1.1 chris * Temporary hack so that I can force the kernel into
626 1.1 chris * the debugger via the serial port
627 1.1 chris */
628 1.1 chris if (byte == DDB_KEYCODE) Debugger();
629 1.1 chris #endif
630 1.1 chris if (tp && (tp->t_state & TS_ISOPEN))
631 1.1 chris if (sc->sc_rxpos < RX_BUFFER_SIZE) {
632 1.1 chris sc->sc_rxbuf[sc->sc_rxpos++] = byte;
633 1.1 chris if (!softint_scheduled) {
634 1.1 chris softint_scheduled = 1;
635 1.1 chris callout_reset(&sc->sc_softintr_ch,
636 1.1 chris 1, fcom_softintr, sc);
637 1.1 chris }
638 1.1 chris }
639 1.1 chris } while (1);
640 1.1 chris return(0);
641 1.1 chris }
642 1.1 chris
643 1.1 chris #if 0
644 1.1 chris void
645 1.1 chris fcom_iflush(sc)
646 1.1 chris struct fcom_softc *sc;
647 1.1 chris {
648 1.1 chris bus_space_tag_t iot = sc->sc_iot;
649 1.1 chris bus_space_handle_t ioh = sc->sc_ioh;
650 1.1 chris
651 1.1 chris /* flush any pending I/O */
652 1.1 chris while (!ISSET(bus_space_read_4(iot, ioh, UART_FLAGS), UART_RX_FULL))
653 1.1 chris (void) bus_space_read_4(iot, ioh, UART_DATA);
654 1.1 chris }
655 1.1 chris #endif
656 1.1 chris
657 1.1 chris /*
658 1.1 chris * Following are all routines needed for COM to act as console
659 1.1 chris */
660 1.1 chris
661 1.1 chris #if 0
662 1.1 chris void
663 1.1 chris fcomcnprobe(cp)
664 1.1 chris struct consdev *cp;
665 1.1 chris {
666 1.1 chris int major;
667 1.1 chris
668 1.1 chris /* Serial console is always present so no probe */
669 1.1 chris
670 1.1 chris /* locate the major number */
671 1.1 chris for (major = 0; major < nchrdev; major++)
672 1.1 chris if (cdevsw[major].d_open == fcomopen)
673 1.1 chris break;
674 1.1 chris
675 1.1 chris /* initialize required fields */
676 1.1 chris cp->cn_dev = makedev(major, CONUNIT);
677 1.1 chris cp->cn_pri = CN_REMOTE; /* Force a serial port console */
678 1.1 chris }
679 1.1 chris
680 1.1 chris void
681 1.1 chris fcomcninit(cp)
682 1.1 chris struct consdev *cp;
683 1.1 chris {
684 1.1 chris fcomconstag = &fcomcons_bs_tag;
685 1.1 chris
686 1.1 chris if (bus_space_map(fcomconstag, DC21285_ARMCSR_BASE, DC21285_ARMCSR_SIZE, 0, &fcomconsioh))
687 1.1 chris panic("fcomcninit: mapping failed");
688 1.1 chris
689 1.1 chris fcominitcons(fcomconstag, fcomconsioh);
690 1.1 chris }
691 1.1 chris #endif
692 1.1 chris
693 1.1 chris int
694 1.1 chris fcomcnattach(iobase, rate, cflag)
695 1.1 chris u_int iobase;
696 1.1 chris int rate;
697 1.1 chris tcflag_t cflag;
698 1.1 chris {
699 1.1 chris static struct consdev fcomcons = {
700 1.1 chris NULL, NULL, fcomcngetc, fcomcnputc, fcomcnpollc, NULL,
701 1.1 chris NODEV, CN_NORMAL
702 1.1 chris };
703 1.1 chris
704 1.1 chris fcomconstag = &fcomcons_bs_tag;
705 1.1 chris
706 1.1 chris if (bus_space_map(fcomconstag, iobase, DC21285_ARMCSR_SIZE,
707 1.1 chris 0, &fcomconsioh))
708 1.1 chris panic("fcomcninit: mapping failed");
709 1.1 chris
710 1.1 chris fcominit(fcomconstag, fcomconsioh, rate, cflag);
711 1.1 chris
712 1.1 chris cn_tab = &fcomcons;
713 1.1 chris
714 1.1 chris /* comcnspeed = rate;
715 1.1 chris comcnmode = cflag;*/
716 1.1 chris return (0);
717 1.1 chris }
718 1.1 chris
719 1.1 chris int
720 1.1 chris fcomcndetach(void)
721 1.1 chris {
722 1.1 chris bus_space_unmap(fcomconstag, fcomconsioh, DC21285_ARMCSR_SIZE);
723 1.1 chris
724 1.1 chris cn_tab = NULL;
725 1.1 chris return (0);
726 1.1 chris }
727 1.1 chris
728 1.1 chris /*
729 1.1 chris * Initialize UART to known state.
730 1.1 chris */
731 1.1 chris void
732 1.1 chris fcominit(iot, ioh, rate, mode)
733 1.1 chris bus_space_tag_t iot;
734 1.1 chris bus_space_handle_t ioh;
735 1.1 chris int rate;
736 1.1 chris int mode;
737 1.1 chris {
738 1.1 chris int baudrate;
739 1.1 chris int h_ubrlcr;
740 1.1 chris int m_ubrlcr;
741 1.1 chris int l_ubrlcr;
742 1.1 chris
743 1.1 chris switch (rate) {
744 1.1 chris case B1200:
745 1.1 chris case B2400:
746 1.1 chris case B4800:
747 1.1 chris case B9600:
748 1.1 chris case B19200:
749 1.1 chris case B38400:
750 1.1 chris baudrate = UART_BRD(dc21285_fclk, rate);
751 1.1 chris break;
752 1.1 chris default:
753 1.1 chris baudrate = UART_BRD(dc21285_fclk, 9600);
754 1.1 chris break;
755 1.1 chris }
756 1.1 chris
757 1.1 chris h_ubrlcr = 0;
758 1.1 chris switch (mode & CSIZE) {
759 1.1 chris case CS5:
760 1.1 chris h_ubrlcr |= UART_DATA_BITS_5;
761 1.1 chris break;
762 1.1 chris case CS6:
763 1.1 chris h_ubrlcr |= UART_DATA_BITS_6;
764 1.1 chris break;
765 1.1 chris case CS7:
766 1.1 chris h_ubrlcr |= UART_DATA_BITS_7;
767 1.1 chris break;
768 1.1 chris case CS8:
769 1.1 chris h_ubrlcr |= UART_DATA_BITS_8;
770 1.1 chris break;
771 1.1 chris }
772 1.1 chris
773 1.1 chris if (mode & PARENB)
774 1.1 chris h_ubrlcr |= UART_PARITY_ENABLE;
775 1.1 chris if (mode & PARODD)
776 1.1 chris h_ubrlcr |= UART_ODD_PARITY;
777 1.1 chris else
778 1.1 chris h_ubrlcr |= UART_EVEN_PARITY;
779 1.1 chris
780 1.1 chris if (mode & CSTOPB)
781 1.1 chris h_ubrlcr |= UART_STOP_BITS_2;
782 1.1 chris
783 1.1 chris m_ubrlcr = (baudrate >> 8) & 0xf;
784 1.1 chris l_ubrlcr = baudrate & 0xff;
785 1.1 chris
786 1.1 chris bus_space_write_4(iot, ioh, UART_L_UBRLCR, l_ubrlcr);
787 1.1 chris bus_space_write_4(iot, ioh, UART_M_UBRLCR, m_ubrlcr);
788 1.1 chris bus_space_write_4(iot, ioh, UART_H_UBRLCR, h_ubrlcr);
789 1.1 chris }
790 1.1 chris #if 0
791 1.1 chris /*
792 1.1 chris * Set UART for console use. Do normal init, then enable interrupts.
793 1.1 chris */
794 1.1 chris void
795 1.1 chris fcominitcons(iot, ioh)
796 1.1 chris bus_space_tag_t iot;
797 1.1 chris bus_space_handle_t ioh;
798 1.1 chris {
799 1.1 chris int s = splserial();
800 1.1 chris
801 1.1 chris fcominit(iot, ioh, comcnspeed, comcnmode);
802 1.1 chris
803 1.1 chris delay(10000);
804 1.1 chris
805 1.1 chris (void)splx(s);
806 1.1 chris }
807 1.1 chris #endif
808 1.1 chris
809 1.1 chris int
810 1.1 chris fcomcngetc(dev)
811 1.1 chris dev_t dev;
812 1.1 chris {
813 1.1 chris int s = splserial();
814 1.1 chris bus_space_tag_t iot = fcomconstag;
815 1.1 chris bus_space_handle_t ioh = fcomconsioh;
816 1.1 chris u_char stat, c;
817 1.1 chris
818 1.1 chris while ((bus_space_read_4(iot, ioh, UART_FLAGS) & UART_RX_FULL) != 0)
819 1.1 chris ;
820 1.1 chris c = bus_space_read_4(iot, ioh, UART_DATA);
821 1.1 chris stat = bus_space_read_4(iot, ioh, UART_RX_STAT);
822 1.1 chris (void)splx(s);
823 1.1 chris #if DDB_KEYCODE > 0
824 1.1 chris /*
825 1.1 chris * Temporary hack so that I can force the kernel into
826 1.1 chris * the debugger via the serial port
827 1.1 chris */
828 1.1 chris if (c == DDB_KEYCODE) Debugger();
829 1.1 chris #endif
830 1.1 chris
831 1.1 chris return (c);
832 1.1 chris }
833 1.1 chris
834 1.1 chris /*
835 1.1 chris * Console kernel output character routine.
836 1.1 chris */
837 1.1 chris void
838 1.1 chris fcomcnputc(dev, c)
839 1.1 chris dev_t dev;
840 1.1 chris int c;
841 1.1 chris {
842 1.1 chris int s = splserial();
843 1.1 chris bus_space_tag_t iot = fcomconstag;
844 1.1 chris bus_space_handle_t ioh = fcomconsioh;
845 1.1 chris int timo;
846 1.1 chris
847 1.1 chris /* wait for any pending transmission to finish */
848 1.1 chris timo = 50000;
849 1.1 chris while ((bus_space_read_4(iot, ioh, UART_FLAGS) & UART_TX_BUSY) && --timo)
850 1.1 chris ;
851 1.1 chris bus_space_write_4(iot, ioh, UART_DATA, c);
852 1.1 chris
853 1.1 chris /* wait for this transmission to complete */
854 1.1 chris timo = 1500000;
855 1.1 chris while ((bus_space_read_4(iot, ioh, UART_FLAGS) & UART_TX_BUSY) && --timo)
856 1.1 chris ;
857 1.1 chris /* Clear interrupt status here */
858 1.1 chris (void)splx(s);
859 1.1 chris }
860 1.1 chris
861 1.1 chris void
862 1.1 chris fcomcnpollc(dev, on)
863 1.1 chris dev_t dev;
864 1.1 chris int on;
865 1.1 chris {
866 1.1 chris }
867