sdricoh_cs.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * sdricoh_cs.c - driver for Ricoh Secure Digital Card Readers that can be
  4. * found on some Ricoh RL5c476 II cardbus bridge
  5. *
  6. * Copyright (C) 2006 - 2008 Sascha Sommer <saschasommer@freenet.de>
  7. */
  8. /*
  9. #define DEBUG
  10. #define VERBOSE_DEBUG
  11. */
  12. #include <linux/delay.h>
  13. #include <linux/highmem.h>
  14. #include <linux/module.h>
  15. #include <linux/pci.h>
  16. #include <linux/ioport.h>
  17. #include <linux/iopoll.h>
  18. #include <linux/scatterlist.h>
  19. #include <pcmcia/cistpl.h>
  20. #include <pcmcia/ds.h>
  21. #include <linux/io.h>
  22. #include <linux/mmc/host.h>
  23. #include <linux/mmc/mmc.h>
  24. #define DRIVER_NAME "sdricoh_cs"
  25. static unsigned int switchlocked;
  26. /* i/o region */
  27. #define SDRICOH_PCI_REGION 0
  28. #define SDRICOH_PCI_REGION_SIZE 0x1000
  29. /* registers */
  30. #define R104_VERSION 0x104
  31. #define R200_CMD 0x200
  32. #define R204_CMD_ARG 0x204
  33. #define R208_DATAIO 0x208
  34. #define R20C_RESP 0x20c
  35. #define R21C_STATUS 0x21c
  36. #define R2E0_INIT 0x2e0
  37. #define R2E4_STATUS_RESP 0x2e4
  38. #define R2F0_RESET 0x2f0
  39. #define R224_MODE 0x224
  40. #define R226_BLOCKSIZE 0x226
  41. #define R228_POWER 0x228
  42. #define R230_DATA 0x230
  43. /* flags for the R21C_STATUS register */
  44. #define STATUS_CMD_FINISHED 0x00000001
  45. #define STATUS_TRANSFER_FINISHED 0x00000004
  46. #define STATUS_CARD_INSERTED 0x00000020
  47. #define STATUS_CARD_LOCKED 0x00000080
  48. #define STATUS_CMD_TIMEOUT 0x00400000
  49. #define STATUS_READY_TO_READ 0x01000000
  50. #define STATUS_READY_TO_WRITE 0x02000000
  51. #define STATUS_BUSY 0x40000000
  52. /* timeouts */
  53. #define SDRICOH_CMD_TIMEOUT_US 1000000
  54. #define SDRICOH_DATA_TIMEOUT_US 1000000
  55. /* list of supported pcmcia devices */
  56. static const struct pcmcia_device_id pcmcia_ids[] = {
  57. /* vendor and device strings followed by their crc32 hashes */
  58. PCMCIA_DEVICE_PROD_ID12("RICOH", "Bay1Controller", 0xd9f522ed,
  59. 0xc3901202),
  60. PCMCIA_DEVICE_PROD_ID12("RICOH", "Bay Controller", 0xd9f522ed,
  61. 0xace80909),
  62. PCMCIA_DEVICE_NULL,
  63. };
  64. MODULE_DEVICE_TABLE(pcmcia, pcmcia_ids);
  65. /* mmc privdata */
  66. struct sdricoh_host {
  67. struct device *dev;
  68. struct mmc_host *mmc; /* MMC structure */
  69. unsigned char __iomem *iobase;
  70. struct pci_dev *pci_dev;
  71. int app_cmd;
  72. };
  73. /***************** register i/o helper functions *****************************/
  74. static inline unsigned int sdricoh_readl(struct sdricoh_host *host,
  75. unsigned int reg)
  76. {
  77. unsigned int value = readl(host->iobase + reg);
  78. dev_vdbg(host->dev, "rl %x 0x%x\n", reg, value);
  79. return value;
  80. }
  81. static inline void sdricoh_writel(struct sdricoh_host *host, unsigned int reg,
  82. unsigned int value)
  83. {
  84. writel(value, host->iobase + reg);
  85. dev_vdbg(host->dev, "wl %x 0x%x\n", reg, value);
  86. }
  87. static inline void sdricoh_writew(struct sdricoh_host *host, unsigned int reg,
  88. unsigned short value)
  89. {
  90. writew(value, host->iobase + reg);
  91. dev_vdbg(host->dev, "ww %x 0x%x\n", reg, value);
  92. }
  93. static inline unsigned int sdricoh_readb(struct sdricoh_host *host,
  94. unsigned int reg)
  95. {
  96. unsigned int value = readb(host->iobase + reg);
  97. dev_vdbg(host->dev, "rb %x 0x%x\n", reg, value);
  98. return value;
  99. }
  100. static bool sdricoh_status_ok(struct sdricoh_host *host, unsigned int status,
  101. unsigned int wanted)
  102. {
  103. sdricoh_writel(host, R2E4_STATUS_RESP, status);
  104. return status & wanted;
  105. }
  106. static int sdricoh_query_status(struct sdricoh_host *host, unsigned int wanted)
  107. {
  108. int ret;
  109. unsigned int status = 0;
  110. struct device *dev = host->dev;
  111. ret = read_poll_timeout(sdricoh_readl, status,
  112. sdricoh_status_ok(host, status, wanted),
  113. 32, SDRICOH_DATA_TIMEOUT_US, false,
  114. host, R21C_STATUS);
  115. if (ret) {
  116. dev_err(dev, "query_status: timeout waiting for %x\n", wanted);
  117. return -ETIMEDOUT;
  118. }
  119. /* do not do this check in the loop as some commands fail otherwise */
  120. if (status & 0x7F0000) {
  121. dev_err(dev, "waiting for status bit %x failed\n", wanted);
  122. return -EINVAL;
  123. }
  124. return 0;
  125. }
  126. static int sdricoh_mmc_cmd(struct sdricoh_host *host, struct mmc_command *cmd)
  127. {
  128. unsigned int status, timeout_us;
  129. int ret;
  130. unsigned char opcode = cmd->opcode;
  131. /* reset status reg? */
  132. sdricoh_writel(host, R21C_STATUS, 0x18);
  133. /* MMC_APP_CMDs need some special handling */
  134. if (host->app_cmd) {
  135. opcode |= 64;
  136. host->app_cmd = 0;
  137. } else if (opcode == MMC_APP_CMD)
  138. host->app_cmd = 1;
  139. /* fill parameters */
  140. sdricoh_writel(host, R204_CMD_ARG, cmd->arg);
  141. sdricoh_writel(host, R200_CMD, (0x10000 << 8) | opcode);
  142. /* wait for command completion */
  143. if (!opcode)
  144. return 0;
  145. timeout_us = cmd->busy_timeout ? cmd->busy_timeout * 1000 :
  146. SDRICOH_CMD_TIMEOUT_US;
  147. ret = read_poll_timeout(sdricoh_readl, status,
  148. sdricoh_status_ok(host, status, STATUS_CMD_FINISHED),
  149. 32, timeout_us, false,
  150. host, R21C_STATUS);
  151. /*
  152. * Don't check for timeout status in the loop, as it's not always reset
  153. * correctly.
  154. */
  155. if (ret || status & STATUS_CMD_TIMEOUT)
  156. return -ETIMEDOUT;
  157. return 0;
  158. }
  159. static int sdricoh_reset(struct sdricoh_host *host)
  160. {
  161. dev_dbg(host->dev, "reset\n");
  162. sdricoh_writel(host, R2F0_RESET, 0x10001);
  163. sdricoh_writel(host, R2E0_INIT, 0x10000);
  164. if (sdricoh_readl(host, R2E0_INIT) != 0x10000)
  165. return -EIO;
  166. sdricoh_writel(host, R2E0_INIT, 0x10007);
  167. sdricoh_writel(host, R224_MODE, 0x2000000);
  168. sdricoh_writel(host, R228_POWER, 0xe0);
  169. /* status register ? */
  170. sdricoh_writel(host, R21C_STATUS, 0x18);
  171. return 0;
  172. }
  173. static int sdricoh_blockio(struct sdricoh_host *host, int read,
  174. u8 *buf, int len)
  175. {
  176. int size;
  177. u32 data = 0;
  178. /* wait until the data is available */
  179. if (read) {
  180. if (sdricoh_query_status(host, STATUS_READY_TO_READ))
  181. return -ETIMEDOUT;
  182. sdricoh_writel(host, R21C_STATUS, 0x18);
  183. /* read data */
  184. while (len) {
  185. data = sdricoh_readl(host, R230_DATA);
  186. size = min(len, 4);
  187. len -= size;
  188. while (size) {
  189. *buf = data & 0xFF;
  190. buf++;
  191. data >>= 8;
  192. size--;
  193. }
  194. }
  195. } else {
  196. if (sdricoh_query_status(host, STATUS_READY_TO_WRITE))
  197. return -ETIMEDOUT;
  198. sdricoh_writel(host, R21C_STATUS, 0x18);
  199. /* write data */
  200. while (len) {
  201. size = min(len, 4);
  202. len -= size;
  203. while (size) {
  204. data >>= 8;
  205. data |= (u32)*buf << 24;
  206. buf++;
  207. size--;
  208. }
  209. sdricoh_writel(host, R230_DATA, data);
  210. }
  211. }
  212. return 0;
  213. }
  214. static void sdricoh_request(struct mmc_host *mmc, struct mmc_request *mrq)
  215. {
  216. struct sdricoh_host *host = mmc_priv(mmc);
  217. struct mmc_command *cmd = mrq->cmd;
  218. struct mmc_data *data = cmd->data;
  219. struct device *dev = host->dev;
  220. int i;
  221. dev_dbg(dev, "=============================\n");
  222. dev_dbg(dev, "sdricoh_request opcode=%i\n", cmd->opcode);
  223. sdricoh_writel(host, R21C_STATUS, 0x18);
  224. /* read/write commands seem to require this */
  225. if (data) {
  226. sdricoh_writew(host, R226_BLOCKSIZE, data->blksz);
  227. sdricoh_writel(host, R208_DATAIO, 0);
  228. }
  229. cmd->error = sdricoh_mmc_cmd(host, cmd);
  230. /* read response buffer */
  231. if (cmd->flags & MMC_RSP_PRESENT) {
  232. if (cmd->flags & MMC_RSP_136) {
  233. /* CRC is stripped so we need to do some shifting. */
  234. for (i = 0; i < 4; i++) {
  235. cmd->resp[i] =
  236. sdricoh_readl(host,
  237. R20C_RESP + (3 - i) * 4) << 8;
  238. if (i != 3)
  239. cmd->resp[i] |=
  240. sdricoh_readb(host, R20C_RESP +
  241. (3 - i) * 4 - 1);
  242. }
  243. } else
  244. cmd->resp[0] = sdricoh_readl(host, R20C_RESP);
  245. }
  246. /* transfer data */
  247. if (data && cmd->error == 0) {
  248. dev_dbg(dev, "transfer: blksz %i blocks %i sg_len %i "
  249. "sg length %i\n", data->blksz, data->blocks,
  250. data->sg_len, data->sg->length);
  251. /* enter data reading mode */
  252. sdricoh_writel(host, R21C_STATUS, 0x837f031e);
  253. for (i = 0; i < data->blocks; i++) {
  254. size_t len = data->blksz;
  255. u8 *buf;
  256. struct page *page;
  257. int result;
  258. page = sg_page(data->sg);
  259. buf = kmap(page) + data->sg->offset + (len * i);
  260. result =
  261. sdricoh_blockio(host,
  262. data->flags & MMC_DATA_READ, buf, len);
  263. kunmap(page);
  264. flush_dcache_page(page);
  265. if (result) {
  266. dev_err(dev, "sdricoh_request: cmd %i "
  267. "block transfer failed\n", cmd->opcode);
  268. cmd->error = result;
  269. break;
  270. } else
  271. data->bytes_xfered += len;
  272. }
  273. sdricoh_writel(host, R208_DATAIO, 1);
  274. if (sdricoh_query_status(host, STATUS_TRANSFER_FINISHED)) {
  275. dev_err(dev, "sdricoh_request: transfer end error\n");
  276. cmd->error = -EINVAL;
  277. }
  278. }
  279. /* FIXME check busy flag */
  280. mmc_request_done(mmc, mrq);
  281. dev_dbg(dev, "=============================\n");
  282. }
  283. static void sdricoh_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
  284. {
  285. struct sdricoh_host *host = mmc_priv(mmc);
  286. dev_dbg(host->dev, "set_ios\n");
  287. if (ios->power_mode == MMC_POWER_ON) {
  288. sdricoh_writel(host, R228_POWER, 0xc0e0);
  289. if (ios->bus_width == MMC_BUS_WIDTH_4) {
  290. sdricoh_writel(host, R224_MODE, 0x2000300);
  291. sdricoh_writel(host, R228_POWER, 0x40e0);
  292. } else {
  293. sdricoh_writel(host, R224_MODE, 0x2000340);
  294. }
  295. } else if (ios->power_mode == MMC_POWER_UP) {
  296. sdricoh_writel(host, R224_MODE, 0x2000320);
  297. sdricoh_writel(host, R228_POWER, 0xe0);
  298. }
  299. }
  300. static int sdricoh_get_ro(struct mmc_host *mmc)
  301. {
  302. struct sdricoh_host *host = mmc_priv(mmc);
  303. unsigned int status;
  304. status = sdricoh_readl(host, R21C_STATUS);
  305. sdricoh_writel(host, R2E4_STATUS_RESP, status);
  306. /* some notebooks seem to have the locked flag switched */
  307. if (switchlocked)
  308. return !(status & STATUS_CARD_LOCKED);
  309. return (status & STATUS_CARD_LOCKED);
  310. }
  311. static const struct mmc_host_ops sdricoh_ops = {
  312. .request = sdricoh_request,
  313. .set_ios = sdricoh_set_ios,
  314. .get_ro = sdricoh_get_ro,
  315. };
  316. /* initialize the control and register it to the mmc framework */
  317. static int sdricoh_init_mmc(struct pci_dev *pci_dev,
  318. struct pcmcia_device *pcmcia_dev)
  319. {
  320. int result;
  321. void __iomem *iobase;
  322. struct mmc_host *mmc;
  323. struct sdricoh_host *host;
  324. struct device *dev = &pcmcia_dev->dev;
  325. /* map iomem */
  326. if (pci_resource_len(pci_dev, SDRICOH_PCI_REGION) !=
  327. SDRICOH_PCI_REGION_SIZE) {
  328. dev_dbg(dev, "unexpected pci resource len\n");
  329. return -ENODEV;
  330. }
  331. iobase =
  332. pci_iomap(pci_dev, SDRICOH_PCI_REGION, SDRICOH_PCI_REGION_SIZE);
  333. if (!iobase) {
  334. dev_err(dev, "unable to map iobase\n");
  335. return -ENODEV;
  336. }
  337. /* check version? */
  338. if (readl(iobase + R104_VERSION) != 0x4000) {
  339. dev_dbg(dev, "no supported mmc controller found\n");
  340. result = -ENODEV;
  341. goto unmap_io;
  342. }
  343. /* allocate privdata */
  344. mmc = pcmcia_dev->priv =
  345. devm_mmc_alloc_host(&pcmcia_dev->dev, sizeof(*host));
  346. if (!mmc) {
  347. dev_err(dev, "devm_mmc_alloc_host failed\n");
  348. result = -ENOMEM;
  349. goto unmap_io;
  350. }
  351. host = mmc_priv(mmc);
  352. host->iobase = iobase;
  353. host->dev = dev;
  354. host->pci_dev = pci_dev;
  355. mmc->ops = &sdricoh_ops;
  356. /* FIXME: frequency and voltage handling is done by the controller
  357. */
  358. mmc->f_min = 450000;
  359. mmc->f_max = 24000000;
  360. mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34;
  361. mmc->caps |= MMC_CAP_4_BIT_DATA;
  362. mmc->max_seg_size = 1024 * 512;
  363. mmc->max_blk_size = 512;
  364. /* reset the controller */
  365. if (sdricoh_reset(host)) {
  366. dev_dbg(dev, "could not reset\n");
  367. result = -EIO;
  368. goto unmap_io;
  369. }
  370. result = mmc_add_host(mmc);
  371. if (!result) {
  372. dev_dbg(dev, "mmc host registered\n");
  373. return 0;
  374. }
  375. unmap_io:
  376. pci_iounmap(pci_dev, iobase);
  377. return result;
  378. }
  379. /* search for supported mmc controllers */
  380. static int sdricoh_pcmcia_probe(struct pcmcia_device *pcmcia_dev)
  381. {
  382. struct pci_dev *pci_dev = NULL;
  383. dev_info(&pcmcia_dev->dev, "Searching MMC controller for pcmcia device"
  384. " %s %s ...\n", pcmcia_dev->prod_id[0], pcmcia_dev->prod_id[1]);
  385. /* search pci cardbus bridge that contains the mmc controller */
  386. /* the io region is already claimed by yenta_socket... */
  387. while ((pci_dev =
  388. pci_get_device(PCI_VENDOR_ID_RICOH, PCI_DEVICE_ID_RICOH_RL5C476,
  389. pci_dev))) {
  390. /* try to init the device */
  391. if (!sdricoh_init_mmc(pci_dev, pcmcia_dev)) {
  392. dev_info(&pcmcia_dev->dev, "MMC controller found\n");
  393. return 0;
  394. }
  395. }
  396. dev_err(&pcmcia_dev->dev, "No MMC controller was found.\n");
  397. return -ENODEV;
  398. }
  399. static void sdricoh_pcmcia_detach(struct pcmcia_device *link)
  400. {
  401. struct mmc_host *mmc = link->priv;
  402. dev_dbg(&link->dev, "detach\n");
  403. /* remove mmc host */
  404. if (mmc) {
  405. struct sdricoh_host *host = mmc_priv(mmc);
  406. mmc_remove_host(mmc);
  407. pci_iounmap(host->pci_dev, host->iobase);
  408. pci_dev_put(host->pci_dev);
  409. }
  410. pcmcia_disable_device(link);
  411. }
  412. #ifdef CONFIG_PM
  413. static int sdricoh_pcmcia_suspend(struct pcmcia_device *link)
  414. {
  415. dev_dbg(&link->dev, "suspend\n");
  416. return 0;
  417. }
  418. static int sdricoh_pcmcia_resume(struct pcmcia_device *link)
  419. {
  420. struct mmc_host *mmc = link->priv;
  421. dev_dbg(&link->dev, "resume\n");
  422. sdricoh_reset(mmc_priv(mmc));
  423. return 0;
  424. }
  425. #else
  426. #define sdricoh_pcmcia_suspend NULL
  427. #define sdricoh_pcmcia_resume NULL
  428. #endif
  429. static struct pcmcia_driver sdricoh_driver = {
  430. .name = DRIVER_NAME,
  431. .probe = sdricoh_pcmcia_probe,
  432. .remove = sdricoh_pcmcia_detach,
  433. .id_table = pcmcia_ids,
  434. .suspend = sdricoh_pcmcia_suspend,
  435. .resume = sdricoh_pcmcia_resume,
  436. };
  437. module_pcmcia_driver(sdricoh_driver);
  438. module_param(switchlocked, uint, 0444);
  439. MODULE_AUTHOR("Sascha Sommer <saschasommer@freenet.de>");
  440. MODULE_DESCRIPTION("Ricoh PCMCIA Secure Digital Interface driver");
  441. MODULE_LICENSE("GPL");
  442. MODULE_PARM_DESC(switchlocked, "Switch the cards locked status."
  443. "Use this when unlocked cards are shown readonly (default 0)");