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>
26 #include "wlc_types.h"
27 #include <brcmu_utils.h>
30 #include <chipcommon.h>
33 #define OTPS_GUP_MASK 0x00000f00
34 #define OTPS_GUP_SHIFT 8
35 #define OTPS_GUP_HW 0x00000100 /* h/w subregion is programmed */
36 #define OTPS_GUP_SW 0x00000200 /* s/w subregion is programmed */
37 #define OTPS_GUP_CI 0x00000400 /* chipid/pkgopt subregion is programmed */
38 #define OTPS_GUP_FUSE 0x00000800 /* fuse subregion is programmed */
40 /* Fields in otpprog in rev >= 21 */
41 #define OTPP_COL_MASK 0x000000ff
42 #define OTPP_COL_SHIFT 0
43 #define OTPP_ROW_MASK 0x0000ff00
44 #define OTPP_ROW_SHIFT 8
45 #define OTPP_OC_MASK 0x0f000000
46 #define OTPP_OC_SHIFT 24
47 #define OTPP_READERR 0x10000000
48 #define OTPP_VALUE_MASK 0x20000000
49 #define OTPP_VALUE_SHIFT 29
50 #define OTPP_START_BUSY 0x80000000
51 #define OTPP_READ 0x40000000
53 /* Opcodes for OTPP_OC field */
55 #define OTPPOC_BIT_PROG 1
56 #define OTPPOC_VERIFY 3
59 #define OTPPOC_RESET 6
61 #define OTPPOC_ROW_LOCK 8
62 #define OTPPOC_PRESCN_TEST 9
64 #define OTPTYPE_IPX(ccrev) ((ccrev) == 21 || (ccrev) >= 23)
66 #define OTPP_TRIES 10000000 /* # of tries for OTPP */
68 #define MAXNUMRDES 9 /* Maximum OTP redundancy entries */
70 /* OTP common function type */
71 typedef int (*otp_status_t) (void *oh);
72 typedef int (*otp_size_t) (void *oh);
73 typedef void *(*otp_init_t) (struct si_pub *sih);
74 typedef u16(*otp_read_bit_t) (void *oh, chipcregs_t *cc, uint off);
75 typedef int (*otp_read_region_t) (struct si_pub *sih, int region, u16 *data,
77 typedef int (*otp_nvread_t) (void *oh, char *data, uint *len);
79 /* OTP function struct */
80 typedef struct otp_fn_s {
82 otp_read_bit_t read_bit;
84 otp_read_region_t read_region;
90 uint ccrev; /* chipc revision */
91 otp_fn_t *fn; /* OTP functions */
92 struct si_pub *sih; /* Saved sb handle */
95 u16 wsize; /* Size of otp in words */
96 u16 rows; /* Geometry */
97 u16 cols; /* Geometry */
98 u32 status; /* Flag bits (lock/prog/rv).
99 * (Reflected only when OTP is power cycled)
101 u16 hwbase; /* hardware subregion offset */
102 u16 hwlim; /* hardware subregion boundary */
103 u16 swbase; /* software subregion offset */
104 u16 swlim; /* software subregion boundary */
105 u16 fbase; /* fuse subregion offset */
106 u16 flim; /* fuse subregion boundary */
107 int otpgu_base; /* offset to General Use Region */
110 static otpinfo_t otpinfo;
115 * Exported functions:
120 * 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(struct si_pub *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(struct si_pub *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
478 int otp_status(void *oh)
480 otpinfo_t *oi = (otpinfo_t *) oh;
482 return oi->fn->status(oh);
485 int otp_size(void *oh)
487 otpinfo_t *oi = (otpinfo_t *) oh;
489 return oi->fn->size(oh);
492 u16 otp_read_bit(void *oh, uint offset)
494 otpinfo_t *oi = (otpinfo_t *) oh;
495 uint idx = ai_coreidx(oi->sih);
496 chipcregs_t *cc = ai_setcoreidx(oi->sih, SI_CC_IDX);
497 u16 readBit = (u16) oi->fn->read_bit(oh, cc, offset);
498 ai_setcoreidx(oi->sih, idx);
502 void *otp_init(struct si_pub *sih)
508 memset(oi, 0, sizeof(otpinfo_t));
510 oi->ccrev = sih->ccrev;
512 if (OTPTYPE_IPX(oi->ccrev))
515 if (oi->fn == NULL) {
521 ret = (oi->fn->init) (sih);
527 otp_read_region(struct si_pub *sih, int region, u16 *data,
533 wasup = ai_is_otp_powered(sih);
535 ai_otp_power(sih, true);
537 if (!ai_is_otp_powered(sih) || ai_is_otp_disabled(sih)) {
548 err = (((otpinfo_t *) oh)->fn->read_region) (oh, region, data, wlen);
552 ai_otp_power(sih, false);
557 int otp_nvread(void *oh, char *data, uint *len)
559 otpinfo_t *oi = (otpinfo_t *) oh;
561 return oi->fn->nvread(oh, data, len);