2 * (C) Copyright 2008-2010 Freescale Semiconductor, Inc.
4 * See file CREDITS for list of people who contributed to this
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation; either version 2 of
10 * the License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
26 #include <spi_flash.h>
27 #include <asm/errno.h>
28 #include <linux/types.h>
32 #include <imx_spi_nor.h>
34 static u8 g_tx_buf[256];
35 static u8 g_rx_buf[256];
37 #define WRITE_ENABLE(a) spi_nor_cmd_1byte(a, WREN)
38 #define ENABLE_WRITE_STATUS(a) spi_nor_cmd_1byte(a, EWSR)
40 struct imx_spi_flash_params {
48 struct imx_spi_flash {
49 const struct imx_spi_flash_params *params;
50 struct spi_flash flash;
53 static inline struct imx_spi_flash *
54 to_imx_spi_flash(struct spi_flash *flash)
56 return container_of(flash, struct imx_spi_flash, flash);
59 static const struct imx_spi_flash_params imx_spi_flash_table[] = {
64 .device_size = SZ_64K * 64,
65 .name = "AT45DB321D - 4MB",
69 static s32 spi_nor_flash_query(struct spi_flash *flash, void* data)
72 u8 *pData = (u8 *)data;
74 g_tx_buf[3] = JEDEC_ID;
76 if (spi_xfer(flash->spi, (4 << 3), g_tx_buf, au8Tmp,
77 SPI_XFER_BEGIN | SPI_XFER_END)) {
81 printf("JEDEC ID: 0x%02x:0x%02x:0x%02x\n",
82 au8Tmp[2], au8Tmp[1], au8Tmp[0]);
91 static s32 spi_nor_status(struct spi_flash *flash)
93 g_tx_buf[1] = STAT_READ;
95 if (spi_xfer(flash->spi, 2 << 3, g_tx_buf, g_rx_buf,
96 SPI_XFER_BEGIN | SPI_XFER_END) != 0) {
97 printf("Error: %s(): %d\n", __func__, __LINE__);
105 * Erase a block_size data from block_addr offset in the flash
107 static int spi_nor_erase_page(struct spi_flash *flash,
110 u32 addr = (u32)page_addr;
112 if ((addr & 512) != 0) {
113 printf("Error - page_addr is not "
114 "512 Bytes aligned: %p\n",
119 /* now do the block erase */
120 if (spi_xfer(flash->spi, 4 << 3, g_tx_buf, g_rx_buf,
121 SPI_XFER_BEGIN | SPI_XFER_END) != 0) {
125 while (spi_nor_status(flash) & RDSR_BUSY)
132 static int spi_nor_flash_read(struct spi_flash *flash, u32 offset,
133 size_t len, void *buf)
135 struct imx_spi_flash *imx_sf = to_imx_spi_flash(flash);
136 u32 *cmd = (u32 *)g_tx_buf;
137 u32 max_rx_sz = (MAX_SPI_BYTES) - 4;
138 u8 *d_buf = (u8 *)buf;
140 s32 s32remain_size = len;
146 printf("Reading SPI NOR flash 0x%x [0x%x bytes] -> ram 0x%p\n",
148 debug("%s(from flash=0x%08x to ram=%p len=0x%x)\n",
155 *cmd = (READ << 24) | ((u32)offset & 0x00FFFFFF);
157 for (; s32remain_size > 0;
158 s32remain_size -= max_rx_sz, *cmd += max_rx_sz) {
159 debug("Addr:0x%p=>Offset:0x%08x, %d bytes transferred\n",
162 (len - s32remain_size));
163 debug("%d%% completed\n", ((len - s32remain_size) * 100 / len));
165 if (s32remain_size < max_rx_sz) {
166 debug("100%% completed\n");
168 if (spi_xfer(flash->spi, (s32remain_size + 4) << 3,
170 SPI_XFER_BEGIN | SPI_XFER_END) != 0) {
171 printf("Error: %s(%d): failed\n",
175 /* throw away 4 bytes (5th received bytes is real) */
176 s_buf = g_rx_buf + 4;
178 /* now adjust the endianness */
179 for (i = s32remain_size; i >= 0; i -= 4, s_buf += 4) {
191 printf("SUCCESS\n\n");
202 /* now grab max_rx_sz data (+4 is
203 *needed due to 4-throw away bytes */
204 if (spi_xfer(flash->spi, (max_rx_sz + 4) << 3,
206 SPI_XFER_BEGIN | SPI_XFER_END) != 0) {
207 printf("Error: %s(%d): failed\n", __FILE__, __LINE__);
210 /* throw away 4 bytes (5th received bytes is real) */
211 s_buf = g_rx_buf + 4;
212 /* now adjust the endianness */
213 for (i = 0; i < max_rx_sz; i += 4, s_buf += 4) {
220 if ((s32remain_size % imx_sf->params->block_size) == 0)
223 printf("SUCCESS\n\n");
228 static int spi_nor_flash_write(struct spi_flash *flash, u32 offset,
229 size_t len, const void *buf)
232 u8 *s_buf = (u8 *)buf;
233 unsigned int final_addr = 0;
234 int page_size = 528, trans_bytes = 0, buf_ptr = 0,
235 bytes_sent = 0, byte_sent_per_iter = 0;
236 int page_no = 0, buf_addr = 0, page_off = 0,
237 i = 0, j = 0, k = 0, fifo_size = 32;
246 printf("Writing SPI NOR flash 0x%x [0x%x bytes] <- ram 0x%p\n",
248 debug("%s(flash addr=0x%08x, ram=%p, len=0x%x)\n",
249 __func__, offset, buf, len);
251 /* Read the status register to get the Page size */
252 if (spi_nor_status(flash) & STAT_PG_SZ) {
255 puts("Unsupported Page Size of 528 bytes\n");
256 g_tx_buf[0] = CONFIG_REG4;
257 g_tx_buf[1] = CONFIG_REG3;
258 g_tx_buf[2] = CONFIG_REG2;
259 g_tx_buf[3] = CONFIG_REG1;
261 if (spi_xfer(flash->spi, 4 << 3, g_tx_buf, g_rx_buf,
262 SPI_XFER_BEGIN | SPI_XFER_END) != 0) {
263 printf("Error: %s(): %d", __func__, __LINE__);
267 while (!(spi_nor_status(flash) & STAT_BUSY))
270 puts("Reprogrammed the Page Size to 512 bytes\n");
271 puts("Please Power Cycle the board for the change to take effect\n");
276 /* Due to the way CSPI operates send data less
277 that 4 bytes in a different manner */
278 remain_len = len % 4;
283 page_no = d_addr / page_size;
284 /* Get the offset within the page
285 if address is not page-aligned */
286 page_off = (d_addr % page_size);
293 trans_bytes = page_size - buf_addr;
296 trans_bytes = page_size;
302 if (trans_bytes > len)
305 bytes_sent = trans_bytes;
306 /* Write the data to the SPI-NOR Buffer first */
307 while (trans_bytes > 0) {
308 final_addr = (buf_addr & 0x3FF);
309 g_tx_buf[0] = final_addr;
310 g_tx_buf[1] = final_addr >> 8;
311 g_tx_buf[2] = final_addr >> 16;
312 g_tx_buf[3] = BUF1_WR; /*Opcode */
314 /* 4 bytes already used for Opcode & address bytes,
315 check to ensure we do not overflow the SPI TX buffer */
316 if (trans_bytes > (fifo_size - 4))
317 byte_sent_per_iter = fifo_size;
319 byte_sent_per_iter = trans_bytes + 4;
321 for (i = 4; i < byte_sent_per_iter; i += 4) {
322 g_tx_buf[i + 3] = s_buf[buf_ptr++];
323 g_tx_buf[i + 2] = s_buf[buf_ptr++];
324 g_tx_buf[i + 1] = s_buf[buf_ptr++];
325 g_tx_buf[i] = s_buf[buf_ptr++];
328 if (spi_xfer(flash->spi, byte_sent_per_iter << 3,
330 SPI_XFER_BEGIN | SPI_XFER_END) != 0) {
331 printf("Error: %s(%d): failed\n",
336 while (!(spi_nor_status(flash) & STAT_BUSY))
339 /* Deduct 4 bytes as it is used for Opcode & address bytes */
340 trans_bytes -= (byte_sent_per_iter - 4);
341 /* Update the destination buffer address */
342 buf_addr += (byte_sent_per_iter - 4);
345 /* Send the command to write data from the SPI-NOR Buffer to Flash memory */
346 final_addr = (page_size == 512) ? ((page_no & 0x1FFF) << 9) : \
347 ((page_no & 0x1FFF) << 10);
349 /* Specify the Page address in Flash where the data should be written to */
350 g_tx_buf[0] = final_addr;
351 g_tx_buf[1] = final_addr >> 8;
352 g_tx_buf[2] = final_addr >> 16;
353 g_tx_buf[3] = BUF1_TO_MEM; /*Opcode */
354 if (spi_xfer(flash->spi, 4 << 3, g_tx_buf, g_rx_buf,
355 SPI_XFER_BEGIN | SPI_XFER_END) != 0) {
356 printf("Error: %s(%d): failed\n", __FILE__, __LINE__);
360 while (!(spi_nor_status(flash) & STAT_BUSY))
363 d_addr += bytes_sent;
365 if (d_addr % (page_size * 50) == 0)
370 buf_ptr += remain_len;
371 /* Write the remaining data bytes first */
372 for (i = 0; i < remain_len; ++i)
373 g_tx_buf[i] = s_buf[buf_ptr--];
375 /* Write the address bytes next in the same word
376 as the data byte from the next byte */
377 for (j = i, k = 0; j < 4; j++, k++)
378 g_tx_buf[j] = final_addr >> (k * 8);
380 /* Write the remaining address bytes in the next word */
382 final_addr = (buf_addr & 0x3FF);
384 for (j = 0; k < 3; j++, k++)
385 g_tx_buf[j] = final_addr >> (k * 8);
387 /* Finally the Opcode to write the data to the buffer */
388 g_tx_buf[j] = BUF1_WR; /*Opcode */
390 if (spi_xfer(flash->spi, (remain_len + 4) << 3,
392 SPI_XFER_BEGIN | SPI_XFER_END) != 0) {
393 printf("Error: %s(%d): failed\n", __FILE__, __LINE__);
397 while (!(spi_nor_status(flash) & STAT_BUSY))
400 if (page_size == 512)
401 final_addr = (page_no & 0x1FFF) << 9;
403 final_addr = (page_no & 0x1FFF) << 10;
405 g_tx_buf[0] = final_addr;
406 g_tx_buf[1] = final_addr >> 8;
407 g_tx_buf[2] = final_addr >> 16;
408 g_tx_buf[3] = BUF1_TO_MEM; /*Opcode */
409 if (spi_xfer(flash->spi, 4 << 3, g_tx_buf, g_rx_buf,
410 SPI_XFER_BEGIN | SPI_XFER_END) != 0) {
411 printf("Error: %s(%d): failed\n", __FILE__, __LINE__);
415 while (!(spi_nor_status(flash) & STAT_BUSY))
419 printf("SUCCESS\n\n");
424 static int spi_nor_flash_erase(struct spi_flash *flash, u32 offset,
427 printf("Erase is built in program.\n");
432 struct spi_flash *spi_flash_probe(unsigned int bus, unsigned int cs, unsigned int max_hz, unsigned int spi_mode)
434 struct spi_slave *spi = NULL;
435 const struct imx_spi_flash_params *params = NULL;
436 struct imx_spi_flash *imx_sf = NULL;
437 u8 idcode[4] = { 0 };
441 if (CONFIG_SPI_FLASH_CS != cs) {
442 printf("Invalid cs for SPI NOR.\n");
446 spi = spi_setup_slave(bus, cs, max_hz, spi_mode);
449 debug("SF: Failed to set up slave\n");
453 ret = spi_claim_bus(spi);
455 debug("SF: Failed to claim SPI bus: %d\n", ret);
459 imx_sf = (struct imx_spi_flash *)malloc(sizeof(struct imx_spi_flash));
462 debug("SF: Failed to allocate memory\n");
467 imx_sf->flash.spi = spi;
469 /* Read the ID codes */
470 ret = spi_nor_flash_query(&(imx_sf->flash), idcode);
474 for (i = 0; i < ARRAY_SIZE(imx_spi_flash_table); ++i) {
475 params = &imx_spi_flash_table[i];
476 if (params->idcode1 == idcode[1])
480 if (i == ARRAY_SIZE(imx_spi_flash_table)) {
481 debug("SF: Unsupported DataFlash ID %02x\n",
484 goto err_invalid_dev;
487 imx_sf->params = params;
489 imx_sf->flash.name = params->name;
490 imx_sf->flash.size = params->device_size;
492 imx_sf->flash.read = spi_nor_flash_read;
493 imx_sf->flash.write = spi_nor_flash_write;
494 imx_sf->flash.erase = spi_nor_flash_erase;
496 debug("SF: Detected %s with block size %lu, "
497 "block count %lu, total %u bytes\n",
501 params->device_size);
503 return &(imx_sf->flash);
506 spi_release_bus(spi);
516 void spi_flash_free(struct spi_flash *flash)
518 struct imx_spi_flash *imx_sf = NULL;
523 imx_sf = to_imx_spi_flash(flash);
526 spi_free_slave(flash->spi);