summaryrefslogtreecommitdiffstats
path: root/firmware/drivers/ata_flash.c
blob: 17796e0d50fb758eb2bd4d6313c517f6e97d7043 (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
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
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * Copyright (C) 2005 Tomasz Malesinski
 *
 * 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 <stdbool.h>
#include <string.h>

#if CONFIG_CPU == PNX0101
#include "pnx0101.h"
#endif

/*
#include "kernel.h"
#include "thread.h"
#include "led.h"
#include "cpu.h"
#include "system.h"
#include "debug.h"
#include "panic.h"
#include "usb.h"
#include "power.h"
#include "string.h"
#include "hwcompat.h"
*/

#define SECTOR_SIZE     (512)

static unsigned short identify_info[SECTOR_SIZE];
int ata_spinup_time = 0;
long last_disk_activity = -1;

#if CONFIG_FLASH == FLASH_IFP7XX
static unsigned char flash_ce[4] = {0x20, 0x02, 0x10, 0x08};

#define FLASH_IO_BASE 0x28000000
#define FLASH_REG_DATA (*((volatile unsigned char*)(FLASH_IO_BASE)))
#define FLASH_REG_CMD  (*((volatile unsigned char*)(FLASH_IO_BASE + 4)))
#define FLASH_REG_ADDR (*((volatile unsigned char*)(FLASH_IO_BASE + 8)))

#define SEGMENT_SIZE 1000
#define MAX_N_SEGMENTS 2

#endif

#define FLASH_MODEL_NONE 0
#define FLASH_MODEL_256 1

struct flash_disk
{
    short model;
    unsigned short block_map[MAX_N_SEGMENTS][SEGMENT_SIZE];
    short cur_block;
    int cur_phblock_start;
};

static struct flash_disk flash_disk;

void flash_select_chip(int no, int sel)
{
#if CONFIG_FLASH == FLASH_IFP7XX
    if (sel)
        GPIO5_CLR = flash_ce[no];
    else
        GPIO5_SET = flash_ce[no];
#endif
}

unsigned char flash_read_data(void)
{
    return FLASH_REG_DATA;
}

void flash_write_data(unsigned char data)
{
    FLASH_REG_DATA = data;
}

void flash_write_cmd(unsigned char cmd)
{
    FLASH_REG_CMD = cmd;
}

void flash_write_addr(unsigned char addr)
{
    FLASH_REG_ADDR = addr;
}

void flash_wait_ready(void)
{
    int i;
    for (i = 0; i < 5; i++)
        while ((GPIO6_READ & 8) == 0);
}

int flash_map_sector(int sector, int* chip, int* chip_sector)
{
    switch (flash_disk.model)
    {
        case FLASH_MODEL_NONE:
        default:
            return -1;
        case FLASH_MODEL_256:
            *chip = 0;
            *chip_sector = sector;
            return 0;
    }
}

int flash_read_id(int no) {
    int id;

    flash_select_chip(no, 1);
    flash_write_cmd(0x90);
    flash_write_addr(0);

    flash_read_data();
    id = flash_read_data();

    flash_select_chip(no, 0);
    return id;
}

int flash_read_sector(int sector, unsigned char* buf,
                      unsigned char* oob)
{
    int chip, chip_sector;
    int i;
    
    if (flash_map_sector(sector, &chip, &chip_sector) < 0)
        return -1;

    flash_select_chip(chip, 1);

    flash_write_cmd(0x00);
    flash_write_addr(0);
    flash_write_addr((chip_sector << 1) & 7);
    flash_write_addr((chip_sector >> 2) & 0xff);
    flash_write_addr((chip_sector >> 10) & 0xff);
    flash_write_addr((chip_sector >> 18) & 0xff);
    flash_write_cmd(0x30);
    
    flash_wait_ready();
    
    for (i = 0; i < 512; i++)
        buf[i] = flash_read_data();

    flash_write_cmd(0x05);
    flash_write_addr((chip_sector & 3) * 0x10);
    flash_write_addr(8);
    flash_write_cmd(0xe0);

    for (i = 0; i < 16; i++)
        oob[i] = flash_read_data();

    flash_select_chip(chip, 0);
    return 0;
}

int flash_read_sector_oob(int sector, unsigned char* oob)
{
    int chip, chip_sector;
    int i;
    
    if (flash_map_sector(sector, &chip, &chip_sector) < 0)
        return -1;

    flash_select_chip(chip, 1);

    flash_write_cmd(0x00);
    flash_write_addr((chip_sector & 3) * 0x10);
    flash_write_addr(8);
    flash_write_addr((chip_sector >> 2) & 0xff);
    flash_write_addr((chip_sector >> 10) & 0xff);
    flash_write_addr((chip_sector >> 18) & 0xff);
    flash_write_cmd(0x30);
    
    flash_wait_ready();
    
    for (i = 0; i < 16; i++)
        oob[i] = flash_read_data();

    flash_select_chip(chip, 0);
    return 0;
}

static unsigned char model_n_segments[] = {0, 2};

static int flash_get_n_segments(void)
{
    return model_n_segments[flash_disk.model];
}

static int flash_get_n_phblocks(void)
{
    return 1024;
}

static int model_n_sectors_in_block[] = {0, 256};

static int flash_get_n_sectors_in_block(void)
{
    return model_n_sectors_in_block[flash_disk.model];
}

static int flash_phblock_to_sector(int segment, int block)
{
    return (segment * flash_get_n_phblocks() + block)
        * flash_get_n_sectors_in_block();
}

static int flash_is_bad_block(unsigned char* oob)
{
    /* TODO: should we check two pages? (see datasheet) */
    return oob[0] != 0xff;
}

static int count_1(int n) {
    int r = 0;
    while (n != 0) {
        r += (n & 1);
        n >>= 1;
    }
    return r;
}

static int flash_get_logical_block_no(unsigned char* oob)
{
    int no1, no2;
    no1 = oob[6] + (oob[7] << 8);
    no2 = oob[11] + (oob[12] << 8);

    if (no1 == no2 && (no1 & 0xf000) == 0x1000)
        return (no1 & 0xfff) >> 1;

    if (count_1(no1 ^ no2) > 1)
        return -1;

    if ((no1 & 0xf000) == 0x1000
        && (count_1(no1) & 1) == 0)
        return (no1 & 0xfff) >> 1;

    if ((no2 & 0xf000) == 0x1000
        && (count_1(no2) & 1) == 0)
        return (no2 & 0xfff) >> 1;

    return -1;
}

int flash_disk_scan(void)
{
    int n_segments, n_phblocks;
    unsigned char oob[16];
    int s, b;
    
    /* TODO: checking for double blocks */

    n_segments = flash_get_n_segments();
    n_phblocks = flash_get_n_phblocks();

    flash_disk.cur_block = -1;
    flash_disk.cur_phblock_start = -1;

    for (s = 0; s < n_segments; s++)
    {
        for (b = 0; b < n_phblocks; b++)
        {
            int r;
            r = flash_read_sector_oob(flash_phblock_to_sector(s, b),
                                      oob);
            if (r >= 0 && !flash_is_bad_block(oob))
            {
                int lb;
                lb = flash_get_logical_block_no(oob);
                if (lb >= 0 && lb < SEGMENT_SIZE)
                    flash_disk.block_map[s][lb] = b;
            }
        }
    }
    return 0;
}

int flash_disk_find_block(int block)
{
    int seg, bmod, phb;
    unsigned char oob[16];
    int r;
   
    if (block >= SEGMENT_SIZE * flash_get_n_segments())
        return -1;

    if (block == flash_disk.cur_block)
        return flash_disk.cur_phblock_start;

    seg = block / SEGMENT_SIZE;
    bmod = block % SEGMENT_SIZE;

    phb = flash_disk.block_map[seg][bmod];
    r = flash_read_sector_oob(flash_phblock_to_sector(seg, phb), oob);
    if (r < 0)
        return -1;
    if (flash_is_bad_block(oob))
        return -1;
    if (flash_get_logical_block_no(oob) != bmod)
        return -1;

    flash_disk.cur_block = block;
    flash_disk.cur_phblock_start = flash_phblock_to_sector(seg, phb);
    return flash_disk.cur_phblock_start;
}

int flash_disk_read_sectors(unsigned long start,
                            int count,
                            void* buf)
{
    int block, secmod, done;
    int phb;
    char oob[16];

    block = start / flash_get_n_sectors_in_block();
    secmod = start % flash_get_n_sectors_in_block();

    phb = flash_disk_find_block(block);
    done = 0;
    while (count > 0 && secmod < flash_get_n_sectors_in_block())
    {
        if (phb >= 0)
            flash_read_sector(phb + secmod, buf, oob);
        else
            memset(buf, 0, SECTOR_SIZE);

        buf += SECTOR_SIZE;
        count--;
        secmod++;
        done++;
    }
    return done;
}

int ata_read_sectors(IF_MV2(int drive,)
                     unsigned long start,
                     int incount,
                     void* inbuf)
{
    while (incount > 0)
    {
        int done = flash_disk_read_sectors(start, incount, inbuf);
        if (done < 0)
            return -1;
        incount -= done;
        inbuf += SECTOR_SIZE * done;
    }
    return 0;
}

int ata_write_sectors(IF_MV2(int drive,)
                      unsigned long start,
                      int count,
                      const void* buf)
{
    (void)start;
    (void)count;
    (void)buf;
    return -1;
}

/* schedule a single sector write, executed with the the next spinup 
   (volume 0 only, used for config sector) */
extern void ata_delayed_write(unsigned long sector, const void* buf)
{
    (void)sector;
    (void)buf;
}

/* write the delayed sector to volume 0 */
extern void ata_flush(void)
{

}

void ata_spindown(int seconds)
{
    (void)seconds;
}

bool ata_disk_is_active(void)
{
  return 0;
}

void ata_sleep(void)
{
}

void ata_spin(void)
{
}

/* Hardware reset protocol as specified in chapter 9.1, ATA spec draft v5 */
int ata_hard_reset(void)
{
  return 0;
}

int ata_soft_reset(void)
{
  return 0;
}

void ata_enable(bool on)
{
    (void)on;
}

unsigned short* ata_get_identify(void)
{
    return identify_info;
}

int ata_init(void)
{
    int id;

    id = flash_read_id(0);
    switch (id)
    {
        case 0xda:
            flash_disk.model = FLASH_MODEL_256;
            break;
        default:
            flash_disk.model = FLASH_MODEL_NONE;
            return -1;
    }

    if (flash_disk_scan() < 0)
        return -2;

    return 0;
}