1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * sdricoh_cs.c - driver for Ricoh Secure Digital Card Readers that can be
4 * found on some Ricoh RL5c476 II cardbus bridge
6 * Copyright (C) 2006 - 2008 Sascha Sommer <saschasommer@freenet.de>
13 #include <linux/delay.h>
14 #include <linux/highmem.h>
15 #include <linux/module.h>
16 #include <linux/pci.h>
17 #include <linux/ioport.h>
18 #include <linux/iopoll.h>
19 #include <linux/scatterlist.h>
21 #include <pcmcia/cistpl.h>
22 #include <pcmcia/ds.h>
25 #include <linux/mmc/host.h>
26 #include <linux/mmc/mmc.h>
28 #define DRIVER_NAME "sdricoh_cs"
30 static unsigned int switchlocked
;
33 #define SDRICOH_PCI_REGION 0
34 #define SDRICOH_PCI_REGION_SIZE 0x1000
37 #define R104_VERSION 0x104
38 #define R200_CMD 0x200
39 #define R204_CMD_ARG 0x204
40 #define R208_DATAIO 0x208
41 #define R20C_RESP 0x20c
42 #define R21C_STATUS 0x21c
43 #define R2E0_INIT 0x2e0
44 #define R2E4_STATUS_RESP 0x2e4
45 #define R2F0_RESET 0x2f0
46 #define R224_MODE 0x224
47 #define R226_BLOCKSIZE 0x226
48 #define R228_POWER 0x228
49 #define R230_DATA 0x230
51 /* flags for the R21C_STATUS register */
52 #define STATUS_CMD_FINISHED 0x00000001
53 #define STATUS_TRANSFER_FINISHED 0x00000004
54 #define STATUS_CARD_INSERTED 0x00000020
55 #define STATUS_CARD_LOCKED 0x00000080
56 #define STATUS_CMD_TIMEOUT 0x00400000
57 #define STATUS_READY_TO_READ 0x01000000
58 #define STATUS_READY_TO_WRITE 0x02000000
59 #define STATUS_BUSY 0x40000000
62 #define SDRICOH_CMD_TIMEOUT_US 1000000
63 #define SDRICOH_DATA_TIMEOUT_US 1000000
65 /* list of supported pcmcia devices */
66 static const struct pcmcia_device_id pcmcia_ids
[] = {
67 /* vendor and device strings followed by their crc32 hashes */
68 PCMCIA_DEVICE_PROD_ID12("RICOH", "Bay1Controller", 0xd9f522ed,
70 PCMCIA_DEVICE_PROD_ID12("RICOH", "Bay Controller", 0xd9f522ed,
75 MODULE_DEVICE_TABLE(pcmcia
, pcmcia_ids
);
80 struct mmc_host
*mmc
; /* MMC structure */
81 unsigned char __iomem
*iobase
;
82 struct pci_dev
*pci_dev
;
86 /***************** register i/o helper functions *****************************/
88 static inline unsigned int sdricoh_readl(struct sdricoh_host
*host
,
91 unsigned int value
= readl(host
->iobase
+ reg
);
92 dev_vdbg(host
->dev
, "rl %x 0x%x\n", reg
, value
);
96 static inline void sdricoh_writel(struct sdricoh_host
*host
, unsigned int reg
,
99 writel(value
, host
->iobase
+ reg
);
100 dev_vdbg(host
->dev
, "wl %x 0x%x\n", reg
, value
);
104 static inline void sdricoh_writew(struct sdricoh_host
*host
, unsigned int reg
,
105 unsigned short value
)
107 writew(value
, host
->iobase
+ reg
);
108 dev_vdbg(host
->dev
, "ww %x 0x%x\n", reg
, value
);
111 static inline unsigned int sdricoh_readb(struct sdricoh_host
*host
,
114 unsigned int value
= readb(host
->iobase
+ reg
);
115 dev_vdbg(host
->dev
, "rb %x 0x%x\n", reg
, value
);
119 static bool sdricoh_status_ok(struct sdricoh_host
*host
, unsigned int status
,
122 sdricoh_writel(host
, R2E4_STATUS_RESP
, status
);
123 return status
& wanted
;
126 static int sdricoh_query_status(struct sdricoh_host
*host
, unsigned int wanted
)
129 unsigned int status
= 0;
130 struct device
*dev
= host
->dev
;
132 ret
= read_poll_timeout(sdricoh_readl
, status
,
133 sdricoh_status_ok(host
, status
, wanted
),
134 32, SDRICOH_DATA_TIMEOUT_US
, false,
137 dev_err(dev
, "query_status: timeout waiting for %x\n", wanted
);
141 /* do not do this check in the loop as some commands fail otherwise */
142 if (status
& 0x7F0000) {
143 dev_err(dev
, "waiting for status bit %x failed\n", wanted
);
150 static int sdricoh_mmc_cmd(struct sdricoh_host
*host
, struct mmc_command
*cmd
)
152 unsigned int status
, timeout_us
;
154 unsigned char opcode
= cmd
->opcode
;
156 /* reset status reg? */
157 sdricoh_writel(host
, R21C_STATUS
, 0x18);
159 /* MMC_APP_CMDs need some special handling */
163 } else if (opcode
== MMC_APP_CMD
)
166 /* fill parameters */
167 sdricoh_writel(host
, R204_CMD_ARG
, cmd
->arg
);
168 sdricoh_writel(host
, R200_CMD
, (0x10000 << 8) | opcode
);
170 /* wait for command completion */
174 timeout_us
= cmd
->busy_timeout
? cmd
->busy_timeout
* 1000 :
175 SDRICOH_CMD_TIMEOUT_US
;
177 ret
= read_poll_timeout(sdricoh_readl
, status
,
178 sdricoh_status_ok(host
, status
, STATUS_CMD_FINISHED
),
179 32, timeout_us
, false,
183 * Don't check for timeout status in the loop, as it's not always reset
186 if (ret
|| status
& STATUS_CMD_TIMEOUT
)
192 static int sdricoh_reset(struct sdricoh_host
*host
)
194 dev_dbg(host
->dev
, "reset\n");
195 sdricoh_writel(host
, R2F0_RESET
, 0x10001);
196 sdricoh_writel(host
, R2E0_INIT
, 0x10000);
197 if (sdricoh_readl(host
, R2E0_INIT
) != 0x10000)
199 sdricoh_writel(host
, R2E0_INIT
, 0x10007);
201 sdricoh_writel(host
, R224_MODE
, 0x2000000);
202 sdricoh_writel(host
, R228_POWER
, 0xe0);
205 /* status register ? */
206 sdricoh_writel(host
, R21C_STATUS
, 0x18);
211 static int sdricoh_blockio(struct sdricoh_host
*host
, int read
,
216 /* wait until the data is available */
218 if (sdricoh_query_status(host
, STATUS_READY_TO_READ
))
220 sdricoh_writel(host
, R21C_STATUS
, 0x18);
223 data
= sdricoh_readl(host
, R230_DATA
);
234 if (sdricoh_query_status(host
, STATUS_READY_TO_WRITE
))
236 sdricoh_writel(host
, R21C_STATUS
, 0x18);
243 data
|= (u32
)*buf
<< 24;
247 sdricoh_writel(host
, R230_DATA
, data
);
254 static void sdricoh_request(struct mmc_host
*mmc
, struct mmc_request
*mrq
)
256 struct sdricoh_host
*host
= mmc_priv(mmc
);
257 struct mmc_command
*cmd
= mrq
->cmd
;
258 struct mmc_data
*data
= cmd
->data
;
259 struct device
*dev
= host
->dev
;
262 dev_dbg(dev
, "=============================\n");
263 dev_dbg(dev
, "sdricoh_request opcode=%i\n", cmd
->opcode
);
265 sdricoh_writel(host
, R21C_STATUS
, 0x18);
267 /* read/write commands seem to require this */
269 sdricoh_writew(host
, R226_BLOCKSIZE
, data
->blksz
);
270 sdricoh_writel(host
, R208_DATAIO
, 0);
273 cmd
->error
= sdricoh_mmc_cmd(host
, cmd
);
275 /* read response buffer */
276 if (cmd
->flags
& MMC_RSP_PRESENT
) {
277 if (cmd
->flags
& MMC_RSP_136
) {
278 /* CRC is stripped so we need to do some shifting. */
279 for (i
= 0; i
< 4; i
++) {
282 R20C_RESP
+ (3 - i
) * 4) << 8;
285 sdricoh_readb(host
, R20C_RESP
+
289 cmd
->resp
[0] = sdricoh_readl(host
, R20C_RESP
);
293 if (data
&& cmd
->error
== 0) {
294 dev_dbg(dev
, "transfer: blksz %i blocks %i sg_len %i "
295 "sg length %i\n", data
->blksz
, data
->blocks
,
296 data
->sg_len
, data
->sg
->length
);
298 /* enter data reading mode */
299 sdricoh_writel(host
, R21C_STATUS
, 0x837f031e);
300 for (i
= 0; i
< data
->blocks
; i
++) {
301 size_t len
= data
->blksz
;
305 page
= sg_page(data
->sg
);
307 buf
= kmap(page
) + data
->sg
->offset
+ (len
* i
);
309 sdricoh_blockio(host
,
310 data
->flags
& MMC_DATA_READ
, buf
, len
);
312 flush_dcache_page(page
);
314 dev_err(dev
, "sdricoh_request: cmd %i "
315 "block transfer failed\n", cmd
->opcode
);
319 data
->bytes_xfered
+= len
;
322 sdricoh_writel(host
, R208_DATAIO
, 1);
324 if (sdricoh_query_status(host
, STATUS_TRANSFER_FINISHED
)) {
325 dev_err(dev
, "sdricoh_request: transfer end error\n");
326 cmd
->error
= -EINVAL
;
329 /* FIXME check busy flag */
331 mmc_request_done(mmc
, mrq
);
332 dev_dbg(dev
, "=============================\n");
335 static void sdricoh_set_ios(struct mmc_host
*mmc
, struct mmc_ios
*ios
)
337 struct sdricoh_host
*host
= mmc_priv(mmc
);
338 dev_dbg(host
->dev
, "set_ios\n");
340 if (ios
->power_mode
== MMC_POWER_ON
) {
341 sdricoh_writel(host
, R228_POWER
, 0xc0e0);
343 if (ios
->bus_width
== MMC_BUS_WIDTH_4
) {
344 sdricoh_writel(host
, R224_MODE
, 0x2000300);
345 sdricoh_writel(host
, R228_POWER
, 0x40e0);
347 sdricoh_writel(host
, R224_MODE
, 0x2000340);
350 } else if (ios
->power_mode
== MMC_POWER_UP
) {
351 sdricoh_writel(host
, R224_MODE
, 0x2000320);
352 sdricoh_writel(host
, R228_POWER
, 0xe0);
356 static int sdricoh_get_ro(struct mmc_host
*mmc
)
358 struct sdricoh_host
*host
= mmc_priv(mmc
);
361 status
= sdricoh_readl(host
, R21C_STATUS
);
362 sdricoh_writel(host
, R2E4_STATUS_RESP
, status
);
364 /* some notebooks seem to have the locked flag switched */
366 return !(status
& STATUS_CARD_LOCKED
);
368 return (status
& STATUS_CARD_LOCKED
);
371 static const struct mmc_host_ops sdricoh_ops
= {
372 .request
= sdricoh_request
,
373 .set_ios
= sdricoh_set_ios
,
374 .get_ro
= sdricoh_get_ro
,
377 /* initialize the control and register it to the mmc framework */
378 static int sdricoh_init_mmc(struct pci_dev
*pci_dev
,
379 struct pcmcia_device
*pcmcia_dev
)
382 void __iomem
*iobase
;
383 struct mmc_host
*mmc
;
384 struct sdricoh_host
*host
;
385 struct device
*dev
= &pcmcia_dev
->dev
;
387 if (pci_resource_len(pci_dev
, SDRICOH_PCI_REGION
) !=
388 SDRICOH_PCI_REGION_SIZE
) {
389 dev_dbg(dev
, "unexpected pci resource len\n");
393 pci_iomap(pci_dev
, SDRICOH_PCI_REGION
, SDRICOH_PCI_REGION_SIZE
);
395 dev_err(dev
, "unable to map iobase\n");
399 if (readl(iobase
+ R104_VERSION
) != 0x4000) {
400 dev_dbg(dev
, "no supported mmc controller found\n");
404 /* allocate privdata */
405 mmc
= pcmcia_dev
->priv
=
406 mmc_alloc_host(sizeof(struct sdricoh_host
), &pcmcia_dev
->dev
);
408 dev_err(dev
, "mmc_alloc_host failed\n");
412 host
= mmc_priv(mmc
);
414 host
->iobase
= iobase
;
416 host
->pci_dev
= pci_dev
;
418 mmc
->ops
= &sdricoh_ops
;
420 /* FIXME: frequency and voltage handling is done by the controller
423 mmc
->f_max
= 24000000;
424 mmc
->ocr_avail
= MMC_VDD_32_33
| MMC_VDD_33_34
;
425 mmc
->caps
|= MMC_CAP_4_BIT_DATA
;
427 mmc
->max_seg_size
= 1024 * 512;
428 mmc
->max_blk_size
= 512;
430 /* reset the controller */
431 if (sdricoh_reset(host
)) {
432 dev_dbg(dev
, "could not reset\n");
437 result
= mmc_add_host(mmc
);
440 dev_dbg(dev
, "mmc host registered\n");
446 pci_iounmap(pci_dev
, iobase
);
450 /* search for supported mmc controllers */
451 static int sdricoh_pcmcia_probe(struct pcmcia_device
*pcmcia_dev
)
453 struct pci_dev
*pci_dev
= NULL
;
455 dev_info(&pcmcia_dev
->dev
, "Searching MMC controller for pcmcia device"
456 " %s %s ...\n", pcmcia_dev
->prod_id
[0], pcmcia_dev
->prod_id
[1]);
458 /* search pci cardbus bridge that contains the mmc controller */
459 /* the io region is already claimed by yenta_socket... */
461 pci_get_device(PCI_VENDOR_ID_RICOH
, PCI_DEVICE_ID_RICOH_RL5C476
,
463 /* try to init the device */
464 if (!sdricoh_init_mmc(pci_dev
, pcmcia_dev
)) {
465 dev_info(&pcmcia_dev
->dev
, "MMC controller found\n");
470 dev_err(&pcmcia_dev
->dev
, "No MMC controller was found.\n");
474 static void sdricoh_pcmcia_detach(struct pcmcia_device
*link
)
476 struct mmc_host
*mmc
= link
->priv
;
478 dev_dbg(&link
->dev
, "detach\n");
480 /* remove mmc host */
482 struct sdricoh_host
*host
= mmc_priv(mmc
);
483 mmc_remove_host(mmc
);
484 pci_iounmap(host
->pci_dev
, host
->iobase
);
485 pci_dev_put(host
->pci_dev
);
488 pcmcia_disable_device(link
);
493 static int sdricoh_pcmcia_suspend(struct pcmcia_device
*link
)
495 dev_dbg(&link
->dev
, "suspend\n");
499 static int sdricoh_pcmcia_resume(struct pcmcia_device
*link
)
501 struct mmc_host
*mmc
= link
->priv
;
502 dev_dbg(&link
->dev
, "resume\n");
503 sdricoh_reset(mmc_priv(mmc
));
507 #define sdricoh_pcmcia_suspend NULL
508 #define sdricoh_pcmcia_resume NULL
511 static struct pcmcia_driver sdricoh_driver
= {
513 .probe
= sdricoh_pcmcia_probe
,
514 .remove
= sdricoh_pcmcia_detach
,
515 .id_table
= pcmcia_ids
,
516 .suspend
= sdricoh_pcmcia_suspend
,
517 .resume
= sdricoh_pcmcia_resume
,
519 module_pcmcia_driver(sdricoh_driver
);
521 module_param(switchlocked
, uint
, 0444);
523 MODULE_AUTHOR("Sascha Sommer <saschasommer@freenet.de>");
524 MODULE_DESCRIPTION("Ricoh PCMCIA Secure Digital Interface driver");
525 MODULE_LICENSE("GPL");
527 MODULE_PARM_DESC(switchlocked
, "Switch the cards locked status."
528 "Use this when unlocked cards are shown readonly (default 0)");