blob: b9feb8046a112dc7f3557517470b1939368ad615 [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#ifdef CONFIG_8xx
42# include <mpc8xx.h>
43#endif
Grant Likelyffc2dd72007-02-20 09:04:34 +010044
wdenk6ea1cf02004-02-27 08:20:54 +000045#ifdef CONFIG_MPC5xxx
46#include <mpc5xxx.h>
47#endif
Grant Likelyffc2dd72007-02-20 09:04:34 +010048
wdenkc6097192002-11-03 00:24:07 +000049#include <ide.h>
50#include <ata.h>
Grant Likelyffc2dd72007-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 Likelyffc2dd72007-02-20 09:04:34 +010055
wdenk20c98a62004-04-23 20:32:05 +000056#ifdef __PPC__
57# define EIEIO __asm__ volatile ("eieio")
wdenkf1276d42005-02-03 23:00:49 +000058# define SYNC __asm__ volatile ("sync")
wdenk20c98a62004-04-23 20:32:05 +000059#else
60# define EIEIO /* nothing */
wdenkf1276d42005-02-03 23:00:49 +000061# define SYNC /* nothing */
wdenk20c98a62004-04-23 20:32:05 +000062#endif
wdenkc6097192002-11-03 00:24:07 +000063
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-VILLARD03836942008-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-VILLARD03836942008-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-VILLARD03836942008-10-16 15:01:15 +020079static int ide_bus_ok[CONFIG_SYS_IDE_MAXBUS];
wdenkc6097192002-11-03 00:24:07 +000080
Jean-Christophe PLAGNIOL-VILLARD03836942008-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 Denkb0b104a2010-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
wdenk90e7e422002-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-VILLARD03836942008-10-16 15:01:15 +0200114#ifndef CONFIG_SYS_ATA_PORT_ADDR
115#define CONFIG_SYS_ATA_PORT_ADDR(port) (port)
Heiko Schocher633e03a2007-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 Likely2089cab2007-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
wdenkc6097192002-11-03 00:24:07 +0000123
124/* ------------------------------------------------------------------------- */
125
Wolfgang Denk2bc10cf2011-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 Denk2bc10cf2011-10-29 09:41:40 +0000128 int rcode = 0;
wdenkc6097192002-11-03 00:24:07 +0000129
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000130 switch (argc) {
131 case 0:
132 case 1:
Simon Glassa06dfc72011-12-10 08:44:01 +0000133 return CMD_RET_USAGE;
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000134 case 2:
135 if (strncmp(argv[1], "res", 3) == 0) {
136 puts("\nReset IDE"
wdenkc6097192002-11-03 00:24:07 +0000137#ifdef CONFIG_IDE_8xx_DIRECT
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000138 " on PCMCIA " PCMCIA_SLOT_MSG
wdenkc6097192002-11-03 00:24:07 +0000139#endif
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000140 ": ");
wdenkc6097192002-11-03 00:24:07 +0000141
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000142 ide_init();
143 return 0;
144 } else if (strncmp(argv[1], "inf", 3) == 0) {
145 int i;
wdenkc6097192002-11-03 00:24:07 +0000146
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000147 putc('\n');
wdenkc6097192002-11-03 00:24:07 +0000148
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000149 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;
wdenkc6097192002-11-03 00:24:07 +0000156
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000157 } 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;
wdenkc6097192002-11-03 00:24:07 +0000168
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000169 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 }
wdenkc6097192002-11-03 00:24:07 +0000179 }
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000180 if (!ok) {
181 puts("\nno IDE devices available\n");
182 rcode++;
183 }
184 return rcode;
wdenkc6097192002-11-03 00:24:07 +0000185 }
Simon Glassa06dfc72011-12-10 08:44:01 +0000186 return CMD_RET_USAGE;
Wolfgang Denk2bc10cf2011-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);
wdenkc6097192002-11-03 00:24:07 +0000190
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000191 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); */
wdenkc6097192002-11-03 00:24:07 +0000198
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000199 if (ide_dev_desc[dev].type == DEV_TYPE_UNKNOWN)
200 return 1;
wdenkc6097192002-11-03 00:24:07 +0000201
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000202 curr_device = dev;
wdenkc6097192002-11-03 00:24:07 +0000203
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000204 puts("... is now current device\n");
wdenkc6097192002-11-03 00:24:07 +0000205
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000206 return 0;
207 } else if (strncmp(argv[1], "part", 4) == 0) {
208 int dev = (int) simple_strtoul(argv[2], NULL, 10);
wdenkc6097192002-11-03 00:24:07 +0000209
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000210 if (ide_dev_desc[dev].part_type != PART_TYPE_UNKNOWN) {
wdenkc6097192002-11-03 00:24:07 +0000211 print_part(&ide_dev_desc[dev]);
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000212 } else {
213 printf("\nIDE device %d not available\n",
214 dev);
215 rcode = 1;
216 }
217 return rcode;
wdenkc6097192002-11-03 00:24:07 +0000218 }
wdenkc6097192002-11-03 00:24:07 +0000219
Simon Glassa06dfc72011-12-10 08:44:01 +0000220 return CMD_RET_USAGE;
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000221 default:
222 /* at least 4 args */
wdenkc6097192002-11-03 00:24:07 +0000223
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000224 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;
wdenkc6097192002-11-03 00:24:07 +0000228
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200229#ifdef CONFIG_SYS_64BIT_LBA
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000230 lbaint_t blk = simple_strtoull(argv[3], NULL, 16);
wdenkc6097192002-11-03 00:24:07 +0000231
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000232 printf("\nIDE read: device %d block # %lld, count %ld ... ",
233 curr_device, blk, cnt);
wdenkc35ba4e2004-03-14 22:25:36 +0000234#else
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000235 lbaint_t blk = simple_strtoul(argv[3], NULL, 16);
wdenkc35ba4e2004-03-14 22:25:36 +0000236
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000237 printf("\nIDE read: device %d block # %ld, count %ld ... ",
238 curr_device, blk, cnt);
wdenkc35ba4e2004-03-14 22:25:36 +0000239#endif
wdenkc6097192002-11-03 00:24:07 +0000240
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000241 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);
wdenkc6097192002-11-03 00:24:07 +0000247
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000248 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
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200259#ifdef CONFIG_SYS_64BIT_LBA
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000260 lbaint_t blk = simple_strtoull(argv[3], NULL, 16);
wdenkc6097192002-11-03 00:24:07 +0000261
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000262 printf("\nIDE write: device %d block # %lld, count %ld ... ",
263 curr_device, blk, cnt);
wdenkc35ba4e2004-03-14 22:25:36 +0000264#else
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000265 lbaint_t blk = simple_strtoul(argv[3], NULL, 16);
wdenkc35ba4e2004-03-14 22:25:36 +0000266
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000267 printf("\nIDE write: device %d block # %ld, count %ld ... ",
268 curr_device, blk, cnt);
wdenkc35ba4e2004-03-14 22:25:36 +0000269#endif
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000270 n = ide_write(curr_device, blk, cnt, (ulong *) addr);
wdenkc6097192002-11-03 00:24:07 +0000271
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000272 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 Glassa06dfc72011-12-10 08:44:01 +0000279 return CMD_RET_USAGE;
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000280 }
wdenkc6097192002-11-03 00:24:07 +0000281
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000282 return rcode;
wdenkc6097192002-11-03 00:24:07 +0000283 }
wdenkc6097192002-11-03 00:24:07 +0000284}
285
Wolfgang Denk2bc10cf2011-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 Herringa20cbf92012-09-21 04:02:30 +0000288 return common_diskboot(cmdtp, "ide", argc, argv);
wdenkc6097192002-11-03 00:24:07 +0000289}
290
291/* ------------------------------------------------------------------------- */
292
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000293inline void __ide_outb(int dev, int port, unsigned char val)
Stefan Roese37628252008-08-06 14:05:38 +0200294{
Wolfgang Denk2bc10cf2011-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 Lind1e49942011-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 Denk2bc10cf2011-10-29 09:41:40 +0000308 outb(val, (ATA_CURR_BASE(dev) + CONFIG_SYS_ATA_PORT_ADDR(port)));
Macpaul Lind1e49942011-04-11 20:45:32 +0000309#endif
Stefan Roese37628252008-08-06 14:05:38 +0200310}
Macpaul Lind1e49942011-04-11 20:45:32 +0000311
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000312void ide_outb(int dev, int port, unsigned char val)
313 __attribute__ ((weak, alias("__ide_outb")));
Stefan Roese37628252008-08-06 14:05:38 +0200314
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000315inline unsigned char __ide_inb(int dev, int port)
Stefan Roese37628252008-08-06 14:05:38 +0200316{
317 uchar val;
Macpaul Lind1e49942011-04-11 20:45:32 +0000318
319#if defined(CONFIG_IDE_AHB)
320 val = ide_read_register(dev, port);
321#else
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000322 val = inb((ATA_CURR_BASE(dev) + CONFIG_SYS_ATA_PORT_ADDR(port)));
Macpaul Lind1e49942011-04-11 20:45:32 +0000323#endif
324
Wolfgang Denk2bc10cf2011-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 Roese37628252008-08-06 14:05:38 +0200328 return val;
329}
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000330
Kim Phillips8167f3c2009-05-19 12:53:36 -0500331unsigned char ide_inb(int dev, int port)
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000332 __attribute__ ((weak, alias("__ide_inb")));
Stefan Roese37628252008-08-06 14:05:38 +0200333
Steven A. Falco2b7574e2008-08-15 15:34:10 -0400334#ifdef CONFIG_TUNE_PIO
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000335inline int __ide_set_piomode(int pio_mode)
Steven A. Falco2b7574e2008-08-15 15:34:10 -0400336{
337 return 0;
338}
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000339
340inline int ide_set_piomode(int pio_mode)
341 __attribute__ ((weak, alias("__ide_set_piomode")));
Steven A. Falco2b7574e2008-08-15 15:34:10 -0400342#endif
343
Wolfgang Denk2bc10cf2011-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 Denk2bc10cf2011-10-29 09:41:40 +0000348
wdenk7f00b942003-12-07 23:55:12 +0000349#ifdef CONFIG_IDE_8xx_PCCARD
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000350 extern int ide_devices_found; /* Initialized in check_ide_device() */
351#endif /* CONFIG_IDE_8xx_PCCARD */
wdenk7f00b942003-12-07 23:55:12 +0000352
353#ifdef CONFIG_IDE_PREINIT
354 WATCHDOG_RESET();
355
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000356 if (ide_preinit()) {
357 puts("ide_preinit failed\n");
wdenk7f00b942003-12-07 23:55:12 +0000358 return;
359 }
Wolfgang Denk2bc10cf2011-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 Denk2bc10cf2011-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 Denk2bc10cf2011-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 Herrmann2c13c4a2012-10-09 07:01:56 +0000373#ifdef CONFIG_IDE_INIT_POSTRESET
374 WATCHDOG_RESET();
wdenkc6097192002-11-03 00:24:07 +0000375
Pavel Herrmann2c13c4a2012-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 Denk2bc10cf2011-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
wdenk4fc95692003-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 Denk2bc10cf2011-10-29 09:41:40 +0000394 ide_led((LED_IDE1 | LED_IDE2), 0); /* LED's off */
wdenk4fc95692003-02-28 00:49:47 +0000395 continue;
396 }
397#endif
Wolfgang Denk2bc10cf2011-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 Denk2bc10cf2011-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 Denk2bc10cf2011-10-29 09:41:40 +0000409 udelay(10000); /* 10 ms */
wdenkc6097192002-11-03 00:24:07 +0000410
Wolfgang Denk2bc10cf2011-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 Denk2bc10cf2011-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 Denk2bc10cf2011-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 Denk2bc10cf2011-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 Denk2bc10cf2011-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 }
wdenk452cfd62002-11-19 11:04:11 +0000438
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000439 putc('\n');
wdenkc6097192002-11-03 00:24:07 +0000440
Wolfgang Denk2bc10cf2011-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 Denk2bc10cf2011-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 Denk2bc10cf2011-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 Lind1e49942011-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 Denk2bc10cf2011-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 Denk2bc10cf2011-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 Denk2bc10cf2011-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 Denk2bc10cf2011-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 McClintock6252b4f2011-05-24 05:31:19 +0000475#ifdef CONFIG_PARTITIONS
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000476block_dev_desc_t *ide_get_dev(int dev)
wdenkc6097192002-11-03 00:24:07 +0000477{
Jean-Christophe PLAGNIOL-VILLARD03836942008-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 McClintock6252b4f2011-05-24 05:31:19 +0000480#endif
wdenkc6097192002-11-03 00:24:07 +0000481
wdenkc6097192002-11-03 00:24:07 +0000482/* ------------------------------------------------------------------------- */
483
Pavel Herrmann35ad9a82012-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
wdenk9b7f3842003-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 Rini31327152012-09-26 15:20:33 -0700495#if defined(__LITTLE_ENDIAN) || defined(CONFIG_SOC_AU1X00)
Pavel Herrmann35ad9a82012-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}
wdenk9b7f3842003-10-09 20:09:04 +0000500#else
Pavel Herrmann35ad9a82012-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 Denkfb52e952010-09-19 21:28:25 +0200503#if defined(CONFIG_CPC45)
wdenk2f99a692004-01-04 22:51:12 +0000504 uchar i;
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000505 volatile uchar *pbuf_even =
506 (uchar *) (ATA_CURR_BASE(dev) + ATA_DATA_EVEN);
507 volatile uchar *pbuf_odd =
508 (uchar *) (ATA_CURR_BASE(dev) + ATA_DATA_ODD);
509 ushort *dbuf = (ushort *) sect_buf;
wdenk2f99a692004-01-04 22:51:12 +0000510
511 while (words--) {
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000512 for (i = 0; i < 2; i++) {
513 *(((uchar *) (dbuf)) + 1) = *pbuf_even;
514 *(uchar *) dbuf = *pbuf_odd;
515 dbuf += 1;
wdenk2f99a692004-01-04 22:51:12 +0000516 }
517 }
wdenkb995b0f2005-03-06 01:21:30 +0000518#else
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000519 volatile ushort *pbuf =
520 (ushort *) (ATA_CURR_BASE(dev) + ATA_DATA_REG);
521 ushort *dbuf = (ushort *) sect_buf;
wdenkf1276d42005-02-03 23:00:49 +0000522
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000523 debug("in input swap data base for read is %lx\n",
524 (unsigned long) pbuf);
wdenkf1276d42005-02-03 23:00:49 +0000525
526 while (words--) {
Wolfgang Denk39c76422006-06-16 17:32:31 +0200527#ifdef __MIPS__
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000528 *dbuf++ = swab16p((u16 *) pbuf);
529 *dbuf++ = swab16p((u16 *) pbuf);
Heiko Schocher633e03a2007-06-22 19:11:54 +0200530#elif defined(CONFIG_PCS440EP)
531 *dbuf++ = *pbuf;
532 *dbuf++ = *pbuf;
Wolfgang Denk39c76422006-06-16 17:32:31 +0200533#else
wdenkf1276d42005-02-03 23:00:49 +0000534 *dbuf++ = ld_le16(pbuf);
535 *dbuf++ = ld_le16(pbuf);
Wolfgang Denk39c76422006-06-16 17:32:31 +0200536#endif /* !MIPS */
wdenkf1276d42005-02-03 23:00:49 +0000537 }
538#endif
wdenkc6097192002-11-03 00:24:07 +0000539}
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000540#endif /* __LITTLE_ENDIAN || CONFIG_AU1X00 */
wdenk591dda52002-11-18 00:14:45 +0000541
542
Albert Aribaud036c6b42010-08-08 05:17:05 +0530543#if defined(CONFIG_IDE_SWAP_IO)
Pavel Herrmann35ad9a82012-10-09 07:04:39 +0000544void __ide_output_data(int dev, const ulong *sect_buf, int words)
wdenkc6097192002-11-03 00:24:07 +0000545{
Wolfgang Denkfb52e952010-09-19 21:28:25 +0200546#if defined(CONFIG_CPC45)
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000547 uchar *dbuf;
548 volatile uchar *pbuf_even;
549 volatile uchar *pbuf_odd;
wdenk2f99a692004-01-04 22:51:12 +0000550
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000551 pbuf_even = (uchar *) (ATA_CURR_BASE(dev) + ATA_DATA_EVEN);
552 pbuf_odd = (uchar *) (ATA_CURR_BASE(dev) + ATA_DATA_ODD);
553 dbuf = (uchar *) sect_buf;
wdenk2f99a692004-01-04 22:51:12 +0000554 while (words--) {
wdenk20c98a62004-04-23 20:32:05 +0000555 EIEIO;
wdenk2f99a692004-01-04 22:51:12 +0000556 *pbuf_even = *dbuf++;
wdenk20c98a62004-04-23 20:32:05 +0000557 EIEIO;
wdenk2f99a692004-01-04 22:51:12 +0000558 *pbuf_odd = *dbuf++;
wdenk20c98a62004-04-23 20:32:05 +0000559 EIEIO;
wdenk2f99a692004-01-04 22:51:12 +0000560 *pbuf_even = *dbuf++;
wdenk20c98a62004-04-23 20:32:05 +0000561 EIEIO;
wdenk2f99a692004-01-04 22:51:12 +0000562 *pbuf_odd = *dbuf++;
563 }
wdenkf1276d42005-02-03 23:00:49 +0000564#else
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000565 ushort *dbuf;
566 volatile ushort *pbuf;
wdenkf1276d42005-02-03 23:00:49 +0000567
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000568 pbuf = (ushort *) (ATA_CURR_BASE(dev) + ATA_DATA_REG);
569 dbuf = (ushort *) sect_buf;
wdenkf1276d42005-02-03 23:00:49 +0000570 while (words--) {
Heiko Schocher633e03a2007-06-22 19:11:54 +0200571#if defined(CONFIG_PCS440EP)
572 /* not tested, because CF was write protected */
573 EIEIO;
574 *pbuf = ld_le16(dbuf++);
575 EIEIO;
576 *pbuf = ld_le16(dbuf++);
577#else
wdenkf1276d42005-02-03 23:00:49 +0000578 EIEIO;
579 *pbuf = *dbuf++;
580 EIEIO;
581 *pbuf = *dbuf++;
Heiko Schocher633e03a2007-06-22 19:11:54 +0200582#endif
wdenkf1276d42005-02-03 23:00:49 +0000583 }
584#endif
wdenkc6097192002-11-03 00:24:07 +0000585}
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000586#else /* ! CONFIG_IDE_SWAP_IO */
Pavel Herrmann35ad9a82012-10-09 07:04:39 +0000587void __ide_output_data(int dev, const ulong *sect_buf, int words)
wdenk591dda52002-11-18 00:14:45 +0000588{
Macpaul Lind1e49942011-04-11 20:45:32 +0000589#if defined(CONFIG_IDE_AHB)
590 ide_write_data(dev, sect_buf, words);
591#else
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000592 outsw(ATA_CURR_BASE(dev) + ATA_DATA_REG, sect_buf, words << 1);
Macpaul Lind1e49942011-04-11 20:45:32 +0000593#endif
wdenk591dda52002-11-18 00:14:45 +0000594}
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000595#endif /* CONFIG_IDE_SWAP_IO */
wdenkc6097192002-11-03 00:24:07 +0000596
Albert Aribaud036c6b42010-08-08 05:17:05 +0530597#if defined(CONFIG_IDE_SWAP_IO)
Pavel Herrmann35ad9a82012-10-09 07:04:39 +0000598void __ide_input_data(int dev, ulong *sect_buf, int words)
wdenkc6097192002-11-03 00:24:07 +0000599{
Wolfgang Denkfb52e952010-09-19 21:28:25 +0200600#if defined(CONFIG_CPC45)
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000601 uchar *dbuf;
602 volatile uchar *pbuf_even;
603 volatile uchar *pbuf_odd;
wdenk2f99a692004-01-04 22:51:12 +0000604
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000605 pbuf_even = (uchar *) (ATA_CURR_BASE(dev) + ATA_DATA_EVEN);
606 pbuf_odd = (uchar *) (ATA_CURR_BASE(dev) + ATA_DATA_ODD);
607 dbuf = (uchar *) sect_buf;
wdenk2f99a692004-01-04 22:51:12 +0000608 while (words--) {
wdenkf1276d42005-02-03 23:00:49 +0000609 *dbuf++ = *pbuf_even;
wdenk20c98a62004-04-23 20:32:05 +0000610 EIEIO;
wdenkf1276d42005-02-03 23:00:49 +0000611 SYNC;
612 *dbuf++ = *pbuf_odd;
wdenkf6925c72005-01-22 18:26:04 +0000613 EIEIO;
wdenkf1276d42005-02-03 23:00:49 +0000614 SYNC;
wdenk2f99a692004-01-04 22:51:12 +0000615 *dbuf++ = *pbuf_even;
wdenk20c98a62004-04-23 20:32:05 +0000616 EIEIO;
wdenkf1276d42005-02-03 23:00:49 +0000617 SYNC;
wdenk2f99a692004-01-04 22:51:12 +0000618 *dbuf++ = *pbuf_odd;
wdenk20c98a62004-04-23 20:32:05 +0000619 EIEIO;
wdenkf1276d42005-02-03 23:00:49 +0000620 SYNC;
621 }
622#else
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000623 ushort *dbuf;
624 volatile ushort *pbuf;
wdenkf1276d42005-02-03 23:00:49 +0000625
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000626 pbuf = (ushort *) (ATA_CURR_BASE(dev) + ATA_DATA_REG);
627 dbuf = (ushort *) sect_buf;
wdenkf1276d42005-02-03 23:00:49 +0000628
629 debug("in input data base for read is %lx\n", (unsigned long) pbuf);
630
631 while (words--) {
Heiko Schocher633e03a2007-06-22 19:11:54 +0200632#if defined(CONFIG_PCS440EP)
633 EIEIO;
634 *dbuf++ = ld_le16(pbuf);
635 EIEIO;
636 *dbuf++ = ld_le16(pbuf);
637#else
wdenk20c98a62004-04-23 20:32:05 +0000638 EIEIO;
wdenkf1276d42005-02-03 23:00:49 +0000639 *dbuf++ = *pbuf;
wdenkf6925c72005-01-22 18:26:04 +0000640 EIEIO;
wdenkf1276d42005-02-03 23:00:49 +0000641 *dbuf++ = *pbuf;
Heiko Schocher633e03a2007-06-22 19:11:54 +0200642#endif
wdenk2f99a692004-01-04 22:51:12 +0000643 }
wdenkf1276d42005-02-03 23:00:49 +0000644#endif
wdenkc6097192002-11-03 00:24:07 +0000645}
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000646#else /* ! CONFIG_IDE_SWAP_IO */
Pavel Herrmann35ad9a82012-10-09 07:04:39 +0000647void __ide_input_data(int dev, ulong *sect_buf, int words)
wdenk591dda52002-11-18 00:14:45 +0000648{
Macpaul Lind1e49942011-04-11 20:45:32 +0000649#if defined(CONFIG_IDE_AHB)
650 ide_read_data(dev, sect_buf, words);
651#else
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000652 insw(ATA_CURR_BASE(dev) + ATA_DATA_REG, sect_buf, words << 1);
Macpaul Lind1e49942011-04-11 20:45:32 +0000653#endif
wdenk591dda52002-11-18 00:14:45 +0000654}
655
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000656#endif /* CONFIG_IDE_SWAP_IO */
wdenkc6097192002-11-03 00:24:07 +0000657
658/* -------------------------------------------------------------------------
659 */
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000660static void ide_ident(block_dev_desc_t *dev_desc)
wdenkc6097192002-11-03 00:24:07 +0000661{
wdenkc6097192002-11-03 00:24:07 +0000662 unsigned char c;
Marek Vasute49757e2011-08-20 09:15:13 +0000663 hd_driveid_t iop;
wdenkc6097192002-11-03 00:24:07 +0000664
wdenke2d6d742004-09-28 20:34:50 +0000665#ifdef CONFIG_ATAPI
666 int retries = 0;
wdenk452cfd62002-11-19 11:04:11 +0000667#endif
668
Steven A. Falco2b7574e2008-08-15 15:34:10 -0400669#ifdef CONFIG_TUNE_PIO
670 int pio_mode;
671#endif
672
wdenkc6097192002-11-03 00:24:07 +0000673#if 0
674 int mode, cycle_time;
675#endif
676 int device;
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000677
678 device = dev_desc->dev;
679 printf(" Device %d: ", device);
wdenkc6097192002-11-03 00:24:07 +0000680
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000681 ide_led(DEVICE_LED(device), 1); /* LED on */
wdenkc6097192002-11-03 00:24:07 +0000682 /* Select device
683 */
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000684 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
685 dev_desc->if_type = IF_TYPE_IDE;
wdenkc6097192002-11-03 00:24:07 +0000686#ifdef CONFIG_ATAPI
wdenk452cfd62002-11-19 11:04:11 +0000687
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000688 retries = 0;
wdenk452cfd62002-11-19 11:04:11 +0000689
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000690 /* Warning: This will be tricky to read */
691 while (retries <= 1) {
692 /* check signature */
693 if ((ide_inb(device, ATA_SECT_CNT) == 0x01) &&
694 (ide_inb(device, ATA_SECT_NUM) == 0x01) &&
695 (ide_inb(device, ATA_CYL_LOW) == 0x14) &&
696 (ide_inb(device, ATA_CYL_HIGH) == 0xEB)) {
697 /* ATAPI Signature found */
698 dev_desc->if_type = IF_TYPE_ATAPI;
699 /*
700 * Start Ident Command
701 */
702 ide_outb(device, ATA_COMMAND, ATAPI_CMD_IDENT);
703 /*
704 * Wait for completion - ATAPI devices need more time
705 * to become ready
706 */
707 c = ide_wait(device, ATAPI_TIME_OUT);
708 } else
wdenkc6097192002-11-03 00:24:07 +0000709#endif
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000710 {
711 /*
712 * Start Ident Command
713 */
714 ide_outb(device, ATA_COMMAND, ATA_CMD_IDENT);
wdenkc6097192002-11-03 00:24:07 +0000715
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000716 /*
717 * Wait for completion
718 */
719 c = ide_wait(device, IDE_TIME_OUT);
720 }
721 ide_led(DEVICE_LED(device), 0); /* LED off */
wdenkc6097192002-11-03 00:24:07 +0000722
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000723 if (((c & ATA_STAT_DRQ) == 0) ||
724 ((c & (ATA_STAT_FAULT | ATA_STAT_ERR)) != 0)) {
wdenke2d6d742004-09-28 20:34:50 +0000725#ifdef CONFIG_ATAPI
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000726 {
727 /*
728 * Need to soft reset the device
729 * in case it's an ATAPI...
730 */
731 debug("Retrying...\n");
732 ide_outb(device, ATA_DEV_HD,
733 ATA_LBA | ATA_DEVICE(device));
734 udelay(100000);
735 ide_outb(device, ATA_COMMAND, 0x08);
736 udelay(500000); /* 500 ms */
737 }
738 /*
739 * Select device
740 */
741 ide_outb(device, ATA_DEV_HD,
742 ATA_LBA | ATA_DEVICE(device));
743 retries++;
wdenke2d6d742004-09-28 20:34:50 +0000744#else
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000745 return;
wdenke2d6d742004-09-28 20:34:50 +0000746#endif
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000747 }
wdenke2d6d742004-09-28 20:34:50 +0000748#ifdef CONFIG_ATAPI
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000749 else
750 break;
751 } /* see above - ugly to read */
wdenke2d6d742004-09-28 20:34:50 +0000752
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000753 if (retries == 2) /* Not found */
wdenke2d6d742004-09-28 20:34:50 +0000754 return;
755#endif
wdenkc6097192002-11-03 00:24:07 +0000756
Pavel Herrmann35ad9a82012-10-09 07:04:39 +0000757 ide_input_swap_data(device, (ulong *)&iop, ATA_SECTORWORDS);
wdenkc6097192002-11-03 00:24:07 +0000758
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000759 ident_cpy((unsigned char *) dev_desc->revision, iop.fw_rev,
760 sizeof(dev_desc->revision));
761 ident_cpy((unsigned char *) dev_desc->vendor, iop.model,
762 sizeof(dev_desc->vendor));
763 ident_cpy((unsigned char *) dev_desc->product, iop.serial_no,
764 sizeof(dev_desc->product));
wdenkacd9b102004-03-14 00:59:59 +0000765#ifdef __LITTLE_ENDIAN
766 /*
Richard Retanubun54f30622008-11-06 14:01:51 -0500767 * firmware revision, model, and serial number have Big Endian Byte
768 * order in Word. Convert all three to little endian.
wdenkacd9b102004-03-14 00:59:59 +0000769 *
770 * See CF+ and CompactFlash Specification Revision 2.0:
Richard Retanubun54f30622008-11-06 14:01:51 -0500771 * 6.2.1.6: Identify Drive, Table 39 for more details
wdenkacd9b102004-03-14 00:59:59 +0000772 */
773
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000774 strswab(dev_desc->revision);
775 strswab(dev_desc->vendor);
776 strswab(dev_desc->product);
wdenkacd9b102004-03-14 00:59:59 +0000777#endif /* __LITTLE_ENDIAN */
wdenkc6097192002-11-03 00:24:07 +0000778
Marek Vasute49757e2011-08-20 09:15:13 +0000779 if ((iop.config & 0x0080) == 0x0080)
wdenkc6097192002-11-03 00:24:07 +0000780 dev_desc->removable = 1;
781 else
782 dev_desc->removable = 0;
783
Steven A. Falco2b7574e2008-08-15 15:34:10 -0400784#ifdef CONFIG_TUNE_PIO
785 /* Mode 0 - 2 only, are directly determined by word 51. */
Marek Vasute49757e2011-08-20 09:15:13 +0000786 pio_mode = iop.tPIO;
Steven A. Falco2b7574e2008-08-15 15:34:10 -0400787 if (pio_mode > 2) {
788 printf("WARNING: Invalid PIO (word 51 = %d).\n", pio_mode);
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000789 /* Force it to dead slow, and hope for the best... */
790 pio_mode = 0;
Steven A. Falco2b7574e2008-08-15 15:34:10 -0400791 }
792
793 /* Any CompactFlash Storage Card that supports PIO mode 3 or above
794 * shall set bit 1 of word 53 to one and support the fields contained
795 * in words 64 through 70.
796 */
Marek Vasute49757e2011-08-20 09:15:13 +0000797 if (iop.field_valid & 0x02) {
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000798 /*
799 * Mode 3 and above are possible. Check in order from slow
Steven A. Falco2b7574e2008-08-15 15:34:10 -0400800 * to fast, so we wind up with the highest mode allowed.
801 */
Marek Vasute49757e2011-08-20 09:15:13 +0000802 if (iop.eide_pio_modes & 0x01)
Steven A. Falco2b7574e2008-08-15 15:34:10 -0400803 pio_mode = 3;
Marek Vasute49757e2011-08-20 09:15:13 +0000804 if (iop.eide_pio_modes & 0x02)
Steven A. Falco2b7574e2008-08-15 15:34:10 -0400805 pio_mode = 4;
Marek Vasute49757e2011-08-20 09:15:13 +0000806 if (ata_id_is_cfa((u16 *)&iop)) {
807 if ((iop.cf_advanced_caps & 0x07) == 0x01)
Steven A. Falco2b7574e2008-08-15 15:34:10 -0400808 pio_mode = 5;
Marek Vasute49757e2011-08-20 09:15:13 +0000809 if ((iop.cf_advanced_caps & 0x07) == 0x02)
Steven A. Falco2b7574e2008-08-15 15:34:10 -0400810 pio_mode = 6;
811 }
812 }
813
814 /* System-specific, depends on bus speeds, etc. */
815 ide_set_piomode(pio_mode);
816#endif /* CONFIG_TUNE_PIO */
817
wdenkc6097192002-11-03 00:24:07 +0000818#if 0
819 /*
820 * Drive PIO mode autoselection
821 */
Marek Vasute49757e2011-08-20 09:15:13 +0000822 mode = iop.tPIO;
wdenkc6097192002-11-03 00:24:07 +0000823
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000824 printf("tPIO = 0x%02x = %d\n", mode, mode);
wdenkc6097192002-11-03 00:24:07 +0000825 if (mode > 2) { /* 2 is maximum allowed tPIO value */
826 mode = 2;
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000827 debug("Override tPIO -> 2\n");
wdenkc6097192002-11-03 00:24:07 +0000828 }
Marek Vasute49757e2011-08-20 09:15:13 +0000829 if (iop.field_valid & 2) { /* drive implements ATA2? */
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000830 debug("Drive implements ATA2\n");
Marek Vasute49757e2011-08-20 09:15:13 +0000831 if (iop.capability & 8) { /* drive supports use_iordy? */
832 cycle_time = iop.eide_pio_iordy;
wdenkc6097192002-11-03 00:24:07 +0000833 } else {
Marek Vasute49757e2011-08-20 09:15:13 +0000834 cycle_time = iop.eide_pio;
wdenkc6097192002-11-03 00:24:07 +0000835 }
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000836 debug("cycle time = %d\n", cycle_time);
wdenkc6097192002-11-03 00:24:07 +0000837 mode = 4;
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000838 if (cycle_time > 120)
839 mode = 3; /* 120 ns for PIO mode 4 */
840 if (cycle_time > 180)
841 mode = 2; /* 180 ns for PIO mode 3 */
842 if (cycle_time > 240)
843 mode = 1; /* 240 ns for PIO mode 4 */
844 if (cycle_time > 383)
845 mode = 0; /* 383 ns for PIO mode 4 */
wdenkc6097192002-11-03 00:24:07 +0000846 }
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000847 printf("PIO mode to use: PIO %d\n", mode);
wdenkc6097192002-11-03 00:24:07 +0000848#endif /* 0 */
849
850#ifdef CONFIG_ATAPI
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000851 if (dev_desc->if_type == IF_TYPE_ATAPI) {
wdenkc6097192002-11-03 00:24:07 +0000852 atapi_inquiry(dev_desc);
853 return;
854 }
855#endif /* CONFIG_ATAPI */
856
wdenkacd9b102004-03-14 00:59:59 +0000857#ifdef __BIG_ENDIAN
wdenkc6097192002-11-03 00:24:07 +0000858 /* swap shorts */
Marek Vasute49757e2011-08-20 09:15:13 +0000859 dev_desc->lba = (iop.lba_capacity << 16) | (iop.lba_capacity >> 16);
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000860#else /* ! __BIG_ENDIAN */
wdenkacd9b102004-03-14 00:59:59 +0000861 /*
862 * do not swap shorts on little endian
863 *
864 * See CF+ and CompactFlash Specification Revision 2.0:
865 * 6.2.1.6: Identfy Drive, Table 39, Word Address 57-58 for details.
866 */
Marek Vasute49757e2011-08-20 09:15:13 +0000867 dev_desc->lba = iop.lba_capacity;
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000868#endif /* __BIG_ENDIAN */
wdenkf602aa02004-03-13 23:29:43 +0000869
wdenkc35ba4e2004-03-14 22:25:36 +0000870#ifdef CONFIG_LBA48
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000871 if (iop.command_set_2 & 0x0400) { /* LBA 48 support */
wdenk05939202004-04-18 17:39:38 +0000872 dev_desc->lba48 = 1;
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000873 dev_desc->lba = (unsigned long long) iop.lba48_capacity[0] |
874 ((unsigned long long) iop.lba48_capacity[1] << 16) |
875 ((unsigned long long) iop.lba48_capacity[2] << 32) |
876 ((unsigned long long) iop.lba48_capacity[3] << 48);
wdenkf602aa02004-03-13 23:29:43 +0000877 } else {
wdenkf602aa02004-03-13 23:29:43 +0000878 dev_desc->lba48 = 0;
879 }
880#endif /* CONFIG_LBA48 */
wdenkc6097192002-11-03 00:24:07 +0000881 /* assuming HD */
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000882 dev_desc->type = DEV_TYPE_HARDDISK;
883 dev_desc->blksz = ATA_BLOCKSIZE;
884 dev_desc->lun = 0; /* just to fill something in... */
wdenkc6097192002-11-03 00:24:07 +0000885
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000886#if 0 /* only used to test the powersaving mode,
887 * if enabled, the drive goes after 5 sec
888 * in standby mode */
889 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
890 c = ide_wait(device, IDE_TIME_OUT);
891 ide_outb(device, ATA_SECT_CNT, 1);
892 ide_outb(device, ATA_LBA_LOW, 0);
893 ide_outb(device, ATA_LBA_MID, 0);
894 ide_outb(device, ATA_LBA_HIGH, 0);
895 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
896 ide_outb(device, ATA_COMMAND, 0xe3);
897 udelay(50);
898 c = ide_wait(device, IDE_TIME_OUT); /* can't take over 500 ms */
wdenkc6097192002-11-03 00:24:07 +0000899#endif
900}
901
902
903/* ------------------------------------------------------------------------- */
904
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000905ulong ide_read(int device, lbaint_t blknr, ulong blkcnt, void *buffer)
wdenkc6097192002-11-03 00:24:07 +0000906{
907 ulong n = 0;
908 unsigned char c;
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000909 unsigned char pwrsave = 0; /* power save */
910
wdenkc35ba4e2004-03-14 22:25:36 +0000911#ifdef CONFIG_LBA48
wdenkf602aa02004-03-13 23:29:43 +0000912 unsigned char lba48 = 0;
wdenkc6097192002-11-03 00:24:07 +0000913
Guennadi Liakhovetskib71f1ca2008-04-28 14:36:06 +0200914 if (blknr & 0x0000fffff0000000ULL) {
wdenkf602aa02004-03-13 23:29:43 +0000915 /* more than 28 bits used, use 48bit mode */
916 lba48 = 1;
917 }
918#endif
Marek Vasut92131712011-10-25 11:39:15 +0200919 debug("ide_read dev %d start %lX, blocks %lX buffer at %lX\n",
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000920 device, blknr, blkcnt, (ulong) buffer);
wdenkc6097192002-11-03 00:24:07 +0000921
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000922 ide_led(DEVICE_LED(device), 1); /* LED on */
wdenkc6097192002-11-03 00:24:07 +0000923
924 /* Select device
925 */
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000926 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
927 c = ide_wait(device, IDE_TIME_OUT);
wdenkc6097192002-11-03 00:24:07 +0000928
929 if (c & ATA_STAT_BUSY) {
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000930 printf("IDE read: device %d not ready\n", device);
wdenkc6097192002-11-03 00:24:07 +0000931 goto IDE_READ_E;
932 }
933
934 /* first check if the drive is in Powersaving mode, if yes,
935 * increase the timeout value */
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000936 ide_outb(device, ATA_COMMAND, ATA_CMD_CHK_PWR);
937 udelay(50);
wdenkc6097192002-11-03 00:24:07 +0000938
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000939 c = ide_wait(device, IDE_TIME_OUT); /* can't take over 500 ms */
wdenkc6097192002-11-03 00:24:07 +0000940
941 if (c & ATA_STAT_BUSY) {
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000942 printf("IDE read: device %d not ready\n", device);
wdenkc6097192002-11-03 00:24:07 +0000943 goto IDE_READ_E;
944 }
945 if ((c & ATA_STAT_ERR) == ATA_STAT_ERR) {
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000946 printf("No Powersaving mode %X\n", c);
wdenkc6097192002-11-03 00:24:07 +0000947 } else {
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000948 c = ide_inb(device, ATA_SECT_CNT);
949 debug("Powersaving %02X\n", c);
950 if (c == 0)
951 pwrsave = 1;
wdenkc6097192002-11-03 00:24:07 +0000952 }
953
954
955 while (blkcnt-- > 0) {
956
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000957 c = ide_wait(device, IDE_TIME_OUT);
wdenkc6097192002-11-03 00:24:07 +0000958
959 if (c & ATA_STAT_BUSY) {
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000960 printf("IDE read: device %d not ready\n", device);
wdenkc6097192002-11-03 00:24:07 +0000961 break;
962 }
wdenkc35ba4e2004-03-14 22:25:36 +0000963#ifdef CONFIG_LBA48
wdenkf602aa02004-03-13 23:29:43 +0000964 if (lba48) {
965 /* write high bits */
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000966 ide_outb(device, ATA_SECT_CNT, 0);
967 ide_outb(device, ATA_LBA_LOW, (blknr >> 24) & 0xFF);
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200968#ifdef CONFIG_SYS_64BIT_LBA
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000969 ide_outb(device, ATA_LBA_MID, (blknr >> 32) & 0xFF);
970 ide_outb(device, ATA_LBA_HIGH, (blknr >> 40) & 0xFF);
Guennadi Liakhovetskib71f1ca2008-04-28 14:36:06 +0200971#else
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000972 ide_outb(device, ATA_LBA_MID, 0);
973 ide_outb(device, ATA_LBA_HIGH, 0);
Guennadi Liakhovetskib71f1ca2008-04-28 14:36:06 +0200974#endif
wdenkf602aa02004-03-13 23:29:43 +0000975 }
976#endif
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000977 ide_outb(device, ATA_SECT_CNT, 1);
978 ide_outb(device, ATA_LBA_LOW, (blknr >> 0) & 0xFF);
979 ide_outb(device, ATA_LBA_MID, (blknr >> 8) & 0xFF);
980 ide_outb(device, ATA_LBA_HIGH, (blknr >> 16) & 0xFF);
wdenkf602aa02004-03-13 23:29:43 +0000981
wdenkc35ba4e2004-03-14 22:25:36 +0000982#ifdef CONFIG_LBA48
wdenkf602aa02004-03-13 23:29:43 +0000983 if (lba48) {
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000984 ide_outb(device, ATA_DEV_HD,
985 ATA_LBA | ATA_DEVICE(device));
986 ide_outb(device, ATA_COMMAND, ATA_CMD_READ_EXT);
wdenkf602aa02004-03-13 23:29:43 +0000987
988 } else
989#endif
990 {
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000991 ide_outb(device, ATA_DEV_HD, ATA_LBA |
992 ATA_DEVICE(device) | ((blknr >> 24) & 0xF));
993 ide_outb(device, ATA_COMMAND, ATA_CMD_READ);
wdenkf602aa02004-03-13 23:29:43 +0000994 }
wdenkc6097192002-11-03 00:24:07 +0000995
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000996 udelay(50);
wdenkc6097192002-11-03 00:24:07 +0000997
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000998 if (pwrsave) {
999 /* may take up to 4 sec */
1000 c = ide_wait(device, IDE_SPIN_UP_TIME_OUT);
1001 pwrsave = 0;
wdenkc6097192002-11-03 00:24:07 +00001002 } else {
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001003 /* can't take over 500 ms */
1004 c = ide_wait(device, IDE_TIME_OUT);
wdenkc6097192002-11-03 00:24:07 +00001005 }
1006
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001007 if ((c & (ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR)) !=
1008 ATA_STAT_DRQ) {
Heiko Schocher0f602e12009-12-03 11:21:21 +01001009#if defined(CONFIG_SYS_64BIT_LBA)
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001010 printf("Error (no IRQ) dev %d blk %lld: status 0x%02x\n",
wdenkc6097192002-11-03 00:24:07 +00001011 device, blknr, c);
wdenkf602aa02004-03-13 23:29:43 +00001012#else
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001013 printf("Error (no IRQ) dev %d blk %ld: status 0x%02x\n",
1014 device, (ulong) blknr, c);
wdenkf602aa02004-03-13 23:29:43 +00001015#endif
wdenkc6097192002-11-03 00:24:07 +00001016 break;
1017 }
1018
Pavel Herrmann35ad9a82012-10-09 07:04:39 +00001019 ide_input_data(device, buffer, ATA_SECTORWORDS);
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001020 (void) ide_inb(device, ATA_STATUS); /* clear IRQ */
wdenkc6097192002-11-03 00:24:07 +00001021
1022 ++n;
1023 ++blknr;
Greg Lopp50abf9f2007-04-13 08:02:24 +02001024 buffer += ATA_BLOCKSIZE;
wdenkc6097192002-11-03 00:24:07 +00001025 }
1026IDE_READ_E:
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001027 ide_led(DEVICE_LED(device), 0); /* LED off */
wdenkc6097192002-11-03 00:24:07 +00001028 return (n);
1029}
1030
1031/* ------------------------------------------------------------------------- */
1032
1033
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001034ulong ide_write(int device, lbaint_t blknr, ulong blkcnt, const void *buffer)
wdenkc6097192002-11-03 00:24:07 +00001035{
1036 ulong n = 0;
1037 unsigned char c;
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001038
wdenkc35ba4e2004-03-14 22:25:36 +00001039#ifdef CONFIG_LBA48
wdenkf602aa02004-03-13 23:29:43 +00001040 unsigned char lba48 = 0;
1041
Guennadi Liakhovetskib71f1ca2008-04-28 14:36:06 +02001042 if (blknr & 0x0000fffff0000000ULL) {
wdenkf602aa02004-03-13 23:29:43 +00001043 /* more than 28 bits used, use 48bit mode */
1044 lba48 = 1;
1045 }
1046#endif
wdenkc6097192002-11-03 00:24:07 +00001047
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001048 ide_led(DEVICE_LED(device), 1); /* LED on */
wdenkc6097192002-11-03 00:24:07 +00001049
1050 /* Select device
1051 */
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001052 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
wdenkc6097192002-11-03 00:24:07 +00001053
1054 while (blkcnt-- > 0) {
1055
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001056 c = ide_wait(device, IDE_TIME_OUT);
wdenkc6097192002-11-03 00:24:07 +00001057
1058 if (c & ATA_STAT_BUSY) {
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001059 printf("IDE read: device %d not ready\n", device);
wdenkc6097192002-11-03 00:24:07 +00001060 goto WR_OUT;
1061 }
wdenkc35ba4e2004-03-14 22:25:36 +00001062#ifdef CONFIG_LBA48
wdenkf602aa02004-03-13 23:29:43 +00001063 if (lba48) {
1064 /* write high bits */
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001065 ide_outb(device, ATA_SECT_CNT, 0);
1066 ide_outb(device, ATA_LBA_LOW, (blknr >> 24) & 0xFF);
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001067#ifdef CONFIG_SYS_64BIT_LBA
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001068 ide_outb(device, ATA_LBA_MID, (blknr >> 32) & 0xFF);
1069 ide_outb(device, ATA_LBA_HIGH, (blknr >> 40) & 0xFF);
Guennadi Liakhovetskib71f1ca2008-04-28 14:36:06 +02001070#else
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001071 ide_outb(device, ATA_LBA_MID, 0);
1072 ide_outb(device, ATA_LBA_HIGH, 0);
Guennadi Liakhovetskib71f1ca2008-04-28 14:36:06 +02001073#endif
wdenkf602aa02004-03-13 23:29:43 +00001074 }
1075#endif
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001076 ide_outb(device, ATA_SECT_CNT, 1);
1077 ide_outb(device, ATA_LBA_LOW, (blknr >> 0) & 0xFF);
1078 ide_outb(device, ATA_LBA_MID, (blknr >> 8) & 0xFF);
1079 ide_outb(device, ATA_LBA_HIGH, (blknr >> 16) & 0xFF);
wdenkf602aa02004-03-13 23:29:43 +00001080
wdenkc35ba4e2004-03-14 22:25:36 +00001081#ifdef CONFIG_LBA48
wdenkf602aa02004-03-13 23:29:43 +00001082 if (lba48) {
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001083 ide_outb(device, ATA_DEV_HD,
1084 ATA_LBA | ATA_DEVICE(device));
1085 ide_outb(device, ATA_COMMAND, ATA_CMD_WRITE_EXT);
wdenkf602aa02004-03-13 23:29:43 +00001086
1087 } else
1088#endif
1089 {
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001090 ide_outb(device, ATA_DEV_HD, ATA_LBA |
1091 ATA_DEVICE(device) | ((blknr >> 24) & 0xF));
1092 ide_outb(device, ATA_COMMAND, ATA_CMD_WRITE);
wdenkf602aa02004-03-13 23:29:43 +00001093 }
wdenkc6097192002-11-03 00:24:07 +00001094
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001095 udelay(50);
wdenkc6097192002-11-03 00:24:07 +00001096
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001097 /* can't take over 500 ms */
1098 c = ide_wait(device, IDE_TIME_OUT);
wdenkc6097192002-11-03 00:24:07 +00001099
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001100 if ((c & (ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR)) !=
1101 ATA_STAT_DRQ) {
Heiko Schocher0f602e12009-12-03 11:21:21 +01001102#if defined(CONFIG_SYS_64BIT_LBA)
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001103 printf("Error (no IRQ) dev %d blk %lld: status 0x%02x\n",
wdenkc6097192002-11-03 00:24:07 +00001104 device, blknr, c);
wdenkf602aa02004-03-13 23:29:43 +00001105#else
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001106 printf("Error (no IRQ) dev %d blk %ld: status 0x%02x\n",
1107 device, (ulong) blknr, c);
wdenkf602aa02004-03-13 23:29:43 +00001108#endif
wdenkc6097192002-11-03 00:24:07 +00001109 goto WR_OUT;
1110 }
1111
Pavel Herrmann35ad9a82012-10-09 07:04:39 +00001112 ide_output_data(device, buffer, ATA_SECTORWORDS);
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001113 c = ide_inb(device, ATA_STATUS); /* clear IRQ */
wdenkc6097192002-11-03 00:24:07 +00001114 ++n;
1115 ++blknr;
Greg Lopp50abf9f2007-04-13 08:02:24 +02001116 buffer += ATA_BLOCKSIZE;
wdenkc6097192002-11-03 00:24:07 +00001117 }
1118WR_OUT:
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001119 ide_led(DEVICE_LED(device), 0); /* LED off */
wdenkc6097192002-11-03 00:24:07 +00001120 return (n);
1121}
1122
1123/* ------------------------------------------------------------------------- */
1124
1125/*
1126 * copy src to dest, skipping leading and trailing blanks and null
1127 * terminate the string
wdenk18bd81e2004-03-17 01:13:07 +00001128 * "len" is the size of available memory including the terminating '\0'
wdenkc6097192002-11-03 00:24:07 +00001129 */
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001130static void ident_cpy(unsigned char *dst, unsigned char *src,
1131 unsigned int len)
wdenkc6097192002-11-03 00:24:07 +00001132{
wdenk18bd81e2004-03-17 01:13:07 +00001133 unsigned char *end, *last;
wdenkc6097192002-11-03 00:24:07 +00001134
wdenk18bd81e2004-03-17 01:13:07 +00001135 last = dst;
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001136 end = src + len - 1;
wdenk18bd81e2004-03-17 01:13:07 +00001137
1138 /* reserve space for '\0' */
1139 if (len < 2)
1140 goto OUT;
wdenkc12081a2004-03-23 20:18:25 +00001141
wdenk18bd81e2004-03-17 01:13:07 +00001142 /* skip leading white space */
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001143 while ((*src) && (src < end) && (*src == ' '))
wdenk18bd81e2004-03-17 01:13:07 +00001144 ++src;
1145
1146 /* copy string, omitting trailing white space */
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001147 while ((*src) && (src < end)) {
wdenk18bd81e2004-03-17 01:13:07 +00001148 *dst++ = *src;
1149 if (*src++ != ' ')
1150 last = dst;
wdenkc6097192002-11-03 00:24:07 +00001151 }
wdenk18bd81e2004-03-17 01:13:07 +00001152OUT:
1153 *last = '\0';
wdenkc6097192002-11-03 00:24:07 +00001154}
1155
1156/* ------------------------------------------------------------------------- */
1157
1158/*
1159 * Wait until Busy bit is off, or timeout (in ms)
1160 * Return last status
1161 */
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001162static uchar ide_wait(int dev, ulong t)
wdenkc6097192002-11-03 00:24:07 +00001163{
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001164 ulong delay = 10 * t; /* poll every 100 us */
wdenkc6097192002-11-03 00:24:07 +00001165 uchar c;
1166
wdenk591dda52002-11-18 00:14:45 +00001167 while ((c = ide_inb(dev, ATA_STATUS)) & ATA_STAT_BUSY) {
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001168 udelay(100);
1169 if (delay-- == 0)
wdenkc6097192002-11-03 00:24:07 +00001170 break;
wdenkc6097192002-11-03 00:24:07 +00001171 }
1172 return (c);
1173}
1174
1175/* ------------------------------------------------------------------------- */
1176
1177#ifdef CONFIG_IDE_RESET
1178extern void ide_set_reset(int idereset);
1179
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001180static void ide_reset(void)
wdenkc6097192002-11-03 00:24:07 +00001181{
wdenkc6097192002-11-03 00:24:07 +00001182 int i;
1183
1184 curr_device = -1;
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001185 for (i = 0; i < CONFIG_SYS_IDE_MAXBUS; ++i)
wdenkc6097192002-11-03 00:24:07 +00001186 ide_bus_ok[i] = 0;
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001187 for (i = 0; i < CONFIG_SYS_IDE_MAXDEVICE; ++i)
wdenkc6097192002-11-03 00:24:07 +00001188 ide_dev_desc[i].type = DEV_TYPE_UNKNOWN;
1189
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001190 ide_set_reset(1); /* assert reset */
wdenkc6097192002-11-03 00:24:07 +00001191
Martin Krause7e35caf2008-04-03 13:37:56 +02001192 /* the reset signal shall be asserted for et least 25 us */
1193 udelay(25);
1194
wdenkc6097192002-11-03 00:24:07 +00001195 WATCHDOG_RESET();
1196
wdenkc6097192002-11-03 00:24:07 +00001197 /* de-assert RESET signal */
1198 ide_set_reset(0);
1199
1200 /* wait 250 ms */
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001201 for (i = 0; i < 250; ++i)
1202 udelay(1000);
wdenkc6097192002-11-03 00:24:07 +00001203}
1204
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001205#endif /* CONFIG_IDE_RESET */
wdenkc6097192002-11-03 00:24:07 +00001206
1207/* ------------------------------------------------------------------------- */
1208
wdenk54070ab2004-12-31 09:32:47 +00001209#if defined(CONFIG_IDE_LED) && \
wdenk54070ab2004-12-31 09:32:47 +00001210 !defined(CONFIG_CPC45) && \
wdenk54070ab2004-12-31 09:32:47 +00001211 !defined(CONFIG_KUP4K) && \
1212 !defined(CONFIG_KUP4X)
wdenkc6097192002-11-03 00:24:07 +00001213
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001214static uchar led_buffer; /* Buffer for current LED status */
wdenkc6097192002-11-03 00:24:07 +00001215
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001216static void ide_led(uchar led, uchar status)
wdenkc6097192002-11-03 00:24:07 +00001217{
1218 uchar *led_port = LED_PORT;
1219
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001220 if (status) /* switch LED on */
1221 led_buffer |= led;
1222 else /* switch LED off */
wdenkc6097192002-11-03 00:24:07 +00001223 led_buffer &= ~led;
wdenkc6097192002-11-03 00:24:07 +00001224
1225 *led_port = led_buffer;
1226}
1227
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001228#endif /* CONFIG_IDE_LED */
wdenkc6097192002-11-03 00:24:07 +00001229
Heiko Schocherffb293a2009-09-23 07:56:08 +02001230#if defined(CONFIG_OF_IDE_FIXUP)
1231int ide_device_present(int dev)
1232{
1233 if (dev >= CONFIG_SYS_IDE_MAXBUS)
1234 return 0;
1235 return (ide_dev_desc[dev].type == DEV_TYPE_UNKNOWN ? 0 : 1);
1236}
1237#endif
wdenkc6097192002-11-03 00:24:07 +00001238/* ------------------------------------------------------------------------- */
1239
1240#ifdef CONFIG_ATAPI
1241/****************************************************************************
1242 * ATAPI Support
1243 */
1244
Pavel Herrmann35ad9a82012-10-09 07:04:39 +00001245void ide_input_data_shorts(int dev, ushort *sect_buf, int shorts)
1246 __attribute__ ((weak, alias("__ide_input_data_shorts")));
1247
1248void ide_output_data_shorts(int dev, ushort *sect_buf, int shorts)
1249 __attribute__ ((weak, alias("__ide_output_data_shorts")));
1250
1251
Albert Aribaud036c6b42010-08-08 05:17:05 +05301252#if defined(CONFIG_IDE_SWAP_IO)
wdenkc6097192002-11-03 00:24:07 +00001253/* since ATAPI may use commands with not 4 bytes alligned length
1254 * we have our own transfer functions, 2 bytes alligned */
Pavel Herrmann35ad9a82012-10-09 07:04:39 +00001255void __ide_output_data_shorts(int dev, ushort *sect_buf, int shorts)
wdenkc6097192002-11-03 00:24:07 +00001256{
Wolfgang Denkfb52e952010-09-19 21:28:25 +02001257#if defined(CONFIG_CPC45)
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001258 uchar *dbuf;
1259 volatile uchar *pbuf_even;
1260 volatile uchar *pbuf_odd;
wdenk2f99a692004-01-04 22:51:12 +00001261
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001262 pbuf_even = (uchar *) (ATA_CURR_BASE(dev) + ATA_DATA_EVEN);
1263 pbuf_odd = (uchar *) (ATA_CURR_BASE(dev) + ATA_DATA_ODD);
wdenk2f99a692004-01-04 22:51:12 +00001264 while (shorts--) {
wdenk20c98a62004-04-23 20:32:05 +00001265 EIEIO;
wdenk2f99a692004-01-04 22:51:12 +00001266 *pbuf_even = *dbuf++;
wdenk20c98a62004-04-23 20:32:05 +00001267 EIEIO;
wdenk2f99a692004-01-04 22:51:12 +00001268 *pbuf_odd = *dbuf++;
1269 }
wdenkf1276d42005-02-03 23:00:49 +00001270#else
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001271 ushort *dbuf;
1272 volatile ushort *pbuf;
wdenkc6097192002-11-03 00:24:07 +00001273
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001274 pbuf = (ushort *) (ATA_CURR_BASE(dev) + ATA_DATA_REG);
1275 dbuf = (ushort *) sect_buf;
wdenk634d2f72004-04-15 23:14:49 +00001276
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001277 debug("in output data shorts base for read is %lx\n",
1278 (unsigned long) pbuf);
wdenk634d2f72004-04-15 23:14:49 +00001279
wdenkc6097192002-11-03 00:24:07 +00001280 while (shorts--) {
wdenk20c98a62004-04-23 20:32:05 +00001281 EIEIO;
wdenkf1276d42005-02-03 23:00:49 +00001282 *pbuf = *dbuf++;
wdenkc6097192002-11-03 00:24:07 +00001283 }
wdenkf1276d42005-02-03 23:00:49 +00001284#endif
1285}
1286
Pavel Herrmann35ad9a82012-10-09 07:04:39 +00001287void __ide_input_data_shorts(int dev, ushort *sect_buf, int shorts)
wdenkf1276d42005-02-03 23:00:49 +00001288{
Wolfgang Denkfb52e952010-09-19 21:28:25 +02001289#if defined(CONFIG_CPC45)
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001290 uchar *dbuf;
1291 volatile uchar *pbuf_even;
1292 volatile uchar *pbuf_odd;
wdenk2f99a692004-01-04 22:51:12 +00001293
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001294 pbuf_even = (uchar *) (ATA_CURR_BASE(dev) + ATA_DATA_EVEN);
1295 pbuf_odd = (uchar *) (ATA_CURR_BASE(dev) + ATA_DATA_ODD);
wdenk2f99a692004-01-04 22:51:12 +00001296 while (shorts--) {
wdenk20c98a62004-04-23 20:32:05 +00001297 EIEIO;
wdenk2f99a692004-01-04 22:51:12 +00001298 *dbuf++ = *pbuf_even;
wdenk20c98a62004-04-23 20:32:05 +00001299 EIEIO;
wdenk2f99a692004-01-04 22:51:12 +00001300 *dbuf++ = *pbuf_odd;
1301 }
wdenkf1276d42005-02-03 23:00:49 +00001302#else
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001303 ushort *dbuf;
1304 volatile ushort *pbuf;
wdenkf1276d42005-02-03 23:00:49 +00001305
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001306 pbuf = (ushort *) (ATA_CURR_BASE(dev) + ATA_DATA_REG);
1307 dbuf = (ushort *) sect_buf;
wdenkf1276d42005-02-03 23:00:49 +00001308
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001309 debug("in input data shorts base for read is %lx\n",
1310 (unsigned long) pbuf);
wdenkf1276d42005-02-03 23:00:49 +00001311
1312 while (shorts--) {
1313 EIEIO;
1314 *dbuf++ = *pbuf;
1315 }
1316#endif
wdenkc6097192002-11-03 00:24:07 +00001317}
1318
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001319#else /* ! CONFIG_IDE_SWAP_IO */
Pavel Herrmann35ad9a82012-10-09 07:04:39 +00001320void __ide_output_data_shorts(int dev, ushort *sect_buf, int shorts)
wdenk591dda52002-11-18 00:14:45 +00001321{
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001322 outsw(ATA_CURR_BASE(dev) + ATA_DATA_REG, sect_buf, shorts);
wdenk591dda52002-11-18 00:14:45 +00001323}
1324
Pavel Herrmann35ad9a82012-10-09 07:04:39 +00001325void __ide_input_data_shorts(int dev, ushort *sect_buf, int shorts)
wdenk591dda52002-11-18 00:14:45 +00001326{
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001327 insw(ATA_CURR_BASE(dev) + ATA_DATA_REG, sect_buf, shorts);
wdenk591dda52002-11-18 00:14:45 +00001328}
1329
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001330#endif /* CONFIG_IDE_SWAP_IO */
wdenk591dda52002-11-18 00:14:45 +00001331
wdenkc6097192002-11-03 00:24:07 +00001332/*
1333 * Wait until (Status & mask) == res, or timeout (in ms)
1334 * Return last status
1335 * This is used since some ATAPI CD ROMs clears their Busy Bit first
1336 * and then they set their DRQ Bit
1337 */
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001338static uchar atapi_wait_mask(int dev, ulong t, uchar mask, uchar res)
wdenkc6097192002-11-03 00:24:07 +00001339{
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001340 ulong delay = 10 * t; /* poll every 100 us */
wdenkc6097192002-11-03 00:24:07 +00001341 uchar c;
1342
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001343 /* prevents to read the status before valid */
1344 c = ide_inb(dev, ATA_DEV_CTL);
1345
wdenk591dda52002-11-18 00:14:45 +00001346 while (((c = ide_inb(dev, ATA_STATUS)) & mask) != res) {
wdenkc6097192002-11-03 00:24:07 +00001347 /* break if error occurs (doesn't make sense to wait more) */
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001348 if ((c & ATA_STAT_ERR) == ATA_STAT_ERR)
wdenkc6097192002-11-03 00:24:07 +00001349 break;
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001350 udelay(100);
1351 if (delay-- == 0)
wdenkc6097192002-11-03 00:24:07 +00001352 break;
wdenkc6097192002-11-03 00:24:07 +00001353 }
1354 return (c);
1355}
1356
1357/*
1358 * issue an atapi command
1359 */
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001360unsigned char atapi_issue(int device, unsigned char *ccb, int ccblen,
1361 unsigned char *buffer, int buflen)
wdenkc6097192002-11-03 00:24:07 +00001362{
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001363 unsigned char c, err, mask, res;
wdenkc6097192002-11-03 00:24:07 +00001364 int n;
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001365
1366 ide_led(DEVICE_LED(device), 1); /* LED on */
wdenkc6097192002-11-03 00:24:07 +00001367
1368 /* Select device
1369 */
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001370 mask = ATA_STAT_BUSY | ATA_STAT_DRQ;
wdenkc6097192002-11-03 00:24:07 +00001371 res = 0;
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001372 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
1373 c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
wdenkc6097192002-11-03 00:24:07 +00001374 if ((c & mask) != res) {
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001375 printf("ATAPI_ISSUE: device %d not ready status %X\n", device,
1376 c);
1377 err = 0xFF;
wdenkc6097192002-11-03 00:24:07 +00001378 goto AI_OUT;
1379 }
1380 /* write taskfile */
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001381 ide_outb(device, ATA_ERROR_REG, 0); /* no DMA, no overlaped */
1382 ide_outb(device, ATA_SECT_CNT, 0);
1383 ide_outb(device, ATA_SECT_NUM, 0);
1384 ide_outb(device, ATA_CYL_LOW, (unsigned char) (buflen & 0xFF));
1385 ide_outb(device, ATA_CYL_HIGH,
1386 (unsigned char) ((buflen >> 8) & 0xFF));
1387 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
wdenkc6097192002-11-03 00:24:07 +00001388
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001389 ide_outb(device, ATA_COMMAND, ATAPI_CMD_PACKET);
1390 udelay(50);
wdenkc6097192002-11-03 00:24:07 +00001391
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001392 mask = ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR;
wdenkc6097192002-11-03 00:24:07 +00001393 res = ATA_STAT_DRQ;
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001394 c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
wdenkc6097192002-11-03 00:24:07 +00001395
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001396 if ((c & mask) != res) { /* DRQ must be 1, BSY 0 */
1397 printf("ATAPI_ISSUE: Error (no IRQ) before sending ccb dev %d status 0x%02x\n",
1398 device, c);
1399 err = 0xFF;
wdenkc6097192002-11-03 00:24:07 +00001400 goto AI_OUT;
1401 }
1402
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001403 /* write command block */
Pavel Herrmann35ad9a82012-10-09 07:04:39 +00001404 ide_output_data_shorts(device, (unsigned short *) ccb, ccblen / 2);
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001405
Wolfgang Denka1be4762008-05-20 16:00:29 +02001406 /* ATAPI Command written wait for completition */
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001407 udelay(5000); /* device must set bsy */
wdenkc6097192002-11-03 00:24:07 +00001408
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001409 mask = ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR;
1410 /*
1411 * if no data wait for DRQ = 0 BSY = 0
1412 * if data wait for DRQ = 1 BSY = 0
1413 */
1414 res = 0;
1415 if (buflen)
wdenkc6097192002-11-03 00:24:07 +00001416 res = ATA_STAT_DRQ;
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001417 c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
1418 if ((c & mask) != res) {
wdenkc6097192002-11-03 00:24:07 +00001419 if (c & ATA_STAT_ERR) {
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001420 err = (ide_inb(device, ATA_ERROR_REG)) >> 4;
1421 debug("atapi_issue 1 returned sense key %X status %02X\n",
1422 err, c);
wdenkc6097192002-11-03 00:24:07 +00001423 } else {
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001424 printf("ATAPI_ISSUE: (no DRQ) after sending ccb (%x) status 0x%02x\n",
1425 ccb[0], c);
1426 err = 0xFF;
wdenkc6097192002-11-03 00:24:07 +00001427 }
1428 goto AI_OUT;
1429 }
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001430 n = ide_inb(device, ATA_CYL_HIGH);
1431 n <<= 8;
1432 n += ide_inb(device, ATA_CYL_LOW);
1433 if (n > buflen) {
1434 printf("ERROR, transfer bytes %d requested only %d\n", n,
1435 buflen);
1436 err = 0xff;
wdenkc6097192002-11-03 00:24:07 +00001437 goto AI_OUT;
1438 }
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001439 if ((n == 0) && (buflen < 0)) {
1440 printf("ERROR, transfer bytes %d requested %d\n", n, buflen);
1441 err = 0xff;
wdenkc6097192002-11-03 00:24:07 +00001442 goto AI_OUT;
1443 }
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001444 if (n != buflen) {
1445 debug("WARNING, transfer bytes %d not equal with requested %d\n",
1446 n, buflen);
wdenkc6097192002-11-03 00:24:07 +00001447 }
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001448 if (n != 0) { /* data transfer */
1449 debug("ATAPI_ISSUE: %d Bytes to transfer\n", n);
1450 /* we transfer shorts */
1451 n >>= 1;
wdenkc6097192002-11-03 00:24:07 +00001452 /* ok now decide if it is an in or output */
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001453 if ((ide_inb(device, ATA_SECT_CNT) & 0x02) == 0) {
1454 debug("Write to device\n");
Pavel Herrmann35ad9a82012-10-09 07:04:39 +00001455 ide_output_data_shorts(device,
1456 (unsigned short *) buffer, n);
wdenkc6097192002-11-03 00:24:07 +00001457 } else {
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001458 debug("Read from device @ %p shorts %d\n", buffer, n);
Pavel Herrmann35ad9a82012-10-09 07:04:39 +00001459 ide_input_data_shorts(device,
1460 (unsigned short *) buffer, n);
wdenkc6097192002-11-03 00:24:07 +00001461 }
1462 }
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001463 udelay(5000); /* seems that some CD ROMs need this... */
1464 mask = ATA_STAT_BUSY | ATA_STAT_ERR;
1465 res = 0;
1466 c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
wdenkc6097192002-11-03 00:24:07 +00001467 if ((c & ATA_STAT_ERR) == ATA_STAT_ERR) {
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001468 err = (ide_inb(device, ATA_ERROR_REG) >> 4);
1469 debug("atapi_issue 2 returned sense key %X status %X\n", err,
1470 c);
wdenkc6097192002-11-03 00:24:07 +00001471 } else {
1472 err = 0;
1473 }
1474AI_OUT:
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001475 ide_led(DEVICE_LED(device), 0); /* LED off */
wdenkc6097192002-11-03 00:24:07 +00001476 return (err);
1477}
1478
1479/*
1480 * sending the command to atapi_issue. If an status other than good
1481 * returns, an request_sense will be issued
1482 */
1483
Wolfgang Denka1be4762008-05-20 16:00:29 +02001484#define ATAPI_DRIVE_NOT_READY 100
wdenkc6097192002-11-03 00:24:07 +00001485#define ATAPI_UNIT_ATTN 10
1486
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001487unsigned char atapi_issue_autoreq(int device,
1488 unsigned char *ccb,
1489 int ccblen,
1490 unsigned char *buffer, int buflen)
wdenkc6097192002-11-03 00:24:07 +00001491{
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001492 unsigned char sense_data[18], sense_ccb[12];
1493 unsigned char res, key, asc, ascq;
1494 int notready, unitattn;
wdenkc6097192002-11-03 00:24:07 +00001495
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001496 unitattn = ATAPI_UNIT_ATTN;
1497 notready = ATAPI_DRIVE_NOT_READY;
wdenkc6097192002-11-03 00:24:07 +00001498
1499retry:
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001500 res = atapi_issue(device, ccb, ccblen, buffer, buflen);
1501 if (res == 0)
1502 return 0; /* Ok */
wdenkc6097192002-11-03 00:24:07 +00001503
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001504 if (res == 0xFF)
1505 return 0xFF; /* error */
wdenkc6097192002-11-03 00:24:07 +00001506
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001507 debug("(auto_req)atapi_issue returned sense key %X\n", res);
wdenkc6097192002-11-03 00:24:07 +00001508
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001509 memset(sense_ccb, 0, sizeof(sense_ccb));
1510 memset(sense_data, 0, sizeof(sense_data));
1511 sense_ccb[0] = ATAPI_CMD_REQ_SENSE;
1512 sense_ccb[4] = 18; /* allocation Length */
wdenkc6097192002-11-03 00:24:07 +00001513
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001514 res = atapi_issue(device, sense_ccb, 12, sense_data, 18);
1515 key = (sense_data[2] & 0xF);
1516 asc = (sense_data[12]);
1517 ascq = (sense_data[13]);
wdenkc6097192002-11-03 00:24:07 +00001518
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001519 debug("ATAPI_CMD_REQ_SENSE returned %x\n", res);
1520 debug(" Sense page: %02X key %02X ASC %02X ASCQ %02X\n",
1521 sense_data[0], key, asc, ascq);
wdenkc6097192002-11-03 00:24:07 +00001522
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001523 if ((key == 0))
1524 return 0; /* ok device ready */
wdenkc6097192002-11-03 00:24:07 +00001525
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001526 if ((key == 6) || (asc == 0x29) || (asc == 0x28)) { /* Unit Attention */
1527 if (unitattn-- > 0) {
1528 udelay(200 * 1000);
wdenkc6097192002-11-03 00:24:07 +00001529 goto retry;
1530 }
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001531 printf("Unit Attention, tried %d\n", ATAPI_UNIT_ATTN);
wdenkc6097192002-11-03 00:24:07 +00001532 goto error;
1533 }
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001534 if ((asc == 0x4) && (ascq == 0x1)) {
1535 /* not ready, but will be ready soon */
1536 if (notready-- > 0) {
1537 udelay(200 * 1000);
wdenkc6097192002-11-03 00:24:07 +00001538 goto retry;
1539 }
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001540 printf("Drive not ready, tried %d times\n",
1541 ATAPI_DRIVE_NOT_READY);
wdenkc6097192002-11-03 00:24:07 +00001542 goto error;
1543 }
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001544 if (asc == 0x3a) {
1545 debug("Media not present\n");
wdenkc6097192002-11-03 00:24:07 +00001546 goto error;
1547 }
wdenk452cfd62002-11-19 11:04:11 +00001548
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001549 printf("ERROR: Unknown Sense key %02X ASC %02X ASCQ %02X\n", key, asc,
1550 ascq);
wdenkc6097192002-11-03 00:24:07 +00001551error:
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001552 debug("ERROR Sense key %02X ASC %02X ASCQ %02X\n", key, asc, ascq);
wdenkc6097192002-11-03 00:24:07 +00001553 return (0xFF);
1554}
1555
1556
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001557static void atapi_inquiry(block_dev_desc_t *dev_desc)
wdenkc6097192002-11-03 00:24:07 +00001558{
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001559 unsigned char ccb[12]; /* Command descriptor block */
1560 unsigned char iobuf[64]; /* temp buf */
wdenkc6097192002-11-03 00:24:07 +00001561 unsigned char c;
1562 int device;
1563
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001564 device = dev_desc->dev;
1565 dev_desc->type = DEV_TYPE_UNKNOWN; /* not yet valid */
1566 dev_desc->block_read = atapi_read;
wdenkc6097192002-11-03 00:24:07 +00001567
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001568 memset(ccb, 0, sizeof(ccb));
1569 memset(iobuf, 0, sizeof(iobuf));
wdenkc6097192002-11-03 00:24:07 +00001570
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001571 ccb[0] = ATAPI_CMD_INQUIRY;
1572 ccb[4] = 40; /* allocation Legnth */
1573 c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *) iobuf, 40);
wdenkc6097192002-11-03 00:24:07 +00001574
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001575 debug("ATAPI_CMD_INQUIRY returned %x\n", c);
1576 if (c != 0)
wdenkc6097192002-11-03 00:24:07 +00001577 return;
1578
1579 /* copy device ident strings */
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001580 ident_cpy((unsigned char *) dev_desc->vendor, &iobuf[8], 8);
1581 ident_cpy((unsigned char *) dev_desc->product, &iobuf[16], 16);
1582 ident_cpy((unsigned char *) dev_desc->revision, &iobuf[32], 5);
wdenkc6097192002-11-03 00:24:07 +00001583
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001584 dev_desc->lun = 0;
1585 dev_desc->lba = 0;
1586 dev_desc->blksz = 0;
1587 dev_desc->type = iobuf[0] & 0x1f;
wdenkc6097192002-11-03 00:24:07 +00001588
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001589 if ((iobuf[1] & 0x80) == 0x80)
wdenkc6097192002-11-03 00:24:07 +00001590 dev_desc->removable = 1;
1591 else
1592 dev_desc->removable = 0;
1593
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001594 memset(ccb, 0, sizeof(ccb));
1595 memset(iobuf, 0, sizeof(iobuf));
1596 ccb[0] = ATAPI_CMD_START_STOP;
1597 ccb[4] = 0x03; /* start */
wdenkc6097192002-11-03 00:24:07 +00001598
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001599 c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *) iobuf, 0);
wdenkc6097192002-11-03 00:24:07 +00001600
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001601 debug("ATAPI_CMD_START_STOP returned %x\n", c);
1602 if (c != 0)
wdenkc6097192002-11-03 00:24:07 +00001603 return;
1604
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001605 memset(ccb, 0, sizeof(ccb));
1606 memset(iobuf, 0, sizeof(iobuf));
1607 c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *) iobuf, 0);
wdenkc6097192002-11-03 00:24:07 +00001608
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001609 debug("ATAPI_CMD_UNIT_TEST_READY returned %x\n", c);
1610 if (c != 0)
wdenkc6097192002-11-03 00:24:07 +00001611 return;
1612
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001613 memset(ccb, 0, sizeof(ccb));
1614 memset(iobuf, 0, sizeof(iobuf));
1615 ccb[0] = ATAPI_CMD_READ_CAP;
1616 c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *) iobuf, 8);
1617 debug("ATAPI_CMD_READ_CAP returned %x\n", c);
1618 if (c != 0)
wdenkc6097192002-11-03 00:24:07 +00001619 return;
1620
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001621 debug("Read Cap: LBA %02X%02X%02X%02X blksize %02X%02X%02X%02X\n",
1622 iobuf[0], iobuf[1], iobuf[2], iobuf[3],
1623 iobuf[4], iobuf[5], iobuf[6], iobuf[7]);
wdenkc6097192002-11-03 00:24:07 +00001624
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001625 dev_desc->lba = ((unsigned long) iobuf[0] << 24) +
1626 ((unsigned long) iobuf[1] << 16) +
1627 ((unsigned long) iobuf[2] << 8) + ((unsigned long) iobuf[3]);
1628 dev_desc->blksz = ((unsigned long) iobuf[4] << 24) +
1629 ((unsigned long) iobuf[5] << 16) +
1630 ((unsigned long) iobuf[6] << 8) + ((unsigned long) iobuf[7]);
wdenkc35ba4e2004-03-14 22:25:36 +00001631#ifdef CONFIG_LBA48
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001632 /* ATAPI devices cannot use 48bit addressing (ATA/ATAPI v7) */
1633 dev_desc->lba48 = 0;
wdenkc35ba4e2004-03-14 22:25:36 +00001634#endif
wdenkc6097192002-11-03 00:24:07 +00001635 return;
1636}
1637
1638
1639/*
1640 * atapi_read:
1641 * we transfer only one block per command, since the multiple DRQ per
1642 * command is not yet implemented
1643 */
1644#define ATAPI_READ_MAX_BYTES 2048 /* we read max 2kbytes */
1645#define ATAPI_READ_BLOCK_SIZE 2048 /* assuming CD part */
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001646#define ATAPI_READ_MAX_BLOCK (ATAPI_READ_MAX_BYTES/ATAPI_READ_BLOCK_SIZE)
wdenkc6097192002-11-03 00:24:07 +00001647
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001648ulong atapi_read(int device, lbaint_t blknr, ulong blkcnt, void *buffer)
wdenkc6097192002-11-03 00:24:07 +00001649{
1650 ulong n = 0;
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001651 unsigned char ccb[12]; /* Command descriptor block */
wdenkc6097192002-11-03 00:24:07 +00001652 ulong cnt;
1653
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001654 debug("atapi_read dev %d start %lX, blocks %lX buffer at %lX\n",
1655 device, blknr, blkcnt, (ulong) buffer);
wdenkc6097192002-11-03 00:24:07 +00001656
1657 do {
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001658 if (blkcnt > ATAPI_READ_MAX_BLOCK)
1659 cnt = ATAPI_READ_MAX_BLOCK;
1660 else
1661 cnt = blkcnt;
wdenkc6097192002-11-03 00:24:07 +00001662
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001663 ccb[0] = ATAPI_CMD_READ_12;
1664 ccb[1] = 0; /* reserved */
1665 ccb[2] = (unsigned char) (blknr >> 24) & 0xFF; /* MSB Block */
1666 ccb[3] = (unsigned char) (blknr >> 16) & 0xFF; /* */
1667 ccb[4] = (unsigned char) (blknr >> 8) & 0xFF;
1668 ccb[5] = (unsigned char) blknr & 0xFF; /* LSB Block */
1669 ccb[6] = (unsigned char) (cnt >> 24) & 0xFF; /* MSB Block cnt */
1670 ccb[7] = (unsigned char) (cnt >> 16) & 0xFF;
1671 ccb[8] = (unsigned char) (cnt >> 8) & 0xFF;
1672 ccb[9] = (unsigned char) cnt & 0xFF; /* LSB Block */
1673 ccb[10] = 0; /* reserved */
1674 ccb[11] = 0; /* reserved */
1675
1676 if (atapi_issue_autoreq(device, ccb, 12,
1677 (unsigned char *) buffer,
1678 cnt * ATAPI_READ_BLOCK_SIZE)
1679 == 0xFF) {
wdenkc6097192002-11-03 00:24:07 +00001680 return (n);
1681 }
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001682 n += cnt;
1683 blkcnt -= cnt;
1684 blknr += cnt;
1685 buffer += (cnt * ATAPI_READ_BLOCK_SIZE);
wdenkc6097192002-11-03 00:24:07 +00001686 } while (blkcnt > 0);
1687 return (n);
1688}
1689
1690/* ------------------------------------------------------------------------- */
1691
1692#endif /* CONFIG_ATAPI */
1693
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001694U_BOOT_CMD(ide, 5, 1, do_ide,
1695 "IDE sub-system",
1696 "reset - reset IDE controller\n"
1697 "ide info - show available IDE devices\n"
1698 "ide device [dev] - show or set current device\n"
1699 "ide part [dev] - print partition table of one or all IDE devices\n"
1700 "ide read addr blk# cnt\n"
1701 "ide write addr blk# cnt - read/write `cnt'"
1702 " blocks starting at block `blk#'\n"
1703 " to/from memory address `addr'");
wdenk57b2d802003-06-27 21:31:46 +00001704
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001705U_BOOT_CMD(diskboot, 3, 1, do_diskboot,
1706 "boot from IDE device", "loadAddr dev:part");