ramtron.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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. /*
  55. * Properties of supported FRAMs
  56. * Note: speed is currently not used because we have no method to deliver that
  57. * value to the upper layers
  58. */
  59. struct ramtron_spi_fram_params {
  60. u32 size; /* size in bytes */
  61. u8 addr_len; /* number of address bytes */
  62. u8 merge_cmd; /* some address bits are in the command byte */
  63. u8 id1; /* device ID 1 (family, density) */
  64. u8 id2; /* device ID 2 (sub, rev, rsvd) */
  65. u32 speed; /* max. SPI clock in Hz */
  66. const char *name; /* name for display and/or matching */
  67. };
  68. struct ramtron_spi_fram {
  69. struct spi_flash flash;
  70. const struct ramtron_spi_fram_params *params;
  71. };
  72. static inline struct ramtron_spi_fram *to_ramtron_spi_fram(struct spi_flash
  73. *flash)
  74. {
  75. return container_of(flash, struct ramtron_spi_fram, flash);
  76. }
  77. /*
  78. * table describing supported FRAM chips:
  79. * chips without RDID command must have the values 0xff for id1 and id2
  80. */
  81. static const struct ramtron_spi_fram_params ramtron_spi_fram_table[] = {
  82. {
  83. .size = 32*1024,
  84. .addr_len = 2,
  85. .merge_cmd = 0,
  86. .id1 = 0x22,
  87. .id2 = 0x00,
  88. .speed = 40000000,
  89. .name = "FM25V02",
  90. },
  91. {
  92. .size = 32*1024,
  93. .addr_len = 2,
  94. .merge_cmd = 0,
  95. .id1 = 0x22,
  96. .id2 = 0x01,
  97. .speed = 40000000,
  98. .name = "FM25VN02",
  99. },
  100. {
  101. .size = 64*1024,
  102. .addr_len = 2,
  103. .merge_cmd = 0,
  104. .id1 = 0x23,
  105. .id2 = 0x00,
  106. .speed = 40000000,
  107. .name = "FM25V05",
  108. },
  109. {
  110. .size = 64*1024,
  111. .addr_len = 2,
  112. .merge_cmd = 0,
  113. .id1 = 0x23,
  114. .id2 = 0x01,
  115. .speed = 40000000,
  116. .name = "FM25VN05",
  117. },
  118. {
  119. .size = 128*1024,
  120. .addr_len = 3,
  121. .merge_cmd = 0,
  122. .id1 = 0x24,
  123. .id2 = 0x00,
  124. .speed = 40000000,
  125. .name = "FM25V10",
  126. },
  127. {
  128. .size = 128*1024,
  129. .addr_len = 3,
  130. .merge_cmd = 0,
  131. .id1 = 0x24,
  132. .id2 = 0x01,
  133. .speed = 40000000,
  134. .name = "FM25VN10",
  135. },
  136. #ifdef CONFIG_SPI_FRAM_RAMTRON_NON_JEDEC
  137. {
  138. .size = 256*1024,
  139. .addr_len = 3,
  140. .merge_cmd = 0,
  141. .id1 = 0xff,
  142. .id2 = 0xff,
  143. .speed = 40000000,
  144. .name = "FM25H20",
  145. },
  146. #endif
  147. };
  148. static int ramtron_common(struct spi_flash *flash,
  149. u32 offset, size_t len, void *buf, u8 command)
  150. {
  151. struct ramtron_spi_fram *sn = to_ramtron_spi_fram(flash);
  152. u8 cmd[4];
  153. int cmd_len;
  154. int ret;
  155. if (sn->params->addr_len == 3 && sn->params->merge_cmd == 0) {
  156. cmd[0] = command;
  157. cmd[1] = offset >> 16;
  158. cmd[2] = offset >> 8;
  159. cmd[3] = offset;
  160. cmd_len = 4;
  161. } else if (sn->params->addr_len == 2 && sn->params->merge_cmd == 0) {
  162. cmd[0] = command;
  163. cmd[1] = offset >> 8;
  164. cmd[2] = offset;
  165. cmd_len = 3;
  166. } else {
  167. printf("SF: unsupported addr_len or merge_cmd\n");
  168. return -1;
  169. }
  170. /* claim the bus */
  171. ret = spi_claim_bus(flash->spi);
  172. if (ret) {
  173. debug("SF: Unable to claim SPI bus\n");
  174. return ret;
  175. }
  176. if (command == CMD_PAGE_PROGRAM) {
  177. /* send WREN */
  178. ret = spi_flash_cmd_write_enable(flash);
  179. if (ret < 0) {
  180. debug("SF: Enabling Write failed\n");
  181. goto releasebus;
  182. }
  183. }
  184. /* do the transaction */
  185. if (command == CMD_PAGE_PROGRAM)
  186. ret = spi_flash_cmd_write(flash->spi, cmd, cmd_len, buf, len);
  187. else
  188. ret = spi_flash_cmd_read(flash->spi, cmd, cmd_len, buf, len);
  189. if (ret < 0)
  190. debug("SF: Transaction failed\n");
  191. releasebus:
  192. /* release the bus */
  193. spi_release_bus(flash->spi);
  194. return ret;
  195. }
  196. static int ramtron_read(struct spi_flash *flash,
  197. u32 offset, size_t len, void *buf)
  198. {
  199. return ramtron_common(flash, offset, len, buf,
  200. CMD_READ_ARRAY_SLOW);
  201. }
  202. static int ramtron_write(struct spi_flash *flash,
  203. u32 offset, size_t len, const void *buf)
  204. {
  205. return ramtron_common(flash, offset, len, (void *)buf,
  206. CMD_PAGE_PROGRAM);
  207. }
  208. static int ramtron_erase(struct spi_flash *flash, u32 offset, size_t len)
  209. {
  210. debug("SF: Erase of RAMTRON FRAMs is pointless\n");
  211. return -1;
  212. }
  213. /*
  214. * nore: we are called here with idcode pointing to the first non-0x7f byte
  215. * already!
  216. */
  217. struct spi_flash *spi_fram_probe_ramtron(struct spi_slave *spi, u8 *idcode)
  218. {
  219. const struct ramtron_spi_fram_params *params;
  220. struct ramtron_spi_fram *sn;
  221. unsigned int i;
  222. #ifdef CONFIG_SPI_FRAM_RAMTRON_NON_JEDEC
  223. int ret;
  224. u8 sr;
  225. #endif
  226. /* NOTE: the bus has been claimed before this function is called! */
  227. switch (idcode[0]) {
  228. case 0xc2:
  229. /* JEDEC conformant RAMTRON id */
  230. for (i = 0; i < ARRAY_SIZE(ramtron_spi_fram_table); i++) {
  231. params = &ramtron_spi_fram_table[i];
  232. if (idcode[1] == params->id1 && idcode[2] == params->id2)
  233. goto found;
  234. }
  235. break;
  236. #ifdef CONFIG_SPI_FRAM_RAMTRON_NON_JEDEC
  237. case 0xff:
  238. /*
  239. * probably open MISO line, pulled up.
  240. * We COULD have a non JEDEC conformant FRAM here,
  241. * read the status register to verify
  242. */
  243. ret = spi_flash_cmd(spi, CMD_READ_STATUS, &sr, 1);
  244. if (ret)
  245. return NULL;
  246. /* Bits 5,4,0 are fixed 0 for all devices */
  247. if ((sr & 0x31) != 0x00)
  248. return NULL;
  249. /* now find the device */
  250. for (i = 0; i < ARRAY_SIZE(ramtron_spi_fram_table); i++) {
  251. params = &ramtron_spi_fram_table[i];
  252. if (!strcmp(params->name, CONFIG_SPI_FRAM_RAMTRON_NON_JEDEC))
  253. goto found;
  254. }
  255. debug("SF: Unsupported non-JEDEC RAMTRON device "
  256. CONFIG_SPI_FRAM_RAMTRON_NON_JEDEC "\n");
  257. break;
  258. #endif
  259. default:
  260. break;
  261. }
  262. /* arriving here means no method has found a device we can handle */
  263. debug("SF/ramtron: unsupported device id0=%02x id1=%02x id2=%02x\n",
  264. idcode[0], idcode[1], idcode[2]);
  265. return NULL;
  266. found:
  267. sn = spi_flash_alloc(struct ramtron_spi_fram, spi, params->name);
  268. if (!sn) {
  269. debug("SF: Failed to allocate memory\n");
  270. return NULL;
  271. }
  272. sn->params = params;
  273. sn->flash.write = ramtron_write;
  274. sn->flash.read = ramtron_read;
  275. sn->flash.erase = ramtron_erase;
  276. sn->flash.size = params->size;
  277. return &sn->flash;
  278. }