xref: /netbsd-src/sys/arch/evbppc/dht/machdep.c (revision c1d86c14663dbd9e4adc65740b011d226b6c7f9c)
1 /*	$NetBSD: machdep.c,v 1.3 2021/09/19 11:37:00 andvar Exp $	*/
2 
3 /*
4  * Taken from src/sys/arch/evbppc/walnut/machdep.c:
5  *	NetBSD: machdep.c,v 1.67 2021/03/30 05:08:16 rin Exp
6  */
7 
8 /*
9  * Copyright 2001, 2002 Wasabi Systems, Inc.
10  * All rights reserved.
11  *
12  * Written by Eduardo Horvath and Simon Burge for Wasabi Systems, Inc.
13  *
14  * Redistribution and use in source and binary forms, with or without
15  * modification, are permitted provided that the following conditions
16  * are met:
17  * 1. Redistributions of source code must retain the above copyright
18  *    notice, this list of conditions and the following disclaimer.
19  * 2. Redistributions in binary form must reproduce the above copyright
20  *    notice, this list of conditions and the following disclaimer in the
21  *    documentation and/or other materials provided with the distribution.
22  * 3. All advertising materials mentioning features or use of this software
23  *    must display the following acknowledgement:
24  *      This product includes software developed for the NetBSD Project by
25  *      Wasabi Systems, Inc.
26  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
27  *    or promote products derived from this software without specific prior
28  *    written permission.
29  *
30  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
31  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
32  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
33  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
34  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
35  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
36  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
37  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
38  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
39  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
40  * POSSIBILITY OF SUCH DAMAGE.
41  */
42 
43 /*
44  * Copyright (C) 1995, 1996 Wolfgang Solfrank.
45  * Copyright (C) 1995, 1996 TooLs GmbH.
46  * All rights reserved.
47  *
48  * Redistribution and use in source and binary forms, with or without
49  * modification, are permitted provided that the following conditions
50  * are met:
51  * 1. Redistributions of source code must retain the above copyright
52  *    notice, this list of conditions and the following disclaimer.
53  * 2. Redistributions in binary form must reproduce the above copyright
54  *    notice, this list of conditions and the following disclaimer in the
55  *    documentation and/or other materials provided with the distribution.
56  * 3. All advertising materials mentioning features or use of this software
57  *    must display the following acknowledgement:
58  *	This product includes software developed by TooLs GmbH.
59  * 4. The name of TooLs GmbH may not be used to endorse or promote products
60  *    derived from this software without specific prior written permission.
61  *
62  * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
63  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
64  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
65  * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
66  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
67  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
68  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
69  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
70  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
71  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
72  */
73 
74 #include <sys/cdefs.h>
75 __KERNEL_RCSID(0, "$NetBSD: machdep.c,v 1.3 2021/09/19 11:37:00 andvar Exp $");
76 
77 #include "opt_ddb.h"
78 #include "opt_pci.h"
79 
80 #include <sys/param.h>
81 #include <sys/bus.h>
82 #include <sys/device.h>
83 #include <sys/kernel.h>
84 #include <sys/module.h>
85 #include <sys/reboot.h>
86 #include <sys/systm.h>
87 
88 #include <machine/dht.h>
89 
90 #include <powerpc/spr.h>
91 #include <powerpc/ibm4xx/spr.h>
92 
93 #include <powerpc/ibm4xx/cpu.h>
94 #include <powerpc/ibm4xx/dcr4xx.h>
95 #include <powerpc/ibm4xx/ibm405gp.h>
96 #include <powerpc/ibm4xx/openbios.h>
97 #include <powerpc/ibm4xx/tlb.h>
98 
99 #include "com.h"
100 #if NCOM > 0
101 #include <sys/termios.h>
102 #include <dev/ic/comreg.h>
103 #include <dev/ic/comvar.h>
104 #include <powerpc/ibm4xx/dev/comopbvar.h>
105 #ifndef CONADDR
106 #define	CONADDR		IBM405GP_UART0_BASE
107 #endif
108 #ifndef CONSPEED
109 #define	CONSPEED	B115200
110 #endif
111 #ifndef CONMODE
112 #define	CONMODE		TTYDEF_CFLAG
113 #endif
114 #endif /* NCOM > 0 */
115 
116 #include "emac.h"
117 #if NEMAC > 0
118 #include <net/if_ether.h>
119 #endif
120 
121 #include "pci.h"
122 #if NPCI > 0
123 #ifndef PCI_NETBSD_CONFIGURE
124 #error options PCI_NETBSD_CONFIGURE is mandatory.
125 #endif
126 #include <powerpc/ibm4xx/pci_machdep.h>
127 #include <dev/pci/pciconf.h>
128 #include <dev/pci/pcivar.h>
129 #endif
130 
131 #define TLB_PG_SIZE 	(16 * 1024 * 1024)
132 
133 static u_int memsize;
134 
135 void initppc(vaddr_t, vaddr_t, char *, void *);
136 
137 /*
138  * Get memory size from SDRAM bank register.
139  */
140 static u_int
dht_memsize(void)141 dht_memsize(void)
142 {
143 	u_int total = 0;
144 	uint32_t val, addr;
145 
146 	for (addr = DCR_SDRAM0_B0CR; addr <= DCR_SDRAM0_B3CR; addr += 4) {
147 		mtdcr(DCR_SDRAM0_CFGADDR, addr);
148 		val = mfdcr(DCR_SDRAM0_CFGDATA);
149 		if (val & SDRAM0_BnCR_EN)
150 			total += SDRAM0_BnCR_SZ(val);
151 	}
152 	return total;
153 }
154 
155 void
consinit(void)156 consinit(void)
157 {
158 
159 #if NCOM > 0
160 	com_opb_cnattach(DHT_COM_FREQ, CONADDR, CONSPEED, CONMODE);
161 #endif
162 }
163 
164 void
initppc(vaddr_t startkernel,vaddr_t endkernel,char * args,void * info_block)165 initppc(vaddr_t startkernel, vaddr_t endkernel, char *args, void *info_block)
166 {
167 
168 	/* Disable all external interrupts */
169 	mtdcr(DCR_UIC0_BASE + DCR_UIC_ER, 0);
170 
171 	memsize = dht_memsize();
172 
173 	/* Linear map kernel memory */
174 	for (vaddr_t va = 0; va < endkernel; va += TLB_PG_SIZE)
175 		ppc4xx_tlb_reserve(va, va, TLB_PG_SIZE, TLB_EX);
176 
177 	/* Map console after physmem (see pmap_tlbmiss()) */
178 	ppc4xx_tlb_reserve(IBM405GP_UART0_BASE, roundup(memsize, TLB_PG_SIZE),
179 	    TLB_PG_SIZE, TLB_I | TLB_G);
180 
181 	/* Disable all timers */
182 	mtspr(SPR_TCR, 0);
183 
184 	ibm40x_memsize_init(memsize, startkernel);
185 	ibm4xx_init(startkernel, endkernel, pic_ext_intr);
186 
187 #ifdef DDB
188 	if (boothowto & RB_KDB)
189 		Debugger();
190 #endif
191 }
192 
193 void
cpu_startup(void)194 cpu_startup(void)
195 {
196 	prop_number_t pn;
197 
198 	ibm4xx_cpu_startup("DHT Walnut 405GP Evaluation Board");
199 
200 	/*
201 	 * Set up the board properties database.
202 	 */
203 	board_info_init();
204 
205 	pn = prop_number_create_integer(DHT_CPU_FREQ);
206 	KASSERT(pn != NULL);
207 	if (prop_dictionary_set(board_properties, "processor-frequency", pn) ==
208 	    false)
209 		panic("setting processor-frequency");
210 	prop_object_release(pn);
211 
212 	pn = prop_number_create_integer(memsize);
213 	KASSERT(pn != NULL);
214 	if (prop_dictionary_set(board_properties, "mem-size", pn) == false)
215 		panic("setting mem-size");
216 	prop_object_release(pn);
217 
218 #if NEMAC > 0
219 	/*
220 	 * XXX
221 	 * Unfortunately, no MAC address is assigned to this board.
222 	 * Set fake address de:ad:be:ef:00:00.
223 	 *
224 	 * XXX
225 	 * This should be same with what U-Boot/PPC-Boot set.
226 	 */
227 	static uint8_t enaddr[ETHER_ADDR_LEN];
228 	enaddr[0] = 0xde; enaddr[1] = 0xad; enaddr[2] = 0xbe;
229 	enaddr[3] = 0xef; enaddr[4] = 0x00; enaddr[5] = 0x00;
230 	prop_data_t pd = prop_data_create_data_nocopy(enaddr, ETHER_ADDR_LEN);
231 	KASSERT(pd != NULL);
232 	if (prop_dictionary_set(board_properties, "emac0-mac-addr", pd) ==
233 	    false)
234 		panic("setting emac0-mac-addr");
235 	prop_object_release(pd);
236 #endif
237 
238 	/*
239 	 * Now that we have VM, malloc()s are OK in bus_space.
240 	 */
241 	bus_space_mallocok();
242 
243 	/*
244 	 * No fake mapiodev.
245 	 */
246 	fake_mapiodev = 0;
247 }
248 
249 #if NPCI > 0
250 int
ibm4xx_pci_bus_maxdevs(void * v,int busno)251 ibm4xx_pci_bus_maxdevs(void *v, int busno)
252 {
253 
254 	return 32;
255 }
256 
257 /*
258  * This is only possible mapping known to work b/w PCI devices and IRQ pins.
259  */
260 #define	DEV_TO_IRQ(dev)		(27 + (dev))
261 #define	PARENT_DEV(swiz, dev)	((swiz) - (dev))
262 
263 int
ibm4xx_pci_intr_map(const struct pci_attach_args * pa,pci_intr_handle_t * ihp)264 ibm4xx_pci_intr_map(const struct pci_attach_args *pa, pci_intr_handle_t *ihp)
265 {
266 	int pin = pa->pa_intrpin, bus = pa->pa_bus, dev = pa->pa_device;
267 
268 	if (pin <= 0 || pin > 4)
269 		goto bad;
270 
271 	if (bus != 0) {
272 		/*
273 		 * XXX
274 		 * We only support ppb(4) directly attached to pci0.
275 		 */
276 		dev = PARENT_DEV(pa->pa_intrswiz, dev);
277 		goto out;
278 	}
279 
280 	switch (dev) {
281 	case 2:
282 	case 3:
283 	case 4:
284 out:
285 		*ihp = DEV_TO_IRQ(dev);
286 		return 0;
287 	default:
288 bad:
289 		printf("%s: invalid request: bus %d dev %d pin %d\n",
290 		    __func__, bus, dev, pin);
291 		*ihp = -1;
292 		return 1;
293 	}
294 }
295 
296 void
ibm4xx_pci_conf_interrupt(void * v,int bus,int dev,int pin,int swiz,int * iline)297 ibm4xx_pci_conf_interrupt(void *v, int bus, int dev, int pin, int swiz,
298     int *iline)
299 {
300 
301 	if (bus != 0) {
302 		/*
303 		 * XXX
304 		 * See comment above.
305 		 */
306 		dev = PARENT_DEV(swiz, dev);
307 		goto out;
308 	}
309 
310 	switch (dev) {
311 	case 2:
312 	case 3:
313 	case 4:
314 out:
315 		*iline = DEV_TO_IRQ(dev);
316 		break;
317 	default:
318 		printf("%s: invalid request: bus %d dev %d pin %d swiz %d\n",
319 		    __func__, bus, dev, pin, swiz);
320 		*iline = 0;
321 		break;
322 	}
323 }
324 #endif /* NPCI > 0 */
325