Subversion Repositories freemyipod

Rev

Rev 317 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 317 Rev 328
Line 36... Line 36...
36
static long ata_last_activity_value = -1;
36
static long ata_last_activity_value = -1;
37
static long ata_sleep_timeout = 20000000;
37
static long ata_sleep_timeout = 20000000;
38
static uint32_t ata_stack[0x80];
38
static uint32_t ata_stack[0x80];
39
static bool ata_powered;
39
static bool ata_powered;
40
 
40
 
-
 
41
#ifdef ATA_HAVE_BBT
-
 
42
#include "panic.h"
-
 
43
uint16_t ata_bbt[ATA_BBT_PAGES][0x20];
-
 
44
uint32_t ata_virtual_sectors;
-
 
45
 
-
 
46
int ata_rw_sectors_internal(uint64_t sector, uint32_t count, void* buffer, bool write);
-
 
47
 
-
 
48
void ata_bbt_read_sectors(uint32_t sector, uint32_t count, void* buffer)
-
 
49
{
-
 
50
    int rc = ata_rw_sectors_internal(sector, count, buffer, false);
-
 
51
    if (IS_ERR(rc))
-
 
52
        panicf(PANIC_KILLTHREAD, "ATA: Error %08X while reading BBT (sector %d, count %d)\n",
-
 
53
               rc, sector, count);
-
 
54
}
-
 
55
#endif
-
 
56
 
41
 
57
 
42
static uint16_t ata_read_cbr(uint32_t volatile* reg)
58
static uint16_t ata_read_cbr(uint32_t volatile* reg)
43
{
59
{
44
    while (!(ATA_PIO_READY & 2)) yield();
60
    while (!(ATA_PIO_READY & 2)) yield();
45
    volatile uint32_t dummy = *reg;
61
    volatile uint32_t dummy = *reg;
Line 157... Line 173...
157
        ata_total_sectors = ata_identify_data[100]
173
        ata_total_sectors = ata_identify_data[100]
158
                          | (((uint64_t)ata_identify_data[101]) << 16)
174
                          | (((uint64_t)ata_identify_data[101]) << 16)
159
                          | (((uint64_t)ata_identify_data[102]) << 32)
175
                          | (((uint64_t)ata_identify_data[102]) << 32)
160
                          | (((uint64_t)ata_identify_data[103]) << 48);
176
                          | (((uint64_t)ata_identify_data[103]) << 48);
161
    else ata_total_sectors = ata_identify_data[60] | (((uint32_t)ata_identify_data[61]) << 16);
177
    else ata_total_sectors = ata_identify_data[60] | (((uint32_t)ata_identify_data[61]) << 16);
-
 
178
    ata_total_sectors >>= 3;
162
    if (ata_identify_data[53] & BIT(1))
179
    if (ata_identify_data[53] & BIT(1))
163
    {
180
    {
164
        if (ata_identify_data[64] & BIT(1)) piotime = 0x2072;
181
        if (ata_identify_data[64] & BIT(1)) piotime = 0x2072;
165
        else if (ata_identify_data[64] & BIT(0)) piotime = 0x7083;
182
        else if (ata_identify_data[64] & BIT(0)) piotime = 0x7083;
166
    }
183
    }
Line 207... Line 224...
207
            param |= 0x40;
224
            param |= 0x40;
208
        }
225
        }
209
    }
226
    }
210
    ata_dma = param ? true : false;
227
    ata_dma = param ? true : false;
211
    PASS_RC(ata_set_feature(0xef, param), 2, 1);
228
    PASS_RC(ata_set_feature(0xef, param), 2, 1);
212
    if (ata_identify_data[82] & BIT(5)) PASS_RC(ata_set_feature(2, 0), 2, 2);
229
    if (ata_identify_data[82] & BIT(5)) PASS_RC(ata_set_feature(0x82, 0), 2, 2);
213
    if (ata_identify_data[82] & BIT(6)) PASS_RC(ata_set_feature(0x55, 0), 2, 3);
230
    if (ata_identify_data[82] & BIT(6)) PASS_RC(ata_set_feature(0xaa, 0), 2, 3);
214
    ATA_PIO_TIME = piotime;
231
    ATA_PIO_TIME = piotime;
215
    ATA_MDMA_TIME = mdmatime;
232
    ATA_MDMA_TIME = mdmatime;
216
    ATA_UDMA_TIME = udmatime;
233
    ATA_UDMA_TIME = udmatime;
217
    ata_powered = true;
234
    ata_powered = true;
218
    ata_set_active();
235
    ata_set_active();
Line 233... Line 250...
233
    i2c_sendbyte(0, 0xe6, 0x1b, 0);
250
    i2c_sendbyte(0, 0xe6, 0x1b, 0);
234
}
251
}
235
 
252
 
236
int ata_rw_sectors(uint64_t sector, uint32_t count, void* buffer, bool write)
253
int ata_rw_sectors(uint64_t sector, uint32_t count, void* buffer, bool write)
237
{
254
{
-
 
255
#ifdef ATA_HAVE_BBT
-
 
256
    if (sector + count > ata_virtual_sectors) RET_ERR(0);
-
 
257
    while (count)
-
 
258
    {
-
 
259
        uint32_t l0idx = sector >> 15;
-
 
260
        uint32_t l0offs = sector & 0x7fff;
-
 
261
        uint32_t cnt = MIN(count, 0x8000 - l0offs);
-
 
262
        uint32_t l0data = ata_bbt[0][l0idx << 1];
-
 
263
        uint32_t base = ata_bbt[0][(l0idx << 1) | 1] << 12;
-
 
264
        uint32_t phys;
-
 
265
        if (l0data < 0x8000) phys = sector + l0data + base;
-
 
266
        else
-
 
267
        {
-
 
268
            uint32_t l1idx = (sector >> 10) & 0x1f;
-
 
269
            uint32_t l1offs = sector & 0x3ff;
-
 
270
            cnt = MIN(count, 0x400 - l1offs);
-
 
271
            uint32_t l1data = ata_bbt[l0data & 0x7fff][l1idx];
-
 
272
            if (l1data < 0x8000) phys = sector + l1data + base;
-
 
273
            else
-
 
274
            {
-
 
275
                uint32_t l2idx = (sector >> 5) & 0x1f;
-
 
276
                uint32_t l2offs = sector & 0x1f;
-
 
277
                cnt = MIN(count, 0x20 - l2offs);
-
 
278
                uint32_t l2data = ata_bbt[l1data & 0x7fff][l2idx];
-
 
279
                if (l2data < 0x8000) phys = sector + l2data + base;
-
 
280
                else
-
 
281
                {
-
 
282
                    uint32_t l3idx = sector & 0x1f;
-
 
283
                    phys = ata_bbt[l2data & 0x7fff][l3idx];
-
 
284
                    for (cnt = 1; cnt < count && l3idx + cnt < 0x20; cnt++)
-
 
285
                        if (ata_bbt[l2data & 0x7fff][l3idx + cnt] != phys)
-
 
286
                            break;
-
 
287
                    phys += sector + base;
-
 
288
                }
-
 
289
            }
-
 
290
        }
-
 
291
        PASS_RC(ata_rw_sectors_internal(phys, cnt, buffer, write), 0, 0);
-
 
292
        buffer += cnt * SECTOR_SIZE;
-
 
293
        sector += cnt;
-
 
294
        count -= cnt;
-
 
295
    }
-
 
296
    return 0;
-
 
297
}
-
 
298
 
-
 
299
int ata_rw_sectors_internal(uint64_t sector, uint32_t count, void* buffer, bool write)
-
 
300
{
-
 
301
#endif
238
    if (sector + count > ata_total_sectors) RET_ERR(0);
302
    if (sector + count > ata_total_sectors) RET_ERR(0);
239
    if (!ata_powered) ata_power_up();
303
    if (!ata_powered) ata_power_up();
240
    ata_set_active();
304
    ata_set_active();
241
    if (ata_dma && write) clean_dcache();
305
    if (ata_dma && write) clean_dcache();
242
    else if (ata_dma) invalidate_dcache();
306
    else if (ata_dma) invalidate_dcache();
243
    ATA_COMMAND = BIT(1);
307
    ATA_COMMAND = BIT(1);
244
    while (count)
308
    while (count)
245
    {
309
    {
246
        uint32_t cnt = MIN(ata_lba48 ? 8192 : 32, count);
310
        uint32_t cnt = MIN(ata_lba48 ? 8192 : 32, count);
247
        PASS_RC(ata_wait_for_rdy(100000), 2, 0);
311
        PASS_RC(ata_wait_for_rdy(100000), 2, 1);
248
        ata_write_cbr(&ATA_PIO_DVR, 0);
312
        ata_write_cbr(&ATA_PIO_DVR, 0);
249
        if (ata_lba48)
313
        if (ata_lba48)
250
        {
314
        {
251
            ata_write_cbr(&ATA_PIO_SCR, cnt >> 5);
315
            ata_write_cbr(&ATA_PIO_SCR, cnt >> 5);
252
            ata_write_cbr(&ATA_PIO_SCR, (cnt << 3) & 0xff);
316
            ata_write_cbr(&ATA_PIO_SCR, (cnt << 3) & 0xff);
Line 272... Line 336...
272
        }
336
        }
273
        sector += cnt;
337
        sector += cnt;
274
        count -= cnt;
338
        count -= cnt;
275
        if (ata_dma)
339
        if (ata_dma)
276
        {
340
        {
277
            PASS_RC(ata_wait_for_start_of_transfer(10000000), 2, 1);
341
            PASS_RC(ata_wait_for_start_of_transfer(1500000), 2, 2);
278
            if (write)
342
            if (write)
279
            {
343
            {
280
                ATA_SBUF_START = buffer;
344
                ATA_SBUF_START = buffer;
281
                ATA_SBUF_SIZE = SECTOR_SIZE * cnt;
345
                ATA_SBUF_SIZE = SECTOR_SIZE * cnt;
282
                ATA_CFG |= BIT(4);
346
                ATA_CFG |= BIT(4);
Line 292... Line 356...
292
            ATA_CFG &= ~(BIT(7) | BIT(8));
356
            ATA_CFG &= ~(BIT(7) | BIT(8));
293
            wakeup_wait(&ata_wakeup, TIMEOUT_NONE);
357
            wakeup_wait(&ata_wakeup, TIMEOUT_NONE);
294
            ATA_IRQ = BITRANGE(0, 4);
358
            ATA_IRQ = BITRANGE(0, 4);
295
            ATA_IRQ_MASK = BIT(0);
359
            ATA_IRQ_MASK = BIT(0);
296
            ATA_COMMAND = BIT(0);
360
            ATA_COMMAND = BIT(0);
297
            if (wakeup_wait(&ata_wakeup, 10000000) == THREAD_TIMEOUT)
361
            if (wakeup_wait(&ata_wakeup, 1500000) == THREAD_TIMEOUT)
298
            {
362
            {
299
                ATA_COMMAND = BIT(1);
363
                ATA_COMMAND = BIT(1);
300
                ATA_CFG &= ~(BITRANGE(2, 3) | BIT(12));
364
                ATA_CFG &= ~(BITRANGE(2, 3) | BIT(12));
301
                RET_ERR(2);
365
                RET_ERR(3);
302
            }
366
            }
303
            ATA_COMMAND = BIT(1);
367
            ATA_COMMAND = BIT(1);
304
            ATA_CFG &= ~(BITRANGE(2, 3) | BIT(12));
368
            ATA_CFG &= ~(BITRANGE(2, 3) | BIT(12));
305
            buffer += SECTOR_SIZE * cnt;
369
            buffer += SECTOR_SIZE * cnt;
306
        }
370
        }
Line 308... Line 372...
308
        {
372
        {
309
            cnt *= SECTOR_SIZE / 512;
373
            cnt *= SECTOR_SIZE / 512;
310
            while (cnt--)
374
            while (cnt--)
311
            {
375
            {
312
                int i;
376
                int i;
313
                PASS_RC(ata_wait_for_start_of_transfer(10000000), 2, 1);
377
                PASS_RC(ata_wait_for_start_of_transfer(1500000), 2, 2);
314
                if (write)
378
                if (write)
315
                    for (i = 0; i < 256; i++)
379
                    for (i = 0; i < 256; i++)
316
                        ata_write_cbr(&ATA_PIO_DTR, ((uint16_t*)buffer)[i]);
380
                        ata_write_cbr(&ATA_PIO_DTR, ((uint16_t*)buffer)[i]);
317
                else
381
                else
318
                    for (i = 0; i < 256; i++)
382
                    for (i = 0; i < 256; i++)
Line 354... Line 418...
354
 
418
 
355
int ata_read_sectors(IF_MD2(int drive,) unsigned long start, int incount,
419
int ata_read_sectors(IF_MD2(int drive,) unsigned long start, int incount,
356
                     void* inbuf)
420
                     void* inbuf)
357
{
421
{
358
    mutex_lock(&ata_mutex, TIMEOUT_BLOCK);
422
    mutex_lock(&ata_mutex, TIMEOUT_BLOCK);
359
    int tries = 3;
423
    int tries = 1;
360
    int rc = -1;
424
    int rc = -1;
361
    while (--tries && rc)
425
    while (tries-- && rc)
362
    {
426
    {
363
        rc = ata_rw_sectors(start, incount, inbuf, false);
427
        rc = ata_rw_sectors(start, incount, inbuf, false);
364
        if (rc) ata_soft_reset();
428
        if (rc) ata_soft_reset();
365
    }
429
    }
366
    mutex_unlock(&ata_mutex);
430
    mutex_unlock(&ata_mutex);
Line 369... Line 433...
369
 
433
 
370
int ata_write_sectors(IF_MD2(int drive,) unsigned long start, int count,
434
int ata_write_sectors(IF_MD2(int drive,) unsigned long start, int count,
371
                      const void* outbuf)
435
                      const void* outbuf)
372
{
436
{
373
    mutex_lock(&ata_mutex, TIMEOUT_BLOCK);
437
    mutex_lock(&ata_mutex, TIMEOUT_BLOCK);
374
    int tries = 3;
438
    int tries = 1;
375
    int rc = -1;
439
    int rc = -1;
376
    while (--tries && rc)
440
    while (tries-- && rc)
377
    {
441
    {
378
        rc = ata_rw_sectors(start, count, (void*)((uint32_t)outbuf), true);
442
        rc = ata_rw_sectors(start, count, (void*)((uint32_t)outbuf), true);
379
        if (rc) ata_soft_reset();
443
        if (rc) ata_soft_reset();
380
    }
444
    }
381
    mutex_unlock(&ata_mutex);
445
    mutex_unlock(&ata_mutex);
Line 387... Line 451...
387
    ata_sleep_timeout = seconds * 1000000;
451
    ata_sleep_timeout = seconds * 1000000;
388
}
452
}
389
 
453
 
390
void ata_sleep(void)
454
void ata_sleep(void)
391
{
455
{
392
    call_storage_idle_notifys(false);
-
 
393
    mutex_lock(&ata_mutex, TIMEOUT_BLOCK);
456
    mutex_lock(&ata_mutex, TIMEOUT_BLOCK);
394
    ata_power_down();
457
    ata_power_down();
395
    mutex_unlock(&ata_mutex);
458
    mutex_unlock(&ata_mutex);
396
}
459
}
397
 
460
 
Line 411... Line 474...
411
}
474
}
412
 
475
 
413
void ata_get_info(IF_MD2(int drive,) struct storage_info *info)
476
void ata_get_info(IF_MD2(int drive,) struct storage_info *info)
414
{
477
{
415
    (*info).sector_size = SECTOR_SIZE;
478
    (*info).sector_size = SECTOR_SIZE;
-
 
479
#ifdef ATA_HAVE_BBT
-
 
480
    (*info).num_sectors = ata_virtual_sectors;
-
 
481
#else
416
    (*info).num_sectors = ata_total_sectors;
482
    (*info).num_sectors = ata_total_sectors;
-
 
483
#endif
417
    (*info).vendor = "Apple";
484
    (*info).vendor = "Apple";
418
    (*info).product = "iPod Classic";
485
    (*info).product = "iPod Classic";
419
    (*info).revision = "1.0";
486
    (*info).revision = "1.0";
420
}
487
}
421
 
488
 
Line 432... Line 499...
432
    PCON(8) = 0x44444444;
499
    PCON(8) = 0x44444444;
433
    PCON(9) = 0x44444444;
500
    PCON(9) = 0x44444444;
434
    PCON(10) = (PCON(10) & ~0xffff) | 0x4444;
501
    PCON(10) = (PCON(10) & ~0xffff) | 0x4444;
435
    ata_powered = false;
502
    ata_powered = false;
436
    ata_total_sectors = 0;
503
    ata_total_sectors = 0;
-
 
504
#ifdef ATA_HAVE_BBT
-
 
505
    mutex_lock(&ata_mutex, TIMEOUT_BLOCK);
-
 
506
    memset(ata_bbt, 0, sizeof(ata_bbt));
-
 
507
    ata_power_up();
-
 
508
    uint32_t* buf = (uint32_t*)(ata_bbt[ARRAYLEN(ata_bbt) - 64]);
-
 
509
    ata_bbt_read_sectors(0, 1, buf);
-
 
510
    if (!memcmp(buf, "emBIbbth", 8))
-
 
511
    {
-
 
512
        ata_virtual_sectors = buf[0x1fe];
-
 
513
        uint32_t count = buf[0x1ff];
-
 
514
        if (count > (ATA_BBT_PAGES >> 6))
-
 
515
            panicf(PANIC_KILLTHREAD, "ATA: BBT too big!\n");
-
 
516
        uint32_t i;
-
 
517
        uint32_t cnt;
-
 
518
        for (i = 0; i < count; i += cnt)
-
 
519
        {
-
 
520
            uint32_t phys = buf[0x200 + i];
-
 
521
            for (cnt = 1; cnt < count; cnt++)
-
 
522
                if (buf[0x200 + i + cnt] != phys + cnt)
-
 
523
                    break;
-
 
524
            ata_bbt_read_sectors(phys, cnt, ata_bbt[i << 6]);
-
 
525
        }
-
 
526
    }
-
 
527
    else ata_virtual_sectors = ata_total_sectors;
-
 
528
    mutex_unlock(&ata_mutex);
-
 
529
#endif
437
    thread_create("ATA idle monitor", ata_thread, ata_stack,
530
    thread_create("ATA idle monitor", ata_thread, ata_stack,
438
                  sizeof(ata_stack), USER_THREAD, 1, true);
531
                  sizeof(ata_stack), USER_THREAD, 1, true);
439
    return 0;
532
    return 0;
440
}
533
}
441
 
534