1e47d4889SJohn Crispin /* 2e47d4889SJohn Crispin * This program is free software; you can redistribute it and/or modify it 3e47d4889SJohn Crispin * under the terms of the GNU General Public License version 2 as published 4e47d4889SJohn Crispin * by the Free Software Foundation. 5e47d4889SJohn Crispin * 6*97b92108SJohn Crispin * Copyright (C) 2010 John Crispin <john@phrozen.org> 7e47d4889SJohn Crispin */ 8e47d4889SJohn Crispin 9e47d4889SJohn Crispin #include <linux/types.h> 10e47d4889SJohn Crispin #include <linux/pci.h> 11e47d4889SJohn Crispin #include <linux/kernel.h> 12e47d4889SJohn Crispin #include <linux/delay.h> 13e47d4889SJohn Crispin #include <linux/mm.h> 14e47d4889SJohn Crispin #include <asm/addrspace.h> 15e47d4889SJohn Crispin #include <linux/vmalloc.h> 16e47d4889SJohn Crispin 17e47d4889SJohn Crispin #include <lantiq_soc.h> 18e47d4889SJohn Crispin 19e47d4889SJohn Crispin #include "pci-lantiq.h" 20e47d4889SJohn Crispin 21e47d4889SJohn Crispin #define LTQ_PCI_CFG_BUSNUM_SHF 16 22e47d4889SJohn Crispin #define LTQ_PCI_CFG_DEVNUM_SHF 11 23e47d4889SJohn Crispin #define LTQ_PCI_CFG_FUNNUM_SHF 8 24e47d4889SJohn Crispin 25e47d4889SJohn Crispin #define PCI_ACCESS_READ 0 26e47d4889SJohn Crispin #define PCI_ACCESS_WRITE 1 27e47d4889SJohn Crispin 28e47d4889SJohn Crispin static int ltq_pci_config_access(unsigned char access_type, struct pci_bus *bus, 29e47d4889SJohn Crispin unsigned int devfn, unsigned int where, u32 *data) 30e47d4889SJohn Crispin { 31e47d4889SJohn Crispin unsigned long cfg_base; 32e47d4889SJohn Crispin unsigned long flags; 33e47d4889SJohn Crispin u32 temp; 34e47d4889SJohn Crispin 35e47d4889SJohn Crispin /* we support slot from 0 to 15 dev_fn & 0x68 (AD29) is the 36e47d4889SJohn Crispin SoC itself */ 37e47d4889SJohn Crispin if ((bus->number != 0) || ((devfn & 0xf8) > 0x78) 38e47d4889SJohn Crispin || ((devfn & 0xf8) == 0) || ((devfn & 0xf8) == 0x68)) 39e47d4889SJohn Crispin return 1; 40e47d4889SJohn Crispin 41e47d4889SJohn Crispin spin_lock_irqsave(&ebu_lock, flags); 42e47d4889SJohn Crispin 43e47d4889SJohn Crispin cfg_base = (unsigned long) ltq_pci_mapped_cfg; 44e47d4889SJohn Crispin cfg_base |= (bus->number << LTQ_PCI_CFG_BUSNUM_SHF) | (devfn << 45e47d4889SJohn Crispin LTQ_PCI_CFG_FUNNUM_SHF) | (where & ~0x3); 46e47d4889SJohn Crispin 47e47d4889SJohn Crispin /* Perform access */ 48e47d4889SJohn Crispin if (access_type == PCI_ACCESS_WRITE) { 49e47d4889SJohn Crispin ltq_w32(swab32(*data), ((u32 *)cfg_base)); 50e47d4889SJohn Crispin } else { 51e47d4889SJohn Crispin *data = ltq_r32(((u32 *)(cfg_base))); 52e47d4889SJohn Crispin *data = swab32(*data); 53e47d4889SJohn Crispin } 54e47d4889SJohn Crispin wmb(); 55e47d4889SJohn Crispin 56e47d4889SJohn Crispin /* clean possible Master abort */ 57e47d4889SJohn Crispin cfg_base = (unsigned long) ltq_pci_mapped_cfg; 58e47d4889SJohn Crispin cfg_base |= (0x0 << LTQ_PCI_CFG_FUNNUM_SHF) + 4; 59e47d4889SJohn Crispin temp = ltq_r32(((u32 *)(cfg_base))); 60e47d4889SJohn Crispin temp = swab32(temp); 61e47d4889SJohn Crispin cfg_base = (unsigned long) ltq_pci_mapped_cfg; 62e47d4889SJohn Crispin cfg_base |= (0x68 << LTQ_PCI_CFG_FUNNUM_SHF) + 4; 63e47d4889SJohn Crispin ltq_w32(temp, ((u32 *)cfg_base)); 64e47d4889SJohn Crispin 65e47d4889SJohn Crispin spin_unlock_irqrestore(&ebu_lock, flags); 66e47d4889SJohn Crispin 67e47d4889SJohn Crispin if (((*data) == 0xffffffff) && (access_type == PCI_ACCESS_READ)) 68e47d4889SJohn Crispin return 1; 69e47d4889SJohn Crispin 70e47d4889SJohn Crispin return 0; 71e47d4889SJohn Crispin } 72e47d4889SJohn Crispin 73e47d4889SJohn Crispin int ltq_pci_read_config_dword(struct pci_bus *bus, unsigned int devfn, 74e47d4889SJohn Crispin int where, int size, u32 *val) 75e47d4889SJohn Crispin { 76e47d4889SJohn Crispin u32 data = 0; 77e47d4889SJohn Crispin 78e47d4889SJohn Crispin if (ltq_pci_config_access(PCI_ACCESS_READ, bus, devfn, where, &data)) 79e47d4889SJohn Crispin return PCIBIOS_DEVICE_NOT_FOUND; 80e47d4889SJohn Crispin 81e47d4889SJohn Crispin if (size == 1) 82e47d4889SJohn Crispin *val = (data >> ((where & 3) << 3)) & 0xff; 83e47d4889SJohn Crispin else if (size == 2) 84e47d4889SJohn Crispin *val = (data >> ((where & 3) << 3)) & 0xffff; 85e47d4889SJohn Crispin else 86e47d4889SJohn Crispin *val = data; 87e47d4889SJohn Crispin 88e47d4889SJohn Crispin return PCIBIOS_SUCCESSFUL; 89e47d4889SJohn Crispin } 90e47d4889SJohn Crispin 91e47d4889SJohn Crispin int ltq_pci_write_config_dword(struct pci_bus *bus, unsigned int devfn, 92e47d4889SJohn Crispin int where, int size, u32 val) 93e47d4889SJohn Crispin { 94e47d4889SJohn Crispin u32 data = 0; 95e47d4889SJohn Crispin 96e47d4889SJohn Crispin if (size == 4) { 97e47d4889SJohn Crispin data = val; 98e47d4889SJohn Crispin } else { 99e47d4889SJohn Crispin if (ltq_pci_config_access(PCI_ACCESS_READ, bus, 100e47d4889SJohn Crispin devfn, where, &data)) 101e47d4889SJohn Crispin return PCIBIOS_DEVICE_NOT_FOUND; 102e47d4889SJohn Crispin 103e47d4889SJohn Crispin if (size == 1) 104e47d4889SJohn Crispin data = (data & ~(0xff << ((where & 3) << 3))) | 105e47d4889SJohn Crispin (val << ((where & 3) << 3)); 106e47d4889SJohn Crispin else if (size == 2) 107e47d4889SJohn Crispin data = (data & ~(0xffff << ((where & 3) << 3))) | 108e47d4889SJohn Crispin (val << ((where & 3) << 3)); 109e47d4889SJohn Crispin } 110e47d4889SJohn Crispin 111e47d4889SJohn Crispin if (ltq_pci_config_access(PCI_ACCESS_WRITE, bus, devfn, where, &data)) 112e47d4889SJohn Crispin return PCIBIOS_DEVICE_NOT_FOUND; 113e47d4889SJohn Crispin 114e47d4889SJohn Crispin return PCIBIOS_SUCCESSFUL; 115e47d4889SJohn Crispin } 116