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

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