Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | e11f0ef | 2016-05-01 11:36:10 -0600 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2000-2011 |
| 4 | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. |
Simon Glass | e11f0ef | 2016-05-01 11:36:10 -0600 | [diff] [blame] | 5 | */ |
| 6 | |
Patrick Delaunay | 8131335 | 2021-04-27 11:02:19 +0200 | [diff] [blame] | 7 | #define LOG_CATEGORY UCLASS_IDE |
| 8 | |
Simon Glass | e11f0ef | 2016-05-01 11:36:10 -0600 | [diff] [blame] | 9 | #include <common.h> |
| 10 | #include <ata.h> |
Simon Glass | 655306c | 2020-05-10 11:39:58 -0600 | [diff] [blame] | 11 | #include <blk.h> |
Simon Glass | f94bea0 | 2016-05-01 11:36:22 -0600 | [diff] [blame] | 12 | #include <dm.h> |
Simon Glass | e11f0ef | 2016-05-01 11:36:10 -0600 | [diff] [blame] | 13 | #include <ide.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 14 | #include <log.h> |
Simon Glass | 655306c | 2020-05-10 11:39:58 -0600 | [diff] [blame] | 15 | #include <part.h> |
Simon Glass | e11f0ef | 2016-05-01 11:36:10 -0600 | [diff] [blame] | 16 | #include <watchdog.h> |
| 17 | #include <asm/io.h> |
Simon Glass | dbd7954 | 2020-05-10 11:40:11 -0600 | [diff] [blame] | 18 | #include <linux/delay.h> |
Simon Glass | e11f0ef | 2016-05-01 11:36:10 -0600 | [diff] [blame] | 19 | |
| 20 | #ifdef __PPC__ |
| 21 | # define EIEIO __asm__ volatile ("eieio") |
| 22 | # define SYNC __asm__ volatile ("sync") |
| 23 | #else |
| 24 | # define EIEIO /* nothing */ |
| 25 | # define SYNC /* nothing */ |
| 26 | #endif |
| 27 | |
| 28 | /* Current offset for IDE0 / IDE1 bus access */ |
| 29 | ulong ide_bus_offset[CONFIG_SYS_IDE_MAXBUS] = { |
| 30 | #if defined(CONFIG_SYS_ATA_IDE0_OFFSET) |
| 31 | CONFIG_SYS_ATA_IDE0_OFFSET, |
| 32 | #endif |
| 33 | #if defined(CONFIG_SYS_ATA_IDE1_OFFSET) && (CONFIG_SYS_IDE_MAXBUS > 1) |
| 34 | CONFIG_SYS_ATA_IDE1_OFFSET, |
| 35 | #endif |
| 36 | }; |
| 37 | |
| 38 | static int ide_bus_ok[CONFIG_SYS_IDE_MAXBUS]; |
| 39 | |
| 40 | struct blk_desc ide_dev_desc[CONFIG_SYS_IDE_MAXDEVICE]; |
| 41 | |
| 42 | #define IDE_TIME_OUT 2000 /* 2 sec timeout */ |
| 43 | |
| 44 | #define ATAPI_TIME_OUT 7000 /* 7 sec timeout (5 sec seems to work...) */ |
| 45 | |
| 46 | #define IDE_SPIN_UP_TIME_OUT 5000 /* 5 sec spin-up timeout */ |
| 47 | |
Simon Glass | e11f0ef | 2016-05-01 11:36:10 -0600 | [diff] [blame] | 48 | #ifdef CONFIG_IDE_RESET |
| 49 | extern void ide_set_reset(int idereset); |
| 50 | |
| 51 | static void ide_reset(void) |
| 52 | { |
| 53 | int i; |
| 54 | |
| 55 | for (i = 0; i < CONFIG_SYS_IDE_MAXBUS; ++i) |
| 56 | ide_bus_ok[i] = 0; |
| 57 | for (i = 0; i < CONFIG_SYS_IDE_MAXDEVICE; ++i) |
| 58 | ide_dev_desc[i].type = DEV_TYPE_UNKNOWN; |
| 59 | |
| 60 | ide_set_reset(1); /* assert reset */ |
| 61 | |
| 62 | /* the reset signal shall be asserted for et least 25 us */ |
| 63 | udelay(25); |
| 64 | |
Stefan Roese | 80877fa | 2022-09-02 14:10:46 +0200 | [diff] [blame] | 65 | schedule(); |
Simon Glass | e11f0ef | 2016-05-01 11:36:10 -0600 | [diff] [blame] | 66 | |
| 67 | /* de-assert RESET signal */ |
| 68 | ide_set_reset(0); |
| 69 | |
| 70 | /* wait 250 ms */ |
| 71 | for (i = 0; i < 250; ++i) |
| 72 | udelay(1000); |
| 73 | } |
| 74 | #else |
| 75 | #define ide_reset() /* dummy */ |
| 76 | #endif /* CONFIG_IDE_RESET */ |
| 77 | |
| 78 | /* |
| 79 | * Wait until Busy bit is off, or timeout (in ms) |
| 80 | * Return last status |
| 81 | */ |
| 82 | static uchar ide_wait(int dev, ulong t) |
| 83 | { |
| 84 | ulong delay = 10 * t; /* poll every 100 us */ |
| 85 | uchar c; |
| 86 | |
| 87 | while ((c = ide_inb(dev, ATA_STATUS)) & ATA_STAT_BUSY) { |
| 88 | udelay(100); |
| 89 | if (delay-- == 0) |
| 90 | break; |
| 91 | } |
| 92 | return c; |
| 93 | } |
| 94 | |
| 95 | /* |
| 96 | * copy src to dest, skipping leading and trailing blanks and null |
| 97 | * terminate the string |
| 98 | * "len" is the size of available memory including the terminating '\0' |
| 99 | */ |
| 100 | static void ident_cpy(unsigned char *dst, unsigned char *src, |
| 101 | unsigned int len) |
| 102 | { |
| 103 | unsigned char *end, *last; |
| 104 | |
| 105 | last = dst; |
| 106 | end = src + len - 1; |
| 107 | |
| 108 | /* reserve space for '\0' */ |
| 109 | if (len < 2) |
| 110 | goto OUT; |
| 111 | |
| 112 | /* skip leading white space */ |
| 113 | while ((*src) && (src < end) && (*src == ' ')) |
| 114 | ++src; |
| 115 | |
| 116 | /* copy string, omitting trailing white space */ |
| 117 | while ((*src) && (src < end)) { |
| 118 | *dst++ = *src; |
| 119 | if (*src++ != ' ') |
| 120 | last = dst; |
| 121 | } |
| 122 | OUT: |
| 123 | *last = '\0'; |
| 124 | } |
| 125 | |
| 126 | #ifdef CONFIG_ATAPI |
| 127 | /**************************************************************************** |
| 128 | * ATAPI Support |
| 129 | */ |
| 130 | |
Simon Glass | e11f0ef | 2016-05-01 11:36:10 -0600 | [diff] [blame] | 131 | /* since ATAPI may use commands with not 4 bytes alligned length |
| 132 | * we have our own transfer functions, 2 bytes alligned */ |
| 133 | __weak void ide_output_data_shorts(int dev, ushort *sect_buf, int shorts) |
| 134 | { |
Reinoud Zandijk | 3b5dc94 | 2021-02-24 17:44:42 +0100 | [diff] [blame] | 135 | uintptr_t paddr = (ATA_CURR_BASE(dev) + ATA_DATA_REG); |
Simon Glass | e11f0ef | 2016-05-01 11:36:10 -0600 | [diff] [blame] | 136 | ushort *dbuf; |
Simon Glass | e11f0ef | 2016-05-01 11:36:10 -0600 | [diff] [blame] | 137 | |
Simon Glass | e11f0ef | 2016-05-01 11:36:10 -0600 | [diff] [blame] | 138 | dbuf = (ushort *)sect_buf; |
| 139 | |
Reinoud Zandijk | 3b5dc94 | 2021-02-24 17:44:42 +0100 | [diff] [blame] | 140 | debug("in output data shorts base for read is %p\n", (void *)paddr); |
Simon Glass | e11f0ef | 2016-05-01 11:36:10 -0600 | [diff] [blame] | 141 | |
| 142 | while (shorts--) { |
| 143 | EIEIO; |
Reinoud Zandijk | 3b5dc94 | 2021-02-24 17:44:42 +0100 | [diff] [blame] | 144 | outw(cpu_to_le16(*dbuf++), paddr); |
Simon Glass | e11f0ef | 2016-05-01 11:36:10 -0600 | [diff] [blame] | 145 | } |
| 146 | } |
| 147 | |
| 148 | __weak void ide_input_data_shorts(int dev, ushort *sect_buf, int shorts) |
| 149 | { |
Reinoud Zandijk | 3b5dc94 | 2021-02-24 17:44:42 +0100 | [diff] [blame] | 150 | uintptr_t paddr = (ATA_CURR_BASE(dev) + ATA_DATA_REG); |
Simon Glass | e11f0ef | 2016-05-01 11:36:10 -0600 | [diff] [blame] | 151 | ushort *dbuf; |
Simon Glass | e11f0ef | 2016-05-01 11:36:10 -0600 | [diff] [blame] | 152 | |
Simon Glass | e11f0ef | 2016-05-01 11:36:10 -0600 | [diff] [blame] | 153 | dbuf = (ushort *)sect_buf; |
| 154 | |
Reinoud Zandijk | 3b5dc94 | 2021-02-24 17:44:42 +0100 | [diff] [blame] | 155 | debug("in input data shorts base for read is %p\n", (void *)paddr); |
Simon Glass | e11f0ef | 2016-05-01 11:36:10 -0600 | [diff] [blame] | 156 | |
| 157 | while (shorts--) { |
| 158 | EIEIO; |
Reinoud Zandijk | 3b5dc94 | 2021-02-24 17:44:42 +0100 | [diff] [blame] | 159 | *dbuf++ = le16_to_cpu(inw(paddr)); |
Simon Glass | e11f0ef | 2016-05-01 11:36:10 -0600 | [diff] [blame] | 160 | } |
| 161 | } |
| 162 | |
Simon Glass | e11f0ef | 2016-05-01 11:36:10 -0600 | [diff] [blame] | 163 | /* |
| 164 | * Wait until (Status & mask) == res, or timeout (in ms) |
| 165 | * Return last status |
| 166 | * This is used since some ATAPI CD ROMs clears their Busy Bit first |
| 167 | * and then they set their DRQ Bit |
| 168 | */ |
| 169 | static uchar atapi_wait_mask(int dev, ulong t, uchar mask, uchar res) |
| 170 | { |
| 171 | ulong delay = 10 * t; /* poll every 100 us */ |
| 172 | uchar c; |
| 173 | |
| 174 | /* prevents to read the status before valid */ |
| 175 | c = ide_inb(dev, ATA_DEV_CTL); |
| 176 | |
| 177 | while (((c = ide_inb(dev, ATA_STATUS)) & mask) != res) { |
| 178 | /* break if error occurs (doesn't make sense to wait more) */ |
| 179 | if ((c & ATA_STAT_ERR) == ATA_STAT_ERR) |
| 180 | break; |
| 181 | udelay(100); |
| 182 | if (delay-- == 0) |
| 183 | break; |
| 184 | } |
| 185 | return c; |
| 186 | } |
| 187 | |
| 188 | /* |
| 189 | * issue an atapi command |
| 190 | */ |
| 191 | unsigned char atapi_issue(int device, unsigned char *ccb, int ccblen, |
| 192 | unsigned char *buffer, int buflen) |
| 193 | { |
| 194 | unsigned char c, err, mask, res; |
| 195 | int n; |
| 196 | |
Simon Glass | e11f0ef | 2016-05-01 11:36:10 -0600 | [diff] [blame] | 197 | /* Select device |
| 198 | */ |
| 199 | mask = ATA_STAT_BUSY | ATA_STAT_DRQ; |
| 200 | res = 0; |
| 201 | ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device)); |
| 202 | c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res); |
| 203 | if ((c & mask) != res) { |
| 204 | printf("ATAPI_ISSUE: device %d not ready status %X\n", device, |
| 205 | c); |
| 206 | err = 0xFF; |
| 207 | goto AI_OUT; |
| 208 | } |
| 209 | /* write taskfile */ |
| 210 | ide_outb(device, ATA_ERROR_REG, 0); /* no DMA, no overlaped */ |
| 211 | ide_outb(device, ATA_SECT_CNT, 0); |
| 212 | ide_outb(device, ATA_SECT_NUM, 0); |
| 213 | ide_outb(device, ATA_CYL_LOW, (unsigned char) (buflen & 0xFF)); |
| 214 | ide_outb(device, ATA_CYL_HIGH, |
| 215 | (unsigned char) ((buflen >> 8) & 0xFF)); |
| 216 | ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device)); |
| 217 | |
Heinrich Schuchardt | 203d83a | 2020-02-27 18:28:00 +0100 | [diff] [blame] | 218 | ide_outb(device, ATA_COMMAND, ATA_CMD_PACKET); |
Simon Glass | e11f0ef | 2016-05-01 11:36:10 -0600 | [diff] [blame] | 219 | udelay(50); |
| 220 | |
| 221 | mask = ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR; |
| 222 | res = ATA_STAT_DRQ; |
| 223 | c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res); |
| 224 | |
| 225 | if ((c & mask) != res) { /* DRQ must be 1, BSY 0 */ |
| 226 | printf("ATAPI_ISSUE: Error (no IRQ) before sending ccb dev %d status 0x%02x\n", |
| 227 | device, c); |
| 228 | err = 0xFF; |
| 229 | goto AI_OUT; |
| 230 | } |
| 231 | |
| 232 | /* write command block */ |
| 233 | ide_output_data_shorts(device, (unsigned short *)ccb, ccblen / 2); |
| 234 | |
| 235 | /* ATAPI Command written wait for completition */ |
| 236 | udelay(5000); /* device must set bsy */ |
| 237 | |
| 238 | mask = ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR; |
| 239 | /* |
| 240 | * if no data wait for DRQ = 0 BSY = 0 |
| 241 | * if data wait for DRQ = 1 BSY = 0 |
| 242 | */ |
| 243 | res = 0; |
| 244 | if (buflen) |
| 245 | res = ATA_STAT_DRQ; |
| 246 | c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res); |
| 247 | if ((c & mask) != res) { |
| 248 | if (c & ATA_STAT_ERR) { |
| 249 | err = (ide_inb(device, ATA_ERROR_REG)) >> 4; |
| 250 | debug("atapi_issue 1 returned sense key %X status %02X\n", |
| 251 | err, c); |
| 252 | } else { |
| 253 | printf("ATAPI_ISSUE: (no DRQ) after sending ccb (%x) status 0x%02x\n", |
| 254 | ccb[0], c); |
| 255 | err = 0xFF; |
| 256 | } |
| 257 | goto AI_OUT; |
| 258 | } |
| 259 | n = ide_inb(device, ATA_CYL_HIGH); |
| 260 | n <<= 8; |
| 261 | n += ide_inb(device, ATA_CYL_LOW); |
| 262 | if (n > buflen) { |
| 263 | printf("ERROR, transfer bytes %d requested only %d\n", n, |
| 264 | buflen); |
| 265 | err = 0xff; |
| 266 | goto AI_OUT; |
| 267 | } |
| 268 | if ((n == 0) && (buflen < 0)) { |
| 269 | printf("ERROR, transfer bytes %d requested %d\n", n, buflen); |
| 270 | err = 0xff; |
| 271 | goto AI_OUT; |
| 272 | } |
| 273 | if (n != buflen) { |
| 274 | debug("WARNING, transfer bytes %d not equal with requested %d\n", |
| 275 | n, buflen); |
| 276 | } |
| 277 | if (n != 0) { /* data transfer */ |
| 278 | debug("ATAPI_ISSUE: %d Bytes to transfer\n", n); |
| 279 | /* we transfer shorts */ |
| 280 | n >>= 1; |
| 281 | /* ok now decide if it is an in or output */ |
| 282 | if ((ide_inb(device, ATA_SECT_CNT) & 0x02) == 0) { |
| 283 | debug("Write to device\n"); |
| 284 | ide_output_data_shorts(device, (unsigned short *)buffer, |
| 285 | n); |
| 286 | } else { |
| 287 | debug("Read from device @ %p shorts %d\n", buffer, n); |
| 288 | ide_input_data_shorts(device, (unsigned short *)buffer, |
| 289 | n); |
| 290 | } |
| 291 | } |
| 292 | udelay(5000); /* seems that some CD ROMs need this... */ |
| 293 | mask = ATA_STAT_BUSY | ATA_STAT_ERR; |
| 294 | res = 0; |
| 295 | c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res); |
| 296 | if ((c & ATA_STAT_ERR) == ATA_STAT_ERR) { |
| 297 | err = (ide_inb(device, ATA_ERROR_REG) >> 4); |
| 298 | debug("atapi_issue 2 returned sense key %X status %X\n", err, |
| 299 | c); |
| 300 | } else { |
| 301 | err = 0; |
| 302 | } |
| 303 | AI_OUT: |
Simon Glass | e11f0ef | 2016-05-01 11:36:10 -0600 | [diff] [blame] | 304 | return err; |
| 305 | } |
| 306 | |
| 307 | /* |
| 308 | * sending the command to atapi_issue. If an status other than good |
| 309 | * returns, an request_sense will be issued |
| 310 | */ |
| 311 | |
| 312 | #define ATAPI_DRIVE_NOT_READY 100 |
| 313 | #define ATAPI_UNIT_ATTN 10 |
| 314 | |
| 315 | unsigned char atapi_issue_autoreq(int device, |
| 316 | unsigned char *ccb, |
| 317 | int ccblen, |
| 318 | unsigned char *buffer, int buflen) |
| 319 | { |
| 320 | unsigned char sense_data[18], sense_ccb[12]; |
| 321 | unsigned char res, key, asc, ascq; |
| 322 | int notready, unitattn; |
| 323 | |
| 324 | unitattn = ATAPI_UNIT_ATTN; |
| 325 | notready = ATAPI_DRIVE_NOT_READY; |
| 326 | |
| 327 | retry: |
| 328 | res = atapi_issue(device, ccb, ccblen, buffer, buflen); |
| 329 | if (res == 0) |
| 330 | return 0; /* Ok */ |
| 331 | |
| 332 | if (res == 0xFF) |
| 333 | return 0xFF; /* error */ |
| 334 | |
| 335 | debug("(auto_req)atapi_issue returned sense key %X\n", res); |
| 336 | |
| 337 | memset(sense_ccb, 0, sizeof(sense_ccb)); |
| 338 | memset(sense_data, 0, sizeof(sense_data)); |
| 339 | sense_ccb[0] = ATAPI_CMD_REQ_SENSE; |
| 340 | sense_ccb[4] = 18; /* allocation Length */ |
| 341 | |
| 342 | res = atapi_issue(device, sense_ccb, 12, sense_data, 18); |
| 343 | key = (sense_data[2] & 0xF); |
| 344 | asc = (sense_data[12]); |
| 345 | ascq = (sense_data[13]); |
| 346 | |
| 347 | debug("ATAPI_CMD_REQ_SENSE returned %x\n", res); |
| 348 | debug(" Sense page: %02X key %02X ASC %02X ASCQ %02X\n", |
| 349 | sense_data[0], key, asc, ascq); |
| 350 | |
| 351 | if ((key == 0)) |
| 352 | return 0; /* ok device ready */ |
| 353 | |
| 354 | if ((key == 6) || (asc == 0x29) || (asc == 0x28)) { /* Unit Attention */ |
| 355 | if (unitattn-- > 0) { |
| 356 | udelay(200 * 1000); |
| 357 | goto retry; |
| 358 | } |
| 359 | printf("Unit Attention, tried %d\n", ATAPI_UNIT_ATTN); |
| 360 | goto error; |
| 361 | } |
| 362 | if ((asc == 0x4) && (ascq == 0x1)) { |
| 363 | /* not ready, but will be ready soon */ |
| 364 | if (notready-- > 0) { |
| 365 | udelay(200 * 1000); |
| 366 | goto retry; |
| 367 | } |
| 368 | printf("Drive not ready, tried %d times\n", |
| 369 | ATAPI_DRIVE_NOT_READY); |
| 370 | goto error; |
| 371 | } |
| 372 | if (asc == 0x3a) { |
| 373 | debug("Media not present\n"); |
| 374 | goto error; |
| 375 | } |
| 376 | |
| 377 | printf("ERROR: Unknown Sense key %02X ASC %02X ASCQ %02X\n", key, asc, |
| 378 | ascq); |
| 379 | error: |
| 380 | debug("ERROR Sense key %02X ASC %02X ASCQ %02X\n", key, asc, ascq); |
| 381 | return 0xFF; |
| 382 | } |
| 383 | |
| 384 | /* |
| 385 | * atapi_read: |
| 386 | * we transfer only one block per command, since the multiple DRQ per |
| 387 | * command is not yet implemented |
| 388 | */ |
| 389 | #define ATAPI_READ_MAX_BYTES 2048 /* we read max 2kbytes */ |
| 390 | #define ATAPI_READ_BLOCK_SIZE 2048 /* assuming CD part */ |
| 391 | #define ATAPI_READ_MAX_BLOCK (ATAPI_READ_MAX_BYTES/ATAPI_READ_BLOCK_SIZE) |
| 392 | |
| 393 | ulong atapi_read(struct blk_desc *block_dev, lbaint_t blknr, lbaint_t blkcnt, |
| 394 | void *buffer) |
| 395 | { |
| 396 | int device = block_dev->devnum; |
| 397 | ulong n = 0; |
| 398 | unsigned char ccb[12]; /* Command descriptor block */ |
| 399 | ulong cnt; |
| 400 | |
| 401 | debug("atapi_read dev %d start " LBAF " blocks " LBAF |
| 402 | " buffer at %lX\n", device, blknr, blkcnt, (ulong) buffer); |
| 403 | |
| 404 | do { |
| 405 | if (blkcnt > ATAPI_READ_MAX_BLOCK) |
| 406 | cnt = ATAPI_READ_MAX_BLOCK; |
| 407 | else |
| 408 | cnt = blkcnt; |
| 409 | |
| 410 | ccb[0] = ATAPI_CMD_READ_12; |
| 411 | ccb[1] = 0; /* reserved */ |
| 412 | ccb[2] = (unsigned char) (blknr >> 24) & 0xFF; /* MSB Block */ |
| 413 | ccb[3] = (unsigned char) (blknr >> 16) & 0xFF; /* */ |
| 414 | ccb[4] = (unsigned char) (blknr >> 8) & 0xFF; |
| 415 | ccb[5] = (unsigned char) blknr & 0xFF; /* LSB Block */ |
| 416 | ccb[6] = (unsigned char) (cnt >> 24) & 0xFF; /* MSB Block cnt */ |
| 417 | ccb[7] = (unsigned char) (cnt >> 16) & 0xFF; |
| 418 | ccb[8] = (unsigned char) (cnt >> 8) & 0xFF; |
| 419 | ccb[9] = (unsigned char) cnt & 0xFF; /* LSB Block */ |
| 420 | ccb[10] = 0; /* reserved */ |
| 421 | ccb[11] = 0; /* reserved */ |
| 422 | |
| 423 | if (atapi_issue_autoreq(device, ccb, 12, |
| 424 | (unsigned char *)buffer, |
| 425 | cnt * ATAPI_READ_BLOCK_SIZE) |
| 426 | == 0xFF) { |
| 427 | return n; |
| 428 | } |
| 429 | n += cnt; |
| 430 | blkcnt -= cnt; |
| 431 | blknr += cnt; |
| 432 | buffer += (cnt * ATAPI_READ_BLOCK_SIZE); |
| 433 | } while (blkcnt > 0); |
| 434 | return n; |
| 435 | } |
| 436 | |
| 437 | static void atapi_inquiry(struct blk_desc *dev_desc) |
| 438 | { |
| 439 | unsigned char ccb[12]; /* Command descriptor block */ |
| 440 | unsigned char iobuf[64]; /* temp buf */ |
| 441 | unsigned char c; |
| 442 | int device; |
| 443 | |
| 444 | device = dev_desc->devnum; |
| 445 | dev_desc->type = DEV_TYPE_UNKNOWN; /* not yet valid */ |
Simon Glass | e11f0ef | 2016-05-01 11:36:10 -0600 | [diff] [blame] | 446 | |
| 447 | memset(ccb, 0, sizeof(ccb)); |
| 448 | memset(iobuf, 0, sizeof(iobuf)); |
| 449 | |
| 450 | ccb[0] = ATAPI_CMD_INQUIRY; |
| 451 | ccb[4] = 40; /* allocation Legnth */ |
| 452 | c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *)iobuf, 40); |
| 453 | |
| 454 | debug("ATAPI_CMD_INQUIRY returned %x\n", c); |
| 455 | if (c != 0) |
| 456 | return; |
| 457 | |
| 458 | /* copy device ident strings */ |
| 459 | ident_cpy((unsigned char *)dev_desc->vendor, &iobuf[8], 8); |
| 460 | ident_cpy((unsigned char *)dev_desc->product, &iobuf[16], 16); |
| 461 | ident_cpy((unsigned char *)dev_desc->revision, &iobuf[32], 5); |
| 462 | |
| 463 | dev_desc->lun = 0; |
| 464 | dev_desc->lba = 0; |
| 465 | dev_desc->blksz = 0; |
| 466 | dev_desc->log2blksz = LOG2_INVALID(typeof(dev_desc->log2blksz)); |
| 467 | dev_desc->type = iobuf[0] & 0x1f; |
| 468 | |
| 469 | if ((iobuf[1] & 0x80) == 0x80) |
| 470 | dev_desc->removable = 1; |
| 471 | else |
| 472 | dev_desc->removable = 0; |
| 473 | |
| 474 | memset(ccb, 0, sizeof(ccb)); |
| 475 | memset(iobuf, 0, sizeof(iobuf)); |
| 476 | ccb[0] = ATAPI_CMD_START_STOP; |
| 477 | ccb[4] = 0x03; /* start */ |
| 478 | |
| 479 | c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *)iobuf, 0); |
| 480 | |
| 481 | debug("ATAPI_CMD_START_STOP returned %x\n", c); |
| 482 | if (c != 0) |
| 483 | return; |
| 484 | |
| 485 | memset(ccb, 0, sizeof(ccb)); |
| 486 | memset(iobuf, 0, sizeof(iobuf)); |
| 487 | c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *)iobuf, 0); |
| 488 | |
| 489 | debug("ATAPI_CMD_UNIT_TEST_READY returned %x\n", c); |
| 490 | if (c != 0) |
| 491 | return; |
| 492 | |
| 493 | memset(ccb, 0, sizeof(ccb)); |
| 494 | memset(iobuf, 0, sizeof(iobuf)); |
| 495 | ccb[0] = ATAPI_CMD_READ_CAP; |
| 496 | c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *)iobuf, 8); |
| 497 | debug("ATAPI_CMD_READ_CAP returned %x\n", c); |
| 498 | if (c != 0) |
| 499 | return; |
| 500 | |
| 501 | debug("Read Cap: LBA %02X%02X%02X%02X blksize %02X%02X%02X%02X\n", |
| 502 | iobuf[0], iobuf[1], iobuf[2], iobuf[3], |
| 503 | iobuf[4], iobuf[5], iobuf[6], iobuf[7]); |
| 504 | |
| 505 | dev_desc->lba = ((unsigned long) iobuf[0] << 24) + |
| 506 | ((unsigned long) iobuf[1] << 16) + |
| 507 | ((unsigned long) iobuf[2] << 8) + ((unsigned long) iobuf[3]); |
| 508 | dev_desc->blksz = ((unsigned long) iobuf[4] << 24) + |
| 509 | ((unsigned long) iobuf[5] << 16) + |
| 510 | ((unsigned long) iobuf[6] << 8) + ((unsigned long) iobuf[7]); |
| 511 | dev_desc->log2blksz = LOG2(dev_desc->blksz); |
| 512 | #ifdef CONFIG_LBA48 |
| 513 | /* ATAPI devices cannot use 48bit addressing (ATA/ATAPI v7) */ |
| 514 | dev_desc->lba48 = 0; |
| 515 | #endif |
| 516 | return; |
| 517 | } |
| 518 | |
| 519 | #endif /* CONFIG_ATAPI */ |
| 520 | |
| 521 | static void ide_ident(struct blk_desc *dev_desc) |
| 522 | { |
| 523 | unsigned char c; |
| 524 | hd_driveid_t iop; |
Simon Glass | e11f0ef | 2016-05-01 11:36:10 -0600 | [diff] [blame] | 525 | #ifdef CONFIG_ATAPI |
Simon Glass | 0cdb293 | 2022-08-11 19:34:53 -0600 | [diff] [blame] | 526 | bool is_atapi = false; |
Simon Glass | e11f0ef | 2016-05-01 11:36:10 -0600 | [diff] [blame] | 527 | int retries = 0; |
| 528 | #endif |
| 529 | int device; |
| 530 | |
| 531 | device = dev_desc->devnum; |
| 532 | printf(" Device %d: ", device); |
| 533 | |
Simon Glass | e11f0ef | 2016-05-01 11:36:10 -0600 | [diff] [blame] | 534 | /* Select device |
| 535 | */ |
| 536 | ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device)); |
Simon Glass | fada3f9 | 2022-09-17 09:00:09 -0600 | [diff] [blame] | 537 | dev_desc->uclass_id = UCLASS_IDE; |
Simon Glass | e11f0ef | 2016-05-01 11:36:10 -0600 | [diff] [blame] | 538 | #ifdef CONFIG_ATAPI |
| 539 | |
| 540 | retries = 0; |
| 541 | |
| 542 | /* Warning: This will be tricky to read */ |
| 543 | while (retries <= 1) { |
| 544 | /* check signature */ |
| 545 | if ((ide_inb(device, ATA_SECT_CNT) == 0x01) && |
| 546 | (ide_inb(device, ATA_SECT_NUM) == 0x01) && |
| 547 | (ide_inb(device, ATA_CYL_LOW) == 0x14) && |
| 548 | (ide_inb(device, ATA_CYL_HIGH) == 0xEB)) { |
| 549 | /* ATAPI Signature found */ |
Simon Glass | 0cdb293 | 2022-08-11 19:34:53 -0600 | [diff] [blame] | 550 | is_atapi = true; |
Simon Glass | e11f0ef | 2016-05-01 11:36:10 -0600 | [diff] [blame] | 551 | /* |
| 552 | * Start Ident Command |
| 553 | */ |
Heinrich Schuchardt | 203d83a | 2020-02-27 18:28:00 +0100 | [diff] [blame] | 554 | ide_outb(device, ATA_COMMAND, ATA_CMD_ID_ATAPI); |
Simon Glass | e11f0ef | 2016-05-01 11:36:10 -0600 | [diff] [blame] | 555 | /* |
| 556 | * Wait for completion - ATAPI devices need more time |
| 557 | * to become ready |
| 558 | */ |
| 559 | c = ide_wait(device, ATAPI_TIME_OUT); |
| 560 | } else |
| 561 | #endif |
| 562 | { |
| 563 | /* |
| 564 | * Start Ident Command |
| 565 | */ |
Heinrich Schuchardt | 203d83a | 2020-02-27 18:28:00 +0100 | [diff] [blame] | 566 | ide_outb(device, ATA_COMMAND, ATA_CMD_ID_ATA); |
Simon Glass | e11f0ef | 2016-05-01 11:36:10 -0600 | [diff] [blame] | 567 | |
| 568 | /* |
| 569 | * Wait for completion |
| 570 | */ |
| 571 | c = ide_wait(device, IDE_TIME_OUT); |
| 572 | } |
Simon Glass | e11f0ef | 2016-05-01 11:36:10 -0600 | [diff] [blame] | 573 | |
| 574 | if (((c & ATA_STAT_DRQ) == 0) || |
| 575 | ((c & (ATA_STAT_FAULT | ATA_STAT_ERR)) != 0)) { |
| 576 | #ifdef CONFIG_ATAPI |
| 577 | { |
| 578 | /* |
| 579 | * Need to soft reset the device |
| 580 | * in case it's an ATAPI... |
| 581 | */ |
| 582 | debug("Retrying...\n"); |
| 583 | ide_outb(device, ATA_DEV_HD, |
| 584 | ATA_LBA | ATA_DEVICE(device)); |
| 585 | udelay(100000); |
| 586 | ide_outb(device, ATA_COMMAND, 0x08); |
| 587 | udelay(500000); /* 500 ms */ |
| 588 | } |
| 589 | /* |
| 590 | * Select device |
| 591 | */ |
| 592 | ide_outb(device, ATA_DEV_HD, |
| 593 | ATA_LBA | ATA_DEVICE(device)); |
| 594 | retries++; |
| 595 | #else |
| 596 | return; |
| 597 | #endif |
| 598 | } |
| 599 | #ifdef CONFIG_ATAPI |
| 600 | else |
| 601 | break; |
| 602 | } /* see above - ugly to read */ |
| 603 | |
| 604 | if (retries == 2) /* Not found */ |
| 605 | return; |
| 606 | #endif |
| 607 | |
| 608 | ide_input_swap_data(device, (ulong *)&iop, ATA_SECTORWORDS); |
| 609 | |
| 610 | ident_cpy((unsigned char *)dev_desc->revision, iop.fw_rev, |
| 611 | sizeof(dev_desc->revision)); |
| 612 | ident_cpy((unsigned char *)dev_desc->vendor, iop.model, |
| 613 | sizeof(dev_desc->vendor)); |
| 614 | ident_cpy((unsigned char *)dev_desc->product, iop.serial_no, |
| 615 | sizeof(dev_desc->product)); |
Simon Glass | e11f0ef | 2016-05-01 11:36:10 -0600 | [diff] [blame] | 616 | |
| 617 | if ((iop.config & 0x0080) == 0x0080) |
| 618 | dev_desc->removable = 1; |
| 619 | else |
| 620 | dev_desc->removable = 0; |
| 621 | |
| 622 | #ifdef CONFIG_ATAPI |
Simon Glass | 0cdb293 | 2022-08-11 19:34:53 -0600 | [diff] [blame] | 623 | if (is_atapi) { |
Simon Glass | e11f0ef | 2016-05-01 11:36:10 -0600 | [diff] [blame] | 624 | atapi_inquiry(dev_desc); |
| 625 | return; |
| 626 | } |
| 627 | #endif /* CONFIG_ATAPI */ |
| 628 | |
Reinoud Zandijk | 3b5dc94 | 2021-02-24 17:44:42 +0100 | [diff] [blame] | 629 | iop.lba_capacity[0] = be16_to_cpu(iop.lba_capacity[0]); |
| 630 | iop.lba_capacity[1] = be16_to_cpu(iop.lba_capacity[1]); |
| 631 | dev_desc->lba = |
| 632 | ((unsigned long)iop.lba_capacity[0]) | |
| 633 | ((unsigned long)iop.lba_capacity[1] << 16); |
Simon Glass | e11f0ef | 2016-05-01 11:36:10 -0600 | [diff] [blame] | 634 | |
| 635 | #ifdef CONFIG_LBA48 |
| 636 | if (iop.command_set_2 & 0x0400) { /* LBA 48 support */ |
| 637 | dev_desc->lba48 = 1; |
Reinoud Zandijk | 3b5dc94 | 2021-02-24 17:44:42 +0100 | [diff] [blame] | 638 | for (int i = 0; i < 4; i++) |
| 639 | iop.lba48_capacity[i] = be16_to_cpu(iop.lba48_capacity[i]); |
| 640 | dev_desc->lba = |
| 641 | ((unsigned long long)iop.lba48_capacity[0] | |
| 642 | ((unsigned long long)iop.lba48_capacity[1] << 16) | |
| 643 | ((unsigned long long)iop.lba48_capacity[2] << 32) | |
| 644 | ((unsigned long long)iop.lba48_capacity[3] << 48)); |
Simon Glass | e11f0ef | 2016-05-01 11:36:10 -0600 | [diff] [blame] | 645 | } else { |
| 646 | dev_desc->lba48 = 0; |
| 647 | } |
| 648 | #endif /* CONFIG_LBA48 */ |
| 649 | /* assuming HD */ |
| 650 | dev_desc->type = DEV_TYPE_HARDDISK; |
| 651 | dev_desc->blksz = ATA_BLOCKSIZE; |
| 652 | dev_desc->log2blksz = LOG2(dev_desc->blksz); |
| 653 | dev_desc->lun = 0; /* just to fill something in... */ |
| 654 | |
| 655 | #if 0 /* only used to test the powersaving mode, |
| 656 | * if enabled, the drive goes after 5 sec |
| 657 | * in standby mode */ |
| 658 | ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device)); |
| 659 | c = ide_wait(device, IDE_TIME_OUT); |
| 660 | ide_outb(device, ATA_SECT_CNT, 1); |
| 661 | ide_outb(device, ATA_LBA_LOW, 0); |
| 662 | ide_outb(device, ATA_LBA_MID, 0); |
| 663 | ide_outb(device, ATA_LBA_HIGH, 0); |
| 664 | ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device)); |
| 665 | ide_outb(device, ATA_COMMAND, 0xe3); |
| 666 | udelay(50); |
| 667 | c = ide_wait(device, IDE_TIME_OUT); /* can't take over 500 ms */ |
| 668 | #endif |
| 669 | } |
| 670 | |
Simon Glass | e11f0ef | 2016-05-01 11:36:10 -0600 | [diff] [blame] | 671 | __weak void ide_outb(int dev, int port, unsigned char val) |
| 672 | { |
| 673 | debug("ide_outb (dev= %d, port= 0x%x, val= 0x%02x) : @ 0x%08lx\n", |
Simon Glass | 1105680 | 2021-11-24 09:26:48 -0700 | [diff] [blame] | 674 | dev, port, val, ATA_CURR_BASE(dev) + port); |
Simon Glass | e11f0ef | 2016-05-01 11:36:10 -0600 | [diff] [blame] | 675 | |
Simon Glass | 1105680 | 2021-11-24 09:26:48 -0700 | [diff] [blame] | 676 | outb(val, ATA_CURR_BASE(dev) + port); |
Simon Glass | e11f0ef | 2016-05-01 11:36:10 -0600 | [diff] [blame] | 677 | } |
| 678 | |
| 679 | __weak unsigned char ide_inb(int dev, int port) |
| 680 | { |
| 681 | uchar val; |
| 682 | |
Simon Glass | 1105680 | 2021-11-24 09:26:48 -0700 | [diff] [blame] | 683 | val = inb(ATA_CURR_BASE(dev) + port); |
Simon Glass | e11f0ef | 2016-05-01 11:36:10 -0600 | [diff] [blame] | 684 | |
| 685 | debug("ide_inb (dev= %d, port= 0x%x) : @ 0x%08lx -> 0x%02x\n", |
Simon Glass | 1105680 | 2021-11-24 09:26:48 -0700 | [diff] [blame] | 686 | dev, port, ATA_CURR_BASE(dev) + port, val); |
Simon Glass | e11f0ef | 2016-05-01 11:36:10 -0600 | [diff] [blame] | 687 | return val; |
| 688 | } |
| 689 | |
| 690 | void ide_init(void) |
| 691 | { |
Simon Glass | 6e2bee3 | 2023-01-17 10:47:24 -0700 | [diff] [blame] | 692 | struct udevice *dev; |
Simon Glass | e11f0ef | 2016-05-01 11:36:10 -0600 | [diff] [blame] | 693 | unsigned char c; |
| 694 | int i, bus; |
| 695 | |
Stefan Roese | 80877fa | 2022-09-02 14:10:46 +0200 | [diff] [blame] | 696 | schedule(); |
Simon Glass | e11f0ef | 2016-05-01 11:36:10 -0600 | [diff] [blame] | 697 | |
Simon Glass | e11f0ef | 2016-05-01 11:36:10 -0600 | [diff] [blame] | 698 | /* ATAPI Drives seems to need a proper IDE Reset */ |
| 699 | ide_reset(); |
| 700 | |
Simon Glass | e11f0ef | 2016-05-01 11:36:10 -0600 | [diff] [blame] | 701 | /* |
| 702 | * Wait for IDE to get ready. |
| 703 | * According to spec, this can take up to 31 seconds! |
| 704 | */ |
| 705 | for (bus = 0; bus < CONFIG_SYS_IDE_MAXBUS; ++bus) { |
| 706 | int dev = |
| 707 | bus * (CONFIG_SYS_IDE_MAXDEVICE / |
| 708 | CONFIG_SYS_IDE_MAXBUS); |
| 709 | |
Simon Glass | e11f0ef | 2016-05-01 11:36:10 -0600 | [diff] [blame] | 710 | printf("Bus %d: ", bus); |
| 711 | |
| 712 | ide_bus_ok[bus] = 0; |
| 713 | |
| 714 | /* Select device |
| 715 | */ |
| 716 | udelay(100000); /* 100 ms */ |
| 717 | ide_outb(dev, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(dev)); |
| 718 | udelay(100000); /* 100 ms */ |
| 719 | i = 0; |
| 720 | do { |
| 721 | udelay(10000); /* 10 ms */ |
| 722 | |
| 723 | c = ide_inb(dev, ATA_STATUS); |
| 724 | i++; |
| 725 | if (i > (ATA_RESET_TIME * 100)) { |
| 726 | puts("** Timeout **\n"); |
Simon Glass | e11f0ef | 2016-05-01 11:36:10 -0600 | [diff] [blame] | 727 | return; |
| 728 | } |
| 729 | if ((i >= 100) && ((i % 100) == 0)) |
| 730 | putc('.'); |
| 731 | |
| 732 | } while (c & ATA_STAT_BUSY); |
| 733 | |
| 734 | if (c & (ATA_STAT_BUSY | ATA_STAT_FAULT)) { |
| 735 | puts("not available "); |
| 736 | debug("Status = 0x%02X ", c); |
| 737 | #ifndef CONFIG_ATAPI /* ATAPI Devices do not set DRDY */ |
| 738 | } else if ((c & ATA_STAT_READY) == 0) { |
| 739 | puts("not available "); |
| 740 | debug("Status = 0x%02X ", c); |
| 741 | #endif |
| 742 | } else { |
| 743 | puts("OK "); |
| 744 | ide_bus_ok[bus] = 1; |
| 745 | } |
Stefan Roese | 80877fa | 2022-09-02 14:10:46 +0200 | [diff] [blame] | 746 | schedule(); |
Simon Glass | e11f0ef | 2016-05-01 11:36:10 -0600 | [diff] [blame] | 747 | } |
| 748 | |
| 749 | putc('\n'); |
| 750 | |
Simon Glass | e11f0ef | 2016-05-01 11:36:10 -0600 | [diff] [blame] | 751 | for (i = 0; i < CONFIG_SYS_IDE_MAXDEVICE; ++i) { |
Simon Glass | e11f0ef | 2016-05-01 11:36:10 -0600 | [diff] [blame] | 752 | ide_dev_desc[i].type = DEV_TYPE_UNKNOWN; |
Simon Glass | fada3f9 | 2022-09-17 09:00:09 -0600 | [diff] [blame] | 753 | ide_dev_desc[i].uclass_id = UCLASS_IDE; |
Simon Glass | e11f0ef | 2016-05-01 11:36:10 -0600 | [diff] [blame] | 754 | ide_dev_desc[i].devnum = i; |
| 755 | ide_dev_desc[i].part_type = PART_TYPE_UNKNOWN; |
| 756 | ide_dev_desc[i].blksz = 0; |
| 757 | ide_dev_desc[i].log2blksz = |
| 758 | LOG2_INVALID(typeof(ide_dev_desc[i].log2blksz)); |
| 759 | ide_dev_desc[i].lba = 0; |
Simon Glass | e11f0ef | 2016-05-01 11:36:10 -0600 | [diff] [blame] | 760 | if (!ide_bus_ok[IDE_BUS(i)]) |
| 761 | continue; |
Simon Glass | e11f0ef | 2016-05-01 11:36:10 -0600 | [diff] [blame] | 762 | ide_ident(&ide_dev_desc[i]); |
Simon Glass | e11f0ef | 2016-05-01 11:36:10 -0600 | [diff] [blame] | 763 | dev_print(&ide_dev_desc[i]); |
Simon Glass | e11f0ef | 2016-05-01 11:36:10 -0600 | [diff] [blame] | 764 | } |
Stefan Roese | 80877fa | 2022-09-02 14:10:46 +0200 | [diff] [blame] | 765 | schedule(); |
Bin Meng | b650afa | 2017-09-10 05:12:51 -0700 | [diff] [blame] | 766 | |
Bin Meng | b650afa | 2017-09-10 05:12:51 -0700 | [diff] [blame] | 767 | uclass_first_device(UCLASS_IDE, &dev); |
Simon Glass | e11f0ef | 2016-05-01 11:36:10 -0600 | [diff] [blame] | 768 | } |
| 769 | |
Simon Glass | e11f0ef | 2016-05-01 11:36:10 -0600 | [diff] [blame] | 770 | __weak void ide_input_swap_data(int dev, ulong *sect_buf, int words) |
| 771 | { |
Reinoud Zandijk | 3b5dc94 | 2021-02-24 17:44:42 +0100 | [diff] [blame] | 772 | uintptr_t paddr = (ATA_CURR_BASE(dev) + ATA_DATA_REG); |
Simon Glass | e11f0ef | 2016-05-01 11:36:10 -0600 | [diff] [blame] | 773 | ushort *dbuf = (ushort *)sect_buf; |
| 774 | |
Reinoud Zandijk | 3b5dc94 | 2021-02-24 17:44:42 +0100 | [diff] [blame] | 775 | debug("in input swap data base for read is %p\n", (void *)paddr); |
Simon Glass | e11f0ef | 2016-05-01 11:36:10 -0600 | [diff] [blame] | 776 | |
| 777 | while (words--) { |
Reinoud Zandijk | 3b5dc94 | 2021-02-24 17:44:42 +0100 | [diff] [blame] | 778 | EIEIO; |
| 779 | *dbuf++ = be16_to_cpu(inw(paddr)); |
| 780 | EIEIO; |
| 781 | *dbuf++ = be16_to_cpu(inw(paddr)); |
Simon Glass | e11f0ef | 2016-05-01 11:36:10 -0600 | [diff] [blame] | 782 | } |
| 783 | } |
Simon Glass | e11f0ef | 2016-05-01 11:36:10 -0600 | [diff] [blame] | 784 | |
Simon Glass | e11f0ef | 2016-05-01 11:36:10 -0600 | [diff] [blame] | 785 | __weak void ide_output_data(int dev, const ulong *sect_buf, int words) |
| 786 | { |
Reinoud Zandijk | 3b5dc94 | 2021-02-24 17:44:42 +0100 | [diff] [blame] | 787 | uintptr_t paddr = (ATA_CURR_BASE(dev) + ATA_DATA_REG); |
Simon Glass | e11f0ef | 2016-05-01 11:36:10 -0600 | [diff] [blame] | 788 | ushort *dbuf; |
Simon Glass | e11f0ef | 2016-05-01 11:36:10 -0600 | [diff] [blame] | 789 | |
Simon Glass | e11f0ef | 2016-05-01 11:36:10 -0600 | [diff] [blame] | 790 | dbuf = (ushort *)sect_buf; |
| 791 | while (words--) { |
| 792 | EIEIO; |
Reinoud Zandijk | 3b5dc94 | 2021-02-24 17:44:42 +0100 | [diff] [blame] | 793 | outw(cpu_to_le16(*dbuf++), paddr); |
Simon Glass | e11f0ef | 2016-05-01 11:36:10 -0600 | [diff] [blame] | 794 | EIEIO; |
Reinoud Zandijk | 3b5dc94 | 2021-02-24 17:44:42 +0100 | [diff] [blame] | 795 | outw(cpu_to_le16(*dbuf++), paddr); |
Simon Glass | e11f0ef | 2016-05-01 11:36:10 -0600 | [diff] [blame] | 796 | } |
| 797 | } |
Simon Glass | e11f0ef | 2016-05-01 11:36:10 -0600 | [diff] [blame] | 798 | |
Simon Glass | e11f0ef | 2016-05-01 11:36:10 -0600 | [diff] [blame] | 799 | __weak void ide_input_data(int dev, ulong *sect_buf, int words) |
| 800 | { |
Reinoud Zandijk | 3b5dc94 | 2021-02-24 17:44:42 +0100 | [diff] [blame] | 801 | uintptr_t paddr = (ATA_CURR_BASE(dev) + ATA_DATA_REG); |
Simon Glass | e11f0ef | 2016-05-01 11:36:10 -0600 | [diff] [blame] | 802 | ushort *dbuf; |
Simon Glass | e11f0ef | 2016-05-01 11:36:10 -0600 | [diff] [blame] | 803 | |
Simon Glass | e11f0ef | 2016-05-01 11:36:10 -0600 | [diff] [blame] | 804 | dbuf = (ushort *)sect_buf; |
| 805 | |
Reinoud Zandijk | 3b5dc94 | 2021-02-24 17:44:42 +0100 | [diff] [blame] | 806 | debug("in input data base for read is %p\n", (void *)paddr); |
Simon Glass | e11f0ef | 2016-05-01 11:36:10 -0600 | [diff] [blame] | 807 | |
| 808 | while (words--) { |
| 809 | EIEIO; |
Reinoud Zandijk | 3b5dc94 | 2021-02-24 17:44:42 +0100 | [diff] [blame] | 810 | *dbuf++ = le16_to_cpu(inw(paddr)); |
Simon Glass | e11f0ef | 2016-05-01 11:36:10 -0600 | [diff] [blame] | 811 | EIEIO; |
Reinoud Zandijk | 3b5dc94 | 2021-02-24 17:44:42 +0100 | [diff] [blame] | 812 | *dbuf++ = le16_to_cpu(inw(paddr)); |
Simon Glass | e11f0ef | 2016-05-01 11:36:10 -0600 | [diff] [blame] | 813 | } |
Simon Glass | e11f0ef | 2016-05-01 11:36:10 -0600 | [diff] [blame] | 814 | } |
| 815 | |
Simon Glass | f94bea0 | 2016-05-01 11:36:22 -0600 | [diff] [blame] | 816 | ulong ide_read(struct udevice *dev, lbaint_t blknr, lbaint_t blkcnt, |
| 817 | void *buffer) |
Simon Glass | e11f0ef | 2016-05-01 11:36:10 -0600 | [diff] [blame] | 818 | { |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 819 | struct blk_desc *block_dev = dev_get_uclass_plat(dev); |
Simon Glass | e11f0ef | 2016-05-01 11:36:10 -0600 | [diff] [blame] | 820 | int device = block_dev->devnum; |
| 821 | ulong n = 0; |
| 822 | unsigned char c; |
| 823 | unsigned char pwrsave = 0; /* power save */ |
| 824 | |
| 825 | #ifdef CONFIG_LBA48 |
| 826 | unsigned char lba48 = 0; |
| 827 | |
| 828 | if (blknr & 0x0000fffff0000000ULL) { |
| 829 | /* more than 28 bits used, use 48bit mode */ |
| 830 | lba48 = 1; |
| 831 | } |
| 832 | #endif |
| 833 | debug("ide_read dev %d start " LBAF ", blocks " LBAF " buffer at %lX\n", |
| 834 | device, blknr, blkcnt, (ulong) buffer); |
| 835 | |
Simon Glass | e11f0ef | 2016-05-01 11:36:10 -0600 | [diff] [blame] | 836 | /* Select device |
| 837 | */ |
| 838 | ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device)); |
| 839 | c = ide_wait(device, IDE_TIME_OUT); |
| 840 | |
| 841 | if (c & ATA_STAT_BUSY) { |
| 842 | printf("IDE read: device %d not ready\n", device); |
| 843 | goto IDE_READ_E; |
| 844 | } |
| 845 | |
| 846 | /* first check if the drive is in Powersaving mode, if yes, |
| 847 | * increase the timeout value */ |
Heinrich Schuchardt | 203d83a | 2020-02-27 18:28:00 +0100 | [diff] [blame] | 848 | ide_outb(device, ATA_COMMAND, ATA_CMD_CHK_POWER); |
Simon Glass | e11f0ef | 2016-05-01 11:36:10 -0600 | [diff] [blame] | 849 | udelay(50); |
| 850 | |
| 851 | c = ide_wait(device, IDE_TIME_OUT); /* can't take over 500 ms */ |
| 852 | |
| 853 | if (c & ATA_STAT_BUSY) { |
| 854 | printf("IDE read: device %d not ready\n", device); |
| 855 | goto IDE_READ_E; |
| 856 | } |
| 857 | if ((c & ATA_STAT_ERR) == ATA_STAT_ERR) { |
| 858 | printf("No Powersaving mode %X\n", c); |
| 859 | } else { |
| 860 | c = ide_inb(device, ATA_SECT_CNT); |
| 861 | debug("Powersaving %02X\n", c); |
| 862 | if (c == 0) |
| 863 | pwrsave = 1; |
| 864 | } |
| 865 | |
| 866 | |
| 867 | while (blkcnt-- > 0) { |
| 868 | c = ide_wait(device, IDE_TIME_OUT); |
| 869 | |
| 870 | if (c & ATA_STAT_BUSY) { |
| 871 | printf("IDE read: device %d not ready\n", device); |
| 872 | break; |
| 873 | } |
| 874 | #ifdef CONFIG_LBA48 |
| 875 | if (lba48) { |
| 876 | /* write high bits */ |
| 877 | ide_outb(device, ATA_SECT_CNT, 0); |
| 878 | ide_outb(device, ATA_LBA_LOW, (blknr >> 24) & 0xFF); |
| 879 | #ifdef CONFIG_SYS_64BIT_LBA |
| 880 | ide_outb(device, ATA_LBA_MID, (blknr >> 32) & 0xFF); |
| 881 | ide_outb(device, ATA_LBA_HIGH, (blknr >> 40) & 0xFF); |
| 882 | #else |
| 883 | ide_outb(device, ATA_LBA_MID, 0); |
| 884 | ide_outb(device, ATA_LBA_HIGH, 0); |
| 885 | #endif |
| 886 | } |
| 887 | #endif |
| 888 | ide_outb(device, ATA_SECT_CNT, 1); |
| 889 | ide_outb(device, ATA_LBA_LOW, (blknr >> 0) & 0xFF); |
| 890 | ide_outb(device, ATA_LBA_MID, (blknr >> 8) & 0xFF); |
| 891 | ide_outb(device, ATA_LBA_HIGH, (blknr >> 16) & 0xFF); |
| 892 | |
| 893 | #ifdef CONFIG_LBA48 |
| 894 | if (lba48) { |
| 895 | ide_outb(device, ATA_DEV_HD, |
| 896 | ATA_LBA | ATA_DEVICE(device)); |
Reinoud Zandijk | 3b5dc94 | 2021-02-24 17:44:42 +0100 | [diff] [blame] | 897 | ide_outb(device, ATA_COMMAND, ATA_CMD_PIO_READ_EXT); |
Simon Glass | e11f0ef | 2016-05-01 11:36:10 -0600 | [diff] [blame] | 898 | |
| 899 | } else |
| 900 | #endif |
| 901 | { |
| 902 | ide_outb(device, ATA_DEV_HD, ATA_LBA | |
| 903 | ATA_DEVICE(device) | ((blknr >> 24) & 0xF)); |
Reinoud Zandijk | 3b5dc94 | 2021-02-24 17:44:42 +0100 | [diff] [blame] | 904 | ide_outb(device, ATA_COMMAND, ATA_CMD_PIO_READ); |
Simon Glass | e11f0ef | 2016-05-01 11:36:10 -0600 | [diff] [blame] | 905 | } |
| 906 | |
| 907 | udelay(50); |
| 908 | |
| 909 | if (pwrsave) { |
| 910 | /* may take up to 4 sec */ |
| 911 | c = ide_wait(device, IDE_SPIN_UP_TIME_OUT); |
| 912 | pwrsave = 0; |
| 913 | } else { |
| 914 | /* can't take over 500 ms */ |
| 915 | c = ide_wait(device, IDE_TIME_OUT); |
| 916 | } |
| 917 | |
| 918 | if ((c & (ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR)) != |
| 919 | ATA_STAT_DRQ) { |
| 920 | printf("Error (no IRQ) dev %d blk " LBAF |
| 921 | ": status %#02x\n", device, blknr, c); |
| 922 | break; |
| 923 | } |
| 924 | |
| 925 | ide_input_data(device, buffer, ATA_SECTORWORDS); |
| 926 | (void) ide_inb(device, ATA_STATUS); /* clear IRQ */ |
| 927 | |
| 928 | ++n; |
| 929 | ++blknr; |
| 930 | buffer += ATA_BLOCKSIZE; |
| 931 | } |
| 932 | IDE_READ_E: |
Simon Glass | e11f0ef | 2016-05-01 11:36:10 -0600 | [diff] [blame] | 933 | return n; |
| 934 | } |
| 935 | |
Simon Glass | f94bea0 | 2016-05-01 11:36:22 -0600 | [diff] [blame] | 936 | ulong ide_write(struct udevice *dev, lbaint_t blknr, lbaint_t blkcnt, |
| 937 | const void *buffer) |
Simon Glass | e11f0ef | 2016-05-01 11:36:10 -0600 | [diff] [blame] | 938 | { |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 939 | struct blk_desc *block_dev = dev_get_uclass_plat(dev); |
Simon Glass | e11f0ef | 2016-05-01 11:36:10 -0600 | [diff] [blame] | 940 | int device = block_dev->devnum; |
| 941 | ulong n = 0; |
| 942 | unsigned char c; |
| 943 | |
| 944 | #ifdef CONFIG_LBA48 |
| 945 | unsigned char lba48 = 0; |
| 946 | |
| 947 | if (blknr & 0x0000fffff0000000ULL) { |
| 948 | /* more than 28 bits used, use 48bit mode */ |
| 949 | lba48 = 1; |
| 950 | } |
| 951 | #endif |
| 952 | |
Simon Glass | e11f0ef | 2016-05-01 11:36:10 -0600 | [diff] [blame] | 953 | /* Select device |
| 954 | */ |
| 955 | ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device)); |
| 956 | |
| 957 | while (blkcnt-- > 0) { |
| 958 | c = ide_wait(device, IDE_TIME_OUT); |
| 959 | |
| 960 | if (c & ATA_STAT_BUSY) { |
| 961 | printf("IDE read: device %d not ready\n", device); |
| 962 | goto WR_OUT; |
| 963 | } |
| 964 | #ifdef CONFIG_LBA48 |
| 965 | if (lba48) { |
| 966 | /* write high bits */ |
| 967 | ide_outb(device, ATA_SECT_CNT, 0); |
| 968 | ide_outb(device, ATA_LBA_LOW, (blknr >> 24) & 0xFF); |
| 969 | #ifdef CONFIG_SYS_64BIT_LBA |
| 970 | ide_outb(device, ATA_LBA_MID, (blknr >> 32) & 0xFF); |
| 971 | ide_outb(device, ATA_LBA_HIGH, (blknr >> 40) & 0xFF); |
| 972 | #else |
| 973 | ide_outb(device, ATA_LBA_MID, 0); |
| 974 | ide_outb(device, ATA_LBA_HIGH, 0); |
| 975 | #endif |
| 976 | } |
| 977 | #endif |
| 978 | ide_outb(device, ATA_SECT_CNT, 1); |
| 979 | ide_outb(device, ATA_LBA_LOW, (blknr >> 0) & 0xFF); |
| 980 | ide_outb(device, ATA_LBA_MID, (blknr >> 8) & 0xFF); |
| 981 | ide_outb(device, ATA_LBA_HIGH, (blknr >> 16) & 0xFF); |
| 982 | |
| 983 | #ifdef CONFIG_LBA48 |
| 984 | if (lba48) { |
| 985 | ide_outb(device, ATA_DEV_HD, |
| 986 | ATA_LBA | ATA_DEVICE(device)); |
Reinoud Zandijk | 3b5dc94 | 2021-02-24 17:44:42 +0100 | [diff] [blame] | 987 | ide_outb(device, ATA_COMMAND, ATA_CMD_PIO_WRITE_EXT); |
Simon Glass | e11f0ef | 2016-05-01 11:36:10 -0600 | [diff] [blame] | 988 | |
| 989 | } else |
| 990 | #endif |
| 991 | { |
| 992 | ide_outb(device, ATA_DEV_HD, ATA_LBA | |
| 993 | ATA_DEVICE(device) | ((blknr >> 24) & 0xF)); |
Reinoud Zandijk | 3b5dc94 | 2021-02-24 17:44:42 +0100 | [diff] [blame] | 994 | ide_outb(device, ATA_COMMAND, ATA_CMD_PIO_WRITE); |
Simon Glass | e11f0ef | 2016-05-01 11:36:10 -0600 | [diff] [blame] | 995 | } |
| 996 | |
| 997 | udelay(50); |
| 998 | |
| 999 | /* can't take over 500 ms */ |
| 1000 | c = ide_wait(device, IDE_TIME_OUT); |
| 1001 | |
| 1002 | if ((c & (ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR)) != |
| 1003 | ATA_STAT_DRQ) { |
| 1004 | printf("Error (no IRQ) dev %d blk " LBAF |
| 1005 | ": status %#02x\n", device, blknr, c); |
| 1006 | goto WR_OUT; |
| 1007 | } |
| 1008 | |
| 1009 | ide_output_data(device, buffer, ATA_SECTORWORDS); |
| 1010 | c = ide_inb(device, ATA_STATUS); /* clear IRQ */ |
| 1011 | ++n; |
| 1012 | ++blknr; |
| 1013 | buffer += ATA_BLOCKSIZE; |
| 1014 | } |
| 1015 | WR_OUT: |
Simon Glass | e11f0ef | 2016-05-01 11:36:10 -0600 | [diff] [blame] | 1016 | return n; |
| 1017 | } |
| 1018 | |
| 1019 | #if defined(CONFIG_OF_IDE_FIXUP) |
| 1020 | int ide_device_present(int dev) |
| 1021 | { |
| 1022 | if (dev >= CONFIG_SYS_IDE_MAXBUS) |
| 1023 | return 0; |
| 1024 | return ide_dev_desc[dev].type == DEV_TYPE_UNKNOWN ? 0 : 1; |
| 1025 | } |
| 1026 | #endif |
| 1027 | |
Bin Meng | b650afa | 2017-09-10 05:12:51 -0700 | [diff] [blame] | 1028 | static int ide_blk_probe(struct udevice *udev) |
| 1029 | { |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 1030 | struct blk_desc *desc = dev_get_uclass_plat(udev); |
Bin Meng | b650afa | 2017-09-10 05:12:51 -0700 | [diff] [blame] | 1031 | |
| 1032 | /* fill in device vendor/product/rev strings */ |
| 1033 | strncpy(desc->vendor, ide_dev_desc[desc->devnum].vendor, |
| 1034 | BLK_VEN_SIZE); |
| 1035 | desc->vendor[BLK_VEN_SIZE] = '\0'; |
| 1036 | strncpy(desc->product, ide_dev_desc[desc->devnum].product, |
| 1037 | BLK_PRD_SIZE); |
| 1038 | desc->product[BLK_PRD_SIZE] = '\0'; |
| 1039 | strncpy(desc->revision, ide_dev_desc[desc->devnum].revision, |
| 1040 | BLK_REV_SIZE); |
| 1041 | desc->revision[BLK_REV_SIZE] = '\0'; |
| 1042 | |
Bin Meng | b650afa | 2017-09-10 05:12:51 -0700 | [diff] [blame] | 1043 | return 0; |
| 1044 | } |
| 1045 | |
Simon Glass | f94bea0 | 2016-05-01 11:36:22 -0600 | [diff] [blame] | 1046 | static const struct blk_ops ide_blk_ops = { |
| 1047 | .read = ide_read, |
| 1048 | .write = ide_write, |
| 1049 | }; |
| 1050 | |
| 1051 | U_BOOT_DRIVER(ide_blk) = { |
| 1052 | .name = "ide_blk", |
| 1053 | .id = UCLASS_BLK, |
| 1054 | .ops = &ide_blk_ops, |
Bin Meng | b650afa | 2017-09-10 05:12:51 -0700 | [diff] [blame] | 1055 | .probe = ide_blk_probe, |
| 1056 | }; |
| 1057 | |
| 1058 | static int ide_probe(struct udevice *udev) |
| 1059 | { |
| 1060 | struct udevice *blk_dev; |
| 1061 | char name[20]; |
| 1062 | int blksz; |
| 1063 | lbaint_t size; |
| 1064 | int i; |
| 1065 | int ret; |
| 1066 | |
| 1067 | for (i = 0; i < CONFIG_SYS_IDE_MAXDEVICE; i++) { |
| 1068 | if (ide_dev_desc[i].type != DEV_TYPE_UNKNOWN) { |
| 1069 | sprintf(name, "blk#%d", i); |
| 1070 | |
| 1071 | blksz = ide_dev_desc[i].blksz; |
| 1072 | size = blksz * ide_dev_desc[i].lba; |
Bin Meng | 5781c46 | 2017-09-10 05:12:52 -0700 | [diff] [blame] | 1073 | |
| 1074 | /* |
| 1075 | * With CDROM, if there is no CD inserted, blksz will |
| 1076 | * be zero, don't bother to create IDE block device. |
| 1077 | */ |
| 1078 | if (!blksz) |
| 1079 | continue; |
Bin Meng | b650afa | 2017-09-10 05:12:51 -0700 | [diff] [blame] | 1080 | ret = blk_create_devicef(udev, "ide_blk", name, |
Simon Glass | dbfa32c | 2022-08-11 19:34:59 -0600 | [diff] [blame] | 1081 | UCLASS_IDE, i, |
Bin Meng | b650afa | 2017-09-10 05:12:51 -0700 | [diff] [blame] | 1082 | blksz, size, &blk_dev); |
| 1083 | if (ret) |
| 1084 | return ret; |
AKASHI Takahiro | ba748cf | 2022-03-08 20:36:44 +0900 | [diff] [blame] | 1085 | |
| 1086 | ret = blk_probe_or_unbind(blk_dev); |
| 1087 | if (ret) |
| 1088 | return ret; |
Bin Meng | b650afa | 2017-09-10 05:12:51 -0700 | [diff] [blame] | 1089 | } |
| 1090 | } |
| 1091 | |
| 1092 | return 0; |
| 1093 | } |
| 1094 | |
| 1095 | U_BOOT_DRIVER(ide) = { |
| 1096 | .name = "ide", |
| 1097 | .id = UCLASS_IDE, |
| 1098 | .probe = ide_probe, |
| 1099 | }; |
| 1100 | |
| 1101 | struct pci_device_id ide_supported[] = { |
| 1102 | { PCI_DEVICE_CLASS(PCI_CLASS_STORAGE_IDE << 8, 0xffff00) }, |
| 1103 | { } |
| 1104 | }; |
| 1105 | |
| 1106 | U_BOOT_PCI_DEVICE(ide, ide_supported); |
| 1107 | |
| 1108 | UCLASS_DRIVER(ide) = { |
| 1109 | .name = "ide", |
| 1110 | .id = UCLASS_IDE, |
Simon Glass | f94bea0 | 2016-05-01 11:36:22 -0600 | [diff] [blame] | 1111 | }; |