1 /*	$NetBSD: cy_pci.c,v 1.25 2016/07/07 06:55:41 msaitoh 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.25 2016/07/07 06:55:41 msaitoh 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 
30 struct 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 
37 static 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 };
58 static const int cy_pci_nproducts =
59     sizeof(cy_pci_products) / sizeof(cy_pci_products[0]);
60 
61 static const struct cy_pci_product *
cy_pci_lookup(const struct pci_attach_args * pa)62 cy_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 
78 static int
cy_pci_match(device_t parent,cfdata_t match,void * aux)79 cy_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 
86 static void
cy_pci_attach(device_t parent,device_t self,void * aux)87 cy_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,
119 		    "unable to map device registers\n");
120 		return;
121 	}
122 
123 	if (cy_find(sc) == 0) {
124 		aprint_error_dev(sc->sc_dev, "unable to find CD1400s\n");
125 		return;
126 	}
127 
128 	/*
129 	 * XXX Like the Cyclades-Z, we should really check the EEPROM to
130 	 * determine the "poll or interrupt" setting.  For now, we always
131 	 * map the interrupt and enable it in the PLX.
132 	 */
133 
134 	/* Map and establish the interrupt. */
135 	if (pci_intr_map(pa, &ih) != 0) {
136 		aprint_error_dev(sc->sc_dev, "unable to map interrupt\n");
137 		return;
138 	}
139 	intrstr = pci_intr_string(pa->pa_pc, ih, intrbuf, sizeof(intrbuf));
140 	sc->sc_ih = pci_intr_establish(pa->pa_pc, ih, IPL_TTY, cy_intr, sc);
141 	if (sc->sc_ih == NULL) {
142 		aprint_error_dev(sc->sc_dev, "unable to establish interrupt");
143 		if (intrstr != NULL)
144 			aprint_error(" at %s", intrstr);
145 		aprint_error("\n");
146 		return;
147 	}
148 	aprint_normal_dev(sc->sc_dev, "interrupting at %s\n", intrstr);
149 
150 	cy_attach(sc);
151 
152 	plx_ver = bus_space_read_1(sc->sc_memt, sc->sc_bsh, CY_PLX_VER) & 0x0f;
153 
154 	/* Enable PCI card interrupts */
155 	switch (plx_ver) {
156 	case CY_PLX_9050:
157 		bus_space_write_2(psc->sc_iot, psc->sc_ioh, CY_PCI_INTENA_9050,
158 		    bus_space_read_2(psc->sc_iot, psc->sc_ioh,
159 		    CY_PCI_INTENA_9050) | 0x40);
160 		break;
161 
162 	case CY_PLX_9060:
163 	case CY_PLX_9080:
164 	default:
165 		bus_space_write_2(psc->sc_iot, psc->sc_ioh, CY_PCI_INTENA,
166 		    bus_space_read_2(psc->sc_iot, psc->sc_ioh,
167 		    CY_PCI_INTENA) | 0x900);
168 	}
169 }
170 
171 CFATTACH_DECL_NEW(cy_pci, sizeof(struct cy_pci_softc),
172     cy_pci_match, cy_pci_attach, NULL, NULL);
173