Stefan Roese | 4963968 | 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 | a183188 | 2006-10-07 11:35:25 +0200 | [diff] [blame] | 5 | * (C) Copyright 2006 |
| 6 | * Stefan Roese, DENX Software Engineering, sr@denx.de. |
| 7 | * |
Wolfgang Denk | d79de1d | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 8 | * SPDX-License-Identifier: GPL-2.0+ |
Stefan Roese | 4963968 | 2006-08-15 14:22:35 +0200 | [diff] [blame] | 9 | */ |
| 10 | |
| 11 | #include <common.h> |
Stefan Roese | 4963968 | 2006-08-15 14:22:35 +0200 | [diff] [blame] | 12 | |
Jon Loeliger | 145318c | 2007-07-09 18:38:39 -0500 | [diff] [blame] | 13 | #if defined(CONFIG_CMD_NAND) |
Stefan Roese | 4963968 | 2006-08-15 14:22:35 +0200 | [diff] [blame] | 14 | |
Stefan Roese | a183188 | 2006-10-07 11:35:25 +0200 | [diff] [blame] | 15 | #include <asm/processor.h> |
Stefan Roese | 4963968 | 2006-08-15 14:22:35 +0200 | [diff] [blame] | 16 | #include <nand.h> |
| 17 | |
Stefan Roese | 4963968 | 2006-08-15 14:22:35 +0200 | [diff] [blame] | 18 | struct alpr_ndfc_regs { |
Stefan Roese | 2a4a943 | 2006-11-27 14:12:17 +0100 | [diff] [blame] | 19 | u8 cmd[4]; |
| 20 | u8 addr_wait; |
| 21 | u8 term; |
| 22 | u8 dummy; |
| 23 | u8 dummy2; |
| 24 | u8 data; |
Stefan Roese | 4963968 | 2006-08-15 14:22:35 +0200 | [diff] [blame] | 25 | }; |
| 26 | |
| 27 | static u8 hwctl; |
Stefan Roese | a183188 | 2006-10-07 11:35:25 +0200 | [diff] [blame] | 28 | static struct alpr_ndfc_regs *alpr_ndfc = NULL; |
Stefan Roese | 4963968 | 2006-08-15 14:22:35 +0200 | [diff] [blame] | 29 | |
Stefan Roese | 2a4a943 | 2006-11-27 14:12:17 +0100 | [diff] [blame] | 30 | #define readb(addr) (u8)(*(volatile u8 *)(addr)) |
| 31 | #define writeb(d,addr) *(volatile u8 *)(addr) = ((u8)(d)) |
Stefan Roese | 4963968 | 2006-08-15 14:22:35 +0200 | [diff] [blame] | 32 | |
Stefan Roese | 4963968 | 2006-08-15 14:22:35 +0200 | [diff] [blame] | 33 | /* |
| 34 | * The ALPR has a NAND Flash Controller (NDFC) that handles all accesses to |
| 35 | * the NAND devices. The NDFC has command, address and data registers that |
| 36 | * when accessed will set up the NAND flash pins appropriately. We'll use the |
| 37 | * hwcontrol function to save the configuration in a global variable. |
| 38 | * We can then use this information in the read and write functions to |
| 39 | * determine which NDFC register to access. |
| 40 | * |
Stefan Roese | a183188 | 2006-10-07 11:35:25 +0200 | [diff] [blame] | 41 | * There are 2 NAND devices on the board, a Hynix HY27US08561A (1 GByte). |
Stefan Roese | 4963968 | 2006-08-15 14:22:35 +0200 | [diff] [blame] | 42 | */ |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 43 | static void alpr_nand_hwcontrol(struct mtd_info *mtd, int cmd, unsigned int ctrl) |
William Juul | b76ec38 | 2007-11-08 10:39:53 +0100 | [diff] [blame] | 44 | { |
William Juul | 9e9c2c1 | 2007-11-09 13:32:30 +0100 | [diff] [blame] | 45 | struct nand_chip *this = mtd->priv; |
Stefan Roese | 4963968 | 2006-08-15 14:22:35 +0200 | [diff] [blame] | 46 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 47 | if (ctrl & NAND_CTRL_CHANGE) { |
| 48 | if ( ctrl & NAND_CLE ) |
| 49 | hwctl |= 0x1; |
| 50 | else |
| 51 | hwctl &= ~0x1; |
| 52 | if ( ctrl & NAND_ALE ) |
| 53 | hwctl |= 0x2; |
| 54 | else |
| 55 | hwctl &= ~0x2; |
| 56 | if ( (ctrl & NAND_NCE) != NAND_NCE) |
| 57 | writeb(0x00, &(alpr_ndfc->term)); |
| 58 | } |
| 59 | if (cmd != NAND_CMD_NONE) |
| 60 | writeb(cmd, this->IO_ADDR_W); |
Stefan Roese | 4963968 | 2006-08-15 14:22:35 +0200 | [diff] [blame] | 61 | } |
| 62 | |
Stefan Roese | a183188 | 2006-10-07 11:35:25 +0200 | [diff] [blame] | 63 | static u_char alpr_nand_read_byte(struct mtd_info *mtd) |
Stefan Roese | 4963968 | 2006-08-15 14:22:35 +0200 | [diff] [blame] | 64 | { |
Stefan Roese | a183188 | 2006-10-07 11:35:25 +0200 | [diff] [blame] | 65 | return readb(&(alpr_ndfc->data)); |
Stefan Roese | 4963968 | 2006-08-15 14:22:35 +0200 | [diff] [blame] | 66 | } |
| 67 | |
Stefan Roese | a183188 | 2006-10-07 11:35:25 +0200 | [diff] [blame] | 68 | static void alpr_nand_write_buf(struct mtd_info *mtd, const u_char *buf, int len) |
Stefan Roese | 4963968 | 2006-08-15 14:22:35 +0200 | [diff] [blame] | 69 | { |
Stefan Roese | a183188 | 2006-10-07 11:35:25 +0200 | [diff] [blame] | 70 | struct nand_chip *nand = mtd->priv; |
Stefan Roese | 4963968 | 2006-08-15 14:22:35 +0200 | [diff] [blame] | 71 | int i; |
| 72 | |
Stefan Roese | 4963968 | 2006-08-15 14:22:35 +0200 | [diff] [blame] | 73 | for (i = 0; i < len; i++) { |
| 74 | if (hwctl & 0x1) |
Stefan Roese | a183188 | 2006-10-07 11:35:25 +0200 | [diff] [blame] | 75 | /* |
| 76 | * IO_ADDR_W used as CMD[i] reg to support multiple NAND |
| 77 | * chips. |
| 78 | */ |
| 79 | writeb(buf[i], nand->IO_ADDR_W); |
| 80 | else if (hwctl & 0x2) |
| 81 | writeb(buf[i], &(alpr_ndfc->addr_wait)); |
| 82 | else |
| 83 | writeb(buf[i], &(alpr_ndfc->data)); |
Stefan Roese | 4963968 | 2006-08-15 14:22:35 +0200 | [diff] [blame] | 84 | } |
| 85 | } |
| 86 | |
Stefan Roese | a183188 | 2006-10-07 11:35:25 +0200 | [diff] [blame] | 87 | static void alpr_nand_read_buf(struct mtd_info *mtd, u_char *buf, int len) |
Stefan Roese | 4963968 | 2006-08-15 14:22:35 +0200 | [diff] [blame] | 88 | { |
| 89 | int i; |
| 90 | |
| 91 | for (i = 0; i < len; i++) { |
Stefan Roese | a183188 | 2006-10-07 11:35:25 +0200 | [diff] [blame] | 92 | buf[i] = readb(&(alpr_ndfc->data)); |
Stefan Roese | 4963968 | 2006-08-15 14:22:35 +0200 | [diff] [blame] | 93 | } |
| 94 | } |
| 95 | |
Stefan Roese | a183188 | 2006-10-07 11:35:25 +0200 | [diff] [blame] | 96 | static int alpr_nand_dev_ready(struct mtd_info *mtd) |
Stefan Roese | 4963968 | 2006-08-15 14:22:35 +0200 | [diff] [blame] | 97 | { |
Stefan Roese | 4963968 | 2006-08-15 14:22:35 +0200 | [diff] [blame] | 98 | /* |
| 99 | * Blocking read to wait for NAND to be ready |
| 100 | */ |
Wolfgang Denk | bfed7be | 2011-11-29 22:17:44 +0000 | [diff] [blame] | 101 | (void)readb(&(alpr_ndfc->addr_wait)); |
Stefan Roese | 4963968 | 2006-08-15 14:22:35 +0200 | [diff] [blame] | 102 | |
| 103 | /* |
| 104 | * Return always true |
| 105 | */ |
| 106 | return 1; |
Stefan Roese | 4963968 | 2006-08-15 14:22:35 +0200 | [diff] [blame] | 107 | } |
| 108 | |
Stefan Roese | 1d9192f | 2007-01-06 15:56:13 +0100 | [diff] [blame] | 109 | int board_nand_init(struct nand_chip *nand) |
Stefan Roese | 4963968 | 2006-08-15 14:22:35 +0200 | [diff] [blame] | 110 | { |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 111 | alpr_ndfc = (struct alpr_ndfc_regs *)CONFIG_SYS_NAND_BASE; |
Stefan Roese | 4963968 | 2006-08-15 14:22:35 +0200 | [diff] [blame] | 112 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 113 | nand->ecc.mode = NAND_ECC_SOFT; |
Stefan Roese | 4963968 | 2006-08-15 14:22:35 +0200 | [diff] [blame] | 114 | |
Stefan Roese | 4963968 | 2006-08-15 14:22:35 +0200 | [diff] [blame] | 115 | /* Reference hardware control function */ |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 116 | nand->cmd_ctrl = alpr_nand_hwcontrol; |
Stefan Roese | a183188 | 2006-10-07 11:35:25 +0200 | [diff] [blame] | 117 | nand->read_byte = alpr_nand_read_byte; |
| 118 | nand->write_buf = alpr_nand_write_buf; |
| 119 | nand->read_buf = alpr_nand_read_buf; |
Stefan Roese | a183188 | 2006-10-07 11:35:25 +0200 | [diff] [blame] | 120 | nand->dev_ready = alpr_nand_dev_ready; |
Stefan Roese | 1d9192f | 2007-01-06 15:56:13 +0100 | [diff] [blame] | 121 | |
| 122 | return 0; |
Stefan Roese | 4963968 | 2006-08-15 14:22:35 +0200 | [diff] [blame] | 123 | } |
| 124 | #endif |