Path: blob/master/arch/sh/drivers/pci/ops-sh7786.c
10819 views
/*1* Generic SH7786 PCI-Express operations.2*3* Copyright (C) 2009 - 2010 Paul Mundt4*5* This file is subject to the terms and conditions of the GNU General Public6* License v2. See the file "COPYING" in the main directory of this archive7* for more details.8*/9#include <linux/kernel.h>10#include <linux/init.h>11#include <linux/pci.h>12#include <linux/io.h>13#include <linux/spinlock.h>14#include "pcie-sh7786.h"1516enum {17PCI_ACCESS_READ,18PCI_ACCESS_WRITE,19};2021static int sh7786_pcie_config_access(unsigned char access_type,22struct pci_bus *bus, unsigned int devfn, int where, u32 *data)23{24struct pci_channel *chan = bus->sysdata;25int dev, func, type, reg;2627dev = PCI_SLOT(devfn);28func = PCI_FUNC(devfn);29type = !!bus->parent;30reg = where & ~3;3132if (bus->number > 255 || dev > 31 || func > 7)33return PCIBIOS_FUNC_NOT_SUPPORTED;3435/*36* While each channel has its own memory-mapped extended config37* space, it's generally only accessible when in endpoint mode.38* When in root complex mode, the controller is unable to target39* itself with either type 0 or type 1 accesses, and indeed, any40* controller initiated target transfer to its own config space41* result in a completer abort.42*43* Each channel effectively only supports a single device, but as44* the same channel <-> device access works for any PCI_SLOT()45* value, we cheat a bit here and bind the controller's config46* space to devfn 0 in order to enable self-enumeration. In this47* case the regular PAR/PDR path is sidelined and the mangled48* config access itself is initiated as a SuperHyway transaction.49*/50if (pci_is_root_bus(bus)) {51if (dev == 0) {52if (access_type == PCI_ACCESS_READ)53*data = pci_read_reg(chan, PCI_REG(reg));54else55pci_write_reg(chan, *data, PCI_REG(reg));5657return PCIBIOS_SUCCESSFUL;58} else if (dev > 1)59return PCIBIOS_DEVICE_NOT_FOUND;60}6162/* Clear errors */63pci_write_reg(chan, pci_read_reg(chan, SH4A_PCIEERRFR), SH4A_PCIEERRFR);6465/* Set the PIO address */66pci_write_reg(chan, (bus->number << 24) | (dev << 19) |67(func << 16) | reg, SH4A_PCIEPAR);6869/* Enable the configuration access */70pci_write_reg(chan, (1 << 31) | (type << 8), SH4A_PCIEPCTLR);7172/* Check for errors */73if (pci_read_reg(chan, SH4A_PCIEERRFR) & 0x10)74return PCIBIOS_DEVICE_NOT_FOUND;7576/* Check for master and target aborts */77if (pci_read_reg(chan, SH4A_PCIEPCICONF1) & ((1 << 29) | (1 << 28)))78return PCIBIOS_DEVICE_NOT_FOUND;7980if (access_type == PCI_ACCESS_READ)81*data = pci_read_reg(chan, SH4A_PCIEPDR);82else83pci_write_reg(chan, *data, SH4A_PCIEPDR);8485/* Disable the configuration access */86pci_write_reg(chan, 0, SH4A_PCIEPCTLR);8788return PCIBIOS_SUCCESSFUL;89}9091static int sh7786_pcie_read(struct pci_bus *bus, unsigned int devfn,92int where, int size, u32 *val)93{94unsigned long flags;95int ret;96u32 data;9798if ((size == 2) && (where & 1))99return PCIBIOS_BAD_REGISTER_NUMBER;100else if ((size == 4) && (where & 3))101return PCIBIOS_BAD_REGISTER_NUMBER;102103raw_spin_lock_irqsave(&pci_config_lock, flags);104ret = sh7786_pcie_config_access(PCI_ACCESS_READ, bus,105devfn, where, &data);106if (ret != PCIBIOS_SUCCESSFUL) {107*val = 0xffffffff;108goto out;109}110111if (size == 1)112*val = (data >> ((where & 3) << 3)) & 0xff;113else if (size == 2)114*val = (data >> ((where & 2) << 3)) & 0xffff;115else116*val = data;117118dev_dbg(&bus->dev, "pcie-config-read: bus=%3d devfn=0x%04x "119"where=0x%04x size=%d val=0x%08lx\n", bus->number,120devfn, where, size, (unsigned long)*val);121122out:123raw_spin_unlock_irqrestore(&pci_config_lock, flags);124return ret;125}126127static int sh7786_pcie_write(struct pci_bus *bus, unsigned int devfn,128int where, int size, u32 val)129{130unsigned long flags;131int shift, ret;132u32 data;133134if ((size == 2) && (where & 1))135return PCIBIOS_BAD_REGISTER_NUMBER;136else if ((size == 4) && (where & 3))137return PCIBIOS_BAD_REGISTER_NUMBER;138139raw_spin_lock_irqsave(&pci_config_lock, flags);140ret = sh7786_pcie_config_access(PCI_ACCESS_READ, bus,141devfn, where, &data);142if (ret != PCIBIOS_SUCCESSFUL)143goto out;144145dev_dbg(&bus->dev, "pcie-config-write: bus=%3d devfn=0x%04x "146"where=0x%04x size=%d val=%08lx\n", bus->number,147devfn, where, size, (unsigned long)val);148149if (size == 1) {150shift = (where & 3) << 3;151data &= ~(0xff << shift);152data |= ((val & 0xff) << shift);153} else if (size == 2) {154shift = (where & 2) << 3;155data &= ~(0xffff << shift);156data |= ((val & 0xffff) << shift);157} else158data = val;159160ret = sh7786_pcie_config_access(PCI_ACCESS_WRITE, bus,161devfn, where, &data);162out:163raw_spin_unlock_irqrestore(&pci_config_lock, flags);164return ret;165}166167struct pci_ops sh7786_pci_ops = {168.read = sh7786_pcie_read,169.write = sh7786_pcie_write,170};171172173