ata-cbus.c revision 144707
1/*-
2 * Copyright (c) 2002 - 2005 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 144707 2005-04-06 10:22:56Z 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/module.h>
40#include <sys/conf.h>
41#include <sys/sema.h>
42#include <sys/taskqueue.h>
43#include <vm/uma.h>
44#include <machine/resource.h>
45#include <machine/bus.h>
46#include <sys/rman.h>
47#include <isa/isavar.h>
48#include <dev/ata/ata-all.h>
49#include <ata_if.h>
50
51/* local vars */
52struct ata_cbus_controller {
53    struct resource *io;
54    struct resource *ctlio;
55    struct resource *bankio;
56    struct resource *irq;
57    void *ih;
58    struct mtx bank_mtx;
59    int locked_bank;
60    int restart_bank;
61    int hardware_bank;
62    struct {
63	void (*function)(void *);
64	void *argument;
65    } interrupt[2];
66};
67
68/* local prototypes */
69static void ata_cbus_intr(void *);
70static int ata_cbuschannel_banking(device_t dev, int flags);
71
72static int
73ata_cbus_probe(device_t dev)
74{
75    struct resource *io;
76    int rid;
77    u_long tmp;
78
79    /* dont probe PnP devices */
80    if (isa_get_vendorid(dev))
81	return (ENXIO);
82
83    /* allocate the ioport range */
84    rid = ATA_IOADDR_RID;
85    io = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0,
86			    ATA_PC98_IOSIZE, RF_ACTIVE);
87    if (!io)
88	return ENOMEM;
89
90    /* calculate & set the altport range */
91    rid = ATA_PC98_CTLADDR_RID;
92    if (bus_get_resource(dev, SYS_RES_IOPORT, rid, &tmp, &tmp)) {
93	bus_set_resource(dev, SYS_RES_IOPORT, rid,
94			 rman_get_start(io)+ATA_PC98_CTLOFFSET, ATA_CTLIOSIZE);
95    }
96
97    /* calculate & set the bank range */
98    rid = ATA_PC98_BANKADDR_RID;
99    if (bus_get_resource(dev, SYS_RES_IOPORT, rid, &tmp, &tmp)) {
100	bus_set_resource(dev, SYS_RES_IOPORT, rid,
101			 ATA_PC98_BANK, ATA_PC98_BANKIOSIZE);
102    }
103
104    bus_release_resource(dev, SYS_RES_IOPORT, ATA_IOADDR_RID, io);
105    return 0;
106}
107
108static int
109ata_cbus_attach(device_t dev)
110{
111    struct ata_cbus_controller *ctlr = device_get_softc(dev);
112    int rid;
113
114    /* allocate resources */
115    rid = ATA_IOADDR_RID;
116    ctlr->io = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0,
117				  ATA_PC98_IOSIZE, RF_ACTIVE);
118    if (!ctlr->io)
119       return ENOMEM;
120
121    rid = ATA_PC98_CTLADDR_RID;
122    ctlr->ctlio =
123	bus_alloc_resource(dev, SYS_RES_IOPORT, &rid,
124			   rman_get_start(ctlr->io) + ATA_PC98_CTLOFFSET, ~0,
125			   ATA_CTLIOSIZE, RF_ACTIVE);
126    if (!ctlr->ctlio) {
127	bus_release_resource(dev, SYS_RES_IOPORT, ATA_IOADDR_RID, ctlr->io);
128	return ENOMEM;
129    }
130
131    rid = ATA_PC98_BANKADDR_RID;
132    ctlr->bankio = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid,
133				      ATA_PC98_BANK, ~0,
134				      ATA_PC98_BANKIOSIZE, RF_ACTIVE);
135    if (!ctlr->bankio) {
136	bus_release_resource(dev, SYS_RES_IOPORT, ATA_IOADDR_RID, ctlr->io);
137	bus_release_resource(dev, SYS_RES_IOPORT, ATA_CTLADDR_RID, ctlr->ctlio);
138	return ENOMEM;
139    }
140
141    rid = ATA_IRQ_RID;
142    if (!(ctlr->irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
143					     RF_ACTIVE | RF_SHAREABLE))) {
144	device_printf(dev, "unable to alloc interrupt\n");
145	bus_release_resource(dev, SYS_RES_IOPORT, ATA_IOADDR_RID, ctlr->io);
146	bus_release_resource(dev, SYS_RES_IOPORT, ATA_CTLADDR_RID, ctlr->ctlio);
147	bus_release_resource(dev, SYS_RES_IOPORT,
148			     ATA_PC98_BANKADDR_RID, ctlr->bankio);
149	return ENXIO;
150    }
151
152    if ((bus_setup_intr(dev, ctlr->irq, ATA_INTR_FLAGS,
153			ata_cbus_intr, ctlr, &ctlr->ih))) {
154	device_printf(dev, "unable to setup interrupt\n");
155	bus_release_resource(dev, SYS_RES_IOPORT, ATA_IOADDR_RID, ctlr->io);
156	bus_release_resource(dev, SYS_RES_IOPORT, ATA_CTLADDR_RID, ctlr->ctlio);
157	bus_release_resource(dev, SYS_RES_IOPORT,
158			     ATA_PC98_BANKADDR_RID, ctlr->bankio);
159	bus_release_resource(dev, SYS_RES_IOPORT, ATA_IRQ_RID, ctlr->irq);
160	return ENXIO;
161    }
162
163    mtx_init(&ctlr->bank_mtx, "ATA cbus bank lock", NULL, MTX_DEF);
164    ctlr->hardware_bank = -1;
165    ctlr->locked_bank = -1;
166    ctlr->restart_bank = -1;
167
168    if (!device_add_child(dev, "ata", 0))
169	return ENOMEM;
170    if (!device_add_child(dev, "ata", 1))
171	return ENOMEM;
172
173    return bus_generic_attach(dev);
174}
175
176static struct resource *
177ata_cbus_alloc_resource(device_t dev, device_t child, int type, int *rid,
178			u_long start, u_long end, u_long count, u_int flags)
179{
180    struct ata_cbus_controller *ctlr = device_get_softc(dev);
181
182    if (type == SYS_RES_IOPORT) {
183	switch (*rid) {
184	case ATA_IOADDR_RID:
185	    return ctlr->io;
186	case ATA_CTLADDR_RID:
187	    return ctlr->ctlio;
188	}
189    }
190    if (type == SYS_RES_IRQ)
191	return ctlr->irq;
192    return 0;
193}
194
195static int
196ata_cbus_setup_intr(device_t dev, device_t child, struct resource *irq,
197	       int flags, driver_intr_t *intr, void *arg,
198	       void **cookiep)
199{
200    struct ata_cbus_controller *controller = device_get_softc(dev);
201    int unit = ((struct ata_channel *)device_get_softc(child))->unit;
202
203    controller->interrupt[unit].function = intr;
204    controller->interrupt[unit].argument = arg;
205    *cookiep = controller;
206
207    return 0;
208}
209
210static int
211ata_cbus_print_child(device_t dev, device_t child)
212{
213    struct ata_channel *ch = device_get_softc(child);
214    int retval = 0;
215
216    retval += bus_print_child_header(dev, child);
217    retval += printf(" at bank %d", ch->unit);
218    retval += bus_print_child_footer(dev, child);
219    return retval;
220}
221
222static void
223ata_cbus_intr(void *data)
224{
225    struct ata_cbus_controller *ctlr = data;
226    struct ata_channel *ch;
227    int unit;
228
229    for (unit = 0; unit < 2; unit++) {
230	if (!(ch = ctlr->interrupt[unit].argument))
231	    continue;
232	if (ata_cbuschannel_banking(ch->dev, ATA_LF_WHICH) == unit)
233	    ctlr->interrupt[unit].function(ch);
234    }
235}
236
237static device_method_t ata_cbus_methods[] = {
238    /* device interface */
239    DEVMETHOD(device_probe,             ata_cbus_probe),
240    DEVMETHOD(device_attach,            ata_cbus_attach),
241//  DEVMETHOD(device_detach,            ata_cbus_detach),
242
243    /* bus methods */
244    DEVMETHOD(bus_alloc_resource,       ata_cbus_alloc_resource),
245    DEVMETHOD(bus_setup_intr,           ata_cbus_setup_intr),
246    DEVMETHOD(bus_print_child,          ata_cbus_print_child),
247
248    { 0, 0 }
249};
250
251static driver_t ata_cbus_driver = {
252    "atacbus",
253    ata_cbus_methods,
254    sizeof(struct ata_cbus_controller),
255};
256
257static devclass_t ata_cbus_devclass;
258
259DRIVER_MODULE(atacbus, isa, ata_cbus_driver, ata_cbus_devclass, 0, 0);
260
261static int
262ata_cbuschannel_probe(device_t dev)
263{
264    struct ata_cbus_controller *ctlr = device_get_softc(device_get_parent(dev));
265    struct ata_channel *ch = device_get_softc(dev);
266    device_t *children;
267    int count, i;
268
269    /* find channel number on this controller */
270    device_get_children(device_get_parent(dev), &children, &count);
271    for (i = 0; i < count; i++) {
272	if (children[i] == dev)
273	    ch->unit = i;
274    }
275    free(children, M_TEMP);
276
277    /* setup the resource vectors */
278    for (i = ATA_DATA; i <= ATA_COMMAND; i ++) {
279	ch->r_io[i].res = ctlr->io;
280	ch->r_io[i].offset = i << 1;
281    }
282    ch->r_io[ATA_CONTROL].res = ctlr->ctlio;
283    ch->r_io[ATA_CONTROL].offset = 0;
284    ch->r_io[ATA_IDX_ADDR].res = ctlr->io;
285    ata_default_registers(ch);
286
287    /* initialize softc for this channel */
288    ch->flags |= ATA_USE_16BIT;
289    ata_generic_hw(ch);
290    return ata_probe(dev);
291}
292
293static int
294ata_cbuschannel_banking(device_t dev, int flags)
295{
296    struct ata_cbus_controller *ctlr = device_get_softc(device_get_parent(dev));
297    struct ata_channel *ch = device_get_softc(dev);
298    int res;
299
300    mtx_lock(&ctlr->bank_mtx);
301    switch (flags) {
302    case ATA_LF_LOCK:
303	if (ctlr->locked_bank == -1)
304	    ctlr->locked_bank = ch->unit;
305	if (ctlr->locked_bank == ch->unit) {
306	    ctlr->hardware_bank = ch->unit;
307	    ATA_OUTB(ctlr->bankio, 0, ch->unit);
308	}
309	else
310	    ctlr->restart_bank = ch->unit;
311	break;
312
313    case ATA_LF_UNLOCK:
314	if (ctlr->locked_bank == ch->unit) {
315	    ctlr->locked_bank = -1;
316	    if (ctlr->restart_bank != -1) {
317		if ((ch = ctlr->interrupt[ctlr->restart_bank].argument)) {
318		    ctlr->restart_bank = -1;
319		    mtx_unlock(&ctlr->bank_mtx);
320		    ata_start(ch->dev);
321		    return -1;
322		}
323	    }
324	}
325	break;
326
327    case ATA_LF_WHICH:
328	break;
329    }
330    res = ctlr->locked_bank;
331    mtx_unlock(&ctlr->bank_mtx);
332    return res;
333}
334
335static device_method_t ata_cbuschannel_methods[] = {
336    /* device interface */
337    DEVMETHOD(device_probe,     ata_cbuschannel_probe),
338    DEVMETHOD(device_attach,    ata_attach),
339    DEVMETHOD(device_detach,    ata_detach),
340    DEVMETHOD(device_suspend,   ata_suspend),
341    DEVMETHOD(device_resume,    ata_resume),
342
343    /* ATA methods */
344    DEVMETHOD(ata_locking,      ata_cbuschannel_banking),
345
346    { 0, 0 }
347};
348
349static driver_t ata_cbuschannel_driver = {
350    "ata",
351    ata_cbuschannel_methods,
352    sizeof(struct ata_channel),
353};
354
355DRIVER_MODULE(ata, atacbus, ata_cbuschannel_driver, ata_devclass, 0, 0);
356MODULE_DEPEND(ata, ata, 1, 1, 1);
357