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