debugfs.c 6.9 KB

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