Subversion Repositories freemyipod

Rev

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

Rev 328 Rev 337
Line 40... Line 40...
40
 
40
 
41
#ifdef ATA_HAVE_BBT
41
#ifdef ATA_HAVE_BBT
42
#include "panic.h"
42
#include "panic.h"
43
uint16_t ata_bbt[ATA_BBT_PAGES][0x20];
43
uint16_t ata_bbt[ATA_BBT_PAGES][0x20];
44
uint32_t ata_virtual_sectors;
44
uint32_t ata_virtual_sectors;
-
 
45
uint32_t ata_last_offset;
-
 
46
uint64_t ata_last_phys;
45
 
47
 
46
int ata_rw_sectors_internal(uint64_t sector, uint32_t count, void* buffer, bool write);
48
int ata_rw_sectors_internal(uint64_t sector, uint32_t count, void* buffer, bool write);
47
 
49
 
48
void ata_bbt_read_sectors(uint32_t sector, uint32_t count, void* buffer)
50
void ata_bbt_read_sectors(uint32_t sector, uint32_t count, void* buffer)
49
{
51
{
Line 224... Line 226...
224
            param |= 0x40;
226
            param |= 0x40;
225
        }
227
        }
226
    }
228
    }
227
    ata_dma = param ? true : false;
229
    ata_dma = param ? true : false;
228
    PASS_RC(ata_set_feature(0xef, param), 2, 1);
230
    PASS_RC(ata_set_feature(0xef, param), 2, 1);
229
    if (ata_identify_data[82] & BIT(5)) PASS_RC(ata_set_feature(0x82, 0), 2, 2);
231
    if (ata_identify_data[82] & BIT(5)) PASS_RC(ata_set_feature(0x02, 0), 2, 2);
230
    if (ata_identify_data[82] & BIT(6)) PASS_RC(ata_set_feature(0xaa, 0), 2, 3);
232
    if (ata_identify_data[82] & BIT(6)) PASS_RC(ata_set_feature(0x55, 0), 2, 3);
231
    ATA_PIO_TIME = piotime;
233
    ATA_PIO_TIME = piotime;
232
    ATA_MDMA_TIME = mdmatime;
234
    ATA_MDMA_TIME = mdmatime;
233
    ATA_UDMA_TIME = udmatime;
235
    ATA_UDMA_TIME = udmatime;
234
    ata_powered = true;
236
    ata_powered = true;
235
    ata_set_active();
237
    ata_set_active();
Line 248... Line 250...
248
    while (!(ATA_CONTROL & BIT(1))) yield();
250
    while (!(ATA_CONTROL & BIT(1))) yield();
249
    clockgate_enable(5, false);
251
    clockgate_enable(5, false);
250
    i2c_sendbyte(0, 0xe6, 0x1b, 0);
252
    i2c_sendbyte(0, 0xe6, 0x1b, 0);
251
}
253
}
252
 
254
 
-
 
255
int ata_rw_chunk(uint64_t sector, uint32_t cnt, void* buffer, bool write)
-
 
256
{
-
 
257
    PASS_RC(ata_wait_for_rdy(100000), 2, 0);
-
 
258
    ata_write_cbr(&ATA_PIO_DVR, 0);
-
 
259
    if (ata_lba48)
-
 
260
    {
-
 
261
        ata_write_cbr(&ATA_PIO_SCR, cnt >> 5);
-
 
262
        ata_write_cbr(&ATA_PIO_SCR, (cnt << 3) & 0xff);
-
 
263
        ata_write_cbr(&ATA_PIO_LHR, (sector >> 37) & 0xff);
-
 
264
        ata_write_cbr(&ATA_PIO_LMR, (sector >> 29) & 0xff);
-
 
265
        ata_write_cbr(&ATA_PIO_LLR, (sector >> 21) & 0xff);
-
 
266
        ata_write_cbr(&ATA_PIO_LHR, (sector >> 13) & 0xff);
-
 
267
        ata_write_cbr(&ATA_PIO_LMR, (sector >> 5) & 0xff);
-
 
268
        ata_write_cbr(&ATA_PIO_LLR, (sector << 3) & 0xff);
-
 
269
        ata_write_cbr(&ATA_PIO_DVR, BIT(6));
-
 
270
        if (write) ata_write_cbr(&ATA_PIO_CSD, ata_dma ? 0x35 : 0x39);
-
 
271
        else ata_write_cbr(&ATA_PIO_CSD, ata_dma ? 0x25 : 0x29);
-
 
272
    }
-
 
273
    else
-
 
274
    {
-
 
275
        ata_write_cbr(&ATA_PIO_SCR, (cnt << 3) & 0xff);
-
 
276
        ata_write_cbr(&ATA_PIO_LHR, (sector >> 13) & 0xff);
-
 
277
        ata_write_cbr(&ATA_PIO_LMR, (sector >> 5) & 0xff);
-
 
278
        ata_write_cbr(&ATA_PIO_LLR, (sector << 3) & 0xff);
-
 
279
        ata_write_cbr(&ATA_PIO_DVR, BIT(6) | ((sector >> 21) & 0xf));
-
 
280
        if (write) ata_write_cbr(&ATA_PIO_CSD, ata_dma ? 0xca : 0x30);
-
 
281
        else ata_write_cbr(&ATA_PIO_CSD, ata_dma ? 0xc8 : 0xc4);
-
 
282
    }
-
 
283
    if (ata_dma)
-
 
284
    {
-
 
285
        PASS_RC(ata_wait_for_start_of_transfer(500000), 2, 1);
-
 
286
        if (write)
-
 
287
        {
-
 
288
            ATA_SBUF_START = buffer;
-
 
289
            ATA_SBUF_SIZE = SECTOR_SIZE * cnt;
-
 
290
            ATA_CFG |= BIT(4);
-
 
291
        }
-
 
292
        else
-
 
293
        {
-
 
294
            ATA_TBUF_START = buffer;
-
 
295
            ATA_TBUF_SIZE = SECTOR_SIZE * cnt;
-
 
296
            ATA_CFG &= ~BIT(4);
-
 
297
        }
-
 
298
        ATA_XFR_NUM = SECTOR_SIZE * cnt - 1;
-
 
299
        ATA_CFG |= ata_dma_flags;
-
 
300
        ATA_CFG &= ~(BIT(7) | BIT(8));
-
 
301
        wakeup_wait(&ata_wakeup, TIMEOUT_NONE);
-
 
302
        ATA_IRQ = BITRANGE(0, 4);
-
 
303
        ATA_IRQ_MASK = BIT(0);
-
 
304
        ATA_COMMAND = BIT(0);
-
 
305
        if (wakeup_wait(&ata_wakeup, 500000) == THREAD_TIMEOUT)
-
 
306
        {
-
 
307
            ATA_COMMAND = BIT(1);
-
 
308
            ATA_CFG &= ~(BITRANGE(2, 3) | BIT(12));
-
 
309
            RET_ERR(2);
-
 
310
        }
-
 
311
        ATA_COMMAND = BIT(1);
-
 
312
        ATA_CFG &= ~(BITRANGE(2, 3) | BIT(12));
-
 
313
    }
-
 
314
    else
-
 
315
    {
-
 
316
        cnt *= SECTOR_SIZE / 512;
-
 
317
        while (cnt--)
-
 
318
        {
-
 
319
            int i;
-
 
320
            PASS_RC(ata_wait_for_start_of_transfer(500000), 2, 1);
-
 
321
            if (write)
-
 
322
                for (i = 0; i < 256; i++)
-
 
323
                    ata_write_cbr(&ATA_PIO_DTR, ((uint16_t*)buffer)[i]);
-
 
324
            else
-
 
325
                for (i = 0; i < 256; i++)
-
 
326
                    ((uint16_t*)buffer)[i] = ata_read_cbr(&ATA_PIO_DTR);
-
 
327
            buffer += 512;
-
 
328
        }
-
 
329
    }
-
 
330
    PASS_RC(ata_wait_for_end_of_transfer(100000), 2, 3);
-
 
331
    return 0;
-
 
332
}
-
 
333
 
253
int ata_rw_sectors(uint64_t sector, uint32_t count, void* buffer, bool write)
334
int ata_rw_sectors(uint64_t sector, uint32_t count, void* buffer, bool write)
254
{
335
{
255
#ifdef ATA_HAVE_BBT
336
#ifdef ATA_HAVE_BBT
256
    if (sector + count > ata_virtual_sectors) RET_ERR(0);
337
    if (sector + count > ata_virtual_sectors) RET_ERR(0);
257
    while (count)
338
    while (count)
258
    {
339
    {
-
 
340
        uint32_t offset;
259
        uint32_t l0idx = sector >> 15;
341
        uint32_t l0idx = sector >> 15;
260
        uint32_t l0offs = sector & 0x7fff;
342
        uint32_t l0offs = sector & 0x7fff;
261
        uint32_t cnt = MIN(count, 0x8000 - l0offs);
343
        uint32_t cnt = MIN(count, 0x8000 - l0offs);
262
        uint32_t l0data = ata_bbt[0][l0idx << 1];
344
        uint32_t l0data = ata_bbt[0][l0idx << 1];
263
        uint32_t base = ata_bbt[0][(l0idx << 1) | 1] << 12;
345
        uint32_t base = ata_bbt[0][(l0idx << 1) | 1] << 12;
264
        uint32_t phys;
-
 
265
        if (l0data < 0x8000) phys = sector + l0data + base;
346
        if (l0data < 0x8000) offset = l0data + base;
266
        else
347
        else
267
        {
348
        {
268
            uint32_t l1idx = (sector >> 10) & 0x1f;
349
            uint32_t l1idx = (sector >> 10) & 0x1f;
269
            uint32_t l1offs = sector & 0x3ff;
350
            uint32_t l1offs = sector & 0x3ff;
270
            cnt = MIN(count, 0x400 - l1offs);
351
            cnt = MIN(count, 0x400 - l1offs);
271
            uint32_t l1data = ata_bbt[l0data & 0x7fff][l1idx];
352
            uint32_t l1data = ata_bbt[l0data & 0x7fff][l1idx];
272
            if (l1data < 0x8000) phys = sector + l1data + base;
353
            if (l1data < 0x8000) offset = l1data + base;
273
            else
354
            else
274
            {
355
            {
275
                uint32_t l2idx = (sector >> 5) & 0x1f;
356
                uint32_t l2idx = (sector >> 5) & 0x1f;
276
                uint32_t l2offs = sector & 0x1f;
357
                uint32_t l2offs = sector & 0x1f;
277
                cnt = MIN(count, 0x20 - l2offs);
358
                cnt = MIN(count, 0x20 - l2offs);
278
                uint32_t l2data = ata_bbt[l1data & 0x7fff][l2idx];
359
                uint32_t l2data = ata_bbt[l1data & 0x7fff][l2idx];
279
                if (l2data < 0x8000) phys = sector + l2data + base;
360
                if (l2data < 0x8000) offset = l2data + base;
280
                else
361
                else
281
                {
362
                {
282
                    uint32_t l3idx = sector & 0x1f;
363
                    uint32_t l3idx = sector & 0x1f;
283
                    phys = ata_bbt[l2data & 0x7fff][l3idx];
364
                    uint32_t l3data = ata_bbt[l2data & 0x7fff][l3idx];
284
                    for (cnt = 1; cnt < count && l3idx + cnt < 0x20; cnt++)
365
                    for (cnt = 1; cnt < count && l3idx + cnt < 0x20; cnt++)
285
                        if (ata_bbt[l2data & 0x7fff][l3idx + cnt] != phys)
366
                        if (ata_bbt[l2data & 0x7fff][l3idx + cnt] != l3data)
286
                            break;
367
                            break;
287
                    phys += sector + base;
368
                    offset = l3data + base;
288
                }
369
                }
289
            }
370
            }
290
        }
371
        }
-
 
372
        uint64_t phys = sector + offset;
-
 
373
        if (offset != ata_last_offset && phys - ata_last_phys < 64) ata_soft_reset();
-
 
374
        ata_last_offset = offset;
-
 
375
        ata_last_phys = phys + cnt;
291
        PASS_RC(ata_rw_sectors_internal(phys, cnt, buffer, write), 0, 0);
376
        PASS_RC(ata_rw_sectors_internal(phys, cnt, buffer, write), 0, 0);
292
        buffer += cnt * SECTOR_SIZE;
377
        buffer += cnt * SECTOR_SIZE;
293
        sector += cnt;
378
        sector += cnt;
294
        count -= cnt;
379
        count -= cnt;
295
    }
380
    }
Line 306... Line 391...
306
    else if (ata_dma) invalidate_dcache();
391
    else if (ata_dma) invalidate_dcache();
307
    ATA_COMMAND = BIT(1);
392
    ATA_COMMAND = BIT(1);
308
    while (count)
393
    while (count)
309
    {
394
    {
310
        uint32_t cnt = MIN(ata_lba48 ? 8192 : 32, count);
395
        uint32_t cnt = MIN(ata_lba48 ? 8192 : 32, count);
311
        PASS_RC(ata_wait_for_rdy(100000), 2, 1);
-
 
312
        ata_write_cbr(&ATA_PIO_DVR, 0);
-
 
313
        if (ata_lba48)
-
 
314
        {
-
 
315
            ata_write_cbr(&ATA_PIO_SCR, cnt >> 5);
-
 
316
            ata_write_cbr(&ATA_PIO_SCR, (cnt << 3) & 0xff);
-
 
317
            ata_write_cbr(&ATA_PIO_LHR, (sector >> 37) & 0xff);
-
 
318
            ata_write_cbr(&ATA_PIO_LMR, (sector >> 29) & 0xff);
-
 
319
            ata_write_cbr(&ATA_PIO_LLR, (sector >> 21) & 0xff);
-
 
320
            ata_write_cbr(&ATA_PIO_LHR, (sector >> 13) & 0xff);
-
 
321
            ata_write_cbr(&ATA_PIO_LMR, (sector >> 5) & 0xff);
-
 
322
            ata_write_cbr(&ATA_PIO_LLR, (sector << 3) & 0xff);
-
 
323
            ata_write_cbr(&ATA_PIO_DVR, BIT(6));
-
 
324
            if (write) ata_write_cbr(&ATA_PIO_CSD, ata_dma ? 0x35 : 0x39);
-
 
325
            else ata_write_cbr(&ATA_PIO_CSD, ata_dma ? 0x25 : 0x29);
-
 
326
        }
-
 
327
        else
-
 
328
        {
-
 
329
            ata_write_cbr(&ATA_PIO_SCR, (cnt << 3) & 0xff);
-
 
330
            ata_write_cbr(&ATA_PIO_LHR, (sector >> 13) & 0xff);
-
 
331
            ata_write_cbr(&ATA_PIO_LMR, (sector >> 5) & 0xff);
-
 
332
            ata_write_cbr(&ATA_PIO_LLR, (sector << 3) & 0xff);
-
 
333
            ata_write_cbr(&ATA_PIO_DVR, BIT(6) | ((sector >> 21) & 0xf));
-
 
334
            if (write) ata_write_cbr(&ATA_PIO_CSD, ata_dma ? 0xca : 0x30);
-
 
335
            else ata_write_cbr(&ATA_PIO_CSD, ata_dma ? 0xc8 : 0xc4);
-
 
336
        }
-
 
337
        sector += cnt;
396
        int rc = -1;
338
        count -= cnt;
397
        int tries = 3;
339
        if (ata_dma)
398
        while (tries-- && rc)
340
        {
399
        {
341
            PASS_RC(ata_wait_for_start_of_transfer(1500000), 2, 2);
-
 
342
            if (write)
-
 
343
            {
-
 
344
                ATA_SBUF_START = buffer;
400
            rc = ata_rw_chunk(sector, cnt, buffer, write);
345
                ATA_SBUF_SIZE = SECTOR_SIZE * cnt;
-
 
346
                ATA_CFG |= BIT(4);
401
            if (rc) ata_soft_reset();
347
            }
-
 
348
            else
-
 
349
            {
-
 
350
                ATA_TBUF_START = buffer;
-
 
351
                ATA_TBUF_SIZE = SECTOR_SIZE * cnt;
-
 
352
                ATA_CFG &= ~BIT(4);
-
 
353
            }
-
 
354
            ATA_XFR_NUM = SECTOR_SIZE * cnt - 1;
-
 
355
            ATA_CFG |= ata_dma_flags;
-
 
356
            ATA_CFG &= ~(BIT(7) | BIT(8));
-
 
357
            wakeup_wait(&ata_wakeup, TIMEOUT_NONE);
-
 
358
            ATA_IRQ = BITRANGE(0, 4);
-
 
359
            ATA_IRQ_MASK = BIT(0);
-
 
360
            ATA_COMMAND = BIT(0);
-
 
361
            if (wakeup_wait(&ata_wakeup, 1500000) == THREAD_TIMEOUT)
-
 
362
            {
-
 
363
                ATA_COMMAND = BIT(1);
-
 
364
                ATA_CFG &= ~(BITRANGE(2, 3) | BIT(12));
-
 
365
                RET_ERR(3);
-
 
366
            }
-
 
367
            ATA_COMMAND = BIT(1);
-
 
368
            ATA_CFG &= ~(BITRANGE(2, 3) | BIT(12));
-
 
369
            buffer += SECTOR_SIZE * cnt;
-
 
370
        }
402
        }
371
        else
403
        if (rc)
372
        {
404
        {
373
            cnt *= SECTOR_SIZE / 512;
405
            void* buf = buffer;
374
            while (cnt--)
406
            int sect;
-
 
407
            for (sect = sector; sect < sector + cnt; sect++)
375
            {
408
            {
376
                int i;
409
                rc = -1;
-
 
410
                tries = 3;
377
                PASS_RC(ata_wait_for_start_of_transfer(1500000), 2, 2);
411
                while (tries-- && rc)
378
                if (write)
412
                {
379
                    for (i = 0; i < 256; i++)
413
                    rc = ata_rw_chunk(sect, 1, buf, write);
380
                        ata_write_cbr(&ATA_PIO_DTR, ((uint16_t*)buffer)[i]);
414
                    if (rc) ata_soft_reset();
381
                else
415
                }
382
                    for (i = 0; i < 256; i++)
416
                if (rc) break;
383
                        ((uint16_t*)buffer)[i] = ata_read_cbr(&ATA_PIO_DTR);
-
 
384
                buffer += 512;
417
                buf += SECTOR_SIZE;
385
            }
418
            }
386
        }
419
        }
387
        PASS_RC(ata_wait_for_end_of_transfer(100000), 2, 3);
420
        PASS_RC(rc, 1, 1);
-
 
421
        buffer += SECTOR_SIZE * cnt;
-
 
422
        sector += cnt;
-
 
423
        count -= cnt;
388
    }
424
    }
389
    ata_set_active();
425
    ata_set_active();
390
    return 0;
426
    return 0;
391
}
427
}
392
 
428
 
Line 409... Line 445...
409
    if (!ata_powered) ata_power_up();
445
    if (!ata_powered) ata_power_up();
410
    ata_set_active();
446
    ata_set_active();
411
    ata_write_cbr(&ATA_PIO_DAD, BIT(1) | BIT(2));
447
    ata_write_cbr(&ATA_PIO_DAD, BIT(1) | BIT(2));
412
    sleep(10);
448
    sleep(10);
413
    ata_write_cbr(&ATA_PIO_DAD, 0);
449
    ata_write_cbr(&ATA_PIO_DAD, 0);
414
    PASS_RC_MTX(ata_wait_for_rdy(5000000), 0, 0, &ata_mutex);
450
    PASS_RC_MTX(ata_wait_for_rdy(60000000), 0, 0, &ata_mutex);
415
    ata_set_active();
451
    ata_set_active();
416
    mutex_unlock(&ata_mutex);
452
    mutex_unlock(&ata_mutex);
417
}
453
}
418
 
454
 
419
int ata_read_sectors(IF_MD2(int drive,) unsigned long start, int incount,
455
int ata_read_sectors(IF_MD2(int drive,) unsigned long start, int incount,
420
                     void* inbuf)
456
                     void* inbuf)
421
{
457
{
422
    mutex_lock(&ata_mutex, TIMEOUT_BLOCK);
458
    mutex_lock(&ata_mutex, TIMEOUT_BLOCK);
423
    int tries = 1;
-
 
424
    int rc = -1;
-
 
425
    while (tries-- && rc)
-
 
426
    {
-
 
427
        rc = ata_rw_sectors(start, incount, inbuf, false);
459
    int rc = ata_rw_sectors(start, incount, inbuf, false);
428
        if (rc) ata_soft_reset();
-
 
429
    }
-
 
430
    mutex_unlock(&ata_mutex);
460
    mutex_unlock(&ata_mutex);
431
    return rc;
461
    return rc;
432
}
462
}
433
 
463
 
434
int ata_write_sectors(IF_MD2(int drive,) unsigned long start, int count,
464
int ata_write_sectors(IF_MD2(int drive,) unsigned long start, int count,
435
                      const void* outbuf)
465
                      const void* outbuf)
436
{
466
{
437
    mutex_lock(&ata_mutex, TIMEOUT_BLOCK);
467
    mutex_lock(&ata_mutex, TIMEOUT_BLOCK);
438
    int tries = 1;
-
 
439
    int rc = -1;
-
 
440
    while (tries-- && rc)
-
 
441
    {
-
 
442
        rc = ata_rw_sectors(start, count, (void*)((uint32_t)outbuf), true);
468
    int rc = ata_rw_sectors(start, count, (void*)((uint32_t)outbuf), true);
443
        if (rc) ata_soft_reset();
-
 
444
    }
-
 
445
    mutex_unlock(&ata_mutex);
469
    mutex_unlock(&ata_mutex);
446
    return rc;
470
    return rc;
447
}
471
}
448
 
472
 
449
void ata_spindown(int seconds)
473
void ata_spindown(int seconds)
450
{
474
{
451
    ata_sleep_timeout = seconds * 1000000;
475
    ata_sleep_timeout = seconds * 1000000;
452
}
476
}
453
 
477
 
454
void ata_sleep(void)
478
void ata_sleep(void)
455
{
479
{
456
    mutex_lock(&ata_mutex, TIMEOUT_BLOCK);
480
    mutex_lock(&ata_mutex, TIMEOUT_BLOCK);
457
    ata_power_down();
481
    ata_power_down();
Line 510... Line 534...
510
    if (!memcmp(buf, "emBIbbth", 8))
534
    if (!memcmp(buf, "emBIbbth", 8))
511
    {
535
    {
512
        ata_virtual_sectors = buf[0x1fe];
536
        ata_virtual_sectors = buf[0x1fe];
513
        uint32_t count = buf[0x1ff];
537
        uint32_t count = buf[0x1ff];
514
        if (count > (ATA_BBT_PAGES >> 6))
538
        if (count > (ATA_BBT_PAGES >> 6))
515
            panicf(PANIC_KILLTHREAD, "ATA: BBT too big!\n");
539
            panicf(PANIC_KILLTHREAD, "ATA: BBT too big! (%d pages, limit: %d)\n", count << 6, ATA_BBT_PAGES);
516
        uint32_t i;
540
        uint32_t i;
517
        uint32_t cnt;
541
        uint32_t cnt;
518
        for (i = 0; i < count; i += cnt)
542
        for (i = 0; i < count; i += cnt)
519
        {
543
        {
520
            uint32_t phys = buf[0x200 + i];
544
            uint32_t phys = buf[0x200 + i];