Subversion Repositories freemyipod

Rev

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

Rev 627 Rev 629
Line 22... Line 22...
22
#include "thread.h"
22
#include "thread.h"
23
#include "disk.h"
23
#include "disk.h"
24
#include "storage.h"
24
#include "storage.h"
25
#include "storage_ata-target.h"
25
#include "storage_ata-target.h"
26
#include "timer.h"
26
#include "timer.h"
-
 
27
#include "constants/mmc.h"
27
#include "../ipodnano3g/s5l8702.h"
28
#include "../ipodnano3g/s5l8702.h"
28
 
29
 
29
 
30
 
30
#ifndef ATA_RETRIES
31
#ifndef ATA_RETRIES
31
#define ATA_RETRIES 3
32
#define ATA_RETRIES 3
32
#endif
33
#endif
33
 
34
 
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
 
35
/** static, private data **/ 
42
/** static, private data **/ 
-
 
43
static uint8_t ceata_taskfile[16] __attribute__((aligned(16)));
36
uint16_t ata_identify_data[0x100];
44
uint16_t ata_identify_data[0x100];
-
 
45
bool ceata;
37
bool ata_lba48;
46
bool ata_lba48;
38
bool ata_dma;
47
bool ata_dma;
39
uint64_t ata_total_sectors;
48
uint64_t ata_total_sectors;
40
struct mutex ata_mutex;
49
struct mutex ata_mutex;
41
static struct wakeup ata_wakeup;
50
static struct wakeup ata_wakeup;
Line 45... Line 54...
45
static struct scheduler_thread ata_thread_handle;
54
static struct scheduler_thread ata_thread_handle;
46
static uint32_t ata_stack[0x80] STACK_ATTR;
55
static uint32_t ata_stack[0x80] STACK_ATTR;
47
static bool ata_powered;
56
static bool ata_powered;
48
static int ata_retries = ATA_RETRIES;
57
static int ata_retries = ATA_RETRIES;
49
static bool ata_error_srst = true;
58
static bool ata_error_srst = true;
-
 
59
static struct wakeup mmc_wakeup;
-
 
60
static struct wakeup mmc_comp_wakeup;
-
 
61
 
50
 
62
 
51
#ifdef ATA_HAVE_BBT
63
#ifdef ATA_HAVE_BBT
52
#include "panic.h"
64
#include "panic.h"
53
uint16_t (*ata_bbt)[0x20];
65
uint16_t (*ata_bbt)[0x20];
54
uint64_t ata_virtual_sectors;
66
uint64_t ata_virtual_sectors;
Line 148... Line 160...
148
    if (dad & BIT(0)) RET_ERR(1);
160
    if (dad & BIT(0)) RET_ERR(1);
149
    if ((dad & (BIT(3) | BITRANGE(5, 7))) == BIT(6)) return 0;
161
    if ((dad & (BIT(3) | BITRANGE(5, 7))) == BIT(6)) return 0;
150
    RET_ERR(2);
162
    RET_ERR(2);
151
}    
163
}    
152
 
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
    PASS_RC(mmc_send_command(SDCI_CMD_CMD_NUM(MMC_CMD_CEATA_RW_MULTIPLE_REG)
-
 
347
                           | SDCI_CMD_CMD_TYPE_ADTC | SDCI_CMD_RES_TYPE_R1
-
 
348
                           | SDCI_CMD_RES_SIZE_48 | SDCI_CMD_NCR_NID_NCR,
-
 
349
                             MMC_CMD_CEATA_RW_MULTIPLE_REG_DIRECTION_READ
-
 
350
                           | MMC_CMD_CEATA_RW_MULTIPLE_REG_ADDRESS(addr & 0xfc)
-
 
351
                           | MMC_CMD_CEATA_RW_MULTIPLE_REG_COUNT(size & 0xfc),
-
 
352
                             NULL, CEATA_COMMAND_TIMEOUT), 2, 1);
-
 
353
    long startusec = USEC_TIMER;
-
 
354
    if (wakeup_wait(&mmc_wakeup, CEATA_COMMAND_TIMEOUT) == THREAD_TIMEOUT) RET_ERR(2);
-
 
355
    invalidate_dcache();
-
 
356
    PASS_RC(mmc_dsta_check_data_success(), 2, 3);
-
 
357
    return 0;
-
 
358
}
-
 
359
 
-
 
360
int ceata_write_multiple_register(uint32_t addr, void* dest, uint32_t size)
-
 
361
{
-
 
362
    int i;
-
 
363
    if (size > 0x10) RET_ERR(0);
-
 
364
    mmc_discard_irq();
-
 
365
    SDCI_DMASIZE = size;
-
 
366
    SDCI_DMACOUNT = 0;
-
 
367
    SDCI_DCTRL = SDCI_DCTRL_TXFIFORST | SDCI_DCTRL_RXFIFORST;
-
 
368
    PASS_RC(mmc_send_command(SDCI_CMD_CMD_NUM(MMC_CMD_CEATA_RW_MULTIPLE_REG)
-
 
369
                           | SDCI_CMD_CMD_TYPE_ADTC | SDCI_CMD_CMD_RD_WR
-
 
370
                           | SDCI_CMD_RES_BUSY | SDCI_CMD_RES_TYPE_R1
-
 
371
                           | SDCI_CMD_RES_SIZE_48 | SDCI_CMD_NCR_NID_NCR,
-
 
372
                             MMC_CMD_CEATA_RW_MULTIPLE_REG_DIRECTION_WRITE
-
 
373
                           | MMC_CMD_CEATA_RW_MULTIPLE_REG_ADDRESS(addr & 0xfc)
-
 
374
                           | MMC_CMD_CEATA_RW_MULTIPLE_REG_COUNT(size & 0xfc),
-
 
375
                             NULL, CEATA_COMMAND_TIMEOUT), 3, 1);
-
 
376
    SDCI_DCTRL = SDCI_DCTRL_TRCONT_TX;
-
 
377
    for (i = 0; i < size / 4; i++) SDCI_DATA = ((uint32_t*)dest)[i];
-
 
378
    long startusec = USEC_TIMER;
-
 
379
    if (wakeup_wait(&mmc_wakeup, CEATA_COMMAND_TIMEOUT) == THREAD_TIMEOUT) RET_ERR(2);
-
 
380
    while ((SDCI_STATE & SDCI_STATE_DAT_STATE_MASK) != SDCI_STATE_DAT_STATE_IDLE)
-
 
381
    {
-
 
382
        if (TIMEOUT_EXPIRED(startusec, CEATA_COMMAND_TIMEOUT)) RET_ERR(3);
-
 
383
        yield();
-
 
384
    }
-
 
385
    PASS_RC(mmc_dsta_check_data_success(), 3, 4);
-
 
386
    return 0;
-
 
387
}
-
 
388
 
-
 
389
int ceata_init(int buswidth)
-
 
390
{
-
 
391
    uint32_t result;
-
 
392
    PASS_RC(mmc_send_command(SDCI_CMD_CMD_NUM(MMC_CMD_SWITCH) | SDCI_CMD_RES_BUSY
-
 
393
                           | SDCI_CMD_CMD_TYPE_AC | SDCI_CMD_RES_TYPE_R1 
-
 
394
                           | SDCI_CMD_RES_SIZE_48 | SDCI_CMD_NCR_NID_NCR,
-
 
395
                             MMC_CMD_SWITCH_ACCESS_WRITE_BYTE
-
 
396
                           | MMC_CMD_SWITCH_INDEX(MMC_CMD_SWITCH_FIELD_HS_TIMING)
-
 
397
                           | MMC_CMD_SWITCH_VALUE(MMC_CMD_SWITCH_FIELD_HS_TIMING_HIGH_SPEED),
-
 
398
                             &result, CEATA_COMMAND_TIMEOUT), 3, 0);
-
 
399
    if (result & MMC_STATUS_SWITCH_ERROR) RET_ERR(1);
-
 
400
    if (buswidth > 1)
-
 
401
    {
-
 
402
        int setting;
-
 
403
        if (buswidth == 4) setting = MMC_CMD_SWITCH_FIELD_BUS_WIDTH_4BIT;
-
 
404
        else if (buswidth == 8) setting = MMC_CMD_SWITCH_FIELD_BUS_WIDTH_8BIT;
-
 
405
        else setting = MMC_CMD_SWITCH_FIELD_BUS_WIDTH_1BIT;
-
 
406
        PASS_RC(mmc_send_command(SDCI_CMD_CMD_NUM(MMC_CMD_SWITCH) | SDCI_CMD_RES_BUSY
-
 
407
                               | SDCI_CMD_CMD_TYPE_AC | SDCI_CMD_RES_TYPE_R1
-
 
408
                               | SDCI_CMD_RES_SIZE_48 | SDCI_CMD_NCR_NID_NCR,
-
 
409
                                 MMC_CMD_SWITCH_ACCESS_WRITE_BYTE
-
 
410
                               | MMC_CMD_SWITCH_INDEX(MMC_CMD_SWITCH_FIELD_BUS_WIDTH)
-
 
411
                               | MMC_CMD_SWITCH_VALUE(setting),
-
 
412
                                 &result, CEATA_COMMAND_TIMEOUT), 3, 2);
-
 
413
        if (result & MMC_STATUS_SWITCH_ERROR) RET_ERR(3);
-
 
414
        if (buswidth == 4)
-
 
415
            SDCI_CTRL = (SDCI_CTRL & ~SDCI_CTRL_BUS_WIDTH_MASK) | SDCI_CTRL_BUS_WIDTH_4BIT;
-
 
416
        else if (buswidth == 8)
-
 
417
            SDCI_CTRL = (SDCI_CTRL & ~SDCI_CTRL_BUS_WIDTH_MASK) | SDCI_CTRL_BUS_WIDTH_8BIT;
-
 
418
    }
-
 
419
    PASS_RC(ceata_soft_reset(), 3, 4);
-
 
420
    PASS_RC(ceata_read_multiple_register(0, ceata_taskfile, 0x10), 3, 5);
-
 
421
    if (ceata_taskfile[0xc] != 0xce || ceata_taskfile[0xd] != 0xaa) RET_ERR(6);
-
 
422
    PASS_RC(mmc_fastio_write(6, 0), 3, 7);
-
 
423
    return 0;
-
 
424
}
-
 
425
 
-
 
426
int ceata_check_error()
-
 
427
{
-
 
428
    uint32_t status, error;
-
 
429
    PASS_RC(mmc_fastio_read(0xf, &status), 2, 0);
-
 
430
    if (status & 1)
-
 
431
    {
-
 
432
        PASS_RC(mmc_fastio_read(0x9, &error), 2, 1);
-
 
433
        RET_ERR((error << 2) | 2);
-
 
434
    }
-
 
435
    return 0;
-
 
436
}
-
 
437
 
-
 
438
int ceata_wait_idle()
-
 
439
{
-
 
440
    long startusec = USEC_TIMER;
-
 
441
    while (true)
-
 
442
    {
-
 
443
        uint32_t status;
-
 
444
        PASS_RC(mmc_fastio_read(0xf, &status), 1, 0);
-
 
445
        if (!(status & 0x88)) return 0;
-
 
446
        if (TIMEOUT_EXPIRED(startusec, CEATA_DAT_NONBUSY_TIMEOUT)) RET_ERR(1);
-
 
447
        sleep(50000);
-
 
448
    }
-
 
449
}
-
 
450
 
-
 
451
int ceata_cancel_command()
-
 
452
{
-
 
453
    *((uint32_t volatile*)0x3cf00200) = 0x9000e;
-
 
454
    sleep(1);
-
 
455
    *((uint32_t volatile*)0x3cf00200) = 0x9000f;
-
 
456
    sleep(1);
-
 
457
    *((uint32_t volatile*)0x3cf00200) = 0x90003;
-
 
458
    sleep(1);
-
 
459
    PASS_RC(mmc_send_command(SDCI_CMD_CMD_NUM(MMC_CMD_STOP_TRANSMISSION)
-
 
460
                           | SDCI_CMD_CMD_TYPE_AC | SDCI_CMD_RES_TYPE_R1 | SDCI_CMD_RES_BUSY
-
 
461
                           | SDCI_CMD_RES_SIZE_48 | SDCI_CMD_NCR_NID_NCR,
-
 
462
                             0, NULL, CEATA_COMMAND_TIMEOUT), 1, 0);
-
 
463
    PASS_RC(ceata_wait_idle(), 1, 1);
-
 
464
    return 0;
-
 
465
}
-
 
466
 
-
 
467
int ceata_rw_multiple_block(bool write, void* buf, uint32_t count, long timeout)
-
 
468
{
-
 
469
    mmc_discard_irq();
-
 
470
    uint32_t responsetype;
-
 
471
    uint32_t cmdtype;
-
 
472
    uint32_t direction;
-
 
473
    if (write)
-
 
474
    {
-
 
475
        clean_dcache();
-
 
476
        cmdtype = SDCI_CMD_CMD_TYPE_ADTC | SDCI_CMD_CMD_RD_WR;
-
 
477
        responsetype = SDCI_CMD_RES_TYPE_R1 | SDCI_CMD_RES_BUSY;
-
 
478
        direction = MMC_CMD_CEATA_RW_MULTIPLE_BLOCK_DIRECTION_WRITE;
-
 
479
    }
-
 
480
    else
-
 
481
    {
-
 
482
        cmdtype = SDCI_CMD_CMD_TYPE_ADTC;
-
 
483
        responsetype = SDCI_CMD_RES_TYPE_R1;
-
 
484
        direction = MMC_CMD_CEATA_RW_MULTIPLE_BLOCK_DIRECTION_READ;
-
 
485
    }
-
 
486
    SDCI_DMASIZE = 0x200;
-
 
487
    SDCI_DMAADDR = buf;
-
 
488
    SDCI_DMACOUNT = count;
-
 
489
    SDCI_DCTRL = SDCI_DCTRL_TXFIFORST | SDCI_DCTRL_RXFIFORST;
-
 
490
    PASS_RC(mmc_send_command(SDCI_CMD_CMD_NUM(MMC_CMD_CEATA_RW_MULTIPLE_BLOCK)
-
 
491
                           | SDCI_CMD_CMD_TYPE_ADTC | cmdtype | responsetype
-
 
492
                           | SDCI_CMD_RES_SIZE_48 | SDCI_CMD_NCR_NID_NCR,
-
 
493
                             direction | MMC_CMD_CEATA_RW_MULTIPLE_BLOCK_COUNT(count),
-
 
494
                             NULL, CEATA_COMMAND_TIMEOUT), 4, 0);
-
 
495
    if (write) SDCI_DCTRL = SDCI_DCTRL_TRCONT_TX;
-
 
496
    if (wakeup_wait(&mmc_wakeup, timeout) == THREAD_TIMEOUT)
-
 
497
    {
-
 
498
        PASS_RC(ceata_cancel_command(), 4, 1);
-
 
499
        RET_ERR(2);
-
 
500
    }
-
 
501
    if (!write) invalidate_dcache();
-
 
502
    PASS_RC(mmc_dsta_check_data_success(), 4, 3);
-
 
503
    if (wakeup_wait(&mmc_comp_wakeup, timeout) == THREAD_TIMEOUT)
-
 
504
    {
-
 
505
        PASS_RC(ceata_cancel_command(), 4, 4);
-
 
506
        RET_ERR(4);
-
 
507
    }
-
 
508
    PASS_RC(ceata_check_error(), 4, 5);
-
 
509
    return 0;
-
 
510
}
-
 
511
 
153
int ata_identify(uint16_t* buf)
512
int ata_identify(uint16_t* buf)
154
{
513
{
155
    int i;
514
    int i;
156
    PASS_RC(ata_wait_for_not_bsy(10000000), 1, 0);
-
 
157
    ata_write_cbr(&ATA_PIO_DVR, 0);
-
 
158
    ata_write_cbr(&ATA_PIO_CSD, 0xec);
-
 
159
    PASS_RC(ata_wait_for_start_of_transfer(10000000), 1, 1);
-
 
160
    for (i = 0; i < 0x100; i++)
515
    if (ceata)
161
    {
516
    {
-
 
517
        memset(ceata_taskfile, 0, 16);
-
 
518
        ceata_taskfile[0xf] = 0xec;
-
 
519
        PASS_RC(ceata_wait_idle(), 2, 0);
-
 
520
        PASS_RC(ceata_write_multiple_register(0, ceata_taskfile, 16), 2, 1);
-
 
521
        PASS_RC(ceata_rw_multiple_block(false, buf, 1, CEATA_COMMAND_TIMEOUT), 2, 2);
-
 
522
        for (i = 0; i < 0x100; i++)
-
 
523
        {
162
        uint16_t word = ata_read_cbr(&ATA_PIO_DTR);
524
            uint16_t word = buf[i];
163
        buf[i] = (word >> 8) | (word << 8);
525
            buf[i] = (word >> 8) | (word << 8);
-
 
526
        }
164
    }
527
    }
-
 
528
    else
-
 
529
    {
-
 
530
        PASS_RC(ata_wait_for_not_bsy(10000000), 1, 0);
-
 
531
        ata_write_cbr(&ATA_PIO_DVR, 0);
-
 
532
        ata_write_cbr(&ATA_PIO_CSD, 0xec);
-
 
533
        PASS_RC(ata_wait_for_start_of_transfer(10000000), 1, 1);
-
 
534
        for (i = 0; i < 0x100; i++)
-
 
535
        {
-
 
536
            uint16_t word = ata_read_cbr(&ATA_PIO_DTR);
-
 
537
            buf[i] = (word >> 8) | (word << 8);
-
 
538
        }
-
 
539
    }
-
 
540
    return 0;
165
}
541
}
166
 
542
 
167
void ata_set_active(void)
543
void ata_set_active(void)
168
{
544
{
169
    ata_last_activity_value = USEC_TIMER;
545
    ata_last_activity_value = USEC_TIMER;
Line 193... Line 569...
193
int ata_power_up()
569
int ata_power_up()
194
{
570
{
195
    ata_set_active();
571
    ata_set_active();
196
    if (ata_powered) return 0;
572
    if (ata_powered) return 0;
197
    i2c_sendbyte(0, 0xe6, 0x1b, 1);
573
    i2c_sendbyte(0, 0xe6, 0x1b, 1);
198
    clockgate_enable(5, true);
-
 
199
    ATA_CFG = BIT(0);
-
 
200
    sleep(1000);
-
 
201
    ATA_CFG = 0;
-
 
202
    sleep(6000);
-
 
203
    ATA_SWRST = BIT(0);
-
 
204
    sleep(500);
-
 
205
    ATA_SWRST = 0;
-
 
206
    sleep(90000);
-
 
207
    ATA_CONTROL = BIT(0);
-
 
208
    sleep(200000);
-
 
209
    ATA_PIO_TIME = 0x191f7;
-
 
210
    ATA_PIO_LHR = 0;
-
 
211
    while (!(ATA_PIO_READY & BIT(1))) sleep(100);
-
 
212
    PASS_RC(ata_identify(ata_identify_data), 2, 0);
-
 
213
    uint32_t piotime = 0x11f3;
-
 
214
    uint32_t mdmatime = 0x1c175;
-
 
215
    uint32_t udmatime = 0x5071152;
-
 
216
    uint32_t param = 0;
-
 
217
    ata_dma_flags = 0;
-
 
218
    ata_lba48 = ata_identify_data[83] & BIT(10) ? true : false;
-
 
219
    if (ata_lba48)
574
    if (ceata)
220
        ata_total_sectors = ata_identify_data[100]
-
 
221
                          | (((uint64_t)ata_identify_data[101]) << 16)
-
 
222
                          | (((uint64_t)ata_identify_data[102]) << 32)
-
 
223
                          | (((uint64_t)ata_identify_data[103]) << 48);
-
 
224
    else ata_total_sectors = ata_identify_data[60] | (((uint32_t)ata_identify_data[61]) << 16);
-
 
225
    ata_total_sectors >>= 3;
-
 
226
    if (ata_identify_data[53] & BIT(1))
-
 
227
    {
-
 
228
        if (ata_identify_data[64] & BIT(1)) piotime = 0x2072;
-
 
229
        else if (ata_identify_data[64] & BIT(0)) piotime = 0x7083;
-
 
230
    }
-
 
231
    if (ata_identify_data[63] & BIT(2))
-
 
232
    {
-
 
233
        mdmatime = 0x5072;
-
 
234
        param = 0x22;
-
 
235
    }
-
 
236
    else if (ata_identify_data[63] & BIT(1))
-
 
237
    {
-
 
238
        mdmatime = 0x7083;
-
 
239
        param = 0x21;
-
 
240
    }
-
 
241
    if (ata_identify_data[63] & BITRANGE(0, 2))
-
 
242
    {
575
    {
-
 
576
        clockgate_enable(9, true);
-
 
577
        SDCI_RESET = 0xa5;
-
 
578
        sleep(1000);
-
 
579
        *((uint32_t volatile*)0x3cf00380) = 0;
-
 
580
        *((uint32_t volatile*)0x3cf0010c) = 0xff;
-
 
581
        SDCI_CTRL = SDCI_CTRL_SDCIEN | SDCI_CTRL_CLK_SEL_SDCLK
-
 
582
                  | SDCI_CTRL_BIT_8 | SDCI_CTRL_BIT_14;
243
        ata_dma_flags = BIT(3) | BIT(10);
583
        SDCI_CDIV = SDCI_CDIV_CLKDIV(260);
-
 
584
        *((uint32_t volatile*)0x3cf00200) = 0xb000f;
-
 
585
        SDCI_IRQ_MASK = SDCI_IRQ_MASK_MASK_DAT_DONE_INT | SDCI_IRQ_MASK_MASK_IOCARD_IRQ_INT;
-
 
586
        PASS_RC(mmc_init(), 2, 0);
-
 
587
        SDCI_CDIV = SDCI_CDIV_CLKDIV(4);
244
        param |= 0x20;
588
        sleep(10000);
-
 
589
        PASS_RC(ceata_init(8), 2, 1);
-
 
590
        PASS_RC(ata_identify(ata_identify_data), 2, 2);
245
    }
591
    }
246
    if (ata_identify_data[53] & BIT(2))
592
    else
247
    {
593
    {
-
 
594
        clockgate_enable(5, true);
-
 
595
        ATA_CFG = BIT(0);
-
 
596
        sleep(1000);
-
 
597
        ATA_CFG = 0;
-
 
598
        sleep(6000);
-
 
599
        ATA_SWRST = BIT(0);
-
 
600
        sleep(500);
-
 
601
        ATA_SWRST = 0;
-
 
602
        sleep(90000);
-
 
603
        ATA_CONTROL = BIT(0);
-
 
604
        sleep(200000);
-
 
605
        ATA_PIO_TIME = 0x191f7;
-
 
606
        ATA_PIO_LHR = 0;
-
 
607
        while (!(ATA_PIO_READY & BIT(1))) sleep(100);
-
 
608
        PASS_RC(ata_identify(ata_identify_data), 2, 0);
-
 
609
        uint32_t piotime = 0x11f3;
-
 
610
        uint32_t mdmatime = 0x1c175;
-
 
611
        uint32_t udmatime = 0x5071152;
-
 
612
        uint32_t param = 0;
-
 
613
        ata_dma_flags = 0;
-
 
614
        ata_lba48 = ata_identify_data[83] & BIT(10) ? true : false;
248
        if (ata_identify_data[88] & BIT(4))
615
        if (ata_identify_data[53] & BIT(1))
249
        {
616
        {
250
            udmatime = 0x2010a52;
617
            if (ata_identify_data[64] & BIT(1)) piotime = 0x2072;
251
            param = 0x44;
618
            else if (ata_identify_data[64] & BIT(0)) piotime = 0x7083;
252
        }
619
        }
253
        else if (ata_identify_data[88] & BIT(3))
620
        if (ata_identify_data[63] & BIT(2))
254
        {
621
        {
255
            udmatime = 0x2020a52;
622
            mdmatime = 0x5072;
256
            param = 0x43;
623
            param = 0x22;
257
        }
624
        }
258
        else if (ata_identify_data[88] & BIT(2))
625
        else if (ata_identify_data[63] & BIT(1))
259
        {
626
        {
260
            udmatime = 0x3030a52;
627
            mdmatime = 0x7083;
261
            param = 0x42;
628
            param = 0x21;
262
        }
629
        }
263
        else if (ata_identify_data[88] & BIT(1))
630
        if (ata_identify_data[63] & BITRANGE(0, 2))
264
        {
631
        {
265
            udmatime = 0x3050a52;
632
            ata_dma_flags = BIT(3) | BIT(10);
266
            param = 0x41;
633
            param |= 0x20;
267
        }
634
        }
268
        if (ata_identify_data[88] & BITRANGE(0, 4))
635
        if (ata_identify_data[53] & BIT(2))
269
        {
636
        {
-
 
637
            if (ata_identify_data[88] & BIT(4))
-
 
638
            {
-
 
639
                udmatime = 0x2010a52;
-
 
640
                param = 0x44;
-
 
641
            }
-
 
642
            else if (ata_identify_data[88] & BIT(3))
-
 
643
            {
-
 
644
                udmatime = 0x2020a52;
-
 
645
                param = 0x43;
-
 
646
            }
-
 
647
            else if (ata_identify_data[88] & BIT(2))
-
 
648
            {
-
 
649
                udmatime = 0x3030a52;
-
 
650
                param = 0x42;
-
 
651
            }
-
 
652
            else if (ata_identify_data[88] & BIT(1))
-
 
653
            {
-
 
654
                udmatime = 0x3050a52;
-
 
655
                param = 0x41;
-
 
656
            }
-
 
657
            if (ata_identify_data[88] & BITRANGE(0, 4))
-
 
658
            {
270
            ata_dma_flags = BIT(2) | BIT(3) | BIT(9) | BIT(10);
659
                ata_dma_flags = BIT(2) | BIT(3) | BIT(9) | BIT(10);
271
            param |= 0x40;
660
                param |= 0x40;
-
 
661
            }
272
        }
662
        }
-
 
663
        ata_dma = param ? true : false;
-
 
664
        PASS_RC(ata_set_feature(0xef, param), 2, 1);
-
 
665
        if (ata_identify_data[82] & BIT(5)) PASS_RC(ata_set_feature(0x02, 0), 2, 2);
-
 
666
        if (ata_identify_data[82] & BIT(6)) PASS_RC(ata_set_feature(0x55, 0), 2, 3);
-
 
667
        ATA_PIO_TIME = piotime;
-
 
668
        ATA_MDMA_TIME = mdmatime;
-
 
669
        ATA_UDMA_TIME = udmatime;
273
    }
670
    }
274
    ata_dma = param ? true : false;
671
    if (ata_lba48)
275
    PASS_RC(ata_set_feature(0xef, param), 2, 1);
672
        ata_total_sectors = ata_identify_data[100]
276
    if (ata_identify_data[82] & BIT(5)) PASS_RC(ata_set_feature(0x02, 0), 2, 2);
673
                            | (((uint64_t)ata_identify_data[101]) << 16)
277
    if (ata_identify_data[82] & BIT(6)) PASS_RC(ata_set_feature(0x55, 0), 2, 3);
674
                            | (((uint64_t)ata_identify_data[102]) << 32)
278
    ATA_PIO_TIME = piotime;
675
                            | (((uint64_t)ata_identify_data[103]) << 48);
279
    ATA_MDMA_TIME = mdmatime;
676
    else ata_total_sectors = ata_identify_data[60] | (((uint32_t)ata_identify_data[61]) << 16);
280
    ATA_UDMA_TIME = udmatime;
677
    ata_total_sectors >>= 3;
281
    ata_powered = true;
678
    ata_powered = true;
282
    ata_set_active();
679
    ata_set_active();
283
    return 0;
680
    return 0;
284
}
681
}
285
 
682
 
286
void ata_power_down()
683
void ata_power_down()
287
{
684
{
288
    if (!ata_powered) return;
685
    if (!ata_powered) return;
289
    ata_powered = false;
686
    ata_powered = false;
-
 
687
    if (ceata)
-
 
688
    {
-
 
689
        memset(ceata_taskfile, 0, 16);
-
 
690
        ceata_taskfile[0xf] = 0xe0;
-
 
691
        ceata_wait_idle();
-
 
692
        ceata_write_multiple_register(0, ceata_taskfile, 16);
-
 
693
        wakeup_wait(&mmc_comp_wakeup, CEATA_COMMAND_TIMEOUT);
-
 
694
        sleep(30000);
-
 
695
        clockgate_enable(9, false);
-
 
696
    }
-
 
697
    else
-
 
698
    {
290
    ata_wait_for_rdy(1000000);
699
        ata_wait_for_rdy(1000000);
291
    ata_write_cbr(&ATA_PIO_DVR, 0);
700
        ata_write_cbr(&ATA_PIO_DVR, 0);
292
    ata_write_cbr(&ATA_PIO_CSD, 0xe0);
701
        ata_write_cbr(&ATA_PIO_CSD, 0xe0);
293
    ata_wait_for_rdy(1000000);
702
        ata_wait_for_rdy(1000000);
294
    sleep(30000);
703
        sleep(30000);
295
    ATA_CONTROL = 0;
704
        ATA_CONTROL = 0;
296
    while (!(ATA_CONTROL & BIT(1))) yield();
705
        while (!(ATA_CONTROL & BIT(1))) yield();
297
    clockgate_enable(5, false);
706
        clockgate_enable(5, false);
-
 
707
    }
298
    i2c_sendbyte(0, 0xe6, 0x1b, 0);
708
    i2c_sendbyte(0, 0xe6, 0x1b, 0);
299
}
709
}
300
 
710
 
301
int ata_rw_chunk(uint64_t sector, uint32_t cnt, void* buffer, bool write)
711
int ata_rw_chunk(uint64_t sector, uint32_t cnt, void* buffer, bool write)
302
{
712
{
303
    PASS_RC(ata_wait_for_rdy(100000), 2, 0);
-
 
304
    ata_write_cbr(&ATA_PIO_DVR, 0);
-
 
305
    if (ata_lba48)
713
    if (ceata)
306
    {
714
    {
-
 
715
        memset(ceata_taskfile, 0, 16);
307
        ata_write_cbr(&ATA_PIO_SCR, cnt >> 5);
716
        ceata_taskfile[0x2] = cnt >> 5;
308
        ata_write_cbr(&ATA_PIO_SCR, (cnt << 3) & 0xff);
717
        ceata_taskfile[0x3] = sector >> 21;
309
        ata_write_cbr(&ATA_PIO_LHR, (sector >> 37) & 0xff);
718
        ceata_taskfile[0x4] = sector >> 29;
310
        ata_write_cbr(&ATA_PIO_LMR, (sector >> 29) & 0xff);
719
        ceata_taskfile[0x5] = sector >> 37;
-
 
720
        ceata_taskfile[0xa] = cnt << 3;
311
        ata_write_cbr(&ATA_PIO_LLR, (sector >> 21) & 0xff);
721
        ceata_taskfile[0xb] = sector << 3;
312
        ata_write_cbr(&ATA_PIO_LHR, (sector >> 13) & 0xff);
722
        ceata_taskfile[0xc] = sector >> 5;
313
        ata_write_cbr(&ATA_PIO_LMR, (sector >> 5) & 0xff);
723
        ceata_taskfile[0xd] = sector >> 13;
314
        ata_write_cbr(&ATA_PIO_LLR, (sector << 3) & 0xff);
724
        ceata_taskfile[0xf] = write ? 0x35 : 0x25;
315
        ata_write_cbr(&ATA_PIO_DVR, BIT(6));
725
        PASS_RC(ceata_wait_idle(), 2, 0);
316
        if (write) ata_write_cbr(&ATA_PIO_CSD, ata_dma ? 0x35 : 0x39);
726
        PASS_RC(ceata_write_multiple_register(0, ceata_taskfile, 16), 2, 1);
317
        else ata_write_cbr(&ATA_PIO_CSD, ata_dma ? 0x25 : 0x29);
727
        PASS_RC(ceata_rw_multiple_block(write, buffer, cnt, CEATA_COMMAND_TIMEOUT), 2, 2);
318
    }
728
    }
319
    else
729
    else
320
    {
730
    {
-
 
731
        PASS_RC(ata_wait_for_rdy(100000), 2, 0);
321
        ata_write_cbr(&ATA_PIO_SCR, (cnt << 3) & 0xff);
732
        ata_write_cbr(&ATA_PIO_DVR, 0);
-
 
733
        if (ata_lba48)
-
 
734
        {
322
        ata_write_cbr(&ATA_PIO_LHR, (sector >> 13) & 0xff);
735
            ata_write_cbr(&ATA_PIO_SCR, cnt >> 5);
323
        ata_write_cbr(&ATA_PIO_LMR, (sector >> 5) & 0xff);
736
            ata_write_cbr(&ATA_PIO_SCR, (cnt << 3) & 0xff);
324
        ata_write_cbr(&ATA_PIO_LLR, (sector << 3) & 0xff);
737
            ata_write_cbr(&ATA_PIO_LHR, (sector >> 37) & 0xff);
325
        ata_write_cbr(&ATA_PIO_DVR, BIT(6) | ((sector >> 21) & 0xf));
738
            ata_write_cbr(&ATA_PIO_LMR, (sector >> 29) & 0xff);
326
        if (write) ata_write_cbr(&ATA_PIO_CSD, ata_dma ? 0xca : 0x30);
739
            ata_write_cbr(&ATA_PIO_LLR, (sector >> 21) & 0xff);
327
        else ata_write_cbr(&ATA_PIO_CSD, ata_dma ? 0xc8 : 0xc4);
740
            ata_write_cbr(&ATA_PIO_LHR, (sector >> 13) & 0xff);
328
    }
-
 
329
    if (ata_dma)
-
 
330
    {
-
 
331
        PASS_RC(ata_wait_for_start_of_transfer(500000), 2, 1);
741
            ata_write_cbr(&ATA_PIO_LMR, (sector >> 5) & 0xff);
332
        if (write)
742
            ata_write_cbr(&ATA_PIO_LLR, (sector << 3) & 0xff);
333
        {
-
 
334
            ATA_SBUF_START = buffer;
743
            ata_write_cbr(&ATA_PIO_DVR, BIT(6));
335
            ATA_SBUF_SIZE = SECTOR_SIZE * cnt;
744
            if (write) ata_write_cbr(&ATA_PIO_CSD, ata_dma ? 0x35 : 0x39);
336
            ATA_CFG |= BIT(4);
745
            else ata_write_cbr(&ATA_PIO_CSD, ata_dma ? 0x25 : 0x29);
337
        }
746
        }
338
        else
747
        else
339
        {
748
        {
340
            ATA_TBUF_START = buffer;
749
            ata_write_cbr(&ATA_PIO_SCR, (cnt << 3) & 0xff);
341
            ATA_TBUF_SIZE = SECTOR_SIZE * cnt;
750
            ata_write_cbr(&ATA_PIO_LHR, (sector >> 13) & 0xff);
342
            ATA_CFG &= ~BIT(4);
-
 
343
        }
-
 
344
        ATA_XFR_NUM = SECTOR_SIZE * cnt - 1;
751
            ata_write_cbr(&ATA_PIO_LMR, (sector >> 5) & 0xff);
345
        ATA_CFG |= ata_dma_flags;
-
 
346
        ATA_CFG &= ~(BIT(7) | BIT(8));
-
 
347
        wakeup_wait(&ata_wakeup, TIMEOUT_NONE);
752
            ata_write_cbr(&ATA_PIO_LLR, (sector << 3) & 0xff);
348
        ATA_IRQ = BITRANGE(0, 4);
753
            ata_write_cbr(&ATA_PIO_DVR, BIT(6) | ((sector >> 21) & 0xf));
349
        ATA_IRQ_MASK = BIT(0);
-
 
350
        ATA_COMMAND = BIT(0);
-
 
351
        if (wakeup_wait(&ata_wakeup, 500000) == THREAD_TIMEOUT)
754
            if (write) ata_write_cbr(&ATA_PIO_CSD, ata_dma ? 0xca : 0x30);
352
        {
-
 
353
            ATA_COMMAND = BIT(1);
-
 
354
            ATA_CFG &= ~(BITRANGE(2, 3) | BIT(12));
755
            else ata_write_cbr(&ATA_PIO_CSD, ata_dma ? 0xc8 : 0xc4);
355
            RET_ERR(2);
-
 
356
        }
756
        }
357
        ATA_COMMAND = BIT(1);
-
 
358
        ATA_CFG &= ~(BITRANGE(2, 3) | BIT(12));
-
 
359
    }
-
 
360
    else
-
 
361
    {
-
 
362
        cnt *= SECTOR_SIZE / 512;
-
 
363
        while (cnt--)
757
        if (ata_dma)
364
        {
758
        {
365
            int i;
-
 
366
            PASS_RC(ata_wait_for_start_of_transfer(500000), 2, 1);
759
            PASS_RC(ata_wait_for_start_of_transfer(500000), 2, 1);
367
            if (write)
760
            if (write)
-
 
761
            {
368
                for (i = 0; i < 256; i++)
762
                ATA_SBUF_START = buffer;
369
                    ata_write_cbr(&ATA_PIO_DTR, ((uint16_t*)buffer)[i]);
763
                ATA_SBUF_SIZE = SECTOR_SIZE * cnt;
-
 
764
                ATA_CFG |= BIT(4);
-
 
765
            }
370
            else
766
            else
-
 
767
            {
-
 
768
                ATA_TBUF_START = buffer;
-
 
769
                ATA_TBUF_SIZE = SECTOR_SIZE * cnt;
371
                for (i = 0; i < 256; i++)
770
                ATA_CFG &= ~BIT(4);
-
 
771
            }
-
 
772
            ATA_XFR_NUM = SECTOR_SIZE * cnt - 1;
-
 
773
            ATA_CFG |= ata_dma_flags;
-
 
774
            ATA_CFG &= ~(BIT(7) | BIT(8));
-
 
775
            wakeup_wait(&ata_wakeup, TIMEOUT_NONE);
-
 
776
            ATA_IRQ = BITRANGE(0, 4);
-
 
777
            ATA_IRQ_MASK = BIT(0);
-
 
778
            ATA_COMMAND = BIT(0);
372
                    ((uint16_t*)buffer)[i] = ata_read_cbr(&ATA_PIO_DTR);
779
            if (wakeup_wait(&ata_wakeup, 500000) == THREAD_TIMEOUT)
-
 
780
            {
-
 
781
                ATA_COMMAND = BIT(1);
-
 
782
                ATA_CFG &= ~(BITRANGE(2, 3) | BIT(12));
373
            buffer += 512;
783
                RET_ERR(2);
-
 
784
            }
-
 
785
            ATA_COMMAND = BIT(1);
-
 
786
            ATA_CFG &= ~(BITRANGE(2, 3) | BIT(12));
374
        }
787
        }
-
 
788
        else
-
 
789
        {
-
 
790
            cnt *= SECTOR_SIZE / 512;
-
 
791
            while (cnt--)
-
 
792
            {
-
 
793
                int i;
-
 
794
                PASS_RC(ata_wait_for_start_of_transfer(500000), 2, 1);
-
 
795
                if (write)
-
 
796
                    for (i = 0; i < 256; i++)
-
 
797
                        ata_write_cbr(&ATA_PIO_DTR, ((uint16_t*)buffer)[i]);
-
 
798
                else
-
 
799
                    for (i = 0; i < 256; i++)
-
 
800
                        ((uint16_t*)buffer)[i] = ata_read_cbr(&ATA_PIO_DTR);
-
 
801
                buffer += 512;
-
 
802
            }
-
 
803
        }
-
 
804
        PASS_RC(ata_wait_for_end_of_transfer(100000), 2, 3);
375
    }
805
    }
376
    PASS_RC(ata_wait_for_end_of_transfer(100000), 2, 3);
-
 
377
    return 0;
806
    return 0;
378
}
807
}
379
 
808
 
380
#ifdef ATA_HAVE_BBT
809
#ifdef ATA_HAVE_BBT
381
int ata_bbt_translate(uint64_t sector, uint32_t count, uint64_t* phys, uint32_t* physcount)
810
int ata_bbt_translate(uint64_t sector, uint32_t count, uint64_t* phys, uint32_t* physcount)
Line 463... Line 892...
463
    if (sector + count > ata_total_sectors) RET_ERR(0);
892
    if (sector + count > ata_total_sectors) RET_ERR(0);
464
    if (!ata_powered) ata_power_up();
893
    if (!ata_powered) ata_power_up();
465
    ata_set_active();
894
    ata_set_active();
466
    if (ata_dma && write) clean_dcache();
895
    if (ata_dma && write) clean_dcache();
467
    else if (ata_dma) invalidate_dcache();
896
    else if (ata_dma) invalidate_dcache();
468
    ATA_COMMAND = BIT(1);
897
    if (!ceata) ATA_COMMAND = BIT(1);
469
    while (count)
898
    while (count)
470
    {
899
    {
471
        uint32_t cnt = MIN(ata_lba48 ? 8192 : 32, count);
900
        uint32_t cnt = MIN(ata_lba48 ? 8192 : 32, count);
472
        int rc = -1;
901
        int rc = -1;
473
        rc = ata_rw_chunk(sector, cnt, buffer, write);
902
        rc = ata_rw_chunk(sector, cnt, buffer, write);
Line 511... Line 940...
511
}
940
}
512
 
941
 
513
/* API Functions */
942
/* API Functions */
514
int ata_soft_reset()
943
int ata_soft_reset()
515
{
944
{
-
 
945
    int rc;
516
    mutex_lock(&ata_mutex, TIMEOUT_BLOCK);
946
    mutex_lock(&ata_mutex, TIMEOUT_BLOCK);
517
    if (!ata_powered) ata_power_up();
947
    if (!ata_powered) ata_power_up();
518
    ata_set_active();
948
    ata_set_active();
-
 
949
    if (ceata) rc = ceata_soft_reset();
-
 
950
    else
-
 
951
    {
519
    ata_write_cbr(&ATA_PIO_DAD, BIT(1) | BIT(2));
952
        ata_write_cbr(&ATA_PIO_DAD, BIT(1) | BIT(2));
520
    sleep(10);
953
        sleep(10);
521
    ata_write_cbr(&ATA_PIO_DAD, 0);
954
        ata_write_cbr(&ATA_PIO_DAD, 0);
522
    int rc = ata_wait_for_rdy(20000000);
955
        rc = ata_wait_for_rdy(20000000);
-
 
956
    }
523
    if (IS_ERR(rc))
957
    if (IS_ERR(rc))
524
    {
958
    {
525
        ata_power_down();
959
        ata_power_down();
526
        sleep(3000000);
960
        sleep(3000000);
527
        ata_power_up();
961
        ata_power_up();
Line 656... Line 1090...
656
 
1090
 
657
int ata_init(void)
1091
int ata_init(void)
658
{
1092
{
659
    mutex_init(&ata_mutex);
1093
    mutex_init(&ata_mutex);
660
    wakeup_init(&ata_wakeup);
1094
    wakeup_init(&ata_wakeup);
-
 
1095
    wakeup_init(&mmc_wakeup);
-
 
1096
    wakeup_init(&mmc_comp_wakeup);
-
 
1097
    ceata = PDAT(11) & BIT(1);
-
 
1098
    if (ceata)
-
 
1099
    {
-
 
1100
        ata_lba48 = true;
-
 
1101
        ata_dma = true;
-
 
1102
        PCON(8) = 0x33333333;
-
 
1103
        PCON(9) = (PCON(9) & ~0xff) | 0x33;
-
 
1104
        PCON(11) |= 0xf;
-
 
1105
        *((uint32_t volatile*)0x38a00000) = 0;
-
 
1106
        *((uint32_t volatile*)0x38700000) = 0;
-
 
1107
    }
-
 
1108
    else
-
 
1109
    {
661
    PCON(7) = 0x44444444;
1110
        PCON(7) = 0x44444444;
662
    PCON(8) = 0x44444444;
1111
        PCON(8) = 0x44444444;
663
    PCON(9) = 0x44444444;
1112
        PCON(9) = 0x44444444;
664
    PCON(10) = (PCON(10) & ~0xffff) | 0x4444;
1113
        PCON(10) = (PCON(10) & ~0xffff) | 0x4444;
-
 
1114
    }
665
    ata_powered = false;
1115
    ata_powered = false;
666
    ata_total_sectors = 0;
1116
    ata_total_sectors = 0;
667
#ifdef ATA_HAVE_BBT
1117
#ifdef ATA_HAVE_BBT
668
    ata_bbt_reload();
1118
    ata_bbt_reload();
669
#endif
1119
#endif
Line 687... Line 1137...
687
    uint32_t ata_irq = ATA_IRQ;
1137
    uint32_t ata_irq = ATA_IRQ;
688
    ATA_IRQ = ata_irq;
1138
    ATA_IRQ = ata_irq;
689
    if (ata_irq & ATA_IRQ_MASK) wakeup_signal(&ata_wakeup);
1139
    if (ata_irq & ATA_IRQ_MASK) wakeup_signal(&ata_wakeup);
690
    ATA_IRQ_MASK = 0;
1140
    ATA_IRQ_MASK = 0;
691
}
1141
}
-
 
1142
 
-
 
1143
void INT_MMC()
-
 
1144
{
-
 
1145
    uint32_t irq = SDCI_IRQ;
-
 
1146
    if (irq & SDCI_IRQ_DAT_DONE_INT) wakeup_signal(&mmc_wakeup);
-
 
1147
    if (irq & SDCI_IRQ_IOCARD_IRQ_INT) wakeup_signal(&mmc_comp_wakeup);
-
 
1148
    SDCI_IRQ = irq;
-
 
1149
}