1098ca2bdSWarner Losh /*- 217d24755SJustin T. Gibbs * FreeBSD, PCI product support functions 317d24755SJustin T. Gibbs * 417d24755SJustin T. Gibbs * Copyright (c) 1995-2001 Justin T. Gibbs 517d24755SJustin T. Gibbs * All rights reserved. 617d24755SJustin T. Gibbs * 717d24755SJustin T. Gibbs * Redistribution and use in source and binary forms, with or without 817d24755SJustin T. Gibbs * modification, are permitted provided that the following conditions 917d24755SJustin T. Gibbs * are met: 1017d24755SJustin T. Gibbs * 1. Redistributions of source code must retain the above copyright 1117d24755SJustin T. Gibbs * notice, this list of conditions, and the following disclaimer, 1217d24755SJustin T. Gibbs * without modification, immediately at the beginning of the file. 1317d24755SJustin T. Gibbs * 2. The name of the author may not be used to endorse or promote products 1417d24755SJustin T. Gibbs * derived from this software without specific prior written permission. 1517d24755SJustin T. Gibbs * 1617d24755SJustin T. Gibbs * Alternatively, this software may be distributed under the terms of the 1717d24755SJustin T. Gibbs * GNU Public License ("GPL"). 1817d24755SJustin T. Gibbs * 1917d24755SJustin T. Gibbs * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 2017d24755SJustin T. Gibbs * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2117d24755SJustin T. Gibbs * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2217d24755SJustin T. Gibbs * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR 2317d24755SJustin T. Gibbs * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2417d24755SJustin T. Gibbs * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2517d24755SJustin T. Gibbs * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2617d24755SJustin T. Gibbs * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2717d24755SJustin T. Gibbs * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2817d24755SJustin T. Gibbs * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2917d24755SJustin T. Gibbs * SUCH DAMAGE. 3017d24755SJustin T. Gibbs * 31b3b25f2cSJustin T. Gibbs * $Id: //depot/aic7xxx/freebsd/dev/aic7xxx/ahd_pci.c#17 $ 3217d24755SJustin T. Gibbs */ 3317d24755SJustin T. Gibbs 34aad970f1SDavid E. O'Brien #include <sys/cdefs.h> 35aad970f1SDavid E. O'Brien __FBSDID("$FreeBSD$"); 36aad970f1SDavid E. O'Brien 3717d24755SJustin T. Gibbs #include <dev/aic7xxx/aic79xx_osm.h> 3817d24755SJustin T. Gibbs 3917d24755SJustin T. Gibbs static int ahd_pci_probe(device_t dev); 4017d24755SJustin T. Gibbs static int ahd_pci_attach(device_t dev); 4117d24755SJustin T. Gibbs 4217d24755SJustin T. Gibbs static device_method_t ahd_pci_device_methods[] = { 4317d24755SJustin T. Gibbs /* Device interface */ 4417d24755SJustin T. Gibbs DEVMETHOD(device_probe, ahd_pci_probe), 4517d24755SJustin T. Gibbs DEVMETHOD(device_attach, ahd_pci_attach), 4617d24755SJustin T. Gibbs DEVMETHOD(device_detach, ahd_detach), 4717d24755SJustin T. Gibbs { 0, 0 } 4817d24755SJustin T. Gibbs }; 4917d24755SJustin T. Gibbs 5017d24755SJustin T. Gibbs static driver_t ahd_pci_driver = { 5117d24755SJustin T. Gibbs "ahd", 5217d24755SJustin T. Gibbs ahd_pci_device_methods, 5317d24755SJustin T. Gibbs sizeof(struct ahd_softc) 5417d24755SJustin T. Gibbs }; 5517d24755SJustin T. Gibbs 56*43fb772cSJohn Baldwin DRIVER_MODULE(ahd, pci, ahd_pci_driver, 0, 0); 5717d24755SJustin T. Gibbs MODULE_DEPEND(ahd_pci, ahd, 1, 1, 1); 5817d24755SJustin T. Gibbs MODULE_VERSION(ahd_pci, 1); 5917d24755SJustin T. Gibbs 6017d24755SJustin T. Gibbs static int 6117d24755SJustin T. Gibbs ahd_pci_probe(device_t dev) 6217d24755SJustin T. Gibbs { 6317d24755SJustin T. Gibbs struct ahd_pci_identity *entry; 6417d24755SJustin T. Gibbs 6517d24755SJustin T. Gibbs entry = ahd_find_pci_device(dev); 6617d24755SJustin T. Gibbs if (entry != NULL) { 6717d24755SJustin T. Gibbs device_set_desc(dev, entry->name); 68494f3ca1SWarner Losh return (BUS_PROBE_DEFAULT); 6917d24755SJustin T. Gibbs } 7017d24755SJustin T. Gibbs return (ENXIO); 7117d24755SJustin T. Gibbs } 7217d24755SJustin T. Gibbs 7317d24755SJustin T. Gibbs static int 7417d24755SJustin T. Gibbs ahd_pci_attach(device_t dev) 7517d24755SJustin T. Gibbs { 7617d24755SJustin T. Gibbs struct ahd_pci_identity *entry; 7717d24755SJustin T. Gibbs struct ahd_softc *ahd; 7817d24755SJustin T. Gibbs char *name; 7917d24755SJustin T. Gibbs int error; 8017d24755SJustin T. Gibbs 8117d24755SJustin T. Gibbs entry = ahd_find_pci_device(dev); 8217d24755SJustin T. Gibbs if (entry == NULL) 8317d24755SJustin T. Gibbs return (ENXIO); 8417d24755SJustin T. Gibbs 8517d24755SJustin T. Gibbs /* 8617d24755SJustin T. Gibbs * Allocate a softc for this card and 8717d24755SJustin T. Gibbs * set it up for attachment by our 8817d24755SJustin T. Gibbs * common detect routine. 8917d24755SJustin T. Gibbs */ 9017d24755SJustin T. Gibbs name = malloc(strlen(device_get_nameunit(dev)) + 1, M_DEVBUF, M_NOWAIT); 9117d24755SJustin T. Gibbs if (name == NULL) 9217d24755SJustin T. Gibbs return (ENOMEM); 9317d24755SJustin T. Gibbs strcpy(name, device_get_nameunit(dev)); 9417d24755SJustin T. Gibbs ahd = ahd_alloc(dev, name); 9517d24755SJustin T. Gibbs if (ahd == NULL) 9617d24755SJustin T. Gibbs return (ENOMEM); 9717d24755SJustin T. Gibbs 9817d24755SJustin T. Gibbs ahd_set_unit(ahd, device_get_unit(dev)); 9917d24755SJustin T. Gibbs 10017d24755SJustin T. Gibbs /* 10117d24755SJustin T. Gibbs * Should we bother disabling 39Bit addressing 10217d24755SJustin T. Gibbs * based on installed memory? 10317d24755SJustin T. Gibbs */ 10417d24755SJustin T. Gibbs if (sizeof(bus_addr_t) > 4) 10517d24755SJustin T. Gibbs ahd->flags |= AHD_39BIT_ADDRESSING; 10617d24755SJustin T. Gibbs 10717d24755SJustin T. Gibbs /* Allocate a dmatag for our SCB DMA maps */ 108378f231eSJohn-Mark Gurney error = aic_dma_tag_create(ahd, /*parent*/bus_get_dma_tag(dev), 109378f231eSJohn-Mark Gurney /*alignment*/1, /*boundary*/0, 11017d24755SJustin T. Gibbs (ahd->flags & AHD_39BIT_ADDRESSING) 111b3b25f2cSJustin T. Gibbs ? 0x7FFFFFFFFF 11217d24755SJustin T. Gibbs : BUS_SPACE_MAXADDR_32BIT, 11317d24755SJustin T. Gibbs /*highaddr*/BUS_SPACE_MAXADDR, 11417d24755SJustin T. Gibbs /*filter*/NULL, /*filterarg*/NULL, 1151c754048SScott Long /*maxsize*/BUS_SPACE_MAXSIZE_32BIT, 1161c754048SScott Long /*nsegments*/AHD_NSEG, 11717d24755SJustin T. Gibbs /*maxsegsz*/AHD_MAXTRANSFER_SIZE, 1188270490aSJustin T. Gibbs /*flags*/0, 11917d24755SJustin T. Gibbs &ahd->parent_dmat); 12017d24755SJustin T. Gibbs 12117d24755SJustin T. Gibbs if (error != 0) { 12217d24755SJustin T. Gibbs printf("ahd_pci_attach: Could not allocate DMA tag " 12317d24755SJustin T. Gibbs "- error %d\n", error); 12417d24755SJustin T. Gibbs ahd_free(ahd); 12517d24755SJustin T. Gibbs return (ENOMEM); 12617d24755SJustin T. Gibbs } 12717d24755SJustin T. Gibbs ahd->dev_softc = dev; 12817d24755SJustin T. Gibbs error = ahd_pci_config(ahd, entry); 12917d24755SJustin T. Gibbs if (error != 0) { 13017d24755SJustin T. Gibbs ahd_free(ahd); 13117d24755SJustin T. Gibbs return (error); 13217d24755SJustin T. Gibbs } 13317d24755SJustin T. Gibbs 134884015f6SAttilio Rao ahd_sysctl(ahd); 13517d24755SJustin T. Gibbs ahd_attach(ahd); 13617d24755SJustin T. Gibbs return (0); 13717d24755SJustin T. Gibbs } 13817d24755SJustin T. Gibbs 13917d24755SJustin T. Gibbs int 14017d24755SJustin T. Gibbs ahd_pci_map_registers(struct ahd_softc *ahd) 14117d24755SJustin T. Gibbs { 14217d24755SJustin T. Gibbs struct resource *regs; 14317d24755SJustin T. Gibbs struct resource *regs2; 14417d24755SJustin T. Gibbs int regs_type; 14517d24755SJustin T. Gibbs int regs_id; 14617d24755SJustin T. Gibbs int regs_id2; 14797cae63dSScott Long int allow_memio; 14817d24755SJustin T. Gibbs 14917d24755SJustin T. Gibbs regs = NULL; 15017d24755SJustin T. Gibbs regs2 = NULL; 15117d24755SJustin T. Gibbs regs_type = 0; 15217d24755SJustin T. Gibbs regs_id = 0; 15397cae63dSScott Long 15497cae63dSScott Long /* Retrieve the per-device 'allow_memio' hint */ 15597cae63dSScott Long if (resource_int_value(device_get_name(ahd->dev_softc), 15697cae63dSScott Long device_get_unit(ahd->dev_softc), 15797cae63dSScott Long "allow_memio", &allow_memio) != 0) { 15897cae63dSScott Long if (bootverbose) 15997cae63dSScott Long device_printf(ahd->dev_softc, 16097cae63dSScott Long "Defaulting to MEMIO on\n"); 1616bdc5bdfSJustin T. Gibbs allow_memio = 1; 16297cae63dSScott Long } 16397cae63dSScott Long 164c68534f1SScott Long if ((ahd->bugs & AHD_PCIX_MMAPIO_BUG) == 0 16597cae63dSScott Long && allow_memio != 0) { 16617d24755SJustin T. Gibbs regs_type = SYS_RES_MEMORY; 16717d24755SJustin T. Gibbs regs_id = AHD_PCI_MEMADDR; 1685f96beb9SNate Lawson regs = bus_alloc_resource_any(ahd->dev_softc, regs_type, 1695f96beb9SNate Lawson ®s_id, RF_ACTIVE); 17017d24755SJustin T. Gibbs if (regs != NULL) { 17117d24755SJustin T. Gibbs int error; 17217d24755SJustin T. Gibbs 17317d24755SJustin T. Gibbs ahd->tags[0] = rman_get_bustag(regs); 17417d24755SJustin T. Gibbs ahd->bshs[0] = rman_get_bushandle(regs); 17517d24755SJustin T. Gibbs ahd->tags[1] = ahd->tags[0]; 17617d24755SJustin T. Gibbs error = bus_space_subregion(ahd->tags[0], ahd->bshs[0], 17717d24755SJustin T. Gibbs /*offset*/0x100, 17817d24755SJustin T. Gibbs /*size*/0x100, 17917d24755SJustin T. Gibbs &ahd->bshs[1]); 18017d24755SJustin T. Gibbs /* 18117d24755SJustin T. Gibbs * Do a quick test to see if memory mapped 18217d24755SJustin T. Gibbs * I/O is functioning correctly. 18317d24755SJustin T. Gibbs */ 184add68794SScott Long if (error != 0 185add68794SScott Long || ahd_pci_test_register_access(ahd) != 0) { 18617d24755SJustin T. Gibbs device_printf(ahd->dev_softc, 18717d24755SJustin T. Gibbs "PCI Device %d:%d:%d failed memory " 18817d24755SJustin T. Gibbs "mapped test. Using PIO.\n", 189b3b25f2cSJustin T. Gibbs aic_get_pci_bus(ahd->dev_softc), 190b3b25f2cSJustin T. Gibbs aic_get_pci_slot(ahd->dev_softc), 191b3b25f2cSJustin T. Gibbs aic_get_pci_function(ahd->dev_softc)); 19217d24755SJustin T. Gibbs bus_release_resource(ahd->dev_softc, regs_type, 19317d24755SJustin T. Gibbs regs_id, regs); 19417d24755SJustin T. Gibbs regs = NULL; 195884015f6SAttilio Rao AHD_CORRECTABLE_ERROR(ahd); 19617d24755SJustin T. Gibbs } 19717d24755SJustin T. Gibbs } 19817d24755SJustin T. Gibbs } 199c68534f1SScott Long if (regs == NULL) { 20017d24755SJustin T. Gibbs regs_type = SYS_RES_IOPORT; 20117d24755SJustin T. Gibbs regs_id = AHD_PCI_IOADDR0; 2025f96beb9SNate Lawson regs = bus_alloc_resource_any(ahd->dev_softc, regs_type, 2035f96beb9SNate Lawson ®s_id, RF_ACTIVE); 20417d24755SJustin T. Gibbs if (regs == NULL) { 20517d24755SJustin T. Gibbs device_printf(ahd->dev_softc, 20617d24755SJustin T. Gibbs "can't allocate register resources\n"); 207884015f6SAttilio Rao AHD_UNCORRECTABLE_ERROR(ahd); 20817d24755SJustin T. Gibbs return (ENOMEM); 20917d24755SJustin T. Gibbs } 21017d24755SJustin T. Gibbs ahd->tags[0] = rman_get_bustag(regs); 21117d24755SJustin T. Gibbs ahd->bshs[0] = rman_get_bushandle(regs); 21217d24755SJustin T. Gibbs 21317d24755SJustin T. Gibbs /* And now the second BAR */ 21417d24755SJustin T. Gibbs regs_id2 = AHD_PCI_IOADDR1; 2155f96beb9SNate Lawson regs2 = bus_alloc_resource_any(ahd->dev_softc, regs_type, 2165f96beb9SNate Lawson ®s_id2, RF_ACTIVE); 21717d24755SJustin T. Gibbs if (regs2 == NULL) { 21817d24755SJustin T. Gibbs device_printf(ahd->dev_softc, 21917d24755SJustin T. Gibbs "can't allocate register resources\n"); 220884015f6SAttilio Rao AHD_UNCORRECTABLE_ERROR(ahd); 22117d24755SJustin T. Gibbs return (ENOMEM); 22217d24755SJustin T. Gibbs } 22317d24755SJustin T. Gibbs ahd->tags[1] = rman_get_bustag(regs2); 22417d24755SJustin T. Gibbs ahd->bshs[1] = rman_get_bushandle(regs2); 22517d24755SJustin T. Gibbs ahd->platform_data->regs_res_type[1] = regs_type; 22617d24755SJustin T. Gibbs ahd->platform_data->regs_res_id[1] = regs_id2; 22717d24755SJustin T. Gibbs ahd->platform_data->regs[1] = regs2; 22817d24755SJustin T. Gibbs } 22917d24755SJustin T. Gibbs ahd->platform_data->regs_res_type[0] = regs_type; 23017d24755SJustin T. Gibbs ahd->platform_data->regs_res_id[0] = regs_id; 23117d24755SJustin T. Gibbs ahd->platform_data->regs[0] = regs; 23217d24755SJustin T. Gibbs return (0); 23317d24755SJustin T. Gibbs } 23417d24755SJustin T. Gibbs 23517d24755SJustin T. Gibbs int 23617d24755SJustin T. Gibbs ahd_pci_map_int(struct ahd_softc *ahd) 23717d24755SJustin T. Gibbs { 23817d24755SJustin T. Gibbs int zero; 23917d24755SJustin T. Gibbs 24017d24755SJustin T. Gibbs zero = 0; 24117d24755SJustin T. Gibbs ahd->platform_data->irq = 2425f96beb9SNate Lawson bus_alloc_resource_any(ahd->dev_softc, SYS_RES_IRQ, &zero, 2435f96beb9SNate Lawson RF_ACTIVE | RF_SHAREABLE); 24417d24755SJustin T. Gibbs if (ahd->platform_data->irq == NULL) 24517d24755SJustin T. Gibbs return (ENOMEM); 24617d24755SJustin T. Gibbs ahd->platform_data->irq_res_type = SYS_RES_IRQ; 24717d24755SJustin T. Gibbs return (ahd_map_int(ahd)); 24817d24755SJustin T. Gibbs } 249