sdricoh_cs.c 14 KB

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