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