blob: 036855b197ebbabd52646191b5233cf51e63f230 [file] [log] [blame]
Chong Huangd1d906562010-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
26#define EON_ID_EN25Q128 0x18
27
Chong Huangd1d906562010-11-30 03:33:25 -050028struct eon_spi_flash_params {
29 u8 idcode1;
30 u16 page_size;
31 u16 pages_per_sector;
32 u16 sectors_per_block;
33 u16 nr_sectors;
34 const char *name;
35};
36
37/* spi_flash needs to be first so upper layers can free() it */
38struct eon_spi_flash {
39 struct spi_flash flash;
40 const struct eon_spi_flash_params *params;
41};
42
43static inline struct eon_spi_flash *to_eon_spi_flash(struct spi_flash *flash)
44{
45 return container_of(flash, struct eon_spi_flash, flash);
46}
47
48static const struct eon_spi_flash_params eon_spi_flash_table[] = {
49 {
50 .idcode1 = EON_ID_EN25Q128,
51 .page_size = 256,
52 .pages_per_sector = 16,
53 .sectors_per_block = 16,
54 .nr_sectors = 4096,
55 .name = "EN25Q128",
56 },
57};
58
Mike Frysingerf8f07572011-04-12 01:51:29 -040059static int eon_erase(struct spi_flash *flash, u32 offset, size_t len)
Chong Huangd1d906562010-11-30 03:33:25 -050060{
Richard Retanubun4e6a5152011-02-16 16:37:22 -050061 return spi_flash_cmd_erase(flash, CMD_EN25Q128_BE, offset, len);
Chong Huangd1d906562010-11-30 03:33:25 -050062}
63
64struct spi_flash *spi_flash_probe_eon(struct spi_slave *spi, u8 *idcode)
65{
66 const struct eon_spi_flash_params *params;
67 struct eon_spi_flash *eon;
68 unsigned int i;
69
70 for (i = 0; i < ARRAY_SIZE(eon_spi_flash_table); ++i) {
71 params = &eon_spi_flash_table[i];
72 if (params->idcode1 == idcode[2])
73 break;
74 }
75
76 if (i == ARRAY_SIZE(eon_spi_flash_table)) {
77 debug("SF: Unsupported EON ID %02x\n", idcode[1]);
78 return NULL;
79 }
80
81 eon = malloc(sizeof(*eon));
82 if (!eon) {
83 debug("SF: Failed to allocate memory\n");
84 return NULL;
85 }
86
87 eon->params = params;
88 eon->flash.spi = spi;
89 eon->flash.name = params->name;
90
Mike Frysingerd4aa5002011-04-25 06:58:29 +000091 eon->flash.write = spi_flash_cmd_write_multi;
Chong Huangd1d906562010-11-30 03:33:25 -050092 eon->flash.erase = eon_erase;
Mike Frysingera4c3b402011-01-10 02:20:14 -050093 eon->flash.read = spi_flash_cmd_read_fast;
Mike Frysingerd4aa5002011-04-25 06:58:29 +000094 eon->flash.page_size = params->page_size;
Richard Retanubun4e6a5152011-02-16 16:37:22 -050095 eon->flash.sector_size = params->page_size * params->pages_per_sector
96 * params->sectors_per_block;
Chong Huangd1d906562010-11-30 03:33:25 -050097 eon->flash.size = params->page_size * params->pages_per_sector
98 * params->nr_sectors;
99
Chong Huangd1d906562010-11-30 03:33:25 -0500100 return &eon->flash;
101}