1/* $NetBSD: dec_alphabook1.c,v 1.25 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_alphabook1.c,v 1.25 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 <machine/rpb.h>
47#include <machine/autoconf.h>
48#include <machine/cpuconf.h>
49#include <sys/bus.h>
50
51#include <dev/ic/comreg.h>
52#include <dev/ic/comvar.h>
53
54#include <dev/isa/isareg.h>
55#include <dev/isa/isavar.h>
56#include <dev/ic/i8042reg.h>
57#include <dev/ic/pckbcvar.h>
58#include <dev/pci/pcireg.h>
59#include <dev/pci/pcivar.h>
60
61#include <alpha/pci/lcareg.h>
62#include <alpha/pci/lcavar.h>
63
64#include <dev/scsipi/scsi_all.h>
65#include <dev/scsipi/scsipi_all.h>
66#include <dev/scsipi/scsiconf.h>
67
68#include "pckbd.h"
69
70#ifndef CONSPEED
71#define CONSPEED TTYDEF_SPEED
72#endif
73static int comcnrate = CONSPEED;
74
75void dec_alphabook1_init(void);
76static void dec_alphabook1_cons_init(void);
77static void dec_alphabook1_device_register(device_t, void *);
78
79#ifdef KGDB
80#include <machine/db_machdep.h>
81
82static const char *kgdb_devlist[] = {
83	"com",
84	NULL,
85};
86#endif /* KGDB */
87
88const struct alpha_variation_table dec_alphabook1_variations[] = {
89	{ 0, "AlphaBook" },
90	{ 0, NULL },
91};
92
93void
94dec_alphabook1_init(void)
95{
96	uint64_t variation;
97
98	platform.family = "AlphaBook";
99
100	if ((platform.model = alpha_dsr_sysname()) == NULL) {
101		variation = hwrpb->rpb_variation & SV_ST_MASK;
102		if ((platform.model = alpha_variation_name(variation,
103		    dec_alphabook1_variations)) == NULL)
104			platform.model = alpha_unknown_sysname();
105	}
106
107	platform.iobus = "lca";
108	platform.cons_init = dec_alphabook1_cons_init;
109	platform.device_register = dec_alphabook1_device_register;
110}
111
112static void
113dec_alphabook1_cons_init(void)
114{
115	struct ctb *ctb;
116	struct lca_config *lcp;
117	extern struct lca_config lca_configuration;
118
119	lcp = &lca_configuration;
120	lca_init(lcp, 0);
121
122	ctb = (struct ctb *)(((char *)hwrpb) + hwrpb->rpb_ctb_off);
123
124	switch (ctb->ctb_term_type) {
125	case CTB_PRINTERPORT:
126		/* serial console ... */
127		/* XXX */
128		{
129			/*
130			 * Delay to allow PROM putchars to complete.
131			 * FIFO depth * character time,
132			 * character time = (1000000 / (defaultrate / 10))
133			 */
134			DELAY(160000000 / comcnrate);
135
136			if(comcnattach(&lcp->lc_iot, 0x3f8, comcnrate,
137			    COM_FREQ, COM_TYPE_NORMAL,
138			    (TTYDEF_CFLAG & ~(CSIZE | PARENB)) | CS8))
139				panic("can't init serial console");
140
141			break;
142		}
143
144	case CTB_GRAPHICS:
145#if NPCKBD > 0
146		/* display console ... */
147		/* XXX */
148		(void) pckbc_cnattach(&lcp->lc_iot, IO_KBD, KBCMDP,
149		    PCKBC_KBD_SLOT);
150
151		if (CTB_TURBOSLOT_TYPE(ctb->ctb_turboslot) ==
152		    CTB_TURBOSLOT_TYPE_ISA)
153			isa_display_console(&lcp->lc_iot, &lcp->lc_memt);
154		else
155			pci_display_console(&lcp->lc_iot, &lcp->lc_memt,
156			    &lcp->lc_pc, CTB_TURBOSLOT_BUS(ctb->ctb_turboslot),
157			    CTB_TURBOSLOT_SLOT(ctb->ctb_turboslot), 0);
158#else
159		panic("not configured to use display && keyboard console");
160#endif
161		break;
162
163	default:
164		printf("ctb->ctb_term_type = 0x%lx\n", ctb->ctb_term_type);
165		printf("ctb->ctb_turboslot = 0x%lx\n", ctb->ctb_turboslot);
166
167		panic("consinit: unknown console type %ld",
168		    ctb->ctb_term_type);
169	}
170#ifdef KGDB
171	/* Attach the KGDB device. */
172	alpha_kgdb_init(kgdb_devlist, &lcp->lc_iot);
173#endif /* KGDB */
174}
175
176static void
177dec_alphabook1_device_register(device_t dev, void *aux)
178{
179	static int found, initted, diskboot, netboot;
180	static device_t pcidev, ctrlrdev;
181	struct bootdev_data *b = bootdev_data;
182	device_t parent = device_parent(dev);
183
184	if (found)
185		return;
186
187	if (!initted) {
188		diskboot = (strcasecmp(b->protocol, "SCSI") == 0);
189		netboot = (strcasecmp(b->protocol, "BOOTP") == 0) ||
190		    (strcasecmp(b->protocol, "MOP") == 0);
191#if 0
192		printf("diskboot = %d, netboot = %d\n", diskboot, netboot);
193#endif
194		initted =1;
195	}
196
197	if (pcidev == NULL) {
198		if (!device_is_a(dev, "pci"))
199			return;
200		else {
201			struct pcibus_attach_args *pba = aux;
202
203			if ((b->slot / 1000) != pba->pba_bus)
204				return;
205
206			pcidev = dev;
207#if 0
208			printf("\npcidev = %s\n", device_xname(dev));
209#endif
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#if 0
229				printf("\nbooted_device = %s\n", device_xname(dev));
230#endif
231				found = 1;
232			} else {
233				ctrlrdev = dev;
234#if 0
235				printf("\nctrlrdev = %s\n", device_xname(dev));
236#endif
237			}
238			return;
239		}
240	}
241
242	if (!diskboot)
243		return;
244
245	if (device_is_a(dev, "sd") ||
246	    device_is_a(dev, "st") ||
247	    device_is_a(dev, "cd")) {
248		struct scsipibus_attach_args *sa = aux;
249		struct scsipi_periph *periph = sa->sa_periph;
250		int unit;
251
252		if (device_parent(parent) != ctrlrdev)
253			return;
254
255		unit = periph->periph_target * 100 + periph->periph_lun;
256		if (b->unit != unit)
257			return;
258		if (b->channel != periph->periph_channel->chan_channel)
259			return;
260
261		/* we've found it! */
262		booted_device = dev;
263#if 0
264		printf("\nbooted_device = %s\n", device_xname(dev));
265#endif
266		found = 1;
267	}
268}
269