1 /* 2 * Copyright 2002 by Peter Grehan. All rights reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 1. Redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer. 9 * 2. Redistributions in binary form must reproduce the above copyright 10 * notice, this list of conditions and the following disclaimer in the 11 * documentation and/or other materials provided with the distribution. 12 * 3. The name of the author may not be used to endorse or promote products 13 * derived from this software without specific prior written permission. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 20 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 21 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 22 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 23 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 * SUCH DAMAGE. 26 * 27 * $FreeBSD$ 28 */ 29 30 /* 31 * Mac-io ATA controller 32 */ 33 #include "opt_ata.h" 34 #include <sys/param.h> 35 #include <sys/systm.h> 36 #include <sys/kernel.h> 37 #include <sys/module.h> 38 #include <sys/bus.h> 39 #include <sys/malloc.h> 40 #include <machine/stdarg.h> 41 #include <machine/resource.h> 42 #include <machine/bus.h> 43 #include <sys/rman.h> 44 #include <dev/ata/ata-all.h> 45 46 #include <dev/ofw/openfirm.h> 47 #include <powerpc/powermac/maciovar.h> 48 49 /* 50 * Define the macio ata bus attachment. This creates a pseudo-bus that 51 * the ATA device can be attached to 52 */ 53 static int ata_macio_attach(device_t dev); 54 static int ata_macio_probe(device_t dev); 55 static int ata_macio_print_child(device_t dev, device_t child); 56 struct resource *ata_macio_alloc_resource(device_t, device_t, int, int *, 57 u_long, u_long, u_long, u_int); 58 static int ata_macio_release_resource(device_t, device_t, int, int, 59 struct resource *); 60 61 static device_method_t ata_macio_methods[] = { 62 /* Device interface */ 63 DEVMETHOD(device_probe, ata_macio_probe), 64 DEVMETHOD(device_attach, ata_macio_attach), 65 DEVMETHOD(device_shutdown, bus_generic_shutdown), 66 DEVMETHOD(device_suspend, bus_generic_suspend), 67 DEVMETHOD(device_resume, bus_generic_resume), 68 69 /* Bus methods */ 70 DEVMETHOD(bus_print_child, ata_macio_print_child), 71 DEVMETHOD(bus_alloc_resource, ata_macio_alloc_resource), 72 DEVMETHOD(bus_release_resource, ata_macio_release_resource), 73 DEVMETHOD(bus_activate_resource, bus_generic_activate_resource), 74 DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource), 75 DEVMETHOD(bus_setup_intr, bus_generic_setup_intr), 76 DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr), 77 78 { 0, 0 } 79 }; 80 81 static driver_t ata_macio_driver = { 82 "atamacio", 83 ata_macio_methods, 84 0, 85 }; 86 87 static devclass_t ata_macio_devclass; 88 89 DRIVER_MODULE(atamacio, macio, ata_macio_driver, ata_macio_devclass, 0, 0); 90 91 92 static int 93 ata_macio_probe(device_t dev) 94 { 95 char *type = macio_get_devtype(dev); 96 97 if (strcmp(type, "ata") != 0) 98 return (ENXIO); 99 100 /* Print keylargo/pangea ??? */ 101 device_set_desc(dev, "Mac-IO ATA Controller"); 102 return (0); 103 } 104 105 106 static int 107 ata_macio_attach(device_t dev) 108 { 109 /* 110 * Add a single child per controller. Should be able 111 * to add two 112 */ 113 device_add_child(dev, "ata", 114 devclass_find_free_unit(ata_devclass, 0)); 115 116 return (bus_generic_attach(dev)); 117 } 118 119 120 static int 121 ata_macio_print_child(device_t dev, device_t child) 122 { 123 int retval = 0; 124 125 retval += bus_print_child_header(dev, child); 126 retval += bus_print_child_footer(dev, child); 127 128 return (retval); 129 } 130 131 /* offset to control registers from base */ 132 #define ATA_MACIO_ALTOFFSET 0x160 133 134 struct resource * 135 ata_macio_alloc_resource(device_t dev, device_t child, int type, int *rid, 136 u_long start, u_long end, u_long count, u_int flags) 137 { 138 struct resource *res = NULL; 139 int myrid; 140 u_int *ofw_regs; 141 142 ofw_regs = macio_get_regs(dev); 143 144 /* 145 * The offset for the register bank is in the first ofw register, 146 * with the base address coming from the parent macio bus (but 147 * accessible via the ata-macio child) 148 */ 149 if (type == SYS_RES_IOPORT) { 150 switch (*rid) { 151 case ATA_IOADDR_RID: 152 myrid = 0; 153 start = ofw_regs[0]; 154 end = start + (ATA_IOSIZE << 4) - 1; 155 count = ATA_IOSIZE << 4; 156 res = BUS_ALLOC_RESOURCE(device_get_parent(dev), child, 157 SYS_RES_IOPORT, &myrid, 158 start, end, count, 159 PPC_BUS_SPARSE4 | flags); 160 break; 161 162 case ATA_ALTADDR_RID: 163 myrid = 0; 164 start = ofw_regs[0] + ATA_MACIO_ALTOFFSET; 165 end = start + (ATA_ALTIOSIZE << 4) - 1; 166 count = ATA_ALTIOSIZE << 4; 167 res = BUS_ALLOC_RESOURCE(device_get_parent(dev), child, 168 SYS_RES_IOPORT, &myrid, 169 start, end, count, 170 PPC_BUS_SPARSE4 | flags); 171 break; 172 173 case ATA_BMADDR_RID: 174 /* looks difficult to support DBDMA in FreeBSD... */ 175 break; 176 } 177 return (res); 178 179 } else if (type == SYS_RES_IRQ && *rid == ATA_IRQ_RID) { 180 /* 181 * Pass this on to the parent, using the IRQ from the 182 * ATA pseudo-bus resource 183 */ 184 myrid = 0; 185 res = bus_generic_rl_alloc_resource(device_get_parent(dev), 186 dev, SYS_RES_IRQ, &myrid, 0, ~0, 1, flags); 187 return (res); 188 189 } else { 190 return (NULL); 191 } 192 } 193 194 195 static int 196 ata_macio_release_resource(device_t dev, device_t child, int type, int rid, 197 struct resource *r) 198 { 199 printf("macio: release resource\n"); 200 return (0); 201 } 202 203 204 /* 205 * Define the actual ATA device. This is a sub-bus to the ata-macio layer 206 * to allow the higher layer bus to massage the resource allocation. 207 */ 208 209 static int ata_macio_sub_probe(device_t dev); 210 211 static device_method_t ata_macio_sub_methods[] = { 212 /* Device interface */ 213 DEVMETHOD(device_probe, ata_macio_sub_probe), 214 DEVMETHOD(device_attach, ata_attach), 215 DEVMETHOD(device_detach, ata_detach), 216 DEVMETHOD(device_resume, ata_resume), 217 218 { 0, 0 } 219 }; 220 221 static driver_t ata_macio_sub_driver = { 222 "ata", 223 ata_macio_sub_methods, 224 sizeof(struct ata_channel), 225 }; 226 227 DRIVER_MODULE(ata, atamacio, ata_macio_sub_driver, ata_devclass, 0, 0); 228 229 static int 230 ata_macio_intrnoop(struct ata_channel *ch) 231 { 232 233 return (1); 234 } 235 236 static void 237 ata_macio_locknoop(struct ata_channel *ch, int type) 238 { 239 /* XXX SMP ? */ 240 } 241 242 static int 243 ata_macio_sub_probe(device_t dev) 244 { 245 struct ata_channel *ch = device_get_softc(dev); 246 247 ch->unit = 0; 248 ch->flags = ATA_USE_16BIT; 249 ch->intr_func = ata_macio_intrnoop; 250 ch->lock_func = ata_macio_locknoop; 251 252 return ata_probe(dev); 253 } 254