blob: db26f4320dc8d76a4b69d5f5a0a1c1654fbf18c4 [file] [log] [blame]
wdenkc6097192002-11-03 00:24:07 +00001/*
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00002 * (C) Copyright 2000-2011
wdenkc6097192002-11-03 00:24:07 +00003 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02005 * SPDX-License-Identifier: GPL-2.0+
wdenkc6097192002-11-03 00:24:07 +00006 */
7
8/*
9 * IDE support
10 */
Albert Aribaud45027552010-08-08 05:17:06 +053011
wdenkc6097192002-11-03 00:24:07 +000012#include <common.h>
Simon Glass2ee8ada2016-02-29 15:25:52 -070013#include <blk.h>
wdenkc6097192002-11-03 00:24:07 +000014#include <config.h>
15#include <watchdog.h>
16#include <command.h>
17#include <image.h>
18#include <asm/byteorder.h>
Heiko Schocher2559e0f2007-08-28 17:39:14 +020019#include <asm/io.h>
Grant Likelyffc2dd72007-02-20 09:04:34 +010020
wdenkc6097192002-11-03 00:24:07 +000021#if defined(CONFIG_IDE_8xx_DIRECT) || defined(CONFIG_IDE_PCMCIA)
22# include <pcmcia.h>
23#endif
Grant Likelyffc2dd72007-02-20 09:04:34 +010024
wdenkc6097192002-11-03 00:24:07 +000025#include <ide.h>
26#include <ata.h>
Grant Likelyffc2dd72007-02-20 09:04:34 +010027
wdenkc6097192002-11-03 00:24:07 +000028#ifdef CONFIG_STATUS_LED
29# include <status_led.h>
30#endif
Grant Likelyffc2dd72007-02-20 09:04:34 +010031
wdenk20c98a62004-04-23 20:32:05 +000032#ifdef __PPC__
33# define EIEIO __asm__ volatile ("eieio")
wdenkf1276d42005-02-03 23:00:49 +000034# define SYNC __asm__ volatile ("sync")
wdenk20c98a62004-04-23 20:32:05 +000035#else
36# define EIEIO /* nothing */
wdenkf1276d42005-02-03 23:00:49 +000037# define SYNC /* nothing */
wdenk20c98a62004-04-23 20:32:05 +000038#endif
wdenkc6097192002-11-03 00:24:07 +000039
wdenkc6097192002-11-03 00:24:07 +000040/* ------------------------------------------------------------------------- */
41
42/* Current I/O Device */
43static int curr_device = -1;
44
45/* Current offset for IDE0 / IDE1 bus access */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020046ulong ide_bus_offset[CONFIG_SYS_IDE_MAXBUS] = {
47#if defined(CONFIG_SYS_ATA_IDE0_OFFSET)
48 CONFIG_SYS_ATA_IDE0_OFFSET,
wdenkc6097192002-11-03 00:24:07 +000049#endif
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020050#if defined(CONFIG_SYS_ATA_IDE1_OFFSET) && (CONFIG_SYS_IDE_MAXBUS > 1)
51 CONFIG_SYS_ATA_IDE1_OFFSET,
wdenkc6097192002-11-03 00:24:07 +000052#endif
53};
54
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020055static int ide_bus_ok[CONFIG_SYS_IDE_MAXBUS];
wdenkc6097192002-11-03 00:24:07 +000056
Simon Glasse3394752016-02-29 15:25:34 -070057struct blk_desc ide_dev_desc[CONFIG_SYS_IDE_MAXDEVICE];
wdenkc6097192002-11-03 00:24:07 +000058/* ------------------------------------------------------------------------- */
59
wdenkc6097192002-11-03 00:24:07 +000060#define IDE_TIME_OUT 2000 /* 2 sec timeout */
61
62#define ATAPI_TIME_OUT 7000 /* 7 sec timeout (5 sec seems to work...) */
63
64#define IDE_SPIN_UP_TIME_OUT 5000 /* 5 sec spin-up timeout */
65
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020066#ifndef CONFIG_SYS_ATA_PORT_ADDR
67#define CONFIG_SYS_ATA_PORT_ADDR(port) (port)
Heiko Schocher633e03a2007-06-22 19:11:54 +020068#endif
wdenkc6097192002-11-03 00:24:07 +000069
Simon Glass43a464a2016-05-01 11:36:00 -060070#ifndef CONFIG_IDE_LED /* define LED macros, they are not used anyways */
71# define DEVICE_LED(x) 0
72# define LED_IDE1 1
73# define LED_IDE2 2
wdenkc6097192002-11-03 00:24:07 +000074#endif
75
Simon Glass43a464a2016-05-01 11:36:00 -060076#ifdef CONFIG_IDE_RESET
77extern void ide_set_reset(int idereset);
wdenkc6097192002-11-03 00:24:07 +000078
Simon Glass43a464a2016-05-01 11:36:00 -060079static void ide_reset(void)
wdenkc6097192002-11-03 00:24:07 +000080{
Simon Glass43a464a2016-05-01 11:36:00 -060081 int i;
wdenkc6097192002-11-03 00:24:07 +000082
Simon Glass43a464a2016-05-01 11:36:00 -060083 curr_device = -1;
84 for (i = 0; i < CONFIG_SYS_IDE_MAXBUS; ++i)
85 ide_bus_ok[i] = 0;
86 for (i = 0; i < CONFIG_SYS_IDE_MAXDEVICE; ++i)
87 ide_dev_desc[i].type = DEV_TYPE_UNKNOWN;
wdenkc6097192002-11-03 00:24:07 +000088
Simon Glass43a464a2016-05-01 11:36:00 -060089 ide_set_reset(1); /* assert reset */
wdenkc6097192002-11-03 00:24:07 +000090
Simon Glass43a464a2016-05-01 11:36:00 -060091 /* the reset signal shall be asserted for et least 25 us */
92 udelay(25);
wdenkc6097192002-11-03 00:24:07 +000093
Simon Glass43a464a2016-05-01 11:36:00 -060094 WATCHDOG_RESET();
wdenkc6097192002-11-03 00:24:07 +000095
Simon Glass43a464a2016-05-01 11:36:00 -060096 /* de-assert RESET signal */
97 ide_set_reset(0);
wdenkc6097192002-11-03 00:24:07 +000098
Simon Glass43a464a2016-05-01 11:36:00 -060099 /* wait 250 ms */
100 for (i = 0; i < 250; ++i)
101 udelay(1000);
102}
103#else
104#define ide_reset() /* dummy */
105#endif /* CONFIG_IDE_RESET */
wdenkc6097192002-11-03 00:24:07 +0000106
Simon Glass43a464a2016-05-01 11:36:00 -0600107/*
108 * Wait until Busy bit is off, or timeout (in ms)
109 * Return last status
110 */
111static uchar ide_wait(int dev, ulong t)
112{
113 ulong delay = 10 * t; /* poll every 100 us */
114 uchar c;
wdenkc6097192002-11-03 00:24:07 +0000115
Simon Glass43a464a2016-05-01 11:36:00 -0600116 while ((c = ide_inb(dev, ATA_STATUS)) & ATA_STAT_BUSY) {
117 udelay(100);
118 if (delay-- == 0)
119 break;
120 }
121 return c;
122}
wdenkc6097192002-11-03 00:24:07 +0000123
Simon Glass43a464a2016-05-01 11:36:00 -0600124/*
125 * copy src to dest, skipping leading and trailing blanks and null
126 * terminate the string
127 * "len" is the size of available memory including the terminating '\0'
128 */
129static void ident_cpy(unsigned char *dst, unsigned char *src,
130 unsigned int len)
131{
132 unsigned char *end, *last;
wdenkc6097192002-11-03 00:24:07 +0000133
Simon Glass43a464a2016-05-01 11:36:00 -0600134 last = dst;
135 end = src + len - 1;
wdenkc6097192002-11-03 00:24:07 +0000136
Simon Glass43a464a2016-05-01 11:36:00 -0600137 /* reserve space for '\0' */
138 if (len < 2)
139 goto OUT;
wdenkc6097192002-11-03 00:24:07 +0000140
Simon Glass43a464a2016-05-01 11:36:00 -0600141 /* skip leading white space */
142 while ((*src) && (src < end) && (*src == ' '))
143 ++src;
wdenkc6097192002-11-03 00:24:07 +0000144
Simon Glass43a464a2016-05-01 11:36:00 -0600145 /* copy string, omitting trailing white space */
146 while ((*src) && (src < end)) {
147 *dst++ = *src;
148 if (*src++ != ' ')
149 last = dst;
150 }
151OUT:
152 *last = '\0';
153}
wdenkc6097192002-11-03 00:24:07 +0000154
Simon Glass43a464a2016-05-01 11:36:00 -0600155#ifdef CONFIG_ATAPI
156/****************************************************************************
157 * ATAPI Support
158 */
wdenkc35ba4e2004-03-14 22:25:36 +0000159
Simon Glass43a464a2016-05-01 11:36:00 -0600160#if defined(CONFIG_IDE_SWAP_IO)
161/* since ATAPI may use commands with not 4 bytes alligned length
162 * we have our own transfer functions, 2 bytes alligned */
163__weak void ide_output_data_shorts(int dev, ushort *sect_buf, int shorts)
164{
165 ushort *dbuf;
166 volatile ushort *pbuf;
wdenkc6097192002-11-03 00:24:07 +0000167
Simon Glass43a464a2016-05-01 11:36:00 -0600168 pbuf = (ushort *)(ATA_CURR_BASE(dev) + ATA_DATA_REG);
169 dbuf = (ushort *)sect_buf;
wdenkc6097192002-11-03 00:24:07 +0000170
Simon Glass43a464a2016-05-01 11:36:00 -0600171 debug("in output data shorts base for read is %lx\n",
172 (unsigned long) pbuf);
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000173
Simon Glass43a464a2016-05-01 11:36:00 -0600174 while (shorts--) {
175 EIEIO;
176 *pbuf = *dbuf++;
177 }
178}
wdenkc6097192002-11-03 00:24:07 +0000179
Simon Glass43a464a2016-05-01 11:36:00 -0600180__weak void ide_input_data_shorts(int dev, ushort *sect_buf, int shorts)
181{
182 ushort *dbuf;
183 volatile ushort *pbuf;
wdenkc35ba4e2004-03-14 22:25:36 +0000184
Simon Glass43a464a2016-05-01 11:36:00 -0600185 pbuf = (ushort *)(ATA_CURR_BASE(dev) + ATA_DATA_REG);
186 dbuf = (ushort *)sect_buf;
wdenkc6097192002-11-03 00:24:07 +0000187
Simon Glass43a464a2016-05-01 11:36:00 -0600188 debug("in input data shorts base for read is %lx\n",
189 (unsigned long) pbuf);
wdenkc6097192002-11-03 00:24:07 +0000190
Simon Glass43a464a2016-05-01 11:36:00 -0600191 while (shorts--) {
192 EIEIO;
193 *dbuf++ = *pbuf;
wdenkc6097192002-11-03 00:24:07 +0000194 }
wdenkc6097192002-11-03 00:24:07 +0000195}
196
Simon Glass43a464a2016-05-01 11:36:00 -0600197#else /* ! CONFIG_IDE_SWAP_IO */
198__weak void ide_output_data_shorts(int dev, ushort *sect_buf, int shorts)
wdenkc6097192002-11-03 00:24:07 +0000199{
Simon Glass43a464a2016-05-01 11:36:00 -0600200 outsw(ATA_CURR_BASE(dev) + ATA_DATA_REG, sect_buf, shorts);
wdenkc6097192002-11-03 00:24:07 +0000201}
202
Simon Glass43a464a2016-05-01 11:36:00 -0600203__weak void ide_input_data_shorts(int dev, ushort *sect_buf, int shorts)
Pavel Herrmann95b0cb52012-10-07 05:56:10 +0000204{
Simon Glass43a464a2016-05-01 11:36:00 -0600205 insw(ATA_CURR_BASE(dev) + ATA_DATA_REG, sect_buf, shorts);
206}
Pavel Herrmann95b0cb52012-10-07 05:56:10 +0000207
Simon Glass43a464a2016-05-01 11:36:00 -0600208#endif /* CONFIG_IDE_SWAP_IO */
Pavel Herrmann95b0cb52012-10-07 05:56:10 +0000209
Simon Glass43a464a2016-05-01 11:36:00 -0600210/*
211 * Wait until (Status & mask) == res, or timeout (in ms)
212 * Return last status
213 * This is used since some ATAPI CD ROMs clears their Busy Bit first
214 * and then they set their DRQ Bit
215 */
216static uchar atapi_wait_mask(int dev, ulong t, uchar mask, uchar res)
Stefan Roese37628252008-08-06 14:05:38 +0200217{
Simon Glass43a464a2016-05-01 11:36:00 -0600218 ulong delay = 10 * t; /* poll every 100 us */
219 uchar c;
Macpaul Lind1e49942011-04-11 20:45:32 +0000220
Simon Glass43a464a2016-05-01 11:36:00 -0600221 /* prevents to read the status before valid */
222 c = ide_inb(dev, ATA_DEV_CTL);
223
224 while (((c = ide_inb(dev, ATA_STATUS)) & mask) != res) {
225 /* break if error occurs (doesn't make sense to wait more) */
226 if ((c & ATA_STAT_ERR) == ATA_STAT_ERR)
227 break;
228 udelay(100);
229 if (delay-- == 0)
230 break;
Macpaul Lind1e49942011-04-11 20:45:32 +0000231 }
Simon Glass43a464a2016-05-01 11:36:00 -0600232 return c;
Stefan Roese37628252008-08-06 14:05:38 +0200233}
Macpaul Lind1e49942011-04-11 20:45:32 +0000234
Simon Glass43a464a2016-05-01 11:36:00 -0600235/*
236 * issue an atapi command
237 */
238unsigned char atapi_issue(int device, unsigned char *ccb, int ccblen,
239 unsigned char *buffer, int buflen)
Stefan Roese37628252008-08-06 14:05:38 +0200240{
Simon Glass43a464a2016-05-01 11:36:00 -0600241 unsigned char c, err, mask, res;
242 int n;
Macpaul Lind1e49942011-04-11 20:45:32 +0000243
Simon Glass43a464a2016-05-01 11:36:00 -0600244 ide_led(DEVICE_LED(device), 1); /* LED on */
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000245
Simon Glass43a464a2016-05-01 11:36:00 -0600246 /* Select device
247 */
248 mask = ATA_STAT_BUSY | ATA_STAT_DRQ;
249 res = 0;
250 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
251 c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
252 if ((c & mask) != res) {
253 printf("ATAPI_ISSUE: device %d not ready status %X\n", device,
254 c);
255 err = 0xFF;
256 goto AI_OUT;
257 }
258 /* write taskfile */
259 ide_outb(device, ATA_ERROR_REG, 0); /* no DMA, no overlaped */
260 ide_outb(device, ATA_SECT_CNT, 0);
261 ide_outb(device, ATA_SECT_NUM, 0);
262 ide_outb(device, ATA_CYL_LOW, (unsigned char) (buflen & 0xFF));
263 ide_outb(device, ATA_CYL_HIGH,
264 (unsigned char) ((buflen >> 8) & 0xFF));
265 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000266
Simon Glass43a464a2016-05-01 11:36:00 -0600267 ide_outb(device, ATA_COMMAND, ATAPI_CMD_PACKET);
268 udelay(50);
wdenk7f00b942003-12-07 23:55:12 +0000269
Simon Glass43a464a2016-05-01 11:36:00 -0600270 mask = ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR;
271 res = ATA_STAT_DRQ;
272 c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
wdenk7f00b942003-12-07 23:55:12 +0000273
Simon Glass43a464a2016-05-01 11:36:00 -0600274 if ((c & mask) != res) { /* DRQ must be 1, BSY 0 */
275 printf("ATAPI_ISSUE: Error (no IRQ) before sending ccb dev %d status 0x%02x\n",
276 device, c);
277 err = 0xFF;
278 goto AI_OUT;
wdenk7f00b942003-12-07 23:55:12 +0000279 }
wdenkc6097192002-11-03 00:24:07 +0000280
Simon Glass43a464a2016-05-01 11:36:00 -0600281 /* write command block */
282 ide_output_data_shorts(device, (unsigned short *)ccb, ccblen / 2);
wdenkc6097192002-11-03 00:24:07 +0000283
Simon Glass43a464a2016-05-01 11:36:00 -0600284 /* ATAPI Command written wait for completition */
285 udelay(5000); /* device must set bsy */
286
287 mask = ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR;
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000288 /*
Simon Glass43a464a2016-05-01 11:36:00 -0600289 * if no data wait for DRQ = 0 BSY = 0
290 * if data wait for DRQ = 1 BSY = 0
wdenkc6097192002-11-03 00:24:07 +0000291 */
Simon Glass43a464a2016-05-01 11:36:00 -0600292 res = 0;
293 if (buflen)
294 res = ATA_STAT_DRQ;
295 c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
296 if ((c & mask) != res) {
297 if (c & ATA_STAT_ERR) {
298 err = (ide_inb(device, ATA_ERROR_REG)) >> 4;
299 debug("atapi_issue 1 returned sense key %X status %02X\n",
300 err, c);
301 } else {
302 printf("ATAPI_ISSUE: (no DRQ) after sending ccb (%x) status 0x%02x\n",
303 ccb[0], c);
304 err = 0xFF;
305 }
306 goto AI_OUT;
Pavel Herrmann2c13c4a2012-10-09 07:01:56 +0000307 }
Simon Glass43a464a2016-05-01 11:36:00 -0600308 n = ide_inb(device, ATA_CYL_HIGH);
309 n <<= 8;
310 n += ide_inb(device, ATA_CYL_LOW);
311 if (n > buflen) {
312 printf("ERROR, transfer bytes %d requested only %d\n", n,
313 buflen);
314 err = 0xff;
315 goto AI_OUT;
316 }
317 if ((n == 0) && (buflen < 0)) {
318 printf("ERROR, transfer bytes %d requested %d\n", n, buflen);
319 err = 0xff;
320 goto AI_OUT;
321 }
322 if (n != buflen) {
323 debug("WARNING, transfer bytes %d not equal with requested %d\n",
324 n, buflen);
325 }
326 if (n != 0) { /* data transfer */
327 debug("ATAPI_ISSUE: %d Bytes to transfer\n", n);
328 /* we transfer shorts */
329 n >>= 1;
330 /* ok now decide if it is an in or output */
331 if ((ide_inb(device, ATA_SECT_CNT) & 0x02) == 0) {
332 debug("Write to device\n");
333 ide_output_data_shorts(device, (unsigned short *)buffer,
334 n);
335 } else {
336 debug("Read from device @ %p shorts %d\n", buffer, n);
337 ide_input_data_shorts(device, (unsigned short *)buffer,
338 n);
339 }
340 }
341 udelay(5000); /* seems that some CD ROMs need this... */
342 mask = ATA_STAT_BUSY | ATA_STAT_ERR;
343 res = 0;
344 c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
345 if ((c & ATA_STAT_ERR) == ATA_STAT_ERR) {
346 err = (ide_inb(device, ATA_ERROR_REG) >> 4);
347 debug("atapi_issue 2 returned sense key %X status %X\n", err,
348 c);
349 } else {
350 err = 0;
351 }
352AI_OUT:
353 ide_led(DEVICE_LED(device), 0); /* LED off */
354 return err;
355}
wdenkc6097192002-11-03 00:24:07 +0000356
Simon Glass43a464a2016-05-01 11:36:00 -0600357/*
358 * sending the command to atapi_issue. If an status other than good
359 * returns, an request_sense will be issued
360 */
wdenkc6097192002-11-03 00:24:07 +0000361
Simon Glass43a464a2016-05-01 11:36:00 -0600362#define ATAPI_DRIVE_NOT_READY 100
363#define ATAPI_UNIT_ATTN 10
wdenkc6097192002-11-03 00:24:07 +0000364
Simon Glass43a464a2016-05-01 11:36:00 -0600365unsigned char atapi_issue_autoreq(int device,
366 unsigned char *ccb,
367 int ccblen,
368 unsigned char *buffer, int buflen)
369{
370 unsigned char sense_data[18], sense_ccb[12];
371 unsigned char res, key, asc, ascq;
372 int notready, unitattn;
wdenkc6097192002-11-03 00:24:07 +0000373
Simon Glass43a464a2016-05-01 11:36:00 -0600374 unitattn = ATAPI_UNIT_ATTN;
375 notready = ATAPI_DRIVE_NOT_READY;
wdenkc6097192002-11-03 00:24:07 +0000376
Simon Glass43a464a2016-05-01 11:36:00 -0600377retry:
378 res = atapi_issue(device, ccb, ccblen, buffer, buflen);
379 if (res == 0)
380 return 0; /* Ok */
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000381
Simon Glass43a464a2016-05-01 11:36:00 -0600382 if (res == 0xFF)
383 return 0xFF; /* error */
wdenkc6097192002-11-03 00:24:07 +0000384
Simon Glass43a464a2016-05-01 11:36:00 -0600385 debug("(auto_req)atapi_issue returned sense key %X\n", res);
wdenk452cfd62002-11-19 11:04:11 +0000386
Simon Glass43a464a2016-05-01 11:36:00 -0600387 memset(sense_ccb, 0, sizeof(sense_ccb));
388 memset(sense_data, 0, sizeof(sense_data));
389 sense_ccb[0] = ATAPI_CMD_REQ_SENSE;
390 sense_ccb[4] = 18; /* allocation Length */
wdenkc6097192002-11-03 00:24:07 +0000391
Simon Glass43a464a2016-05-01 11:36:00 -0600392 res = atapi_issue(device, sense_ccb, 12, sense_data, 18);
393 key = (sense_data[2] & 0xF);
394 asc = (sense_data[12]);
395 ascq = (sense_data[13]);
wdenkc6097192002-11-03 00:24:07 +0000396
Simon Glass43a464a2016-05-01 11:36:00 -0600397 debug("ATAPI_CMD_REQ_SENSE returned %x\n", res);
398 debug(" Sense page: %02X key %02X ASC %02X ASCQ %02X\n",
399 sense_data[0], key, asc, ascq);
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000400
Simon Glass43a464a2016-05-01 11:36:00 -0600401 if ((key == 0))
402 return 0; /* ok device ready */
403
404 if ((key == 6) || (asc == 0x29) || (asc == 0x28)) { /* Unit Attention */
405 if (unitattn-- > 0) {
406 udelay(200 * 1000);
407 goto retry;
wdenkc6097192002-11-03 00:24:07 +0000408 }
Simon Glass43a464a2016-05-01 11:36:00 -0600409 printf("Unit Attention, tried %d\n", ATAPI_UNIT_ATTN);
410 goto error;
wdenkc6097192002-11-03 00:24:07 +0000411 }
Simon Glass43a464a2016-05-01 11:36:00 -0600412 if ((asc == 0x4) && (ascq == 0x1)) {
413 /* not ready, but will be ready soon */
414 if (notready-- > 0) {
415 udelay(200 * 1000);
416 goto retry;
417 }
418 printf("Drive not ready, tried %d times\n",
419 ATAPI_DRIVE_NOT_READY);
420 goto error;
421 }
422 if (asc == 0x3a) {
423 debug("Media not present\n");
424 goto error;
425 }
426
427 printf("ERROR: Unknown Sense key %02X ASC %02X ASCQ %02X\n", key, asc,
428 ascq);
429error:
430 debug("ERROR Sense key %02X ASC %02X ASCQ %02X\n", key, asc, ascq);
431 return 0xFF;
wdenkc6097192002-11-03 00:24:07 +0000432}
433
Simon Glass43a464a2016-05-01 11:36:00 -0600434/*
435 * atapi_read:
436 * we transfer only one block per command, since the multiple DRQ per
437 * command is not yet implemented
438 */
439#define ATAPI_READ_MAX_BYTES 2048 /* we read max 2kbytes */
440#define ATAPI_READ_BLOCK_SIZE 2048 /* assuming CD part */
441#define ATAPI_READ_MAX_BLOCK (ATAPI_READ_MAX_BYTES/ATAPI_READ_BLOCK_SIZE)
wdenkc6097192002-11-03 00:24:07 +0000442
Simon Glass43a464a2016-05-01 11:36:00 -0600443ulong atapi_read(struct blk_desc *block_dev, lbaint_t blknr, lbaint_t blkcnt,
444 void *buffer)
wdenkc6097192002-11-03 00:24:07 +0000445{
Simon Glass43a464a2016-05-01 11:36:00 -0600446 int device = block_dev->devnum;
447 ulong n = 0;
448 unsigned char ccb[12]; /* Command descriptor block */
449 ulong cnt;
wdenkc6097192002-11-03 00:24:07 +0000450
Simon Glass43a464a2016-05-01 11:36:00 -0600451 debug("atapi_read dev %d start " LBAF " blocks " LBAF
452 " buffer at %lX\n", device, blknr, blkcnt, (ulong) buffer);
wdenkc6097192002-11-03 00:24:07 +0000453
Simon Glass43a464a2016-05-01 11:36:00 -0600454 do {
455 if (blkcnt > ATAPI_READ_MAX_BLOCK)
456 cnt = ATAPI_READ_MAX_BLOCK;
457 else
458 cnt = blkcnt;
459
460 ccb[0] = ATAPI_CMD_READ_12;
461 ccb[1] = 0; /* reserved */
462 ccb[2] = (unsigned char) (blknr >> 24) & 0xFF; /* MSB Block */
463 ccb[3] = (unsigned char) (blknr >> 16) & 0xFF; /* */
464 ccb[4] = (unsigned char) (blknr >> 8) & 0xFF;
465 ccb[5] = (unsigned char) blknr & 0xFF; /* LSB Block */
466 ccb[6] = (unsigned char) (cnt >> 24) & 0xFF; /* MSB Block cnt */
467 ccb[7] = (unsigned char) (cnt >> 16) & 0xFF;
468 ccb[8] = (unsigned char) (cnt >> 8) & 0xFF;
469 ccb[9] = (unsigned char) cnt & 0xFF; /* LSB Block */
470 ccb[10] = 0; /* reserved */
471 ccb[11] = 0; /* reserved */
472
473 if (atapi_issue_autoreq(device, ccb, 12,
474 (unsigned char *)buffer,
475 cnt * ATAPI_READ_BLOCK_SIZE)
476 == 0xFF) {
477 return n;
478 }
479 n += cnt;
480 blkcnt -= cnt;
481 blknr += cnt;
482 buffer += (cnt * ATAPI_READ_BLOCK_SIZE);
483 } while (blkcnt > 0);
484 return n;
Pavel Herrmann35ad9a82012-10-09 07:04:39 +0000485}
Simon Glass43a464a2016-05-01 11:36:00 -0600486
487static void atapi_inquiry(struct blk_desc *dev_desc)
wdenkc6097192002-11-03 00:24:07 +0000488{
Simon Glass43a464a2016-05-01 11:36:00 -0600489 unsigned char ccb[12]; /* Command descriptor block */
490 unsigned char iobuf[64]; /* temp buf */
491 unsigned char c;
492 int device;
wdenkf1276d42005-02-03 23:00:49 +0000493
Simon Glass43a464a2016-05-01 11:36:00 -0600494 device = dev_desc->devnum;
495 dev_desc->type = DEV_TYPE_UNKNOWN; /* not yet valid */
496 dev_desc->block_read = atapi_read;
wdenkf1276d42005-02-03 23:00:49 +0000497
Simon Glass43a464a2016-05-01 11:36:00 -0600498 memset(ccb, 0, sizeof(ccb));
499 memset(iobuf, 0, sizeof(iobuf));
wdenk591dda52002-11-18 00:14:45 +0000500
Simon Glass43a464a2016-05-01 11:36:00 -0600501 ccb[0] = ATAPI_CMD_INQUIRY;
502 ccb[4] = 40; /* allocation Legnth */
503 c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *)iobuf, 40);
wdenk591dda52002-11-18 00:14:45 +0000504
Simon Glass43a464a2016-05-01 11:36:00 -0600505 debug("ATAPI_CMD_INQUIRY returned %x\n", c);
506 if (c != 0)
507 return;
wdenkf1276d42005-02-03 23:00:49 +0000508
Simon Glass43a464a2016-05-01 11:36:00 -0600509 /* copy device ident strings */
510 ident_cpy((unsigned char *)dev_desc->vendor, &iobuf[8], 8);
511 ident_cpy((unsigned char *)dev_desc->product, &iobuf[16], 16);
512 ident_cpy((unsigned char *)dev_desc->revision, &iobuf[32], 5);
wdenkc6097192002-11-03 00:24:07 +0000513
Simon Glass43a464a2016-05-01 11:36:00 -0600514 dev_desc->lun = 0;
515 dev_desc->lba = 0;
516 dev_desc->blksz = 0;
517 dev_desc->log2blksz = LOG2_INVALID(typeof(dev_desc->log2blksz));
518 dev_desc->type = iobuf[0] & 0x1f;
wdenkf1276d42005-02-03 23:00:49 +0000519
Simon Glass43a464a2016-05-01 11:36:00 -0600520 if ((iobuf[1] & 0x80) == 0x80)
521 dev_desc->removable = 1;
522 else
523 dev_desc->removable = 0;
wdenkf1276d42005-02-03 23:00:49 +0000524
Simon Glass43a464a2016-05-01 11:36:00 -0600525 memset(ccb, 0, sizeof(ccb));
526 memset(iobuf, 0, sizeof(iobuf));
527 ccb[0] = ATAPI_CMD_START_STOP;
528 ccb[4] = 0x03; /* start */
wdenkf1276d42005-02-03 23:00:49 +0000529
Simon Glass43a464a2016-05-01 11:36:00 -0600530 c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *)iobuf, 0);
531
532 debug("ATAPI_CMD_START_STOP returned %x\n", c);
533 if (c != 0)
534 return;
535
536 memset(ccb, 0, sizeof(ccb));
537 memset(iobuf, 0, sizeof(iobuf));
538 c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *)iobuf, 0);
539
540 debug("ATAPI_CMD_UNIT_TEST_READY returned %x\n", c);
541 if (c != 0)
542 return;
543
544 memset(ccb, 0, sizeof(ccb));
545 memset(iobuf, 0, sizeof(iobuf));
546 ccb[0] = ATAPI_CMD_READ_CAP;
547 c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *)iobuf, 8);
548 debug("ATAPI_CMD_READ_CAP returned %x\n", c);
549 if (c != 0)
550 return;
551
552 debug("Read Cap: LBA %02X%02X%02X%02X blksize %02X%02X%02X%02X\n",
553 iobuf[0], iobuf[1], iobuf[2], iobuf[3],
554 iobuf[4], iobuf[5], iobuf[6], iobuf[7]);
555
556 dev_desc->lba = ((unsigned long) iobuf[0] << 24) +
557 ((unsigned long) iobuf[1] << 16) +
558 ((unsigned long) iobuf[2] << 8) + ((unsigned long) iobuf[3]);
559 dev_desc->blksz = ((unsigned long) iobuf[4] << 24) +
560 ((unsigned long) iobuf[5] << 16) +
561 ((unsigned long) iobuf[6] << 8) + ((unsigned long) iobuf[7]);
562 dev_desc->log2blksz = LOG2(dev_desc->blksz);
563#ifdef CONFIG_LBA48
564 /* ATAPI devices cannot use 48bit addressing (ATA/ATAPI v7) */
565 dev_desc->lba48 = 0;
Macpaul Lind1e49942011-04-11 20:45:32 +0000566#endif
Simon Glass43a464a2016-05-01 11:36:00 -0600567 return;
wdenk591dda52002-11-18 00:14:45 +0000568}
569
Simon Glass43a464a2016-05-01 11:36:00 -0600570#endif /* CONFIG_ATAPI */
wdenkc6097192002-11-03 00:24:07 +0000571
Simon Glasse3394752016-02-29 15:25:34 -0700572static void ide_ident(struct blk_desc *dev_desc)
wdenkc6097192002-11-03 00:24:07 +0000573{
wdenkc6097192002-11-03 00:24:07 +0000574 unsigned char c;
Marek Vasute49757e2011-08-20 09:15:13 +0000575 hd_driveid_t iop;
wdenkc6097192002-11-03 00:24:07 +0000576
wdenke2d6d742004-09-28 20:34:50 +0000577#ifdef CONFIG_ATAPI
578 int retries = 0;
wdenk452cfd62002-11-19 11:04:11 +0000579#endif
wdenkc6097192002-11-03 00:24:07 +0000580 int device;
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000581
Simon Glass2f26fff2016-02-29 15:25:51 -0700582 device = dev_desc->devnum;
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000583 printf(" Device %d: ", device);
wdenkc6097192002-11-03 00:24:07 +0000584
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000585 ide_led(DEVICE_LED(device), 1); /* LED on */
wdenkc6097192002-11-03 00:24:07 +0000586 /* Select device
587 */
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000588 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
589 dev_desc->if_type = IF_TYPE_IDE;
wdenkc6097192002-11-03 00:24:07 +0000590#ifdef CONFIG_ATAPI
wdenk452cfd62002-11-19 11:04:11 +0000591
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000592 retries = 0;
wdenk452cfd62002-11-19 11:04:11 +0000593
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000594 /* Warning: This will be tricky to read */
595 while (retries <= 1) {
596 /* check signature */
597 if ((ide_inb(device, ATA_SECT_CNT) == 0x01) &&
598 (ide_inb(device, ATA_SECT_NUM) == 0x01) &&
599 (ide_inb(device, ATA_CYL_LOW) == 0x14) &&
600 (ide_inb(device, ATA_CYL_HIGH) == 0xEB)) {
601 /* ATAPI Signature found */
602 dev_desc->if_type = IF_TYPE_ATAPI;
603 /*
604 * Start Ident Command
605 */
606 ide_outb(device, ATA_COMMAND, ATAPI_CMD_IDENT);
607 /*
608 * Wait for completion - ATAPI devices need more time
609 * to become ready
610 */
611 c = ide_wait(device, ATAPI_TIME_OUT);
612 } else
wdenkc6097192002-11-03 00:24:07 +0000613#endif
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000614 {
615 /*
616 * Start Ident Command
617 */
618 ide_outb(device, ATA_COMMAND, ATA_CMD_IDENT);
wdenkc6097192002-11-03 00:24:07 +0000619
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000620 /*
621 * Wait for completion
622 */
623 c = ide_wait(device, IDE_TIME_OUT);
624 }
625 ide_led(DEVICE_LED(device), 0); /* LED off */
wdenkc6097192002-11-03 00:24:07 +0000626
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000627 if (((c & ATA_STAT_DRQ) == 0) ||
628 ((c & (ATA_STAT_FAULT | ATA_STAT_ERR)) != 0)) {
wdenke2d6d742004-09-28 20:34:50 +0000629#ifdef CONFIG_ATAPI
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000630 {
631 /*
632 * Need to soft reset the device
633 * in case it's an ATAPI...
634 */
635 debug("Retrying...\n");
636 ide_outb(device, ATA_DEV_HD,
637 ATA_LBA | ATA_DEVICE(device));
638 udelay(100000);
639 ide_outb(device, ATA_COMMAND, 0x08);
640 udelay(500000); /* 500 ms */
641 }
642 /*
643 * Select device
644 */
645 ide_outb(device, ATA_DEV_HD,
646 ATA_LBA | ATA_DEVICE(device));
647 retries++;
wdenke2d6d742004-09-28 20:34:50 +0000648#else
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000649 return;
wdenke2d6d742004-09-28 20:34:50 +0000650#endif
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000651 }
wdenke2d6d742004-09-28 20:34:50 +0000652#ifdef CONFIG_ATAPI
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000653 else
654 break;
655 } /* see above - ugly to read */
wdenke2d6d742004-09-28 20:34:50 +0000656
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000657 if (retries == 2) /* Not found */
wdenke2d6d742004-09-28 20:34:50 +0000658 return;
659#endif
wdenkc6097192002-11-03 00:24:07 +0000660
Pavel Herrmann35ad9a82012-10-09 07:04:39 +0000661 ide_input_swap_data(device, (ulong *)&iop, ATA_SECTORWORDS);
wdenkc6097192002-11-03 00:24:07 +0000662
Simon Glass43a464a2016-05-01 11:36:00 -0600663 ident_cpy((unsigned char *)dev_desc->revision, iop.fw_rev,
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000664 sizeof(dev_desc->revision));
Simon Glass43a464a2016-05-01 11:36:00 -0600665 ident_cpy((unsigned char *)dev_desc->vendor, iop.model,
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000666 sizeof(dev_desc->vendor));
Simon Glass43a464a2016-05-01 11:36:00 -0600667 ident_cpy((unsigned char *)dev_desc->product, iop.serial_no,
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000668 sizeof(dev_desc->product));
wdenkacd9b102004-03-14 00:59:59 +0000669#ifdef __LITTLE_ENDIAN
670 /*
Richard Retanubun54f30622008-11-06 14:01:51 -0500671 * firmware revision, model, and serial number have Big Endian Byte
672 * order in Word. Convert all three to little endian.
wdenkacd9b102004-03-14 00:59:59 +0000673 *
674 * See CF+ and CompactFlash Specification Revision 2.0:
Richard Retanubun54f30622008-11-06 14:01:51 -0500675 * 6.2.1.6: Identify Drive, Table 39 for more details
wdenkacd9b102004-03-14 00:59:59 +0000676 */
677
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000678 strswab(dev_desc->revision);
679 strswab(dev_desc->vendor);
680 strswab(dev_desc->product);
wdenkacd9b102004-03-14 00:59:59 +0000681#endif /* __LITTLE_ENDIAN */
wdenkc6097192002-11-03 00:24:07 +0000682
Marek Vasute49757e2011-08-20 09:15:13 +0000683 if ((iop.config & 0x0080) == 0x0080)
wdenkc6097192002-11-03 00:24:07 +0000684 dev_desc->removable = 1;
685 else
686 dev_desc->removable = 0;
687
wdenkc6097192002-11-03 00:24:07 +0000688#ifdef CONFIG_ATAPI
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000689 if (dev_desc->if_type == IF_TYPE_ATAPI) {
wdenkc6097192002-11-03 00:24:07 +0000690 atapi_inquiry(dev_desc);
691 return;
692 }
693#endif /* CONFIG_ATAPI */
694
wdenkacd9b102004-03-14 00:59:59 +0000695#ifdef __BIG_ENDIAN
wdenkc6097192002-11-03 00:24:07 +0000696 /* swap shorts */
Marek Vasute49757e2011-08-20 09:15:13 +0000697 dev_desc->lba = (iop.lba_capacity << 16) | (iop.lba_capacity >> 16);
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000698#else /* ! __BIG_ENDIAN */
wdenkacd9b102004-03-14 00:59:59 +0000699 /*
700 * do not swap shorts on little endian
701 *
702 * See CF+ and CompactFlash Specification Revision 2.0:
703 * 6.2.1.6: Identfy Drive, Table 39, Word Address 57-58 for details.
704 */
Marek Vasute49757e2011-08-20 09:15:13 +0000705 dev_desc->lba = iop.lba_capacity;
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000706#endif /* __BIG_ENDIAN */
wdenkf602aa02004-03-13 23:29:43 +0000707
wdenkc35ba4e2004-03-14 22:25:36 +0000708#ifdef CONFIG_LBA48
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000709 if (iop.command_set_2 & 0x0400) { /* LBA 48 support */
wdenk05939202004-04-18 17:39:38 +0000710 dev_desc->lba48 = 1;
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000711 dev_desc->lba = (unsigned long long) iop.lba48_capacity[0] |
712 ((unsigned long long) iop.lba48_capacity[1] << 16) |
713 ((unsigned long long) iop.lba48_capacity[2] << 32) |
714 ((unsigned long long) iop.lba48_capacity[3] << 48);
wdenkf602aa02004-03-13 23:29:43 +0000715 } else {
wdenkf602aa02004-03-13 23:29:43 +0000716 dev_desc->lba48 = 0;
717 }
718#endif /* CONFIG_LBA48 */
wdenkc6097192002-11-03 00:24:07 +0000719 /* assuming HD */
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000720 dev_desc->type = DEV_TYPE_HARDDISK;
721 dev_desc->blksz = ATA_BLOCKSIZE;
Egbert Eich2eec2ab2013-04-09 21:11:56 +0000722 dev_desc->log2blksz = LOG2(dev_desc->blksz);
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000723 dev_desc->lun = 0; /* just to fill something in... */
wdenkc6097192002-11-03 00:24:07 +0000724
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000725#if 0 /* only used to test the powersaving mode,
726 * if enabled, the drive goes after 5 sec
727 * in standby mode */
728 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
729 c = ide_wait(device, IDE_TIME_OUT);
730 ide_outb(device, ATA_SECT_CNT, 1);
731 ide_outb(device, ATA_LBA_LOW, 0);
732 ide_outb(device, ATA_LBA_MID, 0);
733 ide_outb(device, ATA_LBA_HIGH, 0);
734 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
735 ide_outb(device, ATA_COMMAND, 0xe3);
736 udelay(50);
737 c = ide_wait(device, IDE_TIME_OUT); /* can't take over 500 ms */
wdenkc6097192002-11-03 00:24:07 +0000738#endif
739}
740
Simon Glass43a464a2016-05-01 11:36:00 -0600741/* ------------------------------------------------------------------------- */
742
743int do_ide(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
744{
745 int rcode = 0;
746
747 switch (argc) {
748 case 0:
749 case 1:
750 return CMD_RET_USAGE;
751 case 2:
752 if (strncmp(argv[1], "res", 3) == 0) {
753 puts("\nReset IDE"
754#ifdef CONFIG_IDE_8xx_DIRECT
755 " on PCMCIA " PCMCIA_SLOT_MSG
756#endif
757 ": ");
758
759 ide_init();
760 return 0;
761 } else if (strncmp(argv[1], "inf", 3) == 0) {
762 int i;
763
764 putc('\n');
765
766 for (i = 0; i < CONFIG_SYS_IDE_MAXDEVICE; ++i) {
767 if (ide_dev_desc[i].type == DEV_TYPE_UNKNOWN)
768 continue; /* list only known devices */
769 printf("IDE device %d: ", i);
770 dev_print(&ide_dev_desc[i]);
771 }
772 return 0;
773
774 } else if (strncmp(argv[1], "dev", 3) == 0) {
775 if (curr_device < 0 ||
776 curr_device >= CONFIG_SYS_IDE_MAXDEVICE) {
777 puts("\nno IDE devices available\n");
778 return 1;
779 }
780 printf("\nIDE device %d: ", curr_device);
781 dev_print(&ide_dev_desc[curr_device]);
782 return 0;
783 } else if (strncmp(argv[1], "part", 4) == 0) {
784 int dev, ok;
785
786 for (ok = 0, dev = 0;
787 dev < CONFIG_SYS_IDE_MAXDEVICE;
788 ++dev) {
789 if (ide_dev_desc[dev].part_type !=
790 PART_TYPE_UNKNOWN) {
791 ++ok;
792 if (dev)
793 putc('\n');
794 part_print(&ide_dev_desc[dev]);
795 }
796 }
797 if (!ok) {
798 puts("\nno IDE devices available\n");
799 rcode++;
800 }
801 return rcode;
802 }
803 return CMD_RET_USAGE;
804 case 3:
805 if (strncmp(argv[1], "dev", 3) == 0) {
806 int dev = (int)simple_strtoul(argv[2], NULL, 10);
wdenkc6097192002-11-03 00:24:07 +0000807
Simon Glass43a464a2016-05-01 11:36:00 -0600808 printf("\nIDE device %d: ", dev);
809 if (dev >= CONFIG_SYS_IDE_MAXDEVICE) {
810 puts("unknown device\n");
811 return 1;
812 }
813 dev_print(&ide_dev_desc[dev]);
814 /*ide_print (dev); */
wdenkc6097192002-11-03 00:24:07 +0000815
Simon Glass43a464a2016-05-01 11:36:00 -0600816 if (ide_dev_desc[dev].type == DEV_TYPE_UNKNOWN)
817 return 1;
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000818
Simon Glass43a464a2016-05-01 11:36:00 -0600819 curr_device = dev;
wdenkc6097192002-11-03 00:24:07 +0000820
Simon Glass43a464a2016-05-01 11:36:00 -0600821 puts("... is now current device\n");
wdenkc6097192002-11-03 00:24:07 +0000822
Simon Glass43a464a2016-05-01 11:36:00 -0600823 return 0;
824 } else if (strncmp(argv[1], "part", 4) == 0) {
825 int dev = (int)simple_strtoul(argv[2], NULL, 10);
wdenkc6097192002-11-03 00:24:07 +0000826
Simon Glass43a464a2016-05-01 11:36:00 -0600827 if (ide_dev_desc[dev].part_type != PART_TYPE_UNKNOWN) {
828 part_print(&ide_dev_desc[dev]);
829 } else {
830 printf("\nIDE device %d not available\n",
831 dev);
832 rcode = 1;
833 }
834 return rcode;
835 }
wdenkc6097192002-11-03 00:24:07 +0000836
Simon Glass43a464a2016-05-01 11:36:00 -0600837 return CMD_RET_USAGE;
838 default:
839 /* at least 4 args */
wdenkc6097192002-11-03 00:24:07 +0000840
Simon Glass43a464a2016-05-01 11:36:00 -0600841 if (strcmp(argv[1], "read") == 0) {
842 ulong addr = simple_strtoul(argv[2], NULL, 16);
843 ulong cnt = simple_strtoul(argv[4], NULL, 16);
844 struct blk_desc *dev_desc;
845 ulong n;
wdenkc6097192002-11-03 00:24:07 +0000846
Simon Glass43a464a2016-05-01 11:36:00 -0600847#ifdef CONFIG_SYS_64BIT_LBA
848 lbaint_t blk = simple_strtoull(argv[3], NULL, 16);
wdenkc6097192002-11-03 00:24:07 +0000849
Simon Glass43a464a2016-05-01 11:36:00 -0600850 printf("\nIDE read: device %d block # %lld, count %ld...",
851 curr_device, blk, cnt);
852#else
853 lbaint_t blk = simple_strtoul(argv[3], NULL, 16);
wdenkc6097192002-11-03 00:24:07 +0000854
Simon Glass43a464a2016-05-01 11:36:00 -0600855 printf("\nIDE read: device %d block # %ld, count %ld...",
856 curr_device, blk, cnt);
857#endif
wdenkc6097192002-11-03 00:24:07 +0000858
Simon Glass43a464a2016-05-01 11:36:00 -0600859 dev_desc = &ide_dev_desc[curr_device];
860 n = blk_dread(dev_desc, blk, cnt, (ulong *)addr);
861 /* flush cache after read */
862 flush_cache(addr,
863 cnt * ide_dev_desc[curr_device].blksz);
wdenkc6097192002-11-03 00:24:07 +0000864
Simon Glass43a464a2016-05-01 11:36:00 -0600865 printf("%ld blocks read: %s\n",
866 n, (n == cnt) ? "OK" : "ERROR");
867 if (n == cnt)
868 return 0;
869 else
870 return 1;
871 } else if (strcmp(argv[1], "write") == 0) {
872 ulong addr = simple_strtoul(argv[2], NULL, 16);
873 ulong cnt = simple_strtoul(argv[4], NULL, 16);
874 ulong n;
wdenkc6097192002-11-03 00:24:07 +0000875
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200876#ifdef CONFIG_SYS_64BIT_LBA
Simon Glass43a464a2016-05-01 11:36:00 -0600877 lbaint_t blk = simple_strtoull(argv[3], NULL, 16);
878
879 printf("\nIDE write: device %d block # %lld, count %ld...",
880 curr_device, blk, cnt);
Guennadi Liakhovetskib71f1ca2008-04-28 14:36:06 +0200881#else
Simon Glass43a464a2016-05-01 11:36:00 -0600882 lbaint_t blk = simple_strtoul(argv[3], NULL, 16);
883
884 printf("\nIDE write: device %d block # %ld, count %ld...",
885 curr_device, blk, cnt);
Guennadi Liakhovetskib71f1ca2008-04-28 14:36:06 +0200886#endif
Simon Glass43a464a2016-05-01 11:36:00 -0600887 n = ide_write(&ide_dev_desc[curr_device], blk, cnt,
888 (ulong *)addr);
889
890 printf("%ld blocks written: %s\n", n,
891 n == cnt ? "OK" : "ERROR");
892 if (n == cnt)
893 return 0;
894 else
895 return 1;
896 } else {
897 return CMD_RET_USAGE;
wdenkf602aa02004-03-13 23:29:43 +0000898 }
wdenkf602aa02004-03-13 23:29:43 +0000899
Simon Glass43a464a2016-05-01 11:36:00 -0600900 return rcode;
901 }
902}
wdenkf602aa02004-03-13 23:29:43 +0000903
Simon Glass43a464a2016-05-01 11:36:00 -0600904int do_diskboot(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
905{
906 return common_diskboot(cmdtp, "ide", argc, argv);
907}
wdenkc6097192002-11-03 00:24:07 +0000908
Simon Glass43a464a2016-05-01 11:36:00 -0600909/* ------------------------------------------------------------------------- */
wdenkc6097192002-11-03 00:24:07 +0000910
Simon Glass43a464a2016-05-01 11:36:00 -0600911__weak void ide_led(uchar led, uchar status)
912{
913#if defined(CONFIG_IDE_LED) && defined(PER8_BASE) /* required by LED_PORT */
914 static uchar led_buffer; /* Buffer for current LED status */
wdenkc6097192002-11-03 00:24:07 +0000915
Simon Glass43a464a2016-05-01 11:36:00 -0600916 uchar *led_port = LED_PORT;
wdenkc6097192002-11-03 00:24:07 +0000917
Simon Glass43a464a2016-05-01 11:36:00 -0600918 if (status) /* switch LED on */
919 led_buffer |= led;
920 else /* switch LED off */
921 led_buffer &= ~led;
wdenkc6097192002-11-03 00:24:07 +0000922
Simon Glass43a464a2016-05-01 11:36:00 -0600923 *led_port = led_buffer;
924#endif
wdenkc6097192002-11-03 00:24:07 +0000925}
926
927/* ------------------------------------------------------------------------- */
928
Simon Glass43a464a2016-05-01 11:36:00 -0600929__weak void ide_outb(int dev, int port, unsigned char val)
930{
931 debug("ide_outb (dev= %d, port= 0x%x, val= 0x%02x) : @ 0x%08lx\n",
932 dev, port, val,
933 (ATA_CURR_BASE(dev) + CONFIG_SYS_ATA_PORT_ADDR(port)));
wdenkc6097192002-11-03 00:24:07 +0000934
Simon Glass43a464a2016-05-01 11:36:00 -0600935#if defined(CONFIG_IDE_AHB)
936 if (port) {
937 /* write command */
938 ide_write_register(dev, port, val);
939 } else {
940 /* write data */
941 outb(val, (ATA_CURR_BASE(dev)));
942 }
943#else
944 outb(val, (ATA_CURR_BASE(dev) + CONFIG_SYS_ATA_PORT_ADDR(port)));
945#endif
946}
947
948__weak unsigned char ide_inb(int dev, int port)
wdenkc6097192002-11-03 00:24:07 +0000949{
Simon Glass43a464a2016-05-01 11:36:00 -0600950 uchar val;
951
952#if defined(CONFIG_IDE_AHB)
953 val = ide_read_register(dev, port);
954#else
955 val = inb((ATA_CURR_BASE(dev) + CONFIG_SYS_ATA_PORT_ADDR(port)));
956#endif
957
958 debug("ide_inb (dev= %d, port= 0x%x) : @ 0x%08lx -> 0x%02x\n",
959 dev, port,
960 (ATA_CURR_BASE(dev) + CONFIG_SYS_ATA_PORT_ADDR(port)), val);
961 return val;
962}
963
964void ide_init(void)
965{
wdenkc6097192002-11-03 00:24:07 +0000966 unsigned char c;
Simon Glass43a464a2016-05-01 11:36:00 -0600967 int i, bus;
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000968
Simon Glass43a464a2016-05-01 11:36:00 -0600969#ifdef CONFIG_IDE_8xx_PCCARD
970 extern int ide_devices_found; /* Initialized in check_ide_device() */
971#endif /* CONFIG_IDE_8xx_PCCARD */
wdenkf602aa02004-03-13 23:29:43 +0000972
Simon Glass43a464a2016-05-01 11:36:00 -0600973#ifdef CONFIG_IDE_PREINIT
974 WATCHDOG_RESET();
975
976 if (ide_preinit()) {
977 puts("ide_preinit failed\n");
978 return;
wdenkf602aa02004-03-13 23:29:43 +0000979 }
Simon Glass43a464a2016-05-01 11:36:00 -0600980#endif /* CONFIG_IDE_PREINIT */
wdenkc6097192002-11-03 00:24:07 +0000981
Simon Glass43a464a2016-05-01 11:36:00 -0600982 WATCHDOG_RESET();
wdenkc6097192002-11-03 00:24:07 +0000983
Simon Glass43a464a2016-05-01 11:36:00 -0600984 /*
985 * Reset the IDE just to be sure.
986 * Light LED's to show
wdenkc6097192002-11-03 00:24:07 +0000987 */
Simon Glass43a464a2016-05-01 11:36:00 -0600988 ide_led((LED_IDE1 | LED_IDE2), 1); /* LED's on */
wdenkc6097192002-11-03 00:24:07 +0000989
Simon Glass43a464a2016-05-01 11:36:00 -0600990 /* ATAPI Drives seems to need a proper IDE Reset */
991 ide_reset();
wdenkc6097192002-11-03 00:24:07 +0000992
Simon Glass43a464a2016-05-01 11:36:00 -0600993#ifdef CONFIG_IDE_INIT_POSTRESET
994 WATCHDOG_RESET();
wdenkc6097192002-11-03 00:24:07 +0000995
Simon Glass43a464a2016-05-01 11:36:00 -0600996 if (ide_init_postreset()) {
997 puts("ide_preinit_postreset failed\n");
998 return;
999 }
1000#endif /* CONFIG_IDE_INIT_POSTRESET */
1001
1002 /*
1003 * Wait for IDE to get ready.
1004 * According to spec, this can take up to 31 seconds!
1005 */
1006 for (bus = 0; bus < CONFIG_SYS_IDE_MAXBUS; ++bus) {
1007 int dev =
1008 bus * (CONFIG_SYS_IDE_MAXDEVICE /
1009 CONFIG_SYS_IDE_MAXBUS);
1010
1011#ifdef CONFIG_IDE_8xx_PCCARD
1012 /* Skip non-ide devices from probing */
1013 if ((ide_devices_found & (1 << bus)) == 0) {
1014 ide_led((LED_IDE1 | LED_IDE2), 0); /* LED's off */
1015 continue;
wdenkf602aa02004-03-13 23:29:43 +00001016 }
1017#endif
Simon Glass43a464a2016-05-01 11:36:00 -06001018 printf("Bus %d: ", bus);
wdenkf602aa02004-03-13 23:29:43 +00001019
Simon Glass43a464a2016-05-01 11:36:00 -06001020 ide_bus_ok[bus] = 0;
wdenkf602aa02004-03-13 23:29:43 +00001021
Simon Glass43a464a2016-05-01 11:36:00 -06001022 /* Select device
1023 */
1024 udelay(100000); /* 100 ms */
1025 ide_outb(dev, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(dev));
1026 udelay(100000); /* 100 ms */
1027 i = 0;
1028 do {
1029 udelay(10000); /* 10 ms */
wdenkc6097192002-11-03 00:24:07 +00001030
Simon Glass43a464a2016-05-01 11:36:00 -06001031 c = ide_inb(dev, ATA_STATUS);
1032 i++;
1033 if (i > (ATA_RESET_TIME * 100)) {
1034 puts("** Timeout **\n");
1035 /* LED's off */
1036 ide_led((LED_IDE1 | LED_IDE2), 0);
1037 return;
1038 }
1039 if ((i >= 100) && ((i % 100) == 0))
1040 putc('.');
wdenkc6097192002-11-03 00:24:07 +00001041
Simon Glass43a464a2016-05-01 11:36:00 -06001042 } while (c & ATA_STAT_BUSY);
wdenkc6097192002-11-03 00:24:07 +00001043
Simon Glass43a464a2016-05-01 11:36:00 -06001044 if (c & (ATA_STAT_BUSY | ATA_STAT_FAULT)) {
1045 puts("not available ");
1046 debug("Status = 0x%02X ", c);
1047#ifndef CONFIG_ATAPI /* ATAPI Devices do not set DRDY */
1048 } else if ((c & ATA_STAT_READY) == 0) {
1049 puts("not available ");
1050 debug("Status = 0x%02X ", c);
1051#endif
1052 } else {
1053 puts("OK ");
1054 ide_bus_ok[bus] = 1;
wdenkc6097192002-11-03 00:24:07 +00001055 }
Simon Glass43a464a2016-05-01 11:36:00 -06001056 WATCHDOG_RESET();
wdenkc6097192002-11-03 00:24:07 +00001057 }
wdenkc6097192002-11-03 00:24:07 +00001058
Simon Glass43a464a2016-05-01 11:36:00 -06001059 putc('\n');
wdenk18bd81e2004-03-17 01:13:07 +00001060
Simon Glass43a464a2016-05-01 11:36:00 -06001061 ide_led((LED_IDE1 | LED_IDE2), 0); /* LED's off */
wdenkc12081a2004-03-23 20:18:25 +00001062
Simon Glass43a464a2016-05-01 11:36:00 -06001063 curr_device = -1;
1064 for (i = 0; i < CONFIG_SYS_IDE_MAXDEVICE; ++i) {
1065 int led = (IDE_BUS(i) == 0) ? LED_IDE1 : LED_IDE2;
1066 ide_dev_desc[i].type = DEV_TYPE_UNKNOWN;
1067 ide_dev_desc[i].if_type = IF_TYPE_IDE;
1068 ide_dev_desc[i].devnum = i;
1069 ide_dev_desc[i].part_type = PART_TYPE_UNKNOWN;
1070 ide_dev_desc[i].blksz = 0;
1071 ide_dev_desc[i].log2blksz =
1072 LOG2_INVALID(typeof(ide_dev_desc[i].log2blksz));
1073 ide_dev_desc[i].lba = 0;
1074 ide_dev_desc[i].block_read = ide_read;
1075 ide_dev_desc[i].block_write = ide_write;
1076 if (!ide_bus_ok[IDE_BUS(i)])
1077 continue;
1078 ide_led(led, 1); /* LED on */
1079 ide_ident(&ide_dev_desc[i]);
1080 ide_led(led, 0); /* LED off */
1081 dev_print(&ide_dev_desc[i]);
wdenk18bd81e2004-03-17 01:13:07 +00001082
Simon Glass43a464a2016-05-01 11:36:00 -06001083 if ((ide_dev_desc[i].lba > 0) && (ide_dev_desc[i].blksz > 0)) {
1084 /* initialize partition type */
1085 part_init(&ide_dev_desc[i]);
1086 if (curr_device < 0)
1087 curr_device = i;
1088 }
wdenkc6097192002-11-03 00:24:07 +00001089 }
Simon Glass43a464a2016-05-01 11:36:00 -06001090 WATCHDOG_RESET();
wdenkc6097192002-11-03 00:24:07 +00001091}
1092
1093/* ------------------------------------------------------------------------- */
1094
Simon Glass43a464a2016-05-01 11:36:00 -06001095#ifdef CONFIG_PARTITIONS
1096struct blk_desc *ide_get_dev(int dev)
wdenkc6097192002-11-03 00:24:07 +00001097{
Simon Glass43a464a2016-05-01 11:36:00 -06001098 return (dev < CONFIG_SYS_IDE_MAXDEVICE) ? &ide_dev_desc[dev] : NULL;
wdenkc6097192002-11-03 00:24:07 +00001099}
Simon Glass43a464a2016-05-01 11:36:00 -06001100#endif
wdenkc6097192002-11-03 00:24:07 +00001101
1102/* ------------------------------------------------------------------------- */
1103
Simon Glass43a464a2016-05-01 11:36:00 -06001104/* We only need to swap data if we are running on a big endian cpu. */
1105#if defined(__LITTLE_ENDIAN)
1106__weak void ide_input_swap_data(int dev, ulong *sect_buf, int words)
wdenkc6097192002-11-03 00:24:07 +00001107{
Simon Glass43a464a2016-05-01 11:36:00 -06001108 ide_input_data(dev, sect_buf, words);
wdenkc6097192002-11-03 00:24:07 +00001109}
Simon Glass43a464a2016-05-01 11:36:00 -06001110#else
1111__weak void ide_input_swap_data(int dev, ulong *sect_buf, int words)
1112{
1113 volatile ushort *pbuf =
1114 (ushort *)(ATA_CURR_BASE(dev) + ATA_DATA_REG);
1115 ushort *dbuf = (ushort *)sect_buf;
wdenkc6097192002-11-03 00:24:07 +00001116
Simon Glass43a464a2016-05-01 11:36:00 -06001117 debug("in input swap data base for read is %lx\n",
1118 (unsigned long) pbuf);
wdenkc6097192002-11-03 00:24:07 +00001119
Simon Glass43a464a2016-05-01 11:36:00 -06001120 while (words--) {
1121#ifdef __MIPS__
1122 *dbuf++ = swab16p((u16 *)pbuf);
1123 *dbuf++ = swab16p((u16 *)pbuf);
1124#else
1125 *dbuf++ = ld_le16(pbuf);
1126 *dbuf++ = ld_le16(pbuf);
1127#endif /* !MIPS */
1128 }
Heiko Schocherffb293a2009-09-23 07:56:08 +02001129}
Simon Glass43a464a2016-05-01 11:36:00 -06001130#endif /* __LITTLE_ENDIAN */
wdenkc6097192002-11-03 00:24:07 +00001131
wdenkc6097192002-11-03 00:24:07 +00001132
Albert Aribaud036c6b42010-08-08 05:17:05 +05301133#if defined(CONFIG_IDE_SWAP_IO)
Simon Glass43a464a2016-05-01 11:36:00 -06001134__weak void ide_output_data(int dev, const ulong *sect_buf, int words)
wdenkc6097192002-11-03 00:24:07 +00001135{
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001136 ushort *dbuf;
1137 volatile ushort *pbuf;
wdenkc6097192002-11-03 00:24:07 +00001138
Simon Glass43a464a2016-05-01 11:36:00 -06001139 pbuf = (ushort *)(ATA_CURR_BASE(dev) + ATA_DATA_REG);
1140 dbuf = (ushort *)sect_buf;
1141 while (words--) {
1142 EIEIO;
1143 *pbuf = *dbuf++;
wdenk20c98a62004-04-23 20:32:05 +00001144 EIEIO;
wdenkf1276d42005-02-03 23:00:49 +00001145 *pbuf = *dbuf++;
wdenkc6097192002-11-03 00:24:07 +00001146 }
wdenkf1276d42005-02-03 23:00:49 +00001147}
Simon Glass43a464a2016-05-01 11:36:00 -06001148#else /* ! CONFIG_IDE_SWAP_IO */
1149__weak void ide_output_data(int dev, const ulong *sect_buf, int words)
1150{
1151#if defined(CONFIG_IDE_AHB)
1152 ide_write_data(dev, sect_buf, words);
1153#else
1154 outsw(ATA_CURR_BASE(dev) + ATA_DATA_REG, sect_buf, words << 1);
1155#endif
1156}
1157#endif /* CONFIG_IDE_SWAP_IO */
wdenkf1276d42005-02-03 23:00:49 +00001158
Simon Glass43a464a2016-05-01 11:36:00 -06001159#if defined(CONFIG_IDE_SWAP_IO)
1160__weak void ide_input_data(int dev, ulong *sect_buf, int words)
wdenkf1276d42005-02-03 23:00:49 +00001161{
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001162 ushort *dbuf;
1163 volatile ushort *pbuf;
wdenkf1276d42005-02-03 23:00:49 +00001164
Simon Glass43a464a2016-05-01 11:36:00 -06001165 pbuf = (ushort *)(ATA_CURR_BASE(dev) + ATA_DATA_REG);
1166 dbuf = (ushort *)sect_buf;
wdenkf1276d42005-02-03 23:00:49 +00001167
Simon Glass43a464a2016-05-01 11:36:00 -06001168 debug("in input data base for read is %lx\n", (unsigned long) pbuf);
wdenkf1276d42005-02-03 23:00:49 +00001169
Simon Glass43a464a2016-05-01 11:36:00 -06001170 while (words--) {
1171 EIEIO;
1172 *dbuf++ = *pbuf;
wdenkf1276d42005-02-03 23:00:49 +00001173 EIEIO;
1174 *dbuf++ = *pbuf;
1175 }
wdenkc6097192002-11-03 00:24:07 +00001176}
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001177#else /* ! CONFIG_IDE_SWAP_IO */
Simon Glass43a464a2016-05-01 11:36:00 -06001178__weak void ide_input_data(int dev, ulong *sect_buf, int words)
wdenk591dda52002-11-18 00:14:45 +00001179{
Simon Glass43a464a2016-05-01 11:36:00 -06001180#if defined(CONFIG_IDE_AHB)
1181 ide_read_data(dev, sect_buf, words);
1182#else
1183 insw(ATA_CURR_BASE(dev) + ATA_DATA_REG, sect_buf, words << 1);
1184#endif
wdenk591dda52002-11-18 00:14:45 +00001185}
1186
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001187#endif /* CONFIG_IDE_SWAP_IO */
wdenk591dda52002-11-18 00:14:45 +00001188
Simon Glass43a464a2016-05-01 11:36:00 -06001189/* ------------------------------------------------------------------------- */
1190
1191ulong ide_read(struct blk_desc *block_dev, lbaint_t blknr, lbaint_t blkcnt,
1192 void *buffer)
wdenkc6097192002-11-03 00:24:07 +00001193{
Simon Glass43a464a2016-05-01 11:36:00 -06001194 int device = block_dev->devnum;
1195 ulong n = 0;
1196 unsigned char c;
1197 unsigned char pwrsave = 0; /* power save */
wdenkc6097192002-11-03 00:24:07 +00001198
Simon Glass43a464a2016-05-01 11:36:00 -06001199#ifdef CONFIG_LBA48
1200 unsigned char lba48 = 0;
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001201
Simon Glass43a464a2016-05-01 11:36:00 -06001202 if (blknr & 0x0000fffff0000000ULL) {
1203 /* more than 28 bits used, use 48bit mode */
1204 lba48 = 1;
wdenkc6097192002-11-03 00:24:07 +00001205 }
Simon Glass43a464a2016-05-01 11:36:00 -06001206#endif
1207 debug("ide_read dev %d start " LBAF ", blocks " LBAF " buffer at %lX\n",
1208 device, blknr, blkcnt, (ulong) buffer);
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001209
1210 ide_led(DEVICE_LED(device), 1); /* LED on */
wdenkc6097192002-11-03 00:24:07 +00001211
1212 /* Select device
1213 */
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001214 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
Simon Glass43a464a2016-05-01 11:36:00 -06001215 c = ide_wait(device, IDE_TIME_OUT);
wdenkc6097192002-11-03 00:24:07 +00001216
Simon Glass43a464a2016-05-01 11:36:00 -06001217 if (c & ATA_STAT_BUSY) {
1218 printf("IDE read: device %d not ready\n", device);
1219 goto IDE_READ_E;
wdenkc6097192002-11-03 00:24:07 +00001220 }
wdenkc6097192002-11-03 00:24:07 +00001221
Simon Glass43a464a2016-05-01 11:36:00 -06001222 /* first check if the drive is in Powersaving mode, if yes,
1223 * increase the timeout value */
1224 ide_outb(device, ATA_COMMAND, ATA_CMD_CHK_PWR);
1225 udelay(50);
wdenkc6097192002-11-03 00:24:07 +00001226
Simon Glass43a464a2016-05-01 11:36:00 -06001227 c = ide_wait(device, IDE_TIME_OUT); /* can't take over 500 ms */
wdenkc6097192002-11-03 00:24:07 +00001228
Simon Glass43a464a2016-05-01 11:36:00 -06001229 if (c & ATA_STAT_BUSY) {
1230 printf("IDE read: device %d not ready\n", device);
1231 goto IDE_READ_E;
wdenkc6097192002-11-03 00:24:07 +00001232 }
Simon Glass43a464a2016-05-01 11:36:00 -06001233 if ((c & ATA_STAT_ERR) == ATA_STAT_ERR) {
1234 printf("No Powersaving mode %X\n", c);
1235 } else {
1236 c = ide_inb(device, ATA_SECT_CNT);
1237 debug("Powersaving %02X\n", c);
1238 if (c == 0)
1239 pwrsave = 1;
wdenkc6097192002-11-03 00:24:07 +00001240 }
wdenk452cfd62002-11-19 11:04:11 +00001241
wdenkc6097192002-11-03 00:24:07 +00001242
Simon Glass43a464a2016-05-01 11:36:00 -06001243 while (blkcnt-- > 0) {
wdenkc6097192002-11-03 00:24:07 +00001244
Simon Glass43a464a2016-05-01 11:36:00 -06001245 c = ide_wait(device, IDE_TIME_OUT);
wdenkc6097192002-11-03 00:24:07 +00001246
Simon Glass43a464a2016-05-01 11:36:00 -06001247 if (c & ATA_STAT_BUSY) {
1248 printf("IDE read: device %d not ready\n", device);
1249 break;
1250 }
1251#ifdef CONFIG_LBA48
1252 if (lba48) {
1253 /* write high bits */
1254 ide_outb(device, ATA_SECT_CNT, 0);
1255 ide_outb(device, ATA_LBA_LOW, (blknr >> 24) & 0xFF);
1256#ifdef CONFIG_SYS_64BIT_LBA
1257 ide_outb(device, ATA_LBA_MID, (blknr >> 32) & 0xFF);
1258 ide_outb(device, ATA_LBA_HIGH, (blknr >> 40) & 0xFF);
1259#else
1260 ide_outb(device, ATA_LBA_MID, 0);
1261 ide_outb(device, ATA_LBA_HIGH, 0);
1262#endif
1263 }
1264#endif
1265 ide_outb(device, ATA_SECT_CNT, 1);
1266 ide_outb(device, ATA_LBA_LOW, (blknr >> 0) & 0xFF);
1267 ide_outb(device, ATA_LBA_MID, (blknr >> 8) & 0xFF);
1268 ide_outb(device, ATA_LBA_HIGH, (blknr >> 16) & 0xFF);
wdenkc6097192002-11-03 00:24:07 +00001269
Simon Glass43a464a2016-05-01 11:36:00 -06001270#ifdef CONFIG_LBA48
1271 if (lba48) {
1272 ide_outb(device, ATA_DEV_HD,
1273 ATA_LBA | ATA_DEVICE(device));
1274 ide_outb(device, ATA_COMMAND, ATA_CMD_READ_EXT);
wdenkc6097192002-11-03 00:24:07 +00001275
Simon Glass43a464a2016-05-01 11:36:00 -06001276 } else
1277#endif
1278 {
1279 ide_outb(device, ATA_DEV_HD, ATA_LBA |
1280 ATA_DEVICE(device) | ((blknr >> 24) & 0xF));
1281 ide_outb(device, ATA_COMMAND, ATA_CMD_READ);
1282 }
wdenkc6097192002-11-03 00:24:07 +00001283
Simon Glass43a464a2016-05-01 11:36:00 -06001284 udelay(50);
wdenkc6097192002-11-03 00:24:07 +00001285
Simon Glass43a464a2016-05-01 11:36:00 -06001286 if (pwrsave) {
1287 /* may take up to 4 sec */
1288 c = ide_wait(device, IDE_SPIN_UP_TIME_OUT);
1289 pwrsave = 0;
1290 } else {
1291 /* can't take over 500 ms */
1292 c = ide_wait(device, IDE_TIME_OUT);
1293 }
wdenkc6097192002-11-03 00:24:07 +00001294
Simon Glass43a464a2016-05-01 11:36:00 -06001295 if ((c & (ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR)) !=
1296 ATA_STAT_DRQ) {
1297 printf("Error (no IRQ) dev %d blk " LBAF
1298 ": status %#02x\n", device, blknr, c);
1299 break;
1300 }
wdenkc6097192002-11-03 00:24:07 +00001301
Simon Glass43a464a2016-05-01 11:36:00 -06001302 ide_input_data(device, buffer, ATA_SECTORWORDS);
1303 (void) ide_inb(device, ATA_STATUS); /* clear IRQ */
wdenkc6097192002-11-03 00:24:07 +00001304
Simon Glass43a464a2016-05-01 11:36:00 -06001305 ++n;
1306 ++blknr;
1307 buffer += ATA_BLOCKSIZE;
1308 }
1309IDE_READ_E:
1310 ide_led(DEVICE_LED(device), 0); /* LED off */
1311 return n;
1312}
wdenkc6097192002-11-03 00:24:07 +00001313
Simon Glass43a464a2016-05-01 11:36:00 -06001314/* ------------------------------------------------------------------------- */
wdenkc6097192002-11-03 00:24:07 +00001315
wdenkc6097192002-11-03 00:24:07 +00001316
Simon Glass43a464a2016-05-01 11:36:00 -06001317ulong ide_write(struct blk_desc *block_dev, lbaint_t blknr, lbaint_t blkcnt,
1318 const void *buffer)
1319{
1320 int device = block_dev->devnum;
1321 ulong n = 0;
1322 unsigned char c;
wdenkc6097192002-11-03 00:24:07 +00001323
Simon Glass43a464a2016-05-01 11:36:00 -06001324#ifdef CONFIG_LBA48
1325 unsigned char lba48 = 0;
wdenkc6097192002-11-03 00:24:07 +00001326
Simon Glass43a464a2016-05-01 11:36:00 -06001327 if (blknr & 0x0000fffff0000000ULL) {
1328 /* more than 28 bits used, use 48bit mode */
1329 lba48 = 1;
1330 }
1331#endif
wdenkc6097192002-11-03 00:24:07 +00001332
Simon Glass43a464a2016-05-01 11:36:00 -06001333 ide_led(DEVICE_LED(device), 1); /* LED on */
wdenkc6097192002-11-03 00:24:07 +00001334
Simon Glass43a464a2016-05-01 11:36:00 -06001335 /* Select device
1336 */
1337 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
wdenkc6097192002-11-03 00:24:07 +00001338
Simon Glass43a464a2016-05-01 11:36:00 -06001339 while (blkcnt-- > 0) {
1340 c = ide_wait(device, IDE_TIME_OUT);
wdenkc6097192002-11-03 00:24:07 +00001341
Simon Glass43a464a2016-05-01 11:36:00 -06001342 if (c & ATA_STAT_BUSY) {
1343 printf("IDE read: device %d not ready\n", device);
1344 goto WR_OUT;
1345 }
1346#ifdef CONFIG_LBA48
1347 if (lba48) {
1348 /* write high bits */
1349 ide_outb(device, ATA_SECT_CNT, 0);
1350 ide_outb(device, ATA_LBA_LOW, (blknr >> 24) & 0xFF);
1351#ifdef CONFIG_SYS_64BIT_LBA
1352 ide_outb(device, ATA_LBA_MID, (blknr >> 32) & 0xFF);
1353 ide_outb(device, ATA_LBA_HIGH, (blknr >> 40) & 0xFF);
1354#else
1355 ide_outb(device, ATA_LBA_MID, 0);
1356 ide_outb(device, ATA_LBA_HIGH, 0);
1357#endif
1358 }
1359#endif
1360 ide_outb(device, ATA_SECT_CNT, 1);
1361 ide_outb(device, ATA_LBA_LOW, (blknr >> 0) & 0xFF);
1362 ide_outb(device, ATA_LBA_MID, (blknr >> 8) & 0xFF);
1363 ide_outb(device, ATA_LBA_HIGH, (blknr >> 16) & 0xFF);
wdenkc6097192002-11-03 00:24:07 +00001364
Simon Glass43a464a2016-05-01 11:36:00 -06001365#ifdef CONFIG_LBA48
1366 if (lba48) {
1367 ide_outb(device, ATA_DEV_HD,
1368 ATA_LBA | ATA_DEVICE(device));
1369 ide_outb(device, ATA_COMMAND, ATA_CMD_WRITE_EXT);
wdenkc6097192002-11-03 00:24:07 +00001370
Simon Glass43a464a2016-05-01 11:36:00 -06001371 } else
1372#endif
1373 {
1374 ide_outb(device, ATA_DEV_HD, ATA_LBA |
1375 ATA_DEVICE(device) | ((blknr >> 24) & 0xF));
1376 ide_outb(device, ATA_COMMAND, ATA_CMD_WRITE);
1377 }
wdenkc6097192002-11-03 00:24:07 +00001378
Simon Glass43a464a2016-05-01 11:36:00 -06001379 udelay(50);
wdenkc6097192002-11-03 00:24:07 +00001380
Simon Glass43a464a2016-05-01 11:36:00 -06001381 /* can't take over 500 ms */
1382 c = ide_wait(device, IDE_TIME_OUT);
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001383
Simon Glass43a464a2016-05-01 11:36:00 -06001384 if ((c & (ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR)) !=
1385 ATA_STAT_DRQ) {
1386 printf("Error (no IRQ) dev %d blk " LBAF
1387 ": status %#02x\n", device, blknr, c);
1388 goto WR_OUT;
wdenkc6097192002-11-03 00:24:07 +00001389 }
Simon Glass43a464a2016-05-01 11:36:00 -06001390
1391 ide_output_data(device, buffer, ATA_SECTORWORDS);
1392 c = ide_inb(device, ATA_STATUS); /* clear IRQ */
1393 ++n;
1394 ++blknr;
1395 buffer += ATA_BLOCKSIZE;
1396 }
1397WR_OUT:
1398 ide_led(DEVICE_LED(device), 0); /* LED off */
Simon Glass73e43142016-05-01 11:35:59 -06001399 return n;
wdenkc6097192002-11-03 00:24:07 +00001400}
1401
1402/* ------------------------------------------------------------------------- */
1403
Simon Glass43a464a2016-05-01 11:36:00 -06001404#if defined(CONFIG_OF_IDE_FIXUP)
1405int ide_device_present(int dev)
1406{
1407 if (dev >= CONFIG_SYS_IDE_MAXBUS)
1408 return 0;
1409 return ide_dev_desc[dev].type == DEV_TYPE_UNKNOWN ? 0 : 1;
1410}
1411#endif
1412/* ------------------------------------------------------------------------- */
wdenkc6097192002-11-03 00:24:07 +00001413
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001414U_BOOT_CMD(ide, 5, 1, do_ide,
1415 "IDE sub-system",
1416 "reset - reset IDE controller\n"
1417 "ide info - show available IDE devices\n"
1418 "ide device [dev] - show or set current device\n"
1419 "ide part [dev] - print partition table of one or all IDE devices\n"
1420 "ide read addr blk# cnt\n"
1421 "ide write addr blk# cnt - read/write `cnt'"
1422 " blocks starting at block `blk#'\n"
1423 " to/from memory address `addr'");
wdenk57b2d802003-06-27 21:31:46 +00001424
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001425U_BOOT_CMD(diskboot, 3, 1, do_diskboot,
1426 "boot from IDE device", "loadAddr dev:part");