blob: 085aa356feef568ae16639984d0bf2847a152769 [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 Glass145df842016-05-01 11:36:22 -060012#include <dm.h>
Simon Glasse9be1ee2016-05-01 11:36:10 -060013#include <ide.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060014#include <log.h>
Simon Glasse6f6f9e2020-05-10 11:39:58 -060015#include <part.h>
Simon Glasse9be1ee2016-05-01 11:36:10 -060016#include <watchdog.h>
17#include <asm/io.h>
Simon Glassc05ed002020-05-10 11:40:11 -060018#include <linux/delay.h>
Simon Glasse9be1ee2016-05-01 11:36:10 -060019
20#ifdef __PPC__
21# define EIEIO __asm__ volatile ("eieio")
22# define SYNC __asm__ volatile ("sync")
23#else
24# define EIEIO /* nothing */
25# define SYNC /* nothing */
26#endif
27
28/* Current offset for IDE0 / IDE1 bus access */
29ulong ide_bus_offset[CONFIG_SYS_IDE_MAXBUS] = {
30#if defined(CONFIG_SYS_ATA_IDE0_OFFSET)
31 CONFIG_SYS_ATA_IDE0_OFFSET,
32#endif
33#if defined(CONFIG_SYS_ATA_IDE1_OFFSET) && (CONFIG_SYS_IDE_MAXBUS > 1)
34 CONFIG_SYS_ATA_IDE1_OFFSET,
35#endif
36};
37
38static int ide_bus_ok[CONFIG_SYS_IDE_MAXBUS];
39
40struct blk_desc ide_dev_desc[CONFIG_SYS_IDE_MAXDEVICE];
41
42#define IDE_TIME_OUT 2000 /* 2 sec timeout */
43
44#define ATAPI_TIME_OUT 7000 /* 7 sec timeout (5 sec seems to work...) */
45
46#define IDE_SPIN_UP_TIME_OUT 5000 /* 5 sec spin-up timeout */
47
Simon Glasse9be1ee2016-05-01 11:36:10 -060048#ifdef CONFIG_IDE_RESET
49extern void ide_set_reset(int idereset);
50
51static void ide_reset(void)
52{
53 int i;
54
55 for (i = 0; i < CONFIG_SYS_IDE_MAXBUS; ++i)
56 ide_bus_ok[i] = 0;
57 for (i = 0; i < CONFIG_SYS_IDE_MAXDEVICE; ++i)
58 ide_dev_desc[i].type = DEV_TYPE_UNKNOWN;
59
60 ide_set_reset(1); /* assert reset */
61
62 /* the reset signal shall be asserted for et least 25 us */
63 udelay(25);
64
65 WATCHDOG_RESET();
66
67 /* de-assert RESET signal */
68 ide_set_reset(0);
69
70 /* wait 250 ms */
71 for (i = 0; i < 250; ++i)
72 udelay(1000);
73}
74#else
75#define ide_reset() /* dummy */
76#endif /* CONFIG_IDE_RESET */
77
78/*
79 * Wait until Busy bit is off, or timeout (in ms)
80 * Return last status
81 */
82static uchar ide_wait(int dev, ulong t)
83{
84 ulong delay = 10 * t; /* poll every 100 us */
85 uchar c;
86
87 while ((c = ide_inb(dev, ATA_STATUS)) & ATA_STAT_BUSY) {
88 udelay(100);
89 if (delay-- == 0)
90 break;
91 }
92 return c;
93}
94
95/*
96 * copy src to dest, skipping leading and trailing blanks and null
97 * terminate the string
98 * "len" is the size of available memory including the terminating '\0'
99 */
100static void ident_cpy(unsigned char *dst, unsigned char *src,
101 unsigned int len)
102{
103 unsigned char *end, *last;
104
105 last = dst;
106 end = src + len - 1;
107
108 /* reserve space for '\0' */
109 if (len < 2)
110 goto OUT;
111
112 /* skip leading white space */
113 while ((*src) && (src < end) && (*src == ' '))
114 ++src;
115
116 /* copy string, omitting trailing white space */
117 while ((*src) && (src < end)) {
118 *dst++ = *src;
119 if (*src++ != ' ')
120 last = dst;
121 }
122OUT:
123 *last = '\0';
124}
125
126#ifdef CONFIG_ATAPI
127/****************************************************************************
128 * ATAPI Support
129 */
130
Simon Glasse9be1ee2016-05-01 11:36:10 -0600131/* since ATAPI may use commands with not 4 bytes alligned length
132 * we have our own transfer functions, 2 bytes alligned */
133__weak void ide_output_data_shorts(int dev, ushort *sect_buf, int shorts)
134{
Reinoud Zandijk0a527fd2021-02-24 17:44:42 +0100135 uintptr_t paddr = (ATA_CURR_BASE(dev) + ATA_DATA_REG);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600136 ushort *dbuf;
Simon Glasse9be1ee2016-05-01 11:36:10 -0600137
Simon Glasse9be1ee2016-05-01 11:36:10 -0600138 dbuf = (ushort *)sect_buf;
139
Reinoud Zandijk0a527fd2021-02-24 17:44:42 +0100140 debug("in output data shorts base for read is %p\n", (void *)paddr);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600141
142 while (shorts--) {
143 EIEIO;
Reinoud Zandijk0a527fd2021-02-24 17:44:42 +0100144 outw(cpu_to_le16(*dbuf++), paddr);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600145 }
146}
147
148__weak void ide_input_data_shorts(int dev, ushort *sect_buf, int shorts)
149{
Reinoud Zandijk0a527fd2021-02-24 17:44:42 +0100150 uintptr_t paddr = (ATA_CURR_BASE(dev) + ATA_DATA_REG);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600151 ushort *dbuf;
Simon Glasse9be1ee2016-05-01 11:36:10 -0600152
Simon Glasse9be1ee2016-05-01 11:36:10 -0600153 dbuf = (ushort *)sect_buf;
154
Reinoud Zandijk0a527fd2021-02-24 17:44:42 +0100155 debug("in input data shorts base for read is %p\n", (void *)paddr);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600156
157 while (shorts--) {
158 EIEIO;
Reinoud Zandijk0a527fd2021-02-24 17:44:42 +0100159 *dbuf++ = le16_to_cpu(inw(paddr));
Simon Glasse9be1ee2016-05-01 11:36:10 -0600160 }
161}
162
Simon Glasse9be1ee2016-05-01 11:36:10 -0600163/*
164 * Wait until (Status & mask) == res, or timeout (in ms)
165 * Return last status
166 * This is used since some ATAPI CD ROMs clears their Busy Bit first
167 * and then they set their DRQ Bit
168 */
169static uchar atapi_wait_mask(int dev, ulong t, uchar mask, uchar res)
170{
171 ulong delay = 10 * t; /* poll every 100 us */
172 uchar c;
173
174 /* prevents to read the status before valid */
175 c = ide_inb(dev, ATA_DEV_CTL);
176
177 while (((c = ide_inb(dev, ATA_STATUS)) & mask) != res) {
178 /* break if error occurs (doesn't make sense to wait more) */
179 if ((c & ATA_STAT_ERR) == ATA_STAT_ERR)
180 break;
181 udelay(100);
182 if (delay-- == 0)
183 break;
184 }
185 return c;
186}
187
188/*
189 * issue an atapi command
190 */
191unsigned char atapi_issue(int device, unsigned char *ccb, int ccblen,
192 unsigned char *buffer, int buflen)
193{
194 unsigned char c, err, mask, res;
195 int n;
196
Simon Glasse9be1ee2016-05-01 11:36:10 -0600197 /* Select device
198 */
199 mask = ATA_STAT_BUSY | ATA_STAT_DRQ;
200 res = 0;
201 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
202 c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
203 if ((c & mask) != res) {
204 printf("ATAPI_ISSUE: device %d not ready status %X\n", device,
205 c);
206 err = 0xFF;
207 goto AI_OUT;
208 }
209 /* write taskfile */
210 ide_outb(device, ATA_ERROR_REG, 0); /* no DMA, no overlaped */
211 ide_outb(device, ATA_SECT_CNT, 0);
212 ide_outb(device, ATA_SECT_NUM, 0);
213 ide_outb(device, ATA_CYL_LOW, (unsigned char) (buflen & 0xFF));
214 ide_outb(device, ATA_CYL_HIGH,
215 (unsigned char) ((buflen >> 8) & 0xFF));
216 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
217
Heinrich Schuchardte6e9a4f2020-02-27 18:28:00 +0100218 ide_outb(device, ATA_COMMAND, ATA_CMD_PACKET);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600219 udelay(50);
220
221 mask = ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR;
222 res = ATA_STAT_DRQ;
223 c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
224
225 if ((c & mask) != res) { /* DRQ must be 1, BSY 0 */
226 printf("ATAPI_ISSUE: Error (no IRQ) before sending ccb dev %d status 0x%02x\n",
227 device, c);
228 err = 0xFF;
229 goto AI_OUT;
230 }
231
232 /* write command block */
233 ide_output_data_shorts(device, (unsigned short *)ccb, ccblen / 2);
234
235 /* ATAPI Command written wait for completition */
236 udelay(5000); /* device must set bsy */
237
238 mask = ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR;
239 /*
240 * if no data wait for DRQ = 0 BSY = 0
241 * if data wait for DRQ = 1 BSY = 0
242 */
243 res = 0;
244 if (buflen)
245 res = ATA_STAT_DRQ;
246 c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
247 if ((c & mask) != res) {
248 if (c & ATA_STAT_ERR) {
249 err = (ide_inb(device, ATA_ERROR_REG)) >> 4;
250 debug("atapi_issue 1 returned sense key %X status %02X\n",
251 err, c);
252 } else {
253 printf("ATAPI_ISSUE: (no DRQ) after sending ccb (%x) status 0x%02x\n",
254 ccb[0], c);
255 err = 0xFF;
256 }
257 goto AI_OUT;
258 }
259 n = ide_inb(device, ATA_CYL_HIGH);
260 n <<= 8;
261 n += ide_inb(device, ATA_CYL_LOW);
262 if (n > buflen) {
263 printf("ERROR, transfer bytes %d requested only %d\n", n,
264 buflen);
265 err = 0xff;
266 goto AI_OUT;
267 }
268 if ((n == 0) && (buflen < 0)) {
269 printf("ERROR, transfer bytes %d requested %d\n", n, buflen);
270 err = 0xff;
271 goto AI_OUT;
272 }
273 if (n != buflen) {
274 debug("WARNING, transfer bytes %d not equal with requested %d\n",
275 n, buflen);
276 }
277 if (n != 0) { /* data transfer */
278 debug("ATAPI_ISSUE: %d Bytes to transfer\n", n);
279 /* we transfer shorts */
280 n >>= 1;
281 /* ok now decide if it is an in or output */
282 if ((ide_inb(device, ATA_SECT_CNT) & 0x02) == 0) {
283 debug("Write to device\n");
284 ide_output_data_shorts(device, (unsigned short *)buffer,
285 n);
286 } else {
287 debug("Read from device @ %p shorts %d\n", buffer, n);
288 ide_input_data_shorts(device, (unsigned short *)buffer,
289 n);
290 }
291 }
292 udelay(5000); /* seems that some CD ROMs need this... */
293 mask = ATA_STAT_BUSY | ATA_STAT_ERR;
294 res = 0;
295 c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
296 if ((c & ATA_STAT_ERR) == ATA_STAT_ERR) {
297 err = (ide_inb(device, ATA_ERROR_REG) >> 4);
298 debug("atapi_issue 2 returned sense key %X status %X\n", err,
299 c);
300 } else {
301 err = 0;
302 }
303AI_OUT:
Simon Glasse9be1ee2016-05-01 11:36:10 -0600304 return err;
305}
306
307/*
308 * sending the command to atapi_issue. If an status other than good
309 * returns, an request_sense will be issued
310 */
311
312#define ATAPI_DRIVE_NOT_READY 100
313#define ATAPI_UNIT_ATTN 10
314
315unsigned char atapi_issue_autoreq(int device,
316 unsigned char *ccb,
317 int ccblen,
318 unsigned char *buffer, int buflen)
319{
320 unsigned char sense_data[18], sense_ccb[12];
321 unsigned char res, key, asc, ascq;
322 int notready, unitattn;
323
324 unitattn = ATAPI_UNIT_ATTN;
325 notready = ATAPI_DRIVE_NOT_READY;
326
327retry:
328 res = atapi_issue(device, ccb, ccblen, buffer, buflen);
329 if (res == 0)
330 return 0; /* Ok */
331
332 if (res == 0xFF)
333 return 0xFF; /* error */
334
335 debug("(auto_req)atapi_issue returned sense key %X\n", res);
336
337 memset(sense_ccb, 0, sizeof(sense_ccb));
338 memset(sense_data, 0, sizeof(sense_data));
339 sense_ccb[0] = ATAPI_CMD_REQ_SENSE;
340 sense_ccb[4] = 18; /* allocation Length */
341
342 res = atapi_issue(device, sense_ccb, 12, sense_data, 18);
343 key = (sense_data[2] & 0xF);
344 asc = (sense_data[12]);
345 ascq = (sense_data[13]);
346
347 debug("ATAPI_CMD_REQ_SENSE returned %x\n", res);
348 debug(" Sense page: %02X key %02X ASC %02X ASCQ %02X\n",
349 sense_data[0], key, asc, ascq);
350
351 if ((key == 0))
352 return 0; /* ok device ready */
353
354 if ((key == 6) || (asc == 0x29) || (asc == 0x28)) { /* Unit Attention */
355 if (unitattn-- > 0) {
356 udelay(200 * 1000);
357 goto retry;
358 }
359 printf("Unit Attention, tried %d\n", ATAPI_UNIT_ATTN);
360 goto error;
361 }
362 if ((asc == 0x4) && (ascq == 0x1)) {
363 /* not ready, but will be ready soon */
364 if (notready-- > 0) {
365 udelay(200 * 1000);
366 goto retry;
367 }
368 printf("Drive not ready, tried %d times\n",
369 ATAPI_DRIVE_NOT_READY);
370 goto error;
371 }
372 if (asc == 0x3a) {
373 debug("Media not present\n");
374 goto error;
375 }
376
377 printf("ERROR: Unknown Sense key %02X ASC %02X ASCQ %02X\n", key, asc,
378 ascq);
379error:
380 debug("ERROR Sense key %02X ASC %02X ASCQ %02X\n", key, asc, ascq);
381 return 0xFF;
382}
383
384/*
385 * atapi_read:
386 * we transfer only one block per command, since the multiple DRQ per
387 * command is not yet implemented
388 */
389#define ATAPI_READ_MAX_BYTES 2048 /* we read max 2kbytes */
390#define ATAPI_READ_BLOCK_SIZE 2048 /* assuming CD part */
391#define ATAPI_READ_MAX_BLOCK (ATAPI_READ_MAX_BYTES/ATAPI_READ_BLOCK_SIZE)
392
393ulong atapi_read(struct blk_desc *block_dev, lbaint_t blknr, lbaint_t blkcnt,
394 void *buffer)
395{
396 int device = block_dev->devnum;
397 ulong n = 0;
398 unsigned char ccb[12]; /* Command descriptor block */
399 ulong cnt;
400
401 debug("atapi_read dev %d start " LBAF " blocks " LBAF
402 " buffer at %lX\n", device, blknr, blkcnt, (ulong) buffer);
403
404 do {
405 if (blkcnt > ATAPI_READ_MAX_BLOCK)
406 cnt = ATAPI_READ_MAX_BLOCK;
407 else
408 cnt = blkcnt;
409
410 ccb[0] = ATAPI_CMD_READ_12;
411 ccb[1] = 0; /* reserved */
412 ccb[2] = (unsigned char) (blknr >> 24) & 0xFF; /* MSB Block */
413 ccb[3] = (unsigned char) (blknr >> 16) & 0xFF; /* */
414 ccb[4] = (unsigned char) (blknr >> 8) & 0xFF;
415 ccb[5] = (unsigned char) blknr & 0xFF; /* LSB Block */
416 ccb[6] = (unsigned char) (cnt >> 24) & 0xFF; /* MSB Block cnt */
417 ccb[7] = (unsigned char) (cnt >> 16) & 0xFF;
418 ccb[8] = (unsigned char) (cnt >> 8) & 0xFF;
419 ccb[9] = (unsigned char) cnt & 0xFF; /* LSB Block */
420 ccb[10] = 0; /* reserved */
421 ccb[11] = 0; /* reserved */
422
423 if (atapi_issue_autoreq(device, ccb, 12,
424 (unsigned char *)buffer,
425 cnt * ATAPI_READ_BLOCK_SIZE)
426 == 0xFF) {
427 return n;
428 }
429 n += cnt;
430 blkcnt -= cnt;
431 blknr += cnt;
432 buffer += (cnt * ATAPI_READ_BLOCK_SIZE);
433 } while (blkcnt > 0);
434 return n;
435}
436
437static void atapi_inquiry(struct blk_desc *dev_desc)
438{
439 unsigned char ccb[12]; /* Command descriptor block */
440 unsigned char iobuf[64]; /* temp buf */
441 unsigned char c;
442 int device;
443
444 device = dev_desc->devnum;
445 dev_desc->type = DEV_TYPE_UNKNOWN; /* not yet valid */
Bin Meng52a1c2c2017-07-30 19:24:00 -0700446#ifndef CONFIG_BLK
Simon Glasse9be1ee2016-05-01 11:36:10 -0600447 dev_desc->block_read = atapi_read;
Bin Meng52a1c2c2017-07-30 19:24:00 -0700448#endif
Simon Glasse9be1ee2016-05-01 11:36:10 -0600449
450 memset(ccb, 0, sizeof(ccb));
451 memset(iobuf, 0, sizeof(iobuf));
452
453 ccb[0] = ATAPI_CMD_INQUIRY;
454 ccb[4] = 40; /* allocation Legnth */
455 c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *)iobuf, 40);
456
457 debug("ATAPI_CMD_INQUIRY returned %x\n", c);
458 if (c != 0)
459 return;
460
461 /* copy device ident strings */
462 ident_cpy((unsigned char *)dev_desc->vendor, &iobuf[8], 8);
463 ident_cpy((unsigned char *)dev_desc->product, &iobuf[16], 16);
464 ident_cpy((unsigned char *)dev_desc->revision, &iobuf[32], 5);
465
466 dev_desc->lun = 0;
467 dev_desc->lba = 0;
468 dev_desc->blksz = 0;
469 dev_desc->log2blksz = LOG2_INVALID(typeof(dev_desc->log2blksz));
470 dev_desc->type = iobuf[0] & 0x1f;
471
472 if ((iobuf[1] & 0x80) == 0x80)
473 dev_desc->removable = 1;
474 else
475 dev_desc->removable = 0;
476
477 memset(ccb, 0, sizeof(ccb));
478 memset(iobuf, 0, sizeof(iobuf));
479 ccb[0] = ATAPI_CMD_START_STOP;
480 ccb[4] = 0x03; /* start */
481
482 c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *)iobuf, 0);
483
484 debug("ATAPI_CMD_START_STOP returned %x\n", c);
485 if (c != 0)
486 return;
487
488 memset(ccb, 0, sizeof(ccb));
489 memset(iobuf, 0, sizeof(iobuf));
490 c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *)iobuf, 0);
491
492 debug("ATAPI_CMD_UNIT_TEST_READY returned %x\n", c);
493 if (c != 0)
494 return;
495
496 memset(ccb, 0, sizeof(ccb));
497 memset(iobuf, 0, sizeof(iobuf));
498 ccb[0] = ATAPI_CMD_READ_CAP;
499 c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *)iobuf, 8);
500 debug("ATAPI_CMD_READ_CAP returned %x\n", c);
501 if (c != 0)
502 return;
503
504 debug("Read Cap: LBA %02X%02X%02X%02X blksize %02X%02X%02X%02X\n",
505 iobuf[0], iobuf[1], iobuf[2], iobuf[3],
506 iobuf[4], iobuf[5], iobuf[6], iobuf[7]);
507
508 dev_desc->lba = ((unsigned long) iobuf[0] << 24) +
509 ((unsigned long) iobuf[1] << 16) +
510 ((unsigned long) iobuf[2] << 8) + ((unsigned long) iobuf[3]);
511 dev_desc->blksz = ((unsigned long) iobuf[4] << 24) +
512 ((unsigned long) iobuf[5] << 16) +
513 ((unsigned long) iobuf[6] << 8) + ((unsigned long) iobuf[7]);
514 dev_desc->log2blksz = LOG2(dev_desc->blksz);
515#ifdef CONFIG_LBA48
516 /* ATAPI devices cannot use 48bit addressing (ATA/ATAPI v7) */
517 dev_desc->lba48 = 0;
518#endif
519 return;
520}
521
522#endif /* CONFIG_ATAPI */
523
524static void ide_ident(struct blk_desc *dev_desc)
525{
526 unsigned char c;
527 hd_driveid_t iop;
528
529#ifdef CONFIG_ATAPI
530 int retries = 0;
531#endif
532 int device;
533
534 device = dev_desc->devnum;
535 printf(" Device %d: ", device);
536
Simon Glasse9be1ee2016-05-01 11:36:10 -0600537 /* Select device
538 */
539 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
540 dev_desc->if_type = IF_TYPE_IDE;
541#ifdef CONFIG_ATAPI
542
543 retries = 0;
544
545 /* Warning: This will be tricky to read */
546 while (retries <= 1) {
547 /* check signature */
548 if ((ide_inb(device, ATA_SECT_CNT) == 0x01) &&
549 (ide_inb(device, ATA_SECT_NUM) == 0x01) &&
550 (ide_inb(device, ATA_CYL_LOW) == 0x14) &&
551 (ide_inb(device, ATA_CYL_HIGH) == 0xEB)) {
552 /* ATAPI Signature found */
553 dev_desc->if_type = IF_TYPE_ATAPI;
554 /*
555 * Start Ident Command
556 */
Heinrich Schuchardte6e9a4f2020-02-27 18:28:00 +0100557 ide_outb(device, ATA_COMMAND, ATA_CMD_ID_ATAPI);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600558 /*
559 * Wait for completion - ATAPI devices need more time
560 * to become ready
561 */
562 c = ide_wait(device, ATAPI_TIME_OUT);
563 } else
564#endif
565 {
566 /*
567 * Start Ident Command
568 */
Heinrich Schuchardte6e9a4f2020-02-27 18:28:00 +0100569 ide_outb(device, ATA_COMMAND, ATA_CMD_ID_ATA);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600570
571 /*
572 * Wait for completion
573 */
574 c = ide_wait(device, IDE_TIME_OUT);
575 }
Simon Glasse9be1ee2016-05-01 11:36:10 -0600576
577 if (((c & ATA_STAT_DRQ) == 0) ||
578 ((c & (ATA_STAT_FAULT | ATA_STAT_ERR)) != 0)) {
579#ifdef CONFIG_ATAPI
580 {
581 /*
582 * Need to soft reset the device
583 * in case it's an ATAPI...
584 */
585 debug("Retrying...\n");
586 ide_outb(device, ATA_DEV_HD,
587 ATA_LBA | ATA_DEVICE(device));
588 udelay(100000);
589 ide_outb(device, ATA_COMMAND, 0x08);
590 udelay(500000); /* 500 ms */
591 }
592 /*
593 * Select device
594 */
595 ide_outb(device, ATA_DEV_HD,
596 ATA_LBA | ATA_DEVICE(device));
597 retries++;
598#else
599 return;
600#endif
601 }
602#ifdef CONFIG_ATAPI
603 else
604 break;
605 } /* see above - ugly to read */
606
607 if (retries == 2) /* Not found */
608 return;
609#endif
610
611 ide_input_swap_data(device, (ulong *)&iop, ATA_SECTORWORDS);
612
613 ident_cpy((unsigned char *)dev_desc->revision, iop.fw_rev,
614 sizeof(dev_desc->revision));
615 ident_cpy((unsigned char *)dev_desc->vendor, iop.model,
616 sizeof(dev_desc->vendor));
617 ident_cpy((unsigned char *)dev_desc->product, iop.serial_no,
618 sizeof(dev_desc->product));
Simon Glasse9be1ee2016-05-01 11:36:10 -0600619
620 if ((iop.config & 0x0080) == 0x0080)
621 dev_desc->removable = 1;
622 else
623 dev_desc->removable = 0;
624
625#ifdef CONFIG_ATAPI
626 if (dev_desc->if_type == IF_TYPE_ATAPI) {
627 atapi_inquiry(dev_desc);
628 return;
629 }
630#endif /* CONFIG_ATAPI */
631
Reinoud Zandijk0a527fd2021-02-24 17:44:42 +0100632 iop.lba_capacity[0] = be16_to_cpu(iop.lba_capacity[0]);
633 iop.lba_capacity[1] = be16_to_cpu(iop.lba_capacity[1]);
634 dev_desc->lba =
635 ((unsigned long)iop.lba_capacity[0]) |
636 ((unsigned long)iop.lba_capacity[1] << 16);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600637
638#ifdef CONFIG_LBA48
639 if (iop.command_set_2 & 0x0400) { /* LBA 48 support */
640 dev_desc->lba48 = 1;
Reinoud Zandijk0a527fd2021-02-24 17:44:42 +0100641 for (int i = 0; i < 4; i++)
642 iop.lba48_capacity[i] = be16_to_cpu(iop.lba48_capacity[i]);
643 dev_desc->lba =
644 ((unsigned long long)iop.lba48_capacity[0] |
645 ((unsigned long long)iop.lba48_capacity[1] << 16) |
646 ((unsigned long long)iop.lba48_capacity[2] << 32) |
647 ((unsigned long long)iop.lba48_capacity[3] << 48));
Simon Glasse9be1ee2016-05-01 11:36:10 -0600648 } else {
649 dev_desc->lba48 = 0;
650 }
651#endif /* CONFIG_LBA48 */
652 /* assuming HD */
653 dev_desc->type = DEV_TYPE_HARDDISK;
654 dev_desc->blksz = ATA_BLOCKSIZE;
655 dev_desc->log2blksz = LOG2(dev_desc->blksz);
656 dev_desc->lun = 0; /* just to fill something in... */
657
658#if 0 /* only used to test the powersaving mode,
659 * if enabled, the drive goes after 5 sec
660 * in standby mode */
661 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
662 c = ide_wait(device, IDE_TIME_OUT);
663 ide_outb(device, ATA_SECT_CNT, 1);
664 ide_outb(device, ATA_LBA_LOW, 0);
665 ide_outb(device, ATA_LBA_MID, 0);
666 ide_outb(device, ATA_LBA_HIGH, 0);
667 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
668 ide_outb(device, ATA_COMMAND, 0xe3);
669 udelay(50);
670 c = ide_wait(device, IDE_TIME_OUT); /* can't take over 500 ms */
671#endif
672}
673
Simon Glasse9be1ee2016-05-01 11:36:10 -0600674__weak void ide_outb(int dev, int port, unsigned char val)
675{
676 debug("ide_outb (dev= %d, port= 0x%x, val= 0x%02x) : @ 0x%08lx\n",
Simon Glassc229cd22021-11-24 09:26:48 -0700677 dev, port, val, ATA_CURR_BASE(dev) + port);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600678
679#if defined(CONFIG_IDE_AHB)
680 if (port) {
681 /* write command */
682 ide_write_register(dev, port, val);
683 } else {
684 /* write data */
685 outb(val, (ATA_CURR_BASE(dev)));
686 }
687#else
Simon Glassc229cd22021-11-24 09:26:48 -0700688 outb(val, ATA_CURR_BASE(dev) + port);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600689#endif
690}
691
692__weak unsigned char ide_inb(int dev, int port)
693{
694 uchar val;
695
696#if defined(CONFIG_IDE_AHB)
697 val = ide_read_register(dev, port);
698#else
Simon Glassc229cd22021-11-24 09:26:48 -0700699 val = inb(ATA_CURR_BASE(dev) + port);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600700#endif
701
702 debug("ide_inb (dev= %d, port= 0x%x) : @ 0x%08lx -> 0x%02x\n",
Simon Glassc229cd22021-11-24 09:26:48 -0700703 dev, port, ATA_CURR_BASE(dev) + port, val);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600704 return val;
705}
706
707void ide_init(void)
708{
709 unsigned char c;
710 int i, bus;
711
Simon Glasse9be1ee2016-05-01 11:36:10 -0600712#ifdef CONFIG_IDE_PREINIT
713 WATCHDOG_RESET();
714
715 if (ide_preinit()) {
716 puts("ide_preinit failed\n");
717 return;
718 }
719#endif /* CONFIG_IDE_PREINIT */
720
721 WATCHDOG_RESET();
722
Simon Glasse9be1ee2016-05-01 11:36:10 -0600723 /* ATAPI Drives seems to need a proper IDE Reset */
724 ide_reset();
725
Simon Glasse9be1ee2016-05-01 11:36:10 -0600726 /*
727 * Wait for IDE to get ready.
728 * According to spec, this can take up to 31 seconds!
729 */
730 for (bus = 0; bus < CONFIG_SYS_IDE_MAXBUS; ++bus) {
731 int dev =
732 bus * (CONFIG_SYS_IDE_MAXDEVICE /
733 CONFIG_SYS_IDE_MAXBUS);
734
Simon Glasse9be1ee2016-05-01 11:36:10 -0600735 printf("Bus %d: ", bus);
736
737 ide_bus_ok[bus] = 0;
738
739 /* Select device
740 */
741 udelay(100000); /* 100 ms */
742 ide_outb(dev, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(dev));
743 udelay(100000); /* 100 ms */
744 i = 0;
745 do {
746 udelay(10000); /* 10 ms */
747
748 c = ide_inb(dev, ATA_STATUS);
749 i++;
750 if (i > (ATA_RESET_TIME * 100)) {
751 puts("** Timeout **\n");
Simon Glasse9be1ee2016-05-01 11:36:10 -0600752 return;
753 }
754 if ((i >= 100) && ((i % 100) == 0))
755 putc('.');
756
757 } while (c & ATA_STAT_BUSY);
758
759 if (c & (ATA_STAT_BUSY | ATA_STAT_FAULT)) {
760 puts("not available ");
761 debug("Status = 0x%02X ", c);
762#ifndef CONFIG_ATAPI /* ATAPI Devices do not set DRDY */
763 } else if ((c & ATA_STAT_READY) == 0) {
764 puts("not available ");
765 debug("Status = 0x%02X ", c);
766#endif
767 } else {
768 puts("OK ");
769 ide_bus_ok[bus] = 1;
770 }
771 WATCHDOG_RESET();
772 }
773
774 putc('\n');
775
Simon Glasse9be1ee2016-05-01 11:36:10 -0600776 for (i = 0; i < CONFIG_SYS_IDE_MAXDEVICE; ++i) {
Simon Glasse9be1ee2016-05-01 11:36:10 -0600777 ide_dev_desc[i].type = DEV_TYPE_UNKNOWN;
778 ide_dev_desc[i].if_type = IF_TYPE_IDE;
779 ide_dev_desc[i].devnum = i;
780 ide_dev_desc[i].part_type = PART_TYPE_UNKNOWN;
781 ide_dev_desc[i].blksz = 0;
782 ide_dev_desc[i].log2blksz =
783 LOG2_INVALID(typeof(ide_dev_desc[i].log2blksz));
784 ide_dev_desc[i].lba = 0;
Simon Glass145df842016-05-01 11:36:22 -0600785#ifndef CONFIG_BLK
Simon Glasse9be1ee2016-05-01 11:36:10 -0600786 ide_dev_desc[i].block_read = ide_read;
787 ide_dev_desc[i].block_write = ide_write;
Simon Glass145df842016-05-01 11:36:22 -0600788#endif
Simon Glasse9be1ee2016-05-01 11:36:10 -0600789 if (!ide_bus_ok[IDE_BUS(i)])
790 continue;
Simon Glasse9be1ee2016-05-01 11:36:10 -0600791 ide_ident(&ide_dev_desc[i]);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600792 dev_print(&ide_dev_desc[i]);
793
Bin Meng68e6f222017-09-10 05:12:51 -0700794#ifndef CONFIG_BLK
Simon Glasse9be1ee2016-05-01 11:36:10 -0600795 if ((ide_dev_desc[i].lba > 0) && (ide_dev_desc[i].blksz > 0)) {
796 /* initialize partition type */
797 part_init(&ide_dev_desc[i]);
798 }
Bin Meng68e6f222017-09-10 05:12:51 -0700799#endif
Simon Glasse9be1ee2016-05-01 11:36:10 -0600800 }
801 WATCHDOG_RESET();
Bin Meng68e6f222017-09-10 05:12:51 -0700802
803#ifdef CONFIG_BLK
804 struct udevice *dev;
805
806 uclass_first_device(UCLASS_IDE, &dev);
807#endif
Simon Glasse9be1ee2016-05-01 11:36:10 -0600808}
809
Simon Glasse9be1ee2016-05-01 11:36:10 -0600810__weak void ide_input_swap_data(int dev, ulong *sect_buf, int words)
811{
Reinoud Zandijk0a527fd2021-02-24 17:44:42 +0100812 uintptr_t paddr = (ATA_CURR_BASE(dev) + ATA_DATA_REG);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600813 ushort *dbuf = (ushort *)sect_buf;
814
Reinoud Zandijk0a527fd2021-02-24 17:44:42 +0100815 debug("in input swap data base for read is %p\n", (void *)paddr);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600816
817 while (words--) {
Reinoud Zandijk0a527fd2021-02-24 17:44:42 +0100818 EIEIO;
819 *dbuf++ = be16_to_cpu(inw(paddr));
820 EIEIO;
821 *dbuf++ = be16_to_cpu(inw(paddr));
Simon Glasse9be1ee2016-05-01 11:36:10 -0600822 }
823}
Simon Glasse9be1ee2016-05-01 11:36:10 -0600824
Simon Glasse9be1ee2016-05-01 11:36:10 -0600825__weak void ide_output_data(int dev, const ulong *sect_buf, int words)
826{
827#if defined(CONFIG_IDE_AHB)
828 ide_write_data(dev, sect_buf, words);
829#else
Reinoud Zandijk0a527fd2021-02-24 17:44:42 +0100830 uintptr_t paddr = (ATA_CURR_BASE(dev) + ATA_DATA_REG);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600831 ushort *dbuf;
Simon Glasse9be1ee2016-05-01 11:36:10 -0600832
Simon Glasse9be1ee2016-05-01 11:36:10 -0600833 dbuf = (ushort *)sect_buf;
Simon Glasse9be1ee2016-05-01 11:36:10 -0600834 while (words--) {
835 EIEIO;
Reinoud Zandijk0a527fd2021-02-24 17:44:42 +0100836 outw(cpu_to_le16(*dbuf++), paddr);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600837 EIEIO;
Reinoud Zandijk0a527fd2021-02-24 17:44:42 +0100838 outw(cpu_to_le16(*dbuf++), paddr);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600839 }
Reinoud Zandijk0a527fd2021-02-24 17:44:42 +0100840#endif /* CONFIG_IDE_AHB */
Simon Glasse9be1ee2016-05-01 11:36:10 -0600841}
Reinoud Zandijk0a527fd2021-02-24 17:44:42 +0100842
Simon Glasse9be1ee2016-05-01 11:36:10 -0600843__weak void ide_input_data(int dev, ulong *sect_buf, int words)
844{
845#if defined(CONFIG_IDE_AHB)
846 ide_read_data(dev, sect_buf, words);
847#else
Reinoud Zandijk0a527fd2021-02-24 17:44:42 +0100848 uintptr_t paddr = (ATA_CURR_BASE(dev) + ATA_DATA_REG);
849 ushort *dbuf;
Simon Glasse9be1ee2016-05-01 11:36:10 -0600850
Reinoud Zandijk0a527fd2021-02-24 17:44:42 +0100851 dbuf = (ushort *)sect_buf;
852
853 debug("in input data base for read is %p\n", (void *)paddr);
854
855 while (words--) {
856 EIEIO;
857 *dbuf++ = le16_to_cpu(inw(paddr));
858 EIEIO;
859 *dbuf++ = le16_to_cpu(inw(paddr));
860 }
861#endif /* CONFIG_IDE_AHB */
862}
Simon Glasse9be1ee2016-05-01 11:36:10 -0600863
Simon Glass145df842016-05-01 11:36:22 -0600864#ifdef CONFIG_BLK
865ulong ide_read(struct udevice *dev, lbaint_t blknr, lbaint_t blkcnt,
866 void *buffer)
867#else
Simon Glasse9be1ee2016-05-01 11:36:10 -0600868ulong ide_read(struct blk_desc *block_dev, lbaint_t blknr, lbaint_t blkcnt,
869 void *buffer)
Simon Glass145df842016-05-01 11:36:22 -0600870#endif
Simon Glasse9be1ee2016-05-01 11:36:10 -0600871{
Simon Glass145df842016-05-01 11:36:22 -0600872#ifdef CONFIG_BLK
Simon Glasscaa4daa2020-12-03 16:55:18 -0700873 struct blk_desc *block_dev = dev_get_uclass_plat(dev);
Simon Glass145df842016-05-01 11:36:22 -0600874#endif
Simon Glasse9be1ee2016-05-01 11:36:10 -0600875 int device = block_dev->devnum;
876 ulong n = 0;
877 unsigned char c;
878 unsigned char pwrsave = 0; /* power save */
879
880#ifdef CONFIG_LBA48
881 unsigned char lba48 = 0;
882
883 if (blknr & 0x0000fffff0000000ULL) {
884 /* more than 28 bits used, use 48bit mode */
885 lba48 = 1;
886 }
887#endif
888 debug("ide_read dev %d start " LBAF ", blocks " LBAF " buffer at %lX\n",
889 device, blknr, blkcnt, (ulong) buffer);
890
Simon Glasse9be1ee2016-05-01 11:36:10 -0600891 /* Select device
892 */
893 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
894 c = ide_wait(device, IDE_TIME_OUT);
895
896 if (c & ATA_STAT_BUSY) {
897 printf("IDE read: device %d not ready\n", device);
898 goto IDE_READ_E;
899 }
900
901 /* first check if the drive is in Powersaving mode, if yes,
902 * increase the timeout value */
Heinrich Schuchardte6e9a4f2020-02-27 18:28:00 +0100903 ide_outb(device, ATA_COMMAND, ATA_CMD_CHK_POWER);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600904 udelay(50);
905
906 c = ide_wait(device, IDE_TIME_OUT); /* can't take over 500 ms */
907
908 if (c & ATA_STAT_BUSY) {
909 printf("IDE read: device %d not ready\n", device);
910 goto IDE_READ_E;
911 }
912 if ((c & ATA_STAT_ERR) == ATA_STAT_ERR) {
913 printf("No Powersaving mode %X\n", c);
914 } else {
915 c = ide_inb(device, ATA_SECT_CNT);
916 debug("Powersaving %02X\n", c);
917 if (c == 0)
918 pwrsave = 1;
919 }
920
921
922 while (blkcnt-- > 0) {
923 c = ide_wait(device, IDE_TIME_OUT);
924
925 if (c & ATA_STAT_BUSY) {
926 printf("IDE read: device %d not ready\n", device);
927 break;
928 }
929#ifdef CONFIG_LBA48
930 if (lba48) {
931 /* write high bits */
932 ide_outb(device, ATA_SECT_CNT, 0);
933 ide_outb(device, ATA_LBA_LOW, (blknr >> 24) & 0xFF);
934#ifdef CONFIG_SYS_64BIT_LBA
935 ide_outb(device, ATA_LBA_MID, (blknr >> 32) & 0xFF);
936 ide_outb(device, ATA_LBA_HIGH, (blknr >> 40) & 0xFF);
937#else
938 ide_outb(device, ATA_LBA_MID, 0);
939 ide_outb(device, ATA_LBA_HIGH, 0);
940#endif
941 }
942#endif
943 ide_outb(device, ATA_SECT_CNT, 1);
944 ide_outb(device, ATA_LBA_LOW, (blknr >> 0) & 0xFF);
945 ide_outb(device, ATA_LBA_MID, (blknr >> 8) & 0xFF);
946 ide_outb(device, ATA_LBA_HIGH, (blknr >> 16) & 0xFF);
947
948#ifdef CONFIG_LBA48
949 if (lba48) {
950 ide_outb(device, ATA_DEV_HD,
951 ATA_LBA | ATA_DEVICE(device));
Reinoud Zandijk0a527fd2021-02-24 17:44:42 +0100952 ide_outb(device, ATA_COMMAND, ATA_CMD_PIO_READ_EXT);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600953
954 } else
955#endif
956 {
957 ide_outb(device, ATA_DEV_HD, ATA_LBA |
958 ATA_DEVICE(device) | ((blknr >> 24) & 0xF));
Reinoud Zandijk0a527fd2021-02-24 17:44:42 +0100959 ide_outb(device, ATA_COMMAND, ATA_CMD_PIO_READ);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600960 }
961
962 udelay(50);
963
964 if (pwrsave) {
965 /* may take up to 4 sec */
966 c = ide_wait(device, IDE_SPIN_UP_TIME_OUT);
967 pwrsave = 0;
968 } else {
969 /* can't take over 500 ms */
970 c = ide_wait(device, IDE_TIME_OUT);
971 }
972
973 if ((c & (ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR)) !=
974 ATA_STAT_DRQ) {
975 printf("Error (no IRQ) dev %d blk " LBAF
976 ": status %#02x\n", device, blknr, c);
977 break;
978 }
979
980 ide_input_data(device, buffer, ATA_SECTORWORDS);
981 (void) ide_inb(device, ATA_STATUS); /* clear IRQ */
982
983 ++n;
984 ++blknr;
985 buffer += ATA_BLOCKSIZE;
986 }
987IDE_READ_E:
Simon Glasse9be1ee2016-05-01 11:36:10 -0600988 return n;
989}
990
Simon Glass145df842016-05-01 11:36:22 -0600991#ifdef CONFIG_BLK
992ulong ide_write(struct udevice *dev, lbaint_t blknr, lbaint_t blkcnt,
993 const void *buffer)
994#else
Simon Glasse9be1ee2016-05-01 11:36:10 -0600995ulong ide_write(struct blk_desc *block_dev, lbaint_t blknr, lbaint_t blkcnt,
996 const void *buffer)
Simon Glass145df842016-05-01 11:36:22 -0600997#endif
Simon Glasse9be1ee2016-05-01 11:36:10 -0600998{
Simon Glass145df842016-05-01 11:36:22 -0600999#ifdef CONFIG_BLK
Simon Glasscaa4daa2020-12-03 16:55:18 -07001000 struct blk_desc *block_dev = dev_get_uclass_plat(dev);
Simon Glass145df842016-05-01 11:36:22 -06001001#endif
Simon Glasse9be1ee2016-05-01 11:36:10 -06001002 int device = block_dev->devnum;
1003 ulong n = 0;
1004 unsigned char c;
1005
1006#ifdef CONFIG_LBA48
1007 unsigned char lba48 = 0;
1008
1009 if (blknr & 0x0000fffff0000000ULL) {
1010 /* more than 28 bits used, use 48bit mode */
1011 lba48 = 1;
1012 }
1013#endif
1014
Simon Glasse9be1ee2016-05-01 11:36:10 -06001015 /* Select device
1016 */
1017 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
1018
1019 while (blkcnt-- > 0) {
1020 c = ide_wait(device, IDE_TIME_OUT);
1021
1022 if (c & ATA_STAT_BUSY) {
1023 printf("IDE read: device %d not ready\n", device);
1024 goto WR_OUT;
1025 }
1026#ifdef CONFIG_LBA48
1027 if (lba48) {
1028 /* write high bits */
1029 ide_outb(device, ATA_SECT_CNT, 0);
1030 ide_outb(device, ATA_LBA_LOW, (blknr >> 24) & 0xFF);
1031#ifdef CONFIG_SYS_64BIT_LBA
1032 ide_outb(device, ATA_LBA_MID, (blknr >> 32) & 0xFF);
1033 ide_outb(device, ATA_LBA_HIGH, (blknr >> 40) & 0xFF);
1034#else
1035 ide_outb(device, ATA_LBA_MID, 0);
1036 ide_outb(device, ATA_LBA_HIGH, 0);
1037#endif
1038 }
1039#endif
1040 ide_outb(device, ATA_SECT_CNT, 1);
1041 ide_outb(device, ATA_LBA_LOW, (blknr >> 0) & 0xFF);
1042 ide_outb(device, ATA_LBA_MID, (blknr >> 8) & 0xFF);
1043 ide_outb(device, ATA_LBA_HIGH, (blknr >> 16) & 0xFF);
1044
1045#ifdef CONFIG_LBA48
1046 if (lba48) {
1047 ide_outb(device, ATA_DEV_HD,
1048 ATA_LBA | ATA_DEVICE(device));
Reinoud Zandijk0a527fd2021-02-24 17:44:42 +01001049 ide_outb(device, ATA_COMMAND, ATA_CMD_PIO_WRITE_EXT);
Simon Glasse9be1ee2016-05-01 11:36:10 -06001050
1051 } else
1052#endif
1053 {
1054 ide_outb(device, ATA_DEV_HD, ATA_LBA |
1055 ATA_DEVICE(device) | ((blknr >> 24) & 0xF));
Reinoud Zandijk0a527fd2021-02-24 17:44:42 +01001056 ide_outb(device, ATA_COMMAND, ATA_CMD_PIO_WRITE);
Simon Glasse9be1ee2016-05-01 11:36:10 -06001057 }
1058
1059 udelay(50);
1060
1061 /* can't take over 500 ms */
1062 c = ide_wait(device, IDE_TIME_OUT);
1063
1064 if ((c & (ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR)) !=
1065 ATA_STAT_DRQ) {
1066 printf("Error (no IRQ) dev %d blk " LBAF
1067 ": status %#02x\n", device, blknr, c);
1068 goto WR_OUT;
1069 }
1070
1071 ide_output_data(device, buffer, ATA_SECTORWORDS);
1072 c = ide_inb(device, ATA_STATUS); /* clear IRQ */
1073 ++n;
1074 ++blknr;
1075 buffer += ATA_BLOCKSIZE;
1076 }
1077WR_OUT:
Simon Glasse9be1ee2016-05-01 11:36:10 -06001078 return n;
1079}
1080
1081#if defined(CONFIG_OF_IDE_FIXUP)
1082int ide_device_present(int dev)
1083{
1084 if (dev >= CONFIG_SYS_IDE_MAXBUS)
1085 return 0;
1086 return ide_dev_desc[dev].type == DEV_TYPE_UNKNOWN ? 0 : 1;
1087}
1088#endif
1089
Simon Glass145df842016-05-01 11:36:22 -06001090#ifdef CONFIG_BLK
Bin Meng68e6f222017-09-10 05:12:51 -07001091static int ide_blk_probe(struct udevice *udev)
1092{
Simon Glasscaa4daa2020-12-03 16:55:18 -07001093 struct blk_desc *desc = dev_get_uclass_plat(udev);
Bin Meng68e6f222017-09-10 05:12:51 -07001094
1095 /* fill in device vendor/product/rev strings */
1096 strncpy(desc->vendor, ide_dev_desc[desc->devnum].vendor,
1097 BLK_VEN_SIZE);
1098 desc->vendor[BLK_VEN_SIZE] = '\0';
1099 strncpy(desc->product, ide_dev_desc[desc->devnum].product,
1100 BLK_PRD_SIZE);
1101 desc->product[BLK_PRD_SIZE] = '\0';
1102 strncpy(desc->revision, ide_dev_desc[desc->devnum].revision,
1103 BLK_REV_SIZE);
1104 desc->revision[BLK_REV_SIZE] = '\0';
1105
Bin Meng68e6f222017-09-10 05:12:51 -07001106 return 0;
1107}
1108
Simon Glass145df842016-05-01 11:36:22 -06001109static const struct blk_ops ide_blk_ops = {
1110 .read = ide_read,
1111 .write = ide_write,
1112};
1113
1114U_BOOT_DRIVER(ide_blk) = {
1115 .name = "ide_blk",
1116 .id = UCLASS_BLK,
1117 .ops = &ide_blk_ops,
Bin Meng68e6f222017-09-10 05:12:51 -07001118 .probe = ide_blk_probe,
1119};
1120
1121static int ide_probe(struct udevice *udev)
1122{
1123 struct udevice *blk_dev;
1124 char name[20];
1125 int blksz;
1126 lbaint_t size;
1127 int i;
1128 int ret;
1129
1130 for (i = 0; i < CONFIG_SYS_IDE_MAXDEVICE; i++) {
1131 if (ide_dev_desc[i].type != DEV_TYPE_UNKNOWN) {
1132 sprintf(name, "blk#%d", i);
1133
1134 blksz = ide_dev_desc[i].blksz;
1135 size = blksz * ide_dev_desc[i].lba;
Bin Meng1f4adab2017-09-10 05:12:52 -07001136
1137 /*
1138 * With CDROM, if there is no CD inserted, blksz will
1139 * be zero, don't bother to create IDE block device.
1140 */
1141 if (!blksz)
1142 continue;
Bin Meng68e6f222017-09-10 05:12:51 -07001143 ret = blk_create_devicef(udev, "ide_blk", name,
1144 IF_TYPE_IDE, i,
1145 blksz, size, &blk_dev);
1146 if (ret)
1147 return ret;
1148 }
1149 }
1150
1151 return 0;
1152}
1153
1154U_BOOT_DRIVER(ide) = {
1155 .name = "ide",
1156 .id = UCLASS_IDE,
1157 .probe = ide_probe,
1158};
1159
1160struct pci_device_id ide_supported[] = {
1161 { PCI_DEVICE_CLASS(PCI_CLASS_STORAGE_IDE << 8, 0xffff00) },
1162 { }
1163};
1164
1165U_BOOT_PCI_DEVICE(ide, ide_supported);
1166
1167UCLASS_DRIVER(ide) = {
1168 .name = "ide",
1169 .id = UCLASS_IDE,
Simon Glass145df842016-05-01 11:36:22 -06001170};
1171#else
Simon Glasse9be1ee2016-05-01 11:36:10 -06001172U_BOOT_LEGACY_BLK(ide) = {
1173 .if_typename = "ide",
1174 .if_type = IF_TYPE_IDE,
1175 .max_devs = CONFIG_SYS_IDE_MAXDEVICE,
1176 .desc = ide_dev_desc,
1177};
Simon Glass145df842016-05-01 11:36:22 -06001178#endif