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