SP5100's code is based on SB700. Change the legacy sb700 of sb7xx_51xx.
[coreboot.git] / src / southbridge / amd / sb700 / usb.c
1 /*
2  * This file is part of the coreboot project.
3  *
4  * Copyright (C) 2010 Advanced Micro Devices, Inc.
5  *
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; version 2 of the License.
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., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
18  */
19
20 #include <console/console.h>
21 #include <device/device.h>
22 #include <device/pci.h>
23 #include <device/pci_ids.h>
24 #include <device/pci_ops.h>
25 #include <usbdebug.h>
26 #include <arch/io.h>
27 #include "sb700.h"
28
29 static struct pci_operations lops_pci = {
30         .set_subsystem = pci_dev_set_subsystem,
31 };
32
33 static void usb_init(struct device *dev)
34 {
35         u8 byte;
36         u16 word;
37         u32 dword;
38
39         /* 6.1 Enable OHCI0-4 and EHCI Controllers */
40         device_t sm_dev;
41         sm_dev = dev_find_slot(0, PCI_DEVFN(0x14, 0));
42         byte = pci_read_config8(sm_dev, 0x68);
43         byte |= 0xFF;
44         pci_write_config8(sm_dev, 0x68, byte);
45
46         /* RPR 6.2 Enables the USB PME Event,Enable USB resume support */
47         byte = pm_ioread(0x61);
48         byte |= 1 << 6;
49         pm_iowrite(0x61, byte);
50         byte = pm_ioread(0x65);
51         byte |= 1 << 2;
52         pm_iowrite(0x65, byte);
53
54         /* RPR 6.3 Support USB device wakeup from the S4/S5 state */
55         byte = pm_ioread(0x65);
56         byte &= ~(1 << 0);
57         pm_iowrite(0x65, byte);
58
59         /* RPR 6.5 Enable the USB controller to get reset by any software that generate a PCIRst# condition */
60         byte = pm_ioread(0x65);
61         byte |= (1 << 4);
62         pm_iowrite(0x65, byte);
63
64         /* USB_ADVANCED_SLEEP_CONTROL */
65         byte = pm_ioread(0x95);
66         byte &= ~(7 << 0);
67         byte |= 6 << 0;         /* Advanced sleep up to 6 uframes */
68         pm_iowrite(0x95, byte);
69
70         /* RPR 6.10 Disable OHCI MSI Capability. */
71         word = pci_read_config16(dev, 0x40);
72         word |= (0x3 << 8);
73         pci_write_config16(dev, 0x40, word);
74
75         /* USB-1_OHCI0_Corner Case S3 Wake Up */
76         dword = pci_read_config32(dev, 0x50);
77         dword |= (1 << 16);
78         pci_write_config32(dev, 0x50, dword);
79 }
80
81 static void usb_init2(struct device *dev)
82 {
83         u32 dword;
84         u32 usb2_bar0;
85         device_t sm_dev;
86         u8 rev;
87
88         sm_dev = dev_find_slot(0, PCI_DEVFN(0x14, 0));
89         rev = get_sb700_revision(sm_dev);
90
91         /* dword = pci_read_config32(dev, 0xf8); */
92         /* dword |= 40; */
93         /* pci_write_config32(dev, 0xf8, dword); */
94
95         usb2_bar0 = pci_read_config32(dev, 0x10) & ~0xFF;
96         printk(BIOS_INFO, "usb2_bar0=0x%x\n", usb2_bar0);
97
98         /* RPR6.4 Enables the USB PHY auto calibration resister to match 45ohm resistance */
99         dword = 0x00020F00;
100         write32(usb2_bar0 + 0xC0, dword);
101
102         /* RPR6.9 Sets In/OUT FIFO threshold for best performance */
103         dword = 0x00400040;
104         write32(usb2_bar0 + 0xA4, dword);
105
106         /* RPR6.11 Disabling EHCI Advance Asynchronous Enhancement */
107         dword = pci_read_config32(dev, 0x50);
108         dword |= (1 << 28);
109         pci_write_config32(dev, 0x50, dword);
110
111         /* RPR 6.12 EHCI Advance PHY Power Savings */
112         /* RPR says it is just for A12. CIMM sets it when it is above A11. */
113         /* But it makes the linux crash, so we skip it */
114         dword = pci_read_config32(dev, 0x50);
115         dword |= 1 << 31;
116         pci_write_config32(dev, 0x50, dword);
117
118         /* RPR6.13 Enabling Fix for EHCI Controller Driver Yellow Sign Issue */
119         /* RPR says it is just for A12. CIMx sets it when it is above A11. */
120         dword = pci_read_config32(dev, 0x50);
121         dword |= (1 << 20);
122         pci_write_config32(dev, 0x50, dword);
123
124         /* RPR6.15 EHCI Async Park Mode */
125         dword = pci_read_config32(dev, 0x50);
126         dword |= (1 << 23);
127         pci_write_config32(dev, 0x50, dword);
128
129         /* Each step below causes the linux crashes. Leave them here
130          * for future debugging. */
131         u8 byte;
132         u16 word;
133
134         /* RPR6.16 Disable EHCI MSI support */
135         byte = pci_read_config8(dev, 0x50);
136         byte |= (1 << 6);
137         pci_write_config8(dev, 0x50, byte);
138
139         /* RPR6.17 Disable the EHCI Dynamic Power Saving feature */
140         word = read32(usb2_bar0 + 0xBC);
141         word &= ~(1 << 12);
142         write16(usb2_bar0 + 0xBC, word);
143
144         /* RPR6.19 USB Controller DMA Read Delay Tolerant. */
145         if (rev >= REV_SB700_A14) {
146                 byte = pci_read_config8(dev, 0x50);
147                 byte |= (1 << 7);
148                 pci_write_config8(dev, 0x50, byte);
149         }
150
151         /* SB700_A15, USB-2_EHCI_PID_ERROR_CHECKING */
152         if (rev == REV_SB700_A15) {
153                 word = pci_read_config16(dev, 0x50);
154                 word |= (1 << 9);
155                 pci_write_config16(dev, 0x50, word);
156         }
157
158         /* RPR6.20 Async Park Mode. */
159         /* RPR recommends not to set these bits. */
160         #if 0
161         dword = pci_read_config32(dev, 0x50);
162         dword |= 1 << 23;
163         if (rev >= REV_SB700_A14) {
164                 dword &= ~(1 << 2);
165         }
166         pci_write_config32(dev, 0x50, dword);
167         #endif
168
169         /* RPR6.22 Advance Async Enhancement */
170         /* RPR6.23 USB Periodic Cache Setting */
171         dword = pci_read_config32(dev, 0x50);
172         if (rev == REV_SB700_A12) {
173                 dword |= 1 << 28; /* 6.22 */
174                 dword |= 1 << 27; /* 6.23 */
175         } else if (rev >= REV_SB700_A14) {
176                 dword |= 1 << 3;
177                 dword &= ~(1 << 28); /* 6.22 */
178                 dword |= 1 << 8;
179                 dword &= ~(1 << 27); /* 6.23 */
180         }
181         printk(BIOS_DEBUG, "rpr 6.23, final dword=%x\n", dword);
182 }
183
184 static void usb_set_resources(struct device *dev)
185 {
186 #if CONFIG_USBDEBUG
187         struct resource *res;
188         u32 base;
189         u32 old_debug;
190
191         old_debug = get_ehci_debug();
192         set_ehci_debug(0);
193 #endif
194         pci_dev_set_resources(dev);
195
196 #if CONFIG_USBDEBUG
197         res = find_resource(dev, 0x10);
198         set_ehci_debug(old_debug);
199         if (!res)
200                 return;
201         base = res->base;
202         set_ehci_base(base);
203         report_resource_stored(dev, res, "");
204 #endif
205 }
206
207 static struct device_operations usb_ops = {
208         .read_resources = pci_dev_read_resources,
209         .set_resources = usb_set_resources, /* pci_dev_set_resources, */
210         .enable_resources = pci_dev_enable_resources,
211         .init = usb_init,
212         .scan_bus = 0,
213         .ops_pci = &lops_pci,
214 };
215
216 static const struct pci_driver usb_0_driver __pci_driver = {
217         .ops = &usb_ops,
218         .vendor = PCI_VENDOR_ID_ATI,
219         .device = PCI_DEVICE_ID_ATI_SB700_USB_18_0,
220 };
221
222 static const struct pci_driver usb_1_driver __pci_driver = {
223         .ops = &usb_ops,
224         .vendor = PCI_VENDOR_ID_ATI,
225         .device = PCI_DEVICE_ID_ATI_SB700_USB_18_1,
226 };
227
228 /* the pci id of usb ctrl 0 and 1 are the same. */
229 /*
230  * static const struct pci_driver usb_3_driver __pci_driver = {
231  *      .ops = &usb_ops,
232  *      .vendor = PCI_VENDOR_ID_ATI,
233  *      .device = PCI_DEVICE_ID_ATI_SB700_USB_19_0,
234  * };
235  * static const struct pci_driver usb_4_driver __pci_driver = {
236  *      .ops = &usb_ops,
237  *      .vendor = PCI_VENDOR_ID_ATI,
238  *      .device = PCI_DEVICE_ID_ATI_SB700_USB_19_1,
239  * };
240  */
241
242 static const struct pci_driver usb_4_driver __pci_driver = {
243         .ops = &usb_ops,
244         .vendor = PCI_VENDOR_ID_ATI,
245         .device = PCI_DEVICE_ID_ATI_SB700_USB_20_5,
246 };
247
248 static struct device_operations usb_ops2 = {
249         .read_resources = pci_dev_read_resources,
250         .set_resources = usb_set_resources, /* pci_dev_set_resources, */
251         .enable_resources = pci_dev_enable_resources,
252         .init = usb_init2,
253         .scan_bus = 0,
254         .ops_pci = &lops_pci,
255 };
256
257 static const struct pci_driver usb_5_driver __pci_driver = {
258         .ops = &usb_ops2,
259         .vendor = PCI_VENDOR_ID_ATI,
260         .device = PCI_DEVICE_ID_ATI_SB700_USB_18_2,
261 };
262 /*
263  * static const struct pci_driver usb_5_driver __pci_driver = {
264  *      .ops = &usb_ops2,
265  *      .vendor = PCI_VENDOR_ID_ATI,
266  *      .device = PCI_DEVICE_ID_ATI_SB700_USB_19_2,
267  * };
268  */