1*252161ecSYoichi Yuasa /* 2*252161ecSYoichi Yuasa * Copyright (C) 1999, 2000, 2004 MIPS Technologies, Inc. 3*252161ecSYoichi Yuasa * All rights reserved. 4*252161ecSYoichi Yuasa * Authors: Carsten Langgaard <carstenl@mips.com> 5*252161ecSYoichi Yuasa * Maciej W. Rozycki <macro@mips.com> 6*252161ecSYoichi Yuasa * 7*252161ecSYoichi Yuasa * This program is free software; you can distribute it and/or modify it 8*252161ecSYoichi Yuasa * under the terms of the GNU General Public License (Version 2) as 9*252161ecSYoichi Yuasa * published by the Free Software Foundation. 10*252161ecSYoichi Yuasa * 11*252161ecSYoichi Yuasa * This program is distributed in the hope it will be useful, but WITHOUT 12*252161ecSYoichi Yuasa * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13*252161ecSYoichi Yuasa * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14*252161ecSYoichi Yuasa * for more details. 15*252161ecSYoichi Yuasa * 16*252161ecSYoichi Yuasa * You should have received a copy of the GNU General Public License along 17*252161ecSYoichi Yuasa * with this program; if not, write to the Free Software Foundation, Inc., 18*252161ecSYoichi Yuasa * 59 Temple Place - Suite 330, Boston MA 02111-1307, USA. 19*252161ecSYoichi Yuasa */ 20*252161ecSYoichi Yuasa #include <linux/types.h> 21*252161ecSYoichi Yuasa #include <linux/pci.h> 22*252161ecSYoichi Yuasa #include <linux/kernel.h> 23*252161ecSYoichi Yuasa 24*252161ecSYoichi Yuasa #include <asm/gt64120.h> 25*252161ecSYoichi Yuasa 26*252161ecSYoichi Yuasa #define PCI_ACCESS_READ 0 27*252161ecSYoichi Yuasa #define PCI_ACCESS_WRITE 1 28*252161ecSYoichi Yuasa 29*252161ecSYoichi Yuasa /* 30*252161ecSYoichi Yuasa * PCI configuration cycle AD bus definition 31*252161ecSYoichi Yuasa */ 32*252161ecSYoichi Yuasa /* Type 0 */ 33*252161ecSYoichi Yuasa #define PCI_CFG_TYPE0_REG_SHF 0 34*252161ecSYoichi Yuasa #define PCI_CFG_TYPE0_FUNC_SHF 8 35*252161ecSYoichi Yuasa 36*252161ecSYoichi Yuasa /* Type 1 */ 37*252161ecSYoichi Yuasa #define PCI_CFG_TYPE1_REG_SHF 0 38*252161ecSYoichi Yuasa #define PCI_CFG_TYPE1_FUNC_SHF 8 39*252161ecSYoichi Yuasa #define PCI_CFG_TYPE1_DEV_SHF 11 40*252161ecSYoichi Yuasa #define PCI_CFG_TYPE1_BUS_SHF 16 41*252161ecSYoichi Yuasa 42*252161ecSYoichi Yuasa static int gt64xxx_pci0_pcibios_config_access(unsigned char access_type, 43*252161ecSYoichi Yuasa struct pci_bus *bus, unsigned int devfn, int where, u32 * data) 44*252161ecSYoichi Yuasa { 45*252161ecSYoichi Yuasa unsigned char busnum = bus->number; 46*252161ecSYoichi Yuasa u32 intr; 47*252161ecSYoichi Yuasa 48*252161ecSYoichi Yuasa if ((busnum == 0) && (devfn >= PCI_DEVFN(31, 0))) 49*252161ecSYoichi Yuasa return -1; /* Because of a bug in the galileo (for slot 31). */ 50*252161ecSYoichi Yuasa 51*252161ecSYoichi Yuasa /* Clear cause register bits */ 52*252161ecSYoichi Yuasa GT_WRITE(GT_INTRCAUSE_OFS, ~(GT_INTRCAUSE_MASABORT0_BIT | 53*252161ecSYoichi Yuasa GT_INTRCAUSE_TARABORT0_BIT)); 54*252161ecSYoichi Yuasa 55*252161ecSYoichi Yuasa /* Setup address */ 56*252161ecSYoichi Yuasa GT_WRITE(GT_PCI0_CFGADDR_OFS, 57*252161ecSYoichi Yuasa (busnum << GT_PCI0_CFGADDR_BUSNUM_SHF) | 58*252161ecSYoichi Yuasa (devfn << GT_PCI0_CFGADDR_FUNCTNUM_SHF) | 59*252161ecSYoichi Yuasa ((where / 4) << GT_PCI0_CFGADDR_REGNUM_SHF) | 60*252161ecSYoichi Yuasa GT_PCI0_CFGADDR_CONFIGEN_BIT); 61*252161ecSYoichi Yuasa 62*252161ecSYoichi Yuasa if (access_type == PCI_ACCESS_WRITE) { 63*252161ecSYoichi Yuasa if (busnum == 0 && PCI_SLOT(devfn) == 0) { 64*252161ecSYoichi Yuasa /* 65*252161ecSYoichi Yuasa * The Galileo system controller is acting 66*252161ecSYoichi Yuasa * differently than other devices. 67*252161ecSYoichi Yuasa */ 68*252161ecSYoichi Yuasa GT_WRITE(GT_PCI0_CFGDATA_OFS, *data); 69*252161ecSYoichi Yuasa } else 70*252161ecSYoichi Yuasa __GT_WRITE(GT_PCI0_CFGDATA_OFS, *data); 71*252161ecSYoichi Yuasa } else { 72*252161ecSYoichi Yuasa if (busnum == 0 && PCI_SLOT(devfn) == 0) { 73*252161ecSYoichi Yuasa /* 74*252161ecSYoichi Yuasa * The Galileo system controller is acting 75*252161ecSYoichi Yuasa * differently than other devices. 76*252161ecSYoichi Yuasa */ 77*252161ecSYoichi Yuasa *data = GT_READ(GT_PCI0_CFGDATA_OFS); 78*252161ecSYoichi Yuasa } else 79*252161ecSYoichi Yuasa *data = __GT_READ(GT_PCI0_CFGDATA_OFS); 80*252161ecSYoichi Yuasa } 81*252161ecSYoichi Yuasa 82*252161ecSYoichi Yuasa /* Check for master or target abort */ 83*252161ecSYoichi Yuasa intr = GT_READ(GT_INTRCAUSE_OFS); 84*252161ecSYoichi Yuasa 85*252161ecSYoichi Yuasa if (intr & (GT_INTRCAUSE_MASABORT0_BIT | GT_INTRCAUSE_TARABORT0_BIT)) { 86*252161ecSYoichi Yuasa /* Error occurred */ 87*252161ecSYoichi Yuasa 88*252161ecSYoichi Yuasa /* Clear bits */ 89*252161ecSYoichi Yuasa GT_WRITE(GT_INTRCAUSE_OFS, ~(GT_INTRCAUSE_MASABORT0_BIT | 90*252161ecSYoichi Yuasa GT_INTRCAUSE_TARABORT0_BIT)); 91*252161ecSYoichi Yuasa 92*252161ecSYoichi Yuasa return -1; 93*252161ecSYoichi Yuasa } 94*252161ecSYoichi Yuasa 95*252161ecSYoichi Yuasa return 0; 96*252161ecSYoichi Yuasa } 97*252161ecSYoichi Yuasa 98*252161ecSYoichi Yuasa 99*252161ecSYoichi Yuasa /* 100*252161ecSYoichi Yuasa * We can't address 8 and 16 bit words directly. Instead we have to 101*252161ecSYoichi Yuasa * read/write a 32bit word and mask/modify the data we actually want. 102*252161ecSYoichi Yuasa */ 103*252161ecSYoichi Yuasa static int gt64xxx_pci0_pcibios_read(struct pci_bus *bus, unsigned int devfn, 104*252161ecSYoichi Yuasa int where, int size, u32 * val) 105*252161ecSYoichi Yuasa { 106*252161ecSYoichi Yuasa u32 data = 0; 107*252161ecSYoichi Yuasa 108*252161ecSYoichi Yuasa if (gt64xxx_pci0_pcibios_config_access(PCI_ACCESS_READ, bus, devfn, 109*252161ecSYoichi Yuasa where, &data)) 110*252161ecSYoichi Yuasa return PCIBIOS_DEVICE_NOT_FOUND; 111*252161ecSYoichi Yuasa 112*252161ecSYoichi Yuasa if (size == 1) 113*252161ecSYoichi Yuasa *val = (data >> ((where & 3) << 3)) & 0xff; 114*252161ecSYoichi Yuasa else if (size == 2) 115*252161ecSYoichi Yuasa *val = (data >> ((where & 3) << 3)) & 0xffff; 116*252161ecSYoichi Yuasa else 117*252161ecSYoichi Yuasa *val = data; 118*252161ecSYoichi Yuasa 119*252161ecSYoichi Yuasa return PCIBIOS_SUCCESSFUL; 120*252161ecSYoichi Yuasa } 121*252161ecSYoichi Yuasa 122*252161ecSYoichi Yuasa static int gt64xxx_pci0_pcibios_write(struct pci_bus *bus, unsigned int devfn, 123*252161ecSYoichi Yuasa int where, int size, u32 val) 124*252161ecSYoichi Yuasa { 125*252161ecSYoichi Yuasa u32 data = 0; 126*252161ecSYoichi Yuasa 127*252161ecSYoichi Yuasa if (size == 4) 128*252161ecSYoichi Yuasa data = val; 129*252161ecSYoichi Yuasa else { 130*252161ecSYoichi Yuasa if (gt64xxx_pci0_pcibios_config_access(PCI_ACCESS_READ, bus, 131*252161ecSYoichi Yuasa devfn, where, &data)) 132*252161ecSYoichi Yuasa return PCIBIOS_DEVICE_NOT_FOUND; 133*252161ecSYoichi Yuasa 134*252161ecSYoichi Yuasa if (size == 1) 135*252161ecSYoichi Yuasa data = (data & ~(0xff << ((where & 3) << 3))) | 136*252161ecSYoichi Yuasa (val << ((where & 3) << 3)); 137*252161ecSYoichi Yuasa else if (size == 2) 138*252161ecSYoichi Yuasa data = (data & ~(0xffff << ((where & 3) << 3))) | 139*252161ecSYoichi Yuasa (val << ((where & 3) << 3)); 140*252161ecSYoichi Yuasa } 141*252161ecSYoichi Yuasa 142*252161ecSYoichi Yuasa if (gt64xxx_pci0_pcibios_config_access(PCI_ACCESS_WRITE, bus, devfn, 143*252161ecSYoichi Yuasa where, &data)) 144*252161ecSYoichi Yuasa return PCIBIOS_DEVICE_NOT_FOUND; 145*252161ecSYoichi Yuasa 146*252161ecSYoichi Yuasa return PCIBIOS_SUCCESSFUL; 147*252161ecSYoichi Yuasa } 148*252161ecSYoichi Yuasa 149*252161ecSYoichi Yuasa struct pci_ops gt64xxx_pci0_ops = { 150*252161ecSYoichi Yuasa .read = gt64xxx_pci0_pcibios_read, 151*252161ecSYoichi Yuasa .write = gt64xxx_pci0_pcibios_write 152*252161ecSYoichi Yuasa }; 153