blob: ab790f67537d7de4e9a78a98bbc84cc01c3d8881 [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
84#ifdef CONFIG_IDE_LED
Wolfgang Denk953b7e62010-06-13 18:28:54 +020085# if !defined(CONFIG_BMS2003) && \
86 !defined(CONFIG_CPC45) && \
87 !defined(CONFIG_KUP4K) && \
88 !defined(CONFIG_KUP4X)
wdenkc6097192002-11-03 00:24:07 +000089static void ide_led (uchar led, uchar status);
90#else
wdenk1f53a412002-12-04 23:39:58 +000091extern void ide_led (uchar led, uchar status);
92#endif
93#else
wdenkc6097192002-11-03 00:24:07 +000094#define ide_led(a,b) /* dummy */
95#endif
96
97#ifdef CONFIG_IDE_RESET
98static void ide_reset (void);
99#else
100#define ide_reset() /* dummy */
101#endif
102
103static void ide_ident (block_dev_desc_t *dev_desc);
104static uchar ide_wait (int dev, ulong t);
105
106#define IDE_TIME_OUT 2000 /* 2 sec timeout */
107
108#define ATAPI_TIME_OUT 7000 /* 7 sec timeout (5 sec seems to work...) */
109
110#define IDE_SPIN_UP_TIME_OUT 5000 /* 5 sec spin-up timeout */
111
wdenkc6097192002-11-03 00:24:07 +0000112static void ident_cpy (unsigned char *dest, unsigned char *src, unsigned int len);
113
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200114#ifndef CONFIG_SYS_ATA_PORT_ADDR
115#define CONFIG_SYS_ATA_PORT_ADDR(port) (port)
Heiko Schocher566a4942007-06-22 19:11:54 +0200116#endif
wdenkc6097192002-11-03 00:24:07 +0000117
118#ifdef CONFIG_ATAPI
119static void atapi_inquiry(block_dev_desc_t *dev_desc);
Grant Likelyeb867a72007-02-20 09:05:45 +0100120ulong atapi_read (int device, lbaint_t blknr, ulong blkcnt, void *buffer);
wdenkc6097192002-11-03 00:24:07 +0000121#endif
122
123
wdenkc6097192002-11-03 00:24:07 +0000124/* ------------------------------------------------------------------------- */
125
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000126int do_ide(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
wdenkc6097192002-11-03 00:24:07 +0000127{
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000128 int rcode = 0;
wdenkc6097192002-11-03 00:24:07 +0000129
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000130 switch (argc) {
131 case 0:
132 case 1:
Simon Glass4c12eeb2011-12-10 08:44:01 +0000133 return CMD_RET_USAGE;
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000134 case 2:
135 if (strncmp(argv[1], "res", 3) == 0) {
136 puts("\nReset IDE"
137#ifdef CONFIG_IDE_8xx_DIRECT
138 " on PCMCIA " PCMCIA_SLOT_MSG
139#endif
140 ": ");
wdenkc6097192002-11-03 00:24:07 +0000141
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000142 ide_init();
143 return 0;
144 } else if (strncmp(argv[1], "inf", 3) == 0) {
145 int i;
146
147 putc('\n');
148
149 for (i = 0; i < CONFIG_SYS_IDE_MAXDEVICE; ++i) {
150 if (ide_dev_desc[i].type == DEV_TYPE_UNKNOWN)
151 continue; /* list only known devices */
152 printf("IDE device %d: ", i);
153 dev_print(&ide_dev_desc[i]);
154 }
155 return 0;
156
157 } else if (strncmp(argv[1], "dev", 3) == 0) {
158 if ((curr_device < 0)
159 || (curr_device >= CONFIG_SYS_IDE_MAXDEVICE)) {
160 puts("\nno IDE devices available\n");
161 return 1;
162 }
163 printf("\nIDE device %d: ", curr_device);
164 dev_print(&ide_dev_desc[curr_device]);
165 return 0;
166 } else if (strncmp(argv[1], "part", 4) == 0) {
167 int dev, ok;
168
169 for (ok = 0, dev = 0;
170 dev < CONFIG_SYS_IDE_MAXDEVICE;
171 ++dev) {
172 if (ide_dev_desc[dev].part_type !=
173 PART_TYPE_UNKNOWN) {
174 ++ok;
175 if (dev)
176 putc('\n');
177 print_part(&ide_dev_desc[dev]);
178 }
179 }
180 if (!ok) {
181 puts("\nno IDE devices available\n");
182 rcode++;
183 }
184 return rcode;
185 }
Simon Glass4c12eeb2011-12-10 08:44:01 +0000186 return CMD_RET_USAGE;
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000187 case 3:
188 if (strncmp(argv[1], "dev", 3) == 0) {
189 int dev = (int) simple_strtoul(argv[2], NULL, 10);
190
191 printf("\nIDE device %d: ", dev);
192 if (dev >= CONFIG_SYS_IDE_MAXDEVICE) {
193 puts("unknown device\n");
194 return 1;
195 }
196 dev_print(&ide_dev_desc[dev]);
197 /*ide_print (dev); */
198
199 if (ide_dev_desc[dev].type == DEV_TYPE_UNKNOWN)
200 return 1;
201
202 curr_device = dev;
203
204 puts("... is now current device\n");
205
206 return 0;
207 } else if (strncmp(argv[1], "part", 4) == 0) {
208 int dev = (int) simple_strtoul(argv[2], NULL, 10);
209
210 if (ide_dev_desc[dev].part_type != PART_TYPE_UNKNOWN) {
211 print_part(&ide_dev_desc[dev]);
212 } else {
213 printf("\nIDE device %d not available\n",
214 dev);
215 rcode = 1;
216 }
217 return rcode;
218 }
219
Simon Glass4c12eeb2011-12-10 08:44:01 +0000220 return CMD_RET_USAGE;
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000221 default:
222 /* at least 4 args */
223
224 if (strcmp(argv[1], "read") == 0) {
225 ulong addr = simple_strtoul(argv[2], NULL, 16);
226 ulong cnt = simple_strtoul(argv[4], NULL, 16);
227 ulong n;
228
229#ifdef CONFIG_SYS_64BIT_LBA
230 lbaint_t blk = simple_strtoull(argv[3], NULL, 16);
231
232 printf("\nIDE read: device %d block # %lld, count %ld ... ",
233 curr_device, blk, cnt);
234#else
235 lbaint_t blk = simple_strtoul(argv[3], NULL, 16);
236
237 printf("\nIDE read: device %d block # %ld, count %ld ... ",
238 curr_device, blk, cnt);
239#endif
240
241 n = ide_dev_desc[curr_device].block_read(curr_device,
242 blk, cnt,
243 (ulong *)addr);
244 /* flush cache after read */
245 flush_cache(addr,
246 cnt * ide_dev_desc[curr_device].blksz);
247
248 printf("%ld blocks read: %s\n",
249 n, (n == cnt) ? "OK" : "ERROR");
250 if (n == cnt)
251 return 0;
252 else
253 return 1;
254 } else if (strcmp(argv[1], "write") == 0) {
255 ulong addr = simple_strtoul(argv[2], NULL, 16);
256 ulong cnt = simple_strtoul(argv[4], NULL, 16);
257 ulong n;
258
259#ifdef CONFIG_SYS_64BIT_LBA
260 lbaint_t blk = simple_strtoull(argv[3], NULL, 16);
261
262 printf("\nIDE write: device %d block # %lld, count %ld ... ",
263 curr_device, blk, cnt);
264#else
265 lbaint_t blk = simple_strtoul(argv[3], NULL, 16);
266
267 printf("\nIDE write: device %d block # %ld, count %ld ... ",
268 curr_device, blk, cnt);
269#endif
270 n = ide_write(curr_device, blk, cnt, (ulong *) addr);
271
272 printf("%ld blocks written: %s\n",
273 n, (n == cnt) ? "OK" : "ERROR");
274 if (n == cnt)
275 return 0;
276 else
277 return 1;
278 } else {
Simon Glass4c12eeb2011-12-10 08:44:01 +0000279 return CMD_RET_USAGE;
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000280 }
281
282 return rcode;
283 }
wdenkc6097192002-11-03 00:24:07 +0000284}
285
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000286int do_diskboot(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
wdenkc6097192002-11-03 00:24:07 +0000287{
Rob Herring7405a132012-09-21 04:02:30 +0000288 return common_diskboot(cmdtp, "ide", argc, argv);
wdenkc6097192002-11-03 00:24:07 +0000289}
290
291/* ------------------------------------------------------------------------- */
292
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000293inline void __ide_outb(int dev, int port, unsigned char val)
Stefan Roesef2302d42008-08-06 14:05:38 +0200294{
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000295 debug("ide_outb (dev= %d, port= 0x%x, val= 0x%02x) : @ 0x%08lx\n",
296 dev, port, val,
297 (ATA_CURR_BASE(dev) + CONFIG_SYS_ATA_PORT_ADDR(port)));
Macpaul Lin0abddf82011-04-11 20:45:32 +0000298
299#if defined(CONFIG_IDE_AHB)
300 if (port) {
301 /* write command */
302 ide_write_register(dev, port, val);
303 } else {
304 /* write data */
305 outb(val, (ATA_CURR_BASE(dev)));
306 }
307#else
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000308 outb(val, (ATA_CURR_BASE(dev) + CONFIG_SYS_ATA_PORT_ADDR(port)));
Macpaul Lin0abddf82011-04-11 20:45:32 +0000309#endif
Stefan Roesef2302d42008-08-06 14:05:38 +0200310}
Macpaul Lin0abddf82011-04-11 20:45:32 +0000311
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000312void ide_outb(int dev, int port, unsigned char val)
313 __attribute__ ((weak, alias("__ide_outb")));
Stefan Roesef2302d42008-08-06 14:05:38 +0200314
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000315inline unsigned char __ide_inb(int dev, int port)
Stefan Roesef2302d42008-08-06 14:05:38 +0200316{
317 uchar val;
Macpaul Lin0abddf82011-04-11 20:45:32 +0000318
319#if defined(CONFIG_IDE_AHB)
320 val = ide_read_register(dev, port);
321#else
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000322 val = inb((ATA_CURR_BASE(dev) + CONFIG_SYS_ATA_PORT_ADDR(port)));
Macpaul Lin0abddf82011-04-11 20:45:32 +0000323#endif
324
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000325 debug("ide_inb (dev= %d, port= 0x%x) : @ 0x%08lx -> 0x%02x\n",
326 dev, port,
327 (ATA_CURR_BASE(dev) + CONFIG_SYS_ATA_PORT_ADDR(port)), val);
Stefan Roesef2302d42008-08-06 14:05:38 +0200328 return val;
329}
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000330
Kim Phillips2df72b82009-05-19 12:53:36 -0500331unsigned char ide_inb(int dev, int port)
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000332 __attribute__ ((weak, alias("__ide_inb")));
Stefan Roesef2302d42008-08-06 14:05:38 +0200333
Steven A. Falco36c2d302008-08-15 15:34:10 -0400334#ifdef CONFIG_TUNE_PIO
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000335inline int __ide_set_piomode(int pio_mode)
Steven A. Falco36c2d302008-08-15 15:34:10 -0400336{
337 return 0;
338}
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000339
340inline int ide_set_piomode(int pio_mode)
341 __attribute__ ((weak, alias("__ide_set_piomode")));
Steven A. Falco36c2d302008-08-15 15:34:10 -0400342#endif
343
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000344void ide_init(void)
wdenkc6097192002-11-03 00:24:07 +0000345{
wdenkc6097192002-11-03 00:24:07 +0000346 unsigned char c;
347 int i, bus;
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000348
wdenk9fd5e312003-12-07 23:55:12 +0000349#ifdef CONFIG_IDE_8xx_PCCARD
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000350 extern int ide_devices_found; /* Initialized in check_ide_device() */
351#endif /* CONFIG_IDE_8xx_PCCARD */
wdenk9fd5e312003-12-07 23:55:12 +0000352
353#ifdef CONFIG_IDE_PREINIT
354 WATCHDOG_RESET();
355
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000356 if (ide_preinit()) {
357 puts("ide_preinit failed\n");
wdenk9fd5e312003-12-07 23:55:12 +0000358 return;
359 }
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000360#endif /* CONFIG_IDE_PREINIT */
wdenkc6097192002-11-03 00:24:07 +0000361
wdenkc6097192002-11-03 00:24:07 +0000362 WATCHDOG_RESET();
363
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000364 /*
365 * Reset the IDE just to be sure.
wdenkc6097192002-11-03 00:24:07 +0000366 * Light LED's to show
367 */
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000368 ide_led((LED_IDE1 | LED_IDE2), 1); /* LED's on */
369
370 /* ATAPI Drives seems to need a proper IDE Reset */
371 ide_reset();
wdenkc6097192002-11-03 00:24:07 +0000372
Pavel Herrmann8d1165e11a2012-10-09 07:01:56 +0000373#ifdef CONFIG_IDE_INIT_POSTRESET
374 WATCHDOG_RESET();
wdenkc6097192002-11-03 00:24:07 +0000375
Pavel Herrmann8d1165e11a2012-10-09 07:01:56 +0000376 if (ide_init_postreset()) {
377 puts("ide_preinit_postreset failed\n");
378 return;
379 }
380#endif /* CONFIG_IDE_INIT_POSTRESET */
wdenkc6097192002-11-03 00:24:07 +0000381
382 /*
383 * Wait for IDE to get ready.
384 * According to spec, this can take up to 31 seconds!
385 */
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000386 for (bus = 0; bus < CONFIG_SYS_IDE_MAXBUS; ++bus) {
387 int dev =
388 bus * (CONFIG_SYS_IDE_MAXDEVICE /
389 CONFIG_SYS_IDE_MAXBUS);
wdenkc6097192002-11-03 00:24:07 +0000390
wdenk6069ff22003-02-28 00:49:47 +0000391#ifdef CONFIG_IDE_8xx_PCCARD
392 /* Skip non-ide devices from probing */
393 if ((ide_devices_found & (1 << bus)) == 0) {
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000394 ide_led((LED_IDE1 | LED_IDE2), 0); /* LED's off */
wdenk6069ff22003-02-28 00:49:47 +0000395 continue;
396 }
397#endif
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000398 printf("Bus %d: ", bus);
wdenkc6097192002-11-03 00:24:07 +0000399
400 ide_bus_ok[bus] = 0;
401
402 /* Select device
403 */
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000404 udelay(100000); /* 100 ms */
405 ide_outb(dev, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(dev));
406 udelay(100000); /* 100 ms */
wdenkc6097192002-11-03 00:24:07 +0000407 i = 0;
408 do {
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000409 udelay(10000); /* 10 ms */
wdenkc6097192002-11-03 00:24:07 +0000410
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000411 c = ide_inb(dev, ATA_STATUS);
wdenkc6097192002-11-03 00:24:07 +0000412 i++;
413 if (i > (ATA_RESET_TIME * 100)) {
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000414 puts("** Timeout **\n");
415 /* LED's off */
416 ide_led((LED_IDE1 | LED_IDE2), 0);
wdenkc6097192002-11-03 00:24:07 +0000417 return;
418 }
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000419 if ((i >= 100) && ((i % 100) == 0))
420 putc('.');
421
wdenkc6097192002-11-03 00:24:07 +0000422 } while (c & ATA_STAT_BUSY);
423
424 if (c & (ATA_STAT_BUSY | ATA_STAT_FAULT)) {
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000425 puts("not available ");
426 debug("Status = 0x%02X ", c);
427#ifndef CONFIG_ATAPI /* ATAPI Devices do not set DRDY */
428 } else if ((c & ATA_STAT_READY) == 0) {
429 puts("not available ");
430 debug("Status = 0x%02X ", c);
wdenkc6097192002-11-03 00:24:07 +0000431#endif
432 } else {
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000433 puts("OK ");
wdenkc6097192002-11-03 00:24:07 +0000434 ide_bus_ok[bus] = 1;
435 }
436 WATCHDOG_RESET();
437 }
wdenkc7de8292002-11-19 11:04:11 +0000438
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000439 putc('\n');
wdenkc6097192002-11-03 00:24:07 +0000440
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000441 ide_led((LED_IDE1 | LED_IDE2), 0); /* LED's off */
wdenkc6097192002-11-03 00:24:07 +0000442
443 curr_device = -1;
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000444 for (i = 0; i < CONFIG_SYS_IDE_MAXDEVICE; ++i) {
wdenkc6097192002-11-03 00:24:07 +0000445#ifdef CONFIG_IDE_LED
446 int led = (IDE_BUS(i) == 0) ? LED_IDE1 : LED_IDE2;
447#endif
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000448 ide_dev_desc[i].type = DEV_TYPE_UNKNOWN;
449 ide_dev_desc[i].if_type = IF_TYPE_IDE;
450 ide_dev_desc[i].dev = i;
451 ide_dev_desc[i].part_type = PART_TYPE_UNKNOWN;
452 ide_dev_desc[i].blksz = 0;
453 ide_dev_desc[i].lba = 0;
454 ide_dev_desc[i].block_read = ide_read;
Macpaul Lin0abddf82011-04-11 20:45:32 +0000455 ide_dev_desc[i].block_write = ide_write;
wdenkc6097192002-11-03 00:24:07 +0000456 if (!ide_bus_ok[IDE_BUS(i)])
457 continue;
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000458 ide_led(led, 1); /* LED on */
wdenkc6097192002-11-03 00:24:07 +0000459 ide_ident(&ide_dev_desc[i]);
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000460 ide_led(led, 0); /* LED off */
wdenkc6097192002-11-03 00:24:07 +0000461 dev_print(&ide_dev_desc[i]);
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000462
wdenkc6097192002-11-03 00:24:07 +0000463 if ((ide_dev_desc[i].lba > 0) && (ide_dev_desc[i].blksz > 0)) {
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000464 /* initialize partition type */
465 init_part(&ide_dev_desc[i]);
wdenkc6097192002-11-03 00:24:07 +0000466 if (curr_device < 0)
467 curr_device = i;
468 }
469 }
470 WATCHDOG_RESET();
471}
472
473/* ------------------------------------------------------------------------- */
474
Matthew McClintockdf3fc522011-05-24 05:31:19 +0000475#ifdef CONFIG_PARTITIONS
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000476block_dev_desc_t *ide_get_dev(int dev)
wdenkc6097192002-11-03 00:24:07 +0000477{
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200478 return (dev < CONFIG_SYS_IDE_MAXDEVICE) ? &ide_dev_desc[dev] : NULL;
wdenkc6097192002-11-03 00:24:07 +0000479}
Matthew McClintockdf3fc522011-05-24 05:31:19 +0000480#endif
wdenkc6097192002-11-03 00:24:07 +0000481
wdenkc6097192002-11-03 00:24:07 +0000482/* ------------------------------------------------------------------------- */
483
Pavel Herrmannf5b82c02012-10-09 07:04:39 +0000484void ide_input_swap_data(int dev, ulong *sect_buf, int words)
485 __attribute__ ((weak, alias("__ide_input_swap_data")));
486
487void ide_input_data(int dev, ulong *sect_buf, int words)
488 __attribute__ ((weak, alias("__ide_input_data")));
489
490void ide_output_data(int dev, const ulong *sect_buf, int words)
491 __attribute__ ((weak, alias("__ide_output_data")));
492
wdenk5da627a2003-10-09 20:09:04 +0000493/* We only need to swap data if we are running on a big endian cpu. */
494/* But Au1x00 cpu:s already swaps data in big endian mode! */
Tom Rini61c0d6a2012-09-26 15:20:33 -0700495#if defined(__LITTLE_ENDIAN) || defined(CONFIG_SOC_AU1X00)
Pavel Herrmannf5b82c02012-10-09 07:04:39 +0000496void __ide_input_swap_data(int dev, ulong *sect_buf, int words)
497{
498 ide_input_data(dev, sect_buf, words);
499}
wdenk5da627a2003-10-09 20:09:04 +0000500#else
Pavel Herrmannf5b82c02012-10-09 07:04:39 +0000501void __ide_input_swap_data(int dev, ulong *sect_buf, int words)
wdenkc6097192002-11-03 00:24:07 +0000502{
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000503 volatile ushort *pbuf =
504 (ushort *) (ATA_CURR_BASE(dev) + ATA_DATA_REG);
505 ushort *dbuf = (ushort *) sect_buf;
wdenk1a344f22005-02-03 23:00:49 +0000506
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000507 debug("in input swap data base for read is %lx\n",
508 (unsigned long) pbuf);
wdenk1a344f22005-02-03 23:00:49 +0000509
510 while (words--) {
Wolfgang Denk0c32d962006-06-16 17:32:31 +0200511#ifdef __MIPS__
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000512 *dbuf++ = swab16p((u16 *) pbuf);
513 *dbuf++ = swab16p((u16 *) pbuf);
Heiko Schocher566a4942007-06-22 19:11:54 +0200514#elif defined(CONFIG_PCS440EP)
515 *dbuf++ = *pbuf;
516 *dbuf++ = *pbuf;
Wolfgang Denk0c32d962006-06-16 17:32:31 +0200517#else
wdenk1a344f22005-02-03 23:00:49 +0000518 *dbuf++ = ld_le16(pbuf);
519 *dbuf++ = ld_le16(pbuf);
Wolfgang Denk0c32d962006-06-16 17:32:31 +0200520#endif /* !MIPS */
wdenk1a344f22005-02-03 23:00:49 +0000521 }
wdenkc6097192002-11-03 00:24:07 +0000522}
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000523#endif /* __LITTLE_ENDIAN || CONFIG_AU1X00 */
wdenkc6097192002-11-03 00:24:07 +0000524
wdenk2262cfe2002-11-18 00:14:45 +0000525
Albert Aribaudf2a37fc2010-08-08 05:17:05 +0530526#if defined(CONFIG_IDE_SWAP_IO)
Pavel Herrmannf5b82c02012-10-09 07:04:39 +0000527void __ide_output_data(int dev, const ulong *sect_buf, int words)
wdenkc6097192002-11-03 00:24:07 +0000528{
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000529 ushort *dbuf;
530 volatile ushort *pbuf;
wdenk1a344f22005-02-03 23:00:49 +0000531
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000532 pbuf = (ushort *) (ATA_CURR_BASE(dev) + ATA_DATA_REG);
533 dbuf = (ushort *) sect_buf;
wdenk1a344f22005-02-03 23:00:49 +0000534 while (words--) {
Heiko Schocher566a4942007-06-22 19:11:54 +0200535#if defined(CONFIG_PCS440EP)
536 /* not tested, because CF was write protected */
537 EIEIO;
538 *pbuf = ld_le16(dbuf++);
539 EIEIO;
540 *pbuf = ld_le16(dbuf++);
541#else
wdenk1a344f22005-02-03 23:00:49 +0000542 EIEIO;
543 *pbuf = *dbuf++;
544 EIEIO;
545 *pbuf = *dbuf++;
Heiko Schocher566a4942007-06-22 19:11:54 +0200546#endif
wdenk1a344f22005-02-03 23:00:49 +0000547 }
wdenkc6097192002-11-03 00:24:07 +0000548}
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000549#else /* ! CONFIG_IDE_SWAP_IO */
Pavel Herrmannf5b82c02012-10-09 07:04:39 +0000550void __ide_output_data(int dev, const ulong *sect_buf, int words)
wdenk2262cfe2002-11-18 00:14:45 +0000551{
Macpaul Lin0abddf82011-04-11 20:45:32 +0000552#if defined(CONFIG_IDE_AHB)
553 ide_write_data(dev, sect_buf, words);
554#else
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000555 outsw(ATA_CURR_BASE(dev) + ATA_DATA_REG, sect_buf, words << 1);
Macpaul Lin0abddf82011-04-11 20:45:32 +0000556#endif
wdenk2262cfe2002-11-18 00:14:45 +0000557}
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000558#endif /* CONFIG_IDE_SWAP_IO */
wdenkc6097192002-11-03 00:24:07 +0000559
Albert Aribaudf2a37fc2010-08-08 05:17:05 +0530560#if defined(CONFIG_IDE_SWAP_IO)
Pavel Herrmannf5b82c02012-10-09 07:04:39 +0000561void __ide_input_data(int dev, ulong *sect_buf, int words)
wdenkc6097192002-11-03 00:24:07 +0000562{
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000563 ushort *dbuf;
564 volatile ushort *pbuf;
wdenk1a344f22005-02-03 23:00:49 +0000565
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000566 pbuf = (ushort *) (ATA_CURR_BASE(dev) + ATA_DATA_REG);
567 dbuf = (ushort *) sect_buf;
wdenk1a344f22005-02-03 23:00:49 +0000568
569 debug("in input data base for read is %lx\n", (unsigned long) pbuf);
570
571 while (words--) {
Heiko Schocher566a4942007-06-22 19:11:54 +0200572#if defined(CONFIG_PCS440EP)
573 EIEIO;
574 *dbuf++ = ld_le16(pbuf);
575 EIEIO;
576 *dbuf++ = ld_le16(pbuf);
577#else
wdenk1a344f22005-02-03 23:00:49 +0000578 EIEIO;
579 *dbuf++ = *pbuf;
580 EIEIO;
581 *dbuf++ = *pbuf;
Heiko Schocher566a4942007-06-22 19:11:54 +0200582#endif
wdenk1a344f22005-02-03 23:00:49 +0000583 }
wdenkc6097192002-11-03 00:24:07 +0000584}
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000585#else /* ! CONFIG_IDE_SWAP_IO */
Pavel Herrmannf5b82c02012-10-09 07:04:39 +0000586void __ide_input_data(int dev, ulong *sect_buf, int words)
wdenk2262cfe2002-11-18 00:14:45 +0000587{
Macpaul Lin0abddf82011-04-11 20:45:32 +0000588#if defined(CONFIG_IDE_AHB)
589 ide_read_data(dev, sect_buf, words);
590#else
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000591 insw(ATA_CURR_BASE(dev) + ATA_DATA_REG, sect_buf, words << 1);
Macpaul Lin0abddf82011-04-11 20:45:32 +0000592#endif
wdenk2262cfe2002-11-18 00:14:45 +0000593}
594
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000595#endif /* CONFIG_IDE_SWAP_IO */
wdenkc6097192002-11-03 00:24:07 +0000596
597/* -------------------------------------------------------------------------
598 */
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000599static void ide_ident(block_dev_desc_t *dev_desc)
wdenkc6097192002-11-03 00:24:07 +0000600{
wdenkc6097192002-11-03 00:24:07 +0000601 unsigned char c;
Marek Vasutb18eabf2011-08-20 09:15:13 +0000602 hd_driveid_t iop;
wdenkc6097192002-11-03 00:24:07 +0000603
wdenk64f70be2004-09-28 20:34:50 +0000604#ifdef CONFIG_ATAPI
605 int retries = 0;
wdenkc7de8292002-11-19 11:04:11 +0000606#endif
607
Steven A. Falco36c2d302008-08-15 15:34:10 -0400608#ifdef CONFIG_TUNE_PIO
609 int pio_mode;
610#endif
611
wdenkc6097192002-11-03 00:24:07 +0000612#if 0
613 int mode, cycle_time;
614#endif
615 int device;
wdenkc6097192002-11-03 00:24:07 +0000616
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000617 device = dev_desc->dev;
618 printf(" Device %d: ", device);
619
620 ide_led(DEVICE_LED(device), 1); /* LED on */
wdenkc6097192002-11-03 00:24:07 +0000621 /* Select device
622 */
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000623 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
624 dev_desc->if_type = IF_TYPE_IDE;
wdenkc6097192002-11-03 00:24:07 +0000625#ifdef CONFIG_ATAPI
wdenkc7de8292002-11-19 11:04:11 +0000626
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000627 retries = 0;
wdenkc7de8292002-11-19 11:04:11 +0000628
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000629 /* Warning: This will be tricky to read */
630 while (retries <= 1) {
631 /* check signature */
632 if ((ide_inb(device, ATA_SECT_CNT) == 0x01) &&
633 (ide_inb(device, ATA_SECT_NUM) == 0x01) &&
634 (ide_inb(device, ATA_CYL_LOW) == 0x14) &&
635 (ide_inb(device, ATA_CYL_HIGH) == 0xEB)) {
636 /* ATAPI Signature found */
637 dev_desc->if_type = IF_TYPE_ATAPI;
638 /*
639 * Start Ident Command
640 */
641 ide_outb(device, ATA_COMMAND, ATAPI_CMD_IDENT);
642 /*
643 * Wait for completion - ATAPI devices need more time
644 * to become ready
645 */
646 c = ide_wait(device, ATAPI_TIME_OUT);
647 } else
wdenkc6097192002-11-03 00:24:07 +0000648#endif
wdenk1a344f22005-02-03 23:00:49 +0000649 {
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000650 /*
651 * Start Ident Command
652 */
653 ide_outb(device, ATA_COMMAND, ATA_CMD_IDENT);
654
655 /*
656 * Wait for completion
657 */
658 c = ide_wait(device, IDE_TIME_OUT);
wdenk1a344f22005-02-03 23:00:49 +0000659 }
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000660 ide_led(DEVICE_LED(device), 0); /* LED off */
661
662 if (((c & ATA_STAT_DRQ) == 0) ||
663 ((c & (ATA_STAT_FAULT | ATA_STAT_ERR)) != 0)) {
wdenk64f70be2004-09-28 20:34:50 +0000664#ifdef CONFIG_ATAPI
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000665 {
666 /*
667 * Need to soft reset the device
668 * in case it's an ATAPI...
669 */
670 debug("Retrying...\n");
671 ide_outb(device, ATA_DEV_HD,
672 ATA_LBA | ATA_DEVICE(device));
673 udelay(100000);
674 ide_outb(device, ATA_COMMAND, 0x08);
675 udelay(500000); /* 500 ms */
676 }
677 /*
678 * Select device
679 */
680 ide_outb(device, ATA_DEV_HD,
681 ATA_LBA | ATA_DEVICE(device));
682 retries++;
683#else
684 return;
685#endif
686 }
687#ifdef CONFIG_ATAPI
688 else
689 break;
690 } /* see above - ugly to read */
wdenk64f70be2004-09-28 20:34:50 +0000691
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000692 if (retries == 2) /* Not found */
wdenk64f70be2004-09-28 20:34:50 +0000693 return;
694#endif
wdenkc7de8292002-11-19 11:04:11 +0000695
Pavel Herrmannf5b82c02012-10-09 07:04:39 +0000696 ide_input_swap_data(device, (ulong *)&iop, ATA_SECTORWORDS);
wdenkc6097192002-11-03 00:24:07 +0000697
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000698 ident_cpy((unsigned char *) dev_desc->revision, iop.fw_rev,
699 sizeof(dev_desc->revision));
700 ident_cpy((unsigned char *) dev_desc->vendor, iop.model,
701 sizeof(dev_desc->vendor));
702 ident_cpy((unsigned char *) dev_desc->product, iop.serial_no,
703 sizeof(dev_desc->product));
wdenkc3f9d492004-03-14 00:59:59 +0000704#ifdef __LITTLE_ENDIAN
705 /*
Richard Retanubunbcdf1d22008-11-06 14:01:51 -0500706 * firmware revision, model, and serial number have Big Endian Byte
707 * order in Word. Convert all three to little endian.
wdenkc3f9d492004-03-14 00:59:59 +0000708 *
709 * See CF+ and CompactFlash Specification Revision 2.0:
Richard Retanubunbcdf1d22008-11-06 14:01:51 -0500710 * 6.2.1.6: Identify Drive, Table 39 for more details
wdenkc3f9d492004-03-14 00:59:59 +0000711 */
712
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000713 strswab(dev_desc->revision);
714 strswab(dev_desc->vendor);
715 strswab(dev_desc->product);
wdenkc3f9d492004-03-14 00:59:59 +0000716#endif /* __LITTLE_ENDIAN */
wdenkc6097192002-11-03 00:24:07 +0000717
Marek Vasutb18eabf2011-08-20 09:15:13 +0000718 if ((iop.config & 0x0080) == 0x0080)
wdenkc6097192002-11-03 00:24:07 +0000719 dev_desc->removable = 1;
720 else
721 dev_desc->removable = 0;
722
Steven A. Falco36c2d302008-08-15 15:34:10 -0400723#ifdef CONFIG_TUNE_PIO
724 /* Mode 0 - 2 only, are directly determined by word 51. */
Marek Vasutb18eabf2011-08-20 09:15:13 +0000725 pio_mode = iop.tPIO;
Steven A. Falco36c2d302008-08-15 15:34:10 -0400726 if (pio_mode > 2) {
727 printf("WARNING: Invalid PIO (word 51 = %d).\n", pio_mode);
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000728 /* Force it to dead slow, and hope for the best... */
729 pio_mode = 0;
Steven A. Falco36c2d302008-08-15 15:34:10 -0400730 }
731
732 /* Any CompactFlash Storage Card that supports PIO mode 3 or above
733 * shall set bit 1 of word 53 to one and support the fields contained
734 * in words 64 through 70.
735 */
Marek Vasutb18eabf2011-08-20 09:15:13 +0000736 if (iop.field_valid & 0x02) {
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000737 /*
738 * Mode 3 and above are possible. Check in order from slow
Steven A. Falco36c2d302008-08-15 15:34:10 -0400739 * to fast, so we wind up with the highest mode allowed.
740 */
Marek Vasutb18eabf2011-08-20 09:15:13 +0000741 if (iop.eide_pio_modes & 0x01)
Steven A. Falco36c2d302008-08-15 15:34:10 -0400742 pio_mode = 3;
Marek Vasutb18eabf2011-08-20 09:15:13 +0000743 if (iop.eide_pio_modes & 0x02)
Steven A. Falco36c2d302008-08-15 15:34:10 -0400744 pio_mode = 4;
Marek Vasutb18eabf2011-08-20 09:15:13 +0000745 if (ata_id_is_cfa((u16 *)&iop)) {
746 if ((iop.cf_advanced_caps & 0x07) == 0x01)
Steven A. Falco36c2d302008-08-15 15:34:10 -0400747 pio_mode = 5;
Marek Vasutb18eabf2011-08-20 09:15:13 +0000748 if ((iop.cf_advanced_caps & 0x07) == 0x02)
Steven A. Falco36c2d302008-08-15 15:34:10 -0400749 pio_mode = 6;
750 }
751 }
752
753 /* System-specific, depends on bus speeds, etc. */
754 ide_set_piomode(pio_mode);
755#endif /* CONFIG_TUNE_PIO */
756
wdenkc6097192002-11-03 00:24:07 +0000757#if 0
758 /*
759 * Drive PIO mode autoselection
760 */
Marek Vasutb18eabf2011-08-20 09:15:13 +0000761 mode = iop.tPIO;
wdenkc6097192002-11-03 00:24:07 +0000762
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000763 printf("tPIO = 0x%02x = %d\n", mode, mode);
wdenkc6097192002-11-03 00:24:07 +0000764 if (mode > 2) { /* 2 is maximum allowed tPIO value */
765 mode = 2;
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000766 debug("Override tPIO -> 2\n");
wdenkc6097192002-11-03 00:24:07 +0000767 }
Marek Vasutb18eabf2011-08-20 09:15:13 +0000768 if (iop.field_valid & 2) { /* drive implements ATA2? */
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000769 debug("Drive implements ATA2\n");
Marek Vasutb18eabf2011-08-20 09:15:13 +0000770 if (iop.capability & 8) { /* drive supports use_iordy? */
771 cycle_time = iop.eide_pio_iordy;
wdenkc6097192002-11-03 00:24:07 +0000772 } else {
Marek Vasutb18eabf2011-08-20 09:15:13 +0000773 cycle_time = iop.eide_pio;
wdenkc6097192002-11-03 00:24:07 +0000774 }
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000775 debug("cycle time = %d\n", cycle_time);
wdenkc6097192002-11-03 00:24:07 +0000776 mode = 4;
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000777 if (cycle_time > 120)
778 mode = 3; /* 120 ns for PIO mode 4 */
779 if (cycle_time > 180)
780 mode = 2; /* 180 ns for PIO mode 3 */
781 if (cycle_time > 240)
782 mode = 1; /* 240 ns for PIO mode 4 */
783 if (cycle_time > 383)
784 mode = 0; /* 383 ns for PIO mode 4 */
wdenkc6097192002-11-03 00:24:07 +0000785 }
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000786 printf("PIO mode to use: PIO %d\n", mode);
wdenkc6097192002-11-03 00:24:07 +0000787#endif /* 0 */
788
789#ifdef CONFIG_ATAPI
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000790 if (dev_desc->if_type == IF_TYPE_ATAPI) {
wdenkc6097192002-11-03 00:24:07 +0000791 atapi_inquiry(dev_desc);
792 return;
793 }
794#endif /* CONFIG_ATAPI */
795
wdenkc3f9d492004-03-14 00:59:59 +0000796#ifdef __BIG_ENDIAN
wdenkc6097192002-11-03 00:24:07 +0000797 /* swap shorts */
Marek Vasutb18eabf2011-08-20 09:15:13 +0000798 dev_desc->lba = (iop.lba_capacity << 16) | (iop.lba_capacity >> 16);
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000799#else /* ! __BIG_ENDIAN */
wdenkc3f9d492004-03-14 00:59:59 +0000800 /*
801 * do not swap shorts on little endian
802 *
803 * See CF+ and CompactFlash Specification Revision 2.0:
804 * 6.2.1.6: Identfy Drive, Table 39, Word Address 57-58 for details.
805 */
Marek Vasutb18eabf2011-08-20 09:15:13 +0000806 dev_desc->lba = iop.lba_capacity;
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000807#endif /* __BIG_ENDIAN */
wdenkc40b2952004-03-13 23:29:43 +0000808
wdenk42dfe7a2004-03-14 22:25:36 +0000809#ifdef CONFIG_LBA48
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000810 if (iop.command_set_2 & 0x0400) { /* LBA 48 support */
wdenk6e592382004-04-18 17:39:38 +0000811 dev_desc->lba48 = 1;
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000812 dev_desc->lba = (unsigned long long) iop.lba48_capacity[0] |
813 ((unsigned long long) iop.lba48_capacity[1] << 16) |
814 ((unsigned long long) iop.lba48_capacity[2] << 32) |
815 ((unsigned long long) iop.lba48_capacity[3] << 48);
wdenkc40b2952004-03-13 23:29:43 +0000816 } else {
wdenkc40b2952004-03-13 23:29:43 +0000817 dev_desc->lba48 = 0;
818 }
819#endif /* CONFIG_LBA48 */
wdenkc6097192002-11-03 00:24:07 +0000820 /* assuming HD */
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000821 dev_desc->type = DEV_TYPE_HARDDISK;
822 dev_desc->blksz = ATA_BLOCKSIZE;
823 dev_desc->lun = 0; /* just to fill something in... */
wdenkc6097192002-11-03 00:24:07 +0000824
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000825#if 0 /* only used to test the powersaving mode,
826 * if enabled, the drive goes after 5 sec
827 * in standby mode */
828 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
829 c = ide_wait(device, IDE_TIME_OUT);
830 ide_outb(device, ATA_SECT_CNT, 1);
831 ide_outb(device, ATA_LBA_LOW, 0);
832 ide_outb(device, ATA_LBA_MID, 0);
833 ide_outb(device, ATA_LBA_HIGH, 0);
834 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
835 ide_outb(device, ATA_COMMAND, 0xe3);
836 udelay(50);
837 c = ide_wait(device, IDE_TIME_OUT); /* can't take over 500 ms */
wdenkc6097192002-11-03 00:24:07 +0000838#endif
839}
840
841
842/* ------------------------------------------------------------------------- */
843
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000844ulong ide_read(int device, lbaint_t blknr, ulong blkcnt, void *buffer)
wdenkc6097192002-11-03 00:24:07 +0000845{
846 ulong n = 0;
847 unsigned char c;
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000848 unsigned char pwrsave = 0; /* power save */
849
wdenk42dfe7a2004-03-14 22:25:36 +0000850#ifdef CONFIG_LBA48
wdenkc40b2952004-03-13 23:29:43 +0000851 unsigned char lba48 = 0;
wdenkc6097192002-11-03 00:24:07 +0000852
Guennadi Liakhovetski413bf582008-04-28 14:36:06 +0200853 if (blknr & 0x0000fffff0000000ULL) {
wdenkc40b2952004-03-13 23:29:43 +0000854 /* more than 28 bits used, use 48bit mode */
855 lba48 = 1;
856 }
857#endif
Marek Vasut5bbe10d2011-10-25 11:39:15 +0200858 debug("ide_read dev %d start %lX, blocks %lX buffer at %lX\n",
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000859 device, blknr, blkcnt, (ulong) buffer);
wdenkc6097192002-11-03 00:24:07 +0000860
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000861 ide_led(DEVICE_LED(device), 1); /* LED on */
wdenkc6097192002-11-03 00:24:07 +0000862
863 /* Select device
864 */
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000865 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
866 c = ide_wait(device, IDE_TIME_OUT);
wdenkc6097192002-11-03 00:24:07 +0000867
868 if (c & ATA_STAT_BUSY) {
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000869 printf("IDE read: device %d not ready\n", device);
wdenkc6097192002-11-03 00:24:07 +0000870 goto IDE_READ_E;
871 }
872
873 /* first check if the drive is in Powersaving mode, if yes,
874 * increase the timeout value */
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000875 ide_outb(device, ATA_COMMAND, ATA_CMD_CHK_PWR);
876 udelay(50);
wdenkc6097192002-11-03 00:24:07 +0000877
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000878 c = ide_wait(device, IDE_TIME_OUT); /* can't take over 500 ms */
wdenkc6097192002-11-03 00:24:07 +0000879
880 if (c & ATA_STAT_BUSY) {
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000881 printf("IDE read: device %d not ready\n", device);
wdenkc6097192002-11-03 00:24:07 +0000882 goto IDE_READ_E;
883 }
884 if ((c & ATA_STAT_ERR) == ATA_STAT_ERR) {
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000885 printf("No Powersaving mode %X\n", c);
wdenkc6097192002-11-03 00:24:07 +0000886 } else {
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000887 c = ide_inb(device, ATA_SECT_CNT);
888 debug("Powersaving %02X\n", c);
889 if (c == 0)
890 pwrsave = 1;
wdenkc6097192002-11-03 00:24:07 +0000891 }
892
893
894 while (blkcnt-- > 0) {
895
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000896 c = ide_wait(device, IDE_TIME_OUT);
wdenkc6097192002-11-03 00:24:07 +0000897
898 if (c & ATA_STAT_BUSY) {
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000899 printf("IDE read: device %d not ready\n", device);
wdenkc6097192002-11-03 00:24:07 +0000900 break;
901 }
wdenk42dfe7a2004-03-14 22:25:36 +0000902#ifdef CONFIG_LBA48
wdenkc40b2952004-03-13 23:29:43 +0000903 if (lba48) {
904 /* write high bits */
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000905 ide_outb(device, ATA_SECT_CNT, 0);
906 ide_outb(device, ATA_LBA_LOW, (blknr >> 24) & 0xFF);
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200907#ifdef CONFIG_SYS_64BIT_LBA
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000908 ide_outb(device, ATA_LBA_MID, (blknr >> 32) & 0xFF);
909 ide_outb(device, ATA_LBA_HIGH, (blknr >> 40) & 0xFF);
Guennadi Liakhovetski413bf582008-04-28 14:36:06 +0200910#else
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000911 ide_outb(device, ATA_LBA_MID, 0);
912 ide_outb(device, ATA_LBA_HIGH, 0);
Guennadi Liakhovetski413bf582008-04-28 14:36:06 +0200913#endif
wdenkc40b2952004-03-13 23:29:43 +0000914 }
915#endif
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000916 ide_outb(device, ATA_SECT_CNT, 1);
917 ide_outb(device, ATA_LBA_LOW, (blknr >> 0) & 0xFF);
918 ide_outb(device, ATA_LBA_MID, (blknr >> 8) & 0xFF);
919 ide_outb(device, ATA_LBA_HIGH, (blknr >> 16) & 0xFF);
wdenkc40b2952004-03-13 23:29:43 +0000920
wdenk42dfe7a2004-03-14 22:25:36 +0000921#ifdef CONFIG_LBA48
wdenkc40b2952004-03-13 23:29:43 +0000922 if (lba48) {
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000923 ide_outb(device, ATA_DEV_HD,
924 ATA_LBA | ATA_DEVICE(device));
925 ide_outb(device, ATA_COMMAND, ATA_CMD_READ_EXT);
wdenkc40b2952004-03-13 23:29:43 +0000926
927 } else
928#endif
929 {
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000930 ide_outb(device, ATA_DEV_HD, ATA_LBA |
931 ATA_DEVICE(device) | ((blknr >> 24) & 0xF));
932 ide_outb(device, ATA_COMMAND, ATA_CMD_READ);
wdenkc40b2952004-03-13 23:29:43 +0000933 }
wdenkc6097192002-11-03 00:24:07 +0000934
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000935 udelay(50);
wdenkc6097192002-11-03 00:24:07 +0000936
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000937 if (pwrsave) {
938 /* may take up to 4 sec */
939 c = ide_wait(device, IDE_SPIN_UP_TIME_OUT);
940 pwrsave = 0;
wdenkc6097192002-11-03 00:24:07 +0000941 } else {
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000942 /* can't take over 500 ms */
943 c = ide_wait(device, IDE_TIME_OUT);
wdenkc6097192002-11-03 00:24:07 +0000944 }
945
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000946 if ((c & (ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR)) !=
947 ATA_STAT_DRQ) {
Heiko Schocher4b142fe2009-12-03 11:21:21 +0100948#if defined(CONFIG_SYS_64BIT_LBA)
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000949 printf("Error (no IRQ) dev %d blk %lld: status 0x%02x\n",
wdenkc6097192002-11-03 00:24:07 +0000950 device, blknr, c);
wdenkc40b2952004-03-13 23:29:43 +0000951#else
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000952 printf("Error (no IRQ) dev %d blk %ld: status 0x%02x\n",
953 device, (ulong) blknr, c);
wdenkc40b2952004-03-13 23:29:43 +0000954#endif
wdenkc6097192002-11-03 00:24:07 +0000955 break;
956 }
957
Pavel Herrmannf5b82c02012-10-09 07:04:39 +0000958 ide_input_data(device, buffer, ATA_SECTORWORDS);
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000959 (void) ide_inb(device, ATA_STATUS); /* clear IRQ */
wdenkc6097192002-11-03 00:24:07 +0000960
961 ++n;
962 ++blknr;
Greg Lopp0b945042007-04-13 08:02:24 +0200963 buffer += ATA_BLOCKSIZE;
wdenkc6097192002-11-03 00:24:07 +0000964 }
965IDE_READ_E:
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000966 ide_led(DEVICE_LED(device), 0); /* LED off */
wdenkc6097192002-11-03 00:24:07 +0000967 return (n);
968}
969
970/* ------------------------------------------------------------------------- */
971
972
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000973ulong ide_write(int device, lbaint_t blknr, ulong blkcnt, const void *buffer)
wdenkc6097192002-11-03 00:24:07 +0000974{
975 ulong n = 0;
976 unsigned char c;
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000977
wdenk42dfe7a2004-03-14 22:25:36 +0000978#ifdef CONFIG_LBA48
wdenkc40b2952004-03-13 23:29:43 +0000979 unsigned char lba48 = 0;
980
Guennadi Liakhovetski413bf582008-04-28 14:36:06 +0200981 if (blknr & 0x0000fffff0000000ULL) {
wdenkc40b2952004-03-13 23:29:43 +0000982 /* more than 28 bits used, use 48bit mode */
983 lba48 = 1;
984 }
985#endif
wdenkc6097192002-11-03 00:24:07 +0000986
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000987 ide_led(DEVICE_LED(device), 1); /* LED on */
wdenkc6097192002-11-03 00:24:07 +0000988
989 /* Select device
990 */
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000991 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
wdenkc6097192002-11-03 00:24:07 +0000992
993 while (blkcnt-- > 0) {
994
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000995 c = ide_wait(device, IDE_TIME_OUT);
wdenkc6097192002-11-03 00:24:07 +0000996
997 if (c & ATA_STAT_BUSY) {
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000998 printf("IDE read: device %d not ready\n", device);
wdenkc6097192002-11-03 00:24:07 +0000999 goto WR_OUT;
1000 }
wdenk42dfe7a2004-03-14 22:25:36 +00001001#ifdef CONFIG_LBA48
wdenkc40b2952004-03-13 23:29:43 +00001002 if (lba48) {
1003 /* write high bits */
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001004 ide_outb(device, ATA_SECT_CNT, 0);
1005 ide_outb(device, ATA_LBA_LOW, (blknr >> 24) & 0xFF);
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001006#ifdef CONFIG_SYS_64BIT_LBA
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001007 ide_outb(device, ATA_LBA_MID, (blknr >> 32) & 0xFF);
1008 ide_outb(device, ATA_LBA_HIGH, (blknr >> 40) & 0xFF);
Guennadi Liakhovetski413bf582008-04-28 14:36:06 +02001009#else
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001010 ide_outb(device, ATA_LBA_MID, 0);
1011 ide_outb(device, ATA_LBA_HIGH, 0);
Guennadi Liakhovetski413bf582008-04-28 14:36:06 +02001012#endif
wdenkc40b2952004-03-13 23:29:43 +00001013 }
1014#endif
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001015 ide_outb(device, ATA_SECT_CNT, 1);
1016 ide_outb(device, ATA_LBA_LOW, (blknr >> 0) & 0xFF);
1017 ide_outb(device, ATA_LBA_MID, (blknr >> 8) & 0xFF);
1018 ide_outb(device, ATA_LBA_HIGH, (blknr >> 16) & 0xFF);
wdenkc40b2952004-03-13 23:29:43 +00001019
wdenk42dfe7a2004-03-14 22:25:36 +00001020#ifdef CONFIG_LBA48
wdenkc40b2952004-03-13 23:29:43 +00001021 if (lba48) {
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001022 ide_outb(device, ATA_DEV_HD,
1023 ATA_LBA | ATA_DEVICE(device));
1024 ide_outb(device, ATA_COMMAND, ATA_CMD_WRITE_EXT);
wdenkc40b2952004-03-13 23:29:43 +00001025
1026 } else
1027#endif
1028 {
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001029 ide_outb(device, ATA_DEV_HD, ATA_LBA |
1030 ATA_DEVICE(device) | ((blknr >> 24) & 0xF));
1031 ide_outb(device, ATA_COMMAND, ATA_CMD_WRITE);
wdenkc40b2952004-03-13 23:29:43 +00001032 }
wdenkc6097192002-11-03 00:24:07 +00001033
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001034 udelay(50);
wdenkc6097192002-11-03 00:24:07 +00001035
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001036 /* can't take over 500 ms */
1037 c = ide_wait(device, IDE_TIME_OUT);
wdenkc6097192002-11-03 00:24:07 +00001038
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001039 if ((c & (ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR)) !=
1040 ATA_STAT_DRQ) {
Heiko Schocher4b142fe2009-12-03 11:21:21 +01001041#if defined(CONFIG_SYS_64BIT_LBA)
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001042 printf("Error (no IRQ) dev %d blk %lld: status 0x%02x\n",
wdenkc6097192002-11-03 00:24:07 +00001043 device, blknr, c);
wdenkc40b2952004-03-13 23:29:43 +00001044#else
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001045 printf("Error (no IRQ) dev %d blk %ld: status 0x%02x\n",
1046 device, (ulong) blknr, c);
wdenkc40b2952004-03-13 23:29:43 +00001047#endif
wdenkc6097192002-11-03 00:24:07 +00001048 goto WR_OUT;
1049 }
1050
Pavel Herrmannf5b82c02012-10-09 07:04:39 +00001051 ide_output_data(device, buffer, ATA_SECTORWORDS);
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001052 c = ide_inb(device, ATA_STATUS); /* clear IRQ */
wdenkc6097192002-11-03 00:24:07 +00001053 ++n;
1054 ++blknr;
Greg Lopp0b945042007-04-13 08:02:24 +02001055 buffer += ATA_BLOCKSIZE;
wdenkc6097192002-11-03 00:24:07 +00001056 }
1057WR_OUT:
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001058 ide_led(DEVICE_LED(device), 0); /* LED off */
wdenkc6097192002-11-03 00:24:07 +00001059 return (n);
1060}
1061
1062/* ------------------------------------------------------------------------- */
1063
1064/*
1065 * copy src to dest, skipping leading and trailing blanks and null
1066 * terminate the string
wdenk7d7ce412004-03-17 01:13:07 +00001067 * "len" is the size of available memory including the terminating '\0'
wdenkc6097192002-11-03 00:24:07 +00001068 */
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001069static void ident_cpy(unsigned char *dst, unsigned char *src,
1070 unsigned int len)
wdenkc6097192002-11-03 00:24:07 +00001071{
wdenk7d7ce412004-03-17 01:13:07 +00001072 unsigned char *end, *last;
wdenkc6097192002-11-03 00:24:07 +00001073
wdenk7d7ce412004-03-17 01:13:07 +00001074 last = dst;
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001075 end = src + len - 1;
wdenk7d7ce412004-03-17 01:13:07 +00001076
1077 /* reserve space for '\0' */
1078 if (len < 2)
1079 goto OUT;
wdenkefa329c2004-03-23 20:18:25 +00001080
wdenk7d7ce412004-03-17 01:13:07 +00001081 /* skip leading white space */
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001082 while ((*src) && (src < end) && (*src == ' '))
wdenk7d7ce412004-03-17 01:13:07 +00001083 ++src;
1084
1085 /* copy string, omitting trailing white space */
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001086 while ((*src) && (src < end)) {
wdenk7d7ce412004-03-17 01:13:07 +00001087 *dst++ = *src;
1088 if (*src++ != ' ')
1089 last = dst;
wdenkc6097192002-11-03 00:24:07 +00001090 }
wdenk7d7ce412004-03-17 01:13:07 +00001091OUT:
1092 *last = '\0';
wdenkc6097192002-11-03 00:24:07 +00001093}
1094
1095/* ------------------------------------------------------------------------- */
1096
1097/*
1098 * Wait until Busy bit is off, or timeout (in ms)
1099 * Return last status
1100 */
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001101static uchar ide_wait(int dev, ulong t)
wdenkc6097192002-11-03 00:24:07 +00001102{
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001103 ulong delay = 10 * t; /* poll every 100 us */
wdenkc6097192002-11-03 00:24:07 +00001104 uchar c;
1105
wdenk2262cfe2002-11-18 00:14:45 +00001106 while ((c = ide_inb(dev, ATA_STATUS)) & ATA_STAT_BUSY) {
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001107 udelay(100);
1108 if (delay-- == 0)
wdenkc6097192002-11-03 00:24:07 +00001109 break;
wdenkc6097192002-11-03 00:24:07 +00001110 }
1111 return (c);
1112}
1113
1114/* ------------------------------------------------------------------------- */
1115
1116#ifdef CONFIG_IDE_RESET
1117extern void ide_set_reset(int idereset);
1118
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001119static void ide_reset(void)
wdenkc6097192002-11-03 00:24:07 +00001120{
wdenkc6097192002-11-03 00:24:07 +00001121 int i;
1122
1123 curr_device = -1;
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001124 for (i = 0; i < CONFIG_SYS_IDE_MAXBUS; ++i)
wdenkc6097192002-11-03 00:24:07 +00001125 ide_bus_ok[i] = 0;
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001126 for (i = 0; i < CONFIG_SYS_IDE_MAXDEVICE; ++i)
wdenkc6097192002-11-03 00:24:07 +00001127 ide_dev_desc[i].type = DEV_TYPE_UNKNOWN;
1128
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001129 ide_set_reset(1); /* assert reset */
wdenkc6097192002-11-03 00:24:07 +00001130
Martin Krausee175eac2008-04-03 13:37:56 +02001131 /* the reset signal shall be asserted for et least 25 us */
1132 udelay(25);
1133
wdenkc6097192002-11-03 00:24:07 +00001134 WATCHDOG_RESET();
1135
wdenkc6097192002-11-03 00:24:07 +00001136 /* de-assert RESET signal */
1137 ide_set_reset(0);
1138
1139 /* wait 250 ms */
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001140 for (i = 0; i < 250; ++i)
1141 udelay(1000);
wdenkc6097192002-11-03 00:24:07 +00001142}
1143
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001144#endif /* CONFIG_IDE_RESET */
wdenkc6097192002-11-03 00:24:07 +00001145
1146/* ------------------------------------------------------------------------- */
1147
wdenke2ffd592004-12-31 09:32:47 +00001148#if defined(CONFIG_IDE_LED) && \
wdenke2ffd592004-12-31 09:32:47 +00001149 !defined(CONFIG_CPC45) && \
wdenke2ffd592004-12-31 09:32:47 +00001150 !defined(CONFIG_KUP4K) && \
1151 !defined(CONFIG_KUP4X)
wdenkc6097192002-11-03 00:24:07 +00001152
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001153static uchar led_buffer; /* Buffer for current LED status */
wdenkc6097192002-11-03 00:24:07 +00001154
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001155static void ide_led(uchar led, uchar status)
wdenkc6097192002-11-03 00:24:07 +00001156{
1157 uchar *led_port = LED_PORT;
1158
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001159 if (status) /* switch LED on */
1160 led_buffer |= led;
1161 else /* switch LED off */
wdenkc6097192002-11-03 00:24:07 +00001162 led_buffer &= ~led;
wdenkc6097192002-11-03 00:24:07 +00001163
1164 *led_port = led_buffer;
1165}
1166
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001167#endif /* CONFIG_IDE_LED */
wdenkc6097192002-11-03 00:24:07 +00001168
Heiko Schocher3887c3f2009-09-23 07:56:08 +02001169#if defined(CONFIG_OF_IDE_FIXUP)
1170int ide_device_present(int dev)
1171{
1172 if (dev >= CONFIG_SYS_IDE_MAXBUS)
1173 return 0;
1174 return (ide_dev_desc[dev].type == DEV_TYPE_UNKNOWN ? 0 : 1);
1175}
1176#endif
wdenkc6097192002-11-03 00:24:07 +00001177/* ------------------------------------------------------------------------- */
1178
1179#ifdef CONFIG_ATAPI
1180/****************************************************************************
1181 * ATAPI Support
1182 */
1183
Pavel Herrmannf5b82c02012-10-09 07:04:39 +00001184void ide_input_data_shorts(int dev, ushort *sect_buf, int shorts)
1185 __attribute__ ((weak, alias("__ide_input_data_shorts")));
1186
1187void ide_output_data_shorts(int dev, ushort *sect_buf, int shorts)
1188 __attribute__ ((weak, alias("__ide_output_data_shorts")));
1189
1190
Albert Aribaudf2a37fc2010-08-08 05:17:05 +05301191#if defined(CONFIG_IDE_SWAP_IO)
wdenkc6097192002-11-03 00:24:07 +00001192/* since ATAPI may use commands with not 4 bytes alligned length
1193 * we have our own transfer functions, 2 bytes alligned */
Pavel Herrmannf5b82c02012-10-09 07:04:39 +00001194void __ide_output_data_shorts(int dev, ushort *sect_buf, int shorts)
wdenkc6097192002-11-03 00:24:07 +00001195{
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001196 ushort *dbuf;
1197 volatile ushort *pbuf;
wdenkc6097192002-11-03 00:24:07 +00001198
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001199 pbuf = (ushort *) (ATA_CURR_BASE(dev) + ATA_DATA_REG);
1200 dbuf = (ushort *) sect_buf;
wdenkdb01a2e2004-04-15 23:14:49 +00001201
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001202 debug("in output data shorts base for read is %lx\n",
1203 (unsigned long) pbuf);
wdenkdb01a2e2004-04-15 23:14:49 +00001204
wdenkc6097192002-11-03 00:24:07 +00001205 while (shorts--) {
wdenk5cf91d62004-04-23 20:32:05 +00001206 EIEIO;
wdenk1a344f22005-02-03 23:00:49 +00001207 *pbuf = *dbuf++;
wdenkc6097192002-11-03 00:24:07 +00001208 }
wdenk1a344f22005-02-03 23:00:49 +00001209}
1210
Pavel Herrmannf5b82c02012-10-09 07:04:39 +00001211void __ide_input_data_shorts(int dev, ushort *sect_buf, int shorts)
wdenk1a344f22005-02-03 23:00:49 +00001212{
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001213 ushort *dbuf;
1214 volatile ushort *pbuf;
wdenk1a344f22005-02-03 23:00:49 +00001215
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001216 pbuf = (ushort *) (ATA_CURR_BASE(dev) + ATA_DATA_REG);
1217 dbuf = (ushort *) sect_buf;
wdenk1a344f22005-02-03 23:00:49 +00001218
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001219 debug("in input data shorts base for read is %lx\n",
1220 (unsigned long) pbuf);
wdenk1a344f22005-02-03 23:00:49 +00001221
1222 while (shorts--) {
1223 EIEIO;
1224 *dbuf++ = *pbuf;
1225 }
wdenkc6097192002-11-03 00:24:07 +00001226}
1227
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001228#else /* ! CONFIG_IDE_SWAP_IO */
Pavel Herrmannf5b82c02012-10-09 07:04:39 +00001229void __ide_output_data_shorts(int dev, ushort *sect_buf, int shorts)
wdenk2262cfe2002-11-18 00:14:45 +00001230{
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001231 outsw(ATA_CURR_BASE(dev) + ATA_DATA_REG, sect_buf, shorts);
wdenk2262cfe2002-11-18 00:14:45 +00001232}
1233
Pavel Herrmannf5b82c02012-10-09 07:04:39 +00001234void __ide_input_data_shorts(int dev, ushort *sect_buf, int shorts)
wdenk2262cfe2002-11-18 00:14:45 +00001235{
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001236 insw(ATA_CURR_BASE(dev) + ATA_DATA_REG, sect_buf, shorts);
wdenk2262cfe2002-11-18 00:14:45 +00001237}
1238
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001239#endif /* CONFIG_IDE_SWAP_IO */
wdenk2262cfe2002-11-18 00:14:45 +00001240
wdenkc6097192002-11-03 00:24:07 +00001241/*
1242 * Wait until (Status & mask) == res, or timeout (in ms)
1243 * Return last status
1244 * This is used since some ATAPI CD ROMs clears their Busy Bit first
1245 * and then they set their DRQ Bit
1246 */
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001247static uchar atapi_wait_mask(int dev, ulong t, uchar mask, uchar res)
wdenkc6097192002-11-03 00:24:07 +00001248{
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001249 ulong delay = 10 * t; /* poll every 100 us */
wdenkc6097192002-11-03 00:24:07 +00001250 uchar c;
1251
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001252 /* prevents to read the status before valid */
1253 c = ide_inb(dev, ATA_DEV_CTL);
1254
wdenk2262cfe2002-11-18 00:14:45 +00001255 while (((c = ide_inb(dev, ATA_STATUS)) & mask) != res) {
wdenkc6097192002-11-03 00:24:07 +00001256 /* break if error occurs (doesn't make sense to wait more) */
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001257 if ((c & ATA_STAT_ERR) == ATA_STAT_ERR)
wdenkc6097192002-11-03 00:24:07 +00001258 break;
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001259 udelay(100);
1260 if (delay-- == 0)
wdenkc6097192002-11-03 00:24:07 +00001261 break;
wdenkc6097192002-11-03 00:24:07 +00001262 }
1263 return (c);
1264}
1265
1266/*
1267 * issue an atapi command
1268 */
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001269unsigned char atapi_issue(int device, unsigned char *ccb, int ccblen,
1270 unsigned char *buffer, int buflen)
wdenkc6097192002-11-03 00:24:07 +00001271{
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001272 unsigned char c, err, mask, res;
wdenkc6097192002-11-03 00:24:07 +00001273 int n;
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001274
1275 ide_led(DEVICE_LED(device), 1); /* LED on */
wdenkc6097192002-11-03 00:24:07 +00001276
1277 /* Select device
1278 */
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001279 mask = ATA_STAT_BUSY | ATA_STAT_DRQ;
wdenkc6097192002-11-03 00:24:07 +00001280 res = 0;
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001281 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
1282 c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
wdenkc6097192002-11-03 00:24:07 +00001283 if ((c & mask) != res) {
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001284 printf("ATAPI_ISSUE: device %d not ready status %X\n", device,
1285 c);
1286 err = 0xFF;
wdenkc6097192002-11-03 00:24:07 +00001287 goto AI_OUT;
1288 }
1289 /* write taskfile */
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001290 ide_outb(device, ATA_ERROR_REG, 0); /* no DMA, no overlaped */
1291 ide_outb(device, ATA_SECT_CNT, 0);
1292 ide_outb(device, ATA_SECT_NUM, 0);
1293 ide_outb(device, ATA_CYL_LOW, (unsigned char) (buflen & 0xFF));
1294 ide_outb(device, ATA_CYL_HIGH,
1295 (unsigned char) ((buflen >> 8) & 0xFF));
1296 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
wdenkc6097192002-11-03 00:24:07 +00001297
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001298 ide_outb(device, ATA_COMMAND, ATAPI_CMD_PACKET);
1299 udelay(50);
wdenkc6097192002-11-03 00:24:07 +00001300
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001301 mask = ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR;
wdenkc6097192002-11-03 00:24:07 +00001302 res = ATA_STAT_DRQ;
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001303 c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
wdenkc6097192002-11-03 00:24:07 +00001304
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001305 if ((c & mask) != res) { /* DRQ must be 1, BSY 0 */
1306 printf("ATAPI_ISSUE: Error (no IRQ) before sending ccb dev %d status 0x%02x\n",
1307 device, c);
1308 err = 0xFF;
wdenkc6097192002-11-03 00:24:07 +00001309 goto AI_OUT;
1310 }
1311
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001312 /* write command block */
Pavel Herrmannf5b82c02012-10-09 07:04:39 +00001313 ide_output_data_shorts(device, (unsigned short *) ccb, ccblen / 2);
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001314
Wolfgang Denk53677ef2008-05-20 16:00:29 +02001315 /* ATAPI Command written wait for completition */
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001316 udelay(5000); /* device must set bsy */
wdenkc6097192002-11-03 00:24:07 +00001317
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001318 mask = ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR;
1319 /*
1320 * if no data wait for DRQ = 0 BSY = 0
1321 * if data wait for DRQ = 1 BSY = 0
1322 */
1323 res = 0;
1324 if (buflen)
wdenkc6097192002-11-03 00:24:07 +00001325 res = ATA_STAT_DRQ;
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001326 c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
1327 if ((c & mask) != res) {
wdenkc6097192002-11-03 00:24:07 +00001328 if (c & ATA_STAT_ERR) {
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001329 err = (ide_inb(device, ATA_ERROR_REG)) >> 4;
1330 debug("atapi_issue 1 returned sense key %X status %02X\n",
1331 err, c);
wdenkc6097192002-11-03 00:24:07 +00001332 } else {
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001333 printf("ATAPI_ISSUE: (no DRQ) after sending ccb (%x) status 0x%02x\n",
1334 ccb[0], c);
1335 err = 0xFF;
wdenkc6097192002-11-03 00:24:07 +00001336 }
1337 goto AI_OUT;
1338 }
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001339 n = ide_inb(device, ATA_CYL_HIGH);
1340 n <<= 8;
1341 n += ide_inb(device, ATA_CYL_LOW);
1342 if (n > buflen) {
1343 printf("ERROR, transfer bytes %d requested only %d\n", n,
1344 buflen);
1345 err = 0xff;
wdenkc6097192002-11-03 00:24:07 +00001346 goto AI_OUT;
1347 }
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001348 if ((n == 0) && (buflen < 0)) {
1349 printf("ERROR, transfer bytes %d requested %d\n", n, buflen);
1350 err = 0xff;
wdenkc6097192002-11-03 00:24:07 +00001351 goto AI_OUT;
1352 }
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001353 if (n != buflen) {
1354 debug("WARNING, transfer bytes %d not equal with requested %d\n",
1355 n, buflen);
wdenkc6097192002-11-03 00:24:07 +00001356 }
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001357 if (n != 0) { /* data transfer */
1358 debug("ATAPI_ISSUE: %d Bytes to transfer\n", n);
1359 /* we transfer shorts */
1360 n >>= 1;
wdenkc6097192002-11-03 00:24:07 +00001361 /* ok now decide if it is an in or output */
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001362 if ((ide_inb(device, ATA_SECT_CNT) & 0x02) == 0) {
1363 debug("Write to device\n");
Pavel Herrmannf5b82c02012-10-09 07:04:39 +00001364 ide_output_data_shorts(device,
1365 (unsigned short *) buffer, n);
wdenkc6097192002-11-03 00:24:07 +00001366 } else {
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001367 debug("Read from device @ %p shorts %d\n", buffer, n);
Pavel Herrmannf5b82c02012-10-09 07:04:39 +00001368 ide_input_data_shorts(device,
1369 (unsigned short *) buffer, n);
wdenkc6097192002-11-03 00:24:07 +00001370 }
1371 }
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001372 udelay(5000); /* seems that some CD ROMs need this... */
1373 mask = ATA_STAT_BUSY | ATA_STAT_ERR;
1374 res = 0;
1375 c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
wdenkc6097192002-11-03 00:24:07 +00001376 if ((c & ATA_STAT_ERR) == ATA_STAT_ERR) {
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001377 err = (ide_inb(device, ATA_ERROR_REG) >> 4);
1378 debug("atapi_issue 2 returned sense key %X status %X\n", err,
1379 c);
wdenkc6097192002-11-03 00:24:07 +00001380 } else {
1381 err = 0;
1382 }
1383AI_OUT:
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001384 ide_led(DEVICE_LED(device), 0); /* LED off */
wdenkc6097192002-11-03 00:24:07 +00001385 return (err);
1386}
1387
1388/*
1389 * sending the command to atapi_issue. If an status other than good
1390 * returns, an request_sense will be issued
1391 */
1392
Wolfgang Denk53677ef2008-05-20 16:00:29 +02001393#define ATAPI_DRIVE_NOT_READY 100
wdenkc6097192002-11-03 00:24:07 +00001394#define ATAPI_UNIT_ATTN 10
1395
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001396unsigned char atapi_issue_autoreq(int device,
1397 unsigned char *ccb,
1398 int ccblen,
1399 unsigned char *buffer, int buflen)
wdenkc6097192002-11-03 00:24:07 +00001400{
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001401 unsigned char sense_data[18], sense_ccb[12];
1402 unsigned char res, key, asc, ascq;
1403 int notready, unitattn;
wdenkc6097192002-11-03 00:24:07 +00001404
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001405 unitattn = ATAPI_UNIT_ATTN;
1406 notready = ATAPI_DRIVE_NOT_READY;
wdenkc6097192002-11-03 00:24:07 +00001407
1408retry:
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001409 res = atapi_issue(device, ccb, ccblen, buffer, buflen);
1410 if (res == 0)
1411 return 0; /* Ok */
wdenkc6097192002-11-03 00:24:07 +00001412
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001413 if (res == 0xFF)
1414 return 0xFF; /* error */
wdenkc6097192002-11-03 00:24:07 +00001415
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001416 debug("(auto_req)atapi_issue returned sense key %X\n", res);
wdenkc6097192002-11-03 00:24:07 +00001417
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001418 memset(sense_ccb, 0, sizeof(sense_ccb));
1419 memset(sense_data, 0, sizeof(sense_data));
1420 sense_ccb[0] = ATAPI_CMD_REQ_SENSE;
1421 sense_ccb[4] = 18; /* allocation Length */
wdenkc6097192002-11-03 00:24:07 +00001422
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001423 res = atapi_issue(device, sense_ccb, 12, sense_data, 18);
1424 key = (sense_data[2] & 0xF);
1425 asc = (sense_data[12]);
1426 ascq = (sense_data[13]);
wdenkc6097192002-11-03 00:24:07 +00001427
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001428 debug("ATAPI_CMD_REQ_SENSE returned %x\n", res);
1429 debug(" Sense page: %02X key %02X ASC %02X ASCQ %02X\n",
1430 sense_data[0], key, asc, ascq);
wdenkc6097192002-11-03 00:24:07 +00001431
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001432 if ((key == 0))
1433 return 0; /* ok device ready */
wdenkc6097192002-11-03 00:24:07 +00001434
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001435 if ((key == 6) || (asc == 0x29) || (asc == 0x28)) { /* Unit Attention */
1436 if (unitattn-- > 0) {
1437 udelay(200 * 1000);
wdenkc6097192002-11-03 00:24:07 +00001438 goto retry;
1439 }
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001440 printf("Unit Attention, tried %d\n", ATAPI_UNIT_ATTN);
wdenkc6097192002-11-03 00:24:07 +00001441 goto error;
1442 }
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001443 if ((asc == 0x4) && (ascq == 0x1)) {
1444 /* not ready, but will be ready soon */
1445 if (notready-- > 0) {
1446 udelay(200 * 1000);
wdenkc6097192002-11-03 00:24:07 +00001447 goto retry;
1448 }
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001449 printf("Drive not ready, tried %d times\n",
1450 ATAPI_DRIVE_NOT_READY);
wdenkc6097192002-11-03 00:24:07 +00001451 goto error;
1452 }
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001453 if (asc == 0x3a) {
1454 debug("Media not present\n");
wdenkc6097192002-11-03 00:24:07 +00001455 goto error;
1456 }
wdenkc7de8292002-11-19 11:04:11 +00001457
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001458 printf("ERROR: Unknown Sense key %02X ASC %02X ASCQ %02X\n", key, asc,
1459 ascq);
wdenkc6097192002-11-03 00:24:07 +00001460error:
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001461 debug("ERROR Sense key %02X ASC %02X ASCQ %02X\n", key, asc, ascq);
wdenkc6097192002-11-03 00:24:07 +00001462 return (0xFF);
1463}
1464
1465
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001466static void atapi_inquiry(block_dev_desc_t *dev_desc)
wdenkc6097192002-11-03 00:24:07 +00001467{
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001468 unsigned char ccb[12]; /* Command descriptor block */
1469 unsigned char iobuf[64]; /* temp buf */
wdenkc6097192002-11-03 00:24:07 +00001470 unsigned char c;
1471 int device;
1472
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001473 device = dev_desc->dev;
1474 dev_desc->type = DEV_TYPE_UNKNOWN; /* not yet valid */
1475 dev_desc->block_read = atapi_read;
wdenkc6097192002-11-03 00:24:07 +00001476
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001477 memset(ccb, 0, sizeof(ccb));
1478 memset(iobuf, 0, sizeof(iobuf));
wdenkc6097192002-11-03 00:24:07 +00001479
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001480 ccb[0] = ATAPI_CMD_INQUIRY;
1481 ccb[4] = 40; /* allocation Legnth */
1482 c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *) iobuf, 40);
wdenkc6097192002-11-03 00:24:07 +00001483
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001484 debug("ATAPI_CMD_INQUIRY returned %x\n", c);
1485 if (c != 0)
wdenkc6097192002-11-03 00:24:07 +00001486 return;
1487
1488 /* copy device ident strings */
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001489 ident_cpy((unsigned char *) dev_desc->vendor, &iobuf[8], 8);
1490 ident_cpy((unsigned char *) dev_desc->product, &iobuf[16], 16);
1491 ident_cpy((unsigned char *) dev_desc->revision, &iobuf[32], 5);
wdenkc6097192002-11-03 00:24:07 +00001492
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001493 dev_desc->lun = 0;
1494 dev_desc->lba = 0;
1495 dev_desc->blksz = 0;
1496 dev_desc->type = iobuf[0] & 0x1f;
wdenkc6097192002-11-03 00:24:07 +00001497
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001498 if ((iobuf[1] & 0x80) == 0x80)
wdenkc6097192002-11-03 00:24:07 +00001499 dev_desc->removable = 1;
1500 else
1501 dev_desc->removable = 0;
1502
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001503 memset(ccb, 0, sizeof(ccb));
1504 memset(iobuf, 0, sizeof(iobuf));
1505 ccb[0] = ATAPI_CMD_START_STOP;
1506 ccb[4] = 0x03; /* start */
wdenkc6097192002-11-03 00:24:07 +00001507
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001508 c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *) iobuf, 0);
wdenkc6097192002-11-03 00:24:07 +00001509
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001510 debug("ATAPI_CMD_START_STOP returned %x\n", c);
1511 if (c != 0)
wdenkc6097192002-11-03 00:24:07 +00001512 return;
1513
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001514 memset(ccb, 0, sizeof(ccb));
1515 memset(iobuf, 0, sizeof(iobuf));
1516 c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *) iobuf, 0);
wdenkc6097192002-11-03 00:24:07 +00001517
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001518 debug("ATAPI_CMD_UNIT_TEST_READY returned %x\n", c);
1519 if (c != 0)
wdenkc6097192002-11-03 00:24:07 +00001520 return;
1521
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001522 memset(ccb, 0, sizeof(ccb));
1523 memset(iobuf, 0, sizeof(iobuf));
1524 ccb[0] = ATAPI_CMD_READ_CAP;
1525 c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *) iobuf, 8);
1526 debug("ATAPI_CMD_READ_CAP returned %x\n", c);
1527 if (c != 0)
wdenkc6097192002-11-03 00:24:07 +00001528 return;
1529
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001530 debug("Read Cap: LBA %02X%02X%02X%02X blksize %02X%02X%02X%02X\n",
1531 iobuf[0], iobuf[1], iobuf[2], iobuf[3],
1532 iobuf[4], iobuf[5], iobuf[6], iobuf[7]);
wdenkc6097192002-11-03 00:24:07 +00001533
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001534 dev_desc->lba = ((unsigned long) iobuf[0] << 24) +
1535 ((unsigned long) iobuf[1] << 16) +
1536 ((unsigned long) iobuf[2] << 8) + ((unsigned long) iobuf[3]);
1537 dev_desc->blksz = ((unsigned long) iobuf[4] << 24) +
1538 ((unsigned long) iobuf[5] << 16) +
1539 ((unsigned long) iobuf[6] << 8) + ((unsigned long) iobuf[7]);
wdenk42dfe7a2004-03-14 22:25:36 +00001540#ifdef CONFIG_LBA48
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001541 /* ATAPI devices cannot use 48bit addressing (ATA/ATAPI v7) */
1542 dev_desc->lba48 = 0;
wdenk42dfe7a2004-03-14 22:25:36 +00001543#endif
wdenkc6097192002-11-03 00:24:07 +00001544 return;
1545}
1546
1547
1548/*
1549 * atapi_read:
1550 * we transfer only one block per command, since the multiple DRQ per
1551 * command is not yet implemented
1552 */
1553#define ATAPI_READ_MAX_BYTES 2048 /* we read max 2kbytes */
1554#define ATAPI_READ_BLOCK_SIZE 2048 /* assuming CD part */
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001555#define ATAPI_READ_MAX_BLOCK (ATAPI_READ_MAX_BYTES/ATAPI_READ_BLOCK_SIZE)
wdenkc6097192002-11-03 00:24:07 +00001556
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001557ulong atapi_read(int device, lbaint_t blknr, ulong blkcnt, void *buffer)
wdenkc6097192002-11-03 00:24:07 +00001558{
1559 ulong n = 0;
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001560 unsigned char ccb[12]; /* Command descriptor block */
wdenkc6097192002-11-03 00:24:07 +00001561 ulong cnt;
1562
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001563 debug("atapi_read dev %d start %lX, blocks %lX buffer at %lX\n",
1564 device, blknr, blkcnt, (ulong) buffer);
wdenkc6097192002-11-03 00:24:07 +00001565
1566 do {
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001567 if (blkcnt > ATAPI_READ_MAX_BLOCK)
1568 cnt = ATAPI_READ_MAX_BLOCK;
1569 else
1570 cnt = blkcnt;
wdenkc6097192002-11-03 00:24:07 +00001571
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001572 ccb[0] = ATAPI_CMD_READ_12;
1573 ccb[1] = 0; /* reserved */
1574 ccb[2] = (unsigned char) (blknr >> 24) & 0xFF; /* MSB Block */
1575 ccb[3] = (unsigned char) (blknr >> 16) & 0xFF; /* */
1576 ccb[4] = (unsigned char) (blknr >> 8) & 0xFF;
1577 ccb[5] = (unsigned char) blknr & 0xFF; /* LSB Block */
1578 ccb[6] = (unsigned char) (cnt >> 24) & 0xFF; /* MSB Block cnt */
1579 ccb[7] = (unsigned char) (cnt >> 16) & 0xFF;
1580 ccb[8] = (unsigned char) (cnt >> 8) & 0xFF;
1581 ccb[9] = (unsigned char) cnt & 0xFF; /* LSB Block */
1582 ccb[10] = 0; /* reserved */
1583 ccb[11] = 0; /* reserved */
1584
1585 if (atapi_issue_autoreq(device, ccb, 12,
1586 (unsigned char *) buffer,
1587 cnt * ATAPI_READ_BLOCK_SIZE)
1588 == 0xFF) {
wdenkc6097192002-11-03 00:24:07 +00001589 return (n);
1590 }
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001591 n += cnt;
1592 blkcnt -= cnt;
1593 blknr += cnt;
1594 buffer += (cnt * ATAPI_READ_BLOCK_SIZE);
wdenkc6097192002-11-03 00:24:07 +00001595 } while (blkcnt > 0);
1596 return (n);
1597}
1598
1599/* ------------------------------------------------------------------------- */
1600
1601#endif /* CONFIG_ATAPI */
1602
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001603U_BOOT_CMD(ide, 5, 1, do_ide,
1604 "IDE sub-system",
1605 "reset - reset IDE controller\n"
1606 "ide info - show available IDE devices\n"
1607 "ide device [dev] - show or set current device\n"
1608 "ide part [dev] - print partition table of one or all IDE devices\n"
1609 "ide read addr blk# cnt\n"
1610 "ide write addr blk# cnt - read/write `cnt'"
1611 " blocks starting at block `blk#'\n"
1612 " to/from memory address `addr'");
wdenk8bde7f72003-06-27 21:31:46 +00001613
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001614U_BOOT_CMD(diskboot, 3, 1, do_diskboot,
1615 "boot from IDE device", "loadAddr dev:part");