1 | /* |
---|
2 | * DS1307-based Non-Volatile memory device driver |
---|
3 | * |
---|
4 | * DS1307 chip is a I2C Real-Time Clock. It contains 56 bytes of |
---|
5 | * non-volatile RAM storage. This driver provide file-like interface to |
---|
6 | * this memory. |
---|
7 | * |
---|
8 | * Copyright (C) 2000 OKTET Ltd., St.-Petersburg, Russia |
---|
9 | * Author: Victor V. Vengerov <vvv@oktet.ru> |
---|
10 | * |
---|
11 | * The license and distribution terms for this file may be |
---|
12 | * found in the file LICENSE in this distribution or at |
---|
13 | * |
---|
14 | * http://www.rtems.com/license/LICENSE. |
---|
15 | * |
---|
16 | * @(#) $Id$ |
---|
17 | */ |
---|
18 | |
---|
19 | #include <rtems.h> |
---|
20 | #include <rtems/libio.h> |
---|
21 | #include <errno.h> |
---|
22 | #include <string.h> |
---|
23 | #include <bsp.h> |
---|
24 | #include <nvram.h> |
---|
25 | #include <i2c.h> |
---|
26 | #include <ds1307.h> |
---|
27 | |
---|
28 | |
---|
29 | /* nvram_driver_initialize -- |
---|
30 | * Non-volatile memory device driver initialization. |
---|
31 | */ |
---|
32 | rtems_device_driver |
---|
33 | nvram_driver_initialize(rtems_device_major_number major, |
---|
34 | rtems_device_minor_number minor, |
---|
35 | void *arg) |
---|
36 | { |
---|
37 | rtems_status_code sc; |
---|
38 | i2c_message_status status; |
---|
39 | i2c_bus_number bus = DS1307_I2C_BUS_NUMBER; |
---|
40 | i2c_address addr = DS1307_I2C_ADDRESS; |
---|
41 | int try = 0; |
---|
42 | do { |
---|
43 | status = i2c_wrbyte(bus, addr, 0); |
---|
44 | if (status == I2C_NO_DEVICE) |
---|
45 | break; |
---|
46 | try++; |
---|
47 | } while ((try < 15) && (status != I2C_SUCCESSFUL)); |
---|
48 | |
---|
49 | if (status == I2C_SUCCESSFUL) |
---|
50 | { |
---|
51 | sc = rtems_io_register_name("/dev/nvram", major, 0); |
---|
52 | if (sc != RTEMS_SUCCESSFUL) |
---|
53 | { |
---|
54 | errno = EIO; |
---|
55 | return RTEMS_UNSATISFIED; |
---|
56 | } |
---|
57 | else |
---|
58 | return RTEMS_SUCCESSFUL; |
---|
59 | } |
---|
60 | else |
---|
61 | { |
---|
62 | errno = ENODEV; |
---|
63 | return RTEMS_UNSATISFIED; |
---|
64 | } |
---|
65 | } |
---|
66 | |
---|
67 | /* nvram_driver_open -- |
---|
68 | * Non-volatile memory device driver open primitive. |
---|
69 | */ |
---|
70 | rtems_device_driver |
---|
71 | nvram_driver_open(rtems_device_major_number major, |
---|
72 | rtems_device_minor_number minor, |
---|
73 | void *arg) |
---|
74 | { |
---|
75 | return RTEMS_SUCCESSFUL; |
---|
76 | } |
---|
77 | |
---|
78 | /* nvram_driver_close -- |
---|
79 | * Non-volatile memory device driver close primitive. |
---|
80 | */ |
---|
81 | rtems_device_driver |
---|
82 | nvram_driver_close(rtems_device_major_number major, |
---|
83 | rtems_device_minor_number minor, |
---|
84 | void *arg) |
---|
85 | { |
---|
86 | return RTEMS_SUCCESSFUL; |
---|
87 | } |
---|
88 | |
---|
89 | /* nvram_driver_read -- |
---|
90 | * Non-volatile memory device driver read primitive. |
---|
91 | */ |
---|
92 | rtems_device_driver |
---|
93 | nvram_driver_read(rtems_device_major_number major, |
---|
94 | rtems_device_minor_number minor, |
---|
95 | void *arg) |
---|
96 | { |
---|
97 | rtems_libio_rw_args_t *args = arg; |
---|
98 | unsigned32 count; |
---|
99 | i2c_bus_number bus = DS1307_I2C_BUS_NUMBER; |
---|
100 | i2c_address addr = DS1307_I2C_ADDRESS; |
---|
101 | i2c_message_status status; |
---|
102 | if (args->offset >= DS1307_NVRAM_SIZE) |
---|
103 | { |
---|
104 | count = 0; |
---|
105 | } |
---|
106 | else if (args->offset + args->count >= DS1307_NVRAM_SIZE) |
---|
107 | { |
---|
108 | count = DS1307_NVRAM_SIZE - args->offset; |
---|
109 | } |
---|
110 | else |
---|
111 | { |
---|
112 | count = args->count; |
---|
113 | } |
---|
114 | if (count > 0) |
---|
115 | { |
---|
116 | int try = 0; |
---|
117 | do { |
---|
118 | status = i2c_wbrd(bus, addr, DS1307_NVRAM_START + args->offset, |
---|
119 | args->buffer, count); |
---|
120 | try++; |
---|
121 | } while ((try < 15) && (status != I2C_SUCCESSFUL)); |
---|
122 | if (status != I2C_SUCCESSFUL) |
---|
123 | { |
---|
124 | errno = EIO; |
---|
125 | return RTEMS_UNSATISFIED; |
---|
126 | } |
---|
127 | } |
---|
128 | args->bytes_moved = count; |
---|
129 | return RTEMS_SUCCESSFUL; |
---|
130 | } |
---|
131 | |
---|
132 | /* nvram_driver_write -- |
---|
133 | * Non-volatile memory device driver write primitive. |
---|
134 | */ |
---|
135 | rtems_device_driver |
---|
136 | nvram_driver_write(rtems_device_major_number major, |
---|
137 | rtems_device_minor_number minor, |
---|
138 | void *arg) |
---|
139 | { |
---|
140 | rtems_libio_rw_args_t *args = arg; |
---|
141 | unsigned32 count; |
---|
142 | i2c_bus_number bus = DS1307_I2C_BUS_NUMBER; |
---|
143 | i2c_address addr = DS1307_I2C_ADDRESS; |
---|
144 | i2c_message_status status; |
---|
145 | |
---|
146 | if (args->offset >= DS1307_NVRAM_SIZE) |
---|
147 | { |
---|
148 | count = 0; |
---|
149 | } |
---|
150 | else if (args->offset + args->count >= DS1307_NVRAM_SIZE) |
---|
151 | { |
---|
152 | count = DS1307_NVRAM_SIZE - args->offset; |
---|
153 | } |
---|
154 | else |
---|
155 | { |
---|
156 | count = args->count; |
---|
157 | } |
---|
158 | if (count > 0) |
---|
159 | { |
---|
160 | int try = 0; |
---|
161 | do { |
---|
162 | rtems_unsigned8 buf[DS1307_NVRAM_SIZE + 1]; |
---|
163 | buf[0] = DS1307_NVRAM_START + args->offset; |
---|
164 | memcpy(buf+1, args->buffer, count); |
---|
165 | status = i2c_write(bus, addr, buf, count+1); |
---|
166 | try++; |
---|
167 | } while ((try < 15) && (status != I2C_SUCCESSFUL)); |
---|
168 | if (status != I2C_SUCCESSFUL) |
---|
169 | { |
---|
170 | errno = EIO; |
---|
171 | return RTEMS_UNSATISFIED; |
---|
172 | } |
---|
173 | } |
---|
174 | args->bytes_moved = count; |
---|
175 | return RTEMS_SUCCESSFUL; |
---|
176 | } |
---|