1132166Snjl/*-
2140469Simp * Copyright (c) 2004-2005 M. Warner Losh.
3131767Simp * All rights reserved.
4131767Simp *
5131767Simp * Redistribution and use in source and binary forms, with or without
6131767Simp * modification, are permitted provided that the following conditions
7131767Simp * are met:
8131767Simp * 1. Redistributions of source code must retain the above copyright
9140040Simp *    notice, this list of conditions and the following disclaimer.
10131767Simp * 2. Redistributions in binary form must reproduce the above copyright
11140040Simp *    notice, this list of conditions and the following disclaimer in the
12140040Simp *    documentation and/or other materials provided with the distribution.
13131767Simp *
14131767Simp * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15131767Simp * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16131767Simp * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17140040Simp * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18140040Simp * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19131767Simp * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20131767Simp * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21131767Simp * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22131767Simp * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23131767Simp * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24131767Simp * SUCH DAMAGE.
25131767Simp */
26131767Simp
27131767Simp#include <sys/cdefs.h>
28131767Simp__FBSDID("$FreeBSD$");
29131767Simp
30131767Simp#include <sys/param.h>
31131767Simp#include <sys/bio.h>
32131767Simp#include <sys/bus.h>
33131767Simp#include <sys/kernel.h>
34134081Sphk#include <sys/lock.h>
35131767Simp#include <sys/module.h>
36134081Sphk#include <sys/mutex.h>
37131767Simp#include <sys/rman.h>
38131767Simp#include <sys/systm.h>
39132137Simp#include <machine/bus.h>
40131767Simp
41131767Simp#include <dev/fdc/fdcvar.h>
42132137Simp#include <dev/pccard/pccardvar.h>
43132137Simp#include "pccarddevs.h"
44131767Simp
45131767Simpstatic int fdc_pccard_probe(device_t);
46132137Simpstatic int fdc_pccard_attach(device_t);
47131767Simp
48132137Simpstatic const struct pccard_product fdc_pccard_products[] = {
49147580Simp	PCMCIA_CARD(YEDATA, EXTERNAL_FDD),
50132137Simp};
51132137Simp
52131767Simpstatic int
53132137Simpfdc_pccard_alloc_resources(device_t dev, struct fdc_data *fdc)
54132137Simp{
55140469Simp	struct resource *res;
56140469Simp	int rid, i;
57140469Simp
58140469Simp	rid = 0;
59140469Simp	res = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0ul, ~0ul, 1,
60140469Simp	    RF_ACTIVE);
61140469Simp	if (res == NULL) {
62132137Simp		device_printf(dev, "cannot alloc I/O port range\n");
63132137Simp		return (ENXIO);
64132137Simp	}
65140469Simp	for (i = 0; i < FDC_MAXREG; i++) {
66140469Simp		fdc->resio[i] = res;
67140469Simp		fdc->ridio[i] = rid;
68140469Simp		fdc->ioff[i] = i;
69140469Simp		fdc->ioh[i] = rman_get_bushandle(res);
70140469Simp	}
71140469Simp	fdc->iot = rman_get_bustag(res);
72132137Simp
73132137Simp	fdc->rid_irq = 0;
74132137Simp	fdc->res_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &fdc->rid_irq,
75132137Simp	    RF_ACTIVE | RF_SHAREABLE);
76132137Simp	if (fdc->res_irq == NULL) {
77132137Simp		device_printf(dev, "cannot reserve interrupt line\n");
78132137Simp		return (ENXIO);
79132137Simp	}
80132137Simp	return (0);
81132137Simp}
82132137Simp
83132137Simpstatic int
84131767Simpfdc_pccard_probe(device_t dev)
85131767Simp{
86185235Simp	if (pccard_product_lookup(dev, fdc_pccard_products,
87185235Simp	    sizeof(fdc_pccard_products[0]), NULL) != NULL) {
88135212Simp		device_set_desc(dev, "PC Card Floppy");
89132137Simp		return (0);
90132137Simp	}
91132137Simp	return (ENXIO);
92132137Simp}
93132137Simp
94132137Simpstatic int
95132137Simpfdc_pccard_attach(device_t dev)
96132137Simp{
97132216Snjl	int error;
98131767Simp	struct	fdc_data *fdc;
99135212Simp	device_t child;
100131767Simp
101131767Simp	fdc = device_get_softc(dev);
102140469Simp	fdc->flags = FDC_NODMA | FDC_NOFAST;
103131767Simp	fdc->fdct = FDC_NE765;
104132216Snjl	error = fdc_pccard_alloc_resources(dev, fdc);
105134081Sphk	if (error == 0)
106134081Sphk		error = fdc_attach(dev);
107135212Simp	if (error == 0) {
108135212Simp		child = fdc_add_child(dev, "fd", -1);
109135212Simp		device_set_flags(child, 0x24);
110135212Simp		error = bus_generic_attach(dev);
111135212Simp	}
112132216Snjl	if (error)
113132216Snjl		fdc_release_resources(fdc);
114132216Snjl	return (error);
115131767Simp}
116131767Simp
117131767Simpstatic device_method_t fdc_pccard_methods[] = {
118131767Simp	/* Device interface */
119131767Simp	DEVMETHOD(device_probe,		fdc_pccard_probe),
120132137Simp	DEVMETHOD(device_attach,	fdc_pccard_attach),
121131767Simp	DEVMETHOD(device_detach,	fdc_detach),
122131767Simp	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
123131767Simp	DEVMETHOD(device_suspend,	bus_generic_suspend),
124131767Simp	DEVMETHOD(device_resume,	bus_generic_resume),
125131767Simp
126131767Simp	/* Bus interface */
127131767Simp	DEVMETHOD(bus_print_child,	fdc_print_child),
128131767Simp	DEVMETHOD(bus_read_ivar,	fdc_read_ivar),
129132048Snjl	DEVMETHOD(bus_write_ivar,       fdc_write_ivar),
130131767Simp	/* Our children never use any other bus interface methods. */
131131767Simp
132131767Simp	{ 0, 0 }
133131767Simp};
134131767Simp
135131767Simpstatic driver_t fdc_pccard_driver = {
136131767Simp	"fdc",
137131767Simp	fdc_pccard_methods,
138131767Simp	sizeof(struct fdc_data)
139131767Simp};
140131767Simp
141131767SimpDRIVER_MODULE(fdc, pccard, fdc_pccard_driver, fdc_devclass, 0, 0);
142