blob: cf3b14ef90c7ef98bc8448545fe56bacb94c61e4 [file] [log] [blame]
Stefan Reinauer8e073822012-04-04 00:07:22 +02001/*
2 * This file is part of the coreboot project.
3 *
4 * Copyright (C) 2008-2009 coresystems GmbH
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation; version 2 of
9 * 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
Paul Menzela46a7122013-02-23 18:37:27 +010018 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Stefan Reinauer8e073822012-04-04 00:07:22 +020019 */
20
21#include <arch/io.h>
22#include <console/console.h>
23#include <device/device.h>
24#include <device/pci.h>
25#include <device/pci_ids.h>
26#include "pch.h"
Vladimir Serbinenko6d6298d2014-01-11 07:46:50 +010027#include <pc80/mc146818rtc.h>
Stefan Reinauer8e073822012-04-04 00:07:22 +020028
29typedef struct southbridge_intel_bd82x6x_config config_t;
30
Stefan Reinauer16b022a2012-07-17 16:42:51 -070031static inline u32 sir_read(struct device *dev, int idx)
32{
33 pci_write_config32(dev, SATA_SIRI, idx);
34 return pci_read_config32(dev, SATA_SIRD);
35}
36
37static inline void sir_write(struct device *dev, int idx, u32 value)
38{
39 pci_write_config32(dev, SATA_SIRI, idx);
40 pci_write_config32(dev, SATA_SIRD, value);
41}
42
Stefan Reinauer8e073822012-04-04 00:07:22 +020043static void sata_init(struct device *dev)
44{
45 u32 reg32;
46 u16 reg16;
47 /* Get the chip configuration */
48 config_t *config = dev->chip_info;
Vladimir Serbinenko6d6298d2014-01-11 07:46:50 +010049 u8 sata_mode;
Stefan Reinauer8e073822012-04-04 00:07:22 +020050
Stefan Reinauer16b022a2012-07-17 16:42:51 -070051 printk(BIOS_DEBUG, "SATA: Initializing...\n");
Stefan Reinauer8e073822012-04-04 00:07:22 +020052
53 if (config == NULL) {
Stefan Reinauer16b022a2012-07-17 16:42:51 -070054 printk(BIOS_ERR, "SATA: ERROR: Device not in devicetree.cb!\n");
Stefan Reinauer8e073822012-04-04 00:07:22 +020055 return;
56 }
57
Vladimir Serbinenko6d6298d2014-01-11 07:46:50 +010058 if (get_option(&sata_mode, "sata_mode") != CB_SUCCESS)
59 /* Default to AHCI */
60 sata_mode = 0;
61
Stefan Reinauer8e073822012-04-04 00:07:22 +020062 /* SATA configuration */
63
64 /* Enable BARs */
65 pci_write_config16(dev, PCI_COMMAND, 0x0007);
66
Vladimir Serbinenko6d6298d2014-01-11 07:46:50 +010067 /* AHCI */
68 if (sata_mode == 0) {
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080069 u8 *abar;
Stefan Reinauer8e073822012-04-04 00:07:22 +020070
Stefan Reinauer16b022a2012-07-17 16:42:51 -070071 printk(BIOS_DEBUG, "SATA: Controller in AHCI mode.\n");
Stefan Reinauer8e073822012-04-04 00:07:22 +020072
73 /* Set Interrupt Line */
74 /* Interrupt Pin is set by D31IP.PIP */
75 pci_write_config8(dev, INTR_LN, 0x0a);
76
77 /* Set timings */
78 pci_write_config16(dev, IDE_TIM_PRI, IDE_DECODE_ENABLE |
79 IDE_ISP_3_CLOCKS | IDE_RCT_1_CLOCKS |
80 IDE_PPE0 | IDE_IE0 | IDE_TIME0);
81 pci_write_config16(dev, IDE_TIM_SEC, IDE_DECODE_ENABLE |
82 IDE_ISP_5_CLOCKS | IDE_RCT_4_CLOCKS);
83
84 /* Sync DMA */
85 pci_write_config16(dev, IDE_SDMA_CNT, IDE_PSDE0);
86 pci_write_config16(dev, IDE_SDMA_TIM, 0x0001);
87
88 /* Set IDE I/O Configuration */
89 reg32 = SIG_MODE_PRI_NORMAL | FAST_PCB1 | FAST_PCB0 | PCB1 | PCB0;
90 pci_write_config32(dev, IDE_CONFIG, reg32);
91
92 /* for AHCI, Port Enable is managed in memory mapped space */
93 reg16 = pci_read_config16(dev, 0x92);
94 reg16 &= ~0x3f; /* 6 ports SKU + ORM */
95 reg16 |= 0x8000 | config->sata_port_map;
96 pci_write_config16(dev, 0x92, reg16);
97
98 /* SATA Initialization register */
99 pci_write_config32(dev, 0x94,
100 ((config->sata_port_map ^ 0x3f) << 24) | 0x183);
101
102 /* Initialize AHCI memory-mapped space */
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800103 abar = (u8 *)pci_read_config32(dev, PCI_BASE_ADDRESS_5);
104 printk(BIOS_DEBUG, "ABAR: %p\n", abar);
Stefan Reinauer8e073822012-04-04 00:07:22 +0200105 /* CAP (HBA Capabilities) : enable power management */
106 reg32 = read32(abar + 0x00);
107 reg32 |= 0x0c006000; // set PSC+SSC+SALP+SSS
108 reg32 &= ~0x00020060; // clear SXS+EMS+PMS
Shawn Nematbakhshc9fc0292013-03-14 10:44:13 -0700109 /* Set ISS, if available */
110 if (config->sata_interface_speed_support)
111 {
112 reg32 &= ~0x00f00000;
113 reg32 |= (config->sata_interface_speed_support & 0x03)
114 << 20;
115 }
Stefan Reinauer8e073822012-04-04 00:07:22 +0200116 write32(abar + 0x00, reg32);
117 /* PI (Ports implemented) */
118 write32(abar + 0x0c, config->sata_port_map);
119 (void) read32(abar + 0x0c); /* Read back 1 */
120 (void) read32(abar + 0x0c); /* Read back 2 */
121 /* CAP2 (HBA Capabilities Extended)*/
122 reg32 = read32(abar + 0x24);
123 reg32 &= ~0x00000002;
124 write32(abar + 0x24, reg32);
125 /* VSP (Vendor Specific Register */
126 reg32 = read32(abar + 0xa0);
127 reg32 &= ~0x00000005;
128 write32(abar + 0xa0, reg32);
129 } else {
Vladimir Serbinenko6d6298d2014-01-11 07:46:50 +0100130 /* IDE */
Stefan Reinauer16b022a2012-07-17 16:42:51 -0700131 printk(BIOS_DEBUG, "SATA: Controller in plain mode.\n");
Stefan Reinauer8e073822012-04-04 00:07:22 +0200132
133 /* No AHCI: clear AHCI base */
134 pci_write_config32(dev, 0x24, 0x00000000);
135
136 /* And without AHCI BAR no memory decoding */
137 reg16 = pci_read_config16(dev, PCI_COMMAND);
138 reg16 &= ~PCI_COMMAND_MEMORY;
139 pci_write_config16(dev, PCI_COMMAND, reg16);
140
141 /* Native mode capable on both primary and secondary (0xa)
142 * or'ed with enabled (0x50) = 0xf
143 */
144 pci_write_config8(dev, 0x09, 0x8f);
145
146 /* Set Interrupt Line */
147 /* Interrupt Pin is set by D31IP.PIP */
148 pci_write_config8(dev, INTR_LN, 0xff);
149
150 /* Set timings */
151 pci_write_config16(dev, IDE_TIM_PRI, IDE_DECODE_ENABLE |
152 IDE_ISP_3_CLOCKS | IDE_RCT_1_CLOCKS |
153 IDE_PPE0 | IDE_IE0 | IDE_TIME0);
154 pci_write_config16(dev, IDE_TIM_SEC, IDE_DECODE_ENABLE |
155 IDE_SITRE | IDE_ISP_3_CLOCKS |
156 IDE_RCT_1_CLOCKS | IDE_IE0 | IDE_TIME0);
157
158 /* Sync DMA */
159 pci_write_config16(dev, IDE_SDMA_CNT, IDE_SSDE0 | IDE_PSDE0);
160 pci_write_config16(dev, IDE_SDMA_TIM, 0x0201);
161
162 /* Set IDE I/O Configuration */
163 reg32 = SIG_MODE_PRI_NORMAL | FAST_PCB1 | FAST_PCB0 | PCB1 | PCB0;
164 pci_write_config32(dev, IDE_CONFIG, reg32);
165
166 /* Port enable */
167 reg16 = pci_read_config16(dev, 0x92);
168 reg16 &= ~0x3f;
169 reg16 |= config->sata_port_map;
170 pci_write_config16(dev, 0x92, reg16);
171
172 /* SATA Initialization register */
173 pci_write_config32(dev, 0x94,
174 ((config->sata_port_map ^ 0x3f) << 24) | 0x183);
175 }
Duncan Lauriecfb64bd2012-07-16 16:16:31 -0700176
177 /* Set Gen3 Transmitter settings if needed */
178 if (config->sata_port0_gen3_tx)
179 pch_iobp_update(SATA_IOBP_SP0G3IR, 0,
180 config->sata_port0_gen3_tx);
181
182 if (config->sata_port1_gen3_tx)
183 pch_iobp_update(SATA_IOBP_SP1G3IR, 0,
184 config->sata_port1_gen3_tx);
Stefan Reinauer16b022a2012-07-17 16:42:51 -0700185
186 /* Additional Programming Requirements */
187 sir_write(dev, 0x04, 0x00001600);
188 sir_write(dev, 0x28, 0xa0000033);
189 reg32 = sir_read(dev, 0x54);
190 reg32 &= 0xff000000;
191 reg32 |= 0x5555aa;
192 sir_write(dev, 0x54, reg32);
193 sir_write(dev, 0x64, 0xcccc8484);
194 reg32 = sir_read(dev, 0x68);
195 reg32 &= 0xffff0000;
196 reg32 |= 0xcccc;
197 sir_write(dev, 0x68, reg32);
198 reg32 = sir_read(dev, 0x78);
199 reg32 &= 0x0000ffff;
200 reg32 |= 0x88880000;
201 sir_write(dev, 0x78, reg32);
202 sir_write(dev, 0x84, 0x001c7000);
203 sir_write(dev, 0x88, 0x88338822);
204 sir_write(dev, 0xa0, 0x001c7000);
205 // a4
206 sir_write(dev, 0xc4, 0x0c0c0c0c);
207 sir_write(dev, 0xc8, 0x0c0c0c0c);
208 sir_write(dev, 0xd4, 0x10000000);
209
210 pch_iobp_update(0xea004001, 0x3fffffff, 0xc0000000);
211 pch_iobp_update(0xea00408a, 0xfffffcff, 0x00000100);
Stefan Reinauer8e073822012-04-04 00:07:22 +0200212}
213
Stefan Reinauer816d0812012-04-30 16:42:07 -0700214static void sata_enable(device_t dev)
215{
216 /* Get the chip configuration */
217 config_t *config = dev->chip_info;
218 u16 map = 0;
Vladimir Serbinenko6d6298d2014-01-11 07:46:50 +0100219 u8 sata_mode;
Stefan Reinauer816d0812012-04-30 16:42:07 -0700220
221 if (!config)
222 return;
223
Vladimir Serbinenko6d6298d2014-01-11 07:46:50 +0100224 if (get_option(&sata_mode, "sata_mode") != CB_SUCCESS)
225 sata_mode = 0;
226
Stefan Reinauer816d0812012-04-30 16:42:07 -0700227 /*
228 * Set SATA controller mode early so the resource allocator can
229 * properly assign IO/Memory resources for the controller.
230 */
Vladimir Serbinenko6d6298d2014-01-11 07:46:50 +0100231 if (sata_mode == 0)
Stefan Reinauer816d0812012-04-30 16:42:07 -0700232 map = 0x0060;
233
234 map |= (config->sata_port_map ^ 0x3f) << 8;
235
236 pci_write_config16(dev, 0x90, map);
237}
238
Stefan Reinauer8e073822012-04-04 00:07:22 +0200239static void sata_set_subsystem(device_t dev, unsigned vendor, unsigned device)
240{
241 if (!vendor || !device) {
242 pci_write_config32(dev, PCI_SUBSYSTEM_VENDOR_ID,
243 pci_read_config32(dev, PCI_VENDOR_ID));
244 } else {
245 pci_write_config32(dev, PCI_SUBSYSTEM_VENDOR_ID,
246 ((device & 0xffff) << 16) | (vendor & 0xffff));
247 }
248}
249
250static struct pci_operations sata_pci_ops = {
251 .set_subsystem = sata_set_subsystem,
252};
253
254static struct device_operations sata_ops = {
255 .read_resources = pci_dev_read_resources,
256 .set_resources = pci_dev_set_resources,
257 .enable_resources = pci_dev_enable_resources,
258 .init = sata_init,
Stefan Reinauer816d0812012-04-30 16:42:07 -0700259 .enable = sata_enable,
Stefan Reinauer8e073822012-04-04 00:07:22 +0200260 .scan_bus = 0,
261 .ops_pci = &sata_pci_ops,
262};
263
Stefan Reinauer9a380ab2012-06-22 13:16:11 -0700264static const unsigned short pci_device_ids[] = { 0x1c00, 0x1c01, 0x1c02, 0x1c03,
265 0x1e00, 0x1e01, 0x1e02, 0x1e03,
266 0 };
267
268static const struct pci_driver pch_sata __pci_driver = {
269 .ops = &sata_ops,
270 .vendor = PCI_VENDOR_ID_INTEL,
271 .devices = pci_device_ids,
Stefan Reinauer8e073822012-04-04 00:07:22 +0200272};