blob: f36bec8b3a8aeeeb66756322287f68f2f022082f [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
74 /* wait 250 ms */
75 for (i = 0; i < 250; ++i)
76 udelay(1000);
77}
78#else
79#define ide_reset() /* dummy */
80#endif /* CONFIG_IDE_RESET */
81
82/*
83 * Wait until Busy bit is off, or timeout (in ms)
84 * Return last status
85 */
86static uchar ide_wait(int dev, ulong t)
87{
88 ulong delay = 10 * t; /* poll every 100 us */
89 uchar c;
90
91 while ((c = ide_inb(dev, ATA_STATUS)) & ATA_STAT_BUSY) {
92 udelay(100);
93 if (delay-- == 0)
94 break;
95 }
96 return c;
97}
98
99/*
100 * copy src to dest, skipping leading and trailing blanks and null
101 * terminate the string
102 * "len" is the size of available memory including the terminating '\0'
103 */
104static void ident_cpy(unsigned char *dst, unsigned char *src,
105 unsigned int len)
106{
107 unsigned char *end, *last;
108
109 last = dst;
110 end = src + len - 1;
111
112 /* reserve space for '\0' */
113 if (len < 2)
114 goto OUT;
115
116 /* skip leading white space */
117 while ((*src) && (src < end) && (*src == ' '))
118 ++src;
119
120 /* copy string, omitting trailing white space */
121 while ((*src) && (src < end)) {
122 *dst++ = *src;
123 if (*src++ != ' ')
124 last = dst;
125 }
126OUT:
127 *last = '\0';
128}
129
130#ifdef CONFIG_ATAPI
131/****************************************************************************
132 * ATAPI Support
133 */
134
Simon Glasse9be1ee2016-05-01 11:36:10 -0600135/* since ATAPI may use commands with not 4 bytes alligned length
136 * we have our own transfer functions, 2 bytes alligned */
137__weak void ide_output_data_shorts(int dev, ushort *sect_buf, int shorts)
138{
Reinoud Zandijk0a527fd2021-02-24 17:44:42 +0100139 uintptr_t paddr = (ATA_CURR_BASE(dev) + ATA_DATA_REG);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600140 ushort *dbuf;
Simon Glasse9be1ee2016-05-01 11:36:10 -0600141
Simon Glasse9be1ee2016-05-01 11:36:10 -0600142 dbuf = (ushort *)sect_buf;
143
Reinoud Zandijk0a527fd2021-02-24 17:44:42 +0100144 debug("in output data shorts base for read is %p\n", (void *)paddr);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600145
146 while (shorts--) {
147 EIEIO;
Reinoud Zandijk0a527fd2021-02-24 17:44:42 +0100148 outw(cpu_to_le16(*dbuf++), paddr);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600149 }
150}
151
152__weak void ide_input_data_shorts(int dev, ushort *sect_buf, int shorts)
153{
Reinoud Zandijk0a527fd2021-02-24 17:44:42 +0100154 uintptr_t paddr = (ATA_CURR_BASE(dev) + ATA_DATA_REG);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600155 ushort *dbuf;
Simon Glasse9be1ee2016-05-01 11:36:10 -0600156
Simon Glasse9be1ee2016-05-01 11:36:10 -0600157 dbuf = (ushort *)sect_buf;
158
Reinoud Zandijk0a527fd2021-02-24 17:44:42 +0100159 debug("in input data shorts base for read is %p\n", (void *)paddr);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600160
161 while (shorts--) {
162 EIEIO;
Reinoud Zandijk0a527fd2021-02-24 17:44:42 +0100163 *dbuf++ = le16_to_cpu(inw(paddr));
Simon Glasse9be1ee2016-05-01 11:36:10 -0600164 }
165}
166
Simon Glasse9be1ee2016-05-01 11:36:10 -0600167/*
168 * Wait until (Status & mask) == res, or timeout (in ms)
169 * Return last status
170 * This is used since some ATAPI CD ROMs clears their Busy Bit first
171 * and then they set their DRQ Bit
172 */
173static uchar atapi_wait_mask(int dev, ulong t, uchar mask, uchar res)
174{
175 ulong delay = 10 * t; /* poll every 100 us */
176 uchar c;
177
178 /* prevents to read the status before valid */
179 c = ide_inb(dev, ATA_DEV_CTL);
180
181 while (((c = ide_inb(dev, ATA_STATUS)) & mask) != res) {
182 /* break if error occurs (doesn't make sense to wait more) */
183 if ((c & ATA_STAT_ERR) == ATA_STAT_ERR)
184 break;
185 udelay(100);
186 if (delay-- == 0)
187 break;
188 }
189 return c;
190}
191
192/*
193 * issue an atapi command
194 */
195unsigned char atapi_issue(int device, unsigned char *ccb, int ccblen,
196 unsigned char *buffer, int buflen)
197{
198 unsigned char c, err, mask, res;
199 int n;
200
Simon Glasse9be1ee2016-05-01 11:36:10 -0600201 /* Select device
202 */
203 mask = ATA_STAT_BUSY | ATA_STAT_DRQ;
204 res = 0;
205 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
206 c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
207 if ((c & mask) != res) {
208 printf("ATAPI_ISSUE: device %d not ready status %X\n", device,
209 c);
210 err = 0xFF;
211 goto AI_OUT;
212 }
213 /* write taskfile */
214 ide_outb(device, ATA_ERROR_REG, 0); /* no DMA, no overlaped */
215 ide_outb(device, ATA_SECT_CNT, 0);
216 ide_outb(device, ATA_SECT_NUM, 0);
217 ide_outb(device, ATA_CYL_LOW, (unsigned char) (buflen & 0xFF));
218 ide_outb(device, ATA_CYL_HIGH,
219 (unsigned char) ((buflen >> 8) & 0xFF));
220 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
221
Heinrich Schuchardte6e9a4f2020-02-27 18:28:00 +0100222 ide_outb(device, ATA_COMMAND, ATA_CMD_PACKET);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600223 udelay(50);
224
225 mask = ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR;
226 res = ATA_STAT_DRQ;
227 c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
228
229 if ((c & mask) != res) { /* DRQ must be 1, BSY 0 */
230 printf("ATAPI_ISSUE: Error (no IRQ) before sending ccb dev %d status 0x%02x\n",
231 device, c);
232 err = 0xFF;
233 goto AI_OUT;
234 }
235
236 /* write command block */
237 ide_output_data_shorts(device, (unsigned short *)ccb, ccblen / 2);
238
239 /* ATAPI Command written wait for completition */
240 udelay(5000); /* device must set bsy */
241
242 mask = ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR;
243 /*
244 * if no data wait for DRQ = 0 BSY = 0
245 * if data wait for DRQ = 1 BSY = 0
246 */
247 res = 0;
248 if (buflen)
249 res = ATA_STAT_DRQ;
250 c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
251 if ((c & mask) != res) {
252 if (c & ATA_STAT_ERR) {
253 err = (ide_inb(device, ATA_ERROR_REG)) >> 4;
254 debug("atapi_issue 1 returned sense key %X status %02X\n",
255 err, c);
256 } else {
257 printf("ATAPI_ISSUE: (no DRQ) after sending ccb (%x) status 0x%02x\n",
258 ccb[0], c);
259 err = 0xFF;
260 }
261 goto AI_OUT;
262 }
263 n = ide_inb(device, ATA_CYL_HIGH);
264 n <<= 8;
265 n += ide_inb(device, ATA_CYL_LOW);
266 if (n > buflen) {
267 printf("ERROR, transfer bytes %d requested only %d\n", n,
268 buflen);
269 err = 0xff;
270 goto AI_OUT;
271 }
272 if ((n == 0) && (buflen < 0)) {
273 printf("ERROR, transfer bytes %d requested %d\n", n, buflen);
274 err = 0xff;
275 goto AI_OUT;
276 }
277 if (n != buflen) {
278 debug("WARNING, transfer bytes %d not equal with requested %d\n",
279 n, buflen);
280 }
281 if (n != 0) { /* data transfer */
282 debug("ATAPI_ISSUE: %d Bytes to transfer\n", n);
283 /* we transfer shorts */
284 n >>= 1;
285 /* ok now decide if it is an in or output */
286 if ((ide_inb(device, ATA_SECT_CNT) & 0x02) == 0) {
287 debug("Write to device\n");
288 ide_output_data_shorts(device, (unsigned short *)buffer,
289 n);
290 } else {
291 debug("Read from device @ %p shorts %d\n", buffer, n);
292 ide_input_data_shorts(device, (unsigned short *)buffer,
293 n);
294 }
295 }
296 udelay(5000); /* seems that some CD ROMs need this... */
297 mask = ATA_STAT_BUSY | ATA_STAT_ERR;
298 res = 0;
299 c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
300 if ((c & ATA_STAT_ERR) == ATA_STAT_ERR) {
301 err = (ide_inb(device, ATA_ERROR_REG) >> 4);
302 debug("atapi_issue 2 returned sense key %X status %X\n", err,
303 c);
304 } else {
305 err = 0;
306 }
307AI_OUT:
Simon Glasse9be1ee2016-05-01 11:36:10 -0600308 return err;
309}
310
311/*
312 * sending the command to atapi_issue. If an status other than good
313 * returns, an request_sense will be issued
314 */
315
316#define ATAPI_DRIVE_NOT_READY 100
317#define ATAPI_UNIT_ATTN 10
318
319unsigned char atapi_issue_autoreq(int device,
320 unsigned char *ccb,
321 int ccblen,
322 unsigned char *buffer, int buflen)
323{
324 unsigned char sense_data[18], sense_ccb[12];
325 unsigned char res, key, asc, ascq;
326 int notready, unitattn;
327
328 unitattn = ATAPI_UNIT_ATTN;
329 notready = ATAPI_DRIVE_NOT_READY;
330
331retry:
332 res = atapi_issue(device, ccb, ccblen, buffer, buflen);
333 if (res == 0)
334 return 0; /* Ok */
335
336 if (res == 0xFF)
337 return 0xFF; /* error */
338
339 debug("(auto_req)atapi_issue returned sense key %X\n", res);
340
341 memset(sense_ccb, 0, sizeof(sense_ccb));
342 memset(sense_data, 0, sizeof(sense_data));
343 sense_ccb[0] = ATAPI_CMD_REQ_SENSE;
344 sense_ccb[4] = 18; /* allocation Length */
345
346 res = atapi_issue(device, sense_ccb, 12, sense_data, 18);
347 key = (sense_data[2] & 0xF);
348 asc = (sense_data[12]);
349 ascq = (sense_data[13]);
350
351 debug("ATAPI_CMD_REQ_SENSE returned %x\n", res);
352 debug(" Sense page: %02X key %02X ASC %02X ASCQ %02X\n",
353 sense_data[0], key, asc, ascq);
354
355 if ((key == 0))
356 return 0; /* ok device ready */
357
358 if ((key == 6) || (asc == 0x29) || (asc == 0x28)) { /* Unit Attention */
359 if (unitattn-- > 0) {
360 udelay(200 * 1000);
361 goto retry;
362 }
363 printf("Unit Attention, tried %d\n", ATAPI_UNIT_ATTN);
364 goto error;
365 }
366 if ((asc == 0x4) && (ascq == 0x1)) {
367 /* not ready, but will be ready soon */
368 if (notready-- > 0) {
369 udelay(200 * 1000);
370 goto retry;
371 }
372 printf("Drive not ready, tried %d times\n",
373 ATAPI_DRIVE_NOT_READY);
374 goto error;
375 }
376 if (asc == 0x3a) {
377 debug("Media not present\n");
378 goto error;
379 }
380
381 printf("ERROR: Unknown Sense key %02X ASC %02X ASCQ %02X\n", key, asc,
382 ascq);
383error:
384 debug("ERROR Sense key %02X ASC %02X ASCQ %02X\n", key, asc, ascq);
385 return 0xFF;
386}
387
388/*
389 * atapi_read:
390 * we transfer only one block per command, since the multiple DRQ per
391 * command is not yet implemented
392 */
393#define ATAPI_READ_MAX_BYTES 2048 /* we read max 2kbytes */
394#define ATAPI_READ_BLOCK_SIZE 2048 /* assuming CD part */
395#define ATAPI_READ_MAX_BLOCK (ATAPI_READ_MAX_BYTES/ATAPI_READ_BLOCK_SIZE)
396
397ulong atapi_read(struct blk_desc *block_dev, lbaint_t blknr, lbaint_t blkcnt,
398 void *buffer)
399{
400 int device = block_dev->devnum;
401 ulong n = 0;
402 unsigned char ccb[12]; /* Command descriptor block */
403 ulong cnt;
404
405 debug("atapi_read dev %d start " LBAF " blocks " LBAF
406 " buffer at %lX\n", device, blknr, blkcnt, (ulong) buffer);
407
408 do {
409 if (blkcnt > ATAPI_READ_MAX_BLOCK)
410 cnt = ATAPI_READ_MAX_BLOCK;
411 else
412 cnt = blkcnt;
413
414 ccb[0] = ATAPI_CMD_READ_12;
415 ccb[1] = 0; /* reserved */
416 ccb[2] = (unsigned char) (blknr >> 24) & 0xFF; /* MSB Block */
417 ccb[3] = (unsigned char) (blknr >> 16) & 0xFF; /* */
418 ccb[4] = (unsigned char) (blknr >> 8) & 0xFF;
419 ccb[5] = (unsigned char) blknr & 0xFF; /* LSB Block */
420 ccb[6] = (unsigned char) (cnt >> 24) & 0xFF; /* MSB Block cnt */
421 ccb[7] = (unsigned char) (cnt >> 16) & 0xFF;
422 ccb[8] = (unsigned char) (cnt >> 8) & 0xFF;
423 ccb[9] = (unsigned char) cnt & 0xFF; /* LSB Block */
424 ccb[10] = 0; /* reserved */
425 ccb[11] = 0; /* reserved */
426
427 if (atapi_issue_autoreq(device, ccb, 12,
428 (unsigned char *)buffer,
429 cnt * ATAPI_READ_BLOCK_SIZE)
430 == 0xFF) {
431 return n;
432 }
433 n += cnt;
434 blkcnt -= cnt;
435 blknr += cnt;
436 buffer += (cnt * ATAPI_READ_BLOCK_SIZE);
437 } while (blkcnt > 0);
438 return n;
439}
440
441static void atapi_inquiry(struct blk_desc *dev_desc)
442{
443 unsigned char ccb[12]; /* Command descriptor block */
444 unsigned char iobuf[64]; /* temp buf */
445 unsigned char c;
446 int device;
447
448 device = dev_desc->devnum;
449 dev_desc->type = DEV_TYPE_UNKNOWN; /* not yet valid */
Simon Glasse9be1ee2016-05-01 11:36:10 -0600450
451 memset(ccb, 0, sizeof(ccb));
452 memset(iobuf, 0, sizeof(iobuf));
453
454 ccb[0] = ATAPI_CMD_INQUIRY;
455 ccb[4] = 40; /* allocation Legnth */
456 c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *)iobuf, 40);
457
458 debug("ATAPI_CMD_INQUIRY returned %x\n", c);
459 if (c != 0)
460 return;
461
462 /* copy device ident strings */
463 ident_cpy((unsigned char *)dev_desc->vendor, &iobuf[8], 8);
464 ident_cpy((unsigned char *)dev_desc->product, &iobuf[16], 16);
465 ident_cpy((unsigned char *)dev_desc->revision, &iobuf[32], 5);
466
467 dev_desc->lun = 0;
468 dev_desc->lba = 0;
469 dev_desc->blksz = 0;
470 dev_desc->log2blksz = LOG2_INVALID(typeof(dev_desc->log2blksz));
471 dev_desc->type = iobuf[0] & 0x1f;
472
473 if ((iobuf[1] & 0x80) == 0x80)
474 dev_desc->removable = 1;
475 else
476 dev_desc->removable = 0;
477
478 memset(ccb, 0, sizeof(ccb));
479 memset(iobuf, 0, sizeof(iobuf));
480 ccb[0] = ATAPI_CMD_START_STOP;
481 ccb[4] = 0x03; /* start */
482
483 c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *)iobuf, 0);
484
485 debug("ATAPI_CMD_START_STOP returned %x\n", c);
486 if (c != 0)
487 return;
488
489 memset(ccb, 0, sizeof(ccb));
490 memset(iobuf, 0, sizeof(iobuf));
491 c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *)iobuf, 0);
492
493 debug("ATAPI_CMD_UNIT_TEST_READY returned %x\n", c);
494 if (c != 0)
495 return;
496
497 memset(ccb, 0, sizeof(ccb));
498 memset(iobuf, 0, sizeof(iobuf));
499 ccb[0] = ATAPI_CMD_READ_CAP;
500 c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *)iobuf, 8);
501 debug("ATAPI_CMD_READ_CAP returned %x\n", c);
502 if (c != 0)
503 return;
504
505 debug("Read Cap: LBA %02X%02X%02X%02X blksize %02X%02X%02X%02X\n",
506 iobuf[0], iobuf[1], iobuf[2], iobuf[3],
507 iobuf[4], iobuf[5], iobuf[6], iobuf[7]);
508
509 dev_desc->lba = ((unsigned long) iobuf[0] << 24) +
510 ((unsigned long) iobuf[1] << 16) +
511 ((unsigned long) iobuf[2] << 8) + ((unsigned long) iobuf[3]);
512 dev_desc->blksz = ((unsigned long) iobuf[4] << 24) +
513 ((unsigned long) iobuf[5] << 16) +
514 ((unsigned long) iobuf[6] << 8) + ((unsigned long) iobuf[7]);
515 dev_desc->log2blksz = LOG2(dev_desc->blksz);
516#ifdef CONFIG_LBA48
517 /* ATAPI devices cannot use 48bit addressing (ATA/ATAPI v7) */
518 dev_desc->lba48 = 0;
519#endif
520 return;
521}
522
523#endif /* CONFIG_ATAPI */
524
525static void ide_ident(struct blk_desc *dev_desc)
526{
527 unsigned char c;
528 hd_driveid_t iop;
Simon Glasse9be1ee2016-05-01 11:36:10 -0600529#ifdef CONFIG_ATAPI
Simon Glass606e0542022-08-11 19:34:53 -0600530 bool is_atapi = false;
Simon Glasse9be1ee2016-05-01 11:36:10 -0600531 int retries = 0;
532#endif
533 int device;
534
535 device = dev_desc->devnum;
536 printf(" Device %d: ", device);
537
Simon Glasse9be1ee2016-05-01 11:36:10 -0600538 /* Select device
539 */
540 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
Simon Glass8149b152022-09-17 09:00:09 -0600541 dev_desc->uclass_id = UCLASS_IDE;
Simon Glasse9be1ee2016-05-01 11:36:10 -0600542#ifdef CONFIG_ATAPI
543
544 retries = 0;
545
546 /* Warning: This will be tricky to read */
547 while (retries <= 1) {
548 /* check signature */
549 if ((ide_inb(device, ATA_SECT_CNT) == 0x01) &&
550 (ide_inb(device, ATA_SECT_NUM) == 0x01) &&
551 (ide_inb(device, ATA_CYL_LOW) == 0x14) &&
552 (ide_inb(device, ATA_CYL_HIGH) == 0xEB)) {
553 /* ATAPI Signature found */
Simon Glass606e0542022-08-11 19:34:53 -0600554 is_atapi = true;
Simon Glasse9be1ee2016-05-01 11:36:10 -0600555 /*
556 * Start Ident Command
557 */
Heinrich Schuchardte6e9a4f2020-02-27 18:28:00 +0100558 ide_outb(device, ATA_COMMAND, ATA_CMD_ID_ATAPI);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600559 /*
560 * Wait for completion - ATAPI devices need more time
561 * to become ready
562 */
563 c = ide_wait(device, ATAPI_TIME_OUT);
564 } else
565#endif
566 {
567 /*
568 * Start Ident Command
569 */
Heinrich Schuchardte6e9a4f2020-02-27 18:28:00 +0100570 ide_outb(device, ATA_COMMAND, ATA_CMD_ID_ATA);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600571
572 /*
573 * Wait for completion
574 */
575 c = ide_wait(device, IDE_TIME_OUT);
576 }
Simon Glasse9be1ee2016-05-01 11:36:10 -0600577
578 if (((c & ATA_STAT_DRQ) == 0) ||
579 ((c & (ATA_STAT_FAULT | ATA_STAT_ERR)) != 0)) {
580#ifdef CONFIG_ATAPI
581 {
582 /*
583 * Need to soft reset the device
584 * in case it's an ATAPI...
585 */
586 debug("Retrying...\n");
587 ide_outb(device, ATA_DEV_HD,
588 ATA_LBA | ATA_DEVICE(device));
589 udelay(100000);
590 ide_outb(device, ATA_COMMAND, 0x08);
591 udelay(500000); /* 500 ms */
592 }
593 /*
594 * Select device
595 */
596 ide_outb(device, ATA_DEV_HD,
597 ATA_LBA | ATA_DEVICE(device));
598 retries++;
599#else
600 return;
601#endif
602 }
603#ifdef CONFIG_ATAPI
604 else
605 break;
606 } /* see above - ugly to read */
607
608 if (retries == 2) /* Not found */
609 return;
610#endif
611
612 ide_input_swap_data(device, (ulong *)&iop, ATA_SECTORWORDS);
613
614 ident_cpy((unsigned char *)dev_desc->revision, iop.fw_rev,
615 sizeof(dev_desc->revision));
616 ident_cpy((unsigned char *)dev_desc->vendor, iop.model,
617 sizeof(dev_desc->vendor));
618 ident_cpy((unsigned char *)dev_desc->product, iop.serial_no,
619 sizeof(dev_desc->product));
Simon Glasse9be1ee2016-05-01 11:36:10 -0600620
621 if ((iop.config & 0x0080) == 0x0080)
622 dev_desc->removable = 1;
623 else
624 dev_desc->removable = 0;
625
626#ifdef CONFIG_ATAPI
Simon Glass606e0542022-08-11 19:34:53 -0600627 if (is_atapi) {
Simon Glasse9be1ee2016-05-01 11:36:10 -0600628 atapi_inquiry(dev_desc);
629 return;
630 }
631#endif /* CONFIG_ATAPI */
632
Reinoud Zandijk0a527fd2021-02-24 17:44:42 +0100633 iop.lba_capacity[0] = be16_to_cpu(iop.lba_capacity[0]);
634 iop.lba_capacity[1] = be16_to_cpu(iop.lba_capacity[1]);
635 dev_desc->lba =
636 ((unsigned long)iop.lba_capacity[0]) |
637 ((unsigned long)iop.lba_capacity[1] << 16);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600638
639#ifdef CONFIG_LBA48
640 if (iop.command_set_2 & 0x0400) { /* LBA 48 support */
641 dev_desc->lba48 = 1;
Reinoud Zandijk0a527fd2021-02-24 17:44:42 +0100642 for (int i = 0; i < 4; i++)
643 iop.lba48_capacity[i] = be16_to_cpu(iop.lba48_capacity[i]);
644 dev_desc->lba =
645 ((unsigned long long)iop.lba48_capacity[0] |
646 ((unsigned long long)iop.lba48_capacity[1] << 16) |
647 ((unsigned long long)iop.lba48_capacity[2] << 32) |
648 ((unsigned long long)iop.lba48_capacity[3] << 48));
Simon Glasse9be1ee2016-05-01 11:36:10 -0600649 } else {
650 dev_desc->lba48 = 0;
651 }
652#endif /* CONFIG_LBA48 */
653 /* assuming HD */
654 dev_desc->type = DEV_TYPE_HARDDISK;
655 dev_desc->blksz = ATA_BLOCKSIZE;
656 dev_desc->log2blksz = LOG2(dev_desc->blksz);
657 dev_desc->lun = 0; /* just to fill something in... */
658
659#if 0 /* only used to test the powersaving mode,
660 * if enabled, the drive goes after 5 sec
661 * in standby mode */
662 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
663 c = ide_wait(device, IDE_TIME_OUT);
664 ide_outb(device, ATA_SECT_CNT, 1);
665 ide_outb(device, ATA_LBA_LOW, 0);
666 ide_outb(device, ATA_LBA_MID, 0);
667 ide_outb(device, ATA_LBA_HIGH, 0);
668 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
669 ide_outb(device, ATA_COMMAND, 0xe3);
670 udelay(50);
671 c = ide_wait(device, IDE_TIME_OUT); /* can't take over 500 ms */
672#endif
673}
674
Simon Glasse9be1ee2016-05-01 11:36:10 -0600675__weak void ide_outb(int dev, int port, unsigned char val)
676{
677 debug("ide_outb (dev= %d, port= 0x%x, val= 0x%02x) : @ 0x%08lx\n",
Simon Glassc229cd22021-11-24 09:26:48 -0700678 dev, port, val, ATA_CURR_BASE(dev) + port);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600679
Simon Glassc229cd22021-11-24 09:26:48 -0700680 outb(val, ATA_CURR_BASE(dev) + port);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600681}
682
683__weak unsigned char ide_inb(int dev, int port)
684{
685 uchar val;
686
Simon Glassc229cd22021-11-24 09:26:48 -0700687 val = inb(ATA_CURR_BASE(dev) + port);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600688
689 debug("ide_inb (dev= %d, port= 0x%x) : @ 0x%08lx -> 0x%02x\n",
Simon Glassc229cd22021-11-24 09:26:48 -0700690 dev, port, ATA_CURR_BASE(dev) + port, val);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600691 return val;
692}
693
694void ide_init(void)
695{
Simon Glassd0075052023-01-17 10:47:24 -0700696 struct udevice *dev;
Simon Glasse9be1ee2016-05-01 11:36:10 -0600697 unsigned char c;
698 int i, bus;
699
Stefan Roese29caf932022-09-02 14:10:46 +0200700 schedule();
Simon Glasse9be1ee2016-05-01 11:36:10 -0600701
Simon Glasse9be1ee2016-05-01 11:36:10 -0600702 /* ATAPI Drives seems to need a proper IDE Reset */
703 ide_reset();
704
Simon Glasse9be1ee2016-05-01 11:36:10 -0600705 /*
706 * Wait for IDE to get ready.
707 * According to spec, this can take up to 31 seconds!
708 */
709 for (bus = 0; bus < CONFIG_SYS_IDE_MAXBUS; ++bus) {
710 int dev =
711 bus * (CONFIG_SYS_IDE_MAXDEVICE /
712 CONFIG_SYS_IDE_MAXBUS);
713
Simon Glasse9be1ee2016-05-01 11:36:10 -0600714 printf("Bus %d: ", bus);
715
716 ide_bus_ok[bus] = 0;
717
718 /* Select device
719 */
720 udelay(100000); /* 100 ms */
721 ide_outb(dev, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(dev));
722 udelay(100000); /* 100 ms */
723 i = 0;
724 do {
725 udelay(10000); /* 10 ms */
726
727 c = ide_inb(dev, ATA_STATUS);
728 i++;
729 if (i > (ATA_RESET_TIME * 100)) {
730 puts("** Timeout **\n");
Simon Glasse9be1ee2016-05-01 11:36:10 -0600731 return;
732 }
733 if ((i >= 100) && ((i % 100) == 0))
734 putc('.');
735
736 } while (c & ATA_STAT_BUSY);
737
738 if (c & (ATA_STAT_BUSY | ATA_STAT_FAULT)) {
739 puts("not available ");
740 debug("Status = 0x%02X ", c);
741#ifndef CONFIG_ATAPI /* ATAPI Devices do not set DRDY */
742 } else if ((c & ATA_STAT_READY) == 0) {
743 puts("not available ");
744 debug("Status = 0x%02X ", c);
745#endif
746 } else {
747 puts("OK ");
748 ide_bus_ok[bus] = 1;
749 }
Stefan Roese29caf932022-09-02 14:10:46 +0200750 schedule();
Simon Glasse9be1ee2016-05-01 11:36:10 -0600751 }
752
753 putc('\n');
754
Simon Glasse9be1ee2016-05-01 11:36:10 -0600755 for (i = 0; i < CONFIG_SYS_IDE_MAXDEVICE; ++i) {
Simon Glasse9be1ee2016-05-01 11:36:10 -0600756 ide_dev_desc[i].type = DEV_TYPE_UNKNOWN;
Simon Glass8149b152022-09-17 09:00:09 -0600757 ide_dev_desc[i].uclass_id = UCLASS_IDE;
Simon Glasse9be1ee2016-05-01 11:36:10 -0600758 ide_dev_desc[i].devnum = i;
759 ide_dev_desc[i].part_type = PART_TYPE_UNKNOWN;
760 ide_dev_desc[i].blksz = 0;
761 ide_dev_desc[i].log2blksz =
762 LOG2_INVALID(typeof(ide_dev_desc[i].log2blksz));
763 ide_dev_desc[i].lba = 0;
Simon Glasse9be1ee2016-05-01 11:36:10 -0600764 if (!ide_bus_ok[IDE_BUS(i)])
765 continue;
Simon Glasse9be1ee2016-05-01 11:36:10 -0600766 ide_ident(&ide_dev_desc[i]);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600767 dev_print(&ide_dev_desc[i]);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600768 }
Stefan Roese29caf932022-09-02 14:10:46 +0200769 schedule();
Bin Meng68e6f222017-09-10 05:12:51 -0700770
Bin Meng68e6f222017-09-10 05:12:51 -0700771 uclass_first_device(UCLASS_IDE, &dev);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600772}
773
Simon Glasse9be1ee2016-05-01 11:36:10 -0600774__weak void ide_input_swap_data(int dev, ulong *sect_buf, int words)
775{
Reinoud Zandijk0a527fd2021-02-24 17:44:42 +0100776 uintptr_t paddr = (ATA_CURR_BASE(dev) + ATA_DATA_REG);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600777 ushort *dbuf = (ushort *)sect_buf;
778
Reinoud Zandijk0a527fd2021-02-24 17:44:42 +0100779 debug("in input swap data base for read is %p\n", (void *)paddr);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600780
781 while (words--) {
Reinoud Zandijk0a527fd2021-02-24 17:44:42 +0100782 EIEIO;
783 *dbuf++ = be16_to_cpu(inw(paddr));
784 EIEIO;
785 *dbuf++ = be16_to_cpu(inw(paddr));
Simon Glasse9be1ee2016-05-01 11:36:10 -0600786 }
787}
Simon Glasse9be1ee2016-05-01 11:36:10 -0600788
Simon Glasse9be1ee2016-05-01 11:36:10 -0600789__weak void ide_output_data(int dev, const ulong *sect_buf, int words)
790{
Reinoud Zandijk0a527fd2021-02-24 17:44:42 +0100791 uintptr_t paddr = (ATA_CURR_BASE(dev) + ATA_DATA_REG);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600792 ushort *dbuf;
Simon Glasse9be1ee2016-05-01 11:36:10 -0600793
Simon Glasse9be1ee2016-05-01 11:36:10 -0600794 dbuf = (ushort *)sect_buf;
Simon Glasse9be1ee2016-05-01 11:36:10 -0600795 while (words--) {
796 EIEIO;
Reinoud Zandijk0a527fd2021-02-24 17:44:42 +0100797 outw(cpu_to_le16(*dbuf++), paddr);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600798 EIEIO;
Reinoud Zandijk0a527fd2021-02-24 17:44:42 +0100799 outw(cpu_to_le16(*dbuf++), paddr);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600800 }
801}
Reinoud Zandijk0a527fd2021-02-24 17:44:42 +0100802
Simon Glasse9be1ee2016-05-01 11:36:10 -0600803__weak void ide_input_data(int dev, ulong *sect_buf, int words)
804{
Reinoud Zandijk0a527fd2021-02-24 17:44:42 +0100805 uintptr_t paddr = (ATA_CURR_BASE(dev) + ATA_DATA_REG);
806 ushort *dbuf;
Simon Glasse9be1ee2016-05-01 11:36:10 -0600807
Reinoud Zandijk0a527fd2021-02-24 17:44:42 +0100808 dbuf = (ushort *)sect_buf;
809
810 debug("in input data base for read is %p\n", (void *)paddr);
811
812 while (words--) {
813 EIEIO;
814 *dbuf++ = le16_to_cpu(inw(paddr));
815 EIEIO;
816 *dbuf++ = le16_to_cpu(inw(paddr));
817 }
Reinoud Zandijk0a527fd2021-02-24 17:44:42 +0100818}
Simon Glasse9be1ee2016-05-01 11:36:10 -0600819
Simon Glass145df842016-05-01 11:36:22 -0600820ulong ide_read(struct udevice *dev, lbaint_t blknr, lbaint_t blkcnt,
821 void *buffer)
Simon Glasse9be1ee2016-05-01 11:36:10 -0600822{
Simon Glasscaa4daa2020-12-03 16:55:18 -0700823 struct blk_desc *block_dev = dev_get_uclass_plat(dev);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600824 int device = block_dev->devnum;
825 ulong n = 0;
826 unsigned char c;
827 unsigned char pwrsave = 0; /* power save */
828
829#ifdef CONFIG_LBA48
830 unsigned char lba48 = 0;
831
832 if (blknr & 0x0000fffff0000000ULL) {
833 /* more than 28 bits used, use 48bit mode */
834 lba48 = 1;
835 }
836#endif
837 debug("ide_read dev %d start " LBAF ", blocks " LBAF " buffer at %lX\n",
838 device, blknr, blkcnt, (ulong) buffer);
839
Simon Glasse9be1ee2016-05-01 11:36:10 -0600840 /* Select device
841 */
842 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
843 c = ide_wait(device, IDE_TIME_OUT);
844
845 if (c & ATA_STAT_BUSY) {
846 printf("IDE read: device %d not ready\n", device);
847 goto IDE_READ_E;
848 }
849
850 /* first check if the drive is in Powersaving mode, if yes,
851 * increase the timeout value */
Heinrich Schuchardte6e9a4f2020-02-27 18:28:00 +0100852 ide_outb(device, ATA_COMMAND, ATA_CMD_CHK_POWER);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600853 udelay(50);
854
855 c = ide_wait(device, IDE_TIME_OUT); /* can't take over 500 ms */
856
857 if (c & ATA_STAT_BUSY) {
858 printf("IDE read: device %d not ready\n", device);
859 goto IDE_READ_E;
860 }
861 if ((c & ATA_STAT_ERR) == ATA_STAT_ERR) {
862 printf("No Powersaving mode %X\n", c);
863 } else {
864 c = ide_inb(device, ATA_SECT_CNT);
865 debug("Powersaving %02X\n", c);
866 if (c == 0)
867 pwrsave = 1;
868 }
869
870
871 while (blkcnt-- > 0) {
872 c = ide_wait(device, IDE_TIME_OUT);
873
874 if (c & ATA_STAT_BUSY) {
875 printf("IDE read: device %d not ready\n", device);
876 break;
877 }
878#ifdef CONFIG_LBA48
879 if (lba48) {
880 /* write high bits */
881 ide_outb(device, ATA_SECT_CNT, 0);
882 ide_outb(device, ATA_LBA_LOW, (blknr >> 24) & 0xFF);
883#ifdef CONFIG_SYS_64BIT_LBA
884 ide_outb(device, ATA_LBA_MID, (blknr >> 32) & 0xFF);
885 ide_outb(device, ATA_LBA_HIGH, (blknr >> 40) & 0xFF);
886#else
887 ide_outb(device, ATA_LBA_MID, 0);
888 ide_outb(device, ATA_LBA_HIGH, 0);
889#endif
890 }
891#endif
892 ide_outb(device, ATA_SECT_CNT, 1);
893 ide_outb(device, ATA_LBA_LOW, (blknr >> 0) & 0xFF);
894 ide_outb(device, ATA_LBA_MID, (blknr >> 8) & 0xFF);
895 ide_outb(device, ATA_LBA_HIGH, (blknr >> 16) & 0xFF);
896
897#ifdef CONFIG_LBA48
898 if (lba48) {
899 ide_outb(device, ATA_DEV_HD,
900 ATA_LBA | ATA_DEVICE(device));
Reinoud Zandijk0a527fd2021-02-24 17:44:42 +0100901 ide_outb(device, ATA_COMMAND, ATA_CMD_PIO_READ_EXT);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600902
903 } else
904#endif
905 {
906 ide_outb(device, ATA_DEV_HD, ATA_LBA |
907 ATA_DEVICE(device) | ((blknr >> 24) & 0xF));
Reinoud Zandijk0a527fd2021-02-24 17:44:42 +0100908 ide_outb(device, ATA_COMMAND, ATA_CMD_PIO_READ);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600909 }
910
911 udelay(50);
912
913 if (pwrsave) {
914 /* may take up to 4 sec */
915 c = ide_wait(device, IDE_SPIN_UP_TIME_OUT);
916 pwrsave = 0;
917 } else {
918 /* can't take over 500 ms */
919 c = ide_wait(device, IDE_TIME_OUT);
920 }
921
922 if ((c & (ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR)) !=
923 ATA_STAT_DRQ) {
924 printf("Error (no IRQ) dev %d blk " LBAF
925 ": status %#02x\n", device, blknr, c);
926 break;
927 }
928
929 ide_input_data(device, buffer, ATA_SECTORWORDS);
930 (void) ide_inb(device, ATA_STATUS); /* clear IRQ */
931
932 ++n;
933 ++blknr;
934 buffer += ATA_BLOCKSIZE;
935 }
936IDE_READ_E:
Simon Glasse9be1ee2016-05-01 11:36:10 -0600937 return n;
938}
939
Simon Glass145df842016-05-01 11:36:22 -0600940ulong ide_write(struct udevice *dev, lbaint_t blknr, lbaint_t blkcnt,
941 const void *buffer)
Simon Glasse9be1ee2016-05-01 11:36:10 -0600942{
Simon Glasscaa4daa2020-12-03 16:55:18 -0700943 struct blk_desc *block_dev = dev_get_uclass_plat(dev);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600944 int device = block_dev->devnum;
945 ulong n = 0;
946 unsigned char c;
947
948#ifdef CONFIG_LBA48
949 unsigned char lba48 = 0;
950
951 if (blknr & 0x0000fffff0000000ULL) {
952 /* more than 28 bits used, use 48bit mode */
953 lba48 = 1;
954 }
955#endif
956
Simon Glasse9be1ee2016-05-01 11:36:10 -0600957 /* Select device
958 */
959 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
960
961 while (blkcnt-- > 0) {
962 c = ide_wait(device, IDE_TIME_OUT);
963
964 if (c & ATA_STAT_BUSY) {
965 printf("IDE read: device %d not ready\n", device);
966 goto WR_OUT;
967 }
968#ifdef CONFIG_LBA48
969 if (lba48) {
970 /* write high bits */
971 ide_outb(device, ATA_SECT_CNT, 0);
972 ide_outb(device, ATA_LBA_LOW, (blknr >> 24) & 0xFF);
973#ifdef CONFIG_SYS_64BIT_LBA
974 ide_outb(device, ATA_LBA_MID, (blknr >> 32) & 0xFF);
975 ide_outb(device, ATA_LBA_HIGH, (blknr >> 40) & 0xFF);
976#else
977 ide_outb(device, ATA_LBA_MID, 0);
978 ide_outb(device, ATA_LBA_HIGH, 0);
979#endif
980 }
981#endif
982 ide_outb(device, ATA_SECT_CNT, 1);
983 ide_outb(device, ATA_LBA_LOW, (blknr >> 0) & 0xFF);
984 ide_outb(device, ATA_LBA_MID, (blknr >> 8) & 0xFF);
985 ide_outb(device, ATA_LBA_HIGH, (blknr >> 16) & 0xFF);
986
987#ifdef CONFIG_LBA48
988 if (lba48) {
989 ide_outb(device, ATA_DEV_HD,
990 ATA_LBA | ATA_DEVICE(device));
Reinoud Zandijk0a527fd2021-02-24 17:44:42 +0100991 ide_outb(device, ATA_COMMAND, ATA_CMD_PIO_WRITE_EXT);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600992
993 } else
994#endif
995 {
996 ide_outb(device, ATA_DEV_HD, ATA_LBA |
997 ATA_DEVICE(device) | ((blknr >> 24) & 0xF));
Reinoud Zandijk0a527fd2021-02-24 17:44:42 +0100998 ide_outb(device, ATA_COMMAND, ATA_CMD_PIO_WRITE);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600999 }
1000
1001 udelay(50);
1002
1003 /* can't take over 500 ms */
1004 c = ide_wait(device, IDE_TIME_OUT);
1005
1006 if ((c & (ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR)) !=
1007 ATA_STAT_DRQ) {
1008 printf("Error (no IRQ) dev %d blk " LBAF
1009 ": status %#02x\n", device, blknr, c);
1010 goto WR_OUT;
1011 }
1012
1013 ide_output_data(device, buffer, ATA_SECTORWORDS);
1014 c = ide_inb(device, ATA_STATUS); /* clear IRQ */
1015 ++n;
1016 ++blknr;
1017 buffer += ATA_BLOCKSIZE;
1018 }
1019WR_OUT:
Simon Glasse9be1ee2016-05-01 11:36:10 -06001020 return n;
1021}
1022
1023#if defined(CONFIG_OF_IDE_FIXUP)
1024int ide_device_present(int dev)
1025{
1026 if (dev >= CONFIG_SYS_IDE_MAXBUS)
1027 return 0;
1028 return ide_dev_desc[dev].type == DEV_TYPE_UNKNOWN ? 0 : 1;
1029}
1030#endif
1031
Bin Meng68e6f222017-09-10 05:12:51 -07001032static int ide_blk_probe(struct udevice *udev)
1033{
Simon Glasscaa4daa2020-12-03 16:55:18 -07001034 struct blk_desc *desc = dev_get_uclass_plat(udev);
Bin Meng68e6f222017-09-10 05:12:51 -07001035
1036 /* fill in device vendor/product/rev strings */
1037 strncpy(desc->vendor, ide_dev_desc[desc->devnum].vendor,
1038 BLK_VEN_SIZE);
1039 desc->vendor[BLK_VEN_SIZE] = '\0';
1040 strncpy(desc->product, ide_dev_desc[desc->devnum].product,
1041 BLK_PRD_SIZE);
1042 desc->product[BLK_PRD_SIZE] = '\0';
1043 strncpy(desc->revision, ide_dev_desc[desc->devnum].revision,
1044 BLK_REV_SIZE);
1045 desc->revision[BLK_REV_SIZE] = '\0';
1046
Bin Meng68e6f222017-09-10 05:12:51 -07001047 return 0;
1048}
1049
Simon Glass145df842016-05-01 11:36:22 -06001050static const struct blk_ops ide_blk_ops = {
1051 .read = ide_read,
1052 .write = ide_write,
1053};
1054
1055U_BOOT_DRIVER(ide_blk) = {
1056 .name = "ide_blk",
1057 .id = UCLASS_BLK,
1058 .ops = &ide_blk_ops,
Bin Meng68e6f222017-09-10 05:12:51 -07001059 .probe = ide_blk_probe,
1060};
1061
Simon Glass0d77f8f2023-01-17 10:47:46 -07001062static int ide_bootdev_bind(struct udevice *dev)
1063{
1064 struct bootdev_uc_plat *ucp = dev_get_uclass_plat(dev);
1065
Simon Glasseacc2612023-01-17 10:48:08 -07001066 ucp->prio = BOOTDEVP_5_SCAN_SLOW;
Simon Glass0d77f8f2023-01-17 10:47:46 -07001067
1068 return 0;
1069}
1070
1071static int ide_bootdev_hunt(struct bootdev_hunter *info, bool show)
1072{
1073 ide_init();
1074
1075 return 0;
1076}
1077
1078struct bootdev_ops ide_bootdev_ops = {
1079};
1080
1081static const struct udevice_id ide_bootdev_ids[] = {
1082 { .compatible = "u-boot,bootdev-ide" },
1083 { }
1084};
1085
1086U_BOOT_DRIVER(ide_bootdev) = {
1087 .name = "ide_bootdev",
1088 .id = UCLASS_BOOTDEV,
1089 .ops = &ide_bootdev_ops,
1090 .bind = ide_bootdev_bind,
1091 .of_match = ide_bootdev_ids,
1092};
1093
1094BOOTDEV_HUNTER(ide_bootdev_hunter) = {
Simon Glasseacc2612023-01-17 10:48:08 -07001095 .prio = BOOTDEVP_5_SCAN_SLOW,
Simon Glass0d77f8f2023-01-17 10:47:46 -07001096 .uclass = UCLASS_IDE,
1097 .hunt = ide_bootdev_hunt,
1098 .drv = DM_DRIVER_REF(ide_bootdev),
1099};
1100
Bin Meng68e6f222017-09-10 05:12:51 -07001101static int ide_probe(struct udevice *udev)
1102{
1103 struct udevice *blk_dev;
1104 char name[20];
1105 int blksz;
1106 lbaint_t size;
1107 int i;
1108 int ret;
1109
1110 for (i = 0; i < CONFIG_SYS_IDE_MAXDEVICE; i++) {
1111 if (ide_dev_desc[i].type != DEV_TYPE_UNKNOWN) {
1112 sprintf(name, "blk#%d", i);
1113
1114 blksz = ide_dev_desc[i].blksz;
1115 size = blksz * ide_dev_desc[i].lba;
Bin Meng1f4adab2017-09-10 05:12:52 -07001116
1117 /*
1118 * With CDROM, if there is no CD inserted, blksz will
1119 * be zero, don't bother to create IDE block device.
1120 */
1121 if (!blksz)
1122 continue;
Bin Meng68e6f222017-09-10 05:12:51 -07001123 ret = blk_create_devicef(udev, "ide_blk", name,
Simon Glasse33a5c62022-08-11 19:34:59 -06001124 UCLASS_IDE, i,
Bin Meng68e6f222017-09-10 05:12:51 -07001125 blksz, size, &blk_dev);
1126 if (ret)
1127 return ret;
AKASHI Takahiro4c73b032022-03-08 20:36:44 +09001128
1129 ret = blk_probe_or_unbind(blk_dev);
1130 if (ret)
1131 return ret;
Simon Glass0d77f8f2023-01-17 10:47:46 -07001132
1133 ret = bootdev_setup_for_dev(udev, "ide_bootdev");
1134 if (ret)
1135 return log_msg_ret("bootdev", ret);
Bin Meng68e6f222017-09-10 05:12:51 -07001136 }
1137 }
1138
1139 return 0;
1140}
1141
1142U_BOOT_DRIVER(ide) = {
1143 .name = "ide",
1144 .id = UCLASS_IDE,
1145 .probe = ide_probe,
1146};
1147
1148struct pci_device_id ide_supported[] = {
1149 { PCI_DEVICE_CLASS(PCI_CLASS_STORAGE_IDE << 8, 0xffff00) },
1150 { }
1151};
1152
1153U_BOOT_PCI_DEVICE(ide, ide_supported);
1154
1155UCLASS_DRIVER(ide) = {
1156 .name = "ide",
1157 .id = UCLASS_IDE,
Simon Glass145df842016-05-01 11:36:22 -06001158};