1 /*****************************************************************************
2 * Copyright 2003 - 2009 Broadcom Corporation. All rights reserved.
4 * Unless you and Broadcom execute a separate written software license
5 * agreement governing use of this software, this software is licensed to you
6 * under the terms of the GNU General Public License version 2, available at
7 * http://www.broadcom.com/licenses/GPLv2.php (the "GPL").
9 * Notwithstanding the above, under no circumstances may you combine this
10 * software in any way with any other Broadcom software provided under a
11 * license other than the GPL, without Broadcom's express prior written
13 *****************************************************************************/
14 #ifndef NAND_BCM_UMI_H
15 #define NAND_BCM_UMI_H
17 /* ---- Include Files ---------------------------------------------------- */
18 #include <mach/reg_umi.h>
19 #include <mach/reg_nand.h>
20 #include <mach/cfg_global.h>
22 /* ---- Constants and Types ---------------------------------------------- */
23 #if (CFG_GLOBAL_CHIP_FAMILY == CFG_GLOBAL_CHIP_FAMILY_BCMRING)
24 #define NAND_ECC_BCH (CFG_GLOBAL_CHIP_REV > 0xA0)
26 #define NAND_ECC_BCH 0
29 #define CFG_GLOBAL_NAND_ECC_BCH_NUM_BYTES 13
33 #define NAND_ECC_NUM_BYTES 13
35 #define NAND_ECC_NUM_BYTES CFG_GLOBAL_NAND_ECC_BCH_NUM_BYTES
38 #define NAND_ECC_NUM_BYTES 3
41 #define NAND_DATA_ACCESS_SIZE 512
43 /* ---- Variable Externs ------------------------------------------ */
44 /* ---- Function Prototypes --------------------------------------- */
45 int nand_bcm_umi_bch_correct_page(uint8_t *datap, uint8_t *readEccData,
48 /* Check in device is ready */
49 static inline int nand_bcm_umi_dev_ready(void)
51 return readl(®_UMI_NAND_RCSR) & REG_UMI_NAND_RCSR_RDY;
54 /* Wait until device is ready */
55 static inline void nand_bcm_umi_wait_till_ready(void)
57 while (nand_bcm_umi_dev_ready() == 0)
61 /* Enable Hamming ECC */
62 static inline void nand_bcm_umi_hamming_enable_hwecc(void)
64 /* disable and reset ECC, 512 byte page */
65 writel(readl(®_UMI_NAND_ECC_CSR) & ~(REG_UMI_NAND_ECC_CSR_ECC_ENABLE |
66 REG_UMI_NAND_ECC_CSR_256BYTE), ®_UMI_NAND_ECC_CSR);
68 writel(readl(®_UMI_NAND_ECC_CSR) | REG_UMI_NAND_ECC_CSR_ECC_ENABLE,
69 ®_UMI_NAND_ECC_CSR);
73 /* BCH ECC specifics */
74 #define ECC_BITS_PER_CORRECTABLE_BIT 13
76 /* Enable BCH Read ECC */
77 static inline void nand_bcm_umi_bch_enable_read_hwecc(void)
79 /* disable and reset ECC */
80 writel(REG_UMI_BCH_CTRL_STATUS_RD_ECC_VALID, ®_UMI_BCH_CTRL_STATUS);
82 writel(REG_UMI_BCH_CTRL_STATUS_ECC_RD_EN, ®_UMI_BCH_CTRL_STATUS);
85 /* Enable BCH Write ECC */
86 static inline void nand_bcm_umi_bch_enable_write_hwecc(void)
88 /* disable and reset ECC */
89 writel(REG_UMI_BCH_CTRL_STATUS_WR_ECC_VALID, ®_UMI_BCH_CTRL_STATUS);
91 writel(REG_UMI_BCH_CTRL_STATUS_ECC_WR_EN, ®_UMI_BCH_CTRL_STATUS);
94 /* Config number of BCH ECC bytes */
95 static inline void nand_bcm_umi_bch_config_ecc(uint8_t numEccBytes)
100 uint32_t numBits = numEccBytes * 8;
102 /* disable and reset ECC */
103 writel(REG_UMI_BCH_CTRL_STATUS_WR_ECC_VALID |
104 REG_UMI_BCH_CTRL_STATUS_RD_ECC_VALID,
105 ®_UMI_BCH_CTRL_STATUS);
107 /* Every correctible bit requires 13 ECC bits */
108 tValue = (uint32_t) (numBits / ECC_BITS_PER_CORRECTABLE_BIT);
110 /* Total data in number of bits for generating and computing BCH ECC */
111 nValue = (NAND_DATA_ACCESS_SIZE + numEccBytes) * 8;
113 /* K parameter is used internally. K = N - (T * 13) */
114 kValue = nValue - (tValue * ECC_BITS_PER_CORRECTABLE_BIT);
116 /* Write the settings */
117 writel(nValue, ®_UMI_BCH_N);
118 writel(tValue, ®_UMI_BCH_T);
119 writel(kValue, ®_UMI_BCH_K);
122 /* Pause during ECC read calculation to skip bytes in OOB */
123 static inline void nand_bcm_umi_bch_pause_read_ecc_calc(void)
125 writel(REG_UMI_BCH_CTRL_STATUS_ECC_RD_EN | REG_UMI_BCH_CTRL_STATUS_PAUSE_ECC_DEC, ®_UMI_BCH_CTRL_STATUS);
128 /* Resume during ECC read calculation after skipping bytes in OOB */
129 static inline void nand_bcm_umi_bch_resume_read_ecc_calc(void)
131 writel(REG_UMI_BCH_CTRL_STATUS_ECC_RD_EN, ®_UMI_BCH_CTRL_STATUS);
134 /* Poll read ECC calc to check when hardware completes */
135 static inline uint32_t nand_bcm_umi_bch_poll_read_ecc_calc(void)
140 /* wait for ECC to be valid */
141 regVal = readl(®_UMI_BCH_CTRL_STATUS);
142 } while ((regVal & REG_UMI_BCH_CTRL_STATUS_RD_ECC_VALID) == 0);
147 /* Poll write ECC calc to check when hardware completes */
148 static inline void nand_bcm_umi_bch_poll_write_ecc_calc(void)
150 /* wait for ECC to be valid */
151 while ((readl(®_UMI_BCH_CTRL_STATUS) & REG_UMI_BCH_CTRL_STATUS_WR_ECC_VALID)
156 /* Read the OOB and ECC, for kernel write OOB to a buffer */
157 #if defined(__KERNEL__) && !defined(STANDALONE)
158 static inline void nand_bcm_umi_bch_read_oobEcc(uint32_t pageSize,
159 uint8_t *eccCalc, int numEccBytes, uint8_t *oobp)
161 static inline void nand_bcm_umi_bch_read_oobEcc(uint32_t pageSize,
162 uint8_t *eccCalc, int numEccBytes)
166 int numToRead = 16; /* There are 16 bytes per sector in the OOB */
168 /* ECC is already paused when this function is called */
169 if (pageSize != NAND_DATA_ACCESS_SIZE) {
171 #if defined(__KERNEL__) && !defined(STANDALONE)
172 *oobp++ = readb(®_NAND_DATA8);
174 readb(®_NAND_DATA8);
179 while (numToRead > numEccBytes) {
180 /* skip free oob region */
181 #if defined(__KERNEL__) && !defined(STANDALONE)
182 *oobp++ = readb(®_NAND_DATA8);
184 readb(®_NAND_DATA8);
189 if (pageSize == NAND_DATA_ACCESS_SIZE) {
190 /* read ECC bytes before BI */
191 nand_bcm_umi_bch_resume_read_ecc_calc();
193 while (numToRead > 11) {
194 #if defined(__KERNEL__) && !defined(STANDALONE)
195 *oobp = readb(®_NAND_DATA8);
196 eccCalc[eccPos++] = *oobp;
199 eccCalc[eccPos++] = readb(®_NAND_DATA8);
204 nand_bcm_umi_bch_pause_read_ecc_calc();
206 if (numToRead == 11) {
208 #if defined(__KERNEL__) && !defined(STANDALONE)
209 *oobp++ = readb(®_NAND_DATA8);
211 readb(®_NAND_DATA8);
218 nand_bcm_umi_bch_resume_read_ecc_calc();
220 #if defined(__KERNEL__) && !defined(STANDALONE)
221 *oobp = readb(®_NAND_DATA8);
222 eccCalc[eccPos++] = *oobp;
225 eccCalc[eccPos++] = readb(®_NAND_DATA8);
231 /* Helper function to write ECC */
232 static inline void NAND_BCM_UMI_ECC_WRITE(int numEccBytes, int eccBytePos,
233 uint8_t *oobp, uint8_t eccVal)
235 if (eccBytePos <= numEccBytes)
239 /* Write OOB with ECC */
240 static inline void nand_bcm_umi_bch_write_oobEcc(uint32_t pageSize,
241 uint8_t *oobp, int numEccBytes)
243 uint32_t eccVal = 0xffffffff;
245 /* wait for write ECC to be valid */
246 nand_bcm_umi_bch_poll_write_ecc_calc();
249 ** Get the hardware ecc from the 32-bit result registers.
250 ** Read after 512 byte accesses. Format B3B2B1B0
251 ** where B3 = ecc3, etc.
254 if (pageSize == NAND_DATA_ACCESS_SIZE) {
255 /* Now fill in the ECC bytes */
256 if (numEccBytes >= 13)
257 eccVal = readl(®_UMI_BCH_WR_ECC_3);
259 /* Usually we skip CM in oob[0,1] */
260 NAND_BCM_UMI_ECC_WRITE(numEccBytes, 15, &oobp[0],
261 (eccVal >> 16) & 0xff);
262 NAND_BCM_UMI_ECC_WRITE(numEccBytes, 14, &oobp[1],
263 (eccVal >> 8) & 0xff);
265 /* Write ECC in oob[2,3,4] */
266 NAND_BCM_UMI_ECC_WRITE(numEccBytes, 13, &oobp[2],
267 eccVal & 0xff); /* ECC 12 */
269 if (numEccBytes >= 9)
270 eccVal = readl(®_UMI_BCH_WR_ECC_2);
272 NAND_BCM_UMI_ECC_WRITE(numEccBytes, 12, &oobp[3],
273 (eccVal >> 24) & 0xff); /* ECC11 */
274 NAND_BCM_UMI_ECC_WRITE(numEccBytes, 11, &oobp[4],
275 (eccVal >> 16) & 0xff); /* ECC10 */
277 /* Always Skip BI in oob[5] */
279 /* Always Skip BI in oob[0] */
281 /* Now fill in the ECC bytes */
282 if (numEccBytes >= 13)
283 eccVal = readl(®_UMI_BCH_WR_ECC_3);
285 /* Usually skip CM in oob[1,2] */
286 NAND_BCM_UMI_ECC_WRITE(numEccBytes, 15, &oobp[1],
287 (eccVal >> 16) & 0xff);
288 NAND_BCM_UMI_ECC_WRITE(numEccBytes, 14, &oobp[2],
289 (eccVal >> 8) & 0xff);
291 /* Write ECC in oob[3-15] */
292 NAND_BCM_UMI_ECC_WRITE(numEccBytes, 13, &oobp[3],
293 eccVal & 0xff); /* ECC12 */
295 if (numEccBytes >= 9)
296 eccVal = readl(®_UMI_BCH_WR_ECC_2);
298 NAND_BCM_UMI_ECC_WRITE(numEccBytes, 12, &oobp[4],
299 (eccVal >> 24) & 0xff); /* ECC11 */
300 NAND_BCM_UMI_ECC_WRITE(numEccBytes, 11, &oobp[5],
301 (eccVal >> 16) & 0xff); /* ECC10 */
304 /* Fill in the remainder of ECC locations */
305 NAND_BCM_UMI_ECC_WRITE(numEccBytes, 10, &oobp[6],
306 (eccVal >> 8) & 0xff); /* ECC9 */
307 NAND_BCM_UMI_ECC_WRITE(numEccBytes, 9, &oobp[7],
308 eccVal & 0xff); /* ECC8 */
310 if (numEccBytes >= 5)
311 eccVal = readl(®_UMI_BCH_WR_ECC_1);
313 NAND_BCM_UMI_ECC_WRITE(numEccBytes, 8, &oobp[8],
314 (eccVal >> 24) & 0xff); /* ECC7 */
315 NAND_BCM_UMI_ECC_WRITE(numEccBytes, 7, &oobp[9],
316 (eccVal >> 16) & 0xff); /* ECC6 */
317 NAND_BCM_UMI_ECC_WRITE(numEccBytes, 6, &oobp[10],
318 (eccVal >> 8) & 0xff); /* ECC5 */
319 NAND_BCM_UMI_ECC_WRITE(numEccBytes, 5, &oobp[11],
320 eccVal & 0xff); /* ECC4 */
322 if (numEccBytes >= 1)
323 eccVal = readl(®_UMI_BCH_WR_ECC_0);
325 NAND_BCM_UMI_ECC_WRITE(numEccBytes, 4, &oobp[12],
326 (eccVal >> 24) & 0xff); /* ECC3 */
327 NAND_BCM_UMI_ECC_WRITE(numEccBytes, 3, &oobp[13],
328 (eccVal >> 16) & 0xff); /* ECC2 */
329 NAND_BCM_UMI_ECC_WRITE(numEccBytes, 2, &oobp[14],
330 (eccVal >> 8) & 0xff); /* ECC1 */
331 NAND_BCM_UMI_ECC_WRITE(numEccBytes, 1, &oobp[15],
332 eccVal & 0xff); /* ECC0 */
336 #endif /* NAND_BCM_UMI_H */