blob: 1ad9b6c1267ef10c482af69b59e3c3d0cadd145f [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glasse11f0ef2016-05-01 11:36:10 -06002/*
3 * (C) Copyright 2000-2011
4 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
Simon Glasse11f0ef2016-05-01 11:36:10 -06005 */
6
Patrick Delaunay81313352021-04-27 11:02:19 +02007#define LOG_CATEGORY UCLASS_IDE
8
Simon Glasse11f0ef2016-05-01 11:36:10 -06009#include <common.h>
10#include <ata.h>
Simon Glass655306c2020-05-10 11:39:58 -060011#include <blk.h>
Simon Glassbe7c6662023-01-17 10:47:46 -070012#include <bootdev.h>
Simon Glassf94bea02016-05-01 11:36:22 -060013#include <dm.h>
Simon Glasse11f0ef2016-05-01 11:36:10 -060014#include <ide.h>
Simon Glass0f2af882020-05-10 11:40:05 -060015#include <log.h>
Simon Glass655306c2020-05-10 11:39:58 -060016#include <part.h>
Simon Glasse11f0ef2016-05-01 11:36:10 -060017#include <watchdog.h>
18#include <asm/io.h>
Simon Glassdbd79542020-05-10 11:40:11 -060019#include <linux/delay.h>
Simon Glasse11f0ef2016-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
39static int ide_bus_ok[CONFIG_SYS_IDE_MAXBUS];
40
41struct blk_desc ide_dev_desc[CONFIG_SYS_IDE_MAXDEVICE];
42
43#define IDE_TIME_OUT 2000 /* 2 sec timeout */
44
45#define ATAPI_TIME_OUT 7000 /* 7 sec timeout (5 sec seems to work...) */
46
47#define IDE_SPIN_UP_TIME_OUT 5000 /* 5 sec spin-up timeout */
48
Simon Glasse11f0ef2016-05-01 11:36:10 -060049#ifdef CONFIG_IDE_RESET
50extern void ide_set_reset(int idereset);
51
52static void ide_reset(void)
53{
54 int i;
55
56 for (i = 0; i < CONFIG_SYS_IDE_MAXBUS; ++i)
57 ide_bus_ok[i] = 0;
58 for (i = 0; i < CONFIG_SYS_IDE_MAXDEVICE; ++i)
59 ide_dev_desc[i].type = DEV_TYPE_UNKNOWN;
60
61 ide_set_reset(1); /* assert reset */
62
63 /* the reset signal shall be asserted for et least 25 us */
64 udelay(25);
65
Stefan Roese80877fa2022-09-02 14:10:46 +020066 schedule();
Simon Glasse11f0ef2016-05-01 11:36:10 -060067
68 /* de-assert RESET signal */
69 ide_set_reset(0);
70
71 /* wait 250 ms */
72 for (i = 0; i < 250; ++i)
73 udelay(1000);
74}
75#else
76#define ide_reset() /* dummy */
77#endif /* CONFIG_IDE_RESET */
78
79/*
80 * Wait until Busy bit is off, or timeout (in ms)
81 * Return last status
82 */
83static uchar ide_wait(int dev, ulong t)
84{
85 ulong delay = 10 * t; /* poll every 100 us */
86 uchar c;
87
88 while ((c = ide_inb(dev, ATA_STATUS)) & ATA_STAT_BUSY) {
89 udelay(100);
90 if (delay-- == 0)
91 break;
92 }
93 return c;
94}
95
96/*
97 * copy src to dest, skipping leading and trailing blanks and null
98 * terminate the string
99 * "len" is the size of available memory including the terminating '\0'
100 */
101static void ident_cpy(unsigned char *dst, unsigned char *src,
102 unsigned int len)
103{
104 unsigned char *end, *last;
105
106 last = dst;
107 end = src + len - 1;
108
109 /* reserve space for '\0' */
110 if (len < 2)
111 goto OUT;
112
113 /* skip leading white space */
114 while ((*src) && (src < end) && (*src == ' '))
115 ++src;
116
117 /* copy string, omitting trailing white space */
118 while ((*src) && (src < end)) {
119 *dst++ = *src;
120 if (*src++ != ' ')
121 last = dst;
122 }
123OUT:
124 *last = '\0';
125}
126
127#ifdef CONFIG_ATAPI
128/****************************************************************************
129 * ATAPI Support
130 */
131
Simon Glasse11f0ef2016-05-01 11:36:10 -0600132/* since ATAPI may use commands with not 4 bytes alligned length
133 * we have our own transfer functions, 2 bytes alligned */
134__weak void ide_output_data_shorts(int dev, ushort *sect_buf, int shorts)
135{
Reinoud Zandijk3b5dc942021-02-24 17:44:42 +0100136 uintptr_t paddr = (ATA_CURR_BASE(dev) + ATA_DATA_REG);
Simon Glasse11f0ef2016-05-01 11:36:10 -0600137 ushort *dbuf;
Simon Glasse11f0ef2016-05-01 11:36:10 -0600138
Simon Glasse11f0ef2016-05-01 11:36:10 -0600139 dbuf = (ushort *)sect_buf;
140
Reinoud Zandijk3b5dc942021-02-24 17:44:42 +0100141 debug("in output data shorts base for read is %p\n", (void *)paddr);
Simon Glasse11f0ef2016-05-01 11:36:10 -0600142
143 while (shorts--) {
144 EIEIO;
Reinoud Zandijk3b5dc942021-02-24 17:44:42 +0100145 outw(cpu_to_le16(*dbuf++), paddr);
Simon Glasse11f0ef2016-05-01 11:36:10 -0600146 }
147}
148
149__weak void ide_input_data_shorts(int dev, ushort *sect_buf, int shorts)
150{
Reinoud Zandijk3b5dc942021-02-24 17:44:42 +0100151 uintptr_t paddr = (ATA_CURR_BASE(dev) + ATA_DATA_REG);
Simon Glasse11f0ef2016-05-01 11:36:10 -0600152 ushort *dbuf;
Simon Glasse11f0ef2016-05-01 11:36:10 -0600153
Simon Glasse11f0ef2016-05-01 11:36:10 -0600154 dbuf = (ushort *)sect_buf;
155
Reinoud Zandijk3b5dc942021-02-24 17:44:42 +0100156 debug("in input data shorts base for read is %p\n", (void *)paddr);
Simon Glasse11f0ef2016-05-01 11:36:10 -0600157
158 while (shorts--) {
159 EIEIO;
Reinoud Zandijk3b5dc942021-02-24 17:44:42 +0100160 *dbuf++ = le16_to_cpu(inw(paddr));
Simon Glasse11f0ef2016-05-01 11:36:10 -0600161 }
162}
163
Simon Glasse11f0ef2016-05-01 11:36:10 -0600164/*
165 * Wait until (Status & mask) == res, or timeout (in ms)
166 * Return last status
167 * This is used since some ATAPI CD ROMs clears their Busy Bit first
168 * and then they set their DRQ Bit
169 */
170static uchar atapi_wait_mask(int dev, ulong t, uchar mask, uchar res)
171{
172 ulong delay = 10 * t; /* poll every 100 us */
173 uchar c;
174
175 /* prevents to read the status before valid */
176 c = ide_inb(dev, ATA_DEV_CTL);
177
178 while (((c = ide_inb(dev, ATA_STATUS)) & mask) != res) {
179 /* break if error occurs (doesn't make sense to wait more) */
180 if ((c & ATA_STAT_ERR) == ATA_STAT_ERR)
181 break;
182 udelay(100);
183 if (delay-- == 0)
184 break;
185 }
186 return c;
187}
188
189/*
190 * issue an atapi command
191 */
192unsigned char atapi_issue(int device, unsigned char *ccb, int ccblen,
193 unsigned char *buffer, int buflen)
194{
195 unsigned char c, err, mask, res;
196 int n;
197
Simon Glasse11f0ef2016-05-01 11:36:10 -0600198 /* Select device
199 */
200 mask = ATA_STAT_BUSY | ATA_STAT_DRQ;
201 res = 0;
202 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
203 c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
204 if ((c & mask) != res) {
205 printf("ATAPI_ISSUE: device %d not ready status %X\n", device,
206 c);
207 err = 0xFF;
208 goto AI_OUT;
209 }
210 /* write taskfile */
211 ide_outb(device, ATA_ERROR_REG, 0); /* no DMA, no overlaped */
212 ide_outb(device, ATA_SECT_CNT, 0);
213 ide_outb(device, ATA_SECT_NUM, 0);
214 ide_outb(device, ATA_CYL_LOW, (unsigned char) (buflen & 0xFF));
215 ide_outb(device, ATA_CYL_HIGH,
216 (unsigned char) ((buflen >> 8) & 0xFF));
217 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
218
Heinrich Schuchardt203d83a2020-02-27 18:28:00 +0100219 ide_outb(device, ATA_COMMAND, ATA_CMD_PACKET);
Simon Glasse11f0ef2016-05-01 11:36:10 -0600220 udelay(50);
221
222 mask = ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR;
223 res = ATA_STAT_DRQ;
224 c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
225
226 if ((c & mask) != res) { /* DRQ must be 1, BSY 0 */
227 printf("ATAPI_ISSUE: Error (no IRQ) before sending ccb dev %d status 0x%02x\n",
228 device, c);
229 err = 0xFF;
230 goto AI_OUT;
231 }
232
233 /* write command block */
234 ide_output_data_shorts(device, (unsigned short *)ccb, ccblen / 2);
235
236 /* ATAPI Command written wait for completition */
237 udelay(5000); /* device must set bsy */
238
239 mask = ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR;
240 /*
241 * if no data wait for DRQ = 0 BSY = 0
242 * if data wait for DRQ = 1 BSY = 0
243 */
244 res = 0;
245 if (buflen)
246 res = ATA_STAT_DRQ;
247 c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
248 if ((c & mask) != res) {
249 if (c & ATA_STAT_ERR) {
250 err = (ide_inb(device, ATA_ERROR_REG)) >> 4;
251 debug("atapi_issue 1 returned sense key %X status %02X\n",
252 err, c);
253 } else {
254 printf("ATAPI_ISSUE: (no DRQ) after sending ccb (%x) status 0x%02x\n",
255 ccb[0], c);
256 err = 0xFF;
257 }
258 goto AI_OUT;
259 }
260 n = ide_inb(device, ATA_CYL_HIGH);
261 n <<= 8;
262 n += ide_inb(device, ATA_CYL_LOW);
263 if (n > buflen) {
264 printf("ERROR, transfer bytes %d requested only %d\n", n,
265 buflen);
266 err = 0xff;
267 goto AI_OUT;
268 }
269 if ((n == 0) && (buflen < 0)) {
270 printf("ERROR, transfer bytes %d requested %d\n", n, buflen);
271 err = 0xff;
272 goto AI_OUT;
273 }
274 if (n != buflen) {
275 debug("WARNING, transfer bytes %d not equal with requested %d\n",
276 n, buflen);
277 }
278 if (n != 0) { /* data transfer */
279 debug("ATAPI_ISSUE: %d Bytes to transfer\n", n);
280 /* we transfer shorts */
281 n >>= 1;
282 /* ok now decide if it is an in or output */
283 if ((ide_inb(device, ATA_SECT_CNT) & 0x02) == 0) {
284 debug("Write to device\n");
285 ide_output_data_shorts(device, (unsigned short *)buffer,
286 n);
287 } else {
288 debug("Read from device @ %p shorts %d\n", buffer, n);
289 ide_input_data_shorts(device, (unsigned short *)buffer,
290 n);
291 }
292 }
293 udelay(5000); /* seems that some CD ROMs need this... */
294 mask = ATA_STAT_BUSY | ATA_STAT_ERR;
295 res = 0;
296 c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
297 if ((c & ATA_STAT_ERR) == ATA_STAT_ERR) {
298 err = (ide_inb(device, ATA_ERROR_REG) >> 4);
299 debug("atapi_issue 2 returned sense key %X status %X\n", err,
300 c);
301 } else {
302 err = 0;
303 }
304AI_OUT:
Simon Glasse11f0ef2016-05-01 11:36:10 -0600305 return err;
306}
307
308/*
309 * sending the command to atapi_issue. If an status other than good
310 * returns, an request_sense will be issued
311 */
312
313#define ATAPI_DRIVE_NOT_READY 100
314#define ATAPI_UNIT_ATTN 10
315
316unsigned char atapi_issue_autoreq(int device,
317 unsigned char *ccb,
318 int ccblen,
319 unsigned char *buffer, int buflen)
320{
321 unsigned char sense_data[18], sense_ccb[12];
322 unsigned char res, key, asc, ascq;
323 int notready, unitattn;
324
325 unitattn = ATAPI_UNIT_ATTN;
326 notready = ATAPI_DRIVE_NOT_READY;
327
328retry:
329 res = atapi_issue(device, ccb, ccblen, buffer, buflen);
330 if (res == 0)
331 return 0; /* Ok */
332
333 if (res == 0xFF)
334 return 0xFF; /* error */
335
336 debug("(auto_req)atapi_issue returned sense key %X\n", res);
337
338 memset(sense_ccb, 0, sizeof(sense_ccb));
339 memset(sense_data, 0, sizeof(sense_data));
340 sense_ccb[0] = ATAPI_CMD_REQ_SENSE;
341 sense_ccb[4] = 18; /* allocation Length */
342
343 res = atapi_issue(device, sense_ccb, 12, sense_data, 18);
344 key = (sense_data[2] & 0xF);
345 asc = (sense_data[12]);
346 ascq = (sense_data[13]);
347
348 debug("ATAPI_CMD_REQ_SENSE returned %x\n", res);
349 debug(" Sense page: %02X key %02X ASC %02X ASCQ %02X\n",
350 sense_data[0], key, asc, ascq);
351
352 if ((key == 0))
353 return 0; /* ok device ready */
354
355 if ((key == 6) || (asc == 0x29) || (asc == 0x28)) { /* Unit Attention */
356 if (unitattn-- > 0) {
357 udelay(200 * 1000);
358 goto retry;
359 }
360 printf("Unit Attention, tried %d\n", ATAPI_UNIT_ATTN);
361 goto error;
362 }
363 if ((asc == 0x4) && (ascq == 0x1)) {
364 /* not ready, but will be ready soon */
365 if (notready-- > 0) {
366 udelay(200 * 1000);
367 goto retry;
368 }
369 printf("Drive not ready, tried %d times\n",
370 ATAPI_DRIVE_NOT_READY);
371 goto error;
372 }
373 if (asc == 0x3a) {
374 debug("Media not present\n");
375 goto error;
376 }
377
378 printf("ERROR: Unknown Sense key %02X ASC %02X ASCQ %02X\n", key, asc,
379 ascq);
380error:
381 debug("ERROR Sense key %02X ASC %02X ASCQ %02X\n", key, asc, ascq);
382 return 0xFF;
383}
384
385/*
386 * atapi_read:
387 * we transfer only one block per command, since the multiple DRQ per
388 * command is not yet implemented
389 */
390#define ATAPI_READ_MAX_BYTES 2048 /* we read max 2kbytes */
391#define ATAPI_READ_BLOCK_SIZE 2048 /* assuming CD part */
392#define ATAPI_READ_MAX_BLOCK (ATAPI_READ_MAX_BYTES/ATAPI_READ_BLOCK_SIZE)
393
394ulong atapi_read(struct blk_desc *block_dev, lbaint_t blknr, lbaint_t blkcnt,
395 void *buffer)
396{
397 int device = block_dev->devnum;
398 ulong n = 0;
399 unsigned char ccb[12]; /* Command descriptor block */
400 ulong cnt;
401
402 debug("atapi_read dev %d start " LBAF " blocks " LBAF
403 " buffer at %lX\n", device, blknr, blkcnt, (ulong) buffer);
404
405 do {
406 if (blkcnt > ATAPI_READ_MAX_BLOCK)
407 cnt = ATAPI_READ_MAX_BLOCK;
408 else
409 cnt = blkcnt;
410
411 ccb[0] = ATAPI_CMD_READ_12;
412 ccb[1] = 0; /* reserved */
413 ccb[2] = (unsigned char) (blknr >> 24) & 0xFF; /* MSB Block */
414 ccb[3] = (unsigned char) (blknr >> 16) & 0xFF; /* */
415 ccb[4] = (unsigned char) (blknr >> 8) & 0xFF;
416 ccb[5] = (unsigned char) blknr & 0xFF; /* LSB Block */
417 ccb[6] = (unsigned char) (cnt >> 24) & 0xFF; /* MSB Block cnt */
418 ccb[7] = (unsigned char) (cnt >> 16) & 0xFF;
419 ccb[8] = (unsigned char) (cnt >> 8) & 0xFF;
420 ccb[9] = (unsigned char) cnt & 0xFF; /* LSB Block */
421 ccb[10] = 0; /* reserved */
422 ccb[11] = 0; /* reserved */
423
424 if (atapi_issue_autoreq(device, ccb, 12,
425 (unsigned char *)buffer,
426 cnt * ATAPI_READ_BLOCK_SIZE)
427 == 0xFF) {
428 return n;
429 }
430 n += cnt;
431 blkcnt -= cnt;
432 blknr += cnt;
433 buffer += (cnt * ATAPI_READ_BLOCK_SIZE);
434 } while (blkcnt > 0);
435 return n;
436}
437
438static void atapi_inquiry(struct blk_desc *dev_desc)
439{
440 unsigned char ccb[12]; /* Command descriptor block */
441 unsigned char iobuf[64]; /* temp buf */
442 unsigned char c;
443 int device;
444
445 device = dev_desc->devnum;
446 dev_desc->type = DEV_TYPE_UNKNOWN; /* not yet valid */
Simon Glasse11f0ef2016-05-01 11:36:10 -0600447
448 memset(ccb, 0, sizeof(ccb));
449 memset(iobuf, 0, sizeof(iobuf));
450
451 ccb[0] = ATAPI_CMD_INQUIRY;
452 ccb[4] = 40; /* allocation Legnth */
453 c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *)iobuf, 40);
454
455 debug("ATAPI_CMD_INQUIRY returned %x\n", c);
456 if (c != 0)
457 return;
458
459 /* copy device ident strings */
460 ident_cpy((unsigned char *)dev_desc->vendor, &iobuf[8], 8);
461 ident_cpy((unsigned char *)dev_desc->product, &iobuf[16], 16);
462 ident_cpy((unsigned char *)dev_desc->revision, &iobuf[32], 5);
463
464 dev_desc->lun = 0;
465 dev_desc->lba = 0;
466 dev_desc->blksz = 0;
467 dev_desc->log2blksz = LOG2_INVALID(typeof(dev_desc->log2blksz));
468 dev_desc->type = iobuf[0] & 0x1f;
469
470 if ((iobuf[1] & 0x80) == 0x80)
471 dev_desc->removable = 1;
472 else
473 dev_desc->removable = 0;
474
475 memset(ccb, 0, sizeof(ccb));
476 memset(iobuf, 0, sizeof(iobuf));
477 ccb[0] = ATAPI_CMD_START_STOP;
478 ccb[4] = 0x03; /* start */
479
480 c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *)iobuf, 0);
481
482 debug("ATAPI_CMD_START_STOP returned %x\n", c);
483 if (c != 0)
484 return;
485
486 memset(ccb, 0, sizeof(ccb));
487 memset(iobuf, 0, sizeof(iobuf));
488 c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *)iobuf, 0);
489
490 debug("ATAPI_CMD_UNIT_TEST_READY returned %x\n", c);
491 if (c != 0)
492 return;
493
494 memset(ccb, 0, sizeof(ccb));
495 memset(iobuf, 0, sizeof(iobuf));
496 ccb[0] = ATAPI_CMD_READ_CAP;
497 c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *)iobuf, 8);
498 debug("ATAPI_CMD_READ_CAP returned %x\n", c);
499 if (c != 0)
500 return;
501
502 debug("Read Cap: LBA %02X%02X%02X%02X blksize %02X%02X%02X%02X\n",
503 iobuf[0], iobuf[1], iobuf[2], iobuf[3],
504 iobuf[4], iobuf[5], iobuf[6], iobuf[7]);
505
506 dev_desc->lba = ((unsigned long) iobuf[0] << 24) +
507 ((unsigned long) iobuf[1] << 16) +
508 ((unsigned long) iobuf[2] << 8) + ((unsigned long) iobuf[3]);
509 dev_desc->blksz = ((unsigned long) iobuf[4] << 24) +
510 ((unsigned long) iobuf[5] << 16) +
511 ((unsigned long) iobuf[6] << 8) + ((unsigned long) iobuf[7]);
512 dev_desc->log2blksz = LOG2(dev_desc->blksz);
513#ifdef CONFIG_LBA48
514 /* ATAPI devices cannot use 48bit addressing (ATA/ATAPI v7) */
515 dev_desc->lba48 = 0;
516#endif
517 return;
518}
519
520#endif /* CONFIG_ATAPI */
521
522static void ide_ident(struct blk_desc *dev_desc)
523{
524 unsigned char c;
525 hd_driveid_t iop;
Simon Glasse11f0ef2016-05-01 11:36:10 -0600526#ifdef CONFIG_ATAPI
Simon Glass0cdb2932022-08-11 19:34:53 -0600527 bool is_atapi = false;
Simon Glasse11f0ef2016-05-01 11:36:10 -0600528 int retries = 0;
529#endif
530 int device;
531
532 device = dev_desc->devnum;
533 printf(" Device %d: ", device);
534
Simon Glasse11f0ef2016-05-01 11:36:10 -0600535 /* Select device
536 */
537 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
Simon Glassfada3f92022-09-17 09:00:09 -0600538 dev_desc->uclass_id = UCLASS_IDE;
Simon Glasse11f0ef2016-05-01 11:36:10 -0600539#ifdef CONFIG_ATAPI
540
541 retries = 0;
542
543 /* Warning: This will be tricky to read */
544 while (retries <= 1) {
545 /* check signature */
546 if ((ide_inb(device, ATA_SECT_CNT) == 0x01) &&
547 (ide_inb(device, ATA_SECT_NUM) == 0x01) &&
548 (ide_inb(device, ATA_CYL_LOW) == 0x14) &&
549 (ide_inb(device, ATA_CYL_HIGH) == 0xEB)) {
550 /* ATAPI Signature found */
Simon Glass0cdb2932022-08-11 19:34:53 -0600551 is_atapi = true;
Simon Glasse11f0ef2016-05-01 11:36:10 -0600552 /*
553 * Start Ident Command
554 */
Heinrich Schuchardt203d83a2020-02-27 18:28:00 +0100555 ide_outb(device, ATA_COMMAND, ATA_CMD_ID_ATAPI);
Simon Glasse11f0ef2016-05-01 11:36:10 -0600556 /*
557 * Wait for completion - ATAPI devices need more time
558 * to become ready
559 */
560 c = ide_wait(device, ATAPI_TIME_OUT);
561 } else
562#endif
563 {
564 /*
565 * Start Ident Command
566 */
Heinrich Schuchardt203d83a2020-02-27 18:28:00 +0100567 ide_outb(device, ATA_COMMAND, ATA_CMD_ID_ATA);
Simon Glasse11f0ef2016-05-01 11:36:10 -0600568
569 /*
570 * Wait for completion
571 */
572 c = ide_wait(device, IDE_TIME_OUT);
573 }
Simon Glasse11f0ef2016-05-01 11:36:10 -0600574
575 if (((c & ATA_STAT_DRQ) == 0) ||
576 ((c & (ATA_STAT_FAULT | ATA_STAT_ERR)) != 0)) {
577#ifdef CONFIG_ATAPI
578 {
579 /*
580 * Need to soft reset the device
581 * in case it's an ATAPI...
582 */
583 debug("Retrying...\n");
584 ide_outb(device, ATA_DEV_HD,
585 ATA_LBA | ATA_DEVICE(device));
586 udelay(100000);
587 ide_outb(device, ATA_COMMAND, 0x08);
588 udelay(500000); /* 500 ms */
589 }
590 /*
591 * Select device
592 */
593 ide_outb(device, ATA_DEV_HD,
594 ATA_LBA | ATA_DEVICE(device));
595 retries++;
596#else
597 return;
598#endif
599 }
600#ifdef CONFIG_ATAPI
601 else
602 break;
603 } /* see above - ugly to read */
604
605 if (retries == 2) /* Not found */
606 return;
607#endif
608
609 ide_input_swap_data(device, (ulong *)&iop, ATA_SECTORWORDS);
610
611 ident_cpy((unsigned char *)dev_desc->revision, iop.fw_rev,
612 sizeof(dev_desc->revision));
613 ident_cpy((unsigned char *)dev_desc->vendor, iop.model,
614 sizeof(dev_desc->vendor));
615 ident_cpy((unsigned char *)dev_desc->product, iop.serial_no,
616 sizeof(dev_desc->product));
Simon Glasse11f0ef2016-05-01 11:36:10 -0600617
618 if ((iop.config & 0x0080) == 0x0080)
619 dev_desc->removable = 1;
620 else
621 dev_desc->removable = 0;
622
623#ifdef CONFIG_ATAPI
Simon Glass0cdb2932022-08-11 19:34:53 -0600624 if (is_atapi) {
Simon Glasse11f0ef2016-05-01 11:36:10 -0600625 atapi_inquiry(dev_desc);
626 return;
627 }
628#endif /* CONFIG_ATAPI */
629
Reinoud Zandijk3b5dc942021-02-24 17:44:42 +0100630 iop.lba_capacity[0] = be16_to_cpu(iop.lba_capacity[0]);
631 iop.lba_capacity[1] = be16_to_cpu(iop.lba_capacity[1]);
632 dev_desc->lba =
633 ((unsigned long)iop.lba_capacity[0]) |
634 ((unsigned long)iop.lba_capacity[1] << 16);
Simon Glasse11f0ef2016-05-01 11:36:10 -0600635
636#ifdef CONFIG_LBA48
637 if (iop.command_set_2 & 0x0400) { /* LBA 48 support */
638 dev_desc->lba48 = 1;
Reinoud Zandijk3b5dc942021-02-24 17:44:42 +0100639 for (int i = 0; i < 4; i++)
640 iop.lba48_capacity[i] = be16_to_cpu(iop.lba48_capacity[i]);
641 dev_desc->lba =
642 ((unsigned long long)iop.lba48_capacity[0] |
643 ((unsigned long long)iop.lba48_capacity[1] << 16) |
644 ((unsigned long long)iop.lba48_capacity[2] << 32) |
645 ((unsigned long long)iop.lba48_capacity[3] << 48));
Simon Glasse11f0ef2016-05-01 11:36:10 -0600646 } else {
647 dev_desc->lba48 = 0;
648 }
649#endif /* CONFIG_LBA48 */
650 /* assuming HD */
651 dev_desc->type = DEV_TYPE_HARDDISK;
652 dev_desc->blksz = ATA_BLOCKSIZE;
653 dev_desc->log2blksz = LOG2(dev_desc->blksz);
654 dev_desc->lun = 0; /* just to fill something in... */
655
656#if 0 /* only used to test the powersaving mode,
657 * if enabled, the drive goes after 5 sec
658 * in standby mode */
659 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
660 c = ide_wait(device, IDE_TIME_OUT);
661 ide_outb(device, ATA_SECT_CNT, 1);
662 ide_outb(device, ATA_LBA_LOW, 0);
663 ide_outb(device, ATA_LBA_MID, 0);
664 ide_outb(device, ATA_LBA_HIGH, 0);
665 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
666 ide_outb(device, ATA_COMMAND, 0xe3);
667 udelay(50);
668 c = ide_wait(device, IDE_TIME_OUT); /* can't take over 500 ms */
669#endif
670}
671
Simon Glasse11f0ef2016-05-01 11:36:10 -0600672__weak void ide_outb(int dev, int port, unsigned char val)
673{
674 debug("ide_outb (dev= %d, port= 0x%x, val= 0x%02x) : @ 0x%08lx\n",
Simon Glass11056802021-11-24 09:26:48 -0700675 dev, port, val, ATA_CURR_BASE(dev) + port);
Simon Glasse11f0ef2016-05-01 11:36:10 -0600676
Simon Glass11056802021-11-24 09:26:48 -0700677 outb(val, ATA_CURR_BASE(dev) + port);
Simon Glasse11f0ef2016-05-01 11:36:10 -0600678}
679
680__weak unsigned char ide_inb(int dev, int port)
681{
682 uchar val;
683
Simon Glass11056802021-11-24 09:26:48 -0700684 val = inb(ATA_CURR_BASE(dev) + port);
Simon Glasse11f0ef2016-05-01 11:36:10 -0600685
686 debug("ide_inb (dev= %d, port= 0x%x) : @ 0x%08lx -> 0x%02x\n",
Simon Glass11056802021-11-24 09:26:48 -0700687 dev, port, ATA_CURR_BASE(dev) + port, val);
Simon Glasse11f0ef2016-05-01 11:36:10 -0600688 return val;
689}
690
691void ide_init(void)
692{
Simon Glass6e2bee32023-01-17 10:47:24 -0700693 struct udevice *dev;
Simon Glasse11f0ef2016-05-01 11:36:10 -0600694 unsigned char c;
695 int i, bus;
696
Stefan Roese80877fa2022-09-02 14:10:46 +0200697 schedule();
Simon Glasse11f0ef2016-05-01 11:36:10 -0600698
Simon Glasse11f0ef2016-05-01 11:36:10 -0600699 /* ATAPI Drives seems to need a proper IDE Reset */
700 ide_reset();
701
Simon Glasse11f0ef2016-05-01 11:36:10 -0600702 /*
703 * Wait for IDE to get ready.
704 * According to spec, this can take up to 31 seconds!
705 */
706 for (bus = 0; bus < CONFIG_SYS_IDE_MAXBUS; ++bus) {
707 int dev =
708 bus * (CONFIG_SYS_IDE_MAXDEVICE /
709 CONFIG_SYS_IDE_MAXBUS);
710
Simon Glasse11f0ef2016-05-01 11:36:10 -0600711 printf("Bus %d: ", bus);
712
713 ide_bus_ok[bus] = 0;
714
715 /* Select device
716 */
717 udelay(100000); /* 100 ms */
718 ide_outb(dev, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(dev));
719 udelay(100000); /* 100 ms */
720 i = 0;
721 do {
722 udelay(10000); /* 10 ms */
723
724 c = ide_inb(dev, ATA_STATUS);
725 i++;
726 if (i > (ATA_RESET_TIME * 100)) {
727 puts("** Timeout **\n");
Simon Glasse11f0ef2016-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 Roese80877fa2022-09-02 14:10:46 +0200747 schedule();
Simon Glasse11f0ef2016-05-01 11:36:10 -0600748 }
749
750 putc('\n');
751
Simon Glasse11f0ef2016-05-01 11:36:10 -0600752 for (i = 0; i < CONFIG_SYS_IDE_MAXDEVICE; ++i) {
Simon Glasse11f0ef2016-05-01 11:36:10 -0600753 ide_dev_desc[i].type = DEV_TYPE_UNKNOWN;
Simon Glassfada3f92022-09-17 09:00:09 -0600754 ide_dev_desc[i].uclass_id = UCLASS_IDE;
Simon Glasse11f0ef2016-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 Glasse11f0ef2016-05-01 11:36:10 -0600761 if (!ide_bus_ok[IDE_BUS(i)])
762 continue;
Simon Glasse11f0ef2016-05-01 11:36:10 -0600763 ide_ident(&ide_dev_desc[i]);
Simon Glasse11f0ef2016-05-01 11:36:10 -0600764 dev_print(&ide_dev_desc[i]);
Simon Glasse11f0ef2016-05-01 11:36:10 -0600765 }
Stefan Roese80877fa2022-09-02 14:10:46 +0200766 schedule();
Bin Mengb650afa2017-09-10 05:12:51 -0700767
Bin Mengb650afa2017-09-10 05:12:51 -0700768 uclass_first_device(UCLASS_IDE, &dev);
Simon Glasse11f0ef2016-05-01 11:36:10 -0600769}
770
Simon Glasse11f0ef2016-05-01 11:36:10 -0600771__weak void ide_input_swap_data(int dev, ulong *sect_buf, int words)
772{
Reinoud Zandijk3b5dc942021-02-24 17:44:42 +0100773 uintptr_t paddr = (ATA_CURR_BASE(dev) + ATA_DATA_REG);
Simon Glasse11f0ef2016-05-01 11:36:10 -0600774 ushort *dbuf = (ushort *)sect_buf;
775
Reinoud Zandijk3b5dc942021-02-24 17:44:42 +0100776 debug("in input swap data base for read is %p\n", (void *)paddr);
Simon Glasse11f0ef2016-05-01 11:36:10 -0600777
778 while (words--) {
Reinoud Zandijk3b5dc942021-02-24 17:44:42 +0100779 EIEIO;
780 *dbuf++ = be16_to_cpu(inw(paddr));
781 EIEIO;
782 *dbuf++ = be16_to_cpu(inw(paddr));
Simon Glasse11f0ef2016-05-01 11:36:10 -0600783 }
784}
Simon Glasse11f0ef2016-05-01 11:36:10 -0600785
Simon Glasse11f0ef2016-05-01 11:36:10 -0600786__weak void ide_output_data(int dev, const ulong *sect_buf, int words)
787{
Reinoud Zandijk3b5dc942021-02-24 17:44:42 +0100788 uintptr_t paddr = (ATA_CURR_BASE(dev) + ATA_DATA_REG);
Simon Glasse11f0ef2016-05-01 11:36:10 -0600789 ushort *dbuf;
Simon Glasse11f0ef2016-05-01 11:36:10 -0600790
Simon Glasse11f0ef2016-05-01 11:36:10 -0600791 dbuf = (ushort *)sect_buf;
792 while (words--) {
793 EIEIO;
Reinoud Zandijk3b5dc942021-02-24 17:44:42 +0100794 outw(cpu_to_le16(*dbuf++), paddr);
Simon Glasse11f0ef2016-05-01 11:36:10 -0600795 EIEIO;
Reinoud Zandijk3b5dc942021-02-24 17:44:42 +0100796 outw(cpu_to_le16(*dbuf++), paddr);
Simon Glasse11f0ef2016-05-01 11:36:10 -0600797 }
798}
Simon Glasse11f0ef2016-05-01 11:36:10 -0600799
Simon Glasse11f0ef2016-05-01 11:36:10 -0600800__weak void ide_input_data(int dev, ulong *sect_buf, int words)
801{
Reinoud Zandijk3b5dc942021-02-24 17:44:42 +0100802 uintptr_t paddr = (ATA_CURR_BASE(dev) + ATA_DATA_REG);
Simon Glasse11f0ef2016-05-01 11:36:10 -0600803 ushort *dbuf;
Simon Glasse11f0ef2016-05-01 11:36:10 -0600804
Simon Glasse11f0ef2016-05-01 11:36:10 -0600805 dbuf = (ushort *)sect_buf;
806
Reinoud Zandijk3b5dc942021-02-24 17:44:42 +0100807 debug("in input data base for read is %p\n", (void *)paddr);
Simon Glasse11f0ef2016-05-01 11:36:10 -0600808
809 while (words--) {
810 EIEIO;
Reinoud Zandijk3b5dc942021-02-24 17:44:42 +0100811 *dbuf++ = le16_to_cpu(inw(paddr));
Simon Glasse11f0ef2016-05-01 11:36:10 -0600812 EIEIO;
Reinoud Zandijk3b5dc942021-02-24 17:44:42 +0100813 *dbuf++ = le16_to_cpu(inw(paddr));
Simon Glasse11f0ef2016-05-01 11:36:10 -0600814 }
Simon Glasse11f0ef2016-05-01 11:36:10 -0600815}
816
Simon Glassf94bea02016-05-01 11:36:22 -0600817ulong ide_read(struct udevice *dev, lbaint_t blknr, lbaint_t blkcnt,
818 void *buffer)
Simon Glasse11f0ef2016-05-01 11:36:10 -0600819{
Simon Glass71fa5b42020-12-03 16:55:18 -0700820 struct blk_desc *block_dev = dev_get_uclass_plat(dev);
Simon Glasse11f0ef2016-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 Glasse11f0ef2016-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 Schuchardt203d83a2020-02-27 18:28:00 +0100849 ide_outb(device, ATA_COMMAND, ATA_CMD_CHK_POWER);
Simon Glasse11f0ef2016-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 Zandijk3b5dc942021-02-24 17:44:42 +0100898 ide_outb(device, ATA_COMMAND, ATA_CMD_PIO_READ_EXT);
Simon Glasse11f0ef2016-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 Zandijk3b5dc942021-02-24 17:44:42 +0100905 ide_outb(device, ATA_COMMAND, ATA_CMD_PIO_READ);
Simon Glasse11f0ef2016-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 Glasse11f0ef2016-05-01 11:36:10 -0600934 return n;
935}
936
Simon Glassf94bea02016-05-01 11:36:22 -0600937ulong ide_write(struct udevice *dev, lbaint_t blknr, lbaint_t blkcnt,
938 const void *buffer)
Simon Glasse11f0ef2016-05-01 11:36:10 -0600939{
Simon Glass71fa5b42020-12-03 16:55:18 -0700940 struct blk_desc *block_dev = dev_get_uclass_plat(dev);
Simon Glasse11f0ef2016-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 Glasse11f0ef2016-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 Zandijk3b5dc942021-02-24 17:44:42 +0100988 ide_outb(device, ATA_COMMAND, ATA_CMD_PIO_WRITE_EXT);
Simon Glasse11f0ef2016-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 Zandijk3b5dc942021-02-24 17:44:42 +0100995 ide_outb(device, ATA_COMMAND, ATA_CMD_PIO_WRITE);
Simon Glasse11f0ef2016-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 Glasse11f0ef2016-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 Mengb650afa2017-09-10 05:12:51 -07001029static int ide_blk_probe(struct udevice *udev)
1030{
Simon Glass71fa5b42020-12-03 16:55:18 -07001031 struct blk_desc *desc = dev_get_uclass_plat(udev);
Bin Mengb650afa2017-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 Mengb650afa2017-09-10 05:12:51 -07001044 return 0;
1045}
1046
Simon Glassf94bea02016-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 Mengb650afa2017-09-10 05:12:51 -07001056 .probe = ide_blk_probe,
1057};
1058
Simon Glassbe7c6662023-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 Glass7e1f6a42023-01-17 10:48:08 -07001063 ucp->prio = BOOTDEVP_5_SCAN_SLOW;
Simon Glassbe7c6662023-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 Glass7e1f6a42023-01-17 10:48:08 -07001092 .prio = BOOTDEVP_5_SCAN_SLOW,
Simon Glassbe7c6662023-01-17 10:47:46 -07001093 .uclass = UCLASS_IDE,
1094 .hunt = ide_bootdev_hunt,
1095 .drv = DM_DRIVER_REF(ide_bootdev),
1096};
1097
Bin Mengb650afa2017-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 Meng5781c462017-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 Mengb650afa2017-09-10 05:12:51 -07001120 ret = blk_create_devicef(udev, "ide_blk", name,
Simon Glassdbfa32c2022-08-11 19:34:59 -06001121 UCLASS_IDE, i,
Bin Mengb650afa2017-09-10 05:12:51 -07001122 blksz, size, &blk_dev);
1123 if (ret)
1124 return ret;
AKASHI Takahiroba748cf2022-03-08 20:36:44 +09001125
1126 ret = blk_probe_or_unbind(blk_dev);
1127 if (ret)
1128 return ret;
Simon Glassbe7c6662023-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 Mengb650afa2017-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 Glassf94bea02016-05-01 11:36:22 -06001155};