Subversion Repositories freemyipod

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
643 theseven 1
/***************************************************************************
2
 *             __________               __   ___.
3
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
4
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
5
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
6
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
7
 *                     \/            \/     \/    \/            \/
8
 * $Id$
9
 *
10
 * Copyright (C) 2007 Dave Chapman
11
 *
12
 * This program is free software; you can redistribute it and/or
13
 * modify it under the terms of the GNU General Public License
14
 * as published by the Free Software Foundation; either version 2
15
 * of the License, or (at your option) any later version.
16
 *
17
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18
 * KIND, either express or implied.
19
 *
20
 ****************************************************************************/
21
#include "global.h"
22
#include "thread.h"
23
#include "disk.h"
24
#include "storage.h"
25
#include "storage_ata-target.h"
26
#include "timer.h"
27
#include "constants/mmc.h"
28
#include "../ipodnano3g/s5l8702.h"
29
 
30
 
31
#ifndef ATA_RETRIES
32
#define ATA_RETRIES 3
33
#endif
34
 
35
 
36
#define CEATA_POWERUP_TIMEOUT 30000000
37
#define CEATA_COMMAND_TIMEOUT 1000000
38
#define CEATA_DAT_NONBUSY_TIMEOUT 5000000
39
#define CEATA_MMC_RCA 1
40
 
41
 
42
/** static, private data **/ 
738 theseven 43
static uint8_t ceata_taskfile[16] CACHEALIGN_ATTR;
44
uint16_t ata_identify_data[0x100] CACHEALIGN_ATTR;
643 theseven 45
bool ceata;
46
bool ata_lba48;
47
bool ata_dma;
48
uint64_t ata_total_sectors;
49
struct mutex ata_mutex;
50
static struct wakeup ata_wakeup;
51
static uint32_t ata_dma_flags;
52
static long ata_last_activity_value = -1;
53
static long ata_sleep_timeout = 20000000;
54
static struct scheduler_thread ata_thread_handle;
55
static uint32_t ata_stack[0x80] STACK_ATTR;
56
static bool ata_powered;
57
static int ata_retries = ATA_RETRIES;
58
static bool ata_error_srst = true;
59
static struct wakeup mmc_wakeup;
60
static struct wakeup mmc_comp_wakeup;
61
 
62
 
63
#ifdef ATA_HAVE_BBT
64
#include "panic.h"
65
uint16_t (*ata_bbt)[0x20];
66
uint64_t ata_virtual_sectors;
67
uint32_t ata_last_offset;
68
uint64_t ata_last_phys;
69
 
70
int ata_bbt_read_sectors(uint32_t sector, uint32_t count, void* buffer)
71
{
72
    if (ata_last_phys != sector - 1 && ata_last_phys > sector - 64) ata_soft_reset();
73
    int rc = ata_rw_sectors_internal(sector, count, buffer, false);
74
    if (rc) rc = ata_rw_sectors_internal(sector, count, buffer, false);
75
    ata_last_phys = sector + count - 1;
76
    ata_last_offset = 0;
77
    if (IS_ERR(rc))
78
        cprintf(CONSOLE_BOOT, "ATA: Error %08X while reading BBT (sector %d, count %d)\n",
79
                rc, sector, count);
80
    return rc;
81
}
82
#endif
83
 
84
static struct ata_target_driverinfo drvinfo =
85
{
86
    .set_retries = ata_set_retries,
87
    .srst_after_error = ata_srst_after_error,
88
#ifdef ATA_HAVE_BBT
89
    .bbt_translate = ata_bbt_translate,
90
    .bbt_reload = ata_bbt_reload,
91
    .bbt_disable = ata_bbt_disable
92
#endif
93
};
94
 
95
 
96
void ata_set_retries(int retries)
97
{
98
    ata_retries = retries;
99
}
100
 
101
void ata_srst_after_error(bool enable)
102
{
103
    ata_error_srst = enable;
104
}
105
 
106
static uint16_t ata_read_cbr(uint32_t volatile* reg)
107
{
108
    while (!(ATA_PIO_READY & 2)) yield();
109
    volatile uint32_t dummy = *reg;
110
    while (!(ATA_PIO_READY & 1)) yield();
111
    return ATA_PIO_RDATA;
112
}
113
 
114
static void ata_write_cbr(uint32_t volatile* reg, uint16_t data)
115
{
116
    while (!(ATA_PIO_READY & 2)) yield();
117
    *reg = data;
118
}
119
 
120
static int ata_wait_for_not_bsy(long timeout)
121
{
122
    long startusec = USEC_TIMER;
123
    while (true)
124
    {
125
        uint8_t csd = ata_read_cbr(&ATA_PIO_CSD);
126
        if (!(csd & BIT(7))) return 0;
127
        if (TIMEOUT_EXPIRED(startusec, timeout)) RET_ERR(0);
128
    }
129
}
130
 
131
static int ata_wait_for_rdy(long timeout)
132
{
133
    long startusec = USEC_TIMER;
134
    PASS_RC(ata_wait_for_not_bsy(timeout), 1, 0);
135
    while (true)
136
    {
137
        uint8_t dad = ata_read_cbr(&ATA_PIO_DAD);
138
        if (dad & BIT(6)) return 0;
139
        if (TIMEOUT_EXPIRED(startusec, timeout)) RET_ERR(1);
140
    }
141
}
142
 
143
static int ata_wait_for_start_of_transfer(long timeout)
144
{
145
    long startusec = USEC_TIMER;
146
    PASS_RC(ata_wait_for_not_bsy(timeout), 2, 0);
147
    while (true)
148
    {
149
        uint8_t dad = ata_read_cbr(&ATA_PIO_DAD);
150
        if (dad & BIT(0)) RET_ERR(1);
151
        if ((dad & (BIT(7) | BIT(3))) == BIT(3)) return 0;
152
        if (TIMEOUT_EXPIRED(startusec, timeout)) RET_ERR(2);
153
    }
154
}
155
 
156
static int ata_wait_for_end_of_transfer(long timeout)
157
{
158
    PASS_RC(ata_wait_for_not_bsy(timeout), 2, 0);
159
    uint8_t dad = ata_read_cbr(&ATA_PIO_DAD);
160
    if (dad & BIT(0)) RET_ERR(1);
161
    if ((dad & (BIT(3) | BITRANGE(5, 7))) == BIT(6)) return 0;
162
    RET_ERR(2);
163
}    
164
 
165
int mmc_dsta_check_command_success(bool disable_crc)
166
{
167
    int rc = 0;
168
    uint32_t dsta = SDCI_DSTA;
169
    if (dsta & SDCI_DSTA_RESTOUTE) rc |= 1; 
170
    if (dsta & SDCI_DSTA_RESENDE) rc |= 2;
171
    if (dsta & SDCI_DSTA_RESINDE) rc |= 4;
172
    if (!disable_crc)
173
        if (dsta & SDCI_DSTA_RESCRCE)
174
            rc |= 8;
175
    if (rc) RET_ERR(rc);
176
    return 0;
177
}
178
 
179
bool mmc_send_command(uint32_t cmd, uint32_t arg, uint32_t* result, int timeout)
180
{
181
    long starttime = USEC_TIMER;
182
    while ((SDCI_STATE & SDCI_STATE_CMD_STATE_MASK) != SDCI_STATE_CMD_STATE_CMD_IDLE)
183
    {
184
        if (TIMEOUT_EXPIRED(starttime, timeout)) RET_ERR(0);
185
        yield();
186
    }
187
    SDCI_STAC = SDCI_STAC_CLR_CMDEND | SDCI_STAC_CLR_BIT_3
188
              | SDCI_STAC_CLR_RESEND | SDCI_STAC_CLR_DATEND
189
              | SDCI_STAC_CLR_DAT_CRCEND | SDCI_STAC_CLR_CRC_STAEND
190
              | SDCI_STAC_CLR_RESTOUTE | SDCI_STAC_CLR_RESENDE
191
              | SDCI_STAC_CLR_RESINDE | SDCI_STAC_CLR_RESCRCE
192
              | SDCI_STAC_CLR_WR_DATCRCE | SDCI_STAC_CLR_RD_DATCRCE
193
              | SDCI_STAC_CLR_RD_DATENDE0 | SDCI_STAC_CLR_RD_DATENDE1
194
              | SDCI_STAC_CLR_RD_DATENDE2 | SDCI_STAC_CLR_RD_DATENDE3
195
              | SDCI_STAC_CLR_RD_DATENDE4 | SDCI_STAC_CLR_RD_DATENDE5
196
              | SDCI_STAC_CLR_RD_DATENDE6 | SDCI_STAC_CLR_RD_DATENDE7;
197
    SDCI_ARGU = arg;
198
    SDCI_CMD = cmd;
199
    if (!(SDCI_DSTA & SDCI_DSTA_CMDRDY)) RET_ERR(1);
200
    SDCI_CMD = cmd | SDCI_CMD_CMDSTR;
201
    sleep(1000);
202
    while (!(SDCI_DSTA & SDCI_DSTA_CMDEND))
203
    {
204
        if (TIMEOUT_EXPIRED(starttime, timeout)) RET_ERR(2);
205
        yield();
206
    }
207
    if ((cmd & SDCI_CMD_RES_TYPE_MASK) != SDCI_CMD_RES_TYPE_NONE)
208
    {
209
        while (!(SDCI_DSTA & SDCI_DSTA_RESEND))
210
        {
211
            if (TIMEOUT_EXPIRED(starttime, timeout)) RET_ERR(3);
212
            yield();
213
        }
214
        if (cmd & SDCI_CMD_RES_BUSY)
215
            while (SDCI_DSTA & SDCI_DSTA_DAT_BUSY)
216
            {
217
                if (TIMEOUT_EXPIRED(starttime, CEATA_DAT_NONBUSY_TIMEOUT)) RET_ERR(4);
218
                yield();
219
            }
220
    }
221
    bool nocrc = (cmd & SDCI_CMD_RES_SIZE_MASK) == SDCI_CMD_RES_SIZE_136;
222
    PASS_RC(mmc_dsta_check_command_success(nocrc), 3, 5);
223
    if (result) *result = SDCI_RESP0;
224
    return 0;
225
}
226
 
227
int mmc_get_card_status(uint32_t* result)
228
{
229
    return mmc_send_command(SDCI_CMD_CMD_NUM(MMC_CMD_SEND_STATUS)
230
                          | SDCI_CMD_CMD_TYPE_AC | SDCI_CMD_RES_TYPE_R1
231
                          | SDCI_CMD_RES_SIZE_48 | SDCI_CMD_NCR_NID_NCR,
232
                            MMC_CMD_SEND_STATUS_RCA(CEATA_MMC_RCA), result, CEATA_COMMAND_TIMEOUT);
233
}
234
 
235
int mmc_init()
236
{
237
    sleep(100000);
238
    PASS_RC(mmc_send_command(SDCI_CMD_CMD_NUM(MMC_CMD_GO_IDLE_STATE)
239
                           | SDCI_CMD_CMD_TYPE_BC | SDCI_CMD_RES_TYPE_NONE
240
                           | SDCI_CMD_RES_SIZE_48 | SDCI_CMD_NCR_NID_NID,
241
                             0, NULL, CEATA_COMMAND_TIMEOUT), 3, 0);
242
    long startusec = USEC_TIMER;
243
    uint32_t result;
244
    do
245
    {
246
        if (TIMEOUT_EXPIRED(startusec, CEATA_POWERUP_TIMEOUT)) RET_ERR(1);
247
        sleep(1000);
248
        PASS_RC(mmc_send_command(SDCI_CMD_CMD_NUM(MMC_CMD_SEND_OP_COND)
249
                               | SDCI_CMD_CMD_TYPE_BCR | SDCI_CMD_RES_TYPE_R3
250
                               | SDCI_CMD_RES_SIZE_48 | SDCI_CMD_NCR_NID_NID,
251
                                 MMC_CMD_SEND_OP_COND_OCR(MMC_OCR_270_360),
252
                                 NULL, CEATA_COMMAND_TIMEOUT), 3, 2);
253
        result = SDCI_RESP0;
254
    }
255
    while (!(result & MMC_OCR_POWER_UP_DONE));
256
    PASS_RC(mmc_send_command(SDCI_CMD_CMD_NUM(MMC_CMD_ALL_SEND_CID)
257
                           | SDCI_CMD_CMD_TYPE_BCR | SDCI_CMD_RES_TYPE_R2
258
                           | SDCI_CMD_RES_SIZE_136 | SDCI_CMD_NCR_NID_NID,
259
                             0, NULL, CEATA_COMMAND_TIMEOUT), 3, 3);
260
    PASS_RC(mmc_send_command(SDCI_CMD_CMD_NUM(MMC_CMD_SET_RELATIVE_ADDR)
261
                           | SDCI_CMD_CMD_TYPE_BCR | SDCI_CMD_RES_TYPE_R1
262
                           | SDCI_CMD_RES_SIZE_48 | SDCI_CMD_NCR_NID_NCR,
263
                             MMC_CMD_SET_RELATIVE_ADDR_RCA(CEATA_MMC_RCA),
264
                             NULL, CEATA_COMMAND_TIMEOUT), 3, 4);
265
    PASS_RC(mmc_send_command(SDCI_CMD_CMD_NUM(MMC_CMD_SELECT_CARD)
266
                           | SDCI_CMD_CMD_TYPE_AC | SDCI_CMD_RES_TYPE_R1
267
                           | SDCI_CMD_RES_SIZE_48 | SDCI_CMD_NCR_NID_NCR,
268
                             MMC_CMD_SELECT_CARD_RCA(CEATA_MMC_RCA),
269
                             NULL, CEATA_COMMAND_TIMEOUT), 3, 5);
270
    PASS_RC(mmc_get_card_status(&result), 3, 6);
271
    if ((result & MMC_STATUS_CURRENT_STATE_MASK) != MMC_STATUS_CURRENT_STATE_TRAN) RET_ERR(7);
272
    return 0;
273
}
274
 
275
int mmc_fastio_write(uint32_t addr, uint32_t data)
276
{
277
    return mmc_send_command(SDCI_CMD_CMD_NUM(MMC_CMD_FAST_IO)
278
                          | SDCI_CMD_CMD_TYPE_AC | SDCI_CMD_RES_TYPE_R4
279
                          | SDCI_CMD_RES_SIZE_48 | SDCI_CMD_NCR_NID_NCR,
280
                            MMC_CMD_FAST_IO_RCA(CEATA_MMC_RCA) | MMC_CMD_FAST_IO_DIRECTION_WRITE
281
                          | MMC_CMD_FAST_IO_ADDRESS(addr) | MMC_CMD_FAST_IO_DATA(data),
282
                            NULL, CEATA_COMMAND_TIMEOUT);
283
}
284
 
285
int mmc_fastio_read(uint32_t addr, uint32_t* data)
286
{
287
    return mmc_send_command(SDCI_CMD_CMD_NUM(MMC_CMD_FAST_IO)
288
                          | SDCI_CMD_CMD_TYPE_AC | SDCI_CMD_RES_TYPE_R4
289
                          | SDCI_CMD_RES_SIZE_48 | SDCI_CMD_NCR_NID_NCR,
290
                            MMC_CMD_FAST_IO_RCA(CEATA_MMC_RCA) | MMC_CMD_FAST_IO_DIRECTION_READ
291
                          | MMC_CMD_FAST_IO_ADDRESS(addr), data, CEATA_COMMAND_TIMEOUT);
292
}
293
 
294
int ceata_soft_reset()
295
{
296
    PASS_RC(mmc_fastio_write(6, 4), 2, 0);
297
    sleep(1000);
298
    PASS_RC(mmc_fastio_write(6, 0), 2, 1);
299
    sleep(10000);
300
    long startusec = USEC_TIMER;
301
    uint32_t status;
302
    do
303
    {
304
        PASS_RC(mmc_fastio_read(0xf, &status), 2, 2);
305
        if (TIMEOUT_EXPIRED(startusec, CEATA_POWERUP_TIMEOUT)) RET_ERR(3);
306
        sleep(1000);
307
    }
308
    while (status & 0x80);
309
    return 0;
310
}
311
 
312
int mmc_dsta_check_data_success()
313
{
314
    int rc = 0;
315
    uint32_t dsta = SDCI_DSTA;
316
    if (dsta & (SDCI_DSTA_WR_DATCRCE | SDCI_DSTA_RD_DATCRCE))
317
    {
318
        if (dsta & SDCI_DSTA_WR_DATCRCE) rc |= 1;
319
        if (dsta & SDCI_DSTA_RD_DATCRCE) rc |= 2;
320
        if ((dsta & SDCI_DSTA_WR_CRC_STATUS_MASK) == SDCI_DSTA_WR_CRC_STATUS_TXERR) rc |= 4;
321
        else if ((dsta & SDCI_DSTA_WR_CRC_STATUS_MASK) == SDCI_DSTA_WR_CRC_STATUS_CARDERR) rc |= 8;
322
    }
323
    if (dsta & (SDCI_DSTA_RD_DATENDE0 | SDCI_DSTA_RD_DATENDE1 | SDCI_DSTA_RD_DATENDE2
324
              | SDCI_DSTA_RD_DATENDE3 | SDCI_DSTA_RD_DATENDE4 | SDCI_DSTA_RD_DATENDE5
325
              | SDCI_DSTA_RD_DATENDE6 | SDCI_DSTA_RD_DATENDE7))
326
        rc |= 16;
327
    if (rc) RET_ERR(rc);
328
    return 0;
329
}
330
 
331
void mmc_discard_irq()
332
{
333
    SDCI_IRQ = SDCI_IRQ_DAT_DONE_INT | SDCI_IRQ_MASK_MASK_IOCARD_IRQ_INT
334
             | SDCI_IRQ_MASK_MASK_READ_WAIT_INT;
335
    wakeup_wait(&mmc_wakeup, TIMEOUT_NONE);
336
}
337
 
338
int ceata_read_multiple_register(uint32_t addr, void* dest, uint32_t size)
339
{
340
    if (size > 0x10) RET_ERR(0);
341
    mmc_discard_irq();
342
    SDCI_DMASIZE = size;
343
    SDCI_DMACOUNT = 1;
344
    SDCI_DMAADDR = dest;
345
    SDCI_DCTRL = SDCI_DCTRL_TXFIFORST | SDCI_DCTRL_RXFIFORST;
346
    invalidate_dcache();
347
    PASS_RC(mmc_send_command(SDCI_CMD_CMD_NUM(MMC_CMD_CEATA_RW_MULTIPLE_REG)
348
                           | SDCI_CMD_CMD_TYPE_ADTC | SDCI_CMD_RES_TYPE_R1
349
                           | SDCI_CMD_RES_SIZE_48 | SDCI_CMD_NCR_NID_NCR,
350
                             MMC_CMD_CEATA_RW_MULTIPLE_REG_DIRECTION_READ
351
                           | MMC_CMD_CEATA_RW_MULTIPLE_REG_ADDRESS(addr & 0xfc)
352
                           | MMC_CMD_CEATA_RW_MULTIPLE_REG_COUNT(size & 0xfc),
353
                             NULL, CEATA_COMMAND_TIMEOUT), 2, 1);
354
    if (wakeup_wait(&mmc_wakeup, CEATA_COMMAND_TIMEOUT) == THREAD_TIMEOUT) RET_ERR(2);
355
    PASS_RC(mmc_dsta_check_data_success(), 2, 3);
356
    return 0;
357
}
358
 
359
int ceata_write_multiple_register(uint32_t addr, void* dest, uint32_t size)
360
{
361
    int i;
362
    if (size > 0x10) RET_ERR(0);
363
    mmc_discard_irq();
364
    SDCI_DMASIZE = size;
365
    SDCI_DMACOUNT = 0;
366
    SDCI_DCTRL = SDCI_DCTRL_TXFIFORST | SDCI_DCTRL_RXFIFORST;
367
    PASS_RC(mmc_send_command(SDCI_CMD_CMD_NUM(MMC_CMD_CEATA_RW_MULTIPLE_REG)
368
                           | SDCI_CMD_CMD_TYPE_ADTC | SDCI_CMD_CMD_RD_WR
369
                           | SDCI_CMD_RES_BUSY | SDCI_CMD_RES_TYPE_R1
370
                           | SDCI_CMD_RES_SIZE_48 | SDCI_CMD_NCR_NID_NCR,
371
                             MMC_CMD_CEATA_RW_MULTIPLE_REG_DIRECTION_WRITE
372
                           | MMC_CMD_CEATA_RW_MULTIPLE_REG_ADDRESS(addr & 0xfc)
373
                           | MMC_CMD_CEATA_RW_MULTIPLE_REG_COUNT(size & 0xfc),
374
                             NULL, CEATA_COMMAND_TIMEOUT), 3, 1);
375
    SDCI_DCTRL = SDCI_DCTRL_TRCONT_TX;
376
    for (i = 0; i < size / 4; i++) SDCI_DATA = ((uint32_t*)dest)[i];
377
    long startusec = USEC_TIMER;
378
    if (wakeup_wait(&mmc_wakeup, CEATA_COMMAND_TIMEOUT) == THREAD_TIMEOUT) RET_ERR(2);
379
    while ((SDCI_STATE & SDCI_STATE_DAT_STATE_MASK) != SDCI_STATE_DAT_STATE_IDLE)
380
    {
381
        if (TIMEOUT_EXPIRED(startusec, CEATA_COMMAND_TIMEOUT)) RET_ERR(3);
382
        yield();
383
    }
384
    PASS_RC(mmc_dsta_check_data_success(), 3, 4);
385
    return 0;
386
}
387
 
388
int ceata_init(int buswidth)
389
{
390
    uint32_t result;
391
    PASS_RC(mmc_send_command(SDCI_CMD_CMD_NUM(MMC_CMD_SWITCH) | SDCI_CMD_RES_BUSY
392
                           | SDCI_CMD_CMD_TYPE_AC | SDCI_CMD_RES_TYPE_R1 
393
                           | SDCI_CMD_RES_SIZE_48 | SDCI_CMD_NCR_NID_NCR,
394
                             MMC_CMD_SWITCH_ACCESS_WRITE_BYTE
395
                           | MMC_CMD_SWITCH_INDEX(MMC_CMD_SWITCH_FIELD_HS_TIMING)
396
                           | MMC_CMD_SWITCH_VALUE(MMC_CMD_SWITCH_FIELD_HS_TIMING_HIGH_SPEED),
397
                             &result, CEATA_COMMAND_TIMEOUT), 3, 0);
398
    if (result & MMC_STATUS_SWITCH_ERROR) RET_ERR(1);
399
    if (buswidth > 1)
400
    {
401
        int setting;
402
        if (buswidth == 4) setting = MMC_CMD_SWITCH_FIELD_BUS_WIDTH_4BIT;
403
        else if (buswidth == 8) setting = MMC_CMD_SWITCH_FIELD_BUS_WIDTH_8BIT;
404
        else setting = MMC_CMD_SWITCH_FIELD_BUS_WIDTH_1BIT;
405
        PASS_RC(mmc_send_command(SDCI_CMD_CMD_NUM(MMC_CMD_SWITCH) | SDCI_CMD_RES_BUSY
406
                               | SDCI_CMD_CMD_TYPE_AC | SDCI_CMD_RES_TYPE_R1
407
                               | SDCI_CMD_RES_SIZE_48 | SDCI_CMD_NCR_NID_NCR,
408
                                 MMC_CMD_SWITCH_ACCESS_WRITE_BYTE
409
                               | MMC_CMD_SWITCH_INDEX(MMC_CMD_SWITCH_FIELD_BUS_WIDTH)
410
                               | MMC_CMD_SWITCH_VALUE(setting),
411
                                 &result, CEATA_COMMAND_TIMEOUT), 3, 2);
412
        if (result & MMC_STATUS_SWITCH_ERROR) RET_ERR(3);
413
        if (buswidth == 4)
414
            SDCI_CTRL = (SDCI_CTRL & ~SDCI_CTRL_BUS_WIDTH_MASK) | SDCI_CTRL_BUS_WIDTH_4BIT;
415
        else if (buswidth == 8)
416
            SDCI_CTRL = (SDCI_CTRL & ~SDCI_CTRL_BUS_WIDTH_MASK) | SDCI_CTRL_BUS_WIDTH_8BIT;
417
    }
418
    PASS_RC(ceata_soft_reset(), 3, 4);
419
    PASS_RC(ceata_read_multiple_register(0, ceata_taskfile, 0x10), 3, 5);
420
    if (ceata_taskfile[0xc] != 0xce || ceata_taskfile[0xd] != 0xaa) RET_ERR(6);
421
    PASS_RC(mmc_fastio_write(6, 0), 3, 7);
422
    return 0;
423
}
424
 
425
int ceata_check_error()
426
{
427
    uint32_t status, error;
428
    PASS_RC(mmc_fastio_read(0xf, &status), 2, 0);
429
    if (status & 1)
430
    {
431
        PASS_RC(mmc_fastio_read(0x9, &error), 2, 1);
432
        RET_ERR((error << 2) | 2);
433
    }
434
    return 0;
435
}
436
 
437
int ceata_wait_idle()
438
{
439
    long startusec = USEC_TIMER;
440
    while (true)
441
    {
442
        uint32_t status;
443
        PASS_RC(mmc_fastio_read(0xf, &status), 1, 0);
444
        if (!(status & 0x88)) return 0;
445
        if (TIMEOUT_EXPIRED(startusec, CEATA_DAT_NONBUSY_TIMEOUT)) RET_ERR(1);
446
        sleep(50000);
447
    }
448
}
449
 
450
int ceata_cancel_command()
451
{
452
    *((uint32_t volatile*)0x3cf00200) = 0x9000e;
453
    sleep(1);
454
    *((uint32_t volatile*)0x3cf00200) = 0x9000f;
455
    sleep(1);
456
    *((uint32_t volatile*)0x3cf00200) = 0x90003;
457
    sleep(1);
458
    PASS_RC(mmc_send_command(SDCI_CMD_CMD_NUM(MMC_CMD_STOP_TRANSMISSION)
459
                           | SDCI_CMD_CMD_TYPE_AC | SDCI_CMD_RES_TYPE_R1 | SDCI_CMD_RES_BUSY
460
                           | SDCI_CMD_RES_SIZE_48 | SDCI_CMD_NCR_NID_NCR,
461
                             0, NULL, CEATA_COMMAND_TIMEOUT), 1, 0);
462
    PASS_RC(ceata_wait_idle(), 1, 1);
463
    return 0;
464
}
465
 
466
int ceata_rw_multiple_block(bool write, void* buf, uint32_t count, long timeout)
467
{
468
    mmc_discard_irq();
469
    uint32_t responsetype;
470
    uint32_t cmdtype;
471
    uint32_t direction;
472
    if (write)
473
    {
474
        cmdtype = SDCI_CMD_CMD_TYPE_ADTC | SDCI_CMD_CMD_RD_WR;
475
        responsetype = SDCI_CMD_RES_TYPE_R1 | SDCI_CMD_RES_BUSY;
476
        direction = MMC_CMD_CEATA_RW_MULTIPLE_BLOCK_DIRECTION_WRITE;
477
    }
478
    else
479
    {
480
        cmdtype = SDCI_CMD_CMD_TYPE_ADTC;
481
        responsetype = SDCI_CMD_RES_TYPE_R1;
482
        direction = MMC_CMD_CEATA_RW_MULTIPLE_BLOCK_DIRECTION_READ;
483
    }
484
    SDCI_DMASIZE = 0x200;
485
    SDCI_DMAADDR = buf;
486
    SDCI_DMACOUNT = count;
487
    SDCI_DCTRL = SDCI_DCTRL_TXFIFORST | SDCI_DCTRL_RXFIFORST;
488
    invalidate_dcache();
489
    PASS_RC(mmc_send_command(SDCI_CMD_CMD_NUM(MMC_CMD_CEATA_RW_MULTIPLE_BLOCK)
490
                           | SDCI_CMD_CMD_TYPE_ADTC | cmdtype | responsetype
491
                           | SDCI_CMD_RES_SIZE_48 | SDCI_CMD_NCR_NID_NCR,
492
                             direction | MMC_CMD_CEATA_RW_MULTIPLE_BLOCK_COUNT(count),
493
                             NULL, CEATA_COMMAND_TIMEOUT), 4, 0);
494
    if (write) SDCI_DCTRL = SDCI_DCTRL_TRCONT_TX;
495
    if (wakeup_wait(&mmc_wakeup, timeout) == THREAD_TIMEOUT)
496
    {
497
        PASS_RC(ceata_cancel_command(), 4, 1);
498
        RET_ERR(2);
499
    }
500
    PASS_RC(mmc_dsta_check_data_success(), 4, 3);
501
    if (wakeup_wait(&mmc_comp_wakeup, timeout) == THREAD_TIMEOUT)
502
    {
503
        PASS_RC(ceata_cancel_command(), 4, 4);
504
        RET_ERR(4);
505
    }
506
    PASS_RC(ceata_check_error(), 4, 5);
507
    return 0;
508
}
509
 
510
int ata_identify(uint16_t* buf)
511
{
512
    int i;
513
    if (ceata)
514
    {
515
        memset(ceata_taskfile, 0, 16);
516
        ceata_taskfile[0xf] = 0xec;
517
        PASS_RC(ceata_wait_idle(), 2, 0);
518
        PASS_RC(ceata_write_multiple_register(0, ceata_taskfile, 16), 2, 1);
519
        PASS_RC(ceata_rw_multiple_block(false, buf, 1, CEATA_COMMAND_TIMEOUT), 2, 2);
520
    }
521
    else
522
    {
523
        PASS_RC(ata_wait_for_not_bsy(10000000), 1, 0);
524
        ata_write_cbr(&ATA_PIO_DVR, 0);
525
        ata_write_cbr(&ATA_PIO_CSD, 0xec);
526
        PASS_RC(ata_wait_for_start_of_transfer(10000000), 1, 1);
527
        for (i = 0; i < 0x100; i++)
528
        {
529
            uint16_t word = ata_read_cbr(&ATA_PIO_DTR);
530
            buf[i] = (word >> 8) | (word << 8);
531
        }
532
    }
533
    return 0;
534
}
535
 
536
void ata_set_active(void)
537
{
538
    ata_last_activity_value = USEC_TIMER;
539
}
540
 
541
bool ata_disk_is_active(void)
542
{
543
    return ata_powered;
544
}
545
 
546
int ata_set_feature(uint32_t feature, uint32_t param)
547
{
548
    PASS_RC(ata_wait_for_rdy(500000), 1, 0);
549
    ata_write_cbr(&ATA_PIO_DVR, 0);
550
    ata_write_cbr(&ATA_PIO_FED, 3);
551
    ata_write_cbr(&ATA_PIO_SCR, param);
552
    ata_write_cbr(&ATA_PIO_CSD, feature);
553
    PASS_RC(ata_wait_for_rdy(500000), 1, 1);
554
    return 0;
555
}
556
 
557
int ata_power_up()
558
{
559
    ata_set_active();
560
    if (ata_powered) return 0;
561
    i2c_sendbyte(0, 0xe6, 0x1b, 1);
562
    if (ceata)
563
    {
564
        clockgate_enable(9, true);
565
        SDCI_RESET = 0xa5;
566
        sleep(1000);
567
        *((uint32_t volatile*)0x3cf00380) = 0;
568
        *((uint32_t volatile*)0x3cf0010c) = 0xff;
569
        SDCI_CTRL = SDCI_CTRL_SDCIEN | SDCI_CTRL_CLK_SEL_SDCLK
570
                  | SDCI_CTRL_BIT_8 | SDCI_CTRL_BIT_14;
571
        SDCI_CDIV = SDCI_CDIV_CLKDIV(260);
572
        *((uint32_t volatile*)0x3cf00200) = 0xb000f;
573
        SDCI_IRQ_MASK = SDCI_IRQ_MASK_MASK_DAT_DONE_INT | SDCI_IRQ_MASK_MASK_IOCARD_IRQ_INT;
574
        PASS_RC(mmc_init(), 2, 0);
575
        SDCI_CDIV = SDCI_CDIV_CLKDIV(4);
576
        sleep(10000);
577
        PASS_RC(ceata_init(8), 2, 1);
578
        PASS_RC(ata_identify(ata_identify_data), 2, 2);
579
    }
580
    else
581
    {
582
        clockgate_enable(5, true);
583
        ATA_CFG = BIT(0);
584
        sleep(1000);
585
        ATA_CFG = 0;
586
        sleep(6000);
587
        ATA_SWRST = BIT(0);
588
        sleep(500);
589
        ATA_SWRST = 0;
590
        sleep(90000);
591
        ATA_CONTROL = BIT(0);
592
        sleep(200000);
593
        ATA_PIO_TIME = 0x191f7;
594
        ATA_PIO_LHR = 0;
595
        while (!(ATA_PIO_READY & BIT(1))) sleep(100);
596
        PASS_RC(ata_identify(ata_identify_data), 2, 0);
597
        uint32_t piotime = 0x11f3;
598
        uint32_t mdmatime = 0x1c175;
599
        uint32_t udmatime = 0x5071152;
600
        uint32_t param = 0;
601
        ata_dma_flags = 0;
602
        ata_lba48 = ata_identify_data[83] & BIT(10) ? true : false;
603
        if (ata_identify_data[53] & BIT(1))
604
        {
605
            if (ata_identify_data[64] & BIT(1)) piotime = 0x2072;
606
            else if (ata_identify_data[64] & BIT(0)) piotime = 0x7083;
607
        }
608
        if (ata_identify_data[63] & BIT(2))
609
        {
610
            mdmatime = 0x5072;
611
            param = 0x22;
612
        }
613
        else if (ata_identify_data[63] & BIT(1))
614
        {
615
            mdmatime = 0x7083;
616
            param = 0x21;
617
        }
618
        if (ata_identify_data[63] & BITRANGE(0, 2))
619
        {
620
            ata_dma_flags = BIT(3) | BIT(10);
621
            param |= 0x20;
622
        }
623
        if (ata_identify_data[53] & BIT(2))
624
        {
625
            if (ata_identify_data[88] & BIT(4))
626
            {
627
                udmatime = 0x2010a52;
628
                param = 0x44;
629
            }
630
            else if (ata_identify_data[88] & BIT(3))
631
            {
632
                udmatime = 0x2020a52;
633
                param = 0x43;
634
            }
635
            else if (ata_identify_data[88] & BIT(2))
636
            {
637
                udmatime = 0x3030a52;
638
                param = 0x42;
639
            }
640
            else if (ata_identify_data[88] & BIT(1))
641
            {
642
                udmatime = 0x3050a52;
643
                param = 0x41;
644
            }
645
            if (ata_identify_data[88] & BITRANGE(0, 4))
646
            {
647
                ata_dma_flags = BIT(2) | BIT(3) | BIT(9) | BIT(10);
648
                param |= 0x40;
649
            }
650
        }
651
        ata_dma = param ? true : false;
652
        PASS_RC(ata_set_feature(0xef, param), 2, 1);
653
        if (ata_identify_data[82] & BIT(5)) PASS_RC(ata_set_feature(0x02, 0), 2, 2);
654
        if (ata_identify_data[82] & BIT(6)) PASS_RC(ata_set_feature(0x55, 0), 2, 3);
655
        ATA_PIO_TIME = piotime;
656
        ATA_MDMA_TIME = mdmatime;
657
        ATA_UDMA_TIME = udmatime;
658
    }
659
    if (ata_lba48)
660
        ata_total_sectors = ata_identify_data[100]
661
                            | (((uint64_t)ata_identify_data[101]) << 16)
662
                            | (((uint64_t)ata_identify_data[102]) << 32)
663
                            | (((uint64_t)ata_identify_data[103]) << 48);
664
    else ata_total_sectors = ata_identify_data[60] | (((uint32_t)ata_identify_data[61]) << 16);
665
    ata_total_sectors >>= 3;
666
    ata_powered = true;
667
    ata_set_active();
668
    return 0;
669
}
670
 
671
void ata_power_down()
672
{
673
    if (!ata_powered) return;
674
    ata_powered = false;
675
    if (ceata)
676
    {
677
        memset(ceata_taskfile, 0, 16);
678
        ceata_taskfile[0xf] = 0xe0;
679
        ceata_wait_idle();
680
        ceata_write_multiple_register(0, ceata_taskfile, 16);
681
        sleep(1000000);
682
        clockgate_enable(9, false);
683
    }
684
    else
685
    {
686
        ata_wait_for_rdy(1000000);
687
        ata_write_cbr(&ATA_PIO_DVR, 0);
688
        ata_write_cbr(&ATA_PIO_CSD, 0xe0);
689
        ata_wait_for_rdy(1000000);
690
        sleep(30000);
691
        ATA_CONTROL = 0;
692
        while (!(ATA_CONTROL & BIT(1))) yield();
693
        clockgate_enable(5, false);
694
    }
695
    i2c_sendbyte(0, 0xe6, 0x1b, 0);
696
}
697
 
698
int ata_rw_chunk(uint64_t sector, uint32_t cnt, void* buffer, bool write)
699
{
700
    if (ceata)
701
    {
702
        memset(ceata_taskfile, 0, 16);
703
        ceata_taskfile[0x2] = cnt >> 5;
704
        ceata_taskfile[0x3] = sector >> 21;
705
        ceata_taskfile[0x4] = sector >> 29;
706
        ceata_taskfile[0x5] = sector >> 37;
707
        ceata_taskfile[0xa] = cnt << 3;
708
        ceata_taskfile[0xb] = sector << 3;
709
        ceata_taskfile[0xc] = sector >> 5;
710
        ceata_taskfile[0xd] = sector >> 13;
711
        ceata_taskfile[0xf] = write ? 0x35 : 0x25;
712
        PASS_RC(ceata_wait_idle(), 2, 0);
713
        PASS_RC(ceata_write_multiple_register(0, ceata_taskfile, 16), 2, 1);
714
        PASS_RC(ceata_rw_multiple_block(write, buffer, cnt << 3, CEATA_COMMAND_TIMEOUT), 2, 2);
715
    }
716
    else
717
    {
718
        PASS_RC(ata_wait_for_rdy(100000), 2, 0);
719
        ata_write_cbr(&ATA_PIO_DVR, 0);
720
        if (ata_lba48)
721
        {
722
            ata_write_cbr(&ATA_PIO_SCR, cnt >> 5);
723
            ata_write_cbr(&ATA_PIO_SCR, (cnt << 3) & 0xff);
724
            ata_write_cbr(&ATA_PIO_LHR, (sector >> 37) & 0xff);
725
            ata_write_cbr(&ATA_PIO_LMR, (sector >> 29) & 0xff);
726
            ata_write_cbr(&ATA_PIO_LLR, (sector >> 21) & 0xff);
727
            ata_write_cbr(&ATA_PIO_LHR, (sector >> 13) & 0xff);
728
            ata_write_cbr(&ATA_PIO_LMR, (sector >> 5) & 0xff);
729
            ata_write_cbr(&ATA_PIO_LLR, (sector << 3) & 0xff);
730
            ata_write_cbr(&ATA_PIO_DVR, BIT(6));
731
            if (write) ata_write_cbr(&ATA_PIO_CSD, ata_dma ? 0x35 : 0x39);
732
            else ata_write_cbr(&ATA_PIO_CSD, ata_dma ? 0x25 : 0x29);
733
        }
734
        else
735
        {
736
            ata_write_cbr(&ATA_PIO_SCR, (cnt << 3) & 0xff);
737
            ata_write_cbr(&ATA_PIO_LHR, (sector >> 13) & 0xff);
738
            ata_write_cbr(&ATA_PIO_LMR, (sector >> 5) & 0xff);
739
            ata_write_cbr(&ATA_PIO_LLR, (sector << 3) & 0xff);
740
            ata_write_cbr(&ATA_PIO_DVR, BIT(6) | ((sector >> 21) & 0xf));
741
            if (write) ata_write_cbr(&ATA_PIO_CSD, ata_dma ? 0xca : 0x30);
742
            else ata_write_cbr(&ATA_PIO_CSD, ata_dma ? 0xc8 : 0xc4);
743
        }
744
        if (ata_dma)
745
        {
746
            PASS_RC(ata_wait_for_start_of_transfer(500000), 2, 1);
747
            if (write)
748
            {
749
                ATA_SBUF_START = buffer;
750
                ATA_SBUF_SIZE = SECTOR_SIZE * cnt;
751
                ATA_CFG |= BIT(4);
752
            }
753
            else
754
            {
755
                ATA_TBUF_START = buffer;
756
                ATA_TBUF_SIZE = SECTOR_SIZE * cnt;
757
                ATA_CFG &= ~BIT(4);
758
            }
759
            ATA_XFR_NUM = SECTOR_SIZE * cnt - 1;
760
            ATA_CFG |= ata_dma_flags;
761
            ATA_CFG &= ~(BIT(7) | BIT(8));
762
            wakeup_wait(&ata_wakeup, TIMEOUT_NONE);
763
            ATA_IRQ = BITRANGE(0, 4);
764
            ATA_IRQ_MASK = BIT(0);
765
            ATA_COMMAND = BIT(0);
766
            if (wakeup_wait(&ata_wakeup, 500000) == THREAD_TIMEOUT)
767
            {
768
                ATA_COMMAND = BIT(1);
769
                ATA_CFG &= ~(BITRANGE(2, 3) | BIT(12));
770
                RET_ERR(2);
771
            }
772
            ATA_COMMAND = BIT(1);
773
            ATA_CFG &= ~(BITRANGE(2, 3) | BIT(12));
774
        }
775
        else
776
        {
777
            cnt *= SECTOR_SIZE / 512;
778
            while (cnt--)
779
            {
780
                int i;
781
                PASS_RC(ata_wait_for_start_of_transfer(500000), 2, 1);
782
                if (write)
783
                    for (i = 0; i < 256; i++)
784
                        ata_write_cbr(&ATA_PIO_DTR, ((uint16_t*)buffer)[i]);
785
                else
786
                    for (i = 0; i < 256; i++)
787
                        ((uint16_t*)buffer)[i] = ata_read_cbr(&ATA_PIO_DTR);
788
                buffer += 512;
789
            }
790
        }
791
        PASS_RC(ata_wait_for_end_of_transfer(100000), 2, 3);
792
    }
793
    return 0;
794
}
795
 
796
#ifdef ATA_HAVE_BBT
797
int ata_bbt_translate(uint64_t sector, uint32_t count, uint64_t* phys, uint32_t* physcount)
798
{
799
    if (sector + count > ata_virtual_sectors) RET_ERR(0);
800
    if (!ata_bbt)
801
    {
802
        *phys = sector;
803
        *physcount = count;
804
        return 0;
805
    }
806
    if (!count)
807
    {
808
        *phys = 0;
809
        *physcount = 0;
810
        return 0;
811
    }
812
    uint32_t offset;
813
    uint32_t l0idx = sector >> 15;
814
    uint32_t l0offs = sector & 0x7fff;
815
    *physcount = MIN(count, 0x8000 - l0offs);
816
    uint32_t l0data = ata_bbt[0][l0idx << 1];
817
    uint32_t base = ata_bbt[0][(l0idx << 1) | 1] << 12;
818
    if (l0data < 0x8000) offset = l0data + base;
819
    else
820
    {
821
        uint32_t l1idx = (sector >> 10) & 0x1f;
822
        uint32_t l1offs = sector & 0x3ff;
823
        *physcount = MIN(count, 0x400 - l1offs);
824
        uint32_t l1data = ata_bbt[l0data & 0x7fff][l1idx];
825
        if (l1data < 0x8000) offset = l1data + base;
826
        else
827
        {
828
            uint32_t l2idx = (sector >> 5) & 0x1f;
829
            uint32_t l2offs = sector & 0x1f;
830
            *physcount = MIN(count, 0x20 - l2offs);
831
            uint32_t l2data = ata_bbt[l1data & 0x7fff][l2idx];
832
            if (l2data < 0x8000) offset = l2data + base;
833
            else
834
            {
835
                uint32_t l3idx = sector & 0x1f;
836
                uint32_t l3data = ata_bbt[l2data & 0x7fff][l3idx];
650 theseven 837
                for (*physcount = 1; *physcount < count && l3idx + *physcount < 0x20; (*physcount)++)
643 theseven 838
                    if (ata_bbt[l2data & 0x7fff][l3idx + *physcount] != l3data)
839
                        break;
840
                offset = l3data + base;
841
            }
842
        }
843
    }
844
    *phys = sector + offset;
845
    return 0;
846
}
847
#endif
848
 
849
int ata_rw_sectors(uint64_t sector, uint32_t count, void* buffer, bool write)
850
{
851
    if (((uint32_t)buffer) & (CACHEALIGN_SIZE - 1))
852
        panicf(PANIC_KILLTHREAD,
853
               "ATA: Misaligned data buffer at %08X (sector %lu, count %lu)",
854
               (unsigned int)buffer, (unsigned int)sector, count);
855
#ifdef ATA_HAVE_BBT
856
    if (sector + count > ata_virtual_sectors) RET_ERR(0);
857
    if (ata_bbt)
858
        while (count)
859
        {
860
            uint64_t phys;
861
            uint32_t cnt;
862
            PASS_RC(ata_bbt_translate(sector, count, &phys, &cnt), 0, 0);
863
            uint32_t offset = phys - sector;
864
            if (offset != ata_last_offset && phys - ata_last_phys < 64) ata_soft_reset();
865
            ata_last_offset = offset;
866
            ata_last_phys = phys + cnt;
867
            PASS_RC(ata_rw_sectors_internal(phys, cnt, buffer, write), 0, 0);
868
            buffer += cnt * SECTOR_SIZE;
869
            sector += cnt;
870
            count -= cnt;
871
        }
872
    else PASS_RC(ata_rw_sectors_internal(sector, count, buffer, write), 0, 0);
873
    return 0;
874
}
875
 
876
int ata_rw_sectors_internal(uint64_t sector, uint32_t count, void* buffer, bool write)
877
{
878
#endif
879
    if (sector + count > ata_total_sectors) RET_ERR(0);
880
    if (!ata_powered) ata_power_up();
881
    ata_set_active();
882
    if (ata_dma && write) clean_dcache();
883
    else if (ata_dma) invalidate_dcache();
884
    if (!ceata) ATA_COMMAND = BIT(1);
885
    while (count)
886
    {
887
        uint32_t cnt = MIN(ata_lba48 ? 8192 : 32, count);
888
        int rc = -1;
889
        rc = ata_rw_chunk(sector, cnt, buffer, write);
890
        if (rc && ata_error_srst) ata_soft_reset();
891
        if (rc && ata_retries)
892
        {
893
            void* buf = buffer;
894
            uint64_t sect;
895
            for (sect = sector; sect < sector + cnt; sect++)
896
            {
897
                rc = -1;
898
                int tries = ata_retries;
899
                while (tries-- && rc)
900
                {
901
                    rc = ata_rw_chunk(sect, 1, buf, write);
902
                    if (rc && ata_error_srst) ata_soft_reset();
903
                }
904
                if (rc) break;
905
                buf += SECTOR_SIZE;
906
            }
907
        }
908
        PASS_RC(rc, 1, 1);
909
        buffer += SECTOR_SIZE * cnt;
910
        sector += cnt;
911
        count -= cnt;
912
    }
913
    ata_set_active();
914
    return 0;
915
}
916
 
917
static void ata_thread(void)
918
{
919
    while (true)
920
    {
921
        mutex_lock(&ata_mutex, TIMEOUT_BLOCK);
922
        if (TIME_AFTER(USEC_TIMER, ata_last_activity_value + ata_sleep_timeout) && ata_powered)
923
            ata_power_down();
924
        mutex_unlock(&ata_mutex);
925
        sleep(1000000);
926
    }
927
}
928
 
929
/* API Functions */
930
int ata_soft_reset()
931
{
932
    int rc;
933
    mutex_lock(&ata_mutex, TIMEOUT_BLOCK);
934
    if (!ata_powered) ata_power_up();
935
    ata_set_active();
936
    if (ceata) rc = ceata_soft_reset();
937
    else
938
    {
939
        ata_write_cbr(&ATA_PIO_DAD, BIT(1) | BIT(2));
940
        sleep(10);
941
        ata_write_cbr(&ATA_PIO_DAD, 0);
942
        rc = ata_wait_for_rdy(20000000);
943
    }
944
    if (IS_ERR(rc))
945
    {
946
        ata_power_down();
947
        sleep(3000000);
948
        ata_power_up();
949
    }
950
    ata_set_active();
951
    mutex_unlock(&ata_mutex);
952
    return rc;
953
}
954
 
955
int ata_read_sectors(IF_MD2(int drive,) unsigned long start, int incount,
956
                     void* inbuf)
957
{
958
    mutex_lock(&ata_mutex, TIMEOUT_BLOCK);
959
    int rc = ata_rw_sectors(start, incount, inbuf, false);
960
    mutex_unlock(&ata_mutex);
961
    return rc;
962
}
963
 
964
int ata_write_sectors(IF_MD2(int drive,) unsigned long start, int count,
965
                      const void* outbuf)
966
{
967
    mutex_lock(&ata_mutex, TIMEOUT_BLOCK);
968
    int rc = ata_rw_sectors(start, count, (void*)((uint32_t)outbuf), true);
969
    mutex_unlock(&ata_mutex);
970
    return rc;
971
}
972
 
973
void ata_spindown(int seconds)
974
{
975
    ata_sleep_timeout = seconds * 1000000;
976
}
977
 
978
void ata_sleep(void)
979
{
646 theseven 980
    ata_last_activity_value = USEC_TIMER - ata_sleep_timeout + 200000;
643 theseven 981
}
982
 
983
void ata_sleepnow(void)
984
{
646 theseven 985
    mutex_lock(&ata_mutex, TIMEOUT_BLOCK);
986
    ata_power_down();
987
    mutex_unlock(&ata_mutex);
643 theseven 988
}
989
 
990
void ata_close(void)
991
{
646 theseven 992
    ata_sleepnow();
643 theseven 993
}
994
 
995
void ata_spin(void)
996
{
646 theseven 997
    ata_set_active();
643 theseven 998
}
999
 
1000
void ata_get_info(IF_MD2(int drive,) struct storage_info *info)
1001
{
1002
    (*info).sector_size = SECTOR_SIZE;
1003
#ifdef ATA_HAVE_BBT
1004
    (*info).num_sectors = ata_virtual_sectors;
1005
#else
1006
    (*info).num_sectors = ata_total_sectors;
1007
#endif
1008
    (*info).vendor = "Apple";
1009
    (*info).product = "iPod Classic";
1010
    (*info).revision = "1.0";
1011
    (*info).driverinfo = &drvinfo;
1012
}
1013
 
1014
long ata_last_disk_activity(void)
1015
{
1016
    return ata_last_activity_value;
1017
}
1018
 
1019
#ifdef ATA_HAVE_BBT
1020
void ata_bbt_disable()
1021
{
1022
    mutex_lock(&ata_mutex, TIMEOUT_BLOCK);
1023
    if (ata_bbt) free(ata_bbt);
1024
    ata_bbt = NULL;
1025
    ata_virtual_sectors = ata_total_sectors;
1026
    mutex_unlock(&ata_mutex);
1027
}
1028
 
1029
void ata_bbt_reload()
1030
{
1031
    mutex_lock(&ata_mutex, TIMEOUT_BLOCK);
1032
    ata_bbt_disable();
1033
    ata_power_up();
1034
    uint32_t* buf = (uint32_t*)memalign(0x10, 0x1000);
1035
    if (buf)
1036
    {
1037
        if (IS_ERR(ata_bbt_read_sectors(0, 1, buf)))
1038
            ata_virtual_sectors = ata_total_sectors;
1039
        else if (!memcmp(buf, "emBIbbth", 8))
1040
        {
1041
            ata_virtual_sectors = (((uint64_t)buf[0x1fd]) << 32) | buf[0x1fc];
1042
            uint32_t count = buf[0x1ff];
1043
            ata_bbt = (typeof(ata_bbt))memalign(0x10, 0x1000 * count);
1044
            if (!ata_bbt)
1045
            {
1046
                cprintf(CONSOLE_BOOT, "ATA: Failed to allocate memory for BBT! (%d bytes)",
1047
                        0x1000 * count);
1048
                ata_virtual_sectors = ata_total_sectors;
1049
            }
1050
            else
1051
            {
1052
                uint32_t i;
1053
                uint32_t cnt;
1054
                for (i = 0; i < count; i += cnt)
1055
                {
1056
                    uint32_t phys = buf[0x200 + i];
1057
                    for (cnt = 1; cnt < count; cnt++)
1058
                        if (buf[0x200 + i + cnt] != phys + cnt)
1059
                            break;
1060
                    if (IS_ERR(ata_bbt_read_sectors(phys, cnt, ata_bbt[i << 6])))
1061
                    {
1062
                        free(ata_bbt);
1063
                        ata_virtual_sectors = ata_total_sectors;
1064
                        break;
1065
                    }
1066
                }
1067
                if (ata_bbt) reownalloc(ata_bbt, NULL);
1068
            }
1069
        }
1070
        else ata_virtual_sectors = ata_total_sectors;
1071
        free(buf);
1072
    }
1073
    else ata_virtual_sectors = ata_total_sectors;
1074
    mutex_unlock(&ata_mutex);
1075
}
1076
#endif
1077
 
1078
int ata_init(void)
1079
{
1080
    mutex_init(&ata_mutex);
1081
    wakeup_init(&ata_wakeup);
1082
    wakeup_init(&mmc_wakeup);
1083
    wakeup_init(&mmc_comp_wakeup);
1084
    ceata = PDAT(11) & BIT(1);
1085
    if (ceata)
1086
    {
1087
        ata_lba48 = true;
1088
        ata_dma = true;
1089
        PCON(8) = 0x33333333;
1090
        PCON(9) = (PCON(9) & ~0xff) | 0x33;
1091
        PCON(11) |= 0xf;
1092
        *((uint32_t volatile*)0x38a00000) = 0;
1093
        *((uint32_t volatile*)0x38700000) = 0;
1094
    }
1095
    else
1096
    {
1097
        PCON(7) = 0x44444444;
1098
        PCON(8) = 0x44444444;
1099
        PCON(9) = 0x44444444;
1100
        PCON(10) = (PCON(10) & ~0xffff) | 0x4444;
1101
    }
1102
    ata_powered = false;
1103
    ata_total_sectors = 0;
1104
#ifdef ATA_HAVE_BBT
1105
    ata_bbt_reload();
1106
#endif
1107
    thread_create(&ata_thread_handle, "ATA idle monitor", ata_thread, ata_stack,
1108
                  sizeof(ata_stack), OS_THREAD, 1, true);
1109
    return 0;
1110
}
1111
 
1112
int ata_num_drives(int first_drive)
1113
{
1114
    /* We don't care which logical drive number(s) we have been assigned */
1115
    (void)first_drive;
1116
 
1117
    return 1;
1118
}
1119
 
1120
void INT_ATA()
1121
{
1122
    uint32_t ata_irq = ATA_IRQ;
1123
    ATA_IRQ = ata_irq;
1124
    if (ata_irq & ATA_IRQ_MASK) wakeup_signal(&ata_wakeup);
1125
    ATA_IRQ_MASK = 0;
1126
}
1127
 
1128
void INT_MMC()
1129
{
1130
    uint32_t irq = SDCI_IRQ;
1131
    if (irq & SDCI_IRQ_DAT_DONE_INT) wakeup_signal(&mmc_wakeup);
1132
    if (irq & SDCI_IRQ_IOCARD_IRQ_INT) wakeup_signal(&mmc_comp_wakeup);
1133
    SDCI_IRQ = irq;
1134
}