bcm2835.c 37 KB

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