summaryrefslogtreecommitdiffstats
path: root/c/src/lib/libbsp/powerpc/gen5200/nvram/nvram.c
blob: 99192ac3c4fa83aeeb13a58d7bc3cb598c76d725 (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
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
/*===============================================================*\
| Project: RTEMS generic MPC5200 BSP                              |
+-----------------------------------------------------------------+
| Partially based on the code references which are named below.   |
| Adaptions, modifications, enhancements and any recent parts of  |
| the code are:                                                   |
|                    Copyright (c) 2005                           |
|                    Embedded Brains GmbH                         |
|                    Obere Lagerstr. 30                           |
|                    D-82178 Puchheim                             |
|                    Germany                                      |
|                    rtems@embedded-brains.de                     |
+-----------------------------------------------------------------+
| The license and distribution terms for this file may be         |
| found in the file LICENSE in this distribution or at            |
|                                                                 |
| http://www.rtems.org/license/LICENSE.                           |
|                                                                 |
+-----------------------------------------------------------------+
| this file contains the nvram functions                          |
\*===============================================================*/
/***********************************************************************/
/*                                                                     */
/*   Module:       nvram.c                                             */
/*   Date:         07/17/2003                                          */
/*   Purpose:      RTEMS M93C64-based NV memory device driver          */
/*                                                                     */
/*---------------------------------------------------------------------*/
/*                                                                     */
/*   Description:  M93C46 is a serial microwire EEPROM which contains  */
/*                 1Kbit (128 bytes/64 words) of non-volatile memory.  */
/*                 The device can be coigured for byte- or word-       */
/*                 access. The driver provides a file-like interface   */
/*                 to this memory.                                     */
/*                                                                     */
/*                 MPC5x00 PIN settings:                               */
/*                                                                     */
/*                 PSC3_6 (output) -> MC93C46 serial data in    (D)    */
/*                 PSC3_7 (input)  -> MC93C46 serial data out   (Q)    */
/*                 PSC3_8 (output) -> MC93C46 chip select input (S)    */
/*                 PSC3_9 (output) -> MC93C46 serial clock      (C)    */
/*                                                                     */
/*---------------------------------------------------------------------*/
/*                                                                     */
/*   Code                                                              */
/*   References:   DS1307-based Non-Volatile memory device driver      */
/*   Module:       nvram.c                                             */
/*   Project:      RTEMS 4.6.0pre1 / MCF5206Elite BSP                  */
/*   Version       1.2                                                 */
/*   Date:         11/04/2002                                          */
/*   Author:       Victor V. Vengerov                                  */
/*   Copyright:    Copyright (C) 2000 OKTET Ltd.,St.-Petersburg,Russia */
/*                                                                     */
/*   The license and distribution terms for this file may be           */
/*   found in the file LICENSE in this distribution or at              */
/*   http://www.rtems.org/license/LICENSE.                        */
/*                                                                     */
/*---------------------------------------------------------------------*/
/*                                                                     */
/*   Partially based on the code references which are named above.     */
/*   Adaptions, modifications, enhancements and any recent parts of    */
/*   the code are under the right of                                   */
/*                                                                     */
/*         IPR Engineering, Dachauer Straße 38, D-80335 München        */
/*                        Copyright(C) 2003                            */
/*                                                                     */
/*---------------------------------------------------------------------*/
/*                                                                     */
/*   IPR Engineering makes no representation or warranties with        */
/*   respect to the performance of this computer program, and          */
/*   specifically disclaims any responsibility for any damages,        */
/*   special or consequential, connected with the use of this program. */
/*                                                                     */
/*---------------------------------------------------------------------*/
/*                                                                     */
/*   Version history:  1.0                                             */
/*                                                                     */
/***********************************************************************/

#include <rtems.h>
#include <rtems/libio.h>
#include <errno.h>
#include <string.h>
#include <stdio.h>
#include <bsp.h>
#include <bsp/mpc5200.h>
#include <bsp/nvram.h>
#include "m93cxx.h"

/*
 * Simple usec delay function using lower half of HARPO Time Base Register
 */
void wait_usec(unsigned long usec)
  {
  unsigned long start_count = 0, update_count;
  unsigned long delay_count;

  if(TMBASE_CLOCK < 1000000)
    delay_count = (TMBASE_CLOCK * usec )/1000000;
  else
    delay_count = (TMBASE_CLOCK / 1000000) * usec;

  TBL_READ(start_count);

  update_count = start_count;

  while((update_count - start_count) < delay_count)
    TBL_READ(update_count);

  }


/*
 * Enable M93Cxx chip-write
 */
static void m93cxx_enable_write()
  {
  uint32_t header, i;

  ENABLE_CHIP_SELECT;

  WAIT(1);

  header = M93C46_EWEN;

  for(i = 0; i < M93C46_CLOCK_CYCLES; i++)
	{

	MASK_HEAD_SHIFT(header);

	WAIT(1);

	DO_CLOCK_CYCLE;

	WAIT(1);

	}

  DISABLE_CHIP_SELECT;

  return;

  }


/*
 * Disable M93Cxx chip-write
 */
static void m93cxx_disable_write()
  {
  uint32_t header, i;

  ENABLE_CHIP_SELECT;

  WAIT(1);

  header = M93C46_EWDS;

  for(i = 0; i < M93C46_CLOCK_CYCLES; i++)
	{

	MASK_HEAD_SHIFT(header);

	WAIT(1);

	DO_CLOCK_CYCLE;

	WAIT(1);

	}

  DISABLE_CHIP_SELECT;

  return;

  }


/*
 * Read one byte from specified offset
 */
static uint8_t m93cxx_read_byte(uint32_t offset)
  {
  uint8_t byte2read;
  uint32_t header, tmp_offset, i;
#ifdef M93CXX_MODE_BYTE
  uint8_t byte_recv = 0;
#else
	uint32_t word_recv = 0;
#endif

  ENABLE_CHIP_SELECT;

  WAIT(1);

#ifdef M93CXX_MODE_BYTE

  header = M93C46_READ(offset);

  for(i = 0; i < M93C46_CLOCK_CYCLES; i++)
	{

	MASK_HEAD_SHIFT(header);

	WAIT(1);

	DO_CLOCK_CYCLE;

	WAIT(1);

	}

  for(i = 0; i < 8; i++)
	{

    WAIT(1);

    DO_CLOCK_CYCLE;

    WAIT(1);

    GET_DATA_BYTE_SHIFT(byte_recv);

    }

  byte_recv >>= 1;

  byte2read = byte_recv;

#else
  tmp_offset = offset/2;

  header = M93C46_READ(tmp_offset);

  for(i = 0; i < M93C46_CLOCK_CYCLES; i++)
    {

    MASK_HEAD_SHIFT(header);

    WAIT(1);

    DO_CLOCK_CYCLE;

    WAIT(1);

    }

  for(i = 0; i < 16; i++)
    {

    DO_CLOCK_CYCLE;

    WAIT(1);

    GET_DATA_WORD_SHIFT(word_recv);

    WAIT(1);

    }

  word_recv >>= 1;

  if(offset%2)
	{

	byte2read = (uint8_t)((word_recv & 0xFF00) >> 8);

#ifdef NVRAM_DEBUG
	printf("\nbyte_read(o) = %x", byte2read);
#endif

	}
  else
	{

	byte2read = (uint8_t)(word_recv & 0x00FF);

#ifdef NVRAM_DEBUG
	printf("\nbyte_read(e) = %x", byte2read);
#endif
	}

#endif

  WAIT(1);

  DISABLE_CHIP_SELECT;

  return byte2read;

  }


/*
 * Write one byte to specified offset
 */
void m93cxx_write_byte(uint32_t offset, uint8_t byte2write)
  {
  uint32_t header, tmp_offset, i;
#ifdef M93CXX_MODE_BYTE
  uint8_t byte_send;
#else
  uint16_t word_send;
#endif

  ENABLE_CHIP_SELECT;

  WAIT(1);

#ifdef M93CXX_MODE_BYTE
  header = M93C46_WRITE(offset);

  for(i = 0; i < M93C46_CLOCK_CYCLES; i++)
	{

	MASK_HEAD_SHIFT(header);

	WAIT(1);

	DO_CLOCK_CYCLE;

	WAIT(1);

	}

  byte_send = byte2write;

  for(i = 0; i < 8; i++)
	{

	SET_DATA_BYTE_SHIFT(byte_send);

	WAIT(1);

	DO_CLOCK_CYCLE;

	WAIT(1);

	}

  }
#else

  if(offset%2)
	{

	word_send  = (uint16_t)m93cxx_read_byte(offset-1);
	word_send |= (uint16_t)(m93cxx_read_byte(offset) << 8);

	}
  else
	{

	word_send  = (uint16_t)m93cxx_read_byte(offset);
	word_send |= (uint16_t)(m93cxx_read_byte(offset + 1) << 8);

	}

  tmp_offset = offset/2;

  WAIT(1);

  ENABLE_CHIP_SELECT;

  WAIT(1);

  header = M93C46_WRITE(tmp_offset);

  for(i = 0; i < M93C46_CLOCK_CYCLES; i++)
	{

	MASK_HEAD_SHIFT(header);

	WAIT(1);

	DO_CLOCK_CYCLE;

	WAIT(1);

	}

  if(offset%2)
	{

	word_send  = (word_send & 0x00FF) | ((uint16_t)(byte2write << 8));

#ifdef NVRAM_DEBUG
	printf("\nword_send = %x", word_send);
#endif

	}
  else
	{

	word_send  = (word_send & 0xFF00) | (uint16_t)byte2write;
#ifdef NVRAM_DEBUG
	printf("\nword_send = %x", word_send);
#endif

	}

  for(i = 0; i < 16; i++)
	{

	SET_DATA_WORD_SHIFT(word_send);

	WAIT(1);

	DO_CLOCK_CYCLE;

	WAIT(1);

	}

  DISABLE_CHIP_SELECT;

  WAIT(1);

  ENABLE_CHIP_SELECT;

  WAIT(1);

  CHECK_WRITE_BUSY;

#endif

  WAIT(1);

  DISABLE_CHIP_SELECT;

  return;

  }


/* nvram_driver_initialize --
 *     Non-volatile memory device driver initialization.
 */
rtems_device_driver nvram_driver_initialize(rtems_device_major_number major, rtems_device_minor_number minor, void *arg)
  {
  rtems_status_code sc;

  /* enable PSC3_6/PSC3_7 as general purpose pins */
  mpc5200.gpiosen |= (GPIO_PSC3_6 | GPIO_PSC3_7);

  /* PSC3_6/PSC3_7 has normal CMOS output */
  mpc5200.gpiosod &= ~(GPIO_PSC3_6 | GPIO_PSC3_7);

  /* switch PSC3_6 (MC93C46 serial data in (D)) to low */
  mpc5200.gpiosdo &= ~GPIO_PSC3_6;

  /* PSC3_6 is an output (MC93C46 serial data in (D)) and PSC3_7 (MC93C46 serial data out (Q)) is an input pin */
  mpc5200.gpiosdd |= GPIO_PSC3_6;
  mpc5200.gpiosdd &= ~GPIO_PSC3_7;

  /* disable PSC3_8 interrupt capabilities */
  mpc5200.gpiosiie &= ~GPIO_PSC3_8;

  /* enable PSC3_8 as general purpose pin */
  mpc5200.gpiosie |= GPIO_PSC3_8;

  /* PSC3_8 has normal CMOS output */
  mpc5200.gpiosiod &= ~GPIO_PSC3_8;

  /* switch PSC3_8 (MC93C46 chip select input (S)) to low (high activ) */
  mpc5200.gpiosido &= ~GPIO_PSC3_8;

  /* PSC3_8 is an output (MC93C46 chip select input (S)) pin */
  mpc5200.gpiosidd |= GPIO_PSC3_8;

  /* disable PSC3_9 interrupt capabilities */
  mpc5200.gpiowue &= ~GPIO_PSC3_9;

  /* enable PSC3_9 as general purpose pins */
  mpc5200.gpiowe |= GPIO_PSC3_9;

  /* PSC3_9 has normal CMOS output */
  mpc5200.gpiowod &= ~GPIO_PSC3_9;

  /* switch PSC3_9 (MC93C46 serial clock (C)) to low */
  mpc5200.gpiowdo &= ~GPIO_PSC3_9;

  /* PSC3_9 is an output (MC93C46 serial clock (C)) pin */
  mpc5200.gpiowdd |= GPIO_PSC3_9;

  sc = rtems_io_register_name("/dev/nvram", major, 0);

  if(sc != RTEMS_SUCCESSFUL)
	{

	errno = EIO;
	/*errno = ENODEV;*/
	return RTEMS_UNSATISFIED;

	}
  else
	return RTEMS_SUCCESSFUL;

  }


/* nvram_driver_open --
 *     Non-volatile memory device driver open primitive.
 */
rtems_device_driver nvram_driver_open(rtems_device_major_number major, rtems_device_minor_number minor, void *arg)
  {

  return RTEMS_SUCCESSFUL;

  }


/* nvram_driver_close --
 *     Non-volatile memory device driver close primitive.
 */
rtems_device_driver nvram_driver_close(rtems_device_major_number major, rtems_device_minor_number minor, void *arg)
  {

  return RTEMS_SUCCESSFUL;

  }


/* nvram_driver_read --
 *     Non-volatile memory device driver read primitive.
 */
rtems_device_driver nvram_driver_read(rtems_device_major_number major, rtems_device_minor_number minor, void *arg)
  {
  rtems_libio_rw_args_t *args = arg;
  uint32_t count, i;

#ifdef NVRAM_DEBUG
  printf("\nread count  = %2x", (int)(args->count));
  printf("\nread offset = %2x", (int)(args->offset));
#endif

  if((args->offset >= M93C46_NVRAM_SIZE) || (args->offset + args->count > M93C46_NVRAM_SIZE))
    {

    args->bytes_moved = 0;
    errno = EINVAL;
    return RTEMS_UNSATISFIED;

    }
  else
    count = args->count;

  for(i = 0; i < count; i++)
    {

    (args->buffer)[i] = m93cxx_read_byte((args->offset) + i);
    (args->bytes_moved) += 1;

    }

  return RTEMS_SUCCESSFUL;

  }


/* nvram_driver_write --
 *     Non-volatile memory device driver write primitive.
 */
rtems_device_driver nvram_driver_write(rtems_device_major_number major, rtems_device_minor_number minor, void *arg)
  {
  rtems_libio_rw_args_t *args = arg;
  uint32_t count, i;

#ifdef NVRAM_DEBUG
  printf("\nwrite count  = %2x", (int)(args->count));
  printf("\nwrite offset = %2x", (int)(args->offset));
#endif

  if((args->offset >= M93C46_NVRAM_SIZE) || (args->offset + args->count > M93C46_NVRAM_SIZE))
    {

    args->bytes_moved = 0;
    errno = EINVAL;
    return RTEMS_UNSATISFIED;

    }

  count = args->count;

  m93cxx_enable_write();

  WAIT(1);

  for(i = 0; i < count; i++)
    {

    m93cxx_write_byte((args->offset) + i, (args->buffer)[i]);
    (args->bytes_moved) += 1;

    }

  WAIT(1);

  m93cxx_disable_write();

  return RTEMS_SUCCESSFUL;

  }