blob: 5bde5006a6a50411933561cfe0ccb1d7b5987a47 [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
wdenk132ba5f2004-02-27 08:20:54 +000040#ifdef CONFIG_MPC5xxx
41#include <mpc5xxx.h>
42#endif
wdenkc6097192002-11-03 00:24:07 +000043#include <ide.h>
44#include <ata.h>
wdenkc6097192002-11-03 00:24:07 +000045#ifdef CONFIG_STATUS_LED
46# include <status_led.h>
47#endif
wdenk15647dc2003-10-09 19:00:25 +000048#ifndef __PPC__
wdenk2262cfe2002-11-18 00:14:45 +000049#include <asm/io.h>
wdenk15647dc2003-10-09 19:00:25 +000050#ifdef __MIPS__
51/* Macros depend on this variable */
52static unsigned long mips_io_port_base = 0;
53#endif
wdenk2262cfe2002-11-18 00:14:45 +000054#endif
wdenkc6097192002-11-03 00:24:07 +000055
56#ifdef CONFIG_SHOW_BOOT_PROGRESS
57# include <status_led.h>
58# define SHOW_BOOT_PROGRESS(arg) show_boot_progress(arg)
59#else
60# define SHOW_BOOT_PROGRESS(arg)
61#endif
62
63
64#undef IDE_DEBUG
65
wdenkc7de8292002-11-19 11:04:11 +000066
wdenkc6097192002-11-03 00:24:07 +000067#ifdef IDE_DEBUG
68#define PRINTF(fmt,args...) printf (fmt ,##args)
69#else
70#define PRINTF(fmt,args...)
71#endif
72
73#if (CONFIG_COMMANDS & CFG_CMD_IDE)
74
wdenk15647dc2003-10-09 19:00:25 +000075#ifdef CONFIG_IDE_8xx_DIRECT
wdenkc6097192002-11-03 00:24:07 +000076/* Timings for IDE Interface
77 *
78 * SETUP / LENGTH / HOLD - cycles valid for 50 MHz clk
79 * 70 165 30 PIO-Mode 0, [ns]
80 * 4 9 2 [Cycles]
81 * 50 125 20 PIO-Mode 1, [ns]
82 * 3 7 2 [Cycles]
83 * 30 100 15 PIO-Mode 2, [ns]
84 * 2 6 1 [Cycles]
85 * 30 80 10 PIO-Mode 3, [ns]
86 * 2 5 1 [Cycles]
87 * 25 70 10 PIO-Mode 4, [ns]
88 * 2 4 1 [Cycles]
89 */
90
91const static pio_config_t pio_config_ns [IDE_MAX_PIO_MODE+1] =
92{
93 /* Setup Length Hold */
94 { 70, 165, 30 }, /* PIO-Mode 0, [ns] */
95 { 50, 125, 20 }, /* PIO-Mode 1, [ns] */
96 { 30, 101, 15 }, /* PIO-Mode 2, [ns] */
97 { 30, 80, 10 }, /* PIO-Mode 3, [ns] */
98 { 25, 70, 10 }, /* PIO-Mode 4, [ns] */
99};
100
101static pio_config_t pio_config_clk [IDE_MAX_PIO_MODE+1];
102
103#ifndef CFG_PIO_MODE
104#define CFG_PIO_MODE 0 /* use a relaxed default */
105#endif
106static int pio_mode = CFG_PIO_MODE;
107
108/* Make clock cycles and always round up */
109
110#define PCMCIA_MK_CLKS( t, T ) (( (t) * (T) + 999U ) / 1000U )
111
wdenk15647dc2003-10-09 19:00:25 +0000112#endif /* CONFIG_IDE_8xx_DIRECT */
113
wdenkc6097192002-11-03 00:24:07 +0000114/* ------------------------------------------------------------------------- */
115
116/* Current I/O Device */
117static int curr_device = -1;
118
119/* Current offset for IDE0 / IDE1 bus access */
120ulong ide_bus_offset[CFG_IDE_MAXBUS] = {
121#if defined(CFG_ATA_IDE0_OFFSET)
122 CFG_ATA_IDE0_OFFSET,
123#endif
124#if defined(CFG_ATA_IDE1_OFFSET) && (CFG_IDE_MAXBUS > 1)
125 CFG_ATA_IDE1_OFFSET,
126#endif
127};
128
wdenk15647dc2003-10-09 19:00:25 +0000129
wdenkc6097192002-11-03 00:24:07 +0000130#define ATA_CURR_BASE(dev) (CFG_ATA_BASE_ADDR+ide_bus_offset[IDE_BUS(dev)])
131
wdenkc7de8292002-11-19 11:04:11 +0000132#ifndef CONFIG_AMIGAONEG3SE
wdenkc6097192002-11-03 00:24:07 +0000133static int ide_bus_ok[CFG_IDE_MAXBUS];
wdenkc7de8292002-11-19 11:04:11 +0000134#else
135static int ide_bus_ok[CFG_IDE_MAXBUS] = {0,};
136#endif
wdenkc6097192002-11-03 00:24:07 +0000137
138static block_dev_desc_t ide_dev_desc[CFG_IDE_MAXDEVICE];
139/* ------------------------------------------------------------------------- */
140
141#ifdef CONFIG_IDE_LED
wdenk1c437712004-01-16 00:30:56 +0000142#if !defined(CONFIG_KUP4K) && !defined(CONFIG_BMS2003)
wdenkc6097192002-11-03 00:24:07 +0000143static void ide_led (uchar led, uchar status);
144#else
wdenk1f53a412002-12-04 23:39:58 +0000145extern void ide_led (uchar led, uchar status);
146#endif
147#else
wdenkc7de8292002-11-19 11:04:11 +0000148#ifndef CONFIG_AMIGAONEG3SE
wdenkc6097192002-11-03 00:24:07 +0000149#define ide_led(a,b) /* dummy */
wdenkc7de8292002-11-19 11:04:11 +0000150#else
151extern void ide_led(uchar led, uchar status);
152#define LED_IDE1 1
153#define LED_IDE2 2
154#define CONFIG_IDE_LED 1
155#define DEVICE_LED(x) 1
156#endif
wdenkc6097192002-11-03 00:24:07 +0000157#endif
158
159#ifdef CONFIG_IDE_RESET
160static void ide_reset (void);
161#else
162#define ide_reset() /* dummy */
163#endif
164
165static void ide_ident (block_dev_desc_t *dev_desc);
166static uchar ide_wait (int dev, ulong t);
167
168#define IDE_TIME_OUT 2000 /* 2 sec timeout */
169
170#define ATAPI_TIME_OUT 7000 /* 7 sec timeout (5 sec seems to work...) */
171
172#define IDE_SPIN_UP_TIME_OUT 5000 /* 5 sec spin-up timeout */
173
wdenk2262cfe2002-11-18 00:14:45 +0000174static void __inline__ ide_outb(int dev, int port, unsigned char val);
175static unsigned char __inline__ ide_inb(int dev, int port);
wdenkc6097192002-11-03 00:24:07 +0000176static void input_data(int dev, ulong *sect_buf, int words);
177static void output_data(int dev, ulong *sect_buf, int words);
178static void ident_cpy (unsigned char *dest, unsigned char *src, unsigned int len);
179
180
181#ifdef CONFIG_ATAPI
182static void atapi_inquiry(block_dev_desc_t *dev_desc);
183ulong atapi_read (int device, ulong blknr, ulong blkcnt, ulong *buffer);
184#endif
185
186
187#ifdef CONFIG_IDE_8xx_DIRECT
188static void set_pcmcia_timing (int pmode);
wdenkc6097192002-11-03 00:24:07 +0000189#endif
190
191/* ------------------------------------------------------------------------- */
192
193int do_ide (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
194{
195 int rcode = 0;
196
197 switch (argc) {
198 case 0:
199 case 1:
200 printf ("Usage:\n%s\n", cmdtp->usage);
201 return 1;
202 case 2:
203 if (strncmp(argv[1],"res",3) == 0) {
204 puts ("\nReset IDE"
205#ifdef CONFIG_IDE_8xx_DIRECT
206 " on PCMCIA " PCMCIA_SLOT_MSG
207#endif
208 ": ");
209
210 ide_init ();
211 return 0;
212 } else if (strncmp(argv[1],"inf",3) == 0) {
213 int i;
214
215 putc ('\n');
216
217 for (i=0; i<CFG_IDE_MAXDEVICE; ++i) {
218 if (ide_dev_desc[i].type==DEV_TYPE_UNKNOWN)
219 continue; /* list only known devices */
220 printf ("IDE device %d: ", i);
221 dev_print(&ide_dev_desc[i]);
222 }
223 return 0;
224
225 } else if (strncmp(argv[1],"dev",3) == 0) {
226 if ((curr_device < 0) || (curr_device >= CFG_IDE_MAXDEVICE)) {
227 puts ("\nno IDE devices available\n");
228 return 1;
229 }
230 printf ("\nIDE device %d: ", curr_device);
231 dev_print(&ide_dev_desc[curr_device]);
232 return 0;
233 } else if (strncmp(argv[1],"part",4) == 0) {
234 int dev, ok;
235
236 for (ok=0, dev=0; dev<CFG_IDE_MAXDEVICE; ++dev) {
237 if (ide_dev_desc[dev].part_type!=PART_TYPE_UNKNOWN) {
238 ++ok;
239 if (dev)
240 putc ('\n');
241 print_part(&ide_dev_desc[dev]);
242 }
243 }
244 if (!ok) {
245 puts ("\nno IDE devices available\n");
246 rcode ++;
247 }
248 return rcode;
249 }
250 printf ("Usage:\n%s\n", cmdtp->usage);
251 return 1;
252 case 3:
253 if (strncmp(argv[1],"dev",3) == 0) {
254 int dev = (int)simple_strtoul(argv[2], NULL, 10);
255
256 printf ("\nIDE device %d: ", dev);
257 if (dev >= CFG_IDE_MAXDEVICE) {
258 puts ("unknown device\n");
259 return 1;
260 }
261 dev_print(&ide_dev_desc[dev]);
262 /*ide_print (dev);*/
263
264 if (ide_dev_desc[dev].type == DEV_TYPE_UNKNOWN) {
265 return 1;
266 }
267
268 curr_device = dev;
269
270 puts ("... is now current device\n");
271
272 return 0;
273 } else if (strncmp(argv[1],"part",4) == 0) {
274 int dev = (int)simple_strtoul(argv[2], NULL, 10);
275
276 if (ide_dev_desc[dev].part_type!=PART_TYPE_UNKNOWN) {
277 print_part(&ide_dev_desc[dev]);
278 } else {
279 printf ("\nIDE device %d not available\n", dev);
280 rcode = 1;
281 }
282 return rcode;
283#if 0
284 } else if (strncmp(argv[1],"pio",4) == 0) {
285 int mode = (int)simple_strtoul(argv[2], NULL, 10);
286
287 if ((mode >= 0) && (mode <= IDE_MAX_PIO_MODE)) {
288 puts ("\nSetting ");
289 pio_mode = mode;
290 ide_init ();
291 } else {
292 printf ("\nInvalid PIO mode %d (0 ... %d only)\n",
293 mode, IDE_MAX_PIO_MODE);
294 }
295 return;
296#endif
297 }
298
299 printf ("Usage:\n%s\n", cmdtp->usage);
300 return 1;
301 default:
302 /* at least 4 args */
303
304 if (strcmp(argv[1],"read") == 0) {
305 ulong addr = simple_strtoul(argv[2], NULL, 16);
306 ulong blk = simple_strtoul(argv[3], NULL, 16);
307 ulong cnt = simple_strtoul(argv[4], NULL, 16);
308 ulong n;
309
310 printf ("\nIDE read: device %d block # %ld, count %ld ... ",
311 curr_device, blk, cnt);
312
313 n = ide_dev_desc[curr_device].block_read (curr_device,
314 blk, cnt,
315 (ulong *)addr);
316 /* flush cache after read */
317 flush_cache (addr, cnt*ide_dev_desc[curr_device].blksz);
318
319 printf ("%ld blocks read: %s\n",
320 n, (n==cnt) ? "OK" : "ERROR");
321 if (n==cnt) {
322 return 0;
323 } else {
324 return 1;
325 }
326 } else if (strcmp(argv[1],"write") == 0) {
327 ulong addr = simple_strtoul(argv[2], NULL, 16);
328 ulong blk = simple_strtoul(argv[3], NULL, 16);
329 ulong cnt = simple_strtoul(argv[4], NULL, 16);
330 ulong n;
331
332 printf ("\nIDE write: device %d block # %ld, count %ld ... ",
333 curr_device, blk, cnt);
334
335 n = ide_write (curr_device, blk, cnt, (ulong *)addr);
336
337 printf ("%ld blocks written: %s\n",
338 n, (n==cnt) ? "OK" : "ERROR");
339 if (n==cnt) {
340 return 0;
341 } else {
342 return 1;
343 }
344 } else {
345 printf ("Usage:\n%s\n", cmdtp->usage);
346 rcode = 1;
347 }
348
349 return rcode;
350 }
351}
352
353int do_diskboot (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
354{
355 char *boot_device = NULL;
356 char *ep;
357 int dev, part = 0;
358 ulong cnt;
359 ulong addr;
360 disk_partition_t info;
361 image_header_t *hdr;
362 int rcode = 0;
363
364 switch (argc) {
365 case 1:
366 addr = CFG_LOAD_ADDR;
367 boot_device = getenv ("bootdevice");
368 break;
369 case 2:
370 addr = simple_strtoul(argv[1], NULL, 16);
371 boot_device = getenv ("bootdevice");
372 break;
373 case 3:
374 addr = simple_strtoul(argv[1], NULL, 16);
375 boot_device = argv[2];
376 break;
377 default:
378 printf ("Usage:\n%s\n", cmdtp->usage);
379 SHOW_BOOT_PROGRESS (-1);
380 return 1;
381 }
382
383 if (!boot_device) {
384 puts ("\n** No boot device **\n");
385 SHOW_BOOT_PROGRESS (-1);
386 return 1;
387 }
388
389 dev = simple_strtoul(boot_device, &ep, 16);
390
391 if (ide_dev_desc[dev].type==DEV_TYPE_UNKNOWN) {
392 printf ("\n** Device %d not available\n", dev);
393 SHOW_BOOT_PROGRESS (-1);
394 return 1;
395 }
396
397 if (*ep) {
398 if (*ep != ':') {
399 puts ("\n** Invalid boot device, use `dev[:part]' **\n");
400 SHOW_BOOT_PROGRESS (-1);
401 return 1;
402 }
403 part = simple_strtoul(++ep, NULL, 16);
404 }
405 if (get_partition_info (&ide_dev_desc[dev], part, &info)) {
406 SHOW_BOOT_PROGRESS (-1);
407 return 1;
408 }
wdenk4532cb62003-04-27 22:52:51 +0000409 if ((strncmp(info.type, BOOT_PART_TYPE, sizeof(info.type)) != 0) &&
410 (strncmp(info.type, BOOT_PART_COMP, sizeof(info.type)) != 0)) {
wdenkc6097192002-11-03 00:24:07 +0000411 printf ("\n** Invalid partition type \"%.32s\""
412 " (expect \"" BOOT_PART_TYPE "\")\n",
413 info.type);
414 SHOW_BOOT_PROGRESS (-1);
415 return 1;
416 }
417
418 printf ("\nLoading from IDE device %d, partition %d: "
419 "Name: %.32s Type: %.32s\n",
420 dev, part, info.name, info.type);
421
422 PRINTF ("First Block: %ld, # of blocks: %ld, Block Size: %ld\n",
423 info.start, info.size, info.blksz);
424
425 if (ide_dev_desc[dev].block_read (dev, info.start, 1, (ulong *)addr) != 1) {
426 printf ("** Read error on %d:%d\n", dev, part);
427 SHOW_BOOT_PROGRESS (-1);
428 return 1;
429 }
430
431 hdr = (image_header_t *)addr;
432
433 if (ntohl(hdr->ih_magic) == IH_MAGIC) {
434
435 print_image_hdr (hdr);
436
437 cnt = (ntohl(hdr->ih_size) + sizeof(image_header_t));
438 cnt += info.blksz - 1;
439 cnt /= info.blksz;
440 cnt -= 1;
441 } else {
442 printf("\n** Bad Magic Number **\n");
443 SHOW_BOOT_PROGRESS (-1);
444 return 1;
445 }
446
447 if (ide_dev_desc[dev].block_read (dev, info.start+1, cnt,
448 (ulong *)(addr+info.blksz)) != cnt) {
449 printf ("** Read error on %d:%d\n", dev, part);
450 SHOW_BOOT_PROGRESS (-1);
451 return 1;
452 }
453
454
455 /* Loading ok, update default load address */
456
457 load_addr = addr;
458
459 /* Check if we should attempt an auto-start */
460 if (((ep = getenv("autostart")) != NULL) && (strcmp(ep,"yes") == 0)) {
461 char *local_args[2];
462 extern int do_bootm (cmd_tbl_t *, int, int, char *[]);
463
464 local_args[0] = argv[0];
465 local_args[1] = NULL;
466
467 printf ("Automatic boot of image at addr 0x%08lX ...\n", addr);
468
469 do_bootm (cmdtp, 0, 1, local_args);
470 rcode = 1;
471 }
472 return rcode;
473}
474
475/* ------------------------------------------------------------------------- */
476
477void ide_init (void)
478{
wdenkc6097192002-11-03 00:24:07 +0000479
480#ifdef CONFIG_IDE_8xx_DIRECT
wdenk15647dc2003-10-09 19:00:25 +0000481 DECLARE_GLOBAL_DATA_PTR;
wdenkc6097192002-11-03 00:24:07 +0000482 volatile immap_t *immr = (immap_t *)CFG_IMMR;
483 volatile pcmconf8xx_t *pcmp = &(immr->im_pcmcia);
484#endif
485 unsigned char c;
486 int i, bus;
wdenkc7de8292002-11-19 11:04:11 +0000487#ifdef CONFIG_AMIGAONEG3SE
488 unsigned int max_bus_scan;
489 unsigned int ata_reset_time;
490 char *s;
491#endif
wdenk9fd5e312003-12-07 23:55:12 +0000492#ifdef CONFIG_IDE_8xx_PCCARD
493 extern int pcmcia_on (void);
494 extern int ide_devices_found; /* Initialized in check_ide_device() */
495#endif /* CONFIG_IDE_8xx_PCCARD */
496
497#ifdef CONFIG_IDE_PREINIT
498 WATCHDOG_RESET();
499
500 if (ide_preinit ()) {
501 puts ("ide_preinit failed\n");
502 return;
503 }
504#endif /* CONFIG_IDE_PREINIT */
wdenkc6097192002-11-03 00:24:07 +0000505
506#ifdef CONFIG_IDE_8xx_PCCARD
507 extern int pcmcia_on (void);
wdenk6069ff22003-02-28 00:49:47 +0000508 extern int ide_devices_found; /* Initialized in check_ide_device() */
wdenkc6097192002-11-03 00:24:07 +0000509
510 WATCHDOG_RESET();
511
wdenk6069ff22003-02-28 00:49:47 +0000512 ide_devices_found = 0;
wdenkc6097192002-11-03 00:24:07 +0000513 /* initialize the PCMCIA IDE adapter card */
wdenk6069ff22003-02-28 00:49:47 +0000514 pcmcia_on();
515 if (!ide_devices_found)
wdenkc6097192002-11-03 00:24:07 +0000516 return;
517 udelay (1000000); /* 1 s */
518#endif /* CONFIG_IDE_8xx_PCCARD */
519
520 WATCHDOG_RESET();
521
wdenk15647dc2003-10-09 19:00:25 +0000522#ifdef CONFIG_IDE_8xx_DIRECT
wdenkc6097192002-11-03 00:24:07 +0000523 /* Initialize PIO timing tables */
524 for (i=0; i <= IDE_MAX_PIO_MODE; ++i) {
525 pio_config_clk[i].t_setup = PCMCIA_MK_CLKS(pio_config_ns[i].t_setup,
526 gd->bus_clk);
527 pio_config_clk[i].t_length = PCMCIA_MK_CLKS(pio_config_ns[i].t_length,
528 gd->bus_clk);
529 pio_config_clk[i].t_hold = PCMCIA_MK_CLKS(pio_config_ns[i].t_hold,
530 gd->bus_clk);
531 PRINTF ("PIO Mode %d: setup=%2d ns/%d clk"
532 " len=%3d ns/%d clk"
533 " hold=%2d ns/%d clk\n",
534 i,
535 pio_config_ns[i].t_setup, pio_config_clk[i].t_setup,
536 pio_config_ns[i].t_length, pio_config_clk[i].t_length,
537 pio_config_ns[i].t_hold, pio_config_clk[i].t_hold);
538 }
wdenk15647dc2003-10-09 19:00:25 +0000539#endif /* CONFIG_IDE_8xx_DIRECT */
wdenkc6097192002-11-03 00:24:07 +0000540
541 /* Reset the IDE just to be sure.
542 * Light LED's to show
543 */
544 ide_led ((LED_IDE1 | LED_IDE2), 1); /* LED's on */
545 ide_reset (); /* ATAPI Drives seems to need a proper IDE Reset */
546
547#ifdef CONFIG_IDE_8xx_DIRECT
548 /* PCMCIA / IDE initialization for common mem space */
549 pcmp->pcmc_pgcrb = 0;
wdenkc6097192002-11-03 00:24:07 +0000550
551 /* start in PIO mode 0 - most relaxed timings */
552 pio_mode = 0;
553 set_pcmcia_timing (pio_mode);
wdenk15647dc2003-10-09 19:00:25 +0000554#endif /* CONFIG_IDE_8xx_DIRECT */
wdenkc6097192002-11-03 00:24:07 +0000555
556 /*
557 * Wait for IDE to get ready.
558 * According to spec, this can take up to 31 seconds!
559 */
wdenkc7de8292002-11-19 11:04:11 +0000560#ifndef CONFIG_AMIGAONEG3SE
wdenkc6097192002-11-03 00:24:07 +0000561 for (bus=0; bus<CFG_IDE_MAXBUS; ++bus) {
562 int dev = bus * (CFG_IDE_MAXDEVICE / CFG_IDE_MAXBUS);
wdenkc7de8292002-11-19 11:04:11 +0000563#else
564 s = getenv("ide_maxbus");
565 if (s)
566 max_bus_scan = simple_strtol(s, NULL, 10);
567 else
568 max_bus_scan = CFG_IDE_MAXBUS;
569
570 for (bus=0; bus<max_bus_scan; ++bus) {
571 int dev = bus * (CFG_IDE_MAXDEVICE / max_bus_scan);
572#endif
wdenkc6097192002-11-03 00:24:07 +0000573
wdenk6069ff22003-02-28 00:49:47 +0000574#ifdef CONFIG_IDE_8xx_PCCARD
575 /* Skip non-ide devices from probing */
576 if ((ide_devices_found & (1 << bus)) == 0) {
577 ide_led ((LED_IDE1 | LED_IDE2), 0); /* LED's off */
578 continue;
579 }
580#endif
wdenkc6097192002-11-03 00:24:07 +0000581 printf ("Bus %d: ", bus);
582
583 ide_bus_ok[bus] = 0;
584
585 /* Select device
586 */
587 udelay (100000); /* 100 ms */
wdenk2262cfe2002-11-18 00:14:45 +0000588 ide_outb (dev, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(dev));
wdenkc6097192002-11-03 00:24:07 +0000589 udelay (100000); /* 100 ms */
wdenkc7de8292002-11-19 11:04:11 +0000590#ifdef CONFIG_AMIGAONEG3SE
591 ata_reset_time = ATA_RESET_TIME;
592 s = getenv("ide_reset_timeout");
593 if (s) ata_reset_time = 2*simple_strtol(s, NULL, 10);
594#endif
wdenkc6097192002-11-03 00:24:07 +0000595 i = 0;
596 do {
597 udelay (10000); /* 10 ms */
598
wdenk2262cfe2002-11-18 00:14:45 +0000599 c = ide_inb (dev, ATA_STATUS);
wdenkc6097192002-11-03 00:24:07 +0000600 i++;
wdenkc7de8292002-11-19 11:04:11 +0000601#ifdef CONFIG_AMIGAONEG3SE
602 if (i > (ata_reset_time * 100)) {
603#else
wdenkc6097192002-11-03 00:24:07 +0000604 if (i > (ATA_RESET_TIME * 100)) {
wdenkc7de8292002-11-19 11:04:11 +0000605#endif
wdenkc6097192002-11-03 00:24:07 +0000606 puts ("** Timeout **\n");
607 ide_led ((LED_IDE1 | LED_IDE2), 0); /* LED's off */
wdenkc7de8292002-11-19 11:04:11 +0000608#ifdef CONFIG_AMIGAONEG3SE
609 /* If this is the second bus, the first one was OK */
610 if (bus != 0)
611 {
612 ide_bus_ok[bus] = 0;
613 goto skip_bus;
614 }
615#endif
wdenkc6097192002-11-03 00:24:07 +0000616 return;
617 }
618 if ((i >= 100) && ((i%100)==0)) {
619 putc ('.');
620 }
621 } while (c & ATA_STAT_BUSY);
622
623 if (c & (ATA_STAT_BUSY | ATA_STAT_FAULT)) {
624 puts ("not available ");
625 PRINTF ("Status = 0x%02X ", c);
626#ifndef CONFIG_ATAPI /* ATAPI Devices do not set DRDY */
627 } else if ((c & ATA_STAT_READY) == 0) {
628 puts ("not available ");
629 PRINTF ("Status = 0x%02X ", c);
630#endif
631 } else {
632 puts ("OK ");
633 ide_bus_ok[bus] = 1;
634 }
635 WATCHDOG_RESET();
636 }
wdenkc7de8292002-11-19 11:04:11 +0000637
638#ifdef CONFIG_AMIGAONEG3SE
639 skip_bus:
640#endif
wdenkc6097192002-11-03 00:24:07 +0000641 putc ('\n');
642
643 ide_led ((LED_IDE1 | LED_IDE2), 0); /* LED's off */
644
645 curr_device = -1;
646 for (i=0; i<CFG_IDE_MAXDEVICE; ++i) {
647#ifdef CONFIG_IDE_LED
648 int led = (IDE_BUS(i) == 0) ? LED_IDE1 : LED_IDE2;
649#endif
wdenk5cf9da42003-11-07 13:42:26 +0000650 ide_dev_desc[i].type=DEV_TYPE_UNKNOWN;
wdenkc6097192002-11-03 00:24:07 +0000651 ide_dev_desc[i].if_type=IF_TYPE_IDE;
652 ide_dev_desc[i].dev=i;
653 ide_dev_desc[i].part_type=PART_TYPE_UNKNOWN;
654 ide_dev_desc[i].blksz=0;
655 ide_dev_desc[i].lba=0;
656 ide_dev_desc[i].block_read=ide_read;
657 if (!ide_bus_ok[IDE_BUS(i)])
658 continue;
659 ide_led (led, 1); /* LED on */
660 ide_ident(&ide_dev_desc[i]);
661 ide_led (led, 0); /* LED off */
662 dev_print(&ide_dev_desc[i]);
663/* ide_print (i); */
664 if ((ide_dev_desc[i].lba > 0) && (ide_dev_desc[i].blksz > 0)) {
665 init_part (&ide_dev_desc[i]); /* initialize partition type */
666 if (curr_device < 0)
667 curr_device = i;
668 }
669 }
670 WATCHDOG_RESET();
671}
672
673/* ------------------------------------------------------------------------- */
674
675block_dev_desc_t * ide_get_dev(int dev)
676{
677 return ((block_dev_desc_t *)&ide_dev_desc[dev]);
678}
679
680
681#ifdef CONFIG_IDE_8xx_DIRECT
682
683static void
684set_pcmcia_timing (int pmode)
685{
686 volatile immap_t *immr = (immap_t *)CFG_IMMR;
687 volatile pcmconf8xx_t *pcmp = &(immr->im_pcmcia);
688 ulong timings;
689
690 PRINTF ("Set timing for PIO Mode %d\n", pmode);
691
692 timings = PCMCIA_SHT(pio_config_clk[pmode].t_hold)
693 | PCMCIA_SST(pio_config_clk[pmode].t_setup)
694 | PCMCIA_SL (pio_config_clk[pmode].t_length)
695 ;
696
697 /* IDE 0
698 */
699 pcmp->pcmc_pbr0 = CFG_PCMCIA_PBR0;
700 pcmp->pcmc_por0 = CFG_PCMCIA_POR0
701#if (CFG_PCMCIA_POR0 != 0)
702 | timings
703#endif
704 ;
705 PRINTF ("PBR0: %08x POR0: %08x\n", pcmp->pcmc_pbr0, pcmp->pcmc_por0);
706
707 pcmp->pcmc_pbr1 = CFG_PCMCIA_PBR1;
708 pcmp->pcmc_por1 = CFG_PCMCIA_POR1
709#if (CFG_PCMCIA_POR1 != 0)
710 | timings
711#endif
712 ;
713 PRINTF ("PBR1: %08x POR1: %08x\n", pcmp->pcmc_pbr1, pcmp->pcmc_por1);
714
715 pcmp->pcmc_pbr2 = CFG_PCMCIA_PBR2;
716 pcmp->pcmc_por2 = CFG_PCMCIA_POR2
717#if (CFG_PCMCIA_POR2 != 0)
718 | timings
719#endif
720 ;
721 PRINTF ("PBR2: %08x POR2: %08x\n", pcmp->pcmc_pbr2, pcmp->pcmc_por2);
722
723 pcmp->pcmc_pbr3 = CFG_PCMCIA_PBR3;
724 pcmp->pcmc_por3 = CFG_PCMCIA_POR3
725#if (CFG_PCMCIA_POR3 != 0)
726 | timings
727#endif
728 ;
729 PRINTF ("PBR3: %08x POR3: %08x\n", pcmp->pcmc_pbr3, pcmp->pcmc_por3);
730
731 /* IDE 1
732 */
733 pcmp->pcmc_pbr4 = CFG_PCMCIA_PBR4;
734 pcmp->pcmc_por4 = CFG_PCMCIA_POR4
735#if (CFG_PCMCIA_POR4 != 0)
736 | timings
737#endif
738 ;
739 PRINTF ("PBR4: %08x POR4: %08x\n", pcmp->pcmc_pbr4, pcmp->pcmc_por4);
740
741 pcmp->pcmc_pbr5 = CFG_PCMCIA_PBR5;
742 pcmp->pcmc_por5 = CFG_PCMCIA_POR5
743#if (CFG_PCMCIA_POR5 != 0)
744 | timings
745#endif
746 ;
747 PRINTF ("PBR5: %08x POR5: %08x\n", pcmp->pcmc_pbr5, pcmp->pcmc_por5);
748
749 pcmp->pcmc_pbr6 = CFG_PCMCIA_PBR6;
750 pcmp->pcmc_por6 = CFG_PCMCIA_POR6
751#if (CFG_PCMCIA_POR6 != 0)
752 | timings
753#endif
754 ;
755 PRINTF ("PBR6: %08x POR6: %08x\n", pcmp->pcmc_pbr6, pcmp->pcmc_por6);
756
757 pcmp->pcmc_pbr7 = CFG_PCMCIA_PBR7;
758 pcmp->pcmc_por7 = CFG_PCMCIA_POR7
759#if (CFG_PCMCIA_POR7 != 0)
760 | timings
761#endif
762 ;
763 PRINTF ("PBR7: %08x POR7: %08x\n", pcmp->pcmc_pbr7, pcmp->pcmc_por7);
764
765}
766
767#endif /* CONFIG_IDE_8xx_DIRECT */
768
769/* ------------------------------------------------------------------------- */
770
wdenk2262cfe2002-11-18 00:14:45 +0000771#ifdef __PPC__
wdenkc6097192002-11-03 00:24:07 +0000772static void __inline__
wdenk2262cfe2002-11-18 00:14:45 +0000773ide_outb(int dev, int port, unsigned char val)
wdenkc6097192002-11-03 00:24:07 +0000774{
wdenk9fd5e312003-12-07 23:55:12 +0000775 PRINTF ("ide_outb (dev= %d, port= %d, val= 0x%02x) : @ 0x%08lx\n",
776 dev, port, val, (ATA_CURR_BASE(dev)+port));
wdenkd4ca31c2004-01-02 14:00:00 +0000777
wdenkc6097192002-11-03 00:24:07 +0000778 /* Ensure I/O operations complete */
779 __asm__ volatile("eieio");
780 *((uchar *)(ATA_CURR_BASE(dev)+port)) = val;
wdenkc6097192002-11-03 00:24:07 +0000781}
wdenk2262cfe2002-11-18 00:14:45 +0000782#else /* ! __PPC__ */
783static void __inline__
784ide_outb(int dev, int port, unsigned char val)
785{
wdenk15647dc2003-10-09 19:00:25 +0000786 outb(val, ATA_CURR_BASE(dev)+port);
wdenk2262cfe2002-11-18 00:14:45 +0000787}
788#endif /* __PPC__ */
wdenkc6097192002-11-03 00:24:07 +0000789
wdenk2262cfe2002-11-18 00:14:45 +0000790
791#ifdef __PPC__
wdenkc6097192002-11-03 00:24:07 +0000792static unsigned char __inline__
wdenk2262cfe2002-11-18 00:14:45 +0000793ide_inb(int dev, int port)
wdenkc6097192002-11-03 00:24:07 +0000794{
795 uchar val;
796 /* Ensure I/O operations complete */
797 __asm__ volatile("eieio");
798 val = *((uchar *)(ATA_CURR_BASE(dev)+port));
wdenk9fd5e312003-12-07 23:55:12 +0000799 PRINTF ("ide_inb (dev= %d, port= %d) : @ 0x%08lx -> 0x%02x\n",
800 dev, port, (ATA_CURR_BASE(dev)+port), val);
wdenkc6097192002-11-03 00:24:07 +0000801 return (val);
802}
wdenk2262cfe2002-11-18 00:14:45 +0000803#else /* ! __PPC__ */
804static unsigned char __inline__
805ide_inb(int dev, int port)
806{
wdenk15647dc2003-10-09 19:00:25 +0000807 return inb(ATA_CURR_BASE(dev)+port);
wdenk2262cfe2002-11-18 00:14:45 +0000808}
809#endif /* __PPC__ */
wdenkc6097192002-11-03 00:24:07 +0000810
wdenk2262cfe2002-11-18 00:14:45 +0000811#ifdef __PPC__
wdenkcceb8712003-06-23 18:12:28 +0000812# ifdef CONFIG_AMIGAONEG3SE
wdenkc7de8292002-11-19 11:04:11 +0000813static void
814output_data_short(int dev, ulong *sect_buf, int words)
815{
816 ushort *dbuf;
817 volatile ushort *pbuf;
wdenk8bde7f72003-06-27 21:31:46 +0000818
wdenkc7de8292002-11-19 11:04:11 +0000819 pbuf = (ushort *)(ATA_CURR_BASE(dev)+ATA_DATA_REG);
820 dbuf = (ushort *)sect_buf;
821 while (words--) {
822 __asm__ volatile ("eieio");
823 *pbuf = *dbuf++;
824 __asm__ volatile ("eieio");
825 }
826
827 if (words&1)
828 *pbuf = 0;
829}
wdenkcceb8712003-06-23 18:12:28 +0000830# endif /* CONFIG_AMIGAONEG3SE */
wdenk5da627a2003-10-09 20:09:04 +0000831#endif /* __PPC_ */
wdenkc7de8292002-11-19 11:04:11 +0000832
wdenk5da627a2003-10-09 20:09:04 +0000833/* We only need to swap data if we are running on a big endian cpu. */
834/* But Au1x00 cpu:s already swaps data in big endian mode! */
835#if defined(__LITTLE_ENDIAN) || defined(CONFIG_AU1X00)
836#define input_swap_data(x,y,z) input_data(x,y,z)
837#else
wdenkc6097192002-11-03 00:24:07 +0000838static void
839input_swap_data(int dev, ulong *sect_buf, int words)
840{
wdenka522fa02004-01-04 22:51:12 +0000841#ifndef CONFIG_BMS2003
wdenkc6097192002-11-03 00:24:07 +0000842 volatile ushort *pbuf = (ushort *)(ATA_CURR_BASE(dev)+ATA_DATA_REG);
843 ushort *dbuf = (ushort *)sect_buf;
844
845 while (words--) {
846 *dbuf++ = ld_le16(pbuf);
847 *dbuf++ = ld_le16(pbuf);
848 }
wdenka522fa02004-01-04 22:51:12 +0000849#else /* CONFIG_BMS2003 */
850 uchar i;
851 volatile uchar *pbuf_even = (uchar *)(ATA_CURR_BASE(dev)+ATA_DATA_EVEN);
852 volatile uchar *pbuf_odd = (uchar *)(ATA_CURR_BASE(dev)+ATA_DATA_ODD);
853 ushort *dbuf = (ushort *)sect_buf;
854
855 while (words--) {
856 for (i=0; i<2; i++) {
857 *(((uchar *)(dbuf)) + 1) = *pbuf_even;
858 *(uchar *)dbuf = *pbuf_odd;
859 dbuf+=1;
860 }
861 }
862#endif /* CONFIG_BMS2003 */
wdenkc6097192002-11-03 00:24:07 +0000863}
wdenk5da627a2003-10-09 20:09:04 +0000864#endif /* __LITTLE_ENDIAN || CONFIG_AU1X00 */
wdenkc6097192002-11-03 00:24:07 +0000865
wdenk2262cfe2002-11-18 00:14:45 +0000866
wdenk2262cfe2002-11-18 00:14:45 +0000867#ifdef __PPC__
wdenkc6097192002-11-03 00:24:07 +0000868static void
869output_data(int dev, ulong *sect_buf, int words)
870{
wdenka522fa02004-01-04 22:51:12 +0000871#ifndef CONFIG_BMS2003
wdenkc6097192002-11-03 00:24:07 +0000872 ushort *dbuf;
873 volatile ushort *pbuf;
874
875 pbuf = (ushort *)(ATA_CURR_BASE(dev)+ATA_DATA_REG);
876 dbuf = (ushort *)sect_buf;
877 while (words--) {
878 __asm__ volatile ("eieio");
879 *pbuf = *dbuf++;
880 __asm__ volatile ("eieio");
881 *pbuf = *dbuf++;
882 }
wdenka522fa02004-01-04 22:51:12 +0000883#else /* CONFIG_BMS2003 */
884 uchar *dbuf;
885 volatile uchar *pbuf_even;
886 volatile uchar *pbuf_odd;
887
888 pbuf_even = (uchar *)(ATA_CURR_BASE(dev)+ATA_DATA_EVEN);
889 pbuf_odd = (uchar *)(ATA_CURR_BASE(dev)+ATA_DATA_ODD);
890 dbuf = (uchar *)sect_buf;
891 while (words--) {
892 __asm__ volatile ("eieio");
893 *pbuf_even = *dbuf++;
894 __asm__ volatile ("eieio");
895 *pbuf_odd = *dbuf++;
896 __asm__ volatile ("eieio");
897 *pbuf_even = *dbuf++;
898 __asm__ volatile ("eieio");
899 *pbuf_odd = *dbuf++;
900 }
901#endif /* CONFIG_BMS2003 */
wdenkc6097192002-11-03 00:24:07 +0000902}
wdenk2262cfe2002-11-18 00:14:45 +0000903#else /* ! __PPC__ */
904static void
905output_data(int dev, ulong *sect_buf, int words)
906{
wdenk15647dc2003-10-09 19:00:25 +0000907 outsw(ATA_CURR_BASE(dev)+ATA_DATA_REG, sect_buf, words<<1);
wdenk2262cfe2002-11-18 00:14:45 +0000908}
909#endif /* __PPC__ */
wdenkc6097192002-11-03 00:24:07 +0000910
wdenk2262cfe2002-11-18 00:14:45 +0000911#ifdef __PPC__
wdenkc6097192002-11-03 00:24:07 +0000912static void
913input_data(int dev, ulong *sect_buf, int words)
914{
wdenka522fa02004-01-04 22:51:12 +0000915#ifndef CONFIG_BMS2003
wdenkc6097192002-11-03 00:24:07 +0000916 ushort *dbuf;
917 volatile ushort *pbuf;
918
919 pbuf = (ushort *)(ATA_CURR_BASE(dev)+ATA_DATA_REG);
920 dbuf = (ushort *)sect_buf;
921 while (words--) {
922 __asm__ volatile ("eieio");
923 *dbuf++ = *pbuf;
924 __asm__ volatile ("eieio");
925 *dbuf++ = *pbuf;
926 }
wdenka522fa02004-01-04 22:51:12 +0000927#else /* CONFIG_BMS2003 */
928 uchar *dbuf;
929 volatile uchar *pbuf_even;
930 volatile uchar *pbuf_odd;
931
932 pbuf_even = (uchar *)(ATA_CURR_BASE(dev)+ATA_DATA_EVEN);
933 pbuf_odd = (uchar *)(ATA_CURR_BASE(dev)+ATA_DATA_ODD);
934 dbuf = (uchar *)sect_buf;
935 while (words--) {
936 __asm__ volatile ("eieio");
937 *dbuf++ = *pbuf_even;
938 __asm__ volatile ("eieio");
939 *dbuf++ = *pbuf_odd;
940 __asm__ volatile ("eieio");
941 *dbuf++ = *pbuf_even;
942 __asm__ volatile ("eieio");
943 *dbuf++ = *pbuf_odd;
944 }
945#endif /* CONFIG_BMS2003 */
wdenkc6097192002-11-03 00:24:07 +0000946}
wdenk2262cfe2002-11-18 00:14:45 +0000947#else /* ! __PPC__ */
948static void
949input_data(int dev, ulong *sect_buf, int words)
950{
wdenk15647dc2003-10-09 19:00:25 +0000951 insw(ATA_CURR_BASE(dev)+ATA_DATA_REG, sect_buf, words << 1);
wdenk2262cfe2002-11-18 00:14:45 +0000952}
953
954#endif /* __PPC__ */
wdenkc6097192002-11-03 00:24:07 +0000955
wdenkc7de8292002-11-19 11:04:11 +0000956#ifdef CONFIG_AMIGAONEG3SE
957static void
958input_data_short(int dev, ulong *sect_buf, int words)
959{
960 ushort *dbuf;
961 volatile ushort *pbuf;
962
963 pbuf = (ushort *)(ATA_CURR_BASE(dev)+ATA_DATA_REG);
964 dbuf = (ushort *)sect_buf;
965 while (words--) {
966 __asm__ volatile ("eieio");
967 *dbuf++ = *pbuf;
968 __asm__ volatile ("eieio");
969 }
970
971 if (words&1)
972 {
973 ushort dummy;
974 dummy = *pbuf;
975 }
976}
977#endif
978
wdenkc6097192002-11-03 00:24:07 +0000979/* -------------------------------------------------------------------------
980 */
981static void ide_ident (block_dev_desc_t *dev_desc)
982{
983 ulong iobuf[ATA_SECTORWORDS];
984 unsigned char c;
985 hd_driveid_t *iop = (hd_driveid_t *)iobuf;
986
wdenkc7de8292002-11-19 11:04:11 +0000987#ifdef CONFIG_AMIGAONEG3SE
988 int max_bus_scan;
989 int retries = 0;
990 char *s;
991 int do_retry = 0;
992#endif
993
wdenkc6097192002-11-03 00:24:07 +0000994#if 0
995 int mode, cycle_time;
996#endif
997 int device;
998 device=dev_desc->dev;
999 printf (" Device %d: ", device);
1000
wdenkc7de8292002-11-19 11:04:11 +00001001#ifdef CONFIG_AMIGAONEG3SE
1002 s = getenv("ide_maxbus");
1003 if (s) {
1004 max_bus_scan = simple_strtol(s, NULL, 10);
1005 } else {
1006 max_bus_scan = CFG_IDE_MAXBUS;
1007 }
1008 if (device >= max_bus_scan*2) {
1009 dev_desc->type=DEV_TYPE_UNKNOWN;
1010 return;
1011 }
1012#endif
1013
wdenkc6097192002-11-03 00:24:07 +00001014 ide_led (DEVICE_LED(device), 1); /* LED on */
1015 /* Select device
1016 */
wdenk2262cfe2002-11-18 00:14:45 +00001017 ide_outb (device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
wdenkc6097192002-11-03 00:24:07 +00001018 dev_desc->if_type=IF_TYPE_IDE;
1019#ifdef CONFIG_ATAPI
wdenkc7de8292002-11-19 11:04:11 +00001020
1021#ifdef CONFIG_AMIGAONEG3SE
1022 do_retry = 0;
1023 retries = 0;
1024
1025 /* Warning: This will be tricky to read */
1026 while (retries <= 1)
1027 {
1028#endif /* CONFIG_AMIGAONEG3SE */
1029
wdenkc6097192002-11-03 00:24:07 +00001030 /* check signature */
wdenk2262cfe2002-11-18 00:14:45 +00001031 if ((ide_inb(device,ATA_SECT_CNT) == 0x01) &&
1032 (ide_inb(device,ATA_SECT_NUM) == 0x01) &&
1033 (ide_inb(device,ATA_CYL_LOW) == 0x14) &&
1034 (ide_inb(device,ATA_CYL_HIGH) == 0xEB)) {
wdenkc6097192002-11-03 00:24:07 +00001035 /* ATAPI Signature found */
1036 dev_desc->if_type=IF_TYPE_ATAPI;
1037 /* Start Ident Command
1038 */
wdenk2262cfe2002-11-18 00:14:45 +00001039 ide_outb (device, ATA_COMMAND, ATAPI_CMD_IDENT);
wdenkc6097192002-11-03 00:24:07 +00001040 /*
1041 * Wait for completion - ATAPI devices need more time
1042 * to become ready
1043 */
1044 c = ide_wait (device, ATAPI_TIME_OUT);
1045 }
1046 else
1047#endif
1048 {
1049 /* Start Ident Command
1050 */
wdenk2262cfe2002-11-18 00:14:45 +00001051 ide_outb (device, ATA_COMMAND, ATA_CMD_IDENT);
wdenkc6097192002-11-03 00:24:07 +00001052
1053 /* Wait for completion
1054 */
1055 c = ide_wait (device, IDE_TIME_OUT);
1056 }
1057 ide_led (DEVICE_LED(device), 0); /* LED off */
1058
1059 if (((c & ATA_STAT_DRQ) == 0) ||
1060 ((c & (ATA_STAT_FAULT|ATA_STAT_ERR)) != 0) ) {
wdenkc7de8292002-11-19 11:04:11 +00001061#ifdef CONFIG_AMIGAONEG3SE
1062 if (retries == 0) {
1063 do_retry = 1;
1064 } else {
wdenkc7de8292002-11-19 11:04:11 +00001065 return;
1066 }
1067#else
wdenkc6097192002-11-03 00:24:07 +00001068 return;
wdenkc7de8292002-11-19 11:04:11 +00001069#endif /* CONFIG_AMIGAONEG3SE */
wdenkc6097192002-11-03 00:24:07 +00001070 }
1071
wdenkc7de8292002-11-19 11:04:11 +00001072#ifdef CONFIG_AMIGAONEG3SE
1073 s = getenv("ide_doreset");
1074 if (s && strcmp(s, "on") == 0 && 1 == do_retry) {
1075 /* Need to soft reset the device in case it's an ATAPI... */
1076 PRINTF("Retrying...\n");
1077 ide_outb (device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
1078 udelay(100000);
1079 ide_outb (device, ATA_COMMAND, 0x08);
1080 udelay (100000); /* 100 ms */
1081 retries++;
1082 } else {
1083 retries = 100;
1084 }
1085 } /* see above - ugly to read */
1086#endif /* CONFIG_AMIGAONEG3SE */
1087
wdenkc6097192002-11-03 00:24:07 +00001088 input_swap_data (device, iobuf, ATA_SECTORWORDS);
1089
1090 ident_cpy (dev_desc->revision, iop->fw_rev, sizeof(dev_desc->revision));
1091 ident_cpy (dev_desc->vendor, iop->model, sizeof(dev_desc->vendor));
1092 ident_cpy (dev_desc->product, iop->serial_no, sizeof(dev_desc->product));
1093
1094 if ((iop->config & 0x0080)==0x0080)
1095 dev_desc->removable = 1;
1096 else
1097 dev_desc->removable = 0;
1098
1099#if 0
1100 /*
1101 * Drive PIO mode autoselection
1102 */
1103 mode = iop->tPIO;
1104
1105 printf ("tPIO = 0x%02x = %d\n",mode, mode);
1106 if (mode > 2) { /* 2 is maximum allowed tPIO value */
1107 mode = 2;
1108 PRINTF ("Override tPIO -> 2\n");
1109 }
1110 if (iop->field_valid & 2) { /* drive implements ATA2? */
1111 PRINTF ("Drive implements ATA2\n");
1112 if (iop->capability & 8) { /* drive supports use_iordy? */
1113 cycle_time = iop->eide_pio_iordy;
1114 } else {
1115 cycle_time = iop->eide_pio;
1116 }
1117 PRINTF ("cycle time = %d\n", cycle_time);
1118 mode = 4;
1119 if (cycle_time > 120) mode = 3; /* 120 ns for PIO mode 4 */
1120 if (cycle_time > 180) mode = 2; /* 180 ns for PIO mode 3 */
1121 if (cycle_time > 240) mode = 1; /* 240 ns for PIO mode 4 */
1122 if (cycle_time > 383) mode = 0; /* 383 ns for PIO mode 4 */
1123 }
1124 printf ("PIO mode to use: PIO %d\n", mode);
1125#endif /* 0 */
1126
1127#ifdef CONFIG_ATAPI
1128 if (dev_desc->if_type==IF_TYPE_ATAPI) {
1129 atapi_inquiry(dev_desc);
1130 return;
1131 }
1132#endif /* CONFIG_ATAPI */
1133
1134 /* swap shorts */
1135 dev_desc->lba = (iop->lba_capacity << 16) | (iop->lba_capacity >> 16);
1136 /* assuming HD */
1137 dev_desc->type=DEV_TYPE_HARDDISK;
1138 dev_desc->blksz=ATA_BLOCKSIZE;
1139 dev_desc->lun=0; /* just to fill something in... */
1140
1141#if 0 /* only used to test the powersaving mode,
1142 * if enabled, the drive goes after 5 sec
1143 * in standby mode */
wdenk2262cfe2002-11-18 00:14:45 +00001144 ide_outb (device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
wdenkc6097192002-11-03 00:24:07 +00001145 c = ide_wait (device, IDE_TIME_OUT);
wdenk2262cfe2002-11-18 00:14:45 +00001146 ide_outb (device, ATA_SECT_CNT, 1);
1147 ide_outb (device, ATA_LBA_LOW, 0);
1148 ide_outb (device, ATA_LBA_MID, 0);
1149 ide_outb (device, ATA_LBA_HIGH, 0);
1150 ide_outb (device, ATA_DEV_HD, ATA_LBA |
wdenkc6097192002-11-03 00:24:07 +00001151 ATA_DEVICE(device));
wdenk2262cfe2002-11-18 00:14:45 +00001152 ide_outb (device, ATA_COMMAND, 0xe3);
wdenkc6097192002-11-03 00:24:07 +00001153 udelay (50);
1154 c = ide_wait (device, IDE_TIME_OUT); /* can't take over 500 ms */
1155#endif
1156}
1157
1158
1159/* ------------------------------------------------------------------------- */
1160
1161ulong ide_read (int device, ulong blknr, ulong blkcnt, ulong *buffer)
1162{
1163 ulong n = 0;
1164 unsigned char c;
1165 unsigned char pwrsave=0; /* power save */
1166
1167 PRINTF ("ide_read dev %d start %lX, blocks %lX buffer at %lX\n",
1168 device, blknr, blkcnt, (ulong)buffer);
1169
1170 ide_led (DEVICE_LED(device), 1); /* LED on */
1171
1172 /* Select device
1173 */
wdenk2262cfe2002-11-18 00:14:45 +00001174 ide_outb (device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
wdenkc6097192002-11-03 00:24:07 +00001175 c = ide_wait (device, IDE_TIME_OUT);
1176
1177 if (c & ATA_STAT_BUSY) {
1178 printf ("IDE read: device %d not ready\n", device);
1179 goto IDE_READ_E;
1180 }
1181
1182 /* first check if the drive is in Powersaving mode, if yes,
1183 * increase the timeout value */
wdenk2262cfe2002-11-18 00:14:45 +00001184 ide_outb (device, ATA_COMMAND, ATA_CMD_CHK_PWR);
wdenkc6097192002-11-03 00:24:07 +00001185 udelay (50);
1186
1187 c = ide_wait (device, IDE_TIME_OUT); /* can't take over 500 ms */
1188
1189 if (c & ATA_STAT_BUSY) {
1190 printf ("IDE read: device %d not ready\n", device);
1191 goto IDE_READ_E;
1192 }
1193 if ((c & ATA_STAT_ERR) == ATA_STAT_ERR) {
1194 printf ("No Powersaving mode %X\n", c);
1195 } else {
wdenk2262cfe2002-11-18 00:14:45 +00001196 c = ide_inb(device,ATA_SECT_CNT);
wdenkc6097192002-11-03 00:24:07 +00001197 PRINTF("Powersaving %02X\n",c);
1198 if(c==0)
1199 pwrsave=1;
1200 }
1201
1202
1203 while (blkcnt-- > 0) {
1204
1205 c = ide_wait (device, IDE_TIME_OUT);
1206
1207 if (c & ATA_STAT_BUSY) {
1208 printf ("IDE read: device %d not ready\n", device);
1209 break;
1210 }
1211
wdenk2262cfe2002-11-18 00:14:45 +00001212 ide_outb (device, ATA_SECT_CNT, 1);
1213 ide_outb (device, ATA_LBA_LOW, (blknr >> 0) & 0xFF);
1214 ide_outb (device, ATA_LBA_MID, (blknr >> 8) & 0xFF);
1215 ide_outb (device, ATA_LBA_HIGH, (blknr >> 16) & 0xFF);
1216 ide_outb (device, ATA_DEV_HD, ATA_LBA |
wdenkc6097192002-11-03 00:24:07 +00001217 ATA_DEVICE(device) |
1218 ((blknr >> 24) & 0xF) );
wdenk2262cfe2002-11-18 00:14:45 +00001219 ide_outb (device, ATA_COMMAND, ATA_CMD_READ);
wdenkc6097192002-11-03 00:24:07 +00001220
1221 udelay (50);
1222
1223 if(pwrsave) {
1224 c = ide_wait (device, IDE_SPIN_UP_TIME_OUT); /* may take up to 4 sec */
1225 pwrsave=0;
1226 } else {
1227 c = ide_wait (device, IDE_TIME_OUT); /* can't take over 500 ms */
1228 }
1229
1230 if ((c&(ATA_STAT_DRQ|ATA_STAT_BUSY|ATA_STAT_ERR)) != ATA_STAT_DRQ) {
1231 printf ("Error (no IRQ) dev %d blk %ld: status 0x%02x\n",
1232 device, blknr, c);
1233 break;
1234 }
1235
1236 input_data (device, buffer, ATA_SECTORWORDS);
wdenk2262cfe2002-11-18 00:14:45 +00001237 (void) ide_inb (device, ATA_STATUS); /* clear IRQ */
wdenkc6097192002-11-03 00:24:07 +00001238
1239 ++n;
1240 ++blknr;
1241 buffer += ATA_SECTORWORDS;
1242 }
1243IDE_READ_E:
1244 ide_led (DEVICE_LED(device), 0); /* LED off */
1245 return (n);
1246}
1247
1248/* ------------------------------------------------------------------------- */
1249
1250
1251ulong ide_write (int device, ulong blknr, ulong blkcnt, ulong *buffer)
1252{
1253 ulong n = 0;
1254 unsigned char c;
1255
1256 ide_led (DEVICE_LED(device), 1); /* LED on */
1257
1258 /* Select device
1259 */
wdenk2262cfe2002-11-18 00:14:45 +00001260 ide_outb (device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
wdenkc6097192002-11-03 00:24:07 +00001261
1262 while (blkcnt-- > 0) {
1263
1264 c = ide_wait (device, IDE_TIME_OUT);
1265
1266 if (c & ATA_STAT_BUSY) {
1267 printf ("IDE read: device %d not ready\n", device);
1268 goto WR_OUT;
1269 }
1270
wdenk2262cfe2002-11-18 00:14:45 +00001271 ide_outb (device, ATA_SECT_CNT, 1);
1272 ide_outb (device, ATA_LBA_LOW, (blknr >> 0) & 0xFF);
1273 ide_outb (device, ATA_LBA_MID, (blknr >> 8) & 0xFF);
1274 ide_outb (device, ATA_LBA_HIGH, (blknr >> 16) & 0xFF);
1275 ide_outb (device, ATA_DEV_HD, ATA_LBA |
wdenkc6097192002-11-03 00:24:07 +00001276 ATA_DEVICE(device) |
1277 ((blknr >> 24) & 0xF) );
wdenk2262cfe2002-11-18 00:14:45 +00001278 ide_outb (device, ATA_COMMAND, ATA_CMD_WRITE);
wdenkc6097192002-11-03 00:24:07 +00001279
1280 udelay (50);
1281
1282 c = ide_wait (device, IDE_TIME_OUT); /* can't take over 500 ms */
1283
1284 if ((c&(ATA_STAT_DRQ|ATA_STAT_BUSY|ATA_STAT_ERR)) != ATA_STAT_DRQ) {
1285 printf ("Error (no IRQ) dev %d blk %ld: status 0x%02x\n",
1286 device, blknr, c);
1287 goto WR_OUT;
1288 }
1289
1290 output_data (device, buffer, ATA_SECTORWORDS);
wdenk2262cfe2002-11-18 00:14:45 +00001291 c = ide_inb (device, ATA_STATUS); /* clear IRQ */
wdenkc6097192002-11-03 00:24:07 +00001292 ++n;
1293 ++blknr;
1294 buffer += ATA_SECTORWORDS;
1295 }
1296WR_OUT:
1297 ide_led (DEVICE_LED(device), 0); /* LED off */
1298 return (n);
1299}
1300
1301/* ------------------------------------------------------------------------- */
1302
1303/*
1304 * copy src to dest, skipping leading and trailing blanks and null
1305 * terminate the string
1306 */
1307static void ident_cpy (unsigned char *dest, unsigned char *src, unsigned int len)
1308{
1309 int start,end;
1310
1311 start=0;
1312 while (start<len) {
1313 if (src[start]!=' ')
1314 break;
1315 start++;
1316 }
1317 end=len-1;
1318 while (end>start) {
1319 if (src[end]!=' ')
1320 break;
1321 end--;
1322 }
1323 for ( ; start<=end; start++) {
1324 *dest++=src[start];
1325 }
1326 *dest='\0';
1327}
1328
1329/* ------------------------------------------------------------------------- */
1330
1331/*
1332 * Wait until Busy bit is off, or timeout (in ms)
1333 * Return last status
1334 */
1335static uchar ide_wait (int dev, ulong t)
1336{
1337 ulong delay = 10 * t; /* poll every 100 us */
1338 uchar c;
1339
wdenk2262cfe2002-11-18 00:14:45 +00001340 while ((c = ide_inb(dev, ATA_STATUS)) & ATA_STAT_BUSY) {
wdenkc6097192002-11-03 00:24:07 +00001341 udelay (100);
1342 if (delay-- == 0) {
1343 break;
1344 }
1345 }
1346 return (c);
1347}
1348
1349/* ------------------------------------------------------------------------- */
1350
1351#ifdef CONFIG_IDE_RESET
1352extern void ide_set_reset(int idereset);
1353
1354static void ide_reset (void)
1355{
1356#if defined(CFG_PB_12V_ENABLE) || defined(CFG_PB_IDE_MOTOR)
1357 volatile immap_t *immr = (immap_t *)CFG_IMMR;
1358#endif
1359 int i;
1360
1361 curr_device = -1;
1362 for (i=0; i<CFG_IDE_MAXBUS; ++i)
1363 ide_bus_ok[i] = 0;
1364 for (i=0; i<CFG_IDE_MAXDEVICE; ++i)
1365 ide_dev_desc[i].type = DEV_TYPE_UNKNOWN;
1366
1367 ide_set_reset (1); /* assert reset */
1368
1369 WATCHDOG_RESET();
1370
1371#ifdef CFG_PB_12V_ENABLE
1372 immr->im_cpm.cp_pbdat &= ~(CFG_PB_12V_ENABLE); /* 12V Enable output OFF */
1373 immr->im_cpm.cp_pbpar &= ~(CFG_PB_12V_ENABLE);
1374 immr->im_cpm.cp_pbodr &= ~(CFG_PB_12V_ENABLE);
1375 immr->im_cpm.cp_pbdir |= CFG_PB_12V_ENABLE;
1376
1377 /* wait 500 ms for the voltage to stabilize
1378 */
1379 for (i=0; i<500; ++i) {
1380 udelay (1000);
1381 }
1382
1383 immr->im_cpm.cp_pbdat |= CFG_PB_12V_ENABLE; /* 12V Enable output ON */
1384#endif /* CFG_PB_12V_ENABLE */
1385
1386#ifdef CFG_PB_IDE_MOTOR
1387 /* configure IDE Motor voltage monitor pin as input */
1388 immr->im_cpm.cp_pbpar &= ~(CFG_PB_IDE_MOTOR);
1389 immr->im_cpm.cp_pbodr &= ~(CFG_PB_IDE_MOTOR);
1390 immr->im_cpm.cp_pbdir &= ~(CFG_PB_IDE_MOTOR);
1391
1392 /* wait up to 1 s for the motor voltage to stabilize
1393 */
1394 for (i=0; i<1000; ++i) {
1395 if ((immr->im_cpm.cp_pbdat & CFG_PB_IDE_MOTOR) != 0) {
1396 break;
1397 }
1398 udelay (1000);
1399 }
1400
1401 if (i == 1000) { /* Timeout */
1402 printf ("\nWarning: 5V for IDE Motor missing\n");
1403# ifdef CONFIG_STATUS_LED
1404# ifdef STATUS_LED_YELLOW
1405 status_led_set (STATUS_LED_YELLOW, STATUS_LED_ON );
1406# endif
1407# ifdef STATUS_LED_GREEN
1408 status_led_set (STATUS_LED_GREEN, STATUS_LED_OFF);
1409# endif
1410# endif /* CONFIG_STATUS_LED */
1411 }
1412#endif /* CFG_PB_IDE_MOTOR */
1413
1414 WATCHDOG_RESET();
1415
1416 /* de-assert RESET signal */
1417 ide_set_reset(0);
1418
1419 /* wait 250 ms */
1420 for (i=0; i<250; ++i) {
1421 udelay (1000);
1422 }
1423}
1424
1425#endif /* CONFIG_IDE_RESET */
1426
1427/* ------------------------------------------------------------------------- */
1428
wdenk1c437712004-01-16 00:30:56 +00001429#if defined(CONFIG_IDE_LED) && !defined(CONFIG_AMIGAONEG3SE) && !defined(CONFIG_KUP4K) && !defined(CONFIG_BMS2003)
wdenkc6097192002-11-03 00:24:07 +00001430
1431static uchar led_buffer = 0; /* Buffer for current LED status */
1432
1433static void ide_led (uchar led, uchar status)
1434{
1435 uchar *led_port = LED_PORT;
1436
1437 if (status) { /* switch LED on */
1438 led_buffer |= led;
1439 } else { /* switch LED off */
1440 led_buffer &= ~led;
1441 }
1442
1443 *led_port = led_buffer;
1444}
1445
1446#endif /* CONFIG_IDE_LED */
1447
1448/* ------------------------------------------------------------------------- */
1449
1450#ifdef CONFIG_ATAPI
1451/****************************************************************************
1452 * ATAPI Support
1453 */
1454
1455
wdenkc6097192002-11-03 00:24:07 +00001456#undef ATAPI_DEBUG
1457
1458#ifdef ATAPI_DEBUG
1459#define AT_PRINTF(fmt,args...) printf (fmt ,##args)
1460#else
1461#define AT_PRINTF(fmt,args...)
1462#endif
1463
wdenk2262cfe2002-11-18 00:14:45 +00001464#ifdef __PPC__
wdenkc6097192002-11-03 00:24:07 +00001465/* since ATAPI may use commands with not 4 bytes alligned length
1466 * we have our own transfer functions, 2 bytes alligned */
1467static void
1468output_data_shorts(int dev, ushort *sect_buf, int shorts)
1469{
wdenka522fa02004-01-04 22:51:12 +00001470#ifndef CONFIG_BMS2003
wdenkc6097192002-11-03 00:24:07 +00001471 ushort *dbuf;
1472 volatile ushort *pbuf;
1473
1474 pbuf = (ushort *)(ATA_CURR_BASE(dev)+ATA_DATA_REG);
1475 dbuf = (ushort *)sect_buf;
1476 while (shorts--) {
1477 __asm__ volatile ("eieio");
1478 *pbuf = *dbuf++;
1479 }
wdenka522fa02004-01-04 22:51:12 +00001480#else /* CONFIG_BMS2003 */
1481 uchar *dbuf;
1482 volatile uchar *pbuf_even;
1483 volatile uchar *pbuf_odd;
1484
1485 pbuf_even = (uchar *)(ATA_CURR_BASE(dev)+ATA_DATA_EVEN);
1486 pbuf_odd = (uchar *)(ATA_CURR_BASE(dev)+ATA_DATA_ODD);
1487 while (shorts--) {
1488 __asm__ volatile ("eieio");
1489 *pbuf_even = *dbuf++;
1490 __asm__ volatile ("eieio");
1491 *pbuf_odd = *dbuf++;
1492 }
1493#endif /* CONFIG_BMS2003 */
wdenkc6097192002-11-03 00:24:07 +00001494}
1495
1496static void
1497input_data_shorts(int dev, ushort *sect_buf, int shorts)
1498{
wdenka522fa02004-01-04 22:51:12 +00001499#ifndef CONFIG_BMS2003
wdenkc6097192002-11-03 00:24:07 +00001500 ushort *dbuf;
1501 volatile ushort *pbuf;
1502
1503 pbuf = (ushort *)(ATA_CURR_BASE(dev)+ATA_DATA_REG);
1504 dbuf = (ushort *)sect_buf;
1505 while (shorts--) {
1506 __asm__ volatile ("eieio");
1507 *dbuf++ = *pbuf;
1508 }
wdenka522fa02004-01-04 22:51:12 +00001509#else /* CONFIG_BMS2003 */
1510 uchar *dbuf;
1511 volatile uchar *pbuf_even;
1512 volatile uchar *pbuf_odd;
1513
1514 pbuf_even = (uchar *)(ATA_CURR_BASE(dev)+ATA_DATA_EVEN);
1515 pbuf_odd = (uchar *)(ATA_CURR_BASE(dev)+ATA_DATA_ODD);
1516 while (shorts--) {
1517 __asm__ volatile ("eieio");
1518 *dbuf++ = *pbuf_even;
1519 __asm__ volatile ("eieio");
1520 *dbuf++ = *pbuf_odd;
1521 }
1522#endif /* CONFIG_BMS2003 */
wdenkc6097192002-11-03 00:24:07 +00001523}
1524
wdenk2262cfe2002-11-18 00:14:45 +00001525#else /* ! __PPC__ */
1526static void
1527output_data_shorts(int dev, ushort *sect_buf, int shorts)
1528{
wdenk15647dc2003-10-09 19:00:25 +00001529 outsw(ATA_CURR_BASE(dev)+ATA_DATA_REG, sect_buf, shorts);
wdenk2262cfe2002-11-18 00:14:45 +00001530}
1531
1532
1533static void
1534input_data_shorts(int dev, ushort *sect_buf, int shorts)
1535{
wdenk15647dc2003-10-09 19:00:25 +00001536 insw(ATA_CURR_BASE(dev)+ATA_DATA_REG, sect_buf, shorts);
wdenk2262cfe2002-11-18 00:14:45 +00001537}
1538
1539#endif /* __PPC__ */
1540
wdenkc6097192002-11-03 00:24:07 +00001541/*
1542 * Wait until (Status & mask) == res, or timeout (in ms)
1543 * Return last status
1544 * This is used since some ATAPI CD ROMs clears their Busy Bit first
1545 * and then they set their DRQ Bit
1546 */
1547static uchar atapi_wait_mask (int dev, ulong t,uchar mask, uchar res)
1548{
1549 ulong delay = 10 * t; /* poll every 100 us */
1550 uchar c;
1551
wdenk2262cfe2002-11-18 00:14:45 +00001552 c = ide_inb(dev,ATA_DEV_CTL); /* prevents to read the status before valid */
1553 while (((c = ide_inb(dev, ATA_STATUS)) & mask) != res) {
wdenkc6097192002-11-03 00:24:07 +00001554 /* break if error occurs (doesn't make sense to wait more) */
1555 if((c & ATA_STAT_ERR)==ATA_STAT_ERR)
1556 break;
1557 udelay (100);
1558 if (delay-- == 0) {
1559 break;
1560 }
1561 }
1562 return (c);
1563}
1564
1565/*
1566 * issue an atapi command
1567 */
1568unsigned char atapi_issue(int device,unsigned char* ccb,int ccblen, unsigned char * buffer,int buflen)
1569{
1570 unsigned char c,err,mask,res;
1571 int n;
1572 ide_led (DEVICE_LED(device), 1); /* LED on */
1573
1574 /* Select device
1575 */
1576 mask = ATA_STAT_BUSY|ATA_STAT_DRQ;
1577 res = 0;
wdenkc7de8292002-11-19 11:04:11 +00001578#ifdef CONFIG_AMIGAONEG3SE
1579# warning THF: Removed LBA mode ???
1580#endif
wdenk2262cfe2002-11-18 00:14:45 +00001581 ide_outb (device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
wdenkc6097192002-11-03 00:24:07 +00001582 c = atapi_wait_mask(device,ATAPI_TIME_OUT,mask,res);
1583 if ((c & mask) != res) {
1584 printf ("ATAPI_ISSUE: device %d not ready status %X\n", device,c);
1585 err=0xFF;
1586 goto AI_OUT;
1587 }
1588 /* write taskfile */
wdenk2262cfe2002-11-18 00:14:45 +00001589 ide_outb (device, ATA_ERROR_REG, 0); /* no DMA, no overlaped */
wdenkc7de8292002-11-19 11:04:11 +00001590 ide_outb (device, ATA_SECT_CNT, 0);
1591 ide_outb (device, ATA_SECT_NUM, 0);
wdenk2262cfe2002-11-18 00:14:45 +00001592 ide_outb (device, ATA_CYL_LOW, (unsigned char)(buflen & 0xFF));
wdenkc7de8292002-11-19 11:04:11 +00001593 ide_outb (device, ATA_CYL_HIGH, (unsigned char)((buflen>>8) & 0xFF));
1594#ifdef CONFIG_AMIGAONEG3SE
1595# warning THF: Removed LBA mode ???
1596#endif
wdenk2262cfe2002-11-18 00:14:45 +00001597 ide_outb (device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
wdenkc6097192002-11-03 00:24:07 +00001598
wdenk2262cfe2002-11-18 00:14:45 +00001599 ide_outb (device, ATA_COMMAND, ATAPI_CMD_PACKET);
wdenkc6097192002-11-03 00:24:07 +00001600 udelay (50);
1601
1602 mask = ATA_STAT_DRQ|ATA_STAT_BUSY|ATA_STAT_ERR;
1603 res = ATA_STAT_DRQ;
1604 c = atapi_wait_mask(device,ATAPI_TIME_OUT,mask,res);
1605
1606 if ((c & mask) != res) { /* DRQ must be 1, BSY 0 */
1607 printf ("ATTAPI_ISSUE: Error (no IRQ) before sending ccb dev %d status 0x%02x\n",device,c);
1608 err=0xFF;
1609 goto AI_OUT;
1610 }
1611
1612 output_data_shorts (device, (unsigned short *)ccb,ccblen/2); /* write command block */
1613 /* ATAPI Command written wait for completition */
1614 udelay (5000); /* device must set bsy */
1615
1616 mask = ATA_STAT_DRQ|ATA_STAT_BUSY|ATA_STAT_ERR;
1617 /* if no data wait for DRQ = 0 BSY = 0
1618 * if data wait for DRQ = 1 BSY = 0 */
1619 res=0;
1620 if(buflen)
1621 res = ATA_STAT_DRQ;
1622 c = atapi_wait_mask(device,ATAPI_TIME_OUT,mask,res);
1623 if ((c & mask) != res ) {
1624 if (c & ATA_STAT_ERR) {
wdenk2262cfe2002-11-18 00:14:45 +00001625 err=(ide_inb(device,ATA_ERROR_REG))>>4;
wdenkc6097192002-11-03 00:24:07 +00001626 AT_PRINTF("atapi_issue 1 returned sense key %X status %02X\n",err,c);
1627 } else {
1628 printf ("ATTAPI_ISSUE: (no DRQ) after sending ccb (%x) status 0x%02x\n", ccb[0],c);
1629 err=0xFF;
1630 }
1631 goto AI_OUT;
1632 }
wdenk2262cfe2002-11-18 00:14:45 +00001633 n=ide_inb(device, ATA_CYL_HIGH);
wdenkc6097192002-11-03 00:24:07 +00001634 n<<=8;
wdenk2262cfe2002-11-18 00:14:45 +00001635 n+=ide_inb(device, ATA_CYL_LOW);
wdenkc6097192002-11-03 00:24:07 +00001636 if(n>buflen) {
1637 printf("ERROR, transfer bytes %d requested only %d\n",n,buflen);
1638 err=0xff;
1639 goto AI_OUT;
1640 }
1641 if((n==0)&&(buflen<0)) {
1642 printf("ERROR, transfer bytes %d requested %d\n",n,buflen);
1643 err=0xff;
1644 goto AI_OUT;
1645 }
1646 if(n!=buflen) {
1647 AT_PRINTF("WARNING, transfer bytes %d not equal with requested %d\n",n,buflen);
1648 }
1649 if(n!=0) { /* data transfer */
1650 AT_PRINTF("ATAPI_ISSUE: %d Bytes to transfer\n",n);
1651 /* we transfer shorts */
1652 n>>=1;
1653 /* ok now decide if it is an in or output */
wdenk2262cfe2002-11-18 00:14:45 +00001654 if ((ide_inb(device, ATA_SECT_CNT)&0x02)==0) {
wdenkc6097192002-11-03 00:24:07 +00001655 AT_PRINTF("Write to device\n");
1656 output_data_shorts(device,(unsigned short *)buffer,n);
1657 } else {
1658 AT_PRINTF("Read from device @ %p shorts %d\n",buffer,n);
1659 input_data_shorts(device,(unsigned short *)buffer,n);
1660 }
1661 }
1662 udelay(5000); /* seems that some CD ROMs need this... */
1663 mask = ATA_STAT_BUSY|ATA_STAT_ERR;
1664 res=0;
1665 c = atapi_wait_mask(device,ATAPI_TIME_OUT,mask,res);
1666 if ((c & ATA_STAT_ERR) == ATA_STAT_ERR) {
wdenk2262cfe2002-11-18 00:14:45 +00001667 err=(ide_inb(device,ATA_ERROR_REG) >> 4);
wdenkc6097192002-11-03 00:24:07 +00001668 AT_PRINTF("atapi_issue 2 returned sense key %X status %X\n",err,c);
1669 } else {
1670 err = 0;
1671 }
1672AI_OUT:
1673 ide_led (DEVICE_LED(device), 0); /* LED off */
1674 return (err);
1675}
1676
1677/*
1678 * sending the command to atapi_issue. If an status other than good
1679 * returns, an request_sense will be issued
1680 */
1681
1682#define ATAPI_DRIVE_NOT_READY 100
1683#define ATAPI_UNIT_ATTN 10
1684
1685unsigned char atapi_issue_autoreq (int device,
1686 unsigned char* ccb,
1687 int ccblen,
1688 unsigned char *buffer,
1689 int buflen)
1690{
1691 unsigned char sense_data[18],sense_ccb[12];
1692 unsigned char res,key,asc,ascq;
1693 int notready,unitattn;
1694
wdenkc7de8292002-11-19 11:04:11 +00001695#ifdef CONFIG_AMIGAONEG3SE
1696 char *s;
1697 unsigned int timeout, retrycnt;
1698
1699 s = getenv("ide_cd_timeout");
1700 timeout = s ? (simple_strtol(s, NULL, 10)*1000000)/5 : 0;
1701
1702 retrycnt = 0;
1703#endif
1704
wdenkc6097192002-11-03 00:24:07 +00001705 unitattn=ATAPI_UNIT_ATTN;
1706 notready=ATAPI_DRIVE_NOT_READY;
1707
1708retry:
1709 res= atapi_issue(device,ccb,ccblen,buffer,buflen);
1710 if (res==0)
1711 return (0); /* Ok */
1712
1713 if (res==0xFF)
1714 return (0xFF); /* error */
1715
1716 AT_PRINTF("(auto_req)atapi_issue returned sense key %X\n",res);
1717
1718 memset(sense_ccb,0,sizeof(sense_ccb));
1719 memset(sense_data,0,sizeof(sense_data));
1720 sense_ccb[0]=ATAPI_CMD_REQ_SENSE;
wdenkc7de8292002-11-19 11:04:11 +00001721 sense_ccb[4]=18; /* allocation Length */
wdenkc6097192002-11-03 00:24:07 +00001722
1723 res=atapi_issue(device,sense_ccb,12,sense_data,18);
1724 key=(sense_data[2]&0xF);
1725 asc=(sense_data[12]);
1726 ascq=(sense_data[13]);
1727
1728 AT_PRINTF("ATAPI_CMD_REQ_SENSE returned %x\n",res);
1729 AT_PRINTF(" Sense page: %02X key %02X ASC %02X ASCQ %02X\n",
1730 sense_data[0],
1731 key,
1732 asc,
1733 ascq);
1734
1735 if((key==0))
1736 return 0; /* ok device ready */
1737
1738 if((key==6)|| (asc==0x29) || (asc==0x28)) { /* Unit Attention */
1739 if(unitattn-->0) {
1740 udelay(200*1000);
1741 goto retry;
1742 }
1743 printf("Unit Attention, tried %d\n",ATAPI_UNIT_ATTN);
1744 goto error;
1745 }
1746 if((asc==0x4) && (ascq==0x1)) { /* not ready, but will be ready soon */
1747 if (notready-->0) {
1748 udelay(200*1000);
1749 goto retry;
1750 }
1751 printf("Drive not ready, tried %d times\n",ATAPI_DRIVE_NOT_READY);
1752 goto error;
1753 }
1754 if(asc==0x3a) {
1755 AT_PRINTF("Media not present\n");
1756 goto error;
1757 }
wdenkc7de8292002-11-19 11:04:11 +00001758
1759#ifdef CONFIG_AMIGAONEG3SE
1760 if ((sense_data[2]&0xF)==0x0B) {
1761 AT_PRINTF("ABORTED COMMAND...retry\n");
1762 if (retrycnt++ < 4)
1763 goto retry;
1764 return (0xFF);
1765 }
1766
1767 if ((sense_data[2]&0xf) == 0x02 &&
1768 sense_data[12] == 0x04 &&
1769 sense_data[13] == 0x01 ) {
1770 AT_PRINTF("Waiting for unit to become active\n");
1771 udelay(timeout);
1772 if (retrycnt++ < 4)
1773 goto retry;
1774 return 0xFF;
1775 }
1776#endif /* CONFIG_AMIGAONEG3SE */
1777
wdenkc6097192002-11-03 00:24:07 +00001778 printf ("ERROR: Unknown Sense key %02X ASC %02X ASCQ %02X\n",key,asc,ascq);
1779error:
1780 AT_PRINTF ("ERROR Sense key %02X ASC %02X ASCQ %02X\n",key,asc,ascq);
1781 return (0xFF);
1782}
1783
1784
wdenkc6097192002-11-03 00:24:07 +00001785static void atapi_inquiry(block_dev_desc_t * dev_desc)
1786{
1787 unsigned char ccb[12]; /* Command descriptor block */
1788 unsigned char iobuf[64]; /* temp buf */
1789 unsigned char c;
1790 int device;
1791
1792 device=dev_desc->dev;
1793 dev_desc->type=DEV_TYPE_UNKNOWN; /* not yet valid */
1794 dev_desc->block_read=atapi_read;
1795
1796 memset(ccb,0,sizeof(ccb));
1797 memset(iobuf,0,sizeof(iobuf));
1798
1799 ccb[0]=ATAPI_CMD_INQUIRY;
1800 ccb[4]=40; /* allocation Legnth */
1801 c=atapi_issue_autoreq(device,ccb,12,(unsigned char *)iobuf,40);
1802
1803 AT_PRINTF("ATAPI_CMD_INQUIRY returned %x\n",c);
1804 if (c!=0)
1805 return;
1806
1807 /* copy device ident strings */
1808 ident_cpy(dev_desc->vendor,&iobuf[8],8);
1809 ident_cpy(dev_desc->product,&iobuf[16],16);
1810 ident_cpy(dev_desc->revision,&iobuf[32],5);
1811
1812 dev_desc->lun=0;
1813 dev_desc->lba=0;
1814 dev_desc->blksz=0;
1815 dev_desc->type=iobuf[0] & 0x1f;
1816
1817 if ((iobuf[1]&0x80)==0x80)
1818 dev_desc->removable = 1;
1819 else
1820 dev_desc->removable = 0;
1821
1822 memset(ccb,0,sizeof(ccb));
1823 memset(iobuf,0,sizeof(iobuf));
1824 ccb[0]=ATAPI_CMD_START_STOP;
1825 ccb[4]=0x03; /* start */
1826
1827 c=atapi_issue_autoreq(device,ccb,12,(unsigned char *)iobuf,0);
1828
1829 AT_PRINTF("ATAPI_CMD_START_STOP returned %x\n",c);
1830 if (c!=0)
1831 return;
1832
1833 memset(ccb,0,sizeof(ccb));
1834 memset(iobuf,0,sizeof(iobuf));
1835 c=atapi_issue_autoreq(device,ccb,12,(unsigned char *)iobuf,0);
1836
1837 AT_PRINTF("ATAPI_CMD_UNIT_TEST_READY returned %x\n",c);
1838 if (c!=0)
1839 return;
1840
1841 memset(ccb,0,sizeof(ccb));
1842 memset(iobuf,0,sizeof(iobuf));
1843 ccb[0]=ATAPI_CMD_READ_CAP;
1844 c=atapi_issue_autoreq(device,ccb,12,(unsigned char *)iobuf,8);
1845 AT_PRINTF("ATAPI_CMD_READ_CAP returned %x\n",c);
1846 if (c!=0)
1847 return;
1848
1849 AT_PRINTF("Read Cap: LBA %02X%02X%02X%02X blksize %02X%02X%02X%02X\n",
1850 iobuf[0],iobuf[1],iobuf[2],iobuf[3],
1851 iobuf[4],iobuf[5],iobuf[6],iobuf[7]);
1852
1853 dev_desc->lba =((unsigned long)iobuf[0]<<24) +
1854 ((unsigned long)iobuf[1]<<16) +
1855 ((unsigned long)iobuf[2]<< 8) +
1856 ((unsigned long)iobuf[3]);
1857 dev_desc->blksz=((unsigned long)iobuf[4]<<24) +
1858 ((unsigned long)iobuf[5]<<16) +
1859 ((unsigned long)iobuf[6]<< 8) +
1860 ((unsigned long)iobuf[7]);
1861 return;
1862}
1863
1864
1865/*
1866 * atapi_read:
1867 * we transfer only one block per command, since the multiple DRQ per
1868 * command is not yet implemented
1869 */
1870#define ATAPI_READ_MAX_BYTES 2048 /* we read max 2kbytes */
1871#define ATAPI_READ_BLOCK_SIZE 2048 /* assuming CD part */
1872#define ATAPI_READ_MAX_BLOCK ATAPI_READ_MAX_BYTES/ATAPI_READ_BLOCK_SIZE /* max blocks */
1873
1874ulong atapi_read (int device, ulong blknr, ulong blkcnt, ulong *buffer)
1875{
1876 ulong n = 0;
1877 unsigned char ccb[12]; /* Command descriptor block */
1878 ulong cnt;
1879
1880 AT_PRINTF("atapi_read dev %d start %lX, blocks %lX buffer at %lX\n",
1881 device, blknr, blkcnt, (ulong)buffer);
1882
1883 do {
1884 if (blkcnt>ATAPI_READ_MAX_BLOCK) {
1885 cnt=ATAPI_READ_MAX_BLOCK;
1886 } else {
1887 cnt=blkcnt;
1888 }
1889 ccb[0]=ATAPI_CMD_READ_12;
1890 ccb[1]=0; /* reserved */
1891 ccb[2]=(unsigned char) (blknr>>24) & 0xFF; /* MSB Block */
1892 ccb[3]=(unsigned char) (blknr>>16) & 0xFF; /* */
1893 ccb[4]=(unsigned char) (blknr>> 8) & 0xFF;
1894 ccb[5]=(unsigned char) blknr & 0xFF; /* LSB Block */
1895 ccb[6]=(unsigned char) (cnt >>24) & 0xFF; /* MSB Block count */
1896 ccb[7]=(unsigned char) (cnt >>16) & 0xFF;
1897 ccb[8]=(unsigned char) (cnt >> 8) & 0xFF;
1898 ccb[9]=(unsigned char) cnt & 0xFF; /* LSB Block */
1899 ccb[10]=0; /* reserved */
1900 ccb[11]=0; /* reserved */
1901
1902 if (atapi_issue_autoreq(device,ccb,12,
1903 (unsigned char *)buffer,
1904 cnt*ATAPI_READ_BLOCK_SIZE) == 0xFF) {
1905 return (n);
1906 }
1907 n+=cnt;
1908 blkcnt-=cnt;
1909 blknr+=cnt;
1910 buffer+=cnt*(ATAPI_READ_BLOCK_SIZE/4); /* ulong blocksize in ulong */
1911 } while (blkcnt > 0);
1912 return (n);
1913}
1914
1915/* ------------------------------------------------------------------------- */
1916
1917#endif /* CONFIG_ATAPI */
1918
wdenk0d498392003-07-01 21:06:45 +00001919U_BOOT_CMD(
1920 ide, 5, 1, do_ide,
wdenk8bde7f72003-06-27 21:31:46 +00001921 "ide - IDE sub-system\n",
1922 "reset - reset IDE controller\n"
1923 "ide info - show available IDE devices\n"
1924 "ide device [dev] - show or set current device\n"
1925 "ide part [dev] - print partition table of one or all IDE devices\n"
1926 "ide read addr blk# cnt\n"
1927 "ide write addr blk# cnt - read/write `cnt'"
1928 " blocks starting at block `blk#'\n"
1929 " to/from memory address `addr'\n"
1930);
1931
wdenk0d498392003-07-01 21:06:45 +00001932U_BOOT_CMD(
1933 diskboot, 3, 1, do_diskboot,
wdenk8bde7f72003-06-27 21:31:46 +00001934 "diskboot- boot from IDE device\n",
1935 "loadAddr dev:part\n"
1936);
1937
wdenkc6097192002-11-03 00:24:07 +00001938#endif /* CONFIG_COMMANDS & CFG_CMD_IDE */