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