blob: ea875dc812b4bcf34e41af00072ff3c8004207c0 [file] [log] [blame]
Haavard Skinnemoen2f5bfb72008-05-16 11:10:33 +02001/*
2 * SPI flash interface
3 *
4 * Copyright (C) 2008 Atmel Corporation
Mike Frysinger22f8b682009-10-09 17:12:44 -04005 * Licensed under the GPL-2 or later.
Haavard Skinnemoen2f5bfb72008-05-16 11:10:33 +02006 */
Mike Frysingerc5431ac2009-03-23 23:03:58 -04007
Haavard Skinnemoen2f5bfb72008-05-16 11:10:33 +02008#include <common.h>
9#include <malloc.h>
10#include <spi.h>
11#include <spi_flash.h>
12
13#include "spi_flash_internal.h"
14
15int spi_flash_cmd(struct spi_slave *spi, u8 cmd, void *response, size_t len)
16{
17 unsigned long flags = SPI_XFER_BEGIN;
18 int ret;
19
20 if (len == 0)
21 flags |= SPI_XFER_END;
22
23 ret = spi_xfer(spi, 8, &cmd, NULL, flags);
24 if (ret) {
25 debug("SF: Failed to send command %02x: %d\n", cmd, ret);
26 return ret;
27 }
28
29 if (len) {
30 ret = spi_xfer(spi, len * 8, NULL, response, SPI_XFER_END);
31 if (ret)
32 debug("SF: Failed to read response (%zu bytes): %d\n",
33 len, ret);
34 }
35
36 return ret;
37}
38
39int spi_flash_cmd_read(struct spi_slave *spi, const u8 *cmd,
40 size_t cmd_len, void *data, size_t data_len)
41{
42 unsigned long flags = SPI_XFER_BEGIN;
43 int ret;
44
45 if (data_len == 0)
46 flags |= SPI_XFER_END;
47
48 ret = spi_xfer(spi, cmd_len * 8, cmd, NULL, flags);
49 if (ret) {
50 debug("SF: Failed to send read command (%zu bytes): %d\n",
51 cmd_len, ret);
52 } else if (data_len != 0) {
53 ret = spi_xfer(spi, data_len * 8, NULL, data, SPI_XFER_END);
54 if (ret)
55 debug("SF: Failed to read %zu bytes of data: %d\n",
56 data_len, ret);
57 }
58
59 return ret;
60}
61
62int spi_flash_cmd_write(struct spi_slave *spi, const u8 *cmd, size_t cmd_len,
63 const void *data, size_t data_len)
64{
65 unsigned long flags = SPI_XFER_BEGIN;
66 int ret;
67
68 if (data_len == 0)
69 flags |= SPI_XFER_END;
70
71 ret = spi_xfer(spi, cmd_len * 8, cmd, NULL, flags);
72 if (ret) {
73 debug("SF: Failed to send read command (%zu bytes): %d\n",
74 cmd_len, ret);
75 } else if (data_len != 0) {
76 ret = spi_xfer(spi, data_len * 8, data, NULL, SPI_XFER_END);
77 if (ret)
78 debug("SF: Failed to read %zu bytes of data: %d\n",
79 data_len, ret);
80 }
81
82 return ret;
83}
84
85
86int spi_flash_read_common(struct spi_flash *flash, const u8 *cmd,
87 size_t cmd_len, void *data, size_t data_len)
88{
89 struct spi_slave *spi = flash->spi;
90 int ret;
91
92 spi_claim_bus(spi);
93 ret = spi_flash_cmd_read(spi, cmd, cmd_len, data, data_len);
94 spi_release_bus(spi);
95
96 return ret;
97}
98
99struct spi_flash *spi_flash_probe(unsigned int bus, unsigned int cs,
100 unsigned int max_hz, unsigned int spi_mode)
101{
102 struct spi_slave *spi;
103 struct spi_flash *flash;
104 int ret;
Mike Frysingerf145ce52009-03-28 06:41:09 -0400105 u8 idcode[5];
Haavard Skinnemoen2f5bfb72008-05-16 11:10:33 +0200106
107 spi = spi_setup_slave(bus, cs, max_hz, spi_mode);
108 if (!spi) {
Mike Frysinger02110b52010-04-29 00:35:12 -0400109 printf("SF: Failed to set up slave\n");
Haavard Skinnemoen2f5bfb72008-05-16 11:10:33 +0200110 return NULL;
111 }
112
113 ret = spi_claim_bus(spi);
114 if (ret) {
115 debug("SF: Failed to claim SPI bus: %d\n", ret);
116 goto err_claim_bus;
117 }
118
119 /* Read the ID codes */
120 ret = spi_flash_cmd(spi, CMD_READ_ID, &idcode, sizeof(idcode));
121 if (ret)
122 goto err_read_id;
123
Mike Frysingerf145ce52009-03-28 06:41:09 -0400124 debug("SF: Got idcode %02x %02x %02x %02x %02x\n", idcode[0],
125 idcode[1], idcode[2], idcode[3], idcode[4]);
Haavard Skinnemoen2f5bfb72008-05-16 11:10:33 +0200126
127 switch (idcode[0]) {
128#ifdef CONFIG_SPI_FLASH_SPANSION
129 case 0x01:
130 flash = spi_flash_probe_spansion(spi, idcode);
131 break;
132#endif
133#ifdef CONFIG_SPI_FLASH_ATMEL
134 case 0x1F:
135 flash = spi_flash_probe_atmel(spi, idcode);
136 break;
137#endif
Prafulla Wadaskar09569072009-04-06 21:24:43 +0530138#ifdef CONFIG_SPI_FLASH_MACRONIX
139 case 0xc2:
140 flash = spi_flash_probe_macronix(spi, idcode);
141 break;
142#endif
Jason McMullan64e5f3a2009-10-09 17:12:23 -0400143#ifdef CONFIG_SPI_FLASH_WINBOND
144 case 0xef:
145 flash = spi_flash_probe_winbond(spi, idcode);
146 break;
147#endif
TsiChung Liew8dd593a2008-08-06 16:08:41 -0500148#ifdef CONFIG_SPI_FLASH_STMICRO
149 case 0x20:
Thomas Chou51bc6bb2010-04-30 21:11:20 +0800150 case 0xff: /* Let the stmicro func handle non-JEDEC ids */
TsiChung Liew8dd593a2008-08-06 16:08:41 -0500151 flash = spi_flash_probe_stmicro(spi, idcode);
152 break;
153#endif
Mike Frysingerf1cb7c82009-03-27 19:27:58 -0400154#ifdef CONFIG_SPI_FLASH_SST
155 case 0xBF:
156 flash = spi_flash_probe_sst(spi, idcode);
157 break;
158#endif
Haavard Skinnemoen2f5bfb72008-05-16 11:10:33 +0200159 default:
Mike Frysinger02110b52010-04-29 00:35:12 -0400160 printf("SF: Unsupported manufacturer %02X\n", idcode[0]);
Haavard Skinnemoen2f5bfb72008-05-16 11:10:33 +0200161 flash = NULL;
162 break;
163 }
164
165 if (!flash)
166 goto err_manufacturer_probe;
167
168 spi_release_bus(spi);
169
170 return flash;
171
172err_manufacturer_probe:
173err_read_id:
174 spi_release_bus(spi);
175err_claim_bus:
176 spi_free_slave(spi);
177 return NULL;
178}
179
180void spi_flash_free(struct spi_flash *flash)
181{
182 spi_free_slave(flash->spi);
183 free(flash);
184}