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