xlcom.c revision 1.1 1 /* $NetBSD: xlcom.c,v 1.1 2006/12/02 22:18:47 freza Exp $ */
2
3 /*
4 * Copyright (c) 2006 Jachym Holecek
5 * All rights reserved.
6 *
7 * Written for DFC Design, s.r.o.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 *
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 *
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 /* TODO: kgdb support */
33
34 #include <sys/cdefs.h>
35 __KERNEL_RCSID(0, "$NetBSD: xlcom.c,v 1.1 2006/12/02 22:18:47 freza Exp $");
36
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/conf.h>
40 #include <sys/device.h>
41 #include <sys/file.h>
42 #include <sys/ioctl.h>
43 #include <sys/kauth.h>
44 #include <sys/kernel.h>
45 #include <sys/proc.h>
46 #include <sys/tty.h>
47 #include <sys/time.h>
48 #include <sys/syslog.h>
49
50 #include <dev/cons.h>
51
52 #include <machine/intr.h>
53 #include <machine/bus.h>
54
55 #include <evbppc/virtex/virtex.h>
56 #include <evbppc/virtex/dev/xcvbusvar.h>
57 #include <evbppc/virtex/dev/xlcomreg.h>
58
59
60 #define XLCOM_UNIT_MASK 0x7f
61 #define XLCOM_DIALOUT_MASK 0x80
62
63 #define UNIT(dev) (minor(dev) & XLCOM_UNIT_MASK)
64 #define DIALOUT(dev) (minor(dev) & XLCOM_DIALOUT_MASK)
65
66 #define XLCOM_CHAR_PE 0x8000 /* Parity error flag */
67 #define XLCOM_CHAR_FE 0x4000 /* Frame error flag */
68 #define next(idx) (void)((idx) = ((idx) + 1) % XLCOM_RXBUF_SIZE)
69
70 #define XLCOM_RXBUF_SIZE 1024
71
72 struct xlcom_softc {
73 struct device sc_dev;
74 struct tty *sc_tty;
75 void *sc_ih;
76
77 bus_space_tag_t sc_iot;
78 bus_space_handle_t sc_ioh;
79
80 /* Deffered execution context. */
81 void *sc_rx_soft;
82 void *sc_tx_soft;
83
84 /* Receive buffer */
85 u_short sc_rbuf[XLCOM_RXBUF_SIZE];
86 volatile u_int sc_rput;
87 volatile u_int sc_rget;
88 volatile u_int sc_ravail;
89
90 /* Transmit buffer */
91 u_char *sc_tba;
92 u_int sc_tbc;
93 };
94
95 static int xlcom_intr(void *);
96 static void xlcom_rx_soft(void *);
97 static void xlcom_tx_soft(void *);
98 static void xlcom_reset(bus_space_tag_t, bus_space_handle_t);
99
100 /* System console interface. */
101 static int xlcom_cngetc(dev_t);
102 static void xlcom_cnputc(dev_t, int);
103 void xlcom_cninit(struct consdev *);
104
105 static struct cnm_state xlcom_cnm_state;
106
107 struct consdev consdev_xlcom = {
108 .cn_probe = nullcnprobe,
109 .cn_init = xlcom_cninit,
110 .cn_getc = xlcom_cngetc,
111 .cn_putc = xlcom_cnputc,
112 .cn_pollc = nullcnpollc,
113 .cn_pri = CN_REMOTE
114 };
115
116 /* Character device. */
117 static dev_type_open(xlcom_open);
118 static dev_type_read(xlcom_read);
119 static dev_type_write(xlcom_write);
120 static dev_type_ioctl(xlcom_ioctl);
121 static dev_type_poll(xlcom_poll);
122 static dev_type_close(xlcom_close);
123
124 static dev_type_tty(xlcom_tty);
125 static dev_type_stop(xlcom_stop);
126
127 const struct cdevsw xlcom_cdevsw = {
128 xlcom_open, xlcom_close, xlcom_read, xlcom_write, xlcom_ioctl,
129 xlcom_stop, xlcom_tty, xlcom_poll, nommap, ttykqfilter, D_TTY
130 };
131
132 extern struct cfdriver xlcom_cd;
133
134 /* Terminal line. */
135 static int xlcom_param(struct tty *, struct termios *);
136 static void xlcom_start(struct tty *);
137
138 /* Generic device. */
139 static void xlcom_attach(struct device *, struct device *, void *);
140
141 CFATTACH_DECL(xlcom, sizeof(struct xlcom_softc),
142 xcvbus_child_match, xlcom_attach, NULL, NULL);
143
144
145 static void
146 xlcom_attach(struct device *parent, struct device *self, void *aux)
147 {
148 struct xcvbus_attach_args *vaa = aux;
149 struct xlcom_softc *sc = (struct xlcom_softc *)self;
150 struct tty *tp;
151 dev_t dev;
152
153 printf(": UartLite serial port\n");
154
155 if ((sc->sc_ih = intr_establish(vaa->vaa_intr, IST_LEVEL, IPL_SERIAL,
156 xlcom_intr, sc)) == NULL) {
157 printf("%s: could not establish interrupt\n",
158 device_xname(self));
159 return ;
160 }
161
162 dev = makedev(cdevsw_lookup_major(&xlcom_cdevsw), device_unit(self));
163 if (cn_tab == &consdev_xlcom) {
164 cn_init_magic(&xlcom_cnm_state);
165 cn_set_magic("\x23\x2e"); /* #. */
166 cn_tab->cn_dev = dev;
167
168 sc->sc_iot = consdev_iot;
169 sc->sc_ioh = consdev_ioh;
170
171 printf("%s: console\n", sc->sc_dev.dv_xname);
172 } else {
173 sc->sc_iot = vaa->vaa_iot;
174
175 if (bus_space_map(vaa->vaa_iot, vaa->vaa_addr, XLCOM_SIZE, 0,
176 &sc->sc_ioh) != 0) {
177 printf("%s: could not map registers\n",
178 device_xname(self));
179 return ;
180 }
181
182 /* Reset FIFOs. */
183 xlcom_reset(sc->sc_iot, sc->sc_ioh);
184 }
185
186 sc->sc_tbc = 0;
187 sc->sc_tba = NULL;
188
189 sc->sc_rput = sc->sc_rget = 0;
190 sc->sc_ravail = XLCOM_RXBUF_SIZE;
191
192 sc->sc_rx_soft = softintr_establish(IPL_SOFTSERIAL, xlcom_rx_soft, sc);
193 sc->sc_tx_soft = softintr_establish(IPL_SOFTSERIAL, xlcom_tx_soft, sc);
194
195 if (sc->sc_rx_soft == NULL || sc->sc_tx_soft == NULL) {
196 printf("%s: could not establish Rx or Tx softintr\n",
197 sc->sc_dev.dv_xname);
198 return ;
199 }
200
201 tp = ttymalloc();
202 tp->t_dev = dev;
203 tp->t_oproc = xlcom_start;
204 tp->t_param = xlcom_param;
205 tp->t_hwiflow = NULL; /* No HW flow control */
206 tty_attach(tp);
207
208 /* XXX anything else to do for console early? */
209 if (cn_tab == &consdev_xlcom) {
210 /* Before first open, so that we can enter ddb(4). */
211 bus_space_write_4(sc->sc_iot, sc->sc_ioh, XLCOM_CNTL,
212 CNTL_INTR_EN);
213 }
214
215 sc->sc_tty = tp;
216 }
217
218 /*
219 * Misc hooks.
220 */
221 static void
222 xlcom_tx_soft(void *arg)
223 {
224 struct xlcom_softc *sc = (struct xlcom_softc *)arg;
225 struct tty *tp = sc->sc_tty;
226
227 if (tp->t_state & TS_FLUSH)
228 tp->t_state &= ~TS_FLUSH;
229 else
230 ndflush(&tp->t_outq,
231 (int)(sc->sc_tba - tp->t_outq.c_cf));
232 (tp->t_linesw->l_start)(tp);
233 }
234
235 static void
236 xlcom_rx_soft(void *arg)
237 {
238 struct xlcom_softc *sc = (struct xlcom_softc *)arg;
239 struct tty *tp = sc->sc_tty;
240 int (*rint)(int, struct tty *);
241 u_short c;
242 int d;
243
244 /*
245 * XXX: we don't do any synchronization, rput may change below
246 * XXX: our hands -- it doesn't seem to be troublesome as long
247 * XXX: as "sc->sc_rget = sc->sc_rput" is atomic.
248 */
249 rint = tp->t_linesw->l_rint;
250
251 /* Run until we catch our tail. */
252 while (sc->sc_rput != sc->sc_rget) {
253 c = sc->sc_rbuf[sc->sc_rget];
254
255 next(sc->sc_rget);
256 sc->sc_ravail++;
257
258 d = (c & 0xff) |
259 ((c & XLCOM_CHAR_PE) != 0 ? TTY_PE : 0) |
260 ((c & XLCOM_CHAR_FE) != 0 ? TTY_FE : 0);
261
262 /*
263 * Drop the rest of data if discipline runs out of buffer
264 * space. We'd use flow control here, if we had any.
265 */
266 if ((rint)(d, tp) == -1) {
267 sc->sc_rget = sc->sc_rput;
268 return ;
269 }
270 }
271 }
272
273 static void
274 xlcom_send_chunk(struct xlcom_softc *sc)
275 {
276 uint32_t stat;
277
278 /* Chunk flushed, no more data available. */
279 if (sc->sc_tbc <= 0) {
280 return ;
281 }
282
283 /* Run as long as we have space and data. */
284 while (sc->sc_tbc > 0) {
285 stat = bus_space_read_4(sc->sc_iot, sc->sc_ioh, XLCOM_STAT);
286 if (stat & STAT_TX_FULL)
287 break;
288
289 bus_space_write_4(sc->sc_iot, sc->sc_ioh, XLCOM_TX_FIFO,
290 *sc->sc_tba);
291
292 sc->sc_tbc--;
293 sc->sc_tba++;
294 }
295
296 /* Try to grab more data while FIFO drains. */
297 if (sc->sc_tbc == 0) {
298 sc->sc_tty->t_state &= ~TS_BUSY;
299 softintr_schedule(sc->sc_tx_soft);
300 }
301 }
302
303 static void
304 xlcom_recv_chunk(struct xlcom_softc *sc)
305 {
306 uint32_t stat;
307 u_short c;
308 u_int n;
309
310 n = sc->sc_ravail;
311 stat = bus_space_read_4(sc->sc_iot, sc->sc_ioh, XLCOM_STAT);
312
313 /* Run as long as we have data and space. */
314 while ((stat & STAT_RX_DATA) != 0 && sc->sc_ravail > 0) {
315 c = bus_space_read_4(sc->sc_iot, sc->sc_ioh, XLCOM_RX_FIFO);
316
317 cn_check_magic(sc->sc_tty->t_dev, c, xlcom_cnm_state);
318
319 /* XXX: Should we pass rx-overrun upstream too? */
320 c |= ((stat & STAT_PARITY_ERR) != 0 ? XLCOM_CHAR_PE : 0) |
321 ((stat & STAT_FRAME_ERR) != 0 ? XLCOM_CHAR_FE : 0);
322 sc->sc_rbuf[sc->sc_rput] = c;
323 sc->sc_ravail--;
324
325 next(sc->sc_rput);
326 stat = bus_space_read_4(sc->sc_iot, sc->sc_ioh, XLCOM_STAT);
327 }
328
329 /* Shedule completion hook if we received any. */
330 if (n != sc->sc_ravail)
331 softintr_schedule(sc->sc_rx_soft);
332 }
333
334 static int
335 xlcom_intr(void *arg)
336 {
337 struct xlcom_softc *sc = arg;
338 uint32_t stat;
339
340 /*
341 * Xilinx DS422, "OPB UART Lite v1.00b"
342 *
343 * If interrupts are enabled, an interrupt is generated when one
344 * of the following conditions is true:
345 */
346 stat = bus_space_read_4(sc->sc_iot, sc->sc_ioh, XLCOM_STAT);
347
348 /*
349 * 1. When there exists any valid character in the receive FIFO,
350 * the interrupt stays active until the receive FIFO is empty.
351 * This is a level interrupt.
352 */
353 if (stat & STAT_RX_DATA)
354 xlcom_recv_chunk(sc);
355
356 /*
357 * 2. When the transmit FIFO goes from not empty to empty, such
358 * as when the last character in the transmit FIFO is transmitted,
359 * the interrupt is only active one clock cycle. This is an
360 * edge interrupt.
361 */
362 if (stat & STAT_TX_EMPTY)
363 xlcom_send_chunk(sc);
364
365 return (0);
366 }
367
368 /*
369 * Character device.
370 */
371 static int
372 xlcom_open(dev_t dev, int flags, int mode, struct lwp *l)
373 {
374 struct xlcom_softc *sc;
375 struct tty *tp;
376 struct proc *p;
377 int error, s;
378
379 sc = device_lookup(&xlcom_cd, minor(dev));
380 if (sc == NULL)
381 return (ENXIO);
382
383 tp = sc->sc_tty;
384 p = l->l_proc;
385
386 s = spltty(); /* { */
387
388 if ((tp->t_state & TS_ISOPEN) && (tp->t_state & TS_XCLUDE) &&
389 kauth_authorize_generic(p->p_cred, KAUTH_GENERIC_ISSUSER,
390 &p->p_acflag) != 0) {
391 error = EBUSY;
392 goto fail;
393 }
394
395 /* Is this the first open? */
396 if (!(tp->t_state & TS_ISOPEN) && tp->t_wopen == 0) {
397 tp->t_dev = dev;
398
399 /* Values hardwired at synthesis time. XXXFreza: xparam.h */
400 tp->t_ispeed = tp->t_ospeed = B38400;
401 tp->t_cflag = CLOCAL | CREAD | CS8;
402 tp->t_iflag = TTYDEF_IFLAG;
403 tp->t_oflag = TTYDEF_OFLAG;
404 tp->t_lflag = TTYDEF_LFLAG;
405
406 ttychars(tp);
407 ttsetwater(tp);
408
409 /* Enable interrupt. */
410 bus_space_write_4(sc->sc_iot, sc->sc_ioh, XLCOM_CNTL,
411 CNTL_INTR_EN);
412 }
413
414 error = ttyopen(tp, DIALOUT(dev), (flags & O_NONBLOCK));
415 if (error)
416 goto fail;
417
418 error = (tp->t_linesw->l_open)(dev, tp);
419 if (error)
420 goto fail;
421
422 splx(s); /* } */
423 return (0);
424
425 fail:
426 /* XXXFreza: Shutdown if nobody else has the device open. */
427 splx(s);
428 return (error);
429 }
430
431 static int
432 xlcom_read(dev_t dev, struct uio *uio, int flag)
433 {
434 struct xlcom_softc *sc;
435 struct tty *tp;
436
437 sc = device_lookup(&xlcom_cd, minor(dev));
438 if (sc == NULL)
439 return (ENXIO);
440 tp = sc->sc_tty;
441
442 return (tp->t_linesw->l_read)(tp, uio, flag);
443 }
444
445 static int
446 xlcom_write(dev_t dev, struct uio *uio, int flag)
447 {
448 struct xlcom_softc *sc;
449 struct tty *tp;
450
451 sc = device_lookup(&xlcom_cd, minor(dev));
452 if (sc == NULL)
453 return (ENXIO);
454 tp = sc->sc_tty;
455
456 return (tp->t_linesw->l_write)(tp, uio, flag);
457 }
458
459 static int
460 xlcom_poll(dev_t dev, int events, struct lwp *l)
461 {
462 struct xlcom_softc *sc;
463 struct tty *tp;
464
465 sc = device_lookup(&xlcom_cd, minor(dev));
466 if (sc == NULL)
467 return (ENXIO);
468 tp = sc->sc_tty;
469
470 return (tp->t_linesw->l_poll)(tp, events, l);
471 }
472
473 static struct tty *
474 xlcom_tty(dev_t dev)
475 {
476 struct xlcom_softc *sc;
477 struct tty *tp;
478
479 sc = device_lookup(&xlcom_cd, minor(dev));
480 if (sc == NULL)
481 return (NULL);
482 tp = sc->sc_tty;
483
484 return (tp);
485 }
486
487 static int
488 xlcom_ioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct lwp *l)
489 {
490 struct xlcom_softc *sc;
491 struct tty *tp;
492 int error;
493
494 sc = device_lookup(&xlcom_cd, minor(dev));
495 if (sc == NULL)
496 return (ENXIO);
497 tp = sc->sc_tty;
498
499 error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, l);
500 if (error != EPASSTHROUGH)
501 return (error);
502
503 error = ttioctl(tp, cmd, data, flag, l);
504 if (error != EPASSTHROUGH)
505 return (error);
506
507 /* XXXFreza: error = 0; switch (cmd); return error; */
508
509 return (error);
510 }
511
512 static int
513 xlcom_close(dev_t dev, int flag, int mode, struct lwp *l)
514 {
515 struct xlcom_softc *sc;
516 struct tty *tp;
517
518 sc = device_lookup(&xlcom_cd, minor(dev));
519 if (sc == NULL)
520 return (ENXIO);
521 tp = sc->sc_tty;
522
523 (tp->t_linesw->l_close)(tp, flag);
524 ttyclose(tp);
525
526 /* Is this the last close? XXXFreza: hum? */
527 if (!(tp->t_state & TS_ISOPEN) && tp->t_wopen == 0) {
528 }
529
530 return (0);
531 }
532
533 static void
534 xlcom_stop(struct tty *tp, int flag)
535 {
536 struct xlcom_softc *sc;
537 int s;
538
539 sc = device_lookup(&xlcom_cd, UNIT(tp->t_dev));
540 if (sc == NULL)
541 return ;
542
543 s = splserial();
544 if (tp->t_state & TS_BUSY) {
545 /* XXXFreza: make sure we stop xmitting at next chunk */
546
547 if (! (tp->t_state & TS_TTSTOP))
548 tp->t_state |= TS_FLUSH;
549 }
550 splx(s);
551 }
552
553 /*
554 * Terminal line.
555 */
556 static int
557 xlcom_param(struct tty *tp, struct termios *t)
558 {
559 t->c_cflag &= ~HUPCL;
560
561 if (tp->t_ospeed == t->c_ospeed &&
562 tp->t_ispeed == t->c_ispeed &&
563 tp->t_cflag == t->c_cflag)
564 return (0);
565
566 return (EINVAL);
567 }
568
569 static void
570 xlcom_start(struct tty *tp)
571 {
572 struct xlcom_softc *sc;
573 int s1, s2;
574
575 sc = device_lookup(&xlcom_cd, UNIT(tp->t_dev));
576 if (sc == NULL)
577 return ;
578
579 s1 = spltty();
580
581 if (tp->t_state & (TS_BUSY | TS_TIMEOUT | TS_TTSTOP)) {
582 splx(s1);
583 return ;
584 }
585
586 if (tp->t_outq.c_cc <= tp->t_lowat) {
587 if (tp->t_state & TS_ASLEEP) {
588 tp->t_state &= ~TS_ASLEEP;
589 wakeup(&tp->t_outq);
590 }
591 selwakeup(&tp->t_wsel);
592
593 if (tp->t_outq.c_cc == 0) {
594 splx(s1);
595 return ;
596 }
597 }
598
599 tp->t_state |= TS_BUSY;
600 splx(s1);
601
602 s2 = splserial();
603 sc->sc_tba = tp->t_outq.c_cf;
604 sc->sc_tbc = ndqb(&tp->t_outq, 0);
605 xlcom_send_chunk(sc);
606 splx(s2);
607 }
608
609 static void
610 xlcom_reset(bus_space_tag_t iot, bus_space_handle_t ioh)
611 {
612 /* Wait while the fifo drains. */
613 while (! (bus_space_read_4(iot, ioh, XLCOM_STAT) & STAT_TX_EMPTY))
614 ;
615
616 bus_space_write_4(iot, ioh, XLCOM_CNTL, CNTL_RX_CLEAR | CNTL_TX_CLEAR);
617 }
618
619 /*
620 * Console on UartLite.
621 */
622 void
623 nullcnprobe(struct consdev *cn)
624 {
625 }
626
627 void
628 xlcom_cninit(struct consdev *cn)
629 {
630 if (bus_space_map(consdev_iot, CONS_ADDR, XLCOM_SIZE, 0, &consdev_ioh))
631 panic("xlcom_cninit: could not map consdev_ioh");
632
633 xlcom_reset(consdev_iot, consdev_ioh);
634 }
635
636 static int
637 xlcom_cngetc(dev_t dev)
638 {
639 while (! (bus_space_read_4(consdev_iot, consdev_ioh, XLCOM_STAT) &
640 STAT_RX_DATA))
641 ;
642
643 return (bus_space_read_4(consdev_iot, consdev_ioh,
644 XLCOM_RX_FIFO));
645 }
646
647 static void
648 xlcom_cnputc(dev_t dev, int c)
649 {
650 while (bus_space_read_4(consdev_iot, consdev_ioh, XLCOM_STAT) &
651 STAT_TX_FULL)
652 ;
653
654 bus_space_write_4(consdev_iot, consdev_ioh, XLCOM_TX_FIFO, c);
655 }
656