debugfs.c 7.5 KB

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