ata-cbus.c revision 119418
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 119418 2003-08-24 17:55:58Z obrien $");
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    }
187    return 0;
188}
189
190static int
191ata_cbus_setup_intr(device_t dev, device_t child, struct resource *irq,
192	       int flags, driver_intr_t *intr, void *arg,
193	       void **cookiep)
194{
195    struct ata_cbus_controller *controller = device_get_softc(dev);
196    int unit = ((struct ata_channel *)device_get_softc(child))->unit;
197
198    controller->interrupt[unit].function = intr;
199    controller->interrupt[unit].argument = arg;
200    *cookiep = controller;
201
202    return 0;
203}
204
205static int
206ata_cbus_print_child(device_t dev, device_t child)
207{
208    struct ata_channel *ch = device_get_softc(child);
209    int retval = 0;
210
211    retval += bus_print_child_header(dev, child);
212    retval += printf(" at bank %d", ch->unit);
213    retval += bus_print_child_footer(dev, child);
214    return retval;
215}
216
217static void
218ata_cbus_intr(void *data)
219{
220    struct ata_cbus_controller *ctlr = data;
221
222    if (ctlr->current_bank != -1 &&
223	ctlr->interrupt[ctlr->current_bank].argument)
224	ctlr->interrupt[ctlr->current_bank].
225	    function(ctlr->interrupt[ctlr->current_bank].argument);
226}
227
228static void
229ata_cbus_banking(struct ata_channel *ch, int flags)
230{
231    struct ata_cbus_controller *ctlr =
232	device_get_softc(device_get_parent(ch->dev));
233
234    switch (flags) {
235    case ATA_LF_LOCK:
236	if (ctlr->current_bank == ch->unit)
237	    break;
238	while (!atomic_cmpset_acq_int(&ctlr->current_bank, -1, ch->unit))
239	    tsleep((caddr_t)ch->locking, PRIBIO, "atabnk", 1);
240	ATA_OUTB(ctlr->bankio, 0, ch->unit);
241	break;
242
243    case ATA_LF_UNLOCK:
244	if (ctlr->current_bank == -1 || ctlr->current_bank != ch->unit)
245	    break;
246	atomic_store_rel_int(&ctlr->current_bank, -1);
247	break;
248    }
249    return;
250}
251
252static void
253ata_cbus_setmode(struct ata_device *atadev, int mode)
254{
255    atadev->mode = ata_limit_mode(atadev, mode, ATA_PIO_MAX);
256}
257
258static device_method_t ata_cbus_methods[] = {
259    /* device_interface */
260    DEVMETHOD(device_probe,	ata_cbus_probe),
261    DEVMETHOD(device_attach,	ata_cbus_attach),
262
263    /* bus methods */
264    DEVMETHOD(bus_alloc_resource,	ata_cbus_alloc_resource),
265    DEVMETHOD(bus_setup_intr,		ata_cbus_setup_intr),
266    DEVMETHOD(bus_print_child,		ata_cbus_print_child),
267    { 0, 0 }
268};
269
270static driver_t ata_cbus_driver = {
271    "atacbus",
272    ata_cbus_methods,
273    sizeof(struct ata_cbus_controller),
274};
275
276static devclass_t ata_cbus_devclass;
277
278DRIVER_MODULE(atacbus, isa, ata_cbus_driver, ata_cbus_devclass, 0, 0);
279
280static int
281ata_cbussub_probe(device_t dev)
282{
283    struct ata_cbus_controller *ctlr = device_get_softc(device_get_parent(dev));
284    struct ata_channel *ch = device_get_softc(dev);
285    device_t *children;
286    int count, i;
287
288    /* find channel number on this controller */
289    device_get_children(device_get_parent(dev), &children, &count);
290    for (i = 0; i < count; i++) {
291	if (children[i] == dev)
292	    ch->unit = i;
293    }
294    free(children, M_TEMP);
295
296    /* setup the resource vectors */
297    for (i = ATA_DATA; i <= ATA_STATUS; i ++) {
298	ch->r_io[i].res = ctlr->io;
299	ch->r_io[i].offset = i << 1;
300    }
301    ch->r_io[ATA_ALTSTAT].res = ctlr->altio;
302    ch->r_io[ATA_ALTSTAT].offset = 0;
303
304    /* initialize softc for this channel */
305    ch->flags |= ATA_USE_16BIT | ATA_USE_PC98GEOM;
306    ch->locking = ctlr->locking;
307    ch->device[MASTER].setmode = ctlr->setmode;
308    ch->device[SLAVE].setmode = ctlr->setmode;
309    return ata_probe(dev);
310}
311
312static device_method_t ata_cbussub_methods[] = {
313    /* device interface */
314    DEVMETHOD(device_probe,	ata_cbussub_probe),
315    DEVMETHOD(device_attach,	ata_attach),
316    DEVMETHOD(device_detach,	ata_detach),
317    DEVMETHOD(device_resume,	ata_resume),
318    { 0, 0 }
319};
320
321static driver_t ata_cbussub_driver = {
322    "ata",
323    ata_cbussub_methods,
324    sizeof(struct ata_channel),
325};
326
327DRIVER_MODULE(ata, atacbus, ata_cbussub_driver, ata_devclass, 0, 0);
328