blob: 5294207cd4cad4265b92fb200e3b3f3b32e12ebc [file] [log] [blame]
Shawn Nematbakhsh1dbd0e22013-10-28 16:15:02 -07001/*
2 * This file is part of the coreboot project.
3 *
4 * Copyright (C) 2013 Google 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 <arch/io.h>
Julius Werner18ea2d32014-10-07 16:42:17 -070021#include <soc/pci_devs.h>
22#include <soc/ramstage.h>
23#include <soc/sata.h>
Shawn Nematbakhsh1dbd0e22013-10-28 16:15:02 -070024#include <console/console.h>
25#include <delay.h>
26#include <device/device.h>
27#include <device/pci.h>
28#include <device/pci_ids.h>
29
30#include "chip.h"
31
32typedef struct soc_intel_baytrail_config config_t;
33
34static inline void sir_write(struct device *dev, int idx, u32 value)
35{
36 pci_write_config32(dev, SATA_SIRI, idx);
37 pci_write_config32(dev, SATA_SIRD, value);
38}
39
40static void sata_init(struct device *dev)
41{
42 config_t *config = dev->chip_info;
43 u32 reg32;
44 u16 reg16;
45 u8 reg8;
46
47 printk(BIOS_DEBUG, "SATA: Initializing...\n");
48
49 if (config == NULL) {
50 printk(BIOS_ERR, "SATA: ERROR: Device not in devicetree.cb!\n");
51 return;
52 }
53
54 if (!config->sata_ahci) {
55 /* Set legacy or native decoding mode */
56 if (config->ide_legacy_combined) {
57 reg8 = pci_read_config8(dev, 0x09);
58 reg8 &= ~0x5;
59 pci_write_config8(dev, 0x09, reg8);
60 } else {
61 reg8 = pci_read_config8(dev, 0x09);
62 reg8 |= 0x5;
63 pci_write_config8(dev, 0x09, reg8);
64 }
65
66 /* Set capabilities pointer */
67 pci_write_config8(dev, 0x34, 0x70);
68 reg16 = pci_read_config16(dev, 0x70);
69 reg16 &= ~0xFF00;
70 pci_write_config16(dev, 0x70, reg16);
71 }
72
73 /* Primary timing - decode enable */
74 reg16 = pci_read_config16(dev, 0x40);
75 reg16 |= 1 << 15;
76 pci_write_config16(dev, 0x40, reg16);
77
78 /* Secondary timing - decode enable */
79 reg16 = pci_read_config16(dev, 0x42);
80 reg16 |= 1 << 15;
81 pci_write_config16(dev, 0x42, reg16);
82
83 /* Port mapping enables */
84 reg16 = pci_read_config16(dev, 0x90);
85 reg16 |= (config->sata_port_map ^ 0x3) << 8;
86 pci_write_config16(dev, 0x90, reg16);
87
88 /* Port control enables */
89 reg16 = pci_read_config16(dev, 0x92);
90 reg16 &= ~0x003f;
91 reg16 |= config->sata_port_map;
92 pci_write_config16(dev, 0x92, reg16);
93
94 if (config->sata_ahci) {
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080095 u8 *abar = (u8 *)pci_read_config32(dev, PCI_BASE_ADDRESS_5);
Shawn Nematbakhsh1dbd0e22013-10-28 16:15:02 -070096
97 /* Enable CR memory space decoding */
98 reg16 = pci_read_config16(dev, 0x04);
99 reg16 |= 0x2;
100 pci_write_config16(dev, 0x04, reg16);
101
102 /* Set capability register */
103 reg32 = read32(abar + 0x00);
104 reg32 |= 0x0c046000; // set PSC+SSC+SALP+SSS+SAM
105 reg32 &= ~0x00f20060; // clear SXS+EMS+PMS+gen bits
106 reg32 |= (0x3 << 20); // Gen3 SATA
107 write32(abar + 0x00, reg32);
108
109 /* Ports enabled */
110 reg32 = read32(abar + 0x0c);
111 reg32 &= (u32)(~0x3f);
112 reg32 |= config->sata_port_map;
113 write32(abar + 0xc, reg32);
114 /* Two extra reads to latch */
115 read32(abar + 0x0c);
116 read32(abar + 0x0c);
117
118 /* Set cap2 - Support devslp */
119 reg32 = (1 << 5) | (1 << 4) | (1 << 3);
120 write32(abar + 0x24, reg32);
121
122 /* Set PxCMD registers */
123 reg32 = read32(abar + 0x118);
124 reg32 &= ~((1 << 27) | (1 << 26) | (1 << 22) | (1 << 21) |
125 (1 << 19) | (1 << 18) | (1 << 1));
126 reg32 |= 2;
127 write32(abar + 0x118, reg32);
128
129 reg32 = read32(abar + 0x198);
130 reg32 &= ~((1 << 27) | (1 << 26) | (1 << 22) | (1 << 21) |
131 (1 << 19) | (1 << 18) | (1 << 1));
132 reg32 |= 2;
133 write32(abar + 0x198, reg32);
134
135 /* Clear reset features */
136 write32(abar + 0xc8, 0);
137
138 /* Enable interrupts */
139 reg8 = read8(abar + 0x04);
140 reg8 |= 0x02;
141 write8(abar + 0x04, reg8);
142
143 } else {
144 /* TODO(shawnn): Configure IDE SATA speed regs */
145 }
146
147 /* 1.4 us delay after configuring port / enable bits */
148 udelay(2);
149
150 /* Enable clock for ports */
151 reg32 = pci_read_config32(dev, 0x94);
152 reg32 |= 0x3f << 24;
153 pci_write_config32(dev, 0x94, reg32);
154 reg32 &= (config->sata_port_map ^ 0x3) << 24;
155 pci_write_config32(dev, 0x94, reg32);
156
157 /* Lock SataGc register */
158 reg32 = (0x1 << 31) | (0x7 << 12);
159 pci_write_config32(dev, 0x98, reg32);
160}
161
162static void sata_enable(device_t dev)
163{
164 config_t *config = dev->chip_info;
165 u8 reg8;
166 u16 reg16;
167 u32 reg32;
168
169 southcluster_enable_dev(dev);
170 if (!config)
171 return;
172
173 /* Port mapping -- mask off SPD + SMS + SC bits, then re-set */
174 reg16 = pci_read_config16(dev, 0x90);
175 reg16 &= ~0x03e0;
176 reg16 |= (config->sata_port_map ^ 0x3) << 8;
177 if(config->sata_ahci)
178 reg16 |= 0x60;
179 pci_write_config16(dev, 0x90, reg16);
180
181 /* Set reg 0x94 before starting configuration */
182 reg32 = pci_read_config32(dev, 0x94);
183 reg32 &= (u32)(~0x1ff);
184 reg32 |= 0x183;
185 pci_write_config32(dev, 0x94, reg32);
186
187 /* Set ORM bit */
188 reg16 = pci_read_config16(dev, 0x92);
189 reg16 |= (1 << 15);
190 pci_write_config16(dev, 0x92, reg16);
191
192 /* R_PCH_SATA_TM2 - Undocumented in EDS, set according to ref. code */
193 reg32 = pci_read_config32(dev, 0x98);
194 reg32 &= (u32)~(0x1f80 | (1 << 6) | (1 << 5));
195 reg32 |= (1 << 29) | (1 << 25) | (1 << 23) | (1 << 22) |
196 (1 << 20) | (1 << 19) | (1 << 18) | (1 << 9) | (1 << 5);
197 pci_write_config32(dev, 0x98, reg32);
198
199 /* CMD reg - set bus master enable (BME) */
200 reg8 = pci_read_config8(dev, 0x04);
201 reg8 |= (1 << 2);
202 pci_write_config8(dev, 0x04, reg8);
203
204 /* "Test mode registers" */
205 sir_write(dev, 0x70, 0x00288301);
206 sir_write(dev, 0x54, 0x00000300);
207 sir_write(dev, 0x58, 0x50000000);
208 /* "OOB Detection Margin */
209 sir_write(dev, 0x6c, 0x130C0603);
210 /* "Gasket Control" */
211 sir_write(dev, 0xf4, 0);
212
213 /* PCS - Enable requested SATA ports */
214 reg8 = pci_read_config8(dev, 0x92);
215 reg8 &= ~0x03;
216 reg8 |= config->sata_port_map;
217 pci_write_config8(dev, 0x92, reg8);
218}
219
220static struct device_operations sata_ops = {
221 .read_resources = pci_dev_read_resources,
222 .set_resources = pci_dev_set_resources,
223 .enable_resources = pci_dev_enable_resources,
224 .init = sata_init,
225 .enable = sata_enable,
226 .scan_bus = NULL,
227 .ops_pci = &soc_pci_ops,
228};
229
230static const unsigned short pci_device_ids[] = {
231 IDE1_DEVID, IDE2_DEVID, /* IDE */
232 AHCI1_DEVID, AHCI2_DEVID, /* AHCI */
233 0,
234};
235
236static const struct pci_driver baytrail_sata __pci_driver = {
237 .ops = &sata_ops,
238 .vendor = PCI_VENDOR_ID_INTEL,
239 .devices = pci_device_ids,
240};