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