blob: a46d0acfd63895277f1763d3a6d93e6d28ed3409 [file] [log] [blame]
wdenkc6097192002-11-03 00:24:07 +00001/*
Wolfgang Denk34c202c2011-10-29 09:41:40 +00002 * (C) Copyright 2000-2011
wdenkc6097192002-11-03 00:24:07 +00003 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02005 * SPDX-License-Identifier: GPL-2.0+
wdenkc6097192002-11-03 00:24:07 +00006 */
7
8/*
9 * IDE support
10 */
Albert Aribaud113bfe42010-08-08 05:17:06 +053011
wdenkc6097192002-11-03 00:24:07 +000012#include <common.h>
Simon Glass2a981dc2016-02-29 15:25:52 -070013#include <blk.h>
wdenkc6097192002-11-03 00:24:07 +000014#include <config.h>
15#include <watchdog.h>
16#include <command.h>
17#include <image.h>
18#include <asm/byteorder.h>
Heiko Schocherf98984c2007-08-28 17:39:14 +020019#include <asm/io.h>
Grant Likely735dd972007-02-20 09:04:34 +010020
wdenkc6097192002-11-03 00:24:07 +000021#if defined(CONFIG_IDE_8xx_DIRECT) || defined(CONFIG_IDE_PCMCIA)
22# include <pcmcia.h>
23#endif
Grant Likely735dd972007-02-20 09:04:34 +010024
wdenkc6097192002-11-03 00:24:07 +000025#include <ide.h>
26#include <ata.h>
Grant Likely735dd972007-02-20 09:04:34 +010027
wdenkc6097192002-11-03 00:24:07 +000028#ifdef CONFIG_STATUS_LED
29# include <status_led.h>
30#endif
Grant Likely735dd972007-02-20 09:04:34 +010031
wdenk5cf91d62004-04-23 20:32:05 +000032#ifdef __PPC__
33# define EIEIO __asm__ volatile ("eieio")
wdenk1a344f22005-02-03 23:00:49 +000034# define SYNC __asm__ volatile ("sync")
wdenk5cf91d62004-04-23 20:32:05 +000035#else
36# define EIEIO /* nothing */
wdenk1a344f22005-02-03 23:00:49 +000037# define SYNC /* nothing */
wdenkc6097192002-11-03 00:24:07 +000038#endif
39
wdenkc6097192002-11-03 00:24:07 +000040/* ------------------------------------------------------------------------- */
41
42/* Current I/O Device */
43static int curr_device = -1;
44
45/* Current offset for IDE0 / IDE1 bus access */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020046ulong ide_bus_offset[CONFIG_SYS_IDE_MAXBUS] = {
47#if defined(CONFIG_SYS_ATA_IDE0_OFFSET)
48 CONFIG_SYS_ATA_IDE0_OFFSET,
wdenkc6097192002-11-03 00:24:07 +000049#endif
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020050#if defined(CONFIG_SYS_ATA_IDE1_OFFSET) && (CONFIG_SYS_IDE_MAXBUS > 1)
51 CONFIG_SYS_ATA_IDE1_OFFSET,
wdenkc6097192002-11-03 00:24:07 +000052#endif
53};
54
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020055static int ide_bus_ok[CONFIG_SYS_IDE_MAXBUS];
wdenkc6097192002-11-03 00:24:07 +000056
Simon Glass4101f682016-02-29 15:25:34 -070057struct blk_desc ide_dev_desc[CONFIG_SYS_IDE_MAXDEVICE];
wdenkc6097192002-11-03 00:24:07 +000058/* ------------------------------------------------------------------------- */
59
wdenkc6097192002-11-03 00:24:07 +000060#ifdef CONFIG_IDE_RESET
Simon Glass53dbcdd2016-05-01 11:35:59 -060061static void ide_reset(void);
wdenkc6097192002-11-03 00:24:07 +000062#else
63#define ide_reset() /* dummy */
64#endif
65
Simon Glass4101f682016-02-29 15:25:34 -070066static void ide_ident(struct blk_desc *dev_desc);
Simon Glass53dbcdd2016-05-01 11:35:59 -060067static uchar ide_wait(int dev, ulong t);
wdenkc6097192002-11-03 00:24:07 +000068
69#define IDE_TIME_OUT 2000 /* 2 sec timeout */
70
71#define ATAPI_TIME_OUT 7000 /* 7 sec timeout (5 sec seems to work...) */
72
73#define IDE_SPIN_UP_TIME_OUT 5000 /* 5 sec spin-up timeout */
74
Simon Glass53dbcdd2016-05-01 11:35:59 -060075static void ident_cpy(unsigned char *dest, unsigned char *src,
76 unsigned int len);
wdenkc6097192002-11-03 00:24:07 +000077
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020078#ifndef CONFIG_SYS_ATA_PORT_ADDR
79#define CONFIG_SYS_ATA_PORT_ADDR(port) (port)
Heiko Schocher566a4942007-06-22 19:11:54 +020080#endif
wdenkc6097192002-11-03 00:24:07 +000081
82#ifdef CONFIG_ATAPI
Simon Glass53dbcdd2016-05-01 11:35:59 -060083static void atapi_inquiry(struct blk_desc *dev_desc);
Simon Glass4101f682016-02-29 15:25:34 -070084static ulong atapi_read(struct blk_desc *block_dev, lbaint_t blknr,
Stephen Warren7c4213f2015-12-07 11:38:48 -070085 lbaint_t blkcnt, void *buffer);
wdenkc6097192002-11-03 00:24:07 +000086#endif
87
88
wdenkc6097192002-11-03 00:24:07 +000089/* ------------------------------------------------------------------------- */
90
Wolfgang Denk34c202c2011-10-29 09:41:40 +000091int do_ide(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
wdenkc6097192002-11-03 00:24:07 +000092{
Wolfgang Denk34c202c2011-10-29 09:41:40 +000093 int rcode = 0;
wdenkc6097192002-11-03 00:24:07 +000094
Wolfgang Denk34c202c2011-10-29 09:41:40 +000095 switch (argc) {
96 case 0:
97 case 1:
Simon Glass4c12eeb2011-12-10 08:44:01 +000098 return CMD_RET_USAGE;
Wolfgang Denk34c202c2011-10-29 09:41:40 +000099 case 2:
100 if (strncmp(argv[1], "res", 3) == 0) {
101 puts("\nReset IDE"
102#ifdef CONFIG_IDE_8xx_DIRECT
103 " on PCMCIA " PCMCIA_SLOT_MSG
104#endif
105 ": ");
wdenkc6097192002-11-03 00:24:07 +0000106
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000107 ide_init();
108 return 0;
109 } else if (strncmp(argv[1], "inf", 3) == 0) {
110 int i;
111
112 putc('\n');
113
114 for (i = 0; i < CONFIG_SYS_IDE_MAXDEVICE; ++i) {
115 if (ide_dev_desc[i].type == DEV_TYPE_UNKNOWN)
116 continue; /* list only known devices */
117 printf("IDE device %d: ", i);
118 dev_print(&ide_dev_desc[i]);
119 }
120 return 0;
121
122 } else if (strncmp(argv[1], "dev", 3) == 0) {
123 if ((curr_device < 0)
124 || (curr_device >= CONFIG_SYS_IDE_MAXDEVICE)) {
125 puts("\nno IDE devices available\n");
126 return 1;
127 }
128 printf("\nIDE device %d: ", curr_device);
129 dev_print(&ide_dev_desc[curr_device]);
130 return 0;
131 } else if (strncmp(argv[1], "part", 4) == 0) {
132 int dev, ok;
133
134 for (ok = 0, dev = 0;
135 dev < CONFIG_SYS_IDE_MAXDEVICE;
136 ++dev) {
137 if (ide_dev_desc[dev].part_type !=
138 PART_TYPE_UNKNOWN) {
139 ++ok;
140 if (dev)
141 putc('\n');
Simon Glass3e8bd462016-02-29 15:25:48 -0700142 part_print(&ide_dev_desc[dev]);
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000143 }
144 }
145 if (!ok) {
146 puts("\nno IDE devices available\n");
147 rcode++;
148 }
149 return rcode;
150 }
Simon Glass4c12eeb2011-12-10 08:44:01 +0000151 return CMD_RET_USAGE;
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000152 case 3:
153 if (strncmp(argv[1], "dev", 3) == 0) {
Simon Glass53dbcdd2016-05-01 11:35:59 -0600154 int dev = (int)simple_strtoul(argv[2], NULL, 10);
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000155
156 printf("\nIDE device %d: ", dev);
157 if (dev >= CONFIG_SYS_IDE_MAXDEVICE) {
158 puts("unknown device\n");
159 return 1;
160 }
161 dev_print(&ide_dev_desc[dev]);
162 /*ide_print (dev); */
163
164 if (ide_dev_desc[dev].type == DEV_TYPE_UNKNOWN)
165 return 1;
166
167 curr_device = dev;
168
169 puts("... is now current device\n");
170
171 return 0;
172 } else if (strncmp(argv[1], "part", 4) == 0) {
Simon Glass53dbcdd2016-05-01 11:35:59 -0600173 int dev = (int)simple_strtoul(argv[2], NULL, 10);
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000174
175 if (ide_dev_desc[dev].part_type != PART_TYPE_UNKNOWN) {
Simon Glass3e8bd462016-02-29 15:25:48 -0700176 part_print(&ide_dev_desc[dev]);
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000177 } else {
178 printf("\nIDE device %d not available\n",
179 dev);
180 rcode = 1;
181 }
182 return rcode;
183 }
184
Simon Glass4c12eeb2011-12-10 08:44:01 +0000185 return CMD_RET_USAGE;
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000186 default:
187 /* at least 4 args */
188
189 if (strcmp(argv[1], "read") == 0) {
190 ulong addr = simple_strtoul(argv[2], NULL, 16);
191 ulong cnt = simple_strtoul(argv[4], NULL, 16);
Simon Glass4101f682016-02-29 15:25:34 -0700192 struct blk_desc *dev_desc;
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000193 ulong n;
194
195#ifdef CONFIG_SYS_64BIT_LBA
196 lbaint_t blk = simple_strtoull(argv[3], NULL, 16);
197
Simon Glass53dbcdd2016-05-01 11:35:59 -0600198 printf("\nIDE read: device %d block # %lld, count %ld...",
199 curr_device, blk, cnt);
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000200#else
201 lbaint_t blk = simple_strtoul(argv[3], NULL, 16);
202
Simon Glass53dbcdd2016-05-01 11:35:59 -0600203 printf("\nIDE read: device %d block # %ld, count %ld...",
204 curr_device, blk, cnt);
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000205#endif
206
Stephen Warren7c4213f2015-12-07 11:38:48 -0700207 dev_desc = &ide_dev_desc[curr_device];
Simon Glass2a981dc2016-02-29 15:25:52 -0700208 n = blk_dread(dev_desc, blk, cnt, (ulong *)addr);
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000209 /* flush cache after read */
210 flush_cache(addr,
211 cnt * ide_dev_desc[curr_device].blksz);
212
213 printf("%ld blocks read: %s\n",
214 n, (n == cnt) ? "OK" : "ERROR");
215 if (n == cnt)
216 return 0;
217 else
218 return 1;
219 } else if (strcmp(argv[1], "write") == 0) {
220 ulong addr = simple_strtoul(argv[2], NULL, 16);
221 ulong cnt = simple_strtoul(argv[4], NULL, 16);
222 ulong n;
223
224#ifdef CONFIG_SYS_64BIT_LBA
225 lbaint_t blk = simple_strtoull(argv[3], NULL, 16);
226
Simon Glass53dbcdd2016-05-01 11:35:59 -0600227 printf("\nIDE write: device %d block # %lld, count %ld...",
228 curr_device, blk, cnt);
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000229#else
230 lbaint_t blk = simple_strtoul(argv[3], NULL, 16);
231
Simon Glass53dbcdd2016-05-01 11:35:59 -0600232 printf("\nIDE write: device %d block # %ld, count %ld...",
233 curr_device, blk, cnt);
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000234#endif
Stephen Warren7c4213f2015-12-07 11:38:48 -0700235 n = ide_write(&ide_dev_desc[curr_device], blk, cnt,
236 (ulong *)addr);
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000237
238 printf("%ld blocks written: %s\n",
239 n, (n == cnt) ? "OK" : "ERROR");
240 if (n == cnt)
241 return 0;
242 else
243 return 1;
244 } else {
Simon Glass4c12eeb2011-12-10 08:44:01 +0000245 return CMD_RET_USAGE;
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000246 }
247
248 return rcode;
249 }
wdenkc6097192002-11-03 00:24:07 +0000250}
251
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000252int do_diskboot(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
wdenkc6097192002-11-03 00:24:07 +0000253{
Rob Herring7405a132012-09-21 04:02:30 +0000254 return common_diskboot(cmdtp, "ide", argc, argv);
wdenkc6097192002-11-03 00:24:07 +0000255}
256
257/* ------------------------------------------------------------------------- */
258
Jeroen Hofstee288afdc2014-07-12 15:07:19 +0200259__weak void ide_led(uchar led, uchar status)
Pavel Herrmann19be2ea2012-10-07 05:56:10 +0000260{
261#if defined(CONFIG_IDE_LED) && defined(PER8_BASE) /* required by LED_PORT */
262 static uchar led_buffer; /* Buffer for current LED status */
263
264 uchar *led_port = LED_PORT;
265
266 if (status) /* switch LED on */
267 led_buffer |= led;
268 else /* switch LED off */
269 led_buffer &= ~led;
270
271 *led_port = led_buffer;
272#endif
273}
274
Pavel Herrmann19be2ea2012-10-07 05:56:10 +0000275#ifndef CONFIG_IDE_LED /* define LED macros, they are not used anyways */
276# define DEVICE_LED(x) 0
277# define LED_IDE1 1
278# define LED_IDE2 2
279#endif
280
281/* ------------------------------------------------------------------------- */
282
Jeroen Hofstee288afdc2014-07-12 15:07:19 +0200283__weak void ide_outb(int dev, int port, unsigned char val)
Stefan Roesef2302d42008-08-06 14:05:38 +0200284{
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000285 debug("ide_outb (dev= %d, port= 0x%x, val= 0x%02x) : @ 0x%08lx\n",
286 dev, port, val,
287 (ATA_CURR_BASE(dev) + CONFIG_SYS_ATA_PORT_ADDR(port)));
Macpaul Lin0abddf82011-04-11 20:45:32 +0000288
289#if defined(CONFIG_IDE_AHB)
290 if (port) {
291 /* write command */
292 ide_write_register(dev, port, val);
293 } else {
294 /* write data */
295 outb(val, (ATA_CURR_BASE(dev)));
296 }
297#else
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000298 outb(val, (ATA_CURR_BASE(dev) + CONFIG_SYS_ATA_PORT_ADDR(port)));
Macpaul Lin0abddf82011-04-11 20:45:32 +0000299#endif
Stefan Roesef2302d42008-08-06 14:05:38 +0200300}
Macpaul Lin0abddf82011-04-11 20:45:32 +0000301
Jeroen Hofstee288afdc2014-07-12 15:07:19 +0200302__weak unsigned char ide_inb(int dev, int port)
Stefan Roesef2302d42008-08-06 14:05:38 +0200303{
304 uchar val;
Macpaul Lin0abddf82011-04-11 20:45:32 +0000305
306#if defined(CONFIG_IDE_AHB)
307 val = ide_read_register(dev, port);
308#else
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000309 val = inb((ATA_CURR_BASE(dev) + CONFIG_SYS_ATA_PORT_ADDR(port)));
Macpaul Lin0abddf82011-04-11 20:45:32 +0000310#endif
311
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000312 debug("ide_inb (dev= %d, port= 0x%x) : @ 0x%08lx -> 0x%02x\n",
313 dev, port,
314 (ATA_CURR_BASE(dev) + CONFIG_SYS_ATA_PORT_ADDR(port)), val);
Stefan Roesef2302d42008-08-06 14:05:38 +0200315 return val;
316}
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000317
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000318void ide_init(void)
wdenkc6097192002-11-03 00:24:07 +0000319{
wdenkc6097192002-11-03 00:24:07 +0000320 unsigned char c;
321 int i, bus;
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000322
wdenk9fd5e312003-12-07 23:55:12 +0000323#ifdef CONFIG_IDE_8xx_PCCARD
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000324 extern int ide_devices_found; /* Initialized in check_ide_device() */
325#endif /* CONFIG_IDE_8xx_PCCARD */
wdenk9fd5e312003-12-07 23:55:12 +0000326
327#ifdef CONFIG_IDE_PREINIT
328 WATCHDOG_RESET();
329
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000330 if (ide_preinit()) {
331 puts("ide_preinit failed\n");
wdenk9fd5e312003-12-07 23:55:12 +0000332 return;
333 }
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000334#endif /* CONFIG_IDE_PREINIT */
wdenkc6097192002-11-03 00:24:07 +0000335
wdenkc6097192002-11-03 00:24:07 +0000336 WATCHDOG_RESET();
337
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000338 /*
339 * Reset the IDE just to be sure.
wdenkc6097192002-11-03 00:24:07 +0000340 * Light LED's to show
341 */
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000342 ide_led((LED_IDE1 | LED_IDE2), 1); /* LED's on */
343
344 /* ATAPI Drives seems to need a proper IDE Reset */
345 ide_reset();
wdenkc6097192002-11-03 00:24:07 +0000346
Pavel Herrmann8d1165e11a2012-10-09 07:01:56 +0000347#ifdef CONFIG_IDE_INIT_POSTRESET
348 WATCHDOG_RESET();
wdenkc6097192002-11-03 00:24:07 +0000349
Pavel Herrmann8d1165e11a2012-10-09 07:01:56 +0000350 if (ide_init_postreset()) {
351 puts("ide_preinit_postreset failed\n");
352 return;
353 }
354#endif /* CONFIG_IDE_INIT_POSTRESET */
wdenkc6097192002-11-03 00:24:07 +0000355
356 /*
357 * Wait for IDE to get ready.
358 * According to spec, this can take up to 31 seconds!
359 */
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000360 for (bus = 0; bus < CONFIG_SYS_IDE_MAXBUS; ++bus) {
361 int dev =
362 bus * (CONFIG_SYS_IDE_MAXDEVICE /
363 CONFIG_SYS_IDE_MAXBUS);
wdenkc6097192002-11-03 00:24:07 +0000364
wdenk6069ff22003-02-28 00:49:47 +0000365#ifdef CONFIG_IDE_8xx_PCCARD
366 /* Skip non-ide devices from probing */
367 if ((ide_devices_found & (1 << bus)) == 0) {
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000368 ide_led((LED_IDE1 | LED_IDE2), 0); /* LED's off */
wdenk6069ff22003-02-28 00:49:47 +0000369 continue;
370 }
371#endif
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000372 printf("Bus %d: ", bus);
wdenkc6097192002-11-03 00:24:07 +0000373
374 ide_bus_ok[bus] = 0;
375
376 /* Select device
377 */
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000378 udelay(100000); /* 100 ms */
379 ide_outb(dev, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(dev));
380 udelay(100000); /* 100 ms */
wdenkc6097192002-11-03 00:24:07 +0000381 i = 0;
382 do {
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000383 udelay(10000); /* 10 ms */
wdenkc6097192002-11-03 00:24:07 +0000384
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000385 c = ide_inb(dev, ATA_STATUS);
wdenkc6097192002-11-03 00:24:07 +0000386 i++;
387 if (i > (ATA_RESET_TIME * 100)) {
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000388 puts("** Timeout **\n");
389 /* LED's off */
390 ide_led((LED_IDE1 | LED_IDE2), 0);
wdenkc6097192002-11-03 00:24:07 +0000391 return;
392 }
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000393 if ((i >= 100) && ((i % 100) == 0))
394 putc('.');
395
wdenkc6097192002-11-03 00:24:07 +0000396 } while (c & ATA_STAT_BUSY);
397
398 if (c & (ATA_STAT_BUSY | ATA_STAT_FAULT)) {
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000399 puts("not available ");
400 debug("Status = 0x%02X ", c);
401#ifndef CONFIG_ATAPI /* ATAPI Devices do not set DRDY */
402 } else if ((c & ATA_STAT_READY) == 0) {
403 puts("not available ");
404 debug("Status = 0x%02X ", c);
wdenkc6097192002-11-03 00:24:07 +0000405#endif
406 } else {
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000407 puts("OK ");
wdenkc6097192002-11-03 00:24:07 +0000408 ide_bus_ok[bus] = 1;
409 }
410 WATCHDOG_RESET();
411 }
wdenkc7de8292002-11-19 11:04:11 +0000412
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000413 putc('\n');
wdenkc6097192002-11-03 00:24:07 +0000414
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000415 ide_led((LED_IDE1 | LED_IDE2), 0); /* LED's off */
wdenkc6097192002-11-03 00:24:07 +0000416
417 curr_device = -1;
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000418 for (i = 0; i < CONFIG_SYS_IDE_MAXDEVICE; ++i) {
wdenkc6097192002-11-03 00:24:07 +0000419 int led = (IDE_BUS(i) == 0) ? LED_IDE1 : LED_IDE2;
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000420 ide_dev_desc[i].type = DEV_TYPE_UNKNOWN;
421 ide_dev_desc[i].if_type = IF_TYPE_IDE;
Simon Glassbcce53d2016-02-29 15:25:51 -0700422 ide_dev_desc[i].devnum = i;
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000423 ide_dev_desc[i].part_type = PART_TYPE_UNKNOWN;
424 ide_dev_desc[i].blksz = 0;
Egbert Eich0472fbf2013-04-09 21:11:56 +0000425 ide_dev_desc[i].log2blksz =
426 LOG2_INVALID(typeof(ide_dev_desc[i].log2blksz));
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000427 ide_dev_desc[i].lba = 0;
428 ide_dev_desc[i].block_read = ide_read;
Macpaul Lin0abddf82011-04-11 20:45:32 +0000429 ide_dev_desc[i].block_write = ide_write;
wdenkc6097192002-11-03 00:24:07 +0000430 if (!ide_bus_ok[IDE_BUS(i)])
431 continue;
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000432 ide_led(led, 1); /* LED on */
wdenkc6097192002-11-03 00:24:07 +0000433 ide_ident(&ide_dev_desc[i]);
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000434 ide_led(led, 0); /* LED off */
wdenkc6097192002-11-03 00:24:07 +0000435 dev_print(&ide_dev_desc[i]);
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000436
wdenkc6097192002-11-03 00:24:07 +0000437 if ((ide_dev_desc[i].lba > 0) && (ide_dev_desc[i].blksz > 0)) {
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000438 /* initialize partition type */
Simon Glass3e8bd462016-02-29 15:25:48 -0700439 part_init(&ide_dev_desc[i]);
wdenkc6097192002-11-03 00:24:07 +0000440 if (curr_device < 0)
441 curr_device = i;
442 }
443 }
444 WATCHDOG_RESET();
445}
446
447/* ------------------------------------------------------------------------- */
448
Matthew McClintockdf3fc522011-05-24 05:31:19 +0000449#ifdef CONFIG_PARTITIONS
Simon Glass4101f682016-02-29 15:25:34 -0700450struct blk_desc *ide_get_dev(int dev)
wdenkc6097192002-11-03 00:24:07 +0000451{
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200452 return (dev < CONFIG_SYS_IDE_MAXDEVICE) ? &ide_dev_desc[dev] : NULL;
wdenkc6097192002-11-03 00:24:07 +0000453}
Matthew McClintockdf3fc522011-05-24 05:31:19 +0000454#endif
wdenkc6097192002-11-03 00:24:07 +0000455
wdenkc6097192002-11-03 00:24:07 +0000456/* ------------------------------------------------------------------------- */
457
wdenk5da627a2003-10-09 20:09:04 +0000458/* We only need to swap data if we are running on a big endian cpu. */
Pavel Herrmann4d1361d2012-10-09 07:10:08 +0000459#if defined(__LITTLE_ENDIAN)
Jeroen Hofstee288afdc2014-07-12 15:07:19 +0200460__weak void ide_input_swap_data(int dev, ulong *sect_buf, int words)
Pavel Herrmannf5b82c02012-10-09 07:04:39 +0000461{
462 ide_input_data(dev, sect_buf, words);
463}
wdenk5da627a2003-10-09 20:09:04 +0000464#else
Jeroen Hofstee288afdc2014-07-12 15:07:19 +0200465__weak void ide_input_swap_data(int dev, ulong *sect_buf, int words)
wdenkc6097192002-11-03 00:24:07 +0000466{
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000467 volatile ushort *pbuf =
468 (ushort *) (ATA_CURR_BASE(dev) + ATA_DATA_REG);
469 ushort *dbuf = (ushort *) sect_buf;
wdenk1a344f22005-02-03 23:00:49 +0000470
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000471 debug("in input swap data base for read is %lx\n",
472 (unsigned long) pbuf);
wdenk1a344f22005-02-03 23:00:49 +0000473
474 while (words--) {
Wolfgang Denk0c32d962006-06-16 17:32:31 +0200475#ifdef __MIPS__
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000476 *dbuf++ = swab16p((u16 *) pbuf);
477 *dbuf++ = swab16p((u16 *) pbuf);
Wolfgang Denk0c32d962006-06-16 17:32:31 +0200478#else
wdenk1a344f22005-02-03 23:00:49 +0000479 *dbuf++ = ld_le16(pbuf);
480 *dbuf++ = ld_le16(pbuf);
Wolfgang Denk0c32d962006-06-16 17:32:31 +0200481#endif /* !MIPS */
wdenk1a344f22005-02-03 23:00:49 +0000482 }
wdenkc6097192002-11-03 00:24:07 +0000483}
Pavel Herrmann4d1361d2012-10-09 07:10:08 +0000484#endif /* __LITTLE_ENDIAN */
wdenkc6097192002-11-03 00:24:07 +0000485
wdenk2262cfe2002-11-18 00:14:45 +0000486
Albert Aribaudf2a37fc2010-08-08 05:17:05 +0530487#if defined(CONFIG_IDE_SWAP_IO)
Jeroen Hofstee288afdc2014-07-12 15:07:19 +0200488__weak void ide_output_data(int dev, const ulong *sect_buf, int words)
wdenkc6097192002-11-03 00:24:07 +0000489{
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000490 ushort *dbuf;
491 volatile ushort *pbuf;
wdenk1a344f22005-02-03 23:00:49 +0000492
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000493 pbuf = (ushort *) (ATA_CURR_BASE(dev) + ATA_DATA_REG);
494 dbuf = (ushort *) sect_buf;
wdenk1a344f22005-02-03 23:00:49 +0000495 while (words--) {
496 EIEIO;
497 *pbuf = *dbuf++;
498 EIEIO;
499 *pbuf = *dbuf++;
500 }
wdenkc6097192002-11-03 00:24:07 +0000501}
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000502#else /* ! CONFIG_IDE_SWAP_IO */
Jeroen Hofstee288afdc2014-07-12 15:07:19 +0200503__weak void ide_output_data(int dev, const ulong *sect_buf, int words)
wdenk2262cfe2002-11-18 00:14:45 +0000504{
Macpaul Lin0abddf82011-04-11 20:45:32 +0000505#if defined(CONFIG_IDE_AHB)
506 ide_write_data(dev, sect_buf, words);
507#else
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000508 outsw(ATA_CURR_BASE(dev) + ATA_DATA_REG, sect_buf, words << 1);
Macpaul Lin0abddf82011-04-11 20:45:32 +0000509#endif
wdenk2262cfe2002-11-18 00:14:45 +0000510}
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000511#endif /* CONFIG_IDE_SWAP_IO */
wdenkc6097192002-11-03 00:24:07 +0000512
Albert Aribaudf2a37fc2010-08-08 05:17:05 +0530513#if defined(CONFIG_IDE_SWAP_IO)
Jeroen Hofstee288afdc2014-07-12 15:07:19 +0200514__weak void ide_input_data(int dev, ulong *sect_buf, int words)
wdenkc6097192002-11-03 00:24:07 +0000515{
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000516 ushort *dbuf;
517 volatile ushort *pbuf;
wdenk1a344f22005-02-03 23:00:49 +0000518
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000519 pbuf = (ushort *) (ATA_CURR_BASE(dev) + ATA_DATA_REG);
520 dbuf = (ushort *) sect_buf;
wdenk1a344f22005-02-03 23:00:49 +0000521
522 debug("in input data base for read is %lx\n", (unsigned long) pbuf);
523
524 while (words--) {
525 EIEIO;
526 *dbuf++ = *pbuf;
527 EIEIO;
528 *dbuf++ = *pbuf;
529 }
wdenkc6097192002-11-03 00:24:07 +0000530}
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000531#else /* ! CONFIG_IDE_SWAP_IO */
Jeroen Hofstee288afdc2014-07-12 15:07:19 +0200532__weak void ide_input_data(int dev, ulong *sect_buf, int words)
wdenk2262cfe2002-11-18 00:14:45 +0000533{
Macpaul Lin0abddf82011-04-11 20:45:32 +0000534#if defined(CONFIG_IDE_AHB)
535 ide_read_data(dev, sect_buf, words);
536#else
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000537 insw(ATA_CURR_BASE(dev) + ATA_DATA_REG, sect_buf, words << 1);
Macpaul Lin0abddf82011-04-11 20:45:32 +0000538#endif
wdenk2262cfe2002-11-18 00:14:45 +0000539}
540
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000541#endif /* CONFIG_IDE_SWAP_IO */
wdenkc6097192002-11-03 00:24:07 +0000542
543/* -------------------------------------------------------------------------
544 */
Simon Glass4101f682016-02-29 15:25:34 -0700545static void ide_ident(struct blk_desc *dev_desc)
wdenkc6097192002-11-03 00:24:07 +0000546{
wdenkc6097192002-11-03 00:24:07 +0000547 unsigned char c;
Marek Vasutb18eabf2011-08-20 09:15:13 +0000548 hd_driveid_t iop;
wdenkc6097192002-11-03 00:24:07 +0000549
wdenk64f70be2004-09-28 20:34:50 +0000550#ifdef CONFIG_ATAPI
551 int retries = 0;
wdenkc7de8292002-11-19 11:04:11 +0000552#endif
wdenkc6097192002-11-03 00:24:07 +0000553 int device;
wdenkc6097192002-11-03 00:24:07 +0000554
Simon Glassbcce53d2016-02-29 15:25:51 -0700555 device = dev_desc->devnum;
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000556 printf(" Device %d: ", device);
557
558 ide_led(DEVICE_LED(device), 1); /* LED on */
wdenkc6097192002-11-03 00:24:07 +0000559 /* Select device
560 */
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000561 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
562 dev_desc->if_type = IF_TYPE_IDE;
wdenkc6097192002-11-03 00:24:07 +0000563#ifdef CONFIG_ATAPI
wdenkc7de8292002-11-19 11:04:11 +0000564
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000565 retries = 0;
wdenkc7de8292002-11-19 11:04:11 +0000566
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000567 /* Warning: This will be tricky to read */
568 while (retries <= 1) {
569 /* check signature */
570 if ((ide_inb(device, ATA_SECT_CNT) == 0x01) &&
571 (ide_inb(device, ATA_SECT_NUM) == 0x01) &&
572 (ide_inb(device, ATA_CYL_LOW) == 0x14) &&
573 (ide_inb(device, ATA_CYL_HIGH) == 0xEB)) {
574 /* ATAPI Signature found */
575 dev_desc->if_type = IF_TYPE_ATAPI;
576 /*
577 * Start Ident Command
578 */
579 ide_outb(device, ATA_COMMAND, ATAPI_CMD_IDENT);
580 /*
581 * Wait for completion - ATAPI devices need more time
582 * to become ready
583 */
584 c = ide_wait(device, ATAPI_TIME_OUT);
585 } else
wdenkc6097192002-11-03 00:24:07 +0000586#endif
wdenk1a344f22005-02-03 23:00:49 +0000587 {
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000588 /*
589 * Start Ident Command
590 */
591 ide_outb(device, ATA_COMMAND, ATA_CMD_IDENT);
592
593 /*
594 * Wait for completion
595 */
596 c = ide_wait(device, IDE_TIME_OUT);
wdenk1a344f22005-02-03 23:00:49 +0000597 }
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000598 ide_led(DEVICE_LED(device), 0); /* LED off */
599
600 if (((c & ATA_STAT_DRQ) == 0) ||
601 ((c & (ATA_STAT_FAULT | ATA_STAT_ERR)) != 0)) {
wdenk64f70be2004-09-28 20:34:50 +0000602#ifdef CONFIG_ATAPI
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000603 {
604 /*
605 * Need to soft reset the device
606 * in case it's an ATAPI...
607 */
608 debug("Retrying...\n");
609 ide_outb(device, ATA_DEV_HD,
610 ATA_LBA | ATA_DEVICE(device));
611 udelay(100000);
612 ide_outb(device, ATA_COMMAND, 0x08);
613 udelay(500000); /* 500 ms */
614 }
615 /*
616 * Select device
617 */
618 ide_outb(device, ATA_DEV_HD,
619 ATA_LBA | ATA_DEVICE(device));
620 retries++;
621#else
622 return;
623#endif
624 }
625#ifdef CONFIG_ATAPI
626 else
627 break;
628 } /* see above - ugly to read */
wdenk64f70be2004-09-28 20:34:50 +0000629
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000630 if (retries == 2) /* Not found */
wdenk64f70be2004-09-28 20:34:50 +0000631 return;
632#endif
wdenkc7de8292002-11-19 11:04:11 +0000633
Pavel Herrmannf5b82c02012-10-09 07:04:39 +0000634 ide_input_swap_data(device, (ulong *)&iop, ATA_SECTORWORDS);
wdenkc6097192002-11-03 00:24:07 +0000635
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000636 ident_cpy((unsigned char *) dev_desc->revision, iop.fw_rev,
637 sizeof(dev_desc->revision));
638 ident_cpy((unsigned char *) dev_desc->vendor, iop.model,
639 sizeof(dev_desc->vendor));
640 ident_cpy((unsigned char *) dev_desc->product, iop.serial_no,
641 sizeof(dev_desc->product));
wdenkc3f9d492004-03-14 00:59:59 +0000642#ifdef __LITTLE_ENDIAN
643 /*
Richard Retanubunbcdf1d22008-11-06 14:01:51 -0500644 * firmware revision, model, and serial number have Big Endian Byte
645 * order in Word. Convert all three to little endian.
wdenkc3f9d492004-03-14 00:59:59 +0000646 *
647 * See CF+ and CompactFlash Specification Revision 2.0:
Richard Retanubunbcdf1d22008-11-06 14:01:51 -0500648 * 6.2.1.6: Identify Drive, Table 39 for more details
wdenkc3f9d492004-03-14 00:59:59 +0000649 */
650
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000651 strswab(dev_desc->revision);
652 strswab(dev_desc->vendor);
653 strswab(dev_desc->product);
wdenkc3f9d492004-03-14 00:59:59 +0000654#endif /* __LITTLE_ENDIAN */
wdenkc6097192002-11-03 00:24:07 +0000655
Marek Vasutb18eabf2011-08-20 09:15:13 +0000656 if ((iop.config & 0x0080) == 0x0080)
wdenkc6097192002-11-03 00:24:07 +0000657 dev_desc->removable = 1;
658 else
659 dev_desc->removable = 0;
660
wdenkc6097192002-11-03 00:24:07 +0000661#ifdef CONFIG_ATAPI
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000662 if (dev_desc->if_type == IF_TYPE_ATAPI) {
wdenkc6097192002-11-03 00:24:07 +0000663 atapi_inquiry(dev_desc);
664 return;
665 }
666#endif /* CONFIG_ATAPI */
667
wdenkc3f9d492004-03-14 00:59:59 +0000668#ifdef __BIG_ENDIAN
wdenkc6097192002-11-03 00:24:07 +0000669 /* swap shorts */
Marek Vasutb18eabf2011-08-20 09:15:13 +0000670 dev_desc->lba = (iop.lba_capacity << 16) | (iop.lba_capacity >> 16);
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000671#else /* ! __BIG_ENDIAN */
wdenkc3f9d492004-03-14 00:59:59 +0000672 /*
673 * do not swap shorts on little endian
674 *
675 * See CF+ and CompactFlash Specification Revision 2.0:
676 * 6.2.1.6: Identfy Drive, Table 39, Word Address 57-58 for details.
677 */
Marek Vasutb18eabf2011-08-20 09:15:13 +0000678 dev_desc->lba = iop.lba_capacity;
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000679#endif /* __BIG_ENDIAN */
wdenkc40b2952004-03-13 23:29:43 +0000680
wdenk42dfe7a2004-03-14 22:25:36 +0000681#ifdef CONFIG_LBA48
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000682 if (iop.command_set_2 & 0x0400) { /* LBA 48 support */
wdenk6e592382004-04-18 17:39:38 +0000683 dev_desc->lba48 = 1;
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000684 dev_desc->lba = (unsigned long long) iop.lba48_capacity[0] |
685 ((unsigned long long) iop.lba48_capacity[1] << 16) |
686 ((unsigned long long) iop.lba48_capacity[2] << 32) |
687 ((unsigned long long) iop.lba48_capacity[3] << 48);
wdenkc40b2952004-03-13 23:29:43 +0000688 } else {
wdenkc40b2952004-03-13 23:29:43 +0000689 dev_desc->lba48 = 0;
690 }
691#endif /* CONFIG_LBA48 */
wdenkc6097192002-11-03 00:24:07 +0000692 /* assuming HD */
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000693 dev_desc->type = DEV_TYPE_HARDDISK;
694 dev_desc->blksz = ATA_BLOCKSIZE;
Egbert Eich0472fbf2013-04-09 21:11:56 +0000695 dev_desc->log2blksz = LOG2(dev_desc->blksz);
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000696 dev_desc->lun = 0; /* just to fill something in... */
wdenkc6097192002-11-03 00:24:07 +0000697
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000698#if 0 /* only used to test the powersaving mode,
699 * if enabled, the drive goes after 5 sec
700 * in standby mode */
701 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
702 c = ide_wait(device, IDE_TIME_OUT);
703 ide_outb(device, ATA_SECT_CNT, 1);
704 ide_outb(device, ATA_LBA_LOW, 0);
705 ide_outb(device, ATA_LBA_MID, 0);
706 ide_outb(device, ATA_LBA_HIGH, 0);
707 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
708 ide_outb(device, ATA_COMMAND, 0xe3);
709 udelay(50);
710 c = ide_wait(device, IDE_TIME_OUT); /* can't take over 500 ms */
wdenkc6097192002-11-03 00:24:07 +0000711#endif
712}
713
714
715/* ------------------------------------------------------------------------- */
716
Simon Glass4101f682016-02-29 15:25:34 -0700717ulong ide_read(struct blk_desc *block_dev, lbaint_t blknr, lbaint_t blkcnt,
Stephen Warren7c4213f2015-12-07 11:38:48 -0700718 void *buffer)
wdenkc6097192002-11-03 00:24:07 +0000719{
Simon Glassbcce53d2016-02-29 15:25:51 -0700720 int device = block_dev->devnum;
wdenkc6097192002-11-03 00:24:07 +0000721 ulong n = 0;
722 unsigned char c;
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000723 unsigned char pwrsave = 0; /* power save */
724
wdenk42dfe7a2004-03-14 22:25:36 +0000725#ifdef CONFIG_LBA48
wdenkc40b2952004-03-13 23:29:43 +0000726 unsigned char lba48 = 0;
wdenkc6097192002-11-03 00:24:07 +0000727
Guennadi Liakhovetski413bf582008-04-28 14:36:06 +0200728 if (blknr & 0x0000fffff0000000ULL) {
wdenkc40b2952004-03-13 23:29:43 +0000729 /* more than 28 bits used, use 48bit mode */
730 lba48 = 1;
731 }
732#endif
Sascha Silbeff8fef52013-06-14 13:07:25 +0200733 debug("ide_read dev %d start " LBAF ", blocks " LBAF " buffer at %lX\n",
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000734 device, blknr, blkcnt, (ulong) buffer);
wdenkc6097192002-11-03 00:24:07 +0000735
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000736 ide_led(DEVICE_LED(device), 1); /* LED on */
wdenkc6097192002-11-03 00:24:07 +0000737
738 /* Select device
739 */
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000740 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
741 c = ide_wait(device, IDE_TIME_OUT);
wdenkc6097192002-11-03 00:24:07 +0000742
743 if (c & ATA_STAT_BUSY) {
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000744 printf("IDE read: device %d not ready\n", device);
wdenkc6097192002-11-03 00:24:07 +0000745 goto IDE_READ_E;
746 }
747
748 /* first check if the drive is in Powersaving mode, if yes,
749 * increase the timeout value */
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000750 ide_outb(device, ATA_COMMAND, ATA_CMD_CHK_PWR);
751 udelay(50);
wdenkc6097192002-11-03 00:24:07 +0000752
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000753 c = ide_wait(device, IDE_TIME_OUT); /* can't take over 500 ms */
wdenkc6097192002-11-03 00:24:07 +0000754
755 if (c & ATA_STAT_BUSY) {
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000756 printf("IDE read: device %d not ready\n", device);
wdenkc6097192002-11-03 00:24:07 +0000757 goto IDE_READ_E;
758 }
759 if ((c & ATA_STAT_ERR) == ATA_STAT_ERR) {
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000760 printf("No Powersaving mode %X\n", c);
wdenkc6097192002-11-03 00:24:07 +0000761 } else {
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000762 c = ide_inb(device, ATA_SECT_CNT);
763 debug("Powersaving %02X\n", c);
764 if (c == 0)
765 pwrsave = 1;
wdenkc6097192002-11-03 00:24:07 +0000766 }
767
768
769 while (blkcnt-- > 0) {
770
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000771 c = ide_wait(device, IDE_TIME_OUT);
wdenkc6097192002-11-03 00:24:07 +0000772
773 if (c & ATA_STAT_BUSY) {
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000774 printf("IDE read: device %d not ready\n", device);
wdenkc6097192002-11-03 00:24:07 +0000775 break;
776 }
wdenk42dfe7a2004-03-14 22:25:36 +0000777#ifdef CONFIG_LBA48
wdenkc40b2952004-03-13 23:29:43 +0000778 if (lba48) {
779 /* write high bits */
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000780 ide_outb(device, ATA_SECT_CNT, 0);
781 ide_outb(device, ATA_LBA_LOW, (blknr >> 24) & 0xFF);
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200782#ifdef CONFIG_SYS_64BIT_LBA
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000783 ide_outb(device, ATA_LBA_MID, (blknr >> 32) & 0xFF);
784 ide_outb(device, ATA_LBA_HIGH, (blknr >> 40) & 0xFF);
Guennadi Liakhovetski413bf582008-04-28 14:36:06 +0200785#else
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000786 ide_outb(device, ATA_LBA_MID, 0);
787 ide_outb(device, ATA_LBA_HIGH, 0);
Guennadi Liakhovetski413bf582008-04-28 14:36:06 +0200788#endif
wdenkc40b2952004-03-13 23:29:43 +0000789 }
790#endif
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000791 ide_outb(device, ATA_SECT_CNT, 1);
792 ide_outb(device, ATA_LBA_LOW, (blknr >> 0) & 0xFF);
793 ide_outb(device, ATA_LBA_MID, (blknr >> 8) & 0xFF);
794 ide_outb(device, ATA_LBA_HIGH, (blknr >> 16) & 0xFF);
wdenkc40b2952004-03-13 23:29:43 +0000795
wdenk42dfe7a2004-03-14 22:25:36 +0000796#ifdef CONFIG_LBA48
wdenkc40b2952004-03-13 23:29:43 +0000797 if (lba48) {
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000798 ide_outb(device, ATA_DEV_HD,
799 ATA_LBA | ATA_DEVICE(device));
800 ide_outb(device, ATA_COMMAND, ATA_CMD_READ_EXT);
wdenkc40b2952004-03-13 23:29:43 +0000801
802 } else
803#endif
804 {
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000805 ide_outb(device, ATA_DEV_HD, ATA_LBA |
806 ATA_DEVICE(device) | ((blknr >> 24) & 0xF));
807 ide_outb(device, ATA_COMMAND, ATA_CMD_READ);
wdenkc40b2952004-03-13 23:29:43 +0000808 }
wdenkc6097192002-11-03 00:24:07 +0000809
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000810 udelay(50);
wdenkc6097192002-11-03 00:24:07 +0000811
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000812 if (pwrsave) {
813 /* may take up to 4 sec */
814 c = ide_wait(device, IDE_SPIN_UP_TIME_OUT);
815 pwrsave = 0;
wdenkc6097192002-11-03 00:24:07 +0000816 } else {
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000817 /* can't take over 500 ms */
818 c = ide_wait(device, IDE_TIME_OUT);
wdenkc6097192002-11-03 00:24:07 +0000819 }
820
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000821 if ((c & (ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR)) !=
822 ATA_STAT_DRQ) {
Sascha Silbeff8fef52013-06-14 13:07:25 +0200823 printf("Error (no IRQ) dev %d blk " LBAF ": status "
824 "%#02x\n", device, blknr, c);
wdenkc6097192002-11-03 00:24:07 +0000825 break;
826 }
827
Pavel Herrmannf5b82c02012-10-09 07:04:39 +0000828 ide_input_data(device, buffer, ATA_SECTORWORDS);
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000829 (void) ide_inb(device, ATA_STATUS); /* clear IRQ */
wdenkc6097192002-11-03 00:24:07 +0000830
831 ++n;
832 ++blknr;
Greg Lopp0b945042007-04-13 08:02:24 +0200833 buffer += ATA_BLOCKSIZE;
wdenkc6097192002-11-03 00:24:07 +0000834 }
835IDE_READ_E:
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000836 ide_led(DEVICE_LED(device), 0); /* LED off */
Simon Glass53dbcdd2016-05-01 11:35:59 -0600837 return n;
wdenkc6097192002-11-03 00:24:07 +0000838}
839
840/* ------------------------------------------------------------------------- */
841
842
Simon Glass4101f682016-02-29 15:25:34 -0700843ulong ide_write(struct blk_desc *block_dev, lbaint_t blknr, lbaint_t blkcnt,
Stephen Warren7c4213f2015-12-07 11:38:48 -0700844 const void *buffer)
wdenkc6097192002-11-03 00:24:07 +0000845{
Simon Glassbcce53d2016-02-29 15:25:51 -0700846 int device = block_dev->devnum;
wdenkc6097192002-11-03 00:24:07 +0000847 ulong n = 0;
848 unsigned char c;
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000849
wdenk42dfe7a2004-03-14 22:25:36 +0000850#ifdef CONFIG_LBA48
wdenkc40b2952004-03-13 23:29:43 +0000851 unsigned char lba48 = 0;
852
Guennadi Liakhovetski413bf582008-04-28 14:36:06 +0200853 if (blknr & 0x0000fffff0000000ULL) {
wdenkc40b2952004-03-13 23:29:43 +0000854 /* more than 28 bits used, use 48bit mode */
855 lba48 = 1;
856 }
857#endif
wdenkc6097192002-11-03 00:24:07 +0000858
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000859 ide_led(DEVICE_LED(device), 1); /* LED on */
wdenkc6097192002-11-03 00:24:07 +0000860
861 /* Select device
862 */
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000863 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
wdenkc6097192002-11-03 00:24:07 +0000864
865 while (blkcnt-- > 0) {
866
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000867 c = ide_wait(device, IDE_TIME_OUT);
wdenkc6097192002-11-03 00:24:07 +0000868
869 if (c & ATA_STAT_BUSY) {
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000870 printf("IDE read: device %d not ready\n", device);
wdenkc6097192002-11-03 00:24:07 +0000871 goto WR_OUT;
872 }
wdenk42dfe7a2004-03-14 22:25:36 +0000873#ifdef CONFIG_LBA48
wdenkc40b2952004-03-13 23:29:43 +0000874 if (lba48) {
875 /* write high bits */
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000876 ide_outb(device, ATA_SECT_CNT, 0);
877 ide_outb(device, ATA_LBA_LOW, (blknr >> 24) & 0xFF);
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200878#ifdef CONFIG_SYS_64BIT_LBA
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000879 ide_outb(device, ATA_LBA_MID, (blknr >> 32) & 0xFF);
880 ide_outb(device, ATA_LBA_HIGH, (blknr >> 40) & 0xFF);
Guennadi Liakhovetski413bf582008-04-28 14:36:06 +0200881#else
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000882 ide_outb(device, ATA_LBA_MID, 0);
883 ide_outb(device, ATA_LBA_HIGH, 0);
Guennadi Liakhovetski413bf582008-04-28 14:36:06 +0200884#endif
wdenkc40b2952004-03-13 23:29:43 +0000885 }
886#endif
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000887 ide_outb(device, ATA_SECT_CNT, 1);
888 ide_outb(device, ATA_LBA_LOW, (blknr >> 0) & 0xFF);
889 ide_outb(device, ATA_LBA_MID, (blknr >> 8) & 0xFF);
890 ide_outb(device, ATA_LBA_HIGH, (blknr >> 16) & 0xFF);
wdenkc40b2952004-03-13 23:29:43 +0000891
wdenk42dfe7a2004-03-14 22:25:36 +0000892#ifdef CONFIG_LBA48
wdenkc40b2952004-03-13 23:29:43 +0000893 if (lba48) {
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000894 ide_outb(device, ATA_DEV_HD,
895 ATA_LBA | ATA_DEVICE(device));
896 ide_outb(device, ATA_COMMAND, ATA_CMD_WRITE_EXT);
wdenkc40b2952004-03-13 23:29:43 +0000897
898 } else
899#endif
900 {
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000901 ide_outb(device, ATA_DEV_HD, ATA_LBA |
902 ATA_DEVICE(device) | ((blknr >> 24) & 0xF));
903 ide_outb(device, ATA_COMMAND, ATA_CMD_WRITE);
wdenkc40b2952004-03-13 23:29:43 +0000904 }
wdenkc6097192002-11-03 00:24:07 +0000905
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000906 udelay(50);
wdenkc6097192002-11-03 00:24:07 +0000907
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000908 /* can't take over 500 ms */
909 c = ide_wait(device, IDE_TIME_OUT);
wdenkc6097192002-11-03 00:24:07 +0000910
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000911 if ((c & (ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR)) !=
912 ATA_STAT_DRQ) {
Sascha Silbeff8fef52013-06-14 13:07:25 +0200913 printf("Error (no IRQ) dev %d blk " LBAF ": status "
914 "%#02x\n", device, blknr, c);
wdenkc6097192002-11-03 00:24:07 +0000915 goto WR_OUT;
916 }
917
Pavel Herrmannf5b82c02012-10-09 07:04:39 +0000918 ide_output_data(device, buffer, ATA_SECTORWORDS);
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000919 c = ide_inb(device, ATA_STATUS); /* clear IRQ */
wdenkc6097192002-11-03 00:24:07 +0000920 ++n;
921 ++blknr;
Greg Lopp0b945042007-04-13 08:02:24 +0200922 buffer += ATA_BLOCKSIZE;
wdenkc6097192002-11-03 00:24:07 +0000923 }
924WR_OUT:
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000925 ide_led(DEVICE_LED(device), 0); /* LED off */
Simon Glass53dbcdd2016-05-01 11:35:59 -0600926 return n;
wdenkc6097192002-11-03 00:24:07 +0000927}
928
929/* ------------------------------------------------------------------------- */
930
931/*
932 * copy src to dest, skipping leading and trailing blanks and null
933 * terminate the string
wdenk7d7ce412004-03-17 01:13:07 +0000934 * "len" is the size of available memory including the terminating '\0'
wdenkc6097192002-11-03 00:24:07 +0000935 */
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000936static void ident_cpy(unsigned char *dst, unsigned char *src,
937 unsigned int len)
wdenkc6097192002-11-03 00:24:07 +0000938{
wdenk7d7ce412004-03-17 01:13:07 +0000939 unsigned char *end, *last;
wdenkc6097192002-11-03 00:24:07 +0000940
wdenk7d7ce412004-03-17 01:13:07 +0000941 last = dst;
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000942 end = src + len - 1;
wdenk7d7ce412004-03-17 01:13:07 +0000943
944 /* reserve space for '\0' */
945 if (len < 2)
946 goto OUT;
wdenkefa329c2004-03-23 20:18:25 +0000947
wdenk7d7ce412004-03-17 01:13:07 +0000948 /* skip leading white space */
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000949 while ((*src) && (src < end) && (*src == ' '))
wdenk7d7ce412004-03-17 01:13:07 +0000950 ++src;
951
952 /* copy string, omitting trailing white space */
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000953 while ((*src) && (src < end)) {
wdenk7d7ce412004-03-17 01:13:07 +0000954 *dst++ = *src;
955 if (*src++ != ' ')
956 last = dst;
wdenkc6097192002-11-03 00:24:07 +0000957 }
wdenk7d7ce412004-03-17 01:13:07 +0000958OUT:
959 *last = '\0';
wdenkc6097192002-11-03 00:24:07 +0000960}
961
962/* ------------------------------------------------------------------------- */
963
964/*
965 * Wait until Busy bit is off, or timeout (in ms)
966 * Return last status
967 */
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000968static uchar ide_wait(int dev, ulong t)
wdenkc6097192002-11-03 00:24:07 +0000969{
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000970 ulong delay = 10 * t; /* poll every 100 us */
wdenkc6097192002-11-03 00:24:07 +0000971 uchar c;
972
wdenk2262cfe2002-11-18 00:14:45 +0000973 while ((c = ide_inb(dev, ATA_STATUS)) & ATA_STAT_BUSY) {
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000974 udelay(100);
975 if (delay-- == 0)
wdenkc6097192002-11-03 00:24:07 +0000976 break;
wdenkc6097192002-11-03 00:24:07 +0000977 }
Simon Glass53dbcdd2016-05-01 11:35:59 -0600978 return c;
wdenkc6097192002-11-03 00:24:07 +0000979}
980
981/* ------------------------------------------------------------------------- */
982
983#ifdef CONFIG_IDE_RESET
984extern void ide_set_reset(int idereset);
985
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000986static void ide_reset(void)
wdenkc6097192002-11-03 00:24:07 +0000987{
wdenkc6097192002-11-03 00:24:07 +0000988 int i;
989
990 curr_device = -1;
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000991 for (i = 0; i < CONFIG_SYS_IDE_MAXBUS; ++i)
wdenkc6097192002-11-03 00:24:07 +0000992 ide_bus_ok[i] = 0;
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000993 for (i = 0; i < CONFIG_SYS_IDE_MAXDEVICE; ++i)
wdenkc6097192002-11-03 00:24:07 +0000994 ide_dev_desc[i].type = DEV_TYPE_UNKNOWN;
995
Wolfgang Denk34c202c2011-10-29 09:41:40 +0000996 ide_set_reset(1); /* assert reset */
wdenkc6097192002-11-03 00:24:07 +0000997
Martin Krausee175eac2008-04-03 13:37:56 +0200998 /* the reset signal shall be asserted for et least 25 us */
999 udelay(25);
1000
wdenkc6097192002-11-03 00:24:07 +00001001 WATCHDOG_RESET();
1002
wdenkc6097192002-11-03 00:24:07 +00001003 /* de-assert RESET signal */
1004 ide_set_reset(0);
1005
1006 /* wait 250 ms */
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001007 for (i = 0; i < 250; ++i)
1008 udelay(1000);
wdenkc6097192002-11-03 00:24:07 +00001009}
1010
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001011#endif /* CONFIG_IDE_RESET */
wdenkc6097192002-11-03 00:24:07 +00001012
1013/* ------------------------------------------------------------------------- */
1014
Heiko Schocher3887c3f2009-09-23 07:56:08 +02001015#if defined(CONFIG_OF_IDE_FIXUP)
1016int ide_device_present(int dev)
1017{
1018 if (dev >= CONFIG_SYS_IDE_MAXBUS)
1019 return 0;
1020 return (ide_dev_desc[dev].type == DEV_TYPE_UNKNOWN ? 0 : 1);
1021}
1022#endif
wdenkc6097192002-11-03 00:24:07 +00001023/* ------------------------------------------------------------------------- */
1024
1025#ifdef CONFIG_ATAPI
1026/****************************************************************************
1027 * ATAPI Support
1028 */
1029
Albert Aribaudf2a37fc2010-08-08 05:17:05 +05301030#if defined(CONFIG_IDE_SWAP_IO)
wdenkc6097192002-11-03 00:24:07 +00001031/* since ATAPI may use commands with not 4 bytes alligned length
1032 * we have our own transfer functions, 2 bytes alligned */
Jeroen Hofstee288afdc2014-07-12 15:07:19 +02001033__weak void ide_output_data_shorts(int dev, ushort *sect_buf, int shorts)
wdenkc6097192002-11-03 00:24:07 +00001034{
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001035 ushort *dbuf;
1036 volatile ushort *pbuf;
wdenkc6097192002-11-03 00:24:07 +00001037
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001038 pbuf = (ushort *) (ATA_CURR_BASE(dev) + ATA_DATA_REG);
1039 dbuf = (ushort *) sect_buf;
wdenkdb01a2e2004-04-15 23:14:49 +00001040
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001041 debug("in output data shorts base for read is %lx\n",
1042 (unsigned long) pbuf);
wdenkdb01a2e2004-04-15 23:14:49 +00001043
wdenkc6097192002-11-03 00:24:07 +00001044 while (shorts--) {
wdenk5cf91d62004-04-23 20:32:05 +00001045 EIEIO;
wdenk1a344f22005-02-03 23:00:49 +00001046 *pbuf = *dbuf++;
wdenkc6097192002-11-03 00:24:07 +00001047 }
wdenk1a344f22005-02-03 23:00:49 +00001048}
1049
Jeroen Hofstee288afdc2014-07-12 15:07:19 +02001050__weak void ide_input_data_shorts(int dev, ushort *sect_buf, int shorts)
wdenk1a344f22005-02-03 23:00:49 +00001051{
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001052 ushort *dbuf;
1053 volatile ushort *pbuf;
wdenk1a344f22005-02-03 23:00:49 +00001054
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001055 pbuf = (ushort *) (ATA_CURR_BASE(dev) + ATA_DATA_REG);
1056 dbuf = (ushort *) sect_buf;
wdenk1a344f22005-02-03 23:00:49 +00001057
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001058 debug("in input data shorts base for read is %lx\n",
1059 (unsigned long) pbuf);
wdenk1a344f22005-02-03 23:00:49 +00001060
1061 while (shorts--) {
1062 EIEIO;
1063 *dbuf++ = *pbuf;
1064 }
wdenkc6097192002-11-03 00:24:07 +00001065}
1066
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001067#else /* ! CONFIG_IDE_SWAP_IO */
Jeroen Hofstee288afdc2014-07-12 15:07:19 +02001068__weak void ide_output_data_shorts(int dev, ushort *sect_buf, int shorts)
wdenk2262cfe2002-11-18 00:14:45 +00001069{
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001070 outsw(ATA_CURR_BASE(dev) + ATA_DATA_REG, sect_buf, shorts);
wdenk2262cfe2002-11-18 00:14:45 +00001071}
1072
Jeroen Hofstee288afdc2014-07-12 15:07:19 +02001073__weak void ide_input_data_shorts(int dev, ushort *sect_buf, int shorts)
wdenk2262cfe2002-11-18 00:14:45 +00001074{
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001075 insw(ATA_CURR_BASE(dev) + ATA_DATA_REG, sect_buf, shorts);
wdenk2262cfe2002-11-18 00:14:45 +00001076}
1077
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001078#endif /* CONFIG_IDE_SWAP_IO */
wdenk2262cfe2002-11-18 00:14:45 +00001079
wdenkc6097192002-11-03 00:24:07 +00001080/*
1081 * Wait until (Status & mask) == res, or timeout (in ms)
1082 * Return last status
1083 * This is used since some ATAPI CD ROMs clears their Busy Bit first
1084 * and then they set their DRQ Bit
1085 */
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001086static uchar atapi_wait_mask(int dev, ulong t, uchar mask, uchar res)
wdenkc6097192002-11-03 00:24:07 +00001087{
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001088 ulong delay = 10 * t; /* poll every 100 us */
wdenkc6097192002-11-03 00:24:07 +00001089 uchar c;
1090
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001091 /* prevents to read the status before valid */
1092 c = ide_inb(dev, ATA_DEV_CTL);
1093
wdenk2262cfe2002-11-18 00:14:45 +00001094 while (((c = ide_inb(dev, ATA_STATUS)) & mask) != res) {
wdenkc6097192002-11-03 00:24:07 +00001095 /* break if error occurs (doesn't make sense to wait more) */
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001096 if ((c & ATA_STAT_ERR) == ATA_STAT_ERR)
wdenkc6097192002-11-03 00:24:07 +00001097 break;
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001098 udelay(100);
1099 if (delay-- == 0)
wdenkc6097192002-11-03 00:24:07 +00001100 break;
wdenkc6097192002-11-03 00:24:07 +00001101 }
Simon Glass53dbcdd2016-05-01 11:35:59 -06001102 return c;
wdenkc6097192002-11-03 00:24:07 +00001103}
1104
1105/*
1106 * issue an atapi command
1107 */
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001108unsigned char atapi_issue(int device, unsigned char *ccb, int ccblen,
1109 unsigned char *buffer, int buflen)
wdenkc6097192002-11-03 00:24:07 +00001110{
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001111 unsigned char c, err, mask, res;
wdenkc6097192002-11-03 00:24:07 +00001112 int n;
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001113
1114 ide_led(DEVICE_LED(device), 1); /* LED on */
wdenkc6097192002-11-03 00:24:07 +00001115
1116 /* Select device
1117 */
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001118 mask = ATA_STAT_BUSY | ATA_STAT_DRQ;
wdenkc6097192002-11-03 00:24:07 +00001119 res = 0;
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001120 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
1121 c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
wdenkc6097192002-11-03 00:24:07 +00001122 if ((c & mask) != res) {
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001123 printf("ATAPI_ISSUE: device %d not ready status %X\n", device,
1124 c);
1125 err = 0xFF;
wdenkc6097192002-11-03 00:24:07 +00001126 goto AI_OUT;
1127 }
1128 /* write taskfile */
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001129 ide_outb(device, ATA_ERROR_REG, 0); /* no DMA, no overlaped */
1130 ide_outb(device, ATA_SECT_CNT, 0);
1131 ide_outb(device, ATA_SECT_NUM, 0);
1132 ide_outb(device, ATA_CYL_LOW, (unsigned char) (buflen & 0xFF));
1133 ide_outb(device, ATA_CYL_HIGH,
1134 (unsigned char) ((buflen >> 8) & 0xFF));
1135 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
wdenkc6097192002-11-03 00:24:07 +00001136
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001137 ide_outb(device, ATA_COMMAND, ATAPI_CMD_PACKET);
1138 udelay(50);
wdenkc6097192002-11-03 00:24:07 +00001139
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001140 mask = ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR;
wdenkc6097192002-11-03 00:24:07 +00001141 res = ATA_STAT_DRQ;
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001142 c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
wdenkc6097192002-11-03 00:24:07 +00001143
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001144 if ((c & mask) != res) { /* DRQ must be 1, BSY 0 */
1145 printf("ATAPI_ISSUE: Error (no IRQ) before sending ccb dev %d status 0x%02x\n",
Simon Glass53dbcdd2016-05-01 11:35:59 -06001146 device, c);
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001147 err = 0xFF;
wdenkc6097192002-11-03 00:24:07 +00001148 goto AI_OUT;
1149 }
1150
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001151 /* write command block */
Pavel Herrmannf5b82c02012-10-09 07:04:39 +00001152 ide_output_data_shorts(device, (unsigned short *) ccb, ccblen / 2);
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001153
Wolfgang Denk53677ef2008-05-20 16:00:29 +02001154 /* ATAPI Command written wait for completition */
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001155 udelay(5000); /* device must set bsy */
wdenkc6097192002-11-03 00:24:07 +00001156
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001157 mask = ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR;
1158 /*
1159 * if no data wait for DRQ = 0 BSY = 0
1160 * if data wait for DRQ = 1 BSY = 0
1161 */
1162 res = 0;
1163 if (buflen)
wdenkc6097192002-11-03 00:24:07 +00001164 res = ATA_STAT_DRQ;
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001165 c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
1166 if ((c & mask) != res) {
wdenkc6097192002-11-03 00:24:07 +00001167 if (c & ATA_STAT_ERR) {
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001168 err = (ide_inb(device, ATA_ERROR_REG)) >> 4;
1169 debug("atapi_issue 1 returned sense key %X status %02X\n",
Simon Glass53dbcdd2016-05-01 11:35:59 -06001170 err, c);
wdenkc6097192002-11-03 00:24:07 +00001171 } else {
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001172 printf("ATAPI_ISSUE: (no DRQ) after sending ccb (%x) status 0x%02x\n",
Simon Glass53dbcdd2016-05-01 11:35:59 -06001173 ccb[0], c);
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001174 err = 0xFF;
wdenkc6097192002-11-03 00:24:07 +00001175 }
1176 goto AI_OUT;
1177 }
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001178 n = ide_inb(device, ATA_CYL_HIGH);
1179 n <<= 8;
1180 n += ide_inb(device, ATA_CYL_LOW);
1181 if (n > buflen) {
1182 printf("ERROR, transfer bytes %d requested only %d\n", n,
1183 buflen);
1184 err = 0xff;
wdenkc6097192002-11-03 00:24:07 +00001185 goto AI_OUT;
1186 }
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001187 if ((n == 0) && (buflen < 0)) {
1188 printf("ERROR, transfer bytes %d requested %d\n", n, buflen);
1189 err = 0xff;
wdenkc6097192002-11-03 00:24:07 +00001190 goto AI_OUT;
1191 }
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001192 if (n != buflen) {
1193 debug("WARNING, transfer bytes %d not equal with requested %d\n",
Simon Glass53dbcdd2016-05-01 11:35:59 -06001194 n, buflen);
wdenkc6097192002-11-03 00:24:07 +00001195 }
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001196 if (n != 0) { /* data transfer */
1197 debug("ATAPI_ISSUE: %d Bytes to transfer\n", n);
1198 /* we transfer shorts */
1199 n >>= 1;
wdenkc6097192002-11-03 00:24:07 +00001200 /* ok now decide if it is an in or output */
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001201 if ((ide_inb(device, ATA_SECT_CNT) & 0x02) == 0) {
1202 debug("Write to device\n");
Pavel Herrmannf5b82c02012-10-09 07:04:39 +00001203 ide_output_data_shorts(device,
1204 (unsigned short *) buffer, n);
wdenkc6097192002-11-03 00:24:07 +00001205 } else {
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001206 debug("Read from device @ %p shorts %d\n", buffer, n);
Pavel Herrmannf5b82c02012-10-09 07:04:39 +00001207 ide_input_data_shorts(device,
1208 (unsigned short *) buffer, n);
wdenkc6097192002-11-03 00:24:07 +00001209 }
1210 }
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001211 udelay(5000); /* seems that some CD ROMs need this... */
1212 mask = ATA_STAT_BUSY | ATA_STAT_ERR;
1213 res = 0;
1214 c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
wdenkc6097192002-11-03 00:24:07 +00001215 if ((c & ATA_STAT_ERR) == ATA_STAT_ERR) {
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001216 err = (ide_inb(device, ATA_ERROR_REG) >> 4);
1217 debug("atapi_issue 2 returned sense key %X status %X\n", err,
1218 c);
wdenkc6097192002-11-03 00:24:07 +00001219 } else {
1220 err = 0;
1221 }
1222AI_OUT:
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001223 ide_led(DEVICE_LED(device), 0); /* LED off */
Simon Glass53dbcdd2016-05-01 11:35:59 -06001224 return err;
wdenkc6097192002-11-03 00:24:07 +00001225}
1226
1227/*
1228 * sending the command to atapi_issue. If an status other than good
1229 * returns, an request_sense will be issued
1230 */
1231
Wolfgang Denk53677ef2008-05-20 16:00:29 +02001232#define ATAPI_DRIVE_NOT_READY 100
wdenkc6097192002-11-03 00:24:07 +00001233#define ATAPI_UNIT_ATTN 10
1234
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001235unsigned char atapi_issue_autoreq(int device,
1236 unsigned char *ccb,
1237 int ccblen,
1238 unsigned char *buffer, int buflen)
wdenkc6097192002-11-03 00:24:07 +00001239{
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001240 unsigned char sense_data[18], sense_ccb[12];
1241 unsigned char res, key, asc, ascq;
1242 int notready, unitattn;
wdenkc6097192002-11-03 00:24:07 +00001243
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001244 unitattn = ATAPI_UNIT_ATTN;
1245 notready = ATAPI_DRIVE_NOT_READY;
wdenkc6097192002-11-03 00:24:07 +00001246
1247retry:
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001248 res = atapi_issue(device, ccb, ccblen, buffer, buflen);
1249 if (res == 0)
1250 return 0; /* Ok */
wdenkc6097192002-11-03 00:24:07 +00001251
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001252 if (res == 0xFF)
1253 return 0xFF; /* error */
wdenkc6097192002-11-03 00:24:07 +00001254
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001255 debug("(auto_req)atapi_issue returned sense key %X\n", res);
wdenkc6097192002-11-03 00:24:07 +00001256
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001257 memset(sense_ccb, 0, sizeof(sense_ccb));
1258 memset(sense_data, 0, sizeof(sense_data));
1259 sense_ccb[0] = ATAPI_CMD_REQ_SENSE;
1260 sense_ccb[4] = 18; /* allocation Length */
wdenkc6097192002-11-03 00:24:07 +00001261
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001262 res = atapi_issue(device, sense_ccb, 12, sense_data, 18);
1263 key = (sense_data[2] & 0xF);
1264 asc = (sense_data[12]);
1265 ascq = (sense_data[13]);
wdenkc6097192002-11-03 00:24:07 +00001266
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001267 debug("ATAPI_CMD_REQ_SENSE returned %x\n", res);
1268 debug(" Sense page: %02X key %02X ASC %02X ASCQ %02X\n",
1269 sense_data[0], key, asc, ascq);
wdenkc6097192002-11-03 00:24:07 +00001270
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001271 if ((key == 0))
1272 return 0; /* ok device ready */
wdenkc6097192002-11-03 00:24:07 +00001273
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001274 if ((key == 6) || (asc == 0x29) || (asc == 0x28)) { /* Unit Attention */
1275 if (unitattn-- > 0) {
1276 udelay(200 * 1000);
wdenkc6097192002-11-03 00:24:07 +00001277 goto retry;
1278 }
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001279 printf("Unit Attention, tried %d\n", ATAPI_UNIT_ATTN);
wdenkc6097192002-11-03 00:24:07 +00001280 goto error;
1281 }
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001282 if ((asc == 0x4) && (ascq == 0x1)) {
1283 /* not ready, but will be ready soon */
1284 if (notready-- > 0) {
1285 udelay(200 * 1000);
wdenkc6097192002-11-03 00:24:07 +00001286 goto retry;
1287 }
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001288 printf("Drive not ready, tried %d times\n",
1289 ATAPI_DRIVE_NOT_READY);
wdenkc6097192002-11-03 00:24:07 +00001290 goto error;
1291 }
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001292 if (asc == 0x3a) {
1293 debug("Media not present\n");
wdenkc6097192002-11-03 00:24:07 +00001294 goto error;
1295 }
wdenkc7de8292002-11-19 11:04:11 +00001296
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001297 printf("ERROR: Unknown Sense key %02X ASC %02X ASCQ %02X\n", key, asc,
1298 ascq);
wdenkc6097192002-11-03 00:24:07 +00001299error:
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001300 debug("ERROR Sense key %02X ASC %02X ASCQ %02X\n", key, asc, ascq);
Simon Glass53dbcdd2016-05-01 11:35:59 -06001301 return 0xFF;
wdenkc6097192002-11-03 00:24:07 +00001302}
1303
1304
Simon Glass4101f682016-02-29 15:25:34 -07001305static void atapi_inquiry(struct blk_desc *dev_desc)
wdenkc6097192002-11-03 00:24:07 +00001306{
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001307 unsigned char ccb[12]; /* Command descriptor block */
1308 unsigned char iobuf[64]; /* temp buf */
wdenkc6097192002-11-03 00:24:07 +00001309 unsigned char c;
1310 int device;
1311
Simon Glassbcce53d2016-02-29 15:25:51 -07001312 device = dev_desc->devnum;
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001313 dev_desc->type = DEV_TYPE_UNKNOWN; /* not yet valid */
1314 dev_desc->block_read = atapi_read;
wdenkc6097192002-11-03 00:24:07 +00001315
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001316 memset(ccb, 0, sizeof(ccb));
1317 memset(iobuf, 0, sizeof(iobuf));
wdenkc6097192002-11-03 00:24:07 +00001318
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001319 ccb[0] = ATAPI_CMD_INQUIRY;
1320 ccb[4] = 40; /* allocation Legnth */
1321 c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *) iobuf, 40);
wdenkc6097192002-11-03 00:24:07 +00001322
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001323 debug("ATAPI_CMD_INQUIRY returned %x\n", c);
1324 if (c != 0)
wdenkc6097192002-11-03 00:24:07 +00001325 return;
1326
1327 /* copy device ident strings */
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001328 ident_cpy((unsigned char *) dev_desc->vendor, &iobuf[8], 8);
1329 ident_cpy((unsigned char *) dev_desc->product, &iobuf[16], 16);
1330 ident_cpy((unsigned char *) dev_desc->revision, &iobuf[32], 5);
wdenkc6097192002-11-03 00:24:07 +00001331
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001332 dev_desc->lun = 0;
1333 dev_desc->lba = 0;
1334 dev_desc->blksz = 0;
Egbert Eich0472fbf2013-04-09 21:11:56 +00001335 dev_desc->log2blksz = LOG2_INVALID(typeof(dev_desc->log2blksz));
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001336 dev_desc->type = iobuf[0] & 0x1f;
wdenkc6097192002-11-03 00:24:07 +00001337
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001338 if ((iobuf[1] & 0x80) == 0x80)
wdenkc6097192002-11-03 00:24:07 +00001339 dev_desc->removable = 1;
1340 else
1341 dev_desc->removable = 0;
1342
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001343 memset(ccb, 0, sizeof(ccb));
1344 memset(iobuf, 0, sizeof(iobuf));
1345 ccb[0] = ATAPI_CMD_START_STOP;
1346 ccb[4] = 0x03; /* start */
wdenkc6097192002-11-03 00:24:07 +00001347
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001348 c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *) iobuf, 0);
wdenkc6097192002-11-03 00:24:07 +00001349
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001350 debug("ATAPI_CMD_START_STOP returned %x\n", c);
1351 if (c != 0)
wdenkc6097192002-11-03 00:24:07 +00001352 return;
1353
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001354 memset(ccb, 0, sizeof(ccb));
1355 memset(iobuf, 0, sizeof(iobuf));
1356 c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *) iobuf, 0);
wdenkc6097192002-11-03 00:24:07 +00001357
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001358 debug("ATAPI_CMD_UNIT_TEST_READY returned %x\n", c);
1359 if (c != 0)
wdenkc6097192002-11-03 00:24:07 +00001360 return;
1361
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001362 memset(ccb, 0, sizeof(ccb));
1363 memset(iobuf, 0, sizeof(iobuf));
1364 ccb[0] = ATAPI_CMD_READ_CAP;
1365 c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *) iobuf, 8);
1366 debug("ATAPI_CMD_READ_CAP returned %x\n", c);
1367 if (c != 0)
wdenkc6097192002-11-03 00:24:07 +00001368 return;
1369
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001370 debug("Read Cap: LBA %02X%02X%02X%02X blksize %02X%02X%02X%02X\n",
1371 iobuf[0], iobuf[1], iobuf[2], iobuf[3],
1372 iobuf[4], iobuf[5], iobuf[6], iobuf[7]);
wdenkc6097192002-11-03 00:24:07 +00001373
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001374 dev_desc->lba = ((unsigned long) iobuf[0] << 24) +
1375 ((unsigned long) iobuf[1] << 16) +
1376 ((unsigned long) iobuf[2] << 8) + ((unsigned long) iobuf[3]);
1377 dev_desc->blksz = ((unsigned long) iobuf[4] << 24) +
1378 ((unsigned long) iobuf[5] << 16) +
1379 ((unsigned long) iobuf[6] << 8) + ((unsigned long) iobuf[7]);
Egbert Eich0472fbf2013-04-09 21:11:56 +00001380 dev_desc->log2blksz = LOG2(dev_desc->blksz);
wdenk42dfe7a2004-03-14 22:25:36 +00001381#ifdef CONFIG_LBA48
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001382 /* ATAPI devices cannot use 48bit addressing (ATA/ATAPI v7) */
1383 dev_desc->lba48 = 0;
wdenk42dfe7a2004-03-14 22:25:36 +00001384#endif
wdenkc6097192002-11-03 00:24:07 +00001385 return;
1386}
1387
1388
1389/*
1390 * atapi_read:
1391 * we transfer only one block per command, since the multiple DRQ per
1392 * command is not yet implemented
1393 */
1394#define ATAPI_READ_MAX_BYTES 2048 /* we read max 2kbytes */
1395#define ATAPI_READ_BLOCK_SIZE 2048 /* assuming CD part */
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001396#define ATAPI_READ_MAX_BLOCK (ATAPI_READ_MAX_BYTES/ATAPI_READ_BLOCK_SIZE)
wdenkc6097192002-11-03 00:24:07 +00001397
Simon Glass4101f682016-02-29 15:25:34 -07001398ulong atapi_read(struct blk_desc *block_dev, lbaint_t blknr, lbaint_t blkcnt,
Stephen Warren7c4213f2015-12-07 11:38:48 -07001399 void *buffer)
wdenkc6097192002-11-03 00:24:07 +00001400{
Simon Glassbcce53d2016-02-29 15:25:51 -07001401 int device = block_dev->devnum;
wdenkc6097192002-11-03 00:24:07 +00001402 ulong n = 0;
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001403 unsigned char ccb[12]; /* Command descriptor block */
wdenkc6097192002-11-03 00:24:07 +00001404 ulong cnt;
1405
Simon Glass53dbcdd2016-05-01 11:35:59 -06001406 debug("atapi_read dev %d start " LBAF " blocks " LBAF
1407 " buffer at %lX\n", device, blknr, blkcnt, (ulong) buffer);
wdenkc6097192002-11-03 00:24:07 +00001408
1409 do {
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001410 if (blkcnt > ATAPI_READ_MAX_BLOCK)
1411 cnt = ATAPI_READ_MAX_BLOCK;
1412 else
1413 cnt = blkcnt;
wdenkc6097192002-11-03 00:24:07 +00001414
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001415 ccb[0] = ATAPI_CMD_READ_12;
1416 ccb[1] = 0; /* reserved */
1417 ccb[2] = (unsigned char) (blknr >> 24) & 0xFF; /* MSB Block */
1418 ccb[3] = (unsigned char) (blknr >> 16) & 0xFF; /* */
1419 ccb[4] = (unsigned char) (blknr >> 8) & 0xFF;
1420 ccb[5] = (unsigned char) blknr & 0xFF; /* LSB Block */
1421 ccb[6] = (unsigned char) (cnt >> 24) & 0xFF; /* MSB Block cnt */
1422 ccb[7] = (unsigned char) (cnt >> 16) & 0xFF;
1423 ccb[8] = (unsigned char) (cnt >> 8) & 0xFF;
1424 ccb[9] = (unsigned char) cnt & 0xFF; /* LSB Block */
1425 ccb[10] = 0; /* reserved */
1426 ccb[11] = 0; /* reserved */
1427
1428 if (atapi_issue_autoreq(device, ccb, 12,
1429 (unsigned char *) buffer,
1430 cnt * ATAPI_READ_BLOCK_SIZE)
1431 == 0xFF) {
Simon Glass53dbcdd2016-05-01 11:35:59 -06001432 return n;
wdenkc6097192002-11-03 00:24:07 +00001433 }
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001434 n += cnt;
1435 blkcnt -= cnt;
1436 blknr += cnt;
1437 buffer += (cnt * ATAPI_READ_BLOCK_SIZE);
wdenkc6097192002-11-03 00:24:07 +00001438 } while (blkcnt > 0);
Simon Glass53dbcdd2016-05-01 11:35:59 -06001439 return n;
wdenkc6097192002-11-03 00:24:07 +00001440}
1441
1442/* ------------------------------------------------------------------------- */
1443
1444#endif /* CONFIG_ATAPI */
1445
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001446U_BOOT_CMD(ide, 5, 1, do_ide,
1447 "IDE sub-system",
1448 "reset - reset IDE controller\n"
1449 "ide info - show available IDE devices\n"
1450 "ide device [dev] - show or set current device\n"
1451 "ide part [dev] - print partition table of one or all IDE devices\n"
1452 "ide read addr blk# cnt\n"
1453 "ide write addr blk# cnt - read/write `cnt'"
1454 " blocks starting at block `blk#'\n"
1455 " to/from memory address `addr'");
wdenk8bde7f72003-06-27 21:31:46 +00001456
Wolfgang Denk34c202c2011-10-29 09:41:40 +00001457U_BOOT_CMD(diskboot, 3, 1, do_diskboot,
1458 "boot from IDE device", "loadAddr dev:part");