blob: 0e7fd7d8e0047b1070383ffab39430e3b025ff89 [file] [log] [blame]
Nico Huber1f6bd942012-08-30 15:36:57 +02001/*
2 * This file is part of the libpayload project.
3 *
4 * Copyright (C) 2012 secunet Security Networks AG
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30//#define DEBUG_STATUS
31
32#include <stdlib.h>
33#include <stdint.h>
34#include <string.h>
35#include <libpayload.h>
36#include <pci.h>
37#include <storage/ata.h>
38#include <storage/ahci.h>
39
40#include "ahci_private.h"
41
42
43#ifdef DEBUG_STATUS
44static inline u32 _ahci_clear_status(volatile u32 *const reg,
45 const char *const r,
46 const char *const f)
47{
48 const u32 bits = *reg;
49 if (bits)
50 *reg = bits;
51 printf("ahci: %s: %s == 0x%08x\n", f, r, bits);
52
53 return bits;
54}
55#define ahci_clear_status(p, r) _ahci_clear_status(&(p)->r, #r, __func__)
56#else
57static inline u32 _ahci_clear_status(volatile u32 *const reg)
58{
59 const u32 bits = *reg;
60 if (bits)
61 *reg = bits;
62 return bits;
63}
64#define ahci_clear_status(p, r) _ahci_clear_status(&(p)->r)
65#endif
66
67
68static inline int ahci_port_is_active(const hba_port_t *const port)
69{
70 return (port->sata_status & (HBA_PxSSTS_IPM_MASK | HBA_PxSSTS_DET_MASK))
71 == (HBA_PxSSTS_IPM_ACTIVE | HBA_PxSSTS_DET_ESTABLISHED);
72}
73
Nico Huber1f6bd942012-08-30 15:36:57 +020074/** Do minimal error recovery. */
Edward O'Callaghanefc58412014-01-23 08:30:42 +110075int ahci_error_recovery(ahci_dev_t *const dev, const u32 intr_status)
Nico Huber1f6bd942012-08-30 15:36:57 +020076{
77 /* Command engine has to be restarted.
78 We don't call ahci_cmdengine_stop() here as it also checks
79 HBA_PxCMD_FR which won't clear on fatal errors. */
80 dev->port->cmd_stat &= ~HBA_PxCMD_ST;
81
82 /* Always clear sata_error. */
83 ahci_clear_status(dev->port, sata_error);
84
85 /* Perform COMRESET if appropriate. */
86 const u32 tfd = dev->port->taskfile_data;
87 if ((tfd & (HBA_PxTFD_BSY | HBA_PxTFD_DRQ)) |
88 (intr_status & HBA_PxIS_PCS)) {
89 const u32 sctl = dev->port->sata_control & ~HBA_PxSCTL_DET_MASK;
90 dev->port->sata_control = sctl | HBA_PxSCTL_DET_COMRESET;
91 mdelay(1);
92 dev->port->sata_control = sctl;
93 }
94
95 if (ahci_port_is_active(dev->port))
96 /* Start command engine. */
97 return ahci_cmdengine_start(dev->port);
98 else
99 return -1;
100}
101
Nico Huber1f6bd942012-08-30 15:36:57 +0200102static int ahci_dev_init(hba_ctrl_t *const ctrl,
103 hba_port_t *const port,
104 const int portnum)
105{
106 int ret = 1;
107
108 const int ncs = HBA_CAPS_DECODE_NCS(ctrl->caps);
109
110 /* Allocate command list, one command table and received FIS. */
111 cmd_t *const cmdlist = memalign(1024, ncs * sizeof(cmd_t));
112 cmdtable_t *const cmdtable = memalign(128, sizeof(cmdtable_t));
113 rcvd_fis_t *const rcvd_fis = memalign(256, sizeof(rcvd_fis_t));
114 /* Allocate our device structure. */
115 ahci_dev_t *const dev = calloc(1, sizeof(ahci_dev_t));
116 if (!cmdlist || !cmdtable || !rcvd_fis || !dev)
117 goto _cleanup_ret;
118 memset((void *)cmdlist, '\0', ncs * sizeof(cmd_t));
119 memset((void *)cmdtable, '\0', sizeof(*cmdtable));
120 memset((void *)rcvd_fis, '\0', sizeof(*rcvd_fis));
121
122 /* Set command list base and received FIS base. */
123 if (ahci_cmdengine_stop(port))
124 return 1;
125 port->cmdlist_base = virt_to_phys(cmdlist);
126 port->frameinfo_base = virt_to_phys(rcvd_fis);
127 if (ahci_cmdengine_start(port))
128 return 1;
129 /* Put port into active state. */
130 port->cmd_stat |= HBA_PxCMD_ICC_ACTIVE;
131
132 dev->ctrl = ctrl;
133 dev->port = port;
134 dev->cmdlist = cmdlist;
135 dev->cmdtable = cmdtable;
136 dev->rcvd_fis = rcvd_fis;
137
Nico Huber354066e2013-06-17 17:42:35 +0200138 /*
139 * Wait for D2H Register FIS with device' signature.
140 * The drive has to spin up here, so wait up to 30s.
141 */
142 const int timeout_s = 30; /* Time out after 30s. */
143 int timeout = timeout_s * 100;
Nico Huber1f6bd942012-08-30 15:36:57 +0200144 while ((port->taskfile_data & HBA_PxTFD_BSY) && timeout--)
145 mdelay(10);
146
Nico Huber354066e2013-06-17 17:42:35 +0200147 if (port->taskfile_data & HBA_PxTFD_BSY)
148 printf("ahci: Timed out after %d seconds "
149 "of waiting for device to spin up.\n", timeout_s);
150
Nico Huber1f6bd942012-08-30 15:36:57 +0200151 /* Initialize device or fall through to clean up. */
152 switch (port->signature) {
153 case HBA_PxSIG_ATA:
154 printf("ahci: ATA drive on port #%d.\n", portnum);
Stefan Reinauer1b4d3942015-06-29 15:47:34 -0700155#if IS_ENABLED(CONFIG_LP_STORAGE_ATA)
Nico Huber1f6bd942012-08-30 15:36:57 +0200156 dev->ata_dev.identify = ahci_identify_device;
157 dev->ata_dev.read_sectors = ahci_ata_read_sectors;
158 return ata_attach_device(&dev->ata_dev, PORT_TYPE_SATA);
159#endif
160 break;
161 case HBA_PxSIG_ATAPI:
162 printf("ahci: ATAPI drive on port #%d.\n", portnum);
Stefan Reinauer1b4d3942015-06-29 15:47:34 -0700163#if IS_ENABLED(CONFIG_LP_STORAGE_ATAPI)
Nico Huber1f6bd942012-08-30 15:36:57 +0200164 dev->atapi_dev.identify = ahci_identify_device;
165 dev->atapi_dev.packet_read_cmd = ahci_packet_read_cmd;
166 return atapi_attach_device(&dev->atapi_dev, PORT_TYPE_SATA);
167#endif
168 break;
169 default:
170 printf("ahci: Unsupported device (signature == 0x%08x) "
171 "on port #%d.\n", port->signature, portnum);
172 break;
173 }
174 ret = 2;
175
176_cleanup_ret:
177 /* Clean up (not reached for initialized devices). */
178 if (dev)
179 free(dev);
180 if (!ahci_cmdengine_stop(port)) {
181 port->cmdlist_base = 0;
182 port->frameinfo_base = 0;
183 if (rcvd_fis)
184 free((void *)rcvd_fis);
185 if (cmdtable)
186 free((void *)cmdtable);
187 if (cmdlist)
188 free((void *)cmdlist);
189 }
190 return ret;
191}
192
193static void ahci_port_probe(hba_ctrl_t *const ctrl,
194 hba_port_t *const port,
195 const int portnum)
196{
197 /* If staggered spin-up is supported, spin-up device. */
198 if (ctrl->caps & HBA_CAPS_SSS) {
199 port->cmd_stat |= HBA_PxCMD_SUD;
200 }
201
202 /* Wait 1s if we just told the device to spin up or
203 if it's the first port. */
204 if ((ctrl->caps & HBA_CAPS_SSS) ||
205 !(ctrl->ports_impl & ((1 << (portnum - 1)) - 1))) {
206 /* Wait for port to become active. */
207 int timeout = 100; /* Time out after 100 * 100us == 10ms. */
208 while (!ahci_port_is_active(port) && timeout--)
209 udelay(100);
210 }
211 if (!ahci_port_is_active(port))
212 return;
213
214 ahci_clear_status(port, sata_error);
215 ahci_clear_status(port, intr_status);
216
217 ahci_dev_init(ctrl, port, portnum);
218}
219
Stefan Reinauer1b4d3942015-06-29 15:47:34 -0700220#if IS_ENABLED(CONFIG_LP_STORAGE_AHCI_ONLY_TESTED)
Nico Huber1f6bd942012-08-30 15:36:57 +0200221static u32 working_controllers[] = {
Nico Huber5d1edf62013-05-21 12:26:47 +0200222 0x8086 | 0x2929 << 16, /* Mobile ICH9 */
Nico Huberd1e2edf2016-10-23 02:26:57 +0200223 0x8086 | 0x1c03 << 16, /* Mobile Cougar Point PCH */
Nico Huber5d1edf62013-05-21 12:26:47 +0200224 0x8086 | 0x1e03 << 16, /* Mobile Panther Point PCH */
Nico Huber1f6bd942012-08-30 15:36:57 +0200225};
226#endif
227static void ahci_init_pci(pcidev_t dev)
228{
229 int i;
230
231 const u16 class = pci_read_config16(dev, 0xa);
232 if (class != 0x0106)
233 return;
234 const u16 vendor = pci_read_config16(dev, 0x00);
235 const u16 device = pci_read_config16(dev, 0x02);
236
Stefan Reinauer1b4d3942015-06-29 15:47:34 -0700237#if IS_ENABLED(CONFIG_LP_STORAGE_AHCI_ONLY_TESTED)
Nico Huber1f6bd942012-08-30 15:36:57 +0200238 const u32 vendor_device = pci_read_config32(dev, 0x0);
239 for (i = 0; i < ARRAY_SIZE(working_controllers); ++i)
240 if (vendor_device == working_controllers[i])
241 break;
242 if (i == ARRAY_SIZE(working_controllers)) {
243 printf("ahci: Not using untested SATA controller "
244 "%02x:%02x.%02x (%04x:%04x).\n", PCI_BUS(dev),
245 PCI_SLOT(dev), PCI_FUNC(dev), vendor, device);
246 return;
247 }
248#endif
249
250 printf("ahci: Found SATA controller %02x:%02x.%02x (%04x:%04x).\n",
251 PCI_BUS(dev), PCI_SLOT(dev), PCI_FUNC(dev), vendor, device);
252
253 hba_ctrl_t *const ctrl = phys_to_virt(
254 pci_read_config32(dev, 0x24) & ~0x3ff);
255 hba_port_t *const ports = ctrl->ports;
256
257 /* Reset host controller. */
258 ctrl->global_ctrl |= HBA_CTRL_RESET;
259 /* Reset has to be finished after 1s. */
260 delay(1);
261 if (ctrl->global_ctrl & HBA_CTRL_RESET) {
262 printf("ahci: ERROR: "
263 "Controller reset didn't finish within 1s.\n");
264 return;
265 }
266
267 /* Set AHCI access mode. */
268 ctrl->global_ctrl |= HBA_CTRL_AHCI_EN;
269
270 /* Probe for devices. */
271 for (i = 0; i < 32; ++i) {
272 if (ctrl->ports_impl & (1 << i))
273 ahci_port_probe(ctrl, &ports[i], i + 1);
274 }
275}
276
277void ahci_initialize(void)
278{
279 int bus, dev, func;
280
281 for (bus = 0; bus < 256; ++bus) {
282 for (dev = 0; dev < 32; ++dev) {
283 const u16 class =
284 pci_read_config16(PCI_DEV(bus, dev, 0), 0xa);
285 if (class != 0xffff) {
286 for (func = 0; func < 8; ++func)
287 ahci_init_pci(PCI_DEV(bus, dev, func));
288 }
289 }
290 }
291}