Home | History | Annotate | Line # | Download | only in pci
puccn.c revision 1.13
      1  1.13  msaitoh /*	$NetBSD: puccn.c,v 1.13 2014/01/26 10:54:24 msaitoh Exp $ */
      2   1.1    jeffs 
      3   1.1    jeffs /*
      4   1.1    jeffs  * Derived from  pci.c
      5   1.1    jeffs  * Copyright (c) 2000 Geocast Networks Systems.  All rights reserved.
      6   1.1    jeffs  *
      7   1.1    jeffs  * Copyright (c) 1995, 1996, 1997, 1998
      8   1.1    jeffs  *     Christopher G. Demetriou.  All rights reserved.
      9   1.1    jeffs  * Copyright (c) 1994 Charles M. Hannum.  All rights reserved.
     10   1.1    jeffs  *
     11   1.1    jeffs  * Redistribution and use in source and binary forms, with or without
     12   1.1    jeffs  * modification, are permitted provided that the following conditions
     13   1.1    jeffs  * are met:
     14   1.1    jeffs  * 1. Redistributions of source code must retain the above copyright
     15   1.1    jeffs  *    notice, this list of conditions and the following disclaimer.
     16   1.1    jeffs  * 2. Redistributions in binary form must reproduce the above copyright
     17   1.1    jeffs  *    notice, this list of conditions and the following disclaimer in the
     18   1.1    jeffs  *    documentation and/or other materials provided with the distribution.
     19   1.1    jeffs  * 3. All advertising materials mentioning features or use of this software
     20   1.1    jeffs  *    must display the following acknowledgement:
     21   1.1    jeffs  *	This product includes software developed by Charles M. Hannum.
     22   1.1    jeffs  * 4. The name of the author may not be used to endorse or promote products
     23   1.1    jeffs  *    derived from this software without specific prior written permission.
     24   1.1    jeffs  *
     25   1.1    jeffs  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     26   1.1    jeffs  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     27   1.1    jeffs  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     28   1.1    jeffs  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     29   1.1    jeffs  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     30   1.1    jeffs  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     31   1.1    jeffs  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     32   1.1    jeffs  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     33   1.1    jeffs  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     34   1.1    jeffs  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     35   1.1    jeffs  */
     36   1.1    jeffs 
     37   1.1    jeffs 
     38   1.1    jeffs /*
     39   1.1    jeffs  * Machine independent support for PCI serial console support.
     40   1.1    jeffs  *
     41   1.1    jeffs  * Scan the PCI bus for something which resembles a 16550
     42   1.1    jeffs  */
     43   1.4    lukem 
     44   1.4    lukem #include <sys/cdefs.h>
     45  1.13  msaitoh __KERNEL_RCSID(0, "$NetBSD: puccn.c,v 1.13 2014/01/26 10:54:24 msaitoh Exp $");
     46   1.3    lukem 
     47   1.3    lukem #include "opt_kgdb.h"
     48   1.1    jeffs 
     49   1.1    jeffs #include <sys/param.h>
     50   1.1    jeffs #include <sys/systm.h>
     51   1.1    jeffs #include <sys/conf.h>
     52   1.1    jeffs #include <sys/device.h>
     53   1.1    jeffs 
     54   1.1    jeffs #include <dev/pci/pcireg.h>
     55   1.1    jeffs #include <dev/pci/pcivar.h>
     56   1.1    jeffs #include <dev/pci/pcidevs.h>
     57   1.1    jeffs 
     58   1.1    jeffs #include <sys/termios.h>
     59   1.1    jeffs #include <dev/ic/comreg.h>
     60   1.1    jeffs #include <dev/ic/comvar.h>
     61   1.1    jeffs 
     62   1.1    jeffs #include <dev/cons.h>
     63   1.1    jeffs 
     64   1.1    jeffs #include <dev/pci/pucvar.h>
     65   1.1    jeffs #include <dev/pci/puccn.h>
     66   1.1    jeffs 
     67   1.1    jeffs #ifndef CONSPEED
     68   1.1    jeffs #define CONSPEED	TTYDEF_SPEED
     69   1.1    jeffs #endif
     70   1.1    jeffs #ifndef CONMODE
     71   1.1    jeffs #define	CONMODE		((TTYDEF_CFLAG & ~(CSIZE|CSTOPB|PARENB))|CS8) /* 8N1 */
     72   1.1    jeffs #endif
     73   1.1    jeffs 
     74   1.1    jeffs cons_decl(com);
     75   1.1    jeffs 
     76   1.1    jeffs static bus_addr_t puccnbase;
     77   1.1    jeffs static bus_space_tag_t puctag;
     78  1.13  msaitoh static int puccnflags;
     79   1.1    jeffs 
     80   1.1    jeffs #ifdef KGDB
     81   1.1    jeffs static bus_addr_t pucgdbbase;
     82   1.1    jeffs #endif
     83   1.1    jeffs 
     84   1.1    jeffs /*
     85   1.1    jeffs  * Static dev/func variables allow pucprobe to be called multiple times,
     86   1.1    jeffs  * resuming the search where it left off, never retrying the same adaptor.
     87   1.1    jeffs  */
     88   1.1    jeffs 
     89   1.1    jeffs static bus_addr_t
     90   1.1    jeffs pucprobe_doit(struct consdev *cn)
     91   1.1    jeffs {
     92   1.1    jeffs 	struct pci_attach_args pa;
     93   1.1    jeffs 	int bus;
     94   1.1    jeffs 	static int dev = 0, func = 0;
     95   1.9     cube 	int maxdev, nfunctions = 0, i; /* XXX */
     96   1.9     cube 	pcireg_t reg, bhlcr, subsys = 0; /* XXX */
     97   1.1    jeffs 	int foundport = 0;
     98   1.1    jeffs 	const struct puc_device_description *desc;
     99   1.1    jeffs 	pcireg_t base;
    100   1.1    jeffs 
    101   1.1    jeffs 	/* Fetch our tags */
    102  1.11    soren #if defined(amd64) || defined(i386)
    103  1.13  msaitoh 	if (cpu_puc_cnprobe(cn, &pa) != 0)
    104  1.11    soren #endif
    105   1.1    jeffs 		return 0;
    106  1.11    soren 
    107   1.1    jeffs 	pci_decompose_tag(pa.pa_pc, pa.pa_tag, &bus, &maxdev, NULL);
    108   1.1    jeffs 
    109  1.12  msaitoh 	/* Scan through devices and find a communication class device. */
    110   1.1    jeffs 	for (; dev <= maxdev ; dev++) {
    111   1.1    jeffs 		pa.pa_tag = pci_make_tag(pa.pa_pc, bus, dev, 0);
    112   1.1    jeffs 		reg = pci_conf_read(pa.pa_pc, pa.pa_tag, PCI_ID_REG);
    113   1.1    jeffs 		if (PCI_VENDOR(reg) == PCI_VENDOR_INVALID
    114   1.1    jeffs 		    || PCI_VENDOR(reg) == 0)
    115   1.1    jeffs 			continue;
    116   1.1    jeffs 		bhlcr = pci_conf_read(pa.pa_pc, pa.pa_tag, PCI_BHLC_REG);
    117   1.1    jeffs 		if (PCI_HDRTYPE_MULTIFN(bhlcr)) {
    118   1.1    jeffs 			nfunctions = 8;
    119   1.1    jeffs 		} else {
    120   1.1    jeffs 			nfunctions = 1;
    121   1.1    jeffs 		}
    122   1.1    jeffs resume_scan:
    123   1.1    jeffs 		for (; func < nfunctions; func++)  {
    124   1.1    jeffs 			pa.pa_tag = pci_make_tag(pa.pa_pc, bus, dev, func);
    125   1.1    jeffs 			reg = pci_conf_read(pa.pa_pc, pa.pa_tag, PCI_CLASS_REG);
    126   1.1    jeffs 			if (PCI_CLASS(reg)  == PCI_CLASS_COMMUNICATIONS
    127   1.1    jeffs 			    && PCI_SUBCLASS(reg)
    128   1.1    jeffs 			       == PCI_SUBCLASS_COMMUNICATIONS_SERIAL) {
    129   1.1    jeffs 				pa.pa_id = pci_conf_read(pa.pa_pc, pa.pa_tag, PCI_ID_REG);
    130   1.1    jeffs 				subsys = pci_conf_read(pa.pa_pc, pa.pa_tag,
    131   1.1    jeffs 				    PCI_SUBSYS_ID_REG);
    132   1.1    jeffs 				foundport = 1;
    133   1.1    jeffs 				break;
    134   1.1    jeffs 			}
    135   1.1    jeffs 		}
    136   1.1    jeffs 		if (foundport)
    137   1.1    jeffs 			break;
    138   1.1    jeffs 
    139   1.1    jeffs 		func = 0;
    140   1.1    jeffs 	}
    141  1.12  msaitoh 
    142  1.12  msaitoh 	/*
    143  1.12  msaitoh 	 * If all devices was scanned and couldn't find any communication
    144  1.12  msaitoh 	 * device, return with 0.
    145  1.12  msaitoh 	 */
    146   1.1    jeffs 	if (!foundport)
    147   1.1    jeffs 		return 0;
    148  1.13  msaitoh 
    149  1.13  msaitoh 	/* Clear foundport flag */
    150   1.1    jeffs 	foundport = 0;
    151   1.1    jeffs 
    152  1.12  msaitoh 	/* Check whether the device is in the puc device table or not */
    153   1.1    jeffs 	desc = puc_find_description(PCI_VENDOR(pa.pa_id),
    154   1.1    jeffs 	    PCI_PRODUCT(pa.pa_id), PCI_VENDOR(subsys), PCI_PRODUCT(subsys));
    155  1.12  msaitoh 
    156  1.12  msaitoh 	/* If not, check the next communication device */
    157   1.1    jeffs 	if (desc == NULL) {
    158  1.12  msaitoh 		/* Resume from the next function */
    159   1.1    jeffs 		func++;
    160   1.1    jeffs 		goto resume_scan;
    161   1.1    jeffs 	}
    162   1.1    jeffs 
    163  1.12  msaitoh 	/*
    164  1.12  msaitoh 	 * We found a device and it's on the puc table. Set the tag and
    165  1.12  msaitoh 	 * the base address.
    166  1.12  msaitoh 	 */
    167   1.1    jeffs 	for (i = 0; PUC_PORT_VALID(desc, i); i++)
    168   1.1    jeffs 	{
    169   1.1    jeffs 		if (desc->ports[i].type != PUC_PORT_TYPE_COM)
    170   1.1    jeffs 			continue;
    171  1.13  msaitoh 		puccnflags = desc->ports[i].flags;
    172   1.1    jeffs 		base = pci_conf_read(pa.pa_pc, pa.pa_tag, desc->ports[i].bar);
    173   1.1    jeffs 		base += desc->ports[i].offset;
    174   1.1    jeffs 
    175  1.13  msaitoh 		if (PCI_MAPREG_TYPE(base) == PCI_MAPREG_TYPE_IO) {
    176  1.13  msaitoh 			puctag = pa.pa_iot;
    177  1.13  msaitoh 			base = PCI_MAPREG_IO_ADDR(base);
    178  1.13  msaitoh 		}
    179  1.13  msaitoh #if 0 /* For MMIO device */
    180  1.13  msaitoh 		else {
    181  1.13  msaitoh 			puctag = pa.pa_memt;
    182  1.13  msaitoh 			base = PCI_MAPREG_MEM_ADDR(base);
    183  1.13  msaitoh 		}
    184  1.13  msaitoh #endif
    185  1.13  msaitoh 
    186   1.1    jeffs 		if (com_is_console(puctag, base, NULL))
    187   1.1    jeffs 			continue;
    188   1.1    jeffs 		foundport = 1;
    189   1.1    jeffs 		break;
    190   1.1    jeffs 	}
    191   1.7    perry 
    192   1.1    jeffs 	if (foundport == 0) {
    193   1.1    jeffs 		func++;
    194   1.1    jeffs 		goto resume_scan;
    195   1.1    jeffs 	}
    196   1.1    jeffs 
    197  1.13  msaitoh #if 0
    198   1.1    jeffs 	cn->cn_pri = CN_REMOTE;
    199  1.13  msaitoh #else
    200  1.13  msaitoh 	if (cn)
    201  1.13  msaitoh 		cn->cn_pri = CN_REMOTE;
    202  1.13  msaitoh #endif
    203  1.13  msaitoh 	return base;
    204   1.1    jeffs }
    205   1.1    jeffs 
    206   1.1    jeffs #ifdef KGDB
    207   1.1    jeffs void comgdbprobe(struct consdev *);
    208   1.1    jeffs void comgdbinit(struct consdev *);
    209   1.1    jeffs 
    210   1.1    jeffs void
    211   1.1    jeffs comgdbprobe(struct consdev *cn)
    212   1.1    jeffs {
    213  1.12  msaitoh 
    214   1.1    jeffs 	pucgdbbase = pucprobe_doit(cn);
    215   1.1    jeffs }
    216   1.1    jeffs 
    217   1.1    jeffs void
    218   1.1    jeffs comgdbinit(struct consdev *cn)
    219   1.1    jeffs {
    220  1.12  msaitoh 
    221  1.12  msaitoh 	if (pucgdbbase == 0)
    222   1.1    jeffs 		return;
    223  1.12  msaitoh 
    224   1.6  thorpej 	com_kgdb_attach(puctag, pucgdbbase, CONSPEED, COM_FREQ,
    225   1.6  thorpej 	    COM_TYPE_NORMAL, CONMODE);
    226   1.1    jeffs }
    227   1.1    jeffs #endif
    228   1.1    jeffs 
    229   1.1    jeffs void
    230  1.13  msaitoh puc_cnprobe(struct consdev *cn)
    231   1.1    jeffs {
    232  1.12  msaitoh 
    233   1.1    jeffs 	puccnbase = pucprobe_doit(cn);
    234   1.1    jeffs }
    235   1.1    jeffs 
    236  1.13  msaitoh int
    237  1.13  msaitoh puc_cninit(struct consdev *cn)
    238   1.1    jeffs {
    239  1.13  msaitoh 
    240  1.12  msaitoh 	if (puccnbase == 0)
    241  1.13  msaitoh 		return -1;
    242  1.12  msaitoh 
    243  1.13  msaitoh 	return comcnattach(puctag, puccnbase, CONSPEED,
    244  1.13  msaitoh 	    puccnflags & PUC_COM_CLOCKMASK, COM_TYPE_NORMAL,
    245   1.6  thorpej 	    CONMODE);
    246   1.1    jeffs }
    247   1.1    jeffs 
    248   1.1    jeffs /* comcngetc, comcnputc, comcnpollc provided by dev/ic/com.c */
    249