Prafulla Wadaskar | 7ce6031 | 2009-04-06 21:24:43 +0530 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2009(C) Marvell International Ltd. and its affiliates |
| 3 | * Prafulla Wadaskar <prafulla@marvell.com> |
| 4 | * |
| 5 | * Based on drivers/mtd/spi/stmicro.c |
| 6 | * |
| 7 | * Copyright 2008, Network Appliance Inc. |
| 8 | * Jason McMullan <mcmullan@netapp.com> |
| 9 | * |
| 10 | * Copyright (C) 2004-2007 Freescale Semiconductor, Inc. |
| 11 | * TsiChung Liew (Tsi-Chung.Liew@freescale.com) |
| 12 | * |
| 13 | * See file CREDITS for list of people who contributed to this |
| 14 | * project. |
| 15 | * |
| 16 | * This program is free software; you can redistribute it and/or |
| 17 | * modify it under the terms of the GNU General Public License as |
| 18 | * published by the Free Software Foundation; either version 2 of |
| 19 | * the License, or (at your option) any later version. |
| 20 | * |
| 21 | * This program is distributed in the hope that it will be useful, |
| 22 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 23 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 24 | * GNU General Public License for more details. |
| 25 | * |
| 26 | * You should have received a copy of the GNU General Public License |
| 27 | * along with this program; if not, write to the Free Software |
| 28 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
| 29 | * MA 02110-1301 USA |
| 30 | */ |
| 31 | |
| 32 | #include <common.h> |
| 33 | #include <malloc.h> |
| 34 | #include <spi_flash.h> |
| 35 | |
| 36 | #include "spi_flash_internal.h" |
| 37 | |
| 38 | /* MX25xx-specific commands */ |
Prafulla Wadaskar | 7ce6031 | 2009-04-06 21:24:43 +0530 | [diff] [blame] | 39 | #define CMD_MX25XX_SE 0x20 /* Sector Erase */ |
| 40 | #define CMD_MX25XX_BE 0xD8 /* Block Erase */ |
| 41 | #define CMD_MX25XX_CE 0xc7 /* Chip Erase */ |
Prafulla Wadaskar | 7ce6031 | 2009-04-06 21:24:43 +0530 | [diff] [blame] | 42 | |
Prafulla Wadaskar | 7ce6031 | 2009-04-06 21:24:43 +0530 | [diff] [blame] | 43 | struct macronix_spi_flash_params { |
Prafulla Wadaskar | 2efee52 | 2009-07-06 20:29:15 +0530 | [diff] [blame] | 44 | u16 idcode; |
Prafulla Wadaskar | 7ce6031 | 2009-04-06 21:24:43 +0530 | [diff] [blame] | 45 | u16 page_size; |
| 46 | u16 pages_per_sector; |
| 47 | u16 sectors_per_block; |
| 48 | u16 nr_blocks; |
| 49 | const char *name; |
| 50 | }; |
| 51 | |
Prafulla Wadaskar | 7ce6031 | 2009-04-06 21:24:43 +0530 | [diff] [blame] | 52 | static const struct macronix_spi_flash_params macronix_spi_flash_table[] = { |
| 53 | { |
Macpaul Lin | 2d722e0 | 2011-04-20 16:51:39 +0000 | [diff] [blame] | 54 | .idcode = 0x2013, |
| 55 | .page_size = 256, |
| 56 | .pages_per_sector = 16, |
| 57 | .sectors_per_block = 16, |
| 58 | .nr_blocks = 8, |
| 59 | .name = "MX25L4005", |
| 60 | }, |
| 61 | { |
| 62 | .idcode = 0x2014, |
| 63 | .page_size = 256, |
| 64 | .pages_per_sector = 16, |
| 65 | .sectors_per_block = 16, |
| 66 | .nr_blocks = 16, |
| 67 | .name = "MX25L8005", |
| 68 | }, |
| 69 | { |
Prafulla Wadaskar | 2efee52 | 2009-07-06 20:29:15 +0530 | [diff] [blame] | 70 | .idcode = 0x2015, |
| 71 | .page_size = 256, |
| 72 | .pages_per_sector = 16, |
| 73 | .sectors_per_block = 16, |
| 74 | .nr_blocks = 32, |
| 75 | .name = "MX25L1605D", |
| 76 | }, |
| 77 | { |
| 78 | .idcode = 0x2016, |
| 79 | .page_size = 256, |
| 80 | .pages_per_sector = 16, |
| 81 | .sectors_per_block = 16, |
| 82 | .nr_blocks = 64, |
| 83 | .name = "MX25L3205D", |
| 84 | }, |
| 85 | { |
| 86 | .idcode = 0x2017, |
| 87 | .page_size = 256, |
| 88 | .pages_per_sector = 16, |
| 89 | .sectors_per_block = 16, |
| 90 | .nr_blocks = 128, |
| 91 | .name = "MX25L6405D", |
| 92 | }, |
| 93 | { |
| 94 | .idcode = 0x2018, |
Prafulla Wadaskar | 7ce6031 | 2009-04-06 21:24:43 +0530 | [diff] [blame] | 95 | .page_size = 256, |
| 96 | .pages_per_sector = 16, |
| 97 | .sectors_per_block = 16, |
| 98 | .nr_blocks = 256, |
| 99 | .name = "MX25L12805D", |
| 100 | }, |
Prafulla Wadaskar | 2efee52 | 2009-07-06 20:29:15 +0530 | [diff] [blame] | 101 | { |
| 102 | .idcode = 0x2618, |
| 103 | .page_size = 256, |
| 104 | .pages_per_sector = 16, |
| 105 | .sectors_per_block = 16, |
| 106 | .nr_blocks = 256, |
| 107 | .name = "MX25L12855E", |
| 108 | }, |
Prafulla Wadaskar | 7ce6031 | 2009-04-06 21:24:43 +0530 | [diff] [blame] | 109 | }; |
| 110 | |
Simon Guinot | 7432ed0 | 2011-05-02 11:01:38 +0000 | [diff] [blame] | 111 | static int macronix_write_status(struct spi_flash *flash, u8 sr) |
| 112 | { |
| 113 | u8 cmd; |
| 114 | int ret; |
| 115 | |
| 116 | ret = spi_flash_cmd_write_enable(flash); |
| 117 | if (ret < 0) { |
| 118 | debug("SF: enabling write failed\n"); |
| 119 | return ret; |
| 120 | } |
| 121 | |
Mike Frysinger | b4c87d6 | 2012-01-28 16:26:03 -0800 | [diff] [blame] | 122 | cmd = CMD_WRITE_STATUS; |
Simon Guinot | 7432ed0 | 2011-05-02 11:01:38 +0000 | [diff] [blame] | 123 | ret = spi_flash_cmd_write(flash->spi, &cmd, 1, &sr, 1); |
| 124 | if (ret) { |
| 125 | debug("SF: fail to write status register\n"); |
| 126 | return ret; |
| 127 | } |
| 128 | |
| 129 | ret = spi_flash_cmd_wait_ready(flash, SPI_FLASH_PROG_TIMEOUT); |
| 130 | if (ret < 0) { |
| 131 | debug("SF: write status register timed out\n"); |
| 132 | return ret; |
| 133 | } |
| 134 | |
| 135 | return 0; |
| 136 | } |
| 137 | |
| 138 | static int macronix_unlock(struct spi_flash *flash) |
| 139 | { |
| 140 | int ret; |
| 141 | |
| 142 | /* Enable status register writing and clear BP# bits */ |
| 143 | ret = macronix_write_status(flash, 0); |
| 144 | if (ret) |
| 145 | debug("SF: fail to disable write protection\n"); |
| 146 | |
| 147 | return ret; |
| 148 | } |
| 149 | |
Mike Frysinger | f8f0757 | 2011-04-12 01:51:29 -0400 | [diff] [blame] | 150 | static int macronix_erase(struct spi_flash *flash, u32 offset, size_t len) |
Prafulla Wadaskar | 7ce6031 | 2009-04-06 21:24:43 +0530 | [diff] [blame] | 151 | { |
Richard Retanubun | 4e6a515 | 2011-02-16 16:37:22 -0500 | [diff] [blame] | 152 | return spi_flash_cmd_erase(flash, CMD_MX25XX_BE, offset, len); |
Prafulla Wadaskar | 7ce6031 | 2009-04-06 21:24:43 +0530 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | struct spi_flash *spi_flash_probe_macronix(struct spi_slave *spi, u8 *idcode) |
| 156 | { |
| 157 | const struct macronix_spi_flash_params *params; |
Mike Frysinger | b06afa7 | 2011-06-28 07:38:10 +0000 | [diff] [blame] | 158 | struct spi_flash *flash; |
Prafulla Wadaskar | 7ce6031 | 2009-04-06 21:24:43 +0530 | [diff] [blame] | 159 | unsigned int i; |
Prafulla Wadaskar | 2efee52 | 2009-07-06 20:29:15 +0530 | [diff] [blame] | 160 | u16 id = idcode[2] | idcode[1] << 8; |
Prafulla Wadaskar | 7ce6031 | 2009-04-06 21:24:43 +0530 | [diff] [blame] | 161 | |
| 162 | for (i = 0; i < ARRAY_SIZE(macronix_spi_flash_table); i++) { |
| 163 | params = ¯onix_spi_flash_table[i]; |
Prafulla Wadaskar | 2efee52 | 2009-07-06 20:29:15 +0530 | [diff] [blame] | 164 | if (params->idcode == id) |
Prafulla Wadaskar | 7ce6031 | 2009-04-06 21:24:43 +0530 | [diff] [blame] | 165 | break; |
| 166 | } |
| 167 | |
| 168 | if (i == ARRAY_SIZE(macronix_spi_flash_table)) { |
Prafulla Wadaskar | 2efee52 | 2009-07-06 20:29:15 +0530 | [diff] [blame] | 169 | debug("SF: Unsupported Macronix ID %04x\n", id); |
Prafulla Wadaskar | 7ce6031 | 2009-04-06 21:24:43 +0530 | [diff] [blame] | 170 | return NULL; |
| 171 | } |
| 172 | |
Mike Frysinger | b06afa7 | 2011-06-28 07:38:10 +0000 | [diff] [blame] | 173 | flash = malloc(sizeof(*flash)); |
| 174 | if (!flash) { |
Prafulla Wadaskar | 7ce6031 | 2009-04-06 21:24:43 +0530 | [diff] [blame] | 175 | debug("SF: Failed to allocate memory\n"); |
| 176 | return NULL; |
| 177 | } |
| 178 | |
Mike Frysinger | b06afa7 | 2011-06-28 07:38:10 +0000 | [diff] [blame] | 179 | flash->spi = spi; |
| 180 | flash->name = params->name; |
Prafulla Wadaskar | 7ce6031 | 2009-04-06 21:24:43 +0530 | [diff] [blame] | 181 | |
Mike Frysinger | b06afa7 | 2011-06-28 07:38:10 +0000 | [diff] [blame] | 182 | flash->write = spi_flash_cmd_write_multi; |
| 183 | flash->erase = macronix_erase; |
| 184 | flash->read = spi_flash_cmd_read_fast; |
| 185 | flash->page_size = params->page_size; |
| 186 | flash->sector_size = params->page_size * params->pages_per_sector |
Richard Retanubun | 4e6a515 | 2011-02-16 16:37:22 -0500 | [diff] [blame] | 187 | * params->sectors_per_block; |
Mike Frysinger | b06afa7 | 2011-06-28 07:38:10 +0000 | [diff] [blame] | 188 | flash->size = flash->sector_size * params->nr_blocks; |
Prafulla Wadaskar | 7ce6031 | 2009-04-06 21:24:43 +0530 | [diff] [blame] | 189 | |
Simon Guinot | 7432ed0 | 2011-05-02 11:01:38 +0000 | [diff] [blame] | 190 | /* Clear BP# bits for read-only flash */ |
| 191 | macronix_unlock(flash); |
| 192 | |
Mike Frysinger | b06afa7 | 2011-06-28 07:38:10 +0000 | [diff] [blame] | 193 | return flash; |
Prafulla Wadaskar | 7ce6031 | 2009-04-06 21:24:43 +0530 | [diff] [blame] | 194 | } |