27 28#include <sys/param.h> 29#include <sys/systm.h> 30#include <sys/bus.h> 31#include <sys/kernel.h> 32#include <sys/module.h> 33#include <sys/malloc.h> 34#include <sys/rman.h> 35#include <sys/timetc.h> 36#include <machine/bus.h> 37#include <machine/intr.h> 38 39#include <vm/vm.h> 40#include <vm/pmap.h> 41 42#include <arm/xscale/pxa/pxavar.h> 43#include <arm/xscale/pxa/pxareg.h> 44 45static MALLOC_DEFINE(M_PXASMI, "PXA SMI", 46 "Data for static memory interface devices."); 47 48struct pxa_smi_softc { 49 struct resource *ps_res[1]; 50 struct rman ps_mem; 51 bus_space_tag_t ps_bst; 52 bus_addr_t ps_base; 53}; 54 55struct smi_ivars { 56 struct resource_list smid_resources; 57 bus_addr_t smid_mem; 58}; 59 60static struct resource_spec pxa_smi_spec[] = { 61 { SYS_RES_MEMORY, 0, RF_ACTIVE }, 62 { -1, 0 } 63}; 64 65static int pxa_smi_probe(device_t); 66static int pxa_smi_attach(device_t); 67 68static int pxa_smi_print_child(device_t, device_t); 69 70static int pxa_smi_read_ivar(device_t, device_t, int, uintptr_t *); 71 72static struct resource * pxa_smi_alloc_resource(device_t, device_t, 73 int, int *, u_long, u_long, u_long, u_int); 74static int pxa_smi_release_resource(device_t, device_t, 75 int, int, struct resource *); 76static int pxa_smi_activate_resource(device_t, device_t, 77 int, int, struct resource *); 78 79static void pxa_smi_add_device(device_t, const char *, int); 80 81static int 82pxa_smi_probe(device_t dev) 83{ 84
| 27 28#include <sys/param.h> 29#include <sys/systm.h> 30#include <sys/bus.h> 31#include <sys/kernel.h> 32#include <sys/module.h> 33#include <sys/malloc.h> 34#include <sys/rman.h> 35#include <sys/timetc.h> 36#include <machine/bus.h> 37#include <machine/intr.h> 38 39#include <vm/vm.h> 40#include <vm/pmap.h> 41 42#include <arm/xscale/pxa/pxavar.h> 43#include <arm/xscale/pxa/pxareg.h> 44 45static MALLOC_DEFINE(M_PXASMI, "PXA SMI", 46 "Data for static memory interface devices."); 47 48struct pxa_smi_softc { 49 struct resource *ps_res[1]; 50 struct rman ps_mem; 51 bus_space_tag_t ps_bst; 52 bus_addr_t ps_base; 53}; 54 55struct smi_ivars { 56 struct resource_list smid_resources; 57 bus_addr_t smid_mem; 58}; 59 60static struct resource_spec pxa_smi_spec[] = { 61 { SYS_RES_MEMORY, 0, RF_ACTIVE }, 62 { -1, 0 } 63}; 64 65static int pxa_smi_probe(device_t); 66static int pxa_smi_attach(device_t); 67 68static int pxa_smi_print_child(device_t, device_t); 69 70static int pxa_smi_read_ivar(device_t, device_t, int, uintptr_t *); 71 72static struct resource * pxa_smi_alloc_resource(device_t, device_t, 73 int, int *, u_long, u_long, u_long, u_int); 74static int pxa_smi_release_resource(device_t, device_t, 75 int, int, struct resource *); 76static int pxa_smi_activate_resource(device_t, device_t, 77 int, int, struct resource *); 78 79static void pxa_smi_add_device(device_t, const char *, int); 80 81static int 82pxa_smi_probe(device_t dev) 83{ 84
|
85 device_set_desc(dev, "Static Memory Interface"); 86 return (0); 87} 88 89static int 90pxa_smi_attach(device_t dev) 91{ 92 int error, i, dunit; 93 const char *dname; 94 struct pxa_smi_softc *sc; 95 96 sc = (struct pxa_smi_softc *)device_get_softc(dev); 97 98 error = bus_alloc_resources(dev, pxa_smi_spec, sc->ps_res); 99 if (error) { 100 device_printf(dev, "could not allocate resources\n"); 101 return (ENXIO); 102 } 103 104 sc->ps_mem.rm_type = RMAN_ARRAY; 105 sc->ps_mem.rm_descr = device_get_nameunit(dev); 106 if (rman_init(&sc->ps_mem) != 0) 107 panic("pxa_smi_attach: failed to init mem rman"); 108 if (rman_manage_region(&sc->ps_mem, 0, PXA2X0_CS_SIZE * 6) != 0) 109 panic("pxa_smi_attach: failed ot set up mem rman"); 110 111 sc->ps_bst = base_tag; 112 sc->ps_base = rman_get_start(sc->ps_res[0]); 113 114 i = 0; 115 while (resource_find_match(&i, &dname, &dunit, "at", 116 device_get_nameunit(dev)) == 0) { 117 pxa_smi_add_device(dev, dname, dunit); 118 } 119 120 bus_generic_probe(dev); 121 bus_generic_attach(dev); 122 123 return (0); 124} 125 126static int 127pxa_smi_print_child(device_t dev, device_t child) 128{ 129 struct smi_ivars *smid; 130 int retval; 131 132 smid = (struct smi_ivars *)device_get_ivars(child); 133 if (smid == NULL) { 134 device_printf(dev, "unknown device: %s\n", 135 device_get_nameunit(child)); 136 return (0); 137 } 138 139 retval = 0; 140 141 retval += bus_print_child_header(dev, child); 142 143 retval += resource_list_print_type(&smid->smid_resources, "at mem", 144 SYS_RES_MEMORY, "%#lx"); 145 retval += resource_list_print_type(&smid->smid_resources, "irq", 146 SYS_RES_IRQ, "%ld"); 147 148 retval += bus_print_child_footer(dev, child); 149 150 return (retval); 151} 152 153static int 154pxa_smi_read_ivar(device_t dev, device_t child, int which, uintptr_t *result) 155{ 156 struct pxa_smi_softc *sc; 157 struct smi_ivars *smid; 158 159 sc = device_get_softc(dev); 160 smid = device_get_ivars(child); 161 162 switch (which) { 163 case SMI_IVAR_PHYSBASE: 164 *((bus_addr_t *)result) = smid->smid_mem; 165 break; 166 167 default: 168 return (ENOENT); 169 } 170 171 return (0); 172} 173 174static struct resource * 175pxa_smi_alloc_resource(device_t dev, device_t child, int type, int *rid, 176 u_long start, u_long end, u_long count, u_int flags) 177{ 178 struct pxa_smi_softc *sc; 179 struct smi_ivars *smid; 180 struct resource *rv; 181 struct resource_list *rl; 182 struct resource_list_entry *rle; 183 int needactivate; 184 185 sc = (struct pxa_smi_softc *)device_get_softc(dev); 186 smid = (struct smi_ivars *)device_get_ivars(child); 187 rl = &smid->smid_resources; 188 189 if (type == SYS_RES_IOPORT) 190 type = SYS_RES_MEMORY; 191 192 rle = resource_list_find(rl, type, *rid); 193 if (rle == NULL) 194 return (NULL); 195 if (rle->res != NULL) 196 panic("pxa_smi_alloc_resource: resource is busy"); 197 198 needactivate = flags & RF_ACTIVE; 199 flags &= ~RF_ACTIVE; 200 201 switch (type) { 202 case SYS_RES_MEMORY: 203 rv = rman_reserve_resource(&sc->ps_mem, rle->start, rle->end, 204 rle->count, flags, child); 205 if (rv == NULL) 206 return (NULL); 207 rle->res = rv; 208 rman_set_rid(rv, *rid); 209 rman_set_bustag(rv, sc->ps_bst); 210 rman_set_bushandle(rv, rle->start); 211 if (needactivate) { 212 if (bus_activate_resource(child, type, *rid, rv) != 0) { 213 rman_release_resource(rv); 214 return (NULL); 215 } 216 } 217 218 break; 219 220 case SYS_RES_IRQ: 221 rv = bus_alloc_resource(dev, type, rid, rle->start, rle->end, 222 rle->count, flags); 223 if (rv == NULL) 224 return (NULL); 225 if (needactivate) { 226 if (bus_activate_resource(child, type, *rid, rv) != 0) { 227 bus_release_resource(dev, type, *rid, rv); 228 return (NULL); 229 } 230 } 231 232 break; 233 234 default: 235 return (NULL); 236 } 237 238 return (rv); 239} 240 241static int 242pxa_smi_release_resource(device_t dev, device_t child, int type, int rid, 243 struct resource *r) 244{ 245 struct smi_ivars *smid; 246 struct resource_list *rl; 247 struct resource_list_entry *rle; 248 249 if (type == SYS_RES_IRQ) 250 return (bus_release_resource(dev, SYS_RES_IRQ, rid, r)); 251 252 smid = (struct smi_ivars *)device_get_ivars(child); 253 rl = &smid->smid_resources; 254 255 if (type == SYS_RES_IOPORT) 256 type = SYS_RES_MEMORY; 257 258 rle = resource_list_find(rl, type, rid); 259 if (rle == NULL) 260 panic("pxa_smi_release_resource: can't find resource"); 261 if (rle->res == NULL) 262 panic("pxa_smi_release_resource: resource entry not busy"); 263 264 rman_release_resource(rle->res); 265 rle->res = NULL; 266 267 return (0); 268} 269 270static int 271pxa_smi_activate_resource(device_t dev, device_t child, int type, int rid, 272 struct resource *r) 273{ 274 struct pxa_smi_softc *sc; 275 276 sc = (struct pxa_smi_softc *)device_get_softc(dev); 277 278 if (type == SYS_RES_IRQ) 279 return (bus_activate_resource(dev, SYS_RES_IRQ, rid, r)); 280 281 rman_set_bushandle(r, (bus_space_handle_t)pmap_mapdev(rman_get_start(r), 282 rman_get_size(r))); 283 return (rman_activate_resource(r)); 284} 285 286static device_method_t pxa_smi_methods[] = { 287 DEVMETHOD(device_probe, pxa_smi_probe), 288 DEVMETHOD(device_attach, pxa_smi_attach), 289 290 DEVMETHOD(bus_print_child, pxa_smi_print_child), 291 292 DEVMETHOD(bus_read_ivar, pxa_smi_read_ivar), 293 294 DEVMETHOD(bus_setup_intr, bus_generic_setup_intr), 295 296 DEVMETHOD(bus_alloc_resource, pxa_smi_alloc_resource), 297 DEVMETHOD(bus_release_resource, pxa_smi_release_resource), 298 DEVMETHOD(bus_activate_resource, pxa_smi_activate_resource), 299 300 {0, 0} 301}; 302 303static driver_t pxa_smi_driver = { 304 "smi", 305 pxa_smi_methods, 306 sizeof(struct pxa_smi_softc), 307}; 308 309static devclass_t pxa_smi_devclass; 310 311DRIVER_MODULE(smi, pxa, pxa_smi_driver, pxa_smi_devclass, 0, 0); 312 313static void 314pxa_smi_add_device(device_t dev, const char *name, int unit) 315{ 316 device_t child; 317 int start, count; 318 struct smi_ivars *ivars; 319 320 ivars = (struct smi_ivars *)malloc( 321 sizeof(struct smi_ivars), M_PXASMI, M_WAITOK); 322 if (ivars == NULL) 323 return; 324 325 child = device_add_child(dev, name, unit); 326 if (child == NULL) { 327 free(ivars, M_PXASMI); 328 return; 329 } 330 331 device_set_ivars(child, ivars); 332 resource_list_init(&ivars->smid_resources); 333 334 start = 0; 335 count = 0; 336 resource_int_value(name, unit, "mem", &start); 337 resource_int_value(name, unit, "size", &count); 338 if (start > 0 || count > 0) { 339 resource_list_add(&ivars->smid_resources, SYS_RES_MEMORY, 0, 340 start, start + count, count); 341 ivars->smid_mem = (bus_addr_t)start; 342 } 343 344 start = -1; 345 count = 0; 346 resource_int_value(name, unit, "irq", &start); 347 if (start > -1) 348 resource_list_add(&ivars->smid_resources, SYS_RES_IRQ, 0, start, 349 start, 1); 350 351 if (resource_disabled(name, unit)) 352 device_disable(child); 353}
| 88 device_set_desc(dev, "Static Memory Interface"); 89 return (0); 90} 91 92static int 93pxa_smi_attach(device_t dev) 94{ 95 int error, i, dunit; 96 const char *dname; 97 struct pxa_smi_softc *sc; 98 99 sc = (struct pxa_smi_softc *)device_get_softc(dev); 100 101 error = bus_alloc_resources(dev, pxa_smi_spec, sc->ps_res); 102 if (error) { 103 device_printf(dev, "could not allocate resources\n"); 104 return (ENXIO); 105 } 106 107 sc->ps_mem.rm_type = RMAN_ARRAY; 108 sc->ps_mem.rm_descr = device_get_nameunit(dev); 109 if (rman_init(&sc->ps_mem) != 0) 110 panic("pxa_smi_attach: failed to init mem rman"); 111 if (rman_manage_region(&sc->ps_mem, 0, PXA2X0_CS_SIZE * 6) != 0) 112 panic("pxa_smi_attach: failed ot set up mem rman"); 113 114 sc->ps_bst = base_tag; 115 sc->ps_base = rman_get_start(sc->ps_res[0]); 116 117 i = 0; 118 while (resource_find_match(&i, &dname, &dunit, "at", 119 device_get_nameunit(dev)) == 0) { 120 pxa_smi_add_device(dev, dname, dunit); 121 } 122 123 bus_generic_probe(dev); 124 bus_generic_attach(dev); 125 126 return (0); 127} 128 129static int 130pxa_smi_print_child(device_t dev, device_t child) 131{ 132 struct smi_ivars *smid; 133 int retval; 134 135 smid = (struct smi_ivars *)device_get_ivars(child); 136 if (smid == NULL) { 137 device_printf(dev, "unknown device: %s\n", 138 device_get_nameunit(child)); 139 return (0); 140 } 141 142 retval = 0; 143 144 retval += bus_print_child_header(dev, child); 145 146 retval += resource_list_print_type(&smid->smid_resources, "at mem", 147 SYS_RES_MEMORY, "%#lx"); 148 retval += resource_list_print_type(&smid->smid_resources, "irq", 149 SYS_RES_IRQ, "%ld"); 150 151 retval += bus_print_child_footer(dev, child); 152 153 return (retval); 154} 155 156static int 157pxa_smi_read_ivar(device_t dev, device_t child, int which, uintptr_t *result) 158{ 159 struct pxa_smi_softc *sc; 160 struct smi_ivars *smid; 161 162 sc = device_get_softc(dev); 163 smid = device_get_ivars(child); 164 165 switch (which) { 166 case SMI_IVAR_PHYSBASE: 167 *((bus_addr_t *)result) = smid->smid_mem; 168 break; 169 170 default: 171 return (ENOENT); 172 } 173 174 return (0); 175} 176 177static struct resource * 178pxa_smi_alloc_resource(device_t dev, device_t child, int type, int *rid, 179 u_long start, u_long end, u_long count, u_int flags) 180{ 181 struct pxa_smi_softc *sc; 182 struct smi_ivars *smid; 183 struct resource *rv; 184 struct resource_list *rl; 185 struct resource_list_entry *rle; 186 int needactivate; 187 188 sc = (struct pxa_smi_softc *)device_get_softc(dev); 189 smid = (struct smi_ivars *)device_get_ivars(child); 190 rl = &smid->smid_resources; 191 192 if (type == SYS_RES_IOPORT) 193 type = SYS_RES_MEMORY; 194 195 rle = resource_list_find(rl, type, *rid); 196 if (rle == NULL) 197 return (NULL); 198 if (rle->res != NULL) 199 panic("pxa_smi_alloc_resource: resource is busy"); 200 201 needactivate = flags & RF_ACTIVE; 202 flags &= ~RF_ACTIVE; 203 204 switch (type) { 205 case SYS_RES_MEMORY: 206 rv = rman_reserve_resource(&sc->ps_mem, rle->start, rle->end, 207 rle->count, flags, child); 208 if (rv == NULL) 209 return (NULL); 210 rle->res = rv; 211 rman_set_rid(rv, *rid); 212 rman_set_bustag(rv, sc->ps_bst); 213 rman_set_bushandle(rv, rle->start); 214 if (needactivate) { 215 if (bus_activate_resource(child, type, *rid, rv) != 0) { 216 rman_release_resource(rv); 217 return (NULL); 218 } 219 } 220 221 break; 222 223 case SYS_RES_IRQ: 224 rv = bus_alloc_resource(dev, type, rid, rle->start, rle->end, 225 rle->count, flags); 226 if (rv == NULL) 227 return (NULL); 228 if (needactivate) { 229 if (bus_activate_resource(child, type, *rid, rv) != 0) { 230 bus_release_resource(dev, type, *rid, rv); 231 return (NULL); 232 } 233 } 234 235 break; 236 237 default: 238 return (NULL); 239 } 240 241 return (rv); 242} 243 244static int 245pxa_smi_release_resource(device_t dev, device_t child, int type, int rid, 246 struct resource *r) 247{ 248 struct smi_ivars *smid; 249 struct resource_list *rl; 250 struct resource_list_entry *rle; 251 252 if (type == SYS_RES_IRQ) 253 return (bus_release_resource(dev, SYS_RES_IRQ, rid, r)); 254 255 smid = (struct smi_ivars *)device_get_ivars(child); 256 rl = &smid->smid_resources; 257 258 if (type == SYS_RES_IOPORT) 259 type = SYS_RES_MEMORY; 260 261 rle = resource_list_find(rl, type, rid); 262 if (rle == NULL) 263 panic("pxa_smi_release_resource: can't find resource"); 264 if (rle->res == NULL) 265 panic("pxa_smi_release_resource: resource entry not busy"); 266 267 rman_release_resource(rle->res); 268 rle->res = NULL; 269 270 return (0); 271} 272 273static int 274pxa_smi_activate_resource(device_t dev, device_t child, int type, int rid, 275 struct resource *r) 276{ 277 struct pxa_smi_softc *sc; 278 279 sc = (struct pxa_smi_softc *)device_get_softc(dev); 280 281 if (type == SYS_RES_IRQ) 282 return (bus_activate_resource(dev, SYS_RES_IRQ, rid, r)); 283 284 rman_set_bushandle(r, (bus_space_handle_t)pmap_mapdev(rman_get_start(r), 285 rman_get_size(r))); 286 return (rman_activate_resource(r)); 287} 288 289static device_method_t pxa_smi_methods[] = { 290 DEVMETHOD(device_probe, pxa_smi_probe), 291 DEVMETHOD(device_attach, pxa_smi_attach), 292 293 DEVMETHOD(bus_print_child, pxa_smi_print_child), 294 295 DEVMETHOD(bus_read_ivar, pxa_smi_read_ivar), 296 297 DEVMETHOD(bus_setup_intr, bus_generic_setup_intr), 298 299 DEVMETHOD(bus_alloc_resource, pxa_smi_alloc_resource), 300 DEVMETHOD(bus_release_resource, pxa_smi_release_resource), 301 DEVMETHOD(bus_activate_resource, pxa_smi_activate_resource), 302 303 {0, 0} 304}; 305 306static driver_t pxa_smi_driver = { 307 "smi", 308 pxa_smi_methods, 309 sizeof(struct pxa_smi_softc), 310}; 311 312static devclass_t pxa_smi_devclass; 313 314DRIVER_MODULE(smi, pxa, pxa_smi_driver, pxa_smi_devclass, 0, 0); 315 316static void 317pxa_smi_add_device(device_t dev, const char *name, int unit) 318{ 319 device_t child; 320 int start, count; 321 struct smi_ivars *ivars; 322 323 ivars = (struct smi_ivars *)malloc( 324 sizeof(struct smi_ivars), M_PXASMI, M_WAITOK); 325 if (ivars == NULL) 326 return; 327 328 child = device_add_child(dev, name, unit); 329 if (child == NULL) { 330 free(ivars, M_PXASMI); 331 return; 332 } 333 334 device_set_ivars(child, ivars); 335 resource_list_init(&ivars->smid_resources); 336 337 start = 0; 338 count = 0; 339 resource_int_value(name, unit, "mem", &start); 340 resource_int_value(name, unit, "size", &count); 341 if (start > 0 || count > 0) { 342 resource_list_add(&ivars->smid_resources, SYS_RES_MEMORY, 0, 343 start, start + count, count); 344 ivars->smid_mem = (bus_addr_t)start; 345 } 346 347 start = -1; 348 count = 0; 349 resource_int_value(name, unit, "irq", &start); 350 if (start > -1) 351 resource_list_add(&ivars->smid_resources, SYS_RES_IRQ, 0, start, 352 start, 1); 353 354 if (resource_disabled(name, unit)) 355 device_disable(child); 356}
|