1/*-
2 * Copyright (c) 2004 TAKAHASHI Yoshihiro
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions, and the following disclaimer,
10 *    without modification, immediately at the beginning of the file.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in
13 *    the documentation and/or other materials provided with the
14 *    distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
20 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29#include <sys/cdefs.h>
30__FBSDID("$FreeBSD$");
31
32#include <sys/param.h>
33#include <sys/bio.h>
34#include <sys/bus.h>
35#include <sys/kernel.h>
36#include <sys/module.h>
37#include <sys/rman.h>
38#include <sys/systm.h>
39
40#include <machine/bus.h>
41
42#include <pc98/cbus/cbus.h>
43#include <pc98/cbus/fdcreg.h>
44#include <pc98/cbus/fdcvar.h>
45
46#include <isa/isavar.h>
47
48static bus_addr_t fdc_iat[] = {0, 2, 4};
49
50static int
51fdc_cbus_alloc_resources(device_t dev, struct fdc_data *fdc)
52{
53	int rid;
54
55	fdc->fdc_dev = dev;
56	fdc->rid_ioport = 0;
57	fdc->rid_irq = 0;
58	fdc->rid_drq = 0;
59	fdc->res_irq = 0;
60	fdc->res_drq = 0;
61
62	fdc->res_ioport = isa_alloc_resourcev(dev, SYS_RES_IOPORT,
63					      &fdc->rid_ioport, fdc_iat,
64					      3, RF_ACTIVE);
65	if (fdc->res_ioport == 0) {
66		device_printf(dev, "cannot reserve I/O port range\n");
67		return ENXIO;
68	}
69	isa_load_resourcev(fdc->res_ioport, fdc_iat, 3);
70	fdc->portt = rman_get_bustag(fdc->res_ioport);
71	fdc->porth = rman_get_bushandle(fdc->res_ioport);
72
73	rid = 3;
74	bus_set_resource(dev, SYS_RES_IOPORT, rid, IO_FDPORT, 1);
75	fdc->res_fdsio = bus_alloc_resource_any(dev, SYS_RES_IOPORT, &rid,
76						RF_ACTIVE);
77	if (fdc->res_fdsio == 0)
78		return ENXIO;
79	fdc->sc_fdsiot = rman_get_bustag(fdc->res_fdsio);
80	fdc->sc_fdsioh = rman_get_bushandle(fdc->res_fdsio);
81
82	rid = 4;
83	bus_set_resource(dev, SYS_RES_IOPORT, rid, 0x4be, 1);
84	fdc->res_fdemsio = bus_alloc_resource_any(dev, SYS_RES_IOPORT, &rid,
85						  RF_ACTIVE);
86	if (fdc->res_fdemsio == 0)
87		return ENXIO;
88	fdc->sc_fdemsiot = rman_get_bustag(fdc->res_fdemsio);
89	fdc->sc_fdemsioh = rman_get_bushandle(fdc->res_fdemsio);
90
91	fdc->res_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &fdc->rid_irq,
92					      RF_ACTIVE);
93	if (fdc->res_irq == 0) {
94		device_printf(dev, "cannot reserve interrupt line\n");
95		return ENXIO;
96	}
97
98	if ((fdc->flags & FDC_NODMA) == 0) {
99		fdc->res_drq = bus_alloc_resource_any(dev, SYS_RES_DRQ,
100						      &fdc->rid_drq, RF_ACTIVE);
101		if (fdc->res_drq == 0) {
102			device_printf(dev, "cannot reserve DMA request line\n");
103			return ENXIO;
104		}
105		fdc->dmachan = rman_get_start(fdc->res_drq);
106	}
107
108	return 0;
109}
110
111static int
112fdc_cbus_probe(device_t dev)
113{
114	int	error;
115	struct	fdc_data *fdc;
116
117	fdc = device_get_softc(dev);
118
119	/* Check pnp ids */
120	if (isa_get_vendorid(dev))
121		return (ENXIO);
122
123	/* Attempt to allocate our resources for the duration of the probe */
124	error = fdc_cbus_alloc_resources(dev, fdc);
125	if (!error)
126		error = fdc_initial_reset(fdc);
127
128	fdc_release_resources(fdc);
129	return (error);
130}
131
132static int
133fdc_cbus_attach(device_t dev)
134{
135	struct	fdc_data *fdc;
136	int error;
137
138	fdc = device_get_softc(dev);
139
140	if ((error = fdc_cbus_alloc_resources(dev, fdc)) != 0 ||
141	    (error = fdc_attach(dev)) != 0 ||
142	    (error = fdc_hints_probe(dev)) != 0) {
143		fdc_release_resources(fdc);
144		return (error);
145	}
146
147	return (0);
148}
149
150static device_method_t fdc_methods[] = {
151	/* Device interface */
152	DEVMETHOD(device_probe,		fdc_cbus_probe),
153	DEVMETHOD(device_attach,	fdc_cbus_attach),
154	DEVMETHOD(device_detach,	fdc_detach),
155	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
156	DEVMETHOD(device_suspend,	bus_generic_suspend),
157	DEVMETHOD(device_resume,	bus_generic_resume),
158
159	/* Bus interface */
160	DEVMETHOD(bus_print_child,	fdc_print_child),
161	DEVMETHOD(bus_read_ivar,	fdc_read_ivar),
162	DEVMETHOD(bus_write_ivar,       fdc_write_ivar),
163	/* Our children never use any other bus interface methods. */
164
165	{ 0, 0 }
166};
167
168static driver_t fdc_driver = {
169	"fdc",
170	fdc_methods,
171	sizeof(struct fdc_data)
172};
173
174DRIVER_MODULE(fdc, isa, fdc_driver, fdc_devclass, 0, 0);
175