950-0111-MMC-added-alternative-MMC-driver.patch 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018
  1. From c9b2c57e7fbfbebf73f96e58fb783bba83c50bd9 Mon Sep 17 00:00:00 2001
  2. From: gellert <[email protected]>
  3. Date: Fri, 15 Aug 2014 16:35:06 +0100
  4. Subject: [PATCH] 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 <[email protected]>
  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 <[email protected]>
  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 <[email protected]>
  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 <[email protected]>
  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 <[email protected]>
  43. bcm2835-mmc: Don't overwrite MMC capabilities from DT
  44. bcm2835-mmc: Don't override bus width capabilities from devicetree
  45. Take out the force setting of the MMC_CAP_4_BIT_DATA host capability
  46. so that the result read from devicetree via mmc_of_parse() is
  47. preserved.
  48. bcm2835-mmc: Only claim one DMA channel
  49. With both MMC controllers enabled there are few DMA channels left. The
  50. bcm2835-mmc driver only uses DMA in one direction at a time, so it
  51. doesn't need to claim two channels.
  52. See: https://github.com/raspberrypi/linux/issues/1327
  53. Signed-off-by: Phil Elwell <[email protected]>
  54. bcm2835-mmc: New timer API
  55. mmc: bcm2835-mmc: Support underclocking
  56. Support underclocking of the SD bus using the max-frequency DT property
  57. (which currently has no DT parameter). The sd_overclock parameter
  58. already provides another way to achieve the same thing which should be
  59. equivalent in end result, but it is a bug not to support max-frequency
  60. as well.
  61. See: https://github.com/raspberrypi/linux/issues/2350
  62. Signed-off-by: Phil Elwell <[email protected]>
  63. mmc/bcm2835: Recover from MMC_SEND_EXT_CSD
  64. If the user issues an "mmc extcsd read", the SD controller receives
  65. what it thinks is a SEND_IF_COND command with an unexpected data block.
  66. The resulting operations leave the FSM stuck in READWAIT, a state which
  67. persists until the MMC framework resets the controller, by which point
  68. the root filesystem is likely to have been unmounted.
  69. A less heavyweight solution is to detect the condition and nudge the
  70. FSM by asserting the (self-clearing) FORCE_DATA_MODE bit.
  71. N.B. This workaround was essentially discovered by accident and without
  72. a full understanding the inner workings of the controller, so it is
  73. fortunate that the "fix" only modifies error paths.
  74. See: https://github.com/raspberrypi/linux/issues/2728
  75. Signed-off-by: Phil Elwell <[email protected]>
  76. bcm2835-mmc: Fix DMA channel leak
  77. The BCM2835 MMC host driver requests a DMA channel on probe but neglects
  78. to release the channel in the probe error path and on driver unbind.
  79. I'm seeing this happen on every boot of the Compute Module 3: On first
  80. driver probe, DMA channel 2 is allocated and then leaked with a "could
  81. not get clk, deferring probe" message. On second driver probe, channel 4
  82. is allocated.
  83. Fix it.
  84. Signed-off-by: Lukas Wunner <[email protected]>
  85. Cc: Frank Pavlic <[email protected]>
  86. bcm2835-mmc: Fix struct mmc_host leak on probe
  87. The BCM2835 MMC host driver requests the bus address of the host's
  88. register map on probe. If that fails, the driver leaks the struct
  89. mmc_host allocated earlier.
  90. Fix it.
  91. Signed-off-by: Lukas Wunner <[email protected]>
  92. Cc: Frank Pavlic <[email protected]>
  93. bcm2835-mmc: Fix duplicate free_irq() on remove
  94. The BCM2835 MMC host driver requests its interrupt as a device-managed
  95. resource, so the interrupt is automatically freed after the driver is
  96. unbound.
  97. However on driver unbind, bcm2835_mmc_remove() frees the interrupt
  98. explicitly to avoid invocation of the interrupt handler after driver
  99. structures have been torn down.
  100. The interrupt is thus freed twice, leading to a WARN splat in
  101. __free_irq(). Fix by not requesting the interrupt as a device-managed
  102. resource.
  103. Signed-off-by: Lukas Wunner <[email protected]>
  104. Cc: Frank Pavlic <[email protected]>
  105. bcm2835-mmc: Handle mmc_add_host() errors
  106. The BCM2835 MMC host driver calls mmc_add_host() but doesn't check its
  107. return value. Errors occurring in that function are therefore not
  108. handled. Fix it.
  109. Signed-off-by: Lukas Wunner <[email protected]>
  110. Cc: Frank Pavlic <[email protected]>
  111. bcm2835-mmc: Deduplicate reset of driver data on remove
  112. The BCM2835 MMC host driver sets the device's driver data pointer to
  113. NULL on ->remove() even though the driver core subsequently does the
  114. same in __device_release_driver(). Drop the duplicate assignment.
  115. Signed-off-by: Lukas Wunner <[email protected]>
  116. Cc: Frank Pavlic <[email protected]>
  117. bcm2835_mmc: Remove vestigial threaded IRQ
  118. With SDIO processing now managed by the MMC framework with a
  119. workqueue, the bcm2835_mmc driver no longer needs a threaded
  120. IRQ.
  121. Signed-off-by: Phil Elwell <[email protected]>
  122. Add missing dma_unmap_sg calls to free relevant swiotlb bounce buffers.
  123. This prevents DMA leaks.
  124. Signed-off-by: Yaroslav Rosomakho <[email protected]>
  125. Limit max_req_size under arm64 (or any other platform that uses swiotlb) to prevent potential buffer overflow due to bouncing.
  126. Signed-off-by: Yaroslav Rosomakho <[email protected]>
  127. mmc: sdhci: Silence MMC warnings
  128. When the MMC isn't plugged in, the driver will spam the console which is
  129. pretty annoying when using NFS.
  130. Signed-off-by: Maxime Ripard <[email protected]>
  131. mmc: sdhci-iproc: Fix vmmc regulators on iProc
  132. The Linux support for controlling card power via regulators appears to
  133. be contentious. I would argue that the default behaviour is contrary to
  134. the SDHCI spec - turning off the power writes a reserved value to the
  135. SD Bus Voltage Select field of the Power Control Register, which
  136. seems to kill the Arasan/iProc controller - but fortunately there is a
  137. hook in sdhci_ops to override the behaviour. Borrow the implementation
  138. from sdhci_arasan_set_power.
  139. Signed-off-by: Phil Elwell <[email protected]>
  140. bcm2835-mmc: uninitialized_var is no more
  141. Revert "mmc: sdhci-iproc: Fix vmmc regulators on iProc"
  142. This reverts commit aed19399a01733dbad9be8bf026a4f7dd823b04f.
  143. Commit 6c92ae1e452f ("mmc: sdhci: Introduce sdhci_set_power_and_bus_voltage()")
  144. introduced a generic helper that does the same thing so use that instead in
  145. the following commit.
  146. Signed-off-by: Juerg Haefliger <[email protected]>
  147. mmc: sdhci-iproc: Fix vmmc regulators (pre-bcm2711)
  148. The Linux support for controlling card power via regulators appears to
  149. be contentious. I would argue that the default behaviour is contrary to
  150. the SDHCI spec - turning off the power writes a reserved value to the
  151. SD Bus Voltage Select field of the Power Control Register, which
  152. seems to kill the Arasan/iProc controller - but fortunately there is a
  153. hook in sdhci_ops to override the behaviour.
  154. Signed-off-by: Juerg Haefliger <[email protected]>
  155. Signed-off-by: Phil Elwell <[email protected]>
  156. ---
  157. drivers/mmc/core/block.c | 31 +-
  158. drivers/mmc/core/core.c | 3 +-
  159. drivers/mmc/core/quirks.h | 8 +
  160. drivers/mmc/host/Kconfig | 29 +
  161. drivers/mmc/host/Makefile | 1 +
  162. drivers/mmc/host/bcm2835-mmc.c | 1575 ++++++++++++++++++++++++++++++++
  163. drivers/mmc/host/sdhci-iproc.c | 1 +
  164. drivers/mmc/host/sdhci.c | 6 +-
  165. include/linux/mmc/card.h | 2 +
  166. 9 files changed, 1651 insertions(+), 5 deletions(-)
  167. create mode 100644 drivers/mmc/host/bcm2835-mmc.c
  168. --- a/drivers/mmc/core/block.c
  169. +++ b/drivers/mmc/core/block.c
  170. @@ -173,6 +173,13 @@ static DEFINE_MUTEX(open_lock);
  171. module_param(perdev_minors, int, 0444);
  172. MODULE_PARM_DESC(perdev_minors, "Minors numbers to allocate per device");
  173. +/*
  174. + * Allow quirks to be overridden for the current card
  175. + */
  176. +static char *card_quirks;
  177. +module_param(card_quirks, charp, 0644);
  178. +MODULE_PARM_DESC(card_quirks, "Force the use of the indicated quirks (a bitfield)");
  179. +
  180. static inline int mmc_blk_part_switch(struct mmc_card *card,
  181. unsigned int part_type);
  182. static void mmc_blk_rw_rq_prep(struct mmc_queue_req *mqrq,
  183. @@ -3000,6 +3007,8 @@ static int mmc_blk_probe(struct mmc_card
  184. {
  185. struct mmc_blk_data *md;
  186. int ret = 0;
  187. + char quirk_str[24];
  188. + char cap_str[10];
  189. /*
  190. * Check that the card supports the command class(es) we need.
  191. @@ -3007,7 +3016,16 @@ static int mmc_blk_probe(struct mmc_card
  192. if (!(card->csd.cmdclass & CCC_BLOCK_READ))
  193. return -ENODEV;
  194. - mmc_fixup_device(card, mmc_blk_fixups);
  195. + if (card_quirks) {
  196. + unsigned long quirks;
  197. + if (kstrtoul(card_quirks, 0, &quirks) == 0)
  198. + card->quirks = (unsigned int)quirks;
  199. + else
  200. + pr_err("mmc_block: Invalid card_quirks parameter '%s'\n",
  201. + card_quirks);
  202. + }
  203. + else
  204. + mmc_fixup_device(card, mmc_blk_fixups);
  205. card->complete_wq = alloc_workqueue("mmc_complete",
  206. WQ_MEM_RECLAIM | WQ_HIGHPRI, 0);
  207. @@ -3022,6 +3040,17 @@ static int mmc_blk_probe(struct mmc_card
  208. goto out_free;
  209. }
  210. + string_get_size((u64)get_capacity(md->disk), 512, STRING_UNITS_2,
  211. + cap_str, sizeof(cap_str));
  212. + if (card->quirks)
  213. + snprintf(quirk_str, sizeof(quirk_str),
  214. + " (quirks 0x%08x)", card->quirks);
  215. + else
  216. + quirk_str[0] = '\0';
  217. + pr_info("%s: %s %s %s%s%s\n",
  218. + md->disk->disk_name, mmc_card_id(card), mmc_card_name(card),
  219. + cap_str, md->read_only ? " (ro)" : "", quirk_str);
  220. +
  221. ret = mmc_blk_alloc_parts(card, md);
  222. if (ret)
  223. goto out;
  224. --- a/drivers/mmc/core/core.c
  225. +++ b/drivers/mmc/core/core.c
  226. @@ -1819,7 +1819,8 @@ EXPORT_SYMBOL(mmc_erase);
  227. int mmc_can_erase(struct mmc_card *card)
  228. {
  229. - if (card->csd.cmdclass & CCC_ERASE && card->erase_size)
  230. + if (card->csd.cmdclass & CCC_ERASE && card->erase_size &&
  231. + !(card->quirks & MMC_QUIRK_ERASE_BROKEN))
  232. return 1;
  233. return 0;
  234. }
  235. --- a/drivers/mmc/core/quirks.h
  236. +++ b/drivers/mmc/core/quirks.h
  237. @@ -130,6 +130,14 @@ static const struct mmc_fixup __maybe_un
  238. MMC_FIXUP(CID_NAME_ANY, CID_MANFID_SANDISK_SD, 0x5344, add_quirk_sd,
  239. MMC_QUIRK_BROKEN_SD_DISCARD),
  240. + /*
  241. + * On some Kingston SD cards, multiple erases of less than 64
  242. + * sectors can cause corruption.
  243. + */
  244. + MMC_FIXUP("SD16G", 0x41, 0x3432, add_quirk, MMC_QUIRK_ERASE_BROKEN),
  245. + MMC_FIXUP("SD32G", 0x41, 0x3432, add_quirk, MMC_QUIRK_ERASE_BROKEN),
  246. + MMC_FIXUP("SD64G", 0x41, 0x3432, add_quirk, MMC_QUIRK_ERASE_BROKEN),
  247. +
  248. END_FIXUP
  249. };
  250. --- a/drivers/mmc/host/Kconfig
  251. +++ b/drivers/mmc/host/Kconfig
  252. @@ -5,6 +5,35 @@
  253. comment "MMC/SD/SDIO Host Controller Drivers"
  254. +config MMC_BCM2835_MMC
  255. + tristate "MMC support on BCM2835"
  256. + depends on MACH_BCM2708 || MACH_BCM2709 || ARCH_BCM2835
  257. + help
  258. + This selects the MMC Interface on BCM2835.
  259. +
  260. + If you have a controller with this interface, say Y or M here.
  261. +
  262. + If unsure, say N.
  263. +
  264. +config MMC_BCM2835_DMA
  265. + bool "DMA support on BCM2835 Arasan controller"
  266. + depends on MMC_BCM2835_MMC
  267. + help
  268. + Enable DMA support on the Arasan SDHCI controller in Broadcom 2708
  269. + based chips.
  270. +
  271. + If unsure, say N.
  272. +
  273. +config MMC_BCM2835_PIO_DMA_BARRIER
  274. + int "Block count limit for PIO transfers"
  275. + depends on MMC_BCM2835_MMC && MMC_BCM2835_DMA
  276. + range 0 256
  277. + default 2
  278. + help
  279. + The inclusive limit in bytes under which PIO will be used instead of DMA
  280. +
  281. + If unsure, say 2 here.
  282. +
  283. config MMC_DEBUG
  284. bool "MMC host drivers debugging"
  285. depends on MMC != n
  286. --- a/drivers/mmc/host/Makefile
  287. +++ b/drivers/mmc/host/Makefile
  288. @@ -22,6 +22,7 @@ obj-$(CONFIG_MMC_SDHCI_F_SDH30) += sdhci
  289. obj-$(CONFIG_MMC_SDHCI_MILBEAUT) += sdhci-milbeaut.o
  290. obj-$(CONFIG_MMC_SDHCI_SPEAR) += sdhci-spear.o
  291. obj-$(CONFIG_MMC_SDHCI_AM654) += sdhci_am654.o
  292. +obj-$(CONFIG_MMC_BCM2835_MMC) += bcm2835-mmc.o
  293. obj-$(CONFIG_MMC_WBSD) += wbsd.o
  294. obj-$(CONFIG_MMC_AU1X) += au1xmmc.o
  295. obj-$(CONFIG_MMC_ALCOR) += alcor.o
  296. --- /dev/null
  297. +++ b/drivers/mmc/host/bcm2835-mmc.c
  298. @@ -0,0 +1,1575 @@
  299. +/*
  300. + * BCM2835 MMC host driver.
  301. + *
  302. + * Author: Gellert Weisz <[email protected]>
  303. + * Copyright 2014
  304. + *
  305. + * Based on
  306. + * sdhci-bcm2708.c by Broadcom
  307. + * sdhci-bcm2835.c by Stephen Warren and Oleksandr Tymoshenko
  308. + * sdhci.c and sdhci-pci.c by Pierre Ossman
  309. + *
  310. + * This program is free software; you can redistribute it and/or modify it
  311. + * under the terms and conditions of the GNU General Public License,
  312. + * version 2, as published by the Free Software Foundation.
  313. + *
  314. + * This program is distributed in the hope it will be useful, but WITHOUT
  315. + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  316. + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  317. + * more details.
  318. + *
  319. + * You should have received a copy of the GNU General Public License
  320. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  321. + */
  322. +
  323. +#include <linux/delay.h>
  324. +#include <linux/module.h>
  325. +#include <linux/io.h>
  326. +#include <linux/mmc/mmc.h>
  327. +#include <linux/mmc/host.h>
  328. +#include <linux/mmc/sd.h>
  329. +#include <linux/scatterlist.h>
  330. +#include <linux/of_address.h>
  331. +#include <linux/of_irq.h>
  332. +#include <linux/clk.h>
  333. +#include <linux/platform_device.h>
  334. +#include <linux/err.h>
  335. +#include <linux/blkdev.h>
  336. +#include <linux/dmaengine.h>
  337. +#include <linux/dma-mapping.h>
  338. +#include <linux/of_dma.h>
  339. +#include <linux/swiotlb.h>
  340. +
  341. +#include "sdhci.h"
  342. +
  343. +
  344. +#define DRIVER_NAME "mmc-bcm2835"
  345. +
  346. +#define DBG(f, x...) \
  347. +pr_debug(DRIVER_NAME " [%s()]: " f, __func__, ## x)
  348. +
  349. +#ifndef CONFIG_MMC_BCM2835_DMA
  350. + #define FORCE_PIO
  351. +#endif
  352. +
  353. +
  354. +/* the inclusive limit in bytes under which PIO will be used instead of DMA */
  355. +#ifdef CONFIG_MMC_BCM2835_PIO_DMA_BARRIER
  356. +#define PIO_DMA_BARRIER CONFIG_MMC_BCM2835_PIO_DMA_BARRIER
  357. +#else
  358. +#define PIO_DMA_BARRIER 00
  359. +#endif
  360. +
  361. +#define MIN_FREQ 400000
  362. +#define TIMEOUT_VAL 0xE
  363. +#define BCM2835_SDHCI_WRITE_DELAY(f) (((2 * 1000000) / f) + 1)
  364. +
  365. +
  366. +unsigned mmc_debug;
  367. +unsigned mmc_debug2;
  368. +
  369. +struct bcm2835_host {
  370. + spinlock_t lock;
  371. +
  372. + void __iomem *ioaddr;
  373. + u32 bus_addr;
  374. +
  375. + struct mmc_host *mmc;
  376. +
  377. + u32 timeout;
  378. +
  379. + int clock; /* Current clock speed */
  380. + u8 pwr; /* Current voltage */
  381. +
  382. + unsigned int max_clk; /* Max possible freq */
  383. + unsigned int timeout_clk; /* Timeout freq (KHz) */
  384. + unsigned int clk_mul; /* Clock Muliplier value */
  385. +
  386. + struct tasklet_struct finish_tasklet; /* Tasklet structures */
  387. +
  388. + struct timer_list timer; /* Timer for timeouts */
  389. +
  390. + struct sg_mapping_iter sg_miter; /* SG state for PIO */
  391. + unsigned int blocks; /* remaining PIO blocks */
  392. +
  393. + int irq; /* Device IRQ */
  394. +
  395. +
  396. + u32 ier; /* cached registers */
  397. +
  398. + struct mmc_request *mrq; /* Current request */
  399. + struct mmc_command *cmd; /* Current command */
  400. + struct mmc_data *data; /* Current data request */
  401. + unsigned int data_early:1; /* Data finished before cmd */
  402. +
  403. + wait_queue_head_t buf_ready_int; /* Waitqueue for Buffer Read Ready interrupt */
  404. +
  405. + u32 shadow;
  406. +
  407. + /*DMA part*/
  408. + struct dma_chan *dma_chan_rxtx; /* DMA channel for reads and writes */
  409. + struct dma_slave_config dma_cfg_rx;
  410. + struct dma_slave_config dma_cfg_tx;
  411. + struct dma_async_tx_descriptor *tx_desc; /* descriptor */
  412. +
  413. + bool have_dma;
  414. + bool use_dma;
  415. + bool wait_for_dma;
  416. + /*end of DMA part*/
  417. +
  418. + int max_delay; /* maximum length of time spent waiting */
  419. +
  420. + int flags; /* Host attributes */
  421. +#define SDHCI_REQ_USE_DMA (1<<2) /* Use DMA for this req. */
  422. +#define SDHCI_DEVICE_DEAD (1<<3) /* Device unresponsive */
  423. +#define SDHCI_AUTO_CMD12 (1<<6) /* Auto CMD12 support */
  424. +#define SDHCI_AUTO_CMD23 (1<<7) /* Auto CMD23 support */
  425. +#define SDHCI_SDIO_IRQ_ENABLED (1<<9) /* SDIO irq enabled */
  426. +
  427. + u32 overclock_50; /* frequency to use when 50MHz is requested (in MHz) */
  428. + u32 max_overclock; /* Highest reported */
  429. +};
  430. +
  431. +
  432. +static inline void bcm2835_mmc_writel(struct bcm2835_host *host, u32 val, int reg, int from)
  433. +{
  434. + unsigned delay;
  435. + lockdep_assert_held_once(&host->lock);
  436. + writel(val, host->ioaddr + reg);
  437. + udelay(BCM2835_SDHCI_WRITE_DELAY(max(host->clock, MIN_FREQ)));
  438. +
  439. + delay = ((mmc_debug >> 16) & 0xf) << ((mmc_debug >> 20) & 0xf);
  440. + if (delay && !((1<<from) & mmc_debug2))
  441. + udelay(delay);
  442. +}
  443. +
  444. +static inline void mmc_raw_writel(struct bcm2835_host *host, u32 val, int reg)
  445. +{
  446. + unsigned delay;
  447. + lockdep_assert_held_once(&host->lock);
  448. + writel(val, host->ioaddr + reg);
  449. +
  450. + delay = ((mmc_debug >> 24) & 0xf) << ((mmc_debug >> 28) & 0xf);
  451. + if (delay)
  452. + udelay(delay);
  453. +}
  454. +
  455. +static inline u32 bcm2835_mmc_readl(struct bcm2835_host *host, int reg)
  456. +{
  457. + lockdep_assert_held_once(&host->lock);
  458. + return readl(host->ioaddr + reg);
  459. +}
  460. +
  461. +static inline void bcm2835_mmc_writew(struct bcm2835_host *host, u16 val, int reg)
  462. +{
  463. + u32 oldval = (reg == SDHCI_COMMAND) ? host->shadow :
  464. + bcm2835_mmc_readl(host, reg & ~3);
  465. + u32 word_num = (reg >> 1) & 1;
  466. + u32 word_shift = word_num * 16;
  467. + u32 mask = 0xffff << word_shift;
  468. + u32 newval = (oldval & ~mask) | (val << word_shift);
  469. +
  470. + if (reg == SDHCI_TRANSFER_MODE)
  471. + host->shadow = newval;
  472. + else
  473. + bcm2835_mmc_writel(host, newval, reg & ~3, 0);
  474. +
  475. +}
  476. +
  477. +static inline void bcm2835_mmc_writeb(struct bcm2835_host *host, u8 val, int reg)
  478. +{
  479. + u32 oldval = bcm2835_mmc_readl(host, reg & ~3);
  480. + u32 byte_num = reg & 3;
  481. + u32 byte_shift = byte_num * 8;
  482. + u32 mask = 0xff << byte_shift;
  483. + u32 newval = (oldval & ~mask) | (val << byte_shift);
  484. +
  485. + bcm2835_mmc_writel(host, newval, reg & ~3, 1);
  486. +}
  487. +
  488. +
  489. +static inline u16 bcm2835_mmc_readw(struct bcm2835_host *host, int reg)
  490. +{
  491. + u32 val = bcm2835_mmc_readl(host, (reg & ~3));
  492. + u32 word_num = (reg >> 1) & 1;
  493. + u32 word_shift = word_num * 16;
  494. + u32 word = (val >> word_shift) & 0xffff;
  495. +
  496. + return word;
  497. +}
  498. +
  499. +static inline u8 bcm2835_mmc_readb(struct bcm2835_host *host, int reg)
  500. +{
  501. + u32 val = bcm2835_mmc_readl(host, (reg & ~3));
  502. + u32 byte_num = reg & 3;
  503. + u32 byte_shift = byte_num * 8;
  504. + u32 byte = (val >> byte_shift) & 0xff;
  505. +
  506. + return byte;
  507. +}
  508. +
  509. +static void bcm2835_mmc_unsignal_irqs(struct bcm2835_host *host, u32 clear)
  510. +{
  511. + u32 ier;
  512. +
  513. + ier = bcm2835_mmc_readl(host, SDHCI_SIGNAL_ENABLE);
  514. + ier &= ~clear;
  515. + /* change which requests generate IRQs - makes no difference to
  516. + the content of SDHCI_INT_STATUS, or the need to acknowledge IRQs */
  517. + bcm2835_mmc_writel(host, ier, SDHCI_SIGNAL_ENABLE, 2);
  518. +}
  519. +
  520. +
  521. +static void bcm2835_mmc_dumpregs(struct bcm2835_host *host)
  522. +{
  523. + pr_debug(DRIVER_NAME ": =========== REGISTER DUMP (%s)===========\n",
  524. + mmc_hostname(host->mmc));
  525. +
  526. + pr_debug(DRIVER_NAME ": Sys addr: 0x%08x | Version: 0x%08x\n",
  527. + bcm2835_mmc_readl(host, SDHCI_DMA_ADDRESS),
  528. + bcm2835_mmc_readw(host, SDHCI_HOST_VERSION));
  529. + pr_debug(DRIVER_NAME ": Blk size: 0x%08x | Blk cnt: 0x%08x\n",
  530. + bcm2835_mmc_readw(host, SDHCI_BLOCK_SIZE),
  531. + bcm2835_mmc_readw(host, SDHCI_BLOCK_COUNT));
  532. + pr_debug(DRIVER_NAME ": Argument: 0x%08x | Trn mode: 0x%08x\n",
  533. + bcm2835_mmc_readl(host, SDHCI_ARGUMENT),
  534. + bcm2835_mmc_readw(host, SDHCI_TRANSFER_MODE));
  535. + pr_debug(DRIVER_NAME ": Present: 0x%08x | Host ctl: 0x%08x\n",
  536. + bcm2835_mmc_readl(host, SDHCI_PRESENT_STATE),
  537. + bcm2835_mmc_readb(host, SDHCI_HOST_CONTROL));
  538. + pr_debug(DRIVER_NAME ": Power: 0x%08x | Blk gap: 0x%08x\n",
  539. + bcm2835_mmc_readb(host, SDHCI_POWER_CONTROL),
  540. + bcm2835_mmc_readb(host, SDHCI_BLOCK_GAP_CONTROL));
  541. + pr_debug(DRIVER_NAME ": Wake-up: 0x%08x | Clock: 0x%08x\n",
  542. + bcm2835_mmc_readb(host, SDHCI_WAKE_UP_CONTROL),
  543. + bcm2835_mmc_readw(host, SDHCI_CLOCK_CONTROL));
  544. + pr_debug(DRIVER_NAME ": Timeout: 0x%08x | Int stat: 0x%08x\n",
  545. + bcm2835_mmc_readb(host, SDHCI_TIMEOUT_CONTROL),
  546. + bcm2835_mmc_readl(host, SDHCI_INT_STATUS));
  547. + pr_debug(DRIVER_NAME ": Int enab: 0x%08x | Sig enab: 0x%08x\n",
  548. + bcm2835_mmc_readl(host, SDHCI_INT_ENABLE),
  549. + bcm2835_mmc_readl(host, SDHCI_SIGNAL_ENABLE));
  550. + pr_debug(DRIVER_NAME ": AC12 err: 0x%08x | Slot int: 0x%08x\n",
  551. + bcm2835_mmc_readw(host, SDHCI_AUTO_CMD_STATUS),
  552. + bcm2835_mmc_readw(host, SDHCI_SLOT_INT_STATUS));
  553. + pr_debug(DRIVER_NAME ": Caps: 0x%08x | Caps_1: 0x%08x\n",
  554. + bcm2835_mmc_readl(host, SDHCI_CAPABILITIES),
  555. + bcm2835_mmc_readl(host, SDHCI_CAPABILITIES_1));
  556. + pr_debug(DRIVER_NAME ": Cmd: 0x%08x | Max curr: 0x%08x\n",
  557. + bcm2835_mmc_readw(host, SDHCI_COMMAND),
  558. + bcm2835_mmc_readl(host, SDHCI_MAX_CURRENT));
  559. + pr_debug(DRIVER_NAME ": Host ctl2: 0x%08x\n",
  560. + bcm2835_mmc_readw(host, SDHCI_HOST_CONTROL2));
  561. +
  562. + pr_debug(DRIVER_NAME ": ===========================================\n");
  563. +}
  564. +
  565. +
  566. +static void bcm2835_mmc_reset(struct bcm2835_host *host, u8 mask)
  567. +{
  568. + unsigned long timeout;
  569. + unsigned long flags;
  570. +
  571. + spin_lock_irqsave(&host->lock, flags);
  572. + bcm2835_mmc_writeb(host, mask, SDHCI_SOFTWARE_RESET);
  573. +
  574. + if (mask & SDHCI_RESET_ALL)
  575. + host->clock = 0;
  576. +
  577. + /* Wait max 100 ms */
  578. + timeout = 100;
  579. +
  580. + /* hw clears the bit when it's done */
  581. + while (bcm2835_mmc_readb(host, SDHCI_SOFTWARE_RESET) & mask) {
  582. + if (timeout == 0) {
  583. + pr_err("%s: Reset 0x%x never completed.\n",
  584. + mmc_hostname(host->mmc), (int)mask);
  585. + bcm2835_mmc_dumpregs(host);
  586. + return;
  587. + }
  588. + timeout--;
  589. + spin_unlock_irqrestore(&host->lock, flags);
  590. + mdelay(1);
  591. + spin_lock_irqsave(&host->lock, flags);
  592. + }
  593. +
  594. + if (100-timeout > 10 && 100-timeout > host->max_delay) {
  595. + host->max_delay = 100-timeout;
  596. + pr_warn("Warning: MMC controller hung for %d ms\n", host->max_delay);
  597. + }
  598. + spin_unlock_irqrestore(&host->lock, flags);
  599. +}
  600. +
  601. +static void bcm2835_mmc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios);
  602. +
  603. +static void bcm2835_mmc_init(struct bcm2835_host *host, int soft)
  604. +{
  605. + unsigned long flags;
  606. + if (soft)
  607. + bcm2835_mmc_reset(host, SDHCI_RESET_CMD|SDHCI_RESET_DATA);
  608. + else
  609. + bcm2835_mmc_reset(host, SDHCI_RESET_ALL);
  610. +
  611. + host->ier = SDHCI_INT_BUS_POWER | SDHCI_INT_DATA_END_BIT |
  612. + SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_TIMEOUT |
  613. + SDHCI_INT_INDEX | SDHCI_INT_END_BIT | SDHCI_INT_CRC |
  614. + SDHCI_INT_TIMEOUT | SDHCI_INT_DATA_END |
  615. + SDHCI_INT_RESPONSE;
  616. +
  617. + spin_lock_irqsave(&host->lock, flags);
  618. + bcm2835_mmc_writel(host, host->ier, SDHCI_INT_ENABLE, 3);
  619. + bcm2835_mmc_writel(host, host->ier, SDHCI_SIGNAL_ENABLE, 3);
  620. + spin_unlock_irqrestore(&host->lock, flags);
  621. +
  622. + if (soft) {
  623. + /* force clock reconfiguration */
  624. + host->clock = 0;
  625. + bcm2835_mmc_set_ios(host->mmc, &host->mmc->ios);
  626. + }
  627. +}
  628. +
  629. +
  630. +
  631. +static void bcm2835_mmc_finish_data(struct bcm2835_host *host);
  632. +
  633. +static void bcm2835_mmc_dma_complete(void *param)
  634. +{
  635. + struct bcm2835_host *host = param;
  636. + struct dma_chan *dma_chan;
  637. + unsigned long flags;
  638. + u32 dir_data;
  639. +
  640. + spin_lock_irqsave(&host->lock, flags);
  641. +
  642. + host->use_dma = false;
  643. +
  644. + if (host->data) {
  645. + dma_chan = host->dma_chan_rxtx;
  646. + if (host->data->flags & MMC_DATA_WRITE)
  647. + dir_data = DMA_TO_DEVICE;
  648. + else
  649. + dir_data = DMA_FROM_DEVICE;
  650. + dma_unmap_sg(dma_chan->device->dev,
  651. + host->data->sg, host->data->sg_len,
  652. + dir_data);
  653. + if (! (host->data->flags & MMC_DATA_WRITE))
  654. + bcm2835_mmc_finish_data(host);
  655. + } else if (host->wait_for_dma) {
  656. + host->wait_for_dma = false;
  657. + tasklet_schedule(&host->finish_tasklet);
  658. + }
  659. +
  660. + spin_unlock_irqrestore(&host->lock, flags);
  661. +}
  662. +
  663. +static void bcm2835_bcm2835_mmc_read_block_pio(struct bcm2835_host *host)
  664. +{
  665. + unsigned long flags;
  666. + size_t blksize, len, chunk;
  667. +
  668. + u32 scratch = 0;
  669. + u8 *buf;
  670. +
  671. + blksize = host->data->blksz;
  672. + chunk = 0;
  673. +
  674. + local_irq_save(flags);
  675. +
  676. + while (blksize) {
  677. + if (!sg_miter_next(&host->sg_miter))
  678. + BUG();
  679. +
  680. + len = min(host->sg_miter.length, blksize);
  681. +
  682. + blksize -= len;
  683. + host->sg_miter.consumed = len;
  684. +
  685. + buf = host->sg_miter.addr;
  686. +
  687. + while (len) {
  688. + if (chunk == 0) {
  689. + scratch = bcm2835_mmc_readl(host, SDHCI_BUFFER);
  690. + chunk = 4;
  691. + }
  692. +
  693. + *buf = scratch & 0xFF;
  694. +
  695. + buf++;
  696. + scratch >>= 8;
  697. + chunk--;
  698. + len--;
  699. + }
  700. + }
  701. +
  702. + sg_miter_stop(&host->sg_miter);
  703. +
  704. + local_irq_restore(flags);
  705. +}
  706. +
  707. +static void bcm2835_bcm2835_mmc_write_block_pio(struct bcm2835_host *host)
  708. +{
  709. + unsigned long flags;
  710. + size_t blksize, len, chunk;
  711. + u32 scratch;
  712. + u8 *buf;
  713. +
  714. + blksize = host->data->blksz;
  715. + chunk = 0;
  716. + chunk = 0;
  717. + scratch = 0;
  718. +
  719. + local_irq_save(flags);
  720. +
  721. + while (blksize) {
  722. + if (!sg_miter_next(&host->sg_miter))
  723. + BUG();
  724. +
  725. + len = min(host->sg_miter.length, blksize);
  726. +
  727. + blksize -= len;
  728. + host->sg_miter.consumed = len;
  729. +
  730. + buf = host->sg_miter.addr;
  731. +
  732. + while (len) {
  733. + scratch |= (u32)*buf << (chunk * 8);
  734. +
  735. + buf++;
  736. + chunk++;
  737. + len--;
  738. +
  739. + if ((chunk == 4) || ((len == 0) && (blksize == 0))) {
  740. + mmc_raw_writel(host, scratch, SDHCI_BUFFER);
  741. + chunk = 0;
  742. + scratch = 0;
  743. + }
  744. + }
  745. + }
  746. +
  747. + sg_miter_stop(&host->sg_miter);
  748. +
  749. + local_irq_restore(flags);
  750. +}
  751. +
  752. +
  753. +static void bcm2835_mmc_transfer_pio(struct bcm2835_host *host)
  754. +{
  755. + u32 mask;
  756. +
  757. + BUG_ON(!host->data);
  758. +
  759. + if (host->blocks == 0)
  760. + return;
  761. +
  762. + if (host->data->flags & MMC_DATA_READ)
  763. + mask = SDHCI_DATA_AVAILABLE;
  764. + else
  765. + mask = SDHCI_SPACE_AVAILABLE;
  766. +
  767. + while (bcm2835_mmc_readl(host, SDHCI_PRESENT_STATE) & mask) {
  768. +
  769. + if (host->data->flags & MMC_DATA_READ)
  770. + bcm2835_bcm2835_mmc_read_block_pio(host);
  771. + else
  772. + bcm2835_bcm2835_mmc_write_block_pio(host);
  773. +
  774. + host->blocks--;
  775. +
  776. + /* QUIRK used in sdhci.c removes the 'if' */
  777. + /* but it seems this is unnecessary */
  778. + if (host->blocks == 0)
  779. + break;
  780. +
  781. +
  782. + }
  783. +}
  784. +
  785. +
  786. +static void bcm2835_mmc_transfer_dma(struct bcm2835_host *host)
  787. +{
  788. + u32 len, dir_data, dir_slave;
  789. + struct dma_async_tx_descriptor *desc = NULL;
  790. + struct dma_chan *dma_chan;
  791. +
  792. +
  793. + WARN_ON(!host->data);
  794. +
  795. + if (!host->data)
  796. + return;
  797. +
  798. + if (host->blocks == 0)
  799. + return;
  800. +
  801. + dma_chan = host->dma_chan_rxtx;
  802. + if (host->data->flags & MMC_DATA_READ) {
  803. + dir_data = DMA_FROM_DEVICE;
  804. + dir_slave = DMA_DEV_TO_MEM;
  805. + } else {
  806. + dir_data = DMA_TO_DEVICE;
  807. + dir_slave = DMA_MEM_TO_DEV;
  808. + }
  809. +
  810. + /* The parameters have already been validated, so this will not fail */
  811. + (void)dmaengine_slave_config(dma_chan,
  812. + (dir_data == DMA_FROM_DEVICE) ?
  813. + &host->dma_cfg_rx :
  814. + &host->dma_cfg_tx);
  815. +
  816. + BUG_ON(!dma_chan->device);
  817. + BUG_ON(!dma_chan->device->dev);
  818. + BUG_ON(!host->data->sg);
  819. +
  820. + len = dma_map_sg(dma_chan->device->dev, host->data->sg,
  821. + host->data->sg_len, dir_data);
  822. + if (len > 0) {
  823. + desc = dmaengine_prep_slave_sg(dma_chan, host->data->sg,
  824. + len, dir_slave,
  825. + DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  826. + } else {
  827. + dev_err(mmc_dev(host->mmc), "dma_map_sg returned zero length\n");
  828. + }
  829. + if (desc) {
  830. + unsigned long flags;
  831. + spin_lock_irqsave(&host->lock, flags);
  832. + bcm2835_mmc_unsignal_irqs(host, SDHCI_INT_DATA_AVAIL |
  833. + SDHCI_INT_SPACE_AVAIL);
  834. + host->tx_desc = desc;
  835. + desc->callback = bcm2835_mmc_dma_complete;
  836. + desc->callback_param = host;
  837. + spin_unlock_irqrestore(&host->lock, flags);
  838. + dmaengine_submit(desc);
  839. + dma_async_issue_pending(dma_chan);
  840. + } else {
  841. + dma_unmap_sg(dma_chan->device->dev, host->data->sg, len, dir_data);
  842. + }
  843. +
  844. +}
  845. +
  846. +
  847. +
  848. +static void bcm2835_mmc_set_transfer_irqs(struct bcm2835_host *host)
  849. +{
  850. + u32 pio_irqs = SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL;
  851. + u32 dma_irqs = SDHCI_INT_DMA_END | SDHCI_INT_ADMA_ERROR;
  852. +
  853. + if (host->use_dma)
  854. + host->ier = (host->ier & ~pio_irqs) | dma_irqs;
  855. + else
  856. + host->ier = (host->ier & ~dma_irqs) | pio_irqs;
  857. +
  858. + bcm2835_mmc_writel(host, host->ier, SDHCI_INT_ENABLE, 4);
  859. + bcm2835_mmc_writel(host, host->ier, SDHCI_SIGNAL_ENABLE, 4);
  860. +}
  861. +
  862. +
  863. +static void bcm2835_mmc_prepare_data(struct bcm2835_host *host, struct mmc_command *cmd)
  864. +{
  865. + u8 count;
  866. + struct mmc_data *data = cmd->data;
  867. +
  868. + WARN_ON(host->data);
  869. +
  870. + if (data || (cmd->flags & MMC_RSP_BUSY)) {
  871. + count = TIMEOUT_VAL;
  872. + bcm2835_mmc_writeb(host, count, SDHCI_TIMEOUT_CONTROL);
  873. + }
  874. +
  875. + if (!data)
  876. + return;
  877. +
  878. + /* Sanity checks */
  879. + BUG_ON(data->blksz * data->blocks > 524288);
  880. + BUG_ON(data->blksz > host->mmc->max_blk_size);
  881. + BUG_ON(data->blocks > 65535);
  882. +
  883. + host->data = data;
  884. + host->data_early = 0;
  885. + host->data->bytes_xfered = 0;
  886. +
  887. +
  888. + if (!(host->flags & SDHCI_REQ_USE_DMA)) {
  889. + int flags;
  890. +
  891. + flags = SG_MITER_ATOMIC;
  892. + if (host->data->flags & MMC_DATA_READ)
  893. + flags |= SG_MITER_TO_SG;
  894. + else
  895. + flags |= SG_MITER_FROM_SG;
  896. + sg_miter_start(&host->sg_miter, data->sg, data->sg_len, flags);
  897. + host->blocks = data->blocks;
  898. + }
  899. +
  900. + host->use_dma = host->have_dma && data->blocks > PIO_DMA_BARRIER;
  901. +
  902. + bcm2835_mmc_set_transfer_irqs(host);
  903. +
  904. + /* Set the DMA boundary value and block size */
  905. + bcm2835_mmc_writew(host, SDHCI_MAKE_BLKSZ(SDHCI_DEFAULT_BOUNDARY_ARG,
  906. + data->blksz), SDHCI_BLOCK_SIZE);
  907. + bcm2835_mmc_writew(host, data->blocks, SDHCI_BLOCK_COUNT);
  908. +
  909. + BUG_ON(!host->data);
  910. +}
  911. +
  912. +static void bcm2835_mmc_set_transfer_mode(struct bcm2835_host *host,
  913. + struct mmc_command *cmd)
  914. +{
  915. + u16 mode;
  916. + struct mmc_data *data = cmd->data;
  917. +
  918. + if (data == NULL) {
  919. + /* clear Auto CMD settings for no data CMDs */
  920. + mode = bcm2835_mmc_readw(host, SDHCI_TRANSFER_MODE);
  921. + bcm2835_mmc_writew(host, mode & ~(SDHCI_TRNS_AUTO_CMD12 |
  922. + SDHCI_TRNS_AUTO_CMD23), SDHCI_TRANSFER_MODE);
  923. + return;
  924. + }
  925. +
  926. + WARN_ON(!host->data);
  927. +
  928. + mode = SDHCI_TRNS_BLK_CNT_EN;
  929. +
  930. + if ((mmc_op_multi(cmd->opcode) || data->blocks > 1)) {
  931. + mode |= SDHCI_TRNS_MULTI;
  932. +
  933. + /*
  934. + * If we are sending CMD23, CMD12 never gets sent
  935. + * on successful completion (so no Auto-CMD12).
  936. + */
  937. + if (!host->mrq->sbc && (host->flags & SDHCI_AUTO_CMD12))
  938. + mode |= SDHCI_TRNS_AUTO_CMD12;
  939. + else if (host->mrq->sbc && (host->flags & SDHCI_AUTO_CMD23)) {
  940. + mode |= SDHCI_TRNS_AUTO_CMD23;
  941. + bcm2835_mmc_writel(host, host->mrq->sbc->arg, SDHCI_ARGUMENT2, 5);
  942. + }
  943. + }
  944. +
  945. + if (data->flags & MMC_DATA_READ)
  946. + mode |= SDHCI_TRNS_READ;
  947. + if (host->flags & SDHCI_REQ_USE_DMA)
  948. + mode |= SDHCI_TRNS_DMA;
  949. +
  950. + bcm2835_mmc_writew(host, mode, SDHCI_TRANSFER_MODE);
  951. +}
  952. +
  953. +void bcm2835_mmc_send_command(struct bcm2835_host *host, struct mmc_command *cmd)
  954. +{
  955. + int flags;
  956. + u32 mask;
  957. + unsigned long timeout;
  958. +
  959. + WARN_ON(host->cmd);
  960. +
  961. + /* Wait max 10 ms */
  962. + timeout = 1000;
  963. +
  964. + mask = SDHCI_CMD_INHIBIT;
  965. + if ((cmd->data != NULL) || (cmd->flags & MMC_RSP_BUSY))
  966. + mask |= SDHCI_DATA_INHIBIT;
  967. +
  968. + /* We shouldn't wait for data inihibit for stop commands, even
  969. + though they might use busy signaling */
  970. + if (host->mrq->data && (cmd == host->mrq->data->stop))
  971. + mask &= ~SDHCI_DATA_INHIBIT;
  972. +
  973. + while (bcm2835_mmc_readl(host, SDHCI_PRESENT_STATE) & mask) {
  974. + if (timeout == 0) {
  975. + pr_err("%s: Controller never released inhibit bit(s).\n",
  976. + mmc_hostname(host->mmc));
  977. + bcm2835_mmc_dumpregs(host);
  978. + cmd->error = -EIO;
  979. + tasklet_schedule(&host->finish_tasklet);
  980. + return;
  981. + }
  982. + timeout--;
  983. + udelay(10);
  984. + }
  985. +
  986. + if ((1000-timeout)/100 > 1 && (1000-timeout)/100 > host->max_delay) {
  987. + host->max_delay = (1000-timeout)/100;
  988. + pr_warn("Warning: MMC controller hung for %d ms\n", host->max_delay);
  989. + }
  990. +
  991. + timeout = jiffies;
  992. + if (!cmd->data && cmd->busy_timeout > 9000)
  993. + timeout += DIV_ROUND_UP(cmd->busy_timeout, 1000) * HZ + HZ;
  994. + else
  995. + timeout += 10 * HZ;
  996. + mod_timer(&host->timer, timeout);
  997. +
  998. + host->cmd = cmd;
  999. + host->use_dma = false;
  1000. +
  1001. + bcm2835_mmc_prepare_data(host, cmd);
  1002. +
  1003. + bcm2835_mmc_writel(host, cmd->arg, SDHCI_ARGUMENT, 6);
  1004. +
  1005. + bcm2835_mmc_set_transfer_mode(host, cmd);
  1006. +
  1007. + if ((cmd->flags & MMC_RSP_136) && (cmd->flags & MMC_RSP_BUSY)) {
  1008. + pr_err("%s: Unsupported response type!\n",
  1009. + mmc_hostname(host->mmc));
  1010. + cmd->error = -EINVAL;
  1011. + tasklet_schedule(&host->finish_tasklet);
  1012. + return;
  1013. + }
  1014. +
  1015. + if (!(cmd->flags & MMC_RSP_PRESENT))
  1016. + flags = SDHCI_CMD_RESP_NONE;
  1017. + else if (cmd->flags & MMC_RSP_136)
  1018. + flags = SDHCI_CMD_RESP_LONG;
  1019. + else if (cmd->flags & MMC_RSP_BUSY)
  1020. + flags = SDHCI_CMD_RESP_SHORT_BUSY;
  1021. + else
  1022. + flags = SDHCI_CMD_RESP_SHORT;
  1023. +
  1024. + if (cmd->flags & MMC_RSP_CRC)
  1025. + flags |= SDHCI_CMD_CRC;
  1026. + if (cmd->flags & MMC_RSP_OPCODE)
  1027. + flags |= SDHCI_CMD_INDEX;
  1028. +
  1029. + if (cmd->data)
  1030. + flags |= SDHCI_CMD_DATA;
  1031. +
  1032. + bcm2835_mmc_writew(host, SDHCI_MAKE_CMD(cmd->opcode, flags), SDHCI_COMMAND);
  1033. +}
  1034. +
  1035. +
  1036. +static void bcm2835_mmc_finish_data(struct bcm2835_host *host)
  1037. +{
  1038. + struct mmc_data *data;
  1039. +
  1040. + BUG_ON(!host->data);
  1041. +
  1042. + data = host->data;
  1043. + host->data = NULL;
  1044. +
  1045. + if (data->error)
  1046. + data->bytes_xfered = 0;
  1047. + else
  1048. + data->bytes_xfered = data->blksz * data->blocks;
  1049. +
  1050. + /*
  1051. + * Need to send CMD12 if -
  1052. + * a) open-ended multiblock transfer (no CMD23)
  1053. + * b) error in multiblock transfer
  1054. + */
  1055. + if (data->stop &&
  1056. + (data->error ||
  1057. + !host->mrq->sbc)) {
  1058. +
  1059. + /*
  1060. + * The controller needs a reset of internal state machines
  1061. + * upon error conditions.
  1062. + */
  1063. + if (data->error) {
  1064. + bcm2835_mmc_reset(host, SDHCI_RESET_CMD);
  1065. + bcm2835_mmc_reset(host, SDHCI_RESET_DATA);
  1066. + }
  1067. +
  1068. + bcm2835_mmc_send_command(host, data->stop);
  1069. + } else if (host->use_dma) {
  1070. + host->wait_for_dma = true;
  1071. + } else {
  1072. + tasklet_schedule(&host->finish_tasklet);
  1073. + }
  1074. +}
  1075. +
  1076. +static void bcm2835_mmc_finish_command(struct bcm2835_host *host)
  1077. +{
  1078. + int i;
  1079. +
  1080. + BUG_ON(host->cmd == NULL);
  1081. +
  1082. + if (host->cmd->flags & MMC_RSP_PRESENT) {
  1083. + if (host->cmd->flags & MMC_RSP_136) {
  1084. + /* CRC is stripped so we need to do some shifting. */
  1085. + for (i = 0; i < 4; i++) {
  1086. + host->cmd->resp[i] = bcm2835_mmc_readl(host,
  1087. + SDHCI_RESPONSE + (3-i)*4) << 8;
  1088. + if (i != 3)
  1089. + host->cmd->resp[i] |=
  1090. + bcm2835_mmc_readb(host,
  1091. + SDHCI_RESPONSE + (3-i)*4-1);
  1092. + }
  1093. + } else {
  1094. + host->cmd->resp[0] = bcm2835_mmc_readl(host, SDHCI_RESPONSE);
  1095. + }
  1096. + }
  1097. +
  1098. + host->cmd->error = 0;
  1099. +
  1100. + /* Finished CMD23, now send actual command. */
  1101. + if (host->cmd == host->mrq->sbc) {
  1102. + host->cmd = NULL;
  1103. + bcm2835_mmc_send_command(host, host->mrq->cmd);
  1104. +
  1105. + if (host->mrq->cmd->data && host->use_dma) {
  1106. + /* DMA transfer starts now, PIO starts after interrupt */
  1107. + bcm2835_mmc_transfer_dma(host);
  1108. + }
  1109. + } else {
  1110. +
  1111. + /* Processed actual command. */
  1112. + if (host->data && host->data_early)
  1113. + bcm2835_mmc_finish_data(host);
  1114. +
  1115. + if (!host->cmd->data)
  1116. + tasklet_schedule(&host->finish_tasklet);
  1117. +
  1118. + host->cmd = NULL;
  1119. + }
  1120. +}
  1121. +
  1122. +
  1123. +static void bcm2835_mmc_timeout_timer(struct timer_list *t)
  1124. +{
  1125. + struct bcm2835_host *host = from_timer(host, t, timer);
  1126. + unsigned long flags;
  1127. +
  1128. + spin_lock_irqsave(&host->lock, flags);
  1129. +
  1130. + if (host->mrq) {
  1131. + pr_err("%s: Timeout waiting for hardware interrupt.\n",
  1132. + mmc_hostname(host->mmc));
  1133. + bcm2835_mmc_dumpregs(host);
  1134. +
  1135. + if (host->data) {
  1136. + host->data->error = -ETIMEDOUT;
  1137. + bcm2835_mmc_finish_data(host);
  1138. + } else {
  1139. + if (host->cmd)
  1140. + host->cmd->error = -ETIMEDOUT;
  1141. + else
  1142. + host->mrq->cmd->error = -ETIMEDOUT;
  1143. +
  1144. + tasklet_schedule(&host->finish_tasklet);
  1145. + }
  1146. + }
  1147. +
  1148. + spin_unlock_irqrestore(&host->lock, flags);
  1149. +}
  1150. +
  1151. +
  1152. +static void bcm2835_mmc_enable_sdio_irq_nolock(struct bcm2835_host *host, int enable)
  1153. +{
  1154. + if (!(host->flags & SDHCI_DEVICE_DEAD)) {
  1155. + if (enable)
  1156. + host->ier |= SDHCI_INT_CARD_INT;
  1157. + else
  1158. + host->ier &= ~SDHCI_INT_CARD_INT;
  1159. +
  1160. + bcm2835_mmc_writel(host, host->ier, SDHCI_INT_ENABLE, 7);
  1161. + bcm2835_mmc_writel(host, host->ier, SDHCI_SIGNAL_ENABLE, 7);
  1162. + }
  1163. +}
  1164. +
  1165. +static void bcm2835_mmc_enable_sdio_irq(struct mmc_host *mmc, int enable)
  1166. +{
  1167. + struct bcm2835_host *host = mmc_priv(mmc);
  1168. + unsigned long flags;
  1169. +
  1170. + spin_lock_irqsave(&host->lock, flags);
  1171. + if (enable)
  1172. + host->flags |= SDHCI_SDIO_IRQ_ENABLED;
  1173. + else
  1174. + host->flags &= ~SDHCI_SDIO_IRQ_ENABLED;
  1175. +
  1176. + bcm2835_mmc_enable_sdio_irq_nolock(host, enable);
  1177. + spin_unlock_irqrestore(&host->lock, flags);
  1178. +}
  1179. +
  1180. +static void bcm2835_mmc_cmd_irq(struct bcm2835_host *host, u32 intmask)
  1181. +{
  1182. +
  1183. + BUG_ON(intmask == 0);
  1184. +
  1185. + if (!host->cmd) {
  1186. + pr_err("%s: Got command interrupt 0x%08x even "
  1187. + "though no command operation was in progress.\n",
  1188. + mmc_hostname(host->mmc), (unsigned)intmask);
  1189. + bcm2835_mmc_dumpregs(host);
  1190. + return;
  1191. + }
  1192. +
  1193. + if (intmask & SDHCI_INT_TIMEOUT)
  1194. + host->cmd->error = -ETIMEDOUT;
  1195. + else if (intmask & (SDHCI_INT_CRC | SDHCI_INT_END_BIT |
  1196. + SDHCI_INT_INDEX)) {
  1197. + host->cmd->error = -EILSEQ;
  1198. + }
  1199. +
  1200. + if (host->cmd->error) {
  1201. + tasklet_schedule(&host->finish_tasklet);
  1202. + return;
  1203. + }
  1204. +
  1205. + if (intmask & SDHCI_INT_RESPONSE)
  1206. + bcm2835_mmc_finish_command(host);
  1207. +
  1208. +}
  1209. +
  1210. +static void bcm2835_mmc_data_irq(struct bcm2835_host *host, u32 intmask)
  1211. +{
  1212. + struct dma_chan *dma_chan;
  1213. + u32 dir_data;
  1214. +
  1215. + BUG_ON(intmask == 0);
  1216. +
  1217. + if (!host->data) {
  1218. + /*
  1219. + * The "data complete" interrupt is also used to
  1220. + * indicate that a busy state has ended. See comment
  1221. + * above in sdhci_cmd_irq().
  1222. + */
  1223. + if (host->cmd && (host->cmd->flags & MMC_RSP_BUSY)) {
  1224. + if (intmask & SDHCI_INT_DATA_END) {
  1225. + bcm2835_mmc_finish_command(host);
  1226. + return;
  1227. + }
  1228. + }
  1229. +
  1230. + pr_debug("%s: Got data interrupt 0x%08x even "
  1231. + "though no data operation was in progress.\n",
  1232. + mmc_hostname(host->mmc), (unsigned)intmask);
  1233. + bcm2835_mmc_dumpregs(host);
  1234. +
  1235. + return;
  1236. + }
  1237. +
  1238. + if (intmask & SDHCI_INT_DATA_TIMEOUT)
  1239. + host->data->error = -ETIMEDOUT;
  1240. + else if (intmask & SDHCI_INT_DATA_END_BIT)
  1241. + host->data->error = -EILSEQ;
  1242. + else if ((intmask & SDHCI_INT_DATA_CRC) &&
  1243. + SDHCI_GET_CMD(bcm2835_mmc_readw(host, SDHCI_COMMAND))
  1244. + != MMC_BUS_TEST_R)
  1245. + host->data->error = -EILSEQ;
  1246. +
  1247. + if (host->use_dma) {
  1248. + if (host->data->flags & MMC_DATA_WRITE) {
  1249. + /* IRQ handled here */
  1250. +
  1251. + dma_chan = host->dma_chan_rxtx;
  1252. + dir_data = DMA_TO_DEVICE;
  1253. + dma_unmap_sg(dma_chan->device->dev,
  1254. + host->data->sg, host->data->sg_len,
  1255. + dir_data);
  1256. +
  1257. + bcm2835_mmc_finish_data(host);
  1258. + }
  1259. +
  1260. + } else {
  1261. + if (host->data->error)
  1262. + bcm2835_mmc_finish_data(host);
  1263. + else {
  1264. + if (intmask & (SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL))
  1265. + bcm2835_mmc_transfer_pio(host);
  1266. +
  1267. + if (intmask & SDHCI_INT_DATA_END) {
  1268. + if (host->cmd) {
  1269. + /*
  1270. + * Data managed to finish before the
  1271. + * command completed. Make sure we do
  1272. + * things in the proper order.
  1273. + */
  1274. + host->data_early = 1;
  1275. + } else {
  1276. + bcm2835_mmc_finish_data(host);
  1277. + }
  1278. + }
  1279. + }
  1280. + }
  1281. +}
  1282. +
  1283. +
  1284. +static irqreturn_t bcm2835_mmc_irq(int irq, void *dev_id)
  1285. +{
  1286. + irqreturn_t result = IRQ_NONE;
  1287. + struct bcm2835_host *host = dev_id;
  1288. + u32 intmask, mask, unexpected = 0;
  1289. + int max_loops = 16;
  1290. +
  1291. + spin_lock(&host->lock);
  1292. +
  1293. + intmask = bcm2835_mmc_readl(host, SDHCI_INT_STATUS);
  1294. +
  1295. + if (!intmask || intmask == 0xffffffff) {
  1296. + result = IRQ_NONE;
  1297. + goto out;
  1298. + }
  1299. +
  1300. + do {
  1301. + /* Clear selected interrupts. */
  1302. + mask = intmask & (SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK |
  1303. + SDHCI_INT_BUS_POWER);
  1304. + bcm2835_mmc_writel(host, mask, SDHCI_INT_STATUS, 8);
  1305. +
  1306. +
  1307. + if (intmask & SDHCI_INT_CMD_MASK)
  1308. + bcm2835_mmc_cmd_irq(host, intmask & SDHCI_INT_CMD_MASK);
  1309. +
  1310. + if (intmask & SDHCI_INT_DATA_MASK)
  1311. + bcm2835_mmc_data_irq(host, intmask & SDHCI_INT_DATA_MASK);
  1312. +
  1313. + if (intmask & SDHCI_INT_BUS_POWER)
  1314. + pr_err("%s: Card is consuming too much power!\n",
  1315. + mmc_hostname(host->mmc));
  1316. +
  1317. + if (intmask & SDHCI_INT_CARD_INT) {
  1318. + bcm2835_mmc_enable_sdio_irq_nolock(host, false);
  1319. + sdio_signal_irq(host->mmc);
  1320. + }
  1321. +
  1322. + intmask &= ~(SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE |
  1323. + SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK |
  1324. + SDHCI_INT_ERROR | SDHCI_INT_BUS_POWER |
  1325. + SDHCI_INT_CARD_INT);
  1326. +
  1327. + if (intmask) {
  1328. + unexpected |= intmask;
  1329. + bcm2835_mmc_writel(host, intmask, SDHCI_INT_STATUS, 9);
  1330. + }
  1331. +
  1332. + if (result == IRQ_NONE)
  1333. + result = IRQ_HANDLED;
  1334. +
  1335. + intmask = bcm2835_mmc_readl(host, SDHCI_INT_STATUS);
  1336. + } while (intmask && --max_loops);
  1337. +out:
  1338. + spin_unlock(&host->lock);
  1339. +
  1340. + if (unexpected) {
  1341. + pr_err("%s: Unexpected interrupt 0x%08x.\n",
  1342. + mmc_hostname(host->mmc), unexpected);
  1343. + bcm2835_mmc_dumpregs(host);
  1344. + }
  1345. +
  1346. + return result;
  1347. +}
  1348. +
  1349. +
  1350. +static void bcm2835_mmc_ack_sdio_irq(struct mmc_host *mmc)
  1351. +{
  1352. + struct bcm2835_host *host = mmc_priv(mmc);
  1353. + unsigned long flags;
  1354. +
  1355. + spin_lock_irqsave(&host->lock, flags);
  1356. + if (host->flags & SDHCI_SDIO_IRQ_ENABLED)
  1357. + bcm2835_mmc_enable_sdio_irq_nolock(host, true);
  1358. + spin_unlock_irqrestore(&host->lock, flags);
  1359. +}
  1360. +
  1361. +void bcm2835_mmc_set_clock(struct bcm2835_host *host, unsigned int clock)
  1362. +{
  1363. + int div = 0; /* Initialized for compiler warning */
  1364. + int real_div = div, clk_mul = 1;
  1365. + u16 clk = 0;
  1366. + unsigned long timeout;
  1367. + unsigned int input_clock = clock;
  1368. +
  1369. + if (host->overclock_50 && (clock == 50000000))
  1370. + clock = host->overclock_50 * 1000000 + 999999;
  1371. +
  1372. + host->mmc->actual_clock = 0;
  1373. +
  1374. + bcm2835_mmc_writew(host, 0, SDHCI_CLOCK_CONTROL);
  1375. +
  1376. + if (clock == 0)
  1377. + return;
  1378. +
  1379. + /* Version 3.00 divisors must be a multiple of 2. */
  1380. + if (host->max_clk <= clock)
  1381. + div = 1;
  1382. + else {
  1383. + for (div = 2; div < SDHCI_MAX_DIV_SPEC_300;
  1384. + div += 2) {
  1385. + if ((host->max_clk / div) <= clock)
  1386. + break;
  1387. + }
  1388. + }
  1389. +
  1390. + real_div = div;
  1391. + div >>= 1;
  1392. +
  1393. + if (real_div)
  1394. + clock = (host->max_clk * clk_mul) / real_div;
  1395. + host->mmc->actual_clock = clock;
  1396. +
  1397. + if ((clock > input_clock) && (clock > host->max_overclock)) {
  1398. + pr_warn("%s: Overclocking to %dHz\n",
  1399. + mmc_hostname(host->mmc), clock);
  1400. + host->max_overclock = clock;
  1401. + }
  1402. +
  1403. + clk |= (div & SDHCI_DIV_MASK) << SDHCI_DIVIDER_SHIFT;
  1404. + clk |= ((div & SDHCI_DIV_HI_MASK) >> SDHCI_DIV_MASK_LEN)
  1405. + << SDHCI_DIVIDER_HI_SHIFT;
  1406. + clk |= SDHCI_CLOCK_INT_EN;
  1407. + bcm2835_mmc_writew(host, clk, SDHCI_CLOCK_CONTROL);
  1408. +
  1409. + /* Wait max 20 ms */
  1410. + timeout = 20;
  1411. + while (!((clk = bcm2835_mmc_readw(host, SDHCI_CLOCK_CONTROL))
  1412. + & SDHCI_CLOCK_INT_STABLE)) {
  1413. + if (timeout == 0) {
  1414. + pr_err("%s: Internal clock never "
  1415. + "stabilised.\n", mmc_hostname(host->mmc));
  1416. + bcm2835_mmc_dumpregs(host);
  1417. + return;
  1418. + }
  1419. + timeout--;
  1420. + mdelay(1);
  1421. + }
  1422. +
  1423. + if (20-timeout > 10 && 20-timeout > host->max_delay) {
  1424. + host->max_delay = 20-timeout;
  1425. + pr_warn("Warning: MMC controller hung for %d ms\n", host->max_delay);
  1426. + }
  1427. +
  1428. + clk |= SDHCI_CLOCK_CARD_EN;
  1429. + bcm2835_mmc_writew(host, clk, SDHCI_CLOCK_CONTROL);
  1430. +}
  1431. +
  1432. +static void bcm2835_mmc_request(struct mmc_host *mmc, struct mmc_request *mrq)
  1433. +{
  1434. + struct bcm2835_host *host;
  1435. + unsigned long flags;
  1436. +
  1437. + host = mmc_priv(mmc);
  1438. +
  1439. + spin_lock_irqsave(&host->lock, flags);
  1440. +
  1441. + WARN_ON(host->mrq != NULL);
  1442. +
  1443. + host->mrq = mrq;
  1444. +
  1445. + if (mrq->sbc && !(host->flags & SDHCI_AUTO_CMD23))
  1446. + bcm2835_mmc_send_command(host, mrq->sbc);
  1447. + else
  1448. + bcm2835_mmc_send_command(host, mrq->cmd);
  1449. +
  1450. + spin_unlock_irqrestore(&host->lock, flags);
  1451. +
  1452. + if (!(mrq->sbc && !(host->flags & SDHCI_AUTO_CMD23)) && mrq->cmd->data && host->use_dma) {
  1453. + /* DMA transfer starts now, PIO starts after interrupt */
  1454. + bcm2835_mmc_transfer_dma(host);
  1455. + }
  1456. +}
  1457. +
  1458. +
  1459. +static void bcm2835_mmc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
  1460. +{
  1461. +
  1462. + struct bcm2835_host *host = mmc_priv(mmc);
  1463. + unsigned long flags;
  1464. + u8 ctrl;
  1465. + u16 clk, ctrl_2;
  1466. +
  1467. + pr_debug("bcm2835_mmc_set_ios: clock %d, pwr %d, bus_width %d, timing %d, vdd %d, drv_type %d\n",
  1468. + ios->clock, ios->power_mode, ios->bus_width,
  1469. + ios->timing, ios->signal_voltage, ios->drv_type);
  1470. +
  1471. + spin_lock_irqsave(&host->lock, flags);
  1472. +
  1473. + if (!ios->clock || ios->clock != host->clock) {
  1474. + bcm2835_mmc_set_clock(host, ios->clock);
  1475. + host->clock = ios->clock;
  1476. + }
  1477. +
  1478. + if (host->pwr != SDHCI_POWER_330) {
  1479. + host->pwr = SDHCI_POWER_330;
  1480. + bcm2835_mmc_writeb(host, SDHCI_POWER_330 | SDHCI_POWER_ON, SDHCI_POWER_CONTROL);
  1481. + }
  1482. +
  1483. + ctrl = bcm2835_mmc_readb(host, SDHCI_HOST_CONTROL);
  1484. +
  1485. + /* set bus width */
  1486. + ctrl &= ~SDHCI_CTRL_8BITBUS;
  1487. + if (ios->bus_width == MMC_BUS_WIDTH_4)
  1488. + ctrl |= SDHCI_CTRL_4BITBUS;
  1489. + else
  1490. + ctrl &= ~SDHCI_CTRL_4BITBUS;
  1491. +
  1492. + ctrl &= ~SDHCI_CTRL_HISPD; /* NO_HISPD_BIT */
  1493. +
  1494. +
  1495. + bcm2835_mmc_writeb(host, ctrl, SDHCI_HOST_CONTROL);
  1496. + /*
  1497. + * We only need to set Driver Strength if the
  1498. + * preset value enable is not set.
  1499. + */
  1500. + ctrl_2 = bcm2835_mmc_readw(host, SDHCI_HOST_CONTROL2);
  1501. + ctrl_2 &= ~SDHCI_CTRL_DRV_TYPE_MASK;
  1502. + if (ios->drv_type == MMC_SET_DRIVER_TYPE_A)
  1503. + ctrl_2 |= SDHCI_CTRL_DRV_TYPE_A;
  1504. + else if (ios->drv_type == MMC_SET_DRIVER_TYPE_C)
  1505. + ctrl_2 |= SDHCI_CTRL_DRV_TYPE_C;
  1506. +
  1507. + bcm2835_mmc_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
  1508. +
  1509. + /* Reset SD Clock Enable */
  1510. + clk = bcm2835_mmc_readw(host, SDHCI_CLOCK_CONTROL);
  1511. + clk &= ~SDHCI_CLOCK_CARD_EN;
  1512. + bcm2835_mmc_writew(host, clk, SDHCI_CLOCK_CONTROL);
  1513. +
  1514. + /* Re-enable SD Clock */
  1515. + bcm2835_mmc_set_clock(host, host->clock);
  1516. + bcm2835_mmc_writeb(host, ctrl, SDHCI_HOST_CONTROL);
  1517. +
  1518. + spin_unlock_irqrestore(&host->lock, flags);
  1519. +}
  1520. +
  1521. +
  1522. +static struct mmc_host_ops bcm2835_ops = {
  1523. + .request = bcm2835_mmc_request,
  1524. + .set_ios = bcm2835_mmc_set_ios,
  1525. + .enable_sdio_irq = bcm2835_mmc_enable_sdio_irq,
  1526. + .ack_sdio_irq = bcm2835_mmc_ack_sdio_irq,
  1527. +};
  1528. +
  1529. +
  1530. +static void bcm2835_mmc_tasklet_finish(unsigned long param)
  1531. +{
  1532. + struct bcm2835_host *host;
  1533. + unsigned long flags;
  1534. + struct mmc_request *mrq;
  1535. +
  1536. + host = (struct bcm2835_host *)param;
  1537. +
  1538. + spin_lock_irqsave(&host->lock, flags);
  1539. +
  1540. + /*
  1541. + * If this tasklet gets rescheduled while running, it will
  1542. + * be run again afterwards but without any active request.
  1543. + */
  1544. + if (!host->mrq) {
  1545. + spin_unlock_irqrestore(&host->lock, flags);
  1546. + return;
  1547. + }
  1548. +
  1549. + del_timer(&host->timer);
  1550. +
  1551. + mrq = host->mrq;
  1552. +
  1553. + /*
  1554. + * The controller needs a reset of internal state machines
  1555. + * upon error conditions.
  1556. + */
  1557. + if (!(host->flags & SDHCI_DEVICE_DEAD) &&
  1558. + ((mrq->cmd && mrq->cmd->error) ||
  1559. + (mrq->data && (mrq->data->error ||
  1560. + (mrq->data->stop && mrq->data->stop->error))))) {
  1561. +
  1562. + spin_unlock_irqrestore(&host->lock, flags);
  1563. + bcm2835_mmc_reset(host, SDHCI_RESET_CMD);
  1564. + bcm2835_mmc_reset(host, SDHCI_RESET_DATA);
  1565. + spin_lock_irqsave(&host->lock, flags);
  1566. + }
  1567. +
  1568. + host->mrq = NULL;
  1569. + host->cmd = NULL;
  1570. + host->data = NULL;
  1571. +
  1572. + spin_unlock_irqrestore(&host->lock, flags);
  1573. + mmc_request_done(host->mmc, mrq);
  1574. +}
  1575. +
  1576. +
  1577. +
  1578. +static int bcm2835_mmc_add_host(struct bcm2835_host *host)
  1579. +{
  1580. + struct mmc_host *mmc = host->mmc;
  1581. + struct device *dev = mmc->parent;
  1582. +#ifndef FORCE_PIO
  1583. + struct dma_slave_config cfg;
  1584. +#endif
  1585. + int ret;
  1586. +
  1587. + bcm2835_mmc_reset(host, SDHCI_RESET_ALL);
  1588. +
  1589. + host->clk_mul = 0;
  1590. +
  1591. + if (!mmc->f_max || mmc->f_max > host->max_clk)
  1592. + mmc->f_max = host->max_clk;
  1593. + mmc->f_min = host->max_clk / SDHCI_MAX_DIV_SPEC_300;
  1594. +
  1595. + /* SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK */
  1596. + host->timeout_clk = mmc->f_max / 1000;
  1597. + mmc->max_busy_timeout = (1 << 27) / host->timeout_clk;
  1598. +
  1599. + /* host controller capabilities */
  1600. + mmc->caps |= MMC_CAP_CMD23 | MMC_CAP_NEEDS_POLL |
  1601. + MMC_CAP_SDIO_IRQ | MMC_CAP_SD_HIGHSPEED |
  1602. + MMC_CAP_MMC_HIGHSPEED;
  1603. +
  1604. + mmc->caps2 |= MMC_CAP2_SDIO_IRQ_NOTHREAD;
  1605. +
  1606. + host->flags = SDHCI_AUTO_CMD23;
  1607. +
  1608. + dev_info(dev, "mmc_debug:%x mmc_debug2:%x\n", mmc_debug, mmc_debug2);
  1609. +#ifdef FORCE_PIO
  1610. + dev_info(dev, "Forcing PIO mode\n");
  1611. + host->have_dma = false;
  1612. +#else
  1613. + if (IS_ERR_OR_NULL(host->dma_chan_rxtx)) {
  1614. + dev_err(dev, "%s: Unable to initialise DMA channel. Falling back to PIO\n",
  1615. + DRIVER_NAME);
  1616. + host->have_dma = false;
  1617. + } else {
  1618. + dev_info(dev, "DMA channel allocated");
  1619. +
  1620. + cfg.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  1621. + cfg.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  1622. +
  1623. + /* Validate the slave configurations */
  1624. +
  1625. + cfg.direction = DMA_MEM_TO_DEV;
  1626. + cfg.src_addr = 0;
  1627. + cfg.dst_addr = host->bus_addr + SDHCI_BUFFER;
  1628. +
  1629. + ret = dmaengine_slave_config(host->dma_chan_rxtx, &cfg);
  1630. +
  1631. + if (ret == 0) {
  1632. + host->dma_cfg_tx = cfg;
  1633. +
  1634. + cfg.direction = DMA_DEV_TO_MEM;
  1635. + cfg.src_addr = host->bus_addr + SDHCI_BUFFER;
  1636. + cfg.dst_addr = 0;
  1637. +
  1638. + ret = dmaengine_slave_config(host->dma_chan_rxtx, &cfg);
  1639. + }
  1640. +
  1641. + if (ret == 0) {
  1642. + host->dma_cfg_rx = cfg;
  1643. +
  1644. + host->have_dma = true;
  1645. + } else {
  1646. + pr_err("%s: unable to configure DMA channel. "
  1647. + "Falling back to PIO\n",
  1648. + mmc_hostname(mmc));
  1649. + dma_release_channel(host->dma_chan_rxtx);
  1650. + host->dma_chan_rxtx = NULL;
  1651. + host->have_dma = false;
  1652. + }
  1653. + }
  1654. +#endif
  1655. + mmc->max_segs = 128;
  1656. + if (swiotlb_max_segment())
  1657. + mmc->max_req_size = (1 << IO_TLB_SHIFT) * IO_TLB_SEGSIZE;
  1658. + else
  1659. + mmc->max_req_size = 524288;
  1660. + mmc->max_seg_size = mmc->max_req_size;
  1661. + mmc->max_blk_size = 512;
  1662. + mmc->max_blk_count = 65535;
  1663. +
  1664. + /* report supported voltage ranges */
  1665. + mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34;
  1666. +
  1667. + tasklet_init(&host->finish_tasklet,
  1668. + bcm2835_mmc_tasklet_finish, (unsigned long)host);
  1669. +
  1670. + timer_setup(&host->timer, bcm2835_mmc_timeout_timer, 0);
  1671. + init_waitqueue_head(&host->buf_ready_int);
  1672. +
  1673. + bcm2835_mmc_init(host, 0);
  1674. + ret = request_irq(host->irq, bcm2835_mmc_irq, IRQF_SHARED,
  1675. + mmc_hostname(mmc), host);
  1676. + if (ret) {
  1677. + dev_err(dev, "Failed to request IRQ %d: %d\n", host->irq, ret);
  1678. + goto untasklet;
  1679. + }
  1680. +
  1681. + ret = mmc_add_host(mmc);
  1682. + if (ret) {
  1683. + dev_err(dev, "could not add MMC host\n");
  1684. + goto free_irq;
  1685. + }
  1686. +
  1687. + return 0;
  1688. +
  1689. +free_irq:
  1690. + free_irq(host->irq, host);
  1691. +untasklet:
  1692. + tasklet_kill(&host->finish_tasklet);
  1693. +
  1694. + return ret;
  1695. +}
  1696. +
  1697. +static int bcm2835_mmc_probe(struct platform_device *pdev)
  1698. +{
  1699. + struct device *dev = &pdev->dev;
  1700. + struct device_node *node = dev->of_node;
  1701. + struct clk *clk;
  1702. + struct resource *iomem;
  1703. + struct bcm2835_host *host;
  1704. + struct mmc_host *mmc;
  1705. + const __be32 *addr;
  1706. + int ret;
  1707. +
  1708. + mmc = mmc_alloc_host(sizeof(*host), dev);
  1709. + if (!mmc)
  1710. + return -ENOMEM;
  1711. +
  1712. + mmc->ops = &bcm2835_ops;
  1713. + host = mmc_priv(mmc);
  1714. + host->mmc = mmc;
  1715. + host->timeout = msecs_to_jiffies(1000);
  1716. + spin_lock_init(&host->lock);
  1717. +
  1718. + iomem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1719. + host->ioaddr = devm_ioremap_resource(dev, iomem);
  1720. + if (IS_ERR(host->ioaddr)) {
  1721. + ret = PTR_ERR(host->ioaddr);
  1722. + goto err;
  1723. + }
  1724. +
  1725. + addr = of_get_address(node, 0, NULL, NULL);
  1726. + if (!addr) {
  1727. + dev_err(dev, "could not get DMA-register address\n");
  1728. + ret = -ENODEV;
  1729. + goto err;
  1730. + }
  1731. + host->bus_addr = be32_to_cpup(addr);
  1732. + pr_debug(" - ioaddr %lx, iomem->start %lx, bus_addr %lx\n",
  1733. + (unsigned long)host->ioaddr,
  1734. + (unsigned long)iomem->start,
  1735. + (unsigned long)host->bus_addr);
  1736. +
  1737. +#ifndef FORCE_PIO
  1738. + if (node) {
  1739. + host->dma_chan_rxtx = dma_request_slave_channel(dev, "rx-tx");
  1740. + if (!host->dma_chan_rxtx)
  1741. + host->dma_chan_rxtx =
  1742. + dma_request_slave_channel(dev, "tx");
  1743. + if (!host->dma_chan_rxtx)
  1744. + host->dma_chan_rxtx =
  1745. + dma_request_slave_channel(dev, "rx");
  1746. + } else {
  1747. + dma_cap_mask_t mask;
  1748. +
  1749. + dma_cap_zero(mask);
  1750. + /* we don't care about the channel, any would work */
  1751. + dma_cap_set(DMA_SLAVE, mask);
  1752. + host->dma_chan_rxtx = dma_request_channel(mask, NULL, NULL);
  1753. + }
  1754. +#endif
  1755. + clk = devm_clk_get(dev, NULL);
  1756. + if (IS_ERR(clk)) {
  1757. + ret = PTR_ERR(clk);
  1758. + if (ret == -EPROBE_DEFER)
  1759. + dev_info(dev, "could not get clk, deferring probe\n");
  1760. + else
  1761. + dev_err(dev, "could not get clk\n");
  1762. + goto err;
  1763. + }
  1764. +
  1765. + host->max_clk = clk_get_rate(clk);
  1766. +
  1767. + host->irq = platform_get_irq(pdev, 0);
  1768. + if (host->irq <= 0) {
  1769. + dev_err(dev, "get IRQ failed\n");
  1770. + ret = -EINVAL;
  1771. + goto err;
  1772. + }
  1773. +
  1774. + if (node) {
  1775. + mmc_of_parse(mmc);
  1776. +
  1777. + /* Read any custom properties */
  1778. + of_property_read_u32(node,
  1779. + "brcm,overclock-50",
  1780. + &host->overclock_50);
  1781. + } else {
  1782. + mmc->caps |= MMC_CAP_4_BIT_DATA;
  1783. + }
  1784. +
  1785. + ret = bcm2835_mmc_add_host(host);
  1786. + if (ret)
  1787. + goto err;
  1788. +
  1789. + platform_set_drvdata(pdev, host);
  1790. +
  1791. + return 0;
  1792. +err:
  1793. + if (host->dma_chan_rxtx)
  1794. + dma_release_channel(host->dma_chan_rxtx);
  1795. + mmc_free_host(mmc);
  1796. +
  1797. + return ret;
  1798. +}
  1799. +
  1800. +static int bcm2835_mmc_remove(struct platform_device *pdev)
  1801. +{
  1802. + struct bcm2835_host *host = platform_get_drvdata(pdev);
  1803. + unsigned long flags;
  1804. + int dead;
  1805. + u32 scratch;
  1806. +
  1807. + dead = 0;
  1808. + scratch = bcm2835_mmc_readl(host, SDHCI_INT_STATUS);
  1809. + if (scratch == (u32)-1)
  1810. + dead = 1;
  1811. +
  1812. +
  1813. + if (dead) {
  1814. + spin_lock_irqsave(&host->lock, flags);
  1815. +
  1816. + host->flags |= SDHCI_DEVICE_DEAD;
  1817. +
  1818. + if (host->mrq) {
  1819. + pr_err("%s: Controller removed during "
  1820. + " transfer!\n", mmc_hostname(host->mmc));
  1821. +
  1822. + host->mrq->cmd->error = -ENOMEDIUM;
  1823. + tasklet_schedule(&host->finish_tasklet);
  1824. + }
  1825. +
  1826. + spin_unlock_irqrestore(&host->lock, flags);
  1827. + }
  1828. +
  1829. + mmc_remove_host(host->mmc);
  1830. +
  1831. + if (!dead)
  1832. + bcm2835_mmc_reset(host, SDHCI_RESET_ALL);
  1833. +
  1834. + free_irq(host->irq, host);
  1835. +
  1836. + del_timer_sync(&host->timer);
  1837. +
  1838. + tasklet_kill(&host->finish_tasklet);
  1839. +
  1840. + if (host->dma_chan_rxtx)
  1841. + dma_release_channel(host->dma_chan_rxtx);
  1842. +
  1843. + mmc_free_host(host->mmc);
  1844. +
  1845. + return 0;
  1846. +}
  1847. +
  1848. +
  1849. +static const struct of_device_id bcm2835_mmc_match[] = {
  1850. + { .compatible = "brcm,bcm2835-mmc" },
  1851. + { }
  1852. +};
  1853. +MODULE_DEVICE_TABLE(of, bcm2835_mmc_match);
  1854. +
  1855. +
  1856. +
  1857. +static struct platform_driver bcm2835_mmc_driver = {
  1858. + .probe = bcm2835_mmc_probe,
  1859. + .remove = bcm2835_mmc_remove,
  1860. + .driver = {
  1861. + .name = DRIVER_NAME,
  1862. + .owner = THIS_MODULE,
  1863. + .of_match_table = bcm2835_mmc_match,
  1864. + },
  1865. +};
  1866. +module_platform_driver(bcm2835_mmc_driver);
  1867. +
  1868. +module_param(mmc_debug, uint, 0644);
  1869. +module_param(mmc_debug2, uint, 0644);
  1870. +MODULE_ALIAS("platform:mmc-bcm2835");
  1871. +MODULE_DESCRIPTION("BCM2835 SDHCI driver");
  1872. +MODULE_LICENSE("GPL v2");
  1873. +MODULE_AUTHOR("Gellert Weisz");
  1874. --- a/drivers/mmc/host/sdhci-iproc.c
  1875. +++ b/drivers/mmc/host/sdhci-iproc.c
  1876. @@ -197,6 +197,7 @@ static const struct sdhci_ops sdhci_ipro
  1877. .write_b = sdhci_iproc_writeb,
  1878. .set_clock = sdhci_set_clock,
  1879. .get_max_clock = sdhci_iproc_get_max_clock,
  1880. + .set_power = sdhci_set_power_and_bus_voltage,
  1881. .set_bus_width = sdhci_set_bus_width,
  1882. .reset = sdhci_reset,
  1883. .set_uhs_signaling = sdhci_set_uhs_signaling,
  1884. --- a/drivers/mmc/host/sdhci.c
  1885. +++ b/drivers/mmc/host/sdhci.c
  1886. @@ -40,7 +40,7 @@
  1887. pr_debug("%s: " DRIVER_NAME ": " f, mmc_hostname(host->mmc), ## x)
  1888. #define SDHCI_DUMP(f, x...) \
  1889. - pr_err("%s: " DRIVER_NAME ": " f, mmc_hostname(host->mmc), ## x)
  1890. + pr_debug("%s: " DRIVER_NAME ": " f, mmc_hostname(host->mmc), ## x)
  1891. #define MAX_TUNING_LOOP 40
  1892. @@ -3233,7 +3233,7 @@ static void sdhci_timeout_timer(struct t
  1893. spin_lock_irqsave(&host->lock, flags);
  1894. if (host->cmd && !sdhci_data_line_cmd(host->cmd)) {
  1895. - pr_err("%s: Timeout waiting for hardware cmd interrupt.\n",
  1896. + pr_debug("%s: Timeout waiting for hardware cmd interrupt.\n",
  1897. mmc_hostname(host->mmc));
  1898. sdhci_err_stats_inc(host, REQ_TIMEOUT);
  1899. sdhci_dumpregs(host);
  1900. @@ -3256,7 +3256,7 @@ static void sdhci_timeout_data_timer(str
  1901. if (host->data || host->data_cmd ||
  1902. (host->cmd && sdhci_data_line_cmd(host->cmd))) {
  1903. - pr_err("%s: Timeout waiting for hardware interrupt.\n",
  1904. + pr_debug("%s: Timeout waiting for hardware interrupt.\n",
  1905. mmc_hostname(host->mmc));
  1906. sdhci_err_stats_inc(host, REQ_TIMEOUT);
  1907. sdhci_dumpregs(host);
  1908. --- a/include/linux/mmc/card.h
  1909. +++ b/include/linux/mmc/card.h
  1910. @@ -297,6 +297,8 @@ struct mmc_card {
  1911. #define MMC_QUIRK_BROKEN_SD_CACHE (1<<15) /* Disable broken SD cache support */
  1912. #define MMC_QUIRK_BROKEN_CACHE_FLUSH (1<<16) /* Don't flush cache until the write has occurred */
  1913. +#define MMC_QUIRK_ERASE_BROKEN (1<<31) /* Skip erase */
  1914. +
  1915. bool written_flag; /* Indicates eMMC has been written since power on */
  1916. bool reenable_cmdq; /* Re-enable Command Queue */