blob: 01cb70fbd80334dd913b48be6063bb7dee729c72 [file] [log] [blame]
Paul Mundt66765fe2009-06-16 06:26:08 +09001/*
2 * Generic SH7786 PCI-Express operations.
3 *
Paul Mundt7656e242010-08-20 15:59:40 +09004 * Copyright (C) 2009 - 2010 Paul Mundt
Paul Mundt66765fe2009-06-16 06:26:08 +09005 *
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
17enum {
18 PCI_ACCESS_READ,
19 PCI_ACCESS_WRITE,
20};
21
22static DEFINE_SPINLOCK(sh7786_pcie_lock);
23
24static 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 Mundt2c65d752010-09-20 15:39:54 +090028 int dev, func, type, reg;
Paul Mundt66765fe2009-06-16 06:26:08 +090029
30 dev = PCI_SLOT(devfn);
31 func = PCI_FUNC(devfn);
Paul Mundt65c23f52010-08-20 20:26:41 +090032 type = !!bus->parent;
Paul Mundt2c65d752010-09-20 15:39:54 +090033 reg = where & ~3;
Paul Mundt66765fe2009-06-16 06:26:08 +090034
35 if (bus->number > 255 || dev > 31 || func > 7)
36 return PCIBIOS_FUNC_NOT_SUPPORTED;
Paul Mundt2c65d752010-09-20 15:39:54 +090037
38 /*
39 * While each channel has its own memory-mapped extended config
40 * space, it's generally only accessible when in endpoint mode.
41 * When in root complex mode, the controller is unable to target
42 * itself with either type 0 or type 1 accesses, and indeed, any
43 * controller initiated target transfer to its own config space
44 * result in a completer abort.
45 *
46 * Each channel effectively only supports a single device, but as
47 * the same channel <-> device access works for any PCI_SLOT()
48 * value, we cheat a bit here and bind the controller's config
49 * space to devfn 0 in order to enable self-enumeration. In this
50 * case the regular PAR/PDR path is sidelined and the mangled
51 * config access itself is initiated as a SuperHyway transaction.
52 */
53 if (pci_is_root_bus(bus)) {
54 if (dev == 0) {
55 if (access_type == PCI_ACCESS_READ)
56 *data = pci_read_reg(chan, PCI_REG(reg));
57 else
58 pci_write_reg(chan, *data, PCI_REG(reg));
59
60 return PCIBIOS_SUCCESSFUL;
61 } else if (dev > 1)
62 return PCIBIOS_DEVICE_NOT_FOUND;
63 }
Paul Mundt66765fe2009-06-16 06:26:08 +090064
Paul Mundt7656e242010-08-20 15:59:40 +090065 /* Clear errors */
66 pci_write_reg(chan, pci_read_reg(chan, SH4A_PCIEERRFR), SH4A_PCIEERRFR);
67
Paul Mundt66765fe2009-06-16 06:26:08 +090068 /* Set the PIO address */
69 pci_write_reg(chan, (bus->number << 24) | (dev << 19) |
Paul Mundt2c65d752010-09-20 15:39:54 +090070 (func << 16) | reg, SH4A_PCIEPAR);
Paul Mundt66765fe2009-06-16 06:26:08 +090071
72 /* Enable the configuration access */
Paul Mundt65c23f52010-08-20 20:26:41 +090073 pci_write_reg(chan, (1 << 31) | (type << 8), SH4A_PCIEPCTLR);
Paul Mundt7656e242010-08-20 15:59:40 +090074
75 /* Check for errors */
76 if (pci_read_reg(chan, SH4A_PCIEERRFR) & 0x10)
77 return PCIBIOS_DEVICE_NOT_FOUND;
Paul Mundt2c65d752010-09-20 15:39:54 +090078
Paul Mundt7656e242010-08-20 15:59:40 +090079 /* Check for master and target aborts */
80 if (pci_read_reg(chan, SH4A_PCIEPCICONF1) & ((1 << 29) | (1 << 28)))
81 return PCIBIOS_DEVICE_NOT_FOUND;
Paul Mundt66765fe2009-06-16 06:26:08 +090082
83 if (access_type == PCI_ACCESS_READ)
84 *data = pci_read_reg(chan, SH4A_PCIEPDR);
85 else
86 pci_write_reg(chan, *data, SH4A_PCIEPDR);
87
Paul Mundtbdf74992010-09-19 13:54:50 +090088 /* Disable the configuration access */
89 pci_write_reg(chan, 0, SH4A_PCIEPCTLR);
90
Paul Mundt66765fe2009-06-16 06:26:08 +090091 return PCIBIOS_SUCCESSFUL;
92}
93
94static int sh7786_pcie_read(struct pci_bus *bus, unsigned int devfn,
95 int where, int size, u32 *val)
96{
97 unsigned long flags;
98 int ret;
99 u32 data;
100
101 if ((size == 2) && (where & 1))
102 return PCIBIOS_BAD_REGISTER_NUMBER;
103 else if ((size == 4) && (where & 3))
104 return PCIBIOS_BAD_REGISTER_NUMBER;
105
106 spin_lock_irqsave(&sh7786_pcie_lock, flags);
107 ret = sh7786_pcie_config_access(PCI_ACCESS_READ, bus,
108 devfn, where, &data);
Paul Mundt7656e242010-08-20 15:59:40 +0900109 if (ret != PCIBIOS_SUCCESSFUL) {
110 *val = 0xffffffff;
Paul Mundt66765fe2009-06-16 06:26:08 +0900111 goto out;
Paul Mundt7656e242010-08-20 15:59:40 +0900112 }
Paul Mundt66765fe2009-06-16 06:26:08 +0900113
114 if (size == 1)
115 *val = (data >> ((where & 3) << 3)) & 0xff;
116 else if (size == 2)
117 *val = (data >> ((where & 2) << 3)) & 0xffff;
118 else
119 *val = data;
120
121 dev_dbg(&bus->dev, "pcie-config-read: bus=%3d devfn=0x%04x "
122 "where=0x%04x size=%d val=0x%08lx\n", bus->number,
123 devfn, where, size, (unsigned long)*val);
124
125out:
126 spin_unlock_irqrestore(&sh7786_pcie_lock, flags);
127 return ret;
128}
129
130static int sh7786_pcie_write(struct pci_bus *bus, unsigned int devfn,
131 int where, int size, u32 val)
132{
133 unsigned long flags;
134 int shift, ret;
135 u32 data;
136
137 if ((size == 2) && (where & 1))
138 return PCIBIOS_BAD_REGISTER_NUMBER;
139 else if ((size == 4) && (where & 3))
140 return PCIBIOS_BAD_REGISTER_NUMBER;
141
142 spin_lock_irqsave(&sh7786_pcie_lock, flags);
143 ret = sh7786_pcie_config_access(PCI_ACCESS_READ, bus,
144 devfn, where, &data);
145 if (ret != PCIBIOS_SUCCESSFUL)
146 goto out;
147
148 dev_dbg(&bus->dev, "pcie-config-write: bus=%3d devfn=0x%04x "
149 "where=0x%04x size=%d val=%08lx\n", bus->number,
150 devfn, where, size, (unsigned long)val);
151
152 if (size == 1) {
153 shift = (where & 3) << 3;
154 data &= ~(0xff << shift);
155 data |= ((val & 0xff) << shift);
156 } else if (size == 2) {
157 shift = (where & 2) << 3;
158 data &= ~(0xffff << shift);
159 data |= ((val & 0xffff) << shift);
160 } else
161 data = val;
162
163 ret = sh7786_pcie_config_access(PCI_ACCESS_WRITE, bus,
164 devfn, where, &data);
165out:
166 spin_unlock_irqrestore(&sh7786_pcie_lock, flags);
167 return ret;
168}
169
170struct pci_ops sh7786_pci_ops = {
171 .read = sh7786_pcie_read,
172 .write = sh7786_pcie_write,
173};