ata-cbus.c revision 124403
1/*-
2 * Copyright (c) 2002 - 2004 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 124403 2004-01-11 22:08:34Z 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/sema.h>
40#include <sys/taskqueue.h>
41#include <machine/resource.h>
42#include <machine/bus.h>
43#include <sys/rman.h>
44#include <isa/isavar.h>
45#include <dev/ata/ata-all.h>
46
47/* local vars */
48struct ata_cbus_controller {
49    struct resource *io;
50    struct resource *altio;
51    struct resource *bankio;
52    struct resource *irq;
53    void *ih;
54    void (*setmode)(struct ata_device *, int);
55    void (*locking)(struct ata_channel *, int);
56    int current_bank;
57    struct {
58	void (*function)(void *);
59	void *argument;
60    } interrupt[2];
61};
62
63/* local prototypes */
64static void ata_cbus_intr(void *);
65static void ata_cbus_banking(struct ata_channel *, int);
66static void ata_cbus_setmode(struct ata_device *, int);
67
68static int
69ata_cbus_probe(device_t dev)
70{
71    struct resource *io;
72    int rid;
73    u_long tmp;
74
75    /* dont probe PnP devices */
76    if (isa_get_vendorid(dev))
77	return (ENXIO);
78
79    /* allocate the ioport range */
80    rid = ATA_IOADDR_RID;
81    io = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0,
82			    ATA_PC98_IOSIZE, RF_ACTIVE);
83    if (!io)
84	return ENOMEM;
85
86    /* calculate & set the altport range */
87    rid = ATA_PC98_ALTADDR_RID;
88    if (bus_get_resource(dev, SYS_RES_IOPORT, rid, &tmp, &tmp)) {
89	bus_set_resource(dev, SYS_RES_IOPORT, rid,
90			 rman_get_start(io)+ATA_PC98_ALTOFFSET, ATA_ALTIOSIZE);
91    }
92
93    /* calculate & set the bank range */
94    rid = ATA_PC98_BANKADDR_RID;
95    if (bus_get_resource(dev, SYS_RES_IOPORT, rid, &tmp, &tmp)) {
96	bus_set_resource(dev, SYS_RES_IOPORT, rid,
97			 ATA_PC98_BANK, ATA_PC98_BANKIOSIZE);
98    }
99
100    bus_release_resource(dev, SYS_RES_IOPORT, ATA_IOADDR_RID, io);
101    return 0;
102}
103
104static int
105ata_cbus_attach(device_t dev)
106{
107    struct ata_cbus_controller *ctlr = device_get_softc(dev);
108    int rid;
109
110    /* allocate resources */
111    rid = ATA_IOADDR_RID;
112    ctlr->io = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0,
113				  ATA_PC98_IOSIZE, RF_ACTIVE);
114    if (!ctlr->io)
115       return ENOMEM;
116
117    rid = ATA_PC98_ALTADDR_RID;
118    ctlr->altio =
119	bus_alloc_resource(dev, SYS_RES_IOPORT, &rid,
120			   rman_get_start(ctlr->io) + ATA_PC98_ALTOFFSET, ~0,
121			   ATA_ALTIOSIZE, RF_ACTIVE);
122    if (!ctlr->altio) {
123	bus_release_resource(dev, SYS_RES_IOPORT, ATA_IOADDR_RID, ctlr->io);
124	return ENOMEM;
125    }
126
127    rid = ATA_PC98_BANKADDR_RID;
128    ctlr->bankio = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid,
129				      ATA_PC98_BANK, ~0,
130				      ATA_PC98_BANKIOSIZE, RF_ACTIVE);
131    if (!ctlr->bankio) {
132	bus_release_resource(dev, SYS_RES_IOPORT, ATA_IOADDR_RID, ctlr->io);
133	bus_release_resource(dev, SYS_RES_IOPORT, ATA_ALTADDR_RID, ctlr->altio);
134	return ENOMEM;
135    }
136
137    rid = ATA_IRQ_RID;
138    if (!(ctlr->irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid,
139					 0, ~0, 1, RF_ACTIVE | RF_SHAREABLE))) {
140	device_printf(dev, "unable to alloc interrupt\n");
141	bus_release_resource(dev, SYS_RES_IOPORT, ATA_IOADDR_RID, ctlr->io);
142	bus_release_resource(dev, SYS_RES_IOPORT, ATA_ALTADDR_RID, ctlr->altio);
143	bus_release_resource(dev, SYS_RES_IOPORT,
144			     ATA_PC98_BANKADDR_RID, ctlr->bankio);
145	return ENXIO;
146    }
147
148    if ((bus_setup_intr(dev, ctlr->irq, ATA_INTR_FLAGS,
149			ata_cbus_intr, ctlr, &ctlr->ih))) {
150	device_printf(dev, "unable to setup interrupt\n");
151	bus_release_resource(dev, SYS_RES_IOPORT, ATA_IOADDR_RID, ctlr->io);
152	bus_release_resource(dev, SYS_RES_IOPORT, ATA_ALTADDR_RID, ctlr->altio);
153	bus_release_resource(dev, SYS_RES_IOPORT,
154			     ATA_PC98_BANKADDR_RID, ctlr->bankio);
155	bus_release_resource(dev, SYS_RES_IOPORT, ATA_IRQ_RID, ctlr->irq);
156	return ENXIO;
157    }
158
159    ctlr->locking = ata_cbus_banking;
160    ctlr->current_bank = -1;
161    ctlr->setmode = ata_cbus_setmode;;
162
163    if (!device_add_child(dev, "ata", 0))
164	return ENOMEM;
165    if (!device_add_child(dev, "ata", 1))
166	return ENOMEM;
167
168    return bus_generic_attach(dev);
169}
170
171static struct resource *
172ata_cbus_alloc_resource(device_t dev, device_t child, int type, int *rid,
173			u_long start, u_long end, u_long count, u_int flags)
174{
175    struct ata_cbus_controller *ctlr = device_get_softc(dev);
176
177    if (type == SYS_RES_IOPORT) {
178	switch (*rid) {
179	case ATA_IOADDR_RID:
180	    return ctlr->io;
181	case ATA_ALTADDR_RID:
182	    return ctlr->altio;
183	}
184    }
185    if (type == SYS_RES_IRQ)
186	return ctlr->irq;
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