debugfs.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. /*
  2. * Debugfs support for hosts and cards
  3. *
  4. * Copyright (C) 2008 Atmel Corporation
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. */
  10. #include <linux/moduleparam.h>
  11. #include <linux/export.h>
  12. #include <linux/debugfs.h>
  13. #include <linux/fs.h>
  14. #include <linux/seq_file.h>
  15. #include <linux/slab.h>
  16. #include <linux/stat.h>
  17. #include <linux/fault-inject.h>
  18. #include <linux/mmc/card.h>
  19. #include <linux/mmc/host.h>
  20. #include "core.h"
  21. #include "card.h"
  22. #include "host.h"
  23. #include "mmc_ops.h"
  24. #ifdef CONFIG_FAIL_MMC_REQUEST
  25. static DECLARE_FAULT_ATTR(fail_default_attr);
  26. static char *fail_request;
  27. module_param(fail_request, charp, 0);
  28. #endif /* CONFIG_FAIL_MMC_REQUEST */
  29. /* The debugfs functions are optimized away when CONFIG_DEBUG_FS isn't set. */
  30. static int mmc_ios_show(struct seq_file *s, void *data)
  31. {
  32. static const char *vdd_str[] = {
  33. [8] = "2.0",
  34. [9] = "2.1",
  35. [10] = "2.2",
  36. [11] = "2.3",
  37. [12] = "2.4",
  38. [13] = "2.5",
  39. [14] = "2.6",
  40. [15] = "2.7",
  41. [16] = "2.8",
  42. [17] = "2.9",
  43. [18] = "3.0",
  44. [19] = "3.1",
  45. [20] = "3.2",
  46. [21] = "3.3",
  47. [22] = "3.4",
  48. [23] = "3.5",
  49. [24] = "3.6",
  50. };
  51. struct mmc_host *host = s->private;
  52. struct mmc_ios *ios = &host->ios;
  53. const char *str;
  54. seq_printf(s, "clock:\t\t%u Hz\n", ios->clock);
  55. if (host->actual_clock)
  56. seq_printf(s, "actual clock:\t%u Hz\n", host->actual_clock);
  57. seq_printf(s, "vdd:\t\t%u ", ios->vdd);
  58. if ((1 << ios->vdd) & MMC_VDD_165_195)
  59. seq_printf(s, "(1.65 - 1.95 V)\n");
  60. else if (ios->vdd < (ARRAY_SIZE(vdd_str) - 1)
  61. && vdd_str[ios->vdd] && vdd_str[ios->vdd + 1])
  62. seq_printf(s, "(%s ~ %s V)\n", vdd_str[ios->vdd],
  63. vdd_str[ios->vdd + 1]);
  64. else
  65. seq_printf(s, "(invalid)\n");
  66. switch (ios->bus_mode) {
  67. case MMC_BUSMODE_OPENDRAIN:
  68. str = "open drain";
  69. break;
  70. case MMC_BUSMODE_PUSHPULL:
  71. str = "push-pull";
  72. break;
  73. default:
  74. str = "invalid";
  75. break;
  76. }
  77. seq_printf(s, "bus mode:\t%u (%s)\n", ios->bus_mode, str);
  78. switch (ios->chip_select) {
  79. case MMC_CS_DONTCARE:
  80. str = "don't care";
  81. break;
  82. case MMC_CS_HIGH:
  83. str = "active high";
  84. break;
  85. case MMC_CS_LOW:
  86. str = "active low";
  87. break;
  88. default:
  89. str = "invalid";
  90. break;
  91. }
  92. seq_printf(s, "chip select:\t%u (%s)\n", ios->chip_select, str);
  93. switch (ios->power_mode) {
  94. case MMC_POWER_OFF:
  95. str = "off";
  96. break;
  97. case MMC_POWER_UP:
  98. str = "up";
  99. break;
  100. case MMC_POWER_ON:
  101. str = "on";
  102. break;
  103. default:
  104. str = "invalid";
  105. break;
  106. }
  107. seq_printf(s, "power mode:\t%u (%s)\n", ios->power_mode, str);
  108. seq_printf(s, "bus width:\t%u (%u bits)\n",
  109. ios->bus_width, 1 << ios->bus_width);
  110. switch (ios->timing) {
  111. case MMC_TIMING_LEGACY:
  112. str = "legacy";
  113. break;
  114. case MMC_TIMING_MMC_HS:
  115. str = "mmc high-speed";
  116. break;
  117. case MMC_TIMING_SD_HS:
  118. str = "sd high-speed";
  119. break;
  120. case MMC_TIMING_UHS_SDR12:
  121. str = "sd uhs SDR12";
  122. break;
  123. case MMC_TIMING_UHS_SDR25:
  124. str = "sd uhs SDR25";
  125. break;
  126. case MMC_TIMING_UHS_SDR50:
  127. str = "sd uhs SDR50";
  128. break;
  129. case MMC_TIMING_UHS_SDR104:
  130. str = "sd uhs SDR104";
  131. break;
  132. case MMC_TIMING_UHS_DDR50:
  133. str = "sd uhs DDR50";
  134. break;
  135. case MMC_TIMING_MMC_DDR52:
  136. str = "mmc DDR52";
  137. break;
  138. case MMC_TIMING_MMC_HS200:
  139. str = "mmc HS200";
  140. break;
  141. case MMC_TIMING_MMC_HS400:
  142. str = mmc_card_hs400es(host->card) ?
  143. "mmc HS400 enhanced strobe" : "mmc HS400";
  144. break;
  145. default:
  146. str = "invalid";
  147. break;
  148. }
  149. seq_printf(s, "timing spec:\t%u (%s)\n", ios->timing, str);
  150. switch (ios->signal_voltage) {
  151. case MMC_SIGNAL_VOLTAGE_330:
  152. str = "3.30 V";
  153. break;
  154. case MMC_SIGNAL_VOLTAGE_180:
  155. str = "1.80 V";
  156. break;
  157. case MMC_SIGNAL_VOLTAGE_120:
  158. str = "1.20 V";
  159. break;
  160. default:
  161. str = "invalid";
  162. break;
  163. }
  164. seq_printf(s, "signal voltage:\t%u (%s)\n", ios->signal_voltage, str);
  165. switch (ios->drv_type) {
  166. case MMC_SET_DRIVER_TYPE_A:
  167. str = "driver type A";
  168. break;
  169. case MMC_SET_DRIVER_TYPE_B:
  170. str = "driver type B";
  171. break;
  172. case MMC_SET_DRIVER_TYPE_C:
  173. str = "driver type C";
  174. break;
  175. case MMC_SET_DRIVER_TYPE_D:
  176. str = "driver type D";
  177. break;
  178. default:
  179. str = "invalid";
  180. break;
  181. }
  182. seq_printf(s, "driver type:\t%u (%s)\n", ios->drv_type, str);
  183. return 0;
  184. }
  185. static int mmc_ios_open(struct inode *inode, struct file *file)
  186. {
  187. return single_open(file, mmc_ios_show, inode->i_private);
  188. }
  189. static const struct file_operations mmc_ios_fops = {
  190. .open = mmc_ios_open,
  191. .read = seq_read,
  192. .llseek = seq_lseek,
  193. .release = single_release,
  194. };
  195. static int mmc_clock_opt_get(void *data, u64 *val)
  196. {
  197. struct mmc_host *host = data;
  198. *val = host->ios.clock;
  199. return 0;
  200. }
  201. static int mmc_clock_opt_set(void *data, u64 val)
  202. {
  203. struct mmc_host *host = data;
  204. /* We need this check due to input value is u64
  205. * error case: val < host->f_min; the case will trigger
  206. * IO hang
  207. */
  208. if (val > host->f_max || val < host->f_min)
  209. return -EINVAL;
  210. mmc_claim_host(host);
  211. mmc_set_clock(host, (unsigned int) val);
  212. mmc_release_host(host);
  213. return 0;
  214. }
  215. DEFINE_SIMPLE_ATTRIBUTE(mmc_clock_fops, mmc_clock_opt_get, mmc_clock_opt_set,
  216. "%llu\n");
  217. void mmc_add_host_debugfs(struct mmc_host *host)
  218. {
  219. struct dentry *root;
  220. root = debugfs_create_dir(mmc_hostname(host), NULL);
  221. if (IS_ERR(root))
  222. /* Don't complain -- debugfs just isn't enabled */
  223. return;
  224. if (!root)
  225. /* Complain -- debugfs is enabled, but it failed to
  226. * create the directory. */
  227. goto err_root;
  228. host->debugfs_root = root;
  229. if (!debugfs_create_file("ios", S_IRUSR, root, host, &mmc_ios_fops))
  230. goto err_node;
  231. if (!debugfs_create_file("clock", S_IRUSR | S_IWUSR, root, host,
  232. &mmc_clock_fops))
  233. goto err_node;
  234. #ifdef CONFIG_FAIL_MMC_REQUEST
  235. if (fail_request)
  236. setup_fault_attr(&fail_default_attr, fail_request);
  237. host->fail_mmc_request = fail_default_attr;
  238. if (IS_ERR(fault_create_debugfs_attr("fail_mmc_request",
  239. root,
  240. &host->fail_mmc_request)))
  241. goto err_node;
  242. #endif
  243. return;
  244. err_node:
  245. debugfs_remove_recursive(root);
  246. host->debugfs_root = NULL;
  247. err_root:
  248. dev_err(&host->class_dev, "failed to initialize debugfs\n");
  249. }
  250. void mmc_remove_host_debugfs(struct mmc_host *host)
  251. {
  252. debugfs_remove_recursive(host->debugfs_root);
  253. }
  254. void mmc_add_card_debugfs(struct mmc_card *card)
  255. {
  256. struct mmc_host *host = card->host;
  257. struct dentry *root;
  258. if (!host->debugfs_root)
  259. return;
  260. root = debugfs_create_dir(mmc_card_id(card), host->debugfs_root);
  261. if (IS_ERR(root))
  262. /* Don't complain -- debugfs just isn't enabled */
  263. return;
  264. if (!root)
  265. /* Complain -- debugfs is enabled, but it failed to
  266. * create the directory. */
  267. goto err;
  268. card->debugfs_root = root;
  269. if (!debugfs_create_x32("state", S_IRUSR, root, &card->state))
  270. goto err;
  271. return;
  272. err:
  273. debugfs_remove_recursive(root);
  274. card->debugfs_root = NULL;
  275. dev_err(&card->dev, "failed to initialize debugfs\n");
  276. }
  277. void mmc_remove_card_debugfs(struct mmc_card *card)
  278. {
  279. debugfs_remove_recursive(card->debugfs_root);
  280. card->debugfs_root = NULL;
  281. }