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.
20 #include <linux/kernel.h>
21 #include <linux/string.h>
26 #include <bcmendian.h>
30 #include "siutils_priv.h"
33 * There are two different OTP controllers so far:
34 * 1. new IPX OTP controller: chipc 21, >=23
35 * 2. older HND OTP controller: chipc 12, 17, 22
37 * Define BCMHNDOTP to include support for the HND OTP controller.
38 * Define BCMIPXOTP to include support for the IPX OTP controller.
40 * NOTE 1: More than one may be defined
41 * NOTE 2: If none are defined, the default is to include them all.
44 #if !defined(BCMHNDOTP) && !defined(BCMIPXOTP)
49 #define OTPTYPE_HND(ccrev) ((ccrev) < 21 || (ccrev) == 22)
50 #define OTPTYPE_IPX(ccrev) ((ccrev) == 21 || (ccrev) >= 23)
52 #define OTPP_TRIES 10000000 /* # of tries for OTPP */
55 #define MAXNUMRDES 9 /* Maximum OTP redundancy entries */
58 /* OTP common function type */
59 typedef int (*otp_status_t
) (void *oh
);
60 typedef int (*otp_size_t
) (void *oh
);
61 typedef void *(*otp_init_t
) (si_t
*sih
);
62 typedef u16(*otp_read_bit_t
) (void *oh
, chipcregs_t
*cc
, uint off
);
63 typedef int (*otp_read_region_t
) (si_t
*sih
, int region
, u16
*data
,
65 typedef int (*otp_nvread_t
) (void *oh
, char *data
, uint
*len
);
67 /* OTP function struct */
68 typedef struct otp_fn_s
{
70 otp_read_bit_t read_bit
;
72 otp_read_region_t read_region
;
78 uint ccrev
; /* chipc revision */
79 otp_fn_t
*fn
; /* OTP functions */
80 si_t
*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 */
98 #endif /* BCMIPXOTP */
101 /* HND OTP section */
102 uint size
; /* Size of otp in bytes */
103 uint hwprot
; /* Hardware protection bits */
104 uint signvalid
; /* Signature valid bits */
105 int boundary
; /* hw/sw boundary */
106 #endif /* BCMHNDOTP */
109 static otpinfo_t otpinfo
;
114 * Exported functions:
119 * ipxotp_read_region()
126 #define HWSW_RGN(rgn) (((rgn) == OTP_HW_RGN) ? "h/w" : "s/w")
129 /* CC revs 21, 24 and 27 OTP General Use Region word offset */
130 #define REVA4_OTPGU_BASE 12
132 /* CC revs 23, 25, 26, 28 and above OTP General Use Region word offset */
133 #define REVB8_OTPGU_BASE 20
135 /* CC rev 36 OTP General Use Region word offset */
136 #define REV36_OTPGU_BASE 12
138 /* Subregion word offsets in General Use region */
139 #define OTPGU_HSB_OFF 0
140 #define OTPGU_SFB_OFF 1
141 #define OTPGU_CI_OFF 2
142 #define OTPGU_P_OFF 3
143 #define OTPGU_SROM_OFF 4
145 /* Flag bit offsets in General Use region */
146 #define OTPGU_HWP_OFF 60
147 #define OTPGU_SWP_OFF 61
148 #define OTPGU_CIP_OFF 62
149 #define OTPGU_FUSEP_OFF 63
150 #define OTPGU_CIP_MSK 0x4000
151 #define OTPGU_P_MSK 0xf000
152 #define OTPGU_P_SHIFT (OTPGU_HWP_OFF % 16)
155 #define OTP_SZ_FU_324 ((roundup(324, 8))/8) /* 324 bits */
156 #define OTP_SZ_FU_288 (288/8) /* 288 bits */
157 #define OTP_SZ_FU_216 (216/8) /* 216 bits */
158 #define OTP_SZ_FU_72 (72/8) /* 72 bits */
159 #define OTP_SZ_CHECKSUM (16/8) /* 16 bits */
160 #define OTP4315_SWREG_SZ 178 /* 178 bytes */
161 #define OTP_SZ_FU_144 (144/8) /* 144 bits */
163 static int ipxotp_status(void *oh
)
165 otpinfo_t
*oi
= (otpinfo_t
*) oh
;
166 return (int)(oi
->status
);
169 /* Return size in bytes */
170 static int ipxotp_size(void *oh
)
172 otpinfo_t
*oi
= (otpinfo_t
*) oh
;
173 return (int)oi
->wsize
* 2;
176 static u16
ipxotp_otpr(void *oh
, chipcregs_t
*cc
, uint wn
)
180 oi
= (otpinfo_t
*) oh
;
182 ASSERT(wn
< oi
->wsize
);
185 return R_REG(oi
->osh
, &cc
->sromotp
[wn
]);
188 static u16
ipxotp_read_bit(void *oh
, chipcregs_t
*cc
, uint off
)
190 otpinfo_t
*oi
= (otpinfo_t
*) oh
;
194 row
= off
/ oi
->cols
;
195 col
= off
% oi
->cols
;
197 otpp
= OTPP_START_BUSY
|
198 ((OTPPOC_READ
<< OTPP_OC_SHIFT
) & OTPP_OC_MASK
) |
199 ((row
<< OTPP_ROW_SHIFT
) & OTPP_ROW_MASK
) |
200 ((col
<< OTPP_COL_SHIFT
) & OTPP_COL_MASK
);
201 W_REG(oi
->osh
, &cc
->otpprog
, otpp
);
204 ((st
= R_REG(oi
->osh
, &cc
->otpprog
)) & OTPP_START_BUSY
)
205 && (k
< OTPP_TRIES
); k
++)
207 if (k
>= OTPP_TRIES
) {
210 if (st
& OTPP_READERR
) {
213 st
= (st
& OTPP_VALUE_MASK
) >> OTPP_VALUE_SHIFT
;
218 /* Calculate max HW/SW region byte size by substracting fuse region and checksum size,
219 * osizew is oi->wsize (OTP size - GU size) in words
221 static int ipxotp_max_rgnsz(si_t
*sih
, int osizew
)
225 switch (CHIPID(sih
->chip
)) {
226 case BCM43224_CHIP_ID
:
227 case BCM43225_CHIP_ID
:
228 ret
= osizew
* 2 - OTP_SZ_FU_72
- OTP_SZ_CHECKSUM
;
230 case BCM4313_CHIP_ID
:
231 ret
= osizew
* 2 - OTP_SZ_FU_72
- OTP_SZ_CHECKSUM
;
234 ASSERT(0); /* Don't konw about this chip */
240 static void BCMNMIATTACHFN(_ipxotp_init
) (otpinfo_t
*oi
, chipcregs_t
*cc
)
245 /* record word offset of General Use Region for various chipcommon revs */
246 if (oi
->sih
->ccrev
== 21 || oi
->sih
->ccrev
== 24
247 || oi
->sih
->ccrev
== 27) {
248 oi
->otpgu_base
= REVA4_OTPGU_BASE
;
249 } else if (oi
->sih
->ccrev
== 36) {
250 /* OTP size greater than equal to 2KB (128 words), otpgu_base is similar to rev23 */
251 if (oi
->wsize
>= 128)
252 oi
->otpgu_base
= REVB8_OTPGU_BASE
;
254 oi
->otpgu_base
= REV36_OTPGU_BASE
;
255 } else if (oi
->sih
->ccrev
== 23 || oi
->sih
->ccrev
>= 25) {
256 oi
->otpgu_base
= REVB8_OTPGU_BASE
;
259 /* First issue an init command so the status is up to date */
261 OTPP_START_BUSY
| ((OTPPOC_INIT
<< OTPP_OC_SHIFT
) & OTPP_OC_MASK
);
263 W_REG(oi
->osh
, &cc
->otpprog
, otpp
);
265 ((st
= R_REG(oi
->osh
, &cc
->otpprog
)) & OTPP_START_BUSY
)
266 && (k
< OTPP_TRIES
); k
++)
268 if (k
>= OTPP_TRIES
) {
272 /* Read OTP lock bits and subregion programmed indication bits */
273 oi
->status
= R_REG(oi
->osh
, &cc
->otpstatus
);
275 if ((CHIPID(oi
->sih
->chip
) == BCM43224_CHIP_ID
)
276 || (CHIPID(oi
->sih
->chip
) == BCM43225_CHIP_ID
)) {
279 (ipxotp_otpr(oi
, cc
, oi
->otpgu_base
+ OTPGU_P_OFF
) &
282 oi
->status
|= (p_bits
<< OTPS_GUP_SHIFT
);
286 * h/w region base and fuse region limit are fixed to the top and
287 * the bottom of the general use region. Everything else can be flexible.
289 oi
->hwbase
= oi
->otpgu_base
+ OTPGU_SROM_OFF
;
290 oi
->hwlim
= oi
->wsize
;
291 if (oi
->status
& OTPS_GUP_HW
) {
293 ipxotp_otpr(oi
, cc
, oi
->otpgu_base
+ OTPGU_HSB_OFF
) / 16;
294 oi
->swbase
= oi
->hwlim
;
296 oi
->swbase
= oi
->hwbase
;
298 /* subtract fuse and checksum from beginning */
299 oi
->swlim
= ipxotp_max_rgnsz(oi
->sih
, oi
->wsize
) / 2;
301 if (oi
->status
& OTPS_GUP_SW
) {
303 ipxotp_otpr(oi
, cc
, oi
->otpgu_base
+ OTPGU_SFB_OFF
) / 16;
304 oi
->fbase
= oi
->swlim
;
306 oi
->fbase
= oi
->swbase
;
308 oi
->flim
= oi
->wsize
;
311 static void *BCMNMIATTACHFN(ipxotp_init
) (si_t
*sih
)
317 /* Make sure we're running IPX OTP */
318 ASSERT(OTPTYPE_IPX(sih
->ccrev
));
319 if (!OTPTYPE_IPX(sih
->ccrev
))
322 /* Make sure OTP is not disabled */
323 if (si_is_otp_disabled(sih
)) {
327 /* Make sure OTP is powered up */
328 if (!si_is_otp_powered(sih
)) {
334 /* Check for otp size */
335 switch ((sih
->cccaps
& CC_CAP_OTPSIZE
) >> CC_CAP_OTPSIZE_SHIFT
) {
354 case 7: /* 16x64 *//* 1024 bits */
360 /* Don't know the geometry */
364 /* Retrieve OTP region info */
365 idx
= si_coreidx(sih
);
366 cc
= si_setcoreidx(sih
, SI_CC_IDX
);
369 _ipxotp_init(oi
, cc
);
371 si_setcoreidx(sih
, idx
);
376 static int ipxotp_read_region(void *oh
, int region
, u16
*data
, uint
*wlen
)
378 otpinfo_t
*oi
= (otpinfo_t
*) oh
;
383 /* Validate region selection */
386 sz
= (uint
) oi
->hwlim
- oi
->hwbase
;
387 if (!(oi
->status
& OTPS_GUP_HW
)) {
389 return BCME_NOTFOUND
;
393 return BCME_BUFTOOSHORT
;
398 sz
= ((uint
) oi
->swlim
- oi
->swbase
);
399 if (!(oi
->status
& OTPS_GUP_SW
)) {
401 return BCME_NOTFOUND
;
405 return BCME_BUFTOOSHORT
;
411 if (!(oi
->status
& OTPS_GUP_CI
)) {
413 return BCME_NOTFOUND
;
417 return BCME_BUFTOOSHORT
;
419 base
= oi
->otpgu_base
+ OTPGU_CI_OFF
;
422 sz
= (uint
) oi
->flim
- oi
->fbase
;
423 if (!(oi
->status
& OTPS_GUP_FUSE
)) {
425 return BCME_NOTFOUND
;
429 return BCME_BUFTOOSHORT
;
434 sz
= ((uint
) oi
->flim
- oi
->hwbase
);
435 if (!(oi
->status
& (OTPS_GUP_HW
| OTPS_GUP_SW
))) {
437 return BCME_NOTFOUND
;
441 return BCME_BUFTOOSHORT
;
449 idx
= si_coreidx(oi
->sih
);
450 cc
= si_setcoreidx(oi
->sih
, SI_CC_IDX
);
454 for (i
= 0; i
< sz
; i
++)
455 data
[i
] = ipxotp_otpr(oh
, cc
, base
+ i
);
457 si_setcoreidx(oi
->sih
, idx
);
462 static int ipxotp_nvread(void *oh
, char *data
, uint
*len
)
464 return BCME_UNSUPPORTED
;
467 static otp_fn_t ipxotp_fn
= {
468 (otp_size_t
) ipxotp_size
,
469 (otp_read_bit_t
) ipxotp_read_bit
,
471 (otp_init_t
) ipxotp_init
,
472 (otp_read_region_t
) ipxotp_read_region
,
473 (otp_nvread_t
) ipxotp_nvread
,
475 (otp_status_t
) ipxotp_status
478 #endif /* BCMIPXOTP */
483 * Exported functions:
488 * hndotp_read_region()
495 /* Fields in otpstatus */
496 #define OTPS_PROGFAIL 0x80000000
497 #define OTPS_PROTECT 0x00000007
498 #define OTPS_HW_PROTECT 0x00000001
499 #define OTPS_SW_PROTECT 0x00000002
500 #define OTPS_CID_PROTECT 0x00000004
501 #define OTPS_RCEV_MSK 0x00003f00
502 #define OTPS_RCEV_SHIFT 8
504 /* Fields in the otpcontrol register */
505 #define OTPC_RECWAIT 0xff000000
506 #define OTPC_PROGWAIT 0x00ffff00
507 #define OTPC_PRW_SHIFT 8
508 #define OTPC_MAXFAIL 0x00000038
509 #define OTPC_VSEL 0x00000006
510 #define OTPC_SELVL 0x00000001
512 /* OTP regions (Word offsets from otp size) */
513 #define OTP_SWLIM_OFF (-4)
514 #define OTP_CIDBASE_OFF 0
515 #define OTP_CIDLIM_OFF 4
517 /* Predefined OTP words (Word offset from otp size) */
518 #define OTP_BOUNDARY_OFF (-4)
519 #define OTP_HWSIGN_OFF (-3)
520 #define OTP_SWSIGN_OFF (-2)
521 #define OTP_CIDSIGN_OFF (-1)
522 #define OTP_CID_OFF 0
523 #define OTP_PKG_OFF 1
524 #define OTP_FID_OFF 2
525 #define OTP_RSV_OFF 3
526 #define OTP_LIM_OFF 4
527 #define OTP_RD_OFF 4 /* Redundancy row starts here */
528 #define OTP_RC0_OFF 28 /* Redundancy control word 1 */
529 #define OTP_RC1_OFF 32 /* Redundancy control word 2 */
530 #define OTP_RC_LIM_OFF 36 /* Redundancy control word end */
532 #define OTP_HW_REGION OTPS_HW_PROTECT
533 #define OTP_SW_REGION OTPS_SW_PROTECT
534 #define OTP_CID_REGION OTPS_CID_PROTECT
536 #if OTP_HW_REGION != OTP_HW_RGN
537 #error "incompatible OTP_HW_RGN"
539 #if OTP_SW_REGION != OTP_SW_RGN
540 #error "incompatible OTP_SW_RGN"
542 #if OTP_CID_REGION != OTP_CI_RGN
543 #error "incompatible OTP_CI_RGN"
546 /* Redundancy entry definitions */
547 #define OTP_RCE_ROW_SZ 6
548 #define OTP_RCE_SIGN_MASK 0x7fff
549 #define OTP_RCE_ROW_MASK 0x3f
550 #define OTP_RCE_BITS 21
551 #define OTP_RCE_SIGN_SZ 15
552 #define OTP_RCE_BIT0 1
555 #define OTP_SIGNATURE 0x578a
556 #define OTP_MAGIC 0x4e56
558 static int hndotp_status(void *oh
)
560 otpinfo_t
*oi
= (otpinfo_t
*) oh
;
561 return (int)(oi
->hwprot
| oi
->signvalid
);
564 static int hndotp_size(void *oh
)
566 otpinfo_t
*oi
= (otpinfo_t
*) oh
;
567 return (int)(oi
->size
);
570 static u16
hndotp_otpr(void *oh
, chipcregs_t
*cc
, uint wn
)
572 otpinfo_t
*oi
= (otpinfo_t
*) oh
;
576 ASSERT(wn
< ((oi
->size
/ 2) + OTP_RC_LIM_OFF
));
579 osh
= si_osh(oi
->sih
);
581 ptr
= (volatile u16
*)((volatile char *)cc
+ CC_SROM_OTP
);
582 return R_REG(osh
, &ptr
[wn
]);
585 static u16
hndotp_otproff(void *oh
, chipcregs_t
*cc
, int woff
)
587 otpinfo_t
*oi
= (otpinfo_t
*) oh
;
591 ASSERT(woff
>= (-((int)oi
->size
/ 2)));
592 ASSERT(woff
< OTP_LIM_OFF
);
595 osh
= si_osh(oi
->sih
);
597 ptr
= (volatile u16
*)((volatile char *)cc
+ CC_SROM_OTP
);
599 return R_REG(osh
, &ptr
[(oi
->size
/ 2) + woff
]);
602 static u16
hndotp_read_bit(void *oh
, chipcregs_t
*cc
, uint idx
)
604 otpinfo_t
*oi
= (otpinfo_t
*) oh
;
609 osh
= si_osh(oi
->sih
);
613 otpp
= OTPP_START_BUSY
| OTPP_READ
|
614 ((row
<< OTPP_ROW_SHIFT
) & OTPP_ROW_MASK
) | (col
& OTPP_COL_MASK
);
616 W_REG(osh
, &cc
->otpprog
, otpp
);
617 st
= R_REG(osh
, &cc
->otpprog
);
619 ((st
& OTPP_START_BUSY
) == OTPP_START_BUSY
) && (k
< OTPP_TRIES
);
621 st
= R_REG(osh
, &cc
->otpprog
);
623 if (k
>= OTPP_TRIES
) {
626 if (st
& OTPP_READERR
) {
629 st
= (st
& OTPP_VALUE_MASK
) >> OTPP_VALUE_SHIFT
;
633 static void *BCMNMIATTACHFN(hndotp_init
) (si_t
*sih
)
638 u32 cap
= 0, clkdiv
, otpdiv
= 0;
644 idx
= si_coreidx(sih
);
645 osh
= si_osh(oi
->sih
);
648 cc
= si_setcoreidx(sih
, SI_CC_IDX
);
650 cap
= R_REG(osh
, &cc
->capabilities
);
651 if ((cap
& CC_CAP_OTPSIZE
) == 0) {
656 /* As of right now, support only 4320a2, 4311a1 and 4312 */
657 ASSERT((oi
->ccrev
== 12) || (oi
->ccrev
== 17)
658 || (oi
->ccrev
== 22));
660 ((oi
->ccrev
== 12) || (oi
->ccrev
== 17)
661 || (oi
->ccrev
== 22)))
664 /* Read the OTP byte size. chipcommon rev >= 18 has RCE so the size is
665 * 8 row (64 bytes) smaller
668 1 << (((cap
& CC_CAP_OTPSIZE
) >> CC_CAP_OTPSIZE_SHIFT
)
669 + CC_CAP_OTPSIZE_BASE
);
671 oi
->size
-= ((OTP_RC0_OFF
- OTP_BOUNDARY_OFF
) * 2);
673 oi
->hwprot
= (int)(R_REG(osh
, &cc
->otpstatus
) & OTPS_PROTECT
);
676 /* Check the region signature */
677 if (hndotp_otproff(oi
, cc
, OTP_HWSIGN_OFF
) == OTP_SIGNATURE
) {
678 oi
->signvalid
|= OTP_HW_REGION
;
679 oi
->boundary
= hndotp_otproff(oi
, cc
, OTP_BOUNDARY_OFF
);
682 if (hndotp_otproff(oi
, cc
, OTP_SWSIGN_OFF
) == OTP_SIGNATURE
)
683 oi
->signvalid
|= OTP_SW_REGION
;
685 if (hndotp_otproff(oi
, cc
, OTP_CIDSIGN_OFF
) == OTP_SIGNATURE
)
686 oi
->signvalid
|= OTP_CID_REGION
;
688 /* Set OTP clkdiv for stability */
693 clkdiv
= R_REG(osh
, &cc
->clkdiv
);
695 (clkdiv
& ~CLKD_OTP
) | (otpdiv
<< CLKD_OTP_SHIFT
);
696 W_REG(osh
, &cc
->clkdiv
, clkdiv
);
704 si_setcoreidx(sih
, idx
);
709 static int hndotp_read_region(void *oh
, int region
, u16
*data
, uint
*wlen
)
711 otpinfo_t
*oi
= (otpinfo_t
*) oh
;
716 /* Only support HW region (no active chips use HND OTP SW region) */
717 ASSERT(region
== OTP_HW_REGION
);
720 st
= oi
->hwprot
| oi
->signvalid
;
721 if ((st
& region
) == 0)
722 return BCME_NOTFOUND
;
725 ((int)*wlen
< oi
->boundary
/ 2) ? *wlen
: (uint
) oi
->boundary
/ 2;
727 idx
= si_coreidx(oi
->sih
);
728 cc
= si_setcoreidx(oi
->sih
, SI_CC_IDX
);
731 for (i
= 0; i
< (int)*wlen
; i
++)
732 data
[i
] = hndotp_otpr(oh
, cc
, i
);
734 si_setcoreidx(oi
->sih
, idx
);
739 static int hndotp_nvread(void *oh
, char *data
, uint
*len
)
742 otpinfo_t
*oi
= (otpinfo_t
*) oh
;
743 u32 base
, bound
, lim
= 0, st
;
744 int i
, chunk
, gchunks
, tsz
= 0;
750 /* save the orig core */
751 idx
= si_coreidx(oi
->sih
);
752 cc
= si_setcoreidx(oi
->sih
, SI_CC_IDX
);
755 st
= hndotp_status(oh
);
756 if (!(st
& (OTP_HW_REGION
| OTP_SW_REGION
))) {
761 /* Read the whole otp so we can easily manipulate it */
762 lim
= hndotp_size(oh
);
763 rawotp
= MALLOC(si_osh(oi
->sih
), lim
);
764 if (rawotp
== NULL
) {
768 for (i
= 0; i
< (int)(lim
/ 2); i
++)
769 rawotp
[i
] = hndotp_otpr(oh
, cc
, i
);
771 if ((st
& OTP_HW_REGION
) == 0) {
772 /* This could be a programming failure in the first
773 * chunk followed by one or more good chunks
775 for (i
= 0; i
< (int)(lim
/ 2); i
++)
776 if (rawotp
[i
] == OTP_MAGIC
)
779 if (i
< (int)(lim
/ 2)) {
781 bound
= (i
* 2) + rawotp
[i
+ 1];
787 bound
= rawotp
[(lim
/ 2) + OTP_BOUNDARY_OFF
];
789 /* There are two cases: 1) The whole otp is used as nvram
790 * and 2) There is a hardware header followed by nvram.
792 if (rawotp
[0] == OTP_MAGIC
) {
798 /* Find and copy the data */
804 while ((i
< (int)(lim
/ 2)) && (rawotp
[i
] == OTP_MAGIC
)) {
805 int dsz
, rsz
= rawotp
[i
+ 1];
807 if (((i
* 2) + rsz
) >= (int)lim
) {
808 /* Bad length, try to find another chunk anyway */
811 if (hndcrc16((u8
*) &rawotp
[i
], rsz
,
812 CRC16_INIT_VALUE
) == CRC16_GOOD_VALUE
) {
813 /* Good crc, copy the vars */
817 if (offset
+ dsz
>= *len
) {
820 bcopy((char *)&rawotp
[i
+ 2], &data
[offset
], dsz
);
822 /* Remove extra null characters at the end */
824 data
[offset
- 1] == 0 && data
[offset
- 2] == 0)
828 /* bad length or crc didn't check, try to find the next set */
829 if (rawotp
[i
+ (rsz
/ 2)] == OTP_MAGIC
) {
830 /* Assume length is good */
833 while (++i
< (int)(lim
/ 2))
834 if (rawotp
[i
] == OTP_MAGIC
)
845 MFREE(si_osh(oi
->sih
), rawotp
, lim
);
846 si_setcoreidx(oi
->sih
, idx
);
851 static otp_fn_t hndotp_fn
= {
852 (otp_size_t
) hndotp_size
,
853 (otp_read_bit_t
) hndotp_read_bit
,
855 (otp_init_t
) hndotp_init
,
856 (otp_read_region_t
) hndotp_read_region
,
857 (otp_nvread_t
) hndotp_nvread
,
859 (otp_status_t
) hndotp_status
862 #endif /* BCMHNDOTP */
865 * Common Code: Compiled for IPX / HND / AUTO
874 int otp_status(void *oh
)
876 otpinfo_t
*oi
= (otpinfo_t
*) oh
;
878 return oi
->fn
->status(oh
);
881 int otp_size(void *oh
)
883 otpinfo_t
*oi
= (otpinfo_t
*) oh
;
885 return oi
->fn
->size(oh
);
888 u16
otp_read_bit(void *oh
, uint offset
)
890 otpinfo_t
*oi
= (otpinfo_t
*) oh
;
891 uint idx
= si_coreidx(oi
->sih
);
892 chipcregs_t
*cc
= si_setcoreidx(oi
->sih
, SI_CC_IDX
);
893 u16 readBit
= (u16
) oi
->fn
->read_bit(oh
, cc
, offset
);
894 si_setcoreidx(oi
->sih
, idx
);
898 void *BCMNMIATTACHFN(otp_init
) (si_t
*sih
)
904 bzero(oi
, sizeof(otpinfo_t
));
906 oi
->ccrev
= sih
->ccrev
;
909 if (OTPTYPE_IPX(oi
->ccrev
))
914 if (OTPTYPE_HND(oi
->ccrev
))
918 if (oi
->fn
== NULL
) {
923 oi
->osh
= si_osh(oi
->sih
);
925 ret
= (oi
->fn
->init
) (sih
);
931 BCMNMIATTACHFN(otp_read_region
) (si_t
*sih
, int region
, u16
*data
,
937 wasup
= si_is_otp_powered(sih
);
939 si_otp_power(sih
, TRUE
);
941 if (!si_is_otp_powered(sih
) || si_is_otp_disabled(sih
)) {
952 err
= (((otpinfo_t
*) oh
)->fn
->read_region
) (oh
, region
, data
, wlen
);
956 si_otp_power(sih
, FALSE
);
961 int otp_nvread(void *oh
, char *data
, uint
*len
)
963 otpinfo_t
*oi
= (otpinfo_t
*) oh
;
965 return oi
->fn
->nvread(oh
, data
, len
);