blob: 37236f6058b4db7b3d35ea26e1d08147e98c0f14 [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 -060048static void ide_reset(void)
49{
Simon Glass9666de82023-04-25 10:54:52 -060050 if (IS_ENABLED(CONFIG_IDE_RESET)) {
51 /* assert reset */
52 ide_set_reset(1);
Simon Glasse9be1ee2016-05-01 11:36:10 -060053
Simon Glass9666de82023-04-25 10:54:52 -060054 /* the reset signal shall be asserted for et least 25 us */
55 udelay(25);
Simon Glasse9be1ee2016-05-01 11:36:10 -060056
Simon Glass9666de82023-04-25 10:54:52 -060057 schedule();
Simon Glasse9be1ee2016-05-01 11:36:10 -060058
Simon Glass9666de82023-04-25 10:54:52 -060059 /* de-assert RESET signal */
60 ide_set_reset(0);
Simon Glasse9be1ee2016-05-01 11:36:10 -060061
Simon Glass9666de82023-04-25 10:54:52 -060062 mdelay(250);
63 }
Simon Glasse9be1ee2016-05-01 11:36:10 -060064}
Simon Glasse9be1ee2016-05-01 11:36:10 -060065
Simon Glassf8e87e72023-04-25 10:54:33 -060066static void ide_outb(int dev, int port, unsigned char val)
Simon Glassbc65bff2023-04-25 10:54:32 -060067{
Simon Glass692bccb2023-04-25 10:54:53 -060068 log_debug("(dev= %d, port= %#x, val= 0x%02x) : @ 0x%08lx\n",
69 dev, port, val, ATA_CURR_BASE(dev) + port);
Simon Glassbc65bff2023-04-25 10:54:32 -060070
71 outb(val, ATA_CURR_BASE(dev) + port);
72}
73
Simon Glassf8e87e72023-04-25 10:54:33 -060074static unsigned char ide_inb(int dev, int port)
Simon Glassbc65bff2023-04-25 10:54:32 -060075{
76 uchar val;
77
78 val = inb(ATA_CURR_BASE(dev) + port);
79
Simon Glass692bccb2023-04-25 10:54:53 -060080 log_debug("(dev= %d, port= %#x) : @ 0x%08lx -> 0x%02x\n",
81 dev, port, ATA_CURR_BASE(dev) + port, val);
Simon Glassbc65bff2023-04-25 10:54:32 -060082 return val;
83}
84
Simon Glassf8e87e72023-04-25 10:54:33 -060085static void ide_input_swap_data(int dev, ulong *sect_buf, int words)
Simon Glassbc65bff2023-04-25 10:54:32 -060086{
87 uintptr_t paddr = (ATA_CURR_BASE(dev) + ATA_DATA_REG);
88 ushort *dbuf = (ushort *)sect_buf;
89
Simon Glass692bccb2023-04-25 10:54:53 -060090 log_debug("in input swap data base for read is %p\n", (void *)paddr);
Simon Glassbc65bff2023-04-25 10:54:32 -060091
92 while (words--) {
93 EIEIO;
94 *dbuf++ = be16_to_cpu(inw(paddr));
95 EIEIO;
96 *dbuf++ = be16_to_cpu(inw(paddr));
97 }
98}
99
Simon Glasse9be1ee2016-05-01 11:36:10 -0600100/*
101 * Wait until Busy bit is off, or timeout (in ms)
102 * Return last status
103 */
104static uchar ide_wait(int dev, ulong t)
105{
106 ulong delay = 10 * t; /* poll every 100 us */
107 uchar c;
108
109 while ((c = ide_inb(dev, ATA_STATUS)) & ATA_STAT_BUSY) {
110 udelay(100);
111 if (delay-- == 0)
112 break;
113 }
114 return c;
115}
116
117/*
118 * copy src to dest, skipping leading and trailing blanks and null
119 * terminate the string
120 * "len" is the size of available memory including the terminating '\0'
121 */
122static void ident_cpy(unsigned char *dst, unsigned char *src,
123 unsigned int len)
124{
125 unsigned char *end, *last;
126
127 last = dst;
128 end = src + len - 1;
129
130 /* reserve space for '\0' */
131 if (len < 2)
132 goto OUT;
133
134 /* skip leading white space */
135 while ((*src) && (src < end) && (*src == ' '))
136 ++src;
137
138 /* copy string, omitting trailing white space */
139 while ((*src) && (src < end)) {
140 *dst++ = *src;
141 if (*src++ != ' ')
142 last = dst;
143 }
144OUT:
145 *last = '\0';
146}
147
Simon Glasse9be1ee2016-05-01 11:36:10 -0600148/****************************************************************************
149 * ATAPI Support
150 */
151
Simon Glasse9be1ee2016-05-01 11:36:10 -0600152/* since ATAPI may use commands with not 4 bytes alligned length
153 * we have our own transfer functions, 2 bytes alligned */
Simon Glassf8e87e72023-04-25 10:54:33 -0600154static void ide_output_data_shorts(int dev, ushort *sect_buf, int shorts)
Simon Glasse9be1ee2016-05-01 11:36:10 -0600155{
Reinoud Zandijk0a527fd2021-02-24 17:44:42 +0100156 uintptr_t paddr = (ATA_CURR_BASE(dev) + ATA_DATA_REG);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600157 ushort *dbuf;
Simon Glasse9be1ee2016-05-01 11:36:10 -0600158
Simon Glasse9be1ee2016-05-01 11:36:10 -0600159 dbuf = (ushort *)sect_buf;
160
Simon Glass692bccb2023-04-25 10:54:53 -0600161 log_debug("in output data shorts base for read is %p\n", (void *)paddr);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600162
163 while (shorts--) {
164 EIEIO;
Reinoud Zandijk0a527fd2021-02-24 17:44:42 +0100165 outw(cpu_to_le16(*dbuf++), paddr);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600166 }
167}
168
Simon Glassf8e87e72023-04-25 10:54:33 -0600169static void ide_input_data_shorts(int dev, ushort *sect_buf, int shorts)
Simon Glasse9be1ee2016-05-01 11:36:10 -0600170{
Reinoud Zandijk0a527fd2021-02-24 17:44:42 +0100171 uintptr_t paddr = (ATA_CURR_BASE(dev) + ATA_DATA_REG);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600172 ushort *dbuf;
Simon Glasse9be1ee2016-05-01 11:36:10 -0600173
Simon Glasse9be1ee2016-05-01 11:36:10 -0600174 dbuf = (ushort *)sect_buf;
175
Simon Glass692bccb2023-04-25 10:54:53 -0600176 log_debug("in input data shorts base for read is %p\n", (void *)paddr);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600177
178 while (shorts--) {
179 EIEIO;
Reinoud Zandijk0a527fd2021-02-24 17:44:42 +0100180 *dbuf++ = le16_to_cpu(inw(paddr));
Simon Glasse9be1ee2016-05-01 11:36:10 -0600181 }
182}
183
Simon Glasse9be1ee2016-05-01 11:36:10 -0600184/*
185 * Wait until (Status & mask) == res, or timeout (in ms)
186 * Return last status
187 * This is used since some ATAPI CD ROMs clears their Busy Bit first
188 * and then they set their DRQ Bit
189 */
190static uchar atapi_wait_mask(int dev, ulong t, uchar mask, uchar res)
191{
192 ulong delay = 10 * t; /* poll every 100 us */
193 uchar c;
194
195 /* prevents to read the status before valid */
196 c = ide_inb(dev, ATA_DEV_CTL);
197
198 while (((c = ide_inb(dev, ATA_STATUS)) & mask) != res) {
199 /* break if error occurs (doesn't make sense to wait more) */
200 if ((c & ATA_STAT_ERR) == ATA_STAT_ERR)
201 break;
202 udelay(100);
203 if (delay-- == 0)
204 break;
205 }
206 return c;
207}
208
209/*
210 * issue an atapi command
211 */
Simon Glass1b33fd82023-04-25 10:54:36 -0600212static unsigned char atapi_issue(int device, unsigned char *ccb, int ccblen,
213 unsigned char *buffer, int buflen)
Simon Glasse9be1ee2016-05-01 11:36:10 -0600214{
215 unsigned char c, err, mask, res;
216 int n;
217
Simon Glasse9be1ee2016-05-01 11:36:10 -0600218 /* Select device
219 */
220 mask = ATA_STAT_BUSY | ATA_STAT_DRQ;
221 res = 0;
222 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
223 c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
224 if ((c & mask) != res) {
Simon Glass692bccb2023-04-25 10:54:53 -0600225 printf("ATAPI_ISSUE: device %d not ready status %x\n", device,
Simon Glasse9be1ee2016-05-01 11:36:10 -0600226 c);
227 err = 0xFF;
228 goto AI_OUT;
229 }
230 /* write taskfile */
231 ide_outb(device, ATA_ERROR_REG, 0); /* no DMA, no overlaped */
232 ide_outb(device, ATA_SECT_CNT, 0);
233 ide_outb(device, ATA_SECT_NUM, 0);
234 ide_outb(device, ATA_CYL_LOW, (unsigned char) (buflen & 0xFF));
235 ide_outb(device, ATA_CYL_HIGH,
236 (unsigned char) ((buflen >> 8) & 0xFF));
237 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
238
Heinrich Schuchardte6e9a4f2020-02-27 18:28:00 +0100239 ide_outb(device, ATA_COMMAND, ATA_CMD_PACKET);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600240 udelay(50);
241
242 mask = ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR;
243 res = ATA_STAT_DRQ;
244 c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
245
246 if ((c & mask) != res) { /* DRQ must be 1, BSY 0 */
247 printf("ATAPI_ISSUE: Error (no IRQ) before sending ccb dev %d status 0x%02x\n",
248 device, c);
249 err = 0xFF;
250 goto AI_OUT;
251 }
252
253 /* write command block */
254 ide_output_data_shorts(device, (unsigned short *)ccb, ccblen / 2);
255
256 /* ATAPI Command written wait for completition */
Simon Glass4d89f4b2023-04-25 10:54:27 -0600257 mdelay(5); /* device must set bsy */
Simon Glasse9be1ee2016-05-01 11:36:10 -0600258
259 mask = ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR;
260 /*
261 * if no data wait for DRQ = 0 BSY = 0
262 * if data wait for DRQ = 1 BSY = 0
263 */
264 res = 0;
265 if (buflen)
266 res = ATA_STAT_DRQ;
267 c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
268 if ((c & mask) != res) {
269 if (c & ATA_STAT_ERR) {
270 err = (ide_inb(device, ATA_ERROR_REG)) >> 4;
Simon Glass692bccb2023-04-25 10:54:53 -0600271 log_debug("1 returned sense key %x status %02x\n",
272 err, c);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600273 } else {
274 printf("ATAPI_ISSUE: (no DRQ) after sending ccb (%x) status 0x%02x\n",
275 ccb[0], c);
276 err = 0xFF;
277 }
278 goto AI_OUT;
279 }
280 n = ide_inb(device, ATA_CYL_HIGH);
281 n <<= 8;
282 n += ide_inb(device, ATA_CYL_LOW);
283 if (n > buflen) {
284 printf("ERROR, transfer bytes %d requested only %d\n", n,
285 buflen);
286 err = 0xff;
287 goto AI_OUT;
288 }
289 if ((n == 0) && (buflen < 0)) {
290 printf("ERROR, transfer bytes %d requested %d\n", n, buflen);
291 err = 0xff;
292 goto AI_OUT;
293 }
294 if (n != buflen) {
Simon Glass692bccb2023-04-25 10:54:53 -0600295 log_debug("WARNING, transfer bytes %d not equal with requested %d\n",
296 n, buflen);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600297 }
298 if (n != 0) { /* data transfer */
Simon Glass692bccb2023-04-25 10:54:53 -0600299 log_debug("ATAPI_ISSUE: %d Bytes to transfer\n", n);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600300 /* we transfer shorts */
301 n >>= 1;
302 /* ok now decide if it is an in or output */
303 if ((ide_inb(device, ATA_SECT_CNT) & 0x02) == 0) {
Simon Glass692bccb2023-04-25 10:54:53 -0600304 log_debug("Write to device\n");
Simon Glasse9be1ee2016-05-01 11:36:10 -0600305 ide_output_data_shorts(device, (unsigned short *)buffer,
306 n);
307 } else {
Simon Glass692bccb2023-04-25 10:54:53 -0600308 log_debug("Read from device @ %p shorts %d\n", buffer,
309 n);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600310 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);
Simon Glass692bccb2023-04-25 10:54:53 -0600320 log_debug("2 returned sense key %x status %x\n", err, c);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600321 } else {
322 err = 0;
323 }
324AI_OUT:
Simon Glasse9be1ee2016-05-01 11:36:10 -0600325 return err;
326}
327
328/*
329 * sending the command to atapi_issue. If an status other than good
330 * returns, an request_sense will be issued
331 */
332
333#define ATAPI_DRIVE_NOT_READY 100
334#define ATAPI_UNIT_ATTN 10
335
Simon Glass1b33fd82023-04-25 10:54:36 -0600336static unsigned char atapi_issue_autoreq(int device, unsigned char *ccb,
337 int ccblen,
338 unsigned char *buffer, int buflen)
Simon Glasse9be1ee2016-05-01 11:36:10 -0600339{
340 unsigned char sense_data[18], sense_ccb[12];
341 unsigned char res, key, asc, ascq;
342 int notready, unitattn;
343
344 unitattn = ATAPI_UNIT_ATTN;
345 notready = ATAPI_DRIVE_NOT_READY;
346
347retry:
348 res = atapi_issue(device, ccb, ccblen, buffer, buflen);
349 if (res == 0)
350 return 0; /* Ok */
351
352 if (res == 0xFF)
353 return 0xFF; /* error */
354
Simon Glass692bccb2023-04-25 10:54:53 -0600355 log_debug("(auto_req)atapi_issue returned sense key %x\n", res);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600356
357 memset(sense_ccb, 0, sizeof(sense_ccb));
358 memset(sense_data, 0, sizeof(sense_data));
359 sense_ccb[0] = ATAPI_CMD_REQ_SENSE;
360 sense_ccb[4] = 18; /* allocation Length */
361
362 res = atapi_issue(device, sense_ccb, 12, sense_data, 18);
363 key = (sense_data[2] & 0xF);
364 asc = (sense_data[12]);
365 ascq = (sense_data[13]);
366
Simon Glass692bccb2023-04-25 10:54:53 -0600367 log_debug("ATAPI_CMD_REQ_SENSE returned %x\n", res);
368 log_debug(" Sense page: %02X key %02X ASC %02X ASCQ %02X\n",
369 sense_data[0], key, asc, ascq);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600370
371 if ((key == 0))
372 return 0; /* ok device ready */
373
374 if ((key == 6) || (asc == 0x29) || (asc == 0x28)) { /* Unit Attention */
375 if (unitattn-- > 0) {
Simon Glass4d89f4b2023-04-25 10:54:27 -0600376 mdelay(200);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600377 goto retry;
378 }
379 printf("Unit Attention, tried %d\n", ATAPI_UNIT_ATTN);
380 goto error;
381 }
382 if ((asc == 0x4) && (ascq == 0x1)) {
383 /* not ready, but will be ready soon */
384 if (notready-- > 0) {
Simon Glass4d89f4b2023-04-25 10:54:27 -0600385 mdelay(200);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600386 goto retry;
387 }
388 printf("Drive not ready, tried %d times\n",
389 ATAPI_DRIVE_NOT_READY);
390 goto error;
391 }
392 if (asc == 0x3a) {
Simon Glass692bccb2023-04-25 10:54:53 -0600393 log_debug("Media not present\n");
Simon Glasse9be1ee2016-05-01 11:36:10 -0600394 goto error;
395 }
396
397 printf("ERROR: Unknown Sense key %02X ASC %02X ASCQ %02X\n", key, asc,
398 ascq);
399error:
Simon Glass692bccb2023-04-25 10:54:53 -0600400 log_debug("ERROR Sense key %02X ASC %02X ASCQ %02X\n", key, asc, ascq);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600401 return 0xFF;
402}
403
404/*
405 * atapi_read:
406 * we transfer only one block per command, since the multiple DRQ per
407 * command is not yet implemented
408 */
409#define ATAPI_READ_MAX_BYTES 2048 /* we read max 2kbytes */
410#define ATAPI_READ_BLOCK_SIZE 2048 /* assuming CD part */
411#define ATAPI_READ_MAX_BLOCK (ATAPI_READ_MAX_BYTES/ATAPI_READ_BLOCK_SIZE)
412
Simon Glass1b33fd82023-04-25 10:54:36 -0600413static ulong atapi_read(struct udevice *dev, lbaint_t blknr, lbaint_t blkcnt,
414 void *buffer)
Simon Glasse9be1ee2016-05-01 11:36:10 -0600415{
Simon Glassce99e292023-04-25 10:54:47 -0600416 struct blk_desc *desc = dev_get_uclass_plat(dev);
417 int device = desc->devnum;
Simon Glasse9be1ee2016-05-01 11:36:10 -0600418 ulong n = 0;
419 unsigned char ccb[12]; /* Command descriptor block */
420 ulong cnt;
421
Simon Glass692bccb2023-04-25 10:54:53 -0600422 log_debug("%d start " LBAF " blocks " LBAF " buffer at %lx\n", device,
423 blknr, blkcnt, (ulong)buffer);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600424
425 do {
426 if (blkcnt > ATAPI_READ_MAX_BLOCK)
427 cnt = ATAPI_READ_MAX_BLOCK;
428 else
429 cnt = blkcnt;
430
431 ccb[0] = ATAPI_CMD_READ_12;
432 ccb[1] = 0; /* reserved */
433 ccb[2] = (unsigned char) (blknr >> 24) & 0xFF; /* MSB Block */
434 ccb[3] = (unsigned char) (blknr >> 16) & 0xFF; /* */
435 ccb[4] = (unsigned char) (blknr >> 8) & 0xFF;
436 ccb[5] = (unsigned char) blknr & 0xFF; /* LSB Block */
437 ccb[6] = (unsigned char) (cnt >> 24) & 0xFF; /* MSB Block cnt */
438 ccb[7] = (unsigned char) (cnt >> 16) & 0xFF;
439 ccb[8] = (unsigned char) (cnt >> 8) & 0xFF;
440 ccb[9] = (unsigned char) cnt & 0xFF; /* LSB Block */
441 ccb[10] = 0; /* reserved */
442 ccb[11] = 0; /* reserved */
443
444 if (atapi_issue_autoreq(device, ccb, 12,
445 (unsigned char *)buffer,
446 cnt * ATAPI_READ_BLOCK_SIZE)
447 == 0xFF) {
448 return n;
449 }
450 n += cnt;
451 blkcnt -= cnt;
452 blknr += cnt;
453 buffer += (cnt * ATAPI_READ_BLOCK_SIZE);
454 } while (blkcnt > 0);
455 return n;
456}
457
Simon Glassce99e292023-04-25 10:54:47 -0600458static void atapi_inquiry(struct blk_desc *desc)
Simon Glasse9be1ee2016-05-01 11:36:10 -0600459{
460 unsigned char ccb[12]; /* Command descriptor block */
461 unsigned char iobuf[64]; /* temp buf */
462 unsigned char c;
463 int device;
464
Simon Glassce99e292023-04-25 10:54:47 -0600465 device = desc->devnum;
466 desc->type = DEV_TYPE_UNKNOWN; /* not yet valid */
Simon Glasse9be1ee2016-05-01 11:36:10 -0600467
468 memset(ccb, 0, sizeof(ccb));
469 memset(iobuf, 0, sizeof(iobuf));
470
471 ccb[0] = ATAPI_CMD_INQUIRY;
472 ccb[4] = 40; /* allocation Legnth */
473 c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *)iobuf, 40);
474
Simon Glass692bccb2023-04-25 10:54:53 -0600475 log_debug("ATAPI_CMD_INQUIRY returned %x\n", c);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600476 if (c != 0)
477 return;
478
479 /* copy device ident strings */
Simon Glassce99e292023-04-25 10:54:47 -0600480 ident_cpy((u8 *)desc->vendor, &iobuf[8], 8);
481 ident_cpy((u8 *)desc->product, &iobuf[16], 16);
482 ident_cpy((u8 *)desc->revision, &iobuf[32], 5);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600483
Simon Glassce99e292023-04-25 10:54:47 -0600484 desc->lun = 0;
485 desc->lba = 0;
486 desc->blksz = 0;
487 desc->log2blksz = LOG2_INVALID(typeof(desc->log2blksz));
488 desc->type = iobuf[0] & 0x1f;
Simon Glasse9be1ee2016-05-01 11:36:10 -0600489
490 if ((iobuf[1] & 0x80) == 0x80)
Simon Glassce99e292023-04-25 10:54:47 -0600491 desc->removable = 1;
Simon Glasse9be1ee2016-05-01 11:36:10 -0600492 else
Simon Glassce99e292023-04-25 10:54:47 -0600493 desc->removable = 0;
Simon Glasse9be1ee2016-05-01 11:36:10 -0600494
495 memset(ccb, 0, sizeof(ccb));
496 memset(iobuf, 0, sizeof(iobuf));
497 ccb[0] = ATAPI_CMD_START_STOP;
498 ccb[4] = 0x03; /* start */
499
500 c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *)iobuf, 0);
501
Simon Glass692bccb2023-04-25 10:54:53 -0600502 log_debug("ATAPI_CMD_START_STOP returned %x\n", c);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600503 if (c != 0)
504 return;
505
506 memset(ccb, 0, sizeof(ccb));
507 memset(iobuf, 0, sizeof(iobuf));
508 c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *)iobuf, 0);
509
Simon Glass692bccb2023-04-25 10:54:53 -0600510 log_debug("ATAPI_CMD_UNIT_TEST_READY returned %x\n", c);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600511 if (c != 0)
512 return;
513
514 memset(ccb, 0, sizeof(ccb));
515 memset(iobuf, 0, sizeof(iobuf));
516 ccb[0] = ATAPI_CMD_READ_CAP;
517 c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *)iobuf, 8);
Simon Glass692bccb2023-04-25 10:54:53 -0600518 log_debug("ATAPI_CMD_READ_CAP returned %x\n", c);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600519 if (c != 0)
520 return;
521
Simon Glass692bccb2023-04-25 10:54:53 -0600522 log_debug("Read Cap: LBA %02X%02X%02X%02X blksize %02X%02X%02X%02X\n",
523 iobuf[0], iobuf[1], iobuf[2], iobuf[3],
524 iobuf[4], iobuf[5], iobuf[6], iobuf[7]);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600525
Simon Glassce99e292023-04-25 10:54:47 -0600526 desc->lba = (ulong)iobuf[0] << 24 | (ulong)iobuf[1] << 16 |
527 (ulong)iobuf[2] << 8 | (ulong)iobuf[3];
528 desc->blksz = (ulong)iobuf[4] << 24 | (ulong)iobuf[5] << 16 |
529 (ulong)iobuf[6] << 8 | (ulong)iobuf[7];
530 desc->log2blksz = LOG2(desc->blksz);
Simon Glass8b1b9432023-04-25 10:54:41 -0600531
Simon Glasse9be1ee2016-05-01 11:36:10 -0600532 /* ATAPI devices cannot use 48bit addressing (ATA/ATAPI v7) */
Simon Glassce99e292023-04-25 10:54:47 -0600533 desc->lba48 = false;
Simon Glasse9be1ee2016-05-01 11:36:10 -0600534}
535
Simon Glass038590a2023-04-25 10:54:48 -0600536/**
537 * ide_ident() - Identify an IDE device
538 *
539 * @device: Device number to use
540 * @desc: Block descriptor to fill in
541 * Returns: 0 if OK, -ENOENT if no device is found
542 */
543static int ide_ident(int device, struct blk_desc *desc)
Simon Glasse9be1ee2016-05-01 11:36:10 -0600544{
545 unsigned char c;
546 hd_driveid_t iop;
Simon Glass606e0542022-08-11 19:34:53 -0600547 bool is_atapi = false;
Simon Glass2a165952023-04-25 10:54:37 -0600548 int tries = 1;
Simon Glasse9be1ee2016-05-01 11:36:10 -0600549
Simon Glass96083112023-04-25 10:54:49 -0600550 memset(desc, '\0', sizeof(*desc));
Simon Glass038590a2023-04-25 10:54:48 -0600551 desc->devnum = device;
Simon Glass96083112023-04-25 10:54:49 -0600552 desc->type = DEV_TYPE_UNKNOWN;
553 desc->uclass_id = UCLASS_IDE;
554 desc->log2blksz = LOG2_INVALID(typeof(desc->log2blksz));
Simon Glasse9be1ee2016-05-01 11:36:10 -0600555 printf(" Device %d: ", device);
556
Simon Glasse9be1ee2016-05-01 11:36:10 -0600557 /* Select device
558 */
559 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
Simon Glass6579bb02023-04-25 10:54:38 -0600560 if (IS_ENABLED(CONFIG_ATAPI))
561 tries = 2;
Simon Glasse9be1ee2016-05-01 11:36:10 -0600562
Simon Glass2a165952023-04-25 10:54:37 -0600563 while (tries) {
Simon Glasse9be1ee2016-05-01 11:36:10 -0600564 /* check signature */
Simon Glass6579bb02023-04-25 10:54:38 -0600565 if (IS_ENABLED(CONFIG_ATAPI) &&
566 ide_inb(device, ATA_SECT_CNT) == 0x01 &&
567 ide_inb(device, ATA_SECT_NUM) == 0x01 &&
568 ide_inb(device, ATA_CYL_LOW) == 0x14 &&
569 ide_inb(device, ATA_CYL_HIGH) == 0xeb) {
Simon Glasse9be1ee2016-05-01 11:36:10 -0600570 /* ATAPI Signature found */
Simon Glass606e0542022-08-11 19:34:53 -0600571 is_atapi = true;
Simon Glasse9be1ee2016-05-01 11:36:10 -0600572 /*
573 * Start Ident Command
574 */
Heinrich Schuchardte6e9a4f2020-02-27 18:28:00 +0100575 ide_outb(device, ATA_COMMAND, ATA_CMD_ID_ATAPI);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600576 /*
577 * Wait for completion - ATAPI devices need more time
578 * to become ready
579 */
580 c = ide_wait(device, ATAPI_TIME_OUT);
Simon Glass6579bb02023-04-25 10:54:38 -0600581 } else {
Simon Glasse9be1ee2016-05-01 11:36:10 -0600582 /*
583 * Start Ident Command
584 */
Heinrich Schuchardte6e9a4f2020-02-27 18:28:00 +0100585 ide_outb(device, ATA_COMMAND, ATA_CMD_ID_ATA);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600586
587 /*
588 * Wait for completion
589 */
590 c = ide_wait(device, IDE_TIME_OUT);
591 }
Simon Glasse9be1ee2016-05-01 11:36:10 -0600592
Simon Glass9367e612023-04-25 10:54:39 -0600593 if ((c & ATA_STAT_DRQ) &&
594 !(c & (ATA_STAT_FAULT | ATA_STAT_ERR))) {
Simon Glasse9be1ee2016-05-01 11:36:10 -0600595 break;
Simon Glass9367e612023-04-25 10:54:39 -0600596 } else if (IS_ENABLED(CONFIG_ATAPI)) {
597 /*
598 * Need to soft reset the device
599 * in case it's an ATAPI...
600 */
Simon Glass692bccb2023-04-25 10:54:53 -0600601 log_debug("Retrying...\n");
Simon Glass9367e612023-04-25 10:54:39 -0600602 ide_outb(device, ATA_DEV_HD,
603 ATA_LBA | ATA_DEVICE(device));
604 mdelay(100);
605 ide_outb(device, ATA_COMMAND, 0x08);
606 mdelay(500);
607 /* Select device */
608 ide_outb(device, ATA_DEV_HD,
609 ATA_LBA | ATA_DEVICE(device));
Simon Glass6579bb02023-04-25 10:54:38 -0600610 }
Simon Glass9367e612023-04-25 10:54:39 -0600611 tries--;
Simon Glass6579bb02023-04-25 10:54:38 -0600612 }
Simon Glasse9be1ee2016-05-01 11:36:10 -0600613
Simon Glass2a165952023-04-25 10:54:37 -0600614 if (!tries) /* Not found */
Simon Glass038590a2023-04-25 10:54:48 -0600615 return -ENOENT;
Simon Glasse9be1ee2016-05-01 11:36:10 -0600616
617 ide_input_swap_data(device, (ulong *)&iop, ATA_SECTORWORDS);
618
Simon Glassce99e292023-04-25 10:54:47 -0600619 ident_cpy((u8 *)desc->revision, iop.fw_rev, sizeof(desc->revision));
620 ident_cpy((u8 *)desc->vendor, iop.model, sizeof(desc->vendor));
621 ident_cpy((u8 *)desc->product, iop.serial_no, sizeof(desc->product));
Simon Glasse9be1ee2016-05-01 11:36:10 -0600622
623 if ((iop.config & 0x0080) == 0x0080)
Simon Glassce99e292023-04-25 10:54:47 -0600624 desc->removable = 1;
Simon Glasse9be1ee2016-05-01 11:36:10 -0600625 else
Simon Glassce99e292023-04-25 10:54:47 -0600626 desc->removable = 0;
Simon Glasse9be1ee2016-05-01 11:36:10 -0600627
Simon Glassc94a3312023-04-25 10:54:40 -0600628 if (IS_ENABLED(CONFIG_ATAPI) && is_atapi) {
Simon Glassce99e292023-04-25 10:54:47 -0600629 desc->atapi = true;
630 atapi_inquiry(desc);
Simon Glass038590a2023-04-25 10:54:48 -0600631 return 0;
Simon Glasse9be1ee2016-05-01 11:36:10 -0600632 }
Simon Glasse9be1ee2016-05-01 11:36:10 -0600633
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]);
Simon Glassce99e292023-04-25 10:54:47 -0600636 desc->lba = (ulong)iop.lba_capacity[0] |
637 (ulong)iop.lba_capacity[1] << 16;
Simon Glasse9be1ee2016-05-01 11:36:10 -0600638
Simon Glass8b1b9432023-04-25 10:54:41 -0600639 if (IS_ENABLED(CONFIG_LBA48) && (iop.command_set_2 & 0x0400)) {
640 /* LBA 48 support */
Simon Glassce99e292023-04-25 10:54:47 -0600641 desc->lba48 = true;
Reinoud Zandijk0a527fd2021-02-24 17:44:42 +0100642 for (int i = 0; i < 4; i++)
643 iop.lba48_capacity[i] = be16_to_cpu(iop.lba48_capacity[i]);
Simon Glassce99e292023-04-25 10:54:47 -0600644 desc->lba = (unsigned long long)iop.lba48_capacity[0] |
645 (unsigned long long)iop.lba48_capacity[1] << 16 |
646 (unsigned long long)iop.lba48_capacity[2] << 32 |
647 (unsigned long long)iop.lba48_capacity[3] << 48;
Simon Glasse9be1ee2016-05-01 11:36:10 -0600648 } else {
Simon Glassce99e292023-04-25 10:54:47 -0600649 desc->lba48 = false;
Simon Glasse9be1ee2016-05-01 11:36:10 -0600650 }
Simon Glass8b1b9432023-04-25 10:54:41 -0600651
Simon Glasse9be1ee2016-05-01 11:36:10 -0600652 /* assuming HD */
Simon Glassce99e292023-04-25 10:54:47 -0600653 desc->type = DEV_TYPE_HARDDISK;
654 desc->blksz = ATA_BLOCKSIZE;
655 desc->log2blksz = LOG2(desc->blksz);
656 desc->lun = 0; /* just to fill something in... */
Simon Glasse9be1ee2016-05-01 11:36:10 -0600657
658#if 0 /* only used to test the powersaving mode,
659 * if enabled, the drive goes after 5 sec
660 * in standby mode */
661 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
662 c = ide_wait(device, IDE_TIME_OUT);
663 ide_outb(device, ATA_SECT_CNT, 1);
664 ide_outb(device, ATA_LBA_LOW, 0);
665 ide_outb(device, ATA_LBA_MID, 0);
666 ide_outb(device, ATA_LBA_HIGH, 0);
667 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
668 ide_outb(device, ATA_COMMAND, 0xe3);
669 udelay(50);
670 c = ide_wait(device, IDE_TIME_OUT); /* can't take over 500 ms */
671#endif
Simon Glass038590a2023-04-25 10:54:48 -0600672
673 return 0;
Simon Glasse9be1ee2016-05-01 11:36:10 -0600674}
675
Simon Glassb6483ea2023-04-25 10:54:42 -0600676/**
677 * ide_init_one() - Init one IDE device
678 *
679 * @bus: Bus to use
680 * Return: 0 iuf OK, -EIO if not available, -ETIMEDOUT if timed out
681 */
682static int ide_init_one(int bus)
683{
684 int dev = bus * CONFIG_SYS_IDE_MAXDEVICE / CONFIG_SYS_IDE_MAXBUS;
685 int i;
686 u8 c;
687
688 printf("Bus %d: ", bus);
689
690 /* Select device */
691 mdelay(100);
692 ide_outb(dev, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(dev));
693 mdelay(100);
694 i = 0;
695 do {
696 mdelay(10);
697
698 c = ide_inb(dev, ATA_STATUS);
699 i++;
700 if (i > (ATA_RESET_TIME * 100)) {
701 puts("** Timeout **\n");
702 return -ETIMEDOUT;
703 }
704 if (i >= 100 && !(i % 100))
705 putc('.');
706 } while (c & ATA_STAT_BUSY);
707
708 if (c & (ATA_STAT_BUSY | ATA_STAT_FAULT)) {
709 puts("not available ");
Simon Glass692bccb2023-04-25 10:54:53 -0600710 log_debug("Status = 0x%02X ", c);
Simon Glassb6483ea2023-04-25 10:54:42 -0600711 return -EIO;
712 } else if (IS_ENABLED(CONFIG_ATAPI) && !(c & ATA_STAT_READY)) {
713 /* ATAPI Devices do not set DRDY */
714 puts("not available ");
Simon Glass692bccb2023-04-25 10:54:53 -0600715 log_debug("Status = 0x%02X ", c);
Simon Glassb6483ea2023-04-25 10:54:42 -0600716 return -EIO;
717 }
718 puts("OK ");
719
720 return 0;
721}
722
Simon Glassf8e87e72023-04-25 10:54:33 -0600723static void ide_output_data(int dev, const ulong *sect_buf, int words)
Simon Glasse9be1ee2016-05-01 11:36:10 -0600724{
Reinoud Zandijk0a527fd2021-02-24 17:44:42 +0100725 uintptr_t paddr = (ATA_CURR_BASE(dev) + ATA_DATA_REG);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600726 ushort *dbuf;
Simon Glasse9be1ee2016-05-01 11:36:10 -0600727
Simon Glasse9be1ee2016-05-01 11:36:10 -0600728 dbuf = (ushort *)sect_buf;
Simon Glasse9be1ee2016-05-01 11:36:10 -0600729 while (words--) {
730 EIEIO;
Reinoud Zandijk0a527fd2021-02-24 17:44:42 +0100731 outw(cpu_to_le16(*dbuf++), paddr);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600732 EIEIO;
Reinoud Zandijk0a527fd2021-02-24 17:44:42 +0100733 outw(cpu_to_le16(*dbuf++), paddr);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600734 }
735}
Reinoud Zandijk0a527fd2021-02-24 17:44:42 +0100736
Simon Glassf8e87e72023-04-25 10:54:33 -0600737static void ide_input_data(int dev, ulong *sect_buf, int words)
Simon Glasse9be1ee2016-05-01 11:36:10 -0600738{
Reinoud Zandijk0a527fd2021-02-24 17:44:42 +0100739 uintptr_t paddr = (ATA_CURR_BASE(dev) + ATA_DATA_REG);
740 ushort *dbuf;
Simon Glasse9be1ee2016-05-01 11:36:10 -0600741
Reinoud Zandijk0a527fd2021-02-24 17:44:42 +0100742 dbuf = (ushort *)sect_buf;
743
Simon Glass692bccb2023-04-25 10:54:53 -0600744 log_debug("in input data base for read is %p\n", (void *)paddr);
Reinoud Zandijk0a527fd2021-02-24 17:44:42 +0100745
746 while (words--) {
747 EIEIO;
748 *dbuf++ = le16_to_cpu(inw(paddr));
749 EIEIO;
750 *dbuf++ = le16_to_cpu(inw(paddr));
751 }
Reinoud Zandijk0a527fd2021-02-24 17:44:42 +0100752}
Simon Glasse9be1ee2016-05-01 11:36:10 -0600753
Simon Glass1b33fd82023-04-25 10:54:36 -0600754static ulong ide_read(struct udevice *dev, lbaint_t blknr, lbaint_t blkcnt,
755 void *buffer)
Simon Glasse9be1ee2016-05-01 11:36:10 -0600756{
Simon Glassce99e292023-04-25 10:54:47 -0600757 struct blk_desc *desc = dev_get_uclass_plat(dev);
758 int device = desc->devnum;
Simon Glasse9be1ee2016-05-01 11:36:10 -0600759 ulong n = 0;
760 unsigned char c;
761 unsigned char pwrsave = 0; /* power save */
Simon Glass8b1b9432023-04-25 10:54:41 -0600762 bool lba48 = false;
Simon Glasse9be1ee2016-05-01 11:36:10 -0600763
Simon Glass8b1b9432023-04-25 10:54:41 -0600764 if (IS_ENABLED(CONFIG_LBA48) && (blknr & 0x0000fffff0000000ULL)) {
Simon Glasse9be1ee2016-05-01 11:36:10 -0600765 /* more than 28 bits used, use 48bit mode */
Simon Glass8b1b9432023-04-25 10:54:41 -0600766 lba48 = true;
Simon Glasse9be1ee2016-05-01 11:36:10 -0600767 }
Simon Glass8b1b9432023-04-25 10:54:41 -0600768
Simon Glass692bccb2023-04-25 10:54:53 -0600769 log_debug("dev %d start " LBAF ", blocks " LBAF " buffer at %lx\n",
770 device, blknr, blkcnt, (ulong)buffer);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600771
Simon Glasse9be1ee2016-05-01 11:36:10 -0600772 /* Select device
773 */
774 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
775 c = ide_wait(device, IDE_TIME_OUT);
776
777 if (c & ATA_STAT_BUSY) {
778 printf("IDE read: device %d not ready\n", device);
779 goto IDE_READ_E;
780 }
781
782 /* first check if the drive is in Powersaving mode, if yes,
783 * increase the timeout value */
Heinrich Schuchardte6e9a4f2020-02-27 18:28:00 +0100784 ide_outb(device, ATA_COMMAND, ATA_CMD_CHK_POWER);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600785 udelay(50);
786
787 c = ide_wait(device, IDE_TIME_OUT); /* can't take over 500 ms */
788
789 if (c & ATA_STAT_BUSY) {
790 printf("IDE read: device %d not ready\n", device);
791 goto IDE_READ_E;
792 }
793 if ((c & ATA_STAT_ERR) == ATA_STAT_ERR) {
Simon Glass692bccb2023-04-25 10:54:53 -0600794 printf("No Powersaving mode %x\n", c);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600795 } else {
796 c = ide_inb(device, ATA_SECT_CNT);
Simon Glass692bccb2023-04-25 10:54:53 -0600797 log_debug("Powersaving %02X\n", c);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600798 if (c == 0)
799 pwrsave = 1;
800 }
801
802
803 while (blkcnt-- > 0) {
804 c = ide_wait(device, IDE_TIME_OUT);
805
806 if (c & ATA_STAT_BUSY) {
807 printf("IDE read: device %d not ready\n", device);
808 break;
809 }
Simon Glass8b1b9432023-04-25 10:54:41 -0600810 if (IS_ENABLED(CONFIG_LBA48) && lba48) {
Simon Glasse9be1ee2016-05-01 11:36:10 -0600811 /* write high bits */
812 ide_outb(device, ATA_SECT_CNT, 0);
813 ide_outb(device, ATA_LBA_LOW, (blknr >> 24) & 0xFF);
814#ifdef CONFIG_SYS_64BIT_LBA
815 ide_outb(device, ATA_LBA_MID, (blknr >> 32) & 0xFF);
816 ide_outb(device, ATA_LBA_HIGH, (blknr >> 40) & 0xFF);
817#else
818 ide_outb(device, ATA_LBA_MID, 0);
819 ide_outb(device, ATA_LBA_HIGH, 0);
820#endif
821 }
Simon Glasse9be1ee2016-05-01 11:36:10 -0600822 ide_outb(device, ATA_SECT_CNT, 1);
823 ide_outb(device, ATA_LBA_LOW, (blknr >> 0) & 0xFF);
824 ide_outb(device, ATA_LBA_MID, (blknr >> 8) & 0xFF);
825 ide_outb(device, ATA_LBA_HIGH, (blknr >> 16) & 0xFF);
826
Simon Glass8b1b9432023-04-25 10:54:41 -0600827 if (IS_ENABLED(CONFIG_LBA48) && lba48) {
Simon Glasse9be1ee2016-05-01 11:36:10 -0600828 ide_outb(device, ATA_DEV_HD,
829 ATA_LBA | ATA_DEVICE(device));
Reinoud Zandijk0a527fd2021-02-24 17:44:42 +0100830 ide_outb(device, ATA_COMMAND, ATA_CMD_PIO_READ_EXT);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600831
Simon Glass8b1b9432023-04-25 10:54:41 -0600832 } else {
Simon Glasse9be1ee2016-05-01 11:36:10 -0600833 ide_outb(device, ATA_DEV_HD, ATA_LBA |
834 ATA_DEVICE(device) | ((blknr >> 24) & 0xF));
Reinoud Zandijk0a527fd2021-02-24 17:44:42 +0100835 ide_outb(device, ATA_COMMAND, ATA_CMD_PIO_READ);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600836 }
837
838 udelay(50);
839
840 if (pwrsave) {
841 /* may take up to 4 sec */
842 c = ide_wait(device, IDE_SPIN_UP_TIME_OUT);
843 pwrsave = 0;
844 } else {
845 /* can't take over 500 ms */
846 c = ide_wait(device, IDE_TIME_OUT);
847 }
848
849 if ((c & (ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR)) !=
850 ATA_STAT_DRQ) {
851 printf("Error (no IRQ) dev %d blk " LBAF
852 ": status %#02x\n", device, blknr, c);
853 break;
854 }
855
856 ide_input_data(device, buffer, ATA_SECTORWORDS);
857 (void) ide_inb(device, ATA_STATUS); /* clear IRQ */
858
859 ++n;
860 ++blknr;
861 buffer += ATA_BLOCKSIZE;
862 }
863IDE_READ_E:
Simon Glasse9be1ee2016-05-01 11:36:10 -0600864 return n;
865}
866
Simon Glass1b33fd82023-04-25 10:54:36 -0600867static ulong ide_write(struct udevice *dev, lbaint_t blknr, lbaint_t blkcnt,
868 const void *buffer)
Simon Glasse9be1ee2016-05-01 11:36:10 -0600869{
Simon Glassce99e292023-04-25 10:54:47 -0600870 struct blk_desc *desc = dev_get_uclass_plat(dev);
871 int device = desc->devnum;
Simon Glasse9be1ee2016-05-01 11:36:10 -0600872 ulong n = 0;
873 unsigned char c;
Simon Glass8b1b9432023-04-25 10:54:41 -0600874 bool lba48 = false;
Simon Glasse9be1ee2016-05-01 11:36:10 -0600875
Simon Glass8b1b9432023-04-25 10:54:41 -0600876 if (IS_ENABLED(CONFIG_LBA48) && (blknr & 0x0000fffff0000000ULL)) {
Simon Glasse9be1ee2016-05-01 11:36:10 -0600877 /* more than 28 bits used, use 48bit mode */
Simon Glass8b1b9432023-04-25 10:54:41 -0600878 lba48 = true;
Simon Glasse9be1ee2016-05-01 11:36:10 -0600879 }
Simon Glasse9be1ee2016-05-01 11:36:10 -0600880
Simon Glasse9be1ee2016-05-01 11:36:10 -0600881 /* Select device
882 */
883 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
884
885 while (blkcnt-- > 0) {
886 c = ide_wait(device, IDE_TIME_OUT);
887
888 if (c & ATA_STAT_BUSY) {
889 printf("IDE read: device %d not ready\n", device);
890 goto WR_OUT;
891 }
Simon Glass8b1b9432023-04-25 10:54:41 -0600892 if (IS_ENABLED(CONFIG_LBA48) && lba48) {
Simon Glasse9be1ee2016-05-01 11:36:10 -0600893 /* write high bits */
894 ide_outb(device, ATA_SECT_CNT, 0);
895 ide_outb(device, ATA_LBA_LOW, (blknr >> 24) & 0xFF);
896#ifdef CONFIG_SYS_64BIT_LBA
897 ide_outb(device, ATA_LBA_MID, (blknr >> 32) & 0xFF);
898 ide_outb(device, ATA_LBA_HIGH, (blknr >> 40) & 0xFF);
899#else
900 ide_outb(device, ATA_LBA_MID, 0);
901 ide_outb(device, ATA_LBA_HIGH, 0);
902#endif
903 }
Simon Glasse9be1ee2016-05-01 11:36:10 -0600904 ide_outb(device, ATA_SECT_CNT, 1);
905 ide_outb(device, ATA_LBA_LOW, (blknr >> 0) & 0xFF);
906 ide_outb(device, ATA_LBA_MID, (blknr >> 8) & 0xFF);
907 ide_outb(device, ATA_LBA_HIGH, (blknr >> 16) & 0xFF);
908
Simon Glass8b1b9432023-04-25 10:54:41 -0600909 if (IS_ENABLED(CONFIG_LBA48) && lba48) {
Simon Glasse9be1ee2016-05-01 11:36:10 -0600910 ide_outb(device, ATA_DEV_HD,
911 ATA_LBA | ATA_DEVICE(device));
Reinoud Zandijk0a527fd2021-02-24 17:44:42 +0100912 ide_outb(device, ATA_COMMAND, ATA_CMD_PIO_WRITE_EXT);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600913
Simon Glass8b1b9432023-04-25 10:54:41 -0600914 } else {
Simon Glasse9be1ee2016-05-01 11:36:10 -0600915 ide_outb(device, ATA_DEV_HD, ATA_LBA |
916 ATA_DEVICE(device) | ((blknr >> 24) & 0xF));
Reinoud Zandijk0a527fd2021-02-24 17:44:42 +0100917 ide_outb(device, ATA_COMMAND, ATA_CMD_PIO_WRITE);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600918 }
919
920 udelay(50);
921
922 /* can't take over 500 ms */
923 c = ide_wait(device, IDE_TIME_OUT);
924
925 if ((c & (ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR)) !=
926 ATA_STAT_DRQ) {
927 printf("Error (no IRQ) dev %d blk " LBAF
928 ": status %#02x\n", device, blknr, c);
929 goto WR_OUT;
930 }
931
932 ide_output_data(device, buffer, ATA_SECTORWORDS);
933 c = ide_inb(device, ATA_STATUS); /* clear IRQ */
934 ++n;
935 ++blknr;
936 buffer += ATA_BLOCKSIZE;
937 }
938WR_OUT:
Simon Glasse9be1ee2016-05-01 11:36:10 -0600939 return n;
940}
941
Simon Glass646deed2023-04-25 10:54:35 -0600942ulong ide_or_atapi_read(struct udevice *dev, lbaint_t blknr, lbaint_t blkcnt,
943 void *buffer)
944{
945 struct blk_desc *desc = dev_get_uclass_plat(dev);
946
947 if (IS_ENABLED(CONFIG_ATAPI) && desc->atapi)
948 return atapi_read(dev, blknr, blkcnt, buffer);
949
950 return ide_read(dev, blknr, blkcnt, buffer);
951}
952
Simon Glass145df842016-05-01 11:36:22 -0600953static const struct blk_ops ide_blk_ops = {
Simon Glass646deed2023-04-25 10:54:35 -0600954 .read = ide_or_atapi_read,
Simon Glass145df842016-05-01 11:36:22 -0600955 .write = ide_write,
956};
957
958U_BOOT_DRIVER(ide_blk) = {
959 .name = "ide_blk",
960 .id = UCLASS_BLK,
961 .ops = &ide_blk_ops,
Bin Meng68e6f222017-09-10 05:12:51 -0700962};
963
Simon Glass0d77f8f2023-01-17 10:47:46 -0700964static int ide_bootdev_bind(struct udevice *dev)
965{
966 struct bootdev_uc_plat *ucp = dev_get_uclass_plat(dev);
967
Simon Glasseacc2612023-01-17 10:48:08 -0700968 ucp->prio = BOOTDEVP_5_SCAN_SLOW;
Simon Glass0d77f8f2023-01-17 10:47:46 -0700969
970 return 0;
971}
972
973static int ide_bootdev_hunt(struct bootdev_hunter *info, bool show)
974{
Simon Glass80778f52023-04-25 10:54:30 -0600975 struct udevice *dev;
976
977 uclass_first_device(UCLASS_IDE, &dev);
Simon Glass0d77f8f2023-01-17 10:47:46 -0700978
979 return 0;
980}
981
982struct bootdev_ops ide_bootdev_ops = {
983};
984
985static const struct udevice_id ide_bootdev_ids[] = {
986 { .compatible = "u-boot,bootdev-ide" },
987 { }
988};
989
990U_BOOT_DRIVER(ide_bootdev) = {
991 .name = "ide_bootdev",
992 .id = UCLASS_BOOTDEV,
993 .ops = &ide_bootdev_ops,
994 .bind = ide_bootdev_bind,
995 .of_match = ide_bootdev_ids,
996};
997
998BOOTDEV_HUNTER(ide_bootdev_hunter) = {
Simon Glasseacc2612023-01-17 10:48:08 -0700999 .prio = BOOTDEVP_5_SCAN_SLOW,
Simon Glass0d77f8f2023-01-17 10:47:46 -07001000 .uclass = UCLASS_IDE,
1001 .hunt = ide_bootdev_hunt,
1002 .drv = DM_DRIVER_REF(ide_bootdev),
1003};
1004
Bin Meng68e6f222017-09-10 05:12:51 -07001005static int ide_probe(struct udevice *udev)
1006{
Simon Glass708404c2023-04-25 10:54:45 -06001007 bool bus_ok[CONFIG_SYS_IDE_MAXBUS];
1008 int i, bus;
Bin Meng68e6f222017-09-10 05:12:51 -07001009
Simon Glass708404c2023-04-25 10:54:45 -06001010 schedule();
1011
1012 /* ATAPI Drives seems to need a proper IDE Reset */
1013 ide_reset();
1014
1015 /*
1016 * Wait for IDE to get ready.
1017 * According to spec, this can take up to 31 seconds!
1018 */
1019 for (bus = 0; bus < CONFIG_SYS_IDE_MAXBUS; ++bus) {
1020 bus_ok[bus] = !ide_init_one(bus);
1021 schedule();
1022 }
1023
1024 putc('\n');
1025
Simon Glassf0af25a2023-04-25 10:54:46 -06001026 schedule();
1027
1028 for (i = 0; i < CONFIG_SYS_IDE_MAXDEVICE; i++) {
Simon Glassd7d57432023-04-25 10:54:50 -06001029 struct blk_desc *desc, pdesc;
Simon Glass038590a2023-04-25 10:54:48 -06001030 struct udevice *blk;
Simon Glass038590a2023-04-25 10:54:48 -06001031 char name[20];
Simon Glass038590a2023-04-25 10:54:48 -06001032 int ret;
1033
Simon Glassf0af25a2023-04-25 10:54:46 -06001034 if (!bus_ok[IDE_BUS(i)])
1035 continue;
1036
Simon Glassd7d57432023-04-25 10:54:50 -06001037 ret = ide_ident(i, &pdesc);
1038 dev_print(&pdesc);
Simon Glass80778f52023-04-25 10:54:30 -06001039
Simon Glass038590a2023-04-25 10:54:48 -06001040 if (ret)
1041 continue;
Simon Glassdb89e722023-04-25 10:54:44 -06001042
Simon Glass038590a2023-04-25 10:54:48 -06001043 sprintf(name, "blk#%d", i);
Bin Meng68e6f222017-09-10 05:12:51 -07001044
Simon Glass038590a2023-04-25 10:54:48 -06001045 /*
1046 * With CDROM, if there is no CD inserted, blksz will
1047 * be zero, don't bother to create IDE block device.
1048 */
Simon Glass49aa7782023-04-25 10:54:51 -06001049 if (!pdesc.blksz)
Simon Glass038590a2023-04-25 10:54:48 -06001050 continue;
1051 ret = blk_create_devicef(udev, "ide_blk", name, UCLASS_IDE, i,
Simon Glass49aa7782023-04-25 10:54:51 -06001052 pdesc.blksz, pdesc.lba, &blk);
Simon Glass038590a2023-04-25 10:54:48 -06001053 if (ret)
1054 return ret;
AKASHI Takahiro4c73b032022-03-08 20:36:44 +09001055
Simon Glass038590a2023-04-25 10:54:48 -06001056 ret = blk_probe_or_unbind(blk);
1057 if (ret)
1058 return ret;
Simon Glass0d77f8f2023-01-17 10:47:46 -07001059
Simon Glass038590a2023-04-25 10:54:48 -06001060 /* fill in device vendor/product/rev strings */
1061 desc = dev_get_uclass_plat(blk);
Simon Glassd7d57432023-04-25 10:54:50 -06001062 strlcpy(desc->vendor, pdesc.vendor, BLK_VEN_SIZE);
1063 strlcpy(desc->product, pdesc.product, BLK_PRD_SIZE);
1064 strlcpy(desc->revision, pdesc.revision, BLK_REV_SIZE);
1065 desc->removable = pdesc.removable;
1066 desc->atapi = pdesc.atapi;
1067 desc->lba48 = pdesc.lba48;
1068 desc->type = pdesc.type;
Simon Glass708404c2023-04-25 10:54:45 -06001069
Simon Glass038590a2023-04-25 10:54:48 -06001070 ret = bootdev_setup_for_dev(udev, "ide_bootdev");
1071 if (ret)
1072 return log_msg_ret("bootdev", ret);
Bin Meng68e6f222017-09-10 05:12:51 -07001073 }
1074
1075 return 0;
1076}
1077
1078U_BOOT_DRIVER(ide) = {
1079 .name = "ide",
1080 .id = UCLASS_IDE,
1081 .probe = ide_probe,
1082};
1083
1084struct pci_device_id ide_supported[] = {
1085 { PCI_DEVICE_CLASS(PCI_CLASS_STORAGE_IDE << 8, 0xffff00) },
1086 { }
1087};
1088
1089U_BOOT_PCI_DEVICE(ide, ide_supported);
1090
1091UCLASS_DRIVER(ide) = {
1092 .name = "ide",
1093 .id = UCLASS_IDE,
Simon Glass145df842016-05-01 11:36:22 -06001094};