1/*	$NetBSD: fdc_pnpbios.c,v 1.19 2016/07/11 11:31:49 msaitoh Exp $	*/
2
3/*-
4 * Copyright (c) 2000 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jason R. Thorpe.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 *    notice, 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 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32/*
33 * PNPBIOS attachment for the PC Floppy Controller driver.
34 */
35
36#include <sys/cdefs.h>
37__KERNEL_RCSID(0, "$NetBSD: fdc_pnpbios.c,v 1.19 2016/07/11 11:31:49 msaitoh Exp $");
38
39
40
41#include <sys/param.h>
42#include <sys/systm.h>
43#include <sys/callout.h>
44#include <sys/device.h>
45#include <sys/buf.h>
46#include <sys/queue.h>
47
48#include <sys/bus.h>
49#include <machine/intr.h>
50
51#include <dev/isa/isavar.h>
52#include <dev/isa/isadmavar.h>
53
54#include <dev/isa/fdcvar.h>
55
56#include <i386/pnpbios/pnpbiosvar.h>
57
58int	fdc_pnpbios_match(device_t, cfdata_t, void *);
59void	fdc_pnpbios_attach(device_t, device_t, void *);
60
61struct fdc_pnpbios_softc {
62        struct fdc_softc sc_fdc;        /* base fdc device */
63
64        bus_space_handle_t sc_baseioh;  /* base I/O handle */
65};
66
67
68CFATTACH_DECL_NEW(fdc_pnpbios, sizeof(struct fdc_pnpbios_softc),
69    fdc_pnpbios_match, fdc_pnpbios_attach, NULL, NULL);
70
71int
72fdc_pnpbios_match(device_t parent, cfdata_t match, void *aux)
73{
74	struct pnpbiosdev_attach_args *aa = aux;
75
76	if (strcmp(aa->idstr, "PNP0700") == 0)
77		return (1);
78
79	return (0);
80}
81
82void
83fdc_pnpbios_attach(device_t parent, device_t self, void *aux)
84{
85        struct fdc_pnpbios_softc *pdc = device_private(self);
86	struct fdc_softc *fdc = &pdc->sc_fdc;
87	struct pnpbiosdev_attach_args *aa = aux;
88        int size, base;
89
90	aprint_normal("\n");
91
92	fdc->sc_dev = self;
93	fdc->sc_ic = aa->ic;
94
95	if (pnpbios_io_map(aa->pbt, aa->resc, 0, &fdc->sc_iot,
96            &pdc->sc_baseioh)) {
97		aprint_error_dev(self, "unable to map I/O space\n");
98		return;
99	}
100
101	/*
102         * Start accounting for "odd" pnpbios's. Some probe as 4 ports,
103         * some as 6 and some don't give the ctl port back.
104         */
105
106        if (pnpbios_getiosize(aa->pbt, aa->resc, 0, &size)) {
107                aprint_error_dev(self, "can't get iobase size\n");
108                return;
109        }
110
111        switch (size) {
112
113        /* Easy case. This matches right up with what the fdc code expects. */
114        case 4:
115                fdc->sc_ioh = pdc->sc_baseioh;
116                break;
117
118        /* Map a subregion so this all lines up with the fdc code. */
119        case 6:
120                if (bus_space_subregion(fdc->sc_iot, pdc->sc_baseioh, 2, 4,
121                    &fdc->sc_ioh)) {
122                        aprint_error_dev(self,
123			    "unable to subregion I/O space\n");
124                        return;
125                }
126                break;
127        default:
128                aprint_error_dev(self, "unknown size: %d of io mapping\n",
129		    size);
130                return;
131        }
132
133        /*
134         * XXX: This is bad. If the pnpbios claims only 1 I/O range then it's
135         * omitting the controller I/O port. (One has to exist for there to
136         * be a working fdc). Just try and force the mapping in.
137         */
138
139        if (aa->resc->numio == 1) {
140
141                if (pnpbios_getiobase(aa->pbt, aa->resc, 0, 0, &base)) {
142                        aprint_error_dev(self, "can't get iobase\n");
143                        return;
144                }
145                if (bus_space_map(fdc->sc_iot, base + size + 1, 1, 0,
146                    &fdc->sc_fdctlioh)) {
147                        aprint_error_dev(self,
148			    "unable to force map CTL I/O space\n");
149                        return;
150                }
151        } else if (pnpbios_io_map(aa->pbt, aa->resc, 1, &fdc->sc_iot,
152            &fdc->sc_fdctlioh)) {
153                aprint_error_dev(self, "unable to map CTL I/O space\n");
154		return;
155	}
156
157	if (pnpbios_getdmachan(aa->pbt, aa->resc, 0, &fdc->sc_drq)) {
158		aprint_error_dev(self, "unable to get DMA channel\n");
159		return;
160	}
161
162	pnpbios_print_devres(self, aa);
163        if (aa->resc->numio == 1)
164                aprint_error_dev(self,
165		    "ctl io %x didn't probe. Forced attach\n",
166		    base + size + 1);
167
168	fdc->sc_ih = pnpbios_intr_establish(aa->pbt, aa->resc, 0, IPL_BIO,
169	    fdcintr, fdc);
170
171	fdcattach(fdc);
172}
173