ata-cbus.c revision 119450
1/*-
2 * Copyright (c) 2002, 2003 S�ren Schmidt <sos@FreeBSD.org>
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 the
13 *    documentation and/or other materials provided with the distribution.
14 * 3. The name of the author may not be used to endorse or promote products
15 *    derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#include <sys/cdefs.h>
30__FBSDID("$FreeBSD: head/sys/dev/ata/ata-cbus.c 119450 2003-08-25 09:01:49Z sos $");
31
32#include "opt_ata.h"
33#include <sys/param.h>
34#include <sys/systm.h>
35#include <sys/kernel.h>
36#include <sys/ata.h>
37#include <sys/bus.h>
38#include <sys/malloc.h>
39#include <sys/taskqueue.h>
40#include <machine/resource.h>
41#include <machine/bus.h>
42#include <sys/rman.h>
43#include <isa/isavar.h>
44#include <dev/ata/ata-all.h>
45
46/* local vars */
47struct ata_cbus_controller {
48    struct resource *io;
49    struct resource *altio;
50    struct resource *bankio;
51    struct resource *irq;
52    void *ih;
53    void (*setmode)(struct ata_device *, int);
54    void (*locking)(struct ata_channel *, int);
55    int current_bank;
56    struct {
57	void (*function)(void *);
58	void *argument;
59    } interrupt[2];
60};
61
62/* local prototypes */
63static void ata_cbus_intr(void *);
64static void ata_cbus_banking(struct ata_channel *, int);
65static void ata_cbus_setmode(struct ata_device *, int);
66
67static int
68ata_cbus_probe(device_t dev)
69{
70    struct resource *io;
71    int rid;
72    u_long tmp;
73
74    /* dont probe PnP devices */
75    if (isa_get_vendorid(dev))
76	return (ENXIO);
77
78    /* allocate the ioport range */
79    rid = ATA_IOADDR_RID;
80    io = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0,
81			    ATA_PC98_IOSIZE, RF_ACTIVE);
82    if (!io)
83	return ENOMEM;
84
85    /* calculate & set the altport range */
86    rid = ATA_PC98_ALTADDR_RID;
87    if (bus_get_resource(dev, SYS_RES_IOPORT, rid, &tmp, &tmp)) {
88	bus_set_resource(dev, SYS_RES_IOPORT, rid,
89			 rman_get_start(io)+ATA_PC98_ALTOFFSET, ATA_ALTIOSIZE);
90    }
91
92    /* calculate & set the bank range */
93    rid = ATA_PC98_BANKADDR_RID;
94    if (bus_get_resource(dev, SYS_RES_IOPORT, rid, &tmp, &tmp)) {
95	bus_set_resource(dev, SYS_RES_IOPORT, rid,
96			 ATA_PC98_BANK, ATA_PC98_BANKIOSIZE);
97    }
98
99    bus_release_resource(dev, SYS_RES_IOPORT, ATA_IOADDR_RID, io);
100    return 0;
101}
102
103static int
104ata_cbus_attach(device_t dev)
105{
106    struct ata_cbus_controller *ctlr = device_get_softc(dev);
107    int rid;
108
109    /* allocate resources */
110    rid = ATA_IOADDR_RID;
111    ctlr->io = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0,
112				  ATA_PC98_IOSIZE, RF_ACTIVE);
113    if (!ctlr->io)
114       return ENOMEM;
115
116    rid = ATA_PC98_ALTADDR_RID;
117    ctlr->altio =
118	bus_alloc_resource(dev, SYS_RES_IOPORT, &rid,
119			   rman_get_start(ctlr->io) + ATA_PC98_ALTOFFSET, ~0,
120			   ATA_ALTIOSIZE, RF_ACTIVE);
121    if (!ctlr->altio) {
122	bus_release_resource(dev, SYS_RES_IOPORT, ATA_IOADDR_RID, ctlr->io);
123	return ENOMEM;
124    }
125
126    rid = ATA_PC98_BANKADDR_RID;
127    ctlr->bankio = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid,
128				      ATA_PC98_BANK, ~0,
129				      ATA_PC98_BANKIOSIZE, RF_ACTIVE);
130    if (!ctlr->bankio) {
131	bus_release_resource(dev, SYS_RES_IOPORT, ATA_IOADDR_RID, ctlr->io);
132	bus_release_resource(dev, SYS_RES_IOPORT, ATA_ALTADDR_RID, ctlr->altio);
133	return ENOMEM;
134    }
135
136    rid = ATA_IRQ_RID;
137    if (!(ctlr->irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid,
138					 0, ~0, 1, RF_ACTIVE | RF_SHAREABLE))) {
139	device_printf(dev, "unable to alloc interrupt\n");
140	bus_release_resource(dev, SYS_RES_IOPORT, ATA_IOADDR_RID, ctlr->io);
141	bus_release_resource(dev, SYS_RES_IOPORT, ATA_ALTADDR_RID, ctlr->altio);
142	bus_release_resource(dev, SYS_RES_IOPORT,
143			     ATA_PC98_BANKADDR_RID, ctlr->bankio);
144	return ENXIO;
145    }
146
147    if ((bus_setup_intr(dev, ctlr->irq, INTR_TYPE_BIO | INTR_ENTROPY,
148			ata_cbus_intr, ctlr, &ctlr->ih))) {
149	device_printf(dev, "unable to setup interrupt\n");
150	bus_release_resource(dev, SYS_RES_IOPORT, ATA_IOADDR_RID, ctlr->io);
151	bus_release_resource(dev, SYS_RES_IOPORT, ATA_ALTADDR_RID, ctlr->altio);
152	bus_release_resource(dev, SYS_RES_IOPORT,
153			     ATA_PC98_BANKADDR_RID, ctlr->bankio);
154	bus_release_resource(dev, SYS_RES_IOPORT, ATA_IRQ_RID, ctlr->irq);
155	return ENXIO;
156    }
157
158    ctlr->locking = ata_cbus_banking;
159    ctlr->current_bank = -1;
160    ctlr->setmode = ata_cbus_setmode;;
161
162    if (!device_add_child(dev, "ata", 0))
163	return ENOMEM;
164    if (!device_add_child(dev, "ata", 1))
165	return ENOMEM;
166
167    return bus_generic_attach(dev);
168}
169
170static struct resource *
171ata_cbus_alloc_resource(device_t dev, device_t child, int type, int *rid,
172			u_long start, u_long end, u_long count, u_int flags)
173{
174    struct ata_cbus_controller *ctlr = device_get_softc(dev);
175
176    if (type == SYS_RES_IOPORT) {
177	switch (*rid) {
178	case ATA_IOADDR_RID:
179	    return ctlr->io;
180	case ATA_ALTADDR_RID:
181	    return ctlr->altio;
182	}
183    }
184    if (type == SYS_RES_IRQ)
185	return ctlr->irq;
186    return 0;
187}
188
189static int
190ata_cbus_setup_intr(device_t dev, device_t child, struct resource *irq,
191	       int flags, driver_intr_t *intr, void *arg,
192	       void **cookiep)
193{
194    struct ata_cbus_controller *controller = device_get_softc(dev);
195    int unit = ((struct ata_channel *)device_get_softc(child))->unit;
196
197    controller->interrupt[unit].function = intr;
198    controller->interrupt[unit].argument = arg;
199    *cookiep = controller;
200
201    return 0;
202}
203
204static int
205ata_cbus_print_child(device_t dev, device_t child)
206{
207    struct ata_channel *ch = device_get_softc(child);
208    int retval = 0;
209
210    retval += bus_print_child_header(dev, child);
211    retval += printf(" at bank %d", ch->unit);
212    retval += bus_print_child_footer(dev, child);
213    return retval;
214}
215
216static void
217ata_cbus_intr(void *data)
218{
219    struct ata_cbus_controller *ctlr = data;
220
221    if (ctlr->current_bank != -1 &&
222	ctlr->interrupt[ctlr->current_bank].argument)
223	ctlr->interrupt[ctlr->current_bank].
224	    function(ctlr->interrupt[ctlr->current_bank].argument);
225}
226
227static void
228ata_cbus_banking(struct ata_channel *ch, int flags)
229{
230    struct ata_cbus_controller *ctlr =
231	device_get_softc(device_get_parent(ch->dev));
232
233    switch (flags) {
234    case ATA_LF_LOCK:
235	if (ctlr->current_bank == ch->unit)
236	    break;
237	while (!atomic_cmpset_acq_int(&ctlr->current_bank, -1, ch->unit))
238	    tsleep((caddr_t)ch->locking, PRIBIO, "atabnk", 1);
239	ATA_OUTB(ctlr->bankio, 0, ch->unit);
240	break;
241
242    case ATA_LF_UNLOCK:
243	if (ctlr->current_bank == -1 || ctlr->current_bank != ch->unit)
244	    break;
245	atomic_store_rel_int(&ctlr->current_bank, -1);
246	break;
247    }
248    return;
249}
250
251static void
252ata_cbus_setmode(struct ata_device *atadev, int mode)
253{
254    atadev->mode = ata_limit_mode(atadev, mode, ATA_PIO_MAX);
255}
256
257static device_method_t ata_cbus_methods[] = {
258    /* device_interface */
259    DEVMETHOD(device_probe,	ata_cbus_probe),
260    DEVMETHOD(device_attach,	ata_cbus_attach),
261
262    /* bus methods */
263    DEVMETHOD(bus_alloc_resource,	ata_cbus_alloc_resource),
264    DEVMETHOD(bus_setup_intr,		ata_cbus_setup_intr),
265    DEVMETHOD(bus_print_child,		ata_cbus_print_child),
266    { 0, 0 }
267};
268
269static driver_t ata_cbus_driver = {
270    "atacbus",
271    ata_cbus_methods,
272    sizeof(struct ata_cbus_controller),
273};
274
275static devclass_t ata_cbus_devclass;
276
277DRIVER_MODULE(atacbus, isa, ata_cbus_driver, ata_cbus_devclass, 0, 0);
278
279static int
280ata_cbussub_probe(device_t dev)
281{
282    struct ata_cbus_controller *ctlr = device_get_softc(device_get_parent(dev));
283    struct ata_channel *ch = device_get_softc(dev);
284    device_t *children;
285    int count, i;
286
287    /* find channel number on this controller */
288    device_get_children(device_get_parent(dev), &children, &count);
289    for (i = 0; i < count; i++) {
290	if (children[i] == dev)
291	    ch->unit = i;
292    }
293    free(children, M_TEMP);
294
295    /* setup the resource vectors */
296    for (i = ATA_DATA; i <= ATA_STATUS; i ++) {
297	ch->r_io[i].res = ctlr->io;
298	ch->r_io[i].offset = i << 1;
299    }
300    ch->r_io[ATA_ALTSTAT].res = ctlr->altio;
301    ch->r_io[ATA_ALTSTAT].offset = 0;
302
303    /* initialize softc for this channel */
304    ch->flags |= ATA_USE_16BIT | ATA_USE_PC98GEOM;
305    ch->locking = ctlr->locking;
306    ch->device[MASTER].setmode = ctlr->setmode;
307    ch->device[SLAVE].setmode = ctlr->setmode;
308    return ata_probe(dev);
309}
310
311static device_method_t ata_cbussub_methods[] = {
312    /* device interface */
313    DEVMETHOD(device_probe,	ata_cbussub_probe),
314    DEVMETHOD(device_attach,	ata_attach),
315    DEVMETHOD(device_detach,	ata_detach),
316    DEVMETHOD(device_resume,	ata_resume),
317    { 0, 0 }
318};
319
320static driver_t ata_cbussub_driver = {
321    "ata",
322    ata_cbussub_methods,
323    sizeof(struct ata_channel),
324};
325
326DRIVER_MODULE(ata, atacbus, ata_cbussub_driver, ata_devclass, 0, 0);
327