debugfs.c 6.8 KB

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