0032-MMC-added-alternative-MMC-driver.patch 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691
  1. From ded251905fe3ebb582fd87fecf3454834333afa1 Mon Sep 17 00:00:00 2001
  2. From: gellert <gellert@raspberrypi.org>
  3. Date: Fri, 15 Aug 2014 16:35:06 +0100
  4. Subject: [PATCH 032/381] MMC: added alternative MMC driver
  5. MIME-Version: 1.0
  6. Content-Type: text/plain; charset=UTF-8
  7. Content-Transfer-Encoding: 8bit
  8. mmc: Disable CMD23 transfers on all cards
  9. Pending wire-level investigation of these types of transfers
  10. and associated errors on bcm2835-mmc, disable for now. Fallback of
  11. CMD18/CMD25 transfers will be used automatically by the MMC layer.
  12. Reported/Tested-by: Gellert Weisz <gellert@raspberrypi.org>
  13. mmc: bcm2835-mmc: enable DT support for all architectures
  14. Both ARCH_BCM2835 and ARCH_BCM270x are built with OF now.
  15. Enable Device Tree support for all architectures.
  16. Signed-off-by: Noralf Trønnes <noralf@tronnes.org>
  17. mmc: bcm2835-mmc: fix probe error handling
  18. Probe error handling is broken in several places.
  19. Simplify error handling by using device managed functions.
  20. Replace pr_{err,info} with dev_{err,info}.
  21. Signed-off-by: Noralf Trønnes <noralf@tronnes.org>
  22. bcm2835-mmc: Add locks when accessing sdhost registers
  23. bcm2835-mmc: Add range of debug options for slowing things down
  24. bcm2835-mmc: Add option to disable some delays
  25. bcm2835-mmc: Add option to disable MMC_QUIRK_BLK_NO_CMD23
  26. bcm2835-mmc: Default to disabling MMC_QUIRK_BLK_NO_CMD23
  27. bcm2835-mmc: Adding overclocking option
  28. Allow a different clock speed to be substitued for a requested 50MHz.
  29. This option is exposed using the "overclock_50" DT parameter.
  30. Note that the mmc interface is restricted to EVEN integer divisions of
  31. 250MHz, and the highest sensible option is 63 (250/4 = 62.5), the
  32. next being 125 (250/2) which is much too high.
  33. Use at your own risk.
  34. bcm2835-mmc: Round up the overclock, so 62 works for 62.5Mhz
  35. Also only warn once for each overclock setting.
  36. mmc: bcm2835-mmc: Make available on ARCH_BCM2835
  37. Make the bcm2835-mmc driver available for use on ARCH_BCM2835.
  38. Signed-off-by: Noralf Trønnes <noralf@tronnes.org>
  39. BCM270x_DT: add bcm2835-mmc entry
  40. Add Device Tree entry for bcm2835-mmc.
  41. In non-DT mode, don't add the device in the board file.
  42. Signed-off-by: Noralf Trønnes <noralf@tronnes.org>
  43. bcm2835-mmc: Don't overwrite MMC capabilities from DT
  44. ---
  45. drivers/mmc/core/quirks.c | 6 +
  46. drivers/mmc/host/Kconfig | 29 +
  47. drivers/mmc/host/Makefile | 1 +
  48. drivers/mmc/host/bcm2835-mmc.c | 1542 ++++++++++++++++++++++++++++++++++++++++
  49. 4 files changed, 1578 insertions(+)
  50. create mode 100644 drivers/mmc/host/bcm2835-mmc.c
  51. --- a/drivers/mmc/core/quirks.c
  52. +++ b/drivers/mmc/core/quirks.c
  53. @@ -53,6 +53,7 @@ static const struct mmc_fixup mmc_fixup_
  54. void mmc_fixup_device(struct mmc_card *card, const struct mmc_fixup *table)
  55. {
  56. + extern unsigned mmc_debug;
  57. const struct mmc_fixup *f;
  58. u64 rev = cid_rev_card(card);
  59. @@ -77,5 +78,10 @@ void mmc_fixup_device(struct mmc_card *c
  60. f->vendor_fixup(card, f->data);
  61. }
  62. }
  63. + /* SDHCI on BCM2708 - bug causes a certain sequence of CMD23 operations to fail.
  64. + * Disable this flag for all cards (fall-back to CMD25/CMD18 multi-block transfers).
  65. + */
  66. + if (mmc_debug & (1<<13))
  67. + card->quirks |= MMC_QUIRK_BLK_NO_CMD23;
  68. }
  69. EXPORT_SYMBOL(mmc_fixup_device);
  70. --- a/drivers/mmc/host/Kconfig
  71. +++ b/drivers/mmc/host/Kconfig
  72. @@ -4,6 +4,35 @@
  73. comment "MMC/SD/SDIO Host Controller Drivers"
  74. +config MMC_BCM2835
  75. + tristate "MMC support on BCM2835"
  76. + depends on MACH_BCM2708 || MACH_BCM2709 || ARCH_BCM2835
  77. + help
  78. + This selects the MMC Interface on BCM2835.
  79. +
  80. + If you have a controller with this interface, say Y or M here.
  81. +
  82. + If unsure, say N.
  83. +
  84. +config MMC_BCM2835_DMA
  85. + bool "DMA support on BCM2835 Arasan controller"
  86. + depends on MMC_BCM2835
  87. + help
  88. + Enable DMA support on the Arasan SDHCI controller in Broadcom 2708
  89. + based chips.
  90. +
  91. + If unsure, say N.
  92. +
  93. +config MMC_BCM2835_PIO_DMA_BARRIER
  94. + int "Block count limit for PIO transfers"
  95. + depends on MMC_BCM2835 && MMC_BCM2835_DMA
  96. + range 0 256
  97. + default 2
  98. + help
  99. + The inclusive limit in bytes under which PIO will be used instead of DMA
  100. +
  101. + If unsure, say 2 here.
  102. +
  103. config MMC_ARMMMCI
  104. tristate "ARM AMBA Multimedia Card Interface support"
  105. depends on ARM_AMBA
  106. --- a/drivers/mmc/host/Makefile
  107. +++ b/drivers/mmc/host/Makefile
  108. @@ -18,6 +18,7 @@ obj-$(CONFIG_MMC_SDHCI_S3C) += sdhci-s3c
  109. obj-$(CONFIG_MMC_SDHCI_SIRF) += sdhci-sirf.o
  110. obj-$(CONFIG_MMC_SDHCI_F_SDH30) += sdhci_f_sdh30.o
  111. obj-$(CONFIG_MMC_SDHCI_SPEAR) += sdhci-spear.o
  112. +obj-$(CONFIG_MMC_BCM2835) += bcm2835-mmc.o
  113. obj-$(CONFIG_MMC_WBSD) += wbsd.o
  114. obj-$(CONFIG_MMC_AU1X) += au1xmmc.o
  115. obj-$(CONFIG_MMC_MTK) += mtk-sd.o
  116. --- /dev/null
  117. +++ b/drivers/mmc/host/bcm2835-mmc.c
  118. @@ -0,0 +1,1542 @@
  119. +/*
  120. + * BCM2835 MMC host driver.
  121. + *
  122. + * Author: Gellert Weisz <gellert@raspberrypi.org>
  123. + * Copyright 2014
  124. + *
  125. + * Based on
  126. + * sdhci-bcm2708.c by Broadcom
  127. + * sdhci-bcm2835.c by Stephen Warren and Oleksandr Tymoshenko
  128. + * sdhci.c and sdhci-pci.c by Pierre Ossman
  129. + *
  130. + * This program is free software; you can redistribute it and/or modify it
  131. + * under the terms and conditions of the GNU General Public License,
  132. + * version 2, as published by the Free Software Foundation.
  133. + *
  134. + * This program is distributed in the hope it will be useful, but WITHOUT
  135. + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  136. + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  137. + * more details.
  138. + *
  139. + * You should have received a copy of the GNU General Public License
  140. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  141. + */
  142. +
  143. +#include <linux/delay.h>
  144. +#include <linux/module.h>
  145. +#include <linux/io.h>
  146. +#include <linux/mmc/mmc.h>
  147. +#include <linux/mmc/host.h>
  148. +#include <linux/mmc/sd.h>
  149. +#include <linux/scatterlist.h>
  150. +#include <linux/of_address.h>
  151. +#include <linux/of_irq.h>
  152. +#include <linux/clk.h>
  153. +#include <linux/platform_device.h>
  154. +#include <linux/err.h>
  155. +#include <linux/blkdev.h>
  156. +#include <linux/dmaengine.h>
  157. +#include <linux/dma-mapping.h>
  158. +#include <linux/of_dma.h>
  159. +
  160. +#include "sdhci.h"
  161. +
  162. +
  163. +#define DRIVER_NAME "mmc-bcm2835"
  164. +
  165. +#define DBG(f, x...) \
  166. +pr_debug(DRIVER_NAME " [%s()]: " f, __func__, ## x)
  167. +
  168. +#ifndef CONFIG_MMC_BCM2835_DMA
  169. + #define FORCE_PIO
  170. +#endif
  171. +
  172. +
  173. +/* the inclusive limit in bytes under which PIO will be used instead of DMA */
  174. +#ifdef CONFIG_MMC_BCM2835_PIO_DMA_BARRIER
  175. +#define PIO_DMA_BARRIER CONFIG_MMC_BCM2835_PIO_DMA_BARRIER
  176. +#else
  177. +#define PIO_DMA_BARRIER 00
  178. +#endif
  179. +
  180. +#define MIN_FREQ 400000
  181. +#define TIMEOUT_VAL 0xE
  182. +#define BCM2835_SDHCI_WRITE_DELAY(f) (((2 * 1000000) / f) + 1)
  183. +
  184. +
  185. +unsigned mmc_debug;
  186. +unsigned mmc_debug2;
  187. +
  188. +struct bcm2835_host {
  189. + spinlock_t lock;
  190. +
  191. + void __iomem *ioaddr;
  192. + u32 bus_addr;
  193. +
  194. + struct mmc_host *mmc;
  195. +
  196. + u32 timeout;
  197. +
  198. + int clock; /* Current clock speed */
  199. + u8 pwr; /* Current voltage */
  200. +
  201. + unsigned int max_clk; /* Max possible freq */
  202. + unsigned int timeout_clk; /* Timeout freq (KHz) */
  203. + unsigned int clk_mul; /* Clock Muliplier value */
  204. +
  205. + struct tasklet_struct finish_tasklet; /* Tasklet structures */
  206. +
  207. + struct timer_list timer; /* Timer for timeouts */
  208. +
  209. + struct sg_mapping_iter sg_miter; /* SG state for PIO */
  210. + unsigned int blocks; /* remaining PIO blocks */
  211. +
  212. + int irq; /* Device IRQ */
  213. +
  214. +
  215. + u32 ier; /* cached registers */
  216. +
  217. + struct mmc_request *mrq; /* Current request */
  218. + struct mmc_command *cmd; /* Current command */
  219. + struct mmc_data *data; /* Current data request */
  220. + unsigned int data_early:1; /* Data finished before cmd */
  221. +
  222. + wait_queue_head_t buf_ready_int; /* Waitqueue for Buffer Read Ready interrupt */
  223. +
  224. + u32 thread_isr;
  225. +
  226. + u32 shadow;
  227. +
  228. + /*DMA part*/
  229. + struct dma_chan *dma_chan_rx; /* DMA channel for reads */
  230. + struct dma_chan *dma_chan_tx; /* DMA channel for writes */
  231. + struct dma_async_tx_descriptor *tx_desc; /* descriptor */
  232. +
  233. + bool have_dma;
  234. + bool use_dma;
  235. + /*end of DMA part*/
  236. +
  237. + int max_delay; /* maximum length of time spent waiting */
  238. +
  239. + int flags; /* Host attributes */
  240. +#define SDHCI_REQ_USE_DMA (1<<2) /* Use DMA for this req. */
  241. +#define SDHCI_DEVICE_DEAD (1<<3) /* Device unresponsive */
  242. +#define SDHCI_AUTO_CMD12 (1<<6) /* Auto CMD12 support */
  243. +#define SDHCI_AUTO_CMD23 (1<<7) /* Auto CMD23 support */
  244. +#define SDHCI_SDIO_IRQ_ENABLED (1<<9) /* SDIO irq enabled */
  245. +
  246. + u32 overclock_50; /* frequency to use when 50MHz is requested (in MHz) */
  247. + u32 max_overclock; /* Highest reported */
  248. +};
  249. +
  250. +
  251. +static inline void bcm2835_mmc_writel(struct bcm2835_host *host, u32 val, int reg, int from)
  252. +{
  253. + unsigned delay;
  254. + lockdep_assert_held_once(&host->lock);
  255. + writel(val, host->ioaddr + reg);
  256. + udelay(BCM2835_SDHCI_WRITE_DELAY(max(host->clock, MIN_FREQ)));
  257. +
  258. + delay = ((mmc_debug >> 16) & 0xf) << ((mmc_debug >> 20) & 0xf);
  259. + if (delay && !((1<<from) & mmc_debug2))
  260. + udelay(delay);
  261. +}
  262. +
  263. +static inline void mmc_raw_writel(struct bcm2835_host *host, u32 val, int reg)
  264. +{
  265. + unsigned delay;
  266. + lockdep_assert_held_once(&host->lock);
  267. + writel(val, host->ioaddr + reg);
  268. +
  269. + delay = ((mmc_debug >> 24) & 0xf) << ((mmc_debug >> 28) & 0xf);
  270. + if (delay)
  271. + udelay(delay);
  272. +}
  273. +
  274. +static inline u32 bcm2835_mmc_readl(struct bcm2835_host *host, int reg)
  275. +{
  276. + lockdep_assert_held_once(&host->lock);
  277. + return readl(host->ioaddr + reg);
  278. +}
  279. +
  280. +static inline void bcm2835_mmc_writew(struct bcm2835_host *host, u16 val, int reg)
  281. +{
  282. + u32 oldval = (reg == SDHCI_COMMAND) ? host->shadow :
  283. + bcm2835_mmc_readl(host, reg & ~3);
  284. + u32 word_num = (reg >> 1) & 1;
  285. + u32 word_shift = word_num * 16;
  286. + u32 mask = 0xffff << word_shift;
  287. + u32 newval = (oldval & ~mask) | (val << word_shift);
  288. +
  289. + if (reg == SDHCI_TRANSFER_MODE)
  290. + host->shadow = newval;
  291. + else
  292. + bcm2835_mmc_writel(host, newval, reg & ~3, 0);
  293. +
  294. +}
  295. +
  296. +static inline void bcm2835_mmc_writeb(struct bcm2835_host *host, u8 val, int reg)
  297. +{
  298. + u32 oldval = bcm2835_mmc_readl(host, reg & ~3);
  299. + u32 byte_num = reg & 3;
  300. + u32 byte_shift = byte_num * 8;
  301. + u32 mask = 0xff << byte_shift;
  302. + u32 newval = (oldval & ~mask) | (val << byte_shift);
  303. +
  304. + bcm2835_mmc_writel(host, newval, reg & ~3, 1);
  305. +}
  306. +
  307. +
  308. +static inline u16 bcm2835_mmc_readw(struct bcm2835_host *host, int reg)
  309. +{
  310. + u32 val = bcm2835_mmc_readl(host, (reg & ~3));
  311. + u32 word_num = (reg >> 1) & 1;
  312. + u32 word_shift = word_num * 16;
  313. + u32 word = (val >> word_shift) & 0xffff;
  314. +
  315. + return word;
  316. +}
  317. +
  318. +static inline u8 bcm2835_mmc_readb(struct bcm2835_host *host, int reg)
  319. +{
  320. + u32 val = bcm2835_mmc_readl(host, (reg & ~3));
  321. + u32 byte_num = reg & 3;
  322. + u32 byte_shift = byte_num * 8;
  323. + u32 byte = (val >> byte_shift) & 0xff;
  324. +
  325. + return byte;
  326. +}
  327. +
  328. +static void bcm2835_mmc_unsignal_irqs(struct bcm2835_host *host, u32 clear)
  329. +{
  330. + u32 ier;
  331. +
  332. + ier = bcm2835_mmc_readl(host, SDHCI_SIGNAL_ENABLE);
  333. + ier &= ~clear;
  334. + /* change which requests generate IRQs - makes no difference to
  335. + the content of SDHCI_INT_STATUS, or the need to acknowledge IRQs */
  336. + bcm2835_mmc_writel(host, ier, SDHCI_SIGNAL_ENABLE, 2);
  337. +}
  338. +
  339. +
  340. +static void bcm2835_mmc_dumpregs(struct bcm2835_host *host)
  341. +{
  342. + pr_debug(DRIVER_NAME ": =========== REGISTER DUMP (%s)===========\n",
  343. + mmc_hostname(host->mmc));
  344. +
  345. + pr_debug(DRIVER_NAME ": Sys addr: 0x%08x | Version: 0x%08x\n",
  346. + bcm2835_mmc_readl(host, SDHCI_DMA_ADDRESS),
  347. + bcm2835_mmc_readw(host, SDHCI_HOST_VERSION));
  348. + pr_debug(DRIVER_NAME ": Blk size: 0x%08x | Blk cnt: 0x%08x\n",
  349. + bcm2835_mmc_readw(host, SDHCI_BLOCK_SIZE),
  350. + bcm2835_mmc_readw(host, SDHCI_BLOCK_COUNT));
  351. + pr_debug(DRIVER_NAME ": Argument: 0x%08x | Trn mode: 0x%08x\n",
  352. + bcm2835_mmc_readl(host, SDHCI_ARGUMENT),
  353. + bcm2835_mmc_readw(host, SDHCI_TRANSFER_MODE));
  354. + pr_debug(DRIVER_NAME ": Present: 0x%08x | Host ctl: 0x%08x\n",
  355. + bcm2835_mmc_readl(host, SDHCI_PRESENT_STATE),
  356. + bcm2835_mmc_readb(host, SDHCI_HOST_CONTROL));
  357. + pr_debug(DRIVER_NAME ": Power: 0x%08x | Blk gap: 0x%08x\n",
  358. + bcm2835_mmc_readb(host, SDHCI_POWER_CONTROL),
  359. + bcm2835_mmc_readb(host, SDHCI_BLOCK_GAP_CONTROL));
  360. + pr_debug(DRIVER_NAME ": Wake-up: 0x%08x | Clock: 0x%08x\n",
  361. + bcm2835_mmc_readb(host, SDHCI_WAKE_UP_CONTROL),
  362. + bcm2835_mmc_readw(host, SDHCI_CLOCK_CONTROL));
  363. + pr_debug(DRIVER_NAME ": Timeout: 0x%08x | Int stat: 0x%08x\n",
  364. + bcm2835_mmc_readb(host, SDHCI_TIMEOUT_CONTROL),
  365. + bcm2835_mmc_readl(host, SDHCI_INT_STATUS));
  366. + pr_debug(DRIVER_NAME ": Int enab: 0x%08x | Sig enab: 0x%08x\n",
  367. + bcm2835_mmc_readl(host, SDHCI_INT_ENABLE),
  368. + bcm2835_mmc_readl(host, SDHCI_SIGNAL_ENABLE));
  369. + pr_debug(DRIVER_NAME ": AC12 err: 0x%08x | Slot int: 0x%08x\n",
  370. + bcm2835_mmc_readw(host, SDHCI_ACMD12_ERR),
  371. + bcm2835_mmc_readw(host, SDHCI_SLOT_INT_STATUS));
  372. + pr_debug(DRIVER_NAME ": Caps: 0x%08x | Caps_1: 0x%08x\n",
  373. + bcm2835_mmc_readl(host, SDHCI_CAPABILITIES),
  374. + bcm2835_mmc_readl(host, SDHCI_CAPABILITIES_1));
  375. + pr_debug(DRIVER_NAME ": Cmd: 0x%08x | Max curr: 0x%08x\n",
  376. + bcm2835_mmc_readw(host, SDHCI_COMMAND),
  377. + bcm2835_mmc_readl(host, SDHCI_MAX_CURRENT));
  378. + pr_debug(DRIVER_NAME ": Host ctl2: 0x%08x\n",
  379. + bcm2835_mmc_readw(host, SDHCI_HOST_CONTROL2));
  380. +
  381. + pr_debug(DRIVER_NAME ": ===========================================\n");
  382. +}
  383. +
  384. +
  385. +static void bcm2835_mmc_reset(struct bcm2835_host *host, u8 mask)
  386. +{
  387. + unsigned long timeout;
  388. + unsigned long flags;
  389. +
  390. + spin_lock_irqsave(&host->lock, flags);
  391. + bcm2835_mmc_writeb(host, mask, SDHCI_SOFTWARE_RESET);
  392. +
  393. + if (mask & SDHCI_RESET_ALL)
  394. + host->clock = 0;
  395. +
  396. + /* Wait max 100 ms */
  397. + timeout = 100;
  398. +
  399. + /* hw clears the bit when it's done */
  400. + while (bcm2835_mmc_readb(host, SDHCI_SOFTWARE_RESET) & mask) {
  401. + if (timeout == 0) {
  402. + pr_err("%s: Reset 0x%x never completed.\n",
  403. + mmc_hostname(host->mmc), (int)mask);
  404. + bcm2835_mmc_dumpregs(host);
  405. + return;
  406. + }
  407. + timeout--;
  408. + spin_unlock_irqrestore(&host->lock, flags);
  409. + mdelay(1);
  410. + spin_lock_irqsave(&host->lock, flags);
  411. + }
  412. +
  413. + if (100-timeout > 10 && 100-timeout > host->max_delay) {
  414. + host->max_delay = 100-timeout;
  415. + pr_warning("Warning: MMC controller hung for %d ms\n", host->max_delay);
  416. + }
  417. + spin_unlock_irqrestore(&host->lock, flags);
  418. +}
  419. +
  420. +static void bcm2835_mmc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios);
  421. +
  422. +static void bcm2835_mmc_init(struct bcm2835_host *host, int soft)
  423. +{
  424. + unsigned long flags;
  425. + if (soft)
  426. + bcm2835_mmc_reset(host, SDHCI_RESET_CMD|SDHCI_RESET_DATA);
  427. + else
  428. + bcm2835_mmc_reset(host, SDHCI_RESET_ALL);
  429. +
  430. + host->ier = SDHCI_INT_BUS_POWER | SDHCI_INT_DATA_END_BIT |
  431. + SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_TIMEOUT |
  432. + SDHCI_INT_INDEX | SDHCI_INT_END_BIT | SDHCI_INT_CRC |
  433. + SDHCI_INT_TIMEOUT | SDHCI_INT_DATA_END |
  434. + SDHCI_INT_RESPONSE;
  435. +
  436. + spin_lock_irqsave(&host->lock, flags);
  437. + bcm2835_mmc_writel(host, host->ier, SDHCI_INT_ENABLE, 3);
  438. + bcm2835_mmc_writel(host, host->ier, SDHCI_SIGNAL_ENABLE, 3);
  439. + spin_unlock_irqrestore(&host->lock, flags);
  440. +
  441. + if (soft) {
  442. + /* force clock reconfiguration */
  443. + host->clock = 0;
  444. + bcm2835_mmc_set_ios(host->mmc, &host->mmc->ios);
  445. + }
  446. +}
  447. +
  448. +
  449. +
  450. +static void bcm2835_mmc_finish_data(struct bcm2835_host *host);
  451. +
  452. +static void bcm2835_mmc_dma_complete(void *param)
  453. +{
  454. + struct bcm2835_host *host = param;
  455. + struct dma_chan *dma_chan;
  456. + unsigned long flags;
  457. + u32 dir_data;
  458. +
  459. + spin_lock_irqsave(&host->lock, flags);
  460. +
  461. + if (host->data && !(host->data->flags & MMC_DATA_WRITE)) {
  462. + /* otherwise handled in SDHCI IRQ */
  463. + dma_chan = host->dma_chan_rx;
  464. + dir_data = DMA_FROM_DEVICE;
  465. +
  466. + dma_unmap_sg(dma_chan->device->dev,
  467. + host->data->sg, host->data->sg_len,
  468. + dir_data);
  469. +
  470. + bcm2835_mmc_finish_data(host);
  471. + }
  472. +
  473. + spin_unlock_irqrestore(&host->lock, flags);
  474. +}
  475. +
  476. +static void bcm2835_bcm2835_mmc_read_block_pio(struct bcm2835_host *host)
  477. +{
  478. + unsigned long flags;
  479. + size_t blksize, len, chunk;
  480. +
  481. + u32 uninitialized_var(scratch);
  482. + u8 *buf;
  483. +
  484. + blksize = host->data->blksz;
  485. + chunk = 0;
  486. +
  487. + local_irq_save(flags);
  488. +
  489. + while (blksize) {
  490. + if (!sg_miter_next(&host->sg_miter))
  491. + BUG();
  492. +
  493. + len = min(host->sg_miter.length, blksize);
  494. +
  495. + blksize -= len;
  496. + host->sg_miter.consumed = len;
  497. +
  498. + buf = host->sg_miter.addr;
  499. +
  500. + while (len) {
  501. + if (chunk == 0) {
  502. + scratch = bcm2835_mmc_readl(host, SDHCI_BUFFER);
  503. + chunk = 4;
  504. + }
  505. +
  506. + *buf = scratch & 0xFF;
  507. +
  508. + buf++;
  509. + scratch >>= 8;
  510. + chunk--;
  511. + len--;
  512. + }
  513. + }
  514. +
  515. + sg_miter_stop(&host->sg_miter);
  516. +
  517. + local_irq_restore(flags);
  518. +}
  519. +
  520. +static void bcm2835_bcm2835_mmc_write_block_pio(struct bcm2835_host *host)
  521. +{
  522. + unsigned long flags;
  523. + size_t blksize, len, chunk;
  524. + u32 scratch;
  525. + u8 *buf;
  526. +
  527. + blksize = host->data->blksz;
  528. + chunk = 0;
  529. + chunk = 0;
  530. + scratch = 0;
  531. +
  532. + local_irq_save(flags);
  533. +
  534. + while (blksize) {
  535. + if (!sg_miter_next(&host->sg_miter))
  536. + BUG();
  537. +
  538. + len = min(host->sg_miter.length, blksize);
  539. +
  540. + blksize -= len;
  541. + host->sg_miter.consumed = len;
  542. +
  543. + buf = host->sg_miter.addr;
  544. +
  545. + while (len) {
  546. + scratch |= (u32)*buf << (chunk * 8);
  547. +
  548. + buf++;
  549. + chunk++;
  550. + len--;
  551. +
  552. + if ((chunk == 4) || ((len == 0) && (blksize == 0))) {
  553. + mmc_raw_writel(host, scratch, SDHCI_BUFFER);
  554. + chunk = 0;
  555. + scratch = 0;
  556. + }
  557. + }
  558. + }
  559. +
  560. + sg_miter_stop(&host->sg_miter);
  561. +
  562. + local_irq_restore(flags);
  563. +}
  564. +
  565. +
  566. +static void bcm2835_mmc_transfer_pio(struct bcm2835_host *host)
  567. +{
  568. + u32 mask;
  569. +
  570. + BUG_ON(!host->data);
  571. +
  572. + if (host->blocks == 0)
  573. + return;
  574. +
  575. + if (host->data->flags & MMC_DATA_READ)
  576. + mask = SDHCI_DATA_AVAILABLE;
  577. + else
  578. + mask = SDHCI_SPACE_AVAILABLE;
  579. +
  580. + while (bcm2835_mmc_readl(host, SDHCI_PRESENT_STATE) & mask) {
  581. +
  582. + if (host->data->flags & MMC_DATA_READ)
  583. + bcm2835_bcm2835_mmc_read_block_pio(host);
  584. + else
  585. + bcm2835_bcm2835_mmc_write_block_pio(host);
  586. +
  587. + host->blocks--;
  588. +
  589. + /* QUIRK used in sdhci.c removes the 'if' */
  590. + /* but it seems this is unnecessary */
  591. + if (host->blocks == 0)
  592. + break;
  593. +
  594. +
  595. + }
  596. +}
  597. +
  598. +
  599. +static void bcm2835_mmc_transfer_dma(struct bcm2835_host *host)
  600. +{
  601. + u32 len, dir_data, dir_slave;
  602. + struct dma_async_tx_descriptor *desc = NULL;
  603. + struct dma_chan *dma_chan;
  604. +
  605. +
  606. + WARN_ON(!host->data);
  607. +
  608. + if (!host->data)
  609. + return;
  610. +
  611. + if (host->blocks == 0)
  612. + return;
  613. +
  614. + if (host->data->flags & MMC_DATA_READ) {
  615. + dma_chan = host->dma_chan_rx;
  616. + dir_data = DMA_FROM_DEVICE;
  617. + dir_slave = DMA_DEV_TO_MEM;
  618. + } else {
  619. + dma_chan = host->dma_chan_tx;
  620. + dir_data = DMA_TO_DEVICE;
  621. + dir_slave = DMA_MEM_TO_DEV;
  622. + }
  623. +
  624. + BUG_ON(!dma_chan->device);
  625. + BUG_ON(!dma_chan->device->dev);
  626. + BUG_ON(!host->data->sg);
  627. +
  628. + len = dma_map_sg(dma_chan->device->dev, host->data->sg,
  629. + host->data->sg_len, dir_data);
  630. + if (len > 0) {
  631. + desc = dmaengine_prep_slave_sg(dma_chan, host->data->sg,
  632. + len, dir_slave,
  633. + DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  634. + } else {
  635. + dev_err(mmc_dev(host->mmc), "dma_map_sg returned zero length\n");
  636. + }
  637. + if (desc) {
  638. + unsigned long flags;
  639. + spin_lock_irqsave(&host->lock, flags);
  640. + bcm2835_mmc_unsignal_irqs(host, SDHCI_INT_DATA_AVAIL |
  641. + SDHCI_INT_SPACE_AVAIL);
  642. + host->tx_desc = desc;
  643. + desc->callback = bcm2835_mmc_dma_complete;
  644. + desc->callback_param = host;
  645. + spin_unlock_irqrestore(&host->lock, flags);
  646. + dmaengine_submit(desc);
  647. + dma_async_issue_pending(dma_chan);
  648. + }
  649. +
  650. +}
  651. +
  652. +
  653. +
  654. +static void bcm2835_mmc_set_transfer_irqs(struct bcm2835_host *host)
  655. +{
  656. + u32 pio_irqs = SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL;
  657. + u32 dma_irqs = SDHCI_INT_DMA_END | SDHCI_INT_ADMA_ERROR;
  658. +
  659. + if (host->use_dma)
  660. + host->ier = (host->ier & ~pio_irqs) | dma_irqs;
  661. + else
  662. + host->ier = (host->ier & ~dma_irqs) | pio_irqs;
  663. +
  664. + bcm2835_mmc_writel(host, host->ier, SDHCI_INT_ENABLE, 4);
  665. + bcm2835_mmc_writel(host, host->ier, SDHCI_SIGNAL_ENABLE, 4);
  666. +}
  667. +
  668. +
  669. +static void bcm2835_mmc_prepare_data(struct bcm2835_host *host, struct mmc_command *cmd)
  670. +{
  671. + u8 count;
  672. + struct mmc_data *data = cmd->data;
  673. +
  674. + WARN_ON(host->data);
  675. +
  676. + if (data || (cmd->flags & MMC_RSP_BUSY)) {
  677. + count = TIMEOUT_VAL;
  678. + bcm2835_mmc_writeb(host, count, SDHCI_TIMEOUT_CONTROL);
  679. + }
  680. +
  681. + if (!data)
  682. + return;
  683. +
  684. + /* Sanity checks */
  685. + BUG_ON(data->blksz * data->blocks > 524288);
  686. + BUG_ON(data->blksz > host->mmc->max_blk_size);
  687. + BUG_ON(data->blocks > 65535);
  688. +
  689. + host->data = data;
  690. + host->data_early = 0;
  691. + host->data->bytes_xfered = 0;
  692. +
  693. +
  694. + if (!(host->flags & SDHCI_REQ_USE_DMA)) {
  695. + int flags;
  696. +
  697. + flags = SG_MITER_ATOMIC;
  698. + if (host->data->flags & MMC_DATA_READ)
  699. + flags |= SG_MITER_TO_SG;
  700. + else
  701. + flags |= SG_MITER_FROM_SG;
  702. + sg_miter_start(&host->sg_miter, data->sg, data->sg_len, flags);
  703. + host->blocks = data->blocks;
  704. + }
  705. +
  706. + host->use_dma = host->have_dma && data->blocks > PIO_DMA_BARRIER;
  707. +
  708. + bcm2835_mmc_set_transfer_irqs(host);
  709. +
  710. + /* Set the DMA boundary value and block size */
  711. + bcm2835_mmc_writew(host, SDHCI_MAKE_BLKSZ(SDHCI_DEFAULT_BOUNDARY_ARG,
  712. + data->blksz), SDHCI_BLOCK_SIZE);
  713. + bcm2835_mmc_writew(host, data->blocks, SDHCI_BLOCK_COUNT);
  714. +
  715. + BUG_ON(!host->data);
  716. +}
  717. +
  718. +static void bcm2835_mmc_set_transfer_mode(struct bcm2835_host *host,
  719. + struct mmc_command *cmd)
  720. +{
  721. + u16 mode;
  722. + struct mmc_data *data = cmd->data;
  723. +
  724. + if (data == NULL) {
  725. + /* clear Auto CMD settings for no data CMDs */
  726. + mode = bcm2835_mmc_readw(host, SDHCI_TRANSFER_MODE);
  727. + bcm2835_mmc_writew(host, mode & ~(SDHCI_TRNS_AUTO_CMD12 |
  728. + SDHCI_TRNS_AUTO_CMD23), SDHCI_TRANSFER_MODE);
  729. + return;
  730. + }
  731. +
  732. + WARN_ON(!host->data);
  733. +
  734. + mode = SDHCI_TRNS_BLK_CNT_EN;
  735. +
  736. + if ((mmc_op_multi(cmd->opcode) || data->blocks > 1)) {
  737. + mode |= SDHCI_TRNS_MULTI;
  738. +
  739. + /*
  740. + * If we are sending CMD23, CMD12 never gets sent
  741. + * on successful completion (so no Auto-CMD12).
  742. + */
  743. + if (!host->mrq->sbc && (host->flags & SDHCI_AUTO_CMD12))
  744. + mode |= SDHCI_TRNS_AUTO_CMD12;
  745. + else if (host->mrq->sbc && (host->flags & SDHCI_AUTO_CMD23)) {
  746. + mode |= SDHCI_TRNS_AUTO_CMD23;
  747. + bcm2835_mmc_writel(host, host->mrq->sbc->arg, SDHCI_ARGUMENT2, 5);
  748. + }
  749. + }
  750. +
  751. + if (data->flags & MMC_DATA_READ)
  752. + mode |= SDHCI_TRNS_READ;
  753. + if (host->flags & SDHCI_REQ_USE_DMA)
  754. + mode |= SDHCI_TRNS_DMA;
  755. +
  756. + bcm2835_mmc_writew(host, mode, SDHCI_TRANSFER_MODE);
  757. +}
  758. +
  759. +void bcm2835_mmc_send_command(struct bcm2835_host *host, struct mmc_command *cmd)
  760. +{
  761. + int flags;
  762. + u32 mask;
  763. + unsigned long timeout;
  764. +
  765. + WARN_ON(host->cmd);
  766. +
  767. + /* Wait max 10 ms */
  768. + timeout = 1000;
  769. +
  770. + mask = SDHCI_CMD_INHIBIT;
  771. + if ((cmd->data != NULL) || (cmd->flags & MMC_RSP_BUSY))
  772. + mask |= SDHCI_DATA_INHIBIT;
  773. +
  774. + /* We shouldn't wait for data inihibit for stop commands, even
  775. + though they might use busy signaling */
  776. + if (host->mrq->data && (cmd == host->mrq->data->stop))
  777. + mask &= ~SDHCI_DATA_INHIBIT;
  778. +
  779. + while (bcm2835_mmc_readl(host, SDHCI_PRESENT_STATE) & mask) {
  780. + if (timeout == 0) {
  781. + pr_err("%s: Controller never released inhibit bit(s).\n",
  782. + mmc_hostname(host->mmc));
  783. + bcm2835_mmc_dumpregs(host);
  784. + cmd->error = -EIO;
  785. + tasklet_schedule(&host->finish_tasklet);
  786. + return;
  787. + }
  788. + timeout--;
  789. + udelay(10);
  790. + }
  791. +
  792. + if ((1000-timeout)/100 > 1 && (1000-timeout)/100 > host->max_delay) {
  793. + host->max_delay = (1000-timeout)/100;
  794. + pr_warning("Warning: MMC controller hung for %d ms\n", host->max_delay);
  795. + }
  796. +
  797. + timeout = jiffies;
  798. + if (!cmd->data && cmd->busy_timeout > 9000)
  799. + timeout += DIV_ROUND_UP(cmd->busy_timeout, 1000) * HZ + HZ;
  800. + else
  801. + timeout += 10 * HZ;
  802. + mod_timer(&host->timer, timeout);
  803. +
  804. + host->cmd = cmd;
  805. +
  806. + bcm2835_mmc_prepare_data(host, cmd);
  807. +
  808. + bcm2835_mmc_writel(host, cmd->arg, SDHCI_ARGUMENT, 6);
  809. +
  810. + bcm2835_mmc_set_transfer_mode(host, cmd);
  811. +
  812. + if ((cmd->flags & MMC_RSP_136) && (cmd->flags & MMC_RSP_BUSY)) {
  813. + pr_err("%s: Unsupported response type!\n",
  814. + mmc_hostname(host->mmc));
  815. + cmd->error = -EINVAL;
  816. + tasklet_schedule(&host->finish_tasklet);
  817. + return;
  818. + }
  819. +
  820. + if (!(cmd->flags & MMC_RSP_PRESENT))
  821. + flags = SDHCI_CMD_RESP_NONE;
  822. + else if (cmd->flags & MMC_RSP_136)
  823. + flags = SDHCI_CMD_RESP_LONG;
  824. + else if (cmd->flags & MMC_RSP_BUSY)
  825. + flags = SDHCI_CMD_RESP_SHORT_BUSY;
  826. + else
  827. + flags = SDHCI_CMD_RESP_SHORT;
  828. +
  829. + if (cmd->flags & MMC_RSP_CRC)
  830. + flags |= SDHCI_CMD_CRC;
  831. + if (cmd->flags & MMC_RSP_OPCODE)
  832. + flags |= SDHCI_CMD_INDEX;
  833. +
  834. + if (cmd->data)
  835. + flags |= SDHCI_CMD_DATA;
  836. +
  837. + bcm2835_mmc_writew(host, SDHCI_MAKE_CMD(cmd->opcode, flags), SDHCI_COMMAND);
  838. +}
  839. +
  840. +
  841. +static void bcm2835_mmc_finish_data(struct bcm2835_host *host)
  842. +{
  843. + struct mmc_data *data;
  844. +
  845. + BUG_ON(!host->data);
  846. +
  847. + data = host->data;
  848. + host->data = NULL;
  849. +
  850. + if (data->error)
  851. + data->bytes_xfered = 0;
  852. + else
  853. + data->bytes_xfered = data->blksz * data->blocks;
  854. +
  855. + /*
  856. + * Need to send CMD12 if -
  857. + * a) open-ended multiblock transfer (no CMD23)
  858. + * b) error in multiblock transfer
  859. + */
  860. + if (data->stop &&
  861. + (data->error ||
  862. + !host->mrq->sbc)) {
  863. +
  864. + /*
  865. + * The controller needs a reset of internal state machines
  866. + * upon error conditions.
  867. + */
  868. + if (data->error) {
  869. + bcm2835_mmc_reset(host, SDHCI_RESET_CMD);
  870. + bcm2835_mmc_reset(host, SDHCI_RESET_DATA);
  871. + }
  872. +
  873. + bcm2835_mmc_send_command(host, data->stop);
  874. + } else
  875. + tasklet_schedule(&host->finish_tasklet);
  876. +}
  877. +
  878. +static void bcm2835_mmc_finish_command(struct bcm2835_host *host)
  879. +{
  880. + int i;
  881. +
  882. + BUG_ON(host->cmd == NULL);
  883. +
  884. + if (host->cmd->flags & MMC_RSP_PRESENT) {
  885. + if (host->cmd->flags & MMC_RSP_136) {
  886. + /* CRC is stripped so we need to do some shifting. */
  887. + for (i = 0; i < 4; i++) {
  888. + host->cmd->resp[i] = bcm2835_mmc_readl(host,
  889. + SDHCI_RESPONSE + (3-i)*4) << 8;
  890. + if (i != 3)
  891. + host->cmd->resp[i] |=
  892. + bcm2835_mmc_readb(host,
  893. + SDHCI_RESPONSE + (3-i)*4-1);
  894. + }
  895. + } else {
  896. + host->cmd->resp[0] = bcm2835_mmc_readl(host, SDHCI_RESPONSE);
  897. + }
  898. + }
  899. +
  900. + host->cmd->error = 0;
  901. +
  902. + /* Finished CMD23, now send actual command. */
  903. + if (host->cmd == host->mrq->sbc) {
  904. + host->cmd = NULL;
  905. + bcm2835_mmc_send_command(host, host->mrq->cmd);
  906. +
  907. + if (host->mrq->cmd->data && host->use_dma) {
  908. + /* DMA transfer starts now, PIO starts after interrupt */
  909. + bcm2835_mmc_transfer_dma(host);
  910. + }
  911. + } else {
  912. +
  913. + /* Processed actual command. */
  914. + if (host->data && host->data_early)
  915. + bcm2835_mmc_finish_data(host);
  916. +
  917. + if (!host->cmd->data)
  918. + tasklet_schedule(&host->finish_tasklet);
  919. +
  920. + host->cmd = NULL;
  921. + }
  922. +}
  923. +
  924. +
  925. +static void bcm2835_mmc_timeout_timer(unsigned long data)
  926. +{
  927. + struct bcm2835_host *host;
  928. + unsigned long flags;
  929. +
  930. + host = (struct bcm2835_host *)data;
  931. +
  932. + spin_lock_irqsave(&host->lock, flags);
  933. +
  934. + if (host->mrq) {
  935. + pr_err("%s: Timeout waiting for hardware interrupt.\n",
  936. + mmc_hostname(host->mmc));
  937. + bcm2835_mmc_dumpregs(host);
  938. +
  939. + if (host->data) {
  940. + host->data->error = -ETIMEDOUT;
  941. + bcm2835_mmc_finish_data(host);
  942. + } else {
  943. + if (host->cmd)
  944. + host->cmd->error = -ETIMEDOUT;
  945. + else
  946. + host->mrq->cmd->error = -ETIMEDOUT;
  947. +
  948. + tasklet_schedule(&host->finish_tasklet);
  949. + }
  950. + }
  951. +
  952. + mmiowb();
  953. + spin_unlock_irqrestore(&host->lock, flags);
  954. +}
  955. +
  956. +
  957. +static void bcm2835_mmc_enable_sdio_irq_nolock(struct bcm2835_host *host, int enable)
  958. +{
  959. + if (!(host->flags & SDHCI_DEVICE_DEAD)) {
  960. + if (enable)
  961. + host->ier |= SDHCI_INT_CARD_INT;
  962. + else
  963. + host->ier &= ~SDHCI_INT_CARD_INT;
  964. +
  965. + bcm2835_mmc_writel(host, host->ier, SDHCI_INT_ENABLE, 7);
  966. + bcm2835_mmc_writel(host, host->ier, SDHCI_SIGNAL_ENABLE, 7);
  967. + mmiowb();
  968. + }
  969. +}
  970. +
  971. +static void bcm2835_mmc_enable_sdio_irq(struct mmc_host *mmc, int enable)
  972. +{
  973. + struct bcm2835_host *host = mmc_priv(mmc);
  974. + unsigned long flags;
  975. +
  976. + spin_lock_irqsave(&host->lock, flags);
  977. + if (enable)
  978. + host->flags |= SDHCI_SDIO_IRQ_ENABLED;
  979. + else
  980. + host->flags &= ~SDHCI_SDIO_IRQ_ENABLED;
  981. +
  982. + bcm2835_mmc_enable_sdio_irq_nolock(host, enable);
  983. + spin_unlock_irqrestore(&host->lock, flags);
  984. +}
  985. +
  986. +static void bcm2835_mmc_cmd_irq(struct bcm2835_host *host, u32 intmask)
  987. +{
  988. +
  989. + BUG_ON(intmask == 0);
  990. +
  991. + if (!host->cmd) {
  992. + pr_err("%s: Got command interrupt 0x%08x even "
  993. + "though no command operation was in progress.\n",
  994. + mmc_hostname(host->mmc), (unsigned)intmask);
  995. + bcm2835_mmc_dumpregs(host);
  996. + return;
  997. + }
  998. +
  999. + if (intmask & SDHCI_INT_TIMEOUT)
  1000. + host->cmd->error = -ETIMEDOUT;
  1001. + else if (intmask & (SDHCI_INT_CRC | SDHCI_INT_END_BIT |
  1002. + SDHCI_INT_INDEX)) {
  1003. + host->cmd->error = -EILSEQ;
  1004. + }
  1005. +
  1006. + if (host->cmd->error) {
  1007. + tasklet_schedule(&host->finish_tasklet);
  1008. + return;
  1009. + }
  1010. +
  1011. + if (intmask & SDHCI_INT_RESPONSE)
  1012. + bcm2835_mmc_finish_command(host);
  1013. +
  1014. +}
  1015. +
  1016. +static void bcm2835_mmc_data_irq(struct bcm2835_host *host, u32 intmask)
  1017. +{
  1018. + struct dma_chan *dma_chan;
  1019. + u32 dir_data;
  1020. +
  1021. + BUG_ON(intmask == 0);
  1022. +
  1023. + if (!host->data) {
  1024. + /*
  1025. + * The "data complete" interrupt is also used to
  1026. + * indicate that a busy state has ended. See comment
  1027. + * above in sdhci_cmd_irq().
  1028. + */
  1029. + if (host->cmd && (host->cmd->flags & MMC_RSP_BUSY)) {
  1030. + if (intmask & SDHCI_INT_DATA_END) {
  1031. + bcm2835_mmc_finish_command(host);
  1032. + return;
  1033. + }
  1034. + }
  1035. +
  1036. + pr_debug("%s: Got data interrupt 0x%08x even "
  1037. + "though no data operation was in progress.\n",
  1038. + mmc_hostname(host->mmc), (unsigned)intmask);
  1039. + bcm2835_mmc_dumpregs(host);
  1040. +
  1041. + return;
  1042. + }
  1043. +
  1044. + if (intmask & SDHCI_INT_DATA_TIMEOUT)
  1045. + host->data->error = -ETIMEDOUT;
  1046. + else if (intmask & SDHCI_INT_DATA_END_BIT)
  1047. + host->data->error = -EILSEQ;
  1048. + else if ((intmask & SDHCI_INT_DATA_CRC) &&
  1049. + SDHCI_GET_CMD(bcm2835_mmc_readw(host, SDHCI_COMMAND))
  1050. + != MMC_BUS_TEST_R)
  1051. + host->data->error = -EILSEQ;
  1052. +
  1053. + if (host->use_dma) {
  1054. + if (host->data->flags & MMC_DATA_WRITE) {
  1055. + /* IRQ handled here */
  1056. +
  1057. + dma_chan = host->dma_chan_tx;
  1058. + dir_data = DMA_TO_DEVICE;
  1059. + dma_unmap_sg(dma_chan->device->dev,
  1060. + host->data->sg, host->data->sg_len,
  1061. + dir_data);
  1062. +
  1063. + bcm2835_mmc_finish_data(host);
  1064. + }
  1065. +
  1066. + } else {
  1067. + if (host->data->error)
  1068. + bcm2835_mmc_finish_data(host);
  1069. + else {
  1070. + if (intmask & (SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL))
  1071. + bcm2835_mmc_transfer_pio(host);
  1072. +
  1073. + if (intmask & SDHCI_INT_DATA_END) {
  1074. + if (host->cmd) {
  1075. + /*
  1076. + * Data managed to finish before the
  1077. + * command completed. Make sure we do
  1078. + * things in the proper order.
  1079. + */
  1080. + host->data_early = 1;
  1081. + } else {
  1082. + bcm2835_mmc_finish_data(host);
  1083. + }
  1084. + }
  1085. + }
  1086. + }
  1087. +}
  1088. +
  1089. +
  1090. +static irqreturn_t bcm2835_mmc_irq(int irq, void *dev_id)
  1091. +{
  1092. + irqreturn_t result = IRQ_NONE;
  1093. + struct bcm2835_host *host = dev_id;
  1094. + u32 intmask, mask, unexpected = 0;
  1095. + int max_loops = 16;
  1096. +
  1097. + spin_lock(&host->lock);
  1098. +
  1099. + intmask = bcm2835_mmc_readl(host, SDHCI_INT_STATUS);
  1100. +
  1101. + if (!intmask || intmask == 0xffffffff) {
  1102. + result = IRQ_NONE;
  1103. + goto out;
  1104. + }
  1105. +
  1106. + do {
  1107. + /* Clear selected interrupts. */
  1108. + mask = intmask & (SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK |
  1109. + SDHCI_INT_BUS_POWER);
  1110. + bcm2835_mmc_writel(host, mask, SDHCI_INT_STATUS, 8);
  1111. +
  1112. +
  1113. + if (intmask & SDHCI_INT_CMD_MASK)
  1114. + bcm2835_mmc_cmd_irq(host, intmask & SDHCI_INT_CMD_MASK);
  1115. +
  1116. + if (intmask & SDHCI_INT_DATA_MASK)
  1117. + bcm2835_mmc_data_irq(host, intmask & SDHCI_INT_DATA_MASK);
  1118. +
  1119. + if (intmask & SDHCI_INT_BUS_POWER)
  1120. + pr_err("%s: Card is consuming too much power!\n",
  1121. + mmc_hostname(host->mmc));
  1122. +
  1123. + if (intmask & SDHCI_INT_CARD_INT) {
  1124. + bcm2835_mmc_enable_sdio_irq_nolock(host, false);
  1125. + host->thread_isr |= SDHCI_INT_CARD_INT;
  1126. + result = IRQ_WAKE_THREAD;
  1127. + }
  1128. +
  1129. + intmask &= ~(SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE |
  1130. + SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK |
  1131. + SDHCI_INT_ERROR | SDHCI_INT_BUS_POWER |
  1132. + SDHCI_INT_CARD_INT);
  1133. +
  1134. + if (intmask) {
  1135. + unexpected |= intmask;
  1136. + bcm2835_mmc_writel(host, intmask, SDHCI_INT_STATUS, 9);
  1137. + }
  1138. +
  1139. + if (result == IRQ_NONE)
  1140. + result = IRQ_HANDLED;
  1141. +
  1142. + intmask = bcm2835_mmc_readl(host, SDHCI_INT_STATUS);
  1143. + } while (intmask && --max_loops);
  1144. +out:
  1145. + spin_unlock(&host->lock);
  1146. +
  1147. + if (unexpected) {
  1148. + pr_err("%s: Unexpected interrupt 0x%08x.\n",
  1149. + mmc_hostname(host->mmc), unexpected);
  1150. + bcm2835_mmc_dumpregs(host);
  1151. + }
  1152. +
  1153. + return result;
  1154. +}
  1155. +
  1156. +static irqreturn_t bcm2835_mmc_thread_irq(int irq, void *dev_id)
  1157. +{
  1158. + struct bcm2835_host *host = dev_id;
  1159. + unsigned long flags;
  1160. + u32 isr;
  1161. +
  1162. + spin_lock_irqsave(&host->lock, flags);
  1163. + isr = host->thread_isr;
  1164. + host->thread_isr = 0;
  1165. + spin_unlock_irqrestore(&host->lock, flags);
  1166. +
  1167. + if (isr & SDHCI_INT_CARD_INT) {
  1168. + sdio_run_irqs(host->mmc);
  1169. +
  1170. + spin_lock_irqsave(&host->lock, flags);
  1171. + if (host->flags & SDHCI_SDIO_IRQ_ENABLED)
  1172. + bcm2835_mmc_enable_sdio_irq_nolock(host, true);
  1173. + spin_unlock_irqrestore(&host->lock, flags);
  1174. + }
  1175. +
  1176. + return isr ? IRQ_HANDLED : IRQ_NONE;
  1177. +}
  1178. +
  1179. +
  1180. +
  1181. +void bcm2835_mmc_set_clock(struct bcm2835_host *host, unsigned int clock)
  1182. +{
  1183. + int div = 0; /* Initialized for compiler warning */
  1184. + int real_div = div, clk_mul = 1;
  1185. + u16 clk = 0;
  1186. + unsigned long timeout;
  1187. + unsigned int input_clock = clock;
  1188. +
  1189. + if (host->overclock_50 && (clock == 50000000))
  1190. + clock = host->overclock_50 * 1000000 + 999999;
  1191. +
  1192. + host->mmc->actual_clock = 0;
  1193. +
  1194. + bcm2835_mmc_writew(host, 0, SDHCI_CLOCK_CONTROL);
  1195. +
  1196. + if (clock == 0)
  1197. + return;
  1198. +
  1199. + /* Version 3.00 divisors must be a multiple of 2. */
  1200. + if (host->max_clk <= clock)
  1201. + div = 1;
  1202. + else {
  1203. + for (div = 2; div < SDHCI_MAX_DIV_SPEC_300;
  1204. + div += 2) {
  1205. + if ((host->max_clk / div) <= clock)
  1206. + break;
  1207. + }
  1208. + }
  1209. +
  1210. + real_div = div;
  1211. + div >>= 1;
  1212. +
  1213. + if (real_div)
  1214. + clock = (host->max_clk * clk_mul) / real_div;
  1215. + host->mmc->actual_clock = clock;
  1216. +
  1217. + if ((clock > input_clock) && (clock > host->max_overclock)) {
  1218. + pr_warn("%s: Overclocking to %dHz\n",
  1219. + mmc_hostname(host->mmc), clock);
  1220. + host->max_overclock = clock;
  1221. + }
  1222. +
  1223. + clk |= (div & SDHCI_DIV_MASK) << SDHCI_DIVIDER_SHIFT;
  1224. + clk |= ((div & SDHCI_DIV_HI_MASK) >> SDHCI_DIV_MASK_LEN)
  1225. + << SDHCI_DIVIDER_HI_SHIFT;
  1226. + clk |= SDHCI_CLOCK_INT_EN;
  1227. + bcm2835_mmc_writew(host, clk, SDHCI_CLOCK_CONTROL);
  1228. +
  1229. + /* Wait max 20 ms */
  1230. + timeout = 20;
  1231. + while (!((clk = bcm2835_mmc_readw(host, SDHCI_CLOCK_CONTROL))
  1232. + & SDHCI_CLOCK_INT_STABLE)) {
  1233. + if (timeout == 0) {
  1234. + pr_err("%s: Internal clock never "
  1235. + "stabilised.\n", mmc_hostname(host->mmc));
  1236. + bcm2835_mmc_dumpregs(host);
  1237. + return;
  1238. + }
  1239. + timeout--;
  1240. + mdelay(1);
  1241. + }
  1242. +
  1243. + if (20-timeout > 10 && 20-timeout > host->max_delay) {
  1244. + host->max_delay = 20-timeout;
  1245. + pr_warning("Warning: MMC controller hung for %d ms\n", host->max_delay);
  1246. + }
  1247. +
  1248. + clk |= SDHCI_CLOCK_CARD_EN;
  1249. + bcm2835_mmc_writew(host, clk, SDHCI_CLOCK_CONTROL);
  1250. +}
  1251. +
  1252. +static void bcm2835_mmc_request(struct mmc_host *mmc, struct mmc_request *mrq)
  1253. +{
  1254. + struct bcm2835_host *host;
  1255. + unsigned long flags;
  1256. +
  1257. + host = mmc_priv(mmc);
  1258. +
  1259. + spin_lock_irqsave(&host->lock, flags);
  1260. +
  1261. + WARN_ON(host->mrq != NULL);
  1262. +
  1263. + host->mrq = mrq;
  1264. +
  1265. + if (mrq->sbc && !(host->flags & SDHCI_AUTO_CMD23))
  1266. + bcm2835_mmc_send_command(host, mrq->sbc);
  1267. + else
  1268. + bcm2835_mmc_send_command(host, mrq->cmd);
  1269. +
  1270. + mmiowb();
  1271. + spin_unlock_irqrestore(&host->lock, flags);
  1272. +
  1273. + if (!(mrq->sbc && !(host->flags & SDHCI_AUTO_CMD23)) && mrq->cmd->data && host->use_dma) {
  1274. + /* DMA transfer starts now, PIO starts after interrupt */
  1275. + bcm2835_mmc_transfer_dma(host);
  1276. + }
  1277. +}
  1278. +
  1279. +
  1280. +static void bcm2835_mmc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
  1281. +{
  1282. +
  1283. + struct bcm2835_host *host = mmc_priv(mmc);
  1284. + unsigned long flags;
  1285. + u8 ctrl;
  1286. + u16 clk, ctrl_2;
  1287. +
  1288. + pr_debug("bcm2835_mmc_set_ios: clock %d, pwr %d, bus_width %d, timing %d, vdd %d, drv_type %d\n",
  1289. + ios->clock, ios->power_mode, ios->bus_width,
  1290. + ios->timing, ios->signal_voltage, ios->drv_type);
  1291. +
  1292. + spin_lock_irqsave(&host->lock, flags);
  1293. +
  1294. + if (!ios->clock || ios->clock != host->clock) {
  1295. + bcm2835_mmc_set_clock(host, ios->clock);
  1296. + host->clock = ios->clock;
  1297. + }
  1298. +
  1299. + if (host->pwr != SDHCI_POWER_330) {
  1300. + host->pwr = SDHCI_POWER_330;
  1301. + bcm2835_mmc_writeb(host, SDHCI_POWER_330 | SDHCI_POWER_ON, SDHCI_POWER_CONTROL);
  1302. + }
  1303. +
  1304. + ctrl = bcm2835_mmc_readb(host, SDHCI_HOST_CONTROL);
  1305. +
  1306. + /* set bus width */
  1307. + ctrl &= ~SDHCI_CTRL_8BITBUS;
  1308. + if (ios->bus_width == MMC_BUS_WIDTH_4)
  1309. + ctrl |= SDHCI_CTRL_4BITBUS;
  1310. + else
  1311. + ctrl &= ~SDHCI_CTRL_4BITBUS;
  1312. +
  1313. + ctrl &= ~SDHCI_CTRL_HISPD; /* NO_HISPD_BIT */
  1314. +
  1315. +
  1316. + bcm2835_mmc_writeb(host, ctrl, SDHCI_HOST_CONTROL);
  1317. + /*
  1318. + * We only need to set Driver Strength if the
  1319. + * preset value enable is not set.
  1320. + */
  1321. + ctrl_2 = bcm2835_mmc_readw(host, SDHCI_HOST_CONTROL2);
  1322. + ctrl_2 &= ~SDHCI_CTRL_DRV_TYPE_MASK;
  1323. + if (ios->drv_type == MMC_SET_DRIVER_TYPE_A)
  1324. + ctrl_2 |= SDHCI_CTRL_DRV_TYPE_A;
  1325. + else if (ios->drv_type == MMC_SET_DRIVER_TYPE_C)
  1326. + ctrl_2 |= SDHCI_CTRL_DRV_TYPE_C;
  1327. +
  1328. + bcm2835_mmc_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
  1329. +
  1330. + /* Reset SD Clock Enable */
  1331. + clk = bcm2835_mmc_readw(host, SDHCI_CLOCK_CONTROL);
  1332. + clk &= ~SDHCI_CLOCK_CARD_EN;
  1333. + bcm2835_mmc_writew(host, clk, SDHCI_CLOCK_CONTROL);
  1334. +
  1335. + /* Re-enable SD Clock */
  1336. + bcm2835_mmc_set_clock(host, host->clock);
  1337. + bcm2835_mmc_writeb(host, ctrl, SDHCI_HOST_CONTROL);
  1338. +
  1339. + mmiowb();
  1340. +
  1341. + spin_unlock_irqrestore(&host->lock, flags);
  1342. +}
  1343. +
  1344. +
  1345. +static struct mmc_host_ops bcm2835_ops = {
  1346. + .request = bcm2835_mmc_request,
  1347. + .set_ios = bcm2835_mmc_set_ios,
  1348. + .enable_sdio_irq = bcm2835_mmc_enable_sdio_irq,
  1349. +};
  1350. +
  1351. +
  1352. +static void bcm2835_mmc_tasklet_finish(unsigned long param)
  1353. +{
  1354. + struct bcm2835_host *host;
  1355. + unsigned long flags;
  1356. + struct mmc_request *mrq;
  1357. +
  1358. + host = (struct bcm2835_host *)param;
  1359. +
  1360. + spin_lock_irqsave(&host->lock, flags);
  1361. +
  1362. + /*
  1363. + * If this tasklet gets rescheduled while running, it will
  1364. + * be run again afterwards but without any active request.
  1365. + */
  1366. + if (!host->mrq) {
  1367. + spin_unlock_irqrestore(&host->lock, flags);
  1368. + return;
  1369. + }
  1370. +
  1371. + del_timer(&host->timer);
  1372. +
  1373. + mrq = host->mrq;
  1374. +
  1375. + /*
  1376. + * The controller needs a reset of internal state machines
  1377. + * upon error conditions.
  1378. + */
  1379. + if (!(host->flags & SDHCI_DEVICE_DEAD) &&
  1380. + ((mrq->cmd && mrq->cmd->error) ||
  1381. + (mrq->data && (mrq->data->error ||
  1382. + (mrq->data->stop && mrq->data->stop->error))))) {
  1383. +
  1384. + spin_unlock_irqrestore(&host->lock, flags);
  1385. + bcm2835_mmc_reset(host, SDHCI_RESET_CMD);
  1386. + bcm2835_mmc_reset(host, SDHCI_RESET_DATA);
  1387. + spin_lock_irqsave(&host->lock, flags);
  1388. + }
  1389. +
  1390. + host->mrq = NULL;
  1391. + host->cmd = NULL;
  1392. + host->data = NULL;
  1393. +
  1394. + mmiowb();
  1395. +
  1396. + spin_unlock_irqrestore(&host->lock, flags);
  1397. + mmc_request_done(host->mmc, mrq);
  1398. +}
  1399. +
  1400. +
  1401. +
  1402. +static int bcm2835_mmc_add_host(struct bcm2835_host *host)
  1403. +{
  1404. + struct mmc_host *mmc = host->mmc;
  1405. + struct device *dev = mmc->parent;
  1406. +#ifndef FORCE_PIO
  1407. + struct dma_slave_config cfg;
  1408. +#endif
  1409. + int ret;
  1410. +
  1411. + bcm2835_mmc_reset(host, SDHCI_RESET_ALL);
  1412. +
  1413. + host->clk_mul = 0;
  1414. +
  1415. + mmc->f_max = host->max_clk;
  1416. + mmc->f_max = host->max_clk;
  1417. + mmc->f_min = host->max_clk / SDHCI_MAX_DIV_SPEC_300;
  1418. +
  1419. + /* SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK */
  1420. + host->timeout_clk = mmc->f_max / 1000;
  1421. + mmc->max_busy_timeout = (1 << 27) / host->timeout_clk;
  1422. +
  1423. + /* host controller capabilities */
  1424. + mmc->caps |= MMC_CAP_CMD23 | MMC_CAP_ERASE | MMC_CAP_NEEDS_POLL |
  1425. + MMC_CAP_SDIO_IRQ | MMC_CAP_SD_HIGHSPEED |
  1426. + MMC_CAP_MMC_HIGHSPEED | MMC_CAP_4_BIT_DATA;
  1427. +
  1428. + mmc->caps2 |= MMC_CAP2_SDIO_IRQ_NOTHREAD;
  1429. +
  1430. + host->flags = SDHCI_AUTO_CMD23;
  1431. +
  1432. + dev_info(dev, "mmc_debug:%x mmc_debug2:%x\n", mmc_debug, mmc_debug2);
  1433. +#ifdef FORCE_PIO
  1434. + dev_info(dev, "Forcing PIO mode\n");
  1435. + host->have_dma = false;
  1436. +#else
  1437. + if (IS_ERR_OR_NULL(host->dma_chan_tx) ||
  1438. + IS_ERR_OR_NULL(host->dma_chan_rx)) {
  1439. + dev_err(dev, "%s: Unable to initialise DMA channels. Falling back to PIO\n",
  1440. + DRIVER_NAME);
  1441. + host->have_dma = false;
  1442. + } else {
  1443. + dev_info(dev, "DMA channels allocated");
  1444. + host->have_dma = true;
  1445. +
  1446. + cfg.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  1447. + cfg.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  1448. + cfg.slave_id = 11; /* DREQ channel */
  1449. +
  1450. + cfg.direction = DMA_MEM_TO_DEV;
  1451. + cfg.src_addr = 0;
  1452. + cfg.dst_addr = host->bus_addr + SDHCI_BUFFER;
  1453. + ret = dmaengine_slave_config(host->dma_chan_tx, &cfg);
  1454. +
  1455. + cfg.direction = DMA_DEV_TO_MEM;
  1456. + cfg.src_addr = host->bus_addr + SDHCI_BUFFER;
  1457. + cfg.dst_addr = 0;
  1458. + ret = dmaengine_slave_config(host->dma_chan_rx, &cfg);
  1459. + }
  1460. +#endif
  1461. + mmc->max_segs = 128;
  1462. + mmc->max_req_size = 524288;
  1463. + mmc->max_seg_size = mmc->max_req_size;
  1464. + mmc->max_blk_size = 512;
  1465. + mmc->max_blk_count = 65535;
  1466. +
  1467. + /* report supported voltage ranges */
  1468. + mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34;
  1469. +
  1470. + tasklet_init(&host->finish_tasklet,
  1471. + bcm2835_mmc_tasklet_finish, (unsigned long)host);
  1472. +
  1473. + setup_timer(&host->timer, bcm2835_mmc_timeout_timer, (unsigned long)host);
  1474. + init_waitqueue_head(&host->buf_ready_int);
  1475. +
  1476. + bcm2835_mmc_init(host, 0);
  1477. + ret = devm_request_threaded_irq(dev, host->irq, bcm2835_mmc_irq,
  1478. + bcm2835_mmc_thread_irq, IRQF_SHARED,
  1479. + mmc_hostname(mmc), host);
  1480. + if (ret) {
  1481. + dev_err(dev, "Failed to request IRQ %d: %d\n", host->irq, ret);
  1482. + goto untasklet;
  1483. + }
  1484. +
  1485. + mmiowb();
  1486. + mmc_add_host(mmc);
  1487. +
  1488. + return 0;
  1489. +
  1490. +untasklet:
  1491. + tasklet_kill(&host->finish_tasklet);
  1492. +
  1493. + return ret;
  1494. +}
  1495. +
  1496. +static int bcm2835_mmc_probe(struct platform_device *pdev)
  1497. +{
  1498. + struct device *dev = &pdev->dev;
  1499. + struct device_node *node = dev->of_node;
  1500. + struct clk *clk;
  1501. + struct resource *iomem;
  1502. + struct bcm2835_host *host;
  1503. + struct mmc_host *mmc;
  1504. + const __be32 *addr;
  1505. + int ret;
  1506. +
  1507. + mmc = mmc_alloc_host(sizeof(*host), dev);
  1508. + if (!mmc)
  1509. + return -ENOMEM;
  1510. +
  1511. + mmc->ops = &bcm2835_ops;
  1512. + host = mmc_priv(mmc);
  1513. + host->mmc = mmc;
  1514. + host->timeout = msecs_to_jiffies(1000);
  1515. + spin_lock_init(&host->lock);
  1516. +
  1517. + iomem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1518. + host->ioaddr = devm_ioremap_resource(dev, iomem);
  1519. + if (IS_ERR(host->ioaddr)) {
  1520. + ret = PTR_ERR(host->ioaddr);
  1521. + goto err;
  1522. + }
  1523. +
  1524. + addr = of_get_address(node, 0, NULL, NULL);
  1525. + if (!addr) {
  1526. + dev_err(dev, "could not get DMA-register address\n");
  1527. + return -ENODEV;
  1528. + }
  1529. + host->bus_addr = be32_to_cpup(addr);
  1530. + pr_debug(" - ioaddr %lx, iomem->start %lx, bus_addr %lx\n",
  1531. + (unsigned long)host->ioaddr,
  1532. + (unsigned long)iomem->start,
  1533. + (unsigned long)host->bus_addr);
  1534. +
  1535. +#ifndef FORCE_PIO
  1536. + if (node) {
  1537. + host->dma_chan_tx = dma_request_slave_channel(dev, "tx");
  1538. + host->dma_chan_rx = dma_request_slave_channel(dev, "rx");
  1539. + } else {
  1540. + dma_cap_mask_t mask;
  1541. +
  1542. + dma_cap_zero(mask);
  1543. + /* we don't care about the channel, any would work */
  1544. + dma_cap_set(DMA_SLAVE, mask);
  1545. + host->dma_chan_tx = dma_request_channel(mask, NULL, NULL);
  1546. + host->dma_chan_rx = dma_request_channel(mask, NULL, NULL);
  1547. + }
  1548. +#endif
  1549. + clk = devm_clk_get(dev, NULL);
  1550. + if (IS_ERR(clk)) {
  1551. + dev_err(dev, "could not get clk\n");
  1552. + ret = PTR_ERR(clk);
  1553. + goto err;
  1554. + }
  1555. +
  1556. + host->max_clk = clk_get_rate(clk);
  1557. +
  1558. + host->irq = platform_get_irq(pdev, 0);
  1559. + if (host->irq <= 0) {
  1560. + dev_err(dev, "get IRQ failed\n");
  1561. + ret = -EINVAL;
  1562. + goto err;
  1563. + }
  1564. +
  1565. + if (node) {
  1566. + mmc_of_parse(mmc);
  1567. +
  1568. + /* Read any custom properties */
  1569. + of_property_read_u32(node,
  1570. + "brcm,overclock-50",
  1571. + &host->overclock_50);
  1572. + } else {
  1573. + mmc->caps |= MMC_CAP_4_BIT_DATA;
  1574. + }
  1575. +
  1576. + ret = bcm2835_mmc_add_host(host);
  1577. + if (ret)
  1578. + goto err;
  1579. +
  1580. + platform_set_drvdata(pdev, host);
  1581. +
  1582. + return 0;
  1583. +err:
  1584. + mmc_free_host(mmc);
  1585. +
  1586. + return ret;
  1587. +}
  1588. +
  1589. +static int bcm2835_mmc_remove(struct platform_device *pdev)
  1590. +{
  1591. + struct bcm2835_host *host = platform_get_drvdata(pdev);
  1592. + unsigned long flags;
  1593. + int dead;
  1594. + u32 scratch;
  1595. +
  1596. + dead = 0;
  1597. + scratch = bcm2835_mmc_readl(host, SDHCI_INT_STATUS);
  1598. + if (scratch == (u32)-1)
  1599. + dead = 1;
  1600. +
  1601. +
  1602. + if (dead) {
  1603. + spin_lock_irqsave(&host->lock, flags);
  1604. +
  1605. + host->flags |= SDHCI_DEVICE_DEAD;
  1606. +
  1607. + if (host->mrq) {
  1608. + pr_err("%s: Controller removed during "
  1609. + " transfer!\n", mmc_hostname(host->mmc));
  1610. +
  1611. + host->mrq->cmd->error = -ENOMEDIUM;
  1612. + tasklet_schedule(&host->finish_tasklet);
  1613. + }
  1614. +
  1615. + spin_unlock_irqrestore(&host->lock, flags);
  1616. + }
  1617. +
  1618. + mmc_remove_host(host->mmc);
  1619. +
  1620. + if (!dead)
  1621. + bcm2835_mmc_reset(host, SDHCI_RESET_ALL);
  1622. +
  1623. + free_irq(host->irq, host);
  1624. +
  1625. + del_timer_sync(&host->timer);
  1626. +
  1627. + tasklet_kill(&host->finish_tasklet);
  1628. +
  1629. + mmc_free_host(host->mmc);
  1630. + platform_set_drvdata(pdev, NULL);
  1631. +
  1632. + return 0;
  1633. +}
  1634. +
  1635. +
  1636. +static const struct of_device_id bcm2835_mmc_match[] = {
  1637. + { .compatible = "brcm,bcm2835-mmc" },
  1638. + { }
  1639. +};
  1640. +MODULE_DEVICE_TABLE(of, bcm2835_mmc_match);
  1641. +
  1642. +
  1643. +
  1644. +static struct platform_driver bcm2835_mmc_driver = {
  1645. + .probe = bcm2835_mmc_probe,
  1646. + .remove = bcm2835_mmc_remove,
  1647. + .driver = {
  1648. + .name = DRIVER_NAME,
  1649. + .owner = THIS_MODULE,
  1650. + .of_match_table = bcm2835_mmc_match,
  1651. + },
  1652. +};
  1653. +module_platform_driver(bcm2835_mmc_driver);
  1654. +
  1655. +module_param(mmc_debug, uint, 0644);
  1656. +module_param(mmc_debug2, uint, 0644);
  1657. +MODULE_ALIAS("platform:mmc-bcm2835");
  1658. +MODULE_DESCRIPTION("BCM2835 SDHCI driver");
  1659. +MODULE_LICENSE("GPL v2");
  1660. +MODULE_AUTHOR("Gellert Weisz");