57134a38686a8c9c8eb0e943edfc4e25e8ac2287
[pandora-kernel.git] / arch / sh / drivers / pci / ops-sh7786.c
1 /*
2  * Generic SH7786 PCI-Express operations.
3  *
4  *  Copyright (C) 2009 - 2010  Paul Mundt
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;
28         int dev, func;
29
30         dev = PCI_SLOT(devfn);
31         func = PCI_FUNC(devfn);
32
33         if (bus->number > 255 || dev > 31 || func > 7)
34                 return PCIBIOS_FUNC_NOT_SUPPORTED;
35         if (devfn)
36                 return PCIBIOS_DEVICE_NOT_FOUND;
37
38         /* Clear errors */
39         pci_write_reg(chan, pci_read_reg(chan, SH4A_PCIEERRFR), SH4A_PCIEERRFR);
40
41         /* Set the PIO address */
42         pci_write_reg(chan, (bus->number << 24) | (dev << 19) |
43                                 (func << 16) | (where & ~3), SH4A_PCIEPAR);
44
45         /* Enable the configuration access */
46         if (bus->number) {
47                 /* Type 1 */
48                 pci_write_reg(chan, (1 << 31) | (1 << 8), SH4A_PCIEPCTLR);
49         } else {
50                 /* Type 0 */
51                 pci_write_reg(chan, (1 << 31), SH4A_PCIEPCTLR);
52         }
53
54         /* Check for errors */
55         if (pci_read_reg(chan, SH4A_PCIEERRFR) & 0x10)
56                 return PCIBIOS_DEVICE_NOT_FOUND;
57         /* Check for master and target aborts */
58         if (pci_read_reg(chan, SH4A_PCIEPCICONF1) & ((1 << 29) | (1 << 28)))
59                 return PCIBIOS_DEVICE_NOT_FOUND;
60
61         if (access_type == PCI_ACCESS_READ)
62                 *data = pci_read_reg(chan, SH4A_PCIEPDR);
63         else
64                 pci_write_reg(chan, *data, SH4A_PCIEPDR);
65
66         return PCIBIOS_SUCCESSFUL;
67 }
68
69 static int sh7786_pcie_read(struct pci_bus *bus, unsigned int devfn,
70                             int where, int size, u32 *val)
71 {
72         unsigned long flags;
73         int ret;
74         u32 data;
75
76         if ((size == 2) && (where & 1))
77                 return PCIBIOS_BAD_REGISTER_NUMBER;
78         else if ((size == 4) && (where & 3))
79                 return PCIBIOS_BAD_REGISTER_NUMBER;
80
81         spin_lock_irqsave(&sh7786_pcie_lock, flags);
82         ret = sh7786_pcie_config_access(PCI_ACCESS_READ, bus,
83                                         devfn, where, &data);
84         if (ret != PCIBIOS_SUCCESSFUL) {
85                 *val = 0xffffffff;
86                 goto out;
87         }
88
89         if (size == 1)
90                 *val = (data >> ((where & 3) << 3)) & 0xff;
91         else if (size == 2)
92                 *val = (data >> ((where & 2) << 3)) & 0xffff;
93         else
94                 *val = data;
95
96         dev_dbg(&bus->dev, "pcie-config-read: bus=%3d devfn=0x%04x "
97                 "where=0x%04x size=%d val=0x%08lx\n", bus->number,
98                 devfn, where, size, (unsigned long)*val);
99
100 out:
101         spin_unlock_irqrestore(&sh7786_pcie_lock, flags);
102         return ret;
103 }
104
105 static int sh7786_pcie_write(struct pci_bus *bus, unsigned int devfn,
106                              int where, int size, u32 val)
107 {
108         unsigned long flags;
109         int shift, ret;
110         u32 data;
111
112         if ((size == 2) && (where & 1))
113                 return PCIBIOS_BAD_REGISTER_NUMBER;
114         else if ((size == 4) && (where & 3))
115                 return PCIBIOS_BAD_REGISTER_NUMBER;
116
117         spin_lock_irqsave(&sh7786_pcie_lock, flags);
118         ret = sh7786_pcie_config_access(PCI_ACCESS_READ, bus,
119                                         devfn, where, &data);
120         if (ret != PCIBIOS_SUCCESSFUL)
121                 goto out;
122
123         dev_dbg(&bus->dev, "pcie-config-write: bus=%3d devfn=0x%04x "
124                 "where=0x%04x size=%d val=%08lx\n", bus->number,
125                 devfn, where, size, (unsigned long)val);
126
127         if (size == 1) {
128                 shift = (where & 3) << 3;
129                 data &= ~(0xff << shift);
130                 data |= ((val & 0xff) << shift);
131         } else if (size == 2) {
132                 shift = (where & 2) << 3;
133                 data &= ~(0xffff << shift);
134                 data |= ((val & 0xffff) << shift);
135         } else
136                 data = val;
137
138         ret = sh7786_pcie_config_access(PCI_ACCESS_WRITE, bus,
139                                         devfn, where, &data);
140 out:
141         spin_unlock_irqrestore(&sh7786_pcie_lock, flags);
142         return ret;
143 }
144
145 struct pci_ops sh7786_pci_ops = {
146         .read   = sh7786_pcie_read,
147         .write  = sh7786_pcie_write,
148 };