blob: eccdf833df05a742b20a606ec5d4232784cd3e4d [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 */
Chong Huangd1d906562010-11-30 03:33:25 -050014#define CMD_EN25Q128_SE 0x20 /* Sector Erase */
15#define CMD_EN25Q128_BE 0xd8 /* Block Erase */
Chong Huangd1d906562010-11-30 03:33:25 -050016
Chong Huangd1d906562010-11-30 03:33:25 -050017struct eon_spi_flash_params {
18 u8 idcode1;
Chong Huangd1d906562010-11-30 03:33:25 -050019 u16 nr_sectors;
20 const char *name;
21};
22
Chong Huangd1d906562010-11-30 03:33:25 -050023static const struct eon_spi_flash_params eon_spi_flash_table[] = {
24 {
Shaohui Xiebd75c632011-09-27 19:21:34 +080025 .idcode1 = 0x16,
Shaohui Xiebd75c632011-09-27 19:21:34 +080026 .nr_sectors = 1024,
27 .name = "EN25Q32B",
28 },
29 {
Mike Frysingerfba2c442011-04-23 23:05:58 +000030 .idcode1 = 0x18,
Chong Huangd1d906562010-11-30 03:33:25 -050031 .nr_sectors = 4096,
32 .name = "EN25Q128",
33 },
Shaohui Xie23d8f772011-09-27 01:21:34 +000034 {
35 .idcode1 = 0x16,
Shaohui Xie23d8f772011-09-27 01:21:34 +000036 .nr_sectors = 1024,
37 .name = "EN25Q32B",
38 },
Chong Huangd1d906562010-11-30 03:33:25 -050039};
40
Mike Frysingerf8f07572011-04-12 01:51:29 -040041static int eon_erase(struct spi_flash *flash, u32 offset, size_t len)
Chong Huangd1d906562010-11-30 03:33:25 -050042{
Richard Retanubun4e6a5152011-02-16 16:37:22 -050043 return spi_flash_cmd_erase(flash, CMD_EN25Q128_BE, offset, len);
Chong Huangd1d906562010-11-30 03:33:25 -050044}
45
46struct spi_flash *spi_flash_probe_eon(struct spi_slave *spi, u8 *idcode)
47{
48 const struct eon_spi_flash_params *params;
Mike Frysingerb06afa72011-06-28 07:38:10 +000049 struct spi_flash *flash;
Chong Huangd1d906562010-11-30 03:33:25 -050050 unsigned int i;
51
52 for (i = 0; i < ARRAY_SIZE(eon_spi_flash_table); ++i) {
53 params = &eon_spi_flash_table[i];
54 if (params->idcode1 == idcode[2])
55 break;
56 }
57
58 if (i == ARRAY_SIZE(eon_spi_flash_table)) {
59 debug("SF: Unsupported EON ID %02x\n", idcode[1]);
60 return NULL;
61 }
62
Mike Frysingerb06afa72011-06-28 07:38:10 +000063 flash = malloc(sizeof(*flash));
64 if (!flash) {
Chong Huangd1d906562010-11-30 03:33:25 -050065 debug("SF: Failed to allocate memory\n");
66 return NULL;
67 }
68
Mike Frysingerb06afa72011-06-28 07:38:10 +000069 flash->spi = spi;
70 flash->name = params->name;
Chong Huangd1d906562010-11-30 03:33:25 -050071
Mike Frysingerb06afa72011-06-28 07:38:10 +000072 flash->write = spi_flash_cmd_write_multi;
73 flash->erase = eon_erase;
74 flash->read = spi_flash_cmd_read_fast;
Mike Frysingera4ed3b62012-03-04 22:56:52 -050075 flash->page_size = 256;
76 flash->sector_size = 256 * 16 * 16;
77 flash->size = 256 * 16
Chong Huangd1d906562010-11-30 03:33:25 -050078 * params->nr_sectors;
79
Mike Frysingerb06afa72011-06-28 07:38:10 +000080 return flash;
Chong Huangd1d906562010-11-30 03:33:25 -050081}