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