Chong Huang | d1d90656 | 2010-11-30 03:33:25 -0500 | [diff] [blame] | 1 | /* |
| 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 Huang | d1d90656 | 2010-11-30 03:33:25 -0500 | [diff] [blame] | 28 | struct 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 | |
Chong Huang | d1d90656 | 2010-11-30 03:33:25 -0500 | [diff] [blame] | 37 | static const struct eon_spi_flash_params eon_spi_flash_table[] = { |
| 38 | { |
| 39 | .idcode1 = EON_ID_EN25Q128, |
| 40 | .page_size = 256, |
| 41 | .pages_per_sector = 16, |
| 42 | .sectors_per_block = 16, |
| 43 | .nr_sectors = 4096, |
| 44 | .name = "EN25Q128", |
| 45 | }, |
| 46 | }; |
| 47 | |
Mike Frysinger | f8f0757 | 2011-04-12 01:51:29 -0400 | [diff] [blame] | 48 | static int eon_erase(struct spi_flash *flash, u32 offset, size_t len) |
Chong Huang | d1d90656 | 2010-11-30 03:33:25 -0500 | [diff] [blame] | 49 | { |
Richard Retanubun | 4e6a515 | 2011-02-16 16:37:22 -0500 | [diff] [blame] | 50 | return spi_flash_cmd_erase(flash, CMD_EN25Q128_BE, offset, len); |
Chong Huang | d1d90656 | 2010-11-30 03:33:25 -0500 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | struct spi_flash *spi_flash_probe_eon(struct spi_slave *spi, u8 *idcode) |
| 54 | { |
| 55 | const struct eon_spi_flash_params *params; |
Mike Frysinger | b06afa7 | 2011-06-28 07:38:10 +0000 | [diff] [blame^] | 56 | struct spi_flash *flash; |
Chong Huang | d1d90656 | 2010-11-30 03:33:25 -0500 | [diff] [blame] | 57 | unsigned int i; |
| 58 | |
| 59 | for (i = 0; i < ARRAY_SIZE(eon_spi_flash_table); ++i) { |
| 60 | params = &eon_spi_flash_table[i]; |
| 61 | if (params->idcode1 == idcode[2]) |
| 62 | break; |
| 63 | } |
| 64 | |
| 65 | if (i == ARRAY_SIZE(eon_spi_flash_table)) { |
| 66 | debug("SF: Unsupported EON ID %02x\n", idcode[1]); |
| 67 | return NULL; |
| 68 | } |
| 69 | |
Mike Frysinger | b06afa7 | 2011-06-28 07:38:10 +0000 | [diff] [blame^] | 70 | flash = malloc(sizeof(*flash)); |
| 71 | if (!flash) { |
Chong Huang | d1d90656 | 2010-11-30 03:33:25 -0500 | [diff] [blame] | 72 | debug("SF: Failed to allocate memory\n"); |
| 73 | return NULL; |
| 74 | } |
| 75 | |
Mike Frysinger | b06afa7 | 2011-06-28 07:38:10 +0000 | [diff] [blame^] | 76 | flash->spi = spi; |
| 77 | flash->name = params->name; |
Chong Huang | d1d90656 | 2010-11-30 03:33:25 -0500 | [diff] [blame] | 78 | |
Mike Frysinger | b06afa7 | 2011-06-28 07:38:10 +0000 | [diff] [blame^] | 79 | flash->write = spi_flash_cmd_write_multi; |
| 80 | flash->erase = eon_erase; |
| 81 | flash->read = spi_flash_cmd_read_fast; |
| 82 | flash->page_size = params->page_size; |
| 83 | flash->sector_size = params->page_size * params->pages_per_sector |
Richard Retanubun | 4e6a515 | 2011-02-16 16:37:22 -0500 | [diff] [blame] | 84 | * params->sectors_per_block; |
Mike Frysinger | b06afa7 | 2011-06-28 07:38:10 +0000 | [diff] [blame^] | 85 | flash->size = params->page_size * params->pages_per_sector |
Chong Huang | d1d90656 | 2010-11-30 03:33:25 -0500 | [diff] [blame] | 86 | * params->nr_sectors; |
| 87 | |
Mike Frysinger | b06afa7 | 2011-06-28 07:38:10 +0000 | [diff] [blame^] | 88 | return flash; |
Chong Huang | d1d90656 | 2010-11-30 03:33:25 -0500 | [diff] [blame] | 89 | } |