ata-cbus.c revision 107562
1/*-
2 * Copyright (c) 2002 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 * $FreeBSD: head/sys/dev/ata/ata-cbus.c 107562 2002-12-03 20:20:44Z sos $
29 */
30
31#include "opt_ata.h"
32#include <sys/param.h>
33#include <sys/systm.h>
34#include <sys/kernel.h>
35#include <sys/bus.h>
36#include <sys/malloc.h>
37#include <machine/resource.h>
38#include <machine/bus.h>
39#include <sys/rman.h>
40#include <isa/isavar.h>
41#include <dev/ata/ata-all.h>
42
43/* local vars */
44static bus_addr_t ata_pc98_ports[] = {
45    0x0, 0x2, 0x4, 0x6, 0x8, 0xa, 0xc, 0xe
46};
47
48struct ata_cbus_controller {
49    struct resource *io;
50    struct resource *altio;
51    struct resource *bankio;
52    struct resource *irq;
53    int current_bank;
54};
55
56static int
57ata_cbus_probe(device_t dev)
58{
59    struct resource *io;
60    int rid;
61    u_long tmp;
62
63    /* allocate the ioport range */
64    rid = ATA_IOADDR_RID;
65    io = isa_alloc_resourcev(dev, SYS_RES_IOPORT, &rid, ata_pc98_ports,
66			     ATA_IOSIZE, RF_ACTIVE);
67    if (!io)
68	return ENOMEM;
69    isa_load_resourcev(io, ata_pc98_ports, ATA_IOSIZE);
70
71    /* calculate & set the altport range */
72    rid = ATA_PC98_ALTADDR_RID;
73    if (bus_get_resource(dev, SYS_RES_IOPORT, rid, &tmp, &tmp)) {
74	bus_set_resource(dev, SYS_RES_IOPORT, rid,
75			 rman_get_start(io)+ATA_PC98_ALTOFFSET, ATA_ALTIOSIZE);
76    }
77
78    /* calculate & set the bank range */
79    rid = ATA_PC98_BANKADDR_RID;
80    if (bus_get_resource(dev, SYS_RES_IOPORT, rid, &tmp, &tmp)) {
81	bus_set_resource(dev, SYS_RES_IOPORT, rid,
82			 ATA_PC98_BANK, ATA_PC98_BANKIOSIZE);
83    }
84
85    bus_release_resource(dev, SYS_RES_IOPORT, ATA_IOADDR_RID, io);
86    return 0;
87}
88
89static int
90ata_cbus_attach(device_t dev)
91{
92    struct ata_cbus_controller *scp = device_get_softc(dev);
93    int rid;
94
95    /* allocate resources */
96    rid = ATA_IOADDR_RID;
97    scp->io = isa_alloc_resourcev(dev, SYS_RES_IOPORT, &rid, ata_pc98_ports,
98				  ATA_IOSIZE, RF_ACTIVE);
99    if (!scp->io)
100       return ENOMEM;
101
102    isa_load_resourcev(scp->io, ata_pc98_ports, ATA_IOSIZE);
103
104    rid = ATA_PC98_ALTADDR_RID;
105    scp->altio = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid,
106				    rman_get_start(scp->io)+ATA_PC98_ALTOFFSET,
107				    ~0, ATA_ALTIOSIZE, RF_ACTIVE);
108    if (!scp->altio)
109	return ENOMEM;
110
111    rid = ATA_PC98_BANKADDR_RID;
112    scp->bankio = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid,
113				     ATA_PC98_BANK, ~0,
114				     ATA_PC98_BANKIOSIZE, RF_ACTIVE);
115    if (!scp->bankio)
116	return ENOMEM;
117
118    rid = 0;
119    scp->irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid,
120				  0, ~0, 1, RF_ACTIVE | RF_SHAREABLE);
121
122    scp->current_bank = -1;
123    if (!device_add_child(dev, "ata", 0))
124	return ENOMEM;
125    if (!device_add_child(dev, "ata", 1))
126	return ENOMEM;
127
128    return bus_generic_attach(dev);
129}
130
131static struct resource *
132ata_cbus_alloc_resource(device_t dev, device_t child, int type, int *rid,
133			u_long start, u_long end, u_long count, u_int flags)
134{
135    struct ata_cbus_controller *scp = device_get_softc(dev);
136
137    if (type == SYS_RES_IOPORT) {
138	switch (*rid) {
139	case ATA_IOADDR_RID:
140	    return scp->io;
141	case ATA_ALTADDR_RID:
142	    return scp->altio;
143	}
144    }
145    if (type == SYS_RES_IRQ) {
146	return scp->irq;
147    }
148    return 0;
149}
150
151static int
152ata_cbus_setup_intr(device_t dev, device_t child, struct resource *irq,
153	       int flags, driver_intr_t *intr, void *arg,
154	       void **cookiep)
155{
156    return BUS_SETUP_INTR(device_get_parent(dev), dev, irq,
157			  flags, intr, arg, cookiep);
158}
159
160static int
161ata_cbus_print_child(device_t dev, device_t child)
162{
163    struct ata_channel *ch = device_get_softc(child);
164    int retval = 0;
165
166    retval += bus_print_child_header(dev, child);
167    retval += printf(" at bank %d", ch->unit);
168    retval += bus_print_child_footer(dev, child);
169    return retval;
170}
171
172static int
173ata_cbus_intr(struct ata_channel *ch)
174{
175    struct ata_cbus_controller *scp =
176	device_get_softc(device_get_parent(ch->dev));
177
178    return (ch->unit == scp->current_bank);
179}
180
181static void
182ata_cbus_banking(struct ata_channel *ch, int flags)
183{
184    struct ata_cbus_controller *scp =
185	device_get_softc(device_get_parent(ch->dev));
186
187    switch (flags) {
188    case ATA_LF_LOCK:
189	if (scp->current_bank == ch->unit)
190	    break;
191	while (!atomic_cmpset_acq_int(&scp->current_bank, -1, ch->unit))
192	    tsleep((caddr_t)ch->lock_func, PRIBIO, "atalck", 1);
193	bus_space_write_1(rman_get_bustag(scp->bankio),
194			  rman_get_bushandle(scp->bankio), 0, ch->unit);
195	break;
196
197    case ATA_LF_UNLOCK:
198	if (scp->current_bank == -1 || scp->current_bank != ch->unit)
199	    break;
200	atomic_store_rel_int(&scp->current_bank, -1);
201	break;
202    }
203    return;
204}
205
206static device_method_t ata_cbus_methods[] = {
207    /* device_interface */
208    DEVMETHOD(device_probe,	ata_cbus_probe),
209    DEVMETHOD(device_attach,	ata_cbus_attach),
210
211    /* bus methods */
212    DEVMETHOD(bus_alloc_resource,	ata_cbus_alloc_resource),
213    DEVMETHOD(bus_setup_intr,		ata_cbus_setup_intr),
214    DEVMETHOD(bus_print_child,		ata_cbus_print_child),
215    { 0, 0 }
216};
217
218static driver_t ata_cbus_driver = {
219    "atacbus",
220    ata_cbus_methods,
221    sizeof(struct ata_cbus_controller),
222};
223
224static devclass_t ata_cbus_devclass;
225
226DRIVER_MODULE(atacbus, isa, ata_cbus_driver, ata_cbus_devclass, 0, 0);
227
228static int
229ata_cbussub_probe(device_t dev)
230{
231    struct ata_channel *ch = device_get_softc(dev);
232    device_t *children;
233    int count, i;
234
235    /* find channel number on this controller */
236    device_get_children(device_get_parent(dev), &children, &count);
237    for (i = 0; i < count; i++) {
238	if (children[i] == dev)
239	    ch->unit = i;
240    }
241    free(children, M_TEMP);
242    ch->flags |= ATA_USE_16BIT;
243    ch->intr_func = ata_cbus_intr;
244    ch->lock_func = ata_cbus_banking;
245    return ata_probe(dev);
246}
247
248static device_method_t ata_cbussub_methods[] = {
249    /* device interface */
250    DEVMETHOD(device_probe,	ata_cbussub_probe),
251    DEVMETHOD(device_attach,	ata_attach),
252    DEVMETHOD(device_detach,	ata_detach),
253    DEVMETHOD(device_resume,	ata_resume),
254    { 0, 0 }
255};
256
257static driver_t ata_cbussub_driver = {
258    "ata",
259    ata_cbussub_methods,
260    sizeof(struct ata_channel),
261};
262
263DRIVER_MODULE(ata, atacbus, ata_cbussub_driver, ata_devclass, 0, 0);
264