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: releng/11.0/sys/dev/fdc/fdc_cbus.c 298426 2016-04-21 18:37:36Z jhb $");
31
32#include <sys/param.h>
33#include <sys/bio.h>
34#include <sys/bus.h>
35#include <sys/kernel.h>
36#include <sys/lock.h>
37#include <sys/module.h>
38#include <sys/mutex.h>
39#include <sys/rman.h>
40#include <sys/systm.h>
41
42#include <machine/bus.h>
43
44#include <pc98/cbus/cbus.h>
45#include <dev/fdc/fdcvar.h>
46
47#include <isa/isavar.h>
48
49static bus_addr_t fdc_iat[] = {0, 2, 4};
50
51static int
52fdc_cbus_alloc_resources(device_t dev, struct fdc_data *fdc)
53{
54	struct resource *res;
55	int i, rid;
56
57	fdc->fdc_dev = dev;
58	rid = 0;
59	res = isa_alloc_resourcev(dev, SYS_RES_IOPORT, &rid, fdc_iat, 3,
60	    RF_ACTIVE);
61	if (res == NULL) {
62		device_printf(dev, "cannot reserve I/O port range\n");
63		return (ENXIO);
64	}
65	isa_load_resourcev(res, fdc_iat, 3);
66	for (i = 0; i < 3; i++) {
67		fdc->resio[i] = res;
68		fdc->ridio[i] = rid;
69		fdc->ioff[i] = i;
70		fdc->ioh[i] = rman_get_bushandle(res);
71	}
72	fdc->iot = rman_get_bustag(res);
73
74	rid = 3;
75	bus_set_resource(dev, SYS_RES_IOPORT, rid, IO_FDPORT, 1);
76	res = bus_alloc_resource_any(dev, SYS_RES_IOPORT, &rid, RF_ACTIVE);
77	if (res == NULL) {
78		device_printf(dev, "cannot reserve I/O port range\n");
79		return (ENXIO);
80	}
81	fdc->resio[3] = res;
82	fdc->ridio[3] = rid;
83	fdc->ioff[3] = 0;
84	fdc->ioh[3] = rman_get_bushandle(res);
85	/* XXX: Reuses fdc->iot */
86
87	rid = 4;
88	bus_set_resource(dev, SYS_RES_IOPORT, rid, 0x4be, 1);
89	res = bus_alloc_resource_any(dev, SYS_RES_IOPORT, &rid, RF_ACTIVE);
90	if (res == NULL) {
91		device_printf(dev, "cannot reserve I/O port range\n");
92		return (ENXIO);
93	}
94	fdc->resio[4] = res;
95	fdc->ridio[4] = rid;
96	fdc->ioff[4] = 0;
97	fdc->ioh[4] = rman_get_bushandle(res);
98	/* XXX: Reuses fdc->iot */
99
100	fdc->res_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &fdc->rid_irq,
101	    RF_ACTIVE);
102	if (fdc->res_irq == NULL) {
103		device_printf(dev, "cannot reserve interrupt line\n");
104		return (ENXIO);
105	}
106
107	if ((fdc->flags & FDC_NODMA) == 0) {
108		fdc->res_drq = bus_alloc_resource_any(dev, SYS_RES_DRQ,
109		    &fdc->rid_drq, RF_ACTIVE);
110		if (fdc->res_drq == NULL) {
111			device_printf(dev, "cannot reserve DMA request line\n");
112			return (ENXIO);
113		}
114		fdc->dmachan = rman_get_start(fdc->res_drq);
115	}
116
117	return (0);
118}
119
120static int
121fdc_cbus_probe(device_t dev)
122{
123	int	error;
124	struct	fdc_data *fdc;
125
126	fdc = device_get_softc(dev);
127
128	/* Check pnp ids */
129	if (isa_get_vendorid(dev))
130		return (ENXIO);
131
132	/* Attempt to allocate our resources for the duration of the probe */
133	error = fdc_cbus_alloc_resources(dev, fdc);
134	if (error == 0)
135		error = fdc_initial_reset(dev, fdc);
136
137	fdc_release_resources(fdc);
138	return (error);
139}
140
141static int
142fdc_cbus_attach(device_t dev)
143{
144	struct	fdc_data *fdc;
145	int error;
146
147	fdc = device_get_softc(dev);
148	error = fdc_cbus_alloc_resources(dev, fdc);
149	if (error == 0)
150		error = fdc_attach(dev);
151	if (error == 0)
152		error = fdc_hints_probe(dev);
153	if (error == 0)
154		fdc_start_worker(dev);
155	else
156		fdc_release_resources(fdc);
157	return (error);
158}
159
160static device_method_t fdc_methods[] = {
161	/* Device interface */
162	DEVMETHOD(device_probe,		fdc_cbus_probe),
163	DEVMETHOD(device_attach,	fdc_cbus_attach),
164	DEVMETHOD(device_detach,	fdc_detach),
165	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
166	DEVMETHOD(device_suspend,	bus_generic_suspend),
167	DEVMETHOD(device_resume,	bus_generic_resume),
168
169	/* Bus interface */
170	DEVMETHOD(bus_print_child,	fdc_print_child),
171	DEVMETHOD(bus_read_ivar,	fdc_read_ivar),
172	DEVMETHOD(bus_write_ivar,       fdc_write_ivar),
173	/* Our children never use any other bus interface methods. */
174
175	{ 0, 0 }
176};
177
178static driver_t fdc_driver = {
179	"fdc",
180	fdc_methods,
181	sizeof(struct fdc_data)
182};
183
184DRIVER_MODULE(fdc, isa, fdc_driver, fdc_devclass, 0, 0);
185