debugfs.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/mtd/spi-nor.h>
  3. #include <linux/spi/spi.h>
  4. #include <linux/spi/spi-mem.h>
  5. #include <linux/debugfs.h>
  6. #include "core.h"
  7. #define SPI_NOR_DEBUGFS_ROOT "spi-nor"
  8. #define SNOR_F_NAME(name) [ilog2(SNOR_F_##name)] = #name
  9. static const char *const snor_f_names[] = {
  10. SNOR_F_NAME(HAS_SR_TB),
  11. SNOR_F_NAME(NO_OP_CHIP_ERASE),
  12. SNOR_F_NAME(BROKEN_RESET),
  13. SNOR_F_NAME(4B_OPCODES),
  14. SNOR_F_NAME(HAS_4BAIT),
  15. SNOR_F_NAME(HAS_LOCK),
  16. SNOR_F_NAME(HAS_16BIT_SR),
  17. SNOR_F_NAME(NO_READ_CR),
  18. SNOR_F_NAME(HAS_SR_TB_BIT6),
  19. SNOR_F_NAME(HAS_4BIT_BP),
  20. SNOR_F_NAME(HAS_SR_BP3_BIT6),
  21. SNOR_F_NAME(IO_MODE_EN_VOLATILE),
  22. SNOR_F_NAME(SOFT_RESET),
  23. SNOR_F_NAME(SWP_IS_VOLATILE),
  24. SNOR_F_NAME(RWW),
  25. SNOR_F_NAME(ECC),
  26. };
  27. #undef SNOR_F_NAME
  28. static const char *spi_nor_protocol_name(enum spi_nor_protocol proto)
  29. {
  30. switch (proto) {
  31. case SNOR_PROTO_1_1_1: return "1S-1S-1S";
  32. case SNOR_PROTO_1_1_2: return "1S-1S-2S";
  33. case SNOR_PROTO_1_1_4: return "1S-1S-4S";
  34. case SNOR_PROTO_1_1_8: return "1S-1S-8S";
  35. case SNOR_PROTO_1_2_2: return "1S-2S-2S";
  36. case SNOR_PROTO_1_4_4: return "1S-4S-4S";
  37. case SNOR_PROTO_1_8_8: return "1S-8S-8S";
  38. case SNOR_PROTO_2_2_2: return "2S-2S-2S";
  39. case SNOR_PROTO_4_4_4: return "4S-4S-4S";
  40. case SNOR_PROTO_8_8_8: return "8S-8S-8S";
  41. case SNOR_PROTO_1_1_1_DTR: return "1D-1D-1D";
  42. case SNOR_PROTO_1_2_2_DTR: return "1D-2D-2D";
  43. case SNOR_PROTO_1_4_4_DTR: return "1D-4D-4D";
  44. case SNOR_PROTO_1_8_8_DTR: return "1D-8D-8D";
  45. case SNOR_PROTO_8_8_8_DTR: return "8D-8D-8D";
  46. }
  47. return "<unknown>";
  48. }
  49. static void spi_nor_print_flags(struct seq_file *s, unsigned long flags,
  50. const char *const *names, int names_len)
  51. {
  52. bool sep = false;
  53. int i;
  54. for (i = 0; i < sizeof(flags) * BITS_PER_BYTE; i++) {
  55. if (!(flags & BIT(i)))
  56. continue;
  57. if (sep)
  58. seq_puts(s, " | ");
  59. sep = true;
  60. if (i < names_len && names[i])
  61. seq_puts(s, names[i]);
  62. else
  63. seq_printf(s, "1<<%d", i);
  64. }
  65. }
  66. static int spi_nor_params_show(struct seq_file *s, void *data)
  67. {
  68. struct spi_nor *nor = s->private;
  69. struct spi_nor_flash_parameter *params = nor->params;
  70. struct spi_nor_erase_map *erase_map = &params->erase_map;
  71. struct spi_nor_erase_region *region;
  72. const struct flash_info *info = nor->info;
  73. char buf[16], *str;
  74. int i;
  75. seq_printf(s, "name\t\t%s\n", info->name);
  76. seq_printf(s, "id\t\t%*ph\n", info->id_len, info->id);
  77. string_get_size(params->size, 1, STRING_UNITS_2, buf, sizeof(buf));
  78. seq_printf(s, "size\t\t%s\n", buf);
  79. seq_printf(s, "write size\t%u\n", params->writesize);
  80. seq_printf(s, "page size\t%u\n", params->page_size);
  81. seq_printf(s, "address nbytes\t%u\n", nor->addr_nbytes);
  82. seq_puts(s, "flags\t\t");
  83. spi_nor_print_flags(s, nor->flags, snor_f_names, sizeof(snor_f_names));
  84. seq_puts(s, "\n");
  85. seq_puts(s, "\nopcodes\n");
  86. seq_printf(s, " read\t\t0x%02x\n", nor->read_opcode);
  87. seq_printf(s, " dummy cycles\t%u\n", nor->read_dummy);
  88. seq_printf(s, " erase\t\t0x%02x\n", nor->erase_opcode);
  89. seq_printf(s, " program\t0x%02x\n", nor->program_opcode);
  90. switch (nor->cmd_ext_type) {
  91. case SPI_NOR_EXT_NONE:
  92. str = "none";
  93. break;
  94. case SPI_NOR_EXT_REPEAT:
  95. str = "repeat";
  96. break;
  97. case SPI_NOR_EXT_INVERT:
  98. str = "invert";
  99. break;
  100. default:
  101. str = "<unknown>";
  102. break;
  103. }
  104. seq_printf(s, " 8D extension\t%s\n", str);
  105. seq_puts(s, "\nprotocols\n");
  106. seq_printf(s, " read\t\t%s\n",
  107. spi_nor_protocol_name(nor->read_proto));
  108. seq_printf(s, " write\t\t%s\n",
  109. spi_nor_protocol_name(nor->write_proto));
  110. seq_printf(s, " register\t%s\n",
  111. spi_nor_protocol_name(nor->reg_proto));
  112. seq_puts(s, "\nerase commands\n");
  113. for (i = 0; i < SNOR_ERASE_TYPE_MAX; i++) {
  114. struct spi_nor_erase_type *et = &erase_map->erase_type[i];
  115. if (et->size) {
  116. string_get_size(et->size, 1, STRING_UNITS_2, buf,
  117. sizeof(buf));
  118. seq_printf(s, " %02x (%s) [%d]\n", et->opcode, buf, i);
  119. }
  120. }
  121. if (!(nor->flags & SNOR_F_NO_OP_CHIP_ERASE)) {
  122. string_get_size(params->size, 1, STRING_UNITS_2, buf, sizeof(buf));
  123. seq_printf(s, " %02x (%s)\n", SPINOR_OP_CHIP_ERASE, buf);
  124. }
  125. seq_puts(s, "\nsector map\n");
  126. seq_puts(s, " region (in hex) | erase mask | flags\n");
  127. seq_puts(s, " ------------------+------------+----------\n");
  128. for (region = erase_map->regions;
  129. region;
  130. region = spi_nor_region_next(region)) {
  131. u64 start = region->offset & ~SNOR_ERASE_FLAGS_MASK;
  132. u64 flags = region->offset & SNOR_ERASE_FLAGS_MASK;
  133. u64 end = start + region->size - 1;
  134. seq_printf(s, " %08llx-%08llx | [%c%c%c%c] | %s\n",
  135. start, end,
  136. flags & BIT(0) ? '0' : ' ',
  137. flags & BIT(1) ? '1' : ' ',
  138. flags & BIT(2) ? '2' : ' ',
  139. flags & BIT(3) ? '3' : ' ',
  140. flags & SNOR_OVERLAID_REGION ? "overlaid" : "");
  141. }
  142. return 0;
  143. }
  144. DEFINE_SHOW_ATTRIBUTE(spi_nor_params);
  145. static void spi_nor_print_read_cmd(struct seq_file *s, u32 cap,
  146. struct spi_nor_read_command *cmd)
  147. {
  148. seq_printf(s, " %s%s\n", spi_nor_protocol_name(cmd->proto),
  149. cap == SNOR_HWCAPS_READ_FAST ? " (fast read)" : "");
  150. seq_printf(s, " opcode\t0x%02x\n", cmd->opcode);
  151. seq_printf(s, " mode cycles\t%u\n", cmd->num_mode_clocks);
  152. seq_printf(s, " dummy cycles\t%u\n", cmd->num_wait_states);
  153. }
  154. static void spi_nor_print_pp_cmd(struct seq_file *s,
  155. struct spi_nor_pp_command *cmd)
  156. {
  157. seq_printf(s, " %s\n", spi_nor_protocol_name(cmd->proto));
  158. seq_printf(s, " opcode\t0x%02x\n", cmd->opcode);
  159. }
  160. static int spi_nor_capabilities_show(struct seq_file *s, void *data)
  161. {
  162. struct spi_nor *nor = s->private;
  163. struct spi_nor_flash_parameter *params = nor->params;
  164. u32 hwcaps = params->hwcaps.mask;
  165. int i, cmd;
  166. seq_puts(s, "Supported read modes by the flash\n");
  167. for (i = 0; i < sizeof(hwcaps) * BITS_PER_BYTE; i++) {
  168. if (!(hwcaps & BIT(i)))
  169. continue;
  170. cmd = spi_nor_hwcaps_read2cmd(BIT(i));
  171. if (cmd < 0)
  172. continue;
  173. spi_nor_print_read_cmd(s, BIT(i), &params->reads[cmd]);
  174. hwcaps &= ~BIT(i);
  175. }
  176. seq_puts(s, "\nSupported page program modes by the flash\n");
  177. for (i = 0; i < sizeof(hwcaps) * BITS_PER_BYTE; i++) {
  178. if (!(hwcaps & BIT(i)))
  179. continue;
  180. cmd = spi_nor_hwcaps_pp2cmd(BIT(i));
  181. if (cmd < 0)
  182. continue;
  183. spi_nor_print_pp_cmd(s, &params->page_programs[cmd]);
  184. hwcaps &= ~BIT(i);
  185. }
  186. if (hwcaps)
  187. seq_printf(s, "\nunknown hwcaps 0x%x\n", hwcaps);
  188. return 0;
  189. }
  190. DEFINE_SHOW_ATTRIBUTE(spi_nor_capabilities);
  191. static void spi_nor_debugfs_unregister(void *data)
  192. {
  193. struct spi_nor *nor = data;
  194. debugfs_remove(nor->debugfs_root);
  195. nor->debugfs_root = NULL;
  196. }
  197. static struct dentry *rootdir;
  198. void spi_nor_debugfs_register(struct spi_nor *nor)
  199. {
  200. struct dentry *d;
  201. int ret;
  202. if (!rootdir)
  203. rootdir = debugfs_create_dir(SPI_NOR_DEBUGFS_ROOT, NULL);
  204. ret = devm_add_action(nor->dev, spi_nor_debugfs_unregister, nor);
  205. if (ret)
  206. return;
  207. d = debugfs_create_dir(dev_name(nor->dev), rootdir);
  208. nor->debugfs_root = d;
  209. debugfs_create_file("params", 0444, d, nor, &spi_nor_params_fops);
  210. debugfs_create_file("capabilities", 0444, d, nor,
  211. &spi_nor_capabilities_fops);
  212. }
  213. void spi_nor_debugfs_shutdown(void)
  214. {
  215. debugfs_remove(rootdir);
  216. }