Subversion Repositories freemyipod

Rev

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