blob: 78b4aa70ba7d05da59b0b1f14381a3102d6f4cf1 [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);
Simon Glass11926fb2012-10-29 05:24:04 +000099static ulong atapi_read(int device, ulong blknr, lbaint_t blkcnt,
100 void *buffer);
wdenkc6097192002-11-03 00:24:07 +0000101#endif
102
wdenkc6097192002-11-03 00:24:07 +0000103
104/* ------------------------------------------------------------------------- */
105
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000106int do_ide(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
wdenkc6097192002-11-03 00:24:07 +0000107{
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000108 int rcode = 0;
wdenkc6097192002-11-03 00:24:07 +0000109
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000110 switch (argc) {
111 case 0:
112 case 1:
Simon Glassa06dfc72011-12-10 08:44:01 +0000113 return CMD_RET_USAGE;
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000114 case 2:
115 if (strncmp(argv[1], "res", 3) == 0) {
116 puts("\nReset IDE"
wdenkc6097192002-11-03 00:24:07 +0000117#ifdef CONFIG_IDE_8xx_DIRECT
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000118 " on PCMCIA " PCMCIA_SLOT_MSG
wdenkc6097192002-11-03 00:24:07 +0000119#endif
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000120 ": ");
wdenkc6097192002-11-03 00:24:07 +0000121
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000122 ide_init();
123 return 0;
124 } else if (strncmp(argv[1], "inf", 3) == 0) {
125 int i;
wdenkc6097192002-11-03 00:24:07 +0000126
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000127 putc('\n');
wdenkc6097192002-11-03 00:24:07 +0000128
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000129 for (i = 0; i < CONFIG_SYS_IDE_MAXDEVICE; ++i) {
130 if (ide_dev_desc[i].type == DEV_TYPE_UNKNOWN)
131 continue; /* list only known devices */
132 printf("IDE device %d: ", i);
133 dev_print(&ide_dev_desc[i]);
134 }
135 return 0;
wdenkc6097192002-11-03 00:24:07 +0000136
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000137 } else if (strncmp(argv[1], "dev", 3) == 0) {
138 if ((curr_device < 0)
139 || (curr_device >= CONFIG_SYS_IDE_MAXDEVICE)) {
140 puts("\nno IDE devices available\n");
141 return 1;
142 }
143 printf("\nIDE device %d: ", curr_device);
144 dev_print(&ide_dev_desc[curr_device]);
145 return 0;
146 } else if (strncmp(argv[1], "part", 4) == 0) {
147 int dev, ok;
wdenkc6097192002-11-03 00:24:07 +0000148
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000149 for (ok = 0, dev = 0;
150 dev < CONFIG_SYS_IDE_MAXDEVICE;
151 ++dev) {
152 if (ide_dev_desc[dev].part_type !=
153 PART_TYPE_UNKNOWN) {
154 ++ok;
155 if (dev)
156 putc('\n');
157 print_part(&ide_dev_desc[dev]);
158 }
wdenkc6097192002-11-03 00:24:07 +0000159 }
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000160 if (!ok) {
161 puts("\nno IDE devices available\n");
162 rcode++;
163 }
164 return rcode;
wdenkc6097192002-11-03 00:24:07 +0000165 }
Simon Glassa06dfc72011-12-10 08:44:01 +0000166 return CMD_RET_USAGE;
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000167 case 3:
168 if (strncmp(argv[1], "dev", 3) == 0) {
169 int dev = (int) simple_strtoul(argv[2], NULL, 10);
wdenkc6097192002-11-03 00:24:07 +0000170
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000171 printf("\nIDE device %d: ", dev);
172 if (dev >= CONFIG_SYS_IDE_MAXDEVICE) {
173 puts("unknown device\n");
174 return 1;
175 }
176 dev_print(&ide_dev_desc[dev]);
177 /*ide_print (dev); */
wdenkc6097192002-11-03 00:24:07 +0000178
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000179 if (ide_dev_desc[dev].type == DEV_TYPE_UNKNOWN)
180 return 1;
wdenkc6097192002-11-03 00:24:07 +0000181
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000182 curr_device = dev;
wdenkc6097192002-11-03 00:24:07 +0000183
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000184 puts("... is now current device\n");
wdenkc6097192002-11-03 00:24:07 +0000185
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000186 return 0;
187 } else if (strncmp(argv[1], "part", 4) == 0) {
188 int dev = (int) simple_strtoul(argv[2], NULL, 10);
wdenkc6097192002-11-03 00:24:07 +0000189
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000190 if (ide_dev_desc[dev].part_type != PART_TYPE_UNKNOWN) {
wdenkc6097192002-11-03 00:24:07 +0000191 print_part(&ide_dev_desc[dev]);
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000192 } else {
193 printf("\nIDE device %d not available\n",
194 dev);
195 rcode = 1;
196 }
197 return rcode;
wdenkc6097192002-11-03 00:24:07 +0000198 }
wdenkc6097192002-11-03 00:24:07 +0000199
Simon Glassa06dfc72011-12-10 08:44:01 +0000200 return CMD_RET_USAGE;
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000201 default:
202 /* at least 4 args */
wdenkc6097192002-11-03 00:24:07 +0000203
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000204 if (strcmp(argv[1], "read") == 0) {
205 ulong addr = simple_strtoul(argv[2], NULL, 16);
206 ulong cnt = simple_strtoul(argv[4], NULL, 16);
207 ulong n;
wdenkc6097192002-11-03 00:24:07 +0000208
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200209#ifdef CONFIG_SYS_64BIT_LBA
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000210 lbaint_t blk = simple_strtoull(argv[3], NULL, 16);
wdenkc6097192002-11-03 00:24:07 +0000211
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000212 printf("\nIDE read: device %d block # %lld, count %ld ... ",
213 curr_device, blk, cnt);
wdenkc35ba4e2004-03-14 22:25:36 +0000214#else
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000215 lbaint_t blk = simple_strtoul(argv[3], NULL, 16);
wdenkc35ba4e2004-03-14 22:25:36 +0000216
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000217 printf("\nIDE read: device %d block # %ld, count %ld ... ",
218 curr_device, blk, cnt);
wdenkc35ba4e2004-03-14 22:25:36 +0000219#endif
wdenkc6097192002-11-03 00:24:07 +0000220
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000221 n = ide_dev_desc[curr_device].block_read(curr_device,
222 blk, cnt,
223 (ulong *)addr);
224 /* flush cache after read */
225 flush_cache(addr,
226 cnt * ide_dev_desc[curr_device].blksz);
wdenkc6097192002-11-03 00:24:07 +0000227
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000228 printf("%ld blocks read: %s\n",
229 n, (n == cnt) ? "OK" : "ERROR");
230 if (n == cnt)
231 return 0;
232 else
233 return 1;
234 } else if (strcmp(argv[1], "write") == 0) {
235 ulong addr = simple_strtoul(argv[2], NULL, 16);
236 ulong cnt = simple_strtoul(argv[4], NULL, 16);
237 ulong n;
238
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200239#ifdef CONFIG_SYS_64BIT_LBA
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000240 lbaint_t blk = simple_strtoull(argv[3], NULL, 16);
wdenkc6097192002-11-03 00:24:07 +0000241
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000242 printf("\nIDE write: device %d block # %lld, count %ld ... ",
243 curr_device, blk, cnt);
wdenkc35ba4e2004-03-14 22:25:36 +0000244#else
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000245 lbaint_t blk = simple_strtoul(argv[3], NULL, 16);
wdenkc35ba4e2004-03-14 22:25:36 +0000246
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000247 printf("\nIDE write: device %d block # %ld, count %ld ... ",
248 curr_device, blk, cnt);
wdenkc35ba4e2004-03-14 22:25:36 +0000249#endif
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000250 n = ide_write(curr_device, blk, cnt, (ulong *) addr);
wdenkc6097192002-11-03 00:24:07 +0000251
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000252 printf("%ld blocks written: %s\n",
253 n, (n == cnt) ? "OK" : "ERROR");
254 if (n == cnt)
255 return 0;
256 else
257 return 1;
258 } else {
Simon Glassa06dfc72011-12-10 08:44:01 +0000259 return CMD_RET_USAGE;
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000260 }
wdenkc6097192002-11-03 00:24:07 +0000261
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000262 return rcode;
wdenkc6097192002-11-03 00:24:07 +0000263 }
wdenkc6097192002-11-03 00:24:07 +0000264}
265
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000266int do_diskboot(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
wdenkc6097192002-11-03 00:24:07 +0000267{
Rob Herringa20cbf92012-09-21 04:02:30 +0000268 return common_diskboot(cmdtp, "ide", argc, argv);
wdenkc6097192002-11-03 00:24:07 +0000269}
270
Pavel Herrmann95b0cb52012-10-07 05:56:10 +0000271/* ------------------------------------------------------------------------- */
272
273void __ide_led(uchar led, uchar status)
274{
275#if defined(CONFIG_IDE_LED) && defined(PER8_BASE) /* required by LED_PORT */
276 static uchar led_buffer; /* Buffer for current LED status */
277
278 uchar *led_port = LED_PORT;
279
280 if (status) /* switch LED on */
281 led_buffer |= led;
282 else /* switch LED off */
283 led_buffer &= ~led;
284
285 *led_port = led_buffer;
286#endif
287}
288
289void ide_led(uchar led, uchar status)
290 __attribute__ ((weak, alias("__ide_led")));
291
292#ifndef CONFIG_IDE_LED /* define LED macros, they are not used anyways */
293# define DEVICE_LED(x) 0
294# define LED_IDE1 1
295# define LED_IDE2 2
296#endif
297
wdenkc6097192002-11-03 00:24:07 +0000298/* ------------------------------------------------------------------------- */
299
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000300inline void __ide_outb(int dev, int port, unsigned char val)
Stefan Roese37628252008-08-06 14:05:38 +0200301{
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000302 debug("ide_outb (dev= %d, port= 0x%x, val= 0x%02x) : @ 0x%08lx\n",
303 dev, port, val,
304 (ATA_CURR_BASE(dev) + CONFIG_SYS_ATA_PORT_ADDR(port)));
Macpaul Lind1e49942011-04-11 20:45:32 +0000305
306#if defined(CONFIG_IDE_AHB)
307 if (port) {
308 /* write command */
309 ide_write_register(dev, port, val);
310 } else {
311 /* write data */
312 outb(val, (ATA_CURR_BASE(dev)));
313 }
314#else
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000315 outb(val, (ATA_CURR_BASE(dev) + CONFIG_SYS_ATA_PORT_ADDR(port)));
Macpaul Lind1e49942011-04-11 20:45:32 +0000316#endif
Stefan Roese37628252008-08-06 14:05:38 +0200317}
Macpaul Lind1e49942011-04-11 20:45:32 +0000318
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000319void ide_outb(int dev, int port, unsigned char val)
320 __attribute__ ((weak, alias("__ide_outb")));
Stefan Roese37628252008-08-06 14:05:38 +0200321
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000322inline unsigned char __ide_inb(int dev, int port)
Stefan Roese37628252008-08-06 14:05:38 +0200323{
324 uchar val;
Macpaul Lind1e49942011-04-11 20:45:32 +0000325
326#if defined(CONFIG_IDE_AHB)
327 val = ide_read_register(dev, port);
328#else
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000329 val = inb((ATA_CURR_BASE(dev) + CONFIG_SYS_ATA_PORT_ADDR(port)));
Macpaul Lind1e49942011-04-11 20:45:32 +0000330#endif
331
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000332 debug("ide_inb (dev= %d, port= 0x%x) : @ 0x%08lx -> 0x%02x\n",
333 dev, port,
334 (ATA_CURR_BASE(dev) + CONFIG_SYS_ATA_PORT_ADDR(port)), val);
Stefan Roese37628252008-08-06 14:05:38 +0200335 return val;
336}
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000337
Kim Phillips8167f3c2009-05-19 12:53:36 -0500338unsigned char ide_inb(int dev, int port)
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000339 __attribute__ ((weak, alias("__ide_inb")));
Stefan Roese37628252008-08-06 14:05:38 +0200340
Steven A. Falco2b7574e2008-08-15 15:34:10 -0400341#ifdef CONFIG_TUNE_PIO
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000342inline int __ide_set_piomode(int pio_mode)
Steven A. Falco2b7574e2008-08-15 15:34:10 -0400343{
344 return 0;
345}
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000346
347inline int ide_set_piomode(int pio_mode)
348 __attribute__ ((weak, alias("__ide_set_piomode")));
Steven A. Falco2b7574e2008-08-15 15:34:10 -0400349#endif
350
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000351void ide_init(void)
wdenkc6097192002-11-03 00:24:07 +0000352{
wdenkc6097192002-11-03 00:24:07 +0000353 unsigned char c;
354 int i, bus;
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000355
wdenk7f00b942003-12-07 23:55:12 +0000356#ifdef CONFIG_IDE_8xx_PCCARD
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000357 extern int ide_devices_found; /* Initialized in check_ide_device() */
358#endif /* CONFIG_IDE_8xx_PCCARD */
wdenk7f00b942003-12-07 23:55:12 +0000359
360#ifdef CONFIG_IDE_PREINIT
361 WATCHDOG_RESET();
362
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000363 if (ide_preinit()) {
364 puts("ide_preinit failed\n");
wdenk7f00b942003-12-07 23:55:12 +0000365 return;
366 }
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000367#endif /* CONFIG_IDE_PREINIT */
wdenkc6097192002-11-03 00:24:07 +0000368
wdenkc6097192002-11-03 00:24:07 +0000369 WATCHDOG_RESET();
370
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000371 /*
372 * Reset the IDE just to be sure.
wdenkc6097192002-11-03 00:24:07 +0000373 * Light LED's to show
374 */
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000375 ide_led((LED_IDE1 | LED_IDE2), 1); /* LED's on */
376
377 /* ATAPI Drives seems to need a proper IDE Reset */
378 ide_reset();
wdenkc6097192002-11-03 00:24:07 +0000379
Pavel Herrmann2c13c4a2012-10-09 07:01:56 +0000380#ifdef CONFIG_IDE_INIT_POSTRESET
381 WATCHDOG_RESET();
wdenkc6097192002-11-03 00:24:07 +0000382
Pavel Herrmann2c13c4a2012-10-09 07:01:56 +0000383 if (ide_init_postreset()) {
384 puts("ide_preinit_postreset failed\n");
385 return;
386 }
387#endif /* CONFIG_IDE_INIT_POSTRESET */
wdenkc6097192002-11-03 00:24:07 +0000388
389 /*
390 * Wait for IDE to get ready.
391 * According to spec, this can take up to 31 seconds!
392 */
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000393 for (bus = 0; bus < CONFIG_SYS_IDE_MAXBUS; ++bus) {
394 int dev =
395 bus * (CONFIG_SYS_IDE_MAXDEVICE /
396 CONFIG_SYS_IDE_MAXBUS);
wdenkc6097192002-11-03 00:24:07 +0000397
wdenk4fc95692003-02-28 00:49:47 +0000398#ifdef CONFIG_IDE_8xx_PCCARD
399 /* Skip non-ide devices from probing */
400 if ((ide_devices_found & (1 << bus)) == 0) {
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000401 ide_led((LED_IDE1 | LED_IDE2), 0); /* LED's off */
wdenk4fc95692003-02-28 00:49:47 +0000402 continue;
403 }
404#endif
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000405 printf("Bus %d: ", bus);
wdenkc6097192002-11-03 00:24:07 +0000406
407 ide_bus_ok[bus] = 0;
408
409 /* Select device
410 */
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000411 udelay(100000); /* 100 ms */
412 ide_outb(dev, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(dev));
413 udelay(100000); /* 100 ms */
wdenkc6097192002-11-03 00:24:07 +0000414 i = 0;
415 do {
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000416 udelay(10000); /* 10 ms */
wdenkc6097192002-11-03 00:24:07 +0000417
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000418 c = ide_inb(dev, ATA_STATUS);
wdenkc6097192002-11-03 00:24:07 +0000419 i++;
420 if (i > (ATA_RESET_TIME * 100)) {
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000421 puts("** Timeout **\n");
422 /* LED's off */
423 ide_led((LED_IDE1 | LED_IDE2), 0);
wdenkc6097192002-11-03 00:24:07 +0000424 return;
425 }
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000426 if ((i >= 100) && ((i % 100) == 0))
427 putc('.');
428
wdenkc6097192002-11-03 00:24:07 +0000429 } while (c & ATA_STAT_BUSY);
430
431 if (c & (ATA_STAT_BUSY | ATA_STAT_FAULT)) {
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000432 puts("not available ");
433 debug("Status = 0x%02X ", c);
434#ifndef CONFIG_ATAPI /* ATAPI Devices do not set DRDY */
435 } else if ((c & ATA_STAT_READY) == 0) {
436 puts("not available ");
437 debug("Status = 0x%02X ", c);
wdenkc6097192002-11-03 00:24:07 +0000438#endif
439 } else {
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000440 puts("OK ");
wdenkc6097192002-11-03 00:24:07 +0000441 ide_bus_ok[bus] = 1;
442 }
443 WATCHDOG_RESET();
444 }
wdenk452cfd62002-11-19 11:04:11 +0000445
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000446 putc('\n');
wdenkc6097192002-11-03 00:24:07 +0000447
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000448 ide_led((LED_IDE1 | LED_IDE2), 0); /* LED's off */
wdenkc6097192002-11-03 00:24:07 +0000449
450 curr_device = -1;
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000451 for (i = 0; i < CONFIG_SYS_IDE_MAXDEVICE; ++i) {
wdenkc6097192002-11-03 00:24:07 +0000452 int led = (IDE_BUS(i) == 0) ? LED_IDE1 : LED_IDE2;
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000453 ide_dev_desc[i].type = DEV_TYPE_UNKNOWN;
454 ide_dev_desc[i].if_type = IF_TYPE_IDE;
455 ide_dev_desc[i].dev = i;
456 ide_dev_desc[i].part_type = PART_TYPE_UNKNOWN;
457 ide_dev_desc[i].blksz = 0;
Egbert Eich2eec2ab2013-04-09 21:11:56 +0000458 ide_dev_desc[i].log2blksz =
459 LOG2_INVALID(typeof(ide_dev_desc[i].log2blksz));
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000460 ide_dev_desc[i].lba = 0;
461 ide_dev_desc[i].block_read = ide_read;
Macpaul Lind1e49942011-04-11 20:45:32 +0000462 ide_dev_desc[i].block_write = ide_write;
wdenkc6097192002-11-03 00:24:07 +0000463 if (!ide_bus_ok[IDE_BUS(i)])
464 continue;
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000465 ide_led(led, 1); /* LED on */
wdenkc6097192002-11-03 00:24:07 +0000466 ide_ident(&ide_dev_desc[i]);
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000467 ide_led(led, 0); /* LED off */
wdenkc6097192002-11-03 00:24:07 +0000468 dev_print(&ide_dev_desc[i]);
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000469
wdenkc6097192002-11-03 00:24:07 +0000470 if ((ide_dev_desc[i].lba > 0) && (ide_dev_desc[i].blksz > 0)) {
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000471 /* initialize partition type */
472 init_part(&ide_dev_desc[i]);
wdenkc6097192002-11-03 00:24:07 +0000473 if (curr_device < 0)
474 curr_device = i;
475 }
476 }
477 WATCHDOG_RESET();
478}
479
480/* ------------------------------------------------------------------------- */
481
Matthew McClintock6252b4f2011-05-24 05:31:19 +0000482#ifdef CONFIG_PARTITIONS
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000483block_dev_desc_t *ide_get_dev(int dev)
wdenkc6097192002-11-03 00:24:07 +0000484{
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200485 return (dev < CONFIG_SYS_IDE_MAXDEVICE) ? &ide_dev_desc[dev] : NULL;
wdenkc6097192002-11-03 00:24:07 +0000486}
Matthew McClintock6252b4f2011-05-24 05:31:19 +0000487#endif
wdenkc6097192002-11-03 00:24:07 +0000488
wdenkc6097192002-11-03 00:24:07 +0000489/* ------------------------------------------------------------------------- */
490
Pavel Herrmann35ad9a82012-10-09 07:04:39 +0000491void ide_input_swap_data(int dev, ulong *sect_buf, int words)
492 __attribute__ ((weak, alias("__ide_input_swap_data")));
493
494void ide_input_data(int dev, ulong *sect_buf, int words)
495 __attribute__ ((weak, alias("__ide_input_data")));
496
497void ide_output_data(int dev, const ulong *sect_buf, int words)
498 __attribute__ ((weak, alias("__ide_output_data")));
499
wdenk9b7f3842003-10-09 20:09:04 +0000500/* We only need to swap data if we are running on a big endian cpu. */
Pavel Herrmann3597c522012-10-09 07:10:08 +0000501#if defined(__LITTLE_ENDIAN)
Pavel Herrmann35ad9a82012-10-09 07:04:39 +0000502void __ide_input_swap_data(int dev, ulong *sect_buf, int words)
503{
504 ide_input_data(dev, sect_buf, words);
505}
wdenk9b7f3842003-10-09 20:09:04 +0000506#else
Pavel Herrmann35ad9a82012-10-09 07:04:39 +0000507void __ide_input_swap_data(int dev, ulong *sect_buf, int words)
wdenkc6097192002-11-03 00:24:07 +0000508{
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000509 volatile ushort *pbuf =
510 (ushort *) (ATA_CURR_BASE(dev) + ATA_DATA_REG);
511 ushort *dbuf = (ushort *) sect_buf;
wdenkf1276d42005-02-03 23:00:49 +0000512
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000513 debug("in input swap data base for read is %lx\n",
514 (unsigned long) pbuf);
wdenkf1276d42005-02-03 23:00:49 +0000515
516 while (words--) {
Wolfgang Denk39c76422006-06-16 17:32:31 +0200517#ifdef __MIPS__
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000518 *dbuf++ = swab16p((u16 *) pbuf);
519 *dbuf++ = swab16p((u16 *) pbuf);
Wolfgang Denk39c76422006-06-16 17:32:31 +0200520#else
wdenkf1276d42005-02-03 23:00:49 +0000521 *dbuf++ = ld_le16(pbuf);
522 *dbuf++ = ld_le16(pbuf);
Wolfgang Denk39c76422006-06-16 17:32:31 +0200523#endif /* !MIPS */
wdenkf1276d42005-02-03 23:00:49 +0000524 }
wdenkc6097192002-11-03 00:24:07 +0000525}
Pavel Herrmann3597c522012-10-09 07:10:08 +0000526#endif /* __LITTLE_ENDIAN */
wdenk591dda52002-11-18 00:14:45 +0000527
528
Albert Aribaud036c6b42010-08-08 05:17:05 +0530529#if defined(CONFIG_IDE_SWAP_IO)
Pavel Herrmann35ad9a82012-10-09 07:04:39 +0000530void __ide_output_data(int dev, const ulong *sect_buf, int words)
wdenkc6097192002-11-03 00:24:07 +0000531{
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000532 ushort *dbuf;
533 volatile ushort *pbuf;
wdenkf1276d42005-02-03 23:00:49 +0000534
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000535 pbuf = (ushort *) (ATA_CURR_BASE(dev) + ATA_DATA_REG);
536 dbuf = (ushort *) sect_buf;
wdenkf1276d42005-02-03 23:00:49 +0000537 while (words--) {
538 EIEIO;
539 *pbuf = *dbuf++;
540 EIEIO;
541 *pbuf = *dbuf++;
542 }
wdenkc6097192002-11-03 00:24:07 +0000543}
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000544#else /* ! CONFIG_IDE_SWAP_IO */
Pavel Herrmann35ad9a82012-10-09 07:04:39 +0000545void __ide_output_data(int dev, const ulong *sect_buf, int words)
wdenk591dda52002-11-18 00:14:45 +0000546{
Macpaul Lind1e49942011-04-11 20:45:32 +0000547#if defined(CONFIG_IDE_AHB)
548 ide_write_data(dev, sect_buf, words);
549#else
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000550 outsw(ATA_CURR_BASE(dev) + ATA_DATA_REG, sect_buf, words << 1);
Macpaul Lind1e49942011-04-11 20:45:32 +0000551#endif
wdenk591dda52002-11-18 00:14:45 +0000552}
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000553#endif /* CONFIG_IDE_SWAP_IO */
wdenkc6097192002-11-03 00:24:07 +0000554
Albert Aribaud036c6b42010-08-08 05:17:05 +0530555#if defined(CONFIG_IDE_SWAP_IO)
Pavel Herrmann35ad9a82012-10-09 07:04:39 +0000556void __ide_input_data(int dev, ulong *sect_buf, int words)
wdenkc6097192002-11-03 00:24:07 +0000557{
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000558 ushort *dbuf;
559 volatile ushort *pbuf;
wdenkf1276d42005-02-03 23:00:49 +0000560
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000561 pbuf = (ushort *) (ATA_CURR_BASE(dev) + ATA_DATA_REG);
562 dbuf = (ushort *) sect_buf;
wdenkf1276d42005-02-03 23:00:49 +0000563
564 debug("in input data base for read is %lx\n", (unsigned long) pbuf);
565
566 while (words--) {
wdenk20c98a62004-04-23 20:32:05 +0000567 EIEIO;
wdenkf1276d42005-02-03 23:00:49 +0000568 *dbuf++ = *pbuf;
wdenkf6925c72005-01-22 18:26:04 +0000569 EIEIO;
wdenkf1276d42005-02-03 23:00:49 +0000570 *dbuf++ = *pbuf;
wdenk2f99a692004-01-04 22:51:12 +0000571 }
wdenkc6097192002-11-03 00:24:07 +0000572}
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000573#else /* ! CONFIG_IDE_SWAP_IO */
Pavel Herrmann35ad9a82012-10-09 07:04:39 +0000574void __ide_input_data(int dev, ulong *sect_buf, int words)
wdenk591dda52002-11-18 00:14:45 +0000575{
Macpaul Lind1e49942011-04-11 20:45:32 +0000576#if defined(CONFIG_IDE_AHB)
577 ide_read_data(dev, sect_buf, words);
578#else
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000579 insw(ATA_CURR_BASE(dev) + ATA_DATA_REG, sect_buf, words << 1);
Macpaul Lind1e49942011-04-11 20:45:32 +0000580#endif
wdenk591dda52002-11-18 00:14:45 +0000581}
582
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000583#endif /* CONFIG_IDE_SWAP_IO */
wdenkc6097192002-11-03 00:24:07 +0000584
585/* -------------------------------------------------------------------------
586 */
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000587static void ide_ident(block_dev_desc_t *dev_desc)
wdenkc6097192002-11-03 00:24:07 +0000588{
wdenkc6097192002-11-03 00:24:07 +0000589 unsigned char c;
Marek Vasute49757e2011-08-20 09:15:13 +0000590 hd_driveid_t iop;
wdenkc6097192002-11-03 00:24:07 +0000591
wdenke2d6d742004-09-28 20:34:50 +0000592#ifdef CONFIG_ATAPI
593 int retries = 0;
wdenk452cfd62002-11-19 11:04:11 +0000594#endif
595
Steven A. Falco2b7574e2008-08-15 15:34:10 -0400596#ifdef CONFIG_TUNE_PIO
597 int pio_mode;
598#endif
599
wdenkc6097192002-11-03 00:24:07 +0000600#if 0
601 int mode, cycle_time;
602#endif
603 int device;
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000604
605 device = dev_desc->dev;
606 printf(" Device %d: ", device);
wdenkc6097192002-11-03 00:24:07 +0000607
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000608 ide_led(DEVICE_LED(device), 1); /* LED on */
wdenkc6097192002-11-03 00:24:07 +0000609 /* Select device
610 */
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000611 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
612 dev_desc->if_type = IF_TYPE_IDE;
wdenkc6097192002-11-03 00:24:07 +0000613#ifdef CONFIG_ATAPI
wdenk452cfd62002-11-19 11:04:11 +0000614
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000615 retries = 0;
wdenk452cfd62002-11-19 11:04:11 +0000616
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000617 /* Warning: This will be tricky to read */
618 while (retries <= 1) {
619 /* check signature */
620 if ((ide_inb(device, ATA_SECT_CNT) == 0x01) &&
621 (ide_inb(device, ATA_SECT_NUM) == 0x01) &&
622 (ide_inb(device, ATA_CYL_LOW) == 0x14) &&
623 (ide_inb(device, ATA_CYL_HIGH) == 0xEB)) {
624 /* ATAPI Signature found */
625 dev_desc->if_type = IF_TYPE_ATAPI;
626 /*
627 * Start Ident Command
628 */
629 ide_outb(device, ATA_COMMAND, ATAPI_CMD_IDENT);
630 /*
631 * Wait for completion - ATAPI devices need more time
632 * to become ready
633 */
634 c = ide_wait(device, ATAPI_TIME_OUT);
635 } else
wdenkc6097192002-11-03 00:24:07 +0000636#endif
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000637 {
638 /*
639 * Start Ident Command
640 */
641 ide_outb(device, ATA_COMMAND, ATA_CMD_IDENT);
wdenkc6097192002-11-03 00:24:07 +0000642
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000643 /*
644 * Wait for completion
645 */
646 c = ide_wait(device, IDE_TIME_OUT);
647 }
648 ide_led(DEVICE_LED(device), 0); /* LED off */
wdenkc6097192002-11-03 00:24:07 +0000649
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000650 if (((c & ATA_STAT_DRQ) == 0) ||
651 ((c & (ATA_STAT_FAULT | ATA_STAT_ERR)) != 0)) {
wdenke2d6d742004-09-28 20:34:50 +0000652#ifdef CONFIG_ATAPI
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000653 {
654 /*
655 * Need to soft reset the device
656 * in case it's an ATAPI...
657 */
658 debug("Retrying...\n");
659 ide_outb(device, ATA_DEV_HD,
660 ATA_LBA | ATA_DEVICE(device));
661 udelay(100000);
662 ide_outb(device, ATA_COMMAND, 0x08);
663 udelay(500000); /* 500 ms */
664 }
665 /*
666 * Select device
667 */
668 ide_outb(device, ATA_DEV_HD,
669 ATA_LBA | ATA_DEVICE(device));
670 retries++;
wdenke2d6d742004-09-28 20:34:50 +0000671#else
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000672 return;
wdenke2d6d742004-09-28 20:34:50 +0000673#endif
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000674 }
wdenke2d6d742004-09-28 20:34:50 +0000675#ifdef CONFIG_ATAPI
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000676 else
677 break;
678 } /* see above - ugly to read */
wdenke2d6d742004-09-28 20:34:50 +0000679
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000680 if (retries == 2) /* Not found */
wdenke2d6d742004-09-28 20:34:50 +0000681 return;
682#endif
wdenkc6097192002-11-03 00:24:07 +0000683
Pavel Herrmann35ad9a82012-10-09 07:04:39 +0000684 ide_input_swap_data(device, (ulong *)&iop, ATA_SECTORWORDS);
wdenkc6097192002-11-03 00:24:07 +0000685
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000686 ident_cpy((unsigned char *) dev_desc->revision, iop.fw_rev,
687 sizeof(dev_desc->revision));
688 ident_cpy((unsigned char *) dev_desc->vendor, iop.model,
689 sizeof(dev_desc->vendor));
690 ident_cpy((unsigned char *) dev_desc->product, iop.serial_no,
691 sizeof(dev_desc->product));
wdenkacd9b102004-03-14 00:59:59 +0000692#ifdef __LITTLE_ENDIAN
693 /*
Richard Retanubun54f30622008-11-06 14:01:51 -0500694 * firmware revision, model, and serial number have Big Endian Byte
695 * order in Word. Convert all three to little endian.
wdenkacd9b102004-03-14 00:59:59 +0000696 *
697 * See CF+ and CompactFlash Specification Revision 2.0:
Richard Retanubun54f30622008-11-06 14:01:51 -0500698 * 6.2.1.6: Identify Drive, Table 39 for more details
wdenkacd9b102004-03-14 00:59:59 +0000699 */
700
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000701 strswab(dev_desc->revision);
702 strswab(dev_desc->vendor);
703 strswab(dev_desc->product);
wdenkacd9b102004-03-14 00:59:59 +0000704#endif /* __LITTLE_ENDIAN */
wdenkc6097192002-11-03 00:24:07 +0000705
Marek Vasute49757e2011-08-20 09:15:13 +0000706 if ((iop.config & 0x0080) == 0x0080)
wdenkc6097192002-11-03 00:24:07 +0000707 dev_desc->removable = 1;
708 else
709 dev_desc->removable = 0;
710
Steven A. Falco2b7574e2008-08-15 15:34:10 -0400711#ifdef CONFIG_TUNE_PIO
712 /* Mode 0 - 2 only, are directly determined by word 51. */
Marek Vasute49757e2011-08-20 09:15:13 +0000713 pio_mode = iop.tPIO;
Steven A. Falco2b7574e2008-08-15 15:34:10 -0400714 if (pio_mode > 2) {
715 printf("WARNING: Invalid PIO (word 51 = %d).\n", pio_mode);
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000716 /* Force it to dead slow, and hope for the best... */
717 pio_mode = 0;
Steven A. Falco2b7574e2008-08-15 15:34:10 -0400718 }
719
720 /* Any CompactFlash Storage Card that supports PIO mode 3 or above
721 * shall set bit 1 of word 53 to one and support the fields contained
722 * in words 64 through 70.
723 */
Marek Vasute49757e2011-08-20 09:15:13 +0000724 if (iop.field_valid & 0x02) {
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000725 /*
726 * Mode 3 and above are possible. Check in order from slow
Steven A. Falco2b7574e2008-08-15 15:34:10 -0400727 * to fast, so we wind up with the highest mode allowed.
728 */
Marek Vasute49757e2011-08-20 09:15:13 +0000729 if (iop.eide_pio_modes & 0x01)
Steven A. Falco2b7574e2008-08-15 15:34:10 -0400730 pio_mode = 3;
Marek Vasute49757e2011-08-20 09:15:13 +0000731 if (iop.eide_pio_modes & 0x02)
Steven A. Falco2b7574e2008-08-15 15:34:10 -0400732 pio_mode = 4;
Marek Vasute49757e2011-08-20 09:15:13 +0000733 if (ata_id_is_cfa((u16 *)&iop)) {
734 if ((iop.cf_advanced_caps & 0x07) == 0x01)
Steven A. Falco2b7574e2008-08-15 15:34:10 -0400735 pio_mode = 5;
Marek Vasute49757e2011-08-20 09:15:13 +0000736 if ((iop.cf_advanced_caps & 0x07) == 0x02)
Steven A. Falco2b7574e2008-08-15 15:34:10 -0400737 pio_mode = 6;
738 }
739 }
740
741 /* System-specific, depends on bus speeds, etc. */
742 ide_set_piomode(pio_mode);
743#endif /* CONFIG_TUNE_PIO */
744
wdenkc6097192002-11-03 00:24:07 +0000745#if 0
746 /*
747 * Drive PIO mode autoselection
748 */
Marek Vasute49757e2011-08-20 09:15:13 +0000749 mode = iop.tPIO;
wdenkc6097192002-11-03 00:24:07 +0000750
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000751 printf("tPIO = 0x%02x = %d\n", mode, mode);
wdenkc6097192002-11-03 00:24:07 +0000752 if (mode > 2) { /* 2 is maximum allowed tPIO value */
753 mode = 2;
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000754 debug("Override tPIO -> 2\n");
wdenkc6097192002-11-03 00:24:07 +0000755 }
Marek Vasute49757e2011-08-20 09:15:13 +0000756 if (iop.field_valid & 2) { /* drive implements ATA2? */
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000757 debug("Drive implements ATA2\n");
Marek Vasute49757e2011-08-20 09:15:13 +0000758 if (iop.capability & 8) { /* drive supports use_iordy? */
759 cycle_time = iop.eide_pio_iordy;
wdenkc6097192002-11-03 00:24:07 +0000760 } else {
Marek Vasute49757e2011-08-20 09:15:13 +0000761 cycle_time = iop.eide_pio;
wdenkc6097192002-11-03 00:24:07 +0000762 }
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000763 debug("cycle time = %d\n", cycle_time);
wdenkc6097192002-11-03 00:24:07 +0000764 mode = 4;
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000765 if (cycle_time > 120)
766 mode = 3; /* 120 ns for PIO mode 4 */
767 if (cycle_time > 180)
768 mode = 2; /* 180 ns for PIO mode 3 */
769 if (cycle_time > 240)
770 mode = 1; /* 240 ns for PIO mode 4 */
771 if (cycle_time > 383)
772 mode = 0; /* 383 ns for PIO mode 4 */
wdenkc6097192002-11-03 00:24:07 +0000773 }
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000774 printf("PIO mode to use: PIO %d\n", mode);
wdenkc6097192002-11-03 00:24:07 +0000775#endif /* 0 */
776
777#ifdef CONFIG_ATAPI
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000778 if (dev_desc->if_type == IF_TYPE_ATAPI) {
wdenkc6097192002-11-03 00:24:07 +0000779 atapi_inquiry(dev_desc);
780 return;
781 }
782#endif /* CONFIG_ATAPI */
783
wdenkacd9b102004-03-14 00:59:59 +0000784#ifdef __BIG_ENDIAN
wdenkc6097192002-11-03 00:24:07 +0000785 /* swap shorts */
Marek Vasute49757e2011-08-20 09:15:13 +0000786 dev_desc->lba = (iop.lba_capacity << 16) | (iop.lba_capacity >> 16);
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000787#else /* ! __BIG_ENDIAN */
wdenkacd9b102004-03-14 00:59:59 +0000788 /*
789 * do not swap shorts on little endian
790 *
791 * See CF+ and CompactFlash Specification Revision 2.0:
792 * 6.2.1.6: Identfy Drive, Table 39, Word Address 57-58 for details.
793 */
Marek Vasute49757e2011-08-20 09:15:13 +0000794 dev_desc->lba = iop.lba_capacity;
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000795#endif /* __BIG_ENDIAN */
wdenkf602aa02004-03-13 23:29:43 +0000796
wdenkc35ba4e2004-03-14 22:25:36 +0000797#ifdef CONFIG_LBA48
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000798 if (iop.command_set_2 & 0x0400) { /* LBA 48 support */
wdenk05939202004-04-18 17:39:38 +0000799 dev_desc->lba48 = 1;
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000800 dev_desc->lba = (unsigned long long) iop.lba48_capacity[0] |
801 ((unsigned long long) iop.lba48_capacity[1] << 16) |
802 ((unsigned long long) iop.lba48_capacity[2] << 32) |
803 ((unsigned long long) iop.lba48_capacity[3] << 48);
wdenkf602aa02004-03-13 23:29:43 +0000804 } else {
wdenkf602aa02004-03-13 23:29:43 +0000805 dev_desc->lba48 = 0;
806 }
807#endif /* CONFIG_LBA48 */
wdenkc6097192002-11-03 00:24:07 +0000808 /* assuming HD */
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000809 dev_desc->type = DEV_TYPE_HARDDISK;
810 dev_desc->blksz = ATA_BLOCKSIZE;
Egbert Eich2eec2ab2013-04-09 21:11:56 +0000811 dev_desc->log2blksz = LOG2(dev_desc->blksz);
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000812 dev_desc->lun = 0; /* just to fill something in... */
wdenkc6097192002-11-03 00:24:07 +0000813
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000814#if 0 /* only used to test the powersaving mode,
815 * if enabled, the drive goes after 5 sec
816 * in standby mode */
817 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
818 c = ide_wait(device, IDE_TIME_OUT);
819 ide_outb(device, ATA_SECT_CNT, 1);
820 ide_outb(device, ATA_LBA_LOW, 0);
821 ide_outb(device, ATA_LBA_MID, 0);
822 ide_outb(device, ATA_LBA_HIGH, 0);
823 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
824 ide_outb(device, ATA_COMMAND, 0xe3);
825 udelay(50);
826 c = ide_wait(device, IDE_TIME_OUT); /* can't take over 500 ms */
wdenkc6097192002-11-03 00:24:07 +0000827#endif
828}
829
830
831/* ------------------------------------------------------------------------- */
832
Simon Glass11926fb2012-10-29 05:24:04 +0000833ulong ide_read(int device, ulong blknr, lbaint_t blkcnt, void *buffer)
wdenkc6097192002-11-03 00:24:07 +0000834{
835 ulong n = 0;
836 unsigned char c;
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000837 unsigned char pwrsave = 0; /* power save */
838
wdenkc35ba4e2004-03-14 22:25:36 +0000839#ifdef CONFIG_LBA48
wdenkf602aa02004-03-13 23:29:43 +0000840 unsigned char lba48 = 0;
wdenkc6097192002-11-03 00:24:07 +0000841
Guennadi Liakhovetskib71f1ca2008-04-28 14:36:06 +0200842 if (blknr & 0x0000fffff0000000ULL) {
wdenkf602aa02004-03-13 23:29:43 +0000843 /* more than 28 bits used, use 48bit mode */
844 lba48 = 1;
845 }
846#endif
Simon Glass11926fb2012-10-29 05:24:04 +0000847 debug("ide_read dev %d start %lX, blocks " LBAF " buffer at %lX\n",
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000848 device, blknr, blkcnt, (ulong) buffer);
wdenkc6097192002-11-03 00:24:07 +0000849
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000850 ide_led(DEVICE_LED(device), 1); /* LED on */
wdenkc6097192002-11-03 00:24:07 +0000851
852 /* Select device
853 */
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000854 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
855 c = ide_wait(device, IDE_TIME_OUT);
wdenkc6097192002-11-03 00:24:07 +0000856
857 if (c & ATA_STAT_BUSY) {
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000858 printf("IDE read: device %d not ready\n", device);
wdenkc6097192002-11-03 00:24:07 +0000859 goto IDE_READ_E;
860 }
861
862 /* first check if the drive is in Powersaving mode, if yes,
863 * increase the timeout value */
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000864 ide_outb(device, ATA_COMMAND, ATA_CMD_CHK_PWR);
865 udelay(50);
wdenkc6097192002-11-03 00:24:07 +0000866
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000867 c = ide_wait(device, IDE_TIME_OUT); /* can't take over 500 ms */
wdenkc6097192002-11-03 00:24:07 +0000868
869 if (c & ATA_STAT_BUSY) {
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000870 printf("IDE read: device %d not ready\n", device);
wdenkc6097192002-11-03 00:24:07 +0000871 goto IDE_READ_E;
872 }
873 if ((c & ATA_STAT_ERR) == ATA_STAT_ERR) {
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000874 printf("No Powersaving mode %X\n", c);
wdenkc6097192002-11-03 00:24:07 +0000875 } else {
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000876 c = ide_inb(device, ATA_SECT_CNT);
877 debug("Powersaving %02X\n", c);
878 if (c == 0)
879 pwrsave = 1;
wdenkc6097192002-11-03 00:24:07 +0000880 }
881
882
883 while (blkcnt-- > 0) {
884
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000885 c = ide_wait(device, IDE_TIME_OUT);
wdenkc6097192002-11-03 00:24:07 +0000886
887 if (c & ATA_STAT_BUSY) {
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000888 printf("IDE read: device %d not ready\n", device);
wdenkc6097192002-11-03 00:24:07 +0000889 break;
890 }
wdenkc35ba4e2004-03-14 22:25:36 +0000891#ifdef CONFIG_LBA48
wdenkf602aa02004-03-13 23:29:43 +0000892 if (lba48) {
893 /* write high bits */
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000894 ide_outb(device, ATA_SECT_CNT, 0);
895 ide_outb(device, ATA_LBA_LOW, (blknr >> 24) & 0xFF);
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200896#ifdef CONFIG_SYS_64BIT_LBA
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000897 ide_outb(device, ATA_LBA_MID, (blknr >> 32) & 0xFF);
898 ide_outb(device, ATA_LBA_HIGH, (blknr >> 40) & 0xFF);
Guennadi Liakhovetskib71f1ca2008-04-28 14:36:06 +0200899#else
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000900 ide_outb(device, ATA_LBA_MID, 0);
901 ide_outb(device, ATA_LBA_HIGH, 0);
Guennadi Liakhovetskib71f1ca2008-04-28 14:36:06 +0200902#endif
wdenkf602aa02004-03-13 23:29:43 +0000903 }
904#endif
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000905 ide_outb(device, ATA_SECT_CNT, 1);
906 ide_outb(device, ATA_LBA_LOW, (blknr >> 0) & 0xFF);
907 ide_outb(device, ATA_LBA_MID, (blknr >> 8) & 0xFF);
908 ide_outb(device, ATA_LBA_HIGH, (blknr >> 16) & 0xFF);
wdenkf602aa02004-03-13 23:29:43 +0000909
wdenkc35ba4e2004-03-14 22:25:36 +0000910#ifdef CONFIG_LBA48
wdenkf602aa02004-03-13 23:29:43 +0000911 if (lba48) {
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000912 ide_outb(device, ATA_DEV_HD,
913 ATA_LBA | ATA_DEVICE(device));
914 ide_outb(device, ATA_COMMAND, ATA_CMD_READ_EXT);
wdenkf602aa02004-03-13 23:29:43 +0000915
916 } else
917#endif
918 {
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000919 ide_outb(device, ATA_DEV_HD, ATA_LBA |
920 ATA_DEVICE(device) | ((blknr >> 24) & 0xF));
921 ide_outb(device, ATA_COMMAND, ATA_CMD_READ);
wdenkf602aa02004-03-13 23:29:43 +0000922 }
wdenkc6097192002-11-03 00:24:07 +0000923
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000924 udelay(50);
wdenkc6097192002-11-03 00:24:07 +0000925
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000926 if (pwrsave) {
927 /* may take up to 4 sec */
928 c = ide_wait(device, IDE_SPIN_UP_TIME_OUT);
929 pwrsave = 0;
wdenkc6097192002-11-03 00:24:07 +0000930 } else {
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000931 /* can't take over 500 ms */
932 c = ide_wait(device, IDE_TIME_OUT);
wdenkc6097192002-11-03 00:24:07 +0000933 }
934
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000935 if ((c & (ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR)) !=
936 ATA_STAT_DRQ) {
Simon Glass11926fb2012-10-29 05:24:04 +0000937 printf("Error (no IRQ) dev %d blk %ld: status %#02x\n",
wdenkc6097192002-11-03 00:24:07 +0000938 device, blknr, c);
939 break;
940 }
941
Pavel Herrmann35ad9a82012-10-09 07:04:39 +0000942 ide_input_data(device, buffer, ATA_SECTORWORDS);
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000943 (void) ide_inb(device, ATA_STATUS); /* clear IRQ */
wdenkc6097192002-11-03 00:24:07 +0000944
945 ++n;
946 ++blknr;
Greg Lopp50abf9f2007-04-13 08:02:24 +0200947 buffer += ATA_BLOCKSIZE;
wdenkc6097192002-11-03 00:24:07 +0000948 }
949IDE_READ_E:
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000950 ide_led(DEVICE_LED(device), 0); /* LED off */
wdenkc6097192002-11-03 00:24:07 +0000951 return (n);
952}
953
954/* ------------------------------------------------------------------------- */
955
956
Simon Glass11926fb2012-10-29 05:24:04 +0000957ulong ide_write(int device, ulong blknr, lbaint_t blkcnt, const void *buffer)
wdenkc6097192002-11-03 00:24:07 +0000958{
959 ulong n = 0;
960 unsigned char c;
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000961
wdenkc35ba4e2004-03-14 22:25:36 +0000962#ifdef CONFIG_LBA48
wdenkf602aa02004-03-13 23:29:43 +0000963 unsigned char lba48 = 0;
964
Guennadi Liakhovetskib71f1ca2008-04-28 14:36:06 +0200965 if (blknr & 0x0000fffff0000000ULL) {
wdenkf602aa02004-03-13 23:29:43 +0000966 /* more than 28 bits used, use 48bit mode */
967 lba48 = 1;
968 }
969#endif
wdenkc6097192002-11-03 00:24:07 +0000970
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000971 ide_led(DEVICE_LED(device), 1); /* LED on */
wdenkc6097192002-11-03 00:24:07 +0000972
973 /* Select device
974 */
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000975 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
wdenkc6097192002-11-03 00:24:07 +0000976
977 while (blkcnt-- > 0) {
978
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000979 c = ide_wait(device, IDE_TIME_OUT);
wdenkc6097192002-11-03 00:24:07 +0000980
981 if (c & ATA_STAT_BUSY) {
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000982 printf("IDE read: device %d not ready\n", device);
wdenkc6097192002-11-03 00:24:07 +0000983 goto WR_OUT;
984 }
wdenkc35ba4e2004-03-14 22:25:36 +0000985#ifdef CONFIG_LBA48
wdenkf602aa02004-03-13 23:29:43 +0000986 if (lba48) {
987 /* write high bits */
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000988 ide_outb(device, ATA_SECT_CNT, 0);
989 ide_outb(device, ATA_LBA_LOW, (blknr >> 24) & 0xFF);
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200990#ifdef CONFIG_SYS_64BIT_LBA
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000991 ide_outb(device, ATA_LBA_MID, (blknr >> 32) & 0xFF);
992 ide_outb(device, ATA_LBA_HIGH, (blknr >> 40) & 0xFF);
Guennadi Liakhovetskib71f1ca2008-04-28 14:36:06 +0200993#else
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000994 ide_outb(device, ATA_LBA_MID, 0);
995 ide_outb(device, ATA_LBA_HIGH, 0);
Guennadi Liakhovetskib71f1ca2008-04-28 14:36:06 +0200996#endif
wdenkf602aa02004-03-13 23:29:43 +0000997 }
998#endif
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000999 ide_outb(device, ATA_SECT_CNT, 1);
1000 ide_outb(device, ATA_LBA_LOW, (blknr >> 0) & 0xFF);
1001 ide_outb(device, ATA_LBA_MID, (blknr >> 8) & 0xFF);
1002 ide_outb(device, ATA_LBA_HIGH, (blknr >> 16) & 0xFF);
wdenkf602aa02004-03-13 23:29:43 +00001003
wdenkc35ba4e2004-03-14 22:25:36 +00001004#ifdef CONFIG_LBA48
wdenkf602aa02004-03-13 23:29:43 +00001005 if (lba48) {
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001006 ide_outb(device, ATA_DEV_HD,
1007 ATA_LBA | ATA_DEVICE(device));
1008 ide_outb(device, ATA_COMMAND, ATA_CMD_WRITE_EXT);
wdenkf602aa02004-03-13 23:29:43 +00001009
1010 } else
1011#endif
1012 {
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001013 ide_outb(device, ATA_DEV_HD, ATA_LBA |
1014 ATA_DEVICE(device) | ((blknr >> 24) & 0xF));
1015 ide_outb(device, ATA_COMMAND, ATA_CMD_WRITE);
wdenkf602aa02004-03-13 23:29:43 +00001016 }
wdenkc6097192002-11-03 00:24:07 +00001017
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001018 udelay(50);
wdenkc6097192002-11-03 00:24:07 +00001019
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001020 /* can't take over 500 ms */
1021 c = ide_wait(device, IDE_TIME_OUT);
wdenkc6097192002-11-03 00:24:07 +00001022
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001023 if ((c & (ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR)) !=
1024 ATA_STAT_DRQ) {
Simon Glass11926fb2012-10-29 05:24:04 +00001025 printf("Error (no IRQ) dev %d blk %ld: status %#02x\n",
wdenkc6097192002-11-03 00:24:07 +00001026 device, blknr, c);
1027 goto WR_OUT;
1028 }
1029
Pavel Herrmann35ad9a82012-10-09 07:04:39 +00001030 ide_output_data(device, buffer, ATA_SECTORWORDS);
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001031 c = ide_inb(device, ATA_STATUS); /* clear IRQ */
wdenkc6097192002-11-03 00:24:07 +00001032 ++n;
1033 ++blknr;
Greg Lopp50abf9f2007-04-13 08:02:24 +02001034 buffer += ATA_BLOCKSIZE;
wdenkc6097192002-11-03 00:24:07 +00001035 }
1036WR_OUT:
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001037 ide_led(DEVICE_LED(device), 0); /* LED off */
wdenkc6097192002-11-03 00:24:07 +00001038 return (n);
1039}
1040
1041/* ------------------------------------------------------------------------- */
1042
1043/*
1044 * copy src to dest, skipping leading and trailing blanks and null
1045 * terminate the string
wdenk18bd81e2004-03-17 01:13:07 +00001046 * "len" is the size of available memory including the terminating '\0'
wdenkc6097192002-11-03 00:24:07 +00001047 */
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001048static void ident_cpy(unsigned char *dst, unsigned char *src,
1049 unsigned int len)
wdenkc6097192002-11-03 00:24:07 +00001050{
wdenk18bd81e2004-03-17 01:13:07 +00001051 unsigned char *end, *last;
wdenkc6097192002-11-03 00:24:07 +00001052
wdenk18bd81e2004-03-17 01:13:07 +00001053 last = dst;
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001054 end = src + len - 1;
wdenk18bd81e2004-03-17 01:13:07 +00001055
1056 /* reserve space for '\0' */
1057 if (len < 2)
1058 goto OUT;
wdenkc12081a2004-03-23 20:18:25 +00001059
wdenk18bd81e2004-03-17 01:13:07 +00001060 /* skip leading white space */
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001061 while ((*src) && (src < end) && (*src == ' '))
wdenk18bd81e2004-03-17 01:13:07 +00001062 ++src;
1063
1064 /* copy string, omitting trailing white space */
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001065 while ((*src) && (src < end)) {
wdenk18bd81e2004-03-17 01:13:07 +00001066 *dst++ = *src;
1067 if (*src++ != ' ')
1068 last = dst;
wdenkc6097192002-11-03 00:24:07 +00001069 }
wdenk18bd81e2004-03-17 01:13:07 +00001070OUT:
1071 *last = '\0';
wdenkc6097192002-11-03 00:24:07 +00001072}
1073
1074/* ------------------------------------------------------------------------- */
1075
1076/*
1077 * Wait until Busy bit is off, or timeout (in ms)
1078 * Return last status
1079 */
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001080static uchar ide_wait(int dev, ulong t)
wdenkc6097192002-11-03 00:24:07 +00001081{
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001082 ulong delay = 10 * t; /* poll every 100 us */
wdenkc6097192002-11-03 00:24:07 +00001083 uchar c;
1084
wdenk591dda52002-11-18 00:14:45 +00001085 while ((c = ide_inb(dev, ATA_STATUS)) & ATA_STAT_BUSY) {
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001086 udelay(100);
1087 if (delay-- == 0)
wdenkc6097192002-11-03 00:24:07 +00001088 break;
wdenkc6097192002-11-03 00:24:07 +00001089 }
1090 return (c);
1091}
1092
1093/* ------------------------------------------------------------------------- */
1094
1095#ifdef CONFIG_IDE_RESET
1096extern void ide_set_reset(int idereset);
1097
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001098static void ide_reset(void)
wdenkc6097192002-11-03 00:24:07 +00001099{
wdenkc6097192002-11-03 00:24:07 +00001100 int i;
1101
1102 curr_device = -1;
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001103 for (i = 0; i < CONFIG_SYS_IDE_MAXBUS; ++i)
wdenkc6097192002-11-03 00:24:07 +00001104 ide_bus_ok[i] = 0;
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001105 for (i = 0; i < CONFIG_SYS_IDE_MAXDEVICE; ++i)
wdenkc6097192002-11-03 00:24:07 +00001106 ide_dev_desc[i].type = DEV_TYPE_UNKNOWN;
1107
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001108 ide_set_reset(1); /* assert reset */
wdenkc6097192002-11-03 00:24:07 +00001109
Martin Krause7e35caf2008-04-03 13:37:56 +02001110 /* the reset signal shall be asserted for et least 25 us */
1111 udelay(25);
1112
wdenkc6097192002-11-03 00:24:07 +00001113 WATCHDOG_RESET();
1114
wdenkc6097192002-11-03 00:24:07 +00001115 /* de-assert RESET signal */
1116 ide_set_reset(0);
1117
1118 /* wait 250 ms */
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001119 for (i = 0; i < 250; ++i)
1120 udelay(1000);
wdenkc6097192002-11-03 00:24:07 +00001121}
1122
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001123#endif /* CONFIG_IDE_RESET */
wdenkc6097192002-11-03 00:24:07 +00001124
1125/* ------------------------------------------------------------------------- */
1126
Heiko Schocherffb293a2009-09-23 07:56:08 +02001127#if defined(CONFIG_OF_IDE_FIXUP)
1128int ide_device_present(int dev)
1129{
1130 if (dev >= CONFIG_SYS_IDE_MAXBUS)
1131 return 0;
1132 return (ide_dev_desc[dev].type == DEV_TYPE_UNKNOWN ? 0 : 1);
1133}
1134#endif
wdenkc6097192002-11-03 00:24:07 +00001135/* ------------------------------------------------------------------------- */
1136
1137#ifdef CONFIG_ATAPI
1138/****************************************************************************
1139 * ATAPI Support
1140 */
1141
Pavel Herrmann35ad9a82012-10-09 07:04:39 +00001142void ide_input_data_shorts(int dev, ushort *sect_buf, int shorts)
1143 __attribute__ ((weak, alias("__ide_input_data_shorts")));
1144
1145void ide_output_data_shorts(int dev, ushort *sect_buf, int shorts)
1146 __attribute__ ((weak, alias("__ide_output_data_shorts")));
1147
1148
Albert Aribaud036c6b42010-08-08 05:17:05 +05301149#if defined(CONFIG_IDE_SWAP_IO)
wdenkc6097192002-11-03 00:24:07 +00001150/* since ATAPI may use commands with not 4 bytes alligned length
1151 * we have our own transfer functions, 2 bytes alligned */
Pavel Herrmann35ad9a82012-10-09 07:04:39 +00001152void __ide_output_data_shorts(int dev, ushort *sect_buf, int shorts)
wdenkc6097192002-11-03 00:24:07 +00001153{
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001154 ushort *dbuf;
1155 volatile ushort *pbuf;
wdenkc6097192002-11-03 00:24:07 +00001156
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001157 pbuf = (ushort *) (ATA_CURR_BASE(dev) + ATA_DATA_REG);
1158 dbuf = (ushort *) sect_buf;
wdenk634d2f72004-04-15 23:14:49 +00001159
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001160 debug("in output data shorts base for read is %lx\n",
1161 (unsigned long) pbuf);
wdenk634d2f72004-04-15 23:14:49 +00001162
wdenkc6097192002-11-03 00:24:07 +00001163 while (shorts--) {
wdenk20c98a62004-04-23 20:32:05 +00001164 EIEIO;
wdenkf1276d42005-02-03 23:00:49 +00001165 *pbuf = *dbuf++;
wdenkc6097192002-11-03 00:24:07 +00001166 }
wdenkf1276d42005-02-03 23:00:49 +00001167}
1168
Pavel Herrmann35ad9a82012-10-09 07:04:39 +00001169void __ide_input_data_shorts(int dev, ushort *sect_buf, int shorts)
wdenkf1276d42005-02-03 23:00:49 +00001170{
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001171 ushort *dbuf;
1172 volatile ushort *pbuf;
wdenkf1276d42005-02-03 23:00:49 +00001173
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001174 pbuf = (ushort *) (ATA_CURR_BASE(dev) + ATA_DATA_REG);
1175 dbuf = (ushort *) sect_buf;
wdenkf1276d42005-02-03 23:00:49 +00001176
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001177 debug("in input data shorts base for read is %lx\n",
1178 (unsigned long) pbuf);
wdenkf1276d42005-02-03 23:00:49 +00001179
1180 while (shorts--) {
1181 EIEIO;
1182 *dbuf++ = *pbuf;
1183 }
wdenkc6097192002-11-03 00:24:07 +00001184}
1185
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001186#else /* ! CONFIG_IDE_SWAP_IO */
Pavel Herrmann35ad9a82012-10-09 07:04:39 +00001187void __ide_output_data_shorts(int dev, ushort *sect_buf, int shorts)
wdenk591dda52002-11-18 00:14:45 +00001188{
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001189 outsw(ATA_CURR_BASE(dev) + ATA_DATA_REG, sect_buf, shorts);
wdenk591dda52002-11-18 00:14:45 +00001190}
1191
Pavel Herrmann35ad9a82012-10-09 07:04:39 +00001192void __ide_input_data_shorts(int dev, ushort *sect_buf, int shorts)
wdenk591dda52002-11-18 00:14:45 +00001193{
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001194 insw(ATA_CURR_BASE(dev) + ATA_DATA_REG, sect_buf, shorts);
wdenk591dda52002-11-18 00:14:45 +00001195}
1196
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001197#endif /* CONFIG_IDE_SWAP_IO */
wdenk591dda52002-11-18 00:14:45 +00001198
wdenkc6097192002-11-03 00:24:07 +00001199/*
1200 * Wait until (Status & mask) == res, or timeout (in ms)
1201 * Return last status
1202 * This is used since some ATAPI CD ROMs clears their Busy Bit first
1203 * and then they set their DRQ Bit
1204 */
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001205static uchar atapi_wait_mask(int dev, ulong t, uchar mask, uchar res)
wdenkc6097192002-11-03 00:24:07 +00001206{
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001207 ulong delay = 10 * t; /* poll every 100 us */
wdenkc6097192002-11-03 00:24:07 +00001208 uchar c;
1209
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001210 /* prevents to read the status before valid */
1211 c = ide_inb(dev, ATA_DEV_CTL);
1212
wdenk591dda52002-11-18 00:14:45 +00001213 while (((c = ide_inb(dev, ATA_STATUS)) & mask) != res) {
wdenkc6097192002-11-03 00:24:07 +00001214 /* break if error occurs (doesn't make sense to wait more) */
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001215 if ((c & ATA_STAT_ERR) == ATA_STAT_ERR)
wdenkc6097192002-11-03 00:24:07 +00001216 break;
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001217 udelay(100);
1218 if (delay-- == 0)
wdenkc6097192002-11-03 00:24:07 +00001219 break;
wdenkc6097192002-11-03 00:24:07 +00001220 }
1221 return (c);
1222}
1223
1224/*
1225 * issue an atapi command
1226 */
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001227unsigned char atapi_issue(int device, unsigned char *ccb, int ccblen,
1228 unsigned char *buffer, int buflen)
wdenkc6097192002-11-03 00:24:07 +00001229{
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001230 unsigned char c, err, mask, res;
wdenkc6097192002-11-03 00:24:07 +00001231 int n;
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001232
1233 ide_led(DEVICE_LED(device), 1); /* LED on */
wdenkc6097192002-11-03 00:24:07 +00001234
1235 /* Select device
1236 */
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001237 mask = ATA_STAT_BUSY | ATA_STAT_DRQ;
wdenkc6097192002-11-03 00:24:07 +00001238 res = 0;
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001239 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
1240 c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
wdenkc6097192002-11-03 00:24:07 +00001241 if ((c & mask) != res) {
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001242 printf("ATAPI_ISSUE: device %d not ready status %X\n", device,
1243 c);
1244 err = 0xFF;
wdenkc6097192002-11-03 00:24:07 +00001245 goto AI_OUT;
1246 }
1247 /* write taskfile */
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001248 ide_outb(device, ATA_ERROR_REG, 0); /* no DMA, no overlaped */
1249 ide_outb(device, ATA_SECT_CNT, 0);
1250 ide_outb(device, ATA_SECT_NUM, 0);
1251 ide_outb(device, ATA_CYL_LOW, (unsigned char) (buflen & 0xFF));
1252 ide_outb(device, ATA_CYL_HIGH,
1253 (unsigned char) ((buflen >> 8) & 0xFF));
1254 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
wdenkc6097192002-11-03 00:24:07 +00001255
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001256 ide_outb(device, ATA_COMMAND, ATAPI_CMD_PACKET);
1257 udelay(50);
wdenkc6097192002-11-03 00:24:07 +00001258
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001259 mask = ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR;
wdenkc6097192002-11-03 00:24:07 +00001260 res = ATA_STAT_DRQ;
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001261 c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
wdenkc6097192002-11-03 00:24:07 +00001262
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001263 if ((c & mask) != res) { /* DRQ must be 1, BSY 0 */
1264 printf("ATAPI_ISSUE: Error (no IRQ) before sending ccb dev %d status 0x%02x\n",
1265 device, c);
1266 err = 0xFF;
wdenkc6097192002-11-03 00:24:07 +00001267 goto AI_OUT;
1268 }
1269
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001270 /* write command block */
Pavel Herrmann35ad9a82012-10-09 07:04:39 +00001271 ide_output_data_shorts(device, (unsigned short *) ccb, ccblen / 2);
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001272
Wolfgang Denka1be4762008-05-20 16:00:29 +02001273 /* ATAPI Command written wait for completition */
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001274 udelay(5000); /* device must set bsy */
wdenkc6097192002-11-03 00:24:07 +00001275
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001276 mask = ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR;
1277 /*
1278 * if no data wait for DRQ = 0 BSY = 0
1279 * if data wait for DRQ = 1 BSY = 0
1280 */
1281 res = 0;
1282 if (buflen)
wdenkc6097192002-11-03 00:24:07 +00001283 res = ATA_STAT_DRQ;
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001284 c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
1285 if ((c & mask) != res) {
wdenkc6097192002-11-03 00:24:07 +00001286 if (c & ATA_STAT_ERR) {
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001287 err = (ide_inb(device, ATA_ERROR_REG)) >> 4;
1288 debug("atapi_issue 1 returned sense key %X status %02X\n",
1289 err, c);
wdenkc6097192002-11-03 00:24:07 +00001290 } else {
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001291 printf("ATAPI_ISSUE: (no DRQ) after sending ccb (%x) status 0x%02x\n",
1292 ccb[0], c);
1293 err = 0xFF;
wdenkc6097192002-11-03 00:24:07 +00001294 }
1295 goto AI_OUT;
1296 }
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001297 n = ide_inb(device, ATA_CYL_HIGH);
1298 n <<= 8;
1299 n += ide_inb(device, ATA_CYL_LOW);
1300 if (n > buflen) {
1301 printf("ERROR, transfer bytes %d requested only %d\n", n,
1302 buflen);
1303 err = 0xff;
wdenkc6097192002-11-03 00:24:07 +00001304 goto AI_OUT;
1305 }
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001306 if ((n == 0) && (buflen < 0)) {
1307 printf("ERROR, transfer bytes %d requested %d\n", n, buflen);
1308 err = 0xff;
wdenkc6097192002-11-03 00:24:07 +00001309 goto AI_OUT;
1310 }
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001311 if (n != buflen) {
1312 debug("WARNING, transfer bytes %d not equal with requested %d\n",
1313 n, buflen);
wdenkc6097192002-11-03 00:24:07 +00001314 }
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001315 if (n != 0) { /* data transfer */
1316 debug("ATAPI_ISSUE: %d Bytes to transfer\n", n);
1317 /* we transfer shorts */
1318 n >>= 1;
wdenkc6097192002-11-03 00:24:07 +00001319 /* ok now decide if it is an in or output */
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001320 if ((ide_inb(device, ATA_SECT_CNT) & 0x02) == 0) {
1321 debug("Write to device\n");
Pavel Herrmann35ad9a82012-10-09 07:04:39 +00001322 ide_output_data_shorts(device,
1323 (unsigned short *) buffer, n);
wdenkc6097192002-11-03 00:24:07 +00001324 } else {
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001325 debug("Read from device @ %p shorts %d\n", buffer, n);
Pavel Herrmann35ad9a82012-10-09 07:04:39 +00001326 ide_input_data_shorts(device,
1327 (unsigned short *) buffer, n);
wdenkc6097192002-11-03 00:24:07 +00001328 }
1329 }
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001330 udelay(5000); /* seems that some CD ROMs need this... */
1331 mask = ATA_STAT_BUSY | ATA_STAT_ERR;
1332 res = 0;
1333 c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
wdenkc6097192002-11-03 00:24:07 +00001334 if ((c & ATA_STAT_ERR) == ATA_STAT_ERR) {
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001335 err = (ide_inb(device, ATA_ERROR_REG) >> 4);
1336 debug("atapi_issue 2 returned sense key %X status %X\n", err,
1337 c);
wdenkc6097192002-11-03 00:24:07 +00001338 } else {
1339 err = 0;
1340 }
1341AI_OUT:
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001342 ide_led(DEVICE_LED(device), 0); /* LED off */
wdenkc6097192002-11-03 00:24:07 +00001343 return (err);
1344}
1345
1346/*
1347 * sending the command to atapi_issue. If an status other than good
1348 * returns, an request_sense will be issued
1349 */
1350
Wolfgang Denka1be4762008-05-20 16:00:29 +02001351#define ATAPI_DRIVE_NOT_READY 100
wdenkc6097192002-11-03 00:24:07 +00001352#define ATAPI_UNIT_ATTN 10
1353
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001354unsigned char atapi_issue_autoreq(int device,
1355 unsigned char *ccb,
1356 int ccblen,
1357 unsigned char *buffer, int buflen)
wdenkc6097192002-11-03 00:24:07 +00001358{
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001359 unsigned char sense_data[18], sense_ccb[12];
1360 unsigned char res, key, asc, ascq;
1361 int notready, unitattn;
wdenkc6097192002-11-03 00:24:07 +00001362
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001363 unitattn = ATAPI_UNIT_ATTN;
1364 notready = ATAPI_DRIVE_NOT_READY;
wdenkc6097192002-11-03 00:24:07 +00001365
1366retry:
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001367 res = atapi_issue(device, ccb, ccblen, buffer, buflen);
1368 if (res == 0)
1369 return 0; /* Ok */
wdenkc6097192002-11-03 00:24:07 +00001370
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001371 if (res == 0xFF)
1372 return 0xFF; /* error */
wdenkc6097192002-11-03 00:24:07 +00001373
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001374 debug("(auto_req)atapi_issue returned sense key %X\n", res);
wdenkc6097192002-11-03 00:24:07 +00001375
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001376 memset(sense_ccb, 0, sizeof(sense_ccb));
1377 memset(sense_data, 0, sizeof(sense_data));
1378 sense_ccb[0] = ATAPI_CMD_REQ_SENSE;
1379 sense_ccb[4] = 18; /* allocation Length */
wdenkc6097192002-11-03 00:24:07 +00001380
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001381 res = atapi_issue(device, sense_ccb, 12, sense_data, 18);
1382 key = (sense_data[2] & 0xF);
1383 asc = (sense_data[12]);
1384 ascq = (sense_data[13]);
wdenkc6097192002-11-03 00:24:07 +00001385
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001386 debug("ATAPI_CMD_REQ_SENSE returned %x\n", res);
1387 debug(" Sense page: %02X key %02X ASC %02X ASCQ %02X\n",
1388 sense_data[0], key, asc, ascq);
wdenkc6097192002-11-03 00:24:07 +00001389
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001390 if ((key == 0))
1391 return 0; /* ok device ready */
wdenkc6097192002-11-03 00:24:07 +00001392
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001393 if ((key == 6) || (asc == 0x29) || (asc == 0x28)) { /* Unit Attention */
1394 if (unitattn-- > 0) {
1395 udelay(200 * 1000);
wdenkc6097192002-11-03 00:24:07 +00001396 goto retry;
1397 }
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001398 printf("Unit Attention, tried %d\n", ATAPI_UNIT_ATTN);
wdenkc6097192002-11-03 00:24:07 +00001399 goto error;
1400 }
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001401 if ((asc == 0x4) && (ascq == 0x1)) {
1402 /* not ready, but will be ready soon */
1403 if (notready-- > 0) {
1404 udelay(200 * 1000);
wdenkc6097192002-11-03 00:24:07 +00001405 goto retry;
1406 }
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001407 printf("Drive not ready, tried %d times\n",
1408 ATAPI_DRIVE_NOT_READY);
wdenkc6097192002-11-03 00:24:07 +00001409 goto error;
1410 }
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001411 if (asc == 0x3a) {
1412 debug("Media not present\n");
wdenkc6097192002-11-03 00:24:07 +00001413 goto error;
1414 }
wdenk452cfd62002-11-19 11:04:11 +00001415
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001416 printf("ERROR: Unknown Sense key %02X ASC %02X ASCQ %02X\n", key, asc,
1417 ascq);
wdenkc6097192002-11-03 00:24:07 +00001418error:
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001419 debug("ERROR Sense key %02X ASC %02X ASCQ %02X\n", key, asc, ascq);
wdenkc6097192002-11-03 00:24:07 +00001420 return (0xFF);
1421}
1422
1423
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001424static void atapi_inquiry(block_dev_desc_t *dev_desc)
wdenkc6097192002-11-03 00:24:07 +00001425{
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001426 unsigned char ccb[12]; /* Command descriptor block */
1427 unsigned char iobuf[64]; /* temp buf */
wdenkc6097192002-11-03 00:24:07 +00001428 unsigned char c;
1429 int device;
1430
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001431 device = dev_desc->dev;
1432 dev_desc->type = DEV_TYPE_UNKNOWN; /* not yet valid */
1433 dev_desc->block_read = atapi_read;
wdenkc6097192002-11-03 00:24:07 +00001434
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001435 memset(ccb, 0, sizeof(ccb));
1436 memset(iobuf, 0, sizeof(iobuf));
wdenkc6097192002-11-03 00:24:07 +00001437
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001438 ccb[0] = ATAPI_CMD_INQUIRY;
1439 ccb[4] = 40; /* allocation Legnth */
1440 c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *) iobuf, 40);
wdenkc6097192002-11-03 00:24:07 +00001441
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001442 debug("ATAPI_CMD_INQUIRY returned %x\n", c);
1443 if (c != 0)
wdenkc6097192002-11-03 00:24:07 +00001444 return;
1445
1446 /* copy device ident strings */
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001447 ident_cpy((unsigned char *) dev_desc->vendor, &iobuf[8], 8);
1448 ident_cpy((unsigned char *) dev_desc->product, &iobuf[16], 16);
1449 ident_cpy((unsigned char *) dev_desc->revision, &iobuf[32], 5);
wdenkc6097192002-11-03 00:24:07 +00001450
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001451 dev_desc->lun = 0;
1452 dev_desc->lba = 0;
1453 dev_desc->blksz = 0;
Egbert Eich2eec2ab2013-04-09 21:11:56 +00001454 dev_desc->log2blksz = LOG2_INVALID(typeof(dev_desc->log2blksz));
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001455 dev_desc->type = iobuf[0] & 0x1f;
wdenkc6097192002-11-03 00:24:07 +00001456
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001457 if ((iobuf[1] & 0x80) == 0x80)
wdenkc6097192002-11-03 00:24:07 +00001458 dev_desc->removable = 1;
1459 else
1460 dev_desc->removable = 0;
1461
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001462 memset(ccb, 0, sizeof(ccb));
1463 memset(iobuf, 0, sizeof(iobuf));
1464 ccb[0] = ATAPI_CMD_START_STOP;
1465 ccb[4] = 0x03; /* start */
wdenkc6097192002-11-03 00:24:07 +00001466
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001467 c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *) iobuf, 0);
wdenkc6097192002-11-03 00:24:07 +00001468
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001469 debug("ATAPI_CMD_START_STOP returned %x\n", c);
1470 if (c != 0)
wdenkc6097192002-11-03 00:24:07 +00001471 return;
1472
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001473 memset(ccb, 0, sizeof(ccb));
1474 memset(iobuf, 0, sizeof(iobuf));
1475 c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *) iobuf, 0);
wdenkc6097192002-11-03 00:24:07 +00001476
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001477 debug("ATAPI_CMD_UNIT_TEST_READY returned %x\n", c);
1478 if (c != 0)
wdenkc6097192002-11-03 00:24:07 +00001479 return;
1480
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001481 memset(ccb, 0, sizeof(ccb));
1482 memset(iobuf, 0, sizeof(iobuf));
1483 ccb[0] = ATAPI_CMD_READ_CAP;
1484 c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *) iobuf, 8);
1485 debug("ATAPI_CMD_READ_CAP returned %x\n", c);
1486 if (c != 0)
wdenkc6097192002-11-03 00:24:07 +00001487 return;
1488
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001489 debug("Read Cap: LBA %02X%02X%02X%02X blksize %02X%02X%02X%02X\n",
1490 iobuf[0], iobuf[1], iobuf[2], iobuf[3],
1491 iobuf[4], iobuf[5], iobuf[6], iobuf[7]);
wdenkc6097192002-11-03 00:24:07 +00001492
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001493 dev_desc->lba = ((unsigned long) iobuf[0] << 24) +
1494 ((unsigned long) iobuf[1] << 16) +
1495 ((unsigned long) iobuf[2] << 8) + ((unsigned long) iobuf[3]);
1496 dev_desc->blksz = ((unsigned long) iobuf[4] << 24) +
1497 ((unsigned long) iobuf[5] << 16) +
1498 ((unsigned long) iobuf[6] << 8) + ((unsigned long) iobuf[7]);
Egbert Eich2eec2ab2013-04-09 21:11:56 +00001499 dev_desc->log2blksz = LOG2(dev_desc->blksz);
wdenkc35ba4e2004-03-14 22:25:36 +00001500#ifdef CONFIG_LBA48
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001501 /* ATAPI devices cannot use 48bit addressing (ATA/ATAPI v7) */
1502 dev_desc->lba48 = 0;
wdenkc35ba4e2004-03-14 22:25:36 +00001503#endif
wdenkc6097192002-11-03 00:24:07 +00001504 return;
1505}
1506
1507
1508/*
1509 * atapi_read:
1510 * we transfer only one block per command, since the multiple DRQ per
1511 * command is not yet implemented
1512 */
1513#define ATAPI_READ_MAX_BYTES 2048 /* we read max 2kbytes */
1514#define ATAPI_READ_BLOCK_SIZE 2048 /* assuming CD part */
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001515#define ATAPI_READ_MAX_BLOCK (ATAPI_READ_MAX_BYTES/ATAPI_READ_BLOCK_SIZE)
wdenkc6097192002-11-03 00:24:07 +00001516
Simon Glass11926fb2012-10-29 05:24:04 +00001517ulong atapi_read(int device, ulong blknr, lbaint_t blkcnt, void *buffer)
wdenkc6097192002-11-03 00:24:07 +00001518{
1519 ulong n = 0;
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001520 unsigned char ccb[12]; /* Command descriptor block */
wdenkc6097192002-11-03 00:24:07 +00001521 ulong cnt;
1522
Simon Glass11926fb2012-10-29 05:24:04 +00001523 debug("atapi_read dev %d start %lX, blocks " LBAF " buffer at %lX\n",
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001524 device, blknr, blkcnt, (ulong) buffer);
wdenkc6097192002-11-03 00:24:07 +00001525
1526 do {
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001527 if (blkcnt > ATAPI_READ_MAX_BLOCK)
1528 cnt = ATAPI_READ_MAX_BLOCK;
1529 else
1530 cnt = blkcnt;
wdenkc6097192002-11-03 00:24:07 +00001531
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001532 ccb[0] = ATAPI_CMD_READ_12;
1533 ccb[1] = 0; /* reserved */
1534 ccb[2] = (unsigned char) (blknr >> 24) & 0xFF; /* MSB Block */
1535 ccb[3] = (unsigned char) (blknr >> 16) & 0xFF; /* */
1536 ccb[4] = (unsigned char) (blknr >> 8) & 0xFF;
1537 ccb[5] = (unsigned char) blknr & 0xFF; /* LSB Block */
1538 ccb[6] = (unsigned char) (cnt >> 24) & 0xFF; /* MSB Block cnt */
1539 ccb[7] = (unsigned char) (cnt >> 16) & 0xFF;
1540 ccb[8] = (unsigned char) (cnt >> 8) & 0xFF;
1541 ccb[9] = (unsigned char) cnt & 0xFF; /* LSB Block */
1542 ccb[10] = 0; /* reserved */
1543 ccb[11] = 0; /* reserved */
1544
1545 if (atapi_issue_autoreq(device, ccb, 12,
1546 (unsigned char *) buffer,
1547 cnt * ATAPI_READ_BLOCK_SIZE)
1548 == 0xFF) {
wdenkc6097192002-11-03 00:24:07 +00001549 return (n);
1550 }
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001551 n += cnt;
1552 blkcnt -= cnt;
1553 blknr += cnt;
1554 buffer += (cnt * ATAPI_READ_BLOCK_SIZE);
wdenkc6097192002-11-03 00:24:07 +00001555 } while (blkcnt > 0);
1556 return (n);
1557}
1558
1559/* ------------------------------------------------------------------------- */
1560
1561#endif /* CONFIG_ATAPI */
1562
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001563U_BOOT_CMD(ide, 5, 1, do_ide,
1564 "IDE sub-system",
1565 "reset - reset IDE controller\n"
1566 "ide info - show available IDE devices\n"
1567 "ide device [dev] - show or set current device\n"
1568 "ide part [dev] - print partition table of one or all IDE devices\n"
1569 "ide read addr blk# cnt\n"
1570 "ide write addr blk# cnt - read/write `cnt'"
1571 " blocks starting at block `blk#'\n"
1572 " to/from memory address `addr'");
wdenk57b2d802003-06-27 21:31:46 +00001573
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001574U_BOOT_CMD(diskboot, 3, 1, do_diskboot,
1575 "boot from IDE device", "loadAddr dev:part");