blob: 6a3bf0f3221365e80041a18ad1f84c2d93a5c410 [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;
19 u16 page_size;
20 u16 pages_per_sector;
21 u16 sectors_per_block;
22 u16 nr_sectors;
23 const char *name;
24};
25
Chong Huangc71e6dc2010-11-30 03:33:25 -050026static const struct eon_spi_flash_params eon_spi_flash_table[] = {
27 {
Shaohui Xiefc9d8262011-09-27 19:21:34 +080028 .idcode1 = 0x16,
29 .page_size = 256,
30 .pages_per_sector = 16,
31 .sectors_per_block = 16,
32 .nr_sectors = 1024,
33 .name = "EN25Q32B",
34 },
35 {
Mike Frysingerfc9d1fc2011-04-23 23:05:58 +000036 .idcode1 = 0x18,
Chong Huangc71e6dc2010-11-30 03:33:25 -050037 .page_size = 256,
38 .pages_per_sector = 16,
39 .sectors_per_block = 16,
40 .nr_sectors = 4096,
41 .name = "EN25Q128",
42 },
Shaohui Xie2d0dd6e2011-09-27 01:21:34 +000043 {
44 .idcode1 = 0x16,
45 .page_size = 256,
46 .pages_per_sector = 16,
47 .sectors_per_block = 16,
48 .nr_sectors = 1024,
49 .name = "EN25Q32B",
50 },
Chong Huangc71e6dc2010-11-30 03:33:25 -050051};
52
Mike Frysingeraea4e172011-04-12 01:51:29 -040053static int eon_erase(struct spi_flash *flash, u32 offset, size_t len)
Chong Huangc71e6dc2010-11-30 03:33:25 -050054{
Richard Retanubunb0148dc2011-02-16 16:37:22 -050055 return spi_flash_cmd_erase(flash, CMD_EN25Q128_BE, offset, len);
Chong Huangc71e6dc2010-11-30 03:33:25 -050056}
57
58struct spi_flash *spi_flash_probe_eon(struct spi_slave *spi, u8 *idcode)
59{
60 const struct eon_spi_flash_params *params;
Mike Frysingerce7cc042011-06-28 07:38:10 +000061 struct spi_flash *flash;
Chong Huangc71e6dc2010-11-30 03:33:25 -050062 unsigned int i;
63
64 for (i = 0; i < ARRAY_SIZE(eon_spi_flash_table); ++i) {
65 params = &eon_spi_flash_table[i];
66 if (params->idcode1 == idcode[2])
67 break;
68 }
69
70 if (i == ARRAY_SIZE(eon_spi_flash_table)) {
71 debug("SF: Unsupported EON ID %02x\n", idcode[1]);
72 return NULL;
73 }
74
Mike Frysingerce7cc042011-06-28 07:38:10 +000075 flash = malloc(sizeof(*flash));
76 if (!flash) {
Chong Huangc71e6dc2010-11-30 03:33:25 -050077 debug("SF: Failed to allocate memory\n");
78 return NULL;
79 }
80
Mike Frysingerce7cc042011-06-28 07:38:10 +000081 flash->spi = spi;
82 flash->name = params->name;
Chong Huangc71e6dc2010-11-30 03:33:25 -050083
Mike Frysingerce7cc042011-06-28 07:38:10 +000084 flash->write = spi_flash_cmd_write_multi;
85 flash->erase = eon_erase;
86 flash->read = spi_flash_cmd_read_fast;
87 flash->page_size = params->page_size;
88 flash->sector_size = params->page_size * params->pages_per_sector
Richard Retanubunb0148dc2011-02-16 16:37:22 -050089 * params->sectors_per_block;
Mike Frysingerce7cc042011-06-28 07:38:10 +000090 flash->size = params->page_size * params->pages_per_sector
Chong Huangc71e6dc2010-11-30 03:33:25 -050091 * params->nr_sectors;
92
Mike Frysingerce7cc042011-06-28 07:38:10 +000093 return flash;
Chong Huangc71e6dc2010-11-30 03:33:25 -050094}