summaryrefslogtreecommitdiffstats
path: root/firmware/drivers/ata.c
blob: d75544ef3ae61f69016ac65c14a71627469031b0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * Copyright (C) 2002 by Alan Korr
 *
 * All files in this archive are subject to the GNU General Public License.
 * See the file COPYING in the source tree root for full license agreement.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 ****************************************************************************/
#include "ata.h"
#include "kernel.h"
#include "led.h"
#include "sh7034.h"
#include "system.h"
#include "debug.h"

#define ATA_DATA        (*((volatile unsigned short*)0x06104100))
#define ATA_ERROR       (*((volatile unsigned char*)0x06100101))
#define ATA_FEATURE     ATA_ERROR
#define ATA_NSECTOR     (*((volatile unsigned char*)0x06100102))
#define ATA_SECTOR      (*((volatile unsigned char*)0x06100103))
#define ATA_LCYL        (*((volatile unsigned char*)0x06100104))
#define ATA_HCYL        (*((volatile unsigned char*)0x06100105))
#define ATA_SELECT      (*((volatile unsigned char*)0x06100106))
#define ATA_COMMAND     (*((volatile unsigned char*)0x06100107))
#define ATA_STATUS      ATA_COMMAND
#define ATA_CONTROL     (*((volatile unsigned char*)0x06200306))
#define ATA_ALT_STATUS  ATA_CONTROL

#define SELECT_LBA      0x40

#define STATUS_BSY      0x80
#define STATUS_RDY      0x40
#define STATUS_DRQ      0x08
#define STATUS_ERR      0x01

#define CONTROL_nIEN    0x02
#define CONTROL_SRST    0x04

#define CMD_READ_SECTORS           0x20
#define CMD_WRITE_SECTORS          0x30
#define CMD_STANDBY_IMMEDIATE      0xE0
#define CMD_STANDBY                0xE2
#define CMD_SLEEP                  0xE6
#define CMD_SECURITY_FREEZE_LOCK   0xF5

static int wait_for_bsy(void)
{
    char timeout = current_tick + HZ;
    while (TIME_BEFORE(current_tick, timeout) && (ATA_ALT_STATUS & STATUS_BSY))
        yield();

    if (TIME_BEFORE(current_tick, timeout))
    {
        return 1;
    }
    else
    {
        return 0; /* timeout */
    }
}

static int wait_for_rdy(void)
{
    if (!wait_for_bsy())
        return 0;
    return ATA_ALT_STATUS & STATUS_RDY;
}

static int wait_for_start_of_transfer(void)
{
    if (!wait_for_bsy())
        return 0;
    return (ATA_ALT_STATUS & (STATUS_BSY|STATUS_DRQ)) == STATUS_DRQ;
}

static int wait_for_end_of_transfer(void)
{
    if (!wait_for_bsy())
        return 0;
    return (ATA_ALT_STATUS & (STATUS_RDY|STATUS_DRQ)) == STATUS_RDY;
}    

int ata_read_sectors(unsigned long start,
                     unsigned char count,
                     void* buf)
{
    int i;

    if (!wait_for_rdy())
        return 0;

    led(TRUE);

    ATA_NSECTOR = count;
    ATA_SECTOR  = start & 0xff;
    ATA_LCYL    = (start >> 8) & 0xff;
    ATA_HCYL    = (start >> 16) & 0xff;
    ATA_SELECT  = ((start >> 24) & 0xf) | SELECT_LBA;
    ATA_COMMAND = CMD_READ_SECTORS;

    for (i=0; i<count; i++) {
        int j;
        if (!wait_for_start_of_transfer())
            return 0;

        for (j=0; j<256; j++)
            ((unsigned short*)buf)[j] = SWAB16(ATA_DATA);

#ifdef USE_INTERRUPT
        /* reading the status register clears the interrupt */
        j = ATA_STATUS;
#endif
    }

    led(FALSE);

    return wait_for_end_of_transfer();
}

#ifdef DISK_WRITE
int ata_write_sectors(unsigned long start,
                      unsigned char count,
                      void* buf)
{
    int i;

    if (!wait_for_rdy())
        return 0;

    led(TRUE);

    ATA_NSECTOR = count;
    ATA_SECTOR  = start & 0xff;
    ATA_LCYL    = (start >> 8) & 0xff;
    ATA_HCYL    = (start >> 16) & 0xff;
    ATA_SELECT  = ((start >> 24) & 0xf) | SELECT_LBA;
    ATA_COMMAND = CMD_WRITE_SECTORS;

    for (i=0; i<count; i++) {
        int j;
        if (!wait_for_start_of_transfer())
            return 0;

        for (j=0; j<256; j++)
            ATA_DATA = SWAB16(((unsigned short*)buf)[j]);

#ifdef USE_INTERRUPT
        /* reading the status register clears the interrupt */
        j = ATA_STATUS;
#endif
    }

    led(FALSE);

    return wait_for_end_of_transfer();
}
#endif

static int check_registers(void)
{
    if ( ATA_STATUS & STATUS_BSY )
        return 0;
    
    ATA_NSECTOR = 0xa5;
    ATA_SECTOR  = 0x5a;
    ATA_LCYL    = 0xaa;
    ATA_HCYL    = 0x55;

    if ((ATA_NSECTOR == 0xa5) &&
        (ATA_SECTOR  == 0x5a) &&
        (ATA_LCYL    == 0xaa) &&
        (ATA_HCYL    == 0x55))
        return 1;
    else
        return 0;
}

static int check_harddisk(void)
{
    if (!wait_for_rdy())
        return 0;

    if ((ATA_NSECTOR == 1) &&
        (ATA_SECTOR == 1) &&
        (ATA_LCYL == 0) &&
        (ATA_HCYL == 0) &&
        (ATA_SELECT == 0))
        return 1;
    else
        return 0;
}

static int freeze_lock(void)
{
    if (!wait_for_rdy())
        return -1;

    ATA_COMMAND = CMD_SECURITY_FREEZE_LOCK;

    if (!wait_for_rdy())
        return -1;

    return 0;
}

int ata_spindown(int time)
{
    if(!wait_for_rdy())
        return -1;

    if ( time == -1 ) {
        ATA_COMMAND = CMD_STANDBY_IMMEDIATE;
    }
    else {
        if (time > 255)
            return -1;
        ATA_NSECTOR = time & 0xff;
        ATA_COMMAND = CMD_STANDBY;
    }

    if (!wait_for_rdy())
        return -1;

    return 0;
}

int ata_hard_reset(void)
{
    PADR &= ~0x0002;

    sleep(2);

    PADR |= 0x0002;

    return wait_for_rdy();
}

int ata_soft_reset(void)
{
    ATA_SELECT = SELECT_LBA;
    ATA_CONTROL = CONTROL_nIEN|CONTROL_SRST;
    sleep(HZ/20000); /* >= 5us */

    ATA_CONTROL = CONTROL_nIEN;
    sleep(HZ/400); /* >2ms */

    return wait_for_rdy();
}

int ata_init(void)
{
    led(FALSE);

    /* activate ATA */
    PADR &= ~0x80;

    if (!ata_hard_reset())
        return -1;
            
    if (!check_registers())
        return -2;

//    if (!check_harddisk())
//        return -3;

    if (freeze_lock() < 0)
        return -4;

//    if(ata_spindown(-1) < 0)
//        return -5;

    ATA_SELECT = SELECT_LBA;
    ATA_CONTROL = CONTROL_nIEN;

    return 0;
}