Stefan Roese | 899620c | 2006-08-15 14:22:35 +0200 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2006 |
| 3 | * Heiko Schocher, DENX Software Engineering, hs@denx.de |
| 4 | * |
Stefan Roese | 5bc528f | 2006-10-07 11:35:25 +0200 | [diff] [blame^] | 5 | * (C) Copyright 2006 |
| 6 | * Stefan Roese, DENX Software Engineering, sr@denx.de. |
| 7 | * |
Stefan Roese | 899620c | 2006-08-15 14:22:35 +0200 | [diff] [blame] | 8 | * See file CREDITS for list of people who contributed to this |
| 9 | * project. |
| 10 | * |
| 11 | * This program is free software; you can redistribute it and/or |
| 12 | * modify it under the terms of the GNU General Public License as |
| 13 | * published by the Free Software Foundation; either version 2 of |
| 14 | * the License, or (at your option) any later version. |
| 15 | * |
| 16 | * This program is distributed in the hope that it will be useful, |
| 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | * GNU General Public License for more details. |
| 20 | * |
| 21 | * You should have received a copy of the GNU General Public License |
| 22 | * along with this program; if not, write to the Free Software |
| 23 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
| 24 | * MA 02111-1307 USA |
| 25 | */ |
| 26 | |
| 27 | #include <common.h> |
Stefan Roese | 899620c | 2006-08-15 14:22:35 +0200 | [diff] [blame] | 28 | |
| 29 | #if (CONFIG_COMMANDS & CFG_CMD_NAND) |
| 30 | |
Stefan Roese | 5bc528f | 2006-10-07 11:35:25 +0200 | [diff] [blame^] | 31 | #include <asm/processor.h> |
Stefan Roese | 899620c | 2006-08-15 14:22:35 +0200 | [diff] [blame] | 32 | #include <nand.h> |
| 33 | |
Stefan Roese | 899620c | 2006-08-15 14:22:35 +0200 | [diff] [blame] | 34 | struct alpr_ndfc_regs { |
Stefan Roese | 5bc528f | 2006-10-07 11:35:25 +0200 | [diff] [blame^] | 35 | u16 cmd[4]; |
| 36 | u16 addr_wait; |
| 37 | u16 term; |
| 38 | u16 dummy; |
| 39 | u16 dummy2; |
| 40 | u16 data; |
Stefan Roese | 899620c | 2006-08-15 14:22:35 +0200 | [diff] [blame] | 41 | }; |
| 42 | |
| 43 | static u8 hwctl; |
Stefan Roese | 5bc528f | 2006-10-07 11:35:25 +0200 | [diff] [blame^] | 44 | static struct alpr_ndfc_regs *alpr_ndfc = NULL; |
Stefan Roese | 899620c | 2006-08-15 14:22:35 +0200 | [diff] [blame] | 45 | |
Stefan Roese | 5bc528f | 2006-10-07 11:35:25 +0200 | [diff] [blame^] | 46 | #define readb(addr) (u8)(*(volatile u16 *)(addr)) |
| 47 | #define writeb(d,addr) *(volatile u16 *)(addr) = ((u16)(d)) |
Stefan Roese | 899620c | 2006-08-15 14:22:35 +0200 | [diff] [blame] | 48 | |
Stefan Roese | 899620c | 2006-08-15 14:22:35 +0200 | [diff] [blame] | 49 | /* |
| 50 | * The ALPR has a NAND Flash Controller (NDFC) that handles all accesses to |
| 51 | * the NAND devices. The NDFC has command, address and data registers that |
| 52 | * when accessed will set up the NAND flash pins appropriately. We'll use the |
| 53 | * hwcontrol function to save the configuration in a global variable. |
| 54 | * We can then use this information in the read and write functions to |
| 55 | * determine which NDFC register to access. |
| 56 | * |
Stefan Roese | 5bc528f | 2006-10-07 11:35:25 +0200 | [diff] [blame^] | 57 | * There are 2 NAND devices on the board, a Hynix HY27US08561A (1 GByte). |
Stefan Roese | 899620c | 2006-08-15 14:22:35 +0200 | [diff] [blame] | 58 | */ |
Stefan Roese | 5bc528f | 2006-10-07 11:35:25 +0200 | [diff] [blame^] | 59 | static void alpr_nand_hwcontrol(struct mtd_info *mtd, int cmd) |
Stefan Roese | 899620c | 2006-08-15 14:22:35 +0200 | [diff] [blame] | 60 | { |
Stefan Roese | 899620c | 2006-08-15 14:22:35 +0200 | [diff] [blame] | 61 | switch (cmd) { |
| 62 | case NAND_CTL_SETCLE: |
| 63 | hwctl |= 0x1; |
| 64 | break; |
| 65 | case NAND_CTL_CLRCLE: |
| 66 | hwctl &= ~0x1; |
| 67 | break; |
| 68 | case NAND_CTL_SETALE: |
| 69 | hwctl |= 0x2; |
| 70 | break; |
| 71 | case NAND_CTL_CLRALE: |
| 72 | hwctl &= ~0x2; |
| 73 | break; |
| 74 | case NAND_CTL_SETNCE: |
| 75 | break; |
| 76 | case NAND_CTL_CLRNCE: |
Stefan Roese | 5bc528f | 2006-10-07 11:35:25 +0200 | [diff] [blame^] | 77 | writeb(0x00, &(alpr_ndfc->term)); |
Stefan Roese | 899620c | 2006-08-15 14:22:35 +0200 | [diff] [blame] | 78 | break; |
| 79 | } |
| 80 | } |
| 81 | |
Stefan Roese | 5bc528f | 2006-10-07 11:35:25 +0200 | [diff] [blame^] | 82 | static void alpr_nand_write_byte(struct mtd_info *mtd, u_char byte) |
Stefan Roese | 899620c | 2006-08-15 14:22:35 +0200 | [diff] [blame] | 83 | { |
Stefan Roese | 5bc528f | 2006-10-07 11:35:25 +0200 | [diff] [blame^] | 84 | struct nand_chip *nand = mtd->priv; |
| 85 | |
Stefan Roese | 899620c | 2006-08-15 14:22:35 +0200 | [diff] [blame] | 86 | if (hwctl & 0x1) |
Stefan Roese | 5bc528f | 2006-10-07 11:35:25 +0200 | [diff] [blame^] | 87 | /* |
| 88 | * IO_ADDR_W used as CMD[i] reg to support multiple NAND |
| 89 | * chips. |
| 90 | */ |
| 91 | writeb(byte, nand->IO_ADDR_W); |
Stefan Roese | 899620c | 2006-08-15 14:22:35 +0200 | [diff] [blame] | 92 | else if (hwctl & 0x2) { |
Stefan Roese | 5bc528f | 2006-10-07 11:35:25 +0200 | [diff] [blame^] | 93 | writeb(byte, &(alpr_ndfc->addr_wait)); |
Stefan Roese | 899620c | 2006-08-15 14:22:35 +0200 | [diff] [blame] | 94 | } else |
Stefan Roese | 5bc528f | 2006-10-07 11:35:25 +0200 | [diff] [blame^] | 95 | writeb(byte, &(alpr_ndfc->data)); |
Stefan Roese | 899620c | 2006-08-15 14:22:35 +0200 | [diff] [blame] | 96 | } |
| 97 | |
Stefan Roese | 5bc528f | 2006-10-07 11:35:25 +0200 | [diff] [blame^] | 98 | static u_char alpr_nand_read_byte(struct mtd_info *mtd) |
Stefan Roese | 899620c | 2006-08-15 14:22:35 +0200 | [diff] [blame] | 99 | { |
Stefan Roese | 5bc528f | 2006-10-07 11:35:25 +0200 | [diff] [blame^] | 100 | return readb(&(alpr_ndfc->data)); |
Stefan Roese | 899620c | 2006-08-15 14:22:35 +0200 | [diff] [blame] | 101 | } |
| 102 | |
Stefan Roese | 5bc528f | 2006-10-07 11:35:25 +0200 | [diff] [blame^] | 103 | static void alpr_nand_write_buf(struct mtd_info *mtd, const u_char *buf, int len) |
Stefan Roese | 899620c | 2006-08-15 14:22:35 +0200 | [diff] [blame] | 104 | { |
Stefan Roese | 5bc528f | 2006-10-07 11:35:25 +0200 | [diff] [blame^] | 105 | struct nand_chip *nand = mtd->priv; |
Stefan Roese | 899620c | 2006-08-15 14:22:35 +0200 | [diff] [blame] | 106 | int i; |
| 107 | |
Stefan Roese | 899620c | 2006-08-15 14:22:35 +0200 | [diff] [blame] | 108 | for (i = 0; i < len; i++) { |
| 109 | if (hwctl & 0x1) |
Stefan Roese | 5bc528f | 2006-10-07 11:35:25 +0200 | [diff] [blame^] | 110 | /* |
| 111 | * IO_ADDR_W used as CMD[i] reg to support multiple NAND |
| 112 | * chips. |
| 113 | */ |
| 114 | writeb(buf[i], nand->IO_ADDR_W); |
| 115 | else if (hwctl & 0x2) |
| 116 | writeb(buf[i], &(alpr_ndfc->addr_wait)); |
| 117 | else |
| 118 | writeb(buf[i], &(alpr_ndfc->data)); |
Stefan Roese | 899620c | 2006-08-15 14:22:35 +0200 | [diff] [blame] | 119 | } |
| 120 | } |
| 121 | |
Stefan Roese | 5bc528f | 2006-10-07 11:35:25 +0200 | [diff] [blame^] | 122 | static void alpr_nand_read_buf(struct mtd_info *mtd, u_char *buf, int len) |
Stefan Roese | 899620c | 2006-08-15 14:22:35 +0200 | [diff] [blame] | 123 | { |
| 124 | int i; |
| 125 | |
| 126 | for (i = 0; i < len; i++) { |
Stefan Roese | 5bc528f | 2006-10-07 11:35:25 +0200 | [diff] [blame^] | 127 | buf[i] = readb(&(alpr_ndfc->data)); |
Stefan Roese | 899620c | 2006-08-15 14:22:35 +0200 | [diff] [blame] | 128 | } |
| 129 | } |
| 130 | |
Stefan Roese | 5bc528f | 2006-10-07 11:35:25 +0200 | [diff] [blame^] | 131 | static int alpr_nand_verify_buf(struct mtd_info *mtd, const u_char *buf, int len) |
Stefan Roese | 899620c | 2006-08-15 14:22:35 +0200 | [diff] [blame] | 132 | { |
| 133 | int i; |
| 134 | |
| 135 | for (i = 0; i < len; i++) |
Stefan Roese | 5bc528f | 2006-10-07 11:35:25 +0200 | [diff] [blame^] | 136 | if (buf[i] != readb(&(alpr_ndfc->data))) |
Stefan Roese | 899620c | 2006-08-15 14:22:35 +0200 | [diff] [blame] | 137 | return i; |
| 138 | |
| 139 | return 0; |
| 140 | } |
| 141 | |
Stefan Roese | 5bc528f | 2006-10-07 11:35:25 +0200 | [diff] [blame^] | 142 | static int alpr_nand_dev_ready(struct mtd_info *mtd) |
Stefan Roese | 899620c | 2006-08-15 14:22:35 +0200 | [diff] [blame] | 143 | { |
Stefan Roese | 899620c | 2006-08-15 14:22:35 +0200 | [diff] [blame] | 144 | volatile u_char val; |
| 145 | |
Stefan Roese | 899620c | 2006-08-15 14:22:35 +0200 | [diff] [blame] | 146 | /* |
| 147 | * Blocking read to wait for NAND to be ready |
| 148 | */ |
Stefan Roese | 5bc528f | 2006-10-07 11:35:25 +0200 | [diff] [blame^] | 149 | val = readb(&(alpr_ndfc->addr_wait)); |
Stefan Roese | 899620c | 2006-08-15 14:22:35 +0200 | [diff] [blame] | 150 | |
| 151 | /* |
| 152 | * Return always true |
| 153 | */ |
| 154 | return 1; |
Stefan Roese | 899620c | 2006-08-15 14:22:35 +0200 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | void board_nand_init(struct nand_chip *nand) |
| 158 | { |
| 159 | alpr_ndfc = (struct alpr_ndfc_regs *)CFG_NAND_BASE; |
| 160 | |
| 161 | nand->eccmode = NAND_ECC_SOFT; |
| 162 | |
Stefan Roese | 899620c | 2006-08-15 14:22:35 +0200 | [diff] [blame] | 163 | /* Reference hardware control function */ |
Stefan Roese | 5bc528f | 2006-10-07 11:35:25 +0200 | [diff] [blame^] | 164 | nand->hwcontrol = alpr_nand_hwcontrol; |
Stefan Roese | 899620c | 2006-08-15 14:22:35 +0200 | [diff] [blame] | 165 | /* Set command delay time */ |
Stefan Roese | 5bc528f | 2006-10-07 11:35:25 +0200 | [diff] [blame^] | 166 | nand->write_byte = alpr_nand_write_byte; |
| 167 | nand->read_byte = alpr_nand_read_byte; |
| 168 | nand->write_buf = alpr_nand_write_buf; |
| 169 | nand->read_buf = alpr_nand_read_buf; |
| 170 | nand->verify_buf = alpr_nand_verify_buf; |
| 171 | nand->dev_ready = alpr_nand_dev_ready; |
Stefan Roese | 899620c | 2006-08-15 14:22:35 +0200 | [diff] [blame] | 172 | } |
| 173 | #endif |