bcm2835.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475
  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, data->sg_len,
  430. dir_data);
  431. return;
  432. }
  433. desc->callback = bcm2835_dma_complete;
  434. desc->callback_param = host;
  435. host->dma_desc = desc;
  436. host->dma_chan = dma_chan;
  437. host->dma_dir = dir_data;
  438. }
  439. static void bcm2835_start_dma(struct bcm2835_host *host)
  440. {
  441. dmaengine_submit(host->dma_desc);
  442. dma_async_issue_pending(host->dma_chan);
  443. }
  444. static void bcm2835_set_transfer_irqs(struct bcm2835_host *host)
  445. {
  446. u32 all_irqs = SDHCFG_DATA_IRPT_EN | SDHCFG_BLOCK_IRPT_EN |
  447. SDHCFG_BUSY_IRPT_EN;
  448. if (host->dma_desc) {
  449. host->hcfg = (host->hcfg & ~all_irqs) |
  450. SDHCFG_BUSY_IRPT_EN;
  451. } else {
  452. host->hcfg = (host->hcfg & ~all_irqs) |
  453. SDHCFG_DATA_IRPT_EN |
  454. SDHCFG_BUSY_IRPT_EN;
  455. }
  456. writel(host->hcfg, host->ioaddr + SDHCFG);
  457. }
  458. static
  459. void bcm2835_prepare_data(struct bcm2835_host *host, struct mmc_command *cmd)
  460. {
  461. struct mmc_data *data = cmd->data;
  462. WARN_ON(host->data);
  463. host->data = data;
  464. if (!data)
  465. return;
  466. host->data_complete = false;
  467. host->data->bytes_xfered = 0;
  468. if (!host->dma_desc) {
  469. /* Use PIO */
  470. int flags = SG_MITER_ATOMIC;
  471. if (data->flags & MMC_DATA_READ)
  472. flags |= SG_MITER_TO_SG;
  473. else
  474. flags |= SG_MITER_FROM_SG;
  475. sg_miter_start(&host->sg_miter, data->sg, data->sg_len, flags);
  476. host->blocks = data->blocks;
  477. }
  478. bcm2835_set_transfer_irqs(host);
  479. writel(data->blksz, host->ioaddr + SDHBCT);
  480. writel(data->blocks, host->ioaddr + SDHBLC);
  481. }
  482. static u32 bcm2835_read_wait_sdcmd(struct bcm2835_host *host, u32 max_ms)
  483. {
  484. struct device *dev = &host->pdev->dev;
  485. u32 value;
  486. int ret;
  487. ret = readl_poll_timeout(host->ioaddr + SDCMD, value,
  488. !(value & SDCMD_NEW_FLAG), 1, 10);
  489. if (ret == -ETIMEDOUT)
  490. /* if it takes a while make poll interval bigger */
  491. ret = readl_poll_timeout(host->ioaddr + SDCMD, value,
  492. !(value & SDCMD_NEW_FLAG),
  493. 10, max_ms * 1000);
  494. if (ret == -ETIMEDOUT)
  495. dev_err(dev, "%s: timeout (%d ms)\n", __func__, max_ms);
  496. return value;
  497. }
  498. static void bcm2835_finish_request(struct bcm2835_host *host)
  499. {
  500. struct dma_chan *terminate_chan = NULL;
  501. struct mmc_request *mrq;
  502. cancel_delayed_work(&host->timeout_work);
  503. mrq = host->mrq;
  504. host->mrq = NULL;
  505. host->cmd = NULL;
  506. host->data = NULL;
  507. host->dma_desc = NULL;
  508. terminate_chan = host->dma_chan;
  509. host->dma_chan = NULL;
  510. if (terminate_chan) {
  511. int err = dmaengine_terminate_all(terminate_chan);
  512. if (err)
  513. dev_err(&host->pdev->dev,
  514. "failed to terminate DMA (%d)\n", err);
  515. }
  516. mmc_request_done(mmc_from_priv(host), mrq);
  517. }
  518. static
  519. bool bcm2835_send_command(struct bcm2835_host *host, struct mmc_command *cmd)
  520. {
  521. struct device *dev = &host->pdev->dev;
  522. u32 sdcmd, sdhsts;
  523. unsigned long timeout;
  524. WARN_ON(host->cmd);
  525. sdcmd = bcm2835_read_wait_sdcmd(host, 100);
  526. if (sdcmd & SDCMD_NEW_FLAG) {
  527. dev_err(dev, "previous command never completed.\n");
  528. bcm2835_dumpregs(host);
  529. cmd->error = -EILSEQ;
  530. bcm2835_finish_request(host);
  531. return false;
  532. }
  533. if (!cmd->data && cmd->busy_timeout > 9000)
  534. timeout = DIV_ROUND_UP(cmd->busy_timeout, 1000) * HZ + HZ;
  535. else
  536. timeout = 10 * HZ;
  537. schedule_delayed_work(&host->timeout_work, timeout);
  538. host->cmd = cmd;
  539. /* Clear any error flags */
  540. sdhsts = readl(host->ioaddr + SDHSTS);
  541. if (sdhsts & SDHSTS_ERROR_MASK)
  542. writel(sdhsts, host->ioaddr + SDHSTS);
  543. if ((cmd->flags & MMC_RSP_136) && (cmd->flags & MMC_RSP_BUSY)) {
  544. dev_err(dev, "unsupported response type!\n");
  545. cmd->error = -EINVAL;
  546. bcm2835_finish_request(host);
  547. return false;
  548. }
  549. bcm2835_prepare_data(host, cmd);
  550. writel(cmd->arg, host->ioaddr + SDARG);
  551. sdcmd = cmd->opcode & SDCMD_CMD_MASK;
  552. host->use_busy = false;
  553. if (!(cmd->flags & MMC_RSP_PRESENT)) {
  554. sdcmd |= SDCMD_NO_RESPONSE;
  555. } else {
  556. if (cmd->flags & MMC_RSP_136)
  557. sdcmd |= SDCMD_LONG_RESPONSE;
  558. if (cmd->flags & MMC_RSP_BUSY) {
  559. sdcmd |= SDCMD_BUSYWAIT;
  560. host->use_busy = true;
  561. }
  562. }
  563. if (cmd->data) {
  564. if (cmd->data->flags & MMC_DATA_WRITE)
  565. sdcmd |= SDCMD_WRITE_CMD;
  566. if (cmd->data->flags & MMC_DATA_READ)
  567. sdcmd |= SDCMD_READ_CMD;
  568. }
  569. writel(sdcmd | SDCMD_NEW_FLAG, host->ioaddr + SDCMD);
  570. return true;
  571. }
  572. static void bcm2835_transfer_complete(struct bcm2835_host *host)
  573. {
  574. struct mmc_data *data;
  575. WARN_ON(!host->data_complete);
  576. data = host->data;
  577. host->data = NULL;
  578. /* Need to send CMD12 if -
  579. * a) open-ended multiblock transfer (no CMD23)
  580. * b) error in multiblock transfer
  581. */
  582. if (host->mrq->stop && (data->error || !host->use_sbc)) {
  583. if (bcm2835_send_command(host, host->mrq->stop)) {
  584. /* No busy, so poll for completion */
  585. if (!host->use_busy)
  586. bcm2835_finish_command(host);
  587. }
  588. } else {
  589. bcm2835_wait_transfer_complete(host);
  590. bcm2835_finish_request(host);
  591. }
  592. }
  593. static void bcm2835_finish_data(struct bcm2835_host *host)
  594. {
  595. struct device *dev = &host->pdev->dev;
  596. struct mmc_data *data;
  597. data = host->data;
  598. host->hcfg &= ~(SDHCFG_DATA_IRPT_EN | SDHCFG_BLOCK_IRPT_EN);
  599. writel(host->hcfg, host->ioaddr + SDHCFG);
  600. data->bytes_xfered = data->error ? 0 : (data->blksz * data->blocks);
  601. host->data_complete = true;
  602. if (host->cmd) {
  603. /* Data managed to finish before the
  604. * command completed. Make sure we do
  605. * things in the proper order.
  606. */
  607. dev_dbg(dev, "Finished early - HSTS %08x\n",
  608. readl(host->ioaddr + SDHSTS));
  609. } else {
  610. bcm2835_transfer_complete(host);
  611. }
  612. }
  613. static void bcm2835_finish_command(struct bcm2835_host *host)
  614. {
  615. struct device *dev = &host->pdev->dev;
  616. struct mmc_command *cmd = host->cmd;
  617. u32 sdcmd;
  618. sdcmd = bcm2835_read_wait_sdcmd(host, 100);
  619. /* Check for errors */
  620. if (sdcmd & SDCMD_NEW_FLAG) {
  621. dev_err(dev, "command never completed.\n");
  622. bcm2835_dumpregs(host);
  623. host->cmd->error = -EIO;
  624. bcm2835_finish_request(host);
  625. return;
  626. } else if (sdcmd & SDCMD_FAIL_FLAG) {
  627. u32 sdhsts = readl(host->ioaddr + SDHSTS);
  628. /* Clear the errors */
  629. writel(SDHSTS_ERROR_MASK, host->ioaddr + SDHSTS);
  630. if (!(sdhsts & SDHSTS_CRC7_ERROR) ||
  631. (host->cmd->opcode != MMC_SEND_OP_COND)) {
  632. u32 edm, fsm;
  633. if (sdhsts & SDHSTS_CMD_TIME_OUT) {
  634. host->cmd->error = -ETIMEDOUT;
  635. } else {
  636. dev_err(dev, "unexpected command %d error\n",
  637. host->cmd->opcode);
  638. bcm2835_dumpregs(host);
  639. host->cmd->error = -EILSEQ;
  640. }
  641. edm = readl(host->ioaddr + SDEDM);
  642. fsm = edm & SDEDM_FSM_MASK;
  643. if (fsm == SDEDM_FSM_READWAIT ||
  644. fsm == SDEDM_FSM_WRITESTART1)
  645. /* Kick the FSM out of its wait */
  646. writel(edm | SDEDM_FORCE_DATA_MODE,
  647. host->ioaddr + SDEDM);
  648. bcm2835_finish_request(host);
  649. return;
  650. }
  651. }
  652. if (cmd->flags & MMC_RSP_PRESENT) {
  653. if (cmd->flags & MMC_RSP_136) {
  654. int i;
  655. for (i = 0; i < 4; i++) {
  656. cmd->resp[3 - i] =
  657. readl(host->ioaddr + SDRSP0 + i * 4);
  658. }
  659. } else {
  660. cmd->resp[0] = readl(host->ioaddr + SDRSP0);
  661. }
  662. }
  663. if (cmd == host->mrq->sbc) {
  664. /* Finished CMD23, now send actual command. */
  665. host->cmd = NULL;
  666. if (bcm2835_send_command(host, host->mrq->cmd)) {
  667. if (host->data && host->dma_desc)
  668. /* DMA transfer starts now, PIO starts
  669. * after irq
  670. */
  671. bcm2835_start_dma(host);
  672. if (!host->use_busy)
  673. bcm2835_finish_command(host);
  674. }
  675. } else if (cmd == host->mrq->stop) {
  676. /* Finished CMD12 */
  677. bcm2835_finish_request(host);
  678. } else {
  679. /* Processed actual command. */
  680. host->cmd = NULL;
  681. if (!host->data)
  682. bcm2835_finish_request(host);
  683. else if (host->data_complete)
  684. bcm2835_transfer_complete(host);
  685. }
  686. }
  687. static void bcm2835_timeout(struct work_struct *work)
  688. {
  689. struct delayed_work *d = to_delayed_work(work);
  690. struct bcm2835_host *host =
  691. container_of(d, struct bcm2835_host, timeout_work);
  692. struct device *dev = &host->pdev->dev;
  693. mutex_lock(&host->mutex);
  694. if (host->mrq) {
  695. dev_err(dev, "timeout waiting for hardware interrupt.\n");
  696. bcm2835_dumpregs(host);
  697. bcm2835_reset(mmc_from_priv(host));
  698. if (host->data) {
  699. host->data->error = -ETIMEDOUT;
  700. bcm2835_finish_data(host);
  701. } else {
  702. if (host->cmd)
  703. host->cmd->error = -ETIMEDOUT;
  704. else
  705. host->mrq->cmd->error = -ETIMEDOUT;
  706. bcm2835_finish_request(host);
  707. }
  708. }
  709. mutex_unlock(&host->mutex);
  710. }
  711. static bool bcm2835_check_cmd_error(struct bcm2835_host *host, u32 intmask)
  712. {
  713. struct device *dev = &host->pdev->dev;
  714. if (!(intmask & SDHSTS_ERROR_MASK))
  715. return false;
  716. if (!host->cmd)
  717. return true;
  718. dev_err(dev, "sdhost_busy_irq: intmask %08x\n", intmask);
  719. if (intmask & SDHSTS_CRC7_ERROR) {
  720. host->cmd->error = -EILSEQ;
  721. } else if (intmask & (SDHSTS_CRC16_ERROR |
  722. SDHSTS_FIFO_ERROR)) {
  723. if (host->mrq->data)
  724. host->mrq->data->error = -EILSEQ;
  725. else
  726. host->cmd->error = -EILSEQ;
  727. } else if (intmask & SDHSTS_REW_TIME_OUT) {
  728. if (host->mrq->data)
  729. host->mrq->data->error = -ETIMEDOUT;
  730. else
  731. host->cmd->error = -ETIMEDOUT;
  732. } else if (intmask & SDHSTS_CMD_TIME_OUT) {
  733. host->cmd->error = -ETIMEDOUT;
  734. }
  735. bcm2835_dumpregs(host);
  736. return true;
  737. }
  738. static void bcm2835_check_data_error(struct bcm2835_host *host, u32 intmask)
  739. {
  740. if (!host->data)
  741. return;
  742. if (intmask & (SDHSTS_CRC16_ERROR | SDHSTS_FIFO_ERROR))
  743. host->data->error = -EILSEQ;
  744. if (intmask & SDHSTS_REW_TIME_OUT)
  745. host->data->error = -ETIMEDOUT;
  746. }
  747. static void bcm2835_busy_irq(struct bcm2835_host *host)
  748. {
  749. if (WARN_ON(!host->cmd)) {
  750. bcm2835_dumpregs(host);
  751. return;
  752. }
  753. if (WARN_ON(!host->use_busy)) {
  754. bcm2835_dumpregs(host);
  755. return;
  756. }
  757. host->use_busy = false;
  758. bcm2835_finish_command(host);
  759. }
  760. static void bcm2835_data_irq(struct bcm2835_host *host, u32 intmask)
  761. {
  762. /* There are no dedicated data/space available interrupt
  763. * status bits, so it is necessary to use the single shared
  764. * data/space available FIFO status bits. It is therefore not
  765. * an error to get here when there is no data transfer in
  766. * progress.
  767. */
  768. if (!host->data)
  769. return;
  770. bcm2835_check_data_error(host, intmask);
  771. if (host->data->error)
  772. goto finished;
  773. if (host->data->flags & MMC_DATA_WRITE) {
  774. /* Use the block interrupt for writes after the first block */
  775. host->hcfg &= ~(SDHCFG_DATA_IRPT_EN);
  776. host->hcfg |= SDHCFG_BLOCK_IRPT_EN;
  777. writel(host->hcfg, host->ioaddr + SDHCFG);
  778. bcm2835_transfer_pio(host);
  779. } else {
  780. bcm2835_transfer_pio(host);
  781. host->blocks--;
  782. if ((host->blocks == 0) || host->data->error)
  783. goto finished;
  784. }
  785. return;
  786. finished:
  787. host->hcfg &= ~(SDHCFG_DATA_IRPT_EN | SDHCFG_BLOCK_IRPT_EN);
  788. writel(host->hcfg, host->ioaddr + SDHCFG);
  789. }
  790. static void bcm2835_data_threaded_irq(struct bcm2835_host *host)
  791. {
  792. if (!host->data)
  793. return;
  794. if ((host->blocks == 0) || host->data->error)
  795. bcm2835_finish_data(host);
  796. }
  797. static void bcm2835_block_irq(struct bcm2835_host *host)
  798. {
  799. if (WARN_ON(!host->data)) {
  800. bcm2835_dumpregs(host);
  801. return;
  802. }
  803. if (!host->dma_desc) {
  804. WARN_ON(!host->blocks);
  805. if (host->data->error || (--host->blocks == 0))
  806. bcm2835_finish_data(host);
  807. else
  808. bcm2835_transfer_pio(host);
  809. } else if (host->data->flags & MMC_DATA_WRITE) {
  810. bcm2835_finish_data(host);
  811. }
  812. }
  813. static irqreturn_t bcm2835_irq(int irq, void *dev_id)
  814. {
  815. irqreturn_t result = IRQ_NONE;
  816. struct bcm2835_host *host = dev_id;
  817. u32 intmask;
  818. spin_lock(&host->lock);
  819. intmask = readl(host->ioaddr + SDHSTS);
  820. writel(SDHSTS_BUSY_IRPT |
  821. SDHSTS_BLOCK_IRPT |
  822. SDHSTS_SDIO_IRPT |
  823. SDHSTS_DATA_FLAG,
  824. host->ioaddr + SDHSTS);
  825. if (intmask & SDHSTS_BLOCK_IRPT) {
  826. bcm2835_check_data_error(host, intmask);
  827. host->irq_block = true;
  828. result = IRQ_WAKE_THREAD;
  829. }
  830. if (intmask & SDHSTS_BUSY_IRPT) {
  831. if (!bcm2835_check_cmd_error(host, intmask)) {
  832. host->irq_busy = true;
  833. result = IRQ_WAKE_THREAD;
  834. } else {
  835. result = IRQ_HANDLED;
  836. }
  837. }
  838. /* There is no true data interrupt status bit, so it is
  839. * necessary to qualify the data flag with the interrupt
  840. * enable bit.
  841. */
  842. if ((intmask & SDHSTS_DATA_FLAG) &&
  843. (host->hcfg & SDHCFG_DATA_IRPT_EN)) {
  844. bcm2835_data_irq(host, intmask);
  845. host->irq_data = true;
  846. result = IRQ_WAKE_THREAD;
  847. }
  848. spin_unlock(&host->lock);
  849. return result;
  850. }
  851. static irqreturn_t bcm2835_threaded_irq(int irq, void *dev_id)
  852. {
  853. struct bcm2835_host *host = dev_id;
  854. unsigned long flags;
  855. bool block, busy, data;
  856. spin_lock_irqsave(&host->lock, flags);
  857. block = host->irq_block;
  858. busy = host->irq_busy;
  859. data = host->irq_data;
  860. host->irq_block = false;
  861. host->irq_busy = false;
  862. host->irq_data = false;
  863. spin_unlock_irqrestore(&host->lock, flags);
  864. mutex_lock(&host->mutex);
  865. if (block)
  866. bcm2835_block_irq(host);
  867. if (busy)
  868. bcm2835_busy_irq(host);
  869. if (data)
  870. bcm2835_data_threaded_irq(host);
  871. mutex_unlock(&host->mutex);
  872. return IRQ_HANDLED;
  873. }
  874. static void bcm2835_dma_complete_work(struct work_struct *work)
  875. {
  876. struct bcm2835_host *host =
  877. container_of(work, struct bcm2835_host, dma_work);
  878. struct mmc_data *data;
  879. mutex_lock(&host->mutex);
  880. data = host->data;
  881. if (host->dma_chan) {
  882. dma_unmap_sg(host->dma_chan->device->dev,
  883. data->sg, data->sg_len,
  884. host->dma_dir);
  885. host->dma_chan = NULL;
  886. }
  887. if (host->drain_words) {
  888. void *page;
  889. u32 *buf;
  890. if (host->drain_offset & PAGE_MASK) {
  891. host->drain_page += host->drain_offset >> PAGE_SHIFT;
  892. host->drain_offset &= ~PAGE_MASK;
  893. }
  894. page = kmap_local_page(host->drain_page);
  895. buf = page + host->drain_offset;
  896. while (host->drain_words) {
  897. u32 edm = readl(host->ioaddr + SDEDM);
  898. if ((edm >> 4) & 0x1f)
  899. *(buf++) = readl(host->ioaddr + SDDATA);
  900. host->drain_words--;
  901. }
  902. kunmap_local(page);
  903. }
  904. bcm2835_finish_data(host);
  905. mutex_unlock(&host->mutex);
  906. }
  907. static void bcm2835_set_clock(struct bcm2835_host *host, unsigned int clock)
  908. {
  909. struct mmc_host *mmc = mmc_from_priv(host);
  910. int div;
  911. /* The SDCDIV register has 11 bits, and holds (div - 2). But
  912. * in data mode the max is 50MHz wihout a minimum, and only
  913. * the bottom 3 bits are used. Since the switch over is
  914. * automatic (unless we have marked the card as slow...),
  915. * chosen values have to make sense in both modes. Ident mode
  916. * must be 100-400KHz, so can range check the requested
  917. * clock. CMD15 must be used to return to data mode, so this
  918. * can be monitored.
  919. *
  920. * clock 250MHz -> 0->125MHz, 1->83.3MHz, 2->62.5MHz, 3->50.0MHz
  921. * 4->41.7MHz, 5->35.7MHz, 6->31.3MHz, 7->27.8MHz
  922. *
  923. * 623->400KHz/27.8MHz
  924. * reset value (507)->491159/50MHz
  925. *
  926. * BUT, the 3-bit clock divisor in data mode is too small if
  927. * the core clock is higher than 250MHz, so instead use the
  928. * SLOW_CARD configuration bit to force the use of the ident
  929. * clock divisor at all times.
  930. */
  931. if (clock < 100000) {
  932. /* Can't stop the clock, but make it as slow as possible
  933. * to show willing
  934. */
  935. host->cdiv = SDCDIV_MAX_CDIV;
  936. writel(host->cdiv, host->ioaddr + SDCDIV);
  937. return;
  938. }
  939. div = host->max_clk / clock;
  940. if (div < 2)
  941. div = 2;
  942. if ((host->max_clk / div) > clock)
  943. div++;
  944. div -= 2;
  945. if (div > SDCDIV_MAX_CDIV)
  946. div = SDCDIV_MAX_CDIV;
  947. clock = host->max_clk / (div + 2);
  948. mmc->actual_clock = clock;
  949. /* Calibrate some delays */
  950. host->ns_per_fifo_word = (1000000000 / clock) *
  951. ((mmc->caps & MMC_CAP_4_BIT_DATA) ? 8 : 32);
  952. host->cdiv = div;
  953. writel(host->cdiv, host->ioaddr + SDCDIV);
  954. /* Set the timeout to 500ms */
  955. writel(mmc->actual_clock / 2, host->ioaddr + SDTOUT);
  956. }
  957. static void bcm2835_request(struct mmc_host *mmc, struct mmc_request *mrq)
  958. {
  959. struct bcm2835_host *host = mmc_priv(mmc);
  960. struct device *dev = &host->pdev->dev;
  961. u32 edm, fsm;
  962. /* Reset the error statuses in case this is a retry */
  963. if (mrq->sbc)
  964. mrq->sbc->error = 0;
  965. if (mrq->cmd)
  966. mrq->cmd->error = 0;
  967. if (mrq->data)
  968. mrq->data->error = 0;
  969. if (mrq->stop)
  970. mrq->stop->error = 0;
  971. if (mrq->data && !is_power_of_2(mrq->data->blksz)) {
  972. dev_err(dev, "unsupported block size (%d bytes)\n",
  973. mrq->data->blksz);
  974. if (mrq->cmd)
  975. mrq->cmd->error = -EINVAL;
  976. mmc_request_done(mmc, mrq);
  977. return;
  978. }
  979. mutex_lock(&host->mutex);
  980. WARN_ON(host->mrq);
  981. host->mrq = mrq;
  982. edm = readl(host->ioaddr + SDEDM);
  983. fsm = edm & SDEDM_FSM_MASK;
  984. if ((fsm != SDEDM_FSM_IDENTMODE) &&
  985. (fsm != SDEDM_FSM_DATAMODE)) {
  986. dev_err(dev, "previous command (%d) not complete (EDM %08x)\n",
  987. readl(host->ioaddr + SDCMD) & SDCMD_CMD_MASK,
  988. edm);
  989. bcm2835_dumpregs(host);
  990. if (mrq->cmd)
  991. mrq->cmd->error = -EILSEQ;
  992. bcm2835_finish_request(host);
  993. mutex_unlock(&host->mutex);
  994. return;
  995. }
  996. if (host->use_dma && mrq->data && (mrq->data->blocks > PIO_THRESHOLD))
  997. bcm2835_prepare_dma(host, mrq->data);
  998. host->use_sbc = !!mrq->sbc && host->mrq->data &&
  999. (host->mrq->data->flags & MMC_DATA_READ);
  1000. if (host->use_sbc) {
  1001. if (bcm2835_send_command(host, mrq->sbc)) {
  1002. if (!host->use_busy)
  1003. bcm2835_finish_command(host);
  1004. }
  1005. } else if (mrq->cmd && bcm2835_send_command(host, mrq->cmd)) {
  1006. if (host->data && host->dma_desc) {
  1007. /* DMA transfer starts now, PIO starts after irq */
  1008. bcm2835_start_dma(host);
  1009. }
  1010. if (!host->use_busy)
  1011. bcm2835_finish_command(host);
  1012. }
  1013. mutex_unlock(&host->mutex);
  1014. }
  1015. static void bcm2835_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
  1016. {
  1017. struct bcm2835_host *host = mmc_priv(mmc);
  1018. mutex_lock(&host->mutex);
  1019. if (!ios->clock || ios->clock != host->clock) {
  1020. bcm2835_set_clock(host, ios->clock);
  1021. host->clock = ios->clock;
  1022. }
  1023. /* set bus width */
  1024. host->hcfg &= ~SDHCFG_WIDE_EXT_BUS;
  1025. if (ios->bus_width == MMC_BUS_WIDTH_4)
  1026. host->hcfg |= SDHCFG_WIDE_EXT_BUS;
  1027. host->hcfg |= SDHCFG_WIDE_INT_BUS;
  1028. /* Disable clever clock switching, to cope with fast core clocks */
  1029. host->hcfg |= SDHCFG_SLOW_CARD;
  1030. writel(host->hcfg, host->ioaddr + SDHCFG);
  1031. mutex_unlock(&host->mutex);
  1032. }
  1033. static const struct mmc_host_ops bcm2835_ops = {
  1034. .request = bcm2835_request,
  1035. .set_ios = bcm2835_set_ios,
  1036. .card_hw_reset = bcm2835_reset,
  1037. };
  1038. static int bcm2835_add_host(struct bcm2835_host *host)
  1039. {
  1040. struct mmc_host *mmc = mmc_from_priv(host);
  1041. struct device *dev = &host->pdev->dev;
  1042. char pio_limit_string[20];
  1043. int ret;
  1044. if (!mmc->f_max || mmc->f_max > host->max_clk)
  1045. mmc->f_max = host->max_clk;
  1046. mmc->f_min = host->max_clk / SDCDIV_MAX_CDIV;
  1047. mmc->max_busy_timeout = ~0 / (mmc->f_max / 1000);
  1048. dev_dbg(dev, "f_max %d, f_min %d, max_busy_timeout %d\n",
  1049. mmc->f_max, mmc->f_min, mmc->max_busy_timeout);
  1050. /* host controller capabilities */
  1051. mmc->caps |= MMC_CAP_SD_HIGHSPEED | MMC_CAP_MMC_HIGHSPEED |
  1052. MMC_CAP_NEEDS_POLL | MMC_CAP_HW_RESET | MMC_CAP_CMD23;
  1053. spin_lock_init(&host->lock);
  1054. mutex_init(&host->mutex);
  1055. if (!host->dma_chan_rxtx) {
  1056. dev_warn(dev, "unable to initialise DMA channel. Falling back to PIO\n");
  1057. host->use_dma = false;
  1058. } else {
  1059. host->use_dma = true;
  1060. host->dma_cfg_tx.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  1061. host->dma_cfg_tx.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  1062. host->dma_cfg_tx.direction = DMA_MEM_TO_DEV;
  1063. host->dma_cfg_tx.src_addr = 0;
  1064. host->dma_cfg_tx.dst_addr = host->phys_addr + SDDATA;
  1065. host->dma_cfg_rx.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  1066. host->dma_cfg_rx.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  1067. host->dma_cfg_rx.direction = DMA_DEV_TO_MEM;
  1068. host->dma_cfg_rx.src_addr = host->phys_addr + SDDATA;
  1069. host->dma_cfg_rx.dst_addr = 0;
  1070. if (dmaengine_slave_config(host->dma_chan_rxtx,
  1071. &host->dma_cfg_tx) != 0 ||
  1072. dmaengine_slave_config(host->dma_chan_rxtx,
  1073. &host->dma_cfg_rx) != 0)
  1074. host->use_dma = false;
  1075. }
  1076. mmc->max_segs = 128;
  1077. mmc->max_req_size = min_t(size_t, 524288, dma_max_mapping_size(dev));
  1078. mmc->max_seg_size = mmc->max_req_size;
  1079. mmc->max_blk_size = 1024;
  1080. mmc->max_blk_count = 65535;
  1081. /* report supported voltage ranges */
  1082. mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34;
  1083. INIT_WORK(&host->dma_work, bcm2835_dma_complete_work);
  1084. INIT_DELAYED_WORK(&host->timeout_work, bcm2835_timeout);
  1085. /* Set interrupt enables */
  1086. host->hcfg = SDHCFG_BUSY_IRPT_EN;
  1087. bcm2835_reset_internal(host);
  1088. ret = request_threaded_irq(host->irq, bcm2835_irq,
  1089. bcm2835_threaded_irq,
  1090. 0, mmc_hostname(mmc), host);
  1091. if (ret) {
  1092. dev_err(dev, "failed to request IRQ %d: %d\n", host->irq, ret);
  1093. return ret;
  1094. }
  1095. ret = mmc_add_host(mmc);
  1096. if (ret) {
  1097. free_irq(host->irq, host);
  1098. return ret;
  1099. }
  1100. pio_limit_string[0] = '\0';
  1101. if (host->use_dma && (PIO_THRESHOLD > 0))
  1102. sprintf(pio_limit_string, " (>%d)", PIO_THRESHOLD);
  1103. dev_info(dev, "loaded - DMA %s%s\n",
  1104. host->use_dma ? "enabled" : "disabled", pio_limit_string);
  1105. return 0;
  1106. }
  1107. static int bcm2835_probe(struct platform_device *pdev)
  1108. {
  1109. struct device *dev = &pdev->dev;
  1110. struct clk *clk;
  1111. struct bcm2835_host *host;
  1112. struct mmc_host *mmc;
  1113. const __be32 *regaddr_p;
  1114. int ret;
  1115. dev_dbg(dev, "%s\n", __func__);
  1116. mmc = mmc_alloc_host(sizeof(*host), dev);
  1117. if (!mmc)
  1118. return -ENOMEM;
  1119. mmc->ops = &bcm2835_ops;
  1120. host = mmc_priv(mmc);
  1121. host->pdev = pdev;
  1122. spin_lock_init(&host->lock);
  1123. host->ioaddr = devm_platform_ioremap_resource(pdev, 0);
  1124. if (IS_ERR(host->ioaddr)) {
  1125. ret = PTR_ERR(host->ioaddr);
  1126. goto err;
  1127. }
  1128. /* Parse OF address directly to get the physical address for
  1129. * DMA to our registers.
  1130. */
  1131. regaddr_p = of_get_address(pdev->dev.of_node, 0, NULL, NULL);
  1132. if (!regaddr_p) {
  1133. dev_err(dev, "Can't get phys address\n");
  1134. ret = -EINVAL;
  1135. goto err;
  1136. }
  1137. host->phys_addr = be32_to_cpup(regaddr_p);
  1138. host->dma_chan = NULL;
  1139. host->dma_desc = NULL;
  1140. host->dma_chan_rxtx = dma_request_chan(dev, "rx-tx");
  1141. if (IS_ERR(host->dma_chan_rxtx)) {
  1142. ret = PTR_ERR(host->dma_chan_rxtx);
  1143. host->dma_chan_rxtx = NULL;
  1144. if (ret == -EPROBE_DEFER)
  1145. goto err;
  1146. /* Ignore errors to fall back to PIO mode */
  1147. }
  1148. clk = devm_clk_get(dev, NULL);
  1149. if (IS_ERR(clk)) {
  1150. ret = dev_err_probe(dev, PTR_ERR(clk), "could not get clk\n");
  1151. goto err;
  1152. }
  1153. host->max_clk = clk_get_rate(clk);
  1154. host->irq = platform_get_irq(pdev, 0);
  1155. if (host->irq < 0) {
  1156. ret = host->irq;
  1157. goto err;
  1158. }
  1159. ret = mmc_of_parse(mmc);
  1160. if (ret)
  1161. goto err;
  1162. ret = bcm2835_add_host(host);
  1163. if (ret)
  1164. goto err;
  1165. platform_set_drvdata(pdev, host);
  1166. dev_dbg(dev, "%s -> OK\n", __func__);
  1167. return 0;
  1168. err:
  1169. dev_dbg(dev, "%s -> err %d\n", __func__, ret);
  1170. if (host->dma_chan_rxtx)
  1171. dma_release_channel(host->dma_chan_rxtx);
  1172. mmc_free_host(mmc);
  1173. return ret;
  1174. }
  1175. static void bcm2835_remove(struct platform_device *pdev)
  1176. {
  1177. struct bcm2835_host *host = platform_get_drvdata(pdev);
  1178. struct mmc_host *mmc = mmc_from_priv(host);
  1179. mmc_remove_host(mmc);
  1180. writel(SDVDD_POWER_OFF, host->ioaddr + SDVDD);
  1181. free_irq(host->irq, host);
  1182. cancel_work_sync(&host->dma_work);
  1183. cancel_delayed_work_sync(&host->timeout_work);
  1184. if (host->dma_chan_rxtx)
  1185. dma_release_channel(host->dma_chan_rxtx);
  1186. mmc_free_host(mmc);
  1187. }
  1188. static const struct of_device_id bcm2835_match[] = {
  1189. { .compatible = "brcm,bcm2835-sdhost" },
  1190. { }
  1191. };
  1192. MODULE_DEVICE_TABLE(of, bcm2835_match);
  1193. static struct platform_driver bcm2835_driver = {
  1194. .probe = bcm2835_probe,
  1195. .remove_new = bcm2835_remove,
  1196. .driver = {
  1197. .name = "sdhost-bcm2835",
  1198. .probe_type = PROBE_PREFER_ASYNCHRONOUS,
  1199. .of_match_table = bcm2835_match,
  1200. },
  1201. };
  1202. module_platform_driver(bcm2835_driver);
  1203. MODULE_ALIAS("platform:sdhost-bcm2835");
  1204. MODULE_DESCRIPTION("BCM2835 SDHost driver");
  1205. MODULE_LICENSE("GPL v2");
  1206. MODULE_AUTHOR("Phil Elwell");