-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathata2.c
462 lines (397 loc) · 12.9 KB
/
ata2.c
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
/*
* Basic ATA2 driver for the ipodlinux bootloader
*
* Supports:
* PIO (Polling)
* Multiple block reads
*
* Author: James Jacobsson ( [email protected] )
*
* ATA2 code modified to support double sector reads as a single block for the
* 5.5G 80GB iPod - by Vincent Huisman ( dataghost at dataghost dot com )
* at 2007-01-23
*
*/
#include "bootloader.h"
#include "console.h"
#include "ipodhw.h"
#include "minilibc.h"
#include "ata2.h"
#define REG_DATA 0x0
#define REG_ERROR 0x1
#define REG_FEATURES 0x1
#define REG_SECT_COUNT 0x2
#define REG_SECT 0x3
#define REG_CYL_LOW 0x4
#define REG_CYL_HIGH 0x5
#define REG_DEVICEHEAD 0x6
#define REG_STATUS 0x7
#define REG_COMMAND 0x7
#define REG_CONTROL 0x8
#define REG_ALTSTATUS 0x8
#define REG_DA 0x9
#define CONTROL_NIEN 0x2
#define CONTROL_SRST 0x4
// all commands: see include/linux/hdreg.h
#define COMMAND_IDENTIFY_DEVICE 0xEC
#define COMMAND_READ_MULTIPLE 0xC4
#define COMMAND_READ_SECTORS 0x20
#define COMMAND_READ_SECTORS_VRFY 0x21
#define COMMAND_STANDBY 0xE0
#define DEVICE_0 0xA0
#define DEVICE_1 0xB0
#define STATUS_BSY 0x80
#define STATUS_DRDY 0x40
#define STATUS_DF 0x20
#define STATUS_DSC 0x10
#define STATUS_DRQ 0x08
#define STATUS_CORR 0x04
#define STATUS_IDX 0x02
#define STATUS_ERR 0x01
unsigned int pio_base_addr1,pio_base_addr2;
unsigned int pio_reg_addrs[10];
/*
* To keep memory usage at the same level, 8 blocks of 1024 instead of 16 of 512
* Blocksize _NECESSARY_ for 1024b-sector-devices, unless uncached reads are used
* Maybe this needs to be worked around in some way
*/
#define CACHE_NUMBLOCKS 8
#define CACHE_BLOCKSIZE 1024
static uint8 *cachedata;
static uint32 *cacheaddr;
static uint32 *cachetick;
static uint32 cacheticks;
static uint8 drivetype = 0;
static uint8 readcommand = COMMAND_READ_SECTORS_VRFY;
static uint8 sectorcount = 1;
static struct {
uint16 chs[3];
uint32 sectors;
} ATAdev;
void pio_outbyte(unsigned int addr,unsigned char data) {
outl( data, pio_reg_addrs[ addr ] );
}
volatile unsigned char pio_inbyte( unsigned int addr ) {
return( inl( pio_reg_addrs[ addr ] ) );
}
volatile unsigned short pio_inword( unsigned int addr ) {
return( inl( pio_reg_addrs[ addr ] ) );
}
volatile unsigned int pio_indword( unsigned int addr ) {
return( inl( pio_reg_addrs[ addr ] ) );
}
#define DELAY400NS { \
pio_inbyte(REG_ALTSTATUS); pio_inbyte(REG_ALTSTATUS); \
pio_inbyte(REG_ALTSTATUS); pio_inbyte(REG_ALTSTATUS); \
pio_inbyte(REG_ALTSTATUS); pio_inbyte(REG_ALTSTATUS); \
pio_inbyte(REG_ALTSTATUS); pio_inbyte(REG_ALTSTATUS); \
pio_inbyte(REG_ALTSTATUS); pio_inbyte(REG_ALTSTATUS); \
pio_inbyte(REG_ALTSTATUS); pio_inbyte(REG_ALTSTATUS); \
pio_inbyte(REG_ALTSTATUS); pio_inbyte(REG_ALTSTATUS); \
pio_inbyte(REG_ALTSTATUS); pio_inbyte(REG_ALTSTATUS); \
}
uint32 ata_init(void) {
uint8 tmp[2],i;
ipod_t *ipod;
ipod = ipod_get_hwinfo();
pio_base_addr1 = ipod->ide_base;
pio_base_addr2 = pio_base_addr1 + 0x200;
/*
* Sets up a number of "shortcuts" for us to use via the pio_ macros
* Note: The PP chips have their IO regs 4 byte aligned
*/
pio_reg_addrs[ REG_DATA ] = pio_base_addr1 + 0 * 4;
pio_reg_addrs[ REG_FEATURES ] = pio_base_addr1 + 1 * 4;
pio_reg_addrs[ REG_SECT_COUNT ] = pio_base_addr1 + 2 * 4;
pio_reg_addrs[ REG_SECT ] = pio_base_addr1 + 3 * 4;
pio_reg_addrs[ REG_CYL_LOW ] = pio_base_addr1 + 4 * 4;
pio_reg_addrs[ REG_CYL_HIGH ] = pio_base_addr1 + 5 * 4;
pio_reg_addrs[ REG_DEVICEHEAD ] = pio_base_addr1 + 6 * 4;
pio_reg_addrs[ REG_COMMAND ] = pio_base_addr1 + 7 * 4;
pio_reg_addrs[ REG_CONTROL ] = pio_base_addr2 + 6 * 4;
pio_reg_addrs[ REG_DA ] = pio_base_addr2 + 7 * 4;
/*
* Black magic
*/
if( ipod->hw_ver > 3 ) {
/* PP502x */
outl(inl(0xc3000028) | 0x20, 0xc3000028); // clear intr
outl(inl(0xc3000028) & ~0x10000000, 0xc3000028); // reset?
outl(0x10, 0xc3000000);
outl(0x80002150, 0xc3000004);
} else {
/* PP5002 */
outl(inl(0xc0003024) | 0x80, 0xc0003024);
outl(inl(0xc0003024) & ~(1<<2), 0xc0003024);
outl(0x10, 0xc0003000);
outl(0x80002150, 0xc0003004);
}
/* 1st things first, check if there is an ATA controller here
* We do this by writing values to two GP registers, and expect
* to be able to read them back
*/
pio_outbyte( REG_DEVICEHEAD, DEVICE_0 ); /* Device 0 */
DELAY400NS;
pio_outbyte( REG_SECT_COUNT, 0x55 );
pio_outbyte( REG_SECT , 0xAA );
pio_outbyte( REG_SECT_COUNT, 0xAA );
pio_outbyte( REG_SECT , 0x55 );
pio_outbyte( REG_SECT_COUNT, 0x55 );
pio_outbyte( REG_SECT , 0xAA );
tmp[0] = pio_inbyte( REG_SECT_COUNT );
tmp[1] = pio_inbyte( REG_SECT );
if( (tmp[0] != 0x55) || (tmp[1] != 0xAA) ) return(1);
/*
* Okay, we're sure there's an ATA2 controller and device, so
* lets set up the caching
*/
cachedata = (uint8 *)mlc_malloc(CACHE_NUMBLOCKS * CACHE_BLOCKSIZE);
cacheaddr = (uint32*)mlc_malloc(CACHE_NUMBLOCKS * sizeof(uint32));
cachetick = (uint32*)mlc_malloc(CACHE_NUMBLOCKS * sizeof(uint32));
cacheticks = 0;
for(i=0;i<CACHE_NUMBLOCKS;i++) {
cachetick[i] = 0; /* Time is zero */
cacheaddr[i] = -1; /* Invalid sector number */
}
return(0);
}
static void ata_clear_intr ()
{
if( ipod_get_hwinfo()->hw_ver > 3 ) {
outl(inl(0xc3000028) | 0x30, 0xc3000028); // this hopefully clears all pending intrs
} else {
outl(inl(0xc0003024) | 0x80, 0xc0003024);
}
}
void ata_exit(void)
{
ata_clear_intr ();
}
/*
* Stops (spins down) the drive
*/
void ata_standby (int cmd_variation)
{
uint8 status, cmd = COMMAND_STANDBY;
// this is just a wild guess from "tempel" - I have no idea if this is the correct way to spin a disk down
if (cmd_variation == 1) cmd = 0x94;
if (cmd_variation == 2) cmd = 0x96;
if (cmd_variation == 3) cmd = 0xE0;
if (cmd_variation == 4) cmd = 0xE2;
pio_outbyte( REG_COMMAND, cmd );
DELAY400NS;
while( pio_inbyte( REG_ALTSTATUS) & STATUS_BSY ); /* wait until drive is not busy */
status = pio_inbyte( REG_STATUS );
// The linux kernel notes mention that some drives might cause an interrupt when put to standby mode.
// This interrupt is then to be ignored.
ata_clear_intr ();
}
/*
* Copies one block of data (512 or 1024 bytes) from the device
* to host memory
*/
static void ata_transfer_block(void *ptr) {
uint32 words;
uint16 *dst;
dst = (uint16*)ptr;
if(drivetype == 1) { // 1024b sector reads
words = 512;
} else { // Default: 0 or other
words = 256;
}
while(words--) {
*dst++ = inw( pio_reg_addrs[REG_DATA] );
}
}
/*
* Detect what type of drive we are dealing with (512b-sectors default drive or
* 1024b-sectors for 5.5G 80GB (unable to read odd sectors).
* The variable drivetype is set to:
* 0: 512b sector reads with COMMAND_READ_SECTORS_VRFY (default assumption)
* 1: 2x 512b sector reads with COMMAND_READ_MULTIPLE when unable to read odd
* sectors
*/
void ata_find_transfermode(void) {
uint32 sector = 1; /* We need to read an odd sector */
uint8 status;
pio_outbyte( REG_DEVICEHEAD, (1<<6) | DEVICE_0 | ((sector & 0xF000000) >> 24) );
DELAY400NS;
pio_outbyte( REG_FEATURES , 0 );
pio_outbyte( REG_CONTROL , CONTROL_NIEN | 0x08); /* 8 = HD15 */
pio_outbyte( REG_SECT_COUNT, 1 );
pio_outbyte( REG_SECT , sector & 0xFF );
pio_outbyte( REG_CYL_LOW , (sector & 0xFF00) >> 8 );
pio_outbyte( REG_CYL_HIGH , (sector & 0xFF0000) >> 16 );
pio_outbyte( REG_COMMAND, COMMAND_READ_SECTORS_VRFY );
DELAY400NS; DELAY400NS;
while( pio_inbyte( REG_ALTSTATUS) & STATUS_BSY ); /* Spin until drive is not busy */
DELAY400NS; DELAY400NS;
status = pio_inbyte( REG_STATUS );
if ((status & (STATUS_ERR)) == STATUS_ERR) {
drivetype = 1;
readcommand = COMMAND_READ_MULTIPLE;
sectorcount = 2;
} else {
drivetype = 0;
readcommand = COMMAND_READ_SECTORS_VRFY;
sectorcount = 1;
}
#ifdef DEBUG
mlc_printf("find_trans: dt=%d\n", drivetype);
#endif
}
/*
* Does some extended identification of the ATA device
*/
void ata_identify(void) {
uint8 status,c;
uint16 *buff = (uint16*)mlc_malloc(512);
pio_outbyte( REG_DEVICEHEAD, DEVICE_0 );
pio_outbyte( REG_FEATURES , 0 );
pio_outbyte( REG_CONTROL , CONTROL_NIEN );
pio_outbyte( REG_SECT_COUNT, 0 );
pio_outbyte( REG_SECT , 0 );
pio_outbyte( REG_CYL_LOW , 0 );
pio_outbyte( REG_CYL_HIGH , 0 );
pio_outbyte( REG_COMMAND, COMMAND_IDENTIFY_DEVICE );
DELAY400NS;
while( pio_inbyte( REG_ALTSTATUS) & STATUS_BSY ); /* Spin until drive is not busy */
status = pio_inbyte( REG_STATUS );
if( status & STATUS_DRQ ) {
ata_transfer_block( buff );
ATAdev.sectors = (buff[61] << 16) + buff[60];
ATAdev.chs[0] = buff[1];
ATAdev.chs[1] = buff[3];
ATAdev.chs[2] = buff[6];
mlc_printf("ATA Device\n");
mlc_printf("Size: %uMB (%u/%u/%u)\n",ATAdev.sectors/2048,ATAdev.chs[0],ATAdev.chs[1],ATAdev.chs[2]);
mlc_printf("HDDid: ");
for(c=27;c<47;c++) {
if( buff[c] != ((' ' << 8) + ' ') ) {
mlc_printf("%c%c", buff[c]>>8, buff[c]&0xFF);
}
}
mlc_printf("\n");
} else {
mlc_printf("DRQ not set..\n");
}
/*
* Now also detect the transfermode. It's done afterwards since ata_identify
* expects to get 512 bytes instead of (possibly) 1024.
*/
ata_find_transfermode();
}
/*
* Sets up the transfer of one block of data
*/
static int ata_readblock2(void *dst, uint32 sector, int storeInCache) {
uint8 status,i,cacheindex;
uint8 secteven = 1;
static uint16 *buff = 0;
if ((!buff) && (drivetype == 1)) buff = (uint16*)mlc_malloc(1024);
if (drivetype == 1) {
if ((sector % 2) == 0) {
secteven = 1;
} else {
secteven = 0;
sector--;
}
}
/*
* Check if we have this block in cache first
*/
if (sector != 0) { /* Never EVER try to read sector 0 from cache, it won't be there or needed anyway */
for(i=0;i<CACHE_NUMBLOCKS;i++) {
if( cacheaddr[i] == sector ) {
if ((drivetype == 0) || (secteven == 1)) {
mlc_memcpy(dst,cachedata + CACHE_BLOCKSIZE*i,512); /* We did.. No need to bother the ATA controller */
} else { /* drivetype = 1 && secteven == 0 */
mlc_memcpy(dst,cachedata + CACHE_BLOCKSIZE*i+512,512);
}
cacheticks++;
cachetick[i] = cacheticks;
return(0);
}
}
}
/*
* Okay, it wasnt in cache.. We need to figure out which block
* to replace in the cache. Lets use a simple LRU
*/
cacheindex = 0;
if (storeInCache) {
for(i=0;i<CACHE_NUMBLOCKS;i++) {
if( cachetick[i] < cachetick[cacheindex] ) cacheindex = i;
}
cachetick[cacheindex] = cacheticks;
}
pio_outbyte( REG_DEVICEHEAD, (1<<6) | DEVICE_0 | ((sector & 0xF000000) >> 24) );
DELAY400NS;
pio_outbyte( REG_FEATURES , 0 );
pio_outbyte( REG_CONTROL , CONTROL_NIEN | 0x08); /* 8 = HD15 */
pio_outbyte( REG_SECT_COUNT, sectorcount );
pio_outbyte( REG_SECT , sector & 0xFF );
pio_outbyte( REG_CYL_LOW , (sector & 0xFF00) >> 8 );
pio_outbyte( REG_CYL_HIGH , (sector & 0xFF0000) >> 16 );
pio_outbyte( REG_COMMAND, readcommand );
DELAY400NS; DELAY400NS;
while( pio_inbyte( REG_ALTSTATUS) & STATUS_BSY ); /* Spin until drive is not busy */
DELAY400NS; DELAY400NS;
status = pio_inbyte( REG_STATUS );
if( (status & (STATUS_BSY | STATUS_DRQ)) == STATUS_DRQ) {
if (storeInCache) {
cacheaddr[cacheindex] = sector;
ata_transfer_block(cachedata + cacheindex * CACHE_BLOCKSIZE);
if ((drivetype == 0) || (secteven == 1)) {
mlc_memcpy(dst,cachedata + cacheindex*CACHE_BLOCKSIZE,512);
} else { /* drivetype == 1 && secteven == 0 */
mlc_memcpy(dst,cachedata + cacheindex*CACHE_BLOCKSIZE+512, 512);
}
cacheticks++;
} else {
if (drivetype == 0) {
ata_transfer_block(dst);
} else { /* drivetype == 1 */
ata_transfer_block(buff);
if (secteven == 1) {
mlc_memcpy(dst,buff,512);
} else {
mlc_memcpy(dst,buff+256,512);
}
}
}
} else {
mlc_printf("\nATA2 IO Error\n");
status = pio_inbyte( REG_ERROR );
mlc_printf("Error reg: %u\n",status);
mlc_printf("dst: %lx, blk: %ld\n", dst, sector);
mlc_show_fatal_error ();
}
return(0);
}
int ata_readblock(void *dst, uint32 sector) {
return ata_readblock2(dst, sector, 1);
}
int ata_readblocks(void *dst,uint32 sector,uint32 count) {
/* Replace this with COMMAND_READ_MULTIPLE for FAT32 speedups: */
int err;
while (count-- > 0) {
err = ata_readblock2 (dst, sector++, 1);
if (err) return err;
dst = (char*)dst + 512;
}
return 0;
}
int ata_readblocks_uncached (void *dst, uint32 sector, uint32 count) {
/* Replace this with COMMAND_READ_MULTIPLE for FAT32 speedups: */
int err;
while (count-- > 0) {
err = ata_readblock2 (dst, sector++, 0);
if (err) return err;
dst = (char*)dst + 512;
}
return 0;
}
uint8 ata_get_drivetype (void) {
return drivetype;
}