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.
17 #include <linux/delay.h>
18 #include <linux/kernel.h>
19 #include <linux/string.h>
22 #include <linux/module.h>
23 #include <linux/pci.h>
27 #include <bcmendian.h>
31 #include "siutils_priv.h"
34 * There are two different OTP controllers so far:
35 * 1. new IPX OTP controller: chipc 21, >=23
36 * 2. older HND OTP controller: chipc 12, 17, 22
38 * Define BCMHNDOTP to include support for the HND OTP controller.
39 * Define BCMIPXOTP to include support for the IPX OTP controller.
41 * NOTE 1: More than one may be defined
42 * NOTE 2: If none are defined, the default is to include them all.
45 #if !defined(BCMHNDOTP) && !defined(BCMIPXOTP)
50 #define OTPTYPE_HND(ccrev) ((ccrev) < 21 || (ccrev) == 22)
51 #define OTPTYPE_IPX(ccrev) ((ccrev) == 21 || (ccrev) >= 23)
53 #define OTPP_TRIES 10000000 /* # of tries for OTPP */
56 #define MAXNUMRDES 9 /* Maximum OTP redundancy entries */
59 /* OTP common function type */
60 typedef int (*otp_status_t
) (void *oh
);
61 typedef int (*otp_size_t
) (void *oh
);
62 typedef void *(*otp_init_t
) (si_t
*sih
);
63 typedef u16(*otp_read_bit_t
) (void *oh
, chipcregs_t
*cc
, uint off
);
64 typedef int (*otp_read_region_t
) (si_t
*sih
, int region
, u16
*data
,
66 typedef int (*otp_nvread_t
) (void *oh
, char *data
, uint
*len
);
68 /* OTP function struct */
69 typedef struct otp_fn_s
{
71 otp_read_bit_t read_bit
;
73 otp_read_region_t read_region
;
79 uint ccrev
; /* chipc revision */
80 otp_fn_t
*fn
; /* OTP functions */
81 si_t
*sih
; /* Saved sb handle */
86 u16 wsize
; /* Size of otp in words */
87 u16 rows
; /* Geometry */
88 u16 cols
; /* Geometry */
89 u32 status
; /* Flag bits (lock/prog/rv).
90 * (Reflected only when OTP is power cycled)
92 u16 hwbase
; /* hardware subregion offset */
93 u16 hwlim
; /* hardware subregion boundary */
94 u16 swbase
; /* software subregion offset */
95 u16 swlim
; /* software subregion boundary */
96 u16 fbase
; /* fuse subregion offset */
97 u16 flim
; /* fuse subregion boundary */
98 int otpgu_base
; /* offset to General Use Region */
99 #endif /* BCMIPXOTP */
102 /* HND OTP section */
103 uint size
; /* Size of otp in bytes */
104 uint hwprot
; /* Hardware protection bits */
105 uint signvalid
; /* Signature valid bits */
106 int boundary
; /* hw/sw boundary */
107 #endif /* BCMHNDOTP */
110 static otpinfo_t otpinfo
;
115 * Exported functions:
120 * ipxotp_read_region()
127 #define HWSW_RGN(rgn) (((rgn) == OTP_HW_RGN) ? "h/w" : "s/w")
130 /* CC revs 21, 24 and 27 OTP General Use Region word offset */
131 #define REVA4_OTPGU_BASE 12
133 /* CC revs 23, 25, 26, 28 and above OTP General Use Region word offset */
134 #define REVB8_OTPGU_BASE 20
136 /* CC rev 36 OTP General Use Region word offset */
137 #define REV36_OTPGU_BASE 12
139 /* Subregion word offsets in General Use region */
140 #define OTPGU_HSB_OFF 0
141 #define OTPGU_SFB_OFF 1
142 #define OTPGU_CI_OFF 2
143 #define OTPGU_P_OFF 3
144 #define OTPGU_SROM_OFF 4
146 /* Flag bit offsets in General Use region */
147 #define OTPGU_HWP_OFF 60
148 #define OTPGU_SWP_OFF 61
149 #define OTPGU_CIP_OFF 62
150 #define OTPGU_FUSEP_OFF 63
151 #define OTPGU_CIP_MSK 0x4000
152 #define OTPGU_P_MSK 0xf000
153 #define OTPGU_P_SHIFT (OTPGU_HWP_OFF % 16)
156 #define OTP_SZ_FU_324 ((roundup(324, 8))/8) /* 324 bits */
157 #define OTP_SZ_FU_288 (288/8) /* 288 bits */
158 #define OTP_SZ_FU_216 (216/8) /* 216 bits */
159 #define OTP_SZ_FU_72 (72/8) /* 72 bits */
160 #define OTP_SZ_CHECKSUM (16/8) /* 16 bits */
161 #define OTP4315_SWREG_SZ 178 /* 178 bytes */
162 #define OTP_SZ_FU_144 (144/8) /* 144 bits */
164 static int ipxotp_status(void *oh
)
166 otpinfo_t
*oi
= (otpinfo_t
*) oh
;
167 return (int)(oi
->status
);
170 /* Return size in bytes */
171 static int ipxotp_size(void *oh
)
173 otpinfo_t
*oi
= (otpinfo_t
*) oh
;
174 return (int)oi
->wsize
* 2;
177 static u16
ipxotp_otpr(void *oh
, chipcregs_t
*cc
, uint wn
)
181 oi
= (otpinfo_t
*) oh
;
183 ASSERT(wn
< oi
->wsize
);
186 return R_REG(oi
->osh
, &cc
->sromotp
[wn
]);
189 static u16
ipxotp_read_bit(void *oh
, chipcregs_t
*cc
, uint off
)
191 otpinfo_t
*oi
= (otpinfo_t
*) oh
;
195 row
= off
/ oi
->cols
;
196 col
= off
% oi
->cols
;
198 otpp
= OTPP_START_BUSY
|
199 ((OTPPOC_READ
<< OTPP_OC_SHIFT
) & OTPP_OC_MASK
) |
200 ((row
<< OTPP_ROW_SHIFT
) & OTPP_ROW_MASK
) |
201 ((col
<< OTPP_COL_SHIFT
) & OTPP_COL_MASK
);
202 W_REG(oi
->osh
, &cc
->otpprog
, otpp
);
205 ((st
= R_REG(oi
->osh
, &cc
->otpprog
)) & OTPP_START_BUSY
)
206 && (k
< OTPP_TRIES
); k
++)
208 if (k
>= OTPP_TRIES
) {
211 if (st
& OTPP_READERR
) {
214 st
= (st
& OTPP_VALUE_MASK
) >> OTPP_VALUE_SHIFT
;
219 /* Calculate max HW/SW region byte size by substracting fuse region and checksum size,
220 * osizew is oi->wsize (OTP size - GU size) in words
222 static int ipxotp_max_rgnsz(si_t
*sih
, int osizew
)
227 case BCM43224_CHIP_ID
:
228 case BCM43225_CHIP_ID
:
229 ret
= osizew
* 2 - OTP_SZ_FU_72
- OTP_SZ_CHECKSUM
;
231 case BCM4313_CHIP_ID
:
232 ret
= osizew
* 2 - OTP_SZ_FU_72
- OTP_SZ_CHECKSUM
;
235 ASSERT(0); /* Don't konw about this chip */
241 static void _ipxotp_init(otpinfo_t
*oi
, chipcregs_t
*cc
)
246 /* record word offset of General Use Region for various chipcommon revs */
247 if (oi
->sih
->ccrev
== 21 || oi
->sih
->ccrev
== 24
248 || oi
->sih
->ccrev
== 27) {
249 oi
->otpgu_base
= REVA4_OTPGU_BASE
;
250 } else if (oi
->sih
->ccrev
== 36) {
251 /* OTP size greater than equal to 2KB (128 words), otpgu_base is similar to rev23 */
252 if (oi
->wsize
>= 128)
253 oi
->otpgu_base
= REVB8_OTPGU_BASE
;
255 oi
->otpgu_base
= REV36_OTPGU_BASE
;
256 } else if (oi
->sih
->ccrev
== 23 || oi
->sih
->ccrev
>= 25) {
257 oi
->otpgu_base
= REVB8_OTPGU_BASE
;
260 /* First issue an init command so the status is up to date */
262 OTPP_START_BUSY
| ((OTPPOC_INIT
<< OTPP_OC_SHIFT
) & OTPP_OC_MASK
);
264 W_REG(oi
->osh
, &cc
->otpprog
, otpp
);
266 ((st
= R_REG(oi
->osh
, &cc
->otpprog
)) & OTPP_START_BUSY
)
267 && (k
< OTPP_TRIES
); k
++)
269 if (k
>= OTPP_TRIES
) {
273 /* Read OTP lock bits and subregion programmed indication bits */
274 oi
->status
= R_REG(oi
->osh
, &cc
->otpstatus
);
276 if ((oi
->sih
->chip
== BCM43224_CHIP_ID
)
277 || (oi
->sih
->chip
== BCM43225_CHIP_ID
)) {
280 (ipxotp_otpr(oi
, cc
, oi
->otpgu_base
+ OTPGU_P_OFF
) &
283 oi
->status
|= (p_bits
<< OTPS_GUP_SHIFT
);
287 * h/w region base and fuse region limit are fixed to the top and
288 * the bottom of the general use region. Everything else can be flexible.
290 oi
->hwbase
= oi
->otpgu_base
+ OTPGU_SROM_OFF
;
291 oi
->hwlim
= oi
->wsize
;
292 if (oi
->status
& OTPS_GUP_HW
) {
294 ipxotp_otpr(oi
, cc
, oi
->otpgu_base
+ OTPGU_HSB_OFF
) / 16;
295 oi
->swbase
= oi
->hwlim
;
297 oi
->swbase
= oi
->hwbase
;
299 /* subtract fuse and checksum from beginning */
300 oi
->swlim
= ipxotp_max_rgnsz(oi
->sih
, oi
->wsize
) / 2;
302 if (oi
->status
& OTPS_GUP_SW
) {
304 ipxotp_otpr(oi
, cc
, oi
->otpgu_base
+ OTPGU_SFB_OFF
) / 16;
305 oi
->fbase
= oi
->swlim
;
307 oi
->fbase
= oi
->swbase
;
309 oi
->flim
= oi
->wsize
;
312 static void *ipxotp_init(si_t
*sih
)
318 /* Make sure we're running IPX OTP */
319 ASSERT(OTPTYPE_IPX(sih
->ccrev
));
320 if (!OTPTYPE_IPX(sih
->ccrev
))
323 /* Make sure OTP is not disabled */
324 if (si_is_otp_disabled(sih
)) {
328 /* Make sure OTP is powered up */
329 if (!si_is_otp_powered(sih
)) {
335 /* Check for otp size */
336 switch ((sih
->cccaps
& CC_CAP_OTPSIZE
) >> CC_CAP_OTPSIZE_SHIFT
) {
355 case 7: /* 16x64 *//* 1024 bits */
361 /* Don't know the geometry */
365 /* Retrieve OTP region info */
366 idx
= si_coreidx(sih
);
367 cc
= si_setcoreidx(sih
, SI_CC_IDX
);
370 _ipxotp_init(oi
, cc
);
372 si_setcoreidx(sih
, idx
);
377 static int ipxotp_read_region(void *oh
, int region
, u16
*data
, uint
*wlen
)
379 otpinfo_t
*oi
= (otpinfo_t
*) oh
;
384 /* Validate region selection */
387 sz
= (uint
) oi
->hwlim
- oi
->hwbase
;
388 if (!(oi
->status
& OTPS_GUP_HW
)) {
390 return BCME_NOTFOUND
;
394 return BCME_BUFTOOSHORT
;
399 sz
= ((uint
) oi
->swlim
- oi
->swbase
);
400 if (!(oi
->status
& OTPS_GUP_SW
)) {
402 return BCME_NOTFOUND
;
406 return BCME_BUFTOOSHORT
;
412 if (!(oi
->status
& OTPS_GUP_CI
)) {
414 return BCME_NOTFOUND
;
418 return BCME_BUFTOOSHORT
;
420 base
= oi
->otpgu_base
+ OTPGU_CI_OFF
;
423 sz
= (uint
) oi
->flim
- oi
->fbase
;
424 if (!(oi
->status
& OTPS_GUP_FUSE
)) {
426 return BCME_NOTFOUND
;
430 return BCME_BUFTOOSHORT
;
435 sz
= ((uint
) oi
->flim
- oi
->hwbase
);
436 if (!(oi
->status
& (OTPS_GUP_HW
| OTPS_GUP_SW
))) {
438 return BCME_NOTFOUND
;
442 return BCME_BUFTOOSHORT
;
450 idx
= si_coreidx(oi
->sih
);
451 cc
= si_setcoreidx(oi
->sih
, SI_CC_IDX
);
455 for (i
= 0; i
< sz
; i
++)
456 data
[i
] = ipxotp_otpr(oh
, cc
, base
+ i
);
458 si_setcoreidx(oi
->sih
, idx
);
463 static int ipxotp_nvread(void *oh
, char *data
, uint
*len
)
465 return BCME_UNSUPPORTED
;
468 static otp_fn_t ipxotp_fn
= {
469 (otp_size_t
) ipxotp_size
,
470 (otp_read_bit_t
) ipxotp_read_bit
,
472 (otp_init_t
) ipxotp_init
,
473 (otp_read_region_t
) ipxotp_read_region
,
474 (otp_nvread_t
) ipxotp_nvread
,
476 (otp_status_t
) ipxotp_status
479 #endif /* BCMIPXOTP */
484 * Exported functions:
489 * hndotp_read_region()
496 /* Fields in otpstatus */
497 #define OTPS_PROGFAIL 0x80000000
498 #define OTPS_PROTECT 0x00000007
499 #define OTPS_HW_PROTECT 0x00000001
500 #define OTPS_SW_PROTECT 0x00000002
501 #define OTPS_CID_PROTECT 0x00000004
502 #define OTPS_RCEV_MSK 0x00003f00
503 #define OTPS_RCEV_SHIFT 8
505 /* Fields in the otpcontrol register */
506 #define OTPC_RECWAIT 0xff000000
507 #define OTPC_PROGWAIT 0x00ffff00
508 #define OTPC_PRW_SHIFT 8
509 #define OTPC_MAXFAIL 0x00000038
510 #define OTPC_VSEL 0x00000006
511 #define OTPC_SELVL 0x00000001
513 /* OTP regions (Word offsets from otp size) */
514 #define OTP_SWLIM_OFF (-4)
515 #define OTP_CIDBASE_OFF 0
516 #define OTP_CIDLIM_OFF 4
518 /* Predefined OTP words (Word offset from otp size) */
519 #define OTP_BOUNDARY_OFF (-4)
520 #define OTP_HWSIGN_OFF (-3)
521 #define OTP_SWSIGN_OFF (-2)
522 #define OTP_CIDSIGN_OFF (-1)
523 #define OTP_CID_OFF 0
524 #define OTP_PKG_OFF 1
525 #define OTP_FID_OFF 2
526 #define OTP_RSV_OFF 3
527 #define OTP_LIM_OFF 4
528 #define OTP_RD_OFF 4 /* Redundancy row starts here */
529 #define OTP_RC0_OFF 28 /* Redundancy control word 1 */
530 #define OTP_RC1_OFF 32 /* Redundancy control word 2 */
531 #define OTP_RC_LIM_OFF 36 /* Redundancy control word end */
533 #define OTP_HW_REGION OTPS_HW_PROTECT
534 #define OTP_SW_REGION OTPS_SW_PROTECT
535 #define OTP_CID_REGION OTPS_CID_PROTECT
537 #if OTP_HW_REGION != OTP_HW_RGN
538 #error "incompatible OTP_HW_RGN"
540 #if OTP_SW_REGION != OTP_SW_RGN
541 #error "incompatible OTP_SW_RGN"
543 #if OTP_CID_REGION != OTP_CI_RGN
544 #error "incompatible OTP_CI_RGN"
547 /* Redundancy entry definitions */
548 #define OTP_RCE_ROW_SZ 6
549 #define OTP_RCE_SIGN_MASK 0x7fff
550 #define OTP_RCE_ROW_MASK 0x3f
551 #define OTP_RCE_BITS 21
552 #define OTP_RCE_SIGN_SZ 15
553 #define OTP_RCE_BIT0 1
556 #define OTP_SIGNATURE 0x578a
557 #define OTP_MAGIC 0x4e56
559 static int hndotp_status(void *oh
)
561 otpinfo_t
*oi
= (otpinfo_t
*) oh
;
562 return (int)(oi
->hwprot
| oi
->signvalid
);
565 static int hndotp_size(void *oh
)
567 otpinfo_t
*oi
= (otpinfo_t
*) oh
;
568 return (int)(oi
->size
);
571 static u16
hndotp_otpr(void *oh
, chipcregs_t
*cc
, uint wn
)
573 otpinfo_t
*oi
= (otpinfo_t
*) oh
;
574 struct osl_info
*osh
;
577 ASSERT(wn
< ((oi
->size
/ 2) + OTP_RC_LIM_OFF
));
580 osh
= si_osh(oi
->sih
);
582 ptr
= (volatile u16
*)((volatile char *)cc
+ CC_SROM_OTP
);
583 return R_REG(osh
, &ptr
[wn
]);
586 static u16
hndotp_otproff(void *oh
, chipcregs_t
*cc
, int woff
)
588 otpinfo_t
*oi
= (otpinfo_t
*) oh
;
589 struct osl_info
*osh
;
592 ASSERT(woff
>= (-((int)oi
->size
/ 2)));
593 ASSERT(woff
< OTP_LIM_OFF
);
596 osh
= si_osh(oi
->sih
);
598 ptr
= (volatile u16
*)((volatile char *)cc
+ CC_SROM_OTP
);
600 return R_REG(osh
, &ptr
[(oi
->size
/ 2) + woff
]);
603 static u16
hndotp_read_bit(void *oh
, chipcregs_t
*cc
, uint idx
)
605 otpinfo_t
*oi
= (otpinfo_t
*) oh
;
608 struct osl_info
*osh
;
610 osh
= si_osh(oi
->sih
);
614 otpp
= OTPP_START_BUSY
| OTPP_READ
|
615 ((row
<< OTPP_ROW_SHIFT
) & OTPP_ROW_MASK
) | (col
& OTPP_COL_MASK
);
617 W_REG(osh
, &cc
->otpprog
, otpp
);
618 st
= R_REG(osh
, &cc
->otpprog
);
620 ((st
& OTPP_START_BUSY
) == OTPP_START_BUSY
) && (k
< OTPP_TRIES
);
622 st
= R_REG(osh
, &cc
->otpprog
);
624 if (k
>= OTPP_TRIES
) {
627 if (st
& OTPP_READERR
) {
630 st
= (st
& OTPP_VALUE_MASK
) >> OTPP_VALUE_SHIFT
;
634 static void *hndotp_init(si_t
*sih
)
639 u32 cap
= 0, clkdiv
, otpdiv
= 0;
641 struct osl_info
*osh
;
645 idx
= si_coreidx(sih
);
646 osh
= si_osh(oi
->sih
);
649 cc
= si_setcoreidx(sih
, SI_CC_IDX
);
651 cap
= R_REG(osh
, &cc
->capabilities
);
652 if ((cap
& CC_CAP_OTPSIZE
) == 0) {
657 /* As of right now, support only 4320a2, 4311a1 and 4312 */
658 ASSERT((oi
->ccrev
== 12) || (oi
->ccrev
== 17)
659 || (oi
->ccrev
== 22));
661 ((oi
->ccrev
== 12) || (oi
->ccrev
== 17)
662 || (oi
->ccrev
== 22)))
665 /* Read the OTP byte size. chipcommon rev >= 18 has RCE so the size is
666 * 8 row (64 bytes) smaller
669 1 << (((cap
& CC_CAP_OTPSIZE
) >> CC_CAP_OTPSIZE_SHIFT
)
670 + CC_CAP_OTPSIZE_BASE
);
672 oi
->size
-= ((OTP_RC0_OFF
- OTP_BOUNDARY_OFF
) * 2);
674 oi
->hwprot
= (int)(R_REG(osh
, &cc
->otpstatus
) & OTPS_PROTECT
);
677 /* Check the region signature */
678 if (hndotp_otproff(oi
, cc
, OTP_HWSIGN_OFF
) == OTP_SIGNATURE
) {
679 oi
->signvalid
|= OTP_HW_REGION
;
680 oi
->boundary
= hndotp_otproff(oi
, cc
, OTP_BOUNDARY_OFF
);
683 if (hndotp_otproff(oi
, cc
, OTP_SWSIGN_OFF
) == OTP_SIGNATURE
)
684 oi
->signvalid
|= OTP_SW_REGION
;
686 if (hndotp_otproff(oi
, cc
, OTP_CIDSIGN_OFF
) == OTP_SIGNATURE
)
687 oi
->signvalid
|= OTP_CID_REGION
;
689 /* Set OTP clkdiv for stability */
694 clkdiv
= R_REG(osh
, &cc
->clkdiv
);
696 (clkdiv
& ~CLKD_OTP
) | (otpdiv
<< CLKD_OTP_SHIFT
);
697 W_REG(osh
, &cc
->clkdiv
, clkdiv
);
705 si_setcoreidx(sih
, idx
);
710 static int hndotp_read_region(void *oh
, int region
, u16
*data
, uint
*wlen
)
712 otpinfo_t
*oi
= (otpinfo_t
*) oh
;
717 /* Only support HW region (no active chips use HND OTP SW region) */
718 ASSERT(region
== OTP_HW_REGION
);
721 st
= oi
->hwprot
| oi
->signvalid
;
722 if ((st
& region
) == 0)
723 return BCME_NOTFOUND
;
726 ((int)*wlen
< oi
->boundary
/ 2) ? *wlen
: (uint
) oi
->boundary
/ 2;
728 idx
= si_coreidx(oi
->sih
);
729 cc
= si_setcoreidx(oi
->sih
, SI_CC_IDX
);
732 for (i
= 0; i
< (int)*wlen
; i
++)
733 data
[i
] = hndotp_otpr(oh
, cc
, i
);
735 si_setcoreidx(oi
->sih
, idx
);
740 static int hndotp_nvread(void *oh
, char *data
, uint
*len
)
743 otpinfo_t
*oi
= (otpinfo_t
*) oh
;
744 u32 base
, bound
, lim
= 0, st
;
745 int i
, chunk
, gchunks
, tsz
= 0;
751 /* save the orig core */
752 idx
= si_coreidx(oi
->sih
);
753 cc
= si_setcoreidx(oi
->sih
, SI_CC_IDX
);
756 st
= hndotp_status(oh
);
757 if (!(st
& (OTP_HW_REGION
| OTP_SW_REGION
))) {
762 /* Read the whole otp so we can easily manipulate it */
763 lim
= hndotp_size(oh
);
764 rawotp
= kmalloc(lim
, GFP_ATOMIC
);
765 if (rawotp
== NULL
) {
769 for (i
= 0; i
< (int)(lim
/ 2); i
++)
770 rawotp
[i
] = hndotp_otpr(oh
, cc
, i
);
772 if ((st
& OTP_HW_REGION
) == 0) {
773 /* This could be a programming failure in the first
774 * chunk followed by one or more good chunks
776 for (i
= 0; i
< (int)(lim
/ 2); i
++)
777 if (rawotp
[i
] == OTP_MAGIC
)
780 if (i
< (int)(lim
/ 2)) {
782 bound
= (i
* 2) + rawotp
[i
+ 1];
788 bound
= rawotp
[(lim
/ 2) + OTP_BOUNDARY_OFF
];
790 /* There are two cases: 1) The whole otp is used as nvram
791 * and 2) There is a hardware header followed by nvram.
793 if (rawotp
[0] == OTP_MAGIC
) {
799 /* Find and copy the data */
805 while ((i
< (int)(lim
/ 2)) && (rawotp
[i
] == OTP_MAGIC
)) {
806 int dsz
, rsz
= rawotp
[i
+ 1];
808 if (((i
* 2) + rsz
) >= (int)lim
) {
809 /* Bad length, try to find another chunk anyway */
812 if (hndcrc16((u8
*) &rawotp
[i
], rsz
,
813 CRC16_INIT_VALUE
) == CRC16_GOOD_VALUE
) {
814 /* Good crc, copy the vars */
818 if (offset
+ dsz
>= *len
) {
821 bcopy((char *)&rawotp
[i
+ 2], &data
[offset
], dsz
);
823 /* Remove extra null characters at the end */
825 data
[offset
- 1] == 0 && data
[offset
- 2] == 0)
829 /* bad length or crc didn't check, try to find the next set */
830 if (rawotp
[i
+ (rsz
/ 2)] == OTP_MAGIC
) {
831 /* Assume length is good */
834 while (++i
< (int)(lim
/ 2))
835 if (rawotp
[i
] == OTP_MAGIC
)
847 si_setcoreidx(oi
->sih
, idx
);
852 static otp_fn_t hndotp_fn
= {
853 (otp_size_t
) hndotp_size
,
854 (otp_read_bit_t
) hndotp_read_bit
,
856 (otp_init_t
) hndotp_init
,
857 (otp_read_region_t
) hndotp_read_region
,
858 (otp_nvread_t
) hndotp_nvread
,
860 (otp_status_t
) hndotp_status
863 #endif /* BCMHNDOTP */
866 * Common Code: Compiled for IPX / HND / AUTO
875 int otp_status(void *oh
)
877 otpinfo_t
*oi
= (otpinfo_t
*) oh
;
879 return oi
->fn
->status(oh
);
882 int otp_size(void *oh
)
884 otpinfo_t
*oi
= (otpinfo_t
*) oh
;
886 return oi
->fn
->size(oh
);
889 u16
otp_read_bit(void *oh
, uint offset
)
891 otpinfo_t
*oi
= (otpinfo_t
*) oh
;
892 uint idx
= si_coreidx(oi
->sih
);
893 chipcregs_t
*cc
= si_setcoreidx(oi
->sih
, SI_CC_IDX
);
894 u16 readBit
= (u16
) oi
->fn
->read_bit(oh
, cc
, offset
);
895 si_setcoreidx(oi
->sih
, idx
);
899 void *otp_init(si_t
*sih
)
905 memset(oi
, 0, sizeof(otpinfo_t
));
907 oi
->ccrev
= sih
->ccrev
;
910 if (OTPTYPE_IPX(oi
->ccrev
))
915 if (OTPTYPE_HND(oi
->ccrev
))
919 if (oi
->fn
== NULL
) {
924 oi
->osh
= si_osh(oi
->sih
);
926 ret
= (oi
->fn
->init
) (sih
);
932 otp_read_region(si_t
*sih
, int region
, u16
*data
,
938 wasup
= si_is_otp_powered(sih
);
940 si_otp_power(sih
, true);
942 if (!si_is_otp_powered(sih
) || si_is_otp_disabled(sih
)) {
953 err
= (((otpinfo_t
*) oh
)->fn
->read_region
) (oh
, region
, data
, wlen
);
957 si_otp_power(sih
, false);
962 int otp_nvread(void *oh
, char *data
, uint
*len
)
964 otpinfo_t
*oi
= (otpinfo_t
*) oh
;
966 return oi
->fn
->nvread(oh
, data
, len
);