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