Wolfgang Denk | 4646d2a | 2006-05-30 15:56:48 +0200 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2006 |
| 3 | * Stefan Roese, DENX Software Engineering, sr@denx.de. |
| 4 | * |
Wolfgang Denk | d79de1d | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 5 | * SPDX-License-Identifier: GPL-2.0+ |
Wolfgang Denk | 4646d2a | 2006-05-30 15:56:48 +0200 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include <common.h> |
| 9 | |
Jon Loeliger | 145318c | 2007-07-09 18:38:39 -0500 | [diff] [blame] | 10 | #if defined(CONFIG_CMD_NAND) |
Wolfgang Denk | 4646d2a | 2006-05-30 15:56:48 +0200 | [diff] [blame] | 11 | |
| 12 | #include <nand.h> |
| 13 | |
| 14 | struct pdnb3_ndfc_regs { |
| 15 | uchar cmd; |
| 16 | uchar wait; |
| 17 | uchar addr; |
| 18 | uchar term; |
| 19 | uchar data; |
| 20 | }; |
| 21 | |
| 22 | static u8 hwctl; |
| 23 | static struct pdnb3_ndfc_regs *pdnb3_ndfc; |
| 24 | |
| 25 | #define readb(addr) *(volatile u_char *)(addr) |
| 26 | #define readl(addr) *(volatile u_long *)(addr) |
| 27 | #define writeb(d,addr) *(volatile u_char *)(addr) = (d) |
| 28 | |
| 29 | /* |
| 30 | * The PDNB3 has a NAND Flash Controller (NDFC) that handles all accesses to |
| 31 | * the NAND devices. The NDFC has command, address and data registers that |
| 32 | * when accessed will set up the NAND flash pins appropriately. We'll use the |
| 33 | * hwcontrol function to save the configuration in a global variable. |
| 34 | * We can then use this information in the read and write functions to |
| 35 | * determine which NDFC register to access. |
| 36 | * |
| 37 | * There is one NAND devices on the board, a Hynix HY27US08561A (32 MByte). |
| 38 | */ |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 39 | static void pdnb3_nand_hwcontrol(struct mtd_info *mtd, int cmd, unsigned int ctrl) |
Wolfgang Denk | 4646d2a | 2006-05-30 15:56:48 +0200 | [diff] [blame] | 40 | { |
William Juul | 9e9c2c1 | 2007-11-09 13:32:30 +0100 | [diff] [blame] | 41 | struct nand_chip *this = mtd->priv; |
Wolfgang Denk | 4646d2a | 2006-05-30 15:56:48 +0200 | [diff] [blame] | 42 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 43 | if (ctrl & NAND_CTRL_CHANGE) { |
| 44 | if ( ctrl & NAND_CLE ) |
| 45 | hwctl |= 0x1; |
| 46 | else |
| 47 | hwctl &= ~0x1; |
| 48 | if ( ctrl & NAND_ALE ) |
| 49 | hwctl |= 0x2; |
| 50 | else |
| 51 | hwctl &= ~0x2; |
| 52 | if ( (ctrl & NAND_NCE) != NAND_NCE) |
| 53 | writeb(0x00, &(pdnb3_ndfc->term)); |
Wolfgang Denk | 4646d2a | 2006-05-30 15:56:48 +0200 | [diff] [blame] | 54 | } |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 55 | if (cmd != NAND_CMD_NONE) |
| 56 | writeb(cmd, this->IO_ADDR_W); |
Wolfgang Denk | 4646d2a | 2006-05-30 15:56:48 +0200 | [diff] [blame] | 57 | } |
| 58 | |
Wolfgang Denk | 4646d2a | 2006-05-30 15:56:48 +0200 | [diff] [blame] | 59 | |
| 60 | static u_char pdnb3_nand_read_byte(struct mtd_info *mtd) |
| 61 | { |
| 62 | return readb(&(pdnb3_ndfc->data)); |
| 63 | } |
| 64 | |
| 65 | static void pdnb3_nand_write_buf(struct mtd_info *mtd, const u_char *buf, int len) |
| 66 | { |
| 67 | int i; |
| 68 | |
| 69 | for (i = 0; i < len; i++) { |
| 70 | if (hwctl & 0x1) |
| 71 | writeb(buf[i], &(pdnb3_ndfc->cmd)); |
| 72 | else if (hwctl & 0x2) |
| 73 | writeb(buf[i], &(pdnb3_ndfc->addr)); |
| 74 | else |
| 75 | writeb(buf[i], &(pdnb3_ndfc->data)); |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | static void pdnb3_nand_read_buf(struct mtd_info *mtd, u_char *buf, int len) |
| 80 | { |
| 81 | int i; |
| 82 | |
Marek Vasut | 071a612 | 2012-03-06 01:10:00 +0100 | [diff] [blame] | 83 | for (i = 0; i < len; i++) |
| 84 | buf[i] = readb(&(pdnb3_ndfc->data)); |
Wolfgang Denk | 4646d2a | 2006-05-30 15:56:48 +0200 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | static int pdnb3_nand_verify_buf(struct mtd_info *mtd, const u_char *buf, int len) |
| 88 | { |
| 89 | int i; |
| 90 | |
| 91 | for (i = 0; i < len; i++) |
| 92 | if (buf[i] != readb(&(pdnb3_ndfc->data))) |
| 93 | return i; |
| 94 | |
| 95 | return 0; |
| 96 | } |
| 97 | |
| 98 | static int pdnb3_nand_dev_ready(struct mtd_info *mtd) |
| 99 | { |
Wolfgang Denk | 4646d2a | 2006-05-30 15:56:48 +0200 | [diff] [blame] | 100 | /* |
| 101 | * Blocking read to wait for NAND to be ready |
| 102 | */ |
Marek Vasut | 071a612 | 2012-03-06 01:10:00 +0100 | [diff] [blame] | 103 | readb(&(pdnb3_ndfc->wait)); |
Wolfgang Denk | 4646d2a | 2006-05-30 15:56:48 +0200 | [diff] [blame] | 104 | |
| 105 | /* |
| 106 | * Return always true |
| 107 | */ |
| 108 | return 1; |
| 109 | } |
| 110 | |
Heiko Schocher | 3ec4366 | 2006-12-21 17:17:02 +0100 | [diff] [blame] | 111 | int board_nand_init(struct nand_chip *nand) |
Wolfgang Denk | 4646d2a | 2006-05-30 15:56:48 +0200 | [diff] [blame] | 112 | { |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 113 | pdnb3_ndfc = (struct pdnb3_ndfc_regs *)CONFIG_SYS_NAND_BASE; |
Wolfgang Denk | 4646d2a | 2006-05-30 15:56:48 +0200 | [diff] [blame] | 114 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 115 | nand->ecc.mode = NAND_ECC_SOFT; |
Wolfgang Denk | 4646d2a | 2006-05-30 15:56:48 +0200 | [diff] [blame] | 116 | |
| 117 | /* Set address of NAND IO lines (Using Linear Data Access Region) */ |
| 118 | nand->IO_ADDR_R = (void __iomem *) ((ulong) pdnb3_ndfc + 0x4); |
| 119 | nand->IO_ADDR_W = (void __iomem *) ((ulong) pdnb3_ndfc + 0x4); |
| 120 | /* Reference hardware control function */ |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 121 | nand->cmd_ctrl = pdnb3_nand_hwcontrol; |
Wolfgang Denk | 4646d2a | 2006-05-30 15:56:48 +0200 | [diff] [blame] | 122 | nand->read_byte = pdnb3_nand_read_byte; |
| 123 | nand->write_buf = pdnb3_nand_write_buf; |
| 124 | nand->read_buf = pdnb3_nand_read_buf; |
| 125 | nand->verify_buf = pdnb3_nand_verify_buf; |
| 126 | nand->dev_ready = pdnb3_nand_dev_ready; |
Heiko Schocher | 3ec4366 | 2006-12-21 17:17:02 +0100 | [diff] [blame] | 127 | return 0; |
Wolfgang Denk | 4646d2a | 2006-05-30 15:56:48 +0200 | [diff] [blame] | 128 | } |
| 129 | #endif |