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