cb710-mmc.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781
  1. /*
  2. * cb710/mmc.c
  3. *
  4. * Copyright by Michał Mirosław, 2008-2009
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. */
  10. #include <linux/kernel.h>
  11. #include <linux/module.h>
  12. #include <linux/pci.h>
  13. #include <linux/delay.h>
  14. #include "cb710-mmc.h"
  15. static const u8 cb710_clock_divider_log2[8] = {
  16. /* 1, 2, 4, 8, 16, 32, 128, 512 */
  17. 0, 1, 2, 3, 4, 5, 7, 9
  18. };
  19. #define CB710_MAX_DIVIDER_IDX \
  20. (ARRAY_SIZE(cb710_clock_divider_log2) - 1)
  21. static const u8 cb710_src_freq_mhz[16] = {
  22. 33, 10, 20, 25, 30, 35, 40, 45,
  23. 50, 55, 60, 65, 70, 75, 80, 85
  24. };
  25. static void cb710_mmc_select_clock_divider(struct mmc_host *mmc, int hz)
  26. {
  27. struct cb710_slot *slot = cb710_mmc_to_slot(mmc);
  28. struct pci_dev *pdev = cb710_slot_to_chip(slot)->pdev;
  29. u32 src_freq_idx;
  30. u32 divider_idx;
  31. int src_hz;
  32. /* on CB710 in HP nx9500:
  33. * src_freq_idx == 0
  34. * indexes 1-7 work as written in the table
  35. * indexes 0,8-15 give no clock output
  36. */
  37. pci_read_config_dword(pdev, 0x48, &src_freq_idx);
  38. src_freq_idx = (src_freq_idx >> 16) & 0xF;
  39. src_hz = cb710_src_freq_mhz[src_freq_idx] * 1000000;
  40. for (divider_idx = 0; divider_idx < CB710_MAX_DIVIDER_IDX; ++divider_idx) {
  41. if (hz >= src_hz >> cb710_clock_divider_log2[divider_idx])
  42. break;
  43. }
  44. if (src_freq_idx)
  45. divider_idx |= 0x8;
  46. else if (divider_idx == 0)
  47. divider_idx = 1;
  48. cb710_pci_update_config_reg(pdev, 0x40, ~0xF0000000, divider_idx << 28);
  49. dev_dbg(cb710_slot_dev(slot),
  50. "clock set to %d Hz, wanted %d Hz; src_freq_idx = %d, divider_idx = %d|%d\n",
  51. src_hz >> cb710_clock_divider_log2[divider_idx & 7],
  52. hz, src_freq_idx, divider_idx & 7, divider_idx & 8);
  53. }
  54. static void __cb710_mmc_enable_irq(struct cb710_slot *slot,
  55. unsigned short enable, unsigned short mask)
  56. {
  57. /* clear global IE
  58. * - it gets set later if any interrupt sources are enabled */
  59. mask |= CB710_MMC_IE_IRQ_ENABLE;
  60. /* look like interrupt is fired whenever
  61. * WORD[0x0C] & WORD[0x10] != 0;
  62. * -> bit 15 port 0x0C seems to be global interrupt enable
  63. */
  64. enable = (cb710_read_port_16(slot, CB710_MMC_IRQ_ENABLE_PORT)
  65. & ~mask) | enable;
  66. if (enable)
  67. enable |= CB710_MMC_IE_IRQ_ENABLE;
  68. cb710_write_port_16(slot, CB710_MMC_IRQ_ENABLE_PORT, enable);
  69. }
  70. static void cb710_mmc_enable_irq(struct cb710_slot *slot,
  71. unsigned short enable, unsigned short mask)
  72. {
  73. struct cb710_mmc_reader *reader = mmc_priv(cb710_slot_to_mmc(slot));
  74. unsigned long flags;
  75. spin_lock_irqsave(&reader->irq_lock, flags);
  76. /* this is the only thing irq_lock protects */
  77. __cb710_mmc_enable_irq(slot, enable, mask);
  78. spin_unlock_irqrestore(&reader->irq_lock, flags);
  79. }
  80. static void cb710_mmc_reset_events(struct cb710_slot *slot)
  81. {
  82. cb710_write_port_8(slot, CB710_MMC_STATUS0_PORT, 0xFF);
  83. cb710_write_port_8(slot, CB710_MMC_STATUS1_PORT, 0xFF);
  84. cb710_write_port_8(slot, CB710_MMC_STATUS2_PORT, 0xFF);
  85. }
  86. static void cb710_mmc_enable_4bit_data(struct cb710_slot *slot, int enable)
  87. {
  88. if (enable)
  89. cb710_modify_port_8(slot, CB710_MMC_CONFIG1_PORT,
  90. CB710_MMC_C1_4BIT_DATA_BUS, 0);
  91. else
  92. cb710_modify_port_8(slot, CB710_MMC_CONFIG1_PORT,
  93. 0, CB710_MMC_C1_4BIT_DATA_BUS);
  94. }
  95. static int cb710_check_event(struct cb710_slot *slot, u8 what)
  96. {
  97. u16 status;
  98. status = cb710_read_port_16(slot, CB710_MMC_STATUS_PORT);
  99. if (status & CB710_MMC_S0_FIFO_UNDERFLOW) {
  100. /* it is just a guess, so log it */
  101. dev_dbg(cb710_slot_dev(slot),
  102. "CHECK : ignoring bit 6 in status %04X\n", status);
  103. cb710_write_port_8(slot, CB710_MMC_STATUS0_PORT,
  104. CB710_MMC_S0_FIFO_UNDERFLOW);
  105. status &= ~CB710_MMC_S0_FIFO_UNDERFLOW;
  106. }
  107. if (status & CB710_MMC_STATUS_ERROR_EVENTS) {
  108. dev_dbg(cb710_slot_dev(slot),
  109. "CHECK : returning EIO on status %04X\n", status);
  110. cb710_write_port_8(slot, CB710_MMC_STATUS0_PORT, status & 0xFF);
  111. cb710_write_port_8(slot, CB710_MMC_STATUS1_PORT,
  112. CB710_MMC_S1_RESET);
  113. return -EIO;
  114. }
  115. /* 'what' is a bit in MMC_STATUS1 */
  116. if ((status >> 8) & what) {
  117. cb710_write_port_8(slot, CB710_MMC_STATUS1_PORT, what);
  118. return 1;
  119. }
  120. return 0;
  121. }
  122. static int cb710_wait_for_event(struct cb710_slot *slot, u8 what)
  123. {
  124. int err = 0;
  125. unsigned limit = 2000000; /* FIXME: real timeout */
  126. #ifdef CONFIG_CB710_DEBUG
  127. u32 e, x;
  128. e = cb710_read_port_32(slot, CB710_MMC_STATUS_PORT);
  129. #endif
  130. while (!(err = cb710_check_event(slot, what))) {
  131. if (!--limit) {
  132. cb710_dump_regs(cb710_slot_to_chip(slot),
  133. CB710_DUMP_REGS_MMC);
  134. err = -ETIMEDOUT;
  135. break;
  136. }
  137. udelay(1);
  138. }
  139. #ifdef CONFIG_CB710_DEBUG
  140. x = cb710_read_port_32(slot, CB710_MMC_STATUS_PORT);
  141. limit = 2000000 - limit;
  142. if (limit > 100)
  143. dev_dbg(cb710_slot_dev(slot),
  144. "WAIT10: waited %d loops, what %d, entry val %08X, exit val %08X\n",
  145. limit, what, e, x);
  146. #endif
  147. return err < 0 ? err : 0;
  148. }
  149. static int cb710_wait_while_busy(struct cb710_slot *slot, uint8_t mask)
  150. {
  151. unsigned limit = 500000; /* FIXME: real timeout */
  152. int err = 0;
  153. #ifdef CONFIG_CB710_DEBUG
  154. u32 e, x;
  155. e = cb710_read_port_32(slot, CB710_MMC_STATUS_PORT);
  156. #endif
  157. while (cb710_read_port_8(slot, CB710_MMC_STATUS2_PORT) & mask) {
  158. if (!--limit) {
  159. cb710_dump_regs(cb710_slot_to_chip(slot),
  160. CB710_DUMP_REGS_MMC);
  161. err = -ETIMEDOUT;
  162. break;
  163. }
  164. udelay(1);
  165. }
  166. #ifdef CONFIG_CB710_DEBUG
  167. x = cb710_read_port_32(slot, CB710_MMC_STATUS_PORT);
  168. limit = 500000 - limit;
  169. if (limit > 100)
  170. dev_dbg(cb710_slot_dev(slot),
  171. "WAIT12: waited %d loops, mask %02X, entry val %08X, exit val %08X\n",
  172. limit, mask, e, x);
  173. #endif
  174. return err;
  175. }
  176. static void cb710_mmc_set_transfer_size(struct cb710_slot *slot,
  177. size_t count, size_t blocksize)
  178. {
  179. cb710_wait_while_busy(slot, CB710_MMC_S2_BUSY_20);
  180. cb710_write_port_32(slot, CB710_MMC_TRANSFER_SIZE_PORT,
  181. ((count - 1) << 16)|(blocksize - 1));
  182. dev_vdbg(cb710_slot_dev(slot), "set up for %zu block%s of %zu bytes\n",
  183. count, count == 1 ? "" : "s", blocksize);
  184. }
  185. static void cb710_mmc_fifo_hack(struct cb710_slot *slot)
  186. {
  187. /* without this, received data is prepended with 8-bytes of zeroes */
  188. u32 r1, r2;
  189. int ok = 0;
  190. r1 = cb710_read_port_32(slot, CB710_MMC_DATA_PORT);
  191. r2 = cb710_read_port_32(slot, CB710_MMC_DATA_PORT);
  192. if (cb710_read_port_8(slot, CB710_MMC_STATUS0_PORT)
  193. & CB710_MMC_S0_FIFO_UNDERFLOW) {
  194. cb710_write_port_8(slot, CB710_MMC_STATUS0_PORT,
  195. CB710_MMC_S0_FIFO_UNDERFLOW);
  196. ok = 1;
  197. }
  198. dev_dbg(cb710_slot_dev(slot),
  199. "FIFO-read-hack: expected STATUS0 bit was %s\n",
  200. ok ? "set." : "NOT SET!");
  201. dev_dbg(cb710_slot_dev(slot),
  202. "FIFO-read-hack: dwords ignored: %08X %08X - %s\n",
  203. r1, r2, (r1|r2) ? "BAD (NOT ZERO)!" : "ok");
  204. }
  205. static int cb710_mmc_receive_pio(struct cb710_slot *slot,
  206. struct sg_mapping_iter *miter, size_t dw_count)
  207. {
  208. if (!(cb710_read_port_8(slot, CB710_MMC_STATUS2_PORT) & CB710_MMC_S2_FIFO_READY)) {
  209. int err = cb710_wait_for_event(slot,
  210. CB710_MMC_S1_PIO_TRANSFER_DONE);
  211. if (err)
  212. return err;
  213. }
  214. cb710_sg_dwiter_write_from_io(miter,
  215. slot->iobase + CB710_MMC_DATA_PORT, dw_count);
  216. return 0;
  217. }
  218. static bool cb710_is_transfer_size_supported(struct mmc_data *data)
  219. {
  220. return !(data->blksz & 15 && (data->blocks != 1 || data->blksz != 8));
  221. }
  222. static int cb710_mmc_receive(struct cb710_slot *slot, struct mmc_data *data)
  223. {
  224. struct sg_mapping_iter miter;
  225. size_t len, blocks = data->blocks;
  226. int err = 0;
  227. /* TODO: I don't know how/if the hardware handles non-16B-boundary blocks
  228. * except single 8B block */
  229. if (unlikely(data->blksz & 15 && (data->blocks != 1 || data->blksz != 8)))
  230. return -EINVAL;
  231. sg_miter_start(&miter, data->sg, data->sg_len, SG_MITER_TO_SG);
  232. cb710_modify_port_8(slot, CB710_MMC_CONFIG2_PORT,
  233. 15, CB710_MMC_C2_READ_PIO_SIZE_MASK);
  234. cb710_mmc_fifo_hack(slot);
  235. while (blocks-- > 0) {
  236. len = data->blksz;
  237. while (len >= 16) {
  238. err = cb710_mmc_receive_pio(slot, &miter, 4);
  239. if (err)
  240. goto out;
  241. len -= 16;
  242. }
  243. if (!len)
  244. continue;
  245. cb710_modify_port_8(slot, CB710_MMC_CONFIG2_PORT,
  246. len - 1, CB710_MMC_C2_READ_PIO_SIZE_MASK);
  247. len = (len >= 8) ? 4 : 2;
  248. err = cb710_mmc_receive_pio(slot, &miter, len);
  249. if (err)
  250. goto out;
  251. }
  252. out:
  253. sg_miter_stop(&miter);
  254. return err;
  255. }
  256. static int cb710_mmc_send(struct cb710_slot *slot, struct mmc_data *data)
  257. {
  258. struct sg_mapping_iter miter;
  259. size_t len, blocks = data->blocks;
  260. int err = 0;
  261. /* TODO: I don't know how/if the hardware handles multiple
  262. * non-16B-boundary blocks */
  263. if (unlikely(data->blocks > 1 && data->blksz & 15))
  264. return -EINVAL;
  265. sg_miter_start(&miter, data->sg, data->sg_len, SG_MITER_FROM_SG);
  266. cb710_modify_port_8(slot, CB710_MMC_CONFIG2_PORT,
  267. 0, CB710_MMC_C2_READ_PIO_SIZE_MASK);
  268. while (blocks-- > 0) {
  269. len = (data->blksz + 15) >> 4;
  270. do {
  271. if (!(cb710_read_port_8(slot, CB710_MMC_STATUS2_PORT)
  272. & CB710_MMC_S2_FIFO_EMPTY)) {
  273. err = cb710_wait_for_event(slot,
  274. CB710_MMC_S1_PIO_TRANSFER_DONE);
  275. if (err)
  276. goto out;
  277. }
  278. cb710_sg_dwiter_read_to_io(&miter,
  279. slot->iobase + CB710_MMC_DATA_PORT, 4);
  280. } while (--len);
  281. }
  282. out:
  283. sg_miter_stop(&miter);
  284. return err;
  285. }
  286. static u16 cb710_encode_cmd_flags(struct cb710_mmc_reader *reader,
  287. struct mmc_command *cmd)
  288. {
  289. unsigned int flags = cmd->flags;
  290. u16 cb_flags = 0;
  291. /* Windows driver returned 0 for commands for which no response
  292. * is expected. It happened that there were only two such commands
  293. * used: MMC_GO_IDLE_STATE and MMC_GO_INACTIVE_STATE so it might
  294. * as well be a bug in that driver.
  295. *
  296. * Original driver set bit 14 for MMC/SD application
  297. * commands. There's no difference 'on the wire' and
  298. * it apparently works without it anyway.
  299. */
  300. switch (flags & MMC_CMD_MASK) {
  301. case MMC_CMD_AC: cb_flags = CB710_MMC_CMD_AC; break;
  302. case MMC_CMD_ADTC: cb_flags = CB710_MMC_CMD_ADTC; break;
  303. case MMC_CMD_BC: cb_flags = CB710_MMC_CMD_BC; break;
  304. case MMC_CMD_BCR: cb_flags = CB710_MMC_CMD_BCR; break;
  305. }
  306. if (flags & MMC_RSP_BUSY)
  307. cb_flags |= CB710_MMC_RSP_BUSY;
  308. cb_flags |= cmd->opcode << CB710_MMC_CMD_CODE_SHIFT;
  309. if (cmd->data && (cmd->data->flags & MMC_DATA_READ))
  310. cb_flags |= CB710_MMC_DATA_READ;
  311. if (flags & MMC_RSP_PRESENT) {
  312. /* Windows driver set 01 at bits 4,3 except for
  313. * MMC_SET_BLOCKLEN where it set 10. Maybe the
  314. * hardware can do something special about this
  315. * command? The original driver looks buggy/incomplete
  316. * anyway so we ignore this for now.
  317. *
  318. * I assume that 00 here means no response is expected.
  319. */
  320. cb_flags |= CB710_MMC_RSP_PRESENT;
  321. if (flags & MMC_RSP_136)
  322. cb_flags |= CB710_MMC_RSP_136;
  323. if (!(flags & MMC_RSP_CRC))
  324. cb_flags |= CB710_MMC_RSP_NO_CRC;
  325. }
  326. return cb_flags;
  327. }
  328. static void cb710_receive_response(struct cb710_slot *slot,
  329. struct mmc_command *cmd)
  330. {
  331. unsigned rsp_opcode, wanted_opcode;
  332. /* Looks like final byte with CRC is always stripped (same as SDHCI) */
  333. if (cmd->flags & MMC_RSP_136) {
  334. u32 resp[4];
  335. resp[0] = cb710_read_port_32(slot, CB710_MMC_RESPONSE3_PORT);
  336. resp[1] = cb710_read_port_32(slot, CB710_MMC_RESPONSE2_PORT);
  337. resp[2] = cb710_read_port_32(slot, CB710_MMC_RESPONSE1_PORT);
  338. resp[3] = cb710_read_port_32(slot, CB710_MMC_RESPONSE0_PORT);
  339. rsp_opcode = resp[0] >> 24;
  340. cmd->resp[0] = (resp[0] << 8)|(resp[1] >> 24);
  341. cmd->resp[1] = (resp[1] << 8)|(resp[2] >> 24);
  342. cmd->resp[2] = (resp[2] << 8)|(resp[3] >> 24);
  343. cmd->resp[3] = (resp[3] << 8);
  344. } else {
  345. rsp_opcode = cb710_read_port_32(slot, CB710_MMC_RESPONSE1_PORT) & 0x3F;
  346. cmd->resp[0] = cb710_read_port_32(slot, CB710_MMC_RESPONSE0_PORT);
  347. }
  348. wanted_opcode = (cmd->flags & MMC_RSP_OPCODE) ? cmd->opcode : 0x3F;
  349. if (rsp_opcode != wanted_opcode)
  350. cmd->error = -EILSEQ;
  351. }
  352. static int cb710_mmc_transfer_data(struct cb710_slot *slot,
  353. struct mmc_data *data)
  354. {
  355. int error, to;
  356. if (data->flags & MMC_DATA_READ)
  357. error = cb710_mmc_receive(slot, data);
  358. else
  359. error = cb710_mmc_send(slot, data);
  360. to = cb710_wait_for_event(slot, CB710_MMC_S1_DATA_TRANSFER_DONE);
  361. if (!error)
  362. error = to;
  363. if (!error)
  364. data->bytes_xfered = data->blksz * data->blocks;
  365. return error;
  366. }
  367. static int cb710_mmc_command(struct mmc_host *mmc, struct mmc_command *cmd)
  368. {
  369. struct cb710_slot *slot = cb710_mmc_to_slot(mmc);
  370. struct cb710_mmc_reader *reader = mmc_priv(mmc);
  371. struct mmc_data *data = cmd->data;
  372. u16 cb_cmd = cb710_encode_cmd_flags(reader, cmd);
  373. dev_dbg(cb710_slot_dev(slot), "cmd request: 0x%04X\n", cb_cmd);
  374. if (data) {
  375. if (!cb710_is_transfer_size_supported(data)) {
  376. data->error = -EINVAL;
  377. return -1;
  378. }
  379. cb710_mmc_set_transfer_size(slot, data->blocks, data->blksz);
  380. }
  381. cb710_wait_while_busy(slot, CB710_MMC_S2_BUSY_20|CB710_MMC_S2_BUSY_10);
  382. cb710_write_port_16(slot, CB710_MMC_CMD_TYPE_PORT, cb_cmd);
  383. cb710_wait_while_busy(slot, CB710_MMC_S2_BUSY_20);
  384. cb710_write_port_32(slot, CB710_MMC_CMD_PARAM_PORT, cmd->arg);
  385. cb710_mmc_reset_events(slot);
  386. cb710_wait_while_busy(slot, CB710_MMC_S2_BUSY_20);
  387. cb710_modify_port_8(slot, CB710_MMC_CONFIG0_PORT, 0x01, 0);
  388. cmd->error = cb710_wait_for_event(slot, CB710_MMC_S1_COMMAND_SENT);
  389. if (cmd->error)
  390. return -1;
  391. if (cmd->flags & MMC_RSP_PRESENT) {
  392. cb710_receive_response(slot, cmd);
  393. if (cmd->error)
  394. return -1;
  395. }
  396. if (data)
  397. data->error = cb710_mmc_transfer_data(slot, data);
  398. return 0;
  399. }
  400. static void cb710_mmc_request(struct mmc_host *mmc, struct mmc_request *mrq)
  401. {
  402. struct cb710_slot *slot = cb710_mmc_to_slot(mmc);
  403. struct cb710_mmc_reader *reader = mmc_priv(mmc);
  404. WARN_ON(reader->mrq != NULL);
  405. reader->mrq = mrq;
  406. cb710_mmc_enable_irq(slot, CB710_MMC_IE_TEST_MASK, 0);
  407. if (!cb710_mmc_command(mmc, mrq->cmd) && mrq->stop)
  408. cb710_mmc_command(mmc, mrq->stop);
  409. tasklet_schedule(&reader->finish_req_tasklet);
  410. }
  411. static int cb710_mmc_powerup(struct cb710_slot *slot)
  412. {
  413. #ifdef CONFIG_CB710_DEBUG
  414. struct cb710_chip *chip = cb710_slot_to_chip(slot);
  415. #endif
  416. int err;
  417. /* a lot of magic for now */
  418. dev_dbg(cb710_slot_dev(slot), "bus powerup\n");
  419. cb710_dump_regs(chip, CB710_DUMP_REGS_MMC);
  420. err = cb710_wait_while_busy(slot, CB710_MMC_S2_BUSY_20);
  421. if (unlikely(err))
  422. return err;
  423. cb710_modify_port_8(slot, CB710_MMC_CONFIG1_PORT, 0x80, 0);
  424. cb710_modify_port_8(slot, CB710_MMC_CONFIG3_PORT, 0x80, 0);
  425. cb710_dump_regs(chip, CB710_DUMP_REGS_MMC);
  426. mdelay(1);
  427. dev_dbg(cb710_slot_dev(slot), "after delay 1\n");
  428. cb710_dump_regs(chip, CB710_DUMP_REGS_MMC);
  429. err = cb710_wait_while_busy(slot, CB710_MMC_S2_BUSY_20);
  430. if (unlikely(err))
  431. return err;
  432. cb710_modify_port_8(slot, CB710_MMC_CONFIG1_PORT, 0x09, 0);
  433. cb710_dump_regs(chip, CB710_DUMP_REGS_MMC);
  434. mdelay(1);
  435. dev_dbg(cb710_slot_dev(slot), "after delay 2\n");
  436. cb710_dump_regs(chip, CB710_DUMP_REGS_MMC);
  437. err = cb710_wait_while_busy(slot, CB710_MMC_S2_BUSY_20);
  438. if (unlikely(err))
  439. return err;
  440. cb710_modify_port_8(slot, CB710_MMC_CONFIG1_PORT, 0, 0x08);
  441. cb710_dump_regs(chip, CB710_DUMP_REGS_MMC);
  442. mdelay(2);
  443. dev_dbg(cb710_slot_dev(slot), "after delay 3\n");
  444. cb710_dump_regs(chip, CB710_DUMP_REGS_MMC);
  445. cb710_modify_port_8(slot, CB710_MMC_CONFIG0_PORT, 0x06, 0);
  446. cb710_modify_port_8(slot, CB710_MMC_CONFIG1_PORT, 0x70, 0);
  447. cb710_modify_port_8(slot, CB710_MMC_CONFIG2_PORT, 0x80, 0);
  448. cb710_modify_port_8(slot, CB710_MMC_CONFIG3_PORT, 0x03, 0);
  449. cb710_dump_regs(chip, CB710_DUMP_REGS_MMC);
  450. err = cb710_wait_while_busy(slot, CB710_MMC_S2_BUSY_20);
  451. if (unlikely(err))
  452. return err;
  453. /* This port behaves weird: quick byte reads of 0x08,0x09 return
  454. * 0xFF,0x00 after writing 0xFFFF to 0x08; it works correctly when
  455. * read/written from userspace... What am I missing here?
  456. * (it doesn't depend on write-to-read delay) */
  457. cb710_write_port_16(slot, CB710_MMC_CONFIGB_PORT, 0xFFFF);
  458. cb710_modify_port_8(slot, CB710_MMC_CONFIG0_PORT, 0x06, 0);
  459. cb710_dump_regs(chip, CB710_DUMP_REGS_MMC);
  460. dev_dbg(cb710_slot_dev(slot), "bus powerup finished\n");
  461. return cb710_check_event(slot, 0);
  462. }
  463. static void cb710_mmc_powerdown(struct cb710_slot *slot)
  464. {
  465. cb710_modify_port_8(slot, CB710_MMC_CONFIG1_PORT, 0, 0x81);
  466. cb710_modify_port_8(slot, CB710_MMC_CONFIG3_PORT, 0, 0x80);
  467. }
  468. static void cb710_mmc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
  469. {
  470. struct cb710_slot *slot = cb710_mmc_to_slot(mmc);
  471. struct cb710_mmc_reader *reader = mmc_priv(mmc);
  472. int err;
  473. cb710_mmc_select_clock_divider(mmc, ios->clock);
  474. if (ios->power_mode != reader->last_power_mode)
  475. switch (ios->power_mode) {
  476. case MMC_POWER_ON:
  477. err = cb710_mmc_powerup(slot);
  478. if (err) {
  479. dev_warn(cb710_slot_dev(slot),
  480. "powerup failed (%d)- retrying\n", err);
  481. cb710_mmc_powerdown(slot);
  482. udelay(1);
  483. err = cb710_mmc_powerup(slot);
  484. if (err)
  485. dev_warn(cb710_slot_dev(slot),
  486. "powerup retry failed (%d) - expect errors\n",
  487. err);
  488. }
  489. reader->last_power_mode = MMC_POWER_ON;
  490. break;
  491. case MMC_POWER_OFF:
  492. cb710_mmc_powerdown(slot);
  493. reader->last_power_mode = MMC_POWER_OFF;
  494. break;
  495. case MMC_POWER_UP:
  496. default:
  497. /* ignore */;
  498. }
  499. cb710_mmc_enable_4bit_data(slot, ios->bus_width != MMC_BUS_WIDTH_1);
  500. cb710_mmc_enable_irq(slot, CB710_MMC_IE_TEST_MASK, 0);
  501. }
  502. static int cb710_mmc_get_ro(struct mmc_host *mmc)
  503. {
  504. struct cb710_slot *slot = cb710_mmc_to_slot(mmc);
  505. return cb710_read_port_8(slot, CB710_MMC_STATUS3_PORT)
  506. & CB710_MMC_S3_WRITE_PROTECTED;
  507. }
  508. static int cb710_mmc_get_cd(struct mmc_host *mmc)
  509. {
  510. struct cb710_slot *slot = cb710_mmc_to_slot(mmc);
  511. return cb710_read_port_8(slot, CB710_MMC_STATUS3_PORT)
  512. & CB710_MMC_S3_CARD_DETECTED;
  513. }
  514. static int cb710_mmc_irq_handler(struct cb710_slot *slot)
  515. {
  516. struct mmc_host *mmc = cb710_slot_to_mmc(slot);
  517. struct cb710_mmc_reader *reader = mmc_priv(mmc);
  518. u32 status, config1, config2, irqen;
  519. status = cb710_read_port_32(slot, CB710_MMC_STATUS_PORT);
  520. irqen = cb710_read_port_32(slot, CB710_MMC_IRQ_ENABLE_PORT);
  521. config2 = cb710_read_port_32(slot, CB710_MMC_CONFIGB_PORT);
  522. config1 = cb710_read_port_32(slot, CB710_MMC_CONFIG_PORT);
  523. dev_dbg(cb710_slot_dev(slot), "interrupt; status: %08X, "
  524. "ie: %08X, c2: %08X, c1: %08X\n",
  525. status, irqen, config2, config1);
  526. if (status & (CB710_MMC_S1_CARD_CHANGED << 8)) {
  527. /* ack the event */
  528. cb710_write_port_8(slot, CB710_MMC_STATUS1_PORT,
  529. CB710_MMC_S1_CARD_CHANGED);
  530. if ((irqen & CB710_MMC_IE_CISTATUS_MASK)
  531. == CB710_MMC_IE_CISTATUS_MASK)
  532. mmc_detect_change(mmc, HZ/5);
  533. } else {
  534. dev_dbg(cb710_slot_dev(slot), "unknown interrupt (test)\n");
  535. spin_lock(&reader->irq_lock);
  536. __cb710_mmc_enable_irq(slot, 0, CB710_MMC_IE_TEST_MASK);
  537. spin_unlock(&reader->irq_lock);
  538. }
  539. return 1;
  540. }
  541. static void cb710_mmc_finish_request_tasklet(unsigned long data)
  542. {
  543. struct mmc_host *mmc = (void *)data;
  544. struct cb710_mmc_reader *reader = mmc_priv(mmc);
  545. struct mmc_request *mrq = reader->mrq;
  546. reader->mrq = NULL;
  547. mmc_request_done(mmc, mrq);
  548. }
  549. static const struct mmc_host_ops cb710_mmc_host = {
  550. .request = cb710_mmc_request,
  551. .set_ios = cb710_mmc_set_ios,
  552. .get_ro = cb710_mmc_get_ro,
  553. .get_cd = cb710_mmc_get_cd,
  554. };
  555. #ifdef CONFIG_PM
  556. static int cb710_mmc_suspend(struct platform_device *pdev, pm_message_t state)
  557. {
  558. struct cb710_slot *slot = cb710_pdev_to_slot(pdev);
  559. cb710_mmc_enable_irq(slot, 0, ~0);
  560. return 0;
  561. }
  562. static int cb710_mmc_resume(struct platform_device *pdev)
  563. {
  564. struct cb710_slot *slot = cb710_pdev_to_slot(pdev);
  565. cb710_mmc_enable_irq(slot, 0, ~0);
  566. return 0;
  567. }
  568. #endif /* CONFIG_PM */
  569. static int cb710_mmc_init(struct platform_device *pdev)
  570. {
  571. struct cb710_slot *slot = cb710_pdev_to_slot(pdev);
  572. struct cb710_chip *chip = cb710_slot_to_chip(slot);
  573. struct mmc_host *mmc;
  574. struct cb710_mmc_reader *reader;
  575. int err;
  576. u32 val;
  577. mmc = mmc_alloc_host(sizeof(*reader), cb710_slot_dev(slot));
  578. if (!mmc)
  579. return -ENOMEM;
  580. platform_set_drvdata(pdev, mmc);
  581. /* harmless (maybe) magic */
  582. pci_read_config_dword(chip->pdev, 0x48, &val);
  583. val = cb710_src_freq_mhz[(val >> 16) & 0xF];
  584. dev_dbg(cb710_slot_dev(slot), "source frequency: %dMHz\n", val);
  585. val *= 1000000;
  586. mmc->ops = &cb710_mmc_host;
  587. mmc->f_max = val;
  588. mmc->f_min = val >> cb710_clock_divider_log2[CB710_MAX_DIVIDER_IDX];
  589. mmc->ocr_avail = MMC_VDD_32_33|MMC_VDD_33_34;
  590. mmc->caps = MMC_CAP_4_BIT_DATA;
  591. reader = mmc_priv(mmc);
  592. tasklet_init(&reader->finish_req_tasklet,
  593. cb710_mmc_finish_request_tasklet, (unsigned long)mmc);
  594. spin_lock_init(&reader->irq_lock);
  595. cb710_dump_regs(chip, CB710_DUMP_REGS_MMC);
  596. cb710_mmc_enable_irq(slot, 0, ~0);
  597. cb710_set_irq_handler(slot, cb710_mmc_irq_handler);
  598. err = mmc_add_host(mmc);
  599. if (unlikely(err))
  600. goto err_free_mmc;
  601. dev_dbg(cb710_slot_dev(slot), "mmc_hostname is %s\n",
  602. mmc_hostname(mmc));
  603. cb710_mmc_enable_irq(slot, CB710_MMC_IE_CARD_INSERTION_STATUS, 0);
  604. return 0;
  605. err_free_mmc:
  606. dev_dbg(cb710_slot_dev(slot), "mmc_add_host() failed: %d\n", err);
  607. cb710_set_irq_handler(slot, NULL);
  608. mmc_free_host(mmc);
  609. return err;
  610. }
  611. static int cb710_mmc_exit(struct platform_device *pdev)
  612. {
  613. struct cb710_slot *slot = cb710_pdev_to_slot(pdev);
  614. struct mmc_host *mmc = cb710_slot_to_mmc(slot);
  615. struct cb710_mmc_reader *reader = mmc_priv(mmc);
  616. cb710_mmc_enable_irq(slot, 0, CB710_MMC_IE_CARD_INSERTION_STATUS);
  617. mmc_remove_host(mmc);
  618. /* IRQs should be disabled now, but let's stay on the safe side */
  619. cb710_mmc_enable_irq(slot, 0, ~0);
  620. cb710_set_irq_handler(slot, NULL);
  621. /* clear config ports - just in case */
  622. cb710_write_port_32(slot, CB710_MMC_CONFIG_PORT, 0);
  623. cb710_write_port_16(slot, CB710_MMC_CONFIGB_PORT, 0);
  624. tasklet_kill(&reader->finish_req_tasklet);
  625. mmc_free_host(mmc);
  626. return 0;
  627. }
  628. static struct platform_driver cb710_mmc_driver = {
  629. .driver.name = "cb710-mmc",
  630. .probe = cb710_mmc_init,
  631. .remove = cb710_mmc_exit,
  632. #ifdef CONFIG_PM
  633. .suspend = cb710_mmc_suspend,
  634. .resume = cb710_mmc_resume,
  635. #endif
  636. };
  637. module_platform_driver(cb710_mmc_driver);
  638. MODULE_AUTHOR("Michał Mirosław <mirq-linux@rere.qmqm.pl>");
  639. MODULE_DESCRIPTION("ENE CB710 memory card reader driver - MMC/SD part");
  640. MODULE_LICENSE("GPL");
  641. MODULE_ALIAS("platform:cb710-mmc");