2 * Support for SCC external PCI
4 * (C) Copyright 2004-2007 TOSHIBA CORPORATION
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 #include <linux/kernel.h>
24 #include <linux/threads.h>
25 #include <linux/pci.h>
26 #include <linux/init.h>
27 #include <linux/pci_regs.h>
28 #include <linux/bootmem.h>
33 #include <asm/machdep.h>
34 #include <asm/pci-bridge.h>
35 #include <asm/ppc-pci.h>
39 #include "interrupt.h"
41 #define MAX_PCI_DEVICES 32
42 #define MAX_PCI_FUNCTIONS 8
44 #define iob() __asm__ __volatile__("eieio; sync":::"memory")
47 dma_addr_t dummy_page_da
;
50 static inline PCI_IO_ADDR
celleb_epci_get_epci_base(
51 struct pci_controller
*hose
)
55 * Celleb epci uses cfg_addr as a base address for
56 * epci control registers.
59 return hose
->cfg_addr
;
62 static inline PCI_IO_ADDR
celleb_epci_get_epci_cfg(
63 struct pci_controller
*hose
)
67 * Celleb epci uses cfg_data as a base address for
68 * configuration area for epci devices.
71 return hose
->cfg_data
;
74 static void scc_epci_dummy_read(struct pci_controller
*hose
)
76 PCI_IO_ADDR epci_base
;
79 epci_base
= celleb_epci_get_epci_base(hose
);
81 val
= in_be32(epci_base
+ SCC_EPCI_WATRP
);
87 void __init
epci_workaround_init(struct pci_controller
*hose
)
89 PCI_IO_ADDR epci_base
;
91 struct epci_private
*private = hose
->private_data
;
95 private->dummy_page_da
= dma_map_single(hose
->parent
,
96 celleb_dummy_page_va
, PAGE_SIZE
, DMA_FROM_DEVICE
);
97 if (private->dummy_page_da
== DMA_ERROR_CODE
) {
98 printk(KERN_ERR
"EPCI: dummy read disabled. "
99 "Map dummy page failed.\n");
103 celleb_pci_add_one(hose
, scc_epci_dummy_read
);
104 epci_base
= celleb_epci_get_epci_base(hose
);
106 reg
= epci_base
+ SCC_EPCI_DUMYRADR
;
107 out_be32(reg
, private->dummy_page_da
);
110 static inline void clear_and_disable_master_abort_interrupt(
111 struct pci_controller
*hose
)
113 PCI_IO_ADDR epci_base
;
115 epci_base
= celleb_epci_get_epci_base(hose
);
116 reg
= epci_base
+ PCI_COMMAND
;
117 out_be32(reg
, in_be32(reg
) | (PCI_STATUS_REC_MASTER_ABORT
<< 16));
120 static int celleb_epci_check_abort(struct pci_controller
*hose
,
124 PCI_IO_ADDR epci_base
;
128 epci_base
= celleb_epci_get_epci_base(hose
);
130 reg
= epci_base
+ PCI_COMMAND
;
133 if (val
& (PCI_STATUS_REC_MASTER_ABORT
<< 16)) {
135 (val
& 0xffff) | (PCI_STATUS_REC_MASTER_ABORT
<< 16));
137 /* clear PCI Controller error, FRE, PMFE */
138 reg
= epci_base
+ SCC_EPCI_STATUS
;
139 out_be32(reg
, SCC_EPCI_INT_PAI
);
141 reg
= epci_base
+ SCC_EPCI_VCSR
;
142 val
= in_be32(reg
) & 0xffff;
143 val
|= SCC_EPCI_VCSR_FRE
;
146 reg
= epci_base
+ SCC_EPCI_VISTAT
;
147 out_be32(reg
, SCC_EPCI_VISTAT_PMFE
);
148 return PCIBIOS_DEVICE_NOT_FOUND
;
151 return PCIBIOS_SUCCESSFUL
;
154 static PCI_IO_ADDR
celleb_epci_make_config_addr(
156 struct pci_controller
*hose
,
157 unsigned int devfn
, int where
)
161 if (bus
!= hose
->bus
)
162 addr
= celleb_epci_get_epci_cfg(hose
) +
163 (((bus
->number
& 0xff) << 16)
164 | ((devfn
& 0xff) << 8)
168 addr
= celleb_epci_get_epci_cfg(hose
) +
169 (((devfn
& 0xff) << 8) | (where
& 0xff));
171 pr_debug("EPCI: config_addr = 0x%p\n", addr
);
176 static int celleb_epci_read_config(struct pci_bus
*bus
,
177 unsigned int devfn
, int where
, int size
, u32
* val
)
179 PCI_IO_ADDR epci_base
;
181 struct device_node
*node
;
182 struct pci_controller
*hose
;
184 /* allignment check */
185 BUG_ON(where
% size
);
187 node
= (struct device_node
*)bus
->sysdata
;
188 hose
= pci_find_hose_for_OF_device(node
);
190 if (!celleb_epci_get_epci_cfg(hose
))
191 return PCIBIOS_DEVICE_NOT_FOUND
;
193 if (bus
->number
== hose
->first_busno
&& devfn
== 0) {
194 /* EPCI controller self */
196 epci_base
= celleb_epci_get_epci_base(hose
);
197 addr
= epci_base
+ where
;
204 *val
= in_be16(addr
);
207 *val
= in_be32(addr
);
210 return PCIBIOS_DEVICE_NOT_FOUND
;
215 clear_and_disable_master_abort_interrupt(hose
);
216 addr
= celleb_epci_make_config_addr(bus
, hose
, devfn
, where
);
223 *val
= in_le16(addr
);
226 *val
= in_le32(addr
);
229 return PCIBIOS_DEVICE_NOT_FOUND
;
234 "addr=0x%p, devfn=0x%x, where=0x%x, size=0x%x, val=0x%x\n",
235 addr
, devfn
, where
, size
, *val
);
237 return celleb_epci_check_abort(hose
, NULL
);
240 static int celleb_epci_write_config(struct pci_bus
*bus
,
241 unsigned int devfn
, int where
, int size
, u32 val
)
243 PCI_IO_ADDR epci_base
;
245 struct device_node
*node
;
246 struct pci_controller
*hose
;
248 /* allignment check */
249 BUG_ON(where
% size
);
251 node
= (struct device_node
*)bus
->sysdata
;
252 hose
= pci_find_hose_for_OF_device(node
);
255 if (!celleb_epci_get_epci_cfg(hose
))
256 return PCIBIOS_DEVICE_NOT_FOUND
;
258 if (bus
->number
== hose
->first_busno
&& devfn
== 0) {
259 /* EPCI controller self */
261 epci_base
= celleb_epci_get_epci_base(hose
);
262 addr
= epci_base
+ where
;
275 return PCIBIOS_DEVICE_NOT_FOUND
;
280 clear_and_disable_master_abort_interrupt(hose
);
281 addr
= celleb_epci_make_config_addr(bus
, hose
, devfn
, where
);
294 return PCIBIOS_DEVICE_NOT_FOUND
;
298 return celleb_epci_check_abort(hose
, addr
);
301 struct pci_ops celleb_epci_ops
= {
302 .read
= celleb_epci_read_config
,
303 .write
= celleb_epci_write_config
,
306 /* to be moved in FW */
307 static int __init
celleb_epci_init(struct pci_controller
*hose
)
311 PCI_IO_ADDR epci_base
;
314 epci_base
= celleb_epci_get_epci_base(hose
);
316 /* PCI core reset(Internal bus and PCI clock) */
317 reg
= epci_base
+ SCC_EPCI_CKCTRL
;
319 if (val
== 0x00030101)
322 val
&= ~(SCC_EPCI_CKCTRL_CRST0
| SCC_EPCI_CKCTRL_CRST1
);
325 /* set PCI core clock */
327 val
|= (SCC_EPCI_CKCTRL_OCLKEN
| SCC_EPCI_CKCTRL_LCLKEN
);
330 /* release PCI core reset (internal bus) */
332 val
|= SCC_EPCI_CKCTRL_CRST0
;
335 /* set PCI clock select */
336 reg
= epci_base
+ SCC_EPCI_CLKRST
;
338 val
&= ~SCC_EPCI_CLKRST_CKS_MASK
;
339 val
|= SCC_EPCI_CLKRST_CKS_2
;
343 reg
= epci_base
+ SCC_EPCI_ABTSET
;
344 out_be32(reg
, 0x0f1f001f); /* temporary value */
347 reg
= epci_base
+ SCC_EPCI_CLKRST
;
349 val
|= SCC_EPCI_CLKRST_BC
;
352 /* PCI clock enable */
354 val
|= SCC_EPCI_CLKRST_PCKEN
;
357 /* release PCI core reset (all) */
358 reg
= epci_base
+ SCC_EPCI_CKCTRL
;
360 val
|= (SCC_EPCI_CKCTRL_CRST0
| SCC_EPCI_CKCTRL_CRST1
);
363 /* set base translation registers. (already set by Beat) */
365 /* set base address masks. (already set by Beat) */
368 /* release interrupt masks and clear all interrupts */
369 reg
= epci_base
+ SCC_EPCI_INTSET
;
370 out_be32(reg
, 0x013f011f); /* all interrupts enable */
371 reg
= epci_base
+ SCC_EPCI_VIENAB
;
372 val
= SCC_EPCI_VIENAB_PMPEE
| SCC_EPCI_VIENAB_PMFEE
;
374 reg
= epci_base
+ SCC_EPCI_STATUS
;
375 out_be32(reg
, 0xffffffff);
376 reg
= epci_base
+ SCC_EPCI_VISTAT
;
377 out_be32(reg
, 0xffffffff);
379 /* disable PCI->IB address translation */
380 reg
= epci_base
+ SCC_EPCI_VCSR
;
382 val
&= ~(SCC_EPCI_VCSR_DR
| SCC_EPCI_VCSR_AT
);
385 /* set base addresses. (no need to set?) */
387 /* memory space, bus master enable */
388 reg
= epci_base
+ PCI_COMMAND
;
389 val
= PCI_COMMAND_MEMORY
| PCI_COMMAND_MASTER
;
392 /* endian mode setup */
393 reg
= epci_base
+ SCC_EPCI_ECMODE
;
397 /* set control option */
398 reg
= epci_base
+ SCC_EPCI_CNTOPT
;
400 val
|= SCC_EPCI_CNTOPT_O2PMB
;
403 /* XXX: temporay: set registers for address conversion setup */
404 reg
= epci_base
+ SCC_EPCI_CNF10_REG
;
405 out_be32(reg
, 0x80000008);
406 reg
= epci_base
+ SCC_EPCI_CNF14_REG
;
407 out_be32(reg
, 0x40000008);
409 reg
= epci_base
+ SCC_EPCI_BAM0
;
410 out_be32(reg
, 0x80000000);
411 reg
= epci_base
+ SCC_EPCI_BAM1
;
412 out_be32(reg
, 0xe0000000);
414 reg
= epci_base
+ SCC_EPCI_PVBAT
;
415 out_be32(reg
, 0x80000000);
418 /* release external PCI reset */
419 reg
= epci_base
+ SCC_EPCI_CLKRST
;
421 val
|= SCC_EPCI_CLKRST_PCIRST
;
428 int __init
celleb_setup_epci(struct device_node
*node
,
429 struct pci_controller
*hose
)
433 pr_debug("PCI: celleb_setup_epci()\n");
437 * Celleb epci uses cfg_addr and cfg_data member of
438 * pci_controller structure in irregular way.
440 * cfg_addr is used to map for control registers of
443 * cfg_data is used for configuration area of devices
444 * on Celleb epci buses.
447 if (of_address_to_resource(node
, 0, &r
))
449 hose
->cfg_addr
= ioremap(r
.start
, (r
.end
- r
.start
+ 1));
452 pr_debug("EPCI: cfg_addr map 0x%016lx->0x%016lx + 0x%016lx\n",
453 r
.start
, (unsigned long)hose
->cfg_addr
,
454 (r
.end
- r
.start
+ 1));
456 if (of_address_to_resource(node
, 2, &r
))
458 hose
->cfg_data
= ioremap(r
.start
, (r
.end
- r
.start
+ 1));
461 pr_debug("EPCI: cfg_data map 0x%016lx->0x%016lx + 0x%016lx\n",
462 r
.start
, (unsigned long)hose
->cfg_data
,
463 (r
.end
- r
.start
+ 1));
465 hose
->private_data
= kzalloc(sizeof(struct epci_private
), GFP_KERNEL
);
466 if (hose
->private_data
== NULL
) {
467 printk(KERN_ERR
"EPCI: no memory for private data.\n");
471 hose
->ops
= &celleb_epci_ops
;
472 celleb_epci_init(hose
);
477 kfree(hose
->private_data
);
480 iounmap(hose
->cfg_addr
);
483 iounmap(hose
->cfg_data
);