Colour targets: Revert an optimisation from almost 18 months ago that actually turned...
[Rockbox.git] / firmware / drivers / ata_flash.c
blobd77e05647b873c885b6c314320c21481645151cd
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
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 ****************************************************************************/
22 #include "ata.h"
23 #include <stdbool.h>
24 #include <string.h>
26 #if CONFIG_CPU == PNX0101
27 #include "pnx0101.h"
28 #endif
31 #include "kernel.h"
32 #include "thread.h"
33 #include "led.h"
34 #include "cpu.h"
35 #include "system.h"
36 #include "debug.h"
37 #include "panic.h"
38 #include "usb.h"
39 #include "power.h"
40 #include "string.h"
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
60 #endif
62 #define FLASH_MODEL_NONE 0
63 #define FLASH_MODEL_256 1
64 #define FLASH_MODEL_512 2
66 struct flash_disk
68 unsigned short block_map[MAX_N_SEGMENTS][SEGMENT_SIZE];
69 short cur_block;
70 int cur_phblock_start;
71 int n_chips;
72 unsigned char chip_no[4];
73 unsigned char model;
76 static struct flash_disk flash_disk;
78 void flash_select_chip(int no, int sel)
80 #if CONFIG_FLASH == FLASH_IFP7XX
81 if (sel)
82 GPIO5_CLR = flash_ce[no];
83 else
84 GPIO5_SET = flash_ce[no];
85 #endif
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
99 delay is required */
101 static void flash_write_cmd(unsigned char cmd)
103 FLASH_REG_CMD = cmd;
106 static void flash_write_addr(unsigned char addr)
108 FLASH_REG_ADDR = addr;
111 static void flash_wait_ready(void)
113 int i;
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)
122 int ord, c;
123 if (flash_disk.model == FLASH_MODEL_NONE)
124 return -1;
126 ord = model_n_sectors_order[flash_disk.model];
127 c = sector >> ord;
128 *chip_sector = sector & ((1 << ord) - 1);
130 if (c >= flash_disk.n_chips)
131 return -1;
133 *chip = flash_disk.chip_no[c];
134 return 0;
137 int flash_read_id(int no) {
138 int id;
140 flash_select_chip(no, 1);
141 flash_write_cmd(0x90);
142 flash_write_addr(0);
144 flash_read_data();
145 id = flash_read_data();
147 flash_select_chip(no, 0);
148 return id;
151 int flash_read_sector(int sector, unsigned char* buf,
152 unsigned char* oob)
154 unsigned long *bufl = (unsigned long *)buf;
155 int chip, chip_sector;
156 int i;
158 if (flash_map_sector(sector, &chip, &chip_sector) < 0)
159 return -1;
161 flash_select_chip(chip, 1);
163 flash_write_cmd(0x00);
164 flash_write_addr(0);
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);
171 flash_wait_ready();
173 if ((unsigned long)buf & 3)
175 for (i = 0; i < 512; i++)
176 buf[i] = flash_read_data();
178 else
180 for (i = 0; i < 512 / 4; i++) {
181 unsigned long v;
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;
187 #else
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();
192 #endif
193 bufl[i] = v;
197 flash_write_cmd(0x05);
198 flash_write_addr((chip_sector & 3) * 0x10);
199 flash_write_addr(8);
200 flash_write_cmd(0xe0);
202 for (i = 0; i < 16; i++)
203 oob[i] = flash_read_data();
205 flash_select_chip(chip, 0);
206 return 0;
209 int flash_read_sector_oob(int sector, unsigned char* oob)
211 int chip, chip_sector;
212 int i;
214 if (flash_map_sector(sector, &chip, &chip_sector) < 0)
215 return -1;
217 flash_select_chip(chip, 1);
219 flash_write_cmd(0x00);
220 flash_write_addr((chip_sector & 3) * 0x10);
221 flash_write_addr(8);
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);
227 flash_wait_ready();
229 for (i = 0; i < 16; i++)
230 oob[i] = flash_read_data();
232 flash_select_chip(chip, 0);
233 return 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)
245 return 1024;
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) {
268 int r = 0;
269 while (n != 0) {
270 r += (n & 1);
271 n >>= 1;
273 return r;
276 static int flash_get_logical_block_no(unsigned char* oob)
278 int no1, no2;
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)
286 return -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;
296 return -1;
299 int flash_disk_scan(void)
301 int n_segments, n_phblocks;
302 unsigned char oob[16];
303 int s, b;
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++)
317 int r;
318 r = flash_read_sector_oob(flash_phblock_to_sector(s, b),
319 oob);
320 if (r >= 0 && !flash_is_bad_block(oob))
322 int lb;
323 lb = flash_get_logical_block_no(oob);
324 if (lb >= 0 && lb < SEGMENT_SIZE)
325 flash_disk.block_map[s][lb] = b;
329 return 0;
332 int flash_disk_find_block(int block)
334 int seg, bmod, phb;
335 unsigned char oob[16];
336 int r;
338 if (block >= SEGMENT_SIZE * flash_get_n_segments())
339 return -1;
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);
349 if (r < 0)
350 return -1;
351 if (flash_is_bad_block(oob))
352 return -1;
353 if (flash_get_logical_block_no(oob) != bmod)
354 return -1;
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,
362 int count,
363 void* buf)
365 int block, secmod, done;
366 int phb;
367 char oob[16];
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);
373 done = 0;
374 while (count > 0 && secmod < flash_get_n_sectors_in_block())
376 if (phb >= 0)
377 flash_read_sector(phb + secmod, buf, oob);
378 else
379 memset(buf, 0, SECTOR_SIZE);
381 buf += SECTOR_SIZE;
382 count--;
383 secmod++;
384 done++;
386 return done;
389 int ata_read_sectors(IF_MV2(int drive,)
390 unsigned long start,
391 int incount,
392 void* inbuf)
394 while (incount > 0)
396 int done = flash_disk_read_sectors(start, incount, inbuf);
397 if (done < 0)
398 return -1;
399 start += done;
400 incount -= done;
401 inbuf += SECTOR_SIZE * done;
403 return 0;
406 int ata_write_sectors(IF_MV2(int drive,)
407 unsigned long start,
408 int count,
409 const void* buf)
411 (void)start;
412 (void)count;
413 (void)buf;
414 return -1;
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)
421 (void)sector;
422 (void)buf;
425 /* write the delayed sector to volume 0 */
426 extern void ata_flush(void)
431 void ata_spindown(int seconds)
433 (void)seconds;
436 bool ata_disk_is_active(void)
438 return 0;
441 void ata_sleep(void)
445 void ata_spin(void)
449 /* Hardware reset protocol as specified in chapter 9.1, ATA spec draft v5 */
450 int ata_hard_reset(void)
452 return 0;
455 int ata_soft_reset(void)
457 return 0;
460 void ata_enable(bool on)
462 (void)on;
465 unsigned short* ata_get_identify(void)
467 return identify_info;
470 int ata_init(void)
472 int i, id, id2;
474 id = flash_read_id(0);
475 switch (id)
477 case 0xda:
478 flash_disk.model = FLASH_MODEL_256;
479 break;
480 case 0xdc:
481 flash_disk.model = FLASH_MODEL_512;
482 break;
483 default:
484 flash_disk.model = FLASH_MODEL_NONE;
485 return -1;
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);
493 if (id2 == id)
494 flash_disk.chip_no[flash_disk.n_chips++] = i;
497 if (flash_disk_scan() < 0)
498 return -2;
500 return 0;