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