blob: 6f601bcf864664d6a47c9134ae2c75e9f111230d [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 -060042static int ide_bus_ok[CONFIG_SYS_IDE_MAXBUS];
43
44struct blk_desc ide_dev_desc[CONFIG_SYS_IDE_MAXDEVICE];
45
46#define IDE_TIME_OUT 2000 /* 2 sec timeout */
47
48#define ATAPI_TIME_OUT 7000 /* 7 sec timeout (5 sec seems to work...) */
49
50#define IDE_SPIN_UP_TIME_OUT 5000 /* 5 sec spin-up timeout */
51
Simon Glasse9be1ee2016-05-01 11:36:10 -060052#ifdef CONFIG_IDE_RESET
53extern void ide_set_reset(int idereset);
54
55static void ide_reset(void)
56{
57 int i;
58
59 for (i = 0; i < CONFIG_SYS_IDE_MAXBUS; ++i)
60 ide_bus_ok[i] = 0;
61 for (i = 0; i < CONFIG_SYS_IDE_MAXDEVICE; ++i)
62 ide_dev_desc[i].type = DEV_TYPE_UNKNOWN;
63
64 ide_set_reset(1); /* assert reset */
65
66 /* the reset signal shall be asserted for et least 25 us */
67 udelay(25);
68
Stefan Roese29caf932022-09-02 14:10:46 +020069 schedule();
Simon Glasse9be1ee2016-05-01 11:36:10 -060070
71 /* de-assert RESET signal */
72 ide_set_reset(0);
73
Simon Glass4d89f4b2023-04-25 10:54:27 -060074 mdelay(250);
Simon Glasse9be1ee2016-05-01 11:36:10 -060075}
76#else
77#define ide_reset() /* dummy */
78#endif /* CONFIG_IDE_RESET */
79
80/*
81 * Wait until Busy bit is off, or timeout (in ms)
82 * Return last status
83 */
84static uchar ide_wait(int dev, ulong t)
85{
86 ulong delay = 10 * t; /* poll every 100 us */
87 uchar c;
88
89 while ((c = ide_inb(dev, ATA_STATUS)) & ATA_STAT_BUSY) {
90 udelay(100);
91 if (delay-- == 0)
92 break;
93 }
94 return c;
95}
96
97/*
98 * copy src to dest, skipping leading and trailing blanks and null
99 * terminate the string
100 * "len" is the size of available memory including the terminating '\0'
101 */
102static void ident_cpy(unsigned char *dst, unsigned char *src,
103 unsigned int len)
104{
105 unsigned char *end, *last;
106
107 last = dst;
108 end = src + len - 1;
109
110 /* reserve space for '\0' */
111 if (len < 2)
112 goto OUT;
113
114 /* skip leading white space */
115 while ((*src) && (src < end) && (*src == ' '))
116 ++src;
117
118 /* copy string, omitting trailing white space */
119 while ((*src) && (src < end)) {
120 *dst++ = *src;
121 if (*src++ != ' ')
122 last = dst;
123 }
124OUT:
125 *last = '\0';
126}
127
128#ifdef CONFIG_ATAPI
129/****************************************************************************
130 * ATAPI Support
131 */
132
Simon Glasse9be1ee2016-05-01 11:36:10 -0600133/* since ATAPI may use commands with not 4 bytes alligned length
134 * we have our own transfer functions, 2 bytes alligned */
135__weak void ide_output_data_shorts(int dev, ushort *sect_buf, int shorts)
136{
Reinoud Zandijk0a527fd2021-02-24 17:44:42 +0100137 uintptr_t paddr = (ATA_CURR_BASE(dev) + ATA_DATA_REG);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600138 ushort *dbuf;
Simon Glasse9be1ee2016-05-01 11:36:10 -0600139
Simon Glasse9be1ee2016-05-01 11:36:10 -0600140 dbuf = (ushort *)sect_buf;
141
Reinoud Zandijk0a527fd2021-02-24 17:44:42 +0100142 debug("in output data shorts base for read is %p\n", (void *)paddr);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600143
144 while (shorts--) {
145 EIEIO;
Reinoud Zandijk0a527fd2021-02-24 17:44:42 +0100146 outw(cpu_to_le16(*dbuf++), paddr);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600147 }
148}
149
150__weak void ide_input_data_shorts(int dev, ushort *sect_buf, int shorts)
151{
Reinoud Zandijk0a527fd2021-02-24 17:44:42 +0100152 uintptr_t paddr = (ATA_CURR_BASE(dev) + ATA_DATA_REG);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600153 ushort *dbuf;
Simon Glasse9be1ee2016-05-01 11:36:10 -0600154
Simon Glasse9be1ee2016-05-01 11:36:10 -0600155 dbuf = (ushort *)sect_buf;
156
Reinoud Zandijk0a527fd2021-02-24 17:44:42 +0100157 debug("in input data shorts base for read is %p\n", (void *)paddr);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600158
159 while (shorts--) {
160 EIEIO;
Reinoud Zandijk0a527fd2021-02-24 17:44:42 +0100161 *dbuf++ = le16_to_cpu(inw(paddr));
Simon Glasse9be1ee2016-05-01 11:36:10 -0600162 }
163}
164
Simon Glasse9be1ee2016-05-01 11:36:10 -0600165/*
166 * Wait until (Status & mask) == res, or timeout (in ms)
167 * Return last status
168 * This is used since some ATAPI CD ROMs clears their Busy Bit first
169 * and then they set their DRQ Bit
170 */
171static uchar atapi_wait_mask(int dev, ulong t, uchar mask, uchar res)
172{
173 ulong delay = 10 * t; /* poll every 100 us */
174 uchar c;
175
176 /* prevents to read the status before valid */
177 c = ide_inb(dev, ATA_DEV_CTL);
178
179 while (((c = ide_inb(dev, ATA_STATUS)) & mask) != res) {
180 /* break if error occurs (doesn't make sense to wait more) */
181 if ((c & ATA_STAT_ERR) == ATA_STAT_ERR)
182 break;
183 udelay(100);
184 if (delay-- == 0)
185 break;
186 }
187 return c;
188}
189
190/*
191 * issue an atapi command
192 */
193unsigned char atapi_issue(int device, unsigned char *ccb, int ccblen,
194 unsigned char *buffer, int buflen)
195{
196 unsigned char c, err, mask, res;
197 int n;
198
Simon Glasse9be1ee2016-05-01 11:36:10 -0600199 /* Select device
200 */
201 mask = ATA_STAT_BUSY | ATA_STAT_DRQ;
202 res = 0;
203 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
204 c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
205 if ((c & mask) != res) {
206 printf("ATAPI_ISSUE: device %d not ready status %X\n", device,
207 c);
208 err = 0xFF;
209 goto AI_OUT;
210 }
211 /* write taskfile */
212 ide_outb(device, ATA_ERROR_REG, 0); /* no DMA, no overlaped */
213 ide_outb(device, ATA_SECT_CNT, 0);
214 ide_outb(device, ATA_SECT_NUM, 0);
215 ide_outb(device, ATA_CYL_LOW, (unsigned char) (buflen & 0xFF));
216 ide_outb(device, ATA_CYL_HIGH,
217 (unsigned char) ((buflen >> 8) & 0xFF));
218 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
219
Heinrich Schuchardte6e9a4f2020-02-27 18:28:00 +0100220 ide_outb(device, ATA_COMMAND, ATA_CMD_PACKET);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600221 udelay(50);
222
223 mask = ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR;
224 res = ATA_STAT_DRQ;
225 c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
226
227 if ((c & mask) != res) { /* DRQ must be 1, BSY 0 */
228 printf("ATAPI_ISSUE: Error (no IRQ) before sending ccb dev %d status 0x%02x\n",
229 device, c);
230 err = 0xFF;
231 goto AI_OUT;
232 }
233
234 /* write command block */
235 ide_output_data_shorts(device, (unsigned short *)ccb, ccblen / 2);
236
237 /* ATAPI Command written wait for completition */
Simon Glass4d89f4b2023-04-25 10:54:27 -0600238 mdelay(5); /* device must set bsy */
Simon Glasse9be1ee2016-05-01 11:36:10 -0600239
240 mask = ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR;
241 /*
242 * if no data wait for DRQ = 0 BSY = 0
243 * if data wait for DRQ = 1 BSY = 0
244 */
245 res = 0;
246 if (buflen)
247 res = ATA_STAT_DRQ;
248 c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
249 if ((c & mask) != res) {
250 if (c & ATA_STAT_ERR) {
251 err = (ide_inb(device, ATA_ERROR_REG)) >> 4;
252 debug("atapi_issue 1 returned sense key %X status %02X\n",
253 err, c);
254 } else {
255 printf("ATAPI_ISSUE: (no DRQ) after sending ccb (%x) status 0x%02x\n",
256 ccb[0], c);
257 err = 0xFF;
258 }
259 goto AI_OUT;
260 }
261 n = ide_inb(device, ATA_CYL_HIGH);
262 n <<= 8;
263 n += ide_inb(device, ATA_CYL_LOW);
264 if (n > buflen) {
265 printf("ERROR, transfer bytes %d requested only %d\n", n,
266 buflen);
267 err = 0xff;
268 goto AI_OUT;
269 }
270 if ((n == 0) && (buflen < 0)) {
271 printf("ERROR, transfer bytes %d requested %d\n", n, buflen);
272 err = 0xff;
273 goto AI_OUT;
274 }
275 if (n != buflen) {
276 debug("WARNING, transfer bytes %d not equal with requested %d\n",
277 n, buflen);
278 }
279 if (n != 0) { /* data transfer */
280 debug("ATAPI_ISSUE: %d Bytes to transfer\n", n);
281 /* we transfer shorts */
282 n >>= 1;
283 /* ok now decide if it is an in or output */
284 if ((ide_inb(device, ATA_SECT_CNT) & 0x02) == 0) {
285 debug("Write to device\n");
286 ide_output_data_shorts(device, (unsigned short *)buffer,
287 n);
288 } else {
289 debug("Read from device @ %p shorts %d\n", buffer, n);
290 ide_input_data_shorts(device, (unsigned short *)buffer,
291 n);
292 }
293 }
Simon Glass4d89f4b2023-04-25 10:54:27 -0600294 mdelay(5); /* seems that some CD ROMs need this... */
Simon Glasse9be1ee2016-05-01 11:36:10 -0600295 mask = ATA_STAT_BUSY | ATA_STAT_ERR;
296 res = 0;
297 c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
298 if ((c & ATA_STAT_ERR) == ATA_STAT_ERR) {
299 err = (ide_inb(device, ATA_ERROR_REG) >> 4);
300 debug("atapi_issue 2 returned sense key %X status %X\n", err,
301 c);
302 } else {
303 err = 0;
304 }
305AI_OUT:
Simon Glasse9be1ee2016-05-01 11:36:10 -0600306 return err;
307}
308
309/*
310 * sending the command to atapi_issue. If an status other than good
311 * returns, an request_sense will be issued
312 */
313
314#define ATAPI_DRIVE_NOT_READY 100
315#define ATAPI_UNIT_ATTN 10
316
317unsigned char atapi_issue_autoreq(int device,
318 unsigned char *ccb,
319 int ccblen,
320 unsigned char *buffer, int buflen)
321{
322 unsigned char sense_data[18], sense_ccb[12];
323 unsigned char res, key, asc, ascq;
324 int notready, unitattn;
325
326 unitattn = ATAPI_UNIT_ATTN;
327 notready = ATAPI_DRIVE_NOT_READY;
328
329retry:
330 res = atapi_issue(device, ccb, ccblen, buffer, buflen);
331 if (res == 0)
332 return 0; /* Ok */
333
334 if (res == 0xFF)
335 return 0xFF; /* error */
336
337 debug("(auto_req)atapi_issue returned sense key %X\n", res);
338
339 memset(sense_ccb, 0, sizeof(sense_ccb));
340 memset(sense_data, 0, sizeof(sense_data));
341 sense_ccb[0] = ATAPI_CMD_REQ_SENSE;
342 sense_ccb[4] = 18; /* allocation Length */
343
344 res = atapi_issue(device, sense_ccb, 12, sense_data, 18);
345 key = (sense_data[2] & 0xF);
346 asc = (sense_data[12]);
347 ascq = (sense_data[13]);
348
349 debug("ATAPI_CMD_REQ_SENSE returned %x\n", res);
350 debug(" Sense page: %02X key %02X ASC %02X ASCQ %02X\n",
351 sense_data[0], key, asc, ascq);
352
353 if ((key == 0))
354 return 0; /* ok device ready */
355
356 if ((key == 6) || (asc == 0x29) || (asc == 0x28)) { /* Unit Attention */
357 if (unitattn-- > 0) {
Simon Glass4d89f4b2023-04-25 10:54:27 -0600358 mdelay(200);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600359 goto retry;
360 }
361 printf("Unit Attention, tried %d\n", ATAPI_UNIT_ATTN);
362 goto error;
363 }
364 if ((asc == 0x4) && (ascq == 0x1)) {
365 /* not ready, but will be ready soon */
366 if (notready-- > 0) {
Simon Glass4d89f4b2023-04-25 10:54:27 -0600367 mdelay(200);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600368 goto retry;
369 }
370 printf("Drive not ready, tried %d times\n",
371 ATAPI_DRIVE_NOT_READY);
372 goto error;
373 }
374 if (asc == 0x3a) {
375 debug("Media not present\n");
376 goto error;
377 }
378
379 printf("ERROR: Unknown Sense key %02X ASC %02X ASCQ %02X\n", key, asc,
380 ascq);
381error:
382 debug("ERROR Sense key %02X ASC %02X ASCQ %02X\n", key, asc, ascq);
383 return 0xFF;
384}
385
386/*
387 * atapi_read:
388 * we transfer only one block per command, since the multiple DRQ per
389 * command is not yet implemented
390 */
391#define ATAPI_READ_MAX_BYTES 2048 /* we read max 2kbytes */
392#define ATAPI_READ_BLOCK_SIZE 2048 /* assuming CD part */
393#define ATAPI_READ_MAX_BLOCK (ATAPI_READ_MAX_BYTES/ATAPI_READ_BLOCK_SIZE)
394
395ulong atapi_read(struct blk_desc *block_dev, lbaint_t blknr, lbaint_t blkcnt,
396 void *buffer)
397{
398 int device = block_dev->devnum;
399 ulong n = 0;
400 unsigned char ccb[12]; /* Command descriptor block */
401 ulong cnt;
402
403 debug("atapi_read dev %d start " LBAF " blocks " LBAF
404 " buffer at %lX\n", device, blknr, blkcnt, (ulong) buffer);
405
406 do {
407 if (blkcnt > ATAPI_READ_MAX_BLOCK)
408 cnt = ATAPI_READ_MAX_BLOCK;
409 else
410 cnt = blkcnt;
411
412 ccb[0] = ATAPI_CMD_READ_12;
413 ccb[1] = 0; /* reserved */
414 ccb[2] = (unsigned char) (blknr >> 24) & 0xFF; /* MSB Block */
415 ccb[3] = (unsigned char) (blknr >> 16) & 0xFF; /* */
416 ccb[4] = (unsigned char) (blknr >> 8) & 0xFF;
417 ccb[5] = (unsigned char) blknr & 0xFF; /* LSB Block */
418 ccb[6] = (unsigned char) (cnt >> 24) & 0xFF; /* MSB Block cnt */
419 ccb[7] = (unsigned char) (cnt >> 16) & 0xFF;
420 ccb[8] = (unsigned char) (cnt >> 8) & 0xFF;
421 ccb[9] = (unsigned char) cnt & 0xFF; /* LSB Block */
422 ccb[10] = 0; /* reserved */
423 ccb[11] = 0; /* reserved */
424
425 if (atapi_issue_autoreq(device, ccb, 12,
426 (unsigned char *)buffer,
427 cnt * ATAPI_READ_BLOCK_SIZE)
428 == 0xFF) {
429 return n;
430 }
431 n += cnt;
432 blkcnt -= cnt;
433 blknr += cnt;
434 buffer += (cnt * ATAPI_READ_BLOCK_SIZE);
435 } while (blkcnt > 0);
436 return n;
437}
438
439static void atapi_inquiry(struct blk_desc *dev_desc)
440{
441 unsigned char ccb[12]; /* Command descriptor block */
442 unsigned char iobuf[64]; /* temp buf */
443 unsigned char c;
444 int device;
445
446 device = dev_desc->devnum;
447 dev_desc->type = DEV_TYPE_UNKNOWN; /* not yet valid */
Simon Glasse9be1ee2016-05-01 11:36:10 -0600448
449 memset(ccb, 0, sizeof(ccb));
450 memset(iobuf, 0, sizeof(iobuf));
451
452 ccb[0] = ATAPI_CMD_INQUIRY;
453 ccb[4] = 40; /* allocation Legnth */
454 c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *)iobuf, 40);
455
456 debug("ATAPI_CMD_INQUIRY returned %x\n", c);
457 if (c != 0)
458 return;
459
460 /* copy device ident strings */
461 ident_cpy((unsigned char *)dev_desc->vendor, &iobuf[8], 8);
462 ident_cpy((unsigned char *)dev_desc->product, &iobuf[16], 16);
463 ident_cpy((unsigned char *)dev_desc->revision, &iobuf[32], 5);
464
465 dev_desc->lun = 0;
466 dev_desc->lba = 0;
467 dev_desc->blksz = 0;
468 dev_desc->log2blksz = LOG2_INVALID(typeof(dev_desc->log2blksz));
469 dev_desc->type = iobuf[0] & 0x1f;
470
471 if ((iobuf[1] & 0x80) == 0x80)
472 dev_desc->removable = 1;
473 else
474 dev_desc->removable = 0;
475
476 memset(ccb, 0, sizeof(ccb));
477 memset(iobuf, 0, sizeof(iobuf));
478 ccb[0] = ATAPI_CMD_START_STOP;
479 ccb[4] = 0x03; /* start */
480
481 c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *)iobuf, 0);
482
483 debug("ATAPI_CMD_START_STOP returned %x\n", c);
484 if (c != 0)
485 return;
486
487 memset(ccb, 0, sizeof(ccb));
488 memset(iobuf, 0, sizeof(iobuf));
489 c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *)iobuf, 0);
490
491 debug("ATAPI_CMD_UNIT_TEST_READY returned %x\n", c);
492 if (c != 0)
493 return;
494
495 memset(ccb, 0, sizeof(ccb));
496 memset(iobuf, 0, sizeof(iobuf));
497 ccb[0] = ATAPI_CMD_READ_CAP;
498 c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *)iobuf, 8);
499 debug("ATAPI_CMD_READ_CAP returned %x\n", c);
500 if (c != 0)
501 return;
502
503 debug("Read Cap: LBA %02X%02X%02X%02X blksize %02X%02X%02X%02X\n",
504 iobuf[0], iobuf[1], iobuf[2], iobuf[3],
505 iobuf[4], iobuf[5], iobuf[6], iobuf[7]);
506
507 dev_desc->lba = ((unsigned long) iobuf[0] << 24) +
508 ((unsigned long) iobuf[1] << 16) +
509 ((unsigned long) iobuf[2] << 8) + ((unsigned long) iobuf[3]);
510 dev_desc->blksz = ((unsigned long) iobuf[4] << 24) +
511 ((unsigned long) iobuf[5] << 16) +
512 ((unsigned long) iobuf[6] << 8) + ((unsigned long) iobuf[7]);
513 dev_desc->log2blksz = LOG2(dev_desc->blksz);
514#ifdef CONFIG_LBA48
515 /* ATAPI devices cannot use 48bit addressing (ATA/ATAPI v7) */
516 dev_desc->lba48 = 0;
517#endif
518 return;
519}
520
521#endif /* CONFIG_ATAPI */
522
523static void ide_ident(struct blk_desc *dev_desc)
524{
525 unsigned char c;
526 hd_driveid_t iop;
Simon Glasse9be1ee2016-05-01 11:36:10 -0600527#ifdef CONFIG_ATAPI
Simon Glass606e0542022-08-11 19:34:53 -0600528 bool is_atapi = false;
Simon Glasse9be1ee2016-05-01 11:36:10 -0600529 int retries = 0;
530#endif
531 int device;
532
533 device = dev_desc->devnum;
534 printf(" Device %d: ", device);
535
Simon Glasse9be1ee2016-05-01 11:36:10 -0600536 /* Select device
537 */
538 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
Simon Glass8149b152022-09-17 09:00:09 -0600539 dev_desc->uclass_id = UCLASS_IDE;
Simon Glasse9be1ee2016-05-01 11:36:10 -0600540#ifdef CONFIG_ATAPI
541
542 retries = 0;
543
544 /* Warning: This will be tricky to read */
545 while (retries <= 1) {
546 /* check signature */
547 if ((ide_inb(device, ATA_SECT_CNT) == 0x01) &&
548 (ide_inb(device, ATA_SECT_NUM) == 0x01) &&
549 (ide_inb(device, ATA_CYL_LOW) == 0x14) &&
550 (ide_inb(device, ATA_CYL_HIGH) == 0xEB)) {
551 /* ATAPI Signature found */
Simon Glass606e0542022-08-11 19:34:53 -0600552 is_atapi = true;
Simon Glasse9be1ee2016-05-01 11:36:10 -0600553 /*
554 * Start Ident Command
555 */
Heinrich Schuchardte6e9a4f2020-02-27 18:28:00 +0100556 ide_outb(device, ATA_COMMAND, ATA_CMD_ID_ATAPI);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600557 /*
558 * Wait for completion - ATAPI devices need more time
559 * to become ready
560 */
561 c = ide_wait(device, ATAPI_TIME_OUT);
562 } else
563#endif
564 {
565 /*
566 * Start Ident Command
567 */
Heinrich Schuchardte6e9a4f2020-02-27 18:28:00 +0100568 ide_outb(device, ATA_COMMAND, ATA_CMD_ID_ATA);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600569
570 /*
571 * Wait for completion
572 */
573 c = ide_wait(device, IDE_TIME_OUT);
574 }
Simon Glasse9be1ee2016-05-01 11:36:10 -0600575
576 if (((c & ATA_STAT_DRQ) == 0) ||
577 ((c & (ATA_STAT_FAULT | ATA_STAT_ERR)) != 0)) {
578#ifdef CONFIG_ATAPI
579 {
580 /*
581 * Need to soft reset the device
582 * in case it's an ATAPI...
583 */
584 debug("Retrying...\n");
585 ide_outb(device, ATA_DEV_HD,
586 ATA_LBA | ATA_DEVICE(device));
Simon Glass4d89f4b2023-04-25 10:54:27 -0600587 mdelay(100);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600588 ide_outb(device, ATA_COMMAND, 0x08);
Simon Glass4d89f4b2023-04-25 10:54:27 -0600589 mdelay(500);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600590 }
591 /*
592 * Select device
593 */
594 ide_outb(device, ATA_DEV_HD,
595 ATA_LBA | ATA_DEVICE(device));
596 retries++;
597#else
598 return;
599#endif
600 }
601#ifdef CONFIG_ATAPI
602 else
603 break;
604 } /* see above - ugly to read */
605
606 if (retries == 2) /* Not found */
607 return;
608#endif
609
610 ide_input_swap_data(device, (ulong *)&iop, ATA_SECTORWORDS);
611
612 ident_cpy((unsigned char *)dev_desc->revision, iop.fw_rev,
613 sizeof(dev_desc->revision));
614 ident_cpy((unsigned char *)dev_desc->vendor, iop.model,
615 sizeof(dev_desc->vendor));
616 ident_cpy((unsigned char *)dev_desc->product, iop.serial_no,
617 sizeof(dev_desc->product));
Simon Glasse9be1ee2016-05-01 11:36:10 -0600618
619 if ((iop.config & 0x0080) == 0x0080)
620 dev_desc->removable = 1;
621 else
622 dev_desc->removable = 0;
623
624#ifdef CONFIG_ATAPI
Simon Glass606e0542022-08-11 19:34:53 -0600625 if (is_atapi) {
Simon Glasse9be1ee2016-05-01 11:36:10 -0600626 atapi_inquiry(dev_desc);
627 return;
628 }
629#endif /* CONFIG_ATAPI */
630
Reinoud Zandijk0a527fd2021-02-24 17:44:42 +0100631 iop.lba_capacity[0] = be16_to_cpu(iop.lba_capacity[0]);
632 iop.lba_capacity[1] = be16_to_cpu(iop.lba_capacity[1]);
633 dev_desc->lba =
634 ((unsigned long)iop.lba_capacity[0]) |
635 ((unsigned long)iop.lba_capacity[1] << 16);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600636
637#ifdef CONFIG_LBA48
638 if (iop.command_set_2 & 0x0400) { /* LBA 48 support */
639 dev_desc->lba48 = 1;
Reinoud Zandijk0a527fd2021-02-24 17:44:42 +0100640 for (int i = 0; i < 4; i++)
641 iop.lba48_capacity[i] = be16_to_cpu(iop.lba48_capacity[i]);
642 dev_desc->lba =
643 ((unsigned long long)iop.lba48_capacity[0] |
644 ((unsigned long long)iop.lba48_capacity[1] << 16) |
645 ((unsigned long long)iop.lba48_capacity[2] << 32) |
646 ((unsigned long long)iop.lba48_capacity[3] << 48));
Simon Glasse9be1ee2016-05-01 11:36:10 -0600647 } else {
648 dev_desc->lba48 = 0;
649 }
650#endif /* CONFIG_LBA48 */
651 /* assuming HD */
652 dev_desc->type = DEV_TYPE_HARDDISK;
653 dev_desc->blksz = ATA_BLOCKSIZE;
654 dev_desc->log2blksz = LOG2(dev_desc->blksz);
655 dev_desc->lun = 0; /* just to fill something in... */
656
657#if 0 /* only used to test the powersaving mode,
658 * if enabled, the drive goes after 5 sec
659 * in standby mode */
660 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
661 c = ide_wait(device, IDE_TIME_OUT);
662 ide_outb(device, ATA_SECT_CNT, 1);
663 ide_outb(device, ATA_LBA_LOW, 0);
664 ide_outb(device, ATA_LBA_MID, 0);
665 ide_outb(device, ATA_LBA_HIGH, 0);
666 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
667 ide_outb(device, ATA_COMMAND, 0xe3);
668 udelay(50);
669 c = ide_wait(device, IDE_TIME_OUT); /* can't take over 500 ms */
670#endif
671}
672
Simon Glasse9be1ee2016-05-01 11:36:10 -0600673__weak void ide_outb(int dev, int port, unsigned char val)
674{
675 debug("ide_outb (dev= %d, port= 0x%x, val= 0x%02x) : @ 0x%08lx\n",
Simon Glassc229cd22021-11-24 09:26:48 -0700676 dev, port, val, ATA_CURR_BASE(dev) + port);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600677
Simon Glassc229cd22021-11-24 09:26:48 -0700678 outb(val, ATA_CURR_BASE(dev) + port);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600679}
680
681__weak unsigned char ide_inb(int dev, int port)
682{
683 uchar val;
684
Simon Glassc229cd22021-11-24 09:26:48 -0700685 val = inb(ATA_CURR_BASE(dev) + port);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600686
687 debug("ide_inb (dev= %d, port= 0x%x) : @ 0x%08lx -> 0x%02x\n",
Simon Glassc229cd22021-11-24 09:26:48 -0700688 dev, port, ATA_CURR_BASE(dev) + port, val);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600689 return val;
690}
691
692void ide_init(void)
693{
Simon Glassd0075052023-01-17 10:47:24 -0700694 struct udevice *dev;
Simon Glasse9be1ee2016-05-01 11:36:10 -0600695 unsigned char c;
696 int i, bus;
697
Stefan Roese29caf932022-09-02 14:10:46 +0200698 schedule();
Simon Glasse9be1ee2016-05-01 11:36:10 -0600699
Simon Glasse9be1ee2016-05-01 11:36:10 -0600700 /* ATAPI Drives seems to need a proper IDE Reset */
701 ide_reset();
702
Simon Glasse9be1ee2016-05-01 11:36:10 -0600703 /*
704 * Wait for IDE to get ready.
705 * According to spec, this can take up to 31 seconds!
706 */
707 for (bus = 0; bus < CONFIG_SYS_IDE_MAXBUS; ++bus) {
708 int dev =
709 bus * (CONFIG_SYS_IDE_MAXDEVICE /
710 CONFIG_SYS_IDE_MAXBUS);
711
Simon Glasse9be1ee2016-05-01 11:36:10 -0600712 printf("Bus %d: ", bus);
713
714 ide_bus_ok[bus] = 0;
715
Simon Glass4d89f4b2023-04-25 10:54:27 -0600716 /* Select device */
717 mdelay(100);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600718 ide_outb(dev, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(dev));
Simon Glass4d89f4b2023-04-25 10:54:27 -0600719 mdelay(100);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600720 i = 0;
721 do {
Simon Glass4d89f4b2023-04-25 10:54:27 -0600722 mdelay(10);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600723
724 c = ide_inb(dev, ATA_STATUS);
725 i++;
726 if (i > (ATA_RESET_TIME * 100)) {
727 puts("** Timeout **\n");
Simon Glasse9be1ee2016-05-01 11:36:10 -0600728 return;
729 }
730 if ((i >= 100) && ((i % 100) == 0))
731 putc('.');
732
733 } while (c & ATA_STAT_BUSY);
734
735 if (c & (ATA_STAT_BUSY | ATA_STAT_FAULT)) {
736 puts("not available ");
737 debug("Status = 0x%02X ", c);
738#ifndef CONFIG_ATAPI /* ATAPI Devices do not set DRDY */
739 } else if ((c & ATA_STAT_READY) == 0) {
740 puts("not available ");
741 debug("Status = 0x%02X ", c);
742#endif
743 } else {
744 puts("OK ");
745 ide_bus_ok[bus] = 1;
746 }
Stefan Roese29caf932022-09-02 14:10:46 +0200747 schedule();
Simon Glasse9be1ee2016-05-01 11:36:10 -0600748 }
749
750 putc('\n');
751
Simon Glasse9be1ee2016-05-01 11:36:10 -0600752 for (i = 0; i < CONFIG_SYS_IDE_MAXDEVICE; ++i) {
Simon Glasse9be1ee2016-05-01 11:36:10 -0600753 ide_dev_desc[i].type = DEV_TYPE_UNKNOWN;
Simon Glass8149b152022-09-17 09:00:09 -0600754 ide_dev_desc[i].uclass_id = UCLASS_IDE;
Simon Glasse9be1ee2016-05-01 11:36:10 -0600755 ide_dev_desc[i].devnum = i;
756 ide_dev_desc[i].part_type = PART_TYPE_UNKNOWN;
757 ide_dev_desc[i].blksz = 0;
758 ide_dev_desc[i].log2blksz =
759 LOG2_INVALID(typeof(ide_dev_desc[i].log2blksz));
760 ide_dev_desc[i].lba = 0;
Simon Glasse9be1ee2016-05-01 11:36:10 -0600761 if (!ide_bus_ok[IDE_BUS(i)])
762 continue;
Simon Glasse9be1ee2016-05-01 11:36:10 -0600763 ide_ident(&ide_dev_desc[i]);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600764 dev_print(&ide_dev_desc[i]);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600765 }
Stefan Roese29caf932022-09-02 14:10:46 +0200766 schedule();
Bin Meng68e6f222017-09-10 05:12:51 -0700767
Bin Meng68e6f222017-09-10 05:12:51 -0700768 uclass_first_device(UCLASS_IDE, &dev);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600769}
770
Simon Glasse9be1ee2016-05-01 11:36:10 -0600771__weak void ide_input_swap_data(int dev, ulong *sect_buf, int words)
772{
Reinoud Zandijk0a527fd2021-02-24 17:44:42 +0100773 uintptr_t paddr = (ATA_CURR_BASE(dev) + ATA_DATA_REG);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600774 ushort *dbuf = (ushort *)sect_buf;
775
Reinoud Zandijk0a527fd2021-02-24 17:44:42 +0100776 debug("in input swap data base for read is %p\n", (void *)paddr);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600777
778 while (words--) {
Reinoud Zandijk0a527fd2021-02-24 17:44:42 +0100779 EIEIO;
780 *dbuf++ = be16_to_cpu(inw(paddr));
781 EIEIO;
782 *dbuf++ = be16_to_cpu(inw(paddr));
Simon Glasse9be1ee2016-05-01 11:36:10 -0600783 }
784}
Simon Glasse9be1ee2016-05-01 11:36:10 -0600785
Simon Glasse9be1ee2016-05-01 11:36:10 -0600786__weak void ide_output_data(int dev, const ulong *sect_buf, int words)
787{
Reinoud Zandijk0a527fd2021-02-24 17:44:42 +0100788 uintptr_t paddr = (ATA_CURR_BASE(dev) + ATA_DATA_REG);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600789 ushort *dbuf;
Simon Glasse9be1ee2016-05-01 11:36:10 -0600790
Simon Glasse9be1ee2016-05-01 11:36:10 -0600791 dbuf = (ushort *)sect_buf;
Simon Glasse9be1ee2016-05-01 11:36:10 -0600792 while (words--) {
793 EIEIO;
Reinoud Zandijk0a527fd2021-02-24 17:44:42 +0100794 outw(cpu_to_le16(*dbuf++), paddr);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600795 EIEIO;
Reinoud Zandijk0a527fd2021-02-24 17:44:42 +0100796 outw(cpu_to_le16(*dbuf++), paddr);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600797 }
798}
Reinoud Zandijk0a527fd2021-02-24 17:44:42 +0100799
Simon Glasse9be1ee2016-05-01 11:36:10 -0600800__weak void ide_input_data(int dev, ulong *sect_buf, int words)
801{
Reinoud Zandijk0a527fd2021-02-24 17:44:42 +0100802 uintptr_t paddr = (ATA_CURR_BASE(dev) + ATA_DATA_REG);
803 ushort *dbuf;
Simon Glasse9be1ee2016-05-01 11:36:10 -0600804
Reinoud Zandijk0a527fd2021-02-24 17:44:42 +0100805 dbuf = (ushort *)sect_buf;
806
807 debug("in input data base for read is %p\n", (void *)paddr);
808
809 while (words--) {
810 EIEIO;
811 *dbuf++ = le16_to_cpu(inw(paddr));
812 EIEIO;
813 *dbuf++ = le16_to_cpu(inw(paddr));
814 }
Reinoud Zandijk0a527fd2021-02-24 17:44:42 +0100815}
Simon Glasse9be1ee2016-05-01 11:36:10 -0600816
Simon Glass145df842016-05-01 11:36:22 -0600817ulong ide_read(struct udevice *dev, lbaint_t blknr, lbaint_t blkcnt,
818 void *buffer)
Simon Glasse9be1ee2016-05-01 11:36:10 -0600819{
Simon Glasscaa4daa2020-12-03 16:55:18 -0700820 struct blk_desc *block_dev = dev_get_uclass_plat(dev);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600821 int device = block_dev->devnum;
822 ulong n = 0;
823 unsigned char c;
824 unsigned char pwrsave = 0; /* power save */
825
826#ifdef CONFIG_LBA48
827 unsigned char lba48 = 0;
828
829 if (blknr & 0x0000fffff0000000ULL) {
830 /* more than 28 bits used, use 48bit mode */
831 lba48 = 1;
832 }
833#endif
834 debug("ide_read dev %d start " LBAF ", blocks " LBAF " buffer at %lX\n",
835 device, blknr, blkcnt, (ulong) buffer);
836
Simon Glasse9be1ee2016-05-01 11:36:10 -0600837 /* Select device
838 */
839 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
840 c = ide_wait(device, IDE_TIME_OUT);
841
842 if (c & ATA_STAT_BUSY) {
843 printf("IDE read: device %d not ready\n", device);
844 goto IDE_READ_E;
845 }
846
847 /* first check if the drive is in Powersaving mode, if yes,
848 * increase the timeout value */
Heinrich Schuchardte6e9a4f2020-02-27 18:28:00 +0100849 ide_outb(device, ATA_COMMAND, ATA_CMD_CHK_POWER);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600850 udelay(50);
851
852 c = ide_wait(device, IDE_TIME_OUT); /* can't take over 500 ms */
853
854 if (c & ATA_STAT_BUSY) {
855 printf("IDE read: device %d not ready\n", device);
856 goto IDE_READ_E;
857 }
858 if ((c & ATA_STAT_ERR) == ATA_STAT_ERR) {
859 printf("No Powersaving mode %X\n", c);
860 } else {
861 c = ide_inb(device, ATA_SECT_CNT);
862 debug("Powersaving %02X\n", c);
863 if (c == 0)
864 pwrsave = 1;
865 }
866
867
868 while (blkcnt-- > 0) {
869 c = ide_wait(device, IDE_TIME_OUT);
870
871 if (c & ATA_STAT_BUSY) {
872 printf("IDE read: device %d not ready\n", device);
873 break;
874 }
875#ifdef CONFIG_LBA48
876 if (lba48) {
877 /* write high bits */
878 ide_outb(device, ATA_SECT_CNT, 0);
879 ide_outb(device, ATA_LBA_LOW, (blknr >> 24) & 0xFF);
880#ifdef CONFIG_SYS_64BIT_LBA
881 ide_outb(device, ATA_LBA_MID, (blknr >> 32) & 0xFF);
882 ide_outb(device, ATA_LBA_HIGH, (blknr >> 40) & 0xFF);
883#else
884 ide_outb(device, ATA_LBA_MID, 0);
885 ide_outb(device, ATA_LBA_HIGH, 0);
886#endif
887 }
888#endif
889 ide_outb(device, ATA_SECT_CNT, 1);
890 ide_outb(device, ATA_LBA_LOW, (blknr >> 0) & 0xFF);
891 ide_outb(device, ATA_LBA_MID, (blknr >> 8) & 0xFF);
892 ide_outb(device, ATA_LBA_HIGH, (blknr >> 16) & 0xFF);
893
894#ifdef CONFIG_LBA48
895 if (lba48) {
896 ide_outb(device, ATA_DEV_HD,
897 ATA_LBA | ATA_DEVICE(device));
Reinoud Zandijk0a527fd2021-02-24 17:44:42 +0100898 ide_outb(device, ATA_COMMAND, ATA_CMD_PIO_READ_EXT);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600899
900 } else
901#endif
902 {
903 ide_outb(device, ATA_DEV_HD, ATA_LBA |
904 ATA_DEVICE(device) | ((blknr >> 24) & 0xF));
Reinoud Zandijk0a527fd2021-02-24 17:44:42 +0100905 ide_outb(device, ATA_COMMAND, ATA_CMD_PIO_READ);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600906 }
907
908 udelay(50);
909
910 if (pwrsave) {
911 /* may take up to 4 sec */
912 c = ide_wait(device, IDE_SPIN_UP_TIME_OUT);
913 pwrsave = 0;
914 } else {
915 /* can't take over 500 ms */
916 c = ide_wait(device, IDE_TIME_OUT);
917 }
918
919 if ((c & (ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR)) !=
920 ATA_STAT_DRQ) {
921 printf("Error (no IRQ) dev %d blk " LBAF
922 ": status %#02x\n", device, blknr, c);
923 break;
924 }
925
926 ide_input_data(device, buffer, ATA_SECTORWORDS);
927 (void) ide_inb(device, ATA_STATUS); /* clear IRQ */
928
929 ++n;
930 ++blknr;
931 buffer += ATA_BLOCKSIZE;
932 }
933IDE_READ_E:
Simon Glasse9be1ee2016-05-01 11:36:10 -0600934 return n;
935}
936
Simon Glass145df842016-05-01 11:36:22 -0600937ulong ide_write(struct udevice *dev, lbaint_t blknr, lbaint_t blkcnt,
938 const void *buffer)
Simon Glasse9be1ee2016-05-01 11:36:10 -0600939{
Simon Glasscaa4daa2020-12-03 16:55:18 -0700940 struct blk_desc *block_dev = dev_get_uclass_plat(dev);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600941 int device = block_dev->devnum;
942 ulong n = 0;
943 unsigned char c;
944
945#ifdef CONFIG_LBA48
946 unsigned char lba48 = 0;
947
948 if (blknr & 0x0000fffff0000000ULL) {
949 /* more than 28 bits used, use 48bit mode */
950 lba48 = 1;
951 }
952#endif
953
Simon Glasse9be1ee2016-05-01 11:36:10 -0600954 /* Select device
955 */
956 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
957
958 while (blkcnt-- > 0) {
959 c = ide_wait(device, IDE_TIME_OUT);
960
961 if (c & ATA_STAT_BUSY) {
962 printf("IDE read: device %d not ready\n", device);
963 goto WR_OUT;
964 }
965#ifdef CONFIG_LBA48
966 if (lba48) {
967 /* write high bits */
968 ide_outb(device, ATA_SECT_CNT, 0);
969 ide_outb(device, ATA_LBA_LOW, (blknr >> 24) & 0xFF);
970#ifdef CONFIG_SYS_64BIT_LBA
971 ide_outb(device, ATA_LBA_MID, (blknr >> 32) & 0xFF);
972 ide_outb(device, ATA_LBA_HIGH, (blknr >> 40) & 0xFF);
973#else
974 ide_outb(device, ATA_LBA_MID, 0);
975 ide_outb(device, ATA_LBA_HIGH, 0);
976#endif
977 }
978#endif
979 ide_outb(device, ATA_SECT_CNT, 1);
980 ide_outb(device, ATA_LBA_LOW, (blknr >> 0) & 0xFF);
981 ide_outb(device, ATA_LBA_MID, (blknr >> 8) & 0xFF);
982 ide_outb(device, ATA_LBA_HIGH, (blknr >> 16) & 0xFF);
983
984#ifdef CONFIG_LBA48
985 if (lba48) {
986 ide_outb(device, ATA_DEV_HD,
987 ATA_LBA | ATA_DEVICE(device));
Reinoud Zandijk0a527fd2021-02-24 17:44:42 +0100988 ide_outb(device, ATA_COMMAND, ATA_CMD_PIO_WRITE_EXT);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600989
990 } else
991#endif
992 {
993 ide_outb(device, ATA_DEV_HD, ATA_LBA |
994 ATA_DEVICE(device) | ((blknr >> 24) & 0xF));
Reinoud Zandijk0a527fd2021-02-24 17:44:42 +0100995 ide_outb(device, ATA_COMMAND, ATA_CMD_PIO_WRITE);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600996 }
997
998 udelay(50);
999
1000 /* can't take over 500 ms */
1001 c = ide_wait(device, IDE_TIME_OUT);
1002
1003 if ((c & (ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR)) !=
1004 ATA_STAT_DRQ) {
1005 printf("Error (no IRQ) dev %d blk " LBAF
1006 ": status %#02x\n", device, blknr, c);
1007 goto WR_OUT;
1008 }
1009
1010 ide_output_data(device, buffer, ATA_SECTORWORDS);
1011 c = ide_inb(device, ATA_STATUS); /* clear IRQ */
1012 ++n;
1013 ++blknr;
1014 buffer += ATA_BLOCKSIZE;
1015 }
1016WR_OUT:
Simon Glasse9be1ee2016-05-01 11:36:10 -06001017 return n;
1018}
1019
1020#if defined(CONFIG_OF_IDE_FIXUP)
1021int ide_device_present(int dev)
1022{
1023 if (dev >= CONFIG_SYS_IDE_MAXBUS)
1024 return 0;
1025 return ide_dev_desc[dev].type == DEV_TYPE_UNKNOWN ? 0 : 1;
1026}
1027#endif
1028
Bin Meng68e6f222017-09-10 05:12:51 -07001029static int ide_blk_probe(struct udevice *udev)
1030{
Simon Glasscaa4daa2020-12-03 16:55:18 -07001031 struct blk_desc *desc = dev_get_uclass_plat(udev);
Bin Meng68e6f222017-09-10 05:12:51 -07001032
1033 /* fill in device vendor/product/rev strings */
1034 strncpy(desc->vendor, ide_dev_desc[desc->devnum].vendor,
1035 BLK_VEN_SIZE);
1036 desc->vendor[BLK_VEN_SIZE] = '\0';
1037 strncpy(desc->product, ide_dev_desc[desc->devnum].product,
1038 BLK_PRD_SIZE);
1039 desc->product[BLK_PRD_SIZE] = '\0';
1040 strncpy(desc->revision, ide_dev_desc[desc->devnum].revision,
1041 BLK_REV_SIZE);
1042 desc->revision[BLK_REV_SIZE] = '\0';
1043
Bin Meng68e6f222017-09-10 05:12:51 -07001044 return 0;
1045}
1046
Simon Glass145df842016-05-01 11:36:22 -06001047static const struct blk_ops ide_blk_ops = {
1048 .read = ide_read,
1049 .write = ide_write,
1050};
1051
1052U_BOOT_DRIVER(ide_blk) = {
1053 .name = "ide_blk",
1054 .id = UCLASS_BLK,
1055 .ops = &ide_blk_ops,
Bin Meng68e6f222017-09-10 05:12:51 -07001056 .probe = ide_blk_probe,
1057};
1058
Simon Glass0d77f8f2023-01-17 10:47:46 -07001059static int ide_bootdev_bind(struct udevice *dev)
1060{
1061 struct bootdev_uc_plat *ucp = dev_get_uclass_plat(dev);
1062
Simon Glasseacc2612023-01-17 10:48:08 -07001063 ucp->prio = BOOTDEVP_5_SCAN_SLOW;
Simon Glass0d77f8f2023-01-17 10:47:46 -07001064
1065 return 0;
1066}
1067
1068static int ide_bootdev_hunt(struct bootdev_hunter *info, bool show)
1069{
1070 ide_init();
1071
1072 return 0;
1073}
1074
1075struct bootdev_ops ide_bootdev_ops = {
1076};
1077
1078static const struct udevice_id ide_bootdev_ids[] = {
1079 { .compatible = "u-boot,bootdev-ide" },
1080 { }
1081};
1082
1083U_BOOT_DRIVER(ide_bootdev) = {
1084 .name = "ide_bootdev",
1085 .id = UCLASS_BOOTDEV,
1086 .ops = &ide_bootdev_ops,
1087 .bind = ide_bootdev_bind,
1088 .of_match = ide_bootdev_ids,
1089};
1090
1091BOOTDEV_HUNTER(ide_bootdev_hunter) = {
Simon Glasseacc2612023-01-17 10:48:08 -07001092 .prio = BOOTDEVP_5_SCAN_SLOW,
Simon Glass0d77f8f2023-01-17 10:47:46 -07001093 .uclass = UCLASS_IDE,
1094 .hunt = ide_bootdev_hunt,
1095 .drv = DM_DRIVER_REF(ide_bootdev),
1096};
1097
Bin Meng68e6f222017-09-10 05:12:51 -07001098static int ide_probe(struct udevice *udev)
1099{
1100 struct udevice *blk_dev;
1101 char name[20];
1102 int blksz;
1103 lbaint_t size;
1104 int i;
1105 int ret;
1106
1107 for (i = 0; i < CONFIG_SYS_IDE_MAXDEVICE; i++) {
1108 if (ide_dev_desc[i].type != DEV_TYPE_UNKNOWN) {
1109 sprintf(name, "blk#%d", i);
1110
1111 blksz = ide_dev_desc[i].blksz;
1112 size = blksz * ide_dev_desc[i].lba;
Bin Meng1f4adab2017-09-10 05:12:52 -07001113
1114 /*
1115 * With CDROM, if there is no CD inserted, blksz will
1116 * be zero, don't bother to create IDE block device.
1117 */
1118 if (!blksz)
1119 continue;
Bin Meng68e6f222017-09-10 05:12:51 -07001120 ret = blk_create_devicef(udev, "ide_blk", name,
Simon Glasse33a5c62022-08-11 19:34:59 -06001121 UCLASS_IDE, i,
Bin Meng68e6f222017-09-10 05:12:51 -07001122 blksz, size, &blk_dev);
1123 if (ret)
1124 return ret;
AKASHI Takahiro4c73b032022-03-08 20:36:44 +09001125
1126 ret = blk_probe_or_unbind(blk_dev);
1127 if (ret)
1128 return ret;
Simon Glass0d77f8f2023-01-17 10:47:46 -07001129
1130 ret = bootdev_setup_for_dev(udev, "ide_bootdev");
1131 if (ret)
1132 return log_msg_ret("bootdev", ret);
Bin Meng68e6f222017-09-10 05:12:51 -07001133 }
1134 }
1135
1136 return 0;
1137}
1138
1139U_BOOT_DRIVER(ide) = {
1140 .name = "ide",
1141 .id = UCLASS_IDE,
1142 .probe = ide_probe,
1143};
1144
1145struct pci_device_id ide_supported[] = {
1146 { PCI_DEVICE_CLASS(PCI_CLASS_STORAGE_IDE << 8, 0xffff00) },
1147 { }
1148};
1149
1150U_BOOT_PCI_DEVICE(ide, ide_supported);
1151
1152UCLASS_DRIVER(ide) = {
1153 .name = "ide",
1154 .id = UCLASS_IDE,
Simon Glass145df842016-05-01 11:36:22 -06001155};