blob: f1b24be684f5f58769dcfa5727b2ea1af4f3670f [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
wdenk591dda52002-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
wdenk452cfd62002-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
wdenk591dda52002-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)])
wdenk591dda52002-11-18 00:14:45 +0000123#endif
wdenkc6097192002-11-03 00:24:07 +0000124
wdenk452cfd62002-11-19 11:04:11 +0000125#ifndef CONFIG_AMIGAONEG3SE
wdenkc6097192002-11-03 00:24:07 +0000126static int ide_bus_ok[CFG_IDE_MAXBUS];
wdenk452cfd62002-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
wdenk90e7e422002-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
wdenk90e7e422002-12-04 23:39:58 +0000138extern void ide_led (uchar led, uchar status);
139#endif
140#else
wdenk452cfd62002-11-19 11:04:11 +0000141#ifndef CONFIG_AMIGAONEG3SE
wdenkc6097192002-11-03 00:24:07 +0000142#define ide_led(a,b) /* dummy */
wdenk452cfd62002-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
wdenk591dda52002-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);
wdenk591dda52002-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 }
wdenkc08f1582003-04-27 22:52:51 +0000407 if ((strncmp(info.type, BOOT_PART_TYPE, sizeof(info.type)) != 0) &&
408 (strncmp(info.type, BOOT_PART_COMP, sizeof(info.type)) != 0)) {
wdenkc6097192002-11-03 00:24:07 +0000409 printf ("\n** Invalid partition type \"%.32s\""
410 " (expect \"" BOOT_PART_TYPE "\")\n",
411 info.type);
412 SHOW_BOOT_PROGRESS (-1);
413 return 1;
414 }
415
416 printf ("\nLoading from IDE device %d, partition %d: "
417 "Name: %.32s Type: %.32s\n",
418 dev, part, info.name, info.type);
419
420 PRINTF ("First Block: %ld, # of blocks: %ld, Block Size: %ld\n",
421 info.start, info.size, info.blksz);
422
423 if (ide_dev_desc[dev].block_read (dev, info.start, 1, (ulong *)addr) != 1) {
424 printf ("** Read error on %d:%d\n", dev, part);
425 SHOW_BOOT_PROGRESS (-1);
426 return 1;
427 }
428
429 hdr = (image_header_t *)addr;
430
431 if (ntohl(hdr->ih_magic) == IH_MAGIC) {
432
433 print_image_hdr (hdr);
434
435 cnt = (ntohl(hdr->ih_size) + sizeof(image_header_t));
436 cnt += info.blksz - 1;
437 cnt /= info.blksz;
438 cnt -= 1;
439 } else {
440 printf("\n** Bad Magic Number **\n");
441 SHOW_BOOT_PROGRESS (-1);
442 return 1;
443 }
444
445 if (ide_dev_desc[dev].block_read (dev, info.start+1, cnt,
446 (ulong *)(addr+info.blksz)) != cnt) {
447 printf ("** Read error on %d:%d\n", dev, part);
448 SHOW_BOOT_PROGRESS (-1);
449 return 1;
450 }
451
452
453 /* Loading ok, update default load address */
454
455 load_addr = addr;
456
457 /* Check if we should attempt an auto-start */
458 if (((ep = getenv("autostart")) != NULL) && (strcmp(ep,"yes") == 0)) {
459 char *local_args[2];
460 extern int do_bootm (cmd_tbl_t *, int, int, char *[]);
461
462 local_args[0] = argv[0];
463 local_args[1] = NULL;
464
465 printf ("Automatic boot of image at addr 0x%08lX ...\n", addr);
466
467 do_bootm (cmdtp, 0, 1, local_args);
468 rcode = 1;
469 }
470 return rcode;
471}
472
473/* ------------------------------------------------------------------------- */
474
475void ide_init (void)
476{
477 DECLARE_GLOBAL_DATA_PTR;
478
479#ifdef CONFIG_IDE_8xx_DIRECT
480 volatile immap_t *immr = (immap_t *)CFG_IMMR;
481 volatile pcmconf8xx_t *pcmp = &(immr->im_pcmcia);
482#endif
483 unsigned char c;
484 int i, bus;
wdenk452cfd62002-11-19 11:04:11 +0000485#ifdef CONFIG_AMIGAONEG3SE
486 unsigned int max_bus_scan;
487 unsigned int ata_reset_time;
488 char *s;
489#endif
wdenkc6097192002-11-03 00:24:07 +0000490
491#ifdef CONFIG_IDE_8xx_PCCARD
492 extern int pcmcia_on (void);
wdenk4fc95692003-02-28 00:49:47 +0000493 extern int ide_devices_found; /* Initialized in check_ide_device() */
wdenkc6097192002-11-03 00:24:07 +0000494
495 WATCHDOG_RESET();
496
wdenk4fc95692003-02-28 00:49:47 +0000497 ide_devices_found = 0;
wdenkc6097192002-11-03 00:24:07 +0000498 /* initialize the PCMCIA IDE adapter card */
wdenk4fc95692003-02-28 00:49:47 +0000499 pcmcia_on();
500 if (!ide_devices_found)
wdenkc6097192002-11-03 00:24:07 +0000501 return;
502 udelay (1000000); /* 1 s */
503#endif /* CONFIG_IDE_8xx_PCCARD */
504
505 WATCHDOG_RESET();
506
507 /* Initialize PIO timing tables */
508 for (i=0; i <= IDE_MAX_PIO_MODE; ++i) {
509 pio_config_clk[i].t_setup = PCMCIA_MK_CLKS(pio_config_ns[i].t_setup,
510 gd->bus_clk);
511 pio_config_clk[i].t_length = PCMCIA_MK_CLKS(pio_config_ns[i].t_length,
512 gd->bus_clk);
513 pio_config_clk[i].t_hold = PCMCIA_MK_CLKS(pio_config_ns[i].t_hold,
514 gd->bus_clk);
515 PRINTF ("PIO Mode %d: setup=%2d ns/%d clk"
516 " len=%3d ns/%d clk"
517 " hold=%2d ns/%d clk\n",
518 i,
519 pio_config_ns[i].t_setup, pio_config_clk[i].t_setup,
520 pio_config_ns[i].t_length, pio_config_clk[i].t_length,
521 pio_config_ns[i].t_hold, pio_config_clk[i].t_hold);
522 }
523
524 /* Reset the IDE just to be sure.
525 * Light LED's to show
526 */
527 ide_led ((LED_IDE1 | LED_IDE2), 1); /* LED's on */
528 ide_reset (); /* ATAPI Drives seems to need a proper IDE Reset */
529
530#ifdef CONFIG_IDE_8xx_DIRECT
531 /* PCMCIA / IDE initialization for common mem space */
532 pcmp->pcmc_pgcrb = 0;
533#endif
534
535 /* start in PIO mode 0 - most relaxed timings */
536 pio_mode = 0;
537 set_pcmcia_timing (pio_mode);
538
539 /*
540 * Wait for IDE to get ready.
541 * According to spec, this can take up to 31 seconds!
542 */
wdenk452cfd62002-11-19 11:04:11 +0000543#ifndef CONFIG_AMIGAONEG3SE
wdenkc6097192002-11-03 00:24:07 +0000544 for (bus=0; bus<CFG_IDE_MAXBUS; ++bus) {
545 int dev = bus * (CFG_IDE_MAXDEVICE / CFG_IDE_MAXBUS);
wdenk452cfd62002-11-19 11:04:11 +0000546#else
547 s = getenv("ide_maxbus");
548 if (s)
549 max_bus_scan = simple_strtol(s, NULL, 10);
550 else
551 max_bus_scan = CFG_IDE_MAXBUS;
552
553 for (bus=0; bus<max_bus_scan; ++bus) {
554 int dev = bus * (CFG_IDE_MAXDEVICE / max_bus_scan);
555#endif
wdenkc6097192002-11-03 00:24:07 +0000556
wdenk4fc95692003-02-28 00:49:47 +0000557#ifdef CONFIG_IDE_8xx_PCCARD
558 /* Skip non-ide devices from probing */
559 if ((ide_devices_found & (1 << bus)) == 0) {
560 ide_led ((LED_IDE1 | LED_IDE2), 0); /* LED's off */
561 continue;
562 }
563#endif
wdenkc6097192002-11-03 00:24:07 +0000564 printf ("Bus %d: ", bus);
565
566 ide_bus_ok[bus] = 0;
567
568 /* Select device
569 */
570 udelay (100000); /* 100 ms */
wdenk591dda52002-11-18 00:14:45 +0000571 ide_outb (dev, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(dev));
wdenkc6097192002-11-03 00:24:07 +0000572 udelay (100000); /* 100 ms */
wdenk452cfd62002-11-19 11:04:11 +0000573#ifdef CONFIG_AMIGAONEG3SE
574 ata_reset_time = ATA_RESET_TIME;
575 s = getenv("ide_reset_timeout");
576 if (s) ata_reset_time = 2*simple_strtol(s, NULL, 10);
577#endif
wdenkc6097192002-11-03 00:24:07 +0000578 i = 0;
579 do {
580 udelay (10000); /* 10 ms */
581
wdenk591dda52002-11-18 00:14:45 +0000582 c = ide_inb (dev, ATA_STATUS);
wdenkc6097192002-11-03 00:24:07 +0000583 i++;
wdenk452cfd62002-11-19 11:04:11 +0000584#ifdef CONFIG_AMIGAONEG3SE
585 if (i > (ata_reset_time * 100)) {
586#else
wdenkc6097192002-11-03 00:24:07 +0000587 if (i > (ATA_RESET_TIME * 100)) {
wdenk452cfd62002-11-19 11:04:11 +0000588#endif
wdenkc6097192002-11-03 00:24:07 +0000589 puts ("** Timeout **\n");
590 ide_led ((LED_IDE1 | LED_IDE2), 0); /* LED's off */
wdenk452cfd62002-11-19 11:04:11 +0000591#ifdef CONFIG_AMIGAONEG3SE
592 /* If this is the second bus, the first one was OK */
593 if (bus != 0)
594 {
595 ide_bus_ok[bus] = 0;
596 goto skip_bus;
597 }
598#endif
wdenkc6097192002-11-03 00:24:07 +0000599 return;
600 }
601 if ((i >= 100) && ((i%100)==0)) {
602 putc ('.');
603 }
604 } while (c & ATA_STAT_BUSY);
605
606 if (c & (ATA_STAT_BUSY | ATA_STAT_FAULT)) {
607 puts ("not available ");
608 PRINTF ("Status = 0x%02X ", c);
609#ifndef CONFIG_ATAPI /* ATAPI Devices do not set DRDY */
610 } else if ((c & ATA_STAT_READY) == 0) {
611 puts ("not available ");
612 PRINTF ("Status = 0x%02X ", c);
613#endif
614 } else {
615 puts ("OK ");
616 ide_bus_ok[bus] = 1;
617 }
618 WATCHDOG_RESET();
619 }
wdenk452cfd62002-11-19 11:04:11 +0000620
621#ifdef CONFIG_AMIGAONEG3SE
622 skip_bus:
623#endif
wdenkc6097192002-11-03 00:24:07 +0000624 putc ('\n');
625
626 ide_led ((LED_IDE1 | LED_IDE2), 0); /* LED's off */
627
628 curr_device = -1;
629 for (i=0; i<CFG_IDE_MAXDEVICE; ++i) {
630#ifdef CONFIG_IDE_LED
631 int led = (IDE_BUS(i) == 0) ? LED_IDE1 : LED_IDE2;
632#endif
633 ide_dev_desc[i].if_type=IF_TYPE_IDE;
634 ide_dev_desc[i].dev=i;
635 ide_dev_desc[i].part_type=PART_TYPE_UNKNOWN;
636 ide_dev_desc[i].blksz=0;
637 ide_dev_desc[i].lba=0;
638 ide_dev_desc[i].block_read=ide_read;
639 if (!ide_bus_ok[IDE_BUS(i)])
640 continue;
641 ide_led (led, 1); /* LED on */
642 ide_ident(&ide_dev_desc[i]);
643 ide_led (led, 0); /* LED off */
644 dev_print(&ide_dev_desc[i]);
645/* ide_print (i); */
646 if ((ide_dev_desc[i].lba > 0) && (ide_dev_desc[i].blksz > 0)) {
647 init_part (&ide_dev_desc[i]); /* initialize partition type */
648 if (curr_device < 0)
649 curr_device = i;
650 }
651 }
652 WATCHDOG_RESET();
653}
654
655/* ------------------------------------------------------------------------- */
656
657block_dev_desc_t * ide_get_dev(int dev)
658{
659 return ((block_dev_desc_t *)&ide_dev_desc[dev]);
660}
661
662
663#ifdef CONFIG_IDE_8xx_DIRECT
664
665static void
666set_pcmcia_timing (int pmode)
667{
668 volatile immap_t *immr = (immap_t *)CFG_IMMR;
669 volatile pcmconf8xx_t *pcmp = &(immr->im_pcmcia);
670 ulong timings;
671
672 PRINTF ("Set timing for PIO Mode %d\n", pmode);
673
674 timings = PCMCIA_SHT(pio_config_clk[pmode].t_hold)
675 | PCMCIA_SST(pio_config_clk[pmode].t_setup)
676 | PCMCIA_SL (pio_config_clk[pmode].t_length)
677 ;
678
679 /* IDE 0
680 */
681 pcmp->pcmc_pbr0 = CFG_PCMCIA_PBR0;
682 pcmp->pcmc_por0 = CFG_PCMCIA_POR0
683#if (CFG_PCMCIA_POR0 != 0)
684 | timings
685#endif
686 ;
687 PRINTF ("PBR0: %08x POR0: %08x\n", pcmp->pcmc_pbr0, pcmp->pcmc_por0);
688
689 pcmp->pcmc_pbr1 = CFG_PCMCIA_PBR1;
690 pcmp->pcmc_por1 = CFG_PCMCIA_POR1
691#if (CFG_PCMCIA_POR1 != 0)
692 | timings
693#endif
694 ;
695 PRINTF ("PBR1: %08x POR1: %08x\n", pcmp->pcmc_pbr1, pcmp->pcmc_por1);
696
697 pcmp->pcmc_pbr2 = CFG_PCMCIA_PBR2;
698 pcmp->pcmc_por2 = CFG_PCMCIA_POR2
699#if (CFG_PCMCIA_POR2 != 0)
700 | timings
701#endif
702 ;
703 PRINTF ("PBR2: %08x POR2: %08x\n", pcmp->pcmc_pbr2, pcmp->pcmc_por2);
704
705 pcmp->pcmc_pbr3 = CFG_PCMCIA_PBR3;
706 pcmp->pcmc_por3 = CFG_PCMCIA_POR3
707#if (CFG_PCMCIA_POR3 != 0)
708 | timings
709#endif
710 ;
711 PRINTF ("PBR3: %08x POR3: %08x\n", pcmp->pcmc_pbr3, pcmp->pcmc_por3);
712
713 /* IDE 1
714 */
715 pcmp->pcmc_pbr4 = CFG_PCMCIA_PBR4;
716 pcmp->pcmc_por4 = CFG_PCMCIA_POR4
717#if (CFG_PCMCIA_POR4 != 0)
718 | timings
719#endif
720 ;
721 PRINTF ("PBR4: %08x POR4: %08x\n", pcmp->pcmc_pbr4, pcmp->pcmc_por4);
722
723 pcmp->pcmc_pbr5 = CFG_PCMCIA_PBR5;
724 pcmp->pcmc_por5 = CFG_PCMCIA_POR5
725#if (CFG_PCMCIA_POR5 != 0)
726 | timings
727#endif
728 ;
729 PRINTF ("PBR5: %08x POR5: %08x\n", pcmp->pcmc_pbr5, pcmp->pcmc_por5);
730
731 pcmp->pcmc_pbr6 = CFG_PCMCIA_PBR6;
732 pcmp->pcmc_por6 = CFG_PCMCIA_POR6
733#if (CFG_PCMCIA_POR6 != 0)
734 | timings
735#endif
736 ;
737 PRINTF ("PBR6: %08x POR6: %08x\n", pcmp->pcmc_pbr6, pcmp->pcmc_por6);
738
739 pcmp->pcmc_pbr7 = CFG_PCMCIA_PBR7;
740 pcmp->pcmc_por7 = CFG_PCMCIA_POR7
741#if (CFG_PCMCIA_POR7 != 0)
742 | timings
743#endif
744 ;
745 PRINTF ("PBR7: %08x POR7: %08x\n", pcmp->pcmc_pbr7, pcmp->pcmc_por7);
746
747}
748
749#endif /* CONFIG_IDE_8xx_DIRECT */
750
751/* ------------------------------------------------------------------------- */
752
wdenk591dda52002-11-18 00:14:45 +0000753#ifdef __PPC__
wdenkc6097192002-11-03 00:24:07 +0000754static void __inline__
wdenk591dda52002-11-18 00:14:45 +0000755ide_outb(int dev, int port, unsigned char val)
wdenkc6097192002-11-03 00:24:07 +0000756{
757 /* Ensure I/O operations complete */
758 __asm__ volatile("eieio");
759 *((uchar *)(ATA_CURR_BASE(dev)+port)) = val;
760#if 0
wdenk591dda52002-11-18 00:14:45 +0000761 printf ("ide_outb: 0x%08lx <== 0x%02x\n", ATA_CURR_BASE(dev)+port, val);
wdenkc6097192002-11-03 00:24:07 +0000762#endif
763}
wdenk591dda52002-11-18 00:14:45 +0000764#else /* ! __PPC__ */
765static void __inline__
766ide_outb(int dev, int port, unsigned char val)
767{
768 outb(val, port);
769}
770#endif /* __PPC__ */
771
wdenkc6097192002-11-03 00:24:07 +0000772
wdenk591dda52002-11-18 00:14:45 +0000773#ifdef __PPC__
wdenkc6097192002-11-03 00:24:07 +0000774static unsigned char __inline__
wdenk591dda52002-11-18 00:14:45 +0000775ide_inb(int dev, int port)
wdenkc6097192002-11-03 00:24:07 +0000776{
777 uchar val;
778 /* Ensure I/O operations complete */
779 __asm__ volatile("eieio");
780 val = *((uchar *)(ATA_CURR_BASE(dev)+port));
781#if 0
wdenk591dda52002-11-18 00:14:45 +0000782 printf ("ide_inb: 0x%08lx ==> 0x%02x\n", ATA_CURR_BASE(dev)+port, val);
wdenkc6097192002-11-03 00:24:07 +0000783#endif
784 return (val);
785}
wdenk591dda52002-11-18 00:14:45 +0000786#else /* ! __PPC__ */
787static unsigned char __inline__
788ide_inb(int dev, int port)
789{
790 return inb(port);
791}
792#endif /* __PPC__ */
wdenkc6097192002-11-03 00:24:07 +0000793
wdenk591dda52002-11-18 00:14:45 +0000794#ifdef __PPC__
wdenk88d2ba92003-06-23 18:12:28 +0000795# ifdef CONFIG_AMIGAONEG3SE
wdenk452cfd62002-11-19 11:04:11 +0000796static void
797output_data_short(int dev, ulong *sect_buf, int words)
798{
799 ushort *dbuf;
800 volatile ushort *pbuf;
801
802 pbuf = (ushort *)(ATA_CURR_BASE(dev)+ATA_DATA_REG);
803 dbuf = (ushort *)sect_buf;
804 while (words--) {
805 __asm__ volatile ("eieio");
806 *pbuf = *dbuf++;
807 __asm__ volatile ("eieio");
808 }
809
810 if (words&1)
811 *pbuf = 0;
812}
wdenk88d2ba92003-06-23 18:12:28 +0000813# endif /* CONFIG_AMIGAONEG3SE */
wdenk452cfd62002-11-19 11:04:11 +0000814
wdenkc6097192002-11-03 00:24:07 +0000815static void
816input_swap_data(int dev, ulong *sect_buf, int words)
817{
818 volatile ushort *pbuf = (ushort *)(ATA_CURR_BASE(dev)+ATA_DATA_REG);
819 ushort *dbuf = (ushort *)sect_buf;
820
821 while (words--) {
822 *dbuf++ = ld_le16(pbuf);
823 *dbuf++ = ld_le16(pbuf);
824 }
825}
wdenk591dda52002-11-18 00:14:45 +0000826#else /* ! __PPC__ */
827#define input_swap_data(x,y,z) input_data(x,y,z)
828#endif /* __PPC__ */
829
830
831
wdenkc6097192002-11-03 00:24:07 +0000832
wdenk591dda52002-11-18 00:14:45 +0000833#ifdef __PPC__
wdenkc6097192002-11-03 00:24:07 +0000834static void
835output_data(int dev, ulong *sect_buf, int words)
836{
837 ushort *dbuf;
838 volatile ushort *pbuf;
839
840 pbuf = (ushort *)(ATA_CURR_BASE(dev)+ATA_DATA_REG);
841 dbuf = (ushort *)sect_buf;
842 while (words--) {
843 __asm__ volatile ("eieio");
844 *pbuf = *dbuf++;
845 __asm__ volatile ("eieio");
846 *pbuf = *dbuf++;
847 }
848}
wdenk591dda52002-11-18 00:14:45 +0000849#else /* ! __PPC__ */
850static void
851output_data(int dev, ulong *sect_buf, int words)
852{
853 outsw(ATA_DATA_REG, sect_buf, words<<1);
854}
855#endif /* __PPC__ */
wdenkc6097192002-11-03 00:24:07 +0000856
wdenk591dda52002-11-18 00:14:45 +0000857#ifdef __PPC__
wdenkc6097192002-11-03 00:24:07 +0000858static void
859input_data(int dev, ulong *sect_buf, int words)
860{
861 ushort *dbuf;
862 volatile ushort *pbuf;
863
864 pbuf = (ushort *)(ATA_CURR_BASE(dev)+ATA_DATA_REG);
865 dbuf = (ushort *)sect_buf;
866 while (words--) {
867 __asm__ volatile ("eieio");
868 *dbuf++ = *pbuf;
869 __asm__ volatile ("eieio");
870 *dbuf++ = *pbuf;
871 }
872}
wdenk591dda52002-11-18 00:14:45 +0000873#else /* ! __PPC__ */
874static void
875input_data(int dev, ulong *sect_buf, int words)
876{
877 insw(ATA_DATA_REG, sect_buf, words << 1);
878}
879
880#endif /* __PPC__ */
wdenkc6097192002-11-03 00:24:07 +0000881
wdenk452cfd62002-11-19 11:04:11 +0000882#ifdef CONFIG_AMIGAONEG3SE
883static void
884input_data_short(int dev, ulong *sect_buf, int words)
885{
886 ushort *dbuf;
887 volatile ushort *pbuf;
888
889 pbuf = (ushort *)(ATA_CURR_BASE(dev)+ATA_DATA_REG);
890 dbuf = (ushort *)sect_buf;
891 while (words--) {
892 __asm__ volatile ("eieio");
893 *dbuf++ = *pbuf;
894 __asm__ volatile ("eieio");
895 }
896
897 if (words&1)
898 {
899 ushort dummy;
900 dummy = *pbuf;
901 }
902}
903#endif
904
wdenkc6097192002-11-03 00:24:07 +0000905/* -------------------------------------------------------------------------
906 */
907static void ide_ident (block_dev_desc_t *dev_desc)
908{
909 ulong iobuf[ATA_SECTORWORDS];
910 unsigned char c;
911 hd_driveid_t *iop = (hd_driveid_t *)iobuf;
912
wdenk452cfd62002-11-19 11:04:11 +0000913#ifdef CONFIG_AMIGAONEG3SE
914 int max_bus_scan;
915 int retries = 0;
916 char *s;
917 int do_retry = 0;
918#endif
919
wdenkc6097192002-11-03 00:24:07 +0000920#if 0
921 int mode, cycle_time;
922#endif
923 int device;
924 device=dev_desc->dev;
925 printf (" Device %d: ", device);
926
wdenk452cfd62002-11-19 11:04:11 +0000927#ifdef CONFIG_AMIGAONEG3SE
928 s = getenv("ide_maxbus");
929 if (s) {
930 max_bus_scan = simple_strtol(s, NULL, 10);
931 } else {
932 max_bus_scan = CFG_IDE_MAXBUS;
933 }
934 if (device >= max_bus_scan*2) {
935 dev_desc->type=DEV_TYPE_UNKNOWN;
936 return;
937 }
938#endif
939
wdenkc6097192002-11-03 00:24:07 +0000940 ide_led (DEVICE_LED(device), 1); /* LED on */
941 /* Select device
942 */
wdenk591dda52002-11-18 00:14:45 +0000943 ide_outb (device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
wdenkc6097192002-11-03 00:24:07 +0000944 dev_desc->if_type=IF_TYPE_IDE;
945#ifdef CONFIG_ATAPI
wdenk452cfd62002-11-19 11:04:11 +0000946
947#ifdef CONFIG_AMIGAONEG3SE
948 do_retry = 0;
949 retries = 0;
950
951 /* Warning: This will be tricky to read */
952 while (retries <= 1)
953 {
954#endif /* CONFIG_AMIGAONEG3SE */
955
wdenkc6097192002-11-03 00:24:07 +0000956 /* check signature */
wdenk591dda52002-11-18 00:14:45 +0000957 if ((ide_inb(device,ATA_SECT_CNT) == 0x01) &&
958 (ide_inb(device,ATA_SECT_NUM) == 0x01) &&
959 (ide_inb(device,ATA_CYL_LOW) == 0x14) &&
960 (ide_inb(device,ATA_CYL_HIGH) == 0xEB)) {
wdenkc6097192002-11-03 00:24:07 +0000961 /* ATAPI Signature found */
962 dev_desc->if_type=IF_TYPE_ATAPI;
963 /* Start Ident Command
964 */
wdenk591dda52002-11-18 00:14:45 +0000965 ide_outb (device, ATA_COMMAND, ATAPI_CMD_IDENT);
wdenkc6097192002-11-03 00:24:07 +0000966 /*
967 * Wait for completion - ATAPI devices need more time
968 * to become ready
969 */
970 c = ide_wait (device, ATAPI_TIME_OUT);
971 }
972 else
973#endif
974 {
975 /* Start Ident Command
976 */
wdenk591dda52002-11-18 00:14:45 +0000977 ide_outb (device, ATA_COMMAND, ATA_CMD_IDENT);
wdenkc6097192002-11-03 00:24:07 +0000978
979 /* Wait for completion
980 */
981 c = ide_wait (device, IDE_TIME_OUT);
982 }
983 ide_led (DEVICE_LED(device), 0); /* LED off */
984
985 if (((c & ATA_STAT_DRQ) == 0) ||
986 ((c & (ATA_STAT_FAULT|ATA_STAT_ERR)) != 0) ) {
wdenk452cfd62002-11-19 11:04:11 +0000987#ifdef CONFIG_AMIGAONEG3SE
988 if (retries == 0) {
989 do_retry = 1;
990 } else {
991 dev_desc->type=DEV_TYPE_UNKNOWN;
992 return;
993 }
994#else
wdenkc6097192002-11-03 00:24:07 +0000995 dev_desc->type=DEV_TYPE_UNKNOWN;
996 return;
wdenk452cfd62002-11-19 11:04:11 +0000997#endif /* CONFIG_AMIGAONEG3SE */
998 }
999
1000#ifdef CONFIG_AMIGAONEG3SE
1001 s = getenv("ide_doreset");
1002 if (s && strcmp(s, "on") == 0 && 1 == do_retry) {
1003 /* Need to soft reset the device in case it's an ATAPI... */
1004 PRINTF("Retrying...\n");
1005 ide_outb (device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
1006 udelay(100000);
1007 ide_outb (device, ATA_COMMAND, 0x08);
1008 udelay (100000); /* 100 ms */
1009 retries++;
1010 } else {
1011 retries = 100;
wdenkc6097192002-11-03 00:24:07 +00001012 }
wdenk452cfd62002-11-19 11:04:11 +00001013 } /* see above - ugly to read */
1014#endif /* CONFIG_AMIGAONEG3SE */
wdenkc6097192002-11-03 00:24:07 +00001015
1016 input_swap_data (device, iobuf, ATA_SECTORWORDS);
1017
1018 ident_cpy (dev_desc->revision, iop->fw_rev, sizeof(dev_desc->revision));
1019 ident_cpy (dev_desc->vendor, iop->model, sizeof(dev_desc->vendor));
1020 ident_cpy (dev_desc->product, iop->serial_no, sizeof(dev_desc->product));
1021
1022 if ((iop->config & 0x0080)==0x0080)
1023 dev_desc->removable = 1;
1024 else
1025 dev_desc->removable = 0;
1026
1027#if 0
1028 /*
1029 * Drive PIO mode autoselection
1030 */
1031 mode = iop->tPIO;
1032
1033 printf ("tPIO = 0x%02x = %d\n",mode, mode);
1034 if (mode > 2) { /* 2 is maximum allowed tPIO value */
1035 mode = 2;
1036 PRINTF ("Override tPIO -> 2\n");
1037 }
1038 if (iop->field_valid & 2) { /* drive implements ATA2? */
1039 PRINTF ("Drive implements ATA2\n");
1040 if (iop->capability & 8) { /* drive supports use_iordy? */
1041 cycle_time = iop->eide_pio_iordy;
1042 } else {
1043 cycle_time = iop->eide_pio;
1044 }
1045 PRINTF ("cycle time = %d\n", cycle_time);
1046 mode = 4;
1047 if (cycle_time > 120) mode = 3; /* 120 ns for PIO mode 4 */
1048 if (cycle_time > 180) mode = 2; /* 180 ns for PIO mode 3 */
1049 if (cycle_time > 240) mode = 1; /* 240 ns for PIO mode 4 */
1050 if (cycle_time > 383) mode = 0; /* 383 ns for PIO mode 4 */
1051 }
1052 printf ("PIO mode to use: PIO %d\n", mode);
1053#endif /* 0 */
1054
1055#ifdef CONFIG_ATAPI
1056 if (dev_desc->if_type==IF_TYPE_ATAPI) {
1057 atapi_inquiry(dev_desc);
1058 return;
1059 }
1060#endif /* CONFIG_ATAPI */
1061
1062 /* swap shorts */
1063 dev_desc->lba = (iop->lba_capacity << 16) | (iop->lba_capacity >> 16);
1064 /* assuming HD */
1065 dev_desc->type=DEV_TYPE_HARDDISK;
1066 dev_desc->blksz=ATA_BLOCKSIZE;
1067 dev_desc->lun=0; /* just to fill something in... */
1068
1069#if 0 /* only used to test the powersaving mode,
1070 * if enabled, the drive goes after 5 sec
1071 * in standby mode */
wdenk591dda52002-11-18 00:14:45 +00001072 ide_outb (device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
wdenkc6097192002-11-03 00:24:07 +00001073 c = ide_wait (device, IDE_TIME_OUT);
wdenk591dda52002-11-18 00:14:45 +00001074 ide_outb (device, ATA_SECT_CNT, 1);
1075 ide_outb (device, ATA_LBA_LOW, 0);
1076 ide_outb (device, ATA_LBA_MID, 0);
1077 ide_outb (device, ATA_LBA_HIGH, 0);
1078 ide_outb (device, ATA_DEV_HD, ATA_LBA |
wdenkc6097192002-11-03 00:24:07 +00001079 ATA_DEVICE(device));
wdenk591dda52002-11-18 00:14:45 +00001080 ide_outb (device, ATA_COMMAND, 0xe3);
wdenkc6097192002-11-03 00:24:07 +00001081 udelay (50);
1082 c = ide_wait (device, IDE_TIME_OUT); /* can't take over 500 ms */
1083#endif
1084}
1085
1086
1087/* ------------------------------------------------------------------------- */
1088
1089ulong ide_read (int device, ulong blknr, ulong blkcnt, ulong *buffer)
1090{
1091 ulong n = 0;
1092 unsigned char c;
1093 unsigned char pwrsave=0; /* power save */
1094
1095 PRINTF ("ide_read dev %d start %lX, blocks %lX buffer at %lX\n",
1096 device, blknr, blkcnt, (ulong)buffer);
1097
1098 ide_led (DEVICE_LED(device), 1); /* LED on */
1099
1100 /* Select device
1101 */
wdenk591dda52002-11-18 00:14:45 +00001102 ide_outb (device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
wdenkc6097192002-11-03 00:24:07 +00001103 c = ide_wait (device, IDE_TIME_OUT);
1104
1105 if (c & ATA_STAT_BUSY) {
1106 printf ("IDE read: device %d not ready\n", device);
1107 goto IDE_READ_E;
1108 }
1109
1110 /* first check if the drive is in Powersaving mode, if yes,
1111 * increase the timeout value */
wdenk591dda52002-11-18 00:14:45 +00001112 ide_outb (device, ATA_COMMAND, ATA_CMD_CHK_PWR);
wdenkc6097192002-11-03 00:24:07 +00001113 udelay (50);
1114
1115 c = ide_wait (device, IDE_TIME_OUT); /* can't take over 500 ms */
1116
1117 if (c & ATA_STAT_BUSY) {
1118 printf ("IDE read: device %d not ready\n", device);
1119 goto IDE_READ_E;
1120 }
1121 if ((c & ATA_STAT_ERR) == ATA_STAT_ERR) {
1122 printf ("No Powersaving mode %X\n", c);
1123 } else {
wdenk591dda52002-11-18 00:14:45 +00001124 c = ide_inb(device,ATA_SECT_CNT);
wdenkc6097192002-11-03 00:24:07 +00001125 PRINTF("Powersaving %02X\n",c);
1126 if(c==0)
1127 pwrsave=1;
1128 }
1129
1130
1131 while (blkcnt-- > 0) {
1132
1133 c = ide_wait (device, IDE_TIME_OUT);
1134
1135 if (c & ATA_STAT_BUSY) {
1136 printf ("IDE read: device %d not ready\n", device);
1137 break;
1138 }
1139
wdenk591dda52002-11-18 00:14:45 +00001140 ide_outb (device, ATA_SECT_CNT, 1);
1141 ide_outb (device, ATA_LBA_LOW, (blknr >> 0) & 0xFF);
1142 ide_outb (device, ATA_LBA_MID, (blknr >> 8) & 0xFF);
1143 ide_outb (device, ATA_LBA_HIGH, (blknr >> 16) & 0xFF);
1144 ide_outb (device, ATA_DEV_HD, ATA_LBA |
wdenkc6097192002-11-03 00:24:07 +00001145 ATA_DEVICE(device) |
1146 ((blknr >> 24) & 0xF) );
wdenk591dda52002-11-18 00:14:45 +00001147 ide_outb (device, ATA_COMMAND, ATA_CMD_READ);
wdenkc6097192002-11-03 00:24:07 +00001148
1149 udelay (50);
1150
1151 if(pwrsave) {
1152 c = ide_wait (device, IDE_SPIN_UP_TIME_OUT); /* may take up to 4 sec */
1153 pwrsave=0;
1154 } else {
1155 c = ide_wait (device, IDE_TIME_OUT); /* can't take over 500 ms */
1156 }
1157
1158 if ((c&(ATA_STAT_DRQ|ATA_STAT_BUSY|ATA_STAT_ERR)) != ATA_STAT_DRQ) {
1159 printf ("Error (no IRQ) dev %d blk %ld: status 0x%02x\n",
1160 device, blknr, c);
1161 break;
1162 }
1163
1164 input_data (device, buffer, ATA_SECTORWORDS);
wdenk591dda52002-11-18 00:14:45 +00001165 (void) ide_inb (device, ATA_STATUS); /* clear IRQ */
wdenkc6097192002-11-03 00:24:07 +00001166
1167 ++n;
1168 ++blknr;
1169 buffer += ATA_SECTORWORDS;
1170 }
1171IDE_READ_E:
1172 ide_led (DEVICE_LED(device), 0); /* LED off */
1173 return (n);
1174}
1175
1176/* ------------------------------------------------------------------------- */
1177
1178
1179ulong ide_write (int device, ulong blknr, ulong blkcnt, ulong *buffer)
1180{
1181 ulong n = 0;
1182 unsigned char c;
1183
1184 ide_led (DEVICE_LED(device), 1); /* LED on */
1185
1186 /* Select device
1187 */
wdenk591dda52002-11-18 00:14:45 +00001188 ide_outb (device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
wdenkc6097192002-11-03 00:24:07 +00001189
1190 while (blkcnt-- > 0) {
1191
1192 c = ide_wait (device, IDE_TIME_OUT);
1193
1194 if (c & ATA_STAT_BUSY) {
1195 printf ("IDE read: device %d not ready\n", device);
1196 goto WR_OUT;
1197 }
1198
wdenk591dda52002-11-18 00:14:45 +00001199 ide_outb (device, ATA_SECT_CNT, 1);
1200 ide_outb (device, ATA_LBA_LOW, (blknr >> 0) & 0xFF);
1201 ide_outb (device, ATA_LBA_MID, (blknr >> 8) & 0xFF);
1202 ide_outb (device, ATA_LBA_HIGH, (blknr >> 16) & 0xFF);
1203 ide_outb (device, ATA_DEV_HD, ATA_LBA |
wdenkc6097192002-11-03 00:24:07 +00001204 ATA_DEVICE(device) |
1205 ((blknr >> 24) & 0xF) );
wdenk591dda52002-11-18 00:14:45 +00001206 ide_outb (device, ATA_COMMAND, ATA_CMD_WRITE);
wdenkc6097192002-11-03 00:24:07 +00001207
1208 udelay (50);
1209
1210 c = ide_wait (device, IDE_TIME_OUT); /* can't take over 500 ms */
1211
1212 if ((c&(ATA_STAT_DRQ|ATA_STAT_BUSY|ATA_STAT_ERR)) != ATA_STAT_DRQ) {
1213 printf ("Error (no IRQ) dev %d blk %ld: status 0x%02x\n",
1214 device, blknr, c);
1215 goto WR_OUT;
1216 }
1217
1218 output_data (device, buffer, ATA_SECTORWORDS);
wdenk591dda52002-11-18 00:14:45 +00001219 c = ide_inb (device, ATA_STATUS); /* clear IRQ */
wdenkc6097192002-11-03 00:24:07 +00001220 ++n;
1221 ++blknr;
1222 buffer += ATA_SECTORWORDS;
1223 }
1224WR_OUT:
1225 ide_led (DEVICE_LED(device), 0); /* LED off */
1226 return (n);
1227}
1228
1229/* ------------------------------------------------------------------------- */
1230
1231/*
1232 * copy src to dest, skipping leading and trailing blanks and null
1233 * terminate the string
1234 */
1235static void ident_cpy (unsigned char *dest, unsigned char *src, unsigned int len)
1236{
1237 int start,end;
1238
1239 start=0;
1240 while (start<len) {
1241 if (src[start]!=' ')
1242 break;
1243 start++;
1244 }
1245 end=len-1;
1246 while (end>start) {
1247 if (src[end]!=' ')
1248 break;
1249 end--;
1250 }
1251 for ( ; start<=end; start++) {
1252 *dest++=src[start];
1253 }
1254 *dest='\0';
1255}
1256
1257/* ------------------------------------------------------------------------- */
1258
1259/*
1260 * Wait until Busy bit is off, or timeout (in ms)
1261 * Return last status
1262 */
1263static uchar ide_wait (int dev, ulong t)
1264{
1265 ulong delay = 10 * t; /* poll every 100 us */
1266 uchar c;
1267
wdenk591dda52002-11-18 00:14:45 +00001268 while ((c = ide_inb(dev, ATA_STATUS)) & ATA_STAT_BUSY) {
wdenkc6097192002-11-03 00:24:07 +00001269 udelay (100);
1270 if (delay-- == 0) {
1271 break;
1272 }
1273 }
1274 return (c);
1275}
1276
1277/* ------------------------------------------------------------------------- */
1278
1279#ifdef CONFIG_IDE_RESET
1280extern void ide_set_reset(int idereset);
1281
1282static void ide_reset (void)
1283{
1284#if defined(CFG_PB_12V_ENABLE) || defined(CFG_PB_IDE_MOTOR)
1285 volatile immap_t *immr = (immap_t *)CFG_IMMR;
1286#endif
1287 int i;
1288
1289 curr_device = -1;
1290 for (i=0; i<CFG_IDE_MAXBUS; ++i)
1291 ide_bus_ok[i] = 0;
1292 for (i=0; i<CFG_IDE_MAXDEVICE; ++i)
1293 ide_dev_desc[i].type = DEV_TYPE_UNKNOWN;
1294
1295 ide_set_reset (1); /* assert reset */
1296
1297 WATCHDOG_RESET();
1298
1299#ifdef CFG_PB_12V_ENABLE
1300 immr->im_cpm.cp_pbdat &= ~(CFG_PB_12V_ENABLE); /* 12V Enable output OFF */
1301 immr->im_cpm.cp_pbpar &= ~(CFG_PB_12V_ENABLE);
1302 immr->im_cpm.cp_pbodr &= ~(CFG_PB_12V_ENABLE);
1303 immr->im_cpm.cp_pbdir |= CFG_PB_12V_ENABLE;
1304
1305 /* wait 500 ms for the voltage to stabilize
1306 */
1307 for (i=0; i<500; ++i) {
1308 udelay (1000);
1309 }
1310
1311 immr->im_cpm.cp_pbdat |= CFG_PB_12V_ENABLE; /* 12V Enable output ON */
1312#endif /* CFG_PB_12V_ENABLE */
1313
1314#ifdef CFG_PB_IDE_MOTOR
1315 /* configure IDE Motor voltage monitor pin as input */
1316 immr->im_cpm.cp_pbpar &= ~(CFG_PB_IDE_MOTOR);
1317 immr->im_cpm.cp_pbodr &= ~(CFG_PB_IDE_MOTOR);
1318 immr->im_cpm.cp_pbdir &= ~(CFG_PB_IDE_MOTOR);
1319
1320 /* wait up to 1 s for the motor voltage to stabilize
1321 */
1322 for (i=0; i<1000; ++i) {
1323 if ((immr->im_cpm.cp_pbdat & CFG_PB_IDE_MOTOR) != 0) {
1324 break;
1325 }
1326 udelay (1000);
1327 }
1328
1329 if (i == 1000) { /* Timeout */
1330 printf ("\nWarning: 5V for IDE Motor missing\n");
1331# ifdef CONFIG_STATUS_LED
1332# ifdef STATUS_LED_YELLOW
1333 status_led_set (STATUS_LED_YELLOW, STATUS_LED_ON );
1334# endif
1335# ifdef STATUS_LED_GREEN
1336 status_led_set (STATUS_LED_GREEN, STATUS_LED_OFF);
1337# endif
1338# endif /* CONFIG_STATUS_LED */
1339 }
1340#endif /* CFG_PB_IDE_MOTOR */
1341
1342 WATCHDOG_RESET();
1343
1344 /* de-assert RESET signal */
1345 ide_set_reset(0);
1346
1347 /* wait 250 ms */
1348 for (i=0; i<250; ++i) {
1349 udelay (1000);
1350 }
1351}
1352
1353#endif /* CONFIG_IDE_RESET */
1354
1355/* ------------------------------------------------------------------------- */
1356
wdenk90e7e422002-12-04 23:39:58 +00001357#if defined(CONFIG_IDE_LED) && !defined(CONFIG_AMIGAONEG3SE) && !defined(CONFIG_KUP4K)
wdenkc6097192002-11-03 00:24:07 +00001358
1359static uchar led_buffer = 0; /* Buffer for current LED status */
1360
1361static void ide_led (uchar led, uchar status)
1362{
1363 uchar *led_port = LED_PORT;
1364
1365 if (status) { /* switch LED on */
1366 led_buffer |= led;
1367 } else { /* switch LED off */
1368 led_buffer &= ~led;
1369 }
1370
1371 *led_port = led_buffer;
1372}
1373
1374#endif /* CONFIG_IDE_LED */
1375
1376/* ------------------------------------------------------------------------- */
1377
1378#ifdef CONFIG_ATAPI
1379/****************************************************************************
1380 * ATAPI Support
1381 */
1382
1383
1384
1385#undef ATAPI_DEBUG
1386
1387#ifdef ATAPI_DEBUG
1388#define AT_PRINTF(fmt,args...) printf (fmt ,##args)
1389#else
1390#define AT_PRINTF(fmt,args...)
1391#endif
1392
wdenk591dda52002-11-18 00:14:45 +00001393#ifdef __PPC__
wdenkc6097192002-11-03 00:24:07 +00001394/* since ATAPI may use commands with not 4 bytes alligned length
1395 * we have our own transfer functions, 2 bytes alligned */
1396static void
1397output_data_shorts(int dev, ushort *sect_buf, int shorts)
1398{
1399 ushort *dbuf;
1400 volatile ushort *pbuf;
1401
1402 pbuf = (ushort *)(ATA_CURR_BASE(dev)+ATA_DATA_REG);
1403 dbuf = (ushort *)sect_buf;
1404 while (shorts--) {
1405 __asm__ volatile ("eieio");
1406 *pbuf = *dbuf++;
1407 }
1408}
1409
1410static void
1411input_data_shorts(int dev, ushort *sect_buf, int shorts)
1412{
1413 ushort *dbuf;
1414 volatile ushort *pbuf;
1415
1416 pbuf = (ushort *)(ATA_CURR_BASE(dev)+ATA_DATA_REG);
1417 dbuf = (ushort *)sect_buf;
1418 while (shorts--) {
1419 __asm__ volatile ("eieio");
1420 *dbuf++ = *pbuf;
1421 }
1422}
1423
wdenk591dda52002-11-18 00:14:45 +00001424#else /* ! __PPC__ */
1425static void
1426output_data_shorts(int dev, ushort *sect_buf, int shorts)
1427{
1428 outsw(ATA_DATA_REG, sect_buf, shorts);
1429}
1430
1431
1432static void
1433input_data_shorts(int dev, ushort *sect_buf, int shorts)
1434{
1435 insw(ATA_DATA_REG, sect_buf, shorts);
1436}
1437
1438#endif /* __PPC__ */
1439
wdenkc6097192002-11-03 00:24:07 +00001440/*
1441 * Wait until (Status & mask) == res, or timeout (in ms)
1442 * Return last status
1443 * This is used since some ATAPI CD ROMs clears their Busy Bit first
1444 * and then they set their DRQ Bit
1445 */
1446static uchar atapi_wait_mask (int dev, ulong t,uchar mask, uchar res)
1447{
1448 ulong delay = 10 * t; /* poll every 100 us */
1449 uchar c;
1450
wdenk591dda52002-11-18 00:14:45 +00001451 c = ide_inb(dev,ATA_DEV_CTL); /* prevents to read the status before valid */
1452 while (((c = ide_inb(dev, ATA_STATUS)) & mask) != res) {
wdenkc6097192002-11-03 00:24:07 +00001453 /* break if error occurs (doesn't make sense to wait more) */
1454 if((c & ATA_STAT_ERR)==ATA_STAT_ERR)
1455 break;
1456 udelay (100);
1457 if (delay-- == 0) {
1458 break;
1459 }
1460 }
1461 return (c);
1462}
1463
1464/*
1465 * issue an atapi command
1466 */
1467unsigned char atapi_issue(int device,unsigned char* ccb,int ccblen, unsigned char * buffer,int buflen)
1468{
1469 unsigned char c,err,mask,res;
1470 int n;
1471 ide_led (DEVICE_LED(device), 1); /* LED on */
1472
1473 /* Select device
1474 */
1475 mask = ATA_STAT_BUSY|ATA_STAT_DRQ;
1476 res = 0;
wdenk452cfd62002-11-19 11:04:11 +00001477#ifdef CONFIG_AMIGAONEG3SE
1478# warning THF: Removed LBA mode ???
1479#endif
wdenk591dda52002-11-18 00:14:45 +00001480 ide_outb (device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
wdenkc6097192002-11-03 00:24:07 +00001481 c = atapi_wait_mask(device,ATAPI_TIME_OUT,mask,res);
1482 if ((c & mask) != res) {
1483 printf ("ATAPI_ISSUE: device %d not ready status %X\n", device,c);
1484 err=0xFF;
1485 goto AI_OUT;
1486 }
1487 /* write taskfile */
wdenk591dda52002-11-18 00:14:45 +00001488 ide_outb (device, ATA_ERROR_REG, 0); /* no DMA, no overlaped */
wdenk452cfd62002-11-19 11:04:11 +00001489 ide_outb (device, ATA_SECT_CNT, 0);
1490 ide_outb (device, ATA_SECT_NUM, 0);
wdenk591dda52002-11-18 00:14:45 +00001491 ide_outb (device, ATA_CYL_LOW, (unsigned char)(buflen & 0xFF));
wdenk452cfd62002-11-19 11:04:11 +00001492 ide_outb (device, ATA_CYL_HIGH, (unsigned char)((buflen>>8) & 0xFF));
1493#ifdef CONFIG_AMIGAONEG3SE
1494# warning THF: Removed LBA mode ???
1495#endif
wdenk591dda52002-11-18 00:14:45 +00001496 ide_outb (device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
wdenkc6097192002-11-03 00:24:07 +00001497
wdenk591dda52002-11-18 00:14:45 +00001498 ide_outb (device, ATA_COMMAND, ATAPI_CMD_PACKET);
wdenkc6097192002-11-03 00:24:07 +00001499 udelay (50);
1500
1501 mask = ATA_STAT_DRQ|ATA_STAT_BUSY|ATA_STAT_ERR;
1502 res = ATA_STAT_DRQ;
1503 c = atapi_wait_mask(device,ATAPI_TIME_OUT,mask,res);
1504
1505 if ((c & mask) != res) { /* DRQ must be 1, BSY 0 */
1506 printf ("ATTAPI_ISSUE: Error (no IRQ) before sending ccb dev %d status 0x%02x\n",device,c);
1507 err=0xFF;
1508 goto AI_OUT;
1509 }
1510
1511 output_data_shorts (device, (unsigned short *)ccb,ccblen/2); /* write command block */
1512 /* ATAPI Command written wait for completition */
1513 udelay (5000); /* device must set bsy */
1514
1515 mask = ATA_STAT_DRQ|ATA_STAT_BUSY|ATA_STAT_ERR;
1516 /* if no data wait for DRQ = 0 BSY = 0
1517 * if data wait for DRQ = 1 BSY = 0 */
1518 res=0;
1519 if(buflen)
1520 res = ATA_STAT_DRQ;
1521 c = atapi_wait_mask(device,ATAPI_TIME_OUT,mask,res);
1522 if ((c & mask) != res ) {
1523 if (c & ATA_STAT_ERR) {
wdenk591dda52002-11-18 00:14:45 +00001524 err=(ide_inb(device,ATA_ERROR_REG))>>4;
wdenkc6097192002-11-03 00:24:07 +00001525 AT_PRINTF("atapi_issue 1 returned sense key %X status %02X\n",err,c);
1526 } else {
1527 printf ("ATTAPI_ISSUE: (no DRQ) after sending ccb (%x) status 0x%02x\n", ccb[0],c);
1528 err=0xFF;
1529 }
1530 goto AI_OUT;
1531 }
wdenk591dda52002-11-18 00:14:45 +00001532 n=ide_inb(device, ATA_CYL_HIGH);
wdenkc6097192002-11-03 00:24:07 +00001533 n<<=8;
wdenk591dda52002-11-18 00:14:45 +00001534 n+=ide_inb(device, ATA_CYL_LOW);
wdenkc6097192002-11-03 00:24:07 +00001535 if(n>buflen) {
1536 printf("ERROR, transfer bytes %d requested only %d\n",n,buflen);
1537 err=0xff;
1538 goto AI_OUT;
1539 }
1540 if((n==0)&&(buflen<0)) {
1541 printf("ERROR, transfer bytes %d requested %d\n",n,buflen);
1542 err=0xff;
1543 goto AI_OUT;
1544 }
1545 if(n!=buflen) {
1546 AT_PRINTF("WARNING, transfer bytes %d not equal with requested %d\n",n,buflen);
1547 }
1548 if(n!=0) { /* data transfer */
1549 AT_PRINTF("ATAPI_ISSUE: %d Bytes to transfer\n",n);
1550 /* we transfer shorts */
1551 n>>=1;
1552 /* ok now decide if it is an in or output */
wdenk591dda52002-11-18 00:14:45 +00001553 if ((ide_inb(device, ATA_SECT_CNT)&0x02)==0) {
wdenkc6097192002-11-03 00:24:07 +00001554 AT_PRINTF("Write to device\n");
1555 output_data_shorts(device,(unsigned short *)buffer,n);
1556 } else {
1557 AT_PRINTF("Read from device @ %p shorts %d\n",buffer,n);
1558 input_data_shorts(device,(unsigned short *)buffer,n);
1559 }
1560 }
1561 udelay(5000); /* seems that some CD ROMs need this... */
1562 mask = ATA_STAT_BUSY|ATA_STAT_ERR;
1563 res=0;
1564 c = atapi_wait_mask(device,ATAPI_TIME_OUT,mask,res);
1565 if ((c & ATA_STAT_ERR) == ATA_STAT_ERR) {
wdenk591dda52002-11-18 00:14:45 +00001566 err=(ide_inb(device,ATA_ERROR_REG) >> 4);
wdenkc6097192002-11-03 00:24:07 +00001567 AT_PRINTF("atapi_issue 2 returned sense key %X status %X\n",err,c);
1568 } else {
1569 err = 0;
1570 }
1571AI_OUT:
1572 ide_led (DEVICE_LED(device), 0); /* LED off */
1573 return (err);
1574}
1575
1576/*
1577 * sending the command to atapi_issue. If an status other than good
1578 * returns, an request_sense will be issued
1579 */
1580
1581#define ATAPI_DRIVE_NOT_READY 100
1582#define ATAPI_UNIT_ATTN 10
1583
1584unsigned char atapi_issue_autoreq (int device,
1585 unsigned char* ccb,
1586 int ccblen,
1587 unsigned char *buffer,
1588 int buflen)
1589{
1590 unsigned char sense_data[18],sense_ccb[12];
1591 unsigned char res,key,asc,ascq;
1592 int notready,unitattn;
1593
wdenk452cfd62002-11-19 11:04:11 +00001594#ifdef CONFIG_AMIGAONEG3SE
1595 char *s;
1596 unsigned int timeout, retrycnt;
1597
1598 s = getenv("ide_cd_timeout");
1599 timeout = s ? (simple_strtol(s, NULL, 10)*1000000)/5 : 0;
1600
1601 retrycnt = 0;
1602#endif
1603
wdenkc6097192002-11-03 00:24:07 +00001604 unitattn=ATAPI_UNIT_ATTN;
1605 notready=ATAPI_DRIVE_NOT_READY;
1606
1607retry:
1608 res= atapi_issue(device,ccb,ccblen,buffer,buflen);
1609 if (res==0)
1610 return (0); /* Ok */
1611
1612 if (res==0xFF)
1613 return (0xFF); /* error */
1614
1615 AT_PRINTF("(auto_req)atapi_issue returned sense key %X\n",res);
1616
1617 memset(sense_ccb,0,sizeof(sense_ccb));
1618 memset(sense_data,0,sizeof(sense_data));
1619 sense_ccb[0]=ATAPI_CMD_REQ_SENSE;
wdenk452cfd62002-11-19 11:04:11 +00001620 sense_ccb[4]=18; /* allocation Length */
wdenkc6097192002-11-03 00:24:07 +00001621
1622 res=atapi_issue(device,sense_ccb,12,sense_data,18);
1623 key=(sense_data[2]&0xF);
1624 asc=(sense_data[12]);
1625 ascq=(sense_data[13]);
1626
1627 AT_PRINTF("ATAPI_CMD_REQ_SENSE returned %x\n",res);
1628 AT_PRINTF(" Sense page: %02X key %02X ASC %02X ASCQ %02X\n",
1629 sense_data[0],
1630 key,
1631 asc,
1632 ascq);
1633
1634 if((key==0))
1635 return 0; /* ok device ready */
1636
1637 if((key==6)|| (asc==0x29) || (asc==0x28)) { /* Unit Attention */
1638 if(unitattn-->0) {
1639 udelay(200*1000);
1640 goto retry;
1641 }
1642 printf("Unit Attention, tried %d\n",ATAPI_UNIT_ATTN);
1643 goto error;
1644 }
1645 if((asc==0x4) && (ascq==0x1)) { /* not ready, but will be ready soon */
1646 if (notready-->0) {
1647 udelay(200*1000);
1648 goto retry;
1649 }
1650 printf("Drive not ready, tried %d times\n",ATAPI_DRIVE_NOT_READY);
1651 goto error;
1652 }
1653 if(asc==0x3a) {
1654 AT_PRINTF("Media not present\n");
1655 goto error;
1656 }
wdenk452cfd62002-11-19 11:04:11 +00001657
1658#ifdef CONFIG_AMIGAONEG3SE
1659 if ((sense_data[2]&0xF)==0x0B) {
1660 AT_PRINTF("ABORTED COMMAND...retry\n");
1661 if (retrycnt++ < 4)
1662 goto retry;
1663 return (0xFF);
1664 }
1665
1666 if ((sense_data[2]&0xf) == 0x02 &&
1667 sense_data[12] == 0x04 &&
1668 sense_data[13] == 0x01 ) {
1669 AT_PRINTF("Waiting for unit to become active\n");
1670 udelay(timeout);
1671 if (retrycnt++ < 4)
1672 goto retry;
1673 return 0xFF;
1674 }
1675#endif /* CONFIG_AMIGAONEG3SE */
1676
wdenkc6097192002-11-03 00:24:07 +00001677 printf ("ERROR: Unknown Sense key %02X ASC %02X ASCQ %02X\n",key,asc,ascq);
1678error:
1679 AT_PRINTF ("ERROR Sense key %02X ASC %02X ASCQ %02X\n",key,asc,ascq);
1680 return (0xFF);
1681}
1682
1683
1684
1685static void atapi_inquiry(block_dev_desc_t * dev_desc)
1686{
1687 unsigned char ccb[12]; /* Command descriptor block */
1688 unsigned char iobuf[64]; /* temp buf */
1689 unsigned char c;
1690 int device;
1691
1692 device=dev_desc->dev;
1693 dev_desc->type=DEV_TYPE_UNKNOWN; /* not yet valid */
1694 dev_desc->block_read=atapi_read;
1695
1696 memset(ccb,0,sizeof(ccb));
1697 memset(iobuf,0,sizeof(iobuf));
1698
1699 ccb[0]=ATAPI_CMD_INQUIRY;
1700 ccb[4]=40; /* allocation Legnth */
1701 c=atapi_issue_autoreq(device,ccb,12,(unsigned char *)iobuf,40);
1702
1703 AT_PRINTF("ATAPI_CMD_INQUIRY returned %x\n",c);
1704 if (c!=0)
1705 return;
1706
1707 /* copy device ident strings */
1708 ident_cpy(dev_desc->vendor,&iobuf[8],8);
1709 ident_cpy(dev_desc->product,&iobuf[16],16);
1710 ident_cpy(dev_desc->revision,&iobuf[32],5);
1711
1712 dev_desc->lun=0;
1713 dev_desc->lba=0;
1714 dev_desc->blksz=0;
1715 dev_desc->type=iobuf[0] & 0x1f;
1716
1717 if ((iobuf[1]&0x80)==0x80)
1718 dev_desc->removable = 1;
1719 else
1720 dev_desc->removable = 0;
1721
1722 memset(ccb,0,sizeof(ccb));
1723 memset(iobuf,0,sizeof(iobuf));
1724 ccb[0]=ATAPI_CMD_START_STOP;
1725 ccb[4]=0x03; /* start */
1726
1727 c=atapi_issue_autoreq(device,ccb,12,(unsigned char *)iobuf,0);
1728
1729 AT_PRINTF("ATAPI_CMD_START_STOP returned %x\n",c);
1730 if (c!=0)
1731 return;
1732
1733 memset(ccb,0,sizeof(ccb));
1734 memset(iobuf,0,sizeof(iobuf));
1735 c=atapi_issue_autoreq(device,ccb,12,(unsigned char *)iobuf,0);
1736
1737 AT_PRINTF("ATAPI_CMD_UNIT_TEST_READY returned %x\n",c);
1738 if (c!=0)
1739 return;
1740
1741 memset(ccb,0,sizeof(ccb));
1742 memset(iobuf,0,sizeof(iobuf));
1743 ccb[0]=ATAPI_CMD_READ_CAP;
1744 c=atapi_issue_autoreq(device,ccb,12,(unsigned char *)iobuf,8);
1745 AT_PRINTF("ATAPI_CMD_READ_CAP returned %x\n",c);
1746 if (c!=0)
1747 return;
1748
1749 AT_PRINTF("Read Cap: LBA %02X%02X%02X%02X blksize %02X%02X%02X%02X\n",
1750 iobuf[0],iobuf[1],iobuf[2],iobuf[3],
1751 iobuf[4],iobuf[5],iobuf[6],iobuf[7]);
1752
1753 dev_desc->lba =((unsigned long)iobuf[0]<<24) +
1754 ((unsigned long)iobuf[1]<<16) +
1755 ((unsigned long)iobuf[2]<< 8) +
1756 ((unsigned long)iobuf[3]);
1757 dev_desc->blksz=((unsigned long)iobuf[4]<<24) +
1758 ((unsigned long)iobuf[5]<<16) +
1759 ((unsigned long)iobuf[6]<< 8) +
1760 ((unsigned long)iobuf[7]);
1761 return;
1762}
1763
1764
1765/*
1766 * atapi_read:
1767 * we transfer only one block per command, since the multiple DRQ per
1768 * command is not yet implemented
1769 */
1770#define ATAPI_READ_MAX_BYTES 2048 /* we read max 2kbytes */
1771#define ATAPI_READ_BLOCK_SIZE 2048 /* assuming CD part */
1772#define ATAPI_READ_MAX_BLOCK ATAPI_READ_MAX_BYTES/ATAPI_READ_BLOCK_SIZE /* max blocks */
1773
1774ulong atapi_read (int device, ulong blknr, ulong blkcnt, ulong *buffer)
1775{
1776 ulong n = 0;
1777 unsigned char ccb[12]; /* Command descriptor block */
1778 ulong cnt;
1779
1780 AT_PRINTF("atapi_read dev %d start %lX, blocks %lX buffer at %lX\n",
1781 device, blknr, blkcnt, (ulong)buffer);
1782
1783 do {
1784 if (blkcnt>ATAPI_READ_MAX_BLOCK) {
1785 cnt=ATAPI_READ_MAX_BLOCK;
1786 } else {
1787 cnt=blkcnt;
1788 }
1789 ccb[0]=ATAPI_CMD_READ_12;
1790 ccb[1]=0; /* reserved */
1791 ccb[2]=(unsigned char) (blknr>>24) & 0xFF; /* MSB Block */
1792 ccb[3]=(unsigned char) (blknr>>16) & 0xFF; /* */
1793 ccb[4]=(unsigned char) (blknr>> 8) & 0xFF;
1794 ccb[5]=(unsigned char) blknr & 0xFF; /* LSB Block */
1795 ccb[6]=(unsigned char) (cnt >>24) & 0xFF; /* MSB Block count */
1796 ccb[7]=(unsigned char) (cnt >>16) & 0xFF;
1797 ccb[8]=(unsigned char) (cnt >> 8) & 0xFF;
1798 ccb[9]=(unsigned char) cnt & 0xFF; /* LSB Block */
1799 ccb[10]=0; /* reserved */
1800 ccb[11]=0; /* reserved */
1801
1802 if (atapi_issue_autoreq(device,ccb,12,
1803 (unsigned char *)buffer,
1804 cnt*ATAPI_READ_BLOCK_SIZE) == 0xFF) {
1805 return (n);
1806 }
1807 n+=cnt;
1808 blkcnt-=cnt;
1809 blknr+=cnt;
1810 buffer+=cnt*(ATAPI_READ_BLOCK_SIZE/4); /* ulong blocksize in ulong */
1811 } while (blkcnt > 0);
1812 return (n);
1813}
1814
1815/* ------------------------------------------------------------------------- */
1816
1817#endif /* CONFIG_ATAPI */
1818
1819#endif /* CONFIG_COMMANDS & CFG_CMD_IDE */