cy_pci.c revision 1.24
1/*	$NetBSD: cy_pci.c,v 1.24 2014/03/29 19:28:24 christos Exp $	*/
2
3/*
4 * cy_pci.c
5 *
6 * Driver for Cyclades Cyclom-8/16/32 multiport serial cards
7 * (currently not tested with Cyclom-32 cards)
8 *
9 * Timo Rossi, 1996
10 */
11
12#include <sys/cdefs.h>
13__KERNEL_RCSID(0, "$NetBSD: cy_pci.c,v 1.24 2014/03/29 19:28:24 christos Exp $");
14
15#include <sys/param.h>
16#include <sys/systm.h>
17#include <sys/device.h>
18
19#include <sys/bus.h>
20#include <sys/intr.h>
21
22#include <dev/pci/pcivar.h>
23#include <dev/pci/pcireg.h>
24#include <dev/pci/pcidevs.h>
25
26#include <dev/ic/cd1400reg.h>
27#include <dev/ic/cyreg.h>
28#include <dev/ic/cyvar.h>
29
30struct cy_pci_softc {
31	struct cy_softc sc_cy;		/* real cy softc */
32
33	bus_space_tag_t sc_iot;		/* PLX runtime i/o tag */
34	bus_space_handle_t sc_ioh;	/* PLX runtime i/o handle */
35};
36
37static const struct cy_pci_product {
38	pci_product_id_t cp_product;	/* product ID */
39	pcireg_t cp_memtype;		/* memory type */
40} cy_pci_products[] = {
41	{ PCI_PRODUCT_CYCLADES_CYCLOMY_1,
42	  PCI_MAPREG_TYPE_MEM|PCI_MAPREG_MEM_TYPE_32BIT_1M },
43	{ PCI_PRODUCT_CYCLADES_CYCLOM4Y_1,
44	  PCI_MAPREG_TYPE_MEM|PCI_MAPREG_MEM_TYPE_32BIT_1M },
45	{ PCI_PRODUCT_CYCLADES_CYCLOM8Y_1,
46	  PCI_MAPREG_TYPE_MEM|PCI_MAPREG_MEM_TYPE_32BIT_1M },
47
48	{ PCI_PRODUCT_CYCLADES_CYCLOMY_2,
49	  PCI_MAPREG_TYPE_MEM|PCI_MAPREG_MEM_TYPE_32BIT },
50	{ PCI_PRODUCT_CYCLADES_CYCLOM4Y_2,
51	  PCI_MAPREG_TYPE_MEM|PCI_MAPREG_MEM_TYPE_32BIT },
52	{ PCI_PRODUCT_CYCLADES_CYCLOM8Y_2,
53	  PCI_MAPREG_TYPE_MEM|PCI_MAPREG_MEM_TYPE_32BIT },
54
55	{ 0,
56	  0 },
57};
58static const int cy_pci_nproducts =
59    sizeof(cy_pci_products) / sizeof(cy_pci_products[0]);
60
61static const struct cy_pci_product *
62cy_pci_lookup(const struct pci_attach_args *pa)
63{
64	const struct cy_pci_product *cp;
65	int i;
66
67	if (PCI_VENDOR(pa->pa_id) != PCI_VENDOR_CYCLADES)
68		return (NULL);
69
70	for (i = 0; i < cy_pci_nproducts; i++) {
71		cp = &cy_pci_products[i];
72		if (PCI_PRODUCT(pa->pa_id) == cp->cp_product)
73			return (cp);
74	}
75	return (NULL);
76}
77
78static int
79cy_pci_match(device_t parent, cfdata_t match, void *aux)
80{
81	struct pci_attach_args *pa = aux;
82
83	return (cy_pci_lookup(pa) != NULL);
84}
85
86static void
87cy_pci_attach(device_t parent, device_t self, void *aux)
88{
89	struct cy_pci_softc *psc = device_private(self);
90	struct cy_softc *sc = &psc->sc_cy;
91	struct pci_attach_args *pa = aux;
92	pci_intr_handle_t ih;
93	const struct cy_pci_product *cp;
94	const char *intrstr;
95	int plx_ver;
96	char intrbuf[PCI_INTRSTR_LEN];
97
98	sc->sc_dev = self;
99
100	aprint_naive(": Multi-port serial controller\n");
101
102	sc->sc_bustype = CY_BUSTYPE_PCI;
103
104	cp = cy_pci_lookup(pa);
105	if (cp == NULL)
106		panic("cy_pci_attach: impossible");
107
108	aprint_normal(": Cyclades-Y multiport serial\n");
109
110	if (pci_mapreg_map(pa, 0x14, PCI_MAPREG_TYPE_IO, 0,
111	    &psc->sc_iot, &psc->sc_ioh, NULL, NULL) != 0) {
112		aprint_error_dev(sc->sc_dev, "unable to map PLX registers\n");
113		return;
114	}
115
116	if (pci_mapreg_map(pa, 0x18, cp->cp_memtype, 0,
117	    &sc->sc_memt, &sc->sc_bsh, NULL, NULL) != 0) {
118		aprint_error_dev(sc->sc_dev,"unable to map device registers\n");
119		return;
120	}
121
122	if (cy_find(sc) == 0) {
123		aprint_error_dev(sc->sc_dev, "unable to find CD1400s\n");
124		return;
125	}
126
127	/*
128	 * XXX Like the Cyclades-Z, we should really check the EEPROM to
129	 * determine the "poll or interrupt" setting.  For now, we always
130	 * map the interrupt and enable it in the PLX.
131	 */
132
133	/* Map and establish the interrupt. */
134	if (pci_intr_map(pa, &ih) != 0) {
135		aprint_error_dev(sc->sc_dev, "unable to map interrupt\n");
136		return;
137	}
138	intrstr = pci_intr_string(pa->pa_pc, ih, intrbuf, sizeof(intrbuf));
139	sc->sc_ih = pci_intr_establish(pa->pa_pc, ih, IPL_TTY, cy_intr, sc);
140	if (sc->sc_ih == NULL) {
141		aprint_error_dev(sc->sc_dev, "unable to establish interrupt");
142		if (intrstr != NULL)
143			aprint_error(" at %s", intrstr);
144		aprint_error("\n");
145		return;
146	}
147	aprint_normal_dev(sc->sc_dev, "interrupting at %s\n", intrstr);
148
149	cy_attach(sc);
150
151	plx_ver = bus_space_read_1(sc->sc_memt, sc->sc_bsh, CY_PLX_VER) & 0x0f;
152
153	/* Enable PCI card interrupts */
154	switch (plx_ver) {
155	case CY_PLX_9050:
156		bus_space_write_2(psc->sc_iot, psc->sc_ioh, CY_PCI_INTENA_9050,
157		    bus_space_read_2(psc->sc_iot, psc->sc_ioh,
158		    CY_PCI_INTENA_9050) | 0x40);
159		break;
160
161	case CY_PLX_9060:
162	case CY_PLX_9080:
163	default:
164		bus_space_write_2(psc->sc_iot, psc->sc_ioh, CY_PCI_INTENA,
165		    bus_space_read_2(psc->sc_iot, psc->sc_ioh,
166		    CY_PCI_INTENA) | 0x900);
167	}
168}
169
170CFATTACH_DECL_NEW(cy_pci, sizeof(struct cy_pci_softc),
171    cy_pci_match, cy_pci_attach, NULL, NULL);
172