blob: 1fc03777ebbd4872ce4f97152d55e509ecb9ae38 [file] [log] [blame]
wdenkc6097192002-11-03 00:24:07 +00001/*
wdenkf1276d42005-02-03 23:00:49 +00002 * (C) Copyright 2000-2005
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
wdenkc6097192002-11-03 00:24:07 +0000122
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200123static int ide_bus_ok[CONFIG_SYS_IDE_MAXBUS];
wdenkc6097192002-11-03 00:24:07 +0000124
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200125block_dev_desc_t ide_dev_desc[CONFIG_SYS_IDE_MAXDEVICE];
wdenkc6097192002-11-03 00:24:07 +0000126/* ------------------------------------------------------------------------- */
127
128#ifdef CONFIG_IDE_LED
Wolfgang Denkb0b104a2010-06-13 18:28:54 +0200129# if !defined(CONFIG_BMS2003) && \
130 !defined(CONFIG_CPC45) && \
131 !defined(CONFIG_KUP4K) && \
132 !defined(CONFIG_KUP4X)
wdenkc6097192002-11-03 00:24:07 +0000133static void ide_led (uchar led, uchar status);
134#else
wdenk90e7e422002-12-04 23:39:58 +0000135extern void ide_led (uchar led, uchar status);
136#endif
137#else
wdenkc6097192002-11-03 00:24:07 +0000138#define ide_led(a,b) /* dummy */
139#endif
140
141#ifdef CONFIG_IDE_RESET
142static void ide_reset (void);
143#else
144#define ide_reset() /* dummy */
145#endif
146
147static void ide_ident (block_dev_desc_t *dev_desc);
148static uchar ide_wait (int dev, ulong t);
149
150#define IDE_TIME_OUT 2000 /* 2 sec timeout */
151
152#define ATAPI_TIME_OUT 7000 /* 7 sec timeout (5 sec seems to work...) */
153
154#define IDE_SPIN_UP_TIME_OUT 5000 /* 5 sec spin-up timeout */
155
wdenkc6097192002-11-03 00:24:07 +0000156static void input_data(int dev, ulong *sect_buf, int words);
Wolfgang Denk7fafed22011-04-30 23:29:55 +0200157static void output_data(int dev, const ulong *sect_buf, int words);
wdenkc6097192002-11-03 00:24:07 +0000158static void ident_cpy (unsigned char *dest, unsigned char *src, unsigned int len);
159
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200160#ifndef CONFIG_SYS_ATA_PORT_ADDR
161#define CONFIG_SYS_ATA_PORT_ADDR(port) (port)
Heiko Schocher633e03a2007-06-22 19:11:54 +0200162#endif
wdenkc6097192002-11-03 00:24:07 +0000163
164#ifdef CONFIG_ATAPI
165static void atapi_inquiry(block_dev_desc_t *dev_desc);
Grant Likely2089cab2007-02-20 09:05:45 +0100166ulong atapi_read (int device, lbaint_t blknr, ulong blkcnt, void *buffer);
wdenkc6097192002-11-03 00:24:07 +0000167#endif
168
169
170#ifdef CONFIG_IDE_8xx_DIRECT
171static void set_pcmcia_timing (int pmode);
wdenkc6097192002-11-03 00:24:07 +0000172#endif
173
174/* ------------------------------------------------------------------------- */
175
Wolfgang Denk6262d0212010-06-28 22:00:46 +0200176int do_ide (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenkc6097192002-11-03 00:24:07 +0000177{
178 int rcode = 0;
179
180 switch (argc) {
181 case 0:
182 case 1:
Wolfgang Denk3b683112010-07-17 01:06:04 +0200183 return cmd_usage(cmdtp);
wdenkc6097192002-11-03 00:24:07 +0000184 case 2:
185 if (strncmp(argv[1],"res",3) == 0) {
186 puts ("\nReset IDE"
187#ifdef CONFIG_IDE_8xx_DIRECT
188 " on PCMCIA " PCMCIA_SLOT_MSG
189#endif
190 ": ");
191
192 ide_init ();
193 return 0;
194 } else if (strncmp(argv[1],"inf",3) == 0) {
195 int i;
196
197 putc ('\n');
198
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200199 for (i=0; i<CONFIG_SYS_IDE_MAXDEVICE; ++i) {
wdenkc6097192002-11-03 00:24:07 +0000200 if (ide_dev_desc[i].type==DEV_TYPE_UNKNOWN)
201 continue; /* list only known devices */
202 printf ("IDE device %d: ", i);
203 dev_print(&ide_dev_desc[i]);
204 }
205 return 0;
206
207 } else if (strncmp(argv[1],"dev",3) == 0) {
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200208 if ((curr_device < 0) || (curr_device >= CONFIG_SYS_IDE_MAXDEVICE)) {
wdenkc6097192002-11-03 00:24:07 +0000209 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;
217
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200218 for (ok=0, dev=0; dev<CONFIG_SYS_IDE_MAXDEVICE; ++dev) {
wdenkc6097192002-11-03 00:24:07 +0000219 if (ide_dev_desc[dev].part_type!=PART_TYPE_UNKNOWN) {
220 ++ok;
221 if (dev)
222 putc ('\n');
223 print_part(&ide_dev_desc[dev]);
224 }
225 }
226 if (!ok) {
227 puts ("\nno IDE devices available\n");
228 rcode ++;
229 }
230 return rcode;
231 }
Wolfgang Denk3b683112010-07-17 01:06:04 +0200232 return cmd_usage(cmdtp);
wdenkc6097192002-11-03 00:24:07 +0000233 case 3:
234 if (strncmp(argv[1],"dev",3) == 0) {
235 int dev = (int)simple_strtoul(argv[2], NULL, 10);
236
237 printf ("\nIDE device %d: ", dev);
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200238 if (dev >= CONFIG_SYS_IDE_MAXDEVICE) {
wdenkc6097192002-11-03 00:24:07 +0000239 puts ("unknown device\n");
240 return 1;
241 }
242 dev_print(&ide_dev_desc[dev]);
243 /*ide_print (dev);*/
244
245 if (ide_dev_desc[dev].type == DEV_TYPE_UNKNOWN) {
246 return 1;
247 }
248
249 curr_device = dev;
250
251 puts ("... is now current device\n");
252
253 return 0;
254 } else if (strncmp(argv[1],"part",4) == 0) {
255 int dev = (int)simple_strtoul(argv[2], NULL, 10);
256
257 if (ide_dev_desc[dev].part_type!=PART_TYPE_UNKNOWN) {
258 print_part(&ide_dev_desc[dev]);
259 } else {
260 printf ("\nIDE device %d not available\n", dev);
261 rcode = 1;
262 }
263 return rcode;
264#if 0
265 } else if (strncmp(argv[1],"pio",4) == 0) {
266 int mode = (int)simple_strtoul(argv[2], NULL, 10);
267
268 if ((mode >= 0) && (mode <= IDE_MAX_PIO_MODE)) {
269 puts ("\nSetting ");
270 pio_mode = mode;
271 ide_init ();
272 } else {
273 printf ("\nInvalid PIO mode %d (0 ... %d only)\n",
274 mode, IDE_MAX_PIO_MODE);
275 }
276 return;
277#endif
278 }
279
Wolfgang Denk3b683112010-07-17 01:06:04 +0200280 return cmd_usage(cmdtp);
wdenkc6097192002-11-03 00:24:07 +0000281 default:
282 /* at least 4 args */
283
284 if (strcmp(argv[1],"read") == 0) {
285 ulong addr = simple_strtoul(argv[2], NULL, 16);
wdenkc6097192002-11-03 00:24:07 +0000286 ulong cnt = simple_strtoul(argv[4], NULL, 16);
287 ulong n;
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200288#ifdef CONFIG_SYS_64BIT_LBA
wdenkc35ba4e2004-03-14 22:25:36 +0000289 lbaint_t blk = simple_strtoull(argv[3], NULL, 16);
wdenkc6097192002-11-03 00:24:07 +0000290
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500291 printf ("\nIDE read: device %d block # %Ld, count %ld ... ",
wdenkc6097192002-11-03 00:24:07 +0000292 curr_device, blk, cnt);
wdenkc35ba4e2004-03-14 22:25:36 +0000293#else
294 lbaint_t blk = simple_strtoul(argv[3], NULL, 16);
295
296 printf ("\nIDE read: device %d block # %ld, count %ld ... ",
297 curr_device, blk, cnt);
298#endif
wdenkc6097192002-11-03 00:24:07 +0000299
300 n = ide_dev_desc[curr_device].block_read (curr_device,
301 blk, cnt,
302 (ulong *)addr);
303 /* flush cache after read */
304 flush_cache (addr, cnt*ide_dev_desc[curr_device].blksz);
305
306 printf ("%ld blocks read: %s\n",
307 n, (n==cnt) ? "OK" : "ERROR");
308 if (n==cnt) {
309 return 0;
310 } else {
311 return 1;
312 }
313 } else if (strcmp(argv[1],"write") == 0) {
314 ulong addr = simple_strtoul(argv[2], NULL, 16);
wdenkc6097192002-11-03 00:24:07 +0000315 ulong cnt = simple_strtoul(argv[4], NULL, 16);
316 ulong n;
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200317#ifdef CONFIG_SYS_64BIT_LBA
wdenkc35ba4e2004-03-14 22:25:36 +0000318 lbaint_t blk = simple_strtoull(argv[3], NULL, 16);
wdenkc6097192002-11-03 00:24:07 +0000319
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500320 printf ("\nIDE write: device %d block # %Ld, count %ld ... ",
wdenkc6097192002-11-03 00:24:07 +0000321 curr_device, blk, cnt);
wdenkc35ba4e2004-03-14 22:25:36 +0000322#else
323 lbaint_t blk = simple_strtoul(argv[3], NULL, 16);
324
325 printf ("\nIDE write: device %d block # %ld, count %ld ... ",
326 curr_device, blk, cnt);
327#endif
wdenkc6097192002-11-03 00:24:07 +0000328
329 n = ide_write (curr_device, blk, cnt, (ulong *)addr);
330
331 printf ("%ld blocks written: %s\n",
332 n, (n==cnt) ? "OK" : "ERROR");
Wolfgang Denk3b683112010-07-17 01:06:04 +0200333 if (n==cnt)
wdenkc6097192002-11-03 00:24:07 +0000334 return 0;
Wolfgang Denk3b683112010-07-17 01:06:04 +0200335 else
wdenkc6097192002-11-03 00:24:07 +0000336 return 1;
wdenkc6097192002-11-03 00:24:07 +0000337 } else {
Wolfgang Denk3b683112010-07-17 01:06:04 +0200338 return cmd_usage(cmdtp);
wdenkc6097192002-11-03 00:24:07 +0000339 }
340
341 return rcode;
342 }
343}
344
Wolfgang Denk6262d0212010-06-28 22:00:46 +0200345int do_diskboot (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenkc6097192002-11-03 00:24:07 +0000346{
347 char *boot_device = NULL;
348 char *ep;
349 int dev, part = 0;
Marian Balakowicz41d71ed2008-01-08 18:14:09 +0100350 ulong addr, cnt;
wdenkc6097192002-11-03 00:24:07 +0000351 disk_partition_t info;
352 image_header_t *hdr;
Marian Balakowicze6bdd342008-03-12 10:33:01 +0100353#if defined(CONFIG_FIT)
Marian Balakowiczcefb4022008-06-06 23:07:40 +0200354 const void *fit_hdr = NULL;
Marian Balakowicze6bdd342008-03-12 10:33:01 +0100355#endif
wdenkc6097192002-11-03 00:24:07 +0000356
Heiko Schocher8a8ec532007-07-13 09:54:17 +0200357 show_boot_progress (41);
wdenkc6097192002-11-03 00:24:07 +0000358 switch (argc) {
359 case 1:
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200360 addr = CONFIG_SYS_LOAD_ADDR;
wdenkc6097192002-11-03 00:24:07 +0000361 boot_device = getenv ("bootdevice");
362 break;
363 case 2:
364 addr = simple_strtoul(argv[1], NULL, 16);
365 boot_device = getenv ("bootdevice");
366 break;
367 case 3:
368 addr = simple_strtoul(argv[1], NULL, 16);
369 boot_device = argv[2];
370 break;
371 default:
Heiko Schocher8a8ec532007-07-13 09:54:17 +0200372 show_boot_progress (-42);
Wolfgang Denk3b683112010-07-17 01:06:04 +0200373 return cmd_usage(cmdtp);
wdenkc6097192002-11-03 00:24:07 +0000374 }
Heiko Schocher8a8ec532007-07-13 09:54:17 +0200375 show_boot_progress (42);
wdenkc6097192002-11-03 00:24:07 +0000376
377 if (!boot_device) {
378 puts ("\n** No boot device **\n");
Heiko Schocher8a8ec532007-07-13 09:54:17 +0200379 show_boot_progress (-43);
wdenkc6097192002-11-03 00:24:07 +0000380 return 1;
381 }
Heiko Schocher8a8ec532007-07-13 09:54:17 +0200382 show_boot_progress (43);
wdenkc6097192002-11-03 00:24:07 +0000383
384 dev = simple_strtoul(boot_device, &ep, 16);
385
386 if (ide_dev_desc[dev].type==DEV_TYPE_UNKNOWN) {
387 printf ("\n** Device %d not available\n", dev);
Heiko Schocher8a8ec532007-07-13 09:54:17 +0200388 show_boot_progress (-44);
wdenkc6097192002-11-03 00:24:07 +0000389 return 1;
390 }
Heiko Schocher8a8ec532007-07-13 09:54:17 +0200391 show_boot_progress (44);
wdenkc6097192002-11-03 00:24:07 +0000392
393 if (*ep) {
394 if (*ep != ':') {
395 puts ("\n** Invalid boot device, use `dev[:part]' **\n");
Heiko Schocher8a8ec532007-07-13 09:54:17 +0200396 show_boot_progress (-45);
wdenkc6097192002-11-03 00:24:07 +0000397 return 1;
398 }
399 part = simple_strtoul(++ep, NULL, 16);
400 }
Heiko Schocher8a8ec532007-07-13 09:54:17 +0200401 show_boot_progress (45);
Denis Peter3957bc12007-04-13 09:13:33 +0200402 if (get_partition_info (&ide_dev_desc[dev], part, &info)) {
Heiko Schocher8a8ec532007-07-13 09:54:17 +0200403 show_boot_progress (-46);
wdenkc6097192002-11-03 00:24:07 +0000404 return 1;
405 }
Heiko Schocher8a8ec532007-07-13 09:54:17 +0200406 show_boot_progress (46);
Wolfgang Denk7fb52662005-10-13 16:45:02 +0200407 if ((strncmp((char *)info.type, BOOT_PART_TYPE, sizeof(info.type)) != 0) &&
408 (strncmp((char *)info.type, BOOT_PART_COMP, sizeof(info.type)) != 0)) {
wdenkc6097192002-11-03 00:24:07 +0000409 printf ("\n** Invalid partition type \"%.32s\""
410 " (expect \"" BOOT_PART_TYPE "\")\n",
411 info.type);
Heiko Schocher8a8ec532007-07-13 09:54:17 +0200412 show_boot_progress (-47);
wdenkc6097192002-11-03 00:24:07 +0000413 return 1;
414 }
Heiko Schocher8a8ec532007-07-13 09:54:17 +0200415 show_boot_progress (47);
wdenkc6097192002-11-03 00:24:07 +0000416
417 printf ("\nLoading from IDE device %d, partition %d: "
418 "Name: %.32s Type: %.32s\n",
419 dev, part, info.name, info.type);
420
wdenkf1276d42005-02-03 23:00:49 +0000421 debug ("First Block: %ld, # of blocks: %ld, Block Size: %ld\n",
wdenkc6097192002-11-03 00:24:07 +0000422 info.start, info.size, info.blksz);
423
424 if (ide_dev_desc[dev].block_read (dev, info.start, 1, (ulong *)addr) != 1) {
425 printf ("** Read error on %d:%d\n", dev, part);
Heiko Schocher8a8ec532007-07-13 09:54:17 +0200426 show_boot_progress (-48);
wdenkc6097192002-11-03 00:24:07 +0000427 return 1;
428 }
Heiko Schocher8a8ec532007-07-13 09:54:17 +0200429 show_boot_progress (48);
wdenkc6097192002-11-03 00:24:07 +0000430
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100431 switch (genimg_get_format ((void *)addr)) {
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100432 case IMAGE_FORMAT_LEGACY:
433 hdr = (image_header_t *)addr;
wdenkc6097192002-11-03 00:24:07 +0000434
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100435 show_boot_progress (49);
436
437 if (!image_check_hcrc (hdr)) {
438 puts ("\n** Bad Header Checksum **\n");
439 show_boot_progress (-50);
440 return 1;
441 }
442 show_boot_progress (50);
wdenkc6097192002-11-03 00:24:07 +0000443
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100444 image_print_contents (hdr);
445
446 cnt = image_get_image_size (hdr);
447 break;
448#if defined(CONFIG_FIT)
449 case IMAGE_FORMAT_FIT:
Marian Balakowicze6bdd342008-03-12 10:33:01 +0100450 fit_hdr = (const void *)addr;
Marian Balakowicze6bdd342008-03-12 10:33:01 +0100451 puts ("Fit image detected...\n");
452
453 cnt = fit_get_size (fit_hdr);
454 break;
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100455#endif
456 default:
Marian Balakowicze6bdd342008-03-12 10:33:01 +0100457 show_boot_progress (-49);
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100458 puts ("** Unknown image type\n");
wdenkf1276d42005-02-03 23:00:49 +0000459 return 1;
460 }
wdenkf1276d42005-02-03 23:00:49 +0000461
wdenkf1276d42005-02-03 23:00:49 +0000462 cnt += info.blksz - 1;
463 cnt /= info.blksz;
464 cnt -= 1;
465
wdenkc6097192002-11-03 00:24:07 +0000466 if (ide_dev_desc[dev].block_read (dev, info.start+1, cnt,
467 (ulong *)(addr+info.blksz)) != cnt) {
468 printf ("** Read error on %d:%d\n", dev, part);
Heiko Schocher8a8ec532007-07-13 09:54:17 +0200469 show_boot_progress (-51);
wdenkc6097192002-11-03 00:24:07 +0000470 return 1;
471 }
Heiko Schocher8a8ec532007-07-13 09:54:17 +0200472 show_boot_progress (51);
wdenkc6097192002-11-03 00:24:07 +0000473
Marian Balakowicze6bdd342008-03-12 10:33:01 +0100474#if defined(CONFIG_FIT)
475 /* This cannot be done earlier, we need complete FIT image in RAM first */
Marian Balakowiczcefb4022008-06-06 23:07:40 +0200476 if (genimg_get_format ((void *)addr) == IMAGE_FORMAT_FIT) {
477 if (!fit_check_format (fit_hdr)) {
478 show_boot_progress (-140);
479 puts ("** Bad FIT image format\n");
480 return 1;
481 }
482 show_boot_progress (141);
483 fit_print_contents (fit_hdr);
484 }
Marian Balakowicze6bdd342008-03-12 10:33:01 +0100485#endif
wdenkc6097192002-11-03 00:24:07 +0000486
487 /* Loading ok, update default load address */
488
489 load_addr = addr;
490
Mike Frysinger194c2e82011-06-05 13:43:02 +0000491 return bootm_maybe_autostart(cmdtp, argv[0]);
wdenkc6097192002-11-03 00:24:07 +0000492}
493
494/* ------------------------------------------------------------------------- */
495
Stefan Roese37628252008-08-06 14:05:38 +0200496void inline
497__ide_outb(int dev, int port, unsigned char val)
498{
499 debug ("ide_outb (dev= %d, port= 0x%x, val= 0x%02x) : @ 0x%08lx\n",
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200500 dev, port, val, (ATA_CURR_BASE(dev)+CONFIG_SYS_ATA_PORT_ADDR(port)));
Macpaul Lind1e49942011-04-11 20:45:32 +0000501
502#if defined(CONFIG_IDE_AHB)
503 if (port) {
504 /* write command */
505 ide_write_register(dev, port, val);
506 } else {
507 /* write data */
508 outb(val, (ATA_CURR_BASE(dev)));
509 }
510#else
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200511 outb(val, (ATA_CURR_BASE(dev)+CONFIG_SYS_ATA_PORT_ADDR(port)));
Macpaul Lind1e49942011-04-11 20:45:32 +0000512#endif
Stefan Roese37628252008-08-06 14:05:38 +0200513}
Macpaul Lind1e49942011-04-11 20:45:32 +0000514
Kim Phillips8167f3c2009-05-19 12:53:36 -0500515void ide_outb (int dev, int port, unsigned char val)
Stefan Roese37628252008-08-06 14:05:38 +0200516 __attribute__((weak, alias("__ide_outb")));
517
518unsigned char inline
519__ide_inb(int dev, int port)
520{
521 uchar val;
Macpaul Lind1e49942011-04-11 20:45:32 +0000522
523#if defined(CONFIG_IDE_AHB)
524 val = ide_read_register(dev, port);
525#else
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200526 val = inb((ATA_CURR_BASE(dev)+CONFIG_SYS_ATA_PORT_ADDR(port)));
Macpaul Lind1e49942011-04-11 20:45:32 +0000527#endif
528
Stefan Roese37628252008-08-06 14:05:38 +0200529 debug ("ide_inb (dev= %d, port= 0x%x) : @ 0x%08lx -> 0x%02x\n",
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200530 dev, port, (ATA_CURR_BASE(dev)+CONFIG_SYS_ATA_PORT_ADDR(port)), val);
Stefan Roese37628252008-08-06 14:05:38 +0200531 return val;
532}
Kim Phillips8167f3c2009-05-19 12:53:36 -0500533unsigned char ide_inb(int dev, int port)
Stefan Roese37628252008-08-06 14:05:38 +0200534 __attribute__((weak, alias("__ide_inb")));
535
Steven A. Falco2b7574e2008-08-15 15:34:10 -0400536#ifdef CONFIG_TUNE_PIO
537int inline
538__ide_set_piomode(int pio_mode)
539{
540 return 0;
541}
542int inline ide_set_piomode(int pio_mode)
543 __attribute__((weak, alias("__ide_set_piomode")));
544#endif
545
wdenkc6097192002-11-03 00:24:07 +0000546void ide_init (void)
547{
wdenkc6097192002-11-03 00:24:07 +0000548
549#ifdef CONFIG_IDE_8xx_DIRECT
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200550 volatile immap_t *immr = (immap_t *)CONFIG_SYS_IMMR;
wdenkc6097192002-11-03 00:24:07 +0000551 volatile pcmconf8xx_t *pcmp = &(immr->im_pcmcia);
552#endif
553 unsigned char c;
554 int i, bus;
Wolfgang Denkb0b104a2010-06-13 18:28:54 +0200555#if defined(CONFIG_SC3)
Wolfgang Denkd2a7a742007-06-08 10:24:58 +0200556 unsigned int ata_reset_time = ATA_RESET_TIME;
Wolfgang Denk15c6b152007-04-11 17:22:55 +0200557#endif
wdenk7f00b942003-12-07 23:55:12 +0000558#ifdef CONFIG_IDE_8xx_PCCARD
559 extern int pcmcia_on (void);
560 extern int ide_devices_found; /* Initialized in check_ide_device() */
561#endif /* CONFIG_IDE_8xx_PCCARD */
562
563#ifdef CONFIG_IDE_PREINIT
wdenk369d43d2004-03-14 14:09:05 +0000564 extern int ide_preinit (void);
wdenk7f00b942003-12-07 23:55:12 +0000565 WATCHDOG_RESET();
566
567 if (ide_preinit ()) {
568 puts ("ide_preinit failed\n");
569 return;
570 }
571#endif /* CONFIG_IDE_PREINIT */
wdenkc6097192002-11-03 00:24:07 +0000572
573#ifdef CONFIG_IDE_8xx_PCCARD
574 extern int pcmcia_on (void);
wdenk4fc95692003-02-28 00:49:47 +0000575 extern int ide_devices_found; /* Initialized in check_ide_device() */
wdenkc6097192002-11-03 00:24:07 +0000576
577 WATCHDOG_RESET();
578
wdenk4fc95692003-02-28 00:49:47 +0000579 ide_devices_found = 0;
wdenkc6097192002-11-03 00:24:07 +0000580 /* initialize the PCMCIA IDE adapter card */
wdenk4fc95692003-02-28 00:49:47 +0000581 pcmcia_on();
582 if (!ide_devices_found)
wdenkc6097192002-11-03 00:24:07 +0000583 return;
584 udelay (1000000); /* 1 s */
585#endif /* CONFIG_IDE_8xx_PCCARD */
586
587 WATCHDOG_RESET();
588
wdenk9f837932003-10-09 19:00:25 +0000589#ifdef CONFIG_IDE_8xx_DIRECT
wdenkc6097192002-11-03 00:24:07 +0000590 /* Initialize PIO timing tables */
591 for (i=0; i <= IDE_MAX_PIO_MODE; ++i) {
wdenkf1276d42005-02-03 23:00:49 +0000592 pio_config_clk[i].t_setup = PCMCIA_MK_CLKS(pio_config_ns[i].t_setup,
593 gd->bus_clk);
594 pio_config_clk[i].t_length = PCMCIA_MK_CLKS(pio_config_ns[i].t_length,
595 gd->bus_clk);
596 pio_config_clk[i].t_hold = PCMCIA_MK_CLKS(pio_config_ns[i].t_hold,
597 gd->bus_clk);
598 debug ( "PIO Mode %d: setup=%2d ns/%d clk"
599 " len=%3d ns/%d clk"
600 " hold=%2d ns/%d clk\n",
601 i,
602 pio_config_ns[i].t_setup, pio_config_clk[i].t_setup,
603 pio_config_ns[i].t_length, pio_config_clk[i].t_length,
604 pio_config_ns[i].t_hold, pio_config_clk[i].t_hold);
wdenkc6097192002-11-03 00:24:07 +0000605 }
wdenk9f837932003-10-09 19:00:25 +0000606#endif /* CONFIG_IDE_8xx_DIRECT */
wdenkc6097192002-11-03 00:24:07 +0000607
608 /* Reset the IDE just to be sure.
609 * Light LED's to show
610 */
611 ide_led ((LED_IDE1 | LED_IDE2), 1); /* LED's on */
612 ide_reset (); /* ATAPI Drives seems to need a proper IDE Reset */
613
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;
620 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 */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200627 for (bus=0; bus<CONFIG_SYS_IDE_MAXBUS; ++bus) {
628 int dev = bus * (CONFIG_SYS_IDE_MAXDEVICE / CONFIG_SYS_IDE_MAXBUS);
wdenkc6097192002-11-03 00:24:07 +0000629
wdenk4fc95692003-02-28 00:49:47 +0000630#ifdef CONFIG_IDE_8xx_PCCARD
631 /* Skip non-ide devices from probing */
632 if ((ide_devices_found & (1 << bus)) == 0) {
633 ide_led ((LED_IDE1 | LED_IDE2), 0); /* LED's off */
634 continue;
635 }
636#endif
wdenkc6097192002-11-03 00:24:07 +0000637 printf ("Bus %d: ", bus);
638
639 ide_bus_ok[bus] = 0;
640
641 /* Select device
642 */
643 udelay (100000); /* 100 ms */
wdenk591dda52002-11-18 00:14:45 +0000644 ide_outb (dev, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(dev));
wdenkc6097192002-11-03 00:24:07 +0000645 udelay (100000); /* 100 ms */
wdenkc6097192002-11-03 00:24:07 +0000646 i = 0;
647 do {
648 udelay (10000); /* 10 ms */
649
wdenk591dda52002-11-18 00:14:45 +0000650 c = ide_inb (dev, ATA_STATUS);
wdenkc6097192002-11-03 00:24:07 +0000651 i++;
Wolfgang Denkb0b104a2010-06-13 18:28:54 +0200652#if defined(CONFIG_SC3)
wdenk452cfd62002-11-19 11:04:11 +0000653 if (i > (ata_reset_time * 100)) {
654#else
wdenkc6097192002-11-03 00:24:07 +0000655 if (i > (ATA_RESET_TIME * 100)) {
wdenk452cfd62002-11-19 11:04:11 +0000656#endif
wdenkc6097192002-11-03 00:24:07 +0000657 puts ("** Timeout **\n");
658 ide_led ((LED_IDE1 | LED_IDE2), 0); /* LED's off */
659 return;
660 }
661 if ((i >= 100) && ((i%100)==0)) {
662 putc ('.');
663 }
664 } while (c & ATA_STAT_BUSY);
665
666 if (c & (ATA_STAT_BUSY | ATA_STAT_FAULT)) {
667 puts ("not available ");
wdenkf1276d42005-02-03 23:00:49 +0000668 debug ("Status = 0x%02X ", c);
wdenkc6097192002-11-03 00:24:07 +0000669#ifndef CONFIG_ATAPI /* ATAPI Devices do not set DRDY */
670 } else if ((c & ATA_STAT_READY) == 0) {
671 puts ("not available ");
wdenkf1276d42005-02-03 23:00:49 +0000672 debug ("Status = 0x%02X ", c);
wdenkc6097192002-11-03 00:24:07 +0000673#endif
674 } else {
675 puts ("OK ");
676 ide_bus_ok[bus] = 1;
677 }
678 WATCHDOG_RESET();
679 }
wdenk452cfd62002-11-19 11:04:11 +0000680
wdenkc6097192002-11-03 00:24:07 +0000681 putc ('\n');
682
683 ide_led ((LED_IDE1 | LED_IDE2), 0); /* LED's off */
684
685 curr_device = -1;
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200686 for (i=0; i<CONFIG_SYS_IDE_MAXDEVICE; ++i) {
wdenkc6097192002-11-03 00:24:07 +0000687#ifdef CONFIG_IDE_LED
688 int led = (IDE_BUS(i) == 0) ? LED_IDE1 : LED_IDE2;
689#endif
wdenk4b16c2e2003-11-07 13:42:26 +0000690 ide_dev_desc[i].type=DEV_TYPE_UNKNOWN;
wdenkc6097192002-11-03 00:24:07 +0000691 ide_dev_desc[i].if_type=IF_TYPE_IDE;
692 ide_dev_desc[i].dev=i;
693 ide_dev_desc[i].part_type=PART_TYPE_UNKNOWN;
694 ide_dev_desc[i].blksz=0;
695 ide_dev_desc[i].lba=0;
696 ide_dev_desc[i].block_read=ide_read;
Macpaul Lind1e49942011-04-11 20:45:32 +0000697 ide_dev_desc[i].block_write = ide_write;
wdenkc6097192002-11-03 00:24:07 +0000698 if (!ide_bus_ok[IDE_BUS(i)])
699 continue;
700 ide_led (led, 1); /* LED on */
701 ide_ident(&ide_dev_desc[i]);
702 ide_led (led, 0); /* LED off */
703 dev_print(&ide_dev_desc[i]);
704/* ide_print (i); */
705 if ((ide_dev_desc[i].lba > 0) && (ide_dev_desc[i].blksz > 0)) {
706 init_part (&ide_dev_desc[i]); /* initialize partition type */
707 if (curr_device < 0)
708 curr_device = i;
709 }
710 }
711 WATCHDOG_RESET();
712}
713
714/* ------------------------------------------------------------------------- */
715
Matthew McClintock6252b4f2011-05-24 05:31:19 +0000716#ifdef CONFIG_PARTITIONS
wdenkc6097192002-11-03 00:24:07 +0000717block_dev_desc_t * ide_get_dev(int dev)
718{
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200719 return (dev < CONFIG_SYS_IDE_MAXDEVICE) ? &ide_dev_desc[dev] : NULL;
wdenkc6097192002-11-03 00:24:07 +0000720}
Matthew McClintock6252b4f2011-05-24 05:31:19 +0000721#endif
wdenkc6097192002-11-03 00:24:07 +0000722
723
724#ifdef CONFIG_IDE_8xx_DIRECT
725
726static void
727set_pcmcia_timing (int pmode)
728{
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200729 volatile immap_t *immr = (immap_t *)CONFIG_SYS_IMMR;
wdenkc6097192002-11-03 00:24:07 +0000730 volatile pcmconf8xx_t *pcmp = &(immr->im_pcmcia);
731 ulong timings;
732
wdenkf1276d42005-02-03 23:00:49 +0000733 debug ("Set timing for PIO Mode %d\n", pmode);
wdenkc6097192002-11-03 00:24:07 +0000734
735 timings = PCMCIA_SHT(pio_config_clk[pmode].t_hold)
736 | PCMCIA_SST(pio_config_clk[pmode].t_setup)
737 | PCMCIA_SL (pio_config_clk[pmode].t_length)
738 ;
739
740 /* IDE 0
741 */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200742 pcmp->pcmc_pbr0 = CONFIG_SYS_PCMCIA_PBR0;
743 pcmp->pcmc_por0 = CONFIG_SYS_PCMCIA_POR0
744#if (CONFIG_SYS_PCMCIA_POR0 != 0)
wdenkc6097192002-11-03 00:24:07 +0000745 | timings
746#endif
747 ;
wdenkf1276d42005-02-03 23:00:49 +0000748 debug ("PBR0: %08x POR0: %08x\n", pcmp->pcmc_pbr0, pcmp->pcmc_por0);
wdenkc6097192002-11-03 00:24:07 +0000749
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200750 pcmp->pcmc_pbr1 = CONFIG_SYS_PCMCIA_PBR1;
751 pcmp->pcmc_por1 = CONFIG_SYS_PCMCIA_POR1
752#if (CONFIG_SYS_PCMCIA_POR1 != 0)
wdenkc6097192002-11-03 00:24:07 +0000753 | timings
754#endif
755 ;
wdenkf1276d42005-02-03 23:00:49 +0000756 debug ("PBR1: %08x POR1: %08x\n", pcmp->pcmc_pbr1, pcmp->pcmc_por1);
wdenkc6097192002-11-03 00:24:07 +0000757
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200758 pcmp->pcmc_pbr2 = CONFIG_SYS_PCMCIA_PBR2;
759 pcmp->pcmc_por2 = CONFIG_SYS_PCMCIA_POR2
760#if (CONFIG_SYS_PCMCIA_POR2 != 0)
wdenkc6097192002-11-03 00:24:07 +0000761 | timings
762#endif
763 ;
wdenkf1276d42005-02-03 23:00:49 +0000764 debug ("PBR2: %08x POR2: %08x\n", pcmp->pcmc_pbr2, pcmp->pcmc_por2);
wdenkc6097192002-11-03 00:24:07 +0000765
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200766 pcmp->pcmc_pbr3 = CONFIG_SYS_PCMCIA_PBR3;
767 pcmp->pcmc_por3 = CONFIG_SYS_PCMCIA_POR3
768#if (CONFIG_SYS_PCMCIA_POR3 != 0)
wdenkc6097192002-11-03 00:24:07 +0000769 | timings
770#endif
771 ;
wdenkf1276d42005-02-03 23:00:49 +0000772 debug ("PBR3: %08x POR3: %08x\n", pcmp->pcmc_pbr3, pcmp->pcmc_por3);
wdenkc6097192002-11-03 00:24:07 +0000773
774 /* IDE 1
775 */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200776 pcmp->pcmc_pbr4 = CONFIG_SYS_PCMCIA_PBR4;
777 pcmp->pcmc_por4 = CONFIG_SYS_PCMCIA_POR4
778#if (CONFIG_SYS_PCMCIA_POR4 != 0)
wdenkc6097192002-11-03 00:24:07 +0000779 | timings
780#endif
781 ;
wdenkf1276d42005-02-03 23:00:49 +0000782 debug ("PBR4: %08x POR4: %08x\n", pcmp->pcmc_pbr4, pcmp->pcmc_por4);
wdenkc6097192002-11-03 00:24:07 +0000783
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200784 pcmp->pcmc_pbr5 = CONFIG_SYS_PCMCIA_PBR5;
785 pcmp->pcmc_por5 = CONFIG_SYS_PCMCIA_POR5
786#if (CONFIG_SYS_PCMCIA_POR5 != 0)
wdenkc6097192002-11-03 00:24:07 +0000787 | timings
788#endif
789 ;
wdenkf1276d42005-02-03 23:00:49 +0000790 debug ("PBR5: %08x POR5: %08x\n", pcmp->pcmc_pbr5, pcmp->pcmc_por5);
wdenkc6097192002-11-03 00:24:07 +0000791
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200792 pcmp->pcmc_pbr6 = CONFIG_SYS_PCMCIA_PBR6;
793 pcmp->pcmc_por6 = CONFIG_SYS_PCMCIA_POR6
794#if (CONFIG_SYS_PCMCIA_POR6 != 0)
wdenkc6097192002-11-03 00:24:07 +0000795 | timings
796#endif
797 ;
wdenkf1276d42005-02-03 23:00:49 +0000798 debug ("PBR6: %08x POR6: %08x\n", pcmp->pcmc_pbr6, pcmp->pcmc_por6);
wdenkc6097192002-11-03 00:24:07 +0000799
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200800 pcmp->pcmc_pbr7 = CONFIG_SYS_PCMCIA_PBR7;
801 pcmp->pcmc_por7 = CONFIG_SYS_PCMCIA_POR7
802#if (CONFIG_SYS_PCMCIA_POR7 != 0)
wdenkc6097192002-11-03 00:24:07 +0000803 | timings
804#endif
805 ;
wdenkf1276d42005-02-03 23:00:49 +0000806 debug ("PBR7: %08x POR7: %08x\n", pcmp->pcmc_pbr7, pcmp->pcmc_por7);
wdenkc6097192002-11-03 00:24:07 +0000807
808}
809
810#endif /* CONFIG_IDE_8xx_DIRECT */
811
812/* ------------------------------------------------------------------------- */
813
wdenk9b7f3842003-10-09 20:09:04 +0000814/* We only need to swap data if we are running on a big endian cpu. */
815/* But Au1x00 cpu:s already swaps data in big endian mode! */
Shinya Kuribayashi9b33e662011-02-05 19:07:00 +0900816#if defined(__LITTLE_ENDIAN) || \
817 (defined(CONFIG_SOC_AU1X00) && !defined(CONFIG_GTH2))
wdenk9b7f3842003-10-09 20:09:04 +0000818#define input_swap_data(x,y,z) input_data(x,y,z)
819#else
wdenkc6097192002-11-03 00:24:07 +0000820static void
821input_swap_data(int dev, ulong *sect_buf, int words)
822{
Wolfgang Denkfb52e952010-09-19 21:28:25 +0200823#if defined(CONFIG_CPC45)
wdenk2f99a692004-01-04 22:51:12 +0000824 uchar i;
825 volatile uchar *pbuf_even = (uchar *)(ATA_CURR_BASE(dev)+ATA_DATA_EVEN);
826 volatile uchar *pbuf_odd = (uchar *)(ATA_CURR_BASE(dev)+ATA_DATA_ODD);
827 ushort *dbuf = (ushort *)sect_buf;
828
829 while (words--) {
830 for (i=0; i<2; i++) {
831 *(((uchar *)(dbuf)) + 1) = *pbuf_even;
832 *(uchar *)dbuf = *pbuf_odd;
833 dbuf+=1;
834 }
835 }
wdenkb995b0f2005-03-06 01:21:30 +0000836#else
wdenkf1276d42005-02-03 23:00:49 +0000837 volatile ushort *pbuf = (ushort *)(ATA_CURR_BASE(dev)+ATA_DATA_REG);
838 ushort *dbuf = (ushort *)sect_buf;
839
840 debug("in input swap data base for read is %lx\n", (unsigned long) pbuf);
841
842 while (words--) {
Wolfgang Denk39c76422006-06-16 17:32:31 +0200843#ifdef __MIPS__
844 *dbuf++ = swab16p((u16*)pbuf);
845 *dbuf++ = swab16p((u16*)pbuf);
Heiko Schocher633e03a2007-06-22 19:11:54 +0200846#elif defined(CONFIG_PCS440EP)
847 *dbuf++ = *pbuf;
848 *dbuf++ = *pbuf;
Wolfgang Denk39c76422006-06-16 17:32:31 +0200849#else
wdenkf1276d42005-02-03 23:00:49 +0000850 *dbuf++ = ld_le16(pbuf);
851 *dbuf++ = ld_le16(pbuf);
Wolfgang Denk39c76422006-06-16 17:32:31 +0200852#endif /* !MIPS */
wdenkf1276d42005-02-03 23:00:49 +0000853 }
854#endif
wdenkc6097192002-11-03 00:24:07 +0000855}
wdenk9b7f3842003-10-09 20:09:04 +0000856#endif /* __LITTLE_ENDIAN || CONFIG_AU1X00 */
wdenk591dda52002-11-18 00:14:45 +0000857
858
Albert Aribaud036c6b42010-08-08 05:17:05 +0530859#if defined(CONFIG_IDE_SWAP_IO)
wdenkc6097192002-11-03 00:24:07 +0000860static void
Wolfgang Denk7fafed22011-04-30 23:29:55 +0200861output_data(int dev, const ulong *sect_buf, int words)
wdenkc6097192002-11-03 00:24:07 +0000862{
Wolfgang Denkfb52e952010-09-19 21:28:25 +0200863#if defined(CONFIG_CPC45)
wdenk2f99a692004-01-04 22:51:12 +0000864 uchar *dbuf;
865 volatile uchar *pbuf_even;
866 volatile uchar *pbuf_odd;
867
868 pbuf_even = (uchar *)(ATA_CURR_BASE(dev)+ATA_DATA_EVEN);
869 pbuf_odd = (uchar *)(ATA_CURR_BASE(dev)+ATA_DATA_ODD);
870 dbuf = (uchar *)sect_buf;
871 while (words--) {
wdenk20c98a62004-04-23 20:32:05 +0000872 EIEIO;
wdenk2f99a692004-01-04 22:51:12 +0000873 *pbuf_even = *dbuf++;
wdenk20c98a62004-04-23 20:32:05 +0000874 EIEIO;
wdenk2f99a692004-01-04 22:51:12 +0000875 *pbuf_odd = *dbuf++;
wdenk20c98a62004-04-23 20:32:05 +0000876 EIEIO;
wdenk2f99a692004-01-04 22:51:12 +0000877 *pbuf_even = *dbuf++;
wdenk20c98a62004-04-23 20:32:05 +0000878 EIEIO;
wdenk2f99a692004-01-04 22:51:12 +0000879 *pbuf_odd = *dbuf++;
880 }
wdenkf1276d42005-02-03 23:00:49 +0000881#else
882 ushort *dbuf;
883 volatile ushort *pbuf;
884
885 pbuf = (ushort *)(ATA_CURR_BASE(dev)+ATA_DATA_REG);
886 dbuf = (ushort *)sect_buf;
887 while (words--) {
Heiko Schocher633e03a2007-06-22 19:11:54 +0200888#if defined(CONFIG_PCS440EP)
889 /* not tested, because CF was write protected */
890 EIEIO;
891 *pbuf = ld_le16(dbuf++);
892 EIEIO;
893 *pbuf = ld_le16(dbuf++);
894#else
wdenkf1276d42005-02-03 23:00:49 +0000895 EIEIO;
896 *pbuf = *dbuf++;
897 EIEIO;
898 *pbuf = *dbuf++;
Heiko Schocher633e03a2007-06-22 19:11:54 +0200899#endif
wdenkf1276d42005-02-03 23:00:49 +0000900 }
901#endif
wdenkc6097192002-11-03 00:24:07 +0000902}
Albert Aribaud036c6b42010-08-08 05:17:05 +0530903#else /* ! CONFIG_IDE_SWAP_IO */
wdenk591dda52002-11-18 00:14:45 +0000904static void
Wolfgang Denk7fafed22011-04-30 23:29:55 +0200905output_data(int dev, const ulong *sect_buf, int words)
wdenk591dda52002-11-18 00:14:45 +0000906{
Macpaul Lind1e49942011-04-11 20:45:32 +0000907#if defined(CONFIG_IDE_AHB)
908 ide_write_data(dev, sect_buf, words);
909#else
910 outsw(ATA_CURR_BASE(dev)+ATA_DATA_REG, sect_buf, words << 1);
911#endif
wdenk591dda52002-11-18 00:14:45 +0000912}
Albert Aribaud036c6b42010-08-08 05:17:05 +0530913#endif /* CONFIG_IDE_SWAP_IO */
wdenkc6097192002-11-03 00:24:07 +0000914
Albert Aribaud036c6b42010-08-08 05:17:05 +0530915#if defined(CONFIG_IDE_SWAP_IO)
wdenkc6097192002-11-03 00:24:07 +0000916static void
917input_data(int dev, ulong *sect_buf, int words)
918{
Wolfgang Denkfb52e952010-09-19 21:28:25 +0200919#if defined(CONFIG_CPC45)
wdenk2f99a692004-01-04 22:51:12 +0000920 uchar *dbuf;
921 volatile uchar *pbuf_even;
922 volatile uchar *pbuf_odd;
923
924 pbuf_even = (uchar *)(ATA_CURR_BASE(dev)+ATA_DATA_EVEN);
925 pbuf_odd = (uchar *)(ATA_CURR_BASE(dev)+ATA_DATA_ODD);
926 dbuf = (uchar *)sect_buf;
927 while (words--) {
wdenkf1276d42005-02-03 23:00:49 +0000928 *dbuf++ = *pbuf_even;
wdenk20c98a62004-04-23 20:32:05 +0000929 EIEIO;
wdenkf1276d42005-02-03 23:00:49 +0000930 SYNC;
931 *dbuf++ = *pbuf_odd;
wdenkf6925c72005-01-22 18:26:04 +0000932 EIEIO;
wdenkf1276d42005-02-03 23:00:49 +0000933 SYNC;
wdenk2f99a692004-01-04 22:51:12 +0000934 *dbuf++ = *pbuf_even;
wdenk20c98a62004-04-23 20:32:05 +0000935 EIEIO;
wdenkf1276d42005-02-03 23:00:49 +0000936 SYNC;
wdenk2f99a692004-01-04 22:51:12 +0000937 *dbuf++ = *pbuf_odd;
wdenk20c98a62004-04-23 20:32:05 +0000938 EIEIO;
wdenkf1276d42005-02-03 23:00:49 +0000939 SYNC;
940 }
941#else
942 ushort *dbuf;
943 volatile ushort *pbuf;
944
945 pbuf = (ushort *)(ATA_CURR_BASE(dev)+ATA_DATA_REG);
946 dbuf = (ushort *)sect_buf;
947
948 debug("in input data base for read is %lx\n", (unsigned long) pbuf);
949
950 while (words--) {
Heiko Schocher633e03a2007-06-22 19:11:54 +0200951#if defined(CONFIG_PCS440EP)
952 EIEIO;
953 *dbuf++ = ld_le16(pbuf);
954 EIEIO;
955 *dbuf++ = ld_le16(pbuf);
956#else
wdenk20c98a62004-04-23 20:32:05 +0000957 EIEIO;
wdenkf1276d42005-02-03 23:00:49 +0000958 *dbuf++ = *pbuf;
wdenkf6925c72005-01-22 18:26:04 +0000959 EIEIO;
wdenkf1276d42005-02-03 23:00:49 +0000960 *dbuf++ = *pbuf;
Heiko Schocher633e03a2007-06-22 19:11:54 +0200961#endif
wdenk2f99a692004-01-04 22:51:12 +0000962 }
wdenkf1276d42005-02-03 23:00:49 +0000963#endif
wdenkc6097192002-11-03 00:24:07 +0000964}
Albert Aribaud036c6b42010-08-08 05:17:05 +0530965#else /* ! CONFIG_IDE_SWAP_IO */
wdenk591dda52002-11-18 00:14:45 +0000966static void
967input_data(int dev, ulong *sect_buf, int words)
968{
Macpaul Lind1e49942011-04-11 20:45:32 +0000969#if defined(CONFIG_IDE_AHB)
970 ide_read_data(dev, sect_buf, words);
971#else
wdenk9f837932003-10-09 19:00:25 +0000972 insw(ATA_CURR_BASE(dev)+ATA_DATA_REG, sect_buf, words << 1);
Macpaul Lind1e49942011-04-11 20:45:32 +0000973#endif
wdenk591dda52002-11-18 00:14:45 +0000974}
975
Albert Aribaud036c6b42010-08-08 05:17:05 +0530976#endif /* CONFIG_IDE_SWAP_IO */
wdenkc6097192002-11-03 00:24:07 +0000977
978/* -------------------------------------------------------------------------
979 */
980static void ide_ident (block_dev_desc_t *dev_desc)
981{
wdenkc6097192002-11-03 00:24:07 +0000982 unsigned char c;
Marek Vasute49757e2011-08-20 09:15:13 +0000983 hd_driveid_t iop;
wdenkc6097192002-11-03 00:24:07 +0000984
wdenke2d6d742004-09-28 20:34:50 +0000985#ifdef CONFIG_ATAPI
986 int retries = 0;
wdenk452cfd62002-11-19 11:04:11 +0000987 int do_retry = 0;
988#endif
989
Steven A. Falco2b7574e2008-08-15 15:34:10 -0400990#ifdef CONFIG_TUNE_PIO
991 int pio_mode;
992#endif
993
wdenkc6097192002-11-03 00:24:07 +0000994#if 0
995 int mode, cycle_time;
996#endif
997 int device;
998 device=dev_desc->dev;
999 printf (" Device %d: ", device);
1000
1001 ide_led (DEVICE_LED(device), 1); /* LED on */
1002 /* Select device
1003 */
wdenk591dda52002-11-18 00:14:45 +00001004 ide_outb (device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
wdenkc6097192002-11-03 00:24:07 +00001005 dev_desc->if_type=IF_TYPE_IDE;
1006#ifdef CONFIG_ATAPI
wdenk452cfd62002-11-19 11:04:11 +00001007
wdenk452cfd62002-11-19 11:04:11 +00001008 do_retry = 0;
1009 retries = 0;
1010
1011 /* Warning: This will be tricky to read */
wdenkf602aa02004-03-13 23:29:43 +00001012 while (retries <= 1) {
wdenkc6097192002-11-03 00:24:07 +00001013 /* check signature */
wdenk591dda52002-11-18 00:14:45 +00001014 if ((ide_inb(device,ATA_SECT_CNT) == 0x01) &&
1015 (ide_inb(device,ATA_SECT_NUM) == 0x01) &&
1016 (ide_inb(device,ATA_CYL_LOW) == 0x14) &&
1017 (ide_inb(device,ATA_CYL_HIGH) == 0xEB)) {
wdenkc6097192002-11-03 00:24:07 +00001018 /* ATAPI Signature found */
1019 dev_desc->if_type=IF_TYPE_ATAPI;
1020 /* Start Ident Command
1021 */
wdenk591dda52002-11-18 00:14:45 +00001022 ide_outb (device, ATA_COMMAND, ATAPI_CMD_IDENT);
wdenkc6097192002-11-03 00:24:07 +00001023 /*
1024 * Wait for completion - ATAPI devices need more time
1025 * to become ready
1026 */
1027 c = ide_wait (device, ATAPI_TIME_OUT);
wdenkf602aa02004-03-13 23:29:43 +00001028 } else
wdenkc6097192002-11-03 00:24:07 +00001029#endif
1030 {
1031 /* Start Ident Command
1032 */
wdenk591dda52002-11-18 00:14:45 +00001033 ide_outb (device, ATA_COMMAND, ATA_CMD_IDENT);
wdenkc6097192002-11-03 00:24:07 +00001034
1035 /* Wait for completion
1036 */
1037 c = ide_wait (device, IDE_TIME_OUT);
1038 }
1039 ide_led (DEVICE_LED(device), 0); /* LED off */
1040
1041 if (((c & ATA_STAT_DRQ) == 0) ||
1042 ((c & (ATA_STAT_FAULT|ATA_STAT_ERR)) != 0) ) {
wdenke2d6d742004-09-28 20:34:50 +00001043#ifdef CONFIG_ATAPI
wdenkf1276d42005-02-03 23:00:49 +00001044 {
1045 /* Need to soft reset the device in case it's an ATAPI... */
1046 debug ("Retrying...\n");
1047 ide_outb (device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
1048 udelay(100000);
1049 ide_outb (device, ATA_COMMAND, 0x08);
1050 udelay (500000); /* 500 ms */
1051 }
wdenke2d6d742004-09-28 20:34:50 +00001052 /* Select device
1053 */
wdenk452cfd62002-11-19 11:04:11 +00001054 ide_outb (device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
wdenk452cfd62002-11-19 11:04:11 +00001055 retries++;
wdenke2d6d742004-09-28 20:34:50 +00001056#else
1057 return;
1058#endif
wdenkc6097192002-11-03 00:24:07 +00001059 }
wdenke2d6d742004-09-28 20:34:50 +00001060#ifdef CONFIG_ATAPI
1061 else
1062 break;
wdenk452cfd62002-11-19 11:04:11 +00001063 } /* see above - ugly to read */
wdenke2d6d742004-09-28 20:34:50 +00001064
1065 if (retries == 2) /* Not found */
1066 return;
1067#endif
wdenkc6097192002-11-03 00:24:07 +00001068
Marek Vasute49757e2011-08-20 09:15:13 +00001069 input_swap_data (device, (ulong *)&iop, ATA_SECTORWORDS);
wdenkc6097192002-11-03 00:24:07 +00001070
Marek Vasute49757e2011-08-20 09:15:13 +00001071 ident_cpy ((unsigned char*)dev_desc->revision, iop.fw_rev, sizeof(dev_desc->revision));
1072 ident_cpy ((unsigned char*)dev_desc->vendor, iop.model, sizeof(dev_desc->vendor));
1073 ident_cpy ((unsigned char*)dev_desc->product, iop.serial_no, sizeof(dev_desc->product));
wdenkacd9b102004-03-14 00:59:59 +00001074#ifdef __LITTLE_ENDIAN
1075 /*
Richard Retanubun54f30622008-11-06 14:01:51 -05001076 * firmware revision, model, and serial number have Big Endian Byte
1077 * order in Word. Convert all three to little endian.
wdenkacd9b102004-03-14 00:59:59 +00001078 *
1079 * See CF+ and CompactFlash Specification Revision 2.0:
Richard Retanubun54f30622008-11-06 14:01:51 -05001080 * 6.2.1.6: Identify Drive, Table 39 for more details
wdenkacd9b102004-03-14 00:59:59 +00001081 */
1082
1083 strswab (dev_desc->revision);
1084 strswab (dev_desc->vendor);
Richard Retanubun54f30622008-11-06 14:01:51 -05001085 strswab (dev_desc->product);
wdenkacd9b102004-03-14 00:59:59 +00001086#endif /* __LITTLE_ENDIAN */
wdenkc6097192002-11-03 00:24:07 +00001087
Marek Vasute49757e2011-08-20 09:15:13 +00001088 if ((iop.config & 0x0080) == 0x0080)
wdenkc6097192002-11-03 00:24:07 +00001089 dev_desc->removable = 1;
1090 else
1091 dev_desc->removable = 0;
1092
Steven A. Falco2b7574e2008-08-15 15:34:10 -04001093#ifdef CONFIG_TUNE_PIO
1094 /* Mode 0 - 2 only, are directly determined by word 51. */
Marek Vasute49757e2011-08-20 09:15:13 +00001095 pio_mode = iop.tPIO;
Steven A. Falco2b7574e2008-08-15 15:34:10 -04001096 if (pio_mode > 2) {
1097 printf("WARNING: Invalid PIO (word 51 = %d).\n", pio_mode);
1098 pio_mode = 0; /* Force it to dead slow, and hope for the best... */
1099 }
1100
1101 /* Any CompactFlash Storage Card that supports PIO mode 3 or above
1102 * shall set bit 1 of word 53 to one and support the fields contained
1103 * in words 64 through 70.
1104 */
Marek Vasute49757e2011-08-20 09:15:13 +00001105 if (iop.field_valid & 0x02) {
Steven A. Falco2b7574e2008-08-15 15:34:10 -04001106 /* Mode 3 and above are possible. Check in order from slow
1107 * to fast, so we wind up with the highest mode allowed.
1108 */
Marek Vasute49757e2011-08-20 09:15:13 +00001109 if (iop.eide_pio_modes & 0x01)
Steven A. Falco2b7574e2008-08-15 15:34:10 -04001110 pio_mode = 3;
Marek Vasute49757e2011-08-20 09:15:13 +00001111 if (iop.eide_pio_modes & 0x02)
Steven A. Falco2b7574e2008-08-15 15:34:10 -04001112 pio_mode = 4;
Marek Vasute49757e2011-08-20 09:15:13 +00001113 if (ata_id_is_cfa((u16 *)&iop)) {
1114 if ((iop.cf_advanced_caps & 0x07) == 0x01)
Steven A. Falco2b7574e2008-08-15 15:34:10 -04001115 pio_mode = 5;
Marek Vasute49757e2011-08-20 09:15:13 +00001116 if ((iop.cf_advanced_caps & 0x07) == 0x02)
Steven A. Falco2b7574e2008-08-15 15:34:10 -04001117 pio_mode = 6;
1118 }
1119 }
1120
1121 /* System-specific, depends on bus speeds, etc. */
1122 ide_set_piomode(pio_mode);
1123#endif /* CONFIG_TUNE_PIO */
1124
wdenkc6097192002-11-03 00:24:07 +00001125#if 0
1126 /*
1127 * Drive PIO mode autoselection
1128 */
Marek Vasute49757e2011-08-20 09:15:13 +00001129 mode = iop.tPIO;
wdenkc6097192002-11-03 00:24:07 +00001130
1131 printf ("tPIO = 0x%02x = %d\n",mode, mode);
1132 if (mode > 2) { /* 2 is maximum allowed tPIO value */
1133 mode = 2;
wdenkf1276d42005-02-03 23:00:49 +00001134 debug ("Override tPIO -> 2\n");
wdenkc6097192002-11-03 00:24:07 +00001135 }
Marek Vasute49757e2011-08-20 09:15:13 +00001136 if (iop.field_valid & 2) { /* drive implements ATA2? */
wdenkf1276d42005-02-03 23:00:49 +00001137 debug ("Drive implements ATA2\n");
Marek Vasute49757e2011-08-20 09:15:13 +00001138 if (iop.capability & 8) { /* drive supports use_iordy? */
1139 cycle_time = iop.eide_pio_iordy;
wdenkc6097192002-11-03 00:24:07 +00001140 } else {
Marek Vasute49757e2011-08-20 09:15:13 +00001141 cycle_time = iop.eide_pio;
wdenkc6097192002-11-03 00:24:07 +00001142 }
wdenkf1276d42005-02-03 23:00:49 +00001143 debug ("cycle time = %d\n", cycle_time);
wdenkc6097192002-11-03 00:24:07 +00001144 mode = 4;
1145 if (cycle_time > 120) mode = 3; /* 120 ns for PIO mode 4 */
1146 if (cycle_time > 180) mode = 2; /* 180 ns for PIO mode 3 */
1147 if (cycle_time > 240) mode = 1; /* 240 ns for PIO mode 4 */
1148 if (cycle_time > 383) mode = 0; /* 383 ns for PIO mode 4 */
1149 }
1150 printf ("PIO mode to use: PIO %d\n", mode);
1151#endif /* 0 */
1152
1153#ifdef CONFIG_ATAPI
1154 if (dev_desc->if_type==IF_TYPE_ATAPI) {
1155 atapi_inquiry(dev_desc);
1156 return;
1157 }
1158#endif /* CONFIG_ATAPI */
1159
wdenkacd9b102004-03-14 00:59:59 +00001160#ifdef __BIG_ENDIAN
wdenkc6097192002-11-03 00:24:07 +00001161 /* swap shorts */
Marek Vasute49757e2011-08-20 09:15:13 +00001162 dev_desc->lba = (iop.lba_capacity << 16) | (iop.lba_capacity >> 16);
wdenkacd9b102004-03-14 00:59:59 +00001163#else /* ! __BIG_ENDIAN */
1164 /*
1165 * do not swap shorts on little endian
1166 *
1167 * See CF+ and CompactFlash Specification Revision 2.0:
1168 * 6.2.1.6: Identfy Drive, Table 39, Word Address 57-58 for details.
1169 */
Marek Vasute49757e2011-08-20 09:15:13 +00001170 dev_desc->lba = iop.lba_capacity;
wdenkacd9b102004-03-14 00:59:59 +00001171#endif /* __BIG_ENDIAN */
wdenkf602aa02004-03-13 23:29:43 +00001172
wdenkc35ba4e2004-03-14 22:25:36 +00001173#ifdef CONFIG_LBA48
Marek Vasute49757e2011-08-20 09:15:13 +00001174 if (iop.command_set_2 & 0x0400) { /* LBA 48 support */
wdenk05939202004-04-18 17:39:38 +00001175 dev_desc->lba48 = 1;
Marek Vasute49757e2011-08-20 09:15:13 +00001176 dev_desc->lba = (unsigned long long)iop.lba48_capacity[0] |
1177 ((unsigned long long)iop.lba48_capacity[1] << 16) |
1178 ((unsigned long long)iop.lba48_capacity[2] << 32) |
1179 ((unsigned long long)iop.lba48_capacity[3] << 48);
wdenkf602aa02004-03-13 23:29:43 +00001180 } else {
wdenkf602aa02004-03-13 23:29:43 +00001181 dev_desc->lba48 = 0;
1182 }
1183#endif /* CONFIG_LBA48 */
wdenkc6097192002-11-03 00:24:07 +00001184 /* assuming HD */
1185 dev_desc->type=DEV_TYPE_HARDDISK;
1186 dev_desc->blksz=ATA_BLOCKSIZE;
1187 dev_desc->lun=0; /* just to fill something in... */
1188
Wolfgang Denka1be4762008-05-20 16:00:29 +02001189#if 0 /* only used to test the powersaving mode,
wdenkc6097192002-11-03 00:24:07 +00001190 * if enabled, the drive goes after 5 sec
1191 * in standby mode */
wdenk591dda52002-11-18 00:14:45 +00001192 ide_outb (device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
wdenkc6097192002-11-03 00:24:07 +00001193 c = ide_wait (device, IDE_TIME_OUT);
wdenk591dda52002-11-18 00:14:45 +00001194 ide_outb (device, ATA_SECT_CNT, 1);
1195 ide_outb (device, ATA_LBA_LOW, 0);
1196 ide_outb (device, ATA_LBA_MID, 0);
1197 ide_outb (device, ATA_LBA_HIGH, 0);
wdenkf1276d42005-02-03 23:00:49 +00001198 ide_outb (device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
wdenk591dda52002-11-18 00:14:45 +00001199 ide_outb (device, ATA_COMMAND, 0xe3);
wdenkc6097192002-11-03 00:24:07 +00001200 udelay (50);
1201 c = ide_wait (device, IDE_TIME_OUT); /* can't take over 500 ms */
1202#endif
1203}
1204
1205
1206/* ------------------------------------------------------------------------- */
1207
Grant Likely2089cab2007-02-20 09:05:45 +01001208ulong ide_read (int device, lbaint_t blknr, ulong blkcnt, void *buffer)
wdenkc6097192002-11-03 00:24:07 +00001209{
1210 ulong n = 0;
1211 unsigned char c;
1212 unsigned char pwrsave=0; /* power save */
wdenkc35ba4e2004-03-14 22:25:36 +00001213#ifdef CONFIG_LBA48
wdenkf602aa02004-03-13 23:29:43 +00001214 unsigned char lba48 = 0;
wdenkc6097192002-11-03 00:24:07 +00001215
Guennadi Liakhovetskib71f1ca2008-04-28 14:36:06 +02001216 if (blknr & 0x0000fffff0000000ULL) {
wdenkf602aa02004-03-13 23:29:43 +00001217 /* more than 28 bits used, use 48bit mode */
1218 lba48 = 1;
1219 }
1220#endif
Marek Vasut92131712011-10-25 11:39:15 +02001221 debug("ide_read dev %d start %lX, blocks %lX buffer at %lX\n",
wdenkc6097192002-11-03 00:24:07 +00001222 device, blknr, blkcnt, (ulong)buffer);
1223
1224 ide_led (DEVICE_LED(device), 1); /* LED on */
1225
1226 /* Select device
1227 */
wdenk591dda52002-11-18 00:14:45 +00001228 ide_outb (device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
wdenkc6097192002-11-03 00:24:07 +00001229 c = ide_wait (device, IDE_TIME_OUT);
1230
1231 if (c & ATA_STAT_BUSY) {
1232 printf ("IDE read: device %d not ready\n", device);
1233 goto IDE_READ_E;
1234 }
1235
1236 /* first check if the drive is in Powersaving mode, if yes,
1237 * increase the timeout value */
wdenk591dda52002-11-18 00:14:45 +00001238 ide_outb (device, ATA_COMMAND, ATA_CMD_CHK_PWR);
wdenkc6097192002-11-03 00:24:07 +00001239 udelay (50);
1240
1241 c = ide_wait (device, IDE_TIME_OUT); /* can't take over 500 ms */
1242
1243 if (c & ATA_STAT_BUSY) {
1244 printf ("IDE read: device %d not ready\n", device);
1245 goto IDE_READ_E;
1246 }
1247 if ((c & ATA_STAT_ERR) == ATA_STAT_ERR) {
1248 printf ("No Powersaving mode %X\n", c);
1249 } else {
wdenk591dda52002-11-18 00:14:45 +00001250 c = ide_inb(device,ATA_SECT_CNT);
wdenkf1276d42005-02-03 23:00:49 +00001251 debug ("Powersaving %02X\n",c);
wdenkc6097192002-11-03 00:24:07 +00001252 if(c==0)
1253 pwrsave=1;
1254 }
1255
1256
1257 while (blkcnt-- > 0) {
1258
1259 c = ide_wait (device, IDE_TIME_OUT);
1260
1261 if (c & ATA_STAT_BUSY) {
1262 printf ("IDE read: device %d not ready\n", device);
1263 break;
1264 }
wdenkc35ba4e2004-03-14 22:25:36 +00001265#ifdef CONFIG_LBA48
wdenkf602aa02004-03-13 23:29:43 +00001266 if (lba48) {
1267 /* write high bits */
1268 ide_outb (device, ATA_SECT_CNT, 0);
1269 ide_outb (device, ATA_LBA_LOW, (blknr >> 24) & 0xFF);
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001270#ifdef CONFIG_SYS_64BIT_LBA
wdenkf602aa02004-03-13 23:29:43 +00001271 ide_outb (device, ATA_LBA_MID, (blknr >> 32) & 0xFF);
1272 ide_outb (device, ATA_LBA_HIGH, (blknr >> 40) & 0xFF);
Guennadi Liakhovetskib71f1ca2008-04-28 14:36:06 +02001273#else
1274 ide_outb (device, ATA_LBA_MID, 0);
1275 ide_outb (device, ATA_LBA_HIGH, 0);
1276#endif
wdenkf602aa02004-03-13 23:29:43 +00001277 }
1278#endif
wdenk591dda52002-11-18 00:14:45 +00001279 ide_outb (device, ATA_SECT_CNT, 1);
1280 ide_outb (device, ATA_LBA_LOW, (blknr >> 0) & 0xFF);
1281 ide_outb (device, ATA_LBA_MID, (blknr >> 8) & 0xFF);
1282 ide_outb (device, ATA_LBA_HIGH, (blknr >> 16) & 0xFF);
wdenkf602aa02004-03-13 23:29:43 +00001283
wdenkc35ba4e2004-03-14 22:25:36 +00001284#ifdef CONFIG_LBA48
wdenkf602aa02004-03-13 23:29:43 +00001285 if (lba48) {
1286 ide_outb (device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device) );
1287 ide_outb (device, ATA_COMMAND, ATA_CMD_READ_EXT);
1288
1289 } else
1290#endif
1291 {
1292 ide_outb (device, ATA_DEV_HD, ATA_LBA |
1293 ATA_DEVICE(device) |
1294 ((blknr >> 24) & 0xF) );
1295 ide_outb (device, ATA_COMMAND, ATA_CMD_READ);
1296 }
wdenkc6097192002-11-03 00:24:07 +00001297
1298 udelay (50);
1299
1300 if(pwrsave) {
1301 c = ide_wait (device, IDE_SPIN_UP_TIME_OUT); /* may take up to 4 sec */
1302 pwrsave=0;
1303 } else {
1304 c = ide_wait (device, IDE_TIME_OUT); /* can't take over 500 ms */
1305 }
1306
1307 if ((c&(ATA_STAT_DRQ|ATA_STAT_BUSY|ATA_STAT_ERR)) != ATA_STAT_DRQ) {
Heiko Schocher0f602e12009-12-03 11:21:21 +01001308#if defined(CONFIG_SYS_64BIT_LBA)
Mike Frysinger2b745dc2009-02-16 23:21:36 -05001309 printf ("Error (no IRQ) dev %d blk %Ld: status 0x%02x\n",
wdenkc6097192002-11-03 00:24:07 +00001310 device, blknr, c);
wdenkf602aa02004-03-13 23:29:43 +00001311#else
1312 printf ("Error (no IRQ) dev %d blk %ld: status 0x%02x\n",
1313 device, (ulong)blknr, c);
1314#endif
wdenkc6097192002-11-03 00:24:07 +00001315 break;
1316 }
1317
1318 input_data (device, buffer, ATA_SECTORWORDS);
wdenk591dda52002-11-18 00:14:45 +00001319 (void) ide_inb (device, ATA_STATUS); /* clear IRQ */
wdenkc6097192002-11-03 00:24:07 +00001320
1321 ++n;
1322 ++blknr;
Greg Lopp50abf9f2007-04-13 08:02:24 +02001323 buffer += ATA_BLOCKSIZE;
wdenkc6097192002-11-03 00:24:07 +00001324 }
1325IDE_READ_E:
1326 ide_led (DEVICE_LED(device), 0); /* LED off */
1327 return (n);
1328}
1329
1330/* ------------------------------------------------------------------------- */
1331
1332
Wolfgang Denk7fafed22011-04-30 23:29:55 +02001333ulong ide_write (int device, lbaint_t blknr, ulong blkcnt, const void *buffer)
wdenkc6097192002-11-03 00:24:07 +00001334{
1335 ulong n = 0;
1336 unsigned char c;
wdenkc35ba4e2004-03-14 22:25:36 +00001337#ifdef CONFIG_LBA48
wdenkf602aa02004-03-13 23:29:43 +00001338 unsigned char lba48 = 0;
1339
Guennadi Liakhovetskib71f1ca2008-04-28 14:36:06 +02001340 if (blknr & 0x0000fffff0000000ULL) {
wdenkf602aa02004-03-13 23:29:43 +00001341 /* more than 28 bits used, use 48bit mode */
1342 lba48 = 1;
1343 }
1344#endif
wdenkc6097192002-11-03 00:24:07 +00001345
1346 ide_led (DEVICE_LED(device), 1); /* LED on */
1347
1348 /* Select device
1349 */
wdenk591dda52002-11-18 00:14:45 +00001350 ide_outb (device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
wdenkc6097192002-11-03 00:24:07 +00001351
1352 while (blkcnt-- > 0) {
1353
1354 c = ide_wait (device, IDE_TIME_OUT);
1355
1356 if (c & ATA_STAT_BUSY) {
1357 printf ("IDE read: device %d not ready\n", device);
1358 goto WR_OUT;
1359 }
wdenkc35ba4e2004-03-14 22:25:36 +00001360#ifdef CONFIG_LBA48
wdenkf602aa02004-03-13 23:29:43 +00001361 if (lba48) {
1362 /* write high bits */
1363 ide_outb (device, ATA_SECT_CNT, 0);
1364 ide_outb (device, ATA_LBA_LOW, (blknr >> 24) & 0xFF);
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001365#ifdef CONFIG_SYS_64BIT_LBA
wdenkf602aa02004-03-13 23:29:43 +00001366 ide_outb (device, ATA_LBA_MID, (blknr >> 32) & 0xFF);
1367 ide_outb (device, ATA_LBA_HIGH, (blknr >> 40) & 0xFF);
Guennadi Liakhovetskib71f1ca2008-04-28 14:36:06 +02001368#else
1369 ide_outb (device, ATA_LBA_MID, 0);
1370 ide_outb (device, ATA_LBA_HIGH, 0);
1371#endif
wdenkf602aa02004-03-13 23:29:43 +00001372 }
1373#endif
wdenk591dda52002-11-18 00:14:45 +00001374 ide_outb (device, ATA_SECT_CNT, 1);
1375 ide_outb (device, ATA_LBA_LOW, (blknr >> 0) & 0xFF);
1376 ide_outb (device, ATA_LBA_MID, (blknr >> 8) & 0xFF);
1377 ide_outb (device, ATA_LBA_HIGH, (blknr >> 16) & 0xFF);
wdenkf602aa02004-03-13 23:29:43 +00001378
wdenkc35ba4e2004-03-14 22:25:36 +00001379#ifdef CONFIG_LBA48
wdenkf602aa02004-03-13 23:29:43 +00001380 if (lba48) {
1381 ide_outb (device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device) );
1382 ide_outb (device, ATA_COMMAND, ATA_CMD_WRITE_EXT);
1383
1384 } else
1385#endif
1386 {
1387 ide_outb (device, ATA_DEV_HD, ATA_LBA |
1388 ATA_DEVICE(device) |
1389 ((blknr >> 24) & 0xF) );
1390 ide_outb (device, ATA_COMMAND, ATA_CMD_WRITE);
1391 }
wdenkc6097192002-11-03 00:24:07 +00001392
1393 udelay (50);
1394
1395 c = ide_wait (device, IDE_TIME_OUT); /* can't take over 500 ms */
1396
1397 if ((c&(ATA_STAT_DRQ|ATA_STAT_BUSY|ATA_STAT_ERR)) != ATA_STAT_DRQ) {
Heiko Schocher0f602e12009-12-03 11:21:21 +01001398#if defined(CONFIG_SYS_64BIT_LBA)
Mike Frysinger2b745dc2009-02-16 23:21:36 -05001399 printf ("Error (no IRQ) dev %d blk %Ld: status 0x%02x\n",
wdenkc6097192002-11-03 00:24:07 +00001400 device, blknr, c);
wdenkf602aa02004-03-13 23:29:43 +00001401#else
1402 printf ("Error (no IRQ) dev %d blk %ld: status 0x%02x\n",
1403 device, (ulong)blknr, c);
1404#endif
wdenkc6097192002-11-03 00:24:07 +00001405 goto WR_OUT;
1406 }
1407
1408 output_data (device, buffer, ATA_SECTORWORDS);
wdenk591dda52002-11-18 00:14:45 +00001409 c = ide_inb (device, ATA_STATUS); /* clear IRQ */
wdenkc6097192002-11-03 00:24:07 +00001410 ++n;
1411 ++blknr;
Greg Lopp50abf9f2007-04-13 08:02:24 +02001412 buffer += ATA_BLOCKSIZE;
wdenkc6097192002-11-03 00:24:07 +00001413 }
1414WR_OUT:
1415 ide_led (DEVICE_LED(device), 0); /* LED off */
1416 return (n);
1417}
1418
1419/* ------------------------------------------------------------------------- */
1420
1421/*
1422 * copy src to dest, skipping leading and trailing blanks and null
1423 * terminate the string
wdenk18bd81e2004-03-17 01:13:07 +00001424 * "len" is the size of available memory including the terminating '\0'
wdenkc6097192002-11-03 00:24:07 +00001425 */
wdenk18bd81e2004-03-17 01:13:07 +00001426static void ident_cpy (unsigned char *dst, unsigned char *src, unsigned int len)
wdenkc6097192002-11-03 00:24:07 +00001427{
wdenk18bd81e2004-03-17 01:13:07 +00001428 unsigned char *end, *last;
wdenkc6097192002-11-03 00:24:07 +00001429
wdenk18bd81e2004-03-17 01:13:07 +00001430 last = dst;
wdenkeec9a3d2004-03-23 23:20:24 +00001431 end = src + len - 1;
wdenk18bd81e2004-03-17 01:13:07 +00001432
1433 /* reserve space for '\0' */
1434 if (len < 2)
1435 goto OUT;
wdenkc12081a2004-03-23 20:18:25 +00001436
wdenk18bd81e2004-03-17 01:13:07 +00001437 /* skip leading white space */
1438 while ((*src) && (src<end) && (*src==' '))
1439 ++src;
1440
1441 /* copy string, omitting trailing white space */
1442 while ((*src) && (src<end)) {
1443 *dst++ = *src;
1444 if (*src++ != ' ')
1445 last = dst;
wdenkc6097192002-11-03 00:24:07 +00001446 }
wdenk18bd81e2004-03-17 01:13:07 +00001447OUT:
1448 *last = '\0';
wdenkc6097192002-11-03 00:24:07 +00001449}
1450
1451/* ------------------------------------------------------------------------- */
1452
1453/*
1454 * Wait until Busy bit is off, or timeout (in ms)
1455 * Return last status
1456 */
1457static uchar ide_wait (int dev, ulong t)
1458{
1459 ulong delay = 10 * t; /* poll every 100 us */
1460 uchar c;
1461
wdenk591dda52002-11-18 00:14:45 +00001462 while ((c = ide_inb(dev, ATA_STATUS)) & ATA_STAT_BUSY) {
wdenkc6097192002-11-03 00:24:07 +00001463 udelay (100);
1464 if (delay-- == 0) {
1465 break;
1466 }
1467 }
1468 return (c);
1469}
1470
1471/* ------------------------------------------------------------------------- */
1472
1473#ifdef CONFIG_IDE_RESET
1474extern void ide_set_reset(int idereset);
1475
1476static void ide_reset (void)
1477{
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001478#if defined(CONFIG_SYS_PB_12V_ENABLE) || defined(CONFIG_SYS_PB_IDE_MOTOR)
1479 volatile immap_t *immr = (immap_t *)CONFIG_SYS_IMMR;
wdenkc6097192002-11-03 00:24:07 +00001480#endif
1481 int i;
1482
1483 curr_device = -1;
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001484 for (i=0; i<CONFIG_SYS_IDE_MAXBUS; ++i)
wdenkc6097192002-11-03 00:24:07 +00001485 ide_bus_ok[i] = 0;
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001486 for (i=0; i<CONFIG_SYS_IDE_MAXDEVICE; ++i)
wdenkc6097192002-11-03 00:24:07 +00001487 ide_dev_desc[i].type = DEV_TYPE_UNKNOWN;
1488
1489 ide_set_reset (1); /* assert reset */
1490
Martin Krause7e35caf2008-04-03 13:37:56 +02001491 /* the reset signal shall be asserted for et least 25 us */
1492 udelay(25);
1493
wdenkc6097192002-11-03 00:24:07 +00001494 WATCHDOG_RESET();
1495
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001496#ifdef CONFIG_SYS_PB_12V_ENABLE
1497 immr->im_cpm.cp_pbdat &= ~(CONFIG_SYS_PB_12V_ENABLE); /* 12V Enable output OFF */
1498 immr->im_cpm.cp_pbpar &= ~(CONFIG_SYS_PB_12V_ENABLE);
1499 immr->im_cpm.cp_pbodr &= ~(CONFIG_SYS_PB_12V_ENABLE);
1500 immr->im_cpm.cp_pbdir |= CONFIG_SYS_PB_12V_ENABLE;
wdenkc6097192002-11-03 00:24:07 +00001501
1502 /* wait 500 ms for the voltage to stabilize
1503 */
1504 for (i=0; i<500; ++i) {
1505 udelay (1000);
1506 }
1507
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001508 immr->im_cpm.cp_pbdat |= CONFIG_SYS_PB_12V_ENABLE; /* 12V Enable output ON */
1509#endif /* CONFIG_SYS_PB_12V_ENABLE */
wdenkc6097192002-11-03 00:24:07 +00001510
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001511#ifdef CONFIG_SYS_PB_IDE_MOTOR
wdenkc6097192002-11-03 00:24:07 +00001512 /* configure IDE Motor voltage monitor pin as input */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001513 immr->im_cpm.cp_pbpar &= ~(CONFIG_SYS_PB_IDE_MOTOR);
1514 immr->im_cpm.cp_pbodr &= ~(CONFIG_SYS_PB_IDE_MOTOR);
1515 immr->im_cpm.cp_pbdir &= ~(CONFIG_SYS_PB_IDE_MOTOR);
wdenkc6097192002-11-03 00:24:07 +00001516
1517 /* wait up to 1 s for the motor voltage to stabilize
1518 */
1519 for (i=0; i<1000; ++i) {
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001520 if ((immr->im_cpm.cp_pbdat & CONFIG_SYS_PB_IDE_MOTOR) != 0) {
wdenkc6097192002-11-03 00:24:07 +00001521 break;
1522 }
1523 udelay (1000);
1524 }
1525
1526 if (i == 1000) { /* Timeout */
1527 printf ("\nWarning: 5V for IDE Motor missing\n");
1528# ifdef CONFIG_STATUS_LED
1529# ifdef STATUS_LED_YELLOW
1530 status_led_set (STATUS_LED_YELLOW, STATUS_LED_ON );
1531# endif
1532# ifdef STATUS_LED_GREEN
1533 status_led_set (STATUS_LED_GREEN, STATUS_LED_OFF);
1534# endif
1535# endif /* CONFIG_STATUS_LED */
1536 }
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001537#endif /* CONFIG_SYS_PB_IDE_MOTOR */
wdenkc6097192002-11-03 00:24:07 +00001538
1539 WATCHDOG_RESET();
1540
1541 /* de-assert RESET signal */
1542 ide_set_reset(0);
1543
1544 /* wait 250 ms */
1545 for (i=0; i<250; ++i) {
1546 udelay (1000);
1547 }
1548}
1549
1550#endif /* CONFIG_IDE_RESET */
1551
1552/* ------------------------------------------------------------------------- */
1553
wdenk54070ab2004-12-31 09:32:47 +00001554#if defined(CONFIG_IDE_LED) && \
wdenk54070ab2004-12-31 09:32:47 +00001555 !defined(CONFIG_CPC45) && \
wdenk54070ab2004-12-31 09:32:47 +00001556 !defined(CONFIG_KUP4K) && \
1557 !defined(CONFIG_KUP4X)
wdenkc6097192002-11-03 00:24:07 +00001558
1559static uchar led_buffer = 0; /* Buffer for current LED status */
1560
1561static void ide_led (uchar led, uchar status)
1562{
1563 uchar *led_port = LED_PORT;
1564
1565 if (status) { /* switch LED on */
1566 led_buffer |= led;
1567 } else { /* switch LED off */
1568 led_buffer &= ~led;
1569 }
1570
1571 *led_port = led_buffer;
1572}
1573
1574#endif /* CONFIG_IDE_LED */
1575
Heiko Schocherffb293a2009-09-23 07:56:08 +02001576#if defined(CONFIG_OF_IDE_FIXUP)
1577int ide_device_present(int dev)
1578{
1579 if (dev >= CONFIG_SYS_IDE_MAXBUS)
1580 return 0;
1581 return (ide_dev_desc[dev].type == DEV_TYPE_UNKNOWN ? 0 : 1);
1582}
1583#endif
wdenkc6097192002-11-03 00:24:07 +00001584/* ------------------------------------------------------------------------- */
1585
1586#ifdef CONFIG_ATAPI
1587/****************************************************************************
1588 * ATAPI Support
1589 */
1590
Albert Aribaud036c6b42010-08-08 05:17:05 +05301591#if defined(CONFIG_IDE_SWAP_IO)
wdenkc6097192002-11-03 00:24:07 +00001592/* since ATAPI may use commands with not 4 bytes alligned length
1593 * we have our own transfer functions, 2 bytes alligned */
1594static void
1595output_data_shorts(int dev, ushort *sect_buf, int shorts)
1596{
Wolfgang Denkfb52e952010-09-19 21:28:25 +02001597#if defined(CONFIG_CPC45)
wdenk2f99a692004-01-04 22:51:12 +00001598 uchar *dbuf;
1599 volatile uchar *pbuf_even;
1600 volatile uchar *pbuf_odd;
1601
1602 pbuf_even = (uchar *)(ATA_CURR_BASE(dev)+ATA_DATA_EVEN);
1603 pbuf_odd = (uchar *)(ATA_CURR_BASE(dev)+ATA_DATA_ODD);
1604 while (shorts--) {
wdenk20c98a62004-04-23 20:32:05 +00001605 EIEIO;
wdenk2f99a692004-01-04 22:51:12 +00001606 *pbuf_even = *dbuf++;
wdenk20c98a62004-04-23 20:32:05 +00001607 EIEIO;
wdenk2f99a692004-01-04 22:51:12 +00001608 *pbuf_odd = *dbuf++;
1609 }
wdenkf1276d42005-02-03 23:00:49 +00001610#else
wdenkc6097192002-11-03 00:24:07 +00001611 ushort *dbuf;
1612 volatile ushort *pbuf;
1613
1614 pbuf = (ushort *)(ATA_CURR_BASE(dev)+ATA_DATA_REG);
1615 dbuf = (ushort *)sect_buf;
wdenk634d2f72004-04-15 23:14:49 +00001616
wdenkf1276d42005-02-03 23:00:49 +00001617 debug ("in output data shorts base for read is %lx\n", (unsigned long) pbuf);
wdenk634d2f72004-04-15 23:14:49 +00001618
wdenkc6097192002-11-03 00:24:07 +00001619 while (shorts--) {
wdenk20c98a62004-04-23 20:32:05 +00001620 EIEIO;
wdenkf1276d42005-02-03 23:00:49 +00001621 *pbuf = *dbuf++;
wdenkc6097192002-11-03 00:24:07 +00001622 }
wdenkf1276d42005-02-03 23:00:49 +00001623#endif
1624}
1625
1626static void
1627input_data_shorts(int dev, ushort *sect_buf, int shorts)
1628{
Wolfgang Denkfb52e952010-09-19 21:28:25 +02001629#if defined(CONFIG_CPC45)
wdenk2f99a692004-01-04 22:51:12 +00001630 uchar *dbuf;
1631 volatile uchar *pbuf_even;
1632 volatile uchar *pbuf_odd;
1633
1634 pbuf_even = (uchar *)(ATA_CURR_BASE(dev)+ATA_DATA_EVEN);
1635 pbuf_odd = (uchar *)(ATA_CURR_BASE(dev)+ATA_DATA_ODD);
1636 while (shorts--) {
wdenk20c98a62004-04-23 20:32:05 +00001637 EIEIO;
wdenk2f99a692004-01-04 22:51:12 +00001638 *dbuf++ = *pbuf_even;
wdenk20c98a62004-04-23 20:32:05 +00001639 EIEIO;
wdenk2f99a692004-01-04 22:51:12 +00001640 *dbuf++ = *pbuf_odd;
1641 }
wdenkf1276d42005-02-03 23:00:49 +00001642#else
1643 ushort *dbuf;
1644 volatile ushort *pbuf;
1645
1646 pbuf = (ushort *)(ATA_CURR_BASE(dev)+ATA_DATA_REG);
1647 dbuf = (ushort *)sect_buf;
1648
1649 debug("in input data shorts base for read is %lx\n", (unsigned long) pbuf);
1650
1651 while (shorts--) {
1652 EIEIO;
1653 *dbuf++ = *pbuf;
1654 }
1655#endif
wdenkc6097192002-11-03 00:24:07 +00001656}
1657
Albert Aribaud036c6b42010-08-08 05:17:05 +05301658#else /* ! CONFIG_IDE_SWAP_IO */
wdenk591dda52002-11-18 00:14:45 +00001659static void
1660output_data_shorts(int dev, ushort *sect_buf, int shorts)
1661{
wdenk9f837932003-10-09 19:00:25 +00001662 outsw(ATA_CURR_BASE(dev)+ATA_DATA_REG, sect_buf, shorts);
wdenk591dda52002-11-18 00:14:45 +00001663}
1664
wdenk591dda52002-11-18 00:14:45 +00001665static void
1666input_data_shorts(int dev, ushort *sect_buf, int shorts)
1667{
wdenk9f837932003-10-09 19:00:25 +00001668 insw(ATA_CURR_BASE(dev)+ATA_DATA_REG, sect_buf, shorts);
wdenk591dda52002-11-18 00:14:45 +00001669}
1670
Albert Aribaud036c6b42010-08-08 05:17:05 +05301671#endif /* CONFIG_IDE_SWAP_IO */
wdenk591dda52002-11-18 00:14:45 +00001672
wdenkc6097192002-11-03 00:24:07 +00001673/*
1674 * Wait until (Status & mask) == res, or timeout (in ms)
1675 * Return last status
1676 * This is used since some ATAPI CD ROMs clears their Busy Bit first
1677 * and then they set their DRQ Bit
1678 */
1679static uchar atapi_wait_mask (int dev, ulong t,uchar mask, uchar res)
1680{
1681 ulong delay = 10 * t; /* poll every 100 us */
1682 uchar c;
1683
wdenk591dda52002-11-18 00:14:45 +00001684 c = ide_inb(dev,ATA_DEV_CTL); /* prevents to read the status before valid */
1685 while (((c = ide_inb(dev, ATA_STATUS)) & mask) != res) {
wdenkc6097192002-11-03 00:24:07 +00001686 /* break if error occurs (doesn't make sense to wait more) */
1687 if((c & ATA_STAT_ERR)==ATA_STAT_ERR)
1688 break;
1689 udelay (100);
1690 if (delay-- == 0) {
1691 break;
1692 }
1693 }
1694 return (c);
1695}
1696
1697/*
1698 * issue an atapi command
1699 */
1700unsigned char atapi_issue(int device,unsigned char* ccb,int ccblen, unsigned char * buffer,int buflen)
1701{
1702 unsigned char c,err,mask,res;
1703 int n;
1704 ide_led (DEVICE_LED(device), 1); /* LED on */
1705
1706 /* Select device
1707 */
1708 mask = ATA_STAT_BUSY|ATA_STAT_DRQ;
1709 res = 0;
wdenk591dda52002-11-18 00:14:45 +00001710 ide_outb (device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
wdenkc6097192002-11-03 00:24:07 +00001711 c = atapi_wait_mask(device,ATAPI_TIME_OUT,mask,res);
1712 if ((c & mask) != res) {
1713 printf ("ATAPI_ISSUE: device %d not ready status %X\n", device,c);
1714 err=0xFF;
1715 goto AI_OUT;
1716 }
1717 /* write taskfile */
wdenk591dda52002-11-18 00:14:45 +00001718 ide_outb (device, ATA_ERROR_REG, 0); /* no DMA, no overlaped */
wdenk452cfd62002-11-19 11:04:11 +00001719 ide_outb (device, ATA_SECT_CNT, 0);
1720 ide_outb (device, ATA_SECT_NUM, 0);
wdenk591dda52002-11-18 00:14:45 +00001721 ide_outb (device, ATA_CYL_LOW, (unsigned char)(buflen & 0xFF));
wdenk452cfd62002-11-19 11:04:11 +00001722 ide_outb (device, ATA_CYL_HIGH, (unsigned char)((buflen>>8) & 0xFF));
wdenk591dda52002-11-18 00:14:45 +00001723 ide_outb (device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
wdenkc6097192002-11-03 00:24:07 +00001724
wdenk591dda52002-11-18 00:14:45 +00001725 ide_outb (device, ATA_COMMAND, ATAPI_CMD_PACKET);
wdenkc6097192002-11-03 00:24:07 +00001726 udelay (50);
1727
1728 mask = ATA_STAT_DRQ|ATA_STAT_BUSY|ATA_STAT_ERR;
1729 res = ATA_STAT_DRQ;
1730 c = atapi_wait_mask(device,ATAPI_TIME_OUT,mask,res);
1731
1732 if ((c & mask) != res) { /* DRQ must be 1, BSY 0 */
Steven A. Falco634db892008-08-15 15:37:31 -04001733 printf ("ATAPI_ISSUE: Error (no IRQ) before sending ccb dev %d status 0x%02x\n",device,c);
wdenkc6097192002-11-03 00:24:07 +00001734 err=0xFF;
1735 goto AI_OUT;
1736 }
1737
1738 output_data_shorts (device, (unsigned short *)ccb,ccblen/2); /* write command block */
Wolfgang Denka1be4762008-05-20 16:00:29 +02001739 /* ATAPI Command written wait for completition */
wdenkc6097192002-11-03 00:24:07 +00001740 udelay (5000); /* device must set bsy */
1741
1742 mask = ATA_STAT_DRQ|ATA_STAT_BUSY|ATA_STAT_ERR;
1743 /* if no data wait for DRQ = 0 BSY = 0
1744 * if data wait for DRQ = 1 BSY = 0 */
1745 res=0;
1746 if(buflen)
1747 res = ATA_STAT_DRQ;
1748 c = atapi_wait_mask(device,ATAPI_TIME_OUT,mask,res);
1749 if ((c & mask) != res ) {
1750 if (c & ATA_STAT_ERR) {
wdenk591dda52002-11-18 00:14:45 +00001751 err=(ide_inb(device,ATA_ERROR_REG))>>4;
wdenkf1276d42005-02-03 23:00:49 +00001752 debug ("atapi_issue 1 returned sense key %X status %02X\n",err,c);
wdenkc6097192002-11-03 00:24:07 +00001753 } else {
Steven A. Falco634db892008-08-15 15:37:31 -04001754 printf ("ATAPI_ISSUE: (no DRQ) after sending ccb (%x) status 0x%02x\n", ccb[0],c);
wdenkc6097192002-11-03 00:24:07 +00001755 err=0xFF;
1756 }
1757 goto AI_OUT;
1758 }
wdenk591dda52002-11-18 00:14:45 +00001759 n=ide_inb(device, ATA_CYL_HIGH);
wdenkc6097192002-11-03 00:24:07 +00001760 n<<=8;
wdenk591dda52002-11-18 00:14:45 +00001761 n+=ide_inb(device, ATA_CYL_LOW);
wdenkc6097192002-11-03 00:24:07 +00001762 if(n>buflen) {
1763 printf("ERROR, transfer bytes %d requested only %d\n",n,buflen);
1764 err=0xff;
1765 goto AI_OUT;
1766 }
1767 if((n==0)&&(buflen<0)) {
1768 printf("ERROR, transfer bytes %d requested %d\n",n,buflen);
1769 err=0xff;
1770 goto AI_OUT;
1771 }
1772 if(n!=buflen) {
wdenkf1276d42005-02-03 23:00:49 +00001773 debug ("WARNING, transfer bytes %d not equal with requested %d\n",n,buflen);
wdenkc6097192002-11-03 00:24:07 +00001774 }
1775 if(n!=0) { /* data transfer */
wdenkf1276d42005-02-03 23:00:49 +00001776 debug ("ATAPI_ISSUE: %d Bytes to transfer\n",n);
wdenkc6097192002-11-03 00:24:07 +00001777 /* we transfer shorts */
1778 n>>=1;
1779 /* ok now decide if it is an in or output */
wdenk591dda52002-11-18 00:14:45 +00001780 if ((ide_inb(device, ATA_SECT_CNT)&0x02)==0) {
wdenkf1276d42005-02-03 23:00:49 +00001781 debug ("Write to device\n");
wdenkc6097192002-11-03 00:24:07 +00001782 output_data_shorts(device,(unsigned short *)buffer,n);
1783 } else {
wdenkf1276d42005-02-03 23:00:49 +00001784 debug ("Read from device @ %p shorts %d\n",buffer,n);
wdenkc6097192002-11-03 00:24:07 +00001785 input_data_shorts(device,(unsigned short *)buffer,n);
1786 }
1787 }
1788 udelay(5000); /* seems that some CD ROMs need this... */
1789 mask = ATA_STAT_BUSY|ATA_STAT_ERR;
1790 res=0;
1791 c = atapi_wait_mask(device,ATAPI_TIME_OUT,mask,res);
1792 if ((c & ATA_STAT_ERR) == ATA_STAT_ERR) {
wdenk591dda52002-11-18 00:14:45 +00001793 err=(ide_inb(device,ATA_ERROR_REG) >> 4);
wdenkf1276d42005-02-03 23:00:49 +00001794 debug ("atapi_issue 2 returned sense key %X status %X\n",err,c);
wdenkc6097192002-11-03 00:24:07 +00001795 } else {
1796 err = 0;
1797 }
1798AI_OUT:
1799 ide_led (DEVICE_LED(device), 0); /* LED off */
1800 return (err);
1801}
1802
1803/*
1804 * sending the command to atapi_issue. If an status other than good
1805 * returns, an request_sense will be issued
1806 */
1807
Wolfgang Denka1be4762008-05-20 16:00:29 +02001808#define ATAPI_DRIVE_NOT_READY 100
wdenkc6097192002-11-03 00:24:07 +00001809#define ATAPI_UNIT_ATTN 10
1810
1811unsigned char atapi_issue_autoreq (int device,
1812 unsigned char* ccb,
1813 int ccblen,
1814 unsigned char *buffer,
1815 int buflen)
1816{
1817 unsigned char sense_data[18],sense_ccb[12];
1818 unsigned char res,key,asc,ascq;
1819 int notready,unitattn;
1820
1821 unitattn=ATAPI_UNIT_ATTN;
1822 notready=ATAPI_DRIVE_NOT_READY;
1823
1824retry:
1825 res= atapi_issue(device,ccb,ccblen,buffer,buflen);
1826 if (res==0)
1827 return (0); /* Ok */
1828
1829 if (res==0xFF)
1830 return (0xFF); /* error */
1831
wdenkf1276d42005-02-03 23:00:49 +00001832 debug ("(auto_req)atapi_issue returned sense key %X\n",res);
wdenkc6097192002-11-03 00:24:07 +00001833
1834 memset(sense_ccb,0,sizeof(sense_ccb));
1835 memset(sense_data,0,sizeof(sense_data));
1836 sense_ccb[0]=ATAPI_CMD_REQ_SENSE;
wdenk452cfd62002-11-19 11:04:11 +00001837 sense_ccb[4]=18; /* allocation Length */
wdenkc6097192002-11-03 00:24:07 +00001838
1839 res=atapi_issue(device,sense_ccb,12,sense_data,18);
1840 key=(sense_data[2]&0xF);
1841 asc=(sense_data[12]);
1842 ascq=(sense_data[13]);
1843
wdenkf1276d42005-02-03 23:00:49 +00001844 debug ("ATAPI_CMD_REQ_SENSE returned %x\n",res);
1845 debug (" Sense page: %02X key %02X ASC %02X ASCQ %02X\n",
wdenkc6097192002-11-03 00:24:07 +00001846 sense_data[0],
1847 key,
1848 asc,
1849 ascq);
1850
1851 if((key==0))
1852 return 0; /* ok device ready */
1853
1854 if((key==6)|| (asc==0x29) || (asc==0x28)) { /* Unit Attention */
1855 if(unitattn-->0) {
1856 udelay(200*1000);
1857 goto retry;
1858 }
1859 printf("Unit Attention, tried %d\n",ATAPI_UNIT_ATTN);
1860 goto error;
1861 }
1862 if((asc==0x4) && (ascq==0x1)) { /* not ready, but will be ready soon */
1863 if (notready-->0) {
1864 udelay(200*1000);
1865 goto retry;
1866 }
1867 printf("Drive not ready, tried %d times\n",ATAPI_DRIVE_NOT_READY);
1868 goto error;
1869 }
1870 if(asc==0x3a) {
wdenkf1276d42005-02-03 23:00:49 +00001871 debug ("Media not present\n");
wdenkc6097192002-11-03 00:24:07 +00001872 goto error;
1873 }
wdenk452cfd62002-11-19 11:04:11 +00001874
wdenkc6097192002-11-03 00:24:07 +00001875 printf ("ERROR: Unknown Sense key %02X ASC %02X ASCQ %02X\n",key,asc,ascq);
1876error:
wdenkf1276d42005-02-03 23:00:49 +00001877 debug ("ERROR Sense key %02X ASC %02X ASCQ %02X\n",key,asc,ascq);
wdenkc6097192002-11-03 00:24:07 +00001878 return (0xFF);
1879}
1880
1881
wdenkc6097192002-11-03 00:24:07 +00001882static void atapi_inquiry(block_dev_desc_t * dev_desc)
1883{
1884 unsigned char ccb[12]; /* Command descriptor block */
1885 unsigned char iobuf[64]; /* temp buf */
1886 unsigned char c;
1887 int device;
1888
1889 device=dev_desc->dev;
1890 dev_desc->type=DEV_TYPE_UNKNOWN; /* not yet valid */
1891 dev_desc->block_read=atapi_read;
1892
1893 memset(ccb,0,sizeof(ccb));
1894 memset(iobuf,0,sizeof(iobuf));
1895
1896 ccb[0]=ATAPI_CMD_INQUIRY;
1897 ccb[4]=40; /* allocation Legnth */
1898 c=atapi_issue_autoreq(device,ccb,12,(unsigned char *)iobuf,40);
1899
wdenkf1276d42005-02-03 23:00:49 +00001900 debug ("ATAPI_CMD_INQUIRY returned %x\n",c);
wdenkc6097192002-11-03 00:24:07 +00001901 if (c!=0)
1902 return;
1903
1904 /* copy device ident strings */
Jean-Christophe PLAGNIOL-VILLARD1fa3e362007-11-07 08:19:19 +01001905 ident_cpy((unsigned char*)dev_desc->vendor,&iobuf[8],8);
1906 ident_cpy((unsigned char*)dev_desc->product,&iobuf[16],16);
1907 ident_cpy((unsigned char*)dev_desc->revision,&iobuf[32],5);
wdenkc6097192002-11-03 00:24:07 +00001908
1909 dev_desc->lun=0;
1910 dev_desc->lba=0;
1911 dev_desc->blksz=0;
1912 dev_desc->type=iobuf[0] & 0x1f;
1913
1914 if ((iobuf[1]&0x80)==0x80)
1915 dev_desc->removable = 1;
1916 else
1917 dev_desc->removable = 0;
1918
1919 memset(ccb,0,sizeof(ccb));
1920 memset(iobuf,0,sizeof(iobuf));
1921 ccb[0]=ATAPI_CMD_START_STOP;
1922 ccb[4]=0x03; /* start */
1923
1924 c=atapi_issue_autoreq(device,ccb,12,(unsigned char *)iobuf,0);
1925
wdenkf1276d42005-02-03 23:00:49 +00001926 debug ("ATAPI_CMD_START_STOP returned %x\n",c);
wdenkc6097192002-11-03 00:24:07 +00001927 if (c!=0)
1928 return;
1929
1930 memset(ccb,0,sizeof(ccb));
1931 memset(iobuf,0,sizeof(iobuf));
1932 c=atapi_issue_autoreq(device,ccb,12,(unsigned char *)iobuf,0);
1933
wdenkf1276d42005-02-03 23:00:49 +00001934 debug ("ATAPI_CMD_UNIT_TEST_READY returned %x\n",c);
wdenkc6097192002-11-03 00:24:07 +00001935 if (c!=0)
1936 return;
1937
1938 memset(ccb,0,sizeof(ccb));
1939 memset(iobuf,0,sizeof(iobuf));
1940 ccb[0]=ATAPI_CMD_READ_CAP;
1941 c=atapi_issue_autoreq(device,ccb,12,(unsigned char *)iobuf,8);
wdenkf1276d42005-02-03 23:00:49 +00001942 debug ("ATAPI_CMD_READ_CAP returned %x\n",c);
wdenkc6097192002-11-03 00:24:07 +00001943 if (c!=0)
1944 return;
1945
wdenkf1276d42005-02-03 23:00:49 +00001946 debug ("Read Cap: LBA %02X%02X%02X%02X blksize %02X%02X%02X%02X\n",
wdenkc6097192002-11-03 00:24:07 +00001947 iobuf[0],iobuf[1],iobuf[2],iobuf[3],
1948 iobuf[4],iobuf[5],iobuf[6],iobuf[7]);
1949
1950 dev_desc->lba =((unsigned long)iobuf[0]<<24) +
1951 ((unsigned long)iobuf[1]<<16) +
1952 ((unsigned long)iobuf[2]<< 8) +
1953 ((unsigned long)iobuf[3]);
1954 dev_desc->blksz=((unsigned long)iobuf[4]<<24) +
1955 ((unsigned long)iobuf[5]<<16) +
1956 ((unsigned long)iobuf[6]<< 8) +
1957 ((unsigned long)iobuf[7]);
wdenkc35ba4e2004-03-14 22:25:36 +00001958#ifdef CONFIG_LBA48
wdenkf602aa02004-03-13 23:29:43 +00001959 dev_desc->lba48 = 0; /* ATAPI devices cannot use 48bit addressing (ATA/ATAPI v7) */
wdenkc35ba4e2004-03-14 22:25:36 +00001960#endif
wdenkc6097192002-11-03 00:24:07 +00001961 return;
1962}
1963
1964
1965/*
1966 * atapi_read:
1967 * we transfer only one block per command, since the multiple DRQ per
1968 * command is not yet implemented
1969 */
1970#define ATAPI_READ_MAX_BYTES 2048 /* we read max 2kbytes */
1971#define ATAPI_READ_BLOCK_SIZE 2048 /* assuming CD part */
1972#define ATAPI_READ_MAX_BLOCK ATAPI_READ_MAX_BYTES/ATAPI_READ_BLOCK_SIZE /* max blocks */
1973
Grant Likely2089cab2007-02-20 09:05:45 +01001974ulong atapi_read (int device, lbaint_t blknr, ulong blkcnt, void *buffer)
wdenkc6097192002-11-03 00:24:07 +00001975{
1976 ulong n = 0;
1977 unsigned char ccb[12]; /* Command descriptor block */
1978 ulong cnt;
1979
wdenkf1276d42005-02-03 23:00:49 +00001980 debug ("atapi_read dev %d start %lX, blocks %lX buffer at %lX\n",
wdenkc6097192002-11-03 00:24:07 +00001981 device, blknr, blkcnt, (ulong)buffer);
1982
1983 do {
1984 if (blkcnt>ATAPI_READ_MAX_BLOCK) {
1985 cnt=ATAPI_READ_MAX_BLOCK;
1986 } else {
1987 cnt=blkcnt;
1988 }
1989 ccb[0]=ATAPI_CMD_READ_12;
1990 ccb[1]=0; /* reserved */
1991 ccb[2]=(unsigned char) (blknr>>24) & 0xFF; /* MSB Block */
1992 ccb[3]=(unsigned char) (blknr>>16) & 0xFF; /* */
1993 ccb[4]=(unsigned char) (blknr>> 8) & 0xFF;
1994 ccb[5]=(unsigned char) blknr & 0xFF; /* LSB Block */
1995 ccb[6]=(unsigned char) (cnt >>24) & 0xFF; /* MSB Block count */
1996 ccb[7]=(unsigned char) (cnt >>16) & 0xFF;
1997 ccb[8]=(unsigned char) (cnt >> 8) & 0xFF;
1998 ccb[9]=(unsigned char) cnt & 0xFF; /* LSB Block */
1999 ccb[10]=0; /* reserved */
2000 ccb[11]=0; /* reserved */
2001
2002 if (atapi_issue_autoreq(device,ccb,12,
2003 (unsigned char *)buffer,
2004 cnt*ATAPI_READ_BLOCK_SIZE) == 0xFF) {
2005 return (n);
2006 }
2007 n+=cnt;
2008 blkcnt-=cnt;
2009 blknr+=cnt;
Greg Lopp50abf9f2007-04-13 08:02:24 +02002010 buffer+=(cnt*ATAPI_READ_BLOCK_SIZE);
wdenkc6097192002-11-03 00:24:07 +00002011 } while (blkcnt > 0);
2012 return (n);
2013}
2014
2015/* ------------------------------------------------------------------------- */
2016
2017#endif /* CONFIG_ATAPI */
2018
wdenkf287a242003-07-01 21:06:45 +00002019U_BOOT_CMD(
2020 ide, 5, 1, do_ide,
Peter Tyserdfb72b82009-01-27 18:03:12 -06002021 "IDE sub-system",
wdenk57b2d802003-06-27 21:31:46 +00002022 "reset - reset IDE controller\n"
2023 "ide info - show available IDE devices\n"
2024 "ide device [dev] - show or set current device\n"
2025 "ide part [dev] - print partition table of one or all IDE devices\n"
2026 "ide read addr blk# cnt\n"
2027 "ide write addr blk# cnt - read/write `cnt'"
2028 " blocks starting at block `blk#'\n"
Wolfgang Denkc54781c2009-05-24 17:06:54 +02002029 " to/from memory address `addr'"
wdenk57b2d802003-06-27 21:31:46 +00002030);
2031
wdenkf287a242003-07-01 21:06:45 +00002032U_BOOT_CMD(
2033 diskboot, 3, 1, do_diskboot,
Peter Tyserdfb72b82009-01-27 18:03:12 -06002034 "boot from IDE device",
Wolfgang Denkc54781c2009-05-24 17:06:54 +02002035 "loadAddr dev:part"
wdenk57b2d802003-06-27 21:31:46 +00002036);