wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1 | /* |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 2 | * (C) Copyright 2000-2011 |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 3 | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. |
| 4 | * |
| 5 | * See file CREDITS for list of people who contributed to this |
| 6 | * project. |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or |
| 9 | * modify it under the terms of the GNU General Public License as |
| 10 | * published by the Free Software Foundation; either version 2 of |
| 11 | * the License, or (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License |
| 19 | * along with this program; if not, write to the Free Software |
| 20 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
| 21 | * MA 02111-1307 USA |
| 22 | * |
| 23 | */ |
| 24 | |
| 25 | /* |
| 26 | * IDE support |
| 27 | */ |
Albert Aribaud | 113bfe4 | 2010-08-08 05:17:06 +0530 | [diff] [blame] | 28 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 29 | #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 Schocher | f98984c | 2007-08-28 17:39:14 +0200 | [diff] [blame] | 35 | #include <asm/io.h> |
Grant Likely | 735dd97 | 2007-02-20 09:04:34 +0100 | [diff] [blame] | 36 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 37 | #if defined(CONFIG_IDE_8xx_DIRECT) || defined(CONFIG_IDE_PCMCIA) |
| 38 | # include <pcmcia.h> |
| 39 | #endif |
Grant Likely | 735dd97 | 2007-02-20 09:04:34 +0100 | [diff] [blame] | 40 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 41 | #ifdef CONFIG_8xx |
| 42 | # include <mpc8xx.h> |
| 43 | #endif |
Grant Likely | 735dd97 | 2007-02-20 09:04:34 +0100 | [diff] [blame] | 44 | |
wdenk | 132ba5f | 2004-02-27 08:20:54 +0000 | [diff] [blame] | 45 | #ifdef CONFIG_MPC5xxx |
| 46 | #include <mpc5xxx.h> |
| 47 | #endif |
Grant Likely | 735dd97 | 2007-02-20 09:04:34 +0100 | [diff] [blame] | 48 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 49 | #include <ide.h> |
| 50 | #include <ata.h> |
Grant Likely | 735dd97 | 2007-02-20 09:04:34 +0100 | [diff] [blame] | 51 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 52 | #ifdef CONFIG_STATUS_LED |
| 53 | # include <status_led.h> |
| 54 | #endif |
Grant Likely | 735dd97 | 2007-02-20 09:04:34 +0100 | [diff] [blame] | 55 | |
wdenk | 5cf91d6 | 2004-04-23 20:32:05 +0000 | [diff] [blame] | 56 | #ifdef __PPC__ |
| 57 | # define EIEIO __asm__ volatile ("eieio") |
wdenk | 1a344f2 | 2005-02-03 23:00:49 +0000 | [diff] [blame] | 58 | # define SYNC __asm__ volatile ("sync") |
wdenk | 5cf91d6 | 2004-04-23 20:32:05 +0000 | [diff] [blame] | 59 | #else |
| 60 | # define EIEIO /* nothing */ |
wdenk | 1a344f2 | 2005-02-03 23:00:49 +0000 | [diff] [blame] | 61 | # define SYNC /* nothing */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 62 | #endif |
| 63 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 64 | /* ------------------------------------------------------------------------- */ |
| 65 | |
| 66 | /* Current I/O Device */ |
| 67 | static int curr_device = -1; |
| 68 | |
| 69 | /* Current offset for IDE0 / IDE1 bus access */ |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 70 | ulong ide_bus_offset[CONFIG_SYS_IDE_MAXBUS] = { |
| 71 | #if defined(CONFIG_SYS_ATA_IDE0_OFFSET) |
| 72 | CONFIG_SYS_ATA_IDE0_OFFSET, |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 73 | #endif |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 74 | #if defined(CONFIG_SYS_ATA_IDE1_OFFSET) && (CONFIG_SYS_IDE_MAXBUS > 1) |
| 75 | CONFIG_SYS_ATA_IDE1_OFFSET, |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 76 | #endif |
| 77 | }; |
| 78 | |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 79 | static int ide_bus_ok[CONFIG_SYS_IDE_MAXBUS]; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 80 | |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 81 | block_dev_desc_t ide_dev_desc[CONFIG_SYS_IDE_MAXDEVICE]; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 82 | /* ------------------------------------------------------------------------- */ |
| 83 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 84 | #ifdef CONFIG_IDE_RESET |
| 85 | static void ide_reset (void); |
| 86 | #else |
| 87 | #define ide_reset() /* dummy */ |
| 88 | #endif |
| 89 | |
| 90 | static void ide_ident (block_dev_desc_t *dev_desc); |
| 91 | static uchar ide_wait (int dev, ulong t); |
| 92 | |
| 93 | #define IDE_TIME_OUT 2000 /* 2 sec timeout */ |
| 94 | |
| 95 | #define ATAPI_TIME_OUT 7000 /* 7 sec timeout (5 sec seems to work...) */ |
| 96 | |
| 97 | #define IDE_SPIN_UP_TIME_OUT 5000 /* 5 sec spin-up timeout */ |
| 98 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 99 | static void ident_cpy (unsigned char *dest, unsigned char *src, unsigned int len); |
| 100 | |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 101 | #ifndef CONFIG_SYS_ATA_PORT_ADDR |
| 102 | #define CONFIG_SYS_ATA_PORT_ADDR(port) (port) |
Heiko Schocher | 566a494 | 2007-06-22 19:11:54 +0200 | [diff] [blame] | 103 | #endif |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 104 | |
| 105 | #ifdef CONFIG_ATAPI |
| 106 | static void atapi_inquiry(block_dev_desc_t *dev_desc); |
Grant Likely | eb867a7 | 2007-02-20 09:05:45 +0100 | [diff] [blame] | 107 | ulong atapi_read (int device, lbaint_t blknr, ulong blkcnt, void *buffer); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 108 | #endif |
| 109 | |
| 110 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 111 | /* ------------------------------------------------------------------------- */ |
| 112 | |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 113 | int do_ide(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[]) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 114 | { |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 115 | int rcode = 0; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 116 | |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 117 | switch (argc) { |
| 118 | case 0: |
| 119 | case 1: |
Simon Glass | 4c12eeb | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 120 | return CMD_RET_USAGE; |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 121 | case 2: |
| 122 | if (strncmp(argv[1], "res", 3) == 0) { |
| 123 | puts("\nReset IDE" |
| 124 | #ifdef CONFIG_IDE_8xx_DIRECT |
| 125 | " on PCMCIA " PCMCIA_SLOT_MSG |
| 126 | #endif |
| 127 | ": "); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 128 | |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 129 | ide_init(); |
| 130 | return 0; |
| 131 | } else if (strncmp(argv[1], "inf", 3) == 0) { |
| 132 | int i; |
| 133 | |
| 134 | putc('\n'); |
| 135 | |
| 136 | for (i = 0; i < CONFIG_SYS_IDE_MAXDEVICE; ++i) { |
| 137 | if (ide_dev_desc[i].type == DEV_TYPE_UNKNOWN) |
| 138 | continue; /* list only known devices */ |
| 139 | printf("IDE device %d: ", i); |
| 140 | dev_print(&ide_dev_desc[i]); |
| 141 | } |
| 142 | return 0; |
| 143 | |
| 144 | } else if (strncmp(argv[1], "dev", 3) == 0) { |
| 145 | if ((curr_device < 0) |
| 146 | || (curr_device >= CONFIG_SYS_IDE_MAXDEVICE)) { |
| 147 | puts("\nno IDE devices available\n"); |
| 148 | return 1; |
| 149 | } |
| 150 | printf("\nIDE device %d: ", curr_device); |
| 151 | dev_print(&ide_dev_desc[curr_device]); |
| 152 | return 0; |
| 153 | } else if (strncmp(argv[1], "part", 4) == 0) { |
| 154 | int dev, ok; |
| 155 | |
| 156 | for (ok = 0, dev = 0; |
| 157 | dev < CONFIG_SYS_IDE_MAXDEVICE; |
| 158 | ++dev) { |
| 159 | if (ide_dev_desc[dev].part_type != |
| 160 | PART_TYPE_UNKNOWN) { |
| 161 | ++ok; |
| 162 | if (dev) |
| 163 | putc('\n'); |
| 164 | print_part(&ide_dev_desc[dev]); |
| 165 | } |
| 166 | } |
| 167 | if (!ok) { |
| 168 | puts("\nno IDE devices available\n"); |
| 169 | rcode++; |
| 170 | } |
| 171 | return rcode; |
| 172 | } |
Simon Glass | 4c12eeb | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 173 | return CMD_RET_USAGE; |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 174 | case 3: |
| 175 | if (strncmp(argv[1], "dev", 3) == 0) { |
| 176 | int dev = (int) simple_strtoul(argv[2], NULL, 10); |
| 177 | |
| 178 | printf("\nIDE device %d: ", dev); |
| 179 | if (dev >= CONFIG_SYS_IDE_MAXDEVICE) { |
| 180 | puts("unknown device\n"); |
| 181 | return 1; |
| 182 | } |
| 183 | dev_print(&ide_dev_desc[dev]); |
| 184 | /*ide_print (dev); */ |
| 185 | |
| 186 | if (ide_dev_desc[dev].type == DEV_TYPE_UNKNOWN) |
| 187 | return 1; |
| 188 | |
| 189 | curr_device = dev; |
| 190 | |
| 191 | puts("... is now current device\n"); |
| 192 | |
| 193 | return 0; |
| 194 | } else if (strncmp(argv[1], "part", 4) == 0) { |
| 195 | int dev = (int) simple_strtoul(argv[2], NULL, 10); |
| 196 | |
| 197 | if (ide_dev_desc[dev].part_type != PART_TYPE_UNKNOWN) { |
| 198 | print_part(&ide_dev_desc[dev]); |
| 199 | } else { |
| 200 | printf("\nIDE device %d not available\n", |
| 201 | dev); |
| 202 | rcode = 1; |
| 203 | } |
| 204 | return rcode; |
| 205 | } |
| 206 | |
Simon Glass | 4c12eeb | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 207 | return CMD_RET_USAGE; |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 208 | default: |
| 209 | /* at least 4 args */ |
| 210 | |
| 211 | if (strcmp(argv[1], "read") == 0) { |
| 212 | ulong addr = simple_strtoul(argv[2], NULL, 16); |
| 213 | ulong cnt = simple_strtoul(argv[4], NULL, 16); |
| 214 | ulong n; |
| 215 | |
| 216 | #ifdef CONFIG_SYS_64BIT_LBA |
| 217 | lbaint_t blk = simple_strtoull(argv[3], NULL, 16); |
| 218 | |
| 219 | printf("\nIDE read: device %d block # %lld, count %ld ... ", |
| 220 | curr_device, blk, cnt); |
| 221 | #else |
| 222 | lbaint_t blk = simple_strtoul(argv[3], NULL, 16); |
| 223 | |
| 224 | printf("\nIDE read: device %d block # %ld, count %ld ... ", |
| 225 | curr_device, blk, cnt); |
| 226 | #endif |
| 227 | |
| 228 | n = ide_dev_desc[curr_device].block_read(curr_device, |
| 229 | blk, cnt, |
| 230 | (ulong *)addr); |
| 231 | /* flush cache after read */ |
| 232 | flush_cache(addr, |
| 233 | cnt * ide_dev_desc[curr_device].blksz); |
| 234 | |
| 235 | printf("%ld blocks read: %s\n", |
| 236 | n, (n == cnt) ? "OK" : "ERROR"); |
| 237 | if (n == cnt) |
| 238 | return 0; |
| 239 | else |
| 240 | return 1; |
| 241 | } else if (strcmp(argv[1], "write") == 0) { |
| 242 | ulong addr = simple_strtoul(argv[2], NULL, 16); |
| 243 | ulong cnt = simple_strtoul(argv[4], NULL, 16); |
| 244 | ulong n; |
| 245 | |
| 246 | #ifdef CONFIG_SYS_64BIT_LBA |
| 247 | lbaint_t blk = simple_strtoull(argv[3], NULL, 16); |
| 248 | |
| 249 | printf("\nIDE write: device %d block # %lld, count %ld ... ", |
| 250 | curr_device, blk, cnt); |
| 251 | #else |
| 252 | lbaint_t blk = simple_strtoul(argv[3], NULL, 16); |
| 253 | |
| 254 | printf("\nIDE write: device %d block # %ld, count %ld ... ", |
| 255 | curr_device, blk, cnt); |
| 256 | #endif |
| 257 | n = ide_write(curr_device, blk, cnt, (ulong *) addr); |
| 258 | |
| 259 | printf("%ld blocks written: %s\n", |
| 260 | n, (n == cnt) ? "OK" : "ERROR"); |
| 261 | if (n == cnt) |
| 262 | return 0; |
| 263 | else |
| 264 | return 1; |
| 265 | } else { |
Simon Glass | 4c12eeb | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 266 | return CMD_RET_USAGE; |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | return rcode; |
| 270 | } |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 271 | } |
| 272 | |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 273 | int do_diskboot(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[]) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 274 | { |
Rob Herring | 7405a13 | 2012-09-21 04:02:30 +0000 | [diff] [blame] | 275 | return common_diskboot(cmdtp, "ide", argc, argv); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 276 | } |
| 277 | |
| 278 | /* ------------------------------------------------------------------------- */ |
| 279 | |
Pavel Herrmann | 19be2ea | 2012-10-07 05:56:10 +0000 | [diff] [blame] | 280 | void __ide_led(uchar led, uchar status) |
| 281 | { |
| 282 | #if defined(CONFIG_IDE_LED) && defined(PER8_BASE) /* required by LED_PORT */ |
| 283 | static uchar led_buffer; /* Buffer for current LED status */ |
| 284 | |
| 285 | uchar *led_port = LED_PORT; |
| 286 | |
| 287 | if (status) /* switch LED on */ |
| 288 | led_buffer |= led; |
| 289 | else /* switch LED off */ |
| 290 | led_buffer &= ~led; |
| 291 | |
| 292 | *led_port = led_buffer; |
| 293 | #endif |
| 294 | } |
| 295 | |
| 296 | void ide_led(uchar led, uchar status) |
| 297 | __attribute__ ((weak, alias("__ide_led"))); |
| 298 | |
| 299 | #ifndef CONFIG_IDE_LED /* define LED macros, they are not used anyways */ |
| 300 | # define DEVICE_LED(x) 0 |
| 301 | # define LED_IDE1 1 |
| 302 | # define LED_IDE2 2 |
| 303 | #endif |
| 304 | |
| 305 | /* ------------------------------------------------------------------------- */ |
| 306 | |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 307 | inline void __ide_outb(int dev, int port, unsigned char val) |
Stefan Roese | f2302d4 | 2008-08-06 14:05:38 +0200 | [diff] [blame] | 308 | { |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 309 | debug("ide_outb (dev= %d, port= 0x%x, val= 0x%02x) : @ 0x%08lx\n", |
| 310 | dev, port, val, |
| 311 | (ATA_CURR_BASE(dev) + CONFIG_SYS_ATA_PORT_ADDR(port))); |
Macpaul Lin | 0abddf8 | 2011-04-11 20:45:32 +0000 | [diff] [blame] | 312 | |
| 313 | #if defined(CONFIG_IDE_AHB) |
| 314 | if (port) { |
| 315 | /* write command */ |
| 316 | ide_write_register(dev, port, val); |
| 317 | } else { |
| 318 | /* write data */ |
| 319 | outb(val, (ATA_CURR_BASE(dev))); |
| 320 | } |
| 321 | #else |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 322 | outb(val, (ATA_CURR_BASE(dev) + CONFIG_SYS_ATA_PORT_ADDR(port))); |
Macpaul Lin | 0abddf8 | 2011-04-11 20:45:32 +0000 | [diff] [blame] | 323 | #endif |
Stefan Roese | f2302d4 | 2008-08-06 14:05:38 +0200 | [diff] [blame] | 324 | } |
Macpaul Lin | 0abddf8 | 2011-04-11 20:45:32 +0000 | [diff] [blame] | 325 | |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 326 | void ide_outb(int dev, int port, unsigned char val) |
| 327 | __attribute__ ((weak, alias("__ide_outb"))); |
Stefan Roese | f2302d4 | 2008-08-06 14:05:38 +0200 | [diff] [blame] | 328 | |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 329 | inline unsigned char __ide_inb(int dev, int port) |
Stefan Roese | f2302d4 | 2008-08-06 14:05:38 +0200 | [diff] [blame] | 330 | { |
| 331 | uchar val; |
Macpaul Lin | 0abddf8 | 2011-04-11 20:45:32 +0000 | [diff] [blame] | 332 | |
| 333 | #if defined(CONFIG_IDE_AHB) |
| 334 | val = ide_read_register(dev, port); |
| 335 | #else |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 336 | val = inb((ATA_CURR_BASE(dev) + CONFIG_SYS_ATA_PORT_ADDR(port))); |
Macpaul Lin | 0abddf8 | 2011-04-11 20:45:32 +0000 | [diff] [blame] | 337 | #endif |
| 338 | |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 339 | debug("ide_inb (dev= %d, port= 0x%x) : @ 0x%08lx -> 0x%02x\n", |
| 340 | dev, port, |
| 341 | (ATA_CURR_BASE(dev) + CONFIG_SYS_ATA_PORT_ADDR(port)), val); |
Stefan Roese | f2302d4 | 2008-08-06 14:05:38 +0200 | [diff] [blame] | 342 | return val; |
| 343 | } |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 344 | |
Kim Phillips | 2df72b8 | 2009-05-19 12:53:36 -0500 | [diff] [blame] | 345 | unsigned char ide_inb(int dev, int port) |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 346 | __attribute__ ((weak, alias("__ide_inb"))); |
Stefan Roese | f2302d4 | 2008-08-06 14:05:38 +0200 | [diff] [blame] | 347 | |
Steven A. Falco | 36c2d30 | 2008-08-15 15:34:10 -0400 | [diff] [blame] | 348 | #ifdef CONFIG_TUNE_PIO |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 349 | inline int __ide_set_piomode(int pio_mode) |
Steven A. Falco | 36c2d30 | 2008-08-15 15:34:10 -0400 | [diff] [blame] | 350 | { |
| 351 | return 0; |
| 352 | } |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 353 | |
| 354 | inline int ide_set_piomode(int pio_mode) |
| 355 | __attribute__ ((weak, alias("__ide_set_piomode"))); |
Steven A. Falco | 36c2d30 | 2008-08-15 15:34:10 -0400 | [diff] [blame] | 356 | #endif |
| 357 | |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 358 | void ide_init(void) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 359 | { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 360 | unsigned char c; |
| 361 | int i, bus; |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 362 | |
wdenk | 9fd5e31 | 2003-12-07 23:55:12 +0000 | [diff] [blame] | 363 | #ifdef CONFIG_IDE_8xx_PCCARD |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 364 | extern int ide_devices_found; /* Initialized in check_ide_device() */ |
| 365 | #endif /* CONFIG_IDE_8xx_PCCARD */ |
wdenk | 9fd5e31 | 2003-12-07 23:55:12 +0000 | [diff] [blame] | 366 | |
| 367 | #ifdef CONFIG_IDE_PREINIT |
| 368 | WATCHDOG_RESET(); |
| 369 | |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 370 | if (ide_preinit()) { |
| 371 | puts("ide_preinit failed\n"); |
wdenk | 9fd5e31 | 2003-12-07 23:55:12 +0000 | [diff] [blame] | 372 | return; |
| 373 | } |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 374 | #endif /* CONFIG_IDE_PREINIT */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 375 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 376 | WATCHDOG_RESET(); |
| 377 | |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 378 | /* |
| 379 | * Reset the IDE just to be sure. |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 380 | * Light LED's to show |
| 381 | */ |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 382 | ide_led((LED_IDE1 | LED_IDE2), 1); /* LED's on */ |
| 383 | |
| 384 | /* ATAPI Drives seems to need a proper IDE Reset */ |
| 385 | ide_reset(); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 386 | |
Pavel Herrmann | 8d1165e11a | 2012-10-09 07:01:56 +0000 | [diff] [blame] | 387 | #ifdef CONFIG_IDE_INIT_POSTRESET |
| 388 | WATCHDOG_RESET(); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 389 | |
Pavel Herrmann | 8d1165e11a | 2012-10-09 07:01:56 +0000 | [diff] [blame] | 390 | if (ide_init_postreset()) { |
| 391 | puts("ide_preinit_postreset failed\n"); |
| 392 | return; |
| 393 | } |
| 394 | #endif /* CONFIG_IDE_INIT_POSTRESET */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 395 | |
| 396 | /* |
| 397 | * Wait for IDE to get ready. |
| 398 | * According to spec, this can take up to 31 seconds! |
| 399 | */ |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 400 | for (bus = 0; bus < CONFIG_SYS_IDE_MAXBUS; ++bus) { |
| 401 | int dev = |
| 402 | bus * (CONFIG_SYS_IDE_MAXDEVICE / |
| 403 | CONFIG_SYS_IDE_MAXBUS); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 404 | |
wdenk | 6069ff2 | 2003-02-28 00:49:47 +0000 | [diff] [blame] | 405 | #ifdef CONFIG_IDE_8xx_PCCARD |
| 406 | /* Skip non-ide devices from probing */ |
| 407 | if ((ide_devices_found & (1 << bus)) == 0) { |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 408 | ide_led((LED_IDE1 | LED_IDE2), 0); /* LED's off */ |
wdenk | 6069ff2 | 2003-02-28 00:49:47 +0000 | [diff] [blame] | 409 | continue; |
| 410 | } |
| 411 | #endif |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 412 | printf("Bus %d: ", bus); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 413 | |
| 414 | ide_bus_ok[bus] = 0; |
| 415 | |
| 416 | /* Select device |
| 417 | */ |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 418 | udelay(100000); /* 100 ms */ |
| 419 | ide_outb(dev, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(dev)); |
| 420 | udelay(100000); /* 100 ms */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 421 | i = 0; |
| 422 | do { |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 423 | udelay(10000); /* 10 ms */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 424 | |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 425 | c = ide_inb(dev, ATA_STATUS); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 426 | i++; |
| 427 | if (i > (ATA_RESET_TIME * 100)) { |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 428 | puts("** Timeout **\n"); |
| 429 | /* LED's off */ |
| 430 | ide_led((LED_IDE1 | LED_IDE2), 0); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 431 | return; |
| 432 | } |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 433 | if ((i >= 100) && ((i % 100) == 0)) |
| 434 | putc('.'); |
| 435 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 436 | } while (c & ATA_STAT_BUSY); |
| 437 | |
| 438 | if (c & (ATA_STAT_BUSY | ATA_STAT_FAULT)) { |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 439 | puts("not available "); |
| 440 | debug("Status = 0x%02X ", c); |
| 441 | #ifndef CONFIG_ATAPI /* ATAPI Devices do not set DRDY */ |
| 442 | } else if ((c & ATA_STAT_READY) == 0) { |
| 443 | puts("not available "); |
| 444 | debug("Status = 0x%02X ", c); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 445 | #endif |
| 446 | } else { |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 447 | puts("OK "); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 448 | ide_bus_ok[bus] = 1; |
| 449 | } |
| 450 | WATCHDOG_RESET(); |
| 451 | } |
wdenk | c7de829 | 2002-11-19 11:04:11 +0000 | [diff] [blame] | 452 | |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 453 | putc('\n'); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 454 | |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 455 | ide_led((LED_IDE1 | LED_IDE2), 0); /* LED's off */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 456 | |
| 457 | curr_device = -1; |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 458 | for (i = 0; i < CONFIG_SYS_IDE_MAXDEVICE; ++i) { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 459 | int led = (IDE_BUS(i) == 0) ? LED_IDE1 : LED_IDE2; |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 460 | ide_dev_desc[i].type = DEV_TYPE_UNKNOWN; |
| 461 | ide_dev_desc[i].if_type = IF_TYPE_IDE; |
| 462 | ide_dev_desc[i].dev = i; |
| 463 | ide_dev_desc[i].part_type = PART_TYPE_UNKNOWN; |
| 464 | ide_dev_desc[i].blksz = 0; |
| 465 | ide_dev_desc[i].lba = 0; |
| 466 | ide_dev_desc[i].block_read = ide_read; |
Macpaul Lin | 0abddf8 | 2011-04-11 20:45:32 +0000 | [diff] [blame] | 467 | ide_dev_desc[i].block_write = ide_write; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 468 | if (!ide_bus_ok[IDE_BUS(i)]) |
| 469 | continue; |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 470 | ide_led(led, 1); /* LED on */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 471 | ide_ident(&ide_dev_desc[i]); |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 472 | ide_led(led, 0); /* LED off */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 473 | dev_print(&ide_dev_desc[i]); |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 474 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 475 | if ((ide_dev_desc[i].lba > 0) && (ide_dev_desc[i].blksz > 0)) { |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 476 | /* initialize partition type */ |
| 477 | init_part(&ide_dev_desc[i]); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 478 | if (curr_device < 0) |
| 479 | curr_device = i; |
| 480 | } |
| 481 | } |
| 482 | WATCHDOG_RESET(); |
| 483 | } |
| 484 | |
| 485 | /* ------------------------------------------------------------------------- */ |
| 486 | |
Matthew McClintock | df3fc52 | 2011-05-24 05:31:19 +0000 | [diff] [blame] | 487 | #ifdef CONFIG_PARTITIONS |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 488 | block_dev_desc_t *ide_get_dev(int dev) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 489 | { |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 490 | return (dev < CONFIG_SYS_IDE_MAXDEVICE) ? &ide_dev_desc[dev] : NULL; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 491 | } |
Matthew McClintock | df3fc52 | 2011-05-24 05:31:19 +0000 | [diff] [blame] | 492 | #endif |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 493 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 494 | /* ------------------------------------------------------------------------- */ |
| 495 | |
Pavel Herrmann | f5b82c0 | 2012-10-09 07:04:39 +0000 | [diff] [blame] | 496 | void ide_input_swap_data(int dev, ulong *sect_buf, int words) |
| 497 | __attribute__ ((weak, alias("__ide_input_swap_data"))); |
| 498 | |
| 499 | void ide_input_data(int dev, ulong *sect_buf, int words) |
| 500 | __attribute__ ((weak, alias("__ide_input_data"))); |
| 501 | |
| 502 | void ide_output_data(int dev, const ulong *sect_buf, int words) |
| 503 | __attribute__ ((weak, alias("__ide_output_data"))); |
| 504 | |
wdenk | 5da627a | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 505 | /* We only need to swap data if we are running on a big endian cpu. */ |
| 506 | /* But Au1x00 cpu:s already swaps data in big endian mode! */ |
Tom Rini | 61c0d6a | 2012-09-26 15:20:33 -0700 | [diff] [blame] | 507 | #if defined(__LITTLE_ENDIAN) || defined(CONFIG_SOC_AU1X00) |
Pavel Herrmann | f5b82c0 | 2012-10-09 07:04:39 +0000 | [diff] [blame] | 508 | void __ide_input_swap_data(int dev, ulong *sect_buf, int words) |
| 509 | { |
| 510 | ide_input_data(dev, sect_buf, words); |
| 511 | } |
wdenk | 5da627a | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 512 | #else |
Pavel Herrmann | f5b82c0 | 2012-10-09 07:04:39 +0000 | [diff] [blame] | 513 | void __ide_input_swap_data(int dev, ulong *sect_buf, int words) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 514 | { |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 515 | volatile ushort *pbuf = |
| 516 | (ushort *) (ATA_CURR_BASE(dev) + ATA_DATA_REG); |
| 517 | ushort *dbuf = (ushort *) sect_buf; |
wdenk | 1a344f2 | 2005-02-03 23:00:49 +0000 | [diff] [blame] | 518 | |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 519 | debug("in input swap data base for read is %lx\n", |
| 520 | (unsigned long) pbuf); |
wdenk | 1a344f2 | 2005-02-03 23:00:49 +0000 | [diff] [blame] | 521 | |
| 522 | while (words--) { |
Wolfgang Denk | 0c32d96 | 2006-06-16 17:32:31 +0200 | [diff] [blame] | 523 | #ifdef __MIPS__ |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 524 | *dbuf++ = swab16p((u16 *) pbuf); |
| 525 | *dbuf++ = swab16p((u16 *) pbuf); |
Heiko Schocher | 566a494 | 2007-06-22 19:11:54 +0200 | [diff] [blame] | 526 | #elif defined(CONFIG_PCS440EP) |
| 527 | *dbuf++ = *pbuf; |
| 528 | *dbuf++ = *pbuf; |
Wolfgang Denk | 0c32d96 | 2006-06-16 17:32:31 +0200 | [diff] [blame] | 529 | #else |
wdenk | 1a344f2 | 2005-02-03 23:00:49 +0000 | [diff] [blame] | 530 | *dbuf++ = ld_le16(pbuf); |
| 531 | *dbuf++ = ld_le16(pbuf); |
Wolfgang Denk | 0c32d96 | 2006-06-16 17:32:31 +0200 | [diff] [blame] | 532 | #endif /* !MIPS */ |
wdenk | 1a344f2 | 2005-02-03 23:00:49 +0000 | [diff] [blame] | 533 | } |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 534 | } |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 535 | #endif /* __LITTLE_ENDIAN || CONFIG_AU1X00 */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 536 | |
wdenk | 2262cfe | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 537 | |
Albert Aribaud | f2a37fc | 2010-08-08 05:17:05 +0530 | [diff] [blame] | 538 | #if defined(CONFIG_IDE_SWAP_IO) |
Pavel Herrmann | f5b82c0 | 2012-10-09 07:04:39 +0000 | [diff] [blame] | 539 | void __ide_output_data(int dev, const ulong *sect_buf, int words) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 540 | { |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 541 | ushort *dbuf; |
| 542 | volatile ushort *pbuf; |
wdenk | 1a344f2 | 2005-02-03 23:00:49 +0000 | [diff] [blame] | 543 | |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 544 | pbuf = (ushort *) (ATA_CURR_BASE(dev) + ATA_DATA_REG); |
| 545 | dbuf = (ushort *) sect_buf; |
wdenk | 1a344f2 | 2005-02-03 23:00:49 +0000 | [diff] [blame] | 546 | while (words--) { |
Heiko Schocher | 566a494 | 2007-06-22 19:11:54 +0200 | [diff] [blame] | 547 | #if defined(CONFIG_PCS440EP) |
| 548 | /* not tested, because CF was write protected */ |
| 549 | EIEIO; |
| 550 | *pbuf = ld_le16(dbuf++); |
| 551 | EIEIO; |
| 552 | *pbuf = ld_le16(dbuf++); |
| 553 | #else |
wdenk | 1a344f2 | 2005-02-03 23:00:49 +0000 | [diff] [blame] | 554 | EIEIO; |
| 555 | *pbuf = *dbuf++; |
| 556 | EIEIO; |
| 557 | *pbuf = *dbuf++; |
Heiko Schocher | 566a494 | 2007-06-22 19:11:54 +0200 | [diff] [blame] | 558 | #endif |
wdenk | 1a344f2 | 2005-02-03 23:00:49 +0000 | [diff] [blame] | 559 | } |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 560 | } |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 561 | #else /* ! CONFIG_IDE_SWAP_IO */ |
Pavel Herrmann | f5b82c0 | 2012-10-09 07:04:39 +0000 | [diff] [blame] | 562 | void __ide_output_data(int dev, const ulong *sect_buf, int words) |
wdenk | 2262cfe | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 563 | { |
Macpaul Lin | 0abddf8 | 2011-04-11 20:45:32 +0000 | [diff] [blame] | 564 | #if defined(CONFIG_IDE_AHB) |
| 565 | ide_write_data(dev, sect_buf, words); |
| 566 | #else |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 567 | outsw(ATA_CURR_BASE(dev) + ATA_DATA_REG, sect_buf, words << 1); |
Macpaul Lin | 0abddf8 | 2011-04-11 20:45:32 +0000 | [diff] [blame] | 568 | #endif |
wdenk | 2262cfe | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 569 | } |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 570 | #endif /* CONFIG_IDE_SWAP_IO */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 571 | |
Albert Aribaud | f2a37fc | 2010-08-08 05:17:05 +0530 | [diff] [blame] | 572 | #if defined(CONFIG_IDE_SWAP_IO) |
Pavel Herrmann | f5b82c0 | 2012-10-09 07:04:39 +0000 | [diff] [blame] | 573 | void __ide_input_data(int dev, ulong *sect_buf, int words) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 574 | { |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 575 | ushort *dbuf; |
| 576 | volatile ushort *pbuf; |
wdenk | 1a344f2 | 2005-02-03 23:00:49 +0000 | [diff] [blame] | 577 | |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 578 | pbuf = (ushort *) (ATA_CURR_BASE(dev) + ATA_DATA_REG); |
| 579 | dbuf = (ushort *) sect_buf; |
wdenk | 1a344f2 | 2005-02-03 23:00:49 +0000 | [diff] [blame] | 580 | |
| 581 | debug("in input data base for read is %lx\n", (unsigned long) pbuf); |
| 582 | |
| 583 | while (words--) { |
Heiko Schocher | 566a494 | 2007-06-22 19:11:54 +0200 | [diff] [blame] | 584 | #if defined(CONFIG_PCS440EP) |
| 585 | EIEIO; |
| 586 | *dbuf++ = ld_le16(pbuf); |
| 587 | EIEIO; |
| 588 | *dbuf++ = ld_le16(pbuf); |
| 589 | #else |
wdenk | 1a344f2 | 2005-02-03 23:00:49 +0000 | [diff] [blame] | 590 | EIEIO; |
| 591 | *dbuf++ = *pbuf; |
| 592 | EIEIO; |
| 593 | *dbuf++ = *pbuf; |
Heiko Schocher | 566a494 | 2007-06-22 19:11:54 +0200 | [diff] [blame] | 594 | #endif |
wdenk | 1a344f2 | 2005-02-03 23:00:49 +0000 | [diff] [blame] | 595 | } |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 596 | } |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 597 | #else /* ! CONFIG_IDE_SWAP_IO */ |
Pavel Herrmann | f5b82c0 | 2012-10-09 07:04:39 +0000 | [diff] [blame] | 598 | void __ide_input_data(int dev, ulong *sect_buf, int words) |
wdenk | 2262cfe | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 599 | { |
Macpaul Lin | 0abddf8 | 2011-04-11 20:45:32 +0000 | [diff] [blame] | 600 | #if defined(CONFIG_IDE_AHB) |
| 601 | ide_read_data(dev, sect_buf, words); |
| 602 | #else |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 603 | insw(ATA_CURR_BASE(dev) + ATA_DATA_REG, sect_buf, words << 1); |
Macpaul Lin | 0abddf8 | 2011-04-11 20:45:32 +0000 | [diff] [blame] | 604 | #endif |
wdenk | 2262cfe | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 605 | } |
| 606 | |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 607 | #endif /* CONFIG_IDE_SWAP_IO */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 608 | |
| 609 | /* ------------------------------------------------------------------------- |
| 610 | */ |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 611 | static void ide_ident(block_dev_desc_t *dev_desc) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 612 | { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 613 | unsigned char c; |
Marek Vasut | b18eabf | 2011-08-20 09:15:13 +0000 | [diff] [blame] | 614 | hd_driveid_t iop; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 615 | |
wdenk | 64f70be | 2004-09-28 20:34:50 +0000 | [diff] [blame] | 616 | #ifdef CONFIG_ATAPI |
| 617 | int retries = 0; |
wdenk | c7de829 | 2002-11-19 11:04:11 +0000 | [diff] [blame] | 618 | #endif |
| 619 | |
Steven A. Falco | 36c2d30 | 2008-08-15 15:34:10 -0400 | [diff] [blame] | 620 | #ifdef CONFIG_TUNE_PIO |
| 621 | int pio_mode; |
| 622 | #endif |
| 623 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 624 | #if 0 |
| 625 | int mode, cycle_time; |
| 626 | #endif |
| 627 | int device; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 628 | |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 629 | device = dev_desc->dev; |
| 630 | printf(" Device %d: ", device); |
| 631 | |
| 632 | ide_led(DEVICE_LED(device), 1); /* LED on */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 633 | /* Select device |
| 634 | */ |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 635 | ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device)); |
| 636 | dev_desc->if_type = IF_TYPE_IDE; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 637 | #ifdef CONFIG_ATAPI |
wdenk | c7de829 | 2002-11-19 11:04:11 +0000 | [diff] [blame] | 638 | |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 639 | retries = 0; |
wdenk | c7de829 | 2002-11-19 11:04:11 +0000 | [diff] [blame] | 640 | |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 641 | /* Warning: This will be tricky to read */ |
| 642 | while (retries <= 1) { |
| 643 | /* check signature */ |
| 644 | if ((ide_inb(device, ATA_SECT_CNT) == 0x01) && |
| 645 | (ide_inb(device, ATA_SECT_NUM) == 0x01) && |
| 646 | (ide_inb(device, ATA_CYL_LOW) == 0x14) && |
| 647 | (ide_inb(device, ATA_CYL_HIGH) == 0xEB)) { |
| 648 | /* ATAPI Signature found */ |
| 649 | dev_desc->if_type = IF_TYPE_ATAPI; |
| 650 | /* |
| 651 | * Start Ident Command |
| 652 | */ |
| 653 | ide_outb(device, ATA_COMMAND, ATAPI_CMD_IDENT); |
| 654 | /* |
| 655 | * Wait for completion - ATAPI devices need more time |
| 656 | * to become ready |
| 657 | */ |
| 658 | c = ide_wait(device, ATAPI_TIME_OUT); |
| 659 | } else |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 660 | #endif |
wdenk | 1a344f2 | 2005-02-03 23:00:49 +0000 | [diff] [blame] | 661 | { |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 662 | /* |
| 663 | * Start Ident Command |
| 664 | */ |
| 665 | ide_outb(device, ATA_COMMAND, ATA_CMD_IDENT); |
| 666 | |
| 667 | /* |
| 668 | * Wait for completion |
| 669 | */ |
| 670 | c = ide_wait(device, IDE_TIME_OUT); |
wdenk | 1a344f2 | 2005-02-03 23:00:49 +0000 | [diff] [blame] | 671 | } |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 672 | ide_led(DEVICE_LED(device), 0); /* LED off */ |
| 673 | |
| 674 | if (((c & ATA_STAT_DRQ) == 0) || |
| 675 | ((c & (ATA_STAT_FAULT | ATA_STAT_ERR)) != 0)) { |
wdenk | 64f70be | 2004-09-28 20:34:50 +0000 | [diff] [blame] | 676 | #ifdef CONFIG_ATAPI |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 677 | { |
| 678 | /* |
| 679 | * Need to soft reset the device |
| 680 | * in case it's an ATAPI... |
| 681 | */ |
| 682 | debug("Retrying...\n"); |
| 683 | ide_outb(device, ATA_DEV_HD, |
| 684 | ATA_LBA | ATA_DEVICE(device)); |
| 685 | udelay(100000); |
| 686 | ide_outb(device, ATA_COMMAND, 0x08); |
| 687 | udelay(500000); /* 500 ms */ |
| 688 | } |
| 689 | /* |
| 690 | * Select device |
| 691 | */ |
| 692 | ide_outb(device, ATA_DEV_HD, |
| 693 | ATA_LBA | ATA_DEVICE(device)); |
| 694 | retries++; |
| 695 | #else |
| 696 | return; |
| 697 | #endif |
| 698 | } |
| 699 | #ifdef CONFIG_ATAPI |
| 700 | else |
| 701 | break; |
| 702 | } /* see above - ugly to read */ |
wdenk | 64f70be | 2004-09-28 20:34:50 +0000 | [diff] [blame] | 703 | |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 704 | if (retries == 2) /* Not found */ |
wdenk | 64f70be | 2004-09-28 20:34:50 +0000 | [diff] [blame] | 705 | return; |
| 706 | #endif |
wdenk | c7de829 | 2002-11-19 11:04:11 +0000 | [diff] [blame] | 707 | |
Pavel Herrmann | f5b82c0 | 2012-10-09 07:04:39 +0000 | [diff] [blame] | 708 | ide_input_swap_data(device, (ulong *)&iop, ATA_SECTORWORDS); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 709 | |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 710 | ident_cpy((unsigned char *) dev_desc->revision, iop.fw_rev, |
| 711 | sizeof(dev_desc->revision)); |
| 712 | ident_cpy((unsigned char *) dev_desc->vendor, iop.model, |
| 713 | sizeof(dev_desc->vendor)); |
| 714 | ident_cpy((unsigned char *) dev_desc->product, iop.serial_no, |
| 715 | sizeof(dev_desc->product)); |
wdenk | c3f9d49 | 2004-03-14 00:59:59 +0000 | [diff] [blame] | 716 | #ifdef __LITTLE_ENDIAN |
| 717 | /* |
Richard Retanubun | bcdf1d2 | 2008-11-06 14:01:51 -0500 | [diff] [blame] | 718 | * firmware revision, model, and serial number have Big Endian Byte |
| 719 | * order in Word. Convert all three to little endian. |
wdenk | c3f9d49 | 2004-03-14 00:59:59 +0000 | [diff] [blame] | 720 | * |
| 721 | * See CF+ and CompactFlash Specification Revision 2.0: |
Richard Retanubun | bcdf1d2 | 2008-11-06 14:01:51 -0500 | [diff] [blame] | 722 | * 6.2.1.6: Identify Drive, Table 39 for more details |
wdenk | c3f9d49 | 2004-03-14 00:59:59 +0000 | [diff] [blame] | 723 | */ |
| 724 | |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 725 | strswab(dev_desc->revision); |
| 726 | strswab(dev_desc->vendor); |
| 727 | strswab(dev_desc->product); |
wdenk | c3f9d49 | 2004-03-14 00:59:59 +0000 | [diff] [blame] | 728 | #endif /* __LITTLE_ENDIAN */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 729 | |
Marek Vasut | b18eabf | 2011-08-20 09:15:13 +0000 | [diff] [blame] | 730 | if ((iop.config & 0x0080) == 0x0080) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 731 | dev_desc->removable = 1; |
| 732 | else |
| 733 | dev_desc->removable = 0; |
| 734 | |
Steven A. Falco | 36c2d30 | 2008-08-15 15:34:10 -0400 | [diff] [blame] | 735 | #ifdef CONFIG_TUNE_PIO |
| 736 | /* Mode 0 - 2 only, are directly determined by word 51. */ |
Marek Vasut | b18eabf | 2011-08-20 09:15:13 +0000 | [diff] [blame] | 737 | pio_mode = iop.tPIO; |
Steven A. Falco | 36c2d30 | 2008-08-15 15:34:10 -0400 | [diff] [blame] | 738 | if (pio_mode > 2) { |
| 739 | printf("WARNING: Invalid PIO (word 51 = %d).\n", pio_mode); |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 740 | /* Force it to dead slow, and hope for the best... */ |
| 741 | pio_mode = 0; |
Steven A. Falco | 36c2d30 | 2008-08-15 15:34:10 -0400 | [diff] [blame] | 742 | } |
| 743 | |
| 744 | /* Any CompactFlash Storage Card that supports PIO mode 3 or above |
| 745 | * shall set bit 1 of word 53 to one and support the fields contained |
| 746 | * in words 64 through 70. |
| 747 | */ |
Marek Vasut | b18eabf | 2011-08-20 09:15:13 +0000 | [diff] [blame] | 748 | if (iop.field_valid & 0x02) { |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 749 | /* |
| 750 | * Mode 3 and above are possible. Check in order from slow |
Steven A. Falco | 36c2d30 | 2008-08-15 15:34:10 -0400 | [diff] [blame] | 751 | * to fast, so we wind up with the highest mode allowed. |
| 752 | */ |
Marek Vasut | b18eabf | 2011-08-20 09:15:13 +0000 | [diff] [blame] | 753 | if (iop.eide_pio_modes & 0x01) |
Steven A. Falco | 36c2d30 | 2008-08-15 15:34:10 -0400 | [diff] [blame] | 754 | pio_mode = 3; |
Marek Vasut | b18eabf | 2011-08-20 09:15:13 +0000 | [diff] [blame] | 755 | if (iop.eide_pio_modes & 0x02) |
Steven A. Falco | 36c2d30 | 2008-08-15 15:34:10 -0400 | [diff] [blame] | 756 | pio_mode = 4; |
Marek Vasut | b18eabf | 2011-08-20 09:15:13 +0000 | [diff] [blame] | 757 | if (ata_id_is_cfa((u16 *)&iop)) { |
| 758 | if ((iop.cf_advanced_caps & 0x07) == 0x01) |
Steven A. Falco | 36c2d30 | 2008-08-15 15:34:10 -0400 | [diff] [blame] | 759 | pio_mode = 5; |
Marek Vasut | b18eabf | 2011-08-20 09:15:13 +0000 | [diff] [blame] | 760 | if ((iop.cf_advanced_caps & 0x07) == 0x02) |
Steven A. Falco | 36c2d30 | 2008-08-15 15:34:10 -0400 | [diff] [blame] | 761 | pio_mode = 6; |
| 762 | } |
| 763 | } |
| 764 | |
| 765 | /* System-specific, depends on bus speeds, etc. */ |
| 766 | ide_set_piomode(pio_mode); |
| 767 | #endif /* CONFIG_TUNE_PIO */ |
| 768 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 769 | #if 0 |
| 770 | /* |
| 771 | * Drive PIO mode autoselection |
| 772 | */ |
Marek Vasut | b18eabf | 2011-08-20 09:15:13 +0000 | [diff] [blame] | 773 | mode = iop.tPIO; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 774 | |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 775 | printf("tPIO = 0x%02x = %d\n", mode, mode); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 776 | if (mode > 2) { /* 2 is maximum allowed tPIO value */ |
| 777 | mode = 2; |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 778 | debug("Override tPIO -> 2\n"); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 779 | } |
Marek Vasut | b18eabf | 2011-08-20 09:15:13 +0000 | [diff] [blame] | 780 | if (iop.field_valid & 2) { /* drive implements ATA2? */ |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 781 | debug("Drive implements ATA2\n"); |
Marek Vasut | b18eabf | 2011-08-20 09:15:13 +0000 | [diff] [blame] | 782 | if (iop.capability & 8) { /* drive supports use_iordy? */ |
| 783 | cycle_time = iop.eide_pio_iordy; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 784 | } else { |
Marek Vasut | b18eabf | 2011-08-20 09:15:13 +0000 | [diff] [blame] | 785 | cycle_time = iop.eide_pio; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 786 | } |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 787 | debug("cycle time = %d\n", cycle_time); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 788 | mode = 4; |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 789 | if (cycle_time > 120) |
| 790 | mode = 3; /* 120 ns for PIO mode 4 */ |
| 791 | if (cycle_time > 180) |
| 792 | mode = 2; /* 180 ns for PIO mode 3 */ |
| 793 | if (cycle_time > 240) |
| 794 | mode = 1; /* 240 ns for PIO mode 4 */ |
| 795 | if (cycle_time > 383) |
| 796 | mode = 0; /* 383 ns for PIO mode 4 */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 797 | } |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 798 | printf("PIO mode to use: PIO %d\n", mode); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 799 | #endif /* 0 */ |
| 800 | |
| 801 | #ifdef CONFIG_ATAPI |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 802 | if (dev_desc->if_type == IF_TYPE_ATAPI) { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 803 | atapi_inquiry(dev_desc); |
| 804 | return; |
| 805 | } |
| 806 | #endif /* CONFIG_ATAPI */ |
| 807 | |
wdenk | c3f9d49 | 2004-03-14 00:59:59 +0000 | [diff] [blame] | 808 | #ifdef __BIG_ENDIAN |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 809 | /* swap shorts */ |
Marek Vasut | b18eabf | 2011-08-20 09:15:13 +0000 | [diff] [blame] | 810 | dev_desc->lba = (iop.lba_capacity << 16) | (iop.lba_capacity >> 16); |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 811 | #else /* ! __BIG_ENDIAN */ |
wdenk | c3f9d49 | 2004-03-14 00:59:59 +0000 | [diff] [blame] | 812 | /* |
| 813 | * do not swap shorts on little endian |
| 814 | * |
| 815 | * See CF+ and CompactFlash Specification Revision 2.0: |
| 816 | * 6.2.1.6: Identfy Drive, Table 39, Word Address 57-58 for details. |
| 817 | */ |
Marek Vasut | b18eabf | 2011-08-20 09:15:13 +0000 | [diff] [blame] | 818 | dev_desc->lba = iop.lba_capacity; |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 819 | #endif /* __BIG_ENDIAN */ |
wdenk | c40b295 | 2004-03-13 23:29:43 +0000 | [diff] [blame] | 820 | |
wdenk | 42dfe7a | 2004-03-14 22:25:36 +0000 | [diff] [blame] | 821 | #ifdef CONFIG_LBA48 |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 822 | if (iop.command_set_2 & 0x0400) { /* LBA 48 support */ |
wdenk | 6e59238 | 2004-04-18 17:39:38 +0000 | [diff] [blame] | 823 | dev_desc->lba48 = 1; |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 824 | dev_desc->lba = (unsigned long long) iop.lba48_capacity[0] | |
| 825 | ((unsigned long long) iop.lba48_capacity[1] << 16) | |
| 826 | ((unsigned long long) iop.lba48_capacity[2] << 32) | |
| 827 | ((unsigned long long) iop.lba48_capacity[3] << 48); |
wdenk | c40b295 | 2004-03-13 23:29:43 +0000 | [diff] [blame] | 828 | } else { |
wdenk | c40b295 | 2004-03-13 23:29:43 +0000 | [diff] [blame] | 829 | dev_desc->lba48 = 0; |
| 830 | } |
| 831 | #endif /* CONFIG_LBA48 */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 832 | /* assuming HD */ |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 833 | dev_desc->type = DEV_TYPE_HARDDISK; |
| 834 | dev_desc->blksz = ATA_BLOCKSIZE; |
| 835 | dev_desc->lun = 0; /* just to fill something in... */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 836 | |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 837 | #if 0 /* only used to test the powersaving mode, |
| 838 | * if enabled, the drive goes after 5 sec |
| 839 | * in standby mode */ |
| 840 | ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device)); |
| 841 | c = ide_wait(device, IDE_TIME_OUT); |
| 842 | ide_outb(device, ATA_SECT_CNT, 1); |
| 843 | ide_outb(device, ATA_LBA_LOW, 0); |
| 844 | ide_outb(device, ATA_LBA_MID, 0); |
| 845 | ide_outb(device, ATA_LBA_HIGH, 0); |
| 846 | ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device)); |
| 847 | ide_outb(device, ATA_COMMAND, 0xe3); |
| 848 | udelay(50); |
| 849 | c = ide_wait(device, IDE_TIME_OUT); /* can't take over 500 ms */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 850 | #endif |
| 851 | } |
| 852 | |
| 853 | |
| 854 | /* ------------------------------------------------------------------------- */ |
| 855 | |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 856 | ulong ide_read(int device, lbaint_t blknr, ulong blkcnt, void *buffer) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 857 | { |
| 858 | ulong n = 0; |
| 859 | unsigned char c; |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 860 | unsigned char pwrsave = 0; /* power save */ |
| 861 | |
wdenk | 42dfe7a | 2004-03-14 22:25:36 +0000 | [diff] [blame] | 862 | #ifdef CONFIG_LBA48 |
wdenk | c40b295 | 2004-03-13 23:29:43 +0000 | [diff] [blame] | 863 | unsigned char lba48 = 0; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 864 | |
Guennadi Liakhovetski | 413bf58 | 2008-04-28 14:36:06 +0200 | [diff] [blame] | 865 | if (blknr & 0x0000fffff0000000ULL) { |
wdenk | c40b295 | 2004-03-13 23:29:43 +0000 | [diff] [blame] | 866 | /* more than 28 bits used, use 48bit mode */ |
| 867 | lba48 = 1; |
| 868 | } |
| 869 | #endif |
Marek Vasut | 5bbe10d | 2011-10-25 11:39:15 +0200 | [diff] [blame] | 870 | debug("ide_read dev %d start %lX, blocks %lX buffer at %lX\n", |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 871 | device, blknr, blkcnt, (ulong) buffer); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 872 | |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 873 | ide_led(DEVICE_LED(device), 1); /* LED on */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 874 | |
| 875 | /* Select device |
| 876 | */ |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 877 | ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device)); |
| 878 | c = ide_wait(device, IDE_TIME_OUT); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 879 | |
| 880 | if (c & ATA_STAT_BUSY) { |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 881 | printf("IDE read: device %d not ready\n", device); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 882 | goto IDE_READ_E; |
| 883 | } |
| 884 | |
| 885 | /* first check if the drive is in Powersaving mode, if yes, |
| 886 | * increase the timeout value */ |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 887 | ide_outb(device, ATA_COMMAND, ATA_CMD_CHK_PWR); |
| 888 | udelay(50); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 889 | |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 890 | c = ide_wait(device, IDE_TIME_OUT); /* can't take over 500 ms */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 891 | |
| 892 | if (c & ATA_STAT_BUSY) { |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 893 | printf("IDE read: device %d not ready\n", device); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 894 | goto IDE_READ_E; |
| 895 | } |
| 896 | if ((c & ATA_STAT_ERR) == ATA_STAT_ERR) { |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 897 | printf("No Powersaving mode %X\n", c); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 898 | } else { |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 899 | c = ide_inb(device, ATA_SECT_CNT); |
| 900 | debug("Powersaving %02X\n", c); |
| 901 | if (c == 0) |
| 902 | pwrsave = 1; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 903 | } |
| 904 | |
| 905 | |
| 906 | while (blkcnt-- > 0) { |
| 907 | |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 908 | c = ide_wait(device, IDE_TIME_OUT); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 909 | |
| 910 | if (c & ATA_STAT_BUSY) { |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 911 | printf("IDE read: device %d not ready\n", device); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 912 | break; |
| 913 | } |
wdenk | 42dfe7a | 2004-03-14 22:25:36 +0000 | [diff] [blame] | 914 | #ifdef CONFIG_LBA48 |
wdenk | c40b295 | 2004-03-13 23:29:43 +0000 | [diff] [blame] | 915 | if (lba48) { |
| 916 | /* write high bits */ |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 917 | ide_outb(device, ATA_SECT_CNT, 0); |
| 918 | ide_outb(device, ATA_LBA_LOW, (blknr >> 24) & 0xFF); |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 919 | #ifdef CONFIG_SYS_64BIT_LBA |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 920 | ide_outb(device, ATA_LBA_MID, (blknr >> 32) & 0xFF); |
| 921 | ide_outb(device, ATA_LBA_HIGH, (blknr >> 40) & 0xFF); |
Guennadi Liakhovetski | 413bf58 | 2008-04-28 14:36:06 +0200 | [diff] [blame] | 922 | #else |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 923 | ide_outb(device, ATA_LBA_MID, 0); |
| 924 | ide_outb(device, ATA_LBA_HIGH, 0); |
Guennadi Liakhovetski | 413bf58 | 2008-04-28 14:36:06 +0200 | [diff] [blame] | 925 | #endif |
wdenk | c40b295 | 2004-03-13 23:29:43 +0000 | [diff] [blame] | 926 | } |
| 927 | #endif |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 928 | ide_outb(device, ATA_SECT_CNT, 1); |
| 929 | ide_outb(device, ATA_LBA_LOW, (blknr >> 0) & 0xFF); |
| 930 | ide_outb(device, ATA_LBA_MID, (blknr >> 8) & 0xFF); |
| 931 | ide_outb(device, ATA_LBA_HIGH, (blknr >> 16) & 0xFF); |
wdenk | c40b295 | 2004-03-13 23:29:43 +0000 | [diff] [blame] | 932 | |
wdenk | 42dfe7a | 2004-03-14 22:25:36 +0000 | [diff] [blame] | 933 | #ifdef CONFIG_LBA48 |
wdenk | c40b295 | 2004-03-13 23:29:43 +0000 | [diff] [blame] | 934 | if (lba48) { |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 935 | ide_outb(device, ATA_DEV_HD, |
| 936 | ATA_LBA | ATA_DEVICE(device)); |
| 937 | ide_outb(device, ATA_COMMAND, ATA_CMD_READ_EXT); |
wdenk | c40b295 | 2004-03-13 23:29:43 +0000 | [diff] [blame] | 938 | |
| 939 | } else |
| 940 | #endif |
| 941 | { |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 942 | ide_outb(device, ATA_DEV_HD, ATA_LBA | |
| 943 | ATA_DEVICE(device) | ((blknr >> 24) & 0xF)); |
| 944 | ide_outb(device, ATA_COMMAND, ATA_CMD_READ); |
wdenk | c40b295 | 2004-03-13 23:29:43 +0000 | [diff] [blame] | 945 | } |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 946 | |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 947 | udelay(50); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 948 | |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 949 | if (pwrsave) { |
| 950 | /* may take up to 4 sec */ |
| 951 | c = ide_wait(device, IDE_SPIN_UP_TIME_OUT); |
| 952 | pwrsave = 0; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 953 | } else { |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 954 | /* can't take over 500 ms */ |
| 955 | c = ide_wait(device, IDE_TIME_OUT); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 956 | } |
| 957 | |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 958 | if ((c & (ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR)) != |
| 959 | ATA_STAT_DRQ) { |
Heiko Schocher | 4b142fe | 2009-12-03 11:21:21 +0100 | [diff] [blame] | 960 | #if defined(CONFIG_SYS_64BIT_LBA) |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 961 | printf("Error (no IRQ) dev %d blk %lld: status 0x%02x\n", |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 962 | device, blknr, c); |
wdenk | c40b295 | 2004-03-13 23:29:43 +0000 | [diff] [blame] | 963 | #else |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 964 | printf("Error (no IRQ) dev %d blk %ld: status 0x%02x\n", |
| 965 | device, (ulong) blknr, c); |
wdenk | c40b295 | 2004-03-13 23:29:43 +0000 | [diff] [blame] | 966 | #endif |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 967 | break; |
| 968 | } |
| 969 | |
Pavel Herrmann | f5b82c0 | 2012-10-09 07:04:39 +0000 | [diff] [blame] | 970 | ide_input_data(device, buffer, ATA_SECTORWORDS); |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 971 | (void) ide_inb(device, ATA_STATUS); /* clear IRQ */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 972 | |
| 973 | ++n; |
| 974 | ++blknr; |
Greg Lopp | 0b94504 | 2007-04-13 08:02:24 +0200 | [diff] [blame] | 975 | buffer += ATA_BLOCKSIZE; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 976 | } |
| 977 | IDE_READ_E: |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 978 | ide_led(DEVICE_LED(device), 0); /* LED off */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 979 | return (n); |
| 980 | } |
| 981 | |
| 982 | /* ------------------------------------------------------------------------- */ |
| 983 | |
| 984 | |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 985 | ulong ide_write(int device, lbaint_t blknr, ulong blkcnt, const void *buffer) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 986 | { |
| 987 | ulong n = 0; |
| 988 | unsigned char c; |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 989 | |
wdenk | 42dfe7a | 2004-03-14 22:25:36 +0000 | [diff] [blame] | 990 | #ifdef CONFIG_LBA48 |
wdenk | c40b295 | 2004-03-13 23:29:43 +0000 | [diff] [blame] | 991 | unsigned char lba48 = 0; |
| 992 | |
Guennadi Liakhovetski | 413bf58 | 2008-04-28 14:36:06 +0200 | [diff] [blame] | 993 | if (blknr & 0x0000fffff0000000ULL) { |
wdenk | c40b295 | 2004-03-13 23:29:43 +0000 | [diff] [blame] | 994 | /* more than 28 bits used, use 48bit mode */ |
| 995 | lba48 = 1; |
| 996 | } |
| 997 | #endif |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 998 | |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 999 | ide_led(DEVICE_LED(device), 1); /* LED on */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1000 | |
| 1001 | /* Select device |
| 1002 | */ |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1003 | ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device)); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1004 | |
| 1005 | while (blkcnt-- > 0) { |
| 1006 | |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1007 | c = ide_wait(device, IDE_TIME_OUT); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1008 | |
| 1009 | if (c & ATA_STAT_BUSY) { |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1010 | printf("IDE read: device %d not ready\n", device); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1011 | goto WR_OUT; |
| 1012 | } |
wdenk | 42dfe7a | 2004-03-14 22:25:36 +0000 | [diff] [blame] | 1013 | #ifdef CONFIG_LBA48 |
wdenk | c40b295 | 2004-03-13 23:29:43 +0000 | [diff] [blame] | 1014 | if (lba48) { |
| 1015 | /* write high bits */ |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1016 | ide_outb(device, ATA_SECT_CNT, 0); |
| 1017 | ide_outb(device, ATA_LBA_LOW, (blknr >> 24) & 0xFF); |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 1018 | #ifdef CONFIG_SYS_64BIT_LBA |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1019 | ide_outb(device, ATA_LBA_MID, (blknr >> 32) & 0xFF); |
| 1020 | ide_outb(device, ATA_LBA_HIGH, (blknr >> 40) & 0xFF); |
Guennadi Liakhovetski | 413bf58 | 2008-04-28 14:36:06 +0200 | [diff] [blame] | 1021 | #else |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1022 | ide_outb(device, ATA_LBA_MID, 0); |
| 1023 | ide_outb(device, ATA_LBA_HIGH, 0); |
Guennadi Liakhovetski | 413bf58 | 2008-04-28 14:36:06 +0200 | [diff] [blame] | 1024 | #endif |
wdenk | c40b295 | 2004-03-13 23:29:43 +0000 | [diff] [blame] | 1025 | } |
| 1026 | #endif |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1027 | ide_outb(device, ATA_SECT_CNT, 1); |
| 1028 | ide_outb(device, ATA_LBA_LOW, (blknr >> 0) & 0xFF); |
| 1029 | ide_outb(device, ATA_LBA_MID, (blknr >> 8) & 0xFF); |
| 1030 | ide_outb(device, ATA_LBA_HIGH, (blknr >> 16) & 0xFF); |
wdenk | c40b295 | 2004-03-13 23:29:43 +0000 | [diff] [blame] | 1031 | |
wdenk | 42dfe7a | 2004-03-14 22:25:36 +0000 | [diff] [blame] | 1032 | #ifdef CONFIG_LBA48 |
wdenk | c40b295 | 2004-03-13 23:29:43 +0000 | [diff] [blame] | 1033 | if (lba48) { |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1034 | ide_outb(device, ATA_DEV_HD, |
| 1035 | ATA_LBA | ATA_DEVICE(device)); |
| 1036 | ide_outb(device, ATA_COMMAND, ATA_CMD_WRITE_EXT); |
wdenk | c40b295 | 2004-03-13 23:29:43 +0000 | [diff] [blame] | 1037 | |
| 1038 | } else |
| 1039 | #endif |
| 1040 | { |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1041 | ide_outb(device, ATA_DEV_HD, ATA_LBA | |
| 1042 | ATA_DEVICE(device) | ((blknr >> 24) & 0xF)); |
| 1043 | ide_outb(device, ATA_COMMAND, ATA_CMD_WRITE); |
wdenk | c40b295 | 2004-03-13 23:29:43 +0000 | [diff] [blame] | 1044 | } |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1045 | |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1046 | udelay(50); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1047 | |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1048 | /* can't take over 500 ms */ |
| 1049 | c = ide_wait(device, IDE_TIME_OUT); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1050 | |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1051 | if ((c & (ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR)) != |
| 1052 | ATA_STAT_DRQ) { |
Heiko Schocher | 4b142fe | 2009-12-03 11:21:21 +0100 | [diff] [blame] | 1053 | #if defined(CONFIG_SYS_64BIT_LBA) |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1054 | printf("Error (no IRQ) dev %d blk %lld: status 0x%02x\n", |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1055 | device, blknr, c); |
wdenk | c40b295 | 2004-03-13 23:29:43 +0000 | [diff] [blame] | 1056 | #else |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1057 | printf("Error (no IRQ) dev %d blk %ld: status 0x%02x\n", |
| 1058 | device, (ulong) blknr, c); |
wdenk | c40b295 | 2004-03-13 23:29:43 +0000 | [diff] [blame] | 1059 | #endif |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1060 | goto WR_OUT; |
| 1061 | } |
| 1062 | |
Pavel Herrmann | f5b82c0 | 2012-10-09 07:04:39 +0000 | [diff] [blame] | 1063 | ide_output_data(device, buffer, ATA_SECTORWORDS); |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1064 | c = ide_inb(device, ATA_STATUS); /* clear IRQ */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1065 | ++n; |
| 1066 | ++blknr; |
Greg Lopp | 0b94504 | 2007-04-13 08:02:24 +0200 | [diff] [blame] | 1067 | buffer += ATA_BLOCKSIZE; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1068 | } |
| 1069 | WR_OUT: |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1070 | ide_led(DEVICE_LED(device), 0); /* LED off */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1071 | return (n); |
| 1072 | } |
| 1073 | |
| 1074 | /* ------------------------------------------------------------------------- */ |
| 1075 | |
| 1076 | /* |
| 1077 | * copy src to dest, skipping leading and trailing blanks and null |
| 1078 | * terminate the string |
wdenk | 7d7ce41 | 2004-03-17 01:13:07 +0000 | [diff] [blame] | 1079 | * "len" is the size of available memory including the terminating '\0' |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1080 | */ |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1081 | static void ident_cpy(unsigned char *dst, unsigned char *src, |
| 1082 | unsigned int len) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1083 | { |
wdenk | 7d7ce41 | 2004-03-17 01:13:07 +0000 | [diff] [blame] | 1084 | unsigned char *end, *last; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1085 | |
wdenk | 7d7ce41 | 2004-03-17 01:13:07 +0000 | [diff] [blame] | 1086 | last = dst; |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1087 | end = src + len - 1; |
wdenk | 7d7ce41 | 2004-03-17 01:13:07 +0000 | [diff] [blame] | 1088 | |
| 1089 | /* reserve space for '\0' */ |
| 1090 | if (len < 2) |
| 1091 | goto OUT; |
wdenk | efa329c | 2004-03-23 20:18:25 +0000 | [diff] [blame] | 1092 | |
wdenk | 7d7ce41 | 2004-03-17 01:13:07 +0000 | [diff] [blame] | 1093 | /* skip leading white space */ |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1094 | while ((*src) && (src < end) && (*src == ' ')) |
wdenk | 7d7ce41 | 2004-03-17 01:13:07 +0000 | [diff] [blame] | 1095 | ++src; |
| 1096 | |
| 1097 | /* copy string, omitting trailing white space */ |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1098 | while ((*src) && (src < end)) { |
wdenk | 7d7ce41 | 2004-03-17 01:13:07 +0000 | [diff] [blame] | 1099 | *dst++ = *src; |
| 1100 | if (*src++ != ' ') |
| 1101 | last = dst; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1102 | } |
wdenk | 7d7ce41 | 2004-03-17 01:13:07 +0000 | [diff] [blame] | 1103 | OUT: |
| 1104 | *last = '\0'; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1105 | } |
| 1106 | |
| 1107 | /* ------------------------------------------------------------------------- */ |
| 1108 | |
| 1109 | /* |
| 1110 | * Wait until Busy bit is off, or timeout (in ms) |
| 1111 | * Return last status |
| 1112 | */ |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1113 | static uchar ide_wait(int dev, ulong t) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1114 | { |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1115 | ulong delay = 10 * t; /* poll every 100 us */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1116 | uchar c; |
| 1117 | |
wdenk | 2262cfe | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 1118 | while ((c = ide_inb(dev, ATA_STATUS)) & ATA_STAT_BUSY) { |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1119 | udelay(100); |
| 1120 | if (delay-- == 0) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1121 | break; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1122 | } |
| 1123 | return (c); |
| 1124 | } |
| 1125 | |
| 1126 | /* ------------------------------------------------------------------------- */ |
| 1127 | |
| 1128 | #ifdef CONFIG_IDE_RESET |
| 1129 | extern void ide_set_reset(int idereset); |
| 1130 | |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1131 | static void ide_reset(void) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1132 | { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1133 | int i; |
| 1134 | |
| 1135 | curr_device = -1; |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1136 | for (i = 0; i < CONFIG_SYS_IDE_MAXBUS; ++i) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1137 | ide_bus_ok[i] = 0; |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1138 | for (i = 0; i < CONFIG_SYS_IDE_MAXDEVICE; ++i) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1139 | ide_dev_desc[i].type = DEV_TYPE_UNKNOWN; |
| 1140 | |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1141 | ide_set_reset(1); /* assert reset */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1142 | |
Martin Krause | e175eac | 2008-04-03 13:37:56 +0200 | [diff] [blame] | 1143 | /* the reset signal shall be asserted for et least 25 us */ |
| 1144 | udelay(25); |
| 1145 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1146 | WATCHDOG_RESET(); |
| 1147 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1148 | /* de-assert RESET signal */ |
| 1149 | ide_set_reset(0); |
| 1150 | |
| 1151 | /* wait 250 ms */ |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1152 | for (i = 0; i < 250; ++i) |
| 1153 | udelay(1000); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1154 | } |
| 1155 | |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1156 | #endif /* CONFIG_IDE_RESET */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1157 | |
| 1158 | /* ------------------------------------------------------------------------- */ |
| 1159 | |
Heiko Schocher | 3887c3f | 2009-09-23 07:56:08 +0200 | [diff] [blame] | 1160 | #if defined(CONFIG_OF_IDE_FIXUP) |
| 1161 | int ide_device_present(int dev) |
| 1162 | { |
| 1163 | if (dev >= CONFIG_SYS_IDE_MAXBUS) |
| 1164 | return 0; |
| 1165 | return (ide_dev_desc[dev].type == DEV_TYPE_UNKNOWN ? 0 : 1); |
| 1166 | } |
| 1167 | #endif |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1168 | /* ------------------------------------------------------------------------- */ |
| 1169 | |
| 1170 | #ifdef CONFIG_ATAPI |
| 1171 | /**************************************************************************** |
| 1172 | * ATAPI Support |
| 1173 | */ |
| 1174 | |
Pavel Herrmann | f5b82c0 | 2012-10-09 07:04:39 +0000 | [diff] [blame] | 1175 | void ide_input_data_shorts(int dev, ushort *sect_buf, int shorts) |
| 1176 | __attribute__ ((weak, alias("__ide_input_data_shorts"))); |
| 1177 | |
| 1178 | void ide_output_data_shorts(int dev, ushort *sect_buf, int shorts) |
| 1179 | __attribute__ ((weak, alias("__ide_output_data_shorts"))); |
| 1180 | |
| 1181 | |
Albert Aribaud | f2a37fc | 2010-08-08 05:17:05 +0530 | [diff] [blame] | 1182 | #if defined(CONFIG_IDE_SWAP_IO) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1183 | /* since ATAPI may use commands with not 4 bytes alligned length |
| 1184 | * we have our own transfer functions, 2 bytes alligned */ |
Pavel Herrmann | f5b82c0 | 2012-10-09 07:04:39 +0000 | [diff] [blame] | 1185 | void __ide_output_data_shorts(int dev, ushort *sect_buf, int shorts) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1186 | { |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1187 | ushort *dbuf; |
| 1188 | volatile ushort *pbuf; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1189 | |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1190 | pbuf = (ushort *) (ATA_CURR_BASE(dev) + ATA_DATA_REG); |
| 1191 | dbuf = (ushort *) sect_buf; |
wdenk | db01a2e | 2004-04-15 23:14:49 +0000 | [diff] [blame] | 1192 | |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1193 | debug("in output data shorts base for read is %lx\n", |
| 1194 | (unsigned long) pbuf); |
wdenk | db01a2e | 2004-04-15 23:14:49 +0000 | [diff] [blame] | 1195 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1196 | while (shorts--) { |
wdenk | 5cf91d6 | 2004-04-23 20:32:05 +0000 | [diff] [blame] | 1197 | EIEIO; |
wdenk | 1a344f2 | 2005-02-03 23:00:49 +0000 | [diff] [blame] | 1198 | *pbuf = *dbuf++; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1199 | } |
wdenk | 1a344f2 | 2005-02-03 23:00:49 +0000 | [diff] [blame] | 1200 | } |
| 1201 | |
Pavel Herrmann | f5b82c0 | 2012-10-09 07:04:39 +0000 | [diff] [blame] | 1202 | void __ide_input_data_shorts(int dev, ushort *sect_buf, int shorts) |
wdenk | 1a344f2 | 2005-02-03 23:00:49 +0000 | [diff] [blame] | 1203 | { |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1204 | ushort *dbuf; |
| 1205 | volatile ushort *pbuf; |
wdenk | 1a344f2 | 2005-02-03 23:00:49 +0000 | [diff] [blame] | 1206 | |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1207 | pbuf = (ushort *) (ATA_CURR_BASE(dev) + ATA_DATA_REG); |
| 1208 | dbuf = (ushort *) sect_buf; |
wdenk | 1a344f2 | 2005-02-03 23:00:49 +0000 | [diff] [blame] | 1209 | |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1210 | debug("in input data shorts base for read is %lx\n", |
| 1211 | (unsigned long) pbuf); |
wdenk | 1a344f2 | 2005-02-03 23:00:49 +0000 | [diff] [blame] | 1212 | |
| 1213 | while (shorts--) { |
| 1214 | EIEIO; |
| 1215 | *dbuf++ = *pbuf; |
| 1216 | } |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1217 | } |
| 1218 | |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1219 | #else /* ! CONFIG_IDE_SWAP_IO */ |
Pavel Herrmann | f5b82c0 | 2012-10-09 07:04:39 +0000 | [diff] [blame] | 1220 | void __ide_output_data_shorts(int dev, ushort *sect_buf, int shorts) |
wdenk | 2262cfe | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 1221 | { |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1222 | outsw(ATA_CURR_BASE(dev) + ATA_DATA_REG, sect_buf, shorts); |
wdenk | 2262cfe | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 1223 | } |
| 1224 | |
Pavel Herrmann | f5b82c0 | 2012-10-09 07:04:39 +0000 | [diff] [blame] | 1225 | void __ide_input_data_shorts(int dev, ushort *sect_buf, int shorts) |
wdenk | 2262cfe | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 1226 | { |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1227 | insw(ATA_CURR_BASE(dev) + ATA_DATA_REG, sect_buf, shorts); |
wdenk | 2262cfe | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 1228 | } |
| 1229 | |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1230 | #endif /* CONFIG_IDE_SWAP_IO */ |
wdenk | 2262cfe | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 1231 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1232 | /* |
| 1233 | * Wait until (Status & mask) == res, or timeout (in ms) |
| 1234 | * Return last status |
| 1235 | * This is used since some ATAPI CD ROMs clears their Busy Bit first |
| 1236 | * and then they set their DRQ Bit |
| 1237 | */ |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1238 | static uchar atapi_wait_mask(int dev, ulong t, uchar mask, uchar res) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1239 | { |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1240 | ulong delay = 10 * t; /* poll every 100 us */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1241 | uchar c; |
| 1242 | |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1243 | /* prevents to read the status before valid */ |
| 1244 | c = ide_inb(dev, ATA_DEV_CTL); |
| 1245 | |
wdenk | 2262cfe | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 1246 | while (((c = ide_inb(dev, ATA_STATUS)) & mask) != res) { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1247 | /* break if error occurs (doesn't make sense to wait more) */ |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1248 | if ((c & ATA_STAT_ERR) == ATA_STAT_ERR) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1249 | break; |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1250 | udelay(100); |
| 1251 | if (delay-- == 0) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1252 | break; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1253 | } |
| 1254 | return (c); |
| 1255 | } |
| 1256 | |
| 1257 | /* |
| 1258 | * issue an atapi command |
| 1259 | */ |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1260 | unsigned char atapi_issue(int device, unsigned char *ccb, int ccblen, |
| 1261 | unsigned char *buffer, int buflen) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1262 | { |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1263 | unsigned char c, err, mask, res; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1264 | int n; |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1265 | |
| 1266 | ide_led(DEVICE_LED(device), 1); /* LED on */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1267 | |
| 1268 | /* Select device |
| 1269 | */ |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1270 | mask = ATA_STAT_BUSY | ATA_STAT_DRQ; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1271 | res = 0; |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1272 | ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device)); |
| 1273 | c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1274 | if ((c & mask) != res) { |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1275 | printf("ATAPI_ISSUE: device %d not ready status %X\n", device, |
| 1276 | c); |
| 1277 | err = 0xFF; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1278 | goto AI_OUT; |
| 1279 | } |
| 1280 | /* write taskfile */ |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1281 | ide_outb(device, ATA_ERROR_REG, 0); /* no DMA, no overlaped */ |
| 1282 | ide_outb(device, ATA_SECT_CNT, 0); |
| 1283 | ide_outb(device, ATA_SECT_NUM, 0); |
| 1284 | ide_outb(device, ATA_CYL_LOW, (unsigned char) (buflen & 0xFF)); |
| 1285 | ide_outb(device, ATA_CYL_HIGH, |
| 1286 | (unsigned char) ((buflen >> 8) & 0xFF)); |
| 1287 | ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device)); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1288 | |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1289 | ide_outb(device, ATA_COMMAND, ATAPI_CMD_PACKET); |
| 1290 | udelay(50); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1291 | |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1292 | mask = ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1293 | res = ATA_STAT_DRQ; |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1294 | c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1295 | |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1296 | if ((c & mask) != res) { /* DRQ must be 1, BSY 0 */ |
| 1297 | printf("ATAPI_ISSUE: Error (no IRQ) before sending ccb dev %d status 0x%02x\n", |
| 1298 | device, c); |
| 1299 | err = 0xFF; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1300 | goto AI_OUT; |
| 1301 | } |
| 1302 | |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1303 | /* write command block */ |
Pavel Herrmann | f5b82c0 | 2012-10-09 07:04:39 +0000 | [diff] [blame] | 1304 | ide_output_data_shorts(device, (unsigned short *) ccb, ccblen / 2); |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1305 | |
Wolfgang Denk | 53677ef | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 1306 | /* ATAPI Command written wait for completition */ |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1307 | udelay(5000); /* device must set bsy */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1308 | |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1309 | mask = ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR; |
| 1310 | /* |
| 1311 | * if no data wait for DRQ = 0 BSY = 0 |
| 1312 | * if data wait for DRQ = 1 BSY = 0 |
| 1313 | */ |
| 1314 | res = 0; |
| 1315 | if (buflen) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1316 | res = ATA_STAT_DRQ; |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1317 | c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res); |
| 1318 | if ((c & mask) != res) { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1319 | if (c & ATA_STAT_ERR) { |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1320 | err = (ide_inb(device, ATA_ERROR_REG)) >> 4; |
| 1321 | debug("atapi_issue 1 returned sense key %X status %02X\n", |
| 1322 | err, c); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1323 | } else { |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1324 | printf("ATAPI_ISSUE: (no DRQ) after sending ccb (%x) status 0x%02x\n", |
| 1325 | ccb[0], c); |
| 1326 | err = 0xFF; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1327 | } |
| 1328 | goto AI_OUT; |
| 1329 | } |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1330 | n = ide_inb(device, ATA_CYL_HIGH); |
| 1331 | n <<= 8; |
| 1332 | n += ide_inb(device, ATA_CYL_LOW); |
| 1333 | if (n > buflen) { |
| 1334 | printf("ERROR, transfer bytes %d requested only %d\n", n, |
| 1335 | buflen); |
| 1336 | err = 0xff; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1337 | goto AI_OUT; |
| 1338 | } |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1339 | if ((n == 0) && (buflen < 0)) { |
| 1340 | printf("ERROR, transfer bytes %d requested %d\n", n, buflen); |
| 1341 | err = 0xff; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1342 | goto AI_OUT; |
| 1343 | } |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1344 | if (n != buflen) { |
| 1345 | debug("WARNING, transfer bytes %d not equal with requested %d\n", |
| 1346 | n, buflen); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1347 | } |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1348 | if (n != 0) { /* data transfer */ |
| 1349 | debug("ATAPI_ISSUE: %d Bytes to transfer\n", n); |
| 1350 | /* we transfer shorts */ |
| 1351 | n >>= 1; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1352 | /* ok now decide if it is an in or output */ |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1353 | if ((ide_inb(device, ATA_SECT_CNT) & 0x02) == 0) { |
| 1354 | debug("Write to device\n"); |
Pavel Herrmann | f5b82c0 | 2012-10-09 07:04:39 +0000 | [diff] [blame] | 1355 | ide_output_data_shorts(device, |
| 1356 | (unsigned short *) buffer, n); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1357 | } else { |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1358 | debug("Read from device @ %p shorts %d\n", buffer, n); |
Pavel Herrmann | f5b82c0 | 2012-10-09 07:04:39 +0000 | [diff] [blame] | 1359 | ide_input_data_shorts(device, |
| 1360 | (unsigned short *) buffer, n); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1361 | } |
| 1362 | } |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1363 | udelay(5000); /* seems that some CD ROMs need this... */ |
| 1364 | mask = ATA_STAT_BUSY | ATA_STAT_ERR; |
| 1365 | res = 0; |
| 1366 | c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1367 | if ((c & ATA_STAT_ERR) == ATA_STAT_ERR) { |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1368 | err = (ide_inb(device, ATA_ERROR_REG) >> 4); |
| 1369 | debug("atapi_issue 2 returned sense key %X status %X\n", err, |
| 1370 | c); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1371 | } else { |
| 1372 | err = 0; |
| 1373 | } |
| 1374 | AI_OUT: |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1375 | ide_led(DEVICE_LED(device), 0); /* LED off */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1376 | return (err); |
| 1377 | } |
| 1378 | |
| 1379 | /* |
| 1380 | * sending the command to atapi_issue. If an status other than good |
| 1381 | * returns, an request_sense will be issued |
| 1382 | */ |
| 1383 | |
Wolfgang Denk | 53677ef | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 1384 | #define ATAPI_DRIVE_NOT_READY 100 |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1385 | #define ATAPI_UNIT_ATTN 10 |
| 1386 | |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1387 | unsigned char atapi_issue_autoreq(int device, |
| 1388 | unsigned char *ccb, |
| 1389 | int ccblen, |
| 1390 | unsigned char *buffer, int buflen) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1391 | { |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1392 | unsigned char sense_data[18], sense_ccb[12]; |
| 1393 | unsigned char res, key, asc, ascq; |
| 1394 | int notready, unitattn; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1395 | |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1396 | unitattn = ATAPI_UNIT_ATTN; |
| 1397 | notready = ATAPI_DRIVE_NOT_READY; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1398 | |
| 1399 | retry: |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1400 | res = atapi_issue(device, ccb, ccblen, buffer, buflen); |
| 1401 | if (res == 0) |
| 1402 | return 0; /* Ok */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1403 | |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1404 | if (res == 0xFF) |
| 1405 | return 0xFF; /* error */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1406 | |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1407 | debug("(auto_req)atapi_issue returned sense key %X\n", res); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1408 | |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1409 | memset(sense_ccb, 0, sizeof(sense_ccb)); |
| 1410 | memset(sense_data, 0, sizeof(sense_data)); |
| 1411 | sense_ccb[0] = ATAPI_CMD_REQ_SENSE; |
| 1412 | sense_ccb[4] = 18; /* allocation Length */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1413 | |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1414 | res = atapi_issue(device, sense_ccb, 12, sense_data, 18); |
| 1415 | key = (sense_data[2] & 0xF); |
| 1416 | asc = (sense_data[12]); |
| 1417 | ascq = (sense_data[13]); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1418 | |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1419 | debug("ATAPI_CMD_REQ_SENSE returned %x\n", res); |
| 1420 | debug(" Sense page: %02X key %02X ASC %02X ASCQ %02X\n", |
| 1421 | sense_data[0], key, asc, ascq); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1422 | |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1423 | if ((key == 0)) |
| 1424 | return 0; /* ok device ready */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1425 | |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1426 | if ((key == 6) || (asc == 0x29) || (asc == 0x28)) { /* Unit Attention */ |
| 1427 | if (unitattn-- > 0) { |
| 1428 | udelay(200 * 1000); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1429 | goto retry; |
| 1430 | } |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1431 | printf("Unit Attention, tried %d\n", ATAPI_UNIT_ATTN); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1432 | goto error; |
| 1433 | } |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1434 | if ((asc == 0x4) && (ascq == 0x1)) { |
| 1435 | /* not ready, but will be ready soon */ |
| 1436 | if (notready-- > 0) { |
| 1437 | udelay(200 * 1000); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1438 | goto retry; |
| 1439 | } |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1440 | printf("Drive not ready, tried %d times\n", |
| 1441 | ATAPI_DRIVE_NOT_READY); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1442 | goto error; |
| 1443 | } |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1444 | if (asc == 0x3a) { |
| 1445 | debug("Media not present\n"); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1446 | goto error; |
| 1447 | } |
wdenk | c7de829 | 2002-11-19 11:04:11 +0000 | [diff] [blame] | 1448 | |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1449 | printf("ERROR: Unknown Sense key %02X ASC %02X ASCQ %02X\n", key, asc, |
| 1450 | ascq); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1451 | error: |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1452 | debug("ERROR Sense key %02X ASC %02X ASCQ %02X\n", key, asc, ascq); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1453 | return (0xFF); |
| 1454 | } |
| 1455 | |
| 1456 | |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1457 | static void atapi_inquiry(block_dev_desc_t *dev_desc) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1458 | { |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1459 | unsigned char ccb[12]; /* Command descriptor block */ |
| 1460 | unsigned char iobuf[64]; /* temp buf */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1461 | unsigned char c; |
| 1462 | int device; |
| 1463 | |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1464 | device = dev_desc->dev; |
| 1465 | dev_desc->type = DEV_TYPE_UNKNOWN; /* not yet valid */ |
| 1466 | dev_desc->block_read = atapi_read; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1467 | |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1468 | memset(ccb, 0, sizeof(ccb)); |
| 1469 | memset(iobuf, 0, sizeof(iobuf)); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1470 | |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1471 | ccb[0] = ATAPI_CMD_INQUIRY; |
| 1472 | ccb[4] = 40; /* allocation Legnth */ |
| 1473 | c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *) iobuf, 40); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1474 | |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1475 | debug("ATAPI_CMD_INQUIRY returned %x\n", c); |
| 1476 | if (c != 0) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1477 | return; |
| 1478 | |
| 1479 | /* copy device ident strings */ |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1480 | ident_cpy((unsigned char *) dev_desc->vendor, &iobuf[8], 8); |
| 1481 | ident_cpy((unsigned char *) dev_desc->product, &iobuf[16], 16); |
| 1482 | ident_cpy((unsigned char *) dev_desc->revision, &iobuf[32], 5); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1483 | |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1484 | dev_desc->lun = 0; |
| 1485 | dev_desc->lba = 0; |
| 1486 | dev_desc->blksz = 0; |
| 1487 | dev_desc->type = iobuf[0] & 0x1f; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1488 | |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1489 | if ((iobuf[1] & 0x80) == 0x80) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1490 | dev_desc->removable = 1; |
| 1491 | else |
| 1492 | dev_desc->removable = 0; |
| 1493 | |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1494 | memset(ccb, 0, sizeof(ccb)); |
| 1495 | memset(iobuf, 0, sizeof(iobuf)); |
| 1496 | ccb[0] = ATAPI_CMD_START_STOP; |
| 1497 | ccb[4] = 0x03; /* start */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1498 | |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1499 | c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *) iobuf, 0); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1500 | |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1501 | debug("ATAPI_CMD_START_STOP returned %x\n", c); |
| 1502 | if (c != 0) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1503 | return; |
| 1504 | |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1505 | memset(ccb, 0, sizeof(ccb)); |
| 1506 | memset(iobuf, 0, sizeof(iobuf)); |
| 1507 | c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *) iobuf, 0); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1508 | |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1509 | debug("ATAPI_CMD_UNIT_TEST_READY returned %x\n", c); |
| 1510 | if (c != 0) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1511 | return; |
| 1512 | |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1513 | memset(ccb, 0, sizeof(ccb)); |
| 1514 | memset(iobuf, 0, sizeof(iobuf)); |
| 1515 | ccb[0] = ATAPI_CMD_READ_CAP; |
| 1516 | c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *) iobuf, 8); |
| 1517 | debug("ATAPI_CMD_READ_CAP returned %x\n", c); |
| 1518 | if (c != 0) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1519 | return; |
| 1520 | |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1521 | debug("Read Cap: LBA %02X%02X%02X%02X blksize %02X%02X%02X%02X\n", |
| 1522 | iobuf[0], iobuf[1], iobuf[2], iobuf[3], |
| 1523 | iobuf[4], iobuf[5], iobuf[6], iobuf[7]); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1524 | |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1525 | dev_desc->lba = ((unsigned long) iobuf[0] << 24) + |
| 1526 | ((unsigned long) iobuf[1] << 16) + |
| 1527 | ((unsigned long) iobuf[2] << 8) + ((unsigned long) iobuf[3]); |
| 1528 | dev_desc->blksz = ((unsigned long) iobuf[4] << 24) + |
| 1529 | ((unsigned long) iobuf[5] << 16) + |
| 1530 | ((unsigned long) iobuf[6] << 8) + ((unsigned long) iobuf[7]); |
wdenk | 42dfe7a | 2004-03-14 22:25:36 +0000 | [diff] [blame] | 1531 | #ifdef CONFIG_LBA48 |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1532 | /* ATAPI devices cannot use 48bit addressing (ATA/ATAPI v7) */ |
| 1533 | dev_desc->lba48 = 0; |
wdenk | 42dfe7a | 2004-03-14 22:25:36 +0000 | [diff] [blame] | 1534 | #endif |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1535 | return; |
| 1536 | } |
| 1537 | |
| 1538 | |
| 1539 | /* |
| 1540 | * atapi_read: |
| 1541 | * we transfer only one block per command, since the multiple DRQ per |
| 1542 | * command is not yet implemented |
| 1543 | */ |
| 1544 | #define ATAPI_READ_MAX_BYTES 2048 /* we read max 2kbytes */ |
| 1545 | #define ATAPI_READ_BLOCK_SIZE 2048 /* assuming CD part */ |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1546 | #define ATAPI_READ_MAX_BLOCK (ATAPI_READ_MAX_BYTES/ATAPI_READ_BLOCK_SIZE) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1547 | |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1548 | ulong atapi_read(int device, lbaint_t blknr, ulong blkcnt, void *buffer) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1549 | { |
| 1550 | ulong n = 0; |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1551 | unsigned char ccb[12]; /* Command descriptor block */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1552 | ulong cnt; |
| 1553 | |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1554 | debug("atapi_read dev %d start %lX, blocks %lX buffer at %lX\n", |
| 1555 | device, blknr, blkcnt, (ulong) buffer); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1556 | |
| 1557 | do { |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1558 | if (blkcnt > ATAPI_READ_MAX_BLOCK) |
| 1559 | cnt = ATAPI_READ_MAX_BLOCK; |
| 1560 | else |
| 1561 | cnt = blkcnt; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1562 | |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1563 | ccb[0] = ATAPI_CMD_READ_12; |
| 1564 | ccb[1] = 0; /* reserved */ |
| 1565 | ccb[2] = (unsigned char) (blknr >> 24) & 0xFF; /* MSB Block */ |
| 1566 | ccb[3] = (unsigned char) (blknr >> 16) & 0xFF; /* */ |
| 1567 | ccb[4] = (unsigned char) (blknr >> 8) & 0xFF; |
| 1568 | ccb[5] = (unsigned char) blknr & 0xFF; /* LSB Block */ |
| 1569 | ccb[6] = (unsigned char) (cnt >> 24) & 0xFF; /* MSB Block cnt */ |
| 1570 | ccb[7] = (unsigned char) (cnt >> 16) & 0xFF; |
| 1571 | ccb[8] = (unsigned char) (cnt >> 8) & 0xFF; |
| 1572 | ccb[9] = (unsigned char) cnt & 0xFF; /* LSB Block */ |
| 1573 | ccb[10] = 0; /* reserved */ |
| 1574 | ccb[11] = 0; /* reserved */ |
| 1575 | |
| 1576 | if (atapi_issue_autoreq(device, ccb, 12, |
| 1577 | (unsigned char *) buffer, |
| 1578 | cnt * ATAPI_READ_BLOCK_SIZE) |
| 1579 | == 0xFF) { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1580 | return (n); |
| 1581 | } |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1582 | n += cnt; |
| 1583 | blkcnt -= cnt; |
| 1584 | blknr += cnt; |
| 1585 | buffer += (cnt * ATAPI_READ_BLOCK_SIZE); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1586 | } while (blkcnt > 0); |
| 1587 | return (n); |
| 1588 | } |
| 1589 | |
| 1590 | /* ------------------------------------------------------------------------- */ |
| 1591 | |
| 1592 | #endif /* CONFIG_ATAPI */ |
| 1593 | |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1594 | U_BOOT_CMD(ide, 5, 1, do_ide, |
| 1595 | "IDE sub-system", |
| 1596 | "reset - reset IDE controller\n" |
| 1597 | "ide info - show available IDE devices\n" |
| 1598 | "ide device [dev] - show or set current device\n" |
| 1599 | "ide part [dev] - print partition table of one or all IDE devices\n" |
| 1600 | "ide read addr blk# cnt\n" |
| 1601 | "ide write addr blk# cnt - read/write `cnt'" |
| 1602 | " blocks starting at block `blk#'\n" |
| 1603 | " to/from memory address `addr'"); |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 1604 | |
Wolfgang Denk | 34c202c | 2011-10-29 09:41:40 +0000 | [diff] [blame] | 1605 | U_BOOT_CMD(diskboot, 3, 1, do_diskboot, |
| 1606 | "boot from IDE device", "loadAddr dev:part"); |