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