blob: 92d27f734f2e7dfc9ee27511d3d875223610da6d [file] [log] [blame]
Paul Mundt959f85f2006-09-27 16:43:28 +09001/*
2 * Generic SH-4 / SH-4A PCIC operations (SH7751, SH7780).
3 *
4 * Copyright (C) 2002 - 2006 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/pci.h>
11#include <asm/addrspace.h>
12#include <asm/io.h>
13#include "pci-sh4.h"
14
15/*
16 * Direct access to PCI hardware...
17 */
18#define CONFIG_CMD(bus, devfn, where) \
19 P1SEGADDR((bus->number << 16) | (devfn << 8) | (where & ~3))
20
21static DEFINE_SPINLOCK(sh4_pci_lock);
22
23/*
24 * Functions for accessing PCI configuration space with type 1 accesses
25 */
26static int sh4_pci_read(struct pci_bus *bus, unsigned int devfn,
27 int where, int size, u32 *val)
28{
29 unsigned long flags;
30 u32 data;
31
32 /*
33 * PCIPDR may only be accessed as 32 bit words,
34 * so we must do byte alignment by hand
35 */
36 spin_lock_irqsave(&sh4_pci_lock, flags);
Magnus Dammb8b47bf2009-03-11 15:41:51 +090037 pci_write_reg(NULL, CONFIG_CMD(bus, devfn, where), SH4_PCIPAR);
38 data = pci_read_reg(NULL, SH4_PCIPDR);
Paul Mundt959f85f2006-09-27 16:43:28 +090039 spin_unlock_irqrestore(&sh4_pci_lock, flags);
40
41 switch (size) {
42 case 1:
43 *val = (data >> ((where & 3) << 3)) & 0xff;
44 break;
45 case 2:
46 *val = (data >> ((where & 2) << 3)) & 0xffff;
47 break;
48 case 4:
49 *val = data;
50 break;
51 default:
52 return PCIBIOS_FUNC_NOT_SUPPORTED;
53 }
54
55 return PCIBIOS_SUCCESSFUL;
56}
57
58/*
59 * Since SH4 only does 32bit access we'll have to do a read,
60 * mask,write operation.
61 * We'll allow an odd byte offset, though it should be illegal.
62 */
63static int sh4_pci_write(struct pci_bus *bus, unsigned int devfn,
64 int where, int size, u32 val)
65{
66 unsigned long flags;
67 int shift;
68 u32 data;
69
70 spin_lock_irqsave(&sh4_pci_lock, flags);
Magnus Dammb8b47bf2009-03-11 15:41:51 +090071 pci_write_reg(NULL, CONFIG_CMD(bus, devfn, where), SH4_PCIPAR);
72 data = pci_read_reg(NULL, SH4_PCIPDR);
Paul Mundt959f85f2006-09-27 16:43:28 +090073 spin_unlock_irqrestore(&sh4_pci_lock, flags);
74
75 switch (size) {
76 case 1:
77 shift = (where & 3) << 3;
78 data &= ~(0xff << shift);
79 data |= ((val & 0xff) << shift);
80 break;
81 case 2:
82 shift = (where & 2) << 3;
83 data &= ~(0xffff << shift);
84 data |= ((val & 0xffff) << shift);
85 break;
86 case 4:
87 data = val;
88 break;
89 default:
90 return PCIBIOS_FUNC_NOT_SUPPORTED;
91 }
92
Magnus Dammb8b47bf2009-03-11 15:41:51 +090093 pci_write_reg(NULL, data, SH4_PCIPDR);
Paul Mundt959f85f2006-09-27 16:43:28 +090094
95 return PCIBIOS_SUCCESSFUL;
96}
97
98struct pci_ops sh4_pci_ops = {
99 .read = sh4_pci_read,
100 .write = sh4_pci_write,
101};
102
103/*
104 * Not really related to pci_ops, but it's common and not worth shoving
105 * somewhere else for now..
106 */
107static unsigned int pci_probe = PCI_PROBE_CONF1;
108
Magnus Dammb8b47bf2009-03-11 15:41:51 +0900109int __init sh4_pci_check_direct(struct pci_channel *chan)
Paul Mundt959f85f2006-09-27 16:43:28 +0900110{
111 /*
112 * Check if configuration works.
113 */
114 if (pci_probe & PCI_PROBE_CONF1) {
Magnus Dammb8b47bf2009-03-11 15:41:51 +0900115 unsigned int tmp = pci_read_reg(chan, SH4_PCIPAR);
Paul Mundt959f85f2006-09-27 16:43:28 +0900116
Magnus Dammb8b47bf2009-03-11 15:41:51 +0900117 pci_write_reg(chan, P1SEG, SH4_PCIPAR);
Paul Mundt959f85f2006-09-27 16:43:28 +0900118
Magnus Dammb8b47bf2009-03-11 15:41:51 +0900119 if (pci_read_reg(chan, SH4_PCIPAR) == P1SEG) {
120 pci_write_reg(chan, tmp, SH4_PCIPAR);
Paul Mundt959f85f2006-09-27 16:43:28 +0900121 printk(KERN_INFO "PCI: Using configuration type 1\n");
122 request_region(PCI_REG(SH4_PCIPAR), 8, "PCI conf1");
123
124 return 0;
125 }
126
Magnus Dammb8b47bf2009-03-11 15:41:51 +0900127 pci_write_reg(chan, tmp, SH4_PCIPAR);
Paul Mundt959f85f2006-09-27 16:43:28 +0900128 }
129
130 pr_debug("PCI: pci_check_direct failed\n");
131 return -EINVAL;
132}
133
134/* Handle generic fixups */
135static void __init pci_fixup_ide_bases(struct pci_dev *d)
136{
137 int i;
138
139 /*
140 * PCI IDE controllers use non-standard I/O port decoding, respect it.
141 */
142 if ((d->class >> 8) != PCI_CLASS_STORAGE_IDE)
143 return;
144 pr_debug("PCI: IDE base address fixup for %s\n", pci_name(d));
145 for(i = 0; i < 4; i++) {
146 struct resource *r = &d->resource[i];
147
148 if ((r->start & ~0x80) == 0x374) {
149 r->start |= 2;
150 r->end = r->start;
151 }
152 }
153}
154DECLARE_PCI_FIXUP_HEADER(PCI_ANY_ID, PCI_ANY_ID, pci_fixup_ide_bases);
155
Paul Mundtbd5f0d12007-07-20 13:22:47 +0900156char * __devinit pcibios_setup(char *str)
Paul Mundt959f85f2006-09-27 16:43:28 +0900157{
158 if (!strcmp(str, "off")) {
159 pci_probe = 0;
160 return NULL;
161 }
162
163 return str;
164}
Paul Mundtcd6c7ea2007-03-29 00:04:39 +0900165
Magnus Dammb8b47bf2009-03-11 15:41:51 +0900166int __attribute__((weak)) pci_fixup_pcic(struct pci_channel *chan)
Paul Mundtcd6c7ea2007-03-29 00:04:39 +0900167{
168 /* Nothing to do. */
169 return 0;
170}