0143-bcm2835-sdhost-Major-revision.patch 57 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070
  1. From 6e7c35ac419f74f7edbbc5a09aaa2b4c75b63133 Mon Sep 17 00:00:00 2001
  2. From: Phil Elwell <phil@raspberrypi.org>
  3. Date: Thu, 11 Feb 2016 16:51:01 +0000
  4. Subject: [PATCH 143/381] bcm2835-sdhost: Major revision
  5. This is a significant revision of the bcm2835-sdhost driver. It
  6. improves on the original in a number of ways:
  7. 1) Through the use of CMD23 for reads it appears to avoid problems
  8. reading some sectors on certain high speed cards.
  9. 2) Better atomicity to prevent crashes.
  10. 3) Higher performance.
  11. 4) Activity logging included, for easier diagnosis in the event
  12. of a problem.
  13. Signed-off-by: Phil Elwell <phil@raspberrypi.org>
  14. ---
  15. drivers/mmc/host/bcm2835-sdhost.c | 1284 ++++++++++++++++++++-----------------
  16. 1 file changed, 686 insertions(+), 598 deletions(-)
  17. --- a/drivers/mmc/host/bcm2835-sdhost.c
  18. +++ b/drivers/mmc/host/bcm2835-sdhost.c
  19. @@ -2,7 +2,7 @@
  20. * BCM2835 SD host driver.
  21. *
  22. * Author: Phil Elwell <phil@raspberrypi.org>
  23. - * Copyright 2015
  24. + * Copyright (C) 2015-2016 Raspberry Pi (Trading) Ltd.
  25. *
  26. * Based on
  27. * mmc-bcm2835.c by Gellert Weisz
  28. @@ -24,12 +24,13 @@
  29. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  30. */
  31. -#define SAFE_READ_THRESHOLD 4
  32. -#define SAFE_WRITE_THRESHOLD 4
  33. -#define ALLOW_DMA 1
  34. -#define ALLOW_CMD23 0
  35. -#define ALLOW_FAST 1
  36. -#define USE_BLOCK_IRQ 1
  37. +#define FIFO_READ_THRESHOLD 4
  38. +#define FIFO_WRITE_THRESHOLD 4
  39. +#define ALLOW_CMD23_READ 1
  40. +#define ALLOW_CMD23_WRITE 0
  41. +#define ENABLE_LOG 1
  42. +#define SDDATA_FIFO_PIO_BURST 8
  43. +#define CMD_DALLY_US 1
  44. #include <linux/delay.h>
  45. #include <linux/module.h>
  46. @@ -48,6 +49,7 @@
  47. #include <linux/dma-mapping.h>
  48. #include <linux/of_dma.h>
  49. #include <linux/time.h>
  50. +#include <linux/workqueue.h>
  51. #define DRIVER_NAME "sdhost-bcm2835"
  52. @@ -110,6 +112,28 @@
  53. #define SDEDM_READ_THRESHOLD_SHIFT 14
  54. #define SDEDM_THRESHOLD_MASK 0x1f
  55. +#define SDEDM_FSM_MASK 0xf
  56. +#define SDEDM_FSM_IDENTMODE 0x0
  57. +#define SDEDM_FSM_DATAMODE 0x1
  58. +#define SDEDM_FSM_READDATA 0x2
  59. +#define SDEDM_FSM_WRITEDATA 0x3
  60. +#define SDEDM_FSM_READWAIT 0x4
  61. +#define SDEDM_FSM_READCRC 0x5
  62. +#define SDEDM_FSM_WRITECRC 0x6
  63. +#define SDEDM_FSM_WRITEWAIT1 0x7
  64. +#define SDEDM_FSM_POWERDOWN 0x8
  65. +#define SDEDM_FSM_POWERUP 0x9
  66. +#define SDEDM_FSM_WRITESTART1 0xa
  67. +#define SDEDM_FSM_WRITESTART2 0xb
  68. +#define SDEDM_FSM_GENPULSES 0xc
  69. +#define SDEDM_FSM_WRITEWAIT2 0xd
  70. +#define SDEDM_FSM_STARTPOWDOWN 0xf
  71. +
  72. +#define SDDATA_FIFO_WORDS 16
  73. +
  74. +#define USE_CMD23_FLAGS ((ALLOW_CMD23_READ * MMC_DATA_READ) | \
  75. + (ALLOW_CMD23_WRITE * MMC_DATA_WRITE))
  76. +
  77. #define MHZ 1000000
  78. @@ -131,15 +155,17 @@ struct bcm2835_host {
  79. struct tasklet_struct finish_tasklet; /* Tasklet structures */
  80. - struct timer_list timer; /* Timer for timeouts */
  81. + struct work_struct cmd_wait_wq; /* Workqueue function */
  82. - struct timer_list pio_timer; /* PIO error detection timer */
  83. + struct timer_list timer; /* Timer for timeouts */
  84. struct sg_mapping_iter sg_miter; /* SG state for PIO */
  85. unsigned int blocks; /* remaining PIO blocks */
  86. int irq; /* Device IRQ */
  87. + u32 cmd_quick_poll_retries;
  88. + u32 ns_per_fifo_word;
  89. /* cached registers */
  90. u32 hcfg;
  91. @@ -154,16 +180,21 @@ struct bcm2835_host {
  92. unsigned int use_busy:1; /* Wait for busy interrupt */
  93. - unsigned int debug:1; /* Enable debug output */
  94. + unsigned int use_sbc:1; /* Send CMD23 */
  95. - u32 thread_isr;
  96. + unsigned int debug:1; /* Enable debug output */
  97. /*DMA part*/
  98. struct dma_chan *dma_chan_rx; /* DMA channel for reads */
  99. struct dma_chan *dma_chan_tx; /* DMA channel for writes */
  100. + struct dma_chan *dma_chan; /* Channel in used */
  101. + struct dma_async_tx_descriptor *dma_desc;
  102. + u32 dma_dir;
  103. + u32 drain_words;
  104. + struct page *drain_page;
  105. + u32 drain_offset;
  106. bool allow_dma;
  107. - bool have_dma;
  108. bool use_dma;
  109. /*end of DMA part*/
  110. @@ -173,13 +204,98 @@ struct bcm2835_host {
  111. u32 overclock_50; /* frequency to use when 50MHz is requested (in MHz) */
  112. u32 overclock; /* Current frequency if overclocked, else zero */
  113. u32 pio_limit; /* Maximum block count for PIO (0 = always DMA) */
  114. +};
  115. - u32 debug_flags;
  116. +#if ENABLE_LOG
  117. - u32 sectors; /* Cached card size in sectors */
  118. - u32 single_read_sectors[8];
  119. +struct log_entry_struct {
  120. + char event[4];
  121. + u32 timestamp;
  122. + u32 param1;
  123. + u32 param2;
  124. };
  125. +typedef struct log_entry_struct LOG_ENTRY_T;
  126. +
  127. +LOG_ENTRY_T *sdhost_log_buf;
  128. +dma_addr_t sdhost_log_addr;
  129. +static u32 sdhost_log_idx;
  130. +static spinlock_t log_lock;
  131. +static void __iomem *timer_base;
  132. +
  133. +#define LOG_ENTRIES (256*1)
  134. +#define LOG_SIZE (sizeof(LOG_ENTRY_T)*LOG_ENTRIES)
  135. +
  136. +static void log_init(u32 bus_to_phys)
  137. +{
  138. + spin_lock_init(&log_lock);
  139. + sdhost_log_buf = dma_zalloc_coherent(NULL, LOG_SIZE, &sdhost_log_addr,
  140. + GFP_KERNEL);
  141. + if (sdhost_log_buf) {
  142. + pr_err("sdhost: log_buf @ %p (%x)\n",
  143. + sdhost_log_buf, sdhost_log_addr);
  144. + timer_base = ioremap_nocache(bus_to_phys + 0x7e003000, SZ_4K);
  145. + if (!timer_base)
  146. + pr_err("sdhost: failed to remap timer\n");
  147. + }
  148. + else
  149. + pr_err("sdhost: failed to allocate log buf\n");
  150. +}
  151. +
  152. +static void log_event_impl(const char *event, u32 param1, u32 param2)
  153. +{
  154. + if (sdhost_log_buf) {
  155. + LOG_ENTRY_T *entry;
  156. + unsigned long flags;
  157. +
  158. + spin_lock_irqsave(&log_lock, flags);
  159. +
  160. + entry = sdhost_log_buf + sdhost_log_idx;
  161. + memcpy(entry->event, event, 4);
  162. + entry->timestamp = (readl(timer_base + 4) & 0x3fffffff) +
  163. + (smp_processor_id()<<30);
  164. + entry->param1 = param1;
  165. + entry->param2 = param2;
  166. + sdhost_log_idx = (sdhost_log_idx + 1) % LOG_ENTRIES;
  167. +
  168. + spin_unlock_irqrestore(&log_lock, flags);
  169. + }
  170. +}
  171. +
  172. +static void log_dump(void)
  173. +{
  174. + if (sdhost_log_buf) {
  175. + LOG_ENTRY_T *entry;
  176. + unsigned long flags;
  177. + int idx;
  178. +
  179. + spin_lock_irqsave(&log_lock, flags);
  180. +
  181. + idx = sdhost_log_idx;
  182. + do {
  183. + entry = sdhost_log_buf + idx;
  184. + if (entry->event[0] != '\0')
  185. + pr_err("[%08x] %.4s %x %x\n",
  186. + entry->timestamp,
  187. + entry->event,
  188. + entry->param1,
  189. + entry->param2);
  190. + idx = (idx + 1) % LOG_ENTRIES;
  191. + } while (idx != sdhost_log_idx);
  192. +
  193. + spin_unlock_irqrestore(&log_lock, flags);
  194. + }
  195. +}
  196. +
  197. +#define log_event(event, param1, param2) log_event_impl(event, param1, param2)
  198. +
  199. +#else
  200. +
  201. +#define log_init(x) (void)0
  202. +#define log_event(event, param1, param2) (void)0
  203. +#define log_dump() (void)0
  204. +
  205. +#endif
  206. static inline void bcm2835_sdhost_write(struct bcm2835_host *host, u32 val, int reg)
  207. {
  208. @@ -201,7 +317,7 @@ static void bcm2835_sdhost_dumpcmd(struc
  209. const char *label)
  210. {
  211. if (cmd)
  212. - pr_info("%s:%c%s op %d arg 0x%x flags 0x%x - resp %08x %08x %08x %08x, err %d\n",
  213. + pr_err("%s:%c%s op %d arg 0x%x flags 0x%x - resp %08x %08x %08x %08x, err %d\n",
  214. mmc_hostname(host->mmc),
  215. (cmd == host->cmd) ? '>' : ' ',
  216. label, cmd->opcode, cmd->arg, cmd->flags,
  217. @@ -211,73 +327,74 @@ static void bcm2835_sdhost_dumpcmd(struc
  218. static void bcm2835_sdhost_dumpregs(struct bcm2835_host *host)
  219. {
  220. - bcm2835_sdhost_dumpcmd(host, host->mrq->sbc, "sbc");
  221. - bcm2835_sdhost_dumpcmd(host, host->mrq->cmd, "cmd");
  222. - if (host->mrq->data)
  223. - pr_err("%s: data blocks %x blksz %x - err %d\n",
  224. - mmc_hostname(host->mmc),
  225. - host->mrq->data->blocks,
  226. - host->mrq->data->blksz,
  227. - host->mrq->data->error);
  228. - bcm2835_sdhost_dumpcmd(host, host->mrq->stop, "stop");
  229. + if (host->mrq)
  230. + {
  231. + bcm2835_sdhost_dumpcmd(host, host->mrq->sbc, "sbc");
  232. + bcm2835_sdhost_dumpcmd(host, host->mrq->cmd, "cmd");
  233. + if (host->mrq->data)
  234. + pr_err("%s: data blocks %x blksz %x - err %d\n",
  235. + mmc_hostname(host->mmc),
  236. + host->mrq->data->blocks,
  237. + host->mrq->data->blksz,
  238. + host->mrq->data->error);
  239. + bcm2835_sdhost_dumpcmd(host, host->mrq->stop, "stop");
  240. + }
  241. - pr_info("%s: =========== REGISTER DUMP ===========\n",
  242. + pr_err("%s: =========== REGISTER DUMP ===========\n",
  243. mmc_hostname(host->mmc));
  244. - pr_info("%s: SDCMD 0x%08x\n",
  245. + pr_err("%s: SDCMD 0x%08x\n",
  246. mmc_hostname(host->mmc),
  247. bcm2835_sdhost_read(host, SDCMD));
  248. - pr_info("%s: SDARG 0x%08x\n",
  249. + pr_err("%s: SDARG 0x%08x\n",
  250. mmc_hostname(host->mmc),
  251. bcm2835_sdhost_read(host, SDARG));
  252. - pr_info("%s: SDTOUT 0x%08x\n",
  253. + pr_err("%s: SDTOUT 0x%08x\n",
  254. mmc_hostname(host->mmc),
  255. bcm2835_sdhost_read(host, SDTOUT));
  256. - pr_info("%s: SDCDIV 0x%08x\n",
  257. + pr_err("%s: SDCDIV 0x%08x\n",
  258. mmc_hostname(host->mmc),
  259. bcm2835_sdhost_read(host, SDCDIV));
  260. - pr_info("%s: SDRSP0 0x%08x\n",
  261. + pr_err("%s: SDRSP0 0x%08x\n",
  262. mmc_hostname(host->mmc),
  263. bcm2835_sdhost_read(host, SDRSP0));
  264. - pr_info("%s: SDRSP1 0x%08x\n",
  265. + pr_err("%s: SDRSP1 0x%08x\n",
  266. mmc_hostname(host->mmc),
  267. bcm2835_sdhost_read(host, SDRSP1));
  268. - pr_info("%s: SDRSP2 0x%08x\n",
  269. + pr_err("%s: SDRSP2 0x%08x\n",
  270. mmc_hostname(host->mmc),
  271. bcm2835_sdhost_read(host, SDRSP2));
  272. - pr_info("%s: SDRSP3 0x%08x\n",
  273. + pr_err("%s: SDRSP3 0x%08x\n",
  274. mmc_hostname(host->mmc),
  275. bcm2835_sdhost_read(host, SDRSP3));
  276. - pr_info("%s: SDHSTS 0x%08x\n",
  277. + pr_err("%s: SDHSTS 0x%08x\n",
  278. mmc_hostname(host->mmc),
  279. bcm2835_sdhost_read(host, SDHSTS));
  280. - pr_info("%s: SDVDD 0x%08x\n",
  281. + pr_err("%s: SDVDD 0x%08x\n",
  282. mmc_hostname(host->mmc),
  283. bcm2835_sdhost_read(host, SDVDD));
  284. - pr_info("%s: SDEDM 0x%08x\n",
  285. + pr_err("%s: SDEDM 0x%08x\n",
  286. mmc_hostname(host->mmc),
  287. bcm2835_sdhost_read(host, SDEDM));
  288. - pr_info("%s: SDHCFG 0x%08x\n",
  289. + pr_err("%s: SDHCFG 0x%08x\n",
  290. mmc_hostname(host->mmc),
  291. bcm2835_sdhost_read(host, SDHCFG));
  292. - pr_info("%s: SDHBCT 0x%08x\n",
  293. + pr_err("%s: SDHBCT 0x%08x\n",
  294. mmc_hostname(host->mmc),
  295. bcm2835_sdhost_read(host, SDHBCT));
  296. - pr_info("%s: SDHBLC 0x%08x\n",
  297. + pr_err("%s: SDHBLC 0x%08x\n",
  298. mmc_hostname(host->mmc),
  299. bcm2835_sdhost_read(host, SDHBLC));
  300. - pr_info("%s: ===========================================\n",
  301. + pr_err("%s: ===========================================\n",
  302. mmc_hostname(host->mmc));
  303. }
  304. -
  305. static void bcm2835_sdhost_set_power(struct bcm2835_host *host, bool on)
  306. {
  307. bcm2835_sdhost_write(host, on ? 1 : 0, SDVDD);
  308. }
  309. -
  310. static void bcm2835_sdhost_reset_internal(struct bcm2835_host *host)
  311. {
  312. u32 temp;
  313. @@ -300,26 +417,24 @@ static void bcm2835_sdhost_reset_interna
  314. temp = bcm2835_sdhost_read(host, SDEDM);
  315. temp &= ~((SDEDM_THRESHOLD_MASK<<SDEDM_READ_THRESHOLD_SHIFT) |
  316. (SDEDM_THRESHOLD_MASK<<SDEDM_WRITE_THRESHOLD_SHIFT));
  317. - temp |= (SAFE_READ_THRESHOLD << SDEDM_READ_THRESHOLD_SHIFT) |
  318. - (SAFE_WRITE_THRESHOLD << SDEDM_WRITE_THRESHOLD_SHIFT);
  319. + temp |= (FIFO_READ_THRESHOLD << SDEDM_READ_THRESHOLD_SHIFT) |
  320. + (FIFO_WRITE_THRESHOLD << SDEDM_WRITE_THRESHOLD_SHIFT);
  321. bcm2835_sdhost_write(host, temp, SDEDM);
  322. mdelay(10);
  323. bcm2835_sdhost_set_power(host, true);
  324. mdelay(10);
  325. host->clock = 0;
  326. - host->sectors = 0;
  327. - host->single_read_sectors[0] = ~0;
  328. bcm2835_sdhost_write(host, host->hcfg, SDHCFG);
  329. bcm2835_sdhost_write(host, host->cdiv, SDCDIV);
  330. mmiowb();
  331. }
  332. -
  333. static void bcm2835_sdhost_reset(struct mmc_host *mmc)
  334. {
  335. struct bcm2835_host *host = mmc_priv(mmc);
  336. unsigned long flags;
  337. spin_lock_irqsave(&host->lock, flags);
  338. + log_event("RST<", 0, 0);
  339. bcm2835_sdhost_reset_internal(host);
  340. @@ -344,82 +459,48 @@ static void bcm2835_sdhost_init(struct b
  341. }
  342. }
  343. -static bool bcm2835_sdhost_is_write_complete(struct bcm2835_host *host)
  344. +static void bcm2835_sdhost_wait_transfer_complete(struct bcm2835_host *host)
  345. {
  346. - bool write_complete = ((bcm2835_sdhost_read(host, SDEDM) & 0xf) == 1);
  347. + int timediff;
  348. + u32 alternate_idle;
  349. + u32 edm;
  350. - if (!write_complete) {
  351. - /* Request an IRQ for the last block */
  352. - host->hcfg |= SDHCFG_BLOCK_IRPT_EN;
  353. - bcm2835_sdhost_write(host, host->hcfg, SDHCFG);
  354. - if ((bcm2835_sdhost_read(host, SDEDM) & 0xf) == 1) {
  355. - /* The write has now completed. Disable the interrupt
  356. - and clear the status flag */
  357. - host->hcfg &= ~SDHCFG_BLOCK_IRPT_EN;
  358. - bcm2835_sdhost_write(host, host->hcfg, SDHCFG);
  359. - bcm2835_sdhost_write(host, SDHSTS_BLOCK_IRPT, SDHSTS);
  360. - write_complete = true;
  361. - }
  362. - }
  363. + alternate_idle = (host->mrq->data->flags & MMC_DATA_READ) ?
  364. + SDEDM_FSM_READWAIT : SDEDM_FSM_WRITESTART1;
  365. - return write_complete;
  366. -}
  367. + edm = bcm2835_sdhost_read(host, SDEDM);
  368. -static void bcm2835_sdhost_wait_write_complete(struct bcm2835_host *host)
  369. -{
  370. - int timediff;
  371. -#ifdef DEBUG
  372. - static struct timeval start_time;
  373. - static int max_stall_time = 0;
  374. - static int total_stall_time = 0;
  375. - struct timeval before, after;
  376. -
  377. - do_gettimeofday(&before);
  378. - if (max_stall_time == 0)
  379. - start_time = before;
  380. -#endif
  381. + log_event("WTC<", edm, 0);
  382. timediff = 0;
  383. while (1) {
  384. - u32 edm = bcm2835_sdhost_read(host, SDEDM);
  385. - if ((edm & 0xf) == 1)
  386. + u32 fsm = edm & SDEDM_FSM_MASK;
  387. + if ((fsm == SDEDM_FSM_IDENTMODE) ||
  388. + (fsm == SDEDM_FSM_DATAMODE))
  389. break;
  390. - timediff++;
  391. - if (timediff > 5000000) {
  392. -#ifdef DEBUG
  393. - do_gettimeofday(&after);
  394. - timediff = (after.tv_sec - before.tv_sec)*1000000 +
  395. - (after.tv_usec - before.tv_usec);
  396. + if (fsm == alternate_idle) {
  397. + bcm2835_sdhost_write(host,
  398. + edm | SDEDM_FORCE_DATA_MODE,
  399. + SDEDM);
  400. + break;
  401. + }
  402. - pr_err(" wait_write_complete - still waiting after %dus\n",
  403. - timediff);
  404. -#else
  405. - pr_err(" wait_write_complete - still waiting after %d retries\n",
  406. + timediff++;
  407. + if (timediff == 100000) {
  408. + pr_err("%s: wait_transfer_complete - still waiting after %d retries\n",
  409. + mmc_hostname(host->mmc),
  410. timediff);
  411. -#endif
  412. + log_dump();
  413. bcm2835_sdhost_dumpregs(host);
  414. - host->data->error = -ETIMEDOUT;
  415. + host->mrq->data->error = -ETIMEDOUT;
  416. + log_event("WTC!", edm, 0);
  417. return;
  418. }
  419. + cpu_relax();
  420. + edm = bcm2835_sdhost_read(host, SDEDM);
  421. }
  422. -
  423. -#ifdef DEBUG
  424. - do_gettimeofday(&after);
  425. - timediff = (after.tv_sec - before.tv_sec)*1000000 + (after.tv_usec - before.tv_usec);
  426. -
  427. - total_stall_time += timediff;
  428. - if (timediff > max_stall_time)
  429. - max_stall_time = timediff;
  430. -
  431. - if ((after.tv_sec - start_time.tv_sec) > 10) {
  432. - pr_debug(" wait_write_complete - max wait %dus, total %dus\n",
  433. - max_stall_time, total_stall_time);
  434. - start_time = after;
  435. - max_stall_time = 0;
  436. - total_stall_time = 0;
  437. - }
  438. -#endif
  439. + log_event("WTC>", edm, 0);
  440. }
  441. static void bcm2835_sdhost_finish_data(struct bcm2835_host *host);
  442. @@ -427,65 +508,44 @@ static void bcm2835_sdhost_finish_data(s
  443. static void bcm2835_sdhost_dma_complete(void *param)
  444. {
  445. struct bcm2835_host *host = param;
  446. - struct dma_chan *dma_chan;
  447. + struct mmc_data *data = host->data;
  448. unsigned long flags;
  449. - u32 dir_data;
  450. spin_lock_irqsave(&host->lock, flags);
  451. + log_event("DMA<", (u32)host->data, bcm2835_sdhost_read(host, SDHSTS));
  452. + log_event("DMA ", bcm2835_sdhost_read(host, SDCMD),
  453. + bcm2835_sdhost_read(host, SDEDM));
  454. - if (host->data) {
  455. - bool write_complete;
  456. - if (USE_BLOCK_IRQ)
  457. - write_complete = bcm2835_sdhost_is_write_complete(host);
  458. - else {
  459. - bcm2835_sdhost_wait_write_complete(host);
  460. - write_complete = true;
  461. - }
  462. - pr_debug("dma_complete() - write_complete=%d\n",
  463. - write_complete);
  464. -
  465. - if (write_complete || (host->data->flags & MMC_DATA_READ))
  466. - {
  467. - if (write_complete) {
  468. - dma_chan = host->dma_chan_tx;
  469. - dir_data = DMA_TO_DEVICE;
  470. - } else {
  471. - dma_chan = host->dma_chan_rx;
  472. - dir_data = DMA_FROM_DEVICE;
  473. - }
  474. -
  475. - dma_unmap_sg(dma_chan->device->dev,
  476. - host->data->sg, host->data->sg_len,
  477. - dir_data);
  478. + if (host->dma_chan) {
  479. + dma_unmap_sg(host->dma_chan->device->dev,
  480. + data->sg, data->sg_len,
  481. + host->dma_dir);
  482. - bcm2835_sdhost_finish_data(host);
  483. - }
  484. + host->dma_chan = NULL;
  485. }
  486. - spin_unlock_irqrestore(&host->lock, flags);
  487. -}
  488. + if (host->drain_words) {
  489. + void *page;
  490. + u32 *buf;
  491. -static bool data_transfer_wait(struct bcm2835_host *host)
  492. -{
  493. - unsigned long timeout = 1000000;
  494. - while (timeout)
  495. - {
  496. - u32 sdhsts = bcm2835_sdhost_read(host, SDHSTS);
  497. - if (sdhsts & SDHSTS_DATA_FLAG) {
  498. - bcm2835_sdhost_write(host, SDHSTS_DATA_FLAG, SDHSTS);
  499. - break;
  500. + page = kmap_atomic(host->drain_page);
  501. + buf = page + host->drain_offset;
  502. +
  503. + while (host->drain_words) {
  504. + u32 edm = bcm2835_sdhost_read(host, SDEDM);
  505. + if ((edm >> 4) & 0x1f)
  506. + *(buf++) = bcm2835_sdhost_read(host,
  507. + SDDATA);
  508. + host->drain_words--;
  509. }
  510. - timeout--;
  511. - }
  512. - if (timeout == 0) {
  513. - pr_err("%s: Data %s timeout\n",
  514. - mmc_hostname(host->mmc),
  515. - (host->data->flags & MMC_DATA_READ) ? "read" : "write");
  516. - bcm2835_sdhost_dumpregs(host);
  517. - host->data->error = -ETIMEDOUT;
  518. - return false;
  519. +
  520. + kunmap_atomic(page);
  521. }
  522. - return true;
  523. +
  524. + bcm2835_sdhost_finish_data(host);
  525. +
  526. + log_event("DMA>", (u32)host->data, 0);
  527. + spin_unlock_irqrestore(&host->lock, flags);
  528. }
  529. static void bcm2835_sdhost_read_block_pio(struct bcm2835_host *host)
  530. @@ -493,32 +553,83 @@ static void bcm2835_sdhost_read_block_pi
  531. unsigned long flags;
  532. size_t blksize, len;
  533. u32 *buf;
  534. + unsigned long wait_max;
  535. blksize = host->data->blksz;
  536. + wait_max = jiffies + msecs_to_jiffies(host->pio_timeout);
  537. +
  538. local_irq_save(flags);
  539. while (blksize) {
  540. - if (!sg_miter_next(&host->sg_miter))
  541. - BUG();
  542. + int copy_words;
  543. + u32 hsts = 0;
  544. +
  545. + if (!sg_miter_next(&host->sg_miter)) {
  546. + host->data->error = -EINVAL;
  547. + break;
  548. + }
  549. len = min(host->sg_miter.length, blksize);
  550. - BUG_ON(len % 4);
  551. + if (len % 4) {
  552. + host->data->error = -EINVAL;
  553. + break;
  554. + }
  555. blksize -= len;
  556. host->sg_miter.consumed = len;
  557. buf = (u32 *)host->sg_miter.addr;
  558. - while (len) {
  559. - if (!data_transfer_wait(host))
  560. - break;
  561. + copy_words = len/4;
  562. +
  563. + while (copy_words) {
  564. + int burst_words, words;
  565. + u32 edm;
  566. +
  567. + burst_words = SDDATA_FIFO_PIO_BURST;
  568. + if (burst_words > copy_words)
  569. + burst_words = copy_words;
  570. + edm = bcm2835_sdhost_read(host, SDEDM);
  571. + words = ((edm >> 4) & 0x1f);
  572. +
  573. + if (words < burst_words) {
  574. + int fsm_state = (edm & SDEDM_FSM_MASK);
  575. + if ((fsm_state != SDEDM_FSM_READDATA) &&
  576. + (fsm_state != SDEDM_FSM_READWAIT) &&
  577. + (fsm_state != SDEDM_FSM_READCRC)) {
  578. + hsts = bcm2835_sdhost_read(host,
  579. + SDHSTS);
  580. + pr_err("%s: fsm %x, hsts %x\n",
  581. + mmc_hostname(host->mmc),
  582. + fsm_state, hsts);
  583. + if (hsts & SDHSTS_ERROR_MASK)
  584. + break;
  585. + }
  586. +
  587. + if (time_after(jiffies, wait_max)) {
  588. + pr_err("%s: PIO read timeout - EDM %x\n",
  589. + mmc_hostname(host->mmc),
  590. + edm);
  591. + hsts = SDHSTS_REW_TIME_OUT;
  592. + break;
  593. + }
  594. + ndelay((burst_words - words) *
  595. + host->ns_per_fifo_word);
  596. + continue;
  597. + } else if (words > copy_words) {
  598. + words = copy_words;
  599. + }
  600. +
  601. + copy_words -= words;
  602. - *(buf++) = bcm2835_sdhost_read(host, SDDATA);
  603. - len -= 4;
  604. + while (words) {
  605. + *(buf++) = bcm2835_sdhost_read(host, SDDATA);
  606. + words--;
  607. + }
  608. }
  609. - if (host->data->error)
  610. + if (hsts & SDHSTS_ERROR_MASK)
  611. break;
  612. }
  613. @@ -532,32 +643,83 @@ static void bcm2835_sdhost_write_block_p
  614. unsigned long flags;
  615. size_t blksize, len;
  616. u32 *buf;
  617. + unsigned long wait_max;
  618. blksize = host->data->blksz;
  619. + wait_max = jiffies + msecs_to_jiffies(host->pio_timeout);
  620. +
  621. local_irq_save(flags);
  622. while (blksize) {
  623. - if (!sg_miter_next(&host->sg_miter))
  624. - BUG();
  625. + int copy_words;
  626. + u32 hsts = 0;
  627. +
  628. + if (!sg_miter_next(&host->sg_miter)) {
  629. + host->data->error = -EINVAL;
  630. + break;
  631. + }
  632. len = min(host->sg_miter.length, blksize);
  633. - BUG_ON(len % 4);
  634. + if (len % 4) {
  635. + host->data->error = -EINVAL;
  636. + break;
  637. + }
  638. blksize -= len;
  639. host->sg_miter.consumed = len;
  640. - buf = host->sg_miter.addr;
  641. + buf = (u32 *)host->sg_miter.addr;
  642. - while (len) {
  643. - if (!data_transfer_wait(host))
  644. - break;
  645. + copy_words = len/4;
  646. +
  647. + while (copy_words) {
  648. + int burst_words, words;
  649. + u32 edm;
  650. +
  651. + burst_words = SDDATA_FIFO_PIO_BURST;
  652. + if (burst_words > copy_words)
  653. + burst_words = copy_words;
  654. + edm = bcm2835_sdhost_read(host, SDEDM);
  655. + words = SDDATA_FIFO_WORDS - ((edm >> 4) & 0x1f);
  656. +
  657. + if (words < burst_words) {
  658. + int fsm_state = (edm & SDEDM_FSM_MASK);
  659. + if ((fsm_state != SDEDM_FSM_WRITEDATA) &&
  660. + (fsm_state != SDEDM_FSM_WRITESTART1) &&
  661. + (fsm_state != SDEDM_FSM_WRITESTART2)) {
  662. + hsts = bcm2835_sdhost_read(host,
  663. + SDHSTS);
  664. + pr_err("%s: fsm %x, hsts %x\n",
  665. + mmc_hostname(host->mmc),
  666. + fsm_state, hsts);
  667. + if (hsts & SDHSTS_ERROR_MASK)
  668. + break;
  669. + }
  670. - bcm2835_sdhost_write(host, *(buf++), SDDATA);
  671. - len -= 4;
  672. + if (time_after(jiffies, wait_max)) {
  673. + pr_err("%s: PIO write timeout - EDM %x\n",
  674. + mmc_hostname(host->mmc),
  675. + edm);
  676. + hsts = SDHSTS_REW_TIME_OUT;
  677. + break;
  678. + }
  679. + ndelay((burst_words - words) *
  680. + host->ns_per_fifo_word);
  681. + continue;
  682. + } else if (words > copy_words) {
  683. + words = copy_words;
  684. + }
  685. +
  686. + copy_words -= words;
  687. +
  688. + while (words) {
  689. + bcm2835_sdhost_write(host, *(buf++), SDDATA);
  690. + words--;
  691. + }
  692. }
  693. - if (host->data->error)
  694. + if (hsts & SDHSTS_ERROR_MASK)
  695. break;
  696. }
  697. @@ -566,12 +728,12 @@ static void bcm2835_sdhost_write_block_p
  698. local_irq_restore(flags);
  699. }
  700. -
  701. static void bcm2835_sdhost_transfer_pio(struct bcm2835_host *host)
  702. {
  703. u32 sdhsts;
  704. bool is_read;
  705. BUG_ON(!host->data);
  706. + log_event("XFP<", (u32)host->data, host->blocks);
  707. is_read = (host->data->flags & MMC_DATA_READ) != 0;
  708. if (is_read)
  709. @@ -595,28 +757,21 @@ static void bcm2835_sdhost_transfer_pio(
  710. is_read ? "read" : "write",
  711. sdhsts);
  712. host->data->error = -ETIMEDOUT;
  713. - } else if (!is_read && !host->data->error) {
  714. - /* Start a timer in case a transfer error occurs because
  715. - there is no error interrupt */
  716. - mod_timer(&host->pio_timer, jiffies + host->pio_timeout);
  717. }
  718. + log_event("XFP>", (u32)host->data, host->blocks);
  719. }
  720. -
  721. -static void bcm2835_sdhost_transfer_dma(struct bcm2835_host *host)
  722. +static void bcm2835_sdhost_prepare_dma(struct bcm2835_host *host,
  723. + struct mmc_data *data)
  724. {
  725. - u32 len, dir_data, dir_slave;
  726. + int len, dir_data, dir_slave;
  727. struct dma_async_tx_descriptor *desc = NULL;
  728. struct dma_chan *dma_chan;
  729. - pr_debug("bcm2835_sdhost_transfer_dma()\n");
  730. -
  731. - WARN_ON(!host->data);
  732. + log_event("PRD<", (u32)data, 0);
  733. + pr_debug("bcm2835_sdhost_prepare_dma()\n");
  734. - if (!host->data)
  735. - return;
  736. -
  737. - if (host->data->flags & MMC_DATA_READ) {
  738. + if (data->flags & MMC_DATA_READ) {
  739. dma_chan = host->dma_chan_rx;
  740. dir_data = DMA_FROM_DEVICE;
  741. dir_slave = DMA_DEV_TO_MEM;
  742. @@ -625,35 +780,71 @@ static void bcm2835_sdhost_transfer_dma(
  743. dir_data = DMA_TO_DEVICE;
  744. dir_slave = DMA_MEM_TO_DEV;
  745. }
  746. + log_event("PRD1", (u32)dma_chan, 0);
  747. BUG_ON(!dma_chan->device);
  748. BUG_ON(!dma_chan->device->dev);
  749. - BUG_ON(!host->data->sg);
  750. + BUG_ON(!data->sg);
  751. - len = dma_map_sg(dma_chan->device->dev, host->data->sg,
  752. - host->data->sg_len, dir_data);
  753. - if (len > 0) {
  754. - desc = dmaengine_prep_slave_sg(dma_chan, host->data->sg,
  755. + /* The block doesn't manage the FIFO DREQs properly for multi-block
  756. + transfers, so don't attempt to DMA the final few words.
  757. + Unfortunately this requires the final sg entry to be trimmed.
  758. + N.B. This code demands that the overspill is contained in
  759. + a single sg entry.
  760. + */
  761. +
  762. + host->drain_words = 0;
  763. + if ((data->blocks > 1) && (dir_data == DMA_FROM_DEVICE)) {
  764. + struct scatterlist *sg;
  765. + u32 len;
  766. + int i;
  767. +
  768. + len = min((u32)(FIFO_READ_THRESHOLD - 1) * 4,
  769. + (u32)data->blocks * data->blksz);
  770. +
  771. + for_each_sg(data->sg, sg, data->sg_len, i) {
  772. + if (sg_is_last(sg)) {
  773. + BUG_ON(sg->length < len);
  774. + sg->length -= len;
  775. + host->drain_page = (struct page *)sg->page_link;
  776. + host->drain_offset = sg->offset + sg->length;
  777. + }
  778. + }
  779. + host->drain_words = len/4;
  780. + }
  781. +
  782. + len = dma_map_sg(dma_chan->device->dev, data->sg, data->sg_len,
  783. + dir_data);
  784. +
  785. + log_event("PRD2", len, 0);
  786. + if (len > 0)
  787. + desc = dmaengine_prep_slave_sg(dma_chan, data->sg,
  788. len, dir_slave,
  789. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  790. - } else {
  791. - dev_err(mmc_dev(host->mmc), "dma_map_sg returned zero length\n");
  792. - }
  793. + log_event("PRD3", (u32)desc, 0);
  794. +
  795. if (desc) {
  796. desc->callback = bcm2835_sdhost_dma_complete;
  797. desc->callback_param = host;
  798. - dmaengine_submit(desc);
  799. - dma_async_issue_pending(dma_chan);
  800. + host->dma_desc = desc;
  801. + host->dma_chan = dma_chan;
  802. + host->dma_dir = dir_data;
  803. }
  804. -
  805. + log_event("PDM>", (u32)data, 0);
  806. }
  807. +static void bcm2835_sdhost_start_dma(struct bcm2835_host *host)
  808. +{
  809. + log_event("SDMA", (u32)host->data, (u32)host->dma_chan);
  810. + dmaengine_submit(host->dma_desc);
  811. + dma_async_issue_pending(host->dma_chan);
  812. +}
  813. static void bcm2835_sdhost_set_transfer_irqs(struct bcm2835_host *host)
  814. {
  815. u32 all_irqs = SDHCFG_DATA_IRPT_EN | SDHCFG_BLOCK_IRPT_EN |
  816. SDHCFG_BUSY_IRPT_EN;
  817. - if (host->use_dma)
  818. + if (host->dma_desc)
  819. host->hcfg = (host->hcfg & ~all_irqs) |
  820. SDHCFG_BUSY_IRPT_EN;
  821. else
  822. @@ -664,13 +855,13 @@ static void bcm2835_sdhost_set_transfer_
  823. bcm2835_sdhost_write(host, host->hcfg, SDHCFG);
  824. }
  825. -
  826. static void bcm2835_sdhost_prepare_data(struct bcm2835_host *host, struct mmc_command *cmd)
  827. {
  828. struct mmc_data *data = cmd->data;
  829. WARN_ON(host->data);
  830. + host->data = data;
  831. if (!data)
  832. return;
  833. @@ -679,46 +870,19 @@ static void bcm2835_sdhost_prepare_data(
  834. BUG_ON(data->blksz > host->mmc->max_blk_size);
  835. BUG_ON(data->blocks > 65535);
  836. - host->data = data;
  837. host->data_complete = 0;
  838. host->flush_fifo = 0;
  839. host->data->bytes_xfered = 0;
  840. - if (!host->sectors && host->mmc->card && !(host->debug_flags & 1))
  841. - {
  842. - struct mmc_card *card = host->mmc->card;
  843. - if (!mmc_card_sd(card) && mmc_card_blockaddr(card)) {
  844. - /*
  845. - * The EXT_CSD sector count is in number of 512 byte
  846. - * sectors.
  847. - */
  848. - host->sectors = card->ext_csd.sectors;
  849. - pr_err("%s: using ext_csd!\n", mmc_hostname(host->mmc));
  850. - } else {
  851. - /*
  852. - * The CSD capacity field is in units of read_blkbits.
  853. - * set_capacity takes units of 512 bytes.
  854. - */
  855. - host->sectors = card->csd.capacity <<
  856. - (card->csd.read_blkbits - 9);
  857. - }
  858. - host->single_read_sectors[0] = host->sectors - 65;
  859. - host->single_read_sectors[1] = host->sectors - 64;
  860. - host->single_read_sectors[2] = host->sectors - 33;
  861. - host->single_read_sectors[3] = host->sectors - 32;
  862. - host->single_read_sectors[4] = host->sectors - 1;
  863. - host->single_read_sectors[5] = ~0; /* Safety net */
  864. - }
  865. - host->use_dma = host->have_dma && (data->blocks > host->pio_limit);
  866. - if (!host->use_dma) {
  867. + if (!host->dma_desc) {
  868. + /* Use PIO */
  869. int flags;
  870. - flags = SG_MITER_ATOMIC;
  871. if (data->flags & MMC_DATA_READ)
  872. - flags |= SG_MITER_TO_SG;
  873. + flags = SG_MITER_TO_SG;
  874. else
  875. - flags |= SG_MITER_FROM_SG;
  876. + flags = SG_MITER_FROM_SG;
  877. sg_miter_start(&host->sg_miter, data->sg, data->sg_len, flags);
  878. host->blocks = data->blocks;
  879. }
  880. @@ -726,19 +890,20 @@ static void bcm2835_sdhost_prepare_data(
  881. bcm2835_sdhost_set_transfer_irqs(host);
  882. bcm2835_sdhost_write(host, data->blksz, SDHBCT);
  883. - bcm2835_sdhost_write(host, host->use_dma ? data->blocks : 0, SDHBLC);
  884. + bcm2835_sdhost_write(host, data->blocks, SDHBLC);
  885. BUG_ON(!host->data);
  886. }
  887. -
  888. -void bcm2835_sdhost_send_command(struct bcm2835_host *host, struct mmc_command *cmd)
  889. +bool bcm2835_sdhost_send_command(struct bcm2835_host *host,
  890. + struct mmc_command *cmd)
  891. {
  892. u32 sdcmd, sdhsts;
  893. unsigned long timeout;
  894. int delay;
  895. WARN_ON(host->cmd);
  896. + log_event("CMD<", cmd->opcode, cmd->arg);
  897. if (cmd->data)
  898. pr_debug("%s: send_command %d 0x%x "
  899. @@ -761,9 +926,9 @@ void bcm2835_sdhost_send_command(struct
  900. pr_err("%s: previous command never completed.\n",
  901. mmc_hostname(host->mmc));
  902. bcm2835_sdhost_dumpregs(host);
  903. - cmd->error = -EIO;
  904. + cmd->error = -EILSEQ;
  905. tasklet_schedule(&host->finish_tasklet);
  906. - return;
  907. + return false;
  908. }
  909. timeout--;
  910. udelay(10);
  911. @@ -791,23 +956,24 @@ void bcm2835_sdhost_send_command(struct
  912. if (sdhsts & SDHSTS_ERROR_MASK)
  913. bcm2835_sdhost_write(host, sdhsts, SDHSTS);
  914. - bcm2835_sdhost_prepare_data(host, cmd);
  915. -
  916. - bcm2835_sdhost_write(host, cmd->arg, SDARG);
  917. -
  918. if ((cmd->flags & MMC_RSP_136) && (cmd->flags & MMC_RSP_BUSY)) {
  919. pr_err("%s: unsupported response type!\n",
  920. mmc_hostname(host->mmc));
  921. cmd->error = -EINVAL;
  922. tasklet_schedule(&host->finish_tasklet);
  923. - return;
  924. + return false;
  925. }
  926. + bcm2835_sdhost_prepare_data(host, cmd);
  927. +
  928. + bcm2835_sdhost_write(host, cmd->arg, SDARG);
  929. +
  930. sdcmd = cmd->opcode & SDCMD_CMD_MASK;
  931. - if (!(cmd->flags & MMC_RSP_PRESENT))
  932. + host->use_busy = 0;
  933. + if (!(cmd->flags & MMC_RSP_PRESENT)) {
  934. sdcmd |= SDCMD_NO_RESPONSE;
  935. - else {
  936. + } else {
  937. if (cmd->flags & MMC_RSP_136)
  938. sdcmd |= SDCMD_LONG_RESPONSE;
  939. if (cmd->flags & MMC_RSP_BUSY) {
  940. @@ -817,6 +983,7 @@ void bcm2835_sdhost_send_command(struct
  941. }
  942. if (cmd->data) {
  943. + log_event("CMDD", cmd->data->blocks, cmd->data->blksz);
  944. if (host->delay_after_stop) {
  945. struct timeval now;
  946. int time_since_stop;
  947. @@ -839,10 +1006,12 @@ void bcm2835_sdhost_send_command(struct
  948. }
  949. bcm2835_sdhost_write(host, sdcmd | SDCMD_NEW_FLAG, SDCMD);
  950. -}
  951. + return true;
  952. +}
  953. -static void bcm2835_sdhost_finish_command(struct bcm2835_host *host);
  954. +static void bcm2835_sdhost_finish_command(struct bcm2835_host *host,
  955. + unsigned long *irq_flags);
  956. static void bcm2835_sdhost_transfer_complete(struct bcm2835_host *host);
  957. static void bcm2835_sdhost_finish_data(struct bcm2835_host *host)
  958. @@ -852,6 +1021,7 @@ static void bcm2835_sdhost_finish_data(s
  959. data = host->data;
  960. BUG_ON(!data);
  961. + log_event("FDA<", (u32)host->mrq, (u32)host->cmd);
  962. pr_debug("finish_data(error %d, stop %d, sbc %d)\n",
  963. data->error, data->stop ? 1 : 0,
  964. host->mrq->sbc ? 1 : 0);
  965. @@ -859,10 +1029,7 @@ static void bcm2835_sdhost_finish_data(s
  966. host->hcfg &= ~(SDHCFG_DATA_IRPT_EN | SDHCFG_BLOCK_IRPT_EN);
  967. bcm2835_sdhost_write(host, host->hcfg, SDHCFG);
  968. - if (data->error) {
  969. - data->bytes_xfered = 0;
  970. - } else
  971. - data->bytes_xfered = data->blksz * data->blocks;
  972. + data->bytes_xfered = data->error ? 0 : (data->blksz * data->blocks);
  973. host->data_complete = 1;
  974. @@ -877,9 +1044,9 @@ static void bcm2835_sdhost_finish_data(s
  975. }
  976. else
  977. bcm2835_sdhost_transfer_complete(host);
  978. + log_event("FDA>", (u32)host->mrq, (u32)host->cmd);
  979. }
  980. -
  981. static void bcm2835_sdhost_transfer_complete(struct bcm2835_host *host)
  982. {
  983. struct mmc_data *data;
  984. @@ -891,6 +1058,7 @@ static void bcm2835_sdhost_transfer_comp
  985. data = host->data;
  986. host->data = NULL;
  987. + log_event("TCM<", (u32)data, data->error);
  988. pr_debug("transfer_complete(error %d, stop %d)\n",
  989. data->error, data->stop ? 1 : 0);
  990. @@ -899,88 +1067,114 @@ static void bcm2835_sdhost_transfer_comp
  991. * a) open-ended multiblock transfer (no CMD23)
  992. * b) error in multiblock transfer
  993. */
  994. - if (data->stop &&
  995. - (data->error ||
  996. - !host->mrq->sbc)) {
  997. - host->flush_fifo = 1;
  998. - bcm2835_sdhost_send_command(host, data->stop);
  999. - if (host->delay_after_stop)
  1000. - do_gettimeofday(&host->stop_time);
  1001. - if (!host->use_busy)
  1002. - bcm2835_sdhost_finish_command(host);
  1003. + if (host->mrq->stop && (data->error || !host->use_sbc)) {
  1004. + if (bcm2835_sdhost_send_command(host, host->mrq->stop)) {
  1005. + /* No busy, so poll for completion */
  1006. + if (!host->use_busy)
  1007. + bcm2835_sdhost_finish_command(host, NULL);
  1008. +
  1009. + if (host->delay_after_stop)
  1010. + do_gettimeofday(&host->stop_time);
  1011. + }
  1012. } else {
  1013. + bcm2835_sdhost_wait_transfer_complete(host);
  1014. tasklet_schedule(&host->finish_tasklet);
  1015. }
  1016. + log_event("TCM>", (u32)data, 0);
  1017. }
  1018. -static void bcm2835_sdhost_finish_command(struct bcm2835_host *host)
  1019. +/* If irq_flags is valid, the caller is in a thread context and is allowed
  1020. + to sleep */
  1021. +static void bcm2835_sdhost_finish_command(struct bcm2835_host *host,
  1022. + unsigned long *irq_flags)
  1023. {
  1024. u32 sdcmd;
  1025. - unsigned long timeout;
  1026. + u32 retries;
  1027. #ifdef DEBUG
  1028. struct timeval before, after;
  1029. int timediff = 0;
  1030. #endif
  1031. + log_event("FCM<", (u32)host->mrq, (u32)host->cmd);
  1032. pr_debug("finish_command(%x)\n", bcm2835_sdhost_read(host, SDCMD));
  1033. BUG_ON(!host->cmd || !host->mrq);
  1034. -#ifdef DEBUG
  1035. - do_gettimeofday(&before);
  1036. -#endif
  1037. - /* Wait max 100 ms */
  1038. - timeout = 10000;
  1039. + /* Poll quickly at first */
  1040. +
  1041. + retries = host->cmd_quick_poll_retries;
  1042. + if (!retries) {
  1043. + /* Work out how many polls take 1us by timing 10us */
  1044. + struct timeval start, now;
  1045. + int us_diff;
  1046. +
  1047. + retries = 1;
  1048. + do {
  1049. + int i;
  1050. +
  1051. + retries *= 2;
  1052. +
  1053. + do_gettimeofday(&start);
  1054. +
  1055. + for (i = 0; i < retries; i++) {
  1056. + cpu_relax();
  1057. + sdcmd = bcm2835_sdhost_read(host, SDCMD);
  1058. + }
  1059. +
  1060. + do_gettimeofday(&now);
  1061. + us_diff = (now.tv_sec - start.tv_sec) * 1000000 +
  1062. + (now.tv_usec - start.tv_usec);
  1063. + } while (us_diff < 10);
  1064. +
  1065. + host->cmd_quick_poll_retries = ((retries * us_diff + 9)*CMD_DALLY_US)/10 + 1;
  1066. + retries = 1; // We've already waited long enough this time
  1067. + }
  1068. +
  1069. + retries = host->cmd_quick_poll_retries;
  1070. for (sdcmd = bcm2835_sdhost_read(host, SDCMD);
  1071. - (sdcmd & SDCMD_NEW_FLAG) && timeout;
  1072. - timeout--) {
  1073. - if (host->flush_fifo) {
  1074. - while (bcm2835_sdhost_read(host, SDHSTS) &
  1075. - SDHSTS_DATA_FLAG)
  1076. - (void)bcm2835_sdhost_read(host, SDDATA);
  1077. - }
  1078. - udelay(10);
  1079. + (sdcmd & SDCMD_NEW_FLAG) && !(sdcmd & SDCMD_FAIL_FLAG) && retries;
  1080. + retries--) {
  1081. + cpu_relax();
  1082. sdcmd = bcm2835_sdhost_read(host, SDCMD);
  1083. }
  1084. -#ifdef DEBUG
  1085. - do_gettimeofday(&after);
  1086. - timediff = (after.tv_sec - before.tv_sec)*1000000 +
  1087. - (after.tv_usec - before.tv_usec);
  1088. - pr_debug(" finish_command - waited %dus\n", timediff);
  1089. -#endif
  1090. + if (!retries) {
  1091. + unsigned long wait_max;
  1092. +
  1093. + if (!irq_flags) {
  1094. + /* Schedule the work */
  1095. + log_event("CWWQ", 0, 0);
  1096. + schedule_work(&host->cmd_wait_wq);
  1097. + return;
  1098. + }
  1099. +
  1100. + /* Wait max 100 ms */
  1101. + wait_max = jiffies + msecs_to_jiffies(100);
  1102. + while (time_before(jiffies, wait_max)) {
  1103. + spin_unlock_irqrestore(&host->lock, *irq_flags);
  1104. + usleep_range(1, 10);
  1105. + spin_lock_irqsave(&host->lock, *irq_flags);
  1106. + sdcmd = bcm2835_sdhost_read(host, SDCMD);
  1107. + if (!(sdcmd & SDCMD_NEW_FLAG) ||
  1108. + (sdcmd & SDCMD_FAIL_FLAG))
  1109. + break;
  1110. + }
  1111. + }
  1112. - if (timeout == 0) {
  1113. + /* Check for errors */
  1114. + if (sdcmd & SDCMD_NEW_FLAG) {
  1115. pr_err("%s: command never completed.\n",
  1116. mmc_hostname(host->mmc));
  1117. bcm2835_sdhost_dumpregs(host);
  1118. host->cmd->error = -EIO;
  1119. tasklet_schedule(&host->finish_tasklet);
  1120. return;
  1121. - }
  1122. -
  1123. - if (host->flush_fifo) {
  1124. - for (timeout = 100;
  1125. - (bcm2835_sdhost_read(host, SDHSTS) & SDHSTS_DATA_FLAG) && timeout;
  1126. - timeout--) {
  1127. - (void)bcm2835_sdhost_read(host, SDDATA);
  1128. - }
  1129. - host->flush_fifo = 0;
  1130. - if (timeout == 0) {
  1131. - pr_err("%s: FIFO never drained.\n",
  1132. - mmc_hostname(host->mmc));
  1133. - bcm2835_sdhost_dumpregs(host);
  1134. - host->cmd->error = -EIO;
  1135. - tasklet_schedule(&host->finish_tasklet);
  1136. - return;
  1137. - }
  1138. - }
  1139. -
  1140. - /* Check for errors */
  1141. - if (sdcmd & SDCMD_FAIL_FLAG)
  1142. - {
  1143. + } else if (sdcmd & SDCMD_FAIL_FLAG) {
  1144. u32 sdhsts = bcm2835_sdhost_read(host, SDHSTS);
  1145. + /* Clear the errors */
  1146. + bcm2835_sdhost_write(host, SDHSTS_ERROR_MASK, SDHSTS);
  1147. +
  1148. if (host->debug)
  1149. pr_info("%s: error detected - CMD %x, HSTS %03x, EDM %x\n",
  1150. mmc_hostname(host->mmc), sdcmd, sdhsts,
  1151. @@ -1003,7 +1197,7 @@ static void bcm2835_sdhost_finish_comman
  1152. mmc_hostname(host->mmc),
  1153. host->cmd->opcode);
  1154. bcm2835_sdhost_dumpregs(host);
  1155. - host->cmd->error = -EIO;
  1156. + host->cmd->error = -EILSEQ;
  1157. }
  1158. tasklet_schedule(&host->finish_tasklet);
  1159. return;
  1160. @@ -1018,31 +1212,31 @@ static void bcm2835_sdhost_finish_comman
  1161. pr_debug("%s: finish_command %08x %08x %08x %08x\n",
  1162. mmc_hostname(host->mmc),
  1163. host->cmd->resp[0], host->cmd->resp[1], host->cmd->resp[2], host->cmd->resp[3]);
  1164. + log_event("RSP ", host->cmd->resp[0], host->cmd->resp[1]);
  1165. } else {
  1166. host->cmd->resp[0] = bcm2835_sdhost_read(host, SDRSP0);
  1167. pr_debug("%s: finish_command %08x\n",
  1168. mmc_hostname(host->mmc),
  1169. host->cmd->resp[0]);
  1170. + log_event("RSP ", host->cmd->resp[0], 0);
  1171. }
  1172. }
  1173. - host->cmd->error = 0;
  1174. -
  1175. if (host->cmd == host->mrq->sbc) {
  1176. /* Finished CMD23, now send actual command. */
  1177. host->cmd = NULL;
  1178. - bcm2835_sdhost_send_command(host, host->mrq->cmd);
  1179. + if (bcm2835_sdhost_send_command(host, host->mrq->cmd)) {
  1180. + if (host->data && host->dma_desc)
  1181. + /* DMA transfer starts now, PIO starts after irq */
  1182. + bcm2835_sdhost_start_dma(host);
  1183. - if (host->cmd->data && host->use_dma)
  1184. - /* DMA transfer starts now, PIO starts after irq */
  1185. - bcm2835_sdhost_transfer_dma(host);
  1186. -
  1187. - if (!host->use_busy)
  1188. - bcm2835_sdhost_finish_command(host);
  1189. - } else if (host->cmd == host->mrq->stop)
  1190. + if (!host->use_busy)
  1191. + bcm2835_sdhost_finish_command(host, NULL);
  1192. + }
  1193. + } else if (host->cmd == host->mrq->stop) {
  1194. /* Finished CMD12 */
  1195. tasklet_schedule(&host->finish_tasklet);
  1196. - else {
  1197. + } else {
  1198. /* Processed actual command. */
  1199. host->cmd = NULL;
  1200. if (!host->data)
  1201. @@ -1050,6 +1244,7 @@ static void bcm2835_sdhost_finish_comman
  1202. else if (host->data_complete)
  1203. bcm2835_sdhost_transfer_complete(host);
  1204. }
  1205. + log_event("FCM>", (u32)host->mrq, (u32)host->cmd);
  1206. }
  1207. static void bcm2835_sdhost_timeout(unsigned long data)
  1208. @@ -1060,10 +1255,12 @@ static void bcm2835_sdhost_timeout(unsig
  1209. host = (struct bcm2835_host *)data;
  1210. spin_lock_irqsave(&host->lock, flags);
  1211. + log_event("TIM<", 0, 0);
  1212. if (host->mrq) {
  1213. pr_err("%s: timeout waiting for hardware interrupt.\n",
  1214. mmc_hostname(host->mmc));
  1215. + log_dump();
  1216. bcm2835_sdhost_dumpregs(host);
  1217. if (host->data) {
  1218. @@ -1084,74 +1281,15 @@ static void bcm2835_sdhost_timeout(unsig
  1219. spin_unlock_irqrestore(&host->lock, flags);
  1220. }
  1221. -static void bcm2835_sdhost_pio_timeout(unsigned long data)
  1222. -{
  1223. - struct bcm2835_host *host;
  1224. - unsigned long flags;
  1225. -
  1226. - host = (struct bcm2835_host *)data;
  1227. -
  1228. - spin_lock_irqsave(&host->lock, flags);
  1229. -
  1230. - if (host->data) {
  1231. - u32 sdhsts = bcm2835_sdhost_read(host, SDHSTS);
  1232. -
  1233. - if (sdhsts & SDHSTS_REW_TIME_OUT) {
  1234. - pr_err("%s: transfer timeout\n",
  1235. - mmc_hostname(host->mmc));
  1236. - if (host->debug)
  1237. - bcm2835_sdhost_dumpregs(host);
  1238. - } else {
  1239. - pr_err("%s: unexpected transfer timeout\n",
  1240. - mmc_hostname(host->mmc));
  1241. - bcm2835_sdhost_dumpregs(host);
  1242. - }
  1243. -
  1244. - bcm2835_sdhost_write(host, SDHSTS_TRANSFER_ERROR_MASK,
  1245. - SDHSTS);
  1246. -
  1247. - host->data->error = -ETIMEDOUT;
  1248. -
  1249. - bcm2835_sdhost_finish_data(host);
  1250. - }
  1251. -
  1252. - mmiowb();
  1253. - spin_unlock_irqrestore(&host->lock, flags);
  1254. -}
  1255. -
  1256. -static void bcm2835_sdhost_enable_sdio_irq_nolock(struct bcm2835_host *host, int enable)
  1257. -{
  1258. - if (enable)
  1259. - host->hcfg |= SDHCFG_SDIO_IRPT_EN;
  1260. - else
  1261. - host->hcfg &= ~SDHCFG_SDIO_IRPT_EN;
  1262. - bcm2835_sdhost_write(host, host->hcfg, SDHCFG);
  1263. - mmiowb();
  1264. -}
  1265. -
  1266. -static void bcm2835_sdhost_enable_sdio_irq(struct mmc_host *mmc, int enable)
  1267. -{
  1268. - struct bcm2835_host *host = mmc_priv(mmc);
  1269. - unsigned long flags;
  1270. -
  1271. - pr_debug("%s: enable_sdio_irq(%d)\n", mmc_hostname(mmc), enable);
  1272. - spin_lock_irqsave(&host->lock, flags);
  1273. - bcm2835_sdhost_enable_sdio_irq_nolock(host, enable);
  1274. - spin_unlock_irqrestore(&host->lock, flags);
  1275. -}
  1276. -
  1277. -static u32 bcm2835_sdhost_busy_irq(struct bcm2835_host *host, u32 intmask)
  1278. +static void bcm2835_sdhost_busy_irq(struct bcm2835_host *host, u32 intmask)
  1279. {
  1280. - const u32 handled = (SDHSTS_REW_TIME_OUT | SDHSTS_CMD_TIME_OUT |
  1281. - SDHSTS_CRC16_ERROR | SDHSTS_CRC7_ERROR |
  1282. - SDHSTS_FIFO_ERROR);
  1283. -
  1284. + log_event("IRQB", (u32)host->cmd, intmask);
  1285. if (!host->cmd) {
  1286. pr_err("%s: got command busy interrupt 0x%08x even "
  1287. "though no command operation was in progress.\n",
  1288. mmc_hostname(host->mmc), (unsigned)intmask);
  1289. bcm2835_sdhost_dumpregs(host);
  1290. - return 0;
  1291. + return;
  1292. }
  1293. if (!host->use_busy) {
  1294. @@ -1159,7 +1297,7 @@ static u32 bcm2835_sdhost_busy_irq(struc
  1295. "though not expecting one.\n",
  1296. mmc_hostname(host->mmc), (unsigned)intmask);
  1297. bcm2835_sdhost_dumpregs(host);
  1298. - return 0;
  1299. + return;
  1300. }
  1301. host->use_busy = 0;
  1302. @@ -1182,28 +1320,23 @@ static u32 bcm2835_sdhost_busy_irq(struc
  1303. } else if (intmask & SDHSTS_CMD_TIME_OUT)
  1304. host->cmd->error = -ETIMEDOUT;
  1305. + log_dump();
  1306. bcm2835_sdhost_dumpregs(host);
  1307. - tasklet_schedule(&host->finish_tasklet);
  1308. }
  1309. else
  1310. - bcm2835_sdhost_finish_command(host);
  1311. -
  1312. - return handled;
  1313. + bcm2835_sdhost_finish_command(host, NULL);
  1314. }
  1315. -static u32 bcm2835_sdhost_data_irq(struct bcm2835_host *host, u32 intmask)
  1316. +static void bcm2835_sdhost_data_irq(struct bcm2835_host *host, u32 intmask)
  1317. {
  1318. - const u32 handled = (SDHSTS_REW_TIME_OUT |
  1319. - SDHSTS_CRC16_ERROR |
  1320. - SDHSTS_FIFO_ERROR);
  1321. -
  1322. /* There are no dedicated data/space available interrupt
  1323. status bits, so it is necessary to use the single shared
  1324. data/space available FIFO status bits. It is therefore not
  1325. an error to get here when there is no data transfer in
  1326. progress. */
  1327. + log_event("IRQD", (u32)host->data, intmask);
  1328. if (!host->data)
  1329. - return 0;
  1330. + return;
  1331. if (intmask & (SDHSTS_CRC16_ERROR |
  1332. SDHSTS_FIFO_ERROR |
  1333. @@ -1214,46 +1347,37 @@ static u32 bcm2835_sdhost_data_irq(struc
  1334. else
  1335. host->data->error = -ETIMEDOUT;
  1336. - bcm2835_sdhost_dumpregs(host);
  1337. - tasklet_schedule(&host->finish_tasklet);
  1338. - return handled;
  1339. + if (host->debug) {
  1340. + log_dump();
  1341. + bcm2835_sdhost_dumpregs(host);
  1342. + }
  1343. }
  1344. - /* Use the block interrupt for writes after the first block */
  1345. - if (host->data->flags & MMC_DATA_WRITE) {
  1346. + if (host->data->error) {
  1347. + bcm2835_sdhost_finish_data(host);
  1348. + } else if (host->data->flags & MMC_DATA_WRITE) {
  1349. + /* Use the block interrupt for writes after the first block */
  1350. host->hcfg &= ~(SDHCFG_DATA_IRPT_EN);
  1351. host->hcfg |= SDHCFG_BLOCK_IRPT_EN;
  1352. bcm2835_sdhost_write(host, host->hcfg, SDHCFG);
  1353. - if (host->data->error)
  1354. - bcm2835_sdhost_finish_data(host);
  1355. - else
  1356. - bcm2835_sdhost_transfer_pio(host);
  1357. + bcm2835_sdhost_transfer_pio(host);
  1358. } else {
  1359. - if (!host->data->error) {
  1360. - bcm2835_sdhost_transfer_pio(host);
  1361. - host->blocks--;
  1362. - }
  1363. + bcm2835_sdhost_transfer_pio(host);
  1364. + host->blocks--;
  1365. if ((host->blocks == 0) || host->data->error)
  1366. bcm2835_sdhost_finish_data(host);
  1367. }
  1368. -
  1369. - return handled;
  1370. }
  1371. -static u32 bcm2835_sdhost_block_irq(struct bcm2835_host *host, u32 intmask)
  1372. +static void bcm2835_sdhost_block_irq(struct bcm2835_host *host, u32 intmask)
  1373. {
  1374. - struct dma_chan *dma_chan;
  1375. - u32 dir_data;
  1376. - const u32 handled = (SDHSTS_REW_TIME_OUT |
  1377. - SDHSTS_CRC16_ERROR |
  1378. - SDHSTS_FIFO_ERROR);
  1379. -
  1380. + log_event("IRQK", (u32)host->data, intmask);
  1381. if (!host->data) {
  1382. pr_err("%s: got block interrupt 0x%08x even "
  1383. "though no data operation was in progress.\n",
  1384. mmc_hostname(host->mmc), (unsigned)intmask);
  1385. bcm2835_sdhost_dumpregs(host);
  1386. - return handled;
  1387. + return;
  1388. }
  1389. if (intmask & (SDHSTS_CRC16_ERROR |
  1390. @@ -1265,149 +1389,69 @@ static u32 bcm2835_sdhost_block_irq(stru
  1391. else
  1392. host->data->error = -ETIMEDOUT;
  1393. - if (host->debug)
  1394. + if (host->debug) {
  1395. + log_dump();
  1396. bcm2835_sdhost_dumpregs(host);
  1397. - tasklet_schedule(&host->finish_tasklet);
  1398. - return handled;
  1399. + }
  1400. }
  1401. - if (!host->use_dma) {
  1402. + if (!host->dma_desc) {
  1403. BUG_ON(!host->blocks);
  1404. - host->blocks--;
  1405. - if ((host->blocks == 0) || host->data->error) {
  1406. - /* Cancel the timer */
  1407. - del_timer(&host->pio_timer);
  1408. -
  1409. + if (host->data->error || (--host->blocks == 0)) {
  1410. bcm2835_sdhost_finish_data(host);
  1411. } else {
  1412. - /* Reset the timer */
  1413. - mod_timer(&host->pio_timer,
  1414. - jiffies + host->pio_timeout);
  1415. -
  1416. bcm2835_sdhost_transfer_pio(host);
  1417. -
  1418. - /* Reset the timer */
  1419. - mod_timer(&host->pio_timer,
  1420. - jiffies + host->pio_timeout);
  1421. }
  1422. } else if (host->data->flags & MMC_DATA_WRITE) {
  1423. - dma_chan = host->dma_chan_tx;
  1424. - dir_data = DMA_TO_DEVICE;
  1425. - dma_unmap_sg(dma_chan->device->dev,
  1426. - host->data->sg, host->data->sg_len,
  1427. - dir_data);
  1428. -
  1429. bcm2835_sdhost_finish_data(host);
  1430. }
  1431. -
  1432. - return handled;
  1433. }
  1434. -
  1435. static irqreturn_t bcm2835_sdhost_irq(int irq, void *dev_id)
  1436. {
  1437. irqreturn_t result = IRQ_NONE;
  1438. struct bcm2835_host *host = dev_id;
  1439. - u32 unexpected = 0, early = 0;
  1440. - int loops = 0;
  1441. + u32 intmask;
  1442. spin_lock(&host->lock);
  1443. - for (loops = 0; loops < 1; loops++) {
  1444. - u32 intmask, handled;
  1445. -
  1446. - intmask = bcm2835_sdhost_read(host, SDHSTS);
  1447. - handled = intmask & (SDHSTS_BUSY_IRPT |
  1448. - SDHSTS_BLOCK_IRPT |
  1449. - SDHSTS_SDIO_IRPT |
  1450. - SDHSTS_DATA_FLAG);
  1451. - if ((handled == SDHSTS_DATA_FLAG) &&
  1452. - (loops == 0) && !host->data) {
  1453. - pr_err("%s: sdhost_irq data interrupt 0x%08x even "
  1454. - "though no data operation was in progress.\n",
  1455. - mmc_hostname(host->mmc),
  1456. - (unsigned)intmask);
  1457. -
  1458. - bcm2835_sdhost_dumpregs(host);
  1459. - }
  1460. -
  1461. - if (!handled)
  1462. - break;
  1463. + intmask = bcm2835_sdhost_read(host, SDHSTS);
  1464. + log_event("IRQ<", intmask, 0);
  1465. - if (loops)
  1466. - early |= handled;
  1467. + bcm2835_sdhost_write(host,
  1468. + SDHSTS_BUSY_IRPT |
  1469. + SDHSTS_BLOCK_IRPT |
  1470. + SDHSTS_SDIO_IRPT |
  1471. + SDHSTS_DATA_FLAG,
  1472. + SDHSTS);
  1473. + if (intmask & SDHSTS_BLOCK_IRPT) {
  1474. + bcm2835_sdhost_block_irq(host, intmask);
  1475. result = IRQ_HANDLED;
  1476. + }
  1477. - /* Clear all interrupts and notifications */
  1478. - bcm2835_sdhost_write(host, intmask, SDHSTS);
  1479. -
  1480. - if (intmask & SDHSTS_BUSY_IRPT)
  1481. - handled |= bcm2835_sdhost_busy_irq(host, intmask);
  1482. -
  1483. - /* There is no true data interrupt status bit, so it is
  1484. - necessary to qualify the data flag with the interrupt
  1485. - enable bit */
  1486. - if ((intmask & SDHSTS_DATA_FLAG) &&
  1487. - (host->hcfg & SDHCFG_DATA_IRPT_EN))
  1488. - handled |= bcm2835_sdhost_data_irq(host, intmask);
  1489. -
  1490. - if (intmask & SDHSTS_BLOCK_IRPT)
  1491. - handled |= bcm2835_sdhost_block_irq(host, intmask);
  1492. -
  1493. - if (intmask & SDHSTS_SDIO_IRPT) {
  1494. - bcm2835_sdhost_enable_sdio_irq_nolock(host, false);
  1495. - host->thread_isr |= SDHSTS_SDIO_IRPT;
  1496. - result = IRQ_WAKE_THREAD;
  1497. - }
  1498. + if (intmask & SDHSTS_BUSY_IRPT) {
  1499. + bcm2835_sdhost_busy_irq(host, intmask);
  1500. + result = IRQ_HANDLED;
  1501. + }
  1502. - unexpected |= (intmask & ~handled);
  1503. + /* There is no true data interrupt status bit, so it is
  1504. + necessary to qualify the data flag with the interrupt
  1505. + enable bit */
  1506. + if ((intmask & SDHSTS_DATA_FLAG) &&
  1507. + (host->hcfg & SDHCFG_DATA_IRPT_EN)) {
  1508. + bcm2835_sdhost_data_irq(host, intmask);
  1509. + result = IRQ_HANDLED;
  1510. }
  1511. mmiowb();
  1512. + log_event("IRQ>", bcm2835_sdhost_read(host, SDHSTS), 0);
  1513. spin_unlock(&host->lock);
  1514. - if (early)
  1515. - pr_debug("%s: early %x (loops %d)\n",
  1516. - mmc_hostname(host->mmc), early, loops);
  1517. -
  1518. - if (unexpected) {
  1519. - pr_err("%s: unexpected interrupt 0x%08x.\n",
  1520. - mmc_hostname(host->mmc), unexpected);
  1521. - bcm2835_sdhost_dumpregs(host);
  1522. - }
  1523. -
  1524. return result;
  1525. }
  1526. -static irqreturn_t bcm2835_sdhost_thread_irq(int irq, void *dev_id)
  1527. -{
  1528. - struct bcm2835_host *host = dev_id;
  1529. - unsigned long flags;
  1530. - u32 isr;
  1531. -
  1532. - spin_lock_irqsave(&host->lock, flags);
  1533. - isr = host->thread_isr;
  1534. - host->thread_isr = 0;
  1535. - spin_unlock_irqrestore(&host->lock, flags);
  1536. -
  1537. - if (isr & SDHSTS_SDIO_IRPT) {
  1538. - sdio_run_irqs(host->mmc);
  1539. -
  1540. -/* Is this necessary? Why re-enable an interrupt which is enabled?
  1541. - spin_lock_irqsave(&host->lock, flags);
  1542. - if (host->flags & SDHSTS_SDIO_IRPT_ENABLED)
  1543. - bcm2835_sdhost_enable_sdio_irq_nolock(host, true);
  1544. - spin_unlock_irqrestore(&host->lock, flags);
  1545. -*/
  1546. - }
  1547. -
  1548. - return isr ? IRQ_HANDLED : IRQ_NONE;
  1549. -}
  1550. -
  1551. -
  1552. -
  1553. void bcm2835_sdhost_set_clock(struct bcm2835_host *host, unsigned int clock)
  1554. {
  1555. int div = 0; /* Initialized for compiler warning */
  1556. @@ -1417,9 +1461,8 @@ void bcm2835_sdhost_set_clock(struct bcm
  1557. pr_info("%s: set_clock(%d)\n", mmc_hostname(host->mmc), clock);
  1558. if ((host->overclock_50 > 50) &&
  1559. - (clock == 50*MHZ)) {
  1560. + (clock == 50*MHZ))
  1561. clock = host->overclock_50 * MHZ + (MHZ - 1);
  1562. - }
  1563. /* The SDCDIV register has 11 bits, and holds (div - 2).
  1564. But in data mode the max is 50MHz wihout a minimum, and only the
  1565. @@ -1466,6 +1509,11 @@ void bcm2835_sdhost_set_clock(struct bcm
  1566. clock = host->max_clk / (div + 2);
  1567. host->mmc->actual_clock = clock;
  1568. + /* Calibrate some delays */
  1569. +
  1570. + host->ns_per_fifo_word = (1000000000/clock) *
  1571. + ((host->mmc->caps & MMC_CAP_4_BIT_DATA) ? 8 : 32);
  1572. +
  1573. if (clock > input_clock) {
  1574. /* Save the closest value, to make it easier
  1575. to reduce in the event of error */
  1576. @@ -1501,6 +1549,7 @@ static void bcm2835_sdhost_request(struc
  1577. {
  1578. struct bcm2835_host *host;
  1579. unsigned long flags;
  1580. + u32 edm, fsm;
  1581. host = mmc_priv(mmc);
  1582. @@ -1521,6 +1570,8 @@ static void bcm2835_sdhost_request(struc
  1583. }
  1584. /* Reset the error statuses in case this is a retry */
  1585. + if (mrq->sbc)
  1586. + mrq->sbc->error = 0;
  1587. if (mrq->cmd)
  1588. mrq->cmd->error = 0;
  1589. if (mrq->data)
  1590. @@ -1536,28 +1587,58 @@ static void bcm2835_sdhost_request(struc
  1591. return;
  1592. }
  1593. + if (host->use_dma && mrq->data &&
  1594. + (mrq->data->blocks > host->pio_limit))
  1595. + bcm2835_sdhost_prepare_dma(host, mrq->data);
  1596. +
  1597. spin_lock_irqsave(&host->lock, flags);
  1598. WARN_ON(host->mrq != NULL);
  1599. -
  1600. host->mrq = mrq;
  1601. - if (mrq->sbc)
  1602. - bcm2835_sdhost_send_command(host, mrq->sbc);
  1603. - else
  1604. - bcm2835_sdhost_send_command(host, mrq->cmd);
  1605. + edm = bcm2835_sdhost_read(host, SDEDM);
  1606. + fsm = edm & SDEDM_FSM_MASK;
  1607. - mmiowb();
  1608. - spin_unlock_irqrestore(&host->lock, flags);
  1609. + log_event("REQ<", (u32)mrq, edm);
  1610. + if ((fsm != SDEDM_FSM_IDENTMODE) &&
  1611. + (fsm != SDEDM_FSM_DATAMODE)) {
  1612. + pr_err("%s: previous command (%d) not complete (EDM %x)\n",
  1613. + mmc_hostname(host->mmc),
  1614. + bcm2835_sdhost_read(host, SDCMD) & SDCMD_CMD_MASK,
  1615. + edm);
  1616. + log_event("REQ!", (u32)mrq, edm);
  1617. + log_dump();
  1618. + bcm2835_sdhost_dumpregs(host);
  1619. + mrq->cmd->error = -EILSEQ;
  1620. + tasklet_schedule(&host->finish_tasklet);
  1621. + mmiowb();
  1622. + spin_unlock_irqrestore(&host->lock, flags);
  1623. + return;
  1624. + }
  1625. +
  1626. + host->use_sbc = !!mrq->sbc &&
  1627. + (host->mrq->data->flags & USE_CMD23_FLAGS);
  1628. + if (host->use_sbc) {
  1629. + if (bcm2835_sdhost_send_command(host, mrq->sbc)) {
  1630. + if (!host->use_busy)
  1631. + bcm2835_sdhost_finish_command(host, &flags);
  1632. + }
  1633. + } else if (bcm2835_sdhost_send_command(host, mrq->cmd)) {
  1634. + if (host->data && host->dma_desc)
  1635. + /* DMA transfer starts now, PIO starts after irq */
  1636. + bcm2835_sdhost_start_dma(host);
  1637. - if (!mrq->sbc && mrq->cmd->data && host->use_dma)
  1638. - /* DMA transfer starts now, PIO starts after irq */
  1639. - bcm2835_sdhost_transfer_dma(host);
  1640. + if (!host->use_busy)
  1641. + bcm2835_sdhost_finish_command(host, &flags);
  1642. + }
  1643. - if (!host->use_busy)
  1644. - bcm2835_sdhost_finish_command(host);
  1645. -}
  1646. + log_event("CMD ", (u32)mrq->cmd->opcode,
  1647. + mrq->data ? (u32)mrq->data->blksz : 0);
  1648. + mmiowb();
  1649. + log_event("REQ>", (u32)mrq, 0);
  1650. + spin_unlock_irqrestore(&host->lock, flags);
  1651. +}
  1652. static void bcm2835_sdhost_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
  1653. {
  1654. @@ -1574,6 +1655,8 @@ static void bcm2835_sdhost_set_ios(struc
  1655. spin_lock_irqsave(&host->lock, flags);
  1656. + log_event("IOS<", ios->clock, 0);
  1657. +
  1658. if (!ios->clock || ios->clock != host->clock) {
  1659. bcm2835_sdhost_set_clock(host, ios->clock);
  1660. host->clock = ios->clock;
  1661. @@ -1596,59 +1679,53 @@ static void bcm2835_sdhost_set_ios(struc
  1662. spin_unlock_irqrestore(&host->lock, flags);
  1663. }
  1664. -static int bcm2835_sdhost_multi_io_quirk(struct mmc_card *card,
  1665. - unsigned int direction,
  1666. - u32 blk_pos, int blk_size)
  1667. -{
  1668. - /* There is a bug in the host controller hardware that makes
  1669. - reading the final sector of the card as part of a multiple read
  1670. - problematic. Detect that case and shorten the read accordingly.
  1671. - */
  1672. +static struct mmc_host_ops bcm2835_sdhost_ops = {
  1673. + .request = bcm2835_sdhost_request,
  1674. + .set_ios = bcm2835_sdhost_set_ios,
  1675. + .hw_reset = bcm2835_sdhost_reset,
  1676. +};
  1677. +
  1678. +static void bcm2835_sdhost_cmd_wait_work(struct work_struct *work)
  1679. +{
  1680. struct bcm2835_host *host;
  1681. + unsigned long flags;
  1682. - host = mmc_priv(card->host);
  1683. + host = container_of(work, struct bcm2835_host, cmd_wait_wq);
  1684. - if (!host->sectors) {
  1685. - /* csd.capacity is in weird units - convert to sectors */
  1686. - u32 card_sectors = (card->csd.capacity << (card->csd.read_blkbits - 9));
  1687. - if ((direction == MMC_DATA_READ) &&
  1688. - ((blk_pos + blk_size) == card_sectors))
  1689. - blk_size--;
  1690. - return blk_size;
  1691. - }
  1692. + spin_lock_irqsave(&host->lock, flags);
  1693. - if (direction == MMC_DATA_READ) {
  1694. - int i;
  1695. - int sector;
  1696. - for (i = 0; blk_pos > (sector = host->single_read_sectors[i]); i++)
  1697. - continue;
  1698. + log_event("CWK<", (u32)host->cmd, (u32)host->mrq);
  1699. - if ((blk_pos + blk_size) > sector)
  1700. - blk_size = (blk_pos == sector) ? 1 : (sector - blk_pos);
  1701. + /*
  1702. + * If this tasklet gets rescheduled while running, it will
  1703. + * be run again afterwards but without any active request.
  1704. + */
  1705. + if (!host->mrq) {
  1706. + spin_unlock_irqrestore(&host->lock, flags);
  1707. + return;
  1708. }
  1709. - return blk_size;
  1710. -}
  1711. + bcm2835_sdhost_finish_command(host, &flags);
  1712. -static struct mmc_host_ops bcm2835_sdhost_ops = {
  1713. - .request = bcm2835_sdhost_request,
  1714. - .set_ios = bcm2835_sdhost_set_ios,
  1715. - .enable_sdio_irq = bcm2835_sdhost_enable_sdio_irq,
  1716. - .hw_reset = bcm2835_sdhost_reset,
  1717. - .multi_io_quirk = bcm2835_sdhost_multi_io_quirk,
  1718. -};
  1719. + mmiowb();
  1720. +
  1721. + log_event("CWK>", (u32)host->cmd, 0);
  1722. + spin_unlock_irqrestore(&host->lock, flags);
  1723. +}
  1724. static void bcm2835_sdhost_tasklet_finish(unsigned long param)
  1725. {
  1726. struct bcm2835_host *host;
  1727. unsigned long flags;
  1728. struct mmc_request *mrq;
  1729. + struct dma_chan *terminate_chan = NULL;
  1730. host = (struct bcm2835_host *)param;
  1731. spin_lock_irqsave(&host->lock, flags);
  1732. + log_event("TSK<", (u32)host->mrq, 0);
  1733. /*
  1734. * If this tasklet gets rescheduled while running, it will
  1735. * be run again afterwards but without any active request.
  1736. @@ -1683,11 +1760,23 @@ static void bcm2835_sdhost_tasklet_finis
  1737. mmiowb();
  1738. + host->dma_desc = NULL;
  1739. + terminate_chan = host->dma_chan;
  1740. + host->dma_chan = NULL;
  1741. +
  1742. spin_unlock_irqrestore(&host->lock, flags);
  1743. - mmc_request_done(host->mmc, mrq);
  1744. -}
  1745. + if (terminate_chan)
  1746. + {
  1747. + int err = dmaengine_terminate_all(terminate_chan);
  1748. + if (err)
  1749. + pr_err("%s: failed to terminate DMA (%d)\n",
  1750. + mmc_hostname(host->mmc), err);
  1751. + }
  1752. + mmc_request_done(host->mmc, mrq);
  1753. + log_event("TSK>", (u32)mrq, 0);
  1754. +}
  1755. int bcm2835_sdhost_add_host(struct bcm2835_host *host)
  1756. {
  1757. @@ -1709,10 +1798,10 @@ int bcm2835_sdhost_add_host(struct bcm28
  1758. mmc->f_max, mmc->f_min, mmc->max_busy_timeout);
  1759. /* host controller capabilities */
  1760. - mmc->caps |= /* MMC_CAP_SDIO_IRQ |*/ MMC_CAP_4_BIT_DATA |
  1761. + mmc->caps |=
  1762. MMC_CAP_SD_HIGHSPEED | MMC_CAP_MMC_HIGHSPEED |
  1763. MMC_CAP_NEEDS_POLL | MMC_CAP_HW_RESET | MMC_CAP_ERASE |
  1764. - (ALLOW_CMD23 * MMC_CAP_CMD23);
  1765. + ((ALLOW_CMD23_READ|ALLOW_CMD23_WRITE) * MMC_CAP_CMD23);
  1766. spin_lock_init(&host->lock);
  1767. @@ -1722,9 +1811,9 @@ int bcm2835_sdhost_add_host(struct bcm28
  1768. pr_err("%s: unable to initialise DMA channels. "
  1769. "Falling back to PIO\n",
  1770. mmc_hostname(mmc));
  1771. - host->have_dma = false;
  1772. + host->use_dma = false;
  1773. } else {
  1774. - host->have_dma = true;
  1775. + host->use_dma = true;
  1776. cfg.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  1777. cfg.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  1778. @@ -1741,7 +1830,7 @@ int bcm2835_sdhost_add_host(struct bcm28
  1779. ret = dmaengine_slave_config(host->dma_chan_rx, &cfg);
  1780. }
  1781. } else {
  1782. - host->have_dma = false;
  1783. + host->use_dma = false;
  1784. }
  1785. mmc->max_segs = 128;
  1786. @@ -1756,16 +1845,15 @@ int bcm2835_sdhost_add_host(struct bcm28
  1787. tasklet_init(&host->finish_tasklet,
  1788. bcm2835_sdhost_tasklet_finish, (unsigned long)host);
  1789. - setup_timer(&host->timer, bcm2835_sdhost_timeout,
  1790. - (unsigned long)host);
  1791. + INIT_WORK(&host->cmd_wait_wq, bcm2835_sdhost_cmd_wait_work);
  1792. - setup_timer(&host->pio_timer, bcm2835_sdhost_pio_timeout,
  1793. + setup_timer(&host->timer, bcm2835_sdhost_timeout,
  1794. (unsigned long)host);
  1795. bcm2835_sdhost_init(host, 0);
  1796. - ret = request_threaded_irq(host->irq, bcm2835_sdhost_irq,
  1797. - bcm2835_sdhost_thread_irq,
  1798. - IRQF_SHARED, mmc_hostname(mmc), host);
  1799. +
  1800. + ret = request_irq(host->irq, bcm2835_sdhost_irq, 0 /*IRQF_SHARED*/,
  1801. + mmc_hostname(mmc), host);
  1802. if (ret) {
  1803. pr_err("%s: failed to request IRQ %d: %d\n",
  1804. mmc_hostname(mmc), host->irq, ret);
  1805. @@ -1776,11 +1864,11 @@ int bcm2835_sdhost_add_host(struct bcm28
  1806. mmc_add_host(mmc);
  1807. pio_limit_string[0] = '\0';
  1808. - if (host->have_dma && (host->pio_limit > 0))
  1809. + if (host->use_dma && (host->pio_limit > 0))
  1810. sprintf(pio_limit_string, " (>%d)", host->pio_limit);
  1811. pr_info("%s: %s loaded - DMA %s%s\n",
  1812. mmc_hostname(mmc), DRIVER_NAME,
  1813. - host->have_dma ? "enabled" : "disabled",
  1814. + host->use_dma ? "enabled" : "disabled",
  1815. pio_limit_string);
  1816. return 0;
  1817. @@ -1810,8 +1898,11 @@ static int bcm2835_sdhost_probe(struct p
  1818. mmc->ops = &bcm2835_sdhost_ops;
  1819. host = mmc_priv(mmc);
  1820. host->mmc = mmc;
  1821. + host->cmd_quick_poll_retries = 0;
  1822. host->pio_timeout = msecs_to_jiffies(500);
  1823. + host->pio_limit = 1;
  1824. host->max_delay = 1; /* Warn if over 1ms */
  1825. + host->allow_dma = 1;
  1826. spin_lock_init(&host->lock);
  1827. iomem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1828. @@ -1827,13 +1918,12 @@ static int bcm2835_sdhost_probe(struct p
  1829. return -ENODEV;
  1830. }
  1831. host->bus_addr = be32_to_cpup(addr);
  1832. + log_init(iomem->start - host->bus_addr);
  1833. pr_debug(" - ioaddr %lx, iomem->start %lx, bus_addr %lx\n",
  1834. (unsigned long)host->ioaddr,
  1835. (unsigned long)iomem->start,
  1836. (unsigned long)host->bus_addr);
  1837. - host->allow_dma = ALLOW_DMA;
  1838. -
  1839. if (node) {
  1840. /* Read any custom properties */
  1841. of_property_read_u32(node,
  1842. @@ -1845,16 +1935,17 @@ static int bcm2835_sdhost_probe(struct p
  1843. of_property_read_u32(node,
  1844. "brcm,pio-limit",
  1845. &host->pio_limit);
  1846. - host->allow_dma = ALLOW_DMA &&
  1847. + host->allow_dma =
  1848. !of_property_read_bool(node, "brcm,force-pio");
  1849. host->debug = of_property_read_bool(node, "brcm,debug");
  1850. - of_property_read_u32(node,
  1851. - "brcm,debug-flags",
  1852. - &host->debug_flags);
  1853. }
  1854. - if (host->debug_flags)
  1855. - dev_err(dev, "debug_flags=%x\n", host->debug_flags);
  1856. + host->dma_chan = NULL;
  1857. + host->dma_desc = NULL;
  1858. +
  1859. + /* Formally recognise the other way of disabling DMA */
  1860. + if (host->pio_limit == 0x7fffffff)
  1861. + host->allow_dma = false;
  1862. if (host->allow_dma) {
  1863. if (node) {
  1864. @@ -1940,15 +2031,12 @@ static int bcm2835_sdhost_remove(struct
  1865. return 0;
  1866. }
  1867. -
  1868. static const struct of_device_id bcm2835_sdhost_match[] = {
  1869. { .compatible = "brcm,bcm2835-sdhost" },
  1870. { }
  1871. };
  1872. MODULE_DEVICE_TABLE(of, bcm2835_sdhost_match);
  1873. -
  1874. -
  1875. static struct platform_driver bcm2835_sdhost_driver = {
  1876. .probe = bcm2835_sdhost_probe,
  1877. .remove = bcm2835_sdhost_remove,