blob: 305c6029f9b210759a2f389a4324d698db7a7c94 [file] [log] [blame]
wdenkc6097192002-11-03 00:24:07 +00001/*
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00002 * (C) Copyright 2000-2011
wdenkc6097192002-11-03 00:24:07 +00003 * 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 */
Albert Aribaud45027552010-08-08 05:17:06 +053028
wdenkc6097192002-11-03 00:24:07 +000029#include <common.h>
30#include <config.h>
31#include <watchdog.h>
32#include <command.h>
33#include <image.h>
34#include <asm/byteorder.h>
Heiko Schocher2559e0f2007-08-28 17:39:14 +020035#include <asm/io.h>
Grant Likelyffc2dd72007-02-20 09:04:34 +010036
wdenkc6097192002-11-03 00:24:07 +000037#if defined(CONFIG_IDE_8xx_DIRECT) || defined(CONFIG_IDE_PCMCIA)
38# include <pcmcia.h>
39#endif
Grant Likelyffc2dd72007-02-20 09:04:34 +010040
wdenkc6097192002-11-03 00:24:07 +000041#ifdef CONFIG_8xx
42# include <mpc8xx.h>
43#endif
Grant Likelyffc2dd72007-02-20 09:04:34 +010044
wdenk6ea1cf02004-02-27 08:20:54 +000045#ifdef CONFIG_MPC5xxx
46#include <mpc5xxx.h>
47#endif
Grant Likelyffc2dd72007-02-20 09:04:34 +010048
wdenkc6097192002-11-03 00:24:07 +000049#include <ide.h>
50#include <ata.h>
Grant Likelyffc2dd72007-02-20 09:04:34 +010051
wdenkc6097192002-11-03 00:24:07 +000052#ifdef CONFIG_STATUS_LED
53# include <status_led.h>
54#endif
Grant Likelyffc2dd72007-02-20 09:04:34 +010055
Wolfgang Denk6405a152006-03-31 18:32:53 +020056#ifdef CONFIG_IDE_8xx_DIRECT
57DECLARE_GLOBAL_DATA_PTR;
58#endif
59
wdenk20c98a62004-04-23 20:32:05 +000060#ifdef __PPC__
61# define EIEIO __asm__ volatile ("eieio")
wdenkf1276d42005-02-03 23:00:49 +000062# define SYNC __asm__ volatile ("sync")
wdenk20c98a62004-04-23 20:32:05 +000063#else
64# define EIEIO /* nothing */
wdenkf1276d42005-02-03 23:00:49 +000065# define SYNC /* nothing */
wdenk20c98a62004-04-23 20:32:05 +000066#endif
wdenkc6097192002-11-03 00:24:07 +000067
wdenk9f837932003-10-09 19:00:25 +000068#ifdef CONFIG_IDE_8xx_DIRECT
wdenkc6097192002-11-03 00:24:07 +000069/* Timings for IDE Interface
70 *
71 * SETUP / LENGTH / HOLD - cycles valid for 50 MHz clk
72 * 70 165 30 PIO-Mode 0, [ns]
73 * 4 9 2 [Cycles]
74 * 50 125 20 PIO-Mode 1, [ns]
75 * 3 7 2 [Cycles]
76 * 30 100 15 PIO-Mode 2, [ns]
77 * 2 6 1 [Cycles]
78 * 30 80 10 PIO-Mode 3, [ns]
79 * 2 5 1 [Cycles]
80 * 25 70 10 PIO-Mode 4, [ns]
81 * 2 4 1 [Cycles]
82 */
83
84const static pio_config_t pio_config_ns [IDE_MAX_PIO_MODE+1] =
85{
86 /* Setup Length Hold */
87 { 70, 165, 30 }, /* PIO-Mode 0, [ns] */
88 { 50, 125, 20 }, /* PIO-Mode 1, [ns] */
89 { 30, 101, 15 }, /* PIO-Mode 2, [ns] */
90 { 30, 80, 10 }, /* PIO-Mode 3, [ns] */
91 { 25, 70, 10 }, /* PIO-Mode 4, [ns] */
92};
93
94static pio_config_t pio_config_clk [IDE_MAX_PIO_MODE+1];
95
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020096#ifndef CONFIG_SYS_PIO_MODE
97#define CONFIG_SYS_PIO_MODE 0 /* use a relaxed default */
wdenkc6097192002-11-03 00:24:07 +000098#endif
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020099static int pio_mode = CONFIG_SYS_PIO_MODE;
wdenkc6097192002-11-03 00:24:07 +0000100
101/* Make clock cycles and always round up */
102
103#define PCMCIA_MK_CLKS( t, T ) (( (t) * (T) + 999U ) / 1000U )
104
wdenk9f837932003-10-09 19:00:25 +0000105#endif /* CONFIG_IDE_8xx_DIRECT */
106
wdenkc6097192002-11-03 00:24:07 +0000107/* ------------------------------------------------------------------------- */
108
109/* Current I/O Device */
110static int curr_device = -1;
111
112/* Current offset for IDE0 / IDE1 bus access */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200113ulong ide_bus_offset[CONFIG_SYS_IDE_MAXBUS] = {
114#if defined(CONFIG_SYS_ATA_IDE0_OFFSET)
115 CONFIG_SYS_ATA_IDE0_OFFSET,
wdenkc6097192002-11-03 00:24:07 +0000116#endif
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200117#if defined(CONFIG_SYS_ATA_IDE1_OFFSET) && (CONFIG_SYS_IDE_MAXBUS > 1)
118 CONFIG_SYS_ATA_IDE1_OFFSET,
wdenkc6097192002-11-03 00:24:07 +0000119#endif
120};
121
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200122static int ide_bus_ok[CONFIG_SYS_IDE_MAXBUS];
wdenkc6097192002-11-03 00:24:07 +0000123
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200124block_dev_desc_t ide_dev_desc[CONFIG_SYS_IDE_MAXDEVICE];
wdenkc6097192002-11-03 00:24:07 +0000125/* ------------------------------------------------------------------------- */
126
127#ifdef CONFIG_IDE_LED
Wolfgang Denkb0b104a2010-06-13 18:28:54 +0200128# if !defined(CONFIG_BMS2003) && \
129 !defined(CONFIG_CPC45) && \
130 !defined(CONFIG_KUP4K) && \
131 !defined(CONFIG_KUP4X)
wdenkc6097192002-11-03 00:24:07 +0000132static void ide_led (uchar led, uchar status);
133#else
wdenk90e7e422002-12-04 23:39:58 +0000134extern void ide_led (uchar led, uchar status);
135#endif
136#else
wdenkc6097192002-11-03 00:24:07 +0000137#define ide_led(a,b) /* dummy */
138#endif
139
140#ifdef CONFIG_IDE_RESET
141static void ide_reset (void);
142#else
143#define ide_reset() /* dummy */
144#endif
145
146static void ide_ident (block_dev_desc_t *dev_desc);
147static uchar ide_wait (int dev, ulong t);
148
149#define IDE_TIME_OUT 2000 /* 2 sec timeout */
150
151#define ATAPI_TIME_OUT 7000 /* 7 sec timeout (5 sec seems to work...) */
152
153#define IDE_SPIN_UP_TIME_OUT 5000 /* 5 sec spin-up timeout */
154
wdenkc6097192002-11-03 00:24:07 +0000155static void input_data(int dev, ulong *sect_buf, int words);
Wolfgang Denk7fafed22011-04-30 23:29:55 +0200156static void output_data(int dev, const ulong *sect_buf, int words);
wdenkc6097192002-11-03 00:24:07 +0000157static void ident_cpy (unsigned char *dest, unsigned char *src, unsigned int len);
158
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200159#ifndef CONFIG_SYS_ATA_PORT_ADDR
160#define CONFIG_SYS_ATA_PORT_ADDR(port) (port)
Heiko Schocher633e03a2007-06-22 19:11:54 +0200161#endif
wdenkc6097192002-11-03 00:24:07 +0000162
163#ifdef CONFIG_ATAPI
164static void atapi_inquiry(block_dev_desc_t *dev_desc);
Grant Likely2089cab2007-02-20 09:05:45 +0100165ulong atapi_read (int device, lbaint_t blknr, ulong blkcnt, void *buffer);
wdenkc6097192002-11-03 00:24:07 +0000166#endif
167
168
169#ifdef CONFIG_IDE_8xx_DIRECT
170static void set_pcmcia_timing (int pmode);
wdenkc6097192002-11-03 00:24:07 +0000171#endif
172
173/* ------------------------------------------------------------------------- */
174
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000175int do_ide(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
wdenkc6097192002-11-03 00:24:07 +0000176{
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000177 int rcode = 0;
wdenkc6097192002-11-03 00:24:07 +0000178
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000179 switch (argc) {
180 case 0:
181 case 1:
182 return cmd_usage(cmdtp);
183 case 2:
184 if (strncmp(argv[1], "res", 3) == 0) {
185 puts("\nReset IDE"
wdenkc6097192002-11-03 00:24:07 +0000186#ifdef CONFIG_IDE_8xx_DIRECT
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000187 " on PCMCIA " PCMCIA_SLOT_MSG
wdenkc6097192002-11-03 00:24:07 +0000188#endif
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000189 ": ");
wdenkc6097192002-11-03 00:24:07 +0000190
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000191 ide_init();
192 return 0;
193 } else if (strncmp(argv[1], "inf", 3) == 0) {
194 int i;
wdenkc6097192002-11-03 00:24:07 +0000195
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000196 putc('\n');
wdenkc6097192002-11-03 00:24:07 +0000197
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000198 for (i = 0; i < CONFIG_SYS_IDE_MAXDEVICE; ++i) {
199 if (ide_dev_desc[i].type == DEV_TYPE_UNKNOWN)
200 continue; /* list only known devices */
201 printf("IDE device %d: ", i);
202 dev_print(&ide_dev_desc[i]);
203 }
204 return 0;
wdenkc6097192002-11-03 00:24:07 +0000205
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000206 } else if (strncmp(argv[1], "dev", 3) == 0) {
207 if ((curr_device < 0)
208 || (curr_device >= CONFIG_SYS_IDE_MAXDEVICE)) {
209 puts("\nno IDE devices available\n");
210 return 1;
211 }
212 printf("\nIDE device %d: ", curr_device);
213 dev_print(&ide_dev_desc[curr_device]);
214 return 0;
215 } else if (strncmp(argv[1], "part", 4) == 0) {
216 int dev, ok;
wdenkc6097192002-11-03 00:24:07 +0000217
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000218 for (ok = 0, dev = 0;
219 dev < CONFIG_SYS_IDE_MAXDEVICE;
220 ++dev) {
221 if (ide_dev_desc[dev].part_type !=
222 PART_TYPE_UNKNOWN) {
223 ++ok;
224 if (dev)
225 putc('\n');
226 print_part(&ide_dev_desc[dev]);
227 }
wdenkc6097192002-11-03 00:24:07 +0000228 }
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000229 if (!ok) {
230 puts("\nno IDE devices available\n");
231 rcode++;
232 }
233 return rcode;
wdenkc6097192002-11-03 00:24:07 +0000234 }
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000235 return cmd_usage(cmdtp);
236 case 3:
237 if (strncmp(argv[1], "dev", 3) == 0) {
238 int dev = (int) simple_strtoul(argv[2], NULL, 10);
wdenkc6097192002-11-03 00:24:07 +0000239
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000240 printf("\nIDE device %d: ", dev);
241 if (dev >= CONFIG_SYS_IDE_MAXDEVICE) {
242 puts("unknown device\n");
243 return 1;
244 }
245 dev_print(&ide_dev_desc[dev]);
246 /*ide_print (dev); */
wdenkc6097192002-11-03 00:24:07 +0000247
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000248 if (ide_dev_desc[dev].type == DEV_TYPE_UNKNOWN)
249 return 1;
wdenkc6097192002-11-03 00:24:07 +0000250
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000251 curr_device = dev;
wdenkc6097192002-11-03 00:24:07 +0000252
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000253 puts("... is now current device\n");
wdenkc6097192002-11-03 00:24:07 +0000254
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000255 return 0;
256 } else if (strncmp(argv[1], "part", 4) == 0) {
257 int dev = (int) simple_strtoul(argv[2], NULL, 10);
wdenkc6097192002-11-03 00:24:07 +0000258
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000259 if (ide_dev_desc[dev].part_type != PART_TYPE_UNKNOWN) {
wdenkc6097192002-11-03 00:24:07 +0000260 print_part(&ide_dev_desc[dev]);
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000261 } else {
262 printf("\nIDE device %d not available\n",
263 dev);
264 rcode = 1;
265 }
266 return rcode;
wdenkc6097192002-11-03 00:24:07 +0000267 }
wdenkc6097192002-11-03 00:24:07 +0000268
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000269 return cmd_usage(cmdtp);
270 default:
271 /* at least 4 args */
wdenkc6097192002-11-03 00:24:07 +0000272
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000273 if (strcmp(argv[1], "read") == 0) {
274 ulong addr = simple_strtoul(argv[2], NULL, 16);
275 ulong cnt = simple_strtoul(argv[4], NULL, 16);
276 ulong n;
wdenkc6097192002-11-03 00:24:07 +0000277
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200278#ifdef CONFIG_SYS_64BIT_LBA
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000279 lbaint_t blk = simple_strtoull(argv[3], NULL, 16);
wdenkc6097192002-11-03 00:24:07 +0000280
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000281 printf("\nIDE read: device %d block # %lld, count %ld ... ",
282 curr_device, blk, cnt);
wdenkc35ba4e2004-03-14 22:25:36 +0000283#else
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000284 lbaint_t blk = simple_strtoul(argv[3], NULL, 16);
wdenkc35ba4e2004-03-14 22:25:36 +0000285
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000286 printf("\nIDE read: device %d block # %ld, count %ld ... ",
287 curr_device, blk, cnt);
wdenkc35ba4e2004-03-14 22:25:36 +0000288#endif
wdenkc6097192002-11-03 00:24:07 +0000289
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000290 n = ide_dev_desc[curr_device].block_read(curr_device,
291 blk, cnt,
292 (ulong *)addr);
293 /* flush cache after read */
294 flush_cache(addr,
295 cnt * ide_dev_desc[curr_device].blksz);
wdenkc6097192002-11-03 00:24:07 +0000296
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000297 printf("%ld blocks read: %s\n",
298 n, (n == cnt) ? "OK" : "ERROR");
299 if (n == cnt)
300 return 0;
301 else
302 return 1;
303 } else if (strcmp(argv[1], "write") == 0) {
304 ulong addr = simple_strtoul(argv[2], NULL, 16);
305 ulong cnt = simple_strtoul(argv[4], NULL, 16);
306 ulong n;
307
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200308#ifdef CONFIG_SYS_64BIT_LBA
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000309 lbaint_t blk = simple_strtoull(argv[3], NULL, 16);
wdenkc6097192002-11-03 00:24:07 +0000310
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000311 printf("\nIDE write: device %d block # %lld, count %ld ... ",
312 curr_device, blk, cnt);
wdenkc35ba4e2004-03-14 22:25:36 +0000313#else
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000314 lbaint_t blk = simple_strtoul(argv[3], NULL, 16);
wdenkc35ba4e2004-03-14 22:25:36 +0000315
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000316 printf("\nIDE write: device %d block # %ld, count %ld ... ",
317 curr_device, blk, cnt);
wdenkc35ba4e2004-03-14 22:25:36 +0000318#endif
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000319 n = ide_write(curr_device, blk, cnt, (ulong *) addr);
wdenkc6097192002-11-03 00:24:07 +0000320
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000321 printf("%ld blocks written: %s\n",
322 n, (n == cnt) ? "OK" : "ERROR");
323 if (n == cnt)
324 return 0;
325 else
326 return 1;
327 } else {
328 return cmd_usage(cmdtp);
329 }
wdenkc6097192002-11-03 00:24:07 +0000330
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000331 return rcode;
wdenkc6097192002-11-03 00:24:07 +0000332 }
wdenkc6097192002-11-03 00:24:07 +0000333}
334
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000335int do_diskboot(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
wdenkc6097192002-11-03 00:24:07 +0000336{
337 char *boot_device = NULL;
338 char *ep;
339 int dev, part = 0;
Marian Balakowicz41d71ed2008-01-08 18:14:09 +0100340 ulong addr, cnt;
wdenkc6097192002-11-03 00:24:07 +0000341 disk_partition_t info;
342 image_header_t *hdr;
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000343
Marian Balakowicze6bdd342008-03-12 10:33:01 +0100344#if defined(CONFIG_FIT)
Marian Balakowiczcefb4022008-06-06 23:07:40 +0200345 const void *fit_hdr = NULL;
Marian Balakowicze6bdd342008-03-12 10:33:01 +0100346#endif
wdenkc6097192002-11-03 00:24:07 +0000347
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000348 show_boot_progress(41);
wdenkc6097192002-11-03 00:24:07 +0000349 switch (argc) {
350 case 1:
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200351 addr = CONFIG_SYS_LOAD_ADDR;
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000352 boot_device = getenv("bootdevice");
wdenkc6097192002-11-03 00:24:07 +0000353 break;
354 case 2:
355 addr = simple_strtoul(argv[1], NULL, 16);
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000356 boot_device = getenv("bootdevice");
wdenkc6097192002-11-03 00:24:07 +0000357 break;
358 case 3:
359 addr = simple_strtoul(argv[1], NULL, 16);
360 boot_device = argv[2];
361 break;
362 default:
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000363 show_boot_progress(-42);
Wolfgang Denk3b683112010-07-17 01:06:04 +0200364 return cmd_usage(cmdtp);
wdenkc6097192002-11-03 00:24:07 +0000365 }
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000366 show_boot_progress(42);
wdenkc6097192002-11-03 00:24:07 +0000367
368 if (!boot_device) {
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000369 puts("\n** No boot device **\n");
370 show_boot_progress(-43);
wdenkc6097192002-11-03 00:24:07 +0000371 return 1;
372 }
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000373 show_boot_progress(43);
wdenkc6097192002-11-03 00:24:07 +0000374
375 dev = simple_strtoul(boot_device, &ep, 16);
376
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000377 if (ide_dev_desc[dev].type == DEV_TYPE_UNKNOWN) {
378 printf("\n** Device %d not available\n", dev);
379 show_boot_progress(-44);
wdenkc6097192002-11-03 00:24:07 +0000380 return 1;
381 }
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000382 show_boot_progress(44);
wdenkc6097192002-11-03 00:24:07 +0000383
384 if (*ep) {
385 if (*ep != ':') {
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000386 puts("\n** Invalid boot device, use `dev[:part]' **\n");
387 show_boot_progress(-45);
wdenkc6097192002-11-03 00:24:07 +0000388 return 1;
389 }
390 part = simple_strtoul(++ep, NULL, 16);
391 }
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000392 show_boot_progress(45);
393 if (get_partition_info(&ide_dev_desc[dev], part, &info)) {
394 show_boot_progress(-46);
wdenkc6097192002-11-03 00:24:07 +0000395 return 1;
396 }
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000397 show_boot_progress(46);
398 if ((strncmp((char *)info.type, BOOT_PART_TYPE, sizeof(info.type)) != 0)
399 &&
400 (strncmp((char *)info.type, BOOT_PART_COMP, sizeof(info.type)) != 0)
401 ) {
402 printf("\n** Invalid partition type \"%.32s\"" " (expect \""
403 BOOT_PART_TYPE "\")\n",
wdenkc6097192002-11-03 00:24:07 +0000404 info.type);
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000405 show_boot_progress(-47);
wdenkc6097192002-11-03 00:24:07 +0000406 return 1;
407 }
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000408 show_boot_progress(47);
wdenkc6097192002-11-03 00:24:07 +0000409
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000410 printf("\nLoading from IDE device %d, partition %d: "
411 "Name: %.32s Type: %.32s\n", dev, part, info.name, info.type);
wdenkc6097192002-11-03 00:24:07 +0000412
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000413 debug("First Block: %ld, # of blocks: %ld, Block Size: %ld\n",
414 info.start, info.size, info.blksz);
wdenkc6097192002-11-03 00:24:07 +0000415
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000416 if (ide_dev_desc[dev].
417 block_read(dev, info.start, 1, (ulong *) addr) != 1) {
418 printf("** Read error on %d:%d\n", dev, part);
419 show_boot_progress(-48);
wdenkc6097192002-11-03 00:24:07 +0000420 return 1;
421 }
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000422 show_boot_progress(48);
wdenkc6097192002-11-03 00:24:07 +0000423
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000424 switch (genimg_get_format((void *) addr)) {
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100425 case IMAGE_FORMAT_LEGACY:
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000426 hdr = (image_header_t *) addr;
wdenkc6097192002-11-03 00:24:07 +0000427
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000428 show_boot_progress(49);
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100429
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000430 if (!image_check_hcrc(hdr)) {
431 puts("\n** Bad Header Checksum **\n");
432 show_boot_progress(-50);
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100433 return 1;
434 }
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000435 show_boot_progress(50);
wdenkc6097192002-11-03 00:24:07 +0000436
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000437 image_print_contents(hdr);
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100438
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000439 cnt = image_get_image_size(hdr);
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100440 break;
441#if defined(CONFIG_FIT)
442 case IMAGE_FORMAT_FIT:
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000443 fit_hdr = (const void *) addr;
444 puts("Fit image detected...\n");
Marian Balakowicze6bdd342008-03-12 10:33:01 +0100445
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000446 cnt = fit_get_size(fit_hdr);
Marian Balakowicze6bdd342008-03-12 10:33:01 +0100447 break;
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100448#endif
449 default:
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000450 show_boot_progress(-49);
451 puts("** Unknown image type\n");
wdenkf1276d42005-02-03 23:00:49 +0000452 return 1;
453 }
wdenkf1276d42005-02-03 23:00:49 +0000454
wdenkf1276d42005-02-03 23:00:49 +0000455 cnt += info.blksz - 1;
456 cnt /= info.blksz;
457 cnt -= 1;
458
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000459 if (ide_dev_desc[dev].block_read(dev, info.start + 1, cnt,
460 (ulong *)(addr + info.blksz)) != cnt) {
461 printf("** Read error on %d:%d\n", dev, part);
462 show_boot_progress(-51);
wdenkc6097192002-11-03 00:24:07 +0000463 return 1;
464 }
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000465 show_boot_progress(51);
wdenkc6097192002-11-03 00:24:07 +0000466
Marian Balakowicze6bdd342008-03-12 10:33:01 +0100467#if defined(CONFIG_FIT)
468 /* This cannot be done earlier, we need complete FIT image in RAM first */
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000469 if (genimg_get_format((void *) addr) == IMAGE_FORMAT_FIT) {
470 if (!fit_check_format(fit_hdr)) {
471 show_boot_progress(-140);
472 puts("** Bad FIT image format\n");
Marian Balakowiczcefb4022008-06-06 23:07:40 +0200473 return 1;
474 }
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000475 show_boot_progress(141);
476 fit_print_contents(fit_hdr);
Marian Balakowiczcefb4022008-06-06 23:07:40 +0200477 }
Marian Balakowicze6bdd342008-03-12 10:33:01 +0100478#endif
wdenkc6097192002-11-03 00:24:07 +0000479
480 /* Loading ok, update default load address */
481
482 load_addr = addr;
483
Mike Frysinger194c2e82011-06-05 13:43:02 +0000484 return bootm_maybe_autostart(cmdtp, argv[0]);
wdenkc6097192002-11-03 00:24:07 +0000485}
486
487/* ------------------------------------------------------------------------- */
488
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000489inline void __ide_outb(int dev, int port, unsigned char val)
Stefan Roese37628252008-08-06 14:05:38 +0200490{
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000491 debug("ide_outb (dev= %d, port= 0x%x, val= 0x%02x) : @ 0x%08lx\n",
492 dev, port, val,
493 (ATA_CURR_BASE(dev) + CONFIG_SYS_ATA_PORT_ADDR(port)));
Macpaul Lind1e49942011-04-11 20:45:32 +0000494
495#if defined(CONFIG_IDE_AHB)
496 if (port) {
497 /* write command */
498 ide_write_register(dev, port, val);
499 } else {
500 /* write data */
501 outb(val, (ATA_CURR_BASE(dev)));
502 }
503#else
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000504 outb(val, (ATA_CURR_BASE(dev) + CONFIG_SYS_ATA_PORT_ADDR(port)));
Macpaul Lind1e49942011-04-11 20:45:32 +0000505#endif
Stefan Roese37628252008-08-06 14:05:38 +0200506}
Macpaul Lind1e49942011-04-11 20:45:32 +0000507
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000508void ide_outb(int dev, int port, unsigned char val)
509 __attribute__ ((weak, alias("__ide_outb")));
Stefan Roese37628252008-08-06 14:05:38 +0200510
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000511inline unsigned char __ide_inb(int dev, int port)
Stefan Roese37628252008-08-06 14:05:38 +0200512{
513 uchar val;
Macpaul Lind1e49942011-04-11 20:45:32 +0000514
515#if defined(CONFIG_IDE_AHB)
516 val = ide_read_register(dev, port);
517#else
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000518 val = inb((ATA_CURR_BASE(dev) + CONFIG_SYS_ATA_PORT_ADDR(port)));
Macpaul Lind1e49942011-04-11 20:45:32 +0000519#endif
520
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000521 debug("ide_inb (dev= %d, port= 0x%x) : @ 0x%08lx -> 0x%02x\n",
522 dev, port,
523 (ATA_CURR_BASE(dev) + CONFIG_SYS_ATA_PORT_ADDR(port)), val);
Stefan Roese37628252008-08-06 14:05:38 +0200524 return val;
525}
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000526
Kim Phillips8167f3c2009-05-19 12:53:36 -0500527unsigned char ide_inb(int dev, int port)
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000528 __attribute__ ((weak, alias("__ide_inb")));
Stefan Roese37628252008-08-06 14:05:38 +0200529
Steven A. Falco2b7574e2008-08-15 15:34:10 -0400530#ifdef CONFIG_TUNE_PIO
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000531inline int __ide_set_piomode(int pio_mode)
Steven A. Falco2b7574e2008-08-15 15:34:10 -0400532{
533 return 0;
534}
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000535
536inline int ide_set_piomode(int pio_mode)
537 __attribute__ ((weak, alias("__ide_set_piomode")));
Steven A. Falco2b7574e2008-08-15 15:34:10 -0400538#endif
539
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000540void ide_init(void)
wdenkc6097192002-11-03 00:24:07 +0000541{
wdenkc6097192002-11-03 00:24:07 +0000542
543#ifdef CONFIG_IDE_8xx_DIRECT
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000544 volatile immap_t *immr = (immap_t *) CONFIG_SYS_IMMR;
wdenkc6097192002-11-03 00:24:07 +0000545 volatile pcmconf8xx_t *pcmp = &(immr->im_pcmcia);
546#endif
547 unsigned char c;
548 int i, bus;
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000549
Wolfgang Denkb0b104a2010-06-13 18:28:54 +0200550#if defined(CONFIG_SC3)
Wolfgang Denkd2a7a742007-06-08 10:24:58 +0200551 unsigned int ata_reset_time = ATA_RESET_TIME;
Wolfgang Denk15c6b152007-04-11 17:22:55 +0200552#endif
wdenk7f00b942003-12-07 23:55:12 +0000553#ifdef CONFIG_IDE_8xx_PCCARD
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000554 extern int pcmcia_on(void);
555 extern int ide_devices_found; /* Initialized in check_ide_device() */
556#endif /* CONFIG_IDE_8xx_PCCARD */
wdenk7f00b942003-12-07 23:55:12 +0000557
558#ifdef CONFIG_IDE_PREINIT
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000559 extern int ide_preinit(void);
560
wdenk7f00b942003-12-07 23:55:12 +0000561 WATCHDOG_RESET();
562
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000563 if (ide_preinit()) {
564 puts("ide_preinit failed\n");
wdenk7f00b942003-12-07 23:55:12 +0000565 return;
566 }
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000567#endif /* CONFIG_IDE_PREINIT */
wdenkc6097192002-11-03 00:24:07 +0000568
569#ifdef CONFIG_IDE_8xx_PCCARD
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000570 extern int pcmcia_on(void);
571 extern int ide_devices_found; /* Initialized in check_ide_device() */
wdenkc6097192002-11-03 00:24:07 +0000572
573 WATCHDOG_RESET();
574
wdenk4fc95692003-02-28 00:49:47 +0000575 ide_devices_found = 0;
wdenkc6097192002-11-03 00:24:07 +0000576 /* initialize the PCMCIA IDE adapter card */
wdenk4fc95692003-02-28 00:49:47 +0000577 pcmcia_on();
578 if (!ide_devices_found)
wdenkc6097192002-11-03 00:24:07 +0000579 return;
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000580 udelay(1000000); /* 1 s */
581#endif /* CONFIG_IDE_8xx_PCCARD */
wdenkc6097192002-11-03 00:24:07 +0000582
583 WATCHDOG_RESET();
584
wdenk9f837932003-10-09 19:00:25 +0000585#ifdef CONFIG_IDE_8xx_DIRECT
wdenkc6097192002-11-03 00:24:07 +0000586 /* Initialize PIO timing tables */
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000587 for (i = 0; i <= IDE_MAX_PIO_MODE; ++i) {
588 pio_config_clk[i].t_setup =
589 PCMCIA_MK_CLKS(pio_config_ns[i].t_setup, gd->bus_clk);
590 pio_config_clk[i].t_length =
591 PCMCIA_MK_CLKS(pio_config_ns[i].t_length,
592 gd->bus_clk);
593 pio_config_clk[i].t_hold =
594 PCMCIA_MK_CLKS(pio_config_ns[i].t_hold, gd->bus_clk);
595 debug("PIO Mode %d: setup=%2d ns/%d clk" " len=%3d ns/%d clk"
596 " hold=%2d ns/%d clk\n", i, pio_config_ns[i].t_setup,
597 pio_config_clk[i].t_setup, pio_config_ns[i].t_length,
598 pio_config_clk[i].t_length, pio_config_ns[i].t_hold,
599 pio_config_clk[i].t_hold);
wdenkc6097192002-11-03 00:24:07 +0000600 }
wdenk9f837932003-10-09 19:00:25 +0000601#endif /* CONFIG_IDE_8xx_DIRECT */
wdenkc6097192002-11-03 00:24:07 +0000602
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000603 /*
604 * Reset the IDE just to be sure.
wdenkc6097192002-11-03 00:24:07 +0000605 * Light LED's to show
606 */
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000607 ide_led((LED_IDE1 | LED_IDE2), 1); /* LED's on */
608
609 /* ATAPI Drives seems to need a proper IDE Reset */
610 ide_reset();
wdenkc6097192002-11-03 00:24:07 +0000611
612#ifdef CONFIG_IDE_8xx_DIRECT
613 /* PCMCIA / IDE initialization for common mem space */
614 pcmp->pcmc_pgcrb = 0;
wdenkc6097192002-11-03 00:24:07 +0000615
616 /* start in PIO mode 0 - most relaxed timings */
617 pio_mode = 0;
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000618 set_pcmcia_timing(pio_mode);
wdenk9f837932003-10-09 19:00:25 +0000619#endif /* CONFIG_IDE_8xx_DIRECT */
wdenkc6097192002-11-03 00:24:07 +0000620
621 /*
622 * Wait for IDE to get ready.
623 * According to spec, this can take up to 31 seconds!
624 */
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000625 for (bus = 0; bus < CONFIG_SYS_IDE_MAXBUS; ++bus) {
626 int dev =
627 bus * (CONFIG_SYS_IDE_MAXDEVICE /
628 CONFIG_SYS_IDE_MAXBUS);
wdenkc6097192002-11-03 00:24:07 +0000629
wdenk4fc95692003-02-28 00:49:47 +0000630#ifdef CONFIG_IDE_8xx_PCCARD
631 /* Skip non-ide devices from probing */
632 if ((ide_devices_found & (1 << bus)) == 0) {
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000633 ide_led((LED_IDE1 | LED_IDE2), 0); /* LED's off */
wdenk4fc95692003-02-28 00:49:47 +0000634 continue;
635 }
636#endif
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000637 printf("Bus %d: ", bus);
wdenkc6097192002-11-03 00:24:07 +0000638
639 ide_bus_ok[bus] = 0;
640
641 /* Select device
642 */
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000643 udelay(100000); /* 100 ms */
644 ide_outb(dev, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(dev));
645 udelay(100000); /* 100 ms */
wdenkc6097192002-11-03 00:24:07 +0000646 i = 0;
647 do {
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000648 udelay(10000); /* 10 ms */
wdenkc6097192002-11-03 00:24:07 +0000649
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000650 c = ide_inb(dev, ATA_STATUS);
wdenkc6097192002-11-03 00:24:07 +0000651 i++;
Wolfgang Denkb0b104a2010-06-13 18:28:54 +0200652#if defined(CONFIG_SC3)
wdenk452cfd62002-11-19 11:04:11 +0000653 if (i > (ata_reset_time * 100)) {
654#else
wdenkc6097192002-11-03 00:24:07 +0000655 if (i > (ATA_RESET_TIME * 100)) {
wdenk452cfd62002-11-19 11:04:11 +0000656#endif
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000657 puts("** Timeout **\n");
658 /* LED's off */
659 ide_led((LED_IDE1 | LED_IDE2), 0);
wdenkc6097192002-11-03 00:24:07 +0000660 return;
661 }
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000662 if ((i >= 100) && ((i % 100) == 0))
663 putc('.');
664
wdenkc6097192002-11-03 00:24:07 +0000665 } while (c & ATA_STAT_BUSY);
666
667 if (c & (ATA_STAT_BUSY | ATA_STAT_FAULT)) {
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000668 puts("not available ");
669 debug("Status = 0x%02X ", c);
670#ifndef CONFIG_ATAPI /* ATAPI Devices do not set DRDY */
671 } else if ((c & ATA_STAT_READY) == 0) {
672 puts("not available ");
673 debug("Status = 0x%02X ", c);
wdenkc6097192002-11-03 00:24:07 +0000674#endif
675 } else {
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000676 puts("OK ");
wdenkc6097192002-11-03 00:24:07 +0000677 ide_bus_ok[bus] = 1;
678 }
679 WATCHDOG_RESET();
680 }
wdenk452cfd62002-11-19 11:04:11 +0000681
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000682 putc('\n');
wdenkc6097192002-11-03 00:24:07 +0000683
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000684 ide_led((LED_IDE1 | LED_IDE2), 0); /* LED's off */
wdenkc6097192002-11-03 00:24:07 +0000685
686 curr_device = -1;
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000687 for (i = 0; i < CONFIG_SYS_IDE_MAXDEVICE; ++i) {
wdenkc6097192002-11-03 00:24:07 +0000688#ifdef CONFIG_IDE_LED
689 int led = (IDE_BUS(i) == 0) ? LED_IDE1 : LED_IDE2;
690#endif
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000691 ide_dev_desc[i].type = DEV_TYPE_UNKNOWN;
692 ide_dev_desc[i].if_type = IF_TYPE_IDE;
693 ide_dev_desc[i].dev = i;
694 ide_dev_desc[i].part_type = PART_TYPE_UNKNOWN;
695 ide_dev_desc[i].blksz = 0;
696 ide_dev_desc[i].lba = 0;
697 ide_dev_desc[i].block_read = ide_read;
Macpaul Lind1e49942011-04-11 20:45:32 +0000698 ide_dev_desc[i].block_write = ide_write;
wdenkc6097192002-11-03 00:24:07 +0000699 if (!ide_bus_ok[IDE_BUS(i)])
700 continue;
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000701 ide_led(led, 1); /* LED on */
wdenkc6097192002-11-03 00:24:07 +0000702 ide_ident(&ide_dev_desc[i]);
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000703 ide_led(led, 0); /* LED off */
wdenkc6097192002-11-03 00:24:07 +0000704 dev_print(&ide_dev_desc[i]);
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000705
wdenkc6097192002-11-03 00:24:07 +0000706 if ((ide_dev_desc[i].lba > 0) && (ide_dev_desc[i].blksz > 0)) {
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000707 /* initialize partition type */
708 init_part(&ide_dev_desc[i]);
wdenkc6097192002-11-03 00:24:07 +0000709 if (curr_device < 0)
710 curr_device = i;
711 }
712 }
713 WATCHDOG_RESET();
714}
715
716/* ------------------------------------------------------------------------- */
717
Matthew McClintock6252b4f2011-05-24 05:31:19 +0000718#ifdef CONFIG_PARTITIONS
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000719block_dev_desc_t *ide_get_dev(int dev)
wdenkc6097192002-11-03 00:24:07 +0000720{
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200721 return (dev < CONFIG_SYS_IDE_MAXDEVICE) ? &ide_dev_desc[dev] : NULL;
wdenkc6097192002-11-03 00:24:07 +0000722}
Matthew McClintock6252b4f2011-05-24 05:31:19 +0000723#endif
wdenkc6097192002-11-03 00:24:07 +0000724
725
726#ifdef CONFIG_IDE_8xx_DIRECT
727
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000728static void set_pcmcia_timing(int pmode)
wdenkc6097192002-11-03 00:24:07 +0000729{
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000730 volatile immap_t *immr = (immap_t *) CONFIG_SYS_IMMR;
wdenkc6097192002-11-03 00:24:07 +0000731 volatile pcmconf8xx_t *pcmp = &(immr->im_pcmcia);
732 ulong timings;
733
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000734 debug("Set timing for PIO Mode %d\n", pmode);
wdenkc6097192002-11-03 00:24:07 +0000735
736 timings = PCMCIA_SHT(pio_config_clk[pmode].t_hold)
737 | PCMCIA_SST(pio_config_clk[pmode].t_setup)
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000738 | PCMCIA_SL(pio_config_clk[pmode].t_length);
wdenkc6097192002-11-03 00:24:07 +0000739
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000740 /*
741 * IDE 0
wdenkc6097192002-11-03 00:24:07 +0000742 */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200743 pcmp->pcmc_pbr0 = CONFIG_SYS_PCMCIA_PBR0;
744 pcmp->pcmc_por0 = CONFIG_SYS_PCMCIA_POR0
745#if (CONFIG_SYS_PCMCIA_POR0 != 0)
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000746 | timings
wdenkc6097192002-11-03 00:24:07 +0000747#endif
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000748 ;
749 debug("PBR0: %08x POR0: %08x\n", pcmp->pcmc_pbr0, pcmp->pcmc_por0);
wdenkc6097192002-11-03 00:24:07 +0000750
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200751 pcmp->pcmc_pbr1 = CONFIG_SYS_PCMCIA_PBR1;
752 pcmp->pcmc_por1 = CONFIG_SYS_PCMCIA_POR1
753#if (CONFIG_SYS_PCMCIA_POR1 != 0)
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000754 | timings
wdenkc6097192002-11-03 00:24:07 +0000755#endif
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000756 ;
757 debug("PBR1: %08x POR1: %08x\n", pcmp->pcmc_pbr1, pcmp->pcmc_por1);
wdenkc6097192002-11-03 00:24:07 +0000758
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200759 pcmp->pcmc_pbr2 = CONFIG_SYS_PCMCIA_PBR2;
760 pcmp->pcmc_por2 = CONFIG_SYS_PCMCIA_POR2
761#if (CONFIG_SYS_PCMCIA_POR2 != 0)
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000762 | timings
wdenkc6097192002-11-03 00:24:07 +0000763#endif
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000764 ;
765 debug("PBR2: %08x POR2: %08x\n", pcmp->pcmc_pbr2, pcmp->pcmc_por2);
wdenkc6097192002-11-03 00:24:07 +0000766
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200767 pcmp->pcmc_pbr3 = CONFIG_SYS_PCMCIA_PBR3;
768 pcmp->pcmc_por3 = CONFIG_SYS_PCMCIA_POR3
769#if (CONFIG_SYS_PCMCIA_POR3 != 0)
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000770 | timings
wdenkc6097192002-11-03 00:24:07 +0000771#endif
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000772 ;
773 debug("PBR3: %08x POR3: %08x\n", pcmp->pcmc_pbr3, pcmp->pcmc_por3);
wdenkc6097192002-11-03 00:24:07 +0000774
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000775 /*
776 * IDE 1
wdenkc6097192002-11-03 00:24:07 +0000777 */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200778 pcmp->pcmc_pbr4 = CONFIG_SYS_PCMCIA_PBR4;
779 pcmp->pcmc_por4 = CONFIG_SYS_PCMCIA_POR4
780#if (CONFIG_SYS_PCMCIA_POR4 != 0)
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000781 | timings
wdenkc6097192002-11-03 00:24:07 +0000782#endif
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000783 ;
784 debug("PBR4: %08x POR4: %08x\n", pcmp->pcmc_pbr4, pcmp->pcmc_por4);
wdenkc6097192002-11-03 00:24:07 +0000785
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200786 pcmp->pcmc_pbr5 = CONFIG_SYS_PCMCIA_PBR5;
787 pcmp->pcmc_por5 = CONFIG_SYS_PCMCIA_POR5
788#if (CONFIG_SYS_PCMCIA_POR5 != 0)
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000789 | timings
wdenkc6097192002-11-03 00:24:07 +0000790#endif
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000791 ;
792 debug("PBR5: %08x POR5: %08x\n", pcmp->pcmc_pbr5, pcmp->pcmc_por5);
wdenkc6097192002-11-03 00:24:07 +0000793
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200794 pcmp->pcmc_pbr6 = CONFIG_SYS_PCMCIA_PBR6;
795 pcmp->pcmc_por6 = CONFIG_SYS_PCMCIA_POR6
796#if (CONFIG_SYS_PCMCIA_POR6 != 0)
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000797 | timings
wdenkc6097192002-11-03 00:24:07 +0000798#endif
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000799 ;
800 debug("PBR6: %08x POR6: %08x\n", pcmp->pcmc_pbr6, pcmp->pcmc_por6);
wdenkc6097192002-11-03 00:24:07 +0000801
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200802 pcmp->pcmc_pbr7 = CONFIG_SYS_PCMCIA_PBR7;
803 pcmp->pcmc_por7 = CONFIG_SYS_PCMCIA_POR7
804#if (CONFIG_SYS_PCMCIA_POR7 != 0)
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000805 | timings
wdenkc6097192002-11-03 00:24:07 +0000806#endif
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000807 ;
808 debug("PBR7: %08x POR7: %08x\n", pcmp->pcmc_pbr7, pcmp->pcmc_por7);
wdenkc6097192002-11-03 00:24:07 +0000809
810}
811
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000812#endif /* CONFIG_IDE_8xx_DIRECT */
wdenkc6097192002-11-03 00:24:07 +0000813
814/* ------------------------------------------------------------------------- */
815
wdenk9b7f3842003-10-09 20:09:04 +0000816/* We only need to swap data if we are running on a big endian cpu. */
817/* But Au1x00 cpu:s already swaps data in big endian mode! */
Shinya Kuribayashi9b33e662011-02-05 19:07:00 +0900818#if defined(__LITTLE_ENDIAN) || \
819 (defined(CONFIG_SOC_AU1X00) && !defined(CONFIG_GTH2))
wdenk9b7f3842003-10-09 20:09:04 +0000820#define input_swap_data(x,y,z) input_data(x,y,z)
821#else
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000822static void input_swap_data(int dev, ulong *sect_buf, int words)
wdenkc6097192002-11-03 00:24:07 +0000823{
Wolfgang Denkfb52e952010-09-19 21:28:25 +0200824#if defined(CONFIG_CPC45)
wdenk2f99a692004-01-04 22:51:12 +0000825 uchar i;
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000826 volatile uchar *pbuf_even =
827 (uchar *) (ATA_CURR_BASE(dev) + ATA_DATA_EVEN);
828 volatile uchar *pbuf_odd =
829 (uchar *) (ATA_CURR_BASE(dev) + ATA_DATA_ODD);
830 ushort *dbuf = (ushort *) sect_buf;
wdenk2f99a692004-01-04 22:51:12 +0000831
832 while (words--) {
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000833 for (i = 0; i < 2; i++) {
834 *(((uchar *) (dbuf)) + 1) = *pbuf_even;
835 *(uchar *) dbuf = *pbuf_odd;
836 dbuf += 1;
wdenk2f99a692004-01-04 22:51:12 +0000837 }
838 }
wdenkb995b0f2005-03-06 01:21:30 +0000839#else
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000840 volatile ushort *pbuf =
841 (ushort *) (ATA_CURR_BASE(dev) + ATA_DATA_REG);
842 ushort *dbuf = (ushort *) sect_buf;
wdenkf1276d42005-02-03 23:00:49 +0000843
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000844 debug("in input swap data base for read is %lx\n",
845 (unsigned long) pbuf);
wdenkf1276d42005-02-03 23:00:49 +0000846
847 while (words--) {
Wolfgang Denk39c76422006-06-16 17:32:31 +0200848#ifdef __MIPS__
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000849 *dbuf++ = swab16p((u16 *) pbuf);
850 *dbuf++ = swab16p((u16 *) pbuf);
Heiko Schocher633e03a2007-06-22 19:11:54 +0200851#elif defined(CONFIG_PCS440EP)
852 *dbuf++ = *pbuf;
853 *dbuf++ = *pbuf;
Wolfgang Denk39c76422006-06-16 17:32:31 +0200854#else
wdenkf1276d42005-02-03 23:00:49 +0000855 *dbuf++ = ld_le16(pbuf);
856 *dbuf++ = ld_le16(pbuf);
Wolfgang Denk39c76422006-06-16 17:32:31 +0200857#endif /* !MIPS */
wdenkf1276d42005-02-03 23:00:49 +0000858 }
859#endif
wdenkc6097192002-11-03 00:24:07 +0000860}
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000861#endif /* __LITTLE_ENDIAN || CONFIG_AU1X00 */
wdenk591dda52002-11-18 00:14:45 +0000862
863
Albert Aribaud036c6b42010-08-08 05:17:05 +0530864#if defined(CONFIG_IDE_SWAP_IO)
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000865static void output_data(int dev, const ulong *sect_buf, int words)
wdenkc6097192002-11-03 00:24:07 +0000866{
Wolfgang Denkfb52e952010-09-19 21:28:25 +0200867#if defined(CONFIG_CPC45)
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000868 uchar *dbuf;
869 volatile uchar *pbuf_even;
870 volatile uchar *pbuf_odd;
wdenk2f99a692004-01-04 22:51:12 +0000871
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000872 pbuf_even = (uchar *) (ATA_CURR_BASE(dev) + ATA_DATA_EVEN);
873 pbuf_odd = (uchar *) (ATA_CURR_BASE(dev) + ATA_DATA_ODD);
874 dbuf = (uchar *) sect_buf;
wdenk2f99a692004-01-04 22:51:12 +0000875 while (words--) {
wdenk20c98a62004-04-23 20:32:05 +0000876 EIEIO;
wdenk2f99a692004-01-04 22:51:12 +0000877 *pbuf_even = *dbuf++;
wdenk20c98a62004-04-23 20:32:05 +0000878 EIEIO;
wdenk2f99a692004-01-04 22:51:12 +0000879 *pbuf_odd = *dbuf++;
wdenk20c98a62004-04-23 20:32:05 +0000880 EIEIO;
wdenk2f99a692004-01-04 22:51:12 +0000881 *pbuf_even = *dbuf++;
wdenk20c98a62004-04-23 20:32:05 +0000882 EIEIO;
wdenk2f99a692004-01-04 22:51:12 +0000883 *pbuf_odd = *dbuf++;
884 }
wdenkf1276d42005-02-03 23:00:49 +0000885#else
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000886 ushort *dbuf;
887 volatile ushort *pbuf;
wdenkf1276d42005-02-03 23:00:49 +0000888
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000889 pbuf = (ushort *) (ATA_CURR_BASE(dev) + ATA_DATA_REG);
890 dbuf = (ushort *) sect_buf;
wdenkf1276d42005-02-03 23:00:49 +0000891 while (words--) {
Heiko Schocher633e03a2007-06-22 19:11:54 +0200892#if defined(CONFIG_PCS440EP)
893 /* not tested, because CF was write protected */
894 EIEIO;
895 *pbuf = ld_le16(dbuf++);
896 EIEIO;
897 *pbuf = ld_le16(dbuf++);
898#else
wdenkf1276d42005-02-03 23:00:49 +0000899 EIEIO;
900 *pbuf = *dbuf++;
901 EIEIO;
902 *pbuf = *dbuf++;
Heiko Schocher633e03a2007-06-22 19:11:54 +0200903#endif
wdenkf1276d42005-02-03 23:00:49 +0000904 }
905#endif
wdenkc6097192002-11-03 00:24:07 +0000906}
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000907#else /* ! CONFIG_IDE_SWAP_IO */
908static void output_data(int dev, const ulong *sect_buf, int words)
wdenk591dda52002-11-18 00:14:45 +0000909{
Macpaul Lind1e49942011-04-11 20:45:32 +0000910#if defined(CONFIG_IDE_AHB)
911 ide_write_data(dev, sect_buf, words);
912#else
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000913 outsw(ATA_CURR_BASE(dev) + ATA_DATA_REG, sect_buf, words << 1);
Macpaul Lind1e49942011-04-11 20:45:32 +0000914#endif
wdenk591dda52002-11-18 00:14:45 +0000915}
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000916#endif /* CONFIG_IDE_SWAP_IO */
wdenkc6097192002-11-03 00:24:07 +0000917
Albert Aribaud036c6b42010-08-08 05:17:05 +0530918#if defined(CONFIG_IDE_SWAP_IO)
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000919static void input_data(int dev, ulong *sect_buf, int words)
wdenkc6097192002-11-03 00:24:07 +0000920{
Wolfgang Denkfb52e952010-09-19 21:28:25 +0200921#if defined(CONFIG_CPC45)
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000922 uchar *dbuf;
923 volatile uchar *pbuf_even;
924 volatile uchar *pbuf_odd;
wdenk2f99a692004-01-04 22:51:12 +0000925
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000926 pbuf_even = (uchar *) (ATA_CURR_BASE(dev) + ATA_DATA_EVEN);
927 pbuf_odd = (uchar *) (ATA_CURR_BASE(dev) + ATA_DATA_ODD);
928 dbuf = (uchar *) sect_buf;
wdenk2f99a692004-01-04 22:51:12 +0000929 while (words--) {
wdenkf1276d42005-02-03 23:00:49 +0000930 *dbuf++ = *pbuf_even;
wdenk20c98a62004-04-23 20:32:05 +0000931 EIEIO;
wdenkf1276d42005-02-03 23:00:49 +0000932 SYNC;
933 *dbuf++ = *pbuf_odd;
wdenkf6925c72005-01-22 18:26:04 +0000934 EIEIO;
wdenkf1276d42005-02-03 23:00:49 +0000935 SYNC;
wdenk2f99a692004-01-04 22:51:12 +0000936 *dbuf++ = *pbuf_even;
wdenk20c98a62004-04-23 20:32:05 +0000937 EIEIO;
wdenkf1276d42005-02-03 23:00:49 +0000938 SYNC;
wdenk2f99a692004-01-04 22:51:12 +0000939 *dbuf++ = *pbuf_odd;
wdenk20c98a62004-04-23 20:32:05 +0000940 EIEIO;
wdenkf1276d42005-02-03 23:00:49 +0000941 SYNC;
942 }
943#else
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000944 ushort *dbuf;
945 volatile ushort *pbuf;
wdenkf1276d42005-02-03 23:00:49 +0000946
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000947 pbuf = (ushort *) (ATA_CURR_BASE(dev) + ATA_DATA_REG);
948 dbuf = (ushort *) sect_buf;
wdenkf1276d42005-02-03 23:00:49 +0000949
950 debug("in input data base for read is %lx\n", (unsigned long) pbuf);
951
952 while (words--) {
Heiko Schocher633e03a2007-06-22 19:11:54 +0200953#if defined(CONFIG_PCS440EP)
954 EIEIO;
955 *dbuf++ = ld_le16(pbuf);
956 EIEIO;
957 *dbuf++ = ld_le16(pbuf);
958#else
wdenk20c98a62004-04-23 20:32:05 +0000959 EIEIO;
wdenkf1276d42005-02-03 23:00:49 +0000960 *dbuf++ = *pbuf;
wdenkf6925c72005-01-22 18:26:04 +0000961 EIEIO;
wdenkf1276d42005-02-03 23:00:49 +0000962 *dbuf++ = *pbuf;
Heiko Schocher633e03a2007-06-22 19:11:54 +0200963#endif
wdenk2f99a692004-01-04 22:51:12 +0000964 }
wdenkf1276d42005-02-03 23:00:49 +0000965#endif
wdenkc6097192002-11-03 00:24:07 +0000966}
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000967#else /* ! CONFIG_IDE_SWAP_IO */
968static void input_data(int dev, ulong *sect_buf, int words)
wdenk591dda52002-11-18 00:14:45 +0000969{
Macpaul Lind1e49942011-04-11 20:45:32 +0000970#if defined(CONFIG_IDE_AHB)
971 ide_read_data(dev, sect_buf, words);
972#else
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000973 insw(ATA_CURR_BASE(dev) + ATA_DATA_REG, sect_buf, words << 1);
Macpaul Lind1e49942011-04-11 20:45:32 +0000974#endif
wdenk591dda52002-11-18 00:14:45 +0000975}
976
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000977#endif /* CONFIG_IDE_SWAP_IO */
wdenkc6097192002-11-03 00:24:07 +0000978
979/* -------------------------------------------------------------------------
980 */
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000981static void ide_ident(block_dev_desc_t *dev_desc)
wdenkc6097192002-11-03 00:24:07 +0000982{
wdenkc6097192002-11-03 00:24:07 +0000983 unsigned char c;
Marek Vasute49757e2011-08-20 09:15:13 +0000984 hd_driveid_t iop;
wdenkc6097192002-11-03 00:24:07 +0000985
wdenke2d6d742004-09-28 20:34:50 +0000986#ifdef CONFIG_ATAPI
987 int retries = 0;
wdenk452cfd62002-11-19 11:04:11 +0000988#endif
989
Steven A. Falco2b7574e2008-08-15 15:34:10 -0400990#ifdef CONFIG_TUNE_PIO
991 int pio_mode;
992#endif
993
wdenkc6097192002-11-03 00:24:07 +0000994#if 0
995 int mode, cycle_time;
996#endif
997 int device;
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000998
999 device = dev_desc->dev;
1000 printf(" Device %d: ", device);
wdenkc6097192002-11-03 00:24:07 +00001001
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001002 ide_led(DEVICE_LED(device), 1); /* LED on */
wdenkc6097192002-11-03 00:24:07 +00001003 /* Select device
1004 */
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001005 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
1006 dev_desc->if_type = IF_TYPE_IDE;
wdenkc6097192002-11-03 00:24:07 +00001007#ifdef CONFIG_ATAPI
wdenk452cfd62002-11-19 11:04:11 +00001008
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001009 retries = 0;
wdenk452cfd62002-11-19 11:04:11 +00001010
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001011 /* Warning: This will be tricky to read */
1012 while (retries <= 1) {
1013 /* check signature */
1014 if ((ide_inb(device, ATA_SECT_CNT) == 0x01) &&
1015 (ide_inb(device, ATA_SECT_NUM) == 0x01) &&
1016 (ide_inb(device, ATA_CYL_LOW) == 0x14) &&
1017 (ide_inb(device, ATA_CYL_HIGH) == 0xEB)) {
1018 /* ATAPI Signature found */
1019 dev_desc->if_type = IF_TYPE_ATAPI;
1020 /*
1021 * Start Ident Command
1022 */
1023 ide_outb(device, ATA_COMMAND, ATAPI_CMD_IDENT);
1024 /*
1025 * Wait for completion - ATAPI devices need more time
1026 * to become ready
1027 */
1028 c = ide_wait(device, ATAPI_TIME_OUT);
1029 } else
wdenkc6097192002-11-03 00:24:07 +00001030#endif
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001031 {
1032 /*
1033 * Start Ident Command
1034 */
1035 ide_outb(device, ATA_COMMAND, ATA_CMD_IDENT);
wdenkc6097192002-11-03 00:24:07 +00001036
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001037 /*
1038 * Wait for completion
1039 */
1040 c = ide_wait(device, IDE_TIME_OUT);
1041 }
1042 ide_led(DEVICE_LED(device), 0); /* LED off */
wdenkc6097192002-11-03 00:24:07 +00001043
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001044 if (((c & ATA_STAT_DRQ) == 0) ||
1045 ((c & (ATA_STAT_FAULT | ATA_STAT_ERR)) != 0)) {
wdenke2d6d742004-09-28 20:34:50 +00001046#ifdef CONFIG_ATAPI
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001047 {
1048 /*
1049 * Need to soft reset the device
1050 * in case it's an ATAPI...
1051 */
1052 debug("Retrying...\n");
1053 ide_outb(device, ATA_DEV_HD,
1054 ATA_LBA | ATA_DEVICE(device));
1055 udelay(100000);
1056 ide_outb(device, ATA_COMMAND, 0x08);
1057 udelay(500000); /* 500 ms */
1058 }
1059 /*
1060 * Select device
1061 */
1062 ide_outb(device, ATA_DEV_HD,
1063 ATA_LBA | ATA_DEVICE(device));
1064 retries++;
wdenke2d6d742004-09-28 20:34:50 +00001065#else
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001066 return;
wdenke2d6d742004-09-28 20:34:50 +00001067#endif
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001068 }
wdenke2d6d742004-09-28 20:34:50 +00001069#ifdef CONFIG_ATAPI
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001070 else
1071 break;
1072 } /* see above - ugly to read */
wdenke2d6d742004-09-28 20:34:50 +00001073
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001074 if (retries == 2) /* Not found */
wdenke2d6d742004-09-28 20:34:50 +00001075 return;
1076#endif
wdenkc6097192002-11-03 00:24:07 +00001077
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001078 input_swap_data(device, (ulong *)&iop, ATA_SECTORWORDS);
wdenkc6097192002-11-03 00:24:07 +00001079
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001080 ident_cpy((unsigned char *) dev_desc->revision, iop.fw_rev,
1081 sizeof(dev_desc->revision));
1082 ident_cpy((unsigned char *) dev_desc->vendor, iop.model,
1083 sizeof(dev_desc->vendor));
1084 ident_cpy((unsigned char *) dev_desc->product, iop.serial_no,
1085 sizeof(dev_desc->product));
wdenkacd9b102004-03-14 00:59:59 +00001086#ifdef __LITTLE_ENDIAN
1087 /*
Richard Retanubun54f30622008-11-06 14:01:51 -05001088 * firmware revision, model, and serial number have Big Endian Byte
1089 * order in Word. Convert all three to little endian.
wdenkacd9b102004-03-14 00:59:59 +00001090 *
1091 * See CF+ and CompactFlash Specification Revision 2.0:
Richard Retanubun54f30622008-11-06 14:01:51 -05001092 * 6.2.1.6: Identify Drive, Table 39 for more details
wdenkacd9b102004-03-14 00:59:59 +00001093 */
1094
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001095 strswab(dev_desc->revision);
1096 strswab(dev_desc->vendor);
1097 strswab(dev_desc->product);
wdenkacd9b102004-03-14 00:59:59 +00001098#endif /* __LITTLE_ENDIAN */
wdenkc6097192002-11-03 00:24:07 +00001099
Marek Vasute49757e2011-08-20 09:15:13 +00001100 if ((iop.config & 0x0080) == 0x0080)
wdenkc6097192002-11-03 00:24:07 +00001101 dev_desc->removable = 1;
1102 else
1103 dev_desc->removable = 0;
1104
Steven A. Falco2b7574e2008-08-15 15:34:10 -04001105#ifdef CONFIG_TUNE_PIO
1106 /* Mode 0 - 2 only, are directly determined by word 51. */
Marek Vasute49757e2011-08-20 09:15:13 +00001107 pio_mode = iop.tPIO;
Steven A. Falco2b7574e2008-08-15 15:34:10 -04001108 if (pio_mode > 2) {
1109 printf("WARNING: Invalid PIO (word 51 = %d).\n", pio_mode);
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001110 /* Force it to dead slow, and hope for the best... */
1111 pio_mode = 0;
Steven A. Falco2b7574e2008-08-15 15:34:10 -04001112 }
1113
1114 /* Any CompactFlash Storage Card that supports PIO mode 3 or above
1115 * shall set bit 1 of word 53 to one and support the fields contained
1116 * in words 64 through 70.
1117 */
Marek Vasute49757e2011-08-20 09:15:13 +00001118 if (iop.field_valid & 0x02) {
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001119 /*
1120 * Mode 3 and above are possible. Check in order from slow
Steven A. Falco2b7574e2008-08-15 15:34:10 -04001121 * to fast, so we wind up with the highest mode allowed.
1122 */
Marek Vasute49757e2011-08-20 09:15:13 +00001123 if (iop.eide_pio_modes & 0x01)
Steven A. Falco2b7574e2008-08-15 15:34:10 -04001124 pio_mode = 3;
Marek Vasute49757e2011-08-20 09:15:13 +00001125 if (iop.eide_pio_modes & 0x02)
Steven A. Falco2b7574e2008-08-15 15:34:10 -04001126 pio_mode = 4;
Marek Vasute49757e2011-08-20 09:15:13 +00001127 if (ata_id_is_cfa((u16 *)&iop)) {
1128 if ((iop.cf_advanced_caps & 0x07) == 0x01)
Steven A. Falco2b7574e2008-08-15 15:34:10 -04001129 pio_mode = 5;
Marek Vasute49757e2011-08-20 09:15:13 +00001130 if ((iop.cf_advanced_caps & 0x07) == 0x02)
Steven A. Falco2b7574e2008-08-15 15:34:10 -04001131 pio_mode = 6;
1132 }
1133 }
1134
1135 /* System-specific, depends on bus speeds, etc. */
1136 ide_set_piomode(pio_mode);
1137#endif /* CONFIG_TUNE_PIO */
1138
wdenkc6097192002-11-03 00:24:07 +00001139#if 0
1140 /*
1141 * Drive PIO mode autoselection
1142 */
Marek Vasute49757e2011-08-20 09:15:13 +00001143 mode = iop.tPIO;
wdenkc6097192002-11-03 00:24:07 +00001144
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001145 printf("tPIO = 0x%02x = %d\n", mode, mode);
wdenkc6097192002-11-03 00:24:07 +00001146 if (mode > 2) { /* 2 is maximum allowed tPIO value */
1147 mode = 2;
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001148 debug("Override tPIO -> 2\n");
wdenkc6097192002-11-03 00:24:07 +00001149 }
Marek Vasute49757e2011-08-20 09:15:13 +00001150 if (iop.field_valid & 2) { /* drive implements ATA2? */
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001151 debug("Drive implements ATA2\n");
Marek Vasute49757e2011-08-20 09:15:13 +00001152 if (iop.capability & 8) { /* drive supports use_iordy? */
1153 cycle_time = iop.eide_pio_iordy;
wdenkc6097192002-11-03 00:24:07 +00001154 } else {
Marek Vasute49757e2011-08-20 09:15:13 +00001155 cycle_time = iop.eide_pio;
wdenkc6097192002-11-03 00:24:07 +00001156 }
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001157 debug("cycle time = %d\n", cycle_time);
wdenkc6097192002-11-03 00:24:07 +00001158 mode = 4;
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001159 if (cycle_time > 120)
1160 mode = 3; /* 120 ns for PIO mode 4 */
1161 if (cycle_time > 180)
1162 mode = 2; /* 180 ns for PIO mode 3 */
1163 if (cycle_time > 240)
1164 mode = 1; /* 240 ns for PIO mode 4 */
1165 if (cycle_time > 383)
1166 mode = 0; /* 383 ns for PIO mode 4 */
wdenkc6097192002-11-03 00:24:07 +00001167 }
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001168 printf("PIO mode to use: PIO %d\n", mode);
wdenkc6097192002-11-03 00:24:07 +00001169#endif /* 0 */
1170
1171#ifdef CONFIG_ATAPI
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001172 if (dev_desc->if_type == IF_TYPE_ATAPI) {
wdenkc6097192002-11-03 00:24:07 +00001173 atapi_inquiry(dev_desc);
1174 return;
1175 }
1176#endif /* CONFIG_ATAPI */
1177
wdenkacd9b102004-03-14 00:59:59 +00001178#ifdef __BIG_ENDIAN
wdenkc6097192002-11-03 00:24:07 +00001179 /* swap shorts */
Marek Vasute49757e2011-08-20 09:15:13 +00001180 dev_desc->lba = (iop.lba_capacity << 16) | (iop.lba_capacity >> 16);
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001181#else /* ! __BIG_ENDIAN */
wdenkacd9b102004-03-14 00:59:59 +00001182 /*
1183 * do not swap shorts on little endian
1184 *
1185 * See CF+ and CompactFlash Specification Revision 2.0:
1186 * 6.2.1.6: Identfy Drive, Table 39, Word Address 57-58 for details.
1187 */
Marek Vasute49757e2011-08-20 09:15:13 +00001188 dev_desc->lba = iop.lba_capacity;
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001189#endif /* __BIG_ENDIAN */
wdenkf602aa02004-03-13 23:29:43 +00001190
wdenkc35ba4e2004-03-14 22:25:36 +00001191#ifdef CONFIG_LBA48
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001192 if (iop.command_set_2 & 0x0400) { /* LBA 48 support */
wdenk05939202004-04-18 17:39:38 +00001193 dev_desc->lba48 = 1;
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001194 dev_desc->lba = (unsigned long long) iop.lba48_capacity[0] |
1195 ((unsigned long long) iop.lba48_capacity[1] << 16) |
1196 ((unsigned long long) iop.lba48_capacity[2] << 32) |
1197 ((unsigned long long) iop.lba48_capacity[3] << 48);
wdenkf602aa02004-03-13 23:29:43 +00001198 } else {
wdenkf602aa02004-03-13 23:29:43 +00001199 dev_desc->lba48 = 0;
1200 }
1201#endif /* CONFIG_LBA48 */
wdenkc6097192002-11-03 00:24:07 +00001202 /* assuming HD */
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001203 dev_desc->type = DEV_TYPE_HARDDISK;
1204 dev_desc->blksz = ATA_BLOCKSIZE;
1205 dev_desc->lun = 0; /* just to fill something in... */
wdenkc6097192002-11-03 00:24:07 +00001206
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001207#if 0 /* only used to test the powersaving mode,
1208 * if enabled, the drive goes after 5 sec
1209 * in standby mode */
1210 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
1211 c = ide_wait(device, IDE_TIME_OUT);
1212 ide_outb(device, ATA_SECT_CNT, 1);
1213 ide_outb(device, ATA_LBA_LOW, 0);
1214 ide_outb(device, ATA_LBA_MID, 0);
1215 ide_outb(device, ATA_LBA_HIGH, 0);
1216 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
1217 ide_outb(device, ATA_COMMAND, 0xe3);
1218 udelay(50);
1219 c = ide_wait(device, IDE_TIME_OUT); /* can't take over 500 ms */
wdenkc6097192002-11-03 00:24:07 +00001220#endif
1221}
1222
1223
1224/* ------------------------------------------------------------------------- */
1225
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001226ulong ide_read(int device, lbaint_t blknr, ulong blkcnt, void *buffer)
wdenkc6097192002-11-03 00:24:07 +00001227{
1228 ulong n = 0;
1229 unsigned char c;
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001230 unsigned char pwrsave = 0; /* power save */
1231
wdenkc35ba4e2004-03-14 22:25:36 +00001232#ifdef CONFIG_LBA48
wdenkf602aa02004-03-13 23:29:43 +00001233 unsigned char lba48 = 0;
wdenkc6097192002-11-03 00:24:07 +00001234
Guennadi Liakhovetskib71f1ca2008-04-28 14:36:06 +02001235 if (blknr & 0x0000fffff0000000ULL) {
wdenkf602aa02004-03-13 23:29:43 +00001236 /* more than 28 bits used, use 48bit mode */
1237 lba48 = 1;
1238 }
1239#endif
Marek Vasut92131712011-10-25 11:39:15 +02001240 debug("ide_read dev %d start %lX, blocks %lX buffer at %lX\n",
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001241 device, blknr, blkcnt, (ulong) buffer);
wdenkc6097192002-11-03 00:24:07 +00001242
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001243 ide_led(DEVICE_LED(device), 1); /* LED on */
wdenkc6097192002-11-03 00:24:07 +00001244
1245 /* Select device
1246 */
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001247 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
1248 c = ide_wait(device, IDE_TIME_OUT);
wdenkc6097192002-11-03 00:24:07 +00001249
1250 if (c & ATA_STAT_BUSY) {
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001251 printf("IDE read: device %d not ready\n", device);
wdenkc6097192002-11-03 00:24:07 +00001252 goto IDE_READ_E;
1253 }
1254
1255 /* first check if the drive is in Powersaving mode, if yes,
1256 * increase the timeout value */
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001257 ide_outb(device, ATA_COMMAND, ATA_CMD_CHK_PWR);
1258 udelay(50);
wdenkc6097192002-11-03 00:24:07 +00001259
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001260 c = ide_wait(device, IDE_TIME_OUT); /* can't take over 500 ms */
wdenkc6097192002-11-03 00:24:07 +00001261
1262 if (c & ATA_STAT_BUSY) {
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001263 printf("IDE read: device %d not ready\n", device);
wdenkc6097192002-11-03 00:24:07 +00001264 goto IDE_READ_E;
1265 }
1266 if ((c & ATA_STAT_ERR) == ATA_STAT_ERR) {
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001267 printf("No Powersaving mode %X\n", c);
wdenkc6097192002-11-03 00:24:07 +00001268 } else {
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001269 c = ide_inb(device, ATA_SECT_CNT);
1270 debug("Powersaving %02X\n", c);
1271 if (c == 0)
1272 pwrsave = 1;
wdenkc6097192002-11-03 00:24:07 +00001273 }
1274
1275
1276 while (blkcnt-- > 0) {
1277
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001278 c = ide_wait(device, IDE_TIME_OUT);
wdenkc6097192002-11-03 00:24:07 +00001279
1280 if (c & ATA_STAT_BUSY) {
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001281 printf("IDE read: device %d not ready\n", device);
wdenkc6097192002-11-03 00:24:07 +00001282 break;
1283 }
wdenkc35ba4e2004-03-14 22:25:36 +00001284#ifdef CONFIG_LBA48
wdenkf602aa02004-03-13 23:29:43 +00001285 if (lba48) {
1286 /* write high bits */
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001287 ide_outb(device, ATA_SECT_CNT, 0);
1288 ide_outb(device, ATA_LBA_LOW, (blknr >> 24) & 0xFF);
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001289#ifdef CONFIG_SYS_64BIT_LBA
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001290 ide_outb(device, ATA_LBA_MID, (blknr >> 32) & 0xFF);
1291 ide_outb(device, ATA_LBA_HIGH, (blknr >> 40) & 0xFF);
Guennadi Liakhovetskib71f1ca2008-04-28 14:36:06 +02001292#else
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001293 ide_outb(device, ATA_LBA_MID, 0);
1294 ide_outb(device, ATA_LBA_HIGH, 0);
Guennadi Liakhovetskib71f1ca2008-04-28 14:36:06 +02001295#endif
wdenkf602aa02004-03-13 23:29:43 +00001296 }
1297#endif
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001298 ide_outb(device, ATA_SECT_CNT, 1);
1299 ide_outb(device, ATA_LBA_LOW, (blknr >> 0) & 0xFF);
1300 ide_outb(device, ATA_LBA_MID, (blknr >> 8) & 0xFF);
1301 ide_outb(device, ATA_LBA_HIGH, (blknr >> 16) & 0xFF);
wdenkf602aa02004-03-13 23:29:43 +00001302
wdenkc35ba4e2004-03-14 22:25:36 +00001303#ifdef CONFIG_LBA48
wdenkf602aa02004-03-13 23:29:43 +00001304 if (lba48) {
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001305 ide_outb(device, ATA_DEV_HD,
1306 ATA_LBA | ATA_DEVICE(device));
1307 ide_outb(device, ATA_COMMAND, ATA_CMD_READ_EXT);
wdenkf602aa02004-03-13 23:29:43 +00001308
1309 } else
1310#endif
1311 {
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001312 ide_outb(device, ATA_DEV_HD, ATA_LBA |
1313 ATA_DEVICE(device) | ((blknr >> 24) & 0xF));
1314 ide_outb(device, ATA_COMMAND, ATA_CMD_READ);
wdenkf602aa02004-03-13 23:29:43 +00001315 }
wdenkc6097192002-11-03 00:24:07 +00001316
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001317 udelay(50);
wdenkc6097192002-11-03 00:24:07 +00001318
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001319 if (pwrsave) {
1320 /* may take up to 4 sec */
1321 c = ide_wait(device, IDE_SPIN_UP_TIME_OUT);
1322 pwrsave = 0;
wdenkc6097192002-11-03 00:24:07 +00001323 } else {
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001324 /* can't take over 500 ms */
1325 c = ide_wait(device, IDE_TIME_OUT);
wdenkc6097192002-11-03 00:24:07 +00001326 }
1327
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001328 if ((c & (ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR)) !=
1329 ATA_STAT_DRQ) {
Heiko Schocher0f602e12009-12-03 11:21:21 +01001330#if defined(CONFIG_SYS_64BIT_LBA)
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001331 printf("Error (no IRQ) dev %d blk %lld: status 0x%02x\n",
wdenkc6097192002-11-03 00:24:07 +00001332 device, blknr, c);
wdenkf602aa02004-03-13 23:29:43 +00001333#else
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001334 printf("Error (no IRQ) dev %d blk %ld: status 0x%02x\n",
1335 device, (ulong) blknr, c);
wdenkf602aa02004-03-13 23:29:43 +00001336#endif
wdenkc6097192002-11-03 00:24:07 +00001337 break;
1338 }
1339
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001340 input_data(device, buffer, ATA_SECTORWORDS);
1341 (void) ide_inb(device, ATA_STATUS); /* clear IRQ */
wdenkc6097192002-11-03 00:24:07 +00001342
1343 ++n;
1344 ++blknr;
Greg Lopp50abf9f2007-04-13 08:02:24 +02001345 buffer += ATA_BLOCKSIZE;
wdenkc6097192002-11-03 00:24:07 +00001346 }
1347IDE_READ_E:
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001348 ide_led(DEVICE_LED(device), 0); /* LED off */
wdenkc6097192002-11-03 00:24:07 +00001349 return (n);
1350}
1351
1352/* ------------------------------------------------------------------------- */
1353
1354
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001355ulong ide_write(int device, lbaint_t blknr, ulong blkcnt, const void *buffer)
wdenkc6097192002-11-03 00:24:07 +00001356{
1357 ulong n = 0;
1358 unsigned char c;
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001359
wdenkc35ba4e2004-03-14 22:25:36 +00001360#ifdef CONFIG_LBA48
wdenkf602aa02004-03-13 23:29:43 +00001361 unsigned char lba48 = 0;
1362
Guennadi Liakhovetskib71f1ca2008-04-28 14:36:06 +02001363 if (blknr & 0x0000fffff0000000ULL) {
wdenkf602aa02004-03-13 23:29:43 +00001364 /* more than 28 bits used, use 48bit mode */
1365 lba48 = 1;
1366 }
1367#endif
wdenkc6097192002-11-03 00:24:07 +00001368
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001369 ide_led(DEVICE_LED(device), 1); /* LED on */
wdenkc6097192002-11-03 00:24:07 +00001370
1371 /* Select device
1372 */
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001373 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
wdenkc6097192002-11-03 00:24:07 +00001374
1375 while (blkcnt-- > 0) {
1376
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001377 c = ide_wait(device, IDE_TIME_OUT);
wdenkc6097192002-11-03 00:24:07 +00001378
1379 if (c & ATA_STAT_BUSY) {
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001380 printf("IDE read: device %d not ready\n", device);
wdenkc6097192002-11-03 00:24:07 +00001381 goto WR_OUT;
1382 }
wdenkc35ba4e2004-03-14 22:25:36 +00001383#ifdef CONFIG_LBA48
wdenkf602aa02004-03-13 23:29:43 +00001384 if (lba48) {
1385 /* write high bits */
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001386 ide_outb(device, ATA_SECT_CNT, 0);
1387 ide_outb(device, ATA_LBA_LOW, (blknr >> 24) & 0xFF);
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001388#ifdef CONFIG_SYS_64BIT_LBA
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001389 ide_outb(device, ATA_LBA_MID, (blknr >> 32) & 0xFF);
1390 ide_outb(device, ATA_LBA_HIGH, (blknr >> 40) & 0xFF);
Guennadi Liakhovetskib71f1ca2008-04-28 14:36:06 +02001391#else
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001392 ide_outb(device, ATA_LBA_MID, 0);
1393 ide_outb(device, ATA_LBA_HIGH, 0);
Guennadi Liakhovetskib71f1ca2008-04-28 14:36:06 +02001394#endif
wdenkf602aa02004-03-13 23:29:43 +00001395 }
1396#endif
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001397 ide_outb(device, ATA_SECT_CNT, 1);
1398 ide_outb(device, ATA_LBA_LOW, (blknr >> 0) & 0xFF);
1399 ide_outb(device, ATA_LBA_MID, (blknr >> 8) & 0xFF);
1400 ide_outb(device, ATA_LBA_HIGH, (blknr >> 16) & 0xFF);
wdenkf602aa02004-03-13 23:29:43 +00001401
wdenkc35ba4e2004-03-14 22:25:36 +00001402#ifdef CONFIG_LBA48
wdenkf602aa02004-03-13 23:29:43 +00001403 if (lba48) {
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001404 ide_outb(device, ATA_DEV_HD,
1405 ATA_LBA | ATA_DEVICE(device));
1406 ide_outb(device, ATA_COMMAND, ATA_CMD_WRITE_EXT);
wdenkf602aa02004-03-13 23:29:43 +00001407
1408 } else
1409#endif
1410 {
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001411 ide_outb(device, ATA_DEV_HD, ATA_LBA |
1412 ATA_DEVICE(device) | ((blknr >> 24) & 0xF));
1413 ide_outb(device, ATA_COMMAND, ATA_CMD_WRITE);
wdenkf602aa02004-03-13 23:29:43 +00001414 }
wdenkc6097192002-11-03 00:24:07 +00001415
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001416 udelay(50);
wdenkc6097192002-11-03 00:24:07 +00001417
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001418 /* can't take over 500 ms */
1419 c = ide_wait(device, IDE_TIME_OUT);
wdenkc6097192002-11-03 00:24:07 +00001420
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001421 if ((c & (ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR)) !=
1422 ATA_STAT_DRQ) {
Heiko Schocher0f602e12009-12-03 11:21:21 +01001423#if defined(CONFIG_SYS_64BIT_LBA)
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001424 printf("Error (no IRQ) dev %d blk %lld: status 0x%02x\n",
wdenkc6097192002-11-03 00:24:07 +00001425 device, blknr, c);
wdenkf602aa02004-03-13 23:29:43 +00001426#else
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001427 printf("Error (no IRQ) dev %d blk %ld: status 0x%02x\n",
1428 device, (ulong) blknr, c);
wdenkf602aa02004-03-13 23:29:43 +00001429#endif
wdenkc6097192002-11-03 00:24:07 +00001430 goto WR_OUT;
1431 }
1432
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001433 output_data(device, buffer, ATA_SECTORWORDS);
1434 c = ide_inb(device, ATA_STATUS); /* clear IRQ */
wdenkc6097192002-11-03 00:24:07 +00001435 ++n;
1436 ++blknr;
Greg Lopp50abf9f2007-04-13 08:02:24 +02001437 buffer += ATA_BLOCKSIZE;
wdenkc6097192002-11-03 00:24:07 +00001438 }
1439WR_OUT:
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001440 ide_led(DEVICE_LED(device), 0); /* LED off */
wdenkc6097192002-11-03 00:24:07 +00001441 return (n);
1442}
1443
1444/* ------------------------------------------------------------------------- */
1445
1446/*
1447 * copy src to dest, skipping leading and trailing blanks and null
1448 * terminate the string
wdenk18bd81e2004-03-17 01:13:07 +00001449 * "len" is the size of available memory including the terminating '\0'
wdenkc6097192002-11-03 00:24:07 +00001450 */
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001451static void ident_cpy(unsigned char *dst, unsigned char *src,
1452 unsigned int len)
wdenkc6097192002-11-03 00:24:07 +00001453{
wdenk18bd81e2004-03-17 01:13:07 +00001454 unsigned char *end, *last;
wdenkc6097192002-11-03 00:24:07 +00001455
wdenk18bd81e2004-03-17 01:13:07 +00001456 last = dst;
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001457 end = src + len - 1;
wdenk18bd81e2004-03-17 01:13:07 +00001458
1459 /* reserve space for '\0' */
1460 if (len < 2)
1461 goto OUT;
wdenkc12081a2004-03-23 20:18:25 +00001462
wdenk18bd81e2004-03-17 01:13:07 +00001463 /* skip leading white space */
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001464 while ((*src) && (src < end) && (*src == ' '))
wdenk18bd81e2004-03-17 01:13:07 +00001465 ++src;
1466
1467 /* copy string, omitting trailing white space */
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001468 while ((*src) && (src < end)) {
wdenk18bd81e2004-03-17 01:13:07 +00001469 *dst++ = *src;
1470 if (*src++ != ' ')
1471 last = dst;
wdenkc6097192002-11-03 00:24:07 +00001472 }
wdenk18bd81e2004-03-17 01:13:07 +00001473OUT:
1474 *last = '\0';
wdenkc6097192002-11-03 00:24:07 +00001475}
1476
1477/* ------------------------------------------------------------------------- */
1478
1479/*
1480 * Wait until Busy bit is off, or timeout (in ms)
1481 * Return last status
1482 */
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001483static uchar ide_wait(int dev, ulong t)
wdenkc6097192002-11-03 00:24:07 +00001484{
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001485 ulong delay = 10 * t; /* poll every 100 us */
wdenkc6097192002-11-03 00:24:07 +00001486 uchar c;
1487
wdenk591dda52002-11-18 00:14:45 +00001488 while ((c = ide_inb(dev, ATA_STATUS)) & ATA_STAT_BUSY) {
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001489 udelay(100);
1490 if (delay-- == 0)
wdenkc6097192002-11-03 00:24:07 +00001491 break;
wdenkc6097192002-11-03 00:24:07 +00001492 }
1493 return (c);
1494}
1495
1496/* ------------------------------------------------------------------------- */
1497
1498#ifdef CONFIG_IDE_RESET
1499extern void ide_set_reset(int idereset);
1500
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001501static void ide_reset(void)
wdenkc6097192002-11-03 00:24:07 +00001502{
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001503#if defined(CONFIG_SYS_PB_12V_ENABLE) || defined(CONFIG_SYS_PB_IDE_MOTOR)
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001504 volatile immap_t *immr = (immap_t *) CONFIG_SYS_IMMR;
wdenkc6097192002-11-03 00:24:07 +00001505#endif
1506 int i;
1507
1508 curr_device = -1;
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001509 for (i = 0; i < CONFIG_SYS_IDE_MAXBUS; ++i)
wdenkc6097192002-11-03 00:24:07 +00001510 ide_bus_ok[i] = 0;
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001511 for (i = 0; i < CONFIG_SYS_IDE_MAXDEVICE; ++i)
wdenkc6097192002-11-03 00:24:07 +00001512 ide_dev_desc[i].type = DEV_TYPE_UNKNOWN;
1513
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001514 ide_set_reset(1); /* assert reset */
wdenkc6097192002-11-03 00:24:07 +00001515
Martin Krause7e35caf2008-04-03 13:37:56 +02001516 /* the reset signal shall be asserted for et least 25 us */
1517 udelay(25);
1518
wdenkc6097192002-11-03 00:24:07 +00001519 WATCHDOG_RESET();
1520
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001521#ifdef CONFIG_SYS_PB_12V_ENABLE
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001522 /* 12V Enable output OFF */
1523 immr->im_cpm.cp_pbdat &= ~(CONFIG_SYS_PB_12V_ENABLE);
1524
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001525 immr->im_cpm.cp_pbpar &= ~(CONFIG_SYS_PB_12V_ENABLE);
1526 immr->im_cpm.cp_pbodr &= ~(CONFIG_SYS_PB_12V_ENABLE);
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001527 immr->im_cpm.cp_pbdir |= CONFIG_SYS_PB_12V_ENABLE;
wdenkc6097192002-11-03 00:24:07 +00001528
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001529 /* wait 500 ms for the voltage to stabilize */
1530 for (i = 0; i < 500; ++i)
1531 udelay(1000);
wdenkc6097192002-11-03 00:24:07 +00001532
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001533 /* 12V Enable output ON */
1534 immr->im_cpm.cp_pbdat |= CONFIG_SYS_PB_12V_ENABLE;
1535#endif /* CONFIG_SYS_PB_12V_ENABLE */
wdenkc6097192002-11-03 00:24:07 +00001536
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001537#ifdef CONFIG_SYS_PB_IDE_MOTOR
wdenkc6097192002-11-03 00:24:07 +00001538 /* configure IDE Motor voltage monitor pin as input */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001539 immr->im_cpm.cp_pbpar &= ~(CONFIG_SYS_PB_IDE_MOTOR);
1540 immr->im_cpm.cp_pbodr &= ~(CONFIG_SYS_PB_IDE_MOTOR);
1541 immr->im_cpm.cp_pbdir &= ~(CONFIG_SYS_PB_IDE_MOTOR);
wdenkc6097192002-11-03 00:24:07 +00001542
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001543 /* wait up to 1 s for the motor voltage to stabilize */
1544 for (i = 0; i < 1000; ++i) {
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001545 if ((immr->im_cpm.cp_pbdat & CONFIG_SYS_PB_IDE_MOTOR) != 0) {
wdenkc6097192002-11-03 00:24:07 +00001546 break;
1547 }
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001548 udelay(1000);
wdenkc6097192002-11-03 00:24:07 +00001549 }
1550
1551 if (i == 1000) { /* Timeout */
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001552 printf("\nWarning: 5V for IDE Motor missing\n");
1553#ifdef CONFIG_STATUS_LED
1554#ifdef STATUS_LED_YELLOW
1555 status_led_set(STATUS_LED_YELLOW, STATUS_LED_ON);
1556#endif
1557#ifdef STATUS_LED_GREEN
1558 status_led_set(STATUS_LED_GREEN, STATUS_LED_OFF);
1559#endif
1560#endif /* CONFIG_STATUS_LED */
wdenkc6097192002-11-03 00:24:07 +00001561 }
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001562#endif /* CONFIG_SYS_PB_IDE_MOTOR */
wdenkc6097192002-11-03 00:24:07 +00001563
1564 WATCHDOG_RESET();
1565
1566 /* de-assert RESET signal */
1567 ide_set_reset(0);
1568
1569 /* wait 250 ms */
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001570 for (i = 0; i < 250; ++i)
1571 udelay(1000);
wdenkc6097192002-11-03 00:24:07 +00001572}
1573
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001574#endif /* CONFIG_IDE_RESET */
wdenkc6097192002-11-03 00:24:07 +00001575
1576/* ------------------------------------------------------------------------- */
1577
wdenk54070ab2004-12-31 09:32:47 +00001578#if defined(CONFIG_IDE_LED) && \
wdenk54070ab2004-12-31 09:32:47 +00001579 !defined(CONFIG_CPC45) && \
wdenk54070ab2004-12-31 09:32:47 +00001580 !defined(CONFIG_KUP4K) && \
1581 !defined(CONFIG_KUP4X)
wdenkc6097192002-11-03 00:24:07 +00001582
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001583static uchar led_buffer; /* Buffer for current LED status */
wdenkc6097192002-11-03 00:24:07 +00001584
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001585static void ide_led(uchar led, uchar status)
wdenkc6097192002-11-03 00:24:07 +00001586{
1587 uchar *led_port = LED_PORT;
1588
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001589 if (status) /* switch LED on */
1590 led_buffer |= led;
1591 else /* switch LED off */
wdenkc6097192002-11-03 00:24:07 +00001592 led_buffer &= ~led;
wdenkc6097192002-11-03 00:24:07 +00001593
1594 *led_port = led_buffer;
1595}
1596
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001597#endif /* CONFIG_IDE_LED */
wdenkc6097192002-11-03 00:24:07 +00001598
Heiko Schocherffb293a2009-09-23 07:56:08 +02001599#if defined(CONFIG_OF_IDE_FIXUP)
1600int ide_device_present(int dev)
1601{
1602 if (dev >= CONFIG_SYS_IDE_MAXBUS)
1603 return 0;
1604 return (ide_dev_desc[dev].type == DEV_TYPE_UNKNOWN ? 0 : 1);
1605}
1606#endif
wdenkc6097192002-11-03 00:24:07 +00001607/* ------------------------------------------------------------------------- */
1608
1609#ifdef CONFIG_ATAPI
1610/****************************************************************************
1611 * ATAPI Support
1612 */
1613
Albert Aribaud036c6b42010-08-08 05:17:05 +05301614#if defined(CONFIG_IDE_SWAP_IO)
wdenkc6097192002-11-03 00:24:07 +00001615/* since ATAPI may use commands with not 4 bytes alligned length
1616 * we have our own transfer functions, 2 bytes alligned */
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001617static void output_data_shorts(int dev, ushort *sect_buf, int shorts)
wdenkc6097192002-11-03 00:24:07 +00001618{
Wolfgang Denkfb52e952010-09-19 21:28:25 +02001619#if defined(CONFIG_CPC45)
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001620 uchar *dbuf;
1621 volatile uchar *pbuf_even;
1622 volatile uchar *pbuf_odd;
wdenk2f99a692004-01-04 22:51:12 +00001623
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001624 pbuf_even = (uchar *) (ATA_CURR_BASE(dev) + ATA_DATA_EVEN);
1625 pbuf_odd = (uchar *) (ATA_CURR_BASE(dev) + ATA_DATA_ODD);
wdenk2f99a692004-01-04 22:51:12 +00001626 while (shorts--) {
wdenk20c98a62004-04-23 20:32:05 +00001627 EIEIO;
wdenk2f99a692004-01-04 22:51:12 +00001628 *pbuf_even = *dbuf++;
wdenk20c98a62004-04-23 20:32:05 +00001629 EIEIO;
wdenk2f99a692004-01-04 22:51:12 +00001630 *pbuf_odd = *dbuf++;
1631 }
wdenkf1276d42005-02-03 23:00:49 +00001632#else
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001633 ushort *dbuf;
1634 volatile ushort *pbuf;
wdenkc6097192002-11-03 00:24:07 +00001635
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001636 pbuf = (ushort *) (ATA_CURR_BASE(dev) + ATA_DATA_REG);
1637 dbuf = (ushort *) sect_buf;
wdenk634d2f72004-04-15 23:14:49 +00001638
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001639 debug("in output data shorts base for read is %lx\n",
1640 (unsigned long) pbuf);
wdenk634d2f72004-04-15 23:14:49 +00001641
wdenkc6097192002-11-03 00:24:07 +00001642 while (shorts--) {
wdenk20c98a62004-04-23 20:32:05 +00001643 EIEIO;
wdenkf1276d42005-02-03 23:00:49 +00001644 *pbuf = *dbuf++;
wdenkc6097192002-11-03 00:24:07 +00001645 }
wdenkf1276d42005-02-03 23:00:49 +00001646#endif
1647}
1648
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001649static void input_data_shorts(int dev, ushort *sect_buf, int shorts)
wdenkf1276d42005-02-03 23:00:49 +00001650{
Wolfgang Denkfb52e952010-09-19 21:28:25 +02001651#if defined(CONFIG_CPC45)
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001652 uchar *dbuf;
1653 volatile uchar *pbuf_even;
1654 volatile uchar *pbuf_odd;
wdenk2f99a692004-01-04 22:51:12 +00001655
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001656 pbuf_even = (uchar *) (ATA_CURR_BASE(dev) + ATA_DATA_EVEN);
1657 pbuf_odd = (uchar *) (ATA_CURR_BASE(dev) + ATA_DATA_ODD);
wdenk2f99a692004-01-04 22:51:12 +00001658 while (shorts--) {
wdenk20c98a62004-04-23 20:32:05 +00001659 EIEIO;
wdenk2f99a692004-01-04 22:51:12 +00001660 *dbuf++ = *pbuf_even;
wdenk20c98a62004-04-23 20:32:05 +00001661 EIEIO;
wdenk2f99a692004-01-04 22:51:12 +00001662 *dbuf++ = *pbuf_odd;
1663 }
wdenkf1276d42005-02-03 23:00:49 +00001664#else
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001665 ushort *dbuf;
1666 volatile ushort *pbuf;
wdenkf1276d42005-02-03 23:00:49 +00001667
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001668 pbuf = (ushort *) (ATA_CURR_BASE(dev) + ATA_DATA_REG);
1669 dbuf = (ushort *) sect_buf;
wdenkf1276d42005-02-03 23:00:49 +00001670
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001671 debug("in input data shorts base for read is %lx\n",
1672 (unsigned long) pbuf);
wdenkf1276d42005-02-03 23:00:49 +00001673
1674 while (shorts--) {
1675 EIEIO;
1676 *dbuf++ = *pbuf;
1677 }
1678#endif
wdenkc6097192002-11-03 00:24:07 +00001679}
1680
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001681#else /* ! CONFIG_IDE_SWAP_IO */
1682static void output_data_shorts(int dev, ushort *sect_buf, int shorts)
wdenk591dda52002-11-18 00:14:45 +00001683{
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001684 outsw(ATA_CURR_BASE(dev) + ATA_DATA_REG, sect_buf, shorts);
wdenk591dda52002-11-18 00:14:45 +00001685}
1686
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001687static void input_data_shorts(int dev, ushort *sect_buf, int shorts)
wdenk591dda52002-11-18 00:14:45 +00001688{
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001689 insw(ATA_CURR_BASE(dev) + ATA_DATA_REG, sect_buf, shorts);
wdenk591dda52002-11-18 00:14:45 +00001690}
1691
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001692#endif /* CONFIG_IDE_SWAP_IO */
wdenk591dda52002-11-18 00:14:45 +00001693
wdenkc6097192002-11-03 00:24:07 +00001694/*
1695 * Wait until (Status & mask) == res, or timeout (in ms)
1696 * Return last status
1697 * This is used since some ATAPI CD ROMs clears their Busy Bit first
1698 * and then they set their DRQ Bit
1699 */
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001700static uchar atapi_wait_mask(int dev, ulong t, uchar mask, uchar res)
wdenkc6097192002-11-03 00:24:07 +00001701{
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001702 ulong delay = 10 * t; /* poll every 100 us */
wdenkc6097192002-11-03 00:24:07 +00001703 uchar c;
1704
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001705 /* prevents to read the status before valid */
1706 c = ide_inb(dev, ATA_DEV_CTL);
1707
wdenk591dda52002-11-18 00:14:45 +00001708 while (((c = ide_inb(dev, ATA_STATUS)) & mask) != res) {
wdenkc6097192002-11-03 00:24:07 +00001709 /* break if error occurs (doesn't make sense to wait more) */
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001710 if ((c & ATA_STAT_ERR) == ATA_STAT_ERR)
wdenkc6097192002-11-03 00:24:07 +00001711 break;
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001712 udelay(100);
1713 if (delay-- == 0)
wdenkc6097192002-11-03 00:24:07 +00001714 break;
wdenkc6097192002-11-03 00:24:07 +00001715 }
1716 return (c);
1717}
1718
1719/*
1720 * issue an atapi command
1721 */
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001722unsigned char atapi_issue(int device, unsigned char *ccb, int ccblen,
1723 unsigned char *buffer, int buflen)
wdenkc6097192002-11-03 00:24:07 +00001724{
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001725 unsigned char c, err, mask, res;
wdenkc6097192002-11-03 00:24:07 +00001726 int n;
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001727
1728 ide_led(DEVICE_LED(device), 1); /* LED on */
wdenkc6097192002-11-03 00:24:07 +00001729
1730 /* Select device
1731 */
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001732 mask = ATA_STAT_BUSY | ATA_STAT_DRQ;
wdenkc6097192002-11-03 00:24:07 +00001733 res = 0;
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001734 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
1735 c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
wdenkc6097192002-11-03 00:24:07 +00001736 if ((c & mask) != res) {
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001737 printf("ATAPI_ISSUE: device %d not ready status %X\n", device,
1738 c);
1739 err = 0xFF;
wdenkc6097192002-11-03 00:24:07 +00001740 goto AI_OUT;
1741 }
1742 /* write taskfile */
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001743 ide_outb(device, ATA_ERROR_REG, 0); /* no DMA, no overlaped */
1744 ide_outb(device, ATA_SECT_CNT, 0);
1745 ide_outb(device, ATA_SECT_NUM, 0);
1746 ide_outb(device, ATA_CYL_LOW, (unsigned char) (buflen & 0xFF));
1747 ide_outb(device, ATA_CYL_HIGH,
1748 (unsigned char) ((buflen >> 8) & 0xFF));
1749 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
wdenkc6097192002-11-03 00:24:07 +00001750
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001751 ide_outb(device, ATA_COMMAND, ATAPI_CMD_PACKET);
1752 udelay(50);
wdenkc6097192002-11-03 00:24:07 +00001753
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001754 mask = ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR;
wdenkc6097192002-11-03 00:24:07 +00001755 res = ATA_STAT_DRQ;
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001756 c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
wdenkc6097192002-11-03 00:24:07 +00001757
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001758 if ((c & mask) != res) { /* DRQ must be 1, BSY 0 */
1759 printf("ATAPI_ISSUE: Error (no IRQ) before sending ccb dev %d status 0x%02x\n",
1760 device, c);
1761 err = 0xFF;
wdenkc6097192002-11-03 00:24:07 +00001762 goto AI_OUT;
1763 }
1764
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001765 /* write command block */
1766 output_data_shorts(device, (unsigned short *) ccb, ccblen / 2);
1767
Wolfgang Denka1be4762008-05-20 16:00:29 +02001768 /* ATAPI Command written wait for completition */
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001769 udelay(5000); /* device must set bsy */
wdenkc6097192002-11-03 00:24:07 +00001770
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001771 mask = ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR;
1772 /*
1773 * if no data wait for DRQ = 0 BSY = 0
1774 * if data wait for DRQ = 1 BSY = 0
1775 */
1776 res = 0;
1777 if (buflen)
wdenkc6097192002-11-03 00:24:07 +00001778 res = ATA_STAT_DRQ;
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001779 c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
1780 if ((c & mask) != res) {
wdenkc6097192002-11-03 00:24:07 +00001781 if (c & ATA_STAT_ERR) {
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001782 err = (ide_inb(device, ATA_ERROR_REG)) >> 4;
1783 debug("atapi_issue 1 returned sense key %X status %02X\n",
1784 err, c);
wdenkc6097192002-11-03 00:24:07 +00001785 } else {
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001786 printf("ATAPI_ISSUE: (no DRQ) after sending ccb (%x) status 0x%02x\n",
1787 ccb[0], c);
1788 err = 0xFF;
wdenkc6097192002-11-03 00:24:07 +00001789 }
1790 goto AI_OUT;
1791 }
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001792 n = ide_inb(device, ATA_CYL_HIGH);
1793 n <<= 8;
1794 n += ide_inb(device, ATA_CYL_LOW);
1795 if (n > buflen) {
1796 printf("ERROR, transfer bytes %d requested only %d\n", n,
1797 buflen);
1798 err = 0xff;
wdenkc6097192002-11-03 00:24:07 +00001799 goto AI_OUT;
1800 }
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001801 if ((n == 0) && (buflen < 0)) {
1802 printf("ERROR, transfer bytes %d requested %d\n", n, buflen);
1803 err = 0xff;
wdenkc6097192002-11-03 00:24:07 +00001804 goto AI_OUT;
1805 }
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001806 if (n != buflen) {
1807 debug("WARNING, transfer bytes %d not equal with requested %d\n",
1808 n, buflen);
wdenkc6097192002-11-03 00:24:07 +00001809 }
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001810 if (n != 0) { /* data transfer */
1811 debug("ATAPI_ISSUE: %d Bytes to transfer\n", n);
1812 /* we transfer shorts */
1813 n >>= 1;
wdenkc6097192002-11-03 00:24:07 +00001814 /* ok now decide if it is an in or output */
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001815 if ((ide_inb(device, ATA_SECT_CNT) & 0x02) == 0) {
1816 debug("Write to device\n");
1817 output_data_shorts(device, (unsigned short *) buffer,
1818 n);
wdenkc6097192002-11-03 00:24:07 +00001819 } else {
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001820 debug("Read from device @ %p shorts %d\n", buffer, n);
1821 input_data_shorts(device, (unsigned short *) buffer,
1822 n);
wdenkc6097192002-11-03 00:24:07 +00001823 }
1824 }
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001825 udelay(5000); /* seems that some CD ROMs need this... */
1826 mask = ATA_STAT_BUSY | ATA_STAT_ERR;
1827 res = 0;
1828 c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
wdenkc6097192002-11-03 00:24:07 +00001829 if ((c & ATA_STAT_ERR) == ATA_STAT_ERR) {
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001830 err = (ide_inb(device, ATA_ERROR_REG) >> 4);
1831 debug("atapi_issue 2 returned sense key %X status %X\n", err,
1832 c);
wdenkc6097192002-11-03 00:24:07 +00001833 } else {
1834 err = 0;
1835 }
1836AI_OUT:
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001837 ide_led(DEVICE_LED(device), 0); /* LED off */
wdenkc6097192002-11-03 00:24:07 +00001838 return (err);
1839}
1840
1841/*
1842 * sending the command to atapi_issue. If an status other than good
1843 * returns, an request_sense will be issued
1844 */
1845
Wolfgang Denka1be4762008-05-20 16:00:29 +02001846#define ATAPI_DRIVE_NOT_READY 100
wdenkc6097192002-11-03 00:24:07 +00001847#define ATAPI_UNIT_ATTN 10
1848
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001849unsigned char atapi_issue_autoreq(int device,
1850 unsigned char *ccb,
1851 int ccblen,
1852 unsigned char *buffer, int buflen)
wdenkc6097192002-11-03 00:24:07 +00001853{
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001854 unsigned char sense_data[18], sense_ccb[12];
1855 unsigned char res, key, asc, ascq;
1856 int notready, unitattn;
wdenkc6097192002-11-03 00:24:07 +00001857
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001858 unitattn = ATAPI_UNIT_ATTN;
1859 notready = ATAPI_DRIVE_NOT_READY;
wdenkc6097192002-11-03 00:24:07 +00001860
1861retry:
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001862 res = atapi_issue(device, ccb, ccblen, buffer, buflen);
1863 if (res == 0)
1864 return 0; /* Ok */
wdenkc6097192002-11-03 00:24:07 +00001865
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001866 if (res == 0xFF)
1867 return 0xFF; /* error */
wdenkc6097192002-11-03 00:24:07 +00001868
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001869 debug("(auto_req)atapi_issue returned sense key %X\n", res);
wdenkc6097192002-11-03 00:24:07 +00001870
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001871 memset(sense_ccb, 0, sizeof(sense_ccb));
1872 memset(sense_data, 0, sizeof(sense_data));
1873 sense_ccb[0] = ATAPI_CMD_REQ_SENSE;
1874 sense_ccb[4] = 18; /* allocation Length */
wdenkc6097192002-11-03 00:24:07 +00001875
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001876 res = atapi_issue(device, sense_ccb, 12, sense_data, 18);
1877 key = (sense_data[2] & 0xF);
1878 asc = (sense_data[12]);
1879 ascq = (sense_data[13]);
wdenkc6097192002-11-03 00:24:07 +00001880
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001881 debug("ATAPI_CMD_REQ_SENSE returned %x\n", res);
1882 debug(" Sense page: %02X key %02X ASC %02X ASCQ %02X\n",
1883 sense_data[0], key, asc, ascq);
wdenkc6097192002-11-03 00:24:07 +00001884
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001885 if ((key == 0))
1886 return 0; /* ok device ready */
wdenkc6097192002-11-03 00:24:07 +00001887
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001888 if ((key == 6) || (asc == 0x29) || (asc == 0x28)) { /* Unit Attention */
1889 if (unitattn-- > 0) {
1890 udelay(200 * 1000);
wdenkc6097192002-11-03 00:24:07 +00001891 goto retry;
1892 }
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001893 printf("Unit Attention, tried %d\n", ATAPI_UNIT_ATTN);
wdenkc6097192002-11-03 00:24:07 +00001894 goto error;
1895 }
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001896 if ((asc == 0x4) && (ascq == 0x1)) {
1897 /* not ready, but will be ready soon */
1898 if (notready-- > 0) {
1899 udelay(200 * 1000);
wdenkc6097192002-11-03 00:24:07 +00001900 goto retry;
1901 }
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001902 printf("Drive not ready, tried %d times\n",
1903 ATAPI_DRIVE_NOT_READY);
wdenkc6097192002-11-03 00:24:07 +00001904 goto error;
1905 }
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001906 if (asc == 0x3a) {
1907 debug("Media not present\n");
wdenkc6097192002-11-03 00:24:07 +00001908 goto error;
1909 }
wdenk452cfd62002-11-19 11:04:11 +00001910
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001911 printf("ERROR: Unknown Sense key %02X ASC %02X ASCQ %02X\n", key, asc,
1912 ascq);
wdenkc6097192002-11-03 00:24:07 +00001913error:
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001914 debug("ERROR Sense key %02X ASC %02X ASCQ %02X\n", key, asc, ascq);
wdenkc6097192002-11-03 00:24:07 +00001915 return (0xFF);
1916}
1917
1918
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001919static void atapi_inquiry(block_dev_desc_t *dev_desc)
wdenkc6097192002-11-03 00:24:07 +00001920{
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001921 unsigned char ccb[12]; /* Command descriptor block */
1922 unsigned char iobuf[64]; /* temp buf */
wdenkc6097192002-11-03 00:24:07 +00001923 unsigned char c;
1924 int device;
1925
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001926 device = dev_desc->dev;
1927 dev_desc->type = DEV_TYPE_UNKNOWN; /* not yet valid */
1928 dev_desc->block_read = atapi_read;
wdenkc6097192002-11-03 00:24:07 +00001929
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001930 memset(ccb, 0, sizeof(ccb));
1931 memset(iobuf, 0, sizeof(iobuf));
wdenkc6097192002-11-03 00:24:07 +00001932
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001933 ccb[0] = ATAPI_CMD_INQUIRY;
1934 ccb[4] = 40; /* allocation Legnth */
1935 c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *) iobuf, 40);
wdenkc6097192002-11-03 00:24:07 +00001936
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001937 debug("ATAPI_CMD_INQUIRY returned %x\n", c);
1938 if (c != 0)
wdenkc6097192002-11-03 00:24:07 +00001939 return;
1940
1941 /* copy device ident strings */
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001942 ident_cpy((unsigned char *) dev_desc->vendor, &iobuf[8], 8);
1943 ident_cpy((unsigned char *) dev_desc->product, &iobuf[16], 16);
1944 ident_cpy((unsigned char *) dev_desc->revision, &iobuf[32], 5);
wdenkc6097192002-11-03 00:24:07 +00001945
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001946 dev_desc->lun = 0;
1947 dev_desc->lba = 0;
1948 dev_desc->blksz = 0;
1949 dev_desc->type = iobuf[0] & 0x1f;
wdenkc6097192002-11-03 00:24:07 +00001950
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001951 if ((iobuf[1] & 0x80) == 0x80)
wdenkc6097192002-11-03 00:24:07 +00001952 dev_desc->removable = 1;
1953 else
1954 dev_desc->removable = 0;
1955
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001956 memset(ccb, 0, sizeof(ccb));
1957 memset(iobuf, 0, sizeof(iobuf));
1958 ccb[0] = ATAPI_CMD_START_STOP;
1959 ccb[4] = 0x03; /* start */
wdenkc6097192002-11-03 00:24:07 +00001960
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001961 c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *) iobuf, 0);
wdenkc6097192002-11-03 00:24:07 +00001962
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001963 debug("ATAPI_CMD_START_STOP returned %x\n", c);
1964 if (c != 0)
wdenkc6097192002-11-03 00:24:07 +00001965 return;
1966
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001967 memset(ccb, 0, sizeof(ccb));
1968 memset(iobuf, 0, sizeof(iobuf));
1969 c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *) iobuf, 0);
wdenkc6097192002-11-03 00:24:07 +00001970
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001971 debug("ATAPI_CMD_UNIT_TEST_READY returned %x\n", c);
1972 if (c != 0)
wdenkc6097192002-11-03 00:24:07 +00001973 return;
1974
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001975 memset(ccb, 0, sizeof(ccb));
1976 memset(iobuf, 0, sizeof(iobuf));
1977 ccb[0] = ATAPI_CMD_READ_CAP;
1978 c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *) iobuf, 8);
1979 debug("ATAPI_CMD_READ_CAP returned %x\n", c);
1980 if (c != 0)
wdenkc6097192002-11-03 00:24:07 +00001981 return;
1982
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001983 debug("Read Cap: LBA %02X%02X%02X%02X blksize %02X%02X%02X%02X\n",
1984 iobuf[0], iobuf[1], iobuf[2], iobuf[3],
1985 iobuf[4], iobuf[5], iobuf[6], iobuf[7]);
wdenkc6097192002-11-03 00:24:07 +00001986
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001987 dev_desc->lba = ((unsigned long) iobuf[0] << 24) +
1988 ((unsigned long) iobuf[1] << 16) +
1989 ((unsigned long) iobuf[2] << 8) + ((unsigned long) iobuf[3]);
1990 dev_desc->blksz = ((unsigned long) iobuf[4] << 24) +
1991 ((unsigned long) iobuf[5] << 16) +
1992 ((unsigned long) iobuf[6] << 8) + ((unsigned long) iobuf[7]);
wdenkc35ba4e2004-03-14 22:25:36 +00001993#ifdef CONFIG_LBA48
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001994 /* ATAPI devices cannot use 48bit addressing (ATA/ATAPI v7) */
1995 dev_desc->lba48 = 0;
wdenkc35ba4e2004-03-14 22:25:36 +00001996#endif
wdenkc6097192002-11-03 00:24:07 +00001997 return;
1998}
1999
2000
2001/*
2002 * atapi_read:
2003 * we transfer only one block per command, since the multiple DRQ per
2004 * command is not yet implemented
2005 */
2006#define ATAPI_READ_MAX_BYTES 2048 /* we read max 2kbytes */
2007#define ATAPI_READ_BLOCK_SIZE 2048 /* assuming CD part */
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00002008#define ATAPI_READ_MAX_BLOCK (ATAPI_READ_MAX_BYTES/ATAPI_READ_BLOCK_SIZE)
wdenkc6097192002-11-03 00:24:07 +00002009
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00002010ulong atapi_read(int device, lbaint_t blknr, ulong blkcnt, void *buffer)
wdenkc6097192002-11-03 00:24:07 +00002011{
2012 ulong n = 0;
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00002013 unsigned char ccb[12]; /* Command descriptor block */
wdenkc6097192002-11-03 00:24:07 +00002014 ulong cnt;
2015
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00002016 debug("atapi_read dev %d start %lX, blocks %lX buffer at %lX\n",
2017 device, blknr, blkcnt, (ulong) buffer);
wdenkc6097192002-11-03 00:24:07 +00002018
2019 do {
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00002020 if (blkcnt > ATAPI_READ_MAX_BLOCK)
2021 cnt = ATAPI_READ_MAX_BLOCK;
2022 else
2023 cnt = blkcnt;
wdenkc6097192002-11-03 00:24:07 +00002024
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00002025 ccb[0] = ATAPI_CMD_READ_12;
2026 ccb[1] = 0; /* reserved */
2027 ccb[2] = (unsigned char) (blknr >> 24) & 0xFF; /* MSB Block */
2028 ccb[3] = (unsigned char) (blknr >> 16) & 0xFF; /* */
2029 ccb[4] = (unsigned char) (blknr >> 8) & 0xFF;
2030 ccb[5] = (unsigned char) blknr & 0xFF; /* LSB Block */
2031 ccb[6] = (unsigned char) (cnt >> 24) & 0xFF; /* MSB Block cnt */
2032 ccb[7] = (unsigned char) (cnt >> 16) & 0xFF;
2033 ccb[8] = (unsigned char) (cnt >> 8) & 0xFF;
2034 ccb[9] = (unsigned char) cnt & 0xFF; /* LSB Block */
2035 ccb[10] = 0; /* reserved */
2036 ccb[11] = 0; /* reserved */
2037
2038 if (atapi_issue_autoreq(device, ccb, 12,
2039 (unsigned char *) buffer,
2040 cnt * ATAPI_READ_BLOCK_SIZE)
2041 == 0xFF) {
wdenkc6097192002-11-03 00:24:07 +00002042 return (n);
2043 }
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00002044 n += cnt;
2045 blkcnt -= cnt;
2046 blknr += cnt;
2047 buffer += (cnt * ATAPI_READ_BLOCK_SIZE);
wdenkc6097192002-11-03 00:24:07 +00002048 } while (blkcnt > 0);
2049 return (n);
2050}
2051
2052/* ------------------------------------------------------------------------- */
2053
2054#endif /* CONFIG_ATAPI */
2055
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00002056U_BOOT_CMD(ide, 5, 1, do_ide,
2057 "IDE sub-system",
2058 "reset - reset IDE controller\n"
2059 "ide info - show available IDE devices\n"
2060 "ide device [dev] - show or set current device\n"
2061 "ide part [dev] - print partition table of one or all IDE devices\n"
2062 "ide read addr blk# cnt\n"
2063 "ide write addr blk# cnt - read/write `cnt'"
2064 " blocks starting at block `blk#'\n"
2065 " to/from memory address `addr'");
wdenk57b2d802003-06-27 21:31:46 +00002066
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00002067U_BOOT_CMD(diskboot, 3, 1, do_diskboot,
2068 "boot from IDE device", "loadAddr dev:part");