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 */ |
Chong Huang | d1d90656 | 2010-11-30 03:33:25 -0500 | [diff] [blame] | 14 | #define CMD_EN25Q128_SE 0x20 /* Sector Erase */ |
| 15 | #define CMD_EN25Q128_BE 0xd8 /* Block Erase */ |
Chong Huang | d1d90656 | 2010-11-30 03:33:25 -0500 | [diff] [blame] | 16 | |
Chong Huang | d1d90656 | 2010-11-30 03:33:25 -0500 | [diff] [blame] | 17 | struct eon_spi_flash_params { |
| 18 | u8 idcode1; |
Chong Huang | d1d90656 | 2010-11-30 03:33:25 -0500 | [diff] [blame] | 19 | u16 nr_sectors; |
| 20 | const char *name; |
| 21 | }; |
| 22 | |
Chong Huang | d1d90656 | 2010-11-30 03:33:25 -0500 | [diff] [blame] | 23 | static const struct eon_spi_flash_params eon_spi_flash_table[] = { |
| 24 | { |
Shaohui Xie | bd75c63 | 2011-09-27 19:21:34 +0800 | [diff] [blame] | 25 | .idcode1 = 0x16, |
Shaohui Xie | bd75c63 | 2011-09-27 19:21:34 +0800 | [diff] [blame] | 26 | .nr_sectors = 1024, |
| 27 | .name = "EN25Q32B", |
| 28 | }, |
| 29 | { |
Mike Frysinger | fba2c44 | 2011-04-23 23:05:58 +0000 | [diff] [blame] | 30 | .idcode1 = 0x18, |
Chong Huang | d1d90656 | 2010-11-30 03:33:25 -0500 | [diff] [blame] | 31 | .nr_sectors = 4096, |
| 32 | .name = "EN25Q128", |
| 33 | }, |
Shaohui Xie | 23d8f77 | 2011-09-27 01:21:34 +0000 | [diff] [blame] | 34 | { |
| 35 | .idcode1 = 0x16, |
Shaohui Xie | 23d8f77 | 2011-09-27 01:21:34 +0000 | [diff] [blame] | 36 | .nr_sectors = 1024, |
| 37 | .name = "EN25Q32B", |
| 38 | }, |
Chong Huang | d1d90656 | 2010-11-30 03:33:25 -0500 | [diff] [blame] | 39 | }; |
| 40 | |
Mike Frysinger | f8f0757 | 2011-04-12 01:51:29 -0400 | [diff] [blame] | 41 | 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] | 42 | { |
Richard Retanubun | 4e6a515 | 2011-02-16 16:37:22 -0500 | [diff] [blame] | 43 | return spi_flash_cmd_erase(flash, CMD_EN25Q128_BE, offset, len); |
Chong Huang | d1d90656 | 2010-11-30 03:33:25 -0500 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | struct spi_flash *spi_flash_probe_eon(struct spi_slave *spi, u8 *idcode) |
| 47 | { |
| 48 | const struct eon_spi_flash_params *params; |
Mike Frysinger | b06afa7 | 2011-06-28 07:38:10 +0000 | [diff] [blame] | 49 | struct spi_flash *flash; |
Chong Huang | d1d90656 | 2010-11-30 03:33:25 -0500 | [diff] [blame] | 50 | 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 Frysinger | b06afa7 | 2011-06-28 07:38:10 +0000 | [diff] [blame] | 63 | flash = malloc(sizeof(*flash)); |
| 64 | if (!flash) { |
Chong Huang | d1d90656 | 2010-11-30 03:33:25 -0500 | [diff] [blame] | 65 | debug("SF: Failed to allocate memory\n"); |
| 66 | return NULL; |
| 67 | } |
| 68 | |
Mike Frysinger | b06afa7 | 2011-06-28 07:38:10 +0000 | [diff] [blame] | 69 | flash->spi = spi; |
| 70 | flash->name = params->name; |
Chong Huang | d1d90656 | 2010-11-30 03:33:25 -0500 | [diff] [blame] | 71 | |
Mike Frysinger | b06afa7 | 2011-06-28 07:38:10 +0000 | [diff] [blame] | 72 | flash->write = spi_flash_cmd_write_multi; |
| 73 | flash->erase = eon_erase; |
| 74 | flash->read = spi_flash_cmd_read_fast; |
Mike Frysinger | a4ed3b6 | 2012-03-04 22:56:52 -0500 | [diff] [blame^] | 75 | flash->page_size = 256; |
| 76 | flash->sector_size = 256 * 16 * 16; |
| 77 | flash->size = 256 * 16 |
Chong Huang | d1d90656 | 2010-11-30 03:33:25 -0500 | [diff] [blame] | 78 | * params->nr_sectors; |
| 79 | |
Mike Frysinger | b06afa7 | 2011-06-28 07:38:10 +0000 | [diff] [blame] | 80 | return flash; |
Chong Huang | d1d90656 | 2010-11-30 03:33:25 -0500 | [diff] [blame] | 81 | } |