blob: c698f9cbd558ddb82f8f51d8e9cb84b492a3d90e [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glasse9be1ee2016-05-01 11:36:10 -06002/*
3 * (C) Copyright 2000-2011
4 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
Simon Glasse9be1ee2016-05-01 11:36:10 -06005 */
6
Patrick Delaunayb953ec22021-04-27 11:02:19 +02007#define LOG_CATEGORY UCLASS_IDE
8
Simon Glasse9be1ee2016-05-01 11:36:10 -06009#include <common.h>
10#include <ata.h>
Simon Glasse6f6f9e2020-05-10 11:39:58 -060011#include <blk.h>
Simon Glass0d77f8f2023-01-17 10:47:46 -070012#include <bootdev.h>
Simon Glass145df842016-05-01 11:36:22 -060013#include <dm.h>
Simon Glasse9be1ee2016-05-01 11:36:10 -060014#include <ide.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060015#include <log.h>
Simon Glasse6f6f9e2020-05-10 11:39:58 -060016#include <part.h>
Simon Glasse9be1ee2016-05-01 11:36:10 -060017#include <watchdog.h>
18#include <asm/io.h>
Simon Glassc05ed002020-05-10 11:40:11 -060019#include <linux/delay.h>
Simon Glasse9be1ee2016-05-01 11:36:10 -060020
21#ifdef __PPC__
22# define EIEIO __asm__ volatile ("eieio")
23# define SYNC __asm__ volatile ("sync")
24#else
25# define EIEIO /* nothing */
26# define SYNC /* nothing */
27#endif
28
29/* Current offset for IDE0 / IDE1 bus access */
30ulong ide_bus_offset[CONFIG_SYS_IDE_MAXBUS] = {
31#if defined(CONFIG_SYS_ATA_IDE0_OFFSET)
32 CONFIG_SYS_ATA_IDE0_OFFSET,
33#endif
34#if defined(CONFIG_SYS_ATA_IDE1_OFFSET) && (CONFIG_SYS_IDE_MAXBUS > 1)
35 CONFIG_SYS_ATA_IDE1_OFFSET,
36#endif
37};
38
Simon Glass14a4f522023-04-25 10:54:26 -060039#define ATA_CURR_BASE(dev) (CONFIG_SYS_ATA_BASE_ADDR + \
40 ide_bus_offset[IDE_BUS(dev)])
41
Simon Glasse9be1ee2016-05-01 11:36:10 -060042#define IDE_TIME_OUT 2000 /* 2 sec timeout */
43
44#define ATAPI_TIME_OUT 7000 /* 7 sec timeout (5 sec seems to work...) */
45
46#define IDE_SPIN_UP_TIME_OUT 5000 /* 5 sec spin-up timeout */
47
Simon Glasse9be1ee2016-05-01 11:36:10 -060048static void ide_reset(void)
49{
Simon Glass9666de82023-04-25 10:54:52 -060050 if (IS_ENABLED(CONFIG_IDE_RESET)) {
51 /* assert reset */
52 ide_set_reset(1);
Simon Glasse9be1ee2016-05-01 11:36:10 -060053
Simon Glass9666de82023-04-25 10:54:52 -060054 /* the reset signal shall be asserted for et least 25 us */
55 udelay(25);
Simon Glasse9be1ee2016-05-01 11:36:10 -060056
Simon Glass9666de82023-04-25 10:54:52 -060057 schedule();
Simon Glasse9be1ee2016-05-01 11:36:10 -060058
Simon Glass9666de82023-04-25 10:54:52 -060059 /* de-assert RESET signal */
60 ide_set_reset(0);
Simon Glasse9be1ee2016-05-01 11:36:10 -060061
Simon Glass9666de82023-04-25 10:54:52 -060062 mdelay(250);
63 }
Simon Glasse9be1ee2016-05-01 11:36:10 -060064}
Simon Glasse9be1ee2016-05-01 11:36:10 -060065
Simon Glass22a7ae32023-04-25 10:54:55 -060066static void ide_outb(int dev, int port, u8 val)
Simon Glassbc65bff2023-04-25 10:54:32 -060067{
Simon Glass692bccb2023-04-25 10:54:53 -060068 log_debug("(dev= %d, port= %#x, val= 0x%02x) : @ 0x%08lx\n",
69 dev, port, val, ATA_CURR_BASE(dev) + port);
Simon Glassbc65bff2023-04-25 10:54:32 -060070
71 outb(val, ATA_CURR_BASE(dev) + port);
72}
73
Simon Glass22a7ae32023-04-25 10:54:55 -060074static u8 ide_inb(int dev, int port)
Simon Glassbc65bff2023-04-25 10:54:32 -060075{
76 uchar val;
77
78 val = inb(ATA_CURR_BASE(dev) + port);
79
Simon Glass692bccb2023-04-25 10:54:53 -060080 log_debug("(dev= %d, port= %#x) : @ 0x%08lx -> 0x%02x\n",
81 dev, port, ATA_CURR_BASE(dev) + port, val);
Simon Glassbc65bff2023-04-25 10:54:32 -060082 return val;
83}
84
Simon Glassf8e87e72023-04-25 10:54:33 -060085static void ide_input_swap_data(int dev, ulong *sect_buf, int words)
Simon Glassbc65bff2023-04-25 10:54:32 -060086{
87 uintptr_t paddr = (ATA_CURR_BASE(dev) + ATA_DATA_REG);
88 ushort *dbuf = (ushort *)sect_buf;
89
Simon Glass692bccb2023-04-25 10:54:53 -060090 log_debug("in input swap data base for read is %p\n", (void *)paddr);
Simon Glassbc65bff2023-04-25 10:54:32 -060091
92 while (words--) {
93 EIEIO;
94 *dbuf++ = be16_to_cpu(inw(paddr));
95 EIEIO;
96 *dbuf++ = be16_to_cpu(inw(paddr));
97 }
98}
99
Simon Glasse9be1ee2016-05-01 11:36:10 -0600100/*
101 * Wait until Busy bit is off, or timeout (in ms)
102 * Return last status
103 */
104static uchar ide_wait(int dev, ulong t)
105{
106 ulong delay = 10 * t; /* poll every 100 us */
107 uchar c;
108
109 while ((c = ide_inb(dev, ATA_STATUS)) & ATA_STAT_BUSY) {
110 udelay(100);
Simon Glass79543e62023-04-25 10:54:54 -0600111 if (!delay--)
Simon Glasse9be1ee2016-05-01 11:36:10 -0600112 break;
113 }
114 return c;
115}
116
117/*
118 * copy src to dest, skipping leading and trailing blanks and null
119 * terminate the string
120 * "len" is the size of available memory including the terminating '\0'
121 */
Simon Glass22a7ae32023-04-25 10:54:55 -0600122static void ident_cpy(u8 *dst, u8 *src, uint len)
Simon Glasse9be1ee2016-05-01 11:36:10 -0600123{
Simon Glass22a7ae32023-04-25 10:54:55 -0600124 u8 *end, *last;
Simon Glasse9be1ee2016-05-01 11:36:10 -0600125
126 last = dst;
127 end = src + len - 1;
128
129 /* reserve space for '\0' */
130 if (len < 2)
131 goto OUT;
132
133 /* skip leading white space */
134 while ((*src) && (src < end) && (*src == ' '))
135 ++src;
136
137 /* copy string, omitting trailing white space */
138 while ((*src) && (src < end)) {
139 *dst++ = *src;
140 if (*src++ != ' ')
141 last = dst;
142 }
143OUT:
144 *last = '\0';
145}
146
Simon Glasse9be1ee2016-05-01 11:36:10 -0600147/****************************************************************************
148 * ATAPI Support
149 */
150
Simon Glasse9be1ee2016-05-01 11:36:10 -0600151/* since ATAPI may use commands with not 4 bytes alligned length
152 * we have our own transfer functions, 2 bytes alligned */
Simon Glassf8e87e72023-04-25 10:54:33 -0600153static void ide_output_data_shorts(int dev, ushort *sect_buf, int shorts)
Simon Glasse9be1ee2016-05-01 11:36:10 -0600154{
Simon Glass79543e62023-04-25 10:54:54 -0600155 uintptr_t paddr = ATA_CURR_BASE(dev) + ATA_DATA_REG;
Simon Glasse9be1ee2016-05-01 11:36:10 -0600156 ushort *dbuf;
Simon Glasse9be1ee2016-05-01 11:36:10 -0600157
Simon Glasse9be1ee2016-05-01 11:36:10 -0600158 dbuf = (ushort *)sect_buf;
159
Simon Glass692bccb2023-04-25 10:54:53 -0600160 log_debug("in output data shorts base for read is %p\n", (void *)paddr);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600161
162 while (shorts--) {
163 EIEIO;
Reinoud Zandijk0a527fd2021-02-24 17:44:42 +0100164 outw(cpu_to_le16(*dbuf++), paddr);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600165 }
166}
167
Simon Glassf8e87e72023-04-25 10:54:33 -0600168static void ide_input_data_shorts(int dev, ushort *sect_buf, int shorts)
Simon Glasse9be1ee2016-05-01 11:36:10 -0600169{
Simon Glass79543e62023-04-25 10:54:54 -0600170 uintptr_t paddr = ATA_CURR_BASE(dev) + ATA_DATA_REG;
Simon Glasse9be1ee2016-05-01 11:36:10 -0600171 ushort *dbuf;
Simon Glasse9be1ee2016-05-01 11:36:10 -0600172
Simon Glasse9be1ee2016-05-01 11:36:10 -0600173 dbuf = (ushort *)sect_buf;
174
Simon Glass692bccb2023-04-25 10:54:53 -0600175 log_debug("in input data shorts base for read is %p\n", (void *)paddr);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600176
177 while (shorts--) {
178 EIEIO;
Reinoud Zandijk0a527fd2021-02-24 17:44:42 +0100179 *dbuf++ = le16_to_cpu(inw(paddr));
Simon Glasse9be1ee2016-05-01 11:36:10 -0600180 }
181}
182
Simon Glasse9be1ee2016-05-01 11:36:10 -0600183/*
184 * Wait until (Status & mask) == res, or timeout (in ms)
185 * Return last status
186 * This is used since some ATAPI CD ROMs clears their Busy Bit first
187 * and then they set their DRQ Bit
188 */
189static uchar atapi_wait_mask(int dev, ulong t, uchar mask, uchar res)
190{
191 ulong delay = 10 * t; /* poll every 100 us */
192 uchar c;
193
194 /* prevents to read the status before valid */
195 c = ide_inb(dev, ATA_DEV_CTL);
196
Simon Glass79543e62023-04-25 10:54:54 -0600197 while (c = ide_inb(dev, ATA_STATUS) & mask, c != res) {
Simon Glasse9be1ee2016-05-01 11:36:10 -0600198 /* break if error occurs (doesn't make sense to wait more) */
199 if ((c & ATA_STAT_ERR) == ATA_STAT_ERR)
200 break;
201 udelay(100);
Simon Glass79543e62023-04-25 10:54:54 -0600202 if (!delay--)
Simon Glasse9be1ee2016-05-01 11:36:10 -0600203 break;
204 }
205 return c;
206}
207
208/*
209 * issue an atapi command
210 */
Simon Glass22a7ae32023-04-25 10:54:55 -0600211static u8 atapi_issue(int device, u8 *ccb, int ccblen, u8 *buffer, int buflen)
Simon Glasse9be1ee2016-05-01 11:36:10 -0600212{
Simon Glass22a7ae32023-04-25 10:54:55 -0600213 u8 c, err, mask, res;
Simon Glasse9be1ee2016-05-01 11:36:10 -0600214 int n;
215
Simon Glasse9be1ee2016-05-01 11:36:10 -0600216 /* Select device
217 */
218 mask = ATA_STAT_BUSY | ATA_STAT_DRQ;
219 res = 0;
220 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
221 c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
222 if ((c & mask) != res) {
Simon Glass692bccb2023-04-25 10:54:53 -0600223 printf("ATAPI_ISSUE: device %d not ready status %x\n", device,
Simon Glasse9be1ee2016-05-01 11:36:10 -0600224 c);
Simon Glass79543e62023-04-25 10:54:54 -0600225 err = 0xff;
Simon Glasse9be1ee2016-05-01 11:36:10 -0600226 goto AI_OUT;
227 }
228 /* write taskfile */
229 ide_outb(device, ATA_ERROR_REG, 0); /* no DMA, no overlaped */
230 ide_outb(device, ATA_SECT_CNT, 0);
231 ide_outb(device, ATA_SECT_NUM, 0);
Simon Glass22a7ae32023-04-25 10:54:55 -0600232 ide_outb(device, ATA_CYL_LOW, (u8)(buflen & 0xff));
233 ide_outb(device, ATA_CYL_HIGH, (u8)((buflen >> 8) & 0xff));
Simon Glasse9be1ee2016-05-01 11:36:10 -0600234 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
235
Heinrich Schuchardte6e9a4f2020-02-27 18:28:00 +0100236 ide_outb(device, ATA_COMMAND, ATA_CMD_PACKET);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600237 udelay(50);
238
239 mask = ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR;
240 res = ATA_STAT_DRQ;
241 c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
242
243 if ((c & mask) != res) { /* DRQ must be 1, BSY 0 */
Simon Glass79543e62023-04-25 10:54:54 -0600244 printf("ATAPI_ISSUE: Error (no IRQ) before sending ccb dev %d status %#02x\n",
Simon Glasse9be1ee2016-05-01 11:36:10 -0600245 device, c);
Simon Glass79543e62023-04-25 10:54:54 -0600246 err = 0xff;
Simon Glasse9be1ee2016-05-01 11:36:10 -0600247 goto AI_OUT;
248 }
249
250 /* write command block */
Simon Glass22a7ae32023-04-25 10:54:55 -0600251 ide_output_data_shorts(device, (ushort *)ccb, ccblen / 2);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600252
253 /* ATAPI Command written wait for completition */
Simon Glass4d89f4b2023-04-25 10:54:27 -0600254 mdelay(5); /* device must set bsy */
Simon Glasse9be1ee2016-05-01 11:36:10 -0600255
256 mask = ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR;
257 /*
258 * if no data wait for DRQ = 0 BSY = 0
259 * if data wait for DRQ = 1 BSY = 0
260 */
261 res = 0;
262 if (buflen)
263 res = ATA_STAT_DRQ;
264 c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
265 if ((c & mask) != res) {
266 if (c & ATA_STAT_ERR) {
267 err = (ide_inb(device, ATA_ERROR_REG)) >> 4;
Simon Glass692bccb2023-04-25 10:54:53 -0600268 log_debug("1 returned sense key %x status %02x\n",
269 err, c);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600270 } else {
Simon Glass79543e62023-04-25 10:54:54 -0600271 printf("ATAPI_ISSUE: (no DRQ) after sending ccb (%x) status %#02x\n",
Simon Glasse9be1ee2016-05-01 11:36:10 -0600272 ccb[0], c);
Simon Glass79543e62023-04-25 10:54:54 -0600273 err = 0xff;
Simon Glasse9be1ee2016-05-01 11:36:10 -0600274 }
275 goto AI_OUT;
276 }
277 n = ide_inb(device, ATA_CYL_HIGH);
278 n <<= 8;
279 n += ide_inb(device, ATA_CYL_LOW);
280 if (n > buflen) {
281 printf("ERROR, transfer bytes %d requested only %d\n", n,
282 buflen);
283 err = 0xff;
284 goto AI_OUT;
285 }
Simon Glass79543e62023-04-25 10:54:54 -0600286 if (!n && buflen < 0) {
Simon Glasse9be1ee2016-05-01 11:36:10 -0600287 printf("ERROR, transfer bytes %d requested %d\n", n, buflen);
288 err = 0xff;
289 goto AI_OUT;
290 }
291 if (n != buflen) {
Simon Glass692bccb2023-04-25 10:54:53 -0600292 log_debug("WARNING, transfer bytes %d not equal with requested %d\n",
293 n, buflen);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600294 }
Simon Glass79543e62023-04-25 10:54:54 -0600295 if (n) { /* data transfer */
Simon Glass692bccb2023-04-25 10:54:53 -0600296 log_debug("ATAPI_ISSUE: %d Bytes to transfer\n", n);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600297 /* we transfer shorts */
298 n >>= 1;
299 /* ok now decide if it is an in or output */
Simon Glass79543e62023-04-25 10:54:54 -0600300 if (!(ide_inb(device, ATA_SECT_CNT) & 0x02)) {
Simon Glass692bccb2023-04-25 10:54:53 -0600301 log_debug("Write to device\n");
Simon Glass22a7ae32023-04-25 10:54:55 -0600302 ide_output_data_shorts(device, (ushort *)buffer, n);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600303 } else {
Simon Glass692bccb2023-04-25 10:54:53 -0600304 log_debug("Read from device @ %p shorts %d\n", buffer,
305 n);
Simon Glass22a7ae32023-04-25 10:54:55 -0600306 ide_input_data_shorts(device, (ushort *)buffer, n);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600307 }
308 }
Simon Glass4d89f4b2023-04-25 10:54:27 -0600309 mdelay(5); /* seems that some CD ROMs need this... */
Simon Glasse9be1ee2016-05-01 11:36:10 -0600310 mask = ATA_STAT_BUSY | ATA_STAT_ERR;
311 res = 0;
312 c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
313 if ((c & ATA_STAT_ERR) == ATA_STAT_ERR) {
314 err = (ide_inb(device, ATA_ERROR_REG) >> 4);
Simon Glass692bccb2023-04-25 10:54:53 -0600315 log_debug("2 returned sense key %x status %x\n", err, c);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600316 } else {
317 err = 0;
318 }
319AI_OUT:
Simon Glasse9be1ee2016-05-01 11:36:10 -0600320 return err;
321}
322
323/*
324 * sending the command to atapi_issue. If an status other than good
325 * returns, an request_sense will be issued
326 */
327
328#define ATAPI_DRIVE_NOT_READY 100
329#define ATAPI_UNIT_ATTN 10
330
Simon Glass22a7ae32023-04-25 10:54:55 -0600331static u8 atapi_issue_autoreq(int device, u8 *ccb, int ccblen, u8 *buffer,
332 int buflen)
Simon Glasse9be1ee2016-05-01 11:36:10 -0600333{
Simon Glass22a7ae32023-04-25 10:54:55 -0600334 u8 sense_data[18], sense_ccb[12];
335 u8 res, key, asc, ascq;
Simon Glasse9be1ee2016-05-01 11:36:10 -0600336 int notready, unitattn;
337
338 unitattn = ATAPI_UNIT_ATTN;
339 notready = ATAPI_DRIVE_NOT_READY;
340
341retry:
342 res = atapi_issue(device, ccb, ccblen, buffer, buflen);
Simon Glass79543e62023-04-25 10:54:54 -0600343 if (!res)
Simon Glasse9be1ee2016-05-01 11:36:10 -0600344 return 0; /* Ok */
345
Simon Glass79543e62023-04-25 10:54:54 -0600346 if (res == 0xff)
347 return 0xff; /* error */
Simon Glasse9be1ee2016-05-01 11:36:10 -0600348
Simon Glass692bccb2023-04-25 10:54:53 -0600349 log_debug("(auto_req)atapi_issue returned sense key %x\n", res);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600350
351 memset(sense_ccb, 0, sizeof(sense_ccb));
352 memset(sense_data, 0, sizeof(sense_data));
353 sense_ccb[0] = ATAPI_CMD_REQ_SENSE;
354 sense_ccb[4] = 18; /* allocation Length */
355
356 res = atapi_issue(device, sense_ccb, 12, sense_data, 18);
Simon Glass79543e62023-04-25 10:54:54 -0600357 key = (sense_data[2] & 0xf);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600358 asc = (sense_data[12]);
359 ascq = (sense_data[13]);
360
Simon Glass692bccb2023-04-25 10:54:53 -0600361 log_debug("ATAPI_CMD_REQ_SENSE returned %x\n", res);
362 log_debug(" Sense page: %02X key %02X ASC %02X ASCQ %02X\n",
363 sense_data[0], key, asc, ascq);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600364
Simon Glass79543e62023-04-25 10:54:54 -0600365 if (!key)
Simon Glasse9be1ee2016-05-01 11:36:10 -0600366 return 0; /* ok device ready */
367
Simon Glass79543e62023-04-25 10:54:54 -0600368 if (key == 6 || asc == 0x29 || asc == 0x28) { /* Unit Attention */
Simon Glasse9be1ee2016-05-01 11:36:10 -0600369 if (unitattn-- > 0) {
Simon Glass4d89f4b2023-04-25 10:54:27 -0600370 mdelay(200);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600371 goto retry;
372 }
373 printf("Unit Attention, tried %d\n", ATAPI_UNIT_ATTN);
374 goto error;
375 }
Simon Glass79543e62023-04-25 10:54:54 -0600376 if (asc == 0x4 && ascq == 0x1) {
Simon Glasse9be1ee2016-05-01 11:36:10 -0600377 /* not ready, but will be ready soon */
378 if (notready-- > 0) {
Simon Glass4d89f4b2023-04-25 10:54:27 -0600379 mdelay(200);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600380 goto retry;
381 }
382 printf("Drive not ready, tried %d times\n",
383 ATAPI_DRIVE_NOT_READY);
384 goto error;
385 }
386 if (asc == 0x3a) {
Simon Glass692bccb2023-04-25 10:54:53 -0600387 log_debug("Media not present\n");
Simon Glasse9be1ee2016-05-01 11:36:10 -0600388 goto error;
389 }
390
391 printf("ERROR: Unknown Sense key %02X ASC %02X ASCQ %02X\n", key, asc,
392 ascq);
393error:
Simon Glass692bccb2023-04-25 10:54:53 -0600394 log_debug("ERROR Sense key %02X ASC %02X ASCQ %02X\n", key, asc, ascq);
Simon Glass79543e62023-04-25 10:54:54 -0600395 return 0xff;
Simon Glasse9be1ee2016-05-01 11:36:10 -0600396}
397
398/*
399 * atapi_read:
400 * we transfer only one block per command, since the multiple DRQ per
401 * command is not yet implemented
402 */
403#define ATAPI_READ_MAX_BYTES 2048 /* we read max 2kbytes */
404#define ATAPI_READ_BLOCK_SIZE 2048 /* assuming CD part */
405#define ATAPI_READ_MAX_BLOCK (ATAPI_READ_MAX_BYTES/ATAPI_READ_BLOCK_SIZE)
406
Simon Glass1b33fd82023-04-25 10:54:36 -0600407static ulong atapi_read(struct udevice *dev, lbaint_t blknr, lbaint_t blkcnt,
408 void *buffer)
Simon Glasse9be1ee2016-05-01 11:36:10 -0600409{
Simon Glassce99e292023-04-25 10:54:47 -0600410 struct blk_desc *desc = dev_get_uclass_plat(dev);
411 int device = desc->devnum;
Simon Glasse9be1ee2016-05-01 11:36:10 -0600412 ulong n = 0;
Simon Glass22a7ae32023-04-25 10:54:55 -0600413 u8 ccb[12]; /* Command descriptor block */
Simon Glasse9be1ee2016-05-01 11:36:10 -0600414 ulong cnt;
415
Simon Glass692bccb2023-04-25 10:54:53 -0600416 log_debug("%d start " LBAF " blocks " LBAF " buffer at %lx\n", device,
417 blknr, blkcnt, (ulong)buffer);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600418
419 do {
420 if (blkcnt > ATAPI_READ_MAX_BLOCK)
421 cnt = ATAPI_READ_MAX_BLOCK;
422 else
423 cnt = blkcnt;
424
425 ccb[0] = ATAPI_CMD_READ_12;
426 ccb[1] = 0; /* reserved */
Simon Glass22a7ae32023-04-25 10:54:55 -0600427 ccb[2] = (u8)(blknr >> 24) & 0xff; /* MSB Block */
428 ccb[3] = (u8)(blknr >> 16) & 0xff; /* */
429 ccb[4] = (u8)(blknr >> 8) & 0xff;
430 ccb[5] = (u8)blknr & 0xff; /* LSB Block */
431 ccb[6] = (u8)(cnt >> 24) & 0xff; /* MSB Block cnt */
432 ccb[7] = (u8)(cnt >> 16) & 0xff;
433 ccb[8] = (u8)(cnt >> 8) & 0xff;
434 ccb[9] = (u8)cnt & 0xff; /* LSB Block */
Simon Glasse9be1ee2016-05-01 11:36:10 -0600435 ccb[10] = 0; /* reserved */
436 ccb[11] = 0; /* reserved */
437
438 if (atapi_issue_autoreq(device, ccb, 12,
Simon Glass22a7ae32023-04-25 10:54:55 -0600439 (u8 *)buffer,
Simon Glass79543e62023-04-25 10:54:54 -0600440 cnt * ATAPI_READ_BLOCK_SIZE) == 0xff)
Simon Glasse9be1ee2016-05-01 11:36:10 -0600441 return n;
Simon Glasse9be1ee2016-05-01 11:36:10 -0600442 n += cnt;
443 blkcnt -= cnt;
444 blknr += cnt;
Simon Glass79543e62023-04-25 10:54:54 -0600445 buffer += cnt * ATAPI_READ_BLOCK_SIZE;
Simon Glasse9be1ee2016-05-01 11:36:10 -0600446 } while (blkcnt > 0);
447 return n;
448}
449
Simon Glassce99e292023-04-25 10:54:47 -0600450static void atapi_inquiry(struct blk_desc *desc)
Simon Glasse9be1ee2016-05-01 11:36:10 -0600451{
Simon Glass22a7ae32023-04-25 10:54:55 -0600452 u8 ccb[12]; /* Command descriptor block */
453 u8 iobuf[64]; /* temp buf */
454 u8 c;
Simon Glasse9be1ee2016-05-01 11:36:10 -0600455 int device;
456
Simon Glassce99e292023-04-25 10:54:47 -0600457 device = desc->devnum;
458 desc->type = DEV_TYPE_UNKNOWN; /* not yet valid */
Simon Glasse9be1ee2016-05-01 11:36:10 -0600459
460 memset(ccb, 0, sizeof(ccb));
461 memset(iobuf, 0, sizeof(iobuf));
462
463 ccb[0] = ATAPI_CMD_INQUIRY;
464 ccb[4] = 40; /* allocation Legnth */
Simon Glass22a7ae32023-04-25 10:54:55 -0600465 c = atapi_issue_autoreq(device, ccb, 12, (u8 *)iobuf, 40);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600466
Simon Glass692bccb2023-04-25 10:54:53 -0600467 log_debug("ATAPI_CMD_INQUIRY returned %x\n", c);
Simon Glass79543e62023-04-25 10:54:54 -0600468 if (c)
Simon Glasse9be1ee2016-05-01 11:36:10 -0600469 return;
470
471 /* copy device ident strings */
Simon Glassce99e292023-04-25 10:54:47 -0600472 ident_cpy((u8 *)desc->vendor, &iobuf[8], 8);
473 ident_cpy((u8 *)desc->product, &iobuf[16], 16);
474 ident_cpy((u8 *)desc->revision, &iobuf[32], 5);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600475
Simon Glassce99e292023-04-25 10:54:47 -0600476 desc->lun = 0;
477 desc->lba = 0;
478 desc->blksz = 0;
479 desc->log2blksz = LOG2_INVALID(typeof(desc->log2blksz));
480 desc->type = iobuf[0] & 0x1f;
Simon Glasse9be1ee2016-05-01 11:36:10 -0600481
Simon Glass79543e62023-04-25 10:54:54 -0600482 if (iobuf[1] & 0x80)
Simon Glassce99e292023-04-25 10:54:47 -0600483 desc->removable = 1;
Simon Glasse9be1ee2016-05-01 11:36:10 -0600484 else
Simon Glassce99e292023-04-25 10:54:47 -0600485 desc->removable = 0;
Simon Glasse9be1ee2016-05-01 11:36:10 -0600486
487 memset(ccb, 0, sizeof(ccb));
488 memset(iobuf, 0, sizeof(iobuf));
489 ccb[0] = ATAPI_CMD_START_STOP;
490 ccb[4] = 0x03; /* start */
491
Simon Glass22a7ae32023-04-25 10:54:55 -0600492 c = atapi_issue_autoreq(device, ccb, 12, (u8 *)iobuf, 0);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600493
Simon Glass692bccb2023-04-25 10:54:53 -0600494 log_debug("ATAPI_CMD_START_STOP returned %x\n", c);
Simon Glass79543e62023-04-25 10:54:54 -0600495 if (c)
Simon Glasse9be1ee2016-05-01 11:36:10 -0600496 return;
497
498 memset(ccb, 0, sizeof(ccb));
499 memset(iobuf, 0, sizeof(iobuf));
Simon Glass22a7ae32023-04-25 10:54:55 -0600500 c = atapi_issue_autoreq(device, ccb, 12, (u8 *)iobuf, 0);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600501
Simon Glass692bccb2023-04-25 10:54:53 -0600502 log_debug("ATAPI_CMD_UNIT_TEST_READY returned %x\n", c);
Simon Glass79543e62023-04-25 10:54:54 -0600503 if (c)
Simon Glasse9be1ee2016-05-01 11:36:10 -0600504 return;
505
506 memset(ccb, 0, sizeof(ccb));
507 memset(iobuf, 0, sizeof(iobuf));
508 ccb[0] = ATAPI_CMD_READ_CAP;
Simon Glass22a7ae32023-04-25 10:54:55 -0600509 c = atapi_issue_autoreq(device, ccb, 12, (u8 *)iobuf, 8);
Simon Glass692bccb2023-04-25 10:54:53 -0600510 log_debug("ATAPI_CMD_READ_CAP returned %x\n", c);
Simon Glass79543e62023-04-25 10:54:54 -0600511 if (c)
Simon Glasse9be1ee2016-05-01 11:36:10 -0600512 return;
513
Simon Glass692bccb2023-04-25 10:54:53 -0600514 log_debug("Read Cap: LBA %02X%02X%02X%02X blksize %02X%02X%02X%02X\n",
515 iobuf[0], iobuf[1], iobuf[2], iobuf[3],
516 iobuf[4], iobuf[5], iobuf[6], iobuf[7]);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600517
Simon Glassce99e292023-04-25 10:54:47 -0600518 desc->lba = (ulong)iobuf[0] << 24 | (ulong)iobuf[1] << 16 |
519 (ulong)iobuf[2] << 8 | (ulong)iobuf[3];
520 desc->blksz = (ulong)iobuf[4] << 24 | (ulong)iobuf[5] << 16 |
521 (ulong)iobuf[6] << 8 | (ulong)iobuf[7];
522 desc->log2blksz = LOG2(desc->blksz);
Simon Glass8b1b9432023-04-25 10:54:41 -0600523
Simon Glasse9be1ee2016-05-01 11:36:10 -0600524 /* ATAPI devices cannot use 48bit addressing (ATA/ATAPI v7) */
Simon Glassce99e292023-04-25 10:54:47 -0600525 desc->lba48 = false;
Simon Glasse9be1ee2016-05-01 11:36:10 -0600526}
527
Simon Glass038590a2023-04-25 10:54:48 -0600528/**
529 * ide_ident() - Identify an IDE device
530 *
531 * @device: Device number to use
532 * @desc: Block descriptor to fill in
533 * Returns: 0 if OK, -ENOENT if no device is found
534 */
535static int ide_ident(int device, struct blk_desc *desc)
Simon Glasse9be1ee2016-05-01 11:36:10 -0600536{
Simon Glasse9be1ee2016-05-01 11:36:10 -0600537 hd_driveid_t iop;
Simon Glass606e0542022-08-11 19:34:53 -0600538 bool is_atapi = false;
Simon Glass2a165952023-04-25 10:54:37 -0600539 int tries = 1;
Simon Glass22a7ae32023-04-25 10:54:55 -0600540 u8 c;
Simon Glasse9be1ee2016-05-01 11:36:10 -0600541
Simon Glass96083112023-04-25 10:54:49 -0600542 memset(desc, '\0', sizeof(*desc));
Simon Glass038590a2023-04-25 10:54:48 -0600543 desc->devnum = device;
Simon Glass96083112023-04-25 10:54:49 -0600544 desc->type = DEV_TYPE_UNKNOWN;
545 desc->uclass_id = UCLASS_IDE;
546 desc->log2blksz = LOG2_INVALID(typeof(desc->log2blksz));
Simon Glasse9be1ee2016-05-01 11:36:10 -0600547 printf(" Device %d: ", device);
548
Simon Glasse9be1ee2016-05-01 11:36:10 -0600549 /* Select device
550 */
551 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
Simon Glass6579bb02023-04-25 10:54:38 -0600552 if (IS_ENABLED(CONFIG_ATAPI))
553 tries = 2;
Simon Glasse9be1ee2016-05-01 11:36:10 -0600554
Simon Glass2a165952023-04-25 10:54:37 -0600555 while (tries) {
Simon Glasse9be1ee2016-05-01 11:36:10 -0600556 /* check signature */
Simon Glass6579bb02023-04-25 10:54:38 -0600557 if (IS_ENABLED(CONFIG_ATAPI) &&
558 ide_inb(device, ATA_SECT_CNT) == 0x01 &&
559 ide_inb(device, ATA_SECT_NUM) == 0x01 &&
560 ide_inb(device, ATA_CYL_LOW) == 0x14 &&
561 ide_inb(device, ATA_CYL_HIGH) == 0xeb) {
Simon Glasse9be1ee2016-05-01 11:36:10 -0600562 /* ATAPI Signature found */
Simon Glass606e0542022-08-11 19:34:53 -0600563 is_atapi = true;
Simon Glasse9be1ee2016-05-01 11:36:10 -0600564 /*
565 * Start Ident Command
566 */
Heinrich Schuchardte6e9a4f2020-02-27 18:28:00 +0100567 ide_outb(device, ATA_COMMAND, ATA_CMD_ID_ATAPI);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600568 /*
569 * Wait for completion - ATAPI devices need more time
570 * to become ready
571 */
572 c = ide_wait(device, ATAPI_TIME_OUT);
Simon Glass6579bb02023-04-25 10:54:38 -0600573 } else {
Simon Glasse9be1ee2016-05-01 11:36:10 -0600574 /*
575 * Start Ident Command
576 */
Heinrich Schuchardte6e9a4f2020-02-27 18:28:00 +0100577 ide_outb(device, ATA_COMMAND, ATA_CMD_ID_ATA);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600578
579 /*
580 * Wait for completion
581 */
582 c = ide_wait(device, IDE_TIME_OUT);
583 }
Simon Glasse9be1ee2016-05-01 11:36:10 -0600584
Simon Glass9367e612023-04-25 10:54:39 -0600585 if ((c & ATA_STAT_DRQ) &&
586 !(c & (ATA_STAT_FAULT | ATA_STAT_ERR))) {
Simon Glasse9be1ee2016-05-01 11:36:10 -0600587 break;
Simon Glass9367e612023-04-25 10:54:39 -0600588 } else if (IS_ENABLED(CONFIG_ATAPI)) {
589 /*
590 * Need to soft reset the device
591 * in case it's an ATAPI...
592 */
Simon Glass692bccb2023-04-25 10:54:53 -0600593 log_debug("Retrying...\n");
Simon Glass9367e612023-04-25 10:54:39 -0600594 ide_outb(device, ATA_DEV_HD,
595 ATA_LBA | ATA_DEVICE(device));
596 mdelay(100);
597 ide_outb(device, ATA_COMMAND, 0x08);
598 mdelay(500);
599 /* Select device */
600 ide_outb(device, ATA_DEV_HD,
601 ATA_LBA | ATA_DEVICE(device));
Simon Glass6579bb02023-04-25 10:54:38 -0600602 }
Simon Glass9367e612023-04-25 10:54:39 -0600603 tries--;
Simon Glass6579bb02023-04-25 10:54:38 -0600604 }
Simon Glasse9be1ee2016-05-01 11:36:10 -0600605
Simon Glass2a165952023-04-25 10:54:37 -0600606 if (!tries) /* Not found */
Simon Glass038590a2023-04-25 10:54:48 -0600607 return -ENOENT;
Simon Glasse9be1ee2016-05-01 11:36:10 -0600608
609 ide_input_swap_data(device, (ulong *)&iop, ATA_SECTORWORDS);
610
Simon Glassce99e292023-04-25 10:54:47 -0600611 ident_cpy((u8 *)desc->revision, iop.fw_rev, sizeof(desc->revision));
612 ident_cpy((u8 *)desc->vendor, iop.model, sizeof(desc->vendor));
613 ident_cpy((u8 *)desc->product, iop.serial_no, sizeof(desc->product));
Simon Glasse9be1ee2016-05-01 11:36:10 -0600614
Simon Glass79543e62023-04-25 10:54:54 -0600615 if (iop.config & 0x0080)
Simon Glassce99e292023-04-25 10:54:47 -0600616 desc->removable = 1;
Simon Glasse9be1ee2016-05-01 11:36:10 -0600617 else
Simon Glassce99e292023-04-25 10:54:47 -0600618 desc->removable = 0;
Simon Glasse9be1ee2016-05-01 11:36:10 -0600619
Simon Glassc94a3312023-04-25 10:54:40 -0600620 if (IS_ENABLED(CONFIG_ATAPI) && is_atapi) {
Simon Glassce99e292023-04-25 10:54:47 -0600621 desc->atapi = true;
622 atapi_inquiry(desc);
Simon Glass038590a2023-04-25 10:54:48 -0600623 return 0;
Simon Glasse9be1ee2016-05-01 11:36:10 -0600624 }
Simon Glasse9be1ee2016-05-01 11:36:10 -0600625
Reinoud Zandijk0a527fd2021-02-24 17:44:42 +0100626 iop.lba_capacity[0] = be16_to_cpu(iop.lba_capacity[0]);
627 iop.lba_capacity[1] = be16_to_cpu(iop.lba_capacity[1]);
Simon Glassce99e292023-04-25 10:54:47 -0600628 desc->lba = (ulong)iop.lba_capacity[0] |
629 (ulong)iop.lba_capacity[1] << 16;
Simon Glasse9be1ee2016-05-01 11:36:10 -0600630
Simon Glass8b1b9432023-04-25 10:54:41 -0600631 if (IS_ENABLED(CONFIG_LBA48) && (iop.command_set_2 & 0x0400)) {
632 /* LBA 48 support */
Simon Glassce99e292023-04-25 10:54:47 -0600633 desc->lba48 = true;
Reinoud Zandijk0a527fd2021-02-24 17:44:42 +0100634 for (int i = 0; i < 4; i++)
635 iop.lba48_capacity[i] = be16_to_cpu(iop.lba48_capacity[i]);
Simon Glassce99e292023-04-25 10:54:47 -0600636 desc->lba = (unsigned long long)iop.lba48_capacity[0] |
637 (unsigned long long)iop.lba48_capacity[1] << 16 |
638 (unsigned long long)iop.lba48_capacity[2] << 32 |
639 (unsigned long long)iop.lba48_capacity[3] << 48;
Simon Glasse9be1ee2016-05-01 11:36:10 -0600640 } else {
Simon Glassce99e292023-04-25 10:54:47 -0600641 desc->lba48 = false;
Simon Glasse9be1ee2016-05-01 11:36:10 -0600642 }
Simon Glass8b1b9432023-04-25 10:54:41 -0600643
Simon Glasse9be1ee2016-05-01 11:36:10 -0600644 /* assuming HD */
Simon Glassce99e292023-04-25 10:54:47 -0600645 desc->type = DEV_TYPE_HARDDISK;
646 desc->blksz = ATA_BLOCKSIZE;
647 desc->log2blksz = LOG2(desc->blksz);
648 desc->lun = 0; /* just to fill something in... */
Simon Glasse9be1ee2016-05-01 11:36:10 -0600649
650#if 0 /* only used to test the powersaving mode,
651 * if enabled, the drive goes after 5 sec
652 * in standby mode */
653 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
654 c = ide_wait(device, IDE_TIME_OUT);
655 ide_outb(device, ATA_SECT_CNT, 1);
656 ide_outb(device, ATA_LBA_LOW, 0);
657 ide_outb(device, ATA_LBA_MID, 0);
658 ide_outb(device, ATA_LBA_HIGH, 0);
659 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
660 ide_outb(device, ATA_COMMAND, 0xe3);
661 udelay(50);
662 c = ide_wait(device, IDE_TIME_OUT); /* can't take over 500 ms */
663#endif
Simon Glass038590a2023-04-25 10:54:48 -0600664
665 return 0;
Simon Glasse9be1ee2016-05-01 11:36:10 -0600666}
667
Simon Glassb6483ea2023-04-25 10:54:42 -0600668/**
669 * ide_init_one() - Init one IDE device
670 *
671 * @bus: Bus to use
672 * Return: 0 iuf OK, -EIO if not available, -ETIMEDOUT if timed out
673 */
674static int ide_init_one(int bus)
675{
676 int dev = bus * CONFIG_SYS_IDE_MAXDEVICE / CONFIG_SYS_IDE_MAXBUS;
677 int i;
678 u8 c;
679
680 printf("Bus %d: ", bus);
681
682 /* Select device */
683 mdelay(100);
684 ide_outb(dev, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(dev));
685 mdelay(100);
686 i = 0;
687 do {
688 mdelay(10);
689
690 c = ide_inb(dev, ATA_STATUS);
691 i++;
692 if (i > (ATA_RESET_TIME * 100)) {
693 puts("** Timeout **\n");
694 return -ETIMEDOUT;
695 }
696 if (i >= 100 && !(i % 100))
697 putc('.');
698 } while (c & ATA_STAT_BUSY);
699
700 if (c & (ATA_STAT_BUSY | ATA_STAT_FAULT)) {
701 puts("not available ");
Simon Glass79543e62023-04-25 10:54:54 -0600702 log_debug("Status = %#02X ", c);
Simon Glassb6483ea2023-04-25 10:54:42 -0600703 return -EIO;
704 } else if (IS_ENABLED(CONFIG_ATAPI) && !(c & ATA_STAT_READY)) {
705 /* ATAPI Devices do not set DRDY */
706 puts("not available ");
Simon Glass79543e62023-04-25 10:54:54 -0600707 log_debug("Status = %#02X ", c);
Simon Glassb6483ea2023-04-25 10:54:42 -0600708 return -EIO;
709 }
710 puts("OK ");
711
712 return 0;
713}
714
Simon Glassf8e87e72023-04-25 10:54:33 -0600715static void ide_output_data(int dev, const ulong *sect_buf, int words)
Simon Glasse9be1ee2016-05-01 11:36:10 -0600716{
Reinoud Zandijk0a527fd2021-02-24 17:44:42 +0100717 uintptr_t paddr = (ATA_CURR_BASE(dev) + ATA_DATA_REG);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600718 ushort *dbuf;
Simon Glasse9be1ee2016-05-01 11:36:10 -0600719
Simon Glasse9be1ee2016-05-01 11:36:10 -0600720 dbuf = (ushort *)sect_buf;
Simon Glasse9be1ee2016-05-01 11:36:10 -0600721 while (words--) {
722 EIEIO;
Reinoud Zandijk0a527fd2021-02-24 17:44:42 +0100723 outw(cpu_to_le16(*dbuf++), paddr);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600724 EIEIO;
Reinoud Zandijk0a527fd2021-02-24 17:44:42 +0100725 outw(cpu_to_le16(*dbuf++), paddr);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600726 }
727}
Reinoud Zandijk0a527fd2021-02-24 17:44:42 +0100728
Simon Glassf8e87e72023-04-25 10:54:33 -0600729static void ide_input_data(int dev, ulong *sect_buf, int words)
Simon Glasse9be1ee2016-05-01 11:36:10 -0600730{
Reinoud Zandijk0a527fd2021-02-24 17:44:42 +0100731 uintptr_t paddr = (ATA_CURR_BASE(dev) + ATA_DATA_REG);
732 ushort *dbuf;
Simon Glasse9be1ee2016-05-01 11:36:10 -0600733
Reinoud Zandijk0a527fd2021-02-24 17:44:42 +0100734 dbuf = (ushort *)sect_buf;
735
Simon Glass692bccb2023-04-25 10:54:53 -0600736 log_debug("in input data base for read is %p\n", (void *)paddr);
Reinoud Zandijk0a527fd2021-02-24 17:44:42 +0100737
738 while (words--) {
739 EIEIO;
740 *dbuf++ = le16_to_cpu(inw(paddr));
741 EIEIO;
742 *dbuf++ = le16_to_cpu(inw(paddr));
743 }
Reinoud Zandijk0a527fd2021-02-24 17:44:42 +0100744}
Simon Glasse9be1ee2016-05-01 11:36:10 -0600745
Simon Glass1b33fd82023-04-25 10:54:36 -0600746static ulong ide_read(struct udevice *dev, lbaint_t blknr, lbaint_t blkcnt,
747 void *buffer)
Simon Glasse9be1ee2016-05-01 11:36:10 -0600748{
Simon Glassce99e292023-04-25 10:54:47 -0600749 struct blk_desc *desc = dev_get_uclass_plat(dev);
750 int device = desc->devnum;
Simon Glass8b1b9432023-04-25 10:54:41 -0600751 bool lba48 = false;
Simon Glass22a7ae32023-04-25 10:54:55 -0600752 ulong n = 0;
753 u8 pwrsave = 0; /* power save */
754 u8 c;
Simon Glasse9be1ee2016-05-01 11:36:10 -0600755
Simon Glass8b1b9432023-04-25 10:54:41 -0600756 if (IS_ENABLED(CONFIG_LBA48) && (blknr & 0x0000fffff0000000ULL)) {
Simon Glasse9be1ee2016-05-01 11:36:10 -0600757 /* more than 28 bits used, use 48bit mode */
Simon Glass8b1b9432023-04-25 10:54:41 -0600758 lba48 = true;
Simon Glasse9be1ee2016-05-01 11:36:10 -0600759 }
Simon Glass8b1b9432023-04-25 10:54:41 -0600760
Simon Glass692bccb2023-04-25 10:54:53 -0600761 log_debug("dev %d start " LBAF ", blocks " LBAF " buffer at %lx\n",
762 device, blknr, blkcnt, (ulong)buffer);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600763
Simon Glasse9be1ee2016-05-01 11:36:10 -0600764 /* Select device
765 */
766 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
767 c = ide_wait(device, IDE_TIME_OUT);
768
769 if (c & ATA_STAT_BUSY) {
770 printf("IDE read: device %d not ready\n", device);
771 goto IDE_READ_E;
772 }
773
774 /* first check if the drive is in Powersaving mode, if yes,
775 * increase the timeout value */
Heinrich Schuchardte6e9a4f2020-02-27 18:28:00 +0100776 ide_outb(device, ATA_COMMAND, ATA_CMD_CHK_POWER);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600777 udelay(50);
778
779 c = ide_wait(device, IDE_TIME_OUT); /* can't take over 500 ms */
780
781 if (c & ATA_STAT_BUSY) {
782 printf("IDE read: device %d not ready\n", device);
783 goto IDE_READ_E;
784 }
785 if ((c & ATA_STAT_ERR) == ATA_STAT_ERR) {
Simon Glass692bccb2023-04-25 10:54:53 -0600786 printf("No Powersaving mode %x\n", c);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600787 } else {
788 c = ide_inb(device, ATA_SECT_CNT);
Simon Glass692bccb2023-04-25 10:54:53 -0600789 log_debug("Powersaving %02X\n", c);
Simon Glass79543e62023-04-25 10:54:54 -0600790 if (!c)
Simon Glasse9be1ee2016-05-01 11:36:10 -0600791 pwrsave = 1;
792 }
793
794
795 while (blkcnt-- > 0) {
796 c = ide_wait(device, IDE_TIME_OUT);
797
798 if (c & ATA_STAT_BUSY) {
799 printf("IDE read: device %d not ready\n", device);
800 break;
801 }
Simon Glass8b1b9432023-04-25 10:54:41 -0600802 if (IS_ENABLED(CONFIG_LBA48) && lba48) {
Simon Glasse9be1ee2016-05-01 11:36:10 -0600803 /* write high bits */
804 ide_outb(device, ATA_SECT_CNT, 0);
Simon Glass79543e62023-04-25 10:54:54 -0600805 ide_outb(device, ATA_LBA_LOW, (blknr >> 24) & 0xff);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600806#ifdef CONFIG_SYS_64BIT_LBA
Simon Glass79543e62023-04-25 10:54:54 -0600807 ide_outb(device, ATA_LBA_MID, (blknr >> 32) & 0xff);
808 ide_outb(device, ATA_LBA_HIGH, (blknr >> 40) & 0xff);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600809#else
810 ide_outb(device, ATA_LBA_MID, 0);
811 ide_outb(device, ATA_LBA_HIGH, 0);
812#endif
813 }
Simon Glasse9be1ee2016-05-01 11:36:10 -0600814 ide_outb(device, ATA_SECT_CNT, 1);
Simon Glass79543e62023-04-25 10:54:54 -0600815 ide_outb(device, ATA_LBA_LOW, (blknr >> 0) & 0xff);
816 ide_outb(device, ATA_LBA_MID, (blknr >> 8) & 0xff);
817 ide_outb(device, ATA_LBA_HIGH, (blknr >> 16) & 0xff);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600818
Simon Glass8b1b9432023-04-25 10:54:41 -0600819 if (IS_ENABLED(CONFIG_LBA48) && lba48) {
Simon Glasse9be1ee2016-05-01 11:36:10 -0600820 ide_outb(device, ATA_DEV_HD,
821 ATA_LBA | ATA_DEVICE(device));
Reinoud Zandijk0a527fd2021-02-24 17:44:42 +0100822 ide_outb(device, ATA_COMMAND, ATA_CMD_PIO_READ_EXT);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600823
Simon Glass8b1b9432023-04-25 10:54:41 -0600824 } else {
Simon Glasse9be1ee2016-05-01 11:36:10 -0600825 ide_outb(device, ATA_DEV_HD, ATA_LBA |
Simon Glass79543e62023-04-25 10:54:54 -0600826 ATA_DEVICE(device) | ((blknr >> 24) & 0xf));
Reinoud Zandijk0a527fd2021-02-24 17:44:42 +0100827 ide_outb(device, ATA_COMMAND, ATA_CMD_PIO_READ);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600828 }
829
830 udelay(50);
831
832 if (pwrsave) {
833 /* may take up to 4 sec */
834 c = ide_wait(device, IDE_SPIN_UP_TIME_OUT);
835 pwrsave = 0;
836 } else {
837 /* can't take over 500 ms */
838 c = ide_wait(device, IDE_TIME_OUT);
839 }
840
841 if ((c & (ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR)) !=
842 ATA_STAT_DRQ) {
843 printf("Error (no IRQ) dev %d blk " LBAF
844 ": status %#02x\n", device, blknr, c);
845 break;
846 }
847
848 ide_input_data(device, buffer, ATA_SECTORWORDS);
849 (void) ide_inb(device, ATA_STATUS); /* clear IRQ */
850
851 ++n;
852 ++blknr;
853 buffer += ATA_BLOCKSIZE;
854 }
855IDE_READ_E:
Simon Glasse9be1ee2016-05-01 11:36:10 -0600856 return n;
857}
858
Simon Glass1b33fd82023-04-25 10:54:36 -0600859static ulong ide_write(struct udevice *dev, lbaint_t blknr, lbaint_t blkcnt,
860 const void *buffer)
Simon Glasse9be1ee2016-05-01 11:36:10 -0600861{
Simon Glassce99e292023-04-25 10:54:47 -0600862 struct blk_desc *desc = dev_get_uclass_plat(dev);
863 int device = desc->devnum;
Simon Glasse9be1ee2016-05-01 11:36:10 -0600864 ulong n = 0;
Simon Glass8b1b9432023-04-25 10:54:41 -0600865 bool lba48 = false;
Simon Glass22a7ae32023-04-25 10:54:55 -0600866 u8 c;
Simon Glasse9be1ee2016-05-01 11:36:10 -0600867
Simon Glass8b1b9432023-04-25 10:54:41 -0600868 if (IS_ENABLED(CONFIG_LBA48) && (blknr & 0x0000fffff0000000ULL)) {
Simon Glasse9be1ee2016-05-01 11:36:10 -0600869 /* more than 28 bits used, use 48bit mode */
Simon Glass8b1b9432023-04-25 10:54:41 -0600870 lba48 = true;
Simon Glasse9be1ee2016-05-01 11:36:10 -0600871 }
Simon Glasse9be1ee2016-05-01 11:36:10 -0600872
Simon Glasse9be1ee2016-05-01 11:36:10 -0600873 /* Select device
874 */
875 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
876
877 while (blkcnt-- > 0) {
878 c = ide_wait(device, IDE_TIME_OUT);
879
880 if (c & ATA_STAT_BUSY) {
881 printf("IDE read: device %d not ready\n", device);
882 goto WR_OUT;
883 }
Simon Glass8b1b9432023-04-25 10:54:41 -0600884 if (IS_ENABLED(CONFIG_LBA48) && lba48) {
Simon Glasse9be1ee2016-05-01 11:36:10 -0600885 /* write high bits */
886 ide_outb(device, ATA_SECT_CNT, 0);
Simon Glass79543e62023-04-25 10:54:54 -0600887 ide_outb(device, ATA_LBA_LOW, (blknr >> 24) & 0xff);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600888#ifdef CONFIG_SYS_64BIT_LBA
Simon Glass79543e62023-04-25 10:54:54 -0600889 ide_outb(device, ATA_LBA_MID, (blknr >> 32) & 0xff);
890 ide_outb(device, ATA_LBA_HIGH, (blknr >> 40) & 0xff);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600891#else
892 ide_outb(device, ATA_LBA_MID, 0);
893 ide_outb(device, ATA_LBA_HIGH, 0);
894#endif
895 }
Simon Glasse9be1ee2016-05-01 11:36:10 -0600896 ide_outb(device, ATA_SECT_CNT, 1);
Simon Glass79543e62023-04-25 10:54:54 -0600897 ide_outb(device, ATA_LBA_LOW, (blknr >> 0) & 0xff);
898 ide_outb(device, ATA_LBA_MID, (blknr >> 8) & 0xff);
899 ide_outb(device, ATA_LBA_HIGH, (blknr >> 16) & 0xff);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600900
Simon Glass8b1b9432023-04-25 10:54:41 -0600901 if (IS_ENABLED(CONFIG_LBA48) && lba48) {
Simon Glasse9be1ee2016-05-01 11:36:10 -0600902 ide_outb(device, ATA_DEV_HD,
903 ATA_LBA | ATA_DEVICE(device));
Reinoud Zandijk0a527fd2021-02-24 17:44:42 +0100904 ide_outb(device, ATA_COMMAND, ATA_CMD_PIO_WRITE_EXT);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600905
Simon Glass8b1b9432023-04-25 10:54:41 -0600906 } else {
Simon Glasse9be1ee2016-05-01 11:36:10 -0600907 ide_outb(device, ATA_DEV_HD, ATA_LBA |
Simon Glass79543e62023-04-25 10:54:54 -0600908 ATA_DEVICE(device) | ((blknr >> 24) & 0xf));
Reinoud Zandijk0a527fd2021-02-24 17:44:42 +0100909 ide_outb(device, ATA_COMMAND, ATA_CMD_PIO_WRITE);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600910 }
911
912 udelay(50);
913
914 /* can't take over 500 ms */
915 c = ide_wait(device, IDE_TIME_OUT);
916
917 if ((c & (ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR)) !=
918 ATA_STAT_DRQ) {
919 printf("Error (no IRQ) dev %d blk " LBAF
920 ": status %#02x\n", device, blknr, c);
921 goto WR_OUT;
922 }
923
924 ide_output_data(device, buffer, ATA_SECTORWORDS);
925 c = ide_inb(device, ATA_STATUS); /* clear IRQ */
926 ++n;
927 ++blknr;
928 buffer += ATA_BLOCKSIZE;
929 }
930WR_OUT:
Simon Glasse9be1ee2016-05-01 11:36:10 -0600931 return n;
932}
933
Simon Glass646deed2023-04-25 10:54:35 -0600934ulong ide_or_atapi_read(struct udevice *dev, lbaint_t blknr, lbaint_t blkcnt,
935 void *buffer)
936{
937 struct blk_desc *desc = dev_get_uclass_plat(dev);
938
939 if (IS_ENABLED(CONFIG_ATAPI) && desc->atapi)
940 return atapi_read(dev, blknr, blkcnt, buffer);
941
942 return ide_read(dev, blknr, blkcnt, buffer);
943}
944
Simon Glass145df842016-05-01 11:36:22 -0600945static const struct blk_ops ide_blk_ops = {
Simon Glass646deed2023-04-25 10:54:35 -0600946 .read = ide_or_atapi_read,
Simon Glass145df842016-05-01 11:36:22 -0600947 .write = ide_write,
948};
949
950U_BOOT_DRIVER(ide_blk) = {
951 .name = "ide_blk",
952 .id = UCLASS_BLK,
953 .ops = &ide_blk_ops,
Bin Meng68e6f222017-09-10 05:12:51 -0700954};
955
Simon Glass0d77f8f2023-01-17 10:47:46 -0700956static int ide_bootdev_bind(struct udevice *dev)
957{
958 struct bootdev_uc_plat *ucp = dev_get_uclass_plat(dev);
959
Simon Glasseacc2612023-01-17 10:48:08 -0700960 ucp->prio = BOOTDEVP_5_SCAN_SLOW;
Simon Glass0d77f8f2023-01-17 10:47:46 -0700961
962 return 0;
963}
964
965static int ide_bootdev_hunt(struct bootdev_hunter *info, bool show)
966{
Simon Glass80778f52023-04-25 10:54:30 -0600967 struct udevice *dev;
968
969 uclass_first_device(UCLASS_IDE, &dev);
Simon Glass0d77f8f2023-01-17 10:47:46 -0700970
971 return 0;
972}
973
974struct bootdev_ops ide_bootdev_ops = {
975};
976
977static const struct udevice_id ide_bootdev_ids[] = {
978 { .compatible = "u-boot,bootdev-ide" },
979 { }
980};
981
982U_BOOT_DRIVER(ide_bootdev) = {
983 .name = "ide_bootdev",
984 .id = UCLASS_BOOTDEV,
985 .ops = &ide_bootdev_ops,
986 .bind = ide_bootdev_bind,
987 .of_match = ide_bootdev_ids,
988};
989
990BOOTDEV_HUNTER(ide_bootdev_hunter) = {
Simon Glasseacc2612023-01-17 10:48:08 -0700991 .prio = BOOTDEVP_5_SCAN_SLOW,
Simon Glass0d77f8f2023-01-17 10:47:46 -0700992 .uclass = UCLASS_IDE,
993 .hunt = ide_bootdev_hunt,
994 .drv = DM_DRIVER_REF(ide_bootdev),
995};
996
Bin Meng68e6f222017-09-10 05:12:51 -0700997static int ide_probe(struct udevice *udev)
998{
Simon Glass708404c2023-04-25 10:54:45 -0600999 bool bus_ok[CONFIG_SYS_IDE_MAXBUS];
1000 int i, bus;
Bin Meng68e6f222017-09-10 05:12:51 -07001001
Simon Glass708404c2023-04-25 10:54:45 -06001002 schedule();
1003
1004 /* ATAPI Drives seems to need a proper IDE Reset */
1005 ide_reset();
1006
1007 /*
1008 * Wait for IDE to get ready.
1009 * According to spec, this can take up to 31 seconds!
1010 */
1011 for (bus = 0; bus < CONFIG_SYS_IDE_MAXBUS; ++bus) {
1012 bus_ok[bus] = !ide_init_one(bus);
1013 schedule();
1014 }
1015
1016 putc('\n');
1017
Simon Glassf0af25a2023-04-25 10:54:46 -06001018 schedule();
1019
1020 for (i = 0; i < CONFIG_SYS_IDE_MAXDEVICE; i++) {
Simon Glassd7d57432023-04-25 10:54:50 -06001021 struct blk_desc *desc, pdesc;
Simon Glass038590a2023-04-25 10:54:48 -06001022 struct udevice *blk;
Simon Glass038590a2023-04-25 10:54:48 -06001023 char name[20];
Simon Glass038590a2023-04-25 10:54:48 -06001024 int ret;
1025
Simon Glassf0af25a2023-04-25 10:54:46 -06001026 if (!bus_ok[IDE_BUS(i)])
1027 continue;
1028
Simon Glassd7d57432023-04-25 10:54:50 -06001029 ret = ide_ident(i, &pdesc);
1030 dev_print(&pdesc);
Simon Glass80778f52023-04-25 10:54:30 -06001031
Simon Glass038590a2023-04-25 10:54:48 -06001032 if (ret)
1033 continue;
Simon Glassdb89e722023-04-25 10:54:44 -06001034
Simon Glass038590a2023-04-25 10:54:48 -06001035 sprintf(name, "blk#%d", i);
Bin Meng68e6f222017-09-10 05:12:51 -07001036
Simon Glass038590a2023-04-25 10:54:48 -06001037 /*
1038 * With CDROM, if there is no CD inserted, blksz will
1039 * be zero, don't bother to create IDE block device.
1040 */
Simon Glass49aa7782023-04-25 10:54:51 -06001041 if (!pdesc.blksz)
Simon Glass038590a2023-04-25 10:54:48 -06001042 continue;
1043 ret = blk_create_devicef(udev, "ide_blk", name, UCLASS_IDE, i,
Simon Glass49aa7782023-04-25 10:54:51 -06001044 pdesc.blksz, pdesc.lba, &blk);
Simon Glass038590a2023-04-25 10:54:48 -06001045 if (ret)
1046 return ret;
AKASHI Takahiro4c73b032022-03-08 20:36:44 +09001047
Simon Glass038590a2023-04-25 10:54:48 -06001048 ret = blk_probe_or_unbind(blk);
1049 if (ret)
1050 return ret;
Simon Glass0d77f8f2023-01-17 10:47:46 -07001051
Simon Glass038590a2023-04-25 10:54:48 -06001052 /* fill in device vendor/product/rev strings */
1053 desc = dev_get_uclass_plat(blk);
Simon Glassd7d57432023-04-25 10:54:50 -06001054 strlcpy(desc->vendor, pdesc.vendor, BLK_VEN_SIZE);
1055 strlcpy(desc->product, pdesc.product, BLK_PRD_SIZE);
1056 strlcpy(desc->revision, pdesc.revision, BLK_REV_SIZE);
1057 desc->removable = pdesc.removable;
1058 desc->atapi = pdesc.atapi;
1059 desc->lba48 = pdesc.lba48;
1060 desc->type = pdesc.type;
Simon Glass708404c2023-04-25 10:54:45 -06001061
Simon Glass2d5b5a92023-07-30 11:15:15 -06001062 ret = bootdev_setup_for_sibling_blk(blk, "ide_bootdev");
Simon Glass038590a2023-04-25 10:54:48 -06001063 if (ret)
Simon Glass2d5b5a92023-07-30 11:15:15 -06001064 return log_msg_ret("bd", ret);
Bin Meng68e6f222017-09-10 05:12:51 -07001065 }
1066
1067 return 0;
1068}
1069
1070U_BOOT_DRIVER(ide) = {
1071 .name = "ide",
1072 .id = UCLASS_IDE,
1073 .probe = ide_probe,
1074};
1075
1076struct pci_device_id ide_supported[] = {
1077 { PCI_DEVICE_CLASS(PCI_CLASS_STORAGE_IDE << 8, 0xffff00) },
1078 { }
1079};
1080
1081U_BOOT_PCI_DEVICE(ide, ide_supported);
1082
1083UCLASS_DRIVER(ide) = {
1084 .name = "ide",
1085 .id = UCLASS_IDE,
Simon Glass145df842016-05-01 11:36:22 -06001086};