AMD southbridge: remove sp5100
[coreboot.git] / src / southbridge / amd / sb700 / sata.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 <delay.h>
23 #include <device/pci.h>
24 #include <device/pci_ids.h>
25 #include <device/pci_ops.h>
26 #include <arch/io.h>
27 #include "sb700.h"
28
29 static int sata_drive_detect(int portnum, u16 iobar)
30 {
31         u8 byte, byte2;
32         int i = 0;
33         outb(0xA0 + 0x10 * (portnum % 2), iobar + 0x6);
34         while (byte = inb(iobar + 0x6), byte2 = inb(iobar + 0x7),
35                 (byte != (0xA0 + 0x10 * (portnum % 2))) ||
36                 ((byte2 & 0x88) != 0)) {
37                 printk(BIOS_SPEW, "0x6=%x, 0x7=%x\n", byte, byte2);
38                 if (byte != (0xA0 + 0x10 * (portnum % 2))) {
39                         /* This will happen at the first iteration of this loop
40                          * if the first SATA port is unpopulated and the
41                          * second SATA port is poulated.
42                          */
43                         printk(BIOS_DEBUG, "drive no longer selected after %i ms, "
44                                 "retrying init\n", i * 10);
45                         return 1;
46                 } else
47                         printk(BIOS_SPEW, "drive detection not yet completed, "
48                                 "waiting...\n");
49                 mdelay(10);
50                 i++;
51         }
52         printk(BIOS_SPEW, "drive detection done after %i ms\n", i * 10);
53         return 0;
54 }
55
56 /* This function can be overloaded in mainboard.c */
57 void __attribute__((weak)) sb7xx_51xx_setup_sata_phys(struct device *dev)
58 {
59         /* RPR7.6.1 Program the PHY Global Control to 0x2C00 */
60         pci_write_config16(dev, 0x86, 0x2c00);
61
62         /* RPR7.6.2 SATA GENI PHY ports setting */
63         pci_write_config32(dev, 0x88, 0x01B48017);
64         pci_write_config32(dev, 0x8c, 0x01B48019);
65         pci_write_config32(dev, 0x90, 0x01B48016);
66         pci_write_config32(dev, 0x94, 0x01B48016);
67         pci_write_config32(dev, 0x98, 0x01B48016);
68         pci_write_config32(dev, 0x9C, 0x01B48016);
69
70         /* RPR7.6.3 SATA GEN II PHY port setting for port [0~5]. */
71         pci_write_config16(dev, 0xA0, 0xA09A);
72         pci_write_config16(dev, 0xA2, 0xA09F);
73         pci_write_config16(dev, 0xA4, 0xA07A);
74         pci_write_config16(dev, 0xA6, 0xA07A);
75         pci_write_config16(dev, 0xA8, 0xA07A);
76         pci_write_config16(dev, 0xAA, 0xA07A);
77 }
78
79 static void sata_init(struct device *dev)
80 {
81         u8 byte;
82         u16 word;
83         u32 dword;
84         u8 rev_id;
85         u32 sata_bar5;
86         u16 sata_bar0, sata_bar1, sata_bar2, sata_bar3, sata_bar4;
87         int i, j;
88
89         device_t sm_dev;
90         /* SATA SMBus Disable */
91         sm_dev = dev_find_slot(0, PCI_DEVFN(0x14, 0));
92
93         byte = pci_read_config8(sm_dev, 0xad);
94         /* Disable SATA SMBUS */
95         byte |= (1 << 0);
96         /* Enable SATA and power saving */
97         byte |= (1 << 1);
98         byte |= (1 << 5);
99         pci_write_config8(sm_dev, 0xad, byte);
100
101         /* RPR 7.2 SATA Initialization */
102         /* Set the interrupt Mapping to INTG# */
103         byte = pci_read_config8(sm_dev, 0xaf);
104         byte = 0x6 << 2;
105         pci_write_config8(sm_dev, 0xaf, byte);
106
107         /* get rev_id */
108         rev_id = pci_read_config8(sm_dev, 0x08) - 0x28;
109
110         /* get base address */
111         sata_bar5 = pci_read_config32(dev, 0x24) & ~0x3FF;
112         sata_bar0 = pci_read_config16(dev, 0x10) & ~0x7;
113         sata_bar1 = pci_read_config16(dev, 0x14) & ~0x3;
114         sata_bar2 = pci_read_config16(dev, 0x18) & ~0x7;
115         sata_bar3 = pci_read_config16(dev, 0x1C) & ~0x3;
116         sata_bar4 = pci_read_config16(dev, 0x20) & ~0xf;
117
118         printk(BIOS_SPEW, "sata_bar0=%x\n", sata_bar0); /* 3030 */
119         printk(BIOS_SPEW, "sata_bar1=%x\n", sata_bar1); /* 3070 */
120         printk(BIOS_SPEW, "sata_bar2=%x\n", sata_bar2); /* 3040 */
121         printk(BIOS_SPEW, "sata_bar3=%x\n", sata_bar3); /* 3080 */
122         printk(BIOS_SPEW, "sata_bar4=%x\n", sata_bar4); /* 3000 */
123         printk(BIOS_SPEW, "sata_bar5=%x\n", sata_bar5); /* e0309000 */
124
125         /* disable combined mode */
126         byte = pci_read_config8(sm_dev, 0xAD);
127         byte &= ~(1 << 3);
128         pci_write_config8(sm_dev, 0xAD, byte);
129         /* Program the 2C to 0x43801002 */
130         dword = 0x43801002;
131         pci_write_config32(dev, 0x2c, dword);
132
133         /* SERR-Enable */
134         word = pci_read_config16(dev, 0x04);
135         word |= (1 << 8);
136         pci_write_config16(dev, 0x04, word);
137
138         /* Dynamic power saving */
139         byte = pci_read_config8(dev, 0x40);
140         byte |= (1 << 2);
141         pci_write_config8(dev, 0x40, byte);
142
143         /* Set SATA Operation Mode, Set to IDE mode */
144         byte = pci_read_config8(dev, 0x40);
145         byte |= (1 << 0);
146         byte |= (1 << 4);
147         pci_write_config8(dev, 0x40, byte);
148
149         dword = 0x01018f00;
150         pci_write_config32(dev, 0x8, dword);
151
152         byte = pci_read_config8(dev, 0x40);
153         byte &= ~(1 << 0);
154         pci_write_config8(dev, 0x40, byte);
155
156         /* Enable the SATA watchdog counter */
157         byte = pci_read_config8(dev, 0x44);
158         byte |= (1 << 0);
159         pci_write_config8(dev, 0x44, byte);
160
161         /* Set bit 29 and 24 for A12 */
162         dword = pci_read_config32(dev, 0x40);
163         if (rev_id < 0x14)      /* before A12 */
164                 dword |= (1 << 29);
165         else
166                 dword &= ~(1 << 29); /* A14 and above */
167         pci_write_config32(dev, 0x40, dword);
168
169         /* set bit 21 for A12 */
170         dword = pci_read_config32(dev, 0x48);
171         if (rev_id < 0x14)      /* before A12 */
172                 dword |= 1 << 24 | 1 << 21;
173         else {
174                 dword &= ~(1 << 24 | 1 << 21); /* A14 and above */
175                 dword &= ~0xFF80; /* 15:7 */
176                 dword |= 1 << 15 | 0x7F << 7;
177         }
178         pci_write_config32(dev, 0x48, dword);
179
180         /* Program the watchdog counter to 0x10 */
181         byte = 0x10;
182         pci_write_config8(dev, 0x46, byte);
183         sb7xx_51xx_setup_sata_phys(dev);
184         /* Enable the I/O, MM, BusMaster access for SATA */
185         byte = pci_read_config8(dev, 0x4);
186         byte |= 7 << 0;
187         pci_write_config8(dev, 0x4, byte);
188
189 #if CONFIG_SOUTHBRIDGE_AMD_SUBTYPE_SP5100
190         /* Master Latency Timer */
191         pci_write_config32(dev, 0xC, 0x00004000);
192 #endif
193
194         /* RPR7.7 SATA drive detection. */
195         /* Use BAR5+0x128,BAR0 for Primary Slave */
196         /* Use BAR5+0x1A8,BAR0 for Primary Slave */
197         /* Use BAR5+0x228,BAR2 for Secondary Master */
198         /* Use BAR5+0x2A8,BAR2 for Secondary Slave */
199         /* Use BAR5+0x328,PATA_BAR0/2 for Primary/Secondary master emulation */
200         /* Use BAR5+0x3A8,PATA_BAR0/2 for Primary/Secondary Slave emulation */
201
202         /* TODO: port 4,5, which are PATA emulations. What are PATA_BARs? */
203
204         for (i = 0; i < 4; i++) {
205                 byte = read8(sata_bar5 + 0x128 + 0x80 * i);
206                 printk(BIOS_SPEW, "SATA port %i status = %x\n", i, byte);
207                 byte &= 0xF;
208                 if( byte == 0x1 ) {
209                         /* If the drive status is 0x1 then we see it but we aren't talking to it. */
210                         /* Try to do something about it. */
211                         printk(BIOS_SPEW, "SATA device detected but not talking. Trying lower speed.\n");
212
213                         /* Read in Port-N Serial ATA Control Register */
214                         byte = read8(sata_bar5 + 0x12C + 0x80 * i);
215
216                         /* Set Reset Bit and 1.5g bit */
217                         byte |= 0x11;
218                         write8((sata_bar5 + 0x12C + 0x80 * i), byte);
219
220                         /* Wait 1ms */
221                         mdelay(1);
222
223                         /* Clear Reset Bit */
224                         byte &= ~0x01;
225                         write8((sata_bar5 + 0x12C + 0x80 * i), byte);
226
227                         /* Wait 1ms */
228                         mdelay(1);
229
230                         /* Reread status */
231                         byte = read8(sata_bar5 + 0x128 + 0x80 * i);
232                         printk(BIOS_SPEW, "SATA port %i status = %x\n", i, byte);
233                         byte &= 0xF;
234                 }
235
236                 if (byte == 0x3) {
237                         for (j = 0; j < 10; j++) {
238                                 if (!sata_drive_detect(i, ((i / 2) == 0) ? sata_bar0 : sata_bar2))
239                                         break;
240                         }
241                         printk(BIOS_DEBUG, "%s %s device is %sready after %i tries\n",
242                                         (i / 2) ? "Secondary" : "Primary",
243                                         (i % 2 ) ? "Slave" : "Master",
244                                         (j == 10) ? "not " : "",
245                                         (j == 10) ? j : j + 1);
246                 } else {
247                         printk(BIOS_DEBUG, "No %s %s SATA drive on Slot%i\n",
248                                         (i / 2) ? "Secondary" : "Primary",
249                                         (i % 2 ) ? "Slave" : "Master", i);
250                 }
251         }
252
253         /* Below is CIM InitSataLateFar */
254         /* Enable interrupts from the HBA  */
255         byte = read8(sata_bar5 + 0x4);
256         byte |= 1 << 1;
257         write8((sata_bar5 + 0x4), byte);
258
259         /* Clear error status */
260         write32((sata_bar5 + 0x130), 0xFFFFFFFF);
261         write32((sata_bar5 + 0x1b0), 0xFFFFFFFF);
262         write32((sata_bar5 + 0x230), 0xFFFFFFFF);
263         write32((sata_bar5 + 0x2b0), 0xFFFFFFFF);
264         write32((sata_bar5 + 0x330), 0xFFFFFFFF);
265         write32((sata_bar5 + 0x3b0), 0xFFFFFFFF);
266
267         /* Clear SATA status,Firstly we get the AcpiGpe0BlkAddr */
268         /* ????? why CIM does not set the AcpiGpe0BlkAddr , but use it??? */
269
270         /* word = 0x0000; */
271         /* word = pm_ioread(0x28); */
272         /* byte = pm_ioread(0x29); */
273         /* word |= byte<<8; */
274         /* printk(BIOS_DEBUG, "AcpiGpe0Blk addr = %x\n", word); */
275         /* write32(word, 0x80000000); */
276 }
277
278 static struct pci_operations lops_pci = {
279         .set_subsystem = pci_dev_set_subsystem,
280 };
281
282 static struct device_operations sata_ops = {
283         .read_resources = pci_dev_read_resources,
284         .set_resources = pci_dev_set_resources,
285         .enable_resources = pci_dev_enable_resources,
286         .init = sata_init,
287         .scan_bus = 0,
288         .ops_pci = &lops_pci,
289 };
290
291 static const struct pci_driver sata0_driver __pci_driver = {
292         .ops = &sata_ops,
293         .vendor = PCI_VENDOR_ID_ATI,
294         .device = PCI_DEVICE_ID_ATI_SB700_SATA,
295 };