qinfo_probe.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /*
  2. * Probing flash chips with QINFO records.
  3. * (C) 2008 Korolev Alexey <akorolev@infradead.org>
  4. * (C) 2008 Vasiliy Leonenko <vasiliy.leonenko@gmail.com>
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version 2
  9. * of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  19. * 02110-1301, USA.
  20. */
  21. #include <linux/module.h>
  22. #include <linux/types.h>
  23. #include <linux/kernel.h>
  24. #include <linux/init.h>
  25. #include <linux/errno.h>
  26. #include <linux/slab.h>
  27. #include <linux/interrupt.h>
  28. #include <linux/mtd/xip.h>
  29. #include <linux/mtd/map.h>
  30. #include <linux/mtd/pfow.h>
  31. #include <linux/mtd/qinfo.h>
  32. static int lpddr_chip_setup(struct map_info *map, struct lpddr_private *lpddr);
  33. struct mtd_info *lpddr_probe(struct map_info *map);
  34. static struct lpddr_private *lpddr_probe_chip(struct map_info *map);
  35. static int lpddr_pfow_present(struct map_info *map,
  36. struct lpddr_private *lpddr);
  37. static struct qinfo_query_info qinfo_array[] = {
  38. /* General device info */
  39. {0, 0, "DevSizeShift", "Device size 2^n bytes"},
  40. {0, 3, "BufSizeShift", "Program buffer size 2^n bytes"},
  41. /* Erase block information */
  42. {1, 1, "TotalBlocksNum", "Total number of blocks"},
  43. {1, 2, "UniformBlockSizeShift", "Uniform block size 2^n bytes"},
  44. /* Partition information */
  45. {2, 1, "HWPartsNum", "Number of hardware partitions"},
  46. /* Optional features */
  47. {5, 1, "SuspEraseSupp", "Suspend erase supported"},
  48. /* Operation typical time */
  49. {10, 0, "SingleWordProgTime", "Single word program 2^n u-sec"},
  50. {10, 1, "ProgBufferTime", "Program buffer write 2^n u-sec"},
  51. {10, 2, "BlockEraseTime", "Block erase 2^n m-sec"},
  52. {10, 3, "FullChipEraseTime", "Full chip erase 2^n m-sec"},
  53. };
  54. static long lpddr_get_qinforec_pos(struct map_info *map, char *id_str)
  55. {
  56. int qinfo_lines = ARRAY_SIZE(qinfo_array);
  57. int i;
  58. int bankwidth = map_bankwidth(map) * 8;
  59. int major, minor;
  60. for (i = 0; i < qinfo_lines; i++) {
  61. if (strcmp(id_str, qinfo_array[i].id_str) == 0) {
  62. major = qinfo_array[i].major & ((1 << bankwidth) - 1);
  63. minor = qinfo_array[i].minor & ((1 << bankwidth) - 1);
  64. return minor | (major << bankwidth);
  65. }
  66. }
  67. printk(KERN_ERR"%s qinfo id string is wrong! \n", map->name);
  68. BUG();
  69. return -1;
  70. }
  71. static uint16_t lpddr_info_query(struct map_info *map, char *id_str)
  72. {
  73. unsigned int dsr, val;
  74. int bits_per_chip = map_bankwidth(map) * 8;
  75. unsigned long adr = lpddr_get_qinforec_pos(map, id_str);
  76. int attempts = 20;
  77. /* Write a request for the PFOW record */
  78. map_write(map, CMD(LPDDR_INFO_QUERY),
  79. map->pfow_base + PFOW_COMMAND_CODE);
  80. map_write(map, CMD(adr & ((1 << bits_per_chip) - 1)),
  81. map->pfow_base + PFOW_COMMAND_ADDRESS_L);
  82. map_write(map, CMD(adr >> bits_per_chip),
  83. map->pfow_base + PFOW_COMMAND_ADDRESS_H);
  84. map_write(map, CMD(LPDDR_START_EXECUTION),
  85. map->pfow_base + PFOW_COMMAND_EXECUTE);
  86. while ((attempts--) > 0) {
  87. dsr = CMDVAL(map_read(map, map->pfow_base + PFOW_DSR));
  88. if (dsr & DSR_READY_STATUS)
  89. break;
  90. udelay(10);
  91. }
  92. val = CMDVAL(map_read(map, map->pfow_base + PFOW_COMMAND_DATA));
  93. return val;
  94. }
  95. static int lpddr_pfow_present(struct map_info *map, struct lpddr_private *lpddr)
  96. {
  97. map_word pfow_val[4];
  98. /* Check identification string */
  99. pfow_val[0] = map_read(map, map->pfow_base + PFOW_QUERY_STRING_P);
  100. pfow_val[1] = map_read(map, map->pfow_base + PFOW_QUERY_STRING_F);
  101. pfow_val[2] = map_read(map, map->pfow_base + PFOW_QUERY_STRING_O);
  102. pfow_val[3] = map_read(map, map->pfow_base + PFOW_QUERY_STRING_W);
  103. if (!map_word_equal(map, CMD('P'), pfow_val[0]))
  104. goto out;
  105. if (!map_word_equal(map, CMD('F'), pfow_val[1]))
  106. goto out;
  107. if (!map_word_equal(map, CMD('O'), pfow_val[2]))
  108. goto out;
  109. if (!map_word_equal(map, CMD('W'), pfow_val[3]))
  110. goto out;
  111. return 1; /* "PFOW" is found */
  112. out:
  113. printk(KERN_WARNING"%s: PFOW string at 0x%lx is not found \n",
  114. map->name, map->pfow_base);
  115. return 0;
  116. }
  117. static int lpddr_chip_setup(struct map_info *map, struct lpddr_private *lpddr)
  118. {
  119. lpddr->qinfo = kzalloc(sizeof(struct qinfo_chip), GFP_KERNEL);
  120. if (!lpddr->qinfo)
  121. return 0;
  122. /* Get the ManuID */
  123. lpddr->ManufactId = CMDVAL(map_read(map, map->pfow_base + PFOW_MANUFACTURER_ID));
  124. /* Get the DeviceID */
  125. lpddr->DevId = CMDVAL(map_read(map, map->pfow_base + PFOW_DEVICE_ID));
  126. /* read parameters from chip qinfo table */
  127. lpddr->qinfo->DevSizeShift = lpddr_info_query(map, "DevSizeShift");
  128. lpddr->qinfo->TotalBlocksNum = lpddr_info_query(map, "TotalBlocksNum");
  129. lpddr->qinfo->BufSizeShift = lpddr_info_query(map, "BufSizeShift");
  130. lpddr->qinfo->HWPartsNum = lpddr_info_query(map, "HWPartsNum");
  131. lpddr->qinfo->UniformBlockSizeShift =
  132. lpddr_info_query(map, "UniformBlockSizeShift");
  133. lpddr->qinfo->SuspEraseSupp = lpddr_info_query(map, "SuspEraseSupp");
  134. lpddr->qinfo->SingleWordProgTime =
  135. lpddr_info_query(map, "SingleWordProgTime");
  136. lpddr->qinfo->ProgBufferTime = lpddr_info_query(map, "ProgBufferTime");
  137. lpddr->qinfo->BlockEraseTime = lpddr_info_query(map, "BlockEraseTime");
  138. return 1;
  139. }
  140. static struct lpddr_private *lpddr_probe_chip(struct map_info *map)
  141. {
  142. struct lpddr_private lpddr;
  143. struct lpddr_private *retlpddr;
  144. int numvirtchips;
  145. if ((map->pfow_base + 0x1000) >= map->size) {
  146. printk(KERN_NOTICE"%s Probe at base (0x%08lx) past the end of"
  147. "the map(0x%08lx)\n", map->name,
  148. (unsigned long)map->pfow_base, map->size - 1);
  149. return NULL;
  150. }
  151. memset(&lpddr, 0, sizeof(struct lpddr_private));
  152. if (!lpddr_pfow_present(map, &lpddr))
  153. return NULL;
  154. if (!lpddr_chip_setup(map, &lpddr))
  155. return NULL;
  156. /* Ok so we found a chip */
  157. lpddr.chipshift = lpddr.qinfo->DevSizeShift;
  158. lpddr.numchips = 1;
  159. numvirtchips = lpddr.numchips * lpddr.qinfo->HWPartsNum;
  160. retlpddr = kzalloc(sizeof(struct lpddr_private) +
  161. numvirtchips * sizeof(struct flchip), GFP_KERNEL);
  162. if (!retlpddr)
  163. return NULL;
  164. memcpy(retlpddr, &lpddr, sizeof(struct lpddr_private));
  165. retlpddr->numchips = numvirtchips;
  166. retlpddr->chipshift = retlpddr->qinfo->DevSizeShift -
  167. __ffs(retlpddr->qinfo->HWPartsNum);
  168. return retlpddr;
  169. }
  170. struct mtd_info *lpddr_probe(struct map_info *map)
  171. {
  172. struct mtd_info *mtd = NULL;
  173. struct lpddr_private *lpddr;
  174. /* First probe the map to see if we havecan open PFOW here */
  175. lpddr = lpddr_probe_chip(map);
  176. if (!lpddr)
  177. return NULL;
  178. map->fldrv_priv = lpddr;
  179. mtd = lpddr_cmdset(map);
  180. if (mtd) {
  181. if (mtd->size > map->size) {
  182. printk(KERN_WARNING "Reducing visibility of %ldKiB chip"
  183. "to %ldKiB\n", (unsigned long)mtd->size >> 10,
  184. (unsigned long)map->size >> 10);
  185. mtd->size = map->size;
  186. }
  187. return mtd;
  188. }
  189. kfree(lpddr->qinfo);
  190. kfree(lpddr);
  191. map->fldrv_priv = NULL;
  192. return NULL;
  193. }
  194. static struct mtd_chip_driver lpddr_chipdrv = {
  195. .probe = lpddr_probe,
  196. .name = "qinfo_probe",
  197. .module = THIS_MODULE
  198. };
  199. static int __init lpddr_probe_init(void)
  200. {
  201. register_mtd_chip_driver(&lpddr_chipdrv);
  202. return 0;
  203. }
  204. static void __exit lpddr_probe_exit(void)
  205. {
  206. unregister_mtd_chip_driver(&lpddr_chipdrv);
  207. }
  208. module_init(lpddr_probe_init);
  209. module_exit(lpddr_probe_exit);
  210. MODULE_LICENSE("GPL");
  211. MODULE_AUTHOR("Vasiliy Leonenko <vasiliy.leonenko@gmail.com>");
  212. MODULE_DESCRIPTION("Driver to probe qinfo flash chips");