blob: 16b119ecbe10e31748b23f0f8f55ee82a47d0895 [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 -060042#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
Simon Glasse9be1ee2016-05-01 11:36:10 -060049static void ide_reset(void)
50{
Simon Glasse9be1ee2016-05-01 11:36:10 -060051 ide_set_reset(1); /* assert reset */
52
53 /* the reset signal shall be asserted for et least 25 us */
54 udelay(25);
55
Stefan Roese29caf932022-09-02 14:10:46 +020056 schedule();
Simon Glasse9be1ee2016-05-01 11:36:10 -060057
58 /* de-assert RESET signal */
59 ide_set_reset(0);
60
Simon Glass4d89f4b2023-04-25 10:54:27 -060061 mdelay(250);
Simon Glasse9be1ee2016-05-01 11:36:10 -060062}
63#else
64#define ide_reset() /* dummy */
65#endif /* CONFIG_IDE_RESET */
66
Simon Glassf8e87e72023-04-25 10:54:33 -060067static void ide_outb(int dev, int port, unsigned char val)
Simon Glassbc65bff2023-04-25 10:54:32 -060068{
69 debug("ide_outb (dev= %d, port= 0x%x, val= 0x%02x) : @ 0x%08lx\n",
70 dev, port, val, ATA_CURR_BASE(dev) + port);
71
72 outb(val, ATA_CURR_BASE(dev) + port);
73}
74
Simon Glassf8e87e72023-04-25 10:54:33 -060075static unsigned char ide_inb(int dev, int port)
Simon Glassbc65bff2023-04-25 10:54:32 -060076{
77 uchar val;
78
79 val = inb(ATA_CURR_BASE(dev) + port);
80
81 debug("ide_inb (dev= %d, port= 0x%x) : @ 0x%08lx -> 0x%02x\n",
82 dev, port, ATA_CURR_BASE(dev) + port, val);
83 return val;
84}
85
Simon Glassf8e87e72023-04-25 10:54:33 -060086static void ide_input_swap_data(int dev, ulong *sect_buf, int words)
Simon Glassbc65bff2023-04-25 10:54:32 -060087{
88 uintptr_t paddr = (ATA_CURR_BASE(dev) + ATA_DATA_REG);
89 ushort *dbuf = (ushort *)sect_buf;
90
91 debug("in input swap data base for read is %p\n", (void *)paddr);
92
93 while (words--) {
94 EIEIO;
95 *dbuf++ = be16_to_cpu(inw(paddr));
96 EIEIO;
97 *dbuf++ = be16_to_cpu(inw(paddr));
98 }
99}
100
Simon Glasse9be1ee2016-05-01 11:36:10 -0600101/*
102 * Wait until Busy bit is off, or timeout (in ms)
103 * Return last status
104 */
105static uchar ide_wait(int dev, ulong t)
106{
107 ulong delay = 10 * t; /* poll every 100 us */
108 uchar c;
109
110 while ((c = ide_inb(dev, ATA_STATUS)) & ATA_STAT_BUSY) {
111 udelay(100);
112 if (delay-- == 0)
113 break;
114 }
115 return c;
116}
117
118/*
119 * copy src to dest, skipping leading and trailing blanks and null
120 * terminate the string
121 * "len" is the size of available memory including the terminating '\0'
122 */
123static void ident_cpy(unsigned char *dst, unsigned char *src,
124 unsigned int len)
125{
126 unsigned char *end, *last;
127
128 last = dst;
129 end = src + len - 1;
130
131 /* reserve space for '\0' */
132 if (len < 2)
133 goto OUT;
134
135 /* skip leading white space */
136 while ((*src) && (src < end) && (*src == ' '))
137 ++src;
138
139 /* copy string, omitting trailing white space */
140 while ((*src) && (src < end)) {
141 *dst++ = *src;
142 if (*src++ != ' ')
143 last = dst;
144 }
145OUT:
146 *last = '\0';
147}
148
Simon Glasse9be1ee2016-05-01 11:36:10 -0600149/****************************************************************************
150 * ATAPI Support
151 */
152
Simon Glasse9be1ee2016-05-01 11:36:10 -0600153/* since ATAPI may use commands with not 4 bytes alligned length
154 * we have our own transfer functions, 2 bytes alligned */
Simon Glassf8e87e72023-04-25 10:54:33 -0600155static void ide_output_data_shorts(int dev, ushort *sect_buf, int shorts)
Simon Glasse9be1ee2016-05-01 11:36:10 -0600156{
Reinoud Zandijk0a527fd2021-02-24 17:44:42 +0100157 uintptr_t paddr = (ATA_CURR_BASE(dev) + ATA_DATA_REG);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600158 ushort *dbuf;
Simon Glasse9be1ee2016-05-01 11:36:10 -0600159
Simon Glasse9be1ee2016-05-01 11:36:10 -0600160 dbuf = (ushort *)sect_buf;
161
Reinoud Zandijk0a527fd2021-02-24 17:44:42 +0100162 debug("in output data shorts base for read is %p\n", (void *)paddr);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600163
164 while (shorts--) {
165 EIEIO;
Reinoud Zandijk0a527fd2021-02-24 17:44:42 +0100166 outw(cpu_to_le16(*dbuf++), paddr);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600167 }
168}
169
Simon Glassf8e87e72023-04-25 10:54:33 -0600170static void ide_input_data_shorts(int dev, ushort *sect_buf, int shorts)
Simon Glasse9be1ee2016-05-01 11:36:10 -0600171{
Reinoud Zandijk0a527fd2021-02-24 17:44:42 +0100172 uintptr_t paddr = (ATA_CURR_BASE(dev) + ATA_DATA_REG);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600173 ushort *dbuf;
Simon Glasse9be1ee2016-05-01 11:36:10 -0600174
Simon Glasse9be1ee2016-05-01 11:36:10 -0600175 dbuf = (ushort *)sect_buf;
176
Reinoud Zandijk0a527fd2021-02-24 17:44:42 +0100177 debug("in input data shorts base for read is %p\n", (void *)paddr);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600178
179 while (shorts--) {
180 EIEIO;
Reinoud Zandijk0a527fd2021-02-24 17:44:42 +0100181 *dbuf++ = le16_to_cpu(inw(paddr));
Simon Glasse9be1ee2016-05-01 11:36:10 -0600182 }
183}
184
Simon Glasse9be1ee2016-05-01 11:36:10 -0600185/*
186 * Wait until (Status & mask) == res, or timeout (in ms)
187 * Return last status
188 * This is used since some ATAPI CD ROMs clears their Busy Bit first
189 * and then they set their DRQ Bit
190 */
191static uchar atapi_wait_mask(int dev, ulong t, uchar mask, uchar res)
192{
193 ulong delay = 10 * t; /* poll every 100 us */
194 uchar c;
195
196 /* prevents to read the status before valid */
197 c = ide_inb(dev, ATA_DEV_CTL);
198
199 while (((c = ide_inb(dev, ATA_STATUS)) & mask) != res) {
200 /* break if error occurs (doesn't make sense to wait more) */
201 if ((c & ATA_STAT_ERR) == ATA_STAT_ERR)
202 break;
203 udelay(100);
204 if (delay-- == 0)
205 break;
206 }
207 return c;
208}
209
210/*
211 * issue an atapi command
212 */
Simon Glass1b33fd82023-04-25 10:54:36 -0600213static unsigned char atapi_issue(int device, unsigned char *ccb, int ccblen,
214 unsigned char *buffer, int buflen)
Simon Glasse9be1ee2016-05-01 11:36:10 -0600215{
216 unsigned char c, err, mask, res;
217 int n;
218
Simon Glasse9be1ee2016-05-01 11:36:10 -0600219 /* Select device
220 */
221 mask = ATA_STAT_BUSY | ATA_STAT_DRQ;
222 res = 0;
223 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
224 c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
225 if ((c & mask) != res) {
226 printf("ATAPI_ISSUE: device %d not ready status %X\n", device,
227 c);
228 err = 0xFF;
229 goto AI_OUT;
230 }
231 /* write taskfile */
232 ide_outb(device, ATA_ERROR_REG, 0); /* no DMA, no overlaped */
233 ide_outb(device, ATA_SECT_CNT, 0);
234 ide_outb(device, ATA_SECT_NUM, 0);
235 ide_outb(device, ATA_CYL_LOW, (unsigned char) (buflen & 0xFF));
236 ide_outb(device, ATA_CYL_HIGH,
237 (unsigned char) ((buflen >> 8) & 0xFF));
238 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
239
Heinrich Schuchardte6e9a4f2020-02-27 18:28:00 +0100240 ide_outb(device, ATA_COMMAND, ATA_CMD_PACKET);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600241 udelay(50);
242
243 mask = ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR;
244 res = ATA_STAT_DRQ;
245 c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
246
247 if ((c & mask) != res) { /* DRQ must be 1, BSY 0 */
248 printf("ATAPI_ISSUE: Error (no IRQ) before sending ccb dev %d status 0x%02x\n",
249 device, c);
250 err = 0xFF;
251 goto AI_OUT;
252 }
253
254 /* write command block */
255 ide_output_data_shorts(device, (unsigned short *)ccb, ccblen / 2);
256
257 /* ATAPI Command written wait for completition */
Simon Glass4d89f4b2023-04-25 10:54:27 -0600258 mdelay(5); /* device must set bsy */
Simon Glasse9be1ee2016-05-01 11:36:10 -0600259
260 mask = ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR;
261 /*
262 * if no data wait for DRQ = 0 BSY = 0
263 * if data wait for DRQ = 1 BSY = 0
264 */
265 res = 0;
266 if (buflen)
267 res = ATA_STAT_DRQ;
268 c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
269 if ((c & mask) != res) {
270 if (c & ATA_STAT_ERR) {
271 err = (ide_inb(device, ATA_ERROR_REG)) >> 4;
272 debug("atapi_issue 1 returned sense key %X status %02X\n",
273 err, c);
274 } else {
275 printf("ATAPI_ISSUE: (no DRQ) after sending ccb (%x) status 0x%02x\n",
276 ccb[0], c);
277 err = 0xFF;
278 }
279 goto AI_OUT;
280 }
281 n = ide_inb(device, ATA_CYL_HIGH);
282 n <<= 8;
283 n += ide_inb(device, ATA_CYL_LOW);
284 if (n > buflen) {
285 printf("ERROR, transfer bytes %d requested only %d\n", n,
286 buflen);
287 err = 0xff;
288 goto AI_OUT;
289 }
290 if ((n == 0) && (buflen < 0)) {
291 printf("ERROR, transfer bytes %d requested %d\n", n, buflen);
292 err = 0xff;
293 goto AI_OUT;
294 }
295 if (n != buflen) {
296 debug("WARNING, transfer bytes %d not equal with requested %d\n",
297 n, buflen);
298 }
299 if (n != 0) { /* data transfer */
300 debug("ATAPI_ISSUE: %d Bytes to transfer\n", n);
301 /* we transfer shorts */
302 n >>= 1;
303 /* ok now decide if it is an in or output */
304 if ((ide_inb(device, ATA_SECT_CNT) & 0x02) == 0) {
305 debug("Write to device\n");
306 ide_output_data_shorts(device, (unsigned short *)buffer,
307 n);
308 } else {
309 debug("Read from device @ %p shorts %d\n", buffer, n);
310 ide_input_data_shorts(device, (unsigned short *)buffer,
311 n);
312 }
313 }
Simon Glass4d89f4b2023-04-25 10:54:27 -0600314 mdelay(5); /* seems that some CD ROMs need this... */
Simon Glasse9be1ee2016-05-01 11:36:10 -0600315 mask = ATA_STAT_BUSY | ATA_STAT_ERR;
316 res = 0;
317 c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
318 if ((c & ATA_STAT_ERR) == ATA_STAT_ERR) {
319 err = (ide_inb(device, ATA_ERROR_REG) >> 4);
320 debug("atapi_issue 2 returned sense key %X status %X\n", err,
321 c);
322 } else {
323 err = 0;
324 }
325AI_OUT:
Simon Glasse9be1ee2016-05-01 11:36:10 -0600326 return err;
327}
328
329/*
330 * sending the command to atapi_issue. If an status other than good
331 * returns, an request_sense will be issued
332 */
333
334#define ATAPI_DRIVE_NOT_READY 100
335#define ATAPI_UNIT_ATTN 10
336
Simon Glass1b33fd82023-04-25 10:54:36 -0600337static unsigned char atapi_issue_autoreq(int device, unsigned char *ccb,
338 int ccblen,
339 unsigned char *buffer, int buflen)
Simon Glasse9be1ee2016-05-01 11:36:10 -0600340{
341 unsigned char sense_data[18], sense_ccb[12];
342 unsigned char res, key, asc, ascq;
343 int notready, unitattn;
344
345 unitattn = ATAPI_UNIT_ATTN;
346 notready = ATAPI_DRIVE_NOT_READY;
347
348retry:
349 res = atapi_issue(device, ccb, ccblen, buffer, buflen);
350 if (res == 0)
351 return 0; /* Ok */
352
353 if (res == 0xFF)
354 return 0xFF; /* error */
355
356 debug("(auto_req)atapi_issue returned sense key %X\n", res);
357
358 memset(sense_ccb, 0, sizeof(sense_ccb));
359 memset(sense_data, 0, sizeof(sense_data));
360 sense_ccb[0] = ATAPI_CMD_REQ_SENSE;
361 sense_ccb[4] = 18; /* allocation Length */
362
363 res = atapi_issue(device, sense_ccb, 12, sense_data, 18);
364 key = (sense_data[2] & 0xF);
365 asc = (sense_data[12]);
366 ascq = (sense_data[13]);
367
368 debug("ATAPI_CMD_REQ_SENSE returned %x\n", res);
369 debug(" Sense page: %02X key %02X ASC %02X ASCQ %02X\n",
370 sense_data[0], key, asc, ascq);
371
372 if ((key == 0))
373 return 0; /* ok device ready */
374
375 if ((key == 6) || (asc == 0x29) || (asc == 0x28)) { /* Unit Attention */
376 if (unitattn-- > 0) {
Simon Glass4d89f4b2023-04-25 10:54:27 -0600377 mdelay(200);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600378 goto retry;
379 }
380 printf("Unit Attention, tried %d\n", ATAPI_UNIT_ATTN);
381 goto error;
382 }
383 if ((asc == 0x4) && (ascq == 0x1)) {
384 /* not ready, but will be ready soon */
385 if (notready-- > 0) {
Simon Glass4d89f4b2023-04-25 10:54:27 -0600386 mdelay(200);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600387 goto retry;
388 }
389 printf("Drive not ready, tried %d times\n",
390 ATAPI_DRIVE_NOT_READY);
391 goto error;
392 }
393 if (asc == 0x3a) {
394 debug("Media not present\n");
395 goto error;
396 }
397
398 printf("ERROR: Unknown Sense key %02X ASC %02X ASCQ %02X\n", key, asc,
399 ascq);
400error:
401 debug("ERROR Sense key %02X ASC %02X ASCQ %02X\n", key, asc, ascq);
402 return 0xFF;
403}
404
405/*
406 * atapi_read:
407 * we transfer only one block per command, since the multiple DRQ per
408 * command is not yet implemented
409 */
410#define ATAPI_READ_MAX_BYTES 2048 /* we read max 2kbytes */
411#define ATAPI_READ_BLOCK_SIZE 2048 /* assuming CD part */
412#define ATAPI_READ_MAX_BLOCK (ATAPI_READ_MAX_BYTES/ATAPI_READ_BLOCK_SIZE)
413
Simon Glass1b33fd82023-04-25 10:54:36 -0600414static ulong atapi_read(struct udevice *dev, lbaint_t blknr, lbaint_t blkcnt,
415 void *buffer)
Simon Glasse9be1ee2016-05-01 11:36:10 -0600416{
Simon Glassce99e292023-04-25 10:54:47 -0600417 struct blk_desc *desc = dev_get_uclass_plat(dev);
418 int device = desc->devnum;
Simon Glasse9be1ee2016-05-01 11:36:10 -0600419 ulong n = 0;
420 unsigned char ccb[12]; /* Command descriptor block */
421 ulong cnt;
422
423 debug("atapi_read dev %d start " LBAF " blocks " LBAF
424 " buffer at %lX\n", device, blknr, blkcnt, (ulong) buffer);
425
426 do {
427 if (blkcnt > ATAPI_READ_MAX_BLOCK)
428 cnt = ATAPI_READ_MAX_BLOCK;
429 else
430 cnt = blkcnt;
431
432 ccb[0] = ATAPI_CMD_READ_12;
433 ccb[1] = 0; /* reserved */
434 ccb[2] = (unsigned char) (blknr >> 24) & 0xFF; /* MSB Block */
435 ccb[3] = (unsigned char) (blknr >> 16) & 0xFF; /* */
436 ccb[4] = (unsigned char) (blknr >> 8) & 0xFF;
437 ccb[5] = (unsigned char) blknr & 0xFF; /* LSB Block */
438 ccb[6] = (unsigned char) (cnt >> 24) & 0xFF; /* MSB Block cnt */
439 ccb[7] = (unsigned char) (cnt >> 16) & 0xFF;
440 ccb[8] = (unsigned char) (cnt >> 8) & 0xFF;
441 ccb[9] = (unsigned char) cnt & 0xFF; /* LSB Block */
442 ccb[10] = 0; /* reserved */
443 ccb[11] = 0; /* reserved */
444
445 if (atapi_issue_autoreq(device, ccb, 12,
446 (unsigned char *)buffer,
447 cnt * ATAPI_READ_BLOCK_SIZE)
448 == 0xFF) {
449 return n;
450 }
451 n += cnt;
452 blkcnt -= cnt;
453 blknr += cnt;
454 buffer += (cnt * ATAPI_READ_BLOCK_SIZE);
455 } while (blkcnt > 0);
456 return n;
457}
458
Simon Glassce99e292023-04-25 10:54:47 -0600459static void atapi_inquiry(struct blk_desc *desc)
Simon Glasse9be1ee2016-05-01 11:36:10 -0600460{
461 unsigned char ccb[12]; /* Command descriptor block */
462 unsigned char iobuf[64]; /* temp buf */
463 unsigned char c;
464 int device;
465
Simon Glassce99e292023-04-25 10:54:47 -0600466 device = desc->devnum;
467 desc->type = DEV_TYPE_UNKNOWN; /* not yet valid */
Simon Glasse9be1ee2016-05-01 11:36:10 -0600468
469 memset(ccb, 0, sizeof(ccb));
470 memset(iobuf, 0, sizeof(iobuf));
471
472 ccb[0] = ATAPI_CMD_INQUIRY;
473 ccb[4] = 40; /* allocation Legnth */
474 c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *)iobuf, 40);
475
476 debug("ATAPI_CMD_INQUIRY returned %x\n", c);
477 if (c != 0)
478 return;
479
480 /* copy device ident strings */
Simon Glassce99e292023-04-25 10:54:47 -0600481 ident_cpy((u8 *)desc->vendor, &iobuf[8], 8);
482 ident_cpy((u8 *)desc->product, &iobuf[16], 16);
483 ident_cpy((u8 *)desc->revision, &iobuf[32], 5);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600484
Simon Glassce99e292023-04-25 10:54:47 -0600485 desc->lun = 0;
486 desc->lba = 0;
487 desc->blksz = 0;
488 desc->log2blksz = LOG2_INVALID(typeof(desc->log2blksz));
489 desc->type = iobuf[0] & 0x1f;
Simon Glasse9be1ee2016-05-01 11:36:10 -0600490
491 if ((iobuf[1] & 0x80) == 0x80)
Simon Glassce99e292023-04-25 10:54:47 -0600492 desc->removable = 1;
Simon Glasse9be1ee2016-05-01 11:36:10 -0600493 else
Simon Glassce99e292023-04-25 10:54:47 -0600494 desc->removable = 0;
Simon Glasse9be1ee2016-05-01 11:36:10 -0600495
496 memset(ccb, 0, sizeof(ccb));
497 memset(iobuf, 0, sizeof(iobuf));
498 ccb[0] = ATAPI_CMD_START_STOP;
499 ccb[4] = 0x03; /* start */
500
501 c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *)iobuf, 0);
502
503 debug("ATAPI_CMD_START_STOP returned %x\n", c);
504 if (c != 0)
505 return;
506
507 memset(ccb, 0, sizeof(ccb));
508 memset(iobuf, 0, sizeof(iobuf));
509 c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *)iobuf, 0);
510
511 debug("ATAPI_CMD_UNIT_TEST_READY returned %x\n", c);
512 if (c != 0)
513 return;
514
515 memset(ccb, 0, sizeof(ccb));
516 memset(iobuf, 0, sizeof(iobuf));
517 ccb[0] = ATAPI_CMD_READ_CAP;
518 c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *)iobuf, 8);
519 debug("ATAPI_CMD_READ_CAP returned %x\n", c);
520 if (c != 0)
521 return;
522
523 debug("Read Cap: LBA %02X%02X%02X%02X blksize %02X%02X%02X%02X\n",
524 iobuf[0], iobuf[1], iobuf[2], iobuf[3],
525 iobuf[4], iobuf[5], iobuf[6], iobuf[7]);
526
Simon Glassce99e292023-04-25 10:54:47 -0600527 desc->lba = (ulong)iobuf[0] << 24 | (ulong)iobuf[1] << 16 |
528 (ulong)iobuf[2] << 8 | (ulong)iobuf[3];
529 desc->blksz = (ulong)iobuf[4] << 24 | (ulong)iobuf[5] << 16 |
530 (ulong)iobuf[6] << 8 | (ulong)iobuf[7];
531 desc->log2blksz = LOG2(desc->blksz);
Simon Glass8b1b9432023-04-25 10:54:41 -0600532
Simon Glasse9be1ee2016-05-01 11:36:10 -0600533 /* ATAPI devices cannot use 48bit addressing (ATA/ATAPI v7) */
Simon Glassce99e292023-04-25 10:54:47 -0600534 desc->lba48 = false;
Simon Glasse9be1ee2016-05-01 11:36:10 -0600535}
536
Simon Glass038590a2023-04-25 10:54:48 -0600537/**
538 * ide_ident() - Identify an IDE device
539 *
540 * @device: Device number to use
541 * @desc: Block descriptor to fill in
542 * Returns: 0 if OK, -ENOENT if no device is found
543 */
544static int ide_ident(int device, struct blk_desc *desc)
Simon Glasse9be1ee2016-05-01 11:36:10 -0600545{
546 unsigned char c;
547 hd_driveid_t iop;
Simon Glass606e0542022-08-11 19:34:53 -0600548 bool is_atapi = false;
Simon Glass2a165952023-04-25 10:54:37 -0600549 int tries = 1;
Simon Glasse9be1ee2016-05-01 11:36:10 -0600550
Simon Glass038590a2023-04-25 10:54:48 -0600551 desc->devnum = device;
Simon Glasse9be1ee2016-05-01 11:36:10 -0600552 printf(" Device %d: ", device);
553
Simon Glasse9be1ee2016-05-01 11:36:10 -0600554 /* Select device
555 */
556 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
Simon Glassce99e292023-04-25 10:54:47 -0600557 desc->uclass_id = UCLASS_IDE;
Simon Glass6579bb02023-04-25 10:54:38 -0600558 if (IS_ENABLED(CONFIG_ATAPI))
559 tries = 2;
Simon Glasse9be1ee2016-05-01 11:36:10 -0600560
Simon Glass2a165952023-04-25 10:54:37 -0600561 while (tries) {
Simon Glasse9be1ee2016-05-01 11:36:10 -0600562 /* check signature */
Simon Glass6579bb02023-04-25 10:54:38 -0600563 if (IS_ENABLED(CONFIG_ATAPI) &&
564 ide_inb(device, ATA_SECT_CNT) == 0x01 &&
565 ide_inb(device, ATA_SECT_NUM) == 0x01 &&
566 ide_inb(device, ATA_CYL_LOW) == 0x14 &&
567 ide_inb(device, ATA_CYL_HIGH) == 0xeb) {
Simon Glasse9be1ee2016-05-01 11:36:10 -0600568 /* ATAPI Signature found */
Simon Glass606e0542022-08-11 19:34:53 -0600569 is_atapi = true;
Simon Glasse9be1ee2016-05-01 11:36:10 -0600570 /*
571 * Start Ident Command
572 */
Heinrich Schuchardte6e9a4f2020-02-27 18:28:00 +0100573 ide_outb(device, ATA_COMMAND, ATA_CMD_ID_ATAPI);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600574 /*
575 * Wait for completion - ATAPI devices need more time
576 * to become ready
577 */
578 c = ide_wait(device, ATAPI_TIME_OUT);
Simon Glass6579bb02023-04-25 10:54:38 -0600579 } else {
Simon Glasse9be1ee2016-05-01 11:36:10 -0600580 /*
581 * Start Ident Command
582 */
Heinrich Schuchardte6e9a4f2020-02-27 18:28:00 +0100583 ide_outb(device, ATA_COMMAND, ATA_CMD_ID_ATA);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600584
585 /*
586 * Wait for completion
587 */
588 c = ide_wait(device, IDE_TIME_OUT);
589 }
Simon Glasse9be1ee2016-05-01 11:36:10 -0600590
Simon Glass9367e612023-04-25 10:54:39 -0600591 if ((c & ATA_STAT_DRQ) &&
592 !(c & (ATA_STAT_FAULT | ATA_STAT_ERR))) {
Simon Glasse9be1ee2016-05-01 11:36:10 -0600593 break;
Simon Glass9367e612023-04-25 10:54:39 -0600594 } else if (IS_ENABLED(CONFIG_ATAPI)) {
595 /*
596 * Need to soft reset the device
597 * in case it's an ATAPI...
598 */
599 debug("Retrying...\n");
600 ide_outb(device, ATA_DEV_HD,
601 ATA_LBA | ATA_DEVICE(device));
602 mdelay(100);
603 ide_outb(device, ATA_COMMAND, 0x08);
604 mdelay(500);
605 /* Select device */
606 ide_outb(device, ATA_DEV_HD,
607 ATA_LBA | ATA_DEVICE(device));
Simon Glass6579bb02023-04-25 10:54:38 -0600608 }
Simon Glass9367e612023-04-25 10:54:39 -0600609 tries--;
Simon Glass6579bb02023-04-25 10:54:38 -0600610 }
Simon Glasse9be1ee2016-05-01 11:36:10 -0600611
Simon Glass2a165952023-04-25 10:54:37 -0600612 if (!tries) /* Not found */
Simon Glass038590a2023-04-25 10:54:48 -0600613 return -ENOENT;
Simon Glasse9be1ee2016-05-01 11:36:10 -0600614
615 ide_input_swap_data(device, (ulong *)&iop, ATA_SECTORWORDS);
616
Simon Glassce99e292023-04-25 10:54:47 -0600617 ident_cpy((u8 *)desc->revision, iop.fw_rev, sizeof(desc->revision));
618 ident_cpy((u8 *)desc->vendor, iop.model, sizeof(desc->vendor));
619 ident_cpy((u8 *)desc->product, iop.serial_no, sizeof(desc->product));
Simon Glasse9be1ee2016-05-01 11:36:10 -0600620
621 if ((iop.config & 0x0080) == 0x0080)
Simon Glassce99e292023-04-25 10:54:47 -0600622 desc->removable = 1;
Simon Glasse9be1ee2016-05-01 11:36:10 -0600623 else
Simon Glassce99e292023-04-25 10:54:47 -0600624 desc->removable = 0;
Simon Glasse9be1ee2016-05-01 11:36:10 -0600625
Simon Glassc94a3312023-04-25 10:54:40 -0600626 if (IS_ENABLED(CONFIG_ATAPI) && is_atapi) {
Simon Glassce99e292023-04-25 10:54:47 -0600627 desc->atapi = true;
628 atapi_inquiry(desc);
Simon Glass038590a2023-04-25 10:54:48 -0600629 return 0;
Simon Glasse9be1ee2016-05-01 11:36:10 -0600630 }
Simon Glasse9be1ee2016-05-01 11:36:10 -0600631
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]);
Simon Glassce99e292023-04-25 10:54:47 -0600634 desc->lba = (ulong)iop.lba_capacity[0] |
635 (ulong)iop.lba_capacity[1] << 16;
Simon Glasse9be1ee2016-05-01 11:36:10 -0600636
Simon Glass8b1b9432023-04-25 10:54:41 -0600637 if (IS_ENABLED(CONFIG_LBA48) && (iop.command_set_2 & 0x0400)) {
638 /* LBA 48 support */
Simon Glassce99e292023-04-25 10:54:47 -0600639 desc->lba48 = true;
Reinoud Zandijk0a527fd2021-02-24 17:44:42 +0100640 for (int i = 0; i < 4; i++)
641 iop.lba48_capacity[i] = be16_to_cpu(iop.lba48_capacity[i]);
Simon Glassce99e292023-04-25 10:54:47 -0600642 desc->lba = (unsigned long long)iop.lba48_capacity[0] |
643 (unsigned long long)iop.lba48_capacity[1] << 16 |
644 (unsigned long long)iop.lba48_capacity[2] << 32 |
645 (unsigned long long)iop.lba48_capacity[3] << 48;
Simon Glasse9be1ee2016-05-01 11:36:10 -0600646 } else {
Simon Glassce99e292023-04-25 10:54:47 -0600647 desc->lba48 = false;
Simon Glasse9be1ee2016-05-01 11:36:10 -0600648 }
Simon Glass8b1b9432023-04-25 10:54:41 -0600649
Simon Glasse9be1ee2016-05-01 11:36:10 -0600650 /* assuming HD */
Simon Glassce99e292023-04-25 10:54:47 -0600651 desc->type = DEV_TYPE_HARDDISK;
652 desc->blksz = ATA_BLOCKSIZE;
653 desc->log2blksz = LOG2(desc->blksz);
654 desc->lun = 0; /* just to fill something in... */
Simon Glasse9be1ee2016-05-01 11:36:10 -0600655
656#if 0 /* only used to test the powersaving mode,
657 * if enabled, the drive goes after 5 sec
658 * in standby mode */
659 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
660 c = ide_wait(device, IDE_TIME_OUT);
661 ide_outb(device, ATA_SECT_CNT, 1);
662 ide_outb(device, ATA_LBA_LOW, 0);
663 ide_outb(device, ATA_LBA_MID, 0);
664 ide_outb(device, ATA_LBA_HIGH, 0);
665 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
666 ide_outb(device, ATA_COMMAND, 0xe3);
667 udelay(50);
668 c = ide_wait(device, IDE_TIME_OUT); /* can't take over 500 ms */
669#endif
Simon Glass038590a2023-04-25 10:54:48 -0600670
671 return 0;
Simon Glasse9be1ee2016-05-01 11:36:10 -0600672}
673
Simon Glassb6483ea2023-04-25 10:54:42 -0600674/**
675 * ide_init_one() - Init one IDE device
676 *
677 * @bus: Bus to use
678 * Return: 0 iuf OK, -EIO if not available, -ETIMEDOUT if timed out
679 */
680static int ide_init_one(int bus)
681{
682 int dev = bus * CONFIG_SYS_IDE_MAXDEVICE / CONFIG_SYS_IDE_MAXBUS;
683 int i;
684 u8 c;
685
686 printf("Bus %d: ", bus);
687
688 /* Select device */
689 mdelay(100);
690 ide_outb(dev, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(dev));
691 mdelay(100);
692 i = 0;
693 do {
694 mdelay(10);
695
696 c = ide_inb(dev, ATA_STATUS);
697 i++;
698 if (i > (ATA_RESET_TIME * 100)) {
699 puts("** Timeout **\n");
700 return -ETIMEDOUT;
701 }
702 if (i >= 100 && !(i % 100))
703 putc('.');
704 } while (c & ATA_STAT_BUSY);
705
706 if (c & (ATA_STAT_BUSY | ATA_STAT_FAULT)) {
707 puts("not available ");
708 debug("Status = 0x%02X ", c);
709 return -EIO;
710 } else if (IS_ENABLED(CONFIG_ATAPI) && !(c & ATA_STAT_READY)) {
711 /* ATAPI Devices do not set DRDY */
712 puts("not available ");
713 debug("Status = 0x%02X ", c);
714 return -EIO;
715 }
716 puts("OK ");
717
718 return 0;
719}
720
Simon Glassf8e87e72023-04-25 10:54:33 -0600721static void ide_output_data(int dev, const ulong *sect_buf, int words)
Simon Glasse9be1ee2016-05-01 11:36:10 -0600722{
Reinoud Zandijk0a527fd2021-02-24 17:44:42 +0100723 uintptr_t paddr = (ATA_CURR_BASE(dev) + ATA_DATA_REG);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600724 ushort *dbuf;
Simon Glasse9be1ee2016-05-01 11:36:10 -0600725
Simon Glasse9be1ee2016-05-01 11:36:10 -0600726 dbuf = (ushort *)sect_buf;
Simon Glasse9be1ee2016-05-01 11:36:10 -0600727 while (words--) {
728 EIEIO;
Reinoud Zandijk0a527fd2021-02-24 17:44:42 +0100729 outw(cpu_to_le16(*dbuf++), paddr);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600730 EIEIO;
Reinoud Zandijk0a527fd2021-02-24 17:44:42 +0100731 outw(cpu_to_le16(*dbuf++), paddr);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600732 }
733}
Reinoud Zandijk0a527fd2021-02-24 17:44:42 +0100734
Simon Glassf8e87e72023-04-25 10:54:33 -0600735static void ide_input_data(int dev, ulong *sect_buf, int words)
Simon Glasse9be1ee2016-05-01 11:36:10 -0600736{
Reinoud Zandijk0a527fd2021-02-24 17:44:42 +0100737 uintptr_t paddr = (ATA_CURR_BASE(dev) + ATA_DATA_REG);
738 ushort *dbuf;
Simon Glasse9be1ee2016-05-01 11:36:10 -0600739
Reinoud Zandijk0a527fd2021-02-24 17:44:42 +0100740 dbuf = (ushort *)sect_buf;
741
742 debug("in input data base for read is %p\n", (void *)paddr);
743
744 while (words--) {
745 EIEIO;
746 *dbuf++ = le16_to_cpu(inw(paddr));
747 EIEIO;
748 *dbuf++ = le16_to_cpu(inw(paddr));
749 }
Reinoud Zandijk0a527fd2021-02-24 17:44:42 +0100750}
Simon Glasse9be1ee2016-05-01 11:36:10 -0600751
Simon Glass1b33fd82023-04-25 10:54:36 -0600752static ulong ide_read(struct udevice *dev, lbaint_t blknr, lbaint_t blkcnt,
753 void *buffer)
Simon Glasse9be1ee2016-05-01 11:36:10 -0600754{
Simon Glassce99e292023-04-25 10:54:47 -0600755 struct blk_desc *desc = dev_get_uclass_plat(dev);
756 int device = desc->devnum;
Simon Glasse9be1ee2016-05-01 11:36:10 -0600757 ulong n = 0;
758 unsigned char c;
759 unsigned char pwrsave = 0; /* power save */
Simon Glass8b1b9432023-04-25 10:54:41 -0600760 bool lba48 = false;
Simon Glasse9be1ee2016-05-01 11:36:10 -0600761
Simon Glass8b1b9432023-04-25 10:54:41 -0600762 if (IS_ENABLED(CONFIG_LBA48) && (blknr & 0x0000fffff0000000ULL)) {
Simon Glasse9be1ee2016-05-01 11:36:10 -0600763 /* more than 28 bits used, use 48bit mode */
Simon Glass8b1b9432023-04-25 10:54:41 -0600764 lba48 = true;
Simon Glasse9be1ee2016-05-01 11:36:10 -0600765 }
Simon Glass8b1b9432023-04-25 10:54:41 -0600766
Simon Glasse9be1ee2016-05-01 11:36:10 -0600767 debug("ide_read dev %d start " LBAF ", blocks " LBAF " buffer at %lX\n",
768 device, blknr, blkcnt, (ulong) buffer);
769
Simon Glasse9be1ee2016-05-01 11:36:10 -0600770 /* Select device
771 */
772 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
773 c = ide_wait(device, IDE_TIME_OUT);
774
775 if (c & ATA_STAT_BUSY) {
776 printf("IDE read: device %d not ready\n", device);
777 goto IDE_READ_E;
778 }
779
780 /* first check if the drive is in Powersaving mode, if yes,
781 * increase the timeout value */
Heinrich Schuchardte6e9a4f2020-02-27 18:28:00 +0100782 ide_outb(device, ATA_COMMAND, ATA_CMD_CHK_POWER);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600783 udelay(50);
784
785 c = ide_wait(device, IDE_TIME_OUT); /* can't take over 500 ms */
786
787 if (c & ATA_STAT_BUSY) {
788 printf("IDE read: device %d not ready\n", device);
789 goto IDE_READ_E;
790 }
791 if ((c & ATA_STAT_ERR) == ATA_STAT_ERR) {
792 printf("No Powersaving mode %X\n", c);
793 } else {
794 c = ide_inb(device, ATA_SECT_CNT);
795 debug("Powersaving %02X\n", c);
796 if (c == 0)
797 pwrsave = 1;
798 }
799
800
801 while (blkcnt-- > 0) {
802 c = ide_wait(device, IDE_TIME_OUT);
803
804 if (c & ATA_STAT_BUSY) {
805 printf("IDE read: device %d not ready\n", device);
806 break;
807 }
Simon Glass8b1b9432023-04-25 10:54:41 -0600808 if (IS_ENABLED(CONFIG_LBA48) && lba48) {
Simon Glasse9be1ee2016-05-01 11:36:10 -0600809 /* write high bits */
810 ide_outb(device, ATA_SECT_CNT, 0);
811 ide_outb(device, ATA_LBA_LOW, (blknr >> 24) & 0xFF);
812#ifdef CONFIG_SYS_64BIT_LBA
813 ide_outb(device, ATA_LBA_MID, (blknr >> 32) & 0xFF);
814 ide_outb(device, ATA_LBA_HIGH, (blknr >> 40) & 0xFF);
815#else
816 ide_outb(device, ATA_LBA_MID, 0);
817 ide_outb(device, ATA_LBA_HIGH, 0);
818#endif
819 }
Simon Glasse9be1ee2016-05-01 11:36:10 -0600820 ide_outb(device, ATA_SECT_CNT, 1);
821 ide_outb(device, ATA_LBA_LOW, (blknr >> 0) & 0xFF);
822 ide_outb(device, ATA_LBA_MID, (blknr >> 8) & 0xFF);
823 ide_outb(device, ATA_LBA_HIGH, (blknr >> 16) & 0xFF);
824
Simon Glass8b1b9432023-04-25 10:54:41 -0600825 if (IS_ENABLED(CONFIG_LBA48) && lba48) {
Simon Glasse9be1ee2016-05-01 11:36:10 -0600826 ide_outb(device, ATA_DEV_HD,
827 ATA_LBA | ATA_DEVICE(device));
Reinoud Zandijk0a527fd2021-02-24 17:44:42 +0100828 ide_outb(device, ATA_COMMAND, ATA_CMD_PIO_READ_EXT);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600829
Simon Glass8b1b9432023-04-25 10:54:41 -0600830 } else {
Simon Glasse9be1ee2016-05-01 11:36:10 -0600831 ide_outb(device, ATA_DEV_HD, ATA_LBA |
832 ATA_DEVICE(device) | ((blknr >> 24) & 0xF));
Reinoud Zandijk0a527fd2021-02-24 17:44:42 +0100833 ide_outb(device, ATA_COMMAND, ATA_CMD_PIO_READ);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600834 }
835
836 udelay(50);
837
838 if (pwrsave) {
839 /* may take up to 4 sec */
840 c = ide_wait(device, IDE_SPIN_UP_TIME_OUT);
841 pwrsave = 0;
842 } else {
843 /* can't take over 500 ms */
844 c = ide_wait(device, IDE_TIME_OUT);
845 }
846
847 if ((c & (ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR)) !=
848 ATA_STAT_DRQ) {
849 printf("Error (no IRQ) dev %d blk " LBAF
850 ": status %#02x\n", device, blknr, c);
851 break;
852 }
853
854 ide_input_data(device, buffer, ATA_SECTORWORDS);
855 (void) ide_inb(device, ATA_STATUS); /* clear IRQ */
856
857 ++n;
858 ++blknr;
859 buffer += ATA_BLOCKSIZE;
860 }
861IDE_READ_E:
Simon Glasse9be1ee2016-05-01 11:36:10 -0600862 return n;
863}
864
Simon Glass1b33fd82023-04-25 10:54:36 -0600865static ulong ide_write(struct udevice *dev, lbaint_t blknr, lbaint_t blkcnt,
866 const void *buffer)
Simon Glasse9be1ee2016-05-01 11:36:10 -0600867{
Simon Glassce99e292023-04-25 10:54:47 -0600868 struct blk_desc *desc = dev_get_uclass_plat(dev);
869 int device = desc->devnum;
Simon Glasse9be1ee2016-05-01 11:36:10 -0600870 ulong n = 0;
871 unsigned char c;
Simon Glass8b1b9432023-04-25 10:54:41 -0600872 bool lba48 = false;
Simon Glasse9be1ee2016-05-01 11:36:10 -0600873
Simon Glass8b1b9432023-04-25 10:54:41 -0600874 if (IS_ENABLED(CONFIG_LBA48) && (blknr & 0x0000fffff0000000ULL)) {
Simon Glasse9be1ee2016-05-01 11:36:10 -0600875 /* more than 28 bits used, use 48bit mode */
Simon Glass8b1b9432023-04-25 10:54:41 -0600876 lba48 = true;
Simon Glasse9be1ee2016-05-01 11:36:10 -0600877 }
Simon Glasse9be1ee2016-05-01 11:36:10 -0600878
Simon Glasse9be1ee2016-05-01 11:36:10 -0600879 /* Select device
880 */
881 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
882
883 while (blkcnt-- > 0) {
884 c = ide_wait(device, IDE_TIME_OUT);
885
886 if (c & ATA_STAT_BUSY) {
887 printf("IDE read: device %d not ready\n", device);
888 goto WR_OUT;
889 }
Simon Glass8b1b9432023-04-25 10:54:41 -0600890 if (IS_ENABLED(CONFIG_LBA48) && lba48) {
Simon Glasse9be1ee2016-05-01 11:36:10 -0600891 /* write high bits */
892 ide_outb(device, ATA_SECT_CNT, 0);
893 ide_outb(device, ATA_LBA_LOW, (blknr >> 24) & 0xFF);
894#ifdef CONFIG_SYS_64BIT_LBA
895 ide_outb(device, ATA_LBA_MID, (blknr >> 32) & 0xFF);
896 ide_outb(device, ATA_LBA_HIGH, (blknr >> 40) & 0xFF);
897#else
898 ide_outb(device, ATA_LBA_MID, 0);
899 ide_outb(device, ATA_LBA_HIGH, 0);
900#endif
901 }
Simon Glasse9be1ee2016-05-01 11:36:10 -0600902 ide_outb(device, ATA_SECT_CNT, 1);
903 ide_outb(device, ATA_LBA_LOW, (blknr >> 0) & 0xFF);
904 ide_outb(device, ATA_LBA_MID, (blknr >> 8) & 0xFF);
905 ide_outb(device, ATA_LBA_HIGH, (blknr >> 16) & 0xFF);
906
Simon Glass8b1b9432023-04-25 10:54:41 -0600907 if (IS_ENABLED(CONFIG_LBA48) && lba48) {
Simon Glasse9be1ee2016-05-01 11:36:10 -0600908 ide_outb(device, ATA_DEV_HD,
909 ATA_LBA | ATA_DEVICE(device));
Reinoud Zandijk0a527fd2021-02-24 17:44:42 +0100910 ide_outb(device, ATA_COMMAND, ATA_CMD_PIO_WRITE_EXT);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600911
Simon Glass8b1b9432023-04-25 10:54:41 -0600912 } else {
Simon Glasse9be1ee2016-05-01 11:36:10 -0600913 ide_outb(device, ATA_DEV_HD, ATA_LBA |
914 ATA_DEVICE(device) | ((blknr >> 24) & 0xF));
Reinoud Zandijk0a527fd2021-02-24 17:44:42 +0100915 ide_outb(device, ATA_COMMAND, ATA_CMD_PIO_WRITE);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600916 }
917
918 udelay(50);
919
920 /* can't take over 500 ms */
921 c = ide_wait(device, IDE_TIME_OUT);
922
923 if ((c & (ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR)) !=
924 ATA_STAT_DRQ) {
925 printf("Error (no IRQ) dev %d blk " LBAF
926 ": status %#02x\n", device, blknr, c);
927 goto WR_OUT;
928 }
929
930 ide_output_data(device, buffer, ATA_SECTORWORDS);
931 c = ide_inb(device, ATA_STATUS); /* clear IRQ */
932 ++n;
933 ++blknr;
934 buffer += ATA_BLOCKSIZE;
935 }
936WR_OUT:
Simon Glasse9be1ee2016-05-01 11:36:10 -0600937 return n;
938}
939
Simon Glass646deed2023-04-25 10:54:35 -0600940ulong ide_or_atapi_read(struct udevice *dev, lbaint_t blknr, lbaint_t blkcnt,
941 void *buffer)
942{
943 struct blk_desc *desc = dev_get_uclass_plat(dev);
944
945 if (IS_ENABLED(CONFIG_ATAPI) && desc->atapi)
946 return atapi_read(dev, blknr, blkcnt, buffer);
947
948 return ide_read(dev, blknr, blkcnt, buffer);
949}
950
Simon Glass145df842016-05-01 11:36:22 -0600951static const struct blk_ops ide_blk_ops = {
Simon Glass646deed2023-04-25 10:54:35 -0600952 .read = ide_or_atapi_read,
Simon Glass145df842016-05-01 11:36:22 -0600953 .write = ide_write,
954};
955
956U_BOOT_DRIVER(ide_blk) = {
957 .name = "ide_blk",
958 .id = UCLASS_BLK,
959 .ops = &ide_blk_ops,
Bin Meng68e6f222017-09-10 05:12:51 -0700960};
961
Simon Glass0d77f8f2023-01-17 10:47:46 -0700962static int ide_bootdev_bind(struct udevice *dev)
963{
964 struct bootdev_uc_plat *ucp = dev_get_uclass_plat(dev);
965
Simon Glasseacc2612023-01-17 10:48:08 -0700966 ucp->prio = BOOTDEVP_5_SCAN_SLOW;
Simon Glass0d77f8f2023-01-17 10:47:46 -0700967
968 return 0;
969}
970
971static int ide_bootdev_hunt(struct bootdev_hunter *info, bool show)
972{
Simon Glass80778f52023-04-25 10:54:30 -0600973 struct udevice *dev;
974
975 uclass_first_device(UCLASS_IDE, &dev);
Simon Glass0d77f8f2023-01-17 10:47:46 -0700976
977 return 0;
978}
979
980struct bootdev_ops ide_bootdev_ops = {
981};
982
983static const struct udevice_id ide_bootdev_ids[] = {
984 { .compatible = "u-boot,bootdev-ide" },
985 { }
986};
987
988U_BOOT_DRIVER(ide_bootdev) = {
989 .name = "ide_bootdev",
990 .id = UCLASS_BOOTDEV,
991 .ops = &ide_bootdev_ops,
992 .bind = ide_bootdev_bind,
993 .of_match = ide_bootdev_ids,
994};
995
996BOOTDEV_HUNTER(ide_bootdev_hunter) = {
Simon Glasseacc2612023-01-17 10:48:08 -0700997 .prio = BOOTDEVP_5_SCAN_SLOW,
Simon Glass0d77f8f2023-01-17 10:47:46 -0700998 .uclass = UCLASS_IDE,
999 .hunt = ide_bootdev_hunt,
1000 .drv = DM_DRIVER_REF(ide_bootdev),
1001};
1002
Bin Meng68e6f222017-09-10 05:12:51 -07001003static int ide_probe(struct udevice *udev)
1004{
Simon Glass708404c2023-04-25 10:54:45 -06001005 struct blk_desc ide_dev_desc[CONFIG_SYS_IDE_MAXDEVICE];
1006 bool bus_ok[CONFIG_SYS_IDE_MAXBUS];
1007 int i, bus;
Bin Meng68e6f222017-09-10 05:12:51 -07001008
Simon Glass708404c2023-04-25 10:54:45 -06001009 schedule();
1010
1011 /* ATAPI Drives seems to need a proper IDE Reset */
1012 ide_reset();
1013
1014 /*
1015 * Wait for IDE to get ready.
1016 * According to spec, this can take up to 31 seconds!
1017 */
1018 for (bus = 0; bus < CONFIG_SYS_IDE_MAXBUS; ++bus) {
1019 bus_ok[bus] = !ide_init_one(bus);
1020 schedule();
1021 }
1022
1023 putc('\n');
1024
Simon Glassf0af25a2023-04-25 10:54:46 -06001025 schedule();
1026
1027 for (i = 0; i < CONFIG_SYS_IDE_MAXDEVICE; i++) {
Simon Glass038590a2023-04-25 10:54:48 -06001028 struct blk_desc *desc;
1029 struct udevice *blk;
1030 lbaint_t size;
1031 char name[20];
1032 int blksz;
1033 int ret;
1034
Simon Glassf0af25a2023-04-25 10:54:46 -06001035 if (!bus_ok[IDE_BUS(i)])
1036 continue;
1037
Simon Glass708404c2023-04-25 10:54:45 -06001038 ide_dev_desc[i].type = DEV_TYPE_UNKNOWN;
1039 ide_dev_desc[i].uclass_id = UCLASS_IDE;
Simon Glass708404c2023-04-25 10:54:45 -06001040 ide_dev_desc[i].part_type = PART_TYPE_UNKNOWN;
1041 ide_dev_desc[i].blksz = 0;
1042 ide_dev_desc[i].log2blksz =
1043 LOG2_INVALID(typeof(ide_dev_desc[i].log2blksz));
1044 ide_dev_desc[i].lba = 0;
Simon Glass038590a2023-04-25 10:54:48 -06001045 ret = ide_ident(i, &ide_dev_desc[i]);
Simon Glass708404c2023-04-25 10:54:45 -06001046 dev_print(&ide_dev_desc[i]);
Simon Glass80778f52023-04-25 10:54:30 -06001047
Simon Glass038590a2023-04-25 10:54:48 -06001048 if (ret)
1049 continue;
Simon Glassdb89e722023-04-25 10:54:44 -06001050
Simon Glass038590a2023-04-25 10:54:48 -06001051 sprintf(name, "blk#%d", i);
Bin Meng68e6f222017-09-10 05:12:51 -07001052
Simon Glass038590a2023-04-25 10:54:48 -06001053 blksz = ide_dev_desc[i].blksz;
1054 size = blksz * ide_dev_desc[i].lba;
Bin Meng1f4adab2017-09-10 05:12:52 -07001055
Simon Glass038590a2023-04-25 10:54:48 -06001056 /*
1057 * With CDROM, if there is no CD inserted, blksz will
1058 * be zero, don't bother to create IDE block device.
1059 */
1060 if (!blksz)
1061 continue;
1062 ret = blk_create_devicef(udev, "ide_blk", name, UCLASS_IDE, i,
1063 blksz, size, &blk);
1064 if (ret)
1065 return ret;
AKASHI Takahiro4c73b032022-03-08 20:36:44 +09001066
Simon Glass038590a2023-04-25 10:54:48 -06001067 ret = blk_probe_or_unbind(blk);
1068 if (ret)
1069 return ret;
Simon Glass0d77f8f2023-01-17 10:47:46 -07001070
Simon Glass038590a2023-04-25 10:54:48 -06001071 /* fill in device vendor/product/rev strings */
1072 desc = dev_get_uclass_plat(blk);
1073 strlcpy(desc->vendor, ide_dev_desc[desc->devnum].vendor,
1074 BLK_VEN_SIZE);
1075 strlcpy(desc->product, ide_dev_desc[desc->devnum].product,
1076 BLK_PRD_SIZE);
1077 strlcpy(desc->revision, ide_dev_desc[desc->devnum].revision,
1078 BLK_REV_SIZE);
Simon Glass708404c2023-04-25 10:54:45 -06001079
Simon Glass038590a2023-04-25 10:54:48 -06001080 ret = bootdev_setup_for_dev(udev, "ide_bootdev");
1081 if (ret)
1082 return log_msg_ret("bootdev", ret);
Bin Meng68e6f222017-09-10 05:12:51 -07001083 }
1084
1085 return 0;
1086}
1087
1088U_BOOT_DRIVER(ide) = {
1089 .name = "ide",
1090 .id = UCLASS_IDE,
1091 .probe = ide_probe,
1092};
1093
1094struct pci_device_id ide_supported[] = {
1095 { PCI_DEVICE_CLASS(PCI_CLASS_STORAGE_IDE << 8, 0xffff00) },
1096 { }
1097};
1098
1099U_BOOT_PCI_DEVICE(ide, ide_supported);
1100
1101UCLASS_DRIVER(ide) = {
1102 .name = "ide",
1103 .id = UCLASS_IDE,
Simon Glass145df842016-05-01 11:36:22 -06001104};