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