blob: 3b9c1d81f75ed27b91e8face555623ad5d9998a8 [file] [log] [blame]
Duncan Lauriec88c54c2014-04-30 16:36:13 -07001/*
2 * This file is part of the coreboot project.
3 *
4 * Copyright (C) 2008-2009 coresystems GmbH
5 * Copyright (C) 2014 Google Inc.
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 <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 <delay.h>
27#include <broadwell/iobp.h>
28#include <broadwell/ramstage.h>
29#include <broadwell/rcba.h>
30#include <broadwell/sata.h>
31#include <chip.h>
32
33static inline u32 sir_read(struct device *dev, int idx)
34{
35 pci_write_config32(dev, SATA_SIRI, idx);
36 return pci_read_config32(dev, SATA_SIRD);
37}
38
39static inline void sir_write(struct device *dev, int idx, u32 value)
40{
41 pci_write_config32(dev, SATA_SIRI, idx);
42 pci_write_config32(dev, SATA_SIRD, value);
43}
44
45static void sata_init(struct device *dev)
46{
47 config_t *config = dev->chip_info;
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080048 u32 reg32;
49 u8 *abar;
Duncan Lauriec88c54c2014-04-30 16:36:13 -070050 u16 reg16;
51
52 printk(BIOS_DEBUG, "SATA: Initializing controller in AHCI mode.\n");
53
54 /* Enable BARs */
55 pci_write_config16(dev, PCI_COMMAND, 0x0007);
56
57 /* Set Interrupt Line */
58 /* Interrupt Pin is set by D31IP.PIP */
59 pci_write_config8(dev, PCI_INTERRUPT_LINE, 0x0a);
60
61 /* Set timings */
62 pci_write_config16(dev, IDE_TIM_PRI, IDE_DECODE_ENABLE |
63 IDE_ISP_3_CLOCKS | IDE_RCT_1_CLOCKS |
64 IDE_PPE0 | IDE_IE0 | IDE_TIME0);
65 pci_write_config16(dev, IDE_TIM_SEC, IDE_DECODE_ENABLE |
66 IDE_ISP_5_CLOCKS | IDE_RCT_4_CLOCKS);
67
68 /* Sync DMA */
69 pci_write_config16(dev, IDE_SDMA_CNT, IDE_PSDE0);
70 pci_write_config16(dev, IDE_SDMA_TIM, 0x0001);
71
72 /* Set IDE I/O Configuration */
73 reg32 = SIG_MODE_PRI_NORMAL | FAST_PCB1 | FAST_PCB0 | PCB1 | PCB0;
74 pci_write_config32(dev, IDE_CONFIG, reg32);
75
76 /* for AHCI, Port Enable is managed in memory mapped space */
77 reg16 = pci_read_config16(dev, 0x92);
78 reg16 &= ~0x3f;
79 reg16 |= 0x8000 | config->sata_port_map;
80 pci_write_config16(dev, 0x92, reg16);
81 udelay(2);
82
83 /* Setup register 98h */
84 reg32 = pci_read_config16(dev, 0x98);
85 reg32 |= 1 << 19; /* BWG step 6 */
86 reg32 |= 1 << 22; /* BWG step 5 */
87 reg32 &= ~(0x3f << 7);
88 reg32 |= 0x04 << 7; /* BWG step 7 */
89 reg32 |= 1 << 20; /* BWG step 8 */
90 reg32 &= ~(0x03 << 5);
91 reg32 |= 1 << 5; /* BWG step 9 */
92 reg32 |= 1 << 18; /* BWG step 10 */
93 reg32 |= 1 << 29; /* BWG step 11 */
94 reg32 &= ~((1 << 31) | (1 << 30));
95 reg32 |= 1 << 23;
96 reg32 |= 1 << 24; /* Disable listen mode (hotplug) */
97 pci_write_config32(dev, 0x98, reg32);
98
99 /* Setup register 9Ch */
100 reg16 = 0; /* Disable alternate ID */
101 reg16 = 1 << 5; /* BWG step 12 */
102 pci_write_config16(dev, 0x9c, reg16);
103
104 /* SATA Initialization register */
105 reg32 = 0x183;
106 reg32 |= (config->sata_port_map ^ 0x3f) << 24;
107 reg32 |= (config->sata_devslp_mux & 1) << 15;
108 pci_write_config32(dev, 0x94, reg32);
109
110 /* Initialize AHCI memory-mapped space */
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800111 abar = (u8 *)(pci_read_config32(dev, PCI_BASE_ADDRESS_5));
112 printk(BIOS_DEBUG, "ABAR: %p\n", abar);
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700113
114 /* CAP (HBA Capabilities) : enable power management */
115 reg32 = read32(abar + 0x00);
116 reg32 |= 0x0c006000; // set PSC+SSC+SALP+SSS
117 reg32 &= ~0x00020060; // clear SXS+EMS+PMS
118 reg32 |= (1 << 18); // SAM: SATA AHCI MODE ONLY
119 write32(abar + 0x00, reg32);
120
121 /* PI (Ports implemented) */
122 write32(abar + 0x0c, config->sata_port_map);
123 (void) read32(abar + 0x0c); /* Read back 1 */
124 (void) read32(abar + 0x0c); /* Read back 2 */
125
126 /* CAP2 (HBA Capabilities Extended)*/
127 reg32 = read32(abar + 0x24);
128
129 /*
130 * Static Power Gating for unused ports
131 */
132 reg32 = RCBA32(0x3a84);
133 /* Port 3 and 2 disabled */
134 if ((config->sata_port_map & ((1 << 3)|(1 << 2))) == 0)
135 reg32 |= (1 << 24) | (1 << 26);
136 /* Port 1 and 0 disabled */
137 if ((config->sata_port_map & ((1 << 1)|(1 << 0))) == 0)
138 reg32 |= (1 << 20) | (1 << 18);
139 RCBA32(0x3a84) = reg32;
140
141 /* Enable DEVSLP */
142 if (config->sata_devslp_disable)
143 reg32 &= ~(1 << 3);
144 else
145 reg32 |= (1 << 5)|(1 << 4)|(1 << 3)|(1 << 2);
146 write32(abar + 0x24, reg32);
147
148 /* Set Gen3 Transmitter settings if needed */
149 if (config->sata_port0_gen3_tx)
150 pch_iobp_update(SATA_IOBP_SP0G3IR, 0,
151 config->sata_port0_gen3_tx);
152
153 if (config->sata_port1_gen3_tx)
154 pch_iobp_update(SATA_IOBP_SP1G3IR, 0,
155 config->sata_port1_gen3_tx);
156
157 /* Set Gen3 DTLE DATA / EDGE registers if needed */
158 if (config->sata_port0_gen3_dtle) {
159 pch_iobp_update(SATA_IOBP_SP0DTLE_DATA,
160 ~(SATA_DTLE_MASK << SATA_DTLE_DATA_SHIFT),
161 (config->sata_port0_gen3_dtle & SATA_DTLE_MASK)
162 << SATA_DTLE_DATA_SHIFT);
163
164 pch_iobp_update(SATA_IOBP_SP0DTLE_EDGE,
165 ~(SATA_DTLE_MASK << SATA_DTLE_EDGE_SHIFT),
166 (config->sata_port0_gen3_dtle & SATA_DTLE_MASK)
167 << SATA_DTLE_EDGE_SHIFT);
168 }
169
170 if (config->sata_port1_gen3_dtle) {
171 pch_iobp_update(SATA_IOBP_SP1DTLE_DATA,
172 ~(SATA_DTLE_MASK << SATA_DTLE_DATA_SHIFT),
173 (config->sata_port1_gen3_dtle & SATA_DTLE_MASK)
174 << SATA_DTLE_DATA_SHIFT);
175
176 pch_iobp_update(SATA_IOBP_SP1DTLE_EDGE,
177 ~(SATA_DTLE_MASK << SATA_DTLE_EDGE_SHIFT),
178 (config->sata_port1_gen3_dtle & SATA_DTLE_MASK)
179 << SATA_DTLE_EDGE_SHIFT);
180 }
181
182 /*
183 * Additional Programming Requirements for Power Optimizer
184 */
185
186 /* Step 1 */
187 sir_write(dev, 0x64, 0x883c9003);
188
189 /* Step 2: SIR 68h[15:0] = 880Ah */
190 reg32 = sir_read(dev, 0x68);
191 reg32 &= 0xffff0000;
192 reg32 |= 0x880a;
193 sir_write(dev, 0x68, reg32);
194
195 /* Step 3: SIR 60h[3] = 1 */
196 reg32 = sir_read(dev, 0x60);
197 reg32 |= (1 << 3);
198 sir_write(dev, 0x60, reg32);
199
200 /* Step 4: SIR 60h[0] = 1 */
201 reg32 = sir_read(dev, 0x60);
202 reg32 |= (1 << 0);
203 sir_write(dev, 0x60, reg32);
204
205 /* Step 5: SIR 60h[1] = 1 */
206 reg32 = sir_read(dev, 0x60);
207 reg32 |= (1 << 1);
208 sir_write(dev, 0x60, reg32);
209
210 /* Clock Gating */
211 sir_write(dev, 0x70, 0x3f00bf1f);
212 sir_write(dev, 0x54, 0xcf000f0f);
213 sir_write(dev, 0x58, 0x00190000);
Duncan Laurie446fb8e2014-08-08 09:59:43 -0700214 RCBA32_AND_OR(0x333c, 0xffcfffff, 0x00c00000);
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700215
216 reg32 = pci_read_config32(dev, 0x300);
Duncan Laurie446fb8e2014-08-08 09:59:43 -0700217 reg32 |= (1 << 17) | (1 << 16) | (1 << 19);
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700218 reg32 |= (1 << 31) | (1 << 30) | (1 << 29);
219 pci_write_config32(dev, 0x300, reg32);
220}
221
222/*
223 * Set SATA controller mode early so the resource allocator can
224 * properly assign IO/Memory resources for the controller.
225 */
226static void sata_enable(device_t dev)
227{
228 /* Get the chip configuration */
229 config_t *config = dev->chip_info;
230 u16 map = 0x0060;
231
232 map |= (config->sata_port_map ^ 0x3f) << 8;
233
234 pci_write_config16(dev, 0x90, map);
235}
236
237static struct device_operations sata_ops = {
238 .read_resources = &pci_dev_read_resources,
239 .set_resources = &pci_dev_set_resources,
240 .enable_resources = &pci_dev_enable_resources,
241 .init = &sata_init,
242 .enable = &sata_enable,
243 .ops_pci = &broadwell_pci_ops,
244};
245
246static const unsigned short pci_device_ids[] = {
247 0x9c03, 0x9c05, 0x9c07, 0x9c0f, /* LynxPoint-LP */
248 0x9c83, 0x9c85, 0x282a, 0x9c87, 0x282a, 0x9c8f, /* WildcatPoint */
249 0
250};
251
252static const struct pci_driver pch_sata __pci_driver = {
253 .ops = &sata_ops,
254 .vendor = PCI_VENDOR_ID_INTEL,
255 .devices = pci_device_ids,
256};