bcm2835.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473
  1. /*
  2. * bcm2835 sdhost driver.
  3. *
  4. * The 2835 has two SD controllers: The Arasan sdhci controller
  5. * (supported by the iproc driver) and a custom sdhost controller
  6. * (supported by this driver).
  7. *
  8. * The sdhci controller supports both sdcard and sdio. The sdhost
  9. * controller supports the sdcard only, but has better performance.
  10. * Also note that the rpi3 has sdio wifi, so driving the sdcard with
  11. * the sdhost controller allows to use the sdhci controller for wifi
  12. * support.
  13. *
  14. * The configuration is done by devicetree via pin muxing. Both
  15. * SD controller are available on the same pins (2 pin groups = pin 22
  16. * to 27 + pin 48 to 53). So it's possible to use both SD controllers
  17. * at the same time with different pin groups.
  18. *
  19. * Author: Phil Elwell <phil@raspberrypi.org>
  20. * Copyright (C) 2015-2016 Raspberry Pi (Trading) Ltd.
  21. *
  22. * Based on
  23. * mmc-bcm2835.c by Gellert Weisz
  24. * which is, in turn, based on
  25. * sdhci-bcm2708.c by Broadcom
  26. * sdhci-bcm2835.c by Stephen Warren and Oleksandr Tymoshenko
  27. * sdhci.c and sdhci-pci.c by Pierre Ossman
  28. *
  29. * This program is free software; you can redistribute it and/or modify it
  30. * under the terms and conditions of the GNU General Public License,
  31. * version 2, as published by the Free Software Foundation.
  32. *
  33. * This program is distributed in the hope it will be useful, but WITHOUT
  34. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  35. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  36. * more details.
  37. *
  38. * You should have received a copy of the GNU General Public License
  39. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  40. */
  41. #include <linux/clk.h>
  42. #include <linux/delay.h>
  43. #include <linux/device.h>
  44. #include <linux/dmaengine.h>
  45. #include <linux/dma-mapping.h>
  46. #include <linux/err.h>
  47. #include <linux/highmem.h>
  48. #include <linux/interrupt.h>
  49. #include <linux/io.h>
  50. #include <linux/iopoll.h>
  51. #include <linux/module.h>
  52. #include <linux/of_address.h>
  53. #include <linux/of_irq.h>
  54. #include <linux/platform_device.h>
  55. #include <linux/scatterlist.h>
  56. #include <linux/time.h>
  57. #include <linux/workqueue.h>
  58. #include <linux/mmc/host.h>
  59. #include <linux/mmc/mmc.h>
  60. #include <linux/mmc/sd.h>
  61. #define SDCMD 0x00 /* Command to SD card - 16 R/W */
  62. #define SDARG 0x04 /* Argument to SD card - 32 R/W */
  63. #define SDTOUT 0x08 /* Start value for timeout counter - 32 R/W */
  64. #define SDCDIV 0x0c /* Start value for clock divider - 11 R/W */
  65. #define SDRSP0 0x10 /* SD card response (31:0) - 32 R */
  66. #define SDRSP1 0x14 /* SD card response (63:32) - 32 R */
  67. #define SDRSP2 0x18 /* SD card response (95:64) - 32 R */
  68. #define SDRSP3 0x1c /* SD card response (127:96) - 32 R */
  69. #define SDHSTS 0x20 /* SD host status - 11 R/W */
  70. #define SDVDD 0x30 /* SD card power control - 1 R/W */
  71. #define SDEDM 0x34 /* Emergency Debug Mode - 13 R/W */
  72. #define SDHCFG 0x38 /* Host configuration - 2 R/W */
  73. #define SDHBCT 0x3c /* Host byte count (debug) - 32 R/W */
  74. #define SDDATA 0x40 /* Data to/from SD card - 32 R/W */
  75. #define SDHBLC 0x50 /* Host block count (SDIO/SDHC) - 9 R/W */
  76. #define SDCMD_NEW_FLAG 0x8000
  77. #define SDCMD_FAIL_FLAG 0x4000
  78. #define SDCMD_BUSYWAIT 0x800
  79. #define SDCMD_NO_RESPONSE 0x400
  80. #define SDCMD_LONG_RESPONSE 0x200
  81. #define SDCMD_WRITE_CMD 0x80
  82. #define SDCMD_READ_CMD 0x40
  83. #define SDCMD_CMD_MASK 0x3f
  84. #define SDCDIV_MAX_CDIV 0x7ff
  85. #define SDHSTS_BUSY_IRPT 0x400
  86. #define SDHSTS_BLOCK_IRPT 0x200
  87. #define SDHSTS_SDIO_IRPT 0x100
  88. #define SDHSTS_REW_TIME_OUT 0x80
  89. #define SDHSTS_CMD_TIME_OUT 0x40
  90. #define SDHSTS_CRC16_ERROR 0x20
  91. #define SDHSTS_CRC7_ERROR 0x10
  92. #define SDHSTS_FIFO_ERROR 0x08
  93. /* Reserved */
  94. /* Reserved */
  95. #define SDHSTS_DATA_FLAG 0x01
  96. #define SDHSTS_TRANSFER_ERROR_MASK (SDHSTS_CRC7_ERROR | \
  97. SDHSTS_CRC16_ERROR | \
  98. SDHSTS_REW_TIME_OUT | \
  99. SDHSTS_FIFO_ERROR)
  100. #define SDHSTS_ERROR_MASK (SDHSTS_CMD_TIME_OUT | \
  101. SDHSTS_TRANSFER_ERROR_MASK)
  102. #define SDHCFG_BUSY_IRPT_EN BIT(10)
  103. #define SDHCFG_BLOCK_IRPT_EN BIT(8)
  104. #define SDHCFG_SDIO_IRPT_EN BIT(5)
  105. #define SDHCFG_DATA_IRPT_EN BIT(4)
  106. #define SDHCFG_SLOW_CARD BIT(3)
  107. #define SDHCFG_WIDE_EXT_BUS BIT(2)
  108. #define SDHCFG_WIDE_INT_BUS BIT(1)
  109. #define SDHCFG_REL_CMD_LINE BIT(0)
  110. #define SDVDD_POWER_OFF 0
  111. #define SDVDD_POWER_ON 1
  112. #define SDEDM_FORCE_DATA_MODE BIT(19)
  113. #define SDEDM_CLOCK_PULSE BIT(20)
  114. #define SDEDM_BYPASS BIT(21)
  115. #define SDEDM_WRITE_THRESHOLD_SHIFT 9
  116. #define SDEDM_READ_THRESHOLD_SHIFT 14
  117. #define SDEDM_THRESHOLD_MASK 0x1f
  118. #define SDEDM_FSM_MASK 0xf
  119. #define SDEDM_FSM_IDENTMODE 0x0
  120. #define SDEDM_FSM_DATAMODE 0x1
  121. #define SDEDM_FSM_READDATA 0x2
  122. #define SDEDM_FSM_WRITEDATA 0x3
  123. #define SDEDM_FSM_READWAIT 0x4
  124. #define SDEDM_FSM_READCRC 0x5
  125. #define SDEDM_FSM_WRITECRC 0x6
  126. #define SDEDM_FSM_WRITEWAIT1 0x7
  127. #define SDEDM_FSM_POWERDOWN 0x8
  128. #define SDEDM_FSM_POWERUP 0x9
  129. #define SDEDM_FSM_WRITESTART1 0xa
  130. #define SDEDM_FSM_WRITESTART2 0xb
  131. #define SDEDM_FSM_GENPULSES 0xc
  132. #define SDEDM_FSM_WRITEWAIT2 0xd
  133. #define SDEDM_FSM_STARTPOWDOWN 0xf
  134. #define SDDATA_FIFO_WORDS 16
  135. #define FIFO_READ_THRESHOLD 4
  136. #define FIFO_WRITE_THRESHOLD 4
  137. #define SDDATA_FIFO_PIO_BURST 8
  138. #define PIO_THRESHOLD 1 /* Maximum block count for PIO (0 = always DMA) */
  139. struct bcm2835_host {
  140. spinlock_t lock;
  141. struct mutex mutex;
  142. void __iomem *ioaddr;
  143. u32 phys_addr;
  144. struct mmc_host *mmc;
  145. struct platform_device *pdev;
  146. int clock; /* Current clock speed */
  147. unsigned int max_clk; /* Max possible freq */
  148. struct work_struct dma_work;
  149. struct delayed_work timeout_work; /* Timer for timeouts */
  150. struct sg_mapping_iter sg_miter; /* SG state for PIO */
  151. unsigned int blocks; /* remaining PIO blocks */
  152. int irq; /* Device IRQ */
  153. u32 ns_per_fifo_word;
  154. /* cached registers */
  155. u32 hcfg;
  156. u32 cdiv;
  157. struct mmc_request *mrq; /* Current request */
  158. struct mmc_command *cmd; /* Current command */
  159. struct mmc_data *data; /* Current data request */
  160. bool data_complete:1;/* Data finished before cmd */
  161. bool use_busy:1; /* Wait for busy interrupt */
  162. bool use_sbc:1; /* Send CMD23 */
  163. /* for threaded irq handler */
  164. bool irq_block;
  165. bool irq_busy;
  166. bool irq_data;
  167. /* DMA part */
  168. struct dma_chan *dma_chan_rxtx;
  169. struct dma_chan *dma_chan;
  170. struct dma_slave_config dma_cfg_rx;
  171. struct dma_slave_config dma_cfg_tx;
  172. struct dma_async_tx_descriptor *dma_desc;
  173. u32 dma_dir;
  174. u32 drain_words;
  175. struct page *drain_page;
  176. u32 drain_offset;
  177. bool use_dma;
  178. };
  179. static void bcm2835_dumpcmd(struct bcm2835_host *host, struct mmc_command *cmd,
  180. const char *label)
  181. {
  182. struct device *dev = &host->pdev->dev;
  183. if (!cmd)
  184. return;
  185. dev_dbg(dev, "%c%s op %d arg 0x%x flags 0x%x - resp %08x %08x %08x %08x, err %d\n",
  186. (cmd == host->cmd) ? '>' : ' ',
  187. label, cmd->opcode, cmd->arg, cmd->flags,
  188. cmd->resp[0], cmd->resp[1], cmd->resp[2], cmd->resp[3],
  189. cmd->error);
  190. }
  191. static void bcm2835_dumpregs(struct bcm2835_host *host)
  192. {
  193. struct mmc_request *mrq = host->mrq;
  194. struct device *dev = &host->pdev->dev;
  195. if (mrq) {
  196. bcm2835_dumpcmd(host, mrq->sbc, "sbc");
  197. bcm2835_dumpcmd(host, mrq->cmd, "cmd");
  198. if (mrq->data) {
  199. dev_dbg(dev, "data blocks %x blksz %x - err %d\n",
  200. mrq->data->blocks,
  201. mrq->data->blksz,
  202. mrq->data->error);
  203. }
  204. bcm2835_dumpcmd(host, mrq->stop, "stop");
  205. }
  206. dev_dbg(dev, "=========== REGISTER DUMP ===========\n");
  207. dev_dbg(dev, "SDCMD 0x%08x\n", readl(host->ioaddr + SDCMD));
  208. dev_dbg(dev, "SDARG 0x%08x\n", readl(host->ioaddr + SDARG));
  209. dev_dbg(dev, "SDTOUT 0x%08x\n", readl(host->ioaddr + SDTOUT));
  210. dev_dbg(dev, "SDCDIV 0x%08x\n", readl(host->ioaddr + SDCDIV));
  211. dev_dbg(dev, "SDRSP0 0x%08x\n", readl(host->ioaddr + SDRSP0));
  212. dev_dbg(dev, "SDRSP1 0x%08x\n", readl(host->ioaddr + SDRSP1));
  213. dev_dbg(dev, "SDRSP2 0x%08x\n", readl(host->ioaddr + SDRSP2));
  214. dev_dbg(dev, "SDRSP3 0x%08x\n", readl(host->ioaddr + SDRSP3));
  215. dev_dbg(dev, "SDHSTS 0x%08x\n", readl(host->ioaddr + SDHSTS));
  216. dev_dbg(dev, "SDVDD 0x%08x\n", readl(host->ioaddr + SDVDD));
  217. dev_dbg(dev, "SDEDM 0x%08x\n", readl(host->ioaddr + SDEDM));
  218. dev_dbg(dev, "SDHCFG 0x%08x\n", readl(host->ioaddr + SDHCFG));
  219. dev_dbg(dev, "SDHBCT 0x%08x\n", readl(host->ioaddr + SDHBCT));
  220. dev_dbg(dev, "SDHBLC 0x%08x\n", readl(host->ioaddr + SDHBLC));
  221. dev_dbg(dev, "===========================================\n");
  222. }
  223. static void bcm2835_reset_internal(struct bcm2835_host *host)
  224. {
  225. u32 temp;
  226. writel(SDVDD_POWER_OFF, host->ioaddr + SDVDD);
  227. writel(0, host->ioaddr + SDCMD);
  228. writel(0, host->ioaddr + SDARG);
  229. writel(0xf00000, host->ioaddr + SDTOUT);
  230. writel(0, host->ioaddr + SDCDIV);
  231. writel(0x7f8, host->ioaddr + SDHSTS); /* Write 1s to clear */
  232. writel(0, host->ioaddr + SDHCFG);
  233. writel(0, host->ioaddr + SDHBCT);
  234. writel(0, host->ioaddr + SDHBLC);
  235. /* Limit fifo usage due to silicon bug */
  236. temp = readl(host->ioaddr + SDEDM);
  237. temp &= ~((SDEDM_THRESHOLD_MASK << SDEDM_READ_THRESHOLD_SHIFT) |
  238. (SDEDM_THRESHOLD_MASK << SDEDM_WRITE_THRESHOLD_SHIFT));
  239. temp |= (FIFO_READ_THRESHOLD << SDEDM_READ_THRESHOLD_SHIFT) |
  240. (FIFO_WRITE_THRESHOLD << SDEDM_WRITE_THRESHOLD_SHIFT);
  241. writel(temp, host->ioaddr + SDEDM);
  242. msleep(20);
  243. writel(SDVDD_POWER_ON, host->ioaddr + SDVDD);
  244. msleep(20);
  245. host->clock = 0;
  246. writel(host->hcfg, host->ioaddr + SDHCFG);
  247. writel(host->cdiv, host->ioaddr + SDCDIV);
  248. }
  249. static void bcm2835_reset(struct mmc_host *mmc)
  250. {
  251. struct bcm2835_host *host = mmc_priv(mmc);
  252. if (host->dma_chan)
  253. dmaengine_terminate_sync(host->dma_chan);
  254. bcm2835_reset_internal(host);
  255. }
  256. static void bcm2835_finish_command(struct bcm2835_host *host);
  257. static void bcm2835_wait_transfer_complete(struct bcm2835_host *host)
  258. {
  259. int timediff;
  260. u32 alternate_idle;
  261. alternate_idle = (host->mrq->data->flags & MMC_DATA_READ) ?
  262. SDEDM_FSM_READWAIT : SDEDM_FSM_WRITESTART1;
  263. timediff = 0;
  264. while (1) {
  265. u32 edm, fsm;
  266. edm = readl(host->ioaddr + SDEDM);
  267. fsm = edm & SDEDM_FSM_MASK;
  268. if ((fsm == SDEDM_FSM_IDENTMODE) ||
  269. (fsm == SDEDM_FSM_DATAMODE))
  270. break;
  271. if (fsm == alternate_idle) {
  272. writel(edm | SDEDM_FORCE_DATA_MODE,
  273. host->ioaddr + SDEDM);
  274. break;
  275. }
  276. timediff++;
  277. if (timediff == 100000) {
  278. dev_err(&host->pdev->dev,
  279. "wait_transfer_complete - still waiting after %d retries\n",
  280. timediff);
  281. bcm2835_dumpregs(host);
  282. host->mrq->data->error = -ETIMEDOUT;
  283. return;
  284. }
  285. cpu_relax();
  286. }
  287. }
  288. static void bcm2835_dma_complete(void *param)
  289. {
  290. struct bcm2835_host *host = param;
  291. schedule_work(&host->dma_work);
  292. }
  293. static void bcm2835_transfer_block_pio(struct bcm2835_host *host, bool is_read)
  294. {
  295. unsigned long flags;
  296. size_t blksize;
  297. unsigned long wait_max;
  298. blksize = host->data->blksz;
  299. wait_max = jiffies + msecs_to_jiffies(500);
  300. local_irq_save(flags);
  301. while (blksize) {
  302. int copy_words;
  303. u32 hsts = 0;
  304. size_t len;
  305. u32 *buf;
  306. if (!sg_miter_next(&host->sg_miter)) {
  307. host->data->error = -EINVAL;
  308. break;
  309. }
  310. len = min(host->sg_miter.length, blksize);
  311. if (len % 4) {
  312. host->data->error = -EINVAL;
  313. break;
  314. }
  315. blksize -= len;
  316. host->sg_miter.consumed = len;
  317. buf = (u32 *)host->sg_miter.addr;
  318. copy_words = len / 4;
  319. while (copy_words) {
  320. int burst_words, words;
  321. u32 edm;
  322. burst_words = min(SDDATA_FIFO_PIO_BURST, copy_words);
  323. edm = readl(host->ioaddr + SDEDM);
  324. if (is_read)
  325. words = ((edm >> 4) & 0x1f);
  326. else
  327. words = SDDATA_FIFO_WORDS - ((edm >> 4) & 0x1f);
  328. if (words < burst_words) {
  329. int fsm_state = (edm & SDEDM_FSM_MASK);
  330. struct device *dev = &host->pdev->dev;
  331. if ((is_read &&
  332. (fsm_state != SDEDM_FSM_READDATA &&
  333. fsm_state != SDEDM_FSM_READWAIT &&
  334. fsm_state != SDEDM_FSM_READCRC)) ||
  335. (!is_read &&
  336. (fsm_state != SDEDM_FSM_WRITEDATA &&
  337. fsm_state != SDEDM_FSM_WRITESTART1 &&
  338. fsm_state != SDEDM_FSM_WRITESTART2))) {
  339. hsts = readl(host->ioaddr + SDHSTS);
  340. dev_err(dev, "fsm %x, hsts %08x\n",
  341. fsm_state, hsts);
  342. if (hsts & SDHSTS_ERROR_MASK)
  343. break;
  344. }
  345. if (time_after(jiffies, wait_max)) {
  346. dev_err(dev, "PIO %s timeout - EDM %08x\n",
  347. is_read ? "read" : "write",
  348. edm);
  349. hsts = SDHSTS_REW_TIME_OUT;
  350. break;
  351. }
  352. ndelay((burst_words - words) *
  353. host->ns_per_fifo_word);
  354. continue;
  355. } else if (words > copy_words) {
  356. words = copy_words;
  357. }
  358. copy_words -= words;
  359. while (words) {
  360. if (is_read)
  361. *(buf++) = readl(host->ioaddr + SDDATA);
  362. else
  363. writel(*(buf++), host->ioaddr + SDDATA);
  364. words--;
  365. }
  366. }
  367. if (hsts & SDHSTS_ERROR_MASK)
  368. break;
  369. }
  370. sg_miter_stop(&host->sg_miter);
  371. local_irq_restore(flags);
  372. }
  373. static void bcm2835_transfer_pio(struct bcm2835_host *host)
  374. {
  375. struct device *dev = &host->pdev->dev;
  376. u32 sdhsts;
  377. bool is_read;
  378. is_read = (host->data->flags & MMC_DATA_READ) != 0;
  379. bcm2835_transfer_block_pio(host, is_read);
  380. sdhsts = readl(host->ioaddr + SDHSTS);
  381. if (sdhsts & (SDHSTS_CRC16_ERROR |
  382. SDHSTS_CRC7_ERROR |
  383. SDHSTS_FIFO_ERROR)) {
  384. dev_err(dev, "%s transfer error - HSTS %08x\n",
  385. is_read ? "read" : "write", sdhsts);
  386. host->data->error = -EILSEQ;
  387. } else if ((sdhsts & (SDHSTS_CMD_TIME_OUT |
  388. SDHSTS_REW_TIME_OUT))) {
  389. dev_err(dev, "%s timeout error - HSTS %08x\n",
  390. is_read ? "read" : "write", sdhsts);
  391. host->data->error = -ETIMEDOUT;
  392. }
  393. }
  394. static
  395. void bcm2835_prepare_dma(struct bcm2835_host *host, struct mmc_data *data)
  396. {
  397. int len, dir_data, dir_slave;
  398. struct dma_async_tx_descriptor *desc = NULL;
  399. struct dma_chan *dma_chan;
  400. dma_chan = host->dma_chan_rxtx;
  401. if (data->flags & MMC_DATA_READ) {
  402. dir_data = DMA_FROM_DEVICE;
  403. dir_slave = DMA_DEV_TO_MEM;
  404. } else {
  405. dir_data = DMA_TO_DEVICE;
  406. dir_slave = DMA_MEM_TO_DEV;
  407. }
  408. /* The block doesn't manage the FIFO DREQs properly for
  409. * multi-block transfers, so don't attempt to DMA the final
  410. * few words. Unfortunately this requires the final sg entry
  411. * to be trimmed. N.B. This code demands that the overspill
  412. * is contained in a single sg entry.
  413. */
  414. host->drain_words = 0;
  415. if ((data->blocks > 1) && (dir_data == DMA_FROM_DEVICE)) {
  416. struct scatterlist *sg;
  417. u32 len;
  418. int i;
  419. len = min((u32)(FIFO_READ_THRESHOLD - 1) * 4,
  420. (u32)data->blocks * data->blksz);
  421. for_each_sg(data->sg, sg, data->sg_len, i) {
  422. if (sg_is_last(sg)) {
  423. WARN_ON(sg->length < len);
  424. sg->length -= len;
  425. host->drain_page = sg_page(sg);
  426. host->drain_offset = sg->offset + sg->length;
  427. }
  428. }
  429. host->drain_words = len / 4;
  430. }
  431. /* The parameters have already been validated, so this will not fail */
  432. (void)dmaengine_slave_config(dma_chan,
  433. (dir_data == DMA_FROM_DEVICE) ?
  434. &host->dma_cfg_rx :
  435. &host->dma_cfg_tx);
  436. len = dma_map_sg(dma_chan->device->dev, data->sg, data->sg_len,
  437. dir_data);
  438. if (len > 0) {
  439. desc = dmaengine_prep_slave_sg(dma_chan, data->sg,
  440. len, dir_slave,
  441. DMA_PREP_INTERRUPT |
  442. DMA_CTRL_ACK);
  443. }
  444. if (desc) {
  445. desc->callback = bcm2835_dma_complete;
  446. desc->callback_param = host;
  447. host->dma_desc = desc;
  448. host->dma_chan = dma_chan;
  449. host->dma_dir = dir_data;
  450. }
  451. }
  452. static void bcm2835_start_dma(struct bcm2835_host *host)
  453. {
  454. dmaengine_submit(host->dma_desc);
  455. dma_async_issue_pending(host->dma_chan);
  456. }
  457. static void bcm2835_set_transfer_irqs(struct bcm2835_host *host)
  458. {
  459. u32 all_irqs = SDHCFG_DATA_IRPT_EN | SDHCFG_BLOCK_IRPT_EN |
  460. SDHCFG_BUSY_IRPT_EN;
  461. if (host->dma_desc) {
  462. host->hcfg = (host->hcfg & ~all_irqs) |
  463. SDHCFG_BUSY_IRPT_EN;
  464. } else {
  465. host->hcfg = (host->hcfg & ~all_irqs) |
  466. SDHCFG_DATA_IRPT_EN |
  467. SDHCFG_BUSY_IRPT_EN;
  468. }
  469. writel(host->hcfg, host->ioaddr + SDHCFG);
  470. }
  471. static
  472. void bcm2835_prepare_data(struct bcm2835_host *host, struct mmc_command *cmd)
  473. {
  474. struct mmc_data *data = cmd->data;
  475. WARN_ON(host->data);
  476. host->data = data;
  477. if (!data)
  478. return;
  479. host->data_complete = false;
  480. host->data->bytes_xfered = 0;
  481. if (!host->dma_desc) {
  482. /* Use PIO */
  483. int flags = SG_MITER_ATOMIC;
  484. if (data->flags & MMC_DATA_READ)
  485. flags |= SG_MITER_TO_SG;
  486. else
  487. flags |= SG_MITER_FROM_SG;
  488. sg_miter_start(&host->sg_miter, data->sg, data->sg_len, flags);
  489. host->blocks = data->blocks;
  490. }
  491. bcm2835_set_transfer_irqs(host);
  492. writel(data->blksz, host->ioaddr + SDHBCT);
  493. writel(data->blocks, host->ioaddr + SDHBLC);
  494. }
  495. static u32 bcm2835_read_wait_sdcmd(struct bcm2835_host *host, u32 max_ms)
  496. {
  497. struct device *dev = &host->pdev->dev;
  498. u32 value;
  499. int ret;
  500. ret = readl_poll_timeout(host->ioaddr + SDCMD, value,
  501. !(value & SDCMD_NEW_FLAG), 1, 10);
  502. if (ret == -ETIMEDOUT)
  503. /* if it takes a while make poll interval bigger */
  504. ret = readl_poll_timeout(host->ioaddr + SDCMD, value,
  505. !(value & SDCMD_NEW_FLAG),
  506. 10, max_ms * 1000);
  507. if (ret == -ETIMEDOUT)
  508. dev_err(dev, "%s: timeout (%d ms)\n", __func__, max_ms);
  509. return value;
  510. }
  511. static void bcm2835_finish_request(struct bcm2835_host *host)
  512. {
  513. struct dma_chan *terminate_chan = NULL;
  514. struct mmc_request *mrq;
  515. cancel_delayed_work(&host->timeout_work);
  516. mrq = host->mrq;
  517. host->mrq = NULL;
  518. host->cmd = NULL;
  519. host->data = NULL;
  520. host->dma_desc = NULL;
  521. terminate_chan = host->dma_chan;
  522. host->dma_chan = NULL;
  523. if (terminate_chan) {
  524. int err = dmaengine_terminate_all(terminate_chan);
  525. if (err)
  526. dev_err(&host->pdev->dev,
  527. "failed to terminate DMA (%d)\n", err);
  528. }
  529. mmc_request_done(host->mmc, mrq);
  530. }
  531. static
  532. bool bcm2835_send_command(struct bcm2835_host *host, struct mmc_command *cmd)
  533. {
  534. struct device *dev = &host->pdev->dev;
  535. u32 sdcmd, sdhsts;
  536. unsigned long timeout;
  537. WARN_ON(host->cmd);
  538. sdcmd = bcm2835_read_wait_sdcmd(host, 100);
  539. if (sdcmd & SDCMD_NEW_FLAG) {
  540. dev_err(dev, "previous command never completed.\n");
  541. bcm2835_dumpregs(host);
  542. cmd->error = -EILSEQ;
  543. bcm2835_finish_request(host);
  544. return false;
  545. }
  546. if (!cmd->data && cmd->busy_timeout > 9000)
  547. timeout = DIV_ROUND_UP(cmd->busy_timeout, 1000) * HZ + HZ;
  548. else
  549. timeout = 10 * HZ;
  550. schedule_delayed_work(&host->timeout_work, timeout);
  551. host->cmd = cmd;
  552. /* Clear any error flags */
  553. sdhsts = readl(host->ioaddr + SDHSTS);
  554. if (sdhsts & SDHSTS_ERROR_MASK)
  555. writel(sdhsts, host->ioaddr + SDHSTS);
  556. if ((cmd->flags & MMC_RSP_136) && (cmd->flags & MMC_RSP_BUSY)) {
  557. dev_err(dev, "unsupported response type!\n");
  558. cmd->error = -EINVAL;
  559. bcm2835_finish_request(host);
  560. return false;
  561. }
  562. bcm2835_prepare_data(host, cmd);
  563. writel(cmd->arg, host->ioaddr + SDARG);
  564. sdcmd = cmd->opcode & SDCMD_CMD_MASK;
  565. host->use_busy = false;
  566. if (!(cmd->flags & MMC_RSP_PRESENT)) {
  567. sdcmd |= SDCMD_NO_RESPONSE;
  568. } else {
  569. if (cmd->flags & MMC_RSP_136)
  570. sdcmd |= SDCMD_LONG_RESPONSE;
  571. if (cmd->flags & MMC_RSP_BUSY) {
  572. sdcmd |= SDCMD_BUSYWAIT;
  573. host->use_busy = true;
  574. }
  575. }
  576. if (cmd->data) {
  577. if (cmd->data->flags & MMC_DATA_WRITE)
  578. sdcmd |= SDCMD_WRITE_CMD;
  579. if (cmd->data->flags & MMC_DATA_READ)
  580. sdcmd |= SDCMD_READ_CMD;
  581. }
  582. writel(sdcmd | SDCMD_NEW_FLAG, host->ioaddr + SDCMD);
  583. return true;
  584. }
  585. static void bcm2835_transfer_complete(struct bcm2835_host *host)
  586. {
  587. struct mmc_data *data;
  588. WARN_ON(!host->data_complete);
  589. data = host->data;
  590. host->data = NULL;
  591. /* Need to send CMD12 if -
  592. * a) open-ended multiblock transfer (no CMD23)
  593. * b) error in multiblock transfer
  594. */
  595. if (host->mrq->stop && (data->error || !host->use_sbc)) {
  596. if (bcm2835_send_command(host, host->mrq->stop)) {
  597. /* No busy, so poll for completion */
  598. if (!host->use_busy)
  599. bcm2835_finish_command(host);
  600. }
  601. } else {
  602. bcm2835_wait_transfer_complete(host);
  603. bcm2835_finish_request(host);
  604. }
  605. }
  606. static void bcm2835_finish_data(struct bcm2835_host *host)
  607. {
  608. struct device *dev = &host->pdev->dev;
  609. struct mmc_data *data;
  610. data = host->data;
  611. host->hcfg &= ~(SDHCFG_DATA_IRPT_EN | SDHCFG_BLOCK_IRPT_EN);
  612. writel(host->hcfg, host->ioaddr + SDHCFG);
  613. data->bytes_xfered = data->error ? 0 : (data->blksz * data->blocks);
  614. host->data_complete = true;
  615. if (host->cmd) {
  616. /* Data managed to finish before the
  617. * command completed. Make sure we do
  618. * things in the proper order.
  619. */
  620. dev_dbg(dev, "Finished early - HSTS %08x\n",
  621. readl(host->ioaddr + SDHSTS));
  622. } else {
  623. bcm2835_transfer_complete(host);
  624. }
  625. }
  626. static void bcm2835_finish_command(struct bcm2835_host *host)
  627. {
  628. struct device *dev = &host->pdev->dev;
  629. struct mmc_command *cmd = host->cmd;
  630. u32 sdcmd;
  631. sdcmd = bcm2835_read_wait_sdcmd(host, 100);
  632. /* Check for errors */
  633. if (sdcmd & SDCMD_NEW_FLAG) {
  634. dev_err(dev, "command never completed.\n");
  635. bcm2835_dumpregs(host);
  636. host->cmd->error = -EIO;
  637. bcm2835_finish_request(host);
  638. return;
  639. } else if (sdcmd & SDCMD_FAIL_FLAG) {
  640. u32 sdhsts = readl(host->ioaddr + SDHSTS);
  641. /* Clear the errors */
  642. writel(SDHSTS_ERROR_MASK, host->ioaddr + SDHSTS);
  643. if (!(sdhsts & SDHSTS_CRC7_ERROR) ||
  644. (host->cmd->opcode != MMC_SEND_OP_COND)) {
  645. if (sdhsts & SDHSTS_CMD_TIME_OUT) {
  646. host->cmd->error = -ETIMEDOUT;
  647. } else {
  648. dev_err(dev, "unexpected command %d error\n",
  649. host->cmd->opcode);
  650. bcm2835_dumpregs(host);
  651. host->cmd->error = -EILSEQ;
  652. }
  653. bcm2835_finish_request(host);
  654. return;
  655. }
  656. }
  657. if (cmd->flags & MMC_RSP_PRESENT) {
  658. if (cmd->flags & MMC_RSP_136) {
  659. int i;
  660. for (i = 0; i < 4; i++) {
  661. cmd->resp[3 - i] =
  662. readl(host->ioaddr + SDRSP0 + i * 4);
  663. }
  664. } else {
  665. cmd->resp[0] = readl(host->ioaddr + SDRSP0);
  666. }
  667. }
  668. if (cmd == host->mrq->sbc) {
  669. /* Finished CMD23, now send actual command. */
  670. host->cmd = NULL;
  671. if (bcm2835_send_command(host, host->mrq->cmd)) {
  672. if (host->data && host->dma_desc)
  673. /* DMA transfer starts now, PIO starts
  674. * after irq
  675. */
  676. bcm2835_start_dma(host);
  677. if (!host->use_busy)
  678. bcm2835_finish_command(host);
  679. }
  680. } else if (cmd == host->mrq->stop) {
  681. /* Finished CMD12 */
  682. bcm2835_finish_request(host);
  683. } else {
  684. /* Processed actual command. */
  685. host->cmd = NULL;
  686. if (!host->data)
  687. bcm2835_finish_request(host);
  688. else if (host->data_complete)
  689. bcm2835_transfer_complete(host);
  690. }
  691. }
  692. static void bcm2835_timeout(struct work_struct *work)
  693. {
  694. struct delayed_work *d = to_delayed_work(work);
  695. struct bcm2835_host *host =
  696. container_of(d, struct bcm2835_host, timeout_work);
  697. struct device *dev = &host->pdev->dev;
  698. mutex_lock(&host->mutex);
  699. if (host->mrq) {
  700. dev_err(dev, "timeout waiting for hardware interrupt.\n");
  701. bcm2835_dumpregs(host);
  702. if (host->data) {
  703. host->data->error = -ETIMEDOUT;
  704. bcm2835_finish_data(host);
  705. } else {
  706. if (host->cmd)
  707. host->cmd->error = -ETIMEDOUT;
  708. else
  709. host->mrq->cmd->error = -ETIMEDOUT;
  710. bcm2835_finish_request(host);
  711. }
  712. }
  713. mutex_unlock(&host->mutex);
  714. }
  715. static bool bcm2835_check_cmd_error(struct bcm2835_host *host, u32 intmask)
  716. {
  717. struct device *dev = &host->pdev->dev;
  718. if (!(intmask & SDHSTS_ERROR_MASK))
  719. return false;
  720. if (!host->cmd)
  721. return true;
  722. dev_err(dev, "sdhost_busy_irq: intmask %08x\n", intmask);
  723. if (intmask & SDHSTS_CRC7_ERROR) {
  724. host->cmd->error = -EILSEQ;
  725. } else if (intmask & (SDHSTS_CRC16_ERROR |
  726. SDHSTS_FIFO_ERROR)) {
  727. if (host->mrq->data)
  728. host->mrq->data->error = -EILSEQ;
  729. else
  730. host->cmd->error = -EILSEQ;
  731. } else if (intmask & SDHSTS_REW_TIME_OUT) {
  732. if (host->mrq->data)
  733. host->mrq->data->error = -ETIMEDOUT;
  734. else
  735. host->cmd->error = -ETIMEDOUT;
  736. } else if (intmask & SDHSTS_CMD_TIME_OUT) {
  737. host->cmd->error = -ETIMEDOUT;
  738. }
  739. bcm2835_dumpregs(host);
  740. return true;
  741. }
  742. static void bcm2835_check_data_error(struct bcm2835_host *host, u32 intmask)
  743. {
  744. if (!host->data)
  745. return;
  746. if (intmask & (SDHSTS_CRC16_ERROR | SDHSTS_FIFO_ERROR))
  747. host->data->error = -EILSEQ;
  748. if (intmask & SDHSTS_REW_TIME_OUT)
  749. host->data->error = -ETIMEDOUT;
  750. }
  751. static void bcm2835_busy_irq(struct bcm2835_host *host)
  752. {
  753. if (WARN_ON(!host->cmd)) {
  754. bcm2835_dumpregs(host);
  755. return;
  756. }
  757. if (WARN_ON(!host->use_busy)) {
  758. bcm2835_dumpregs(host);
  759. return;
  760. }
  761. host->use_busy = false;
  762. bcm2835_finish_command(host);
  763. }
  764. static void bcm2835_data_irq(struct bcm2835_host *host, u32 intmask)
  765. {
  766. /* There are no dedicated data/space available interrupt
  767. * status bits, so it is necessary to use the single shared
  768. * data/space available FIFO status bits. It is therefore not
  769. * an error to get here when there is no data transfer in
  770. * progress.
  771. */
  772. if (!host->data)
  773. return;
  774. bcm2835_check_data_error(host, intmask);
  775. if (host->data->error)
  776. goto finished;
  777. if (host->data->flags & MMC_DATA_WRITE) {
  778. /* Use the block interrupt for writes after the first block */
  779. host->hcfg &= ~(SDHCFG_DATA_IRPT_EN);
  780. host->hcfg |= SDHCFG_BLOCK_IRPT_EN;
  781. writel(host->hcfg, host->ioaddr + SDHCFG);
  782. bcm2835_transfer_pio(host);
  783. } else {
  784. bcm2835_transfer_pio(host);
  785. host->blocks--;
  786. if ((host->blocks == 0) || host->data->error)
  787. goto finished;
  788. }
  789. return;
  790. finished:
  791. host->hcfg &= ~(SDHCFG_DATA_IRPT_EN | SDHCFG_BLOCK_IRPT_EN);
  792. writel(host->hcfg, host->ioaddr + SDHCFG);
  793. }
  794. static void bcm2835_data_threaded_irq(struct bcm2835_host *host)
  795. {
  796. if (!host->data)
  797. return;
  798. if ((host->blocks == 0) || host->data->error)
  799. bcm2835_finish_data(host);
  800. }
  801. static void bcm2835_block_irq(struct bcm2835_host *host)
  802. {
  803. if (WARN_ON(!host->data)) {
  804. bcm2835_dumpregs(host);
  805. return;
  806. }
  807. if (!host->dma_desc) {
  808. WARN_ON(!host->blocks);
  809. if (host->data->error || (--host->blocks == 0))
  810. bcm2835_finish_data(host);
  811. else
  812. bcm2835_transfer_pio(host);
  813. } else if (host->data->flags & MMC_DATA_WRITE) {
  814. bcm2835_finish_data(host);
  815. }
  816. }
  817. static irqreturn_t bcm2835_irq(int irq, void *dev_id)
  818. {
  819. irqreturn_t result = IRQ_NONE;
  820. struct bcm2835_host *host = dev_id;
  821. u32 intmask;
  822. spin_lock(&host->lock);
  823. intmask = readl(host->ioaddr + SDHSTS);
  824. writel(SDHSTS_BUSY_IRPT |
  825. SDHSTS_BLOCK_IRPT |
  826. SDHSTS_SDIO_IRPT |
  827. SDHSTS_DATA_FLAG,
  828. host->ioaddr + SDHSTS);
  829. if (intmask & SDHSTS_BLOCK_IRPT) {
  830. bcm2835_check_data_error(host, intmask);
  831. host->irq_block = true;
  832. result = IRQ_WAKE_THREAD;
  833. }
  834. if (intmask & SDHSTS_BUSY_IRPT) {
  835. if (!bcm2835_check_cmd_error(host, intmask)) {
  836. host->irq_busy = true;
  837. result = IRQ_WAKE_THREAD;
  838. } else {
  839. result = IRQ_HANDLED;
  840. }
  841. }
  842. /* There is no true data interrupt status bit, so it is
  843. * necessary to qualify the data flag with the interrupt
  844. * enable bit.
  845. */
  846. if ((intmask & SDHSTS_DATA_FLAG) &&
  847. (host->hcfg & SDHCFG_DATA_IRPT_EN)) {
  848. bcm2835_data_irq(host, intmask);
  849. host->irq_data = true;
  850. result = IRQ_WAKE_THREAD;
  851. }
  852. spin_unlock(&host->lock);
  853. return result;
  854. }
  855. static irqreturn_t bcm2835_threaded_irq(int irq, void *dev_id)
  856. {
  857. struct bcm2835_host *host = dev_id;
  858. unsigned long flags;
  859. bool block, busy, data;
  860. spin_lock_irqsave(&host->lock, flags);
  861. block = host->irq_block;
  862. busy = host->irq_busy;
  863. data = host->irq_data;
  864. host->irq_block = false;
  865. host->irq_busy = false;
  866. host->irq_data = false;
  867. spin_unlock_irqrestore(&host->lock, flags);
  868. mutex_lock(&host->mutex);
  869. if (block)
  870. bcm2835_block_irq(host);
  871. if (busy)
  872. bcm2835_busy_irq(host);
  873. if (data)
  874. bcm2835_data_threaded_irq(host);
  875. mutex_unlock(&host->mutex);
  876. return IRQ_HANDLED;
  877. }
  878. static void bcm2835_dma_complete_work(struct work_struct *work)
  879. {
  880. struct bcm2835_host *host =
  881. container_of(work, struct bcm2835_host, dma_work);
  882. struct mmc_data *data = host->data;
  883. mutex_lock(&host->mutex);
  884. if (host->dma_chan) {
  885. dma_unmap_sg(host->dma_chan->device->dev,
  886. data->sg, data->sg_len,
  887. host->dma_dir);
  888. host->dma_chan = NULL;
  889. }
  890. if (host->drain_words) {
  891. unsigned long flags;
  892. void *page;
  893. u32 *buf;
  894. if (host->drain_offset & PAGE_MASK) {
  895. host->drain_page += host->drain_offset >> PAGE_SHIFT;
  896. host->drain_offset &= ~PAGE_MASK;
  897. }
  898. local_irq_save(flags);
  899. page = kmap_atomic(host->drain_page);
  900. buf = page + host->drain_offset;
  901. while (host->drain_words) {
  902. u32 edm = readl(host->ioaddr + SDEDM);
  903. if ((edm >> 4) & 0x1f)
  904. *(buf++) = readl(host->ioaddr + SDDATA);
  905. host->drain_words--;
  906. }
  907. kunmap_atomic(page);
  908. local_irq_restore(flags);
  909. }
  910. bcm2835_finish_data(host);
  911. mutex_unlock(&host->mutex);
  912. }
  913. static void bcm2835_set_clock(struct bcm2835_host *host, unsigned int clock)
  914. {
  915. int div;
  916. /* The SDCDIV register has 11 bits, and holds (div - 2). But
  917. * in data mode the max is 50MHz wihout a minimum, and only
  918. * the bottom 3 bits are used. Since the switch over is
  919. * automatic (unless we have marked the card as slow...),
  920. * chosen values have to make sense in both modes. Ident mode
  921. * must be 100-400KHz, so can range check the requested
  922. * clock. CMD15 must be used to return to data mode, so this
  923. * can be monitored.
  924. *
  925. * clock 250MHz -> 0->125MHz, 1->83.3MHz, 2->62.5MHz, 3->50.0MHz
  926. * 4->41.7MHz, 5->35.7MHz, 6->31.3MHz, 7->27.8MHz
  927. *
  928. * 623->400KHz/27.8MHz
  929. * reset value (507)->491159/50MHz
  930. *
  931. * BUT, the 3-bit clock divisor in data mode is too small if
  932. * the core clock is higher than 250MHz, so instead use the
  933. * SLOW_CARD configuration bit to force the use of the ident
  934. * clock divisor at all times.
  935. */
  936. if (clock < 100000) {
  937. /* Can't stop the clock, but make it as slow as possible
  938. * to show willing
  939. */
  940. host->cdiv = SDCDIV_MAX_CDIV;
  941. writel(host->cdiv, host->ioaddr + SDCDIV);
  942. return;
  943. }
  944. div = host->max_clk / clock;
  945. if (div < 2)
  946. div = 2;
  947. if ((host->max_clk / div) > clock)
  948. div++;
  949. div -= 2;
  950. if (div > SDCDIV_MAX_CDIV)
  951. div = SDCDIV_MAX_CDIV;
  952. clock = host->max_clk / (div + 2);
  953. host->mmc->actual_clock = clock;
  954. /* Calibrate some delays */
  955. host->ns_per_fifo_word = (1000000000 / clock) *
  956. ((host->mmc->caps & MMC_CAP_4_BIT_DATA) ? 8 : 32);
  957. host->cdiv = div;
  958. writel(host->cdiv, host->ioaddr + SDCDIV);
  959. /* Set the timeout to 500ms */
  960. writel(host->mmc->actual_clock / 2, host->ioaddr + SDTOUT);
  961. }
  962. static void bcm2835_request(struct mmc_host *mmc, struct mmc_request *mrq)
  963. {
  964. struct bcm2835_host *host = mmc_priv(mmc);
  965. struct device *dev = &host->pdev->dev;
  966. u32 edm, fsm;
  967. /* Reset the error statuses in case this is a retry */
  968. if (mrq->sbc)
  969. mrq->sbc->error = 0;
  970. if (mrq->cmd)
  971. mrq->cmd->error = 0;
  972. if (mrq->data)
  973. mrq->data->error = 0;
  974. if (mrq->stop)
  975. mrq->stop->error = 0;
  976. if (mrq->data && !is_power_of_2(mrq->data->blksz)) {
  977. dev_err(dev, "unsupported block size (%d bytes)\n",
  978. mrq->data->blksz);
  979. if (mrq->cmd)
  980. mrq->cmd->error = -EINVAL;
  981. mmc_request_done(mmc, mrq);
  982. return;
  983. }
  984. if (host->use_dma && mrq->data && (mrq->data->blocks > PIO_THRESHOLD))
  985. bcm2835_prepare_dma(host, mrq->data);
  986. mutex_lock(&host->mutex);
  987. WARN_ON(host->mrq);
  988. host->mrq = mrq;
  989. edm = readl(host->ioaddr + SDEDM);
  990. fsm = edm & SDEDM_FSM_MASK;
  991. if ((fsm != SDEDM_FSM_IDENTMODE) &&
  992. (fsm != SDEDM_FSM_DATAMODE)) {
  993. dev_err(dev, "previous command (%d) not complete (EDM %08x)\n",
  994. readl(host->ioaddr + SDCMD) & SDCMD_CMD_MASK,
  995. edm);
  996. bcm2835_dumpregs(host);
  997. if (mrq->cmd)
  998. mrq->cmd->error = -EILSEQ;
  999. bcm2835_finish_request(host);
  1000. mutex_unlock(&host->mutex);
  1001. return;
  1002. }
  1003. host->use_sbc = !!mrq->sbc && host->mrq->data &&
  1004. (host->mrq->data->flags & MMC_DATA_READ);
  1005. if (host->use_sbc) {
  1006. if (bcm2835_send_command(host, mrq->sbc)) {
  1007. if (!host->use_busy)
  1008. bcm2835_finish_command(host);
  1009. }
  1010. } else if (mrq->cmd && bcm2835_send_command(host, mrq->cmd)) {
  1011. if (host->data && host->dma_desc) {
  1012. /* DMA transfer starts now, PIO starts after irq */
  1013. bcm2835_start_dma(host);
  1014. }
  1015. if (!host->use_busy)
  1016. bcm2835_finish_command(host);
  1017. }
  1018. mutex_unlock(&host->mutex);
  1019. }
  1020. static void bcm2835_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
  1021. {
  1022. struct bcm2835_host *host = mmc_priv(mmc);
  1023. mutex_lock(&host->mutex);
  1024. if (!ios->clock || ios->clock != host->clock) {
  1025. bcm2835_set_clock(host, ios->clock);
  1026. host->clock = ios->clock;
  1027. }
  1028. /* set bus width */
  1029. host->hcfg &= ~SDHCFG_WIDE_EXT_BUS;
  1030. if (ios->bus_width == MMC_BUS_WIDTH_4)
  1031. host->hcfg |= SDHCFG_WIDE_EXT_BUS;
  1032. host->hcfg |= SDHCFG_WIDE_INT_BUS;
  1033. /* Disable clever clock switching, to cope with fast core clocks */
  1034. host->hcfg |= SDHCFG_SLOW_CARD;
  1035. writel(host->hcfg, host->ioaddr + SDHCFG);
  1036. mutex_unlock(&host->mutex);
  1037. }
  1038. static const struct mmc_host_ops bcm2835_ops = {
  1039. .request = bcm2835_request,
  1040. .set_ios = bcm2835_set_ios,
  1041. .hw_reset = bcm2835_reset,
  1042. };
  1043. static int bcm2835_add_host(struct bcm2835_host *host)
  1044. {
  1045. struct mmc_host *mmc = host->mmc;
  1046. struct device *dev = &host->pdev->dev;
  1047. char pio_limit_string[20];
  1048. int ret;
  1049. if (!mmc->f_max || mmc->f_max > host->max_clk)
  1050. mmc->f_max = host->max_clk;
  1051. mmc->f_min = host->max_clk / SDCDIV_MAX_CDIV;
  1052. mmc->max_busy_timeout = ~0 / (mmc->f_max / 1000);
  1053. dev_dbg(dev, "f_max %d, f_min %d, max_busy_timeout %d\n",
  1054. mmc->f_max, mmc->f_min, mmc->max_busy_timeout);
  1055. /* host controller capabilities */
  1056. mmc->caps |= MMC_CAP_SD_HIGHSPEED | MMC_CAP_MMC_HIGHSPEED |
  1057. MMC_CAP_NEEDS_POLL | MMC_CAP_HW_RESET | MMC_CAP_ERASE |
  1058. MMC_CAP_CMD23;
  1059. spin_lock_init(&host->lock);
  1060. mutex_init(&host->mutex);
  1061. if (IS_ERR_OR_NULL(host->dma_chan_rxtx)) {
  1062. dev_warn(dev, "unable to initialise DMA channel. Falling back to PIO\n");
  1063. host->use_dma = false;
  1064. } else {
  1065. host->use_dma = true;
  1066. host->dma_cfg_tx.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  1067. host->dma_cfg_tx.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  1068. host->dma_cfg_tx.slave_id = 13; /* DREQ channel */
  1069. host->dma_cfg_tx.direction = DMA_MEM_TO_DEV;
  1070. host->dma_cfg_tx.src_addr = 0;
  1071. host->dma_cfg_tx.dst_addr = host->phys_addr + SDDATA;
  1072. host->dma_cfg_rx.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  1073. host->dma_cfg_rx.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  1074. host->dma_cfg_rx.slave_id = 13; /* DREQ channel */
  1075. host->dma_cfg_rx.direction = DMA_DEV_TO_MEM;
  1076. host->dma_cfg_rx.src_addr = host->phys_addr + SDDATA;
  1077. host->dma_cfg_rx.dst_addr = 0;
  1078. if (dmaengine_slave_config(host->dma_chan_rxtx,
  1079. &host->dma_cfg_tx) != 0 ||
  1080. dmaengine_slave_config(host->dma_chan_rxtx,
  1081. &host->dma_cfg_rx) != 0)
  1082. host->use_dma = false;
  1083. }
  1084. mmc->max_segs = 128;
  1085. mmc->max_req_size = 524288;
  1086. mmc->max_seg_size = mmc->max_req_size;
  1087. mmc->max_blk_size = 1024;
  1088. mmc->max_blk_count = 65535;
  1089. /* report supported voltage ranges */
  1090. mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34;
  1091. INIT_WORK(&host->dma_work, bcm2835_dma_complete_work);
  1092. INIT_DELAYED_WORK(&host->timeout_work, bcm2835_timeout);
  1093. /* Set interrupt enables */
  1094. host->hcfg = SDHCFG_BUSY_IRPT_EN;
  1095. bcm2835_reset_internal(host);
  1096. ret = request_threaded_irq(host->irq, bcm2835_irq,
  1097. bcm2835_threaded_irq,
  1098. 0, mmc_hostname(mmc), host);
  1099. if (ret) {
  1100. dev_err(dev, "failed to request IRQ %d: %d\n", host->irq, ret);
  1101. return ret;
  1102. }
  1103. ret = mmc_add_host(mmc);
  1104. if (ret) {
  1105. free_irq(host->irq, host);
  1106. return ret;
  1107. }
  1108. pio_limit_string[0] = '\0';
  1109. if (host->use_dma && (PIO_THRESHOLD > 0))
  1110. sprintf(pio_limit_string, " (>%d)", PIO_THRESHOLD);
  1111. dev_info(dev, "loaded - DMA %s%s\n",
  1112. host->use_dma ? "enabled" : "disabled", pio_limit_string);
  1113. return 0;
  1114. }
  1115. static int bcm2835_probe(struct platform_device *pdev)
  1116. {
  1117. struct device *dev = &pdev->dev;
  1118. struct clk *clk;
  1119. struct resource *iomem;
  1120. struct bcm2835_host *host;
  1121. struct mmc_host *mmc;
  1122. const __be32 *regaddr_p;
  1123. int ret;
  1124. dev_dbg(dev, "%s\n", __func__);
  1125. mmc = mmc_alloc_host(sizeof(*host), dev);
  1126. if (!mmc)
  1127. return -ENOMEM;
  1128. mmc->ops = &bcm2835_ops;
  1129. host = mmc_priv(mmc);
  1130. host->mmc = mmc;
  1131. host->pdev = pdev;
  1132. spin_lock_init(&host->lock);
  1133. iomem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1134. host->ioaddr = devm_ioremap_resource(dev, iomem);
  1135. if (IS_ERR(host->ioaddr)) {
  1136. ret = PTR_ERR(host->ioaddr);
  1137. goto err;
  1138. }
  1139. /* Parse OF address directly to get the physical address for
  1140. * DMA to our registers.
  1141. */
  1142. regaddr_p = of_get_address(pdev->dev.of_node, 0, NULL, NULL);
  1143. if (!regaddr_p) {
  1144. dev_err(dev, "Can't get phys address\n");
  1145. ret = -EINVAL;
  1146. goto err;
  1147. }
  1148. host->phys_addr = be32_to_cpup(regaddr_p);
  1149. host->dma_chan = NULL;
  1150. host->dma_desc = NULL;
  1151. host->dma_chan_rxtx = dma_request_slave_channel(dev, "rx-tx");
  1152. clk = devm_clk_get(dev, NULL);
  1153. if (IS_ERR(clk)) {
  1154. ret = PTR_ERR(clk);
  1155. if (ret != -EPROBE_DEFER)
  1156. dev_err(dev, "could not get clk: %d\n", ret);
  1157. goto err;
  1158. }
  1159. host->max_clk = clk_get_rate(clk);
  1160. host->irq = platform_get_irq(pdev, 0);
  1161. if (host->irq <= 0) {
  1162. dev_err(dev, "get IRQ failed\n");
  1163. ret = -EINVAL;
  1164. goto err;
  1165. }
  1166. ret = mmc_of_parse(mmc);
  1167. if (ret)
  1168. goto err;
  1169. ret = bcm2835_add_host(host);
  1170. if (ret)
  1171. goto err;
  1172. platform_set_drvdata(pdev, host);
  1173. dev_dbg(dev, "%s -> OK\n", __func__);
  1174. return 0;
  1175. err:
  1176. dev_dbg(dev, "%s -> err %d\n", __func__, ret);
  1177. mmc_free_host(mmc);
  1178. return ret;
  1179. }
  1180. static int bcm2835_remove(struct platform_device *pdev)
  1181. {
  1182. struct bcm2835_host *host = platform_get_drvdata(pdev);
  1183. mmc_remove_host(host->mmc);
  1184. writel(SDVDD_POWER_OFF, host->ioaddr + SDVDD);
  1185. free_irq(host->irq, host);
  1186. cancel_work_sync(&host->dma_work);
  1187. cancel_delayed_work_sync(&host->timeout_work);
  1188. mmc_free_host(host->mmc);
  1189. platform_set_drvdata(pdev, NULL);
  1190. return 0;
  1191. }
  1192. static const struct of_device_id bcm2835_match[] = {
  1193. { .compatible = "brcm,bcm2835-sdhost" },
  1194. { }
  1195. };
  1196. MODULE_DEVICE_TABLE(of, bcm2835_match);
  1197. static struct platform_driver bcm2835_driver = {
  1198. .probe = bcm2835_probe,
  1199. .remove = bcm2835_remove,
  1200. .driver = {
  1201. .name = "sdhost-bcm2835",
  1202. .of_match_table = bcm2835_match,
  1203. },
  1204. };
  1205. module_platform_driver(bcm2835_driver);
  1206. MODULE_ALIAS("platform:sdhost-bcm2835");
  1207. MODULE_DESCRIPTION("BCM2835 SDHost driver");
  1208. MODULE_LICENSE("GPL v2");
  1209. MODULE_AUTHOR("Phil Elwell");