OpenCores
URL https://opencores.org/ocsvn/test_project/test_project/trunk

Subversion Repositories test_project

[/] [test_project/] [trunk/] [linux_sd_driver/] [drivers/] [mmc/] [host/] [mmc_ocores.c] - Blame information for rev 70

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 65 AlbertVeli
/* -*- linux-c -*-
2
 *
3
 * OpenCores MMC Controller driver
4
 *
5
 * Copyright (C) 2009 ORSoC, All Rights Reserved.
6
 *
7
 * This program is free software; you can redistribute it and/or modify it
8
 * under the terms of the GNU General Public License as published
9
 * by the Free Software Foundation; version 2 of the License.
10
 *
11
 * This program is distributed in the hope that it will be useful, but
12
 * WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
 * General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License along
17
 * with this program; if not, write to the Free Software Foundation, Inc.,
18
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
 */
20
#include <linux/module.h>
21
#include <linux/init.h>
22
#include <linux/ioport.h>
23
#include <linux/platform_device.h>
24
#include <linux/delay.h>
25
#include <linux/interrupt.h>
26
#include <linux/err.h>
27
#include <linux/mmc/host.h>
28
 
29
#include <asm/board.h>
30
#include <asm/io.h>
31
#include <asm/mmc.h>
32 68 tac2
#include <asm/system.h>
33 65 AlbertVeli
#include "mmc_ocores.h"
34
 
35
#define DRIVER_NAME "mmc-ocores"
36
 
37
#define NR_SG   1
38
 
39 66 tac2
 
40 70 tac2
 
41 65 AlbertVeli
struct ocores_host {
42
        struct mmc_host         *mmc;
43
        spinlock_t              lock;
44
        struct resource         *res;
45
        void __iomem            *base;
46
        unsigned int            cmdat;
47
        unsigned int            power_mode;
48
        struct ocores_platform_data *pdata;
49 69 AlbertVeli
        unsigned int            word_cnt;
50 65 AlbertVeli
        struct mmc_request      *mrq;
51
        struct mmc_command      *cmd;
52
        struct mmc_data         *data;
53 66 tac2
        int irq_cmd;
54
        int irq_dat;
55
        unsigned int flags;
56
        struct tasklet_struct finish_cmd;
57 68 tac2
        struct {
58 69 AlbertVeli
                unsigned int normal_int_status;
59
                unsigned int error_int_status;
60
        } registers;
61 65 AlbertVeli
};
62
 
63 66 tac2
struct ocores_host *oc_host;
64
 
65
static void ocores_tasklet_finish_cmd(unsigned long param);
66
static inline void CMD_IRQ_ON(struct ocores_host *host, u32 mask)
67 65 AlbertVeli
{
68 66 tac2
        u32 val = readl(host->base  + SD_NOMAL_INT_SIGNAL_ENABLE);
69
        printk(KERN_ALERT "Int mask = %08x\n", val);
70
        val |= mask;
71
        writel (val, host->base  + SD_NOMAL_INT_SIGNAL_ENABLE);
72
        printk(KERN_ALERT "Int mask = %08x\n", val);
73
}
74
 
75
static inline void CMD_IRQ_OFF(struct ocores_host *host, u32 mask)
76 69 AlbertVeli
{
77 66 tac2
        u32 val = readl(host->base  + SD_NOMAL_INT_SIGNAL_ENABLE);
78
        val  &= ~mask;
79
        writel (val, host->base  + SD_NOMAL_INT_SIGNAL_ENABLE);
80
}
81 69 AlbertVeli
 
82 66 tac2
static void ocores_start_cmd(struct ocores_host *host, struct mmc_command *cmd)
83
{
84 69 AlbertVeli
        unsigned int cmd_arg, cmd_command=0;
85 66 tac2
 
86 69 AlbertVeli
        //struct mmc_data *data = cmd->data;
87 68 tac2
        //WARN_ON(host->cmd != NULL);
88 65 AlbertVeli
        host->cmd = cmd;
89 69 AlbertVeli
 
90
        //Set up command
91 66 tac2
        cmd_arg = cmd->arg;
92
        cmd_command |= cmd->opcode << 8;
93 70 tac2
    cmd_command |= host->word_cnt << 6;
94
 
95
    if ( mmc_resp_type(cmd) == MMC_RSP_CRC  )
96
                cmd_command |= CRCE;
97
        if ( mmc_resp_type(cmd) == MMC_RSP_OPCODE  )
98
                cmd_command |= CICE;
99
 
100 66 tac2
        switch (mmc_resp_type(cmd)) {
101 70 tac2
                case MMC_RSP_NONE:
102
                        cmd_command |= MMCOC_RSP_NONE;
103 66 tac2
                break;
104 70 tac2
                case MMC_RSP_R1:
105
                        cmd_command |= MMCOC_RSP_48;
106 66 tac2
                break;
107 70 tac2
                case MMC_RSP_R1B:
108
                        cmd_command |= MMCOC_RSP_48;
109 66 tac2
                break;
110 70 tac2
                case MMC_RSP_R2:
111
                        cmd_command |= MMCOC_RSP_136;
112 66 tac2
                break;
113 70 tac2
                case MMC_RSP_R3:
114
                        cmd_command |= MMCOC_RSP_48;
115 66 tac2
                break;
116 70 tac2
                default:
117 66 tac2
                printk(KERN_INFO "mmc_ocores: unhandled response type %02x\n",
118
                        mmc_resp_type(cmd));
119
        }
120 69 AlbertVeli
 
121 68 tac2
        printk(KERN_ALERT "%s: cmd_arg = %08x\n", __FUNCTION__, cmd_arg);
122 66 tac2
        printk(KERN_ALERT "%s: cmd_command   = %08x\n", __FUNCTION__, cmd_command);
123 70 tac2
    oc_host=host;
124
 
125
        CMD_IRQ_ON (host,(ECC|EEI));
126 68 tac2
        writel(cmd_command, host->base + SD_COMMAND);
127
        wmb();
128 66 tac2
        writel(cmd_arg, host->base + SD_ARG);
129 69 AlbertVeli
 
130 65 AlbertVeli
}
131 69 AlbertVeli
 
132 66 tac2
static void ocores_process_next(struct ocores_host *host)
133 70 tac2
{
134
    host->word_cnt=0;
135 66 tac2
        if (!(host->flags & FL_SENT_COMMAND)) {
136
                host->flags |= FL_SENT_COMMAND;
137 69 AlbertVeli
                ocores_start_cmd(host, host->mrq->cmd);
138 66 tac2
        }
139
        else if ((!(host->flags & FL_SENT_STOP)) && host->mrq->stop) {
140
                host->flags |= FL_SENT_STOP;
141
                ocores_start_cmd(host, host->mrq->stop);
142
        }
143
}
144 69 AlbertVeli
 
145 65 AlbertVeli
static void ocores_request(struct mmc_host *mmc, struct mmc_request *mrq)
146
{
147
        struct ocores_host *host = mmc_priv(mmc);
148 69 AlbertVeli
 
149
        //unsigned int cmdr, mr;
150
 
151 65 AlbertVeli
        printk(KERN_ALERT "%s: mrq->cmd->opcode = %08x\n", __FUNCTION__, mrq->cmd->opcode);
152
        printk(KERN_ALERT "%s: mrq->cmd->arg    = %08x\n", __FUNCTION__, mrq->cmd->arg);
153
 
154 66 tac2
        //WARN_ON(host->mrq != NULL);
155 65 AlbertVeli
 
156
        host->mrq = mrq;
157 66 tac2
        host->flags = 0;
158 65 AlbertVeli
 
159 69 AlbertVeli
        ocores_process_next(host);
160
 
161
 
162 66 tac2
        printk(KERN_ALERT "%s: exit\n", __FUNCTION__);
163
}
164 65 AlbertVeli
 
165
 
166
 
167
static int ocores_get_ro(struct mmc_host *mmc)
168
{
169
        /* struct ocores_host *host = mmc_priv(mmc); */
170
 
171
        printk(KERN_ALERT "%s: enter\n", __FUNCTION__);
172
 
173
        /* if (host->pdata && host->pdata->get_ro) */
174
        /*      return host->pdata->get_ro(mmc_dev(mmc)); */
175
        /* /\* Host doesn't support read only detection so assume writeable *\/ */
176
        return 0;
177
}
178
 
179
static void ocores_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
180 69 AlbertVeli
{
181
        struct ocores_host *host = mmc_priv(mmc);
182 66 tac2
        int clk_div, cmd_timeout;
183 65 AlbertVeli
        /* struct ocores_host *host = mmc_priv(mmc); */
184 69 AlbertVeli
 
185 65 AlbertVeli
        printk(KERN_ALERT "%s: clock = 0x%08x\n", __FUNCTION__, ios->clock);
186
        printk(KERN_ALERT "%s: vdd = 0x%04x\n", __FUNCTION__, ios->vdd);
187
        printk(KERN_ALERT "%s: bus_mode = 0x%02x\n", __FUNCTION__, ios->bus_mode);
188
        printk(KERN_ALERT "%s: power_mode = 0x%02x\n", __FUNCTION__, ios->power_mode);
189
        printk(KERN_ALERT "%s: bus_width = 0x%02x\n", __FUNCTION__, ios->bus_width);
190
        printk(KERN_ALERT "%s: timing = 0x%02x\n", __FUNCTION__, ios->timing);
191 69 AlbertVeli
 
192 66 tac2
        //Set clock divider and timeout registers
193 69 AlbertVeli
        printk(KERN_ALERT "%s: host->base = %p\n", __FUNCTION__, host->base);
194 66 tac2
        if (ios->clock == 0) {
195 69 AlbertVeli
                //ocores_mci_write (host, SD_SOFTWARE_RST, SD_DISABLE);
196 66 tac2
        }
197
        else
198 69 AlbertVeli
        {
199 66 tac2
                writel(SD_DISABLE, host->base + SD_SOFTWARE_RST);
200
                clk_div = ((SYS_CLK / ios->clock)-2 )/ 2;
201 68 tac2
                cmd_timeout = ((SYS_CLK/ios->clock) * 512);
202 69 AlbertVeli
 
203 66 tac2
                printk(KERN_ALERT " clk_div = 0x%02x\n", clk_div);
204
                printk(KERN_ALERT " cmd_timeout = 0x%02x\n", cmd_timeout);
205 69 AlbertVeli
 
206 66 tac2
                writel (clk_div, host->base  + SD_CLOCK_DIVIDER);
207
                writel (cmd_timeout, host->base  + SD_TIMEOUT);
208 69 AlbertVeli
 
209
 
210 66 tac2
                writel(SD_ENABLE, host->base + SD_SOFTWARE_RST);
211
        }
212
 
213 65 AlbertVeli
}
214
 
215 66 tac2
 
216
 
217
static irqreturn_t ocores_irq_cmd(int irq, void *devid)
218
{
219 70 tac2
         struct ocores_host *host = oc_host;
220
 
221
         disable_irq(host->irq_cmd);
222
 
223
                //printk(KERN_ALERT "%s: IRQ START***** Normal In  = %08x\n", __FUNCTION__, readl(host->base + SD_NORMAL_INT_STATUS));
224
 
225
         host->registers.normal_int_status  = readl(host->base + SD_NORMAL_INT_STATUS);
226
         rmb();
227
         host->registers.error_int_status  = readl(host->base + SD_ERROR_INT_STATUS);
228
 
229
         writel(0,host->base + SD_NORMAL_INT_STATUS);
230
         writel(0,host->base + SD_ERROR_INT_STATUS);
231
 
232
    //printk(KERN_ALERT "%s: IRQ END***** Error In  = %08x\n", __FUNCTION__, readl(host->base + SD_ERROR_INT_STATUS));  
233
    tasklet_schedule(&host->finish_cmd);
234
    CMD_IRQ_OFF (host,(ECC|EEI));
235
    enable_irq(host->irq_cmd);
236
   return IRQ_HANDLED;
237 66 tac2
}
238
 
239
static irqreturn_t ocores_irq_dat(int irq, void *dev_id)
240
{
241
 
242
}
243 69 AlbertVeli
 
244 65 AlbertVeli
static const struct mmc_host_ops ocores_ops = {
245
        .request                = ocores_request,
246
        .get_ro                 = ocores_get_ro,
247
        .set_ios                = ocores_set_ios,
248
        /* .enable_sdio_irq     = ocores_enable_sdio_irq, */
249
};
250
 
251
static int ocores_probe(struct platform_device *pdev)
252
{
253
        struct mmc_host *mmc;
254
        struct ocores_host *host = NULL;
255
        struct resource *r;
256
        int ret;
257
 
258
        printk(KERN_ALERT "%s: enter\n", __FUNCTION__);
259
 
260
        r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
261
 
262
        printk(KERN_ALERT "%s: resource %x, %x\n", __FUNCTION__, r->start, r->end);
263
 
264
        r = request_mem_region(r->start, r->end - r->start, DRIVER_NAME);
265
        if (!r) {
266
                return -EBUSY;
267
        }
268
 
269
        mmc = mmc_alloc_host(sizeof(struct ocores_host), &pdev->dev);
270
        if (!mmc) {
271
                ret = -ENOMEM;
272
                goto out;
273
        }
274
 
275 69 AlbertVeli
 
276 65 AlbertVeli
        mmc->ops = &ocores_ops;
277 69 AlbertVeli
 
278 65 AlbertVeli
        mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34;
279
        mmc->caps = MMC_CAP_4_BIT_DATA;
280 66 tac2
        mmc->f_min = 700000;  //SYS_CLK  60; 0.7 Mhz
281 69 AlbertVeli
        mmc->f_max = 4166666;  //SYS_CLK;   4.166 666 mhz
282 65 AlbertVeli
 
283
        mmc->max_blk_count = 8;
284
        mmc->max_hw_segs = mmc->max_blk_count;
285
        mmc->max_blk_size = MMCOC_MAX_BLOCK_SIZE;
286
        mmc->max_seg_size = mmc->max_blk_count * mmc->max_blk_size;
287
        mmc->max_req_size = mmc->max_seg_size;
288
        mmc->max_phys_segs = mmc->max_hw_segs;
289
 
290
        host = mmc_priv(mmc);
291
        host->mmc = mmc;
292
        host->cmdat = 0;
293 69 AlbertVeli
        host->registers.normal_int_status =0;
294 65 AlbertVeli
 
295 66 tac2
 
296 69 AlbertVeli
        tasklet_init(&host->finish_cmd, ocores_tasklet_finish_cmd,
297
                     (unsigned long) host);
298
 
299 65 AlbertVeli
        spin_lock_init(&host->lock);
300
        host->res = r;
301
 
302 66 tac2
        host->base = ioremap(r->start, r->end - r->start +1);
303 65 AlbertVeli
        /* host->base = (void *)r->start; */
304
        if (!host->base) {
305
                ret = -ENOMEM;
306
                goto out;
307
        }
308
        host->pdata = pdev->dev.platform_data;
309
        mmc->ocr_avail = host->pdata->ocr_mask;
310 69 AlbertVeli
 
311
        host->irq_cmd = platform_get_irq_byname(pdev, "cmd_irq");
312 66 tac2
        ret = request_irq(host->irq_cmd, ocores_irq_cmd, IRQF_DISABLED, DRIVER_NAME, host);
313 69 AlbertVeli
        printk(KERN_ALERT "%s: IRQ dat resource  %x\n", __FUNCTION__, host->irq_cmd );
314
        printk(KERN_ALERT "%s: RET cmd irq %x\n", __FUNCTION__, ret);
315
        if (ret)
316 66 tac2
                goto out;
317
        disable_irq(host->irq_cmd);
318 69 AlbertVeli
 
319 66 tac2
        host->irq_dat = platform_get_irq_byname(pdev, "dat_irq");
320
        ret = request_irq(host->irq_dat, ocores_irq_dat, IRQF_DISABLED, DRIVER_NAME, host);
321 69 AlbertVeli
        printk(KERN_ALERT "%s: IRQ dat resource  %x\n", __FUNCTION__, host->irq_dat );
322
        printk(KERN_ALERT "%s: RET Dat irq  %x\n", __FUNCTION__, ret);
323
        if (ret)
324 66 tac2
                goto out;
325 69 AlbertVeli
        disable_irq(host->irq_dat);
326 65 AlbertVeli
 
327 66 tac2
 
328
 
329 69 AlbertVeli
        enable_irq(host->irq_cmd);
330 66 tac2
        enable_irq(host->irq_dat);
331 65 AlbertVeli
        printk(KERN_ALERT "%s: host->base = %p\n", __FUNCTION__, host->base);
332
        printk(KERN_ALERT "%s: SD_BLOCK = %08x\n", __FUNCTION__, readl(host->base + SD_BLOCK));
333
        printk(KERN_ALERT "%s: host->pdata->ocr_mask = %08x\n", __FUNCTION__, host->pdata->ocr_mask);
334 69 AlbertVeli
        oc_host=host;
335 65 AlbertVeli
        mmc_add_host(mmc);
336
 
337
        printk(KERN_ALERT "%s: exit\n", __FUNCTION__);
338
 
339
        return 0;
340
 
341
 out:
342 66 tac2
        printk(KERN_ALERT "%s: ERROR REQUESTINING RESOURCES\n", __FUNCTION__);
343 65 AlbertVeli
        if (mmc) {
344
                mmc_free_host(mmc);
345
        }
346
        release_resource(r);
347
 
348
        return ret;
349
}
350
 
351 69 AlbertVeli
static void ocores_tasklet_finish_cmd(unsigned long param)
352
{
353
        struct ocores_host *host = (struct ocores_host *) param;
354
 
355
        printk(KERN_ALERT " TASKLET RUNNS************\n");
356
 
357
        printk(KERN_ALERT "%s: TASKLET RUNNS****** Normal INT = %08x\n", __FUNCTION__,  host->registers.normal_int_status);
358
        printk(KERN_ALERT "%s: TASKLET RUNNS****** Error INT = %08x\n", __FUNCTION__,  host->registers.error_int_status);
359
 
360
        //Check For Transmissions errors
361
        if ((host->registers.normal_int_status & EI) == EI)
362
        {
363
                printk(KERN_ALERT "TRANSMISSION ERROR DETECTED");
364
                switch ( host->registers.error_int_status )
365
                {
366
                case (CTE):
367
                        pr_debug("Card took too long to respond\n");
368
                        host->mrq->cmd->error = -ETIMEDOUT ;
369
                        break;
370
                case (CCRC  ):
371
                        pr_debug(" CRC  problem with the received or sent data\n");
372
                        host->mrq->cmd->error = -EILSEQ;
373
                        break;
374
                case (CIE  ):
375
                        pr_debug("Index problem with the received or sent data\n");
376
                        host->mrq->cmd->error = -EILSEQ;
377
                        break;
378
                }
379
        }
380
        else
381 70 tac2
        {
382
                if ( mmc_resp_type(host->mrq->cmd) == MMCOC_RSP_136      )   //Long response
383
        {
384
                printk(KERN_ALERT "Long Response, Word Cnt  * = %08x\n ",host->word_cnt);
385
                host->word_cnt+=1;
386
                switch(host->word_cnt-1)
387
                {
388
                        case (0):
389
                                host->mrq->cmd->resp[3]  =  readl(host->base + SD_RESP1);
390
                            ocores_start_cmd(host, host->mrq->cmd);
391
                        break;
392
                        case (1):
393
                                host->mrq->cmd->resp[2]  =  readl(host->base + SD_RESP1);
394
                                ocores_start_cmd(host, host->mrq->cmd);
395
                        break;
396
                        case (2):
397
                                host->mrq->cmd->resp[1]  =  readl(host->base + SD_RESP1);
398
                                ocores_start_cmd(host, host->mrq->cmd);
399
                        break;
400
                        case (3):
401
                                host->mrq->cmd->resp[0]  =  readl(host->base + SD_RESP1);
402
                                mmc_request_done(host->mmc, host->mrq);
403
                        break;
404
                        }
405
            }
406
            else  //Short response
407
            {
408
                host->mrq->cmd->error = 0 ;
409
                host->mrq->cmd->resp[0] = readl(host->base + SD_RESP1);
410
                printk(KERN_ALERT "Short Response CMD RSP * = %08x\n", host->mrq->cmd->resp[0]);
411
                mmc_request_done(host->mmc, host->mrq);
412
             }
413 69 AlbertVeli
        }
414
 
415
 
416 70 tac2
 
417 69 AlbertVeli
}
418
 
419 65 AlbertVeli
static int ocores_remove(struct platform_device *pdev)
420
{
421
        struct mmc_host *mmc = platform_get_drvdata(pdev);
422
 
423
        printk(KERN_ALERT "%s: enter\n", __FUNCTION__);
424
 
425
        platform_set_drvdata(pdev, NULL);
426
 
427
        if (mmc) {
428
                struct ocores_host *host = mmc_priv(mmc);
429
 
430
                mmc_remove_host(mmc);
431
 
432
                release_resource(host->res);
433
 
434
                mmc_free_host(mmc);
435
        }
436
 
437
        printk(KERN_ALERT "%s: exit\n", __FUNCTION__);
438
 
439
        return 0;
440
}
441
 
442
#ifdef CONFIG_PM
443
static int ocores_suspend(struct platform_device *dev, pm_message_t state)
444
{
445
        struct mmc_host *mmc = platform_get_drvdata(dev);
446
        int ret = 0;
447
 
448
        printk(KERN_ALERT "%s: enter\n", __FUNCTION__);
449
 
450
        if (mmc) {
451
                ret = mmc_suspend_host(mmc, state);
452
        }
453
 
454
        printk(KERN_ALERT "%s: exit\n", __FUNCTION__);
455
 
456
        return ret;
457
}
458
 
459
static int ocores_resume(struct platform_device *dev)
460
{
461
        struct mmc_host *mmc = platform_get_drvdata(dev);
462
        int ret = 0;
463
 
464
        printk(KERN_ALERT "%s: enter\n", __FUNCTION__);
465
 
466
        if (mmc) {
467
                ret = mmc_resume_host(mmc);
468
        }
469
 
470
        printk(KERN_ALERT "%s: exit\n", __FUNCTION__);
471
 
472
        return ret;
473
}
474
#else
475
#define ocores_suspend  NULL
476
#define ocores_resume   NULL
477
#endif
478
 
479
static struct platform_driver ocores_driver = {
480
        .probe          = ocores_probe,
481
        .remove         = ocores_remove,
482
        .suspend        = ocores_suspend,
483
        .resume         = ocores_resume,
484
        .driver         = {
485
                .name   = DRIVER_NAME,
486
        },
487
};
488
 
489
static int __init ocores_init(void)
490
{
491
        printk(KERN_ALERT "%s: registering ocores platform_driver\n", __FUNCTION__);
492
 
493
        return platform_driver_register(&ocores_driver);
494
}
495
 
496
static void __exit ocores_exit(void)
497
{
498
        printk(KERN_ALERT "%s: unregistering ocores platform_driver\n", __FUNCTION__);
499
 
500
        platform_driver_unregister(&ocores_driver);
501
}
502
 
503
module_init(ocores_init);
504
module_exit(ocores_exit);
505
 
506
MODULE_DESCRIPTION("OpenCores Multimedia Card Interface Driver");
507
MODULE_LICENSE("GPL");

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.