mb/google/nissa/var/rull: Add 6W and 15W DPTF parameters
[coreboot2.git] / payloads / libpayload / drivers / storage / atapi.c
blob7f46e9cc3e8527db869079f89b2b89311b32d7d7
1 /*
3 * Copyright (C) 2012 secunet Security Networks AG
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
29 #include <stdint.h>
30 #include <stdio.h>
31 #include <libpayload.h>
33 #include <storage/ata.h>
34 #include <storage/atapi.h>
36 static int atapi_request_sense(atapi_dev_t *const dev)
38 u8 cdb[12] = { 0, };
39 cdb[0] = ATAPI_REQUEST_SENSE;
40 cdb[4] = sizeof(dev->sense_data);
41 return dev->packet_read_cmd(dev, cdb, sizeof(cdb),
42 dev->sense_data, sizeof(dev->sense_data));
45 static ssize_t atapi_packet_read_cmd(atapi_dev_t *const dev,
46 const u8 *const cmd, const size_t cmdlen,
47 u8 *const buf, const size_t buflen)
49 int retries = 10;
50 ssize_t ret;
52 do {
53 ret = dev->packet_read_cmd(dev, cmd, cmdlen, buf, buflen);
54 if (ret >= 0)
55 return ret;
57 ret = atapi_request_sense(dev);
58 if (ret < 0) {
59 printf("atapi: Requesting sense failed.\n");
60 return -1;
63 switch (dev->sense_data[2] & 0xf) {
64 case ATAPI_SENSE_UNIT_ATTENTION:
65 /* Nothing to do. */
66 break;
67 case ATAPI_SENSE_NOT_READY:
68 switch (dev->sense_data[12]) {
69 case ATAPI_ADDITIONAL_SENSE_LOGICAL_UNIT_NOT_READY:
70 /* Device wants more time. */
71 delay(3);
72 break;
73 case ATAPI_ADDITIONAL_SENSE_MEDIUM_NOT_PRESENT:
74 printf("atapi: No medium present.\n");
75 dev->medium_present = 0;
76 return -1;
78 break;
79 default:
80 return -1;
82 } while (retries--);
84 /* No more retries. */
85 return -1;
88 static ssize_t atapi_packet_read_sectors(ata_dev_t *const _dev,
89 const lba_t start, size_t count,
90 u8 *const buf)
92 atapi_dev_t *const dev = (atapi_dev_t *)_dev;
94 if (start >= (1ULL << 32)) {
95 printf("atapi: Sector is not 32-bit addressable.\n");
96 return -1;
97 } else if (count >= (64 * 1024)) {
98 printf("ahci: Sector count too high (max. 65535).\n");
99 count = (64 * 1024) - 1;
102 u8 cdb[12] = { 0, };
103 cdb[0] = ATAPI_READ_10;
104 cdb[2] = (start >> 24) & 0xff;
105 cdb[3] = (start >> 16) & 0xff;
106 cdb[4] = (start >> 8) & 0xff;
107 cdb[5] = (start >> 0) & 0xff;
108 cdb[7] = (count >> 8) & 0xff;
109 cdb[8] = (count >> 0) & 0xff;
110 const ssize_t ret = atapi_packet_read_cmd(dev, cdb, sizeof(cdb),
111 buf, count << dev->ata_dev.sector_size_shift);
112 if (ret < 0)
113 return ret;
114 else
115 return ret >> dev->ata_dev.sector_size_shift;
118 static storage_poll_t atapi_poll(storage_dev_t *const _dev)
120 atapi_dev_t *const dev = (atapi_dev_t *)_dev;
122 u8 cdb[12] = { 0, };
123 u8 capacity[8];
125 if (dev->medium_present)
126 return POLL_MEDIUM_PRESENT;
128 cdb[0] = ATAPI_TEST_UNIT_READY;
129 const int ret = atapi_packet_read_cmd(dev, cdb, sizeof(cdb), NULL, 0);
130 if (!ret) {
131 printf("atapi: Found medium.\n");
132 dev->medium_present = 1;
133 } else if (dev->medium_present) {
134 return POLL_ERROR;
137 if (dev->medium_present) {
138 cdb[0] = ATAPI_START_STOP_UNIT;
139 cdb[4] = 0x01; /* Start Disc, read TOC. */
140 if (atapi_packet_read_cmd(dev, cdb, sizeof(cdb), NULL, 0))
141 goto _error_ret;
142 cdb[4] = 0x00;
145 cdb[0] = ATAPI_PREVENT_ALLOW_MEDIUM_REMOVAL;
146 cdb[2] = 0x02; /* Clear persistent prevent removal bit. */
147 atapi_packet_read_cmd(dev, cdb, sizeof(cdb), NULL, 0);
148 cdb[2] = 0x00; /* Clear prevent removal bit. */
149 atapi_packet_read_cmd(dev, cdb, sizeof(cdb), NULL, 0);
151 /* If we don't have a medium, we're done. */
152 if (!dev->medium_present)
153 return POLL_NO_MEDIUM;
155 /* Read capacity and sector size. */
156 cdb[0] = ATAPI_READ_CAPACITY;
157 if (atapi_packet_read_cmd(dev, cdb, sizeof(cdb),
158 (u8 *)capacity, sizeof(capacity))
159 != sizeof(capacity))
160 goto _error_ret;
161 const u32 sector_size = (capacity[4] << 24) |
162 (capacity[5] << 16) |
163 (capacity[6] << 8) |
164 (capacity[7] << 0);
165 if (ata_set_sector_size(&dev->ata_dev, sector_size)) {
166 dev->medium_present = 0;
167 return POLL_MEDIUM_ERROR;
170 return POLL_MEDIUM_PRESENT;
172 _error_ret:
173 dev->medium_present = 0;
174 return POLL_ERROR;
177 int atapi_attach_device(atapi_dev_t *const dev, const storage_port_t port_type)
179 u16 id[256];
181 dev->ata_dev.identify_cmd = ATA_IDENTIFY_PACKET_DEVICE;
182 if (dev->identify(&dev->ata_dev, (u8 *)id))
183 return -1;
185 char fw[9], model[41];
186 ata_strncpy(fw, id + 23, sizeof(fw));
187 ata_strncpy(model, id + 27, sizeof(model));
188 printf("atapi: Identified %s [%s]\n", model, fw);
190 /* Initialize to sane values. */
191 dev->ata_dev.sector_size = 2048;
192 dev->ata_dev.sector_size_shift = 11;
194 dev->medium_present = 0;
196 /* Reuse ata procedures. */
197 ata_initialize_storage_ops(&dev->ata_dev);
199 dev->ata_dev.read_sectors = atapi_packet_read_sectors;
200 dev->ata_dev.storage_dev.port_type = port_type;
201 dev->ata_dev.storage_dev.poll = atapi_poll;
203 return storage_attach_device(&dev->ata_dev.storage_dev);