1/* $OpenBSD: dec_kn300.c,v 1.9 2017/04/30 13:04:49 mpi Exp $ */
2/* $NetBSD: dec_kn300.c,v 1.34 2007/03/04 15:18:10 yamt Exp $ */
3
4/*
5 * Copyright (c) 1998 by Matthew Jacob
6 * NASA AMES Research Center.
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 *    notice immediately at the beginning of the file, without modification,
14 *    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. The name of the author may not be used to endorse or promote products
19 *    derived from this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
25 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34#include <sys/param.h>
35#include <sys/systm.h>
36#include <sys/device.h>
37#include <sys/termios.h>
38#include <sys/conf.h>
39#include <dev/cons.h>
40
41#include <machine/rpb.h>
42#include <machine/autoconf.h>
43#include <machine/frame.h>
44#include <machine/cpuconf.h>
45
46#include <dev/ic/comreg.h>
47#include <dev/ic/comvar.h>
48
49#include <dev/isa/isareg.h>
50#include <dev/isa/isavar.h>
51#include <dev/ic/i8042reg.h>
52#include <dev/ic/pckbcvar.h>
53#include <dev/pci/pcireg.h>
54#include <dev/pci/pcivar.h>
55
56#include <alpha/mcbus/mcbusreg.h>
57#include <alpha/mcbus/mcbusvar.h>
58#include <alpha/pci/mcpciareg.h>
59#include <alpha/pci/mcpciavar.h>
60#include <alpha/pci/pci_kn300.h>
61
62#include <scsi/scsi_all.h>
63#include <scsi/scsiconf.h>
64
65#include "pckbd.h"
66
67#ifndef	CONSPEED
68#define	CONSPEED	TTYDEF_SPEED
69#endif
70static int comcnrate = CONSPEED;
71
72#ifdef DEBUG
73int bootdev_debug;
74#define DPRINTF(x)	do { if (bootdev_debug) printf x; } while (0)
75#else
76#define DPRINTF(x)	do { } while (0)
77#endif
78
79void dec_kn300_init (void);
80void dec_kn300_cons_init (void);
81static void dec_kn300_device_register (struct device *, void *);
82
83#define	ALPHASERVER_4100	"AlphaServer 4100"
84
85const struct alpha_variation_table dec_kn300_variations[] = {
86	{ 0, ALPHASERVER_4100 },
87	{ 0, NULL },
88};
89
90void
91dec_kn300_init()
92{
93	u_int64_t variation;
94	int cachesize;
95
96	platform.family = ALPHASERVER_4100;
97
98	if ((platform.model = alpha_dsr_sysname()) == NULL) {
99		variation = hwrpb->rpb_variation & SV_ST_MASK;
100		if ((platform.model = alpha_variation_name(variation,
101		    dec_kn300_variations)) == NULL)
102			platform.model = alpha_unknown_sysname();
103	}
104
105	platform.iobus = "mcbus";
106	platform.cons_init = dec_kn300_cons_init;
107	platform.device_register = dec_kn300_device_register;
108
109	/*
110	 * Determine B-cache size by looking at the primary (console)
111	 * MCPCIA's WHOAMI register.
112	 */
113	mcpcia_init();
114
115	if (mcbus_primary.mcbus_valid) {
116		switch (mcbus_primary.mcbus_bcache) {
117		default:
118		case CPU_BCache_0MB:
119			/* No B-cache or invalid; default to 1MB. */
120			/* FALLTHROUGH */
121
122		case CPU_BCache_1MB:
123			cachesize = (1 * 1024 * 1024);
124			break;
125
126		case CPU_BCache_2MB:
127			cachesize = (2 * 1024 * 1024);
128			break;
129
130		case CPU_BCache_4MB:
131			cachesize = (4 * 1024 * 1024);
132			break;
133		}
134	} else {
135		/* Default to 1MB. */
136		cachesize = (1 * 1024 * 1024);
137	}
138}
139
140void
141dec_kn300_cons_init()
142{
143	struct ctb *ctb;
144	struct mcpcia_config *ccp;
145	extern struct mcpcia_config mcpcia_console_configuration;
146
147	ccp = &mcpcia_console_configuration;
148	/* It's already initialized. */
149
150	ctb = (struct ctb *)(((char *)hwrpb) + hwrpb->rpb_ctb_off);
151
152	switch (ctb->ctb_term_type) {
153	case CTB_PRINTERPORT:
154		/* serial console ... */
155		/*
156		 * Delay to allow PROM putchars to complete.
157		 * FIFO depth * character time,
158		 * character time = (1000000 / (defaultrate / 10))
159		 */
160		DELAY(160000000 / comcnrate);
161		if (comcnattach(&ccp->cc_iot, 0x3f8, comcnrate,
162		    COM_FREQ,
163		    (TTYDEF_CFLAG & ~(CSIZE | PARENB)) | CS8)) {
164			panic("can't init serial console");
165
166		}
167		break;
168
169	case CTB_GRAPHICS:
170#if NPCKBD > 0
171		/* display console ... */
172		/* XXX */
173		(void) pckbc_cnattach(&ccp->cc_iot, IO_KBD, KBCMDP, 0);
174
175		if (CTB_TURBOSLOT_TYPE(ctb->ctb_turboslot) ==
176		    CTB_TURBOSLOT_TYPE_ISA)
177			isa_display_console(&ccp->cc_iot, &ccp->cc_memt);
178		else
179			pci_display_console(&ccp->cc_iot, &ccp->cc_memt,
180			    &ccp->cc_pc, CTB_TURBOSLOT_BUS(ctb->ctb_turboslot),
181			    CTB_TURBOSLOT_SLOT(ctb->ctb_turboslot), 0);
182#else
183		panic("not configured to use display && keyboard console");
184#endif
185		break;
186
187	default:
188		printf("ctb->ctb_term_type = 0x%lx\n",
189		    (unsigned long)ctb->ctb_term_type);
190		printf("ctb->ctb_turboslot = 0x%lx\n",
191		    (unsigned long)ctb->ctb_turboslot);
192
193		panic("consinit: unknown console type %lu",
194		    (unsigned long)ctb->ctb_term_type);
195	}
196}
197
198static void
199dec_kn300_device_register(dev, aux)
200	struct device *dev;
201	void *aux;
202{
203	static int found, initted, diskboot, netboot;
204	static struct device *primarydev, *pcidev, *ctrlrdev;
205	struct bootdev_data *b = bootdev_data;
206	struct device *parent = dev->dv_parent;
207	struct cfdata *cf = dev->dv_cfdata;
208	struct cfdriver *cd = cf->cf_driver;
209
210	if (found)
211		return;
212
213	if (!initted) {
214		diskboot = (strncasecmp(b->protocol, "SCSI", 4) == 0);
215		netboot = (strncasecmp(b->protocol, "BOOTP", 5) == 0) ||
216		    (strncasecmp(b->protocol, "MOP", 3) == 0);
217
218		DPRINTF(("proto:%s bus:%d slot:%d chan:%d", b->protocol,
219		    b->bus, b->slot, b->channel));
220		if (b->remote_address)
221			DPRINTF((" remote_addr:%s", b->remote_address));
222		DPRINTF((" un:%d bdt:%d", b->unit, b->boot_dev_type));
223		if (b->ctrl_dev_type)
224			DPRINTF((" cdt:%s\n", b->ctrl_dev_type));
225		else
226			DPRINTF(("\n"));
227		DPRINTF(("diskboot = %d, netboot = %d\n", diskboot, netboot));
228		initted = 1;
229	}
230
231	if (primarydev == NULL) {
232		if (strcmp(cd->cd_name, "mcpcia"))
233			return;
234		else {
235			struct mcbus_dev_attach_args *ma = aux;
236
237			if (b->bus != ma->ma_mid - 4)
238				return;
239			primarydev = dev;
240			DPRINTF(("\nprimarydev = %s\n", dev->dv_xname));
241			return;
242		}
243	}
244
245	if (pcidev == NULL) {
246		if (strcmp(cd->cd_name, "pci"))
247			return;
248		/*
249		 * Try to find primarydev anywhere in the ancestry.  This is
250		 * necessary if the PCI bus is hidden behind a bridge.
251		 */
252		else {
253			struct pcibus_attach_args *pba = aux;
254
255			if ((b->slot / 1000) != pba->pba_bus)
256				return;
257
258			pcidev = dev;
259			DPRINTF(("\npcidev = %s\n", dev->dv_xname));
260			return;
261		}
262	}
263
264	if (ctrlrdev == NULL) {
265		if (parent != pcidev)
266			return;
267		else {
268			struct pci_attach_args *pa = aux;
269			int slot;
270
271			slot = pa->pa_bus * 1000 + pa->pa_function * 100 +
272			    pa->pa_device;
273			if (b->slot != slot)
274				return;
275
276			if (netboot) {
277				booted_device = dev;
278				DPRINTF(("\nbooted_device = %s\n", dev->dv_xname));
279				found = 1;
280			} else {
281				ctrlrdev = dev;
282				DPRINTF(("\nctrlrdev = %s\n", dev->dv_xname));
283			}
284			return;
285		}
286	}
287
288	if (!diskboot)
289		return;
290
291	if (strcmp(cd->cd_name, "sd") ||
292	    strcmp(cd->cd_name, "st") ||
293	    strcmp(cd->cd_name, "cd")) {
294		struct scsi_attach_args *sa = aux;
295		struct scsi_link *periph = sa->sa_sc_link;
296		int unit;
297
298		if (parent->dv_parent != ctrlrdev)
299			return;
300
301		unit = periph->target * 100 + periph->lun;
302		if (b->unit != unit)
303			return;
304
305		/* we've found it! */
306		booted_device = dev;
307		DPRINTF(("\nbooted_device = %s\n", dev->dv_xname));
308		found = 1;
309	}
310}
311