summaryrefslogtreecommitdiffstats
path: root/c/src/libchip/flash/am29lv160.c
blob: 6bbcb36845ab63007a59fb48ac6b76723a034a28 (plain) (blame)
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
463
464
465
466
467
468
469
470
471
472
/*
 * RTEMS Project (http://www.rtems.org/)
 *
 * Copyright 2007 Chris Johns (chrisj@rtems.org)
 */
/**
 * Provide flash support for the AM26LV160 device.
 *
 * The M29W160D is the same device.
 */

#include <stdio.h>
#include <errno.h>

#include <rtems.h>

#include <libchip/am29lv160.h>

#ifndef AM26LV160_ERROR_TRACE
#define AM26LV160_ERROR_TRACE (0)
#endif

/**
 * Boot blocks at the top
 */
const rtems_fdisk_segment_desc rtems_am29lv160t_segments[4] =
{
  {
    .count =   31,
    .segment = 0,
    .offset =  0x00000000,
    .size =    RTEMS_FDISK_KBYTES (64)
  },
  {
    .count =   1,
    .segment = 31,
    .offset =  0x001f0000,
    .size =    RTEMS_FDISK_KBYTES (32)
  },
  {
    .count =   2,
    .segment = 32,
    .offset =  0x001f8000,
    .size =    RTEMS_FDISK_KBYTES (8)
  },
  {
    .count =   1,
    .segment = 34,
    .offset =  0x001fc000,
    .size =    RTEMS_FDISK_KBYTES (16)
  }
};

/**
 * Boot blocks at the bottom.
 */
const rtems_fdisk_segment_desc rtems_am29lv160b_segments[] =
{
  {
    .count =   1,
    .segment = 0,
    .offset =  0x00000000,
    .size =    RTEMS_FDISK_KBYTES (16)
  },
  {
  .  count =   2,
    .segment = 1,
    .offset =  0x00004000,
    .size =    RTEMS_FDISK_KBYTES (8)
  },
  {
    .count =   1,
    .segment = 3,
    .offset =  0x00008000,
    .size =    RTEMS_FDISK_KBYTES (32)
  },
  {
    .count =   31,
    .segment = 4,
    .offset =  0x00010000,
    .size =    RTEMS_FDISK_KBYTES (64)
  }
};

static int
rtems_am29lv160_blank (const rtems_fdisk_segment_desc* sd,
                       uint32_t                        device,
                       uint32_t                        segment,
                       uint32_t                        offset,
                       uint32_t                        size)
{
  const rtems_am29lv160_config* ac = &rtems_am29lv160_configuration[device];
  volatile uint8_t*             seg_8 = ac->base;
  volatile uint32_t*            seg_32;
  uint32_t                      count;

  offset += sd->offset + (segment - sd->segment) * sd->size;

  seg_8 += offset;

  count = offset & (sizeof (uint32_t) - 1);
  size -= count;

  while (count--)
    if (*seg_8++ != 0xff)
    {
#if AM26LV160_ERROR_TRACE
      printf ("AM26LV160: blank check error: %p = 0x%02x\n",
              seg_8 - 1, *(seg_8 - 1));
#endif
      return EIO;
    }

  seg_32 = (volatile uint32_t*) seg_8;

  count = size  / sizeof (uint32_t);
  size -= count * sizeof (uint32_t);

  while (count--)
    if (*seg_32++ != 0xffffffff)
    {
#if AM26LV160_ERROR_TRACE
      printf ("AM26LV160: blank check error: %p = 0x%08lx\n",
              seg_32 - 1, *(seg_32 - 1));
#endif
      return EIO;
    }

  seg_8 = (volatile uint8_t*) seg_32;

  while (size--)
    if (*seg_8++ != 0xff)
    {
#if AM26LV160_ERROR_TRACE
      printf ("AM26LV160: blank check error: %p = 0x%02x\n",
              seg_8 - 1, *(seg_8 - 1));
#endif
      return EIO;
    }

  return 0;
}

static int
rtems_am29lv160_verify (const rtems_fdisk_segment_desc* sd,
                        uint32_t                        device,
                        uint32_t                        segment,
                        uint32_t                        offset,
                        const void*                     buffer,
                        uint32_t                        size)
{
  const rtems_am29lv160_config* ac = &rtems_am29lv160_configuration[device];
  const uint8_t*                addr = ac->base;

  addr += (sd->offset + (segment - sd->segment) * sd->size) + offset;

  if (memcmp (addr, buffer, size) != 0)
    return EIO;

  return 0;
}

static int
rtems_am29lv160_toggle_wait_8 (volatile uint8_t* status)
{
  while (1)
  {
    volatile uint8_t status1 = *status;
    volatile uint8_t status2 = *status;

    if (((status1 ^ status2) & (1 << 6)) == 0)
      return 0;

    if ((status1 & (1 << 5)) != 0)
    {
      status1 = *status;
      status2 = *status;

      if (((status1 ^ status2) & (1 << 6)) == 0)
        return 0;

#if AM26LV160_ERROR_TRACE
      printf ("AM26LV160: error bit detected: %p = 0x%04x\n",
              status, status1);
#endif

      *status = 0xf0;
      return EIO;
    }
  }
}

static int
rtems_am29lv160_toggle_wait_16 (volatile uint16_t* status)
{
  while (1)
  {
    volatile uint16_t status1 = *status;
    volatile uint16_t status2 = *status;

    if (((status1 ^ status2) & (1 << 6)) == 0)
      return 0;

    if ((status1 & (1 << 5)) != 0)
    {
      status1 = *status;
      status2 = *status;

      if (((status1 ^ status2) & (1 << 6)) == 0)
        return 0;

#if AM26LV160_ERROR_TRACE
      printf ("AM26LV160: error bit detected: %p = 0x%04x/0x%04x\n",
              status, status1, status2);
#endif

      *status = 0xf0;
      return EIO;
    }
  }
}

static int
rtems_am29lv160_write_data_8 (volatile uint8_t* base,
                              uint32_t          offset,
                              const uint8_t*    data,
                              uint32_t          size)
{
  volatile uint8_t*     seg = base + offset;
  rtems_interrupt_level level;

  /*
   * Issue a reset.
   */
  *base = 0xf0;

  while (size)
  {
    rtems_interrupt_disable (level);
    *(base + 0xaaa) = 0xaa;
    *(base + 0x555) = 0x55;
    *(base + 0xaaa) = 0xa0;
    *seg = *data++;
    rtems_interrupt_enable (level);
    if (rtems_am29lv160_toggle_wait_8 (seg++) != 0)
      return EIO;
    size--;
  }

  /*
   * Issue a reset.
   */
  *base = 0xf0;

  return 0;
}

static int
rtems_am29lv160_write_data_16 (volatile uint16_t* base,
                               uint32_t           offset,
                               const uint16_t*    data,
                               uint32_t           size)
{
  volatile uint16_t*    seg = base + (offset / 2);
  rtems_interrupt_level level;

  size /= 2;

  /*
   * Issue a reset.
   */
  *base = 0xf0;

  while (size)
  {
    rtems_interrupt_disable (level);
    *(base + 0x555) = 0xaa;
    *(base + 0x2aa) = 0x55;
    *(base + 0x555) = 0xa0;
    *seg = *data++;
    rtems_interrupt_enable (level);
    if (rtems_am29lv160_toggle_wait_16 (seg++) != 0)
      return EIO;
    size--;
  }

  /*
   * Issue a reset.
   */
  *base = 0xf0;

  return 0;
}

static int
rtems_am29lv160_read (const rtems_fdisk_segment_desc* sd,
                      uint32_t                        device,
                      uint32_t                        segment,
                      uint32_t                        offset,
                      void*                           buffer,
                      uint32_t                        size)
{
  unsigned char* addr =
    rtems_am29lv160_configuration[device].base +
    sd->offset + ((segment - sd->segment) * sd->size) + offset;
  memcpy (buffer, addr, size);
  return 0;
}

/*
 * @todo Fix the odd alignment and odd sizes.
 */
static int
rtems_am29lv160_write (const rtems_fdisk_segment_desc* sd,
                       uint32_t                        device,
                       uint32_t                        segment,
                       uint32_t                        offset,
                       const void*                     buffer,
                       uint32_t                        size)
{
  int ret = rtems_am29lv160_verify (sd, device, segment, offset, buffer, size);

  if (ret != 0)
  {
    const rtems_am29lv160_config* ac = &rtems_am29lv160_configuration[device];
    uint32_t                      soffset;

    soffset = offset + sd->offset + ((segment - sd->segment) * sd->size);

    if (offset & 1)
      printf ("rtems_am29lv160_write: offset is odd\n");

    if (size & 1)
      printf ("rtems_am29lv160_write: size is odd\n");

    if (ac->bus_8bit)
      ret = rtems_am29lv160_write_data_8 (ac->base, soffset, buffer, size);
    else
      ret = rtems_am29lv160_write_data_16 (ac->base, soffset, buffer, size);

    /*
     * Verify the write worked.
     */
    if (ret == 0)
    {
      ret = rtems_am29lv160_verify (sd, device, segment, offset, buffer, size);
#if AM26LV160_ERROR_TRACE
      if (ret)
        printf ("AM26LV160: verify failed: %ld-%ld-%08lx: s=%ld\n",
                device, segment, offset, size);
#endif
    }
  }

  return ret;
}

static int
rtems_am29lv160_erase (const rtems_fdisk_segment_desc* sd,
                       uint32_t                        device,
                       uint32_t                        segment)
{
  int ret = rtems_am29lv160_blank (sd, device, segment, 0, sd->size);
  if (ret != 0)
  {
    const rtems_am29lv160_config* ac = &rtems_am29lv160_configuration[device];
    uint32_t                      offset;
    rtems_interrupt_level         level;

    offset = sd->offset + ((segment - sd->segment) * sd->size);

    if (ac->bus_8bit)
    {
      volatile uint8_t* base = ac->base;
      volatile uint8_t* seg  = base + offset;

      /*
       * Issue a reset.
       */
      rtems_interrupt_disable (level);
      *base = 0xf0;
      *(base + 0xaaa) = 0xaa;
      *(base + 0x555) = 0x55;
      *(base + 0xaaa) = 0x80;
      *(base + 0xaaa) = 0xaa;
      *(base + 0x555) = 0x55;
      *seg = 0x30;
      rtems_interrupt_enable (level);

      ret = rtems_am29lv160_toggle_wait_8 (seg);

      /*
       * Issue a reset.
       */
      *base = 0xf0;
    }
    else
    {
      volatile uint16_t* base = ac->base;
      volatile uint16_t* seg  = base + (offset / 2);

      /*
       * Issue a reset.
       */
      rtems_interrupt_disable (level);
      *base = 0xf0;
      *(base + 0x555) = 0xaa;
      *(base + 0x2aa) = 0x55;
      *(base + 0x555) = 0x80;
      *(base + 0x555) = 0xaa;
      *(base + 0x2aa) = 0x55;
      *seg = 0x30;
      rtems_interrupt_enable (level);

      ret = rtems_am29lv160_toggle_wait_16 (seg);

      /*
       * Issue a reset.
       */
      *base = 0xf0;
    }

    /*
     * Check the erase worked.
     */
    if (ret == 0)
    {
      ret = rtems_am29lv160_blank (sd, device, segment, 0, sd->size);
#if AM26LV160_ERROR_TRACE
      if (ret)
        printf ("AM26LV160: erase failed: %ld-%ld\n", device, segment);
#endif
    }
  }

  return ret;
}

static int
rtems_am29lv160_erase_device (const rtems_fdisk_device_desc* dd,
                              uint32_t                       device)
{
  uint32_t segment;

  for (segment = 0; segment < dd->segment_count; segment++)
  {
    uint32_t seg_segment;

    for (seg_segment = 0;
         seg_segment < dd->segments[segment].count;
         seg_segment++)
    {
      int ret = rtems_am29lv160_erase (&dd->segments[segment],
                                       device,
                                       segment + seg_segment);
      if (ret)
        return ret;
    }
  }

  return 0;
}

const rtems_fdisk_driver_handlers rtems_am29lv160_handlers =
{
  .read =         rtems_am29lv160_read,
  .write =        rtems_am29lv160_write,
  .blank =        rtems_am29lv160_blank,
  .verify =       rtems_am29lv160_verify,
  .erase =        rtems_am29lv160_erase,
  .erase_device = rtems_am29lv160_erase_device
};