1 /*        $NetBSD: obs266_machdep.c,v 1.27 2021/08/03 09:25:44 rin Exp $        */
2 /*        Original: md_machdep.c,v 1.3 2005/01/24 18:47:37 shige Exp $          */
3 
4 /*
5  * Copyright 2001, 2002 Wasabi Systems, Inc.
6  * All rights reserved.
7  *
8  * Written by Eduardo Horvath and Simon Burge for Wasabi Systems, Inc.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *      This product includes software developed for the NetBSD Project by
21  *      Wasabi Systems, Inc.
22  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
23  *    or promote products derived from this software without specific prior
24  *    written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36  * POSSIBILITY OF SUCH DAMAGE.
37  */
38 
39 /*
40  * Copyright (C) 1995, 1996 Wolfgang Solfrank.
41  * Copyright (C) 1995, 1996 TooLs GmbH.
42  * All rights reserved.
43  *
44  * Redistribution and use in source and binary forms, with or without
45  * modification, are permitted provided that the following conditions
46  * are met:
47  * 1. Redistributions of source code must retain the above copyright
48  *    notice, this list of conditions and the following disclaimer.
49  * 2. Redistributions in binary form must reproduce the above copyright
50  *    notice, this list of conditions and the following disclaimer in the
51  *    documentation and/or other materials provided with the distribution.
52  * 3. All advertising materials mentioning features or use of this software
53  *    must display the following acknowledgement:
54  *        This product includes software developed by TooLs GmbH.
55  * 4. The name of TooLs GmbH may not be used to endorse or promote products
56  *    derived from this software without specific prior written permission.
57  *
58  * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
59  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
60  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
61  * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
62  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
63  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
64  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
65  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
66  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
67  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
68  */
69 
70 #include <sys/cdefs.h>
71 __KERNEL_RCSID(0, "$NetBSD: obs266_machdep.c,v 1.27 2021/08/03 09:25:44 rin Exp $");
72 
73 #include "opt_ddb.h"
74 
75 #include <sys/param.h>
76 #include <sys/bus.h>
77 #include <sys/device.h>
78 #include <sys/kernel.h>
79 #include <sys/module.h>
80 #include <sys/reboot.h>
81 #include <sys/systm.h>
82 
83 #include <machine/obs266.h>
84 
85 #include <powerpc/spr.h>
86 #include <powerpc/ibm4xx/spr.h>
87 
88 #include <powerpc/ibm4xx/cpu.h>
89 #include <powerpc/ibm4xx/dcr4xx.h>
90 #include <powerpc/ibm4xx/ibm405gp.h>
91 #include <powerpc/ibm4xx/openbios.h>
92 #include <powerpc/ibm4xx/tlb.h>
93 
94 #include <powerpc/ibm4xx/pci_machdep.h>
95 #include <dev/pci/pciconf.h>
96 #include <dev/pci/pcivar.h>
97 
98 #include "com.h"
99 #if (NCOM > 0)
100 #include <sys/termios.h>
101 #include <powerpc/ibm4xx/dev/comopbvar.h>
102 #include <dev/ic/comreg.h>
103 
104 #ifndef CONADDR
105 #define CONADDR               IBM405GP_UART0_BASE
106 #endif
107 #ifndef CONSPEED
108 #define CONSPEED    B9600
109 #endif
110 #ifndef CONMODE
111                               /* 8N1 */
112 #define CONMODE               ((TTYDEF_CFLAG & ~(CSIZE | CSTOPB | PARENB)) | CS8)
113 #endif
114 #endif    /* NCOM */
115 
116 #define   TLB_PG_SIZE         (16*1024*1024)
117 
118 void initppc(vaddr_t, vaddr_t, char *, void *);
119 
120 void
initppc(vaddr_t startkernel,vaddr_t endkernel,char * args,void * info_block)121 initppc(vaddr_t startkernel, vaddr_t endkernel, char *args, void *info_block)
122 {
123           vaddr_t va;
124           u_int memsize;
125 
126           /* Setup board from OpenBIOS */
127           openbios_board_init(info_block);
128           memsize = openbios_board_memsize_get();
129 
130           /* Linear map kernel memory */
131           for (va = 0; va < endkernel; va += TLB_PG_SIZE)
132                     ppc4xx_tlb_reserve(va, va, TLB_PG_SIZE, TLB_EX);
133 
134           /* Map console after RAM (see pmap_tlbmiss()) */
135           ppc4xx_tlb_reserve(CONADDR, roundup(memsize, TLB_PG_SIZE), TLB_PG_SIZE,
136               TLB_I | TLB_G);
137 
138           /* Initialize IBM405GPr CPU */
139           ibm40x_memsize_init(memsize, startkernel);
140           ibm4xx_init(startkernel, endkernel, pic_ext_intr);
141 
142 #ifdef DEBUG
143           openbios_board_print();
144 #endif
145 
146 #ifdef DDB
147           if (boothowto & RB_KDB)
148                     Debugger();
149 #endif
150 }
151 
152 void
consinit(void)153 consinit(void)
154 {
155 
156 #if (NCOM > 0)
157           com_opb_cnattach(OBS266_COM_FREQ, CONADDR, CONSPEED, CONMODE);
158 #endif
159 }
160 
161 /*
162  * Machine dependent startup code.
163  */
164 void
cpu_startup(void)165 cpu_startup(void)
166 {
167 
168           /*
169            * cpu common startup
170            */
171           ibm4xx_cpu_startup("OpenBlockS266 IBM PowerPC 405GPr Board");
172 
173           /*
174            * Set up the board properties database.
175            */
176           openbios_board_info_set();
177 
178           /*
179            * Now that we have VM, malloc()s are OK in bus_space.
180            */
181           bus_space_mallocok();
182 
183           /*
184            * no fake mapiodev
185            */
186           fake_mapiodev = 0;
187 }
188 
189 int
ibm4xx_pci_bus_maxdevs(void * v,int busno)190 ibm4xx_pci_bus_maxdevs(void *v, int busno)
191 {
192 
193           /*
194            * Bus number is irrelevant.  Configuration Mechanism 1 is in
195            * use, can have devices 0-32 (i.e. the `normal' range).
196            */
197           return 31;
198 }
199 
200 int
ibm4xx_pci_intr_map(const struct pci_attach_args * pa,pci_intr_handle_t * ihp)201 ibm4xx_pci_intr_map(const struct pci_attach_args *pa, pci_intr_handle_t *ihp)
202 {
203           /*
204            * We need to map the interrupt pin to the interrupt bit
205            * in the UIC associated with it.
206            *
207            * This platform has 4 PCI devices.
208            *
209            # External IRQ Mappings:
210            *  dev 1 (Ext IRQ3):         PCI Connector
211            *  dev 2 (Ext IRQ4):         PCI Connector
212            *  dev 3 (Ext IRQ5):         HPT IDE Controller
213            *  dev 4 (Ext IRQ6):         Davicom Ethernet
214            */
215           static const int irqmap[4/*device*/][4/*pin*/] = {
216                     {  3,  3,  3,  3 }, /* 1: PCI Connector 1 */
217                     {  4,  4,  4,  4 }, /* 2: PCI Connector 2 */
218                     {  5,  5, -1, -1 }, /* 3: HPT IDE Controller */
219                     {  6,  6, -1, -1 }, /* 4: Damicom Ethernet */
220           };
221 
222           int pin, dev, irq;
223 
224           pin = pa->pa_intrpin;
225           dev = pa->pa_device;
226         *ihp = -1;
227 
228           /* if interrupt pin not used... */
229           if (pin == 0)
230                     return 1;
231 
232           if (pin > 4) {
233                     printf("pci_intr_map: bad interrupt pin %d\n", pin);
234                     return 1;
235           }
236 
237           if ((dev < 1) || (dev > 4)) {
238                     printf("pci_intr_map: bad device %d\n", dev);
239                     return 1;
240           }
241 
242 
243           if ((irq = irqmap[dev - 1][pin - 1]) == -1) {
244                     printf("pci_intr_map: no IRQ routing for device %d pin %d\n",
245                               dev, pin);
246                     return 1;
247           }
248 
249           *ihp = irq + 25;
250           return 0;
251 }
252 
253 void
ibm4xx_pci_conf_interrupt(void * v,int bus,int dev,int pin,int swiz,int * iline)254 ibm4xx_pci_conf_interrupt(void *v, int bus, int dev, int pin, int swiz,
255     int *iline)
256 {
257 
258           static const int ilinemap[4/*device*/] = {
259                     28, 29, 30, 31
260           };
261 
262           if ((dev < 1) || (dev > 4)) {
263                     printf("%s: bad device %d\n", __func__, dev);
264                     *iline = 0;
265                     return;
266           }
267           *iline = ilinemap[dev - 1];
268 }
269