1*71034770Sskrll /*	$NetBSD: footbridge_pci.c,v 1.34 2022/09/27 06:36:41 skrll Exp $	*/
2af8ce959Schris 
3af8ce959Schris /*
4af8ce959Schris  * Copyright (c) 1997,1998 Mark Brinicombe.
5af8ce959Schris  * Copyright (c) 1997,1998 Causality Limited
6af8ce959Schris  * All rights reserved.
7af8ce959Schris  *
8af8ce959Schris  * Redistribution and use in source and binary forms, with or without
9af8ce959Schris  * modification, are permitted provided that the following conditions
10af8ce959Schris  * are met:
11af8ce959Schris  * 1. Redistributions of source code must retain the above copyright
12af8ce959Schris  *    notice, this list of conditions and the following disclaimer.
13af8ce959Schris  * 2. Redistributions in binary form must reproduce the above copyright
14af8ce959Schris  *    notice, this list of conditions and the following disclaimer in the
15af8ce959Schris  *    documentation and/or other materials provided with the distribution.
16af8ce959Schris  * 3. All advertising materials mentioning features or use of this software
17af8ce959Schris  *    must display the following acknowledgement:
18af8ce959Schris  *	This product includes software developed by Mark Brinicombe
19af8ce959Schris  *	for the NetBSD Project.
20af8ce959Schris  * 4. The name of the company nor the name of the author may be used to
21af8ce959Schris  *    endorse or promote products derived from this software without specific
22af8ce959Schris  *    prior written permission.
23af8ce959Schris  *
24af8ce959Schris  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
25af8ce959Schris  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
26af8ce959Schris  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
27af8ce959Schris  * IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
28af8ce959Schris  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
29af8ce959Schris  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
30af8ce959Schris  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31af8ce959Schris  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32af8ce959Schris  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33af8ce959Schris  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34af8ce959Schris  * SUCH DAMAGE.
35af8ce959Schris  */
36af8ce959Schris 
379fd86b68Schris #include <sys/cdefs.h>
38*71034770Sskrll __KERNEL_RCSID(0, "$NetBSD: footbridge_pci.c,v 1.34 2022/09/27 06:36:41 skrll Exp $");
399fd86b68Schris 
40af8ce959Schris #include <sys/param.h>
41af8ce959Schris #include <sys/systm.h>
42af8ce959Schris #include <sys/conf.h>
43af8ce959Schris #include <sys/device.h>
44af8ce959Schris 
45af8ce959Schris #define _ARM32_BUS_DMA_PRIVATE
46fe6db6ffSdyoung #include <sys/bus.h>
47792b7ebdSmatt #include <machine/intr.h>
48af8ce959Schris 
49af8ce959Schris #include <dev/pci/pcireg.h>
50af8ce959Schris #include <dev/pci/pcivar.h>
51af8ce959Schris 
52af8ce959Schris #include <arm/footbridge/dc21285reg.h>
53af8ce959Schris #include <arm/footbridge/dc21285mem.h>
54af8ce959Schris 
55af8ce959Schris #include "isa.h"
56af8ce959Schris #if NISA > 0
57af8ce959Schris #include <dev/isa/isavar.h>
58af8ce959Schris #endif
59af8ce959Schris 
605f819ca3Schs void		footbridge_pci_attach_hook(device_t, device_t,
615f819ca3Schs 		    struct pcibus_attach_args *);
6202cdf4d2Sdsl int		footbridge_pci_bus_maxdevs(void *, int);
6302cdf4d2Sdsl pcitag_t	footbridge_pci_make_tag(void *, int, int, int);
6402cdf4d2Sdsl void		footbridge_pci_decompose_tag(void *, pcitag_t, int *,
6502cdf4d2Sdsl 		    int *, int *);
6602cdf4d2Sdsl pcireg_t	footbridge_pci_conf_read(void *, pcitag_t, int);
6702cdf4d2Sdsl void		footbridge_pci_conf_write(void *, pcitag_t, int,
6802cdf4d2Sdsl 		    pcireg_t);
69dd3ec23bSdyoung int		footbridge_pci_intr_map(const struct pci_attach_args *,
7002cdf4d2Sdsl 		    pci_intr_handle_t *);
7110870ca5Schristos const char	*footbridge_pci_intr_string(void *, pci_intr_handle_t,
7210870ca5Schristos 		    char *, size_t);
7302cdf4d2Sdsl void		*footbridge_pci_intr_establish(void *, pci_intr_handle_t,
7471cf1f8fSrjs 		    int, int (*)(void *), void *, const char *);
7502cdf4d2Sdsl void		footbridge_pci_intr_disestablish(void *, void *);
7602cdf4d2Sdsl const struct evcnt *footbridge_pci_intr_evcnt(void *, pci_intr_handle_t);
77af8ce959Schris 
78af8ce959Schris struct arm32_pci_chipset footbridge_pci_chipset = {
79fa3621e3Smatt #ifdef netwinder
80c56e094eSskrll 	.pc_attach_hook = netwinder_pci_attach_hook,
81af8ce959Schris #else
82c56e094eSskrll 	.pc_attach_hook = footbridge_pci_attach_hook,
83af8ce959Schris #endif
84c56e094eSskrll 	.pc_bus_maxdevs = footbridge_pci_bus_maxdevs,
85c56e094eSskrll 	.pc_make_tag = footbridge_pci_make_tag,
86c56e094eSskrll 	.pc_decompose_tag = footbridge_pci_decompose_tag,
87c56e094eSskrll 	.pc_conf_read = footbridge_pci_conf_read,
88c56e094eSskrll 	.pc_conf_write = footbridge_pci_conf_write,
89c56e094eSskrll 	.pc_intr_map = footbridge_pci_intr_map,
90c56e094eSskrll 	.pc_intr_string = footbridge_pci_intr_string,
91c56e094eSskrll 	.pc_intr_evcnt = footbridge_pci_intr_evcnt,
92c56e094eSskrll 	.pc_intr_establish = footbridge_pci_intr_establish,
93c56e094eSskrll 	.pc_intr_disestablish = footbridge_pci_intr_disestablish
94af8ce959Schris };
95af8ce959Schris 
9686e1b8a4Schris struct arm32_dma_range footbridge_dma_ranges[1];
9786e1b8a4Schris 
98af8ce959Schris /*
99af8ce959Schris  * PCI doesn't have any special needs; just use the generic versions
100af8ce959Schris  * of these functions.
101af8ce959Schris  */
102af8ce959Schris struct arm32_bus_dma_tag footbridge_pci_bus_dma_tag = {
103e26eebddSmatt 	._ranges = footbridge_dma_ranges,
104e26eebddSmatt 	._nranges = 1,
105e26eebddSmatt 	_BUS_DMAMAP_FUNCS,
106e26eebddSmatt 	_BUS_DMAMEM_FUNCS,
107e26eebddSmatt 	_BUS_DMATAG_FUNCS,
108af8ce959Schris };
109af8ce959Schris 
110af8ce959Schris /*
111af8ce959Schris  * Currently we only support 12 devices as we select directly in the
112af8ce959Schris  * type 0 config cycle
113af8ce959Schris  * (See conf_{read,write} for more detail
114af8ce959Schris  */
115af8ce959Schris #define MAX_PCI_DEVICES	21
116af8ce959Schris 
117af8ce959Schris /*static int
118af8ce959Schris pci_intr(void *arg)
119af8ce959Schris {
120af8ce959Schris 	printf("pci int %x\n", (int)arg);
121af8ce959Schris 	return(0);
122af8ce959Schris }*/
123af8ce959Schris 
124af8ce959Schris 
125af8ce959Schris void
footbridge_pci_attach_hook(device_t parent,device_t self,struct pcibus_attach_args * pba)1265f819ca3Schs footbridge_pci_attach_hook(device_t parent, device_t self, struct pcibus_attach_args *pba)
127af8ce959Schris {
128af8ce959Schris #ifdef PCI_DEBUG
129af8ce959Schris 	printf("footbridge_pci_attach_hook()\n");
130af8ce959Schris #endif
131af8ce959Schris 
132af8ce959Schris /*	intr_claim(18, IPL_NONE, "pci int 0", pci_intr, (void *)0x10000);
133af8ce959Schris 	intr_claim(8, IPL_NONE, "pci int 1", pci_intr, (void *)0x10001);
134af8ce959Schris 	intr_claim(9, IPL_NONE, "pci int 2", pci_intr, (void *)0x10002);
135af8ce959Schris 	intr_claim(11, IPL_NONE, "pci int 3", pci_intr, (void *)0x10003);*/
136af8ce959Schris }
137af8ce959Schris 
138af8ce959Schris int
footbridge_pci_bus_maxdevs(void * pcv,int busno)139454af1c0Sdsl footbridge_pci_bus_maxdevs(void *pcv, int busno)
140af8ce959Schris {
141af8ce959Schris #ifdef PCI_DEBUG
142af8ce959Schris 	printf("footbridge_pci_bus_maxdevs(pcv=%p, busno=%d)\n", pcv, busno);
143af8ce959Schris #endif
144af8ce959Schris 	return(MAX_PCI_DEVICES);
145af8ce959Schris }
146af8ce959Schris 
147af8ce959Schris pcitag_t
footbridge_pci_make_tag(void * pcv,int bus,int device,int function)14882357f6dSdsl footbridge_pci_make_tag(void *pcv, int bus, int device, int function)
149af8ce959Schris {
150af8ce959Schris #ifdef PCI_DEBUG
151af8ce959Schris 	printf("footbridge_pci_make_tag(pcv=%p, bus=%d, device=%d, function=%d)\n",
152af8ce959Schris 	    pcv, bus, device, function);
153af8ce959Schris #endif
154af8ce959Schris 	return ((bus << 16) | (device << 11) | (function << 8));
155af8ce959Schris }
156af8ce959Schris 
157af8ce959Schris void
footbridge_pci_decompose_tag(void * pcv,pcitag_t tag,int * busp,int * devicep,int * functionp)15882357f6dSdsl footbridge_pci_decompose_tag(void *pcv, pcitag_t tag, int *busp, int *devicep, int *functionp)
159af8ce959Schris {
160af8ce959Schris #ifdef PCI_DEBUG
161b07025aeSchris 	printf("footbridge_pci_decompose_tag(pcv=%p, tag=0x%08x, bp=%p, dp=%p, fp=%p)\n",
162b07025aeSchris 	    pcv, (uint32_t)tag, busp, devicep, functionp);
163af8ce959Schris #endif
164af8ce959Schris 
165af8ce959Schris 	if (busp != NULL)
166af8ce959Schris 		*busp = (tag >> 16) & 0xff;
167af8ce959Schris 	if (devicep != NULL)
168af8ce959Schris 		*devicep = (tag >> 11) & 0x1f;
169af8ce959Schris 	if (functionp != NULL)
170af8ce959Schris 		*functionp = (tag >> 8) & 0x7;
171af8ce959Schris }
172af8ce959Schris 
173af8ce959Schris pcireg_t
footbridge_pci_conf_read(void * pcv,pcitag_t tag,int reg)174454af1c0Sdsl footbridge_pci_conf_read(void *pcv, pcitag_t tag, int reg)
175af8ce959Schris {
176af8ce959Schris 	int bus, device, function;
177af8ce959Schris 	u_int address;
178af8ce959Schris 	pcireg_t data;
179af8ce959Schris 
18099886603Smsaitoh 	if ((unsigned int)reg >= PCI_CONF_SIZE)
18199886603Smsaitoh 		return ((pcireg_t) -1);
18299886603Smsaitoh 
183af8ce959Schris 	footbridge_pci_decompose_tag(pcv, tag, &bus, &device, &function);
184af8ce959Schris 	if (bus == 0)
185af8ce959Schris 		/* Limited to 12 devices or we exceed type 0 config space */
186af8ce959Schris 		address = DC21285_PCI_TYPE_0_CONFIG_VBASE | (3 << 22) | (device << 11);
187af8ce959Schris 	else
188af8ce959Schris 		address = DC21285_PCI_TYPE_1_CONFIG_VBASE | (device << 11) |
189af8ce959Schris 		    (bus << 16);
190af8ce959Schris 
191af8ce959Schris 	address |= (function << 8) | reg;
192af8ce959Schris 
193af8ce959Schris 	data = *((unsigned int *)address);
194af8ce959Schris #ifdef PCI_DEBUG
195af8ce959Schris 	printf("footbridge_pci_conf_read(pcv=%p tag=0x%08x reg=0x%02x)=0x%08x\n",
196b07025aeSchris 	    pcv, (uint32_t)tag, reg, data);
197af8ce959Schris #endif
198af8ce959Schris 	return(data);
199af8ce959Schris }
200af8ce959Schris 
201af8ce959Schris void
footbridge_pci_conf_write(void * pcv,pcitag_t tag,int reg,pcireg_t data)202454af1c0Sdsl footbridge_pci_conf_write(void *pcv, pcitag_t tag, int reg, pcireg_t data)
203af8ce959Schris {
204af8ce959Schris 	int bus, device, function;
205af8ce959Schris 	u_int address;
206af8ce959Schris 
20799886603Smsaitoh 	if ((unsigned int)reg >= PCI_CONF_SIZE)
20899886603Smsaitoh 		return;
20999886603Smsaitoh 
210af8ce959Schris 	footbridge_pci_decompose_tag(pcv, tag, &bus, &device, &function);
211af8ce959Schris 	if (bus == 0)
212af8ce959Schris 		address = DC21285_PCI_TYPE_0_CONFIG_VBASE | (3 << 22) | (device << 11);
213af8ce959Schris 	else
214af8ce959Schris 		address = DC21285_PCI_TYPE_1_CONFIG_VBASE | (device << 11) |
215af8ce959Schris 		    (bus << 16);
216af8ce959Schris 
217af8ce959Schris 	address |= (function << 8) | reg;
218af8ce959Schris 
219af8ce959Schris #ifdef PCI_DEBUG
220af8ce959Schris 	printf("footbridge_pci_conf_write(pcv=%p tag=0x%08x reg=0x%02x, 0x%08x)\n",
221b07025aeSchris 	    pcv, (uint32_t)tag, reg, data);
222af8ce959Schris #endif
223af8ce959Schris 
224af8ce959Schris 	*((unsigned int *)address) = data;
225af8ce959Schris }
226af8ce959Schris 
227af8ce959Schris int
footbridge_pci_intr_map(const struct pci_attach_args * pa,pci_intr_handle_t * ihp)228dd3ec23bSdyoung footbridge_pci_intr_map(const struct pci_attach_args *pa,
229dd3ec23bSdyoung     pci_intr_handle_t *ihp)
230af8ce959Schris {
231af8ce959Schris 	int pin = pa->pa_intrpin, line = pa->pa_intrline;
232af8ce959Schris 	int intr = -1;
233af8ce959Schris 
234af8ce959Schris #ifdef PCI_DEBUG
235af8ce959Schris 	void *pcv = pa->pa_pc;
236af8ce959Schris 	pcitag_t intrtag = pa->pa_intrtag;
237af8ce959Schris 	int bus, device, function;
238af8ce959Schris 
239af8ce959Schris 	footbridge_pci_decompose_tag(pcv, intrtag, &bus, &device, &function);
240b07025aeSchris 	printf("footbridge_pci_intr_map: pcv=%p, tag=%08x pin=%d line=%d dev=%d\n",
241b07025aeSchris 	    pcv, (uint32_t)intrtag, pin, line, device);
242af8ce959Schris #endif
243af8ce959Schris 
244af8ce959Schris 	/*
245af8ce959Schris 	 * Only the line is used to map the interrupt.
246af8ce959Schris 	 * The firmware is expected to setup up the interrupt
247af8ce959Schris 	 * line as seen from the CPU
248af8ce959Schris 	 * This means the firmware deals with the interrupt rotation
249af8ce959Schris 	 * between slots etc.
250af8ce959Schris 	 *
251af8ce959Schris 	 * Perhaps the firmware should also to the final mapping
252af8ce959Schris 	 * to a 21285 interrupt bit so the code below would be
253af8ce959Schris 	 * completely MI.
254af8ce959Schris 	 */
255af8ce959Schris 
256af8ce959Schris 	switch (line) {
257af8ce959Schris 	case PCI_INTERRUPT_PIN_NONE:
258af8ce959Schris 	case 0xff:
259af8ce959Schris 		/* No IRQ */
260af8ce959Schris 		printf("pci_intr_map: no mapping for pin %c\n", '@' + pin);
261af8ce959Schris 		*ihp = -1;
262af8ce959Schris 		return(1);
263af8ce959Schris 		break;
2647383fda9Schris #ifdef cats
265fcddf71fSwiz 	/* This is machine dependent and needs to be moved */
266af8ce959Schris 	case PCI_INTERRUPT_PIN_A:
267af8ce959Schris 		intr = IRQ_PCI;
268af8ce959Schris 		break;
269af8ce959Schris 	case PCI_INTERRUPT_PIN_B:
270af8ce959Schris 		intr = IRQ_IN_L0;
271af8ce959Schris 		break;
272af8ce959Schris 	case PCI_INTERRUPT_PIN_C:
273af8ce959Schris 		intr = IRQ_IN_L1;
274af8ce959Schris 		break;
275af8ce959Schris 	case PCI_INTERRUPT_PIN_D:
276af8ce959Schris 		intr = IRQ_IN_L3;
277af8ce959Schris 		break;
278af8ce959Schris #endif
279af8ce959Schris 	default:
280af8ce959Schris 		/*
281af8ce959Schris 		 * Experimental firmware feature ...
282af8ce959Schris 		 *
283af8ce959Schris 		 * If the interrupt line is in the range 0x80 to 0x8F
284af8ce959Schris 		 * then the lower 4 bits indicate the ISA interrupt
285af8ce959Schris 		 * bit that should be used.
286af8ce959Schris 		 * If the interrupt line is in the range 0x40 to 0x5F
287af8ce959Schris 		 * then the lower 5 bits indicate the actual DC21285
288af8ce959Schris 		 * interrupt bit that should be used.
289af8ce959Schris 		 */
290af8ce959Schris 
291af8ce959Schris 		if (line >= 0x40 && line <= 0x5f)
292af8ce959Schris 			intr = line & 0x1f;
293af8ce959Schris 		else if (line >= 0x80 && line <= 0x8f)
294af8ce959Schris 			intr = line;
295af8ce959Schris 		else {
296af8ce959Schris 	                printf("footbridge_pci_intr_map: out of range interrupt"
297af8ce959Schris 			       "pin %d line %d (%#x)\n", pin, line, line);
298af8ce959Schris 			*ihp = -1;
299af8ce959Schris 			return(1);
300af8ce959Schris 		}
301af8ce959Schris 		break;
302af8ce959Schris 	}
303af8ce959Schris 
304af8ce959Schris #ifdef PCI_DEBUG
305af8ce959Schris 	printf("pin %d, line %d mapped to int %d\n", pin, line, intr);
306af8ce959Schris #endif
307af8ce959Schris 
308af8ce959Schris 	*ihp = intr;
309af8ce959Schris 	return(0);
310af8ce959Schris }
311af8ce959Schris 
312af8ce959Schris const char *
footbridge_pci_intr_string(void * pcv,pci_intr_handle_t ih,char * buf,size_t len)313b8930f84Schristos footbridge_pci_intr_string(void *pcv, pci_intr_handle_t ih, char *buf, size_t len)
314af8ce959Schris {
315af8ce959Schris #ifdef PCI_DEBUG
316fe83f0c8Sjmcneill 	printf("footbridge_pci_intr_string(pcv=%p, ih=0x%" PRIx64 ")\n", pcv, ih);
317af8ce959Schris #endif
318af8ce959Schris 	if (ih == 0)
319fe83f0c8Sjmcneill 		panic("footbridge_pci_intr_string: bogus handle 0x%" PRIx64, ih);
320af8ce959Schris 
321af8ce959Schris #if NISA > 0
322af8ce959Schris 	if (ih >= 0x80 && ih <= 0x8f) {
323fe83f0c8Sjmcneill 		snprintf(buf, len, "isairq %" PRIu64, (ih & 0x0f));
324b8930f84Schristos 		return buf;
325af8ce959Schris 	}
326af8ce959Schris #endif
327fe83f0c8Sjmcneill 	snprintf(buf, len, "irq %" PRIu64, ih);
328b8930f84Schristos 	return buf;
329af8ce959Schris }
330af8ce959Schris 
331af8ce959Schris void *
footbridge_pci_intr_establish(void * pcv,pci_intr_handle_t ih,int level,int (* func)(void *),void * arg,const char * xname)33241ba3596Smatt footbridge_pci_intr_establish(
33341ba3596Smatt 	void *pcv,
33441ba3596Smatt 	pci_intr_handle_t ih,
33541ba3596Smatt 	int level,
33641ba3596Smatt 	int (*func)(void *),
33771cf1f8fSrjs 	void *arg, const char *xname)
338af8ce959Schris {
339af8ce959Schris 	void *intr;
340b8930f84Schristos 	char buf[PCI_INTRSTR_LEN];
341b8930f84Schristos 	const char *intrstr;
342af8ce959Schris 
343af8ce959Schris #ifdef PCI_DEBUG
34471cf1f8fSrjs 	printf("footbridge_pci_intr_establish(pcv=%p, ih=0x%" PRIx64 ", level=%d, func=%p, arg=%p, xname=%s)\n",
34571cf1f8fSrjs 	    pcv, ih, level, func, arg, xname);
346af8ce959Schris #endif
347af8ce959Schris 
348af8ce959Schris 	/* Copy the interrupt string to a private buffer */
349b8930f84Schristos 	intrstr = footbridge_pci_intr_string(pcv, ih, buf, sizeof(buf));
350af8ce959Schris #if NISA > 0
351af8ce959Schris 	/*
352af8ce959Schris 	 * XXX the IDE driver will attach the interrupts in compat mode and
353af8ce959Schris 	 * thus we need to fail this here.
354af8ce959Schris 	 * This assumes that the interrupts are 14 and 15 which they are for
355af8ce959Schris 	 * IDE compat mode.
356af8ce959Schris 	 * Really the firmware should make this clear in the interrupt reg.
357af8ce959Schris 	 */
358af8ce959Schris 	if (ih >= 0x80 && ih <= 0x8d) {
359af8ce959Schris 		intr = isa_intr_establish(NULL, (ih & 0x0f), IST_EDGE,
360af8ce959Schris 		    level, func, arg);
361af8ce959Schris 	} else
362af8ce959Schris #endif
363b8930f84Schristos 	intr = footbridge_intr_claim(ih, level, intrstr, func, arg);
364af8ce959Schris 
365af8ce959Schris 	return(intr);
366af8ce959Schris }
367af8ce959Schris 
368af8ce959Schris void
footbridge_pci_intr_disestablish(void * pcv,void * cookie)369454af1c0Sdsl footbridge_pci_intr_disestablish(void *pcv, void *cookie)
370af8ce959Schris {
371af8ce959Schris #ifdef PCI_DEBUG
3720ead1e8cSrin 	printf("footbridge_pci_intr_disestablish(pcv=%p, cookie=%p)\n",
373af8ce959Schris 	    pcv, cookie);
374af8ce959Schris #endif
375af8ce959Schris 	/* XXXX Need to free the string */
37661578bc3Schris 	footbridge_intr_disestablish(cookie);
377af8ce959Schris }
378