blob: bae7767310fc7e479f27ead114a42e602b6b089b [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{
Rob Herringa20cbf92012-09-21 04:02:30 +0000337 return common_diskboot(cmdtp, "ide", argc, argv);
wdenkc6097192002-11-03 00:24:07 +0000338}
339
340/* ------------------------------------------------------------------------- */
341
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000342inline void __ide_outb(int dev, int port, unsigned char val)
Stefan Roese37628252008-08-06 14:05:38 +0200343{
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000344 debug("ide_outb (dev= %d, port= 0x%x, val= 0x%02x) : @ 0x%08lx\n",
345 dev, port, val,
346 (ATA_CURR_BASE(dev) + CONFIG_SYS_ATA_PORT_ADDR(port)));
Macpaul Lind1e49942011-04-11 20:45:32 +0000347
348#if defined(CONFIG_IDE_AHB)
349 if (port) {
350 /* write command */
351 ide_write_register(dev, port, val);
352 } else {
353 /* write data */
354 outb(val, (ATA_CURR_BASE(dev)));
355 }
356#else
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000357 outb(val, (ATA_CURR_BASE(dev) + CONFIG_SYS_ATA_PORT_ADDR(port)));
Macpaul Lind1e49942011-04-11 20:45:32 +0000358#endif
Stefan Roese37628252008-08-06 14:05:38 +0200359}
Macpaul Lind1e49942011-04-11 20:45:32 +0000360
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000361void ide_outb(int dev, int port, unsigned char val)
362 __attribute__ ((weak, alias("__ide_outb")));
Stefan Roese37628252008-08-06 14:05:38 +0200363
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000364inline unsigned char __ide_inb(int dev, int port)
Stefan Roese37628252008-08-06 14:05:38 +0200365{
366 uchar val;
Macpaul Lind1e49942011-04-11 20:45:32 +0000367
368#if defined(CONFIG_IDE_AHB)
369 val = ide_read_register(dev, port);
370#else
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000371 val = inb((ATA_CURR_BASE(dev) + CONFIG_SYS_ATA_PORT_ADDR(port)));
Macpaul Lind1e49942011-04-11 20:45:32 +0000372#endif
373
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000374 debug("ide_inb (dev= %d, port= 0x%x) : @ 0x%08lx -> 0x%02x\n",
375 dev, port,
376 (ATA_CURR_BASE(dev) + CONFIG_SYS_ATA_PORT_ADDR(port)), val);
Stefan Roese37628252008-08-06 14:05:38 +0200377 return val;
378}
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000379
Kim Phillips8167f3c2009-05-19 12:53:36 -0500380unsigned char ide_inb(int dev, int port)
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000381 __attribute__ ((weak, alias("__ide_inb")));
Stefan Roese37628252008-08-06 14:05:38 +0200382
Steven A. Falco2b7574e2008-08-15 15:34:10 -0400383#ifdef CONFIG_TUNE_PIO
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000384inline int __ide_set_piomode(int pio_mode)
Steven A. Falco2b7574e2008-08-15 15:34:10 -0400385{
386 return 0;
387}
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000388
389inline int ide_set_piomode(int pio_mode)
390 __attribute__ ((weak, alias("__ide_set_piomode")));
Steven A. Falco2b7574e2008-08-15 15:34:10 -0400391#endif
392
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000393void ide_init(void)
wdenkc6097192002-11-03 00:24:07 +0000394{
wdenkc6097192002-11-03 00:24:07 +0000395
396#ifdef CONFIG_IDE_8xx_DIRECT
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000397 volatile immap_t *immr = (immap_t *) CONFIG_SYS_IMMR;
wdenkc6097192002-11-03 00:24:07 +0000398 volatile pcmconf8xx_t *pcmp = &(immr->im_pcmcia);
399#endif
400 unsigned char c;
401 int i, bus;
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000402
Wolfgang Denkb0b104a2010-06-13 18:28:54 +0200403#if defined(CONFIG_SC3)
Wolfgang Denkd2a7a742007-06-08 10:24:58 +0200404 unsigned int ata_reset_time = ATA_RESET_TIME;
Wolfgang Denk15c6b152007-04-11 17:22:55 +0200405#endif
wdenk7f00b942003-12-07 23:55:12 +0000406#ifdef CONFIG_IDE_8xx_PCCARD
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000407 extern int pcmcia_on(void);
408 extern int ide_devices_found; /* Initialized in check_ide_device() */
409#endif /* CONFIG_IDE_8xx_PCCARD */
wdenk7f00b942003-12-07 23:55:12 +0000410
411#ifdef CONFIG_IDE_PREINIT
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000412 extern int ide_preinit(void);
413
wdenk7f00b942003-12-07 23:55:12 +0000414 WATCHDOG_RESET();
415
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000416 if (ide_preinit()) {
417 puts("ide_preinit failed\n");
wdenk7f00b942003-12-07 23:55:12 +0000418 return;
419 }
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000420#endif /* CONFIG_IDE_PREINIT */
wdenkc6097192002-11-03 00:24:07 +0000421
422#ifdef CONFIG_IDE_8xx_PCCARD
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000423 extern int pcmcia_on(void);
424 extern int ide_devices_found; /* Initialized in check_ide_device() */
wdenkc6097192002-11-03 00:24:07 +0000425
426 WATCHDOG_RESET();
427
wdenk4fc95692003-02-28 00:49:47 +0000428 ide_devices_found = 0;
wdenkc6097192002-11-03 00:24:07 +0000429 /* initialize the PCMCIA IDE adapter card */
wdenk4fc95692003-02-28 00:49:47 +0000430 pcmcia_on();
431 if (!ide_devices_found)
wdenkc6097192002-11-03 00:24:07 +0000432 return;
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000433 udelay(1000000); /* 1 s */
434#endif /* CONFIG_IDE_8xx_PCCARD */
wdenkc6097192002-11-03 00:24:07 +0000435
436 WATCHDOG_RESET();
437
wdenk9f837932003-10-09 19:00:25 +0000438#ifdef CONFIG_IDE_8xx_DIRECT
wdenkc6097192002-11-03 00:24:07 +0000439 /* Initialize PIO timing tables */
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000440 for (i = 0; i <= IDE_MAX_PIO_MODE; ++i) {
441 pio_config_clk[i].t_setup =
442 PCMCIA_MK_CLKS(pio_config_ns[i].t_setup, gd->bus_clk);
443 pio_config_clk[i].t_length =
444 PCMCIA_MK_CLKS(pio_config_ns[i].t_length,
445 gd->bus_clk);
446 pio_config_clk[i].t_hold =
447 PCMCIA_MK_CLKS(pio_config_ns[i].t_hold, gd->bus_clk);
448 debug("PIO Mode %d: setup=%2d ns/%d clk" " len=%3d ns/%d clk"
449 " hold=%2d ns/%d clk\n", i, pio_config_ns[i].t_setup,
450 pio_config_clk[i].t_setup, pio_config_ns[i].t_length,
451 pio_config_clk[i].t_length, pio_config_ns[i].t_hold,
452 pio_config_clk[i].t_hold);
wdenkc6097192002-11-03 00:24:07 +0000453 }
wdenk9f837932003-10-09 19:00:25 +0000454#endif /* CONFIG_IDE_8xx_DIRECT */
wdenkc6097192002-11-03 00:24:07 +0000455
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000456 /*
457 * Reset the IDE just to be sure.
wdenkc6097192002-11-03 00:24:07 +0000458 * Light LED's to show
459 */
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000460 ide_led((LED_IDE1 | LED_IDE2), 1); /* LED's on */
461
462 /* ATAPI Drives seems to need a proper IDE Reset */
463 ide_reset();
wdenkc6097192002-11-03 00:24:07 +0000464
465#ifdef CONFIG_IDE_8xx_DIRECT
466 /* PCMCIA / IDE initialization for common mem space */
467 pcmp->pcmc_pgcrb = 0;
wdenkc6097192002-11-03 00:24:07 +0000468
469 /* start in PIO mode 0 - most relaxed timings */
470 pio_mode = 0;
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000471 set_pcmcia_timing(pio_mode);
wdenk9f837932003-10-09 19:00:25 +0000472#endif /* CONFIG_IDE_8xx_DIRECT */
wdenkc6097192002-11-03 00:24:07 +0000473
474 /*
475 * Wait for IDE to get ready.
476 * According to spec, this can take up to 31 seconds!
477 */
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000478 for (bus = 0; bus < CONFIG_SYS_IDE_MAXBUS; ++bus) {
479 int dev =
480 bus * (CONFIG_SYS_IDE_MAXDEVICE /
481 CONFIG_SYS_IDE_MAXBUS);
wdenkc6097192002-11-03 00:24:07 +0000482
wdenk4fc95692003-02-28 00:49:47 +0000483#ifdef CONFIG_IDE_8xx_PCCARD
484 /* Skip non-ide devices from probing */
485 if ((ide_devices_found & (1 << bus)) == 0) {
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000486 ide_led((LED_IDE1 | LED_IDE2), 0); /* LED's off */
wdenk4fc95692003-02-28 00:49:47 +0000487 continue;
488 }
489#endif
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000490 printf("Bus %d: ", bus);
wdenkc6097192002-11-03 00:24:07 +0000491
492 ide_bus_ok[bus] = 0;
493
494 /* Select device
495 */
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000496 udelay(100000); /* 100 ms */
497 ide_outb(dev, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(dev));
498 udelay(100000); /* 100 ms */
wdenkc6097192002-11-03 00:24:07 +0000499 i = 0;
500 do {
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000501 udelay(10000); /* 10 ms */
wdenkc6097192002-11-03 00:24:07 +0000502
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000503 c = ide_inb(dev, ATA_STATUS);
wdenkc6097192002-11-03 00:24:07 +0000504 i++;
Wolfgang Denkb0b104a2010-06-13 18:28:54 +0200505#if defined(CONFIG_SC3)
wdenk452cfd62002-11-19 11:04:11 +0000506 if (i > (ata_reset_time * 100)) {
507#else
wdenkc6097192002-11-03 00:24:07 +0000508 if (i > (ATA_RESET_TIME * 100)) {
wdenk452cfd62002-11-19 11:04:11 +0000509#endif
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000510 puts("** Timeout **\n");
511 /* LED's off */
512 ide_led((LED_IDE1 | LED_IDE2), 0);
wdenkc6097192002-11-03 00:24:07 +0000513 return;
514 }
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000515 if ((i >= 100) && ((i % 100) == 0))
516 putc('.');
517
wdenkc6097192002-11-03 00:24:07 +0000518 } while (c & ATA_STAT_BUSY);
519
520 if (c & (ATA_STAT_BUSY | ATA_STAT_FAULT)) {
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000521 puts("not available ");
522 debug("Status = 0x%02X ", c);
523#ifndef CONFIG_ATAPI /* ATAPI Devices do not set DRDY */
524 } else if ((c & ATA_STAT_READY) == 0) {
525 puts("not available ");
526 debug("Status = 0x%02X ", c);
wdenkc6097192002-11-03 00:24:07 +0000527#endif
528 } else {
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000529 puts("OK ");
wdenkc6097192002-11-03 00:24:07 +0000530 ide_bus_ok[bus] = 1;
531 }
532 WATCHDOG_RESET();
533 }
wdenk452cfd62002-11-19 11:04:11 +0000534
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000535 putc('\n');
wdenkc6097192002-11-03 00:24:07 +0000536
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000537 ide_led((LED_IDE1 | LED_IDE2), 0); /* LED's off */
wdenkc6097192002-11-03 00:24:07 +0000538
539 curr_device = -1;
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000540 for (i = 0; i < CONFIG_SYS_IDE_MAXDEVICE; ++i) {
wdenkc6097192002-11-03 00:24:07 +0000541#ifdef CONFIG_IDE_LED
542 int led = (IDE_BUS(i) == 0) ? LED_IDE1 : LED_IDE2;
543#endif
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000544 ide_dev_desc[i].type = DEV_TYPE_UNKNOWN;
545 ide_dev_desc[i].if_type = IF_TYPE_IDE;
546 ide_dev_desc[i].dev = i;
547 ide_dev_desc[i].part_type = PART_TYPE_UNKNOWN;
548 ide_dev_desc[i].blksz = 0;
549 ide_dev_desc[i].lba = 0;
550 ide_dev_desc[i].block_read = ide_read;
Macpaul Lind1e49942011-04-11 20:45:32 +0000551 ide_dev_desc[i].block_write = ide_write;
wdenkc6097192002-11-03 00:24:07 +0000552 if (!ide_bus_ok[IDE_BUS(i)])
553 continue;
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000554 ide_led(led, 1); /* LED on */
wdenkc6097192002-11-03 00:24:07 +0000555 ide_ident(&ide_dev_desc[i]);
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000556 ide_led(led, 0); /* LED off */
wdenkc6097192002-11-03 00:24:07 +0000557 dev_print(&ide_dev_desc[i]);
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000558
wdenkc6097192002-11-03 00:24:07 +0000559 if ((ide_dev_desc[i].lba > 0) && (ide_dev_desc[i].blksz > 0)) {
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000560 /* initialize partition type */
561 init_part(&ide_dev_desc[i]);
wdenkc6097192002-11-03 00:24:07 +0000562 if (curr_device < 0)
563 curr_device = i;
564 }
565 }
566 WATCHDOG_RESET();
567}
568
569/* ------------------------------------------------------------------------- */
570
Matthew McClintock6252b4f2011-05-24 05:31:19 +0000571#ifdef CONFIG_PARTITIONS
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000572block_dev_desc_t *ide_get_dev(int dev)
wdenkc6097192002-11-03 00:24:07 +0000573{
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200574 return (dev < CONFIG_SYS_IDE_MAXDEVICE) ? &ide_dev_desc[dev] : NULL;
wdenkc6097192002-11-03 00:24:07 +0000575}
Matthew McClintock6252b4f2011-05-24 05:31:19 +0000576#endif
wdenkc6097192002-11-03 00:24:07 +0000577
578
579#ifdef CONFIG_IDE_8xx_DIRECT
580
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000581static void set_pcmcia_timing(int pmode)
wdenkc6097192002-11-03 00:24:07 +0000582{
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000583 volatile immap_t *immr = (immap_t *) CONFIG_SYS_IMMR;
wdenkc6097192002-11-03 00:24:07 +0000584 volatile pcmconf8xx_t *pcmp = &(immr->im_pcmcia);
585 ulong timings;
586
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000587 debug("Set timing for PIO Mode %d\n", pmode);
wdenkc6097192002-11-03 00:24:07 +0000588
589 timings = PCMCIA_SHT(pio_config_clk[pmode].t_hold)
590 | PCMCIA_SST(pio_config_clk[pmode].t_setup)
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000591 | PCMCIA_SL(pio_config_clk[pmode].t_length);
wdenkc6097192002-11-03 00:24:07 +0000592
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000593 /*
594 * IDE 0
wdenkc6097192002-11-03 00:24:07 +0000595 */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200596 pcmp->pcmc_pbr0 = CONFIG_SYS_PCMCIA_PBR0;
597 pcmp->pcmc_por0 = CONFIG_SYS_PCMCIA_POR0
598#if (CONFIG_SYS_PCMCIA_POR0 != 0)
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000599 | timings
wdenkc6097192002-11-03 00:24:07 +0000600#endif
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000601 ;
602 debug("PBR0: %08x POR0: %08x\n", pcmp->pcmc_pbr0, pcmp->pcmc_por0);
wdenkc6097192002-11-03 00:24:07 +0000603
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200604 pcmp->pcmc_pbr1 = CONFIG_SYS_PCMCIA_PBR1;
605 pcmp->pcmc_por1 = CONFIG_SYS_PCMCIA_POR1
606#if (CONFIG_SYS_PCMCIA_POR1 != 0)
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000607 | timings
wdenkc6097192002-11-03 00:24:07 +0000608#endif
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000609 ;
610 debug("PBR1: %08x POR1: %08x\n", pcmp->pcmc_pbr1, pcmp->pcmc_por1);
wdenkc6097192002-11-03 00:24:07 +0000611
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200612 pcmp->pcmc_pbr2 = CONFIG_SYS_PCMCIA_PBR2;
613 pcmp->pcmc_por2 = CONFIG_SYS_PCMCIA_POR2
614#if (CONFIG_SYS_PCMCIA_POR2 != 0)
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000615 | timings
wdenkc6097192002-11-03 00:24:07 +0000616#endif
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000617 ;
618 debug("PBR2: %08x POR2: %08x\n", pcmp->pcmc_pbr2, pcmp->pcmc_por2);
wdenkc6097192002-11-03 00:24:07 +0000619
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200620 pcmp->pcmc_pbr3 = CONFIG_SYS_PCMCIA_PBR3;
621 pcmp->pcmc_por3 = CONFIG_SYS_PCMCIA_POR3
622#if (CONFIG_SYS_PCMCIA_POR3 != 0)
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000623 | timings
wdenkc6097192002-11-03 00:24:07 +0000624#endif
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000625 ;
626 debug("PBR3: %08x POR3: %08x\n", pcmp->pcmc_pbr3, pcmp->pcmc_por3);
wdenkc6097192002-11-03 00:24:07 +0000627
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000628 /*
629 * IDE 1
wdenkc6097192002-11-03 00:24:07 +0000630 */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200631 pcmp->pcmc_pbr4 = CONFIG_SYS_PCMCIA_PBR4;
632 pcmp->pcmc_por4 = CONFIG_SYS_PCMCIA_POR4
633#if (CONFIG_SYS_PCMCIA_POR4 != 0)
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000634 | timings
wdenkc6097192002-11-03 00:24:07 +0000635#endif
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000636 ;
637 debug("PBR4: %08x POR4: %08x\n", pcmp->pcmc_pbr4, pcmp->pcmc_por4);
wdenkc6097192002-11-03 00:24:07 +0000638
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200639 pcmp->pcmc_pbr5 = CONFIG_SYS_PCMCIA_PBR5;
640 pcmp->pcmc_por5 = CONFIG_SYS_PCMCIA_POR5
641#if (CONFIG_SYS_PCMCIA_POR5 != 0)
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000642 | timings
wdenkc6097192002-11-03 00:24:07 +0000643#endif
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000644 ;
645 debug("PBR5: %08x POR5: %08x\n", pcmp->pcmc_pbr5, pcmp->pcmc_por5);
wdenkc6097192002-11-03 00:24:07 +0000646
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200647 pcmp->pcmc_pbr6 = CONFIG_SYS_PCMCIA_PBR6;
648 pcmp->pcmc_por6 = CONFIG_SYS_PCMCIA_POR6
649#if (CONFIG_SYS_PCMCIA_POR6 != 0)
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000650 | timings
wdenkc6097192002-11-03 00:24:07 +0000651#endif
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000652 ;
653 debug("PBR6: %08x POR6: %08x\n", pcmp->pcmc_pbr6, pcmp->pcmc_por6);
wdenkc6097192002-11-03 00:24:07 +0000654
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200655 pcmp->pcmc_pbr7 = CONFIG_SYS_PCMCIA_PBR7;
656 pcmp->pcmc_por7 = CONFIG_SYS_PCMCIA_POR7
657#if (CONFIG_SYS_PCMCIA_POR7 != 0)
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000658 | timings
wdenkc6097192002-11-03 00:24:07 +0000659#endif
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000660 ;
661 debug("PBR7: %08x POR7: %08x\n", pcmp->pcmc_pbr7, pcmp->pcmc_por7);
wdenkc6097192002-11-03 00:24:07 +0000662
663}
664
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000665#endif /* CONFIG_IDE_8xx_DIRECT */
wdenkc6097192002-11-03 00:24:07 +0000666
667/* ------------------------------------------------------------------------- */
668
wdenk9b7f3842003-10-09 20:09:04 +0000669/* We only need to swap data if we are running on a big endian cpu. */
670/* But Au1x00 cpu:s already swaps data in big endian mode! */
Tom Rini31327152012-09-26 15:20:33 -0700671#if defined(__LITTLE_ENDIAN) || defined(CONFIG_SOC_AU1X00)
wdenk9b7f3842003-10-09 20:09:04 +0000672#define input_swap_data(x,y,z) input_data(x,y,z)
673#else
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000674static void input_swap_data(int dev, ulong *sect_buf, int words)
wdenkc6097192002-11-03 00:24:07 +0000675{
Wolfgang Denkfb52e952010-09-19 21:28:25 +0200676#if defined(CONFIG_CPC45)
wdenk2f99a692004-01-04 22:51:12 +0000677 uchar i;
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000678 volatile uchar *pbuf_even =
679 (uchar *) (ATA_CURR_BASE(dev) + ATA_DATA_EVEN);
680 volatile uchar *pbuf_odd =
681 (uchar *) (ATA_CURR_BASE(dev) + ATA_DATA_ODD);
682 ushort *dbuf = (ushort *) sect_buf;
wdenk2f99a692004-01-04 22:51:12 +0000683
684 while (words--) {
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000685 for (i = 0; i < 2; i++) {
686 *(((uchar *) (dbuf)) + 1) = *pbuf_even;
687 *(uchar *) dbuf = *pbuf_odd;
688 dbuf += 1;
wdenk2f99a692004-01-04 22:51:12 +0000689 }
690 }
wdenkb995b0f2005-03-06 01:21:30 +0000691#else
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000692 volatile ushort *pbuf =
693 (ushort *) (ATA_CURR_BASE(dev) + ATA_DATA_REG);
694 ushort *dbuf = (ushort *) sect_buf;
wdenkf1276d42005-02-03 23:00:49 +0000695
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000696 debug("in input swap data base for read is %lx\n",
697 (unsigned long) pbuf);
wdenkf1276d42005-02-03 23:00:49 +0000698
699 while (words--) {
Wolfgang Denk39c76422006-06-16 17:32:31 +0200700#ifdef __MIPS__
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000701 *dbuf++ = swab16p((u16 *) pbuf);
702 *dbuf++ = swab16p((u16 *) pbuf);
Heiko Schocher633e03a2007-06-22 19:11:54 +0200703#elif defined(CONFIG_PCS440EP)
704 *dbuf++ = *pbuf;
705 *dbuf++ = *pbuf;
Wolfgang Denk39c76422006-06-16 17:32:31 +0200706#else
wdenkf1276d42005-02-03 23:00:49 +0000707 *dbuf++ = ld_le16(pbuf);
708 *dbuf++ = ld_le16(pbuf);
Wolfgang Denk39c76422006-06-16 17:32:31 +0200709#endif /* !MIPS */
wdenkf1276d42005-02-03 23:00:49 +0000710 }
711#endif
wdenkc6097192002-11-03 00:24:07 +0000712}
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000713#endif /* __LITTLE_ENDIAN || CONFIG_AU1X00 */
wdenk591dda52002-11-18 00:14:45 +0000714
715
Albert Aribaud036c6b42010-08-08 05:17:05 +0530716#if defined(CONFIG_IDE_SWAP_IO)
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000717static void output_data(int dev, const ulong *sect_buf, int words)
wdenkc6097192002-11-03 00:24:07 +0000718{
Wolfgang Denkfb52e952010-09-19 21:28:25 +0200719#if defined(CONFIG_CPC45)
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000720 uchar *dbuf;
721 volatile uchar *pbuf_even;
722 volatile uchar *pbuf_odd;
wdenk2f99a692004-01-04 22:51:12 +0000723
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000724 pbuf_even = (uchar *) (ATA_CURR_BASE(dev) + ATA_DATA_EVEN);
725 pbuf_odd = (uchar *) (ATA_CURR_BASE(dev) + ATA_DATA_ODD);
726 dbuf = (uchar *) sect_buf;
wdenk2f99a692004-01-04 22:51:12 +0000727 while (words--) {
wdenk20c98a62004-04-23 20:32:05 +0000728 EIEIO;
wdenk2f99a692004-01-04 22:51:12 +0000729 *pbuf_even = *dbuf++;
wdenk20c98a62004-04-23 20:32:05 +0000730 EIEIO;
wdenk2f99a692004-01-04 22:51:12 +0000731 *pbuf_odd = *dbuf++;
wdenk20c98a62004-04-23 20:32:05 +0000732 EIEIO;
wdenk2f99a692004-01-04 22:51:12 +0000733 *pbuf_even = *dbuf++;
wdenk20c98a62004-04-23 20:32:05 +0000734 EIEIO;
wdenk2f99a692004-01-04 22:51:12 +0000735 *pbuf_odd = *dbuf++;
736 }
wdenkf1276d42005-02-03 23:00:49 +0000737#else
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000738 ushort *dbuf;
739 volatile ushort *pbuf;
wdenkf1276d42005-02-03 23:00:49 +0000740
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000741 pbuf = (ushort *) (ATA_CURR_BASE(dev) + ATA_DATA_REG);
742 dbuf = (ushort *) sect_buf;
wdenkf1276d42005-02-03 23:00:49 +0000743 while (words--) {
Heiko Schocher633e03a2007-06-22 19:11:54 +0200744#if defined(CONFIG_PCS440EP)
745 /* not tested, because CF was write protected */
746 EIEIO;
747 *pbuf = ld_le16(dbuf++);
748 EIEIO;
749 *pbuf = ld_le16(dbuf++);
750#else
wdenkf1276d42005-02-03 23:00:49 +0000751 EIEIO;
752 *pbuf = *dbuf++;
753 EIEIO;
754 *pbuf = *dbuf++;
Heiko Schocher633e03a2007-06-22 19:11:54 +0200755#endif
wdenkf1276d42005-02-03 23:00:49 +0000756 }
757#endif
wdenkc6097192002-11-03 00:24:07 +0000758}
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000759#else /* ! CONFIG_IDE_SWAP_IO */
760static void output_data(int dev, const ulong *sect_buf, int words)
wdenk591dda52002-11-18 00:14:45 +0000761{
Macpaul Lind1e49942011-04-11 20:45:32 +0000762#if defined(CONFIG_IDE_AHB)
763 ide_write_data(dev, sect_buf, words);
764#else
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000765 outsw(ATA_CURR_BASE(dev) + ATA_DATA_REG, sect_buf, words << 1);
Macpaul Lind1e49942011-04-11 20:45:32 +0000766#endif
wdenk591dda52002-11-18 00:14:45 +0000767}
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000768#endif /* CONFIG_IDE_SWAP_IO */
wdenkc6097192002-11-03 00:24:07 +0000769
Albert Aribaud036c6b42010-08-08 05:17:05 +0530770#if defined(CONFIG_IDE_SWAP_IO)
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000771static void input_data(int dev, ulong *sect_buf, int words)
wdenkc6097192002-11-03 00:24:07 +0000772{
Wolfgang Denkfb52e952010-09-19 21:28:25 +0200773#if defined(CONFIG_CPC45)
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000774 uchar *dbuf;
775 volatile uchar *pbuf_even;
776 volatile uchar *pbuf_odd;
wdenk2f99a692004-01-04 22:51:12 +0000777
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000778 pbuf_even = (uchar *) (ATA_CURR_BASE(dev) + ATA_DATA_EVEN);
779 pbuf_odd = (uchar *) (ATA_CURR_BASE(dev) + ATA_DATA_ODD);
780 dbuf = (uchar *) sect_buf;
wdenk2f99a692004-01-04 22:51:12 +0000781 while (words--) {
wdenkf1276d42005-02-03 23:00:49 +0000782 *dbuf++ = *pbuf_even;
wdenk20c98a62004-04-23 20:32:05 +0000783 EIEIO;
wdenkf1276d42005-02-03 23:00:49 +0000784 SYNC;
785 *dbuf++ = *pbuf_odd;
wdenkf6925c72005-01-22 18:26:04 +0000786 EIEIO;
wdenkf1276d42005-02-03 23:00:49 +0000787 SYNC;
wdenk2f99a692004-01-04 22:51:12 +0000788 *dbuf++ = *pbuf_even;
wdenk20c98a62004-04-23 20:32:05 +0000789 EIEIO;
wdenkf1276d42005-02-03 23:00:49 +0000790 SYNC;
wdenk2f99a692004-01-04 22:51:12 +0000791 *dbuf++ = *pbuf_odd;
wdenk20c98a62004-04-23 20:32:05 +0000792 EIEIO;
wdenkf1276d42005-02-03 23:00:49 +0000793 SYNC;
794 }
795#else
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000796 ushort *dbuf;
797 volatile ushort *pbuf;
wdenkf1276d42005-02-03 23:00:49 +0000798
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000799 pbuf = (ushort *) (ATA_CURR_BASE(dev) + ATA_DATA_REG);
800 dbuf = (ushort *) sect_buf;
wdenkf1276d42005-02-03 23:00:49 +0000801
802 debug("in input data base for read is %lx\n", (unsigned long) pbuf);
803
804 while (words--) {
Heiko Schocher633e03a2007-06-22 19:11:54 +0200805#if defined(CONFIG_PCS440EP)
806 EIEIO;
807 *dbuf++ = ld_le16(pbuf);
808 EIEIO;
809 *dbuf++ = ld_le16(pbuf);
810#else
wdenk20c98a62004-04-23 20:32:05 +0000811 EIEIO;
wdenkf1276d42005-02-03 23:00:49 +0000812 *dbuf++ = *pbuf;
wdenkf6925c72005-01-22 18:26:04 +0000813 EIEIO;
wdenkf1276d42005-02-03 23:00:49 +0000814 *dbuf++ = *pbuf;
Heiko Schocher633e03a2007-06-22 19:11:54 +0200815#endif
wdenk2f99a692004-01-04 22:51:12 +0000816 }
wdenkf1276d42005-02-03 23:00:49 +0000817#endif
wdenkc6097192002-11-03 00:24:07 +0000818}
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000819#else /* ! CONFIG_IDE_SWAP_IO */
820static void input_data(int dev, ulong *sect_buf, int words)
wdenk591dda52002-11-18 00:14:45 +0000821{
Macpaul Lind1e49942011-04-11 20:45:32 +0000822#if defined(CONFIG_IDE_AHB)
823 ide_read_data(dev, sect_buf, words);
824#else
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000825 insw(ATA_CURR_BASE(dev) + ATA_DATA_REG, sect_buf, words << 1);
Macpaul Lind1e49942011-04-11 20:45:32 +0000826#endif
wdenk591dda52002-11-18 00:14:45 +0000827}
828
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000829#endif /* CONFIG_IDE_SWAP_IO */
wdenkc6097192002-11-03 00:24:07 +0000830
831/* -------------------------------------------------------------------------
832 */
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000833static void ide_ident(block_dev_desc_t *dev_desc)
wdenkc6097192002-11-03 00:24:07 +0000834{
wdenkc6097192002-11-03 00:24:07 +0000835 unsigned char c;
Marek Vasute49757e2011-08-20 09:15:13 +0000836 hd_driveid_t iop;
wdenkc6097192002-11-03 00:24:07 +0000837
wdenke2d6d742004-09-28 20:34:50 +0000838#ifdef CONFIG_ATAPI
839 int retries = 0;
wdenk452cfd62002-11-19 11:04:11 +0000840#endif
841
Steven A. Falco2b7574e2008-08-15 15:34:10 -0400842#ifdef CONFIG_TUNE_PIO
843 int pio_mode;
844#endif
845
wdenkc6097192002-11-03 00:24:07 +0000846#if 0
847 int mode, cycle_time;
848#endif
849 int device;
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000850
851 device = dev_desc->dev;
852 printf(" Device %d: ", device);
wdenkc6097192002-11-03 00:24:07 +0000853
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000854 ide_led(DEVICE_LED(device), 1); /* LED on */
wdenkc6097192002-11-03 00:24:07 +0000855 /* Select device
856 */
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000857 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
858 dev_desc->if_type = IF_TYPE_IDE;
wdenkc6097192002-11-03 00:24:07 +0000859#ifdef CONFIG_ATAPI
wdenk452cfd62002-11-19 11:04:11 +0000860
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000861 retries = 0;
wdenk452cfd62002-11-19 11:04:11 +0000862
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000863 /* Warning: This will be tricky to read */
864 while (retries <= 1) {
865 /* check signature */
866 if ((ide_inb(device, ATA_SECT_CNT) == 0x01) &&
867 (ide_inb(device, ATA_SECT_NUM) == 0x01) &&
868 (ide_inb(device, ATA_CYL_LOW) == 0x14) &&
869 (ide_inb(device, ATA_CYL_HIGH) == 0xEB)) {
870 /* ATAPI Signature found */
871 dev_desc->if_type = IF_TYPE_ATAPI;
872 /*
873 * Start Ident Command
874 */
875 ide_outb(device, ATA_COMMAND, ATAPI_CMD_IDENT);
876 /*
877 * Wait for completion - ATAPI devices need more time
878 * to become ready
879 */
880 c = ide_wait(device, ATAPI_TIME_OUT);
881 } else
wdenkc6097192002-11-03 00:24:07 +0000882#endif
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000883 {
884 /*
885 * Start Ident Command
886 */
887 ide_outb(device, ATA_COMMAND, ATA_CMD_IDENT);
wdenkc6097192002-11-03 00:24:07 +0000888
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000889 /*
890 * Wait for completion
891 */
892 c = ide_wait(device, IDE_TIME_OUT);
893 }
894 ide_led(DEVICE_LED(device), 0); /* LED off */
wdenkc6097192002-11-03 00:24:07 +0000895
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000896 if (((c & ATA_STAT_DRQ) == 0) ||
897 ((c & (ATA_STAT_FAULT | ATA_STAT_ERR)) != 0)) {
wdenke2d6d742004-09-28 20:34:50 +0000898#ifdef CONFIG_ATAPI
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000899 {
900 /*
901 * Need to soft reset the device
902 * in case it's an ATAPI...
903 */
904 debug("Retrying...\n");
905 ide_outb(device, ATA_DEV_HD,
906 ATA_LBA | ATA_DEVICE(device));
907 udelay(100000);
908 ide_outb(device, ATA_COMMAND, 0x08);
909 udelay(500000); /* 500 ms */
910 }
911 /*
912 * Select device
913 */
914 ide_outb(device, ATA_DEV_HD,
915 ATA_LBA | ATA_DEVICE(device));
916 retries++;
wdenke2d6d742004-09-28 20:34:50 +0000917#else
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000918 return;
wdenke2d6d742004-09-28 20:34:50 +0000919#endif
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000920 }
wdenke2d6d742004-09-28 20:34:50 +0000921#ifdef CONFIG_ATAPI
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000922 else
923 break;
924 } /* see above - ugly to read */
wdenke2d6d742004-09-28 20:34:50 +0000925
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000926 if (retries == 2) /* Not found */
wdenke2d6d742004-09-28 20:34:50 +0000927 return;
928#endif
wdenkc6097192002-11-03 00:24:07 +0000929
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000930 input_swap_data(device, (ulong *)&iop, ATA_SECTORWORDS);
wdenkc6097192002-11-03 00:24:07 +0000931
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000932 ident_cpy((unsigned char *) dev_desc->revision, iop.fw_rev,
933 sizeof(dev_desc->revision));
934 ident_cpy((unsigned char *) dev_desc->vendor, iop.model,
935 sizeof(dev_desc->vendor));
936 ident_cpy((unsigned char *) dev_desc->product, iop.serial_no,
937 sizeof(dev_desc->product));
wdenkacd9b102004-03-14 00:59:59 +0000938#ifdef __LITTLE_ENDIAN
939 /*
Richard Retanubun54f30622008-11-06 14:01:51 -0500940 * firmware revision, model, and serial number have Big Endian Byte
941 * order in Word. Convert all three to little endian.
wdenkacd9b102004-03-14 00:59:59 +0000942 *
943 * See CF+ and CompactFlash Specification Revision 2.0:
Richard Retanubun54f30622008-11-06 14:01:51 -0500944 * 6.2.1.6: Identify Drive, Table 39 for more details
wdenkacd9b102004-03-14 00:59:59 +0000945 */
946
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000947 strswab(dev_desc->revision);
948 strswab(dev_desc->vendor);
949 strswab(dev_desc->product);
wdenkacd9b102004-03-14 00:59:59 +0000950#endif /* __LITTLE_ENDIAN */
wdenkc6097192002-11-03 00:24:07 +0000951
Marek Vasute49757e2011-08-20 09:15:13 +0000952 if ((iop.config & 0x0080) == 0x0080)
wdenkc6097192002-11-03 00:24:07 +0000953 dev_desc->removable = 1;
954 else
955 dev_desc->removable = 0;
956
Steven A. Falco2b7574e2008-08-15 15:34:10 -0400957#ifdef CONFIG_TUNE_PIO
958 /* Mode 0 - 2 only, are directly determined by word 51. */
Marek Vasute49757e2011-08-20 09:15:13 +0000959 pio_mode = iop.tPIO;
Steven A. Falco2b7574e2008-08-15 15:34:10 -0400960 if (pio_mode > 2) {
961 printf("WARNING: Invalid PIO (word 51 = %d).\n", pio_mode);
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000962 /* Force it to dead slow, and hope for the best... */
963 pio_mode = 0;
Steven A. Falco2b7574e2008-08-15 15:34:10 -0400964 }
965
966 /* Any CompactFlash Storage Card that supports PIO mode 3 or above
967 * shall set bit 1 of word 53 to one and support the fields contained
968 * in words 64 through 70.
969 */
Marek Vasute49757e2011-08-20 09:15:13 +0000970 if (iop.field_valid & 0x02) {
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000971 /*
972 * Mode 3 and above are possible. Check in order from slow
Steven A. Falco2b7574e2008-08-15 15:34:10 -0400973 * to fast, so we wind up with the highest mode allowed.
974 */
Marek Vasute49757e2011-08-20 09:15:13 +0000975 if (iop.eide_pio_modes & 0x01)
Steven A. Falco2b7574e2008-08-15 15:34:10 -0400976 pio_mode = 3;
Marek Vasute49757e2011-08-20 09:15:13 +0000977 if (iop.eide_pio_modes & 0x02)
Steven A. Falco2b7574e2008-08-15 15:34:10 -0400978 pio_mode = 4;
Marek Vasute49757e2011-08-20 09:15:13 +0000979 if (ata_id_is_cfa((u16 *)&iop)) {
980 if ((iop.cf_advanced_caps & 0x07) == 0x01)
Steven A. Falco2b7574e2008-08-15 15:34:10 -0400981 pio_mode = 5;
Marek Vasute49757e2011-08-20 09:15:13 +0000982 if ((iop.cf_advanced_caps & 0x07) == 0x02)
Steven A. Falco2b7574e2008-08-15 15:34:10 -0400983 pio_mode = 6;
984 }
985 }
986
987 /* System-specific, depends on bus speeds, etc. */
988 ide_set_piomode(pio_mode);
989#endif /* CONFIG_TUNE_PIO */
990
wdenkc6097192002-11-03 00:24:07 +0000991#if 0
992 /*
993 * Drive PIO mode autoselection
994 */
Marek Vasute49757e2011-08-20 09:15:13 +0000995 mode = iop.tPIO;
wdenkc6097192002-11-03 00:24:07 +0000996
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +0000997 printf("tPIO = 0x%02x = %d\n", mode, mode);
wdenkc6097192002-11-03 00:24:07 +0000998 if (mode > 2) { /* 2 is maximum allowed tPIO value */
999 mode = 2;
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001000 debug("Override tPIO -> 2\n");
wdenkc6097192002-11-03 00:24:07 +00001001 }
Marek Vasute49757e2011-08-20 09:15:13 +00001002 if (iop.field_valid & 2) { /* drive implements ATA2? */
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001003 debug("Drive implements ATA2\n");
Marek Vasute49757e2011-08-20 09:15:13 +00001004 if (iop.capability & 8) { /* drive supports use_iordy? */
1005 cycle_time = iop.eide_pio_iordy;
wdenkc6097192002-11-03 00:24:07 +00001006 } else {
Marek Vasute49757e2011-08-20 09:15:13 +00001007 cycle_time = iop.eide_pio;
wdenkc6097192002-11-03 00:24:07 +00001008 }
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001009 debug("cycle time = %d\n", cycle_time);
wdenkc6097192002-11-03 00:24:07 +00001010 mode = 4;
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001011 if (cycle_time > 120)
1012 mode = 3; /* 120 ns for PIO mode 4 */
1013 if (cycle_time > 180)
1014 mode = 2; /* 180 ns for PIO mode 3 */
1015 if (cycle_time > 240)
1016 mode = 1; /* 240 ns for PIO mode 4 */
1017 if (cycle_time > 383)
1018 mode = 0; /* 383 ns for PIO mode 4 */
wdenkc6097192002-11-03 00:24:07 +00001019 }
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001020 printf("PIO mode to use: PIO %d\n", mode);
wdenkc6097192002-11-03 00:24:07 +00001021#endif /* 0 */
1022
1023#ifdef CONFIG_ATAPI
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001024 if (dev_desc->if_type == IF_TYPE_ATAPI) {
wdenkc6097192002-11-03 00:24:07 +00001025 atapi_inquiry(dev_desc);
1026 return;
1027 }
1028#endif /* CONFIG_ATAPI */
1029
wdenkacd9b102004-03-14 00:59:59 +00001030#ifdef __BIG_ENDIAN
wdenkc6097192002-11-03 00:24:07 +00001031 /* swap shorts */
Marek Vasute49757e2011-08-20 09:15:13 +00001032 dev_desc->lba = (iop.lba_capacity << 16) | (iop.lba_capacity >> 16);
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001033#else /* ! __BIG_ENDIAN */
wdenkacd9b102004-03-14 00:59:59 +00001034 /*
1035 * do not swap shorts on little endian
1036 *
1037 * See CF+ and CompactFlash Specification Revision 2.0:
1038 * 6.2.1.6: Identfy Drive, Table 39, Word Address 57-58 for details.
1039 */
Marek Vasute49757e2011-08-20 09:15:13 +00001040 dev_desc->lba = iop.lba_capacity;
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001041#endif /* __BIG_ENDIAN */
wdenkf602aa02004-03-13 23:29:43 +00001042
wdenkc35ba4e2004-03-14 22:25:36 +00001043#ifdef CONFIG_LBA48
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001044 if (iop.command_set_2 & 0x0400) { /* LBA 48 support */
wdenk05939202004-04-18 17:39:38 +00001045 dev_desc->lba48 = 1;
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001046 dev_desc->lba = (unsigned long long) iop.lba48_capacity[0] |
1047 ((unsigned long long) iop.lba48_capacity[1] << 16) |
1048 ((unsigned long long) iop.lba48_capacity[2] << 32) |
1049 ((unsigned long long) iop.lba48_capacity[3] << 48);
wdenkf602aa02004-03-13 23:29:43 +00001050 } else {
wdenkf602aa02004-03-13 23:29:43 +00001051 dev_desc->lba48 = 0;
1052 }
1053#endif /* CONFIG_LBA48 */
wdenkc6097192002-11-03 00:24:07 +00001054 /* assuming HD */
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001055 dev_desc->type = DEV_TYPE_HARDDISK;
1056 dev_desc->blksz = ATA_BLOCKSIZE;
1057 dev_desc->lun = 0; /* just to fill something in... */
wdenkc6097192002-11-03 00:24:07 +00001058
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001059#if 0 /* only used to test the powersaving mode,
1060 * if enabled, the drive goes after 5 sec
1061 * in standby mode */
1062 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
1063 c = ide_wait(device, IDE_TIME_OUT);
1064 ide_outb(device, ATA_SECT_CNT, 1);
1065 ide_outb(device, ATA_LBA_LOW, 0);
1066 ide_outb(device, ATA_LBA_MID, 0);
1067 ide_outb(device, ATA_LBA_HIGH, 0);
1068 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
1069 ide_outb(device, ATA_COMMAND, 0xe3);
1070 udelay(50);
1071 c = ide_wait(device, IDE_TIME_OUT); /* can't take over 500 ms */
wdenkc6097192002-11-03 00:24:07 +00001072#endif
1073}
1074
1075
1076/* ------------------------------------------------------------------------- */
1077
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001078ulong ide_read(int device, lbaint_t blknr, ulong blkcnt, void *buffer)
wdenkc6097192002-11-03 00:24:07 +00001079{
1080 ulong n = 0;
1081 unsigned char c;
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001082 unsigned char pwrsave = 0; /* power save */
1083
wdenkc35ba4e2004-03-14 22:25:36 +00001084#ifdef CONFIG_LBA48
wdenkf602aa02004-03-13 23:29:43 +00001085 unsigned char lba48 = 0;
wdenkc6097192002-11-03 00:24:07 +00001086
Guennadi Liakhovetskib71f1ca2008-04-28 14:36:06 +02001087 if (blknr & 0x0000fffff0000000ULL) {
wdenkf602aa02004-03-13 23:29:43 +00001088 /* more than 28 bits used, use 48bit mode */
1089 lba48 = 1;
1090 }
1091#endif
Marek Vasut92131712011-10-25 11:39:15 +02001092 debug("ide_read dev %d start %lX, blocks %lX buffer at %lX\n",
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001093 device, blknr, blkcnt, (ulong) buffer);
wdenkc6097192002-11-03 00:24:07 +00001094
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001095 ide_led(DEVICE_LED(device), 1); /* LED on */
wdenkc6097192002-11-03 00:24:07 +00001096
1097 /* Select device
1098 */
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001099 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
1100 c = ide_wait(device, IDE_TIME_OUT);
wdenkc6097192002-11-03 00:24:07 +00001101
1102 if (c & ATA_STAT_BUSY) {
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001103 printf("IDE read: device %d not ready\n", device);
wdenkc6097192002-11-03 00:24:07 +00001104 goto IDE_READ_E;
1105 }
1106
1107 /* first check if the drive is in Powersaving mode, if yes,
1108 * increase the timeout value */
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001109 ide_outb(device, ATA_COMMAND, ATA_CMD_CHK_PWR);
1110 udelay(50);
wdenkc6097192002-11-03 00:24:07 +00001111
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001112 c = ide_wait(device, IDE_TIME_OUT); /* can't take over 500 ms */
wdenkc6097192002-11-03 00:24:07 +00001113
1114 if (c & ATA_STAT_BUSY) {
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001115 printf("IDE read: device %d not ready\n", device);
wdenkc6097192002-11-03 00:24:07 +00001116 goto IDE_READ_E;
1117 }
1118 if ((c & ATA_STAT_ERR) == ATA_STAT_ERR) {
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001119 printf("No Powersaving mode %X\n", c);
wdenkc6097192002-11-03 00:24:07 +00001120 } else {
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001121 c = ide_inb(device, ATA_SECT_CNT);
1122 debug("Powersaving %02X\n", c);
1123 if (c == 0)
1124 pwrsave = 1;
wdenkc6097192002-11-03 00:24:07 +00001125 }
1126
1127
1128 while (blkcnt-- > 0) {
1129
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001130 c = ide_wait(device, IDE_TIME_OUT);
wdenkc6097192002-11-03 00:24:07 +00001131
1132 if (c & ATA_STAT_BUSY) {
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001133 printf("IDE read: device %d not ready\n", device);
wdenkc6097192002-11-03 00:24:07 +00001134 break;
1135 }
wdenkc35ba4e2004-03-14 22:25:36 +00001136#ifdef CONFIG_LBA48
wdenkf602aa02004-03-13 23:29:43 +00001137 if (lba48) {
1138 /* write high bits */
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001139 ide_outb(device, ATA_SECT_CNT, 0);
1140 ide_outb(device, ATA_LBA_LOW, (blknr >> 24) & 0xFF);
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001141#ifdef CONFIG_SYS_64BIT_LBA
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001142 ide_outb(device, ATA_LBA_MID, (blknr >> 32) & 0xFF);
1143 ide_outb(device, ATA_LBA_HIGH, (blknr >> 40) & 0xFF);
Guennadi Liakhovetskib71f1ca2008-04-28 14:36:06 +02001144#else
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001145 ide_outb(device, ATA_LBA_MID, 0);
1146 ide_outb(device, ATA_LBA_HIGH, 0);
Guennadi Liakhovetskib71f1ca2008-04-28 14:36:06 +02001147#endif
wdenkf602aa02004-03-13 23:29:43 +00001148 }
1149#endif
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001150 ide_outb(device, ATA_SECT_CNT, 1);
1151 ide_outb(device, ATA_LBA_LOW, (blknr >> 0) & 0xFF);
1152 ide_outb(device, ATA_LBA_MID, (blknr >> 8) & 0xFF);
1153 ide_outb(device, ATA_LBA_HIGH, (blknr >> 16) & 0xFF);
wdenkf602aa02004-03-13 23:29:43 +00001154
wdenkc35ba4e2004-03-14 22:25:36 +00001155#ifdef CONFIG_LBA48
wdenkf602aa02004-03-13 23:29:43 +00001156 if (lba48) {
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001157 ide_outb(device, ATA_DEV_HD,
1158 ATA_LBA | ATA_DEVICE(device));
1159 ide_outb(device, ATA_COMMAND, ATA_CMD_READ_EXT);
wdenkf602aa02004-03-13 23:29:43 +00001160
1161 } else
1162#endif
1163 {
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001164 ide_outb(device, ATA_DEV_HD, ATA_LBA |
1165 ATA_DEVICE(device) | ((blknr >> 24) & 0xF));
1166 ide_outb(device, ATA_COMMAND, ATA_CMD_READ);
wdenkf602aa02004-03-13 23:29:43 +00001167 }
wdenkc6097192002-11-03 00:24:07 +00001168
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001169 udelay(50);
wdenkc6097192002-11-03 00:24:07 +00001170
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001171 if (pwrsave) {
1172 /* may take up to 4 sec */
1173 c = ide_wait(device, IDE_SPIN_UP_TIME_OUT);
1174 pwrsave = 0;
wdenkc6097192002-11-03 00:24:07 +00001175 } else {
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001176 /* can't take over 500 ms */
1177 c = ide_wait(device, IDE_TIME_OUT);
wdenkc6097192002-11-03 00:24:07 +00001178 }
1179
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001180 if ((c & (ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR)) !=
1181 ATA_STAT_DRQ) {
Heiko Schocher0f602e12009-12-03 11:21:21 +01001182#if defined(CONFIG_SYS_64BIT_LBA)
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001183 printf("Error (no IRQ) dev %d blk %lld: status 0x%02x\n",
wdenkc6097192002-11-03 00:24:07 +00001184 device, blknr, c);
wdenkf602aa02004-03-13 23:29:43 +00001185#else
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001186 printf("Error (no IRQ) dev %d blk %ld: status 0x%02x\n",
1187 device, (ulong) blknr, c);
wdenkf602aa02004-03-13 23:29:43 +00001188#endif
wdenkc6097192002-11-03 00:24:07 +00001189 break;
1190 }
1191
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001192 input_data(device, buffer, ATA_SECTORWORDS);
1193 (void) ide_inb(device, ATA_STATUS); /* clear IRQ */
wdenkc6097192002-11-03 00:24:07 +00001194
1195 ++n;
1196 ++blknr;
Greg Lopp50abf9f2007-04-13 08:02:24 +02001197 buffer += ATA_BLOCKSIZE;
wdenkc6097192002-11-03 00:24:07 +00001198 }
1199IDE_READ_E:
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001200 ide_led(DEVICE_LED(device), 0); /* LED off */
wdenkc6097192002-11-03 00:24:07 +00001201 return (n);
1202}
1203
1204/* ------------------------------------------------------------------------- */
1205
1206
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001207ulong ide_write(int device, lbaint_t blknr, ulong blkcnt, const void *buffer)
wdenkc6097192002-11-03 00:24:07 +00001208{
1209 ulong n = 0;
1210 unsigned char c;
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001211
wdenkc35ba4e2004-03-14 22:25:36 +00001212#ifdef CONFIG_LBA48
wdenkf602aa02004-03-13 23:29:43 +00001213 unsigned char lba48 = 0;
1214
Guennadi Liakhovetskib71f1ca2008-04-28 14:36:06 +02001215 if (blknr & 0x0000fffff0000000ULL) {
wdenkf602aa02004-03-13 23:29:43 +00001216 /* more than 28 bits used, use 48bit mode */
1217 lba48 = 1;
1218 }
1219#endif
wdenkc6097192002-11-03 00:24:07 +00001220
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001221 ide_led(DEVICE_LED(device), 1); /* LED on */
wdenkc6097192002-11-03 00:24:07 +00001222
1223 /* Select device
1224 */
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001225 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
wdenkc6097192002-11-03 00:24:07 +00001226
1227 while (blkcnt-- > 0) {
1228
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001229 c = ide_wait(device, IDE_TIME_OUT);
wdenkc6097192002-11-03 00:24:07 +00001230
1231 if (c & ATA_STAT_BUSY) {
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001232 printf("IDE read: device %d not ready\n", device);
wdenkc6097192002-11-03 00:24:07 +00001233 goto WR_OUT;
1234 }
wdenkc35ba4e2004-03-14 22:25:36 +00001235#ifdef CONFIG_LBA48
wdenkf602aa02004-03-13 23:29:43 +00001236 if (lba48) {
1237 /* write high bits */
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001238 ide_outb(device, ATA_SECT_CNT, 0);
1239 ide_outb(device, ATA_LBA_LOW, (blknr >> 24) & 0xFF);
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001240#ifdef CONFIG_SYS_64BIT_LBA
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001241 ide_outb(device, ATA_LBA_MID, (blknr >> 32) & 0xFF);
1242 ide_outb(device, ATA_LBA_HIGH, (blknr >> 40) & 0xFF);
Guennadi Liakhovetskib71f1ca2008-04-28 14:36:06 +02001243#else
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001244 ide_outb(device, ATA_LBA_MID, 0);
1245 ide_outb(device, ATA_LBA_HIGH, 0);
Guennadi Liakhovetskib71f1ca2008-04-28 14:36:06 +02001246#endif
wdenkf602aa02004-03-13 23:29:43 +00001247 }
1248#endif
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001249 ide_outb(device, ATA_SECT_CNT, 1);
1250 ide_outb(device, ATA_LBA_LOW, (blknr >> 0) & 0xFF);
1251 ide_outb(device, ATA_LBA_MID, (blknr >> 8) & 0xFF);
1252 ide_outb(device, ATA_LBA_HIGH, (blknr >> 16) & 0xFF);
wdenkf602aa02004-03-13 23:29:43 +00001253
wdenkc35ba4e2004-03-14 22:25:36 +00001254#ifdef CONFIG_LBA48
wdenkf602aa02004-03-13 23:29:43 +00001255 if (lba48) {
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001256 ide_outb(device, ATA_DEV_HD,
1257 ATA_LBA | ATA_DEVICE(device));
1258 ide_outb(device, ATA_COMMAND, ATA_CMD_WRITE_EXT);
wdenkf602aa02004-03-13 23:29:43 +00001259
1260 } else
1261#endif
1262 {
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001263 ide_outb(device, ATA_DEV_HD, ATA_LBA |
1264 ATA_DEVICE(device) | ((blknr >> 24) & 0xF));
1265 ide_outb(device, ATA_COMMAND, ATA_CMD_WRITE);
wdenkf602aa02004-03-13 23:29:43 +00001266 }
wdenkc6097192002-11-03 00:24:07 +00001267
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001268 udelay(50);
wdenkc6097192002-11-03 00:24:07 +00001269
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001270 /* can't take over 500 ms */
1271 c = ide_wait(device, IDE_TIME_OUT);
wdenkc6097192002-11-03 00:24:07 +00001272
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001273 if ((c & (ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR)) !=
1274 ATA_STAT_DRQ) {
Heiko Schocher0f602e12009-12-03 11:21:21 +01001275#if defined(CONFIG_SYS_64BIT_LBA)
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001276 printf("Error (no IRQ) dev %d blk %lld: status 0x%02x\n",
wdenkc6097192002-11-03 00:24:07 +00001277 device, blknr, c);
wdenkf602aa02004-03-13 23:29:43 +00001278#else
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001279 printf("Error (no IRQ) dev %d blk %ld: status 0x%02x\n",
1280 device, (ulong) blknr, c);
wdenkf602aa02004-03-13 23:29:43 +00001281#endif
wdenkc6097192002-11-03 00:24:07 +00001282 goto WR_OUT;
1283 }
1284
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001285 output_data(device, buffer, ATA_SECTORWORDS);
1286 c = ide_inb(device, ATA_STATUS); /* clear IRQ */
wdenkc6097192002-11-03 00:24:07 +00001287 ++n;
1288 ++blknr;
Greg Lopp50abf9f2007-04-13 08:02:24 +02001289 buffer += ATA_BLOCKSIZE;
wdenkc6097192002-11-03 00:24:07 +00001290 }
1291WR_OUT:
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001292 ide_led(DEVICE_LED(device), 0); /* LED off */
wdenkc6097192002-11-03 00:24:07 +00001293 return (n);
1294}
1295
1296/* ------------------------------------------------------------------------- */
1297
1298/*
1299 * copy src to dest, skipping leading and trailing blanks and null
1300 * terminate the string
wdenk18bd81e2004-03-17 01:13:07 +00001301 * "len" is the size of available memory including the terminating '\0'
wdenkc6097192002-11-03 00:24:07 +00001302 */
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001303static void ident_cpy(unsigned char *dst, unsigned char *src,
1304 unsigned int len)
wdenkc6097192002-11-03 00:24:07 +00001305{
wdenk18bd81e2004-03-17 01:13:07 +00001306 unsigned char *end, *last;
wdenkc6097192002-11-03 00:24:07 +00001307
wdenk18bd81e2004-03-17 01:13:07 +00001308 last = dst;
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001309 end = src + len - 1;
wdenk18bd81e2004-03-17 01:13:07 +00001310
1311 /* reserve space for '\0' */
1312 if (len < 2)
1313 goto OUT;
wdenkc12081a2004-03-23 20:18:25 +00001314
wdenk18bd81e2004-03-17 01:13:07 +00001315 /* skip leading white space */
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001316 while ((*src) && (src < end) && (*src == ' '))
wdenk18bd81e2004-03-17 01:13:07 +00001317 ++src;
1318
1319 /* copy string, omitting trailing white space */
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001320 while ((*src) && (src < end)) {
wdenk18bd81e2004-03-17 01:13:07 +00001321 *dst++ = *src;
1322 if (*src++ != ' ')
1323 last = dst;
wdenkc6097192002-11-03 00:24:07 +00001324 }
wdenk18bd81e2004-03-17 01:13:07 +00001325OUT:
1326 *last = '\0';
wdenkc6097192002-11-03 00:24:07 +00001327}
1328
1329/* ------------------------------------------------------------------------- */
1330
1331/*
1332 * Wait until Busy bit is off, or timeout (in ms)
1333 * Return last status
1334 */
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001335static uchar ide_wait(int dev, ulong t)
wdenkc6097192002-11-03 00:24:07 +00001336{
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001337 ulong delay = 10 * t; /* poll every 100 us */
wdenkc6097192002-11-03 00:24:07 +00001338 uchar c;
1339
wdenk591dda52002-11-18 00:14:45 +00001340 while ((c = ide_inb(dev, ATA_STATUS)) & ATA_STAT_BUSY) {
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001341 udelay(100);
1342 if (delay-- == 0)
wdenkc6097192002-11-03 00:24:07 +00001343 break;
wdenkc6097192002-11-03 00:24:07 +00001344 }
1345 return (c);
1346}
1347
1348/* ------------------------------------------------------------------------- */
1349
1350#ifdef CONFIG_IDE_RESET
1351extern void ide_set_reset(int idereset);
1352
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001353static void ide_reset(void)
wdenkc6097192002-11-03 00:24:07 +00001354{
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001355#if defined(CONFIG_SYS_PB_12V_ENABLE) || defined(CONFIG_SYS_PB_IDE_MOTOR)
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001356 volatile immap_t *immr = (immap_t *) CONFIG_SYS_IMMR;
wdenkc6097192002-11-03 00:24:07 +00001357#endif
1358 int i;
1359
1360 curr_device = -1;
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001361 for (i = 0; i < CONFIG_SYS_IDE_MAXBUS; ++i)
wdenkc6097192002-11-03 00:24:07 +00001362 ide_bus_ok[i] = 0;
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001363 for (i = 0; i < CONFIG_SYS_IDE_MAXDEVICE; ++i)
wdenkc6097192002-11-03 00:24:07 +00001364 ide_dev_desc[i].type = DEV_TYPE_UNKNOWN;
1365
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001366 ide_set_reset(1); /* assert reset */
wdenkc6097192002-11-03 00:24:07 +00001367
Martin Krause7e35caf2008-04-03 13:37:56 +02001368 /* the reset signal shall be asserted for et least 25 us */
1369 udelay(25);
1370
wdenkc6097192002-11-03 00:24:07 +00001371 WATCHDOG_RESET();
1372
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001373#ifdef CONFIG_SYS_PB_12V_ENABLE
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001374 /* 12V Enable output OFF */
1375 immr->im_cpm.cp_pbdat &= ~(CONFIG_SYS_PB_12V_ENABLE);
1376
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001377 immr->im_cpm.cp_pbpar &= ~(CONFIG_SYS_PB_12V_ENABLE);
1378 immr->im_cpm.cp_pbodr &= ~(CONFIG_SYS_PB_12V_ENABLE);
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001379 immr->im_cpm.cp_pbdir |= CONFIG_SYS_PB_12V_ENABLE;
wdenkc6097192002-11-03 00:24:07 +00001380
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001381 /* wait 500 ms for the voltage to stabilize */
1382 for (i = 0; i < 500; ++i)
1383 udelay(1000);
wdenkc6097192002-11-03 00:24:07 +00001384
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001385 /* 12V Enable output ON */
1386 immr->im_cpm.cp_pbdat |= CONFIG_SYS_PB_12V_ENABLE;
1387#endif /* CONFIG_SYS_PB_12V_ENABLE */
wdenkc6097192002-11-03 00:24:07 +00001388
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001389#ifdef CONFIG_SYS_PB_IDE_MOTOR
wdenkc6097192002-11-03 00:24:07 +00001390 /* configure IDE Motor voltage monitor pin as input */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001391 immr->im_cpm.cp_pbpar &= ~(CONFIG_SYS_PB_IDE_MOTOR);
1392 immr->im_cpm.cp_pbodr &= ~(CONFIG_SYS_PB_IDE_MOTOR);
1393 immr->im_cpm.cp_pbdir &= ~(CONFIG_SYS_PB_IDE_MOTOR);
wdenkc6097192002-11-03 00:24:07 +00001394
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001395 /* wait up to 1 s for the motor voltage to stabilize */
1396 for (i = 0; i < 1000; ++i) {
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001397 if ((immr->im_cpm.cp_pbdat & CONFIG_SYS_PB_IDE_MOTOR) != 0) {
wdenkc6097192002-11-03 00:24:07 +00001398 break;
1399 }
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001400 udelay(1000);
wdenkc6097192002-11-03 00:24:07 +00001401 }
1402
1403 if (i == 1000) { /* Timeout */
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001404 printf("\nWarning: 5V for IDE Motor missing\n");
1405#ifdef CONFIG_STATUS_LED
1406#ifdef STATUS_LED_YELLOW
1407 status_led_set(STATUS_LED_YELLOW, STATUS_LED_ON);
1408#endif
1409#ifdef STATUS_LED_GREEN
1410 status_led_set(STATUS_LED_GREEN, STATUS_LED_OFF);
1411#endif
1412#endif /* CONFIG_STATUS_LED */
wdenkc6097192002-11-03 00:24:07 +00001413 }
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001414#endif /* CONFIG_SYS_PB_IDE_MOTOR */
wdenkc6097192002-11-03 00:24:07 +00001415
1416 WATCHDOG_RESET();
1417
1418 /* de-assert RESET signal */
1419 ide_set_reset(0);
1420
1421 /* wait 250 ms */
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001422 for (i = 0; i < 250; ++i)
1423 udelay(1000);
wdenkc6097192002-11-03 00:24:07 +00001424}
1425
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001426#endif /* CONFIG_IDE_RESET */
wdenkc6097192002-11-03 00:24:07 +00001427
1428/* ------------------------------------------------------------------------- */
1429
wdenk54070ab2004-12-31 09:32:47 +00001430#if defined(CONFIG_IDE_LED) && \
wdenk54070ab2004-12-31 09:32:47 +00001431 !defined(CONFIG_CPC45) && \
wdenk54070ab2004-12-31 09:32:47 +00001432 !defined(CONFIG_KUP4K) && \
1433 !defined(CONFIG_KUP4X)
wdenkc6097192002-11-03 00:24:07 +00001434
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001435static uchar led_buffer; /* Buffer for current LED status */
wdenkc6097192002-11-03 00:24:07 +00001436
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001437static void ide_led(uchar led, uchar status)
wdenkc6097192002-11-03 00:24:07 +00001438{
1439 uchar *led_port = LED_PORT;
1440
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001441 if (status) /* switch LED on */
1442 led_buffer |= led;
1443 else /* switch LED off */
wdenkc6097192002-11-03 00:24:07 +00001444 led_buffer &= ~led;
wdenkc6097192002-11-03 00:24:07 +00001445
1446 *led_port = led_buffer;
1447}
1448
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001449#endif /* CONFIG_IDE_LED */
wdenkc6097192002-11-03 00:24:07 +00001450
Heiko Schocherffb293a2009-09-23 07:56:08 +02001451#if defined(CONFIG_OF_IDE_FIXUP)
1452int ide_device_present(int dev)
1453{
1454 if (dev >= CONFIG_SYS_IDE_MAXBUS)
1455 return 0;
1456 return (ide_dev_desc[dev].type == DEV_TYPE_UNKNOWN ? 0 : 1);
1457}
1458#endif
wdenkc6097192002-11-03 00:24:07 +00001459/* ------------------------------------------------------------------------- */
1460
1461#ifdef CONFIG_ATAPI
1462/****************************************************************************
1463 * ATAPI Support
1464 */
1465
Albert Aribaud036c6b42010-08-08 05:17:05 +05301466#if defined(CONFIG_IDE_SWAP_IO)
wdenkc6097192002-11-03 00:24:07 +00001467/* since ATAPI may use commands with not 4 bytes alligned length
1468 * we have our own transfer functions, 2 bytes alligned */
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001469static void output_data_shorts(int dev, ushort *sect_buf, int shorts)
wdenkc6097192002-11-03 00:24:07 +00001470{
Wolfgang Denkfb52e952010-09-19 21:28:25 +02001471#if defined(CONFIG_CPC45)
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001472 uchar *dbuf;
1473 volatile uchar *pbuf_even;
1474 volatile uchar *pbuf_odd;
wdenk2f99a692004-01-04 22:51:12 +00001475
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001476 pbuf_even = (uchar *) (ATA_CURR_BASE(dev) + ATA_DATA_EVEN);
1477 pbuf_odd = (uchar *) (ATA_CURR_BASE(dev) + ATA_DATA_ODD);
wdenk2f99a692004-01-04 22:51:12 +00001478 while (shorts--) {
wdenk20c98a62004-04-23 20:32:05 +00001479 EIEIO;
wdenk2f99a692004-01-04 22:51:12 +00001480 *pbuf_even = *dbuf++;
wdenk20c98a62004-04-23 20:32:05 +00001481 EIEIO;
wdenk2f99a692004-01-04 22:51:12 +00001482 *pbuf_odd = *dbuf++;
1483 }
wdenkf1276d42005-02-03 23:00:49 +00001484#else
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001485 ushort *dbuf;
1486 volatile ushort *pbuf;
wdenkc6097192002-11-03 00:24:07 +00001487
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001488 pbuf = (ushort *) (ATA_CURR_BASE(dev) + ATA_DATA_REG);
1489 dbuf = (ushort *) sect_buf;
wdenk634d2f72004-04-15 23:14:49 +00001490
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001491 debug("in output data shorts base for read is %lx\n",
1492 (unsigned long) pbuf);
wdenk634d2f72004-04-15 23:14:49 +00001493
wdenkc6097192002-11-03 00:24:07 +00001494 while (shorts--) {
wdenk20c98a62004-04-23 20:32:05 +00001495 EIEIO;
wdenkf1276d42005-02-03 23:00:49 +00001496 *pbuf = *dbuf++;
wdenkc6097192002-11-03 00:24:07 +00001497 }
wdenkf1276d42005-02-03 23:00:49 +00001498#endif
1499}
1500
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001501static void input_data_shorts(int dev, ushort *sect_buf, int shorts)
wdenkf1276d42005-02-03 23:00:49 +00001502{
Wolfgang Denkfb52e952010-09-19 21:28:25 +02001503#if defined(CONFIG_CPC45)
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001504 uchar *dbuf;
1505 volatile uchar *pbuf_even;
1506 volatile uchar *pbuf_odd;
wdenk2f99a692004-01-04 22:51:12 +00001507
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001508 pbuf_even = (uchar *) (ATA_CURR_BASE(dev) + ATA_DATA_EVEN);
1509 pbuf_odd = (uchar *) (ATA_CURR_BASE(dev) + ATA_DATA_ODD);
wdenk2f99a692004-01-04 22:51:12 +00001510 while (shorts--) {
wdenk20c98a62004-04-23 20:32:05 +00001511 EIEIO;
wdenk2f99a692004-01-04 22:51:12 +00001512 *dbuf++ = *pbuf_even;
wdenk20c98a62004-04-23 20:32:05 +00001513 EIEIO;
wdenk2f99a692004-01-04 22:51:12 +00001514 *dbuf++ = *pbuf_odd;
1515 }
wdenkf1276d42005-02-03 23:00:49 +00001516#else
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001517 ushort *dbuf;
1518 volatile ushort *pbuf;
wdenkf1276d42005-02-03 23:00:49 +00001519
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001520 pbuf = (ushort *) (ATA_CURR_BASE(dev) + ATA_DATA_REG);
1521 dbuf = (ushort *) sect_buf;
wdenkf1276d42005-02-03 23:00:49 +00001522
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001523 debug("in input data shorts base for read is %lx\n",
1524 (unsigned long) pbuf);
wdenkf1276d42005-02-03 23:00:49 +00001525
1526 while (shorts--) {
1527 EIEIO;
1528 *dbuf++ = *pbuf;
1529 }
1530#endif
wdenkc6097192002-11-03 00:24:07 +00001531}
1532
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001533#else /* ! CONFIG_IDE_SWAP_IO */
1534static void output_data_shorts(int dev, ushort *sect_buf, int shorts)
wdenk591dda52002-11-18 00:14:45 +00001535{
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001536 outsw(ATA_CURR_BASE(dev) + ATA_DATA_REG, sect_buf, shorts);
wdenk591dda52002-11-18 00:14:45 +00001537}
1538
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001539static void input_data_shorts(int dev, ushort *sect_buf, int shorts)
wdenk591dda52002-11-18 00:14:45 +00001540{
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001541 insw(ATA_CURR_BASE(dev) + ATA_DATA_REG, sect_buf, shorts);
wdenk591dda52002-11-18 00:14:45 +00001542}
1543
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001544#endif /* CONFIG_IDE_SWAP_IO */
wdenk591dda52002-11-18 00:14:45 +00001545
wdenkc6097192002-11-03 00:24:07 +00001546/*
1547 * Wait until (Status & mask) == res, or timeout (in ms)
1548 * Return last status
1549 * This is used since some ATAPI CD ROMs clears their Busy Bit first
1550 * and then they set their DRQ Bit
1551 */
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001552static uchar atapi_wait_mask(int dev, ulong t, uchar mask, uchar res)
wdenkc6097192002-11-03 00:24:07 +00001553{
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001554 ulong delay = 10 * t; /* poll every 100 us */
wdenkc6097192002-11-03 00:24:07 +00001555 uchar c;
1556
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001557 /* prevents to read the status before valid */
1558 c = ide_inb(dev, ATA_DEV_CTL);
1559
wdenk591dda52002-11-18 00:14:45 +00001560 while (((c = ide_inb(dev, ATA_STATUS)) & mask) != res) {
wdenkc6097192002-11-03 00:24:07 +00001561 /* break if error occurs (doesn't make sense to wait more) */
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001562 if ((c & ATA_STAT_ERR) == ATA_STAT_ERR)
wdenkc6097192002-11-03 00:24:07 +00001563 break;
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001564 udelay(100);
1565 if (delay-- == 0)
wdenkc6097192002-11-03 00:24:07 +00001566 break;
wdenkc6097192002-11-03 00:24:07 +00001567 }
1568 return (c);
1569}
1570
1571/*
1572 * issue an atapi command
1573 */
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001574unsigned char atapi_issue(int device, unsigned char *ccb, int ccblen,
1575 unsigned char *buffer, int buflen)
wdenkc6097192002-11-03 00:24:07 +00001576{
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001577 unsigned char c, err, mask, res;
wdenkc6097192002-11-03 00:24:07 +00001578 int n;
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001579
1580 ide_led(DEVICE_LED(device), 1); /* LED on */
wdenkc6097192002-11-03 00:24:07 +00001581
1582 /* Select device
1583 */
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001584 mask = ATA_STAT_BUSY | ATA_STAT_DRQ;
wdenkc6097192002-11-03 00:24:07 +00001585 res = 0;
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001586 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
1587 c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
wdenkc6097192002-11-03 00:24:07 +00001588 if ((c & mask) != res) {
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001589 printf("ATAPI_ISSUE: device %d not ready status %X\n", device,
1590 c);
1591 err = 0xFF;
wdenkc6097192002-11-03 00:24:07 +00001592 goto AI_OUT;
1593 }
1594 /* write taskfile */
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001595 ide_outb(device, ATA_ERROR_REG, 0); /* no DMA, no overlaped */
1596 ide_outb(device, ATA_SECT_CNT, 0);
1597 ide_outb(device, ATA_SECT_NUM, 0);
1598 ide_outb(device, ATA_CYL_LOW, (unsigned char) (buflen & 0xFF));
1599 ide_outb(device, ATA_CYL_HIGH,
1600 (unsigned char) ((buflen >> 8) & 0xFF));
1601 ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
wdenkc6097192002-11-03 00:24:07 +00001602
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001603 ide_outb(device, ATA_COMMAND, ATAPI_CMD_PACKET);
1604 udelay(50);
wdenkc6097192002-11-03 00:24:07 +00001605
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001606 mask = ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR;
wdenkc6097192002-11-03 00:24:07 +00001607 res = ATA_STAT_DRQ;
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001608 c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
wdenkc6097192002-11-03 00:24:07 +00001609
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001610 if ((c & mask) != res) { /* DRQ must be 1, BSY 0 */
1611 printf("ATAPI_ISSUE: Error (no IRQ) before sending ccb dev %d status 0x%02x\n",
1612 device, c);
1613 err = 0xFF;
wdenkc6097192002-11-03 00:24:07 +00001614 goto AI_OUT;
1615 }
1616
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001617 /* write command block */
1618 output_data_shorts(device, (unsigned short *) ccb, ccblen / 2);
1619
Wolfgang Denka1be4762008-05-20 16:00:29 +02001620 /* ATAPI Command written wait for completition */
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001621 udelay(5000); /* device must set bsy */
wdenkc6097192002-11-03 00:24:07 +00001622
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001623 mask = ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR;
1624 /*
1625 * if no data wait for DRQ = 0 BSY = 0
1626 * if data wait for DRQ = 1 BSY = 0
1627 */
1628 res = 0;
1629 if (buflen)
wdenkc6097192002-11-03 00:24:07 +00001630 res = ATA_STAT_DRQ;
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001631 c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
1632 if ((c & mask) != res) {
wdenkc6097192002-11-03 00:24:07 +00001633 if (c & ATA_STAT_ERR) {
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001634 err = (ide_inb(device, ATA_ERROR_REG)) >> 4;
1635 debug("atapi_issue 1 returned sense key %X status %02X\n",
1636 err, c);
wdenkc6097192002-11-03 00:24:07 +00001637 } else {
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001638 printf("ATAPI_ISSUE: (no DRQ) after sending ccb (%x) status 0x%02x\n",
1639 ccb[0], c);
1640 err = 0xFF;
wdenkc6097192002-11-03 00:24:07 +00001641 }
1642 goto AI_OUT;
1643 }
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001644 n = ide_inb(device, ATA_CYL_HIGH);
1645 n <<= 8;
1646 n += ide_inb(device, ATA_CYL_LOW);
1647 if (n > buflen) {
1648 printf("ERROR, transfer bytes %d requested only %d\n", n,
1649 buflen);
1650 err = 0xff;
wdenkc6097192002-11-03 00:24:07 +00001651 goto AI_OUT;
1652 }
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001653 if ((n == 0) && (buflen < 0)) {
1654 printf("ERROR, transfer bytes %d requested %d\n", n, buflen);
1655 err = 0xff;
wdenkc6097192002-11-03 00:24:07 +00001656 goto AI_OUT;
1657 }
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001658 if (n != buflen) {
1659 debug("WARNING, transfer bytes %d not equal with requested %d\n",
1660 n, buflen);
wdenkc6097192002-11-03 00:24:07 +00001661 }
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001662 if (n != 0) { /* data transfer */
1663 debug("ATAPI_ISSUE: %d Bytes to transfer\n", n);
1664 /* we transfer shorts */
1665 n >>= 1;
wdenkc6097192002-11-03 00:24:07 +00001666 /* ok now decide if it is an in or output */
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001667 if ((ide_inb(device, ATA_SECT_CNT) & 0x02) == 0) {
1668 debug("Write to device\n");
1669 output_data_shorts(device, (unsigned short *) buffer,
1670 n);
wdenkc6097192002-11-03 00:24:07 +00001671 } else {
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001672 debug("Read from device @ %p shorts %d\n", buffer, n);
1673 input_data_shorts(device, (unsigned short *) buffer,
1674 n);
wdenkc6097192002-11-03 00:24:07 +00001675 }
1676 }
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001677 udelay(5000); /* seems that some CD ROMs need this... */
1678 mask = ATA_STAT_BUSY | ATA_STAT_ERR;
1679 res = 0;
1680 c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
wdenkc6097192002-11-03 00:24:07 +00001681 if ((c & ATA_STAT_ERR) == ATA_STAT_ERR) {
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001682 err = (ide_inb(device, ATA_ERROR_REG) >> 4);
1683 debug("atapi_issue 2 returned sense key %X status %X\n", err,
1684 c);
wdenkc6097192002-11-03 00:24:07 +00001685 } else {
1686 err = 0;
1687 }
1688AI_OUT:
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001689 ide_led(DEVICE_LED(device), 0); /* LED off */
wdenkc6097192002-11-03 00:24:07 +00001690 return (err);
1691}
1692
1693/*
1694 * sending the command to atapi_issue. If an status other than good
1695 * returns, an request_sense will be issued
1696 */
1697
Wolfgang Denka1be4762008-05-20 16:00:29 +02001698#define ATAPI_DRIVE_NOT_READY 100
wdenkc6097192002-11-03 00:24:07 +00001699#define ATAPI_UNIT_ATTN 10
1700
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001701unsigned char atapi_issue_autoreq(int device,
1702 unsigned char *ccb,
1703 int ccblen,
1704 unsigned char *buffer, int buflen)
wdenkc6097192002-11-03 00:24:07 +00001705{
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001706 unsigned char sense_data[18], sense_ccb[12];
1707 unsigned char res, key, asc, ascq;
1708 int notready, unitattn;
wdenkc6097192002-11-03 00:24:07 +00001709
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001710 unitattn = ATAPI_UNIT_ATTN;
1711 notready = ATAPI_DRIVE_NOT_READY;
wdenkc6097192002-11-03 00:24:07 +00001712
1713retry:
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001714 res = atapi_issue(device, ccb, ccblen, buffer, buflen);
1715 if (res == 0)
1716 return 0; /* Ok */
wdenkc6097192002-11-03 00:24:07 +00001717
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001718 if (res == 0xFF)
1719 return 0xFF; /* error */
wdenkc6097192002-11-03 00:24:07 +00001720
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001721 debug("(auto_req)atapi_issue returned sense key %X\n", res);
wdenkc6097192002-11-03 00:24:07 +00001722
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001723 memset(sense_ccb, 0, sizeof(sense_ccb));
1724 memset(sense_data, 0, sizeof(sense_data));
1725 sense_ccb[0] = ATAPI_CMD_REQ_SENSE;
1726 sense_ccb[4] = 18; /* allocation Length */
wdenkc6097192002-11-03 00:24:07 +00001727
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001728 res = atapi_issue(device, sense_ccb, 12, sense_data, 18);
1729 key = (sense_data[2] & 0xF);
1730 asc = (sense_data[12]);
1731 ascq = (sense_data[13]);
wdenkc6097192002-11-03 00:24:07 +00001732
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001733 debug("ATAPI_CMD_REQ_SENSE returned %x\n", res);
1734 debug(" Sense page: %02X key %02X ASC %02X ASCQ %02X\n",
1735 sense_data[0], key, asc, ascq);
wdenkc6097192002-11-03 00:24:07 +00001736
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001737 if ((key == 0))
1738 return 0; /* ok device ready */
wdenkc6097192002-11-03 00:24:07 +00001739
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001740 if ((key == 6) || (asc == 0x29) || (asc == 0x28)) { /* Unit Attention */
1741 if (unitattn-- > 0) {
1742 udelay(200 * 1000);
wdenkc6097192002-11-03 00:24:07 +00001743 goto retry;
1744 }
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001745 printf("Unit Attention, tried %d\n", ATAPI_UNIT_ATTN);
wdenkc6097192002-11-03 00:24:07 +00001746 goto error;
1747 }
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001748 if ((asc == 0x4) && (ascq == 0x1)) {
1749 /* not ready, but will be ready soon */
1750 if (notready-- > 0) {
1751 udelay(200 * 1000);
wdenkc6097192002-11-03 00:24:07 +00001752 goto retry;
1753 }
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001754 printf("Drive not ready, tried %d times\n",
1755 ATAPI_DRIVE_NOT_READY);
wdenkc6097192002-11-03 00:24:07 +00001756 goto error;
1757 }
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001758 if (asc == 0x3a) {
1759 debug("Media not present\n");
wdenkc6097192002-11-03 00:24:07 +00001760 goto error;
1761 }
wdenk452cfd62002-11-19 11:04:11 +00001762
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001763 printf("ERROR: Unknown Sense key %02X ASC %02X ASCQ %02X\n", key, asc,
1764 ascq);
wdenkc6097192002-11-03 00:24:07 +00001765error:
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001766 debug("ERROR Sense key %02X ASC %02X ASCQ %02X\n", key, asc, ascq);
wdenkc6097192002-11-03 00:24:07 +00001767 return (0xFF);
1768}
1769
1770
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001771static void atapi_inquiry(block_dev_desc_t *dev_desc)
wdenkc6097192002-11-03 00:24:07 +00001772{
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001773 unsigned char ccb[12]; /* Command descriptor block */
1774 unsigned char iobuf[64]; /* temp buf */
wdenkc6097192002-11-03 00:24:07 +00001775 unsigned char c;
1776 int device;
1777
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001778 device = dev_desc->dev;
1779 dev_desc->type = DEV_TYPE_UNKNOWN; /* not yet valid */
1780 dev_desc->block_read = atapi_read;
wdenkc6097192002-11-03 00:24:07 +00001781
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001782 memset(ccb, 0, sizeof(ccb));
1783 memset(iobuf, 0, sizeof(iobuf));
wdenkc6097192002-11-03 00:24:07 +00001784
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001785 ccb[0] = ATAPI_CMD_INQUIRY;
1786 ccb[4] = 40; /* allocation Legnth */
1787 c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *) iobuf, 40);
wdenkc6097192002-11-03 00:24:07 +00001788
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001789 debug("ATAPI_CMD_INQUIRY returned %x\n", c);
1790 if (c != 0)
wdenkc6097192002-11-03 00:24:07 +00001791 return;
1792
1793 /* copy device ident strings */
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001794 ident_cpy((unsigned char *) dev_desc->vendor, &iobuf[8], 8);
1795 ident_cpy((unsigned char *) dev_desc->product, &iobuf[16], 16);
1796 ident_cpy((unsigned char *) dev_desc->revision, &iobuf[32], 5);
wdenkc6097192002-11-03 00:24:07 +00001797
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001798 dev_desc->lun = 0;
1799 dev_desc->lba = 0;
1800 dev_desc->blksz = 0;
1801 dev_desc->type = iobuf[0] & 0x1f;
wdenkc6097192002-11-03 00:24:07 +00001802
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001803 if ((iobuf[1] & 0x80) == 0x80)
wdenkc6097192002-11-03 00:24:07 +00001804 dev_desc->removable = 1;
1805 else
1806 dev_desc->removable = 0;
1807
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001808 memset(ccb, 0, sizeof(ccb));
1809 memset(iobuf, 0, sizeof(iobuf));
1810 ccb[0] = ATAPI_CMD_START_STOP;
1811 ccb[4] = 0x03; /* start */
wdenkc6097192002-11-03 00:24:07 +00001812
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001813 c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *) iobuf, 0);
wdenkc6097192002-11-03 00:24:07 +00001814
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001815 debug("ATAPI_CMD_START_STOP returned %x\n", c);
1816 if (c != 0)
wdenkc6097192002-11-03 00:24:07 +00001817 return;
1818
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001819 memset(ccb, 0, sizeof(ccb));
1820 memset(iobuf, 0, sizeof(iobuf));
1821 c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *) iobuf, 0);
wdenkc6097192002-11-03 00:24:07 +00001822
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001823 debug("ATAPI_CMD_UNIT_TEST_READY returned %x\n", c);
1824 if (c != 0)
wdenkc6097192002-11-03 00:24:07 +00001825 return;
1826
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001827 memset(ccb, 0, sizeof(ccb));
1828 memset(iobuf, 0, sizeof(iobuf));
1829 ccb[0] = ATAPI_CMD_READ_CAP;
1830 c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *) iobuf, 8);
1831 debug("ATAPI_CMD_READ_CAP returned %x\n", c);
1832 if (c != 0)
wdenkc6097192002-11-03 00:24:07 +00001833 return;
1834
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001835 debug("Read Cap: LBA %02X%02X%02X%02X blksize %02X%02X%02X%02X\n",
1836 iobuf[0], iobuf[1], iobuf[2], iobuf[3],
1837 iobuf[4], iobuf[5], iobuf[6], iobuf[7]);
wdenkc6097192002-11-03 00:24:07 +00001838
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001839 dev_desc->lba = ((unsigned long) iobuf[0] << 24) +
1840 ((unsigned long) iobuf[1] << 16) +
1841 ((unsigned long) iobuf[2] << 8) + ((unsigned long) iobuf[3]);
1842 dev_desc->blksz = ((unsigned long) iobuf[4] << 24) +
1843 ((unsigned long) iobuf[5] << 16) +
1844 ((unsigned long) iobuf[6] << 8) + ((unsigned long) iobuf[7]);
wdenkc35ba4e2004-03-14 22:25:36 +00001845#ifdef CONFIG_LBA48
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001846 /* ATAPI devices cannot use 48bit addressing (ATA/ATAPI v7) */
1847 dev_desc->lba48 = 0;
wdenkc35ba4e2004-03-14 22:25:36 +00001848#endif
wdenkc6097192002-11-03 00:24:07 +00001849 return;
1850}
1851
1852
1853/*
1854 * atapi_read:
1855 * we transfer only one block per command, since the multiple DRQ per
1856 * command is not yet implemented
1857 */
1858#define ATAPI_READ_MAX_BYTES 2048 /* we read max 2kbytes */
1859#define ATAPI_READ_BLOCK_SIZE 2048 /* assuming CD part */
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001860#define ATAPI_READ_MAX_BLOCK (ATAPI_READ_MAX_BYTES/ATAPI_READ_BLOCK_SIZE)
wdenkc6097192002-11-03 00:24:07 +00001861
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001862ulong atapi_read(int device, lbaint_t blknr, ulong blkcnt, void *buffer)
wdenkc6097192002-11-03 00:24:07 +00001863{
1864 ulong n = 0;
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001865 unsigned char ccb[12]; /* Command descriptor block */
wdenkc6097192002-11-03 00:24:07 +00001866 ulong cnt;
1867
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001868 debug("atapi_read dev %d start %lX, blocks %lX buffer at %lX\n",
1869 device, blknr, blkcnt, (ulong) buffer);
wdenkc6097192002-11-03 00:24:07 +00001870
1871 do {
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001872 if (blkcnt > ATAPI_READ_MAX_BLOCK)
1873 cnt = ATAPI_READ_MAX_BLOCK;
1874 else
1875 cnt = blkcnt;
wdenkc6097192002-11-03 00:24:07 +00001876
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001877 ccb[0] = ATAPI_CMD_READ_12;
1878 ccb[1] = 0; /* reserved */
1879 ccb[2] = (unsigned char) (blknr >> 24) & 0xFF; /* MSB Block */
1880 ccb[3] = (unsigned char) (blknr >> 16) & 0xFF; /* */
1881 ccb[4] = (unsigned char) (blknr >> 8) & 0xFF;
1882 ccb[5] = (unsigned char) blknr & 0xFF; /* LSB Block */
1883 ccb[6] = (unsigned char) (cnt >> 24) & 0xFF; /* MSB Block cnt */
1884 ccb[7] = (unsigned char) (cnt >> 16) & 0xFF;
1885 ccb[8] = (unsigned char) (cnt >> 8) & 0xFF;
1886 ccb[9] = (unsigned char) cnt & 0xFF; /* LSB Block */
1887 ccb[10] = 0; /* reserved */
1888 ccb[11] = 0; /* reserved */
1889
1890 if (atapi_issue_autoreq(device, ccb, 12,
1891 (unsigned char *) buffer,
1892 cnt * ATAPI_READ_BLOCK_SIZE)
1893 == 0xFF) {
wdenkc6097192002-11-03 00:24:07 +00001894 return (n);
1895 }
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001896 n += cnt;
1897 blkcnt -= cnt;
1898 blknr += cnt;
1899 buffer += (cnt * ATAPI_READ_BLOCK_SIZE);
wdenkc6097192002-11-03 00:24:07 +00001900 } while (blkcnt > 0);
1901 return (n);
1902}
1903
1904/* ------------------------------------------------------------------------- */
1905
1906#endif /* CONFIG_ATAPI */
1907
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001908U_BOOT_CMD(ide, 5, 1, do_ide,
1909 "IDE sub-system",
1910 "reset - reset IDE controller\n"
1911 "ide info - show available IDE devices\n"
1912 "ide device [dev] - show or set current device\n"
1913 "ide part [dev] - print partition table of one or all IDE devices\n"
1914 "ide read addr blk# cnt\n"
1915 "ide write addr blk# cnt - read/write `cnt'"
1916 " blocks starting at block `blk#'\n"
1917 " to/from memory address `addr'");
wdenk57b2d802003-06-27 21:31:46 +00001918
Wolfgang Denk2bc10cf2011-10-29 09:41:40 +00001919U_BOOT_CMD(diskboot, 3, 1, do_diskboot,
1920 "boot from IDE device", "loadAddr dev:part");