blob: a354ba7d89cf2f7604674c42cd35620a1137b7ce [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.
Duncan Lauriec88c54c2014-04-30 16:36:13 -070015 */
16
17#include <arch/io.h>
18#include <console/console.h>
19#include <device/device.h>
20#include <device/pci.h>
21#include <device/pci_ids.h>
22#include <delay.h>
Julius Werner4ee4bd52014-10-20 13:46:39 -070023#include <soc/iobp.h>
24#include <soc/ramstage.h>
25#include <soc/rcba.h>
26#include <soc/sata.h>
27#include <soc/intel/broadwell/chip.h>
Duncan Lauriec88c54c2014-04-30 16:36:13 -070028
29static inline u32 sir_read(struct device *dev, int idx)
30{
31 pci_write_config32(dev, SATA_SIRI, idx);
32 return pci_read_config32(dev, SATA_SIRD);
33}
34
35static inline void sir_write(struct device *dev, int idx, u32 value)
36{
37 pci_write_config32(dev, SATA_SIRI, idx);
38 pci_write_config32(dev, SATA_SIRD, value);
39}
40
41static void sata_init(struct device *dev)
42{
43 config_t *config = dev->chip_info;
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080044 u32 reg32;
45 u8 *abar;
Duncan Lauriec88c54c2014-04-30 16:36:13 -070046 u16 reg16;
Duncan Laurie1b0d5a32014-08-13 16:59:34 -070047 int port;
Duncan Lauriec88c54c2014-04-30 16:36:13 -070048
49 printk(BIOS_DEBUG, "SATA: Initializing controller in AHCI mode.\n");
50
51 /* Enable BARs */
52 pci_write_config16(dev, PCI_COMMAND, 0x0007);
53
54 /* Set Interrupt Line */
55 /* Interrupt Pin is set by D31IP.PIP */
56 pci_write_config8(dev, PCI_INTERRUPT_LINE, 0x0a);
57
58 /* Set timings */
Kane Chen8c1fd782014-08-19 10:51:46 -070059 pci_write_config16(dev, IDE_TIM_PRI, IDE_DECODE_ENABLE);
60 pci_write_config16(dev, IDE_TIM_SEC, IDE_DECODE_ENABLE);
Duncan Lauriec88c54c2014-04-30 16:36:13 -070061
62 /* for AHCI, Port Enable is managed in memory mapped space */
63 reg16 = pci_read_config16(dev, 0x92);
Wenkai Du038cce22014-12-05 14:04:10 -080064 reg16 &= ~0xf;
Duncan Lauriec88c54c2014-04-30 16:36:13 -070065 reg16 |= 0x8000 | config->sata_port_map;
66 pci_write_config16(dev, 0x92, reg16);
67 udelay(2);
68
69 /* Setup register 98h */
Kane Chen8c1fd782014-08-19 10:51:46 -070070 reg32 = pci_read_config32(dev, 0x98);
Duncan Lauriec88c54c2014-04-30 16:36:13 -070071 reg32 &= ~((1 << 31) | (1 << 30));
72 reg32 |= 1 << 23;
Duncan Laurie1b0d5a32014-08-13 16:59:34 -070073 reg32 |= 1 << 24; /* Enable MPHY Dynamic Power Gating */
Duncan Lauriec88c54c2014-04-30 16:36:13 -070074 pci_write_config32(dev, 0x98, reg32);
75
76 /* Setup register 9Ch */
77 reg16 = 0; /* Disable alternate ID */
78 reg16 = 1 << 5; /* BWG step 12 */
79 pci_write_config16(dev, 0x9c, reg16);
80
81 /* SATA Initialization register */
82 reg32 = 0x183;
Wenkai Du038cce22014-12-05 14:04:10 -080083 reg32 |= (config->sata_port_map ^ 0xf) << 24;
Duncan Lauriec88c54c2014-04-30 16:36:13 -070084 reg32 |= (config->sata_devslp_mux & 1) << 15;
85 pci_write_config32(dev, 0x94, reg32);
86
87 /* Initialize AHCI memory-mapped space */
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080088 abar = (u8 *)(pci_read_config32(dev, PCI_BASE_ADDRESS_5));
89 printk(BIOS_DEBUG, "ABAR: %p\n", abar);
Duncan Lauriec88c54c2014-04-30 16:36:13 -070090
Duncan Laurie55228ba2014-08-25 10:14:08 -070091 /* CAP (HBA Capabilities) : enable power management */
92 reg32 = read32(abar + 0x00);
93 reg32 |= 0x0c006000; /* set PSC+SSC+SALP+SSS */
94 reg32 &= ~0x00020060; /* clear SXS+EMS+PMS */
95 reg32 |= (1 << 18); /* SAM: SATA AHCI MODE ONLY */
96 write32(abar + 0x00, reg32);
97
Duncan Lauriec88c54c2014-04-30 16:36:13 -070098 /* PI (Ports implemented) */
99 write32(abar + 0x0c, config->sata_port_map);
100 (void) read32(abar + 0x0c); /* Read back 1 */
101 (void) read32(abar + 0x0c); /* Read back 2 */
102
103 /* CAP2 (HBA Capabilities Extended)*/
Duncan Laurie1b0d5a32014-08-13 16:59:34 -0700104 if (config->sata_devslp_disable) {
105 reg32 = read32(abar + 0x24);
106 reg32 &= ~(1 << 3);
107 write32(abar + 0x24, reg32);
108 } else {
109 /* Enable DEVSLP */
110 reg32 = read32(abar + 0x24);
111 reg32 |= (1 << 5)|(1 << 4)|(1 << 3)|(1 << 2);
112 write32(abar + 0x24, reg32);
113
114 for (port = 0; port < 4; port++) {
115 if (!(config->sata_port_map & (1 << port)))
116 continue;
117 reg32 = read32(abar + 0x144 + (0x80 * port));
118 reg32 |= (1 << 1); /* DEVSLP DSP */
119 write32(abar + 0x144 + (0x80 * port), reg32);
120 }
121 }
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700122
123 /*
124 * Static Power Gating for unused ports
125 */
126 reg32 = RCBA32(0x3a84);
127 /* Port 3 and 2 disabled */
128 if ((config->sata_port_map & ((1 << 3)|(1 << 2))) == 0)
129 reg32 |= (1 << 24) | (1 << 26);
130 /* Port 1 and 0 disabled */
131 if ((config->sata_port_map & ((1 << 1)|(1 << 0))) == 0)
132 reg32 |= (1 << 20) | (1 << 18);
133 RCBA32(0x3a84) = reg32;
134
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700135 /* Set Gen3 Transmitter settings if needed */
136 if (config->sata_port0_gen3_tx)
Duncan Laurieb63d3412015-01-06 13:32:42 -0800137 pch_iobp_update(SATA_IOBP_SP0_SECRT88,
138 ~(SATA_SECRT88_VADJ_MASK <<
139 SATA_SECRT88_VADJ_SHIFT),
140 (config->sata_port0_gen3_tx &
141 SATA_SECRT88_VADJ_MASK)
142 << SATA_SECRT88_VADJ_SHIFT);
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700143
144 if (config->sata_port1_gen3_tx)
Duncan Laurieb63d3412015-01-06 13:32:42 -0800145 pch_iobp_update(SATA_IOBP_SP1_SECRT88,
146 ~(SATA_SECRT88_VADJ_MASK <<
147 SATA_SECRT88_VADJ_SHIFT),
148 (config->sata_port1_gen3_tx &
149 SATA_SECRT88_VADJ_MASK)
150 << SATA_SECRT88_VADJ_SHIFT);
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700151
152 /* Set Gen3 DTLE DATA / EDGE registers if needed */
153 if (config->sata_port0_gen3_dtle) {
154 pch_iobp_update(SATA_IOBP_SP0DTLE_DATA,
155 ~(SATA_DTLE_MASK << SATA_DTLE_DATA_SHIFT),
156 (config->sata_port0_gen3_dtle & SATA_DTLE_MASK)
157 << SATA_DTLE_DATA_SHIFT);
158
159 pch_iobp_update(SATA_IOBP_SP0DTLE_EDGE,
160 ~(SATA_DTLE_MASK << SATA_DTLE_EDGE_SHIFT),
161 (config->sata_port0_gen3_dtle & SATA_DTLE_MASK)
162 << SATA_DTLE_EDGE_SHIFT);
163 }
164
165 if (config->sata_port1_gen3_dtle) {
166 pch_iobp_update(SATA_IOBP_SP1DTLE_DATA,
167 ~(SATA_DTLE_MASK << SATA_DTLE_DATA_SHIFT),
168 (config->sata_port1_gen3_dtle & SATA_DTLE_MASK)
169 << SATA_DTLE_DATA_SHIFT);
170
171 pch_iobp_update(SATA_IOBP_SP1DTLE_EDGE,
172 ~(SATA_DTLE_MASK << SATA_DTLE_EDGE_SHIFT),
173 (config->sata_port1_gen3_dtle & SATA_DTLE_MASK)
174 << SATA_DTLE_EDGE_SHIFT);
175 }
176
177 /*
178 * Additional Programming Requirements for Power Optimizer
179 */
180
181 /* Step 1 */
182 sir_write(dev, 0x64, 0x883c9003);
183
184 /* Step 2: SIR 68h[15:0] = 880Ah */
185 reg32 = sir_read(dev, 0x68);
186 reg32 &= 0xffff0000;
187 reg32 |= 0x880a;
188 sir_write(dev, 0x68, reg32);
189
190 /* Step 3: SIR 60h[3] = 1 */
191 reg32 = sir_read(dev, 0x60);
192 reg32 |= (1 << 3);
193 sir_write(dev, 0x60, reg32);
194
195 /* Step 4: SIR 60h[0] = 1 */
196 reg32 = sir_read(dev, 0x60);
197 reg32 |= (1 << 0);
198 sir_write(dev, 0x60, reg32);
199
200 /* Step 5: SIR 60h[1] = 1 */
201 reg32 = sir_read(dev, 0x60);
202 reg32 |= (1 << 1);
203 sir_write(dev, 0x60, reg32);
204
205 /* Clock Gating */
206 sir_write(dev, 0x70, 0x3f00bf1f);
207 sir_write(dev, 0x54, 0xcf000f0f);
208 sir_write(dev, 0x58, 0x00190000);
Duncan Laurie446fb8e2014-08-08 09:59:43 -0700209 RCBA32_AND_OR(0x333c, 0xffcfffff, 0x00c00000);
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700210
211 reg32 = pci_read_config32(dev, 0x300);
Duncan Laurie446fb8e2014-08-08 09:59:43 -0700212 reg32 |= (1 << 17) | (1 << 16) | (1 << 19);
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700213 reg32 |= (1 << 31) | (1 << 30) | (1 << 29);
214 pci_write_config32(dev, 0x300, reg32);
Kane Chen8c1fd782014-08-19 10:51:46 -0700215
Kane Chen46134722014-08-28 17:05:06 -0700216 reg32 = pci_read_config32(dev, 0x98);
217 reg32 |= 1 << 29;
218 pci_write_config32(dev, 0x98, reg32);
219
Kane Chen8c1fd782014-08-19 10:51:46 -0700220 /* Register Lock */
221 reg32 = pci_read_config32(dev, 0x9c);
222 reg32 |= (1 << 31);
223 pci_write_config32(dev, 0x9c, reg32);
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700224}
225
226/*
227 * Set SATA controller mode early so the resource allocator can
228 * properly assign IO/Memory resources for the controller.
229 */
230static void sata_enable(device_t dev)
231{
232 /* Get the chip configuration */
233 config_t *config = dev->chip_info;
234 u16 map = 0x0060;
235
Wenkai Du038cce22014-12-05 14:04:10 -0800236 map |= (config->sata_port_map ^ 0xf) << 8;
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700237
238 pci_write_config16(dev, 0x90, map);
239}
240
241static struct device_operations sata_ops = {
242 .read_resources = &pci_dev_read_resources,
243 .set_resources = &pci_dev_set_resources,
244 .enable_resources = &pci_dev_enable_resources,
245 .init = &sata_init,
246 .enable = &sata_enable,
247 .ops_pci = &broadwell_pci_ops,
248};
249
250static const unsigned short pci_device_ids[] = {
251 0x9c03, 0x9c05, 0x9c07, 0x9c0f, /* LynxPoint-LP */
252 0x9c83, 0x9c85, 0x282a, 0x9c87, 0x282a, 0x9c8f, /* WildcatPoint */
253 0
254};
255
256static const struct pci_driver pch_sata __pci_driver = {
257 .ops = &sata_ops,
258 .vendor = PCI_VENDOR_ID_INTEL,
259 .devices = pci_device_ids,
260};