remove trailing whitespace
[coreboot.git] / src / southbridge / amd / sb600 / sata.c
1 /*
2  * This file is part of the coreboot project.
3  *
4  * Copyright (C) 2008 Advanced Micro Devices, Inc.
5  * Copyright (C) 2008 Carl-Daniel Hailfinger
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; version 2 of the License.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
19  */
20
21 #include <console/console.h>
22 #include <device/device.h>
23 #include <delay.h>
24 #include <device/pci.h>
25 #include <device/pci_ids.h>
26 #include <device/pci_ops.h>
27 #include <arch/io.h>
28 #include "sb600.h"
29 #include <pc80/mc146818rtc.h>
30
31 #define SATA_MODE_IDE 1
32 #define SATA_MODE_AHCI 0
33
34 static int sata_drive_detect(int portnum, u16 iobar)
35 {
36         u8 byte, byte2;
37         int i = 0;
38         outb(0xA0 + 0x10 * (portnum % 2), iobar + 0x6);
39         while (byte = inb(iobar + 0x6), byte2 = inb(iobar + 0x7),
40                 (byte != (0xA0 + 0x10 * (portnum % 2))) ||
41                 ((byte2 & 0x88) != 0)) {
42                 printk(BIOS_SPEW, "0x6=%x, 0x7=%x\n", byte, byte2);
43                 if (byte != (0xA0 + 0x10 * (portnum % 2))) {
44                         /* This will happen at the first iteration of this loop
45                          * if the first SATA port is unpopulated and the
46                          * second SATA port is populated.
47                          */
48                         printk(BIOS_DEBUG, "drive no longer selected after %i ms, "
49                                 "retrying init\n", i * 10);
50                         return 1;
51                 } else
52                         printk(BIOS_SPEW, "drive detection not yet completed, "
53                                 "waiting...\n");
54                 mdelay(10);
55                 i++;
56         }
57         printk(BIOS_SPEW, "drive detection done after %i ms\n", i * 10);
58         return 0;
59 }
60
61 static void sata_init(struct device *dev)
62 {
63         u8 byte;
64         u16 word;
65         u32 dword;
66         u32 sata_bar5;
67         u16 sata_bar0, sata_bar1, sata_bar2, sata_bar3, sata_bar4;
68         int i, j;
69
70         struct southbridge_ati_sb600_config *conf;
71         conf = dev->chip_info;
72
73         device_t sm_dev;
74         /* SATA SMBus Disable */
75         /* sm_dev = pci_locate_device(PCI_ID(0x1002, 0x4385), 0); */
76         sm_dev = dev_find_slot(0, PCI_DEVFN(0x14, 0));
77         /* Disable SATA SMBUS */
78         byte = pci_read_config8(sm_dev, 0xad);
79         byte |= (1 << 1);
80         /* Enable SATA and power saving */
81         byte = pci_read_config8(sm_dev, 0xad);
82         byte |= (1 << 0);
83         byte |= (1 << 5);
84         pci_write_config8(sm_dev, 0xad, byte);
85         /* Set the interrupt Mapping to INTG# */
86         byte = pci_read_config8(sm_dev, 0xaf);
87         byte = 0x6 << 2;
88         pci_write_config8(sm_dev, 0xaf, byte);
89
90         /* get base address */
91         sata_bar5 = pci_read_config32(dev, 0x24) & ~0x3FF;
92         sata_bar0 = pci_read_config16(dev, 0x10) & ~0x7;
93         sata_bar1 = pci_read_config16(dev, 0x14) & ~0x3;
94         sata_bar2 = pci_read_config16(dev, 0x18) & ~0x7;
95         sata_bar3 = pci_read_config16(dev, 0x1C) & ~0x3;
96         sata_bar4 = pci_read_config16(dev, 0x20) & ~0xf;
97
98         printk(BIOS_SPEW, "sata_bar0=%x\n", sata_bar0); /* 3030 */
99         printk(BIOS_SPEW, "sata_bar1=%x\n", sata_bar1); /* 3070 */
100         printk(BIOS_SPEW, "sata_bar2=%x\n", sata_bar2); /* 3040 */
101         printk(BIOS_SPEW, "sata_bar3=%x\n", sata_bar3); /* 3080 */
102         printk(BIOS_SPEW, "sata_bar4=%x\n", sata_bar4); /* 3000 */
103         printk(BIOS_SPEW, "sata_bar5=%x\n", sata_bar5); /* e0309000 */
104
105         /* SERR-Enable */
106         word = pci_read_config16(dev, 0x04);
107         word |= (1 << 8);
108         pci_write_config16(dev, 0x04, word);
109
110         /* Dynamic power saving */
111         byte = pci_read_config8(dev, 0x40);
112         byte |= (1 << 2);
113         pci_write_config8(dev, 0x40, byte);
114
115         /* Set SATA Operation Mode */
116         byte = pci_read_config8(dev, 0x40);
117         byte |= (1 << 0);
118         byte |= (1 << 4);
119         pci_write_config8(dev, 0x40, byte);
120
121         // 1 means IDE, 0 means AHCI
122         if( get_option(&i, "sata_mode") < 0 ) {
123                 // no cmos option
124                 i = CONFIG_SATA_MODE;
125         }
126         printk(BIOS_INFO, "%s: setting sata mode = %s\n", __func__, (i == SATA_MODE_IDE)?"ide":"ahci" );
127
128         dword = pci_read_config32(dev, 0x8);
129         dword &= 0xff0000ff;
130         if (i == SATA_MODE_IDE)
131                 dword |= 0x00018f00; // IDE mode
132         else
133                 dword |= 0x00060100; // AHCI mode
134         pci_write_config32(dev, 0x8, dword);
135
136         byte = pci_read_config8(dev, 0x40);
137         byte &= ~(1 << 0);
138         pci_write_config8(dev, 0x40, byte);
139
140         /* Enable the SATA watchdog counter */
141         byte = pci_read_config8(dev, 0x44);
142         byte |= (1 << 0);
143         pci_write_config8(dev, 0x44, byte);
144
145         /* Program the watchdog counter to 0x10 */
146         byte = 0x10;
147         pci_write_config8(dev, 0x46, byte);
148
149         /* RPR6.5 Program the PHY Global Control to 0x2C00 for A13 */
150         word = 0x2c00;
151         pci_write_config16(dev, 0x86, word);
152
153         /* RPR6.5 Program the Phy Tuning4Ports */
154         dword = 0x00B401D6;
155         pci_write_config32(dev, 0x88, dword);
156         pci_write_config32(dev, 0x8c, dword);
157         pci_write_config32(dev, 0x90, dword);
158         pci_write_config32(dev, 0x94, dword);
159
160         byte = 0xB8;
161         pci_write_config8(dev, 0xA5, byte);
162         pci_write_config8(dev, 0xAD, byte);
163         pci_write_config8(dev, 0xB5, byte);
164         pci_write_config8(dev, 0xBD, byte);
165
166         /* RPR 6.8  */
167         word = pci_read_config16(dev, 0x42);
168         word |= 1 << 7;
169         pci_write_config16(dev, 0x42, word);
170         /* RPR 6.9  */
171         dword = pci_read_config32(dev, 0x40);
172         dword |= 1 << 25;
173         pci_write_config32(dev, 0x40, dword);
174
175         /* Enable the I/O, MM, BusMaster access for SATA */
176         byte = pci_read_config8(dev, 0x4);
177         byte |= 7 << 0;
178         pci_write_config8(dev, 0x4, byte);
179
180         /* RPR6.6 SATA drive detection. */
181         /* Use BAR5+0x128,BAR0 for Primary Slave */
182         /* Use BAR5+0x1A8,BAR0 for Primary Slave */
183         /* Use BAR5+0x228,BAR2 for Secondary Master */
184         /* Use BAR5+0x2A8,BAR2 for Secondary Slave */
185
186         for (i = 0; i < 4; i++) {
187                 byte = read8(sata_bar5 + 0x128 + 0x80 * i);
188                 printk(BIOS_SPEW, "SATA port %i status = %x\n", i, byte);
189                 byte &= 0xF;
190
191                 if( byte == 0x1 ) {
192                         /* If the drive status is 0x1 then we see it but we aren't talking to it. */
193                         /* Try to do something about it. */
194                         printk(BIOS_SPEW, "SATA device detected but not talking. Trying lower speed.\n");
195
196                         /* Read in Port-N Serial ATA Control Register */
197                         byte = read8(sata_bar5 + 0x12C + 0x80 * i);
198
199                         /* Set Reset Bit and 1.5g bit */
200                         byte |= 0x11;
201                         write8((sata_bar5 + 0x12C + 0x80 * i), byte);
202
203                         /* Wait 1ms */
204                         mdelay(1);
205
206                         /* Clear Reset Bit */
207                         byte &= ~0x01;
208                         write8((sata_bar5 + 0x12C + 0x80 * i), byte);
209
210                         /* Wait 1ms */
211                         mdelay(1);
212
213                         /* Reread status */
214                         byte = read8(sata_bar5 + 0x128 + 0x80 * i);
215                         printk(BIOS_SPEW, "SATA port %i status = %x\n", i, byte);
216                         byte &= 0xF;
217                 }
218
219                 if (byte == 0x3) {
220                         for (j = 0; j < 10; j++) {
221                                 if (!sata_drive_detect(i, ((i / 2) == 0) ? sata_bar0 : sata_bar2))
222                                         break;
223                         }
224                         printk(BIOS_DEBUG, "%s %s device is %sready after %i tries\n",
225                                         (i / 2) ? "Secondary" : "Primary",
226                                         (i % 2 ) ? "Slave" : "Master",
227                                         (j == 10) ? "not " : "",
228                                         (j == 10) ? j : j + 1);
229                 } else {
230                         printk(BIOS_DEBUG, "No %s %s SATA drive on Slot%i\n",
231                                         (i / 2) ? "Secondary" : "Primary",
232                                         (i % 2 ) ? "Slave" : "Master", i);
233                 }
234         }
235
236         /* Below is CIM InitSataLateFar */
237         /* Enable interrupts from the HBA  */
238         byte = read8(sata_bar5 + 0x4);
239         byte |= 1 << 1;
240         write8((sata_bar5 + 0x4), byte);
241
242         /* Clear error status */
243         write32((sata_bar5 + 0x130), 0xFFFFFFFF);
244         write32((sata_bar5 + 0x1b0), 0xFFFFFFFF);
245         write32((sata_bar5 + 0x230), 0xFFFFFFFF);
246         write32((sata_bar5 + 0x2b0), 0xFFFFFFFF);
247
248         /* Clear SATA status,Firstly we get the AcpiGpe0BlkAddr */
249         /* ????? why CIM does not set the AcpiGpe0BlkAddr , but use it??? */
250
251         /* word = 0x0000; */
252         /* word = pm_ioread(0x28); */
253         /* byte = pm_ioread(0x29); */
254         /* word |= byte<<8; */
255         /* printk(BIOS_DEBUG, "AcpiGpe0Blk addr = %x\n", word); */
256         /* write32(word, 0x80000000); */
257 }
258
259 static struct pci_operations lops_pci = {
260         .set_subsystem = pci_dev_set_subsystem,
261 };
262
263 static struct device_operations sata_ops = {
264         .read_resources = pci_dev_read_resources,
265         .set_resources = pci_dev_set_resources,
266         .enable_resources = pci_dev_enable_resources,
267         /* .enable           = sb600_enable, */
268         .init = sata_init,
269         .scan_bus = 0,
270         .ops_pci = &lops_pci,
271 };
272
273 static const struct pci_driver sata0_driver __pci_driver = {
274         .ops = &sata_ops,
275         .vendor = PCI_VENDOR_ID_ATI,
276         .device = PCI_DEVICE_ID_ATI_SB600_SATA,
277 };