bfin_sdh.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635
  1. /*
  2. * bfin_sdh.c - Analog Devices Blackfin SDH Controller
  3. *
  4. * Copyright (C) 2007-2009 Analog Device Inc.
  5. *
  6. * Licensed under the GPL-2 or later.
  7. */
  8. #define DRIVER_NAME "bfin-sdh"
  9. #include <linux/module.h>
  10. #include <linux/init.h>
  11. #include <linux/ioport.h>
  12. #include <linux/platform_device.h>
  13. #include <linux/delay.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/dma-mapping.h>
  16. #include <linux/mmc/host.h>
  17. #include <linux/proc_fs.h>
  18. #include <linux/gfp.h>
  19. #include <asm/cacheflush.h>
  20. #include <asm/dma.h>
  21. #include <asm/portmux.h>
  22. #include <asm/bfin_sdh.h>
  23. #if defined(CONFIG_BF51x)
  24. #define bfin_read_SDH_PWR_CTL bfin_read_RSI_PWR_CTL
  25. #define bfin_write_SDH_PWR_CTL bfin_write_RSI_PWR_CTL
  26. #define bfin_read_SDH_CLK_CTL bfin_read_RSI_CLK_CTL
  27. #define bfin_write_SDH_CLK_CTL bfin_write_RSI_CLK_CTL
  28. #define bfin_write_SDH_ARGUMENT bfin_write_RSI_ARGUMENT
  29. #define bfin_write_SDH_COMMAND bfin_write_RSI_COMMAND
  30. #define bfin_write_SDH_DATA_TIMER bfin_write_RSI_DATA_TIMER
  31. #define bfin_read_SDH_RESPONSE0 bfin_read_RSI_RESPONSE0
  32. #define bfin_read_SDH_RESPONSE1 bfin_read_RSI_RESPONSE1
  33. #define bfin_read_SDH_RESPONSE2 bfin_read_RSI_RESPONSE2
  34. #define bfin_read_SDH_RESPONSE3 bfin_read_RSI_RESPONSE3
  35. #define bfin_write_SDH_DATA_LGTH bfin_write_RSI_DATA_LGTH
  36. #define bfin_read_SDH_DATA_CTL bfin_read_RSI_DATA_CTL
  37. #define bfin_write_SDH_DATA_CTL bfin_write_RSI_DATA_CTL
  38. #define bfin_read_SDH_DATA_CNT bfin_read_RSI_DATA_CNT
  39. #define bfin_write_SDH_STATUS_CLR bfin_write_RSI_STATUS_CLR
  40. #define bfin_read_SDH_E_STATUS bfin_read_RSI_E_STATUS
  41. #define bfin_write_SDH_E_STATUS bfin_write_RSI_E_STATUS
  42. #define bfin_read_SDH_STATUS bfin_read_RSI_STATUS
  43. #define bfin_write_SDH_MASK0 bfin_write_RSI_MASK0
  44. #define bfin_read_SDH_CFG bfin_read_RSI_CFG
  45. #define bfin_write_SDH_CFG bfin_write_RSI_CFG
  46. #endif
  47. struct dma_desc_array {
  48. unsigned long start_addr;
  49. unsigned short cfg;
  50. unsigned short x_count;
  51. short x_modify;
  52. } __packed;
  53. struct sdh_host {
  54. struct mmc_host *mmc;
  55. spinlock_t lock;
  56. struct resource *res;
  57. void __iomem *base;
  58. int irq;
  59. int stat_irq;
  60. int dma_ch;
  61. int dma_dir;
  62. struct dma_desc_array *sg_cpu;
  63. dma_addr_t sg_dma;
  64. int dma_len;
  65. unsigned int imask;
  66. unsigned int power_mode;
  67. unsigned int clk_div;
  68. struct mmc_request *mrq;
  69. struct mmc_command *cmd;
  70. struct mmc_data *data;
  71. };
  72. static struct bfin_sd_host *get_sdh_data(struct platform_device *pdev)
  73. {
  74. return pdev->dev.platform_data;
  75. }
  76. static void sdh_stop_clock(struct sdh_host *host)
  77. {
  78. bfin_write_SDH_CLK_CTL(bfin_read_SDH_CLK_CTL() & ~CLK_E);
  79. SSYNC();
  80. }
  81. static void sdh_enable_stat_irq(struct sdh_host *host, unsigned int mask)
  82. {
  83. unsigned long flags;
  84. spin_lock_irqsave(&host->lock, flags);
  85. host->imask |= mask;
  86. bfin_write_SDH_MASK0(mask);
  87. SSYNC();
  88. spin_unlock_irqrestore(&host->lock, flags);
  89. }
  90. static void sdh_disable_stat_irq(struct sdh_host *host, unsigned int mask)
  91. {
  92. unsigned long flags;
  93. spin_lock_irqsave(&host->lock, flags);
  94. host->imask &= ~mask;
  95. bfin_write_SDH_MASK0(host->imask);
  96. SSYNC();
  97. spin_unlock_irqrestore(&host->lock, flags);
  98. }
  99. static int sdh_setup_data(struct sdh_host *host, struct mmc_data *data)
  100. {
  101. unsigned int length;
  102. unsigned int data_ctl;
  103. unsigned int dma_cfg;
  104. unsigned int cycle_ns, timeout;
  105. dev_dbg(mmc_dev(host->mmc), "%s enter flags: 0x%x\n", __func__, data->flags);
  106. host->data = data;
  107. data_ctl = 0;
  108. dma_cfg = 0;
  109. length = data->blksz * data->blocks;
  110. bfin_write_SDH_DATA_LGTH(length);
  111. if (data->flags & MMC_DATA_STREAM)
  112. data_ctl |= DTX_MODE;
  113. if (data->flags & MMC_DATA_READ)
  114. data_ctl |= DTX_DIR;
  115. /* Only supports power-of-2 block size */
  116. if (data->blksz & (data->blksz - 1))
  117. return -EINVAL;
  118. data_ctl |= ((ffs(data->blksz) - 1) << 4);
  119. bfin_write_SDH_DATA_CTL(data_ctl);
  120. /* the time of a host clock period in ns */
  121. cycle_ns = 1000000000 / (get_sclk() / (2 * (host->clk_div + 1)));
  122. timeout = data->timeout_ns / cycle_ns;
  123. timeout += data->timeout_clks;
  124. bfin_write_SDH_DATA_TIMER(timeout);
  125. SSYNC();
  126. if (data->flags & MMC_DATA_READ) {
  127. host->dma_dir = DMA_FROM_DEVICE;
  128. dma_cfg |= WNR;
  129. } else
  130. host->dma_dir = DMA_TO_DEVICE;
  131. sdh_enable_stat_irq(host, (DAT_CRC_FAIL | DAT_TIME_OUT | DAT_END));
  132. host->dma_len = dma_map_sg(mmc_dev(host->mmc), data->sg, data->sg_len, host->dma_dir);
  133. #if defined(CONFIG_BF54x)
  134. dma_cfg |= DMAFLOW_ARRAY | NDSIZE_5 | RESTART | WDSIZE_32 | DMAEN;
  135. {
  136. struct scatterlist *sg;
  137. int i;
  138. for_each_sg(data->sg, sg, host->dma_len, i) {
  139. host->sg_cpu[i].start_addr = sg_dma_address(sg);
  140. host->sg_cpu[i].cfg = dma_cfg;
  141. host->sg_cpu[i].x_count = sg_dma_len(sg) / 4;
  142. host->sg_cpu[i].x_modify = 4;
  143. dev_dbg(mmc_dev(host->mmc), "%d: start_addr:0x%lx, "
  144. "cfg:0x%x, x_count:0x%x, x_modify:0x%x\n",
  145. i, host->sg_cpu[i].start_addr,
  146. host->sg_cpu[i].cfg, host->sg_cpu[i].x_count,
  147. host->sg_cpu[i].x_modify);
  148. }
  149. }
  150. flush_dcache_range((unsigned int)host->sg_cpu,
  151. (unsigned int)host->sg_cpu +
  152. host->dma_len * sizeof(struct dma_desc_array));
  153. /* Set the last descriptor to stop mode */
  154. host->sg_cpu[host->dma_len - 1].cfg &= ~(DMAFLOW | NDSIZE);
  155. host->sg_cpu[host->dma_len - 1].cfg |= DI_EN;
  156. set_dma_curr_desc_addr(host->dma_ch, (unsigned long *)host->sg_dma);
  157. set_dma_x_count(host->dma_ch, 0);
  158. set_dma_x_modify(host->dma_ch, 0);
  159. set_dma_config(host->dma_ch, dma_cfg);
  160. #elif defined(CONFIG_BF51x)
  161. /* RSI DMA doesn't work in array mode */
  162. dma_cfg |= WDSIZE_32 | DMAEN;
  163. set_dma_start_addr(host->dma_ch, sg_dma_address(&data->sg[0]));
  164. set_dma_x_count(host->dma_ch, length / 4);
  165. set_dma_x_modify(host->dma_ch, 4);
  166. set_dma_config(host->dma_ch, dma_cfg);
  167. #endif
  168. bfin_write_SDH_DATA_CTL(bfin_read_SDH_DATA_CTL() | DTX_DMA_E | DTX_E);
  169. SSYNC();
  170. dev_dbg(mmc_dev(host->mmc), "%s exit\n", __func__);
  171. return 0;
  172. }
  173. static void sdh_start_cmd(struct sdh_host *host, struct mmc_command *cmd)
  174. {
  175. unsigned int sdh_cmd;
  176. unsigned int stat_mask;
  177. dev_dbg(mmc_dev(host->mmc), "%s enter cmd: 0x%p\n", __func__, cmd);
  178. WARN_ON(host->cmd != NULL);
  179. host->cmd = cmd;
  180. sdh_cmd = 0;
  181. stat_mask = 0;
  182. sdh_cmd |= cmd->opcode;
  183. if (cmd->flags & MMC_RSP_PRESENT) {
  184. sdh_cmd |= CMD_RSP;
  185. stat_mask |= CMD_RESP_END;
  186. } else {
  187. stat_mask |= CMD_SENT;
  188. }
  189. if (cmd->flags & MMC_RSP_136)
  190. sdh_cmd |= CMD_L_RSP;
  191. stat_mask |= CMD_CRC_FAIL | CMD_TIME_OUT;
  192. sdh_enable_stat_irq(host, stat_mask);
  193. bfin_write_SDH_ARGUMENT(cmd->arg);
  194. bfin_write_SDH_COMMAND(sdh_cmd | CMD_E);
  195. bfin_write_SDH_CLK_CTL(bfin_read_SDH_CLK_CTL() | CLK_E);
  196. SSYNC();
  197. }
  198. static void sdh_finish_request(struct sdh_host *host, struct mmc_request *mrq)
  199. {
  200. dev_dbg(mmc_dev(host->mmc), "%s enter\n", __func__);
  201. host->mrq = NULL;
  202. host->cmd = NULL;
  203. host->data = NULL;
  204. mmc_request_done(host->mmc, mrq);
  205. }
  206. static int sdh_cmd_done(struct sdh_host *host, unsigned int stat)
  207. {
  208. struct mmc_command *cmd = host->cmd;
  209. int ret = 0;
  210. dev_dbg(mmc_dev(host->mmc), "%s enter cmd: %p\n", __func__, cmd);
  211. if (!cmd)
  212. return 0;
  213. host->cmd = NULL;
  214. if (cmd->flags & MMC_RSP_PRESENT) {
  215. cmd->resp[0] = bfin_read_SDH_RESPONSE0();
  216. if (cmd->flags & MMC_RSP_136) {
  217. cmd->resp[1] = bfin_read_SDH_RESPONSE1();
  218. cmd->resp[2] = bfin_read_SDH_RESPONSE2();
  219. cmd->resp[3] = bfin_read_SDH_RESPONSE3();
  220. }
  221. }
  222. if (stat & CMD_TIME_OUT)
  223. cmd->error = -ETIMEDOUT;
  224. else if (stat & CMD_CRC_FAIL && cmd->flags & MMC_RSP_CRC)
  225. cmd->error = -EILSEQ;
  226. sdh_disable_stat_irq(host, (CMD_SENT | CMD_RESP_END | CMD_TIME_OUT | CMD_CRC_FAIL));
  227. if (host->data && !cmd->error) {
  228. if (host->data->flags & MMC_DATA_WRITE) {
  229. ret = sdh_setup_data(host, host->data);
  230. if (ret)
  231. return 0;
  232. }
  233. sdh_enable_stat_irq(host, DAT_END | RX_OVERRUN | TX_UNDERRUN | DAT_TIME_OUT);
  234. } else
  235. sdh_finish_request(host, host->mrq);
  236. return 1;
  237. }
  238. static int sdh_data_done(struct sdh_host *host, unsigned int stat)
  239. {
  240. struct mmc_data *data = host->data;
  241. dev_dbg(mmc_dev(host->mmc), "%s enter stat: 0x%x\n", __func__, stat);
  242. if (!data)
  243. return 0;
  244. disable_dma(host->dma_ch);
  245. dma_unmap_sg(mmc_dev(host->mmc), data->sg, data->sg_len,
  246. host->dma_dir);
  247. if (stat & DAT_TIME_OUT)
  248. data->error = -ETIMEDOUT;
  249. else if (stat & DAT_CRC_FAIL)
  250. data->error = -EILSEQ;
  251. else if (stat & (RX_OVERRUN | TX_UNDERRUN))
  252. data->error = -EIO;
  253. if (!data->error)
  254. data->bytes_xfered = data->blocks * data->blksz;
  255. else
  256. data->bytes_xfered = 0;
  257. sdh_disable_stat_irq(host, DAT_END | DAT_TIME_OUT | DAT_CRC_FAIL | RX_OVERRUN | TX_UNDERRUN);
  258. bfin_write_SDH_STATUS_CLR(DAT_END_STAT | DAT_TIMEOUT_STAT | \
  259. DAT_CRC_FAIL_STAT | DAT_BLK_END_STAT | RX_OVERRUN | TX_UNDERRUN);
  260. bfin_write_SDH_DATA_CTL(0);
  261. SSYNC();
  262. host->data = NULL;
  263. if (host->mrq->stop) {
  264. sdh_stop_clock(host);
  265. sdh_start_cmd(host, host->mrq->stop);
  266. } else {
  267. sdh_finish_request(host, host->mrq);
  268. }
  269. return 1;
  270. }
  271. static void sdh_request(struct mmc_host *mmc, struct mmc_request *mrq)
  272. {
  273. struct sdh_host *host = mmc_priv(mmc);
  274. int ret = 0;
  275. dev_dbg(mmc_dev(host->mmc), "%s enter, mrp:%p, cmd:%p\n", __func__, mrq, mrq->cmd);
  276. WARN_ON(host->mrq != NULL);
  277. host->mrq = mrq;
  278. host->data = mrq->data;
  279. if (mrq->data && mrq->data->flags & MMC_DATA_READ) {
  280. ret = sdh_setup_data(host, mrq->data);
  281. if (ret)
  282. return;
  283. }
  284. sdh_start_cmd(host, mrq->cmd);
  285. }
  286. static void sdh_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
  287. {
  288. struct sdh_host *host;
  289. unsigned long flags;
  290. u16 clk_ctl = 0;
  291. u16 pwr_ctl = 0;
  292. u16 cfg;
  293. host = mmc_priv(mmc);
  294. spin_lock_irqsave(&host->lock, flags);
  295. if (ios->clock) {
  296. unsigned long sys_clk, ios_clk;
  297. unsigned char clk_div;
  298. ios_clk = 2 * ios->clock;
  299. sys_clk = get_sclk();
  300. clk_div = sys_clk / ios_clk;
  301. if (sys_clk % ios_clk == 0)
  302. clk_div -= 1;
  303. clk_div = min_t(unsigned char, clk_div, 0xFF);
  304. clk_ctl |= clk_div;
  305. clk_ctl |= CLK_E;
  306. host->clk_div = clk_div;
  307. } else
  308. sdh_stop_clock(host);
  309. if (ios->bus_mode == MMC_BUSMODE_OPENDRAIN)
  310. #ifdef CONFIG_SDH_BFIN_MISSING_CMD_PULLUP_WORKAROUND
  311. pwr_ctl |= ROD_CTL;
  312. #else
  313. pwr_ctl |= SD_CMD_OD | ROD_CTL;
  314. #endif
  315. if (ios->bus_width == MMC_BUS_WIDTH_4) {
  316. cfg = bfin_read_SDH_CFG();
  317. cfg &= ~PD_SDDAT3;
  318. cfg |= PUP_SDDAT3;
  319. /* Enable 4 bit SDIO */
  320. cfg |= (SD4E | MWE);
  321. bfin_write_SDH_CFG(cfg);
  322. clk_ctl |= WIDE_BUS;
  323. } else {
  324. cfg = bfin_read_SDH_CFG();
  325. cfg |= MWE;
  326. bfin_write_SDH_CFG(cfg);
  327. }
  328. bfin_write_SDH_CLK_CTL(clk_ctl);
  329. host->power_mode = ios->power_mode;
  330. if (ios->power_mode == MMC_POWER_ON)
  331. pwr_ctl |= PWR_ON;
  332. bfin_write_SDH_PWR_CTL(pwr_ctl);
  333. SSYNC();
  334. spin_unlock_irqrestore(&host->lock, flags);
  335. dev_dbg(mmc_dev(host->mmc), "SDH: clk_div = 0x%x actual clock:%ld expected clock:%d\n",
  336. host->clk_div,
  337. host->clk_div ? get_sclk() / (2 * (host->clk_div + 1)) : 0,
  338. ios->clock);
  339. }
  340. static const struct mmc_host_ops sdh_ops = {
  341. .request = sdh_request,
  342. .set_ios = sdh_set_ios,
  343. };
  344. static irqreturn_t sdh_dma_irq(int irq, void *devid)
  345. {
  346. struct sdh_host *host = devid;
  347. dev_dbg(mmc_dev(host->mmc), "%s enter, irq_stat: 0x%04x\n", __func__,
  348. get_dma_curr_irqstat(host->dma_ch));
  349. clear_dma_irqstat(host->dma_ch);
  350. SSYNC();
  351. return IRQ_HANDLED;
  352. }
  353. static irqreturn_t sdh_stat_irq(int irq, void *devid)
  354. {
  355. struct sdh_host *host = devid;
  356. unsigned int status;
  357. int handled = 0;
  358. dev_dbg(mmc_dev(host->mmc), "%s enter\n", __func__);
  359. status = bfin_read_SDH_E_STATUS();
  360. if (status & SD_CARD_DET) {
  361. mmc_detect_change(host->mmc, 0);
  362. bfin_write_SDH_E_STATUS(SD_CARD_DET);
  363. }
  364. status = bfin_read_SDH_STATUS();
  365. if (status & (CMD_SENT | CMD_RESP_END | CMD_TIME_OUT | CMD_CRC_FAIL)) {
  366. handled |= sdh_cmd_done(host, status);
  367. bfin_write_SDH_STATUS_CLR(CMD_SENT_STAT | CMD_RESP_END_STAT | \
  368. CMD_TIMEOUT_STAT | CMD_CRC_FAIL_STAT);
  369. SSYNC();
  370. }
  371. status = bfin_read_SDH_STATUS();
  372. if (status & (DAT_END | DAT_TIME_OUT | DAT_CRC_FAIL | RX_OVERRUN | TX_UNDERRUN))
  373. handled |= sdh_data_done(host, status);
  374. dev_dbg(mmc_dev(host->mmc), "%s exit\n\n", __func__);
  375. return IRQ_RETVAL(handled);
  376. }
  377. static int __devinit sdh_probe(struct platform_device *pdev)
  378. {
  379. struct mmc_host *mmc;
  380. struct sdh_host *host;
  381. struct bfin_sd_host *drv_data = get_sdh_data(pdev);
  382. int ret;
  383. if (!drv_data) {
  384. dev_err(&pdev->dev, "missing platform driver data\n");
  385. ret = -EINVAL;
  386. goto out;
  387. }
  388. mmc = mmc_alloc_host(sizeof(struct sdh_host), &pdev->dev);
  389. if (!mmc) {
  390. ret = -ENOMEM;
  391. goto out;
  392. }
  393. mmc->ops = &sdh_ops;
  394. mmc->max_segs = 32;
  395. mmc->max_seg_size = 1 << 16;
  396. mmc->max_blk_size = 1 << 11;
  397. mmc->max_blk_count = 1 << 11;
  398. mmc->max_req_size = PAGE_SIZE;
  399. mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34;
  400. mmc->f_max = get_sclk();
  401. mmc->f_min = mmc->f_max >> 9;
  402. mmc->caps = MMC_CAP_4_BIT_DATA | MMC_CAP_NEEDS_POLL;
  403. host = mmc_priv(mmc);
  404. host->mmc = mmc;
  405. spin_lock_init(&host->lock);
  406. host->irq = drv_data->irq_int0;
  407. host->dma_ch = drv_data->dma_chan;
  408. ret = request_dma(host->dma_ch, DRIVER_NAME "DMA");
  409. if (ret) {
  410. dev_err(&pdev->dev, "unable to request DMA channel\n");
  411. goto out1;
  412. }
  413. ret = set_dma_callback(host->dma_ch, sdh_dma_irq, host);
  414. if (ret) {
  415. dev_err(&pdev->dev, "unable to request DMA irq\n");
  416. goto out2;
  417. }
  418. host->sg_cpu = dma_alloc_coherent(&pdev->dev, PAGE_SIZE, &host->sg_dma, GFP_KERNEL);
  419. if (host->sg_cpu == NULL) {
  420. ret = -ENOMEM;
  421. goto out2;
  422. }
  423. platform_set_drvdata(pdev, mmc);
  424. mmc_add_host(mmc);
  425. ret = request_irq(host->irq, sdh_stat_irq, 0, "SDH Status IRQ", host);
  426. if (ret) {
  427. dev_err(&pdev->dev, "unable to request status irq\n");
  428. goto out3;
  429. }
  430. ret = peripheral_request_list(drv_data->pin_req, DRIVER_NAME);
  431. if (ret) {
  432. dev_err(&pdev->dev, "unable to request peripheral pins\n");
  433. goto out4;
  434. }
  435. #if defined(CONFIG_BF54x)
  436. /* Secure Digital Host shares DMA with Nand controller */
  437. bfin_write_DMAC1_PERIMUX(bfin_read_DMAC1_PERIMUX() | 0x1);
  438. #endif
  439. bfin_write_SDH_CFG(bfin_read_SDH_CFG() | CLKS_EN);
  440. SSYNC();
  441. /* Disable card inserting detection pin. set MMC_CAP_NEES_POLL, and
  442. * mmc stack will do the detection.
  443. */
  444. bfin_write_SDH_CFG((bfin_read_SDH_CFG() & 0x1F) | (PUP_SDDAT | PUP_SDDAT3));
  445. SSYNC();
  446. return 0;
  447. out4:
  448. free_irq(host->irq, host);
  449. out3:
  450. mmc_remove_host(mmc);
  451. dma_free_coherent(&pdev->dev, PAGE_SIZE, host->sg_cpu, host->sg_dma);
  452. out2:
  453. free_dma(host->dma_ch);
  454. out1:
  455. mmc_free_host(mmc);
  456. out:
  457. return ret;
  458. }
  459. static int __devexit sdh_remove(struct platform_device *pdev)
  460. {
  461. struct mmc_host *mmc = platform_get_drvdata(pdev);
  462. platform_set_drvdata(pdev, NULL);
  463. if (mmc) {
  464. struct sdh_host *host = mmc_priv(mmc);
  465. mmc_remove_host(mmc);
  466. sdh_stop_clock(host);
  467. free_irq(host->irq, host);
  468. free_dma(host->dma_ch);
  469. dma_free_coherent(&pdev->dev, PAGE_SIZE, host->sg_cpu, host->sg_dma);
  470. mmc_free_host(mmc);
  471. }
  472. return 0;
  473. }
  474. #ifdef CONFIG_PM
  475. static int sdh_suspend(struct platform_device *dev, pm_message_t state)
  476. {
  477. struct mmc_host *mmc = platform_get_drvdata(dev);
  478. struct bfin_sd_host *drv_data = get_sdh_data(dev);
  479. int ret = 0;
  480. if (mmc)
  481. ret = mmc_suspend_host(mmc);
  482. bfin_write_SDH_PWR_CTL(bfin_read_SDH_PWR_CTL() & ~PWR_ON);
  483. peripheral_free_list(drv_data->pin_req);
  484. return ret;
  485. }
  486. static int sdh_resume(struct platform_device *dev)
  487. {
  488. struct mmc_host *mmc = platform_get_drvdata(dev);
  489. struct bfin_sd_host *drv_data = get_sdh_data(dev);
  490. int ret = 0;
  491. ret = peripheral_request_list(drv_data->pin_req, DRIVER_NAME);
  492. if (ret) {
  493. dev_err(&dev->dev, "unable to request peripheral pins\n");
  494. return ret;
  495. }
  496. bfin_write_SDH_PWR_CTL(bfin_read_SDH_PWR_CTL() | PWR_ON);
  497. #if defined(CONFIG_BF54x)
  498. /* Secure Digital Host shares DMA with Nand controller */
  499. bfin_write_DMAC1_PERIMUX(bfin_read_DMAC1_PERIMUX() | 0x1);
  500. #endif
  501. bfin_write_SDH_CFG(bfin_read_SDH_CFG() | CLKS_EN);
  502. SSYNC();
  503. bfin_write_SDH_CFG((bfin_read_SDH_CFG() & 0x1F) | (PUP_SDDAT | PUP_SDDAT3));
  504. SSYNC();
  505. if (mmc)
  506. ret = mmc_resume_host(mmc);
  507. return ret;
  508. }
  509. #else
  510. # define sdh_suspend NULL
  511. # define sdh_resume NULL
  512. #endif
  513. static struct platform_driver sdh_driver = {
  514. .probe = sdh_probe,
  515. .remove = __devexit_p(sdh_remove),
  516. .suspend = sdh_suspend,
  517. .resume = sdh_resume,
  518. .driver = {
  519. .name = DRIVER_NAME,
  520. },
  521. };
  522. module_platform_driver(sdh_driver);
  523. MODULE_DESCRIPTION("Blackfin Secure Digital Host Driver");
  524. MODULE_AUTHOR("Cliff Cai, Roy Huang");
  525. MODULE_LICENSE("GPL");