1 /*
2 $ $Id: pci-dc.c,v 1.5 2001/08/24 12:38:19 dwmw2 Exp $
3 * Dreamcast PCI: Supports SEGA Broadband Adaptor only.
4 */
5
6 #include <linux/config.h>
7 #include <linux/sched.h>
8 #include <linux/kernel.h>
9 #include <linux/param.h>
10 #include <linux/interrupt.h>
11 #include <linux/init.h>
12 #include <linux/irq.h>
13 #include <linux/pci.h>
14
15 #include <asm/io.h>
16 #include <asm/irq.h>
17 #include <asm/dc_sysasic.h>
18
19 #define GAPSPCI_REGS 0x01001400
20 #define GAPSPCI_DMA_BASE 0x01840000
21 #define GAPSPCI_DMA_SIZE 32768
22 #define GAPSPCI_BBA_CONFIG 0x01001600
23
24 #define GAPSPCI_IRQ HW_EVENT_EXTERNAL
25
26 static int gapspci_dma_used;
27
28 static struct pci_bus *pci_root_bus;
29
30 struct pci_fixup pcibios_fixups[] = {
31 {0, 0, 0, NULL}
32 };
33
34 #define BBA_SELECTED(dev) (dev->bus->number==0 && dev->devfn==0)
35
gapspci_read_config_byte(struct pci_dev * dev,int where,u8 * val)36 static int gapspci_read_config_byte(struct pci_dev *dev, int where,
37 u8 * val)
38 {
39 if (BBA_SELECTED(dev))
40 *val = inb(GAPSPCI_BBA_CONFIG+where);
41 else
42 *val = 0xff;
43
44 return PCIBIOS_SUCCESSFUL;
45 }
46
gapspci_read_config_word(struct pci_dev * dev,int where,u16 * val)47 static int gapspci_read_config_word(struct pci_dev *dev, int where,
48 u16 * val)
49 {
50 if (BBA_SELECTED(dev))
51 *val = inw(GAPSPCI_BBA_CONFIG+where);
52 else
53 *val = 0xffff;
54
55 return PCIBIOS_SUCCESSFUL;
56 }
57
gapspci_read_config_dword(struct pci_dev * dev,int where,u32 * val)58 static int gapspci_read_config_dword(struct pci_dev *dev, int where,
59 u32 * val)
60 {
61 if (BBA_SELECTED(dev))
62 *val = inl(GAPSPCI_BBA_CONFIG+where);
63 else
64 *val = 0xffffffff;
65
66 return PCIBIOS_SUCCESSFUL;
67 }
68
gapspci_write_config_byte(struct pci_dev * dev,int where,u8 val)69 static int gapspci_write_config_byte(struct pci_dev *dev, int where,
70 u8 val)
71 {
72 if (BBA_SELECTED(dev))
73 outb(val, GAPSPCI_BBA_CONFIG+where);
74
75 return PCIBIOS_SUCCESSFUL;
76 }
77
78
gapspci_write_config_word(struct pci_dev * dev,int where,u16 val)79 static int gapspci_write_config_word(struct pci_dev *dev, int where,
80 u16 val)
81 {
82 if (BBA_SELECTED(dev))
83 outw(val, GAPSPCI_BBA_CONFIG+where);
84
85 return PCIBIOS_SUCCESSFUL;
86 }
87
gapspci_write_config_dword(struct pci_dev * dev,int where,u32 val)88 static int gapspci_write_config_dword(struct pci_dev *dev, int where,
89 u32 val)
90 {
91 if (BBA_SELECTED(dev))
92 outl(val, GAPSPCI_BBA_CONFIG+where);
93
94 return PCIBIOS_SUCCESSFUL;
95 }
96
97 static struct pci_ops pci_config_ops = {
98 gapspci_read_config_byte,
99 gapspci_read_config_word,
100 gapspci_read_config_dword,
101 gapspci_write_config_byte,
102 gapspci_write_config_word,
103 gapspci_write_config_dword
104 };
105
106
pci_alloc_consistent(struct pci_dev * hwdev,size_t size,dma_addr_t * dma_handle)107 void *pci_alloc_consistent(struct pci_dev *hwdev, size_t size,
108 dma_addr_t * dma_handle)
109 {
110 unsigned long buf;
111
112 if (gapspci_dma_used+size > GAPSPCI_DMA_SIZE)
113 return NULL;
114
115 buf = GAPSPCI_DMA_BASE+gapspci_dma_used;
116
117 gapspci_dma_used = PAGE_ALIGN(gapspci_dma_used+size);
118
119 printk("pci_alloc_consistent: %ld bytes at 0x%lx\n", (long)size, buf);
120
121 *dma_handle = (dma_addr_t)buf;
122
123 return (void *)P2SEGADDR(buf);
124 }
125
126
pci_free_consistent(struct pci_dev * hwdev,size_t size,void * vaddr,dma_addr_t dma_handle)127 void pci_free_consistent(struct pci_dev *hwdev, size_t size,
128 void *vaddr, dma_addr_t dma_handle)
129 {
130 /* XXX */
131 gapspci_dma_used = 0;
132 }
133
134
pcibios_fixup_pbus_ranges(struct pci_bus * bus,struct pbus_set_ranges_data * ranges)135 void __init pcibios_fixup_pbus_ranges(struct pci_bus *bus, struct pbus_set_ranges_data *ranges)
136 {
137 }
138
pcibios_fixup_bus(struct pci_bus * bus)139 void __init pcibios_fixup_bus(struct pci_bus *bus)
140 {
141 struct list_head *ln;
142 struct pci_dev *dev;
143
144 for (ln=bus->devices.next; ln != &bus->devices; ln=ln->next) {
145 dev = pci_dev_b(ln);
146 if (!BBA_SELECTED(dev)) continue;
147
148 printk("PCI: MMIO fixup to %s\n", dev->name);
149 dev->resource[1].start=0x01001700;
150 dev->resource[1].end=0x010017ff;
151 }
152 }
153
154
no_swizzle(struct pci_dev * dev,u8 * pin)155 static u8 __init no_swizzle(struct pci_dev *dev, u8 * pin)
156 {
157 return PCI_SLOT(dev->devfn);
158 }
159
160
map_dc_irq(struct pci_dev * dev,u8 slot,u8 pin)161 static int __init map_dc_irq(struct pci_dev *dev, u8 slot, u8 pin)
162 {
163 return GAPSPCI_IRQ;
164 }
165
166
pcibios_init(void)167 void __init pcibios_init(void)
168 {
169 pci_root_bus = pci_scan_bus(0, &pci_config_ops, NULL);
170 /* pci_assign_unassigned_resources(); */
171 pci_fixup_irqs(no_swizzle, map_dc_irq);
172 }
173
174
175 /* Haven't done anything here as yet */
pcibios_setup(char * str)176 char * __init pcibios_setup(char *str)
177 {
178 return str;
179 }
180
181
gapspci_init(void)182 int __init gapspci_init(void)
183 {
184 int i;
185 char idbuf[16];
186
187 for(i=0; i<16; i++)
188 idbuf[i]=inb(GAPSPCI_REGS+i);
189
190 if(strncmp(idbuf, "GAPSPCI_BRIDGE_2", 16))
191 return -1;
192
193 outl(0x5a14a501, GAPSPCI_REGS+0x18);
194
195 for(i=0; i<1000000; i++);
196
197 if(inl(GAPSPCI_REGS+0x18)!=1)
198 return -1;
199
200 outl(0x01000000, GAPSPCI_REGS+0x20);
201 outl(0x01000000, GAPSPCI_REGS+0x24);
202
203 outl(GAPSPCI_DMA_BASE, GAPSPCI_REGS+0x28);
204 outl(GAPSPCI_DMA_BASE+GAPSPCI_DMA_SIZE, GAPSPCI_REGS+0x2c);
205
206 outl(1, GAPSPCI_REGS+0x14);
207 outl(1, GAPSPCI_REGS+0x34);
208
209 gapspci_dma_used=0;
210
211 /* Setting Broadband Adapter */
212 outw(0xf900, GAPSPCI_BBA_CONFIG+0x06);
213 outl(0x00000000, GAPSPCI_BBA_CONFIG+0x30);
214 outb(0x00, GAPSPCI_BBA_CONFIG+0x3c);
215 outb(0xf0, GAPSPCI_BBA_CONFIG+0x0d);
216 outw(0x0006, GAPSPCI_BBA_CONFIG+0x04);
217 outl(0x00002001, GAPSPCI_BBA_CONFIG+0x10);
218 outl(0x01000000, GAPSPCI_BBA_CONFIG+0x14);
219
220 return 0;
221 }
222