11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds * Linux driver attachment glue for PCI based controllers.
31da177e4SLinus Torvalds *
41da177e4SLinus Torvalds * Copyright (c) 2000-2001 Adaptec Inc.
51da177e4SLinus Torvalds * All rights reserved.
61da177e4SLinus Torvalds *
71da177e4SLinus Torvalds * Redistribution and use in source and binary forms, with or without
81da177e4SLinus Torvalds * modification, are permitted provided that the following conditions
91da177e4SLinus Torvalds * are met:
101da177e4SLinus Torvalds * 1. Redistributions of source code must retain the above copyright
111da177e4SLinus Torvalds * notice, this list of conditions, and the following disclaimer,
121da177e4SLinus Torvalds * without modification.
131da177e4SLinus Torvalds * 2. Redistributions in binary form must reproduce at minimum a disclaimer
141da177e4SLinus Torvalds * substantially similar to the "NO WARRANTY" disclaimer below
151da177e4SLinus Torvalds * ("Disclaimer") and any redistribution must be conditioned upon
161da177e4SLinus Torvalds * including a substantially similar Disclaimer requirement for further
171da177e4SLinus Torvalds * binary redistribution.
181da177e4SLinus Torvalds * 3. Neither the names of the above-listed copyright holders nor the names
191da177e4SLinus Torvalds * of any contributors may be used to endorse or promote products derived
201da177e4SLinus Torvalds * from this software without specific prior written permission.
211da177e4SLinus Torvalds *
221da177e4SLinus Torvalds * Alternatively, this software may be distributed under the terms of the
231da177e4SLinus Torvalds * GNU General Public License ("GPL") version 2 as published by the Free
241da177e4SLinus Torvalds * Software Foundation.
251da177e4SLinus Torvalds *
261da177e4SLinus Torvalds * NO WARRANTY
271da177e4SLinus Torvalds * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
281da177e4SLinus Torvalds * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
291da177e4SLinus Torvalds * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
301da177e4SLinus Torvalds * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
311da177e4SLinus Torvalds * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
321da177e4SLinus Torvalds * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
331da177e4SLinus Torvalds * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
341da177e4SLinus Torvalds * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
351da177e4SLinus Torvalds * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
361da177e4SLinus Torvalds * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
371da177e4SLinus Torvalds * POSSIBILITY OF SUCH DAMAGES.
381da177e4SLinus Torvalds *
391da177e4SLinus Torvalds * $Id: //depot/aic7xxx/linux/drivers/scsi/aic7xxx/aic7xxx_osm_pci.c#47 $
401da177e4SLinus Torvalds */
411da177e4SLinus Torvalds
421da177e4SLinus Torvalds #include "aic7xxx_osm.h"
431da177e4SLinus Torvalds #include "aic7xxx_pci.h"
441da177e4SLinus Torvalds
451da177e4SLinus Torvalds /* Define the macro locally since it's different for different class of chips.
461da177e4SLinus Torvalds */
471da177e4SLinus Torvalds #define ID(x) ID_C(x, PCI_CLASS_STORAGE_SCSI)
481da177e4SLinus Torvalds
49980b306aSDenys Vlasenko static const struct pci_device_id ahc_linux_pci_id_table[] = {
501da177e4SLinus Torvalds /* aic7850 based controllers */
511da177e4SLinus Torvalds ID(ID_AHA_2902_04_10_15_20C_30C),
521da177e4SLinus Torvalds /* aic7860 based controllers */
531da177e4SLinus Torvalds ID(ID_AHA_2930CU),
541da177e4SLinus Torvalds ID(ID_AHA_1480A & ID_DEV_VENDOR_MASK),
551da177e4SLinus Torvalds ID(ID_AHA_2940AU_0 & ID_DEV_VENDOR_MASK),
561da177e4SLinus Torvalds ID(ID_AHA_2940AU_CN & ID_DEV_VENDOR_MASK),
571da177e4SLinus Torvalds ID(ID_AHA_2930C_VAR & ID_DEV_VENDOR_MASK),
581da177e4SLinus Torvalds /* aic7870 based controllers */
591da177e4SLinus Torvalds ID(ID_AHA_2940),
601da177e4SLinus Torvalds ID(ID_AHA_3940),
611da177e4SLinus Torvalds ID(ID_AHA_398X),
621da177e4SLinus Torvalds ID(ID_AHA_2944),
631da177e4SLinus Torvalds ID(ID_AHA_3944),
641da177e4SLinus Torvalds ID(ID_AHA_4944),
651da177e4SLinus Torvalds /* aic7880 based controllers */
661da177e4SLinus Torvalds ID(ID_AHA_2940U & ID_DEV_VENDOR_MASK),
671da177e4SLinus Torvalds ID(ID_AHA_3940U & ID_DEV_VENDOR_MASK),
681da177e4SLinus Torvalds ID(ID_AHA_2944U & ID_DEV_VENDOR_MASK),
691da177e4SLinus Torvalds ID(ID_AHA_3944U & ID_DEV_VENDOR_MASK),
701da177e4SLinus Torvalds ID(ID_AHA_398XU & ID_DEV_VENDOR_MASK),
711da177e4SLinus Torvalds ID(ID_AHA_4944U & ID_DEV_VENDOR_MASK),
721da177e4SLinus Torvalds ID(ID_AHA_2930U & ID_DEV_VENDOR_MASK),
731da177e4SLinus Torvalds ID(ID_AHA_2940U_PRO & ID_DEV_VENDOR_MASK),
741da177e4SLinus Torvalds ID(ID_AHA_2940U_CN & ID_DEV_VENDOR_MASK),
751da177e4SLinus Torvalds /* aic7890 based controllers */
761da177e4SLinus Torvalds ID(ID_AHA_2930U2),
771da177e4SLinus Torvalds ID(ID_AHA_2940U2B),
781da177e4SLinus Torvalds ID(ID_AHA_2940U2_OEM),
791da177e4SLinus Torvalds ID(ID_AHA_2940U2),
801da177e4SLinus Torvalds ID(ID_AHA_2950U2B),
811da177e4SLinus Torvalds ID16(ID_AIC7890_ARO & ID_AIC7895_ARO_MASK),
821da177e4SLinus Torvalds ID(ID_AAA_131U2),
831da177e4SLinus Torvalds /* aic7890 based controllers */
841da177e4SLinus Torvalds ID(ID_AHA_29160),
851da177e4SLinus Torvalds ID(ID_AHA_29160_CPQ),
861da177e4SLinus Torvalds ID(ID_AHA_29160N),
871da177e4SLinus Torvalds ID(ID_AHA_29160C),
881da177e4SLinus Torvalds ID(ID_AHA_29160B),
891da177e4SLinus Torvalds ID(ID_AHA_19160B),
901da177e4SLinus Torvalds ID(ID_AIC7892_ARO),
911da177e4SLinus Torvalds /* aic7892 based controllers */
921da177e4SLinus Torvalds ID(ID_AHA_2940U_DUAL),
931da177e4SLinus Torvalds ID(ID_AHA_3940AU),
941da177e4SLinus Torvalds ID(ID_AHA_3944AU),
951da177e4SLinus Torvalds ID(ID_AIC7895_ARO),
961da177e4SLinus Torvalds ID(ID_AHA_3950U2B_0),
971da177e4SLinus Torvalds ID(ID_AHA_3950U2B_1),
981da177e4SLinus Torvalds ID(ID_AHA_3950U2D_0),
991da177e4SLinus Torvalds ID(ID_AHA_3950U2D_1),
1001da177e4SLinus Torvalds ID(ID_AIC7896_ARO),
1011da177e4SLinus Torvalds /* aic7899 based controllers */
1021da177e4SLinus Torvalds ID(ID_AHA_3960D),
1031da177e4SLinus Torvalds ID(ID_AHA_3960D_CPQ),
1041da177e4SLinus Torvalds ID(ID_AIC7899_ARO),
1051da177e4SLinus Torvalds /* Generic chip probes for devices we don't know exactly. */
1061da177e4SLinus Torvalds ID(ID_AIC7850 & ID_DEV_VENDOR_MASK),
1071da177e4SLinus Torvalds ID(ID_AIC7855 & ID_DEV_VENDOR_MASK),
1081da177e4SLinus Torvalds ID(ID_AIC7859 & ID_DEV_VENDOR_MASK),
1091da177e4SLinus Torvalds ID(ID_AIC7860 & ID_DEV_VENDOR_MASK),
1101da177e4SLinus Torvalds ID(ID_AIC7870 & ID_DEV_VENDOR_MASK),
1111da177e4SLinus Torvalds ID(ID_AIC7880 & ID_DEV_VENDOR_MASK),
1121da177e4SLinus Torvalds ID16(ID_AIC7890 & ID_9005_GENERIC_MASK),
1131da177e4SLinus Torvalds ID16(ID_AIC7892 & ID_9005_GENERIC_MASK),
1141da177e4SLinus Torvalds ID(ID_AIC7895 & ID_DEV_VENDOR_MASK),
1151da177e4SLinus Torvalds ID16(ID_AIC7896 & ID_9005_GENERIC_MASK),
1161da177e4SLinus Torvalds ID16(ID_AIC7899 & ID_9005_GENERIC_MASK),
1171da177e4SLinus Torvalds ID(ID_AIC7810 & ID_DEV_VENDOR_MASK),
1181da177e4SLinus Torvalds ID(ID_AIC7815 & ID_DEV_VENDOR_MASK),
1191da177e4SLinus Torvalds { 0 }
1201da177e4SLinus Torvalds };
1211da177e4SLinus Torvalds
1221da177e4SLinus Torvalds MODULE_DEVICE_TABLE(pci, ahc_linux_pci_id_table);
1231da177e4SLinus Torvalds
1246897b9a1SVaibhav Gupta static int __maybe_unused
ahc_linux_pci_dev_suspend(struct device * dev)1256897b9a1SVaibhav Gupta ahc_linux_pci_dev_suspend(struct device *dev)
126b5720729SHannes Reinecke {
1276897b9a1SVaibhav Gupta struct ahc_softc *ahc = dev_get_drvdata(dev);
128b5720729SHannes Reinecke
1296897b9a1SVaibhav Gupta return ahc_suspend(ahc);
130b5720729SHannes Reinecke }
131b5720729SHannes Reinecke
1326897b9a1SVaibhav Gupta static int __maybe_unused
ahc_linux_pci_dev_resume(struct device * dev)1336897b9a1SVaibhav Gupta ahc_linux_pci_dev_resume(struct device *dev)
134b5720729SHannes Reinecke {
1356897b9a1SVaibhav Gupta struct ahc_softc *ahc = dev_get_drvdata(dev);
136b5720729SHannes Reinecke
137b5720729SHannes Reinecke ahc_pci_resume(ahc);
138b5720729SHannes Reinecke
139b5720729SHannes Reinecke return (ahc_resume(ahc));
140b5720729SHannes Reinecke }
141b5720729SHannes Reinecke
1421da177e4SLinus Torvalds static void
ahc_linux_pci_dev_remove(struct pci_dev * pdev)1431da177e4SLinus Torvalds ahc_linux_pci_dev_remove(struct pci_dev *pdev)
1441da177e4SLinus Torvalds {
1453d65692aSChristoph Hellwig struct ahc_softc *ahc = pci_get_drvdata(pdev);
1461da177e4SLinus Torvalds u_long s;
1471da177e4SLinus Torvalds
14897af50f6SJames Bottomley if (ahc->platform_data && ahc->platform_data->host)
14997af50f6SJames Bottomley scsi_remove_host(ahc->platform_data->host);
15097af50f6SJames Bottomley
1511da177e4SLinus Torvalds ahc_lock(ahc, &s);
1521da177e4SLinus Torvalds ahc_intr_enable(ahc, FALSE);
1531da177e4SLinus Torvalds ahc_unlock(ahc, &s);
1541da177e4SLinus Torvalds ahc_free(ahc);
1551da177e4SLinus Torvalds }
1561da177e4SLinus Torvalds
1573a4f5c60SJames Bottomley static void
ahc_linux_pci_inherit_flags(struct ahc_softc * ahc)1583a4f5c60SJames Bottomley ahc_linux_pci_inherit_flags(struct ahc_softc *ahc)
1593a4f5c60SJames Bottomley {
1603a4f5c60SJames Bottomley struct pci_dev *pdev = ahc->dev_softc, *master_pdev;
1613a4f5c60SJames Bottomley unsigned int master_devfn = PCI_DEVFN(PCI_SLOT(pdev->devfn), 0);
1623a4f5c60SJames Bottomley
1633a4f5c60SJames Bottomley master_pdev = pci_get_slot(pdev->bus, master_devfn);
1643a4f5c60SJames Bottomley if (master_pdev) {
1653a4f5c60SJames Bottomley struct ahc_softc *master = pci_get_drvdata(master_pdev);
1663a4f5c60SJames Bottomley if (master) {
1673a4f5c60SJames Bottomley ahc->flags &= ~AHC_BIOS_ENABLED;
1683a4f5c60SJames Bottomley ahc->flags |= master->flags & AHC_BIOS_ENABLED;
1693a4f5c60SJames Bottomley
1703a4f5c60SJames Bottomley ahc->flags &= ~AHC_PRIMARY_CHANNEL;
1713a4f5c60SJames Bottomley ahc->flags |= master->flags & AHC_PRIMARY_CHANNEL;
1723a4f5c60SJames Bottomley } else
1733a4f5c60SJames Bottomley printk(KERN_ERR "aic7xxx: no multichannel peer found!\n");
1743a4f5c60SJames Bottomley pci_dev_put(master_pdev);
1753a4f5c60SJames Bottomley }
1763a4f5c60SJames Bottomley }
1773a4f5c60SJames Bottomley
1781da177e4SLinus Torvalds static int
ahc_linux_pci_dev_probe(struct pci_dev * pdev,const struct pci_device_id * ent)1791da177e4SLinus Torvalds ahc_linux_pci_dev_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
1801da177e4SLinus Torvalds {
1811da177e4SLinus Torvalds char buf[80];
1821da177e4SLinus Torvalds const uint64_t mask_39bit = 0x7FFFFFFFFFULL;
1831da177e4SLinus Torvalds struct ahc_softc *ahc;
1841da177e4SLinus Torvalds ahc_dev_softc_t pci;
185980b306aSDenys Vlasenko const struct ahc_pci_identity *entry;
1861da177e4SLinus Torvalds char *name;
1871da177e4SLinus Torvalds int error;
188a89f29f6SJames Bottomley struct device *dev = &pdev->dev;
1891da177e4SLinus Torvalds
1901da177e4SLinus Torvalds pci = pdev;
1911da177e4SLinus Torvalds entry = ahc_find_pci_device(pci);
1921da177e4SLinus Torvalds if (entry == NULL)
1931da177e4SLinus Torvalds return (-ENODEV);
1941da177e4SLinus Torvalds
1951da177e4SLinus Torvalds /*
1961da177e4SLinus Torvalds * Allocate a softc for this card and
1971da177e4SLinus Torvalds * set it up for attachment by our
1981da177e4SLinus Torvalds * common detect routine.
1991da177e4SLinus Torvalds */
2001da177e4SLinus Torvalds sprintf(buf, "ahc_pci:%d:%d:%d",
2011da177e4SLinus Torvalds ahc_get_pci_bus(pci),
2021da177e4SLinus Torvalds ahc_get_pci_slot(pci),
2031da177e4SLinus Torvalds ahc_get_pci_function(pci));
2041f3d2d9eSHimangi Saraogi name = kstrdup(buf, GFP_ATOMIC);
2051da177e4SLinus Torvalds if (name == NULL)
2061da177e4SLinus Torvalds return (-ENOMEM);
2071da177e4SLinus Torvalds ahc = ahc_alloc(NULL, name);
2081da177e4SLinus Torvalds if (ahc == NULL)
2091da177e4SLinus Torvalds return (-ENOMEM);
2101da177e4SLinus Torvalds if (pci_enable_device(pdev)) {
2111da177e4SLinus Torvalds ahc_free(ahc);
2121da177e4SLinus Torvalds return (-ENODEV);
2131da177e4SLinus Torvalds }
2141da177e4SLinus Torvalds pci_set_master(pdev);
2151da177e4SLinus Torvalds
2161da177e4SLinus Torvalds if (sizeof(dma_addr_t) > 4
217a89f29f6SJames Bottomley && ahc->features & AHC_LARGE_SCBS
218a89f29f6SJames Bottomley && dma_set_mask(dev, mask_39bit) == 0
219284901a9SYang Hongyang && dma_get_required_mask(dev) > DMA_BIT_MASK(32)) {
2201da177e4SLinus Torvalds ahc->flags |= AHC_39BIT_ADDRESSING;
2211da177e4SLinus Torvalds } else {
222284901a9SYang Hongyang if (dma_set_mask(dev, DMA_BIT_MASK(32))) {
2234a6fae1dSJesper Juhl ahc_free(ahc);
2241da177e4SLinus Torvalds printk(KERN_WARNING "aic7xxx: No suitable DMA available.\n");
2251da177e4SLinus Torvalds return (-ENODEV);
2261da177e4SLinus Torvalds }
2271da177e4SLinus Torvalds }
2281da177e4SLinus Torvalds ahc->dev_softc = pci;
229144ec974SChristoph Hellwig ahc->dev = &pci->dev;
2301da177e4SLinus Torvalds error = ahc_pci_config(ahc, entry);
2311da177e4SLinus Torvalds if (error != 0) {
2321da177e4SLinus Torvalds ahc_free(ahc);
2331da177e4SLinus Torvalds return (-error);
2341da177e4SLinus Torvalds }
2353a4f5c60SJames Bottomley
2363a4f5c60SJames Bottomley /*
2373a4f5c60SJames Bottomley * Second Function PCI devices need to inherit some
2383a4f5c60SJames Bottomley * settings from function 0.
2393a4f5c60SJames Bottomley */
2403a4f5c60SJames Bottomley if ((ahc->features & AHC_MULTI_FUNC) && PCI_FUNC(pdev->devfn) != 0)
2413a4f5c60SJames Bottomley ahc_linux_pci_inherit_flags(ahc);
2423a4f5c60SJames Bottomley
2431da177e4SLinus Torvalds pci_set_drvdata(pdev, ahc);
244*70dfaf84SSu Hui return ahc_linux_register_host(ahc, &aic7xxx_driver_template);
2451da177e4SLinus Torvalds }
2461da177e4SLinus Torvalds
247be0d6768SDenys Vlasenko /******************************* PCI Routines *********************************/
248be0d6768SDenys Vlasenko uint32_t
ahc_pci_read_config(ahc_dev_softc_t pci,int reg,int width)249be0d6768SDenys Vlasenko ahc_pci_read_config(ahc_dev_softc_t pci, int reg, int width)
250be0d6768SDenys Vlasenko {
251be0d6768SDenys Vlasenko switch (width) {
252be0d6768SDenys Vlasenko case 1:
253be0d6768SDenys Vlasenko {
254be0d6768SDenys Vlasenko uint8_t retval;
255be0d6768SDenys Vlasenko
256be0d6768SDenys Vlasenko pci_read_config_byte(pci, reg, &retval);
257be0d6768SDenys Vlasenko return (retval);
258be0d6768SDenys Vlasenko }
259be0d6768SDenys Vlasenko case 2:
260be0d6768SDenys Vlasenko {
261be0d6768SDenys Vlasenko uint16_t retval;
262be0d6768SDenys Vlasenko pci_read_config_word(pci, reg, &retval);
263be0d6768SDenys Vlasenko return (retval);
264be0d6768SDenys Vlasenko }
265be0d6768SDenys Vlasenko case 4:
266be0d6768SDenys Vlasenko {
267be0d6768SDenys Vlasenko uint32_t retval;
268be0d6768SDenys Vlasenko pci_read_config_dword(pci, reg, &retval);
269be0d6768SDenys Vlasenko return (retval);
270be0d6768SDenys Vlasenko }
271be0d6768SDenys Vlasenko default:
272be0d6768SDenys Vlasenko panic("ahc_pci_read_config: Read size too big");
273be0d6768SDenys Vlasenko /* NOTREACHED */
274be0d6768SDenys Vlasenko return (0);
275be0d6768SDenys Vlasenko }
276be0d6768SDenys Vlasenko }
277be0d6768SDenys Vlasenko
278be0d6768SDenys Vlasenko void
ahc_pci_write_config(ahc_dev_softc_t pci,int reg,uint32_t value,int width)279be0d6768SDenys Vlasenko ahc_pci_write_config(ahc_dev_softc_t pci, int reg, uint32_t value, int width)
280be0d6768SDenys Vlasenko {
281be0d6768SDenys Vlasenko switch (width) {
282be0d6768SDenys Vlasenko case 1:
283be0d6768SDenys Vlasenko pci_write_config_byte(pci, reg, value);
284be0d6768SDenys Vlasenko break;
285be0d6768SDenys Vlasenko case 2:
286be0d6768SDenys Vlasenko pci_write_config_word(pci, reg, value);
287be0d6768SDenys Vlasenko break;
288be0d6768SDenys Vlasenko case 4:
289be0d6768SDenys Vlasenko pci_write_config_dword(pci, reg, value);
290be0d6768SDenys Vlasenko break;
291be0d6768SDenys Vlasenko default:
292be0d6768SDenys Vlasenko panic("ahc_pci_write_config: Write size too big");
293be0d6768SDenys Vlasenko /* NOTREACHED */
294be0d6768SDenys Vlasenko }
295be0d6768SDenys Vlasenko }
296be0d6768SDenys Vlasenko
2976897b9a1SVaibhav Gupta static SIMPLE_DEV_PM_OPS(ahc_linux_pci_dev_pm_ops,
2986897b9a1SVaibhav Gupta ahc_linux_pci_dev_suspend,
2996897b9a1SVaibhav Gupta ahc_linux_pci_dev_resume);
300be0d6768SDenys Vlasenko
30103e7925dSFUJITA Tomonori static struct pci_driver aic7xxx_pci_driver = {
30203e7925dSFUJITA Tomonori .name = "aic7xxx",
30303e7925dSFUJITA Tomonori .probe = ahc_linux_pci_dev_probe,
3046897b9a1SVaibhav Gupta .driver.pm = &ahc_linux_pci_dev_pm_ops,
30503e7925dSFUJITA Tomonori .remove = ahc_linux_pci_dev_remove,
30603e7925dSFUJITA Tomonori .id_table = ahc_linux_pci_id_table
30703e7925dSFUJITA Tomonori };
30803e7925dSFUJITA Tomonori
3091da177e4SLinus Torvalds int
ahc_linux_pci_init(void)3101da177e4SLinus Torvalds ahc_linux_pci_init(void)
3111da177e4SLinus Torvalds {
312dcbccbdeSHenrik Kretzschmar return pci_register_driver(&aic7xxx_pci_driver);
3131da177e4SLinus Torvalds }
3141da177e4SLinus Torvalds
3151da177e4SLinus Torvalds void
ahc_linux_pci_exit(void)3161da177e4SLinus Torvalds ahc_linux_pci_exit(void)
3171da177e4SLinus Torvalds {
3181da177e4SLinus Torvalds pci_unregister_driver(&aic7xxx_pci_driver);
3191da177e4SLinus Torvalds }
3201da177e4SLinus Torvalds
3211da177e4SLinus Torvalds static int
ahc_linux_pci_reserve_io_region(struct ahc_softc * ahc,resource_size_t * base)32244850413SSergei Shtylyov ahc_linux_pci_reserve_io_region(struct ahc_softc *ahc, resource_size_t *base)
3231da177e4SLinus Torvalds {
3241da177e4SLinus Torvalds if (aic7xxx_allow_memio == 0)
3251da177e4SLinus Torvalds return (ENOMEM);
3261da177e4SLinus Torvalds
3271da177e4SLinus Torvalds *base = pci_resource_start(ahc->dev_softc, 0);
3281da177e4SLinus Torvalds if (*base == 0)
3291da177e4SLinus Torvalds return (ENOMEM);
330172c122dSHarvey Harrison if (!request_region(*base, 256, "aic7xxx"))
3311da177e4SLinus Torvalds return (ENOMEM);
3321da177e4SLinus Torvalds return (0);
3331da177e4SLinus Torvalds }
3341da177e4SLinus Torvalds
3351da177e4SLinus Torvalds static int
ahc_linux_pci_reserve_mem_region(struct ahc_softc * ahc,resource_size_t * bus_addr,uint8_t __iomem ** maddr)3361da177e4SLinus Torvalds ahc_linux_pci_reserve_mem_region(struct ahc_softc *ahc,
33744850413SSergei Shtylyov resource_size_t *bus_addr,
3381da177e4SLinus Torvalds uint8_t __iomem **maddr)
3391da177e4SLinus Torvalds {
34044850413SSergei Shtylyov resource_size_t start;
3411da177e4SLinus Torvalds int error;
3421da177e4SLinus Torvalds
3431da177e4SLinus Torvalds error = 0;
3441da177e4SLinus Torvalds start = pci_resource_start(ahc->dev_softc, 1);
3451da177e4SLinus Torvalds if (start != 0) {
3461da177e4SLinus Torvalds *bus_addr = start;
347172c122dSHarvey Harrison if (!request_mem_region(start, 0x1000, "aic7xxx"))
3481da177e4SLinus Torvalds error = ENOMEM;
3491da177e4SLinus Torvalds if (error == 0) {
3504bdc0d67SChristoph Hellwig *maddr = ioremap(start, 256);
3511da177e4SLinus Torvalds if (*maddr == NULL) {
3521da177e4SLinus Torvalds error = ENOMEM;
3531da177e4SLinus Torvalds release_mem_region(start, 0x1000);
3541da177e4SLinus Torvalds }
3551da177e4SLinus Torvalds }
3561da177e4SLinus Torvalds } else
3571da177e4SLinus Torvalds error = ENOMEM;
3581da177e4SLinus Torvalds return (error);
3591da177e4SLinus Torvalds }
3601da177e4SLinus Torvalds
3611da177e4SLinus Torvalds int
ahc_pci_map_registers(struct ahc_softc * ahc)3621da177e4SLinus Torvalds ahc_pci_map_registers(struct ahc_softc *ahc)
3631da177e4SLinus Torvalds {
3641da177e4SLinus Torvalds uint32_t command;
36544850413SSergei Shtylyov resource_size_t base;
3661da177e4SLinus Torvalds uint8_t __iomem *maddr;
3671da177e4SLinus Torvalds int error;
3681da177e4SLinus Torvalds
3691da177e4SLinus Torvalds /*
3701da177e4SLinus Torvalds * If its allowed, we prefer memory mapped access.
3711da177e4SLinus Torvalds */
3721da177e4SLinus Torvalds command = ahc_pci_read_config(ahc->dev_softc, PCIR_COMMAND, 4);
3731da177e4SLinus Torvalds command &= ~(PCIM_CMD_PORTEN|PCIM_CMD_MEMEN);
3741da177e4SLinus Torvalds base = 0;
3751da177e4SLinus Torvalds maddr = NULL;
3761da177e4SLinus Torvalds error = ahc_linux_pci_reserve_mem_region(ahc, &base, &maddr);
3771da177e4SLinus Torvalds if (error == 0) {
3781da177e4SLinus Torvalds ahc->platform_data->mem_busaddr = base;
3791da177e4SLinus Torvalds ahc->tag = BUS_SPACE_MEMIO;
3801da177e4SLinus Torvalds ahc->bsh.maddr = maddr;
3811da177e4SLinus Torvalds ahc_pci_write_config(ahc->dev_softc, PCIR_COMMAND,
3821da177e4SLinus Torvalds command | PCIM_CMD_MEMEN, 4);
3831da177e4SLinus Torvalds
3841da177e4SLinus Torvalds /*
3851da177e4SLinus Torvalds * Do a quick test to see if memory mapped
3861da177e4SLinus Torvalds * I/O is functioning correctly.
3871da177e4SLinus Torvalds */
3881da177e4SLinus Torvalds if (ahc_pci_test_register_access(ahc) != 0) {
3891da177e4SLinus Torvalds
39048813cf9SPekka Enberg printk("aic7xxx: PCI Device %d:%d:%d "
3911da177e4SLinus Torvalds "failed memory mapped test. Using PIO.\n",
3921da177e4SLinus Torvalds ahc_get_pci_bus(ahc->dev_softc),
3931da177e4SLinus Torvalds ahc_get_pci_slot(ahc->dev_softc),
3941da177e4SLinus Torvalds ahc_get_pci_function(ahc->dev_softc));
3951da177e4SLinus Torvalds iounmap(maddr);
3961da177e4SLinus Torvalds release_mem_region(ahc->platform_data->mem_busaddr,
3971da177e4SLinus Torvalds 0x1000);
3981da177e4SLinus Torvalds ahc->bsh.maddr = NULL;
3991da177e4SLinus Torvalds maddr = NULL;
4001da177e4SLinus Torvalds } else
4011da177e4SLinus Torvalds command |= PCIM_CMD_MEMEN;
4021da177e4SLinus Torvalds } else {
40348813cf9SPekka Enberg printk("aic7xxx: PCI%d:%d:%d MEM region 0x%llx "
4041da177e4SLinus Torvalds "unavailable. Cannot memory map device.\n",
4051da177e4SLinus Torvalds ahc_get_pci_bus(ahc->dev_softc),
4061da177e4SLinus Torvalds ahc_get_pci_slot(ahc->dev_softc),
4071da177e4SLinus Torvalds ahc_get_pci_function(ahc->dev_softc),
40844850413SSergei Shtylyov (unsigned long long)base);
4091da177e4SLinus Torvalds }
4101da177e4SLinus Torvalds
4111da177e4SLinus Torvalds /*
4121da177e4SLinus Torvalds * We always prefer memory mapped access.
4131da177e4SLinus Torvalds */
4141da177e4SLinus Torvalds if (maddr == NULL) {
4151da177e4SLinus Torvalds
4161da177e4SLinus Torvalds error = ahc_linux_pci_reserve_io_region(ahc, &base);
4171da177e4SLinus Torvalds if (error == 0) {
4181da177e4SLinus Torvalds ahc->tag = BUS_SPACE_PIO;
41944850413SSergei Shtylyov ahc->bsh.ioport = (u_long)base;
4201da177e4SLinus Torvalds command |= PCIM_CMD_PORTEN;
4211da177e4SLinus Torvalds } else {
42248813cf9SPekka Enberg printk("aic7xxx: PCI%d:%d:%d IO region 0x%llx[0..255] "
4231da177e4SLinus Torvalds "unavailable. Cannot map device.\n",
4241da177e4SLinus Torvalds ahc_get_pci_bus(ahc->dev_softc),
4251da177e4SLinus Torvalds ahc_get_pci_slot(ahc->dev_softc),
4261da177e4SLinus Torvalds ahc_get_pci_function(ahc->dev_softc),
42744850413SSergei Shtylyov (unsigned long long)base);
4281da177e4SLinus Torvalds }
4291da177e4SLinus Torvalds }
4301da177e4SLinus Torvalds ahc_pci_write_config(ahc->dev_softc, PCIR_COMMAND, command, 4);
4311da177e4SLinus Torvalds return (error);
4321da177e4SLinus Torvalds }
4331da177e4SLinus Torvalds
4341da177e4SLinus Torvalds int
ahc_pci_map_int(struct ahc_softc * ahc)4351da177e4SLinus Torvalds ahc_pci_map_int(struct ahc_softc *ahc)
4361da177e4SLinus Torvalds {
4371da177e4SLinus Torvalds int error;
4381da177e4SLinus Torvalds
4391da177e4SLinus Torvalds error = request_irq(ahc->dev_softc->irq, ahc_linux_isr,
4401d6f359aSThomas Gleixner IRQF_SHARED, "aic7xxx", ahc);
4411da177e4SLinus Torvalds if (error == 0)
4421da177e4SLinus Torvalds ahc->platform_data->irq = ahc->dev_softc->irq;
4431da177e4SLinus Torvalds
4441da177e4SLinus Torvalds return (-error);
4451da177e4SLinus Torvalds }
4461da177e4SLinus Torvalds
447