1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 * Copyright (C) 2005 Tomasz Malesinski
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.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
26 #if CONFIG_CPU == PNX0101
43 #define SECTOR_SIZE (512)
45 static unsigned short identify_info
[SECTOR_SIZE
];
46 int ata_spinup_time
= 0;
47 long last_disk_activity
= -1;
49 #if CONFIG_FLASH == FLASH_IFP7XX
50 static unsigned char flash_ce
[4] = {0x20, 0x02, 0x10, 0x08};
52 #define FLASH_IO_BASE 0x28000000
53 #define FLASH_REG_DATA (*((volatile unsigned char*)(FLASH_IO_BASE)))
54 #define FLASH_REG_CMD (*((volatile unsigned char*)(FLASH_IO_BASE + 4)))
55 #define FLASH_REG_ADDR (*((volatile unsigned char*)(FLASH_IO_BASE + 8)))
57 #define SEGMENT_SIZE 1000
58 #define MAX_N_SEGMENTS 8
62 #define FLASH_MODEL_NONE 0
63 #define FLASH_MODEL_256 1
64 #define FLASH_MODEL_512 2
68 unsigned short block_map
[MAX_N_SEGMENTS
][SEGMENT_SIZE
];
70 int cur_phblock_start
;
72 unsigned char chip_no
[4];
76 static struct flash_disk flash_disk
;
78 void flash_select_chip(int no
, int sel
)
80 #if CONFIG_FLASH == FLASH_IFP7XX
82 GPIO5_CLR
= flash_ce
[no
];
84 GPIO5_SET
= flash_ce
[no
];
88 static inline unsigned char flash_read_data(void)
90 return FLASH_REG_DATA
;
93 static inline void flash_write_data(unsigned char data
)
95 FLASH_REG_DATA
= data
;
98 /* TODO: these two doesn't work when inlined, probably some
101 static void flash_write_cmd(unsigned char cmd
)
106 static void flash_write_addr(unsigned char addr
)
108 FLASH_REG_ADDR
= addr
;
111 static void flash_wait_ready(void)
114 for (i
= 0; i
< 5; i
++)
115 while ((GPIO6_READ
& 8) == 0);
118 static unsigned char model_n_sectors_order
[] = {0, 19, 20};
120 int flash_map_sector(int sector
, int* chip
, int* chip_sector
)
123 if (flash_disk
.model
== FLASH_MODEL_NONE
)
126 ord
= model_n_sectors_order
[flash_disk
.model
];
128 *chip_sector
= sector
& ((1 << ord
) - 1);
130 if (c
>= flash_disk
.n_chips
)
133 *chip
= flash_disk
.chip_no
[c
];
137 int flash_read_id(int no
) {
140 flash_select_chip(no
, 1);
141 flash_write_cmd(0x90);
145 id
= flash_read_data();
147 flash_select_chip(no
, 0);
151 int flash_read_sector(int sector
, unsigned char* buf
,
154 unsigned long *bufl
= (unsigned long *)buf
;
155 int chip
, chip_sector
;
158 if (flash_map_sector(sector
, &chip
, &chip_sector
) < 0)
161 flash_select_chip(chip
, 1);
163 flash_write_cmd(0x00);
165 flash_write_addr((chip_sector
<< 1) & 7);
166 flash_write_addr((chip_sector
>> 2) & 0xff);
167 flash_write_addr((chip_sector
>> 10) & 0xff);
168 flash_write_addr((chip_sector
>> 18) & 0xff);
169 flash_write_cmd(0x30);
173 if ((unsigned long)buf
& 3)
175 for (i
= 0; i
< 512; i
++)
176 buf
[i
] = flash_read_data();
180 for (i
= 0; i
< 512 / 4; i
++) {
182 #ifdef ROCKBOX_LITTLE_ENDIAN
183 v
= flash_read_data();
184 v
|= (unsigned long)flash_read_data() << 8;
185 v
|= (unsigned long)flash_read_data() << 16;
186 v
|= (unsigned long)flash_read_data() << 24;
188 v
= (unsigned long)flash_read_data() << 24;
189 v
|= (unsigned long)flash_read_data() << 16;
190 v
|= (unsigned long)flash_read_data() << 8;
191 v
|= flash_read_data();
197 flash_write_cmd(0x05);
198 flash_write_addr((chip_sector
& 3) * 0x10);
200 flash_write_cmd(0xe0);
202 for (i
= 0; i
< 16; i
++)
203 oob
[i
] = flash_read_data();
205 flash_select_chip(chip
, 0);
209 int flash_read_sector_oob(int sector
, unsigned char* oob
)
211 int chip
, chip_sector
;
214 if (flash_map_sector(sector
, &chip
, &chip_sector
) < 0)
217 flash_select_chip(chip
, 1);
219 flash_write_cmd(0x00);
220 flash_write_addr((chip_sector
& 3) * 0x10);
222 flash_write_addr((chip_sector
>> 2) & 0xff);
223 flash_write_addr((chip_sector
>> 10) & 0xff);
224 flash_write_addr((chip_sector
>> 18) & 0xff);
225 flash_write_cmd(0x30);
229 for (i
= 0; i
< 16; i
++)
230 oob
[i
] = flash_read_data();
232 flash_select_chip(chip
, 0);
236 static unsigned char model_n_segments
[] = {0, 2, 4};
238 static inline int flash_get_n_segments(void)
240 return model_n_segments
[flash_disk
.model
] * flash_disk
.n_chips
;
243 static inline int flash_get_n_phblocks(void)
248 static int model_n_sectors_in_block
[] = {0, 256, 256};
250 static int flash_get_n_sectors_in_block(void)
252 return model_n_sectors_in_block
[flash_disk
.model
];
255 static int flash_phblock_to_sector(int segment
, int block
)
257 return (segment
* flash_get_n_phblocks() + block
)
258 * flash_get_n_sectors_in_block();
261 static int flash_is_bad_block(unsigned char* oob
)
263 /* TODO: should we check two pages? (see datasheet) */
264 return oob
[0] != 0xff;
267 static int count_1(int n
) {
276 static int flash_get_logical_block_no(unsigned char* oob
)
279 no1
= oob
[6] + (oob
[7] << 8);
280 no2
= oob
[11] + (oob
[12] << 8);
282 if (no1
== no2
&& (no1
& 0xf000) == 0x1000)
283 return (no1
& 0xfff) >> 1;
285 if (count_1(no1
^ no2
) > 1)
288 if ((no1
& 0xf000) == 0x1000
289 && (count_1(no1
) & 1) == 0)
290 return (no1
& 0xfff) >> 1;
292 if ((no2
& 0xf000) == 0x1000
293 && (count_1(no2
) & 1) == 0)
294 return (no2
& 0xfff) >> 1;
299 int flash_disk_scan(void)
301 int n_segments
, n_phblocks
;
302 unsigned char oob
[16];
305 /* TODO: checking for double blocks */
307 n_segments
= flash_get_n_segments();
308 n_phblocks
= flash_get_n_phblocks();
310 flash_disk
.cur_block
= -1;
311 flash_disk
.cur_phblock_start
= -1;
313 for (s
= 0; s
< n_segments
; s
++)
315 for (b
= 0; b
< n_phblocks
; b
++)
318 r
= flash_read_sector_oob(flash_phblock_to_sector(s
, b
),
320 if (r
>= 0 && !flash_is_bad_block(oob
))
323 lb
= flash_get_logical_block_no(oob
);
324 if (lb
>= 0 && lb
< SEGMENT_SIZE
)
325 flash_disk
.block_map
[s
][lb
] = b
;
332 int flash_disk_find_block(int block
)
335 unsigned char oob
[16];
338 if (block
>= SEGMENT_SIZE
* flash_get_n_segments())
341 if (block
== flash_disk
.cur_block
)
342 return flash_disk
.cur_phblock_start
;
344 seg
= block
/ SEGMENT_SIZE
;
345 bmod
= block
% SEGMENT_SIZE
;
347 phb
= flash_disk
.block_map
[seg
][bmod
];
348 r
= flash_read_sector_oob(flash_phblock_to_sector(seg
, phb
), oob
);
351 if (flash_is_bad_block(oob
))
353 if (flash_get_logical_block_no(oob
) != bmod
)
356 flash_disk
.cur_block
= block
;
357 flash_disk
.cur_phblock_start
= flash_phblock_to_sector(seg
, phb
);
358 return flash_disk
.cur_phblock_start
;
361 int flash_disk_read_sectors(unsigned long start
,
365 int block
, secmod
, done
;
369 block
= start
/ flash_get_n_sectors_in_block();
370 secmod
= start
% flash_get_n_sectors_in_block();
372 phb
= flash_disk_find_block(block
);
374 while (count
> 0 && secmod
< flash_get_n_sectors_in_block())
377 flash_read_sector(phb
+ secmod
, buf
, oob
);
379 memset(buf
, 0, SECTOR_SIZE
);
389 int ata_read_sectors(IF_MV2(int drive
,)
396 int done
= flash_disk_read_sectors(start
, incount
, inbuf
);
401 inbuf
+= SECTOR_SIZE
* done
;
406 int ata_write_sectors(IF_MV2(int drive
,)
417 /* schedule a single sector write, executed with the the next spinup
418 (volume 0 only, used for config sector) */
419 extern void ata_delayed_write(unsigned long sector
, const void* buf
)
425 /* write the delayed sector to volume 0 */
426 extern void ata_flush(void)
431 void ata_spindown(int seconds
)
436 bool ata_disk_is_active(void)
449 /* Hardware reset protocol as specified in chapter 9.1, ATA spec draft v5 */
450 int ata_hard_reset(void)
455 int ata_soft_reset(void)
460 void ata_enable(bool on
)
465 unsigned short* ata_get_identify(void)
467 return identify_info
;
474 id
= flash_read_id(0);
478 flash_disk
.model
= FLASH_MODEL_256
;
481 flash_disk
.model
= FLASH_MODEL_512
;
484 flash_disk
.model
= FLASH_MODEL_NONE
;
488 flash_disk
.n_chips
= 1;
489 flash_disk
.chip_no
[0] = 0;
490 for (i
= 1; i
< 4; i
++)
492 id2
= flash_read_id(i
);
494 flash_disk
.chip_no
[flash_disk
.n_chips
++] = i
;
497 if (flash_disk_scan() < 0)