blob: 2a7f7b50ff0a3878db41e53d7aa5c226d2f4b104 [file] [log] [blame]
Paul Mundt959f85f2006-09-27 16:43:28 +09001/*
2 * Generic SH-4 / SH-4A PCIC operations (SH7751, SH7780).
3 *
Paul Mundt7e4ba0d2009-04-17 14:07:57 +09004 * Copyright (C) 2002 - 2009 Paul Mundt
Paul Mundt959f85f2006-09-27 16:43:28 +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/pci.h>
Paul Mundt7e4ba0d2009-04-17 14:07:57 +090011#include <linux/io.h>
Paul Mundt959f85f2006-09-27 16:43:28 +090012#include <asm/addrspace.h>
Paul Mundt959f85f2006-09-27 16:43:28 +090013#include "pci-sh4.h"
14
15/*
16 * Direct access to PCI hardware...
17 */
18#define CONFIG_CMD(bus, devfn, where) \
Paul Mundt7e4ba0d2009-04-17 14:07:57 +090019 (P1SEG | (bus->number << 16) | (devfn << 8) | (where & ~3))
Paul Mundt959f85f2006-09-27 16:43:28 +090020
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{
Magnus Dammb6706ef2008-02-19 21:34:55 +090029 struct pci_channel *chan = bus->sysdata;
Paul Mundt959f85f2006-09-27 16:43:28 +090030 unsigned long flags;
31 u32 data;
32
33 /*
34 * PCIPDR may only be accessed as 32 bit words,
35 * so we must do byte alignment by hand
36 */
37 spin_lock_irqsave(&sh4_pci_lock, flags);
Magnus Dammb6706ef2008-02-19 21:34:55 +090038 pci_write_reg(chan, CONFIG_CMD(bus, devfn, where), SH4_PCIPAR);
39 data = pci_read_reg(chan, SH4_PCIPDR);
Paul Mundt959f85f2006-09-27 16:43:28 +090040 spin_unlock_irqrestore(&sh4_pci_lock, flags);
41
42 switch (size) {
43 case 1:
44 *val = (data >> ((where & 3) << 3)) & 0xff;
45 break;
46 case 2:
47 *val = (data >> ((where & 2) << 3)) & 0xffff;
48 break;
49 case 4:
50 *val = data;
51 break;
52 default:
53 return PCIBIOS_FUNC_NOT_SUPPORTED;
54 }
55
56 return PCIBIOS_SUCCESSFUL;
57}
58
59/*
60 * Since SH4 only does 32bit access we'll have to do a read,
61 * mask,write operation.
62 * We'll allow an odd byte offset, though it should be illegal.
63 */
64static int sh4_pci_write(struct pci_bus *bus, unsigned int devfn,
65 int where, int size, u32 val)
66{
Magnus Dammb6706ef2008-02-19 21:34:55 +090067 struct pci_channel *chan = bus->sysdata;
Paul Mundt959f85f2006-09-27 16:43:28 +090068 unsigned long flags;
69 int shift;
70 u32 data;
71
72 spin_lock_irqsave(&sh4_pci_lock, flags);
Magnus Dammb6706ef2008-02-19 21:34:55 +090073 pci_write_reg(chan, CONFIG_CMD(bus, devfn, where), SH4_PCIPAR);
74 data = pci_read_reg(chan, SH4_PCIPDR);
Paul Mundt959f85f2006-09-27 16:43:28 +090075 spin_unlock_irqrestore(&sh4_pci_lock, flags);
76
77 switch (size) {
78 case 1:
79 shift = (where & 3) << 3;
80 data &= ~(0xff << shift);
81 data |= ((val & 0xff) << shift);
82 break;
83 case 2:
84 shift = (where & 2) << 3;
85 data &= ~(0xffff << shift);
86 data |= ((val & 0xffff) << shift);
87 break;
88 case 4:
89 data = val;
90 break;
91 default:
92 return PCIBIOS_FUNC_NOT_SUPPORTED;
93 }
94
Magnus Dammb6706ef2008-02-19 21:34:55 +090095 pci_write_reg(chan, data, SH4_PCIPDR);
Paul Mundt959f85f2006-09-27 16:43:28 +090096
97 return PCIBIOS_SUCCESSFUL;
98}
99
100struct pci_ops sh4_pci_ops = {
101 .read = sh4_pci_read,
102 .write = sh4_pci_write,
103};
104
105/*
106 * Not really related to pci_ops, but it's common and not worth shoving
107 * somewhere else for now..
108 */
109static unsigned int pci_probe = PCI_PROBE_CONF1;
110
Magnus Dammb8b47bf2009-03-11 15:41:51 +0900111int __init sh4_pci_check_direct(struct pci_channel *chan)
Paul Mundt959f85f2006-09-27 16:43:28 +0900112{
113 /*
114 * Check if configuration works.
115 */
116 if (pci_probe & PCI_PROBE_CONF1) {
Magnus Dammb8b47bf2009-03-11 15:41:51 +0900117 unsigned int tmp = pci_read_reg(chan, SH4_PCIPAR);
Paul Mundt959f85f2006-09-27 16:43:28 +0900118
Magnus Dammb8b47bf2009-03-11 15:41:51 +0900119 pci_write_reg(chan, P1SEG, SH4_PCIPAR);
Paul Mundt959f85f2006-09-27 16:43:28 +0900120
Magnus Dammb8b47bf2009-03-11 15:41:51 +0900121 if (pci_read_reg(chan, SH4_PCIPAR) == P1SEG) {
122 pci_write_reg(chan, tmp, SH4_PCIPAR);
Paul Mundt959f85f2006-09-27 16:43:28 +0900123 printk(KERN_INFO "PCI: Using configuration type 1\n");
Magnus Damme4c6a362008-02-19 21:35:04 +0900124 request_region(chan->reg_base + SH4_PCIPAR, 8,
125 "PCI conf1");
Paul Mundt959f85f2006-09-27 16:43:28 +0900126 return 0;
127 }
128
Magnus Dammb8b47bf2009-03-11 15:41:51 +0900129 pci_write_reg(chan, tmp, SH4_PCIPAR);
Paul Mundt959f85f2006-09-27 16:43:28 +0900130 }
131
132 pr_debug("PCI: pci_check_direct failed\n");
133 return -EINVAL;
134}
135
136/* Handle generic fixups */
137static void __init pci_fixup_ide_bases(struct pci_dev *d)
138{
139 int i;
140
141 /*
142 * PCI IDE controllers use non-standard I/O port decoding, respect it.
143 */
144 if ((d->class >> 8) != PCI_CLASS_STORAGE_IDE)
145 return;
146 pr_debug("PCI: IDE base address fixup for %s\n", pci_name(d));
147 for(i = 0; i < 4; i++) {
148 struct resource *r = &d->resource[i];
149
150 if ((r->start & ~0x80) == 0x374) {
151 r->start |= 2;
152 r->end = r->start;
153 }
154 }
155}
156DECLARE_PCI_FIXUP_HEADER(PCI_ANY_ID, PCI_ANY_ID, pci_fixup_ide_bases);
157
Paul Mundtbd5f0d12007-07-20 13:22:47 +0900158char * __devinit pcibios_setup(char *str)
Paul Mundt959f85f2006-09-27 16:43:28 +0900159{
160 if (!strcmp(str, "off")) {
161 pci_probe = 0;
162 return NULL;
163 }
164
165 return str;
166}
Paul Mundtcd6c7ea2007-03-29 00:04:39 +0900167
Magnus Dammb8b47bf2009-03-11 15:41:51 +0900168int __attribute__((weak)) pci_fixup_pcic(struct pci_channel *chan)
Paul Mundtcd6c7ea2007-03-29 00:04:39 +0900169{
170 /* Nothing to do. */
171 return 0;
172}