blob: 668d838afaeef85a7c04e6e1ffc31ddccfc9d8f6 [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>
wdenkc6097192002-11-03 00:24:07 +000042#ifdef CONFIG_STATUS_LED
43# include <status_led.h>
44#endif
wdenk9f837932003-10-09 19:00:25 +000045#ifndef __PPC__
wdenk591dda52002-11-18 00:14:45 +000046#include <asm/io.h>
wdenk9f837932003-10-09 19:00:25 +000047#ifdef __MIPS__
48/* Macros depend on this variable */
49static unsigned long mips_io_port_base = 0;
50#endif
wdenk591dda52002-11-18 00:14:45 +000051#endif
wdenkc6097192002-11-03 00:24:07 +000052
53#ifdef CONFIG_SHOW_BOOT_PROGRESS
54# include <status_led.h>
55# define SHOW_BOOT_PROGRESS(arg) show_boot_progress(arg)
56#else
57# define SHOW_BOOT_PROGRESS(arg)
58#endif
59
60
61#undef IDE_DEBUG
62
wdenk452cfd62002-11-19 11:04:11 +000063
wdenkc6097192002-11-03 00:24:07 +000064#ifdef IDE_DEBUG
65#define PRINTF(fmt,args...) printf (fmt ,##args)
66#else
67#define PRINTF(fmt,args...)
68#endif
69
70#if (CONFIG_COMMANDS & CFG_CMD_IDE)
71
wdenk9f837932003-10-09 19:00:25 +000072#ifdef CONFIG_IDE_8xx_DIRECT
wdenkc6097192002-11-03 00:24:07 +000073/* Timings for IDE Interface
74 *
75 * SETUP / LENGTH / HOLD - cycles valid for 50 MHz clk
76 * 70 165 30 PIO-Mode 0, [ns]
77 * 4 9 2 [Cycles]
78 * 50 125 20 PIO-Mode 1, [ns]
79 * 3 7 2 [Cycles]
80 * 30 100 15 PIO-Mode 2, [ns]
81 * 2 6 1 [Cycles]
82 * 30 80 10 PIO-Mode 3, [ns]
83 * 2 5 1 [Cycles]
84 * 25 70 10 PIO-Mode 4, [ns]
85 * 2 4 1 [Cycles]
86 */
87
88const static pio_config_t pio_config_ns [IDE_MAX_PIO_MODE+1] =
89{
90 /* Setup Length Hold */
91 { 70, 165, 30 }, /* PIO-Mode 0, [ns] */
92 { 50, 125, 20 }, /* PIO-Mode 1, [ns] */
93 { 30, 101, 15 }, /* PIO-Mode 2, [ns] */
94 { 30, 80, 10 }, /* PIO-Mode 3, [ns] */
95 { 25, 70, 10 }, /* PIO-Mode 4, [ns] */
96};
97
98static pio_config_t pio_config_clk [IDE_MAX_PIO_MODE+1];
99
100#ifndef CFG_PIO_MODE
101#define CFG_PIO_MODE 0 /* use a relaxed default */
102#endif
103static int pio_mode = CFG_PIO_MODE;
104
105/* Make clock cycles and always round up */
106
107#define PCMCIA_MK_CLKS( t, T ) (( (t) * (T) + 999U ) / 1000U )
108
wdenk9f837932003-10-09 19:00:25 +0000109#endif /* CONFIG_IDE_8xx_DIRECT */
110
wdenkc6097192002-11-03 00:24:07 +0000111/* ------------------------------------------------------------------------- */
112
113/* Current I/O Device */
114static int curr_device = -1;
115
116/* Current offset for IDE0 / IDE1 bus access */
117ulong ide_bus_offset[CFG_IDE_MAXBUS] = {
118#if defined(CFG_ATA_IDE0_OFFSET)
119 CFG_ATA_IDE0_OFFSET,
120#endif
121#if defined(CFG_ATA_IDE1_OFFSET) && (CFG_IDE_MAXBUS > 1)
122 CFG_ATA_IDE1_OFFSET,
123#endif
124};
125
wdenk9f837932003-10-09 19:00:25 +0000126
wdenkc6097192002-11-03 00:24:07 +0000127#define ATA_CURR_BASE(dev) (CFG_ATA_BASE_ADDR+ide_bus_offset[IDE_BUS(dev)])
128
wdenk452cfd62002-11-19 11:04:11 +0000129#ifndef CONFIG_AMIGAONEG3SE
wdenkc6097192002-11-03 00:24:07 +0000130static int ide_bus_ok[CFG_IDE_MAXBUS];
wdenk452cfd62002-11-19 11:04:11 +0000131#else
132static int ide_bus_ok[CFG_IDE_MAXBUS] = {0,};
133#endif
wdenkc6097192002-11-03 00:24:07 +0000134
135static block_dev_desc_t ide_dev_desc[CFG_IDE_MAXDEVICE];
136/* ------------------------------------------------------------------------- */
137
138#ifdef CONFIG_IDE_LED
wdenk90e7e422002-12-04 23:39:58 +0000139#ifndef CONFIG_KUP4K
wdenkc6097192002-11-03 00:24:07 +0000140static void ide_led (uchar led, uchar status);
141#else
wdenk90e7e422002-12-04 23:39:58 +0000142extern void ide_led (uchar led, uchar status);
143#endif
144#else
wdenk452cfd62002-11-19 11:04:11 +0000145#ifndef CONFIG_AMIGAONEG3SE
wdenkc6097192002-11-03 00:24:07 +0000146#define ide_led(a,b) /* dummy */
wdenk452cfd62002-11-19 11:04:11 +0000147#else
148extern void ide_led(uchar led, uchar status);
149#define LED_IDE1 1
150#define LED_IDE2 2
151#define CONFIG_IDE_LED 1
152#define DEVICE_LED(x) 1
153#endif
wdenkc6097192002-11-03 00:24:07 +0000154#endif
155
156#ifdef CONFIG_IDE_RESET
157static void ide_reset (void);
158#else
159#define ide_reset() /* dummy */
160#endif
161
162static void ide_ident (block_dev_desc_t *dev_desc);
163static uchar ide_wait (int dev, ulong t);
164
165#define IDE_TIME_OUT 2000 /* 2 sec timeout */
166
167#define ATAPI_TIME_OUT 7000 /* 7 sec timeout (5 sec seems to work...) */
168
169#define IDE_SPIN_UP_TIME_OUT 5000 /* 5 sec spin-up timeout */
170
wdenk591dda52002-11-18 00:14:45 +0000171static void __inline__ ide_outb(int dev, int port, unsigned char val);
172static unsigned char __inline__ ide_inb(int dev, int port);
wdenkc6097192002-11-03 00:24:07 +0000173static void input_data(int dev, ulong *sect_buf, int words);
174static void output_data(int dev, ulong *sect_buf, int words);
175static void ident_cpy (unsigned char *dest, unsigned char *src, unsigned int len);
176
177
178#ifdef CONFIG_ATAPI
179static void atapi_inquiry(block_dev_desc_t *dev_desc);
180ulong atapi_read (int device, ulong blknr, ulong blkcnt, ulong *buffer);
181#endif
182
183
184#ifdef CONFIG_IDE_8xx_DIRECT
185static void set_pcmcia_timing (int pmode);
wdenkc6097192002-11-03 00:24:07 +0000186#endif
187
188/* ------------------------------------------------------------------------- */
189
190int do_ide (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
191{
192 int rcode = 0;
193
194 switch (argc) {
195 case 0:
196 case 1:
197 printf ("Usage:\n%s\n", cmdtp->usage);
198 return 1;
199 case 2:
200 if (strncmp(argv[1],"res",3) == 0) {
201 puts ("\nReset IDE"
202#ifdef CONFIG_IDE_8xx_DIRECT
203 " on PCMCIA " PCMCIA_SLOT_MSG
204#endif
205 ": ");
206
207 ide_init ();
208 return 0;
209 } else if (strncmp(argv[1],"inf",3) == 0) {
210 int i;
211
212 putc ('\n');
213
214 for (i=0; i<CFG_IDE_MAXDEVICE; ++i) {
215 if (ide_dev_desc[i].type==DEV_TYPE_UNKNOWN)
216 continue; /* list only known devices */
217 printf ("IDE device %d: ", i);
218 dev_print(&ide_dev_desc[i]);
219 }
220 return 0;
221
222 } else if (strncmp(argv[1],"dev",3) == 0) {
223 if ((curr_device < 0) || (curr_device >= CFG_IDE_MAXDEVICE)) {
224 puts ("\nno IDE devices available\n");
225 return 1;
226 }
227 printf ("\nIDE device %d: ", curr_device);
228 dev_print(&ide_dev_desc[curr_device]);
229 return 0;
230 } else if (strncmp(argv[1],"part",4) == 0) {
231 int dev, ok;
232
233 for (ok=0, dev=0; dev<CFG_IDE_MAXDEVICE; ++dev) {
234 if (ide_dev_desc[dev].part_type!=PART_TYPE_UNKNOWN) {
235 ++ok;
236 if (dev)
237 putc ('\n');
238 print_part(&ide_dev_desc[dev]);
239 }
240 }
241 if (!ok) {
242 puts ("\nno IDE devices available\n");
243 rcode ++;
244 }
245 return rcode;
246 }
247 printf ("Usage:\n%s\n", cmdtp->usage);
248 return 1;
249 case 3:
250 if (strncmp(argv[1],"dev",3) == 0) {
251 int dev = (int)simple_strtoul(argv[2], NULL, 10);
252
253 printf ("\nIDE device %d: ", dev);
254 if (dev >= CFG_IDE_MAXDEVICE) {
255 puts ("unknown device\n");
256 return 1;
257 }
258 dev_print(&ide_dev_desc[dev]);
259 /*ide_print (dev);*/
260
261 if (ide_dev_desc[dev].type == DEV_TYPE_UNKNOWN) {
262 return 1;
263 }
264
265 curr_device = dev;
266
267 puts ("... is now current device\n");
268
269 return 0;
270 } else if (strncmp(argv[1],"part",4) == 0) {
271 int dev = (int)simple_strtoul(argv[2], NULL, 10);
272
273 if (ide_dev_desc[dev].part_type!=PART_TYPE_UNKNOWN) {
274 print_part(&ide_dev_desc[dev]);
275 } else {
276 printf ("\nIDE device %d not available\n", dev);
277 rcode = 1;
278 }
279 return rcode;
280#if 0
281 } else if (strncmp(argv[1],"pio",4) == 0) {
282 int mode = (int)simple_strtoul(argv[2], NULL, 10);
283
284 if ((mode >= 0) && (mode <= IDE_MAX_PIO_MODE)) {
285 puts ("\nSetting ");
286 pio_mode = mode;
287 ide_init ();
288 } else {
289 printf ("\nInvalid PIO mode %d (0 ... %d only)\n",
290 mode, IDE_MAX_PIO_MODE);
291 }
292 return;
293#endif
294 }
295
296 printf ("Usage:\n%s\n", cmdtp->usage);
297 return 1;
298 default:
299 /* at least 4 args */
300
301 if (strcmp(argv[1],"read") == 0) {
302 ulong addr = simple_strtoul(argv[2], NULL, 16);
303 ulong blk = simple_strtoul(argv[3], NULL, 16);
304 ulong cnt = simple_strtoul(argv[4], NULL, 16);
305 ulong n;
306
307 printf ("\nIDE read: device %d block # %ld, count %ld ... ",
308 curr_device, blk, cnt);
309
310 n = ide_dev_desc[curr_device].block_read (curr_device,
311 blk, cnt,
312 (ulong *)addr);
313 /* flush cache after read */
314 flush_cache (addr, cnt*ide_dev_desc[curr_device].blksz);
315
316 printf ("%ld blocks read: %s\n",
317 n, (n==cnt) ? "OK" : "ERROR");
318 if (n==cnt) {
319 return 0;
320 } else {
321 return 1;
322 }
323 } else if (strcmp(argv[1],"write") == 0) {
324 ulong addr = simple_strtoul(argv[2], NULL, 16);
325 ulong blk = simple_strtoul(argv[3], NULL, 16);
326 ulong cnt = simple_strtoul(argv[4], NULL, 16);
327 ulong n;
328
329 printf ("\nIDE write: device %d block # %ld, count %ld ... ",
330 curr_device, blk, cnt);
331
332 n = ide_write (curr_device, blk, cnt, (ulong *)addr);
333
334 printf ("%ld blocks written: %s\n",
335 n, (n==cnt) ? "OK" : "ERROR");
336 if (n==cnt) {
337 return 0;
338 } else {
339 return 1;
340 }
341 } else {
342 printf ("Usage:\n%s\n", cmdtp->usage);
343 rcode = 1;
344 }
345
346 return rcode;
347 }
348}
349
350int do_diskboot (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
351{
352 char *boot_device = NULL;
353 char *ep;
354 int dev, part = 0;
355 ulong cnt;
356 ulong addr;
357 disk_partition_t info;
358 image_header_t *hdr;
359 int rcode = 0;
360
361 switch (argc) {
362 case 1:
363 addr = CFG_LOAD_ADDR;
364 boot_device = getenv ("bootdevice");
365 break;
366 case 2:
367 addr = simple_strtoul(argv[1], NULL, 16);
368 boot_device = getenv ("bootdevice");
369 break;
370 case 3:
371 addr = simple_strtoul(argv[1], NULL, 16);
372 boot_device = argv[2];
373 break;
374 default:
375 printf ("Usage:\n%s\n", cmdtp->usage);
376 SHOW_BOOT_PROGRESS (-1);
377 return 1;
378 }
379
380 if (!boot_device) {
381 puts ("\n** No boot device **\n");
382 SHOW_BOOT_PROGRESS (-1);
383 return 1;
384 }
385
386 dev = simple_strtoul(boot_device, &ep, 16);
387
388 if (ide_dev_desc[dev].type==DEV_TYPE_UNKNOWN) {
389 printf ("\n** Device %d not available\n", dev);
390 SHOW_BOOT_PROGRESS (-1);
391 return 1;
392 }
393
394 if (*ep) {
395 if (*ep != ':') {
396 puts ("\n** Invalid boot device, use `dev[:part]' **\n");
397 SHOW_BOOT_PROGRESS (-1);
398 return 1;
399 }
400 part = simple_strtoul(++ep, NULL, 16);
401 }
402 if (get_partition_info (&ide_dev_desc[dev], part, &info)) {
403 SHOW_BOOT_PROGRESS (-1);
404 return 1;
405 }
wdenkc08f1582003-04-27 22:52:51 +0000406 if ((strncmp(info.type, BOOT_PART_TYPE, sizeof(info.type)) != 0) &&
407 (strncmp(info.type, BOOT_PART_COMP, sizeof(info.type)) != 0)) {
wdenkc6097192002-11-03 00:24:07 +0000408 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{
wdenkc6097192002-11-03 00:24:07 +0000476
477#ifdef CONFIG_IDE_8xx_DIRECT
wdenk9f837932003-10-09 19:00:25 +0000478 DECLARE_GLOBAL_DATA_PTR;
wdenkc6097192002-11-03 00:24:07 +0000479 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;
wdenk452cfd62002-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
wdenk7f00b942003-12-07 23:55:12 +0000489#ifdef CONFIG_IDE_8xx_PCCARD
490 extern int pcmcia_on (void);
491 extern int ide_devices_found; /* Initialized in check_ide_device() */
492#endif /* CONFIG_IDE_8xx_PCCARD */
493
494#ifdef CONFIG_IDE_PREINIT
495 WATCHDOG_RESET();
496
497 if (ide_preinit ()) {
498 puts ("ide_preinit failed\n");
499 return;
500 }
501#endif /* CONFIG_IDE_PREINIT */
wdenkc6097192002-11-03 00:24:07 +0000502
503#ifdef CONFIG_IDE_8xx_PCCARD
504 extern int pcmcia_on (void);
wdenk4fc95692003-02-28 00:49:47 +0000505 extern int ide_devices_found; /* Initialized in check_ide_device() */
wdenkc6097192002-11-03 00:24:07 +0000506
507 WATCHDOG_RESET();
508
wdenk4fc95692003-02-28 00:49:47 +0000509 ide_devices_found = 0;
wdenkc6097192002-11-03 00:24:07 +0000510 /* initialize the PCMCIA IDE adapter card */
wdenk4fc95692003-02-28 00:49:47 +0000511 pcmcia_on();
512 if (!ide_devices_found)
wdenkc6097192002-11-03 00:24:07 +0000513 return;
514 udelay (1000000); /* 1 s */
515#endif /* CONFIG_IDE_8xx_PCCARD */
516
517 WATCHDOG_RESET();
518
wdenk9f837932003-10-09 19:00:25 +0000519#ifdef CONFIG_IDE_8xx_DIRECT
wdenkc6097192002-11-03 00:24:07 +0000520 /* Initialize PIO timing tables */
521 for (i=0; i <= IDE_MAX_PIO_MODE; ++i) {
522 pio_config_clk[i].t_setup = PCMCIA_MK_CLKS(pio_config_ns[i].t_setup,
523 gd->bus_clk);
524 pio_config_clk[i].t_length = PCMCIA_MK_CLKS(pio_config_ns[i].t_length,
525 gd->bus_clk);
526 pio_config_clk[i].t_hold = PCMCIA_MK_CLKS(pio_config_ns[i].t_hold,
527 gd->bus_clk);
528 PRINTF ("PIO Mode %d: setup=%2d ns/%d clk"
529 " len=%3d ns/%d clk"
530 " hold=%2d ns/%d clk\n",
531 i,
532 pio_config_ns[i].t_setup, pio_config_clk[i].t_setup,
533 pio_config_ns[i].t_length, pio_config_clk[i].t_length,
534 pio_config_ns[i].t_hold, pio_config_clk[i].t_hold);
535 }
wdenk9f837932003-10-09 19:00:25 +0000536#endif /* CONFIG_IDE_8xx_DIRECT */
wdenkc6097192002-11-03 00:24:07 +0000537
538 /* Reset the IDE just to be sure.
539 * Light LED's to show
540 */
541 ide_led ((LED_IDE1 | LED_IDE2), 1); /* LED's on */
542 ide_reset (); /* ATAPI Drives seems to need a proper IDE Reset */
543
544#ifdef CONFIG_IDE_8xx_DIRECT
545 /* PCMCIA / IDE initialization for common mem space */
546 pcmp->pcmc_pgcrb = 0;
wdenkc6097192002-11-03 00:24:07 +0000547
548 /* start in PIO mode 0 - most relaxed timings */
549 pio_mode = 0;
550 set_pcmcia_timing (pio_mode);
wdenk9f837932003-10-09 19:00:25 +0000551#endif /* CONFIG_IDE_8xx_DIRECT */
wdenkc6097192002-11-03 00:24:07 +0000552
553 /*
554 * Wait for IDE to get ready.
555 * According to spec, this can take up to 31 seconds!
556 */
wdenk452cfd62002-11-19 11:04:11 +0000557#ifndef CONFIG_AMIGAONEG3SE
wdenkc6097192002-11-03 00:24:07 +0000558 for (bus=0; bus<CFG_IDE_MAXBUS; ++bus) {
559 int dev = bus * (CFG_IDE_MAXDEVICE / CFG_IDE_MAXBUS);
wdenk452cfd62002-11-19 11:04:11 +0000560#else
561 s = getenv("ide_maxbus");
562 if (s)
563 max_bus_scan = simple_strtol(s, NULL, 10);
564 else
565 max_bus_scan = CFG_IDE_MAXBUS;
566
567 for (bus=0; bus<max_bus_scan; ++bus) {
568 int dev = bus * (CFG_IDE_MAXDEVICE / max_bus_scan);
569#endif
wdenkc6097192002-11-03 00:24:07 +0000570
wdenk4fc95692003-02-28 00:49:47 +0000571#ifdef CONFIG_IDE_8xx_PCCARD
572 /* Skip non-ide devices from probing */
573 if ((ide_devices_found & (1 << bus)) == 0) {
574 ide_led ((LED_IDE1 | LED_IDE2), 0); /* LED's off */
575 continue;
576 }
577#endif
wdenkc6097192002-11-03 00:24:07 +0000578 printf ("Bus %d: ", bus);
579
580 ide_bus_ok[bus] = 0;
581
582 /* Select device
583 */
584 udelay (100000); /* 100 ms */
wdenk591dda52002-11-18 00:14:45 +0000585 ide_outb (dev, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(dev));
wdenkc6097192002-11-03 00:24:07 +0000586 udelay (100000); /* 100 ms */
wdenk452cfd62002-11-19 11:04:11 +0000587#ifdef CONFIG_AMIGAONEG3SE
588 ata_reset_time = ATA_RESET_TIME;
589 s = getenv("ide_reset_timeout");
590 if (s) ata_reset_time = 2*simple_strtol(s, NULL, 10);
591#endif
wdenkc6097192002-11-03 00:24:07 +0000592 i = 0;
593 do {
594 udelay (10000); /* 10 ms */
595
wdenk591dda52002-11-18 00:14:45 +0000596 c = ide_inb (dev, ATA_STATUS);
wdenkc6097192002-11-03 00:24:07 +0000597 i++;
wdenk452cfd62002-11-19 11:04:11 +0000598#ifdef CONFIG_AMIGAONEG3SE
599 if (i > (ata_reset_time * 100)) {
600#else
wdenkc6097192002-11-03 00:24:07 +0000601 if (i > (ATA_RESET_TIME * 100)) {
wdenk452cfd62002-11-19 11:04:11 +0000602#endif
wdenkc6097192002-11-03 00:24:07 +0000603 puts ("** Timeout **\n");
604 ide_led ((LED_IDE1 | LED_IDE2), 0); /* LED's off */
wdenk452cfd62002-11-19 11:04:11 +0000605#ifdef CONFIG_AMIGAONEG3SE
606 /* If this is the second bus, the first one was OK */
607 if (bus != 0)
608 {
609 ide_bus_ok[bus] = 0;
610 goto skip_bus;
611 }
612#endif
wdenkc6097192002-11-03 00:24:07 +0000613 return;
614 }
615 if ((i >= 100) && ((i%100)==0)) {
616 putc ('.');
617 }
618 } while (c & ATA_STAT_BUSY);
619
620 if (c & (ATA_STAT_BUSY | ATA_STAT_FAULT)) {
621 puts ("not available ");
622 PRINTF ("Status = 0x%02X ", c);
623#ifndef CONFIG_ATAPI /* ATAPI Devices do not set DRDY */
624 } else if ((c & ATA_STAT_READY) == 0) {
625 puts ("not available ");
626 PRINTF ("Status = 0x%02X ", c);
627#endif
628 } else {
629 puts ("OK ");
630 ide_bus_ok[bus] = 1;
631 }
632 WATCHDOG_RESET();
633 }
wdenk452cfd62002-11-19 11:04:11 +0000634
635#ifdef CONFIG_AMIGAONEG3SE
636 skip_bus:
637#endif
wdenkc6097192002-11-03 00:24:07 +0000638 putc ('\n');
639
640 ide_led ((LED_IDE1 | LED_IDE2), 0); /* LED's off */
641
642 curr_device = -1;
643 for (i=0; i<CFG_IDE_MAXDEVICE; ++i) {
644#ifdef CONFIG_IDE_LED
645 int led = (IDE_BUS(i) == 0) ? LED_IDE1 : LED_IDE2;
646#endif
wdenk4b16c2e2003-11-07 13:42:26 +0000647 ide_dev_desc[i].type=DEV_TYPE_UNKNOWN;
wdenkc6097192002-11-03 00:24:07 +0000648 ide_dev_desc[i].if_type=IF_TYPE_IDE;
649 ide_dev_desc[i].dev=i;
650 ide_dev_desc[i].part_type=PART_TYPE_UNKNOWN;
651 ide_dev_desc[i].blksz=0;
652 ide_dev_desc[i].lba=0;
653 ide_dev_desc[i].block_read=ide_read;
654 if (!ide_bus_ok[IDE_BUS(i)])
655 continue;
656 ide_led (led, 1); /* LED on */
657 ide_ident(&ide_dev_desc[i]);
658 ide_led (led, 0); /* LED off */
659 dev_print(&ide_dev_desc[i]);
660/* ide_print (i); */
661 if ((ide_dev_desc[i].lba > 0) && (ide_dev_desc[i].blksz > 0)) {
662 init_part (&ide_dev_desc[i]); /* initialize partition type */
663 if (curr_device < 0)
664 curr_device = i;
665 }
666 }
667 WATCHDOG_RESET();
668}
669
670/* ------------------------------------------------------------------------- */
671
672block_dev_desc_t * ide_get_dev(int dev)
673{
674 return ((block_dev_desc_t *)&ide_dev_desc[dev]);
675}
676
677
678#ifdef CONFIG_IDE_8xx_DIRECT
679
680static void
681set_pcmcia_timing (int pmode)
682{
683 volatile immap_t *immr = (immap_t *)CFG_IMMR;
684 volatile pcmconf8xx_t *pcmp = &(immr->im_pcmcia);
685 ulong timings;
686
687 PRINTF ("Set timing for PIO Mode %d\n", pmode);
688
689 timings = PCMCIA_SHT(pio_config_clk[pmode].t_hold)
690 | PCMCIA_SST(pio_config_clk[pmode].t_setup)
691 | PCMCIA_SL (pio_config_clk[pmode].t_length)
692 ;
693
694 /* IDE 0
695 */
696 pcmp->pcmc_pbr0 = CFG_PCMCIA_PBR0;
697 pcmp->pcmc_por0 = CFG_PCMCIA_POR0
698#if (CFG_PCMCIA_POR0 != 0)
699 | timings
700#endif
701 ;
702 PRINTF ("PBR0: %08x POR0: %08x\n", pcmp->pcmc_pbr0, pcmp->pcmc_por0);
703
704 pcmp->pcmc_pbr1 = CFG_PCMCIA_PBR1;
705 pcmp->pcmc_por1 = CFG_PCMCIA_POR1
706#if (CFG_PCMCIA_POR1 != 0)
707 | timings
708#endif
709 ;
710 PRINTF ("PBR1: %08x POR1: %08x\n", pcmp->pcmc_pbr1, pcmp->pcmc_por1);
711
712 pcmp->pcmc_pbr2 = CFG_PCMCIA_PBR2;
713 pcmp->pcmc_por2 = CFG_PCMCIA_POR2
714#if (CFG_PCMCIA_POR2 != 0)
715 | timings
716#endif
717 ;
718 PRINTF ("PBR2: %08x POR2: %08x\n", pcmp->pcmc_pbr2, pcmp->pcmc_por2);
719
720 pcmp->pcmc_pbr3 = CFG_PCMCIA_PBR3;
721 pcmp->pcmc_por3 = CFG_PCMCIA_POR3
722#if (CFG_PCMCIA_POR3 != 0)
723 | timings
724#endif
725 ;
726 PRINTF ("PBR3: %08x POR3: %08x\n", pcmp->pcmc_pbr3, pcmp->pcmc_por3);
727
728 /* IDE 1
729 */
730 pcmp->pcmc_pbr4 = CFG_PCMCIA_PBR4;
731 pcmp->pcmc_por4 = CFG_PCMCIA_POR4
732#if (CFG_PCMCIA_POR4 != 0)
733 | timings
734#endif
735 ;
736 PRINTF ("PBR4: %08x POR4: %08x\n", pcmp->pcmc_pbr4, pcmp->pcmc_por4);
737
738 pcmp->pcmc_pbr5 = CFG_PCMCIA_PBR5;
739 pcmp->pcmc_por5 = CFG_PCMCIA_POR5
740#if (CFG_PCMCIA_POR5 != 0)
741 | timings
742#endif
743 ;
744 PRINTF ("PBR5: %08x POR5: %08x\n", pcmp->pcmc_pbr5, pcmp->pcmc_por5);
745
746 pcmp->pcmc_pbr6 = CFG_PCMCIA_PBR6;
747 pcmp->pcmc_por6 = CFG_PCMCIA_POR6
748#if (CFG_PCMCIA_POR6 != 0)
749 | timings
750#endif
751 ;
752 PRINTF ("PBR6: %08x POR6: %08x\n", pcmp->pcmc_pbr6, pcmp->pcmc_por6);
753
754 pcmp->pcmc_pbr7 = CFG_PCMCIA_PBR7;
755 pcmp->pcmc_por7 = CFG_PCMCIA_POR7
756#if (CFG_PCMCIA_POR7 != 0)
757 | timings
758#endif
759 ;
760 PRINTF ("PBR7: %08x POR7: %08x\n", pcmp->pcmc_pbr7, pcmp->pcmc_por7);
761
762}
763
764#endif /* CONFIG_IDE_8xx_DIRECT */
765
766/* ------------------------------------------------------------------------- */
767
wdenk591dda52002-11-18 00:14:45 +0000768#ifdef __PPC__
wdenkc6097192002-11-03 00:24:07 +0000769static void __inline__
wdenk591dda52002-11-18 00:14:45 +0000770ide_outb(int dev, int port, unsigned char val)
wdenkc6097192002-11-03 00:24:07 +0000771{
wdenk7f00b942003-12-07 23:55:12 +0000772 PRINTF ("ide_outb (dev= %d, port= %d, val= 0x%02x) : @ 0x%08lx\n",
773 dev, port, val, (ATA_CURR_BASE(dev)+port));
wdenk1ebf41e2004-01-02 14:00:00 +0000774
wdenkc6097192002-11-03 00:24:07 +0000775 /* Ensure I/O operations complete */
776 __asm__ volatile("eieio");
777 *((uchar *)(ATA_CURR_BASE(dev)+port)) = val;
wdenkc6097192002-11-03 00:24:07 +0000778}
wdenk591dda52002-11-18 00:14:45 +0000779#else /* ! __PPC__ */
780static void __inline__
781ide_outb(int dev, int port, unsigned char val)
782{
wdenk9f837932003-10-09 19:00:25 +0000783 outb(val, ATA_CURR_BASE(dev)+port);
wdenk591dda52002-11-18 00:14:45 +0000784}
785#endif /* __PPC__ */
786
wdenkc6097192002-11-03 00:24:07 +0000787
wdenk591dda52002-11-18 00:14:45 +0000788#ifdef __PPC__
wdenkc6097192002-11-03 00:24:07 +0000789static unsigned char __inline__
wdenk591dda52002-11-18 00:14:45 +0000790ide_inb(int dev, int port)
wdenkc6097192002-11-03 00:24:07 +0000791{
792 uchar val;
793 /* Ensure I/O operations complete */
794 __asm__ volatile("eieio");
795 val = *((uchar *)(ATA_CURR_BASE(dev)+port));
wdenk7f00b942003-12-07 23:55:12 +0000796 PRINTF ("ide_inb (dev= %d, port= %d) : @ 0x%08lx -> 0x%02x\n",
797 dev, port, (ATA_CURR_BASE(dev)+port), val);
wdenkc6097192002-11-03 00:24:07 +0000798 return (val);
799}
wdenk591dda52002-11-18 00:14:45 +0000800#else /* ! __PPC__ */
801static unsigned char __inline__
802ide_inb(int dev, int port)
803{
wdenk9f837932003-10-09 19:00:25 +0000804 return inb(ATA_CURR_BASE(dev)+port);
wdenk591dda52002-11-18 00:14:45 +0000805}
806#endif /* __PPC__ */
wdenkc6097192002-11-03 00:24:07 +0000807
wdenk591dda52002-11-18 00:14:45 +0000808#ifdef __PPC__
wdenk88d2ba92003-06-23 18:12:28 +0000809# ifdef CONFIG_AMIGAONEG3SE
wdenk452cfd62002-11-19 11:04:11 +0000810static void
811output_data_short(int dev, ulong *sect_buf, int words)
812{
813 ushort *dbuf;
814 volatile ushort *pbuf;
wdenk57b2d802003-06-27 21:31:46 +0000815
wdenk452cfd62002-11-19 11:04:11 +0000816 pbuf = (ushort *)(ATA_CURR_BASE(dev)+ATA_DATA_REG);
817 dbuf = (ushort *)sect_buf;
818 while (words--) {
819 __asm__ volatile ("eieio");
820 *pbuf = *dbuf++;
821 __asm__ volatile ("eieio");
822 }
823
824 if (words&1)
825 *pbuf = 0;
826}
wdenk88d2ba92003-06-23 18:12:28 +0000827# endif /* CONFIG_AMIGAONEG3SE */
wdenk9b7f3842003-10-09 20:09:04 +0000828#endif /* __PPC_ */
wdenk452cfd62002-11-19 11:04:11 +0000829
wdenk9b7f3842003-10-09 20:09:04 +0000830/* We only need to swap data if we are running on a big endian cpu. */
831/* But Au1x00 cpu:s already swaps data in big endian mode! */
832#if defined(__LITTLE_ENDIAN) || defined(CONFIG_AU1X00)
833#define input_swap_data(x,y,z) input_data(x,y,z)
834#else
wdenkc6097192002-11-03 00:24:07 +0000835static void
836input_swap_data(int dev, ulong *sect_buf, int words)
837{
wdenk2f99a692004-01-04 22:51:12 +0000838#ifndef CONFIG_BMS2003
wdenkc6097192002-11-03 00:24:07 +0000839 volatile ushort *pbuf = (ushort *)(ATA_CURR_BASE(dev)+ATA_DATA_REG);
840 ushort *dbuf = (ushort *)sect_buf;
841
842 while (words--) {
843 *dbuf++ = ld_le16(pbuf);
844 *dbuf++ = ld_le16(pbuf);
845 }
wdenk2f99a692004-01-04 22:51:12 +0000846#else /* CONFIG_BMS2003 */
847 uchar i;
848 volatile uchar *pbuf_even = (uchar *)(ATA_CURR_BASE(dev)+ATA_DATA_EVEN);
849 volatile uchar *pbuf_odd = (uchar *)(ATA_CURR_BASE(dev)+ATA_DATA_ODD);
850 ushort *dbuf = (ushort *)sect_buf;
851
852 while (words--) {
853 for (i=0; i<2; i++) {
854 *(((uchar *)(dbuf)) + 1) = *pbuf_even;
855 *(uchar *)dbuf = *pbuf_odd;
856 dbuf+=1;
857 }
858 }
859#endif /* CONFIG_BMS2003 */
wdenkc6097192002-11-03 00:24:07 +0000860}
wdenk9b7f3842003-10-09 20:09:04 +0000861#endif /* __LITTLE_ENDIAN || CONFIG_AU1X00 */
wdenk591dda52002-11-18 00:14:45 +0000862
863
wdenk591dda52002-11-18 00:14:45 +0000864#ifdef __PPC__
wdenkc6097192002-11-03 00:24:07 +0000865static void
866output_data(int dev, ulong *sect_buf, int words)
867{
wdenk2f99a692004-01-04 22:51:12 +0000868#ifndef CONFIG_BMS2003
wdenkc6097192002-11-03 00:24:07 +0000869 ushort *dbuf;
870 volatile ushort *pbuf;
871
872 pbuf = (ushort *)(ATA_CURR_BASE(dev)+ATA_DATA_REG);
873 dbuf = (ushort *)sect_buf;
874 while (words--) {
875 __asm__ volatile ("eieio");
876 *pbuf = *dbuf++;
877 __asm__ volatile ("eieio");
878 *pbuf = *dbuf++;
879 }
wdenk2f99a692004-01-04 22:51:12 +0000880#else /* CONFIG_BMS2003 */
881 uchar *dbuf;
882 volatile uchar *pbuf_even;
883 volatile uchar *pbuf_odd;
884
885 pbuf_even = (uchar *)(ATA_CURR_BASE(dev)+ATA_DATA_EVEN);
886 pbuf_odd = (uchar *)(ATA_CURR_BASE(dev)+ATA_DATA_ODD);
887 dbuf = (uchar *)sect_buf;
888 while (words--) {
889 __asm__ volatile ("eieio");
890 *pbuf_even = *dbuf++;
891 __asm__ volatile ("eieio");
892 *pbuf_odd = *dbuf++;
893 __asm__ volatile ("eieio");
894 *pbuf_even = *dbuf++;
895 __asm__ volatile ("eieio");
896 *pbuf_odd = *dbuf++;
897 }
898#endif /* CONFIG_BMS2003 */
wdenkc6097192002-11-03 00:24:07 +0000899}
wdenk591dda52002-11-18 00:14:45 +0000900#else /* ! __PPC__ */
901static void
902output_data(int dev, ulong *sect_buf, int words)
903{
wdenk9f837932003-10-09 19:00:25 +0000904 outsw(ATA_CURR_BASE(dev)+ATA_DATA_REG, sect_buf, words<<1);
wdenk591dda52002-11-18 00:14:45 +0000905}
906#endif /* __PPC__ */
wdenkc6097192002-11-03 00:24:07 +0000907
wdenk591dda52002-11-18 00:14:45 +0000908#ifdef __PPC__
wdenkc6097192002-11-03 00:24:07 +0000909static void
910input_data(int dev, ulong *sect_buf, int words)
911{
wdenk2f99a692004-01-04 22:51:12 +0000912#ifndef CONFIG_BMS2003
wdenkc6097192002-11-03 00:24:07 +0000913 ushort *dbuf;
914 volatile ushort *pbuf;
915
916 pbuf = (ushort *)(ATA_CURR_BASE(dev)+ATA_DATA_REG);
917 dbuf = (ushort *)sect_buf;
918 while (words--) {
919 __asm__ volatile ("eieio");
920 *dbuf++ = *pbuf;
921 __asm__ volatile ("eieio");
922 *dbuf++ = *pbuf;
923 }
wdenk2f99a692004-01-04 22:51:12 +0000924#else /* CONFIG_BMS2003 */
925 uchar *dbuf;
926 volatile uchar *pbuf_even;
927 volatile uchar *pbuf_odd;
928
929 pbuf_even = (uchar *)(ATA_CURR_BASE(dev)+ATA_DATA_EVEN);
930 pbuf_odd = (uchar *)(ATA_CURR_BASE(dev)+ATA_DATA_ODD);
931 dbuf = (uchar *)sect_buf;
932 while (words--) {
933 __asm__ volatile ("eieio");
934 *dbuf++ = *pbuf_even;
935 __asm__ volatile ("eieio");
936 *dbuf++ = *pbuf_odd;
937 __asm__ volatile ("eieio");
938 *dbuf++ = *pbuf_even;
939 __asm__ volatile ("eieio");
940 *dbuf++ = *pbuf_odd;
941 }
942#endif /* CONFIG_BMS2003 */
wdenkc6097192002-11-03 00:24:07 +0000943}
wdenk591dda52002-11-18 00:14:45 +0000944#else /* ! __PPC__ */
945static void
946input_data(int dev, ulong *sect_buf, int words)
947{
wdenk9f837932003-10-09 19:00:25 +0000948 insw(ATA_CURR_BASE(dev)+ATA_DATA_REG, sect_buf, words << 1);
wdenk591dda52002-11-18 00:14:45 +0000949}
950
951#endif /* __PPC__ */
wdenkc6097192002-11-03 00:24:07 +0000952
wdenk452cfd62002-11-19 11:04:11 +0000953#ifdef CONFIG_AMIGAONEG3SE
954static void
955input_data_short(int dev, ulong *sect_buf, int words)
956{
957 ushort *dbuf;
958 volatile ushort *pbuf;
959
960 pbuf = (ushort *)(ATA_CURR_BASE(dev)+ATA_DATA_REG);
961 dbuf = (ushort *)sect_buf;
962 while (words--) {
963 __asm__ volatile ("eieio");
964 *dbuf++ = *pbuf;
965 __asm__ volatile ("eieio");
966 }
967
968 if (words&1)
969 {
970 ushort dummy;
971 dummy = *pbuf;
972 }
973}
974#endif
975
wdenkc6097192002-11-03 00:24:07 +0000976/* -------------------------------------------------------------------------
977 */
978static void ide_ident (block_dev_desc_t *dev_desc)
979{
980 ulong iobuf[ATA_SECTORWORDS];
981 unsigned char c;
982 hd_driveid_t *iop = (hd_driveid_t *)iobuf;
983
wdenk452cfd62002-11-19 11:04:11 +0000984#ifdef CONFIG_AMIGAONEG3SE
985 int max_bus_scan;
986 int retries = 0;
987 char *s;
988 int do_retry = 0;
989#endif
990
wdenkc6097192002-11-03 00:24:07 +0000991#if 0
992 int mode, cycle_time;
993#endif
994 int device;
995 device=dev_desc->dev;
996 printf (" Device %d: ", device);
997
wdenk452cfd62002-11-19 11:04:11 +0000998#ifdef CONFIG_AMIGAONEG3SE
999 s = getenv("ide_maxbus");
1000 if (s) {
1001 max_bus_scan = simple_strtol(s, NULL, 10);
1002 } else {
1003 max_bus_scan = CFG_IDE_MAXBUS;
1004 }
1005 if (device >= max_bus_scan*2) {
1006 dev_desc->type=DEV_TYPE_UNKNOWN;
1007 return;
1008 }
1009#endif
1010
wdenkc6097192002-11-03 00:24:07 +00001011 ide_led (DEVICE_LED(device), 1); /* LED on */
1012 /* Select device
1013 */
wdenk591dda52002-11-18 00:14:45 +00001014 ide_outb (device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
wdenkc6097192002-11-03 00:24:07 +00001015 dev_desc->if_type=IF_TYPE_IDE;
1016#ifdef CONFIG_ATAPI
wdenk452cfd62002-11-19 11:04:11 +00001017
1018#ifdef CONFIG_AMIGAONEG3SE
1019 do_retry = 0;
1020 retries = 0;
1021
1022 /* Warning: This will be tricky to read */
1023 while (retries <= 1)
1024 {
1025#endif /* CONFIG_AMIGAONEG3SE */
1026
wdenkc6097192002-11-03 00:24:07 +00001027 /* check signature */
wdenk591dda52002-11-18 00:14:45 +00001028 if ((ide_inb(device,ATA_SECT_CNT) == 0x01) &&
1029 (ide_inb(device,ATA_SECT_NUM) == 0x01) &&
1030 (ide_inb(device,ATA_CYL_LOW) == 0x14) &&
1031 (ide_inb(device,ATA_CYL_HIGH) == 0xEB)) {
wdenkc6097192002-11-03 00:24:07 +00001032 /* ATAPI Signature found */
1033 dev_desc->if_type=IF_TYPE_ATAPI;
1034 /* Start Ident Command
1035 */
wdenk591dda52002-11-18 00:14:45 +00001036 ide_outb (device, ATA_COMMAND, ATAPI_CMD_IDENT);
wdenkc6097192002-11-03 00:24:07 +00001037 /*
1038 * Wait for completion - ATAPI devices need more time
1039 * to become ready
1040 */
1041 c = ide_wait (device, ATAPI_TIME_OUT);
1042 }
1043 else
1044#endif
1045 {
1046 /* Start Ident Command
1047 */
wdenk591dda52002-11-18 00:14:45 +00001048 ide_outb (device, ATA_COMMAND, ATA_CMD_IDENT);
wdenkc6097192002-11-03 00:24:07 +00001049
1050 /* Wait for completion
1051 */
1052 c = ide_wait (device, IDE_TIME_OUT);
1053 }
1054 ide_led (DEVICE_LED(device), 0); /* LED off */
1055
1056 if (((c & ATA_STAT_DRQ) == 0) ||
1057 ((c & (ATA_STAT_FAULT|ATA_STAT_ERR)) != 0) ) {
wdenk452cfd62002-11-19 11:04:11 +00001058#ifdef CONFIG_AMIGAONEG3SE
1059 if (retries == 0) {
1060 do_retry = 1;
1061 } else {
wdenk452cfd62002-11-19 11:04:11 +00001062 return;
1063 }
1064#else
wdenkc6097192002-11-03 00:24:07 +00001065 return;
wdenk452cfd62002-11-19 11:04:11 +00001066#endif /* CONFIG_AMIGAONEG3SE */
1067 }
1068
1069#ifdef CONFIG_AMIGAONEG3SE
1070 s = getenv("ide_doreset");
1071 if (s && strcmp(s, "on") == 0 && 1 == do_retry) {
1072 /* Need to soft reset the device in case it's an ATAPI... */
1073 PRINTF("Retrying...\n");
1074 ide_outb (device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
1075 udelay(100000);
1076 ide_outb (device, ATA_COMMAND, 0x08);
1077 udelay (100000); /* 100 ms */
1078 retries++;
1079 } else {
1080 retries = 100;
wdenkc6097192002-11-03 00:24:07 +00001081 }
wdenk452cfd62002-11-19 11:04:11 +00001082 } /* see above - ugly to read */
1083#endif /* CONFIG_AMIGAONEG3SE */
wdenkc6097192002-11-03 00:24:07 +00001084
1085 input_swap_data (device, iobuf, ATA_SECTORWORDS);
1086
1087 ident_cpy (dev_desc->revision, iop->fw_rev, sizeof(dev_desc->revision));
1088 ident_cpy (dev_desc->vendor, iop->model, sizeof(dev_desc->vendor));
1089 ident_cpy (dev_desc->product, iop->serial_no, sizeof(dev_desc->product));
1090
1091 if ((iop->config & 0x0080)==0x0080)
1092 dev_desc->removable = 1;
1093 else
1094 dev_desc->removable = 0;
1095
1096#if 0
1097 /*
1098 * Drive PIO mode autoselection
1099 */
1100 mode = iop->tPIO;
1101
1102 printf ("tPIO = 0x%02x = %d\n",mode, mode);
1103 if (mode > 2) { /* 2 is maximum allowed tPIO value */
1104 mode = 2;
1105 PRINTF ("Override tPIO -> 2\n");
1106 }
1107 if (iop->field_valid & 2) { /* drive implements ATA2? */
1108 PRINTF ("Drive implements ATA2\n");
1109 if (iop->capability & 8) { /* drive supports use_iordy? */
1110 cycle_time = iop->eide_pio_iordy;
1111 } else {
1112 cycle_time = iop->eide_pio;
1113 }
1114 PRINTF ("cycle time = %d\n", cycle_time);
1115 mode = 4;
1116 if (cycle_time > 120) mode = 3; /* 120 ns for PIO mode 4 */
1117 if (cycle_time > 180) mode = 2; /* 180 ns for PIO mode 3 */
1118 if (cycle_time > 240) mode = 1; /* 240 ns for PIO mode 4 */
1119 if (cycle_time > 383) mode = 0; /* 383 ns for PIO mode 4 */
1120 }
1121 printf ("PIO mode to use: PIO %d\n", mode);
1122#endif /* 0 */
1123
1124#ifdef CONFIG_ATAPI
1125 if (dev_desc->if_type==IF_TYPE_ATAPI) {
1126 atapi_inquiry(dev_desc);
1127 return;
1128 }
1129#endif /* CONFIG_ATAPI */
1130
1131 /* swap shorts */
1132 dev_desc->lba = (iop->lba_capacity << 16) | (iop->lba_capacity >> 16);
1133 /* assuming HD */
1134 dev_desc->type=DEV_TYPE_HARDDISK;
1135 dev_desc->blksz=ATA_BLOCKSIZE;
1136 dev_desc->lun=0; /* just to fill something in... */
1137
1138#if 0 /* only used to test the powersaving mode,
1139 * if enabled, the drive goes after 5 sec
1140 * in standby mode */
wdenk591dda52002-11-18 00:14:45 +00001141 ide_outb (device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
wdenkc6097192002-11-03 00:24:07 +00001142 c = ide_wait (device, IDE_TIME_OUT);
wdenk591dda52002-11-18 00:14:45 +00001143 ide_outb (device, ATA_SECT_CNT, 1);
1144 ide_outb (device, ATA_LBA_LOW, 0);
1145 ide_outb (device, ATA_LBA_MID, 0);
1146 ide_outb (device, ATA_LBA_HIGH, 0);
1147 ide_outb (device, ATA_DEV_HD, ATA_LBA |
wdenkc6097192002-11-03 00:24:07 +00001148 ATA_DEVICE(device));
wdenk591dda52002-11-18 00:14:45 +00001149 ide_outb (device, ATA_COMMAND, 0xe3);
wdenkc6097192002-11-03 00:24:07 +00001150 udelay (50);
1151 c = ide_wait (device, IDE_TIME_OUT); /* can't take over 500 ms */
1152#endif
1153}
1154
1155
1156/* ------------------------------------------------------------------------- */
1157
1158ulong ide_read (int device, ulong blknr, ulong blkcnt, ulong *buffer)
1159{
1160 ulong n = 0;
1161 unsigned char c;
1162 unsigned char pwrsave=0; /* power save */
1163
1164 PRINTF ("ide_read dev %d start %lX, blocks %lX buffer at %lX\n",
1165 device, blknr, blkcnt, (ulong)buffer);
1166
1167 ide_led (DEVICE_LED(device), 1); /* LED on */
1168
1169 /* Select device
1170 */
wdenk591dda52002-11-18 00:14:45 +00001171 ide_outb (device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
wdenkc6097192002-11-03 00:24:07 +00001172 c = ide_wait (device, IDE_TIME_OUT);
1173
1174 if (c & ATA_STAT_BUSY) {
1175 printf ("IDE read: device %d not ready\n", device);
1176 goto IDE_READ_E;
1177 }
1178
1179 /* first check if the drive is in Powersaving mode, if yes,
1180 * increase the timeout value */
wdenk591dda52002-11-18 00:14:45 +00001181 ide_outb (device, ATA_COMMAND, ATA_CMD_CHK_PWR);
wdenkc6097192002-11-03 00:24:07 +00001182 udelay (50);
1183
1184 c = ide_wait (device, IDE_TIME_OUT); /* can't take over 500 ms */
1185
1186 if (c & ATA_STAT_BUSY) {
1187 printf ("IDE read: device %d not ready\n", device);
1188 goto IDE_READ_E;
1189 }
1190 if ((c & ATA_STAT_ERR) == ATA_STAT_ERR) {
1191 printf ("No Powersaving mode %X\n", c);
1192 } else {
wdenk591dda52002-11-18 00:14:45 +00001193 c = ide_inb(device,ATA_SECT_CNT);
wdenkc6097192002-11-03 00:24:07 +00001194 PRINTF("Powersaving %02X\n",c);
1195 if(c==0)
1196 pwrsave=1;
1197 }
1198
1199
1200 while (blkcnt-- > 0) {
1201
1202 c = ide_wait (device, IDE_TIME_OUT);
1203
1204 if (c & ATA_STAT_BUSY) {
1205 printf ("IDE read: device %d not ready\n", device);
1206 break;
1207 }
1208
wdenk591dda52002-11-18 00:14:45 +00001209 ide_outb (device, ATA_SECT_CNT, 1);
1210 ide_outb (device, ATA_LBA_LOW, (blknr >> 0) & 0xFF);
1211 ide_outb (device, ATA_LBA_MID, (blknr >> 8) & 0xFF);
1212 ide_outb (device, ATA_LBA_HIGH, (blknr >> 16) & 0xFF);
1213 ide_outb (device, ATA_DEV_HD, ATA_LBA |
wdenkc6097192002-11-03 00:24:07 +00001214 ATA_DEVICE(device) |
1215 ((blknr >> 24) & 0xF) );
wdenk591dda52002-11-18 00:14:45 +00001216 ide_outb (device, ATA_COMMAND, ATA_CMD_READ);
wdenkc6097192002-11-03 00:24:07 +00001217
1218 udelay (50);
1219
1220 if(pwrsave) {
1221 c = ide_wait (device, IDE_SPIN_UP_TIME_OUT); /* may take up to 4 sec */
1222 pwrsave=0;
1223 } else {
1224 c = ide_wait (device, IDE_TIME_OUT); /* can't take over 500 ms */
1225 }
1226
1227 if ((c&(ATA_STAT_DRQ|ATA_STAT_BUSY|ATA_STAT_ERR)) != ATA_STAT_DRQ) {
1228 printf ("Error (no IRQ) dev %d blk %ld: status 0x%02x\n",
1229 device, blknr, c);
1230 break;
1231 }
1232
1233 input_data (device, buffer, ATA_SECTORWORDS);
wdenk591dda52002-11-18 00:14:45 +00001234 (void) ide_inb (device, ATA_STATUS); /* clear IRQ */
wdenkc6097192002-11-03 00:24:07 +00001235
1236 ++n;
1237 ++blknr;
1238 buffer += ATA_SECTORWORDS;
1239 }
1240IDE_READ_E:
1241 ide_led (DEVICE_LED(device), 0); /* LED off */
1242 return (n);
1243}
1244
1245/* ------------------------------------------------------------------------- */
1246
1247
1248ulong ide_write (int device, ulong blknr, ulong blkcnt, ulong *buffer)
1249{
1250 ulong n = 0;
1251 unsigned char c;
1252
1253 ide_led (DEVICE_LED(device), 1); /* LED on */
1254
1255 /* Select device
1256 */
wdenk591dda52002-11-18 00:14:45 +00001257 ide_outb (device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
wdenkc6097192002-11-03 00:24:07 +00001258
1259 while (blkcnt-- > 0) {
1260
1261 c = ide_wait (device, IDE_TIME_OUT);
1262
1263 if (c & ATA_STAT_BUSY) {
1264 printf ("IDE read: device %d not ready\n", device);
1265 goto WR_OUT;
1266 }
1267
wdenk591dda52002-11-18 00:14:45 +00001268 ide_outb (device, ATA_SECT_CNT, 1);
1269 ide_outb (device, ATA_LBA_LOW, (blknr >> 0) & 0xFF);
1270 ide_outb (device, ATA_LBA_MID, (blknr >> 8) & 0xFF);
1271 ide_outb (device, ATA_LBA_HIGH, (blknr >> 16) & 0xFF);
1272 ide_outb (device, ATA_DEV_HD, ATA_LBA |
wdenkc6097192002-11-03 00:24:07 +00001273 ATA_DEVICE(device) |
1274 ((blknr >> 24) & 0xF) );
wdenk591dda52002-11-18 00:14:45 +00001275 ide_outb (device, ATA_COMMAND, ATA_CMD_WRITE);
wdenkc6097192002-11-03 00:24:07 +00001276
1277 udelay (50);
1278
1279 c = ide_wait (device, IDE_TIME_OUT); /* can't take over 500 ms */
1280
1281 if ((c&(ATA_STAT_DRQ|ATA_STAT_BUSY|ATA_STAT_ERR)) != ATA_STAT_DRQ) {
1282 printf ("Error (no IRQ) dev %d blk %ld: status 0x%02x\n",
1283 device, blknr, c);
1284 goto WR_OUT;
1285 }
1286
1287 output_data (device, buffer, ATA_SECTORWORDS);
wdenk591dda52002-11-18 00:14:45 +00001288 c = ide_inb (device, ATA_STATUS); /* clear IRQ */
wdenkc6097192002-11-03 00:24:07 +00001289 ++n;
1290 ++blknr;
1291 buffer += ATA_SECTORWORDS;
1292 }
1293WR_OUT:
1294 ide_led (DEVICE_LED(device), 0); /* LED off */
1295 return (n);
1296}
1297
1298/* ------------------------------------------------------------------------- */
1299
1300/*
1301 * copy src to dest, skipping leading and trailing blanks and null
1302 * terminate the string
1303 */
1304static void ident_cpy (unsigned char *dest, unsigned char *src, unsigned int len)
1305{
1306 int start,end;
1307
1308 start=0;
1309 while (start<len) {
1310 if (src[start]!=' ')
1311 break;
1312 start++;
1313 }
1314 end=len-1;
1315 while (end>start) {
1316 if (src[end]!=' ')
1317 break;
1318 end--;
1319 }
1320 for ( ; start<=end; start++) {
1321 *dest++=src[start];
1322 }
1323 *dest='\0';
1324}
1325
1326/* ------------------------------------------------------------------------- */
1327
1328/*
1329 * Wait until Busy bit is off, or timeout (in ms)
1330 * Return last status
1331 */
1332static uchar ide_wait (int dev, ulong t)
1333{
1334 ulong delay = 10 * t; /* poll every 100 us */
1335 uchar c;
1336
wdenk591dda52002-11-18 00:14:45 +00001337 while ((c = ide_inb(dev, ATA_STATUS)) & ATA_STAT_BUSY) {
wdenkc6097192002-11-03 00:24:07 +00001338 udelay (100);
1339 if (delay-- == 0) {
1340 break;
1341 }
1342 }
1343 return (c);
1344}
1345
1346/* ------------------------------------------------------------------------- */
1347
1348#ifdef CONFIG_IDE_RESET
1349extern void ide_set_reset(int idereset);
1350
1351static void ide_reset (void)
1352{
1353#if defined(CFG_PB_12V_ENABLE) || defined(CFG_PB_IDE_MOTOR)
1354 volatile immap_t *immr = (immap_t *)CFG_IMMR;
1355#endif
1356 int i;
1357
1358 curr_device = -1;
1359 for (i=0; i<CFG_IDE_MAXBUS; ++i)
1360 ide_bus_ok[i] = 0;
1361 for (i=0; i<CFG_IDE_MAXDEVICE; ++i)
1362 ide_dev_desc[i].type = DEV_TYPE_UNKNOWN;
1363
1364 ide_set_reset (1); /* assert reset */
1365
1366 WATCHDOG_RESET();
1367
1368#ifdef CFG_PB_12V_ENABLE
1369 immr->im_cpm.cp_pbdat &= ~(CFG_PB_12V_ENABLE); /* 12V Enable output OFF */
1370 immr->im_cpm.cp_pbpar &= ~(CFG_PB_12V_ENABLE);
1371 immr->im_cpm.cp_pbodr &= ~(CFG_PB_12V_ENABLE);
1372 immr->im_cpm.cp_pbdir |= CFG_PB_12V_ENABLE;
1373
1374 /* wait 500 ms for the voltage to stabilize
1375 */
1376 for (i=0; i<500; ++i) {
1377 udelay (1000);
1378 }
1379
1380 immr->im_cpm.cp_pbdat |= CFG_PB_12V_ENABLE; /* 12V Enable output ON */
1381#endif /* CFG_PB_12V_ENABLE */
1382
1383#ifdef CFG_PB_IDE_MOTOR
1384 /* configure IDE Motor voltage monitor pin as input */
1385 immr->im_cpm.cp_pbpar &= ~(CFG_PB_IDE_MOTOR);
1386 immr->im_cpm.cp_pbodr &= ~(CFG_PB_IDE_MOTOR);
1387 immr->im_cpm.cp_pbdir &= ~(CFG_PB_IDE_MOTOR);
1388
1389 /* wait up to 1 s for the motor voltage to stabilize
1390 */
1391 for (i=0; i<1000; ++i) {
1392 if ((immr->im_cpm.cp_pbdat & CFG_PB_IDE_MOTOR) != 0) {
1393 break;
1394 }
1395 udelay (1000);
1396 }
1397
1398 if (i == 1000) { /* Timeout */
1399 printf ("\nWarning: 5V for IDE Motor missing\n");
1400# ifdef CONFIG_STATUS_LED
1401# ifdef STATUS_LED_YELLOW
1402 status_led_set (STATUS_LED_YELLOW, STATUS_LED_ON );
1403# endif
1404# ifdef STATUS_LED_GREEN
1405 status_led_set (STATUS_LED_GREEN, STATUS_LED_OFF);
1406# endif
1407# endif /* CONFIG_STATUS_LED */
1408 }
1409#endif /* CFG_PB_IDE_MOTOR */
1410
1411 WATCHDOG_RESET();
1412
1413 /* de-assert RESET signal */
1414 ide_set_reset(0);
1415
1416 /* wait 250 ms */
1417 for (i=0; i<250; ++i) {
1418 udelay (1000);
1419 }
1420}
1421
1422#endif /* CONFIG_IDE_RESET */
1423
1424/* ------------------------------------------------------------------------- */
1425
wdenk90e7e422002-12-04 23:39:58 +00001426#if defined(CONFIG_IDE_LED) && !defined(CONFIG_AMIGAONEG3SE) && !defined(CONFIG_KUP4K)
wdenkc6097192002-11-03 00:24:07 +00001427
1428static uchar led_buffer = 0; /* Buffer for current LED status */
1429
1430static void ide_led (uchar led, uchar status)
1431{
1432 uchar *led_port = LED_PORT;
1433
1434 if (status) { /* switch LED on */
1435 led_buffer |= led;
1436 } else { /* switch LED off */
1437 led_buffer &= ~led;
1438 }
1439
1440 *led_port = led_buffer;
1441}
1442
1443#endif /* CONFIG_IDE_LED */
1444
1445/* ------------------------------------------------------------------------- */
1446
1447#ifdef CONFIG_ATAPI
1448/****************************************************************************
1449 * ATAPI Support
1450 */
1451
1452
wdenkc6097192002-11-03 00:24:07 +00001453#undef ATAPI_DEBUG
1454
1455#ifdef ATAPI_DEBUG
1456#define AT_PRINTF(fmt,args...) printf (fmt ,##args)
1457#else
1458#define AT_PRINTF(fmt,args...)
1459#endif
1460
wdenk591dda52002-11-18 00:14:45 +00001461#ifdef __PPC__
wdenkc6097192002-11-03 00:24:07 +00001462/* since ATAPI may use commands with not 4 bytes alligned length
1463 * we have our own transfer functions, 2 bytes alligned */
1464static void
1465output_data_shorts(int dev, ushort *sect_buf, int shorts)
1466{
wdenk2f99a692004-01-04 22:51:12 +00001467#ifndef CONFIG_BMS2003
wdenkc6097192002-11-03 00:24:07 +00001468 ushort *dbuf;
1469 volatile ushort *pbuf;
1470
1471 pbuf = (ushort *)(ATA_CURR_BASE(dev)+ATA_DATA_REG);
1472 dbuf = (ushort *)sect_buf;
1473 while (shorts--) {
1474 __asm__ volatile ("eieio");
1475 *pbuf = *dbuf++;
1476 }
wdenk2f99a692004-01-04 22:51:12 +00001477#else /* CONFIG_BMS2003 */
1478 uchar *dbuf;
1479 volatile uchar *pbuf_even;
1480 volatile uchar *pbuf_odd;
1481
1482 pbuf_even = (uchar *)(ATA_CURR_BASE(dev)+ATA_DATA_EVEN);
1483 pbuf_odd = (uchar *)(ATA_CURR_BASE(dev)+ATA_DATA_ODD);
1484 while (shorts--) {
1485 __asm__ volatile ("eieio");
1486 *pbuf_even = *dbuf++;
1487 __asm__ volatile ("eieio");
1488 *pbuf_odd = *dbuf++;
1489 }
1490#endif /* CONFIG_BMS2003 */
wdenkc6097192002-11-03 00:24:07 +00001491}
1492
1493static void
1494input_data_shorts(int dev, ushort *sect_buf, int shorts)
1495{
wdenk2f99a692004-01-04 22:51:12 +00001496#ifndef CONFIG_BMS2003
wdenkc6097192002-11-03 00:24:07 +00001497 ushort *dbuf;
1498 volatile ushort *pbuf;
1499
1500 pbuf = (ushort *)(ATA_CURR_BASE(dev)+ATA_DATA_REG);
1501 dbuf = (ushort *)sect_buf;
1502 while (shorts--) {
1503 __asm__ volatile ("eieio");
1504 *dbuf++ = *pbuf;
1505 }
wdenk2f99a692004-01-04 22:51:12 +00001506#else /* CONFIG_BMS2003 */
1507 uchar *dbuf;
1508 volatile uchar *pbuf_even;
1509 volatile uchar *pbuf_odd;
1510
1511 pbuf_even = (uchar *)(ATA_CURR_BASE(dev)+ATA_DATA_EVEN);
1512 pbuf_odd = (uchar *)(ATA_CURR_BASE(dev)+ATA_DATA_ODD);
1513 while (shorts--) {
1514 __asm__ volatile ("eieio");
1515 *dbuf++ = *pbuf_even;
1516 __asm__ volatile ("eieio");
1517 *dbuf++ = *pbuf_odd;
1518 }
1519#endif /* CONFIG_BMS2003 */
wdenkc6097192002-11-03 00:24:07 +00001520}
1521
wdenk591dda52002-11-18 00:14:45 +00001522#else /* ! __PPC__ */
1523static void
1524output_data_shorts(int dev, ushort *sect_buf, int shorts)
1525{
wdenk9f837932003-10-09 19:00:25 +00001526 outsw(ATA_CURR_BASE(dev)+ATA_DATA_REG, sect_buf, shorts);
wdenk591dda52002-11-18 00:14:45 +00001527}
1528
1529
1530static void
1531input_data_shorts(int dev, ushort *sect_buf, int shorts)
1532{
wdenk9f837932003-10-09 19:00:25 +00001533 insw(ATA_CURR_BASE(dev)+ATA_DATA_REG, sect_buf, shorts);
wdenk591dda52002-11-18 00:14:45 +00001534}
1535
1536#endif /* __PPC__ */
1537
wdenkc6097192002-11-03 00:24:07 +00001538/*
1539 * Wait until (Status & mask) == res, or timeout (in ms)
1540 * Return last status
1541 * This is used since some ATAPI CD ROMs clears their Busy Bit first
1542 * and then they set their DRQ Bit
1543 */
1544static uchar atapi_wait_mask (int dev, ulong t,uchar mask, uchar res)
1545{
1546 ulong delay = 10 * t; /* poll every 100 us */
1547 uchar c;
1548
wdenk591dda52002-11-18 00:14:45 +00001549 c = ide_inb(dev,ATA_DEV_CTL); /* prevents to read the status before valid */
1550 while (((c = ide_inb(dev, ATA_STATUS)) & mask) != res) {
wdenkc6097192002-11-03 00:24:07 +00001551 /* break if error occurs (doesn't make sense to wait more) */
1552 if((c & ATA_STAT_ERR)==ATA_STAT_ERR)
1553 break;
1554 udelay (100);
1555 if (delay-- == 0) {
1556 break;
1557 }
1558 }
1559 return (c);
1560}
1561
1562/*
1563 * issue an atapi command
1564 */
1565unsigned char atapi_issue(int device,unsigned char* ccb,int ccblen, unsigned char * buffer,int buflen)
1566{
1567 unsigned char c,err,mask,res;
1568 int n;
1569 ide_led (DEVICE_LED(device), 1); /* LED on */
1570
1571 /* Select device
1572 */
1573 mask = ATA_STAT_BUSY|ATA_STAT_DRQ;
1574 res = 0;
wdenk452cfd62002-11-19 11:04:11 +00001575#ifdef CONFIG_AMIGAONEG3SE
1576# warning THF: Removed LBA mode ???
1577#endif
wdenk591dda52002-11-18 00:14:45 +00001578 ide_outb (device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
wdenkc6097192002-11-03 00:24:07 +00001579 c = atapi_wait_mask(device,ATAPI_TIME_OUT,mask,res);
1580 if ((c & mask) != res) {
1581 printf ("ATAPI_ISSUE: device %d not ready status %X\n", device,c);
1582 err=0xFF;
1583 goto AI_OUT;
1584 }
1585 /* write taskfile */
wdenk591dda52002-11-18 00:14:45 +00001586 ide_outb (device, ATA_ERROR_REG, 0); /* no DMA, no overlaped */
wdenk452cfd62002-11-19 11:04:11 +00001587 ide_outb (device, ATA_SECT_CNT, 0);
1588 ide_outb (device, ATA_SECT_NUM, 0);
wdenk591dda52002-11-18 00:14:45 +00001589 ide_outb (device, ATA_CYL_LOW, (unsigned char)(buflen & 0xFF));
wdenk452cfd62002-11-19 11:04:11 +00001590 ide_outb (device, ATA_CYL_HIGH, (unsigned char)((buflen>>8) & 0xFF));
1591#ifdef CONFIG_AMIGAONEG3SE
1592# warning THF: Removed LBA mode ???
1593#endif
wdenk591dda52002-11-18 00:14:45 +00001594 ide_outb (device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
wdenkc6097192002-11-03 00:24:07 +00001595
wdenk591dda52002-11-18 00:14:45 +00001596 ide_outb (device, ATA_COMMAND, ATAPI_CMD_PACKET);
wdenkc6097192002-11-03 00:24:07 +00001597 udelay (50);
1598
1599 mask = ATA_STAT_DRQ|ATA_STAT_BUSY|ATA_STAT_ERR;
1600 res = ATA_STAT_DRQ;
1601 c = atapi_wait_mask(device,ATAPI_TIME_OUT,mask,res);
1602
1603 if ((c & mask) != res) { /* DRQ must be 1, BSY 0 */
1604 printf ("ATTAPI_ISSUE: Error (no IRQ) before sending ccb dev %d status 0x%02x\n",device,c);
1605 err=0xFF;
1606 goto AI_OUT;
1607 }
1608
1609 output_data_shorts (device, (unsigned short *)ccb,ccblen/2); /* write command block */
1610 /* ATAPI Command written wait for completition */
1611 udelay (5000); /* device must set bsy */
1612
1613 mask = ATA_STAT_DRQ|ATA_STAT_BUSY|ATA_STAT_ERR;
1614 /* if no data wait for DRQ = 0 BSY = 0
1615 * if data wait for DRQ = 1 BSY = 0 */
1616 res=0;
1617 if(buflen)
1618 res = ATA_STAT_DRQ;
1619 c = atapi_wait_mask(device,ATAPI_TIME_OUT,mask,res);
1620 if ((c & mask) != res ) {
1621 if (c & ATA_STAT_ERR) {
wdenk591dda52002-11-18 00:14:45 +00001622 err=(ide_inb(device,ATA_ERROR_REG))>>4;
wdenkc6097192002-11-03 00:24:07 +00001623 AT_PRINTF("atapi_issue 1 returned sense key %X status %02X\n",err,c);
1624 } else {
1625 printf ("ATTAPI_ISSUE: (no DRQ) after sending ccb (%x) status 0x%02x\n", ccb[0],c);
1626 err=0xFF;
1627 }
1628 goto AI_OUT;
1629 }
wdenk591dda52002-11-18 00:14:45 +00001630 n=ide_inb(device, ATA_CYL_HIGH);
wdenkc6097192002-11-03 00:24:07 +00001631 n<<=8;
wdenk591dda52002-11-18 00:14:45 +00001632 n+=ide_inb(device, ATA_CYL_LOW);
wdenkc6097192002-11-03 00:24:07 +00001633 if(n>buflen) {
1634 printf("ERROR, transfer bytes %d requested only %d\n",n,buflen);
1635 err=0xff;
1636 goto AI_OUT;
1637 }
1638 if((n==0)&&(buflen<0)) {
1639 printf("ERROR, transfer bytes %d requested %d\n",n,buflen);
1640 err=0xff;
1641 goto AI_OUT;
1642 }
1643 if(n!=buflen) {
1644 AT_PRINTF("WARNING, transfer bytes %d not equal with requested %d\n",n,buflen);
1645 }
1646 if(n!=0) { /* data transfer */
1647 AT_PRINTF("ATAPI_ISSUE: %d Bytes to transfer\n",n);
1648 /* we transfer shorts */
1649 n>>=1;
1650 /* ok now decide if it is an in or output */
wdenk591dda52002-11-18 00:14:45 +00001651 if ((ide_inb(device, ATA_SECT_CNT)&0x02)==0) {
wdenkc6097192002-11-03 00:24:07 +00001652 AT_PRINTF("Write to device\n");
1653 output_data_shorts(device,(unsigned short *)buffer,n);
1654 } else {
1655 AT_PRINTF("Read from device @ %p shorts %d\n",buffer,n);
1656 input_data_shorts(device,(unsigned short *)buffer,n);
1657 }
1658 }
1659 udelay(5000); /* seems that some CD ROMs need this... */
1660 mask = ATA_STAT_BUSY|ATA_STAT_ERR;
1661 res=0;
1662 c = atapi_wait_mask(device,ATAPI_TIME_OUT,mask,res);
1663 if ((c & ATA_STAT_ERR) == ATA_STAT_ERR) {
wdenk591dda52002-11-18 00:14:45 +00001664 err=(ide_inb(device,ATA_ERROR_REG) >> 4);
wdenkc6097192002-11-03 00:24:07 +00001665 AT_PRINTF("atapi_issue 2 returned sense key %X status %X\n",err,c);
1666 } else {
1667 err = 0;
1668 }
1669AI_OUT:
1670 ide_led (DEVICE_LED(device), 0); /* LED off */
1671 return (err);
1672}
1673
1674/*
1675 * sending the command to atapi_issue. If an status other than good
1676 * returns, an request_sense will be issued
1677 */
1678
1679#define ATAPI_DRIVE_NOT_READY 100
1680#define ATAPI_UNIT_ATTN 10
1681
1682unsigned char atapi_issue_autoreq (int device,
1683 unsigned char* ccb,
1684 int ccblen,
1685 unsigned char *buffer,
1686 int buflen)
1687{
1688 unsigned char sense_data[18],sense_ccb[12];
1689 unsigned char res,key,asc,ascq;
1690 int notready,unitattn;
1691
wdenk452cfd62002-11-19 11:04:11 +00001692#ifdef CONFIG_AMIGAONEG3SE
1693 char *s;
1694 unsigned int timeout, retrycnt;
1695
1696 s = getenv("ide_cd_timeout");
1697 timeout = s ? (simple_strtol(s, NULL, 10)*1000000)/5 : 0;
1698
1699 retrycnt = 0;
1700#endif
1701
wdenkc6097192002-11-03 00:24:07 +00001702 unitattn=ATAPI_UNIT_ATTN;
1703 notready=ATAPI_DRIVE_NOT_READY;
1704
1705retry:
1706 res= atapi_issue(device,ccb,ccblen,buffer,buflen);
1707 if (res==0)
1708 return (0); /* Ok */
1709
1710 if (res==0xFF)
1711 return (0xFF); /* error */
1712
1713 AT_PRINTF("(auto_req)atapi_issue returned sense key %X\n",res);
1714
1715 memset(sense_ccb,0,sizeof(sense_ccb));
1716 memset(sense_data,0,sizeof(sense_data));
1717 sense_ccb[0]=ATAPI_CMD_REQ_SENSE;
wdenk452cfd62002-11-19 11:04:11 +00001718 sense_ccb[4]=18; /* allocation Length */
wdenkc6097192002-11-03 00:24:07 +00001719
1720 res=atapi_issue(device,sense_ccb,12,sense_data,18);
1721 key=(sense_data[2]&0xF);
1722 asc=(sense_data[12]);
1723 ascq=(sense_data[13]);
1724
1725 AT_PRINTF("ATAPI_CMD_REQ_SENSE returned %x\n",res);
1726 AT_PRINTF(" Sense page: %02X key %02X ASC %02X ASCQ %02X\n",
1727 sense_data[0],
1728 key,
1729 asc,
1730 ascq);
1731
1732 if((key==0))
1733 return 0; /* ok device ready */
1734
1735 if((key==6)|| (asc==0x29) || (asc==0x28)) { /* Unit Attention */
1736 if(unitattn-->0) {
1737 udelay(200*1000);
1738 goto retry;
1739 }
1740 printf("Unit Attention, tried %d\n",ATAPI_UNIT_ATTN);
1741 goto error;
1742 }
1743 if((asc==0x4) && (ascq==0x1)) { /* not ready, but will be ready soon */
1744 if (notready-->0) {
1745 udelay(200*1000);
1746 goto retry;
1747 }
1748 printf("Drive not ready, tried %d times\n",ATAPI_DRIVE_NOT_READY);
1749 goto error;
1750 }
1751 if(asc==0x3a) {
1752 AT_PRINTF("Media not present\n");
1753 goto error;
1754 }
wdenk452cfd62002-11-19 11:04:11 +00001755
1756#ifdef CONFIG_AMIGAONEG3SE
1757 if ((sense_data[2]&0xF)==0x0B) {
1758 AT_PRINTF("ABORTED COMMAND...retry\n");
1759 if (retrycnt++ < 4)
1760 goto retry;
1761 return (0xFF);
1762 }
1763
1764 if ((sense_data[2]&0xf) == 0x02 &&
1765 sense_data[12] == 0x04 &&
1766 sense_data[13] == 0x01 ) {
1767 AT_PRINTF("Waiting for unit to become active\n");
1768 udelay(timeout);
1769 if (retrycnt++ < 4)
1770 goto retry;
1771 return 0xFF;
1772 }
1773#endif /* CONFIG_AMIGAONEG3SE */
1774
wdenkc6097192002-11-03 00:24:07 +00001775 printf ("ERROR: Unknown Sense key %02X ASC %02X ASCQ %02X\n",key,asc,ascq);
1776error:
1777 AT_PRINTF ("ERROR Sense key %02X ASC %02X ASCQ %02X\n",key,asc,ascq);
1778 return (0xFF);
1779}
1780
1781
wdenkc6097192002-11-03 00:24:07 +00001782static void atapi_inquiry(block_dev_desc_t * dev_desc)
1783{
1784 unsigned char ccb[12]; /* Command descriptor block */
1785 unsigned char iobuf[64]; /* temp buf */
1786 unsigned char c;
1787 int device;
1788
1789 device=dev_desc->dev;
1790 dev_desc->type=DEV_TYPE_UNKNOWN; /* not yet valid */
1791 dev_desc->block_read=atapi_read;
1792
1793 memset(ccb,0,sizeof(ccb));
1794 memset(iobuf,0,sizeof(iobuf));
1795
1796 ccb[0]=ATAPI_CMD_INQUIRY;
1797 ccb[4]=40; /* allocation Legnth */
1798 c=atapi_issue_autoreq(device,ccb,12,(unsigned char *)iobuf,40);
1799
1800 AT_PRINTF("ATAPI_CMD_INQUIRY returned %x\n",c);
1801 if (c!=0)
1802 return;
1803
1804 /* copy device ident strings */
1805 ident_cpy(dev_desc->vendor,&iobuf[8],8);
1806 ident_cpy(dev_desc->product,&iobuf[16],16);
1807 ident_cpy(dev_desc->revision,&iobuf[32],5);
1808
1809 dev_desc->lun=0;
1810 dev_desc->lba=0;
1811 dev_desc->blksz=0;
1812 dev_desc->type=iobuf[0] & 0x1f;
1813
1814 if ((iobuf[1]&0x80)==0x80)
1815 dev_desc->removable = 1;
1816 else
1817 dev_desc->removable = 0;
1818
1819 memset(ccb,0,sizeof(ccb));
1820 memset(iobuf,0,sizeof(iobuf));
1821 ccb[0]=ATAPI_CMD_START_STOP;
1822 ccb[4]=0x03; /* start */
1823
1824 c=atapi_issue_autoreq(device,ccb,12,(unsigned char *)iobuf,0);
1825
1826 AT_PRINTF("ATAPI_CMD_START_STOP returned %x\n",c);
1827 if (c!=0)
1828 return;
1829
1830 memset(ccb,0,sizeof(ccb));
1831 memset(iobuf,0,sizeof(iobuf));
1832 c=atapi_issue_autoreq(device,ccb,12,(unsigned char *)iobuf,0);
1833
1834 AT_PRINTF("ATAPI_CMD_UNIT_TEST_READY returned %x\n",c);
1835 if (c!=0)
1836 return;
1837
1838 memset(ccb,0,sizeof(ccb));
1839 memset(iobuf,0,sizeof(iobuf));
1840 ccb[0]=ATAPI_CMD_READ_CAP;
1841 c=atapi_issue_autoreq(device,ccb,12,(unsigned char *)iobuf,8);
1842 AT_PRINTF("ATAPI_CMD_READ_CAP returned %x\n",c);
1843 if (c!=0)
1844 return;
1845
1846 AT_PRINTF("Read Cap: LBA %02X%02X%02X%02X blksize %02X%02X%02X%02X\n",
1847 iobuf[0],iobuf[1],iobuf[2],iobuf[3],
1848 iobuf[4],iobuf[5],iobuf[6],iobuf[7]);
1849
1850 dev_desc->lba =((unsigned long)iobuf[0]<<24) +
1851 ((unsigned long)iobuf[1]<<16) +
1852 ((unsigned long)iobuf[2]<< 8) +
1853 ((unsigned long)iobuf[3]);
1854 dev_desc->blksz=((unsigned long)iobuf[4]<<24) +
1855 ((unsigned long)iobuf[5]<<16) +
1856 ((unsigned long)iobuf[6]<< 8) +
1857 ((unsigned long)iobuf[7]);
1858 return;
1859}
1860
1861
1862/*
1863 * atapi_read:
1864 * we transfer only one block per command, since the multiple DRQ per
1865 * command is not yet implemented
1866 */
1867#define ATAPI_READ_MAX_BYTES 2048 /* we read max 2kbytes */
1868#define ATAPI_READ_BLOCK_SIZE 2048 /* assuming CD part */
1869#define ATAPI_READ_MAX_BLOCK ATAPI_READ_MAX_BYTES/ATAPI_READ_BLOCK_SIZE /* max blocks */
1870
1871ulong atapi_read (int device, ulong blknr, ulong blkcnt, ulong *buffer)
1872{
1873 ulong n = 0;
1874 unsigned char ccb[12]; /* Command descriptor block */
1875 ulong cnt;
1876
1877 AT_PRINTF("atapi_read dev %d start %lX, blocks %lX buffer at %lX\n",
1878 device, blknr, blkcnt, (ulong)buffer);
1879
1880 do {
1881 if (blkcnt>ATAPI_READ_MAX_BLOCK) {
1882 cnt=ATAPI_READ_MAX_BLOCK;
1883 } else {
1884 cnt=blkcnt;
1885 }
1886 ccb[0]=ATAPI_CMD_READ_12;
1887 ccb[1]=0; /* reserved */
1888 ccb[2]=(unsigned char) (blknr>>24) & 0xFF; /* MSB Block */
1889 ccb[3]=(unsigned char) (blknr>>16) & 0xFF; /* */
1890 ccb[4]=(unsigned char) (blknr>> 8) & 0xFF;
1891 ccb[5]=(unsigned char) blknr & 0xFF; /* LSB Block */
1892 ccb[6]=(unsigned char) (cnt >>24) & 0xFF; /* MSB Block count */
1893 ccb[7]=(unsigned char) (cnt >>16) & 0xFF;
1894 ccb[8]=(unsigned char) (cnt >> 8) & 0xFF;
1895 ccb[9]=(unsigned char) cnt & 0xFF; /* LSB Block */
1896 ccb[10]=0; /* reserved */
1897 ccb[11]=0; /* reserved */
1898
1899 if (atapi_issue_autoreq(device,ccb,12,
1900 (unsigned char *)buffer,
1901 cnt*ATAPI_READ_BLOCK_SIZE) == 0xFF) {
1902 return (n);
1903 }
1904 n+=cnt;
1905 blkcnt-=cnt;
1906 blknr+=cnt;
1907 buffer+=cnt*(ATAPI_READ_BLOCK_SIZE/4); /* ulong blocksize in ulong */
1908 } while (blkcnt > 0);
1909 return (n);
1910}
1911
1912/* ------------------------------------------------------------------------- */
1913
1914#endif /* CONFIG_ATAPI */
1915
wdenkf287a242003-07-01 21:06:45 +00001916U_BOOT_CMD(
1917 ide, 5, 1, do_ide,
wdenk57b2d802003-06-27 21:31:46 +00001918 "ide - IDE sub-system\n",
1919 "reset - reset IDE controller\n"
1920 "ide info - show available IDE devices\n"
1921 "ide device [dev] - show or set current device\n"
1922 "ide part [dev] - print partition table of one or all IDE devices\n"
1923 "ide read addr blk# cnt\n"
1924 "ide write addr blk# cnt - read/write `cnt'"
1925 " blocks starting at block `blk#'\n"
1926 " to/from memory address `addr'\n"
1927);
1928
wdenkf287a242003-07-01 21:06:45 +00001929U_BOOT_CMD(
1930 diskboot, 3, 1, do_diskboot,
wdenk57b2d802003-06-27 21:31:46 +00001931 "diskboot- boot from IDE device\n",
1932 "loadAddr dev:part\n"
1933);
1934
wdenkc6097192002-11-03 00:24:07 +00001935#endif /* CONFIG_COMMANDS & CFG_CMD_IDE */