1 /* Blackfin One-Time Programmable Memory (OTP) model
3 Copyright (C) 2010-2011 Free Software Foundation, Inc.
4 Contributed by Analog Devices, Inc.
6 This file is part of simulators.
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.
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.
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/>. */
25 #include "dv-bfin_otp.h"
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. */
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
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
);
49 bu32 data0
, data1
, data2
, data3
;
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)
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",
59 #define mmr_name(off) (mmr_names[(off) / 4] ? : "<INV>")
61 /* XXX: This probably misbehaves with big endian hosts. */
63 bfin_otp_transfer (struct bfin_otp
*otp
, void *vdst
, void *vsrc
)
65 bu8
*dst
= vdst
, *src
= vsrc
;
67 for (bidx
= 0; bidx
< 16; ++bidx
)
68 if (otp
->ben
& (1 << bidx
))
69 dst
[bidx
] = src
[bidx
];
73 bfin_otp_read_page (struct bfin_otp
*otp
, bu16 page
)
75 bfin_otp_transfer (otp
, &otp
->data0
, &otp
->mem
[page
* 4]);
79 bfin_otp_write_page_val (struct bfin_otp
*otp
, bu16 page
, bu64 val
[2])
81 bfin_otp_transfer (otp
, &otp
->mem
[page
* 4], val
);
84 bfin_otp_write_page_val2 (struct bfin_otp
*otp
, bu16 page
, bu64 lo
, bu64 hi
)
86 bu64 val
[2] = { lo
, hi
};
87 bfin_otp_write_page_val (otp
, page
, val
);
90 bfin_otp_write_page (struct bfin_otp
*otp
, bu16 page
)
92 bfin_otp_write_page_val (otp
, page
, (void *)&otp
->data0
);
96 bfin_otp_io_write_buffer (struct hw
*me
, const void *source
, int space
,
97 address_word addr
, unsigned nr_bytes
)
99 struct bfin_otp
*otp
= hw_data (me
);
107 value
= dv_load_4 (source
);
109 value
= dv_load_2 (source
);
111 mmr_off
= addr
- otp
->base
;
112 valuep
= (void *)((unsigned long)otp
+ mmr_base() + mmr_off
);
120 case mmr_offset(control
):
124 dv_bfin_mmr_require_16 (me
, addr
, nr_bytes
, true);
125 /* XXX: Seems like these bits aren't writable. */
126 *value16p
= value
& 0x39FF;
128 /* Low bits seem to be the page address. */
129 page
= value
& PAGE_ADDR
;
131 /* Write operation. */
132 if (value
& DO_WRITE
)
133 bfin_otp_write_page (otp
, page
);
135 /* Read operation. */
137 bfin_otp_read_page (otp
, page
);
139 otp
->status
|= STATUS_DONE
;
143 case mmr_offset(ben
):
144 dv_bfin_mmr_require_16 (me
, addr
, nr_bytes
, true);
145 /* XXX: All bits seem to be writable. */
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);
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);
162 dv_bfin_mmr_invalid (me
, addr
, nr_bytes
, true);
170 bfin_otp_io_read_buffer (struct hw
*me
, void *dest
, int space
,
171 address_word addr
, unsigned nr_bytes
)
173 struct bfin_otp
*otp
= hw_data (me
);
179 mmr_off
= addr
- otp
->base
;
180 valuep
= (void *)((unsigned long)otp
+ mmr_base() + mmr_off
);
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
);
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
);
203 dv_bfin_mmr_invalid (me
, addr
, nr_bytes
, false);
211 attach_bfin_otp_regs (struct hw
*me
, struct bfin_otp
*otp
)
213 address_word attach_address
;
215 unsigned attach_size
;
216 reg_property_spec reg
;
218 if (hw_find_property (me
, "reg") == NULL
)
219 hw_abort (me
, "Missing \"reg\" property");
221 if (!hw_find_reg_array_property (me
, "reg", 0, ®
))
222 hw_abort (me
, "\"reg\" property must contain three addr/size entries");
224 hw_unit_address_to_attach_address (hw_parent (me
),
226 &attach_space
, &attach_address
, me
);
227 hw_unit_size_to_attach_size (hw_parent (me
), ®
.size
, &attach_size
, me
);
229 if (attach_size
!= BFIN_MMR_OTP_SIZE
)
230 hw_abort (me
, "\"reg\" size must be %#x", BFIN_MMR_OTP_SIZE
);
232 hw_attach_address (hw_parent (me
),
233 0, attach_space
, attach_address
, attach_size
, me
);
235 otp
->base
= attach_address
;
239 bfin_otp_finish (struct hw
*me
)
242 struct bfin_otp
*otp
;
244 int type
= hw_find_integer_property (me
, "type");
246 otp
= HW_ZALLOC (me
, struct bfin_otp
);
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
);
252 attach_bfin_otp_regs (me
, otp
);
254 /* Initialize the OTP. */
256 otp
->timing
= 0x00001485;
258 /* Semi-random value for unique chip id. */
259 bfin_otp_write_page_val2 (otp
, FPS00
, (unsigned long)otp
, ~(unsigned long)otp
);
261 memset (part_str
, 0, sizeof (part_str
));
262 sprintf (part_str
, "ADSP-BF%iX", type
);
299 part_str
[14] = (fps03
>> 0);
300 part_str
[15] = (fps03
>> 8);
301 bfin_otp_write_page_val (otp
, FPS03
, (void *)part_str
);
304 const struct hw_descriptor dv_bfin_otp_descriptor
[] = {
305 {"bfin_otp", bfin_otp_finish
,},