blob: eccdf833df05a742b20a606ec5d4232784cd3e4d [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 */
Chong Huangc71e6dc2010-11-30 03:33:25 -050014#define CMD_EN25Q128_SE 0x20 /* Sector Erase */
15#define CMD_EN25Q128_BE 0xd8 /* Block Erase */
Chong Huangc71e6dc2010-11-30 03:33:25 -050016
Chong Huangc71e6dc2010-11-30 03:33:25 -050017struct eon_spi_flash_params {
18 u8 idcode1;
Chong Huangc71e6dc2010-11-30 03:33:25 -050019 u16 nr_sectors;
20 const char *name;
21};
22
Chong Huangc71e6dc2010-11-30 03:33:25 -050023static const struct eon_spi_flash_params eon_spi_flash_table[] = {
24 {
Shaohui Xiefc9d8262011-09-27 19:21:34 +080025 .idcode1 = 0x16,
Shaohui Xiefc9d8262011-09-27 19:21:34 +080026 .nr_sectors = 1024,
27 .name = "EN25Q32B",
28 },
29 {
Mike Frysingerfc9d1fc2011-04-23 23:05:58 +000030 .idcode1 = 0x18,
Chong Huangc71e6dc2010-11-30 03:33:25 -050031 .nr_sectors = 4096,
32 .name = "EN25Q128",
33 },
Shaohui Xie2d0dd6e2011-09-27 01:21:34 +000034 {
35 .idcode1 = 0x16,
Shaohui Xie2d0dd6e2011-09-27 01:21:34 +000036 .nr_sectors = 1024,
37 .name = "EN25Q32B",
38 },
Chong Huangc71e6dc2010-11-30 03:33:25 -050039};
40
Mike Frysingeraea4e172011-04-12 01:51:29 -040041static int eon_erase(struct spi_flash *flash, u32 offset, size_t len)
Chong Huangc71e6dc2010-11-30 03:33:25 -050042{
Richard Retanubunb0148dc2011-02-16 16:37:22 -050043 return spi_flash_cmd_erase(flash, CMD_EN25Q128_BE, offset, len);
Chong Huangc71e6dc2010-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 Frysingerce7cc042011-06-28 07:38:10 +000049 struct spi_flash *flash;
Chong Huangc71e6dc2010-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 Frysingerce7cc042011-06-28 07:38:10 +000063 flash = malloc(sizeof(*flash));
64 if (!flash) {
Chong Huangc71e6dc2010-11-30 03:33:25 -050065 debug("SF: Failed to allocate memory\n");
66 return NULL;
67 }
68
Mike Frysingerce7cc042011-06-28 07:38:10 +000069 flash->spi = spi;
70 flash->name = params->name;
Chong Huangc71e6dc2010-11-30 03:33:25 -050071
Mike Frysingerce7cc042011-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 Frysingera4940782012-03-04 22:56:52 -050075 flash->page_size = 256;
76 flash->sector_size = 256 * 16 * 16;
77 flash->size = 256 * 16
Chong Huangc71e6dc2010-11-30 03:33:25 -050078 * params->nr_sectors;
79
Mike Frysingerce7cc042011-06-28 07:38:10 +000080 return flash;
Chong Huangc71e6dc2010-11-30 03:33:25 -050081}