blob: 782780fd302bcbed8e703d3dbfa3c856bd284385 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glasse9be1ee2016-05-01 11:36:10 -06002/*
3 * (C) Copyright 2000-2011
4 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
Simon Glasse9be1ee2016-05-01 11:36:10 -06005 */
6
Patrick Delaunayb953ec22021-04-27 11:02:19 +02007#define LOG_CATEGORY UCLASS_IDE
8
Simon Glasse9be1ee2016-05-01 11:36:10 -06009#include <common.h>
10#include <ata.h>
Simon Glasse6f6f9e2020-05-10 11:39:58 -060011#include <blk.h>
Simon Glass0d77f8f2023-01-17 10:47:46 -070012#include <bootdev.h>
Simon Glass145df842016-05-01 11:36:22 -060013#include <dm.h>
Simon Glasse9be1ee2016-05-01 11:36:10 -060014#include <ide.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060015#include <log.h>
Simon Glasse6f6f9e2020-05-10 11:39:58 -060016#include <part.h>
Simon Glasse9be1ee2016-05-01 11:36:10 -060017#include <watchdog.h>
18#include <asm/io.h>
Simon Glassc05ed002020-05-10 11:40:11 -060019#include <linux/delay.h>
Simon Glasse9be1ee2016-05-01 11:36:10 -060020
21#ifdef __PPC__
22# define EIEIO __asm__ volatile ("eieio")
23# define SYNC __asm__ volatile ("sync")
24#else
25# define EIEIO /* nothing */
26# define SYNC /* nothing */
27#endif
28
29/* Current offset for IDE0 / IDE1 bus access */
30ulong ide_bus_offset[CONFIG_SYS_IDE_MAXBUS] = {
31#if defined(CONFIG_SYS_ATA_IDE0_OFFSET)
32 CONFIG_SYS_ATA_IDE0_OFFSET,
33#endif
34#if defined(CONFIG_SYS_ATA_IDE1_OFFSET) && (CONFIG_SYS_IDE_MAXBUS > 1)
35 CONFIG_SYS_ATA_IDE1_OFFSET,
36#endif
37};
38
Simon Glass14a4f522023-04-25 10:54:26 -060039#define ATA_CURR_BASE(dev) (CONFIG_SYS_ATA_BASE_ADDR + \
40 ide_bus_offset[IDE_BUS(dev)])
41
Simon Glasse9be1ee2016-05-01 11:36:10 -060042static int ide_bus_ok[CONFIG_SYS_IDE_MAXBUS];
43
44struct blk_desc ide_dev_desc[CONFIG_SYS_IDE_MAXDEVICE];
45
46#define IDE_TIME_OUT 2000 /* 2 sec timeout */
47
48#define ATAPI_TIME_OUT 7000 /* 7 sec timeout (5 sec seems to work...) */
49
50#define IDE_SPIN_UP_TIME_OUT 5000 /* 5 sec spin-up timeout */
51
Simon Glasse9be1ee2016-05-01 11:36:10 -060052#ifdef CONFIG_IDE_RESET
Simon Glasse9be1ee2016-05-01 11:36:10 -060053static 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;
Simon Glasse9be1ee2016-05-01 11:36:10 -060059
60 ide_set_reset(1); /* assert reset */
61
62 /* the reset signal shall be asserted for et least 25 us */
63 udelay(25);
64
Stefan Roese29caf932022-09-02 14:10:46 +020065 schedule();
Simon Glasse9be1ee2016-05-01 11:36:10 -060066
67 /* de-assert RESET signal */
68 ide_set_reset(0);
69
Simon Glass4d89f4b2023-04-25 10:54:27 -060070 mdelay(250);
Simon Glasse9be1ee2016-05-01 11:36:10 -060071}
72#else
73#define ide_reset() /* dummy */
74#endif /* CONFIG_IDE_RESET */
75
Simon Glassf8e87e72023-04-25 10:54:33 -060076static void ide_outb(int dev, int port, unsigned char val)
Simon Glassbc65bff2023-04-25 10:54:32 -060077{
78 debug("ide_outb (dev= %d, port= 0x%x, val= 0x%02x) : @ 0x%08lx\n",
79 dev, port, val, ATA_CURR_BASE(dev) + port);
80
81 outb(val, ATA_CURR_BASE(dev) + port);
82}
83
Simon Glassf8e87e72023-04-25 10:54:33 -060084static unsigned char ide_inb(int dev, int port)
Simon Glassbc65bff2023-04-25 10:54:32 -060085{
86 uchar val;
87
88 val = inb(ATA_CURR_BASE(dev) + port);
89
90 debug("ide_inb (dev= %d, port= 0x%x) : @ 0x%08lx -> 0x%02x\n",
91 dev, port, ATA_CURR_BASE(dev) + port, val);
92 return val;
93}
94
Simon Glassf8e87e72023-04-25 10:54:33 -060095static void ide_input_swap_data(int dev, ulong *sect_buf, int words)
Simon Glassbc65bff2023-04-25 10:54:32 -060096{
97 uintptr_t paddr = (ATA_CURR_BASE(dev) + ATA_DATA_REG);
98 ushort *dbuf = (ushort *)sect_buf;
99
100 debug("in input swap data base for read is %p\n", (void *)paddr);
101
102 while (words--) {
103 EIEIO;
104 *dbuf++ = be16_to_cpu(inw(paddr));
105 EIEIO;
106 *dbuf++ = be16_to_cpu(inw(paddr));
107 }
108}
109
Simon Glasse9be1ee2016-05-01 11:36:10 -0600110/*
111 * Wait until Busy bit is off, or timeout (in ms)
112 * Return last status
113 */
114static uchar ide_wait(int dev, ulong t)
115{
116 ulong delay = 10 * t; /* poll every 100 us */
117 uchar c;
118
119 while ((c = ide_inb(dev, ATA_STATUS)) & ATA_STAT_BUSY) {
120 udelay(100);
121 if (delay-- == 0)
122 break;
123 }
124 return c;
125}
126
127/*
128 * copy src to dest, skipping leading and trailing blanks and null
129 * terminate the string
130 * "len" is the size of available memory including the terminating '\0'
131 */
132static void ident_cpy(unsigned char *dst, unsigned char *src,
133 unsigned int len)
134{
135 unsigned char *end, *last;
136
137 last = dst;
138 end = src + len - 1;
139
140 /* reserve space for '\0' */
141 if (len < 2)
142 goto OUT;
143
144 /* skip leading white space */
145 while ((*src) && (src < end) && (*src == ' '))
146 ++src;
147
148 /* copy string, omitting trailing white space */
149 while ((*src) && (src < end)) {
150 *dst++ = *src;
151 if (*src++ != ' ')
152 last = dst;
153 }
154OUT:
155 *last = '\0';
156}
157
Simon Glasse9be1ee2016-05-01 11:36:10 -0600158/****************************************************************************
159 * ATAPI Support
160 */
161
Simon Glasse9be1ee2016-05-01 11:36:10 -0600162/* since ATAPI may use commands with not 4 bytes alligned length
163 * we have our own transfer functions, 2 bytes alligned */
Simon Glassf8e87e72023-04-25 10:54:33 -0600164static void ide_output_data_shorts(int dev, ushort *sect_buf, int shorts)
Simon Glasse9be1ee2016-05-01 11:36:10 -0600165{
Reinoud Zandijk0a527fd2021-02-24 17:44:42 +0100166 uintptr_t paddr = (ATA_CURR_BASE(dev) + ATA_DATA_REG);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600167 ushort *dbuf;
Simon Glasse9be1ee2016-05-01 11:36:10 -0600168
Simon Glasse9be1ee2016-05-01 11:36:10 -0600169 dbuf = (ushort *)sect_buf;
170
Reinoud Zandijk0a527fd2021-02-24 17:44:42 +0100171 debug("in output data shorts base for read is %p\n", (void *)paddr);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600172
173 while (shorts--) {
174 EIEIO;
Reinoud Zandijk0a527fd2021-02-24 17:44:42 +0100175 outw(cpu_to_le16(*dbuf++), paddr);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600176 }
177}
178
Simon Glassf8e87e72023-04-25 10:54:33 -0600179static void ide_input_data_shorts(int dev, ushort *sect_buf, int shorts)
Simon Glasse9be1ee2016-05-01 11:36:10 -0600180{
Reinoud Zandijk0a527fd2021-02-24 17:44:42 +0100181 uintptr_t paddr = (ATA_CURR_BASE(dev) + ATA_DATA_REG);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600182 ushort *dbuf;
Simon Glasse9be1ee2016-05-01 11:36:10 -0600183
Simon Glasse9be1ee2016-05-01 11:36:10 -0600184 dbuf = (ushort *)sect_buf;
185
Reinoud Zandijk0a527fd2021-02-24 17:44:42 +0100186 debug("in input data shorts base for read is %p\n", (void *)paddr);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600187
188 while (shorts--) {
189 EIEIO;
Reinoud Zandijk0a527fd2021-02-24 17:44:42 +0100190 *dbuf++ = le16_to_cpu(inw(paddr));
Simon Glasse9be1ee2016-05-01 11:36:10 -0600191 }
192}
193
Simon Glasse9be1ee2016-05-01 11:36:10 -0600194/*
195 * Wait until (Status & mask) == res, or timeout (in ms)
196 * Return last status
197 * This is used since some ATAPI CD ROMs clears their Busy Bit first
198 * and then they set their DRQ Bit
199 */
200static uchar atapi_wait_mask(int dev, ulong t, uchar mask, uchar res)
201{
202 ulong delay = 10 * t; /* poll every 100 us */
203 uchar c;
204
205 /* prevents to read the status before valid */
206 c = ide_inb(dev, ATA_DEV_CTL);
207
208 while (((c = ide_inb(dev, ATA_STATUS)) & mask) != res) {
209 /* break if error occurs (doesn't make sense to wait more) */
210 if ((c & ATA_STAT_ERR) == ATA_STAT_ERR)
211 break;
212 udelay(100);
213 if (delay-- == 0)
214 break;
215 }
216 return c;
217}
218
219/*
220 * issue an atapi command
221 */
Simon Glass1b33fd82023-04-25 10:54:36 -0600222static unsigned char atapi_issue(int device, unsigned char *ccb, int ccblen,
223 unsigned char *buffer, int buflen)
Simon Glasse9be1ee2016-05-01 11:36:10 -0600224{
225 unsigned char c, err, mask, res;
226 int n;
227
Simon Glasse9be1ee2016-05-01 11:36:10 -0600228 /* Select device
229 */
230 mask = ATA_STAT_BUSY | ATA_STAT_DRQ;
231 res = 0;
232 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
233 c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
234 if ((c & mask) != res) {
235 printf("ATAPI_ISSUE: device %d not ready status %X\n", device,
236 c);
237 err = 0xFF;
238 goto AI_OUT;
239 }
240 /* write taskfile */
241 ide_outb(device, ATA_ERROR_REG, 0); /* no DMA, no overlaped */
242 ide_outb(device, ATA_SECT_CNT, 0);
243 ide_outb(device, ATA_SECT_NUM, 0);
244 ide_outb(device, ATA_CYL_LOW, (unsigned char) (buflen & 0xFF));
245 ide_outb(device, ATA_CYL_HIGH,
246 (unsigned char) ((buflen >> 8) & 0xFF));
247 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
248
Heinrich Schuchardte6e9a4f2020-02-27 18:28:00 +0100249 ide_outb(device, ATA_COMMAND, ATA_CMD_PACKET);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600250 udelay(50);
251
252 mask = ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR;
253 res = ATA_STAT_DRQ;
254 c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
255
256 if ((c & mask) != res) { /* DRQ must be 1, BSY 0 */
257 printf("ATAPI_ISSUE: Error (no IRQ) before sending ccb dev %d status 0x%02x\n",
258 device, c);
259 err = 0xFF;
260 goto AI_OUT;
261 }
262
263 /* write command block */
264 ide_output_data_shorts(device, (unsigned short *)ccb, ccblen / 2);
265
266 /* ATAPI Command written wait for completition */
Simon Glass4d89f4b2023-04-25 10:54:27 -0600267 mdelay(5); /* device must set bsy */
Simon Glasse9be1ee2016-05-01 11:36:10 -0600268
269 mask = ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR;
270 /*
271 * if no data wait for DRQ = 0 BSY = 0
272 * if data wait for DRQ = 1 BSY = 0
273 */
274 res = 0;
275 if (buflen)
276 res = ATA_STAT_DRQ;
277 c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
278 if ((c & mask) != res) {
279 if (c & ATA_STAT_ERR) {
280 err = (ide_inb(device, ATA_ERROR_REG)) >> 4;
281 debug("atapi_issue 1 returned sense key %X status %02X\n",
282 err, c);
283 } else {
284 printf("ATAPI_ISSUE: (no DRQ) after sending ccb (%x) status 0x%02x\n",
285 ccb[0], c);
286 err = 0xFF;
287 }
288 goto AI_OUT;
289 }
290 n = ide_inb(device, ATA_CYL_HIGH);
291 n <<= 8;
292 n += ide_inb(device, ATA_CYL_LOW);
293 if (n > buflen) {
294 printf("ERROR, transfer bytes %d requested only %d\n", n,
295 buflen);
296 err = 0xff;
297 goto AI_OUT;
298 }
299 if ((n == 0) && (buflen < 0)) {
300 printf("ERROR, transfer bytes %d requested %d\n", n, buflen);
301 err = 0xff;
302 goto AI_OUT;
303 }
304 if (n != buflen) {
305 debug("WARNING, transfer bytes %d not equal with requested %d\n",
306 n, buflen);
307 }
308 if (n != 0) { /* data transfer */
309 debug("ATAPI_ISSUE: %d Bytes to transfer\n", n);
310 /* we transfer shorts */
311 n >>= 1;
312 /* ok now decide if it is an in or output */
313 if ((ide_inb(device, ATA_SECT_CNT) & 0x02) == 0) {
314 debug("Write to device\n");
315 ide_output_data_shorts(device, (unsigned short *)buffer,
316 n);
317 } else {
318 debug("Read from device @ %p shorts %d\n", buffer, n);
319 ide_input_data_shorts(device, (unsigned short *)buffer,
320 n);
321 }
322 }
Simon Glass4d89f4b2023-04-25 10:54:27 -0600323 mdelay(5); /* seems that some CD ROMs need this... */
Simon Glasse9be1ee2016-05-01 11:36:10 -0600324 mask = ATA_STAT_BUSY | ATA_STAT_ERR;
325 res = 0;
326 c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
327 if ((c & ATA_STAT_ERR) == ATA_STAT_ERR) {
328 err = (ide_inb(device, ATA_ERROR_REG) >> 4);
329 debug("atapi_issue 2 returned sense key %X status %X\n", err,
330 c);
331 } else {
332 err = 0;
333 }
334AI_OUT:
Simon Glasse9be1ee2016-05-01 11:36:10 -0600335 return err;
336}
337
338/*
339 * sending the command to atapi_issue. If an status other than good
340 * returns, an request_sense will be issued
341 */
342
343#define ATAPI_DRIVE_NOT_READY 100
344#define ATAPI_UNIT_ATTN 10
345
Simon Glass1b33fd82023-04-25 10:54:36 -0600346static unsigned char atapi_issue_autoreq(int device, unsigned char *ccb,
347 int ccblen,
348 unsigned char *buffer, int buflen)
Simon Glasse9be1ee2016-05-01 11:36:10 -0600349{
350 unsigned char sense_data[18], sense_ccb[12];
351 unsigned char res, key, asc, ascq;
352 int notready, unitattn;
353
354 unitattn = ATAPI_UNIT_ATTN;
355 notready = ATAPI_DRIVE_NOT_READY;
356
357retry:
358 res = atapi_issue(device, ccb, ccblen, buffer, buflen);
359 if (res == 0)
360 return 0; /* Ok */
361
362 if (res == 0xFF)
363 return 0xFF; /* error */
364
365 debug("(auto_req)atapi_issue returned sense key %X\n", res);
366
367 memset(sense_ccb, 0, sizeof(sense_ccb));
368 memset(sense_data, 0, sizeof(sense_data));
369 sense_ccb[0] = ATAPI_CMD_REQ_SENSE;
370 sense_ccb[4] = 18; /* allocation Length */
371
372 res = atapi_issue(device, sense_ccb, 12, sense_data, 18);
373 key = (sense_data[2] & 0xF);
374 asc = (sense_data[12]);
375 ascq = (sense_data[13]);
376
377 debug("ATAPI_CMD_REQ_SENSE returned %x\n", res);
378 debug(" Sense page: %02X key %02X ASC %02X ASCQ %02X\n",
379 sense_data[0], key, asc, ascq);
380
381 if ((key == 0))
382 return 0; /* ok device ready */
383
384 if ((key == 6) || (asc == 0x29) || (asc == 0x28)) { /* Unit Attention */
385 if (unitattn-- > 0) {
Simon Glass4d89f4b2023-04-25 10:54:27 -0600386 mdelay(200);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600387 goto retry;
388 }
389 printf("Unit Attention, tried %d\n", ATAPI_UNIT_ATTN);
390 goto error;
391 }
392 if ((asc == 0x4) && (ascq == 0x1)) {
393 /* not ready, but will be ready soon */
394 if (notready-- > 0) {
Simon Glass4d89f4b2023-04-25 10:54:27 -0600395 mdelay(200);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600396 goto retry;
397 }
398 printf("Drive not ready, tried %d times\n",
399 ATAPI_DRIVE_NOT_READY);
400 goto error;
401 }
402 if (asc == 0x3a) {
403 debug("Media not present\n");
404 goto error;
405 }
406
407 printf("ERROR: Unknown Sense key %02X ASC %02X ASCQ %02X\n", key, asc,
408 ascq);
409error:
410 debug("ERROR Sense key %02X ASC %02X ASCQ %02X\n", key, asc, ascq);
411 return 0xFF;
412}
413
414/*
415 * atapi_read:
416 * we transfer only one block per command, since the multiple DRQ per
417 * command is not yet implemented
418 */
419#define ATAPI_READ_MAX_BYTES 2048 /* we read max 2kbytes */
420#define ATAPI_READ_BLOCK_SIZE 2048 /* assuming CD part */
421#define ATAPI_READ_MAX_BLOCK (ATAPI_READ_MAX_BYTES/ATAPI_READ_BLOCK_SIZE)
422
Simon Glass1b33fd82023-04-25 10:54:36 -0600423static ulong atapi_read(struct udevice *dev, lbaint_t blknr, lbaint_t blkcnt,
424 void *buffer)
Simon Glasse9be1ee2016-05-01 11:36:10 -0600425{
Simon Glass646deed2023-04-25 10:54:35 -0600426 struct blk_desc *block_dev = dev_get_uclass_plat(dev);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600427 int device = block_dev->devnum;
428 ulong n = 0;
429 unsigned char ccb[12]; /* Command descriptor block */
430 ulong cnt;
431
432 debug("atapi_read dev %d start " LBAF " blocks " LBAF
433 " buffer at %lX\n", device, blknr, blkcnt, (ulong) buffer);
434
435 do {
436 if (blkcnt > ATAPI_READ_MAX_BLOCK)
437 cnt = ATAPI_READ_MAX_BLOCK;
438 else
439 cnt = blkcnt;
440
441 ccb[0] = ATAPI_CMD_READ_12;
442 ccb[1] = 0; /* reserved */
443 ccb[2] = (unsigned char) (blknr >> 24) & 0xFF; /* MSB Block */
444 ccb[3] = (unsigned char) (blknr >> 16) & 0xFF; /* */
445 ccb[4] = (unsigned char) (blknr >> 8) & 0xFF;
446 ccb[5] = (unsigned char) blknr & 0xFF; /* LSB Block */
447 ccb[6] = (unsigned char) (cnt >> 24) & 0xFF; /* MSB Block cnt */
448 ccb[7] = (unsigned char) (cnt >> 16) & 0xFF;
449 ccb[8] = (unsigned char) (cnt >> 8) & 0xFF;
450 ccb[9] = (unsigned char) cnt & 0xFF; /* LSB Block */
451 ccb[10] = 0; /* reserved */
452 ccb[11] = 0; /* reserved */
453
454 if (atapi_issue_autoreq(device, ccb, 12,
455 (unsigned char *)buffer,
456 cnt * ATAPI_READ_BLOCK_SIZE)
457 == 0xFF) {
458 return n;
459 }
460 n += cnt;
461 blkcnt -= cnt;
462 blknr += cnt;
463 buffer += (cnt * ATAPI_READ_BLOCK_SIZE);
464 } while (blkcnt > 0);
465 return n;
466}
467
Simon Glass646deed2023-04-25 10:54:35 -0600468#ifdef CONFIG_ATAPI
469
Simon Glasse9be1ee2016-05-01 11:36:10 -0600470static void atapi_inquiry(struct blk_desc *dev_desc)
471{
472 unsigned char ccb[12]; /* Command descriptor block */
473 unsigned char iobuf[64]; /* temp buf */
474 unsigned char c;
475 int device;
476
477 device = dev_desc->devnum;
478 dev_desc->type = DEV_TYPE_UNKNOWN; /* not yet valid */
Simon Glasse9be1ee2016-05-01 11:36:10 -0600479
480 memset(ccb, 0, sizeof(ccb));
481 memset(iobuf, 0, sizeof(iobuf));
482
483 ccb[0] = ATAPI_CMD_INQUIRY;
484 ccb[4] = 40; /* allocation Legnth */
485 c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *)iobuf, 40);
486
487 debug("ATAPI_CMD_INQUIRY returned %x\n", c);
488 if (c != 0)
489 return;
490
491 /* copy device ident strings */
492 ident_cpy((unsigned char *)dev_desc->vendor, &iobuf[8], 8);
493 ident_cpy((unsigned char *)dev_desc->product, &iobuf[16], 16);
494 ident_cpy((unsigned char *)dev_desc->revision, &iobuf[32], 5);
495
496 dev_desc->lun = 0;
497 dev_desc->lba = 0;
498 dev_desc->blksz = 0;
499 dev_desc->log2blksz = LOG2_INVALID(typeof(dev_desc->log2blksz));
500 dev_desc->type = iobuf[0] & 0x1f;
501
502 if ((iobuf[1] & 0x80) == 0x80)
503 dev_desc->removable = 1;
504 else
505 dev_desc->removable = 0;
506
507 memset(ccb, 0, sizeof(ccb));
508 memset(iobuf, 0, sizeof(iobuf));
509 ccb[0] = ATAPI_CMD_START_STOP;
510 ccb[4] = 0x03; /* start */
511
512 c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *)iobuf, 0);
513
514 debug("ATAPI_CMD_START_STOP returned %x\n", c);
515 if (c != 0)
516 return;
517
518 memset(ccb, 0, sizeof(ccb));
519 memset(iobuf, 0, sizeof(iobuf));
520 c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *)iobuf, 0);
521
522 debug("ATAPI_CMD_UNIT_TEST_READY returned %x\n", c);
523 if (c != 0)
524 return;
525
526 memset(ccb, 0, sizeof(ccb));
527 memset(iobuf, 0, sizeof(iobuf));
528 ccb[0] = ATAPI_CMD_READ_CAP;
529 c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *)iobuf, 8);
530 debug("ATAPI_CMD_READ_CAP returned %x\n", c);
531 if (c != 0)
532 return;
533
534 debug("Read Cap: LBA %02X%02X%02X%02X blksize %02X%02X%02X%02X\n",
535 iobuf[0], iobuf[1], iobuf[2], iobuf[3],
536 iobuf[4], iobuf[5], iobuf[6], iobuf[7]);
537
538 dev_desc->lba = ((unsigned long) iobuf[0] << 24) +
539 ((unsigned long) iobuf[1] << 16) +
540 ((unsigned long) iobuf[2] << 8) + ((unsigned long) iobuf[3]);
541 dev_desc->blksz = ((unsigned long) iobuf[4] << 24) +
542 ((unsigned long) iobuf[5] << 16) +
543 ((unsigned long) iobuf[6] << 8) + ((unsigned long) iobuf[7]);
544 dev_desc->log2blksz = LOG2(dev_desc->blksz);
545#ifdef CONFIG_LBA48
546 /* ATAPI devices cannot use 48bit addressing (ATA/ATAPI v7) */
547 dev_desc->lba48 = 0;
548#endif
549 return;
550}
551
552#endif /* CONFIG_ATAPI */
553
554static void ide_ident(struct blk_desc *dev_desc)
555{
556 unsigned char c;
557 hd_driveid_t iop;
Simon Glasse9be1ee2016-05-01 11:36:10 -0600558#ifdef CONFIG_ATAPI
Simon Glass606e0542022-08-11 19:34:53 -0600559 bool is_atapi = false;
Simon Glass2a165952023-04-25 10:54:37 -0600560 int tries = 1;
Simon Glasse9be1ee2016-05-01 11:36:10 -0600561#endif
562 int device;
563
564 device = dev_desc->devnum;
565 printf(" Device %d: ", device);
566
Simon Glasse9be1ee2016-05-01 11:36:10 -0600567 /* Select device
568 */
569 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
Simon Glass8149b152022-09-17 09:00:09 -0600570 dev_desc->uclass_id = UCLASS_IDE;
Simon Glasse9be1ee2016-05-01 11:36:10 -0600571#ifdef CONFIG_ATAPI
572
Simon Glass2a165952023-04-25 10:54:37 -0600573 tries = 2;
Simon Glasse9be1ee2016-05-01 11:36:10 -0600574
575 /* Warning: This will be tricky to read */
Simon Glass2a165952023-04-25 10:54:37 -0600576 while (tries) {
Simon Glasse9be1ee2016-05-01 11:36:10 -0600577 /* check signature */
578 if ((ide_inb(device, ATA_SECT_CNT) == 0x01) &&
579 (ide_inb(device, ATA_SECT_NUM) == 0x01) &&
580 (ide_inb(device, ATA_CYL_LOW) == 0x14) &&
581 (ide_inb(device, ATA_CYL_HIGH) == 0xEB)) {
582 /* ATAPI Signature found */
Simon Glass606e0542022-08-11 19:34:53 -0600583 is_atapi = true;
Simon Glasse9be1ee2016-05-01 11:36:10 -0600584 /*
585 * Start Ident Command
586 */
Heinrich Schuchardte6e9a4f2020-02-27 18:28:00 +0100587 ide_outb(device, ATA_COMMAND, ATA_CMD_ID_ATAPI);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600588 /*
589 * Wait for completion - ATAPI devices need more time
590 * to become ready
591 */
592 c = ide_wait(device, ATAPI_TIME_OUT);
593 } else
594#endif
595 {
596 /*
597 * Start Ident Command
598 */
Heinrich Schuchardte6e9a4f2020-02-27 18:28:00 +0100599 ide_outb(device, ATA_COMMAND, ATA_CMD_ID_ATA);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600600
601 /*
602 * Wait for completion
603 */
604 c = ide_wait(device, IDE_TIME_OUT);
605 }
Simon Glasse9be1ee2016-05-01 11:36:10 -0600606
607 if (((c & ATA_STAT_DRQ) == 0) ||
608 ((c & (ATA_STAT_FAULT | ATA_STAT_ERR)) != 0)) {
609#ifdef CONFIG_ATAPI
610 {
611 /*
612 * Need to soft reset the device
613 * in case it's an ATAPI...
614 */
615 debug("Retrying...\n");
616 ide_outb(device, ATA_DEV_HD,
617 ATA_LBA | ATA_DEVICE(device));
Simon Glass4d89f4b2023-04-25 10:54:27 -0600618 mdelay(100);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600619 ide_outb(device, ATA_COMMAND, 0x08);
Simon Glass4d89f4b2023-04-25 10:54:27 -0600620 mdelay(500);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600621 }
622 /*
623 * Select device
624 */
625 ide_outb(device, ATA_DEV_HD,
626 ATA_LBA | ATA_DEVICE(device));
Simon Glass2a165952023-04-25 10:54:37 -0600627 tries--;
Simon Glasse9be1ee2016-05-01 11:36:10 -0600628#else
629 return;
630#endif
631 }
632#ifdef CONFIG_ATAPI
633 else
634 break;
635 } /* see above - ugly to read */
636
Simon Glass2a165952023-04-25 10:54:37 -0600637 if (!tries) /* Not found */
Simon Glasse9be1ee2016-05-01 11:36:10 -0600638 return;
639#endif
640
641 ide_input_swap_data(device, (ulong *)&iop, ATA_SECTORWORDS);
642
643 ident_cpy((unsigned char *)dev_desc->revision, iop.fw_rev,
644 sizeof(dev_desc->revision));
645 ident_cpy((unsigned char *)dev_desc->vendor, iop.model,
646 sizeof(dev_desc->vendor));
647 ident_cpy((unsigned char *)dev_desc->product, iop.serial_no,
648 sizeof(dev_desc->product));
Simon Glasse9be1ee2016-05-01 11:36:10 -0600649
650 if ((iop.config & 0x0080) == 0x0080)
651 dev_desc->removable = 1;
652 else
653 dev_desc->removable = 0;
654
655#ifdef CONFIG_ATAPI
Simon Glass606e0542022-08-11 19:34:53 -0600656 if (is_atapi) {
Simon Glass646deed2023-04-25 10:54:35 -0600657 dev_desc->atapi = true;
Simon Glasse9be1ee2016-05-01 11:36:10 -0600658 atapi_inquiry(dev_desc);
659 return;
660 }
661#endif /* CONFIG_ATAPI */
662
Reinoud Zandijk0a527fd2021-02-24 17:44:42 +0100663 iop.lba_capacity[0] = be16_to_cpu(iop.lba_capacity[0]);
664 iop.lba_capacity[1] = be16_to_cpu(iop.lba_capacity[1]);
665 dev_desc->lba =
666 ((unsigned long)iop.lba_capacity[0]) |
667 ((unsigned long)iop.lba_capacity[1] << 16);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600668
669#ifdef CONFIG_LBA48
670 if (iop.command_set_2 & 0x0400) { /* LBA 48 support */
671 dev_desc->lba48 = 1;
Reinoud Zandijk0a527fd2021-02-24 17:44:42 +0100672 for (int i = 0; i < 4; i++)
673 iop.lba48_capacity[i] = be16_to_cpu(iop.lba48_capacity[i]);
674 dev_desc->lba =
675 ((unsigned long long)iop.lba48_capacity[0] |
676 ((unsigned long long)iop.lba48_capacity[1] << 16) |
677 ((unsigned long long)iop.lba48_capacity[2] << 32) |
678 ((unsigned long long)iop.lba48_capacity[3] << 48));
Simon Glasse9be1ee2016-05-01 11:36:10 -0600679 } else {
680 dev_desc->lba48 = 0;
681 }
682#endif /* CONFIG_LBA48 */
683 /* assuming HD */
684 dev_desc->type = DEV_TYPE_HARDDISK;
685 dev_desc->blksz = ATA_BLOCKSIZE;
686 dev_desc->log2blksz = LOG2(dev_desc->blksz);
687 dev_desc->lun = 0; /* just to fill something in... */
688
689#if 0 /* only used to test the powersaving mode,
690 * if enabled, the drive goes after 5 sec
691 * in standby mode */
692 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
693 c = ide_wait(device, IDE_TIME_OUT);
694 ide_outb(device, ATA_SECT_CNT, 1);
695 ide_outb(device, ATA_LBA_LOW, 0);
696 ide_outb(device, ATA_LBA_MID, 0);
697 ide_outb(device, ATA_LBA_HIGH, 0);
698 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
699 ide_outb(device, ATA_COMMAND, 0xe3);
700 udelay(50);
701 c = ide_wait(device, IDE_TIME_OUT); /* can't take over 500 ms */
702#endif
703}
704
Simon Glass80778f52023-04-25 10:54:30 -0600705static void ide_init(void)
Simon Glasse9be1ee2016-05-01 11:36:10 -0600706{
707 unsigned char c;
708 int i, bus;
709
Stefan Roese29caf932022-09-02 14:10:46 +0200710 schedule();
Simon Glasse9be1ee2016-05-01 11:36:10 -0600711
Simon Glasse9be1ee2016-05-01 11:36:10 -0600712 /* ATAPI Drives seems to need a proper IDE Reset */
713 ide_reset();
714
Simon Glasse9be1ee2016-05-01 11:36:10 -0600715 /*
716 * Wait for IDE to get ready.
717 * According to spec, this can take up to 31 seconds!
718 */
719 for (bus = 0; bus < CONFIG_SYS_IDE_MAXBUS; ++bus) {
720 int dev =
721 bus * (CONFIG_SYS_IDE_MAXDEVICE /
722 CONFIG_SYS_IDE_MAXBUS);
723
Simon Glasse9be1ee2016-05-01 11:36:10 -0600724 printf("Bus %d: ", bus);
725
726 ide_bus_ok[bus] = 0;
727
Simon Glass4d89f4b2023-04-25 10:54:27 -0600728 /* Select device */
729 mdelay(100);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600730 ide_outb(dev, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(dev));
Simon Glass4d89f4b2023-04-25 10:54:27 -0600731 mdelay(100);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600732 i = 0;
733 do {
Simon Glass4d89f4b2023-04-25 10:54:27 -0600734 mdelay(10);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600735
736 c = ide_inb(dev, ATA_STATUS);
737 i++;
738 if (i > (ATA_RESET_TIME * 100)) {
739 puts("** Timeout **\n");
Simon Glasse9be1ee2016-05-01 11:36:10 -0600740 return;
741 }
742 if ((i >= 100) && ((i % 100) == 0))
743 putc('.');
744
745 } while (c & ATA_STAT_BUSY);
746
747 if (c & (ATA_STAT_BUSY | ATA_STAT_FAULT)) {
748 puts("not available ");
749 debug("Status = 0x%02X ", c);
750#ifndef CONFIG_ATAPI /* ATAPI Devices do not set DRDY */
751 } else if ((c & ATA_STAT_READY) == 0) {
752 puts("not available ");
753 debug("Status = 0x%02X ", c);
754#endif
755 } else {
756 puts("OK ");
757 ide_bus_ok[bus] = 1;
758 }
Stefan Roese29caf932022-09-02 14:10:46 +0200759 schedule();
Simon Glasse9be1ee2016-05-01 11:36:10 -0600760 }
761
762 putc('\n');
763
Simon Glasse9be1ee2016-05-01 11:36:10 -0600764 for (i = 0; i < CONFIG_SYS_IDE_MAXDEVICE; ++i) {
Simon Glasse9be1ee2016-05-01 11:36:10 -0600765 ide_dev_desc[i].type = DEV_TYPE_UNKNOWN;
Simon Glass8149b152022-09-17 09:00:09 -0600766 ide_dev_desc[i].uclass_id = UCLASS_IDE;
Simon Glasse9be1ee2016-05-01 11:36:10 -0600767 ide_dev_desc[i].devnum = i;
768 ide_dev_desc[i].part_type = PART_TYPE_UNKNOWN;
769 ide_dev_desc[i].blksz = 0;
770 ide_dev_desc[i].log2blksz =
771 LOG2_INVALID(typeof(ide_dev_desc[i].log2blksz));
772 ide_dev_desc[i].lba = 0;
Simon Glasse9be1ee2016-05-01 11:36:10 -0600773 if (!ide_bus_ok[IDE_BUS(i)])
774 continue;
Simon Glasse9be1ee2016-05-01 11:36:10 -0600775 ide_ident(&ide_dev_desc[i]);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600776 dev_print(&ide_dev_desc[i]);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600777 }
Stefan Roese29caf932022-09-02 14:10:46 +0200778 schedule();
Simon Glasse9be1ee2016-05-01 11:36:10 -0600779}
780
Simon Glassf8e87e72023-04-25 10:54:33 -0600781static void ide_output_data(int dev, const ulong *sect_buf, int words)
Simon Glasse9be1ee2016-05-01 11:36:10 -0600782{
Reinoud Zandijk0a527fd2021-02-24 17:44:42 +0100783 uintptr_t paddr = (ATA_CURR_BASE(dev) + ATA_DATA_REG);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600784 ushort *dbuf;
Simon Glasse9be1ee2016-05-01 11:36:10 -0600785
Simon Glasse9be1ee2016-05-01 11:36:10 -0600786 dbuf = (ushort *)sect_buf;
Simon Glasse9be1ee2016-05-01 11:36:10 -0600787 while (words--) {
788 EIEIO;
Reinoud Zandijk0a527fd2021-02-24 17:44:42 +0100789 outw(cpu_to_le16(*dbuf++), paddr);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600790 EIEIO;
Reinoud Zandijk0a527fd2021-02-24 17:44:42 +0100791 outw(cpu_to_le16(*dbuf++), paddr);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600792 }
793}
Reinoud Zandijk0a527fd2021-02-24 17:44:42 +0100794
Simon Glassf8e87e72023-04-25 10:54:33 -0600795static void ide_input_data(int dev, ulong *sect_buf, int words)
Simon Glasse9be1ee2016-05-01 11:36:10 -0600796{
Reinoud Zandijk0a527fd2021-02-24 17:44:42 +0100797 uintptr_t paddr = (ATA_CURR_BASE(dev) + ATA_DATA_REG);
798 ushort *dbuf;
Simon Glasse9be1ee2016-05-01 11:36:10 -0600799
Reinoud Zandijk0a527fd2021-02-24 17:44:42 +0100800 dbuf = (ushort *)sect_buf;
801
802 debug("in input data base for read is %p\n", (void *)paddr);
803
804 while (words--) {
805 EIEIO;
806 *dbuf++ = le16_to_cpu(inw(paddr));
807 EIEIO;
808 *dbuf++ = le16_to_cpu(inw(paddr));
809 }
Reinoud Zandijk0a527fd2021-02-24 17:44:42 +0100810}
Simon Glasse9be1ee2016-05-01 11:36:10 -0600811
Simon Glass1b33fd82023-04-25 10:54:36 -0600812static ulong ide_read(struct udevice *dev, lbaint_t blknr, lbaint_t blkcnt,
813 void *buffer)
Simon Glasse9be1ee2016-05-01 11:36:10 -0600814{
Simon Glasscaa4daa2020-12-03 16:55:18 -0700815 struct blk_desc *block_dev = dev_get_uclass_plat(dev);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600816 int device = block_dev->devnum;
817 ulong n = 0;
818 unsigned char c;
819 unsigned char pwrsave = 0; /* power save */
820
821#ifdef CONFIG_LBA48
822 unsigned char lba48 = 0;
823
824 if (blknr & 0x0000fffff0000000ULL) {
825 /* more than 28 bits used, use 48bit mode */
826 lba48 = 1;
827 }
828#endif
829 debug("ide_read dev %d start " LBAF ", blocks " LBAF " buffer at %lX\n",
830 device, blknr, blkcnt, (ulong) buffer);
831
Simon Glasse9be1ee2016-05-01 11:36:10 -0600832 /* Select device
833 */
834 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
835 c = ide_wait(device, IDE_TIME_OUT);
836
837 if (c & ATA_STAT_BUSY) {
838 printf("IDE read: device %d not ready\n", device);
839 goto IDE_READ_E;
840 }
841
842 /* first check if the drive is in Powersaving mode, if yes,
843 * increase the timeout value */
Heinrich Schuchardte6e9a4f2020-02-27 18:28:00 +0100844 ide_outb(device, ATA_COMMAND, ATA_CMD_CHK_POWER);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600845 udelay(50);
846
847 c = ide_wait(device, IDE_TIME_OUT); /* can't take over 500 ms */
848
849 if (c & ATA_STAT_BUSY) {
850 printf("IDE read: device %d not ready\n", device);
851 goto IDE_READ_E;
852 }
853 if ((c & ATA_STAT_ERR) == ATA_STAT_ERR) {
854 printf("No Powersaving mode %X\n", c);
855 } else {
856 c = ide_inb(device, ATA_SECT_CNT);
857 debug("Powersaving %02X\n", c);
858 if (c == 0)
859 pwrsave = 1;
860 }
861
862
863 while (blkcnt-- > 0) {
864 c = ide_wait(device, IDE_TIME_OUT);
865
866 if (c & ATA_STAT_BUSY) {
867 printf("IDE read: device %d not ready\n", device);
868 break;
869 }
870#ifdef CONFIG_LBA48
871 if (lba48) {
872 /* write high bits */
873 ide_outb(device, ATA_SECT_CNT, 0);
874 ide_outb(device, ATA_LBA_LOW, (blknr >> 24) & 0xFF);
875#ifdef CONFIG_SYS_64BIT_LBA
876 ide_outb(device, ATA_LBA_MID, (blknr >> 32) & 0xFF);
877 ide_outb(device, ATA_LBA_HIGH, (blknr >> 40) & 0xFF);
878#else
879 ide_outb(device, ATA_LBA_MID, 0);
880 ide_outb(device, ATA_LBA_HIGH, 0);
881#endif
882 }
883#endif
884 ide_outb(device, ATA_SECT_CNT, 1);
885 ide_outb(device, ATA_LBA_LOW, (blknr >> 0) & 0xFF);
886 ide_outb(device, ATA_LBA_MID, (blknr >> 8) & 0xFF);
887 ide_outb(device, ATA_LBA_HIGH, (blknr >> 16) & 0xFF);
888
889#ifdef CONFIG_LBA48
890 if (lba48) {
891 ide_outb(device, ATA_DEV_HD,
892 ATA_LBA | ATA_DEVICE(device));
Reinoud Zandijk0a527fd2021-02-24 17:44:42 +0100893 ide_outb(device, ATA_COMMAND, ATA_CMD_PIO_READ_EXT);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600894
895 } else
896#endif
897 {
898 ide_outb(device, ATA_DEV_HD, ATA_LBA |
899 ATA_DEVICE(device) | ((blknr >> 24) & 0xF));
Reinoud Zandijk0a527fd2021-02-24 17:44:42 +0100900 ide_outb(device, ATA_COMMAND, ATA_CMD_PIO_READ);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600901 }
902
903 udelay(50);
904
905 if (pwrsave) {
906 /* may take up to 4 sec */
907 c = ide_wait(device, IDE_SPIN_UP_TIME_OUT);
908 pwrsave = 0;
909 } else {
910 /* can't take over 500 ms */
911 c = ide_wait(device, IDE_TIME_OUT);
912 }
913
914 if ((c & (ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR)) !=
915 ATA_STAT_DRQ) {
916 printf("Error (no IRQ) dev %d blk " LBAF
917 ": status %#02x\n", device, blknr, c);
918 break;
919 }
920
921 ide_input_data(device, buffer, ATA_SECTORWORDS);
922 (void) ide_inb(device, ATA_STATUS); /* clear IRQ */
923
924 ++n;
925 ++blknr;
926 buffer += ATA_BLOCKSIZE;
927 }
928IDE_READ_E:
Simon Glasse9be1ee2016-05-01 11:36:10 -0600929 return n;
930}
931
Simon Glass1b33fd82023-04-25 10:54:36 -0600932static ulong ide_write(struct udevice *dev, lbaint_t blknr, lbaint_t blkcnt,
933 const void *buffer)
Simon Glasse9be1ee2016-05-01 11:36:10 -0600934{
Simon Glasscaa4daa2020-12-03 16:55:18 -0700935 struct blk_desc *block_dev = dev_get_uclass_plat(dev);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600936 int device = block_dev->devnum;
937 ulong n = 0;
938 unsigned char c;
939
940#ifdef CONFIG_LBA48
941 unsigned char lba48 = 0;
942
943 if (blknr & 0x0000fffff0000000ULL) {
944 /* more than 28 bits used, use 48bit mode */
945 lba48 = 1;
946 }
947#endif
948
Simon Glasse9be1ee2016-05-01 11:36:10 -0600949 /* Select device
950 */
951 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
952
953 while (blkcnt-- > 0) {
954 c = ide_wait(device, IDE_TIME_OUT);
955
956 if (c & ATA_STAT_BUSY) {
957 printf("IDE read: device %d not ready\n", device);
958 goto WR_OUT;
959 }
960#ifdef CONFIG_LBA48
961 if (lba48) {
962 /* write high bits */
963 ide_outb(device, ATA_SECT_CNT, 0);
964 ide_outb(device, ATA_LBA_LOW, (blknr >> 24) & 0xFF);
965#ifdef CONFIG_SYS_64BIT_LBA
966 ide_outb(device, ATA_LBA_MID, (blknr >> 32) & 0xFF);
967 ide_outb(device, ATA_LBA_HIGH, (blknr >> 40) & 0xFF);
968#else
969 ide_outb(device, ATA_LBA_MID, 0);
970 ide_outb(device, ATA_LBA_HIGH, 0);
971#endif
972 }
973#endif
974 ide_outb(device, ATA_SECT_CNT, 1);
975 ide_outb(device, ATA_LBA_LOW, (blknr >> 0) & 0xFF);
976 ide_outb(device, ATA_LBA_MID, (blknr >> 8) & 0xFF);
977 ide_outb(device, ATA_LBA_HIGH, (blknr >> 16) & 0xFF);
978
979#ifdef CONFIG_LBA48
980 if (lba48) {
981 ide_outb(device, ATA_DEV_HD,
982 ATA_LBA | ATA_DEVICE(device));
Reinoud Zandijk0a527fd2021-02-24 17:44:42 +0100983 ide_outb(device, ATA_COMMAND, ATA_CMD_PIO_WRITE_EXT);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600984
985 } else
986#endif
987 {
988 ide_outb(device, ATA_DEV_HD, ATA_LBA |
989 ATA_DEVICE(device) | ((blknr >> 24) & 0xF));
Reinoud Zandijk0a527fd2021-02-24 17:44:42 +0100990 ide_outb(device, ATA_COMMAND, ATA_CMD_PIO_WRITE);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600991 }
992
993 udelay(50);
994
995 /* can't take over 500 ms */
996 c = ide_wait(device, IDE_TIME_OUT);
997
998 if ((c & (ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR)) !=
999 ATA_STAT_DRQ) {
1000 printf("Error (no IRQ) dev %d blk " LBAF
1001 ": status %#02x\n", device, blknr, c);
1002 goto WR_OUT;
1003 }
1004
1005 ide_output_data(device, buffer, ATA_SECTORWORDS);
1006 c = ide_inb(device, ATA_STATUS); /* clear IRQ */
1007 ++n;
1008 ++blknr;
1009 buffer += ATA_BLOCKSIZE;
1010 }
1011WR_OUT:
Simon Glasse9be1ee2016-05-01 11:36:10 -06001012 return n;
1013}
1014
Simon Glass646deed2023-04-25 10:54:35 -06001015ulong ide_or_atapi_read(struct udevice *dev, lbaint_t blknr, lbaint_t blkcnt,
1016 void *buffer)
1017{
1018 struct blk_desc *desc = dev_get_uclass_plat(dev);
1019
1020 if (IS_ENABLED(CONFIG_ATAPI) && desc->atapi)
1021 return atapi_read(dev, blknr, blkcnt, buffer);
1022
1023 return ide_read(dev, blknr, blkcnt, buffer);
1024}
1025
Bin Meng68e6f222017-09-10 05:12:51 -07001026static int ide_blk_probe(struct udevice *udev)
1027{
Simon Glasscaa4daa2020-12-03 16:55:18 -07001028 struct blk_desc *desc = dev_get_uclass_plat(udev);
Bin Meng68e6f222017-09-10 05:12:51 -07001029
1030 /* fill in device vendor/product/rev strings */
1031 strncpy(desc->vendor, ide_dev_desc[desc->devnum].vendor,
1032 BLK_VEN_SIZE);
1033 desc->vendor[BLK_VEN_SIZE] = '\0';
1034 strncpy(desc->product, ide_dev_desc[desc->devnum].product,
1035 BLK_PRD_SIZE);
1036 desc->product[BLK_PRD_SIZE] = '\0';
1037 strncpy(desc->revision, ide_dev_desc[desc->devnum].revision,
1038 BLK_REV_SIZE);
1039 desc->revision[BLK_REV_SIZE] = '\0';
1040
Bin Meng68e6f222017-09-10 05:12:51 -07001041 return 0;
1042}
1043
Simon Glass145df842016-05-01 11:36:22 -06001044static const struct blk_ops ide_blk_ops = {
Simon Glass646deed2023-04-25 10:54:35 -06001045 .read = ide_or_atapi_read,
Simon Glass145df842016-05-01 11:36:22 -06001046 .write = ide_write,
1047};
1048
1049U_BOOT_DRIVER(ide_blk) = {
1050 .name = "ide_blk",
1051 .id = UCLASS_BLK,
1052 .ops = &ide_blk_ops,
Bin Meng68e6f222017-09-10 05:12:51 -07001053 .probe = ide_blk_probe,
1054};
1055
Simon Glass0d77f8f2023-01-17 10:47:46 -07001056static int ide_bootdev_bind(struct udevice *dev)
1057{
1058 struct bootdev_uc_plat *ucp = dev_get_uclass_plat(dev);
1059
Simon Glasseacc2612023-01-17 10:48:08 -07001060 ucp->prio = BOOTDEVP_5_SCAN_SLOW;
Simon Glass0d77f8f2023-01-17 10:47:46 -07001061
1062 return 0;
1063}
1064
1065static int ide_bootdev_hunt(struct bootdev_hunter *info, bool show)
1066{
Simon Glass80778f52023-04-25 10:54:30 -06001067 struct udevice *dev;
1068
1069 uclass_first_device(UCLASS_IDE, &dev);
Simon Glass0d77f8f2023-01-17 10:47:46 -07001070
1071 return 0;
1072}
1073
1074struct bootdev_ops ide_bootdev_ops = {
1075};
1076
1077static const struct udevice_id ide_bootdev_ids[] = {
1078 { .compatible = "u-boot,bootdev-ide" },
1079 { }
1080};
1081
1082U_BOOT_DRIVER(ide_bootdev) = {
1083 .name = "ide_bootdev",
1084 .id = UCLASS_BOOTDEV,
1085 .ops = &ide_bootdev_ops,
1086 .bind = ide_bootdev_bind,
1087 .of_match = ide_bootdev_ids,
1088};
1089
1090BOOTDEV_HUNTER(ide_bootdev_hunter) = {
Simon Glasseacc2612023-01-17 10:48:08 -07001091 .prio = BOOTDEVP_5_SCAN_SLOW,
Simon Glass0d77f8f2023-01-17 10:47:46 -07001092 .uclass = UCLASS_IDE,
1093 .hunt = ide_bootdev_hunt,
1094 .drv = DM_DRIVER_REF(ide_bootdev),
1095};
1096
Bin Meng68e6f222017-09-10 05:12:51 -07001097static int ide_probe(struct udevice *udev)
1098{
1099 struct udevice *blk_dev;
1100 char name[20];
1101 int blksz;
1102 lbaint_t size;
1103 int i;
1104 int ret;
1105
Simon Glass80778f52023-04-25 10:54:30 -06001106 ide_init();
1107
Bin Meng68e6f222017-09-10 05:12:51 -07001108 for (i = 0; i < CONFIG_SYS_IDE_MAXDEVICE; i++) {
1109 if (ide_dev_desc[i].type != DEV_TYPE_UNKNOWN) {
1110 sprintf(name, "blk#%d", i);
1111
1112 blksz = ide_dev_desc[i].blksz;
1113 size = blksz * ide_dev_desc[i].lba;
Bin Meng1f4adab2017-09-10 05:12:52 -07001114
1115 /*
1116 * With CDROM, if there is no CD inserted, blksz will
1117 * be zero, don't bother to create IDE block device.
1118 */
1119 if (!blksz)
1120 continue;
Bin Meng68e6f222017-09-10 05:12:51 -07001121 ret = blk_create_devicef(udev, "ide_blk", name,
Simon Glasse33a5c62022-08-11 19:34:59 -06001122 UCLASS_IDE, i,
Bin Meng68e6f222017-09-10 05:12:51 -07001123 blksz, size, &blk_dev);
1124 if (ret)
1125 return ret;
AKASHI Takahiro4c73b032022-03-08 20:36:44 +09001126
1127 ret = blk_probe_or_unbind(blk_dev);
1128 if (ret)
1129 return ret;
Simon Glass0d77f8f2023-01-17 10:47:46 -07001130
1131 ret = bootdev_setup_for_dev(udev, "ide_bootdev");
1132 if (ret)
1133 return log_msg_ret("bootdev", ret);
Bin Meng68e6f222017-09-10 05:12:51 -07001134 }
1135 }
1136
1137 return 0;
1138}
1139
1140U_BOOT_DRIVER(ide) = {
1141 .name = "ide",
1142 .id = UCLASS_IDE,
1143 .probe = ide_probe,
1144};
1145
1146struct pci_device_id ide_supported[] = {
1147 { PCI_DEVICE_CLASS(PCI_CLASS_STORAGE_IDE << 8, 0xffff00) },
1148 { }
1149};
1150
1151U_BOOT_PCI_DEVICE(ide, ide_supported);
1152
1153UCLASS_DRIVER(ide) = {
1154 .name = "ide",
1155 .id = UCLASS_IDE,
Simon Glass145df842016-05-01 11:36:22 -06001156};