debugfs.c 8.4 KB

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