spi_fh_adapt.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /*
  2. * This file is part of FH8620 BSP for RT-Thread distribution.
  3. *
  4. * Copyright (c) 2016 Shanghai Fullhan Microelectronics Co., Ltd.
  5. * All rights reserved
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License along
  18. * with this program; if not, write to the Free Software Foundation, Inc.,
  19. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  20. *
  21. * Visit http://www.fullhan.com to get contact with Fullhan.
  22. *
  23. * Change Logs:
  24. * Date Author Notes
  25. */
  26. /*
  27. * spi_fh_adapt.c
  28. *
  29. * Created on: Mar 2, 2016
  30. * Author: duobao
  31. */
  32. #include <stdint.h>
  33. #include "spi_fh_adapt.h"
  34. #include "board_info.h"
  35. #ifdef RT_USING_W25QXX
  36. #include "spi_flash_w25qxx.h"
  37. #endif
  38. #ifdef RT_USING_AT45DBXX
  39. #include "spi_flash_at45dbxx.h"
  40. #endif
  41. #ifdef RT_USING_SST25VFXX
  42. #include "spi_flash_sst25vfxx.h"
  43. #endif
  44. #ifdef RT_USING_GD
  45. #include "spi_flash_gd.h"
  46. #endif
  47. #ifdef RT_USING_FLASH_DEFAULT
  48. #include "spi_flash_default.h"
  49. #endif
  50. #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
  51. #define WX_MANU_ID 0xEF
  52. #define AT_MANU_ID 0x1F /* atmel */
  53. #define SST_MANU_ID 0xBF
  54. #define GD_MANU_ID 0xC8
  55. #define SPI_ADAPT_DEBUG
  56. #ifdef SPI_ADAPT_DEBUG
  57. #define CMD_JEDEC_ID 0x9f
  58. #define FH_SPI_ADAPT_DEBUG(fmt, args...) \
  59. rt_kprintf(fmt,##args);
  60. #else
  61. #define FH_SPI_ADAPT_DEBUG(fmt, args...)
  62. #endif
  63. struct fh_flash_id{
  64. unsigned char id;
  65. rt_err_t (*fh_flash_init)(struct flash_platform_data *plat_flash);
  66. char *name;
  67. };
  68. const struct fh_flash_id id_map[] = {
  69. #ifdef RT_USING_W25QXX
  70. WX_MANU_ID,w25qxx_init,"winbond",
  71. #endif
  72. #ifdef RT_USING_AT45DBXX
  73. AT_MANU_ID,at45dbxx_init,"atmel",
  74. #endif
  75. #ifdef RT_USING_SST25VFXX
  76. SST_MANU_ID,sst25vfxx_init,"SST",
  77. #endif
  78. #ifdef RT_USING_GD
  79. GD_MANU_ID,gd_init,"GD",
  80. #endif
  81. };
  82. struct fh_flash_id * fh_flash_check_id_map(unsigned char id){
  83. struct fh_flash_id *p_map = RT_NULL;
  84. unsigned int i;
  85. for (i = 0; i < ARRAY_SIZE(id_map); i++) {
  86. p_map = (struct fh_flash_id *)&id_map[i];
  87. if (p_map->id == id){
  88. return p_map;
  89. }
  90. }
  91. return RT_NULL;
  92. }
  93. int fh_flash_adapt_probe(void *priv_data)
  94. {
  95. struct flash_platform_data *plat_flash = priv_data;
  96. const char * flash_device_name = plat_flash->flash_name;
  97. const char * spi_device_name = plat_flash->spi_name;
  98. struct rt_spi_device * rt_spi_device;
  99. struct fh_flash_id * flash_model;
  100. rt_spi_device = (struct rt_spi_device *)rt_device_find(spi_device_name);
  101. if(rt_spi_device == RT_NULL)
  102. {
  103. rt_kprintf("spi device %s not found!\r\n", spi_device_name);
  104. return -RT_ENOSYS;
  105. }
  106. /* config spi */
  107. {
  108. struct rt_spi_configuration cfg;
  109. cfg.data_width = 8;
  110. cfg.mode = RT_SPI_MODE_0 | RT_SPI_MSB; /* SPI Compatible: Mode 0 and Mode 3 */
  111. cfg.max_hz = 50 * 1000 * 1000; /* 50M */
  112. rt_spi_configure(rt_spi_device, &cfg);
  113. }
  114. /* init flash */
  115. rt_uint8_t cmd;
  116. rt_uint8_t id_recv[3];
  117. uint16_t memory_type_capacity;
  118. rt_err_t ret;
  119. cmd = 0xFF; /* reset SPI FLASH, cancel all cmd in processing. */
  120. rt_spi_send(rt_spi_device, &cmd, 1);
  121. /* read flash id */
  122. cmd = CMD_JEDEC_ID;
  123. rt_spi_send_then_recv(rt_spi_device, &cmd, 1, id_recv, 3);
  124. //if the flash is already connect.
  125. if(id_recv[0] != 0xff){
  126. flash_model =fh_flash_check_id_map(id_recv[0]);
  127. if(flash_model){
  128. ret = flash_model->fh_flash_init(plat_flash);
  129. if(ret != RT_EOK){
  130. rt_kprintf("flash:%s init error\n",flash_model->name);
  131. rt_kprintf("use default flash ops..\n");
  132. //flash_model->fh_flash_adapt_init =flash_default_init;
  133. ret = flash_default_init(plat_flash);
  134. }
  135. }
  136. else{
  137. rt_kprintf(
  138. "use default flash ops...\nunrecognized flash id is :%02X %02X %02X\n",
  139. id_recv[0], id_recv[1], id_recv[2]);
  140. ret = flash_default_init(plat_flash);
  141. }
  142. int i;
  143. for(i=0; i<plat_flash->nr_parts; i++)
  144. {
  145. fh_spi_partition_register(plat_flash->flash_name, &plat_flash->parts[i]);
  146. }
  147. return ret;
  148. }
  149. else{
  150. rt_kprintf("please check if you connect the flash already...\n");
  151. return RT_ENOSYS;
  152. }
  153. }
  154. int fh_flash_adapt_exit(void *priv_data)
  155. {
  156. return 0;
  157. }
  158. struct fh_board_ops flash_driver_ops =
  159. {
  160. .probe = fh_flash_adapt_probe,
  161. .exit = fh_flash_adapt_exit,
  162. };
  163. rt_err_t fh_flash_adapt_init(void)
  164. {
  165. fh_board_driver_register("fh_flash", &flash_driver_ops);
  166. return 0;
  167. }