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,
88 MODULE_DEVICE_TABLE(pcmcia, pcmcia_ids);
93 struct mmc_host *mmc; /* MMC structure */
94 unsigned char __iomem *iobase;
95 struct pci_dev *pci_dev;
99 /***************** register i/o helper functions *****************************/
101 static inline unsigned int sdricoh_readl(struct sdricoh_host *host,
104 unsigned int value = readl(host->iobase + reg);
105 dev_vdbg(host->dev, "rl %x 0x%x\n", reg, value);
109 static inline void sdricoh_writel(struct sdricoh_host *host, unsigned int reg,
112 writel(value, host->iobase + reg);
113 dev_vdbg(host->dev, "wl %x 0x%x\n", reg, value);
117 static inline unsigned int sdricoh_readw(struct sdricoh_host *host,
120 unsigned int value = readw(host->iobase + reg);
121 dev_vdbg(host->dev, "rb %x 0x%x\n", reg, value);
125 static inline void sdricoh_writew(struct sdricoh_host *host, unsigned int reg,
126 unsigned short value)
128 writew(value, host->iobase + reg);
129 dev_vdbg(host->dev, "ww %x 0x%x\n", reg, value);
132 static inline unsigned int sdricoh_readb(struct sdricoh_host *host,
135 unsigned int value = readb(host->iobase + reg);
136 dev_vdbg(host->dev, "rb %x 0x%x\n", reg, value);
140 static int sdricoh_query_status(struct sdricoh_host *host, unsigned int wanted,
141 unsigned int timeout){
143 unsigned int status = 0;
144 struct device *dev = host->dev;
145 for (loop = 0; loop < timeout; loop++) {
146 status = sdricoh_readl(host, R21C_STATUS);
147 sdricoh_writel(host, R2E4_STATUS_RESP, status);
152 if (loop == timeout) {
153 dev_err(dev, "query_status: timeout waiting for %x\n", wanted);
157 /* do not do this check in the loop as some commands fail otherwise */
158 if (status & 0x7F0000) {
159 dev_err(dev, "waiting for status bit %x failed\n", wanted);
166 static int sdricoh_mmc_cmd(struct sdricoh_host *host, unsigned char opcode,
171 unsigned int loop = 0;
172 /* reset status reg? */
173 sdricoh_writel(host, R21C_STATUS, 0x18);
174 /* fill parameters */
175 sdricoh_writel(host, R204_CMD_ARG, arg);
176 sdricoh_writel(host, R200_CMD, (0x10000 << 8) | opcode);
177 /* wait for command completion */
179 for (loop = 0; loop < CMD_TIMEOUT; loop++) {
180 status = sdricoh_readl(host, R21C_STATUS);
181 sdricoh_writel(host, R2E4_STATUS_RESP, status);
182 if (status & STATUS_CMD_FINISHED)
185 /* don't check for timeout in the loop it is not always
188 if (loop == CMD_TIMEOUT || status & STATUS_CMD_TIMEOUT)
197 static int sdricoh_reset(struct sdricoh_host *host)
199 dev_dbg(host->dev, "reset\n");
200 sdricoh_writel(host, R2F0_RESET, 0x10001);
201 sdricoh_writel(host, R2E0_INIT, 0x10000);
202 if (sdricoh_readl(host, R2E0_INIT) != 0x10000)
204 sdricoh_writel(host, R2E0_INIT, 0x10007);
206 sdricoh_writel(host, R224_MODE, 0x2000000);
207 sdricoh_writel(host, R228_POWER, 0xe0);
210 /* status register ? */
211 sdricoh_writel(host, R21C_STATUS, 0x18);
216 static int sdricoh_blockio(struct sdricoh_host *host, int read,
221 /* wait until the data is available */
223 if (sdricoh_query_status(host, STATUS_READY_TO_READ,
226 sdricoh_writel(host, R21C_STATUS, 0x18);
229 data = sdricoh_readl(host, R230_DATA);
240 if (sdricoh_query_status(host, STATUS_READY_TO_WRITE,
243 sdricoh_writel(host, R21C_STATUS, 0x18);
250 data |= (u32)*buf << 24;
254 sdricoh_writel(host, R230_DATA, data);
264 static void sdricoh_request(struct mmc_host *mmc, struct mmc_request *mrq)
266 struct sdricoh_host *host = mmc_priv(mmc);
267 struct mmc_command *cmd = mrq->cmd;
268 struct mmc_data *data = cmd->data;
269 struct device *dev = host->dev;
270 unsigned char opcode = cmd->opcode;
273 dev_dbg(dev, "=============================\n");
274 dev_dbg(dev, "sdricoh_request opcode=%i\n", opcode);
276 sdricoh_writel(host, R21C_STATUS, 0x18);
278 /* MMC_APP_CMDs need some special handling */
282 } else if (opcode == 55)
285 /* read/write commands seem to require this */
287 sdricoh_writew(host, R226_BLOCKSIZE, data->blksz);
288 sdricoh_writel(host, R208_DATAIO, 0);
291 cmd->error = sdricoh_mmc_cmd(host, opcode, cmd->arg);
293 /* read response buffer */
294 if (cmd->flags & MMC_RSP_PRESENT) {
295 if (cmd->flags & MMC_RSP_136) {
296 /* CRC is stripped so we need to do some shifting. */
297 for (i = 0; i < 4; i++) {
300 R20C_RESP + (3 - i) * 4) << 8;
303 sdricoh_readb(host, R20C_RESP +
307 cmd->resp[0] = sdricoh_readl(host, R20C_RESP);
311 if (data && cmd->error == 0) {
312 dev_dbg(dev, "transfer: blksz %i blocks %i sg_len %i "
313 "sg length %i\n", data->blksz, data->blocks,
314 data->sg_len, data->sg->length);
316 /* enter data reading mode */
317 sdricoh_writel(host, R21C_STATUS, 0x837f031e);
318 for (i = 0; i < data->blocks; i++) {
319 size_t len = data->blksz;
323 page = sg_page(data->sg);
325 buf = kmap(page) + data->sg->offset + (len * i);
327 sdricoh_blockio(host,
328 data->flags & MMC_DATA_READ, buf, len);
330 flush_dcache_page(page);
332 dev_err(dev, "sdricoh_request: cmd %i "
333 "block transfer failed\n", cmd->opcode);
337 data->bytes_xfered += len;
340 sdricoh_writel(host, R208_DATAIO, 1);
342 if (sdricoh_query_status(host, STATUS_TRANSFER_FINISHED,
344 dev_err(dev, "sdricoh_request: transfer end error\n");
345 cmd->error = -EINVAL;
348 /* FIXME check busy flag */
350 mmc_request_done(mmc, mrq);
351 dev_dbg(dev, "=============================\n");
354 static void sdricoh_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
356 struct sdricoh_host *host = mmc_priv(mmc);
357 dev_dbg(host->dev, "set_ios\n");
359 if (ios->power_mode == MMC_POWER_ON) {
360 sdricoh_writel(host, R228_POWER, 0xc0e0);
362 if (ios->bus_width == MMC_BUS_WIDTH_4) {
363 sdricoh_writel(host, R224_MODE, 0x2000300);
364 sdricoh_writel(host, R228_POWER, 0x40e0);
366 sdricoh_writel(host, R224_MODE, 0x2000340);
369 } else if (ios->power_mode == MMC_POWER_UP) {
370 sdricoh_writel(host, R224_MODE, 0x2000320);
371 sdricoh_writel(host, R228_POWER, 0xe0);
375 static int sdricoh_get_ro(struct mmc_host *mmc)
377 struct sdricoh_host *host = mmc_priv(mmc);
380 status = sdricoh_readl(host, R21C_STATUS);
381 sdricoh_writel(host, R2E4_STATUS_RESP, status);
383 /* some notebooks seem to have the locked flag switched */
385 return !(status & STATUS_CARD_LOCKED);
387 return (status & STATUS_CARD_LOCKED);
390 static struct mmc_host_ops sdricoh_ops = {
391 .request = sdricoh_request,
392 .set_ios = sdricoh_set_ios,
393 .get_ro = sdricoh_get_ro,
396 /* initialize the control and register it to the mmc framework */
397 static int sdricoh_init_mmc(struct pci_dev *pci_dev,
398 struct pcmcia_device *pcmcia_dev)
401 void __iomem *iobase = NULL;
402 struct mmc_host *mmc = NULL;
403 struct sdricoh_host *host = NULL;
404 struct device *dev = &pcmcia_dev->dev;
406 if (pci_resource_len(pci_dev, SDRICOH_PCI_REGION) !=
407 SDRICOH_PCI_REGION_SIZE) {
408 dev_dbg(dev, "unexpected pci resource len\n");
412 pci_iomap(pci_dev, SDRICOH_PCI_REGION, SDRICOH_PCI_REGION_SIZE);
414 dev_err(dev, "unable to map iobase\n");
418 if (readl(iobase + R104_VERSION) != 0x4000) {
419 dev_dbg(dev, "no supported mmc controller found\n");
423 /* allocate privdata */
424 mmc = pcmcia_dev->priv =
425 mmc_alloc_host(sizeof(struct sdricoh_host), &pcmcia_dev->dev);
427 dev_err(dev, "mmc_alloc_host failed\n");
431 host = mmc_priv(mmc);
433 host->iobase = iobase;
435 host->pci_dev = pci_dev;
437 mmc->ops = &sdricoh_ops;
439 /* FIXME: frequency and voltage handling is done by the controller
442 mmc->f_max = 24000000;
443 mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34;
444 mmc->caps |= MMC_CAP_4_BIT_DATA;
446 mmc->max_seg_size = 1024 * 512;
447 mmc->max_blk_size = 512;
449 /* reset the controler */
450 if (sdricoh_reset(host)) {
451 dev_dbg(dev, "could not reset\n");
457 result = mmc_add_host(mmc);
460 dev_dbg(dev, "mmc host registered\n");
473 /* search for supported mmc controllers */
474 static int sdricoh_pcmcia_probe(struct pcmcia_device *pcmcia_dev)
476 struct pci_dev *pci_dev = NULL;
478 dev_info(&pcmcia_dev->dev, "Searching MMC controller for pcmcia device"
479 " %s %s ...\n", pcmcia_dev->prod_id[0], pcmcia_dev->prod_id[1]);
481 /* search pci cardbus bridge that contains the mmc controler */
482 /* the io region is already claimed by yenta_socket... */
484 pci_get_device(PCI_VENDOR_ID_RICOH, PCI_DEVICE_ID_RICOH_RL5C476,
486 /* try to init the device */
487 if (!sdricoh_init_mmc(pci_dev, pcmcia_dev)) {
488 dev_info(&pcmcia_dev->dev, "MMC controller found\n");
493 dev_err(&pcmcia_dev->dev, "No MMC controller was found.\n");
497 static void sdricoh_pcmcia_detach(struct pcmcia_device *link)
499 struct mmc_host *mmc = link->priv;
501 dev_dbg(&link->dev, "detach\n");
503 /* remove mmc host */
505 struct sdricoh_host *host = mmc_priv(mmc);
506 mmc_remove_host(mmc);
507 pci_iounmap(host->pci_dev, host->iobase);
508 pci_dev_put(host->pci_dev);
511 pcmcia_disable_device(link);
516 static int sdricoh_pcmcia_suspend(struct pcmcia_device *link)
518 struct mmc_host *mmc = link->priv;
519 dev_dbg(&link->dev, "suspend\n");
520 mmc_suspend_host(mmc, PMSG_SUSPEND);
524 static int sdricoh_pcmcia_resume(struct pcmcia_device *link)
526 struct mmc_host *mmc = link->priv;
527 dev_dbg(&link->dev, "resume\n");
528 sdricoh_reset(mmc_priv(mmc));
529 mmc_resume_host(mmc);
533 #define sdricoh_pcmcia_suspend NULL
534 #define sdricoh_pcmcia_resume NULL
537 static struct pcmcia_driver sdricoh_driver = {
541 .probe = sdricoh_pcmcia_probe,
542 .remove = sdricoh_pcmcia_detach,
543 .id_table = pcmcia_ids,
544 .suspend = sdricoh_pcmcia_suspend,
545 .resume = sdricoh_pcmcia_resume,
548 /*****************************************************************************\
552 \*****************************************************************************/
554 static int __init sdricoh_drv_init(void)
556 return pcmcia_register_driver(&sdricoh_driver);
559 static void __exit sdricoh_drv_exit(void)
561 pcmcia_unregister_driver(&sdricoh_driver);
564 module_init(sdricoh_drv_init);
565 module_exit(sdricoh_drv_exit);
567 module_param(switchlocked, uint, 0444);
569 MODULE_AUTHOR("Sascha Sommer <saschasommer@freenet.de>");
570 MODULE_DESCRIPTION("Ricoh PCMCIA Secure Digital Interface driver");
571 MODULE_LICENSE("GPL");
573 MODULE_PARM_DESC(switchlocked, "Switch the cards locked status."
574 "Use this when unlocked cards are shown readonly (default 0)");