Paul Mundt | 66765fe | 2009-06-16 06:26:08 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Generic SH7786 PCI-Express operations. |
| 3 | * |
Paul Mundt | 7656e24 | 2010-08-20 15:59:40 +0900 | [diff] [blame] | 4 | * Copyright (C) 2009 - 2010 Paul Mundt |
Paul Mundt | 66765fe | 2009-06-16 06:26:08 +0900 | [diff] [blame] | 5 | * |
| 6 | * This file is subject to the terms and conditions of the GNU General Public |
| 7 | * License v2. See the file "COPYING" in the main directory of this archive |
| 8 | * for more details. |
| 9 | */ |
| 10 | #include <linux/kernel.h> |
| 11 | #include <linux/init.h> |
| 12 | #include <linux/pci.h> |
| 13 | #include <linux/io.h> |
| 14 | #include <linux/spinlock.h> |
| 15 | #include "pcie-sh7786.h" |
| 16 | |
| 17 | enum { |
| 18 | PCI_ACCESS_READ, |
| 19 | PCI_ACCESS_WRITE, |
| 20 | }; |
| 21 | |
| 22 | static DEFINE_SPINLOCK(sh7786_pcie_lock); |
| 23 | |
| 24 | static int sh7786_pcie_config_access(unsigned char access_type, |
| 25 | struct pci_bus *bus, unsigned int devfn, int where, u32 *data) |
| 26 | { |
| 27 | struct pci_channel *chan = bus->sysdata; |
Paul Mundt | 65c23f5 | 2010-08-20 20:26:41 +0900 | [diff] [blame] | 28 | int dev, func, type; |
Paul Mundt | 66765fe | 2009-06-16 06:26:08 +0900 | [diff] [blame] | 29 | |
| 30 | dev = PCI_SLOT(devfn); |
| 31 | func = PCI_FUNC(devfn); |
Paul Mundt | 65c23f5 | 2010-08-20 20:26:41 +0900 | [diff] [blame] | 32 | type = !!bus->parent; |
Paul Mundt | 66765fe | 2009-06-16 06:26:08 +0900 | [diff] [blame] | 33 | |
| 34 | if (bus->number > 255 || dev > 31 || func > 7) |
| 35 | return PCIBIOS_FUNC_NOT_SUPPORTED; |
Paul Mundt | 65c23f5 | 2010-08-20 20:26:41 +0900 | [diff] [blame] | 36 | if (bus->parent == NULL && dev) |
Paul Mundt | 66765fe | 2009-06-16 06:26:08 +0900 | [diff] [blame] | 37 | return PCIBIOS_DEVICE_NOT_FOUND; |
| 38 | |
Paul Mundt | 7656e24 | 2010-08-20 15:59:40 +0900 | [diff] [blame] | 39 | /* Clear errors */ |
| 40 | pci_write_reg(chan, pci_read_reg(chan, SH4A_PCIEERRFR), SH4A_PCIEERRFR); |
| 41 | |
Paul Mundt | 66765fe | 2009-06-16 06:26:08 +0900 | [diff] [blame] | 42 | /* Set the PIO address */ |
| 43 | pci_write_reg(chan, (bus->number << 24) | (dev << 19) | |
| 44 | (func << 16) | (where & ~3), SH4A_PCIEPAR); |
| 45 | |
| 46 | /* Enable the configuration access */ |
Paul Mundt | 65c23f5 | 2010-08-20 20:26:41 +0900 | [diff] [blame] | 47 | pci_write_reg(chan, (1 << 31) | (type << 8), SH4A_PCIEPCTLR); |
Paul Mundt | 7656e24 | 2010-08-20 15:59:40 +0900 | [diff] [blame] | 48 | |
| 49 | /* Check for errors */ |
| 50 | if (pci_read_reg(chan, SH4A_PCIEERRFR) & 0x10) |
| 51 | return PCIBIOS_DEVICE_NOT_FOUND; |
| 52 | /* Check for master and target aborts */ |
| 53 | if (pci_read_reg(chan, SH4A_PCIEPCICONF1) & ((1 << 29) | (1 << 28))) |
| 54 | return PCIBIOS_DEVICE_NOT_FOUND; |
Paul Mundt | 66765fe | 2009-06-16 06:26:08 +0900 | [diff] [blame] | 55 | |
| 56 | if (access_type == PCI_ACCESS_READ) |
| 57 | *data = pci_read_reg(chan, SH4A_PCIEPDR); |
| 58 | else |
| 59 | pci_write_reg(chan, *data, SH4A_PCIEPDR); |
| 60 | |
Paul Mundt | bdf7499 | 2010-09-19 13:54:50 +0900 | [diff] [blame^] | 61 | /* Disable the configuration access */ |
| 62 | pci_write_reg(chan, 0, SH4A_PCIEPCTLR); |
| 63 | |
Paul Mundt | 66765fe | 2009-06-16 06:26:08 +0900 | [diff] [blame] | 64 | return PCIBIOS_SUCCESSFUL; |
| 65 | } |
| 66 | |
| 67 | static int sh7786_pcie_read(struct pci_bus *bus, unsigned int devfn, |
| 68 | int where, int size, u32 *val) |
| 69 | { |
| 70 | unsigned long flags; |
| 71 | int ret; |
| 72 | u32 data; |
| 73 | |
| 74 | if ((size == 2) && (where & 1)) |
| 75 | return PCIBIOS_BAD_REGISTER_NUMBER; |
| 76 | else if ((size == 4) && (where & 3)) |
| 77 | return PCIBIOS_BAD_REGISTER_NUMBER; |
| 78 | |
| 79 | spin_lock_irqsave(&sh7786_pcie_lock, flags); |
| 80 | ret = sh7786_pcie_config_access(PCI_ACCESS_READ, bus, |
| 81 | devfn, where, &data); |
Paul Mundt | 7656e24 | 2010-08-20 15:59:40 +0900 | [diff] [blame] | 82 | if (ret != PCIBIOS_SUCCESSFUL) { |
| 83 | *val = 0xffffffff; |
Paul Mundt | 66765fe | 2009-06-16 06:26:08 +0900 | [diff] [blame] | 84 | goto out; |
Paul Mundt | 7656e24 | 2010-08-20 15:59:40 +0900 | [diff] [blame] | 85 | } |
Paul Mundt | 66765fe | 2009-06-16 06:26:08 +0900 | [diff] [blame] | 86 | |
| 87 | if (size == 1) |
| 88 | *val = (data >> ((where & 3) << 3)) & 0xff; |
| 89 | else if (size == 2) |
| 90 | *val = (data >> ((where & 2) << 3)) & 0xffff; |
| 91 | else |
| 92 | *val = data; |
| 93 | |
| 94 | dev_dbg(&bus->dev, "pcie-config-read: bus=%3d devfn=0x%04x " |
| 95 | "where=0x%04x size=%d val=0x%08lx\n", bus->number, |
| 96 | devfn, where, size, (unsigned long)*val); |
| 97 | |
| 98 | out: |
| 99 | spin_unlock_irqrestore(&sh7786_pcie_lock, flags); |
| 100 | return ret; |
| 101 | } |
| 102 | |
| 103 | static int sh7786_pcie_write(struct pci_bus *bus, unsigned int devfn, |
| 104 | int where, int size, u32 val) |
| 105 | { |
| 106 | unsigned long flags; |
| 107 | int shift, ret; |
| 108 | u32 data; |
| 109 | |
| 110 | if ((size == 2) && (where & 1)) |
| 111 | return PCIBIOS_BAD_REGISTER_NUMBER; |
| 112 | else if ((size == 4) && (where & 3)) |
| 113 | return PCIBIOS_BAD_REGISTER_NUMBER; |
| 114 | |
| 115 | spin_lock_irqsave(&sh7786_pcie_lock, flags); |
| 116 | ret = sh7786_pcie_config_access(PCI_ACCESS_READ, bus, |
| 117 | devfn, where, &data); |
| 118 | if (ret != PCIBIOS_SUCCESSFUL) |
| 119 | goto out; |
| 120 | |
| 121 | dev_dbg(&bus->dev, "pcie-config-write: bus=%3d devfn=0x%04x " |
| 122 | "where=0x%04x size=%d val=%08lx\n", bus->number, |
| 123 | devfn, where, size, (unsigned long)val); |
| 124 | |
| 125 | if (size == 1) { |
| 126 | shift = (where & 3) << 3; |
| 127 | data &= ~(0xff << shift); |
| 128 | data |= ((val & 0xff) << shift); |
| 129 | } else if (size == 2) { |
| 130 | shift = (where & 2) << 3; |
| 131 | data &= ~(0xffff << shift); |
| 132 | data |= ((val & 0xffff) << shift); |
| 133 | } else |
| 134 | data = val; |
| 135 | |
| 136 | ret = sh7786_pcie_config_access(PCI_ACCESS_WRITE, bus, |
| 137 | devfn, where, &data); |
| 138 | out: |
| 139 | spin_unlock_irqrestore(&sh7786_pcie_lock, flags); |
| 140 | return ret; |
| 141 | } |
| 142 | |
| 143 | struct pci_ops sh7786_pci_ops = { |
| 144 | .read = sh7786_pcie_read, |
| 145 | .write = sh7786_pcie_write, |
| 146 | }; |