summaryrefslogtreecommitdiffstats
path: root/c/src/lib/libbsp/m68k/mcf5206elite/nvram/nvram.c
blob: f25052545d360b627191a3cc4e4b0e94e261508c (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
/* 
 * DS1307-based Non-Volatile memory device driver
 *
 * DS1307 chip is a I2C Real-Time Clock. It contains 56 bytes of
 * non-volatile RAM storage. This driver provide file-like interface to
 * this memory.
 *
 * Copyright (C) 2000 OKTET Ltd., St.-Petersburg, Russia
 * Author: Victor V. Vengerov <vvv@oktet.ru>
 *
 * The license and distribution terms for this file may be
 * found in the file LICENSE in this distribution or at
 *
 * http://www.OARcorp.com/rtems/license.html.
 *
 * @(#) $Id$
 */

#include <rtems.h>
#include <rtems/libio.h>
#include <errno.h>
#include <string.h>
#include <bsp.h>
#include <nvram.h>
#include <i2c.h>
#include <ds1307.h>


/* 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;
    i2c_message_status status;
    i2c_bus_number bus = DS1307_I2C_BUS_NUMBER;
    i2c_address    addr = DS1307_I2C_ADDRESS;
    int try = 0;
    do {
        status = i2c_wrbyte(bus, addr, 0);
        if (status == I2C_NO_DEVICE)
            break;
        try++;
    } while ((try < 15) && (status != I2C_SUCCESSFUL));

    if (status == I2C_SUCCESSFUL)
    {
        sc = rtems_io_register_name("/dev/nvram", major, 0);
        if (sc != RTEMS_SUCCESSFUL)
        {
            errno = EIO;
            return RTEMS_UNSATISFIED;
        }
        else
            return RTEMS_SUCCESSFUL;
    }
    else
    {
        errno = ENODEV;
        return RTEMS_UNSATISFIED;
    }
}

/* 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;
    unsigned32 count;
    i2c_bus_number bus = DS1307_I2C_BUS_NUMBER;
    i2c_address    addr = DS1307_I2C_ADDRESS;
    i2c_message_status status;
    if (args->offset >= DS1307_NVRAM_SIZE)
    {
        count = 0;
    }
    else if (args->offset + args->count >= DS1307_NVRAM_SIZE)
    {
        count = DS1307_NVRAM_SIZE - args->offset;
    }
    else
    {
        count = args->count;
    }
    if (count > 0)
    {
        int try = 0;
        do {
            status = i2c_wbrd(bus, addr, DS1307_NVRAM_START + args->offset,
                              args->buffer, count);
            try++;
        } while ((try < 15) && (status != I2C_SUCCESSFUL));
        if (status != I2C_SUCCESSFUL)
        {
            errno = EIO;
            return RTEMS_UNSATISFIED;
        }
    }
    args->bytes_moved = count;
    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;
    unsigned32 count;
    i2c_bus_number bus = DS1307_I2C_BUS_NUMBER;
    i2c_address    addr = DS1307_I2C_ADDRESS;
    i2c_message_status status;
    
    if (args->offset >= DS1307_NVRAM_SIZE)
    {
        count = 0;
    }
    else if (args->offset + args->count >= DS1307_NVRAM_SIZE)
    {
        count = DS1307_NVRAM_SIZE - args->offset;
    }
    else
    {
        count = args->count;
    }
    if (count > 0)
    {
        int try = 0;
        do {
            rtems_unsigned8 buf[DS1307_NVRAM_SIZE + 1];
            buf[0] = DS1307_NVRAM_START + args->offset;
            memcpy(buf+1, args->buffer, count);
            status = i2c_write(bus, addr, buf, count+1);
            try++;
        } while ((try < 15) && (status != I2C_SUCCESSFUL));
        if (status != I2C_SUCCESSFUL)
        {
            errno = EIO;
            return RTEMS_UNSATISFIED;
        }
    }
    args->bytes_moved = count;
    return RTEMS_SUCCESSFUL;
}