1/*	$OpenBSD: stp_sbus.c,v 1.13 2022/10/16 01:22:39 jsg Exp $	*/
2/*	$NetBSD: stp4020.c,v 1.23 2002/06/01 23:51:03 lukem Exp $	*/
3
4/*-
5 * Copyright (c) 1998 The NetBSD Foundation, Inc.
6 * All rights reserved.
7 *
8 * This code is derived from software contributed to The NetBSD Foundation
9 * by Paul Kranenburg.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 *    notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 *    notice, this list of conditions and the following disclaimer in the
18 *    documentation and/or other materials provided with the distribution.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32
33/*
34 * STP4020: SBus/PCMCIA bridge supporting one Type-3 PCMCIA card, or up to
35 * two Type-1 and Type-2 PCMCIA cards..
36 */
37
38#include <sys/param.h>
39#include <sys/systm.h>
40#include <sys/errno.h>
41#include <sys/malloc.h>
42#include <sys/extent.h>
43#include <sys/proc.h>
44#include <sys/kernel.h>
45#include <sys/device.h>
46
47#include <dev/pcmcia/pcmciareg.h>
48#include <dev/pcmcia/pcmciavar.h>
49#include <dev/pcmcia/pcmciachip.h>
50
51#include <machine/bus.h>
52#include <machine/intr.h>
53
54#include <dev/sbus/sbusvar.h>
55#include <dev/sbus/stp4020reg.h>
56#include <dev/sbus/stp4020var.h>
57
58struct stp4020_sbus_softc {
59	struct stp4020_softc stp;
60	struct sparc_bus_space_tag *sc_bustag_le;
61};
62
63int	stpmatch(struct device *, void *, void *);
64void	stpattach(struct device *, struct device *, void *);
65
66const struct cfattach stp_sbus_ca = {
67	sizeof(struct stp4020_sbus_softc), stpmatch, stpattach
68};
69
70int
71stpmatch(struct device *parent, void *match, void *aux)
72{
73	struct sbus_attach_args *sa = aux;
74
75	return (strcmp("SUNW,pcmcia", sa->sa_name) == 0);
76}
77
78/*
79 * Attach all the sub-devices we can find
80 */
81void
82stpattach(struct device *parent, struct device *self, void *aux)
83{
84	struct sbus_attach_args *sa = aux;
85	struct stp4020_sbus_softc *ssc = (void *)self;
86	struct stp4020_softc *sc = (void *)self;
87	int node;
88	int i;
89	bus_space_tag_t bt;
90	bus_space_handle_t bh;
91
92	node = sa->sa_node;
93
94	/* Allocate little-endian bus tag */
95	ssc->sc_bustag_le = malloc(sizeof(*sa->sa_bustag), M_DEVBUF, M_NOWAIT);
96	if (ssc->sc_bustag_le == NULL)
97		panic("could not allocate stp bus tag");
98	*ssc->sc_bustag_le = *sa->sa_bustag;
99	ssc->sc_bustag_le->asi = ASI_PRIMARY_LITTLE;
100
101	/* Transfer bus tags */
102	sc->sc_bustag = sa->sa_bustag;
103
104	/* Set up per-socket static initialization */
105	sc->sc_socks[0].sc = sc->sc_socks[1].sc = sc;
106	sc->sc_socks[0].tag = sc->sc_socks[1].tag = sa->sa_bustag;
107
108	if (sa->sa_nreg < 8) {
109		printf(": only %d register sets\n", sa->sa_nreg);
110		return;
111	}
112
113	if (sa->sa_nintr != 2) {
114		printf(": expect 2 interrupt SBus levels; got %d\n",
115		    sa->sa_nintr);
116		return;
117	}
118
119#define STP4020_BANK_PROM	0
120#define STP4020_BANK_CTRL	4
121	for (i = 0; i < 8; i++) {
122
123		/*
124		 * STP4020 Register address map:
125		 *	bank  0:   Forth PROM
126		 *	banks 1-3: socket 0, windows 0-2
127		 *	bank  4:   control registers
128		 *	banks 5-7: socket 1, windows 0-2
129		 */
130
131		if (i == STP4020_BANK_PROM)
132			/* Skip the PROM */
133			continue;
134
135		if (i == STP4020_BANK_CTRL)
136			bt = sc->sc_bustag;
137		else
138			bt = ssc->sc_bustag_le;
139
140		if (sbus_bus_map(bt, sa->sa_reg[i].sbr_slot,
141		    sa->sa_reg[i].sbr_offset, sa->sa_reg[i].sbr_size, 0, 0,
142		    &bh) != 0) {
143			printf(": attach: cannot map registers\n");
144			return;
145		}
146
147		if (i == STP4020_BANK_CTRL) {
148			/*
149			 * Copy tag and handle to both socket structures
150			 * for easy access in control/status IO functions.
151			 */
152			sc->sc_socks[0].regs = sc->sc_socks[1].regs = bh;
153		} else if (i < STP4020_BANK_CTRL) {
154			/* banks 1-3 */
155			sc->sc_socks[0].windows[i-1].winaddr = bh;
156			sc->sc_socks[0].wintag = bt;
157		} else {
158			/* banks 5-7 */
159			sc->sc_socks[1].windows[i-5].winaddr = bh;
160			sc->sc_socks[1].wintag = bt;
161		}
162	}
163
164	/*
165	 * We get to use two SBus interrupt levels.
166	 * The higher level we use for status change interrupts;
167	 * the lower level for PC card I/O.
168	 */
169	bus_intr_establish(sa->sa_bustag, sa->sa_intr[1].sbi_pri,
170	    IPL_NONE, 0, stp4020_statintr, sc, self->dv_xname);
171
172	bus_intr_establish(sa->sa_bustag, sa->sa_intr[0].sbi_pri,
173	    IPL_NONE, 0, stp4020_iointr, sc, self->dv_xname);
174
175	stpattach_common(sc, sa->sa_frequency);
176}
177