Commit | Line | Data |
---|---|---|
ef016f83 MF |
1 | /* Blackfin One-Time Programmable Memory (OTP) model |
2 | ||
3 | Copyright (C) 2010-2011 Free Software Foundation, Inc. | |
4 | Contributed by Analog Devices, Inc. | |
5 | ||
6 | This file is part of simulators. | |
7 | ||
8 | This program is free software; you can redistribute it and/or modify | |
9 | it under the terms of the GNU General Public License as published by | |
10 | the Free Software Foundation; either version 3 of the License, or | |
11 | (at your option) any later version. | |
12 | ||
13 | This program is distributed in the hope that it will be useful, | |
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | GNU General Public License for more details. | |
17 | ||
18 | You should have received a copy of the GNU General Public License | |
19 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ | |
20 | ||
21 | #include "config.h" | |
22 | ||
23 | #include "sim-main.h" | |
24 | #include "devices.h" | |
25 | #include "dv-bfin_otp.h" | |
26 | ||
27 | /* XXX: No public documentation on this interface. This seems to work | |
28 | with the on-chip ROM functions though and was figured out by | |
29 | disassembling & walking that code. */ | |
30 | /* XXX: About only thing that should be done here are CRC fields. And | |
31 | supposedly there is an interrupt that could be generated. */ | |
32 | ||
33 | struct bfin_otp | |
34 | { | |
35 | bu32 base; | |
36 | ||
37 | /* The actual OTP storage -- 0x200 pages, each page is 128bits. | |
38 | While certain pages have predefined and/or secure access, we don't | |
39 | bother trying to implement that coverage. All pages are open for | |
40 | reading & writing. */ | |
41 | bu32 mem[0x200 * 4]; | |
42 | ||
43 | /* Order after here is important -- matches hardware MMR layout. */ | |
44 | bu16 BFIN_MMR_16(control); | |
45 | bu16 BFIN_MMR_16(ben); | |
46 | bu16 BFIN_MMR_16(status); | |
47 | bu32 timing; | |
48 | bu32 _pad0[28]; | |
49 | bu32 data0, data1, data2, data3; | |
50 | }; | |
51 | #define mmr_base() offsetof(struct bfin_otp, control) | |
52 | #define mmr_offset(mmr) (offsetof(struct bfin_otp, mmr) - mmr_base()) | |
53 | #define mmr_idx(mmr) (mmr_offset (mmr) / 4) | |
54 | ||
55 | static const char * const mmr_names[] = { | |
56 | "OTP_CONTROL", "OTP_BEN", "OTP_STATUS", "OTP_TIMING", | |
57 | [mmr_idx (data0)] = "OTP_DATA0", "OTP_DATA1", "OTP_DATA2", "OTP_DATA3", | |
58 | }; | |
59 | #define mmr_name(off) (mmr_names[(off) / 4] ? : "<INV>") | |
60 | ||
61 | /* XXX: This probably misbehaves with big endian hosts. */ | |
62 | static void | |
63 | bfin_otp_transfer (struct bfin_otp *otp, void *vdst, void *vsrc) | |
64 | { | |
65 | bu8 *dst = vdst, *src = vsrc; | |
66 | int bidx; | |
67 | for (bidx = 0; bidx < 16; ++bidx) | |
68 | if (otp->ben & (1 << bidx)) | |
69 | dst[bidx] = src[bidx]; | |
70 | } | |
71 | ||
72 | static void | |
73 | bfin_otp_read_page (struct bfin_otp *otp, bu16 page) | |
74 | { | |
75 | bfin_otp_transfer (otp, &otp->data0, &otp->mem[page * 4]); | |
76 | } | |
77 | ||
78 | static void | |
79 | bfin_otp_write_page_val (struct bfin_otp *otp, bu16 page, bu64 val[2]) | |
80 | { | |
81 | bfin_otp_transfer (otp, &otp->mem[page * 4], val); | |
82 | } | |
83 | static void | |
84 | bfin_otp_write_page_val2 (struct bfin_otp *otp, bu16 page, bu64 lo, bu64 hi) | |
85 | { | |
86 | bu64 val[2] = { lo, hi }; | |
87 | bfin_otp_write_page_val (otp, page, val); | |
88 | } | |
89 | static void | |
90 | bfin_otp_write_page (struct bfin_otp *otp, bu16 page) | |
91 | { | |
92 | bfin_otp_write_page_val (otp, page, (void *)&otp->data0); | |
93 | } | |
94 | ||
95 | static unsigned | |
96 | bfin_otp_io_write_buffer (struct hw *me, const void *source, int space, | |
97 | address_word addr, unsigned nr_bytes) | |
98 | { | |
99 | struct bfin_otp *otp = hw_data (me); | |
100 | bu32 mmr_off; | |
101 | bu32 value; | |
102 | bu16 *value16p; | |
103 | bu32 *value32p; | |
104 | void *valuep; | |
105 | ||
106 | if (nr_bytes == 4) | |
107 | value = dv_load_4 (source); | |
108 | else | |
109 | value = dv_load_2 (source); | |
110 | ||
111 | mmr_off = addr - otp->base; | |
112 | valuep = (void *)((unsigned long)otp + mmr_base() + mmr_off); | |
113 | value16p = valuep; | |
114 | value32p = valuep; | |
115 | ||
116 | HW_TRACE_WRITE (); | |
117 | ||
118 | switch (mmr_off) | |
119 | { | |
120 | case mmr_offset(control): | |
121 | { | |
122 | int page; | |
123 | ||
124 | dv_bfin_mmr_require_16 (me, addr, nr_bytes, true); | |
125 | /* XXX: Seems like these bits aren't writable. */ | |
126 | *value16p = value & 0x39FF; | |
127 | ||
128 | /* Low bits seem to be the page address. */ | |
129 | page = value & PAGE_ADDR; | |
130 | ||
131 | /* Write operation. */ | |
132 | if (value & DO_WRITE) | |
133 | bfin_otp_write_page (otp, page); | |
134 | ||
135 | /* Read operation. */ | |
136 | if (value & DO_READ) | |
137 | bfin_otp_read_page (otp, page); | |
138 | ||
139 | otp->status |= STATUS_DONE; | |
140 | ||
141 | break; | |
142 | } | |
143 | case mmr_offset(ben): | |
144 | dv_bfin_mmr_require_16 (me, addr, nr_bytes, true); | |
145 | /* XXX: All bits seem to be writable. */ | |
146 | *value16p = value; | |
147 | break; | |
148 | case mmr_offset(status): | |
149 | dv_bfin_mmr_require_16 (me, addr, nr_bytes, true); | |
150 | /* XXX: All bits seem to be W1C. */ | |
151 | dv_w1c_2 (value16p, value, 0); | |
152 | break; | |
153 | case mmr_offset(timing): | |
154 | case mmr_offset(data0): | |
155 | case mmr_offset(data1): | |
156 | case mmr_offset(data2): | |
157 | case mmr_offset(data3): | |
158 | dv_bfin_mmr_require_32 (me, addr, nr_bytes, true); | |
159 | *value32p = value; | |
160 | break; | |
161 | default: | |
162 | dv_bfin_mmr_invalid (me, addr, nr_bytes, true); | |
163 | break; | |
164 | } | |
165 | ||
166 | return nr_bytes; | |
167 | } | |
168 | ||
169 | static unsigned | |
170 | bfin_otp_io_read_buffer (struct hw *me, void *dest, int space, | |
171 | address_word addr, unsigned nr_bytes) | |
172 | { | |
173 | struct bfin_otp *otp = hw_data (me); | |
174 | bu32 mmr_off; | |
175 | bu16 *value16p; | |
176 | bu32 *value32p; | |
177 | void *valuep; | |
178 | ||
179 | mmr_off = addr - otp->base; | |
180 | valuep = (void *)((unsigned long)otp + mmr_base() + mmr_off); | |
181 | value16p = valuep; | |
182 | value32p = valuep; | |
183 | ||
184 | HW_TRACE_READ (); | |
185 | ||
186 | switch (mmr_off) | |
187 | { | |
188 | case mmr_offset(control): | |
189 | case mmr_offset(ben): | |
190 | case mmr_offset(status): | |
191 | dv_bfin_mmr_require_16 (me, addr, nr_bytes, false); | |
192 | dv_store_2 (dest, *value16p); | |
193 | break; | |
194 | case mmr_offset(timing): | |
195 | case mmr_offset(data0): | |
196 | case mmr_offset(data1): | |
197 | case mmr_offset(data2): | |
198 | case mmr_offset(data3): | |
199 | dv_bfin_mmr_require_32 (me, addr, nr_bytes, false); | |
200 | dv_store_4 (dest, *value32p); | |
201 | break; | |
202 | default: | |
203 | dv_bfin_mmr_invalid (me, addr, nr_bytes, false); | |
204 | break; | |
205 | } | |
206 | ||
207 | return nr_bytes; | |
208 | } | |
209 | ||
210 | static void | |
211 | attach_bfin_otp_regs (struct hw *me, struct bfin_otp *otp) | |
212 | { | |
213 | address_word attach_address; | |
214 | int attach_space; | |
215 | unsigned attach_size; | |
216 | reg_property_spec reg; | |
217 | ||
218 | if (hw_find_property (me, "reg") == NULL) | |
219 | hw_abort (me, "Missing \"reg\" property"); | |
220 | ||
221 | if (!hw_find_reg_array_property (me, "reg", 0, ®)) | |
222 | hw_abort (me, "\"reg\" property must contain three addr/size entries"); | |
223 | ||
224 | hw_unit_address_to_attach_address (hw_parent (me), | |
225 | ®.address, | |
226 | &attach_space, &attach_address, me); | |
227 | hw_unit_size_to_attach_size (hw_parent (me), ®.size, &attach_size, me); | |
228 | ||
229 | if (attach_size != BFIN_MMR_OTP_SIZE) | |
230 | hw_abort (me, "\"reg\" size must be %#x", BFIN_MMR_OTP_SIZE); | |
231 | ||
232 | hw_attach_address (hw_parent (me), | |
233 | 0, attach_space, attach_address, attach_size, me); | |
234 | ||
235 | otp->base = attach_address; | |
236 | } | |
237 | ||
238 | static void | |
239 | bfin_otp_finish (struct hw *me) | |
240 | { | |
241 | char part_str[16]; | |
242 | struct bfin_otp *otp; | |
243 | unsigned int fps03; | |
244 | int type = hw_find_integer_property (me, "type"); | |
245 | ||
246 | otp = HW_ZALLOC (me, struct bfin_otp); | |
247 | ||
248 | set_hw_data (me, otp); | |
249 | set_hw_io_read_buffer (me, bfin_otp_io_read_buffer); | |
250 | set_hw_io_write_buffer (me, bfin_otp_io_write_buffer); | |
251 | ||
252 | attach_bfin_otp_regs (me, otp); | |
253 | ||
254 | /* Initialize the OTP. */ | |
255 | otp->ben = 0xFFFF; | |
256 | otp->timing = 0x00001485; | |
257 | ||
258 | /* Semi-random value for unique chip id. */ | |
259 | bfin_otp_write_page_val2 (otp, FPS00, (unsigned long)otp, ~(unsigned long)otp); | |
260 | ||
261 | memset (part_str, 0, sizeof (part_str)); | |
262 | sprintf (part_str, "ADSP-BF%iX", type); | |
263 | switch (type) | |
264 | { | |
265 | case 512: | |
266 | fps03 = FPS03_BF512; | |
267 | break; | |
268 | case 514: | |
269 | fps03 = FPS03_BF514; | |
270 | break; | |
271 | case 516: | |
272 | fps03 = FPS03_BF516; | |
273 | break; | |
274 | case 518: | |
275 | fps03 = FPS03_BF518; | |
276 | break; | |
277 | case 522: | |
278 | fps03 = FPS03_BF522; | |
279 | break; | |
280 | case 523: | |
281 | fps03 = FPS03_BF523; | |
282 | break; | |
283 | case 524: | |
284 | fps03 = FPS03_BF524; | |
285 | break; | |
286 | case 525: | |
287 | fps03 = FPS03_BF525; | |
288 | break; | |
289 | case 526: | |
290 | fps03 = FPS03_BF526; | |
291 | break; | |
292 | case 527: | |
293 | fps03 = FPS03_BF527; | |
294 | break; | |
295 | default: | |
296 | fps03 = 0; | |
297 | break; | |
298 | } | |
299 | part_str[14] = (fps03 >> 0); | |
300 | part_str[15] = (fps03 >> 8); | |
301 | bfin_otp_write_page_val (otp, FPS03, (void *)part_str); | |
302 | } | |
303 | ||
304 | const struct hw_descriptor dv_bfin_otp_descriptor[] = { | |
305 | {"bfin_otp", bfin_otp_finish,}, | |
306 | {NULL, NULL}, | |
307 | }; |