spi_at45d.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  1. /*
  2. * Copyright (C) 2008-2009 by egnite GmbH
  3. * Copyright (C) 2006 by egnite Software GmbH
  4. *
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in the
  15. * documentation and/or other materials provided with the distribution.
  16. * 3. Neither the name of the copyright holders nor the names of
  17. * contributors may be used to endorse or promote products derived
  18. * from this software without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  23. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  24. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  25. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  26. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  27. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  28. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  29. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  30. * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31. * SUCH DAMAGE.
  32. *
  33. * For additional information see http://www.ethernut.de/
  34. */
  35. /*!
  36. * \file dev/spi_at45d.c
  37. * \brief Routines for Adesto/Atmel AT45 serial DataFlash memory chips.
  38. *
  39. * \verbatim
  40. * $Id: spi_at45d.c 5227 2013-07-19 08:48:47Z u_bonnes $
  41. * \endverbatim
  42. */
  43. #include <cfg/os.h>
  44. #include <cfg/memory.h>
  45. #include <dev/blockdev.h>
  46. #include <sys/nutdebug.h>
  47. #include <sys/timer.h>
  48. #include <string.h>
  49. #include <dev/at45d.h>
  50. #include <dev/spi_at45d.h>
  51. #ifndef AT45_WRITE_POLLS
  52. #define AT45_WRITE_POLLS 1000
  53. #endif
  54. #if 0
  55. /*! \brief Parameter table of known DataFlash types. */
  56. AT45D_INFO at45d_info[] = {
  57. {8, 512, 256, 0x0D}, /* AT45DB011B - 128kB */
  58. {9, 512, 264, 0x0C}, /* AT45DB011B - 128kB */
  59. {8, 1025, 256, 0x15}, /* AT45DB021B - 256kB */
  60. {9, 1025, 264, 0x14}, /* AT45DB021B - 256kB */
  61. {8, 2048, 256, 0x1D}, /* AT45DB041B - 512kB */
  62. {9, 2048, 264, 0x1C}, /* AT45DB041B - 512kB */
  63. {8, 4096, 256, 0x25}, /* AT45DB081B - 1MB */
  64. {9, 4096, 264, 0x24}, /* AT45DB081B - 1MB */
  65. {9, 4096, 512, 0x2D}, /* AT45DB0161B - 2MB */
  66. {10, 4096, 528, 0x2C}, /* AT45DB0161B - 2MB */
  67. {9, 8192, 512, 0x35}, /* AT45DB0321B - 4MB */
  68. {10, 8192, 528, 0x34}, /* AT45DB0321B - 4MB */
  69. {10, 8192, 1024, 0x39}, /* AT45DB0642 - 8MB */
  70. {11, 8192, 1056, 0x38}, /* AT45DB0642 - 8MB */
  71. {10, 8192, 1024, 0x3D}, /* AT45DB0642D - 8MB */
  72. {11, 8192, 1056, 0x3C} /* AT45DB0642D - 8MB */
  73. };
  74. /*! \brief Number of known Dataflash types. */
  75. uint_fast8_t at45d_known_types = sizeof(at45d_info) / sizeof(AT45D_INFO);
  76. #endif
  77. /*!
  78. * \brief Send DataFlash command.
  79. *
  80. * \param node Specifies the SPI node.
  81. * \param op Command operation code.
  82. * \param parm Optional command parameter.
  83. * \param oplen Command length.
  84. * \param txbuf Pointer to the transmit data buffer, may be set to NULL.
  85. * \param rxbuf Pointer to the receive data buffer, may be set to NULL.
  86. * \param xlen Number of byte to receive and/or transmit.
  87. */
  88. static int At45dCommand(NUTSPINODE * node, uint8_t op, uint32_t parm, int oplen, const void *txbuf, void *rxbuf, int xlen)
  89. {
  90. int rc = -1;
  91. NUTSPIBUS *bus;
  92. uint8_t cmd[8];
  93. NUTASSERT(node != NULL);
  94. bus = (NUTSPIBUS *) node->node_bus;
  95. NUTASSERT(bus != NULL);
  96. NUTASSERT(bus->bus_alloc != NULL);
  97. NUTASSERT(bus->bus_transfer != NULL);
  98. NUTASSERT(bus->bus_release != NULL);
  99. NUTASSERT(oplen <= 8);
  100. memset(cmd, 0, oplen);
  101. cmd[0] = op;
  102. if (parm) {
  103. cmd[1] = (uint8_t) (parm >> 16);
  104. cmd[2] = (uint8_t) (parm >> 8);
  105. cmd[3] = (uint8_t) parm;
  106. }
  107. rc = (*bus->bus_alloc) (node, 1000);
  108. if (rc == 0) {
  109. rc = (*bus->bus_transfer) (node, cmd, NULL, oplen);
  110. if (rc == 0 && xlen) {
  111. rc = (*bus->bus_transfer) (node, txbuf, rxbuf, xlen);
  112. }
  113. (*bus->bus_release) (node);
  114. }
  115. return rc;
  116. }
  117. /*!
  118. * \brief Query the status of the DataFlash.
  119. *
  120. * \param node Specifies the SPI node.
  121. *
  122. * \return 0 on success or -1 in case of an error.
  123. */
  124. static uint8_t At45dStatus(NUTSPINODE * node)
  125. {
  126. int rc;
  127. uint8_t cmd[2] = { DFCMD_READ_STATUS, 0xFF };
  128. NUTSPIBUS *bus;
  129. NUTASSERT(node != NULL);
  130. NUTASSERT(node->node_bus != NULL);
  131. bus = (NUTSPIBUS *) node->node_bus;
  132. NUTASSERT(bus->bus_alloc != NULL);
  133. NUTASSERT(bus->bus_transfer != NULL);
  134. NUTASSERT(bus->bus_wait != NULL);
  135. NUTASSERT(bus->bus_release != NULL);
  136. rc = (*bus->bus_alloc) (node, 1000);
  137. if (rc == 0) {
  138. rc = (*bus->bus_transfer) (node, cmd, cmd, 2);
  139. if (rc == 0) {
  140. (*bus->bus_wait) (node, NUT_WAIT_INFINITE);
  141. rc = cmd[1];
  142. }
  143. (*bus->bus_release) (node);
  144. }
  145. return (uint8_t) rc;
  146. }
  147. /*!
  148. * \brief Wait until DataFlash memory cycle finished.
  149. *
  150. * \param node Specifies the SPI node.
  151. *
  152. * \return 0 on success or -1 in case of an error.
  153. */
  154. static int At45dWaitReady(NUTSPINODE * node, uint32_t tmo, int poll)
  155. {
  156. uint8_t sr;
  157. while (((sr = At45dStatus(node)) & 0x80) == 0) {
  158. if (!poll) {
  159. NutSleep(1);
  160. }
  161. if (tmo-- == 0) {
  162. return -1;
  163. }
  164. }
  165. return 0;
  166. }
  167. /*!
  168. * \brief Send DataFlash command.
  169. *
  170. * \param dev Specifies the DataFlash device.
  171. * \param op Command operation code.
  172. * \param parm Optional command parameter.
  173. * \param oplen Command length.
  174. * \param txbuf Pointer to the transmit data buffer, may be set to NULL.
  175. * \param rxbuf Pointer to the receive data buffer, may be set to NULL.
  176. * \param xlen Number of byte to receive and/or transmit.
  177. */
  178. int SpiAt45dCommand(NUTDEVICE * dev, uint8_t op, uint32_t parm, int oplen, const void *txbuf, void *rxbuf, int xlen)
  179. {
  180. NUTASSERT(dev != NULL);
  181. return At45dCommand(dev->dev_icb, op, parm, oplen, txbuf, rxbuf, xlen);
  182. }
  183. /*!
  184. * \brief Query the status of the DataFlash.
  185. *
  186. * \param dev Specifies the Flash device.
  187. *
  188. * \return 0 on success or -1 in case of an error.
  189. */
  190. uint8_t SpiAt45dStatus(NUTDEVICE * dev)
  191. {
  192. NUTASSERT(dev != NULL);
  193. return At45dStatus(dev->dev_icb);
  194. }
  195. /*!
  196. * \brief Wait until DataFlash memory cycle finished.
  197. *
  198. * \param dev Specifies the DataFlash device.
  199. *
  200. * \return 0 on success or -1 in case of an error.
  201. */
  202. int SpiAt45dWaitReady(NUTDEVICE * dev, uint32_t tmo, int poll)
  203. {
  204. NUTASSERT(dev != NULL);
  205. return At45dWaitReady(dev->dev_icb, tmo, poll);
  206. }
  207. /*!
  208. * \brief Erase specified DataFlash page.
  209. *
  210. * \param dev Specifies the registered DataFlash device.
  211. * \param pgn Page number to erase, starting at 0.
  212. *
  213. * \return 0 on success or -1 in case of an error.
  214. */
  215. int SpiAt45dPageErase(NUTDEVICE * dev, uint32_t pgn)
  216. {
  217. NUTBLOCKIO *blkio;
  218. AT45D_INFO *info;
  219. NUTASSERT(dev != NULL);
  220. NUTASSERT(dev->dev_dcb != NULL);
  221. blkio = dev->dev_dcb;
  222. info = (AT45D_INFO *) blkio->blkio_info;
  223. NUTASSERT(blkio->blkio_info != NULL);
  224. if (pgn >= info->at45d_pages) {
  225. return -1;
  226. }
  227. pgn <<= info->at45d_pshft;
  228. return At45dCommand((NUTSPINODE *) dev->dev_icb, DFCMD_PAGE_ERASE, pgn, 4, NULL, NULL, 0);
  229. }
  230. /*!
  231. * \brief Erase all DataFlash pages.
  232. */
  233. int SpiAt45dChipErase(NUTDEVICE * dev)
  234. {
  235. return -1;
  236. }
  237. /*!
  238. * \brief Initialize the DataFlash device.
  239. *
  240. * This routine determines the DataFlash type. It is internally called
  241. * by Nut/OS during device registration.
  242. *
  243. * The driver framework may call this function more than once.
  244. *
  245. * \param dev Specifies the DataFlash device.
  246. *
  247. * \return 0 on success or -1 if no valid DataFlash was found.
  248. */
  249. int SpiAt45dInit(NUTDEVICE * dev)
  250. {
  251. NUTBLOCKIO *blkio;
  252. NUTSPINODE *node;
  253. uint8_t sr;
  254. int_fast8_t i;
  255. NUTASSERT(dev != NULL);
  256. NUTASSERT(dev->dev_dcb != NULL);
  257. NUTASSERT(dev->dev_icb != NULL);
  258. blkio = dev->dev_dcb;
  259. node = dev->dev_icb;
  260. if (At45dWaitReady(node, 10, 1) == 0) {
  261. /* Read the status byte and locate the related table entry. */
  262. sr = At45dStatus(node);
  263. if (sr != 0xff) {
  264. sr &= AT45D_STATUS_DENSITY | AT45D_STATUS_PAGE_SIZE;
  265. for (i = at45d_known_types; --i >= 0;) {
  266. if (sr == at45d_info[i].at45d_srval) {
  267. /* Known DataFlash type. */
  268. blkio->blkio_info = &at45d_info[i];
  269. blkio->blkio_blk_cnt = at45d_info[i].at45d_pages;
  270. blkio->blkio_blk_siz = at45d_info[i].at45d_psize;
  271. return 0;
  272. }
  273. }
  274. }
  275. }
  276. /* Unknown DataFlash type. */
  277. return -1;
  278. }
  279. /*!
  280. * \brief Read data from DataFlash memory.
  281. *
  282. * \param dev Specifies the registered DataFlash device.
  283. * \param pgn Page number to read, starting at 0.
  284. * \param data Points to a buffer that receives the data.
  285. * \param len Number of bytes to read.
  286. *
  287. * \return The number of bytes actually read. A return value of -1 indicates
  288. * an error.
  289. */
  290. int SpiAt45dPageRead(NUTDEVICE * dev, uint32_t pgn, void *data, int len)
  291. {
  292. NUTBLOCKIO *blkio;
  293. AT45D_INFO *info;
  294. NUTASSERT(dev != NULL);
  295. NUTASSERT(dev->dev_dcb != NULL);
  296. blkio = dev->dev_dcb;
  297. info = (AT45D_INFO *) blkio->blkio_info;
  298. NUTASSERT(blkio->blkio_info != NULL);
  299. if (pgn >= info->at45d_pages) {
  300. return -1;
  301. }
  302. pgn <<= info->at45d_pshft;
  303. if (At45dCommand((NUTSPINODE *) dev->dev_icb, DFCMD_CONT_READ, pgn, 8, NULL, data, len)) {
  304. return -1;
  305. }
  306. return len;
  307. }
  308. /*!
  309. * \brief Write data to DataFlash memory.
  310. *
  311. * Each page will be automatically erased before writing the data. If the
  312. * last page is not completely filled with new data, the contents of
  313. * remaining bytes at the end of the page is undetermined.
  314. *
  315. * \param dev Specifies the registered DataFlash device.
  316. * \param pgn The page number.
  317. * \param data Points to the buffer that contains the bytes to be written.
  318. * \param len Number of bytes available in the buffer. This may be less
  319. * than the page size, in which case the remaining bytes of
  320. * the page will be set to 0xff.
  321. *
  322. * \return The number of bytes actually written. A return value of -1 indicates
  323. * an error.
  324. */
  325. int SpiAt45dPageWrite(NUTDEVICE * dev, uint32_t pgn, const void *data, int len)
  326. {
  327. int rc = -1;
  328. uint8_t *dp = (uint8_t *) data;
  329. int step;
  330. uint_fast8_t pshft;
  331. uint32_t limit;
  332. NUTBLOCKIO *blkio;
  333. NUTSPINODE *node;
  334. /* Sanity check. */
  335. if (len == 0) {
  336. return 0;
  337. }
  338. NUTASSERT(data != NULL);
  339. NUTASSERT(dev != NULL);
  340. NUTASSERT(dev->dev_dcb != NULL);
  341. NUTASSERT(dev->dev_icb != NULL);
  342. blkio = (NUTBLOCKIO *) dev->dev_dcb;
  343. node = (NUTSPINODE *) dev->dev_icb;
  344. NUTASSERT(blkio->blkio_info != NULL);
  345. pshft = ((AT45D_INFO *) blkio->blkio_info)->at45d_pshft;
  346. step = ((AT45D_INFO *) blkio->blkio_info)->at45d_psize;
  347. limit = ((AT45D_INFO *) blkio->blkio_info)->at45d_pages;
  348. while (len) {
  349. if (step > len) {
  350. step = len;
  351. }
  352. /* Copy data to the internal DataFlash RAM buffer. */
  353. if (At45dCommand(node, DFCMD_BUF1_WRITE, 0, 4, dp, NULL, step)) {
  354. break;
  355. }
  356. /* Erase the page and flash the RAM buffer contents. */
  357. if (At45dCommand(node, DFCMD_BUF1_FLASH, pgn << pshft, 4, NULL, NULL, 0)) {
  358. break;
  359. }
  360. if (At45dWaitReady(node, AT45_WRITE_POLLS, 1)) {
  361. break;
  362. }
  363. if (rc < 0) {
  364. rc = 0;
  365. }
  366. rc += step;
  367. dp += step;
  368. len -= step;
  369. if (++pgn >= limit) {
  370. break;
  371. }
  372. }
  373. return rc;
  374. }
  375. #ifdef __HARVARD_ARCH__
  376. int SpiAt45dPageWrite_P(NUTDEVICE * dev, uint32_t pgn, PGM_P data, int len)
  377. {
  378. return -1;
  379. }
  380. #endif
  381. /*!
  382. * \brief Return the number of pages.
  383. *
  384. * \param dev Specifies the registered DataFlash device.
  385. *
  386. * \return The total number of pages available in this device.
  387. */
  388. uint32_t SpiAt45dPages(NUTDEVICE * dev)
  389. {
  390. NUTBLOCKIO *blkio;
  391. NUTASSERT(dev != NULL);
  392. NUTASSERT(dev->dev_dcb != NULL);
  393. blkio = dev->dev_dcb;
  394. NUTASSERT(blkio->blkio_info != NULL);
  395. return ((AT45D_INFO *) blkio->blkio_info)->at45d_pages;
  396. }
  397. /*!
  398. * \brief Return the page size.
  399. *
  400. * \param dev Specifies the registered DataFlash device.
  401. *
  402. * \return The number of bytes per page.
  403. */
  404. int SpiAt45dPageSize(NUTDEVICE * dev)
  405. {
  406. NUTBLOCKIO *blkio;
  407. NUTASSERT(dev != NULL);
  408. NUTASSERT(dev->dev_dcb != NULL);
  409. blkio = dev->dev_dcb;
  410. NUTASSERT(blkio->blkio_info != NULL);
  411. return ((AT45D_INFO *) blkio->blkio_info)->at45d_psize;
  412. }
  413. /*!
  414. * \brief Perform block I/O device control functions.
  415. *
  416. * This function is called by the ioctl() function of the C runtime
  417. * library. Applications should not directly call this function.
  418. *
  419. * \param dev Identifies the device that receives the control command.
  420. * \param req Requested control command. May be set to one of the
  421. * following constants:
  422. * - \ref NUTBLKDEV_MEDIAAVAIL
  423. * - \ref NUTBLKDEV_MEDIACHANGE
  424. *
  425. * \param conf Points to a buffer that contains any data required for
  426. * the given control function or receives data from that
  427. * function.
  428. * \return 0 on success, -1 otherwise.
  429. */
  430. int SpiAt45dIOCtl(NUTDEVICE * dev, int req, void *conf)
  431. {
  432. int rc = 0;
  433. switch (req) {
  434. case NUTBLKDEV_MEDIAAVAIL:
  435. /* Modification required for DataFlash cards. */
  436. {
  437. int *flg;
  438. NUTASSERT(conf != NULL);
  439. flg = (int *) conf;
  440. *flg = 1;
  441. }
  442. break;
  443. case NUTBLKDEV_MEDIACHANGE:
  444. /* Modification required for DataFlash cards. */
  445. {
  446. int *flg;
  447. NUTASSERT(conf != NULL);
  448. flg = (int *) conf;
  449. *flg = 0;
  450. }
  451. break;
  452. default:
  453. rc = -1;
  454. break;
  455. }
  456. return rc;
  457. }