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/disk.h> 38 #include <sys/module.h> 39 #include <sys/bus.h> 40 #include <sys/bio.h> 41 #include <sys/malloc.h> 42 #include <sys/devicestat.h> 43 #include <sys/sysctl.h> 44 #include <machine/stdarg.h> 45 #include <machine/resource.h> 46 #include <machine/bus.h> 47 #include <sys/rman.h> 48 #include <dev/ata/ata-all.h> 49 50 #include <dev/ofw/openfirm.h> 51 #include <powerpc/powermac/maciovar.h> 52 53 /* 54 * Define the macio ata bus attachment. This creates a pseudo-bus that 55 * the ATA device can be attached to 56 */ 57 static int ata_macio_attach(device_t dev); 58 static int ata_macio_probe(device_t dev); 59 static int ata_macio_print_child(device_t dev, device_t child); 60 struct resource *ata_macio_alloc_resource(device_t, device_t, int, int *, 61 u_long, u_long, u_long, u_int); 62 static int ata_macio_release_resource(device_t, device_t, int, int, 63 struct resource *); 64 65 static device_method_t ata_macio_methods[] = { 66 /* Device interface */ 67 DEVMETHOD(device_probe, ata_macio_probe), 68 DEVMETHOD(device_attach, ata_macio_attach), 69 DEVMETHOD(device_shutdown, bus_generic_shutdown), 70 DEVMETHOD(device_suspend, bus_generic_suspend), 71 DEVMETHOD(device_resume, bus_generic_resume), 72 73 /* Bus methods */ 74 DEVMETHOD(bus_print_child, ata_macio_print_child), 75 DEVMETHOD(bus_alloc_resource, ata_macio_alloc_resource), 76 DEVMETHOD(bus_release_resource, ata_macio_release_resource), 77 DEVMETHOD(bus_activate_resource, bus_generic_activate_resource), 78 DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource), 79 DEVMETHOD(bus_setup_intr, bus_generic_setup_intr), 80 DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr), 81 82 { 0, 0 } 83 }; 84 85 static driver_t ata_macio_driver = { 86 "atamacio", 87 ata_macio_methods, 88 0, 89 }; 90 91 static devclass_t ata_macio_devclass; 92 93 DRIVER_MODULE(atamacio, macio, ata_macio_driver, ata_macio_devclass, 0, 0); 94 95 96 static int 97 ata_macio_probe(device_t dev) 98 { 99 char *type = macio_get_devtype(dev); 100 101 if (strcmp(type, "ata") != 0) 102 return (ENXIO); 103 104 /* Print keylargo/pangea ??? */ 105 device_set_desc(dev, "Mac-IO ATA Controller"); 106 return (0); 107 } 108 109 110 static int 111 ata_macio_attach(device_t dev) 112 { 113 /* 114 * Add a single child per controller. Should be able 115 * to add two 116 */ 117 device_add_child(dev, "ata", 118 devclass_find_free_unit(ata_devclass, 0)); 119 120 return (bus_generic_attach(dev)); 121 } 122 123 124 static int 125 ata_macio_print_child(device_t dev, device_t child) 126 { 127 int retval = 0; 128 129 retval += bus_print_child_header(dev, child); 130 retval += bus_print_child_footer(dev, child); 131 132 return (retval); 133 } 134 135 /* offset to control registers from base */ 136 #define ATA_MACIO_ALTOFFSET 0x160 137 138 struct resource * 139 ata_macio_alloc_resource(device_t dev, device_t child, int type, int *rid, 140 u_long start, u_long end, u_long count, u_int flags) 141 { 142 struct resource *res = NULL; 143 int myrid; 144 u_int *ofw_regs; 145 146 ofw_regs = macio_get_regs(dev); 147 148 /* 149 * The offset for the register bank is in the first ofw register, 150 * with the base address coming from the parent macio bus (but 151 * accessible via the ata-macio child) 152 */ 153 if (type == SYS_RES_IOPORT) { 154 switch (*rid) { 155 case ATA_IOADDR_RID: 156 myrid = 0; 157 start = ofw_regs[0]; 158 end = start + (ATA_IOSIZE << 4) - 1; 159 count = ATA_IOSIZE << 4; 160 res = BUS_ALLOC_RESOURCE(device_get_parent(dev), child, 161 SYS_RES_IOPORT, &myrid, 162 start, end, count, 163 PPC_BUS_SPARSE4 | flags); 164 break; 165 166 case ATA_ALTADDR_RID: 167 myrid = 0; 168 start = ofw_regs[0] + ATA_MACIO_ALTOFFSET; 169 end = start + (ATA_ALTIOSIZE << 4) - 1; 170 count = ATA_ALTIOSIZE << 4; 171 res = BUS_ALLOC_RESOURCE(device_get_parent(dev), child, 172 SYS_RES_IOPORT, &myrid, 173 start, end, count, 174 PPC_BUS_SPARSE4 | flags); 175 break; 176 177 case ATA_BMADDR_RID: 178 /* looks difficult to support DBDMA in FreeBSD... */ 179 break; 180 } 181 return (res); 182 183 } else if (type == SYS_RES_IRQ && *rid == ATA_IRQ_RID) { 184 /* 185 * Pass this on to the parent, using the IRQ from the 186 * ATA pseudo-bus resource 187 */ 188 res = bus_generic_rl_alloc_resource(device_get_parent(dev), 189 dev, SYS_RES_IRQ, 0, 0, ~0, 1, flags); 190 return (res); 191 192 } else { 193 return (NULL); 194 } 195 } 196 197 198 static int 199 ata_macio_release_resource(device_t dev, device_t child, int type, int rid, 200 struct resource *r) 201 { 202 printf("macio: release resource\n"); 203 return (0); 204 } 205 206 207 /* 208 * Define the actual ATA device. This is a sub-bus to the ata-macio layer 209 * to allow the higher layer bus to massage the resource allocation. 210 */ 211 212 static int ata_macio_sub_probe(device_t dev); 213 214 static device_method_t ata_macio_sub_methods[] = { 215 /* Device interface */ 216 DEVMETHOD(device_probe, ata_macio_sub_probe), 217 DEVMETHOD(device_attach, ata_attach), 218 DEVMETHOD(device_detach, ata_detach), 219 DEVMETHOD(device_resume, ata_resume), 220 221 { 0, 0 } 222 }; 223 224 static driver_t ata_macio_sub_driver = { 225 "ata", 226 ata_macio_sub_methods, 227 sizeof(struct ata_channel), 228 }; 229 230 DRIVER_MODULE(ata, atamacio, ata_macio_sub_driver, ata_devclass, 0, 0); 231 232 static int 233 ata_macio_sub_probe(device_t dev) 234 { 235 struct ata_channel *ch = device_get_softc(dev); 236 237 ch->unit = 0; 238 ch->flags = ATA_USE_16BIT; 239 240 return ata_probe(dev); 241 } 242