blob: 5b9fb821e065a1b3f907da640aa81b381f446da8 [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 Glass7ee20162021-11-24 09:26:47 -070048#define ATA_PORT_ADDR(port) (port)
Simon Glasse9be1ee2016-05-01 11:36:10 -060049
Simon Glasse9be1ee2016-05-01 11:36:10 -060050#ifdef CONFIG_IDE_RESET
51extern void ide_set_reset(int idereset);
52
53static void ide_reset(void)
54{
55 int i;
56
57 for (i = 0; i < CONFIG_SYS_IDE_MAXBUS; ++i)
58 ide_bus_ok[i] = 0;
59 for (i = 0; i < CONFIG_SYS_IDE_MAXDEVICE; ++i)
60 ide_dev_desc[i].type = DEV_TYPE_UNKNOWN;
61
62 ide_set_reset(1); /* assert reset */
63
64 /* the reset signal shall be asserted for et least 25 us */
65 udelay(25);
66
67 WATCHDOG_RESET();
68
69 /* de-assert RESET signal */
70 ide_set_reset(0);
71
72 /* wait 250 ms */
73 for (i = 0; i < 250; ++i)
74 udelay(1000);
75}
76#else
77#define ide_reset() /* dummy */
78#endif /* CONFIG_IDE_RESET */
79
80/*
81 * Wait until Busy bit is off, or timeout (in ms)
82 * Return last status
83 */
84static uchar ide_wait(int dev, ulong t)
85{
86 ulong delay = 10 * t; /* poll every 100 us */
87 uchar c;
88
89 while ((c = ide_inb(dev, ATA_STATUS)) & ATA_STAT_BUSY) {
90 udelay(100);
91 if (delay-- == 0)
92 break;
93 }
94 return c;
95}
96
97/*
98 * copy src to dest, skipping leading and trailing blanks and null
99 * terminate the string
100 * "len" is the size of available memory including the terminating '\0'
101 */
102static void ident_cpy(unsigned char *dst, unsigned char *src,
103 unsigned int len)
104{
105 unsigned char *end, *last;
106
107 last = dst;
108 end = src + len - 1;
109
110 /* reserve space for '\0' */
111 if (len < 2)
112 goto OUT;
113
114 /* skip leading white space */
115 while ((*src) && (src < end) && (*src == ' '))
116 ++src;
117
118 /* copy string, omitting trailing white space */
119 while ((*src) && (src < end)) {
120 *dst++ = *src;
121 if (*src++ != ' ')
122 last = dst;
123 }
124OUT:
125 *last = '\0';
126}
127
128#ifdef CONFIG_ATAPI
129/****************************************************************************
130 * ATAPI Support
131 */
132
Simon Glasse9be1ee2016-05-01 11:36:10 -0600133/* since ATAPI may use commands with not 4 bytes alligned length
134 * we have our own transfer functions, 2 bytes alligned */
135__weak void ide_output_data_shorts(int dev, ushort *sect_buf, int shorts)
136{
Reinoud Zandijk0a527fd2021-02-24 17:44:42 +0100137 uintptr_t paddr = (ATA_CURR_BASE(dev) + ATA_DATA_REG);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600138 ushort *dbuf;
Simon Glasse9be1ee2016-05-01 11:36:10 -0600139
Simon Glasse9be1ee2016-05-01 11:36:10 -0600140 dbuf = (ushort *)sect_buf;
141
Reinoud Zandijk0a527fd2021-02-24 17:44:42 +0100142 debug("in output data shorts base for read is %p\n", (void *)paddr);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600143
144 while (shorts--) {
145 EIEIO;
Reinoud Zandijk0a527fd2021-02-24 17:44:42 +0100146 outw(cpu_to_le16(*dbuf++), paddr);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600147 }
148}
149
150__weak void ide_input_data_shorts(int dev, ushort *sect_buf, int shorts)
151{
Reinoud Zandijk0a527fd2021-02-24 17:44:42 +0100152 uintptr_t paddr = (ATA_CURR_BASE(dev) + ATA_DATA_REG);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600153 ushort *dbuf;
Simon Glasse9be1ee2016-05-01 11:36:10 -0600154
Simon Glasse9be1ee2016-05-01 11:36:10 -0600155 dbuf = (ushort *)sect_buf;
156
Reinoud Zandijk0a527fd2021-02-24 17:44:42 +0100157 debug("in input data shorts base for read is %p\n", (void *)paddr);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600158
159 while (shorts--) {
160 EIEIO;
Reinoud Zandijk0a527fd2021-02-24 17:44:42 +0100161 *dbuf++ = le16_to_cpu(inw(paddr));
Simon Glasse9be1ee2016-05-01 11:36:10 -0600162 }
163}
164
Simon Glasse9be1ee2016-05-01 11:36:10 -0600165/*
166 * Wait until (Status & mask) == res, or timeout (in ms)
167 * Return last status
168 * This is used since some ATAPI CD ROMs clears their Busy Bit first
169 * and then they set their DRQ Bit
170 */
171static uchar atapi_wait_mask(int dev, ulong t, uchar mask, uchar res)
172{
173 ulong delay = 10 * t; /* poll every 100 us */
174 uchar c;
175
176 /* prevents to read the status before valid */
177 c = ide_inb(dev, ATA_DEV_CTL);
178
179 while (((c = ide_inb(dev, ATA_STATUS)) & mask) != res) {
180 /* break if error occurs (doesn't make sense to wait more) */
181 if ((c & ATA_STAT_ERR) == ATA_STAT_ERR)
182 break;
183 udelay(100);
184 if (delay-- == 0)
185 break;
186 }
187 return c;
188}
189
190/*
191 * issue an atapi command
192 */
193unsigned char atapi_issue(int device, unsigned char *ccb, int ccblen,
194 unsigned char *buffer, int buflen)
195{
196 unsigned char c, err, mask, res;
197 int n;
198
Simon Glasse9be1ee2016-05-01 11:36:10 -0600199 /* Select device
200 */
201 mask = ATA_STAT_BUSY | ATA_STAT_DRQ;
202 res = 0;
203 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
204 c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
205 if ((c & mask) != res) {
206 printf("ATAPI_ISSUE: device %d not ready status %X\n", device,
207 c);
208 err = 0xFF;
209 goto AI_OUT;
210 }
211 /* write taskfile */
212 ide_outb(device, ATA_ERROR_REG, 0); /* no DMA, no overlaped */
213 ide_outb(device, ATA_SECT_CNT, 0);
214 ide_outb(device, ATA_SECT_NUM, 0);
215 ide_outb(device, ATA_CYL_LOW, (unsigned char) (buflen & 0xFF));
216 ide_outb(device, ATA_CYL_HIGH,
217 (unsigned char) ((buflen >> 8) & 0xFF));
218 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
219
Heinrich Schuchardte6e9a4f2020-02-27 18:28:00 +0100220 ide_outb(device, ATA_COMMAND, ATA_CMD_PACKET);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600221 udelay(50);
222
223 mask = ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR;
224 res = ATA_STAT_DRQ;
225 c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
226
227 if ((c & mask) != res) { /* DRQ must be 1, BSY 0 */
228 printf("ATAPI_ISSUE: Error (no IRQ) before sending ccb dev %d status 0x%02x\n",
229 device, c);
230 err = 0xFF;
231 goto AI_OUT;
232 }
233
234 /* write command block */
235 ide_output_data_shorts(device, (unsigned short *)ccb, ccblen / 2);
236
237 /* ATAPI Command written wait for completition */
238 udelay(5000); /* device must set bsy */
239
240 mask = ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR;
241 /*
242 * if no data wait for DRQ = 0 BSY = 0
243 * if data wait for DRQ = 1 BSY = 0
244 */
245 res = 0;
246 if (buflen)
247 res = ATA_STAT_DRQ;
248 c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
249 if ((c & mask) != res) {
250 if (c & ATA_STAT_ERR) {
251 err = (ide_inb(device, ATA_ERROR_REG)) >> 4;
252 debug("atapi_issue 1 returned sense key %X status %02X\n",
253 err, c);
254 } else {
255 printf("ATAPI_ISSUE: (no DRQ) after sending ccb (%x) status 0x%02x\n",
256 ccb[0], c);
257 err = 0xFF;
258 }
259 goto AI_OUT;
260 }
261 n = ide_inb(device, ATA_CYL_HIGH);
262 n <<= 8;
263 n += ide_inb(device, ATA_CYL_LOW);
264 if (n > buflen) {
265 printf("ERROR, transfer bytes %d requested only %d\n", n,
266 buflen);
267 err = 0xff;
268 goto AI_OUT;
269 }
270 if ((n == 0) && (buflen < 0)) {
271 printf("ERROR, transfer bytes %d requested %d\n", n, buflen);
272 err = 0xff;
273 goto AI_OUT;
274 }
275 if (n != buflen) {
276 debug("WARNING, transfer bytes %d not equal with requested %d\n",
277 n, buflen);
278 }
279 if (n != 0) { /* data transfer */
280 debug("ATAPI_ISSUE: %d Bytes to transfer\n", n);
281 /* we transfer shorts */
282 n >>= 1;
283 /* ok now decide if it is an in or output */
284 if ((ide_inb(device, ATA_SECT_CNT) & 0x02) == 0) {
285 debug("Write to device\n");
286 ide_output_data_shorts(device, (unsigned short *)buffer,
287 n);
288 } else {
289 debug("Read from device @ %p shorts %d\n", buffer, n);
290 ide_input_data_shorts(device, (unsigned short *)buffer,
291 n);
292 }
293 }
294 udelay(5000); /* seems that some CD ROMs need this... */
295 mask = ATA_STAT_BUSY | ATA_STAT_ERR;
296 res = 0;
297 c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
298 if ((c & ATA_STAT_ERR) == ATA_STAT_ERR) {
299 err = (ide_inb(device, ATA_ERROR_REG) >> 4);
300 debug("atapi_issue 2 returned sense key %X status %X\n", err,
301 c);
302 } else {
303 err = 0;
304 }
305AI_OUT:
Simon Glasse9be1ee2016-05-01 11:36:10 -0600306 return err;
307}
308
309/*
310 * sending the command to atapi_issue. If an status other than good
311 * returns, an request_sense will be issued
312 */
313
314#define ATAPI_DRIVE_NOT_READY 100
315#define ATAPI_UNIT_ATTN 10
316
317unsigned char atapi_issue_autoreq(int device,
318 unsigned char *ccb,
319 int ccblen,
320 unsigned char *buffer, int buflen)
321{
322 unsigned char sense_data[18], sense_ccb[12];
323 unsigned char res, key, asc, ascq;
324 int notready, unitattn;
325
326 unitattn = ATAPI_UNIT_ATTN;
327 notready = ATAPI_DRIVE_NOT_READY;
328
329retry:
330 res = atapi_issue(device, ccb, ccblen, buffer, buflen);
331 if (res == 0)
332 return 0; /* Ok */
333
334 if (res == 0xFF)
335 return 0xFF; /* error */
336
337 debug("(auto_req)atapi_issue returned sense key %X\n", res);
338
339 memset(sense_ccb, 0, sizeof(sense_ccb));
340 memset(sense_data, 0, sizeof(sense_data));
341 sense_ccb[0] = ATAPI_CMD_REQ_SENSE;
342 sense_ccb[4] = 18; /* allocation Length */
343
344 res = atapi_issue(device, sense_ccb, 12, sense_data, 18);
345 key = (sense_data[2] & 0xF);
346 asc = (sense_data[12]);
347 ascq = (sense_data[13]);
348
349 debug("ATAPI_CMD_REQ_SENSE returned %x\n", res);
350 debug(" Sense page: %02X key %02X ASC %02X ASCQ %02X\n",
351 sense_data[0], key, asc, ascq);
352
353 if ((key == 0))
354 return 0; /* ok device ready */
355
356 if ((key == 6) || (asc == 0x29) || (asc == 0x28)) { /* Unit Attention */
357 if (unitattn-- > 0) {
358 udelay(200 * 1000);
359 goto retry;
360 }
361 printf("Unit Attention, tried %d\n", ATAPI_UNIT_ATTN);
362 goto error;
363 }
364 if ((asc == 0x4) && (ascq == 0x1)) {
365 /* not ready, but will be ready soon */
366 if (notready-- > 0) {
367 udelay(200 * 1000);
368 goto retry;
369 }
370 printf("Drive not ready, tried %d times\n",
371 ATAPI_DRIVE_NOT_READY);
372 goto error;
373 }
374 if (asc == 0x3a) {
375 debug("Media not present\n");
376 goto error;
377 }
378
379 printf("ERROR: Unknown Sense key %02X ASC %02X ASCQ %02X\n", key, asc,
380 ascq);
381error:
382 debug("ERROR Sense key %02X ASC %02X ASCQ %02X\n", key, asc, ascq);
383 return 0xFF;
384}
385
386/*
387 * atapi_read:
388 * we transfer only one block per command, since the multiple DRQ per
389 * command is not yet implemented
390 */
391#define ATAPI_READ_MAX_BYTES 2048 /* we read max 2kbytes */
392#define ATAPI_READ_BLOCK_SIZE 2048 /* assuming CD part */
393#define ATAPI_READ_MAX_BLOCK (ATAPI_READ_MAX_BYTES/ATAPI_READ_BLOCK_SIZE)
394
395ulong atapi_read(struct blk_desc *block_dev, lbaint_t blknr, lbaint_t blkcnt,
396 void *buffer)
397{
398 int device = block_dev->devnum;
399 ulong n = 0;
400 unsigned char ccb[12]; /* Command descriptor block */
401 ulong cnt;
402
403 debug("atapi_read dev %d start " LBAF " blocks " LBAF
404 " buffer at %lX\n", device, blknr, blkcnt, (ulong) buffer);
405
406 do {
407 if (blkcnt > ATAPI_READ_MAX_BLOCK)
408 cnt = ATAPI_READ_MAX_BLOCK;
409 else
410 cnt = blkcnt;
411
412 ccb[0] = ATAPI_CMD_READ_12;
413 ccb[1] = 0; /* reserved */
414 ccb[2] = (unsigned char) (blknr >> 24) & 0xFF; /* MSB Block */
415 ccb[3] = (unsigned char) (blknr >> 16) & 0xFF; /* */
416 ccb[4] = (unsigned char) (blknr >> 8) & 0xFF;
417 ccb[5] = (unsigned char) blknr & 0xFF; /* LSB Block */
418 ccb[6] = (unsigned char) (cnt >> 24) & 0xFF; /* MSB Block cnt */
419 ccb[7] = (unsigned char) (cnt >> 16) & 0xFF;
420 ccb[8] = (unsigned char) (cnt >> 8) & 0xFF;
421 ccb[9] = (unsigned char) cnt & 0xFF; /* LSB Block */
422 ccb[10] = 0; /* reserved */
423 ccb[11] = 0; /* reserved */
424
425 if (atapi_issue_autoreq(device, ccb, 12,
426 (unsigned char *)buffer,
427 cnt * ATAPI_READ_BLOCK_SIZE)
428 == 0xFF) {
429 return n;
430 }
431 n += cnt;
432 blkcnt -= cnt;
433 blknr += cnt;
434 buffer += (cnt * ATAPI_READ_BLOCK_SIZE);
435 } while (blkcnt > 0);
436 return n;
437}
438
439static void atapi_inquiry(struct blk_desc *dev_desc)
440{
441 unsigned char ccb[12]; /* Command descriptor block */
442 unsigned char iobuf[64]; /* temp buf */
443 unsigned char c;
444 int device;
445
446 device = dev_desc->devnum;
447 dev_desc->type = DEV_TYPE_UNKNOWN; /* not yet valid */
Bin Meng52a1c2c2017-07-30 19:24:00 -0700448#ifndef CONFIG_BLK
Simon Glasse9be1ee2016-05-01 11:36:10 -0600449 dev_desc->block_read = atapi_read;
Bin Meng52a1c2c2017-07-30 19:24:00 -0700450#endif
Simon Glasse9be1ee2016-05-01 11:36:10 -0600451
452 memset(ccb, 0, sizeof(ccb));
453 memset(iobuf, 0, sizeof(iobuf));
454
455 ccb[0] = ATAPI_CMD_INQUIRY;
456 ccb[4] = 40; /* allocation Legnth */
457 c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *)iobuf, 40);
458
459 debug("ATAPI_CMD_INQUIRY returned %x\n", c);
460 if (c != 0)
461 return;
462
463 /* copy device ident strings */
464 ident_cpy((unsigned char *)dev_desc->vendor, &iobuf[8], 8);
465 ident_cpy((unsigned char *)dev_desc->product, &iobuf[16], 16);
466 ident_cpy((unsigned char *)dev_desc->revision, &iobuf[32], 5);
467
468 dev_desc->lun = 0;
469 dev_desc->lba = 0;
470 dev_desc->blksz = 0;
471 dev_desc->log2blksz = LOG2_INVALID(typeof(dev_desc->log2blksz));
472 dev_desc->type = iobuf[0] & 0x1f;
473
474 if ((iobuf[1] & 0x80) == 0x80)
475 dev_desc->removable = 1;
476 else
477 dev_desc->removable = 0;
478
479 memset(ccb, 0, sizeof(ccb));
480 memset(iobuf, 0, sizeof(iobuf));
481 ccb[0] = ATAPI_CMD_START_STOP;
482 ccb[4] = 0x03; /* start */
483
484 c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *)iobuf, 0);
485
486 debug("ATAPI_CMD_START_STOP returned %x\n", c);
487 if (c != 0)
488 return;
489
490 memset(ccb, 0, sizeof(ccb));
491 memset(iobuf, 0, sizeof(iobuf));
492 c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *)iobuf, 0);
493
494 debug("ATAPI_CMD_UNIT_TEST_READY returned %x\n", c);
495 if (c != 0)
496 return;
497
498 memset(ccb, 0, sizeof(ccb));
499 memset(iobuf, 0, sizeof(iobuf));
500 ccb[0] = ATAPI_CMD_READ_CAP;
501 c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *)iobuf, 8);
502 debug("ATAPI_CMD_READ_CAP returned %x\n", c);
503 if (c != 0)
504 return;
505
506 debug("Read Cap: LBA %02X%02X%02X%02X blksize %02X%02X%02X%02X\n",
507 iobuf[0], iobuf[1], iobuf[2], iobuf[3],
508 iobuf[4], iobuf[5], iobuf[6], iobuf[7]);
509
510 dev_desc->lba = ((unsigned long) iobuf[0] << 24) +
511 ((unsigned long) iobuf[1] << 16) +
512 ((unsigned long) iobuf[2] << 8) + ((unsigned long) iobuf[3]);
513 dev_desc->blksz = ((unsigned long) iobuf[4] << 24) +
514 ((unsigned long) iobuf[5] << 16) +
515 ((unsigned long) iobuf[6] << 8) + ((unsigned long) iobuf[7]);
516 dev_desc->log2blksz = LOG2(dev_desc->blksz);
517#ifdef CONFIG_LBA48
518 /* ATAPI devices cannot use 48bit addressing (ATA/ATAPI v7) */
519 dev_desc->lba48 = 0;
520#endif
521 return;
522}
523
524#endif /* CONFIG_ATAPI */
525
526static void ide_ident(struct blk_desc *dev_desc)
527{
528 unsigned char c;
529 hd_driveid_t iop;
530
531#ifdef CONFIG_ATAPI
532 int retries = 0;
533#endif
534 int device;
535
536 device = dev_desc->devnum;
537 printf(" Device %d: ", device);
538
Simon Glasse9be1ee2016-05-01 11:36:10 -0600539 /* Select device
540 */
541 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
542 dev_desc->if_type = IF_TYPE_IDE;
543#ifdef CONFIG_ATAPI
544
545 retries = 0;
546
547 /* Warning: This will be tricky to read */
548 while (retries <= 1) {
549 /* check signature */
550 if ((ide_inb(device, ATA_SECT_CNT) == 0x01) &&
551 (ide_inb(device, ATA_SECT_NUM) == 0x01) &&
552 (ide_inb(device, ATA_CYL_LOW) == 0x14) &&
553 (ide_inb(device, ATA_CYL_HIGH) == 0xEB)) {
554 /* ATAPI Signature found */
555 dev_desc->if_type = IF_TYPE_ATAPI;
556 /*
557 * Start Ident Command
558 */
Heinrich Schuchardte6e9a4f2020-02-27 18:28:00 +0100559 ide_outb(device, ATA_COMMAND, ATA_CMD_ID_ATAPI);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600560 /*
561 * Wait for completion - ATAPI devices need more time
562 * to become ready
563 */
564 c = ide_wait(device, ATAPI_TIME_OUT);
565 } else
566#endif
567 {
568 /*
569 * Start Ident Command
570 */
Heinrich Schuchardte6e9a4f2020-02-27 18:28:00 +0100571 ide_outb(device, ATA_COMMAND, ATA_CMD_ID_ATA);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600572
573 /*
574 * Wait for completion
575 */
576 c = ide_wait(device, IDE_TIME_OUT);
577 }
Simon Glasse9be1ee2016-05-01 11:36:10 -0600578
579 if (((c & ATA_STAT_DRQ) == 0) ||
580 ((c & (ATA_STAT_FAULT | ATA_STAT_ERR)) != 0)) {
581#ifdef CONFIG_ATAPI
582 {
583 /*
584 * Need to soft reset the device
585 * in case it's an ATAPI...
586 */
587 debug("Retrying...\n");
588 ide_outb(device, ATA_DEV_HD,
589 ATA_LBA | ATA_DEVICE(device));
590 udelay(100000);
591 ide_outb(device, ATA_COMMAND, 0x08);
592 udelay(500000); /* 500 ms */
593 }
594 /*
595 * Select device
596 */
597 ide_outb(device, ATA_DEV_HD,
598 ATA_LBA | ATA_DEVICE(device));
599 retries++;
600#else
601 return;
602#endif
603 }
604#ifdef CONFIG_ATAPI
605 else
606 break;
607 } /* see above - ugly to read */
608
609 if (retries == 2) /* Not found */
610 return;
611#endif
612
613 ide_input_swap_data(device, (ulong *)&iop, ATA_SECTORWORDS);
614
615 ident_cpy((unsigned char *)dev_desc->revision, iop.fw_rev,
616 sizeof(dev_desc->revision));
617 ident_cpy((unsigned char *)dev_desc->vendor, iop.model,
618 sizeof(dev_desc->vendor));
619 ident_cpy((unsigned char *)dev_desc->product, iop.serial_no,
620 sizeof(dev_desc->product));
Simon Glasse9be1ee2016-05-01 11:36:10 -0600621
622 if ((iop.config & 0x0080) == 0x0080)
623 dev_desc->removable = 1;
624 else
625 dev_desc->removable = 0;
626
627#ifdef CONFIG_ATAPI
628 if (dev_desc->if_type == IF_TYPE_ATAPI) {
629 atapi_inquiry(dev_desc);
630 return;
631 }
632#endif /* CONFIG_ATAPI */
633
Reinoud Zandijk0a527fd2021-02-24 17:44:42 +0100634 iop.lba_capacity[0] = be16_to_cpu(iop.lba_capacity[0]);
635 iop.lba_capacity[1] = be16_to_cpu(iop.lba_capacity[1]);
636 dev_desc->lba =
637 ((unsigned long)iop.lba_capacity[0]) |
638 ((unsigned long)iop.lba_capacity[1] << 16);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600639
640#ifdef CONFIG_LBA48
641 if (iop.command_set_2 & 0x0400) { /* LBA 48 support */
642 dev_desc->lba48 = 1;
Reinoud Zandijk0a527fd2021-02-24 17:44:42 +0100643 for (int i = 0; i < 4; i++)
644 iop.lba48_capacity[i] = be16_to_cpu(iop.lba48_capacity[i]);
645 dev_desc->lba =
646 ((unsigned long long)iop.lba48_capacity[0] |
647 ((unsigned long long)iop.lba48_capacity[1] << 16) |
648 ((unsigned long long)iop.lba48_capacity[2] << 32) |
649 ((unsigned long long)iop.lba48_capacity[3] << 48));
Simon Glasse9be1ee2016-05-01 11:36:10 -0600650 } else {
651 dev_desc->lba48 = 0;
652 }
653#endif /* CONFIG_LBA48 */
654 /* assuming HD */
655 dev_desc->type = DEV_TYPE_HARDDISK;
656 dev_desc->blksz = ATA_BLOCKSIZE;
657 dev_desc->log2blksz = LOG2(dev_desc->blksz);
658 dev_desc->lun = 0; /* just to fill something in... */
659
660#if 0 /* only used to test the powersaving mode,
661 * if enabled, the drive goes after 5 sec
662 * in standby mode */
663 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
664 c = ide_wait(device, IDE_TIME_OUT);
665 ide_outb(device, ATA_SECT_CNT, 1);
666 ide_outb(device, ATA_LBA_LOW, 0);
667 ide_outb(device, ATA_LBA_MID, 0);
668 ide_outb(device, ATA_LBA_HIGH, 0);
669 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
670 ide_outb(device, ATA_COMMAND, 0xe3);
671 udelay(50);
672 c = ide_wait(device, IDE_TIME_OUT); /* can't take over 500 ms */
673#endif
674}
675
Simon Glasse9be1ee2016-05-01 11:36:10 -0600676__weak void ide_outb(int dev, int port, unsigned char val)
677{
678 debug("ide_outb (dev= %d, port= 0x%x, val= 0x%02x) : @ 0x%08lx\n",
679 dev, port, val,
Simon Glass7ee20162021-11-24 09:26:47 -0700680 (ATA_CURR_BASE(dev) + ATA_PORT_ADDR(port)));
Simon Glasse9be1ee2016-05-01 11:36:10 -0600681
682#if defined(CONFIG_IDE_AHB)
683 if (port) {
684 /* write command */
685 ide_write_register(dev, port, val);
686 } else {
687 /* write data */
688 outb(val, (ATA_CURR_BASE(dev)));
689 }
690#else
Simon Glass7ee20162021-11-24 09:26:47 -0700691 outb(val, (ATA_CURR_BASE(dev) + ATA_PORT_ADDR(port)));
Simon Glasse9be1ee2016-05-01 11:36:10 -0600692#endif
693}
694
695__weak unsigned char ide_inb(int dev, int port)
696{
697 uchar val;
698
699#if defined(CONFIG_IDE_AHB)
700 val = ide_read_register(dev, port);
701#else
Simon Glass7ee20162021-11-24 09:26:47 -0700702 val = inb((ATA_CURR_BASE(dev) + ATA_PORT_ADDR(port)));
Simon Glasse9be1ee2016-05-01 11:36:10 -0600703#endif
704
705 debug("ide_inb (dev= %d, port= 0x%x) : @ 0x%08lx -> 0x%02x\n",
706 dev, port,
Simon Glass7ee20162021-11-24 09:26:47 -0700707 (ATA_CURR_BASE(dev) + ATA_PORT_ADDR(port)), val);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600708 return val;
709}
710
711void ide_init(void)
712{
713 unsigned char c;
714 int i, bus;
715
Simon Glasse9be1ee2016-05-01 11:36:10 -0600716#ifdef CONFIG_IDE_PREINIT
717 WATCHDOG_RESET();
718
719 if (ide_preinit()) {
720 puts("ide_preinit failed\n");
721 return;
722 }
723#endif /* CONFIG_IDE_PREINIT */
724
725 WATCHDOG_RESET();
726
Simon Glasse9be1ee2016-05-01 11:36:10 -0600727 /* ATAPI Drives seems to need a proper IDE Reset */
728 ide_reset();
729
Simon Glasse9be1ee2016-05-01 11:36:10 -0600730 /*
731 * Wait for IDE to get ready.
732 * According to spec, this can take up to 31 seconds!
733 */
734 for (bus = 0; bus < CONFIG_SYS_IDE_MAXBUS; ++bus) {
735 int dev =
736 bus * (CONFIG_SYS_IDE_MAXDEVICE /
737 CONFIG_SYS_IDE_MAXBUS);
738
Simon Glasse9be1ee2016-05-01 11:36:10 -0600739 printf("Bus %d: ", bus);
740
741 ide_bus_ok[bus] = 0;
742
743 /* Select device
744 */
745 udelay(100000); /* 100 ms */
746 ide_outb(dev, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(dev));
747 udelay(100000); /* 100 ms */
748 i = 0;
749 do {
750 udelay(10000); /* 10 ms */
751
752 c = ide_inb(dev, ATA_STATUS);
753 i++;
754 if (i > (ATA_RESET_TIME * 100)) {
755 puts("** Timeout **\n");
Simon Glasse9be1ee2016-05-01 11:36:10 -0600756 return;
757 }
758 if ((i >= 100) && ((i % 100) == 0))
759 putc('.');
760
761 } while (c & ATA_STAT_BUSY);
762
763 if (c & (ATA_STAT_BUSY | ATA_STAT_FAULT)) {
764 puts("not available ");
765 debug("Status = 0x%02X ", c);
766#ifndef CONFIG_ATAPI /* ATAPI Devices do not set DRDY */
767 } else if ((c & ATA_STAT_READY) == 0) {
768 puts("not available ");
769 debug("Status = 0x%02X ", c);
770#endif
771 } else {
772 puts("OK ");
773 ide_bus_ok[bus] = 1;
774 }
775 WATCHDOG_RESET();
776 }
777
778 putc('\n');
779
Simon Glasse9be1ee2016-05-01 11:36:10 -0600780 for (i = 0; i < CONFIG_SYS_IDE_MAXDEVICE; ++i) {
Simon Glasse9be1ee2016-05-01 11:36:10 -0600781 ide_dev_desc[i].type = DEV_TYPE_UNKNOWN;
782 ide_dev_desc[i].if_type = IF_TYPE_IDE;
783 ide_dev_desc[i].devnum = i;
784 ide_dev_desc[i].part_type = PART_TYPE_UNKNOWN;
785 ide_dev_desc[i].blksz = 0;
786 ide_dev_desc[i].log2blksz =
787 LOG2_INVALID(typeof(ide_dev_desc[i].log2blksz));
788 ide_dev_desc[i].lba = 0;
Simon Glass145df842016-05-01 11:36:22 -0600789#ifndef CONFIG_BLK
Simon Glasse9be1ee2016-05-01 11:36:10 -0600790 ide_dev_desc[i].block_read = ide_read;
791 ide_dev_desc[i].block_write = ide_write;
Simon Glass145df842016-05-01 11:36:22 -0600792#endif
Simon Glasse9be1ee2016-05-01 11:36:10 -0600793 if (!ide_bus_ok[IDE_BUS(i)])
794 continue;
Simon Glasse9be1ee2016-05-01 11:36:10 -0600795 ide_ident(&ide_dev_desc[i]);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600796 dev_print(&ide_dev_desc[i]);
797
Bin Meng68e6f222017-09-10 05:12:51 -0700798#ifndef CONFIG_BLK
Simon Glasse9be1ee2016-05-01 11:36:10 -0600799 if ((ide_dev_desc[i].lba > 0) && (ide_dev_desc[i].blksz > 0)) {
800 /* initialize partition type */
801 part_init(&ide_dev_desc[i]);
802 }
Bin Meng68e6f222017-09-10 05:12:51 -0700803#endif
Simon Glasse9be1ee2016-05-01 11:36:10 -0600804 }
805 WATCHDOG_RESET();
Bin Meng68e6f222017-09-10 05:12:51 -0700806
807#ifdef CONFIG_BLK
808 struct udevice *dev;
809
810 uclass_first_device(UCLASS_IDE, &dev);
811#endif
Simon Glasse9be1ee2016-05-01 11:36:10 -0600812}
813
Simon Glasse9be1ee2016-05-01 11:36:10 -0600814__weak void ide_input_swap_data(int dev, ulong *sect_buf, int words)
815{
Reinoud Zandijk0a527fd2021-02-24 17:44:42 +0100816 uintptr_t paddr = (ATA_CURR_BASE(dev) + ATA_DATA_REG);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600817 ushort *dbuf = (ushort *)sect_buf;
818
Reinoud Zandijk0a527fd2021-02-24 17:44:42 +0100819 debug("in input swap data base for read is %p\n", (void *)paddr);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600820
821 while (words--) {
Reinoud Zandijk0a527fd2021-02-24 17:44:42 +0100822 EIEIO;
823 *dbuf++ = be16_to_cpu(inw(paddr));
824 EIEIO;
825 *dbuf++ = be16_to_cpu(inw(paddr));
Simon Glasse9be1ee2016-05-01 11:36:10 -0600826 }
827}
Simon Glasse9be1ee2016-05-01 11:36:10 -0600828
Simon Glasse9be1ee2016-05-01 11:36:10 -0600829__weak void ide_output_data(int dev, const ulong *sect_buf, int words)
830{
831#if defined(CONFIG_IDE_AHB)
832 ide_write_data(dev, sect_buf, words);
833#else
Reinoud Zandijk0a527fd2021-02-24 17:44:42 +0100834 uintptr_t paddr = (ATA_CURR_BASE(dev) + ATA_DATA_REG);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600835 ushort *dbuf;
Simon Glasse9be1ee2016-05-01 11:36:10 -0600836
Simon Glasse9be1ee2016-05-01 11:36:10 -0600837 dbuf = (ushort *)sect_buf;
Simon Glasse9be1ee2016-05-01 11:36:10 -0600838 while (words--) {
839 EIEIO;
Reinoud Zandijk0a527fd2021-02-24 17:44:42 +0100840 outw(cpu_to_le16(*dbuf++), paddr);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600841 EIEIO;
Reinoud Zandijk0a527fd2021-02-24 17:44:42 +0100842 outw(cpu_to_le16(*dbuf++), paddr);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600843 }
Reinoud Zandijk0a527fd2021-02-24 17:44:42 +0100844#endif /* CONFIG_IDE_AHB */
Simon Glasse9be1ee2016-05-01 11:36:10 -0600845}
Reinoud Zandijk0a527fd2021-02-24 17:44:42 +0100846
Simon Glasse9be1ee2016-05-01 11:36:10 -0600847__weak void ide_input_data(int dev, ulong *sect_buf, int words)
848{
849#if defined(CONFIG_IDE_AHB)
850 ide_read_data(dev, sect_buf, words);
851#else
Reinoud Zandijk0a527fd2021-02-24 17:44:42 +0100852 uintptr_t paddr = (ATA_CURR_BASE(dev) + ATA_DATA_REG);
853 ushort *dbuf;
Simon Glasse9be1ee2016-05-01 11:36:10 -0600854
Reinoud Zandijk0a527fd2021-02-24 17:44:42 +0100855 dbuf = (ushort *)sect_buf;
856
857 debug("in input data base for read is %p\n", (void *)paddr);
858
859 while (words--) {
860 EIEIO;
861 *dbuf++ = le16_to_cpu(inw(paddr));
862 EIEIO;
863 *dbuf++ = le16_to_cpu(inw(paddr));
864 }
865#endif /* CONFIG_IDE_AHB */
866}
Simon Glasse9be1ee2016-05-01 11:36:10 -0600867
Simon Glass145df842016-05-01 11:36:22 -0600868#ifdef CONFIG_BLK
869ulong ide_read(struct udevice *dev, lbaint_t blknr, lbaint_t blkcnt,
870 void *buffer)
871#else
Simon Glasse9be1ee2016-05-01 11:36:10 -0600872ulong ide_read(struct blk_desc *block_dev, lbaint_t blknr, lbaint_t blkcnt,
873 void *buffer)
Simon Glass145df842016-05-01 11:36:22 -0600874#endif
Simon Glasse9be1ee2016-05-01 11:36:10 -0600875{
Simon Glass145df842016-05-01 11:36:22 -0600876#ifdef CONFIG_BLK
Simon Glasscaa4daa2020-12-03 16:55:18 -0700877 struct blk_desc *block_dev = dev_get_uclass_plat(dev);
Simon Glass145df842016-05-01 11:36:22 -0600878#endif
Simon Glasse9be1ee2016-05-01 11:36:10 -0600879 int device = block_dev->devnum;
880 ulong n = 0;
881 unsigned char c;
882 unsigned char pwrsave = 0; /* power save */
883
884#ifdef CONFIG_LBA48
885 unsigned char lba48 = 0;
886
887 if (blknr & 0x0000fffff0000000ULL) {
888 /* more than 28 bits used, use 48bit mode */
889 lba48 = 1;
890 }
891#endif
892 debug("ide_read dev %d start " LBAF ", blocks " LBAF " buffer at %lX\n",
893 device, blknr, blkcnt, (ulong) buffer);
894
Simon Glasse9be1ee2016-05-01 11:36:10 -0600895 /* Select device
896 */
897 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
898 c = ide_wait(device, IDE_TIME_OUT);
899
900 if (c & ATA_STAT_BUSY) {
901 printf("IDE read: device %d not ready\n", device);
902 goto IDE_READ_E;
903 }
904
905 /* first check if the drive is in Powersaving mode, if yes,
906 * increase the timeout value */
Heinrich Schuchardte6e9a4f2020-02-27 18:28:00 +0100907 ide_outb(device, ATA_COMMAND, ATA_CMD_CHK_POWER);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600908 udelay(50);
909
910 c = ide_wait(device, IDE_TIME_OUT); /* can't take over 500 ms */
911
912 if (c & ATA_STAT_BUSY) {
913 printf("IDE read: device %d not ready\n", device);
914 goto IDE_READ_E;
915 }
916 if ((c & ATA_STAT_ERR) == ATA_STAT_ERR) {
917 printf("No Powersaving mode %X\n", c);
918 } else {
919 c = ide_inb(device, ATA_SECT_CNT);
920 debug("Powersaving %02X\n", c);
921 if (c == 0)
922 pwrsave = 1;
923 }
924
925
926 while (blkcnt-- > 0) {
927 c = ide_wait(device, IDE_TIME_OUT);
928
929 if (c & ATA_STAT_BUSY) {
930 printf("IDE read: device %d not ready\n", device);
931 break;
932 }
933#ifdef CONFIG_LBA48
934 if (lba48) {
935 /* write high bits */
936 ide_outb(device, ATA_SECT_CNT, 0);
937 ide_outb(device, ATA_LBA_LOW, (blknr >> 24) & 0xFF);
938#ifdef CONFIG_SYS_64BIT_LBA
939 ide_outb(device, ATA_LBA_MID, (blknr >> 32) & 0xFF);
940 ide_outb(device, ATA_LBA_HIGH, (blknr >> 40) & 0xFF);
941#else
942 ide_outb(device, ATA_LBA_MID, 0);
943 ide_outb(device, ATA_LBA_HIGH, 0);
944#endif
945 }
946#endif
947 ide_outb(device, ATA_SECT_CNT, 1);
948 ide_outb(device, ATA_LBA_LOW, (blknr >> 0) & 0xFF);
949 ide_outb(device, ATA_LBA_MID, (blknr >> 8) & 0xFF);
950 ide_outb(device, ATA_LBA_HIGH, (blknr >> 16) & 0xFF);
951
952#ifdef CONFIG_LBA48
953 if (lba48) {
954 ide_outb(device, ATA_DEV_HD,
955 ATA_LBA | ATA_DEVICE(device));
Reinoud Zandijk0a527fd2021-02-24 17:44:42 +0100956 ide_outb(device, ATA_COMMAND, ATA_CMD_PIO_READ_EXT);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600957
958 } else
959#endif
960 {
961 ide_outb(device, ATA_DEV_HD, ATA_LBA |
962 ATA_DEVICE(device) | ((blknr >> 24) & 0xF));
Reinoud Zandijk0a527fd2021-02-24 17:44:42 +0100963 ide_outb(device, ATA_COMMAND, ATA_CMD_PIO_READ);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600964 }
965
966 udelay(50);
967
968 if (pwrsave) {
969 /* may take up to 4 sec */
970 c = ide_wait(device, IDE_SPIN_UP_TIME_OUT);
971 pwrsave = 0;
972 } else {
973 /* can't take over 500 ms */
974 c = ide_wait(device, IDE_TIME_OUT);
975 }
976
977 if ((c & (ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR)) !=
978 ATA_STAT_DRQ) {
979 printf("Error (no IRQ) dev %d blk " LBAF
980 ": status %#02x\n", device, blknr, c);
981 break;
982 }
983
984 ide_input_data(device, buffer, ATA_SECTORWORDS);
985 (void) ide_inb(device, ATA_STATUS); /* clear IRQ */
986
987 ++n;
988 ++blknr;
989 buffer += ATA_BLOCKSIZE;
990 }
991IDE_READ_E:
Simon Glasse9be1ee2016-05-01 11:36:10 -0600992 return n;
993}
994
Simon Glass145df842016-05-01 11:36:22 -0600995#ifdef CONFIG_BLK
996ulong ide_write(struct udevice *dev, lbaint_t blknr, lbaint_t blkcnt,
997 const void *buffer)
998#else
Simon Glasse9be1ee2016-05-01 11:36:10 -0600999ulong ide_write(struct blk_desc *block_dev, lbaint_t blknr, lbaint_t blkcnt,
1000 const void *buffer)
Simon Glass145df842016-05-01 11:36:22 -06001001#endif
Simon Glasse9be1ee2016-05-01 11:36:10 -06001002{
Simon Glass145df842016-05-01 11:36:22 -06001003#ifdef CONFIG_BLK
Simon Glasscaa4daa2020-12-03 16:55:18 -07001004 struct blk_desc *block_dev = dev_get_uclass_plat(dev);
Simon Glass145df842016-05-01 11:36:22 -06001005#endif
Simon Glasse9be1ee2016-05-01 11:36:10 -06001006 int device = block_dev->devnum;
1007 ulong n = 0;
1008 unsigned char c;
1009
1010#ifdef CONFIG_LBA48
1011 unsigned char lba48 = 0;
1012
1013 if (blknr & 0x0000fffff0000000ULL) {
1014 /* more than 28 bits used, use 48bit mode */
1015 lba48 = 1;
1016 }
1017#endif
1018
Simon Glasse9be1ee2016-05-01 11:36:10 -06001019 /* Select device
1020 */
1021 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
1022
1023 while (blkcnt-- > 0) {
1024 c = ide_wait(device, IDE_TIME_OUT);
1025
1026 if (c & ATA_STAT_BUSY) {
1027 printf("IDE read: device %d not ready\n", device);
1028 goto WR_OUT;
1029 }
1030#ifdef CONFIG_LBA48
1031 if (lba48) {
1032 /* write high bits */
1033 ide_outb(device, ATA_SECT_CNT, 0);
1034 ide_outb(device, ATA_LBA_LOW, (blknr >> 24) & 0xFF);
1035#ifdef CONFIG_SYS_64BIT_LBA
1036 ide_outb(device, ATA_LBA_MID, (blknr >> 32) & 0xFF);
1037 ide_outb(device, ATA_LBA_HIGH, (blknr >> 40) & 0xFF);
1038#else
1039 ide_outb(device, ATA_LBA_MID, 0);
1040 ide_outb(device, ATA_LBA_HIGH, 0);
1041#endif
1042 }
1043#endif
1044 ide_outb(device, ATA_SECT_CNT, 1);
1045 ide_outb(device, ATA_LBA_LOW, (blknr >> 0) & 0xFF);
1046 ide_outb(device, ATA_LBA_MID, (blknr >> 8) & 0xFF);
1047 ide_outb(device, ATA_LBA_HIGH, (blknr >> 16) & 0xFF);
1048
1049#ifdef CONFIG_LBA48
1050 if (lba48) {
1051 ide_outb(device, ATA_DEV_HD,
1052 ATA_LBA | ATA_DEVICE(device));
Reinoud Zandijk0a527fd2021-02-24 17:44:42 +01001053 ide_outb(device, ATA_COMMAND, ATA_CMD_PIO_WRITE_EXT);
Simon Glasse9be1ee2016-05-01 11:36:10 -06001054
1055 } else
1056#endif
1057 {
1058 ide_outb(device, ATA_DEV_HD, ATA_LBA |
1059 ATA_DEVICE(device) | ((blknr >> 24) & 0xF));
Reinoud Zandijk0a527fd2021-02-24 17:44:42 +01001060 ide_outb(device, ATA_COMMAND, ATA_CMD_PIO_WRITE);
Simon Glasse9be1ee2016-05-01 11:36:10 -06001061 }
1062
1063 udelay(50);
1064
1065 /* can't take over 500 ms */
1066 c = ide_wait(device, IDE_TIME_OUT);
1067
1068 if ((c & (ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR)) !=
1069 ATA_STAT_DRQ) {
1070 printf("Error (no IRQ) dev %d blk " LBAF
1071 ": status %#02x\n", device, blknr, c);
1072 goto WR_OUT;
1073 }
1074
1075 ide_output_data(device, buffer, ATA_SECTORWORDS);
1076 c = ide_inb(device, ATA_STATUS); /* clear IRQ */
1077 ++n;
1078 ++blknr;
1079 buffer += ATA_BLOCKSIZE;
1080 }
1081WR_OUT:
Simon Glasse9be1ee2016-05-01 11:36:10 -06001082 return n;
1083}
1084
1085#if defined(CONFIG_OF_IDE_FIXUP)
1086int ide_device_present(int dev)
1087{
1088 if (dev >= CONFIG_SYS_IDE_MAXBUS)
1089 return 0;
1090 return ide_dev_desc[dev].type == DEV_TYPE_UNKNOWN ? 0 : 1;
1091}
1092#endif
1093
Simon Glass145df842016-05-01 11:36:22 -06001094#ifdef CONFIG_BLK
Bin Meng68e6f222017-09-10 05:12:51 -07001095static int ide_blk_probe(struct udevice *udev)
1096{
Simon Glasscaa4daa2020-12-03 16:55:18 -07001097 struct blk_desc *desc = dev_get_uclass_plat(udev);
Bin Meng68e6f222017-09-10 05:12:51 -07001098
1099 /* fill in device vendor/product/rev strings */
1100 strncpy(desc->vendor, ide_dev_desc[desc->devnum].vendor,
1101 BLK_VEN_SIZE);
1102 desc->vendor[BLK_VEN_SIZE] = '\0';
1103 strncpy(desc->product, ide_dev_desc[desc->devnum].product,
1104 BLK_PRD_SIZE);
1105 desc->product[BLK_PRD_SIZE] = '\0';
1106 strncpy(desc->revision, ide_dev_desc[desc->devnum].revision,
1107 BLK_REV_SIZE);
1108 desc->revision[BLK_REV_SIZE] = '\0';
1109
Bin Meng68e6f222017-09-10 05:12:51 -07001110 return 0;
1111}
1112
Simon Glass145df842016-05-01 11:36:22 -06001113static const struct blk_ops ide_blk_ops = {
1114 .read = ide_read,
1115 .write = ide_write,
1116};
1117
1118U_BOOT_DRIVER(ide_blk) = {
1119 .name = "ide_blk",
1120 .id = UCLASS_BLK,
1121 .ops = &ide_blk_ops,
Bin Meng68e6f222017-09-10 05:12:51 -07001122 .probe = ide_blk_probe,
1123};
1124
1125static int ide_probe(struct udevice *udev)
1126{
1127 struct udevice *blk_dev;
1128 char name[20];
1129 int blksz;
1130 lbaint_t size;
1131 int i;
1132 int ret;
1133
1134 for (i = 0; i < CONFIG_SYS_IDE_MAXDEVICE; i++) {
1135 if (ide_dev_desc[i].type != DEV_TYPE_UNKNOWN) {
1136 sprintf(name, "blk#%d", i);
1137
1138 blksz = ide_dev_desc[i].blksz;
1139 size = blksz * ide_dev_desc[i].lba;
Bin Meng1f4adab2017-09-10 05:12:52 -07001140
1141 /*
1142 * With CDROM, if there is no CD inserted, blksz will
1143 * be zero, don't bother to create IDE block device.
1144 */
1145 if (!blksz)
1146 continue;
Bin Meng68e6f222017-09-10 05:12:51 -07001147 ret = blk_create_devicef(udev, "ide_blk", name,
1148 IF_TYPE_IDE, i,
1149 blksz, size, &blk_dev);
1150 if (ret)
1151 return ret;
1152 }
1153 }
1154
1155 return 0;
1156}
1157
1158U_BOOT_DRIVER(ide) = {
1159 .name = "ide",
1160 .id = UCLASS_IDE,
1161 .probe = ide_probe,
1162};
1163
1164struct pci_device_id ide_supported[] = {
1165 { PCI_DEVICE_CLASS(PCI_CLASS_STORAGE_IDE << 8, 0xffff00) },
1166 { }
1167};
1168
1169U_BOOT_PCI_DEVICE(ide, ide_supported);
1170
1171UCLASS_DRIVER(ide) = {
1172 .name = "ide",
1173 .id = UCLASS_IDE,
Simon Glass145df842016-05-01 11:36:22 -06001174};
1175#else
Simon Glasse9be1ee2016-05-01 11:36:10 -06001176U_BOOT_LEGACY_BLK(ide) = {
1177 .if_typename = "ide",
1178 .if_type = IF_TYPE_IDE,
1179 .max_devs = CONFIG_SYS_IDE_MAXDEVICE,
1180 .desc = ide_dev_desc,
1181};
Simon Glass145df842016-05-01 11:36:22 -06001182#endif