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