1*2a84159aStsutsui /* $NetBSD: fdcisa.c,v 1.16 2023/01/06 10:28:28 tsutsui Exp $ */
2a5fdf3f1Sleo
3a5fdf3f1Sleo /*-
4a5fdf3f1Sleo * Copyright (c) 1998 The NetBSD Foundation, Inc.
5a5fdf3f1Sleo * All rights reserved.
6a5fdf3f1Sleo *
7a5fdf3f1Sleo * This code is derived from software contributed to The NetBSD Foundation
8a5fdf3f1Sleo * by Charles M. Hannum.
9a5fdf3f1Sleo *
10a5fdf3f1Sleo * Redistribution and use in source and binary forms, with or without
11a5fdf3f1Sleo * modification, are permitted provided that the following conditions
12a5fdf3f1Sleo * are met:
13a5fdf3f1Sleo * 1. Redistributions of source code must retain the above copyright
14a5fdf3f1Sleo * notice, this list of conditions and the following disclaimer.
15a5fdf3f1Sleo * 2. Redistributions in binary form must reproduce the above copyright
16a5fdf3f1Sleo * notice, this list of conditions and the following disclaimer in the
17a5fdf3f1Sleo * documentation and/or other materials provided with the distribution.
18a5fdf3f1Sleo *
19a5fdf3f1Sleo * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20a5fdf3f1Sleo * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21a5fdf3f1Sleo * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22a5fdf3f1Sleo * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23a5fdf3f1Sleo * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24a5fdf3f1Sleo * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25a5fdf3f1Sleo * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26a5fdf3f1Sleo * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27a5fdf3f1Sleo * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28a5fdf3f1Sleo * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29a5fdf3f1Sleo * POSSIBILITY OF SUCH DAMAGE.
30a5fdf3f1Sleo */
31a5fdf3f1Sleo
32a5fdf3f1Sleo /*-
33a5fdf3f1Sleo * Copyright (c) 1990 The Regents of the University of California.
34a5fdf3f1Sleo * All rights reserved.
35a5fdf3f1Sleo *
36a5fdf3f1Sleo * This code is derived from software contributed to Berkeley by
37a5fdf3f1Sleo * Don Ahn.
38a5fdf3f1Sleo *
39a5fdf3f1Sleo * Redistribution and use in source and binary forms, with or without
40a5fdf3f1Sleo * modification, are permitted provided that the following conditions
41a5fdf3f1Sleo * are met:
42a5fdf3f1Sleo * 1. Redistributions of source code must retain the above copyright
43a5fdf3f1Sleo * notice, this list of conditions and the following disclaimer.
44a5fdf3f1Sleo * 2. Redistributions in binary form must reproduce the above copyright
45a5fdf3f1Sleo * notice, this list of conditions and the following disclaimer in the
46a5fdf3f1Sleo * documentation and/or other materials provided with the distribution.
47aad01611Sagc * 3. Neither the name of the University nor the names of its contributors
48a5fdf3f1Sleo * may be used to endorse or promote products derived from this software
49a5fdf3f1Sleo * without specific prior written permission.
50a5fdf3f1Sleo *
51a5fdf3f1Sleo * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
52a5fdf3f1Sleo * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
53a5fdf3f1Sleo * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
54a5fdf3f1Sleo * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
55a5fdf3f1Sleo * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
56a5fdf3f1Sleo * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
57a5fdf3f1Sleo * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
58a5fdf3f1Sleo * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
59a5fdf3f1Sleo * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
60a5fdf3f1Sleo * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
61a5fdf3f1Sleo * SUCH DAMAGE.
62a5fdf3f1Sleo *
63a5fdf3f1Sleo * @(#)fd.c 7.4 (Berkeley) 5/25/91
64a5fdf3f1Sleo */
65e803bea7Slukem
66e803bea7Slukem #include <sys/cdefs.h>
67*2a84159aStsutsui __KERNEL_RCSID(0, "$NetBSD: fdcisa.c,v 1.16 2023/01/06 10:28:28 tsutsui Exp $");
68e803bea7Slukem
69a5fdf3f1Sleo #include <sys/param.h>
70a5fdf3f1Sleo #include <sys/systm.h>
71a5fdf3f1Sleo #include <sys/callout.h>
72a5fdf3f1Sleo #include <sys/device.h>
73a5fdf3f1Sleo
74ed9977b1Sdyoung #include <sys/bus.h>
75a5fdf3f1Sleo
76a5fdf3f1Sleo #include <dev/isa/isavar.h>
77a5fdf3f1Sleo #include <dev/isa/isadmavar.h>
78a5fdf3f1Sleo #include <dev/isa/fdreg.h>
79a5fdf3f1Sleo #include <dev/isa/fdcvar.h>
80a5fdf3f1Sleo
81a5fdf3f1Sleo #include <atari/atari/device.h>
82a5fdf3f1Sleo
83a5fdf3f1Sleo
84a5fdf3f1Sleo /* controller driver configuration */
859580ea7cStsutsui static int fdc_isa_probe (device_t, cfdata_t, void *);
869580ea7cStsutsui static void fdc_isa_attach (device_t, device_t, void *);
87a5fdf3f1Sleo
88a5fdf3f1Sleo struct fdc_isa_softc {
89a5fdf3f1Sleo struct fdc_softc sc_fdc; /* base fdc device */
90a5fdf3f1Sleo bus_space_handle_t sc_baseioh; /* base I/O handle */
91a5fdf3f1Sleo };
92a5fdf3f1Sleo
93bb192d4cScube CFATTACH_DECL_NEW(fdcisa, sizeof(struct fdc_isa_softc),
94c5e91d44Sthorpej fdc_isa_probe, fdc_isa_attach, NULL, NULL);
95a5fdf3f1Sleo
969580ea7cStsutsui static int
fdc_isa_probe(device_t parent,cfdata_t cfp,void * aux)97bb192d4cScube fdc_isa_probe(device_t parent, cfdata_t cfp, void *aux)
98a5fdf3f1Sleo {
99a5fdf3f1Sleo struct isa_attach_args *ia = aux;
100a5fdf3f1Sleo static int fdc_matched = 0;
101a5fdf3f1Sleo bus_space_tag_t iot;
102a5fdf3f1Sleo bus_space_handle_t ioh, ctl_ioh, base_ioh;
1033835413bSthorpej int iobase;
104a5fdf3f1Sleo
105a5fdf3f1Sleo if (!atari_realconfig)
106a5fdf3f1Sleo return 0;
107a5fdf3f1Sleo
108a5fdf3f1Sleo /* Match only once */
109a5fdf3f1Sleo if (fdc_matched)
110a5fdf3f1Sleo return 0;
111a5fdf3f1Sleo
112a5fdf3f1Sleo iot = ia->ia_iot;
113a5fdf3f1Sleo
1143835413bSthorpej if (ia->ia_nio < 1)
1153835413bSthorpej return (0);
1163835413bSthorpej if (ia->ia_nirq < 1)
1173835413bSthorpej return (0);
1183835413bSthorpej if (ia->ia_ndrq < 1)
1193835413bSthorpej return (0);
1203835413bSthorpej
1213835413bSthorpej if (ISA_DIRECT_CONFIG(ia))
1223835413bSthorpej return (0);
1233835413bSthorpej
1243835413bSthorpej /* Disallow wildcarded I/O addresses. */
1252db68840Sdrochner if (ia->ia_io[0].ir_addr == ISA_UNKNOWN_PORT)
1263835413bSthorpej return (0);
1273835413bSthorpej
1283835413bSthorpej /* Don't allow wildcarded IRQ/DRQ. */
1292db68840Sdrochner if (ia->ia_irq[0].ir_irq == ISA_UNKNOWN_IRQ)
1303835413bSthorpej return (0);
1313835413bSthorpej
1322db68840Sdrochner if (ia->ia_drq[0].ir_drq == ISA_UNKNOWN_DRQ)
1333835413bSthorpej return (0);
134a5fdf3f1Sleo
135a5fdf3f1Sleo /* Map the i/o space. */
1363835413bSthorpej iobase = ia->ia_io[0].ir_addr;
1373835413bSthorpej if (bus_space_map(iot, iobase, 6 /* FDC_NPORT */, 0, &base_ioh)) {
138a5fdf3f1Sleo printf("fdcisaprobe: cannot map io-area\n");
139a5fdf3f1Sleo return 0;
140a5fdf3f1Sleo }
141a5fdf3f1Sleo if (bus_space_subregion(iot, base_ioh, 2, 4, &ioh)) {
142a5fdf3f1Sleo bus_space_unmap(iot, base_ioh, 6);
143a5fdf3f1Sleo return (0);
144a5fdf3f1Sleo }
145a5fdf3f1Sleo
1463835413bSthorpej if (bus_space_map(iot, iobase + fdctl + 2, 1, 0, &ctl_ioh)) {
147a5fdf3f1Sleo bus_space_unmap(iot, base_ioh, 6);
148a5fdf3f1Sleo return (0);
149a5fdf3f1Sleo }
150a5fdf3f1Sleo
151a5fdf3f1Sleo /* Not needed for the rest of the probe. */
152a5fdf3f1Sleo bus_space_unmap(iot, ctl_ioh, 1);
153a5fdf3f1Sleo
154a5fdf3f1Sleo /* reset */
155a5fdf3f1Sleo bus_space_write_1(iot, ioh, fdout, 0);
156a5fdf3f1Sleo delay(100);
157a5fdf3f1Sleo bus_space_write_1(iot, ioh, fdout, FDO_FRST);
158a5fdf3f1Sleo
159a5fdf3f1Sleo /* see if it can handle a command */
160a5fdf3f1Sleo if (out_fdc(iot, ioh, NE7CMD_SPECIFY) < 0)
161a5fdf3f1Sleo goto out;
162a5fdf3f1Sleo out_fdc(iot, ioh, 0xdf);
163a5fdf3f1Sleo out_fdc(iot, ioh, 2);
164a5fdf3f1Sleo
165a5fdf3f1Sleo fdc_matched = 1;
1663835413bSthorpej ia->ia_nio = 1;
1673835413bSthorpej ia->ia_io[0].ir_size = FDC_NPORT;
1683835413bSthorpej
1693835413bSthorpej ia->ia_nirq = 1;
1703835413bSthorpej ia->ia_ndrq = 1;
1713835413bSthorpej
1723835413bSthorpej ia->ia_niomem = 0;
1733835413bSthorpej
174a5fdf3f1Sleo out:
175a5fdf3f1Sleo bus_space_unmap(iot, base_ioh, 6 /* FDC_NPORT */);
176a5fdf3f1Sleo
177a5fdf3f1Sleo return fdc_matched;
178a5fdf3f1Sleo }
179a5fdf3f1Sleo
1809580ea7cStsutsui static void
fdc_isa_attach(device_t parent,device_t self,void * aux)181bb192d4cScube fdc_isa_attach(device_t parent, device_t self, void *aux)
182a5fdf3f1Sleo {
183bb192d4cScube struct fdc_isa_softc *isc = device_private(self);
184bb192d4cScube struct fdc_softc *fdc = &isc->sc_fdc;
185a5fdf3f1Sleo struct isa_attach_args *ia = aux;
186a5fdf3f1Sleo
187b09761a4Syamt aprint_normal("\n");
188bb192d4cScube
189bb192d4cScube fdc->sc_dev = self;
190a5fdf3f1Sleo fdc->sc_iot = ia->ia_iot;
191a5fdf3f1Sleo fdc->sc_ic = ia->ia_ic;
1923835413bSthorpej fdc->sc_drq = ia->ia_drq[0].ir_drq;
193a5fdf3f1Sleo
1943835413bSthorpej if (bus_space_map(fdc->sc_iot, ia->ia_io[0].ir_addr,
1953835413bSthorpej 6 /* FDC_NPORT */, 0, &isc->sc_baseioh)) {
196bb192d4cScube aprint_error_dev(self, "unable to map I/O space\n");
197a5fdf3f1Sleo return;
198a5fdf3f1Sleo }
199a5fdf3f1Sleo
200a5fdf3f1Sleo if (bus_space_subregion(fdc->sc_iot, isc->sc_baseioh, 2, 4,
201a5fdf3f1Sleo &fdc->sc_ioh)) {
202bb192d4cScube aprint_error_dev(self, "unable to subregion I/O space\n");
203a5fdf3f1Sleo return;
204a5fdf3f1Sleo }
205a5fdf3f1Sleo
2063835413bSthorpej if (bus_space_map(fdc->sc_iot, ia->ia_io[0].ir_addr + fdctl + 2, 1, 0,
207a5fdf3f1Sleo &fdc->sc_fdctlioh)) {
208bb192d4cScube aprint_error_dev(self, "unable to map CTL I/O space\n");
209a5fdf3f1Sleo return;
210a5fdf3f1Sleo }
211a5fdf3f1Sleo
2123835413bSthorpej fdc->sc_ih = isa_intr_establish(ia->ia_ic, ia->ia_irq[0].ir_irq,
2133835413bSthorpej IST_EDGE, IPL_BIO, fdcintr, fdc);
214a5fdf3f1Sleo
215864b9626Smycroft fdcattach(fdc);
216a5fdf3f1Sleo }
217