Wolfgang Denk | ba94a1b | 2006-05-30 15:56:48 +0200 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2006 |
| 3 | * Stefan Roese, DENX Software Engineering, sr@denx.de. |
| 4 | * |
| 5 | * See file CREDITS for list of people who contributed to this |
| 6 | * project. |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or |
| 9 | * modify it under the terms of the GNU General Public License as |
| 10 | * published by the Free Software Foundation; either version 2 of |
| 11 | * the License, or (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License |
| 19 | * along with this program; if not, write to the Free Software |
| 20 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
| 21 | * MA 02111-1307 USA |
| 22 | */ |
| 23 | |
| 24 | #include <common.h> |
| 25 | |
Jon Loeliger | 3fe0010 | 2007-07-09 18:38:39 -0500 | [diff] [blame] | 26 | #if defined(CONFIG_CMD_NAND) |
Wolfgang Denk | ba94a1b | 2006-05-30 15:56:48 +0200 | [diff] [blame] | 27 | |
| 28 | #include <nand.h> |
| 29 | |
| 30 | struct pdnb3_ndfc_regs { |
| 31 | uchar cmd; |
| 32 | uchar wait; |
| 33 | uchar addr; |
| 34 | uchar term; |
| 35 | uchar data; |
| 36 | }; |
| 37 | |
| 38 | static u8 hwctl; |
| 39 | static struct pdnb3_ndfc_regs *pdnb3_ndfc; |
| 40 | |
| 41 | #define readb(addr) *(volatile u_char *)(addr) |
| 42 | #define readl(addr) *(volatile u_long *)(addr) |
| 43 | #define writeb(d,addr) *(volatile u_char *)(addr) = (d) |
| 44 | |
| 45 | /* |
| 46 | * The PDNB3 has a NAND Flash Controller (NDFC) that handles all accesses to |
| 47 | * the NAND devices. The NDFC has command, address and data registers that |
| 48 | * when accessed will set up the NAND flash pins appropriately. We'll use the |
| 49 | * hwcontrol function to save the configuration in a global variable. |
| 50 | * We can then use this information in the read and write functions to |
| 51 | * determine which NDFC register to access. |
| 52 | * |
| 53 | * There is one NAND devices on the board, a Hynix HY27US08561A (32 MByte). |
| 54 | */ |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 55 | static void pdnb3_nand_hwcontrol(struct mtd_info *mtd, int cmd, unsigned int ctrl) |
Wolfgang Denk | ba94a1b | 2006-05-30 15:56:48 +0200 | [diff] [blame] | 56 | { |
William Juul | 5e1dae5 | 2007-11-09 13:32:30 +0100 | [diff] [blame] | 57 | struct nand_chip *this = mtd->priv; |
Wolfgang Denk | ba94a1b | 2006-05-30 15:56:48 +0200 | [diff] [blame] | 58 | |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 59 | if (ctrl & NAND_CTRL_CHANGE) { |
| 60 | if ( ctrl & NAND_CLE ) |
| 61 | hwctl |= 0x1; |
| 62 | else |
| 63 | hwctl &= ~0x1; |
| 64 | if ( ctrl & NAND_ALE ) |
| 65 | hwctl |= 0x2; |
| 66 | else |
| 67 | hwctl &= ~0x2; |
| 68 | if ( (ctrl & NAND_NCE) != NAND_NCE) |
| 69 | writeb(0x00, &(pdnb3_ndfc->term)); |
Wolfgang Denk | ba94a1b | 2006-05-30 15:56:48 +0200 | [diff] [blame] | 70 | } |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 71 | if (cmd != NAND_CMD_NONE) |
| 72 | writeb(cmd, this->IO_ADDR_W); |
Wolfgang Denk | ba94a1b | 2006-05-30 15:56:48 +0200 | [diff] [blame] | 73 | } |
| 74 | |
Wolfgang Denk | ba94a1b | 2006-05-30 15:56:48 +0200 | [diff] [blame] | 75 | |
| 76 | static u_char pdnb3_nand_read_byte(struct mtd_info *mtd) |
| 77 | { |
| 78 | return readb(&(pdnb3_ndfc->data)); |
| 79 | } |
| 80 | |
| 81 | static void pdnb3_nand_write_buf(struct mtd_info *mtd, const u_char *buf, int len) |
| 82 | { |
| 83 | int i; |
| 84 | |
| 85 | for (i = 0; i < len; i++) { |
| 86 | if (hwctl & 0x1) |
| 87 | writeb(buf[i], &(pdnb3_ndfc->cmd)); |
| 88 | else if (hwctl & 0x2) |
| 89 | writeb(buf[i], &(pdnb3_ndfc->addr)); |
| 90 | else |
| 91 | writeb(buf[i], &(pdnb3_ndfc->data)); |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | static void pdnb3_nand_read_buf(struct mtd_info *mtd, u_char *buf, int len) |
| 96 | { |
| 97 | int i; |
| 98 | |
Marek Vasut | 44e63c2 | 2012-03-06 01:10:00 +0100 | [diff] [blame] | 99 | for (i = 0; i < len; i++) |
| 100 | buf[i] = readb(&(pdnb3_ndfc->data)); |
Wolfgang Denk | ba94a1b | 2006-05-30 15:56:48 +0200 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | static int pdnb3_nand_verify_buf(struct mtd_info *mtd, const u_char *buf, int len) |
| 104 | { |
| 105 | int i; |
| 106 | |
| 107 | for (i = 0; i < len; i++) |
| 108 | if (buf[i] != readb(&(pdnb3_ndfc->data))) |
| 109 | return i; |
| 110 | |
| 111 | return 0; |
| 112 | } |
| 113 | |
| 114 | static int pdnb3_nand_dev_ready(struct mtd_info *mtd) |
| 115 | { |
Wolfgang Denk | ba94a1b | 2006-05-30 15:56:48 +0200 | [diff] [blame] | 116 | /* |
| 117 | * Blocking read to wait for NAND to be ready |
| 118 | */ |
Marek Vasut | 44e63c2 | 2012-03-06 01:10:00 +0100 | [diff] [blame] | 119 | readb(&(pdnb3_ndfc->wait)); |
Wolfgang Denk | ba94a1b | 2006-05-30 15:56:48 +0200 | [diff] [blame] | 120 | |
| 121 | /* |
| 122 | * Return always true |
| 123 | */ |
| 124 | return 1; |
| 125 | } |
| 126 | |
Heiko Schocher | fa23044 | 2006-12-21 17:17:02 +0100 | [diff] [blame] | 127 | int board_nand_init(struct nand_chip *nand) |
Wolfgang Denk | ba94a1b | 2006-05-30 15:56:48 +0200 | [diff] [blame] | 128 | { |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 129 | pdnb3_ndfc = (struct pdnb3_ndfc_regs *)CONFIG_SYS_NAND_BASE; |
Wolfgang Denk | ba94a1b | 2006-05-30 15:56:48 +0200 | [diff] [blame] | 130 | |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 131 | nand->ecc.mode = NAND_ECC_SOFT; |
Wolfgang Denk | ba94a1b | 2006-05-30 15:56:48 +0200 | [diff] [blame] | 132 | |
| 133 | /* Set address of NAND IO lines (Using Linear Data Access Region) */ |
| 134 | nand->IO_ADDR_R = (void __iomem *) ((ulong) pdnb3_ndfc + 0x4); |
| 135 | nand->IO_ADDR_W = (void __iomem *) ((ulong) pdnb3_ndfc + 0x4); |
| 136 | /* Reference hardware control function */ |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 137 | nand->cmd_ctrl = pdnb3_nand_hwcontrol; |
Wolfgang Denk | ba94a1b | 2006-05-30 15:56:48 +0200 | [diff] [blame] | 138 | nand->read_byte = pdnb3_nand_read_byte; |
| 139 | nand->write_buf = pdnb3_nand_write_buf; |
| 140 | nand->read_buf = pdnb3_nand_read_buf; |
| 141 | nand->verify_buf = pdnb3_nand_verify_buf; |
| 142 | nand->dev_ready = pdnb3_nand_dev_ready; |
Heiko Schocher | fa23044 | 2006-12-21 17:17:02 +0100 | [diff] [blame] | 143 | return 0; |
Wolfgang Denk | ba94a1b | 2006-05-30 15:56:48 +0200 | [diff] [blame] | 144 | } |
| 145 | #endif |