2f42fe752c6bbc929dbc48224af56201fdd78c22
[coreboot.git] / src / southbridge / ricoh / rl5c476 / rl5c476.c
1 /*
2  * (C) Copyright 2004 Nick Barker <nick.barker9@btinternet.com>
3  *
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License as
7  * published by the Free Software Foundation; either version 2 of
8  * the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
18  * MA 02111-1307 USA
19  */
20
21
22
23 #include <arch/io.h>
24 #include <device/device.h>
25 #include <device/pci.h>
26 #include <device/pci_ops.h>
27 #include <device/pci_ids.h>
28 #include <console/console.h>
29 #include "rl5c476.h"
30 #include "chip.h"
31
32 static void udelay(int i){
33         for(; i > 0 ; i--)
34                 inb(0x80);
35
36 }
37
38 static void
39 dump_south(void)
40 {
41         device_t dev0;
42         dev0 = dev_find_device(PCI_VENDOR_ID_RICOH, PCI_DEVICE_ID_RICOH_RL5C476, 0);
43         dev0 = dev_find_device(PCI_VENDOR_ID_RICOH, PCI_DEVICE_ID_RICOH_RL5C476, dev0);
44         int i,j;
45         
46         for(i = 0; i < 256; i += 16) {
47                 printk_debug("0x%x: ", i);
48                 for(j = 0; j < 16; j++) {
49                         printk_debug("%02x ", pci_read_config8(dev0, i+j));
50                 }
51                 printk_debug("\n");
52         }
53         printk_debug("Card32\n");
54         for(i = 0 ; i < 256 ; i+=16){
55                 printk_debug("0x%x: ",i);
56                 for(j = 0 ; j < 16 ; j++){
57                         printk_debug(" %02x",*(unsigned char *)(0x80000000+i+j));
58                 }
59                 printk_debug("\n");
60         }
61         printk_debug("Card16\n");
62         for(i = 0; i < 256; i += 16) {
63                 printk_debug("0x%x: ", i);
64                 for(j = 0; j < 16; j++) {
65                         printk_debug("%02x ", *(unsigned char *)(0x80000800+ i+j));
66                 }
67                 printk_debug("\n");
68         }
69         printk_debug("CF Config\n");
70         for(i = 0 ; i < 256 ; i+=16){
71                 printk_debug("0x%x: ",i);
72                 for(j=0 ; j < 16 ; j++){
73                         printk_debug("%02x ",*(unsigned char *)(0x81000200 + i + j));
74                 }
75                 printk_debug("\n");
76         }
77 }
78
79
80 static void rl5c476_init(device_t dev)
81 {
82         //unsigned char enables;
83         pc16reg_t *pc16;
84         int i;
85
86 #error "FIXME implement carbus bridge support"
87 #error "FIXME this code is close to a but the conversion needs more work"
88         /* cardbus controller function 1 for CF Socket */
89         printk_debug("rl5c476 init\n");
90
91         /* setup pci header manually because 'pci_device.c' doesn't know how to handle
92          * pci to cardbus bridges - (header type 2 I think)
93          */
94
95
96         /* initialize function zero - pcmcia socket so it behaves itself */
97         /* FIXME - statically put control memory at 0xe0000000 for now
98          * one day the pci_device allocator might do this */
99         pci_write_config32(dev,0x10,0xe0000000);
100         pci_write_config8(dev,0x0d,0x20);
101         pci_write_config8(dev,0x19,0x02);
102         pci_write_config8(dev,0x1a,0x02);
103         pci_write_config8(dev,0x1b,0x20);
104         //pci_write_config8(dev,0x3c,0);
105         pci_write_config8(dev,0x82,0x00a0);
106         pci_write_config16(dev,0x04,0x07);
107
108         
109         /* get second function - i.e. compact flash socket */
110         dev = dev_find_device(PCI_VENDOR_ID_RICOH, PCI_DEVICE_ID_RICOH_RL5C476, dev);
111
112
113         /* FIXME - control structure statically declared at 0xe0008000 for now */
114         pci_write_config32(dev,0x10,0xe0008000);
115         pci_write_config8(dev,0x0d,0x20);
116         pci_write_config8(dev,0x19,0x03);
117         pci_write_config8(dev,0x1a,0x03);
118         pci_write_config8(dev,0x1b,0x20);
119
120         //pci_write_config8(dev,0x3c,0x0);
121         pci_write_config16(dev,0x3e,0x0780);
122         pci_write_config16(dev,0x82,0x00a0);
123
124         pci_write_config16(dev,0x04,0x07);
125
126
127         /* pick up where 16 bit card control structure is */
128         pc16 = (pc16reg_t *)(0xe0008800);
129
130         /* disable memory and io windows and turn off socket power */
131         pc16->pwctrl = 0;
132
133         /* disable irq lines */
134         pc16->igctrl = 0;
135
136         /* disable memory and I/O windows */
137         pc16->awinen = 0;
138
139         /* reset card, configure for I/O and set IRQ line */
140         pc16->igctrl = 0x69;
141
142
143         // set io window 0 for 1e8 - 1ef
144         pc16->iostl0 = 0xe8;
145         pc16->iosth0 = 1;
146
147         pc16->iospl0 = 0xef;
148         pc16->iosph0 = 1;
149
150         // add io offset of 8 so that CF card will decode 0x1e8 as 0x1f0 i.e. the first byte of
151         // a 16 byte aligned, 16 byte window etc
152         pc16->ioffl0 = 0x8;
153         pc16->ioffh0 = 0;
154
155         // set io window 1 for 3ed - 3ee
156         pc16->iostl1 = 0xed;
157         pc16->iosth1 = 3;
158
159         pc16->iospl1 = 0xee;
160         pc16->iosph1 = 3;
161
162         pc16->ioffl1 = 0x0;
163         pc16->ioffh1 = 0;
164
165
166         // FIXME statically declare CF config window at 0xe1000000
167         pc16->smstl0 = 0;
168         pc16->smsth0 = 0;
169         pc16->smspl0 = 0;
170         pc16->smsph0 = 0x80;
171         pc16->moffl0 = 0;
172         pc16->moffh0 = 0x40;
173         pc16->smpga0 = 0xe1;
174
175         // set I/O width for Auto Data width
176         pc16->ioctrl = 0x22;
177
178
179         // enable I/O window 0 and 1
180         pc16->awinen = 0xc1;
181
182
183         pc16->miscc1 = 1;
184
185         // apply power and enable outputs
186         pc16->pwctrl = 0xb0;
187         
188
189         // delay could be optimised, but this works
190         udelay(100000);
191         
192         pc16->igctrl = 0x69;
193
194         unsigned char *cptr;
195         cptr = (unsigned char *)(0xe1000200);
196         printk_debug("CF Config = %x\n",*cptr);
197
198         // FIX Me 16 bit CF always have first config byte at 0x200 into Config structure,
199         // but CF+ May Not according to spec - should locate through reading tuple data,
200         // but this will do for now !!!
201
202
203         // set CF to decode 16 IO bytes on any 16 byte boundary - rely on the io
204         // windows of the bridge set up above to map those bytes into the 
205         // addresses for ide controller 3 (0x1e8 - 0x1ef and 0x3ed - 0x3ee)
206         *cptr = 0x41;
207
208
209 }
210
211 static struct device_operations ricoh_rl5c476_ops = {
212         .read_resources   = pci_bus_read_resources,
213         .set_resources    = pci_dev_set_resources,
214         .enable_resources = pci_bus_enable_resources,
215         .inti             = rl5c476_init,
216         .scan_bus         = pci_scan_bridge,
217 };
218
219 static struct pci_driver ricoh_rl5c476_driver __pci_driver = {
220         .ops    = &ricoh_rl5c476_ops,
221         .vendor = PCI_VENDOR_ID_RICOH,
222         .device = PCI_DEVICE_ID_RICOH_RL5C476,
223 };
224
225 struct chip_operations southbridge_ricoh_rl5c476_control = {
226         CHIP_NAME("RICOH RL5C476")
227         .enable    = southbridge_init,
228 };