blob: 5b46e3ca5edc2b1ee6b8ca38e7b6c2b30f02d301 [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 *
5 * See file CREDITS for list of people who contributed to this
6 * project.
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of
11 * the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
21 * MA 02111-1307 USA
22 *
23 */
24
25/*
26 * IDE support
27 */
Albert Aribaud113bfe42010-08-08 05:17:06 +053028
wdenkc6097192002-11-03 00:24:07 +000029#include <common.h>
30#include <config.h>
31#include <watchdog.h>
32#include <command.h>
33#include <image.h>
34#include <asm/byteorder.h>
Heiko Schocherf98984c2007-08-28 17:39:14 +020035#include <asm/io.h>
Grant Likely735dd972007-02-20 09:04:34 +010036
wdenkc6097192002-11-03 00:24:07 +000037#if defined(CONFIG_IDE_8xx_DIRECT) || defined(CONFIG_IDE_PCMCIA)
38# include <pcmcia.h>
39#endif
Grant Likely735dd972007-02-20 09:04:34 +010040
wdenkc6097192002-11-03 00:24:07 +000041#ifdef CONFIG_8xx
42# include <mpc8xx.h>
43#endif
Grant Likely735dd972007-02-20 09:04:34 +010044
wdenk132ba5f2004-02-27 08:20:54 +000045#ifdef CONFIG_MPC5xxx
46#include <mpc5xxx.h>
47#endif
Grant Likely735dd972007-02-20 09:04:34 +010048
wdenkc6097192002-11-03 00:24:07 +000049#include <ide.h>
50#include <ata.h>
Grant Likely735dd972007-02-20 09:04:34 +010051
wdenkc6097192002-11-03 00:24:07 +000052#ifdef CONFIG_STATUS_LED
53# include <status_led.h>
54#endif
Grant Likely735dd972007-02-20 09:04:34 +010055
wdenk5cf91d62004-04-23 20:32:05 +000056#ifdef __PPC__
57# define EIEIO __asm__ volatile ("eieio")
wdenk1a344f22005-02-03 23:00:49 +000058# define SYNC __asm__ volatile ("sync")
wdenk5cf91d62004-04-23 20:32:05 +000059#else
60# define EIEIO /* nothing */
wdenk1a344f22005-02-03 23:00:49 +000061# define SYNC /* nothing */
wdenkc6097192002-11-03 00:24:07 +000062#endif
63
wdenkc6097192002-11-03 00:24:07 +000064/* ------------------------------------------------------------------------- */
65
66/* Current I/O Device */
67static int curr_device = -1;
68
69/* Current offset for IDE0 / IDE1 bus access */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020070ulong ide_bus_offset[CONFIG_SYS_IDE_MAXBUS] = {
71#if defined(CONFIG_SYS_ATA_IDE0_OFFSET)
72 CONFIG_SYS_ATA_IDE0_OFFSET,
wdenkc6097192002-11-03 00:24:07 +000073#endif
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020074#if defined(CONFIG_SYS_ATA_IDE1_OFFSET) && (CONFIG_SYS_IDE_MAXBUS > 1)
75 CONFIG_SYS_ATA_IDE1_OFFSET,
wdenkc6097192002-11-03 00:24:07 +000076#endif
77};
78
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020079static int ide_bus_ok[CONFIG_SYS_IDE_MAXBUS];
wdenkc6097192002-11-03 00:24:07 +000080
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020081block_dev_desc_t ide_dev_desc[CONFIG_SYS_IDE_MAXDEVICE];
wdenkc6097192002-11-03 00:24:07 +000082/* ------------------------------------------------------------------------- */
83
wdenkc6097192002-11-03 00:24:07 +000084#ifdef CONFIG_IDE_RESET
85static void ide_reset (void);
86#else
87#define ide_reset() /* dummy */
88#endif
89
90static void ide_ident (block_dev_desc_t *dev_desc);
91static uchar ide_wait (int dev, ulong t);
92
93#define IDE_TIME_OUT 2000 /* 2 sec timeout */
94
95#define ATAPI_TIME_OUT 7000 /* 7 sec timeout (5 sec seems to work...) */
96
97#define IDE_SPIN_UP_TIME_OUT 5000 /* 5 sec spin-up timeout */
98
wdenkc6097192002-11-03 00:24:07 +000099static void ident_cpy (unsigned char *dest, unsigned char *src, unsigned int len);
100
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200101#ifndef CONFIG_SYS_ATA_PORT_ADDR
102#define CONFIG_SYS_ATA_PORT_ADDR(port) (port)
Heiko Schocher566a4942007-06-22 19:11:54 +0200103#endif
wdenkc6097192002-11-03 00:24:07 +0000104
105#ifdef CONFIG_ATAPI
106static void atapi_inquiry(block_dev_desc_t *dev_desc);
Grant Likelyeb867a72007-02-20 09:05:45 +0100107ulong atapi_read (int device, lbaint_t blknr, ulong blkcnt, void *buffer);
wdenkc6097192002-11-03 00:24:07 +0000108#endif
109
110
wdenkc6097192002-11-03 00:24:07 +0000111/* ------------------------------------------------------------------------- */
112
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000113int do_ide(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
wdenkc6097192002-11-03 00:24:07 +0000114{
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000115 int rcode = 0;
wdenkc6097192002-11-03 00:24:07 +0000116
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000117 switch (argc) {
118 case 0:
119 case 1:
Simon Glass4c12eeb2011-12-10 08:44:01 +0000120 return CMD_RET_USAGE;
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000121 case 2:
122 if (strncmp(argv[1], "res", 3) == 0) {
123 puts("\nReset IDE"
124#ifdef CONFIG_IDE_8xx_DIRECT
125 " on PCMCIA " PCMCIA_SLOT_MSG
126#endif
127 ": ");
wdenkc6097192002-11-03 00:24:07 +0000128
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000129 ide_init();
130 return 0;
131 } else if (strncmp(argv[1], "inf", 3) == 0) {
132 int i;
133
134 putc('\n');
135
136 for (i = 0; i < CONFIG_SYS_IDE_MAXDEVICE; ++i) {
137 if (ide_dev_desc[i].type == DEV_TYPE_UNKNOWN)
138 continue; /* list only known devices */
139 printf("IDE device %d: ", i);
140 dev_print(&ide_dev_desc[i]);
141 }
142 return 0;
143
144 } else if (strncmp(argv[1], "dev", 3) == 0) {
145 if ((curr_device < 0)
146 || (curr_device >= CONFIG_SYS_IDE_MAXDEVICE)) {
147 puts("\nno IDE devices available\n");
148 return 1;
149 }
150 printf("\nIDE device %d: ", curr_device);
151 dev_print(&ide_dev_desc[curr_device]);
152 return 0;
153 } else if (strncmp(argv[1], "part", 4) == 0) {
154 int dev, ok;
155
156 for (ok = 0, dev = 0;
157 dev < CONFIG_SYS_IDE_MAXDEVICE;
158 ++dev) {
159 if (ide_dev_desc[dev].part_type !=
160 PART_TYPE_UNKNOWN) {
161 ++ok;
162 if (dev)
163 putc('\n');
164 print_part(&ide_dev_desc[dev]);
165 }
166 }
167 if (!ok) {
168 puts("\nno IDE devices available\n");
169 rcode++;
170 }
171 return rcode;
172 }
Simon Glass4c12eeb2011-12-10 08:44:01 +0000173 return CMD_RET_USAGE;
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000174 case 3:
175 if (strncmp(argv[1], "dev", 3) == 0) {
176 int dev = (int) simple_strtoul(argv[2], NULL, 10);
177
178 printf("\nIDE device %d: ", dev);
179 if (dev >= CONFIG_SYS_IDE_MAXDEVICE) {
180 puts("unknown device\n");
181 return 1;
182 }
183 dev_print(&ide_dev_desc[dev]);
184 /*ide_print (dev); */
185
186 if (ide_dev_desc[dev].type == DEV_TYPE_UNKNOWN)
187 return 1;
188
189 curr_device = dev;
190
191 puts("... is now current device\n");
192
193 return 0;
194 } else if (strncmp(argv[1], "part", 4) == 0) {
195 int dev = (int) simple_strtoul(argv[2], NULL, 10);
196
197 if (ide_dev_desc[dev].part_type != PART_TYPE_UNKNOWN) {
198 print_part(&ide_dev_desc[dev]);
199 } else {
200 printf("\nIDE device %d not available\n",
201 dev);
202 rcode = 1;
203 }
204 return rcode;
205 }
206
Simon Glass4c12eeb2011-12-10 08:44:01 +0000207 return CMD_RET_USAGE;
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000208 default:
209 /* at least 4 args */
210
211 if (strcmp(argv[1], "read") == 0) {
212 ulong addr = simple_strtoul(argv[2], NULL, 16);
213 ulong cnt = simple_strtoul(argv[4], NULL, 16);
214 ulong n;
215
216#ifdef CONFIG_SYS_64BIT_LBA
217 lbaint_t blk = simple_strtoull(argv[3], NULL, 16);
218
219 printf("\nIDE read: device %d block # %lld, count %ld ... ",
220 curr_device, blk, cnt);
221#else
222 lbaint_t blk = simple_strtoul(argv[3], NULL, 16);
223
224 printf("\nIDE read: device %d block # %ld, count %ld ... ",
225 curr_device, blk, cnt);
226#endif
227
228 n = ide_dev_desc[curr_device].block_read(curr_device,
229 blk, cnt,
230 (ulong *)addr);
231 /* flush cache after read */
232 flush_cache(addr,
233 cnt * ide_dev_desc[curr_device].blksz);
234
235 printf("%ld blocks read: %s\n",
236 n, (n == cnt) ? "OK" : "ERROR");
237 if (n == cnt)
238 return 0;
239 else
240 return 1;
241 } else if (strcmp(argv[1], "write") == 0) {
242 ulong addr = simple_strtoul(argv[2], NULL, 16);
243 ulong cnt = simple_strtoul(argv[4], NULL, 16);
244 ulong n;
245
246#ifdef CONFIG_SYS_64BIT_LBA
247 lbaint_t blk = simple_strtoull(argv[3], NULL, 16);
248
249 printf("\nIDE write: device %d block # %lld, count %ld ... ",
250 curr_device, blk, cnt);
251#else
252 lbaint_t blk = simple_strtoul(argv[3], NULL, 16);
253
254 printf("\nIDE write: device %d block # %ld, count %ld ... ",
255 curr_device, blk, cnt);
256#endif
257 n = ide_write(curr_device, blk, cnt, (ulong *) addr);
258
259 printf("%ld blocks written: %s\n",
260 n, (n == cnt) ? "OK" : "ERROR");
261 if (n == cnt)
262 return 0;
263 else
264 return 1;
265 } else {
Simon Glass4c12eeb2011-12-10 08:44:01 +0000266 return CMD_RET_USAGE;
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000267 }
268
269 return rcode;
270 }
wdenkc6097192002-11-03 00:24:07 +0000271}
272
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000273int do_diskboot(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
wdenkc6097192002-11-03 00:24:07 +0000274{
Rob Herring7405a132012-09-21 04:02:30 +0000275 return common_diskboot(cmdtp, "ide", argc, argv);
wdenkc6097192002-11-03 00:24:07 +0000276}
277
278/* ------------------------------------------------------------------------- */
279
Pavel Herrmann19be2ea2012-10-07 05:56:10 +0000280void __ide_led(uchar led, uchar status)
281{
282#if defined(CONFIG_IDE_LED) && defined(PER8_BASE) /* required by LED_PORT */
283 static uchar led_buffer; /* Buffer for current LED status */
284
285 uchar *led_port = LED_PORT;
286
287 if (status) /* switch LED on */
288 led_buffer |= led;
289 else /* switch LED off */
290 led_buffer &= ~led;
291
292 *led_port = led_buffer;
293#endif
294}
295
296void ide_led(uchar led, uchar status)
297 __attribute__ ((weak, alias("__ide_led")));
298
299#ifndef CONFIG_IDE_LED /* define LED macros, they are not used anyways */
300# define DEVICE_LED(x) 0
301# define LED_IDE1 1
302# define LED_IDE2 2
303#endif
304
305/* ------------------------------------------------------------------------- */
306
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000307inline void __ide_outb(int dev, int port, unsigned char val)
Stefan Roesef2302d42008-08-06 14:05:38 +0200308{
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000309 debug("ide_outb (dev= %d, port= 0x%x, val= 0x%02x) : @ 0x%08lx\n",
310 dev, port, val,
311 (ATA_CURR_BASE(dev) + CONFIG_SYS_ATA_PORT_ADDR(port)));
Macpaul Lin0abddf82011-04-11 20:45:32 +0000312
313#if defined(CONFIG_IDE_AHB)
314 if (port) {
315 /* write command */
316 ide_write_register(dev, port, val);
317 } else {
318 /* write data */
319 outb(val, (ATA_CURR_BASE(dev)));
320 }
321#else
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000322 outb(val, (ATA_CURR_BASE(dev) + CONFIG_SYS_ATA_PORT_ADDR(port)));
Macpaul Lin0abddf82011-04-11 20:45:32 +0000323#endif
Stefan Roesef2302d42008-08-06 14:05:38 +0200324}
Macpaul Lin0abddf82011-04-11 20:45:32 +0000325
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000326void ide_outb(int dev, int port, unsigned char val)
327 __attribute__ ((weak, alias("__ide_outb")));
Stefan Roesef2302d42008-08-06 14:05:38 +0200328
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000329inline unsigned char __ide_inb(int dev, int port)
Stefan Roesef2302d42008-08-06 14:05:38 +0200330{
331 uchar val;
Macpaul Lin0abddf82011-04-11 20:45:32 +0000332
333#if defined(CONFIG_IDE_AHB)
334 val = ide_read_register(dev, port);
335#else
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000336 val = inb((ATA_CURR_BASE(dev) + CONFIG_SYS_ATA_PORT_ADDR(port)));
Macpaul Lin0abddf82011-04-11 20:45:32 +0000337#endif
338
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000339 debug("ide_inb (dev= %d, port= 0x%x) : @ 0x%08lx -> 0x%02x\n",
340 dev, port,
341 (ATA_CURR_BASE(dev) + CONFIG_SYS_ATA_PORT_ADDR(port)), val);
Stefan Roesef2302d42008-08-06 14:05:38 +0200342 return val;
343}
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000344
Kim Phillips2df72b82009-05-19 12:53:36 -0500345unsigned char ide_inb(int dev, int port)
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000346 __attribute__ ((weak, alias("__ide_inb")));
Stefan Roesef2302d42008-08-06 14:05:38 +0200347
Steven A. Falco36c2d302008-08-15 15:34:10 -0400348#ifdef CONFIG_TUNE_PIO
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000349inline int __ide_set_piomode(int pio_mode)
Steven A. Falco36c2d302008-08-15 15:34:10 -0400350{
351 return 0;
352}
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000353
354inline int ide_set_piomode(int pio_mode)
355 __attribute__ ((weak, alias("__ide_set_piomode")));
Steven A. Falco36c2d302008-08-15 15:34:10 -0400356#endif
357
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000358void ide_init(void)
wdenkc6097192002-11-03 00:24:07 +0000359{
wdenkc6097192002-11-03 00:24:07 +0000360 unsigned char c;
361 int i, bus;
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000362
wdenk9fd5e312003-12-07 23:55:12 +0000363#ifdef CONFIG_IDE_8xx_PCCARD
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000364 extern int ide_devices_found; /* Initialized in check_ide_device() */
365#endif /* CONFIG_IDE_8xx_PCCARD */
wdenk9fd5e312003-12-07 23:55:12 +0000366
367#ifdef CONFIG_IDE_PREINIT
368 WATCHDOG_RESET();
369
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000370 if (ide_preinit()) {
371 puts("ide_preinit failed\n");
wdenk9fd5e312003-12-07 23:55:12 +0000372 return;
373 }
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000374#endif /* CONFIG_IDE_PREINIT */
wdenkc6097192002-11-03 00:24:07 +0000375
wdenkc6097192002-11-03 00:24:07 +0000376 WATCHDOG_RESET();
377
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000378 /*
379 * Reset the IDE just to be sure.
wdenkc6097192002-11-03 00:24:07 +0000380 * Light LED's to show
381 */
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000382 ide_led((LED_IDE1 | LED_IDE2), 1); /* LED's on */
383
384 /* ATAPI Drives seems to need a proper IDE Reset */
385 ide_reset();
wdenkc6097192002-11-03 00:24:07 +0000386
Pavel Herrmann8d1165e11a2012-10-09 07:01:56 +0000387#ifdef CONFIG_IDE_INIT_POSTRESET
388 WATCHDOG_RESET();
wdenkc6097192002-11-03 00:24:07 +0000389
Pavel Herrmann8d1165e11a2012-10-09 07:01:56 +0000390 if (ide_init_postreset()) {
391 puts("ide_preinit_postreset failed\n");
392 return;
393 }
394#endif /* CONFIG_IDE_INIT_POSTRESET */
wdenkc6097192002-11-03 00:24:07 +0000395
396 /*
397 * Wait for IDE to get ready.
398 * According to spec, this can take up to 31 seconds!
399 */
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000400 for (bus = 0; bus < CONFIG_SYS_IDE_MAXBUS; ++bus) {
401 int dev =
402 bus * (CONFIG_SYS_IDE_MAXDEVICE /
403 CONFIG_SYS_IDE_MAXBUS);
wdenkc6097192002-11-03 00:24:07 +0000404
wdenk6069ff22003-02-28 00:49:47 +0000405#ifdef CONFIG_IDE_8xx_PCCARD
406 /* Skip non-ide devices from probing */
407 if ((ide_devices_found & (1 << bus)) == 0) {
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000408 ide_led((LED_IDE1 | LED_IDE2), 0); /* LED's off */
wdenk6069ff22003-02-28 00:49:47 +0000409 continue;
410 }
411#endif
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000412 printf("Bus %d: ", bus);
wdenkc6097192002-11-03 00:24:07 +0000413
414 ide_bus_ok[bus] = 0;
415
416 /* Select device
417 */
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000418 udelay(100000); /* 100 ms */
419 ide_outb(dev, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(dev));
420 udelay(100000); /* 100 ms */
wdenkc6097192002-11-03 00:24:07 +0000421 i = 0;
422 do {
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000423 udelay(10000); /* 10 ms */
wdenkc6097192002-11-03 00:24:07 +0000424
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000425 c = ide_inb(dev, ATA_STATUS);
wdenkc6097192002-11-03 00:24:07 +0000426 i++;
427 if (i > (ATA_RESET_TIME * 100)) {
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000428 puts("** Timeout **\n");
429 /* LED's off */
430 ide_led((LED_IDE1 | LED_IDE2), 0);
wdenkc6097192002-11-03 00:24:07 +0000431 return;
432 }
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000433 if ((i >= 100) && ((i % 100) == 0))
434 putc('.');
435
wdenkc6097192002-11-03 00:24:07 +0000436 } while (c & ATA_STAT_BUSY);
437
438 if (c & (ATA_STAT_BUSY | ATA_STAT_FAULT)) {
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000439 puts("not available ");
440 debug("Status = 0x%02X ", c);
441#ifndef CONFIG_ATAPI /* ATAPI Devices do not set DRDY */
442 } else if ((c & ATA_STAT_READY) == 0) {
443 puts("not available ");
444 debug("Status = 0x%02X ", c);
wdenkc6097192002-11-03 00:24:07 +0000445#endif
446 } else {
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000447 puts("OK ");
wdenkc6097192002-11-03 00:24:07 +0000448 ide_bus_ok[bus] = 1;
449 }
450 WATCHDOG_RESET();
451 }
wdenkc7de8292002-11-19 11:04:11 +0000452
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000453 putc('\n');
wdenkc6097192002-11-03 00:24:07 +0000454
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000455 ide_led((LED_IDE1 | LED_IDE2), 0); /* LED's off */
wdenkc6097192002-11-03 00:24:07 +0000456
457 curr_device = -1;
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000458 for (i = 0; i < CONFIG_SYS_IDE_MAXDEVICE; ++i) {
wdenkc6097192002-11-03 00:24:07 +0000459 int led = (IDE_BUS(i) == 0) ? LED_IDE1 : LED_IDE2;
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000460 ide_dev_desc[i].type = DEV_TYPE_UNKNOWN;
461 ide_dev_desc[i].if_type = IF_TYPE_IDE;
462 ide_dev_desc[i].dev = i;
463 ide_dev_desc[i].part_type = PART_TYPE_UNKNOWN;
464 ide_dev_desc[i].blksz = 0;
465 ide_dev_desc[i].lba = 0;
466 ide_dev_desc[i].block_read = ide_read;
Macpaul Lin0abddf82011-04-11 20:45:32 +0000467 ide_dev_desc[i].block_write = ide_write;
wdenkc6097192002-11-03 00:24:07 +0000468 if (!ide_bus_ok[IDE_BUS(i)])
469 continue;
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000470 ide_led(led, 1); /* LED on */
wdenkc6097192002-11-03 00:24:07 +0000471 ide_ident(&ide_dev_desc[i]);
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000472 ide_led(led, 0); /* LED off */
wdenkc6097192002-11-03 00:24:07 +0000473 dev_print(&ide_dev_desc[i]);
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000474
wdenkc6097192002-11-03 00:24:07 +0000475 if ((ide_dev_desc[i].lba > 0) && (ide_dev_desc[i].blksz > 0)) {
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000476 /* initialize partition type */
477 init_part(&ide_dev_desc[i]);
wdenkc6097192002-11-03 00:24:07 +0000478 if (curr_device < 0)
479 curr_device = i;
480 }
481 }
482 WATCHDOG_RESET();
483}
484
485/* ------------------------------------------------------------------------- */
486
Matthew McClintockdf3fc522011-05-24 05:31:19 +0000487#ifdef CONFIG_PARTITIONS
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000488block_dev_desc_t *ide_get_dev(int dev)
wdenkc6097192002-11-03 00:24:07 +0000489{
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200490 return (dev < CONFIG_SYS_IDE_MAXDEVICE) ? &ide_dev_desc[dev] : NULL;
wdenkc6097192002-11-03 00:24:07 +0000491}
Matthew McClintockdf3fc522011-05-24 05:31:19 +0000492#endif
wdenkc6097192002-11-03 00:24:07 +0000493
wdenkc6097192002-11-03 00:24:07 +0000494/* ------------------------------------------------------------------------- */
495
Pavel Herrmannf5b82c02012-10-09 07:04:39 +0000496void ide_input_swap_data(int dev, ulong *sect_buf, int words)
497 __attribute__ ((weak, alias("__ide_input_swap_data")));
498
499void ide_input_data(int dev, ulong *sect_buf, int words)
500 __attribute__ ((weak, alias("__ide_input_data")));
501
502void ide_output_data(int dev, const ulong *sect_buf, int words)
503 __attribute__ ((weak, alias("__ide_output_data")));
504
wdenk5da627a2003-10-09 20:09:04 +0000505/* We only need to swap data if we are running on a big endian cpu. */
Pavel Herrmann4d1361d2012-10-09 07:10:08 +0000506#if defined(__LITTLE_ENDIAN)
Pavel Herrmannf5b82c02012-10-09 07:04:39 +0000507void __ide_input_swap_data(int dev, ulong *sect_buf, int words)
508{
509 ide_input_data(dev, sect_buf, words);
510}
wdenk5da627a2003-10-09 20:09:04 +0000511#else
Pavel Herrmannf5b82c02012-10-09 07:04:39 +0000512void __ide_input_swap_data(int dev, ulong *sect_buf, int words)
wdenkc6097192002-11-03 00:24:07 +0000513{
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000514 volatile ushort *pbuf =
515 (ushort *) (ATA_CURR_BASE(dev) + ATA_DATA_REG);
516 ushort *dbuf = (ushort *) sect_buf;
wdenk1a344f22005-02-03 23:00:49 +0000517
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000518 debug("in input swap data base for read is %lx\n",
519 (unsigned long) pbuf);
wdenk1a344f22005-02-03 23:00:49 +0000520
521 while (words--) {
Wolfgang Denk0c32d962006-06-16 17:32:31 +0200522#ifdef __MIPS__
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000523 *dbuf++ = swab16p((u16 *) pbuf);
524 *dbuf++ = swab16p((u16 *) pbuf);
Heiko Schocher566a4942007-06-22 19:11:54 +0200525#elif defined(CONFIG_PCS440EP)
526 *dbuf++ = *pbuf;
527 *dbuf++ = *pbuf;
Wolfgang Denk0c32d962006-06-16 17:32:31 +0200528#else
wdenk1a344f22005-02-03 23:00:49 +0000529 *dbuf++ = ld_le16(pbuf);
530 *dbuf++ = ld_le16(pbuf);
Wolfgang Denk0c32d962006-06-16 17:32:31 +0200531#endif /* !MIPS */
wdenk1a344f22005-02-03 23:00:49 +0000532 }
wdenkc6097192002-11-03 00:24:07 +0000533}
Pavel Herrmann4d1361d2012-10-09 07:10:08 +0000534#endif /* __LITTLE_ENDIAN */
wdenkc6097192002-11-03 00:24:07 +0000535
wdenk2262cfe2002-11-18 00:14:45 +0000536
Albert Aribaudf2a37fc2010-08-08 05:17:05 +0530537#if defined(CONFIG_IDE_SWAP_IO)
Pavel Herrmannf5b82c02012-10-09 07:04:39 +0000538void __ide_output_data(int dev, const ulong *sect_buf, int words)
wdenkc6097192002-11-03 00:24:07 +0000539{
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000540 ushort *dbuf;
541 volatile ushort *pbuf;
wdenk1a344f22005-02-03 23:00:49 +0000542
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000543 pbuf = (ushort *) (ATA_CURR_BASE(dev) + ATA_DATA_REG);
544 dbuf = (ushort *) sect_buf;
wdenk1a344f22005-02-03 23:00:49 +0000545 while (words--) {
Heiko Schocher566a4942007-06-22 19:11:54 +0200546#if defined(CONFIG_PCS440EP)
547 /* not tested, because CF was write protected */
548 EIEIO;
549 *pbuf = ld_le16(dbuf++);
550 EIEIO;
551 *pbuf = ld_le16(dbuf++);
552#else
wdenk1a344f22005-02-03 23:00:49 +0000553 EIEIO;
554 *pbuf = *dbuf++;
555 EIEIO;
556 *pbuf = *dbuf++;
Heiko Schocher566a4942007-06-22 19:11:54 +0200557#endif
wdenk1a344f22005-02-03 23:00:49 +0000558 }
wdenkc6097192002-11-03 00:24:07 +0000559}
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000560#else /* ! CONFIG_IDE_SWAP_IO */
Pavel Herrmannf5b82c02012-10-09 07:04:39 +0000561void __ide_output_data(int dev, const ulong *sect_buf, int words)
wdenk2262cfe2002-11-18 00:14:45 +0000562{
Macpaul Lin0abddf82011-04-11 20:45:32 +0000563#if defined(CONFIG_IDE_AHB)
564 ide_write_data(dev, sect_buf, words);
565#else
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000566 outsw(ATA_CURR_BASE(dev) + ATA_DATA_REG, sect_buf, words << 1);
Macpaul Lin0abddf82011-04-11 20:45:32 +0000567#endif
wdenk2262cfe2002-11-18 00:14:45 +0000568}
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000569#endif /* CONFIG_IDE_SWAP_IO */
wdenkc6097192002-11-03 00:24:07 +0000570
Albert Aribaudf2a37fc2010-08-08 05:17:05 +0530571#if defined(CONFIG_IDE_SWAP_IO)
Pavel Herrmannf5b82c02012-10-09 07:04:39 +0000572void __ide_input_data(int dev, ulong *sect_buf, int words)
wdenkc6097192002-11-03 00:24:07 +0000573{
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000574 ushort *dbuf;
575 volatile ushort *pbuf;
wdenk1a344f22005-02-03 23:00:49 +0000576
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000577 pbuf = (ushort *) (ATA_CURR_BASE(dev) + ATA_DATA_REG);
578 dbuf = (ushort *) sect_buf;
wdenk1a344f22005-02-03 23:00:49 +0000579
580 debug("in input data base for read is %lx\n", (unsigned long) pbuf);
581
582 while (words--) {
Heiko Schocher566a4942007-06-22 19:11:54 +0200583#if defined(CONFIG_PCS440EP)
584 EIEIO;
585 *dbuf++ = ld_le16(pbuf);
586 EIEIO;
587 *dbuf++ = ld_le16(pbuf);
588#else
wdenk1a344f22005-02-03 23:00:49 +0000589 EIEIO;
590 *dbuf++ = *pbuf;
591 EIEIO;
592 *dbuf++ = *pbuf;
Heiko Schocher566a4942007-06-22 19:11:54 +0200593#endif
wdenk1a344f22005-02-03 23:00:49 +0000594 }
wdenkc6097192002-11-03 00:24:07 +0000595}
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000596#else /* ! CONFIG_IDE_SWAP_IO */
Pavel Herrmannf5b82c02012-10-09 07:04:39 +0000597void __ide_input_data(int dev, ulong *sect_buf, int words)
wdenk2262cfe2002-11-18 00:14:45 +0000598{
Macpaul Lin0abddf82011-04-11 20:45:32 +0000599#if defined(CONFIG_IDE_AHB)
600 ide_read_data(dev, sect_buf, words);
601#else
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000602 insw(ATA_CURR_BASE(dev) + ATA_DATA_REG, sect_buf, words << 1);
Macpaul Lin0abddf82011-04-11 20:45:32 +0000603#endif
wdenk2262cfe2002-11-18 00:14:45 +0000604}
605
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000606#endif /* CONFIG_IDE_SWAP_IO */
wdenkc6097192002-11-03 00:24:07 +0000607
608/* -------------------------------------------------------------------------
609 */
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000610static void ide_ident(block_dev_desc_t *dev_desc)
wdenkc6097192002-11-03 00:24:07 +0000611{
wdenkc6097192002-11-03 00:24:07 +0000612 unsigned char c;
Marek Vasutb18eabf2011-08-20 09:15:13 +0000613 hd_driveid_t iop;
wdenkc6097192002-11-03 00:24:07 +0000614
wdenk64f70be2004-09-28 20:34:50 +0000615#ifdef CONFIG_ATAPI
616 int retries = 0;
wdenkc7de8292002-11-19 11:04:11 +0000617#endif
618
Steven A. Falco36c2d302008-08-15 15:34:10 -0400619#ifdef CONFIG_TUNE_PIO
620 int pio_mode;
621#endif
622
wdenkc6097192002-11-03 00:24:07 +0000623#if 0
624 int mode, cycle_time;
625#endif
626 int device;
wdenkc6097192002-11-03 00:24:07 +0000627
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000628 device = dev_desc->dev;
629 printf(" Device %d: ", device);
630
631 ide_led(DEVICE_LED(device), 1); /* LED on */
wdenkc6097192002-11-03 00:24:07 +0000632 /* Select device
633 */
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000634 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
635 dev_desc->if_type = IF_TYPE_IDE;
wdenkc6097192002-11-03 00:24:07 +0000636#ifdef CONFIG_ATAPI
wdenkc7de8292002-11-19 11:04:11 +0000637
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000638 retries = 0;
wdenkc7de8292002-11-19 11:04:11 +0000639
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000640 /* Warning: This will be tricky to read */
641 while (retries <= 1) {
642 /* check signature */
643 if ((ide_inb(device, ATA_SECT_CNT) == 0x01) &&
644 (ide_inb(device, ATA_SECT_NUM) == 0x01) &&
645 (ide_inb(device, ATA_CYL_LOW) == 0x14) &&
646 (ide_inb(device, ATA_CYL_HIGH) == 0xEB)) {
647 /* ATAPI Signature found */
648 dev_desc->if_type = IF_TYPE_ATAPI;
649 /*
650 * Start Ident Command
651 */
652 ide_outb(device, ATA_COMMAND, ATAPI_CMD_IDENT);
653 /*
654 * Wait for completion - ATAPI devices need more time
655 * to become ready
656 */
657 c = ide_wait(device, ATAPI_TIME_OUT);
658 } else
wdenkc6097192002-11-03 00:24:07 +0000659#endif
wdenk1a344f22005-02-03 23:00:49 +0000660 {
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000661 /*
662 * Start Ident Command
663 */
664 ide_outb(device, ATA_COMMAND, ATA_CMD_IDENT);
665
666 /*
667 * Wait for completion
668 */
669 c = ide_wait(device, IDE_TIME_OUT);
wdenk1a344f22005-02-03 23:00:49 +0000670 }
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000671 ide_led(DEVICE_LED(device), 0); /* LED off */
672
673 if (((c & ATA_STAT_DRQ) == 0) ||
674 ((c & (ATA_STAT_FAULT | ATA_STAT_ERR)) != 0)) {
wdenk64f70be2004-09-28 20:34:50 +0000675#ifdef CONFIG_ATAPI
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000676 {
677 /*
678 * Need to soft reset the device
679 * in case it's an ATAPI...
680 */
681 debug("Retrying...\n");
682 ide_outb(device, ATA_DEV_HD,
683 ATA_LBA | ATA_DEVICE(device));
684 udelay(100000);
685 ide_outb(device, ATA_COMMAND, 0x08);
686 udelay(500000); /* 500 ms */
687 }
688 /*
689 * Select device
690 */
691 ide_outb(device, ATA_DEV_HD,
692 ATA_LBA | ATA_DEVICE(device));
693 retries++;
694#else
695 return;
696#endif
697 }
698#ifdef CONFIG_ATAPI
699 else
700 break;
701 } /* see above - ugly to read */
wdenk64f70be2004-09-28 20:34:50 +0000702
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000703 if (retries == 2) /* Not found */
wdenk64f70be2004-09-28 20:34:50 +0000704 return;
705#endif
wdenkc7de8292002-11-19 11:04:11 +0000706
Pavel Herrmannf5b82c02012-10-09 07:04:39 +0000707 ide_input_swap_data(device, (ulong *)&iop, ATA_SECTORWORDS);
wdenkc6097192002-11-03 00:24:07 +0000708
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000709 ident_cpy((unsigned char *) dev_desc->revision, iop.fw_rev,
710 sizeof(dev_desc->revision));
711 ident_cpy((unsigned char *) dev_desc->vendor, iop.model,
712 sizeof(dev_desc->vendor));
713 ident_cpy((unsigned char *) dev_desc->product, iop.serial_no,
714 sizeof(dev_desc->product));
wdenkc3f9d492004-03-14 00:59:59 +0000715#ifdef __LITTLE_ENDIAN
716 /*
Richard Retanubunbcdf1d22008-11-06 14:01:51 -0500717 * firmware revision, model, and serial number have Big Endian Byte
718 * order in Word. Convert all three to little endian.
wdenkc3f9d492004-03-14 00:59:59 +0000719 *
720 * See CF+ and CompactFlash Specification Revision 2.0:
Richard Retanubunbcdf1d22008-11-06 14:01:51 -0500721 * 6.2.1.6: Identify Drive, Table 39 for more details
wdenkc3f9d492004-03-14 00:59:59 +0000722 */
723
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000724 strswab(dev_desc->revision);
725 strswab(dev_desc->vendor);
726 strswab(dev_desc->product);
wdenkc3f9d492004-03-14 00:59:59 +0000727#endif /* __LITTLE_ENDIAN */
wdenkc6097192002-11-03 00:24:07 +0000728
Marek Vasutb18eabf2011-08-20 09:15:13 +0000729 if ((iop.config & 0x0080) == 0x0080)
wdenkc6097192002-11-03 00:24:07 +0000730 dev_desc->removable = 1;
731 else
732 dev_desc->removable = 0;
733
Steven A. Falco36c2d302008-08-15 15:34:10 -0400734#ifdef CONFIG_TUNE_PIO
735 /* Mode 0 - 2 only, are directly determined by word 51. */
Marek Vasutb18eabf2011-08-20 09:15:13 +0000736 pio_mode = iop.tPIO;
Steven A. Falco36c2d302008-08-15 15:34:10 -0400737 if (pio_mode > 2) {
738 printf("WARNING: Invalid PIO (word 51 = %d).\n", pio_mode);
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000739 /* Force it to dead slow, and hope for the best... */
740 pio_mode = 0;
Steven A. Falco36c2d302008-08-15 15:34:10 -0400741 }
742
743 /* Any CompactFlash Storage Card that supports PIO mode 3 or above
744 * shall set bit 1 of word 53 to one and support the fields contained
745 * in words 64 through 70.
746 */
Marek Vasutb18eabf2011-08-20 09:15:13 +0000747 if (iop.field_valid & 0x02) {
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000748 /*
749 * Mode 3 and above are possible. Check in order from slow
Steven A. Falco36c2d302008-08-15 15:34:10 -0400750 * to fast, so we wind up with the highest mode allowed.
751 */
Marek Vasutb18eabf2011-08-20 09:15:13 +0000752 if (iop.eide_pio_modes & 0x01)
Steven A. Falco36c2d302008-08-15 15:34:10 -0400753 pio_mode = 3;
Marek Vasutb18eabf2011-08-20 09:15:13 +0000754 if (iop.eide_pio_modes & 0x02)
Steven A. Falco36c2d302008-08-15 15:34:10 -0400755 pio_mode = 4;
Marek Vasutb18eabf2011-08-20 09:15:13 +0000756 if (ata_id_is_cfa((u16 *)&iop)) {
757 if ((iop.cf_advanced_caps & 0x07) == 0x01)
Steven A. Falco36c2d302008-08-15 15:34:10 -0400758 pio_mode = 5;
Marek Vasutb18eabf2011-08-20 09:15:13 +0000759 if ((iop.cf_advanced_caps & 0x07) == 0x02)
Steven A. Falco36c2d302008-08-15 15:34:10 -0400760 pio_mode = 6;
761 }
762 }
763
764 /* System-specific, depends on bus speeds, etc. */
765 ide_set_piomode(pio_mode);
766#endif /* CONFIG_TUNE_PIO */
767
wdenkc6097192002-11-03 00:24:07 +0000768#if 0
769 /*
770 * Drive PIO mode autoselection
771 */
Marek Vasutb18eabf2011-08-20 09:15:13 +0000772 mode = iop.tPIO;
wdenkc6097192002-11-03 00:24:07 +0000773
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000774 printf("tPIO = 0x%02x = %d\n", mode, mode);
wdenkc6097192002-11-03 00:24:07 +0000775 if (mode > 2) { /* 2 is maximum allowed tPIO value */
776 mode = 2;
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000777 debug("Override tPIO -> 2\n");
wdenkc6097192002-11-03 00:24:07 +0000778 }
Marek Vasutb18eabf2011-08-20 09:15:13 +0000779 if (iop.field_valid & 2) { /* drive implements ATA2? */
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000780 debug("Drive implements ATA2\n");
Marek Vasutb18eabf2011-08-20 09:15:13 +0000781 if (iop.capability & 8) { /* drive supports use_iordy? */
782 cycle_time = iop.eide_pio_iordy;
wdenkc6097192002-11-03 00:24:07 +0000783 } else {
Marek Vasutb18eabf2011-08-20 09:15:13 +0000784 cycle_time = iop.eide_pio;
wdenkc6097192002-11-03 00:24:07 +0000785 }
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000786 debug("cycle time = %d\n", cycle_time);
wdenkc6097192002-11-03 00:24:07 +0000787 mode = 4;
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000788 if (cycle_time > 120)
789 mode = 3; /* 120 ns for PIO mode 4 */
790 if (cycle_time > 180)
791 mode = 2; /* 180 ns for PIO mode 3 */
792 if (cycle_time > 240)
793 mode = 1; /* 240 ns for PIO mode 4 */
794 if (cycle_time > 383)
795 mode = 0; /* 383 ns for PIO mode 4 */
wdenkc6097192002-11-03 00:24:07 +0000796 }
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000797 printf("PIO mode to use: PIO %d\n", mode);
wdenkc6097192002-11-03 00:24:07 +0000798#endif /* 0 */
799
800#ifdef CONFIG_ATAPI
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000801 if (dev_desc->if_type == IF_TYPE_ATAPI) {
wdenkc6097192002-11-03 00:24:07 +0000802 atapi_inquiry(dev_desc);
803 return;
804 }
805#endif /* CONFIG_ATAPI */
806
wdenkc3f9d492004-03-14 00:59:59 +0000807#ifdef __BIG_ENDIAN
wdenkc6097192002-11-03 00:24:07 +0000808 /* swap shorts */
Marek Vasutb18eabf2011-08-20 09:15:13 +0000809 dev_desc->lba = (iop.lba_capacity << 16) | (iop.lba_capacity >> 16);
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000810#else /* ! __BIG_ENDIAN */
wdenkc3f9d492004-03-14 00:59:59 +0000811 /*
812 * do not swap shorts on little endian
813 *
814 * See CF+ and CompactFlash Specification Revision 2.0:
815 * 6.2.1.6: Identfy Drive, Table 39, Word Address 57-58 for details.
816 */
Marek Vasutb18eabf2011-08-20 09:15:13 +0000817 dev_desc->lba = iop.lba_capacity;
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000818#endif /* __BIG_ENDIAN */
wdenkc40b2952004-03-13 23:29:43 +0000819
wdenk42dfe7a2004-03-14 22:25:36 +0000820#ifdef CONFIG_LBA48
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000821 if (iop.command_set_2 & 0x0400) { /* LBA 48 support */
wdenk6e592382004-04-18 17:39:38 +0000822 dev_desc->lba48 = 1;
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000823 dev_desc->lba = (unsigned long long) iop.lba48_capacity[0] |
824 ((unsigned long long) iop.lba48_capacity[1] << 16) |
825 ((unsigned long long) iop.lba48_capacity[2] << 32) |
826 ((unsigned long long) iop.lba48_capacity[3] << 48);
wdenkc40b2952004-03-13 23:29:43 +0000827 } else {
wdenkc40b2952004-03-13 23:29:43 +0000828 dev_desc->lba48 = 0;
829 }
830#endif /* CONFIG_LBA48 */
wdenkc6097192002-11-03 00:24:07 +0000831 /* assuming HD */
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000832 dev_desc->type = DEV_TYPE_HARDDISK;
833 dev_desc->blksz = ATA_BLOCKSIZE;
834 dev_desc->lun = 0; /* just to fill something in... */
wdenkc6097192002-11-03 00:24:07 +0000835
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000836#if 0 /* only used to test the powersaving mode,
837 * if enabled, the drive goes after 5 sec
838 * in standby mode */
839 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
840 c = ide_wait(device, IDE_TIME_OUT);
841 ide_outb(device, ATA_SECT_CNT, 1);
842 ide_outb(device, ATA_LBA_LOW, 0);
843 ide_outb(device, ATA_LBA_MID, 0);
844 ide_outb(device, ATA_LBA_HIGH, 0);
845 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
846 ide_outb(device, ATA_COMMAND, 0xe3);
847 udelay(50);
848 c = ide_wait(device, IDE_TIME_OUT); /* can't take over 500 ms */
wdenkc6097192002-11-03 00:24:07 +0000849#endif
850}
851
852
853/* ------------------------------------------------------------------------- */
854
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000855ulong ide_read(int device, lbaint_t blknr, ulong blkcnt, void *buffer)
wdenkc6097192002-11-03 00:24:07 +0000856{
857 ulong n = 0;
858 unsigned char c;
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000859 unsigned char pwrsave = 0; /* power save */
860
wdenk42dfe7a2004-03-14 22:25:36 +0000861#ifdef CONFIG_LBA48
wdenkc40b2952004-03-13 23:29:43 +0000862 unsigned char lba48 = 0;
wdenkc6097192002-11-03 00:24:07 +0000863
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
Marek Vasut5bbe10d2011-10-25 11:39:15 +0200869 debug("ide_read dev %d start %lX, blocks %lX buffer at %lX\n",
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000870 device, blknr, blkcnt, (ulong) buffer);
wdenkc6097192002-11-03 00:24:07 +0000871
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000872 ide_led(DEVICE_LED(device), 1); /* LED on */
wdenkc6097192002-11-03 00:24:07 +0000873
874 /* Select device
875 */
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000876 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
877 c = ide_wait(device, IDE_TIME_OUT);
wdenkc6097192002-11-03 00:24:07 +0000878
879 if (c & ATA_STAT_BUSY) {
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000880 printf("IDE read: device %d not ready\n", device);
wdenkc6097192002-11-03 00:24:07 +0000881 goto IDE_READ_E;
882 }
883
884 /* first check if the drive is in Powersaving mode, if yes,
885 * increase the timeout value */
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000886 ide_outb(device, ATA_COMMAND, ATA_CMD_CHK_PWR);
887 udelay(50);
wdenkc6097192002-11-03 00:24:07 +0000888
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000889 c = ide_wait(device, IDE_TIME_OUT); /* can't take over 500 ms */
wdenkc6097192002-11-03 00:24:07 +0000890
891 if (c & ATA_STAT_BUSY) {
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000892 printf("IDE read: device %d not ready\n", device);
wdenkc6097192002-11-03 00:24:07 +0000893 goto IDE_READ_E;
894 }
895 if ((c & ATA_STAT_ERR) == ATA_STAT_ERR) {
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000896 printf("No Powersaving mode %X\n", c);
wdenkc6097192002-11-03 00:24:07 +0000897 } else {
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000898 c = ide_inb(device, ATA_SECT_CNT);
899 debug("Powersaving %02X\n", c);
900 if (c == 0)
901 pwrsave = 1;
wdenkc6097192002-11-03 00:24:07 +0000902 }
903
904
905 while (blkcnt-- > 0) {
906
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000907 c = ide_wait(device, IDE_TIME_OUT);
wdenkc6097192002-11-03 00:24:07 +0000908
909 if (c & ATA_STAT_BUSY) {
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000910 printf("IDE read: device %d not ready\n", device);
wdenkc6097192002-11-03 00:24:07 +0000911 break;
912 }
wdenk42dfe7a2004-03-14 22:25:36 +0000913#ifdef CONFIG_LBA48
wdenkc40b2952004-03-13 23:29:43 +0000914 if (lba48) {
915 /* write high bits */
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000916 ide_outb(device, ATA_SECT_CNT, 0);
917 ide_outb(device, ATA_LBA_LOW, (blknr >> 24) & 0xFF);
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200918#ifdef CONFIG_SYS_64BIT_LBA
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000919 ide_outb(device, ATA_LBA_MID, (blknr >> 32) & 0xFF);
920 ide_outb(device, ATA_LBA_HIGH, (blknr >> 40) & 0xFF);
Guennadi Liakhovetski413bf582008-04-28 14:36:06 +0200921#else
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000922 ide_outb(device, ATA_LBA_MID, 0);
923 ide_outb(device, ATA_LBA_HIGH, 0);
Guennadi Liakhovetski413bf582008-04-28 14:36:06 +0200924#endif
wdenkc40b2952004-03-13 23:29:43 +0000925 }
926#endif
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000927 ide_outb(device, ATA_SECT_CNT, 1);
928 ide_outb(device, ATA_LBA_LOW, (blknr >> 0) & 0xFF);
929 ide_outb(device, ATA_LBA_MID, (blknr >> 8) & 0xFF);
930 ide_outb(device, ATA_LBA_HIGH, (blknr >> 16) & 0xFF);
wdenkc40b2952004-03-13 23:29:43 +0000931
wdenk42dfe7a2004-03-14 22:25:36 +0000932#ifdef CONFIG_LBA48
wdenkc40b2952004-03-13 23:29:43 +0000933 if (lba48) {
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000934 ide_outb(device, ATA_DEV_HD,
935 ATA_LBA | ATA_DEVICE(device));
936 ide_outb(device, ATA_COMMAND, ATA_CMD_READ_EXT);
wdenkc40b2952004-03-13 23:29:43 +0000937
938 } else
939#endif
940 {
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000941 ide_outb(device, ATA_DEV_HD, ATA_LBA |
942 ATA_DEVICE(device) | ((blknr >> 24) & 0xF));
943 ide_outb(device, ATA_COMMAND, ATA_CMD_READ);
wdenkc40b2952004-03-13 23:29:43 +0000944 }
wdenkc6097192002-11-03 00:24:07 +0000945
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000946 udelay(50);
wdenkc6097192002-11-03 00:24:07 +0000947
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000948 if (pwrsave) {
949 /* may take up to 4 sec */
950 c = ide_wait(device, IDE_SPIN_UP_TIME_OUT);
951 pwrsave = 0;
wdenkc6097192002-11-03 00:24:07 +0000952 } else {
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000953 /* can't take over 500 ms */
954 c = ide_wait(device, IDE_TIME_OUT);
wdenkc6097192002-11-03 00:24:07 +0000955 }
956
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000957 if ((c & (ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR)) !=
958 ATA_STAT_DRQ) {
Heiko Schocher4b142fe2009-12-03 11:21:21 +0100959#if defined(CONFIG_SYS_64BIT_LBA)
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000960 printf("Error (no IRQ) dev %d blk %lld: status 0x%02x\n",
wdenkc6097192002-11-03 00:24:07 +0000961 device, blknr, c);
wdenkc40b2952004-03-13 23:29:43 +0000962#else
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000963 printf("Error (no IRQ) dev %d blk %ld: status 0x%02x\n",
964 device, (ulong) blknr, c);
wdenkc40b2952004-03-13 23:29:43 +0000965#endif
wdenkc6097192002-11-03 00:24:07 +0000966 break;
967 }
968
Pavel Herrmannf5b82c02012-10-09 07:04:39 +0000969 ide_input_data(device, buffer, ATA_SECTORWORDS);
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000970 (void) ide_inb(device, ATA_STATUS); /* clear IRQ */
wdenkc6097192002-11-03 00:24:07 +0000971
972 ++n;
973 ++blknr;
Greg Lopp0b945042007-04-13 08:02:24 +0200974 buffer += ATA_BLOCKSIZE;
wdenkc6097192002-11-03 00:24:07 +0000975 }
976IDE_READ_E:
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000977 ide_led(DEVICE_LED(device), 0); /* LED off */
wdenkc6097192002-11-03 00:24:07 +0000978 return (n);
979}
980
981/* ------------------------------------------------------------------------- */
982
983
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000984ulong ide_write(int device, lbaint_t blknr, ulong blkcnt, const void *buffer)
wdenkc6097192002-11-03 00:24:07 +0000985{
986 ulong n = 0;
987 unsigned char c;
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000988
wdenk42dfe7a2004-03-14 22:25:36 +0000989#ifdef CONFIG_LBA48
wdenkc40b2952004-03-13 23:29:43 +0000990 unsigned char lba48 = 0;
991
Guennadi Liakhovetski413bf582008-04-28 14:36:06 +0200992 if (blknr & 0x0000fffff0000000ULL) {
wdenkc40b2952004-03-13 23:29:43 +0000993 /* more than 28 bits used, use 48bit mode */
994 lba48 = 1;
995 }
996#endif
wdenkc6097192002-11-03 00:24:07 +0000997
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000998 ide_led(DEVICE_LED(device), 1); /* LED on */
wdenkc6097192002-11-03 00:24:07 +0000999
1000 /* Select device
1001 */
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001002 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
wdenkc6097192002-11-03 00:24:07 +00001003
1004 while (blkcnt-- > 0) {
1005
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001006 c = ide_wait(device, IDE_TIME_OUT);
wdenkc6097192002-11-03 00:24:07 +00001007
1008 if (c & ATA_STAT_BUSY) {
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001009 printf("IDE read: device %d not ready\n", device);
wdenkc6097192002-11-03 00:24:07 +00001010 goto WR_OUT;
1011 }
wdenk42dfe7a2004-03-14 22:25:36 +00001012#ifdef CONFIG_LBA48
wdenkc40b2952004-03-13 23:29:43 +00001013 if (lba48) {
1014 /* write high bits */
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001015 ide_outb(device, ATA_SECT_CNT, 0);
1016 ide_outb(device, ATA_LBA_LOW, (blknr >> 24) & 0xFF);
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001017#ifdef CONFIG_SYS_64BIT_LBA
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001018 ide_outb(device, ATA_LBA_MID, (blknr >> 32) & 0xFF);
1019 ide_outb(device, ATA_LBA_HIGH, (blknr >> 40) & 0xFF);
Guennadi Liakhovetski413bf582008-04-28 14:36:06 +02001020#else
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001021 ide_outb(device, ATA_LBA_MID, 0);
1022 ide_outb(device, ATA_LBA_HIGH, 0);
Guennadi Liakhovetski413bf582008-04-28 14:36:06 +02001023#endif
wdenkc40b2952004-03-13 23:29:43 +00001024 }
1025#endif
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001026 ide_outb(device, ATA_SECT_CNT, 1);
1027 ide_outb(device, ATA_LBA_LOW, (blknr >> 0) & 0xFF);
1028 ide_outb(device, ATA_LBA_MID, (blknr >> 8) & 0xFF);
1029 ide_outb(device, ATA_LBA_HIGH, (blknr >> 16) & 0xFF);
wdenkc40b2952004-03-13 23:29:43 +00001030
wdenk42dfe7a2004-03-14 22:25:36 +00001031#ifdef CONFIG_LBA48
wdenkc40b2952004-03-13 23:29:43 +00001032 if (lba48) {
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001033 ide_outb(device, ATA_DEV_HD,
1034 ATA_LBA | ATA_DEVICE(device));
1035 ide_outb(device, ATA_COMMAND, ATA_CMD_WRITE_EXT);
wdenkc40b2952004-03-13 23:29:43 +00001036
1037 } else
1038#endif
1039 {
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001040 ide_outb(device, ATA_DEV_HD, ATA_LBA |
1041 ATA_DEVICE(device) | ((blknr >> 24) & 0xF));
1042 ide_outb(device, ATA_COMMAND, ATA_CMD_WRITE);
wdenkc40b2952004-03-13 23:29:43 +00001043 }
wdenkc6097192002-11-03 00:24:07 +00001044
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001045 udelay(50);
wdenkc6097192002-11-03 00:24:07 +00001046
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001047 /* can't take over 500 ms */
1048 c = ide_wait(device, IDE_TIME_OUT);
wdenkc6097192002-11-03 00:24:07 +00001049
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001050 if ((c & (ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR)) !=
1051 ATA_STAT_DRQ) {
Heiko Schocher4b142fe2009-12-03 11:21:21 +01001052#if defined(CONFIG_SYS_64BIT_LBA)
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001053 printf("Error (no IRQ) dev %d blk %lld: status 0x%02x\n",
wdenkc6097192002-11-03 00:24:07 +00001054 device, blknr, c);
wdenkc40b2952004-03-13 23:29:43 +00001055#else
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001056 printf("Error (no IRQ) dev %d blk %ld: status 0x%02x\n",
1057 device, (ulong) blknr, c);
wdenkc40b2952004-03-13 23:29:43 +00001058#endif
wdenkc6097192002-11-03 00:24:07 +00001059 goto WR_OUT;
1060 }
1061
Pavel Herrmannf5b82c02012-10-09 07:04:39 +00001062 ide_output_data(device, buffer, ATA_SECTORWORDS);
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001063 c = ide_inb(device, ATA_STATUS); /* clear IRQ */
wdenkc6097192002-11-03 00:24:07 +00001064 ++n;
1065 ++blknr;
Greg Lopp0b945042007-04-13 08:02:24 +02001066 buffer += ATA_BLOCKSIZE;
wdenkc6097192002-11-03 00:24:07 +00001067 }
1068WR_OUT:
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001069 ide_led(DEVICE_LED(device), 0); /* LED off */
wdenkc6097192002-11-03 00:24:07 +00001070 return (n);
1071}
1072
1073/* ------------------------------------------------------------------------- */
1074
1075/*
1076 * copy src to dest, skipping leading and trailing blanks and null
1077 * terminate the string
wdenk7d7ce412004-03-17 01:13:07 +00001078 * "len" is the size of available memory including the terminating '\0'
wdenkc6097192002-11-03 00:24:07 +00001079 */
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001080static void ident_cpy(unsigned char *dst, unsigned char *src,
1081 unsigned int len)
wdenkc6097192002-11-03 00:24:07 +00001082{
wdenk7d7ce412004-03-17 01:13:07 +00001083 unsigned char *end, *last;
wdenkc6097192002-11-03 00:24:07 +00001084
wdenk7d7ce412004-03-17 01:13:07 +00001085 last = dst;
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001086 end = src + len - 1;
wdenk7d7ce412004-03-17 01:13:07 +00001087
1088 /* reserve space for '\0' */
1089 if (len < 2)
1090 goto OUT;
wdenkefa329c2004-03-23 20:18:25 +00001091
wdenk7d7ce412004-03-17 01:13:07 +00001092 /* skip leading white space */
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001093 while ((*src) && (src < end) && (*src == ' '))
wdenk7d7ce412004-03-17 01:13:07 +00001094 ++src;
1095
1096 /* copy string, omitting trailing white space */
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001097 while ((*src) && (src < end)) {
wdenk7d7ce412004-03-17 01:13:07 +00001098 *dst++ = *src;
1099 if (*src++ != ' ')
1100 last = dst;
wdenkc6097192002-11-03 00:24:07 +00001101 }
wdenk7d7ce412004-03-17 01:13:07 +00001102OUT:
1103 *last = '\0';
wdenkc6097192002-11-03 00:24:07 +00001104}
1105
1106/* ------------------------------------------------------------------------- */
1107
1108/*
1109 * Wait until Busy bit is off, or timeout (in ms)
1110 * Return last status
1111 */
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001112static uchar ide_wait(int dev, ulong t)
wdenkc6097192002-11-03 00:24:07 +00001113{
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001114 ulong delay = 10 * t; /* poll every 100 us */
wdenkc6097192002-11-03 00:24:07 +00001115 uchar c;
1116
wdenk2262cfe2002-11-18 00:14:45 +00001117 while ((c = ide_inb(dev, ATA_STATUS)) & ATA_STAT_BUSY) {
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001118 udelay(100);
1119 if (delay-- == 0)
wdenkc6097192002-11-03 00:24:07 +00001120 break;
wdenkc6097192002-11-03 00:24:07 +00001121 }
1122 return (c);
1123}
1124
1125/* ------------------------------------------------------------------------- */
1126
1127#ifdef CONFIG_IDE_RESET
1128extern void ide_set_reset(int idereset);
1129
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001130static void ide_reset(void)
wdenkc6097192002-11-03 00:24:07 +00001131{
wdenkc6097192002-11-03 00:24:07 +00001132 int i;
1133
1134 curr_device = -1;
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001135 for (i = 0; i < CONFIG_SYS_IDE_MAXBUS; ++i)
wdenkc6097192002-11-03 00:24:07 +00001136 ide_bus_ok[i] = 0;
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001137 for (i = 0; i < CONFIG_SYS_IDE_MAXDEVICE; ++i)
wdenkc6097192002-11-03 00:24:07 +00001138 ide_dev_desc[i].type = DEV_TYPE_UNKNOWN;
1139
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001140 ide_set_reset(1); /* assert reset */
wdenkc6097192002-11-03 00:24:07 +00001141
Martin Krausee175eac2008-04-03 13:37:56 +02001142 /* the reset signal shall be asserted for et least 25 us */
1143 udelay(25);
1144
wdenkc6097192002-11-03 00:24:07 +00001145 WATCHDOG_RESET();
1146
wdenkc6097192002-11-03 00:24:07 +00001147 /* de-assert RESET signal */
1148 ide_set_reset(0);
1149
1150 /* wait 250 ms */
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001151 for (i = 0; i < 250; ++i)
1152 udelay(1000);
wdenkc6097192002-11-03 00:24:07 +00001153}
1154
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001155#endif /* CONFIG_IDE_RESET */
wdenkc6097192002-11-03 00:24:07 +00001156
1157/* ------------------------------------------------------------------------- */
1158
Heiko Schocher3887c3f2009-09-23 07:56:08 +02001159#if defined(CONFIG_OF_IDE_FIXUP)
1160int ide_device_present(int dev)
1161{
1162 if (dev >= CONFIG_SYS_IDE_MAXBUS)
1163 return 0;
1164 return (ide_dev_desc[dev].type == DEV_TYPE_UNKNOWN ? 0 : 1);
1165}
1166#endif
wdenkc6097192002-11-03 00:24:07 +00001167/* ------------------------------------------------------------------------- */
1168
1169#ifdef CONFIG_ATAPI
1170/****************************************************************************
1171 * ATAPI Support
1172 */
1173
Pavel Herrmannf5b82c02012-10-09 07:04:39 +00001174void ide_input_data_shorts(int dev, ushort *sect_buf, int shorts)
1175 __attribute__ ((weak, alias("__ide_input_data_shorts")));
1176
1177void ide_output_data_shorts(int dev, ushort *sect_buf, int shorts)
1178 __attribute__ ((weak, alias("__ide_output_data_shorts")));
1179
1180
Albert Aribaudf2a37fc2010-08-08 05:17:05 +05301181#if defined(CONFIG_IDE_SWAP_IO)
wdenkc6097192002-11-03 00:24:07 +00001182/* since ATAPI may use commands with not 4 bytes alligned length
1183 * we have our own transfer functions, 2 bytes alligned */
Pavel Herrmannf5b82c02012-10-09 07:04:39 +00001184void __ide_output_data_shorts(int dev, ushort *sect_buf, int shorts)
wdenkc6097192002-11-03 00:24:07 +00001185{
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001186 ushort *dbuf;
1187 volatile ushort *pbuf;
wdenkc6097192002-11-03 00:24:07 +00001188
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001189 pbuf = (ushort *) (ATA_CURR_BASE(dev) + ATA_DATA_REG);
1190 dbuf = (ushort *) sect_buf;
wdenkdb01a2e2004-04-15 23:14:49 +00001191
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001192 debug("in output data shorts base for read is %lx\n",
1193 (unsigned long) pbuf);
wdenkdb01a2e2004-04-15 23:14:49 +00001194
wdenkc6097192002-11-03 00:24:07 +00001195 while (shorts--) {
wdenk5cf91d62004-04-23 20:32:05 +00001196 EIEIO;
wdenk1a344f22005-02-03 23:00:49 +00001197 *pbuf = *dbuf++;
wdenkc6097192002-11-03 00:24:07 +00001198 }
wdenk1a344f22005-02-03 23:00:49 +00001199}
1200
Pavel Herrmannf5b82c02012-10-09 07:04:39 +00001201void __ide_input_data_shorts(int dev, ushort *sect_buf, int shorts)
wdenk1a344f22005-02-03 23:00:49 +00001202{
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001203 ushort *dbuf;
1204 volatile ushort *pbuf;
wdenk1a344f22005-02-03 23:00:49 +00001205
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001206 pbuf = (ushort *) (ATA_CURR_BASE(dev) + ATA_DATA_REG);
1207 dbuf = (ushort *) sect_buf;
wdenk1a344f22005-02-03 23:00:49 +00001208
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001209 debug("in input data shorts base for read is %lx\n",
1210 (unsigned long) pbuf);
wdenk1a344f22005-02-03 23:00:49 +00001211
1212 while (shorts--) {
1213 EIEIO;
1214 *dbuf++ = *pbuf;
1215 }
wdenkc6097192002-11-03 00:24:07 +00001216}
1217
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001218#else /* ! CONFIG_IDE_SWAP_IO */
Pavel Herrmannf5b82c02012-10-09 07:04:39 +00001219void __ide_output_data_shorts(int dev, ushort *sect_buf, int shorts)
wdenk2262cfe2002-11-18 00:14:45 +00001220{
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001221 outsw(ATA_CURR_BASE(dev) + ATA_DATA_REG, sect_buf, shorts);
wdenk2262cfe2002-11-18 00:14:45 +00001222}
1223
Pavel Herrmannf5b82c02012-10-09 07:04:39 +00001224void __ide_input_data_shorts(int dev, ushort *sect_buf, int shorts)
wdenk2262cfe2002-11-18 00:14:45 +00001225{
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001226 insw(ATA_CURR_BASE(dev) + ATA_DATA_REG, sect_buf, shorts);
wdenk2262cfe2002-11-18 00:14:45 +00001227}
1228
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001229#endif /* CONFIG_IDE_SWAP_IO */
wdenk2262cfe2002-11-18 00:14:45 +00001230
wdenkc6097192002-11-03 00:24:07 +00001231/*
1232 * Wait until (Status & mask) == res, or timeout (in ms)
1233 * Return last status
1234 * This is used since some ATAPI CD ROMs clears their Busy Bit first
1235 * and then they set their DRQ Bit
1236 */
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001237static uchar atapi_wait_mask(int dev, ulong t, uchar mask, uchar res)
wdenkc6097192002-11-03 00:24:07 +00001238{
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001239 ulong delay = 10 * t; /* poll every 100 us */
wdenkc6097192002-11-03 00:24:07 +00001240 uchar c;
1241
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001242 /* prevents to read the status before valid */
1243 c = ide_inb(dev, ATA_DEV_CTL);
1244
wdenk2262cfe2002-11-18 00:14:45 +00001245 while (((c = ide_inb(dev, ATA_STATUS)) & mask) != res) {
wdenkc6097192002-11-03 00:24:07 +00001246 /* break if error occurs (doesn't make sense to wait more) */
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001247 if ((c & ATA_STAT_ERR) == ATA_STAT_ERR)
wdenkc6097192002-11-03 00:24:07 +00001248 break;
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001249 udelay(100);
1250 if (delay-- == 0)
wdenkc6097192002-11-03 00:24:07 +00001251 break;
wdenkc6097192002-11-03 00:24:07 +00001252 }
1253 return (c);
1254}
1255
1256/*
1257 * issue an atapi command
1258 */
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001259unsigned char atapi_issue(int device, unsigned char *ccb, int ccblen,
1260 unsigned char *buffer, int buflen)
wdenkc6097192002-11-03 00:24:07 +00001261{
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001262 unsigned char c, err, mask, res;
wdenkc6097192002-11-03 00:24:07 +00001263 int n;
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001264
1265 ide_led(DEVICE_LED(device), 1); /* LED on */
wdenkc6097192002-11-03 00:24:07 +00001266
1267 /* Select device
1268 */
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001269 mask = ATA_STAT_BUSY | ATA_STAT_DRQ;
wdenkc6097192002-11-03 00:24:07 +00001270 res = 0;
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001271 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
1272 c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
wdenkc6097192002-11-03 00:24:07 +00001273 if ((c & mask) != res) {
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001274 printf("ATAPI_ISSUE: device %d not ready status %X\n", device,
1275 c);
1276 err = 0xFF;
wdenkc6097192002-11-03 00:24:07 +00001277 goto AI_OUT;
1278 }
1279 /* write taskfile */
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001280 ide_outb(device, ATA_ERROR_REG, 0); /* no DMA, no overlaped */
1281 ide_outb(device, ATA_SECT_CNT, 0);
1282 ide_outb(device, ATA_SECT_NUM, 0);
1283 ide_outb(device, ATA_CYL_LOW, (unsigned char) (buflen & 0xFF));
1284 ide_outb(device, ATA_CYL_HIGH,
1285 (unsigned char) ((buflen >> 8) & 0xFF));
1286 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
wdenkc6097192002-11-03 00:24:07 +00001287
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001288 ide_outb(device, ATA_COMMAND, ATAPI_CMD_PACKET);
1289 udelay(50);
wdenkc6097192002-11-03 00:24:07 +00001290
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001291 mask = ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR;
wdenkc6097192002-11-03 00:24:07 +00001292 res = ATA_STAT_DRQ;
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001293 c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
wdenkc6097192002-11-03 00:24:07 +00001294
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001295 if ((c & mask) != res) { /* DRQ must be 1, BSY 0 */
1296 printf("ATAPI_ISSUE: Error (no IRQ) before sending ccb dev %d status 0x%02x\n",
1297 device, c);
1298 err = 0xFF;
wdenkc6097192002-11-03 00:24:07 +00001299 goto AI_OUT;
1300 }
1301
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001302 /* write command block */
Pavel Herrmannf5b82c02012-10-09 07:04:39 +00001303 ide_output_data_shorts(device, (unsigned short *) ccb, ccblen / 2);
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001304
Wolfgang Denk53677ef2008-05-20 16:00:29 +02001305 /* ATAPI Command written wait for completition */
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001306 udelay(5000); /* device must set bsy */
wdenkc6097192002-11-03 00:24:07 +00001307
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001308 mask = ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR;
1309 /*
1310 * if no data wait for DRQ = 0 BSY = 0
1311 * if data wait for DRQ = 1 BSY = 0
1312 */
1313 res = 0;
1314 if (buflen)
wdenkc6097192002-11-03 00:24:07 +00001315 res = ATA_STAT_DRQ;
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001316 c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
1317 if ((c & mask) != res) {
wdenkc6097192002-11-03 00:24:07 +00001318 if (c & ATA_STAT_ERR) {
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001319 err = (ide_inb(device, ATA_ERROR_REG)) >> 4;
1320 debug("atapi_issue 1 returned sense key %X status %02X\n",
1321 err, c);
wdenkc6097192002-11-03 00:24:07 +00001322 } else {
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001323 printf("ATAPI_ISSUE: (no DRQ) after sending ccb (%x) status 0x%02x\n",
1324 ccb[0], c);
1325 err = 0xFF;
wdenkc6097192002-11-03 00:24:07 +00001326 }
1327 goto AI_OUT;
1328 }
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001329 n = ide_inb(device, ATA_CYL_HIGH);
1330 n <<= 8;
1331 n += ide_inb(device, ATA_CYL_LOW);
1332 if (n > buflen) {
1333 printf("ERROR, transfer bytes %d requested only %d\n", n,
1334 buflen);
1335 err = 0xff;
wdenkc6097192002-11-03 00:24:07 +00001336 goto AI_OUT;
1337 }
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001338 if ((n == 0) && (buflen < 0)) {
1339 printf("ERROR, transfer bytes %d requested %d\n", n, buflen);
1340 err = 0xff;
wdenkc6097192002-11-03 00:24:07 +00001341 goto AI_OUT;
1342 }
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001343 if (n != buflen) {
1344 debug("WARNING, transfer bytes %d not equal with requested %d\n",
1345 n, buflen);
wdenkc6097192002-11-03 00:24:07 +00001346 }
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001347 if (n != 0) { /* data transfer */
1348 debug("ATAPI_ISSUE: %d Bytes to transfer\n", n);
1349 /* we transfer shorts */
1350 n >>= 1;
wdenkc6097192002-11-03 00:24:07 +00001351 /* ok now decide if it is an in or output */
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001352 if ((ide_inb(device, ATA_SECT_CNT) & 0x02) == 0) {
1353 debug("Write to device\n");
Pavel Herrmannf5b82c02012-10-09 07:04:39 +00001354 ide_output_data_shorts(device,
1355 (unsigned short *) buffer, n);
wdenkc6097192002-11-03 00:24:07 +00001356 } else {
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001357 debug("Read from device @ %p shorts %d\n", buffer, n);
Pavel Herrmannf5b82c02012-10-09 07:04:39 +00001358 ide_input_data_shorts(device,
1359 (unsigned short *) buffer, n);
wdenkc6097192002-11-03 00:24:07 +00001360 }
1361 }
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001362 udelay(5000); /* seems that some CD ROMs need this... */
1363 mask = ATA_STAT_BUSY | ATA_STAT_ERR;
1364 res = 0;
1365 c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
wdenkc6097192002-11-03 00:24:07 +00001366 if ((c & ATA_STAT_ERR) == ATA_STAT_ERR) {
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001367 err = (ide_inb(device, ATA_ERROR_REG) >> 4);
1368 debug("atapi_issue 2 returned sense key %X status %X\n", err,
1369 c);
wdenkc6097192002-11-03 00:24:07 +00001370 } else {
1371 err = 0;
1372 }
1373AI_OUT:
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001374 ide_led(DEVICE_LED(device), 0); /* LED off */
wdenkc6097192002-11-03 00:24:07 +00001375 return (err);
1376}
1377
1378/*
1379 * sending the command to atapi_issue. If an status other than good
1380 * returns, an request_sense will be issued
1381 */
1382
Wolfgang Denk53677ef2008-05-20 16:00:29 +02001383#define ATAPI_DRIVE_NOT_READY 100
wdenkc6097192002-11-03 00:24:07 +00001384#define ATAPI_UNIT_ATTN 10
1385
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001386unsigned char atapi_issue_autoreq(int device,
1387 unsigned char *ccb,
1388 int ccblen,
1389 unsigned char *buffer, int buflen)
wdenkc6097192002-11-03 00:24:07 +00001390{
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001391 unsigned char sense_data[18], sense_ccb[12];
1392 unsigned char res, key, asc, ascq;
1393 int notready, unitattn;
wdenkc6097192002-11-03 00:24:07 +00001394
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001395 unitattn = ATAPI_UNIT_ATTN;
1396 notready = ATAPI_DRIVE_NOT_READY;
wdenkc6097192002-11-03 00:24:07 +00001397
1398retry:
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001399 res = atapi_issue(device, ccb, ccblen, buffer, buflen);
1400 if (res == 0)
1401 return 0; /* Ok */
wdenkc6097192002-11-03 00:24:07 +00001402
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001403 if (res == 0xFF)
1404 return 0xFF; /* error */
wdenkc6097192002-11-03 00:24:07 +00001405
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001406 debug("(auto_req)atapi_issue returned sense key %X\n", res);
wdenkc6097192002-11-03 00:24:07 +00001407
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001408 memset(sense_ccb, 0, sizeof(sense_ccb));
1409 memset(sense_data, 0, sizeof(sense_data));
1410 sense_ccb[0] = ATAPI_CMD_REQ_SENSE;
1411 sense_ccb[4] = 18; /* allocation Length */
wdenkc6097192002-11-03 00:24:07 +00001412
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001413 res = atapi_issue(device, sense_ccb, 12, sense_data, 18);
1414 key = (sense_data[2] & 0xF);
1415 asc = (sense_data[12]);
1416 ascq = (sense_data[13]);
wdenkc6097192002-11-03 00:24:07 +00001417
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001418 debug("ATAPI_CMD_REQ_SENSE returned %x\n", res);
1419 debug(" Sense page: %02X key %02X ASC %02X ASCQ %02X\n",
1420 sense_data[0], key, asc, ascq);
wdenkc6097192002-11-03 00:24:07 +00001421
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001422 if ((key == 0))
1423 return 0; /* ok device ready */
wdenkc6097192002-11-03 00:24:07 +00001424
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001425 if ((key == 6) || (asc == 0x29) || (asc == 0x28)) { /* Unit Attention */
1426 if (unitattn-- > 0) {
1427 udelay(200 * 1000);
wdenkc6097192002-11-03 00:24:07 +00001428 goto retry;
1429 }
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001430 printf("Unit Attention, tried %d\n", ATAPI_UNIT_ATTN);
wdenkc6097192002-11-03 00:24:07 +00001431 goto error;
1432 }
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001433 if ((asc == 0x4) && (ascq == 0x1)) {
1434 /* not ready, but will be ready soon */
1435 if (notready-- > 0) {
1436 udelay(200 * 1000);
wdenkc6097192002-11-03 00:24:07 +00001437 goto retry;
1438 }
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001439 printf("Drive not ready, tried %d times\n",
1440 ATAPI_DRIVE_NOT_READY);
wdenkc6097192002-11-03 00:24:07 +00001441 goto error;
1442 }
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001443 if (asc == 0x3a) {
1444 debug("Media not present\n");
wdenkc6097192002-11-03 00:24:07 +00001445 goto error;
1446 }
wdenkc7de8292002-11-19 11:04:11 +00001447
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001448 printf("ERROR: Unknown Sense key %02X ASC %02X ASCQ %02X\n", key, asc,
1449 ascq);
wdenkc6097192002-11-03 00:24:07 +00001450error:
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001451 debug("ERROR Sense key %02X ASC %02X ASCQ %02X\n", key, asc, ascq);
wdenkc6097192002-11-03 00:24:07 +00001452 return (0xFF);
1453}
1454
1455
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001456static void atapi_inquiry(block_dev_desc_t *dev_desc)
wdenkc6097192002-11-03 00:24:07 +00001457{
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001458 unsigned char ccb[12]; /* Command descriptor block */
1459 unsigned char iobuf[64]; /* temp buf */
wdenkc6097192002-11-03 00:24:07 +00001460 unsigned char c;
1461 int device;
1462
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001463 device = dev_desc->dev;
1464 dev_desc->type = DEV_TYPE_UNKNOWN; /* not yet valid */
1465 dev_desc->block_read = atapi_read;
wdenkc6097192002-11-03 00:24:07 +00001466
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001467 memset(ccb, 0, sizeof(ccb));
1468 memset(iobuf, 0, sizeof(iobuf));
wdenkc6097192002-11-03 00:24:07 +00001469
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001470 ccb[0] = ATAPI_CMD_INQUIRY;
1471 ccb[4] = 40; /* allocation Legnth */
1472 c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *) iobuf, 40);
wdenkc6097192002-11-03 00:24:07 +00001473
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001474 debug("ATAPI_CMD_INQUIRY returned %x\n", c);
1475 if (c != 0)
wdenkc6097192002-11-03 00:24:07 +00001476 return;
1477
1478 /* copy device ident strings */
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001479 ident_cpy((unsigned char *) dev_desc->vendor, &iobuf[8], 8);
1480 ident_cpy((unsigned char *) dev_desc->product, &iobuf[16], 16);
1481 ident_cpy((unsigned char *) dev_desc->revision, &iobuf[32], 5);
wdenkc6097192002-11-03 00:24:07 +00001482
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001483 dev_desc->lun = 0;
1484 dev_desc->lba = 0;
1485 dev_desc->blksz = 0;
1486 dev_desc->type = iobuf[0] & 0x1f;
wdenkc6097192002-11-03 00:24:07 +00001487
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001488 if ((iobuf[1] & 0x80) == 0x80)
wdenkc6097192002-11-03 00:24:07 +00001489 dev_desc->removable = 1;
1490 else
1491 dev_desc->removable = 0;
1492
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001493 memset(ccb, 0, sizeof(ccb));
1494 memset(iobuf, 0, sizeof(iobuf));
1495 ccb[0] = ATAPI_CMD_START_STOP;
1496 ccb[4] = 0x03; /* start */
wdenkc6097192002-11-03 00:24:07 +00001497
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001498 c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *) iobuf, 0);
wdenkc6097192002-11-03 00:24:07 +00001499
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001500 debug("ATAPI_CMD_START_STOP returned %x\n", c);
1501 if (c != 0)
wdenkc6097192002-11-03 00:24:07 +00001502 return;
1503
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001504 memset(ccb, 0, sizeof(ccb));
1505 memset(iobuf, 0, sizeof(iobuf));
1506 c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *) iobuf, 0);
wdenkc6097192002-11-03 00:24:07 +00001507
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001508 debug("ATAPI_CMD_UNIT_TEST_READY returned %x\n", c);
1509 if (c != 0)
wdenkc6097192002-11-03 00:24:07 +00001510 return;
1511
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001512 memset(ccb, 0, sizeof(ccb));
1513 memset(iobuf, 0, sizeof(iobuf));
1514 ccb[0] = ATAPI_CMD_READ_CAP;
1515 c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *) iobuf, 8);
1516 debug("ATAPI_CMD_READ_CAP returned %x\n", c);
1517 if (c != 0)
wdenkc6097192002-11-03 00:24:07 +00001518 return;
1519
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001520 debug("Read Cap: LBA %02X%02X%02X%02X blksize %02X%02X%02X%02X\n",
1521 iobuf[0], iobuf[1], iobuf[2], iobuf[3],
1522 iobuf[4], iobuf[5], iobuf[6], iobuf[7]);
wdenkc6097192002-11-03 00:24:07 +00001523
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001524 dev_desc->lba = ((unsigned long) iobuf[0] << 24) +
1525 ((unsigned long) iobuf[1] << 16) +
1526 ((unsigned long) iobuf[2] << 8) + ((unsigned long) iobuf[3]);
1527 dev_desc->blksz = ((unsigned long) iobuf[4] << 24) +
1528 ((unsigned long) iobuf[5] << 16) +
1529 ((unsigned long) iobuf[6] << 8) + ((unsigned long) iobuf[7]);
wdenk42dfe7a2004-03-14 22:25:36 +00001530#ifdef CONFIG_LBA48
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001531 /* ATAPI devices cannot use 48bit addressing (ATA/ATAPI v7) */
1532 dev_desc->lba48 = 0;
wdenk42dfe7a2004-03-14 22:25:36 +00001533#endif
wdenkc6097192002-11-03 00:24:07 +00001534 return;
1535}
1536
1537
1538/*
1539 * atapi_read:
1540 * we transfer only one block per command, since the multiple DRQ per
1541 * command is not yet implemented
1542 */
1543#define ATAPI_READ_MAX_BYTES 2048 /* we read max 2kbytes */
1544#define ATAPI_READ_BLOCK_SIZE 2048 /* assuming CD part */
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001545#define ATAPI_READ_MAX_BLOCK (ATAPI_READ_MAX_BYTES/ATAPI_READ_BLOCK_SIZE)
wdenkc6097192002-11-03 00:24:07 +00001546
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001547ulong atapi_read(int device, lbaint_t blknr, ulong blkcnt, void *buffer)
wdenkc6097192002-11-03 00:24:07 +00001548{
1549 ulong n = 0;
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001550 unsigned char ccb[12]; /* Command descriptor block */
wdenkc6097192002-11-03 00:24:07 +00001551 ulong cnt;
1552
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001553 debug("atapi_read dev %d start %lX, blocks %lX buffer at %lX\n",
1554 device, blknr, blkcnt, (ulong) buffer);
wdenkc6097192002-11-03 00:24:07 +00001555
1556 do {
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001557 if (blkcnt > ATAPI_READ_MAX_BLOCK)
1558 cnt = ATAPI_READ_MAX_BLOCK;
1559 else
1560 cnt = blkcnt;
wdenkc6097192002-11-03 00:24:07 +00001561
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001562 ccb[0] = ATAPI_CMD_READ_12;
1563 ccb[1] = 0; /* reserved */
1564 ccb[2] = (unsigned char) (blknr >> 24) & 0xFF; /* MSB Block */
1565 ccb[3] = (unsigned char) (blknr >> 16) & 0xFF; /* */
1566 ccb[4] = (unsigned char) (blknr >> 8) & 0xFF;
1567 ccb[5] = (unsigned char) blknr & 0xFF; /* LSB Block */
1568 ccb[6] = (unsigned char) (cnt >> 24) & 0xFF; /* MSB Block cnt */
1569 ccb[7] = (unsigned char) (cnt >> 16) & 0xFF;
1570 ccb[8] = (unsigned char) (cnt >> 8) & 0xFF;
1571 ccb[9] = (unsigned char) cnt & 0xFF; /* LSB Block */
1572 ccb[10] = 0; /* reserved */
1573 ccb[11] = 0; /* reserved */
1574
1575 if (atapi_issue_autoreq(device, ccb, 12,
1576 (unsigned char *) buffer,
1577 cnt * ATAPI_READ_BLOCK_SIZE)
1578 == 0xFF) {
wdenkc6097192002-11-03 00:24:07 +00001579 return (n);
1580 }
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001581 n += cnt;
1582 blkcnt -= cnt;
1583 blknr += cnt;
1584 buffer += (cnt * ATAPI_READ_BLOCK_SIZE);
wdenkc6097192002-11-03 00:24:07 +00001585 } while (blkcnt > 0);
1586 return (n);
1587}
1588
1589/* ------------------------------------------------------------------------- */
1590
1591#endif /* CONFIG_ATAPI */
1592
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001593U_BOOT_CMD(ide, 5, 1, do_ide,
1594 "IDE sub-system",
1595 "reset - reset IDE controller\n"
1596 "ide info - show available IDE devices\n"
1597 "ide device [dev] - show or set current device\n"
1598 "ide part [dev] - print partition table of one or all IDE devices\n"
1599 "ide read addr blk# cnt\n"
1600 "ide write addr blk# cnt - read/write `cnt'"
1601 " blocks starting at block `blk#'\n"
1602 " to/from memory address `addr'");
wdenk8bde7f72003-06-27 21:31:46 +00001603
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001604U_BOOT_CMD(diskboot, 3, 1, do_diskboot,
1605 "boot from IDE device", "loadAddr dev:part");