bcm2835.c 36 KB

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