blob: f3c0425d9892e73d185fdaec0c0a5f04f9c47cdb [file] [log] [blame]
wdenkc6097192002-11-03 00:24:07 +00001/*
Wolfgang Denk34c202c2011-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 Denk1a459662013-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 Aribaud113bfe42010-08-08 05:17:06 +053011
wdenkc6097192002-11-03 00:24:07 +000012#include <common.h>
13#include <config.h>
14#include <watchdog.h>
15#include <command.h>
16#include <image.h>
17#include <asm/byteorder.h>
Heiko Schocherf98984c2007-08-28 17:39:14 +020018#include <asm/io.h>
Grant Likely735dd972007-02-20 09:04:34 +010019
wdenkc6097192002-11-03 00:24:07 +000020#if defined(CONFIG_IDE_8xx_DIRECT) || defined(CONFIG_IDE_PCMCIA)
21# include <pcmcia.h>
22#endif
Grant Likely735dd972007-02-20 09:04:34 +010023
wdenkc6097192002-11-03 00:24:07 +000024#include <ide.h>
25#include <ata.h>
Grant Likely735dd972007-02-20 09:04:34 +010026
wdenkc6097192002-11-03 00:24:07 +000027#ifdef CONFIG_STATUS_LED
28# include <status_led.h>
29#endif
Grant Likely735dd972007-02-20 09:04:34 +010030
wdenk5cf91d62004-04-23 20:32:05 +000031#ifdef __PPC__
32# define EIEIO __asm__ volatile ("eieio")
wdenk1a344f22005-02-03 23:00:49 +000033# define SYNC __asm__ volatile ("sync")
wdenk5cf91d62004-04-23 20:32:05 +000034#else
35# define EIEIO /* nothing */
wdenk1a344f22005-02-03 23:00:49 +000036# define SYNC /* nothing */
wdenkc6097192002-11-03 00:24:07 +000037#endif
38
wdenkc6097192002-11-03 00:24:07 +000039/* ------------------------------------------------------------------------- */
40
41/* Current I/O Device */
42static int curr_device = -1;
43
44/* Current offset for IDE0 / IDE1 bus access */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020045ulong ide_bus_offset[CONFIG_SYS_IDE_MAXBUS] = {
46#if defined(CONFIG_SYS_ATA_IDE0_OFFSET)
47 CONFIG_SYS_ATA_IDE0_OFFSET,
wdenkc6097192002-11-03 00:24:07 +000048#endif
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020049#if defined(CONFIG_SYS_ATA_IDE1_OFFSET) && (CONFIG_SYS_IDE_MAXBUS > 1)
50 CONFIG_SYS_ATA_IDE1_OFFSET,
wdenkc6097192002-11-03 00:24:07 +000051#endif
52};
53
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020054static int ide_bus_ok[CONFIG_SYS_IDE_MAXBUS];
wdenkc6097192002-11-03 00:24:07 +000055
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020056block_dev_desc_t ide_dev_desc[CONFIG_SYS_IDE_MAXDEVICE];
wdenkc6097192002-11-03 00:24:07 +000057/* ------------------------------------------------------------------------- */
58
wdenkc6097192002-11-03 00:24:07 +000059#ifdef CONFIG_IDE_RESET
60static void ide_reset (void);
61#else
62#define ide_reset() /* dummy */
63#endif
64
65static void ide_ident (block_dev_desc_t *dev_desc);
66static uchar ide_wait (int dev, ulong t);
67
68#define IDE_TIME_OUT 2000 /* 2 sec timeout */
69
70#define ATAPI_TIME_OUT 7000 /* 7 sec timeout (5 sec seems to work...) */
71
72#define IDE_SPIN_UP_TIME_OUT 5000 /* 5 sec spin-up timeout */
73
wdenkc6097192002-11-03 00:24:07 +000074static void ident_cpy (unsigned char *dest, unsigned char *src, unsigned int len);
75
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020076#ifndef CONFIG_SYS_ATA_PORT_ADDR
77#define CONFIG_SYS_ATA_PORT_ADDR(port) (port)
Heiko Schocher566a4942007-06-22 19:11:54 +020078#endif
wdenkc6097192002-11-03 00:24:07 +000079
80#ifdef CONFIG_ATAPI
81static void atapi_inquiry(block_dev_desc_t *dev_desc);
Simon Glassb6458d32012-10-29 05:24:04 +000082static ulong atapi_read(int device, ulong blknr, lbaint_t blkcnt,
83 void *buffer);
wdenkc6097192002-11-03 00:24:07 +000084#endif
85
86
wdenkc6097192002-11-03 00:24:07 +000087/* ------------------------------------------------------------------------- */
88
Wolfgang Denk34c202c2011-10-29 09:41:40 +000089int do_ide(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
wdenkc6097192002-11-03 00:24:07 +000090{
Wolfgang Denk34c202c2011-10-29 09:41:40 +000091 int rcode = 0;
wdenkc6097192002-11-03 00:24:07 +000092
Wolfgang Denk34c202c2011-10-29 09:41:40 +000093 switch (argc) {
94 case 0:
95 case 1:
Simon Glass4c12eeb2011-12-10 08:44:01 +000096 return CMD_RET_USAGE;
Wolfgang Denk34c202c2011-10-29 09:41:40 +000097 case 2:
98 if (strncmp(argv[1], "res", 3) == 0) {
99 puts("\nReset IDE"
100#ifdef CONFIG_IDE_8xx_DIRECT
101 " on PCMCIA " PCMCIA_SLOT_MSG
102#endif
103 ": ");
wdenkc6097192002-11-03 00:24:07 +0000104
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000105 ide_init();
106 return 0;
107 } else if (strncmp(argv[1], "inf", 3) == 0) {
108 int i;
109
110 putc('\n');
111
112 for (i = 0; i < CONFIG_SYS_IDE_MAXDEVICE; ++i) {
113 if (ide_dev_desc[i].type == DEV_TYPE_UNKNOWN)
114 continue; /* list only known devices */
115 printf("IDE device %d: ", i);
116 dev_print(&ide_dev_desc[i]);
117 }
118 return 0;
119
120 } else if (strncmp(argv[1], "dev", 3) == 0) {
121 if ((curr_device < 0)
122 || (curr_device >= CONFIG_SYS_IDE_MAXDEVICE)) {
123 puts("\nno IDE devices available\n");
124 return 1;
125 }
126 printf("\nIDE device %d: ", curr_device);
127 dev_print(&ide_dev_desc[curr_device]);
128 return 0;
129 } else if (strncmp(argv[1], "part", 4) == 0) {
130 int dev, ok;
131
132 for (ok = 0, dev = 0;
133 dev < CONFIG_SYS_IDE_MAXDEVICE;
134 ++dev) {
135 if (ide_dev_desc[dev].part_type !=
136 PART_TYPE_UNKNOWN) {
137 ++ok;
138 if (dev)
139 putc('\n');
140 print_part(&ide_dev_desc[dev]);
141 }
142 }
143 if (!ok) {
144 puts("\nno IDE devices available\n");
145 rcode++;
146 }
147 return rcode;
148 }
Simon Glass4c12eeb2011-12-10 08:44:01 +0000149 return CMD_RET_USAGE;
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000150 case 3:
151 if (strncmp(argv[1], "dev", 3) == 0) {
152 int dev = (int) simple_strtoul(argv[2], NULL, 10);
153
154 printf("\nIDE device %d: ", dev);
155 if (dev >= CONFIG_SYS_IDE_MAXDEVICE) {
156 puts("unknown device\n");
157 return 1;
158 }
159 dev_print(&ide_dev_desc[dev]);
160 /*ide_print (dev); */
161
162 if (ide_dev_desc[dev].type == DEV_TYPE_UNKNOWN)
163 return 1;
164
165 curr_device = dev;
166
167 puts("... is now current device\n");
168
169 return 0;
170 } else if (strncmp(argv[1], "part", 4) == 0) {
171 int dev = (int) simple_strtoul(argv[2], NULL, 10);
172
173 if (ide_dev_desc[dev].part_type != PART_TYPE_UNKNOWN) {
174 print_part(&ide_dev_desc[dev]);
175 } else {
176 printf("\nIDE device %d not available\n",
177 dev);
178 rcode = 1;
179 }
180 return rcode;
181 }
182
Simon Glass4c12eeb2011-12-10 08:44:01 +0000183 return CMD_RET_USAGE;
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000184 default:
185 /* at least 4 args */
186
187 if (strcmp(argv[1], "read") == 0) {
188 ulong addr = simple_strtoul(argv[2], NULL, 16);
189 ulong cnt = simple_strtoul(argv[4], NULL, 16);
190 ulong n;
191
192#ifdef CONFIG_SYS_64BIT_LBA
193 lbaint_t blk = simple_strtoull(argv[3], NULL, 16);
194
195 printf("\nIDE read: device %d block # %lld, count %ld ... ",
196 curr_device, blk, cnt);
197#else
198 lbaint_t blk = simple_strtoul(argv[3], NULL, 16);
199
200 printf("\nIDE read: device %d block # %ld, count %ld ... ",
201 curr_device, blk, cnt);
202#endif
203
204 n = ide_dev_desc[curr_device].block_read(curr_device,
205 blk, cnt,
206 (ulong *)addr);
207 /* flush cache after read */
208 flush_cache(addr,
209 cnt * ide_dev_desc[curr_device].blksz);
210
211 printf("%ld blocks read: %s\n",
212 n, (n == cnt) ? "OK" : "ERROR");
213 if (n == cnt)
214 return 0;
215 else
216 return 1;
217 } else if (strcmp(argv[1], "write") == 0) {
218 ulong addr = simple_strtoul(argv[2], NULL, 16);
219 ulong cnt = simple_strtoul(argv[4], NULL, 16);
220 ulong n;
221
222#ifdef CONFIG_SYS_64BIT_LBA
223 lbaint_t blk = simple_strtoull(argv[3], NULL, 16);
224
225 printf("\nIDE write: device %d block # %lld, count %ld ... ",
226 curr_device, blk, cnt);
227#else
228 lbaint_t blk = simple_strtoul(argv[3], NULL, 16);
229
230 printf("\nIDE write: device %d block # %ld, count %ld ... ",
231 curr_device, blk, cnt);
232#endif
233 n = ide_write(curr_device, blk, cnt, (ulong *) addr);
234
235 printf("%ld blocks written: %s\n",
236 n, (n == cnt) ? "OK" : "ERROR");
237 if (n == cnt)
238 return 0;
239 else
240 return 1;
241 } else {
Simon Glass4c12eeb2011-12-10 08:44:01 +0000242 return CMD_RET_USAGE;
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000243 }
244
245 return rcode;
246 }
wdenkc6097192002-11-03 00:24:07 +0000247}
248
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000249int do_diskboot(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
wdenkc6097192002-11-03 00:24:07 +0000250{
Rob Herring7405a132012-09-21 04:02:30 +0000251 return common_diskboot(cmdtp, "ide", argc, argv);
wdenkc6097192002-11-03 00:24:07 +0000252}
253
254/* ------------------------------------------------------------------------- */
255
Pavel Herrmann19be2ea2012-10-07 05:56:10 +0000256void __ide_led(uchar led, uchar status)
257{
258#if defined(CONFIG_IDE_LED) && defined(PER8_BASE) /* required by LED_PORT */
259 static uchar led_buffer; /* Buffer for current LED status */
260
261 uchar *led_port = LED_PORT;
262
263 if (status) /* switch LED on */
264 led_buffer |= led;
265 else /* switch LED off */
266 led_buffer &= ~led;
267
268 *led_port = led_buffer;
269#endif
270}
271
272void ide_led(uchar led, uchar status)
273 __attribute__ ((weak, alias("__ide_led")));
274
275#ifndef CONFIG_IDE_LED /* define LED macros, they are not used anyways */
276# define DEVICE_LED(x) 0
277# define LED_IDE1 1
278# define LED_IDE2 2
279#endif
280
281/* ------------------------------------------------------------------------- */
282
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000283inline void __ide_outb(int dev, int port, unsigned char val)
Stefan Roesef2302d42008-08-06 14:05:38 +0200284{
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000285 debug("ide_outb (dev= %d, port= 0x%x, val= 0x%02x) : @ 0x%08lx\n",
286 dev, port, val,
287 (ATA_CURR_BASE(dev) + CONFIG_SYS_ATA_PORT_ADDR(port)));
Macpaul Lin0abddf82011-04-11 20:45:32 +0000288
289#if defined(CONFIG_IDE_AHB)
290 if (port) {
291 /* write command */
292 ide_write_register(dev, port, val);
293 } else {
294 /* write data */
295 outb(val, (ATA_CURR_BASE(dev)));
296 }
297#else
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000298 outb(val, (ATA_CURR_BASE(dev) + CONFIG_SYS_ATA_PORT_ADDR(port)));
Macpaul Lin0abddf82011-04-11 20:45:32 +0000299#endif
Stefan Roesef2302d42008-08-06 14:05:38 +0200300}
Macpaul Lin0abddf82011-04-11 20:45:32 +0000301
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000302void ide_outb(int dev, int port, unsigned char val)
303 __attribute__ ((weak, alias("__ide_outb")));
Stefan Roesef2302d42008-08-06 14:05:38 +0200304
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000305inline unsigned char __ide_inb(int dev, int port)
Stefan Roesef2302d42008-08-06 14:05:38 +0200306{
307 uchar val;
Macpaul Lin0abddf82011-04-11 20:45:32 +0000308
309#if defined(CONFIG_IDE_AHB)
310 val = ide_read_register(dev, port);
311#else
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000312 val = inb((ATA_CURR_BASE(dev) + CONFIG_SYS_ATA_PORT_ADDR(port)));
Macpaul Lin0abddf82011-04-11 20:45:32 +0000313#endif
314
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000315 debug("ide_inb (dev= %d, port= 0x%x) : @ 0x%08lx -> 0x%02x\n",
316 dev, port,
317 (ATA_CURR_BASE(dev) + CONFIG_SYS_ATA_PORT_ADDR(port)), val);
Stefan Roesef2302d42008-08-06 14:05:38 +0200318 return val;
319}
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000320
Kim Phillips2df72b82009-05-19 12:53:36 -0500321unsigned char ide_inb(int dev, int port)
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000322 __attribute__ ((weak, alias("__ide_inb")));
Stefan Roesef2302d42008-08-06 14:05:38 +0200323
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000324void ide_init(void)
wdenkc6097192002-11-03 00:24:07 +0000325{
wdenkc6097192002-11-03 00:24:07 +0000326 unsigned char c;
327 int i, bus;
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000328
wdenk9fd5e312003-12-07 23:55:12 +0000329#ifdef CONFIG_IDE_8xx_PCCARD
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000330 extern int ide_devices_found; /* Initialized in check_ide_device() */
331#endif /* CONFIG_IDE_8xx_PCCARD */
wdenk9fd5e312003-12-07 23:55:12 +0000332
333#ifdef CONFIG_IDE_PREINIT
334 WATCHDOG_RESET();
335
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000336 if (ide_preinit()) {
337 puts("ide_preinit failed\n");
wdenk9fd5e312003-12-07 23:55:12 +0000338 return;
339 }
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000340#endif /* CONFIG_IDE_PREINIT */
wdenkc6097192002-11-03 00:24:07 +0000341
wdenkc6097192002-11-03 00:24:07 +0000342 WATCHDOG_RESET();
343
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000344 /*
345 * Reset the IDE just to be sure.
wdenkc6097192002-11-03 00:24:07 +0000346 * Light LED's to show
347 */
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000348 ide_led((LED_IDE1 | LED_IDE2), 1); /* LED's on */
349
350 /* ATAPI Drives seems to need a proper IDE Reset */
351 ide_reset();
wdenkc6097192002-11-03 00:24:07 +0000352
Pavel Herrmann8d1165e11a2012-10-09 07:01:56 +0000353#ifdef CONFIG_IDE_INIT_POSTRESET
354 WATCHDOG_RESET();
wdenkc6097192002-11-03 00:24:07 +0000355
Pavel Herrmann8d1165e11a2012-10-09 07:01:56 +0000356 if (ide_init_postreset()) {
357 puts("ide_preinit_postreset failed\n");
358 return;
359 }
360#endif /* CONFIG_IDE_INIT_POSTRESET */
wdenkc6097192002-11-03 00:24:07 +0000361
362 /*
363 * Wait for IDE to get ready.
364 * According to spec, this can take up to 31 seconds!
365 */
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000366 for (bus = 0; bus < CONFIG_SYS_IDE_MAXBUS; ++bus) {
367 int dev =
368 bus * (CONFIG_SYS_IDE_MAXDEVICE /
369 CONFIG_SYS_IDE_MAXBUS);
wdenkc6097192002-11-03 00:24:07 +0000370
wdenk6069ff22003-02-28 00:49:47 +0000371#ifdef CONFIG_IDE_8xx_PCCARD
372 /* Skip non-ide devices from probing */
373 if ((ide_devices_found & (1 << bus)) == 0) {
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000374 ide_led((LED_IDE1 | LED_IDE2), 0); /* LED's off */
wdenk6069ff22003-02-28 00:49:47 +0000375 continue;
376 }
377#endif
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000378 printf("Bus %d: ", bus);
wdenkc6097192002-11-03 00:24:07 +0000379
380 ide_bus_ok[bus] = 0;
381
382 /* Select device
383 */
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000384 udelay(100000); /* 100 ms */
385 ide_outb(dev, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(dev));
386 udelay(100000); /* 100 ms */
wdenkc6097192002-11-03 00:24:07 +0000387 i = 0;
388 do {
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000389 udelay(10000); /* 10 ms */
wdenkc6097192002-11-03 00:24:07 +0000390
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000391 c = ide_inb(dev, ATA_STATUS);
wdenkc6097192002-11-03 00:24:07 +0000392 i++;
393 if (i > (ATA_RESET_TIME * 100)) {
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000394 puts("** Timeout **\n");
395 /* LED's off */
396 ide_led((LED_IDE1 | LED_IDE2), 0);
wdenkc6097192002-11-03 00:24:07 +0000397 return;
398 }
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000399 if ((i >= 100) && ((i % 100) == 0))
400 putc('.');
401
wdenkc6097192002-11-03 00:24:07 +0000402 } while (c & ATA_STAT_BUSY);
403
404 if (c & (ATA_STAT_BUSY | ATA_STAT_FAULT)) {
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000405 puts("not available ");
406 debug("Status = 0x%02X ", c);
407#ifndef CONFIG_ATAPI /* ATAPI Devices do not set DRDY */
408 } else if ((c & ATA_STAT_READY) == 0) {
409 puts("not available ");
410 debug("Status = 0x%02X ", c);
wdenkc6097192002-11-03 00:24:07 +0000411#endif
412 } else {
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000413 puts("OK ");
wdenkc6097192002-11-03 00:24:07 +0000414 ide_bus_ok[bus] = 1;
415 }
416 WATCHDOG_RESET();
417 }
wdenkc7de8292002-11-19 11:04:11 +0000418
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000419 putc('\n');
wdenkc6097192002-11-03 00:24:07 +0000420
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000421 ide_led((LED_IDE1 | LED_IDE2), 0); /* LED's off */
wdenkc6097192002-11-03 00:24:07 +0000422
423 curr_device = -1;
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000424 for (i = 0; i < CONFIG_SYS_IDE_MAXDEVICE; ++i) {
wdenkc6097192002-11-03 00:24:07 +0000425 int led = (IDE_BUS(i) == 0) ? LED_IDE1 : LED_IDE2;
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000426 ide_dev_desc[i].type = DEV_TYPE_UNKNOWN;
427 ide_dev_desc[i].if_type = IF_TYPE_IDE;
428 ide_dev_desc[i].dev = i;
429 ide_dev_desc[i].part_type = PART_TYPE_UNKNOWN;
430 ide_dev_desc[i].blksz = 0;
Egbert Eich0472fbf2013-04-09 21:11:56 +0000431 ide_dev_desc[i].log2blksz =
432 LOG2_INVALID(typeof(ide_dev_desc[i].log2blksz));
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000433 ide_dev_desc[i].lba = 0;
434 ide_dev_desc[i].block_read = ide_read;
Macpaul Lin0abddf82011-04-11 20:45:32 +0000435 ide_dev_desc[i].block_write = ide_write;
wdenkc6097192002-11-03 00:24:07 +0000436 if (!ide_bus_ok[IDE_BUS(i)])
437 continue;
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000438 ide_led(led, 1); /* LED on */
wdenkc6097192002-11-03 00:24:07 +0000439 ide_ident(&ide_dev_desc[i]);
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000440 ide_led(led, 0); /* LED off */
wdenkc6097192002-11-03 00:24:07 +0000441 dev_print(&ide_dev_desc[i]);
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000442
wdenkc6097192002-11-03 00:24:07 +0000443 if ((ide_dev_desc[i].lba > 0) && (ide_dev_desc[i].blksz > 0)) {
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000444 /* initialize partition type */
445 init_part(&ide_dev_desc[i]);
wdenkc6097192002-11-03 00:24:07 +0000446 if (curr_device < 0)
447 curr_device = i;
448 }
449 }
450 WATCHDOG_RESET();
451}
452
453/* ------------------------------------------------------------------------- */
454
Matthew McClintockdf3fc522011-05-24 05:31:19 +0000455#ifdef CONFIG_PARTITIONS
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000456block_dev_desc_t *ide_get_dev(int dev)
wdenkc6097192002-11-03 00:24:07 +0000457{
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200458 return (dev < CONFIG_SYS_IDE_MAXDEVICE) ? &ide_dev_desc[dev] : NULL;
wdenkc6097192002-11-03 00:24:07 +0000459}
Matthew McClintockdf3fc522011-05-24 05:31:19 +0000460#endif
wdenkc6097192002-11-03 00:24:07 +0000461
wdenkc6097192002-11-03 00:24:07 +0000462/* ------------------------------------------------------------------------- */
463
Pavel Herrmannf5b82c02012-10-09 07:04:39 +0000464void ide_input_swap_data(int dev, ulong *sect_buf, int words)
465 __attribute__ ((weak, alias("__ide_input_swap_data")));
466
467void ide_input_data(int dev, ulong *sect_buf, int words)
468 __attribute__ ((weak, alias("__ide_input_data")));
469
470void ide_output_data(int dev, const ulong *sect_buf, int words)
471 __attribute__ ((weak, alias("__ide_output_data")));
472
wdenk5da627a2003-10-09 20:09:04 +0000473/* We only need to swap data if we are running on a big endian cpu. */
Pavel Herrmann4d1361d2012-10-09 07:10:08 +0000474#if defined(__LITTLE_ENDIAN)
Pavel Herrmannf5b82c02012-10-09 07:04:39 +0000475void __ide_input_swap_data(int dev, ulong *sect_buf, int words)
476{
477 ide_input_data(dev, sect_buf, words);
478}
wdenk5da627a2003-10-09 20:09:04 +0000479#else
Pavel Herrmannf5b82c02012-10-09 07:04:39 +0000480void __ide_input_swap_data(int dev, ulong *sect_buf, int words)
wdenkc6097192002-11-03 00:24:07 +0000481{
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000482 volatile ushort *pbuf =
483 (ushort *) (ATA_CURR_BASE(dev) + ATA_DATA_REG);
484 ushort *dbuf = (ushort *) sect_buf;
wdenk1a344f22005-02-03 23:00:49 +0000485
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000486 debug("in input swap data base for read is %lx\n",
487 (unsigned long) pbuf);
wdenk1a344f22005-02-03 23:00:49 +0000488
489 while (words--) {
Wolfgang Denk0c32d962006-06-16 17:32:31 +0200490#ifdef __MIPS__
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000491 *dbuf++ = swab16p((u16 *) pbuf);
492 *dbuf++ = swab16p((u16 *) pbuf);
Wolfgang Denk0c32d962006-06-16 17:32:31 +0200493#else
wdenk1a344f22005-02-03 23:00:49 +0000494 *dbuf++ = ld_le16(pbuf);
495 *dbuf++ = ld_le16(pbuf);
Wolfgang Denk0c32d962006-06-16 17:32:31 +0200496#endif /* !MIPS */
wdenk1a344f22005-02-03 23:00:49 +0000497 }
wdenkc6097192002-11-03 00:24:07 +0000498}
Pavel Herrmann4d1361d2012-10-09 07:10:08 +0000499#endif /* __LITTLE_ENDIAN */
wdenkc6097192002-11-03 00:24:07 +0000500
wdenk2262cfe2002-11-18 00:14:45 +0000501
Albert Aribaudf2a37fc2010-08-08 05:17:05 +0530502#if defined(CONFIG_IDE_SWAP_IO)
Pavel Herrmannf5b82c02012-10-09 07:04:39 +0000503void __ide_output_data(int dev, const ulong *sect_buf, int words)
wdenkc6097192002-11-03 00:24:07 +0000504{
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000505 ushort *dbuf;
506 volatile ushort *pbuf;
wdenk1a344f22005-02-03 23:00:49 +0000507
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000508 pbuf = (ushort *) (ATA_CURR_BASE(dev) + ATA_DATA_REG);
509 dbuf = (ushort *) sect_buf;
wdenk1a344f22005-02-03 23:00:49 +0000510 while (words--) {
511 EIEIO;
512 *pbuf = *dbuf++;
513 EIEIO;
514 *pbuf = *dbuf++;
515 }
wdenkc6097192002-11-03 00:24:07 +0000516}
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000517#else /* ! CONFIG_IDE_SWAP_IO */
Pavel Herrmannf5b82c02012-10-09 07:04:39 +0000518void __ide_output_data(int dev, const ulong *sect_buf, int words)
wdenk2262cfe2002-11-18 00:14:45 +0000519{
Macpaul Lin0abddf82011-04-11 20:45:32 +0000520#if defined(CONFIG_IDE_AHB)
521 ide_write_data(dev, sect_buf, words);
522#else
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000523 outsw(ATA_CURR_BASE(dev) + ATA_DATA_REG, sect_buf, words << 1);
Macpaul Lin0abddf82011-04-11 20:45:32 +0000524#endif
wdenk2262cfe2002-11-18 00:14:45 +0000525}
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000526#endif /* CONFIG_IDE_SWAP_IO */
wdenkc6097192002-11-03 00:24:07 +0000527
Albert Aribaudf2a37fc2010-08-08 05:17:05 +0530528#if defined(CONFIG_IDE_SWAP_IO)
Pavel Herrmannf5b82c02012-10-09 07:04:39 +0000529void __ide_input_data(int dev, ulong *sect_buf, int words)
wdenkc6097192002-11-03 00:24:07 +0000530{
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000531 ushort *dbuf;
532 volatile ushort *pbuf;
wdenk1a344f22005-02-03 23:00:49 +0000533
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000534 pbuf = (ushort *) (ATA_CURR_BASE(dev) + ATA_DATA_REG);
535 dbuf = (ushort *) sect_buf;
wdenk1a344f22005-02-03 23:00:49 +0000536
537 debug("in input data base for read is %lx\n", (unsigned long) pbuf);
538
539 while (words--) {
540 EIEIO;
541 *dbuf++ = *pbuf;
542 EIEIO;
543 *dbuf++ = *pbuf;
544 }
wdenkc6097192002-11-03 00:24:07 +0000545}
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000546#else /* ! CONFIG_IDE_SWAP_IO */
Pavel Herrmannf5b82c02012-10-09 07:04:39 +0000547void __ide_input_data(int dev, ulong *sect_buf, int words)
wdenk2262cfe2002-11-18 00:14:45 +0000548{
Macpaul Lin0abddf82011-04-11 20:45:32 +0000549#if defined(CONFIG_IDE_AHB)
550 ide_read_data(dev, sect_buf, words);
551#else
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000552 insw(ATA_CURR_BASE(dev) + ATA_DATA_REG, sect_buf, words << 1);
Macpaul Lin0abddf82011-04-11 20:45:32 +0000553#endif
wdenk2262cfe2002-11-18 00:14:45 +0000554}
555
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000556#endif /* CONFIG_IDE_SWAP_IO */
wdenkc6097192002-11-03 00:24:07 +0000557
558/* -------------------------------------------------------------------------
559 */
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000560static void ide_ident(block_dev_desc_t *dev_desc)
wdenkc6097192002-11-03 00:24:07 +0000561{
wdenkc6097192002-11-03 00:24:07 +0000562 unsigned char c;
Marek Vasutb18eabf2011-08-20 09:15:13 +0000563 hd_driveid_t iop;
wdenkc6097192002-11-03 00:24:07 +0000564
wdenk64f70be2004-09-28 20:34:50 +0000565#ifdef CONFIG_ATAPI
566 int retries = 0;
wdenkc7de8292002-11-19 11:04:11 +0000567#endif
wdenkc6097192002-11-03 00:24:07 +0000568 int device;
wdenkc6097192002-11-03 00:24:07 +0000569
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000570 device = dev_desc->dev;
571 printf(" Device %d: ", device);
572
573 ide_led(DEVICE_LED(device), 1); /* LED on */
wdenkc6097192002-11-03 00:24:07 +0000574 /* Select device
575 */
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000576 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
577 dev_desc->if_type = IF_TYPE_IDE;
wdenkc6097192002-11-03 00:24:07 +0000578#ifdef CONFIG_ATAPI
wdenkc7de8292002-11-19 11:04:11 +0000579
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000580 retries = 0;
wdenkc7de8292002-11-19 11:04:11 +0000581
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000582 /* Warning: This will be tricky to read */
583 while (retries <= 1) {
584 /* check signature */
585 if ((ide_inb(device, ATA_SECT_CNT) == 0x01) &&
586 (ide_inb(device, ATA_SECT_NUM) == 0x01) &&
587 (ide_inb(device, ATA_CYL_LOW) == 0x14) &&
588 (ide_inb(device, ATA_CYL_HIGH) == 0xEB)) {
589 /* ATAPI Signature found */
590 dev_desc->if_type = IF_TYPE_ATAPI;
591 /*
592 * Start Ident Command
593 */
594 ide_outb(device, ATA_COMMAND, ATAPI_CMD_IDENT);
595 /*
596 * Wait for completion - ATAPI devices need more time
597 * to become ready
598 */
599 c = ide_wait(device, ATAPI_TIME_OUT);
600 } else
wdenkc6097192002-11-03 00:24:07 +0000601#endif
wdenk1a344f22005-02-03 23:00:49 +0000602 {
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000603 /*
604 * Start Ident Command
605 */
606 ide_outb(device, ATA_COMMAND, ATA_CMD_IDENT);
607
608 /*
609 * Wait for completion
610 */
611 c = ide_wait(device, IDE_TIME_OUT);
wdenk1a344f22005-02-03 23:00:49 +0000612 }
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000613 ide_led(DEVICE_LED(device), 0); /* LED off */
614
615 if (((c & ATA_STAT_DRQ) == 0) ||
616 ((c & (ATA_STAT_FAULT | ATA_STAT_ERR)) != 0)) {
wdenk64f70be2004-09-28 20:34:50 +0000617#ifdef CONFIG_ATAPI
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000618 {
619 /*
620 * Need to soft reset the device
621 * in case it's an ATAPI...
622 */
623 debug("Retrying...\n");
624 ide_outb(device, ATA_DEV_HD,
625 ATA_LBA | ATA_DEVICE(device));
626 udelay(100000);
627 ide_outb(device, ATA_COMMAND, 0x08);
628 udelay(500000); /* 500 ms */
629 }
630 /*
631 * Select device
632 */
633 ide_outb(device, ATA_DEV_HD,
634 ATA_LBA | ATA_DEVICE(device));
635 retries++;
636#else
637 return;
638#endif
639 }
640#ifdef CONFIG_ATAPI
641 else
642 break;
643 } /* see above - ugly to read */
wdenk64f70be2004-09-28 20:34:50 +0000644
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000645 if (retries == 2) /* Not found */
wdenk64f70be2004-09-28 20:34:50 +0000646 return;
647#endif
wdenkc7de8292002-11-19 11:04:11 +0000648
Pavel Herrmannf5b82c02012-10-09 07:04:39 +0000649 ide_input_swap_data(device, (ulong *)&iop, ATA_SECTORWORDS);
wdenkc6097192002-11-03 00:24:07 +0000650
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000651 ident_cpy((unsigned char *) dev_desc->revision, iop.fw_rev,
652 sizeof(dev_desc->revision));
653 ident_cpy((unsigned char *) dev_desc->vendor, iop.model,
654 sizeof(dev_desc->vendor));
655 ident_cpy((unsigned char *) dev_desc->product, iop.serial_no,
656 sizeof(dev_desc->product));
wdenkc3f9d492004-03-14 00:59:59 +0000657#ifdef __LITTLE_ENDIAN
658 /*
Richard Retanubunbcdf1d22008-11-06 14:01:51 -0500659 * firmware revision, model, and serial number have Big Endian Byte
660 * order in Word. Convert all three to little endian.
wdenkc3f9d492004-03-14 00:59:59 +0000661 *
662 * See CF+ and CompactFlash Specification Revision 2.0:
Richard Retanubunbcdf1d22008-11-06 14:01:51 -0500663 * 6.2.1.6: Identify Drive, Table 39 for more details
wdenkc3f9d492004-03-14 00:59:59 +0000664 */
665
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000666 strswab(dev_desc->revision);
667 strswab(dev_desc->vendor);
668 strswab(dev_desc->product);
wdenkc3f9d492004-03-14 00:59:59 +0000669#endif /* __LITTLE_ENDIAN */
wdenkc6097192002-11-03 00:24:07 +0000670
Marek Vasutb18eabf2011-08-20 09:15:13 +0000671 if ((iop.config & 0x0080) == 0x0080)
wdenkc6097192002-11-03 00:24:07 +0000672 dev_desc->removable = 1;
673 else
674 dev_desc->removable = 0;
675
wdenkc6097192002-11-03 00:24:07 +0000676#ifdef CONFIG_ATAPI
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000677 if (dev_desc->if_type == IF_TYPE_ATAPI) {
wdenkc6097192002-11-03 00:24:07 +0000678 atapi_inquiry(dev_desc);
679 return;
680 }
681#endif /* CONFIG_ATAPI */
682
wdenkc3f9d492004-03-14 00:59:59 +0000683#ifdef __BIG_ENDIAN
wdenkc6097192002-11-03 00:24:07 +0000684 /* swap shorts */
Marek Vasutb18eabf2011-08-20 09:15:13 +0000685 dev_desc->lba = (iop.lba_capacity << 16) | (iop.lba_capacity >> 16);
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000686#else /* ! __BIG_ENDIAN */
wdenkc3f9d492004-03-14 00:59:59 +0000687 /*
688 * do not swap shorts on little endian
689 *
690 * See CF+ and CompactFlash Specification Revision 2.0:
691 * 6.2.1.6: Identfy Drive, Table 39, Word Address 57-58 for details.
692 */
Marek Vasutb18eabf2011-08-20 09:15:13 +0000693 dev_desc->lba = iop.lba_capacity;
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000694#endif /* __BIG_ENDIAN */
wdenkc40b2952004-03-13 23:29:43 +0000695
wdenk42dfe7a2004-03-14 22:25:36 +0000696#ifdef CONFIG_LBA48
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000697 if (iop.command_set_2 & 0x0400) { /* LBA 48 support */
wdenk6e592382004-04-18 17:39:38 +0000698 dev_desc->lba48 = 1;
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000699 dev_desc->lba = (unsigned long long) iop.lba48_capacity[0] |
700 ((unsigned long long) iop.lba48_capacity[1] << 16) |
701 ((unsigned long long) iop.lba48_capacity[2] << 32) |
702 ((unsigned long long) iop.lba48_capacity[3] << 48);
wdenkc40b2952004-03-13 23:29:43 +0000703 } else {
wdenkc40b2952004-03-13 23:29:43 +0000704 dev_desc->lba48 = 0;
705 }
706#endif /* CONFIG_LBA48 */
wdenkc6097192002-11-03 00:24:07 +0000707 /* assuming HD */
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000708 dev_desc->type = DEV_TYPE_HARDDISK;
709 dev_desc->blksz = ATA_BLOCKSIZE;
Egbert Eich0472fbf2013-04-09 21:11:56 +0000710 dev_desc->log2blksz = LOG2(dev_desc->blksz);
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000711 dev_desc->lun = 0; /* just to fill something in... */
wdenkc6097192002-11-03 00:24:07 +0000712
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000713#if 0 /* only used to test the powersaving mode,
714 * if enabled, the drive goes after 5 sec
715 * in standby mode */
716 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
717 c = ide_wait(device, IDE_TIME_OUT);
718 ide_outb(device, ATA_SECT_CNT, 1);
719 ide_outb(device, ATA_LBA_LOW, 0);
720 ide_outb(device, ATA_LBA_MID, 0);
721 ide_outb(device, ATA_LBA_HIGH, 0);
722 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
723 ide_outb(device, ATA_COMMAND, 0xe3);
724 udelay(50);
725 c = ide_wait(device, IDE_TIME_OUT); /* can't take over 500 ms */
wdenkc6097192002-11-03 00:24:07 +0000726#endif
727}
728
729
730/* ------------------------------------------------------------------------- */
731
Sascha Silbeff8fef52013-06-14 13:07:25 +0200732ulong ide_read(int device, lbaint_t blknr, lbaint_t blkcnt, void *buffer)
wdenkc6097192002-11-03 00:24:07 +0000733{
734 ulong n = 0;
735 unsigned char c;
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000736 unsigned char pwrsave = 0; /* power save */
737
wdenk42dfe7a2004-03-14 22:25:36 +0000738#ifdef CONFIG_LBA48
wdenkc40b2952004-03-13 23:29:43 +0000739 unsigned char lba48 = 0;
wdenkc6097192002-11-03 00:24:07 +0000740
Guennadi Liakhovetski413bf582008-04-28 14:36:06 +0200741 if (blknr & 0x0000fffff0000000ULL) {
wdenkc40b2952004-03-13 23:29:43 +0000742 /* more than 28 bits used, use 48bit mode */
743 lba48 = 1;
744 }
745#endif
Sascha Silbeff8fef52013-06-14 13:07:25 +0200746 debug("ide_read dev %d start " LBAF ", blocks " LBAF " buffer at %lX\n",
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000747 device, blknr, blkcnt, (ulong) buffer);
wdenkc6097192002-11-03 00:24:07 +0000748
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000749 ide_led(DEVICE_LED(device), 1); /* LED on */
wdenkc6097192002-11-03 00:24:07 +0000750
751 /* Select device
752 */
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000753 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
754 c = ide_wait(device, IDE_TIME_OUT);
wdenkc6097192002-11-03 00:24:07 +0000755
756 if (c & ATA_STAT_BUSY) {
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000757 printf("IDE read: device %d not ready\n", device);
wdenkc6097192002-11-03 00:24:07 +0000758 goto IDE_READ_E;
759 }
760
761 /* first check if the drive is in Powersaving mode, if yes,
762 * increase the timeout value */
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000763 ide_outb(device, ATA_COMMAND, ATA_CMD_CHK_PWR);
764 udelay(50);
wdenkc6097192002-11-03 00:24:07 +0000765
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000766 c = ide_wait(device, IDE_TIME_OUT); /* can't take over 500 ms */
wdenkc6097192002-11-03 00:24:07 +0000767
768 if (c & ATA_STAT_BUSY) {
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000769 printf("IDE read: device %d not ready\n", device);
wdenkc6097192002-11-03 00:24:07 +0000770 goto IDE_READ_E;
771 }
772 if ((c & ATA_STAT_ERR) == ATA_STAT_ERR) {
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000773 printf("No Powersaving mode %X\n", c);
wdenkc6097192002-11-03 00:24:07 +0000774 } else {
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000775 c = ide_inb(device, ATA_SECT_CNT);
776 debug("Powersaving %02X\n", c);
777 if (c == 0)
778 pwrsave = 1;
wdenkc6097192002-11-03 00:24:07 +0000779 }
780
781
782 while (blkcnt-- > 0) {
783
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000784 c = ide_wait(device, IDE_TIME_OUT);
wdenkc6097192002-11-03 00:24:07 +0000785
786 if (c & ATA_STAT_BUSY) {
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000787 printf("IDE read: device %d not ready\n", device);
wdenkc6097192002-11-03 00:24:07 +0000788 break;
789 }
wdenk42dfe7a2004-03-14 22:25:36 +0000790#ifdef CONFIG_LBA48
wdenkc40b2952004-03-13 23:29:43 +0000791 if (lba48) {
792 /* write high bits */
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000793 ide_outb(device, ATA_SECT_CNT, 0);
794 ide_outb(device, ATA_LBA_LOW, (blknr >> 24) & 0xFF);
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200795#ifdef CONFIG_SYS_64BIT_LBA
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000796 ide_outb(device, ATA_LBA_MID, (blknr >> 32) & 0xFF);
797 ide_outb(device, ATA_LBA_HIGH, (blknr >> 40) & 0xFF);
Guennadi Liakhovetski413bf582008-04-28 14:36:06 +0200798#else
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000799 ide_outb(device, ATA_LBA_MID, 0);
800 ide_outb(device, ATA_LBA_HIGH, 0);
Guennadi Liakhovetski413bf582008-04-28 14:36:06 +0200801#endif
wdenkc40b2952004-03-13 23:29:43 +0000802 }
803#endif
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000804 ide_outb(device, ATA_SECT_CNT, 1);
805 ide_outb(device, ATA_LBA_LOW, (blknr >> 0) & 0xFF);
806 ide_outb(device, ATA_LBA_MID, (blknr >> 8) & 0xFF);
807 ide_outb(device, ATA_LBA_HIGH, (blknr >> 16) & 0xFF);
wdenkc40b2952004-03-13 23:29:43 +0000808
wdenk42dfe7a2004-03-14 22:25:36 +0000809#ifdef CONFIG_LBA48
wdenkc40b2952004-03-13 23:29:43 +0000810 if (lba48) {
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000811 ide_outb(device, ATA_DEV_HD,
812 ATA_LBA | ATA_DEVICE(device));
813 ide_outb(device, ATA_COMMAND, ATA_CMD_READ_EXT);
wdenkc40b2952004-03-13 23:29:43 +0000814
815 } else
816#endif
817 {
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000818 ide_outb(device, ATA_DEV_HD, ATA_LBA |
819 ATA_DEVICE(device) | ((blknr >> 24) & 0xF));
820 ide_outb(device, ATA_COMMAND, ATA_CMD_READ);
wdenkc40b2952004-03-13 23:29:43 +0000821 }
wdenkc6097192002-11-03 00:24:07 +0000822
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000823 udelay(50);
wdenkc6097192002-11-03 00:24:07 +0000824
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000825 if (pwrsave) {
826 /* may take up to 4 sec */
827 c = ide_wait(device, IDE_SPIN_UP_TIME_OUT);
828 pwrsave = 0;
wdenkc6097192002-11-03 00:24:07 +0000829 } else {
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000830 /* can't take over 500 ms */
831 c = ide_wait(device, IDE_TIME_OUT);
wdenkc6097192002-11-03 00:24:07 +0000832 }
833
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000834 if ((c & (ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR)) !=
835 ATA_STAT_DRQ) {
Sascha Silbeff8fef52013-06-14 13:07:25 +0200836 printf("Error (no IRQ) dev %d blk " LBAF ": status "
837 "%#02x\n", device, blknr, c);
wdenkc6097192002-11-03 00:24:07 +0000838 break;
839 }
840
Pavel Herrmannf5b82c02012-10-09 07:04:39 +0000841 ide_input_data(device, buffer, ATA_SECTORWORDS);
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000842 (void) ide_inb(device, ATA_STATUS); /* clear IRQ */
wdenkc6097192002-11-03 00:24:07 +0000843
844 ++n;
845 ++blknr;
Greg Lopp0b945042007-04-13 08:02:24 +0200846 buffer += ATA_BLOCKSIZE;
wdenkc6097192002-11-03 00:24:07 +0000847 }
848IDE_READ_E:
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000849 ide_led(DEVICE_LED(device), 0); /* LED off */
wdenkc6097192002-11-03 00:24:07 +0000850 return (n);
851}
852
853/* ------------------------------------------------------------------------- */
854
855
Sascha Silbeff8fef52013-06-14 13:07:25 +0200856ulong ide_write(int device, lbaint_t blknr, lbaint_t blkcnt, const void *buffer)
wdenkc6097192002-11-03 00:24:07 +0000857{
858 ulong n = 0;
859 unsigned char c;
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000860
wdenk42dfe7a2004-03-14 22:25:36 +0000861#ifdef CONFIG_LBA48
wdenkc40b2952004-03-13 23:29:43 +0000862 unsigned char lba48 = 0;
863
Guennadi Liakhovetski413bf582008-04-28 14:36:06 +0200864 if (blknr & 0x0000fffff0000000ULL) {
wdenkc40b2952004-03-13 23:29:43 +0000865 /* more than 28 bits used, use 48bit mode */
866 lba48 = 1;
867 }
868#endif
wdenkc6097192002-11-03 00:24:07 +0000869
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000870 ide_led(DEVICE_LED(device), 1); /* LED on */
wdenkc6097192002-11-03 00:24:07 +0000871
872 /* Select device
873 */
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000874 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
wdenkc6097192002-11-03 00:24:07 +0000875
876 while (blkcnt-- > 0) {
877
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000878 c = ide_wait(device, IDE_TIME_OUT);
wdenkc6097192002-11-03 00:24:07 +0000879
880 if (c & ATA_STAT_BUSY) {
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000881 printf("IDE read: device %d not ready\n", device);
wdenkc6097192002-11-03 00:24:07 +0000882 goto WR_OUT;
883 }
wdenk42dfe7a2004-03-14 22:25:36 +0000884#ifdef CONFIG_LBA48
wdenkc40b2952004-03-13 23:29:43 +0000885 if (lba48) {
886 /* write high bits */
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000887 ide_outb(device, ATA_SECT_CNT, 0);
888 ide_outb(device, ATA_LBA_LOW, (blknr >> 24) & 0xFF);
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200889#ifdef CONFIG_SYS_64BIT_LBA
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000890 ide_outb(device, ATA_LBA_MID, (blknr >> 32) & 0xFF);
891 ide_outb(device, ATA_LBA_HIGH, (blknr >> 40) & 0xFF);
Guennadi Liakhovetski413bf582008-04-28 14:36:06 +0200892#else
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000893 ide_outb(device, ATA_LBA_MID, 0);
894 ide_outb(device, ATA_LBA_HIGH, 0);
Guennadi Liakhovetski413bf582008-04-28 14:36:06 +0200895#endif
wdenkc40b2952004-03-13 23:29:43 +0000896 }
897#endif
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000898 ide_outb(device, ATA_SECT_CNT, 1);
899 ide_outb(device, ATA_LBA_LOW, (blknr >> 0) & 0xFF);
900 ide_outb(device, ATA_LBA_MID, (blknr >> 8) & 0xFF);
901 ide_outb(device, ATA_LBA_HIGH, (blknr >> 16) & 0xFF);
wdenkc40b2952004-03-13 23:29:43 +0000902
wdenk42dfe7a2004-03-14 22:25:36 +0000903#ifdef CONFIG_LBA48
wdenkc40b2952004-03-13 23:29:43 +0000904 if (lba48) {
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000905 ide_outb(device, ATA_DEV_HD,
906 ATA_LBA | ATA_DEVICE(device));
907 ide_outb(device, ATA_COMMAND, ATA_CMD_WRITE_EXT);
wdenkc40b2952004-03-13 23:29:43 +0000908
909 } else
910#endif
911 {
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000912 ide_outb(device, ATA_DEV_HD, ATA_LBA |
913 ATA_DEVICE(device) | ((blknr >> 24) & 0xF));
914 ide_outb(device, ATA_COMMAND, ATA_CMD_WRITE);
wdenkc40b2952004-03-13 23:29:43 +0000915 }
wdenkc6097192002-11-03 00:24:07 +0000916
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000917 udelay(50);
wdenkc6097192002-11-03 00:24:07 +0000918
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000919 /* can't take over 500 ms */
920 c = ide_wait(device, IDE_TIME_OUT);
wdenkc6097192002-11-03 00:24:07 +0000921
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000922 if ((c & (ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR)) !=
923 ATA_STAT_DRQ) {
Sascha Silbeff8fef52013-06-14 13:07:25 +0200924 printf("Error (no IRQ) dev %d blk " LBAF ": status "
925 "%#02x\n", device, blknr, c);
wdenkc6097192002-11-03 00:24:07 +0000926 goto WR_OUT;
927 }
928
Pavel Herrmannf5b82c02012-10-09 07:04:39 +0000929 ide_output_data(device, buffer, ATA_SECTORWORDS);
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000930 c = ide_inb(device, ATA_STATUS); /* clear IRQ */
wdenkc6097192002-11-03 00:24:07 +0000931 ++n;
932 ++blknr;
Greg Lopp0b945042007-04-13 08:02:24 +0200933 buffer += ATA_BLOCKSIZE;
wdenkc6097192002-11-03 00:24:07 +0000934 }
935WR_OUT:
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000936 ide_led(DEVICE_LED(device), 0); /* LED off */
wdenkc6097192002-11-03 00:24:07 +0000937 return (n);
938}
939
940/* ------------------------------------------------------------------------- */
941
942/*
943 * copy src to dest, skipping leading and trailing blanks and null
944 * terminate the string
wdenk7d7ce412004-03-17 01:13:07 +0000945 * "len" is the size of available memory including the terminating '\0'
wdenkc6097192002-11-03 00:24:07 +0000946 */
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000947static void ident_cpy(unsigned char *dst, unsigned char *src,
948 unsigned int len)
wdenkc6097192002-11-03 00:24:07 +0000949{
wdenk7d7ce412004-03-17 01:13:07 +0000950 unsigned char *end, *last;
wdenkc6097192002-11-03 00:24:07 +0000951
wdenk7d7ce412004-03-17 01:13:07 +0000952 last = dst;
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000953 end = src + len - 1;
wdenk7d7ce412004-03-17 01:13:07 +0000954
955 /* reserve space for '\0' */
956 if (len < 2)
957 goto OUT;
wdenkefa329c2004-03-23 20:18:25 +0000958
wdenk7d7ce412004-03-17 01:13:07 +0000959 /* skip leading white space */
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000960 while ((*src) && (src < end) && (*src == ' '))
wdenk7d7ce412004-03-17 01:13:07 +0000961 ++src;
962
963 /* copy string, omitting trailing white space */
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000964 while ((*src) && (src < end)) {
wdenk7d7ce412004-03-17 01:13:07 +0000965 *dst++ = *src;
966 if (*src++ != ' ')
967 last = dst;
wdenkc6097192002-11-03 00:24:07 +0000968 }
wdenk7d7ce412004-03-17 01:13:07 +0000969OUT:
970 *last = '\0';
wdenkc6097192002-11-03 00:24:07 +0000971}
972
973/* ------------------------------------------------------------------------- */
974
975/*
976 * Wait until Busy bit is off, or timeout (in ms)
977 * Return last status
978 */
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000979static uchar ide_wait(int dev, ulong t)
wdenkc6097192002-11-03 00:24:07 +0000980{
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000981 ulong delay = 10 * t; /* poll every 100 us */
wdenkc6097192002-11-03 00:24:07 +0000982 uchar c;
983
wdenk2262cfe2002-11-18 00:14:45 +0000984 while ((c = ide_inb(dev, ATA_STATUS)) & ATA_STAT_BUSY) {
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000985 udelay(100);
986 if (delay-- == 0)
wdenkc6097192002-11-03 00:24:07 +0000987 break;
wdenkc6097192002-11-03 00:24:07 +0000988 }
989 return (c);
990}
991
992/* ------------------------------------------------------------------------- */
993
994#ifdef CONFIG_IDE_RESET
995extern void ide_set_reset(int idereset);
996
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000997static void ide_reset(void)
wdenkc6097192002-11-03 00:24:07 +0000998{
wdenkc6097192002-11-03 00:24:07 +0000999 int i;
1000
1001 curr_device = -1;
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001002 for (i = 0; i < CONFIG_SYS_IDE_MAXBUS; ++i)
wdenkc6097192002-11-03 00:24:07 +00001003 ide_bus_ok[i] = 0;
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001004 for (i = 0; i < CONFIG_SYS_IDE_MAXDEVICE; ++i)
wdenkc6097192002-11-03 00:24:07 +00001005 ide_dev_desc[i].type = DEV_TYPE_UNKNOWN;
1006
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001007 ide_set_reset(1); /* assert reset */
wdenkc6097192002-11-03 00:24:07 +00001008
Martin Krausee175eac2008-04-03 13:37:56 +02001009 /* the reset signal shall be asserted for et least 25 us */
1010 udelay(25);
1011
wdenkc6097192002-11-03 00:24:07 +00001012 WATCHDOG_RESET();
1013
wdenkc6097192002-11-03 00:24:07 +00001014 /* de-assert RESET signal */
1015 ide_set_reset(0);
1016
1017 /* wait 250 ms */
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001018 for (i = 0; i < 250; ++i)
1019 udelay(1000);
wdenkc6097192002-11-03 00:24:07 +00001020}
1021
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001022#endif /* CONFIG_IDE_RESET */
wdenkc6097192002-11-03 00:24:07 +00001023
1024/* ------------------------------------------------------------------------- */
1025
Heiko Schocher3887c3f2009-09-23 07:56:08 +02001026#if defined(CONFIG_OF_IDE_FIXUP)
1027int ide_device_present(int dev)
1028{
1029 if (dev >= CONFIG_SYS_IDE_MAXBUS)
1030 return 0;
1031 return (ide_dev_desc[dev].type == DEV_TYPE_UNKNOWN ? 0 : 1);
1032}
1033#endif
wdenkc6097192002-11-03 00:24:07 +00001034/* ------------------------------------------------------------------------- */
1035
1036#ifdef CONFIG_ATAPI
1037/****************************************************************************
1038 * ATAPI Support
1039 */
1040
Pavel Herrmannf5b82c02012-10-09 07:04:39 +00001041void ide_input_data_shorts(int dev, ushort *sect_buf, int shorts)
1042 __attribute__ ((weak, alias("__ide_input_data_shorts")));
1043
1044void ide_output_data_shorts(int dev, ushort *sect_buf, int shorts)
1045 __attribute__ ((weak, alias("__ide_output_data_shorts")));
1046
1047
Albert Aribaudf2a37fc2010-08-08 05:17:05 +05301048#if defined(CONFIG_IDE_SWAP_IO)
wdenkc6097192002-11-03 00:24:07 +00001049/* since ATAPI may use commands with not 4 bytes alligned length
1050 * we have our own transfer functions, 2 bytes alligned */
Pavel Herrmannf5b82c02012-10-09 07:04:39 +00001051void __ide_output_data_shorts(int dev, ushort *sect_buf, int shorts)
wdenkc6097192002-11-03 00:24:07 +00001052{
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001053 ushort *dbuf;
1054 volatile ushort *pbuf;
wdenkc6097192002-11-03 00:24:07 +00001055
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001056 pbuf = (ushort *) (ATA_CURR_BASE(dev) + ATA_DATA_REG);
1057 dbuf = (ushort *) sect_buf;
wdenkdb01a2e2004-04-15 23:14:49 +00001058
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001059 debug("in output data shorts base for read is %lx\n",
1060 (unsigned long) pbuf);
wdenkdb01a2e2004-04-15 23:14:49 +00001061
wdenkc6097192002-11-03 00:24:07 +00001062 while (shorts--) {
wdenk5cf91d62004-04-23 20:32:05 +00001063 EIEIO;
wdenk1a344f22005-02-03 23:00:49 +00001064 *pbuf = *dbuf++;
wdenkc6097192002-11-03 00:24:07 +00001065 }
wdenk1a344f22005-02-03 23:00:49 +00001066}
1067
Pavel Herrmannf5b82c02012-10-09 07:04:39 +00001068void __ide_input_data_shorts(int dev, ushort *sect_buf, int shorts)
wdenk1a344f22005-02-03 23:00:49 +00001069{
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001070 ushort *dbuf;
1071 volatile ushort *pbuf;
wdenk1a344f22005-02-03 23:00:49 +00001072
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001073 pbuf = (ushort *) (ATA_CURR_BASE(dev) + ATA_DATA_REG);
1074 dbuf = (ushort *) sect_buf;
wdenk1a344f22005-02-03 23:00:49 +00001075
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001076 debug("in input data shorts base for read is %lx\n",
1077 (unsigned long) pbuf);
wdenk1a344f22005-02-03 23:00:49 +00001078
1079 while (shorts--) {
1080 EIEIO;
1081 *dbuf++ = *pbuf;
1082 }
wdenkc6097192002-11-03 00:24:07 +00001083}
1084
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001085#else /* ! CONFIG_IDE_SWAP_IO */
Pavel Herrmannf5b82c02012-10-09 07:04:39 +00001086void __ide_output_data_shorts(int dev, ushort *sect_buf, int shorts)
wdenk2262cfe2002-11-18 00:14:45 +00001087{
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001088 outsw(ATA_CURR_BASE(dev) + ATA_DATA_REG, sect_buf, shorts);
wdenk2262cfe2002-11-18 00:14:45 +00001089}
1090
Pavel Herrmannf5b82c02012-10-09 07:04:39 +00001091void __ide_input_data_shorts(int dev, ushort *sect_buf, int shorts)
wdenk2262cfe2002-11-18 00:14:45 +00001092{
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001093 insw(ATA_CURR_BASE(dev) + ATA_DATA_REG, sect_buf, shorts);
wdenk2262cfe2002-11-18 00:14:45 +00001094}
1095
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001096#endif /* CONFIG_IDE_SWAP_IO */
wdenk2262cfe2002-11-18 00:14:45 +00001097
wdenkc6097192002-11-03 00:24:07 +00001098/*
1099 * Wait until (Status & mask) == res, or timeout (in ms)
1100 * Return last status
1101 * This is used since some ATAPI CD ROMs clears their Busy Bit first
1102 * and then they set their DRQ Bit
1103 */
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001104static uchar atapi_wait_mask(int dev, ulong t, uchar mask, uchar res)
wdenkc6097192002-11-03 00:24:07 +00001105{
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001106 ulong delay = 10 * t; /* poll every 100 us */
wdenkc6097192002-11-03 00:24:07 +00001107 uchar c;
1108
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001109 /* prevents to read the status before valid */
1110 c = ide_inb(dev, ATA_DEV_CTL);
1111
wdenk2262cfe2002-11-18 00:14:45 +00001112 while (((c = ide_inb(dev, ATA_STATUS)) & mask) != res) {
wdenkc6097192002-11-03 00:24:07 +00001113 /* break if error occurs (doesn't make sense to wait more) */
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001114 if ((c & ATA_STAT_ERR) == ATA_STAT_ERR)
wdenkc6097192002-11-03 00:24:07 +00001115 break;
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001116 udelay(100);
1117 if (delay-- == 0)
wdenkc6097192002-11-03 00:24:07 +00001118 break;
wdenkc6097192002-11-03 00:24:07 +00001119 }
1120 return (c);
1121}
1122
1123/*
1124 * issue an atapi command
1125 */
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001126unsigned char atapi_issue(int device, unsigned char *ccb, int ccblen,
1127 unsigned char *buffer, int buflen)
wdenkc6097192002-11-03 00:24:07 +00001128{
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001129 unsigned char c, err, mask, res;
wdenkc6097192002-11-03 00:24:07 +00001130 int n;
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001131
1132 ide_led(DEVICE_LED(device), 1); /* LED on */
wdenkc6097192002-11-03 00:24:07 +00001133
1134 /* Select device
1135 */
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001136 mask = ATA_STAT_BUSY | ATA_STAT_DRQ;
wdenkc6097192002-11-03 00:24:07 +00001137 res = 0;
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001138 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
1139 c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
wdenkc6097192002-11-03 00:24:07 +00001140 if ((c & mask) != res) {
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001141 printf("ATAPI_ISSUE: device %d not ready status %X\n", device,
1142 c);
1143 err = 0xFF;
wdenkc6097192002-11-03 00:24:07 +00001144 goto AI_OUT;
1145 }
1146 /* write taskfile */
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001147 ide_outb(device, ATA_ERROR_REG, 0); /* no DMA, no overlaped */
1148 ide_outb(device, ATA_SECT_CNT, 0);
1149 ide_outb(device, ATA_SECT_NUM, 0);
1150 ide_outb(device, ATA_CYL_LOW, (unsigned char) (buflen & 0xFF));
1151 ide_outb(device, ATA_CYL_HIGH,
1152 (unsigned char) ((buflen >> 8) & 0xFF));
1153 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
wdenkc6097192002-11-03 00:24:07 +00001154
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001155 ide_outb(device, ATA_COMMAND, ATAPI_CMD_PACKET);
1156 udelay(50);
wdenkc6097192002-11-03 00:24:07 +00001157
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001158 mask = ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR;
wdenkc6097192002-11-03 00:24:07 +00001159 res = ATA_STAT_DRQ;
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001160 c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
wdenkc6097192002-11-03 00:24:07 +00001161
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001162 if ((c & mask) != res) { /* DRQ must be 1, BSY 0 */
1163 printf("ATAPI_ISSUE: Error (no IRQ) before sending ccb dev %d status 0x%02x\n",
1164 device, c);
1165 err = 0xFF;
wdenkc6097192002-11-03 00:24:07 +00001166 goto AI_OUT;
1167 }
1168
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001169 /* write command block */
Pavel Herrmannf5b82c02012-10-09 07:04:39 +00001170 ide_output_data_shorts(device, (unsigned short *) ccb, ccblen / 2);
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001171
Wolfgang Denk53677ef2008-05-20 16:00:29 +02001172 /* ATAPI Command written wait for completition */
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001173 udelay(5000); /* device must set bsy */
wdenkc6097192002-11-03 00:24:07 +00001174
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001175 mask = ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR;
1176 /*
1177 * if no data wait for DRQ = 0 BSY = 0
1178 * if data wait for DRQ = 1 BSY = 0
1179 */
1180 res = 0;
1181 if (buflen)
wdenkc6097192002-11-03 00:24:07 +00001182 res = ATA_STAT_DRQ;
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001183 c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
1184 if ((c & mask) != res) {
wdenkc6097192002-11-03 00:24:07 +00001185 if (c & ATA_STAT_ERR) {
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001186 err = (ide_inb(device, ATA_ERROR_REG)) >> 4;
1187 debug("atapi_issue 1 returned sense key %X status %02X\n",
1188 err, c);
wdenkc6097192002-11-03 00:24:07 +00001189 } else {
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001190 printf("ATAPI_ISSUE: (no DRQ) after sending ccb (%x) status 0x%02x\n",
1191 ccb[0], c);
1192 err = 0xFF;
wdenkc6097192002-11-03 00:24:07 +00001193 }
1194 goto AI_OUT;
1195 }
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001196 n = ide_inb(device, ATA_CYL_HIGH);
1197 n <<= 8;
1198 n += ide_inb(device, ATA_CYL_LOW);
1199 if (n > buflen) {
1200 printf("ERROR, transfer bytes %d requested only %d\n", n,
1201 buflen);
1202 err = 0xff;
wdenkc6097192002-11-03 00:24:07 +00001203 goto AI_OUT;
1204 }
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001205 if ((n == 0) && (buflen < 0)) {
1206 printf("ERROR, transfer bytes %d requested %d\n", n, buflen);
1207 err = 0xff;
wdenkc6097192002-11-03 00:24:07 +00001208 goto AI_OUT;
1209 }
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001210 if (n != buflen) {
1211 debug("WARNING, transfer bytes %d not equal with requested %d\n",
1212 n, buflen);
wdenkc6097192002-11-03 00:24:07 +00001213 }
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001214 if (n != 0) { /* data transfer */
1215 debug("ATAPI_ISSUE: %d Bytes to transfer\n", n);
1216 /* we transfer shorts */
1217 n >>= 1;
wdenkc6097192002-11-03 00:24:07 +00001218 /* ok now decide if it is an in or output */
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001219 if ((ide_inb(device, ATA_SECT_CNT) & 0x02) == 0) {
1220 debug("Write to device\n");
Pavel Herrmannf5b82c02012-10-09 07:04:39 +00001221 ide_output_data_shorts(device,
1222 (unsigned short *) buffer, n);
wdenkc6097192002-11-03 00:24:07 +00001223 } else {
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001224 debug("Read from device @ %p shorts %d\n", buffer, n);
Pavel Herrmannf5b82c02012-10-09 07:04:39 +00001225 ide_input_data_shorts(device,
1226 (unsigned short *) buffer, n);
wdenkc6097192002-11-03 00:24:07 +00001227 }
1228 }
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001229 udelay(5000); /* seems that some CD ROMs need this... */
1230 mask = ATA_STAT_BUSY | ATA_STAT_ERR;
1231 res = 0;
1232 c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
wdenkc6097192002-11-03 00:24:07 +00001233 if ((c & ATA_STAT_ERR) == ATA_STAT_ERR) {
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001234 err = (ide_inb(device, ATA_ERROR_REG) >> 4);
1235 debug("atapi_issue 2 returned sense key %X status %X\n", err,
1236 c);
wdenkc6097192002-11-03 00:24:07 +00001237 } else {
1238 err = 0;
1239 }
1240AI_OUT:
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001241 ide_led(DEVICE_LED(device), 0); /* LED off */
wdenkc6097192002-11-03 00:24:07 +00001242 return (err);
1243}
1244
1245/*
1246 * sending the command to atapi_issue. If an status other than good
1247 * returns, an request_sense will be issued
1248 */
1249
Wolfgang Denk53677ef2008-05-20 16:00:29 +02001250#define ATAPI_DRIVE_NOT_READY 100
wdenkc6097192002-11-03 00:24:07 +00001251#define ATAPI_UNIT_ATTN 10
1252
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001253unsigned char atapi_issue_autoreq(int device,
1254 unsigned char *ccb,
1255 int ccblen,
1256 unsigned char *buffer, int buflen)
wdenkc6097192002-11-03 00:24:07 +00001257{
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001258 unsigned char sense_data[18], sense_ccb[12];
1259 unsigned char res, key, asc, ascq;
1260 int notready, unitattn;
wdenkc6097192002-11-03 00:24:07 +00001261
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001262 unitattn = ATAPI_UNIT_ATTN;
1263 notready = ATAPI_DRIVE_NOT_READY;
wdenkc6097192002-11-03 00:24:07 +00001264
1265retry:
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001266 res = atapi_issue(device, ccb, ccblen, buffer, buflen);
1267 if (res == 0)
1268 return 0; /* Ok */
wdenkc6097192002-11-03 00:24:07 +00001269
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001270 if (res == 0xFF)
1271 return 0xFF; /* error */
wdenkc6097192002-11-03 00:24:07 +00001272
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001273 debug("(auto_req)atapi_issue returned sense key %X\n", res);
wdenkc6097192002-11-03 00:24:07 +00001274
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001275 memset(sense_ccb, 0, sizeof(sense_ccb));
1276 memset(sense_data, 0, sizeof(sense_data));
1277 sense_ccb[0] = ATAPI_CMD_REQ_SENSE;
1278 sense_ccb[4] = 18; /* allocation Length */
wdenkc6097192002-11-03 00:24:07 +00001279
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001280 res = atapi_issue(device, sense_ccb, 12, sense_data, 18);
1281 key = (sense_data[2] & 0xF);
1282 asc = (sense_data[12]);
1283 ascq = (sense_data[13]);
wdenkc6097192002-11-03 00:24:07 +00001284
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001285 debug("ATAPI_CMD_REQ_SENSE returned %x\n", res);
1286 debug(" Sense page: %02X key %02X ASC %02X ASCQ %02X\n",
1287 sense_data[0], key, asc, ascq);
wdenkc6097192002-11-03 00:24:07 +00001288
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001289 if ((key == 0))
1290 return 0; /* ok device ready */
wdenkc6097192002-11-03 00:24:07 +00001291
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001292 if ((key == 6) || (asc == 0x29) || (asc == 0x28)) { /* Unit Attention */
1293 if (unitattn-- > 0) {
1294 udelay(200 * 1000);
wdenkc6097192002-11-03 00:24:07 +00001295 goto retry;
1296 }
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001297 printf("Unit Attention, tried %d\n", ATAPI_UNIT_ATTN);
wdenkc6097192002-11-03 00:24:07 +00001298 goto error;
1299 }
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001300 if ((asc == 0x4) && (ascq == 0x1)) {
1301 /* not ready, but will be ready soon */
1302 if (notready-- > 0) {
1303 udelay(200 * 1000);
wdenkc6097192002-11-03 00:24:07 +00001304 goto retry;
1305 }
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001306 printf("Drive not ready, tried %d times\n",
1307 ATAPI_DRIVE_NOT_READY);
wdenkc6097192002-11-03 00:24:07 +00001308 goto error;
1309 }
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001310 if (asc == 0x3a) {
1311 debug("Media not present\n");
wdenkc6097192002-11-03 00:24:07 +00001312 goto error;
1313 }
wdenkc7de8292002-11-19 11:04:11 +00001314
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001315 printf("ERROR: Unknown Sense key %02X ASC %02X ASCQ %02X\n", key, asc,
1316 ascq);
wdenkc6097192002-11-03 00:24:07 +00001317error:
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001318 debug("ERROR Sense key %02X ASC %02X ASCQ %02X\n", key, asc, ascq);
wdenkc6097192002-11-03 00:24:07 +00001319 return (0xFF);
1320}
1321
1322
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001323static void atapi_inquiry(block_dev_desc_t *dev_desc)
wdenkc6097192002-11-03 00:24:07 +00001324{
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001325 unsigned char ccb[12]; /* Command descriptor block */
1326 unsigned char iobuf[64]; /* temp buf */
wdenkc6097192002-11-03 00:24:07 +00001327 unsigned char c;
1328 int device;
1329
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001330 device = dev_desc->dev;
1331 dev_desc->type = DEV_TYPE_UNKNOWN; /* not yet valid */
1332 dev_desc->block_read = atapi_read;
wdenkc6097192002-11-03 00:24:07 +00001333
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001334 memset(ccb, 0, sizeof(ccb));
1335 memset(iobuf, 0, sizeof(iobuf));
wdenkc6097192002-11-03 00:24:07 +00001336
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001337 ccb[0] = ATAPI_CMD_INQUIRY;
1338 ccb[4] = 40; /* allocation Legnth */
1339 c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *) iobuf, 40);
wdenkc6097192002-11-03 00:24:07 +00001340
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001341 debug("ATAPI_CMD_INQUIRY returned %x\n", c);
1342 if (c != 0)
wdenkc6097192002-11-03 00:24:07 +00001343 return;
1344
1345 /* copy device ident strings */
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001346 ident_cpy((unsigned char *) dev_desc->vendor, &iobuf[8], 8);
1347 ident_cpy((unsigned char *) dev_desc->product, &iobuf[16], 16);
1348 ident_cpy((unsigned char *) dev_desc->revision, &iobuf[32], 5);
wdenkc6097192002-11-03 00:24:07 +00001349
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001350 dev_desc->lun = 0;
1351 dev_desc->lba = 0;
1352 dev_desc->blksz = 0;
Egbert Eich0472fbf2013-04-09 21:11:56 +00001353 dev_desc->log2blksz = LOG2_INVALID(typeof(dev_desc->log2blksz));
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001354 dev_desc->type = iobuf[0] & 0x1f;
wdenkc6097192002-11-03 00:24:07 +00001355
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001356 if ((iobuf[1] & 0x80) == 0x80)
wdenkc6097192002-11-03 00:24:07 +00001357 dev_desc->removable = 1;
1358 else
1359 dev_desc->removable = 0;
1360
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001361 memset(ccb, 0, sizeof(ccb));
1362 memset(iobuf, 0, sizeof(iobuf));
1363 ccb[0] = ATAPI_CMD_START_STOP;
1364 ccb[4] = 0x03; /* start */
wdenkc6097192002-11-03 00:24:07 +00001365
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001366 c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *) iobuf, 0);
wdenkc6097192002-11-03 00:24:07 +00001367
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001368 debug("ATAPI_CMD_START_STOP returned %x\n", c);
1369 if (c != 0)
wdenkc6097192002-11-03 00:24:07 +00001370 return;
1371
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001372 memset(ccb, 0, sizeof(ccb));
1373 memset(iobuf, 0, sizeof(iobuf));
1374 c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *) iobuf, 0);
wdenkc6097192002-11-03 00:24:07 +00001375
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001376 debug("ATAPI_CMD_UNIT_TEST_READY returned %x\n", c);
1377 if (c != 0)
wdenkc6097192002-11-03 00:24:07 +00001378 return;
1379
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001380 memset(ccb, 0, sizeof(ccb));
1381 memset(iobuf, 0, sizeof(iobuf));
1382 ccb[0] = ATAPI_CMD_READ_CAP;
1383 c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *) iobuf, 8);
1384 debug("ATAPI_CMD_READ_CAP returned %x\n", c);
1385 if (c != 0)
wdenkc6097192002-11-03 00:24:07 +00001386 return;
1387
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001388 debug("Read Cap: LBA %02X%02X%02X%02X blksize %02X%02X%02X%02X\n",
1389 iobuf[0], iobuf[1], iobuf[2], iobuf[3],
1390 iobuf[4], iobuf[5], iobuf[6], iobuf[7]);
wdenkc6097192002-11-03 00:24:07 +00001391
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001392 dev_desc->lba = ((unsigned long) iobuf[0] << 24) +
1393 ((unsigned long) iobuf[1] << 16) +
1394 ((unsigned long) iobuf[2] << 8) + ((unsigned long) iobuf[3]);
1395 dev_desc->blksz = ((unsigned long) iobuf[4] << 24) +
1396 ((unsigned long) iobuf[5] << 16) +
1397 ((unsigned long) iobuf[6] << 8) + ((unsigned long) iobuf[7]);
Egbert Eich0472fbf2013-04-09 21:11:56 +00001398 dev_desc->log2blksz = LOG2(dev_desc->blksz);
wdenk42dfe7a2004-03-14 22:25:36 +00001399#ifdef CONFIG_LBA48
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001400 /* ATAPI devices cannot use 48bit addressing (ATA/ATAPI v7) */
1401 dev_desc->lba48 = 0;
wdenk42dfe7a2004-03-14 22:25:36 +00001402#endif
wdenkc6097192002-11-03 00:24:07 +00001403 return;
1404}
1405
1406
1407/*
1408 * atapi_read:
1409 * we transfer only one block per command, since the multiple DRQ per
1410 * command is not yet implemented
1411 */
1412#define ATAPI_READ_MAX_BYTES 2048 /* we read max 2kbytes */
1413#define ATAPI_READ_BLOCK_SIZE 2048 /* assuming CD part */
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001414#define ATAPI_READ_MAX_BLOCK (ATAPI_READ_MAX_BYTES/ATAPI_READ_BLOCK_SIZE)
wdenkc6097192002-11-03 00:24:07 +00001415
Simon Glassb6458d32012-10-29 05:24:04 +00001416ulong atapi_read(int device, ulong blknr, lbaint_t blkcnt, void *buffer)
wdenkc6097192002-11-03 00:24:07 +00001417{
1418 ulong n = 0;
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001419 unsigned char ccb[12]; /* Command descriptor block */
wdenkc6097192002-11-03 00:24:07 +00001420 ulong cnt;
1421
Simon Glassb6458d32012-10-29 05:24:04 +00001422 debug("atapi_read dev %d start %lX, blocks " LBAF " buffer at %lX\n",
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001423 device, blknr, blkcnt, (ulong) buffer);
wdenkc6097192002-11-03 00:24:07 +00001424
1425 do {
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001426 if (blkcnt > ATAPI_READ_MAX_BLOCK)
1427 cnt = ATAPI_READ_MAX_BLOCK;
1428 else
1429 cnt = blkcnt;
wdenkc6097192002-11-03 00:24:07 +00001430
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001431 ccb[0] = ATAPI_CMD_READ_12;
1432 ccb[1] = 0; /* reserved */
1433 ccb[2] = (unsigned char) (blknr >> 24) & 0xFF; /* MSB Block */
1434 ccb[3] = (unsigned char) (blknr >> 16) & 0xFF; /* */
1435 ccb[4] = (unsigned char) (blknr >> 8) & 0xFF;
1436 ccb[5] = (unsigned char) blknr & 0xFF; /* LSB Block */
1437 ccb[6] = (unsigned char) (cnt >> 24) & 0xFF; /* MSB Block cnt */
1438 ccb[7] = (unsigned char) (cnt >> 16) & 0xFF;
1439 ccb[8] = (unsigned char) (cnt >> 8) & 0xFF;
1440 ccb[9] = (unsigned char) cnt & 0xFF; /* LSB Block */
1441 ccb[10] = 0; /* reserved */
1442 ccb[11] = 0; /* reserved */
1443
1444 if (atapi_issue_autoreq(device, ccb, 12,
1445 (unsigned char *) buffer,
1446 cnt * ATAPI_READ_BLOCK_SIZE)
1447 == 0xFF) {
wdenkc6097192002-11-03 00:24:07 +00001448 return (n);
1449 }
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001450 n += cnt;
1451 blkcnt -= cnt;
1452 blknr += cnt;
1453 buffer += (cnt * ATAPI_READ_BLOCK_SIZE);
wdenkc6097192002-11-03 00:24:07 +00001454 } while (blkcnt > 0);
1455 return (n);
1456}
1457
1458/* ------------------------------------------------------------------------- */
1459
1460#endif /* CONFIG_ATAPI */
1461
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001462U_BOOT_CMD(ide, 5, 1, do_ide,
1463 "IDE sub-system",
1464 "reset - reset IDE controller\n"
1465 "ide info - show available IDE devices\n"
1466 "ide device [dev] - show or set current device\n"
1467 "ide part [dev] - print partition table of one or all IDE devices\n"
1468 "ide read addr blk# cnt\n"
1469 "ide write addr blk# cnt - read/write `cnt'"
1470 " blocks starting at block `blk#'\n"
1471 " to/from memory address `addr'");
wdenk8bde7f72003-06-27 21:31:46 +00001472
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001473U_BOOT_CMD(diskboot, 3, 1, do_diskboot,
1474 "boot from IDE device", "loadAddr dev:part");