blob: 5268c661ea301fd9380a1f15390932dca170cda8 [file] [log] [blame]
Prafulla Wadaskar7ce60312009-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
Prafulla Wadaskar7ce60312009-04-06 21:24:43 +053038struct macronix_spi_flash_params {
Prafulla Wadaskar2efee522009-07-06 20:29:15 +053039 u16 idcode;
Prafulla Wadaskar7ce60312009-04-06 21:24:43 +053040 u16 nr_blocks;
41 const char *name;
42};
43
Prafulla Wadaskar7ce60312009-04-06 21:24:43 +053044static const struct macronix_spi_flash_params macronix_spi_flash_table[] = {
45 {
Macpaul Lin2d722e02011-04-20 16:51:39 +000046 .idcode = 0x2013,
Macpaul Lin2d722e02011-04-20 16:51:39 +000047 .nr_blocks = 8,
48 .name = "MX25L4005",
49 },
50 {
51 .idcode = 0x2014,
Macpaul Lin2d722e02011-04-20 16:51:39 +000052 .nr_blocks = 16,
53 .name = "MX25L8005",
54 },
55 {
Prafulla Wadaskar2efee522009-07-06 20:29:15 +053056 .idcode = 0x2015,
Prafulla Wadaskar2efee522009-07-06 20:29:15 +053057 .nr_blocks = 32,
58 .name = "MX25L1605D",
59 },
60 {
61 .idcode = 0x2016,
Prafulla Wadaskar2efee522009-07-06 20:29:15 +053062 .nr_blocks = 64,
63 .name = "MX25L3205D",
64 },
65 {
66 .idcode = 0x2017,
Prafulla Wadaskar2efee522009-07-06 20:29:15 +053067 .nr_blocks = 128,
68 .name = "MX25L6405D",
69 },
70 {
71 .idcode = 0x2018,
Prafulla Wadaskar7ce60312009-04-06 21:24:43 +053072 .nr_blocks = 256,
73 .name = "MX25L12805D",
74 },
Prafulla Wadaskar2efee522009-07-06 20:29:15 +053075 {
76 .idcode = 0x2618,
Prafulla Wadaskar2efee522009-07-06 20:29:15 +053077 .nr_blocks = 256,
78 .name = "MX25L12855E",
79 },
Prafulla Wadaskar7ce60312009-04-06 21:24:43 +053080};
81
Simon Guinot7432ed02011-05-02 11:01:38 +000082static int macronix_write_status(struct spi_flash *flash, u8 sr)
83{
84 u8 cmd;
85 int ret;
86
87 ret = spi_flash_cmd_write_enable(flash);
88 if (ret < 0) {
89 debug("SF: enabling write failed\n");
90 return ret;
91 }
92
Mike Frysingerb4c87d62012-01-28 16:26:03 -080093 cmd = CMD_WRITE_STATUS;
Simon Guinot7432ed02011-05-02 11:01:38 +000094 ret = spi_flash_cmd_write(flash->spi, &cmd, 1, &sr, 1);
95 if (ret) {
96 debug("SF: fail to write status register\n");
97 return ret;
98 }
99
100 ret = spi_flash_cmd_wait_ready(flash, SPI_FLASH_PROG_TIMEOUT);
101 if (ret < 0) {
102 debug("SF: write status register timed out\n");
103 return ret;
104 }
105
106 return 0;
107}
108
109static int macronix_unlock(struct spi_flash *flash)
110{
111 int ret;
112
113 /* Enable status register writing and clear BP# bits */
114 ret = macronix_write_status(flash, 0);
115 if (ret)
116 debug("SF: fail to disable write protection\n");
117
118 return ret;
119}
120
Prafulla Wadaskar7ce60312009-04-06 21:24:43 +0530121struct spi_flash *spi_flash_probe_macronix(struct spi_slave *spi, u8 *idcode)
122{
123 const struct macronix_spi_flash_params *params;
Mike Frysingerb06afa72011-06-28 07:38:10 +0000124 struct spi_flash *flash;
Prafulla Wadaskar7ce60312009-04-06 21:24:43 +0530125 unsigned int i;
Prafulla Wadaskar2efee522009-07-06 20:29:15 +0530126 u16 id = idcode[2] | idcode[1] << 8;
Prafulla Wadaskar7ce60312009-04-06 21:24:43 +0530127
128 for (i = 0; i < ARRAY_SIZE(macronix_spi_flash_table); i++) {
129 params = &macronix_spi_flash_table[i];
Prafulla Wadaskar2efee522009-07-06 20:29:15 +0530130 if (params->idcode == id)
Prafulla Wadaskar7ce60312009-04-06 21:24:43 +0530131 break;
132 }
133
134 if (i == ARRAY_SIZE(macronix_spi_flash_table)) {
Prafulla Wadaskar2efee522009-07-06 20:29:15 +0530135 debug("SF: Unsupported Macronix ID %04x\n", id);
Prafulla Wadaskar7ce60312009-04-06 21:24:43 +0530136 return NULL;
137 }
138
Mike Frysingerb06afa72011-06-28 07:38:10 +0000139 flash = malloc(sizeof(*flash));
140 if (!flash) {
Prafulla Wadaskar7ce60312009-04-06 21:24:43 +0530141 debug("SF: Failed to allocate memory\n");
142 return NULL;
143 }
144
Mike Frysingerb06afa72011-06-28 07:38:10 +0000145 flash->spi = spi;
146 flash->name = params->name;
Prafulla Wadaskar7ce60312009-04-06 21:24:43 +0530147
Mike Frysingerb06afa72011-06-28 07:38:10 +0000148 flash->write = spi_flash_cmd_write_multi;
Mike Frysingerc4e932c2012-03-04 22:35:50 -0500149 flash->erase = spi_flash_cmd_erase;
Mike Frysingerb06afa72011-06-28 07:38:10 +0000150 flash->read = spi_flash_cmd_read_fast;
Mike Frysingera4ed3b62012-03-04 22:56:52 -0500151 flash->page_size = 256;
152 flash->sector_size = 256 * 16 * 16;
Mike Frysingerb06afa72011-06-28 07:38:10 +0000153 flash->size = flash->sector_size * params->nr_blocks;
Prafulla Wadaskar7ce60312009-04-06 21:24:43 +0530154
Simon Guinot7432ed02011-05-02 11:01:38 +0000155 /* Clear BP# bits for read-only flash */
156 macronix_unlock(flash);
157
Mike Frysingerb06afa72011-06-28 07:38:10 +0000158 return flash;
Prafulla Wadaskar7ce60312009-04-06 21:24:43 +0530159}