ramtron.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. /*
  2. * (C) Copyright 2010
  3. * Reinhard Meyer, EMK Elektronik, reinhard.meyer@emk-elektronik.de
  4. *
  5. * See file CREDITS for list of people who contributed to this
  6. * project.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  21. * MA 02111-1307 USA
  22. */
  23. /*
  24. * Note: RAMTRON SPI FRAMs are ferroelectric, nonvolatile RAMs
  25. * with an interface identical to SPI flash devices.
  26. * However since they behave like RAM there are no delays or
  27. * busy polls required. They can sustain read or write at the
  28. * allowed SPI bus speed, which can be 40 MHz for some devices.
  29. *
  30. * Unfortunately some RAMTRON devices do not have a means of
  31. * identifying them. They will leave the SO line undriven when
  32. * the READ-ID command is issued. It is therefore mandatory
  33. * that the MISO line has a proper pull-up, so that READ-ID
  34. * will return a row of 0xff. This 0xff pseudo-id will cause
  35. * probes by all vendor specific functions that are designed
  36. * to handle it. If the MISO line is not pulled up, READ-ID
  37. * could return any random noise, even mimicking another
  38. * device.
  39. *
  40. * We use CONFIG_SPI_FRAM_RAMTRON_NON_JEDEC
  41. * to define which device will be assumed after a simple status
  42. * register verify. This method is prone to false positive
  43. * detection and should therefore be the last to be tried.
  44. * Enter it in the last position in the table in spi_flash.c!
  45. *
  46. * The define CONFIG_SPI_FRAM_RAMTRON_NON_JEDEC both activates
  47. * compilation of the special handler and defines the device
  48. * to assume.
  49. */
  50. #include <common.h>
  51. #include <malloc.h>
  52. #include <spi_flash.h>
  53. #include "spi_flash_internal.h"
  54. /* RAMTRON commands common to all devices */
  55. #define CMD_RAMTRON_WREN 0x06 /* Write Enable */
  56. #define CMD_RAMTRON_WRDI 0x04 /* Write Disable */
  57. #define CMD_RAMTRON_RDSR 0x05 /* Read Status Register */
  58. #define CMD_RAMTRON_WRSR 0x01 /* Write Status Register */
  59. #define CMD_RAMTRON_READ 0x03 /* Read Data Bytes */
  60. #define CMD_RAMTRON_WRITE 0x02 /* Write Data Bytes */
  61. /* not all have those: */
  62. #define CMD_RAMTRON_FSTRD 0x0b /* Fast Read (for compatibility - not used here) */
  63. #define CMD_RAMTRON_SLEEP 0xb9 /* Enter Sleep Mode */
  64. #define CMD_RAMTRON_RDID 0x9f /* Read ID */
  65. #define CMD_RAMTRON_SNR 0xc3 /* Read Serial Number */
  66. /*
  67. * Properties of supported FRAMs
  68. * Note: speed is currently not used because we have no method to deliver that
  69. * value to the upper layers
  70. */
  71. struct ramtron_spi_fram_params {
  72. u32 size; /* size in bytes */
  73. u8 addr_len; /* number of address bytes */
  74. u8 merge_cmd; /* some address bits are in the command byte */
  75. u8 id1; /* device ID 1 (family, density) */
  76. u8 id2; /* device ID 2 (sub, rev, rsvd) */
  77. u32 speed; /* max. SPI clock in Hz */
  78. const char *name; /* name for display and/or matching */
  79. };
  80. struct ramtron_spi_fram {
  81. struct spi_flash flash;
  82. const struct ramtron_spi_fram_params *params;
  83. };
  84. static inline struct ramtron_spi_fram *to_ramtron_spi_fram(struct spi_flash
  85. *flash)
  86. {
  87. return container_of(flash, struct ramtron_spi_fram, flash);
  88. }
  89. /*
  90. * table describing supported FRAM chips:
  91. * chips without RDID command must have the values 0xff for id1 and id2
  92. */
  93. static const struct ramtron_spi_fram_params ramtron_spi_fram_table[] = {
  94. {
  95. .size = 32*1024,
  96. .addr_len = 2,
  97. .merge_cmd = 0,
  98. .id1 = 0x22,
  99. .id2 = 0x00,
  100. .speed = 40000000,
  101. .name = "FM25V02",
  102. },
  103. {
  104. .size = 32*1024,
  105. .addr_len = 2,
  106. .merge_cmd = 0,
  107. .id1 = 0x22,
  108. .id2 = 0x01,
  109. .speed = 40000000,
  110. .name = "FM25VN02",
  111. },
  112. {
  113. .size = 64*1024,
  114. .addr_len = 2,
  115. .merge_cmd = 0,
  116. .id1 = 0x23,
  117. .id2 = 0x00,
  118. .speed = 40000000,
  119. .name = "FM25V05",
  120. },
  121. {
  122. .size = 64*1024,
  123. .addr_len = 2,
  124. .merge_cmd = 0,
  125. .id1 = 0x23,
  126. .id2 = 0x01,
  127. .speed = 40000000,
  128. .name = "FM25VN05",
  129. },
  130. {
  131. .size = 128*1024,
  132. .addr_len = 3,
  133. .merge_cmd = 0,
  134. .id1 = 0x24,
  135. .id2 = 0x00,
  136. .speed = 40000000,
  137. .name = "FM25V10",
  138. },
  139. {
  140. .size = 128*1024,
  141. .addr_len = 3,
  142. .merge_cmd = 0,
  143. .id1 = 0x24,
  144. .id2 = 0x01,
  145. .speed = 40000000,
  146. .name = "FM25VN10",
  147. },
  148. #ifdef CONFIG_SPI_FRAM_RAMTRON_NON_JEDEC
  149. {
  150. .size = 256*1024,
  151. .addr_len = 3,
  152. .merge_cmd = 0,
  153. .id1 = 0xff,
  154. .id2 = 0xff,
  155. .speed = 40000000,
  156. .name = "FM25H20",
  157. },
  158. #endif
  159. };
  160. static int ramtron_common(struct spi_flash *flash,
  161. u32 offset, size_t len, void *buf, u8 command)
  162. {
  163. struct ramtron_spi_fram *sn = to_ramtron_spi_fram(flash);
  164. u8 cmd[4];
  165. int cmd_len;
  166. int ret;
  167. if (sn->params->addr_len == 3 && sn->params->merge_cmd == 0) {
  168. cmd[0] = command;
  169. cmd[1] = offset >> 16;
  170. cmd[2] = offset >> 8;
  171. cmd[3] = offset;
  172. cmd_len = 4;
  173. } else if (sn->params->addr_len == 2 && sn->params->merge_cmd == 0) {
  174. cmd[0] = command;
  175. cmd[1] = offset >> 8;
  176. cmd[2] = offset;
  177. cmd_len = 3;
  178. } else {
  179. printf("SF: unsupported addr_len or merge_cmd\n");
  180. return -1;
  181. }
  182. /* claim the bus */
  183. ret = spi_claim_bus(flash->spi);
  184. if (ret) {
  185. debug("SF: Unable to claim SPI bus\n");
  186. return ret;
  187. }
  188. if (command == CMD_RAMTRON_WRITE) {
  189. /* send WREN */
  190. ret = spi_flash_cmd(flash->spi, CMD_RAMTRON_WREN, NULL, 0);
  191. if (ret < 0) {
  192. debug("SF: Enabling Write failed\n");
  193. goto releasebus;
  194. }
  195. }
  196. /* do the transaction */
  197. if (command == CMD_RAMTRON_WRITE)
  198. ret = spi_flash_cmd_write(flash->spi, cmd, cmd_len, buf, len);
  199. else
  200. ret = spi_flash_cmd_read(flash->spi, cmd, cmd_len, buf, len);
  201. if (ret < 0)
  202. debug("SF: Transaction failed\n");
  203. releasebus:
  204. /* release the bus */
  205. spi_release_bus(flash->spi);
  206. return ret;
  207. }
  208. static int ramtron_read(struct spi_flash *flash,
  209. u32 offset, size_t len, void *buf)
  210. {
  211. return ramtron_common(flash, offset, len, buf,
  212. CMD_RAMTRON_READ);
  213. }
  214. static int ramtron_write(struct spi_flash *flash,
  215. u32 offset, size_t len, const void *buf)
  216. {
  217. return ramtron_common(flash, offset, len, (void *)buf,
  218. CMD_RAMTRON_WRITE);
  219. }
  220. static int ramtron_erase(struct spi_flash *flash, u32 offset, size_t len)
  221. {
  222. debug("SF: Erase of RAMTRON FRAMs is pointless\n");
  223. return -1;
  224. }
  225. /*
  226. * nore: we are called here with idcode pointing to the first non-0x7f byte
  227. * already!
  228. */
  229. struct spi_flash *spi_fram_probe_ramtron(struct spi_slave *spi, u8 *idcode)
  230. {
  231. const struct ramtron_spi_fram_params *params;
  232. struct ramtron_spi_fram *sn;
  233. unsigned int i;
  234. #ifdef CONFIG_SPI_FRAM_RAMTRON_NON_JEDEC
  235. int ret;
  236. u8 sr;
  237. #endif
  238. /* NOTE: the bus has been claimed before this function is called! */
  239. switch (idcode[0]) {
  240. case 0xc2:
  241. /* JEDEC conformant RAMTRON id */
  242. for (i = 0; i < ARRAY_SIZE(ramtron_spi_fram_table); i++) {
  243. params = &ramtron_spi_fram_table[i];
  244. if (idcode[1] == params->id1 && idcode[2] == params->id2)
  245. goto found;
  246. }
  247. break;
  248. #ifdef CONFIG_SPI_FRAM_RAMTRON_NON_JEDEC
  249. case 0xff:
  250. /*
  251. * probably open MISO line, pulled up.
  252. * We COULD have a non JEDEC conformant FRAM here,
  253. * read the status register to verify
  254. */
  255. ret = spi_flash_cmd(spi, CMD_RAMTRON_RDSR, &sr, 1);
  256. if (ret)
  257. return NULL;
  258. /* Bits 5,4,0 are fixed 0 for all devices */
  259. if ((sr & 0x31) != 0x00)
  260. return NULL;
  261. /* now find the device */
  262. for (i = 0; i < ARRAY_SIZE(ramtron_spi_fram_table); i++) {
  263. params = &ramtron_spi_fram_table[i];
  264. if (!strcmp(params->name, CONFIG_SPI_FRAM_RAMTRON_NON_JEDEC))
  265. goto found;
  266. }
  267. debug("SF: Unsupported non-JEDEC RAMTRON device "
  268. CONFIG_SPI_FRAM_RAMTRON_NON_JEDEC "\n");
  269. break;
  270. #endif
  271. default:
  272. break;
  273. }
  274. /* arriving here means no method has found a device we can handle */
  275. debug("SF/ramtron: unsupported device id0=%02x id1=%02x id2=%02x\n",
  276. idcode[0], idcode[1], idcode[2]);
  277. return NULL;
  278. found:
  279. sn = malloc(sizeof(*sn));
  280. if (!sn) {
  281. debug("SF: Failed to allocate memory\n");
  282. return NULL;
  283. }
  284. sn->params = params;
  285. sn->flash.spi = spi;
  286. sn->flash.name = params->name;
  287. sn->flash.write = ramtron_write;
  288. sn->flash.read = ramtron_read;
  289. sn->flash.erase = ramtron_erase;
  290. sn->flash.size = params->size;
  291. return &sn->flash;
  292. }