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