blob: af0c951eb8906f9cffb240f010f455f1d24f4e76 [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);
Simon Glass79543e62023-04-25 10:54:54 -0600111 if (!delay--)
Simon Glasse9be1ee2016-05-01 11:36:10 -0600112 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{
Simon Glass79543e62023-04-25 10:54:54 -0600156 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{
Simon Glass79543e62023-04-25 10:54:54 -0600171 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
Simon Glass79543e62023-04-25 10:54:54 -0600198 while (c = ide_inb(dev, ATA_STATUS) & mask, c != res) {
Simon Glasse9be1ee2016-05-01 11:36:10 -0600199 /* 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);
Simon Glass79543e62023-04-25 10:54:54 -0600203 if (!delay--)
Simon Glasse9be1ee2016-05-01 11:36:10 -0600204 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);
Simon Glass79543e62023-04-25 10:54:54 -0600227 err = 0xff;
Simon Glasse9be1ee2016-05-01 11:36:10 -0600228 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);
Simon Glass79543e62023-04-25 10:54:54 -0600234 ide_outb(device, ATA_CYL_LOW, (unsigned char)(buflen & 0xff));
235 ide_outb(device, ATA_CYL_HIGH, (unsigned char)((buflen >> 8) & 0xff));
Simon Glasse9be1ee2016-05-01 11:36:10 -0600236 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
237
Heinrich Schuchardte6e9a4f2020-02-27 18:28:00 +0100238 ide_outb(device, ATA_COMMAND, ATA_CMD_PACKET);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600239 udelay(50);
240
241 mask = ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR;
242 res = ATA_STAT_DRQ;
243 c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
244
245 if ((c & mask) != res) { /* DRQ must be 1, BSY 0 */
Simon Glass79543e62023-04-25 10:54:54 -0600246 printf("ATAPI_ISSUE: Error (no IRQ) before sending ccb dev %d status %#02x\n",
Simon Glasse9be1ee2016-05-01 11:36:10 -0600247 device, c);
Simon Glass79543e62023-04-25 10:54:54 -0600248 err = 0xff;
Simon Glasse9be1ee2016-05-01 11:36:10 -0600249 goto AI_OUT;
250 }
251
252 /* write command block */
253 ide_output_data_shorts(device, (unsigned short *)ccb, ccblen / 2);
254
255 /* ATAPI Command written wait for completition */
Simon Glass4d89f4b2023-04-25 10:54:27 -0600256 mdelay(5); /* device must set bsy */
Simon Glasse9be1ee2016-05-01 11:36:10 -0600257
258 mask = ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR;
259 /*
260 * if no data wait for DRQ = 0 BSY = 0
261 * if data wait for DRQ = 1 BSY = 0
262 */
263 res = 0;
264 if (buflen)
265 res = ATA_STAT_DRQ;
266 c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
267 if ((c & mask) != res) {
268 if (c & ATA_STAT_ERR) {
269 err = (ide_inb(device, ATA_ERROR_REG)) >> 4;
Simon Glass692bccb2023-04-25 10:54:53 -0600270 log_debug("1 returned sense key %x status %02x\n",
271 err, c);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600272 } else {
Simon Glass79543e62023-04-25 10:54:54 -0600273 printf("ATAPI_ISSUE: (no DRQ) after sending ccb (%x) status %#02x\n",
Simon Glasse9be1ee2016-05-01 11:36:10 -0600274 ccb[0], c);
Simon Glass79543e62023-04-25 10:54:54 -0600275 err = 0xff;
Simon Glasse9be1ee2016-05-01 11:36:10 -0600276 }
277 goto AI_OUT;
278 }
279 n = ide_inb(device, ATA_CYL_HIGH);
280 n <<= 8;
281 n += ide_inb(device, ATA_CYL_LOW);
282 if (n > buflen) {
283 printf("ERROR, transfer bytes %d requested only %d\n", n,
284 buflen);
285 err = 0xff;
286 goto AI_OUT;
287 }
Simon Glass79543e62023-04-25 10:54:54 -0600288 if (!n && buflen < 0) {
Simon Glasse9be1ee2016-05-01 11:36:10 -0600289 printf("ERROR, transfer bytes %d requested %d\n", n, buflen);
290 err = 0xff;
291 goto AI_OUT;
292 }
293 if (n != buflen) {
Simon Glass692bccb2023-04-25 10:54:53 -0600294 log_debug("WARNING, transfer bytes %d not equal with requested %d\n",
295 n, buflen);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600296 }
Simon Glass79543e62023-04-25 10:54:54 -0600297 if (n) { /* data transfer */
Simon Glass692bccb2023-04-25 10:54:53 -0600298 log_debug("ATAPI_ISSUE: %d Bytes to transfer\n", n);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600299 /* we transfer shorts */
300 n >>= 1;
301 /* ok now decide if it is an in or output */
Simon Glass79543e62023-04-25 10:54:54 -0600302 if (!(ide_inb(device, ATA_SECT_CNT) & 0x02)) {
Simon Glass692bccb2023-04-25 10:54:53 -0600303 log_debug("Write to device\n");
Simon Glasse9be1ee2016-05-01 11:36:10 -0600304 ide_output_data_shorts(device, (unsigned short *)buffer,
305 n);
306 } else {
Simon Glass692bccb2023-04-25 10:54:53 -0600307 log_debug("Read from device @ %p shorts %d\n", buffer,
308 n);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600309 ide_input_data_shorts(device, (unsigned short *)buffer,
310 n);
311 }
312 }
Simon Glass4d89f4b2023-04-25 10:54:27 -0600313 mdelay(5); /* seems that some CD ROMs need this... */
Simon Glasse9be1ee2016-05-01 11:36:10 -0600314 mask = ATA_STAT_BUSY | ATA_STAT_ERR;
315 res = 0;
316 c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
317 if ((c & ATA_STAT_ERR) == ATA_STAT_ERR) {
318 err = (ide_inb(device, ATA_ERROR_REG) >> 4);
Simon Glass692bccb2023-04-25 10:54:53 -0600319 log_debug("2 returned sense key %x status %x\n", err, c);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600320 } else {
321 err = 0;
322 }
323AI_OUT:
Simon Glasse9be1ee2016-05-01 11:36:10 -0600324 return err;
325}
326
327/*
328 * sending the command to atapi_issue. If an status other than good
329 * returns, an request_sense will be issued
330 */
331
332#define ATAPI_DRIVE_NOT_READY 100
333#define ATAPI_UNIT_ATTN 10
334
Simon Glass1b33fd82023-04-25 10:54:36 -0600335static unsigned char atapi_issue_autoreq(int device, unsigned char *ccb,
336 int ccblen,
337 unsigned char *buffer, int buflen)
Simon Glasse9be1ee2016-05-01 11:36:10 -0600338{
339 unsigned char sense_data[18], sense_ccb[12];
340 unsigned char res, key, asc, ascq;
341 int notready, unitattn;
342
343 unitattn = ATAPI_UNIT_ATTN;
344 notready = ATAPI_DRIVE_NOT_READY;
345
346retry:
347 res = atapi_issue(device, ccb, ccblen, buffer, buflen);
Simon Glass79543e62023-04-25 10:54:54 -0600348 if (!res)
Simon Glasse9be1ee2016-05-01 11:36:10 -0600349 return 0; /* Ok */
350
Simon Glass79543e62023-04-25 10:54:54 -0600351 if (res == 0xff)
352 return 0xff; /* error */
Simon Glasse9be1ee2016-05-01 11:36:10 -0600353
Simon Glass692bccb2023-04-25 10:54:53 -0600354 log_debug("(auto_req)atapi_issue returned sense key %x\n", res);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600355
356 memset(sense_ccb, 0, sizeof(sense_ccb));
357 memset(sense_data, 0, sizeof(sense_data));
358 sense_ccb[0] = ATAPI_CMD_REQ_SENSE;
359 sense_ccb[4] = 18; /* allocation Length */
360
361 res = atapi_issue(device, sense_ccb, 12, sense_data, 18);
Simon Glass79543e62023-04-25 10:54:54 -0600362 key = (sense_data[2] & 0xf);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600363 asc = (sense_data[12]);
364 ascq = (sense_data[13]);
365
Simon Glass692bccb2023-04-25 10:54:53 -0600366 log_debug("ATAPI_CMD_REQ_SENSE returned %x\n", res);
367 log_debug(" Sense page: %02X key %02X ASC %02X ASCQ %02X\n",
368 sense_data[0], key, asc, ascq);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600369
Simon Glass79543e62023-04-25 10:54:54 -0600370 if (!key)
Simon Glasse9be1ee2016-05-01 11:36:10 -0600371 return 0; /* ok device ready */
372
Simon Glass79543e62023-04-25 10:54:54 -0600373 if (key == 6 || asc == 0x29 || asc == 0x28) { /* Unit Attention */
Simon Glasse9be1ee2016-05-01 11:36:10 -0600374 if (unitattn-- > 0) {
Simon Glass4d89f4b2023-04-25 10:54:27 -0600375 mdelay(200);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600376 goto retry;
377 }
378 printf("Unit Attention, tried %d\n", ATAPI_UNIT_ATTN);
379 goto error;
380 }
Simon Glass79543e62023-04-25 10:54:54 -0600381 if (asc == 0x4 && ascq == 0x1) {
Simon Glasse9be1ee2016-05-01 11:36:10 -0600382 /* not ready, but will be ready soon */
383 if (notready-- > 0) {
Simon Glass4d89f4b2023-04-25 10:54:27 -0600384 mdelay(200);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600385 goto retry;
386 }
387 printf("Drive not ready, tried %d times\n",
388 ATAPI_DRIVE_NOT_READY);
389 goto error;
390 }
391 if (asc == 0x3a) {
Simon Glass692bccb2023-04-25 10:54:53 -0600392 log_debug("Media not present\n");
Simon Glasse9be1ee2016-05-01 11:36:10 -0600393 goto error;
394 }
395
396 printf("ERROR: Unknown Sense key %02X ASC %02X ASCQ %02X\n", key, asc,
397 ascq);
398error:
Simon Glass692bccb2023-04-25 10:54:53 -0600399 log_debug("ERROR Sense key %02X ASC %02X ASCQ %02X\n", key, asc, ascq);
Simon Glass79543e62023-04-25 10:54:54 -0600400 return 0xff;
Simon Glasse9be1ee2016-05-01 11:36:10 -0600401}
402
403/*
404 * atapi_read:
405 * we transfer only one block per command, since the multiple DRQ per
406 * command is not yet implemented
407 */
408#define ATAPI_READ_MAX_BYTES 2048 /* we read max 2kbytes */
409#define ATAPI_READ_BLOCK_SIZE 2048 /* assuming CD part */
410#define ATAPI_READ_MAX_BLOCK (ATAPI_READ_MAX_BYTES/ATAPI_READ_BLOCK_SIZE)
411
Simon Glass1b33fd82023-04-25 10:54:36 -0600412static ulong atapi_read(struct udevice *dev, lbaint_t blknr, lbaint_t blkcnt,
413 void *buffer)
Simon Glasse9be1ee2016-05-01 11:36:10 -0600414{
Simon Glassce99e292023-04-25 10:54:47 -0600415 struct blk_desc *desc = dev_get_uclass_plat(dev);
416 int device = desc->devnum;
Simon Glasse9be1ee2016-05-01 11:36:10 -0600417 ulong n = 0;
418 unsigned char ccb[12]; /* Command descriptor block */
419 ulong cnt;
420
Simon Glass692bccb2023-04-25 10:54:53 -0600421 log_debug("%d start " LBAF " blocks " LBAF " buffer at %lx\n", device,
422 blknr, blkcnt, (ulong)buffer);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600423
424 do {
425 if (blkcnt > ATAPI_READ_MAX_BLOCK)
426 cnt = ATAPI_READ_MAX_BLOCK;
427 else
428 cnt = blkcnt;
429
430 ccb[0] = ATAPI_CMD_READ_12;
431 ccb[1] = 0; /* reserved */
Simon Glass79543e62023-04-25 10:54:54 -0600432 ccb[2] = (unsigned char)(blknr >> 24) & 0xff; /* MSB Block */
433 ccb[3] = (unsigned char)(blknr >> 16) & 0xff; /* */
434 ccb[4] = (unsigned char)(blknr >> 8) & 0xff;
435 ccb[5] = (unsigned char)blknr & 0xff; /* LSB Block */
436 ccb[6] = (unsigned char)(cnt >> 24) & 0xff; /* MSB Block cnt */
437 ccb[7] = (unsigned char)(cnt >> 16) & 0xff;
438 ccb[8] = (unsigned char)(cnt >> 8) & 0xff;
439 ccb[9] = (unsigned char)cnt & 0xff; /* LSB Block */
Simon Glasse9be1ee2016-05-01 11:36:10 -0600440 ccb[10] = 0; /* reserved */
441 ccb[11] = 0; /* reserved */
442
443 if (atapi_issue_autoreq(device, ccb, 12,
444 (unsigned char *)buffer,
Simon Glass79543e62023-04-25 10:54:54 -0600445 cnt * ATAPI_READ_BLOCK_SIZE) == 0xff)
Simon Glasse9be1ee2016-05-01 11:36:10 -0600446 return n;
Simon Glasse9be1ee2016-05-01 11:36:10 -0600447 n += cnt;
448 blkcnt -= cnt;
449 blknr += cnt;
Simon Glass79543e62023-04-25 10:54:54 -0600450 buffer += cnt * ATAPI_READ_BLOCK_SIZE;
Simon Glasse9be1ee2016-05-01 11:36:10 -0600451 } while (blkcnt > 0);
452 return n;
453}
454
Simon Glassce99e292023-04-25 10:54:47 -0600455static void atapi_inquiry(struct blk_desc *desc)
Simon Glasse9be1ee2016-05-01 11:36:10 -0600456{
457 unsigned char ccb[12]; /* Command descriptor block */
458 unsigned char iobuf[64]; /* temp buf */
459 unsigned char c;
460 int device;
461
Simon Glassce99e292023-04-25 10:54:47 -0600462 device = desc->devnum;
463 desc->type = DEV_TYPE_UNKNOWN; /* not yet valid */
Simon Glasse9be1ee2016-05-01 11:36:10 -0600464
465 memset(ccb, 0, sizeof(ccb));
466 memset(iobuf, 0, sizeof(iobuf));
467
468 ccb[0] = ATAPI_CMD_INQUIRY;
469 ccb[4] = 40; /* allocation Legnth */
470 c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *)iobuf, 40);
471
Simon Glass692bccb2023-04-25 10:54:53 -0600472 log_debug("ATAPI_CMD_INQUIRY returned %x\n", c);
Simon Glass79543e62023-04-25 10:54:54 -0600473 if (c)
Simon Glasse9be1ee2016-05-01 11:36:10 -0600474 return;
475
476 /* copy device ident strings */
Simon Glassce99e292023-04-25 10:54:47 -0600477 ident_cpy((u8 *)desc->vendor, &iobuf[8], 8);
478 ident_cpy((u8 *)desc->product, &iobuf[16], 16);
479 ident_cpy((u8 *)desc->revision, &iobuf[32], 5);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600480
Simon Glassce99e292023-04-25 10:54:47 -0600481 desc->lun = 0;
482 desc->lba = 0;
483 desc->blksz = 0;
484 desc->log2blksz = LOG2_INVALID(typeof(desc->log2blksz));
485 desc->type = iobuf[0] & 0x1f;
Simon Glasse9be1ee2016-05-01 11:36:10 -0600486
Simon Glass79543e62023-04-25 10:54:54 -0600487 if (iobuf[1] & 0x80)
Simon Glassce99e292023-04-25 10:54:47 -0600488 desc->removable = 1;
Simon Glasse9be1ee2016-05-01 11:36:10 -0600489 else
Simon Glassce99e292023-04-25 10:54:47 -0600490 desc->removable = 0;
Simon Glasse9be1ee2016-05-01 11:36:10 -0600491
492 memset(ccb, 0, sizeof(ccb));
493 memset(iobuf, 0, sizeof(iobuf));
494 ccb[0] = ATAPI_CMD_START_STOP;
495 ccb[4] = 0x03; /* start */
496
497 c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *)iobuf, 0);
498
Simon Glass692bccb2023-04-25 10:54:53 -0600499 log_debug("ATAPI_CMD_START_STOP returned %x\n", c);
Simon Glass79543e62023-04-25 10:54:54 -0600500 if (c)
Simon Glasse9be1ee2016-05-01 11:36:10 -0600501 return;
502
503 memset(ccb, 0, sizeof(ccb));
504 memset(iobuf, 0, sizeof(iobuf));
505 c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *)iobuf, 0);
506
Simon Glass692bccb2023-04-25 10:54:53 -0600507 log_debug("ATAPI_CMD_UNIT_TEST_READY returned %x\n", c);
Simon Glass79543e62023-04-25 10:54:54 -0600508 if (c)
Simon Glasse9be1ee2016-05-01 11:36:10 -0600509 return;
510
511 memset(ccb, 0, sizeof(ccb));
512 memset(iobuf, 0, sizeof(iobuf));
513 ccb[0] = ATAPI_CMD_READ_CAP;
514 c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *)iobuf, 8);
Simon Glass692bccb2023-04-25 10:54:53 -0600515 log_debug("ATAPI_CMD_READ_CAP returned %x\n", c);
Simon Glass79543e62023-04-25 10:54:54 -0600516 if (c)
Simon Glasse9be1ee2016-05-01 11:36:10 -0600517 return;
518
Simon Glass692bccb2023-04-25 10:54:53 -0600519 log_debug("Read Cap: LBA %02X%02X%02X%02X blksize %02X%02X%02X%02X\n",
520 iobuf[0], iobuf[1], iobuf[2], iobuf[3],
521 iobuf[4], iobuf[5], iobuf[6], iobuf[7]);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600522
Simon Glassce99e292023-04-25 10:54:47 -0600523 desc->lba = (ulong)iobuf[0] << 24 | (ulong)iobuf[1] << 16 |
524 (ulong)iobuf[2] << 8 | (ulong)iobuf[3];
525 desc->blksz = (ulong)iobuf[4] << 24 | (ulong)iobuf[5] << 16 |
526 (ulong)iobuf[6] << 8 | (ulong)iobuf[7];
527 desc->log2blksz = LOG2(desc->blksz);
Simon Glass8b1b9432023-04-25 10:54:41 -0600528
Simon Glasse9be1ee2016-05-01 11:36:10 -0600529 /* ATAPI devices cannot use 48bit addressing (ATA/ATAPI v7) */
Simon Glassce99e292023-04-25 10:54:47 -0600530 desc->lba48 = false;
Simon Glasse9be1ee2016-05-01 11:36:10 -0600531}
532
Simon Glass038590a2023-04-25 10:54:48 -0600533/**
534 * ide_ident() - Identify an IDE device
535 *
536 * @device: Device number to use
537 * @desc: Block descriptor to fill in
538 * Returns: 0 if OK, -ENOENT if no device is found
539 */
540static int ide_ident(int device, struct blk_desc *desc)
Simon Glasse9be1ee2016-05-01 11:36:10 -0600541{
542 unsigned char c;
543 hd_driveid_t iop;
Simon Glass606e0542022-08-11 19:34:53 -0600544 bool is_atapi = false;
Simon Glass2a165952023-04-25 10:54:37 -0600545 int tries = 1;
Simon Glasse9be1ee2016-05-01 11:36:10 -0600546
Simon Glass96083112023-04-25 10:54:49 -0600547 memset(desc, '\0', sizeof(*desc));
Simon Glass038590a2023-04-25 10:54:48 -0600548 desc->devnum = device;
Simon Glass96083112023-04-25 10:54:49 -0600549 desc->type = DEV_TYPE_UNKNOWN;
550 desc->uclass_id = UCLASS_IDE;
551 desc->log2blksz = LOG2_INVALID(typeof(desc->log2blksz));
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 Glass6579bb02023-04-25 10:54:38 -0600557 if (IS_ENABLED(CONFIG_ATAPI))
558 tries = 2;
Simon Glasse9be1ee2016-05-01 11:36:10 -0600559
Simon Glass2a165952023-04-25 10:54:37 -0600560 while (tries) {
Simon Glasse9be1ee2016-05-01 11:36:10 -0600561 /* check signature */
Simon Glass6579bb02023-04-25 10:54:38 -0600562 if (IS_ENABLED(CONFIG_ATAPI) &&
563 ide_inb(device, ATA_SECT_CNT) == 0x01 &&
564 ide_inb(device, ATA_SECT_NUM) == 0x01 &&
565 ide_inb(device, ATA_CYL_LOW) == 0x14 &&
566 ide_inb(device, ATA_CYL_HIGH) == 0xeb) {
Simon Glasse9be1ee2016-05-01 11:36:10 -0600567 /* ATAPI Signature found */
Simon Glass606e0542022-08-11 19:34:53 -0600568 is_atapi = true;
Simon Glasse9be1ee2016-05-01 11:36:10 -0600569 /*
570 * Start Ident Command
571 */
Heinrich Schuchardte6e9a4f2020-02-27 18:28:00 +0100572 ide_outb(device, ATA_COMMAND, ATA_CMD_ID_ATAPI);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600573 /*
574 * Wait for completion - ATAPI devices need more time
575 * to become ready
576 */
577 c = ide_wait(device, ATAPI_TIME_OUT);
Simon Glass6579bb02023-04-25 10:54:38 -0600578 } else {
Simon Glasse9be1ee2016-05-01 11:36:10 -0600579 /*
580 * Start Ident Command
581 */
Heinrich Schuchardte6e9a4f2020-02-27 18:28:00 +0100582 ide_outb(device, ATA_COMMAND, ATA_CMD_ID_ATA);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600583
584 /*
585 * Wait for completion
586 */
587 c = ide_wait(device, IDE_TIME_OUT);
588 }
Simon Glasse9be1ee2016-05-01 11:36:10 -0600589
Simon Glass9367e612023-04-25 10:54:39 -0600590 if ((c & ATA_STAT_DRQ) &&
591 !(c & (ATA_STAT_FAULT | ATA_STAT_ERR))) {
Simon Glasse9be1ee2016-05-01 11:36:10 -0600592 break;
Simon Glass9367e612023-04-25 10:54:39 -0600593 } else if (IS_ENABLED(CONFIG_ATAPI)) {
594 /*
595 * Need to soft reset the device
596 * in case it's an ATAPI...
597 */
Simon Glass692bccb2023-04-25 10:54:53 -0600598 log_debug("Retrying...\n");
Simon Glass9367e612023-04-25 10:54:39 -0600599 ide_outb(device, ATA_DEV_HD,
600 ATA_LBA | ATA_DEVICE(device));
601 mdelay(100);
602 ide_outb(device, ATA_COMMAND, 0x08);
603 mdelay(500);
604 /* Select device */
605 ide_outb(device, ATA_DEV_HD,
606 ATA_LBA | ATA_DEVICE(device));
Simon Glass6579bb02023-04-25 10:54:38 -0600607 }
Simon Glass9367e612023-04-25 10:54:39 -0600608 tries--;
Simon Glass6579bb02023-04-25 10:54:38 -0600609 }
Simon Glasse9be1ee2016-05-01 11:36:10 -0600610
Simon Glass2a165952023-04-25 10:54:37 -0600611 if (!tries) /* Not found */
Simon Glass038590a2023-04-25 10:54:48 -0600612 return -ENOENT;
Simon Glasse9be1ee2016-05-01 11:36:10 -0600613
614 ide_input_swap_data(device, (ulong *)&iop, ATA_SECTORWORDS);
615
Simon Glassce99e292023-04-25 10:54:47 -0600616 ident_cpy((u8 *)desc->revision, iop.fw_rev, sizeof(desc->revision));
617 ident_cpy((u8 *)desc->vendor, iop.model, sizeof(desc->vendor));
618 ident_cpy((u8 *)desc->product, iop.serial_no, sizeof(desc->product));
Simon Glasse9be1ee2016-05-01 11:36:10 -0600619
Simon Glass79543e62023-04-25 10:54:54 -0600620 if (iop.config & 0x0080)
Simon Glassce99e292023-04-25 10:54:47 -0600621 desc->removable = 1;
Simon Glasse9be1ee2016-05-01 11:36:10 -0600622 else
Simon Glassce99e292023-04-25 10:54:47 -0600623 desc->removable = 0;
Simon Glasse9be1ee2016-05-01 11:36:10 -0600624
Simon Glassc94a3312023-04-25 10:54:40 -0600625 if (IS_ENABLED(CONFIG_ATAPI) && is_atapi) {
Simon Glassce99e292023-04-25 10:54:47 -0600626 desc->atapi = true;
627 atapi_inquiry(desc);
Simon Glass038590a2023-04-25 10:54:48 -0600628 return 0;
Simon Glasse9be1ee2016-05-01 11:36:10 -0600629 }
Simon Glasse9be1ee2016-05-01 11:36:10 -0600630
Reinoud Zandijk0a527fd2021-02-24 17:44:42 +0100631 iop.lba_capacity[0] = be16_to_cpu(iop.lba_capacity[0]);
632 iop.lba_capacity[1] = be16_to_cpu(iop.lba_capacity[1]);
Simon Glassce99e292023-04-25 10:54:47 -0600633 desc->lba = (ulong)iop.lba_capacity[0] |
634 (ulong)iop.lba_capacity[1] << 16;
Simon Glasse9be1ee2016-05-01 11:36:10 -0600635
Simon Glass8b1b9432023-04-25 10:54:41 -0600636 if (IS_ENABLED(CONFIG_LBA48) && (iop.command_set_2 & 0x0400)) {
637 /* LBA 48 support */
Simon Glassce99e292023-04-25 10:54:47 -0600638 desc->lba48 = true;
Reinoud Zandijk0a527fd2021-02-24 17:44:42 +0100639 for (int i = 0; i < 4; i++)
640 iop.lba48_capacity[i] = be16_to_cpu(iop.lba48_capacity[i]);
Simon Glassce99e292023-04-25 10:54:47 -0600641 desc->lba = (unsigned long long)iop.lba48_capacity[0] |
642 (unsigned long long)iop.lba48_capacity[1] << 16 |
643 (unsigned long long)iop.lba48_capacity[2] << 32 |
644 (unsigned long long)iop.lba48_capacity[3] << 48;
Simon Glasse9be1ee2016-05-01 11:36:10 -0600645 } else {
Simon Glassce99e292023-04-25 10:54:47 -0600646 desc->lba48 = false;
Simon Glasse9be1ee2016-05-01 11:36:10 -0600647 }
Simon Glass8b1b9432023-04-25 10:54:41 -0600648
Simon Glasse9be1ee2016-05-01 11:36:10 -0600649 /* assuming HD */
Simon Glassce99e292023-04-25 10:54:47 -0600650 desc->type = DEV_TYPE_HARDDISK;
651 desc->blksz = ATA_BLOCKSIZE;
652 desc->log2blksz = LOG2(desc->blksz);
653 desc->lun = 0; /* just to fill something in... */
Simon Glasse9be1ee2016-05-01 11:36:10 -0600654
655#if 0 /* only used to test the powersaving mode,
656 * if enabled, the drive goes after 5 sec
657 * in standby mode */
658 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
659 c = ide_wait(device, IDE_TIME_OUT);
660 ide_outb(device, ATA_SECT_CNT, 1);
661 ide_outb(device, ATA_LBA_LOW, 0);
662 ide_outb(device, ATA_LBA_MID, 0);
663 ide_outb(device, ATA_LBA_HIGH, 0);
664 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
665 ide_outb(device, ATA_COMMAND, 0xe3);
666 udelay(50);
667 c = ide_wait(device, IDE_TIME_OUT); /* can't take over 500 ms */
668#endif
Simon Glass038590a2023-04-25 10:54:48 -0600669
670 return 0;
Simon Glasse9be1ee2016-05-01 11:36:10 -0600671}
672
Simon Glassb6483ea2023-04-25 10:54:42 -0600673/**
674 * ide_init_one() - Init one IDE device
675 *
676 * @bus: Bus to use
677 * Return: 0 iuf OK, -EIO if not available, -ETIMEDOUT if timed out
678 */
679static int ide_init_one(int bus)
680{
681 int dev = bus * CONFIG_SYS_IDE_MAXDEVICE / CONFIG_SYS_IDE_MAXBUS;
682 int i;
683 u8 c;
684
685 printf("Bus %d: ", bus);
686
687 /* Select device */
688 mdelay(100);
689 ide_outb(dev, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(dev));
690 mdelay(100);
691 i = 0;
692 do {
693 mdelay(10);
694
695 c = ide_inb(dev, ATA_STATUS);
696 i++;
697 if (i > (ATA_RESET_TIME * 100)) {
698 puts("** Timeout **\n");
699 return -ETIMEDOUT;
700 }
701 if (i >= 100 && !(i % 100))
702 putc('.');
703 } while (c & ATA_STAT_BUSY);
704
705 if (c & (ATA_STAT_BUSY | ATA_STAT_FAULT)) {
706 puts("not available ");
Simon Glass79543e62023-04-25 10:54:54 -0600707 log_debug("Status = %#02X ", c);
Simon Glassb6483ea2023-04-25 10:54:42 -0600708 return -EIO;
709 } else if (IS_ENABLED(CONFIG_ATAPI) && !(c & ATA_STAT_READY)) {
710 /* ATAPI Devices do not set DRDY */
711 puts("not available ");
Simon Glass79543e62023-04-25 10:54:54 -0600712 log_debug("Status = %#02X ", c);
Simon Glassb6483ea2023-04-25 10:54:42 -0600713 return -EIO;
714 }
715 puts("OK ");
716
717 return 0;
718}
719
Simon Glassf8e87e72023-04-25 10:54:33 -0600720static void ide_output_data(int dev, const ulong *sect_buf, int words)
Simon Glasse9be1ee2016-05-01 11:36:10 -0600721{
Reinoud Zandijk0a527fd2021-02-24 17:44:42 +0100722 uintptr_t paddr = (ATA_CURR_BASE(dev) + ATA_DATA_REG);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600723 ushort *dbuf;
Simon Glasse9be1ee2016-05-01 11:36:10 -0600724
Simon Glasse9be1ee2016-05-01 11:36:10 -0600725 dbuf = (ushort *)sect_buf;
Simon Glasse9be1ee2016-05-01 11:36:10 -0600726 while (words--) {
727 EIEIO;
Reinoud Zandijk0a527fd2021-02-24 17:44:42 +0100728 outw(cpu_to_le16(*dbuf++), paddr);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600729 EIEIO;
Reinoud Zandijk0a527fd2021-02-24 17:44:42 +0100730 outw(cpu_to_le16(*dbuf++), paddr);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600731 }
732}
Reinoud Zandijk0a527fd2021-02-24 17:44:42 +0100733
Simon Glassf8e87e72023-04-25 10:54:33 -0600734static void ide_input_data(int dev, ulong *sect_buf, int words)
Simon Glasse9be1ee2016-05-01 11:36:10 -0600735{
Reinoud Zandijk0a527fd2021-02-24 17:44:42 +0100736 uintptr_t paddr = (ATA_CURR_BASE(dev) + ATA_DATA_REG);
737 ushort *dbuf;
Simon Glasse9be1ee2016-05-01 11:36:10 -0600738
Reinoud Zandijk0a527fd2021-02-24 17:44:42 +0100739 dbuf = (ushort *)sect_buf;
740
Simon Glass692bccb2023-04-25 10:54:53 -0600741 log_debug("in input data base for read is %p\n", (void *)paddr);
Reinoud Zandijk0a527fd2021-02-24 17:44:42 +0100742
743 while (words--) {
744 EIEIO;
745 *dbuf++ = le16_to_cpu(inw(paddr));
746 EIEIO;
747 *dbuf++ = le16_to_cpu(inw(paddr));
748 }
Reinoud Zandijk0a527fd2021-02-24 17:44:42 +0100749}
Simon Glasse9be1ee2016-05-01 11:36:10 -0600750
Simon Glass1b33fd82023-04-25 10:54:36 -0600751static ulong ide_read(struct udevice *dev, lbaint_t blknr, lbaint_t blkcnt,
752 void *buffer)
Simon Glasse9be1ee2016-05-01 11:36:10 -0600753{
Simon Glassce99e292023-04-25 10:54:47 -0600754 struct blk_desc *desc = dev_get_uclass_plat(dev);
755 int device = desc->devnum;
Simon Glasse9be1ee2016-05-01 11:36:10 -0600756 ulong n = 0;
757 unsigned char c;
758 unsigned char pwrsave = 0; /* power save */
Simon Glass8b1b9432023-04-25 10:54:41 -0600759 bool lba48 = false;
Simon Glasse9be1ee2016-05-01 11:36:10 -0600760
Simon Glass8b1b9432023-04-25 10:54:41 -0600761 if (IS_ENABLED(CONFIG_LBA48) && (blknr & 0x0000fffff0000000ULL)) {
Simon Glasse9be1ee2016-05-01 11:36:10 -0600762 /* more than 28 bits used, use 48bit mode */
Simon Glass8b1b9432023-04-25 10:54:41 -0600763 lba48 = true;
Simon Glasse9be1ee2016-05-01 11:36:10 -0600764 }
Simon Glass8b1b9432023-04-25 10:54:41 -0600765
Simon Glass692bccb2023-04-25 10:54:53 -0600766 log_debug("dev %d start " LBAF ", blocks " LBAF " buffer at %lx\n",
767 device, blknr, blkcnt, (ulong)buffer);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600768
Simon Glasse9be1ee2016-05-01 11:36:10 -0600769 /* Select device
770 */
771 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
772 c = ide_wait(device, IDE_TIME_OUT);
773
774 if (c & ATA_STAT_BUSY) {
775 printf("IDE read: device %d not ready\n", device);
776 goto IDE_READ_E;
777 }
778
779 /* first check if the drive is in Powersaving mode, if yes,
780 * increase the timeout value */
Heinrich Schuchardte6e9a4f2020-02-27 18:28:00 +0100781 ide_outb(device, ATA_COMMAND, ATA_CMD_CHK_POWER);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600782 udelay(50);
783
784 c = ide_wait(device, IDE_TIME_OUT); /* can't take over 500 ms */
785
786 if (c & ATA_STAT_BUSY) {
787 printf("IDE read: device %d not ready\n", device);
788 goto IDE_READ_E;
789 }
790 if ((c & ATA_STAT_ERR) == ATA_STAT_ERR) {
Simon Glass692bccb2023-04-25 10:54:53 -0600791 printf("No Powersaving mode %x\n", c);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600792 } else {
793 c = ide_inb(device, ATA_SECT_CNT);
Simon Glass692bccb2023-04-25 10:54:53 -0600794 log_debug("Powersaving %02X\n", c);
Simon Glass79543e62023-04-25 10:54:54 -0600795 if (!c)
Simon Glasse9be1ee2016-05-01 11:36:10 -0600796 pwrsave = 1;
797 }
798
799
800 while (blkcnt-- > 0) {
801 c = ide_wait(device, IDE_TIME_OUT);
802
803 if (c & ATA_STAT_BUSY) {
804 printf("IDE read: device %d not ready\n", device);
805 break;
806 }
Simon Glass8b1b9432023-04-25 10:54:41 -0600807 if (IS_ENABLED(CONFIG_LBA48) && lba48) {
Simon Glasse9be1ee2016-05-01 11:36:10 -0600808 /* write high bits */
809 ide_outb(device, ATA_SECT_CNT, 0);
Simon Glass79543e62023-04-25 10:54:54 -0600810 ide_outb(device, ATA_LBA_LOW, (blknr >> 24) & 0xff);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600811#ifdef CONFIG_SYS_64BIT_LBA
Simon Glass79543e62023-04-25 10:54:54 -0600812 ide_outb(device, ATA_LBA_MID, (blknr >> 32) & 0xff);
813 ide_outb(device, ATA_LBA_HIGH, (blknr >> 40) & 0xff);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600814#else
815 ide_outb(device, ATA_LBA_MID, 0);
816 ide_outb(device, ATA_LBA_HIGH, 0);
817#endif
818 }
Simon Glasse9be1ee2016-05-01 11:36:10 -0600819 ide_outb(device, ATA_SECT_CNT, 1);
Simon Glass79543e62023-04-25 10:54:54 -0600820 ide_outb(device, ATA_LBA_LOW, (blknr >> 0) & 0xff);
821 ide_outb(device, ATA_LBA_MID, (blknr >> 8) & 0xff);
822 ide_outb(device, ATA_LBA_HIGH, (blknr >> 16) & 0xff);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600823
Simon Glass8b1b9432023-04-25 10:54:41 -0600824 if (IS_ENABLED(CONFIG_LBA48) && lba48) {
Simon Glasse9be1ee2016-05-01 11:36:10 -0600825 ide_outb(device, ATA_DEV_HD,
826 ATA_LBA | ATA_DEVICE(device));
Reinoud Zandijk0a527fd2021-02-24 17:44:42 +0100827 ide_outb(device, ATA_COMMAND, ATA_CMD_PIO_READ_EXT);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600828
Simon Glass8b1b9432023-04-25 10:54:41 -0600829 } else {
Simon Glasse9be1ee2016-05-01 11:36:10 -0600830 ide_outb(device, ATA_DEV_HD, ATA_LBA |
Simon Glass79543e62023-04-25 10:54:54 -0600831 ATA_DEVICE(device) | ((blknr >> 24) & 0xf));
Reinoud Zandijk0a527fd2021-02-24 17:44:42 +0100832 ide_outb(device, ATA_COMMAND, ATA_CMD_PIO_READ);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600833 }
834
835 udelay(50);
836
837 if (pwrsave) {
838 /* may take up to 4 sec */
839 c = ide_wait(device, IDE_SPIN_UP_TIME_OUT);
840 pwrsave = 0;
841 } else {
842 /* can't take over 500 ms */
843 c = ide_wait(device, IDE_TIME_OUT);
844 }
845
846 if ((c & (ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR)) !=
847 ATA_STAT_DRQ) {
848 printf("Error (no IRQ) dev %d blk " LBAF
849 ": status %#02x\n", device, blknr, c);
850 break;
851 }
852
853 ide_input_data(device, buffer, ATA_SECTORWORDS);
854 (void) ide_inb(device, ATA_STATUS); /* clear IRQ */
855
856 ++n;
857 ++blknr;
858 buffer += ATA_BLOCKSIZE;
859 }
860IDE_READ_E:
Simon Glasse9be1ee2016-05-01 11:36:10 -0600861 return n;
862}
863
Simon Glass1b33fd82023-04-25 10:54:36 -0600864static ulong ide_write(struct udevice *dev, lbaint_t blknr, lbaint_t blkcnt,
865 const void *buffer)
Simon Glasse9be1ee2016-05-01 11:36:10 -0600866{
Simon Glassce99e292023-04-25 10:54:47 -0600867 struct blk_desc *desc = dev_get_uclass_plat(dev);
868 int device = desc->devnum;
Simon Glasse9be1ee2016-05-01 11:36:10 -0600869 ulong n = 0;
870 unsigned char c;
Simon Glass8b1b9432023-04-25 10:54:41 -0600871 bool lba48 = false;
Simon Glasse9be1ee2016-05-01 11:36:10 -0600872
Simon Glass8b1b9432023-04-25 10:54:41 -0600873 if (IS_ENABLED(CONFIG_LBA48) && (blknr & 0x0000fffff0000000ULL)) {
Simon Glasse9be1ee2016-05-01 11:36:10 -0600874 /* more than 28 bits used, use 48bit mode */
Simon Glass8b1b9432023-04-25 10:54:41 -0600875 lba48 = true;
Simon Glasse9be1ee2016-05-01 11:36:10 -0600876 }
Simon Glasse9be1ee2016-05-01 11:36:10 -0600877
Simon Glasse9be1ee2016-05-01 11:36:10 -0600878 /* Select device
879 */
880 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
881
882 while (blkcnt-- > 0) {
883 c = ide_wait(device, IDE_TIME_OUT);
884
885 if (c & ATA_STAT_BUSY) {
886 printf("IDE read: device %d not ready\n", device);
887 goto WR_OUT;
888 }
Simon Glass8b1b9432023-04-25 10:54:41 -0600889 if (IS_ENABLED(CONFIG_LBA48) && lba48) {
Simon Glasse9be1ee2016-05-01 11:36:10 -0600890 /* write high bits */
891 ide_outb(device, ATA_SECT_CNT, 0);
Simon Glass79543e62023-04-25 10:54:54 -0600892 ide_outb(device, ATA_LBA_LOW, (blknr >> 24) & 0xff);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600893#ifdef CONFIG_SYS_64BIT_LBA
Simon Glass79543e62023-04-25 10:54:54 -0600894 ide_outb(device, ATA_LBA_MID, (blknr >> 32) & 0xff);
895 ide_outb(device, ATA_LBA_HIGH, (blknr >> 40) & 0xff);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600896#else
897 ide_outb(device, ATA_LBA_MID, 0);
898 ide_outb(device, ATA_LBA_HIGH, 0);
899#endif
900 }
Simon Glasse9be1ee2016-05-01 11:36:10 -0600901 ide_outb(device, ATA_SECT_CNT, 1);
Simon Glass79543e62023-04-25 10:54:54 -0600902 ide_outb(device, ATA_LBA_LOW, (blknr >> 0) & 0xff);
903 ide_outb(device, ATA_LBA_MID, (blknr >> 8) & 0xff);
904 ide_outb(device, ATA_LBA_HIGH, (blknr >> 16) & 0xff);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600905
Simon Glass8b1b9432023-04-25 10:54:41 -0600906 if (IS_ENABLED(CONFIG_LBA48) && lba48) {
Simon Glasse9be1ee2016-05-01 11:36:10 -0600907 ide_outb(device, ATA_DEV_HD,
908 ATA_LBA | ATA_DEVICE(device));
Reinoud Zandijk0a527fd2021-02-24 17:44:42 +0100909 ide_outb(device, ATA_COMMAND, ATA_CMD_PIO_WRITE_EXT);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600910
Simon Glass8b1b9432023-04-25 10:54:41 -0600911 } else {
Simon Glasse9be1ee2016-05-01 11:36:10 -0600912 ide_outb(device, ATA_DEV_HD, ATA_LBA |
Simon Glass79543e62023-04-25 10:54:54 -0600913 ATA_DEVICE(device) | ((blknr >> 24) & 0xf));
Reinoud Zandijk0a527fd2021-02-24 17:44:42 +0100914 ide_outb(device, ATA_COMMAND, ATA_CMD_PIO_WRITE);
Simon Glasse9be1ee2016-05-01 11:36:10 -0600915 }
916
917 udelay(50);
918
919 /* can't take over 500 ms */
920 c = ide_wait(device, IDE_TIME_OUT);
921
922 if ((c & (ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR)) !=
923 ATA_STAT_DRQ) {
924 printf("Error (no IRQ) dev %d blk " LBAF
925 ": status %#02x\n", device, blknr, c);
926 goto WR_OUT;
927 }
928
929 ide_output_data(device, buffer, ATA_SECTORWORDS);
930 c = ide_inb(device, ATA_STATUS); /* clear IRQ */
931 ++n;
932 ++blknr;
933 buffer += ATA_BLOCKSIZE;
934 }
935WR_OUT:
Simon Glasse9be1ee2016-05-01 11:36:10 -0600936 return n;
937}
938
Simon Glass646deed2023-04-25 10:54:35 -0600939ulong ide_or_atapi_read(struct udevice *dev, lbaint_t blknr, lbaint_t blkcnt,
940 void *buffer)
941{
942 struct blk_desc *desc = dev_get_uclass_plat(dev);
943
944 if (IS_ENABLED(CONFIG_ATAPI) && desc->atapi)
945 return atapi_read(dev, blknr, blkcnt, buffer);
946
947 return ide_read(dev, blknr, blkcnt, buffer);
948}
949
Simon Glass145df842016-05-01 11:36:22 -0600950static const struct blk_ops ide_blk_ops = {
Simon Glass646deed2023-04-25 10:54:35 -0600951 .read = ide_or_atapi_read,
Simon Glass145df842016-05-01 11:36:22 -0600952 .write = ide_write,
953};
954
955U_BOOT_DRIVER(ide_blk) = {
956 .name = "ide_blk",
957 .id = UCLASS_BLK,
958 .ops = &ide_blk_ops,
Bin Meng68e6f222017-09-10 05:12:51 -0700959};
960
Simon Glass0d77f8f2023-01-17 10:47:46 -0700961static int ide_bootdev_bind(struct udevice *dev)
962{
963 struct bootdev_uc_plat *ucp = dev_get_uclass_plat(dev);
964
Simon Glasseacc2612023-01-17 10:48:08 -0700965 ucp->prio = BOOTDEVP_5_SCAN_SLOW;
Simon Glass0d77f8f2023-01-17 10:47:46 -0700966
967 return 0;
968}
969
970static int ide_bootdev_hunt(struct bootdev_hunter *info, bool show)
971{
Simon Glass80778f52023-04-25 10:54:30 -0600972 struct udevice *dev;
973
974 uclass_first_device(UCLASS_IDE, &dev);
Simon Glass0d77f8f2023-01-17 10:47:46 -0700975
976 return 0;
977}
978
979struct bootdev_ops ide_bootdev_ops = {
980};
981
982static const struct udevice_id ide_bootdev_ids[] = {
983 { .compatible = "u-boot,bootdev-ide" },
984 { }
985};
986
987U_BOOT_DRIVER(ide_bootdev) = {
988 .name = "ide_bootdev",
989 .id = UCLASS_BOOTDEV,
990 .ops = &ide_bootdev_ops,
991 .bind = ide_bootdev_bind,
992 .of_match = ide_bootdev_ids,
993};
994
995BOOTDEV_HUNTER(ide_bootdev_hunter) = {
Simon Glasseacc2612023-01-17 10:48:08 -0700996 .prio = BOOTDEVP_5_SCAN_SLOW,
Simon Glass0d77f8f2023-01-17 10:47:46 -0700997 .uclass = UCLASS_IDE,
998 .hunt = ide_bootdev_hunt,
999 .drv = DM_DRIVER_REF(ide_bootdev),
1000};
1001
Bin Meng68e6f222017-09-10 05:12:51 -07001002static int ide_probe(struct udevice *udev)
1003{
Simon Glass708404c2023-04-25 10:54:45 -06001004 bool bus_ok[CONFIG_SYS_IDE_MAXBUS];
1005 int i, bus;
Bin Meng68e6f222017-09-10 05:12:51 -07001006
Simon Glass708404c2023-04-25 10:54:45 -06001007 schedule();
1008
1009 /* ATAPI Drives seems to need a proper IDE Reset */
1010 ide_reset();
1011
1012 /*
1013 * Wait for IDE to get ready.
1014 * According to spec, this can take up to 31 seconds!
1015 */
1016 for (bus = 0; bus < CONFIG_SYS_IDE_MAXBUS; ++bus) {
1017 bus_ok[bus] = !ide_init_one(bus);
1018 schedule();
1019 }
1020
1021 putc('\n');
1022
Simon Glassf0af25a2023-04-25 10:54:46 -06001023 schedule();
1024
1025 for (i = 0; i < CONFIG_SYS_IDE_MAXDEVICE; i++) {
Simon Glassd7d57432023-04-25 10:54:50 -06001026 struct blk_desc *desc, pdesc;
Simon Glass038590a2023-04-25 10:54:48 -06001027 struct udevice *blk;
Simon Glass038590a2023-04-25 10:54:48 -06001028 char name[20];
Simon Glass038590a2023-04-25 10:54:48 -06001029 int ret;
1030
Simon Glassf0af25a2023-04-25 10:54:46 -06001031 if (!bus_ok[IDE_BUS(i)])
1032 continue;
1033
Simon Glassd7d57432023-04-25 10:54:50 -06001034 ret = ide_ident(i, &pdesc);
1035 dev_print(&pdesc);
Simon Glass80778f52023-04-25 10:54:30 -06001036
Simon Glass038590a2023-04-25 10:54:48 -06001037 if (ret)
1038 continue;
Simon Glassdb89e722023-04-25 10:54:44 -06001039
Simon Glass038590a2023-04-25 10:54:48 -06001040 sprintf(name, "blk#%d", i);
Bin Meng68e6f222017-09-10 05:12:51 -07001041
Simon Glass038590a2023-04-25 10:54:48 -06001042 /*
1043 * With CDROM, if there is no CD inserted, blksz will
1044 * be zero, don't bother to create IDE block device.
1045 */
Simon Glass49aa7782023-04-25 10:54:51 -06001046 if (!pdesc.blksz)
Simon Glass038590a2023-04-25 10:54:48 -06001047 continue;
1048 ret = blk_create_devicef(udev, "ide_blk", name, UCLASS_IDE, i,
Simon Glass49aa7782023-04-25 10:54:51 -06001049 pdesc.blksz, pdesc.lba, &blk);
Simon Glass038590a2023-04-25 10:54:48 -06001050 if (ret)
1051 return ret;
AKASHI Takahiro4c73b032022-03-08 20:36:44 +09001052
Simon Glass038590a2023-04-25 10:54:48 -06001053 ret = blk_probe_or_unbind(blk);
1054 if (ret)
1055 return ret;
Simon Glass0d77f8f2023-01-17 10:47:46 -07001056
Simon Glass038590a2023-04-25 10:54:48 -06001057 /* fill in device vendor/product/rev strings */
1058 desc = dev_get_uclass_plat(blk);
Simon Glassd7d57432023-04-25 10:54:50 -06001059 strlcpy(desc->vendor, pdesc.vendor, BLK_VEN_SIZE);
1060 strlcpy(desc->product, pdesc.product, BLK_PRD_SIZE);
1061 strlcpy(desc->revision, pdesc.revision, BLK_REV_SIZE);
1062 desc->removable = pdesc.removable;
1063 desc->atapi = pdesc.atapi;
1064 desc->lba48 = pdesc.lba48;
1065 desc->type = pdesc.type;
Simon Glass708404c2023-04-25 10:54:45 -06001066
Simon Glass038590a2023-04-25 10:54:48 -06001067 ret = bootdev_setup_for_dev(udev, "ide_bootdev");
1068 if (ret)
1069 return log_msg_ret("bootdev", ret);
Bin Meng68e6f222017-09-10 05:12:51 -07001070 }
1071
1072 return 0;
1073}
1074
1075U_BOOT_DRIVER(ide) = {
1076 .name = "ide",
1077 .id = UCLASS_IDE,
1078 .probe = ide_probe,
1079};
1080
1081struct pci_device_id ide_supported[] = {
1082 { PCI_DEVICE_CLASS(PCI_CLASS_STORAGE_IDE << 8, 0xffff00) },
1083 { }
1084};
1085
1086U_BOOT_PCI_DEVICE(ide, ide_supported);
1087
1088UCLASS_DRIVER(ide) = {
1089 .name = "ide",
1090 .id = UCLASS_IDE,
Simon Glass145df842016-05-01 11:36:22 -06001091};