]> git.ipfire.org Git - people/ms/u-boot.git/blame - common/cmd_ide.c
Patch by Kenneth Johansson, 30 Jun 2003:
[people/ms/u-boot.git] / common / cmd_ide.c
CommitLineData
c609719b
WD
1/*
2 * (C) Copyright 2000-2002
3 * 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 */
28#include <common.h>
29#include <config.h>
30#include <watchdog.h>
31#include <command.h>
32#include <image.h>
33#include <asm/byteorder.h>
34#if defined(CONFIG_IDE_8xx_DIRECT) || defined(CONFIG_IDE_PCMCIA)
35# include <pcmcia.h>
36#endif
37#ifdef CONFIG_8xx
38# include <mpc8xx.h>
39#endif
40#include <ide.h>
41#include <ata.h>
c609719b
WD
42#ifdef CONFIG_STATUS_LED
43# include <status_led.h>
44#endif
2262cfee
WD
45#ifdef __I386__
46#include <asm/io.h>
47#endif
c609719b
WD
48
49#ifdef CONFIG_SHOW_BOOT_PROGRESS
50# include <status_led.h>
51# define SHOW_BOOT_PROGRESS(arg) show_boot_progress(arg)
52#else
53# define SHOW_BOOT_PROGRESS(arg)
54#endif
55
56
57#undef IDE_DEBUG
58
c7de829c 59
c609719b
WD
60#ifdef IDE_DEBUG
61#define PRINTF(fmt,args...) printf (fmt ,##args)
62#else
63#define PRINTF(fmt,args...)
64#endif
65
66#if (CONFIG_COMMANDS & CFG_CMD_IDE)
67
68/* Timings for IDE Interface
69 *
70 * SETUP / LENGTH / HOLD - cycles valid for 50 MHz clk
71 * 70 165 30 PIO-Mode 0, [ns]
72 * 4 9 2 [Cycles]
73 * 50 125 20 PIO-Mode 1, [ns]
74 * 3 7 2 [Cycles]
75 * 30 100 15 PIO-Mode 2, [ns]
76 * 2 6 1 [Cycles]
77 * 30 80 10 PIO-Mode 3, [ns]
78 * 2 5 1 [Cycles]
79 * 25 70 10 PIO-Mode 4, [ns]
80 * 2 4 1 [Cycles]
81 */
82
83const static pio_config_t pio_config_ns [IDE_MAX_PIO_MODE+1] =
84{
85 /* Setup Length Hold */
86 { 70, 165, 30 }, /* PIO-Mode 0, [ns] */
87 { 50, 125, 20 }, /* PIO-Mode 1, [ns] */
88 { 30, 101, 15 }, /* PIO-Mode 2, [ns] */
89 { 30, 80, 10 }, /* PIO-Mode 3, [ns] */
90 { 25, 70, 10 }, /* PIO-Mode 4, [ns] */
91};
92
93static pio_config_t pio_config_clk [IDE_MAX_PIO_MODE+1];
94
95#ifndef CFG_PIO_MODE
96#define CFG_PIO_MODE 0 /* use a relaxed default */
97#endif
98static int pio_mode = CFG_PIO_MODE;
99
100/* Make clock cycles and always round up */
101
102#define PCMCIA_MK_CLKS( t, T ) (( (t) * (T) + 999U ) / 1000U )
103
104/* ------------------------------------------------------------------------- */
105
106/* Current I/O Device */
107static int curr_device = -1;
108
109/* Current offset for IDE0 / IDE1 bus access */
110ulong ide_bus_offset[CFG_IDE_MAXBUS] = {
111#if defined(CFG_ATA_IDE0_OFFSET)
112 CFG_ATA_IDE0_OFFSET,
113#endif
114#if defined(CFG_ATA_IDE1_OFFSET) && (CFG_IDE_MAXBUS > 1)
115 CFG_ATA_IDE1_OFFSET,
116#endif
117};
118
2262cfee 119#ifdef __PPC__
c609719b 120#define ATA_CURR_BASE(dev) (CFG_ATA_BASE_ADDR+ide_bus_offset[IDE_BUS(dev)])
2262cfee 121#endif
c609719b 122
c7de829c 123#ifndef CONFIG_AMIGAONEG3SE
c609719b 124static int ide_bus_ok[CFG_IDE_MAXBUS];
c7de829c
WD
125#else
126static int ide_bus_ok[CFG_IDE_MAXBUS] = {0,};
127#endif
c609719b
WD
128
129static block_dev_desc_t ide_dev_desc[CFG_IDE_MAXDEVICE];
130/* ------------------------------------------------------------------------- */
131
132#ifdef CONFIG_IDE_LED
1f53a416 133#ifndef CONFIG_KUP4K
c609719b
WD
134static void ide_led (uchar led, uchar status);
135#else
1f53a416
WD
136extern void ide_led (uchar led, uchar status);
137#endif
138#else
c7de829c 139#ifndef CONFIG_AMIGAONEG3SE
c609719b 140#define ide_led(a,b) /* dummy */
c7de829c
WD
141#else
142extern void ide_led(uchar led, uchar status);
143#define LED_IDE1 1
144#define LED_IDE2 2
145#define CONFIG_IDE_LED 1
146#define DEVICE_LED(x) 1
147#endif
c609719b
WD
148#endif
149
150#ifdef CONFIG_IDE_RESET
151static void ide_reset (void);
152#else
153#define ide_reset() /* dummy */
154#endif
155
156static void ide_ident (block_dev_desc_t *dev_desc);
157static uchar ide_wait (int dev, ulong t);
158
159#define IDE_TIME_OUT 2000 /* 2 sec timeout */
160
161#define ATAPI_TIME_OUT 7000 /* 7 sec timeout (5 sec seems to work...) */
162
163#define IDE_SPIN_UP_TIME_OUT 5000 /* 5 sec spin-up timeout */
164
2262cfee
WD
165static void __inline__ ide_outb(int dev, int port, unsigned char val);
166static unsigned char __inline__ ide_inb(int dev, int port);
167#ifdef __PPC__
c609719b 168static void input_swap_data(int dev, ulong *sect_buf, int words);
2262cfee 169#endif
c609719b
WD
170static void input_data(int dev, ulong *sect_buf, int words);
171static void output_data(int dev, ulong *sect_buf, int words);
172static void ident_cpy (unsigned char *dest, unsigned char *src, unsigned int len);
173
174
175#ifdef CONFIG_ATAPI
176static void atapi_inquiry(block_dev_desc_t *dev_desc);
177ulong atapi_read (int device, ulong blknr, ulong blkcnt, ulong *buffer);
178#endif
179
180
181#ifdef CONFIG_IDE_8xx_DIRECT
182static void set_pcmcia_timing (int pmode);
183#else
184#define set_pcmcia_timing(a) /* dummy */
185#endif
186
187/* ------------------------------------------------------------------------- */
188
189int do_ide (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
190{
191 int rcode = 0;
192
193 switch (argc) {
194 case 0:
195 case 1:
196 printf ("Usage:\n%s\n", cmdtp->usage);
197 return 1;
198 case 2:
199 if (strncmp(argv[1],"res",3) == 0) {
200 puts ("\nReset IDE"
201#ifdef CONFIG_IDE_8xx_DIRECT
202 " on PCMCIA " PCMCIA_SLOT_MSG
203#endif
204 ": ");
205
206 ide_init ();
207 return 0;
208 } else if (strncmp(argv[1],"inf",3) == 0) {
209 int i;
210
211 putc ('\n');
212
213 for (i=0; i<CFG_IDE_MAXDEVICE; ++i) {
214 if (ide_dev_desc[i].type==DEV_TYPE_UNKNOWN)
215 continue; /* list only known devices */
216 printf ("IDE device %d: ", i);
217 dev_print(&ide_dev_desc[i]);
218 }
219 return 0;
220
221 } else if (strncmp(argv[1],"dev",3) == 0) {
222 if ((curr_device < 0) || (curr_device >= CFG_IDE_MAXDEVICE)) {
223 puts ("\nno IDE devices available\n");
224 return 1;
225 }
226 printf ("\nIDE device %d: ", curr_device);
227 dev_print(&ide_dev_desc[curr_device]);
228 return 0;
229 } else if (strncmp(argv[1],"part",4) == 0) {
230 int dev, ok;
231
232 for (ok=0, dev=0; dev<CFG_IDE_MAXDEVICE; ++dev) {
233 if (ide_dev_desc[dev].part_type!=PART_TYPE_UNKNOWN) {
234 ++ok;
235 if (dev)
236 putc ('\n');
237 print_part(&ide_dev_desc[dev]);
238 }
239 }
240 if (!ok) {
241 puts ("\nno IDE devices available\n");
242 rcode ++;
243 }
244 return rcode;
245 }
246 printf ("Usage:\n%s\n", cmdtp->usage);
247 return 1;
248 case 3:
249 if (strncmp(argv[1],"dev",3) == 0) {
250 int dev = (int)simple_strtoul(argv[2], NULL, 10);
251
252 printf ("\nIDE device %d: ", dev);
253 if (dev >= CFG_IDE_MAXDEVICE) {
254 puts ("unknown device\n");
255 return 1;
256 }
257 dev_print(&ide_dev_desc[dev]);
258 /*ide_print (dev);*/
259
260 if (ide_dev_desc[dev].type == DEV_TYPE_UNKNOWN) {
261 return 1;
262 }
263
264 curr_device = dev;
265
266 puts ("... is now current device\n");
267
268 return 0;
269 } else if (strncmp(argv[1],"part",4) == 0) {
270 int dev = (int)simple_strtoul(argv[2], NULL, 10);
271
272 if (ide_dev_desc[dev].part_type!=PART_TYPE_UNKNOWN) {
273 print_part(&ide_dev_desc[dev]);
274 } else {
275 printf ("\nIDE device %d not available\n", dev);
276 rcode = 1;
277 }
278 return rcode;
279#if 0
280 } else if (strncmp(argv[1],"pio",4) == 0) {
281 int mode = (int)simple_strtoul(argv[2], NULL, 10);
282
283 if ((mode >= 0) && (mode <= IDE_MAX_PIO_MODE)) {
284 puts ("\nSetting ");
285 pio_mode = mode;
286 ide_init ();
287 } else {
288 printf ("\nInvalid PIO mode %d (0 ... %d only)\n",
289 mode, IDE_MAX_PIO_MODE);
290 }
291 return;
292#endif
293 }
294
295 printf ("Usage:\n%s\n", cmdtp->usage);
296 return 1;
297 default:
298 /* at least 4 args */
299
300 if (strcmp(argv[1],"read") == 0) {
301 ulong addr = simple_strtoul(argv[2], NULL, 16);
302 ulong blk = simple_strtoul(argv[3], NULL, 16);
303 ulong cnt = simple_strtoul(argv[4], NULL, 16);
304 ulong n;
305
306 printf ("\nIDE read: device %d block # %ld, count %ld ... ",
307 curr_device, blk, cnt);
308
309 n = ide_dev_desc[curr_device].block_read (curr_device,
310 blk, cnt,
311 (ulong *)addr);
312 /* flush cache after read */
313 flush_cache (addr, cnt*ide_dev_desc[curr_device].blksz);
314
315 printf ("%ld blocks read: %s\n",
316 n, (n==cnt) ? "OK" : "ERROR");
317 if (n==cnt) {
318 return 0;
319 } else {
320 return 1;
321 }
322 } else if (strcmp(argv[1],"write") == 0) {
323 ulong addr = simple_strtoul(argv[2], NULL, 16);
324 ulong blk = simple_strtoul(argv[3], NULL, 16);
325 ulong cnt = simple_strtoul(argv[4], NULL, 16);
326 ulong n;
327
328 printf ("\nIDE write: device %d block # %ld, count %ld ... ",
329 curr_device, blk, cnt);
330
331 n = ide_write (curr_device, blk, cnt, (ulong *)addr);
332
333 printf ("%ld blocks written: %s\n",
334 n, (n==cnt) ? "OK" : "ERROR");
335 if (n==cnt) {
336 return 0;
337 } else {
338 return 1;
339 }
340 } else {
341 printf ("Usage:\n%s\n", cmdtp->usage);
342 rcode = 1;
343 }
344
345 return rcode;
346 }
347}
348
349int do_diskboot (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
350{
351 char *boot_device = NULL;
352 char *ep;
353 int dev, part = 0;
354 ulong cnt;
355 ulong addr;
356 disk_partition_t info;
357 image_header_t *hdr;
358 int rcode = 0;
359
360 switch (argc) {
361 case 1:
362 addr = CFG_LOAD_ADDR;
363 boot_device = getenv ("bootdevice");
364 break;
365 case 2:
366 addr = simple_strtoul(argv[1], NULL, 16);
367 boot_device = getenv ("bootdevice");
368 break;
369 case 3:
370 addr = simple_strtoul(argv[1], NULL, 16);
371 boot_device = argv[2];
372 break;
373 default:
374 printf ("Usage:\n%s\n", cmdtp->usage);
375 SHOW_BOOT_PROGRESS (-1);
376 return 1;
377 }
378
379 if (!boot_device) {
380 puts ("\n** No boot device **\n");
381 SHOW_BOOT_PROGRESS (-1);
382 return 1;
383 }
384
385 dev = simple_strtoul(boot_device, &ep, 16);
386
387 if (ide_dev_desc[dev].type==DEV_TYPE_UNKNOWN) {
388 printf ("\n** Device %d not available\n", dev);
389 SHOW_BOOT_PROGRESS (-1);
390 return 1;
391 }
392
393 if (*ep) {
394 if (*ep != ':') {
395 puts ("\n** Invalid boot device, use `dev[:part]' **\n");
396 SHOW_BOOT_PROGRESS (-1);
397 return 1;
398 }
399 part = simple_strtoul(++ep, NULL, 16);
400 }
401 if (get_partition_info (&ide_dev_desc[dev], part, &info)) {
402 SHOW_BOOT_PROGRESS (-1);
403 return 1;
404 }
4532cb69
WD
405 if ((strncmp(info.type, BOOT_PART_TYPE, sizeof(info.type)) != 0) &&
406 (strncmp(info.type, BOOT_PART_COMP, sizeof(info.type)) != 0)) {
c609719b
WD
407 printf ("\n** Invalid partition type \"%.32s\""
408 " (expect \"" BOOT_PART_TYPE "\")\n",
409 info.type);
410 SHOW_BOOT_PROGRESS (-1);
411 return 1;
412 }
413
414 printf ("\nLoading from IDE device %d, partition %d: "
415 "Name: %.32s Type: %.32s\n",
416 dev, part, info.name, info.type);
417
418 PRINTF ("First Block: %ld, # of blocks: %ld, Block Size: %ld\n",
419 info.start, info.size, info.blksz);
420
421 if (ide_dev_desc[dev].block_read (dev, info.start, 1, (ulong *)addr) != 1) {
422 printf ("** Read error on %d:%d\n", dev, part);
423 SHOW_BOOT_PROGRESS (-1);
424 return 1;
425 }
426
427 hdr = (image_header_t *)addr;
428
429 if (ntohl(hdr->ih_magic) == IH_MAGIC) {
430
431 print_image_hdr (hdr);
432
433 cnt = (ntohl(hdr->ih_size) + sizeof(image_header_t));
434 cnt += info.blksz - 1;
435 cnt /= info.blksz;
436 cnt -= 1;
437 } else {
438 printf("\n** Bad Magic Number **\n");
439 SHOW_BOOT_PROGRESS (-1);
440 return 1;
441 }
442
443 if (ide_dev_desc[dev].block_read (dev, info.start+1, cnt,
444 (ulong *)(addr+info.blksz)) != cnt) {
445 printf ("** Read error on %d:%d\n", dev, part);
446 SHOW_BOOT_PROGRESS (-1);
447 return 1;
448 }
449
450
451 /* Loading ok, update default load address */
452
453 load_addr = addr;
454
455 /* Check if we should attempt an auto-start */
456 if (((ep = getenv("autostart")) != NULL) && (strcmp(ep,"yes") == 0)) {
457 char *local_args[2];
458 extern int do_bootm (cmd_tbl_t *, int, int, char *[]);
459
460 local_args[0] = argv[0];
461 local_args[1] = NULL;
462
463 printf ("Automatic boot of image at addr 0x%08lX ...\n", addr);
464
465 do_bootm (cmdtp, 0, 1, local_args);
466 rcode = 1;
467 }
468 return rcode;
469}
470
471/* ------------------------------------------------------------------------- */
472
473void ide_init (void)
474{
475 DECLARE_GLOBAL_DATA_PTR;
476
477#ifdef CONFIG_IDE_8xx_DIRECT
478 volatile immap_t *immr = (immap_t *)CFG_IMMR;
479 volatile pcmconf8xx_t *pcmp = &(immr->im_pcmcia);
480#endif
481 unsigned char c;
482 int i, bus;
c7de829c
WD
483#ifdef CONFIG_AMIGAONEG3SE
484 unsigned int max_bus_scan;
485 unsigned int ata_reset_time;
486 char *s;
487#endif
c609719b
WD
488
489#ifdef CONFIG_IDE_8xx_PCCARD
490 extern int pcmcia_on (void);
6069ff26 491 extern int ide_devices_found; /* Initialized in check_ide_device() */
c609719b
WD
492
493 WATCHDOG_RESET();
494
6069ff26 495 ide_devices_found = 0;
c609719b 496 /* initialize the PCMCIA IDE adapter card */
6069ff26
WD
497 pcmcia_on();
498 if (!ide_devices_found)
c609719b
WD
499 return;
500 udelay (1000000); /* 1 s */
501#endif /* CONFIG_IDE_8xx_PCCARD */
502
503 WATCHDOG_RESET();
504
505 /* Initialize PIO timing tables */
506 for (i=0; i <= IDE_MAX_PIO_MODE; ++i) {
507 pio_config_clk[i].t_setup = PCMCIA_MK_CLKS(pio_config_ns[i].t_setup,
508 gd->bus_clk);
509 pio_config_clk[i].t_length = PCMCIA_MK_CLKS(pio_config_ns[i].t_length,
510 gd->bus_clk);
511 pio_config_clk[i].t_hold = PCMCIA_MK_CLKS(pio_config_ns[i].t_hold,
512 gd->bus_clk);
513 PRINTF ("PIO Mode %d: setup=%2d ns/%d clk"
514 " len=%3d ns/%d clk"
515 " hold=%2d ns/%d clk\n",
516 i,
517 pio_config_ns[i].t_setup, pio_config_clk[i].t_setup,
518 pio_config_ns[i].t_length, pio_config_clk[i].t_length,
519 pio_config_ns[i].t_hold, pio_config_clk[i].t_hold);
520 }
521
522 /* Reset the IDE just to be sure.
523 * Light LED's to show
524 */
525 ide_led ((LED_IDE1 | LED_IDE2), 1); /* LED's on */
526 ide_reset (); /* ATAPI Drives seems to need a proper IDE Reset */
527
528#ifdef CONFIG_IDE_8xx_DIRECT
529 /* PCMCIA / IDE initialization for common mem space */
530 pcmp->pcmc_pgcrb = 0;
531#endif
532
533 /* start in PIO mode 0 - most relaxed timings */
534 pio_mode = 0;
535 set_pcmcia_timing (pio_mode);
536
537 /*
538 * Wait for IDE to get ready.
539 * According to spec, this can take up to 31 seconds!
540 */
c7de829c 541#ifndef CONFIG_AMIGAONEG3SE
c609719b
WD
542 for (bus=0; bus<CFG_IDE_MAXBUS; ++bus) {
543 int dev = bus * (CFG_IDE_MAXDEVICE / CFG_IDE_MAXBUS);
c7de829c
WD
544#else
545 s = getenv("ide_maxbus");
546 if (s)
547 max_bus_scan = simple_strtol(s, NULL, 10);
548 else
549 max_bus_scan = CFG_IDE_MAXBUS;
550
551 for (bus=0; bus<max_bus_scan; ++bus) {
552 int dev = bus * (CFG_IDE_MAXDEVICE / max_bus_scan);
553#endif
c609719b 554
6069ff26
WD
555#ifdef CONFIG_IDE_8xx_PCCARD
556 /* Skip non-ide devices from probing */
557 if ((ide_devices_found & (1 << bus)) == 0) {
558 ide_led ((LED_IDE1 | LED_IDE2), 0); /* LED's off */
559 continue;
560 }
561#endif
c609719b
WD
562 printf ("Bus %d: ", bus);
563
564 ide_bus_ok[bus] = 0;
565
566 /* Select device
567 */
568 udelay (100000); /* 100 ms */
2262cfee 569 ide_outb (dev, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(dev));
c609719b 570 udelay (100000); /* 100 ms */
c7de829c
WD
571#ifdef CONFIG_AMIGAONEG3SE
572 ata_reset_time = ATA_RESET_TIME;
573 s = getenv("ide_reset_timeout");
574 if (s) ata_reset_time = 2*simple_strtol(s, NULL, 10);
575#endif
c609719b
WD
576 i = 0;
577 do {
578 udelay (10000); /* 10 ms */
579
2262cfee 580 c = ide_inb (dev, ATA_STATUS);
c609719b 581 i++;
c7de829c
WD
582#ifdef CONFIG_AMIGAONEG3SE
583 if (i > (ata_reset_time * 100)) {
584#else
c609719b 585 if (i > (ATA_RESET_TIME * 100)) {
c7de829c 586#endif
c609719b
WD
587 puts ("** Timeout **\n");
588 ide_led ((LED_IDE1 | LED_IDE2), 0); /* LED's off */
c7de829c
WD
589#ifdef CONFIG_AMIGAONEG3SE
590 /* If this is the second bus, the first one was OK */
591 if (bus != 0)
592 {
593 ide_bus_ok[bus] = 0;
594 goto skip_bus;
595 }
596#endif
c609719b
WD
597 return;
598 }
599 if ((i >= 100) && ((i%100)==0)) {
600 putc ('.');
601 }
602 } while (c & ATA_STAT_BUSY);
603
604 if (c & (ATA_STAT_BUSY | ATA_STAT_FAULT)) {
605 puts ("not available ");
606 PRINTF ("Status = 0x%02X ", c);
607#ifndef CONFIG_ATAPI /* ATAPI Devices do not set DRDY */
608 } else if ((c & ATA_STAT_READY) == 0) {
609 puts ("not available ");
610 PRINTF ("Status = 0x%02X ", c);
611#endif
612 } else {
613 puts ("OK ");
614 ide_bus_ok[bus] = 1;
615 }
616 WATCHDOG_RESET();
617 }
c7de829c
WD
618
619#ifdef CONFIG_AMIGAONEG3SE
620 skip_bus:
621#endif
c609719b
WD
622 putc ('\n');
623
624 ide_led ((LED_IDE1 | LED_IDE2), 0); /* LED's off */
625
626 curr_device = -1;
627 for (i=0; i<CFG_IDE_MAXDEVICE; ++i) {
628#ifdef CONFIG_IDE_LED
629 int led = (IDE_BUS(i) == 0) ? LED_IDE1 : LED_IDE2;
630#endif
631 ide_dev_desc[i].if_type=IF_TYPE_IDE;
632 ide_dev_desc[i].dev=i;
633 ide_dev_desc[i].part_type=PART_TYPE_UNKNOWN;
634 ide_dev_desc[i].blksz=0;
635 ide_dev_desc[i].lba=0;
636 ide_dev_desc[i].block_read=ide_read;
637 if (!ide_bus_ok[IDE_BUS(i)])
638 continue;
639 ide_led (led, 1); /* LED on */
640 ide_ident(&ide_dev_desc[i]);
641 ide_led (led, 0); /* LED off */
642 dev_print(&ide_dev_desc[i]);
643/* ide_print (i); */
644 if ((ide_dev_desc[i].lba > 0) && (ide_dev_desc[i].blksz > 0)) {
645 init_part (&ide_dev_desc[i]); /* initialize partition type */
646 if (curr_device < 0)
647 curr_device = i;
648 }
649 }
650 WATCHDOG_RESET();
651}
652
653/* ------------------------------------------------------------------------- */
654
655block_dev_desc_t * ide_get_dev(int dev)
656{
657 return ((block_dev_desc_t *)&ide_dev_desc[dev]);
658}
659
660
661#ifdef CONFIG_IDE_8xx_DIRECT
662
663static void
664set_pcmcia_timing (int pmode)
665{
666 volatile immap_t *immr = (immap_t *)CFG_IMMR;
667 volatile pcmconf8xx_t *pcmp = &(immr->im_pcmcia);
668 ulong timings;
669
670 PRINTF ("Set timing for PIO Mode %d\n", pmode);
671
672 timings = PCMCIA_SHT(pio_config_clk[pmode].t_hold)
673 | PCMCIA_SST(pio_config_clk[pmode].t_setup)
674 | PCMCIA_SL (pio_config_clk[pmode].t_length)
675 ;
676
677 /* IDE 0
678 */
679 pcmp->pcmc_pbr0 = CFG_PCMCIA_PBR0;
680 pcmp->pcmc_por0 = CFG_PCMCIA_POR0
681#if (CFG_PCMCIA_POR0 != 0)
682 | timings
683#endif
684 ;
685 PRINTF ("PBR0: %08x POR0: %08x\n", pcmp->pcmc_pbr0, pcmp->pcmc_por0);
686
687 pcmp->pcmc_pbr1 = CFG_PCMCIA_PBR1;
688 pcmp->pcmc_por1 = CFG_PCMCIA_POR1
689#if (CFG_PCMCIA_POR1 != 0)
690 | timings
691#endif
692 ;
693 PRINTF ("PBR1: %08x POR1: %08x\n", pcmp->pcmc_pbr1, pcmp->pcmc_por1);
694
695 pcmp->pcmc_pbr2 = CFG_PCMCIA_PBR2;
696 pcmp->pcmc_por2 = CFG_PCMCIA_POR2
697#if (CFG_PCMCIA_POR2 != 0)
698 | timings
699#endif
700 ;
701 PRINTF ("PBR2: %08x POR2: %08x\n", pcmp->pcmc_pbr2, pcmp->pcmc_por2);
702
703 pcmp->pcmc_pbr3 = CFG_PCMCIA_PBR3;
704 pcmp->pcmc_por3 = CFG_PCMCIA_POR3
705#if (CFG_PCMCIA_POR3 != 0)
706 | timings
707#endif
708 ;
709 PRINTF ("PBR3: %08x POR3: %08x\n", pcmp->pcmc_pbr3, pcmp->pcmc_por3);
710
711 /* IDE 1
712 */
713 pcmp->pcmc_pbr4 = CFG_PCMCIA_PBR4;
714 pcmp->pcmc_por4 = CFG_PCMCIA_POR4
715#if (CFG_PCMCIA_POR4 != 0)
716 | timings
717#endif
718 ;
719 PRINTF ("PBR4: %08x POR4: %08x\n", pcmp->pcmc_pbr4, pcmp->pcmc_por4);
720
721 pcmp->pcmc_pbr5 = CFG_PCMCIA_PBR5;
722 pcmp->pcmc_por5 = CFG_PCMCIA_POR5
723#if (CFG_PCMCIA_POR5 != 0)
724 | timings
725#endif
726 ;
727 PRINTF ("PBR5: %08x POR5: %08x\n", pcmp->pcmc_pbr5, pcmp->pcmc_por5);
728
729 pcmp->pcmc_pbr6 = CFG_PCMCIA_PBR6;
730 pcmp->pcmc_por6 = CFG_PCMCIA_POR6
731#if (CFG_PCMCIA_POR6 != 0)
732 | timings
733#endif
734 ;
735 PRINTF ("PBR6: %08x POR6: %08x\n", pcmp->pcmc_pbr6, pcmp->pcmc_por6);
736
737 pcmp->pcmc_pbr7 = CFG_PCMCIA_PBR7;
738 pcmp->pcmc_por7 = CFG_PCMCIA_POR7
739#if (CFG_PCMCIA_POR7 != 0)
740 | timings
741#endif
742 ;
743 PRINTF ("PBR7: %08x POR7: %08x\n", pcmp->pcmc_pbr7, pcmp->pcmc_por7);
744
745}
746
747#endif /* CONFIG_IDE_8xx_DIRECT */
748
749/* ------------------------------------------------------------------------- */
750
2262cfee 751#ifdef __PPC__
c609719b 752static void __inline__
2262cfee 753ide_outb(int dev, int port, unsigned char val)
c609719b
WD
754{
755 /* Ensure I/O operations complete */
756 __asm__ volatile("eieio");
757 *((uchar *)(ATA_CURR_BASE(dev)+port)) = val;
758#if 0
2262cfee 759 printf ("ide_outb: 0x%08lx <== 0x%02x\n", ATA_CURR_BASE(dev)+port, val);
c609719b
WD
760#endif
761}
2262cfee
WD
762#else /* ! __PPC__ */
763static void __inline__
764ide_outb(int dev, int port, unsigned char val)
765{
766 outb(val, port);
767}
768#endif /* __PPC__ */
769
c609719b 770
2262cfee 771#ifdef __PPC__
c609719b 772static unsigned char __inline__
2262cfee 773ide_inb(int dev, int port)
c609719b
WD
774{
775 uchar val;
776 /* Ensure I/O operations complete */
777 __asm__ volatile("eieio");
778 val = *((uchar *)(ATA_CURR_BASE(dev)+port));
779#if 0
2262cfee 780 printf ("ide_inb: 0x%08lx ==> 0x%02x\n", ATA_CURR_BASE(dev)+port, val);
c609719b
WD
781#endif
782 return (val);
783}
2262cfee
WD
784#else /* ! __PPC__ */
785static unsigned char __inline__
786ide_inb(int dev, int port)
787{
788 return inb(port);
789}
790#endif /* __PPC__ */
c609719b 791
2262cfee 792#ifdef __PPC__
cceb871f 793# ifdef CONFIG_AMIGAONEG3SE
c7de829c
WD
794static void
795output_data_short(int dev, ulong *sect_buf, int words)
796{
797 ushort *dbuf;
798 volatile ushort *pbuf;
8bde7f77 799
c7de829c
WD
800 pbuf = (ushort *)(ATA_CURR_BASE(dev)+ATA_DATA_REG);
801 dbuf = (ushort *)sect_buf;
802 while (words--) {
803 __asm__ volatile ("eieio");
804 *pbuf = *dbuf++;
805 __asm__ volatile ("eieio");
806 }
807
808 if (words&1)
809 *pbuf = 0;
810}
cceb871f 811# endif /* CONFIG_AMIGAONEG3SE */
c7de829c 812
c609719b
WD
813static void
814input_swap_data(int dev, ulong *sect_buf, int words)
815{
816 volatile ushort *pbuf = (ushort *)(ATA_CURR_BASE(dev)+ATA_DATA_REG);
817 ushort *dbuf = (ushort *)sect_buf;
818
819 while (words--) {
820 *dbuf++ = ld_le16(pbuf);
821 *dbuf++ = ld_le16(pbuf);
822 }
823}
2262cfee
WD
824#else /* ! __PPC__ */
825#define input_swap_data(x,y,z) input_data(x,y,z)
826#endif /* __PPC__ */
827
828
2262cfee 829#ifdef __PPC__
c609719b
WD
830static void
831output_data(int dev, ulong *sect_buf, int words)
832{
833 ushort *dbuf;
834 volatile ushort *pbuf;
835
836 pbuf = (ushort *)(ATA_CURR_BASE(dev)+ATA_DATA_REG);
837 dbuf = (ushort *)sect_buf;
838 while (words--) {
839 __asm__ volatile ("eieio");
840 *pbuf = *dbuf++;
841 __asm__ volatile ("eieio");
842 *pbuf = *dbuf++;
843 }
844}
2262cfee
WD
845#else /* ! __PPC__ */
846static void
847output_data(int dev, ulong *sect_buf, int words)
848{
849 outsw(ATA_DATA_REG, sect_buf, words<<1);
850}
851#endif /* __PPC__ */
c609719b 852
2262cfee 853#ifdef __PPC__
c609719b
WD
854static void
855input_data(int dev, ulong *sect_buf, int words)
856{
857 ushort *dbuf;
858 volatile ushort *pbuf;
859
860 pbuf = (ushort *)(ATA_CURR_BASE(dev)+ATA_DATA_REG);
861 dbuf = (ushort *)sect_buf;
862 while (words--) {
863 __asm__ volatile ("eieio");
864 *dbuf++ = *pbuf;
865 __asm__ volatile ("eieio");
866 *dbuf++ = *pbuf;
867 }
868}
2262cfee
WD
869#else /* ! __PPC__ */
870static void
871input_data(int dev, ulong *sect_buf, int words)
872{
873 insw(ATA_DATA_REG, sect_buf, words << 1);
874}
875
876#endif /* __PPC__ */
c609719b 877
c7de829c
WD
878#ifdef CONFIG_AMIGAONEG3SE
879static void
880input_data_short(int dev, ulong *sect_buf, int words)
881{
882 ushort *dbuf;
883 volatile ushort *pbuf;
884
885 pbuf = (ushort *)(ATA_CURR_BASE(dev)+ATA_DATA_REG);
886 dbuf = (ushort *)sect_buf;
887 while (words--) {
888 __asm__ volatile ("eieio");
889 *dbuf++ = *pbuf;
890 __asm__ volatile ("eieio");
891 }
892
893 if (words&1)
894 {
895 ushort dummy;
896 dummy = *pbuf;
897 }
898}
899#endif
900
c609719b
WD
901/* -------------------------------------------------------------------------
902 */
903static void ide_ident (block_dev_desc_t *dev_desc)
904{
905 ulong iobuf[ATA_SECTORWORDS];
906 unsigned char c;
907 hd_driveid_t *iop = (hd_driveid_t *)iobuf;
908
c7de829c
WD
909#ifdef CONFIG_AMIGAONEG3SE
910 int max_bus_scan;
911 int retries = 0;
912 char *s;
913 int do_retry = 0;
914#endif
915
c609719b
WD
916#if 0
917 int mode, cycle_time;
918#endif
919 int device;
920 device=dev_desc->dev;
921 printf (" Device %d: ", device);
922
c7de829c
WD
923#ifdef CONFIG_AMIGAONEG3SE
924 s = getenv("ide_maxbus");
925 if (s) {
926 max_bus_scan = simple_strtol(s, NULL, 10);
927 } else {
928 max_bus_scan = CFG_IDE_MAXBUS;
929 }
930 if (device >= max_bus_scan*2) {
931 dev_desc->type=DEV_TYPE_UNKNOWN;
932 return;
933 }
934#endif
935
c609719b
WD
936 ide_led (DEVICE_LED(device), 1); /* LED on */
937 /* Select device
938 */
2262cfee 939 ide_outb (device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
c609719b
WD
940 dev_desc->if_type=IF_TYPE_IDE;
941#ifdef CONFIG_ATAPI
c7de829c
WD
942
943#ifdef CONFIG_AMIGAONEG3SE
944 do_retry = 0;
945 retries = 0;
946
947 /* Warning: This will be tricky to read */
948 while (retries <= 1)
949 {
950#endif /* CONFIG_AMIGAONEG3SE */
951
c609719b 952 /* check signature */
2262cfee
WD
953 if ((ide_inb(device,ATA_SECT_CNT) == 0x01) &&
954 (ide_inb(device,ATA_SECT_NUM) == 0x01) &&
955 (ide_inb(device,ATA_CYL_LOW) == 0x14) &&
956 (ide_inb(device,ATA_CYL_HIGH) == 0xEB)) {
c609719b
WD
957 /* ATAPI Signature found */
958 dev_desc->if_type=IF_TYPE_ATAPI;
959 /* Start Ident Command
960 */
2262cfee 961 ide_outb (device, ATA_COMMAND, ATAPI_CMD_IDENT);
c609719b
WD
962 /*
963 * Wait for completion - ATAPI devices need more time
964 * to become ready
965 */
966 c = ide_wait (device, ATAPI_TIME_OUT);
967 }
968 else
969#endif
970 {
971 /* Start Ident Command
972 */
2262cfee 973 ide_outb (device, ATA_COMMAND, ATA_CMD_IDENT);
c609719b
WD
974
975 /* Wait for completion
976 */
977 c = ide_wait (device, IDE_TIME_OUT);
978 }
979 ide_led (DEVICE_LED(device), 0); /* LED off */
980
981 if (((c & ATA_STAT_DRQ) == 0) ||
982 ((c & (ATA_STAT_FAULT|ATA_STAT_ERR)) != 0) ) {
c7de829c
WD
983#ifdef CONFIG_AMIGAONEG3SE
984 if (retries == 0) {
985 do_retry = 1;
986 } else {
987 dev_desc->type=DEV_TYPE_UNKNOWN;
988 return;
989 }
990#else
c609719b
WD
991 dev_desc->type=DEV_TYPE_UNKNOWN;
992 return;
c7de829c
WD
993#endif /* CONFIG_AMIGAONEG3SE */
994 }
995
996#ifdef CONFIG_AMIGAONEG3SE
997 s = getenv("ide_doreset");
998 if (s && strcmp(s, "on") == 0 && 1 == do_retry) {
999 /* Need to soft reset the device in case it's an ATAPI... */
1000 PRINTF("Retrying...\n");
1001 ide_outb (device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
1002 udelay(100000);
1003 ide_outb (device, ATA_COMMAND, 0x08);
1004 udelay (100000); /* 100 ms */
1005 retries++;
1006 } else {
1007 retries = 100;
c609719b 1008 }
c7de829c
WD
1009 } /* see above - ugly to read */
1010#endif /* CONFIG_AMIGAONEG3SE */
c609719b
WD
1011
1012 input_swap_data (device, iobuf, ATA_SECTORWORDS);
1013
1014 ident_cpy (dev_desc->revision, iop->fw_rev, sizeof(dev_desc->revision));
1015 ident_cpy (dev_desc->vendor, iop->model, sizeof(dev_desc->vendor));
1016 ident_cpy (dev_desc->product, iop->serial_no, sizeof(dev_desc->product));
1017
1018 if ((iop->config & 0x0080)==0x0080)
1019 dev_desc->removable = 1;
1020 else
1021 dev_desc->removable = 0;
1022
1023#if 0
1024 /*
1025 * Drive PIO mode autoselection
1026 */
1027 mode = iop->tPIO;
1028
1029 printf ("tPIO = 0x%02x = %d\n",mode, mode);
1030 if (mode > 2) { /* 2 is maximum allowed tPIO value */
1031 mode = 2;
1032 PRINTF ("Override tPIO -> 2\n");
1033 }
1034 if (iop->field_valid & 2) { /* drive implements ATA2? */
1035 PRINTF ("Drive implements ATA2\n");
1036 if (iop->capability & 8) { /* drive supports use_iordy? */
1037 cycle_time = iop->eide_pio_iordy;
1038 } else {
1039 cycle_time = iop->eide_pio;
1040 }
1041 PRINTF ("cycle time = %d\n", cycle_time);
1042 mode = 4;
1043 if (cycle_time > 120) mode = 3; /* 120 ns for PIO mode 4 */
1044 if (cycle_time > 180) mode = 2; /* 180 ns for PIO mode 3 */
1045 if (cycle_time > 240) mode = 1; /* 240 ns for PIO mode 4 */
1046 if (cycle_time > 383) mode = 0; /* 383 ns for PIO mode 4 */
1047 }
1048 printf ("PIO mode to use: PIO %d\n", mode);
1049#endif /* 0 */
1050
1051#ifdef CONFIG_ATAPI
1052 if (dev_desc->if_type==IF_TYPE_ATAPI) {
1053 atapi_inquiry(dev_desc);
1054 return;
1055 }
1056#endif /* CONFIG_ATAPI */
1057
1058 /* swap shorts */
1059 dev_desc->lba = (iop->lba_capacity << 16) | (iop->lba_capacity >> 16);
1060 /* assuming HD */
1061 dev_desc->type=DEV_TYPE_HARDDISK;
1062 dev_desc->blksz=ATA_BLOCKSIZE;
1063 dev_desc->lun=0; /* just to fill something in... */
1064
1065#if 0 /* only used to test the powersaving mode,
1066 * if enabled, the drive goes after 5 sec
1067 * in standby mode */
2262cfee 1068 ide_outb (device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
c609719b 1069 c = ide_wait (device, IDE_TIME_OUT);
2262cfee
WD
1070 ide_outb (device, ATA_SECT_CNT, 1);
1071 ide_outb (device, ATA_LBA_LOW, 0);
1072 ide_outb (device, ATA_LBA_MID, 0);
1073 ide_outb (device, ATA_LBA_HIGH, 0);
1074 ide_outb (device, ATA_DEV_HD, ATA_LBA |
c609719b 1075 ATA_DEVICE(device));
2262cfee 1076 ide_outb (device, ATA_COMMAND, 0xe3);
c609719b
WD
1077 udelay (50);
1078 c = ide_wait (device, IDE_TIME_OUT); /* can't take over 500 ms */
1079#endif
1080}
1081
1082
1083/* ------------------------------------------------------------------------- */
1084
1085ulong ide_read (int device, ulong blknr, ulong blkcnt, ulong *buffer)
1086{
1087 ulong n = 0;
1088 unsigned char c;
1089 unsigned char pwrsave=0; /* power save */
1090
1091 PRINTF ("ide_read dev %d start %lX, blocks %lX buffer at %lX\n",
1092 device, blknr, blkcnt, (ulong)buffer);
1093
1094 ide_led (DEVICE_LED(device), 1); /* LED on */
1095
1096 /* Select device
1097 */
2262cfee 1098 ide_outb (device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
c609719b
WD
1099 c = ide_wait (device, IDE_TIME_OUT);
1100
1101 if (c & ATA_STAT_BUSY) {
1102 printf ("IDE read: device %d not ready\n", device);
1103 goto IDE_READ_E;
1104 }
1105
1106 /* first check if the drive is in Powersaving mode, if yes,
1107 * increase the timeout value */
2262cfee 1108 ide_outb (device, ATA_COMMAND, ATA_CMD_CHK_PWR);
c609719b
WD
1109 udelay (50);
1110
1111 c = ide_wait (device, IDE_TIME_OUT); /* can't take over 500 ms */
1112
1113 if (c & ATA_STAT_BUSY) {
1114 printf ("IDE read: device %d not ready\n", device);
1115 goto IDE_READ_E;
1116 }
1117 if ((c & ATA_STAT_ERR) == ATA_STAT_ERR) {
1118 printf ("No Powersaving mode %X\n", c);
1119 } else {
2262cfee 1120 c = ide_inb(device,ATA_SECT_CNT);
c609719b
WD
1121 PRINTF("Powersaving %02X\n",c);
1122 if(c==0)
1123 pwrsave=1;
1124 }
1125
1126
1127 while (blkcnt-- > 0) {
1128
1129 c = ide_wait (device, IDE_TIME_OUT);
1130
1131 if (c & ATA_STAT_BUSY) {
1132 printf ("IDE read: device %d not ready\n", device);
1133 break;
1134 }
1135
2262cfee
WD
1136 ide_outb (device, ATA_SECT_CNT, 1);
1137 ide_outb (device, ATA_LBA_LOW, (blknr >> 0) & 0xFF);
1138 ide_outb (device, ATA_LBA_MID, (blknr >> 8) & 0xFF);
1139 ide_outb (device, ATA_LBA_HIGH, (blknr >> 16) & 0xFF);
1140 ide_outb (device, ATA_DEV_HD, ATA_LBA |
c609719b
WD
1141 ATA_DEVICE(device) |
1142 ((blknr >> 24) & 0xF) );
2262cfee 1143 ide_outb (device, ATA_COMMAND, ATA_CMD_READ);
c609719b
WD
1144
1145 udelay (50);
1146
1147 if(pwrsave) {
1148 c = ide_wait (device, IDE_SPIN_UP_TIME_OUT); /* may take up to 4 sec */
1149 pwrsave=0;
1150 } else {
1151 c = ide_wait (device, IDE_TIME_OUT); /* can't take over 500 ms */
1152 }
1153
1154 if ((c&(ATA_STAT_DRQ|ATA_STAT_BUSY|ATA_STAT_ERR)) != ATA_STAT_DRQ) {
1155 printf ("Error (no IRQ) dev %d blk %ld: status 0x%02x\n",
1156 device, blknr, c);
1157 break;
1158 }
1159
1160 input_data (device, buffer, ATA_SECTORWORDS);
2262cfee 1161 (void) ide_inb (device, ATA_STATUS); /* clear IRQ */
c609719b
WD
1162
1163 ++n;
1164 ++blknr;
1165 buffer += ATA_SECTORWORDS;
1166 }
1167IDE_READ_E:
1168 ide_led (DEVICE_LED(device), 0); /* LED off */
1169 return (n);
1170}
1171
1172/* ------------------------------------------------------------------------- */
1173
1174
1175ulong ide_write (int device, ulong blknr, ulong blkcnt, ulong *buffer)
1176{
1177 ulong n = 0;
1178 unsigned char c;
1179
1180 ide_led (DEVICE_LED(device), 1); /* LED on */
1181
1182 /* Select device
1183 */
2262cfee 1184 ide_outb (device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
c609719b
WD
1185
1186 while (blkcnt-- > 0) {
1187
1188 c = ide_wait (device, IDE_TIME_OUT);
1189
1190 if (c & ATA_STAT_BUSY) {
1191 printf ("IDE read: device %d not ready\n", device);
1192 goto WR_OUT;
1193 }
1194
2262cfee
WD
1195 ide_outb (device, ATA_SECT_CNT, 1);
1196 ide_outb (device, ATA_LBA_LOW, (blknr >> 0) & 0xFF);
1197 ide_outb (device, ATA_LBA_MID, (blknr >> 8) & 0xFF);
1198 ide_outb (device, ATA_LBA_HIGH, (blknr >> 16) & 0xFF);
1199 ide_outb (device, ATA_DEV_HD, ATA_LBA |
c609719b
WD
1200 ATA_DEVICE(device) |
1201 ((blknr >> 24) & 0xF) );
2262cfee 1202 ide_outb (device, ATA_COMMAND, ATA_CMD_WRITE);
c609719b
WD
1203
1204 udelay (50);
1205
1206 c = ide_wait (device, IDE_TIME_OUT); /* can't take over 500 ms */
1207
1208 if ((c&(ATA_STAT_DRQ|ATA_STAT_BUSY|ATA_STAT_ERR)) != ATA_STAT_DRQ) {
1209 printf ("Error (no IRQ) dev %d blk %ld: status 0x%02x\n",
1210 device, blknr, c);
1211 goto WR_OUT;
1212 }
1213
1214 output_data (device, buffer, ATA_SECTORWORDS);
2262cfee 1215 c = ide_inb (device, ATA_STATUS); /* clear IRQ */
c609719b
WD
1216 ++n;
1217 ++blknr;
1218 buffer += ATA_SECTORWORDS;
1219 }
1220WR_OUT:
1221 ide_led (DEVICE_LED(device), 0); /* LED off */
1222 return (n);
1223}
1224
1225/* ------------------------------------------------------------------------- */
1226
1227/*
1228 * copy src to dest, skipping leading and trailing blanks and null
1229 * terminate the string
1230 */
1231static void ident_cpy (unsigned char *dest, unsigned char *src, unsigned int len)
1232{
1233 int start,end;
1234
1235 start=0;
1236 while (start<len) {
1237 if (src[start]!=' ')
1238 break;
1239 start++;
1240 }
1241 end=len-1;
1242 while (end>start) {
1243 if (src[end]!=' ')
1244 break;
1245 end--;
1246 }
1247 for ( ; start<=end; start++) {
1248 *dest++=src[start];
1249 }
1250 *dest='\0';
1251}
1252
1253/* ------------------------------------------------------------------------- */
1254
1255/*
1256 * Wait until Busy bit is off, or timeout (in ms)
1257 * Return last status
1258 */
1259static uchar ide_wait (int dev, ulong t)
1260{
1261 ulong delay = 10 * t; /* poll every 100 us */
1262 uchar c;
1263
2262cfee 1264 while ((c = ide_inb(dev, ATA_STATUS)) & ATA_STAT_BUSY) {
c609719b
WD
1265 udelay (100);
1266 if (delay-- == 0) {
1267 break;
1268 }
1269 }
1270 return (c);
1271}
1272
1273/* ------------------------------------------------------------------------- */
1274
1275#ifdef CONFIG_IDE_RESET
1276extern void ide_set_reset(int idereset);
1277
1278static void ide_reset (void)
1279{
1280#if defined(CFG_PB_12V_ENABLE) || defined(CFG_PB_IDE_MOTOR)
1281 volatile immap_t *immr = (immap_t *)CFG_IMMR;
1282#endif
1283 int i;
1284
1285 curr_device = -1;
1286 for (i=0; i<CFG_IDE_MAXBUS; ++i)
1287 ide_bus_ok[i] = 0;
1288 for (i=0; i<CFG_IDE_MAXDEVICE; ++i)
1289 ide_dev_desc[i].type = DEV_TYPE_UNKNOWN;
1290
1291 ide_set_reset (1); /* assert reset */
1292
1293 WATCHDOG_RESET();
1294
1295#ifdef CFG_PB_12V_ENABLE
1296 immr->im_cpm.cp_pbdat &= ~(CFG_PB_12V_ENABLE); /* 12V Enable output OFF */
1297 immr->im_cpm.cp_pbpar &= ~(CFG_PB_12V_ENABLE);
1298 immr->im_cpm.cp_pbodr &= ~(CFG_PB_12V_ENABLE);
1299 immr->im_cpm.cp_pbdir |= CFG_PB_12V_ENABLE;
1300
1301 /* wait 500 ms for the voltage to stabilize
1302 */
1303 for (i=0; i<500; ++i) {
1304 udelay (1000);
1305 }
1306
1307 immr->im_cpm.cp_pbdat |= CFG_PB_12V_ENABLE; /* 12V Enable output ON */
1308#endif /* CFG_PB_12V_ENABLE */
1309
1310#ifdef CFG_PB_IDE_MOTOR
1311 /* configure IDE Motor voltage monitor pin as input */
1312 immr->im_cpm.cp_pbpar &= ~(CFG_PB_IDE_MOTOR);
1313 immr->im_cpm.cp_pbodr &= ~(CFG_PB_IDE_MOTOR);
1314 immr->im_cpm.cp_pbdir &= ~(CFG_PB_IDE_MOTOR);
1315
1316 /* wait up to 1 s for the motor voltage to stabilize
1317 */
1318 for (i=0; i<1000; ++i) {
1319 if ((immr->im_cpm.cp_pbdat & CFG_PB_IDE_MOTOR) != 0) {
1320 break;
1321 }
1322 udelay (1000);
1323 }
1324
1325 if (i == 1000) { /* Timeout */
1326 printf ("\nWarning: 5V for IDE Motor missing\n");
1327# ifdef CONFIG_STATUS_LED
1328# ifdef STATUS_LED_YELLOW
1329 status_led_set (STATUS_LED_YELLOW, STATUS_LED_ON );
1330# endif
1331# ifdef STATUS_LED_GREEN
1332 status_led_set (STATUS_LED_GREEN, STATUS_LED_OFF);
1333# endif
1334# endif /* CONFIG_STATUS_LED */
1335 }
1336#endif /* CFG_PB_IDE_MOTOR */
1337
1338 WATCHDOG_RESET();
1339
1340 /* de-assert RESET signal */
1341 ide_set_reset(0);
1342
1343 /* wait 250 ms */
1344 for (i=0; i<250; ++i) {
1345 udelay (1000);
1346 }
1347}
1348
1349#endif /* CONFIG_IDE_RESET */
1350
1351/* ------------------------------------------------------------------------- */
1352
1f53a416 1353#if defined(CONFIG_IDE_LED) && !defined(CONFIG_AMIGAONEG3SE) && !defined(CONFIG_KUP4K)
c609719b
WD
1354
1355static uchar led_buffer = 0; /* Buffer for current LED status */
1356
1357static void ide_led (uchar led, uchar status)
1358{
1359 uchar *led_port = LED_PORT;
1360
1361 if (status) { /* switch LED on */
1362 led_buffer |= led;
1363 } else { /* switch LED off */
1364 led_buffer &= ~led;
1365 }
1366
1367 *led_port = led_buffer;
1368}
1369
1370#endif /* CONFIG_IDE_LED */
1371
1372/* ------------------------------------------------------------------------- */
1373
1374#ifdef CONFIG_ATAPI
1375/****************************************************************************
1376 * ATAPI Support
1377 */
1378
1379
c609719b
WD
1380#undef ATAPI_DEBUG
1381
1382#ifdef ATAPI_DEBUG
1383#define AT_PRINTF(fmt,args...) printf (fmt ,##args)
1384#else
1385#define AT_PRINTF(fmt,args...)
1386#endif
1387
2262cfee 1388#ifdef __PPC__
c609719b
WD
1389/* since ATAPI may use commands with not 4 bytes alligned length
1390 * we have our own transfer functions, 2 bytes alligned */
1391static void
1392output_data_shorts(int dev, ushort *sect_buf, int shorts)
1393{
1394 ushort *dbuf;
1395 volatile ushort *pbuf;
1396
1397 pbuf = (ushort *)(ATA_CURR_BASE(dev)+ATA_DATA_REG);
1398 dbuf = (ushort *)sect_buf;
1399 while (shorts--) {
1400 __asm__ volatile ("eieio");
1401 *pbuf = *dbuf++;
1402 }
1403}
1404
1405static void
1406input_data_shorts(int dev, ushort *sect_buf, int shorts)
1407{
1408 ushort *dbuf;
1409 volatile ushort *pbuf;
1410
1411 pbuf = (ushort *)(ATA_CURR_BASE(dev)+ATA_DATA_REG);
1412 dbuf = (ushort *)sect_buf;
1413 while (shorts--) {
1414 __asm__ volatile ("eieio");
1415 *dbuf++ = *pbuf;
1416 }
1417}
1418
2262cfee
WD
1419#else /* ! __PPC__ */
1420static void
1421output_data_shorts(int dev, ushort *sect_buf, int shorts)
1422{
1423 outsw(ATA_DATA_REG, sect_buf, shorts);
1424}
1425
1426
1427static void
1428input_data_shorts(int dev, ushort *sect_buf, int shorts)
1429{
1430 insw(ATA_DATA_REG, sect_buf, shorts);
1431}
1432
1433#endif /* __PPC__ */
1434
c609719b
WD
1435/*
1436 * Wait until (Status & mask) == res, or timeout (in ms)
1437 * Return last status
1438 * This is used since some ATAPI CD ROMs clears their Busy Bit first
1439 * and then they set their DRQ Bit
1440 */
1441static uchar atapi_wait_mask (int dev, ulong t,uchar mask, uchar res)
1442{
1443 ulong delay = 10 * t; /* poll every 100 us */
1444 uchar c;
1445
2262cfee
WD
1446 c = ide_inb(dev,ATA_DEV_CTL); /* prevents to read the status before valid */
1447 while (((c = ide_inb(dev, ATA_STATUS)) & mask) != res) {
c609719b
WD
1448 /* break if error occurs (doesn't make sense to wait more) */
1449 if((c & ATA_STAT_ERR)==ATA_STAT_ERR)
1450 break;
1451 udelay (100);
1452 if (delay-- == 0) {
1453 break;
1454 }
1455 }
1456 return (c);
1457}
1458
1459/*
1460 * issue an atapi command
1461 */
1462unsigned char atapi_issue(int device,unsigned char* ccb,int ccblen, unsigned char * buffer,int buflen)
1463{
1464 unsigned char c,err,mask,res;
1465 int n;
1466 ide_led (DEVICE_LED(device), 1); /* LED on */
1467
1468 /* Select device
1469 */
1470 mask = ATA_STAT_BUSY|ATA_STAT_DRQ;
1471 res = 0;
c7de829c
WD
1472#ifdef CONFIG_AMIGAONEG3SE
1473# warning THF: Removed LBA mode ???
1474#endif
2262cfee 1475 ide_outb (device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
c609719b
WD
1476 c = atapi_wait_mask(device,ATAPI_TIME_OUT,mask,res);
1477 if ((c & mask) != res) {
1478 printf ("ATAPI_ISSUE: device %d not ready status %X\n", device,c);
1479 err=0xFF;
1480 goto AI_OUT;
1481 }
1482 /* write taskfile */
2262cfee 1483 ide_outb (device, ATA_ERROR_REG, 0); /* no DMA, no overlaped */
c7de829c
WD
1484 ide_outb (device, ATA_SECT_CNT, 0);
1485 ide_outb (device, ATA_SECT_NUM, 0);
2262cfee 1486 ide_outb (device, ATA_CYL_LOW, (unsigned char)(buflen & 0xFF));
c7de829c
WD
1487 ide_outb (device, ATA_CYL_HIGH, (unsigned char)((buflen>>8) & 0xFF));
1488#ifdef CONFIG_AMIGAONEG3SE
1489# warning THF: Removed LBA mode ???
1490#endif
2262cfee 1491 ide_outb (device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
c609719b 1492
2262cfee 1493 ide_outb (device, ATA_COMMAND, ATAPI_CMD_PACKET);
c609719b
WD
1494 udelay (50);
1495
1496 mask = ATA_STAT_DRQ|ATA_STAT_BUSY|ATA_STAT_ERR;
1497 res = ATA_STAT_DRQ;
1498 c = atapi_wait_mask(device,ATAPI_TIME_OUT,mask,res);
1499
1500 if ((c & mask) != res) { /* DRQ must be 1, BSY 0 */
1501 printf ("ATTAPI_ISSUE: Error (no IRQ) before sending ccb dev %d status 0x%02x\n",device,c);
1502 err=0xFF;
1503 goto AI_OUT;
1504 }
1505
1506 output_data_shorts (device, (unsigned short *)ccb,ccblen/2); /* write command block */
1507 /* ATAPI Command written wait for completition */
1508 udelay (5000); /* device must set bsy */
1509
1510 mask = ATA_STAT_DRQ|ATA_STAT_BUSY|ATA_STAT_ERR;
1511 /* if no data wait for DRQ = 0 BSY = 0
1512 * if data wait for DRQ = 1 BSY = 0 */
1513 res=0;
1514 if(buflen)
1515 res = ATA_STAT_DRQ;
1516 c = atapi_wait_mask(device,ATAPI_TIME_OUT,mask,res);
1517 if ((c & mask) != res ) {
1518 if (c & ATA_STAT_ERR) {
2262cfee 1519 err=(ide_inb(device,ATA_ERROR_REG))>>4;
c609719b
WD
1520 AT_PRINTF("atapi_issue 1 returned sense key %X status %02X\n",err,c);
1521 } else {
1522 printf ("ATTAPI_ISSUE: (no DRQ) after sending ccb (%x) status 0x%02x\n", ccb[0],c);
1523 err=0xFF;
1524 }
1525 goto AI_OUT;
1526 }
2262cfee 1527 n=ide_inb(device, ATA_CYL_HIGH);
c609719b 1528 n<<=8;
2262cfee 1529 n+=ide_inb(device, ATA_CYL_LOW);
c609719b
WD
1530 if(n>buflen) {
1531 printf("ERROR, transfer bytes %d requested only %d\n",n,buflen);
1532 err=0xff;
1533 goto AI_OUT;
1534 }
1535 if((n==0)&&(buflen<0)) {
1536 printf("ERROR, transfer bytes %d requested %d\n",n,buflen);
1537 err=0xff;
1538 goto AI_OUT;
1539 }
1540 if(n!=buflen) {
1541 AT_PRINTF("WARNING, transfer bytes %d not equal with requested %d\n",n,buflen);
1542 }
1543 if(n!=0) { /* data transfer */
1544 AT_PRINTF("ATAPI_ISSUE: %d Bytes to transfer\n",n);
1545 /* we transfer shorts */
1546 n>>=1;
1547 /* ok now decide if it is an in or output */
2262cfee 1548 if ((ide_inb(device, ATA_SECT_CNT)&0x02)==0) {
c609719b
WD
1549 AT_PRINTF("Write to device\n");
1550 output_data_shorts(device,(unsigned short *)buffer,n);
1551 } else {
1552 AT_PRINTF("Read from device @ %p shorts %d\n",buffer,n);
1553 input_data_shorts(device,(unsigned short *)buffer,n);
1554 }
1555 }
1556 udelay(5000); /* seems that some CD ROMs need this... */
1557 mask = ATA_STAT_BUSY|ATA_STAT_ERR;
1558 res=0;
1559 c = atapi_wait_mask(device,ATAPI_TIME_OUT,mask,res);
1560 if ((c & ATA_STAT_ERR) == ATA_STAT_ERR) {
2262cfee 1561 err=(ide_inb(device,ATA_ERROR_REG) >> 4);
c609719b
WD
1562 AT_PRINTF("atapi_issue 2 returned sense key %X status %X\n",err,c);
1563 } else {
1564 err = 0;
1565 }
1566AI_OUT:
1567 ide_led (DEVICE_LED(device), 0); /* LED off */
1568 return (err);
1569}
1570
1571/*
1572 * sending the command to atapi_issue. If an status other than good
1573 * returns, an request_sense will be issued
1574 */
1575
1576#define ATAPI_DRIVE_NOT_READY 100
1577#define ATAPI_UNIT_ATTN 10
1578
1579unsigned char atapi_issue_autoreq (int device,
1580 unsigned char* ccb,
1581 int ccblen,
1582 unsigned char *buffer,
1583 int buflen)
1584{
1585 unsigned char sense_data[18],sense_ccb[12];
1586 unsigned char res,key,asc,ascq;
1587 int notready,unitattn;
1588
c7de829c
WD
1589#ifdef CONFIG_AMIGAONEG3SE
1590 char *s;
1591 unsigned int timeout, retrycnt;
1592
1593 s = getenv("ide_cd_timeout");
1594 timeout = s ? (simple_strtol(s, NULL, 10)*1000000)/5 : 0;
1595
1596 retrycnt = 0;
1597#endif
1598
c609719b
WD
1599 unitattn=ATAPI_UNIT_ATTN;
1600 notready=ATAPI_DRIVE_NOT_READY;
1601
1602retry:
1603 res= atapi_issue(device,ccb,ccblen,buffer,buflen);
1604 if (res==0)
1605 return (0); /* Ok */
1606
1607 if (res==0xFF)
1608 return (0xFF); /* error */
1609
1610 AT_PRINTF("(auto_req)atapi_issue returned sense key %X\n",res);
1611
1612 memset(sense_ccb,0,sizeof(sense_ccb));
1613 memset(sense_data,0,sizeof(sense_data));
1614 sense_ccb[0]=ATAPI_CMD_REQ_SENSE;
c7de829c 1615 sense_ccb[4]=18; /* allocation Length */
c609719b
WD
1616
1617 res=atapi_issue(device,sense_ccb,12,sense_data,18);
1618 key=(sense_data[2]&0xF);
1619 asc=(sense_data[12]);
1620 ascq=(sense_data[13]);
1621
1622 AT_PRINTF("ATAPI_CMD_REQ_SENSE returned %x\n",res);
1623 AT_PRINTF(" Sense page: %02X key %02X ASC %02X ASCQ %02X\n",
1624 sense_data[0],
1625 key,
1626 asc,
1627 ascq);
1628
1629 if((key==0))
1630 return 0; /* ok device ready */
1631
1632 if((key==6)|| (asc==0x29) || (asc==0x28)) { /* Unit Attention */
1633 if(unitattn-->0) {
1634 udelay(200*1000);
1635 goto retry;
1636 }
1637 printf("Unit Attention, tried %d\n",ATAPI_UNIT_ATTN);
1638 goto error;
1639 }
1640 if((asc==0x4) && (ascq==0x1)) { /* not ready, but will be ready soon */
1641 if (notready-->0) {
1642 udelay(200*1000);
1643 goto retry;
1644 }
1645 printf("Drive not ready, tried %d times\n",ATAPI_DRIVE_NOT_READY);
1646 goto error;
1647 }
1648 if(asc==0x3a) {
1649 AT_PRINTF("Media not present\n");
1650 goto error;
1651 }
c7de829c
WD
1652
1653#ifdef CONFIG_AMIGAONEG3SE
1654 if ((sense_data[2]&0xF)==0x0B) {
1655 AT_PRINTF("ABORTED COMMAND...retry\n");
1656 if (retrycnt++ < 4)
1657 goto retry;
1658 return (0xFF);
1659 }
1660
1661 if ((sense_data[2]&0xf) == 0x02 &&
1662 sense_data[12] == 0x04 &&
1663 sense_data[13] == 0x01 ) {
1664 AT_PRINTF("Waiting for unit to become active\n");
1665 udelay(timeout);
1666 if (retrycnt++ < 4)
1667 goto retry;
1668 return 0xFF;
1669 }
1670#endif /* CONFIG_AMIGAONEG3SE */
1671
c609719b
WD
1672 printf ("ERROR: Unknown Sense key %02X ASC %02X ASCQ %02X\n",key,asc,ascq);
1673error:
1674 AT_PRINTF ("ERROR Sense key %02X ASC %02X ASCQ %02X\n",key,asc,ascq);
1675 return (0xFF);
1676}
1677
1678
c609719b
WD
1679static void atapi_inquiry(block_dev_desc_t * dev_desc)
1680{
1681 unsigned char ccb[12]; /* Command descriptor block */
1682 unsigned char iobuf[64]; /* temp buf */
1683 unsigned char c;
1684 int device;
1685
1686 device=dev_desc->dev;
1687 dev_desc->type=DEV_TYPE_UNKNOWN; /* not yet valid */
1688 dev_desc->block_read=atapi_read;
1689
1690 memset(ccb,0,sizeof(ccb));
1691 memset(iobuf,0,sizeof(iobuf));
1692
1693 ccb[0]=ATAPI_CMD_INQUIRY;
1694 ccb[4]=40; /* allocation Legnth */
1695 c=atapi_issue_autoreq(device,ccb,12,(unsigned char *)iobuf,40);
1696
1697 AT_PRINTF("ATAPI_CMD_INQUIRY returned %x\n",c);
1698 if (c!=0)
1699 return;
1700
1701 /* copy device ident strings */
1702 ident_cpy(dev_desc->vendor,&iobuf[8],8);
1703 ident_cpy(dev_desc->product,&iobuf[16],16);
1704 ident_cpy(dev_desc->revision,&iobuf[32],5);
1705
1706 dev_desc->lun=0;
1707 dev_desc->lba=0;
1708 dev_desc->blksz=0;
1709 dev_desc->type=iobuf[0] & 0x1f;
1710
1711 if ((iobuf[1]&0x80)==0x80)
1712 dev_desc->removable = 1;
1713 else
1714 dev_desc->removable = 0;
1715
1716 memset(ccb,0,sizeof(ccb));
1717 memset(iobuf,0,sizeof(iobuf));
1718 ccb[0]=ATAPI_CMD_START_STOP;
1719 ccb[4]=0x03; /* start */
1720
1721 c=atapi_issue_autoreq(device,ccb,12,(unsigned char *)iobuf,0);
1722
1723 AT_PRINTF("ATAPI_CMD_START_STOP returned %x\n",c);
1724 if (c!=0)
1725 return;
1726
1727 memset(ccb,0,sizeof(ccb));
1728 memset(iobuf,0,sizeof(iobuf));
1729 c=atapi_issue_autoreq(device,ccb,12,(unsigned char *)iobuf,0);
1730
1731 AT_PRINTF("ATAPI_CMD_UNIT_TEST_READY returned %x\n",c);
1732 if (c!=0)
1733 return;
1734
1735 memset(ccb,0,sizeof(ccb));
1736 memset(iobuf,0,sizeof(iobuf));
1737 ccb[0]=ATAPI_CMD_READ_CAP;
1738 c=atapi_issue_autoreq(device,ccb,12,(unsigned char *)iobuf,8);
1739 AT_PRINTF("ATAPI_CMD_READ_CAP returned %x\n",c);
1740 if (c!=0)
1741 return;
1742
1743 AT_PRINTF("Read Cap: LBA %02X%02X%02X%02X blksize %02X%02X%02X%02X\n",
1744 iobuf[0],iobuf[1],iobuf[2],iobuf[3],
1745 iobuf[4],iobuf[5],iobuf[6],iobuf[7]);
1746
1747 dev_desc->lba =((unsigned long)iobuf[0]<<24) +
1748 ((unsigned long)iobuf[1]<<16) +
1749 ((unsigned long)iobuf[2]<< 8) +
1750 ((unsigned long)iobuf[3]);
1751 dev_desc->blksz=((unsigned long)iobuf[4]<<24) +
1752 ((unsigned long)iobuf[5]<<16) +
1753 ((unsigned long)iobuf[6]<< 8) +
1754 ((unsigned long)iobuf[7]);
1755 return;
1756}
1757
1758
1759/*
1760 * atapi_read:
1761 * we transfer only one block per command, since the multiple DRQ per
1762 * command is not yet implemented
1763 */
1764#define ATAPI_READ_MAX_BYTES 2048 /* we read max 2kbytes */
1765#define ATAPI_READ_BLOCK_SIZE 2048 /* assuming CD part */
1766#define ATAPI_READ_MAX_BLOCK ATAPI_READ_MAX_BYTES/ATAPI_READ_BLOCK_SIZE /* max blocks */
1767
1768ulong atapi_read (int device, ulong blknr, ulong blkcnt, ulong *buffer)
1769{
1770 ulong n = 0;
1771 unsigned char ccb[12]; /* Command descriptor block */
1772 ulong cnt;
1773
1774 AT_PRINTF("atapi_read dev %d start %lX, blocks %lX buffer at %lX\n",
1775 device, blknr, blkcnt, (ulong)buffer);
1776
1777 do {
1778 if (blkcnt>ATAPI_READ_MAX_BLOCK) {
1779 cnt=ATAPI_READ_MAX_BLOCK;
1780 } else {
1781 cnt=blkcnt;
1782 }
1783 ccb[0]=ATAPI_CMD_READ_12;
1784 ccb[1]=0; /* reserved */
1785 ccb[2]=(unsigned char) (blknr>>24) & 0xFF; /* MSB Block */
1786 ccb[3]=(unsigned char) (blknr>>16) & 0xFF; /* */
1787 ccb[4]=(unsigned char) (blknr>> 8) & 0xFF;
1788 ccb[5]=(unsigned char) blknr & 0xFF; /* LSB Block */
1789 ccb[6]=(unsigned char) (cnt >>24) & 0xFF; /* MSB Block count */
1790 ccb[7]=(unsigned char) (cnt >>16) & 0xFF;
1791 ccb[8]=(unsigned char) (cnt >> 8) & 0xFF;
1792 ccb[9]=(unsigned char) cnt & 0xFF; /* LSB Block */
1793 ccb[10]=0; /* reserved */
1794 ccb[11]=0; /* reserved */
1795
1796 if (atapi_issue_autoreq(device,ccb,12,
1797 (unsigned char *)buffer,
1798 cnt*ATAPI_READ_BLOCK_SIZE) == 0xFF) {
1799 return (n);
1800 }
1801 n+=cnt;
1802 blkcnt-=cnt;
1803 blknr+=cnt;
1804 buffer+=cnt*(ATAPI_READ_BLOCK_SIZE/4); /* ulong blocksize in ulong */
1805 } while (blkcnt > 0);
1806 return (n);
1807}
1808
1809/* ------------------------------------------------------------------------- */
1810
1811#endif /* CONFIG_ATAPI */
1812
0d498393
WD
1813U_BOOT_CMD(
1814 ide, 5, 1, do_ide,
8bde7f77
WD
1815 "ide - IDE sub-system\n",
1816 "reset - reset IDE controller\n"
1817 "ide info - show available IDE devices\n"
1818 "ide device [dev] - show or set current device\n"
1819 "ide part [dev] - print partition table of one or all IDE devices\n"
1820 "ide read addr blk# cnt\n"
1821 "ide write addr blk# cnt - read/write `cnt'"
1822 " blocks starting at block `blk#'\n"
1823 " to/from memory address `addr'\n"
1824);
1825
0d498393
WD
1826U_BOOT_CMD(
1827 diskboot, 3, 1, do_diskboot,
8bde7f77
WD
1828 "diskboot- boot from IDE device\n",
1829 "loadAddr dev:part\n"
1830);
1831
c609719b 1832#endif /* CONFIG_COMMANDS & CFG_CMD_IDE */