blob: f5b6c7b1ec6fb760dc35c55a4f966171941d063a [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:
Simon Glassa06dfc72011-12-10 08:44:01 +0000182 return CMD_RET_USAGE;
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000183 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 }
Simon Glassa06dfc72011-12-10 08:44:01 +0000235 return CMD_RET_USAGE;
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000236 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
Simon Glassa06dfc72011-12-10 08:44:01 +0000269 return CMD_RET_USAGE;
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000270 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 {
Simon Glassa06dfc72011-12-10 08:44:01 +0000328 return CMD_RET_USAGE;
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000329 }
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
Simon Glass0169e6b2012-02-13 13:51:18 +0000348 bootstage_mark(BOOTSTAGE_ID_IDE_START);
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:
Simon Glass0169e6b2012-02-13 13:51:18 +0000363 bootstage_error(BOOTSTAGE_ID_IDE_ADDR);
Simon Glassa06dfc72011-12-10 08:44:01 +0000364 return CMD_RET_USAGE;
wdenkc6097192002-11-03 00:24:07 +0000365 }
Simon Glass0169e6b2012-02-13 13:51:18 +0000366 bootstage_mark(BOOTSTAGE_ID_IDE_ADDR);
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");
Simon Glass0169e6b2012-02-13 13:51:18 +0000370 bootstage_error(BOOTSTAGE_ID_IDE_BOOT_DEVICE);
wdenkc6097192002-11-03 00:24:07 +0000371 return 1;
372 }
Simon Glass0169e6b2012-02-13 13:51:18 +0000373 bootstage_mark(BOOTSTAGE_ID_IDE_BOOT_DEVICE);
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);
Simon Glass0169e6b2012-02-13 13:51:18 +0000379 bootstage_error(BOOTSTAGE_ID_IDE_TYPE);
wdenkc6097192002-11-03 00:24:07 +0000380 return 1;
381 }
Simon Glass0169e6b2012-02-13 13:51:18 +0000382 bootstage_mark(BOOTSTAGE_ID_IDE_TYPE);
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");
Simon Glass0169e6b2012-02-13 13:51:18 +0000387 bootstage_error(BOOTSTAGE_ID_IDE_PART);
wdenkc6097192002-11-03 00:24:07 +0000388 return 1;
389 }
390 part = simple_strtoul(++ep, NULL, 16);
391 }
Simon Glass0169e6b2012-02-13 13:51:18 +0000392 bootstage_mark(BOOTSTAGE_ID_IDE_PART);
Simon Glass1b659cf2011-12-10 11:07:59 +0000393
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000394 if (get_partition_info(&ide_dev_desc[dev], part, &info)) {
Simon Glass0169e6b2012-02-13 13:51:18 +0000395 bootstage_error(BOOTSTAGE_ID_IDE_PART_INFO);
wdenkc6097192002-11-03 00:24:07 +0000396 return 1;
397 }
Simon Glass0169e6b2012-02-13 13:51:18 +0000398 bootstage_mark(BOOTSTAGE_ID_IDE_PART_INFO);
Simon Glass1b659cf2011-12-10 11:07:59 +0000399
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000400 if ((strncmp((char *)info.type, BOOT_PART_TYPE, sizeof(info.type)) != 0)
401 &&
402 (strncmp((char *)info.type, BOOT_PART_COMP, sizeof(info.type)) != 0)
403 ) {
404 printf("\n** Invalid partition type \"%.32s\"" " (expect \""
405 BOOT_PART_TYPE "\")\n",
wdenkc6097192002-11-03 00:24:07 +0000406 info.type);
Simon Glass0169e6b2012-02-13 13:51:18 +0000407 bootstage_error(BOOTSTAGE_ID_IDE_PART_TYPE);
wdenkc6097192002-11-03 00:24:07 +0000408 return 1;
409 }
Simon Glass0169e6b2012-02-13 13:51:18 +0000410 bootstage_mark(BOOTSTAGE_ID_IDE_PART_TYPE);
wdenkc6097192002-11-03 00:24:07 +0000411
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000412 printf("\nLoading from IDE device %d, partition %d: "
413 "Name: %.32s Type: %.32s\n", dev, part, info.name, info.type);
wdenkc6097192002-11-03 00:24:07 +0000414
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000415 debug("First Block: %ld, # of blocks: %ld, Block Size: %ld\n",
416 info.start, info.size, info.blksz);
wdenkc6097192002-11-03 00:24:07 +0000417
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000418 if (ide_dev_desc[dev].
419 block_read(dev, info.start, 1, (ulong *) addr) != 1) {
420 printf("** Read error on %d:%d\n", dev, part);
Simon Glass0169e6b2012-02-13 13:51:18 +0000421 bootstage_error(BOOTSTAGE_ID_IDE_PART_READ);
wdenkc6097192002-11-03 00:24:07 +0000422 return 1;
423 }
Simon Glass0169e6b2012-02-13 13:51:18 +0000424 bootstage_mark(BOOTSTAGE_ID_IDE_PART_READ);
wdenkc6097192002-11-03 00:24:07 +0000425
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000426 switch (genimg_get_format((void *) addr)) {
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100427 case IMAGE_FORMAT_LEGACY:
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000428 hdr = (image_header_t *) addr;
wdenkc6097192002-11-03 00:24:07 +0000429
Simon Glass0169e6b2012-02-13 13:51:18 +0000430 bootstage_mark(BOOTSTAGE_ID_IDE_FORMAT);
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100431
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000432 if (!image_check_hcrc(hdr)) {
433 puts("\n** Bad Header Checksum **\n");
Simon Glass0169e6b2012-02-13 13:51:18 +0000434 bootstage_error(BOOTSTAGE_ID_IDE_CHECKSUM);
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100435 return 1;
436 }
Simon Glass0169e6b2012-02-13 13:51:18 +0000437 bootstage_mark(BOOTSTAGE_ID_IDE_CHECKSUM);
wdenkc6097192002-11-03 00:24:07 +0000438
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000439 image_print_contents(hdr);
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100440
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000441 cnt = image_get_image_size(hdr);
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100442 break;
443#if defined(CONFIG_FIT)
444 case IMAGE_FORMAT_FIT:
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000445 fit_hdr = (const void *) addr;
446 puts("Fit image detected...\n");
Marian Balakowicze6bdd342008-03-12 10:33:01 +0100447
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000448 cnt = fit_get_size(fit_hdr);
Marian Balakowicze6bdd342008-03-12 10:33:01 +0100449 break;
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100450#endif
451 default:
Simon Glass0169e6b2012-02-13 13:51:18 +0000452 bootstage_error(BOOTSTAGE_ID_IDE_FORMAT);
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000453 puts("** Unknown image type\n");
wdenkf1276d42005-02-03 23:00:49 +0000454 return 1;
455 }
wdenkf1276d42005-02-03 23:00:49 +0000456
wdenkf1276d42005-02-03 23:00:49 +0000457 cnt += info.blksz - 1;
458 cnt /= info.blksz;
459 cnt -= 1;
460
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000461 if (ide_dev_desc[dev].block_read(dev, info.start + 1, cnt,
462 (ulong *)(addr + info.blksz)) != cnt) {
463 printf("** Read error on %d:%d\n", dev, part);
Simon Glass0169e6b2012-02-13 13:51:18 +0000464 bootstage_error(BOOTSTAGE_ID_IDE_READ);
wdenkc6097192002-11-03 00:24:07 +0000465 return 1;
466 }
Simon Glass0169e6b2012-02-13 13:51:18 +0000467 bootstage_mark(BOOTSTAGE_ID_IDE_READ);
wdenkc6097192002-11-03 00:24:07 +0000468
Marian Balakowicze6bdd342008-03-12 10:33:01 +0100469#if defined(CONFIG_FIT)
470 /* This cannot be done earlier, we need complete FIT image in RAM first */
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000471 if (genimg_get_format((void *) addr) == IMAGE_FORMAT_FIT) {
472 if (!fit_check_format(fit_hdr)) {
Simon Glass0169e6b2012-02-13 13:51:18 +0000473 bootstage_error(BOOTSTAGE_ID_IDE_FIT_READ);
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000474 puts("** Bad FIT image format\n");
Marian Balakowiczcefb4022008-06-06 23:07:40 +0200475 return 1;
476 }
Simon Glass0169e6b2012-02-13 13:51:18 +0000477 bootstage_mark(BOOTSTAGE_ID_IDE_FIT_READ_OK);
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000478 fit_print_contents(fit_hdr);
Marian Balakowiczcefb4022008-06-06 23:07:40 +0200479 }
Marian Balakowicze6bdd342008-03-12 10:33:01 +0100480#endif
wdenkc6097192002-11-03 00:24:07 +0000481
482 /* Loading ok, update default load address */
483
484 load_addr = addr;
485
Mike Frysinger194c2e82011-06-05 13:43:02 +0000486 return bootm_maybe_autostart(cmdtp, argv[0]);
wdenkc6097192002-11-03 00:24:07 +0000487}
488
489/* ------------------------------------------------------------------------- */
490
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000491inline void __ide_outb(int dev, int port, unsigned char val)
Stefan Roese37628252008-08-06 14:05:38 +0200492{
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000493 debug("ide_outb (dev= %d, port= 0x%x, val= 0x%02x) : @ 0x%08lx\n",
494 dev, port, val,
495 (ATA_CURR_BASE(dev) + CONFIG_SYS_ATA_PORT_ADDR(port)));
Macpaul Lind1e49942011-04-11 20:45:32 +0000496
497#if defined(CONFIG_IDE_AHB)
498 if (port) {
499 /* write command */
500 ide_write_register(dev, port, val);
501 } else {
502 /* write data */
503 outb(val, (ATA_CURR_BASE(dev)));
504 }
505#else
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000506 outb(val, (ATA_CURR_BASE(dev) + CONFIG_SYS_ATA_PORT_ADDR(port)));
Macpaul Lind1e49942011-04-11 20:45:32 +0000507#endif
Stefan Roese37628252008-08-06 14:05:38 +0200508}
Macpaul Lind1e49942011-04-11 20:45:32 +0000509
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000510void ide_outb(int dev, int port, unsigned char val)
511 __attribute__ ((weak, alias("__ide_outb")));
Stefan Roese37628252008-08-06 14:05:38 +0200512
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000513inline unsigned char __ide_inb(int dev, int port)
Stefan Roese37628252008-08-06 14:05:38 +0200514{
515 uchar val;
Macpaul Lind1e49942011-04-11 20:45:32 +0000516
517#if defined(CONFIG_IDE_AHB)
518 val = ide_read_register(dev, port);
519#else
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000520 val = inb((ATA_CURR_BASE(dev) + CONFIG_SYS_ATA_PORT_ADDR(port)));
Macpaul Lind1e49942011-04-11 20:45:32 +0000521#endif
522
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000523 debug("ide_inb (dev= %d, port= 0x%x) : @ 0x%08lx -> 0x%02x\n",
524 dev, port,
525 (ATA_CURR_BASE(dev) + CONFIG_SYS_ATA_PORT_ADDR(port)), val);
Stefan Roese37628252008-08-06 14:05:38 +0200526 return val;
527}
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000528
Kim Phillips8167f3c2009-05-19 12:53:36 -0500529unsigned char ide_inb(int dev, int port)
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000530 __attribute__ ((weak, alias("__ide_inb")));
Stefan Roese37628252008-08-06 14:05:38 +0200531
Steven A. Falco2b7574e2008-08-15 15:34:10 -0400532#ifdef CONFIG_TUNE_PIO
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000533inline int __ide_set_piomode(int pio_mode)
Steven A. Falco2b7574e2008-08-15 15:34:10 -0400534{
535 return 0;
536}
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000537
538inline int ide_set_piomode(int pio_mode)
539 __attribute__ ((weak, alias("__ide_set_piomode")));
Steven A. Falco2b7574e2008-08-15 15:34:10 -0400540#endif
541
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000542void ide_init(void)
wdenkc6097192002-11-03 00:24:07 +0000543{
wdenkc6097192002-11-03 00:24:07 +0000544
545#ifdef CONFIG_IDE_8xx_DIRECT
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000546 volatile immap_t *immr = (immap_t *) CONFIG_SYS_IMMR;
wdenkc6097192002-11-03 00:24:07 +0000547 volatile pcmconf8xx_t *pcmp = &(immr->im_pcmcia);
548#endif
549 unsigned char c;
550 int i, bus;
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000551
Wolfgang Denkb0b104a2010-06-13 18:28:54 +0200552#if defined(CONFIG_SC3)
Wolfgang Denkd2a7a742007-06-08 10:24:58 +0200553 unsigned int ata_reset_time = ATA_RESET_TIME;
Wolfgang Denk15c6b152007-04-11 17:22:55 +0200554#endif
wdenk7f00b942003-12-07 23:55:12 +0000555#ifdef CONFIG_IDE_8xx_PCCARD
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000556 extern int pcmcia_on(void);
557 extern int ide_devices_found; /* Initialized in check_ide_device() */
558#endif /* CONFIG_IDE_8xx_PCCARD */
wdenk7f00b942003-12-07 23:55:12 +0000559
560#ifdef CONFIG_IDE_PREINIT
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000561 extern int ide_preinit(void);
562
wdenk7f00b942003-12-07 23:55:12 +0000563 WATCHDOG_RESET();
564
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000565 if (ide_preinit()) {
566 puts("ide_preinit failed\n");
wdenk7f00b942003-12-07 23:55:12 +0000567 return;
568 }
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000569#endif /* CONFIG_IDE_PREINIT */
wdenkc6097192002-11-03 00:24:07 +0000570
571#ifdef CONFIG_IDE_8xx_PCCARD
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000572 extern int pcmcia_on(void);
573 extern int ide_devices_found; /* Initialized in check_ide_device() */
wdenkc6097192002-11-03 00:24:07 +0000574
575 WATCHDOG_RESET();
576
wdenk4fc95692003-02-28 00:49:47 +0000577 ide_devices_found = 0;
wdenkc6097192002-11-03 00:24:07 +0000578 /* initialize the PCMCIA IDE adapter card */
wdenk4fc95692003-02-28 00:49:47 +0000579 pcmcia_on();
580 if (!ide_devices_found)
wdenkc6097192002-11-03 00:24:07 +0000581 return;
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000582 udelay(1000000); /* 1 s */
583#endif /* CONFIG_IDE_8xx_PCCARD */
wdenkc6097192002-11-03 00:24:07 +0000584
585 WATCHDOG_RESET();
586
wdenk9f837932003-10-09 19:00:25 +0000587#ifdef CONFIG_IDE_8xx_DIRECT
wdenkc6097192002-11-03 00:24:07 +0000588 /* Initialize PIO timing tables */
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000589 for (i = 0; i <= IDE_MAX_PIO_MODE; ++i) {
590 pio_config_clk[i].t_setup =
591 PCMCIA_MK_CLKS(pio_config_ns[i].t_setup, gd->bus_clk);
592 pio_config_clk[i].t_length =
593 PCMCIA_MK_CLKS(pio_config_ns[i].t_length,
594 gd->bus_clk);
595 pio_config_clk[i].t_hold =
596 PCMCIA_MK_CLKS(pio_config_ns[i].t_hold, gd->bus_clk);
597 debug("PIO Mode %d: setup=%2d ns/%d clk" " len=%3d ns/%d clk"
598 " hold=%2d ns/%d clk\n", i, pio_config_ns[i].t_setup,
599 pio_config_clk[i].t_setup, pio_config_ns[i].t_length,
600 pio_config_clk[i].t_length, pio_config_ns[i].t_hold,
601 pio_config_clk[i].t_hold);
wdenkc6097192002-11-03 00:24:07 +0000602 }
wdenk9f837932003-10-09 19:00:25 +0000603#endif /* CONFIG_IDE_8xx_DIRECT */
wdenkc6097192002-11-03 00:24:07 +0000604
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000605 /*
606 * Reset the IDE just to be sure.
wdenkc6097192002-11-03 00:24:07 +0000607 * Light LED's to show
608 */
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000609 ide_led((LED_IDE1 | LED_IDE2), 1); /* LED's on */
610
611 /* ATAPI Drives seems to need a proper IDE Reset */
612 ide_reset();
wdenkc6097192002-11-03 00:24:07 +0000613
614#ifdef CONFIG_IDE_8xx_DIRECT
615 /* PCMCIA / IDE initialization for common mem space */
616 pcmp->pcmc_pgcrb = 0;
wdenkc6097192002-11-03 00:24:07 +0000617
618 /* start in PIO mode 0 - most relaxed timings */
619 pio_mode = 0;
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000620 set_pcmcia_timing(pio_mode);
wdenk9f837932003-10-09 19:00:25 +0000621#endif /* CONFIG_IDE_8xx_DIRECT */
wdenkc6097192002-11-03 00:24:07 +0000622
623 /*
624 * Wait for IDE to get ready.
625 * According to spec, this can take up to 31 seconds!
626 */
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000627 for (bus = 0; bus < CONFIG_SYS_IDE_MAXBUS; ++bus) {
628 int dev =
629 bus * (CONFIG_SYS_IDE_MAXDEVICE /
630 CONFIG_SYS_IDE_MAXBUS);
wdenkc6097192002-11-03 00:24:07 +0000631
wdenk4fc95692003-02-28 00:49:47 +0000632#ifdef CONFIG_IDE_8xx_PCCARD
633 /* Skip non-ide devices from probing */
634 if ((ide_devices_found & (1 << bus)) == 0) {
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000635 ide_led((LED_IDE1 | LED_IDE2), 0); /* LED's off */
wdenk4fc95692003-02-28 00:49:47 +0000636 continue;
637 }
638#endif
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000639 printf("Bus %d: ", bus);
wdenkc6097192002-11-03 00:24:07 +0000640
641 ide_bus_ok[bus] = 0;
642
643 /* Select device
644 */
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000645 udelay(100000); /* 100 ms */
646 ide_outb(dev, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(dev));
647 udelay(100000); /* 100 ms */
wdenkc6097192002-11-03 00:24:07 +0000648 i = 0;
649 do {
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000650 udelay(10000); /* 10 ms */
wdenkc6097192002-11-03 00:24:07 +0000651
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000652 c = ide_inb(dev, ATA_STATUS);
wdenkc6097192002-11-03 00:24:07 +0000653 i++;
Wolfgang Denkb0b104a2010-06-13 18:28:54 +0200654#if defined(CONFIG_SC3)
wdenk452cfd62002-11-19 11:04:11 +0000655 if (i > (ata_reset_time * 100)) {
656#else
wdenkc6097192002-11-03 00:24:07 +0000657 if (i > (ATA_RESET_TIME * 100)) {
wdenk452cfd62002-11-19 11:04:11 +0000658#endif
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000659 puts("** Timeout **\n");
660 /* LED's off */
661 ide_led((LED_IDE1 | LED_IDE2), 0);
wdenkc6097192002-11-03 00:24:07 +0000662 return;
663 }
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000664 if ((i >= 100) && ((i % 100) == 0))
665 putc('.');
666
wdenkc6097192002-11-03 00:24:07 +0000667 } while (c & ATA_STAT_BUSY);
668
669 if (c & (ATA_STAT_BUSY | ATA_STAT_FAULT)) {
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000670 puts("not available ");
671 debug("Status = 0x%02X ", c);
672#ifndef CONFIG_ATAPI /* ATAPI Devices do not set DRDY */
673 } else if ((c & ATA_STAT_READY) == 0) {
674 puts("not available ");
675 debug("Status = 0x%02X ", c);
wdenkc6097192002-11-03 00:24:07 +0000676#endif
677 } else {
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000678 puts("OK ");
wdenkc6097192002-11-03 00:24:07 +0000679 ide_bus_ok[bus] = 1;
680 }
681 WATCHDOG_RESET();
682 }
wdenk452cfd62002-11-19 11:04:11 +0000683
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000684 putc('\n');
wdenkc6097192002-11-03 00:24:07 +0000685
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000686 ide_led((LED_IDE1 | LED_IDE2), 0); /* LED's off */
wdenkc6097192002-11-03 00:24:07 +0000687
688 curr_device = -1;
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000689 for (i = 0; i < CONFIG_SYS_IDE_MAXDEVICE; ++i) {
wdenkc6097192002-11-03 00:24:07 +0000690#ifdef CONFIG_IDE_LED
691 int led = (IDE_BUS(i) == 0) ? LED_IDE1 : LED_IDE2;
692#endif
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000693 ide_dev_desc[i].type = DEV_TYPE_UNKNOWN;
694 ide_dev_desc[i].if_type = IF_TYPE_IDE;
695 ide_dev_desc[i].dev = i;
696 ide_dev_desc[i].part_type = PART_TYPE_UNKNOWN;
697 ide_dev_desc[i].blksz = 0;
698 ide_dev_desc[i].lba = 0;
699 ide_dev_desc[i].block_read = ide_read;
Macpaul Lind1e49942011-04-11 20:45:32 +0000700 ide_dev_desc[i].block_write = ide_write;
wdenkc6097192002-11-03 00:24:07 +0000701 if (!ide_bus_ok[IDE_BUS(i)])
702 continue;
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000703 ide_led(led, 1); /* LED on */
wdenkc6097192002-11-03 00:24:07 +0000704 ide_ident(&ide_dev_desc[i]);
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000705 ide_led(led, 0); /* LED off */
wdenkc6097192002-11-03 00:24:07 +0000706 dev_print(&ide_dev_desc[i]);
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000707
wdenkc6097192002-11-03 00:24:07 +0000708 if ((ide_dev_desc[i].lba > 0) && (ide_dev_desc[i].blksz > 0)) {
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000709 /* initialize partition type */
710 init_part(&ide_dev_desc[i]);
wdenkc6097192002-11-03 00:24:07 +0000711 if (curr_device < 0)
712 curr_device = i;
713 }
714 }
715 WATCHDOG_RESET();
716}
717
718/* ------------------------------------------------------------------------- */
719
Matthew McClintock6252b4f2011-05-24 05:31:19 +0000720#ifdef CONFIG_PARTITIONS
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000721block_dev_desc_t *ide_get_dev(int dev)
wdenkc6097192002-11-03 00:24:07 +0000722{
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200723 return (dev < CONFIG_SYS_IDE_MAXDEVICE) ? &ide_dev_desc[dev] : NULL;
wdenkc6097192002-11-03 00:24:07 +0000724}
Matthew McClintock6252b4f2011-05-24 05:31:19 +0000725#endif
wdenkc6097192002-11-03 00:24:07 +0000726
727
728#ifdef CONFIG_IDE_8xx_DIRECT
729
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000730static void set_pcmcia_timing(int pmode)
wdenkc6097192002-11-03 00:24:07 +0000731{
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000732 volatile immap_t *immr = (immap_t *) CONFIG_SYS_IMMR;
wdenkc6097192002-11-03 00:24:07 +0000733 volatile pcmconf8xx_t *pcmp = &(immr->im_pcmcia);
734 ulong timings;
735
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000736 debug("Set timing for PIO Mode %d\n", pmode);
wdenkc6097192002-11-03 00:24:07 +0000737
738 timings = PCMCIA_SHT(pio_config_clk[pmode].t_hold)
739 | PCMCIA_SST(pio_config_clk[pmode].t_setup)
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000740 | PCMCIA_SL(pio_config_clk[pmode].t_length);
wdenkc6097192002-11-03 00:24:07 +0000741
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000742 /*
743 * IDE 0
wdenkc6097192002-11-03 00:24:07 +0000744 */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200745 pcmp->pcmc_pbr0 = CONFIG_SYS_PCMCIA_PBR0;
746 pcmp->pcmc_por0 = CONFIG_SYS_PCMCIA_POR0
747#if (CONFIG_SYS_PCMCIA_POR0 != 0)
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000748 | timings
wdenkc6097192002-11-03 00:24:07 +0000749#endif
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000750 ;
751 debug("PBR0: %08x POR0: %08x\n", pcmp->pcmc_pbr0, pcmp->pcmc_por0);
wdenkc6097192002-11-03 00:24:07 +0000752
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200753 pcmp->pcmc_pbr1 = CONFIG_SYS_PCMCIA_PBR1;
754 pcmp->pcmc_por1 = CONFIG_SYS_PCMCIA_POR1
755#if (CONFIG_SYS_PCMCIA_POR1 != 0)
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000756 | timings
wdenkc6097192002-11-03 00:24:07 +0000757#endif
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000758 ;
759 debug("PBR1: %08x POR1: %08x\n", pcmp->pcmc_pbr1, pcmp->pcmc_por1);
wdenkc6097192002-11-03 00:24:07 +0000760
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200761 pcmp->pcmc_pbr2 = CONFIG_SYS_PCMCIA_PBR2;
762 pcmp->pcmc_por2 = CONFIG_SYS_PCMCIA_POR2
763#if (CONFIG_SYS_PCMCIA_POR2 != 0)
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000764 | timings
wdenkc6097192002-11-03 00:24:07 +0000765#endif
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000766 ;
767 debug("PBR2: %08x POR2: %08x\n", pcmp->pcmc_pbr2, pcmp->pcmc_por2);
wdenkc6097192002-11-03 00:24:07 +0000768
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200769 pcmp->pcmc_pbr3 = CONFIG_SYS_PCMCIA_PBR3;
770 pcmp->pcmc_por3 = CONFIG_SYS_PCMCIA_POR3
771#if (CONFIG_SYS_PCMCIA_POR3 != 0)
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000772 | timings
wdenkc6097192002-11-03 00:24:07 +0000773#endif
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000774 ;
775 debug("PBR3: %08x POR3: %08x\n", pcmp->pcmc_pbr3, pcmp->pcmc_por3);
wdenkc6097192002-11-03 00:24:07 +0000776
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000777 /*
778 * IDE 1
wdenkc6097192002-11-03 00:24:07 +0000779 */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200780 pcmp->pcmc_pbr4 = CONFIG_SYS_PCMCIA_PBR4;
781 pcmp->pcmc_por4 = CONFIG_SYS_PCMCIA_POR4
782#if (CONFIG_SYS_PCMCIA_POR4 != 0)
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000783 | timings
wdenkc6097192002-11-03 00:24:07 +0000784#endif
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000785 ;
786 debug("PBR4: %08x POR4: %08x\n", pcmp->pcmc_pbr4, pcmp->pcmc_por4);
wdenkc6097192002-11-03 00:24:07 +0000787
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200788 pcmp->pcmc_pbr5 = CONFIG_SYS_PCMCIA_PBR5;
789 pcmp->pcmc_por5 = CONFIG_SYS_PCMCIA_POR5
790#if (CONFIG_SYS_PCMCIA_POR5 != 0)
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000791 | timings
wdenkc6097192002-11-03 00:24:07 +0000792#endif
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000793 ;
794 debug("PBR5: %08x POR5: %08x\n", pcmp->pcmc_pbr5, pcmp->pcmc_por5);
wdenkc6097192002-11-03 00:24:07 +0000795
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200796 pcmp->pcmc_pbr6 = CONFIG_SYS_PCMCIA_PBR6;
797 pcmp->pcmc_por6 = CONFIG_SYS_PCMCIA_POR6
798#if (CONFIG_SYS_PCMCIA_POR6 != 0)
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000799 | timings
wdenkc6097192002-11-03 00:24:07 +0000800#endif
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000801 ;
802 debug("PBR6: %08x POR6: %08x\n", pcmp->pcmc_pbr6, pcmp->pcmc_por6);
wdenkc6097192002-11-03 00:24:07 +0000803
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200804 pcmp->pcmc_pbr7 = CONFIG_SYS_PCMCIA_PBR7;
805 pcmp->pcmc_por7 = CONFIG_SYS_PCMCIA_POR7
806#if (CONFIG_SYS_PCMCIA_POR7 != 0)
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000807 | timings
wdenkc6097192002-11-03 00:24:07 +0000808#endif
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000809 ;
810 debug("PBR7: %08x POR7: %08x\n", pcmp->pcmc_pbr7, pcmp->pcmc_por7);
wdenkc6097192002-11-03 00:24:07 +0000811
812}
813
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000814#endif /* CONFIG_IDE_8xx_DIRECT */
wdenkc6097192002-11-03 00:24:07 +0000815
816/* ------------------------------------------------------------------------- */
817
wdenk9b7f3842003-10-09 20:09:04 +0000818/* We only need to swap data if we are running on a big endian cpu. */
819/* But Au1x00 cpu:s already swaps data in big endian mode! */
Shinya Kuribayashi9b33e662011-02-05 19:07:00 +0900820#if defined(__LITTLE_ENDIAN) || \
821 (defined(CONFIG_SOC_AU1X00) && !defined(CONFIG_GTH2))
wdenk9b7f3842003-10-09 20:09:04 +0000822#define input_swap_data(x,y,z) input_data(x,y,z)
823#else
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000824static void input_swap_data(int dev, ulong *sect_buf, int words)
wdenkc6097192002-11-03 00:24:07 +0000825{
Wolfgang Denkfb52e952010-09-19 21:28:25 +0200826#if defined(CONFIG_CPC45)
wdenk2f99a692004-01-04 22:51:12 +0000827 uchar i;
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000828 volatile uchar *pbuf_even =
829 (uchar *) (ATA_CURR_BASE(dev) + ATA_DATA_EVEN);
830 volatile uchar *pbuf_odd =
831 (uchar *) (ATA_CURR_BASE(dev) + ATA_DATA_ODD);
832 ushort *dbuf = (ushort *) sect_buf;
wdenk2f99a692004-01-04 22:51:12 +0000833
834 while (words--) {
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000835 for (i = 0; i < 2; i++) {
836 *(((uchar *) (dbuf)) + 1) = *pbuf_even;
837 *(uchar *) dbuf = *pbuf_odd;
838 dbuf += 1;
wdenk2f99a692004-01-04 22:51:12 +0000839 }
840 }
wdenkb995b0f2005-03-06 01:21:30 +0000841#else
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000842 volatile ushort *pbuf =
843 (ushort *) (ATA_CURR_BASE(dev) + ATA_DATA_REG);
844 ushort *dbuf = (ushort *) sect_buf;
wdenkf1276d42005-02-03 23:00:49 +0000845
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000846 debug("in input swap data base for read is %lx\n",
847 (unsigned long) pbuf);
wdenkf1276d42005-02-03 23:00:49 +0000848
849 while (words--) {
Wolfgang Denk39c76422006-06-16 17:32:31 +0200850#ifdef __MIPS__
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000851 *dbuf++ = swab16p((u16 *) pbuf);
852 *dbuf++ = swab16p((u16 *) pbuf);
Heiko Schocher633e03a2007-06-22 19:11:54 +0200853#elif defined(CONFIG_PCS440EP)
854 *dbuf++ = *pbuf;
855 *dbuf++ = *pbuf;
Wolfgang Denk39c76422006-06-16 17:32:31 +0200856#else
wdenkf1276d42005-02-03 23:00:49 +0000857 *dbuf++ = ld_le16(pbuf);
858 *dbuf++ = ld_le16(pbuf);
Wolfgang Denk39c76422006-06-16 17:32:31 +0200859#endif /* !MIPS */
wdenkf1276d42005-02-03 23:00:49 +0000860 }
861#endif
wdenkc6097192002-11-03 00:24:07 +0000862}
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000863#endif /* __LITTLE_ENDIAN || CONFIG_AU1X00 */
wdenk591dda52002-11-18 00:14:45 +0000864
865
Albert Aribaud036c6b42010-08-08 05:17:05 +0530866#if defined(CONFIG_IDE_SWAP_IO)
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000867static void output_data(int dev, const ulong *sect_buf, int words)
wdenkc6097192002-11-03 00:24:07 +0000868{
Wolfgang Denkfb52e952010-09-19 21:28:25 +0200869#if defined(CONFIG_CPC45)
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000870 uchar *dbuf;
871 volatile uchar *pbuf_even;
872 volatile uchar *pbuf_odd;
wdenk2f99a692004-01-04 22:51:12 +0000873
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000874 pbuf_even = (uchar *) (ATA_CURR_BASE(dev) + ATA_DATA_EVEN);
875 pbuf_odd = (uchar *) (ATA_CURR_BASE(dev) + ATA_DATA_ODD);
876 dbuf = (uchar *) sect_buf;
wdenk2f99a692004-01-04 22:51:12 +0000877 while (words--) {
wdenk20c98a62004-04-23 20:32:05 +0000878 EIEIO;
wdenk2f99a692004-01-04 22:51:12 +0000879 *pbuf_even = *dbuf++;
wdenk20c98a62004-04-23 20:32:05 +0000880 EIEIO;
wdenk2f99a692004-01-04 22:51:12 +0000881 *pbuf_odd = *dbuf++;
wdenk20c98a62004-04-23 20:32:05 +0000882 EIEIO;
wdenk2f99a692004-01-04 22:51:12 +0000883 *pbuf_even = *dbuf++;
wdenk20c98a62004-04-23 20:32:05 +0000884 EIEIO;
wdenk2f99a692004-01-04 22:51:12 +0000885 *pbuf_odd = *dbuf++;
886 }
wdenkf1276d42005-02-03 23:00:49 +0000887#else
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000888 ushort *dbuf;
889 volatile ushort *pbuf;
wdenkf1276d42005-02-03 23:00:49 +0000890
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000891 pbuf = (ushort *) (ATA_CURR_BASE(dev) + ATA_DATA_REG);
892 dbuf = (ushort *) sect_buf;
wdenkf1276d42005-02-03 23:00:49 +0000893 while (words--) {
Heiko Schocher633e03a2007-06-22 19:11:54 +0200894#if defined(CONFIG_PCS440EP)
895 /* not tested, because CF was write protected */
896 EIEIO;
897 *pbuf = ld_le16(dbuf++);
898 EIEIO;
899 *pbuf = ld_le16(dbuf++);
900#else
wdenkf1276d42005-02-03 23:00:49 +0000901 EIEIO;
902 *pbuf = *dbuf++;
903 EIEIO;
904 *pbuf = *dbuf++;
Heiko Schocher633e03a2007-06-22 19:11:54 +0200905#endif
wdenkf1276d42005-02-03 23:00:49 +0000906 }
907#endif
wdenkc6097192002-11-03 00:24:07 +0000908}
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000909#else /* ! CONFIG_IDE_SWAP_IO */
910static void output_data(int dev, const ulong *sect_buf, int words)
wdenk591dda52002-11-18 00:14:45 +0000911{
Macpaul Lind1e49942011-04-11 20:45:32 +0000912#if defined(CONFIG_IDE_AHB)
913 ide_write_data(dev, sect_buf, words);
914#else
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000915 outsw(ATA_CURR_BASE(dev) + ATA_DATA_REG, sect_buf, words << 1);
Macpaul Lind1e49942011-04-11 20:45:32 +0000916#endif
wdenk591dda52002-11-18 00:14:45 +0000917}
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000918#endif /* CONFIG_IDE_SWAP_IO */
wdenkc6097192002-11-03 00:24:07 +0000919
Albert Aribaud036c6b42010-08-08 05:17:05 +0530920#if defined(CONFIG_IDE_SWAP_IO)
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000921static void input_data(int dev, ulong *sect_buf, int words)
wdenkc6097192002-11-03 00:24:07 +0000922{
Wolfgang Denkfb52e952010-09-19 21:28:25 +0200923#if defined(CONFIG_CPC45)
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000924 uchar *dbuf;
925 volatile uchar *pbuf_even;
926 volatile uchar *pbuf_odd;
wdenk2f99a692004-01-04 22:51:12 +0000927
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000928 pbuf_even = (uchar *) (ATA_CURR_BASE(dev) + ATA_DATA_EVEN);
929 pbuf_odd = (uchar *) (ATA_CURR_BASE(dev) + ATA_DATA_ODD);
930 dbuf = (uchar *) sect_buf;
wdenk2f99a692004-01-04 22:51:12 +0000931 while (words--) {
wdenkf1276d42005-02-03 23:00:49 +0000932 *dbuf++ = *pbuf_even;
wdenk20c98a62004-04-23 20:32:05 +0000933 EIEIO;
wdenkf1276d42005-02-03 23:00:49 +0000934 SYNC;
935 *dbuf++ = *pbuf_odd;
wdenkf6925c72005-01-22 18:26:04 +0000936 EIEIO;
wdenkf1276d42005-02-03 23:00:49 +0000937 SYNC;
wdenk2f99a692004-01-04 22:51:12 +0000938 *dbuf++ = *pbuf_even;
wdenk20c98a62004-04-23 20:32:05 +0000939 EIEIO;
wdenkf1276d42005-02-03 23:00:49 +0000940 SYNC;
wdenk2f99a692004-01-04 22:51:12 +0000941 *dbuf++ = *pbuf_odd;
wdenk20c98a62004-04-23 20:32:05 +0000942 EIEIO;
wdenkf1276d42005-02-03 23:00:49 +0000943 SYNC;
944 }
945#else
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000946 ushort *dbuf;
947 volatile ushort *pbuf;
wdenkf1276d42005-02-03 23:00:49 +0000948
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000949 pbuf = (ushort *) (ATA_CURR_BASE(dev) + ATA_DATA_REG);
950 dbuf = (ushort *) sect_buf;
wdenkf1276d42005-02-03 23:00:49 +0000951
952 debug("in input data base for read is %lx\n", (unsigned long) pbuf);
953
954 while (words--) {
Heiko Schocher633e03a2007-06-22 19:11:54 +0200955#if defined(CONFIG_PCS440EP)
956 EIEIO;
957 *dbuf++ = ld_le16(pbuf);
958 EIEIO;
959 *dbuf++ = ld_le16(pbuf);
960#else
wdenk20c98a62004-04-23 20:32:05 +0000961 EIEIO;
wdenkf1276d42005-02-03 23:00:49 +0000962 *dbuf++ = *pbuf;
wdenkf6925c72005-01-22 18:26:04 +0000963 EIEIO;
wdenkf1276d42005-02-03 23:00:49 +0000964 *dbuf++ = *pbuf;
Heiko Schocher633e03a2007-06-22 19:11:54 +0200965#endif
wdenk2f99a692004-01-04 22:51:12 +0000966 }
wdenkf1276d42005-02-03 23:00:49 +0000967#endif
wdenkc6097192002-11-03 00:24:07 +0000968}
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000969#else /* ! CONFIG_IDE_SWAP_IO */
970static void input_data(int dev, ulong *sect_buf, int words)
wdenk591dda52002-11-18 00:14:45 +0000971{
Macpaul Lind1e49942011-04-11 20:45:32 +0000972#if defined(CONFIG_IDE_AHB)
973 ide_read_data(dev, sect_buf, words);
974#else
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000975 insw(ATA_CURR_BASE(dev) + ATA_DATA_REG, sect_buf, words << 1);
Macpaul Lind1e49942011-04-11 20:45:32 +0000976#endif
wdenk591dda52002-11-18 00:14:45 +0000977}
978
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000979#endif /* CONFIG_IDE_SWAP_IO */
wdenkc6097192002-11-03 00:24:07 +0000980
981/* -------------------------------------------------------------------------
982 */
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000983static void ide_ident(block_dev_desc_t *dev_desc)
wdenkc6097192002-11-03 00:24:07 +0000984{
wdenkc6097192002-11-03 00:24:07 +0000985 unsigned char c;
Marek Vasute49757e2011-08-20 09:15:13 +0000986 hd_driveid_t iop;
wdenkc6097192002-11-03 00:24:07 +0000987
wdenke2d6d742004-09-28 20:34:50 +0000988#ifdef CONFIG_ATAPI
989 int retries = 0;
wdenk452cfd62002-11-19 11:04:11 +0000990#endif
991
Steven A. Falco2b7574e2008-08-15 15:34:10 -0400992#ifdef CONFIG_TUNE_PIO
993 int pio_mode;
994#endif
995
wdenkc6097192002-11-03 00:24:07 +0000996#if 0
997 int mode, cycle_time;
998#endif
999 int device;
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001000
1001 device = dev_desc->dev;
1002 printf(" Device %d: ", device);
wdenkc6097192002-11-03 00:24:07 +00001003
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001004 ide_led(DEVICE_LED(device), 1); /* LED on */
wdenkc6097192002-11-03 00:24:07 +00001005 /* Select device
1006 */
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001007 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
1008 dev_desc->if_type = IF_TYPE_IDE;
wdenkc6097192002-11-03 00:24:07 +00001009#ifdef CONFIG_ATAPI
wdenk452cfd62002-11-19 11:04:11 +00001010
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001011 retries = 0;
wdenk452cfd62002-11-19 11:04:11 +00001012
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001013 /* Warning: This will be tricky to read */
1014 while (retries <= 1) {
1015 /* check signature */
1016 if ((ide_inb(device, ATA_SECT_CNT) == 0x01) &&
1017 (ide_inb(device, ATA_SECT_NUM) == 0x01) &&
1018 (ide_inb(device, ATA_CYL_LOW) == 0x14) &&
1019 (ide_inb(device, ATA_CYL_HIGH) == 0xEB)) {
1020 /* ATAPI Signature found */
1021 dev_desc->if_type = IF_TYPE_ATAPI;
1022 /*
1023 * Start Ident Command
1024 */
1025 ide_outb(device, ATA_COMMAND, ATAPI_CMD_IDENT);
1026 /*
1027 * Wait for completion - ATAPI devices need more time
1028 * to become ready
1029 */
1030 c = ide_wait(device, ATAPI_TIME_OUT);
1031 } else
wdenkc6097192002-11-03 00:24:07 +00001032#endif
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001033 {
1034 /*
1035 * Start Ident Command
1036 */
1037 ide_outb(device, ATA_COMMAND, ATA_CMD_IDENT);
wdenkc6097192002-11-03 00:24:07 +00001038
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001039 /*
1040 * Wait for completion
1041 */
1042 c = ide_wait(device, IDE_TIME_OUT);
1043 }
1044 ide_led(DEVICE_LED(device), 0); /* LED off */
wdenkc6097192002-11-03 00:24:07 +00001045
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001046 if (((c & ATA_STAT_DRQ) == 0) ||
1047 ((c & (ATA_STAT_FAULT | ATA_STAT_ERR)) != 0)) {
wdenke2d6d742004-09-28 20:34:50 +00001048#ifdef CONFIG_ATAPI
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001049 {
1050 /*
1051 * Need to soft reset the device
1052 * in case it's an ATAPI...
1053 */
1054 debug("Retrying...\n");
1055 ide_outb(device, ATA_DEV_HD,
1056 ATA_LBA | ATA_DEVICE(device));
1057 udelay(100000);
1058 ide_outb(device, ATA_COMMAND, 0x08);
1059 udelay(500000); /* 500 ms */
1060 }
1061 /*
1062 * Select device
1063 */
1064 ide_outb(device, ATA_DEV_HD,
1065 ATA_LBA | ATA_DEVICE(device));
1066 retries++;
wdenke2d6d742004-09-28 20:34:50 +00001067#else
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001068 return;
wdenke2d6d742004-09-28 20:34:50 +00001069#endif
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001070 }
wdenke2d6d742004-09-28 20:34:50 +00001071#ifdef CONFIG_ATAPI
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001072 else
1073 break;
1074 } /* see above - ugly to read */
wdenke2d6d742004-09-28 20:34:50 +00001075
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001076 if (retries == 2) /* Not found */
wdenke2d6d742004-09-28 20:34:50 +00001077 return;
1078#endif
wdenkc6097192002-11-03 00:24:07 +00001079
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001080 input_swap_data(device, (ulong *)&iop, ATA_SECTORWORDS);
wdenkc6097192002-11-03 00:24:07 +00001081
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001082 ident_cpy((unsigned char *) dev_desc->revision, iop.fw_rev,
1083 sizeof(dev_desc->revision));
1084 ident_cpy((unsigned char *) dev_desc->vendor, iop.model,
1085 sizeof(dev_desc->vendor));
1086 ident_cpy((unsigned char *) dev_desc->product, iop.serial_no,
1087 sizeof(dev_desc->product));
wdenkacd9b102004-03-14 00:59:59 +00001088#ifdef __LITTLE_ENDIAN
1089 /*
Richard Retanubun54f30622008-11-06 14:01:51 -05001090 * firmware revision, model, and serial number have Big Endian Byte
1091 * order in Word. Convert all three to little endian.
wdenkacd9b102004-03-14 00:59:59 +00001092 *
1093 * See CF+ and CompactFlash Specification Revision 2.0:
Richard Retanubun54f30622008-11-06 14:01:51 -05001094 * 6.2.1.6: Identify Drive, Table 39 for more details
wdenkacd9b102004-03-14 00:59:59 +00001095 */
1096
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001097 strswab(dev_desc->revision);
1098 strswab(dev_desc->vendor);
1099 strswab(dev_desc->product);
wdenkacd9b102004-03-14 00:59:59 +00001100#endif /* __LITTLE_ENDIAN */
wdenkc6097192002-11-03 00:24:07 +00001101
Marek Vasute49757e2011-08-20 09:15:13 +00001102 if ((iop.config & 0x0080) == 0x0080)
wdenkc6097192002-11-03 00:24:07 +00001103 dev_desc->removable = 1;
1104 else
1105 dev_desc->removable = 0;
1106
Steven A. Falco2b7574e2008-08-15 15:34:10 -04001107#ifdef CONFIG_TUNE_PIO
1108 /* Mode 0 - 2 only, are directly determined by word 51. */
Marek Vasute49757e2011-08-20 09:15:13 +00001109 pio_mode = iop.tPIO;
Steven A. Falco2b7574e2008-08-15 15:34:10 -04001110 if (pio_mode > 2) {
1111 printf("WARNING: Invalid PIO (word 51 = %d).\n", pio_mode);
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001112 /* Force it to dead slow, and hope for the best... */
1113 pio_mode = 0;
Steven A. Falco2b7574e2008-08-15 15:34:10 -04001114 }
1115
1116 /* Any CompactFlash Storage Card that supports PIO mode 3 or above
1117 * shall set bit 1 of word 53 to one and support the fields contained
1118 * in words 64 through 70.
1119 */
Marek Vasute49757e2011-08-20 09:15:13 +00001120 if (iop.field_valid & 0x02) {
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001121 /*
1122 * Mode 3 and above are possible. Check in order from slow
Steven A. Falco2b7574e2008-08-15 15:34:10 -04001123 * to fast, so we wind up with the highest mode allowed.
1124 */
Marek Vasute49757e2011-08-20 09:15:13 +00001125 if (iop.eide_pio_modes & 0x01)
Steven A. Falco2b7574e2008-08-15 15:34:10 -04001126 pio_mode = 3;
Marek Vasute49757e2011-08-20 09:15:13 +00001127 if (iop.eide_pio_modes & 0x02)
Steven A. Falco2b7574e2008-08-15 15:34:10 -04001128 pio_mode = 4;
Marek Vasute49757e2011-08-20 09:15:13 +00001129 if (ata_id_is_cfa((u16 *)&iop)) {
1130 if ((iop.cf_advanced_caps & 0x07) == 0x01)
Steven A. Falco2b7574e2008-08-15 15:34:10 -04001131 pio_mode = 5;
Marek Vasute49757e2011-08-20 09:15:13 +00001132 if ((iop.cf_advanced_caps & 0x07) == 0x02)
Steven A. Falco2b7574e2008-08-15 15:34:10 -04001133 pio_mode = 6;
1134 }
1135 }
1136
1137 /* System-specific, depends on bus speeds, etc. */
1138 ide_set_piomode(pio_mode);
1139#endif /* CONFIG_TUNE_PIO */
1140
wdenkc6097192002-11-03 00:24:07 +00001141#if 0
1142 /*
1143 * Drive PIO mode autoselection
1144 */
Marek Vasute49757e2011-08-20 09:15:13 +00001145 mode = iop.tPIO;
wdenkc6097192002-11-03 00:24:07 +00001146
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001147 printf("tPIO = 0x%02x = %d\n", mode, mode);
wdenkc6097192002-11-03 00:24:07 +00001148 if (mode > 2) { /* 2 is maximum allowed tPIO value */
1149 mode = 2;
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001150 debug("Override tPIO -> 2\n");
wdenkc6097192002-11-03 00:24:07 +00001151 }
Marek Vasute49757e2011-08-20 09:15:13 +00001152 if (iop.field_valid & 2) { /* drive implements ATA2? */
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001153 debug("Drive implements ATA2\n");
Marek Vasute49757e2011-08-20 09:15:13 +00001154 if (iop.capability & 8) { /* drive supports use_iordy? */
1155 cycle_time = iop.eide_pio_iordy;
wdenkc6097192002-11-03 00:24:07 +00001156 } else {
Marek Vasute49757e2011-08-20 09:15:13 +00001157 cycle_time = iop.eide_pio;
wdenkc6097192002-11-03 00:24:07 +00001158 }
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001159 debug("cycle time = %d\n", cycle_time);
wdenkc6097192002-11-03 00:24:07 +00001160 mode = 4;
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001161 if (cycle_time > 120)
1162 mode = 3; /* 120 ns for PIO mode 4 */
1163 if (cycle_time > 180)
1164 mode = 2; /* 180 ns for PIO mode 3 */
1165 if (cycle_time > 240)
1166 mode = 1; /* 240 ns for PIO mode 4 */
1167 if (cycle_time > 383)
1168 mode = 0; /* 383 ns for PIO mode 4 */
wdenkc6097192002-11-03 00:24:07 +00001169 }
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001170 printf("PIO mode to use: PIO %d\n", mode);
wdenkc6097192002-11-03 00:24:07 +00001171#endif /* 0 */
1172
1173#ifdef CONFIG_ATAPI
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001174 if (dev_desc->if_type == IF_TYPE_ATAPI) {
wdenkc6097192002-11-03 00:24:07 +00001175 atapi_inquiry(dev_desc);
1176 return;
1177 }
1178#endif /* CONFIG_ATAPI */
1179
wdenkacd9b102004-03-14 00:59:59 +00001180#ifdef __BIG_ENDIAN
wdenkc6097192002-11-03 00:24:07 +00001181 /* swap shorts */
Marek Vasute49757e2011-08-20 09:15:13 +00001182 dev_desc->lba = (iop.lba_capacity << 16) | (iop.lba_capacity >> 16);
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001183#else /* ! __BIG_ENDIAN */
wdenkacd9b102004-03-14 00:59:59 +00001184 /*
1185 * do not swap shorts on little endian
1186 *
1187 * See CF+ and CompactFlash Specification Revision 2.0:
1188 * 6.2.1.6: Identfy Drive, Table 39, Word Address 57-58 for details.
1189 */
Marek Vasute49757e2011-08-20 09:15:13 +00001190 dev_desc->lba = iop.lba_capacity;
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001191#endif /* __BIG_ENDIAN */
wdenkf602aa02004-03-13 23:29:43 +00001192
wdenkc35ba4e2004-03-14 22:25:36 +00001193#ifdef CONFIG_LBA48
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001194 if (iop.command_set_2 & 0x0400) { /* LBA 48 support */
wdenk05939202004-04-18 17:39:38 +00001195 dev_desc->lba48 = 1;
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001196 dev_desc->lba = (unsigned long long) iop.lba48_capacity[0] |
1197 ((unsigned long long) iop.lba48_capacity[1] << 16) |
1198 ((unsigned long long) iop.lba48_capacity[2] << 32) |
1199 ((unsigned long long) iop.lba48_capacity[3] << 48);
wdenkf602aa02004-03-13 23:29:43 +00001200 } else {
wdenkf602aa02004-03-13 23:29:43 +00001201 dev_desc->lba48 = 0;
1202 }
1203#endif /* CONFIG_LBA48 */
wdenkc6097192002-11-03 00:24:07 +00001204 /* assuming HD */
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001205 dev_desc->type = DEV_TYPE_HARDDISK;
1206 dev_desc->blksz = ATA_BLOCKSIZE;
1207 dev_desc->lun = 0; /* just to fill something in... */
wdenkc6097192002-11-03 00:24:07 +00001208
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001209#if 0 /* only used to test the powersaving mode,
1210 * if enabled, the drive goes after 5 sec
1211 * in standby mode */
1212 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
1213 c = ide_wait(device, IDE_TIME_OUT);
1214 ide_outb(device, ATA_SECT_CNT, 1);
1215 ide_outb(device, ATA_LBA_LOW, 0);
1216 ide_outb(device, ATA_LBA_MID, 0);
1217 ide_outb(device, ATA_LBA_HIGH, 0);
1218 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
1219 ide_outb(device, ATA_COMMAND, 0xe3);
1220 udelay(50);
1221 c = ide_wait(device, IDE_TIME_OUT); /* can't take over 500 ms */
wdenkc6097192002-11-03 00:24:07 +00001222#endif
1223}
1224
1225
1226/* ------------------------------------------------------------------------- */
1227
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001228ulong ide_read(int device, lbaint_t blknr, ulong blkcnt, void *buffer)
wdenkc6097192002-11-03 00:24:07 +00001229{
1230 ulong n = 0;
1231 unsigned char c;
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001232 unsigned char pwrsave = 0; /* power save */
1233
wdenkc35ba4e2004-03-14 22:25:36 +00001234#ifdef CONFIG_LBA48
wdenkf602aa02004-03-13 23:29:43 +00001235 unsigned char lba48 = 0;
wdenkc6097192002-11-03 00:24:07 +00001236
Guennadi Liakhovetskib71f1ca2008-04-28 14:36:06 +02001237 if (blknr & 0x0000fffff0000000ULL) {
wdenkf602aa02004-03-13 23:29:43 +00001238 /* more than 28 bits used, use 48bit mode */
1239 lba48 = 1;
1240 }
1241#endif
Marek Vasut92131712011-10-25 11:39:15 +02001242 debug("ide_read dev %d start %lX, blocks %lX buffer at %lX\n",
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001243 device, blknr, blkcnt, (ulong) buffer);
wdenkc6097192002-11-03 00:24:07 +00001244
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001245 ide_led(DEVICE_LED(device), 1); /* LED on */
wdenkc6097192002-11-03 00:24:07 +00001246
1247 /* Select device
1248 */
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001249 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
1250 c = ide_wait(device, IDE_TIME_OUT);
wdenkc6097192002-11-03 00:24:07 +00001251
1252 if (c & ATA_STAT_BUSY) {
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001253 printf("IDE read: device %d not ready\n", device);
wdenkc6097192002-11-03 00:24:07 +00001254 goto IDE_READ_E;
1255 }
1256
1257 /* first check if the drive is in Powersaving mode, if yes,
1258 * increase the timeout value */
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001259 ide_outb(device, ATA_COMMAND, ATA_CMD_CHK_PWR);
1260 udelay(50);
wdenkc6097192002-11-03 00:24:07 +00001261
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001262 c = ide_wait(device, IDE_TIME_OUT); /* can't take over 500 ms */
wdenkc6097192002-11-03 00:24:07 +00001263
1264 if (c & ATA_STAT_BUSY) {
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001265 printf("IDE read: device %d not ready\n", device);
wdenkc6097192002-11-03 00:24:07 +00001266 goto IDE_READ_E;
1267 }
1268 if ((c & ATA_STAT_ERR) == ATA_STAT_ERR) {
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001269 printf("No Powersaving mode %X\n", c);
wdenkc6097192002-11-03 00:24:07 +00001270 } else {
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001271 c = ide_inb(device, ATA_SECT_CNT);
1272 debug("Powersaving %02X\n", c);
1273 if (c == 0)
1274 pwrsave = 1;
wdenkc6097192002-11-03 00:24:07 +00001275 }
1276
1277
1278 while (blkcnt-- > 0) {
1279
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001280 c = ide_wait(device, IDE_TIME_OUT);
wdenkc6097192002-11-03 00:24:07 +00001281
1282 if (c & ATA_STAT_BUSY) {
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001283 printf("IDE read: device %d not ready\n", device);
wdenkc6097192002-11-03 00:24:07 +00001284 break;
1285 }
wdenkc35ba4e2004-03-14 22:25:36 +00001286#ifdef CONFIG_LBA48
wdenkf602aa02004-03-13 23:29:43 +00001287 if (lba48) {
1288 /* write high bits */
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001289 ide_outb(device, ATA_SECT_CNT, 0);
1290 ide_outb(device, ATA_LBA_LOW, (blknr >> 24) & 0xFF);
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001291#ifdef CONFIG_SYS_64BIT_LBA
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001292 ide_outb(device, ATA_LBA_MID, (blknr >> 32) & 0xFF);
1293 ide_outb(device, ATA_LBA_HIGH, (blknr >> 40) & 0xFF);
Guennadi Liakhovetskib71f1ca2008-04-28 14:36:06 +02001294#else
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001295 ide_outb(device, ATA_LBA_MID, 0);
1296 ide_outb(device, ATA_LBA_HIGH, 0);
Guennadi Liakhovetskib71f1ca2008-04-28 14:36:06 +02001297#endif
wdenkf602aa02004-03-13 23:29:43 +00001298 }
1299#endif
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001300 ide_outb(device, ATA_SECT_CNT, 1);
1301 ide_outb(device, ATA_LBA_LOW, (blknr >> 0) & 0xFF);
1302 ide_outb(device, ATA_LBA_MID, (blknr >> 8) & 0xFF);
1303 ide_outb(device, ATA_LBA_HIGH, (blknr >> 16) & 0xFF);
wdenkf602aa02004-03-13 23:29:43 +00001304
wdenkc35ba4e2004-03-14 22:25:36 +00001305#ifdef CONFIG_LBA48
wdenkf602aa02004-03-13 23:29:43 +00001306 if (lba48) {
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001307 ide_outb(device, ATA_DEV_HD,
1308 ATA_LBA | ATA_DEVICE(device));
1309 ide_outb(device, ATA_COMMAND, ATA_CMD_READ_EXT);
wdenkf602aa02004-03-13 23:29:43 +00001310
1311 } else
1312#endif
1313 {
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001314 ide_outb(device, ATA_DEV_HD, ATA_LBA |
1315 ATA_DEVICE(device) | ((blknr >> 24) & 0xF));
1316 ide_outb(device, ATA_COMMAND, ATA_CMD_READ);
wdenkf602aa02004-03-13 23:29:43 +00001317 }
wdenkc6097192002-11-03 00:24:07 +00001318
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001319 udelay(50);
wdenkc6097192002-11-03 00:24:07 +00001320
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001321 if (pwrsave) {
1322 /* may take up to 4 sec */
1323 c = ide_wait(device, IDE_SPIN_UP_TIME_OUT);
1324 pwrsave = 0;
wdenkc6097192002-11-03 00:24:07 +00001325 } else {
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001326 /* can't take over 500 ms */
1327 c = ide_wait(device, IDE_TIME_OUT);
wdenkc6097192002-11-03 00:24:07 +00001328 }
1329
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001330 if ((c & (ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR)) !=
1331 ATA_STAT_DRQ) {
Heiko Schocher0f602e12009-12-03 11:21:21 +01001332#if defined(CONFIG_SYS_64BIT_LBA)
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001333 printf("Error (no IRQ) dev %d blk %lld: status 0x%02x\n",
wdenkc6097192002-11-03 00:24:07 +00001334 device, blknr, c);
wdenkf602aa02004-03-13 23:29:43 +00001335#else
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001336 printf("Error (no IRQ) dev %d blk %ld: status 0x%02x\n",
1337 device, (ulong) blknr, c);
wdenkf602aa02004-03-13 23:29:43 +00001338#endif
wdenkc6097192002-11-03 00:24:07 +00001339 break;
1340 }
1341
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001342 input_data(device, buffer, ATA_SECTORWORDS);
1343 (void) ide_inb(device, ATA_STATUS); /* clear IRQ */
wdenkc6097192002-11-03 00:24:07 +00001344
1345 ++n;
1346 ++blknr;
Greg Lopp50abf9f2007-04-13 08:02:24 +02001347 buffer += ATA_BLOCKSIZE;
wdenkc6097192002-11-03 00:24:07 +00001348 }
1349IDE_READ_E:
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001350 ide_led(DEVICE_LED(device), 0); /* LED off */
wdenkc6097192002-11-03 00:24:07 +00001351 return (n);
1352}
1353
1354/* ------------------------------------------------------------------------- */
1355
1356
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001357ulong ide_write(int device, lbaint_t blknr, ulong blkcnt, const void *buffer)
wdenkc6097192002-11-03 00:24:07 +00001358{
1359 ulong n = 0;
1360 unsigned char c;
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001361
wdenkc35ba4e2004-03-14 22:25:36 +00001362#ifdef CONFIG_LBA48
wdenkf602aa02004-03-13 23:29:43 +00001363 unsigned char lba48 = 0;
1364
Guennadi Liakhovetskib71f1ca2008-04-28 14:36:06 +02001365 if (blknr & 0x0000fffff0000000ULL) {
wdenkf602aa02004-03-13 23:29:43 +00001366 /* more than 28 bits used, use 48bit mode */
1367 lba48 = 1;
1368 }
1369#endif
wdenkc6097192002-11-03 00:24:07 +00001370
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001371 ide_led(DEVICE_LED(device), 1); /* LED on */
wdenkc6097192002-11-03 00:24:07 +00001372
1373 /* Select device
1374 */
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001375 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
wdenkc6097192002-11-03 00:24:07 +00001376
1377 while (blkcnt-- > 0) {
1378
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001379 c = ide_wait(device, IDE_TIME_OUT);
wdenkc6097192002-11-03 00:24:07 +00001380
1381 if (c & ATA_STAT_BUSY) {
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001382 printf("IDE read: device %d not ready\n", device);
wdenkc6097192002-11-03 00:24:07 +00001383 goto WR_OUT;
1384 }
wdenkc35ba4e2004-03-14 22:25:36 +00001385#ifdef CONFIG_LBA48
wdenkf602aa02004-03-13 23:29:43 +00001386 if (lba48) {
1387 /* write high bits */
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001388 ide_outb(device, ATA_SECT_CNT, 0);
1389 ide_outb(device, ATA_LBA_LOW, (blknr >> 24) & 0xFF);
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001390#ifdef CONFIG_SYS_64BIT_LBA
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001391 ide_outb(device, ATA_LBA_MID, (blknr >> 32) & 0xFF);
1392 ide_outb(device, ATA_LBA_HIGH, (blknr >> 40) & 0xFF);
Guennadi Liakhovetskib71f1ca2008-04-28 14:36:06 +02001393#else
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001394 ide_outb(device, ATA_LBA_MID, 0);
1395 ide_outb(device, ATA_LBA_HIGH, 0);
Guennadi Liakhovetskib71f1ca2008-04-28 14:36:06 +02001396#endif
wdenkf602aa02004-03-13 23:29:43 +00001397 }
1398#endif
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001399 ide_outb(device, ATA_SECT_CNT, 1);
1400 ide_outb(device, ATA_LBA_LOW, (blknr >> 0) & 0xFF);
1401 ide_outb(device, ATA_LBA_MID, (blknr >> 8) & 0xFF);
1402 ide_outb(device, ATA_LBA_HIGH, (blknr >> 16) & 0xFF);
wdenkf602aa02004-03-13 23:29:43 +00001403
wdenkc35ba4e2004-03-14 22:25:36 +00001404#ifdef CONFIG_LBA48
wdenkf602aa02004-03-13 23:29:43 +00001405 if (lba48) {
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001406 ide_outb(device, ATA_DEV_HD,
1407 ATA_LBA | ATA_DEVICE(device));
1408 ide_outb(device, ATA_COMMAND, ATA_CMD_WRITE_EXT);
wdenkf602aa02004-03-13 23:29:43 +00001409
1410 } else
1411#endif
1412 {
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001413 ide_outb(device, ATA_DEV_HD, ATA_LBA |
1414 ATA_DEVICE(device) | ((blknr >> 24) & 0xF));
1415 ide_outb(device, ATA_COMMAND, ATA_CMD_WRITE);
wdenkf602aa02004-03-13 23:29:43 +00001416 }
wdenkc6097192002-11-03 00:24:07 +00001417
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001418 udelay(50);
wdenkc6097192002-11-03 00:24:07 +00001419
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001420 /* can't take over 500 ms */
1421 c = ide_wait(device, IDE_TIME_OUT);
wdenkc6097192002-11-03 00:24:07 +00001422
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001423 if ((c & (ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR)) !=
1424 ATA_STAT_DRQ) {
Heiko Schocher0f602e12009-12-03 11:21:21 +01001425#if defined(CONFIG_SYS_64BIT_LBA)
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001426 printf("Error (no IRQ) dev %d blk %lld: status 0x%02x\n",
wdenkc6097192002-11-03 00:24:07 +00001427 device, blknr, c);
wdenkf602aa02004-03-13 23:29:43 +00001428#else
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001429 printf("Error (no IRQ) dev %d blk %ld: status 0x%02x\n",
1430 device, (ulong) blknr, c);
wdenkf602aa02004-03-13 23:29:43 +00001431#endif
wdenkc6097192002-11-03 00:24:07 +00001432 goto WR_OUT;
1433 }
1434
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001435 output_data(device, buffer, ATA_SECTORWORDS);
1436 c = ide_inb(device, ATA_STATUS); /* clear IRQ */
wdenkc6097192002-11-03 00:24:07 +00001437 ++n;
1438 ++blknr;
Greg Lopp50abf9f2007-04-13 08:02:24 +02001439 buffer += ATA_BLOCKSIZE;
wdenkc6097192002-11-03 00:24:07 +00001440 }
1441WR_OUT:
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001442 ide_led(DEVICE_LED(device), 0); /* LED off */
wdenkc6097192002-11-03 00:24:07 +00001443 return (n);
1444}
1445
1446/* ------------------------------------------------------------------------- */
1447
1448/*
1449 * copy src to dest, skipping leading and trailing blanks and null
1450 * terminate the string
wdenk18bd81e2004-03-17 01:13:07 +00001451 * "len" is the size of available memory including the terminating '\0'
wdenkc6097192002-11-03 00:24:07 +00001452 */
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001453static void ident_cpy(unsigned char *dst, unsigned char *src,
1454 unsigned int len)
wdenkc6097192002-11-03 00:24:07 +00001455{
wdenk18bd81e2004-03-17 01:13:07 +00001456 unsigned char *end, *last;
wdenkc6097192002-11-03 00:24:07 +00001457
wdenk18bd81e2004-03-17 01:13:07 +00001458 last = dst;
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001459 end = src + len - 1;
wdenk18bd81e2004-03-17 01:13:07 +00001460
1461 /* reserve space for '\0' */
1462 if (len < 2)
1463 goto OUT;
wdenkc12081a2004-03-23 20:18:25 +00001464
wdenk18bd81e2004-03-17 01:13:07 +00001465 /* skip leading white space */
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001466 while ((*src) && (src < end) && (*src == ' '))
wdenk18bd81e2004-03-17 01:13:07 +00001467 ++src;
1468
1469 /* copy string, omitting trailing white space */
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001470 while ((*src) && (src < end)) {
wdenk18bd81e2004-03-17 01:13:07 +00001471 *dst++ = *src;
1472 if (*src++ != ' ')
1473 last = dst;
wdenkc6097192002-11-03 00:24:07 +00001474 }
wdenk18bd81e2004-03-17 01:13:07 +00001475OUT:
1476 *last = '\0';
wdenkc6097192002-11-03 00:24:07 +00001477}
1478
1479/* ------------------------------------------------------------------------- */
1480
1481/*
1482 * Wait until Busy bit is off, or timeout (in ms)
1483 * Return last status
1484 */
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001485static uchar ide_wait(int dev, ulong t)
wdenkc6097192002-11-03 00:24:07 +00001486{
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001487 ulong delay = 10 * t; /* poll every 100 us */
wdenkc6097192002-11-03 00:24:07 +00001488 uchar c;
1489
wdenk591dda52002-11-18 00:14:45 +00001490 while ((c = ide_inb(dev, ATA_STATUS)) & ATA_STAT_BUSY) {
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001491 udelay(100);
1492 if (delay-- == 0)
wdenkc6097192002-11-03 00:24:07 +00001493 break;
wdenkc6097192002-11-03 00:24:07 +00001494 }
1495 return (c);
1496}
1497
1498/* ------------------------------------------------------------------------- */
1499
1500#ifdef CONFIG_IDE_RESET
1501extern void ide_set_reset(int idereset);
1502
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001503static void ide_reset(void)
wdenkc6097192002-11-03 00:24:07 +00001504{
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001505#if defined(CONFIG_SYS_PB_12V_ENABLE) || defined(CONFIG_SYS_PB_IDE_MOTOR)
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001506 volatile immap_t *immr = (immap_t *) CONFIG_SYS_IMMR;
wdenkc6097192002-11-03 00:24:07 +00001507#endif
1508 int i;
1509
1510 curr_device = -1;
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001511 for (i = 0; i < CONFIG_SYS_IDE_MAXBUS; ++i)
wdenkc6097192002-11-03 00:24:07 +00001512 ide_bus_ok[i] = 0;
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001513 for (i = 0; i < CONFIG_SYS_IDE_MAXDEVICE; ++i)
wdenkc6097192002-11-03 00:24:07 +00001514 ide_dev_desc[i].type = DEV_TYPE_UNKNOWN;
1515
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001516 ide_set_reset(1); /* assert reset */
wdenkc6097192002-11-03 00:24:07 +00001517
Martin Krause7e35caf2008-04-03 13:37:56 +02001518 /* the reset signal shall be asserted for et least 25 us */
1519 udelay(25);
1520
wdenkc6097192002-11-03 00:24:07 +00001521 WATCHDOG_RESET();
1522
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001523#ifdef CONFIG_SYS_PB_12V_ENABLE
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001524 /* 12V Enable output OFF */
1525 immr->im_cpm.cp_pbdat &= ~(CONFIG_SYS_PB_12V_ENABLE);
1526
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001527 immr->im_cpm.cp_pbpar &= ~(CONFIG_SYS_PB_12V_ENABLE);
1528 immr->im_cpm.cp_pbodr &= ~(CONFIG_SYS_PB_12V_ENABLE);
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001529 immr->im_cpm.cp_pbdir |= CONFIG_SYS_PB_12V_ENABLE;
wdenkc6097192002-11-03 00:24:07 +00001530
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001531 /* wait 500 ms for the voltage to stabilize */
1532 for (i = 0; i < 500; ++i)
1533 udelay(1000);
wdenkc6097192002-11-03 00:24:07 +00001534
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001535 /* 12V Enable output ON */
1536 immr->im_cpm.cp_pbdat |= CONFIG_SYS_PB_12V_ENABLE;
1537#endif /* CONFIG_SYS_PB_12V_ENABLE */
wdenkc6097192002-11-03 00:24:07 +00001538
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001539#ifdef CONFIG_SYS_PB_IDE_MOTOR
wdenkc6097192002-11-03 00:24:07 +00001540 /* configure IDE Motor voltage monitor pin as input */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001541 immr->im_cpm.cp_pbpar &= ~(CONFIG_SYS_PB_IDE_MOTOR);
1542 immr->im_cpm.cp_pbodr &= ~(CONFIG_SYS_PB_IDE_MOTOR);
1543 immr->im_cpm.cp_pbdir &= ~(CONFIG_SYS_PB_IDE_MOTOR);
wdenkc6097192002-11-03 00:24:07 +00001544
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001545 /* wait up to 1 s for the motor voltage to stabilize */
1546 for (i = 0; i < 1000; ++i) {
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001547 if ((immr->im_cpm.cp_pbdat & CONFIG_SYS_PB_IDE_MOTOR) != 0) {
wdenkc6097192002-11-03 00:24:07 +00001548 break;
1549 }
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001550 udelay(1000);
wdenkc6097192002-11-03 00:24:07 +00001551 }
1552
1553 if (i == 1000) { /* Timeout */
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001554 printf("\nWarning: 5V for IDE Motor missing\n");
1555#ifdef CONFIG_STATUS_LED
1556#ifdef STATUS_LED_YELLOW
1557 status_led_set(STATUS_LED_YELLOW, STATUS_LED_ON);
1558#endif
1559#ifdef STATUS_LED_GREEN
1560 status_led_set(STATUS_LED_GREEN, STATUS_LED_OFF);
1561#endif
1562#endif /* CONFIG_STATUS_LED */
wdenkc6097192002-11-03 00:24:07 +00001563 }
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001564#endif /* CONFIG_SYS_PB_IDE_MOTOR */
wdenkc6097192002-11-03 00:24:07 +00001565
1566 WATCHDOG_RESET();
1567
1568 /* de-assert RESET signal */
1569 ide_set_reset(0);
1570
1571 /* wait 250 ms */
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001572 for (i = 0; i < 250; ++i)
1573 udelay(1000);
wdenkc6097192002-11-03 00:24:07 +00001574}
1575
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001576#endif /* CONFIG_IDE_RESET */
wdenkc6097192002-11-03 00:24:07 +00001577
1578/* ------------------------------------------------------------------------- */
1579
wdenk54070ab2004-12-31 09:32:47 +00001580#if defined(CONFIG_IDE_LED) && \
wdenk54070ab2004-12-31 09:32:47 +00001581 !defined(CONFIG_CPC45) && \
wdenk54070ab2004-12-31 09:32:47 +00001582 !defined(CONFIG_KUP4K) && \
1583 !defined(CONFIG_KUP4X)
wdenkc6097192002-11-03 00:24:07 +00001584
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001585static uchar led_buffer; /* Buffer for current LED status */
wdenkc6097192002-11-03 00:24:07 +00001586
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001587static void ide_led(uchar led, uchar status)
wdenkc6097192002-11-03 00:24:07 +00001588{
1589 uchar *led_port = LED_PORT;
1590
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001591 if (status) /* switch LED on */
1592 led_buffer |= led;
1593 else /* switch LED off */
wdenkc6097192002-11-03 00:24:07 +00001594 led_buffer &= ~led;
wdenkc6097192002-11-03 00:24:07 +00001595
1596 *led_port = led_buffer;
1597}
1598
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001599#endif /* CONFIG_IDE_LED */
wdenkc6097192002-11-03 00:24:07 +00001600
Heiko Schocherffb293a2009-09-23 07:56:08 +02001601#if defined(CONFIG_OF_IDE_FIXUP)
1602int ide_device_present(int dev)
1603{
1604 if (dev >= CONFIG_SYS_IDE_MAXBUS)
1605 return 0;
1606 return (ide_dev_desc[dev].type == DEV_TYPE_UNKNOWN ? 0 : 1);
1607}
1608#endif
wdenkc6097192002-11-03 00:24:07 +00001609/* ------------------------------------------------------------------------- */
1610
1611#ifdef CONFIG_ATAPI
1612/****************************************************************************
1613 * ATAPI Support
1614 */
1615
Albert Aribaud036c6b42010-08-08 05:17:05 +05301616#if defined(CONFIG_IDE_SWAP_IO)
wdenkc6097192002-11-03 00:24:07 +00001617/* since ATAPI may use commands with not 4 bytes alligned length
1618 * we have our own transfer functions, 2 bytes alligned */
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001619static void output_data_shorts(int dev, ushort *sect_buf, int shorts)
wdenkc6097192002-11-03 00:24:07 +00001620{
Wolfgang Denkfb52e952010-09-19 21:28:25 +02001621#if defined(CONFIG_CPC45)
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001622 uchar *dbuf;
1623 volatile uchar *pbuf_even;
1624 volatile uchar *pbuf_odd;
wdenk2f99a692004-01-04 22:51:12 +00001625
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001626 pbuf_even = (uchar *) (ATA_CURR_BASE(dev) + ATA_DATA_EVEN);
1627 pbuf_odd = (uchar *) (ATA_CURR_BASE(dev) + ATA_DATA_ODD);
wdenk2f99a692004-01-04 22:51:12 +00001628 while (shorts--) {
wdenk20c98a62004-04-23 20:32:05 +00001629 EIEIO;
wdenk2f99a692004-01-04 22:51:12 +00001630 *pbuf_even = *dbuf++;
wdenk20c98a62004-04-23 20:32:05 +00001631 EIEIO;
wdenk2f99a692004-01-04 22:51:12 +00001632 *pbuf_odd = *dbuf++;
1633 }
wdenkf1276d42005-02-03 23:00:49 +00001634#else
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001635 ushort *dbuf;
1636 volatile ushort *pbuf;
wdenkc6097192002-11-03 00:24:07 +00001637
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001638 pbuf = (ushort *) (ATA_CURR_BASE(dev) + ATA_DATA_REG);
1639 dbuf = (ushort *) sect_buf;
wdenk634d2f72004-04-15 23:14:49 +00001640
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001641 debug("in output data shorts base for read is %lx\n",
1642 (unsigned long) pbuf);
wdenk634d2f72004-04-15 23:14:49 +00001643
wdenkc6097192002-11-03 00:24:07 +00001644 while (shorts--) {
wdenk20c98a62004-04-23 20:32:05 +00001645 EIEIO;
wdenkf1276d42005-02-03 23:00:49 +00001646 *pbuf = *dbuf++;
wdenkc6097192002-11-03 00:24:07 +00001647 }
wdenkf1276d42005-02-03 23:00:49 +00001648#endif
1649}
1650
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001651static void input_data_shorts(int dev, ushort *sect_buf, int shorts)
wdenkf1276d42005-02-03 23:00:49 +00001652{
Wolfgang Denkfb52e952010-09-19 21:28:25 +02001653#if defined(CONFIG_CPC45)
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001654 uchar *dbuf;
1655 volatile uchar *pbuf_even;
1656 volatile uchar *pbuf_odd;
wdenk2f99a692004-01-04 22:51:12 +00001657
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001658 pbuf_even = (uchar *) (ATA_CURR_BASE(dev) + ATA_DATA_EVEN);
1659 pbuf_odd = (uchar *) (ATA_CURR_BASE(dev) + ATA_DATA_ODD);
wdenk2f99a692004-01-04 22:51:12 +00001660 while (shorts--) {
wdenk20c98a62004-04-23 20:32:05 +00001661 EIEIO;
wdenk2f99a692004-01-04 22:51:12 +00001662 *dbuf++ = *pbuf_even;
wdenk20c98a62004-04-23 20:32:05 +00001663 EIEIO;
wdenk2f99a692004-01-04 22:51:12 +00001664 *dbuf++ = *pbuf_odd;
1665 }
wdenkf1276d42005-02-03 23:00:49 +00001666#else
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001667 ushort *dbuf;
1668 volatile ushort *pbuf;
wdenkf1276d42005-02-03 23:00:49 +00001669
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001670 pbuf = (ushort *) (ATA_CURR_BASE(dev) + ATA_DATA_REG);
1671 dbuf = (ushort *) sect_buf;
wdenkf1276d42005-02-03 23:00:49 +00001672
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001673 debug("in input data shorts base for read is %lx\n",
1674 (unsigned long) pbuf);
wdenkf1276d42005-02-03 23:00:49 +00001675
1676 while (shorts--) {
1677 EIEIO;
1678 *dbuf++ = *pbuf;
1679 }
1680#endif
wdenkc6097192002-11-03 00:24:07 +00001681}
1682
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001683#else /* ! CONFIG_IDE_SWAP_IO */
1684static void output_data_shorts(int dev, ushort *sect_buf, int shorts)
wdenk591dda52002-11-18 00:14:45 +00001685{
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001686 outsw(ATA_CURR_BASE(dev) + ATA_DATA_REG, sect_buf, shorts);
wdenk591dda52002-11-18 00:14:45 +00001687}
1688
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001689static void input_data_shorts(int dev, ushort *sect_buf, int shorts)
wdenk591dda52002-11-18 00:14:45 +00001690{
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001691 insw(ATA_CURR_BASE(dev) + ATA_DATA_REG, sect_buf, shorts);
wdenk591dda52002-11-18 00:14:45 +00001692}
1693
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001694#endif /* CONFIG_IDE_SWAP_IO */
wdenk591dda52002-11-18 00:14:45 +00001695
wdenkc6097192002-11-03 00:24:07 +00001696/*
1697 * Wait until (Status & mask) == res, or timeout (in ms)
1698 * Return last status
1699 * This is used since some ATAPI CD ROMs clears their Busy Bit first
1700 * and then they set their DRQ Bit
1701 */
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001702static uchar atapi_wait_mask(int dev, ulong t, uchar mask, uchar res)
wdenkc6097192002-11-03 00:24:07 +00001703{
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001704 ulong delay = 10 * t; /* poll every 100 us */
wdenkc6097192002-11-03 00:24:07 +00001705 uchar c;
1706
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001707 /* prevents to read the status before valid */
1708 c = ide_inb(dev, ATA_DEV_CTL);
1709
wdenk591dda52002-11-18 00:14:45 +00001710 while (((c = ide_inb(dev, ATA_STATUS)) & mask) != res) {
wdenkc6097192002-11-03 00:24:07 +00001711 /* break if error occurs (doesn't make sense to wait more) */
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001712 if ((c & ATA_STAT_ERR) == ATA_STAT_ERR)
wdenkc6097192002-11-03 00:24:07 +00001713 break;
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001714 udelay(100);
1715 if (delay-- == 0)
wdenkc6097192002-11-03 00:24:07 +00001716 break;
wdenkc6097192002-11-03 00:24:07 +00001717 }
1718 return (c);
1719}
1720
1721/*
1722 * issue an atapi command
1723 */
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001724unsigned char atapi_issue(int device, unsigned char *ccb, int ccblen,
1725 unsigned char *buffer, int buflen)
wdenkc6097192002-11-03 00:24:07 +00001726{
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001727 unsigned char c, err, mask, res;
wdenkc6097192002-11-03 00:24:07 +00001728 int n;
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001729
1730 ide_led(DEVICE_LED(device), 1); /* LED on */
wdenkc6097192002-11-03 00:24:07 +00001731
1732 /* Select device
1733 */
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001734 mask = ATA_STAT_BUSY | ATA_STAT_DRQ;
wdenkc6097192002-11-03 00:24:07 +00001735 res = 0;
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001736 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
1737 c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
wdenkc6097192002-11-03 00:24:07 +00001738 if ((c & mask) != res) {
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001739 printf("ATAPI_ISSUE: device %d not ready status %X\n", device,
1740 c);
1741 err = 0xFF;
wdenkc6097192002-11-03 00:24:07 +00001742 goto AI_OUT;
1743 }
1744 /* write taskfile */
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001745 ide_outb(device, ATA_ERROR_REG, 0); /* no DMA, no overlaped */
1746 ide_outb(device, ATA_SECT_CNT, 0);
1747 ide_outb(device, ATA_SECT_NUM, 0);
1748 ide_outb(device, ATA_CYL_LOW, (unsigned char) (buflen & 0xFF));
1749 ide_outb(device, ATA_CYL_HIGH,
1750 (unsigned char) ((buflen >> 8) & 0xFF));
1751 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
wdenkc6097192002-11-03 00:24:07 +00001752
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001753 ide_outb(device, ATA_COMMAND, ATAPI_CMD_PACKET);
1754 udelay(50);
wdenkc6097192002-11-03 00:24:07 +00001755
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001756 mask = ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR;
wdenkc6097192002-11-03 00:24:07 +00001757 res = ATA_STAT_DRQ;
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001758 c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
wdenkc6097192002-11-03 00:24:07 +00001759
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001760 if ((c & mask) != res) { /* DRQ must be 1, BSY 0 */
1761 printf("ATAPI_ISSUE: Error (no IRQ) before sending ccb dev %d status 0x%02x\n",
1762 device, c);
1763 err = 0xFF;
wdenkc6097192002-11-03 00:24:07 +00001764 goto AI_OUT;
1765 }
1766
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001767 /* write command block */
1768 output_data_shorts(device, (unsigned short *) ccb, ccblen / 2);
1769
Wolfgang Denka1be4762008-05-20 16:00:29 +02001770 /* ATAPI Command written wait for completition */
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001771 udelay(5000); /* device must set bsy */
wdenkc6097192002-11-03 00:24:07 +00001772
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001773 mask = ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR;
1774 /*
1775 * if no data wait for DRQ = 0 BSY = 0
1776 * if data wait for DRQ = 1 BSY = 0
1777 */
1778 res = 0;
1779 if (buflen)
wdenkc6097192002-11-03 00:24:07 +00001780 res = ATA_STAT_DRQ;
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001781 c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
1782 if ((c & mask) != res) {
wdenkc6097192002-11-03 00:24:07 +00001783 if (c & ATA_STAT_ERR) {
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001784 err = (ide_inb(device, ATA_ERROR_REG)) >> 4;
1785 debug("atapi_issue 1 returned sense key %X status %02X\n",
1786 err, c);
wdenkc6097192002-11-03 00:24:07 +00001787 } else {
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001788 printf("ATAPI_ISSUE: (no DRQ) after sending ccb (%x) status 0x%02x\n",
1789 ccb[0], c);
1790 err = 0xFF;
wdenkc6097192002-11-03 00:24:07 +00001791 }
1792 goto AI_OUT;
1793 }
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001794 n = ide_inb(device, ATA_CYL_HIGH);
1795 n <<= 8;
1796 n += ide_inb(device, ATA_CYL_LOW);
1797 if (n > buflen) {
1798 printf("ERROR, transfer bytes %d requested only %d\n", n,
1799 buflen);
1800 err = 0xff;
wdenkc6097192002-11-03 00:24:07 +00001801 goto AI_OUT;
1802 }
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001803 if ((n == 0) && (buflen < 0)) {
1804 printf("ERROR, transfer bytes %d requested %d\n", n, buflen);
1805 err = 0xff;
wdenkc6097192002-11-03 00:24:07 +00001806 goto AI_OUT;
1807 }
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001808 if (n != buflen) {
1809 debug("WARNING, transfer bytes %d not equal with requested %d\n",
1810 n, buflen);
wdenkc6097192002-11-03 00:24:07 +00001811 }
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001812 if (n != 0) { /* data transfer */
1813 debug("ATAPI_ISSUE: %d Bytes to transfer\n", n);
1814 /* we transfer shorts */
1815 n >>= 1;
wdenkc6097192002-11-03 00:24:07 +00001816 /* ok now decide if it is an in or output */
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001817 if ((ide_inb(device, ATA_SECT_CNT) & 0x02) == 0) {
1818 debug("Write to device\n");
1819 output_data_shorts(device, (unsigned short *) buffer,
1820 n);
wdenkc6097192002-11-03 00:24:07 +00001821 } else {
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001822 debug("Read from device @ %p shorts %d\n", buffer, n);
1823 input_data_shorts(device, (unsigned short *) buffer,
1824 n);
wdenkc6097192002-11-03 00:24:07 +00001825 }
1826 }
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001827 udelay(5000); /* seems that some CD ROMs need this... */
1828 mask = ATA_STAT_BUSY | ATA_STAT_ERR;
1829 res = 0;
1830 c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
wdenkc6097192002-11-03 00:24:07 +00001831 if ((c & ATA_STAT_ERR) == ATA_STAT_ERR) {
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001832 err = (ide_inb(device, ATA_ERROR_REG) >> 4);
1833 debug("atapi_issue 2 returned sense key %X status %X\n", err,
1834 c);
wdenkc6097192002-11-03 00:24:07 +00001835 } else {
1836 err = 0;
1837 }
1838AI_OUT:
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001839 ide_led(DEVICE_LED(device), 0); /* LED off */
wdenkc6097192002-11-03 00:24:07 +00001840 return (err);
1841}
1842
1843/*
1844 * sending the command to atapi_issue. If an status other than good
1845 * returns, an request_sense will be issued
1846 */
1847
Wolfgang Denka1be4762008-05-20 16:00:29 +02001848#define ATAPI_DRIVE_NOT_READY 100
wdenkc6097192002-11-03 00:24:07 +00001849#define ATAPI_UNIT_ATTN 10
1850
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001851unsigned char atapi_issue_autoreq(int device,
1852 unsigned char *ccb,
1853 int ccblen,
1854 unsigned char *buffer, int buflen)
wdenkc6097192002-11-03 00:24:07 +00001855{
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001856 unsigned char sense_data[18], sense_ccb[12];
1857 unsigned char res, key, asc, ascq;
1858 int notready, unitattn;
wdenkc6097192002-11-03 00:24:07 +00001859
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001860 unitattn = ATAPI_UNIT_ATTN;
1861 notready = ATAPI_DRIVE_NOT_READY;
wdenkc6097192002-11-03 00:24:07 +00001862
1863retry:
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001864 res = atapi_issue(device, ccb, ccblen, buffer, buflen);
1865 if (res == 0)
1866 return 0; /* Ok */
wdenkc6097192002-11-03 00:24:07 +00001867
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001868 if (res == 0xFF)
1869 return 0xFF; /* error */
wdenkc6097192002-11-03 00:24:07 +00001870
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001871 debug("(auto_req)atapi_issue returned sense key %X\n", res);
wdenkc6097192002-11-03 00:24:07 +00001872
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001873 memset(sense_ccb, 0, sizeof(sense_ccb));
1874 memset(sense_data, 0, sizeof(sense_data));
1875 sense_ccb[0] = ATAPI_CMD_REQ_SENSE;
1876 sense_ccb[4] = 18; /* allocation Length */
wdenkc6097192002-11-03 00:24:07 +00001877
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001878 res = atapi_issue(device, sense_ccb, 12, sense_data, 18);
1879 key = (sense_data[2] & 0xF);
1880 asc = (sense_data[12]);
1881 ascq = (sense_data[13]);
wdenkc6097192002-11-03 00:24:07 +00001882
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001883 debug("ATAPI_CMD_REQ_SENSE returned %x\n", res);
1884 debug(" Sense page: %02X key %02X ASC %02X ASCQ %02X\n",
1885 sense_data[0], key, asc, ascq);
wdenkc6097192002-11-03 00:24:07 +00001886
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001887 if ((key == 0))
1888 return 0; /* ok device ready */
wdenkc6097192002-11-03 00:24:07 +00001889
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001890 if ((key == 6) || (asc == 0x29) || (asc == 0x28)) { /* Unit Attention */
1891 if (unitattn-- > 0) {
1892 udelay(200 * 1000);
wdenkc6097192002-11-03 00:24:07 +00001893 goto retry;
1894 }
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001895 printf("Unit Attention, tried %d\n", ATAPI_UNIT_ATTN);
wdenkc6097192002-11-03 00:24:07 +00001896 goto error;
1897 }
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001898 if ((asc == 0x4) && (ascq == 0x1)) {
1899 /* not ready, but will be ready soon */
1900 if (notready-- > 0) {
1901 udelay(200 * 1000);
wdenkc6097192002-11-03 00:24:07 +00001902 goto retry;
1903 }
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001904 printf("Drive not ready, tried %d times\n",
1905 ATAPI_DRIVE_NOT_READY);
wdenkc6097192002-11-03 00:24:07 +00001906 goto error;
1907 }
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001908 if (asc == 0x3a) {
1909 debug("Media not present\n");
wdenkc6097192002-11-03 00:24:07 +00001910 goto error;
1911 }
wdenk452cfd62002-11-19 11:04:11 +00001912
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001913 printf("ERROR: Unknown Sense key %02X ASC %02X ASCQ %02X\n", key, asc,
1914 ascq);
wdenkc6097192002-11-03 00:24:07 +00001915error:
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001916 debug("ERROR Sense key %02X ASC %02X ASCQ %02X\n", key, asc, ascq);
wdenkc6097192002-11-03 00:24:07 +00001917 return (0xFF);
1918}
1919
1920
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001921static void atapi_inquiry(block_dev_desc_t *dev_desc)
wdenkc6097192002-11-03 00:24:07 +00001922{
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001923 unsigned char ccb[12]; /* Command descriptor block */
1924 unsigned char iobuf[64]; /* temp buf */
wdenkc6097192002-11-03 00:24:07 +00001925 unsigned char c;
1926 int device;
1927
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001928 device = dev_desc->dev;
1929 dev_desc->type = DEV_TYPE_UNKNOWN; /* not yet valid */
1930 dev_desc->block_read = atapi_read;
wdenkc6097192002-11-03 00:24:07 +00001931
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001932 memset(ccb, 0, sizeof(ccb));
1933 memset(iobuf, 0, sizeof(iobuf));
wdenkc6097192002-11-03 00:24:07 +00001934
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001935 ccb[0] = ATAPI_CMD_INQUIRY;
1936 ccb[4] = 40; /* allocation Legnth */
1937 c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *) iobuf, 40);
wdenkc6097192002-11-03 00:24:07 +00001938
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001939 debug("ATAPI_CMD_INQUIRY returned %x\n", c);
1940 if (c != 0)
wdenkc6097192002-11-03 00:24:07 +00001941 return;
1942
1943 /* copy device ident strings */
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001944 ident_cpy((unsigned char *) dev_desc->vendor, &iobuf[8], 8);
1945 ident_cpy((unsigned char *) dev_desc->product, &iobuf[16], 16);
1946 ident_cpy((unsigned char *) dev_desc->revision, &iobuf[32], 5);
wdenkc6097192002-11-03 00:24:07 +00001947
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001948 dev_desc->lun = 0;
1949 dev_desc->lba = 0;
1950 dev_desc->blksz = 0;
1951 dev_desc->type = iobuf[0] & 0x1f;
wdenkc6097192002-11-03 00:24:07 +00001952
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001953 if ((iobuf[1] & 0x80) == 0x80)
wdenkc6097192002-11-03 00:24:07 +00001954 dev_desc->removable = 1;
1955 else
1956 dev_desc->removable = 0;
1957
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001958 memset(ccb, 0, sizeof(ccb));
1959 memset(iobuf, 0, sizeof(iobuf));
1960 ccb[0] = ATAPI_CMD_START_STOP;
1961 ccb[4] = 0x03; /* start */
wdenkc6097192002-11-03 00:24:07 +00001962
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001963 c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *) iobuf, 0);
wdenkc6097192002-11-03 00:24:07 +00001964
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001965 debug("ATAPI_CMD_START_STOP returned %x\n", c);
1966 if (c != 0)
wdenkc6097192002-11-03 00:24:07 +00001967 return;
1968
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001969 memset(ccb, 0, sizeof(ccb));
1970 memset(iobuf, 0, sizeof(iobuf));
1971 c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *) iobuf, 0);
wdenkc6097192002-11-03 00:24:07 +00001972
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001973 debug("ATAPI_CMD_UNIT_TEST_READY returned %x\n", c);
1974 if (c != 0)
wdenkc6097192002-11-03 00:24:07 +00001975 return;
1976
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001977 memset(ccb, 0, sizeof(ccb));
1978 memset(iobuf, 0, sizeof(iobuf));
1979 ccb[0] = ATAPI_CMD_READ_CAP;
1980 c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *) iobuf, 8);
1981 debug("ATAPI_CMD_READ_CAP returned %x\n", c);
1982 if (c != 0)
wdenkc6097192002-11-03 00:24:07 +00001983 return;
1984
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001985 debug("Read Cap: LBA %02X%02X%02X%02X blksize %02X%02X%02X%02X\n",
1986 iobuf[0], iobuf[1], iobuf[2], iobuf[3],
1987 iobuf[4], iobuf[5], iobuf[6], iobuf[7]);
wdenkc6097192002-11-03 00:24:07 +00001988
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001989 dev_desc->lba = ((unsigned long) iobuf[0] << 24) +
1990 ((unsigned long) iobuf[1] << 16) +
1991 ((unsigned long) iobuf[2] << 8) + ((unsigned long) iobuf[3]);
1992 dev_desc->blksz = ((unsigned long) iobuf[4] << 24) +
1993 ((unsigned long) iobuf[5] << 16) +
1994 ((unsigned long) iobuf[6] << 8) + ((unsigned long) iobuf[7]);
wdenkc35ba4e2004-03-14 22:25:36 +00001995#ifdef CONFIG_LBA48
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001996 /* ATAPI devices cannot use 48bit addressing (ATA/ATAPI v7) */
1997 dev_desc->lba48 = 0;
wdenkc35ba4e2004-03-14 22:25:36 +00001998#endif
wdenkc6097192002-11-03 00:24:07 +00001999 return;
2000}
2001
2002
2003/*
2004 * atapi_read:
2005 * we transfer only one block per command, since the multiple DRQ per
2006 * command is not yet implemented
2007 */
2008#define ATAPI_READ_MAX_BYTES 2048 /* we read max 2kbytes */
2009#define ATAPI_READ_BLOCK_SIZE 2048 /* assuming CD part */
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00002010#define ATAPI_READ_MAX_BLOCK (ATAPI_READ_MAX_BYTES/ATAPI_READ_BLOCK_SIZE)
wdenkc6097192002-11-03 00:24:07 +00002011
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00002012ulong atapi_read(int device, lbaint_t blknr, ulong blkcnt, void *buffer)
wdenkc6097192002-11-03 00:24:07 +00002013{
2014 ulong n = 0;
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00002015 unsigned char ccb[12]; /* Command descriptor block */
wdenkc6097192002-11-03 00:24:07 +00002016 ulong cnt;
2017
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00002018 debug("atapi_read dev %d start %lX, blocks %lX buffer at %lX\n",
2019 device, blknr, blkcnt, (ulong) buffer);
wdenkc6097192002-11-03 00:24:07 +00002020
2021 do {
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00002022 if (blkcnt > ATAPI_READ_MAX_BLOCK)
2023 cnt = ATAPI_READ_MAX_BLOCK;
2024 else
2025 cnt = blkcnt;
wdenkc6097192002-11-03 00:24:07 +00002026
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00002027 ccb[0] = ATAPI_CMD_READ_12;
2028 ccb[1] = 0; /* reserved */
2029 ccb[2] = (unsigned char) (blknr >> 24) & 0xFF; /* MSB Block */
2030 ccb[3] = (unsigned char) (blknr >> 16) & 0xFF; /* */
2031 ccb[4] = (unsigned char) (blknr >> 8) & 0xFF;
2032 ccb[5] = (unsigned char) blknr & 0xFF; /* LSB Block */
2033 ccb[6] = (unsigned char) (cnt >> 24) & 0xFF; /* MSB Block cnt */
2034 ccb[7] = (unsigned char) (cnt >> 16) & 0xFF;
2035 ccb[8] = (unsigned char) (cnt >> 8) & 0xFF;
2036 ccb[9] = (unsigned char) cnt & 0xFF; /* LSB Block */
2037 ccb[10] = 0; /* reserved */
2038 ccb[11] = 0; /* reserved */
2039
2040 if (atapi_issue_autoreq(device, ccb, 12,
2041 (unsigned char *) buffer,
2042 cnt * ATAPI_READ_BLOCK_SIZE)
2043 == 0xFF) {
wdenkc6097192002-11-03 00:24:07 +00002044 return (n);
2045 }
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00002046 n += cnt;
2047 blkcnt -= cnt;
2048 blknr += cnt;
2049 buffer += (cnt * ATAPI_READ_BLOCK_SIZE);
wdenkc6097192002-11-03 00:24:07 +00002050 } while (blkcnt > 0);
2051 return (n);
2052}
2053
2054/* ------------------------------------------------------------------------- */
2055
2056#endif /* CONFIG_ATAPI */
2057
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00002058U_BOOT_CMD(ide, 5, 1, do_ide,
2059 "IDE sub-system",
2060 "reset - reset IDE controller\n"
2061 "ide info - show available IDE devices\n"
2062 "ide device [dev] - show or set current device\n"
2063 "ide part [dev] - print partition table of one or all IDE devices\n"
2064 "ide read addr blk# cnt\n"
2065 "ide write addr blk# cnt - read/write `cnt'"
2066 " blocks starting at block `blk#'\n"
2067 " to/from memory address `addr'");
wdenk57b2d802003-06-27 21:31:46 +00002068
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00002069U_BOOT_CMD(diskboot, 3, 1, do_diskboot,
2070 "boot from IDE device", "loadAddr dev:part");