1 |
/* |
2 |
* Copyright (C) 2005-2007 Anders Gavare. All rights reserved. |
3 |
* |
4 |
* Redistribution and use in source and binary forms, with or without |
5 |
* modification, are permitted provided that the following conditions are met: |
6 |
* |
7 |
* 1. Redistributions of source code must retain the above copyright |
8 |
* notice, this list of conditions and the following disclaimer. |
9 |
* 2. Redistributions in binary form must reproduce the above copyright |
10 |
* notice, this list of conditions and the following disclaimer in the |
11 |
* documentation and/or other materials provided with the distribution. |
12 |
* 3. The name of the author may not be used to endorse or promote products |
13 |
* derived from this software without specific prior written permission. |
14 |
* |
15 |
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
16 |
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
17 |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
18 |
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
19 |
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
20 |
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
21 |
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
22 |
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
23 |
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
24 |
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
25 |
* SUCH DAMAGE. |
26 |
* |
27 |
* |
28 |
* $Id: dev_uninorth.c,v 1.9 2007/06/15 19:57:34 debug Exp $ |
29 |
* |
30 |
* COMMENT: Uni-North PCI controller (used by MacPPC machines) |
31 |
*/ |
32 |
|
33 |
#include <stdio.h> |
34 |
#include <stdlib.h> |
35 |
#include <string.h> |
36 |
|
37 |
#include "bus_pci.h" |
38 |
#include "cpu.h" |
39 |
#include "devices.h" |
40 |
#include "machine.h" |
41 |
#include "memory.h" |
42 |
#include "misc.h" |
43 |
|
44 |
|
45 |
struct uninorth_data { |
46 |
int pciirq; |
47 |
|
48 |
struct pci_data *pci_data; |
49 |
uint64_t cur_addr; |
50 |
}; |
51 |
|
52 |
|
53 |
DEVICE_ACCESS(uninorth_addr) |
54 |
{ |
55 |
struct uninorth_data *d = extra; |
56 |
|
57 |
if (writeflag == MEM_WRITE) { |
58 |
uint64_t idata = memory_readmax64(cpu, data, len |
59 |
| MEM_PCI_LITTLE_ENDIAN); |
60 |
int bus, dev, func, reg; |
61 |
|
62 |
d->cur_addr = idata; |
63 |
if (idata == 0) |
64 |
return 0; |
65 |
|
66 |
/* Decompose the Uni-North tag: */ |
67 |
if (idata & 1) { |
68 |
idata &= ~1; |
69 |
bus_pci_decompose_1(idata, &bus, &dev, &func, ®); |
70 |
} else { |
71 |
bus = 0; |
72 |
for (dev=11; dev<32; dev++) |
73 |
if (idata & (1 << dev)) |
74 |
break; |
75 |
if (dev == 32) |
76 |
fatal("[ dev_uninorth_addr_access: no dev? " |
77 |
"idata=0x%08x ]\n", (int)idata); |
78 |
|
79 |
func = (idata >> 8) & 7; |
80 |
reg = idata & 0xff; |
81 |
} |
82 |
|
83 |
bus_pci_setaddr(cpu, d->pci_data, bus, dev, func, reg); |
84 |
} else { |
85 |
/* TODO: is returning the current address like this |
86 |
the correct behaviour? */ |
87 |
memory_writemax64(cpu, data, len | MEM_PCI_LITTLE_ENDIAN, |
88 |
d->cur_addr); |
89 |
} |
90 |
|
91 |
return 1; |
92 |
} |
93 |
|
94 |
|
95 |
DEVICE_ACCESS(uninorth_data) |
96 |
{ |
97 |
struct uninorth_data *d = extra; |
98 |
uint64_t idata = 0, odata = 0; |
99 |
|
100 |
if (writeflag == MEM_WRITE) |
101 |
idata = memory_readmax64(cpu, data, len|MEM_PCI_LITTLE_ENDIAN); |
102 |
|
103 |
bus_pci_data_access(cpu, d->pci_data, writeflag == MEM_READ? &odata : |
104 |
&idata, len, writeflag); |
105 |
|
106 |
if (writeflag == MEM_READ) |
107 |
memory_writemax64(cpu, data, len|MEM_PCI_LITTLE_ENDIAN, odata); |
108 |
|
109 |
return 1; |
110 |
} |
111 |
|
112 |
|
113 |
struct pci_data *dev_uninorth_init(struct machine *machine, struct memory *mem, |
114 |
uint64_t addr, int isa_irqbase, int pciirq) |
115 |
{ |
116 |
struct uninorth_data *d; |
117 |
uint64_t pci_io_offset, pci_mem_offset; |
118 |
uint64_t isa_portbase = 0, isa_membase = 0; |
119 |
uint64_t pci_portbase = 0, pci_membase = 0; |
120 |
|
121 |
CHECK_ALLOCATION(d = malloc(sizeof(struct uninorth_data))); |
122 |
memset(d, 0, sizeof(struct uninorth_data)); |
123 |
|
124 |
d->pciirq = pciirq; |
125 |
|
126 |
pci_io_offset = 0x00000000ULL; |
127 |
pci_mem_offset = 0x00000000ULL; |
128 |
pci_portbase = 0xd0000000ULL; |
129 |
pci_membase = 0xd1000000ULL; |
130 |
isa_portbase = 0xd2000000ULL; |
131 |
isa_membase = 0xd3000000ULL; |
132 |
|
133 |
/* Create a PCI bus: */ |
134 |
d->pci_data = bus_pci_init(machine, "ZZZ_irq_stuff", |
135 |
pci_io_offset, pci_mem_offset, |
136 |
pci_portbase, pci_membase, "XXX_pci_irqbase", |
137 |
isa_portbase, isa_membase, "YYY_isa_irqbase"); |
138 |
|
139 |
/* Add the PCI glue for the controller itself: */ |
140 |
bus_pci_add(machine, d->pci_data, mem, 0, 11, 0, "uninorth"); |
141 |
|
142 |
/* ADDR and DATA configuration ports: */ |
143 |
memory_device_register(mem, "uninorth_pci_addr", addr + 0x800000, |
144 |
4, dev_uninorth_addr_access, d, DM_DEFAULT, NULL); |
145 |
memory_device_register(mem, "uninorth_pci_data", addr + 0xc00000, |
146 |
8, dev_uninorth_data_access, d, DM_DEFAULT, NULL); |
147 |
|
148 |
return d->pci_data; |
149 |
} |
150 |
|