root/dev/ic/com_subr.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. com_enable_debugport
  2. com_attach_subr
  3. com_fifo_probe

    1 /*      $OpenBSD: com_subr.c,v 1.10 2007/03/20 17:37:34 deraadt Exp $   */
    2 
    3 /*
    4  * Copyright (c) 1997 - 1999, Jason Downs.  All rights reserved.
    5  *
    6  * Redistribution and use in source and binary forms, with or without
    7  * modification, are permitted provided that the following conditions
    8  * are met:
    9  * 1. Redistributions of source code must retain the above copyright
   10  *    notice, this list of conditions and the following disclaimer.
   11  * 2. Redistributions in binary form must reproduce the above copyright
   12  *    notice, this list of conditions and the following disclaimer in the
   13  *    documentation and/or other materials provided with the distribution.
   14  *
   15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS
   16  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
   17  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   18  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT,
   19  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
   20  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
   21  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
   22  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   25  * SUCH DAMAGE.
   26  */
   27 /*-
   28  * Copyright (c) 1993, 1994, 1995, 1996
   29  *      Charles M. Hannum.  All rights reserved.
   30  * Copyright (c) 1991 The Regents of the University of California.
   31  * All rights reserved.
   32  *
   33  * Redistribution and use in source and binary forms, with or without
   34  * modification, are permitted provided that the following conditions
   35  * are met:
   36  * 1. Redistributions of source code must retain the above copyright
   37  *    notice, this list of conditions and the following disclaimer.
   38  * 2. Redistributions in binary form must reproduce the above copyright
   39  *    notice, this list of conditions and the following disclaimer in the
   40  *    documentation and/or other materials provided with the distribution.
   41  * 3. Neither the name of the University nor the names of its contributors
   42  *    may be used to endorse or promote products derived from this software
   43  *    without specific prior written permission.
   44  *
   45  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
   46  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   47  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   48  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
   49  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   50  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   51  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   52  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   53  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   54  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   55  * SUCH DAMAGE.
   56  *
   57  *      @(#)com.c       7.5 (Berkeley) 5/16/91
   58  */
   59 
   60 /*
   61  * COM driver, based on HP dca driver
   62  * uses National Semiconductor NS16450/NS16550AF UART
   63  */
   64 #include <sys/param.h>
   65 #include <sys/systm.h>
   66 #include <sys/kernel.h>
   67 #include <sys/device.h>
   68 #include <sys/tty.h>
   69 
   70 #include "com.h"
   71 #ifdef i386
   72 #include "pccom.h"
   73 #else
   74 #define NPCCOM  0
   75 #endif
   76 
   77 #include <machine/bus.h>
   78 #if defined(__sparc64__) || !defined(__sparc__)
   79 #include <machine/intr.h>
   80 #endif
   81 
   82 #if !defined(__sparc__) || defined(__sparc64__)
   83 #define COM_CONSOLE
   84 #include <dev/cons.h>
   85 #endif
   86 
   87 #include <dev/ic/comreg.h>
   88 #include <dev/ic/ns16550reg.h>
   89 #define com_lcr com_cfcr
   90 
   91 #if NCOM > 0
   92 #include <dev/ic/comvar.h>
   93 #endif
   94 #if NPCCOM > 0
   95 #include <i386/isa/pccomvar.h>
   96 #endif
   97 
   98 #ifdef COM_PXA2X0
   99 #define com_isr 8
  100 #define ISR_SEND        (ISR_RXPL | ISR_XMODE | ISR_XMITIR)
  101 #define ISR_RECV        (ISR_RXPL | ISR_XMODE | ISR_RCVEIR)
  102 #endif
  103 
  104 #ifdef  COM_CONSOLE
  105 #include <sys/conf.h>
  106 cdev_decl(com);
  107 #endif
  108 
  109 void    com_enable_debugport(struct com_softc *);
  110 void    com_fifo_probe(struct com_softc *);
  111 
  112 #if defined(COM_CONSOLE) || defined(KGDB)
  113 void
  114 com_enable_debugport(sc)
  115         struct com_softc *sc;
  116 {
  117         int s;
  118 
  119         /* Turn on line break interrupt, set carrier. */
  120         s = splhigh();
  121 #ifdef KGDB
  122         SET(sc->sc_ier, IER_ERXRDY);
  123 #ifdef COM_PXA2X0
  124         if (sc->sc_uarttype == COM_UART_PXA2X0)
  125                 sc->sc_ier |= IER_EUART | IER_ERXTOUT;
  126 #endif
  127         bus_space_write_1(sc->sc_iot, sc->sc_ioh, com_ier, sc->sc_ier);
  128 #endif
  129         SET(sc->sc_mcr, MCR_DTR | MCR_RTS | MCR_IENABLE);
  130         bus_space_write_1(sc->sc_iot, sc->sc_ioh, com_mcr, sc->sc_mcr);
  131 
  132         splx(s);
  133 }
  134 #endif  /* COM_CONSOLED || KGDB */
  135 
  136 void
  137 com_attach_subr(sc)
  138         struct com_softc *sc;
  139 {
  140         bus_space_tag_t iot = sc->sc_iot;
  141         bus_space_handle_t ioh = sc->sc_ioh;
  142         u_int8_t lcr;
  143 
  144         sc->sc_ier = 0;
  145 #ifdef COM_PXA2X0
  146         if (sc->sc_uarttype == COM_UART_PXA2X0)
  147                 sc->sc_ier |= IER_EUART;
  148 #endif
  149         /* disable interrupts */
  150         bus_space_write_1(iot, ioh, com_ier, sc->sc_ier);
  151 
  152 #ifdef COM_CONSOLE
  153         if (sc->sc_iobase == comconsaddr) {
  154                 comconsattached = 1;
  155                 delay(10000);                   /* wait for output to finish */
  156                 SET(sc->sc_hwflags, COM_HW_CONSOLE);
  157                 SET(sc->sc_swflags, COM_SW_SOFTCAR);
  158         }
  159 #endif
  160 
  161         /*
  162          * Probe for all known forms of UART.
  163          */
  164         lcr = bus_space_read_1(iot, ioh, com_lcr);
  165         bus_space_write_1(iot, ioh, com_lcr, LCR_EFR);
  166         bus_space_write_1(iot, ioh, com_efr, 0);
  167         bus_space_write_1(iot, ioh, com_lcr, 0);
  168 
  169         bus_space_write_1(iot, ioh, com_fifo, FIFO_ENABLE);
  170         delay(100);
  171 
  172         /*
  173          * Skip specific probes if attachment code knows it already.
  174          */
  175         if (sc->sc_uarttype == COM_UART_UNKNOWN)
  176                 switch (bus_space_read_1(iot, ioh, com_iir) >> 6) {
  177                 case 0:
  178                         sc->sc_uarttype = COM_UART_16450;
  179                         break;
  180                 case 2:
  181                         sc->sc_uarttype = COM_UART_16550;
  182                         break;
  183                 case 3:
  184                         sc->sc_uarttype = COM_UART_16550A;
  185                         break;
  186                 default:
  187                         sc->sc_uarttype = COM_UART_UNKNOWN;
  188                         break;
  189                 }
  190 
  191         if (sc->sc_uarttype == COM_UART_16550A) { /* Probe for ST16650s */
  192                 bus_space_write_1(iot, ioh, com_lcr, lcr | LCR_DLAB);
  193                 if (bus_space_read_1(iot, ioh, com_efr) == 0) {
  194                         sc->sc_uarttype = COM_UART_ST16650;
  195                 } else {
  196                         bus_space_write_1(iot, ioh, com_lcr, LCR_EFR);
  197                         if (bus_space_read_1(iot, ioh, com_efr) == 0)
  198                                 sc->sc_uarttype = COM_UART_ST16650V2;
  199                 }
  200         }
  201 
  202 #if NPCCOM > 0  /* until com works with large FIFOs */
  203         if (sc->sc_uarttype == COM_UART_ST16650V2) { /* Probe for XR16850s */
  204                 u_int8_t dlbl, dlbh;
  205 
  206                 /* Enable latch access and get the current values. */
  207                 bus_space_write_1(iot, ioh, com_lcr, lcr | LCR_DLAB);
  208                 dlbl = bus_space_read_1(iot, ioh, com_dlbl);
  209                 dlbh = bus_space_read_1(iot, ioh, com_dlbh);
  210 
  211                 /* Zero out the latch divisors */
  212                 bus_space_write_1(iot, ioh, com_dlbl, 0);
  213                 bus_space_write_1(iot, ioh, com_dlbh, 0);
  214 
  215                 if (bus_space_read_1(iot, ioh, com_dlbh) == 0x10) {
  216                         sc->sc_uarttype = COM_UART_XR16850;
  217                         sc->sc_uartrev = bus_space_read_1(iot, ioh, com_dlbl);
  218                 }
  219 
  220                 /* Reset to original. */
  221                 bus_space_write_1(iot, ioh, com_dlbl, dlbl);
  222                 bus_space_write_1(iot, ioh, com_dlbh, dlbh);
  223         }
  224 #endif
  225 
  226         if (sc->sc_uarttype == COM_UART_16550A) { /* Probe for TI16750s */
  227                 bus_space_write_1(iot, ioh, com_lcr, lcr | LCR_DLAB);
  228                 bus_space_write_1(iot, ioh, com_fifo,
  229                     FIFO_ENABLE | FIFO_ENABLE_64BYTE);
  230                 if ((bus_space_read_1(iot, ioh, com_iir) >> 5) == 7) {
  231 #if 0
  232                         bus_space_write_1(iot, ioh, com_lcr, 0);
  233                         if ((bus_space_read_1(iot, ioh, com_iir) >> 5) == 6)
  234 #endif
  235                                 sc->sc_uarttype = COM_UART_TI16750;
  236                 }
  237                 bus_space_write_1(iot, ioh, com_fifo, FIFO_ENABLE);
  238         }
  239 
  240         /* Reset the LCR (latch access is probably enabled). */
  241         bus_space_write_1(iot, ioh, com_lcr, lcr);
  242         if (sc->sc_uarttype == COM_UART_16450) { /* Probe for 8250 */
  243                 u_int8_t scr0, scr1, scr2;
  244 
  245                 scr0 = bus_space_read_1(iot, ioh, com_scratch);
  246                 bus_space_write_1(iot, ioh, com_scratch, 0xa5);
  247                 scr1 = bus_space_read_1(iot, ioh, com_scratch);
  248                 bus_space_write_1(iot, ioh, com_scratch, 0x5a);
  249                 scr2 = bus_space_read_1(iot, ioh, com_scratch);
  250                 bus_space_write_1(iot, ioh, com_scratch, scr0);
  251 
  252                 if ((scr1 != 0xa5) || (scr2 != 0x5a))
  253                         sc->sc_uarttype = COM_UART_8250;
  254         }
  255 
  256         /*
  257          * Print UART type and initialize ourself.
  258          */
  259         switch (sc->sc_uarttype) {
  260         case COM_UART_UNKNOWN:
  261                 printf(": unknown uart\n");
  262                 break;
  263         case COM_UART_8250:
  264                 printf(": ns8250, no fifo\n");
  265                 break;
  266         case COM_UART_16450:
  267                 printf(": ns16450, no fifo\n");
  268                 break;
  269         case COM_UART_16550:
  270                 printf(": ns16550, no working fifo\n");
  271                 break;
  272         case COM_UART_16550A:
  273                 if (sc->sc_fifolen == 0)
  274                         sc->sc_fifolen = 16;
  275                 printf(": ns16550a, %d byte fifo\n", sc->sc_fifolen);
  276                 SET(sc->sc_hwflags, COM_HW_FIFO);
  277                 break;
  278 #ifdef COM_PXA2X0
  279         case COM_UART_PXA2X0:
  280                 printf(": pxa2x0, 32 byte fifo");
  281                 SET(sc->sc_hwflags, COM_HW_FIFO);
  282                 sc->sc_fifolen = 32;
  283                 if (sc->sc_iobase == comsiraddr) {
  284                         SET(sc->sc_hwflags, COM_HW_SIR);
  285                         printf(" (SIR)");
  286                 }
  287                 printf("\n");
  288                 break;
  289 #endif
  290         case COM_UART_ST16650:
  291                 printf(": st16650, no working fifo\n");
  292                 break;
  293         case COM_UART_ST16650V2:
  294                 if (sc->sc_fifolen == 0)
  295                         sc->sc_fifolen = 32;
  296                 printf(": st16650, %d byte fifo\n", sc->sc_fifolen);
  297                 SET(sc->sc_hwflags, COM_HW_FIFO);
  298                 break;
  299         case COM_UART_ST16C654:
  300                 printf(": st16c654, 64 byte fifo\n");
  301                 SET(sc->sc_hwflags, COM_HW_FIFO);
  302                 sc->sc_fifolen = 64;
  303                 break;
  304         case COM_UART_TI16750:
  305                 printf(": ti16750, 64 byte fifo\n");
  306                 SET(sc->sc_hwflags, COM_HW_FIFO);
  307                 sc->sc_fifolen = 64;
  308                 break;
  309 #if NPCCOM > 0
  310         case COM_UART_XR16850:
  311                 printf(": xr16850 (rev %d), 128 byte fifo\n", sc->sc_uartrev);
  312                 SET(sc->sc_hwflags, COM_HW_FIFO);
  313                 sc->sc_fifolen = 128;
  314                 break;
  315 #ifdef COM_UART_OX16C950
  316         case COM_UART_OX16C950:
  317                 printf(": ox16c950 (rev %d), 128 byte fifo\n", sc->sc_uartrev);
  318                 SET(sc->sc_hwflags, COM_HW_FIFO);
  319                 sc->sc_fifolen = 128;
  320                 break;
  321 #endif
  322 #endif
  323         default:
  324                 panic("comattach: bad fifo type");
  325         }
  326 
  327 #ifdef notyet
  328         com_fifo_probe(sc);
  329 #endif
  330 
  331         if (sc->sc_fifolen == 0) {
  332                 CLR(sc->sc_hwflags, COM_HW_FIFO);
  333                 sc->sc_fifolen = 1;
  334         }
  335 
  336         /* clear and disable fifo */
  337         bus_space_write_1(iot, ioh, com_fifo, FIFO_RCV_RST | FIFO_XMT_RST);
  338         (void)bus_space_read_1(iot, ioh, com_data);
  339         bus_space_write_1(iot, ioh, com_fifo, 0);
  340 
  341         sc->sc_mcr = 0;
  342         bus_space_write_1(iot, ioh, com_mcr, sc->sc_mcr);
  343 
  344 #ifdef KGDB
  345         /*
  346          * Allow kgdb to "take over" this port.  If this is
  347          * the kgdb device, it has exclusive use.
  348          */
  349 
  350         if (iot == com_kgdb_iot && sc->sc_iobase == com_kgdb_addr &&
  351             !ISSET(sc->sc_hwflags, COM_HW_CONSOLE)) {
  352                 printf("%s: kgdb\n", sc->sc_dev.dv_xname);
  353                 SET(sc->sc_hwflags, COM_HW_KGDB);
  354         }
  355 #endif /* KGDB */
  356 
  357 #ifdef COM_CONSOLE
  358         if (ISSET(sc->sc_hwflags, COM_HW_CONSOLE)) {
  359                 int maj;
  360 
  361                 /* locate the major number */
  362                 for (maj = 0; maj < nchrdev; maj++)
  363                         if (cdevsw[maj].d_open == comopen)
  364                                 break;
  365 
  366                 if (maj < nchrdev && cn_tab->cn_dev == NODEV)
  367                         cn_tab->cn_dev = makedev(maj, sc->sc_dev.dv_unit);
  368 
  369                 printf("%s: console\n", sc->sc_dev.dv_xname);
  370         }
  371 #endif
  372 
  373         timeout_set(&sc->sc_diag_tmo, comdiag, sc);
  374         timeout_set(&sc->sc_dtr_tmo, com_raisedtr, sc);
  375 #if NCOM > 0
  376 #ifdef __HAVE_GENERIC_SOFT_INTERRUPTS
  377         sc->sc_si = softintr_establish(IPL_TTY, comsoft, sc);
  378         if (sc->sc_si == NULL)
  379                 panic("%s: can't establish soft interrupt",
  380                     sc->sc_dev.dv_xname);
  381 #else
  382         timeout_set(&sc->sc_comsoft_tmo, comsoft, sc);
  383 #endif
  384 #endif  /* NCOM */
  385 
  386         /*
  387          * If there are no enable/disable functions, assume the device
  388          * is always enabled.
  389          */
  390         if (!sc->enable)
  391                 sc->enabled = 1;
  392 
  393 #if defined(COM_CONSOLE) || defined(KGDB)
  394         if (ISSET(sc->sc_hwflags, COM_HW_CONSOLE|COM_HW_KGDB))
  395                 com_enable_debugport(sc);
  396 #endif
  397 }
  398 
  399 void
  400 com_fifo_probe(struct com_softc *sc)
  401 {
  402         bus_space_handle_t ioh = sc->sc_ioh;
  403         bus_space_tag_t iot = sc->sc_iot;
  404         u_int8_t fifo, ier;
  405         int timo, len;
  406 
  407         if (!ISSET(sc->sc_hwflags, COM_HW_FIFO))
  408                 return;
  409 
  410         ier = 0;
  411 #ifdef COM_PXA2X0
  412         if (sc->sc_uarttype == COM_UART_PXA2X0)
  413                 ier |= IER_EUART;
  414 #endif
  415         bus_space_write_1(iot, ioh, com_ier, ier);
  416         bus_space_write_1(iot, ioh, com_lcr, LCR_DLAB);
  417         bus_space_write_1(iot, ioh, com_dlbl, 3);
  418         bus_space_write_1(iot, ioh, com_dlbh, 0);
  419         bus_space_write_1(iot, ioh, com_lcr, LCR_PNONE | LCR_8BITS);
  420         bus_space_write_1(iot, ioh, com_mcr, MCR_LOOPBACK);
  421 
  422         fifo = FIFO_ENABLE | FIFO_RCV_RST | FIFO_XMT_RST;
  423         if (sc->sc_uarttype == COM_UART_TI16750)
  424                 fifo |= FIFO_ENABLE_64BYTE;
  425 
  426         bus_space_write_1(iot, ioh, com_fifo, fifo);
  427 
  428         for (len = 0; len < 256; len++) {
  429                 bus_space_write_1(iot, ioh, com_data, (len + 1));
  430                 timo = 2000;
  431                 while (!ISSET(bus_space_read_1(iot, ioh, com_lsr),
  432                     LSR_TXRDY) && --timo)
  433                         delay(1);
  434                 if (!timo)
  435                         break;
  436         }
  437 
  438         delay(100);
  439 
  440         for (len = 0; len < 256; len++) {
  441                 timo = 2000;
  442                 while (!ISSET(bus_space_read_1(iot, ioh, com_lsr),
  443                     LSR_RXRDY) && --timo)
  444                         delay(1);
  445                 if (!timo || bus_space_read_1(iot, ioh, com_data) != (len + 1))
  446                         break;
  447         }
  448 
  449         /* For safety, always use the smaller value. */
  450         if (sc->sc_fifolen > len) {
  451                 printf("%s: probed fifo depth: %d bytes\n",
  452                     sc->sc_dev.dv_xname, len);
  453                 sc->sc_fifolen = len;
  454         }
  455 }

/* [<][>][^][v][top][bottom][index][help] */