blob: f9352f95a6c67363daae45c80c7189fcc2773a75 [file] [log] [blame]
Chong Huangc71e6dc2010-11-30 03:33:25 -05001/*
2 * (C) Copyright 2010, ucRobotics Inc.
3 * Author: Chong Huang <chuang@ucrobotics.com>
4 * Licensed under the GPL-2 or later.
5 */
6
7#include <common.h>
8#include <malloc.h>
9#include <spi_flash.h>
10
11#include "spi_flash_internal.h"
12
13/* EN25Q128-specific commands */
14#define CMD_EN25Q128_WREN 0x06 /* Write Enable */
15#define CMD_EN25Q128_WRDI 0x04 /* Write Disable */
16#define CMD_EN25Q128_RDSR 0x05 /* Read Status Register */
17#define CMD_EN25Q128_WRSR 0x01 /* Write Status Register */
18#define CMD_EN25Q128_READ 0x03 /* Read Data Bytes */
19#define CMD_EN25Q128_FAST_READ 0x0b /* Read Data Bytes at Higher Speed */
20#define CMD_EN25Q128_PP 0x02 /* Page Program */
21#define CMD_EN25Q128_SE 0x20 /* Sector Erase */
22#define CMD_EN25Q128_BE 0xd8 /* Block Erase */
23#define CMD_EN25Q128_DP 0xb9 /* Deep Power-down */
24#define CMD_EN25Q128_RES 0xab /* Release from DP, and Read Signature */
25
Chong Huangc71e6dc2010-11-30 03:33:25 -050026struct eon_spi_flash_params {
27 u8 idcode1;
28 u16 page_size;
29 u16 pages_per_sector;
30 u16 sectors_per_block;
31 u16 nr_sectors;
32 const char *name;
33};
34
Chong Huangc71e6dc2010-11-30 03:33:25 -050035static const struct eon_spi_flash_params eon_spi_flash_table[] = {
36 {
Shaohui Xiefc9d8262011-09-27 19:21:34 +080037 .idcode1 = 0x16,
38 .page_size = 256,
39 .pages_per_sector = 16,
40 .sectors_per_block = 16,
41 .nr_sectors = 1024,
42 .name = "EN25Q32B",
43 },
44 {
Mike Frysingerfc9d1fc2011-04-23 23:05:58 +000045 .idcode1 = 0x18,
Chong Huangc71e6dc2010-11-30 03:33:25 -050046 .page_size = 256,
47 .pages_per_sector = 16,
48 .sectors_per_block = 16,
49 .nr_sectors = 4096,
50 .name = "EN25Q128",
51 },
Shaohui Xie2d0dd6e2011-09-27 01:21:34 +000052 {
53 .idcode1 = 0x16,
54 .page_size = 256,
55 .pages_per_sector = 16,
56 .sectors_per_block = 16,
57 .nr_sectors = 1024,
58 .name = "EN25Q32B",
59 },
Chong Huangc71e6dc2010-11-30 03:33:25 -050060};
61
Mike Frysingeraea4e172011-04-12 01:51:29 -040062static int eon_erase(struct spi_flash *flash, u32 offset, size_t len)
Chong Huangc71e6dc2010-11-30 03:33:25 -050063{
Richard Retanubunb0148dc2011-02-16 16:37:22 -050064 return spi_flash_cmd_erase(flash, CMD_EN25Q128_BE, offset, len);
Chong Huangc71e6dc2010-11-30 03:33:25 -050065}
66
67struct spi_flash *spi_flash_probe_eon(struct spi_slave *spi, u8 *idcode)
68{
69 const struct eon_spi_flash_params *params;
Mike Frysingerce7cc042011-06-28 07:38:10 +000070 struct spi_flash *flash;
Chong Huangc71e6dc2010-11-30 03:33:25 -050071 unsigned int i;
72
73 for (i = 0; i < ARRAY_SIZE(eon_spi_flash_table); ++i) {
74 params = &eon_spi_flash_table[i];
75 if (params->idcode1 == idcode[2])
76 break;
77 }
78
79 if (i == ARRAY_SIZE(eon_spi_flash_table)) {
80 debug("SF: Unsupported EON ID %02x\n", idcode[1]);
81 return NULL;
82 }
83
Mike Frysingerce7cc042011-06-28 07:38:10 +000084 flash = malloc(sizeof(*flash));
85 if (!flash) {
Chong Huangc71e6dc2010-11-30 03:33:25 -050086 debug("SF: Failed to allocate memory\n");
87 return NULL;
88 }
89
Mike Frysingerce7cc042011-06-28 07:38:10 +000090 flash->spi = spi;
91 flash->name = params->name;
Chong Huangc71e6dc2010-11-30 03:33:25 -050092
Mike Frysingerce7cc042011-06-28 07:38:10 +000093 flash->write = spi_flash_cmd_write_multi;
94 flash->erase = eon_erase;
95 flash->read = spi_flash_cmd_read_fast;
96 flash->page_size = params->page_size;
97 flash->sector_size = params->page_size * params->pages_per_sector
Richard Retanubunb0148dc2011-02-16 16:37:22 -050098 * params->sectors_per_block;
Mike Frysingerce7cc042011-06-28 07:38:10 +000099 flash->size = params->page_size * params->pages_per_sector
Chong Huangc71e6dc2010-11-30 03:33:25 -0500100 * params->nr_sectors;
101
Mike Frysingerce7cc042011-06-28 07:38:10 +0000102 return flash;
Chong Huangc71e6dc2010-11-30 03:33:25 -0500103}