Harald Welte | 1615877 | 2007-12-19 15:10:52 +0100 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2006 OpenMoko, Inc. |
| 3 | * Author: Harald Welte <laforge@openmoko.org> |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or |
| 6 | * modify it under the terms of the GNU General Public License as |
| 7 | * published by the Free Software Foundation; either version 2 of |
| 8 | * the License, or (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
| 18 | * MA 02111-1307 USA |
| 19 | */ |
| 20 | |
| 21 | #include <common.h> |
| 22 | |
| 23 | #if 0 |
| 24 | #define DEBUGN printf |
| 25 | #else |
| 26 | #define DEBUGN(x, args ...) {} |
| 27 | #endif |
| 28 | |
Harald Welte | 1615877 | 2007-12-19 15:10:52 +0100 | [diff] [blame] | 29 | #include <nand.h> |
| 30 | #include <s3c2410.h> |
Scott Wood | 1a23a19 | 2008-08-13 17:04:30 -0500 | [diff] [blame] | 31 | #include <asm/io.h> |
Harald Welte | 1615877 | 2007-12-19 15:10:52 +0100 | [diff] [blame] | 32 | |
| 33 | #define __REGb(x) (*(volatile unsigned char *)(x)) |
| 34 | #define __REGi(x) (*(volatile unsigned int *)(x)) |
| 35 | |
| 36 | #define NF_BASE 0x4e000000 |
| 37 | #define NFCONF __REGi(NF_BASE + 0x0) |
| 38 | #define NFCMD __REGb(NF_BASE + 0x4) |
| 39 | #define NFADDR __REGb(NF_BASE + 0x8) |
| 40 | #define NFDATA __REGb(NF_BASE + 0xc) |
| 41 | #define NFSTAT __REGb(NF_BASE + 0x10) |
| 42 | #define NFECC0 __REGb(NF_BASE + 0x14) |
| 43 | #define NFECC1 __REGb(NF_BASE + 0x15) |
| 44 | #define NFECC2 __REGb(NF_BASE + 0x16) |
| 45 | |
| 46 | #define S3C2410_NFCONF_EN (1<<15) |
| 47 | #define S3C2410_NFCONF_512BYTE (1<<14) |
| 48 | #define S3C2410_NFCONF_4STEP (1<<13) |
| 49 | #define S3C2410_NFCONF_INITECC (1<<12) |
| 50 | #define S3C2410_NFCONF_nFCE (1<<11) |
| 51 | #define S3C2410_NFCONF_TACLS(x) ((x)<<8) |
| 52 | #define S3C2410_NFCONF_TWRPH0(x) ((x)<<4) |
| 53 | #define S3C2410_NFCONF_TWRPH1(x) ((x)<<0) |
| 54 | |
Scott Wood | 1a23a19 | 2008-08-13 17:04:30 -0500 | [diff] [blame] | 55 | #define S3C2410_ADDR_NALE 4 |
| 56 | #define S3C2410_ADDR_NCLE 8 |
| 57 | |
| 58 | static void s3c2410_hwcontrol(struct mtd_info *mtd, int cmd, unsigned int ctrl) |
Harald Welte | 1615877 | 2007-12-19 15:10:52 +0100 | [diff] [blame] | 59 | { |
| 60 | struct nand_chip *chip = mtd->priv; |
| 61 | |
Scott Wood | 1a23a19 | 2008-08-13 17:04:30 -0500 | [diff] [blame] | 62 | DEBUGN("hwcontrol(): 0x%02x 0x%02x\n", cmd, ctrl); |
Harald Welte | 1615877 | 2007-12-19 15:10:52 +0100 | [diff] [blame] | 63 | |
Scott Wood | 1a23a19 | 2008-08-13 17:04:30 -0500 | [diff] [blame] | 64 | if (ctrl & NAND_CTRL_CHANGE) { |
| 65 | ulong IO_ADDR_W = NF_BASE; |
| 66 | |
| 67 | if (!(ctrl & NAND_CLE)) |
| 68 | IO_ADDR_W |= S3C2410_ADDR_NCLE; |
| 69 | if (!(ctrl & NAND_ALE)) |
| 70 | IO_ADDR_W |= S3C2410_ADDR_NALE; |
| 71 | |
| 72 | chip->IO_ADDR_W = (void *)IO_ADDR_W; |
| 73 | |
| 74 | if (ctrl & NAND_NCE) |
| 75 | NFCONF &= ~S3C2410_NFCONF_nFCE; |
| 76 | else |
| 77 | NFCONF |= S3C2410_NFCONF_nFCE; |
Harald Welte | 1615877 | 2007-12-19 15:10:52 +0100 | [diff] [blame] | 78 | } |
Scott Wood | 1a23a19 | 2008-08-13 17:04:30 -0500 | [diff] [blame] | 79 | |
| 80 | if (cmd != NAND_CMD_NONE) |
| 81 | writeb(cmd, chip->IO_ADDR_W); |
Harald Welte | 1615877 | 2007-12-19 15:10:52 +0100 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | static int s3c2410_dev_ready(struct mtd_info *mtd) |
| 85 | { |
| 86 | DEBUGN("dev_ready\n"); |
| 87 | return (NFSTAT & 0x01); |
| 88 | } |
| 89 | |
| 90 | #ifdef CONFIG_S3C2410_NAND_HWECC |
| 91 | void s3c2410_nand_enable_hwecc(struct mtd_info *mtd, int mode) |
| 92 | { |
Scott Wood | 1a23a19 | 2008-08-13 17:04:30 -0500 | [diff] [blame] | 93 | DEBUGN("s3c2410_nand_enable_hwecc(%p, %d)\n", mtd, mode); |
Harald Welte | 1615877 | 2007-12-19 15:10:52 +0100 | [diff] [blame] | 94 | NFCONF |= S3C2410_NFCONF_INITECC; |
| 95 | } |
| 96 | |
| 97 | static int s3c2410_nand_calculate_ecc(struct mtd_info *mtd, const u_char *dat, |
| 98 | u_char *ecc_code) |
| 99 | { |
| 100 | ecc_code[0] = NFECC0; |
| 101 | ecc_code[1] = NFECC1; |
| 102 | ecc_code[2] = NFECC2; |
| 103 | DEBUGN("s3c2410_nand_calculate_hwecc(%p,): 0x%02x 0x%02x 0x%02x\n", |
| 104 | mtd , ecc_code[0], ecc_code[1], ecc_code[2]); |
| 105 | |
| 106 | return 0; |
| 107 | } |
| 108 | |
| 109 | static int s3c2410_nand_correct_data(struct mtd_info *mtd, u_char *dat, |
| 110 | u_char *read_ecc, u_char *calc_ecc) |
| 111 | { |
| 112 | if (read_ecc[0] == calc_ecc[0] && |
| 113 | read_ecc[1] == calc_ecc[1] && |
| 114 | read_ecc[2] == calc_ecc[2]) |
| 115 | return 0; |
| 116 | |
| 117 | printf("s3c2410_nand_correct_data: not implemented\n"); |
| 118 | return -1; |
| 119 | } |
| 120 | #endif |
| 121 | |
| 122 | int board_nand_init(struct nand_chip *nand) |
| 123 | { |
| 124 | u_int32_t cfg; |
| 125 | u_int8_t tacls, twrph0, twrph1; |
| 126 | S3C24X0_CLOCK_POWER * const clk_power = S3C24X0_GetBase_CLOCK_POWER(); |
| 127 | |
| 128 | DEBUGN("board_nand_init()\n"); |
| 129 | |
| 130 | clk_power->CLKCON |= (1 << 4); |
| 131 | |
| 132 | /* initialize hardware */ |
| 133 | twrph0 = 3; twrph1 = 0; tacls = 0; |
| 134 | |
| 135 | cfg = S3C2410_NFCONF_EN; |
| 136 | cfg |= S3C2410_NFCONF_TACLS(tacls - 1); |
| 137 | cfg |= S3C2410_NFCONF_TWRPH0(twrph0 - 1); |
| 138 | cfg |= S3C2410_NFCONF_TWRPH1(twrph1 - 1); |
| 139 | |
| 140 | NFCONF = cfg; |
| 141 | |
| 142 | /* initialize nand_chip data structure */ |
Scott Wood | 1a23a19 | 2008-08-13 17:04:30 -0500 | [diff] [blame] | 143 | nand->IO_ADDR_R = nand->IO_ADDR_W = (void *)0x4e00000c; |
Harald Welte | 1615877 | 2007-12-19 15:10:52 +0100 | [diff] [blame] | 144 | |
| 145 | /* read_buf and write_buf are default */ |
| 146 | /* read_byte and write_byte are default */ |
| 147 | |
| 148 | /* hwcontrol always must be implemented */ |
Scott Wood | 1a23a19 | 2008-08-13 17:04:30 -0500 | [diff] [blame] | 149 | nand->cmd_ctrl = s3c2410_hwcontrol; |
Harald Welte | 1615877 | 2007-12-19 15:10:52 +0100 | [diff] [blame] | 150 | |
| 151 | nand->dev_ready = s3c2410_dev_ready; |
| 152 | |
| 153 | #ifdef CONFIG_S3C2410_NAND_HWECC |
Scott Wood | 1a23a19 | 2008-08-13 17:04:30 -0500 | [diff] [blame] | 154 | nand->ecc.hwctl = s3c2410_nand_enable_hwecc; |
| 155 | nand->ecc.calculate = s3c2410_nand_calculate_ecc; |
| 156 | nand->ecc.correct = s3c2410_nand_correct_data; |
| 157 | nand->ecc.mode = NAND_ECC_HW3_512; |
Harald Welte | 1615877 | 2007-12-19 15:10:52 +0100 | [diff] [blame] | 158 | #else |
Scott Wood | 1a23a19 | 2008-08-13 17:04:30 -0500 | [diff] [blame] | 159 | nand->ecc.mode = NAND_ECC_SOFT; |
Harald Welte | 1615877 | 2007-12-19 15:10:52 +0100 | [diff] [blame] | 160 | #endif |
| 161 | |
| 162 | #ifdef CONFIG_S3C2410_NAND_BBT |
| 163 | nand->options = NAND_USE_FLASH_BBT; |
| 164 | #else |
| 165 | nand->options = 0; |
| 166 | #endif |
| 167 | |
| 168 | DEBUGN("end of nand_init\n"); |
| 169 | |
| 170 | return 0; |
| 171 | } |