Scott Wood | e4c0950 | 2008-06-30 14:13:28 -0500 | [diff] [blame] | 1 | /* |
| 2 | * NAND boot for Freescale Enhanced Local Bus Controller, Flash Control Machine |
| 3 | * |
| 4 | * (C) Copyright 2006-2008 |
| 5 | * Stefan Roese, DENX Software Engineering, sr@denx.de. |
| 6 | * |
| 7 | * Copyright (c) 2008 Freescale Semiconductor, Inc. |
| 8 | * Author: Scott Wood <scottwood@freescale.com> |
| 9 | * |
Wolfgang Denk | 1a45966 | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 10 | * SPDX-License-Identifier: GPL-2.0+ |
Scott Wood | e4c0950 | 2008-06-30 14:13:28 -0500 | [diff] [blame] | 11 | */ |
| 12 | |
| 13 | #include <common.h> |
| 14 | #include <asm/io.h> |
Scott Wood | e4c0950 | 2008-06-30 14:13:28 -0500 | [diff] [blame] | 15 | #include <asm/fsl_lbc.h> |
| 16 | #include <linux/mtd/nand.h> |
| 17 | |
| 18 | #define WINDOW_SIZE 8192 |
| 19 | |
| 20 | static void nand_wait(void) |
| 21 | { |
Becky Bruce | f51cdaf | 2010-06-17 11:37:20 -0500 | [diff] [blame] | 22 | fsl_lbc_t *regs = LBC_BASE_ADDR; |
Scott Wood | e4c0950 | 2008-06-30 14:13:28 -0500 | [diff] [blame] | 23 | |
| 24 | for (;;) { |
| 25 | uint32_t status = in_be32(®s->ltesr); |
| 26 | |
| 27 | if (status == 1) |
| 28 | return; |
| 29 | |
| 30 | if (status & 1) { |
| 31 | puts("read failed (ltesr)\n"); |
| 32 | for (;;); |
| 33 | } |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | static void nand_load(unsigned int offs, int uboot_size, uchar *dst) |
| 38 | { |
Becky Bruce | f51cdaf | 2010-06-17 11:37:20 -0500 | [diff] [blame] | 39 | fsl_lbc_t *regs = LBC_BASE_ADDR; |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 40 | uchar *buf = (uchar *)CONFIG_SYS_NAND_BASE; |
Matthew McClintock | 6297454 | 2011-04-05 14:39:34 -0500 | [diff] [blame] | 41 | const int large = CONFIG_SYS_NAND_OR_PRELIM & OR_FCM_PGS; |
| 42 | const int block_shift = large ? 17 : 14; |
| 43 | const int block_size = 1 << block_shift; |
| 44 | const int page_size = large ? 2048 : 512; |
| 45 | const int bad_marker = large ? page_size + 0 : page_size + 5; |
Scott Wood | e4c0950 | 2008-06-30 14:13:28 -0500 | [diff] [blame] | 46 | int fmr = (15 << FMR_CWTO_SHIFT) | (2 << FMR_AL_SHIFT) | 2; |
| 47 | int pos = 0; |
| 48 | |
| 49 | if (offs & (block_size - 1)) { |
| 50 | puts("bad offset\n"); |
| 51 | for (;;); |
| 52 | } |
| 53 | |
| 54 | if (large) { |
| 55 | fmr |= FMR_ECCM; |
Scott Wood | 7b8f668 | 2012-08-08 15:03:33 +0000 | [diff] [blame] | 56 | __raw_writel((NAND_CMD_READ0 << FCR_CMD0_SHIFT) | |
| 57 | (NAND_CMD_READSTART << FCR_CMD1_SHIFT), |
| 58 | ®s->fcr); |
| 59 | __raw_writel( |
| 60 | (FIR_OP_CW0 << FIR_OP0_SHIFT) | |
| 61 | (FIR_OP_CA << FIR_OP1_SHIFT) | |
| 62 | (FIR_OP_PA << FIR_OP2_SHIFT) | |
| 63 | (FIR_OP_CW1 << FIR_OP3_SHIFT) | |
| 64 | (FIR_OP_RBW << FIR_OP4_SHIFT), |
| 65 | ®s->fir); |
Scott Wood | e4c0950 | 2008-06-30 14:13:28 -0500 | [diff] [blame] | 66 | } else { |
Scott Wood | 7b8f668 | 2012-08-08 15:03:33 +0000 | [diff] [blame] | 67 | __raw_writel(NAND_CMD_READ0 << FCR_CMD0_SHIFT, ®s->fcr); |
| 68 | __raw_writel( |
| 69 | (FIR_OP_CW0 << FIR_OP0_SHIFT) | |
| 70 | (FIR_OP_CA << FIR_OP1_SHIFT) | |
| 71 | (FIR_OP_PA << FIR_OP2_SHIFT) | |
| 72 | (FIR_OP_RBW << FIR_OP3_SHIFT), |
| 73 | ®s->fir); |
Scott Wood | e4c0950 | 2008-06-30 14:13:28 -0500 | [diff] [blame] | 74 | } |
| 75 | |
Scott Wood | 7b8f668 | 2012-08-08 15:03:33 +0000 | [diff] [blame] | 76 | __raw_writel(0, ®s->fbcr); |
Scott Wood | e4c0950 | 2008-06-30 14:13:28 -0500 | [diff] [blame] | 77 | |
| 78 | while (pos < uboot_size) { |
| 79 | int i = 0; |
Scott Wood | 7b8f668 | 2012-08-08 15:03:33 +0000 | [diff] [blame] | 80 | __raw_writel(offs >> block_shift, ®s->fbar); |
Scott Wood | e4c0950 | 2008-06-30 14:13:28 -0500 | [diff] [blame] | 81 | |
| 82 | do { |
| 83 | int j; |
| 84 | unsigned int page_offs = (offs & (block_size - 1)) << 1; |
| 85 | |
Scott Wood | 7b8f668 | 2012-08-08 15:03:33 +0000 | [diff] [blame] | 86 | __raw_writel(~0, ®s->ltesr); |
| 87 | __raw_writel(0, ®s->lteatr); |
| 88 | __raw_writel(page_offs, ®s->fpar); |
| 89 | __raw_writel(fmr, ®s->fmr); |
| 90 | sync(); |
| 91 | __raw_writel(0, ®s->lsor); |
Scott Wood | e4c0950 | 2008-06-30 14:13:28 -0500 | [diff] [blame] | 92 | nand_wait(); |
| 93 | |
| 94 | page_offs %= WINDOW_SIZE; |
| 95 | |
| 96 | /* |
| 97 | * If either of the first two pages are marked bad, |
| 98 | * continue to the next block. |
| 99 | */ |
| 100 | if (i++ < 2 && buf[page_offs + bad_marker] != 0xff) { |
| 101 | puts("skipping\n"); |
| 102 | offs = (offs + block_size) & ~(block_size - 1); |
| 103 | pos &= ~(block_size - 1); |
| 104 | break; |
| 105 | } |
| 106 | |
| 107 | for (j = 0; j < page_size; j++) |
| 108 | dst[pos + j] = buf[page_offs + j]; |
| 109 | |
| 110 | pos += page_size; |
| 111 | offs += page_size; |
Mingkai Hu | 269610f | 2009-07-30 17:56:51 +0800 | [diff] [blame] | 112 | } while ((offs & (block_size - 1)) && (pos < uboot_size)); |
Scott Wood | e4c0950 | 2008-06-30 14:13:28 -0500 | [diff] [blame] | 113 | } |
| 114 | } |
| 115 | |
| 116 | /* |
| 117 | * The main entry for NAND booting. It's necessary that SDRAM is already |
| 118 | * configured and available since this code loads the main U-Boot image |
| 119 | * from NAND into SDRAM and starts it from there. |
| 120 | */ |
| 121 | void nand_boot(void) |
| 122 | { |
| 123 | __attribute__((noreturn)) void (*uboot)(void); |
| 124 | |
Scott Wood | e4c0950 | 2008-06-30 14:13:28 -0500 | [diff] [blame] | 125 | /* |
| 126 | * Load U-Boot image from NAND into RAM |
| 127 | */ |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 128 | nand_load(CONFIG_SYS_NAND_U_BOOT_OFFS, CONFIG_SYS_NAND_U_BOOT_SIZE, |
| 129 | (uchar *)CONFIG_SYS_NAND_U_BOOT_DST); |
Scott Wood | e4c0950 | 2008-06-30 14:13:28 -0500 | [diff] [blame] | 130 | |
| 131 | /* |
| 132 | * Jump to U-Boot image |
| 133 | */ |
| 134 | puts("transfering control\n"); |
Dave Liu | c70564e | 2008-12-02 11:48:51 +0800 | [diff] [blame] | 135 | /* |
| 136 | * Clean d-cache and invalidate i-cache, to |
| 137 | * make sure that no stale data is executed. |
| 138 | */ |
| 139 | flush_cache(CONFIG_SYS_NAND_U_BOOT_DST, CONFIG_SYS_NAND_U_BOOT_SIZE); |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 140 | uboot = (void *)CONFIG_SYS_NAND_U_BOOT_START; |
Scott Wood | e4c0950 | 2008-06-30 14:13:28 -0500 | [diff] [blame] | 141 | uboot(); |
| 142 | } |