blob: 894b1ca2b4ca63839b4d30331c1ed1ed41ca20f4 [file] [log] [blame]
Prafulla Wadaskar09569072009-04-06 21:24:43 +05301/*
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 Wadaskar09569072009-04-06 21:24:43 +053039#define CMD_MX25XX_SE 0x20 /* Sector Erase */
40#define CMD_MX25XX_BE 0xD8 /* Block Erase */
41#define CMD_MX25XX_CE 0xc7 /* Chip Erase */
Prafulla Wadaskar09569072009-04-06 21:24:43 +053042
Prafulla Wadaskar09569072009-04-06 21:24:43 +053043struct macronix_spi_flash_params {
Prafulla Wadaskar5af0c232009-07-06 20:29:15 +053044 u16 idcode;
Prafulla Wadaskar09569072009-04-06 21:24:43 +053045 u16 nr_blocks;
46 const char *name;
47};
48
Prafulla Wadaskar09569072009-04-06 21:24:43 +053049static const struct macronix_spi_flash_params macronix_spi_flash_table[] = {
50 {
Macpaul Lin479404e2011-04-20 16:51:39 +000051 .idcode = 0x2013,
Macpaul Lin479404e2011-04-20 16:51:39 +000052 .nr_blocks = 8,
53 .name = "MX25L4005",
54 },
55 {
56 .idcode = 0x2014,
Macpaul Lin479404e2011-04-20 16:51:39 +000057 .nr_blocks = 16,
58 .name = "MX25L8005",
59 },
60 {
Prafulla Wadaskar5af0c232009-07-06 20:29:15 +053061 .idcode = 0x2015,
Prafulla Wadaskar5af0c232009-07-06 20:29:15 +053062 .nr_blocks = 32,
63 .name = "MX25L1605D",
64 },
65 {
66 .idcode = 0x2016,
Prafulla Wadaskar5af0c232009-07-06 20:29:15 +053067 .nr_blocks = 64,
68 .name = "MX25L3205D",
69 },
70 {
71 .idcode = 0x2017,
Prafulla Wadaskar5af0c232009-07-06 20:29:15 +053072 .nr_blocks = 128,
73 .name = "MX25L6405D",
74 },
75 {
76 .idcode = 0x2018,
Prafulla Wadaskar09569072009-04-06 21:24:43 +053077 .nr_blocks = 256,
78 .name = "MX25L12805D",
79 },
Prafulla Wadaskar5af0c232009-07-06 20:29:15 +053080 {
81 .idcode = 0x2618,
Prafulla Wadaskar5af0c232009-07-06 20:29:15 +053082 .nr_blocks = 256,
83 .name = "MX25L12855E",
84 },
Prafulla Wadaskar09569072009-04-06 21:24:43 +053085};
86
Simon Guinotcfd72b62011-05-02 11:01:38 +000087static int macronix_write_status(struct spi_flash *flash, u8 sr)
88{
89 u8 cmd;
90 int ret;
91
92 ret = spi_flash_cmd_write_enable(flash);
93 if (ret < 0) {
94 debug("SF: enabling write failed\n");
95 return ret;
96 }
97
Mike Frysinger1302bec2012-01-28 16:26:03 -080098 cmd = CMD_WRITE_STATUS;
Simon Guinotcfd72b62011-05-02 11:01:38 +000099 ret = spi_flash_cmd_write(flash->spi, &cmd, 1, &sr, 1);
100 if (ret) {
101 debug("SF: fail to write status register\n");
102 return ret;
103 }
104
105 ret = spi_flash_cmd_wait_ready(flash, SPI_FLASH_PROG_TIMEOUT);
106 if (ret < 0) {
107 debug("SF: write status register timed out\n");
108 return ret;
109 }
110
111 return 0;
112}
113
114static int macronix_unlock(struct spi_flash *flash)
115{
116 int ret;
117
118 /* Enable status register writing and clear BP# bits */
119 ret = macronix_write_status(flash, 0);
120 if (ret)
121 debug("SF: fail to disable write protection\n");
122
123 return ret;
124}
125
Mike Frysingeraea4e172011-04-12 01:51:29 -0400126static int macronix_erase(struct spi_flash *flash, u32 offset, size_t len)
Prafulla Wadaskar09569072009-04-06 21:24:43 +0530127{
Richard Retanubunb0148dc2011-02-16 16:37:22 -0500128 return spi_flash_cmd_erase(flash, CMD_MX25XX_BE, offset, len);
Prafulla Wadaskar09569072009-04-06 21:24:43 +0530129}
130
131struct spi_flash *spi_flash_probe_macronix(struct spi_slave *spi, u8 *idcode)
132{
133 const struct macronix_spi_flash_params *params;
Mike Frysingerce7cc042011-06-28 07:38:10 +0000134 struct spi_flash *flash;
Prafulla Wadaskar09569072009-04-06 21:24:43 +0530135 unsigned int i;
Prafulla Wadaskar5af0c232009-07-06 20:29:15 +0530136 u16 id = idcode[2] | idcode[1] << 8;
Prafulla Wadaskar09569072009-04-06 21:24:43 +0530137
138 for (i = 0; i < ARRAY_SIZE(macronix_spi_flash_table); i++) {
139 params = &macronix_spi_flash_table[i];
Prafulla Wadaskar5af0c232009-07-06 20:29:15 +0530140 if (params->idcode == id)
Prafulla Wadaskar09569072009-04-06 21:24:43 +0530141 break;
142 }
143
144 if (i == ARRAY_SIZE(macronix_spi_flash_table)) {
Prafulla Wadaskar5af0c232009-07-06 20:29:15 +0530145 debug("SF: Unsupported Macronix ID %04x\n", id);
Prafulla Wadaskar09569072009-04-06 21:24:43 +0530146 return NULL;
147 }
148
Mike Frysingerce7cc042011-06-28 07:38:10 +0000149 flash = malloc(sizeof(*flash));
150 if (!flash) {
Prafulla Wadaskar09569072009-04-06 21:24:43 +0530151 debug("SF: Failed to allocate memory\n");
152 return NULL;
153 }
154
Mike Frysingerce7cc042011-06-28 07:38:10 +0000155 flash->spi = spi;
156 flash->name = params->name;
Prafulla Wadaskar09569072009-04-06 21:24:43 +0530157
Mike Frysingerce7cc042011-06-28 07:38:10 +0000158 flash->write = spi_flash_cmd_write_multi;
159 flash->erase = macronix_erase;
160 flash->read = spi_flash_cmd_read_fast;
Mike Frysingera4940782012-03-04 22:56:52 -0500161 flash->page_size = 256;
162 flash->sector_size = 256 * 16 * 16;
Mike Frysingerce7cc042011-06-28 07:38:10 +0000163 flash->size = flash->sector_size * params->nr_blocks;
Prafulla Wadaskar09569072009-04-06 21:24:43 +0530164
Simon Guinotcfd72b62011-05-02 11:01:38 +0000165 /* Clear BP# bits for read-only flash */
166 macronix_unlock(flash);
167
Mike Frysingerce7cc042011-06-28 07:38:10 +0000168 return flash;
Prafulla Wadaskar09569072009-04-06 21:24:43 +0530169}