bfin_sdh.c 16 KB

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