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