2 * Copyright (c) 2010 Broadcom Corporation
4 * Permission to use, copy, modify, and/or distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
11 * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
13 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
14 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 #include <linux/errno.h>
19 #include <linux/string.h>
21 #include <brcm_hw_ids.h>
22 #include <chipcommon.h>
26 #define OTPS_GUP_MASK 0x00000f00
27 #define OTPS_GUP_SHIFT 8
28 /* h/w subregion is programmed */
29 #define OTPS_GUP_HW 0x00000100
30 /* s/w subregion is programmed */
31 #define OTPS_GUP_SW 0x00000200
32 /* chipid/pkgopt subregion is programmed */
33 #define OTPS_GUP_CI 0x00000400
34 /* fuse subregion is programmed */
35 #define OTPS_GUP_FUSE 0x00000800
37 /* Fields in otpprog in rev >= 21 */
38 #define OTPP_COL_MASK 0x000000ff
39 #define OTPP_COL_SHIFT 0
40 #define OTPP_ROW_MASK 0x0000ff00
41 #define OTPP_ROW_SHIFT 8
42 #define OTPP_OC_MASK 0x0f000000
43 #define OTPP_OC_SHIFT 24
44 #define OTPP_READERR 0x10000000
45 #define OTPP_VALUE_MASK 0x20000000
46 #define OTPP_VALUE_SHIFT 29
47 #define OTPP_START_BUSY 0x80000000
48 #define OTPP_READ 0x40000000
50 /* Opcodes for OTPP_OC field */
52 #define OTPPOC_BIT_PROG 1
53 #define OTPPOC_VERIFY 3
56 #define OTPPOC_RESET 6
58 #define OTPPOC_ROW_LOCK 8
59 #define OTPPOC_PRESCN_TEST 9
61 #define OTPTYPE_IPX(ccrev) ((ccrev) == 21 || (ccrev) >= 23)
63 #define OTPP_TRIES 10000000 /* # of tries for OTPP */
65 #define MAXNUMRDES 9 /* Maximum OTP redundancy entries */
67 /* Fixed size subregions sizes in words */
72 /* OTP function struct */
74 int (*init
)(struct si_pub
*sih
, struct otpinfo
*oi
);
75 int (*read_region
)(struct otpinfo
*oi
, int region
, u16
*data
,
80 struct bcma_device
*core
; /* chipc core */
81 const struct otp_fn_s
*fn
; /* OTP functions */
82 struct si_pub
*sih
; /* Saved sb handle */
85 u16 wsize
; /* Size of otp in words */
86 u16 rows
; /* Geometry */
87 u16 cols
; /* Geometry */
88 u32 status
; /* Flag bits (lock/prog/rv).
89 * (Reflected only when OTP is power cycled)
91 u16 hwbase
; /* hardware subregion offset */
92 u16 hwlim
; /* hardware subregion boundary */
93 u16 swbase
; /* software subregion offset */
94 u16 swlim
; /* software subregion boundary */
95 u16 fbase
; /* fuse subregion offset */
96 u16 flim
; /* fuse subregion boundary */
97 int otpgu_base
; /* offset to General Use Region */
101 /* CC revs 21, 24 and 27 OTP General Use Region word offset */
102 #define REVA4_OTPGU_BASE 12
104 /* CC revs 23, 25, 26, 28 and above OTP General Use Region word offset */
105 #define REVB8_OTPGU_BASE 20
107 /* CC rev 36 OTP General Use Region word offset */
108 #define REV36_OTPGU_BASE 12
110 /* Subregion word offsets in General Use region */
111 #define OTPGU_HSB_OFF 0
112 #define OTPGU_SFB_OFF 1
113 #define OTPGU_CI_OFF 2
114 #define OTPGU_P_OFF 3
115 #define OTPGU_SROM_OFF 4
117 /* Flag bit offsets in General Use region */
118 #define OTPGU_HWP_OFF 60
119 #define OTPGU_SWP_OFF 61
120 #define OTPGU_CIP_OFF 62
121 #define OTPGU_FUSEP_OFF 63
122 #define OTPGU_CIP_MSK 0x4000
123 #define OTPGU_P_MSK 0xf000
124 #define OTPGU_P_SHIFT (OTPGU_HWP_OFF % 16)
127 #define OTP_SZ_FU_324 ((roundup(324, 8))/8) /* 324 bits */
128 #define OTP_SZ_FU_288 (288/8) /* 288 bits */
129 #define OTP_SZ_FU_216 (216/8) /* 216 bits */
130 #define OTP_SZ_FU_72 (72/8) /* 72 bits */
131 #define OTP_SZ_CHECKSUM (16/8) /* 16 bits */
132 #define OTP4315_SWREG_SZ 178 /* 178 bytes */
133 #define OTP_SZ_FU_144 (144/8) /* 144 bits */
136 ipxotp_otpr(struct otpinfo
*oi
, uint wn
)
138 return bcma_read16(oi
->core
,
139 CHIPCREGOFFS(sromotp
[wn
]));
143 * Calculate max HW/SW region byte size by subtracting fuse region
144 * and checksum size, osizew is oi->wsize (OTP size - GU size) in words
146 static int ipxotp_max_rgnsz(struct si_pub
*sih
, int osizew
)
150 switch (ai_get_chip_id(sih
)) {
151 case BCM43224_CHIP_ID
:
152 case BCM43225_CHIP_ID
:
153 ret
= osizew
* 2 - OTP_SZ_FU_72
- OTP_SZ_CHECKSUM
;
155 case BCM4313_CHIP_ID
:
156 ret
= osizew
* 2 - OTP_SZ_FU_72
- OTP_SZ_CHECKSUM
;
159 break; /* Don't know about this chip */
165 static void _ipxotp_init(struct otpinfo
*oi
)
169 int ccrev
= ai_get_ccrev(oi
->sih
);
173 * record word offset of General Use Region
174 * for various chipcommon revs
176 if (ccrev
== 21 || ccrev
== 24
178 oi
->otpgu_base
= REVA4_OTPGU_BASE
;
179 } else if (ccrev
== 36) {
181 * OTP size greater than equal to 2KB (128 words),
182 * otpgu_base is similar to rev23
184 if (oi
->wsize
>= 128)
185 oi
->otpgu_base
= REVB8_OTPGU_BASE
;
187 oi
->otpgu_base
= REV36_OTPGU_BASE
;
188 } else if (ccrev
== 23 || ccrev
>= 25) {
189 oi
->otpgu_base
= REVB8_OTPGU_BASE
;
192 /* First issue an init command so the status is up to date */
194 OTPP_START_BUSY
| ((OTPPOC_INIT
<< OTPP_OC_SHIFT
) & OTPP_OC_MASK
);
196 bcma_write32(oi
->core
, CHIPCREGOFFS(otpprog
), otpp
);
197 st
= bcma_read32(oi
->core
, CHIPCREGOFFS(otpprog
));
198 for (k
= 0; (st
& OTPP_START_BUSY
) && (k
< OTPP_TRIES
); k
++)
199 st
= bcma_read32(oi
->core
, CHIPCREGOFFS(otpprog
));
203 /* Read OTP lock bits and subregion programmed indication bits */
204 oi
->status
= bcma_read32(oi
->core
, CHIPCREGOFFS(otpstatus
));
206 if ((ai_get_chip_id(oi
->sih
) == BCM43224_CHIP_ID
)
207 || (ai_get_chip_id(oi
->sih
) == BCM43225_CHIP_ID
)) {
209 p_bits
= (ipxotp_otpr(oi
, oi
->otpgu_base
+ OTPGU_P_OFF
) &
210 OTPGU_P_MSK
) >> OTPGU_P_SHIFT
;
211 oi
->status
|= (p_bits
<< OTPS_GUP_SHIFT
);
215 * h/w region base and fuse region limit are fixed to
216 * the top and the bottom of the general use region.
217 * Everything else can be flexible.
219 oi
->hwbase
= oi
->otpgu_base
+ OTPGU_SROM_OFF
;
220 oi
->hwlim
= oi
->wsize
;
221 if (oi
->status
& OTPS_GUP_HW
) {
223 ipxotp_otpr(oi
, oi
->otpgu_base
+ OTPGU_HSB_OFF
) / 16;
224 oi
->swbase
= oi
->hwlim
;
226 oi
->swbase
= oi
->hwbase
;
228 /* subtract fuse and checksum from beginning */
229 oi
->swlim
= ipxotp_max_rgnsz(oi
->sih
, oi
->wsize
) / 2;
231 if (oi
->status
& OTPS_GUP_SW
) {
233 ipxotp_otpr(oi
, oi
->otpgu_base
+ OTPGU_SFB_OFF
) / 16;
234 oi
->fbase
= oi
->swlim
;
236 oi
->fbase
= oi
->swbase
;
238 oi
->flim
= oi
->wsize
;
241 static int ipxotp_init(struct si_pub
*sih
, struct otpinfo
*oi
)
243 /* Make sure we're running IPX OTP */
244 if (!OTPTYPE_IPX(ai_get_ccrev(sih
)))
247 /* Make sure OTP is not disabled */
248 if (ai_is_otp_disabled(sih
))
251 /* Check for otp size */
252 switch ((ai_get_cccaps(sih
) & CC_CAP_OTPSIZE
) >> CC_CAP_OTPSIZE_SHIFT
) {
271 case 7: /* 16x64 *//* 1024 bits */
277 /* Don't know the geometry */
281 /* Retrieve OTP region info */
287 ipxotp_read_region(struct otpinfo
*oi
, int region
, u16
*data
, uint
*wlen
)
291 /* Validate region selection */
294 sz
= (uint
) oi
->hwlim
- oi
->hwbase
;
295 if (!(oi
->status
& OTPS_GUP_HW
)) {
306 sz
= ((uint
) oi
->swlim
- oi
->swbase
);
307 if (!(oi
->status
& OTPS_GUP_SW
)) {
319 if (!(oi
->status
& OTPS_GUP_CI
)) {
327 base
= oi
->otpgu_base
+ OTPGU_CI_OFF
;
330 sz
= (uint
) oi
->flim
- oi
->fbase
;
331 if (!(oi
->status
& OTPS_GUP_FUSE
)) {
342 sz
= ((uint
) oi
->flim
- oi
->hwbase
);
343 if (!(oi
->status
& (OTPS_GUP_HW
| OTPS_GUP_SW
))) {
358 for (i
= 0; i
< sz
; i
++)
359 data
[i
] = ipxotp_otpr(oi
, base
+ i
);
365 static const struct otp_fn_s ipxotp_fn
= {
366 (int (*)(struct si_pub
*, struct otpinfo
*)) ipxotp_init
,
367 (int (*)(struct otpinfo
*, int, u16
*, uint
*)) ipxotp_read_region
,
370 static int otp_init(struct si_pub
*sih
, struct otpinfo
*oi
)
374 memset(oi
, 0, sizeof(struct otpinfo
));
376 oi
->core
= ai_findcore(sih
, BCMA_CORE_CHIPCOMMON
, 0);
378 if (OTPTYPE_IPX(ai_get_ccrev(sih
)))
386 ret
= (oi
->fn
->init
)(sih
, oi
);
392 otp_read_region(struct si_pub
*sih
, int region
, u16
*data
, uint
*wlen
) {
393 struct otpinfo otpinfo
;
394 struct otpinfo
*oi
= &otpinfo
;
397 if (ai_is_otp_disabled(sih
)) {
402 err
= otp_init(sih
, oi
);
406 err
= ((oi
)->fn
->read_region
)(oi
, region
, data
, wlen
);