blob: 04a6d9b39833cf6efe876b44239ec2af0accca4b [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
Jeroen Hofstee288afdc2014-07-12 15:07:19 +0200256__weak void ide_led(uchar led, uchar status)
Pavel Herrmann19be2ea2012-10-07 05:56:10 +0000257{
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
Pavel Herrmann19be2ea2012-10-07 05:56:10 +0000272#ifndef CONFIG_IDE_LED /* define LED macros, they are not used anyways */
273# define DEVICE_LED(x) 0
274# define LED_IDE1 1
275# define LED_IDE2 2
276#endif
277
278/* ------------------------------------------------------------------------- */
279
Jeroen Hofstee288afdc2014-07-12 15:07:19 +0200280__weak void ide_outb(int dev, int port, unsigned char val)
Stefan Roesef2302d42008-08-06 14:05:38 +0200281{
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000282 debug("ide_outb (dev= %d, port= 0x%x, val= 0x%02x) : @ 0x%08lx\n",
283 dev, port, val,
284 (ATA_CURR_BASE(dev) + CONFIG_SYS_ATA_PORT_ADDR(port)));
Macpaul Lin0abddf82011-04-11 20:45:32 +0000285
286#if defined(CONFIG_IDE_AHB)
287 if (port) {
288 /* write command */
289 ide_write_register(dev, port, val);
290 } else {
291 /* write data */
292 outb(val, (ATA_CURR_BASE(dev)));
293 }
294#else
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000295 outb(val, (ATA_CURR_BASE(dev) + CONFIG_SYS_ATA_PORT_ADDR(port)));
Macpaul Lin0abddf82011-04-11 20:45:32 +0000296#endif
Stefan Roesef2302d42008-08-06 14:05:38 +0200297}
Macpaul Lin0abddf82011-04-11 20:45:32 +0000298
Jeroen Hofstee288afdc2014-07-12 15:07:19 +0200299__weak unsigned char ide_inb(int dev, int port)
Stefan Roesef2302d42008-08-06 14:05:38 +0200300{
301 uchar val;
Macpaul Lin0abddf82011-04-11 20:45:32 +0000302
303#if defined(CONFIG_IDE_AHB)
304 val = ide_read_register(dev, port);
305#else
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000306 val = inb((ATA_CURR_BASE(dev) + CONFIG_SYS_ATA_PORT_ADDR(port)));
Macpaul Lin0abddf82011-04-11 20:45:32 +0000307#endif
308
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000309 debug("ide_inb (dev= %d, port= 0x%x) : @ 0x%08lx -> 0x%02x\n",
310 dev, port,
311 (ATA_CURR_BASE(dev) + CONFIG_SYS_ATA_PORT_ADDR(port)), val);
Stefan Roesef2302d42008-08-06 14:05:38 +0200312 return val;
313}
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000314
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000315void ide_init(void)
wdenkc6097192002-11-03 00:24:07 +0000316{
wdenkc6097192002-11-03 00:24:07 +0000317 unsigned char c;
318 int i, bus;
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000319
wdenk9fd5e312003-12-07 23:55:12 +0000320#ifdef CONFIG_IDE_8xx_PCCARD
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000321 extern int ide_devices_found; /* Initialized in check_ide_device() */
322#endif /* CONFIG_IDE_8xx_PCCARD */
wdenk9fd5e312003-12-07 23:55:12 +0000323
324#ifdef CONFIG_IDE_PREINIT
325 WATCHDOG_RESET();
326
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000327 if (ide_preinit()) {
328 puts("ide_preinit failed\n");
wdenk9fd5e312003-12-07 23:55:12 +0000329 return;
330 }
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000331#endif /* CONFIG_IDE_PREINIT */
wdenkc6097192002-11-03 00:24:07 +0000332
wdenkc6097192002-11-03 00:24:07 +0000333 WATCHDOG_RESET();
334
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000335 /*
336 * Reset the IDE just to be sure.
wdenkc6097192002-11-03 00:24:07 +0000337 * Light LED's to show
338 */
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000339 ide_led((LED_IDE1 | LED_IDE2), 1); /* LED's on */
340
341 /* ATAPI Drives seems to need a proper IDE Reset */
342 ide_reset();
wdenkc6097192002-11-03 00:24:07 +0000343
Pavel Herrmann8d1165e11a2012-10-09 07:01:56 +0000344#ifdef CONFIG_IDE_INIT_POSTRESET
345 WATCHDOG_RESET();
wdenkc6097192002-11-03 00:24:07 +0000346
Pavel Herrmann8d1165e11a2012-10-09 07:01:56 +0000347 if (ide_init_postreset()) {
348 puts("ide_preinit_postreset failed\n");
349 return;
350 }
351#endif /* CONFIG_IDE_INIT_POSTRESET */
wdenkc6097192002-11-03 00:24:07 +0000352
353 /*
354 * Wait for IDE to get ready.
355 * According to spec, this can take up to 31 seconds!
356 */
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000357 for (bus = 0; bus < CONFIG_SYS_IDE_MAXBUS; ++bus) {
358 int dev =
359 bus * (CONFIG_SYS_IDE_MAXDEVICE /
360 CONFIG_SYS_IDE_MAXBUS);
wdenkc6097192002-11-03 00:24:07 +0000361
wdenk6069ff22003-02-28 00:49:47 +0000362#ifdef CONFIG_IDE_8xx_PCCARD
363 /* Skip non-ide devices from probing */
364 if ((ide_devices_found & (1 << bus)) == 0) {
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000365 ide_led((LED_IDE1 | LED_IDE2), 0); /* LED's off */
wdenk6069ff22003-02-28 00:49:47 +0000366 continue;
367 }
368#endif
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000369 printf("Bus %d: ", bus);
wdenkc6097192002-11-03 00:24:07 +0000370
371 ide_bus_ok[bus] = 0;
372
373 /* Select device
374 */
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000375 udelay(100000); /* 100 ms */
376 ide_outb(dev, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(dev));
377 udelay(100000); /* 100 ms */
wdenkc6097192002-11-03 00:24:07 +0000378 i = 0;
379 do {
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000380 udelay(10000); /* 10 ms */
wdenkc6097192002-11-03 00:24:07 +0000381
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000382 c = ide_inb(dev, ATA_STATUS);
wdenkc6097192002-11-03 00:24:07 +0000383 i++;
384 if (i > (ATA_RESET_TIME * 100)) {
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000385 puts("** Timeout **\n");
386 /* LED's off */
387 ide_led((LED_IDE1 | LED_IDE2), 0);
wdenkc6097192002-11-03 00:24:07 +0000388 return;
389 }
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000390 if ((i >= 100) && ((i % 100) == 0))
391 putc('.');
392
wdenkc6097192002-11-03 00:24:07 +0000393 } while (c & ATA_STAT_BUSY);
394
395 if (c & (ATA_STAT_BUSY | ATA_STAT_FAULT)) {
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000396 puts("not available ");
397 debug("Status = 0x%02X ", c);
398#ifndef CONFIG_ATAPI /* ATAPI Devices do not set DRDY */
399 } else if ((c & ATA_STAT_READY) == 0) {
400 puts("not available ");
401 debug("Status = 0x%02X ", c);
wdenkc6097192002-11-03 00:24:07 +0000402#endif
403 } else {
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000404 puts("OK ");
wdenkc6097192002-11-03 00:24:07 +0000405 ide_bus_ok[bus] = 1;
406 }
407 WATCHDOG_RESET();
408 }
wdenkc7de8292002-11-19 11:04:11 +0000409
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000410 putc('\n');
wdenkc6097192002-11-03 00:24:07 +0000411
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000412 ide_led((LED_IDE1 | LED_IDE2), 0); /* LED's off */
wdenkc6097192002-11-03 00:24:07 +0000413
414 curr_device = -1;
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000415 for (i = 0; i < CONFIG_SYS_IDE_MAXDEVICE; ++i) {
wdenkc6097192002-11-03 00:24:07 +0000416 int led = (IDE_BUS(i) == 0) ? LED_IDE1 : LED_IDE2;
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000417 ide_dev_desc[i].type = DEV_TYPE_UNKNOWN;
418 ide_dev_desc[i].if_type = IF_TYPE_IDE;
419 ide_dev_desc[i].dev = i;
420 ide_dev_desc[i].part_type = PART_TYPE_UNKNOWN;
421 ide_dev_desc[i].blksz = 0;
Egbert Eich0472fbf2013-04-09 21:11:56 +0000422 ide_dev_desc[i].log2blksz =
423 LOG2_INVALID(typeof(ide_dev_desc[i].log2blksz));
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000424 ide_dev_desc[i].lba = 0;
425 ide_dev_desc[i].block_read = ide_read;
Macpaul Lin0abddf82011-04-11 20:45:32 +0000426 ide_dev_desc[i].block_write = ide_write;
wdenkc6097192002-11-03 00:24:07 +0000427 if (!ide_bus_ok[IDE_BUS(i)])
428 continue;
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000429 ide_led(led, 1); /* LED on */
wdenkc6097192002-11-03 00:24:07 +0000430 ide_ident(&ide_dev_desc[i]);
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000431 ide_led(led, 0); /* LED off */
wdenkc6097192002-11-03 00:24:07 +0000432 dev_print(&ide_dev_desc[i]);
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000433
wdenkc6097192002-11-03 00:24:07 +0000434 if ((ide_dev_desc[i].lba > 0) && (ide_dev_desc[i].blksz > 0)) {
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000435 /* initialize partition type */
436 init_part(&ide_dev_desc[i]);
wdenkc6097192002-11-03 00:24:07 +0000437 if (curr_device < 0)
438 curr_device = i;
439 }
440 }
441 WATCHDOG_RESET();
442}
443
444/* ------------------------------------------------------------------------- */
445
Matthew McClintockdf3fc522011-05-24 05:31:19 +0000446#ifdef CONFIG_PARTITIONS
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000447block_dev_desc_t *ide_get_dev(int dev)
wdenkc6097192002-11-03 00:24:07 +0000448{
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200449 return (dev < CONFIG_SYS_IDE_MAXDEVICE) ? &ide_dev_desc[dev] : NULL;
wdenkc6097192002-11-03 00:24:07 +0000450}
Matthew McClintockdf3fc522011-05-24 05:31:19 +0000451#endif
wdenkc6097192002-11-03 00:24:07 +0000452
wdenkc6097192002-11-03 00:24:07 +0000453/* ------------------------------------------------------------------------- */
454
wdenk5da627a2003-10-09 20:09:04 +0000455/* We only need to swap data if we are running on a big endian cpu. */
Pavel Herrmann4d1361d2012-10-09 07:10:08 +0000456#if defined(__LITTLE_ENDIAN)
Jeroen Hofstee288afdc2014-07-12 15:07:19 +0200457__weak void ide_input_swap_data(int dev, ulong *sect_buf, int words)
Pavel Herrmannf5b82c02012-10-09 07:04:39 +0000458{
459 ide_input_data(dev, sect_buf, words);
460}
wdenk5da627a2003-10-09 20:09:04 +0000461#else
Jeroen Hofstee288afdc2014-07-12 15:07:19 +0200462__weak void ide_input_swap_data(int dev, ulong *sect_buf, int words)
wdenkc6097192002-11-03 00:24:07 +0000463{
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000464 volatile ushort *pbuf =
465 (ushort *) (ATA_CURR_BASE(dev) + ATA_DATA_REG);
466 ushort *dbuf = (ushort *) sect_buf;
wdenk1a344f22005-02-03 23:00:49 +0000467
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000468 debug("in input swap data base for read is %lx\n",
469 (unsigned long) pbuf);
wdenk1a344f22005-02-03 23:00:49 +0000470
471 while (words--) {
Wolfgang Denk0c32d962006-06-16 17:32:31 +0200472#ifdef __MIPS__
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000473 *dbuf++ = swab16p((u16 *) pbuf);
474 *dbuf++ = swab16p((u16 *) pbuf);
Wolfgang Denk0c32d962006-06-16 17:32:31 +0200475#else
wdenk1a344f22005-02-03 23:00:49 +0000476 *dbuf++ = ld_le16(pbuf);
477 *dbuf++ = ld_le16(pbuf);
Wolfgang Denk0c32d962006-06-16 17:32:31 +0200478#endif /* !MIPS */
wdenk1a344f22005-02-03 23:00:49 +0000479 }
wdenkc6097192002-11-03 00:24:07 +0000480}
Pavel Herrmann4d1361d2012-10-09 07:10:08 +0000481#endif /* __LITTLE_ENDIAN */
wdenkc6097192002-11-03 00:24:07 +0000482
wdenk2262cfe2002-11-18 00:14:45 +0000483
Albert Aribaudf2a37fc2010-08-08 05:17:05 +0530484#if defined(CONFIG_IDE_SWAP_IO)
Jeroen Hofstee288afdc2014-07-12 15:07:19 +0200485__weak void ide_output_data(int dev, const ulong *sect_buf, int words)
wdenkc6097192002-11-03 00:24:07 +0000486{
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000487 ushort *dbuf;
488 volatile ushort *pbuf;
wdenk1a344f22005-02-03 23:00:49 +0000489
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000490 pbuf = (ushort *) (ATA_CURR_BASE(dev) + ATA_DATA_REG);
491 dbuf = (ushort *) sect_buf;
wdenk1a344f22005-02-03 23:00:49 +0000492 while (words--) {
493 EIEIO;
494 *pbuf = *dbuf++;
495 EIEIO;
496 *pbuf = *dbuf++;
497 }
wdenkc6097192002-11-03 00:24:07 +0000498}
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000499#else /* ! CONFIG_IDE_SWAP_IO */
Jeroen Hofstee288afdc2014-07-12 15:07:19 +0200500__weak void ide_output_data(int dev, const ulong *sect_buf, int words)
wdenk2262cfe2002-11-18 00:14:45 +0000501{
Macpaul Lin0abddf82011-04-11 20:45:32 +0000502#if defined(CONFIG_IDE_AHB)
503 ide_write_data(dev, sect_buf, words);
504#else
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000505 outsw(ATA_CURR_BASE(dev) + ATA_DATA_REG, sect_buf, words << 1);
Macpaul Lin0abddf82011-04-11 20:45:32 +0000506#endif
wdenk2262cfe2002-11-18 00:14:45 +0000507}
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000508#endif /* CONFIG_IDE_SWAP_IO */
wdenkc6097192002-11-03 00:24:07 +0000509
Albert Aribaudf2a37fc2010-08-08 05:17:05 +0530510#if defined(CONFIG_IDE_SWAP_IO)
Jeroen Hofstee288afdc2014-07-12 15:07:19 +0200511__weak void ide_input_data(int dev, ulong *sect_buf, int words)
wdenkc6097192002-11-03 00:24:07 +0000512{
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000513 ushort *dbuf;
514 volatile ushort *pbuf;
wdenk1a344f22005-02-03 23:00:49 +0000515
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000516 pbuf = (ushort *) (ATA_CURR_BASE(dev) + ATA_DATA_REG);
517 dbuf = (ushort *) sect_buf;
wdenk1a344f22005-02-03 23:00:49 +0000518
519 debug("in input data base for read is %lx\n", (unsigned long) pbuf);
520
521 while (words--) {
522 EIEIO;
523 *dbuf++ = *pbuf;
524 EIEIO;
525 *dbuf++ = *pbuf;
526 }
wdenkc6097192002-11-03 00:24:07 +0000527}
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000528#else /* ! CONFIG_IDE_SWAP_IO */
Jeroen Hofstee288afdc2014-07-12 15:07:19 +0200529__weak void ide_input_data(int dev, ulong *sect_buf, int words)
wdenk2262cfe2002-11-18 00:14:45 +0000530{
Macpaul Lin0abddf82011-04-11 20:45:32 +0000531#if defined(CONFIG_IDE_AHB)
532 ide_read_data(dev, sect_buf, words);
533#else
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000534 insw(ATA_CURR_BASE(dev) + ATA_DATA_REG, sect_buf, words << 1);
Macpaul Lin0abddf82011-04-11 20:45:32 +0000535#endif
wdenk2262cfe2002-11-18 00:14:45 +0000536}
537
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000538#endif /* CONFIG_IDE_SWAP_IO */
wdenkc6097192002-11-03 00:24:07 +0000539
540/* -------------------------------------------------------------------------
541 */
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000542static void ide_ident(block_dev_desc_t *dev_desc)
wdenkc6097192002-11-03 00:24:07 +0000543{
wdenkc6097192002-11-03 00:24:07 +0000544 unsigned char c;
Marek Vasutb18eabf2011-08-20 09:15:13 +0000545 hd_driveid_t iop;
wdenkc6097192002-11-03 00:24:07 +0000546
wdenk64f70be2004-09-28 20:34:50 +0000547#ifdef CONFIG_ATAPI
548 int retries = 0;
wdenkc7de8292002-11-19 11:04:11 +0000549#endif
wdenkc6097192002-11-03 00:24:07 +0000550 int device;
wdenkc6097192002-11-03 00:24:07 +0000551
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000552 device = dev_desc->dev;
553 printf(" Device %d: ", device);
554
555 ide_led(DEVICE_LED(device), 1); /* LED on */
wdenkc6097192002-11-03 00:24:07 +0000556 /* Select device
557 */
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000558 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
559 dev_desc->if_type = IF_TYPE_IDE;
wdenkc6097192002-11-03 00:24:07 +0000560#ifdef CONFIG_ATAPI
wdenkc7de8292002-11-19 11:04:11 +0000561
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000562 retries = 0;
wdenkc7de8292002-11-19 11:04:11 +0000563
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000564 /* Warning: This will be tricky to read */
565 while (retries <= 1) {
566 /* check signature */
567 if ((ide_inb(device, ATA_SECT_CNT) == 0x01) &&
568 (ide_inb(device, ATA_SECT_NUM) == 0x01) &&
569 (ide_inb(device, ATA_CYL_LOW) == 0x14) &&
570 (ide_inb(device, ATA_CYL_HIGH) == 0xEB)) {
571 /* ATAPI Signature found */
572 dev_desc->if_type = IF_TYPE_ATAPI;
573 /*
574 * Start Ident Command
575 */
576 ide_outb(device, ATA_COMMAND, ATAPI_CMD_IDENT);
577 /*
578 * Wait for completion - ATAPI devices need more time
579 * to become ready
580 */
581 c = ide_wait(device, ATAPI_TIME_OUT);
582 } else
wdenkc6097192002-11-03 00:24:07 +0000583#endif
wdenk1a344f22005-02-03 23:00:49 +0000584 {
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000585 /*
586 * Start Ident Command
587 */
588 ide_outb(device, ATA_COMMAND, ATA_CMD_IDENT);
589
590 /*
591 * Wait for completion
592 */
593 c = ide_wait(device, IDE_TIME_OUT);
wdenk1a344f22005-02-03 23:00:49 +0000594 }
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000595 ide_led(DEVICE_LED(device), 0); /* LED off */
596
597 if (((c & ATA_STAT_DRQ) == 0) ||
598 ((c & (ATA_STAT_FAULT | ATA_STAT_ERR)) != 0)) {
wdenk64f70be2004-09-28 20:34:50 +0000599#ifdef CONFIG_ATAPI
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000600 {
601 /*
602 * Need to soft reset the device
603 * in case it's an ATAPI...
604 */
605 debug("Retrying...\n");
606 ide_outb(device, ATA_DEV_HD,
607 ATA_LBA | ATA_DEVICE(device));
608 udelay(100000);
609 ide_outb(device, ATA_COMMAND, 0x08);
610 udelay(500000); /* 500 ms */
611 }
612 /*
613 * Select device
614 */
615 ide_outb(device, ATA_DEV_HD,
616 ATA_LBA | ATA_DEVICE(device));
617 retries++;
618#else
619 return;
620#endif
621 }
622#ifdef CONFIG_ATAPI
623 else
624 break;
625 } /* see above - ugly to read */
wdenk64f70be2004-09-28 20:34:50 +0000626
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000627 if (retries == 2) /* Not found */
wdenk64f70be2004-09-28 20:34:50 +0000628 return;
629#endif
wdenkc7de8292002-11-19 11:04:11 +0000630
Pavel Herrmannf5b82c02012-10-09 07:04:39 +0000631 ide_input_swap_data(device, (ulong *)&iop, ATA_SECTORWORDS);
wdenkc6097192002-11-03 00:24:07 +0000632
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000633 ident_cpy((unsigned char *) dev_desc->revision, iop.fw_rev,
634 sizeof(dev_desc->revision));
635 ident_cpy((unsigned char *) dev_desc->vendor, iop.model,
636 sizeof(dev_desc->vendor));
637 ident_cpy((unsigned char *) dev_desc->product, iop.serial_no,
638 sizeof(dev_desc->product));
wdenkc3f9d492004-03-14 00:59:59 +0000639#ifdef __LITTLE_ENDIAN
640 /*
Richard Retanubunbcdf1d22008-11-06 14:01:51 -0500641 * firmware revision, model, and serial number have Big Endian Byte
642 * order in Word. Convert all three to little endian.
wdenkc3f9d492004-03-14 00:59:59 +0000643 *
644 * See CF+ and CompactFlash Specification Revision 2.0:
Richard Retanubunbcdf1d22008-11-06 14:01:51 -0500645 * 6.2.1.6: Identify Drive, Table 39 for more details
wdenkc3f9d492004-03-14 00:59:59 +0000646 */
647
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000648 strswab(dev_desc->revision);
649 strswab(dev_desc->vendor);
650 strswab(dev_desc->product);
wdenkc3f9d492004-03-14 00:59:59 +0000651#endif /* __LITTLE_ENDIAN */
wdenkc6097192002-11-03 00:24:07 +0000652
Marek Vasutb18eabf2011-08-20 09:15:13 +0000653 if ((iop.config & 0x0080) == 0x0080)
wdenkc6097192002-11-03 00:24:07 +0000654 dev_desc->removable = 1;
655 else
656 dev_desc->removable = 0;
657
wdenkc6097192002-11-03 00:24:07 +0000658#ifdef CONFIG_ATAPI
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000659 if (dev_desc->if_type == IF_TYPE_ATAPI) {
wdenkc6097192002-11-03 00:24:07 +0000660 atapi_inquiry(dev_desc);
661 return;
662 }
663#endif /* CONFIG_ATAPI */
664
wdenkc3f9d492004-03-14 00:59:59 +0000665#ifdef __BIG_ENDIAN
wdenkc6097192002-11-03 00:24:07 +0000666 /* swap shorts */
Marek Vasutb18eabf2011-08-20 09:15:13 +0000667 dev_desc->lba = (iop.lba_capacity << 16) | (iop.lba_capacity >> 16);
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000668#else /* ! __BIG_ENDIAN */
wdenkc3f9d492004-03-14 00:59:59 +0000669 /*
670 * do not swap shorts on little endian
671 *
672 * See CF+ and CompactFlash Specification Revision 2.0:
673 * 6.2.1.6: Identfy Drive, Table 39, Word Address 57-58 for details.
674 */
Marek Vasutb18eabf2011-08-20 09:15:13 +0000675 dev_desc->lba = iop.lba_capacity;
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000676#endif /* __BIG_ENDIAN */
wdenkc40b2952004-03-13 23:29:43 +0000677
wdenk42dfe7a2004-03-14 22:25:36 +0000678#ifdef CONFIG_LBA48
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000679 if (iop.command_set_2 & 0x0400) { /* LBA 48 support */
wdenk6e592382004-04-18 17:39:38 +0000680 dev_desc->lba48 = 1;
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000681 dev_desc->lba = (unsigned long long) iop.lba48_capacity[0] |
682 ((unsigned long long) iop.lba48_capacity[1] << 16) |
683 ((unsigned long long) iop.lba48_capacity[2] << 32) |
684 ((unsigned long long) iop.lba48_capacity[3] << 48);
wdenkc40b2952004-03-13 23:29:43 +0000685 } else {
wdenkc40b2952004-03-13 23:29:43 +0000686 dev_desc->lba48 = 0;
687 }
688#endif /* CONFIG_LBA48 */
wdenkc6097192002-11-03 00:24:07 +0000689 /* assuming HD */
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000690 dev_desc->type = DEV_TYPE_HARDDISK;
691 dev_desc->blksz = ATA_BLOCKSIZE;
Egbert Eich0472fbf2013-04-09 21:11:56 +0000692 dev_desc->log2blksz = LOG2(dev_desc->blksz);
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000693 dev_desc->lun = 0; /* just to fill something in... */
wdenkc6097192002-11-03 00:24:07 +0000694
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000695#if 0 /* only used to test the powersaving mode,
696 * if enabled, the drive goes after 5 sec
697 * in standby mode */
698 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
699 c = ide_wait(device, IDE_TIME_OUT);
700 ide_outb(device, ATA_SECT_CNT, 1);
701 ide_outb(device, ATA_LBA_LOW, 0);
702 ide_outb(device, ATA_LBA_MID, 0);
703 ide_outb(device, ATA_LBA_HIGH, 0);
704 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
705 ide_outb(device, ATA_COMMAND, 0xe3);
706 udelay(50);
707 c = ide_wait(device, IDE_TIME_OUT); /* can't take over 500 ms */
wdenkc6097192002-11-03 00:24:07 +0000708#endif
709}
710
711
712/* ------------------------------------------------------------------------- */
713
Sascha Silbeff8fef52013-06-14 13:07:25 +0200714ulong ide_read(int device, lbaint_t blknr, lbaint_t blkcnt, void *buffer)
wdenkc6097192002-11-03 00:24:07 +0000715{
716 ulong n = 0;
717 unsigned char c;
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000718 unsigned char pwrsave = 0; /* power save */
719
wdenk42dfe7a2004-03-14 22:25:36 +0000720#ifdef CONFIG_LBA48
wdenkc40b2952004-03-13 23:29:43 +0000721 unsigned char lba48 = 0;
wdenkc6097192002-11-03 00:24:07 +0000722
Guennadi Liakhovetski413bf582008-04-28 14:36:06 +0200723 if (blknr & 0x0000fffff0000000ULL) {
wdenkc40b2952004-03-13 23:29:43 +0000724 /* more than 28 bits used, use 48bit mode */
725 lba48 = 1;
726 }
727#endif
Sascha Silbeff8fef52013-06-14 13:07:25 +0200728 debug("ide_read dev %d start " LBAF ", blocks " LBAF " buffer at %lX\n",
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000729 device, blknr, blkcnt, (ulong) buffer);
wdenkc6097192002-11-03 00:24:07 +0000730
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000731 ide_led(DEVICE_LED(device), 1); /* LED on */
wdenkc6097192002-11-03 00:24:07 +0000732
733 /* Select device
734 */
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000735 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
736 c = ide_wait(device, IDE_TIME_OUT);
wdenkc6097192002-11-03 00:24:07 +0000737
738 if (c & ATA_STAT_BUSY) {
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000739 printf("IDE read: device %d not ready\n", device);
wdenkc6097192002-11-03 00:24:07 +0000740 goto IDE_READ_E;
741 }
742
743 /* first check if the drive is in Powersaving mode, if yes,
744 * increase the timeout value */
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000745 ide_outb(device, ATA_COMMAND, ATA_CMD_CHK_PWR);
746 udelay(50);
wdenkc6097192002-11-03 00:24:07 +0000747
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000748 c = ide_wait(device, IDE_TIME_OUT); /* can't take over 500 ms */
wdenkc6097192002-11-03 00:24:07 +0000749
750 if (c & ATA_STAT_BUSY) {
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000751 printf("IDE read: device %d not ready\n", device);
wdenkc6097192002-11-03 00:24:07 +0000752 goto IDE_READ_E;
753 }
754 if ((c & ATA_STAT_ERR) == ATA_STAT_ERR) {
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000755 printf("No Powersaving mode %X\n", c);
wdenkc6097192002-11-03 00:24:07 +0000756 } else {
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000757 c = ide_inb(device, ATA_SECT_CNT);
758 debug("Powersaving %02X\n", c);
759 if (c == 0)
760 pwrsave = 1;
wdenkc6097192002-11-03 00:24:07 +0000761 }
762
763
764 while (blkcnt-- > 0) {
765
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000766 c = ide_wait(device, IDE_TIME_OUT);
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 break;
771 }
wdenk42dfe7a2004-03-14 22:25:36 +0000772#ifdef CONFIG_LBA48
wdenkc40b2952004-03-13 23:29:43 +0000773 if (lba48) {
774 /* write high bits */
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000775 ide_outb(device, ATA_SECT_CNT, 0);
776 ide_outb(device, ATA_LBA_LOW, (blknr >> 24) & 0xFF);
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200777#ifdef CONFIG_SYS_64BIT_LBA
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000778 ide_outb(device, ATA_LBA_MID, (blknr >> 32) & 0xFF);
779 ide_outb(device, ATA_LBA_HIGH, (blknr >> 40) & 0xFF);
Guennadi Liakhovetski413bf582008-04-28 14:36:06 +0200780#else
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000781 ide_outb(device, ATA_LBA_MID, 0);
782 ide_outb(device, ATA_LBA_HIGH, 0);
Guennadi Liakhovetski413bf582008-04-28 14:36:06 +0200783#endif
wdenkc40b2952004-03-13 23:29:43 +0000784 }
785#endif
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000786 ide_outb(device, ATA_SECT_CNT, 1);
787 ide_outb(device, ATA_LBA_LOW, (blknr >> 0) & 0xFF);
788 ide_outb(device, ATA_LBA_MID, (blknr >> 8) & 0xFF);
789 ide_outb(device, ATA_LBA_HIGH, (blknr >> 16) & 0xFF);
wdenkc40b2952004-03-13 23:29:43 +0000790
wdenk42dfe7a2004-03-14 22:25:36 +0000791#ifdef CONFIG_LBA48
wdenkc40b2952004-03-13 23:29:43 +0000792 if (lba48) {
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000793 ide_outb(device, ATA_DEV_HD,
794 ATA_LBA | ATA_DEVICE(device));
795 ide_outb(device, ATA_COMMAND, ATA_CMD_READ_EXT);
wdenkc40b2952004-03-13 23:29:43 +0000796
797 } else
798#endif
799 {
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000800 ide_outb(device, ATA_DEV_HD, ATA_LBA |
801 ATA_DEVICE(device) | ((blknr >> 24) & 0xF));
802 ide_outb(device, ATA_COMMAND, ATA_CMD_READ);
wdenkc40b2952004-03-13 23:29:43 +0000803 }
wdenkc6097192002-11-03 00:24:07 +0000804
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000805 udelay(50);
wdenkc6097192002-11-03 00:24:07 +0000806
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000807 if (pwrsave) {
808 /* may take up to 4 sec */
809 c = ide_wait(device, IDE_SPIN_UP_TIME_OUT);
810 pwrsave = 0;
wdenkc6097192002-11-03 00:24:07 +0000811 } else {
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000812 /* can't take over 500 ms */
813 c = ide_wait(device, IDE_TIME_OUT);
wdenkc6097192002-11-03 00:24:07 +0000814 }
815
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000816 if ((c & (ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR)) !=
817 ATA_STAT_DRQ) {
Sascha Silbeff8fef52013-06-14 13:07:25 +0200818 printf("Error (no IRQ) dev %d blk " LBAF ": status "
819 "%#02x\n", device, blknr, c);
wdenkc6097192002-11-03 00:24:07 +0000820 break;
821 }
822
Pavel Herrmannf5b82c02012-10-09 07:04:39 +0000823 ide_input_data(device, buffer, ATA_SECTORWORDS);
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000824 (void) ide_inb(device, ATA_STATUS); /* clear IRQ */
wdenkc6097192002-11-03 00:24:07 +0000825
826 ++n;
827 ++blknr;
Greg Lopp0b945042007-04-13 08:02:24 +0200828 buffer += ATA_BLOCKSIZE;
wdenkc6097192002-11-03 00:24:07 +0000829 }
830IDE_READ_E:
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000831 ide_led(DEVICE_LED(device), 0); /* LED off */
wdenkc6097192002-11-03 00:24:07 +0000832 return (n);
833}
834
835/* ------------------------------------------------------------------------- */
836
837
Sascha Silbeff8fef52013-06-14 13:07:25 +0200838ulong ide_write(int device, lbaint_t blknr, lbaint_t blkcnt, const void *buffer)
wdenkc6097192002-11-03 00:24:07 +0000839{
840 ulong n = 0;
841 unsigned char c;
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000842
wdenk42dfe7a2004-03-14 22:25:36 +0000843#ifdef CONFIG_LBA48
wdenkc40b2952004-03-13 23:29:43 +0000844 unsigned char lba48 = 0;
845
Guennadi Liakhovetski413bf582008-04-28 14:36:06 +0200846 if (blknr & 0x0000fffff0000000ULL) {
wdenkc40b2952004-03-13 23:29:43 +0000847 /* more than 28 bits used, use 48bit mode */
848 lba48 = 1;
849 }
850#endif
wdenkc6097192002-11-03 00:24:07 +0000851
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000852 ide_led(DEVICE_LED(device), 1); /* LED on */
wdenkc6097192002-11-03 00:24:07 +0000853
854 /* Select device
855 */
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000856 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
wdenkc6097192002-11-03 00:24:07 +0000857
858 while (blkcnt-- > 0) {
859
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000860 c = ide_wait(device, IDE_TIME_OUT);
wdenkc6097192002-11-03 00:24:07 +0000861
862 if (c & ATA_STAT_BUSY) {
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000863 printf("IDE read: device %d not ready\n", device);
wdenkc6097192002-11-03 00:24:07 +0000864 goto WR_OUT;
865 }
wdenk42dfe7a2004-03-14 22:25:36 +0000866#ifdef CONFIG_LBA48
wdenkc40b2952004-03-13 23:29:43 +0000867 if (lba48) {
868 /* write high bits */
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000869 ide_outb(device, ATA_SECT_CNT, 0);
870 ide_outb(device, ATA_LBA_LOW, (blknr >> 24) & 0xFF);
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200871#ifdef CONFIG_SYS_64BIT_LBA
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000872 ide_outb(device, ATA_LBA_MID, (blknr >> 32) & 0xFF);
873 ide_outb(device, ATA_LBA_HIGH, (blknr >> 40) & 0xFF);
Guennadi Liakhovetski413bf582008-04-28 14:36:06 +0200874#else
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000875 ide_outb(device, ATA_LBA_MID, 0);
876 ide_outb(device, ATA_LBA_HIGH, 0);
Guennadi Liakhovetski413bf582008-04-28 14:36:06 +0200877#endif
wdenkc40b2952004-03-13 23:29:43 +0000878 }
879#endif
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000880 ide_outb(device, ATA_SECT_CNT, 1);
881 ide_outb(device, ATA_LBA_LOW, (blknr >> 0) & 0xFF);
882 ide_outb(device, ATA_LBA_MID, (blknr >> 8) & 0xFF);
883 ide_outb(device, ATA_LBA_HIGH, (blknr >> 16) & 0xFF);
wdenkc40b2952004-03-13 23:29:43 +0000884
wdenk42dfe7a2004-03-14 22:25:36 +0000885#ifdef CONFIG_LBA48
wdenkc40b2952004-03-13 23:29:43 +0000886 if (lba48) {
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000887 ide_outb(device, ATA_DEV_HD,
888 ATA_LBA | ATA_DEVICE(device));
889 ide_outb(device, ATA_COMMAND, ATA_CMD_WRITE_EXT);
wdenkc40b2952004-03-13 23:29:43 +0000890
891 } else
892#endif
893 {
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000894 ide_outb(device, ATA_DEV_HD, ATA_LBA |
895 ATA_DEVICE(device) | ((blknr >> 24) & 0xF));
896 ide_outb(device, ATA_COMMAND, ATA_CMD_WRITE);
wdenkc40b2952004-03-13 23:29:43 +0000897 }
wdenkc6097192002-11-03 00:24:07 +0000898
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000899 udelay(50);
wdenkc6097192002-11-03 00:24:07 +0000900
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000901 /* can't take over 500 ms */
902 c = ide_wait(device, IDE_TIME_OUT);
wdenkc6097192002-11-03 00:24:07 +0000903
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000904 if ((c & (ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR)) !=
905 ATA_STAT_DRQ) {
Sascha Silbeff8fef52013-06-14 13:07:25 +0200906 printf("Error (no IRQ) dev %d blk " LBAF ": status "
907 "%#02x\n", device, blknr, c);
wdenkc6097192002-11-03 00:24:07 +0000908 goto WR_OUT;
909 }
910
Pavel Herrmannf5b82c02012-10-09 07:04:39 +0000911 ide_output_data(device, buffer, ATA_SECTORWORDS);
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000912 c = ide_inb(device, ATA_STATUS); /* clear IRQ */
wdenkc6097192002-11-03 00:24:07 +0000913 ++n;
914 ++blknr;
Greg Lopp0b945042007-04-13 08:02:24 +0200915 buffer += ATA_BLOCKSIZE;
wdenkc6097192002-11-03 00:24:07 +0000916 }
917WR_OUT:
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000918 ide_led(DEVICE_LED(device), 0); /* LED off */
wdenkc6097192002-11-03 00:24:07 +0000919 return (n);
920}
921
922/* ------------------------------------------------------------------------- */
923
924/*
925 * copy src to dest, skipping leading and trailing blanks and null
926 * terminate the string
wdenk7d7ce412004-03-17 01:13:07 +0000927 * "len" is the size of available memory including the terminating '\0'
wdenkc6097192002-11-03 00:24:07 +0000928 */
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000929static void ident_cpy(unsigned char *dst, unsigned char *src,
930 unsigned int len)
wdenkc6097192002-11-03 00:24:07 +0000931{
wdenk7d7ce412004-03-17 01:13:07 +0000932 unsigned char *end, *last;
wdenkc6097192002-11-03 00:24:07 +0000933
wdenk7d7ce412004-03-17 01:13:07 +0000934 last = dst;
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000935 end = src + len - 1;
wdenk7d7ce412004-03-17 01:13:07 +0000936
937 /* reserve space for '\0' */
938 if (len < 2)
939 goto OUT;
wdenkefa329c2004-03-23 20:18:25 +0000940
wdenk7d7ce412004-03-17 01:13:07 +0000941 /* skip leading white space */
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000942 while ((*src) && (src < end) && (*src == ' '))
wdenk7d7ce412004-03-17 01:13:07 +0000943 ++src;
944
945 /* copy string, omitting trailing white space */
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000946 while ((*src) && (src < end)) {
wdenk7d7ce412004-03-17 01:13:07 +0000947 *dst++ = *src;
948 if (*src++ != ' ')
949 last = dst;
wdenkc6097192002-11-03 00:24:07 +0000950 }
wdenk7d7ce412004-03-17 01:13:07 +0000951OUT:
952 *last = '\0';
wdenkc6097192002-11-03 00:24:07 +0000953}
954
955/* ------------------------------------------------------------------------- */
956
957/*
958 * Wait until Busy bit is off, or timeout (in ms)
959 * Return last status
960 */
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000961static uchar ide_wait(int dev, ulong t)
wdenkc6097192002-11-03 00:24:07 +0000962{
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000963 ulong delay = 10 * t; /* poll every 100 us */
wdenkc6097192002-11-03 00:24:07 +0000964 uchar c;
965
wdenk2262cfe2002-11-18 00:14:45 +0000966 while ((c = ide_inb(dev, ATA_STATUS)) & ATA_STAT_BUSY) {
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000967 udelay(100);
968 if (delay-- == 0)
wdenkc6097192002-11-03 00:24:07 +0000969 break;
wdenkc6097192002-11-03 00:24:07 +0000970 }
971 return (c);
972}
973
974/* ------------------------------------------------------------------------- */
975
976#ifdef CONFIG_IDE_RESET
977extern void ide_set_reset(int idereset);
978
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000979static void ide_reset(void)
wdenkc6097192002-11-03 00:24:07 +0000980{
wdenkc6097192002-11-03 00:24:07 +0000981 int i;
982
983 curr_device = -1;
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000984 for (i = 0; i < CONFIG_SYS_IDE_MAXBUS; ++i)
wdenkc6097192002-11-03 00:24:07 +0000985 ide_bus_ok[i] = 0;
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000986 for (i = 0; i < CONFIG_SYS_IDE_MAXDEVICE; ++i)
wdenkc6097192002-11-03 00:24:07 +0000987 ide_dev_desc[i].type = DEV_TYPE_UNKNOWN;
988
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000989 ide_set_reset(1); /* assert reset */
wdenkc6097192002-11-03 00:24:07 +0000990
Martin Krausee175eac2008-04-03 13:37:56 +0200991 /* the reset signal shall be asserted for et least 25 us */
992 udelay(25);
993
wdenkc6097192002-11-03 00:24:07 +0000994 WATCHDOG_RESET();
995
wdenkc6097192002-11-03 00:24:07 +0000996 /* de-assert RESET signal */
997 ide_set_reset(0);
998
999 /* wait 250 ms */
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001000 for (i = 0; i < 250; ++i)
1001 udelay(1000);
wdenkc6097192002-11-03 00:24:07 +00001002}
1003
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001004#endif /* CONFIG_IDE_RESET */
wdenkc6097192002-11-03 00:24:07 +00001005
1006/* ------------------------------------------------------------------------- */
1007
Heiko Schocher3887c3f2009-09-23 07:56:08 +02001008#if defined(CONFIG_OF_IDE_FIXUP)
1009int ide_device_present(int dev)
1010{
1011 if (dev >= CONFIG_SYS_IDE_MAXBUS)
1012 return 0;
1013 return (ide_dev_desc[dev].type == DEV_TYPE_UNKNOWN ? 0 : 1);
1014}
1015#endif
wdenkc6097192002-11-03 00:24:07 +00001016/* ------------------------------------------------------------------------- */
1017
1018#ifdef CONFIG_ATAPI
1019/****************************************************************************
1020 * ATAPI Support
1021 */
1022
Albert Aribaudf2a37fc2010-08-08 05:17:05 +05301023#if defined(CONFIG_IDE_SWAP_IO)
wdenkc6097192002-11-03 00:24:07 +00001024/* since ATAPI may use commands with not 4 bytes alligned length
1025 * we have our own transfer functions, 2 bytes alligned */
Jeroen Hofstee288afdc2014-07-12 15:07:19 +02001026__weak void ide_output_data_shorts(int dev, ushort *sect_buf, int shorts)
wdenkc6097192002-11-03 00:24:07 +00001027{
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001028 ushort *dbuf;
1029 volatile ushort *pbuf;
wdenkc6097192002-11-03 00:24:07 +00001030
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001031 pbuf = (ushort *) (ATA_CURR_BASE(dev) + ATA_DATA_REG);
1032 dbuf = (ushort *) sect_buf;
wdenkdb01a2e2004-04-15 23:14:49 +00001033
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001034 debug("in output data shorts base for read is %lx\n",
1035 (unsigned long) pbuf);
wdenkdb01a2e2004-04-15 23:14:49 +00001036
wdenkc6097192002-11-03 00:24:07 +00001037 while (shorts--) {
wdenk5cf91d62004-04-23 20:32:05 +00001038 EIEIO;
wdenk1a344f22005-02-03 23:00:49 +00001039 *pbuf = *dbuf++;
wdenkc6097192002-11-03 00:24:07 +00001040 }
wdenk1a344f22005-02-03 23:00:49 +00001041}
1042
Jeroen Hofstee288afdc2014-07-12 15:07:19 +02001043__weak void ide_input_data_shorts(int dev, ushort *sect_buf, int shorts)
wdenk1a344f22005-02-03 23:00:49 +00001044{
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001045 ushort *dbuf;
1046 volatile ushort *pbuf;
wdenk1a344f22005-02-03 23:00:49 +00001047
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001048 pbuf = (ushort *) (ATA_CURR_BASE(dev) + ATA_DATA_REG);
1049 dbuf = (ushort *) sect_buf;
wdenk1a344f22005-02-03 23:00:49 +00001050
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001051 debug("in input data shorts base for read is %lx\n",
1052 (unsigned long) pbuf);
wdenk1a344f22005-02-03 23:00:49 +00001053
1054 while (shorts--) {
1055 EIEIO;
1056 *dbuf++ = *pbuf;
1057 }
wdenkc6097192002-11-03 00:24:07 +00001058}
1059
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001060#else /* ! CONFIG_IDE_SWAP_IO */
Jeroen Hofstee288afdc2014-07-12 15:07:19 +02001061__weak void ide_output_data_shorts(int dev, ushort *sect_buf, int shorts)
wdenk2262cfe2002-11-18 00:14:45 +00001062{
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001063 outsw(ATA_CURR_BASE(dev) + ATA_DATA_REG, sect_buf, shorts);
wdenk2262cfe2002-11-18 00:14:45 +00001064}
1065
Jeroen Hofstee288afdc2014-07-12 15:07:19 +02001066__weak void ide_input_data_shorts(int dev, ushort *sect_buf, int shorts)
wdenk2262cfe2002-11-18 00:14:45 +00001067{
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001068 insw(ATA_CURR_BASE(dev) + ATA_DATA_REG, sect_buf, shorts);
wdenk2262cfe2002-11-18 00:14:45 +00001069}
1070
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001071#endif /* CONFIG_IDE_SWAP_IO */
wdenk2262cfe2002-11-18 00:14:45 +00001072
wdenkc6097192002-11-03 00:24:07 +00001073/*
1074 * Wait until (Status & mask) == res, or timeout (in ms)
1075 * Return last status
1076 * This is used since some ATAPI CD ROMs clears their Busy Bit first
1077 * and then they set their DRQ Bit
1078 */
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001079static uchar atapi_wait_mask(int dev, ulong t, uchar mask, uchar res)
wdenkc6097192002-11-03 00:24:07 +00001080{
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001081 ulong delay = 10 * t; /* poll every 100 us */
wdenkc6097192002-11-03 00:24:07 +00001082 uchar c;
1083
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001084 /* prevents to read the status before valid */
1085 c = ide_inb(dev, ATA_DEV_CTL);
1086
wdenk2262cfe2002-11-18 00:14:45 +00001087 while (((c = ide_inb(dev, ATA_STATUS)) & mask) != res) {
wdenkc6097192002-11-03 00:24:07 +00001088 /* break if error occurs (doesn't make sense to wait more) */
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001089 if ((c & ATA_STAT_ERR) == ATA_STAT_ERR)
wdenkc6097192002-11-03 00:24:07 +00001090 break;
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001091 udelay(100);
1092 if (delay-- == 0)
wdenkc6097192002-11-03 00:24:07 +00001093 break;
wdenkc6097192002-11-03 00:24:07 +00001094 }
1095 return (c);
1096}
1097
1098/*
1099 * issue an atapi command
1100 */
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001101unsigned char atapi_issue(int device, unsigned char *ccb, int ccblen,
1102 unsigned char *buffer, int buflen)
wdenkc6097192002-11-03 00:24:07 +00001103{
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001104 unsigned char c, err, mask, res;
wdenkc6097192002-11-03 00:24:07 +00001105 int n;
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001106
1107 ide_led(DEVICE_LED(device), 1); /* LED on */
wdenkc6097192002-11-03 00:24:07 +00001108
1109 /* Select device
1110 */
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001111 mask = ATA_STAT_BUSY | ATA_STAT_DRQ;
wdenkc6097192002-11-03 00:24:07 +00001112 res = 0;
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001113 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
1114 c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
wdenkc6097192002-11-03 00:24:07 +00001115 if ((c & mask) != res) {
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001116 printf("ATAPI_ISSUE: device %d not ready status %X\n", device,
1117 c);
1118 err = 0xFF;
wdenkc6097192002-11-03 00:24:07 +00001119 goto AI_OUT;
1120 }
1121 /* write taskfile */
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001122 ide_outb(device, ATA_ERROR_REG, 0); /* no DMA, no overlaped */
1123 ide_outb(device, ATA_SECT_CNT, 0);
1124 ide_outb(device, ATA_SECT_NUM, 0);
1125 ide_outb(device, ATA_CYL_LOW, (unsigned char) (buflen & 0xFF));
1126 ide_outb(device, ATA_CYL_HIGH,
1127 (unsigned char) ((buflen >> 8) & 0xFF));
1128 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
wdenkc6097192002-11-03 00:24:07 +00001129
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001130 ide_outb(device, ATA_COMMAND, ATAPI_CMD_PACKET);
1131 udelay(50);
wdenkc6097192002-11-03 00:24:07 +00001132
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001133 mask = ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR;
wdenkc6097192002-11-03 00:24:07 +00001134 res = ATA_STAT_DRQ;
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001135 c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
wdenkc6097192002-11-03 00:24:07 +00001136
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001137 if ((c & mask) != res) { /* DRQ must be 1, BSY 0 */
1138 printf("ATAPI_ISSUE: Error (no IRQ) before sending ccb dev %d status 0x%02x\n",
1139 device, c);
1140 err = 0xFF;
wdenkc6097192002-11-03 00:24:07 +00001141 goto AI_OUT;
1142 }
1143
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001144 /* write command block */
Pavel Herrmannf5b82c02012-10-09 07:04:39 +00001145 ide_output_data_shorts(device, (unsigned short *) ccb, ccblen / 2);
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001146
Wolfgang Denk53677ef2008-05-20 16:00:29 +02001147 /* ATAPI Command written wait for completition */
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001148 udelay(5000); /* device must set bsy */
wdenkc6097192002-11-03 00:24:07 +00001149
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001150 mask = ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR;
1151 /*
1152 * if no data wait for DRQ = 0 BSY = 0
1153 * if data wait for DRQ = 1 BSY = 0
1154 */
1155 res = 0;
1156 if (buflen)
wdenkc6097192002-11-03 00:24:07 +00001157 res = ATA_STAT_DRQ;
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001158 c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
1159 if ((c & mask) != res) {
wdenkc6097192002-11-03 00:24:07 +00001160 if (c & ATA_STAT_ERR) {
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001161 err = (ide_inb(device, ATA_ERROR_REG)) >> 4;
1162 debug("atapi_issue 1 returned sense key %X status %02X\n",
1163 err, c);
wdenkc6097192002-11-03 00:24:07 +00001164 } else {
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001165 printf("ATAPI_ISSUE: (no DRQ) after sending ccb (%x) status 0x%02x\n",
1166 ccb[0], c);
1167 err = 0xFF;
wdenkc6097192002-11-03 00:24:07 +00001168 }
1169 goto AI_OUT;
1170 }
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001171 n = ide_inb(device, ATA_CYL_HIGH);
1172 n <<= 8;
1173 n += ide_inb(device, ATA_CYL_LOW);
1174 if (n > buflen) {
1175 printf("ERROR, transfer bytes %d requested only %d\n", n,
1176 buflen);
1177 err = 0xff;
wdenkc6097192002-11-03 00:24:07 +00001178 goto AI_OUT;
1179 }
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001180 if ((n == 0) && (buflen < 0)) {
1181 printf("ERROR, transfer bytes %d requested %d\n", n, buflen);
1182 err = 0xff;
wdenkc6097192002-11-03 00:24:07 +00001183 goto AI_OUT;
1184 }
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001185 if (n != buflen) {
1186 debug("WARNING, transfer bytes %d not equal with requested %d\n",
1187 n, buflen);
wdenkc6097192002-11-03 00:24:07 +00001188 }
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001189 if (n != 0) { /* data transfer */
1190 debug("ATAPI_ISSUE: %d Bytes to transfer\n", n);
1191 /* we transfer shorts */
1192 n >>= 1;
wdenkc6097192002-11-03 00:24:07 +00001193 /* ok now decide if it is an in or output */
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001194 if ((ide_inb(device, ATA_SECT_CNT) & 0x02) == 0) {
1195 debug("Write to device\n");
Pavel Herrmannf5b82c02012-10-09 07:04:39 +00001196 ide_output_data_shorts(device,
1197 (unsigned short *) buffer, n);
wdenkc6097192002-11-03 00:24:07 +00001198 } else {
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001199 debug("Read from device @ %p shorts %d\n", buffer, n);
Pavel Herrmannf5b82c02012-10-09 07:04:39 +00001200 ide_input_data_shorts(device,
1201 (unsigned short *) buffer, n);
wdenkc6097192002-11-03 00:24:07 +00001202 }
1203 }
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001204 udelay(5000); /* seems that some CD ROMs need this... */
1205 mask = ATA_STAT_BUSY | ATA_STAT_ERR;
1206 res = 0;
1207 c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
wdenkc6097192002-11-03 00:24:07 +00001208 if ((c & ATA_STAT_ERR) == ATA_STAT_ERR) {
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001209 err = (ide_inb(device, ATA_ERROR_REG) >> 4);
1210 debug("atapi_issue 2 returned sense key %X status %X\n", err,
1211 c);
wdenkc6097192002-11-03 00:24:07 +00001212 } else {
1213 err = 0;
1214 }
1215AI_OUT:
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001216 ide_led(DEVICE_LED(device), 0); /* LED off */
wdenkc6097192002-11-03 00:24:07 +00001217 return (err);
1218}
1219
1220/*
1221 * sending the command to atapi_issue. If an status other than good
1222 * returns, an request_sense will be issued
1223 */
1224
Wolfgang Denk53677ef2008-05-20 16:00:29 +02001225#define ATAPI_DRIVE_NOT_READY 100
wdenkc6097192002-11-03 00:24:07 +00001226#define ATAPI_UNIT_ATTN 10
1227
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001228unsigned char atapi_issue_autoreq(int device,
1229 unsigned char *ccb,
1230 int ccblen,
1231 unsigned char *buffer, int buflen)
wdenkc6097192002-11-03 00:24:07 +00001232{
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001233 unsigned char sense_data[18], sense_ccb[12];
1234 unsigned char res, key, asc, ascq;
1235 int notready, unitattn;
wdenkc6097192002-11-03 00:24:07 +00001236
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001237 unitattn = ATAPI_UNIT_ATTN;
1238 notready = ATAPI_DRIVE_NOT_READY;
wdenkc6097192002-11-03 00:24:07 +00001239
1240retry:
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001241 res = atapi_issue(device, ccb, ccblen, buffer, buflen);
1242 if (res == 0)
1243 return 0; /* Ok */
wdenkc6097192002-11-03 00:24:07 +00001244
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001245 if (res == 0xFF)
1246 return 0xFF; /* error */
wdenkc6097192002-11-03 00:24:07 +00001247
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001248 debug("(auto_req)atapi_issue returned sense key %X\n", res);
wdenkc6097192002-11-03 00:24:07 +00001249
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001250 memset(sense_ccb, 0, sizeof(sense_ccb));
1251 memset(sense_data, 0, sizeof(sense_data));
1252 sense_ccb[0] = ATAPI_CMD_REQ_SENSE;
1253 sense_ccb[4] = 18; /* allocation Length */
wdenkc6097192002-11-03 00:24:07 +00001254
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001255 res = atapi_issue(device, sense_ccb, 12, sense_data, 18);
1256 key = (sense_data[2] & 0xF);
1257 asc = (sense_data[12]);
1258 ascq = (sense_data[13]);
wdenkc6097192002-11-03 00:24:07 +00001259
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001260 debug("ATAPI_CMD_REQ_SENSE returned %x\n", res);
1261 debug(" Sense page: %02X key %02X ASC %02X ASCQ %02X\n",
1262 sense_data[0], key, asc, ascq);
wdenkc6097192002-11-03 00:24:07 +00001263
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001264 if ((key == 0))
1265 return 0; /* ok device ready */
wdenkc6097192002-11-03 00:24:07 +00001266
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001267 if ((key == 6) || (asc == 0x29) || (asc == 0x28)) { /* Unit Attention */
1268 if (unitattn-- > 0) {
1269 udelay(200 * 1000);
wdenkc6097192002-11-03 00:24:07 +00001270 goto retry;
1271 }
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001272 printf("Unit Attention, tried %d\n", ATAPI_UNIT_ATTN);
wdenkc6097192002-11-03 00:24:07 +00001273 goto error;
1274 }
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001275 if ((asc == 0x4) && (ascq == 0x1)) {
1276 /* not ready, but will be ready soon */
1277 if (notready-- > 0) {
1278 udelay(200 * 1000);
wdenkc6097192002-11-03 00:24:07 +00001279 goto retry;
1280 }
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001281 printf("Drive not ready, tried %d times\n",
1282 ATAPI_DRIVE_NOT_READY);
wdenkc6097192002-11-03 00:24:07 +00001283 goto error;
1284 }
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001285 if (asc == 0x3a) {
1286 debug("Media not present\n");
wdenkc6097192002-11-03 00:24:07 +00001287 goto error;
1288 }
wdenkc7de8292002-11-19 11:04:11 +00001289
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001290 printf("ERROR: Unknown Sense key %02X ASC %02X ASCQ %02X\n", key, asc,
1291 ascq);
wdenkc6097192002-11-03 00:24:07 +00001292error:
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001293 debug("ERROR Sense key %02X ASC %02X ASCQ %02X\n", key, asc, ascq);
wdenkc6097192002-11-03 00:24:07 +00001294 return (0xFF);
1295}
1296
1297
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001298static void atapi_inquiry(block_dev_desc_t *dev_desc)
wdenkc6097192002-11-03 00:24:07 +00001299{
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001300 unsigned char ccb[12]; /* Command descriptor block */
1301 unsigned char iobuf[64]; /* temp buf */
wdenkc6097192002-11-03 00:24:07 +00001302 unsigned char c;
1303 int device;
1304
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001305 device = dev_desc->dev;
1306 dev_desc->type = DEV_TYPE_UNKNOWN; /* not yet valid */
1307 dev_desc->block_read = atapi_read;
wdenkc6097192002-11-03 00:24:07 +00001308
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001309 memset(ccb, 0, sizeof(ccb));
1310 memset(iobuf, 0, sizeof(iobuf));
wdenkc6097192002-11-03 00:24:07 +00001311
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001312 ccb[0] = ATAPI_CMD_INQUIRY;
1313 ccb[4] = 40; /* allocation Legnth */
1314 c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *) iobuf, 40);
wdenkc6097192002-11-03 00:24:07 +00001315
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001316 debug("ATAPI_CMD_INQUIRY returned %x\n", c);
1317 if (c != 0)
wdenkc6097192002-11-03 00:24:07 +00001318 return;
1319
1320 /* copy device ident strings */
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001321 ident_cpy((unsigned char *) dev_desc->vendor, &iobuf[8], 8);
1322 ident_cpy((unsigned char *) dev_desc->product, &iobuf[16], 16);
1323 ident_cpy((unsigned char *) dev_desc->revision, &iobuf[32], 5);
wdenkc6097192002-11-03 00:24:07 +00001324
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001325 dev_desc->lun = 0;
1326 dev_desc->lba = 0;
1327 dev_desc->blksz = 0;
Egbert Eich0472fbf2013-04-09 21:11:56 +00001328 dev_desc->log2blksz = LOG2_INVALID(typeof(dev_desc->log2blksz));
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001329 dev_desc->type = iobuf[0] & 0x1f;
wdenkc6097192002-11-03 00:24:07 +00001330
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001331 if ((iobuf[1] & 0x80) == 0x80)
wdenkc6097192002-11-03 00:24:07 +00001332 dev_desc->removable = 1;
1333 else
1334 dev_desc->removable = 0;
1335
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001336 memset(ccb, 0, sizeof(ccb));
1337 memset(iobuf, 0, sizeof(iobuf));
1338 ccb[0] = ATAPI_CMD_START_STOP;
1339 ccb[4] = 0x03; /* start */
wdenkc6097192002-11-03 00:24:07 +00001340
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001341 c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *) iobuf, 0);
wdenkc6097192002-11-03 00:24:07 +00001342
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001343 debug("ATAPI_CMD_START_STOP returned %x\n", c);
1344 if (c != 0)
wdenkc6097192002-11-03 00:24:07 +00001345 return;
1346
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001347 memset(ccb, 0, sizeof(ccb));
1348 memset(iobuf, 0, sizeof(iobuf));
1349 c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *) iobuf, 0);
wdenkc6097192002-11-03 00:24:07 +00001350
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001351 debug("ATAPI_CMD_UNIT_TEST_READY returned %x\n", c);
1352 if (c != 0)
wdenkc6097192002-11-03 00:24:07 +00001353 return;
1354
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001355 memset(ccb, 0, sizeof(ccb));
1356 memset(iobuf, 0, sizeof(iobuf));
1357 ccb[0] = ATAPI_CMD_READ_CAP;
1358 c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *) iobuf, 8);
1359 debug("ATAPI_CMD_READ_CAP returned %x\n", c);
1360 if (c != 0)
wdenkc6097192002-11-03 00:24:07 +00001361 return;
1362
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001363 debug("Read Cap: LBA %02X%02X%02X%02X blksize %02X%02X%02X%02X\n",
1364 iobuf[0], iobuf[1], iobuf[2], iobuf[3],
1365 iobuf[4], iobuf[5], iobuf[6], iobuf[7]);
wdenkc6097192002-11-03 00:24:07 +00001366
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001367 dev_desc->lba = ((unsigned long) iobuf[0] << 24) +
1368 ((unsigned long) iobuf[1] << 16) +
1369 ((unsigned long) iobuf[2] << 8) + ((unsigned long) iobuf[3]);
1370 dev_desc->blksz = ((unsigned long) iobuf[4] << 24) +
1371 ((unsigned long) iobuf[5] << 16) +
1372 ((unsigned long) iobuf[6] << 8) + ((unsigned long) iobuf[7]);
Egbert Eich0472fbf2013-04-09 21:11:56 +00001373 dev_desc->log2blksz = LOG2(dev_desc->blksz);
wdenk42dfe7a2004-03-14 22:25:36 +00001374#ifdef CONFIG_LBA48
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001375 /* ATAPI devices cannot use 48bit addressing (ATA/ATAPI v7) */
1376 dev_desc->lba48 = 0;
wdenk42dfe7a2004-03-14 22:25:36 +00001377#endif
wdenkc6097192002-11-03 00:24:07 +00001378 return;
1379}
1380
1381
1382/*
1383 * atapi_read:
1384 * we transfer only one block per command, since the multiple DRQ per
1385 * command is not yet implemented
1386 */
1387#define ATAPI_READ_MAX_BYTES 2048 /* we read max 2kbytes */
1388#define ATAPI_READ_BLOCK_SIZE 2048 /* assuming CD part */
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001389#define ATAPI_READ_MAX_BLOCK (ATAPI_READ_MAX_BYTES/ATAPI_READ_BLOCK_SIZE)
wdenkc6097192002-11-03 00:24:07 +00001390
Simon Glassb6458d32012-10-29 05:24:04 +00001391ulong atapi_read(int device, ulong blknr, lbaint_t blkcnt, void *buffer)
wdenkc6097192002-11-03 00:24:07 +00001392{
1393 ulong n = 0;
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001394 unsigned char ccb[12]; /* Command descriptor block */
wdenkc6097192002-11-03 00:24:07 +00001395 ulong cnt;
1396
Simon Glassb6458d32012-10-29 05:24:04 +00001397 debug("atapi_read dev %d start %lX, blocks " LBAF " buffer at %lX\n",
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001398 device, blknr, blkcnt, (ulong) buffer);
wdenkc6097192002-11-03 00:24:07 +00001399
1400 do {
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001401 if (blkcnt > ATAPI_READ_MAX_BLOCK)
1402 cnt = ATAPI_READ_MAX_BLOCK;
1403 else
1404 cnt = blkcnt;
wdenkc6097192002-11-03 00:24:07 +00001405
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001406 ccb[0] = ATAPI_CMD_READ_12;
1407 ccb[1] = 0; /* reserved */
1408 ccb[2] = (unsigned char) (blknr >> 24) & 0xFF; /* MSB Block */
1409 ccb[3] = (unsigned char) (blknr >> 16) & 0xFF; /* */
1410 ccb[4] = (unsigned char) (blknr >> 8) & 0xFF;
1411 ccb[5] = (unsigned char) blknr & 0xFF; /* LSB Block */
1412 ccb[6] = (unsigned char) (cnt >> 24) & 0xFF; /* MSB Block cnt */
1413 ccb[7] = (unsigned char) (cnt >> 16) & 0xFF;
1414 ccb[8] = (unsigned char) (cnt >> 8) & 0xFF;
1415 ccb[9] = (unsigned char) cnt & 0xFF; /* LSB Block */
1416 ccb[10] = 0; /* reserved */
1417 ccb[11] = 0; /* reserved */
1418
1419 if (atapi_issue_autoreq(device, ccb, 12,
1420 (unsigned char *) buffer,
1421 cnt * ATAPI_READ_BLOCK_SIZE)
1422 == 0xFF) {
wdenkc6097192002-11-03 00:24:07 +00001423 return (n);
1424 }
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001425 n += cnt;
1426 blkcnt -= cnt;
1427 blknr += cnt;
1428 buffer += (cnt * ATAPI_READ_BLOCK_SIZE);
wdenkc6097192002-11-03 00:24:07 +00001429 } while (blkcnt > 0);
1430 return (n);
1431}
1432
1433/* ------------------------------------------------------------------------- */
1434
1435#endif /* CONFIG_ATAPI */
1436
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001437U_BOOT_CMD(ide, 5, 1, do_ide,
1438 "IDE sub-system",
1439 "reset - reset IDE controller\n"
1440 "ide info - show available IDE devices\n"
1441 "ide device [dev] - show or set current device\n"
1442 "ide part [dev] - print partition table of one or all IDE devices\n"
1443 "ide read addr blk# cnt\n"
1444 "ide write addr blk# cnt - read/write `cnt'"
1445 " blocks starting at block `blk#'\n"
1446 " to/from memory address `addr'");
wdenk8bde7f72003-06-27 21:31:46 +00001447
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001448U_BOOT_CMD(diskboot, 3, 1, do_diskboot,
1449 "boot from IDE device", "loadAddr dev:part");