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