1/* $NetBSD: dec_eb164.c,v 1.60 2011/07/01 19:22:35 dyoung Exp $ */
2
3/*
4 * Copyright (c) 1995, 1996, 1997 Carnegie-Mellon University.
5 * All rights reserved.
6 *
7 * Author: Chris G. Demetriou
8 *
9 * Permission to use, copy, modify and distribute this software and
10 * its documentation is hereby granted, provided that both the copyright
11 * notice and this permission notice appear in all copies of the
12 * software, derivative works or modified versions, and any portions
13 * thereof, and that both notices appear in supporting documentation.
14 *
15 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
16 * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
17 * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
18 *
19 * Carnegie Mellon requests users of this software to return to
20 *
21 *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
22 *  School of Computer Science
23 *  Carnegie Mellon University
24 *  Pittsburgh PA 15213-3890
25 *
26 * any improvements or extensions that they make and grant Carnegie the
27 * rights to redistribute these changes.
28 */
29/*
30 * Additional Copyright (c) 1997 by Matthew Jacob for NASA/Ames Research Center
31 */
32
33#include "opt_kgdb.h"
34
35#include <sys/cdefs.h>			/* RCS ID & Copyright macro defns */
36
37__KERNEL_RCSID(0, "$NetBSD: dec_eb164.c,v 1.60 2011/07/01 19:22:35 dyoung Exp $");
38
39#include <sys/param.h>
40#include <sys/systm.h>
41#include <sys/device.h>
42#include <sys/termios.h>
43#include <sys/conf.h>
44#include <dev/cons.h>
45
46#include <uvm/uvm_extern.h>
47
48#include <machine/rpb.h>
49#include <machine/autoconf.h>
50#include <machine/cpuconf.h>
51#include <sys/bus.h>
52
53#include <dev/ic/comreg.h>
54#include <dev/ic/comvar.h>
55
56#include <dev/isa/isareg.h>
57#include <dev/isa/isavar.h>
58#include <dev/ic/i8042reg.h>
59#include <dev/ic/pckbcvar.h>
60#include <dev/pci/pcireg.h>
61#include <dev/pci/pcivar.h>
62
63#include <alpha/pci/ciareg.h>
64#include <alpha/pci/ciavar.h>
65
66#include <dev/scsipi/scsi_all.h>
67#include <dev/scsipi/scsipi_all.h>
68#include <dev/scsipi/scsiconf.h>
69#include <dev/ata/atavar.h>
70
71#include "pckbd.h"
72
73#ifndef CONSPEED
74#define CONSPEED TTYDEF_SPEED
75#endif
76static int comcnrate = CONSPEED;
77
78#define	DR_VERBOSE(f) while (0)
79
80void dec_eb164_init(void);
81static void dec_eb164_cons_init(void);
82static void dec_eb164_device_register(device_t, void *);
83
84#ifdef KGDB
85#include <machine/db_machdep.h>
86
87static const char *kgdb_devlist[] = {
88	"com",
89	NULL,
90};
91#endif /* KGDB */
92
93void
94dec_eb164_init(void)
95{
96
97	platform.family = "EB164";
98
99	if ((platform.model = alpha_dsr_sysname()) == NULL) {
100		/* XXX Don't know the system variations, yet. */
101		platform.model = alpha_unknown_sysname();
102	}
103
104	platform.iobus = "cia";
105	platform.cons_init = dec_eb164_cons_init;
106	platform.device_register = dec_eb164_device_register;
107
108	/*
109	 * EB164 systems have a 2MB secondary cache.
110	 */
111	uvmexp.ncolors = atop(2 * 1024 * 1024);
112}
113
114static void
115dec_eb164_cons_init(void)
116{
117	struct ctb *ctb;
118	struct cia_config *ccp;
119	extern struct cia_config cia_configuration;
120
121	ccp = &cia_configuration;
122	cia_init(ccp, 0);
123
124	ctb = (struct ctb *)(((char *)hwrpb) + hwrpb->rpb_ctb_off);
125
126	switch (ctb->ctb_term_type) {
127	case CTB_PRINTERPORT:
128		/* serial console ... */
129		/* XXX */
130		{
131			/*
132			 * Delay to allow PROM putchars to complete.
133			 * FIFO depth * character time,
134			 * character time = (1000000 / (defaultrate / 10))
135			 */
136			DELAY(160000000 / comcnrate);
137
138			if(comcnattach(&ccp->cc_iot, 0x3f8, comcnrate,
139			    COM_FREQ, COM_TYPE_NORMAL,
140			    (TTYDEF_CFLAG & ~(CSIZE | PARENB)) | CS8))
141				panic("can't init serial console");
142
143			break;
144		}
145
146	case CTB_GRAPHICS:
147#if NPCKBD > 0
148		/* display console ... */
149		/* XXX */
150		(void) pckbc_cnattach(&ccp->cc_iot, IO_KBD, KBCMDP,
151		    PCKBC_KBD_SLOT);
152
153		if (CTB_TURBOSLOT_TYPE(ctb->ctb_turboslot) ==
154		    CTB_TURBOSLOT_TYPE_ISA)
155			isa_display_console(&ccp->cc_iot, &ccp->cc_memt);
156		else
157			pci_display_console(&ccp->cc_iot, &ccp->cc_memt,
158			    &ccp->cc_pc, CTB_TURBOSLOT_BUS(ctb->ctb_turboslot),
159			    CTB_TURBOSLOT_SLOT(ctb->ctb_turboslot), 0);
160#else
161		panic("not configured to use display && keyboard console");
162#endif
163		break;
164
165	default:
166		printf("ctb->ctb_term_type = 0x%lx\n", ctb->ctb_term_type);
167		printf("ctb->ctb_turboslot = 0x%lx\n", ctb->ctb_turboslot);
168
169		panic("consinit: unknown console type %ld",
170		    ctb->ctb_term_type);
171	}
172#ifdef KGDB
173	/* Attach the KGDB device. */
174	alpha_kgdb_init(kgdb_devlist, &ccp->cc_iot);
175#endif /* KGDB */
176}
177
178static void
179dec_eb164_device_register(device_t dev, void *aux)
180{
181	static int found, initted, diskboot, netboot;
182	static device_t pcidev, ctrlrdev;
183	struct bootdev_data *b = bootdev_data;
184	device_t parent = device_parent(dev);
185
186	if (found)
187		return;
188
189	if (!initted) {
190		diskboot = (strcasecmp(b->protocol, "SCSI") == 0) ||
191		    (strcasecmp(b->protocol, "IDE") == 0);
192		netboot = (strcasecmp(b->protocol, "BOOTP") == 0) ||
193		    (strcasecmp(b->protocol, "MOP") == 0);
194		DR_VERBOSE(printf("diskboot = %d, netboot = %d\n", diskboot,
195		    netboot));
196		initted = 1;
197	}
198
199	if (pcidev == NULL) {
200		if (!device_is_a(dev, "pci"))
201			return;
202		else {
203			struct pcibus_attach_args *pba = aux;
204
205			if ((b->slot / 1000) != pba->pba_bus)
206				return;
207
208			pcidev = dev;
209			DR_VERBOSE(printf("\npcidev = %s\n", device_xname(dev)));
210			return;
211		}
212	}
213
214	if (ctrlrdev == NULL) {
215		if (parent != pcidev)
216			return;
217		else {
218			struct pci_attach_args *pa = aux;
219			int slot;
220
221			slot = pa->pa_bus * 1000 + pa->pa_function * 100 +
222			    pa->pa_device;
223			if (b->slot != slot)
224				return;
225
226			if (netboot) {
227				booted_device = dev;
228				DR_VERBOSE(printf("\nbooted_device = %s\n",
229				    device_xname(dev)));
230				found = 1;
231			} else {
232				ctrlrdev = dev;
233				DR_VERBOSE(printf("\nctrlrdev = %s\n",
234				    device_xname(dev)));
235			}
236			return;
237		}
238	}
239
240	if (!diskboot)
241		return;
242
243	if (device_is_a(dev, "sd") ||
244	    device_is_a(dev, "st") ||
245	    device_is_a(dev, "cd")) {
246		struct scsipibus_attach_args *sa = aux;
247		struct scsipi_periph *periph = sa->sa_periph;
248		int unit;
249
250		if (device_parent(parent) != ctrlrdev)
251			return;
252
253		unit = periph->periph_target * 100 + periph->periph_lun;
254		if (b->unit != unit)
255			return;
256		if (b->channel != periph->periph_channel->chan_channel)
257			return;
258
259		/* we've found it! */
260		booted_device = dev;
261		DR_VERBOSE(printf("\nbooted_device = %s\n", device_xname(dev)));
262		found = 1;
263	}
264
265	/*
266	 * Support to boot from IDE drives.
267	 */
268	if (device_is_a(dev, "wd")) {
269		struct ata_device *adev = aux;
270
271		if (!device_is_a(parent, "atabus"))
272			return;
273		if (device_parent(parent) != ctrlrdev)
274			return;
275
276		DR_VERBOSE(printf("\nAtapi info: drive: %d, channel %d\n",
277		    adev->adev_drv_data->drive, adev->adev_channel));
278		DR_VERBOSE(printf("Bootdev info: unit: %d, channel: %d\n",
279		    b->unit, b->channel));
280		if (b->unit != adev->adev_drv_data->drive ||
281		    b->channel != adev->adev_channel)
282			return;
283
284		/* we've found it! */
285		booted_device = dev;
286		DR_VERBOSE(printf("booted_device = %s\n", device_xname(dev)));
287		found = 1;
288	}
289}
290