blob: d508e9f75b40cd6c90b0c267f244324bba7480b8 [file] [log] [blame]
wdenkc6097192002-11-03 00:24:07 +00001/*
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00002 * (C) Copyright 2000-2011
wdenkc6097192002-11-03 00:24:07 +00003 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4 *
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 Aribaud45027552010-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 Schocher2559e0f2007-08-28 17:39:14 +020035#include <asm/io.h>
Grant Likelyffc2dd72007-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 Likelyffc2dd72007-02-20 09:04:34 +010040
wdenkc6097192002-11-03 00:24:07 +000041#include <ide.h>
42#include <ata.h>
Grant Likelyffc2dd72007-02-20 09:04:34 +010043
wdenkc6097192002-11-03 00:24:07 +000044#ifdef CONFIG_STATUS_LED
45# include <status_led.h>
46#endif
Grant Likelyffc2dd72007-02-20 09:04:34 +010047
wdenk20c98a62004-04-23 20:32:05 +000048#ifdef __PPC__
49# define EIEIO __asm__ volatile ("eieio")
wdenkf1276d42005-02-03 23:00:49 +000050# define SYNC __asm__ volatile ("sync")
wdenk20c98a62004-04-23 20:32:05 +000051#else
52# define EIEIO /* nothing */
wdenkf1276d42005-02-03 23:00:49 +000053# define SYNC /* nothing */
wdenk20c98a62004-04-23 20:32:05 +000054#endif
wdenkc6097192002-11-03 00:24:07 +000055
wdenkc6097192002-11-03 00:24:07 +000056/* ------------------------------------------------------------------------- */
57
58/* Current I/O Device */
59static int curr_device = -1;
60
61/* Current offset for IDE0 / IDE1 bus access */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020062ulong ide_bus_offset[CONFIG_SYS_IDE_MAXBUS] = {
63#if defined(CONFIG_SYS_ATA_IDE0_OFFSET)
64 CONFIG_SYS_ATA_IDE0_OFFSET,
wdenkc6097192002-11-03 00:24:07 +000065#endif
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020066#if defined(CONFIG_SYS_ATA_IDE1_OFFSET) && (CONFIG_SYS_IDE_MAXBUS > 1)
67 CONFIG_SYS_ATA_IDE1_OFFSET,
wdenkc6097192002-11-03 00:24:07 +000068#endif
69};
70
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020071static int ide_bus_ok[CONFIG_SYS_IDE_MAXBUS];
wdenkc6097192002-11-03 00:24:07 +000072
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020073block_dev_desc_t ide_dev_desc[CONFIG_SYS_IDE_MAXDEVICE];
wdenkc6097192002-11-03 00:24:07 +000074/* ------------------------------------------------------------------------- */
75
wdenkc6097192002-11-03 00:24:07 +000076#ifdef CONFIG_IDE_RESET
77static void ide_reset (void);
78#else
79#define ide_reset() /* dummy */
80#endif
81
82static void ide_ident (block_dev_desc_t *dev_desc);
83static uchar ide_wait (int dev, ulong t);
84
85#define IDE_TIME_OUT 2000 /* 2 sec timeout */
86
87#define ATAPI_TIME_OUT 7000 /* 7 sec timeout (5 sec seems to work...) */
88
89#define IDE_SPIN_UP_TIME_OUT 5000 /* 5 sec spin-up timeout */
90
wdenkc6097192002-11-03 00:24:07 +000091static void ident_cpy (unsigned char *dest, unsigned char *src, unsigned int len);
92
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020093#ifndef CONFIG_SYS_ATA_PORT_ADDR
94#define CONFIG_SYS_ATA_PORT_ADDR(port) (port)
Heiko Schocher633e03a2007-06-22 19:11:54 +020095#endif
wdenkc6097192002-11-03 00:24:07 +000096
97#ifdef CONFIG_ATAPI
98static void atapi_inquiry(block_dev_desc_t *dev_desc);
Grant Likely2089cab2007-02-20 09:05:45 +010099ulong atapi_read (int device, lbaint_t blknr, ulong blkcnt, void *buffer);
wdenkc6097192002-11-03 00:24:07 +0000100#endif
101
wdenkc6097192002-11-03 00:24:07 +0000102
103/* ------------------------------------------------------------------------- */
104
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000105int do_ide(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
wdenkc6097192002-11-03 00:24:07 +0000106{
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000107 int rcode = 0;
wdenkc6097192002-11-03 00:24:07 +0000108
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000109 switch (argc) {
110 case 0:
111 case 1:
Simon Glassa06dfc72011-12-10 08:44:01 +0000112 return CMD_RET_USAGE;
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000113 case 2:
114 if (strncmp(argv[1], "res", 3) == 0) {
115 puts("\nReset IDE"
wdenkc6097192002-11-03 00:24:07 +0000116#ifdef CONFIG_IDE_8xx_DIRECT
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000117 " on PCMCIA " PCMCIA_SLOT_MSG
wdenkc6097192002-11-03 00:24:07 +0000118#endif
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000119 ": ");
wdenkc6097192002-11-03 00:24:07 +0000120
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000121 ide_init();
122 return 0;
123 } else if (strncmp(argv[1], "inf", 3) == 0) {
124 int i;
wdenkc6097192002-11-03 00:24:07 +0000125
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000126 putc('\n');
wdenkc6097192002-11-03 00:24:07 +0000127
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000128 for (i = 0; i < CONFIG_SYS_IDE_MAXDEVICE; ++i) {
129 if (ide_dev_desc[i].type == DEV_TYPE_UNKNOWN)
130 continue; /* list only known devices */
131 printf("IDE device %d: ", i);
132 dev_print(&ide_dev_desc[i]);
133 }
134 return 0;
wdenkc6097192002-11-03 00:24:07 +0000135
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000136 } else if (strncmp(argv[1], "dev", 3) == 0) {
137 if ((curr_device < 0)
138 || (curr_device >= CONFIG_SYS_IDE_MAXDEVICE)) {
139 puts("\nno IDE devices available\n");
140 return 1;
141 }
142 printf("\nIDE device %d: ", curr_device);
143 dev_print(&ide_dev_desc[curr_device]);
144 return 0;
145 } else if (strncmp(argv[1], "part", 4) == 0) {
146 int dev, ok;
wdenkc6097192002-11-03 00:24:07 +0000147
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000148 for (ok = 0, dev = 0;
149 dev < CONFIG_SYS_IDE_MAXDEVICE;
150 ++dev) {
151 if (ide_dev_desc[dev].part_type !=
152 PART_TYPE_UNKNOWN) {
153 ++ok;
154 if (dev)
155 putc('\n');
156 print_part(&ide_dev_desc[dev]);
157 }
wdenkc6097192002-11-03 00:24:07 +0000158 }
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000159 if (!ok) {
160 puts("\nno IDE devices available\n");
161 rcode++;
162 }
163 return rcode;
wdenkc6097192002-11-03 00:24:07 +0000164 }
Simon Glassa06dfc72011-12-10 08:44:01 +0000165 return CMD_RET_USAGE;
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000166 case 3:
167 if (strncmp(argv[1], "dev", 3) == 0) {
168 int dev = (int) simple_strtoul(argv[2], NULL, 10);
wdenkc6097192002-11-03 00:24:07 +0000169
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000170 printf("\nIDE device %d: ", dev);
171 if (dev >= CONFIG_SYS_IDE_MAXDEVICE) {
172 puts("unknown device\n");
173 return 1;
174 }
175 dev_print(&ide_dev_desc[dev]);
176 /*ide_print (dev); */
wdenkc6097192002-11-03 00:24:07 +0000177
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000178 if (ide_dev_desc[dev].type == DEV_TYPE_UNKNOWN)
179 return 1;
wdenkc6097192002-11-03 00:24:07 +0000180
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000181 curr_device = dev;
wdenkc6097192002-11-03 00:24:07 +0000182
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000183 puts("... is now current device\n");
wdenkc6097192002-11-03 00:24:07 +0000184
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000185 return 0;
186 } else if (strncmp(argv[1], "part", 4) == 0) {
187 int dev = (int) simple_strtoul(argv[2], NULL, 10);
wdenkc6097192002-11-03 00:24:07 +0000188
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000189 if (ide_dev_desc[dev].part_type != PART_TYPE_UNKNOWN) {
wdenkc6097192002-11-03 00:24:07 +0000190 print_part(&ide_dev_desc[dev]);
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000191 } else {
192 printf("\nIDE device %d not available\n",
193 dev);
194 rcode = 1;
195 }
196 return rcode;
wdenkc6097192002-11-03 00:24:07 +0000197 }
wdenkc6097192002-11-03 00:24:07 +0000198
Simon Glassa06dfc72011-12-10 08:44:01 +0000199 return CMD_RET_USAGE;
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000200 default:
201 /* at least 4 args */
wdenkc6097192002-11-03 00:24:07 +0000202
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000203 if (strcmp(argv[1], "read") == 0) {
204 ulong addr = simple_strtoul(argv[2], NULL, 16);
205 ulong cnt = simple_strtoul(argv[4], NULL, 16);
206 ulong n;
wdenkc6097192002-11-03 00:24:07 +0000207
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200208#ifdef CONFIG_SYS_64BIT_LBA
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000209 lbaint_t blk = simple_strtoull(argv[3], NULL, 16);
wdenkc6097192002-11-03 00:24:07 +0000210
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000211 printf("\nIDE read: device %d block # %lld, count %ld ... ",
212 curr_device, blk, cnt);
wdenkc35ba4e2004-03-14 22:25:36 +0000213#else
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000214 lbaint_t blk = simple_strtoul(argv[3], NULL, 16);
wdenkc35ba4e2004-03-14 22:25:36 +0000215
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000216 printf("\nIDE read: device %d block # %ld, count %ld ... ",
217 curr_device, blk, cnt);
wdenkc35ba4e2004-03-14 22:25:36 +0000218#endif
wdenkc6097192002-11-03 00:24:07 +0000219
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000220 n = ide_dev_desc[curr_device].block_read(curr_device,
221 blk, cnt,
222 (ulong *)addr);
223 /* flush cache after read */
224 flush_cache(addr,
225 cnt * ide_dev_desc[curr_device].blksz);
wdenkc6097192002-11-03 00:24:07 +0000226
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000227 printf("%ld blocks read: %s\n",
228 n, (n == cnt) ? "OK" : "ERROR");
229 if (n == cnt)
230 return 0;
231 else
232 return 1;
233 } else if (strcmp(argv[1], "write") == 0) {
234 ulong addr = simple_strtoul(argv[2], NULL, 16);
235 ulong cnt = simple_strtoul(argv[4], NULL, 16);
236 ulong n;
237
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200238#ifdef CONFIG_SYS_64BIT_LBA
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000239 lbaint_t blk = simple_strtoull(argv[3], NULL, 16);
wdenkc6097192002-11-03 00:24:07 +0000240
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000241 printf("\nIDE write: device %d block # %lld, count %ld ... ",
242 curr_device, blk, cnt);
wdenkc35ba4e2004-03-14 22:25:36 +0000243#else
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000244 lbaint_t blk = simple_strtoul(argv[3], NULL, 16);
wdenkc35ba4e2004-03-14 22:25:36 +0000245
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000246 printf("\nIDE write: device %d block # %ld, count %ld ... ",
247 curr_device, blk, cnt);
wdenkc35ba4e2004-03-14 22:25:36 +0000248#endif
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000249 n = ide_write(curr_device, blk, cnt, (ulong *) addr);
wdenkc6097192002-11-03 00:24:07 +0000250
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000251 printf("%ld blocks written: %s\n",
252 n, (n == cnt) ? "OK" : "ERROR");
253 if (n == cnt)
254 return 0;
255 else
256 return 1;
257 } else {
Simon Glassa06dfc72011-12-10 08:44:01 +0000258 return CMD_RET_USAGE;
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000259 }
wdenkc6097192002-11-03 00:24:07 +0000260
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000261 return rcode;
wdenkc6097192002-11-03 00:24:07 +0000262 }
wdenkc6097192002-11-03 00:24:07 +0000263}
264
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000265int do_diskboot(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
wdenkc6097192002-11-03 00:24:07 +0000266{
Rob Herringa20cbf92012-09-21 04:02:30 +0000267 return common_diskboot(cmdtp, "ide", argc, argv);
wdenkc6097192002-11-03 00:24:07 +0000268}
269
Pavel Herrmann95b0cb52012-10-07 05:56:10 +0000270/* ------------------------------------------------------------------------- */
271
272void __ide_led(uchar led, uchar status)
273{
274#if defined(CONFIG_IDE_LED) && defined(PER8_BASE) /* required by LED_PORT */
275 static uchar led_buffer; /* Buffer for current LED status */
276
277 uchar *led_port = LED_PORT;
278
279 if (status) /* switch LED on */
280 led_buffer |= led;
281 else /* switch LED off */
282 led_buffer &= ~led;
283
284 *led_port = led_buffer;
285#endif
286}
287
288void ide_led(uchar led, uchar status)
289 __attribute__ ((weak, alias("__ide_led")));
290
291#ifndef CONFIG_IDE_LED /* define LED macros, they are not used anyways */
292# define DEVICE_LED(x) 0
293# define LED_IDE1 1
294# define LED_IDE2 2
295#endif
296
wdenkc6097192002-11-03 00:24:07 +0000297/* ------------------------------------------------------------------------- */
298
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000299inline void __ide_outb(int dev, int port, unsigned char val)
Stefan Roese37628252008-08-06 14:05:38 +0200300{
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000301 debug("ide_outb (dev= %d, port= 0x%x, val= 0x%02x) : @ 0x%08lx\n",
302 dev, port, val,
303 (ATA_CURR_BASE(dev) + CONFIG_SYS_ATA_PORT_ADDR(port)));
Macpaul Lind1e49942011-04-11 20:45:32 +0000304
305#if defined(CONFIG_IDE_AHB)
306 if (port) {
307 /* write command */
308 ide_write_register(dev, port, val);
309 } else {
310 /* write data */
311 outb(val, (ATA_CURR_BASE(dev)));
312 }
313#else
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000314 outb(val, (ATA_CURR_BASE(dev) + CONFIG_SYS_ATA_PORT_ADDR(port)));
Macpaul Lind1e49942011-04-11 20:45:32 +0000315#endif
Stefan Roese37628252008-08-06 14:05:38 +0200316}
Macpaul Lind1e49942011-04-11 20:45:32 +0000317
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000318void ide_outb(int dev, int port, unsigned char val)
319 __attribute__ ((weak, alias("__ide_outb")));
Stefan Roese37628252008-08-06 14:05:38 +0200320
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000321inline unsigned char __ide_inb(int dev, int port)
Stefan Roese37628252008-08-06 14:05:38 +0200322{
323 uchar val;
Macpaul Lind1e49942011-04-11 20:45:32 +0000324
325#if defined(CONFIG_IDE_AHB)
326 val = ide_read_register(dev, port);
327#else
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000328 val = inb((ATA_CURR_BASE(dev) + CONFIG_SYS_ATA_PORT_ADDR(port)));
Macpaul Lind1e49942011-04-11 20:45:32 +0000329#endif
330
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000331 debug("ide_inb (dev= %d, port= 0x%x) : @ 0x%08lx -> 0x%02x\n",
332 dev, port,
333 (ATA_CURR_BASE(dev) + CONFIG_SYS_ATA_PORT_ADDR(port)), val);
Stefan Roese37628252008-08-06 14:05:38 +0200334 return val;
335}
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000336
Kim Phillips8167f3c2009-05-19 12:53:36 -0500337unsigned char ide_inb(int dev, int port)
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000338 __attribute__ ((weak, alias("__ide_inb")));
Stefan Roese37628252008-08-06 14:05:38 +0200339
Steven A. Falco2b7574e2008-08-15 15:34:10 -0400340#ifdef CONFIG_TUNE_PIO
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000341inline int __ide_set_piomode(int pio_mode)
Steven A. Falco2b7574e2008-08-15 15:34:10 -0400342{
343 return 0;
344}
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000345
346inline int ide_set_piomode(int pio_mode)
347 __attribute__ ((weak, alias("__ide_set_piomode")));
Steven A. Falco2b7574e2008-08-15 15:34:10 -0400348#endif
349
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000350void ide_init(void)
wdenkc6097192002-11-03 00:24:07 +0000351{
wdenkc6097192002-11-03 00:24:07 +0000352 unsigned char c;
353 int i, bus;
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000354
wdenk7f00b942003-12-07 23:55:12 +0000355#ifdef CONFIG_IDE_8xx_PCCARD
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000356 extern int ide_devices_found; /* Initialized in check_ide_device() */
357#endif /* CONFIG_IDE_8xx_PCCARD */
wdenk7f00b942003-12-07 23:55:12 +0000358
359#ifdef CONFIG_IDE_PREINIT
360 WATCHDOG_RESET();
361
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000362 if (ide_preinit()) {
363 puts("ide_preinit failed\n");
wdenk7f00b942003-12-07 23:55:12 +0000364 return;
365 }
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000366#endif /* CONFIG_IDE_PREINIT */
wdenkc6097192002-11-03 00:24:07 +0000367
wdenkc6097192002-11-03 00:24:07 +0000368 WATCHDOG_RESET();
369
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000370 /*
371 * Reset the IDE just to be sure.
wdenkc6097192002-11-03 00:24:07 +0000372 * Light LED's to show
373 */
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000374 ide_led((LED_IDE1 | LED_IDE2), 1); /* LED's on */
375
376 /* ATAPI Drives seems to need a proper IDE Reset */
377 ide_reset();
wdenkc6097192002-11-03 00:24:07 +0000378
Pavel Herrmann2c13c4a2012-10-09 07:01:56 +0000379#ifdef CONFIG_IDE_INIT_POSTRESET
380 WATCHDOG_RESET();
wdenkc6097192002-11-03 00:24:07 +0000381
Pavel Herrmann2c13c4a2012-10-09 07:01:56 +0000382 if (ide_init_postreset()) {
383 puts("ide_preinit_postreset failed\n");
384 return;
385 }
386#endif /* CONFIG_IDE_INIT_POSTRESET */
wdenkc6097192002-11-03 00:24:07 +0000387
388 /*
389 * Wait for IDE to get ready.
390 * According to spec, this can take up to 31 seconds!
391 */
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000392 for (bus = 0; bus < CONFIG_SYS_IDE_MAXBUS; ++bus) {
393 int dev =
394 bus * (CONFIG_SYS_IDE_MAXDEVICE /
395 CONFIG_SYS_IDE_MAXBUS);
wdenkc6097192002-11-03 00:24:07 +0000396
wdenk4fc95692003-02-28 00:49:47 +0000397#ifdef CONFIG_IDE_8xx_PCCARD
398 /* Skip non-ide devices from probing */
399 if ((ide_devices_found & (1 << bus)) == 0) {
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000400 ide_led((LED_IDE1 | LED_IDE2), 0); /* LED's off */
wdenk4fc95692003-02-28 00:49:47 +0000401 continue;
402 }
403#endif
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000404 printf("Bus %d: ", bus);
wdenkc6097192002-11-03 00:24:07 +0000405
406 ide_bus_ok[bus] = 0;
407
408 /* Select device
409 */
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000410 udelay(100000); /* 100 ms */
411 ide_outb(dev, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(dev));
412 udelay(100000); /* 100 ms */
wdenkc6097192002-11-03 00:24:07 +0000413 i = 0;
414 do {
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000415 udelay(10000); /* 10 ms */
wdenkc6097192002-11-03 00:24:07 +0000416
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000417 c = ide_inb(dev, ATA_STATUS);
wdenkc6097192002-11-03 00:24:07 +0000418 i++;
419 if (i > (ATA_RESET_TIME * 100)) {
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000420 puts("** Timeout **\n");
421 /* LED's off */
422 ide_led((LED_IDE1 | LED_IDE2), 0);
wdenkc6097192002-11-03 00:24:07 +0000423 return;
424 }
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000425 if ((i >= 100) && ((i % 100) == 0))
426 putc('.');
427
wdenkc6097192002-11-03 00:24:07 +0000428 } while (c & ATA_STAT_BUSY);
429
430 if (c & (ATA_STAT_BUSY | ATA_STAT_FAULT)) {
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000431 puts("not available ");
432 debug("Status = 0x%02X ", c);
433#ifndef CONFIG_ATAPI /* ATAPI Devices do not set DRDY */
434 } else if ((c & ATA_STAT_READY) == 0) {
435 puts("not available ");
436 debug("Status = 0x%02X ", c);
wdenkc6097192002-11-03 00:24:07 +0000437#endif
438 } else {
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000439 puts("OK ");
wdenkc6097192002-11-03 00:24:07 +0000440 ide_bus_ok[bus] = 1;
441 }
442 WATCHDOG_RESET();
443 }
wdenk452cfd62002-11-19 11:04:11 +0000444
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000445 putc('\n');
wdenkc6097192002-11-03 00:24:07 +0000446
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000447 ide_led((LED_IDE1 | LED_IDE2), 0); /* LED's off */
wdenkc6097192002-11-03 00:24:07 +0000448
449 curr_device = -1;
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000450 for (i = 0; i < CONFIG_SYS_IDE_MAXDEVICE; ++i) {
wdenkc6097192002-11-03 00:24:07 +0000451 int led = (IDE_BUS(i) == 0) ? LED_IDE1 : LED_IDE2;
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000452 ide_dev_desc[i].type = DEV_TYPE_UNKNOWN;
453 ide_dev_desc[i].if_type = IF_TYPE_IDE;
454 ide_dev_desc[i].dev = i;
455 ide_dev_desc[i].part_type = PART_TYPE_UNKNOWN;
456 ide_dev_desc[i].blksz = 0;
457 ide_dev_desc[i].lba = 0;
458 ide_dev_desc[i].block_read = ide_read;
Macpaul Lind1e49942011-04-11 20:45:32 +0000459 ide_dev_desc[i].block_write = ide_write;
wdenkc6097192002-11-03 00:24:07 +0000460 if (!ide_bus_ok[IDE_BUS(i)])
461 continue;
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000462 ide_led(led, 1); /* LED on */
wdenkc6097192002-11-03 00:24:07 +0000463 ide_ident(&ide_dev_desc[i]);
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000464 ide_led(led, 0); /* LED off */
wdenkc6097192002-11-03 00:24:07 +0000465 dev_print(&ide_dev_desc[i]);
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000466
wdenkc6097192002-11-03 00:24:07 +0000467 if ((ide_dev_desc[i].lba > 0) && (ide_dev_desc[i].blksz > 0)) {
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000468 /* initialize partition type */
469 init_part(&ide_dev_desc[i]);
wdenkc6097192002-11-03 00:24:07 +0000470 if (curr_device < 0)
471 curr_device = i;
472 }
473 }
474 WATCHDOG_RESET();
475}
476
477/* ------------------------------------------------------------------------- */
478
Matthew McClintock6252b4f2011-05-24 05:31:19 +0000479#ifdef CONFIG_PARTITIONS
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000480block_dev_desc_t *ide_get_dev(int dev)
wdenkc6097192002-11-03 00:24:07 +0000481{
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200482 return (dev < CONFIG_SYS_IDE_MAXDEVICE) ? &ide_dev_desc[dev] : NULL;
wdenkc6097192002-11-03 00:24:07 +0000483}
Matthew McClintock6252b4f2011-05-24 05:31:19 +0000484#endif
wdenkc6097192002-11-03 00:24:07 +0000485
wdenkc6097192002-11-03 00:24:07 +0000486/* ------------------------------------------------------------------------- */
487
Pavel Herrmann35ad9a82012-10-09 07:04:39 +0000488void ide_input_swap_data(int dev, ulong *sect_buf, int words)
489 __attribute__ ((weak, alias("__ide_input_swap_data")));
490
491void ide_input_data(int dev, ulong *sect_buf, int words)
492 __attribute__ ((weak, alias("__ide_input_data")));
493
494void ide_output_data(int dev, const ulong *sect_buf, int words)
495 __attribute__ ((weak, alias("__ide_output_data")));
496
wdenk9b7f3842003-10-09 20:09:04 +0000497/* We only need to swap data if we are running on a big endian cpu. */
Pavel Herrmann3597c522012-10-09 07:10:08 +0000498#if defined(__LITTLE_ENDIAN)
Pavel Herrmann35ad9a82012-10-09 07:04:39 +0000499void __ide_input_swap_data(int dev, ulong *sect_buf, int words)
500{
501 ide_input_data(dev, sect_buf, words);
502}
wdenk9b7f3842003-10-09 20:09:04 +0000503#else
Pavel Herrmann35ad9a82012-10-09 07:04:39 +0000504void __ide_input_swap_data(int dev, ulong *sect_buf, int words)
wdenkc6097192002-11-03 00:24:07 +0000505{
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000506 volatile ushort *pbuf =
507 (ushort *) (ATA_CURR_BASE(dev) + ATA_DATA_REG);
508 ushort *dbuf = (ushort *) sect_buf;
wdenkf1276d42005-02-03 23:00:49 +0000509
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000510 debug("in input swap data base for read is %lx\n",
511 (unsigned long) pbuf);
wdenkf1276d42005-02-03 23:00:49 +0000512
513 while (words--) {
Wolfgang Denk39c76422006-06-16 17:32:31 +0200514#ifdef __MIPS__
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000515 *dbuf++ = swab16p((u16 *) pbuf);
516 *dbuf++ = swab16p((u16 *) pbuf);
Wolfgang Denk39c76422006-06-16 17:32:31 +0200517#else
wdenkf1276d42005-02-03 23:00:49 +0000518 *dbuf++ = ld_le16(pbuf);
519 *dbuf++ = ld_le16(pbuf);
Wolfgang Denk39c76422006-06-16 17:32:31 +0200520#endif /* !MIPS */
wdenkf1276d42005-02-03 23:00:49 +0000521 }
wdenkc6097192002-11-03 00:24:07 +0000522}
Pavel Herrmann3597c522012-10-09 07:10:08 +0000523#endif /* __LITTLE_ENDIAN */
wdenk591dda52002-11-18 00:14:45 +0000524
525
Albert Aribaud036c6b42010-08-08 05:17:05 +0530526#if defined(CONFIG_IDE_SWAP_IO)
Pavel Herrmann35ad9a82012-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 Denk2bc10cf2011-10-29 09:41:40 +0000529 ushort *dbuf;
530 volatile ushort *pbuf;
wdenkf1276d42005-02-03 23:00:49 +0000531
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000532 pbuf = (ushort *) (ATA_CURR_BASE(dev) + ATA_DATA_REG);
533 dbuf = (ushort *) sect_buf;
wdenkf1276d42005-02-03 23:00:49 +0000534 while (words--) {
535 EIEIO;
536 *pbuf = *dbuf++;
537 EIEIO;
538 *pbuf = *dbuf++;
539 }
wdenkc6097192002-11-03 00:24:07 +0000540}
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000541#else /* ! CONFIG_IDE_SWAP_IO */
Pavel Herrmann35ad9a82012-10-09 07:04:39 +0000542void __ide_output_data(int dev, const ulong *sect_buf, int words)
wdenk591dda52002-11-18 00:14:45 +0000543{
Macpaul Lind1e49942011-04-11 20:45:32 +0000544#if defined(CONFIG_IDE_AHB)
545 ide_write_data(dev, sect_buf, words);
546#else
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000547 outsw(ATA_CURR_BASE(dev) + ATA_DATA_REG, sect_buf, words << 1);
Macpaul Lind1e49942011-04-11 20:45:32 +0000548#endif
wdenk591dda52002-11-18 00:14:45 +0000549}
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000550#endif /* CONFIG_IDE_SWAP_IO */
wdenkc6097192002-11-03 00:24:07 +0000551
Albert Aribaud036c6b42010-08-08 05:17:05 +0530552#if defined(CONFIG_IDE_SWAP_IO)
Pavel Herrmann35ad9a82012-10-09 07:04:39 +0000553void __ide_input_data(int dev, ulong *sect_buf, int words)
wdenkc6097192002-11-03 00:24:07 +0000554{
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000555 ushort *dbuf;
556 volatile ushort *pbuf;
wdenkf1276d42005-02-03 23:00:49 +0000557
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000558 pbuf = (ushort *) (ATA_CURR_BASE(dev) + ATA_DATA_REG);
559 dbuf = (ushort *) sect_buf;
wdenkf1276d42005-02-03 23:00:49 +0000560
561 debug("in input data base for read is %lx\n", (unsigned long) pbuf);
562
563 while (words--) {
wdenk20c98a62004-04-23 20:32:05 +0000564 EIEIO;
wdenkf1276d42005-02-03 23:00:49 +0000565 *dbuf++ = *pbuf;
wdenkf6925c72005-01-22 18:26:04 +0000566 EIEIO;
wdenkf1276d42005-02-03 23:00:49 +0000567 *dbuf++ = *pbuf;
wdenk2f99a692004-01-04 22:51:12 +0000568 }
wdenkc6097192002-11-03 00:24:07 +0000569}
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000570#else /* ! CONFIG_IDE_SWAP_IO */
Pavel Herrmann35ad9a82012-10-09 07:04:39 +0000571void __ide_input_data(int dev, ulong *sect_buf, int words)
wdenk591dda52002-11-18 00:14:45 +0000572{
Macpaul Lind1e49942011-04-11 20:45:32 +0000573#if defined(CONFIG_IDE_AHB)
574 ide_read_data(dev, sect_buf, words);
575#else
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000576 insw(ATA_CURR_BASE(dev) + ATA_DATA_REG, sect_buf, words << 1);
Macpaul Lind1e49942011-04-11 20:45:32 +0000577#endif
wdenk591dda52002-11-18 00:14:45 +0000578}
579
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000580#endif /* CONFIG_IDE_SWAP_IO */
wdenkc6097192002-11-03 00:24:07 +0000581
582/* -------------------------------------------------------------------------
583 */
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000584static void ide_ident(block_dev_desc_t *dev_desc)
wdenkc6097192002-11-03 00:24:07 +0000585{
wdenkc6097192002-11-03 00:24:07 +0000586 unsigned char c;
Marek Vasute49757e2011-08-20 09:15:13 +0000587 hd_driveid_t iop;
wdenkc6097192002-11-03 00:24:07 +0000588
wdenke2d6d742004-09-28 20:34:50 +0000589#ifdef CONFIG_ATAPI
590 int retries = 0;
wdenk452cfd62002-11-19 11:04:11 +0000591#endif
592
Steven A. Falco2b7574e2008-08-15 15:34:10 -0400593#ifdef CONFIG_TUNE_PIO
594 int pio_mode;
595#endif
596
wdenkc6097192002-11-03 00:24:07 +0000597#if 0
598 int mode, cycle_time;
599#endif
600 int device;
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000601
602 device = dev_desc->dev;
603 printf(" Device %d: ", device);
wdenkc6097192002-11-03 00:24:07 +0000604
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000605 ide_led(DEVICE_LED(device), 1); /* LED on */
wdenkc6097192002-11-03 00:24:07 +0000606 /* Select device
607 */
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000608 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
609 dev_desc->if_type = IF_TYPE_IDE;
wdenkc6097192002-11-03 00:24:07 +0000610#ifdef CONFIG_ATAPI
wdenk452cfd62002-11-19 11:04:11 +0000611
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000612 retries = 0;
wdenk452cfd62002-11-19 11:04:11 +0000613
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000614 /* Warning: This will be tricky to read */
615 while (retries <= 1) {
616 /* check signature */
617 if ((ide_inb(device, ATA_SECT_CNT) == 0x01) &&
618 (ide_inb(device, ATA_SECT_NUM) == 0x01) &&
619 (ide_inb(device, ATA_CYL_LOW) == 0x14) &&
620 (ide_inb(device, ATA_CYL_HIGH) == 0xEB)) {
621 /* ATAPI Signature found */
622 dev_desc->if_type = IF_TYPE_ATAPI;
623 /*
624 * Start Ident Command
625 */
626 ide_outb(device, ATA_COMMAND, ATAPI_CMD_IDENT);
627 /*
628 * Wait for completion - ATAPI devices need more time
629 * to become ready
630 */
631 c = ide_wait(device, ATAPI_TIME_OUT);
632 } else
wdenkc6097192002-11-03 00:24:07 +0000633#endif
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000634 {
635 /*
636 * Start Ident Command
637 */
638 ide_outb(device, ATA_COMMAND, ATA_CMD_IDENT);
wdenkc6097192002-11-03 00:24:07 +0000639
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000640 /*
641 * Wait for completion
642 */
643 c = ide_wait(device, IDE_TIME_OUT);
644 }
645 ide_led(DEVICE_LED(device), 0); /* LED off */
wdenkc6097192002-11-03 00:24:07 +0000646
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000647 if (((c & ATA_STAT_DRQ) == 0) ||
648 ((c & (ATA_STAT_FAULT | ATA_STAT_ERR)) != 0)) {
wdenke2d6d742004-09-28 20:34:50 +0000649#ifdef CONFIG_ATAPI
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000650 {
651 /*
652 * Need to soft reset the device
653 * in case it's an ATAPI...
654 */
655 debug("Retrying...\n");
656 ide_outb(device, ATA_DEV_HD,
657 ATA_LBA | ATA_DEVICE(device));
658 udelay(100000);
659 ide_outb(device, ATA_COMMAND, 0x08);
660 udelay(500000); /* 500 ms */
661 }
662 /*
663 * Select device
664 */
665 ide_outb(device, ATA_DEV_HD,
666 ATA_LBA | ATA_DEVICE(device));
667 retries++;
wdenke2d6d742004-09-28 20:34:50 +0000668#else
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000669 return;
wdenke2d6d742004-09-28 20:34:50 +0000670#endif
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000671 }
wdenke2d6d742004-09-28 20:34:50 +0000672#ifdef CONFIG_ATAPI
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000673 else
674 break;
675 } /* see above - ugly to read */
wdenke2d6d742004-09-28 20:34:50 +0000676
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000677 if (retries == 2) /* Not found */
wdenke2d6d742004-09-28 20:34:50 +0000678 return;
679#endif
wdenkc6097192002-11-03 00:24:07 +0000680
Pavel Herrmann35ad9a82012-10-09 07:04:39 +0000681 ide_input_swap_data(device, (ulong *)&iop, ATA_SECTORWORDS);
wdenkc6097192002-11-03 00:24:07 +0000682
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000683 ident_cpy((unsigned char *) dev_desc->revision, iop.fw_rev,
684 sizeof(dev_desc->revision));
685 ident_cpy((unsigned char *) dev_desc->vendor, iop.model,
686 sizeof(dev_desc->vendor));
687 ident_cpy((unsigned char *) dev_desc->product, iop.serial_no,
688 sizeof(dev_desc->product));
wdenkacd9b102004-03-14 00:59:59 +0000689#ifdef __LITTLE_ENDIAN
690 /*
Richard Retanubun54f30622008-11-06 14:01:51 -0500691 * firmware revision, model, and serial number have Big Endian Byte
692 * order in Word. Convert all three to little endian.
wdenkacd9b102004-03-14 00:59:59 +0000693 *
694 * See CF+ and CompactFlash Specification Revision 2.0:
Richard Retanubun54f30622008-11-06 14:01:51 -0500695 * 6.2.1.6: Identify Drive, Table 39 for more details
wdenkacd9b102004-03-14 00:59:59 +0000696 */
697
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000698 strswab(dev_desc->revision);
699 strswab(dev_desc->vendor);
700 strswab(dev_desc->product);
wdenkacd9b102004-03-14 00:59:59 +0000701#endif /* __LITTLE_ENDIAN */
wdenkc6097192002-11-03 00:24:07 +0000702
Marek Vasute49757e2011-08-20 09:15:13 +0000703 if ((iop.config & 0x0080) == 0x0080)
wdenkc6097192002-11-03 00:24:07 +0000704 dev_desc->removable = 1;
705 else
706 dev_desc->removable = 0;
707
Steven A. Falco2b7574e2008-08-15 15:34:10 -0400708#ifdef CONFIG_TUNE_PIO
709 /* Mode 0 - 2 only, are directly determined by word 51. */
Marek Vasute49757e2011-08-20 09:15:13 +0000710 pio_mode = iop.tPIO;
Steven A. Falco2b7574e2008-08-15 15:34:10 -0400711 if (pio_mode > 2) {
712 printf("WARNING: Invalid PIO (word 51 = %d).\n", pio_mode);
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000713 /* Force it to dead slow, and hope for the best... */
714 pio_mode = 0;
Steven A. Falco2b7574e2008-08-15 15:34:10 -0400715 }
716
717 /* Any CompactFlash Storage Card that supports PIO mode 3 or above
718 * shall set bit 1 of word 53 to one and support the fields contained
719 * in words 64 through 70.
720 */
Marek Vasute49757e2011-08-20 09:15:13 +0000721 if (iop.field_valid & 0x02) {
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000722 /*
723 * Mode 3 and above are possible. Check in order from slow
Steven A. Falco2b7574e2008-08-15 15:34:10 -0400724 * to fast, so we wind up with the highest mode allowed.
725 */
Marek Vasute49757e2011-08-20 09:15:13 +0000726 if (iop.eide_pio_modes & 0x01)
Steven A. Falco2b7574e2008-08-15 15:34:10 -0400727 pio_mode = 3;
Marek Vasute49757e2011-08-20 09:15:13 +0000728 if (iop.eide_pio_modes & 0x02)
Steven A. Falco2b7574e2008-08-15 15:34:10 -0400729 pio_mode = 4;
Marek Vasute49757e2011-08-20 09:15:13 +0000730 if (ata_id_is_cfa((u16 *)&iop)) {
731 if ((iop.cf_advanced_caps & 0x07) == 0x01)
Steven A. Falco2b7574e2008-08-15 15:34:10 -0400732 pio_mode = 5;
Marek Vasute49757e2011-08-20 09:15:13 +0000733 if ((iop.cf_advanced_caps & 0x07) == 0x02)
Steven A. Falco2b7574e2008-08-15 15:34:10 -0400734 pio_mode = 6;
735 }
736 }
737
738 /* System-specific, depends on bus speeds, etc. */
739 ide_set_piomode(pio_mode);
740#endif /* CONFIG_TUNE_PIO */
741
wdenkc6097192002-11-03 00:24:07 +0000742#if 0
743 /*
744 * Drive PIO mode autoselection
745 */
Marek Vasute49757e2011-08-20 09:15:13 +0000746 mode = iop.tPIO;
wdenkc6097192002-11-03 00:24:07 +0000747
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000748 printf("tPIO = 0x%02x = %d\n", mode, mode);
wdenkc6097192002-11-03 00:24:07 +0000749 if (mode > 2) { /* 2 is maximum allowed tPIO value */
750 mode = 2;
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000751 debug("Override tPIO -> 2\n");
wdenkc6097192002-11-03 00:24:07 +0000752 }
Marek Vasute49757e2011-08-20 09:15:13 +0000753 if (iop.field_valid & 2) { /* drive implements ATA2? */
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000754 debug("Drive implements ATA2\n");
Marek Vasute49757e2011-08-20 09:15:13 +0000755 if (iop.capability & 8) { /* drive supports use_iordy? */
756 cycle_time = iop.eide_pio_iordy;
wdenkc6097192002-11-03 00:24:07 +0000757 } else {
Marek Vasute49757e2011-08-20 09:15:13 +0000758 cycle_time = iop.eide_pio;
wdenkc6097192002-11-03 00:24:07 +0000759 }
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000760 debug("cycle time = %d\n", cycle_time);
wdenkc6097192002-11-03 00:24:07 +0000761 mode = 4;
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000762 if (cycle_time > 120)
763 mode = 3; /* 120 ns for PIO mode 4 */
764 if (cycle_time > 180)
765 mode = 2; /* 180 ns for PIO mode 3 */
766 if (cycle_time > 240)
767 mode = 1; /* 240 ns for PIO mode 4 */
768 if (cycle_time > 383)
769 mode = 0; /* 383 ns for PIO mode 4 */
wdenkc6097192002-11-03 00:24:07 +0000770 }
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000771 printf("PIO mode to use: PIO %d\n", mode);
wdenkc6097192002-11-03 00:24:07 +0000772#endif /* 0 */
773
774#ifdef CONFIG_ATAPI
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000775 if (dev_desc->if_type == IF_TYPE_ATAPI) {
wdenkc6097192002-11-03 00:24:07 +0000776 atapi_inquiry(dev_desc);
777 return;
778 }
779#endif /* CONFIG_ATAPI */
780
wdenkacd9b102004-03-14 00:59:59 +0000781#ifdef __BIG_ENDIAN
wdenkc6097192002-11-03 00:24:07 +0000782 /* swap shorts */
Marek Vasute49757e2011-08-20 09:15:13 +0000783 dev_desc->lba = (iop.lba_capacity << 16) | (iop.lba_capacity >> 16);
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000784#else /* ! __BIG_ENDIAN */
wdenkacd9b102004-03-14 00:59:59 +0000785 /*
786 * do not swap shorts on little endian
787 *
788 * See CF+ and CompactFlash Specification Revision 2.0:
789 * 6.2.1.6: Identfy Drive, Table 39, Word Address 57-58 for details.
790 */
Marek Vasute49757e2011-08-20 09:15:13 +0000791 dev_desc->lba = iop.lba_capacity;
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000792#endif /* __BIG_ENDIAN */
wdenkf602aa02004-03-13 23:29:43 +0000793
wdenkc35ba4e2004-03-14 22:25:36 +0000794#ifdef CONFIG_LBA48
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000795 if (iop.command_set_2 & 0x0400) { /* LBA 48 support */
wdenk05939202004-04-18 17:39:38 +0000796 dev_desc->lba48 = 1;
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000797 dev_desc->lba = (unsigned long long) iop.lba48_capacity[0] |
798 ((unsigned long long) iop.lba48_capacity[1] << 16) |
799 ((unsigned long long) iop.lba48_capacity[2] << 32) |
800 ((unsigned long long) iop.lba48_capacity[3] << 48);
wdenkf602aa02004-03-13 23:29:43 +0000801 } else {
wdenkf602aa02004-03-13 23:29:43 +0000802 dev_desc->lba48 = 0;
803 }
804#endif /* CONFIG_LBA48 */
wdenkc6097192002-11-03 00:24:07 +0000805 /* assuming HD */
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000806 dev_desc->type = DEV_TYPE_HARDDISK;
807 dev_desc->blksz = ATA_BLOCKSIZE;
808 dev_desc->lun = 0; /* just to fill something in... */
wdenkc6097192002-11-03 00:24:07 +0000809
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000810#if 0 /* only used to test the powersaving mode,
811 * if enabled, the drive goes after 5 sec
812 * in standby mode */
813 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
814 c = ide_wait(device, IDE_TIME_OUT);
815 ide_outb(device, ATA_SECT_CNT, 1);
816 ide_outb(device, ATA_LBA_LOW, 0);
817 ide_outb(device, ATA_LBA_MID, 0);
818 ide_outb(device, ATA_LBA_HIGH, 0);
819 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
820 ide_outb(device, ATA_COMMAND, 0xe3);
821 udelay(50);
822 c = ide_wait(device, IDE_TIME_OUT); /* can't take over 500 ms */
wdenkc6097192002-11-03 00:24:07 +0000823#endif
824}
825
826
827/* ------------------------------------------------------------------------- */
828
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000829ulong ide_read(int device, lbaint_t blknr, ulong blkcnt, void *buffer)
wdenkc6097192002-11-03 00:24:07 +0000830{
831 ulong n = 0;
832 unsigned char c;
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000833 unsigned char pwrsave = 0; /* power save */
834
wdenkc35ba4e2004-03-14 22:25:36 +0000835#ifdef CONFIG_LBA48
wdenkf602aa02004-03-13 23:29:43 +0000836 unsigned char lba48 = 0;
wdenkc6097192002-11-03 00:24:07 +0000837
Guennadi Liakhovetskib71f1ca2008-04-28 14:36:06 +0200838 if (blknr & 0x0000fffff0000000ULL) {
wdenkf602aa02004-03-13 23:29:43 +0000839 /* more than 28 bits used, use 48bit mode */
840 lba48 = 1;
841 }
842#endif
Marek Vasut92131712011-10-25 11:39:15 +0200843 debug("ide_read dev %d start %lX, blocks %lX buffer at %lX\n",
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000844 device, blknr, blkcnt, (ulong) buffer);
wdenkc6097192002-11-03 00:24:07 +0000845
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000846 ide_led(DEVICE_LED(device), 1); /* LED on */
wdenkc6097192002-11-03 00:24:07 +0000847
848 /* Select device
849 */
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000850 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
851 c = ide_wait(device, IDE_TIME_OUT);
wdenkc6097192002-11-03 00:24:07 +0000852
853 if (c & ATA_STAT_BUSY) {
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000854 printf("IDE read: device %d not ready\n", device);
wdenkc6097192002-11-03 00:24:07 +0000855 goto IDE_READ_E;
856 }
857
858 /* first check if the drive is in Powersaving mode, if yes,
859 * increase the timeout value */
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000860 ide_outb(device, ATA_COMMAND, ATA_CMD_CHK_PWR);
861 udelay(50);
wdenkc6097192002-11-03 00:24:07 +0000862
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000863 c = ide_wait(device, IDE_TIME_OUT); /* can't take over 500 ms */
wdenkc6097192002-11-03 00:24:07 +0000864
865 if (c & ATA_STAT_BUSY) {
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000866 printf("IDE read: device %d not ready\n", device);
wdenkc6097192002-11-03 00:24:07 +0000867 goto IDE_READ_E;
868 }
869 if ((c & ATA_STAT_ERR) == ATA_STAT_ERR) {
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000870 printf("No Powersaving mode %X\n", c);
wdenkc6097192002-11-03 00:24:07 +0000871 } else {
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000872 c = ide_inb(device, ATA_SECT_CNT);
873 debug("Powersaving %02X\n", c);
874 if (c == 0)
875 pwrsave = 1;
wdenkc6097192002-11-03 00:24:07 +0000876 }
877
878
879 while (blkcnt-- > 0) {
880
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000881 c = ide_wait(device, IDE_TIME_OUT);
wdenkc6097192002-11-03 00:24:07 +0000882
883 if (c & ATA_STAT_BUSY) {
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000884 printf("IDE read: device %d not ready\n", device);
wdenkc6097192002-11-03 00:24:07 +0000885 break;
886 }
wdenkc35ba4e2004-03-14 22:25:36 +0000887#ifdef CONFIG_LBA48
wdenkf602aa02004-03-13 23:29:43 +0000888 if (lba48) {
889 /* write high bits */
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000890 ide_outb(device, ATA_SECT_CNT, 0);
891 ide_outb(device, ATA_LBA_LOW, (blknr >> 24) & 0xFF);
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200892#ifdef CONFIG_SYS_64BIT_LBA
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000893 ide_outb(device, ATA_LBA_MID, (blknr >> 32) & 0xFF);
894 ide_outb(device, ATA_LBA_HIGH, (blknr >> 40) & 0xFF);
Guennadi Liakhovetskib71f1ca2008-04-28 14:36:06 +0200895#else
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000896 ide_outb(device, ATA_LBA_MID, 0);
897 ide_outb(device, ATA_LBA_HIGH, 0);
Guennadi Liakhovetskib71f1ca2008-04-28 14:36:06 +0200898#endif
wdenkf602aa02004-03-13 23:29:43 +0000899 }
900#endif
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000901 ide_outb(device, ATA_SECT_CNT, 1);
902 ide_outb(device, ATA_LBA_LOW, (blknr >> 0) & 0xFF);
903 ide_outb(device, ATA_LBA_MID, (blknr >> 8) & 0xFF);
904 ide_outb(device, ATA_LBA_HIGH, (blknr >> 16) & 0xFF);
wdenkf602aa02004-03-13 23:29:43 +0000905
wdenkc35ba4e2004-03-14 22:25:36 +0000906#ifdef CONFIG_LBA48
wdenkf602aa02004-03-13 23:29:43 +0000907 if (lba48) {
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000908 ide_outb(device, ATA_DEV_HD,
909 ATA_LBA | ATA_DEVICE(device));
910 ide_outb(device, ATA_COMMAND, ATA_CMD_READ_EXT);
wdenkf602aa02004-03-13 23:29:43 +0000911
912 } else
913#endif
914 {
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000915 ide_outb(device, ATA_DEV_HD, ATA_LBA |
916 ATA_DEVICE(device) | ((blknr >> 24) & 0xF));
917 ide_outb(device, ATA_COMMAND, ATA_CMD_READ);
wdenkf602aa02004-03-13 23:29:43 +0000918 }
wdenkc6097192002-11-03 00:24:07 +0000919
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000920 udelay(50);
wdenkc6097192002-11-03 00:24:07 +0000921
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000922 if (pwrsave) {
923 /* may take up to 4 sec */
924 c = ide_wait(device, IDE_SPIN_UP_TIME_OUT);
925 pwrsave = 0;
wdenkc6097192002-11-03 00:24:07 +0000926 } else {
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000927 /* can't take over 500 ms */
928 c = ide_wait(device, IDE_TIME_OUT);
wdenkc6097192002-11-03 00:24:07 +0000929 }
930
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000931 if ((c & (ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR)) !=
932 ATA_STAT_DRQ) {
Heiko Schocher0f602e12009-12-03 11:21:21 +0100933#if defined(CONFIG_SYS_64BIT_LBA)
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000934 printf("Error (no IRQ) dev %d blk %lld: status 0x%02x\n",
wdenkc6097192002-11-03 00:24:07 +0000935 device, blknr, c);
wdenkf602aa02004-03-13 23:29:43 +0000936#else
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000937 printf("Error (no IRQ) dev %d blk %ld: status 0x%02x\n",
938 device, (ulong) blknr, c);
wdenkf602aa02004-03-13 23:29:43 +0000939#endif
wdenkc6097192002-11-03 00:24:07 +0000940 break;
941 }
942
Pavel Herrmann35ad9a82012-10-09 07:04:39 +0000943 ide_input_data(device, buffer, ATA_SECTORWORDS);
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000944 (void) ide_inb(device, ATA_STATUS); /* clear IRQ */
wdenkc6097192002-11-03 00:24:07 +0000945
946 ++n;
947 ++blknr;
Greg Lopp50abf9f2007-04-13 08:02:24 +0200948 buffer += ATA_BLOCKSIZE;
wdenkc6097192002-11-03 00:24:07 +0000949 }
950IDE_READ_E:
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000951 ide_led(DEVICE_LED(device), 0); /* LED off */
wdenkc6097192002-11-03 00:24:07 +0000952 return (n);
953}
954
955/* ------------------------------------------------------------------------- */
956
957
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000958ulong ide_write(int device, lbaint_t blknr, ulong blkcnt, const void *buffer)
wdenkc6097192002-11-03 00:24:07 +0000959{
960 ulong n = 0;
961 unsigned char c;
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000962
wdenkc35ba4e2004-03-14 22:25:36 +0000963#ifdef CONFIG_LBA48
wdenkf602aa02004-03-13 23:29:43 +0000964 unsigned char lba48 = 0;
965
Guennadi Liakhovetskib71f1ca2008-04-28 14:36:06 +0200966 if (blknr & 0x0000fffff0000000ULL) {
wdenkf602aa02004-03-13 23:29:43 +0000967 /* more than 28 bits used, use 48bit mode */
968 lba48 = 1;
969 }
970#endif
wdenkc6097192002-11-03 00:24:07 +0000971
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000972 ide_led(DEVICE_LED(device), 1); /* LED on */
wdenkc6097192002-11-03 00:24:07 +0000973
974 /* Select device
975 */
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000976 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
wdenkc6097192002-11-03 00:24:07 +0000977
978 while (blkcnt-- > 0) {
979
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000980 c = ide_wait(device, IDE_TIME_OUT);
wdenkc6097192002-11-03 00:24:07 +0000981
982 if (c & ATA_STAT_BUSY) {
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000983 printf("IDE read: device %d not ready\n", device);
wdenkc6097192002-11-03 00:24:07 +0000984 goto WR_OUT;
985 }
wdenkc35ba4e2004-03-14 22:25:36 +0000986#ifdef CONFIG_LBA48
wdenkf602aa02004-03-13 23:29:43 +0000987 if (lba48) {
988 /* write high bits */
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000989 ide_outb(device, ATA_SECT_CNT, 0);
990 ide_outb(device, ATA_LBA_LOW, (blknr >> 24) & 0xFF);
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200991#ifdef CONFIG_SYS_64BIT_LBA
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000992 ide_outb(device, ATA_LBA_MID, (blknr >> 32) & 0xFF);
993 ide_outb(device, ATA_LBA_HIGH, (blknr >> 40) & 0xFF);
Guennadi Liakhovetskib71f1ca2008-04-28 14:36:06 +0200994#else
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000995 ide_outb(device, ATA_LBA_MID, 0);
996 ide_outb(device, ATA_LBA_HIGH, 0);
Guennadi Liakhovetskib71f1ca2008-04-28 14:36:06 +0200997#endif
wdenkf602aa02004-03-13 23:29:43 +0000998 }
999#endif
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001000 ide_outb(device, ATA_SECT_CNT, 1);
1001 ide_outb(device, ATA_LBA_LOW, (blknr >> 0) & 0xFF);
1002 ide_outb(device, ATA_LBA_MID, (blknr >> 8) & 0xFF);
1003 ide_outb(device, ATA_LBA_HIGH, (blknr >> 16) & 0xFF);
wdenkf602aa02004-03-13 23:29:43 +00001004
wdenkc35ba4e2004-03-14 22:25:36 +00001005#ifdef CONFIG_LBA48
wdenkf602aa02004-03-13 23:29:43 +00001006 if (lba48) {
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001007 ide_outb(device, ATA_DEV_HD,
1008 ATA_LBA | ATA_DEVICE(device));
1009 ide_outb(device, ATA_COMMAND, ATA_CMD_WRITE_EXT);
wdenkf602aa02004-03-13 23:29:43 +00001010
1011 } else
1012#endif
1013 {
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001014 ide_outb(device, ATA_DEV_HD, ATA_LBA |
1015 ATA_DEVICE(device) | ((blknr >> 24) & 0xF));
1016 ide_outb(device, ATA_COMMAND, ATA_CMD_WRITE);
wdenkf602aa02004-03-13 23:29:43 +00001017 }
wdenkc6097192002-11-03 00:24:07 +00001018
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001019 udelay(50);
wdenkc6097192002-11-03 00:24:07 +00001020
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001021 /* can't take over 500 ms */
1022 c = ide_wait(device, IDE_TIME_OUT);
wdenkc6097192002-11-03 00:24:07 +00001023
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001024 if ((c & (ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR)) !=
1025 ATA_STAT_DRQ) {
Heiko Schocher0f602e12009-12-03 11:21:21 +01001026#if defined(CONFIG_SYS_64BIT_LBA)
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001027 printf("Error (no IRQ) dev %d blk %lld: status 0x%02x\n",
wdenkc6097192002-11-03 00:24:07 +00001028 device, blknr, c);
wdenkf602aa02004-03-13 23:29:43 +00001029#else
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001030 printf("Error (no IRQ) dev %d blk %ld: status 0x%02x\n",
1031 device, (ulong) blknr, c);
wdenkf602aa02004-03-13 23:29:43 +00001032#endif
wdenkc6097192002-11-03 00:24:07 +00001033 goto WR_OUT;
1034 }
1035
Pavel Herrmann35ad9a82012-10-09 07:04:39 +00001036 ide_output_data(device, buffer, ATA_SECTORWORDS);
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001037 c = ide_inb(device, ATA_STATUS); /* clear IRQ */
wdenkc6097192002-11-03 00:24:07 +00001038 ++n;
1039 ++blknr;
Greg Lopp50abf9f2007-04-13 08:02:24 +02001040 buffer += ATA_BLOCKSIZE;
wdenkc6097192002-11-03 00:24:07 +00001041 }
1042WR_OUT:
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001043 ide_led(DEVICE_LED(device), 0); /* LED off */
wdenkc6097192002-11-03 00:24:07 +00001044 return (n);
1045}
1046
1047/* ------------------------------------------------------------------------- */
1048
1049/*
1050 * copy src to dest, skipping leading and trailing blanks and null
1051 * terminate the string
wdenk18bd81e2004-03-17 01:13:07 +00001052 * "len" is the size of available memory including the terminating '\0'
wdenkc6097192002-11-03 00:24:07 +00001053 */
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001054static void ident_cpy(unsigned char *dst, unsigned char *src,
1055 unsigned int len)
wdenkc6097192002-11-03 00:24:07 +00001056{
wdenk18bd81e2004-03-17 01:13:07 +00001057 unsigned char *end, *last;
wdenkc6097192002-11-03 00:24:07 +00001058
wdenk18bd81e2004-03-17 01:13:07 +00001059 last = dst;
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001060 end = src + len - 1;
wdenk18bd81e2004-03-17 01:13:07 +00001061
1062 /* reserve space for '\0' */
1063 if (len < 2)
1064 goto OUT;
wdenkc12081a2004-03-23 20:18:25 +00001065
wdenk18bd81e2004-03-17 01:13:07 +00001066 /* skip leading white space */
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001067 while ((*src) && (src < end) && (*src == ' '))
wdenk18bd81e2004-03-17 01:13:07 +00001068 ++src;
1069
1070 /* copy string, omitting trailing white space */
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001071 while ((*src) && (src < end)) {
wdenk18bd81e2004-03-17 01:13:07 +00001072 *dst++ = *src;
1073 if (*src++ != ' ')
1074 last = dst;
wdenkc6097192002-11-03 00:24:07 +00001075 }
wdenk18bd81e2004-03-17 01:13:07 +00001076OUT:
1077 *last = '\0';
wdenkc6097192002-11-03 00:24:07 +00001078}
1079
1080/* ------------------------------------------------------------------------- */
1081
1082/*
1083 * Wait until Busy bit is off, or timeout (in ms)
1084 * Return last status
1085 */
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001086static uchar ide_wait(int dev, ulong t)
wdenkc6097192002-11-03 00:24:07 +00001087{
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001088 ulong delay = 10 * t; /* poll every 100 us */
wdenkc6097192002-11-03 00:24:07 +00001089 uchar c;
1090
wdenk591dda52002-11-18 00:14:45 +00001091 while ((c = ide_inb(dev, ATA_STATUS)) & ATA_STAT_BUSY) {
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001092 udelay(100);
1093 if (delay-- == 0)
wdenkc6097192002-11-03 00:24:07 +00001094 break;
wdenkc6097192002-11-03 00:24:07 +00001095 }
1096 return (c);
1097}
1098
1099/* ------------------------------------------------------------------------- */
1100
1101#ifdef CONFIG_IDE_RESET
1102extern void ide_set_reset(int idereset);
1103
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001104static void ide_reset(void)
wdenkc6097192002-11-03 00:24:07 +00001105{
wdenkc6097192002-11-03 00:24:07 +00001106 int i;
1107
1108 curr_device = -1;
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001109 for (i = 0; i < CONFIG_SYS_IDE_MAXBUS; ++i)
wdenkc6097192002-11-03 00:24:07 +00001110 ide_bus_ok[i] = 0;
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001111 for (i = 0; i < CONFIG_SYS_IDE_MAXDEVICE; ++i)
wdenkc6097192002-11-03 00:24:07 +00001112 ide_dev_desc[i].type = DEV_TYPE_UNKNOWN;
1113
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001114 ide_set_reset(1); /* assert reset */
wdenkc6097192002-11-03 00:24:07 +00001115
Martin Krause7e35caf2008-04-03 13:37:56 +02001116 /* the reset signal shall be asserted for et least 25 us */
1117 udelay(25);
1118
wdenkc6097192002-11-03 00:24:07 +00001119 WATCHDOG_RESET();
1120
wdenkc6097192002-11-03 00:24:07 +00001121 /* de-assert RESET signal */
1122 ide_set_reset(0);
1123
1124 /* wait 250 ms */
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001125 for (i = 0; i < 250; ++i)
1126 udelay(1000);
wdenkc6097192002-11-03 00:24:07 +00001127}
1128
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001129#endif /* CONFIG_IDE_RESET */
wdenkc6097192002-11-03 00:24:07 +00001130
1131/* ------------------------------------------------------------------------- */
1132
Heiko Schocherffb293a2009-09-23 07:56:08 +02001133#if defined(CONFIG_OF_IDE_FIXUP)
1134int ide_device_present(int dev)
1135{
1136 if (dev >= CONFIG_SYS_IDE_MAXBUS)
1137 return 0;
1138 return (ide_dev_desc[dev].type == DEV_TYPE_UNKNOWN ? 0 : 1);
1139}
1140#endif
wdenkc6097192002-11-03 00:24:07 +00001141/* ------------------------------------------------------------------------- */
1142
1143#ifdef CONFIG_ATAPI
1144/****************************************************************************
1145 * ATAPI Support
1146 */
1147
Pavel Herrmann35ad9a82012-10-09 07:04:39 +00001148void ide_input_data_shorts(int dev, ushort *sect_buf, int shorts)
1149 __attribute__ ((weak, alias("__ide_input_data_shorts")));
1150
1151void ide_output_data_shorts(int dev, ushort *sect_buf, int shorts)
1152 __attribute__ ((weak, alias("__ide_output_data_shorts")));
1153
1154
Albert Aribaud036c6b42010-08-08 05:17:05 +05301155#if defined(CONFIG_IDE_SWAP_IO)
wdenkc6097192002-11-03 00:24:07 +00001156/* since ATAPI may use commands with not 4 bytes alligned length
1157 * we have our own transfer functions, 2 bytes alligned */
Pavel Herrmann35ad9a82012-10-09 07:04:39 +00001158void __ide_output_data_shorts(int dev, ushort *sect_buf, int shorts)
wdenkc6097192002-11-03 00:24:07 +00001159{
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001160 ushort *dbuf;
1161 volatile ushort *pbuf;
wdenkc6097192002-11-03 00:24:07 +00001162
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001163 pbuf = (ushort *) (ATA_CURR_BASE(dev) + ATA_DATA_REG);
1164 dbuf = (ushort *) sect_buf;
wdenk634d2f72004-04-15 23:14:49 +00001165
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001166 debug("in output data shorts base for read is %lx\n",
1167 (unsigned long) pbuf);
wdenk634d2f72004-04-15 23:14:49 +00001168
wdenkc6097192002-11-03 00:24:07 +00001169 while (shorts--) {
wdenk20c98a62004-04-23 20:32:05 +00001170 EIEIO;
wdenkf1276d42005-02-03 23:00:49 +00001171 *pbuf = *dbuf++;
wdenkc6097192002-11-03 00:24:07 +00001172 }
wdenkf1276d42005-02-03 23:00:49 +00001173}
1174
Pavel Herrmann35ad9a82012-10-09 07:04:39 +00001175void __ide_input_data_shorts(int dev, ushort *sect_buf, int shorts)
wdenkf1276d42005-02-03 23:00:49 +00001176{
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001177 ushort *dbuf;
1178 volatile ushort *pbuf;
wdenkf1276d42005-02-03 23:00:49 +00001179
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001180 pbuf = (ushort *) (ATA_CURR_BASE(dev) + ATA_DATA_REG);
1181 dbuf = (ushort *) sect_buf;
wdenkf1276d42005-02-03 23:00:49 +00001182
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001183 debug("in input data shorts base for read is %lx\n",
1184 (unsigned long) pbuf);
wdenkf1276d42005-02-03 23:00:49 +00001185
1186 while (shorts--) {
1187 EIEIO;
1188 *dbuf++ = *pbuf;
1189 }
wdenkc6097192002-11-03 00:24:07 +00001190}
1191
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001192#else /* ! CONFIG_IDE_SWAP_IO */
Pavel Herrmann35ad9a82012-10-09 07:04:39 +00001193void __ide_output_data_shorts(int dev, ushort *sect_buf, int shorts)
wdenk591dda52002-11-18 00:14:45 +00001194{
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001195 outsw(ATA_CURR_BASE(dev) + ATA_DATA_REG, sect_buf, shorts);
wdenk591dda52002-11-18 00:14:45 +00001196}
1197
Pavel Herrmann35ad9a82012-10-09 07:04:39 +00001198void __ide_input_data_shorts(int dev, ushort *sect_buf, int shorts)
wdenk591dda52002-11-18 00:14:45 +00001199{
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001200 insw(ATA_CURR_BASE(dev) + ATA_DATA_REG, sect_buf, shorts);
wdenk591dda52002-11-18 00:14:45 +00001201}
1202
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001203#endif /* CONFIG_IDE_SWAP_IO */
wdenk591dda52002-11-18 00:14:45 +00001204
wdenkc6097192002-11-03 00:24:07 +00001205/*
1206 * Wait until (Status & mask) == res, or timeout (in ms)
1207 * Return last status
1208 * This is used since some ATAPI CD ROMs clears their Busy Bit first
1209 * and then they set their DRQ Bit
1210 */
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001211static uchar atapi_wait_mask(int dev, ulong t, uchar mask, uchar res)
wdenkc6097192002-11-03 00:24:07 +00001212{
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001213 ulong delay = 10 * t; /* poll every 100 us */
wdenkc6097192002-11-03 00:24:07 +00001214 uchar c;
1215
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001216 /* prevents to read the status before valid */
1217 c = ide_inb(dev, ATA_DEV_CTL);
1218
wdenk591dda52002-11-18 00:14:45 +00001219 while (((c = ide_inb(dev, ATA_STATUS)) & mask) != res) {
wdenkc6097192002-11-03 00:24:07 +00001220 /* break if error occurs (doesn't make sense to wait more) */
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001221 if ((c & ATA_STAT_ERR) == ATA_STAT_ERR)
wdenkc6097192002-11-03 00:24:07 +00001222 break;
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001223 udelay(100);
1224 if (delay-- == 0)
wdenkc6097192002-11-03 00:24:07 +00001225 break;
wdenkc6097192002-11-03 00:24:07 +00001226 }
1227 return (c);
1228}
1229
1230/*
1231 * issue an atapi command
1232 */
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001233unsigned char atapi_issue(int device, unsigned char *ccb, int ccblen,
1234 unsigned char *buffer, int buflen)
wdenkc6097192002-11-03 00:24:07 +00001235{
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001236 unsigned char c, err, mask, res;
wdenkc6097192002-11-03 00:24:07 +00001237 int n;
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001238
1239 ide_led(DEVICE_LED(device), 1); /* LED on */
wdenkc6097192002-11-03 00:24:07 +00001240
1241 /* Select device
1242 */
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001243 mask = ATA_STAT_BUSY | ATA_STAT_DRQ;
wdenkc6097192002-11-03 00:24:07 +00001244 res = 0;
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001245 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
1246 c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
wdenkc6097192002-11-03 00:24:07 +00001247 if ((c & mask) != res) {
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001248 printf("ATAPI_ISSUE: device %d not ready status %X\n", device,
1249 c);
1250 err = 0xFF;
wdenkc6097192002-11-03 00:24:07 +00001251 goto AI_OUT;
1252 }
1253 /* write taskfile */
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001254 ide_outb(device, ATA_ERROR_REG, 0); /* no DMA, no overlaped */
1255 ide_outb(device, ATA_SECT_CNT, 0);
1256 ide_outb(device, ATA_SECT_NUM, 0);
1257 ide_outb(device, ATA_CYL_LOW, (unsigned char) (buflen & 0xFF));
1258 ide_outb(device, ATA_CYL_HIGH,
1259 (unsigned char) ((buflen >> 8) & 0xFF));
1260 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
wdenkc6097192002-11-03 00:24:07 +00001261
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001262 ide_outb(device, ATA_COMMAND, ATAPI_CMD_PACKET);
1263 udelay(50);
wdenkc6097192002-11-03 00:24:07 +00001264
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001265 mask = ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR;
wdenkc6097192002-11-03 00:24:07 +00001266 res = ATA_STAT_DRQ;
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001267 c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
wdenkc6097192002-11-03 00:24:07 +00001268
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001269 if ((c & mask) != res) { /* DRQ must be 1, BSY 0 */
1270 printf("ATAPI_ISSUE: Error (no IRQ) before sending ccb dev %d status 0x%02x\n",
1271 device, c);
1272 err = 0xFF;
wdenkc6097192002-11-03 00:24:07 +00001273 goto AI_OUT;
1274 }
1275
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001276 /* write command block */
Pavel Herrmann35ad9a82012-10-09 07:04:39 +00001277 ide_output_data_shorts(device, (unsigned short *) ccb, ccblen / 2);
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001278
Wolfgang Denka1be4762008-05-20 16:00:29 +02001279 /* ATAPI Command written wait for completition */
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001280 udelay(5000); /* device must set bsy */
wdenkc6097192002-11-03 00:24:07 +00001281
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001282 mask = ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR;
1283 /*
1284 * if no data wait for DRQ = 0 BSY = 0
1285 * if data wait for DRQ = 1 BSY = 0
1286 */
1287 res = 0;
1288 if (buflen)
wdenkc6097192002-11-03 00:24:07 +00001289 res = ATA_STAT_DRQ;
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001290 c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
1291 if ((c & mask) != res) {
wdenkc6097192002-11-03 00:24:07 +00001292 if (c & ATA_STAT_ERR) {
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001293 err = (ide_inb(device, ATA_ERROR_REG)) >> 4;
1294 debug("atapi_issue 1 returned sense key %X status %02X\n",
1295 err, c);
wdenkc6097192002-11-03 00:24:07 +00001296 } else {
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001297 printf("ATAPI_ISSUE: (no DRQ) after sending ccb (%x) status 0x%02x\n",
1298 ccb[0], c);
1299 err = 0xFF;
wdenkc6097192002-11-03 00:24:07 +00001300 }
1301 goto AI_OUT;
1302 }
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001303 n = ide_inb(device, ATA_CYL_HIGH);
1304 n <<= 8;
1305 n += ide_inb(device, ATA_CYL_LOW);
1306 if (n > buflen) {
1307 printf("ERROR, transfer bytes %d requested only %d\n", n,
1308 buflen);
1309 err = 0xff;
wdenkc6097192002-11-03 00:24:07 +00001310 goto AI_OUT;
1311 }
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001312 if ((n == 0) && (buflen < 0)) {
1313 printf("ERROR, transfer bytes %d requested %d\n", n, buflen);
1314 err = 0xff;
wdenkc6097192002-11-03 00:24:07 +00001315 goto AI_OUT;
1316 }
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001317 if (n != buflen) {
1318 debug("WARNING, transfer bytes %d not equal with requested %d\n",
1319 n, buflen);
wdenkc6097192002-11-03 00:24:07 +00001320 }
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001321 if (n != 0) { /* data transfer */
1322 debug("ATAPI_ISSUE: %d Bytes to transfer\n", n);
1323 /* we transfer shorts */
1324 n >>= 1;
wdenkc6097192002-11-03 00:24:07 +00001325 /* ok now decide if it is an in or output */
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001326 if ((ide_inb(device, ATA_SECT_CNT) & 0x02) == 0) {
1327 debug("Write to device\n");
Pavel Herrmann35ad9a82012-10-09 07:04:39 +00001328 ide_output_data_shorts(device,
1329 (unsigned short *) buffer, n);
wdenkc6097192002-11-03 00:24:07 +00001330 } else {
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001331 debug("Read from device @ %p shorts %d\n", buffer, n);
Pavel Herrmann35ad9a82012-10-09 07:04:39 +00001332 ide_input_data_shorts(device,
1333 (unsigned short *) buffer, n);
wdenkc6097192002-11-03 00:24:07 +00001334 }
1335 }
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001336 udelay(5000); /* seems that some CD ROMs need this... */
1337 mask = ATA_STAT_BUSY | ATA_STAT_ERR;
1338 res = 0;
1339 c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
wdenkc6097192002-11-03 00:24:07 +00001340 if ((c & ATA_STAT_ERR) == ATA_STAT_ERR) {
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001341 err = (ide_inb(device, ATA_ERROR_REG) >> 4);
1342 debug("atapi_issue 2 returned sense key %X status %X\n", err,
1343 c);
wdenkc6097192002-11-03 00:24:07 +00001344 } else {
1345 err = 0;
1346 }
1347AI_OUT:
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001348 ide_led(DEVICE_LED(device), 0); /* LED off */
wdenkc6097192002-11-03 00:24:07 +00001349 return (err);
1350}
1351
1352/*
1353 * sending the command to atapi_issue. If an status other than good
1354 * returns, an request_sense will be issued
1355 */
1356
Wolfgang Denka1be4762008-05-20 16:00:29 +02001357#define ATAPI_DRIVE_NOT_READY 100
wdenkc6097192002-11-03 00:24:07 +00001358#define ATAPI_UNIT_ATTN 10
1359
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001360unsigned char atapi_issue_autoreq(int device,
1361 unsigned char *ccb,
1362 int ccblen,
1363 unsigned char *buffer, int buflen)
wdenkc6097192002-11-03 00:24:07 +00001364{
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001365 unsigned char sense_data[18], sense_ccb[12];
1366 unsigned char res, key, asc, ascq;
1367 int notready, unitattn;
wdenkc6097192002-11-03 00:24:07 +00001368
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001369 unitattn = ATAPI_UNIT_ATTN;
1370 notready = ATAPI_DRIVE_NOT_READY;
wdenkc6097192002-11-03 00:24:07 +00001371
1372retry:
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001373 res = atapi_issue(device, ccb, ccblen, buffer, buflen);
1374 if (res == 0)
1375 return 0; /* Ok */
wdenkc6097192002-11-03 00:24:07 +00001376
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001377 if (res == 0xFF)
1378 return 0xFF; /* error */
wdenkc6097192002-11-03 00:24:07 +00001379
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001380 debug("(auto_req)atapi_issue returned sense key %X\n", res);
wdenkc6097192002-11-03 00:24:07 +00001381
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001382 memset(sense_ccb, 0, sizeof(sense_ccb));
1383 memset(sense_data, 0, sizeof(sense_data));
1384 sense_ccb[0] = ATAPI_CMD_REQ_SENSE;
1385 sense_ccb[4] = 18; /* allocation Length */
wdenkc6097192002-11-03 00:24:07 +00001386
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001387 res = atapi_issue(device, sense_ccb, 12, sense_data, 18);
1388 key = (sense_data[2] & 0xF);
1389 asc = (sense_data[12]);
1390 ascq = (sense_data[13]);
wdenkc6097192002-11-03 00:24:07 +00001391
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001392 debug("ATAPI_CMD_REQ_SENSE returned %x\n", res);
1393 debug(" Sense page: %02X key %02X ASC %02X ASCQ %02X\n",
1394 sense_data[0], key, asc, ascq);
wdenkc6097192002-11-03 00:24:07 +00001395
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001396 if ((key == 0))
1397 return 0; /* ok device ready */
wdenkc6097192002-11-03 00:24:07 +00001398
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001399 if ((key == 6) || (asc == 0x29) || (asc == 0x28)) { /* Unit Attention */
1400 if (unitattn-- > 0) {
1401 udelay(200 * 1000);
wdenkc6097192002-11-03 00:24:07 +00001402 goto retry;
1403 }
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001404 printf("Unit Attention, tried %d\n", ATAPI_UNIT_ATTN);
wdenkc6097192002-11-03 00:24:07 +00001405 goto error;
1406 }
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001407 if ((asc == 0x4) && (ascq == 0x1)) {
1408 /* not ready, but will be ready soon */
1409 if (notready-- > 0) {
1410 udelay(200 * 1000);
wdenkc6097192002-11-03 00:24:07 +00001411 goto retry;
1412 }
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001413 printf("Drive not ready, tried %d times\n",
1414 ATAPI_DRIVE_NOT_READY);
wdenkc6097192002-11-03 00:24:07 +00001415 goto error;
1416 }
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001417 if (asc == 0x3a) {
1418 debug("Media not present\n");
wdenkc6097192002-11-03 00:24:07 +00001419 goto error;
1420 }
wdenk452cfd62002-11-19 11:04:11 +00001421
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001422 printf("ERROR: Unknown Sense key %02X ASC %02X ASCQ %02X\n", key, asc,
1423 ascq);
wdenkc6097192002-11-03 00:24:07 +00001424error:
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001425 debug("ERROR Sense key %02X ASC %02X ASCQ %02X\n", key, asc, ascq);
wdenkc6097192002-11-03 00:24:07 +00001426 return (0xFF);
1427}
1428
1429
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001430static void atapi_inquiry(block_dev_desc_t *dev_desc)
wdenkc6097192002-11-03 00:24:07 +00001431{
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001432 unsigned char ccb[12]; /* Command descriptor block */
1433 unsigned char iobuf[64]; /* temp buf */
wdenkc6097192002-11-03 00:24:07 +00001434 unsigned char c;
1435 int device;
1436
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001437 device = dev_desc->dev;
1438 dev_desc->type = DEV_TYPE_UNKNOWN; /* not yet valid */
1439 dev_desc->block_read = atapi_read;
wdenkc6097192002-11-03 00:24:07 +00001440
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001441 memset(ccb, 0, sizeof(ccb));
1442 memset(iobuf, 0, sizeof(iobuf));
wdenkc6097192002-11-03 00:24:07 +00001443
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001444 ccb[0] = ATAPI_CMD_INQUIRY;
1445 ccb[4] = 40; /* allocation Legnth */
1446 c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *) iobuf, 40);
wdenkc6097192002-11-03 00:24:07 +00001447
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001448 debug("ATAPI_CMD_INQUIRY returned %x\n", c);
1449 if (c != 0)
wdenkc6097192002-11-03 00:24:07 +00001450 return;
1451
1452 /* copy device ident strings */
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001453 ident_cpy((unsigned char *) dev_desc->vendor, &iobuf[8], 8);
1454 ident_cpy((unsigned char *) dev_desc->product, &iobuf[16], 16);
1455 ident_cpy((unsigned char *) dev_desc->revision, &iobuf[32], 5);
wdenkc6097192002-11-03 00:24:07 +00001456
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001457 dev_desc->lun = 0;
1458 dev_desc->lba = 0;
1459 dev_desc->blksz = 0;
1460 dev_desc->type = iobuf[0] & 0x1f;
wdenkc6097192002-11-03 00:24:07 +00001461
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001462 if ((iobuf[1] & 0x80) == 0x80)
wdenkc6097192002-11-03 00:24:07 +00001463 dev_desc->removable = 1;
1464 else
1465 dev_desc->removable = 0;
1466
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001467 memset(ccb, 0, sizeof(ccb));
1468 memset(iobuf, 0, sizeof(iobuf));
1469 ccb[0] = ATAPI_CMD_START_STOP;
1470 ccb[4] = 0x03; /* start */
wdenkc6097192002-11-03 00:24:07 +00001471
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001472 c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *) iobuf, 0);
wdenkc6097192002-11-03 00:24:07 +00001473
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001474 debug("ATAPI_CMD_START_STOP returned %x\n", c);
1475 if (c != 0)
wdenkc6097192002-11-03 00:24:07 +00001476 return;
1477
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001478 memset(ccb, 0, sizeof(ccb));
1479 memset(iobuf, 0, sizeof(iobuf));
1480 c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *) iobuf, 0);
wdenkc6097192002-11-03 00:24:07 +00001481
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001482 debug("ATAPI_CMD_UNIT_TEST_READY returned %x\n", c);
1483 if (c != 0)
wdenkc6097192002-11-03 00:24:07 +00001484 return;
1485
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001486 memset(ccb, 0, sizeof(ccb));
1487 memset(iobuf, 0, sizeof(iobuf));
1488 ccb[0] = ATAPI_CMD_READ_CAP;
1489 c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *) iobuf, 8);
1490 debug("ATAPI_CMD_READ_CAP returned %x\n", c);
1491 if (c != 0)
wdenkc6097192002-11-03 00:24:07 +00001492 return;
1493
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001494 debug("Read Cap: LBA %02X%02X%02X%02X blksize %02X%02X%02X%02X\n",
1495 iobuf[0], iobuf[1], iobuf[2], iobuf[3],
1496 iobuf[4], iobuf[5], iobuf[6], iobuf[7]);
wdenkc6097192002-11-03 00:24:07 +00001497
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001498 dev_desc->lba = ((unsigned long) iobuf[0] << 24) +
1499 ((unsigned long) iobuf[1] << 16) +
1500 ((unsigned long) iobuf[2] << 8) + ((unsigned long) iobuf[3]);
1501 dev_desc->blksz = ((unsigned long) iobuf[4] << 24) +
1502 ((unsigned long) iobuf[5] << 16) +
1503 ((unsigned long) iobuf[6] << 8) + ((unsigned long) iobuf[7]);
wdenkc35ba4e2004-03-14 22:25:36 +00001504#ifdef CONFIG_LBA48
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001505 /* ATAPI devices cannot use 48bit addressing (ATA/ATAPI v7) */
1506 dev_desc->lba48 = 0;
wdenkc35ba4e2004-03-14 22:25:36 +00001507#endif
wdenkc6097192002-11-03 00:24:07 +00001508 return;
1509}
1510
1511
1512/*
1513 * atapi_read:
1514 * we transfer only one block per command, since the multiple DRQ per
1515 * command is not yet implemented
1516 */
1517#define ATAPI_READ_MAX_BYTES 2048 /* we read max 2kbytes */
1518#define ATAPI_READ_BLOCK_SIZE 2048 /* assuming CD part */
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001519#define ATAPI_READ_MAX_BLOCK (ATAPI_READ_MAX_BYTES/ATAPI_READ_BLOCK_SIZE)
wdenkc6097192002-11-03 00:24:07 +00001520
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001521ulong atapi_read(int device, lbaint_t blknr, ulong blkcnt, void *buffer)
wdenkc6097192002-11-03 00:24:07 +00001522{
1523 ulong n = 0;
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001524 unsigned char ccb[12]; /* Command descriptor block */
wdenkc6097192002-11-03 00:24:07 +00001525 ulong cnt;
1526
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001527 debug("atapi_read dev %d start %lX, blocks %lX buffer at %lX\n",
1528 device, blknr, blkcnt, (ulong) buffer);
wdenkc6097192002-11-03 00:24:07 +00001529
1530 do {
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001531 if (blkcnt > ATAPI_READ_MAX_BLOCK)
1532 cnt = ATAPI_READ_MAX_BLOCK;
1533 else
1534 cnt = blkcnt;
wdenkc6097192002-11-03 00:24:07 +00001535
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001536 ccb[0] = ATAPI_CMD_READ_12;
1537 ccb[1] = 0; /* reserved */
1538 ccb[2] = (unsigned char) (blknr >> 24) & 0xFF; /* MSB Block */
1539 ccb[3] = (unsigned char) (blknr >> 16) & 0xFF; /* */
1540 ccb[4] = (unsigned char) (blknr >> 8) & 0xFF;
1541 ccb[5] = (unsigned char) blknr & 0xFF; /* LSB Block */
1542 ccb[6] = (unsigned char) (cnt >> 24) & 0xFF; /* MSB Block cnt */
1543 ccb[7] = (unsigned char) (cnt >> 16) & 0xFF;
1544 ccb[8] = (unsigned char) (cnt >> 8) & 0xFF;
1545 ccb[9] = (unsigned char) cnt & 0xFF; /* LSB Block */
1546 ccb[10] = 0; /* reserved */
1547 ccb[11] = 0; /* reserved */
1548
1549 if (atapi_issue_autoreq(device, ccb, 12,
1550 (unsigned char *) buffer,
1551 cnt * ATAPI_READ_BLOCK_SIZE)
1552 == 0xFF) {
wdenkc6097192002-11-03 00:24:07 +00001553 return (n);
1554 }
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001555 n += cnt;
1556 blkcnt -= cnt;
1557 blknr += cnt;
1558 buffer += (cnt * ATAPI_READ_BLOCK_SIZE);
wdenkc6097192002-11-03 00:24:07 +00001559 } while (blkcnt > 0);
1560 return (n);
1561}
1562
1563/* ------------------------------------------------------------------------- */
1564
1565#endif /* CONFIG_ATAPI */
1566
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001567U_BOOT_CMD(ide, 5, 1, do_ide,
1568 "IDE sub-system",
1569 "reset - reset IDE controller\n"
1570 "ide info - show available IDE devices\n"
1571 "ide device [dev] - show or set current device\n"
1572 "ide part [dev] - print partition table of one or all IDE devices\n"
1573 "ide read addr blk# cnt\n"
1574 "ide write addr blk# cnt - read/write `cnt'"
1575 " blocks starting at block `blk#'\n"
1576 " to/from memory address `addr'");
wdenk57b2d802003-06-27 21:31:46 +00001577
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001578U_BOOT_CMD(diskboot, 3, 1, do_diskboot,
1579 "boot from IDE device", "loadAddr dev:part");