debugfs.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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/debugfs.h>
  12. #include <linux/fs.h>
  13. #include <linux/seq_file.h>
  14. #include <linux/slab.h>
  15. #include <linux/stat.h>
  16. #include <linux/fault-inject.h>
  17. #include <linux/mmc/card.h>
  18. #include <linux/mmc/host.h>
  19. #include "core.h"
  20. #include "mmc_ops.h"
  21. #ifdef CONFIG_FAIL_MMC_REQUEST
  22. static DECLARE_FAULT_ATTR(fail_default_attr);
  23. static char *fail_request;
  24. module_param(fail_request, charp, 0);
  25. #endif /* CONFIG_FAIL_MMC_REQUEST */
  26. /* The debugfs functions are optimized away when CONFIG_DEBUG_FS isn't set. */
  27. static int mmc_ios_show(struct seq_file *s, void *data)
  28. {
  29. static const char *vdd_str[] = {
  30. [8] = "2.0",
  31. [9] = "2.1",
  32. [10] = "2.2",
  33. [11] = "2.3",
  34. [12] = "2.4",
  35. [13] = "2.5",
  36. [14] = "2.6",
  37. [15] = "2.7",
  38. [16] = "2.8",
  39. [17] = "2.9",
  40. [18] = "3.0",
  41. [19] = "3.1",
  42. [20] = "3.2",
  43. [21] = "3.3",
  44. [22] = "3.4",
  45. [23] = "3.5",
  46. [24] = "3.6",
  47. };
  48. struct mmc_host *host = s->private;
  49. struct mmc_ios *ios = &host->ios;
  50. const char *str;
  51. seq_printf(s, "clock:\t\t%u Hz\n", ios->clock);
  52. seq_printf(s, "vdd:\t\t%u ", ios->vdd);
  53. if ((1 << ios->vdd) & MMC_VDD_165_195)
  54. seq_printf(s, "(1.65 - 1.95 V)\n");
  55. else if (ios->vdd < (ARRAY_SIZE(vdd_str) - 1)
  56. && vdd_str[ios->vdd] && vdd_str[ios->vdd + 1])
  57. seq_printf(s, "(%s ~ %s V)\n", vdd_str[ios->vdd],
  58. vdd_str[ios->vdd + 1]);
  59. else
  60. seq_printf(s, "(invalid)\n");
  61. switch (ios->bus_mode) {
  62. case MMC_BUSMODE_OPENDRAIN:
  63. str = "open drain";
  64. break;
  65. case MMC_BUSMODE_PUSHPULL:
  66. str = "push-pull";
  67. break;
  68. default:
  69. str = "invalid";
  70. break;
  71. }
  72. seq_printf(s, "bus mode:\t%u (%s)\n", ios->bus_mode, str);
  73. switch (ios->chip_select) {
  74. case MMC_CS_DONTCARE:
  75. str = "don't care";
  76. break;
  77. case MMC_CS_HIGH:
  78. str = "active high";
  79. break;
  80. case MMC_CS_LOW:
  81. str = "active low";
  82. break;
  83. default:
  84. str = "invalid";
  85. break;
  86. }
  87. seq_printf(s, "chip select:\t%u (%s)\n", ios->chip_select, str);
  88. switch (ios->power_mode) {
  89. case MMC_POWER_OFF:
  90. str = "off";
  91. break;
  92. case MMC_POWER_UP:
  93. str = "up";
  94. break;
  95. case MMC_POWER_ON:
  96. str = "on";
  97. break;
  98. default:
  99. str = "invalid";
  100. break;
  101. }
  102. seq_printf(s, "power mode:\t%u (%s)\n", ios->power_mode, str);
  103. seq_printf(s, "bus width:\t%u (%u bits)\n",
  104. ios->bus_width, 1 << ios->bus_width);
  105. switch (ios->timing) {
  106. case MMC_TIMING_LEGACY:
  107. str = "legacy";
  108. break;
  109. case MMC_TIMING_MMC_HS:
  110. str = "mmc high-speed";
  111. break;
  112. case MMC_TIMING_SD_HS:
  113. str = "sd high-speed";
  114. break;
  115. case MMC_TIMING_UHS_SDR50:
  116. str = "sd uhs SDR50";
  117. break;
  118. case MMC_TIMING_UHS_SDR104:
  119. str = "sd uhs SDR104";
  120. break;
  121. case MMC_TIMING_UHS_DDR50:
  122. str = "sd uhs DDR50";
  123. break;
  124. default:
  125. str = "invalid";
  126. break;
  127. }
  128. seq_printf(s, "timing spec:\t%u (%s)\n", ios->timing, str);
  129. return 0;
  130. }
  131. static int mmc_ios_open(struct inode *inode, struct file *file)
  132. {
  133. return single_open(file, mmc_ios_show, inode->i_private);
  134. }
  135. static const struct file_operations mmc_ios_fops = {
  136. .open = mmc_ios_open,
  137. .read = seq_read,
  138. .llseek = seq_lseek,
  139. .release = single_release,
  140. };
  141. static int mmc_clock_opt_get(void *data, u64 *val)
  142. {
  143. struct mmc_host *host = data;
  144. *val = host->ios.clock;
  145. return 0;
  146. }
  147. static int mmc_clock_opt_set(void *data, u64 val)
  148. {
  149. struct mmc_host *host = data;
  150. /* We need this check due to input value is u64 */
  151. if (val > host->f_max)
  152. return -EINVAL;
  153. mmc_claim_host(host);
  154. mmc_set_clock(host, (unsigned int) val);
  155. mmc_release_host(host);
  156. return 0;
  157. }
  158. DEFINE_SIMPLE_ATTRIBUTE(mmc_clock_fops, mmc_clock_opt_get, mmc_clock_opt_set,
  159. "%llu\n");
  160. void mmc_add_host_debugfs(struct mmc_host *host)
  161. {
  162. struct dentry *root;
  163. root = debugfs_create_dir(mmc_hostname(host), NULL);
  164. if (IS_ERR(root))
  165. /* Don't complain -- debugfs just isn't enabled */
  166. return;
  167. if (!root)
  168. /* Complain -- debugfs is enabled, but it failed to
  169. * create the directory. */
  170. goto err_root;
  171. host->debugfs_root = root;
  172. if (!debugfs_create_file("ios", S_IRUSR, root, host, &mmc_ios_fops))
  173. goto err_node;
  174. if (!debugfs_create_file("clock", S_IRUSR | S_IWUSR, root, host,
  175. &mmc_clock_fops))
  176. goto err_node;
  177. #ifdef CONFIG_MMC_CLKGATE
  178. if (!debugfs_create_u32("clk_delay", (S_IRUSR | S_IWUSR),
  179. root, &host->clk_delay))
  180. goto err_node;
  181. #endif
  182. #ifdef CONFIG_FAIL_MMC_REQUEST
  183. if (fail_request)
  184. setup_fault_attr(&fail_default_attr, fail_request);
  185. host->fail_mmc_request = fail_default_attr;
  186. if (IS_ERR(fault_create_debugfs_attr("fail_mmc_request",
  187. root,
  188. &host->fail_mmc_request)))
  189. goto err_node;
  190. #endif
  191. return;
  192. err_node:
  193. debugfs_remove_recursive(root);
  194. host->debugfs_root = NULL;
  195. err_root:
  196. dev_err(&host->class_dev, "failed to initialize debugfs\n");
  197. }
  198. void mmc_remove_host_debugfs(struct mmc_host *host)
  199. {
  200. debugfs_remove_recursive(host->debugfs_root);
  201. }
  202. static int mmc_dbg_card_status_get(void *data, u64 *val)
  203. {
  204. struct mmc_card *card = data;
  205. u32 status;
  206. int ret;
  207. mmc_claim_host(card->host);
  208. ret = mmc_send_status(data, &status);
  209. if (!ret)
  210. *val = status;
  211. mmc_release_host(card->host);
  212. return ret;
  213. }
  214. DEFINE_SIMPLE_ATTRIBUTE(mmc_dbg_card_status_fops, mmc_dbg_card_status_get,
  215. NULL, "%08llx\n");
  216. #define EXT_CSD_STR_LEN 1025
  217. static int mmc_ext_csd_open(struct inode *inode, struct file *filp)
  218. {
  219. struct mmc_card *card = inode->i_private;
  220. char *buf;
  221. ssize_t n = 0;
  222. u8 *ext_csd;
  223. int err, i;
  224. buf = kmalloc(EXT_CSD_STR_LEN + 1, GFP_KERNEL);
  225. if (!buf)
  226. return -ENOMEM;
  227. ext_csd = kmalloc(512, GFP_KERNEL);
  228. if (!ext_csd) {
  229. err = -ENOMEM;
  230. goto out_free;
  231. }
  232. mmc_claim_host(card->host);
  233. err = mmc_send_ext_csd(card, ext_csd);
  234. mmc_release_host(card->host);
  235. if (err)
  236. goto out_free;
  237. for (i = 511; i >= 0; i--)
  238. n += sprintf(buf + n, "%02x", ext_csd[i]);
  239. n += sprintf(buf + n, "\n");
  240. BUG_ON(n != EXT_CSD_STR_LEN);
  241. filp->private_data = buf;
  242. kfree(ext_csd);
  243. return 0;
  244. out_free:
  245. kfree(buf);
  246. kfree(ext_csd);
  247. return err;
  248. }
  249. static ssize_t mmc_ext_csd_read(struct file *filp, char __user *ubuf,
  250. size_t cnt, loff_t *ppos)
  251. {
  252. char *buf = filp->private_data;
  253. return simple_read_from_buffer(ubuf, cnt, ppos,
  254. buf, EXT_CSD_STR_LEN);
  255. }
  256. static int mmc_ext_csd_release(struct inode *inode, struct file *file)
  257. {
  258. kfree(file->private_data);
  259. return 0;
  260. }
  261. static const struct file_operations mmc_dbg_ext_csd_fops = {
  262. .open = mmc_ext_csd_open,
  263. .read = mmc_ext_csd_read,
  264. .release = mmc_ext_csd_release,
  265. .llseek = default_llseek,
  266. };
  267. void mmc_add_card_debugfs(struct mmc_card *card)
  268. {
  269. struct mmc_host *host = card->host;
  270. struct dentry *root;
  271. if (!host->debugfs_root)
  272. return;
  273. root = debugfs_create_dir(mmc_card_id(card), host->debugfs_root);
  274. if (IS_ERR(root))
  275. /* Don't complain -- debugfs just isn't enabled */
  276. return;
  277. if (!root)
  278. /* Complain -- debugfs is enabled, but it failed to
  279. * create the directory. */
  280. goto err;
  281. card->debugfs_root = root;
  282. if (!debugfs_create_x32("state", S_IRUSR, root, &card->state))
  283. goto err;
  284. if (mmc_card_mmc(card) || mmc_card_sd(card))
  285. if (!debugfs_create_file("status", S_IRUSR, root, card,
  286. &mmc_dbg_card_status_fops))
  287. goto err;
  288. if (mmc_card_mmc(card))
  289. if (!debugfs_create_file("ext_csd", S_IRUSR, root, card,
  290. &mmc_dbg_ext_csd_fops))
  291. goto err;
  292. return;
  293. err:
  294. debugfs_remove_recursive(root);
  295. card->debugfs_root = NULL;
  296. dev_err(&card->dev, "failed to initialize debugfs\n");
  297. }
  298. void mmc_remove_card_debugfs(struct mmc_card *card)
  299. {
  300. debugfs_remove_recursive(card->debugfs_root);
  301. }