otp.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. /*
  2. * Copyright (c) 2010 Broadcom Corporation
  3. *
  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.
  7. *
  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.
  15. */
  16. #include <linux/io.h>
  17. #include <linux/errno.h>
  18. #include <linux/string.h>
  19. #include <brcm_hw_ids.h>
  20. #include <chipcommon.h>
  21. #include "aiutils.h"
  22. #include "otp.h"
  23. #define OTPS_GUP_MASK 0x00000f00
  24. #define OTPS_GUP_SHIFT 8
  25. /* h/w subregion is programmed */
  26. #define OTPS_GUP_HW 0x00000100
  27. /* s/w subregion is programmed */
  28. #define OTPS_GUP_SW 0x00000200
  29. /* chipid/pkgopt subregion is programmed */
  30. #define OTPS_GUP_CI 0x00000400
  31. /* fuse subregion is programmed */
  32. #define OTPS_GUP_FUSE 0x00000800
  33. /* Fields in otpprog in rev >= 21 */
  34. #define OTPP_COL_MASK 0x000000ff
  35. #define OTPP_COL_SHIFT 0
  36. #define OTPP_ROW_MASK 0x0000ff00
  37. #define OTPP_ROW_SHIFT 8
  38. #define OTPP_OC_MASK 0x0f000000
  39. #define OTPP_OC_SHIFT 24
  40. #define OTPP_READERR 0x10000000
  41. #define OTPP_VALUE_MASK 0x20000000
  42. #define OTPP_VALUE_SHIFT 29
  43. #define OTPP_START_BUSY 0x80000000
  44. #define OTPP_READ 0x40000000
  45. /* Opcodes for OTPP_OC field */
  46. #define OTPPOC_READ 0
  47. #define OTPPOC_BIT_PROG 1
  48. #define OTPPOC_VERIFY 3
  49. #define OTPPOC_INIT 4
  50. #define OTPPOC_SET 5
  51. #define OTPPOC_RESET 6
  52. #define OTPPOC_OCST 7
  53. #define OTPPOC_ROW_LOCK 8
  54. #define OTPPOC_PRESCN_TEST 9
  55. #define OTPTYPE_IPX(ccrev) ((ccrev) == 21 || (ccrev) >= 23)
  56. #define OTPP_TRIES 10000000 /* # of tries for OTPP */
  57. #define MAXNUMRDES 9 /* Maximum OTP redundancy entries */
  58. /* Fixed size subregions sizes in words */
  59. #define OTPGU_CI_SZ 2
  60. struct otpinfo;
  61. /* OTP function struct */
  62. struct otp_fn_s {
  63. int (*init)(struct si_pub *sih, struct otpinfo *oi);
  64. int (*read_region)(struct otpinfo *oi, int region, u16 *data,
  65. uint *wlen);
  66. };
  67. struct otpinfo {
  68. struct bcma_device *core; /* chipc core */
  69. const struct otp_fn_s *fn; /* OTP functions */
  70. struct si_pub *sih; /* Saved sb handle */
  71. /* IPX OTP section */
  72. u16 wsize; /* Size of otp in words */
  73. u16 rows; /* Geometry */
  74. u16 cols; /* Geometry */
  75. u32 status; /* Flag bits (lock/prog/rv).
  76. * (Reflected only when OTP is power cycled)
  77. */
  78. u16 hwbase; /* hardware subregion offset */
  79. u16 hwlim; /* hardware subregion boundary */
  80. u16 swbase; /* software subregion offset */
  81. u16 swlim; /* software subregion boundary */
  82. u16 fbase; /* fuse subregion offset */
  83. u16 flim; /* fuse subregion boundary */
  84. int otpgu_base; /* offset to General Use Region */
  85. };
  86. /* OTP layout */
  87. /* CC revs 21, 24 and 27 OTP General Use Region word offset */
  88. #define REVA4_OTPGU_BASE 12
  89. /* CC revs 23, 25, 26, 28 and above OTP General Use Region word offset */
  90. #define REVB8_OTPGU_BASE 20
  91. /* CC rev 36 OTP General Use Region word offset */
  92. #define REV36_OTPGU_BASE 12
  93. /* Subregion word offsets in General Use region */
  94. #define OTPGU_HSB_OFF 0
  95. #define OTPGU_SFB_OFF 1
  96. #define OTPGU_CI_OFF 2
  97. #define OTPGU_P_OFF 3
  98. #define OTPGU_SROM_OFF 4
  99. /* Flag bit offsets in General Use region */
  100. #define OTPGU_HWP_OFF 60
  101. #define OTPGU_SWP_OFF 61
  102. #define OTPGU_CIP_OFF 62
  103. #define OTPGU_FUSEP_OFF 63
  104. #define OTPGU_CIP_MSK 0x4000
  105. #define OTPGU_P_MSK 0xf000
  106. #define OTPGU_P_SHIFT (OTPGU_HWP_OFF % 16)
  107. /* OTP Size */
  108. #define OTP_SZ_FU_324 ((roundup(324, 8))/8) /* 324 bits */
  109. #define OTP_SZ_FU_288 (288/8) /* 288 bits */
  110. #define OTP_SZ_FU_216 (216/8) /* 216 bits */
  111. #define OTP_SZ_FU_72 (72/8) /* 72 bits */
  112. #define OTP_SZ_CHECKSUM (16/8) /* 16 bits */
  113. #define OTP4315_SWREG_SZ 178 /* 178 bytes */
  114. #define OTP_SZ_FU_144 (144/8) /* 144 bits */
  115. static u16
  116. ipxotp_otpr(struct otpinfo *oi, uint wn)
  117. {
  118. return bcma_read16(oi->core,
  119. CHIPCREGOFFS(sromotp[wn]));
  120. }
  121. /*
  122. * Calculate max HW/SW region byte size by subtracting fuse region
  123. * and checksum size, osizew is oi->wsize (OTP size - GU size) in words
  124. */
  125. static int ipxotp_max_rgnsz(struct si_pub *sih, int osizew)
  126. {
  127. int ret = 0;
  128. switch (ai_get_chip_id(sih)) {
  129. case BCM43224_CHIP_ID:
  130. case BCM43225_CHIP_ID:
  131. ret = osizew * 2 - OTP_SZ_FU_72 - OTP_SZ_CHECKSUM;
  132. break;
  133. case BCM4313_CHIP_ID:
  134. ret = osizew * 2 - OTP_SZ_FU_72 - OTP_SZ_CHECKSUM;
  135. break;
  136. default:
  137. break; /* Don't know about this chip */
  138. }
  139. return ret;
  140. }
  141. static void _ipxotp_init(struct otpinfo *oi)
  142. {
  143. uint k;
  144. u32 otpp, st;
  145. int ccrev = ai_get_ccrev(oi->sih);
  146. /*
  147. * record word offset of General Use Region
  148. * for various chipcommon revs
  149. */
  150. if (ccrev == 21 || ccrev == 24
  151. || ccrev == 27) {
  152. oi->otpgu_base = REVA4_OTPGU_BASE;
  153. } else if (ccrev == 36) {
  154. /*
  155. * OTP size greater than equal to 2KB (128 words),
  156. * otpgu_base is similar to rev23
  157. */
  158. if (oi->wsize >= 128)
  159. oi->otpgu_base = REVB8_OTPGU_BASE;
  160. else
  161. oi->otpgu_base = REV36_OTPGU_BASE;
  162. } else if (ccrev == 23 || ccrev >= 25) {
  163. oi->otpgu_base = REVB8_OTPGU_BASE;
  164. }
  165. /* First issue an init command so the status is up to date */
  166. otpp =
  167. OTPP_START_BUSY | ((OTPPOC_INIT << OTPP_OC_SHIFT) & OTPP_OC_MASK);
  168. bcma_write32(oi->core, CHIPCREGOFFS(otpprog), otpp);
  169. st = bcma_read32(oi->core, CHIPCREGOFFS(otpprog));
  170. for (k = 0; (st & OTPP_START_BUSY) && (k < OTPP_TRIES); k++)
  171. st = bcma_read32(oi->core, CHIPCREGOFFS(otpprog));
  172. if (k >= OTPP_TRIES)
  173. return;
  174. /* Read OTP lock bits and subregion programmed indication bits */
  175. oi->status = bcma_read32(oi->core, CHIPCREGOFFS(otpstatus));
  176. if ((ai_get_chip_id(oi->sih) == BCM43224_CHIP_ID)
  177. || (ai_get_chip_id(oi->sih) == BCM43225_CHIP_ID)) {
  178. u32 p_bits;
  179. p_bits = (ipxotp_otpr(oi, oi->otpgu_base + OTPGU_P_OFF) &
  180. OTPGU_P_MSK) >> OTPGU_P_SHIFT;
  181. oi->status |= (p_bits << OTPS_GUP_SHIFT);
  182. }
  183. /*
  184. * h/w region base and fuse region limit are fixed to
  185. * the top and the bottom of the general use region.
  186. * Everything else can be flexible.
  187. */
  188. oi->hwbase = oi->otpgu_base + OTPGU_SROM_OFF;
  189. oi->hwlim = oi->wsize;
  190. if (oi->status & OTPS_GUP_HW) {
  191. oi->hwlim =
  192. ipxotp_otpr(oi, oi->otpgu_base + OTPGU_HSB_OFF) / 16;
  193. oi->swbase = oi->hwlim;
  194. } else
  195. oi->swbase = oi->hwbase;
  196. /* subtract fuse and checksum from beginning */
  197. oi->swlim = ipxotp_max_rgnsz(oi->sih, oi->wsize) / 2;
  198. if (oi->status & OTPS_GUP_SW) {
  199. oi->swlim =
  200. ipxotp_otpr(oi, oi->otpgu_base + OTPGU_SFB_OFF) / 16;
  201. oi->fbase = oi->swlim;
  202. } else
  203. oi->fbase = oi->swbase;
  204. oi->flim = oi->wsize;
  205. }
  206. static int ipxotp_init(struct si_pub *sih, struct otpinfo *oi)
  207. {
  208. /* Make sure we're running IPX OTP */
  209. if (!OTPTYPE_IPX(ai_get_ccrev(sih)))
  210. return -EBADE;
  211. /* Make sure OTP is not disabled */
  212. if (ai_is_otp_disabled(sih))
  213. return -EBADE;
  214. /* Check for otp size */
  215. switch ((ai_get_cccaps(sih) & CC_CAP_OTPSIZE) >> CC_CAP_OTPSIZE_SHIFT) {
  216. case 0:
  217. /* Nothing there */
  218. return -EBADE;
  219. case 1: /* 32x64 */
  220. oi->rows = 32;
  221. oi->cols = 64;
  222. oi->wsize = 128;
  223. break;
  224. case 2: /* 64x64 */
  225. oi->rows = 64;
  226. oi->cols = 64;
  227. oi->wsize = 256;
  228. break;
  229. case 5: /* 96x64 */
  230. oi->rows = 96;
  231. oi->cols = 64;
  232. oi->wsize = 384;
  233. break;
  234. case 7: /* 16x64 *//* 1024 bits */
  235. oi->rows = 16;
  236. oi->cols = 64;
  237. oi->wsize = 64;
  238. break;
  239. default:
  240. /* Don't know the geometry */
  241. return -EBADE;
  242. }
  243. /* Retrieve OTP region info */
  244. _ipxotp_init(oi);
  245. return 0;
  246. }
  247. static int
  248. ipxotp_read_region(struct otpinfo *oi, int region, u16 *data, uint *wlen)
  249. {
  250. uint base, i, sz;
  251. /* Validate region selection */
  252. switch (region) {
  253. case OTP_HW_RGN:
  254. sz = (uint) oi->hwlim - oi->hwbase;
  255. if (!(oi->status & OTPS_GUP_HW)) {
  256. *wlen = sz;
  257. return -ENODATA;
  258. }
  259. if (*wlen < sz) {
  260. *wlen = sz;
  261. return -EOVERFLOW;
  262. }
  263. base = oi->hwbase;
  264. break;
  265. case OTP_SW_RGN:
  266. sz = ((uint) oi->swlim - oi->swbase);
  267. if (!(oi->status & OTPS_GUP_SW)) {
  268. *wlen = sz;
  269. return -ENODATA;
  270. }
  271. if (*wlen < sz) {
  272. *wlen = sz;
  273. return -EOVERFLOW;
  274. }
  275. base = oi->swbase;
  276. break;
  277. case OTP_CI_RGN:
  278. sz = OTPGU_CI_SZ;
  279. if (!(oi->status & OTPS_GUP_CI)) {
  280. *wlen = sz;
  281. return -ENODATA;
  282. }
  283. if (*wlen < sz) {
  284. *wlen = sz;
  285. return -EOVERFLOW;
  286. }
  287. base = oi->otpgu_base + OTPGU_CI_OFF;
  288. break;
  289. case OTP_FUSE_RGN:
  290. sz = (uint) oi->flim - oi->fbase;
  291. if (!(oi->status & OTPS_GUP_FUSE)) {
  292. *wlen = sz;
  293. return -ENODATA;
  294. }
  295. if (*wlen < sz) {
  296. *wlen = sz;
  297. return -EOVERFLOW;
  298. }
  299. base = oi->fbase;
  300. break;
  301. case OTP_ALL_RGN:
  302. sz = ((uint) oi->flim - oi->hwbase);
  303. if (!(oi->status & (OTPS_GUP_HW | OTPS_GUP_SW))) {
  304. *wlen = sz;
  305. return -ENODATA;
  306. }
  307. if (*wlen < sz) {
  308. *wlen = sz;
  309. return -EOVERFLOW;
  310. }
  311. base = oi->hwbase;
  312. break;
  313. default:
  314. return -EINVAL;
  315. }
  316. /* Read the data */
  317. for (i = 0; i < sz; i++)
  318. data[i] = ipxotp_otpr(oi, base + i);
  319. *wlen = sz;
  320. return 0;
  321. }
  322. static const struct otp_fn_s ipxotp_fn = {
  323. (int (*)(struct si_pub *, struct otpinfo *)) ipxotp_init,
  324. (int (*)(struct otpinfo *, int, u16 *, uint *)) ipxotp_read_region,
  325. };
  326. static int otp_init(struct si_pub *sih, struct otpinfo *oi)
  327. {
  328. int ret;
  329. memset(oi, 0, sizeof(struct otpinfo));
  330. oi->core = ai_findcore(sih, BCMA_CORE_CHIPCOMMON, 0);
  331. if (OTPTYPE_IPX(ai_get_ccrev(sih)))
  332. oi->fn = &ipxotp_fn;
  333. if (oi->fn == NULL)
  334. return -EBADE;
  335. oi->sih = sih;
  336. ret = (oi->fn->init)(sih, oi);
  337. return ret;
  338. }
  339. int
  340. otp_read_region(struct si_pub *sih, int region, u16 *data, uint *wlen) {
  341. struct otpinfo otpinfo;
  342. struct otpinfo *oi = &otpinfo;
  343. int err = 0;
  344. if (ai_is_otp_disabled(sih)) {
  345. err = -EPERM;
  346. goto out;
  347. }
  348. err = otp_init(sih, oi);
  349. if (err)
  350. goto out;
  351. err = ((oi)->fn->read_region)(oi, region, data, wlen);
  352. out:
  353. return err;
  354. }