1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun * Copyright 2001 MontaVista Software Inc.
3*4882a593Smuzhiyun * Author: MontaVista Software, Inc.
4*4882a593Smuzhiyun * ahennessy@mvista.com
5*4882a593Smuzhiyun *
6*4882a593Smuzhiyun * Copyright (C) 2000-2001 Toshiba Corporation
7*4882a593Smuzhiyun * Copyright (C) 2004 by Ralf Baechle (ralf@linux-mips.org)
8*4882a593Smuzhiyun *
9*4882a593Smuzhiyun * Based on arch/mips/ddb5xxx/ddb5477/pci_ops.c
10*4882a593Smuzhiyun *
11*4882a593Smuzhiyun * Define the pci_ops for TX3927.
12*4882a593Smuzhiyun *
13*4882a593Smuzhiyun * Much of the code is derived from the original DDB5074 port by
14*4882a593Smuzhiyun * Geert Uytterhoeven <geert@linux-m68k.org>
15*4882a593Smuzhiyun *
16*4882a593Smuzhiyun * This program is free software; you can redistribute it and/or modify it
17*4882a593Smuzhiyun * under the terms of the GNU General Public License as published by the
18*4882a593Smuzhiyun * Free Software Foundation; either version 2 of the License, or (at your
19*4882a593Smuzhiyun * option) any later version.
20*4882a593Smuzhiyun *
21*4882a593Smuzhiyun * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
22*4882a593Smuzhiyun * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
23*4882a593Smuzhiyun * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
24*4882a593Smuzhiyun * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25*4882a593Smuzhiyun * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26*4882a593Smuzhiyun * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
27*4882a593Smuzhiyun * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
28*4882a593Smuzhiyun * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29*4882a593Smuzhiyun * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30*4882a593Smuzhiyun * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31*4882a593Smuzhiyun *
32*4882a593Smuzhiyun * You should have received a copy of the GNU General Public License along
33*4882a593Smuzhiyun * with this program; if not, write to the Free Software Foundation, Inc.,
34*4882a593Smuzhiyun * 675 Mass Ave, Cambridge, MA 02139, USA.
35*4882a593Smuzhiyun */
36*4882a593Smuzhiyun #include <linux/types.h>
37*4882a593Smuzhiyun #include <linux/pci.h>
38*4882a593Smuzhiyun #include <linux/kernel.h>
39*4882a593Smuzhiyun #include <linux/init.h>
40*4882a593Smuzhiyun #include <linux/interrupt.h>
41*4882a593Smuzhiyun #include <linux/irq.h>
42*4882a593Smuzhiyun
43*4882a593Smuzhiyun #include <asm/addrspace.h>
44*4882a593Smuzhiyun #include <asm/txx9irq.h>
45*4882a593Smuzhiyun #include <asm/txx9/pci.h>
46*4882a593Smuzhiyun #include <asm/txx9/tx3927.h>
47*4882a593Smuzhiyun
mkaddr(struct pci_bus * bus,unsigned char devfn,unsigned char where)48*4882a593Smuzhiyun static int mkaddr(struct pci_bus *bus, unsigned char devfn, unsigned char where)
49*4882a593Smuzhiyun {
50*4882a593Smuzhiyun if (bus->parent == NULL &&
51*4882a593Smuzhiyun devfn >= PCI_DEVFN(TX3927_PCIC_MAX_DEVNU, 0))
52*4882a593Smuzhiyun return -1;
53*4882a593Smuzhiyun tx3927_pcicptr->ica =
54*4882a593Smuzhiyun ((bus->number & 0xff) << 0x10) |
55*4882a593Smuzhiyun ((devfn & 0xff) << 0x08) |
56*4882a593Smuzhiyun (where & 0xfc) | (bus->parent ? 1 : 0);
57*4882a593Smuzhiyun
58*4882a593Smuzhiyun /* clear M_ABORT and Disable M_ABORT Int. */
59*4882a593Smuzhiyun tx3927_pcicptr->pcistat |= PCI_STATUS_REC_MASTER_ABORT;
60*4882a593Smuzhiyun tx3927_pcicptr->pcistatim &= ~PCI_STATUS_REC_MASTER_ABORT;
61*4882a593Smuzhiyun return 0;
62*4882a593Smuzhiyun }
63*4882a593Smuzhiyun
check_abort(void)64*4882a593Smuzhiyun static inline int check_abort(void)
65*4882a593Smuzhiyun {
66*4882a593Smuzhiyun if (tx3927_pcicptr->pcistat & PCI_STATUS_REC_MASTER_ABORT) {
67*4882a593Smuzhiyun tx3927_pcicptr->pcistat |= PCI_STATUS_REC_MASTER_ABORT;
68*4882a593Smuzhiyun tx3927_pcicptr->pcistatim |= PCI_STATUS_REC_MASTER_ABORT;
69*4882a593Smuzhiyun /* flush write buffer */
70*4882a593Smuzhiyun iob();
71*4882a593Smuzhiyun return PCIBIOS_DEVICE_NOT_FOUND;
72*4882a593Smuzhiyun }
73*4882a593Smuzhiyun return PCIBIOS_SUCCESSFUL;
74*4882a593Smuzhiyun }
75*4882a593Smuzhiyun
tx3927_pci_read_config(struct pci_bus * bus,unsigned int devfn,int where,int size,u32 * val)76*4882a593Smuzhiyun static int tx3927_pci_read_config(struct pci_bus *bus, unsigned int devfn,
77*4882a593Smuzhiyun int where, int size, u32 * val)
78*4882a593Smuzhiyun {
79*4882a593Smuzhiyun if (mkaddr(bus, devfn, where)) {
80*4882a593Smuzhiyun *val = 0xffffffff;
81*4882a593Smuzhiyun return PCIBIOS_DEVICE_NOT_FOUND;
82*4882a593Smuzhiyun }
83*4882a593Smuzhiyun
84*4882a593Smuzhiyun switch (size) {
85*4882a593Smuzhiyun case 1:
86*4882a593Smuzhiyun *val = *(volatile u8 *) ((unsigned long) & tx3927_pcicptr->icd | (where & 3));
87*4882a593Smuzhiyun break;
88*4882a593Smuzhiyun
89*4882a593Smuzhiyun case 2:
90*4882a593Smuzhiyun *val = le16_to_cpu(*(volatile u16 *) ((unsigned long) & tx3927_pcicptr->icd | (where & 3)));
91*4882a593Smuzhiyun break;
92*4882a593Smuzhiyun
93*4882a593Smuzhiyun case 4:
94*4882a593Smuzhiyun *val = le32_to_cpu(tx3927_pcicptr->icd);
95*4882a593Smuzhiyun break;
96*4882a593Smuzhiyun }
97*4882a593Smuzhiyun
98*4882a593Smuzhiyun return check_abort();
99*4882a593Smuzhiyun }
100*4882a593Smuzhiyun
tx3927_pci_write_config(struct pci_bus * bus,unsigned int devfn,int where,int size,u32 val)101*4882a593Smuzhiyun static int tx3927_pci_write_config(struct pci_bus *bus, unsigned int devfn,
102*4882a593Smuzhiyun int where, int size, u32 val)
103*4882a593Smuzhiyun {
104*4882a593Smuzhiyun if (mkaddr(bus, devfn, where))
105*4882a593Smuzhiyun return PCIBIOS_DEVICE_NOT_FOUND;
106*4882a593Smuzhiyun
107*4882a593Smuzhiyun switch (size) {
108*4882a593Smuzhiyun case 1:
109*4882a593Smuzhiyun *(volatile u8 *) ((unsigned long) & tx3927_pcicptr->icd | (where & 3)) = val;
110*4882a593Smuzhiyun break;
111*4882a593Smuzhiyun
112*4882a593Smuzhiyun case 2:
113*4882a593Smuzhiyun *(volatile u16 *) ((unsigned long) & tx3927_pcicptr->icd | (where & 2)) =
114*4882a593Smuzhiyun cpu_to_le16(val);
115*4882a593Smuzhiyun break;
116*4882a593Smuzhiyun
117*4882a593Smuzhiyun case 4:
118*4882a593Smuzhiyun tx3927_pcicptr->icd = cpu_to_le32(val);
119*4882a593Smuzhiyun }
120*4882a593Smuzhiyun
121*4882a593Smuzhiyun return check_abort();
122*4882a593Smuzhiyun }
123*4882a593Smuzhiyun
124*4882a593Smuzhiyun static struct pci_ops tx3927_pci_ops = {
125*4882a593Smuzhiyun .read = tx3927_pci_read_config,
126*4882a593Smuzhiyun .write = tx3927_pci_write_config,
127*4882a593Smuzhiyun };
128*4882a593Smuzhiyun
tx3927_pcic_setup(struct pci_controller * channel,unsigned long sdram_size,int extarb)129*4882a593Smuzhiyun void __init tx3927_pcic_setup(struct pci_controller *channel,
130*4882a593Smuzhiyun unsigned long sdram_size, int extarb)
131*4882a593Smuzhiyun {
132*4882a593Smuzhiyun unsigned long flags;
133*4882a593Smuzhiyun unsigned long io_base =
134*4882a593Smuzhiyun channel->io_resource->start + mips_io_port_base - IO_BASE;
135*4882a593Smuzhiyun unsigned long io_size =
136*4882a593Smuzhiyun channel->io_resource->end - channel->io_resource->start;
137*4882a593Smuzhiyun unsigned long io_pciaddr =
138*4882a593Smuzhiyun channel->io_resource->start - channel->io_offset;
139*4882a593Smuzhiyun unsigned long mem_base =
140*4882a593Smuzhiyun channel->mem_resource->start;
141*4882a593Smuzhiyun unsigned long mem_size =
142*4882a593Smuzhiyun channel->mem_resource->end - channel->mem_resource->start;
143*4882a593Smuzhiyun unsigned long mem_pciaddr =
144*4882a593Smuzhiyun channel->mem_resource->start - channel->mem_offset;
145*4882a593Smuzhiyun
146*4882a593Smuzhiyun printk(KERN_INFO "TX3927 PCIC -- DID:%04x VID:%04x RID:%02x Arbiter:%s",
147*4882a593Smuzhiyun tx3927_pcicptr->did, tx3927_pcicptr->vid,
148*4882a593Smuzhiyun tx3927_pcicptr->rid,
149*4882a593Smuzhiyun extarb ? "External" : "Internal");
150*4882a593Smuzhiyun channel->pci_ops = &tx3927_pci_ops;
151*4882a593Smuzhiyun
152*4882a593Smuzhiyun local_irq_save(flags);
153*4882a593Smuzhiyun /* Disable External PCI Config. Access */
154*4882a593Smuzhiyun tx3927_pcicptr->lbc = TX3927_PCIC_LBC_EPCAD;
155*4882a593Smuzhiyun #ifdef __BIG_ENDIAN
156*4882a593Smuzhiyun tx3927_pcicptr->lbc |= TX3927_PCIC_LBC_IBSE |
157*4882a593Smuzhiyun TX3927_PCIC_LBC_TIBSE |
158*4882a593Smuzhiyun TX3927_PCIC_LBC_TMFBSE | TX3927_PCIC_LBC_MSDSE;
159*4882a593Smuzhiyun #endif
160*4882a593Smuzhiyun /* LB->PCI mappings */
161*4882a593Smuzhiyun tx3927_pcicptr->iomas = ~(io_size - 1);
162*4882a593Smuzhiyun tx3927_pcicptr->ilbioma = io_base;
163*4882a593Smuzhiyun tx3927_pcicptr->ipbioma = io_pciaddr;
164*4882a593Smuzhiyun tx3927_pcicptr->mmas = ~(mem_size - 1);
165*4882a593Smuzhiyun tx3927_pcicptr->ilbmma = mem_base;
166*4882a593Smuzhiyun tx3927_pcicptr->ipbmma = mem_pciaddr;
167*4882a593Smuzhiyun /* PCI->LB mappings */
168*4882a593Smuzhiyun tx3927_pcicptr->iobas = 0xffffffff;
169*4882a593Smuzhiyun tx3927_pcicptr->ioba = 0;
170*4882a593Smuzhiyun tx3927_pcicptr->tlbioma = 0;
171*4882a593Smuzhiyun tx3927_pcicptr->mbas = ~(sdram_size - 1);
172*4882a593Smuzhiyun tx3927_pcicptr->mba = 0;
173*4882a593Smuzhiyun tx3927_pcicptr->tlbmma = 0;
174*4882a593Smuzhiyun /* Enable Direct mapping Address Space Decoder */
175*4882a593Smuzhiyun tx3927_pcicptr->lbc |= TX3927_PCIC_LBC_ILMDE | TX3927_PCIC_LBC_ILIDE;
176*4882a593Smuzhiyun
177*4882a593Smuzhiyun /* Clear All Local Bus Status */
178*4882a593Smuzhiyun tx3927_pcicptr->lbstat = TX3927_PCIC_LBIM_ALL;
179*4882a593Smuzhiyun /* Enable All Local Bus Interrupts */
180*4882a593Smuzhiyun tx3927_pcicptr->lbim = TX3927_PCIC_LBIM_ALL;
181*4882a593Smuzhiyun /* Clear All PCI Status Error */
182*4882a593Smuzhiyun tx3927_pcicptr->pcistat = TX3927_PCIC_PCISTATIM_ALL;
183*4882a593Smuzhiyun /* Enable All PCI Status Error Interrupts */
184*4882a593Smuzhiyun tx3927_pcicptr->pcistatim = TX3927_PCIC_PCISTATIM_ALL;
185*4882a593Smuzhiyun
186*4882a593Smuzhiyun /* PCIC Int => IRC IRQ10 */
187*4882a593Smuzhiyun tx3927_pcicptr->il = TX3927_IR_PCI;
188*4882a593Smuzhiyun /* Target Control (per errata) */
189*4882a593Smuzhiyun tx3927_pcicptr->tc = TX3927_PCIC_TC_OF8E | TX3927_PCIC_TC_IF8E;
190*4882a593Smuzhiyun
191*4882a593Smuzhiyun /* Enable Bus Arbiter */
192*4882a593Smuzhiyun if (!extarb)
193*4882a593Smuzhiyun tx3927_pcicptr->pbapmc = TX3927_PCIC_PBAPMC_PBAEN;
194*4882a593Smuzhiyun
195*4882a593Smuzhiyun tx3927_pcicptr->pcicmd = PCI_COMMAND_MASTER |
196*4882a593Smuzhiyun PCI_COMMAND_MEMORY |
197*4882a593Smuzhiyun PCI_COMMAND_IO |
198*4882a593Smuzhiyun PCI_COMMAND_PARITY | PCI_COMMAND_SERR;
199*4882a593Smuzhiyun local_irq_restore(flags);
200*4882a593Smuzhiyun }
201*4882a593Smuzhiyun
tx3927_pcierr_interrupt(int irq,void * dev_id)202*4882a593Smuzhiyun static irqreturn_t tx3927_pcierr_interrupt(int irq, void *dev_id)
203*4882a593Smuzhiyun {
204*4882a593Smuzhiyun struct pt_regs *regs = get_irq_regs();
205*4882a593Smuzhiyun
206*4882a593Smuzhiyun if (txx9_pci_err_action != TXX9_PCI_ERR_IGNORE) {
207*4882a593Smuzhiyun printk(KERN_WARNING "PCI error interrupt at 0x%08lx.\n",
208*4882a593Smuzhiyun regs->cp0_epc);
209*4882a593Smuzhiyun printk(KERN_WARNING "pcistat:%02x, lbstat:%04lx\n",
210*4882a593Smuzhiyun tx3927_pcicptr->pcistat, tx3927_pcicptr->lbstat);
211*4882a593Smuzhiyun }
212*4882a593Smuzhiyun if (txx9_pci_err_action != TXX9_PCI_ERR_PANIC) {
213*4882a593Smuzhiyun /* clear all pci errors */
214*4882a593Smuzhiyun tx3927_pcicptr->pcistat |= TX3927_PCIC_PCISTATIM_ALL;
215*4882a593Smuzhiyun tx3927_pcicptr->istat = TX3927_PCIC_IIM_ALL;
216*4882a593Smuzhiyun tx3927_pcicptr->tstat = TX3927_PCIC_TIM_ALL;
217*4882a593Smuzhiyun tx3927_pcicptr->lbstat = TX3927_PCIC_LBIM_ALL;
218*4882a593Smuzhiyun return IRQ_HANDLED;
219*4882a593Smuzhiyun }
220*4882a593Smuzhiyun console_verbose();
221*4882a593Smuzhiyun panic("PCI error.");
222*4882a593Smuzhiyun }
223*4882a593Smuzhiyun
tx3927_setup_pcierr_irq(void)224*4882a593Smuzhiyun void __init tx3927_setup_pcierr_irq(void)
225*4882a593Smuzhiyun {
226*4882a593Smuzhiyun if (request_irq(TXX9_IRQ_BASE + TX3927_IR_PCI,
227*4882a593Smuzhiyun tx3927_pcierr_interrupt,
228*4882a593Smuzhiyun 0, "PCI error",
229*4882a593Smuzhiyun (void *)TX3927_PCIC_REG))
230*4882a593Smuzhiyun printk(KERN_WARNING "Failed to request irq for PCIERR\n");
231*4882a593Smuzhiyun }
232