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>
20 #include <linux/module.h>
21 #include <linux/pci.h>
22 #include <linux/crc-ccitt.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 */
84 u16 wsize; /* Size of otp in words */
85 u16 rows; /* Geometry */
86 u16 cols; /* Geometry */
87 u32 status; /* Flag bits (lock/prog/rv).
88 * (Reflected only when OTP is power cycled)
90 u16 hwbase; /* hardware subregion offset */
91 u16 hwlim; /* hardware subregion boundary */
92 u16 swbase; /* software subregion offset */
93 u16 swlim; /* software subregion boundary */
94 u16 fbase; /* fuse subregion offset */
95 u16 flim; /* fuse subregion boundary */
96 int otpgu_base; /* offset to General Use Region */
97 #endif /* BCMIPXOTP */
100 /* HND OTP section */
101 uint size; /* Size of otp in bytes */
102 uint hwprot; /* Hardware protection bits */
103 uint signvalid; /* Signature valid bits */
104 int boundary; /* hw/sw boundary */
105 #endif /* BCMHNDOTP */
108 static otpinfo_t otpinfo;
113 * Exported functions:
118 * ipxotp_read_region()
125 #define HWSW_RGN(rgn) (((rgn) == OTP_HW_RGN) ? "h/w" : "s/w")
128 /* CC revs 21, 24 and 27 OTP General Use Region word offset */
129 #define REVA4_OTPGU_BASE 12
131 /* CC revs 23, 25, 26, 28 and above OTP General Use Region word offset */
132 #define REVB8_OTPGU_BASE 20
134 /* CC rev 36 OTP General Use Region word offset */
135 #define REV36_OTPGU_BASE 12
137 /* Subregion word offsets in General Use region */
138 #define OTPGU_HSB_OFF 0
139 #define OTPGU_SFB_OFF 1
140 #define OTPGU_CI_OFF 2
141 #define OTPGU_P_OFF 3
142 #define OTPGU_SROM_OFF 4
144 /* Flag bit offsets in General Use region */
145 #define OTPGU_HWP_OFF 60
146 #define OTPGU_SWP_OFF 61
147 #define OTPGU_CIP_OFF 62
148 #define OTPGU_FUSEP_OFF 63
149 #define OTPGU_CIP_MSK 0x4000
150 #define OTPGU_P_MSK 0xf000
151 #define OTPGU_P_SHIFT (OTPGU_HWP_OFF % 16)
154 #define OTP_SZ_FU_324 ((roundup(324, 8))/8) /* 324 bits */
155 #define OTP_SZ_FU_288 (288/8) /* 288 bits */
156 #define OTP_SZ_FU_216 (216/8) /* 216 bits */
157 #define OTP_SZ_FU_72 (72/8) /* 72 bits */
158 #define OTP_SZ_CHECKSUM (16/8) /* 16 bits */
159 #define OTP4315_SWREG_SZ 178 /* 178 bytes */
160 #define OTP_SZ_FU_144 (144/8) /* 144 bits */
162 static int ipxotp_status(void *oh)
164 otpinfo_t *oi = (otpinfo_t *) oh;
165 return (int)(oi->status);
168 /* Return size in bytes */
169 static int ipxotp_size(void *oh)
171 otpinfo_t *oi = (otpinfo_t *) oh;
172 return (int)oi->wsize * 2;
175 static u16 ipxotp_otpr(void *oh, chipcregs_t *cc, uint wn)
179 oi = (otpinfo_t *) oh;
181 return R_REG(&cc->sromotp[wn]);
184 static u16 ipxotp_read_bit(void *oh, chipcregs_t *cc, uint off)
186 otpinfo_t *oi = (otpinfo_t *) oh;
190 row = off / oi->cols;
191 col = off % oi->cols;
193 otpp = OTPP_START_BUSY |
194 ((OTPPOC_READ << OTPP_OC_SHIFT) & OTPP_OC_MASK) |
195 ((row << OTPP_ROW_SHIFT) & OTPP_ROW_MASK) |
196 ((col << OTPP_COL_SHIFT) & OTPP_COL_MASK);
197 W_REG(&cc->otpprog, otpp);
200 ((st = R_REG(&cc->otpprog)) & OTPP_START_BUSY)
201 && (k < OTPP_TRIES); k++)
203 if (k >= OTPP_TRIES) {
206 if (st & OTPP_READERR) {
209 st = (st & OTPP_VALUE_MASK) >> OTPP_VALUE_SHIFT;
214 /* Calculate max HW/SW region byte size by subtracting fuse region and checksum size,
215 * osizew is oi->wsize (OTP size - GU size) in words
217 static int ipxotp_max_rgnsz(si_t *sih, int osizew)
222 case BCM43224_CHIP_ID:
223 case BCM43225_CHIP_ID:
224 ret = osizew * 2 - OTP_SZ_FU_72 - OTP_SZ_CHECKSUM;
226 case BCM4313_CHIP_ID:
227 ret = osizew * 2 - OTP_SZ_FU_72 - OTP_SZ_CHECKSUM;
230 break; /* Don't know about this chip */
236 static void _ipxotp_init(otpinfo_t *oi, chipcregs_t *cc)
241 /* record word offset of General Use Region for various chipcommon revs */
242 if (oi->sih->ccrev == 21 || oi->sih->ccrev == 24
243 || oi->sih->ccrev == 27) {
244 oi->otpgu_base = REVA4_OTPGU_BASE;
245 } else if (oi->sih->ccrev == 36) {
246 /* OTP size greater than equal to 2KB (128 words), otpgu_base is similar to rev23 */
247 if (oi->wsize >= 128)
248 oi->otpgu_base = REVB8_OTPGU_BASE;
250 oi->otpgu_base = REV36_OTPGU_BASE;
251 } else if (oi->sih->ccrev == 23 || oi->sih->ccrev >= 25) {
252 oi->otpgu_base = REVB8_OTPGU_BASE;
255 /* First issue an init command so the status is up to date */
257 OTPP_START_BUSY | ((OTPPOC_INIT << OTPP_OC_SHIFT) & OTPP_OC_MASK);
259 W_REG(&cc->otpprog, otpp);
261 ((st = R_REG(&cc->otpprog)) & OTPP_START_BUSY)
262 && (k < OTPP_TRIES); k++)
264 if (k >= OTPP_TRIES) {
268 /* Read OTP lock bits and subregion programmed indication bits */
269 oi->status = R_REG(&cc->otpstatus);
271 if ((oi->sih->chip == BCM43224_CHIP_ID)
272 || (oi->sih->chip == BCM43225_CHIP_ID)) {
275 (ipxotp_otpr(oi, cc, oi->otpgu_base + OTPGU_P_OFF) &
278 oi->status |= (p_bits << OTPS_GUP_SHIFT);
282 * h/w region base and fuse region limit are fixed to the top and
283 * the bottom of the general use region. Everything else can be flexible.
285 oi->hwbase = oi->otpgu_base + OTPGU_SROM_OFF;
286 oi->hwlim = oi->wsize;
287 if (oi->status & OTPS_GUP_HW) {
289 ipxotp_otpr(oi, cc, oi->otpgu_base + OTPGU_HSB_OFF) / 16;
290 oi->swbase = oi->hwlim;
292 oi->swbase = oi->hwbase;
294 /* subtract fuse and checksum from beginning */
295 oi->swlim = ipxotp_max_rgnsz(oi->sih, oi->wsize) / 2;
297 if (oi->status & OTPS_GUP_SW) {
299 ipxotp_otpr(oi, cc, oi->otpgu_base + OTPGU_SFB_OFF) / 16;
300 oi->fbase = oi->swlim;
302 oi->fbase = oi->swbase;
304 oi->flim = oi->wsize;
307 static void *ipxotp_init(si_t *sih)
313 /* Make sure we're running IPX OTP */
314 if (!OTPTYPE_IPX(sih->ccrev))
317 /* Make sure OTP is not disabled */
318 if (ai_is_otp_disabled(sih))
321 /* Make sure OTP is powered up */
322 if (!ai_is_otp_powered(sih))
327 /* Check for otp size */
328 switch ((sih->cccaps & CC_CAP_OTPSIZE) >> CC_CAP_OTPSIZE_SHIFT) {
347 case 7: /* 16x64 *//* 1024 bits */
353 /* Don't know the geometry */
357 /* Retrieve OTP region info */
358 idx = ai_coreidx(sih);
359 cc = ai_setcoreidx(sih, SI_CC_IDX);
361 _ipxotp_init(oi, cc);
363 ai_setcoreidx(sih, idx);
368 static int ipxotp_read_region(void *oh, int region, u16 *data, uint *wlen)
370 otpinfo_t *oi = (otpinfo_t *) oh;
375 /* Validate region selection */
378 sz = (uint) oi->hwlim - oi->hwbase;
379 if (!(oi->status & OTPS_GUP_HW)) {
390 sz = ((uint) oi->swlim - oi->swbase);
391 if (!(oi->status & OTPS_GUP_SW)) {
403 if (!(oi->status & OTPS_GUP_CI)) {
411 base = oi->otpgu_base + OTPGU_CI_OFF;
414 sz = (uint) oi->flim - oi->fbase;
415 if (!(oi->status & OTPS_GUP_FUSE)) {
426 sz = ((uint) oi->flim - oi->hwbase);
427 if (!(oi->status & (OTPS_GUP_HW | OTPS_GUP_SW))) {
441 idx = ai_coreidx(oi->sih);
442 cc = ai_setcoreidx(oi->sih, SI_CC_IDX);
445 for (i = 0; i < sz; i++)
446 data[i] = ipxotp_otpr(oh, cc, base + i);
448 ai_setcoreidx(oi->sih, idx);
453 static int ipxotp_nvread(void *oh, char *data, uint *len)
458 static otp_fn_t ipxotp_fn = {
459 (otp_size_t) ipxotp_size,
460 (otp_read_bit_t) ipxotp_read_bit,
462 (otp_init_t) ipxotp_init,
463 (otp_read_region_t) ipxotp_read_region,
464 (otp_nvread_t) ipxotp_nvread,
466 (otp_status_t) ipxotp_status
469 #endif /* BCMIPXOTP */
474 * Exported functions:
479 * hndotp_read_region()
486 /* Fields in otpstatus */
487 #define OTPS_PROGFAIL 0x80000000
488 #define OTPS_PROTECT 0x00000007
489 #define OTPS_HW_PROTECT 0x00000001
490 #define OTPS_SW_PROTECT 0x00000002
491 #define OTPS_CID_PROTECT 0x00000004
492 #define OTPS_RCEV_MSK 0x00003f00
493 #define OTPS_RCEV_SHIFT 8
495 /* Fields in the otpcontrol register */
496 #define OTPC_RECWAIT 0xff000000
497 #define OTPC_PROGWAIT 0x00ffff00
498 #define OTPC_PRW_SHIFT 8
499 #define OTPC_MAXFAIL 0x00000038
500 #define OTPC_VSEL 0x00000006
501 #define OTPC_SELVL 0x00000001
503 /* OTP regions (Word offsets from otp size) */
504 #define OTP_SWLIM_OFF (-4)
505 #define OTP_CIDBASE_OFF 0
506 #define OTP_CIDLIM_OFF 4
508 /* Predefined OTP words (Word offset from otp size) */
509 #define OTP_BOUNDARY_OFF (-4)
510 #define OTP_HWSIGN_OFF (-3)
511 #define OTP_SWSIGN_OFF (-2)
512 #define OTP_CIDSIGN_OFF (-1)
513 #define OTP_CID_OFF 0
514 #define OTP_PKG_OFF 1
515 #define OTP_FID_OFF 2
516 #define OTP_RSV_OFF 3
517 #define OTP_LIM_OFF 4
518 #define OTP_RD_OFF 4 /* Redundancy row starts here */
519 #define OTP_RC0_OFF 28 /* Redundancy control word 1 */
520 #define OTP_RC1_OFF 32 /* Redundancy control word 2 */
521 #define OTP_RC_LIM_OFF 36 /* Redundancy control word end */
523 #define OTP_HW_REGION OTPS_HW_PROTECT
524 #define OTP_SW_REGION OTPS_SW_PROTECT
525 #define OTP_CID_REGION OTPS_CID_PROTECT
527 #if OTP_HW_REGION != OTP_HW_RGN
528 #error "incompatible OTP_HW_RGN"
530 #if OTP_SW_REGION != OTP_SW_RGN
531 #error "incompatible OTP_SW_RGN"
533 #if OTP_CID_REGION != OTP_CI_RGN
534 #error "incompatible OTP_CI_RGN"
537 /* Redundancy entry definitions */
538 #define OTP_RCE_ROW_SZ 6
539 #define OTP_RCE_SIGN_MASK 0x7fff
540 #define OTP_RCE_ROW_MASK 0x3f
541 #define OTP_RCE_BITS 21
542 #define OTP_RCE_SIGN_SZ 15
543 #define OTP_RCE_BIT0 1
546 #define OTP_SIGNATURE 0x578a
547 #define OTP_MAGIC 0x4e56
549 static int hndotp_status(void *oh)
551 otpinfo_t *oi = (otpinfo_t *) oh;
552 return (int)(oi->hwprot | oi->signvalid);
555 static int hndotp_size(void *oh)
557 otpinfo_t *oi = (otpinfo_t *) oh;
558 return (int)(oi->size);
561 static u16 hndotp_otpr(void *oh, chipcregs_t *cc, uint wn)
565 ptr = (volatile u16 *)((volatile char *)cc + CC_SROM_OTP);
566 return R_REG(&ptr[wn]);
569 static u16 hndotp_otproff(void *oh, chipcregs_t *cc, int woff)
571 otpinfo_t *oi = (otpinfo_t *) oh;
574 ptr = (volatile u16 *)((volatile char *)cc + CC_SROM_OTP);
576 return R_REG(&ptr[(oi->size / 2) + woff]);
579 static u16 hndotp_read_bit(void *oh, chipcregs_t *cc, uint idx)
587 otpp = OTPP_START_BUSY | OTPP_READ |
588 ((row << OTPP_ROW_SHIFT) & OTPP_ROW_MASK) | (col & OTPP_COL_MASK);
590 W_REG(&cc->otpprog, otpp);
591 st = R_REG(&cc->otpprog);
593 ((st & OTPP_START_BUSY) == OTPP_START_BUSY) && (k < OTPP_TRIES);
595 st = R_REG(&cc->otpprog);
597 if (k >= OTPP_TRIES) {
600 if (st & OTPP_READERR) {
603 st = (st & OTPP_VALUE_MASK) >> OTPP_VALUE_SHIFT;
607 static void *hndotp_init(si_t *sih)
612 u32 cap = 0, clkdiv, otpdiv = 0;
617 idx = ai_coreidx(sih);
620 cc = ai_setcoreidx(sih, SI_CC_IDX);
622 cap = R_REG(&cc->capabilities);
623 if ((cap & CC_CAP_OTPSIZE) == 0) {
628 if (!((oi->ccrev == 12) || (oi->ccrev == 17)
629 || (oi->ccrev == 22)))
632 /* Read the OTP byte size. chipcommon rev >= 18 has RCE so the size is
633 * 8 row (64 bytes) smaller
636 1 << (((cap & CC_CAP_OTPSIZE) >> CC_CAP_OTPSIZE_SHIFT)
637 + CC_CAP_OTPSIZE_BASE);
639 oi->size -= ((OTP_RC0_OFF - OTP_BOUNDARY_OFF) * 2);
641 oi->hwprot = (int)(R_REG(&cc->otpstatus) & OTPS_PROTECT);
644 /* Check the region signature */
645 if (hndotp_otproff(oi, cc, OTP_HWSIGN_OFF) == OTP_SIGNATURE) {
646 oi->signvalid |= OTP_HW_REGION;
647 oi->boundary = hndotp_otproff(oi, cc, OTP_BOUNDARY_OFF);
650 if (hndotp_otproff(oi, cc, OTP_SWSIGN_OFF) == OTP_SIGNATURE)
651 oi->signvalid |= OTP_SW_REGION;
653 if (hndotp_otproff(oi, cc, OTP_CIDSIGN_OFF) == OTP_SIGNATURE)
654 oi->signvalid |= OTP_CID_REGION;
656 /* Set OTP clkdiv for stability */
661 clkdiv = R_REG(&cc->clkdiv);
663 (clkdiv & ~CLKD_OTP) | (otpdiv << CLKD_OTP_SHIFT);
664 W_REG(&cc->clkdiv, clkdiv);
672 ai_setcoreidx(sih, idx);
677 static int hndotp_read_region(void *oh, int region, u16 *data, uint *wlen)
679 otpinfo_t *oi = (otpinfo_t *) oh;
685 if (region != OTP_HW_REGION) {
687 * Only support HW region
688 * (no active chips use HND OTP SW region)
694 st = oi->hwprot | oi->signvalid;
695 if ((st & region) == 0)
699 ((int)*wlen < oi->boundary / 2) ? *wlen : (uint) oi->boundary / 2;
701 idx = ai_coreidx(oi->sih);
702 cc = ai_setcoreidx(oi->sih, SI_CC_IDX);
704 for (i = 0; i < (int)*wlen; i++)
705 data[i] = hndotp_otpr(oh, cc, i);
707 ai_setcoreidx(oi->sih, idx);
712 static int hndotp_nvread(void *oh, char *data, uint *len)
715 otpinfo_t *oi = (otpinfo_t *) oh;
716 u32 base, bound, lim = 0, st;
717 int i, chunk, gchunks, tsz = 0;
723 /* save the orig core */
724 idx = ai_coreidx(oi->sih);
725 cc = ai_setcoreidx(oi->sih, SI_CC_IDX);
727 st = hndotp_status(oh);
728 if (!(st & (OTP_HW_REGION | OTP_SW_REGION))) {
733 /* Read the whole otp so we can easily manipulate it */
734 lim = hndotp_size(oh);
735 rawotp = kmalloc(lim, GFP_ATOMIC);
736 if (rawotp == NULL) {
740 for (i = 0; i < (int)(lim / 2); i++)
741 rawotp[i] = hndotp_otpr(oh, cc, i);
743 if ((st & OTP_HW_REGION) == 0) {
744 /* This could be a programming failure in the first
745 * chunk followed by one or more good chunks
747 for (i = 0; i < (int)(lim / 2); i++)
748 if (rawotp[i] == OTP_MAGIC)
751 if (i < (int)(lim / 2)) {
753 bound = (i * 2) + rawotp[i + 1];
759 bound = rawotp[(lim / 2) + OTP_BOUNDARY_OFF];
761 /* There are two cases: 1) The whole otp is used as nvram
762 * and 2) There is a hardware header followed by nvram.
764 if (rawotp[0] == OTP_MAGIC) {
770 /* Find and copy the data */
776 while ((i < (int)(lim / 2)) && (rawotp[i] == OTP_MAGIC)) {
777 int dsz, rsz = rawotp[i + 1];
779 if (((i * 2) + rsz) >= (int)lim) {
780 /* Bad length, try to find another chunk anyway */
783 if (crc_ccitt(CRC16_INIT_VALUE, (u8 *) &rawotp[i], rsz) ==
785 /* Good crc, copy the vars */
789 if (offset + dsz >= *len) {
792 memcpy(&data[offset], &rawotp[i + 2], dsz);
794 /* Remove extra null characters at the end */
796 data[offset - 1] == 0 && data[offset - 2] == 0)
800 /* bad length or crc didn't check, try to find the next set */
801 if (rawotp[i + (rsz / 2)] == OTP_MAGIC) {
802 /* Assume length is good */
805 while (++i < (int)(lim / 2))
806 if (rawotp[i] == OTP_MAGIC)
817 ai_setcoreidx(oi->sih, idx);
822 static otp_fn_t hndotp_fn = {
823 (otp_size_t) hndotp_size,
824 (otp_read_bit_t) hndotp_read_bit,
826 (otp_init_t) hndotp_init,
827 (otp_read_region_t) hndotp_read_region,
828 (otp_nvread_t) hndotp_nvread,
830 (otp_status_t) hndotp_status
833 #endif /* BCMHNDOTP */
836 * Common Code: Compiled for IPX / HND / AUTO
845 int otp_status(void *oh)
847 otpinfo_t *oi = (otpinfo_t *) oh;
849 return oi->fn->status(oh);
852 int otp_size(void *oh)
854 otpinfo_t *oi = (otpinfo_t *) oh;
856 return oi->fn->size(oh);
859 u16 otp_read_bit(void *oh, uint offset)
861 otpinfo_t *oi = (otpinfo_t *) oh;
862 uint idx = ai_coreidx(oi->sih);
863 chipcregs_t *cc = ai_setcoreidx(oi->sih, SI_CC_IDX);
864 u16 readBit = (u16) oi->fn->read_bit(oh, cc, offset);
865 ai_setcoreidx(oi->sih, idx);
869 void *otp_init(si_t *sih)
875 memset(oi, 0, sizeof(otpinfo_t));
877 oi->ccrev = sih->ccrev;
880 if (OTPTYPE_IPX(oi->ccrev))
885 if (OTPTYPE_HND(oi->ccrev))
889 if (oi->fn == NULL) {
895 ret = (oi->fn->init) (sih);
901 otp_read_region(si_t *sih, int region, u16 *data,
907 wasup = ai_is_otp_powered(sih);
909 ai_otp_power(sih, true);
911 if (!ai_is_otp_powered(sih) || ai_is_otp_disabled(sih)) {
922 err = (((otpinfo_t *) oh)->fn->read_region) (oh, region, data, wlen);
926 ai_otp_power(sih, false);
931 int otp_nvread(void *oh, char *data, uint *len)
933 otpinfo_t *oi = (otpinfo_t *) oh;
935 return oi->fn->nvread(oh, data, len);