2 * sdricoh_cs.c - driver for Ricoh Secure Digital Card Readers that can be
3 * found on some Ricoh RL5c476 II cardbus bridge
5 * Copyright (C) 2006 - 2008 Sascha Sommer <saschasommer@freenet.de>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27 #include <linux/delay.h>
28 #include <linux/highmem.h>
29 #include <linux/pci.h>
30 #include <linux/ioport.h>
31 #include <linux/scatterlist.h>
33 #include <pcmcia/cs_types.h>
34 #include <pcmcia/cs.h>
35 #include <pcmcia/cistpl.h>
36 #include <pcmcia/ds.h>
39 #include <linux/mmc/host.h>
41 #define DRIVER_NAME "sdricoh_cs"
43 static unsigned int switchlocked;
46 #define SDRICOH_PCI_REGION 0
47 #define SDRICOH_PCI_REGION_SIZE 0x1000
50 #define R104_VERSION 0x104
51 #define R200_CMD 0x200
52 #define R204_CMD_ARG 0x204
53 #define R208_DATAIO 0x208
54 #define R20C_RESP 0x20c
55 #define R21C_STATUS 0x21c
56 #define R2E0_INIT 0x2e0
57 #define R2E4_STATUS_RESP 0x2e4
58 #define R2F0_RESET 0x2f0
59 #define R224_MODE 0x224
60 #define R226_BLOCKSIZE 0x226
61 #define R228_POWER 0x228
62 #define R230_DATA 0x230
64 /* flags for the R21C_STATUS register */
65 #define STATUS_CMD_FINISHED 0x00000001
66 #define STATUS_TRANSFER_FINISHED 0x00000004
67 #define STATUS_CARD_INSERTED 0x00000020
68 #define STATUS_CARD_LOCKED 0x00000080
69 #define STATUS_CMD_TIMEOUT 0x00400000
70 #define STATUS_READY_TO_READ 0x01000000
71 #define STATUS_READY_TO_WRITE 0x02000000
72 #define STATUS_BUSY 0x40000000
75 #define INIT_TIMEOUT 100
76 #define CMD_TIMEOUT 100000
77 #define TRANSFER_TIMEOUT 100000
78 #define BUSY_TIMEOUT 32767
80 /* list of supported pcmcia devices */
81 static struct pcmcia_device_id pcmcia_ids[] = {
82 /* vendor and device strings followed by their crc32 hashes */
83 PCMCIA_DEVICE_PROD_ID12("RICOH", "Bay1Controller", 0xd9f522ed,
85 PCMCIA_DEVICE_PROD_ID12("RICOH", "Bay Controller", 0xd9f522ed,
90 MODULE_DEVICE_TABLE(pcmcia, pcmcia_ids);
95 struct mmc_host *mmc; /* MMC structure */
96 unsigned char __iomem *iobase;
97 struct pci_dev *pci_dev;
101 /***************** register i/o helper functions *****************************/
103 static inline unsigned int sdricoh_readl(struct sdricoh_host *host,
106 unsigned int value = readl(host->iobase + reg);
107 dev_vdbg(host->dev, "rl %x 0x%x\n", reg, value);
111 static inline void sdricoh_writel(struct sdricoh_host *host, unsigned int reg,
114 writel(value, host->iobase + reg);
115 dev_vdbg(host->dev, "wl %x 0x%x\n", reg, value);
119 static inline unsigned int sdricoh_readw(struct sdricoh_host *host,
122 unsigned int value = readw(host->iobase + reg);
123 dev_vdbg(host->dev, "rb %x 0x%x\n", reg, value);
127 static inline void sdricoh_writew(struct sdricoh_host *host, unsigned int reg,
128 unsigned short value)
130 writew(value, host->iobase + reg);
131 dev_vdbg(host->dev, "ww %x 0x%x\n", reg, value);
134 static inline unsigned int sdricoh_readb(struct sdricoh_host *host,
137 unsigned int value = readb(host->iobase + reg);
138 dev_vdbg(host->dev, "rb %x 0x%x\n", reg, value);
142 static int sdricoh_query_status(struct sdricoh_host *host, unsigned int wanted,
143 unsigned int timeout){
145 unsigned int status = 0;
146 struct device *dev = host->dev;
147 for (loop = 0; loop < timeout; loop++) {
148 status = sdricoh_readl(host, R21C_STATUS);
149 sdricoh_writel(host, R2E4_STATUS_RESP, status);
154 if (loop == timeout) {
155 dev_err(dev, "query_status: timeout waiting for %x\n", wanted);
159 /* do not do this check in the loop as some commands fail otherwise */
160 if (status & 0x7F0000) {
161 dev_err(dev, "waiting for status bit %x failed\n", wanted);
168 static int sdricoh_mmc_cmd(struct sdricoh_host *host, unsigned char opcode,
173 unsigned int loop = 0;
174 /* reset status reg? */
175 sdricoh_writel(host, R21C_STATUS, 0x18);
176 /* fill parameters */
177 sdricoh_writel(host, R204_CMD_ARG, arg);
178 sdricoh_writel(host, R200_CMD, (0x10000 << 8) | opcode);
179 /* wait for command completion */
181 for (loop = 0; loop < CMD_TIMEOUT; loop++) {
182 status = sdricoh_readl(host, R21C_STATUS);
183 sdricoh_writel(host, R2E4_STATUS_RESP, status);
184 if (status & STATUS_CMD_FINISHED)
187 /* don't check for timeout in the loop it is not always
190 if (loop == CMD_TIMEOUT || status & STATUS_CMD_TIMEOUT)
199 static int sdricoh_reset(struct sdricoh_host *host)
201 dev_dbg(host->dev, "reset\n");
202 sdricoh_writel(host, R2F0_RESET, 0x10001);
203 sdricoh_writel(host, R2E0_INIT, 0x10000);
204 if (sdricoh_readl(host, R2E0_INIT) != 0x10000)
206 sdricoh_writel(host, R2E0_INIT, 0x10007);
208 sdricoh_writel(host, R224_MODE, 0x2000000);
209 sdricoh_writel(host, R228_POWER, 0xe0);
212 /* status register ? */
213 sdricoh_writel(host, R21C_STATUS, 0x18);
218 static int sdricoh_blockio(struct sdricoh_host *host, int read,
223 /* wait until the data is available */
225 if (sdricoh_query_status(host, STATUS_READY_TO_READ,
228 sdricoh_writel(host, R21C_STATUS, 0x18);
231 data = sdricoh_readl(host, R230_DATA);
242 if (sdricoh_query_status(host, STATUS_READY_TO_WRITE,
245 sdricoh_writel(host, R21C_STATUS, 0x18);
252 data |= (u32)*buf << 24;
256 sdricoh_writel(host, R230_DATA, data);
266 static void sdricoh_request(struct mmc_host *mmc, struct mmc_request *mrq)
268 struct sdricoh_host *host = mmc_priv(mmc);
269 struct mmc_command *cmd = mrq->cmd;
270 struct mmc_data *data = cmd->data;
271 struct device *dev = host->dev;
272 unsigned char opcode = cmd->opcode;
275 dev_dbg(dev, "=============================\n");
276 dev_dbg(dev, "sdricoh_request opcode=%i\n", opcode);
278 sdricoh_writel(host, R21C_STATUS, 0x18);
280 /* MMC_APP_CMDs need some special handling */
284 } else if (opcode == 55)
287 /* read/write commands seem to require this */
289 sdricoh_writew(host, R226_BLOCKSIZE, data->blksz);
290 sdricoh_writel(host, R208_DATAIO, 0);
293 cmd->error = sdricoh_mmc_cmd(host, opcode, cmd->arg);
295 /* read response buffer */
296 if (cmd->flags & MMC_RSP_PRESENT) {
297 if (cmd->flags & MMC_RSP_136) {
298 /* CRC is stripped so we need to do some shifting. */
299 for (i = 0; i < 4; i++) {
302 R20C_RESP + (3 - i) * 4) << 8;
305 sdricoh_readb(host, R20C_RESP +
309 cmd->resp[0] = sdricoh_readl(host, R20C_RESP);
313 if (data && cmd->error == 0) {
314 dev_dbg(dev, "transfer: blksz %i blocks %i sg_len %i "
315 "sg length %i\n", data->blksz, data->blocks,
316 data->sg_len, data->sg->length);
318 /* enter data reading mode */
319 sdricoh_writel(host, R21C_STATUS, 0x837f031e);
320 for (i = 0; i < data->blocks; i++) {
321 size_t len = data->blksz;
325 page = sg_page(data->sg);
327 buf = kmap(page) + data->sg->offset + (len * i);
329 sdricoh_blockio(host,
330 data->flags & MMC_DATA_READ, buf, len);
332 flush_dcache_page(page);
334 dev_err(dev, "sdricoh_request: cmd %i "
335 "block transfer failed\n", cmd->opcode);
339 data->bytes_xfered += len;
342 sdricoh_writel(host, R208_DATAIO, 1);
344 if (sdricoh_query_status(host, STATUS_TRANSFER_FINISHED,
346 dev_err(dev, "sdricoh_request: transfer end error\n");
347 cmd->error = -EINVAL;
350 /* FIXME check busy flag */
352 mmc_request_done(mmc, mrq);
353 dev_dbg(dev, "=============================\n");
356 static void sdricoh_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
358 struct sdricoh_host *host = mmc_priv(mmc);
359 dev_dbg(host->dev, "set_ios\n");
361 if (ios->power_mode == MMC_POWER_ON) {
362 sdricoh_writel(host, R228_POWER, 0xc0e0);
364 if (ios->bus_width == MMC_BUS_WIDTH_4) {
365 sdricoh_writel(host, R224_MODE, 0x2000300);
366 sdricoh_writel(host, R228_POWER, 0x40e0);
368 sdricoh_writel(host, R224_MODE, 0x2000340);
371 } else if (ios->power_mode == MMC_POWER_UP) {
372 sdricoh_writel(host, R224_MODE, 0x2000320);
373 sdricoh_writel(host, R228_POWER, 0xe0);
377 static int sdricoh_get_ro(struct mmc_host *mmc)
379 struct sdricoh_host *host = mmc_priv(mmc);
382 status = sdricoh_readl(host, R21C_STATUS);
383 sdricoh_writel(host, R2E4_STATUS_RESP, status);
385 /* some notebooks seem to have the locked flag switched */
387 return !(status & STATUS_CARD_LOCKED);
389 return (status & STATUS_CARD_LOCKED);
392 static struct mmc_host_ops sdricoh_ops = {
393 .request = sdricoh_request,
394 .set_ios = sdricoh_set_ios,
395 .get_ro = sdricoh_get_ro,
398 /* initialize the control and register it to the mmc framework */
399 static int sdricoh_init_mmc(struct pci_dev *pci_dev,
400 struct pcmcia_device *pcmcia_dev)
403 void __iomem *iobase = NULL;
404 struct mmc_host *mmc = NULL;
405 struct sdricoh_host *host = NULL;
406 struct device *dev = &pcmcia_dev->dev;
408 if (pci_resource_len(pci_dev, SDRICOH_PCI_REGION) !=
409 SDRICOH_PCI_REGION_SIZE) {
410 dev_dbg(dev, "unexpected pci resource len\n");
414 pci_iomap(pci_dev, SDRICOH_PCI_REGION, SDRICOH_PCI_REGION_SIZE);
416 dev_err(dev, "unable to map iobase\n");
420 if (readl(iobase + R104_VERSION) != 0x4000) {
421 dev_dbg(dev, "no supported mmc controller found\n");
425 /* allocate privdata */
426 mmc = pcmcia_dev->priv =
427 mmc_alloc_host(sizeof(struct sdricoh_host), &pcmcia_dev->dev);
429 dev_err(dev, "mmc_alloc_host failed\n");
433 host = mmc_priv(mmc);
435 host->iobase = iobase;
437 host->pci_dev = pci_dev;
439 mmc->ops = &sdricoh_ops;
441 /* FIXME: frequency and voltage handling is done by the controller
444 mmc->f_max = 24000000;
445 mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34;
446 mmc->caps |= MMC_CAP_4_BIT_DATA;
448 mmc->max_seg_size = 1024 * 512;
449 mmc->max_blk_size = 512;
451 /* reset the controler */
452 if (sdricoh_reset(host)) {
453 dev_dbg(dev, "could not reset\n");
459 result = mmc_add_host(mmc);
462 dev_dbg(dev, "mmc host registered\n");
468 pci_iounmap(pci_dev, iobase);
475 /* search for supported mmc controllers */
476 static int sdricoh_pcmcia_probe(struct pcmcia_device *pcmcia_dev)
478 struct pci_dev *pci_dev = NULL;
480 dev_info(&pcmcia_dev->dev, "Searching MMC controller for pcmcia device"
481 " %s %s ...\n", pcmcia_dev->prod_id[0], pcmcia_dev->prod_id[1]);
483 /* search pci cardbus bridge that contains the mmc controler */
484 /* the io region is already claimed by yenta_socket... */
486 pci_get_device(PCI_VENDOR_ID_RICOH, PCI_DEVICE_ID_RICOH_RL5C476,
488 /* try to init the device */
489 if (!sdricoh_init_mmc(pci_dev, pcmcia_dev)) {
490 dev_info(&pcmcia_dev->dev, "MMC controller found\n");
495 dev_err(&pcmcia_dev->dev, "No MMC controller was found.\n");
499 static void sdricoh_pcmcia_detach(struct pcmcia_device *link)
501 struct mmc_host *mmc = link->priv;
503 dev_dbg(&link->dev, "detach\n");
505 /* remove mmc host */
507 struct sdricoh_host *host = mmc_priv(mmc);
508 mmc_remove_host(mmc);
509 pci_iounmap(host->pci_dev, host->iobase);
510 pci_dev_put(host->pci_dev);
513 pcmcia_disable_device(link);
518 static int sdricoh_pcmcia_suspend(struct pcmcia_device *link)
520 struct mmc_host *mmc = link->priv;
521 dev_dbg(&link->dev, "suspend\n");
522 mmc_suspend_host(mmc, PMSG_SUSPEND);
526 static int sdricoh_pcmcia_resume(struct pcmcia_device *link)
528 struct mmc_host *mmc = link->priv;
529 dev_dbg(&link->dev, "resume\n");
530 sdricoh_reset(mmc_priv(mmc));
531 mmc_resume_host(mmc);
535 #define sdricoh_pcmcia_suspend NULL
536 #define sdricoh_pcmcia_resume NULL
539 static struct pcmcia_driver sdricoh_driver = {
543 .probe = sdricoh_pcmcia_probe,
544 .remove = sdricoh_pcmcia_detach,
545 .id_table = pcmcia_ids,
546 .suspend = sdricoh_pcmcia_suspend,
547 .resume = sdricoh_pcmcia_resume,
550 /*****************************************************************************\
554 \*****************************************************************************/
556 static int __init sdricoh_drv_init(void)
558 return pcmcia_register_driver(&sdricoh_driver);
561 static void __exit sdricoh_drv_exit(void)
563 pcmcia_unregister_driver(&sdricoh_driver);
566 module_init(sdricoh_drv_init);
567 module_exit(sdricoh_drv_exit);
569 module_param(switchlocked, uint, 0444);
571 MODULE_AUTHOR("Sascha Sommer <saschasommer@freenet.de>");
572 MODULE_DESCRIPTION("Ricoh PCMCIA Secure Digital Interface driver");
573 MODULE_LICENSE("GPL");
575 MODULE_PARM_DESC(switchlocked, "Switch the cards locked status."
576 "Use this when unlocked cards are shown readonly (default 0)");